]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/wireless/ti/wlcore/main.c
wlcore: allow using dfs channels
[mirror_ubuntu-bionic-kernel.git] / drivers / net / wireless / ti / wlcore / main.c
CommitLineData
f1d63a59 1
f5fc0f86 2/*
8f6ac537 3 * This file is part of wlcore
f5fc0f86 4 *
8bf29b0e 5 * Copyright (C) 2008-2010 Nokia Corporation
8f6ac537 6 * Copyright (C) 2011-2013 Texas Instruments Inc.
f5fc0f86
LC
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>
f5fc0f86 25#include <linux/firmware.h>
f5fc0f86 26#include <linux/etherdevice.h>
1fba4974 27#include <linux/vmalloc.h>
341b7cde 28#include <linux/wl12xx.h>
a390e85c 29#include <linux/interrupt.h>
f5fc0f86 30
c31be25a 31#include "wlcore.h"
0f4e3122 32#include "debug.h"
f5fc0f86 33#include "wl12xx_80211.h"
00d20100 34#include "io.h"
00d20100 35#include "tx.h"
00d20100
SL
36#include "ps.h"
37#include "init.h"
38#include "debugfs.h"
00d20100 39#include "testmode.h"
d8c5a48d 40#include "vendor_cmd.h"
00d20100 41#include "scan.h"
53d67a50 42#include "hw_ops.h"
33cab57a 43#include "sysfs.h"
f5fc0f86 44
9ccd9217
JO
45#define WL1271_BOOT_RETRIES 3
46
95dac04f 47static char *fwlog_param;
93ac8488 48static int fwlog_mem_blocks = -1;
7230341f
YS
49static int bug_on_recovery = -1;
50static int no_recovery = -1;
95dac04f 51
7dece1c8 52static void __wl1271_op_remove_interface(struct wl1271 *wl,
536129c8 53 struct ieee80211_vif *vif,
7dece1c8 54 bool reset_tx_queues);
c24ec83b 55static void wlcore_op_stop_locked(struct wl1271 *wl);
170d0e67 56static void wl1271_free_ap_keys(struct wl1271 *wl, struct wl12xx_vif *wlvif);
52b0e7a6 57
8f6ac537 58static int wl12xx_set_authorized(struct wl1271 *wl, struct wl12xx_vif *wlvif)
ef4b29e9
EP
59{
60 int ret;
0603d891 61
9fd6f21b
EP
62 if (WARN_ON(wlvif->bss_type != BSS_TYPE_STA_BSS))
63 return -EINVAL;
64
65 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
ef4b29e9
EP
66 return 0;
67
8181aecc 68 if (test_and_set_bit(WLVIF_FLAG_STA_STATE_SENT, &wlvif->flags))
ef4b29e9
EP
69 return 0;
70
d50529c0 71 ret = wl12xx_cmd_set_peer_state(wl, wlvif, wlvif->sta.hlid);
ef4b29e9
EP
72 if (ret < 0)
73 return ret;
74
75 wl1271_info("Association completed.");
76 return 0;
77}
c2c192ac 78
0c0280bd
LR
79static void wl1271_reg_notify(struct wiphy *wiphy,
80 struct regulatory_request *request)
573c67cf 81{
6b70e7eb
VG
82 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
83 struct wl1271 *wl = hw->priv;
b7417d93 84
75592be5 85 wlcore_regdomain_config(wl);
b7417d93
JO
86}
87
9eb599e9
EP
88static int wl1271_set_rx_streaming(struct wl1271 *wl, struct wl12xx_vif *wlvif,
89 bool enable)
77ddaa10
EP
90{
91 int ret = 0;
92
93 /* we should hold wl->mutex */
9eb599e9 94 ret = wl1271_acx_ps_rx_streaming(wl, wlvif, enable);
77ddaa10
EP
95 if (ret < 0)
96 goto out;
97
98 if (enable)
0744bdb6 99 set_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags);
77ddaa10 100 else
0744bdb6 101 clear_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags);
77ddaa10
EP
102out:
103 return ret;
104}
105
106/*
107 * this function is being called when the rx_streaming interval
108 * has beed changed or rx_streaming should be disabled
109 */
9eb599e9 110int wl1271_recalc_rx_streaming(struct wl1271 *wl, struct wl12xx_vif *wlvif)
77ddaa10
EP
111{
112 int ret = 0;
113 int period = wl->conf.rx_streaming.interval;
114
115 /* don't reconfigure if rx_streaming is disabled */
0744bdb6 116 if (!test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags))
77ddaa10
EP
117 goto out;
118
119 /* reconfigure/disable according to new streaming_period */
120 if (period &&
ba8447f6 121 test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) &&
77ddaa10
EP
122 (wl->conf.rx_streaming.always ||
123 test_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags)))
9eb599e9 124 ret = wl1271_set_rx_streaming(wl, wlvif, true);
77ddaa10 125 else {
9eb599e9 126 ret = wl1271_set_rx_streaming(wl, wlvif, false);
77ddaa10 127 /* don't cancel_work_sync since we might deadlock */
9eb599e9 128 del_timer_sync(&wlvif->rx_streaming_timer);
77ddaa10
EP
129 }
130out:
131 return ret;
132}
133
134static void wl1271_rx_streaming_enable_work(struct work_struct *work)
135{
136 int ret;
9eb599e9
EP
137 struct wl12xx_vif *wlvif = container_of(work, struct wl12xx_vif,
138 rx_streaming_enable_work);
139 struct wl1271 *wl = wlvif->wl;
77ddaa10
EP
140
141 mutex_lock(&wl->mutex);
142
0744bdb6 143 if (test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags) ||
ba8447f6 144 !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) ||
77ddaa10
EP
145 (!wl->conf.rx_streaming.always &&
146 !test_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags)))
147 goto out;
148
149 if (!wl->conf.rx_streaming.interval)
150 goto out;
151
152 ret = wl1271_ps_elp_wakeup(wl);
153 if (ret < 0)
154 goto out;
155
9eb599e9 156 ret = wl1271_set_rx_streaming(wl, wlvif, true);
77ddaa10
EP
157 if (ret < 0)
158 goto out_sleep;
159
160 /* stop it after some time of inactivity */
9eb599e9 161 mod_timer(&wlvif->rx_streaming_timer,
77ddaa10
EP
162 jiffies + msecs_to_jiffies(wl->conf.rx_streaming.duration));
163
164out_sleep:
165 wl1271_ps_elp_sleep(wl);
166out:
167 mutex_unlock(&wl->mutex);
168}
169
170static void wl1271_rx_streaming_disable_work(struct work_struct *work)
171{
172 int ret;
9eb599e9
EP
173 struct wl12xx_vif *wlvif = container_of(work, struct wl12xx_vif,
174 rx_streaming_disable_work);
175 struct wl1271 *wl = wlvif->wl;
77ddaa10
EP
176
177 mutex_lock(&wl->mutex);
178
0744bdb6 179 if (!test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags))
77ddaa10
EP
180 goto out;
181
182 ret = wl1271_ps_elp_wakeup(wl);
183 if (ret < 0)
184 goto out;
185
9eb599e9 186 ret = wl1271_set_rx_streaming(wl, wlvif, false);
77ddaa10
EP
187 if (ret)
188 goto out_sleep;
189
190out_sleep:
191 wl1271_ps_elp_sleep(wl);
192out:
193 mutex_unlock(&wl->mutex);
194}
195
196static void wl1271_rx_streaming_timer(unsigned long data)
197{
9eb599e9
EP
198 struct wl12xx_vif *wlvif = (struct wl12xx_vif *)data;
199 struct wl1271 *wl = wlvif->wl;
200 ieee80211_queue_work(wl->hw, &wlvif->rx_streaming_disable_work);
77ddaa10
EP
201}
202
55df5afb
AN
203/* wl->mutex must be taken */
204void wl12xx_rearm_tx_watchdog_locked(struct wl1271 *wl)
205{
206 /* if the watchdog is not armed, don't do anything */
207 if (wl->tx_allocated_blocks == 0)
208 return;
209
210 cancel_delayed_work(&wl->tx_watchdog_work);
211 ieee80211_queue_delayed_work(wl->hw, &wl->tx_watchdog_work,
212 msecs_to_jiffies(wl->conf.tx.tx_watchdog_timeout));
213}
214
7d3b29e5
EP
215static void wlcore_rc_update_work(struct work_struct *work)
216{
217 int ret;
218 struct wl12xx_vif *wlvif = container_of(work, struct wl12xx_vif,
219 rc_update_work);
220 struct wl1271 *wl = wlvif->wl;
221
222 mutex_lock(&wl->mutex);
223
224 if (unlikely(wl->state != WLCORE_STATE_ON))
225 goto out;
226
227 ret = wl1271_ps_elp_wakeup(wl);
228 if (ret < 0)
229 goto out;
230
231 wlcore_hw_sta_rc_update(wl, wlvif);
232
233 wl1271_ps_elp_sleep(wl);
234out:
235 mutex_unlock(&wl->mutex);
236}
237
55df5afb
AN
238static void wl12xx_tx_watchdog_work(struct work_struct *work)
239{
240 struct delayed_work *dwork;
241 struct wl1271 *wl;
242
243 dwork = container_of(work, struct delayed_work, work);
244 wl = container_of(dwork, struct wl1271, tx_watchdog_work);
245
246 mutex_lock(&wl->mutex);
247
4cc53383 248 if (unlikely(wl->state != WLCORE_STATE_ON))
55df5afb
AN
249 goto out;
250
251 /* Tx went out in the meantime - everything is ok */
252 if (unlikely(wl->tx_allocated_blocks == 0))
253 goto out;
254
255 /*
256 * if a ROC is in progress, we might not have any Tx for a long
257 * time (e.g. pending Tx on the non-ROC channels)
258 */
259 if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES) {
260 wl1271_debug(DEBUG_TX, "No Tx (in FW) for %d ms due to ROC",
261 wl->conf.tx.tx_watchdog_timeout);
262 wl12xx_rearm_tx_watchdog_locked(wl);
263 goto out;
264 }
265
266 /*
267 * if a scan is in progress, we might not have any Tx for a long
268 * time
269 */
270 if (wl->scan.state != WL1271_SCAN_STATE_IDLE) {
271 wl1271_debug(DEBUG_TX, "No Tx (in FW) for %d ms due to scan",
272 wl->conf.tx.tx_watchdog_timeout);
273 wl12xx_rearm_tx_watchdog_locked(wl);
274 goto out;
275 }
276
277 /*
278 * AP might cache a frame for a long time for a sleeping station,
279 * so rearm the timer if there's an AP interface with stations. If
280 * Tx is genuinely stuck we will most hopefully discover it when all
281 * stations are removed due to inactivity.
282 */
283 if (wl->active_sta_count) {
284 wl1271_debug(DEBUG_TX, "No Tx (in FW) for %d ms. AP has "
285 " %d stations",
286 wl->conf.tx.tx_watchdog_timeout,
287 wl->active_sta_count);
288 wl12xx_rearm_tx_watchdog_locked(wl);
289 goto out;
290 }
291
292 wl1271_error("Tx stuck (in FW) for %d ms. Starting recovery",
293 wl->conf.tx.tx_watchdog_timeout);
294 wl12xx_queue_recovery_work(wl);
295
296out:
297 mutex_unlock(&wl->mutex);
298}
299
e87288f0 300static void wlcore_adjust_conf(struct wl1271 *wl)
8a08048a 301{
95dac04f 302 /* Adjust settings according to optional module parameters */
7230341f 303
93ac8488
IR
304 /* Firmware Logger params */
305 if (fwlog_mem_blocks != -1) {
306 if (fwlog_mem_blocks >= CONF_FWLOG_MIN_MEM_BLOCKS &&
307 fwlog_mem_blocks <= CONF_FWLOG_MAX_MEM_BLOCKS) {
308 wl->conf.fwlog.mem_blocks = fwlog_mem_blocks;
309 } else {
310 wl1271_error(
311 "Illegal fwlog_mem_blocks=%d using default %d",
312 fwlog_mem_blocks, wl->conf.fwlog.mem_blocks);
313 }
314 }
315
95dac04f
IY
316 if (fwlog_param) {
317 if (!strcmp(fwlog_param, "continuous")) {
318 wl->conf.fwlog.mode = WL12XX_FWLOG_CONTINUOUS;
319 } else if (!strcmp(fwlog_param, "ondemand")) {
320 wl->conf.fwlog.mode = WL12XX_FWLOG_ON_DEMAND;
321 } else if (!strcmp(fwlog_param, "dbgpins")) {
322 wl->conf.fwlog.mode = WL12XX_FWLOG_CONTINUOUS;
323 wl->conf.fwlog.output = WL12XX_FWLOG_OUTPUT_DBG_PINS;
324 } else if (!strcmp(fwlog_param, "disable")) {
325 wl->conf.fwlog.mem_blocks = 0;
326 wl->conf.fwlog.output = WL12XX_FWLOG_OUTPUT_NONE;
327 } else {
328 wl1271_error("Unknown fwlog parameter %s", fwlog_param);
329 }
330 }
7230341f
YS
331
332 if (bug_on_recovery != -1)
333 wl->conf.recovery.bug_on_recovery = (u8) bug_on_recovery;
334
335 if (no_recovery != -1)
336 wl->conf.recovery.no_recovery = (u8) no_recovery;
95dac04f 337}
2b60100b 338
6e8cd331
EP
339static void wl12xx_irq_ps_regulate_link(struct wl1271 *wl,
340 struct wl12xx_vif *wlvif,
341 u8 hlid, u8 tx_pkts)
b622d992 342{
37c68ea6 343 bool fw_ps;
b622d992 344
5e74b3aa 345 fw_ps = test_bit(hlid, &wl->ap_fw_ps_map);
b622d992
AN
346
347 /*
348 * Wake up from high level PS if the STA is asleep with too little
9b17f1b3 349 * packets in FW or if the STA is awake.
b622d992 350 */
9b17f1b3 351 if (!fw_ps || tx_pkts < WL1271_PS_STA_MAX_PACKETS)
6e8cd331 352 wl12xx_ps_link_end(wl, wlvif, hlid);
b622d992 353
da03209e
AN
354 /*
355 * Start high-level PS if the STA is asleep with enough blocks in FW.
9a100968
AN
356 * Make an exception if this is the only connected link. In this
357 * case FW-memory congestion is less of a problem.
41ed1a78
EP
358 * Note that a single connected STA means 2*ap_count + 1 active links,
359 * since we must account for the global and broadcast AP links
360 * for each AP. The "fw_ps" check assures us the other link is a STA
361 * connected to the AP. Otherwise the FW would not set the PSM bit.
da03209e 362 */
41ed1a78 363 else if (wl->active_link_count > (wl->ap_count*2 + 1) && fw_ps &&
37c68ea6 364 tx_pkts >= WL1271_PS_STA_MAX_PACKETS)
6e8cd331 365 wl12xx_ps_link_start(wl, wlvif, hlid, true);
b622d992
AN
366}
367
9b17f1b3 368static void wl12xx_irq_update_links_status(struct wl1271 *wl,
c7ffb902 369 struct wl12xx_vif *wlvif,
75fb4df7 370 struct wl_fw_status *status)
b622d992 371{
5e74b3aa 372 unsigned long cur_fw_ps_map;
9ebcb232 373 u8 hlid;
b622d992 374
75fb4df7 375 cur_fw_ps_map = status->link_ps_bitmap;
b622d992
AN
376 if (wl->ap_fw_ps_map != cur_fw_ps_map) {
377 wl1271_debug(DEBUG_PSM,
5e74b3aa 378 "link ps prev 0x%lx cur 0x%lx changed 0x%lx",
b622d992
AN
379 wl->ap_fw_ps_map, cur_fw_ps_map,
380 wl->ap_fw_ps_map ^ cur_fw_ps_map);
381
382 wl->ap_fw_ps_map = cur_fw_ps_map;
383 }
384
da08fdfa 385 for_each_set_bit(hlid, wlvif->ap.sta_hlid_map, wl->num_links)
6e8cd331 386 wl12xx_irq_ps_regulate_link(wl, wlvif, hlid,
9ebcb232 387 wl->links[hlid].allocated_pkts);
b622d992
AN
388}
389
75fb4df7 390static int wlcore_fw_status(struct wl1271 *wl, struct wl_fw_status *status)
f5fc0f86 391{
6e8cd331 392 struct wl12xx_vif *wlvif;
ac5e1e39 393 struct timespec ts;
13b107dd 394 u32 old_tx_blk_count = wl->tx_blocks_available;
4d56ad9c 395 int avail, freed_blocks;
bf54e301 396 int i;
8b7c0fc3 397 int ret;
9ebcb232 398 struct wl1271_link *lnk;
6bac40a6 399
75fb4df7
EP
400 ret = wlcore_raw_read_data(wl, REG_RAW_FW_STATUS_ADDR,
401 wl->raw_fw_status,
402 wl->fw_status_len, false);
8b7c0fc3
IY
403 if (ret < 0)
404 return ret;
13b107dd 405
75fb4df7
EP
406 wlcore_hw_convert_fw_status(wl, wl->raw_fw_status, wl->fw_status);
407
f5fc0f86
LC
408 wl1271_debug(DEBUG_IRQ, "intr: 0x%x (fw_rx_counter = %d, "
409 "drv_rx_counter = %d, tx_results_counter = %d)",
75fb4df7
EP
410 status->intr,
411 status->fw_rx_counter,
412 status->drv_rx_counter,
413 status->tx_results_counter);
f5fc0f86 414
bf54e301
AN
415 for (i = 0; i < NUM_TX_QUEUES; i++) {
416 /* prevent wrap-around in freed-packets counter */
742246f8 417 wl->tx_allocated_pkts[i] -=
75fb4df7 418 (status->counters.tx_released_pkts[i] -
bf54e301
AN
419 wl->tx_pkts_freed[i]) & 0xff;
420
75fb4df7 421 wl->tx_pkts_freed[i] = status->counters.tx_released_pkts[i];
bf54e301
AN
422 }
423
9ebcb232 424
da08fdfa 425 for_each_set_bit(i, wl->links_map, wl->num_links) {
93d5d100 426 u8 diff;
9ebcb232 427 lnk = &wl->links[i];
93d5d100 428
9ebcb232 429 /* prevent wrap-around in freed-packets counter */
75fb4df7 430 diff = (status->counters.tx_lnk_free_pkts[i] -
93d5d100
AN
431 lnk->prev_freed_pkts) & 0xff;
432
433 if (diff == 0)
434 continue;
9ebcb232 435
93d5d100 436 lnk->allocated_pkts -= diff;
75fb4df7 437 lnk->prev_freed_pkts = status->counters.tx_lnk_free_pkts[i];
93d5d100
AN
438
439 /* accumulate the prev_freed_pkts counter */
440 lnk->total_freed_pkts += diff;
9ebcb232
AN
441 }
442
bdf91cfa 443 /* prevent wrap-around in total blocks counter */
75fb4df7
EP
444 if (likely(wl->tx_blocks_freed <= status->total_released_blks))
445 freed_blocks = status->total_released_blks -
bdf91cfa
AN
446 wl->tx_blocks_freed;
447 else
448 freed_blocks = 0x100000000LL - wl->tx_blocks_freed +
75fb4df7 449 status->total_released_blks;
bdf91cfa 450
75fb4df7 451 wl->tx_blocks_freed = status->total_released_blks;
13b107dd 452
7bb5d6ce
AN
453 wl->tx_allocated_blocks -= freed_blocks;
454
55df5afb
AN
455 /*
456 * If the FW freed some blocks:
457 * If we still have allocated blocks - re-arm the timer, Tx is
458 * not stuck. Otherwise, cancel the timer (no Tx currently).
459 */
460 if (freed_blocks) {
461 if (wl->tx_allocated_blocks)
462 wl12xx_rearm_tx_watchdog_locked(wl);
463 else
464 cancel_delayed_work(&wl->tx_watchdog_work);
465 }
466
75fb4df7 467 avail = status->tx_total - wl->tx_allocated_blocks;
13b107dd 468
4d56ad9c
EP
469 /*
470 * The FW might change the total number of TX memblocks before
471 * we get a notification about blocks being released. Thus, the
472 * available blocks calculation might yield a temporary result
473 * which is lower than the actual available blocks. Keeping in
474 * mind that only blocks that were allocated can be moved from
475 * TX to RX, tx_blocks_available should never decrease here.
476 */
477 wl->tx_blocks_available = max((int)wl->tx_blocks_available,
478 avail);
f5fc0f86 479
a522550a 480 /* if more blocks are available now, tx work can be scheduled */
13b107dd 481 if (wl->tx_blocks_available > old_tx_blk_count)
a522550a 482 clear_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags);
f5fc0f86 483
4d56ad9c 484 /* for AP update num of allocated TX blocks per link and ps status */
6e8cd331 485 wl12xx_for_each_wlvif_ap(wl, wlvif) {
75fb4df7 486 wl12xx_irq_update_links_status(wl, wlvif, status);
6e8cd331 487 }
4d56ad9c 488
f5fc0f86 489 /* update the host-chipset time offset */
ac5e1e39
JO
490 getnstimeofday(&ts);
491 wl->time_offset = (timespec_to_ns(&ts) >> 10) -
75fb4df7 492 (s64)(status->fw_localtime);
8b7c0fc3 493
75fb4df7 494 wl->fw_fast_lnk_map = status->link_fast_bitmap;
0e810479 495
8b7c0fc3 496 return 0;
f5fc0f86
LC
497}
498
a620865e
IY
499static void wl1271_flush_deferred_work(struct wl1271 *wl)
500{
501 struct sk_buff *skb;
502
503 /* Pass all received frames to the network stack */
504 while ((skb = skb_dequeue(&wl->deferred_rx_queue)))
505 ieee80211_rx_ni(wl->hw, skb);
506
507 /* Return sent skbs to the network stack */
508 while ((skb = skb_dequeue(&wl->deferred_tx_queue)))
c27d3acc 509 ieee80211_tx_status_ni(wl->hw, skb);
a620865e
IY
510}
511
512static void wl1271_netstack_work(struct work_struct *work)
513{
514 struct wl1271 *wl =
515 container_of(work, struct wl1271, netstack_work);
516
517 do {
518 wl1271_flush_deferred_work(wl);
519 } while (skb_queue_len(&wl->deferred_rx_queue));
520}
1e73eb62 521
a620865e
IY
522#define WL1271_IRQ_MAX_LOOPS 256
523
b5b45b3c 524static int wlcore_irq_locked(struct wl1271 *wl)
f5fc0f86 525{
b5b45b3c 526 int ret = 0;
c15f63bf 527 u32 intr;
1e73eb62 528 int loopcount = WL1271_IRQ_MAX_LOOPS;
a620865e
IY
529 bool done = false;
530 unsigned int defer_count;
b07d4037
IY
531 unsigned long flags;
532
341b7cde
IY
533 /*
534 * In case edge triggered interrupt must be used, we cannot iterate
535 * more than once without introducing race conditions with the hardirq.
536 */
537 if (wl->platform_quirks & WL12XX_PLATFORM_QUIRK_EDGE_IRQ)
538 loopcount = 1;
539
f5fc0f86
LC
540 wl1271_debug(DEBUG_IRQ, "IRQ work");
541
4cc53383 542 if (unlikely(wl->state != WLCORE_STATE_ON))
f5fc0f86
LC
543 goto out;
544
a620865e 545 ret = wl1271_ps_elp_wakeup(wl);
f5fc0f86
LC
546 if (ret < 0)
547 goto out;
548
a620865e
IY
549 while (!done && loopcount--) {
550 /*
551 * In order to avoid a race with the hardirq, clear the flag
552 * before acknowledging the chip. Since the mutex is held,
553 * wl1271_ps_elp_wakeup cannot be called concurrently.
554 */
555 clear_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
4e857c58 556 smp_mb__after_atomic();
1e73eb62 557
75fb4df7 558 ret = wlcore_fw_status(wl, wl->fw_status);
b5b45b3c 559 if (ret < 0)
8b7c0fc3 560 goto out;
53d67a50
AN
561
562 wlcore_hw_tx_immediate_compl(wl);
563
75fb4df7 564 intr = wl->fw_status->intr;
f5755fe9 565 intr &= WLCORE_ALL_INTR_MASK;
1e73eb62 566 if (!intr) {
a620865e 567 done = true;
1e73eb62
JO
568 continue;
569 }
f5fc0f86 570
ccc83b04 571 if (unlikely(intr & WL1271_ACX_INTR_WATCHDOG)) {
f5755fe9
IR
572 wl1271_error("HW watchdog interrupt received! starting recovery.");
573 wl->watchdog_recovery = true;
b5b45b3c 574 ret = -EIO;
f5755fe9
IR
575
576 /* restarting the chip. ignore any other interrupt. */
577 goto out;
578 }
579
580 if (unlikely(intr & WL1271_ACX_SW_INTR_WATCHDOG)) {
581 wl1271_error("SW watchdog interrupt received! "
ccc83b04 582 "starting recovery.");
afbe3718 583 wl->watchdog_recovery = true;
b5b45b3c 584 ret = -EIO;
ccc83b04
EP
585
586 /* restarting the chip. ignore any other interrupt. */
587 goto out;
588 }
589
a620865e 590 if (likely(intr & WL1271_ACX_INTR_DATA)) {
1e73eb62 591 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_DATA");
1fd2794f 592
75fb4df7 593 ret = wlcore_rx(wl, wl->fw_status);
b5b45b3c 594 if (ret < 0)
045b9b5f 595 goto out;
f5fc0f86 596
a522550a 597 /* Check if any tx blocks were freed */
b07d4037 598 spin_lock_irqsave(&wl->wl_lock, flags);
a522550a 599 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
f1a46384 600 wl1271_tx_total_queue_count(wl) > 0) {
b07d4037 601 spin_unlock_irqrestore(&wl->wl_lock, flags);
a522550a
IY
602 /*
603 * In order to avoid starvation of the TX path,
604 * call the work function directly.
605 */
eb96f841 606 ret = wlcore_tx_work_locked(wl);
b5b45b3c 607 if (ret < 0)
eb96f841 608 goto out;
b07d4037
IY
609 } else {
610 spin_unlock_irqrestore(&wl->wl_lock, flags);
a522550a
IY
611 }
612
8aad2464 613 /* check for tx results */
045b9b5f 614 ret = wlcore_hw_tx_delayed_compl(wl);
b5b45b3c 615 if (ret < 0)
045b9b5f 616 goto out;
a620865e
IY
617
618 /* Make sure the deferred queues don't get too long */
619 defer_count = skb_queue_len(&wl->deferred_tx_queue) +
620 skb_queue_len(&wl->deferred_rx_queue);
621 if (defer_count > WL1271_DEFERRED_QUEUE_LIMIT)
622 wl1271_flush_deferred_work(wl);
1e73eb62 623 }
f5fc0f86 624
1e73eb62
JO
625 if (intr & WL1271_ACX_INTR_EVENT_A) {
626 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_EVENT_A");
045b9b5f 627 ret = wl1271_event_handle(wl, 0);
b5b45b3c 628 if (ret < 0)
045b9b5f 629 goto out;
1e73eb62 630 }
f5fc0f86 631
1e73eb62
JO
632 if (intr & WL1271_ACX_INTR_EVENT_B) {
633 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_EVENT_B");
045b9b5f 634 ret = wl1271_event_handle(wl, 1);
b5b45b3c 635 if (ret < 0)
045b9b5f 636 goto out;
1e73eb62 637 }
f5fc0f86 638
1e73eb62
JO
639 if (intr & WL1271_ACX_INTR_INIT_COMPLETE)
640 wl1271_debug(DEBUG_IRQ,
641 "WL1271_ACX_INTR_INIT_COMPLETE");
f5fc0f86 642
1e73eb62
JO
643 if (intr & WL1271_ACX_INTR_HW_AVAILABLE)
644 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_HW_AVAILABLE");
c15f63bf 645 }
f5fc0f86 646
f5fc0f86
LC
647 wl1271_ps_elp_sleep(wl);
648
649out:
b5b45b3c
AN
650 return ret;
651}
652
653static irqreturn_t wlcore_irq(int irq, void *cookie)
654{
655 int ret;
656 unsigned long flags;
657 struct wl1271 *wl = cookie;
658
97236a06
LC
659 /* complete the ELP completion */
660 spin_lock_irqsave(&wl->wl_lock, flags);
661 set_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
662 if (wl->elp_compl) {
663 complete(wl->elp_compl);
664 wl->elp_compl = NULL;
665 }
666
667 if (test_bit(WL1271_FLAG_SUSPENDED, &wl->flags)) {
668 /* don't enqueue a work right now. mark it as pending */
669 set_bit(WL1271_FLAG_PENDING_WORK, &wl->flags);
670 wl1271_debug(DEBUG_IRQ, "should not enqueue work");
671 disable_irq_nosync(wl->irq);
672 pm_wakeup_event(wl->dev, 0);
673 spin_unlock_irqrestore(&wl->wl_lock, flags);
674 return IRQ_HANDLED;
675 }
676 spin_unlock_irqrestore(&wl->wl_lock, flags);
677
b5b45b3c
AN
678 /* TX might be handled here, avoid redundant work */
679 set_bit(WL1271_FLAG_TX_PENDING, &wl->flags);
680 cancel_work_sync(&wl->tx_work);
681
682 mutex_lock(&wl->mutex);
683
684 ret = wlcore_irq_locked(wl);
685 if (ret)
686 wl12xx_queue_recovery_work(wl);
687
b07d4037
IY
688 spin_lock_irqsave(&wl->wl_lock, flags);
689 /* In case TX was not handled here, queue TX work */
690 clear_bit(WL1271_FLAG_TX_PENDING, &wl->flags);
691 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
f1a46384 692 wl1271_tx_total_queue_count(wl) > 0)
b07d4037
IY
693 ieee80211_queue_work(wl->hw, &wl->tx_work);
694 spin_unlock_irqrestore(&wl->wl_lock, flags);
695
f5fc0f86 696 mutex_unlock(&wl->mutex);
a620865e
IY
697
698 return IRQ_HANDLED;
f5fc0f86
LC
699}
700
4549d09c
EP
701struct vif_counter_data {
702 u8 counter;
703
704 struct ieee80211_vif *cur_vif;
705 bool cur_vif_running;
706};
707
708static void wl12xx_vif_count_iter(void *data, u8 *mac,
709 struct ieee80211_vif *vif)
710{
711 struct vif_counter_data *counter = data;
712
713 counter->counter++;
714 if (counter->cur_vif == vif)
715 counter->cur_vif_running = true;
716}
717
718/* caller must not hold wl->mutex, as it might deadlock */
719static void wl12xx_get_vif_count(struct ieee80211_hw *hw,
720 struct ieee80211_vif *cur_vif,
721 struct vif_counter_data *data)
722{
723 memset(data, 0, sizeof(*data));
724 data->cur_vif = cur_vif;
725
8b2c9824 726 ieee80211_iterate_active_interfaces(hw, IEEE80211_IFACE_ITER_RESUME_ALL,
4549d09c
EP
727 wl12xx_vif_count_iter, data);
728}
729
3fcdab70 730static int wl12xx_fetch_firmware(struct wl1271 *wl, bool plt)
f5fc0f86
LC
731{
732 const struct firmware *fw;
166d504e 733 const char *fw_name;
3fcdab70 734 enum wl12xx_fw_type fw_type;
f5fc0f86
LC
735 int ret;
736
3fcdab70
EP
737 if (plt) {
738 fw_type = WL12XX_FW_TYPE_PLT;
6f7dd16c 739 fw_name = wl->plt_fw_name;
3fcdab70 740 } else {
4549d09c
EP
741 /*
742 * we can't call wl12xx_get_vif_count() here because
743 * wl->mutex is taken, so use the cached last_vif_count value
744 */
9b1a0a77 745 if (wl->last_vif_count > 1 && wl->mr_fw_name) {
4549d09c 746 fw_type = WL12XX_FW_TYPE_MULTI;
6f7dd16c 747 fw_name = wl->mr_fw_name;
4549d09c
EP
748 } else {
749 fw_type = WL12XX_FW_TYPE_NORMAL;
6f7dd16c 750 fw_name = wl->sr_fw_name;
4549d09c 751 }
3fcdab70
EP
752 }
753
754 if (wl->fw_type == fw_type)
755 return 0;
166d504e
AN
756
757 wl1271_debug(DEBUG_BOOT, "booting firmware %s", fw_name);
758
a390e85c 759 ret = request_firmware(&fw, fw_name, wl->dev);
f5fc0f86
LC
760
761 if (ret < 0) {
35898935 762 wl1271_error("could not get firmware %s: %d", fw_name, ret);
f5fc0f86
LC
763 return ret;
764 }
765
766 if (fw->size % 4) {
767 wl1271_error("firmware size is not multiple of 32 bits: %zu",
768 fw->size);
769 ret = -EILSEQ;
770 goto out;
771 }
772
166d504e 773 vfree(wl->fw);
3fcdab70 774 wl->fw_type = WL12XX_FW_TYPE_NONE;
f5fc0f86 775 wl->fw_len = fw->size;
1fba4974 776 wl->fw = vmalloc(wl->fw_len);
f5fc0f86
LC
777
778 if (!wl->fw) {
779 wl1271_error("could not allocate memory for the firmware");
780 ret = -ENOMEM;
781 goto out;
782 }
783
784 memcpy(wl->fw, fw->data, wl->fw_len);
f5fc0f86 785 ret = 0;
3fcdab70 786 wl->fw_type = fw_type;
f5fc0f86
LC
787out:
788 release_firmware(fw);
789
790 return ret;
791}
792
baacb9ae
IY
793void wl12xx_queue_recovery_work(struct wl1271 *wl)
794{
b666bb7f 795 /* Avoid a recursive recovery */
792a58a8 796 if (wl->state == WLCORE_STATE_ON) {
1ede9500
AN
797 WARN_ON(!test_bit(WL1271_FLAG_INTENDED_FW_RECOVERY,
798 &wl->flags));
799
4cc53383 800 wl->state = WLCORE_STATE_RESTARTING;
792a58a8 801 set_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags);
2473ec8f 802 wl1271_ps_elp_wakeup(wl);
b666bb7f 803 wlcore_disable_interrupts_nosync(wl);
baacb9ae 804 ieee80211_queue_work(wl->hw, &wl->recovery_work);
b666bb7f 805 }
baacb9ae
IY
806}
807
95dac04f
IY
808size_t wl12xx_copy_fwlog(struct wl1271 *wl, u8 *memblock, size_t maxlen)
809{
c83cb803 810 size_t len;
95dac04f
IY
811
812 /* Make sure we have enough room */
c8e49556 813 len = min_t(size_t, maxlen, PAGE_SIZE - wl->fwlog_size);
95dac04f
IY
814
815 /* Fill the FW log file, consumed by the sysfs fwlog entry */
816 memcpy(wl->fwlog + wl->fwlog_size, memblock, len);
817 wl->fwlog_size += len;
818
819 return len;
820}
821
822static void wl12xx_read_fwlog_panic(struct wl1271 *wl)
823{
c83cb803 824 struct wlcore_partition_set part, old_part;
95dac04f 825 u32 addr;
1e41213f
IC
826 u32 offset;
827 u32 end_of_log;
95dac04f 828 u8 *block;
8b7c0fc3 829 int ret;
95dac04f 830
6f7dd16c 831 if ((wl->quirks & WLCORE_QUIRK_FWLOG_NOT_IMPLEMENTED) ||
95dac04f
IY
832 (wl->conf.fwlog.mem_blocks == 0))
833 return;
834
835 wl1271_info("Reading FW panic log");
836
c83cb803 837 block = kmalloc(wl->fw_mem_block_size, GFP_KERNEL);
95dac04f
IY
838 if (!block)
839 return;
840
841 /*
842 * Make sure the chip is awake and the logger isn't active.
847cbebd
EP
843 * Do not send a stop fwlog command if the fw is hanged or if
844 * dbgpins are used (due to some fw bug).
95dac04f 845 */
1e41213f 846 if (wl1271_ps_elp_wakeup(wl))
afbe3718 847 goto out;
847cbebd
EP
848 if (!wl->watchdog_recovery &&
849 wl->conf.fwlog.output != WL12XX_FWLOG_OUTPUT_DBG_PINS)
1e41213f 850 wl12xx_cmd_stop_fwlog(wl);
95dac04f
IY
851
852 /* Read the first memory block address */
75fb4df7 853 ret = wlcore_fw_status(wl, wl->fw_status);
8b7c0fc3 854 if (ret < 0)
95dac04f
IY
855 goto out;
856
75fb4df7 857 addr = wl->fw_status->log_start_addr;
1e41213f 858 if (!addr)
95dac04f
IY
859 goto out;
860
1e41213f
IC
861 if (wl->conf.fwlog.mode == WL12XX_FWLOG_CONTINUOUS) {
862 offset = sizeof(addr) + sizeof(struct wl1271_rx_descriptor);
c83cb803 863 end_of_log = wl->fwlog_end;
1e41213f
IC
864 } else {
865 offset = sizeof(addr);
866 end_of_log = addr;
867 }
868
c83cb803
IC
869 old_part = wl->curr_part;
870 memset(&part, 0, sizeof(part));
871
95dac04f 872 /* Traverse the memory blocks linked list */
95dac04f 873 do {
c83cb803
IC
874 part.mem.start = wlcore_hw_convert_hwaddr(wl, addr);
875 part.mem.size = PAGE_SIZE;
876
877 ret = wlcore_set_partition(wl, &part);
878 if (ret < 0) {
879 wl1271_error("%s: set_partition start=0x%X size=%d",
880 __func__, part.mem.start, part.mem.size);
881 goto out;
882 }
883
884 memset(block, 0, wl->fw_mem_block_size);
885 ret = wlcore_read_hwaddr(wl, addr, block,
886 wl->fw_mem_block_size, false);
887
2b800407
IY
888 if (ret < 0)
889 goto out;
95dac04f
IY
890
891 /*
892 * Memory blocks are linked to one another. The first 4 bytes
893 * of each memory block hold the hardware address of the next
1e41213f
IC
894 * one. The last memory block points to the first one in
895 * on demand mode and is equal to 0x2000000 in continuous mode.
95dac04f 896 */
4d56ad9c 897 addr = le32_to_cpup((__le32 *)block);
c83cb803 898
1e41213f 899 if (!wl12xx_copy_fwlog(wl, block + offset,
c83cb803 900 wl->fw_mem_block_size - offset))
95dac04f 901 break;
1e41213f 902 } while (addr && (addr != end_of_log));
95dac04f
IY
903
904 wake_up_interruptible(&wl->fwlog_waitq);
905
906out:
907 kfree(block);
c83cb803 908 wlcore_set_partition(wl, &old_part);
95dac04f
IY
909}
910
50d26aa3
EP
911static void wlcore_save_freed_pkts(struct wl1271 *wl, struct wl12xx_vif *wlvif,
912 u8 hlid, struct ieee80211_sta *sta)
913{
914 struct wl1271_station *wl_sta;
30a00358 915 u32 sqn_recovery_padding = WL1271_TX_SQN_POST_RECOVERY_PADDING;
50d26aa3
EP
916
917 wl_sta = (void *)sta->drv_priv;
918 wl_sta->total_freed_pkts = wl->links[hlid].total_freed_pkts;
919
920 /*
921 * increment the initial seq number on recovery to account for
922 * transmitted packets that we haven't yet got in the FW status
923 */
30a00358
EP
924 if (wlvif->encryption_type == KEY_GEM)
925 sqn_recovery_padding = WL1271_TX_SQN_POST_RECOVERY_PADDING_GEM;
926
50d26aa3 927 if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags))
30a00358 928 wl_sta->total_freed_pkts += sqn_recovery_padding;
50d26aa3
EP
929}
930
931static void wlcore_save_freed_pkts_addr(struct wl1271 *wl,
932 struct wl12xx_vif *wlvif,
933 u8 hlid, const u8 *addr)
934{
935 struct ieee80211_sta *sta;
936 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
937
938 if (WARN_ON(hlid == WL12XX_INVALID_LINK_ID ||
939 is_zero_ether_addr(addr)))
940 return;
941
942 rcu_read_lock();
943 sta = ieee80211_find_sta(vif, addr);
944 if (sta)
945 wlcore_save_freed_pkts(wl, wlvif, hlid, sta);
946 rcu_read_unlock();
947}
948
6134323f
IY
949static void wlcore_print_recovery(struct wl1271 *wl)
950{
951 u32 pc = 0;
952 u32 hint_sts = 0;
953 int ret;
954
955 wl1271_info("Hardware recovery in progress. FW ver: %s",
956 wl->chip.fw_ver_str);
957
958 /* change partitions momentarily so we can read the FW pc */
b0f0ad39
IY
959 ret = wlcore_set_partition(wl, &wl->ptable[PART_BOOT]);
960 if (ret < 0)
961 return;
6134323f
IY
962
963 ret = wlcore_read_reg(wl, REG_PC_ON_RECOVERY, &pc);
964 if (ret < 0)
965 return;
966
967 ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &hint_sts);
968 if (ret < 0)
969 return;
970
c108c905
LC
971 wl1271_info("pc: 0x%x, hint_sts: 0x%08x count: %d",
972 pc, hint_sts, ++wl->recovery_count);
6134323f
IY
973
974 wlcore_set_partition(wl, &wl->ptable[PART_WORK]);
975}
976
977
52b0e7a6
JO
978static void wl1271_recovery_work(struct work_struct *work)
979{
980 struct wl1271 *wl =
981 container_of(work, struct wl1271, recovery_work);
48e93e40 982 struct wl12xx_vif *wlvif;
6e8cd331 983 struct ieee80211_vif *vif;
52b0e7a6
JO
984
985 mutex_lock(&wl->mutex);
986
4cc53383 987 if (wl->state == WLCORE_STATE_OFF || wl->plt)
f0277434 988 goto out_unlock;
52b0e7a6 989
aafec111 990 if (!test_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags)) {
5cc14c04
BB
991 if (wl->conf.fwlog.output == WL12XX_FWLOG_OUTPUT_HOST)
992 wl12xx_read_fwlog_panic(wl);
aafec111
AN
993 wlcore_print_recovery(wl);
994 }
52b0e7a6 995
7230341f 996 BUG_ON(wl->conf.recovery.bug_on_recovery &&
e9ba7152 997 !test_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags));
2a5bff09 998
7230341f 999 if (wl->conf.recovery.no_recovery) {
34785be5 1000 wl1271_info("No recovery (chosen on module load). Fw will remain stuck.");
34785be5
AN
1001 goto out_unlock;
1002 }
1003
7dece1c8 1004 /* Prevent spurious TX during FW restart */
66396114 1005 wlcore_stop_queues(wl, WLCORE_QUEUE_STOP_REASON_FW_RESTART);
7dece1c8 1006
52b0e7a6 1007 /* reboot the chipset */
6e8cd331
EP
1008 while (!list_empty(&wl->wlvif_list)) {
1009 wlvif = list_first_entry(&wl->wlvif_list,
1010 struct wl12xx_vif, list);
1011 vif = wl12xx_wlvif_to_vif(wlvif);
50d26aa3
EP
1012
1013 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
1014 test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
1015 wlcore_save_freed_pkts_addr(wl, wlvif, wlvif->sta.hlid,
1016 vif->bss_conf.bssid);
1017 }
1018
6e8cd331
EP
1019 __wl1271_op_remove_interface(wl, vif, false);
1020 }
c24ec83b
IY
1021
1022 wlcore_op_stop_locked(wl);
baacb9ae 1023
52b0e7a6
JO
1024 ieee80211_restart_hw(wl->hw);
1025
7dece1c8
AN
1026 /*
1027 * Its safe to enable TX now - the queues are stopped after a request
1028 * to restart the HW.
1029 */
66396114 1030 wlcore_wake_queues(wl, WLCORE_QUEUE_STOP_REASON_FW_RESTART);
c24ec83b 1031
f0277434 1032out_unlock:
b034fd6f
AN
1033 wl->watchdog_recovery = false;
1034 clear_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags);
52b0e7a6
JO
1035 mutex_unlock(&wl->mutex);
1036}
1037
b0f0ad39 1038static int wlcore_fw_wakeup(struct wl1271 *wl)
f5fc0f86 1039{
b0f0ad39 1040 return wlcore_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG, ELPCTRL_WAKE_UP);
f5fc0f86
LC
1041}
1042
1043static int wl1271_setup(struct wl1271 *wl)
1044{
75fb4df7
EP
1045 wl->raw_fw_status = kzalloc(wl->fw_status_len, GFP_KERNEL);
1046 if (!wl->raw_fw_status)
1047 goto err;
f5fc0f86 1048
75fb4df7
EP
1049 wl->fw_status = kzalloc(sizeof(*wl->fw_status), GFP_KERNEL);
1050 if (!wl->fw_status)
1051 goto err;
0afd04e5 1052
5cbba2d4 1053 wl->tx_res_if = kzalloc(sizeof(*wl->tx_res_if), GFP_KERNEL);
75fb4df7
EP
1054 if (!wl->tx_res_if)
1055 goto err;
f5fc0f86 1056
f5fc0f86 1057 return 0;
75fb4df7
EP
1058err:
1059 kfree(wl->fw_status);
1060 kfree(wl->raw_fw_status);
1061 return -ENOMEM;
f5fc0f86
LC
1062}
1063
30c5dbd1 1064static int wl12xx_set_power_on(struct wl1271 *wl)
f5fc0f86 1065{
30c5dbd1 1066 int ret;
f5fc0f86 1067
01ac17ec 1068 msleep(WL1271_PRE_POWER_ON_SLEEP);
2cc78ff7
OBC
1069 ret = wl1271_power_on(wl);
1070 if (ret < 0)
1071 goto out;
f5fc0f86 1072 msleep(WL1271_POWER_ON_SLEEP);
9b280722
TP
1073 wl1271_io_reset(wl);
1074 wl1271_io_init(wl);
f5fc0f86 1075
b0f0ad39
IY
1076 ret = wlcore_set_partition(wl, &wl->ptable[PART_BOOT]);
1077 if (ret < 0)
1078 goto fail;
f5fc0f86
LC
1079
1080 /* ELP module wake up */
b0f0ad39
IY
1081 ret = wlcore_fw_wakeup(wl);
1082 if (ret < 0)
1083 goto fail;
f5fc0f86 1084
30c5dbd1
LC
1085out:
1086 return ret;
b0f0ad39
IY
1087
1088fail:
1089 wl1271_power_off(wl);
1090 return ret;
30c5dbd1 1091}
f5fc0f86 1092
3fcdab70 1093static int wl12xx_chip_wakeup(struct wl1271 *wl, bool plt)
30c5dbd1
LC
1094{
1095 int ret = 0;
1096
1097 ret = wl12xx_set_power_on(wl);
1098 if (ret < 0)
1099 goto out;
f5fc0f86 1100
e62c9ce4
LC
1101 /*
1102 * For wl127x based devices we could use the default block
1103 * size (512 bytes), but due to a bug in the sdio driver, we
1104 * need to set it explicitly after the chip is powered on. To
1105 * simplify the code and since the performance impact is
1106 * negligible, we use the same block size for all different
1107 * chip types.
b5d6d9b2
LC
1108 *
1109 * Check if the bus supports blocksize alignment and, if it
1110 * doesn't, make sure we don't have the quirk.
e62c9ce4 1111 */
b5d6d9b2
LC
1112 if (!wl1271_set_block_size(wl))
1113 wl->quirks &= ~WLCORE_QUIRK_TX_BLOCKSIZE_ALIGN;
f5fc0f86 1114
6f7dd16c 1115 /* TODO: make sure the lower driver has set things up correctly */
0830ceed 1116
6f7dd16c
LC
1117 ret = wl1271_setup(wl);
1118 if (ret < 0)
9ccd9217 1119 goto out;
f5fc0f86 1120
3fcdab70
EP
1121 ret = wl12xx_fetch_firmware(wl, plt);
1122 if (ret < 0)
1123 goto out;
f5fc0f86 1124
f5fc0f86
LC
1125out:
1126 return ret;
1127}
1128
7019c80e 1129int wl1271_plt_start(struct wl1271 *wl, const enum plt_mode plt_mode)
f5fc0f86 1130{
9ccd9217 1131 int retries = WL1271_BOOT_RETRIES;
6f07b72a 1132 struct wiphy *wiphy = wl->hw->wiphy;
7019c80e
YS
1133
1134 static const char* const PLT_MODE[] = {
1135 "PLT_OFF",
1136 "PLT_ON",
dd491ffb
YS
1137 "PLT_FEM_DETECT",
1138 "PLT_CHIP_AWAKE"
7019c80e
YS
1139 };
1140
f5fc0f86
LC
1141 int ret;
1142
1143 mutex_lock(&wl->mutex);
1144
1145 wl1271_notice("power up");
1146
4cc53383 1147 if (wl->state != WLCORE_STATE_OFF) {
f5fc0f86
LC
1148 wl1271_error("cannot go into PLT state because not "
1149 "in off state: %d", wl->state);
1150 ret = -EBUSY;
1151 goto out;
1152 }
1153
7019c80e
YS
1154 /* Indicate to lower levels that we are now in PLT mode */
1155 wl->plt = true;
1156 wl->plt_mode = plt_mode;
1157
9ccd9217
JO
1158 while (retries) {
1159 retries--;
3fcdab70 1160 ret = wl12xx_chip_wakeup(wl, true);
9ccd9217
JO
1161 if (ret < 0)
1162 goto power_off;
f5fc0f86 1163
dd491ffb
YS
1164 if (plt_mode != PLT_CHIP_AWAKE) {
1165 ret = wl->ops->plt_init(wl);
1166 if (ret < 0)
1167 goto power_off;
1168 }
eb5b28d0 1169
4cc53383 1170 wl->state = WLCORE_STATE_ON;
7019c80e
YS
1171 wl1271_notice("firmware booted in PLT mode %s (%s)",
1172 PLT_MODE[plt_mode],
4b7fac77 1173 wl->chip.fw_ver_str);
e7ddf549 1174
6f07b72a
GK
1175 /* update hw/fw version info in wiphy struct */
1176 wiphy->hw_version = wl->chip.id;
1177 strncpy(wiphy->fw_version, wl->chip.fw_ver_str,
1178 sizeof(wiphy->fw_version));
1179
9ccd9217 1180 goto out;
eb5b28d0 1181
9ccd9217
JO
1182power_off:
1183 wl1271_power_off(wl);
1184 }
f5fc0f86 1185
7019c80e
YS
1186 wl->plt = false;
1187 wl->plt_mode = PLT_OFF;
1188
9ccd9217
JO
1189 wl1271_error("firmware boot in PLT mode failed despite %d retries",
1190 WL1271_BOOT_RETRIES);
f5fc0f86
LC
1191out:
1192 mutex_unlock(&wl->mutex);
1193
1194 return ret;
1195}
1196
f3df1331 1197int wl1271_plt_stop(struct wl1271 *wl)
f5fc0f86
LC
1198{
1199 int ret = 0;
1200
f5fc0f86
LC
1201 wl1271_notice("power down");
1202
46b0cc9f
IY
1203 /*
1204 * Interrupts must be disabled before setting the state to OFF.
1205 * Otherwise, the interrupt handler might be called and exit without
1206 * reading the interrupt status.
1207 */
dd5512eb 1208 wlcore_disable_interrupts(wl);
f3df1331 1209 mutex_lock(&wl->mutex);
3fcdab70 1210 if (!wl->plt) {
f3df1331 1211 mutex_unlock(&wl->mutex);
46b0cc9f
IY
1212
1213 /*
1214 * This will not necessarily enable interrupts as interrupts
1215 * may have been disabled when op_stop was called. It will,
1216 * however, balance the above call to disable_interrupts().
1217 */
dd5512eb 1218 wlcore_enable_interrupts(wl);
46b0cc9f 1219
f5fc0f86
LC
1220 wl1271_error("cannot power down because not in PLT "
1221 "state: %d", wl->state);
1222 ret = -EBUSY;
1223 goto out;
1224 }
1225
f5fc0f86 1226 mutex_unlock(&wl->mutex);
f3df1331 1227
a620865e
IY
1228 wl1271_flush_deferred_work(wl);
1229 cancel_work_sync(&wl->netstack_work);
52b0e7a6 1230 cancel_work_sync(&wl->recovery_work);
f6fbeccd 1231 cancel_delayed_work_sync(&wl->elp_work);
55df5afb 1232 cancel_delayed_work_sync(&wl->tx_watchdog_work);
a454969e
IY
1233
1234 mutex_lock(&wl->mutex);
1235 wl1271_power_off(wl);
f6fbeccd 1236 wl->flags = 0;
2f18cf7c 1237 wl->sleep_auth = WL1271_PSM_ILLEGAL;
4cc53383 1238 wl->state = WLCORE_STATE_OFF;
3fcdab70 1239 wl->plt = false;
7019c80e 1240 wl->plt_mode = PLT_OFF;
f6fbeccd 1241 wl->rx_counter = 0;
a454969e
IY
1242 mutex_unlock(&wl->mutex);
1243
4ae3fa87
JO
1244out:
1245 return ret;
1246}
1247
36323f81
TH
1248static void wl1271_op_tx(struct ieee80211_hw *hw,
1249 struct ieee80211_tx_control *control,
1250 struct sk_buff *skb)
f5fc0f86
LC
1251{
1252 struct wl1271 *wl = hw->priv;
a8ab39a4
EP
1253 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1254 struct ieee80211_vif *vif = info->control.vif;
0f168014 1255 struct wl12xx_vif *wlvif = NULL;
830fb67b 1256 unsigned long flags;
708bb3cf 1257 int q, mapping;
d6a3cc2e 1258 u8 hlid;
f5fc0f86 1259
f4d02007
AN
1260 if (!vif) {
1261 wl1271_debug(DEBUG_TX, "DROP skb with no vif");
1262 ieee80211_free_txskb(hw, skb);
1263 return;
1264 }
0f168014 1265
f4d02007 1266 wlvif = wl12xx_vif_to_data(vif);
708bb3cf
AN
1267 mapping = skb_get_queue_mapping(skb);
1268 q = wl1271_tx_get_queue(mapping);
b07d4037 1269
36323f81 1270 hlid = wl12xx_tx_get_hlid(wl, wlvif, skb, control->sta);
b07d4037 1271
830fb67b 1272 spin_lock_irqsave(&wl->wl_lock, flags);
b07d4037 1273
66396114
AN
1274 /*
1275 * drop the packet if the link is invalid or the queue is stopped
1276 * for any reason but watermark. Watermark is a "soft"-stop so we
1277 * allow these packets through.
1278 */
d6a3cc2e 1279 if (hlid == WL12XX_INVALID_LINK_ID ||
f4d02007 1280 (!test_bit(hlid, wlvif->links_map)) ||
d6037d22
AN
1281 (wlcore_is_queue_stopped_locked(wl, wlvif, q) &&
1282 !wlcore_is_queue_stopped_by_reason_locked(wl, wlvif, q,
66396114 1283 WLCORE_QUEUE_STOP_REASON_WATERMARK))) {
d6a3cc2e 1284 wl1271_debug(DEBUG_TX, "DROP skb hlid %d q %d", hlid, q);
5de8eef4 1285 ieee80211_free_txskb(hw, skb);
d6a3cc2e 1286 goto out;
a8c0ddb5 1287 }
f5fc0f86 1288
8ccd16e6
EP
1289 wl1271_debug(DEBUG_TX, "queue skb hlid %d q %d len %d",
1290 hlid, q, skb->len);
d6a3cc2e
EP
1291 skb_queue_tail(&wl->links[hlid].tx_queue[q], skb);
1292
04b4d69c 1293 wl->tx_queue_count[q]++;
f4d02007 1294 wlvif->tx_queue_count[q]++;
04b4d69c
AN
1295
1296 /*
1297 * The workqueue is slow to process the tx_queue and we need stop
1298 * the queue here, otherwise the queue will get too long.
1299 */
1c33db78 1300 if (wlvif->tx_queue_count[q] >= WL1271_TX_QUEUE_HIGH_WATERMARK &&
d6037d22 1301 !wlcore_is_queue_stopped_by_reason_locked(wl, wlvif, q,
8cdc44aa 1302 WLCORE_QUEUE_STOP_REASON_WATERMARK)) {
04b4d69c 1303 wl1271_debug(DEBUG_TX, "op_tx: stopping queues for q %d", q);
1c33db78 1304 wlcore_stop_queue_locked(wl, wlvif, q,
66396114 1305 WLCORE_QUEUE_STOP_REASON_WATERMARK);
04b4d69c
AN
1306 }
1307
f5fc0f86
LC
1308 /*
1309 * The chip specific setup must run before the first TX packet -
1310 * before that, the tx_work will not be initialized!
1311 */
1312
b07d4037
IY
1313 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
1314 !test_bit(WL1271_FLAG_TX_PENDING, &wl->flags))
a522550a 1315 ieee80211_queue_work(wl->hw, &wl->tx_work);
b07d4037 1316
04216da3 1317out:
b07d4037 1318 spin_unlock_irqrestore(&wl->wl_lock, flags);
f5fc0f86
LC
1319}
1320
ae47c45f
SL
1321int wl1271_tx_dummy_packet(struct wl1271 *wl)
1322{
990f5de7 1323 unsigned long flags;
14623787
AN
1324 int q;
1325
1326 /* no need to queue a new dummy packet if one is already pending */
1327 if (test_bit(WL1271_FLAG_DUMMY_PACKET_PENDING, &wl->flags))
1328 return 0;
1329
1330 q = wl1271_tx_get_queue(skb_get_queue_mapping(wl->dummy_packet));
990f5de7
IY
1331
1332 spin_lock_irqsave(&wl->wl_lock, flags);
1333 set_bit(WL1271_FLAG_DUMMY_PACKET_PENDING, &wl->flags);
f1a46384 1334 wl->tx_queue_count[q]++;
990f5de7
IY
1335 spin_unlock_irqrestore(&wl->wl_lock, flags);
1336
1337 /* The FW is low on RX memory blocks, so send the dummy packet asap */
1338 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags))
eb96f841 1339 return wlcore_tx_work_locked(wl);
990f5de7
IY
1340
1341 /*
1342 * If the FW TX is busy, TX work will be scheduled by the threaded
1343 * interrupt handler function
1344 */
1345 return 0;
1346}
1347
1348/*
1349 * The size of the dummy packet should be at least 1400 bytes. However, in
1350 * order to minimize the number of bus transactions, aligning it to 512 bytes
1351 * boundaries could be beneficial, performance wise
1352 */
1353#define TOTAL_TX_DUMMY_PACKET_SIZE (ALIGN(1400, 512))
1354
cf27d867 1355static struct sk_buff *wl12xx_alloc_dummy_packet(struct wl1271 *wl)
990f5de7
IY
1356{
1357 struct sk_buff *skb;
ae47c45f 1358 struct ieee80211_hdr_3addr *hdr;
990f5de7
IY
1359 unsigned int dummy_packet_size;
1360
1361 dummy_packet_size = TOTAL_TX_DUMMY_PACKET_SIZE -
1362 sizeof(struct wl1271_tx_hw_descr) - sizeof(*hdr);
ae47c45f 1363
990f5de7 1364 skb = dev_alloc_skb(TOTAL_TX_DUMMY_PACKET_SIZE);
ae47c45f 1365 if (!skb) {
990f5de7
IY
1366 wl1271_warning("Failed to allocate a dummy packet skb");
1367 return NULL;
ae47c45f
SL
1368 }
1369
1370 skb_reserve(skb, sizeof(struct wl1271_tx_hw_descr));
1371
1372 hdr = (struct ieee80211_hdr_3addr *) skb_put(skb, sizeof(*hdr));
1373 memset(hdr, 0, sizeof(*hdr));
1374 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
990f5de7
IY
1375 IEEE80211_STYPE_NULLFUNC |
1376 IEEE80211_FCTL_TODS);
ae47c45f 1377
990f5de7 1378 memset(skb_put(skb, dummy_packet_size), 0, dummy_packet_size);
ae47c45f 1379
18b92ffa
LC
1380 /* Dummy packets require the TID to be management */
1381 skb->priority = WL1271_TID_MGMT;
ae47c45f 1382
990f5de7 1383 /* Initialize all fields that might be used */
86c438f4 1384 skb_set_queue_mapping(skb, 0);
990f5de7 1385 memset(IEEE80211_SKB_CB(skb), 0, sizeof(struct ieee80211_tx_info));
ae47c45f 1386
990f5de7 1387 return skb;
ae47c45f
SL
1388}
1389
990f5de7 1390
f634a4e7 1391#ifdef CONFIG_PM
22479972 1392static int
50ac6607 1393wl1271_validate_wowlan_pattern(struct cfg80211_pkt_pattern *p)
b95d7cef
ES
1394{
1395 int num_fields = 0, in_field = 0, fields_size = 0;
1396 int i, pattern_len = 0;
1397
1398 if (!p->mask) {
1399 wl1271_warning("No mask in WoWLAN pattern");
1400 return -EINVAL;
1401 }
1402
1403 /*
1404 * The pattern is broken up into segments of bytes at different offsets
1405 * that need to be checked by the FW filter. Each segment is called
1406 * a field in the FW API. We verify that the total number of fields
1407 * required for this pattern won't exceed FW limits (8)
1408 * as well as the total fields buffer won't exceed the FW limit.
1409 * Note that if there's a pattern which crosses Ethernet/IP header
1410 * boundary a new field is required.
1411 */
1412 for (i = 0; i < p->pattern_len; i++) {
1413 if (test_bit(i, (unsigned long *)p->mask)) {
1414 if (!in_field) {
1415 in_field = 1;
1416 pattern_len = 1;
1417 } else {
1418 if (i == WL1271_RX_FILTER_ETH_HEADER_SIZE) {
1419 num_fields++;
1420 fields_size += pattern_len +
1421 RX_FILTER_FIELD_OVERHEAD;
1422 pattern_len = 1;
1423 } else
1424 pattern_len++;
1425 }
1426 } else {
1427 if (in_field) {
1428 in_field = 0;
1429 fields_size += pattern_len +
1430 RX_FILTER_FIELD_OVERHEAD;
1431 num_fields++;
1432 }
1433 }
1434 }
1435
1436 if (in_field) {
1437 fields_size += pattern_len + RX_FILTER_FIELD_OVERHEAD;
1438 num_fields++;
1439 }
1440
1441 if (num_fields > WL1271_RX_FILTER_MAX_FIELDS) {
1442 wl1271_warning("RX Filter too complex. Too many segments");
1443 return -EINVAL;
1444 }
1445
1446 if (fields_size > WL1271_RX_FILTER_MAX_FIELDS_SIZE) {
1447 wl1271_warning("RX filter pattern is too big");
1448 return -E2BIG;
1449 }
1450
1451 return 0;
1452}
1453
a6eab0c8
ES
1454struct wl12xx_rx_filter *wl1271_rx_filter_alloc(void)
1455{
1456 return kzalloc(sizeof(struct wl12xx_rx_filter), GFP_KERNEL);
1457}
1458
1459void wl1271_rx_filter_free(struct wl12xx_rx_filter *filter)
1460{
1461 int i;
1462
1463 if (filter == NULL)
1464 return;
1465
1466 for (i = 0; i < filter->num_fields; i++)
1467 kfree(filter->fields[i].pattern);
1468
1469 kfree(filter);
1470}
1471
1472int wl1271_rx_filter_alloc_field(struct wl12xx_rx_filter *filter,
1473 u16 offset, u8 flags,
922bd80f 1474 const u8 *pattern, u8 len)
a6eab0c8
ES
1475{
1476 struct wl12xx_rx_filter_field *field;
1477
1478 if (filter->num_fields == WL1271_RX_FILTER_MAX_FIELDS) {
1479 wl1271_warning("Max fields per RX filter. can't alloc another");
1480 return -EINVAL;
1481 }
1482
1483 field = &filter->fields[filter->num_fields];
1484
1485 field->pattern = kzalloc(len, GFP_KERNEL);
1486 if (!field->pattern) {
1487 wl1271_warning("Failed to allocate RX filter pattern");
1488 return -ENOMEM;
1489 }
1490
1491 filter->num_fields++;
1492
1493 field->offset = cpu_to_le16(offset);
1494 field->flags = flags;
1495 field->len = len;
1496 memcpy(field->pattern, pattern, len);
1497
1498 return 0;
1499}
1500
1501int wl1271_rx_filter_get_fields_size(struct wl12xx_rx_filter *filter)
1502{
1503 int i, fields_size = 0;
1504
1505 for (i = 0; i < filter->num_fields; i++)
1506 fields_size += filter->fields[i].len +
1507 sizeof(struct wl12xx_rx_filter_field) -
1508 sizeof(u8 *);
1509
1510 return fields_size;
1511}
1512
1513void wl1271_rx_filter_flatten_fields(struct wl12xx_rx_filter *filter,
1514 u8 *buf)
1515{
1516 int i;
1517 struct wl12xx_rx_filter_field *field;
1518
1519 for (i = 0; i < filter->num_fields; i++) {
1520 field = (struct wl12xx_rx_filter_field *)buf;
1521
1522 field->offset = filter->fields[i].offset;
1523 field->flags = filter->fields[i].flags;
1524 field->len = filter->fields[i].len;
1525
1526 memcpy(&field->pattern, filter->fields[i].pattern, field->len);
1527 buf += sizeof(struct wl12xx_rx_filter_field) -
1528 sizeof(u8 *) + field->len;
1529 }
1530}
1531
b95d7cef
ES
1532/*
1533 * Allocates an RX filter returned through f
1534 * which needs to be freed using rx_filter_free()
1535 */
50ac6607
AK
1536static int
1537wl1271_convert_wowlan_pattern_to_rx_filter(struct cfg80211_pkt_pattern *p,
1538 struct wl12xx_rx_filter **f)
b95d7cef
ES
1539{
1540 int i, j, ret = 0;
1541 struct wl12xx_rx_filter *filter;
1542 u16 offset;
1543 u8 flags, len;
1544
1545 filter = wl1271_rx_filter_alloc();
1546 if (!filter) {
1547 wl1271_warning("Failed to alloc rx filter");
1548 ret = -ENOMEM;
1549 goto err;
1550 }
1551
1552 i = 0;
1553 while (i < p->pattern_len) {
1554 if (!test_bit(i, (unsigned long *)p->mask)) {
1555 i++;
1556 continue;
1557 }
1558
1559 for (j = i; j < p->pattern_len; j++) {
1560 if (!test_bit(j, (unsigned long *)p->mask))
1561 break;
1562
1563 if (i < WL1271_RX_FILTER_ETH_HEADER_SIZE &&
1564 j >= WL1271_RX_FILTER_ETH_HEADER_SIZE)
1565 break;
1566 }
1567
1568 if (i < WL1271_RX_FILTER_ETH_HEADER_SIZE) {
1569 offset = i;
1570 flags = WL1271_RX_FILTER_FLAG_ETHERNET_HEADER;
1571 } else {
1572 offset = i - WL1271_RX_FILTER_ETH_HEADER_SIZE;
1573 flags = WL1271_RX_FILTER_FLAG_IP_HEADER;
1574 }
1575
1576 len = j - i;
1577
1578 ret = wl1271_rx_filter_alloc_field(filter,
1579 offset,
1580 flags,
1581 &p->pattern[i], len);
1582 if (ret)
1583 goto err;
1584
1585 i = j;
1586 }
1587
1588 filter->action = FILTER_SIGNAL;
1589
1590 *f = filter;
1591 return 0;
1592
1593err:
1594 wl1271_rx_filter_free(filter);
1595 *f = NULL;
1596
1597 return ret;
1598}
1599
1600static int wl1271_configure_wowlan(struct wl1271 *wl,
1601 struct cfg80211_wowlan *wow)
1602{
1603 int i, ret;
1604
1605 if (!wow || wow->any || !wow->n_patterns) {
c439a1ca
AN
1606 ret = wl1271_acx_default_rx_filter_enable(wl, 0,
1607 FILTER_SIGNAL);
1608 if (ret)
1609 goto out;
1610
1611 ret = wl1271_rx_filter_clear_all(wl);
1612 if (ret)
1613 goto out;
1614
b95d7cef
ES
1615 return 0;
1616 }
1617
1618 if (WARN_ON(wow->n_patterns > WL1271_MAX_RX_FILTERS))
1619 return -EINVAL;
1620
1621 /* Validate all incoming patterns before clearing current FW state */
1622 for (i = 0; i < wow->n_patterns; i++) {
1623 ret = wl1271_validate_wowlan_pattern(&wow->patterns[i]);
1624 if (ret) {
1625 wl1271_warning("Bad wowlan pattern %d", i);
1626 return ret;
1627 }
1628 }
1629
c439a1ca
AN
1630 ret = wl1271_acx_default_rx_filter_enable(wl, 0, FILTER_SIGNAL);
1631 if (ret)
1632 goto out;
1633
1634 ret = wl1271_rx_filter_clear_all(wl);
1635 if (ret)
1636 goto out;
b95d7cef
ES
1637
1638 /* Translate WoWLAN patterns into filters */
1639 for (i = 0; i < wow->n_patterns; i++) {
50ac6607 1640 struct cfg80211_pkt_pattern *p;
b95d7cef
ES
1641 struct wl12xx_rx_filter *filter = NULL;
1642
1643 p = &wow->patterns[i];
1644
1645 ret = wl1271_convert_wowlan_pattern_to_rx_filter(p, &filter);
1646 if (ret) {
1647 wl1271_warning("Failed to create an RX filter from "
1648 "wowlan pattern %d", i);
1649 goto out;
1650 }
1651
1652 ret = wl1271_rx_filter_enable(wl, i, 1, filter);
1653
1654 wl1271_rx_filter_free(filter);
1655 if (ret)
1656 goto out;
1657 }
1658
1659 ret = wl1271_acx_default_rx_filter_enable(wl, 1, FILTER_DROP);
1660
1661out:
1662 return ret;
1663}
1664
dae728fe 1665static int wl1271_configure_suspend_sta(struct wl1271 *wl,
b95d7cef
ES
1666 struct wl12xx_vif *wlvif,
1667 struct cfg80211_wowlan *wow)
dae728fe
ES
1668{
1669 int ret = 0;
1670
dae728fe 1671 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
c56dbd57 1672 goto out;
dae728fe 1673
c439a1ca
AN
1674 ret = wl1271_configure_wowlan(wl, wow);
1675 if (ret < 0)
6d5a748d 1676 goto out;
c439a1ca 1677
11bc97eb
ES
1678 if ((wl->conf.conn.suspend_wake_up_event ==
1679 wl->conf.conn.wake_up_event) &&
1680 (wl->conf.conn.suspend_listen_interval ==
1681 wl->conf.conn.listen_interval))
6d5a748d 1682 goto out;
11bc97eb 1683
dae728fe
ES
1684 ret = wl1271_acx_wake_up_conditions(wl, wlvif,
1685 wl->conf.conn.suspend_wake_up_event,
1686 wl->conf.conn.suspend_listen_interval);
1687
1688 if (ret < 0)
1689 wl1271_error("suspend: set wake up conditions failed: %d", ret);
c56dbd57 1690out:
dae728fe
ES
1691 return ret;
1692
1693}
9439064c 1694
0603d891 1695static int wl1271_configure_suspend_ap(struct wl1271 *wl,
b8714d1b
EP
1696 struct wl12xx_vif *wlvif,
1697 struct cfg80211_wowlan *wow)
8a7cf3fe 1698{
e85d1629 1699 int ret = 0;
8a7cf3fe 1700
53d40d0b 1701 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags))
c56dbd57 1702 goto out;
e85d1629 1703
0603d891 1704 ret = wl1271_acx_beacon_filter_opt(wl, wlvif, true);
b8714d1b
EP
1705 if (ret < 0)
1706 goto out;
1707
1708 ret = wl1271_configure_wowlan(wl, wow);
1709 if (ret < 0)
1710 goto out;
8a7cf3fe 1711
c56dbd57 1712out:
8a7cf3fe
EP
1713 return ret;
1714
1715}
1716
d2d66c56 1717static int wl1271_configure_suspend(struct wl1271 *wl,
b95d7cef
ES
1718 struct wl12xx_vif *wlvif,
1719 struct cfg80211_wowlan *wow)
8a7cf3fe 1720{
dae728fe 1721 if (wlvif->bss_type == BSS_TYPE_STA_BSS)
b95d7cef 1722 return wl1271_configure_suspend_sta(wl, wlvif, wow);
536129c8 1723 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
b8714d1b 1724 return wl1271_configure_suspend_ap(wl, wlvif, wow);
8a7cf3fe
EP
1725 return 0;
1726}
1727
8f6ac537 1728static void wl1271_configure_resume(struct wl1271 *wl, struct wl12xx_vif *wlvif)
9439064c 1729{
dae728fe 1730 int ret = 0;
536129c8 1731 bool is_ap = wlvif->bss_type == BSS_TYPE_AP_BSS;
dae728fe 1732 bool is_sta = wlvif->bss_type == BSS_TYPE_STA_BSS;
9439064c 1733
dae728fe 1734 if ((!is_ap) && (!is_sta))
9439064c
EP
1735 return;
1736
b8714d1b
EP
1737 if ((is_sta && !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) ||
1738 (is_ap && !test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)))
d49524d3
EP
1739 return;
1740
b8714d1b 1741 wl1271_configure_wowlan(wl, NULL);
b95d7cef 1742
b8714d1b 1743 if (is_sta) {
11bc97eb
ES
1744 if ((wl->conf.conn.suspend_wake_up_event ==
1745 wl->conf.conn.wake_up_event) &&
1746 (wl->conf.conn.suspend_listen_interval ==
1747 wl->conf.conn.listen_interval))
6d5a748d 1748 return;
11bc97eb 1749
dae728fe
ES
1750 ret = wl1271_acx_wake_up_conditions(wl, wlvif,
1751 wl->conf.conn.wake_up_event,
1752 wl->conf.conn.listen_interval);
1753
1754 if (ret < 0)
1755 wl1271_error("resume: wake up conditions failed: %d",
1756 ret);
1757
1758 } else if (is_ap) {
1759 ret = wl1271_acx_beacon_filter_opt(wl, wlvif, false);
1760 }
9439064c
EP
1761}
1762
402e4861
EP
1763static int wl1271_op_suspend(struct ieee80211_hw *hw,
1764 struct cfg80211_wowlan *wow)
1765{
1766 struct wl1271 *wl = hw->priv;
6e8cd331 1767 struct wl12xx_vif *wlvif;
4a859df8
EP
1768 int ret;
1769
402e4861 1770 wl1271_debug(DEBUG_MAC80211, "mac80211 suspend wow=%d", !!wow);
b95d7cef 1771 WARN_ON(!wow);
f44e5868 1772
96caded8
AN
1773 /* we want to perform the recovery before suspending */
1774 if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags)) {
1775 wl1271_warning("postponing suspend to perform recovery");
1776 return -EBUSY;
1777 }
1778
b9239b66
AN
1779 wl1271_tx_flush(wl);
1780
c56dbd57 1781 mutex_lock(&wl->mutex);
6d5a748d
RA
1782
1783 ret = wl1271_ps_elp_wakeup(wl);
1784 if (ret < 0)
1785 return ret;
1786
4a859df8 1787 wl->wow_enabled = true;
6e8cd331 1788 wl12xx_for_each_wlvif(wl, wlvif) {
b95d7cef 1789 ret = wl1271_configure_suspend(wl, wlvif, wow);
6e8cd331 1790 if (ret < 0) {
cd840f6a 1791 mutex_unlock(&wl->mutex);
6e8cd331
EP
1792 wl1271_warning("couldn't prepare device to suspend");
1793 return ret;
1794 }
4a859df8 1795 }
6d5a748d
RA
1796
1797 /* disable fast link flow control notifications from FW */
1798 ret = wlcore_hw_interrupt_notify(wl, false);
1799 if (ret < 0)
1800 goto out_sleep;
1801
1802 /* if filtering is enabled, configure the FW to drop all RX BA frames */
1803 ret = wlcore_hw_rx_ba_filter(wl,
1804 !!wl->conf.conn.suspend_rx_ba_activity);
1805 if (ret < 0)
1806 goto out_sleep;
1807
1808out_sleep:
1809 wl1271_ps_elp_sleep(wl);
c56dbd57 1810 mutex_unlock(&wl->mutex);
6d5a748d
RA
1811
1812 if (ret < 0) {
1813 wl1271_warning("couldn't prepare device to suspend");
1814 return ret;
1815 }
1816
4a859df8
EP
1817 /* flush any remaining work */
1818 wl1271_debug(DEBUG_MAC80211, "flushing remaining works");
f44e5868 1819
4a859df8
EP
1820 /*
1821 * disable and re-enable interrupts in order to flush
1822 * the threaded_irq
1823 */
dd5512eb 1824 wlcore_disable_interrupts(wl);
4a859df8
EP
1825
1826 /*
1827 * set suspended flag to avoid triggering a new threaded_irq
1828 * work. no need for spinlock as interrupts are disabled.
1829 */
1830 set_bit(WL1271_FLAG_SUSPENDED, &wl->flags);
1831
dd5512eb 1832 wlcore_enable_interrupts(wl);
4a859df8 1833 flush_work(&wl->tx_work);
4a859df8 1834 flush_delayed_work(&wl->elp_work);
f44e5868 1835
9be86cf0
AN
1836 /*
1837 * Cancel the watchdog even if above tx_flush failed. We will detect
1838 * it on resume anyway.
1839 */
1840 cancel_delayed_work(&wl->tx_watchdog_work);
1841
402e4861
EP
1842 return 0;
1843}
1844
1845static int wl1271_op_resume(struct ieee80211_hw *hw)
1846{
1847 struct wl1271 *wl = hw->priv;
6e8cd331 1848 struct wl12xx_vif *wlvif;
4a859df8 1849 unsigned long flags;
ea0a3cf9 1850 bool run_irq_work = false, pending_recovery;
725b8277 1851 int ret;
4a859df8 1852
402e4861
EP
1853 wl1271_debug(DEBUG_MAC80211, "mac80211 resume wow=%d",
1854 wl->wow_enabled);
4a859df8 1855 WARN_ON(!wl->wow_enabled);
f44e5868
EP
1856
1857 /*
1858 * re-enable irq_work enqueuing, and call irq_work directly if
1859 * there is a pending work.
1860 */
4a859df8
EP
1861 spin_lock_irqsave(&wl->wl_lock, flags);
1862 clear_bit(WL1271_FLAG_SUSPENDED, &wl->flags);
1863 if (test_and_clear_bit(WL1271_FLAG_PENDING_WORK, &wl->flags))
1864 run_irq_work = true;
1865 spin_unlock_irqrestore(&wl->wl_lock, flags);
9439064c 1866
725b8277
AN
1867 mutex_lock(&wl->mutex);
1868
ea0a3cf9
AN
1869 /* test the recovery flag before calling any SDIO functions */
1870 pending_recovery = test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS,
1871 &wl->flags);
1872
4a859df8
EP
1873 if (run_irq_work) {
1874 wl1271_debug(DEBUG_MAC80211,
1875 "run postponed irq_work directly");
ea0a3cf9
AN
1876
1877 /* don't talk to the HW if recovery is pending */
725b8277
AN
1878 if (!pending_recovery) {
1879 ret = wlcore_irq_locked(wl);
1880 if (ret)
1881 wl12xx_queue_recovery_work(wl);
1882 }
ea0a3cf9 1883
dd5512eb 1884 wlcore_enable_interrupts(wl);
f44e5868 1885 }
c56dbd57 1886
ea0a3cf9
AN
1887 if (pending_recovery) {
1888 wl1271_warning("queuing forgotten recovery on resume");
1889 ieee80211_queue_work(wl->hw, &wl->recovery_work);
6d5a748d 1890 goto out_sleep;
ea0a3cf9
AN
1891 }
1892
6d5a748d
RA
1893 ret = wl1271_ps_elp_wakeup(wl);
1894 if (ret < 0)
1895 goto out;
1896
6e8cd331
EP
1897 wl12xx_for_each_wlvif(wl, wlvif) {
1898 wl1271_configure_resume(wl, wlvif);
1899 }
ea0a3cf9 1900
6d5a748d
RA
1901 ret = wlcore_hw_interrupt_notify(wl, true);
1902 if (ret < 0)
1903 goto out_sleep;
1904
1905 /* if filtering is enabled, configure the FW to drop all RX BA frames */
1906 ret = wlcore_hw_rx_ba_filter(wl, false);
1907 if (ret < 0)
1908 goto out_sleep;
1909
1910out_sleep:
1911 wl1271_ps_elp_sleep(wl);
1912
ea0a3cf9 1913out:
ff91afc9 1914 wl->wow_enabled = false;
9be86cf0
AN
1915
1916 /*
1917 * Set a flag to re-init the watchdog on the first Tx after resume.
1918 * That way we avoid possible conditions where Tx-complete interrupts
1919 * fail to arrive and we perform a spurious recovery.
1920 */
1921 set_bit(WL1271_FLAG_REINIT_TX_WDOG, &wl->flags);
c56dbd57 1922 mutex_unlock(&wl->mutex);
f44e5868 1923
402e4861
EP
1924 return 0;
1925}
f634a4e7 1926#endif
402e4861 1927
f5fc0f86 1928static int wl1271_op_start(struct ieee80211_hw *hw)
1b72aecd
JO
1929{
1930 wl1271_debug(DEBUG_MAC80211, "mac80211 start");
1931
1932 /*
1933 * We have to delay the booting of the hardware because
1934 * we need to know the local MAC address before downloading and
1935 * initializing the firmware. The MAC address cannot be changed
1936 * after boot, and without the proper MAC address, the firmware
1937 * will not function properly.
1938 *
1939 * The MAC address is first known when the corresponding interface
1940 * is added. That is where we will initialize the hardware.
1941 */
1942
d18da7fc 1943 return 0;
1b72aecd
JO
1944}
1945
c24ec83b 1946static void wlcore_op_stop_locked(struct wl1271 *wl)
1b72aecd 1947{
baf6277a
EP
1948 int i;
1949
4cc53383 1950 if (wl->state == WLCORE_STATE_OFF) {
b666bb7f
IY
1951 if (test_and_clear_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS,
1952 &wl->flags))
1953 wlcore_enable_interrupts(wl);
1954
10c8cd01
EP
1955 return;
1956 }
46b0cc9f 1957
baf6277a
EP
1958 /*
1959 * this must be before the cancel_work calls below, so that the work
1960 * functions don't perform further work.
1961 */
4cc53383 1962 wl->state = WLCORE_STATE_OFF;
c24ec83b
IY
1963
1964 /*
1965 * Use the nosync variant to disable interrupts, so the mutex could be
1966 * held while doing so without deadlocking.
1967 */
1968 wlcore_disable_interrupts_nosync(wl);
1969
10c8cd01
EP
1970 mutex_unlock(&wl->mutex);
1971
c24ec83b 1972 wlcore_synchronize_interrupts(wl);
6dbc5fc2
EP
1973 if (!test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags))
1974 cancel_work_sync(&wl->recovery_work);
baf6277a
EP
1975 wl1271_flush_deferred_work(wl);
1976 cancel_delayed_work_sync(&wl->scan_complete_work);
1977 cancel_work_sync(&wl->netstack_work);
1978 cancel_work_sync(&wl->tx_work);
baf6277a 1979 cancel_delayed_work_sync(&wl->elp_work);
55df5afb 1980 cancel_delayed_work_sync(&wl->tx_watchdog_work);
baf6277a
EP
1981
1982 /* let's notify MAC80211 about the remaining pending TX frames */
baf6277a 1983 mutex_lock(&wl->mutex);
d935e385 1984 wl12xx_tx_reset(wl);
baf6277a
EP
1985
1986 wl1271_power_off(wl);
b666bb7f
IY
1987 /*
1988 * In case a recovery was scheduled, interrupts were disabled to avoid
1989 * an interrupt storm. Now that the power is down, it is safe to
1990 * re-enable interrupts to balance the disable depth
1991 */
1992 if (test_and_clear_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags))
1993 wlcore_enable_interrupts(wl);
baf6277a
EP
1994
1995 wl->band = IEEE80211_BAND_2GHZ;
1996
1997 wl->rx_counter = 0;
1998 wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
83d08d3f 1999 wl->channel_type = NL80211_CHAN_NO_HT;
baf6277a
EP
2000 wl->tx_blocks_available = 0;
2001 wl->tx_allocated_blocks = 0;
2002 wl->tx_results_count = 0;
2003 wl->tx_packets_count = 0;
2004 wl->time_offset = 0;
baf6277a
EP
2005 wl->ap_fw_ps_map = 0;
2006 wl->ap_ps_map = 0;
2f18cf7c 2007 wl->sleep_auth = WL1271_PSM_ILLEGAL;
baf6277a
EP
2008 memset(wl->roles_map, 0, sizeof(wl->roles_map));
2009 memset(wl->links_map, 0, sizeof(wl->links_map));
2010 memset(wl->roc_map, 0, sizeof(wl->roc_map));
978cd3a0 2011 memset(wl->session_ids, 0, sizeof(wl->session_ids));
02d0727c 2012 memset(wl->rx_filter_enabled, 0, sizeof(wl->rx_filter_enabled));
baf6277a 2013 wl->active_sta_count = 0;
9a100968 2014 wl->active_link_count = 0;
baf6277a
EP
2015
2016 /* The system link is always allocated */
9ebcb232
AN
2017 wl->links[WL12XX_SYSTEM_HLID].allocated_pkts = 0;
2018 wl->links[WL12XX_SYSTEM_HLID].prev_freed_pkts = 0;
baf6277a
EP
2019 __set_bit(WL12XX_SYSTEM_HLID, wl->links_map);
2020
2021 /*
2022 * this is performed after the cancel_work calls and the associated
2023 * mutex_lock, so that wl1271_op_add_interface does not accidentally
2024 * get executed before all these vars have been reset.
2025 */
2026 wl->flags = 0;
2027
2028 wl->tx_blocks_freed = 0;
2029
2030 for (i = 0; i < NUM_TX_QUEUES; i++) {
2031 wl->tx_pkts_freed[i] = 0;
2032 wl->tx_allocated_pkts[i] = 0;
2033 }
2034
2035 wl1271_debugfs_reset(wl);
2036
75fb4df7
EP
2037 kfree(wl->raw_fw_status);
2038 wl->raw_fw_status = NULL;
2039 kfree(wl->fw_status);
2040 wl->fw_status = NULL;
baf6277a
EP
2041 kfree(wl->tx_res_if);
2042 wl->tx_res_if = NULL;
2043 kfree(wl->target_mem_map);
2044 wl->target_mem_map = NULL;
6b70e7eb
VG
2045
2046 /*
2047 * FW channels must be re-calibrated after recovery,
8d3c1fd8 2048 * save current Reg-Domain channel configuration and clear it.
6b70e7eb 2049 */
8d3c1fd8
EP
2050 memcpy(wl->reg_ch_conf_pending, wl->reg_ch_conf_last,
2051 sizeof(wl->reg_ch_conf_pending));
6b70e7eb 2052 memset(wl->reg_ch_conf_last, 0, sizeof(wl->reg_ch_conf_last));
c24ec83b
IY
2053}
2054
2055static void wlcore_op_stop(struct ieee80211_hw *hw)
2056{
2057 struct wl1271 *wl = hw->priv;
2058
2059 wl1271_debug(DEBUG_MAC80211, "mac80211 stop");
2060
2061 mutex_lock(&wl->mutex);
2062
2063 wlcore_op_stop_locked(wl);
baf6277a
EP
2064
2065 mutex_unlock(&wl->mutex);
1b72aecd
JO
2066}
2067
c50a2825
EP
2068static void wlcore_channel_switch_work(struct work_struct *work)
2069{
2070 struct delayed_work *dwork;
2071 struct wl1271 *wl;
2072 struct ieee80211_vif *vif;
2073 struct wl12xx_vif *wlvif;
2074 int ret;
2075
2076 dwork = container_of(work, struct delayed_work, work);
2077 wlvif = container_of(dwork, struct wl12xx_vif, channel_switch_work);
2078 wl = wlvif->wl;
2079
2080 wl1271_info("channel switch failed (role_id: %d).", wlvif->role_id);
2081
2082 mutex_lock(&wl->mutex);
2083
2084 if (unlikely(wl->state != WLCORE_STATE_ON))
2085 goto out;
2086
2087 /* check the channel switch is still ongoing */
2088 if (!test_and_clear_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags))
2089 goto out;
2090
2091 vif = wl12xx_wlvif_to_vif(wlvif);
2092 ieee80211_chswitch_done(vif, false);
2093
2094 ret = wl1271_ps_elp_wakeup(wl);
2095 if (ret < 0)
2096 goto out;
2097
2098 wl12xx_cmd_stop_channel_switch(wl, wlvif);
2099
2100 wl1271_ps_elp_sleep(wl);
2101out:
2102 mutex_unlock(&wl->mutex);
2103}
2104
2105static void wlcore_connection_loss_work(struct work_struct *work)
2106{
2107 struct delayed_work *dwork;
2108 struct wl1271 *wl;
2109 struct ieee80211_vif *vif;
2110 struct wl12xx_vif *wlvif;
2111
2112 dwork = container_of(work, struct delayed_work, work);
2113 wlvif = container_of(dwork, struct wl12xx_vif, connection_loss_work);
2114 wl = wlvif->wl;
2115
2116 wl1271_info("Connection loss work (role_id: %d).", wlvif->role_id);
2117
2118 mutex_lock(&wl->mutex);
2119
2120 if (unlikely(wl->state != WLCORE_STATE_ON))
2121 goto out;
2122
2123 /* Call mac80211 connection loss */
2124 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
2125 goto out;
2126
2127 vif = wl12xx_wlvif_to_vif(wlvif);
2128 ieee80211_connection_loss(vif);
2129out:
2130 mutex_unlock(&wl->mutex);
2131}
2132
187e52cc
AN
2133static void wlcore_pending_auth_complete_work(struct work_struct *work)
2134{
2135 struct delayed_work *dwork;
2136 struct wl1271 *wl;
2137 struct wl12xx_vif *wlvif;
2138 unsigned long time_spare;
2139 int ret;
2140
2141 dwork = container_of(work, struct delayed_work, work);
2142 wlvif = container_of(dwork, struct wl12xx_vif,
2143 pending_auth_complete_work);
2144 wl = wlvif->wl;
2145
2146 mutex_lock(&wl->mutex);
2147
2148 if (unlikely(wl->state != WLCORE_STATE_ON))
2149 goto out;
2150
2151 /*
2152 * Make sure a second really passed since the last auth reply. Maybe
2153 * a second auth reply arrived while we were stuck on the mutex.
2154 * Check for a little less than the timeout to protect from scheduler
2155 * irregularities.
2156 */
2157 time_spare = jiffies +
2158 msecs_to_jiffies(WLCORE_PEND_AUTH_ROC_TIMEOUT - 50);
2159 if (!time_after(time_spare, wlvif->pending_auth_reply_time))
2160 goto out;
2161
2162 ret = wl1271_ps_elp_wakeup(wl);
2163 if (ret < 0)
2164 goto out;
2165
2166 /* cancel the ROC if active */
2167 wlcore_update_inconn_sta(wl, wlvif, NULL, false);
2168
2169 wl1271_ps_elp_sleep(wl);
2170out:
2171 mutex_unlock(&wl->mutex);
2172}
2173
e5a359f8
EP
2174static int wl12xx_allocate_rate_policy(struct wl1271 *wl, u8 *idx)
2175{
2176 u8 policy = find_first_zero_bit(wl->rate_policies_map,
2177 WL12XX_MAX_RATE_POLICIES);
2178 if (policy >= WL12XX_MAX_RATE_POLICIES)
2179 return -EBUSY;
2180
2181 __set_bit(policy, wl->rate_policies_map);
2182 *idx = policy;
2183 return 0;
2184}
2185
2186static void wl12xx_free_rate_policy(struct wl1271 *wl, u8 *idx)
2187{
2188 if (WARN_ON(*idx >= WL12XX_MAX_RATE_POLICIES))
2189 return;
2190
2191 __clear_bit(*idx, wl->rate_policies_map);
2192 *idx = WL12XX_MAX_RATE_POLICIES;
2193}
2194
001e39a8
EP
2195static int wlcore_allocate_klv_template(struct wl1271 *wl, u8 *idx)
2196{
2197 u8 policy = find_first_zero_bit(wl->klv_templates_map,
2198 WLCORE_MAX_KLV_TEMPLATES);
2199 if (policy >= WLCORE_MAX_KLV_TEMPLATES)
2200 return -EBUSY;
2201
2202 __set_bit(policy, wl->klv_templates_map);
2203 *idx = policy;
2204 return 0;
2205}
2206
2207static void wlcore_free_klv_template(struct wl1271 *wl, u8 *idx)
2208{
2209 if (WARN_ON(*idx >= WLCORE_MAX_KLV_TEMPLATES))
2210 return;
2211
2212 __clear_bit(*idx, wl->klv_templates_map);
2213 *idx = WLCORE_MAX_KLV_TEMPLATES;
2214}
2215
536129c8 2216static u8 wl12xx_get_role_type(struct wl1271 *wl, struct wl12xx_vif *wlvif)
b78b47eb 2217{
536129c8 2218 switch (wlvif->bss_type) {
b78b47eb 2219 case BSS_TYPE_AP_BSS:
fb0e707c 2220 if (wlvif->p2p)
045c745f
EP
2221 return WL1271_ROLE_P2P_GO;
2222 else
2223 return WL1271_ROLE_AP;
b78b47eb
EP
2224
2225 case BSS_TYPE_STA_BSS:
fb0e707c 2226 if (wlvif->p2p)
045c745f
EP
2227 return WL1271_ROLE_P2P_CL;
2228 else
2229 return WL1271_ROLE_STA;
b78b47eb 2230
227e81e1
EP
2231 case BSS_TYPE_IBSS:
2232 return WL1271_ROLE_IBSS;
2233
b78b47eb 2234 default:
536129c8 2235 wl1271_error("invalid bss_type: %d", wlvif->bss_type);
b78b47eb
EP
2236 }
2237 return WL12XX_INVALID_ROLE_TYPE;
2238}
2239
83587505 2240static int wl12xx_init_vif_data(struct wl1271 *wl, struct ieee80211_vif *vif)
87fbcb0f 2241{
e936bbe0 2242 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
e5a359f8 2243 int i;
e936bbe0 2244
48e93e40
EP
2245 /* clear everything but the persistent data */
2246 memset(wlvif, 0, offsetof(struct wl12xx_vif, persistent));
e936bbe0
EP
2247
2248 switch (ieee80211_vif_type_p2p(vif)) {
2249 case NL80211_IFTYPE_P2P_CLIENT:
2250 wlvif->p2p = 1;
2251 /* fall-through */
2252 case NL80211_IFTYPE_STATION:
2253 wlvif->bss_type = BSS_TYPE_STA_BSS;
2254 break;
2255 case NL80211_IFTYPE_ADHOC:
2256 wlvif->bss_type = BSS_TYPE_IBSS;
2257 break;
2258 case NL80211_IFTYPE_P2P_GO:
2259 wlvif->p2p = 1;
2260 /* fall-through */
2261 case NL80211_IFTYPE_AP:
2262 wlvif->bss_type = BSS_TYPE_AP_BSS;
2263 break;
2264 default:
2265 wlvif->bss_type = MAX_BSS_TYPE;
2266 return -EOPNOTSUPP;
2267 }
2268
0603d891 2269 wlvif->role_id = WL12XX_INVALID_ROLE_ID;
7edebf56 2270 wlvif->dev_role_id = WL12XX_INVALID_ROLE_ID;
afaf8bdb 2271 wlvif->dev_hlid = WL12XX_INVALID_LINK_ID;
a8ab39a4 2272
e936bbe0
EP
2273 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2274 wlvif->bss_type == BSS_TYPE_IBSS) {
2275 /* init sta/ibss data */
2276 wlvif->sta.hlid = WL12XX_INVALID_LINK_ID;
e5a359f8
EP
2277 wl12xx_allocate_rate_policy(wl, &wlvif->sta.basic_rate_idx);
2278 wl12xx_allocate_rate_policy(wl, &wlvif->sta.ap_rate_idx);
2279 wl12xx_allocate_rate_policy(wl, &wlvif->sta.p2p_rate_idx);
001e39a8 2280 wlcore_allocate_klv_template(wl, &wlvif->sta.klv_template_id);
15e05bc0
LC
2281 wlvif->basic_rate_set = CONF_TX_RATE_MASK_BASIC;
2282 wlvif->basic_rate = CONF_TX_RATE_MASK_BASIC;
2283 wlvif->rate_set = CONF_TX_RATE_MASK_BASIC;
e936bbe0
EP
2284 } else {
2285 /* init ap data */
2286 wlvif->ap.bcast_hlid = WL12XX_INVALID_LINK_ID;
2287 wlvif->ap.global_hlid = WL12XX_INVALID_LINK_ID;
e5a359f8
EP
2288 wl12xx_allocate_rate_policy(wl, &wlvif->ap.mgmt_rate_idx);
2289 wl12xx_allocate_rate_policy(wl, &wlvif->ap.bcast_rate_idx);
2290 for (i = 0; i < CONF_TX_MAX_AC_COUNT; i++)
2291 wl12xx_allocate_rate_policy(wl,
2292 &wlvif->ap.ucast_rate_idx[i]);
42ec1f82 2293 wlvif->basic_rate_set = CONF_TX_ENABLED_RATES;
15e05bc0
LC
2294 /*
2295 * TODO: check if basic_rate shouldn't be
2296 * wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
2297 * instead (the same thing for STA above).
2298 */
42ec1f82 2299 wlvif->basic_rate = CONF_TX_ENABLED_RATES;
15e05bc0 2300 /* TODO: this seems to be used only for STA, check it */
42ec1f82 2301 wlvif->rate_set = CONF_TX_ENABLED_RATES;
e936bbe0 2302 }
a8ab39a4 2303
83587505
EP
2304 wlvif->bitrate_masks[IEEE80211_BAND_2GHZ] = wl->conf.tx.basic_rate;
2305 wlvif->bitrate_masks[IEEE80211_BAND_5GHZ] = wl->conf.tx.basic_rate_5;
6a899796
EP
2306 wlvif->beacon_int = WL1271_DEFAULT_BEACON_INT;
2307
1b92f15e
EP
2308 /*
2309 * mac80211 configures some values globally, while we treat them
2310 * per-interface. thus, on init, we have to copy them from wl
2311 */
2312 wlvif->band = wl->band;
61f845f4 2313 wlvif->channel = wl->channel;
6bd65029 2314 wlvif->power_level = wl->power_level;
83d08d3f 2315 wlvif->channel_type = wl->channel_type;
1b92f15e 2316
9eb599e9
EP
2317 INIT_WORK(&wlvif->rx_streaming_enable_work,
2318 wl1271_rx_streaming_enable_work);
2319 INIT_WORK(&wlvif->rx_streaming_disable_work,
2320 wl1271_rx_streaming_disable_work);
7d3b29e5 2321 INIT_WORK(&wlvif->rc_update_work, wlcore_rc_update_work);
c50a2825
EP
2322 INIT_DELAYED_WORK(&wlvif->channel_switch_work,
2323 wlcore_channel_switch_work);
2324 INIT_DELAYED_WORK(&wlvif->connection_loss_work,
2325 wlcore_connection_loss_work);
187e52cc
AN
2326 INIT_DELAYED_WORK(&wlvif->pending_auth_complete_work,
2327 wlcore_pending_auth_complete_work);
87627214 2328 INIT_LIST_HEAD(&wlvif->list);
252efa4f 2329
9eb599e9
EP
2330 setup_timer(&wlvif->rx_streaming_timer, wl1271_rx_streaming_timer,
2331 (unsigned long) wlvif);
e936bbe0 2332 return 0;
87fbcb0f
EP
2333}
2334
5dc283fe 2335static int wl12xx_init_fw(struct wl1271 *wl)
f5fc0f86 2336{
9ccd9217 2337 int retries = WL1271_BOOT_RETRIES;
71125abd 2338 bool booted = false;
1d095475
EP
2339 struct wiphy *wiphy = wl->hw->wiphy;
2340 int ret;
f5fc0f86 2341
9ccd9217
JO
2342 while (retries) {
2343 retries--;
3fcdab70 2344 ret = wl12xx_chip_wakeup(wl, false);
9ccd9217
JO
2345 if (ret < 0)
2346 goto power_off;
f5fc0f86 2347
dd5512eb 2348 ret = wl->ops->boot(wl);
9ccd9217
JO
2349 if (ret < 0)
2350 goto power_off;
f5fc0f86 2351
92c77c73
EP
2352 ret = wl1271_hw_init(wl);
2353 if (ret < 0)
2354 goto irq_disable;
2355
71125abd
EP
2356 booted = true;
2357 break;
eb5b28d0 2358
9ccd9217 2359irq_disable:
9ccd9217
JO
2360 mutex_unlock(&wl->mutex);
2361 /* Unlocking the mutex in the middle of handling is
2362 inherently unsafe. In this case we deem it safe to do,
2363 because we need to let any possibly pending IRQ out of
4cc53383 2364 the system (and while we are WLCORE_STATE_OFF the IRQ
9ccd9217
JO
2365 work function will not do anything.) Also, any other
2366 possible concurrent operations will fail due to the
2367 current state, hence the wl1271 struct should be safe. */
dd5512eb 2368 wlcore_disable_interrupts(wl);
a620865e
IY
2369 wl1271_flush_deferred_work(wl);
2370 cancel_work_sync(&wl->netstack_work);
9ccd9217
JO
2371 mutex_lock(&wl->mutex);
2372power_off:
2373 wl1271_power_off(wl);
2374 }
eb5b28d0 2375
71125abd
EP
2376 if (!booted) {
2377 wl1271_error("firmware boot failed despite %d retries",
2378 WL1271_BOOT_RETRIES);
2379 goto out;
2380 }
2381
4b7fac77 2382 wl1271_info("firmware booted (%s)", wl->chip.fw_ver_str);
71125abd
EP
2383
2384 /* update hw/fw version info in wiphy struct */
2385 wiphy->hw_version = wl->chip.id;
4b7fac77 2386 strncpy(wiphy->fw_version, wl->chip.fw_ver_str,
71125abd
EP
2387 sizeof(wiphy->fw_version));
2388
fb6a6819
LC
2389 /*
2390 * Now we know if 11a is supported (info from the NVS), so disable
2391 * 11a channels if not supported
2392 */
2393 if (!wl->enable_11a)
2394 wiphy->bands[IEEE80211_BAND_5GHZ]->n_channels = 0;
2395
2396 wl1271_debug(DEBUG_MAC80211, "11a is %ssupported",
2397 wl->enable_11a ? "" : "not ");
2398
4cc53383 2399 wl->state = WLCORE_STATE_ON;
1d095475 2400out:
5dc283fe 2401 return ret;
1d095475
EP
2402}
2403
92e712da
EP
2404static bool wl12xx_dev_role_started(struct wl12xx_vif *wlvif)
2405{
2406 return wlvif->dev_hlid != WL12XX_INVALID_LINK_ID;
2407}
2408
4549d09c
EP
2409/*
2410 * Check whether a fw switch (i.e. moving from one loaded
2411 * fw to another) is needed. This function is also responsible
2412 * for updating wl->last_vif_count, so it must be called before
2413 * loading a non-plt fw (so the correct fw (single-role/multi-role)
2414 * will be used).
2415 */
2416static bool wl12xx_need_fw_change(struct wl1271 *wl,
2417 struct vif_counter_data vif_counter_data,
2418 bool add)
2419{
2420 enum wl12xx_fw_type current_fw = wl->fw_type;
2421 u8 vif_count = vif_counter_data.counter;
2422
2423 if (test_bit(WL1271_FLAG_VIF_CHANGE_IN_PROGRESS, &wl->flags))
2424 return false;
2425
2426 /* increase the vif count if this is a new vif */
2427 if (add && !vif_counter_data.cur_vif_running)
2428 vif_count++;
2429
2430 wl->last_vif_count = vif_count;
2431
2432 /* no need for fw change if the device is OFF */
4cc53383 2433 if (wl->state == WLCORE_STATE_OFF)
4549d09c
EP
2434 return false;
2435
9b1a0a77
EP
2436 /* no need for fw change if a single fw is used */
2437 if (!wl->mr_fw_name)
2438 return false;
2439
4549d09c
EP
2440 if (vif_count > 1 && current_fw == WL12XX_FW_TYPE_NORMAL)
2441 return true;
2442 if (vif_count <= 1 && current_fw == WL12XX_FW_TYPE_MULTI)
2443 return true;
2444
2445 return false;
2446}
2447
3dee4393
EP
2448/*
2449 * Enter "forced psm". Make sure the sta is in psm against the ap,
2450 * to make the fw switch a bit more disconnection-persistent.
2451 */
2452static void wl12xx_force_active_psm(struct wl1271 *wl)
2453{
2454 struct wl12xx_vif *wlvif;
2455
2456 wl12xx_for_each_wlvif_sta(wl, wlvif) {
2457 wl1271_ps_set_mode(wl, wlvif, STATION_POWER_SAVE_MODE);
2458 }
2459}
2460
1c33db78
AN
2461struct wlcore_hw_queue_iter_data {
2462 unsigned long hw_queue_map[BITS_TO_LONGS(WLCORE_NUM_MAC_ADDRESSES)];
2463 /* current vif */
2464 struct ieee80211_vif *vif;
2465 /* is the current vif among those iterated */
2466 bool cur_running;
2467};
2468
2469static void wlcore_hw_queue_iter(void *data, u8 *mac,
2470 struct ieee80211_vif *vif)
2471{
2472 struct wlcore_hw_queue_iter_data *iter_data = data;
2473
2474 if (WARN_ON_ONCE(vif->hw_queue[0] == IEEE80211_INVAL_HW_QUEUE))
2475 return;
2476
2477 if (iter_data->cur_running || vif == iter_data->vif) {
2478 iter_data->cur_running = true;
2479 return;
2480 }
2481
2482 __set_bit(vif->hw_queue[0] / NUM_TX_QUEUES, iter_data->hw_queue_map);
2483}
2484
2485static int wlcore_allocate_hw_queue_base(struct wl1271 *wl,
2486 struct wl12xx_vif *wlvif)
2487{
2488 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
2489 struct wlcore_hw_queue_iter_data iter_data = {};
2490 int i, q_base;
2491
2492 iter_data.vif = vif;
2493
2494 /* mark all bits taken by active interfaces */
2495 ieee80211_iterate_active_interfaces_atomic(wl->hw,
2496 IEEE80211_IFACE_ITER_RESUME_ALL,
2497 wlcore_hw_queue_iter, &iter_data);
2498
2499 /* the current vif is already running in mac80211 (resume/recovery) */
2500 if (iter_data.cur_running) {
2501 wlvif->hw_queue_base = vif->hw_queue[0];
2502 wl1271_debug(DEBUG_MAC80211,
2503 "using pre-allocated hw queue base %d",
2504 wlvif->hw_queue_base);
2505
2506 /* interface type might have changed type */
2507 goto adjust_cab_queue;
2508 }
2509
2510 q_base = find_first_zero_bit(iter_data.hw_queue_map,
2511 WLCORE_NUM_MAC_ADDRESSES);
2512 if (q_base >= WLCORE_NUM_MAC_ADDRESSES)
2513 return -EBUSY;
2514
2515 wlvif->hw_queue_base = q_base * NUM_TX_QUEUES;
2516 wl1271_debug(DEBUG_MAC80211, "allocating hw queue base: %d",
2517 wlvif->hw_queue_base);
2518
2519 for (i = 0; i < NUM_TX_QUEUES; i++) {
2520 wl->queue_stop_reasons[wlvif->hw_queue_base + i] = 0;
2521 /* register hw queues in mac80211 */
2522 vif->hw_queue[i] = wlvif->hw_queue_base + i;
2523 }
2524
2525adjust_cab_queue:
2526 /* the last places are reserved for cab queues per interface */
2527 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
2528 vif->cab_queue = NUM_TX_QUEUES * WLCORE_NUM_MAC_ADDRESSES +
2529 wlvif->hw_queue_base / NUM_TX_QUEUES;
2530 else
2531 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
2532
2533 return 0;
2534}
2535
1d095475
EP
2536static int wl1271_op_add_interface(struct ieee80211_hw *hw,
2537 struct ieee80211_vif *vif)
2538{
2539 struct wl1271 *wl = hw->priv;
2540 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4549d09c 2541 struct vif_counter_data vif_count;
1d095475
EP
2542 int ret = 0;
2543 u8 role_type;
1d095475 2544
dd491ffb
YS
2545 if (wl->plt) {
2546 wl1271_error("Adding Interface not allowed while in PLT mode");
2547 return -EBUSY;
2548 }
2549
ea086359
JB
2550 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
2551 IEEE80211_VIF_SUPPORTS_CQM_RSSI;
c1288b12 2552
1d095475
EP
2553 wl1271_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
2554 ieee80211_vif_type_p2p(vif), vif->addr);
2555
4549d09c
EP
2556 wl12xx_get_vif_count(hw, vif, &vif_count);
2557
1d095475 2558 mutex_lock(&wl->mutex);
f750c820
EP
2559 ret = wl1271_ps_elp_wakeup(wl);
2560 if (ret < 0)
2561 goto out_unlock;
2562
1d095475
EP
2563 /*
2564 * in some very corner case HW recovery scenarios its possible to
2565 * get here before __wl1271_op_remove_interface is complete, so
2566 * opt out if that is the case.
2567 */
10c8cd01
EP
2568 if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags) ||
2569 test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)) {
1d095475
EP
2570 ret = -EBUSY;
2571 goto out;
2572 }
2573
3fcdab70 2574
83587505 2575 ret = wl12xx_init_vif_data(wl, vif);
1d095475
EP
2576 if (ret < 0)
2577 goto out;
2578
2579 wlvif->wl = wl;
2580 role_type = wl12xx_get_role_type(wl, wlvif);
2581 if (role_type == WL12XX_INVALID_ROLE_TYPE) {
2582 ret = -EINVAL;
2583 goto out;
2584 }
2585
1c33db78
AN
2586 ret = wlcore_allocate_hw_queue_base(wl, wlvif);
2587 if (ret < 0)
2588 goto out;
2589
4549d09c 2590 if (wl12xx_need_fw_change(wl, vif_count, true)) {
3dee4393 2591 wl12xx_force_active_psm(wl);
e9ba7152 2592 set_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags);
4549d09c
EP
2593 mutex_unlock(&wl->mutex);
2594 wl1271_recovery_work(&wl->recovery_work);
2595 return 0;
2596 }
2597
1d095475
EP
2598 /*
2599 * TODO: after the nvs issue will be solved, move this block
2600 * to start(), and make sure here the driver is ON.
2601 */
4cc53383 2602 if (wl->state == WLCORE_STATE_OFF) {
1d095475
EP
2603 /*
2604 * we still need this in order to configure the fw
2605 * while uploading the nvs
2606 */
5e037e74 2607 memcpy(wl->addresses[0].addr, vif->addr, ETH_ALEN);
1d095475 2608
5dc283fe
LC
2609 ret = wl12xx_init_fw(wl);
2610 if (ret < 0)
1d095475 2611 goto out;
1d095475
EP
2612 }
2613
1d095475
EP
2614 ret = wl12xx_cmd_role_enable(wl, vif->addr,
2615 role_type, &wlvif->role_id);
2616 if (ret < 0)
2617 goto out;
2618
2619 ret = wl1271_init_vif_specific(wl, vif);
2620 if (ret < 0)
2621 goto out;
2622
87627214 2623 list_add(&wlvif->list, &wl->wlvif_list);
10c8cd01 2624 set_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags);
a4e4130d
EP
2625
2626 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
2627 wl->ap_count++;
2628 else
2629 wl->sta_count++;
eb5b28d0 2630out:
f750c820
EP
2631 wl1271_ps_elp_sleep(wl);
2632out_unlock:
f5fc0f86
LC
2633 mutex_unlock(&wl->mutex);
2634
2635 return ret;
2636}
2637
7dece1c8 2638static void __wl1271_op_remove_interface(struct wl1271 *wl,
536129c8 2639 struct ieee80211_vif *vif,
7dece1c8 2640 bool reset_tx_queues)
f5fc0f86 2641{
536129c8 2642 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
e5a359f8 2643 int i, ret;
2f18cf7c 2644 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
f5fc0f86 2645
1b72aecd 2646 wl1271_debug(DEBUG_MAC80211, "mac80211 remove interface");
f5fc0f86 2647
10c8cd01
EP
2648 if (!test_and_clear_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
2649 return;
2650
13026dec 2651 /* because of hardware recovery, we may get here twice */
4cc53383 2652 if (wl->state == WLCORE_STATE_OFF)
13026dec
JO
2653 return;
2654
1b72aecd 2655 wl1271_info("down");
f5fc0f86 2656
baf6277a 2657 if (wl->scan.state != WL1271_SCAN_STATE_IDLE &&
c50a2825 2658 wl->scan_wlvif == wlvif) {
55df5afb
AN
2659 /*
2660 * Rearm the tx watchdog just before idling scan. This
2661 * prevents just-finished scans from triggering the watchdog
2662 */
2663 wl12xx_rearm_tx_watchdog_locked(wl);
2664
08688d6b 2665 wl->scan.state = WL1271_SCAN_STATE_IDLE;
4a31c11c 2666 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
c50a2825 2667 wl->scan_wlvif = NULL;
b739a42c 2668 wl->scan.req = NULL;
76a029fb 2669 ieee80211_scan_completed(wl->hw, true);
f5fc0f86
LC
2670 }
2671
5a441f5f 2672 if (wl->sched_vif == wlvif)
10199756 2673 wl->sched_vif = NULL;
10199756 2674
5d979f35
AN
2675 if (wl->roc_vif == vif) {
2676 wl->roc_vif = NULL;
2677 ieee80211_remain_on_channel_expired(wl->hw);
2678 }
2679
b78b47eb
EP
2680 if (!test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags)) {
2681 /* disable active roles */
2682 ret = wl1271_ps_elp_wakeup(wl);
2683 if (ret < 0)
2684 goto deinit;
2685
b890f4c3
EP
2686 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2687 wlvif->bss_type == BSS_TYPE_IBSS) {
2688 if (wl12xx_dev_role_started(wlvif))
2689 wl12xx_stop_dev(wl, wlvif);
04e8079c
EP
2690 }
2691
0603d891 2692 ret = wl12xx_cmd_role_disable(wl, &wlvif->role_id);
b78b47eb
EP
2693 if (ret < 0)
2694 goto deinit;
2695
2696 wl1271_ps_elp_sleep(wl);
2697 }
2698deinit:
5a99610c
AN
2699 wl12xx_tx_reset_wlvif(wl, wlvif);
2700
e51ae9be 2701 /* clear all hlids (except system_hlid) */
afaf8bdb 2702 wlvif->dev_hlid = WL12XX_INVALID_LINK_ID;
e5a359f8
EP
2703
2704 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2705 wlvif->bss_type == BSS_TYPE_IBSS) {
2706 wlvif->sta.hlid = WL12XX_INVALID_LINK_ID;
2707 wl12xx_free_rate_policy(wl, &wlvif->sta.basic_rate_idx);
2708 wl12xx_free_rate_policy(wl, &wlvif->sta.ap_rate_idx);
2709 wl12xx_free_rate_policy(wl, &wlvif->sta.p2p_rate_idx);
001e39a8 2710 wlcore_free_klv_template(wl, &wlvif->sta.klv_template_id);
e5a359f8
EP
2711 } else {
2712 wlvif->ap.bcast_hlid = WL12XX_INVALID_LINK_ID;
2713 wlvif->ap.global_hlid = WL12XX_INVALID_LINK_ID;
2714 wl12xx_free_rate_policy(wl, &wlvif->ap.mgmt_rate_idx);
2715 wl12xx_free_rate_policy(wl, &wlvif->ap.bcast_rate_idx);
2716 for (i = 0; i < CONF_TX_MAX_AC_COUNT; i++)
2717 wl12xx_free_rate_policy(wl,
2718 &wlvif->ap.ucast_rate_idx[i]);
830be7e0 2719 wl1271_free_ap_keys(wl, wlvif);
e5a359f8 2720 }
b78b47eb 2721
3eba4a0e
ES
2722 dev_kfree_skb(wlvif->probereq);
2723 wlvif->probereq = NULL;
e4120df9
EP
2724 if (wl->last_wlvif == wlvif)
2725 wl->last_wlvif = NULL;
87627214 2726 list_del(&wlvif->list);
c7ffb902 2727 memset(wlvif->ap.sta_hlid_map, 0, sizeof(wlvif->ap.sta_hlid_map));
0603d891 2728 wlvif->role_id = WL12XX_INVALID_ROLE_ID;
7edebf56 2729 wlvif->dev_role_id = WL12XX_INVALID_ROLE_ID;
d6e19d13 2730
2f18cf7c 2731 if (is_ap)
a4e4130d
EP
2732 wl->ap_count--;
2733 else
2734 wl->sta_count--;
2735
42066f9a
AN
2736 /*
2737 * Last AP, have more stations. Configure sleep auth according to STA.
2738 * Don't do thin on unintended recovery.
2739 */
2740 if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags) &&
2741 !test_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags))
2742 goto unlock;
2743
71e996be
EP
2744 if (wl->ap_count == 0 && is_ap) {
2745 /* mask ap events */
2746 wl->event_mask &= ~wl->ap_event_mask;
2747 wl1271_event_unmask(wl);
2748 }
2749
2f18cf7c
AN
2750 if (wl->ap_count == 0 && is_ap && wl->sta_count) {
2751 u8 sta_auth = wl->conf.conn.sta_sleep_auth;
2752 /* Configure for power according to debugfs */
2753 if (sta_auth != WL1271_PSM_ILLEGAL)
2754 wl1271_acx_sleep_auth(wl, sta_auth);
2f18cf7c
AN
2755 /* Configure for ELP power saving */
2756 else
2757 wl1271_acx_sleep_auth(wl, WL1271_PSM_ELP);
2758 }
2759
42066f9a 2760unlock:
baf6277a 2761 mutex_unlock(&wl->mutex);
d6bf9ada 2762
9eb599e9
EP
2763 del_timer_sync(&wlvif->rx_streaming_timer);
2764 cancel_work_sync(&wlvif->rx_streaming_enable_work);
2765 cancel_work_sync(&wlvif->rx_streaming_disable_work);
7d3b29e5 2766 cancel_work_sync(&wlvif->rc_update_work);
c50a2825 2767 cancel_delayed_work_sync(&wlvif->connection_loss_work);
c838478b 2768 cancel_delayed_work_sync(&wlvif->channel_switch_work);
187e52cc 2769 cancel_delayed_work_sync(&wlvif->pending_auth_complete_work);
bd9dc49c 2770
baf6277a 2771 mutex_lock(&wl->mutex);
52a2a375 2772}
bd9dc49c 2773
52a2a375
JO
2774static void wl1271_op_remove_interface(struct ieee80211_hw *hw,
2775 struct ieee80211_vif *vif)
2776{
2777 struct wl1271 *wl = hw->priv;
10c8cd01 2778 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
6e8cd331 2779 struct wl12xx_vif *iter;
4549d09c 2780 struct vif_counter_data vif_count;
52a2a375 2781
4549d09c 2782 wl12xx_get_vif_count(hw, vif, &vif_count);
52a2a375 2783 mutex_lock(&wl->mutex);
10c8cd01 2784
4cc53383 2785 if (wl->state == WLCORE_STATE_OFF ||
10c8cd01
EP
2786 !test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
2787 goto out;
2788
67353299
JO
2789 /*
2790 * wl->vif can be null here if someone shuts down the interface
2791 * just when hardware recovery has been started.
2792 */
6e8cd331
EP
2793 wl12xx_for_each_wlvif(wl, iter) {
2794 if (iter != wlvif)
2795 continue;
2796
536129c8 2797 __wl1271_op_remove_interface(wl, vif, true);
6e8cd331 2798 break;
67353299 2799 }
6e8cd331 2800 WARN_ON(iter != wlvif);
4549d09c 2801 if (wl12xx_need_fw_change(wl, vif_count, false)) {
3dee4393 2802 wl12xx_force_active_psm(wl);
e9ba7152 2803 set_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags);
4549d09c 2804 wl12xx_queue_recovery_work(wl);
4549d09c 2805 }
10c8cd01 2806out:
67353299 2807 mutex_unlock(&wl->mutex);
f5fc0f86
LC
2808}
2809
c0fad1b7
EP
2810static int wl12xx_op_change_interface(struct ieee80211_hw *hw,
2811 struct ieee80211_vif *vif,
2812 enum nl80211_iftype new_type, bool p2p)
2813{
4549d09c
EP
2814 struct wl1271 *wl = hw->priv;
2815 int ret;
2816
2817 set_bit(WL1271_FLAG_VIF_CHANGE_IN_PROGRESS, &wl->flags);
c0fad1b7
EP
2818 wl1271_op_remove_interface(hw, vif);
2819
249e9698 2820 vif->type = new_type;
c0fad1b7 2821 vif->p2p = p2p;
4549d09c
EP
2822 ret = wl1271_op_add_interface(hw, vif);
2823
2824 clear_bit(WL1271_FLAG_VIF_CHANGE_IN_PROGRESS, &wl->flags);
2825 return ret;
c0fad1b7
EP
2826}
2827
3230f35e 2828static int wlcore_join(struct wl1271 *wl, struct wl12xx_vif *wlvif)
82429d32
JO
2829{
2830 int ret;
536129c8 2831 bool is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
82429d32 2832
69e5434c
JO
2833 /*
2834 * One of the side effects of the JOIN command is that is clears
2835 * WPA/WPA2 keys from the chipset. Performing a JOIN while associated
2836 * to a WPA/WPA2 access point will therefore kill the data-path.
8bf69aae
OBC
2837 * Currently the only valid scenario for JOIN during association
2838 * is on roaming, in which case we will also be given new keys.
2839 * Keep the below message for now, unless it starts bothering
2840 * users who really like to roam a lot :)
69e5434c 2841 */
ba8447f6 2842 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
69e5434c
JO
2843 wl1271_info("JOIN while associated.");
2844
5ec8a448
EP
2845 /* clear encryption type */
2846 wlvif->encryption_type = KEY_NONE;
2847
227e81e1 2848 if (is_ibss)
87fbcb0f 2849 ret = wl12xx_cmd_role_start_ibss(wl, wlvif);
18eab430
EP
2850 else {
2851 if (wl->quirks & WLCORE_QUIRK_START_STA_FAILS) {
2852 /*
2853 * TODO: this is an ugly workaround for wl12xx fw
2854 * bug - we are not able to tx/rx after the first
2855 * start_sta, so make dummy start+stop calls,
2856 * and then call start_sta again.
2857 * this should be fixed in the fw.
2858 */
2859 wl12xx_cmd_role_start_sta(wl, wlvif);
2860 wl12xx_cmd_role_stop_sta(wl, wlvif);
2861 }
2862
87fbcb0f 2863 ret = wl12xx_cmd_role_start_sta(wl, wlvif);
18eab430 2864 }
3230f35e
EP
2865
2866 return ret;
2867}
2868
2869static int wl1271_ssid_set(struct wl12xx_vif *wlvif, struct sk_buff *skb,
2870 int offset)
2871{
2872 u8 ssid_len;
2873 const u8 *ptr = cfg80211_find_ie(WLAN_EID_SSID, skb->data + offset,
2874 skb->len - offset);
2875
2876 if (!ptr) {
2877 wl1271_error("No SSID in IEs!");
2878 return -ENOENT;
2879 }
2880
2881 ssid_len = ptr[1];
2882 if (ssid_len > IEEE80211_MAX_SSID_LEN) {
2883 wl1271_error("SSID is too long!");
2884 return -EINVAL;
2885 }
2886
2887 wlvif->ssid_len = ssid_len;
2888 memcpy(wlvif->ssid, ptr+2, ssid_len);
2889 return 0;
2890}
2891
2892static int wlcore_set_ssid(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2893{
2894 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
2895 struct sk_buff *skb;
2896 int ieoffset;
2897
2898 /* we currently only support setting the ssid from the ap probe req */
2899 if (wlvif->bss_type != BSS_TYPE_STA_BSS)
2900 return -EINVAL;
2901
2902 skb = ieee80211_ap_probereq_get(wl->hw, vif);
2903 if (!skb)
2904 return -EINVAL;
2905
2906 ieoffset = offsetof(struct ieee80211_mgmt,
2907 u.probe_req.variable);
2908 wl1271_ssid_set(wlvif, skb, ieoffset);
2909 dev_kfree_skb(skb);
2910
2911 return 0;
2912}
2913
2914static int wlcore_set_assoc(struct wl1271 *wl, struct wl12xx_vif *wlvif,
ec87011a
EP
2915 struct ieee80211_bss_conf *bss_conf,
2916 u32 sta_rate_set)
3230f35e
EP
2917{
2918 int ieoffset;
2919 int ret;
2920
2921 wlvif->aid = bss_conf->aid;
aaabee8b 2922 wlvif->channel_type = cfg80211_get_chandef_type(&bss_conf->chandef);
3230f35e 2923 wlvif->beacon_int = bss_conf->beacon_int;
d50529c0 2924 wlvif->wmm_enabled = bss_conf->qos;
3230f35e
EP
2925
2926 set_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags);
2927
2928 /*
2929 * with wl1271, we don't need to update the
2930 * beacon_int and dtim_period, because the firmware
2931 * updates it by itself when the first beacon is
2932 * received after a join.
2933 */
2934 ret = wl1271_cmd_build_ps_poll(wl, wlvif, wlvif->aid);
82429d32 2935 if (ret < 0)
3230f35e 2936 return ret;
82429d32 2937
3230f35e
EP
2938 /*
2939 * Get a template for hardware connection maintenance
2940 */
2941 dev_kfree_skb(wlvif->probereq);
2942 wlvif->probereq = wl1271_cmd_build_ap_probe_req(wl,
2943 wlvif,
2944 NULL);
2945 ieoffset = offsetof(struct ieee80211_mgmt,
2946 u.probe_req.variable);
2947 wl1271_ssid_set(wlvif, wlvif->probereq, ieoffset);
2948
2949 /* enable the connection monitoring feature */
2950 ret = wl1271_acx_conn_monit_params(wl, wlvif, true);
2951 if (ret < 0)
2952 return ret;
82429d32
JO
2953
2954 /*
2955 * The join command disable the keep-alive mode, shut down its process,
2956 * and also clear the template config, so we need to reset it all after
2957 * the join. The acx_aid starts the keep-alive process, and the order
2958 * of the commands below is relevant.
2959 */
0603d891 2960 ret = wl1271_acx_keep_alive_mode(wl, wlvif, true);
82429d32 2961 if (ret < 0)
3230f35e 2962 return ret;
82429d32 2963
0603d891 2964 ret = wl1271_acx_aid(wl, wlvif, wlvif->aid);
82429d32 2965 if (ret < 0)
3230f35e 2966 return ret;
82429d32 2967
d2d66c56 2968 ret = wl12xx_cmd_build_klv_null_data(wl, wlvif);
82429d32 2969 if (ret < 0)
3230f35e 2970 return ret;
82429d32 2971
0603d891 2972 ret = wl1271_acx_keep_alive_config(wl, wlvif,
001e39a8 2973 wlvif->sta.klv_template_id,
82429d32
JO
2974 ACX_KEEP_ALIVE_TPL_VALID);
2975 if (ret < 0)
3230f35e 2976 return ret;
82429d32 2977
6c7b5194
EP
2978 /*
2979 * The default fw psm configuration is AUTO, while mac80211 default
2980 * setting is off (ACTIVE), so sync the fw with the correct value.
2981 */
2982 ret = wl1271_ps_set_mode(wl, wlvif, STATION_ACTIVE_MODE);
ec87011a
EP
2983 if (ret < 0)
2984 return ret;
2985
2986 if (sta_rate_set) {
2987 wlvif->rate_set =
2988 wl1271_tx_enabled_rates_get(wl,
2989 sta_rate_set,
2990 wlvif->band);
2991 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
2992 if (ret < 0)
2993 return ret;
2994 }
82429d32 2995
82429d32
JO
2996 return ret;
2997}
2998
3230f35e 2999static int wlcore_unset_assoc(struct wl1271 *wl, struct wl12xx_vif *wlvif)
c7f43e45
LC
3000{
3001 int ret;
3230f35e
EP
3002 bool sta = wlvif->bss_type == BSS_TYPE_STA_BSS;
3003
3004 /* make sure we are connected (sta) joined */
3005 if (sta &&
3006 !test_and_clear_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
3007 return false;
3008
3009 /* make sure we are joined (ibss) */
3010 if (!sta &&
3011 test_and_clear_bit(WLVIF_FLAG_IBSS_JOINED, &wlvif->flags))
3012 return false;
3013
3014 if (sta) {
3015 /* use defaults when not associated */
3016 wlvif->aid = 0;
3017
3018 /* free probe-request template */
3019 dev_kfree_skb(wlvif->probereq);
3020 wlvif->probereq = NULL;
3021
3022 /* disable connection monitor features */
3023 ret = wl1271_acx_conn_monit_params(wl, wlvif, false);
3024 if (ret < 0)
3025 return ret;
3026
3027 /* Disable the keep-alive feature */
3028 ret = wl1271_acx_keep_alive_mode(wl, wlvif, false);
3029 if (ret < 0)
3030 return ret;
d881fa2c
EP
3031
3032 /* disable beacon filtering */
3033 ret = wl1271_acx_beacon_filter_opt(wl, wlvif, false);
3034 if (ret < 0)
3035 return ret;
3230f35e 3036 }
c7f43e45 3037
52630c5d 3038 if (test_and_clear_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags)) {
6e8cd331
EP
3039 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
3040
fcab1890 3041 wl12xx_cmd_stop_channel_switch(wl, wlvif);
6e8cd331 3042 ieee80211_chswitch_done(vif, false);
c50a2825 3043 cancel_delayed_work(&wlvif->channel_switch_work);
6d158ff3
SL
3044 }
3045
4137c17c
EP
3046 /* invalidate keep-alive template */
3047 wl1271_acx_keep_alive_config(wl, wlvif,
001e39a8 3048 wlvif->sta.klv_template_id,
4137c17c
EP
3049 ACX_KEEP_ALIVE_TPL_INVALID);
3050
3230f35e 3051 return 0;
c7f43e45
LC
3052}
3053
87fbcb0f 3054static void wl1271_set_band_rate(struct wl1271 *wl, struct wl12xx_vif *wlvif)
ebba60c6 3055{
1b92f15e 3056 wlvif->basic_rate_set = wlvif->bitrate_masks[wlvif->band];
30d0c8fd 3057 wlvif->rate_set = wlvif->basic_rate_set;
ebba60c6
JO
3058}
3059
b0ed8a4d
AN
3060static void wl1271_sta_handle_idle(struct wl1271 *wl, struct wl12xx_vif *wlvif,
3061 bool idle)
3062{
3063 bool cur_idle = !test_bit(WLVIF_FLAG_ACTIVE, &wlvif->flags);
3064
3065 if (idle == cur_idle)
3066 return;
3067
3068 if (idle) {
3069 clear_bit(WLVIF_FLAG_ACTIVE, &wlvif->flags);
3070 } else {
3071 /* The current firmware only supports sched_scan in idle */
3072 if (wl->sched_vif == wlvif)
3073 wl->ops->sched_scan_stop(wl, wlvif);
3074
3075 set_bit(WLVIF_FLAG_ACTIVE, &wlvif->flags);
3076 }
3077}
3078
9f259c4e
EP
3079static int wl12xx_config_vif(struct wl1271 *wl, struct wl12xx_vif *wlvif,
3080 struct ieee80211_conf *conf, u32 changed)
0d58cbff
JO
3081{
3082 int ret;
3083
6bd65029 3084 if (conf->power_level != wlvif->power_level) {
0603d891 3085 ret = wl1271_acx_tx_power(wl, wlvif, conf->power_level);
0d58cbff 3086 if (ret < 0)
9f259c4e 3087 return ret;
33c2c06c 3088
6bd65029 3089 wlvif->power_level = conf->power_level;
0d58cbff
JO
3090 }
3091
9f259c4e 3092 return 0;
0d58cbff
JO
3093}
3094
9f259c4e 3095static int wl1271_op_config(struct ieee80211_hw *hw, u32 changed)
f5fc0f86 3096{
9f259c4e
EP
3097 struct wl1271 *wl = hw->priv;
3098 struct wl12xx_vif *wlvif;
3099 struct ieee80211_conf *conf = &hw->conf;
b6970ee5 3100 int ret = 0;
f5fc0f86 3101
b6970ee5 3102 wl1271_debug(DEBUG_MAC80211, "mac80211 config psm %s power %d %s"
9f259c4e 3103 " changed 0x%x",
9f259c4e
EP
3104 conf->flags & IEEE80211_CONF_PS ? "on" : "off",
3105 conf->power_level,
3106 conf->flags & IEEE80211_CONF_IDLE ? "idle" : "in use",
3107 changed);
3108
9f259c4e
EP
3109 mutex_lock(&wl->mutex);
3110
9f259c4e
EP
3111 if (changed & IEEE80211_CONF_CHANGE_POWER)
3112 wl->power_level = conf->power_level;
3113
4cc53383 3114 if (unlikely(wl->state != WLCORE_STATE_ON))
9f259c4e
EP
3115 goto out;
3116
3117 ret = wl1271_ps_elp_wakeup(wl);
3118 if (ret < 0)
3119 goto out;
3120
3121 /* configure each interface */
3122 wl12xx_for_each_wlvif(wl, wlvif) {
3123 ret = wl12xx_config_vif(wl, wlvif, conf, changed);
3124 if (ret < 0)
3125 goto out_sleep;
3126 }
3127
f5fc0f86
LC
3128out_sleep:
3129 wl1271_ps_elp_sleep(wl);
3130
3131out:
3132 mutex_unlock(&wl->mutex);
3133
3134 return ret;
3135}
3136
b54853f1
JO
3137struct wl1271_filter_params {
3138 bool enabled;
3139 int mc_list_length;
3140 u8 mc_list[ACX_MC_ADDRESS_GROUP_MAX][ETH_ALEN];
3141};
3142
22bedad3
JP
3143static u64 wl1271_op_prepare_multicast(struct ieee80211_hw *hw,
3144 struct netdev_hw_addr_list *mc_list)
c87dec9f 3145{
c87dec9f 3146 struct wl1271_filter_params *fp;
22bedad3 3147 struct netdev_hw_addr *ha;
c87dec9f 3148
74441130 3149 fp = kzalloc(sizeof(*fp), GFP_ATOMIC);
c87dec9f
JO
3150 if (!fp) {
3151 wl1271_error("Out of memory setting filters.");
3152 return 0;
3153 }
3154
3155 /* update multicast filtering parameters */
c87dec9f 3156 fp->mc_list_length = 0;
22bedad3
JP
3157 if (netdev_hw_addr_list_count(mc_list) > ACX_MC_ADDRESS_GROUP_MAX) {
3158 fp->enabled = false;
3159 } else {
3160 fp->enabled = true;
3161 netdev_hw_addr_list_for_each(ha, mc_list) {
c87dec9f 3162 memcpy(fp->mc_list[fp->mc_list_length],
22bedad3 3163 ha->addr, ETH_ALEN);
c87dec9f 3164 fp->mc_list_length++;
22bedad3 3165 }
c87dec9f
JO
3166 }
3167
b54853f1 3168 return (u64)(unsigned long)fp;
c87dec9f 3169}
f5fc0f86 3170
b54853f1
JO
3171#define WL1271_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
3172 FIF_ALLMULTI | \
3173 FIF_FCSFAIL | \
3174 FIF_BCN_PRBRESP_PROMISC | \
3175 FIF_CONTROL | \
3176 FIF_OTHER_BSS)
3177
f5fc0f86
LC
3178static void wl1271_op_configure_filter(struct ieee80211_hw *hw,
3179 unsigned int changed,
c87dec9f 3180 unsigned int *total, u64 multicast)
f5fc0f86 3181{
b54853f1 3182 struct wl1271_filter_params *fp = (void *)(unsigned long)multicast;
f5fc0f86 3183 struct wl1271 *wl = hw->priv;
6e8cd331 3184 struct wl12xx_vif *wlvif;
536129c8 3185
b54853f1 3186 int ret;
f5fc0f86 3187
7d057869
AN
3188 wl1271_debug(DEBUG_MAC80211, "mac80211 configure filter changed %x"
3189 " total %x", changed, *total);
f5fc0f86 3190
b54853f1
JO
3191 mutex_lock(&wl->mutex);
3192
2c10bb9c
SD
3193 *total &= WL1271_SUPPORTED_FILTERS;
3194 changed &= WL1271_SUPPORTED_FILTERS;
3195
4cc53383 3196 if (unlikely(wl->state != WLCORE_STATE_ON))
b54853f1
JO
3197 goto out;
3198
a620865e 3199 ret = wl1271_ps_elp_wakeup(wl);
b54853f1
JO
3200 if (ret < 0)
3201 goto out;
3202
6e8cd331
EP
3203 wl12xx_for_each_wlvif(wl, wlvif) {
3204 if (wlvif->bss_type != BSS_TYPE_AP_BSS) {
3205 if (*total & FIF_ALLMULTI)
3206 ret = wl1271_acx_group_address_tbl(wl, wlvif,
3207 false,
3208 NULL, 0);
3209 else if (fp)
3210 ret = wl1271_acx_group_address_tbl(wl, wlvif,
3211 fp->enabled,
3212 fp->mc_list,
3213 fp->mc_list_length);
3214 if (ret < 0)
3215 goto out_sleep;
3216 }
7d057869 3217 }
f5fc0f86 3218
08c1d1c7
EP
3219 /*
3220 * the fw doesn't provide an api to configure the filters. instead,
3221 * the filters configuration is based on the active roles / ROC
3222 * state.
3223 */
b54853f1
JO
3224
3225out_sleep:
3226 wl1271_ps_elp_sleep(wl);
3227
3228out:
3229 mutex_unlock(&wl->mutex);
14b228a0 3230 kfree(fp);
f5fc0f86
LC
3231}
3232
170d0e67
EP
3233static int wl1271_record_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
3234 u8 id, u8 key_type, u8 key_size,
3235 const u8 *key, u8 hlid, u32 tx_seq_32,
3236 u16 tx_seq_16)
7f179b46
AN
3237{
3238 struct wl1271_ap_key *ap_key;
3239 int i;
3240
3241 wl1271_debug(DEBUG_CRYPT, "record ap key id %d", (int)id);
3242
3243 if (key_size > MAX_KEY_SIZE)
3244 return -EINVAL;
3245
3246 /*
3247 * Find next free entry in ap_keys. Also check we are not replacing
3248 * an existing key.
3249 */
3250 for (i = 0; i < MAX_NUM_KEYS; i++) {
170d0e67 3251 if (wlvif->ap.recorded_keys[i] == NULL)
7f179b46
AN
3252 break;
3253
170d0e67 3254 if (wlvif->ap.recorded_keys[i]->id == id) {
7f179b46
AN
3255 wl1271_warning("trying to record key replacement");
3256 return -EINVAL;
3257 }
3258 }
3259
3260 if (i == MAX_NUM_KEYS)
3261 return -EBUSY;
3262
3263 ap_key = kzalloc(sizeof(*ap_key), GFP_KERNEL);
3264 if (!ap_key)
3265 return -ENOMEM;
3266
3267 ap_key->id = id;
3268 ap_key->key_type = key_type;
3269 ap_key->key_size = key_size;
3270 memcpy(ap_key->key, key, key_size);
3271 ap_key->hlid = hlid;
3272 ap_key->tx_seq_32 = tx_seq_32;
3273 ap_key->tx_seq_16 = tx_seq_16;
3274
170d0e67 3275 wlvif->ap.recorded_keys[i] = ap_key;
7f179b46
AN
3276 return 0;
3277}
3278
170d0e67 3279static void wl1271_free_ap_keys(struct wl1271 *wl, struct wl12xx_vif *wlvif)
7f179b46
AN
3280{
3281 int i;
3282
3283 for (i = 0; i < MAX_NUM_KEYS; i++) {
170d0e67
EP
3284 kfree(wlvif->ap.recorded_keys[i]);
3285 wlvif->ap.recorded_keys[i] = NULL;
7f179b46
AN
3286 }
3287}
3288
a8ab39a4 3289static int wl1271_ap_init_hwenc(struct wl1271 *wl, struct wl12xx_vif *wlvif)
7f179b46
AN
3290{
3291 int i, ret = 0;
3292 struct wl1271_ap_key *key;
3293 bool wep_key_added = false;
3294
3295 for (i = 0; i < MAX_NUM_KEYS; i++) {
7f97b487 3296 u8 hlid;
170d0e67 3297 if (wlvif->ap.recorded_keys[i] == NULL)
7f179b46
AN
3298 break;
3299
170d0e67 3300 key = wlvif->ap.recorded_keys[i];
7f97b487
EP
3301 hlid = key->hlid;
3302 if (hlid == WL12XX_INVALID_LINK_ID)
a8ab39a4 3303 hlid = wlvif->ap.bcast_hlid;
7f97b487 3304
a8ab39a4 3305 ret = wl1271_cmd_set_ap_key(wl, wlvif, KEY_ADD_OR_REPLACE,
7f179b46
AN
3306 key->id, key->key_type,
3307 key->key_size, key->key,
7f97b487 3308 hlid, key->tx_seq_32,
7f179b46
AN
3309 key->tx_seq_16);
3310 if (ret < 0)
3311 goto out;
3312
3313 if (key->key_type == KEY_WEP)
3314 wep_key_added = true;
3315 }
3316
3317 if (wep_key_added) {
f75c753f 3318 ret = wl12xx_cmd_set_default_wep_key(wl, wlvif->default_key,
a8ab39a4 3319 wlvif->ap.bcast_hlid);
7f179b46
AN
3320 if (ret < 0)
3321 goto out;
3322 }
3323
3324out:
170d0e67 3325 wl1271_free_ap_keys(wl, wlvif);
7f179b46
AN
3326 return ret;
3327}
3328
536129c8
EP
3329static int wl1271_set_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
3330 u16 action, u8 id, u8 key_type,
7f179b46
AN
3331 u8 key_size, const u8 *key, u32 tx_seq_32,
3332 u16 tx_seq_16, struct ieee80211_sta *sta)
3333{
3334 int ret;
536129c8 3335 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
7f179b46
AN
3336
3337 if (is_ap) {
3338 struct wl1271_station *wl_sta;
3339 u8 hlid;
3340
3341 if (sta) {
3342 wl_sta = (struct wl1271_station *)sta->drv_priv;
3343 hlid = wl_sta->hlid;
3344 } else {
a8ab39a4 3345 hlid = wlvif->ap.bcast_hlid;
7f179b46
AN
3346 }
3347
53d40d0b 3348 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
7f179b46
AN
3349 /*
3350 * We do not support removing keys after AP shutdown.
3351 * Pretend we do to make mac80211 happy.
3352 */
3353 if (action != KEY_ADD_OR_REPLACE)
3354 return 0;
3355
170d0e67 3356 ret = wl1271_record_ap_key(wl, wlvif, id,
7f179b46
AN
3357 key_type, key_size,
3358 key, hlid, tx_seq_32,
3359 tx_seq_16);
3360 } else {
a8ab39a4 3361 ret = wl1271_cmd_set_ap_key(wl, wlvif, action,
7f179b46
AN
3362 id, key_type, key_size,
3363 key, hlid, tx_seq_32,
3364 tx_seq_16);
3365 }
3366
3367 if (ret < 0)
3368 return ret;
3369 } else {
3370 const u8 *addr;
3371 static const u8 bcast_addr[ETH_ALEN] = {
3372 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
3373 };
3374
3375 addr = sta ? sta->addr : bcast_addr;
3376
3377 if (is_zero_ether_addr(addr)) {
3378 /* We dont support TX only encryption */
3379 return -EOPNOTSUPP;
3380 }
3381
3382 /* The wl1271 does not allow to remove unicast keys - they
3383 will be cleared automatically on next CMD_JOIN. Ignore the
3384 request silently, as we dont want the mac80211 to emit
3385 an error message. */
3386 if (action == KEY_REMOVE && !is_broadcast_ether_addr(addr))
3387 return 0;
3388
010d3d30
EP
3389 /* don't remove key if hlid was already deleted */
3390 if (action == KEY_REMOVE &&
154da67c 3391 wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
010d3d30
EP
3392 return 0;
3393
a8ab39a4 3394 ret = wl1271_cmd_set_sta_key(wl, wlvif, action,
7f179b46
AN
3395 id, key_type, key_size,
3396 key, addr, tx_seq_32,
3397 tx_seq_16);
3398 if (ret < 0)
3399 return ret;
3400
7f179b46
AN
3401 }
3402
3403 return 0;
3404}
3405
a1c597f2 3406static int wlcore_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
f5fc0f86
LC
3407 struct ieee80211_vif *vif,
3408 struct ieee80211_sta *sta,
3409 struct ieee80211_key_conf *key_conf)
3410{
3411 struct wl1271 *wl = hw->priv;
af390f4d
EP
3412 int ret;
3413 bool might_change_spare =
3414 key_conf->cipher == WL1271_CIPHER_SUITE_GEM ||
3415 key_conf->cipher == WLAN_CIPHER_SUITE_TKIP;
3416
3417 if (might_change_spare) {
3418 /*
3419 * stop the queues and flush to ensure the next packets are
3420 * in sync with FW spare block accounting
3421 */
af390f4d 3422 wlcore_stop_queues(wl, WLCORE_QUEUE_STOP_REASON_SPARE_BLK);
af390f4d
EP
3423 wl1271_tx_flush(wl);
3424 }
3425
3426 mutex_lock(&wl->mutex);
3427
3428 if (unlikely(wl->state != WLCORE_STATE_ON)) {
3429 ret = -EAGAIN;
3430 goto out_wake_queues;
3431 }
a1c597f2 3432
af390f4d
EP
3433 ret = wl1271_ps_elp_wakeup(wl);
3434 if (ret < 0)
3435 goto out_wake_queues;
3436
3437 ret = wlcore_hw_set_key(wl, cmd, vif, sta, key_conf);
3438
3439 wl1271_ps_elp_sleep(wl);
3440
3441out_wake_queues:
3442 if (might_change_spare)
3443 wlcore_wake_queues(wl, WLCORE_QUEUE_STOP_REASON_SPARE_BLK);
3444
3445 mutex_unlock(&wl->mutex);
3446
3447 return ret;
a1c597f2
AN
3448}
3449
3450int wlcore_set_key(struct wl1271 *wl, enum set_key_cmd cmd,
3451 struct ieee80211_vif *vif,
3452 struct ieee80211_sta *sta,
3453 struct ieee80211_key_conf *key_conf)
3454{
536129c8 3455 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
f5fc0f86 3456 int ret;
ac4e4ce5
JO
3457 u32 tx_seq_32 = 0;
3458 u16 tx_seq_16 = 0;
f5fc0f86 3459 u8 key_type;
93d5d100 3460 u8 hlid;
f5fc0f86 3461
f5fc0f86
LC
3462 wl1271_debug(DEBUG_MAC80211, "mac80211 set key");
3463
7f179b46 3464 wl1271_debug(DEBUG_CRYPT, "CMD: 0x%x sta: %p", cmd, sta);
f5fc0f86 3465 wl1271_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
97359d12 3466 key_conf->cipher, key_conf->keyidx,
f5fc0f86
LC
3467 key_conf->keylen, key_conf->flags);
3468 wl1271_dump(DEBUG_CRYPT, "KEY: ", key_conf->key, key_conf->keylen);
3469
93d5d100
AN
3470 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
3471 if (sta) {
3472 struct wl1271_station *wl_sta = (void *)sta->drv_priv;
3473 hlid = wl_sta->hlid;
3474 } else {
3475 hlid = wlvif->ap.bcast_hlid;
3476 }
3477 else
3478 hlid = wlvif->sta.hlid;
3479
3480 if (hlid != WL12XX_INVALID_LINK_ID) {
3481 u64 tx_seq = wl->links[hlid].total_freed_pkts;
3482 tx_seq_32 = WL1271_TX_SECURITY_HI32(tx_seq);
3483 tx_seq_16 = WL1271_TX_SECURITY_LO16(tx_seq);
3484 }
3485
97359d12
JB
3486 switch (key_conf->cipher) {
3487 case WLAN_CIPHER_SUITE_WEP40:
3488 case WLAN_CIPHER_SUITE_WEP104:
f5fc0f86
LC
3489 key_type = KEY_WEP;
3490
3491 key_conf->hw_key_idx = key_conf->keyidx;
3492 break;
97359d12 3493 case WLAN_CIPHER_SUITE_TKIP:
f5fc0f86 3494 key_type = KEY_TKIP;
f5fc0f86
LC
3495 key_conf->hw_key_idx = key_conf->keyidx;
3496 break;
97359d12 3497 case WLAN_CIPHER_SUITE_CCMP:
f5fc0f86 3498 key_type = KEY_AES;
12d4b975 3499 key_conf->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
f5fc0f86 3500 break;
7a55724e
JO
3501 case WL1271_CIPHER_SUITE_GEM:
3502 key_type = KEY_GEM;
7a55724e 3503 break;
f5fc0f86 3504 default:
97359d12 3505 wl1271_error("Unknown key algo 0x%x", key_conf->cipher);
f5fc0f86 3506
af390f4d 3507 return -EOPNOTSUPP;
f5fc0f86
LC
3508 }
3509
3510 switch (cmd) {
3511 case SET_KEY:
536129c8 3512 ret = wl1271_set_key(wl, wlvif, KEY_ADD_OR_REPLACE,
7f179b46
AN
3513 key_conf->keyidx, key_type,
3514 key_conf->keylen, key_conf->key,
3515 tx_seq_32, tx_seq_16, sta);
f5fc0f86
LC
3516 if (ret < 0) {
3517 wl1271_error("Could not add or replace key");
af390f4d 3518 return ret;
f5fc0f86 3519 }
5ec8a448
EP
3520
3521 /*
3522 * reconfiguring arp response if the unicast (or common)
3523 * encryption key type was changed
3524 */
3525 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
3526 (sta || key_type == KEY_WEP) &&
3527 wlvif->encryption_type != key_type) {
3528 wlvif->encryption_type = key_type;
3529 ret = wl1271_cmd_build_arp_rsp(wl, wlvif);
3530 if (ret < 0) {
3531 wl1271_warning("build arp rsp failed: %d", ret);
af390f4d 3532 return ret;
5ec8a448
EP
3533 }
3534 }
f5fc0f86
LC
3535 break;
3536
3537 case DISABLE_KEY:
536129c8 3538 ret = wl1271_set_key(wl, wlvif, KEY_REMOVE,
7f179b46
AN
3539 key_conf->keyidx, key_type,
3540 key_conf->keylen, key_conf->key,
3541 0, 0, sta);
f5fc0f86
LC
3542 if (ret < 0) {
3543 wl1271_error("Could not remove key");
af390f4d 3544 return ret;
f5fc0f86
LC
3545 }
3546 break;
3547
3548 default:
3549 wl1271_error("Unsupported key cmd 0x%x", cmd);
af390f4d 3550 return -EOPNOTSUPP;
f5fc0f86
LC
3551 }
3552
f5fc0f86
LC
3553 return ret;
3554}
a1c597f2 3555EXPORT_SYMBOL_GPL(wlcore_set_key);
f5fc0f86 3556
ba1e6eb9
YD
3557static void wl1271_op_set_default_key_idx(struct ieee80211_hw *hw,
3558 struct ieee80211_vif *vif,
3559 int key_idx)
3560{
3561 struct wl1271 *wl = hw->priv;
3562 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3563 int ret;
3564
3565 wl1271_debug(DEBUG_MAC80211, "mac80211 set default key idx %d",
3566 key_idx);
3567
bf4e5f1a
EP
3568 /* we don't handle unsetting of default key */
3569 if (key_idx == -1)
3570 return;
3571
ba1e6eb9
YD
3572 mutex_lock(&wl->mutex);
3573
3574 if (unlikely(wl->state != WLCORE_STATE_ON)) {
3575 ret = -EAGAIN;
3576 goto out_unlock;
3577 }
3578
3579 ret = wl1271_ps_elp_wakeup(wl);
3580 if (ret < 0)
3581 goto out_unlock;
3582
3583 wlvif->default_key = key_idx;
3584
3585 /* the default WEP key needs to be configured at least once */
3586 if (wlvif->encryption_type == KEY_WEP) {
3587 ret = wl12xx_cmd_set_default_wep_key(wl,
3588 key_idx,
3589 wlvif->sta.hlid);
3590 if (ret < 0)
3591 goto out_sleep;
3592 }
3593
3594out_sleep:
3595 wl1271_ps_elp_sleep(wl);
3596
3597out_unlock:
3598 mutex_unlock(&wl->mutex);
3599}
3600
6b70e7eb
VG
3601void wlcore_regdomain_config(struct wl1271 *wl)
3602{
3603 int ret;
3604
3605 if (!(wl->quirks & WLCORE_QUIRK_REGDOMAIN_CONF))
3606 return;
3607
3608 mutex_lock(&wl->mutex);
75592be5
AN
3609
3610 if (unlikely(wl->state != WLCORE_STATE_ON))
3611 goto out;
3612
6b70e7eb
VG
3613 ret = wl1271_ps_elp_wakeup(wl);
3614 if (ret < 0)
3615 goto out;
3616
3617 ret = wlcore_cmd_regdomain_config_locked(wl);
3618 if (ret < 0) {
3619 wl12xx_queue_recovery_work(wl);
3620 goto out;
3621 }
3622
3623 wl1271_ps_elp_sleep(wl);
3624out:
3625 mutex_unlock(&wl->mutex);
3626}
3627
f5fc0f86 3628static int wl1271_op_hw_scan(struct ieee80211_hw *hw,
a060bbfe 3629 struct ieee80211_vif *vif,
c56ef672 3630 struct ieee80211_scan_request *hw_req)
f5fc0f86 3631{
c56ef672 3632 struct cfg80211_scan_request *req = &hw_req->req;
f5fc0f86
LC
3633 struct wl1271 *wl = hw->priv;
3634 int ret;
3635 u8 *ssid = NULL;
abb0b3bf 3636 size_t len = 0;
f5fc0f86
LC
3637
3638 wl1271_debug(DEBUG_MAC80211, "mac80211 hw scan");
3639
3640 if (req->n_ssids) {
3641 ssid = req->ssids[0].ssid;
abb0b3bf 3642 len = req->ssids[0].ssid_len;
f5fc0f86
LC
3643 }
3644
3645 mutex_lock(&wl->mutex);
3646
4cc53383 3647 if (unlikely(wl->state != WLCORE_STATE_ON)) {
b739a42c
JO
3648 /*
3649 * We cannot return -EBUSY here because cfg80211 will expect
3650 * a call to ieee80211_scan_completed if we do - in this case
3651 * there won't be any call.
3652 */
3653 ret = -EAGAIN;
3654 goto out;
3655 }
3656
a620865e 3657 ret = wl1271_ps_elp_wakeup(wl);
f5fc0f86
LC
3658 if (ret < 0)
3659 goto out;
3660
97fd311a
EP
3661 /* fail if there is any role in ROC */
3662 if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES) {
92e712da
EP
3663 /* don't allow scanning right now */
3664 ret = -EBUSY;
3665 goto out_sleep;
3666 }
3667
78e28062 3668 ret = wlcore_scan(hw->priv, vif, ssid, len, req);
251c177f 3669out_sleep:
f5fc0f86 3670 wl1271_ps_elp_sleep(wl);
f5fc0f86
LC
3671out:
3672 mutex_unlock(&wl->mutex);
3673
3674 return ret;
3675}
3676
73ecce31
EP
3677static void wl1271_op_cancel_hw_scan(struct ieee80211_hw *hw,
3678 struct ieee80211_vif *vif)
3679{
3680 struct wl1271 *wl = hw->priv;
78e28062 3681 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
73ecce31
EP
3682 int ret;
3683
3684 wl1271_debug(DEBUG_MAC80211, "mac80211 cancel hw scan");
3685
3686 mutex_lock(&wl->mutex);
3687
4cc53383 3688 if (unlikely(wl->state != WLCORE_STATE_ON))
73ecce31
EP
3689 goto out;
3690
3691 if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
3692 goto out;
3693
3694 ret = wl1271_ps_elp_wakeup(wl);
3695 if (ret < 0)
3696 goto out;
3697
3698 if (wl->scan.state != WL1271_SCAN_STATE_DONE) {
78e28062 3699 ret = wl->ops->scan_stop(wl, wlvif);
73ecce31
EP
3700 if (ret < 0)
3701 goto out_sleep;
3702 }
55df5afb
AN
3703
3704 /*
3705 * Rearm the tx watchdog just before idling scan. This
3706 * prevents just-finished scans from triggering the watchdog
3707 */
3708 wl12xx_rearm_tx_watchdog_locked(wl);
3709
73ecce31
EP
3710 wl->scan.state = WL1271_SCAN_STATE_IDLE;
3711 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
c50a2825 3712 wl->scan_wlvif = NULL;
73ecce31
EP
3713 wl->scan.req = NULL;
3714 ieee80211_scan_completed(wl->hw, true);
3715
3716out_sleep:
3717 wl1271_ps_elp_sleep(wl);
3718out:
3719 mutex_unlock(&wl->mutex);
3720
3721 cancel_delayed_work_sync(&wl->scan_complete_work);
3722}
3723
33c2c06c
LC
3724static int wl1271_op_sched_scan_start(struct ieee80211_hw *hw,
3725 struct ieee80211_vif *vif,
3726 struct cfg80211_sched_scan_request *req,
633e2713 3727 struct ieee80211_scan_ies *ies)
33c2c06c
LC
3728{
3729 struct wl1271 *wl = hw->priv;
536129c8 3730 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
33c2c06c
LC
3731 int ret;
3732
3733 wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_start");
3734
3735 mutex_lock(&wl->mutex);
3736
4cc53383 3737 if (unlikely(wl->state != WLCORE_STATE_ON)) {
9e0dc890
PF
3738 ret = -EAGAIN;
3739 goto out;
3740 }
3741
33c2c06c
LC
3742 ret = wl1271_ps_elp_wakeup(wl);
3743 if (ret < 0)
3744 goto out;
3745
78e28062 3746 ret = wl->ops->sched_scan_start(wl, wlvif, req, ies);
33c2c06c
LC
3747 if (ret < 0)
3748 goto out_sleep;
3749
10199756 3750 wl->sched_vif = wlvif;
33c2c06c
LC
3751
3752out_sleep:
3753 wl1271_ps_elp_sleep(wl);
3754out:
3755 mutex_unlock(&wl->mutex);
3756 return ret;
3757}
3758
37e3308c
JB
3759static int wl1271_op_sched_scan_stop(struct ieee80211_hw *hw,
3760 struct ieee80211_vif *vif)
33c2c06c
LC
3761{
3762 struct wl1271 *wl = hw->priv;
78f85f50 3763 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
33c2c06c
LC
3764 int ret;
3765
3766 wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_stop");
3767
3768 mutex_lock(&wl->mutex);
3769
4cc53383 3770 if (unlikely(wl->state != WLCORE_STATE_ON))
9e0dc890
PF
3771 goto out;
3772
33c2c06c
LC
3773 ret = wl1271_ps_elp_wakeup(wl);
3774 if (ret < 0)
3775 goto out;
3776
78e28062 3777 wl->ops->sched_scan_stop(wl, wlvif);
33c2c06c
LC
3778
3779 wl1271_ps_elp_sleep(wl);
3780out:
3781 mutex_unlock(&wl->mutex);
37e3308c
JB
3782
3783 return 0;
33c2c06c
LC
3784}
3785
68d069c4
AN
3786static int wl1271_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
3787{
3788 struct wl1271 *wl = hw->priv;
3789 int ret = 0;
3790
3791 mutex_lock(&wl->mutex);
3792
4cc53383 3793 if (unlikely(wl->state != WLCORE_STATE_ON)) {
68d069c4
AN
3794 ret = -EAGAIN;
3795 goto out;
3796 }
3797
a620865e 3798 ret = wl1271_ps_elp_wakeup(wl);
68d069c4
AN
3799 if (ret < 0)
3800 goto out;
3801
5f704d18 3802 ret = wl1271_acx_frag_threshold(wl, value);
68d069c4
AN
3803 if (ret < 0)
3804 wl1271_warning("wl1271_op_set_frag_threshold failed: %d", ret);
3805
3806 wl1271_ps_elp_sleep(wl);
3807
3808out:
3809 mutex_unlock(&wl->mutex);
3810
3811 return ret;
3812}
3813
f5fc0f86
LC
3814static int wl1271_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3815{
3816 struct wl1271 *wl = hw->priv;
6e8cd331 3817 struct wl12xx_vif *wlvif;
aecb0565 3818 int ret = 0;
f5fc0f86
LC
3819
3820 mutex_lock(&wl->mutex);
3821
4cc53383 3822 if (unlikely(wl->state != WLCORE_STATE_ON)) {
f8d9802f 3823 ret = -EAGAIN;
aecb0565 3824 goto out;
f8d9802f 3825 }
aecb0565 3826
a620865e 3827 ret = wl1271_ps_elp_wakeup(wl);
f5fc0f86
LC
3828 if (ret < 0)
3829 goto out;
3830
6e8cd331
EP
3831 wl12xx_for_each_wlvif(wl, wlvif) {
3832 ret = wl1271_acx_rts_threshold(wl, wlvif, value);
3833 if (ret < 0)
3834 wl1271_warning("set rts threshold failed: %d", ret);
3835 }
f5fc0f86
LC
3836 wl1271_ps_elp_sleep(wl);
3837
3838out:
3839 mutex_unlock(&wl->mutex);
3840
3841 return ret;
3842}
3843
d48055d9
EP
3844static void wl12xx_remove_ie(struct sk_buff *skb, u8 eid, int ieoffset)
3845{
3846 int len;
3847 const u8 *next, *end = skb->data + skb->len;
3848 u8 *ie = (u8 *)cfg80211_find_ie(eid, skb->data + ieoffset,
3849 skb->len - ieoffset);
3850 if (!ie)
3851 return;
3852 len = ie[1] + 2;
3853 next = ie + len;
3854 memmove(ie, next, end - next);
3855 skb_trim(skb, skb->len - len);
3856}
3857
26b4bf2e
EP
3858static void wl12xx_remove_vendor_ie(struct sk_buff *skb,
3859 unsigned int oui, u8 oui_type,
3860 int ieoffset)
3861{
3862 int len;
3863 const u8 *next, *end = skb->data + skb->len;
3864 u8 *ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
3865 skb->data + ieoffset,
3866 skb->len - ieoffset);
3867 if (!ie)
3868 return;
3869 len = ie[1] + 2;
3870 next = ie + len;
3871 memmove(ie, next, end - next);
3872 skb_trim(skb, skb->len - len);
3873}
3874
341f2c11
AN
3875static int wl1271_ap_set_probe_resp_tmpl(struct wl1271 *wl, u32 rates,
3876 struct ieee80211_vif *vif)
560f0024 3877{
cdaac628 3878 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
560f0024
AN
3879 struct sk_buff *skb;
3880 int ret;
3881
341f2c11 3882 skb = ieee80211_proberesp_get(wl->hw, vif);
560f0024 3883 if (!skb)
341f2c11 3884 return -EOPNOTSUPP;
560f0024 3885
cdaac628 3886 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
560f0024
AN
3887 CMD_TEMPL_AP_PROBE_RESPONSE,
3888 skb->data,
3889 skb->len, 0,
3890 rates);
560f0024 3891 dev_kfree_skb(skb);
62c2e579
LC
3892
3893 if (ret < 0)
3894 goto out;
3895
3896 wl1271_debug(DEBUG_AP, "probe response updated");
3897 set_bit(WLVIF_FLAG_AP_PROBE_RESP_SET, &wlvif->flags);
3898
3899out:
560f0024
AN
3900 return ret;
3901}
3902
3903static int wl1271_ap_set_probe_resp_tmpl_legacy(struct wl1271 *wl,
3904 struct ieee80211_vif *vif,
3905 u8 *probe_rsp_data,
3906 size_t probe_rsp_len,
3907 u32 rates)
68eaaf6e 3908{
1fe9f161
EP
3909 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3910 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
68eaaf6e
AN
3911 u8 probe_rsp_templ[WL1271_CMD_TEMPL_MAX_SIZE];
3912 int ssid_ie_offset, ie_offset, templ_len;
3913 const u8 *ptr;
3914
3915 /* no need to change probe response if the SSID is set correctly */
1fe9f161 3916 if (wlvif->ssid_len > 0)
cdaac628 3917 return wl1271_cmd_template_set(wl, wlvif->role_id,
68eaaf6e
AN
3918 CMD_TEMPL_AP_PROBE_RESPONSE,
3919 probe_rsp_data,
3920 probe_rsp_len, 0,
3921 rates);
3922
3923 if (probe_rsp_len + bss_conf->ssid_len > WL1271_CMD_TEMPL_MAX_SIZE) {
3924 wl1271_error("probe_rsp template too big");
3925 return -EINVAL;
3926 }
3927
3928 /* start searching from IE offset */
3929 ie_offset = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
3930
3931 ptr = cfg80211_find_ie(WLAN_EID_SSID, probe_rsp_data + ie_offset,
3932 probe_rsp_len - ie_offset);
3933 if (!ptr) {
3934 wl1271_error("No SSID in beacon!");
3935 return -EINVAL;
3936 }
3937
3938 ssid_ie_offset = ptr - probe_rsp_data;
3939 ptr += (ptr[1] + 2);
3940
3941 memcpy(probe_rsp_templ, probe_rsp_data, ssid_ie_offset);
3942
3943 /* insert SSID from bss_conf */
3944 probe_rsp_templ[ssid_ie_offset] = WLAN_EID_SSID;
3945 probe_rsp_templ[ssid_ie_offset + 1] = bss_conf->ssid_len;
3946 memcpy(probe_rsp_templ + ssid_ie_offset + 2,
3947 bss_conf->ssid, bss_conf->ssid_len);
3948 templ_len = ssid_ie_offset + 2 + bss_conf->ssid_len;
3949
3950 memcpy(probe_rsp_templ + ssid_ie_offset + 2 + bss_conf->ssid_len,
3951 ptr, probe_rsp_len - (ptr - probe_rsp_data));
3952 templ_len += probe_rsp_len - (ptr - probe_rsp_data);
3953
cdaac628 3954 return wl1271_cmd_template_set(wl, wlvif->role_id,
68eaaf6e
AN
3955 CMD_TEMPL_AP_PROBE_RESPONSE,
3956 probe_rsp_templ,
3957 templ_len, 0,
3958 rates);
3959}
3960
e78a287a 3961static int wl1271_bss_erp_info_changed(struct wl1271 *wl,
0603d891 3962 struct ieee80211_vif *vif,
f5fc0f86
LC
3963 struct ieee80211_bss_conf *bss_conf,
3964 u32 changed)
3965{
0603d891 3966 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
e78a287a 3967 int ret = 0;
f5fc0f86 3968
e78a287a
AN
3969 if (changed & BSS_CHANGED_ERP_SLOT) {
3970 if (bss_conf->use_short_slot)
0603d891 3971 ret = wl1271_acx_slot(wl, wlvif, SLOT_TIME_SHORT);
e78a287a 3972 else
0603d891 3973 ret = wl1271_acx_slot(wl, wlvif, SLOT_TIME_LONG);
e78a287a
AN
3974 if (ret < 0) {
3975 wl1271_warning("Set slot time failed %d", ret);
3976 goto out;
3977 }
3978 }
f5fc0f86 3979
e78a287a
AN
3980 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3981 if (bss_conf->use_short_preamble)
0603d891 3982 wl1271_acx_set_preamble(wl, wlvif, ACX_PREAMBLE_SHORT);
e78a287a 3983 else
0603d891 3984 wl1271_acx_set_preamble(wl, wlvif, ACX_PREAMBLE_LONG);
e78a287a 3985 }
f5fc0f86 3986
e78a287a
AN
3987 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
3988 if (bss_conf->use_cts_prot)
0603d891
EP
3989 ret = wl1271_acx_cts_protect(wl, wlvif,
3990 CTSPROTECT_ENABLE);
e78a287a 3991 else
0603d891
EP
3992 ret = wl1271_acx_cts_protect(wl, wlvif,
3993 CTSPROTECT_DISABLE);
e78a287a
AN
3994 if (ret < 0) {
3995 wl1271_warning("Set ctsprotect failed %d", ret);
3996 goto out;
3997 }
3998 }
f8d9802f 3999
e78a287a
AN
4000out:
4001 return ret;
4002}
f5fc0f86 4003
62c2e579
LC
4004static int wlcore_set_beacon_template(struct wl1271 *wl,
4005 struct ieee80211_vif *vif,
4006 bool is_ap)
4007{
4008 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4009 struct ieee80211_hdr *hdr;
4010 u32 min_rate;
4011 int ret;
8f6ac537 4012 int ieoffset = offsetof(struct ieee80211_mgmt, u.beacon.variable);
62c2e579
LC
4013 struct sk_buff *beacon = ieee80211_beacon_get(wl->hw, vif);
4014 u16 tmpl_id;
4015
4016 if (!beacon) {
4017 ret = -EINVAL;
4018 goto out;
4019 }
4020
4021 wl1271_debug(DEBUG_MASTER, "beacon updated");
4022
3230f35e 4023 ret = wl1271_ssid_set(wlvif, beacon, ieoffset);
62c2e579
LC
4024 if (ret < 0) {
4025 dev_kfree_skb(beacon);
4026 goto out;
4027 }
4028 min_rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
4029 tmpl_id = is_ap ? CMD_TEMPL_AP_BEACON :
4030 CMD_TEMPL_BEACON;
4031 ret = wl1271_cmd_template_set(wl, wlvif->role_id, tmpl_id,
4032 beacon->data,
4033 beacon->len, 0,
4034 min_rate);
4035 if (ret < 0) {
4036 dev_kfree_skb(beacon);
4037 goto out;
4038 }
4039
d50529c0
EP
4040 wlvif->wmm_enabled =
4041 cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
4042 WLAN_OUI_TYPE_MICROSOFT_WMM,
4043 beacon->data + ieoffset,
4044 beacon->len - ieoffset);
4045
62c2e579
LC
4046 /*
4047 * In case we already have a probe-resp beacon set explicitly
4048 * by usermode, don't use the beacon data.
4049 */
4050 if (test_bit(WLVIF_FLAG_AP_PROBE_RESP_SET, &wlvif->flags))
4051 goto end_bcn;
4052
4053 /* remove TIM ie from probe response */
4054 wl12xx_remove_ie(beacon, WLAN_EID_TIM, ieoffset);
4055
4056 /*
4057 * remove p2p ie from probe response.
4058 * the fw reponds to probe requests that don't include
4059 * the p2p ie. probe requests with p2p ie will be passed,
4060 * and will be responded by the supplicant (the spec
4061 * forbids including the p2p ie when responding to probe
4062 * requests that didn't include it).
4063 */
4064 wl12xx_remove_vendor_ie(beacon, WLAN_OUI_WFA,
4065 WLAN_OUI_TYPE_WFA_P2P, ieoffset);
4066
4067 hdr = (struct ieee80211_hdr *) beacon->data;
4068 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4069 IEEE80211_STYPE_PROBE_RESP);
4070 if (is_ap)
4071 ret = wl1271_ap_set_probe_resp_tmpl_legacy(wl, vif,
4072 beacon->data,
4073 beacon->len,
4074 min_rate);
4075 else
4076 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
4077 CMD_TEMPL_PROBE_RESPONSE,
4078 beacon->data,
4079 beacon->len, 0,
4080 min_rate);
4081end_bcn:
4082 dev_kfree_skb(beacon);
4083 if (ret < 0)
4084 goto out;
4085
4086out:
4087 return ret;
4088}
4089
e78a287a
AN
4090static int wl1271_bss_beacon_info_changed(struct wl1271 *wl,
4091 struct ieee80211_vif *vif,
4092 struct ieee80211_bss_conf *bss_conf,
4093 u32 changed)
4094{
87fbcb0f 4095 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
536129c8 4096 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
e78a287a
AN
4097 int ret = 0;
4098
48af2eb0 4099 if (changed & BSS_CHANGED_BEACON_INT) {
e78a287a 4100 wl1271_debug(DEBUG_MASTER, "beacon interval updated: %d",
60e84c2e
JO
4101 bss_conf->beacon_int);
4102
6a899796 4103 wlvif->beacon_int = bss_conf->beacon_int;
60e84c2e
JO
4104 }
4105
560f0024
AN
4106 if ((changed & BSS_CHANGED_AP_PROBE_RESP) && is_ap) {
4107 u32 rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
62c2e579
LC
4108
4109 wl1271_ap_set_probe_resp_tmpl(wl, rate, vif);
560f0024
AN
4110 }
4111
48af2eb0 4112 if (changed & BSS_CHANGED_BEACON) {
62c2e579 4113 ret = wlcore_set_beacon_template(wl, vif, is_ap);
e78a287a
AN
4114 if (ret < 0)
4115 goto out;
e78a287a 4116
830513ab
EP
4117 if (test_and_clear_bit(WLVIF_FLAG_BEACON_DISABLED,
4118 &wlvif->flags)) {
4119 ret = wlcore_hw_dfs_master_restart(wl, wlvif);
4120 if (ret < 0)
4121 goto out;
4122 }
4123 }
e78a287a 4124out:
560f0024
AN
4125 if (ret != 0)
4126 wl1271_error("beacon info change failed: %d", ret);
e78a287a
AN
4127 return ret;
4128}
4129
4130/* AP mode changes */
4131static void wl1271_bss_info_changed_ap(struct wl1271 *wl,
4132 struct ieee80211_vif *vif,
4133 struct ieee80211_bss_conf *bss_conf,
4134 u32 changed)
4135{
87fbcb0f 4136 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
e78a287a 4137 int ret = 0;
e0d8bbf0 4138
b6970ee5 4139 if (changed & BSS_CHANGED_BASIC_RATES) {
e78a287a 4140 u32 rates = bss_conf->basic_rates;
5da11dcd 4141
87fbcb0f 4142 wlvif->basic_rate_set = wl1271_tx_enabled_rates_get(wl, rates,
1b92f15e 4143 wlvif->band);
d2d66c56 4144 wlvif->basic_rate = wl1271_tx_min_rate_get(wl,
87fbcb0f 4145 wlvif->basic_rate_set);
70f47424 4146
87fbcb0f 4147 ret = wl1271_init_ap_rates(wl, wlvif);
e78a287a 4148 if (ret < 0) {
70f47424 4149 wl1271_error("AP rate policy change failed %d", ret);
e78a287a
AN
4150 goto out;
4151 }
c45a85b5 4152
784f694d 4153 ret = wl1271_ap_init_templates(wl, vif);
c45a85b5
AN
4154 if (ret < 0)
4155 goto out;
62c2e579
LC
4156
4157 ret = wl1271_ap_set_probe_resp_tmpl(wl, wlvif->basic_rate, vif);
4158 if (ret < 0)
4159 goto out;
4160
4161 ret = wlcore_set_beacon_template(wl, vif, true);
4162 if (ret < 0)
4163 goto out;
e78a287a 4164 }
2f6724b2 4165
e78a287a
AN
4166 ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf, changed);
4167 if (ret < 0)
4168 goto out;
30240fc7 4169
48af2eb0 4170 if (changed & BSS_CHANGED_BEACON_ENABLED) {
e78a287a 4171 if (bss_conf->enable_beacon) {
53d40d0b 4172 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
87fbcb0f 4173 ret = wl12xx_cmd_role_start_ap(wl, wlvif);
e78a287a
AN
4174 if (ret < 0)
4175 goto out;
e0d8bbf0 4176
a8ab39a4 4177 ret = wl1271_ap_init_hwenc(wl, wlvif);
7f179b46
AN
4178 if (ret < 0)
4179 goto out;
cf42039f 4180
53d40d0b 4181 set_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags);
cf42039f 4182 wl1271_debug(DEBUG_AP, "started AP");
e0d8bbf0 4183 }
e78a287a 4184 } else {
53d40d0b 4185 if (test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
187e52cc
AN
4186 /*
4187 * AP might be in ROC in case we have just
4188 * sent auth reply. handle it.
4189 */
4190 if (test_bit(wlvif->role_id, wl->roc_map))
4191 wl12xx_croc(wl, wlvif->role_id);
4192
0603d891 4193 ret = wl12xx_cmd_role_stop_ap(wl, wlvif);
e78a287a
AN
4194 if (ret < 0)
4195 goto out;
e0d8bbf0 4196
53d40d0b 4197 clear_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags);
560f0024
AN
4198 clear_bit(WLVIF_FLAG_AP_PROBE_RESP_SET,
4199 &wlvif->flags);
e78a287a
AN
4200 wl1271_debug(DEBUG_AP, "stopped AP");
4201 }
4202 }
4203 }
e0d8bbf0 4204
0603d891 4205 ret = wl1271_bss_erp_info_changed(wl, vif, bss_conf, changed);
e78a287a
AN
4206 if (ret < 0)
4207 goto out;
0b932ab9
AN
4208
4209 /* Handle HT information change */
4210 if ((changed & BSS_CHANGED_HT) &&
4bf88530 4211 (bss_conf->chandef.width != NL80211_CHAN_WIDTH_20_NOHT)) {
0603d891 4212 ret = wl1271_acx_set_ht_information(wl, wlvif,
0b932ab9
AN
4213 bss_conf->ht_operation_mode);
4214 if (ret < 0) {
4215 wl1271_warning("Set ht information failed %d", ret);
4216 goto out;
4217 }
4218 }
4219
e78a287a
AN
4220out:
4221 return;
4222}
8bf29b0e 4223
3230f35e
EP
4224static int wlcore_set_bssid(struct wl1271 *wl, struct wl12xx_vif *wlvif,
4225 struct ieee80211_bss_conf *bss_conf,
4226 u32 sta_rate_set)
4227{
4228 u32 rates;
4229 int ret;
4230
4231 wl1271_debug(DEBUG_MAC80211,
4232 "changed_bssid: %pM, aid: %d, bcn_int: %d, brates: 0x%x sta_rate_set: 0x%x",
4233 bss_conf->bssid, bss_conf->aid,
4234 bss_conf->beacon_int,
4235 bss_conf->basic_rates, sta_rate_set);
4236
4237 wlvif->beacon_int = bss_conf->beacon_int;
4238 rates = bss_conf->basic_rates;
4239 wlvif->basic_rate_set =
4240 wl1271_tx_enabled_rates_get(wl, rates,
4241 wlvif->band);
4242 wlvif->basic_rate =
4243 wl1271_tx_min_rate_get(wl,
4244 wlvif->basic_rate_set);
4245
4246 if (sta_rate_set)
4247 wlvif->rate_set =
4248 wl1271_tx_enabled_rates_get(wl,
4249 sta_rate_set,
4250 wlvif->band);
4251
4252 /* we only support sched_scan while not connected */
10199756 4253 if (wl->sched_vif == wlvif)
78e28062 4254 wl->ops->sched_scan_stop(wl, wlvif);
3230f35e
EP
4255
4256 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
4257 if (ret < 0)
4258 return ret;
4259
4260 ret = wl12xx_cmd_build_null_data(wl, wlvif);
4261 if (ret < 0)
4262 return ret;
4263
4264 ret = wl1271_build_qos_null_data(wl, wl12xx_wlvif_to_vif(wlvif));
4265 if (ret < 0)
4266 return ret;
4267
4268 wlcore_set_ssid(wl, wlvif);
4269
4270 set_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
4271
4272 return 0;
4273}
4274
4275static int wlcore_clear_bssid(struct wl1271 *wl, struct wl12xx_vif *wlvif)
4276{
4277 int ret;
4278
4279 /* revert back to minimum rates for the current band */
4280 wl1271_set_band_rate(wl, wlvif);
4281 wlvif->basic_rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
4282
4283 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
4284 if (ret < 0)
4285 return ret;
4286
4287 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
4288 test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags)) {
4289 ret = wl12xx_cmd_role_stop_sta(wl, wlvif);
4290 if (ret < 0)
4291 return ret;
4292 }
4293
4294 clear_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
4295 return 0;
4296}
e78a287a
AN
4297/* STA/IBSS mode changes */
4298static void wl1271_bss_info_changed_sta(struct wl1271 *wl,
4299 struct ieee80211_vif *vif,
4300 struct ieee80211_bss_conf *bss_conf,
4301 u32 changed)
4302{
87fbcb0f 4303 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3230f35e 4304 bool do_join = false;
536129c8 4305 bool is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
227e81e1 4306 bool ibss_joined = false;
72c2d9e5 4307 u32 sta_rate_set = 0;
e78a287a 4308 int ret;
2d6e4e76 4309 struct ieee80211_sta *sta;
a100885d
AN
4310 bool sta_exists = false;
4311 struct ieee80211_sta_ht_cap sta_ht_cap;
e78a287a
AN
4312
4313 if (is_ibss) {
4314 ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf,
4315 changed);
4316 if (ret < 0)
4317 goto out;
e0d8bbf0
JO
4318 }
4319
227e81e1
EP
4320 if (changed & BSS_CHANGED_IBSS) {
4321 if (bss_conf->ibss_joined) {
eee514e3 4322 set_bit(WLVIF_FLAG_IBSS_JOINED, &wlvif->flags);
227e81e1
EP
4323 ibss_joined = true;
4324 } else {
3230f35e
EP
4325 wlcore_unset_assoc(wl, wlvif);
4326 wl12xx_cmd_role_stop_sta(wl, wlvif);
227e81e1
EP
4327 }
4328 }
4329
4330 if ((changed & BSS_CHANGED_BEACON_INT) && ibss_joined)
e78a287a
AN
4331 do_join = true;
4332
4333 /* Need to update the SSID (for filtering etc) */
227e81e1 4334 if ((changed & BSS_CHANGED_BEACON) && ibss_joined)
e78a287a
AN
4335 do_join = true;
4336
227e81e1 4337 if ((changed & BSS_CHANGED_BEACON_ENABLED) && ibss_joined) {
5da11dcd
JO
4338 wl1271_debug(DEBUG_ADHOC, "ad-hoc beaconing: %s",
4339 bss_conf->enable_beacon ? "enabled" : "disabled");
4340
5da11dcd
JO
4341 do_join = true;
4342 }
4343
b0ed8a4d
AN
4344 if (changed & BSS_CHANGED_IDLE && !is_ibss)
4345 wl1271_sta_handle_idle(wl, wlvif, bss_conf->idle);
4346
48af2eb0 4347 if (changed & BSS_CHANGED_CQM) {
00236aed
JO
4348 bool enable = false;
4349 if (bss_conf->cqm_rssi_thold)
4350 enable = true;
0603d891 4351 ret = wl1271_acx_rssi_snr_trigger(wl, wlvif, enable,
00236aed
JO
4352 bss_conf->cqm_rssi_thold,
4353 bss_conf->cqm_rssi_hyst);
4354 if (ret < 0)
4355 goto out;
04324d99 4356 wlvif->rssi_thold = bss_conf->cqm_rssi_thold;
00236aed
JO
4357 }
4358
ec87011a
EP
4359 if (changed & (BSS_CHANGED_BSSID | BSS_CHANGED_HT |
4360 BSS_CHANGED_ASSOC)) {
0f9c8250
AN
4361 rcu_read_lock();
4362 sta = ieee80211_find_sta(vif, bss_conf->bssid);
ef08d028
LC
4363 if (sta) {
4364 u8 *rx_mask = sta->ht_cap.mcs.rx_mask;
4365
4366 /* save the supp_rates of the ap */
4367 sta_rate_set = sta->supp_rates[wlvif->band];
4368 if (sta->ht_cap.ht_supported)
4369 sta_rate_set |=
4370 (rx_mask[0] << HW_HT_RATES_OFFSET) |
4371 (rx_mask[1] << HW_MIMO_RATES_OFFSET);
4372 sta_ht_cap = sta->ht_cap;
4373 sta_exists = true;
4374 }
4375
0f9c8250 4376 rcu_read_unlock();
72c2d9e5 4377 }
72c2d9e5 4378
3230f35e
EP
4379 if (changed & BSS_CHANGED_BSSID) {
4380 if (!is_zero_ether_addr(bss_conf->bssid)) {
4381 ret = wlcore_set_bssid(wl, wlvif, bss_conf,
4382 sta_rate_set);
f5fc0f86 4383 if (ret < 0)
e78a287a 4384 goto out;
f5fc0f86 4385
3230f35e
EP
4386 /* Need to update the BSSID (for filtering etc) */
4387 do_join = true;
d94cd297 4388 } else {
3230f35e 4389 ret = wlcore_clear_bssid(wl, wlvif);
6ccbb92e 4390 if (ret < 0)
e78a287a 4391 goto out;
f5fc0f86 4392 }
f5fc0f86
LC
4393 }
4394
d192d268
EP
4395 if (changed & BSS_CHANGED_IBSS) {
4396 wl1271_debug(DEBUG_ADHOC, "ibss_joined: %d",
4397 bss_conf->ibss_joined);
4398
4399 if (bss_conf->ibss_joined) {
4400 u32 rates = bss_conf->basic_rates;
87fbcb0f 4401 wlvif->basic_rate_set =
af7fbb28 4402 wl1271_tx_enabled_rates_get(wl, rates,
1b92f15e 4403 wlvif->band);
d2d66c56 4404 wlvif->basic_rate =
87fbcb0f
EP
4405 wl1271_tx_min_rate_get(wl,
4406 wlvif->basic_rate_set);
d192d268 4407
06b660e1 4408 /* by default, use 11b + OFDM rates */
30d0c8fd
EP
4409 wlvif->rate_set = CONF_TX_IBSS_DEFAULT_RATES;
4410 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
d192d268
EP
4411 if (ret < 0)
4412 goto out;
4413 }
4414 }
4415
d881fa2c
EP
4416 if ((changed & BSS_CHANGED_BEACON_INFO) && bss_conf->dtim_period) {
4417 /* enable beacon filtering */
4418 ret = wl1271_acx_beacon_filter_opt(wl, wlvif, true);
4419 if (ret < 0)
4420 goto out;
4421 }
4422
0603d891 4423 ret = wl1271_bss_erp_info_changed(wl, vif, bss_conf, changed);
e78a287a
AN
4424 if (ret < 0)
4425 goto out;
f5fc0f86 4426
8bf29b0e 4427 if (do_join) {
3230f35e 4428 ret = wlcore_join(wl, wlvif);
8bf29b0e
JO
4429 if (ret < 0) {
4430 wl1271_warning("cmd join failed %d", ret);
e78a287a 4431 goto out;
8bf29b0e 4432 }
3230f35e 4433 }
251c177f 4434
3230f35e
EP
4435 if (changed & BSS_CHANGED_ASSOC) {
4436 if (bss_conf->assoc) {
ec87011a
EP
4437 ret = wlcore_set_assoc(wl, wlvif, bss_conf,
4438 sta_rate_set);
251c177f
EP
4439 if (ret < 0)
4440 goto out;
4441
9fd6f21b
EP
4442 if (test_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags))
4443 wl12xx_set_authorized(wl, wlvif);
3230f35e
EP
4444 } else {
4445 wlcore_unset_assoc(wl, wlvif);
251c177f 4446 }
c1899554
JO
4447 }
4448
518b680a
EP
4449 if (changed & BSS_CHANGED_PS) {
4450 if ((bss_conf->ps) &&
4451 test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) &&
4452 !test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags)) {
4453 int ps_mode;
4454 char *ps_mode_str;
4455
4456 if (wl->conf.conn.forced_ps) {
4457 ps_mode = STATION_POWER_SAVE_MODE;
4458 ps_mode_str = "forced";
4459 } else {
4460 ps_mode = STATION_AUTO_PS_MODE;
4461 ps_mode_str = "auto";
4462 }
4463
4464 wl1271_debug(DEBUG_PSM, "%s ps enabled", ps_mode_str);
4465
4466 ret = wl1271_ps_set_mode(wl, wlvif, ps_mode);
251c177f 4467 if (ret < 0)
518b680a
EP
4468 wl1271_warning("enter %s ps failed %d",
4469 ps_mode_str, ret);
4470 } else if (!bss_conf->ps &&
4471 test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags)) {
4472 wl1271_debug(DEBUG_PSM, "auto ps disabled");
4473
4474 ret = wl1271_ps_set_mode(wl, wlvif,
4475 STATION_ACTIVE_MODE);
4476 if (ret < 0)
4477 wl1271_warning("exit auto ps failed %d", ret);
251c177f 4478 }
c1899554
JO
4479 }
4480
0b932ab9 4481 /* Handle new association with HT. Do this after join. */
6f0b1bb2 4482 if (sta_exists) {
58321b29 4483 bool enabled =
aaabee8b 4484 bss_conf->chandef.width != NL80211_CHAN_WIDTH_20_NOHT;
58321b29 4485
530abe19
EP
4486 ret = wlcore_hw_set_peer_cap(wl,
4487 &sta_ht_cap,
4488 enabled,
4489 wlvif->rate_set,
4490 wlvif->sta.hlid);
58321b29
EP
4491 if (ret < 0) {
4492 wl1271_warning("Set ht cap failed %d", ret);
4493 goto out;
4494
0f9c8250 4495 }
58321b29
EP
4496
4497 if (enabled) {
4498 ret = wl1271_acx_set_ht_information(wl, wlvif,
4499 bss_conf->ht_operation_mode);
0f9c8250 4500 if (ret < 0) {
58321b29 4501 wl1271_warning("Set ht information failed %d",
0f9c8250
AN
4502 ret);
4503 goto out;
4504 }
4505 }
4506 }
4507
76a74c8a
EP
4508 /* Handle arp filtering. Done after join. */
4509 if ((changed & BSS_CHANGED_ARP_FILTER) ||
4510 (!is_ibss && (changed & BSS_CHANGED_QOS))) {
4511 __be32 addr = bss_conf->arp_addr_list[0];
4512 wlvif->sta.qos = bss_conf->qos;
4513 WARN_ON(wlvif->bss_type != BSS_TYPE_STA_BSS);
4514
0f19b41e 4515 if (bss_conf->arp_addr_cnt == 1 && bss_conf->assoc) {
76a74c8a
EP
4516 wlvif->ip_addr = addr;
4517 /*
4518 * The template should have been configured only upon
4519 * association. however, it seems that the correct ip
4520 * isn't being set (when sending), so we have to
4521 * reconfigure the template upon every ip change.
4522 */
4523 ret = wl1271_cmd_build_arp_rsp(wl, wlvif);
4524 if (ret < 0) {
4525 wl1271_warning("build arp rsp failed: %d", ret);
4526 goto out;
4527 }
4528
4529 ret = wl1271_acx_arp_ip_filter(wl, wlvif,
4530 (ACX_ARP_FILTER_ARP_FILTERING |
4531 ACX_ARP_FILTER_AUTO_ARP),
4532 addr);
4533 } else {
4534 wlvif->ip_addr = 0;
4535 ret = wl1271_acx_arp_ip_filter(wl, wlvif, 0, addr);
4536 }
4537
4538 if (ret < 0)
4539 goto out;
4540 }
4541
e78a287a
AN
4542out:
4543 return;
4544}
4545
4546static void wl1271_op_bss_info_changed(struct ieee80211_hw *hw,
4547 struct ieee80211_vif *vif,
4548 struct ieee80211_bss_conf *bss_conf,
4549 u32 changed)
4550{
4551 struct wl1271 *wl = hw->priv;
536129c8
EP
4552 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4553 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
e78a287a
AN
4554 int ret;
4555
d3f5a1b5
EP
4556 wl1271_debug(DEBUG_MAC80211, "mac80211 bss info role %d changed 0x%x",
4557 wlvif->role_id, (int)changed);
e78a287a 4558
6b8bf5bc
AN
4559 /*
4560 * make sure to cancel pending disconnections if our association
4561 * state changed
4562 */
4563 if (!is_ap && (changed & BSS_CHANGED_ASSOC))
c50a2825 4564 cancel_delayed_work_sync(&wlvif->connection_loss_work);
6b8bf5bc 4565
b515d83a
EP
4566 if (is_ap && (changed & BSS_CHANGED_BEACON_ENABLED) &&
4567 !bss_conf->enable_beacon)
4568 wl1271_tx_flush(wl);
4569
e78a287a
AN
4570 mutex_lock(&wl->mutex);
4571
4cc53383 4572 if (unlikely(wl->state != WLCORE_STATE_ON))
e78a287a
AN
4573 goto out;
4574
10c8cd01
EP
4575 if (unlikely(!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)))
4576 goto out;
4577
a620865e 4578 ret = wl1271_ps_elp_wakeup(wl);
e78a287a
AN
4579 if (ret < 0)
4580 goto out;
4581
b30d49b2
AG
4582 if ((changed & BSS_CHANGED_TXPOWER) &&
4583 bss_conf->txpower != wlvif->power_level) {
4584
4585 ret = wl1271_acx_tx_power(wl, wlvif, bss_conf->txpower);
4586 if (ret < 0)
4587 goto out;
4588
4589 wlvif->power_level = bss_conf->txpower;
4590 }
4591
e78a287a
AN
4592 if (is_ap)
4593 wl1271_bss_info_changed_ap(wl, vif, bss_conf, changed);
4594 else
4595 wl1271_bss_info_changed_sta(wl, vif, bss_conf, changed);
4596
f5fc0f86
LC
4597 wl1271_ps_elp_sleep(wl);
4598
4599out:
4600 mutex_unlock(&wl->mutex);
4601}
4602
b6970ee5
EP
4603static int wlcore_op_add_chanctx(struct ieee80211_hw *hw,
4604 struct ieee80211_chanctx_conf *ctx)
4605{
4606 wl1271_debug(DEBUG_MAC80211, "mac80211 add chanctx %d (type %d)",
aaabee8b
LC
4607 ieee80211_frequency_to_channel(ctx->def.chan->center_freq),
4608 cfg80211_get_chandef_type(&ctx->def));
b6970ee5
EP
4609 return 0;
4610}
4611
4612static void wlcore_op_remove_chanctx(struct ieee80211_hw *hw,
4613 struct ieee80211_chanctx_conf *ctx)
4614{
4615 wl1271_debug(DEBUG_MAC80211, "mac80211 remove chanctx %d (type %d)",
aaabee8b
LC
4616 ieee80211_frequency_to_channel(ctx->def.chan->center_freq),
4617 cfg80211_get_chandef_type(&ctx->def));
b6970ee5
EP
4618}
4619
4620static void wlcore_op_change_chanctx(struct ieee80211_hw *hw,
4621 struct ieee80211_chanctx_conf *ctx,
4622 u32 changed)
4623{
750e9d15
EP
4624 struct wl1271 *wl = hw->priv;
4625 struct wl12xx_vif *wlvif;
4626 int ret;
4627 int channel = ieee80211_frequency_to_channel(
4628 ctx->def.chan->center_freq);
4629
b6970ee5
EP
4630 wl1271_debug(DEBUG_MAC80211,
4631 "mac80211 change chanctx %d (type %d) changed 0x%x",
750e9d15
EP
4632 channel, cfg80211_get_chandef_type(&ctx->def), changed);
4633
4634 mutex_lock(&wl->mutex);
4635
4636 ret = wl1271_ps_elp_wakeup(wl);
4637 if (ret < 0)
4638 goto out;
4639
4640 wl12xx_for_each_wlvif(wl, wlvif) {
4641 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
4642
4643 rcu_read_lock();
4644 if (rcu_access_pointer(vif->chanctx_conf) != ctx) {
4645 rcu_read_unlock();
4646 continue;
4647 }
4648 rcu_read_unlock();
4649
4650 /* start radar if needed */
4651 if (changed & IEEE80211_CHANCTX_CHANGE_RADAR &&
4652 wlvif->bss_type == BSS_TYPE_AP_BSS &&
4653 ctx->radar_enabled && !wlvif->radar_enabled &&
4654 ctx->def.chan->dfs_state == NL80211_DFS_USABLE) {
4655 wl1271_debug(DEBUG_MAC80211, "Start radar detection");
4656 wlcore_hw_set_cac(wl, wlvif, true);
4657 wlvif->radar_enabled = true;
4658 }
4659 }
4660
4661 wl1271_ps_elp_sleep(wl);
4662out:
4663 mutex_unlock(&wl->mutex);
b6970ee5
EP
4664}
4665
4666static int wlcore_op_assign_vif_chanctx(struct ieee80211_hw *hw,
4667 struct ieee80211_vif *vif,
4668 struct ieee80211_chanctx_conf *ctx)
4669{
4670 struct wl1271 *wl = hw->priv;
4671 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4672 int channel = ieee80211_frequency_to_channel(
aaabee8b 4673 ctx->def.chan->center_freq);
750e9d15 4674 int ret = -EINVAL;
b6970ee5
EP
4675
4676 wl1271_debug(DEBUG_MAC80211,
750e9d15
EP
4677 "mac80211 assign chanctx (role %d) %d (type %d) (radar %d dfs_state %d)",
4678 wlvif->role_id, channel,
4679 cfg80211_get_chandef_type(&ctx->def),
4680 ctx->radar_enabled, ctx->def.chan->dfs_state);
b6970ee5
EP
4681
4682 mutex_lock(&wl->mutex);
4683
750e9d15
EP
4684 if (unlikely(wl->state != WLCORE_STATE_ON))
4685 goto out;
4686
4687 if (unlikely(!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)))
4688 goto out;
4689
4690 ret = wl1271_ps_elp_wakeup(wl);
4691 if (ret < 0)
4692 goto out;
4693
aaabee8b 4694 wlvif->band = ctx->def.chan->band;
b6970ee5 4695 wlvif->channel = channel;
aaabee8b 4696 wlvif->channel_type = cfg80211_get_chandef_type(&ctx->def);
b6970ee5
EP
4697
4698 /* update default rates according to the band */
4699 wl1271_set_band_rate(wl, wlvif);
4700
750e9d15
EP
4701 if (ctx->radar_enabled &&
4702 ctx->def.chan->dfs_state == NL80211_DFS_USABLE) {
4703 wl1271_debug(DEBUG_MAC80211, "Start radar detection");
4704 wlcore_hw_set_cac(wl, wlvif, true);
4705 wlvif->radar_enabled = true;
4706 }
4707
4708 wl1271_ps_elp_sleep(wl);
4709out:
b6970ee5
EP
4710 mutex_unlock(&wl->mutex);
4711
4712 return 0;
4713}
4714
4715static void wlcore_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
4716 struct ieee80211_vif *vif,
4717 struct ieee80211_chanctx_conf *ctx)
4718{
4719 struct wl1271 *wl = hw->priv;
4720 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
750e9d15 4721 int ret;
b6970ee5
EP
4722
4723 wl1271_debug(DEBUG_MAC80211,
4724 "mac80211 unassign chanctx (role %d) %d (type %d)",
4725 wlvif->role_id,
aaabee8b
LC
4726 ieee80211_frequency_to_channel(ctx->def.chan->center_freq),
4727 cfg80211_get_chandef_type(&ctx->def));
b6970ee5
EP
4728
4729 wl1271_tx_flush(wl);
750e9d15
EP
4730
4731 mutex_lock(&wl->mutex);
4732
4733 if (unlikely(wl->state != WLCORE_STATE_ON))
4734 goto out;
4735
4736 if (unlikely(!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)))
4737 goto out;
4738
4739 ret = wl1271_ps_elp_wakeup(wl);
4740 if (ret < 0)
4741 goto out;
4742
4743 if (wlvif->radar_enabled) {
4744 wl1271_debug(DEBUG_MAC80211, "Stop radar detection");
4745 wlcore_hw_set_cac(wl, wlvif, false);
4746 wlvif->radar_enabled = false;
4747 }
4748
4749 wl1271_ps_elp_sleep(wl);
4750out:
4751 mutex_unlock(&wl->mutex);
4752}
4753
4754static int __wlcore_switch_vif_chan(struct wl1271 *wl,
4755 struct wl12xx_vif *wlvif,
4756 struct ieee80211_chanctx_conf *new_ctx)
4757{
4758 int channel = ieee80211_frequency_to_channel(
4759 new_ctx->def.chan->center_freq);
4760
4761 wl1271_debug(DEBUG_MAC80211,
4762 "switch vif (role %d) %d -> %d chan_type: %d",
4763 wlvif->role_id, wlvif->channel, channel,
4764 cfg80211_get_chandef_type(&new_ctx->def));
4765
4766 if (WARN_ON_ONCE(wlvif->bss_type != BSS_TYPE_AP_BSS))
4767 return 0;
4768
830513ab
EP
4769 WARN_ON(!test_bit(WLVIF_FLAG_BEACON_DISABLED, &wlvif->flags));
4770
750e9d15
EP
4771 if (wlvif->radar_enabled) {
4772 wl1271_debug(DEBUG_MAC80211, "Stop radar detection");
4773 wlcore_hw_set_cac(wl, wlvif, false);
4774 wlvif->radar_enabled = false;
4775 }
4776
4777 wlvif->band = new_ctx->def.chan->band;
4778 wlvif->channel = channel;
4779 wlvif->channel_type = cfg80211_get_chandef_type(&new_ctx->def);
4780
4781 /* start radar if needed */
4782 if (new_ctx->radar_enabled) {
4783 wl1271_debug(DEBUG_MAC80211, "Start radar detection");
4784 wlcore_hw_set_cac(wl, wlvif, true);
4785 wlvif->radar_enabled = true;
4786 }
4787
4788 return 0;
4789}
4790
4791static int
4792wlcore_op_switch_vif_chanctx(struct ieee80211_hw *hw,
4793 struct ieee80211_vif_chanctx_switch *vifs,
4794 int n_vifs,
4795 enum ieee80211_chanctx_switch_mode mode)
4796{
4797 struct wl1271 *wl = hw->priv;
4798 int i, ret;
4799
4800 wl1271_debug(DEBUG_MAC80211,
4801 "mac80211 switch chanctx n_vifs %d mode %d",
4802 n_vifs, mode);
4803
4804 mutex_lock(&wl->mutex);
4805
4806 ret = wl1271_ps_elp_wakeup(wl);
4807 if (ret < 0)
4808 goto out;
4809
4810 for (i = 0; i < n_vifs; i++) {
4811 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vifs[i].vif);
4812
4813 ret = __wlcore_switch_vif_chan(wl, wlvif, vifs[i].new_ctx);
4814 if (ret)
4815 goto out_sleep;
4816 }
4817out_sleep:
4818 wl1271_ps_elp_sleep(wl);
4819out:
4820 mutex_unlock(&wl->mutex);
4821
4822 return 0;
b6970ee5
EP
4823}
4824
8a3a3c85
EP
4825static int wl1271_op_conf_tx(struct ieee80211_hw *hw,
4826 struct ieee80211_vif *vif, u16 queue,
c6999d83
KV
4827 const struct ieee80211_tx_queue_params *params)
4828{
4829 struct wl1271 *wl = hw->priv;
0603d891 4830 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4695dc91 4831 u8 ps_scheme;
488fc540 4832 int ret = 0;
c6999d83
KV
4833
4834 mutex_lock(&wl->mutex);
4835
4836 wl1271_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
4837
4695dc91
KV
4838 if (params->uapsd)
4839 ps_scheme = CONF_PS_SCHEME_UPSD_TRIGGER;
4840 else
4841 ps_scheme = CONF_PS_SCHEME_LEGACY;
4842
5b37ddfe 4843 if (!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
c1b193eb 4844 goto out;
488fc540 4845
c1b193eb
EP
4846 ret = wl1271_ps_elp_wakeup(wl);
4847 if (ret < 0)
4848 goto out;
488fc540 4849
c1b193eb
EP
4850 /*
4851 * the txop is confed in units of 32us by the mac80211,
4852 * we need us
4853 */
0603d891 4854 ret = wl1271_acx_ac_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
c1b193eb
EP
4855 params->cw_min, params->cw_max,
4856 params->aifs, params->txop << 5);
4857 if (ret < 0)
4858 goto out_sleep;
4859
0603d891 4860 ret = wl1271_acx_tid_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
c1b193eb
EP
4861 CONF_CHANNEL_TYPE_EDCF,
4862 wl1271_tx_get_queue(queue),
4863 ps_scheme, CONF_ACK_POLICY_LEGACY,
4864 0, 0);
c82c1dde
KV
4865
4866out_sleep:
c1b193eb 4867 wl1271_ps_elp_sleep(wl);
c6999d83
KV
4868
4869out:
4870 mutex_unlock(&wl->mutex);
4871
4872 return ret;
4873}
4874
37a41b4a
EP
4875static u64 wl1271_op_get_tsf(struct ieee80211_hw *hw,
4876 struct ieee80211_vif *vif)
bbbb538e
JO
4877{
4878
4879 struct wl1271 *wl = hw->priv;
9c531149 4880 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
bbbb538e
JO
4881 u64 mactime = ULLONG_MAX;
4882 int ret;
4883
4884 wl1271_debug(DEBUG_MAC80211, "mac80211 get tsf");
4885
4886 mutex_lock(&wl->mutex);
4887
4cc53383 4888 if (unlikely(wl->state != WLCORE_STATE_ON))
f8d9802f
JO
4889 goto out;
4890
a620865e 4891 ret = wl1271_ps_elp_wakeup(wl);
bbbb538e
JO
4892 if (ret < 0)
4893 goto out;
4894
9c531149 4895 ret = wl12xx_acx_tsf_info(wl, wlvif, &mactime);
bbbb538e
JO
4896 if (ret < 0)
4897 goto out_sleep;
4898
4899out_sleep:
4900 wl1271_ps_elp_sleep(wl);
4901
4902out:
4903 mutex_unlock(&wl->mutex);
4904 return mactime;
4905}
f5fc0f86 4906
ece550d0
JL
4907static int wl1271_op_get_survey(struct ieee80211_hw *hw, int idx,
4908 struct survey_info *survey)
4909{
ece550d0 4910 struct ieee80211_conf *conf = &hw->conf;
b739a42c 4911
ece550d0
JL
4912 if (idx != 0)
4913 return -ENOENT;
b739a42c 4914
675a0b04 4915 survey->channel = conf->chandef.chan;
add779a0 4916 survey->filled = 0;
ece550d0
JL
4917 return 0;
4918}
4919
409622ec 4920static int wl1271_allocate_sta(struct wl1271 *wl,
c7ffb902
EP
4921 struct wl12xx_vif *wlvif,
4922 struct ieee80211_sta *sta)
f84f7d78
AN
4923{
4924 struct wl1271_station *wl_sta;
c7ffb902 4925 int ret;
f84f7d78 4926
c7ffb902 4927
32f0fd5b 4928 if (wl->active_sta_count >= wl->max_ap_stations) {
f84f7d78
AN
4929 wl1271_warning("could not allocate HLID - too much stations");
4930 return -EBUSY;
4931 }
4932
4933 wl_sta = (struct wl1271_station *)sta->drv_priv;
c7ffb902
EP
4934 ret = wl12xx_allocate_link(wl, wlvif, &wl_sta->hlid);
4935 if (ret < 0) {
4936 wl1271_warning("could not allocate HLID - too many links");
4937 return -EBUSY;
4938 }
4939
0e752df6
AN
4940 /* use the previous security seq, if this is a recovery/resume */
4941 wl->links[wl_sta->hlid].total_freed_pkts = wl_sta->total_freed_pkts;
4942
c7ffb902 4943 set_bit(wl_sta->hlid, wlvif->ap.sta_hlid_map);
b622d992 4944 memcpy(wl->links[wl_sta->hlid].addr, sta->addr, ETH_ALEN);
da03209e 4945 wl->active_sta_count++;
f84f7d78
AN
4946 return 0;
4947}
4948
c7ffb902 4949void wl1271_free_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 hlid)
f84f7d78 4950{
c7ffb902 4951 if (!test_bit(hlid, wlvif->ap.sta_hlid_map))
f1acea9a
AN
4952 return;
4953
c7ffb902 4954 clear_bit(hlid, wlvif->ap.sta_hlid_map);
b622d992 4955 __clear_bit(hlid, &wl->ap_ps_map);
5e74b3aa 4956 __clear_bit(hlid, &wl->ap_fw_ps_map);
0e752df6
AN
4957
4958 /*
4959 * save the last used PN in the private part of iee80211_sta,
4960 * in case of recovery/suspend
4961 */
50d26aa3 4962 wlcore_save_freed_pkts_addr(wl, wlvif, hlid, wl->links[hlid].addr);
0e752df6 4963
c7ffb902 4964 wl12xx_free_link(wl, wlvif, &hlid);
da03209e 4965 wl->active_sta_count--;
55df5afb
AN
4966
4967 /*
4968 * rearm the tx watchdog when the last STA is freed - give the FW a
4969 * chance to return STA-buffered packets before complaining.
4970 */
4971 if (wl->active_sta_count == 0)
4972 wl12xx_rearm_tx_watchdog_locked(wl);
f84f7d78
AN
4973}
4974
2d6cf2b5
EP
4975static int wl12xx_sta_add(struct wl1271 *wl,
4976 struct wl12xx_vif *wlvif,
4977 struct ieee80211_sta *sta)
f84f7d78 4978{
c7ffb902 4979 struct wl1271_station *wl_sta;
f84f7d78
AN
4980 int ret = 0;
4981 u8 hlid;
4982
f84f7d78
AN
4983 wl1271_debug(DEBUG_MAC80211, "mac80211 add sta %d", (int)sta->aid);
4984
c7ffb902 4985 ret = wl1271_allocate_sta(wl, wlvif, sta);
f84f7d78 4986 if (ret < 0)
2d6cf2b5 4987 return ret;
f84f7d78 4988
c7ffb902
EP
4989 wl_sta = (struct wl1271_station *)sta->drv_priv;
4990 hlid = wl_sta->hlid;
4991
1b92f15e 4992 ret = wl12xx_cmd_add_peer(wl, wlvif, sta, hlid);
f84f7d78 4993 if (ret < 0)
2d6cf2b5 4994 wl1271_free_sta(wl, wlvif, hlid);
f84f7d78 4995
2d6cf2b5
EP
4996 return ret;
4997}
b67476ef 4998
2d6cf2b5
EP
4999static int wl12xx_sta_remove(struct wl1271 *wl,
5000 struct wl12xx_vif *wlvif,
5001 struct ieee80211_sta *sta)
5002{
5003 struct wl1271_station *wl_sta;
5004 int ret = 0, id;
0b932ab9 5005
2d6cf2b5
EP
5006 wl1271_debug(DEBUG_MAC80211, "mac80211 remove sta %d", (int)sta->aid);
5007
5008 wl_sta = (struct wl1271_station *)sta->drv_priv;
5009 id = wl_sta->hlid;
5010 if (WARN_ON(!test_bit(id, wlvif->ap.sta_hlid_map)))
5011 return -EINVAL;
f84f7d78 5012
028e7243 5013 ret = wl12xx_cmd_remove_peer(wl, wlvif, wl_sta->hlid);
409622ec 5014 if (ret < 0)
2d6cf2b5 5015 return ret;
409622ec 5016
2d6cf2b5 5017 wl1271_free_sta(wl, wlvif, wl_sta->hlid);
f84f7d78
AN
5018 return ret;
5019}
5020
426001a6
EP
5021static void wlcore_roc_if_possible(struct wl1271 *wl,
5022 struct wl12xx_vif *wlvif)
5023{
5024 if (find_first_bit(wl->roc_map,
5025 WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES)
5026 return;
5027
5028 if (WARN_ON(wlvif->role_id == WL12XX_INVALID_ROLE_ID))
5029 return;
5030
5031 wl12xx_roc(wl, wlvif, wlvif->role_id, wlvif->band, wlvif->channel);
5032}
5033
187e52cc
AN
5034/*
5035 * when wl_sta is NULL, we treat this call as if coming from a
5036 * pending auth reply.
5037 * wl->mutex must be taken and the FW must be awake when the call
5038 * takes place.
5039 */
5040void wlcore_update_inconn_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif,
5041 struct wl1271_station *wl_sta, bool in_conn)
426001a6 5042{
187e52cc
AN
5043 if (in_conn) {
5044 if (WARN_ON(wl_sta && wl_sta->in_connection))
426001a6 5045 return;
187e52cc
AN
5046
5047 if (!wlvif->ap_pending_auth_reply &&
5048 !wlvif->inconn_count)
426001a6 5049 wlcore_roc_if_possible(wl, wlvif);
187e52cc
AN
5050
5051 if (wl_sta) {
5052 wl_sta->in_connection = true;
5053 wlvif->inconn_count++;
5054 } else {
5055 wlvif->ap_pending_auth_reply = true;
5056 }
426001a6 5057 } else {
187e52cc 5058 if (wl_sta && !wl_sta->in_connection)
426001a6
EP
5059 return;
5060
187e52cc 5061 if (WARN_ON(!wl_sta && !wlvif->ap_pending_auth_reply))
426001a6
EP
5062 return;
5063
187e52cc
AN
5064 if (WARN_ON(wl_sta && !wlvif->inconn_count))
5065 return;
5066
5067 if (wl_sta) {
5068 wl_sta->in_connection = false;
5069 wlvif->inconn_count--;
5070 } else {
5071 wlvif->ap_pending_auth_reply = false;
5072 }
5073
5074 if (!wlvif->inconn_count && !wlvif->ap_pending_auth_reply &&
5075 test_bit(wlvif->role_id, wl->roc_map))
5076 wl12xx_croc(wl, wlvif->role_id);
426001a6
EP
5077 }
5078}
5079
2d6cf2b5
EP
5080static int wl12xx_update_sta_state(struct wl1271 *wl,
5081 struct wl12xx_vif *wlvif,
5082 struct ieee80211_sta *sta,
5083 enum ieee80211_sta_state old_state,
5084 enum ieee80211_sta_state new_state)
f84f7d78 5085{
f84f7d78 5086 struct wl1271_station *wl_sta;
2d6cf2b5
EP
5087 bool is_ap = wlvif->bss_type == BSS_TYPE_AP_BSS;
5088 bool is_sta = wlvif->bss_type == BSS_TYPE_STA_BSS;
5089 int ret;
f84f7d78 5090
2d6cf2b5 5091 wl_sta = (struct wl1271_station *)sta->drv_priv;
f84f7d78 5092
2d6cf2b5
EP
5093 /* Add station (AP mode) */
5094 if (is_ap &&
5095 old_state == IEEE80211_STA_NOTEXIST &&
29936266
EP
5096 new_state == IEEE80211_STA_NONE) {
5097 ret = wl12xx_sta_add(wl, wlvif, sta);
5098 if (ret)
5099 return ret;
426001a6
EP
5100
5101 wlcore_update_inconn_sta(wl, wlvif, wl_sta, true);
29936266 5102 }
2d6cf2b5
EP
5103
5104 /* Remove station (AP mode) */
5105 if (is_ap &&
5106 old_state == IEEE80211_STA_NONE &&
5107 new_state == IEEE80211_STA_NOTEXIST) {
5108 /* must not fail */
5109 wl12xx_sta_remove(wl, wlvif, sta);
426001a6
EP
5110
5111 wlcore_update_inconn_sta(wl, wlvif, wl_sta, false);
2d6cf2b5 5112 }
f84f7d78 5113
2d6cf2b5
EP
5114 /* Authorize station (AP mode) */
5115 if (is_ap &&
5116 new_state == IEEE80211_STA_AUTHORIZED) {
2fec3d27 5117 ret = wl12xx_cmd_set_peer_state(wl, wlvif, wl_sta->hlid);
2d6cf2b5
EP
5118 if (ret < 0)
5119 return ret;
f84f7d78 5120
2d6cf2b5 5121 ret = wl1271_acx_set_ht_capabilities(wl, &sta->ht_cap, true,
2fec3d27 5122 wl_sta->hlid);
29936266
EP
5123 if (ret)
5124 return ret;
426001a6
EP
5125
5126 wlcore_update_inconn_sta(wl, wlvif, wl_sta, false);
2d6cf2b5 5127 }
f84f7d78 5128
9fd6f21b
EP
5129 /* Authorize station */
5130 if (is_sta &&
5131 new_state == IEEE80211_STA_AUTHORIZED) {
5132 set_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags);
29936266
EP
5133 ret = wl12xx_set_authorized(wl, wlvif);
5134 if (ret)
5135 return ret;
9fd6f21b
EP
5136 }
5137
5138 if (is_sta &&
5139 old_state == IEEE80211_STA_AUTHORIZED &&
5140 new_state == IEEE80211_STA_ASSOC) {
5141 clear_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags);
3230f35e 5142 clear_bit(WLVIF_FLAG_STA_STATE_SENT, &wlvif->flags);
9fd6f21b
EP
5143 }
5144
50d26aa3
EP
5145 /* save seq number on disassoc (suspend) */
5146 if (is_sta &&
5147 old_state == IEEE80211_STA_ASSOC &&
5148 new_state == IEEE80211_STA_AUTH) {
5149 wlcore_save_freed_pkts(wl, wlvif, wlvif->sta.hlid, sta);
5150 wlvif->total_freed_pkts = 0;
5151 }
5152
5153 /* restore seq number on assoc (resume) */
5154 if (is_sta &&
5155 old_state == IEEE80211_STA_AUTH &&
5156 new_state == IEEE80211_STA_ASSOC) {
5157 wlvif->total_freed_pkts = wl_sta->total_freed_pkts;
5158 }
5159
29936266
EP
5160 /* clear ROCs on failure or authorization */
5161 if (is_sta &&
5162 (new_state == IEEE80211_STA_AUTHORIZED ||
5163 new_state == IEEE80211_STA_NOTEXIST)) {
5164 if (test_bit(wlvif->role_id, wl->roc_map))
5165 wl12xx_croc(wl, wlvif->role_id);
9fd6f21b
EP
5166 }
5167
29936266
EP
5168 if (is_sta &&
5169 old_state == IEEE80211_STA_NOTEXIST &&
5170 new_state == IEEE80211_STA_NONE) {
5171 if (find_first_bit(wl->roc_map,
5172 WL12XX_MAX_ROLES) >= WL12XX_MAX_ROLES) {
5173 WARN_ON(wlvif->role_id == WL12XX_INVALID_ROLE_ID);
5174 wl12xx_roc(wl, wlvif, wlvif->role_id,
5175 wlvif->band, wlvif->channel);
5176 }
5177 }
2d6cf2b5
EP
5178 return 0;
5179}
5180
5181static int wl12xx_op_sta_state(struct ieee80211_hw *hw,
5182 struct ieee80211_vif *vif,
5183 struct ieee80211_sta *sta,
5184 enum ieee80211_sta_state old_state,
5185 enum ieee80211_sta_state new_state)
5186{
5187 struct wl1271 *wl = hw->priv;
5188 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5189 int ret;
5190
5191 wl1271_debug(DEBUG_MAC80211, "mac80211 sta %d state=%d->%d",
5192 sta->aid, old_state, new_state);
5193
5194 mutex_lock(&wl->mutex);
5195
4cc53383 5196 if (unlikely(wl->state != WLCORE_STATE_ON)) {
2d6cf2b5 5197 ret = -EBUSY;
f84f7d78 5198 goto out;
2d6cf2b5 5199 }
f84f7d78 5200
a620865e 5201 ret = wl1271_ps_elp_wakeup(wl);
f84f7d78
AN
5202 if (ret < 0)
5203 goto out;
5204
2d6cf2b5 5205 ret = wl12xx_update_sta_state(wl, wlvif, sta, old_state, new_state);
f84f7d78 5206
f84f7d78 5207 wl1271_ps_elp_sleep(wl);
f84f7d78
AN
5208out:
5209 mutex_unlock(&wl->mutex);
2d6cf2b5
EP
5210 if (new_state < old_state)
5211 return 0;
f84f7d78
AN
5212 return ret;
5213}
5214
4623ec7d
LC
5215static int wl1271_op_ampdu_action(struct ieee80211_hw *hw,
5216 struct ieee80211_vif *vif,
5217 enum ieee80211_ampdu_mlme_action action,
5218 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
5219 u8 buf_size)
bbba3e68
LS
5220{
5221 struct wl1271 *wl = hw->priv;
536129c8 5222 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
bbba3e68 5223 int ret;
0f9c8250
AN
5224 u8 hlid, *ba_bitmap;
5225
5226 wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu action %d tid %d", action,
5227 tid);
5228
5229 /* sanity check - the fields in FW are only 8bits wide */
5230 if (WARN_ON(tid > 0xFF))
5231 return -ENOTSUPP;
bbba3e68
LS
5232
5233 mutex_lock(&wl->mutex);
5234
4cc53383 5235 if (unlikely(wl->state != WLCORE_STATE_ON)) {
bbba3e68
LS
5236 ret = -EAGAIN;
5237 goto out;
5238 }
5239
536129c8 5240 if (wlvif->bss_type == BSS_TYPE_STA_BSS) {
154da67c 5241 hlid = wlvif->sta.hlid;
536129c8 5242 } else if (wlvif->bss_type == BSS_TYPE_AP_BSS) {
0f9c8250
AN
5243 struct wl1271_station *wl_sta;
5244
5245 wl_sta = (struct wl1271_station *)sta->drv_priv;
5246 hlid = wl_sta->hlid;
0f9c8250
AN
5247 } else {
5248 ret = -EINVAL;
5249 goto out;
5250 }
5251
9ae5d8d4
AN
5252 ba_bitmap = &wl->links[hlid].ba_bitmap;
5253
a620865e 5254 ret = wl1271_ps_elp_wakeup(wl);
bbba3e68
LS
5255 if (ret < 0)
5256 goto out;
5257
70559a06
SL
5258 wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu: Rx tid %d action %d",
5259 tid, action);
5260
bbba3e68
LS
5261 switch (action) {
5262 case IEEE80211_AMPDU_RX_START:
d0802abd 5263 if (!wlvif->ba_support || !wlvif->ba_allowed) {
bbba3e68 5264 ret = -ENOTSUPP;
0f9c8250
AN
5265 break;
5266 }
5267
d21553f8 5268 if (wl->ba_rx_session_count >= wl->ba_rx_session_count_max) {
0f9c8250
AN
5269 ret = -EBUSY;
5270 wl1271_error("exceeded max RX BA sessions");
5271 break;
5272 }
5273
5274 if (*ba_bitmap & BIT(tid)) {
5275 ret = -EINVAL;
5276 wl1271_error("cannot enable RX BA session on active "
5277 "tid: %d", tid);
5278 break;
5279 }
5280
5281 ret = wl12xx_acx_set_ba_receiver_session(wl, tid, *ssn, true,
5282 hlid);
5283 if (!ret) {
5284 *ba_bitmap |= BIT(tid);
5285 wl->ba_rx_session_count++;
bbba3e68
LS
5286 }
5287 break;
5288
5289 case IEEE80211_AMPDU_RX_STOP:
0f9c8250 5290 if (!(*ba_bitmap & BIT(tid))) {
c954910b
AN
5291 /*
5292 * this happens on reconfig - so only output a debug
5293 * message for now, and don't fail the function.
5294 */
5295 wl1271_debug(DEBUG_MAC80211,
5296 "no active RX BA session on tid: %d",
0f9c8250 5297 tid);
c954910b 5298 ret = 0;
0f9c8250
AN
5299 break;
5300 }
5301
5302 ret = wl12xx_acx_set_ba_receiver_session(wl, tid, 0, false,
5303 hlid);
5304 if (!ret) {
5305 *ba_bitmap &= ~BIT(tid);
5306 wl->ba_rx_session_count--;
5307 }
bbba3e68
LS
5308 break;
5309
5310 /*
5311 * The BA initiator session management in FW independently.
5312 * Falling break here on purpose for all TX APDU commands.
5313 */
5314 case IEEE80211_AMPDU_TX_START:
18b559d5
JB
5315 case IEEE80211_AMPDU_TX_STOP_CONT:
5316 case IEEE80211_AMPDU_TX_STOP_FLUSH:
5317 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
bbba3e68
LS
5318 case IEEE80211_AMPDU_TX_OPERATIONAL:
5319 ret = -EINVAL;
5320 break;
5321
5322 default:
5323 wl1271_error("Incorrect ampdu action id=%x\n", action);
5324 ret = -EINVAL;
5325 }
5326
5327 wl1271_ps_elp_sleep(wl);
5328
5329out:
5330 mutex_unlock(&wl->mutex);
5331
5332 return ret;
5333}
5334
af7fbb28
EP
5335static int wl12xx_set_bitrate_mask(struct ieee80211_hw *hw,
5336 struct ieee80211_vif *vif,
5337 const struct cfg80211_bitrate_mask *mask)
5338{
83587505 5339 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
af7fbb28 5340 struct wl1271 *wl = hw->priv;
d6fa37c9 5341 int i, ret = 0;
af7fbb28
EP
5342
5343 wl1271_debug(DEBUG_MAC80211, "mac80211 set_bitrate_mask 0x%x 0x%x",
5344 mask->control[NL80211_BAND_2GHZ].legacy,
5345 mask->control[NL80211_BAND_5GHZ].legacy);
5346
5347 mutex_lock(&wl->mutex);
5348
091185d6 5349 for (i = 0; i < WLCORE_NUM_BANDS; i++)
83587505 5350 wlvif->bitrate_masks[i] =
af7fbb28
EP
5351 wl1271_tx_enabled_rates_get(wl,
5352 mask->control[i].legacy,
5353 i);
d6fa37c9 5354
4cc53383 5355 if (unlikely(wl->state != WLCORE_STATE_ON))
d6fa37c9
EP
5356 goto out;
5357
5358 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
5359 !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
5360
5361 ret = wl1271_ps_elp_wakeup(wl);
5362 if (ret < 0)
5363 goto out;
5364
5365 wl1271_set_band_rate(wl, wlvif);
5366 wlvif->basic_rate =
5367 wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
5368 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
5369
5370 wl1271_ps_elp_sleep(wl);
5371 }
5372out:
af7fbb28
EP
5373 mutex_unlock(&wl->mutex);
5374
d6fa37c9 5375 return ret;
af7fbb28
EP
5376}
5377
6d158ff3 5378static void wl12xx_op_channel_switch(struct ieee80211_hw *hw,
0f791eb4 5379 struct ieee80211_vif *vif,
6d158ff3
SL
5380 struct ieee80211_channel_switch *ch_switch)
5381{
5382 struct wl1271 *wl = hw->priv;
0f791eb4 5383 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
6d158ff3
SL
5384 int ret;
5385
5386 wl1271_debug(DEBUG_MAC80211, "mac80211 channel switch");
5387
b9239b66
AN
5388 wl1271_tx_flush(wl);
5389
6d158ff3
SL
5390 mutex_lock(&wl->mutex);
5391
4cc53383 5392 if (unlikely(wl->state == WLCORE_STATE_OFF)) {
0f791eb4 5393 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
6e8cd331 5394 ieee80211_chswitch_done(vif, false);
6e8cd331 5395 goto out;
4cc53383
IY
5396 } else if (unlikely(wl->state != WLCORE_STATE_ON)) {
5397 goto out;
6d158ff3
SL
5398 }
5399
5400 ret = wl1271_ps_elp_wakeup(wl);
5401 if (ret < 0)
5402 goto out;
5403
52630c5d 5404 /* TODO: change mac80211 to pass vif as param */
6d158ff3 5405
0f791eb4
LC
5406 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
5407 unsigned long delay_usec;
e6562869 5408
fcab1890 5409 ret = wl->ops->channel_switch(wl, wlvif, ch_switch);
c50a2825
EP
5410 if (ret)
5411 goto out_sleep;
6d158ff3 5412
c50a2825
EP
5413 set_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags);
5414
5415 /* indicate failure 5 seconds after channel switch time */
5416 delay_usec = ieee80211_tu_to_usec(wlvif->beacon_int) *
0f791eb4 5417 ch_switch->count;
c50a2825 5418 ieee80211_queue_delayed_work(hw, &wlvif->channel_switch_work,
0f791eb4
LC
5419 usecs_to_jiffies(delay_usec) +
5420 msecs_to_jiffies(5000));
52630c5d 5421 }
6d158ff3 5422
c50a2825 5423out_sleep:
6d158ff3
SL
5424 wl1271_ps_elp_sleep(wl);
5425
5426out:
5427 mutex_unlock(&wl->mutex);
5428}
5429
534719f4
EP
5430static const void *wlcore_get_beacon_ie(struct wl1271 *wl,
5431 struct wl12xx_vif *wlvif,
5432 u8 eid)
5433{
5434 int ieoffset = offsetof(struct ieee80211_mgmt, u.beacon.variable);
5435 struct sk_buff *beacon =
5436 ieee80211_beacon_get(wl->hw, wl12xx_wlvif_to_vif(wlvif));
5437
5438 if (!beacon)
5439 return NULL;
5440
5441 return cfg80211_find_ie(eid,
5442 beacon->data + ieoffset,
5443 beacon->len - ieoffset);
5444}
5445
5446static int wlcore_get_csa_count(struct wl1271 *wl, struct wl12xx_vif *wlvif,
5447 u8 *csa_count)
5448{
5449 const u8 *ie;
5450 const struct ieee80211_channel_sw_ie *ie_csa;
5451
5452 ie = wlcore_get_beacon_ie(wl, wlvif, WLAN_EID_CHANNEL_SWITCH);
5453 if (!ie)
5454 return -EINVAL;
5455
5456 ie_csa = (struct ieee80211_channel_sw_ie *)&ie[2];
5457 *csa_count = ie_csa->count;
5458
5459 return 0;
5460}
5461
5462static void wlcore_op_channel_switch_beacon(struct ieee80211_hw *hw,
5463 struct ieee80211_vif *vif,
5464 struct cfg80211_chan_def *chandef)
5465{
5466 struct wl1271 *wl = hw->priv;
5467 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5468 struct ieee80211_channel_switch ch_switch = {
5469 .block_tx = true,
5470 .chandef = *chandef,
5471 };
5472 int ret;
5473
5474 wl1271_debug(DEBUG_MAC80211,
5475 "mac80211 channel switch beacon (role %d)",
5476 wlvif->role_id);
5477
5478 ret = wlcore_get_csa_count(wl, wlvif, &ch_switch.count);
5479 if (ret < 0) {
5480 wl1271_error("error getting beacon (for CSA counter)");
5481 return;
5482 }
5483
5484 mutex_lock(&wl->mutex);
5485
5486 if (unlikely(wl->state != WLCORE_STATE_ON)) {
5487 ret = -EBUSY;
5488 goto out;
5489 }
5490
5491 ret = wl1271_ps_elp_wakeup(wl);
5492 if (ret < 0)
5493 goto out;
5494
5495 ret = wl->ops->channel_switch(wl, wlvif, &ch_switch);
5496 if (ret)
5497 goto out_sleep;
5498
5499 set_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags);
5500
5501out_sleep:
5502 wl1271_ps_elp_sleep(wl);
5503out:
5504 mutex_unlock(&wl->mutex);
5505}
5506
77be2c54
EG
5507static void wlcore_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5508 u32 queues, bool drop)
d8ae5a25
EP
5509{
5510 struct wl1271 *wl = hw->priv;
5511
5512 wl1271_tx_flush(wl);
5513}
5514
dabf37db
EP
5515static int wlcore_op_remain_on_channel(struct ieee80211_hw *hw,
5516 struct ieee80211_vif *vif,
5517 struct ieee80211_channel *chan,
d339d5ca
IP
5518 int duration,
5519 enum ieee80211_roc_type type)
dabf37db
EP
5520{
5521 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5522 struct wl1271 *wl = hw->priv;
5523 int channel, ret = 0;
5524
5525 channel = ieee80211_frequency_to_channel(chan->center_freq);
5526
5527 wl1271_debug(DEBUG_MAC80211, "mac80211 roc %d (%d)",
5528 channel, wlvif->role_id);
5529
5530 mutex_lock(&wl->mutex);
5531
5532 if (unlikely(wl->state != WLCORE_STATE_ON))
5533 goto out;
5534
5535 /* return EBUSY if we can't ROC right now */
5536 if (WARN_ON(wl->roc_vif ||
5537 find_first_bit(wl->roc_map,
5538 WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES)) {
5539 ret = -EBUSY;
5540 goto out;
5541 }
5542
5543 ret = wl1271_ps_elp_wakeup(wl);
5544 if (ret < 0)
5545 goto out;
5546
5547 ret = wl12xx_start_dev(wl, wlvif, chan->band, channel);
5548 if (ret < 0)
5549 goto out_sleep;
5550
5551 wl->roc_vif = vif;
5552 ieee80211_queue_delayed_work(hw, &wl->roc_complete_work,
5553 msecs_to_jiffies(duration));
5554out_sleep:
5555 wl1271_ps_elp_sleep(wl);
5556out:
5557 mutex_unlock(&wl->mutex);
5558 return ret;
5559}
5560
5561static int __wlcore_roc_completed(struct wl1271 *wl)
5562{
5563 struct wl12xx_vif *wlvif;
5564 int ret;
5565
5566 /* already completed */
5567 if (unlikely(!wl->roc_vif))
5568 return 0;
5569
5570 wlvif = wl12xx_vif_to_data(wl->roc_vif);
5571
5572 if (!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
5573 return -EBUSY;
5574
5575 ret = wl12xx_stop_dev(wl, wlvif);
5576 if (ret < 0)
5577 return ret;
5578
5579 wl->roc_vif = NULL;
5580
5581 return 0;
5582}
5583
5584static int wlcore_roc_completed(struct wl1271 *wl)
5585{
5586 int ret;
5587
5588 wl1271_debug(DEBUG_MAC80211, "roc complete");
5589
5590 mutex_lock(&wl->mutex);
5591
5592 if (unlikely(wl->state != WLCORE_STATE_ON)) {
5593 ret = -EBUSY;
5594 goto out;
5595 }
5596
5597 ret = wl1271_ps_elp_wakeup(wl);
5598 if (ret < 0)
5599 goto out;
5600
5601 ret = __wlcore_roc_completed(wl);
5602
5603 wl1271_ps_elp_sleep(wl);
5604out:
5605 mutex_unlock(&wl->mutex);
5606
5607 return ret;
5608}
5609
5610static void wlcore_roc_complete_work(struct work_struct *work)
5611{
5612 struct delayed_work *dwork;
5613 struct wl1271 *wl;
5614 int ret;
5615
5616 dwork = container_of(work, struct delayed_work, work);
5617 wl = container_of(dwork, struct wl1271, roc_complete_work);
5618
5619 ret = wlcore_roc_completed(wl);
5620 if (!ret)
5621 ieee80211_remain_on_channel_expired(wl->hw);
5622}
5623
5624static int wlcore_op_cancel_remain_on_channel(struct ieee80211_hw *hw)
5625{
5626 struct wl1271 *wl = hw->priv;
5627
5628 wl1271_debug(DEBUG_MAC80211, "mac80211 croc");
5629
5630 /* TODO: per-vif */
5631 wl1271_tx_flush(wl);
5632
5633 /*
5634 * we can't just flush_work here, because it might deadlock
5635 * (as we might get called from the same workqueue)
5636 */
5637 cancel_delayed_work_sync(&wl->roc_complete_work);
5638 wlcore_roc_completed(wl);
5639
5640 return 0;
5641}
5642
5f9b6777
AN
5643static void wlcore_op_sta_rc_update(struct ieee80211_hw *hw,
5644 struct ieee80211_vif *vif,
5645 struct ieee80211_sta *sta,
5646 u32 changed)
5647{
5648 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5f9b6777 5649
7d3b29e5
EP
5650 wl1271_debug(DEBUG_MAC80211, "mac80211 sta_rc_update");
5651
5652 if (!(changed & IEEE80211_RC_BW_CHANGED))
5653 return;
5654
5655 /* this callback is atomic, so schedule a new work */
5656 wlvif->rc_update_bw = sta->bandwidth;
5657 ieee80211_queue_work(hw, &wlvif->rc_update_work);
5f9b6777
AN
5658}
5659
0a9ffac0
NZ
5660static int wlcore_op_get_rssi(struct ieee80211_hw *hw,
5661 struct ieee80211_vif *vif,
5662 struct ieee80211_sta *sta,
5663 s8 *rssi_dbm)
5664{
5665 struct wl1271 *wl = hw->priv;
5666 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5667 int ret = 0;
5668
5669 wl1271_debug(DEBUG_MAC80211, "mac80211 get_rssi");
5670
5671 mutex_lock(&wl->mutex);
5672
5673 if (unlikely(wl->state != WLCORE_STATE_ON))
5674 goto out;
5675
5676 ret = wl1271_ps_elp_wakeup(wl);
5677 if (ret < 0)
5678 goto out_sleep;
5679
5680 ret = wlcore_acx_average_rssi(wl, wlvif, rssi_dbm);
5681 if (ret < 0)
5682 goto out_sleep;
5683
5684out_sleep:
5685 wl1271_ps_elp_sleep(wl);
5686
5687out:
5688 mutex_unlock(&wl->mutex);
5689
5690 return ret;
5691}
5692
33437893
AN
5693static bool wl1271_tx_frames_pending(struct ieee80211_hw *hw)
5694{
5695 struct wl1271 *wl = hw->priv;
5696 bool ret = false;
5697
5698 mutex_lock(&wl->mutex);
5699
4cc53383 5700 if (unlikely(wl->state != WLCORE_STATE_ON))
33437893
AN
5701 goto out;
5702
5703 /* packets are considered pending if in the TX queue or the FW */
f1a46384 5704 ret = (wl1271_tx_total_queue_count(wl) > 0) || (wl->tx_frames_cnt > 0);
33437893
AN
5705out:
5706 mutex_unlock(&wl->mutex);
5707
5708 return ret;
5709}
5710
f5fc0f86
LC
5711/* can't be const, mac80211 writes to this */
5712static struct ieee80211_rate wl1271_rates[] = {
5713 { .bitrate = 10,
2b60100b
JO
5714 .hw_value = CONF_HW_BIT_RATE_1MBPS,
5715 .hw_value_short = CONF_HW_BIT_RATE_1MBPS, },
f5fc0f86 5716 { .bitrate = 20,
2b60100b
JO
5717 .hw_value = CONF_HW_BIT_RATE_2MBPS,
5718 .hw_value_short = CONF_HW_BIT_RATE_2MBPS,
f5fc0f86
LC
5719 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
5720 { .bitrate = 55,
2b60100b
JO
5721 .hw_value = CONF_HW_BIT_RATE_5_5MBPS,
5722 .hw_value_short = CONF_HW_BIT_RATE_5_5MBPS,
f5fc0f86
LC
5723 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
5724 { .bitrate = 110,
2b60100b
JO
5725 .hw_value = CONF_HW_BIT_RATE_11MBPS,
5726 .hw_value_short = CONF_HW_BIT_RATE_11MBPS,
f5fc0f86
LC
5727 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
5728 { .bitrate = 60,
2b60100b
JO
5729 .hw_value = CONF_HW_BIT_RATE_6MBPS,
5730 .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
f5fc0f86 5731 { .bitrate = 90,
2b60100b
JO
5732 .hw_value = CONF_HW_BIT_RATE_9MBPS,
5733 .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
f5fc0f86 5734 { .bitrate = 120,
2b60100b
JO
5735 .hw_value = CONF_HW_BIT_RATE_12MBPS,
5736 .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
f5fc0f86 5737 { .bitrate = 180,
2b60100b
JO
5738 .hw_value = CONF_HW_BIT_RATE_18MBPS,
5739 .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
f5fc0f86 5740 { .bitrate = 240,
2b60100b
JO
5741 .hw_value = CONF_HW_BIT_RATE_24MBPS,
5742 .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
f5fc0f86 5743 { .bitrate = 360,
2b60100b
JO
5744 .hw_value = CONF_HW_BIT_RATE_36MBPS,
5745 .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
f5fc0f86 5746 { .bitrate = 480,
2b60100b
JO
5747 .hw_value = CONF_HW_BIT_RATE_48MBPS,
5748 .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
f5fc0f86 5749 { .bitrate = 540,
2b60100b
JO
5750 .hw_value = CONF_HW_BIT_RATE_54MBPS,
5751 .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
f5fc0f86
LC
5752};
5753
fa97f46b 5754/* can't be const, mac80211 writes to this */
f5fc0f86 5755static struct ieee80211_channel wl1271_channels[] = {
583f8164
VG
5756 { .hw_value = 1, .center_freq = 2412, .max_power = WLCORE_MAX_TXPWR },
5757 { .hw_value = 2, .center_freq = 2417, .max_power = WLCORE_MAX_TXPWR },
5758 { .hw_value = 3, .center_freq = 2422, .max_power = WLCORE_MAX_TXPWR },
5759 { .hw_value = 4, .center_freq = 2427, .max_power = WLCORE_MAX_TXPWR },
5760 { .hw_value = 5, .center_freq = 2432, .max_power = WLCORE_MAX_TXPWR },
5761 { .hw_value = 6, .center_freq = 2437, .max_power = WLCORE_MAX_TXPWR },
5762 { .hw_value = 7, .center_freq = 2442, .max_power = WLCORE_MAX_TXPWR },
5763 { .hw_value = 8, .center_freq = 2447, .max_power = WLCORE_MAX_TXPWR },
5764 { .hw_value = 9, .center_freq = 2452, .max_power = WLCORE_MAX_TXPWR },
5765 { .hw_value = 10, .center_freq = 2457, .max_power = WLCORE_MAX_TXPWR },
5766 { .hw_value = 11, .center_freq = 2462, .max_power = WLCORE_MAX_TXPWR },
5767 { .hw_value = 12, .center_freq = 2467, .max_power = WLCORE_MAX_TXPWR },
5768 { .hw_value = 13, .center_freq = 2472, .max_power = WLCORE_MAX_TXPWR },
5769 { .hw_value = 14, .center_freq = 2484, .max_power = WLCORE_MAX_TXPWR },
f5fc0f86
LC
5770};
5771
5772/* can't be const, mac80211 writes to this */
5773static struct ieee80211_supported_band wl1271_band_2ghz = {
5774 .channels = wl1271_channels,
5775 .n_channels = ARRAY_SIZE(wl1271_channels),
5776 .bitrates = wl1271_rates,
5777 .n_bitrates = ARRAY_SIZE(wl1271_rates),
5778};
5779
1ebec3d7
TP
5780/* 5 GHz data rates for WL1273 */
5781static struct ieee80211_rate wl1271_rates_5ghz[] = {
5782 { .bitrate = 60,
5783 .hw_value = CONF_HW_BIT_RATE_6MBPS,
5784 .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
5785 { .bitrate = 90,
5786 .hw_value = CONF_HW_BIT_RATE_9MBPS,
5787 .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
5788 { .bitrate = 120,
5789 .hw_value = CONF_HW_BIT_RATE_12MBPS,
5790 .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
5791 { .bitrate = 180,
5792 .hw_value = CONF_HW_BIT_RATE_18MBPS,
5793 .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
5794 { .bitrate = 240,
5795 .hw_value = CONF_HW_BIT_RATE_24MBPS,
5796 .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
5797 { .bitrate = 360,
5798 .hw_value = CONF_HW_BIT_RATE_36MBPS,
5799 .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
5800 { .bitrate = 480,
5801 .hw_value = CONF_HW_BIT_RATE_48MBPS,
5802 .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
5803 { .bitrate = 540,
5804 .hw_value = CONF_HW_BIT_RATE_54MBPS,
5805 .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
5806};
5807
fa97f46b 5808/* 5 GHz band channels for WL1273 */
1ebec3d7 5809static struct ieee80211_channel wl1271_channels_5ghz[] = {
583f8164 5810 { .hw_value = 8, .center_freq = 5040, .max_power = WLCORE_MAX_TXPWR },
583f8164
VG
5811 { .hw_value = 12, .center_freq = 5060, .max_power = WLCORE_MAX_TXPWR },
5812 { .hw_value = 16, .center_freq = 5080, .max_power = WLCORE_MAX_TXPWR },
5813 { .hw_value = 34, .center_freq = 5170, .max_power = WLCORE_MAX_TXPWR },
5814 { .hw_value = 36, .center_freq = 5180, .max_power = WLCORE_MAX_TXPWR },
5815 { .hw_value = 38, .center_freq = 5190, .max_power = WLCORE_MAX_TXPWR },
5816 { .hw_value = 40, .center_freq = 5200, .max_power = WLCORE_MAX_TXPWR },
5817 { .hw_value = 42, .center_freq = 5210, .max_power = WLCORE_MAX_TXPWR },
5818 { .hw_value = 44, .center_freq = 5220, .max_power = WLCORE_MAX_TXPWR },
5819 { .hw_value = 46, .center_freq = 5230, .max_power = WLCORE_MAX_TXPWR },
5820 { .hw_value = 48, .center_freq = 5240, .max_power = WLCORE_MAX_TXPWR },
5821 { .hw_value = 52, .center_freq = 5260, .max_power = WLCORE_MAX_TXPWR },
5822 { .hw_value = 56, .center_freq = 5280, .max_power = WLCORE_MAX_TXPWR },
5823 { .hw_value = 60, .center_freq = 5300, .max_power = WLCORE_MAX_TXPWR },
5824 { .hw_value = 64, .center_freq = 5320, .max_power = WLCORE_MAX_TXPWR },
5825 { .hw_value = 100, .center_freq = 5500, .max_power = WLCORE_MAX_TXPWR },
5826 { .hw_value = 104, .center_freq = 5520, .max_power = WLCORE_MAX_TXPWR },
5827 { .hw_value = 108, .center_freq = 5540, .max_power = WLCORE_MAX_TXPWR },
5828 { .hw_value = 112, .center_freq = 5560, .max_power = WLCORE_MAX_TXPWR },
5829 { .hw_value = 116, .center_freq = 5580, .max_power = WLCORE_MAX_TXPWR },
5830 { .hw_value = 120, .center_freq = 5600, .max_power = WLCORE_MAX_TXPWR },
5831 { .hw_value = 124, .center_freq = 5620, .max_power = WLCORE_MAX_TXPWR },
5832 { .hw_value = 128, .center_freq = 5640, .max_power = WLCORE_MAX_TXPWR },
5833 { .hw_value = 132, .center_freq = 5660, .max_power = WLCORE_MAX_TXPWR },
5834 { .hw_value = 136, .center_freq = 5680, .max_power = WLCORE_MAX_TXPWR },
5835 { .hw_value = 140, .center_freq = 5700, .max_power = WLCORE_MAX_TXPWR },
5836 { .hw_value = 149, .center_freq = 5745, .max_power = WLCORE_MAX_TXPWR },
5837 { .hw_value = 153, .center_freq = 5765, .max_power = WLCORE_MAX_TXPWR },
5838 { .hw_value = 157, .center_freq = 5785, .max_power = WLCORE_MAX_TXPWR },
5839 { .hw_value = 161, .center_freq = 5805, .max_power = WLCORE_MAX_TXPWR },
5840 { .hw_value = 165, .center_freq = 5825, .max_power = WLCORE_MAX_TXPWR },
1ebec3d7
TP
5841};
5842
1ebec3d7
TP
5843static struct ieee80211_supported_band wl1271_band_5ghz = {
5844 .channels = wl1271_channels_5ghz,
5845 .n_channels = ARRAY_SIZE(wl1271_channels_5ghz),
5846 .bitrates = wl1271_rates_5ghz,
5847 .n_bitrates = ARRAY_SIZE(wl1271_rates_5ghz),
f876bb9a
JO
5848};
5849
f5fc0f86
LC
5850static const struct ieee80211_ops wl1271_ops = {
5851 .start = wl1271_op_start,
c24ec83b 5852 .stop = wlcore_op_stop,
f5fc0f86
LC
5853 .add_interface = wl1271_op_add_interface,
5854 .remove_interface = wl1271_op_remove_interface,
c0fad1b7 5855 .change_interface = wl12xx_op_change_interface,
f634a4e7 5856#ifdef CONFIG_PM
402e4861
EP
5857 .suspend = wl1271_op_suspend,
5858 .resume = wl1271_op_resume,
f634a4e7 5859#endif
f5fc0f86 5860 .config = wl1271_op_config,
c87dec9f 5861 .prepare_multicast = wl1271_op_prepare_multicast,
f5fc0f86
LC
5862 .configure_filter = wl1271_op_configure_filter,
5863 .tx = wl1271_op_tx,
a1c597f2 5864 .set_key = wlcore_op_set_key,
f5fc0f86 5865 .hw_scan = wl1271_op_hw_scan,
73ecce31 5866 .cancel_hw_scan = wl1271_op_cancel_hw_scan,
33c2c06c
LC
5867 .sched_scan_start = wl1271_op_sched_scan_start,
5868 .sched_scan_stop = wl1271_op_sched_scan_stop,
f5fc0f86 5869 .bss_info_changed = wl1271_op_bss_info_changed,
68d069c4 5870 .set_frag_threshold = wl1271_op_set_frag_threshold,
f5fc0f86 5871 .set_rts_threshold = wl1271_op_set_rts_threshold,
c6999d83 5872 .conf_tx = wl1271_op_conf_tx,
bbbb538e 5873 .get_tsf = wl1271_op_get_tsf,
ece550d0 5874 .get_survey = wl1271_op_get_survey,
2d6cf2b5 5875 .sta_state = wl12xx_op_sta_state,
bbba3e68 5876 .ampdu_action = wl1271_op_ampdu_action,
33437893 5877 .tx_frames_pending = wl1271_tx_frames_pending,
af7fbb28 5878 .set_bitrate_mask = wl12xx_set_bitrate_mask,
ba1e6eb9 5879 .set_default_unicast_key = wl1271_op_set_default_key_idx,
6d158ff3 5880 .channel_switch = wl12xx_op_channel_switch,
534719f4 5881 .channel_switch_beacon = wlcore_op_channel_switch_beacon,
d8ae5a25 5882 .flush = wlcore_op_flush,
dabf37db
EP
5883 .remain_on_channel = wlcore_op_remain_on_channel,
5884 .cancel_remain_on_channel = wlcore_op_cancel_remain_on_channel,
b6970ee5
EP
5885 .add_chanctx = wlcore_op_add_chanctx,
5886 .remove_chanctx = wlcore_op_remove_chanctx,
5887 .change_chanctx = wlcore_op_change_chanctx,
5888 .assign_vif_chanctx = wlcore_op_assign_vif_chanctx,
5889 .unassign_vif_chanctx = wlcore_op_unassign_vif_chanctx,
750e9d15 5890 .switch_vif_chanctx = wlcore_op_switch_vif_chanctx,
5f9b6777 5891 .sta_rc_update = wlcore_op_sta_rc_update,
0a9ffac0 5892 .get_rssi = wlcore_op_get_rssi,
c8c90873 5893 CFG80211_TESTMODE_CMD(wl1271_tm_cmd)
f5fc0f86
LC
5894};
5895
f876bb9a 5896
43a8bc5a 5897u8 wlcore_rate_to_idx(struct wl1271 *wl, u8 rate, enum ieee80211_band band)
f876bb9a
JO
5898{
5899 u8 idx;
5900
43a8bc5a 5901 BUG_ON(band >= 2);
f876bb9a 5902
43a8bc5a 5903 if (unlikely(rate >= wl->hw_tx_rate_tbl_size)) {
f876bb9a
JO
5904 wl1271_error("Illegal RX rate from HW: %d", rate);
5905 return 0;
5906 }
5907
43a8bc5a 5908 idx = wl->band_rate_to_idx[band][rate];
f876bb9a
JO
5909 if (unlikely(idx == CONF_HW_RXTX_RATE_UNSUPPORTED)) {
5910 wl1271_error("Unsupported RX rate from HW: %d", rate);
5911 return 0;
5912 }
5913
5914 return idx;
5915}
5916
f4afbed9 5917static void wl12xx_derive_mac_addresses(struct wl1271 *wl, u32 oui, u32 nic)
5e037e74
LC
5918{
5919 int i;
5920
f4afbed9
AN
5921 wl1271_debug(DEBUG_PROBE, "base address: oui %06x nic %06x",
5922 oui, nic);
5e037e74 5923
f4afbed9 5924 if (nic + WLCORE_NUM_MAC_ADDRESSES - wl->num_mac_addr > 0xffffff)
5e037e74
LC
5925 wl1271_warning("NIC part of the MAC address wraps around!");
5926
f4afbed9 5927 for (i = 0; i < wl->num_mac_addr; i++) {
5e037e74
LC
5928 wl->addresses[i].addr[0] = (u8)(oui >> 16);
5929 wl->addresses[i].addr[1] = (u8)(oui >> 8);
5930 wl->addresses[i].addr[2] = (u8) oui;
5931 wl->addresses[i].addr[3] = (u8)(nic >> 16);
5932 wl->addresses[i].addr[4] = (u8)(nic >> 8);
5933 wl->addresses[i].addr[5] = (u8) nic;
5934 nic++;
5935 }
5936
f4afbed9
AN
5937 /* we may be one address short at the most */
5938 WARN_ON(wl->num_mac_addr + 1 < WLCORE_NUM_MAC_ADDRESSES);
5939
5940 /*
5941 * turn on the LAA bit in the first address and use it as
5942 * the last address.
5943 */
5944 if (wl->num_mac_addr < WLCORE_NUM_MAC_ADDRESSES) {
5945 int idx = WLCORE_NUM_MAC_ADDRESSES - 1;
5946 memcpy(&wl->addresses[idx], &wl->addresses[0],
5947 sizeof(wl->addresses[0]));
5948 /* LAA bit */
71a301bb 5949 wl->addresses[idx].addr[0] |= BIT(1);
f4afbed9
AN
5950 }
5951
5952 wl->hw->wiphy->n_addresses = WLCORE_NUM_MAC_ADDRESSES;
5e037e74
LC
5953 wl->hw->wiphy->addresses = wl->addresses;
5954}
5955
30c5dbd1
LC
5956static int wl12xx_get_hw_info(struct wl1271 *wl)
5957{
5958 int ret;
30c5dbd1
LC
5959
5960 ret = wl12xx_set_power_on(wl);
5961 if (ret < 0)
4fb4e0be 5962 return ret;
30c5dbd1 5963
6134323f
IY
5964 ret = wlcore_read_reg(wl, REG_CHIP_ID_B, &wl->chip.id);
5965 if (ret < 0)
5966 goto out;
30c5dbd1 5967
00782136
LC
5968 wl->fuse_oui_addr = 0;
5969 wl->fuse_nic_addr = 0;
30c5dbd1 5970
6134323f
IY
5971 ret = wl->ops->get_pg_ver(wl, &wl->hw_pg_ver);
5972 if (ret < 0)
5973 goto out;
30c5dbd1 5974
30d9b4a5 5975 if (wl->ops->get_mac)
6134323f 5976 ret = wl->ops->get_mac(wl);
5e037e74 5977
30c5dbd1 5978out:
6134323f 5979 wl1271_power_off(wl);
30c5dbd1
LC
5980 return ret;
5981}
5982
4b32a2c9 5983static int wl1271_register_hw(struct wl1271 *wl)
f5fc0f86
LC
5984{
5985 int ret;
5e037e74 5986 u32 oui_addr = 0, nic_addr = 0;
f5fc0f86
LC
5987
5988 if (wl->mac80211_registered)
5989 return 0;
5990
6f8d6b20 5991 if (wl->nvs_len >= 12) {
bc765bf3
SL
5992 /* NOTE: The wl->nvs->nvs element must be first, in
5993 * order to simplify the casting, we assume it is at
5994 * the beginning of the wl->nvs structure.
5995 */
5996 u8 *nvs_ptr = (u8 *)wl->nvs;
31d26ec6 5997
5e037e74
LC
5998 oui_addr =
5999 (nvs_ptr[11] << 16) + (nvs_ptr[10] << 8) + nvs_ptr[6];
6000 nic_addr =
6001 (nvs_ptr[5] << 16) + (nvs_ptr[4] << 8) + nvs_ptr[3];
6002 }
6003
6004 /* if the MAC address is zeroed in the NVS derive from fuse */
6005 if (oui_addr == 0 && nic_addr == 0) {
6006 oui_addr = wl->fuse_oui_addr;
6007 /* fuse has the BD_ADDR, the WLAN addresses are the next two */
6008 nic_addr = wl->fuse_nic_addr + 1;
31d26ec6
AN
6009 }
6010
f4afbed9 6011 wl12xx_derive_mac_addresses(wl, oui_addr, nic_addr);
f5fc0f86
LC
6012
6013 ret = ieee80211_register_hw(wl->hw);
6014 if (ret < 0) {
6015 wl1271_error("unable to register mac80211 hw: %d", ret);
30c5dbd1 6016 goto out;
f5fc0f86
LC
6017 }
6018
6019 wl->mac80211_registered = true;
6020
d60080ae
EP
6021 wl1271_debugfs_init(wl);
6022
f5fc0f86
LC
6023 wl1271_notice("loaded");
6024
30c5dbd1
LC
6025out:
6026 return ret;
f5fc0f86
LC
6027}
6028
4b32a2c9 6029static void wl1271_unregister_hw(struct wl1271 *wl)
3b56dd6a 6030{
3fcdab70 6031 if (wl->plt)
f3df1331 6032 wl1271_plt_stop(wl);
4ae3fa87 6033
3b56dd6a
TP
6034 ieee80211_unregister_hw(wl->hw);
6035 wl->mac80211_registered = false;
6036
6037}
3b56dd6a 6038
4b32a2c9 6039static int wl1271_init_ieee80211(struct wl1271 *wl)
f5fc0f86 6040{
583f8164 6041 int i;
7a55724e
JO
6042 static const u32 cipher_suites[] = {
6043 WLAN_CIPHER_SUITE_WEP40,
6044 WLAN_CIPHER_SUITE_WEP104,
6045 WLAN_CIPHER_SUITE_TKIP,
6046 WLAN_CIPHER_SUITE_CCMP,
6047 WL1271_CIPHER_SUITE_GEM,
6048 };
6049
2c0133a4
AN
6050 /* The tx descriptor buffer */
6051 wl->hw->extra_tx_headroom = sizeof(struct wl1271_tx_hw_descr);
6052
6053 if (wl->quirks & WLCORE_QUIRK_TKIP_HEADER_SPACE)
6054 wl->hw->extra_tx_headroom += WL1271_EXTRA_SPACE_TKIP;
f5fc0f86
LC
6055
6056 /* unit us */
6057 /* FIXME: find a proper value */
50c500ad 6058 wl->hw->max_listen_interval = wl->conf.conn.max_listen_interval;
f5fc0f86
LC
6059
6060 wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
0a34332f 6061 IEEE80211_HW_SUPPORTS_PS |
f1d63a59 6062 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
4695dc91 6063 IEEE80211_HW_SUPPORTS_UAPSD |
a9af092b 6064 IEEE80211_HW_HAS_RATE_CONTROL |
00236aed 6065 IEEE80211_HW_CONNECTION_MONITOR |
25eaea30 6066 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
fcd23b63 6067 IEEE80211_HW_SPECTRUM_MGMT |
93f8c8e0
AN
6068 IEEE80211_HW_AP_LINK_PS |
6069 IEEE80211_HW_AMPDU_AGGREGATION |
79aba1ba 6070 IEEE80211_HW_TX_AMPDU_SETUP_IN_HW |
6b27fe51
AN
6071 IEEE80211_HW_QUEUE_CONTROL |
6072 IEEE80211_HW_CHANCTX_STA_CSA;
f5fc0f86 6073
7a55724e
JO
6074 wl->hw->wiphy->cipher_suites = cipher_suites;
6075 wl->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
6076
e0d8bbf0 6077 wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
045c745f
EP
6078 BIT(NL80211_IFTYPE_ADHOC) | BIT(NL80211_IFTYPE_AP) |
6079 BIT(NL80211_IFTYPE_P2P_CLIENT) | BIT(NL80211_IFTYPE_P2P_GO);
f5fc0f86 6080 wl->hw->wiphy->max_scan_ssids = 1;
221737d2
LC
6081 wl->hw->wiphy->max_sched_scan_ssids = 16;
6082 wl->hw->wiphy->max_match_sets = 16;
ea559b46
GE
6083 /*
6084 * Maximum length of elements in scanning probe request templates
6085 * should be the maximum length possible for a template, without
6086 * the IEEE80211 header of the template
6087 */
c08e371a 6088 wl->hw->wiphy->max_scan_ie_len = WL1271_CMD_TEMPL_MAX_SIZE -
ea559b46 6089 sizeof(struct ieee80211_header);
a8aaaf53 6090
c08e371a 6091 wl->hw->wiphy->max_sched_scan_ie_len = WL1271_CMD_TEMPL_MAX_SIZE -
c9e79a47
LC
6092 sizeof(struct ieee80211_header);
6093
fbddf587 6094 wl->hw->wiphy->max_remain_on_channel_duration = 30000;
dabf37db 6095
81ddbb5c 6096 wl->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD |
1fb90260 6097 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
534719f4
EP
6098 WIPHY_FLAG_SUPPORTS_SCHED_SCAN |
6099 WIPHY_FLAG_HAS_CHANNEL_SWITCH;
1ec23f7f 6100
4a31c11c
LC
6101 /* make sure all our channels fit in the scanned_ch bitmask */
6102 BUILD_BUG_ON(ARRAY_SIZE(wl1271_channels) +
6103 ARRAY_SIZE(wl1271_channels_5ghz) >
6104 WL1271_MAX_CHANNELS);
583f8164
VG
6105 /*
6106 * clear channel flags from the previous usage
6107 * and restore max_power & max_antenna_gain values.
6108 */
6109 for (i = 0; i < ARRAY_SIZE(wl1271_channels); i++) {
6110 wl1271_band_2ghz.channels[i].flags = 0;
6111 wl1271_band_2ghz.channels[i].max_power = WLCORE_MAX_TXPWR;
6112 wl1271_band_2ghz.channels[i].max_antenna_gain = 0;
6113 }
6114
6115 for (i = 0; i < ARRAY_SIZE(wl1271_channels_5ghz); i++) {
6116 wl1271_band_5ghz.channels[i].flags = 0;
6117 wl1271_band_5ghz.channels[i].max_power = WLCORE_MAX_TXPWR;
6118 wl1271_band_5ghz.channels[i].max_antenna_gain = 0;
6119 }
6120
a8aaaf53
LC
6121 /*
6122 * We keep local copies of the band structs because we need to
6123 * modify them on a per-device basis.
6124 */
6125 memcpy(&wl->bands[IEEE80211_BAND_2GHZ], &wl1271_band_2ghz,
6126 sizeof(wl1271_band_2ghz));
bfb92ca1
EP
6127 memcpy(&wl->bands[IEEE80211_BAND_2GHZ].ht_cap,
6128 &wl->ht_cap[IEEE80211_BAND_2GHZ],
6129 sizeof(*wl->ht_cap));
a8aaaf53
LC
6130 memcpy(&wl->bands[IEEE80211_BAND_5GHZ], &wl1271_band_5ghz,
6131 sizeof(wl1271_band_5ghz));
bfb92ca1
EP
6132 memcpy(&wl->bands[IEEE80211_BAND_5GHZ].ht_cap,
6133 &wl->ht_cap[IEEE80211_BAND_5GHZ],
6134 sizeof(*wl->ht_cap));
a8aaaf53
LC
6135
6136 wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
6137 &wl->bands[IEEE80211_BAND_2GHZ];
6138 wl->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
6139 &wl->bands[IEEE80211_BAND_5GHZ];
1ebec3d7 6140
1c33db78
AN
6141 /*
6142 * allow 4 queues per mac address we support +
6143 * 1 cab queue per mac + one global offchannel Tx queue
6144 */
6145 wl->hw->queues = (NUM_TX_QUEUES + 1) * WLCORE_NUM_MAC_ADDRESSES + 1;
6146
6147 /* the last queue is the offchannel queue */
6148 wl->hw->offchannel_tx_hw_queue = wl->hw->queues - 1;
31627dc5 6149 wl->hw->max_rates = 1;
12bd8949 6150
b7417d93
JO
6151 wl->hw->wiphy->reg_notifier = wl1271_reg_notify;
6152
9c1b190b
AN
6153 /* the FW answers probe-requests in AP-mode */
6154 wl->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
6155 wl->hw->wiphy->probe_resp_offload =
6156 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
6157 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
6158 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
6159
bcab320b 6160 /* allowed interface combinations */
abf0b249
EP
6161 wl->hw->wiphy->iface_combinations = wl->iface_combinations;
6162 wl->hw->wiphy->n_iface_combinations = wl->n_iface_combinations;
bcab320b 6163
d8c5a48d
EP
6164 /* register vendor commands */
6165 wlcore_set_vendor_commands(wl->hw->wiphy);
6166
a390e85c 6167 SET_IEEE80211_DEV(wl->hw, wl->dev);
f5fc0f86 6168
f84f7d78 6169 wl->hw->sta_data_size = sizeof(struct wl1271_station);
87fbcb0f 6170 wl->hw->vif_data_size = sizeof(struct wl12xx_vif);
f84f7d78 6171
ba421f8f 6172 wl->hw->max_rx_aggregation_subframes = wl->conf.ht.rx_ba_win_size;
4c9cfa78 6173
f5fc0f86
LC
6174 return 0;
6175}
6176
c50a2825
EP
6177struct ieee80211_hw *wlcore_alloc_hw(size_t priv_size, u32 aggr_buf_size,
6178 u32 mbox_size)
f5fc0f86 6179{
f5fc0f86
LC
6180 struct ieee80211_hw *hw;
6181 struct wl1271 *wl;
a8c0ddb5 6182 int i, j, ret;
1f37cbc9 6183 unsigned int order;
f5fc0f86
LC
6184
6185 hw = ieee80211_alloc_hw(sizeof(*wl), &wl1271_ops);
6186 if (!hw) {
6187 wl1271_error("could not alloc ieee80211_hw");
a1dd8187 6188 ret = -ENOMEM;
3b56dd6a
TP
6189 goto err_hw_alloc;
6190 }
6191
f5fc0f86
LC
6192 wl = hw->priv;
6193 memset(wl, 0, sizeof(*wl));
6194
96e0c683
AN
6195 wl->priv = kzalloc(priv_size, GFP_KERNEL);
6196 if (!wl->priv) {
6197 wl1271_error("could not alloc wl priv");
6198 ret = -ENOMEM;
6199 goto err_priv_alloc;
6200 }
6201
87627214 6202 INIT_LIST_HEAD(&wl->wlvif_list);
01c09162 6203
f5fc0f86 6204 wl->hw = hw;
f5fc0f86 6205
da08fdfa
EP
6206 /*
6207 * wl->num_links is not configured yet, so just use WLCORE_MAX_LINKS.
6208 * we don't allocate any additional resource here, so that's fine.
6209 */
a8c0ddb5 6210 for (i = 0; i < NUM_TX_QUEUES; i++)
da08fdfa 6211 for (j = 0; j < WLCORE_MAX_LINKS; j++)
a8c0ddb5
AN
6212 skb_queue_head_init(&wl->links[j].tx_queue[i]);
6213
a620865e
IY
6214 skb_queue_head_init(&wl->deferred_rx_queue);
6215 skb_queue_head_init(&wl->deferred_tx_queue);
6216
37b70a81 6217 INIT_DELAYED_WORK(&wl->elp_work, wl1271_elp_work);
a620865e 6218 INIT_WORK(&wl->netstack_work, wl1271_netstack_work);
117b38d0
JO
6219 INIT_WORK(&wl->tx_work, wl1271_tx_work);
6220 INIT_WORK(&wl->recovery_work, wl1271_recovery_work);
6221 INIT_DELAYED_WORK(&wl->scan_complete_work, wl1271_scan_complete_work);
dabf37db 6222 INIT_DELAYED_WORK(&wl->roc_complete_work, wlcore_roc_complete_work);
55df5afb 6223 INIT_DELAYED_WORK(&wl->tx_watchdog_work, wl12xx_tx_watchdog_work);
77ddaa10 6224
92ef8960
EP
6225 wl->freezable_wq = create_freezable_workqueue("wl12xx_wq");
6226 if (!wl->freezable_wq) {
6227 ret = -ENOMEM;
6228 goto err_hw;
6229 }
6230
8f6ac537 6231 wl->channel = 0;
f5fc0f86 6232 wl->rx_counter = 0;
f5fc0f86 6233 wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
8a5a37a6 6234 wl->band = IEEE80211_BAND_2GHZ;
83d08d3f 6235 wl->channel_type = NL80211_CHAN_NO_HT;
830fb67b 6236 wl->flags = 0;
7fc3a864 6237 wl->sg_enabled = true;
66340e5b 6238 wl->sleep_auth = WL1271_PSM_ILLEGAL;
c108c905 6239 wl->recovery_count = 0;
d717fd61 6240 wl->hw_pg_ver = -1;
b622d992
AN
6241 wl->ap_ps_map = 0;
6242 wl->ap_fw_ps_map = 0;
606ea9fa 6243 wl->quirks = 0;
341b7cde 6244 wl->platform_quirks = 0;
f4df1bd5 6245 wl->system_hlid = WL12XX_SYSTEM_HLID;
da03209e 6246 wl->active_sta_count = 0;
9a100968 6247 wl->active_link_count = 0;
95dac04f
IY
6248 wl->fwlog_size = 0;
6249 init_waitqueue_head(&wl->fwlog_waitq);
f5fc0f86 6250
f4df1bd5
EP
6251 /* The system link is always allocated */
6252 __set_bit(WL12XX_SYSTEM_HLID, wl->links_map);
6253
25eeb9e3 6254 memset(wl->tx_frames_map, 0, sizeof(wl->tx_frames_map));
72b0624f 6255 for (i = 0; i < wl->num_tx_desc; i++)
f5fc0f86
LC
6256 wl->tx_frames[i] = NULL;
6257
6258 spin_lock_init(&wl->wl_lock);
6259
4cc53383 6260 wl->state = WLCORE_STATE_OFF;
3fcdab70 6261 wl->fw_type = WL12XX_FW_TYPE_NONE;
f5fc0f86 6262 mutex_init(&wl->mutex);
2c38849f 6263 mutex_init(&wl->flush_mutex);
6f8d6b20 6264 init_completion(&wl->nvs_loading_complete);
f5fc0f86 6265
26a309c7 6266 order = get_order(aggr_buf_size);
1f37cbc9
IY
6267 wl->aggr_buf = (u8 *)__get_free_pages(GFP_KERNEL, order);
6268 if (!wl->aggr_buf) {
6269 ret = -ENOMEM;
92ef8960 6270 goto err_wq;
1f37cbc9 6271 }
26a309c7 6272 wl->aggr_buf_size = aggr_buf_size;
1f37cbc9 6273
990f5de7
IY
6274 wl->dummy_packet = wl12xx_alloc_dummy_packet(wl);
6275 if (!wl->dummy_packet) {
6276 ret = -ENOMEM;
6277 goto err_aggr;
6278 }
6279
95dac04f
IY
6280 /* Allocate one page for the FW log */
6281 wl->fwlog = (u8 *)get_zeroed_page(GFP_KERNEL);
6282 if (!wl->fwlog) {
6283 ret = -ENOMEM;
6284 goto err_dummy_packet;
6285 }
6286
c50a2825
EP
6287 wl->mbox_size = mbox_size;
6288 wl->mbox = kmalloc(wl->mbox_size, GFP_KERNEL | GFP_DMA);
690142e9
MG
6289 if (!wl->mbox) {
6290 ret = -ENOMEM;
6291 goto err_fwlog;
6292 }
6293
2e07d028
IY
6294 wl->buffer_32 = kmalloc(sizeof(*wl->buffer_32), GFP_KERNEL);
6295 if (!wl->buffer_32) {
6296 ret = -ENOMEM;
6297 goto err_mbox;
6298 }
6299
c332a4b8 6300 return hw;
a1dd8187 6301
2e07d028
IY
6302err_mbox:
6303 kfree(wl->mbox);
6304
690142e9
MG
6305err_fwlog:
6306 free_page((unsigned long)wl->fwlog);
6307
990f5de7
IY
6308err_dummy_packet:
6309 dev_kfree_skb(wl->dummy_packet);
6310
1f37cbc9
IY
6311err_aggr:
6312 free_pages((unsigned long)wl->aggr_buf, order);
6313
92ef8960
EP
6314err_wq:
6315 destroy_workqueue(wl->freezable_wq);
6316
a1dd8187 6317err_hw:
3b56dd6a 6318 wl1271_debugfs_exit(wl);
96e0c683
AN
6319 kfree(wl->priv);
6320
6321err_priv_alloc:
3b56dd6a
TP
6322 ieee80211_free_hw(hw);
6323
6324err_hw_alloc:
a1dd8187 6325
a1dd8187 6326 return ERR_PTR(ret);
c332a4b8 6327}
ffeb501c 6328EXPORT_SYMBOL_GPL(wlcore_alloc_hw);
c332a4b8 6329
ffeb501c 6330int wlcore_free_hw(struct wl1271 *wl)
c332a4b8 6331{
95dac04f
IY
6332 /* Unblock any fwlog readers */
6333 mutex_lock(&wl->mutex);
6334 wl->fwlog_size = -1;
6335 wake_up_interruptible_all(&wl->fwlog_waitq);
6336 mutex_unlock(&wl->mutex);
6337
33cab57a 6338 wlcore_sysfs_free(wl);
6f07b72a 6339
2e07d028 6340 kfree(wl->buffer_32);
a8e27820 6341 kfree(wl->mbox);
95dac04f 6342 free_page((unsigned long)wl->fwlog);
990f5de7 6343 dev_kfree_skb(wl->dummy_packet);
26a309c7 6344 free_pages((unsigned long)wl->aggr_buf, get_order(wl->aggr_buf_size));
c332a4b8
TP
6345
6346 wl1271_debugfs_exit(wl);
6347
c332a4b8
TP
6348 vfree(wl->fw);
6349 wl->fw = NULL;
3fcdab70 6350 wl->fw_type = WL12XX_FW_TYPE_NONE;
c332a4b8
TP
6351 kfree(wl->nvs);
6352 wl->nvs = NULL;
6353
75fb4df7
EP
6354 kfree(wl->raw_fw_status);
6355 kfree(wl->fw_status);
c332a4b8 6356 kfree(wl->tx_res_if);
92ef8960 6357 destroy_workqueue(wl->freezable_wq);
c332a4b8 6358
96e0c683 6359 kfree(wl->priv);
c332a4b8
TP
6360 ieee80211_free_hw(wl->hw);
6361
6362 return 0;
6363}
ffeb501c 6364EXPORT_SYMBOL_GPL(wlcore_free_hw);
50b3eb4b 6365
964dc9e2
JB
6366#ifdef CONFIG_PM
6367static const struct wiphy_wowlan_support wlcore_wowlan_support = {
6368 .flags = WIPHY_WOWLAN_ANY,
6369 .n_patterns = WL1271_MAX_RX_FILTERS,
6370 .pattern_min_len = 1,
6371 .pattern_max_len = WL1271_RX_FILTER_MAX_PATTERN_SIZE,
6372};
6373#endif
6374
f2cede49
AN
6375static irqreturn_t wlcore_hardirq(int irq, void *cookie)
6376{
6377 return IRQ_WAKE_THREAD;
6378}
6379
6f8d6b20 6380static void wlcore_nvs_cb(const struct firmware *fw, void *context)
ce2a217c 6381{
6f8d6b20
IY
6382 struct wl1271 *wl = context;
6383 struct platform_device *pdev = wl->pdev;
90650625 6384 struct wlcore_platdev_data *pdev_data = dev_get_platdata(&pdev->dev);
afb43e6d 6385 struct wl12xx_platform_data *pdata = pdev_data->pdata;
a390e85c 6386 unsigned long irqflags;
ffeb501c 6387 int ret;
f2cede49 6388 irq_handler_t hardirq_fn = NULL;
a390e85c 6389
6f8d6b20
IY
6390 if (fw) {
6391 wl->nvs = kmemdup(fw->data, fw->size, GFP_KERNEL);
6392 if (!wl->nvs) {
6393 wl1271_error("Could not allocate nvs data");
6394 goto out;
6395 }
6396 wl->nvs_len = fw->size;
6397 } else {
6398 wl1271_debug(DEBUG_BOOT, "Could not get nvs file %s",
6399 WL12XX_NVS_NAME);
6400 wl->nvs = NULL;
6401 wl->nvs_len = 0;
a390e85c
FB
6402 }
6403
3992eb2b
IY
6404 ret = wl->ops->setup(wl);
6405 if (ret < 0)
6f8d6b20 6406 goto out_free_nvs;
3992eb2b 6407
72b0624f
AN
6408 BUG_ON(wl->num_tx_desc > WLCORE_MAX_TX_DESCRIPTORS);
6409
e87288f0
LC
6410 /* adjust some runtime configuration parameters */
6411 wlcore_adjust_conf(wl);
6412
a390e85c 6413 wl->irq = platform_get_irq(pdev, 0);
a390e85c 6414 wl->platform_quirks = pdata->platform_quirks;
afb43e6d 6415 wl->if_ops = pdev_data->if_ops;
a390e85c 6416
f2cede49 6417 if (wl->platform_quirks & WL12XX_PLATFORM_QUIRK_EDGE_IRQ) {
a390e85c 6418 irqflags = IRQF_TRIGGER_RISING;
f2cede49
AN
6419 hardirq_fn = wlcore_hardirq;
6420 } else {
a390e85c 6421 irqflags = IRQF_TRIGGER_HIGH | IRQF_ONESHOT;
f2cede49 6422 }
a390e85c 6423
f2cede49 6424 ret = request_threaded_irq(wl->irq, hardirq_fn, wlcore_irq,
97236a06 6425 irqflags, pdev->name, wl);
a390e85c
FB
6426 if (ret < 0) {
6427 wl1271_error("request_irq() failed: %d", ret);
6f8d6b20 6428 goto out_free_nvs;
a390e85c
FB
6429 }
6430
dfb89c56 6431#ifdef CONFIG_PM
a390e85c
FB
6432 ret = enable_irq_wake(wl->irq);
6433 if (!ret) {
6434 wl->irq_wake_enabled = true;
6435 device_init_wakeup(wl->dev, 1);
964dc9e2
JB
6436 if (pdata->pwr_in_suspend)
6437 wl->hw->wiphy->wowlan = &wlcore_wowlan_support;
a390e85c 6438 }
dfb89c56 6439#endif
a390e85c
FB
6440 disable_irq(wl->irq);
6441
4afc37a0
LC
6442 ret = wl12xx_get_hw_info(wl);
6443 if (ret < 0) {
6444 wl1271_error("couldn't get hw info");
8b425e62 6445 goto out_irq;
4afc37a0
LC
6446 }
6447
6448 ret = wl->ops->identify_chip(wl);
6449 if (ret < 0)
8b425e62 6450 goto out_irq;
4afc37a0 6451
a390e85c
FB
6452 ret = wl1271_init_ieee80211(wl);
6453 if (ret)
6454 goto out_irq;
6455
6456 ret = wl1271_register_hw(wl);
6457 if (ret)
6458 goto out_irq;
6459
33cab57a
LC
6460 ret = wlcore_sysfs_init(wl);
6461 if (ret)
8b425e62 6462 goto out_unreg;
f79f890c 6463
6f8d6b20 6464 wl->initialized = true;
ffeb501c 6465 goto out;
a390e85c 6466
8b425e62
LC
6467out_unreg:
6468 wl1271_unregister_hw(wl);
6469
a390e85c
FB
6470out_irq:
6471 free_irq(wl->irq, wl);
6472
6f8d6b20
IY
6473out_free_nvs:
6474 kfree(wl->nvs);
6475
a390e85c 6476out:
6f8d6b20
IY
6477 release_firmware(fw);
6478 complete_all(&wl->nvs_loading_complete);
6479}
6480
b74324d1 6481int wlcore_probe(struct wl1271 *wl, struct platform_device *pdev)
6f8d6b20
IY
6482{
6483 int ret;
6484
6485 if (!wl->ops || !wl->ptable)
6486 return -EINVAL;
6487
6488 wl->dev = &pdev->dev;
6489 wl->pdev = pdev;
6490 platform_set_drvdata(pdev, wl);
6491
6492 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
6493 WL12XX_NVS_NAME, &pdev->dev, GFP_KERNEL,
6494 wl, wlcore_nvs_cb);
6495 if (ret < 0) {
6496 wl1271_error("request_firmware_nowait failed: %d", ret);
6497 complete_all(&wl->nvs_loading_complete);
6498 }
6499
a390e85c 6500 return ret;
ce2a217c 6501}
b2ba99ff 6502EXPORT_SYMBOL_GPL(wlcore_probe);
ce2a217c 6503
b74324d1 6504int wlcore_remove(struct platform_device *pdev)
ce2a217c 6505{
a390e85c
FB
6506 struct wl1271 *wl = platform_get_drvdata(pdev);
6507
6f8d6b20
IY
6508 wait_for_completion(&wl->nvs_loading_complete);
6509 if (!wl->initialized)
6510 return 0;
6511
a390e85c
FB
6512 if (wl->irq_wake_enabled) {
6513 device_init_wakeup(wl->dev, 0);
6514 disable_irq_wake(wl->irq);
6515 }
6516 wl1271_unregister_hw(wl);
6517 free_irq(wl->irq, wl);
ffeb501c 6518 wlcore_free_hw(wl);
a390e85c 6519
ce2a217c
FB
6520 return 0;
6521}
b2ba99ff 6522EXPORT_SYMBOL_GPL(wlcore_remove);
ce2a217c 6523
491bbd6b 6524u32 wl12xx_debug_level = DEBUG_NONE;
17c1755c 6525EXPORT_SYMBOL_GPL(wl12xx_debug_level);
491bbd6b 6526module_param_named(debug_level, wl12xx_debug_level, uint, S_IRUSR | S_IWUSR);
17c1755c
EP
6527MODULE_PARM_DESC(debug_level, "wl12xx debugging level");
6528
95dac04f 6529module_param_named(fwlog, fwlog_param, charp, 0);
2c882fa4 6530MODULE_PARM_DESC(fwlog,
95dac04f
IY
6531 "FW logger options: continuous, ondemand, dbgpins or disable");
6532
93ac8488
IR
6533module_param(fwlog_mem_blocks, int, S_IRUSR | S_IWUSR);
6534MODULE_PARM_DESC(fwlog_mem_blocks, "fwlog mem_blocks");
6535
7230341f 6536module_param(bug_on_recovery, int, S_IRUSR | S_IWUSR);
2a5bff09
EP
6537MODULE_PARM_DESC(bug_on_recovery, "BUG() on fw recovery");
6538
7230341f 6539module_param(no_recovery, int, S_IRUSR | S_IWUSR);
34785be5
AN
6540MODULE_PARM_DESC(no_recovery, "Prevent HW recovery. FW will remain stuck.");
6541
50b3eb4b 6542MODULE_LICENSE("GPL");
b1a48cab 6543MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
50b3eb4b 6544MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
0635ad45 6545MODULE_FIRMWARE(WL12XX_NVS_NAME);