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