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