]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/wireless/ti/wlcore/cmd.c
mac80211: remove probe response temporary buffer allocation
[mirror_ubuntu-bionic-kernel.git] / drivers / net / wireless / ti / wlcore / cmd.c
CommitLineData
f5fc0f86
LC
1/*
2 * This file is part of wl1271
3 *
2f826f55 4 * Copyright (C) 2009-2010 Nokia Corporation
f5fc0f86
LC
5 *
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 *
22 */
23
24#include <linux/module.h>
25#include <linux/platform_device.h>
f5fc0f86
LC
26#include <linux/spi/spi.h>
27#include <linux/etherdevice.h>
023e0826 28#include <linux/ieee80211.h>
5a0e3ad6 29#include <linux/slab.h>
f5fc0f86 30
c31be25a 31#include "wlcore.h"
0f4e3122 32#include "debug.h"
00d20100
SL
33#include "io.h"
34#include "acx.h"
f5fc0f86 35#include "wl12xx_80211.h"
00d20100
SL
36#include "cmd.h"
37#include "event.h"
98bdaabb 38#include "tx.h"
60462b48 39#include "hw_ops.h"
f5fc0f86 40
16092b5c 41#define WL1271_CMD_FAST_POLL_COUNT 50
c45ee4ff 42#define WL1271_WAIT_EVENT_FAST_POLL_COUNT 20
f5fc0f86
LC
43
44/*
45 * send command to firmware
46 *
47 * @wl: wl struct
48 * @id: command id
49 * @buf: buffer containing the command, must work with dma
50 * @len: length of the buffer
51 */
fa867e73
JO
52int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len,
53 size_t res_len)
f5fc0f86
LC
54{
55 struct wl1271_cmd_header *cmd;
56 unsigned long timeout;
57 u32 intr;
58 int ret = 0;
ad150e96 59 u16 status;
bc0f03ea 60 u16 poll_count = 0;
f5fc0f86 61
4cc53383
IY
62 if (WARN_ON(unlikely(wl->state == WLCORE_STATE_RESTARTING)))
63 return -EIO;
64
f5fc0f86 65 cmd = buf;
d0f63b20 66 cmd->id = cpu_to_le16(id);
f5fc0f86
LC
67 cmd->status = 0;
68
69 WARN_ON(len % 4 != 0);
24225b37 70 WARN_ON(test_bit(WL1271_FLAG_IN_ELP, &wl->flags));
f5fc0f86 71
eb96f841
IY
72 ret = wlcore_write(wl, wl->cmd_box_addr, buf, len, false);
73 if (ret < 0)
74 goto fail;
f5fc0f86 75
f16ff758
LC
76 /*
77 * TODO: we just need this because one bit is in a different
78 * place. Is there any better way?
79 */
eb96f841
IY
80 ret = wl->ops->trigger_cmd(wl, wl->cmd_box_addr, buf, len);
81 if (ret < 0)
82 goto fail;
f5fc0f86
LC
83
84 timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
85
6134323f
IY
86 ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &intr);
87 if (ret < 0)
88 goto fail;
89
f5fc0f86
LC
90 while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
91 if (time_after(jiffies, timeout)) {
92 wl1271_error("command complete timeout");
93 ret = -ETIMEDOUT;
f482b762 94 goto fail;
f5fc0f86
LC
95 }
96
bc0f03ea 97 poll_count++;
16092b5c
JO
98 if (poll_count < WL1271_CMD_FAST_POLL_COUNT)
99 udelay(10);
100 else
101 msleep(1);
f5fc0f86 102
6134323f
IY
103 ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &intr);
104 if (ret < 0)
105 goto fail;
f5fc0f86
LC
106 }
107
3b775b4b 108 /* read back the status code of the command */
fa867e73
JO
109 if (res_len == 0)
110 res_len = sizeof(struct wl1271_cmd_header);
045b9b5f
IY
111
112 ret = wlcore_read(wl, wl->cmd_box_addr, cmd, res_len, false);
113 if (ret < 0)
114 goto fail;
3b775b4b 115
ad150e96
JO
116 status = le16_to_cpu(cmd->status);
117 if (status != CMD_STATUS_SUCCESS) {
118 wl1271_error("command execute failure %d", status);
3b775b4b 119 ret = -EIO;
f482b762 120 goto fail;
3b775b4b
JO
121 }
122
b0f0ad39
IY
123 ret = wlcore_write_reg(wl, REG_INTERRUPT_ACK,
124 WL1271_ACX_INTR_CMD_COMPLETE);
125 if (ret < 0)
126 goto fail;
127
f482b762 128 return 0;
f5fc0f86 129
f482b762 130fail:
baacb9ae 131 wl12xx_queue_recovery_work(wl);
f5fc0f86
LC
132 return ret;
133}
134
99d84c1d
LC
135/*
136 * Poll the mailbox event field until any of the bits in the mask is set or a
137 * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
138 */
6c15c1aa
ES
139static int wl1271_cmd_wait_for_event_or_timeout(struct wl1271 *wl,
140 u32 mask, bool *timeout)
99d84c1d 141{
690142e9
MG
142 u32 *events_vector;
143 u32 event;
6c15c1aa 144 unsigned long timeout_time;
c45ee4ff 145 u16 poll_count = 0;
690142e9
MG
146 int ret = 0;
147
6c15c1aa
ES
148 *timeout = false;
149
0230dfea
DC
150 events_vector = kmalloc(sizeof(*events_vector), GFP_KERNEL | GFP_DMA);
151 if (!events_vector)
152 return -ENOMEM;
99d84c1d 153
6c15c1aa 154 timeout_time = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
99d84c1d
LC
155
156 do {
6c15c1aa 157 if (time_after(jiffies, timeout_time)) {
05285cf9
AN
158 wl1271_debug(DEBUG_CMD, "timeout waiting for event %d",
159 (int)mask);
6c15c1aa 160 *timeout = true;
690142e9 161 goto out;
52b0e7a6 162 }
99d84c1d 163
c45ee4ff
YD
164 poll_count++;
165 if (poll_count < WL1271_WAIT_EVENT_FAST_POLL_COUNT)
166 usleep_range(50, 51);
167 else
168 usleep_range(1000, 5000);
99d84c1d
LC
169
170 /* read from both event fields */
045b9b5f
IY
171 ret = wlcore_read(wl, wl->mbox_ptr[0], events_vector,
172 sizeof(*events_vector), false);
173 if (ret < 0)
174 goto out;
175
690142e9 176 event = *events_vector & mask;
045b9b5f
IY
177
178 ret = wlcore_read(wl, wl->mbox_ptr[1], events_vector,
179 sizeof(*events_vector), false);
180 if (ret < 0)
181 goto out;
182
690142e9 183 event |= *events_vector & mask;
99d84c1d
LC
184 } while (!event);
185
690142e9
MG
186out:
187 kfree(events_vector);
188 return ret;
99d84c1d
LC
189}
190
05285cf9
AN
191static int wl1271_cmd_wait_for_event(struct wl1271 *wl, u32 mask)
192{
193 int ret;
6c15c1aa 194 bool timeout = false;
05285cf9 195
6c15c1aa
ES
196 ret = wl1271_cmd_wait_for_event_or_timeout(wl, mask, &timeout);
197 if (ret != 0 || timeout) {
baacb9ae 198 wl12xx_queue_recovery_work(wl);
05285cf9
AN
199 return ret;
200 }
201
202 return 0;
203}
204
784f694d
EP
205int wl12xx_cmd_role_enable(struct wl1271 *wl, u8 *addr, u8 role_type,
206 u8 *role_id)
f5fc0f86 207{
c690ec81
EP
208 struct wl12xx_cmd_role_enable *cmd;
209 int ret;
210
211 wl1271_debug(DEBUG_CMD, "cmd role enable");
212
213 if (WARN_ON(*role_id != WL12XX_INVALID_ROLE_ID))
214 return -EBUSY;
215
216 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
217 if (!cmd) {
218 ret = -ENOMEM;
219 goto out;
220 }
221
222 /* get role id */
223 cmd->role_id = find_first_zero_bit(wl->roles_map, WL12XX_MAX_ROLES);
224 if (cmd->role_id >= WL12XX_MAX_ROLES) {
225 ret = -EBUSY;
226 goto out_free;
227 }
228
784f694d 229 memcpy(cmd->mac_address, addr, ETH_ALEN);
c690ec81
EP
230 cmd->role_type = role_type;
231
232 ret = wl1271_cmd_send(wl, CMD_ROLE_ENABLE, cmd, sizeof(*cmd), 0);
233 if (ret < 0) {
234 wl1271_error("failed to initiate cmd role enable");
235 goto out_free;
236 }
237
238 __set_bit(cmd->role_id, wl->roles_map);
239 *role_id = cmd->role_id;
f5fc0f86 240
c690ec81
EP
241out_free:
242 kfree(cmd);
243
244out:
245 return ret;
246}
247
248int wl12xx_cmd_role_disable(struct wl1271 *wl, u8 *role_id)
249{
250 struct wl12xx_cmd_role_disable *cmd;
251 int ret;
252
253 wl1271_debug(DEBUG_CMD, "cmd role disable");
254
255 if (WARN_ON(*role_id == WL12XX_INVALID_ROLE_ID))
256 return -ENOENT;
257
258 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
259 if (!cmd) {
f5fc0f86
LC
260 ret = -ENOMEM;
261 goto out;
262 }
c690ec81
EP
263 cmd->role_id = *role_id;
264
265 ret = wl1271_cmd_send(wl, CMD_ROLE_DISABLE, cmd, sizeof(*cmd), 0);
266 if (ret < 0) {
267 wl1271_error("failed to initiate cmd role disable");
268 goto out_free;
269 }
f5fc0f86 270
c690ec81
EP
271 __clear_bit(*role_id, wl->roles_map);
272 *role_id = WL12XX_INVALID_ROLE_ID;
f5fc0f86 273
c690ec81
EP
274out_free:
275 kfree(cmd);
276
277out:
278 return ret;
279}
280
c7ffb902 281int wl12xx_allocate_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid)
c690ec81 282{
0b0e32b7 283 unsigned long flags;
c690ec81
EP
284 u8 link = find_first_zero_bit(wl->links_map, WL12XX_MAX_LINKS);
285 if (link >= WL12XX_MAX_LINKS)
286 return -EBUSY;
287
0b0e32b7
AN
288 /* these bits are used by op_tx */
289 spin_lock_irqsave(&wl->wl_lock, flags);
c690ec81 290 __set_bit(link, wl->links_map);
c7ffb902 291 __set_bit(link, wlvif->links_map);
0b0e32b7 292 spin_unlock_irqrestore(&wl->wl_lock, flags);
c690ec81
EP
293 *hlid = link;
294 return 0;
295}
296
c7ffb902 297void wl12xx_free_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid)
c690ec81 298{
0b0e32b7
AN
299 unsigned long flags;
300
c690ec81
EP
301 if (*hlid == WL12XX_INVALID_LINK_ID)
302 return;
303
0b0e32b7
AN
304 /* these bits are used by op_tx */
305 spin_lock_irqsave(&wl->wl_lock, flags);
c690ec81 306 __clear_bit(*hlid, wl->links_map);
c7ffb902 307 __clear_bit(*hlid, wlvif->links_map);
0b0e32b7 308 spin_unlock_irqrestore(&wl->wl_lock, flags);
6246ca00
AN
309
310 /*
311 * At this point op_tx() will not add more packets to the queues. We
312 * can purge them.
313 */
314 wl1271_tx_reset_link_queues(wl, *hlid);
315
c690ec81
EP
316 *hlid = WL12XX_INVALID_LINK_ID;
317}
318
98b86253
EP
319static int wl12xx_get_new_session_id(struct wl1271 *wl,
320 struct wl12xx_vif *wlvif)
712e9bf7 321{
98b86253
EP
322 if (wlvif->session_counter >= SESSION_COUNTER_MAX)
323 wlvif->session_counter = 0;
712e9bf7 324
98b86253 325 wlvif->session_counter++;
712e9bf7 326
98b86253 327 return wlvif->session_counter;
712e9bf7
AN
328}
329
a6298dbe
AN
330static u8 wlcore_get_native_channel_type(u8 nl_channel_type)
331{
332 switch (nl_channel_type) {
333 case NL80211_CHAN_NO_HT:
334 return WLCORE_CHAN_NO_HT;
335 case NL80211_CHAN_HT20:
336 return WLCORE_CHAN_HT20;
337 case NL80211_CHAN_HT40MINUS:
338 return WLCORE_CHAN_HT40MINUS;
339 case NL80211_CHAN_HT40PLUS:
340 return WLCORE_CHAN_HT40PLUS;
341 default:
342 WARN_ON(1);
343 return WLCORE_CHAN_NO_HT;
344 }
345}
346
679a6734
EP
347static int wl12xx_cmd_role_start_dev(struct wl1271 *wl,
348 struct wl12xx_vif *wlvif)
04e8079c
EP
349{
350 struct wl12xx_cmd_role_start *cmd;
351 int ret;
352
353 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
354 if (!cmd) {
355 ret = -ENOMEM;
356 goto out;
357 }
358
7edebf56 359 wl1271_debug(DEBUG_CMD, "cmd role start dev %d", wlvif->dev_role_id);
04e8079c 360
7edebf56 361 cmd->role_id = wlvif->dev_role_id;
1b92f15e 362 if (wlvif->band == IEEE80211_BAND_5GHZ)
00782136 363 cmd->band = WLCORE_BAND_5GHZ;
61f845f4 364 cmd->channel = wlvif->channel;
04e8079c 365
afaf8bdb 366 if (wlvif->dev_hlid == WL12XX_INVALID_LINK_ID) {
c7ffb902 367 ret = wl12xx_allocate_link(wl, wlvif, &wlvif->dev_hlid);
04e8079c
EP
368 if (ret)
369 goto out_free;
370 }
afaf8bdb 371 cmd->device.hlid = wlvif->dev_hlid;
b32b2b0f 372 cmd->device.session = wl12xx_get_new_session_id(wl, wlvif);
04e8079c
EP
373
374 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d",
375 cmd->role_id, cmd->device.hlid, cmd->device.session);
376
377 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
378 if (ret < 0) {
379 wl1271_error("failed to initiate cmd role enable");
380 goto err_hlid;
381 }
382
383 goto out_free;
384
385err_hlid:
386 /* clear links on error */
c7ffb902 387 wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid);
04e8079c
EP
388
389out_free:
390 kfree(cmd);
391
392out:
393 return ret;
394}
395
679a6734
EP
396static int wl12xx_cmd_role_stop_dev(struct wl1271 *wl,
397 struct wl12xx_vif *wlvif)
04e8079c
EP
398{
399 struct wl12xx_cmd_role_stop *cmd;
400 int ret;
401
afaf8bdb 402 if (WARN_ON(wlvif->dev_hlid == WL12XX_INVALID_LINK_ID))
04e8079c
EP
403 return -EINVAL;
404
405 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
406 if (!cmd) {
407 ret = -ENOMEM;
408 goto out;
409 }
410
411 wl1271_debug(DEBUG_CMD, "cmd role stop dev");
412
7edebf56 413 cmd->role_id = wlvif->dev_role_id;
04e8079c
EP
414 cmd->disc_type = DISCONNECT_IMMEDIATE;
415 cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
416
417 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
418 if (ret < 0) {
419 wl1271_error("failed to initiate cmd role stop");
420 goto out_free;
421 }
422
8332f0f6 423 ret = wl1271_cmd_wait_for_event(wl, ROLE_STOP_COMPLETE_EVENT_ID);
04e8079c
EP
424 if (ret < 0) {
425 wl1271_error("cmd role stop dev event completion error");
426 goto out_free;
427 }
428
c7ffb902 429 wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid);
04e8079c
EP
430
431out_free:
432 kfree(cmd);
433
434out:
435 return ret;
436}
437
87fbcb0f 438int wl12xx_cmd_role_start_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
c690ec81 439{
cdf09495 440 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
c690ec81
EP
441 struct wl12xx_cmd_role_start *cmd;
442 int ret;
f5fc0f86 443
c690ec81
EP
444 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
445 if (!cmd) {
446 ret = -ENOMEM;
447 goto out;
448 }
f5fc0f86 449
0603d891 450 wl1271_debug(DEBUG_CMD, "cmd role start sta %d", wlvif->role_id);
c690ec81 451
0603d891 452 cmd->role_id = wlvif->role_id;
1b92f15e 453 if (wlvif->band == IEEE80211_BAND_5GHZ)
00782136 454 cmd->band = WLCORE_BAND_5GHZ;
61f845f4 455 cmd->channel = wlvif->channel;
87fbcb0f 456 cmd->sta.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
6a899796 457 cmd->sta.beacon_interval = cpu_to_le16(wlvif->beacon_int);
c690ec81 458 cmd->sta.ssid_type = WL12XX_SSID_TYPE_ANY;
1fe9f161
EP
459 cmd->sta.ssid_len = wlvif->ssid_len;
460 memcpy(cmd->sta.ssid, wlvif->ssid, wlvif->ssid_len);
cdf09495 461 memcpy(cmd->sta.bssid, vif->bss_conf.bssid, ETH_ALEN);
30d0c8fd 462 cmd->sta.local_rates = cpu_to_le32(wlvif->rate_set);
a6298dbe 463 cmd->channel_type = wlcore_get_native_channel_type(wlvif->channel_type);
c690ec81 464
154da67c 465 if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
c7ffb902 466 ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid);
c690ec81
EP
467 if (ret)
468 goto out_free;
469 }
154da67c 470 cmd->sta.hlid = wlvif->sta.hlid;
98b86253 471 cmd->sta.session = wl12xx_get_new_session_id(wl, wlvif);
30d0c8fd 472 cmd->sta.remote_rates = cpu_to_le32(wlvif->rate_set);
c690ec81
EP
473
474 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
475 "basic_rate_set: 0x%x, remote_rates: 0x%x",
0603d891 476 wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
30d0c8fd 477 wlvif->basic_rate_set, wlvif->rate_set);
c690ec81
EP
478
479 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
480 if (ret < 0) {
481 wl1271_error("failed to initiate cmd role start sta");
482 goto err_hlid;
483 }
484
485 goto out_free;
486
487err_hlid:
488 /* clear links on error. */
c7ffb902 489 wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
c690ec81
EP
490
491out_free:
492 kfree(cmd);
493
494out:
495 return ret;
496}
f5fc0f86 497
31cd3aed 498/* use this function to stop ibss as well */
0603d891 499int wl12xx_cmd_role_stop_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
c690ec81
EP
500{
501 struct wl12xx_cmd_role_stop *cmd;
502 int ret;
5285eb54 503 bool timeout = false;
c690ec81 504
154da67c 505 if (WARN_ON(wlvif->sta.hlid == WL12XX_INVALID_LINK_ID))
c690ec81
EP
506 return -EINVAL;
507
508 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
509 if (!cmd) {
510 ret = -ENOMEM;
511 goto out;
512 }
a4102645 513
0603d891 514 wl1271_debug(DEBUG_CMD, "cmd role stop sta %d", wlvif->role_id);
f5fc0f86 515
0603d891 516 cmd->role_id = wlvif->role_id;
c690ec81
EP
517 cmd->disc_type = DISCONNECT_IMMEDIATE;
518 cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
f5fc0f86 519
c690ec81
EP
520 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
521 if (ret < 0) {
522 wl1271_error("failed to initiate cmd role stop sta");
523 goto out_free;
524 }
72c2d9e5 525
5285eb54
LC
526 /*
527 * Sometimes the firmware doesn't send this event, so we just
528 * time out without failing. Queue recovery for other
529 * failures.
530 */
531 ret = wl1271_cmd_wait_for_event_or_timeout(wl,
532 ROLE_STOP_COMPLETE_EVENT_ID,
533 &timeout);
534 if (ret)
535 wl12xx_queue_recovery_work(wl);
536
c7ffb902 537 wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
c690ec81
EP
538
539out_free:
540 kfree(cmd);
541
542out:
543 return ret;
544}
545
87fbcb0f 546int wl12xx_cmd_role_start_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
c690ec81
EP
547{
548 struct wl12xx_cmd_role_start *cmd;
6e8cd331
EP
549 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
550 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
60462b48 551 u32 supported_rates;
c690ec81
EP
552 int ret;
553
0603d891 554 wl1271_debug(DEBUG_CMD, "cmd role start ap %d", wlvif->role_id);
c690ec81 555
68eaaf6e 556 /* trying to use hidden SSID with an old hostapd version */
1fe9f161 557 if (wlvif->ssid_len == 0 && !bss_conf->hidden_ssid) {
68eaaf6e 558 wl1271_error("got a null SSID from beacon/bss");
c690ec81
EP
559 ret = -EINVAL;
560 goto out;
561 }
562
563 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
564 if (!cmd) {
565 ret = -ENOMEM;
566 goto out;
567 }
568
c7ffb902 569 ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.global_hlid);
e51ae9be
AN
570 if (ret < 0)
571 goto out_free;
572
c7ffb902 573 ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.bcast_hlid);
e51ae9be
AN
574 if (ret < 0)
575 goto out_free_global;
576
0603d891 577 cmd->role_id = wlvif->role_id;
c690ec81
EP
578 cmd->ap.aging_period = cpu_to_le16(wl->conf.tx.ap_aging_period);
579 cmd->ap.bss_index = WL1271_AP_BSS_INDEX;
a8ab39a4
EP
580 cmd->ap.global_hlid = wlvif->ap.global_hlid;
581 cmd->ap.broadcast_hlid = wlvif->ap.bcast_hlid;
87fbcb0f 582 cmd->ap.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
6a899796 583 cmd->ap.beacon_interval = cpu_to_le16(wlvif->beacon_int);
c690ec81
EP
584 cmd->ap.dtim_interval = bss_conf->dtim_period;
585 cmd->ap.beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
8332f0f6
EP
586 /* FIXME: Change when adding DFS */
587 cmd->ap.reset_tsf = 1; /* By default reset AP TSF */
61f845f4 588 cmd->channel = wlvif->channel;
a6298dbe 589 cmd->channel_type = wlcore_get_native_channel_type(wlvif->channel_type);
68eaaf6e
AN
590
591 if (!bss_conf->hidden_ssid) {
592 /* take the SSID from the beacon for backward compatibility */
593 cmd->ap.ssid_type = WL12XX_SSID_TYPE_PUBLIC;
1fe9f161
EP
594 cmd->ap.ssid_len = wlvif->ssid_len;
595 memcpy(cmd->ap.ssid, wlvif->ssid, wlvif->ssid_len);
68eaaf6e
AN
596 } else {
597 cmd->ap.ssid_type = WL12XX_SSID_TYPE_HIDDEN;
598 cmd->ap.ssid_len = bss_conf->ssid_len;
599 memcpy(cmd->ap.ssid, bss_conf->ssid, bss_conf->ssid_len);
600 }
601
60462b48
LC
602 supported_rates = CONF_TX_AP_ENABLED_RATES | CONF_TX_MCS_RATES |
603 wlcore_hw_ap_get_mimo_wide_rate_mask(wl, wlvif);
604
605 wl1271_debug(DEBUG_CMD, "cmd role start ap with supported_rates 0x%08x",
606 supported_rates);
607
608 cmd->ap.local_rates = cpu_to_le32(supported_rates);
c690ec81 609
1b92f15e 610 switch (wlvif->band) {
c690ec81 611 case IEEE80211_BAND_2GHZ:
00782136 612 cmd->band = WLCORE_BAND_2_4GHZ;
c690ec81
EP
613 break;
614 case IEEE80211_BAND_5GHZ:
00782136 615 cmd->band = WLCORE_BAND_5GHZ;
c690ec81
EP
616 break;
617 default:
1b92f15e 618 wl1271_warning("ap start - unknown band: %d", (int)wlvif->band);
00782136 619 cmd->band = WLCORE_BAND_2_4GHZ;
c690ec81
EP
620 break;
621 }
622
623 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
624 if (ret < 0) {
625 wl1271_error("failed to initiate cmd role start ap");
e51ae9be 626 goto out_free_bcast;
c690ec81 627 }
f5fc0f86 628
e51ae9be
AN
629 goto out_free;
630
631out_free_bcast:
c7ffb902 632 wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
e51ae9be
AN
633
634out_free_global:
c7ffb902 635 wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
e51ae9be 636
f5fc0f86 637out_free:
c690ec81 638 kfree(cmd);
f5fc0f86
LC
639
640out:
641 return ret;
642}
643
0603d891 644int wl12xx_cmd_role_stop_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
c690ec81
EP
645{
646 struct wl12xx_cmd_role_stop *cmd;
647 int ret;
648
649 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
650 if (!cmd) {
651 ret = -ENOMEM;
652 goto out;
653 }
654
0603d891 655 wl1271_debug(DEBUG_CMD, "cmd role stop ap %d", wlvif->role_id);
c690ec81 656
0603d891 657 cmd->role_id = wlvif->role_id;
c690ec81
EP
658
659 ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
660 if (ret < 0) {
661 wl1271_error("failed to initiate cmd role stop ap");
662 goto out_free;
663 }
664
c7ffb902
EP
665 wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
666 wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
e51ae9be 667
c690ec81
EP
668out_free:
669 kfree(cmd);
670
671out:
672 return ret;
673}
674
87fbcb0f 675int wl12xx_cmd_role_start_ibss(struct wl1271 *wl, struct wl12xx_vif *wlvif)
31cd3aed 676{
cdf09495 677 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
31cd3aed 678 struct wl12xx_cmd_role_start *cmd;
6e8cd331 679 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
31cd3aed
EP
680 int ret;
681
682 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
683 if (!cmd) {
684 ret = -ENOMEM;
685 goto out;
686 }
687
0603d891 688 wl1271_debug(DEBUG_CMD, "cmd role start ibss %d", wlvif->role_id);
31cd3aed 689
0603d891 690 cmd->role_id = wlvif->role_id;
1b92f15e 691 if (wlvif->band == IEEE80211_BAND_5GHZ)
00782136 692 cmd->band = WLCORE_BAND_5GHZ;
61f845f4 693 cmd->channel = wlvif->channel;
87fbcb0f 694 cmd->ibss.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
6a899796 695 cmd->ibss.beacon_interval = cpu_to_le16(wlvif->beacon_int);
31cd3aed
EP
696 cmd->ibss.dtim_interval = bss_conf->dtim_period;
697 cmd->ibss.ssid_type = WL12XX_SSID_TYPE_ANY;
1fe9f161
EP
698 cmd->ibss.ssid_len = wlvif->ssid_len;
699 memcpy(cmd->ibss.ssid, wlvif->ssid, wlvif->ssid_len);
cdf09495 700 memcpy(cmd->ibss.bssid, vif->bss_conf.bssid, ETH_ALEN);
30d0c8fd 701 cmd->sta.local_rates = cpu_to_le32(wlvif->rate_set);
31cd3aed 702
154da67c 703 if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
c7ffb902 704 ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid);
31cd3aed
EP
705 if (ret)
706 goto out_free;
707 }
154da67c 708 cmd->ibss.hlid = wlvif->sta.hlid;
30d0c8fd 709 cmd->ibss.remote_rates = cpu_to_le32(wlvif->rate_set);
31cd3aed
EP
710
711 wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
712 "basic_rate_set: 0x%x, remote_rates: 0x%x",
0603d891 713 wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
30d0c8fd 714 wlvif->basic_rate_set, wlvif->rate_set);
31cd3aed 715
cdf09495
EP
716 wl1271_debug(DEBUG_CMD, "vif->bss_conf.bssid = %pM",
717 vif->bss_conf.bssid);
31cd3aed
EP
718
719 ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
720 if (ret < 0) {
721 wl1271_error("failed to initiate cmd role enable");
722 goto err_hlid;
723 }
724
725 goto out_free;
726
727err_hlid:
728 /* clear links on error. */
c7ffb902 729 wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
31cd3aed
EP
730
731out_free:
732 kfree(cmd);
733
734out:
735 return ret;
736}
737
c690ec81 738
f5fc0f86
LC
739/**
740 * send test command to firmware
741 *
742 * @wl: wl struct
743 * @buf: buffer containing the command, with all headers, must work with dma
744 * @len: length of the buffer
745 * @answer: is answer needed
746 */
747int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
748{
749 int ret;
fa867e73 750 size_t res_len = 0;
f5fc0f86
LC
751
752 wl1271_debug(DEBUG_CMD, "cmd test");
753
fa867e73
JO
754 if (answer)
755 res_len = buf_len;
756
757 ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
f5fc0f86
LC
758
759 if (ret < 0) {
760 wl1271_warning("TEST command failed");
761 return ret;
762 }
763
fa867e73 764 return ret;
f5fc0f86 765}
9d68d1ee 766EXPORT_SYMBOL_GPL(wl1271_cmd_test);
f5fc0f86
LC
767
768/**
769 * read acx from firmware
770 *
771 * @wl: wl struct
772 * @id: acx id
773 * @buf: buffer for the response, including all headers, must work with dma
25985edc 774 * @len: length of buf
f5fc0f86
LC
775 */
776int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
777{
778 struct acx_header *acx = buf;
779 int ret;
780
781 wl1271_debug(DEBUG_CMD, "cmd interrogate");
782
d0f63b20 783 acx->id = cpu_to_le16(id);
f5fc0f86
LC
784
785 /* payload length, does not include any headers */
d0f63b20 786 acx->len = cpu_to_le16(len - sizeof(*acx));
f5fc0f86 787
fa867e73
JO
788 ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
789 if (ret < 0)
f5fc0f86 790 wl1271_error("INTERROGATE command failed");
f5fc0f86 791
f5fc0f86
LC
792 return ret;
793}
794
795/**
796 * write acx value to firmware
797 *
798 * @wl: wl struct
799 * @id: acx id
800 * @buf: buffer containing acx, including all headers, must work with dma
801 * @len: length of buf
802 */
803int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
804{
805 struct acx_header *acx = buf;
806 int ret;
807
c91d0600 808 wl1271_debug(DEBUG_CMD, "cmd configure (%d)", id);
f5fc0f86 809
d0f63b20 810 acx->id = cpu_to_le16(id);
f5fc0f86
LC
811
812 /* payload length, does not include any headers */
d0f63b20 813 acx->len = cpu_to_le16(len - sizeof(*acx));
f5fc0f86 814
fa867e73 815 ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
f5fc0f86
LC
816 if (ret < 0) {
817 wl1271_warning("CONFIGURE command NOK");
818 return ret;
819 }
820
821 return 0;
822}
9d68d1ee 823EXPORT_SYMBOL_GPL(wl1271_cmd_configure);
f5fc0f86 824
94210897 825int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
f5fc0f86
LC
826{
827 struct cmd_enabledisable_path *cmd;
828 int ret;
829 u16 cmd_rx, cmd_tx;
830
831 wl1271_debug(DEBUG_CMD, "cmd data path");
832
833 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
834 if (!cmd) {
835 ret = -ENOMEM;
836 goto out;
837 }
838
94210897
LC
839 /* the channel here is only used for calibration, so hardcoded to 1 */
840 cmd->channel = 1;
f5fc0f86
LC
841
842 if (enable) {
843 cmd_rx = CMD_ENABLE_RX;
844 cmd_tx = CMD_ENABLE_TX;
845 } else {
846 cmd_rx = CMD_DISABLE_RX;
847 cmd_tx = CMD_DISABLE_TX;
848 }
849
fa867e73 850 ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
f5fc0f86
LC
851 if (ret < 0) {
852 wl1271_error("rx %s cmd for channel %d failed",
94210897 853 enable ? "start" : "stop", cmd->channel);
f5fc0f86
LC
854 goto out;
855 }
856
857 wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
94210897 858 enable ? "start" : "stop", cmd->channel);
f5fc0f86 859
fa867e73 860 ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
f5fc0f86
LC
861 if (ret < 0) {
862 wl1271_error("tx %s cmd for channel %d failed",
94210897 863 enable ? "start" : "stop", cmd->channel);
1b00f2b5 864 goto out;
f5fc0f86
LC
865 }
866
867 wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
94210897 868 enable ? "start" : "stop", cmd->channel);
f5fc0f86
LC
869
870out:
871 kfree(cmd);
872 return ret;
873}
c331b344 874EXPORT_SYMBOL_GPL(wl1271_cmd_data_path);
f5fc0f86 875
0603d891 876int wl1271_cmd_ps_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
f1d63a59 877 u8 ps_mode, u16 auto_ps_timeout)
f5fc0f86
LC
878{
879 struct wl1271_cmd_ps_params *ps_params = NULL;
880 int ret = 0;
881
f5fc0f86
LC
882 wl1271_debug(DEBUG_CMD, "cmd set ps mode");
883
884 ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
885 if (!ps_params) {
886 ret = -ENOMEM;
887 goto out;
888 }
889
0603d891 890 ps_params->role_id = wlvif->role_id;
f5fc0f86 891 ps_params->ps_mode = ps_mode;
f1d63a59 892 ps_params->auto_ps_timeout = auto_ps_timeout;
f5fc0f86
LC
893
894 ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
fa867e73 895 sizeof(*ps_params), 0);
f5fc0f86
LC
896 if (ret < 0) {
897 wl1271_error("cmd set_ps_mode failed");
898 goto out;
899 }
900
901out:
902 kfree(ps_params);
903 return ret;
904}
905
cdaac628
EP
906int wl1271_cmd_template_set(struct wl1271 *wl, u8 role_id,
907 u16 template_id, void *buf, size_t buf_len,
908 int index, u32 rates)
f5fc0f86
LC
909{
910 struct wl1271_cmd_template_set *cmd;
911 int ret = 0;
912
c059beb2
EP
913 wl1271_debug(DEBUG_CMD, "cmd template_set %d (role %d)",
914 template_id, role_id);
f5fc0f86
LC
915
916 WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
917 buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
918
919 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
920 if (!cmd) {
921 ret = -ENOMEM;
922 goto out;
923 }
924
cdaac628
EP
925 /* during initialization wlvif is NULL */
926 cmd->role_id = role_id;
f5fc0f86
LC
927 cmd->len = cpu_to_le16(buf_len);
928 cmd->template_type = template_id;
606c1487 929 cmd->enabled_rates = cpu_to_le32(rates);
1e05a818
AN
930 cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
931 cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
bfb24c9e 932 cmd->index = index;
f5fc0f86
LC
933
934 if (buf)
935 memcpy(cmd->template_data, buf, buf_len);
936
fa867e73 937 ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
f5fc0f86
LC
938 if (ret < 0) {
939 wl1271_warning("cmd set_template failed: %d", ret);
940 goto out_free;
941 }
942
943out_free:
944 kfree(cmd);
945
946out:
947 return ret;
948}
949
d2d66c56 950int wl12xx_cmd_build_null_data(struct wl1271 *wl, struct wl12xx_vif *wlvif)
f5fc0f86 951{
a0cb7be4
JO
952 struct sk_buff *skb = NULL;
953 int size;
954 void *ptr;
955 int ret = -ENOMEM;
f5fc0f86 956
f5fc0f86 957
536129c8 958 if (wlvif->bss_type == BSS_TYPE_IBSS) {
a0cb7be4
JO
959 size = sizeof(struct wl12xx_null_data_template);
960 ptr = NULL;
961 } else {
d2d66c56
EP
962 skb = ieee80211_nullfunc_get(wl->hw,
963 wl12xx_wlvif_to_vif(wlvif));
a0cb7be4
JO
964 if (!skb)
965 goto out;
966 size = skb->len;
967 ptr = skb->data;
968 }
969
cdaac628
EP
970 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
971 CMD_TEMPL_NULL_DATA, ptr, size, 0,
d2d66c56 972 wlvif->basic_rate);
f5fc0f86 973
899e6e65
KV
974out:
975 dev_kfree_skb(skb);
a0cb7be4
JO
976 if (ret)
977 wl1271_warning("cmd buld null data failed %d", ret);
978
899e6e65 979 return ret;
f5fc0f86
LC
980
981}
982
d2d66c56
EP
983int wl12xx_cmd_build_klv_null_data(struct wl1271 *wl,
984 struct wl12xx_vif *wlvif)
bfb24c9e 985{
d2d66c56 986 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
bfb24c9e
JO
987 struct sk_buff *skb = NULL;
988 int ret = -ENOMEM;
989
d2d66c56 990 skb = ieee80211_nullfunc_get(wl->hw, vif);
bfb24c9e
JO
991 if (!skb)
992 goto out;
993
cdaac628 994 ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_KLV,
bfb24c9e 995 skb->data, skb->len,
001e39a8 996 wlvif->sta.klv_template_id,
d2d66c56 997 wlvif->basic_rate);
bfb24c9e
JO
998
999out:
1000 dev_kfree_skb(skb);
1001 if (ret)
1002 wl1271_warning("cmd build klv null data failed %d", ret);
1003
1004 return ret;
1005
1006}
1007
87fbcb0f
EP
1008int wl1271_cmd_build_ps_poll(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1009 u16 aid)
f5fc0f86 1010{
6e8cd331 1011 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
899e6e65
KV
1012 struct sk_buff *skb;
1013 int ret = 0;
c3fea199 1014
6e8cd331 1015 skb = ieee80211_pspoll_get(wl->hw, vif);
899e6e65
KV
1016 if (!skb)
1017 goto out;
f5fc0f86 1018
cdaac628
EP
1019 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1020 CMD_TEMPL_PS_POLL, skb->data,
87fbcb0f 1021 skb->len, 0, wlvif->basic_rate_set);
f5fc0f86 1022
899e6e65
KV
1023out:
1024 dev_kfree_skb(skb);
1025 return ret;
f5fc0f86
LC
1026}
1027
cdaac628
EP
1028int wl12xx_cmd_build_probe_req(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1029 u8 role_id, u8 band,
818e3063 1030 const u8 *ssid, size_t ssid_len,
3df74f46 1031 const u8 *ie, size_t ie_len, bool sched_scan)
f5fc0f86 1032{
83587505 1033 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
818e3063 1034 struct sk_buff *skb;
abb0b3bf 1035 int ret;
af7fbb28 1036 u32 rate;
3df74f46
YD
1037 u16 template_id_2_4 = CMD_TEMPL_CFG_PROBE_REQ_2_4;
1038 u16 template_id_5 = CMD_TEMPL_CFG_PROBE_REQ_5;
f5fc0f86 1039
83587505 1040 skb = ieee80211_probereq_get(wl->hw, vif, ssid, ssid_len,
b9a9ada1 1041 ie_len);
818e3063
KV
1042 if (!skb) {
1043 ret = -ENOMEM;
1044 goto out;
1045 }
b9a9ada1
JB
1046 if (ie_len)
1047 memcpy(skb_put(skb, ie_len), ie, ie_len);
818e3063
KV
1048
1049 wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
f5fc0f86 1050
3df74f46
YD
1051 if (!sched_scan &&
1052 (wl->quirks & WLCORE_QUIRK_DUAL_PROBE_TMPL)) {
1053 template_id_2_4 = CMD_TEMPL_APP_PROBE_REQ_2_4;
1054 template_id_5 = CMD_TEMPL_APP_PROBE_REQ_5;
1055 }
1056
83587505 1057 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
abb0b3bf 1058 if (band == IEEE80211_BAND_2GHZ)
cdaac628 1059 ret = wl1271_cmd_template_set(wl, role_id,
3df74f46 1060 template_id_2_4,
af7fbb28 1061 skb->data, skb->len, 0, rate);
abb0b3bf 1062 else
cdaac628 1063 ret = wl1271_cmd_template_set(wl, role_id,
3df74f46 1064 template_id_5,
af7fbb28 1065 skb->data, skb->len, 0, rate);
818e3063
KV
1066
1067out:
1068 dev_kfree_skb(skb);
abb0b3bf 1069 return ret;
f5fc0f86
LC
1070}
1071
2f6724b2 1072struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
83587505 1073 struct wl12xx_vif *wlvif,
2f6724b2
JO
1074 struct sk_buff *skb)
1075{
83587505 1076 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
2f6724b2 1077 int ret;
af7fbb28 1078 u32 rate;
2f6724b2
JO
1079
1080 if (!skb)
83587505 1081 skb = ieee80211_ap_probereq_get(wl->hw, vif);
2f6724b2
JO
1082 if (!skb)
1083 goto out;
1084
1085 wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
1086
1b92f15e
EP
1087 rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[wlvif->band]);
1088 if (wlvif->band == IEEE80211_BAND_2GHZ)
cdaac628
EP
1089 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1090 CMD_TEMPL_CFG_PROBE_REQ_2_4,
af7fbb28 1091 skb->data, skb->len, 0, rate);
2f6724b2 1092 else
cdaac628
EP
1093 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
1094 CMD_TEMPL_CFG_PROBE_REQ_5,
af7fbb28 1095 skb->data, skb->len, 0, rate);
2f6724b2
JO
1096
1097 if (ret < 0)
1098 wl1271_error("Unable to set ap probe request template.");
1099
1100out:
1101 return skb;
1102}
1103
5ec8a448 1104int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, struct wl12xx_vif *wlvif)
c5312772 1105{
2c0133a4 1106 int ret, extra = 0;
5ec8a448 1107 u16 fc;
6e8cd331 1108 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
5ec8a448
EP
1109 struct sk_buff *skb;
1110 struct wl12xx_arp_rsp_template *tmpl;
c5312772
EP
1111 struct ieee80211_hdr_3addr *hdr;
1112 struct arphdr *arp_hdr;
1113
5ec8a448
EP
1114 skb = dev_alloc_skb(sizeof(*hdr) + sizeof(__le16) + sizeof(*tmpl) +
1115 WL1271_EXTRA_SPACE_MAX);
1116 if (!skb) {
1117 wl1271_error("failed to allocate buffer for arp rsp template");
1118 return -ENOMEM;
1119 }
c5312772 1120
5ec8a448
EP
1121 skb_reserve(skb, sizeof(*hdr) + WL1271_EXTRA_SPACE_MAX);
1122
1123 tmpl = (struct wl12xx_arp_rsp_template *)skb_put(skb, sizeof(*tmpl));
161f17b5 1124 memset(tmpl, 0, sizeof(*tmpl));
c5312772
EP
1125
1126 /* llc layer */
5ec8a448
EP
1127 memcpy(tmpl->llc_hdr, rfc1042_header, sizeof(rfc1042_header));
1128 tmpl->llc_type = cpu_to_be16(ETH_P_ARP);
c5312772
EP
1129
1130 /* arp header */
5ec8a448 1131 arp_hdr = &tmpl->arp_hdr;
6177eaea
EP
1132 arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
1133 arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
c5312772
EP
1134 arp_hdr->ar_hln = ETH_ALEN;
1135 arp_hdr->ar_pln = 4;
6177eaea 1136 arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
c5312772
EP
1137
1138 /* arp payload */
5ec8a448
EP
1139 memcpy(tmpl->sender_hw, vif->addr, ETH_ALEN);
1140 tmpl->sender_ip = wlvif->ip_addr;
1141
1142 /* encryption space */
1143 switch (wlvif->encryption_type) {
1144 case KEY_TKIP:
2c0133a4
AN
1145 if (wl->quirks & WLCORE_QUIRK_TKIP_HEADER_SPACE)
1146 extra = WL1271_EXTRA_SPACE_TKIP;
5ec8a448
EP
1147 break;
1148 case KEY_AES:
1149 extra = WL1271_EXTRA_SPACE_AES;
1150 break;
1151 case KEY_NONE:
1152 case KEY_WEP:
1153 case KEY_GEM:
1154 extra = 0;
1155 break;
1156 default:
1157 wl1271_warning("Unknown encryption type: %d",
1158 wlvif->encryption_type);
1159 ret = -EINVAL;
1160 goto out;
1161 }
1162
1163 if (extra) {
1164 u8 *space = skb_push(skb, extra);
1165 memset(space, 0, extra);
1166 }
1167
1168 /* QoS header - BE */
1169 if (wlvif->sta.qos)
1170 memset(skb_push(skb, sizeof(__le16)), 0, sizeof(__le16));
1171
1172 /* mac80211 header */
1173 hdr = (struct ieee80211_hdr_3addr *)skb_push(skb, sizeof(*hdr));
161f17b5 1174 memset(hdr, 0, sizeof(*hdr));
5ec8a448
EP
1175 fc = IEEE80211_FTYPE_DATA | IEEE80211_FCTL_TODS;
1176 if (wlvif->sta.qos)
1177 fc |= IEEE80211_STYPE_QOS_DATA;
1178 else
1179 fc |= IEEE80211_STYPE_DATA;
1180 if (wlvif->encryption_type != KEY_NONE)
1181 fc |= IEEE80211_FCTL_PROTECTED;
1182
1183 hdr->frame_control = cpu_to_le16(fc);
1184 memcpy(hdr->addr1, vif->bss_conf.bssid, ETH_ALEN);
1185 memcpy(hdr->addr2, vif->addr, ETH_ALEN);
1186 memset(hdr->addr3, 0xff, ETH_ALEN);
c5312772 1187
cdaac628 1188 ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_ARP_RSP,
5ec8a448 1189 skb->data, skb->len, 0,
d2d66c56 1190 wlvif->basic_rate);
5ec8a448
EP
1191out:
1192 dev_kfree_skb(skb);
c5312772
EP
1193 return ret;
1194}
1195
784f694d 1196int wl1271_build_qos_null_data(struct wl1271 *wl, struct ieee80211_vif *vif)
023e0826 1197{
d2d66c56 1198 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
023e0826
KV
1199 struct ieee80211_qos_hdr template;
1200
1201 memset(&template, 0, sizeof(template));
1202
cdf09495 1203 memcpy(template.addr1, vif->bss_conf.bssid, ETH_ALEN);
784f694d 1204 memcpy(template.addr2, vif->addr, ETH_ALEN);
cdf09495 1205 memcpy(template.addr3, vif->bss_conf.bssid, ETH_ALEN);
023e0826
KV
1206
1207 template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1208 IEEE80211_STYPE_QOS_NULLFUNC |
1209 IEEE80211_FCTL_TODS);
1210
1211 /* FIXME: not sure what priority to use here */
1212 template.qos_ctrl = cpu_to_le16(0);
1213
cdaac628
EP
1214 return wl1271_cmd_template_set(wl, wlvif->role_id,
1215 CMD_TEMPL_QOS_NULL_DATA, &template,
606c1487 1216 sizeof(template), 0,
d2d66c56 1217 wlvif->basic_rate);
023e0826
KV
1218}
1219
c690ec81 1220int wl12xx_cmd_set_default_wep_key(struct wl1271 *wl, u8 id, u8 hlid)
f5fc0f86 1221{
c690ec81 1222 struct wl1271_cmd_set_keys *cmd;
f5fc0f86
LC
1223 int ret = 0;
1224
1225 wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
1226
1227 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1228 if (!cmd) {
1229 ret = -ENOMEM;
1230 goto out;
1231 }
1232
c690ec81 1233 cmd->hlid = hlid;
98bdaabb
AN
1234 cmd->key_id = id;
1235 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1236 cmd->key_action = cpu_to_le16(KEY_SET_ID);
1237 cmd->key_type = KEY_WEP;
1238
1239 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1240 if (ret < 0) {
c690ec81 1241 wl1271_warning("cmd set_default_wep_key failed: %d", ret);
98bdaabb
AN
1242 goto out;
1243 }
1244
1245out:
1246 kfree(cmd);
1247
1248 return ret;
1249}
1250
a8ab39a4 1251int wl1271_cmd_set_sta_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
154da67c 1252 u16 action, u8 id, u8 key_type,
ac4e4ce5
JO
1253 u8 key_size, const u8 *key, const u8 *addr,
1254 u32 tx_seq_32, u16 tx_seq_16)
f5fc0f86 1255{
c690ec81 1256 struct wl1271_cmd_set_keys *cmd;
f5fc0f86
LC
1257 int ret = 0;
1258
b42f068b 1259 /* hlid might have already been deleted */
154da67c 1260 if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
b42f068b
EP
1261 return 0;
1262
f5fc0f86
LC
1263 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1264 if (!cmd) {
1265 ret = -ENOMEM;
1266 goto out;
1267 }
1268
154da67c 1269 cmd->hlid = wlvif->sta.hlid;
c690ec81
EP
1270
1271 if (key_type == KEY_WEP)
1272 cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
1273 else if (is_broadcast_ether_addr(addr))
1274 cmd->lid_key_type = BROADCAST_LID_TYPE;
1275 else
1276 cmd->lid_key_type = UNICAST_LID_TYPE;
f5fc0f86 1277
d0f63b20 1278 cmd->key_action = cpu_to_le16(action);
f5fc0f86
LC
1279 cmd->key_size = key_size;
1280 cmd->key_type = key_type;
1281
d0f63b20
LC
1282 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1283 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
ac4e4ce5 1284
c690ec81 1285 cmd->key_id = id;
f5fc0f86 1286
f5fc0f86
LC
1287 if (key_type == KEY_TKIP) {
1288 /*
1289 * We get the key in the following form:
1290 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1291 * but the target is expecting:
1292 * TKIP - RX MIC - TX MIC
1293 */
1294 memcpy(cmd->key, key, 16);
1295 memcpy(cmd->key + 16, key + 24, 8);
1296 memcpy(cmd->key + 24, key + 16, 8);
1297
1298 } else {
1299 memcpy(cmd->key, key, key_size);
1300 }
1301
1302 wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
1303
fa867e73 1304 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
f5fc0f86
LC
1305 if (ret < 0) {
1306 wl1271_warning("could not set keys");
152ee6e0 1307 goto out;
f5fc0f86
LC
1308 }
1309
1310out:
1311 kfree(cmd);
1312
1313 return ret;
1314}
25a7dc6d 1315
c690ec81
EP
1316/*
1317 * TODO: merge with sta/ibss into 1 set_key function.
1318 * note there are slight diffs
1319 */
a8ab39a4
EP
1320int wl1271_cmd_set_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1321 u16 action, u8 id, u8 key_type,
1322 u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
1323 u16 tx_seq_16)
98bdaabb 1324{
c690ec81 1325 struct wl1271_cmd_set_keys *cmd;
98bdaabb
AN
1326 int ret = 0;
1327 u8 lid_type;
1328
1329 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1330 if (!cmd)
1331 return -ENOMEM;
1332
a8ab39a4 1333 if (hlid == wlvif->ap.bcast_hlid) {
98bdaabb
AN
1334 if (key_type == KEY_WEP)
1335 lid_type = WEP_DEFAULT_LID_TYPE;
1336 else
1337 lid_type = BROADCAST_LID_TYPE;
1338 } else {
1339 lid_type = UNICAST_LID_TYPE;
1340 }
1341
1342 wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
1343 " hlid: %d", (int)action, (int)id, (int)lid_type,
1344 (int)key_type, (int)hlid);
1345
1346 cmd->lid_key_type = lid_type;
1347 cmd->hlid = hlid;
1348 cmd->key_action = cpu_to_le16(action);
1349 cmd->key_size = key_size;
1350 cmd->key_type = key_type;
1351 cmd->key_id = id;
1352 cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
1353 cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
1354
1355 if (key_type == KEY_TKIP) {
1356 /*
1357 * We get the key in the following form:
1358 * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
1359 * but the target is expecting:
1360 * TKIP - RX MIC - TX MIC
1361 */
1362 memcpy(cmd->key, key, 16);
1363 memcpy(cmd->key + 16, key + 24, 8);
1364 memcpy(cmd->key + 24, key + 16, 8);
1365 } else {
1366 memcpy(cmd->key, key, key_size);
1367 }
1368
1369 wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
1370
1371 ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
1372 if (ret < 0) {
1373 wl1271_warning("could not set ap keys");
1374 goto out;
1375 }
1376
1377out:
1378 kfree(cmd);
1379 return ret;
1380}
1381
b67476ef 1382int wl12xx_cmd_set_peer_state(struct wl1271 *wl, u8 hlid)
be86cbea 1383{
c690ec81 1384 struct wl12xx_cmd_set_peer_state *cmd;
be86cbea
JO
1385 int ret = 0;
1386
b67476ef 1387 wl1271_debug(DEBUG_CMD, "cmd set peer state (hlid=%d)", hlid);
be86cbea
JO
1388
1389 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1390 if (!cmd) {
1391 ret = -ENOMEM;
1392 goto out;
1393 }
1394
b67476ef 1395 cmd->hlid = hlid;
be86cbea
JO
1396 cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
1397
c690ec81 1398 ret = wl1271_cmd_send(wl, CMD_SET_PEER_STATE, cmd, sizeof(*cmd), 0);
be86cbea 1399 if (ret < 0) {
c690ec81 1400 wl1271_error("failed to send set peer state command");
be86cbea
JO
1401 goto out_free;
1402 }
1403
1404out_free:
1405 kfree(cmd);
1406
1407out:
1408 return ret;
1409}
99d5ad7b 1410
1b92f15e
EP
1411int wl12xx_cmd_add_peer(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1412 struct ieee80211_sta *sta, u8 hlid)
98bdaabb 1413{
c690ec81 1414 struct wl12xx_cmd_add_peer *cmd;
1ec23f7f 1415 int i, ret;
99d5ad7b 1416 u32 sta_rates;
98bdaabb 1417
c690ec81 1418 wl1271_debug(DEBUG_CMD, "cmd add peer %d", (int)hlid);
98bdaabb
AN
1419
1420 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1421 if (!cmd) {
1422 ret = -ENOMEM;
1423 goto out;
1424 }
1425
98bdaabb
AN
1426 memcpy(cmd->addr, sta->addr, ETH_ALEN);
1427 cmd->bss_index = WL1271_AP_BSS_INDEX;
1428 cmd->aid = sta->aid;
1429 cmd->hlid = hlid;
1ec23f7f 1430 cmd->sp_len = sta->max_sp;
b4d38db1 1431 cmd->wmm = sta->wme ? 1 : 0;
98bdaabb 1432
1ec23f7f
EP
1433 for (i = 0; i < NUM_ACCESS_CATEGORIES_COPY; i++)
1434 if (sta->wme && (sta->uapsd_queues & BIT(i)))
2e42c203
YD
1435 cmd->psd_type[NUM_ACCESS_CATEGORIES_COPY-1-i] =
1436 WL1271_PSD_UPSD_TRIGGER;
1ec23f7f 1437 else
2e42c203
YD
1438 cmd->psd_type[NUM_ACCESS_CATEGORIES_COPY-1-i] =
1439 WL1271_PSD_LEGACY;
1440
1ec23f7f 1441
1b92f15e 1442 sta_rates = sta->supp_rates[wlvif->band];
99d5ad7b 1443 if (sta->ht_cap.ht_supported)
b3a47ee0
AN
1444 sta_rates |=
1445 (sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET) |
1446 (sta->ht_cap.mcs.rx_mask[1] << HW_MIMO_RATES_OFFSET);
99d5ad7b
AN
1447
1448 cmd->supported_rates =
af7fbb28 1449 cpu_to_le32(wl1271_tx_enabled_rates_get(wl, sta_rates,
1b92f15e 1450 wlvif->band));
98bdaabb 1451
1ec23f7f
EP
1452 wl1271_debug(DEBUG_CMD, "new peer rates=0x%x queues=0x%x",
1453 cmd->supported_rates, sta->uapsd_queues);
98bdaabb 1454
c690ec81 1455 ret = wl1271_cmd_send(wl, CMD_ADD_PEER, cmd, sizeof(*cmd), 0);
98bdaabb 1456 if (ret < 0) {
c690ec81 1457 wl1271_error("failed to initiate cmd add peer");
98bdaabb
AN
1458 goto out_free;
1459 }
1460
1461out_free:
1462 kfree(cmd);
1463
1464out:
1465 return ret;
1466}
1467
c690ec81 1468int wl12xx_cmd_remove_peer(struct wl1271 *wl, u8 hlid)
98bdaabb 1469{
c690ec81 1470 struct wl12xx_cmd_remove_peer *cmd;
98bdaabb 1471 int ret;
6c15c1aa 1472 bool timeout = false;
98bdaabb 1473
c690ec81 1474 wl1271_debug(DEBUG_CMD, "cmd remove peer %d", (int)hlid);
98bdaabb
AN
1475
1476 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1477 if (!cmd) {
1478 ret = -ENOMEM;
1479 goto out;
1480 }
1481
1482 cmd->hlid = hlid;
1483 /* We never send a deauth, mac80211 is in charge of this */
1484 cmd->reason_opcode = 0;
1485 cmd->send_deauth_flag = 0;
1486
c690ec81 1487 ret = wl1271_cmd_send(wl, CMD_REMOVE_PEER, cmd, sizeof(*cmd), 0);
98bdaabb 1488 if (ret < 0) {
c690ec81 1489 wl1271_error("failed to initiate cmd remove peer");
98bdaabb
AN
1490 goto out_free;
1491 }
1492
6c15c1aa
ES
1493 ret = wl1271_cmd_wait_for_event_or_timeout(wl,
1494 PEER_REMOVE_COMPLETE_EVENT_ID,
1495 &timeout);
05285cf9
AN
1496 /*
1497 * We are ok with a timeout here. The event is sometimes not sent
6c15c1aa
ES
1498 * due to a firmware bug. In case of another error (like SDIO timeout)
1499 * queue a recovery.
05285cf9 1500 */
6c15c1aa
ES
1501 if (ret)
1502 wl12xx_queue_recovery_work(wl);
98bdaabb
AN
1503
1504out_free:
1505 kfree(cmd);
1506
1507out:
1508 return ret;
1509}
95dac04f
IY
1510
1511int wl12xx_cmd_config_fwlog(struct wl1271 *wl)
1512{
1513 struct wl12xx_cmd_config_fwlog *cmd;
1514 int ret = 0;
1515
1516 wl1271_debug(DEBUG_CMD, "cmd config firmware logger");
1517
1518 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1519 if (!cmd) {
1520 ret = -ENOMEM;
1521 goto out;
1522 }
1523
1524 cmd->logger_mode = wl->conf.fwlog.mode;
1525 cmd->log_severity = wl->conf.fwlog.severity;
1526 cmd->timestamp = wl->conf.fwlog.timestamp;
1527 cmd->output = wl->conf.fwlog.output;
1528 cmd->threshold = wl->conf.fwlog.threshold;
1529
1530 ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0);
1531 if (ret < 0) {
1532 wl1271_error("failed to send config firmware logger command");
1533 goto out_free;
1534 }
1535
1536out_free:
1537 kfree(cmd);
1538
1539out:
1540 return ret;
1541}
1542
1543int wl12xx_cmd_start_fwlog(struct wl1271 *wl)
1544{
1545 struct wl12xx_cmd_start_fwlog *cmd;
1546 int ret = 0;
1547
1548 wl1271_debug(DEBUG_CMD, "cmd start firmware logger");
1549
1550 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1551 if (!cmd) {
1552 ret = -ENOMEM;
1553 goto out;
1554 }
1555
1556 ret = wl1271_cmd_send(wl, CMD_START_FWLOGGER, cmd, sizeof(*cmd), 0);
1557 if (ret < 0) {
1558 wl1271_error("failed to send start firmware logger command");
1559 goto out_free;
1560 }
1561
1562out_free:
1563 kfree(cmd);
1564
1565out:
1566 return ret;
1567}
1568
1569int wl12xx_cmd_stop_fwlog(struct wl1271 *wl)
1570{
1571 struct wl12xx_cmd_stop_fwlog *cmd;
1572 int ret = 0;
1573
1574 wl1271_debug(DEBUG_CMD, "cmd stop firmware logger");
1575
1576 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1577 if (!cmd) {
1578 ret = -ENOMEM;
1579 goto out;
1580 }
1581
1582 ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0);
1583 if (ret < 0) {
1584 wl1271_error("failed to send stop firmware logger command");
1585 goto out_free;
1586 }
1587
1588out_free:
1589 kfree(cmd);
1590
1591out:
1592 return ret;
1593}
a7cba384 1594
1b92f15e
EP
1595static int wl12xx_cmd_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif,
1596 u8 role_id)
a7cba384
EP
1597{
1598 struct wl12xx_cmd_roc *cmd;
1599 int ret = 0;
1600
61f845f4 1601 wl1271_debug(DEBUG_CMD, "cmd roc %d (%d)", wlvif->channel, role_id);
a7cba384
EP
1602
1603 if (WARN_ON(role_id == WL12XX_INVALID_ROLE_ID))
1604 return -EINVAL;
1605
1606 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1607 if (!cmd) {
1608 ret = -ENOMEM;
1609 goto out;
1610 }
1611
1612 cmd->role_id = role_id;
61f845f4 1613 cmd->channel = wlvif->channel;
1b92f15e 1614 switch (wlvif->band) {
a7cba384 1615 case IEEE80211_BAND_2GHZ:
00782136 1616 cmd->band = WLCORE_BAND_2_4GHZ;
a7cba384
EP
1617 break;
1618 case IEEE80211_BAND_5GHZ:
00782136 1619 cmd->band = WLCORE_BAND_5GHZ;
a7cba384
EP
1620 break;
1621 default:
1b92f15e 1622 wl1271_error("roc - unknown band: %d", (int)wlvif->band);
a7cba384
EP
1623 ret = -EINVAL;
1624 goto out_free;
1625 }
1626
1627
1628 ret = wl1271_cmd_send(wl, CMD_REMAIN_ON_CHANNEL, cmd, sizeof(*cmd), 0);
1629 if (ret < 0) {
1630 wl1271_error("failed to send ROC command");
1631 goto out_free;
1632 }
1633
1634out_free:
1635 kfree(cmd);
1636
1637out:
1638 return ret;
1639}
1640
1641static int wl12xx_cmd_croc(struct wl1271 *wl, u8 role_id)
1642{
1643 struct wl12xx_cmd_croc *cmd;
1644 int ret = 0;
1645
1646 wl1271_debug(DEBUG_CMD, "cmd croc (%d)", role_id);
1647
1648 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1649 if (!cmd) {
1650 ret = -ENOMEM;
1651 goto out;
1652 }
1653 cmd->role_id = role_id;
1654
1655 ret = wl1271_cmd_send(wl, CMD_CANCEL_REMAIN_ON_CHANNEL, cmd,
1656 sizeof(*cmd), 0);
1657 if (ret < 0) {
1658 wl1271_error("failed to send ROC command");
1659 goto out_free;
1660 }
1661
1662out_free:
1663 kfree(cmd);
1664
1665out:
1666 return ret;
1667}
251c177f 1668
1b92f15e 1669int wl12xx_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 role_id)
251c177f
EP
1670{
1671 int ret = 0;
a4203c64 1672 bool is_first_roc;
251c177f
EP
1673
1674 if (WARN_ON(test_bit(role_id, wl->roc_map)))
1675 return 0;
1676
a4203c64
VG
1677 is_first_roc = (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) >=
1678 WL12XX_MAX_ROLES);
1679
1b92f15e 1680 ret = wl12xx_cmd_roc(wl, wlvif, role_id);
251c177f
EP
1681 if (ret < 0)
1682 goto out;
1683
a4203c64
VG
1684 if (is_first_roc) {
1685 ret = wl1271_cmd_wait_for_event(wl,
1686 REMAIN_ON_CHANNEL_COMPLETE_EVENT_ID);
1687 if (ret < 0) {
1688 wl1271_error("cmd roc event completion error");
1689 goto out;
1690 }
251c177f
EP
1691 }
1692
1693 __set_bit(role_id, wl->roc_map);
1694out:
1695 return ret;
1696}
1697
1698int wl12xx_croc(struct wl1271 *wl, u8 role_id)
1699{
1700 int ret = 0;
1701
1702 if (WARN_ON(!test_bit(role_id, wl->roc_map)))
1703 return 0;
1704
1705 ret = wl12xx_cmd_croc(wl, role_id);
1706 if (ret < 0)
1707 goto out;
1708
1709 __clear_bit(role_id, wl->roc_map);
55df5afb
AN
1710
1711 /*
1712 * Rearm the tx watchdog when removing the last ROC. This prevents
1713 * recoveries due to just finished ROCs - when Tx hasn't yet had
1714 * a chance to get out.
1715 */
1716 if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) >= WL12XX_MAX_ROLES)
1717 wl12xx_rearm_tx_watchdog_locked(wl);
251c177f
EP
1718out:
1719 return ret;
1720}
6d158ff3
SL
1721
1722int wl12xx_cmd_channel_switch(struct wl1271 *wl,
8332f0f6 1723 struct wl12xx_vif *wlvif,
6d158ff3
SL
1724 struct ieee80211_channel_switch *ch_switch)
1725{
1726 struct wl12xx_cmd_channel_switch *cmd;
1727 int ret;
1728
1729 wl1271_debug(DEBUG_ACX, "cmd channel switch");
1730
1731 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1732 if (!cmd) {
1733 ret = -ENOMEM;
1734 goto out;
1735 }
1736
8332f0f6 1737 cmd->role_id = wlvif->role_id;
6d158ff3
SL
1738 cmd->channel = ch_switch->channel->hw_value;
1739 cmd->switch_time = ch_switch->count;
8332f0f6
EP
1740 cmd->stop_tx = ch_switch->block_tx;
1741
1742 /* FIXME: control from mac80211 in the future */
1743 cmd->post_switch_tx_disable = 0; /* Enable TX on the target channel */
6d158ff3
SL
1744
1745 ret = wl1271_cmd_send(wl, CMD_CHANNEL_SWITCH, cmd, sizeof(*cmd), 0);
1746 if (ret < 0) {
1747 wl1271_error("failed to send channel switch command");
1748 goto out_free;
1749 }
1750
1751out_free:
1752 kfree(cmd);
1753
1754out:
1755 return ret;
1756}
1757
1758int wl12xx_cmd_stop_channel_switch(struct wl1271 *wl)
1759{
1760 struct wl12xx_cmd_stop_channel_switch *cmd;
1761 int ret;
1762
1763 wl1271_debug(DEBUG_ACX, "cmd stop channel switch");
1764
1765 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1766 if (!cmd) {
1767 ret = -ENOMEM;
1768 goto out;
1769 }
1770
1771 ret = wl1271_cmd_send(wl, CMD_STOP_CHANNEL_SWICTH, cmd, sizeof(*cmd), 0);
1772 if (ret < 0) {
1773 wl1271_error("failed to stop channel switch command");
1774 goto out_free;
1775 }
1776
1777out_free:
1778 kfree(cmd);
1779
1780out:
1781 return ret;
1782}
679a6734
EP
1783
1784/* start dev role and roc on its channel */
1785int wl12xx_start_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1786{
1787 int ret;
1788
1789 if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
1790 wlvif->bss_type == BSS_TYPE_IBSS)))
1791 return -EINVAL;
1792
889300fa
EP
1793 ret = wl12xx_cmd_role_enable(wl,
1794 wl12xx_wlvif_to_vif(wlvif)->addr,
1795 WL1271_ROLE_DEVICE,
1796 &wlvif->dev_role_id);
679a6734
EP
1797 if (ret < 0)
1798 goto out;
1799
889300fa
EP
1800 ret = wl12xx_cmd_role_start_dev(wl, wlvif);
1801 if (ret < 0)
1802 goto out_disable;
1803
679a6734
EP
1804 ret = wl12xx_roc(wl, wlvif, wlvif->dev_role_id);
1805 if (ret < 0)
1806 goto out_stop;
1807
1808 return 0;
1809
1810out_stop:
1811 wl12xx_cmd_role_stop_dev(wl, wlvif);
889300fa
EP
1812out_disable:
1813 wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
679a6734
EP
1814out:
1815 return ret;
1816}
1817
1818/* croc dev hlid, and stop the role */
1819int wl12xx_stop_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1820{
1821 int ret;
1822
1823 if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
1824 wlvif->bss_type == BSS_TYPE_IBSS)))
1825 return -EINVAL;
1826
8aefffea 1827 /* flush all pending packets */
eb96f841
IY
1828 ret = wlcore_tx_work_locked(wl);
1829 if (ret < 0)
1830 goto out;
8aefffea 1831
679a6734
EP
1832 if (test_bit(wlvif->dev_role_id, wl->roc_map)) {
1833 ret = wl12xx_croc(wl, wlvif->dev_role_id);
1834 if (ret < 0)
1835 goto out;
1836 }
1837
1838 ret = wl12xx_cmd_role_stop_dev(wl, wlvif);
1839 if (ret < 0)
1840 goto out;
889300fa
EP
1841
1842 ret = wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
1843 if (ret < 0)
1844 goto out;
1845
679a6734
EP
1846out:
1847 return ret;
1848}