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