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