wlcore: consolidate free_link and always call it
[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 */
baf6277a 1855 mutex_lock(&wl->mutex);
d935e385 1856 wl12xx_tx_reset(wl);
baf6277a
EP
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
5d979f35
AN
2417 if (wl->roc_vif == vif) {
2418 wl->roc_vif = NULL;
2419 ieee80211_remain_on_channel_expired(wl->hw);
2420 }
2421
b78b47eb
EP
2422 if (!test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags)) {
2423 /* disable active roles */
2424 ret = wl1271_ps_elp_wakeup(wl);
2425 if (ret < 0)
2426 goto deinit;
2427
b890f4c3
EP
2428 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2429 wlvif->bss_type == BSS_TYPE_IBSS) {
2430 if (wl12xx_dev_role_started(wlvif))
2431 wl12xx_stop_dev(wl, wlvif);
04e8079c
EP
2432 }
2433
0603d891 2434 ret = wl12xx_cmd_role_disable(wl, &wlvif->role_id);
b78b47eb
EP
2435 if (ret < 0)
2436 goto deinit;
2437
2438 wl1271_ps_elp_sleep(wl);
2439 }
2440deinit:
e51ae9be 2441 /* clear all hlids (except system_hlid) */
afaf8bdb 2442 wlvif->dev_hlid = WL12XX_INVALID_LINK_ID;
e5a359f8
EP
2443
2444 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2445 wlvif->bss_type == BSS_TYPE_IBSS) {
2446 wlvif->sta.hlid = WL12XX_INVALID_LINK_ID;
2447 wl12xx_free_rate_policy(wl, &wlvif->sta.basic_rate_idx);
2448 wl12xx_free_rate_policy(wl, &wlvif->sta.ap_rate_idx);
2449 wl12xx_free_rate_policy(wl, &wlvif->sta.p2p_rate_idx);
001e39a8 2450 wlcore_free_klv_template(wl, &wlvif->sta.klv_template_id);
e5a359f8
EP
2451 } else {
2452 wlvif->ap.bcast_hlid = WL12XX_INVALID_LINK_ID;
2453 wlvif->ap.global_hlid = WL12XX_INVALID_LINK_ID;
2454 wl12xx_free_rate_policy(wl, &wlvif->ap.mgmt_rate_idx);
2455 wl12xx_free_rate_policy(wl, &wlvif->ap.bcast_rate_idx);
2456 for (i = 0; i < CONF_TX_MAX_AC_COUNT; i++)
2457 wl12xx_free_rate_policy(wl,
2458 &wlvif->ap.ucast_rate_idx[i]);
830be7e0 2459 wl1271_free_ap_keys(wl, wlvif);
e5a359f8 2460 }
b78b47eb 2461
3eba4a0e
ES
2462 dev_kfree_skb(wlvif->probereq);
2463 wlvif->probereq = NULL;
d6a3cc2e 2464 wl12xx_tx_reset_wlvif(wl, wlvif);
e4120df9
EP
2465 if (wl->last_wlvif == wlvif)
2466 wl->last_wlvif = NULL;
87627214 2467 list_del(&wlvif->list);
c7ffb902 2468 memset(wlvif->ap.sta_hlid_map, 0, sizeof(wlvif->ap.sta_hlid_map));
0603d891 2469 wlvif->role_id = WL12XX_INVALID_ROLE_ID;
7edebf56 2470 wlvif->dev_role_id = WL12XX_INVALID_ROLE_ID;
d6e19d13 2471
2f18cf7c 2472 if (is_ap)
a4e4130d
EP
2473 wl->ap_count--;
2474 else
2475 wl->sta_count--;
2476
42066f9a
AN
2477 /*
2478 * Last AP, have more stations. Configure sleep auth according to STA.
2479 * Don't do thin on unintended recovery.
2480 */
2481 if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags) &&
2482 !test_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags))
2483 goto unlock;
2484
2f18cf7c
AN
2485 if (wl->ap_count == 0 && is_ap && wl->sta_count) {
2486 u8 sta_auth = wl->conf.conn.sta_sleep_auth;
2487 /* Configure for power according to debugfs */
2488 if (sta_auth != WL1271_PSM_ILLEGAL)
2489 wl1271_acx_sleep_auth(wl, sta_auth);
2f18cf7c
AN
2490 /* Configure for ELP power saving */
2491 else
2492 wl1271_acx_sleep_auth(wl, WL1271_PSM_ELP);
2493 }
2494
42066f9a 2495unlock:
baf6277a 2496 mutex_unlock(&wl->mutex);
d6bf9ada 2497
9eb599e9
EP
2498 del_timer_sync(&wlvif->rx_streaming_timer);
2499 cancel_work_sync(&wlvif->rx_streaming_enable_work);
2500 cancel_work_sync(&wlvif->rx_streaming_disable_work);
c50a2825 2501 cancel_delayed_work_sync(&wlvif->connection_loss_work);
bd9dc49c 2502
baf6277a 2503 mutex_lock(&wl->mutex);
52a2a375 2504}
bd9dc49c 2505
52a2a375
JO
2506static void wl1271_op_remove_interface(struct ieee80211_hw *hw,
2507 struct ieee80211_vif *vif)
2508{
2509 struct wl1271 *wl = hw->priv;
10c8cd01 2510 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
6e8cd331 2511 struct wl12xx_vif *iter;
4549d09c 2512 struct vif_counter_data vif_count;
52a2a375 2513
4549d09c 2514 wl12xx_get_vif_count(hw, vif, &vif_count);
52a2a375 2515 mutex_lock(&wl->mutex);
10c8cd01 2516
4cc53383 2517 if (wl->state == WLCORE_STATE_OFF ||
10c8cd01
EP
2518 !test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
2519 goto out;
2520
67353299
JO
2521 /*
2522 * wl->vif can be null here if someone shuts down the interface
2523 * just when hardware recovery has been started.
2524 */
6e8cd331
EP
2525 wl12xx_for_each_wlvif(wl, iter) {
2526 if (iter != wlvif)
2527 continue;
2528
536129c8 2529 __wl1271_op_remove_interface(wl, vif, true);
6e8cd331 2530 break;
67353299 2531 }
6e8cd331 2532 WARN_ON(iter != wlvif);
4549d09c 2533 if (wl12xx_need_fw_change(wl, vif_count, false)) {
3dee4393 2534 wl12xx_force_active_psm(wl);
e9ba7152 2535 set_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags);
4549d09c 2536 wl12xx_queue_recovery_work(wl);
4549d09c 2537 }
10c8cd01 2538out:
67353299 2539 mutex_unlock(&wl->mutex);
f5fc0f86
LC
2540}
2541
c0fad1b7
EP
2542static int wl12xx_op_change_interface(struct ieee80211_hw *hw,
2543 struct ieee80211_vif *vif,
2544 enum nl80211_iftype new_type, bool p2p)
2545{
4549d09c
EP
2546 struct wl1271 *wl = hw->priv;
2547 int ret;
2548
2549 set_bit(WL1271_FLAG_VIF_CHANGE_IN_PROGRESS, &wl->flags);
c0fad1b7
EP
2550 wl1271_op_remove_interface(hw, vif);
2551
249e9698 2552 vif->type = new_type;
c0fad1b7 2553 vif->p2p = p2p;
4549d09c
EP
2554 ret = wl1271_op_add_interface(hw, vif);
2555
2556 clear_bit(WL1271_FLAG_VIF_CHANGE_IN_PROGRESS, &wl->flags);
2557 return ret;
c0fad1b7
EP
2558}
2559
3230f35e 2560static int wlcore_join(struct wl1271 *wl, struct wl12xx_vif *wlvif)
82429d32
JO
2561{
2562 int ret;
536129c8 2563 bool is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
82429d32 2564
69e5434c
JO
2565 /*
2566 * One of the side effects of the JOIN command is that is clears
2567 * WPA/WPA2 keys from the chipset. Performing a JOIN while associated
2568 * to a WPA/WPA2 access point will therefore kill the data-path.
8bf69aae
OBC
2569 * Currently the only valid scenario for JOIN during association
2570 * is on roaming, in which case we will also be given new keys.
2571 * Keep the below message for now, unless it starts bothering
2572 * users who really like to roam a lot :)
69e5434c 2573 */
ba8447f6 2574 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
69e5434c
JO
2575 wl1271_info("JOIN while associated.");
2576
5ec8a448
EP
2577 /* clear encryption type */
2578 wlvif->encryption_type = KEY_NONE;
2579
227e81e1 2580 if (is_ibss)
87fbcb0f 2581 ret = wl12xx_cmd_role_start_ibss(wl, wlvif);
18eab430
EP
2582 else {
2583 if (wl->quirks & WLCORE_QUIRK_START_STA_FAILS) {
2584 /*
2585 * TODO: this is an ugly workaround for wl12xx fw
2586 * bug - we are not able to tx/rx after the first
2587 * start_sta, so make dummy start+stop calls,
2588 * and then call start_sta again.
2589 * this should be fixed in the fw.
2590 */
2591 wl12xx_cmd_role_start_sta(wl, wlvif);
2592 wl12xx_cmd_role_stop_sta(wl, wlvif);
2593 }
2594
87fbcb0f 2595 ret = wl12xx_cmd_role_start_sta(wl, wlvif);
18eab430 2596 }
3230f35e
EP
2597
2598 return ret;
2599}
2600
2601static int wl1271_ssid_set(struct wl12xx_vif *wlvif, struct sk_buff *skb,
2602 int offset)
2603{
2604 u8 ssid_len;
2605 const u8 *ptr = cfg80211_find_ie(WLAN_EID_SSID, skb->data + offset,
2606 skb->len - offset);
2607
2608 if (!ptr) {
2609 wl1271_error("No SSID in IEs!");
2610 return -ENOENT;
2611 }
2612
2613 ssid_len = ptr[1];
2614 if (ssid_len > IEEE80211_MAX_SSID_LEN) {
2615 wl1271_error("SSID is too long!");
2616 return -EINVAL;
2617 }
2618
2619 wlvif->ssid_len = ssid_len;
2620 memcpy(wlvif->ssid, ptr+2, ssid_len);
2621 return 0;
2622}
2623
2624static int wlcore_set_ssid(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2625{
2626 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
2627 struct sk_buff *skb;
2628 int ieoffset;
2629
2630 /* we currently only support setting the ssid from the ap probe req */
2631 if (wlvif->bss_type != BSS_TYPE_STA_BSS)
2632 return -EINVAL;
2633
2634 skb = ieee80211_ap_probereq_get(wl->hw, vif);
2635 if (!skb)
2636 return -EINVAL;
2637
2638 ieoffset = offsetof(struct ieee80211_mgmt,
2639 u.probe_req.variable);
2640 wl1271_ssid_set(wlvif, skb, ieoffset);
2641 dev_kfree_skb(skb);
2642
2643 return 0;
2644}
2645
2646static int wlcore_set_assoc(struct wl1271 *wl, struct wl12xx_vif *wlvif,
ec87011a
EP
2647 struct ieee80211_bss_conf *bss_conf,
2648 u32 sta_rate_set)
3230f35e
EP
2649{
2650 int ieoffset;
2651 int ret;
2652
2653 wlvif->aid = bss_conf->aid;
aaabee8b 2654 wlvif->channel_type = cfg80211_get_chandef_type(&bss_conf->chandef);
3230f35e 2655 wlvif->beacon_int = bss_conf->beacon_int;
d50529c0 2656 wlvif->wmm_enabled = bss_conf->qos;
3230f35e
EP
2657
2658 set_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags);
2659
2660 /*
2661 * with wl1271, we don't need to update the
2662 * beacon_int and dtim_period, because the firmware
2663 * updates it by itself when the first beacon is
2664 * received after a join.
2665 */
2666 ret = wl1271_cmd_build_ps_poll(wl, wlvif, wlvif->aid);
82429d32 2667 if (ret < 0)
3230f35e 2668 return ret;
82429d32 2669
3230f35e
EP
2670 /*
2671 * Get a template for hardware connection maintenance
2672 */
2673 dev_kfree_skb(wlvif->probereq);
2674 wlvif->probereq = wl1271_cmd_build_ap_probe_req(wl,
2675 wlvif,
2676 NULL);
2677 ieoffset = offsetof(struct ieee80211_mgmt,
2678 u.probe_req.variable);
2679 wl1271_ssid_set(wlvif, wlvif->probereq, ieoffset);
2680
2681 /* enable the connection monitoring feature */
2682 ret = wl1271_acx_conn_monit_params(wl, wlvif, true);
2683 if (ret < 0)
2684 return ret;
82429d32
JO
2685
2686 /*
2687 * The join command disable the keep-alive mode, shut down its process,
2688 * and also clear the template config, so we need to reset it all after
2689 * the join. The acx_aid starts the keep-alive process, and the order
2690 * of the commands below is relevant.
2691 */
0603d891 2692 ret = wl1271_acx_keep_alive_mode(wl, wlvif, true);
82429d32 2693 if (ret < 0)
3230f35e 2694 return ret;
82429d32 2695
0603d891 2696 ret = wl1271_acx_aid(wl, wlvif, wlvif->aid);
82429d32 2697 if (ret < 0)
3230f35e 2698 return ret;
82429d32 2699
d2d66c56 2700 ret = wl12xx_cmd_build_klv_null_data(wl, wlvif);
82429d32 2701 if (ret < 0)
3230f35e 2702 return ret;
82429d32 2703
0603d891 2704 ret = wl1271_acx_keep_alive_config(wl, wlvif,
001e39a8 2705 wlvif->sta.klv_template_id,
82429d32
JO
2706 ACX_KEEP_ALIVE_TPL_VALID);
2707 if (ret < 0)
3230f35e 2708 return ret;
82429d32 2709
6c7b5194
EP
2710 /*
2711 * The default fw psm configuration is AUTO, while mac80211 default
2712 * setting is off (ACTIVE), so sync the fw with the correct value.
2713 */
2714 ret = wl1271_ps_set_mode(wl, wlvif, STATION_ACTIVE_MODE);
ec87011a
EP
2715 if (ret < 0)
2716 return ret;
2717
2718 if (sta_rate_set) {
2719 wlvif->rate_set =
2720 wl1271_tx_enabled_rates_get(wl,
2721 sta_rate_set,
2722 wlvif->band);
2723 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
2724 if (ret < 0)
2725 return ret;
2726 }
82429d32 2727
82429d32
JO
2728 return ret;
2729}
2730
3230f35e 2731static int wlcore_unset_assoc(struct wl1271 *wl, struct wl12xx_vif *wlvif)
c7f43e45
LC
2732{
2733 int ret;
3230f35e
EP
2734 bool sta = wlvif->bss_type == BSS_TYPE_STA_BSS;
2735
2736 /* make sure we are connected (sta) joined */
2737 if (sta &&
2738 !test_and_clear_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
2739 return false;
2740
2741 /* make sure we are joined (ibss) */
2742 if (!sta &&
2743 test_and_clear_bit(WLVIF_FLAG_IBSS_JOINED, &wlvif->flags))
2744 return false;
2745
2746 if (sta) {
2747 /* use defaults when not associated */
2748 wlvif->aid = 0;
2749
2750 /* free probe-request template */
2751 dev_kfree_skb(wlvif->probereq);
2752 wlvif->probereq = NULL;
2753
2754 /* disable connection monitor features */
2755 ret = wl1271_acx_conn_monit_params(wl, wlvif, false);
2756 if (ret < 0)
2757 return ret;
2758
2759 /* Disable the keep-alive feature */
2760 ret = wl1271_acx_keep_alive_mode(wl, wlvif, false);
2761 if (ret < 0)
2762 return ret;
2763 }
c7f43e45 2764
52630c5d 2765 if (test_and_clear_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags)) {
6e8cd331
EP
2766 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
2767
fcab1890 2768 wl12xx_cmd_stop_channel_switch(wl, wlvif);
6e8cd331 2769 ieee80211_chswitch_done(vif, false);
c50a2825 2770 cancel_delayed_work(&wlvif->channel_switch_work);
6d158ff3
SL
2771 }
2772
4137c17c
EP
2773 /* invalidate keep-alive template */
2774 wl1271_acx_keep_alive_config(wl, wlvif,
001e39a8 2775 wlvif->sta.klv_template_id,
4137c17c
EP
2776 ACX_KEEP_ALIVE_TPL_INVALID);
2777
b992c682 2778 /* reset TX security counters on a clean disconnect */
48e93e40
EP
2779 wlvif->tx_security_last_seq_lsb = 0;
2780 wlvif->tx_security_seq = 0;
b992c682 2781
3230f35e 2782 return 0;
c7f43e45
LC
2783}
2784
87fbcb0f 2785static void wl1271_set_band_rate(struct wl1271 *wl, struct wl12xx_vif *wlvif)
ebba60c6 2786{
1b92f15e 2787 wlvif->basic_rate_set = wlvif->bitrate_masks[wlvif->band];
30d0c8fd 2788 wlvif->rate_set = wlvif->basic_rate_set;
ebba60c6
JO
2789}
2790
9f259c4e
EP
2791static int wl12xx_config_vif(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2792 struct ieee80211_conf *conf, u32 changed)
f5fc0f86 2793{
b6970ee5 2794 int ret;
f5fc0f86 2795
6bd65029 2796 if (conf->power_level != wlvif->power_level) {
0603d891 2797 ret = wl1271_acx_tx_power(wl, wlvif, conf->power_level);
f5fc0f86 2798 if (ret < 0)
9f259c4e 2799 return ret;
f5fc0f86 2800
6bd65029 2801 wlvif->power_level = conf->power_level;
f5fc0f86
LC
2802 }
2803
9f259c4e
EP
2804 return 0;
2805}
2806
2807static int wl1271_op_config(struct ieee80211_hw *hw, u32 changed)
2808{
2809 struct wl1271 *wl = hw->priv;
2810 struct wl12xx_vif *wlvif;
2811 struct ieee80211_conf *conf = &hw->conf;
b6970ee5 2812 int ret = 0;
9f259c4e 2813
b6970ee5 2814 wl1271_debug(DEBUG_MAC80211, "mac80211 config psm %s power %d %s"
9f259c4e 2815 " changed 0x%x",
9f259c4e
EP
2816 conf->flags & IEEE80211_CONF_PS ? "on" : "off",
2817 conf->power_level,
2818 conf->flags & IEEE80211_CONF_IDLE ? "idle" : "in use",
2819 changed);
2820
9f259c4e
EP
2821 mutex_lock(&wl->mutex);
2822
9f259c4e
EP
2823 if (changed & IEEE80211_CONF_CHANGE_POWER)
2824 wl->power_level = conf->power_level;
2825
4cc53383 2826 if (unlikely(wl->state != WLCORE_STATE_ON))
9f259c4e
EP
2827 goto out;
2828
2829 ret = wl1271_ps_elp_wakeup(wl);
2830 if (ret < 0)
2831 goto out;
2832
2833 /* configure each interface */
2834 wl12xx_for_each_wlvif(wl, wlvif) {
2835 ret = wl12xx_config_vif(wl, wlvif, conf, changed);
2836 if (ret < 0)
2837 goto out_sleep;
2838 }
2839
f5fc0f86
LC
2840out_sleep:
2841 wl1271_ps_elp_sleep(wl);
2842
2843out:
2844 mutex_unlock(&wl->mutex);
2845
2846 return ret;
2847}
2848
b54853f1
JO
2849struct wl1271_filter_params {
2850 bool enabled;
2851 int mc_list_length;
2852 u8 mc_list[ACX_MC_ADDRESS_GROUP_MAX][ETH_ALEN];
2853};
2854
22bedad3
JP
2855static u64 wl1271_op_prepare_multicast(struct ieee80211_hw *hw,
2856 struct netdev_hw_addr_list *mc_list)
c87dec9f 2857{
c87dec9f 2858 struct wl1271_filter_params *fp;
22bedad3 2859 struct netdev_hw_addr *ha;
c87dec9f 2860
74441130 2861 fp = kzalloc(sizeof(*fp), GFP_ATOMIC);
c87dec9f
JO
2862 if (!fp) {
2863 wl1271_error("Out of memory setting filters.");
2864 return 0;
2865 }
2866
2867 /* update multicast filtering parameters */
c87dec9f 2868 fp->mc_list_length = 0;
22bedad3
JP
2869 if (netdev_hw_addr_list_count(mc_list) > ACX_MC_ADDRESS_GROUP_MAX) {
2870 fp->enabled = false;
2871 } else {
2872 fp->enabled = true;
2873 netdev_hw_addr_list_for_each(ha, mc_list) {
c87dec9f 2874 memcpy(fp->mc_list[fp->mc_list_length],
22bedad3 2875 ha->addr, ETH_ALEN);
c87dec9f 2876 fp->mc_list_length++;
22bedad3 2877 }
c87dec9f
JO
2878 }
2879
b54853f1 2880 return (u64)(unsigned long)fp;
c87dec9f 2881}
f5fc0f86 2882
b54853f1
JO
2883#define WL1271_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
2884 FIF_ALLMULTI | \
2885 FIF_FCSFAIL | \
2886 FIF_BCN_PRBRESP_PROMISC | \
2887 FIF_CONTROL | \
2888 FIF_OTHER_BSS)
2889
f5fc0f86
LC
2890static void wl1271_op_configure_filter(struct ieee80211_hw *hw,
2891 unsigned int changed,
c87dec9f 2892 unsigned int *total, u64 multicast)
f5fc0f86 2893{
b54853f1 2894 struct wl1271_filter_params *fp = (void *)(unsigned long)multicast;
f5fc0f86 2895 struct wl1271 *wl = hw->priv;
6e8cd331 2896 struct wl12xx_vif *wlvif;
536129c8 2897
b54853f1 2898 int ret;
f5fc0f86 2899
7d057869
AN
2900 wl1271_debug(DEBUG_MAC80211, "mac80211 configure filter changed %x"
2901 " total %x", changed, *total);
f5fc0f86 2902
b54853f1
JO
2903 mutex_lock(&wl->mutex);
2904
2c10bb9c
SD
2905 *total &= WL1271_SUPPORTED_FILTERS;
2906 changed &= WL1271_SUPPORTED_FILTERS;
2907
4cc53383 2908 if (unlikely(wl->state != WLCORE_STATE_ON))
b54853f1
JO
2909 goto out;
2910
a620865e 2911 ret = wl1271_ps_elp_wakeup(wl);
b54853f1
JO
2912 if (ret < 0)
2913 goto out;
2914
6e8cd331
EP
2915 wl12xx_for_each_wlvif(wl, wlvif) {
2916 if (wlvif->bss_type != BSS_TYPE_AP_BSS) {
2917 if (*total & FIF_ALLMULTI)
2918 ret = wl1271_acx_group_address_tbl(wl, wlvif,
2919 false,
2920 NULL, 0);
2921 else if (fp)
2922 ret = wl1271_acx_group_address_tbl(wl, wlvif,
2923 fp->enabled,
2924 fp->mc_list,
2925 fp->mc_list_length);
2926 if (ret < 0)
2927 goto out_sleep;
2928 }
7d057869 2929 }
f5fc0f86 2930
08c1d1c7
EP
2931 /*
2932 * the fw doesn't provide an api to configure the filters. instead,
2933 * the filters configuration is based on the active roles / ROC
2934 * state.
2935 */
b54853f1
JO
2936
2937out_sleep:
2938 wl1271_ps_elp_sleep(wl);
2939
2940out:
2941 mutex_unlock(&wl->mutex);
14b228a0 2942 kfree(fp);
f5fc0f86
LC
2943}
2944
170d0e67
EP
2945static int wl1271_record_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2946 u8 id, u8 key_type, u8 key_size,
2947 const u8 *key, u8 hlid, u32 tx_seq_32,
2948 u16 tx_seq_16)
7f179b46
AN
2949{
2950 struct wl1271_ap_key *ap_key;
2951 int i;
2952
2953 wl1271_debug(DEBUG_CRYPT, "record ap key id %d", (int)id);
2954
2955 if (key_size > MAX_KEY_SIZE)
2956 return -EINVAL;
2957
2958 /*
2959 * Find next free entry in ap_keys. Also check we are not replacing
2960 * an existing key.
2961 */
2962 for (i = 0; i < MAX_NUM_KEYS; i++) {
170d0e67 2963 if (wlvif->ap.recorded_keys[i] == NULL)
7f179b46
AN
2964 break;
2965
170d0e67 2966 if (wlvif->ap.recorded_keys[i]->id == id) {
7f179b46
AN
2967 wl1271_warning("trying to record key replacement");
2968 return -EINVAL;
2969 }
2970 }
2971
2972 if (i == MAX_NUM_KEYS)
2973 return -EBUSY;
2974
2975 ap_key = kzalloc(sizeof(*ap_key), GFP_KERNEL);
2976 if (!ap_key)
2977 return -ENOMEM;
2978
2979 ap_key->id = id;
2980 ap_key->key_type = key_type;
2981 ap_key->key_size = key_size;
2982 memcpy(ap_key->key, key, key_size);
2983 ap_key->hlid = hlid;
2984 ap_key->tx_seq_32 = tx_seq_32;
2985 ap_key->tx_seq_16 = tx_seq_16;
2986
170d0e67 2987 wlvif->ap.recorded_keys[i] = ap_key;
7f179b46
AN
2988 return 0;
2989}
2990
170d0e67 2991static void wl1271_free_ap_keys(struct wl1271 *wl, struct wl12xx_vif *wlvif)
7f179b46
AN
2992{
2993 int i;
2994
2995 for (i = 0; i < MAX_NUM_KEYS; i++) {
170d0e67
EP
2996 kfree(wlvif->ap.recorded_keys[i]);
2997 wlvif->ap.recorded_keys[i] = NULL;
7f179b46
AN
2998 }
2999}
3000
a8ab39a4 3001static int wl1271_ap_init_hwenc(struct wl1271 *wl, struct wl12xx_vif *wlvif)
7f179b46
AN
3002{
3003 int i, ret = 0;
3004 struct wl1271_ap_key *key;
3005 bool wep_key_added = false;
3006
3007 for (i = 0; i < MAX_NUM_KEYS; i++) {
7f97b487 3008 u8 hlid;
170d0e67 3009 if (wlvif->ap.recorded_keys[i] == NULL)
7f179b46
AN
3010 break;
3011
170d0e67 3012 key = wlvif->ap.recorded_keys[i];
7f97b487
EP
3013 hlid = key->hlid;
3014 if (hlid == WL12XX_INVALID_LINK_ID)
a8ab39a4 3015 hlid = wlvif->ap.bcast_hlid;
7f97b487 3016
a8ab39a4 3017 ret = wl1271_cmd_set_ap_key(wl, wlvif, KEY_ADD_OR_REPLACE,
7f179b46
AN
3018 key->id, key->key_type,
3019 key->key_size, key->key,
7f97b487 3020 hlid, key->tx_seq_32,
7f179b46
AN
3021 key->tx_seq_16);
3022 if (ret < 0)
3023 goto out;
3024
3025 if (key->key_type == KEY_WEP)
3026 wep_key_added = true;
3027 }
3028
3029 if (wep_key_added) {
f75c753f 3030 ret = wl12xx_cmd_set_default_wep_key(wl, wlvif->default_key,
a8ab39a4 3031 wlvif->ap.bcast_hlid);
7f179b46
AN
3032 if (ret < 0)
3033 goto out;
3034 }
3035
3036out:
170d0e67 3037 wl1271_free_ap_keys(wl, wlvif);
7f179b46
AN
3038 return ret;
3039}
3040
536129c8
EP
3041static int wl1271_set_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
3042 u16 action, u8 id, u8 key_type,
7f179b46
AN
3043 u8 key_size, const u8 *key, u32 tx_seq_32,
3044 u16 tx_seq_16, struct ieee80211_sta *sta)
3045{
3046 int ret;
536129c8 3047 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
7f179b46
AN
3048
3049 if (is_ap) {
3050 struct wl1271_station *wl_sta;
3051 u8 hlid;
3052
3053 if (sta) {
3054 wl_sta = (struct wl1271_station *)sta->drv_priv;
3055 hlid = wl_sta->hlid;
3056 } else {
a8ab39a4 3057 hlid = wlvif->ap.bcast_hlid;
7f179b46
AN
3058 }
3059
53d40d0b 3060 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
7f179b46
AN
3061 /*
3062 * We do not support removing keys after AP shutdown.
3063 * Pretend we do to make mac80211 happy.
3064 */
3065 if (action != KEY_ADD_OR_REPLACE)
3066 return 0;
3067
170d0e67 3068 ret = wl1271_record_ap_key(wl, wlvif, id,
7f179b46
AN
3069 key_type, key_size,
3070 key, hlid, tx_seq_32,
3071 tx_seq_16);
3072 } else {
a8ab39a4 3073 ret = wl1271_cmd_set_ap_key(wl, wlvif, action,
7f179b46
AN
3074 id, key_type, key_size,
3075 key, hlid, tx_seq_32,
3076 tx_seq_16);
3077 }
3078
3079 if (ret < 0)
3080 return ret;
3081 } else {
3082 const u8 *addr;
3083 static const u8 bcast_addr[ETH_ALEN] = {
3084 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
3085 };
3086
3087 addr = sta ? sta->addr : bcast_addr;
3088
3089 if (is_zero_ether_addr(addr)) {
3090 /* We dont support TX only encryption */
3091 return -EOPNOTSUPP;
3092 }
3093
3094 /* The wl1271 does not allow to remove unicast keys - they
3095 will be cleared automatically on next CMD_JOIN. Ignore the
3096 request silently, as we dont want the mac80211 to emit
3097 an error message. */
3098 if (action == KEY_REMOVE && !is_broadcast_ether_addr(addr))
3099 return 0;
3100
010d3d30
EP
3101 /* don't remove key if hlid was already deleted */
3102 if (action == KEY_REMOVE &&
154da67c 3103 wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
010d3d30
EP
3104 return 0;
3105
a8ab39a4 3106 ret = wl1271_cmd_set_sta_key(wl, wlvif, action,
7f179b46
AN
3107 id, key_type, key_size,
3108 key, addr, tx_seq_32,
3109 tx_seq_16);
3110 if (ret < 0)
3111 return ret;
3112
3113 /* the default WEP key needs to be configured at least once */
3114 if (key_type == KEY_WEP) {
c690ec81 3115 ret = wl12xx_cmd_set_default_wep_key(wl,
f75c753f
EP
3116 wlvif->default_key,
3117 wlvif->sta.hlid);
7f179b46
AN
3118 if (ret < 0)
3119 return ret;
3120 }
3121 }
3122
3123 return 0;
3124}
3125
a1c597f2 3126static int wlcore_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
f5fc0f86
LC
3127 struct ieee80211_vif *vif,
3128 struct ieee80211_sta *sta,
3129 struct ieee80211_key_conf *key_conf)
3130{
3131 struct wl1271 *wl = hw->priv;
af390f4d
EP
3132 int ret;
3133 bool might_change_spare =
3134 key_conf->cipher == WL1271_CIPHER_SUITE_GEM ||
3135 key_conf->cipher == WLAN_CIPHER_SUITE_TKIP;
3136
3137 if (might_change_spare) {
3138 /*
3139 * stop the queues and flush to ensure the next packets are
3140 * in sync with FW spare block accounting
3141 */
3142 mutex_lock(&wl->mutex);
3143 wlcore_stop_queues(wl, WLCORE_QUEUE_STOP_REASON_SPARE_BLK);
3144 mutex_unlock(&wl->mutex);
3145
3146 wl1271_tx_flush(wl);
3147 }
3148
3149 mutex_lock(&wl->mutex);
3150
3151 if (unlikely(wl->state != WLCORE_STATE_ON)) {
3152 ret = -EAGAIN;
3153 goto out_wake_queues;
3154 }
a1c597f2 3155
af390f4d
EP
3156 ret = wl1271_ps_elp_wakeup(wl);
3157 if (ret < 0)
3158 goto out_wake_queues;
3159
3160 ret = wlcore_hw_set_key(wl, cmd, vif, sta, key_conf);
3161
3162 wl1271_ps_elp_sleep(wl);
3163
3164out_wake_queues:
3165 if (might_change_spare)
3166 wlcore_wake_queues(wl, WLCORE_QUEUE_STOP_REASON_SPARE_BLK);
3167
3168 mutex_unlock(&wl->mutex);
3169
3170 return ret;
a1c597f2
AN
3171}
3172
3173int wlcore_set_key(struct wl1271 *wl, enum set_key_cmd cmd,
3174 struct ieee80211_vif *vif,
3175 struct ieee80211_sta *sta,
3176 struct ieee80211_key_conf *key_conf)
3177{
536129c8 3178 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
f5fc0f86 3179 int ret;
ac4e4ce5
JO
3180 u32 tx_seq_32 = 0;
3181 u16 tx_seq_16 = 0;
f5fc0f86
LC
3182 u8 key_type;
3183
f5fc0f86
LC
3184 wl1271_debug(DEBUG_MAC80211, "mac80211 set key");
3185
7f179b46 3186 wl1271_debug(DEBUG_CRYPT, "CMD: 0x%x sta: %p", cmd, sta);
f5fc0f86 3187 wl1271_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
97359d12 3188 key_conf->cipher, key_conf->keyidx,
f5fc0f86
LC
3189 key_conf->keylen, key_conf->flags);
3190 wl1271_dump(DEBUG_CRYPT, "KEY: ", key_conf->key, key_conf->keylen);
3191
97359d12
JB
3192 switch (key_conf->cipher) {
3193 case WLAN_CIPHER_SUITE_WEP40:
3194 case WLAN_CIPHER_SUITE_WEP104:
f5fc0f86
LC
3195 key_type = KEY_WEP;
3196
3197 key_conf->hw_key_idx = key_conf->keyidx;
3198 break;
97359d12 3199 case WLAN_CIPHER_SUITE_TKIP:
f5fc0f86
LC
3200 key_type = KEY_TKIP;
3201
3202 key_conf->hw_key_idx = key_conf->keyidx;
48e93e40
EP
3203 tx_seq_32 = WL1271_TX_SECURITY_HI32(wlvif->tx_security_seq);
3204 tx_seq_16 = WL1271_TX_SECURITY_LO16(wlvif->tx_security_seq);
f5fc0f86 3205 break;
97359d12 3206 case WLAN_CIPHER_SUITE_CCMP:
f5fc0f86
LC
3207 key_type = KEY_AES;
3208
12d4b975 3209 key_conf->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
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);
f5fc0f86 3212 break;
7a55724e
JO
3213 case WL1271_CIPHER_SUITE_GEM:
3214 key_type = KEY_GEM;
48e93e40
EP
3215 tx_seq_32 = WL1271_TX_SECURITY_HI32(wlvif->tx_security_seq);
3216 tx_seq_16 = WL1271_TX_SECURITY_LO16(wlvif->tx_security_seq);
7a55724e 3217 break;
f5fc0f86 3218 default:
97359d12 3219 wl1271_error("Unknown key algo 0x%x", key_conf->cipher);
f5fc0f86 3220
af390f4d 3221 return -EOPNOTSUPP;
f5fc0f86
LC
3222 }
3223
3224 switch (cmd) {
3225 case SET_KEY:
536129c8 3226 ret = wl1271_set_key(wl, wlvif, KEY_ADD_OR_REPLACE,
7f179b46
AN
3227 key_conf->keyidx, key_type,
3228 key_conf->keylen, key_conf->key,
3229 tx_seq_32, tx_seq_16, sta);
f5fc0f86
LC
3230 if (ret < 0) {
3231 wl1271_error("Could not add or replace key");
af390f4d 3232 return ret;
f5fc0f86 3233 }
5ec8a448
EP
3234
3235 /*
3236 * reconfiguring arp response if the unicast (or common)
3237 * encryption key type was changed
3238 */
3239 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
3240 (sta || key_type == KEY_WEP) &&
3241 wlvif->encryption_type != key_type) {
3242 wlvif->encryption_type = key_type;
3243 ret = wl1271_cmd_build_arp_rsp(wl, wlvif);
3244 if (ret < 0) {
3245 wl1271_warning("build arp rsp failed: %d", ret);
af390f4d 3246 return ret;
5ec8a448
EP
3247 }
3248 }
f5fc0f86
LC
3249 break;
3250
3251 case DISABLE_KEY:
536129c8 3252 ret = wl1271_set_key(wl, wlvif, KEY_REMOVE,
7f179b46
AN
3253 key_conf->keyidx, key_type,
3254 key_conf->keylen, key_conf->key,
3255 0, 0, sta);
f5fc0f86
LC
3256 if (ret < 0) {
3257 wl1271_error("Could not remove key");
af390f4d 3258 return ret;
f5fc0f86
LC
3259 }
3260 break;
3261
3262 default:
3263 wl1271_error("Unsupported key cmd 0x%x", cmd);
af390f4d 3264 return -EOPNOTSUPP;
f5fc0f86
LC
3265 }
3266
f5fc0f86
LC
3267 return ret;
3268}
a1c597f2 3269EXPORT_SYMBOL_GPL(wlcore_set_key);
f5fc0f86 3270
6b70e7eb
VG
3271void wlcore_regdomain_config(struct wl1271 *wl)
3272{
3273 int ret;
3274
3275 if (!(wl->quirks & WLCORE_QUIRK_REGDOMAIN_CONF))
3276 return;
3277
3278 mutex_lock(&wl->mutex);
3279 ret = wl1271_ps_elp_wakeup(wl);
3280 if (ret < 0)
3281 goto out;
3282
3283 ret = wlcore_cmd_regdomain_config_locked(wl);
3284 if (ret < 0) {
3285 wl12xx_queue_recovery_work(wl);
3286 goto out;
3287 }
3288
3289 wl1271_ps_elp_sleep(wl);
3290out:
3291 mutex_unlock(&wl->mutex);
3292}
3293
f5fc0f86 3294static int wl1271_op_hw_scan(struct ieee80211_hw *hw,
a060bbfe 3295 struct ieee80211_vif *vif,
f5fc0f86
LC
3296 struct cfg80211_scan_request *req)
3297{
3298 struct wl1271 *wl = hw->priv;
3299 int ret;
3300 u8 *ssid = NULL;
abb0b3bf 3301 size_t len = 0;
f5fc0f86
LC
3302
3303 wl1271_debug(DEBUG_MAC80211, "mac80211 hw scan");
3304
3305 if (req->n_ssids) {
3306 ssid = req->ssids[0].ssid;
abb0b3bf 3307 len = req->ssids[0].ssid_len;
f5fc0f86
LC
3308 }
3309
3310 mutex_lock(&wl->mutex);
3311
4cc53383 3312 if (unlikely(wl->state != WLCORE_STATE_ON)) {
b739a42c
JO
3313 /*
3314 * We cannot return -EBUSY here because cfg80211 will expect
3315 * a call to ieee80211_scan_completed if we do - in this case
3316 * there won't be any call.
3317 */
3318 ret = -EAGAIN;
3319 goto out;
3320 }
3321
a620865e 3322 ret = wl1271_ps_elp_wakeup(wl);
f5fc0f86
LC
3323 if (ret < 0)
3324 goto out;
3325
97fd311a
EP
3326 /* fail if there is any role in ROC */
3327 if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES) {
92e712da
EP
3328 /* don't allow scanning right now */
3329 ret = -EBUSY;
3330 goto out_sleep;
3331 }
3332
78e28062 3333 ret = wlcore_scan(hw->priv, vif, ssid, len, req);
251c177f 3334out_sleep:
f5fc0f86 3335 wl1271_ps_elp_sleep(wl);
f5fc0f86
LC
3336out:
3337 mutex_unlock(&wl->mutex);
3338
3339 return ret;
3340}
3341
73ecce31
EP
3342static void wl1271_op_cancel_hw_scan(struct ieee80211_hw *hw,
3343 struct ieee80211_vif *vif)
3344{
3345 struct wl1271 *wl = hw->priv;
78e28062 3346 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
73ecce31
EP
3347 int ret;
3348
3349 wl1271_debug(DEBUG_MAC80211, "mac80211 cancel hw scan");
3350
3351 mutex_lock(&wl->mutex);
3352
4cc53383 3353 if (unlikely(wl->state != WLCORE_STATE_ON))
73ecce31
EP
3354 goto out;
3355
3356 if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
3357 goto out;
3358
3359 ret = wl1271_ps_elp_wakeup(wl);
3360 if (ret < 0)
3361 goto out;
3362
3363 if (wl->scan.state != WL1271_SCAN_STATE_DONE) {
78e28062 3364 ret = wl->ops->scan_stop(wl, wlvif);
73ecce31
EP
3365 if (ret < 0)
3366 goto out_sleep;
3367 }
55df5afb
AN
3368
3369 /*
3370 * Rearm the tx watchdog just before idling scan. This
3371 * prevents just-finished scans from triggering the watchdog
3372 */
3373 wl12xx_rearm_tx_watchdog_locked(wl);
3374
73ecce31
EP
3375 wl->scan.state = WL1271_SCAN_STATE_IDLE;
3376 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
c50a2825 3377 wl->scan_wlvif = NULL;
73ecce31
EP
3378 wl->scan.req = NULL;
3379 ieee80211_scan_completed(wl->hw, true);
3380
3381out_sleep:
3382 wl1271_ps_elp_sleep(wl);
3383out:
3384 mutex_unlock(&wl->mutex);
3385
3386 cancel_delayed_work_sync(&wl->scan_complete_work);
3387}
3388
33c2c06c
LC
3389static int wl1271_op_sched_scan_start(struct ieee80211_hw *hw,
3390 struct ieee80211_vif *vif,
3391 struct cfg80211_sched_scan_request *req,
3392 struct ieee80211_sched_scan_ies *ies)
3393{
3394 struct wl1271 *wl = hw->priv;
536129c8 3395 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
33c2c06c
LC
3396 int ret;
3397
3398 wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_start");
3399
3400 mutex_lock(&wl->mutex);
3401
4cc53383 3402 if (unlikely(wl->state != WLCORE_STATE_ON)) {
9e0dc890
PF
3403 ret = -EAGAIN;
3404 goto out;
3405 }
3406
33c2c06c
LC
3407 ret = wl1271_ps_elp_wakeup(wl);
3408 if (ret < 0)
3409 goto out;
3410
78e28062 3411 ret = wl->ops->sched_scan_start(wl, wlvif, req, ies);
33c2c06c
LC
3412 if (ret < 0)
3413 goto out_sleep;
3414
10199756 3415 wl->sched_vif = wlvif;
33c2c06c
LC
3416
3417out_sleep:
3418 wl1271_ps_elp_sleep(wl);
3419out:
3420 mutex_unlock(&wl->mutex);
3421 return ret;
3422}
3423
3424static void wl1271_op_sched_scan_stop(struct ieee80211_hw *hw,
3425 struct ieee80211_vif *vif)
3426{
3427 struct wl1271 *wl = hw->priv;
78f85f50 3428 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
33c2c06c
LC
3429 int ret;
3430
3431 wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_stop");
3432
3433 mutex_lock(&wl->mutex);
3434
4cc53383 3435 if (unlikely(wl->state != WLCORE_STATE_ON))
9e0dc890
PF
3436 goto out;
3437
33c2c06c
LC
3438 ret = wl1271_ps_elp_wakeup(wl);
3439 if (ret < 0)
3440 goto out;
3441
78e28062 3442 wl->ops->sched_scan_stop(wl, wlvif);
33c2c06c
LC
3443
3444 wl1271_ps_elp_sleep(wl);
3445out:
3446 mutex_unlock(&wl->mutex);
3447}
3448
68d069c4
AN
3449static int wl1271_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
3450{
3451 struct wl1271 *wl = hw->priv;
3452 int ret = 0;
3453
3454 mutex_lock(&wl->mutex);
3455
4cc53383 3456 if (unlikely(wl->state != WLCORE_STATE_ON)) {
68d069c4
AN
3457 ret = -EAGAIN;
3458 goto out;
3459 }
3460
a620865e 3461 ret = wl1271_ps_elp_wakeup(wl);
68d069c4
AN
3462 if (ret < 0)
3463 goto out;
3464
5f704d18 3465 ret = wl1271_acx_frag_threshold(wl, value);
68d069c4
AN
3466 if (ret < 0)
3467 wl1271_warning("wl1271_op_set_frag_threshold failed: %d", ret);
3468
3469 wl1271_ps_elp_sleep(wl);
3470
3471out:
3472 mutex_unlock(&wl->mutex);
3473
3474 return ret;
3475}
3476
f5fc0f86
LC
3477static int wl1271_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3478{
3479 struct wl1271 *wl = hw->priv;
6e8cd331 3480 struct wl12xx_vif *wlvif;
aecb0565 3481 int ret = 0;
f5fc0f86
LC
3482
3483 mutex_lock(&wl->mutex);
3484
4cc53383 3485 if (unlikely(wl->state != WLCORE_STATE_ON)) {
f8d9802f 3486 ret = -EAGAIN;
aecb0565 3487 goto out;
f8d9802f 3488 }
aecb0565 3489
a620865e 3490 ret = wl1271_ps_elp_wakeup(wl);
f5fc0f86
LC
3491 if (ret < 0)
3492 goto out;
3493
6e8cd331
EP
3494 wl12xx_for_each_wlvif(wl, wlvif) {
3495 ret = wl1271_acx_rts_threshold(wl, wlvif, value);
3496 if (ret < 0)
3497 wl1271_warning("set rts threshold failed: %d", ret);
3498 }
f5fc0f86
LC
3499 wl1271_ps_elp_sleep(wl);
3500
3501out:
3502 mutex_unlock(&wl->mutex);
3503
3504 return ret;
3505}
3506
d48055d9
EP
3507static void wl12xx_remove_ie(struct sk_buff *skb, u8 eid, int ieoffset)
3508{
3509 int len;
3510 const u8 *next, *end = skb->data + skb->len;
3511 u8 *ie = (u8 *)cfg80211_find_ie(eid, skb->data + ieoffset,
3512 skb->len - ieoffset);
3513 if (!ie)
3514 return;
3515 len = ie[1] + 2;
3516 next = ie + len;
3517 memmove(ie, next, end - next);
3518 skb_trim(skb, skb->len - len);
3519}
3520
26b4bf2e
EP
3521static void wl12xx_remove_vendor_ie(struct sk_buff *skb,
3522 unsigned int oui, u8 oui_type,
3523 int ieoffset)
3524{
3525 int len;
3526 const u8 *next, *end = skb->data + skb->len;
3527 u8 *ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
3528 skb->data + ieoffset,
3529 skb->len - ieoffset);
3530 if (!ie)
3531 return;
3532 len = ie[1] + 2;
3533 next = ie + len;
3534 memmove(ie, next, end - next);
3535 skb_trim(skb, skb->len - len);
3536}
3537
341f2c11
AN
3538static int wl1271_ap_set_probe_resp_tmpl(struct wl1271 *wl, u32 rates,
3539 struct ieee80211_vif *vif)
560f0024 3540{
cdaac628 3541 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
560f0024
AN
3542 struct sk_buff *skb;
3543 int ret;
3544
341f2c11 3545 skb = ieee80211_proberesp_get(wl->hw, vif);
560f0024 3546 if (!skb)
341f2c11 3547 return -EOPNOTSUPP;
560f0024 3548
cdaac628 3549 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
560f0024
AN
3550 CMD_TEMPL_AP_PROBE_RESPONSE,
3551 skb->data,
3552 skb->len, 0,
3553 rates);
560f0024 3554 dev_kfree_skb(skb);
62c2e579
LC
3555
3556 if (ret < 0)
3557 goto out;
3558
3559 wl1271_debug(DEBUG_AP, "probe response updated");
3560 set_bit(WLVIF_FLAG_AP_PROBE_RESP_SET, &wlvif->flags);
3561
3562out:
560f0024
AN
3563 return ret;
3564}
3565
3566static int wl1271_ap_set_probe_resp_tmpl_legacy(struct wl1271 *wl,
3567 struct ieee80211_vif *vif,
3568 u8 *probe_rsp_data,
3569 size_t probe_rsp_len,
3570 u32 rates)
68eaaf6e 3571{
1fe9f161
EP
3572 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3573 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
68eaaf6e
AN
3574 u8 probe_rsp_templ[WL1271_CMD_TEMPL_MAX_SIZE];
3575 int ssid_ie_offset, ie_offset, templ_len;
3576 const u8 *ptr;
3577
3578 /* no need to change probe response if the SSID is set correctly */
1fe9f161 3579 if (wlvif->ssid_len > 0)
cdaac628 3580 return wl1271_cmd_template_set(wl, wlvif->role_id,
68eaaf6e
AN
3581 CMD_TEMPL_AP_PROBE_RESPONSE,
3582 probe_rsp_data,
3583 probe_rsp_len, 0,
3584 rates);
3585
3586 if (probe_rsp_len + bss_conf->ssid_len > WL1271_CMD_TEMPL_MAX_SIZE) {
3587 wl1271_error("probe_rsp template too big");
3588 return -EINVAL;
3589 }
3590
3591 /* start searching from IE offset */
3592 ie_offset = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
3593
3594 ptr = cfg80211_find_ie(WLAN_EID_SSID, probe_rsp_data + ie_offset,
3595 probe_rsp_len - ie_offset);
3596 if (!ptr) {
3597 wl1271_error("No SSID in beacon!");
3598 return -EINVAL;
3599 }
3600
3601 ssid_ie_offset = ptr - probe_rsp_data;
3602 ptr += (ptr[1] + 2);
3603
3604 memcpy(probe_rsp_templ, probe_rsp_data, ssid_ie_offset);
3605
3606 /* insert SSID from bss_conf */
3607 probe_rsp_templ[ssid_ie_offset] = WLAN_EID_SSID;
3608 probe_rsp_templ[ssid_ie_offset + 1] = bss_conf->ssid_len;
3609 memcpy(probe_rsp_templ + ssid_ie_offset + 2,
3610 bss_conf->ssid, bss_conf->ssid_len);
3611 templ_len = ssid_ie_offset + 2 + bss_conf->ssid_len;
3612
3613 memcpy(probe_rsp_templ + ssid_ie_offset + 2 + bss_conf->ssid_len,
3614 ptr, probe_rsp_len - (ptr - probe_rsp_data));
3615 templ_len += probe_rsp_len - (ptr - probe_rsp_data);
3616
cdaac628 3617 return wl1271_cmd_template_set(wl, wlvif->role_id,
68eaaf6e
AN
3618 CMD_TEMPL_AP_PROBE_RESPONSE,
3619 probe_rsp_templ,
3620 templ_len, 0,
3621 rates);
3622}
3623
e78a287a 3624static int wl1271_bss_erp_info_changed(struct wl1271 *wl,
0603d891 3625 struct ieee80211_vif *vif,
f5fc0f86
LC
3626 struct ieee80211_bss_conf *bss_conf,
3627 u32 changed)
3628{
0603d891 3629 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
e78a287a 3630 int ret = 0;
f5fc0f86 3631
e78a287a
AN
3632 if (changed & BSS_CHANGED_ERP_SLOT) {
3633 if (bss_conf->use_short_slot)
0603d891 3634 ret = wl1271_acx_slot(wl, wlvif, SLOT_TIME_SHORT);
e78a287a 3635 else
0603d891 3636 ret = wl1271_acx_slot(wl, wlvif, SLOT_TIME_LONG);
e78a287a
AN
3637 if (ret < 0) {
3638 wl1271_warning("Set slot time failed %d", ret);
3639 goto out;
3640 }
3641 }
f5fc0f86 3642
e78a287a
AN
3643 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3644 if (bss_conf->use_short_preamble)
0603d891 3645 wl1271_acx_set_preamble(wl, wlvif, ACX_PREAMBLE_SHORT);
e78a287a 3646 else
0603d891 3647 wl1271_acx_set_preamble(wl, wlvif, ACX_PREAMBLE_LONG);
e78a287a 3648 }
f5fc0f86 3649
e78a287a
AN
3650 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
3651 if (bss_conf->use_cts_prot)
0603d891
EP
3652 ret = wl1271_acx_cts_protect(wl, wlvif,
3653 CTSPROTECT_ENABLE);
e78a287a 3654 else
0603d891
EP
3655 ret = wl1271_acx_cts_protect(wl, wlvif,
3656 CTSPROTECT_DISABLE);
e78a287a
AN
3657 if (ret < 0) {
3658 wl1271_warning("Set ctsprotect failed %d", ret);
3659 goto out;
3660 }
3661 }
f8d9802f 3662
e78a287a
AN
3663out:
3664 return ret;
3665}
f5fc0f86 3666
62c2e579
LC
3667static int wlcore_set_beacon_template(struct wl1271 *wl,
3668 struct ieee80211_vif *vif,
3669 bool is_ap)
3670{
3671 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3672 struct ieee80211_hdr *hdr;
3673 u32 min_rate;
3674 int ret;
3675 int ieoffset = offsetof(struct ieee80211_mgmt,
3676 u.beacon.variable);
3677 struct sk_buff *beacon = ieee80211_beacon_get(wl->hw, vif);
3678 u16 tmpl_id;
3679
3680 if (!beacon) {
3681 ret = -EINVAL;
3682 goto out;
3683 }
3684
3685 wl1271_debug(DEBUG_MASTER, "beacon updated");
3686
3230f35e 3687 ret = wl1271_ssid_set(wlvif, beacon, ieoffset);
62c2e579
LC
3688 if (ret < 0) {
3689 dev_kfree_skb(beacon);
3690 goto out;
3691 }
3692 min_rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
3693 tmpl_id = is_ap ? CMD_TEMPL_AP_BEACON :
3694 CMD_TEMPL_BEACON;
3695 ret = wl1271_cmd_template_set(wl, wlvif->role_id, tmpl_id,
3696 beacon->data,
3697 beacon->len, 0,
3698 min_rate);
3699 if (ret < 0) {
3700 dev_kfree_skb(beacon);
3701 goto out;
3702 }
3703
d50529c0
EP
3704 wlvif->wmm_enabled =
3705 cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
3706 WLAN_OUI_TYPE_MICROSOFT_WMM,
3707 beacon->data + ieoffset,
3708 beacon->len - ieoffset);
3709
62c2e579
LC
3710 /*
3711 * In case we already have a probe-resp beacon set explicitly
3712 * by usermode, don't use the beacon data.
3713 */
3714 if (test_bit(WLVIF_FLAG_AP_PROBE_RESP_SET, &wlvif->flags))
3715 goto end_bcn;
3716
3717 /* remove TIM ie from probe response */
3718 wl12xx_remove_ie(beacon, WLAN_EID_TIM, ieoffset);
3719
3720 /*
3721 * remove p2p ie from probe response.
3722 * the fw reponds to probe requests that don't include
3723 * the p2p ie. probe requests with p2p ie will be passed,
3724 * and will be responded by the supplicant (the spec
3725 * forbids including the p2p ie when responding to probe
3726 * requests that didn't include it).
3727 */
3728 wl12xx_remove_vendor_ie(beacon, WLAN_OUI_WFA,
3729 WLAN_OUI_TYPE_WFA_P2P, ieoffset);
3730
3731 hdr = (struct ieee80211_hdr *) beacon->data;
3732 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3733 IEEE80211_STYPE_PROBE_RESP);
3734 if (is_ap)
3735 ret = wl1271_ap_set_probe_resp_tmpl_legacy(wl, vif,
3736 beacon->data,
3737 beacon->len,
3738 min_rate);
3739 else
3740 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
3741 CMD_TEMPL_PROBE_RESPONSE,
3742 beacon->data,
3743 beacon->len, 0,
3744 min_rate);
3745end_bcn:
3746 dev_kfree_skb(beacon);
3747 if (ret < 0)
3748 goto out;
3749
3750out:
3751 return ret;
3752}
3753
e78a287a
AN
3754static int wl1271_bss_beacon_info_changed(struct wl1271 *wl,
3755 struct ieee80211_vif *vif,
3756 struct ieee80211_bss_conf *bss_conf,
3757 u32 changed)
3758{
87fbcb0f 3759 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
536129c8 3760 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
e78a287a
AN
3761 int ret = 0;
3762
48af2eb0 3763 if (changed & BSS_CHANGED_BEACON_INT) {
e78a287a 3764 wl1271_debug(DEBUG_MASTER, "beacon interval updated: %d",
60e84c2e
JO
3765 bss_conf->beacon_int);
3766
6a899796 3767 wlvif->beacon_int = bss_conf->beacon_int;
60e84c2e
JO
3768 }
3769
560f0024
AN
3770 if ((changed & BSS_CHANGED_AP_PROBE_RESP) && is_ap) {
3771 u32 rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
62c2e579
LC
3772
3773 wl1271_ap_set_probe_resp_tmpl(wl, rate, vif);
560f0024
AN
3774 }
3775
48af2eb0 3776 if (changed & BSS_CHANGED_BEACON) {
62c2e579 3777 ret = wlcore_set_beacon_template(wl, vif, is_ap);
e78a287a
AN
3778 if (ret < 0)
3779 goto out;
3780 }
3781
3782out:
560f0024
AN
3783 if (ret != 0)
3784 wl1271_error("beacon info change failed: %d", ret);
e78a287a
AN
3785 return ret;
3786}
3787
3788/* AP mode changes */
3789static void wl1271_bss_info_changed_ap(struct wl1271 *wl,
3790 struct ieee80211_vif *vif,
3791 struct ieee80211_bss_conf *bss_conf,
3792 u32 changed)
3793{
87fbcb0f 3794 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
e78a287a 3795 int ret = 0;
e0d8bbf0 3796
b6970ee5 3797 if (changed & BSS_CHANGED_BASIC_RATES) {
e78a287a 3798 u32 rates = bss_conf->basic_rates;
5da11dcd 3799
87fbcb0f 3800 wlvif->basic_rate_set = wl1271_tx_enabled_rates_get(wl, rates,
1b92f15e 3801 wlvif->band);
d2d66c56 3802 wlvif->basic_rate = wl1271_tx_min_rate_get(wl,
87fbcb0f 3803 wlvif->basic_rate_set);
70f47424 3804
87fbcb0f 3805 ret = wl1271_init_ap_rates(wl, wlvif);
e78a287a 3806 if (ret < 0) {
70f47424 3807 wl1271_error("AP rate policy change failed %d", ret);
e78a287a
AN
3808 goto out;
3809 }
c45a85b5 3810
784f694d 3811 ret = wl1271_ap_init_templates(wl, vif);
c45a85b5
AN
3812 if (ret < 0)
3813 goto out;
62c2e579
LC
3814
3815 ret = wl1271_ap_set_probe_resp_tmpl(wl, wlvif->basic_rate, vif);
3816 if (ret < 0)
3817 goto out;
3818
3819 ret = wlcore_set_beacon_template(wl, vif, true);
3820 if (ret < 0)
3821 goto out;
e78a287a 3822 }
2f6724b2 3823
e78a287a
AN
3824 ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf, changed);
3825 if (ret < 0)
3826 goto out;
30240fc7 3827
48af2eb0 3828 if (changed & BSS_CHANGED_BEACON_ENABLED) {
e78a287a 3829 if (bss_conf->enable_beacon) {
53d40d0b 3830 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
87fbcb0f 3831 ret = wl12xx_cmd_role_start_ap(wl, wlvif);
e78a287a
AN
3832 if (ret < 0)
3833 goto out;
e0d8bbf0 3834
a8ab39a4 3835 ret = wl1271_ap_init_hwenc(wl, wlvif);
7f179b46
AN
3836 if (ret < 0)
3837 goto out;
cf42039f 3838
53d40d0b 3839 set_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags);
cf42039f 3840 wl1271_debug(DEBUG_AP, "started AP");
e0d8bbf0 3841 }
e78a287a 3842 } else {
53d40d0b 3843 if (test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
0603d891 3844 ret = wl12xx_cmd_role_stop_ap(wl, wlvif);
e78a287a
AN
3845 if (ret < 0)
3846 goto out;
e0d8bbf0 3847
53d40d0b 3848 clear_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags);
560f0024
AN
3849 clear_bit(WLVIF_FLAG_AP_PROBE_RESP_SET,
3850 &wlvif->flags);
e78a287a
AN
3851 wl1271_debug(DEBUG_AP, "stopped AP");
3852 }
3853 }
3854 }
e0d8bbf0 3855
0603d891 3856 ret = wl1271_bss_erp_info_changed(wl, vif, bss_conf, changed);
e78a287a
AN
3857 if (ret < 0)
3858 goto out;
0b932ab9
AN
3859
3860 /* Handle HT information change */
3861 if ((changed & BSS_CHANGED_HT) &&
4bf88530 3862 (bss_conf->chandef.width != NL80211_CHAN_WIDTH_20_NOHT)) {
0603d891 3863 ret = wl1271_acx_set_ht_information(wl, wlvif,
0b932ab9
AN
3864 bss_conf->ht_operation_mode);
3865 if (ret < 0) {
3866 wl1271_warning("Set ht information failed %d", ret);
3867 goto out;
3868 }
3869 }
3870
e78a287a
AN
3871out:
3872 return;
3873}
8bf29b0e 3874
3230f35e
EP
3875static int wlcore_set_bssid(struct wl1271 *wl, struct wl12xx_vif *wlvif,
3876 struct ieee80211_bss_conf *bss_conf,
3877 u32 sta_rate_set)
3878{
3879 u32 rates;
3880 int ret;
3881
3882 wl1271_debug(DEBUG_MAC80211,
3883 "changed_bssid: %pM, aid: %d, bcn_int: %d, brates: 0x%x sta_rate_set: 0x%x",
3884 bss_conf->bssid, bss_conf->aid,
3885 bss_conf->beacon_int,
3886 bss_conf->basic_rates, sta_rate_set);
3887
3888 wlvif->beacon_int = bss_conf->beacon_int;
3889 rates = bss_conf->basic_rates;
3890 wlvif->basic_rate_set =
3891 wl1271_tx_enabled_rates_get(wl, rates,
3892 wlvif->band);
3893 wlvif->basic_rate =
3894 wl1271_tx_min_rate_get(wl,
3895 wlvif->basic_rate_set);
3896
3897 if (sta_rate_set)
3898 wlvif->rate_set =
3899 wl1271_tx_enabled_rates_get(wl,
3900 sta_rate_set,
3901 wlvif->band);
3902
3903 /* we only support sched_scan while not connected */
10199756 3904 if (wl->sched_vif == wlvif)
78e28062 3905 wl->ops->sched_scan_stop(wl, wlvif);
3230f35e
EP
3906
3907 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
3908 if (ret < 0)
3909 return ret;
3910
3911 ret = wl12xx_cmd_build_null_data(wl, wlvif);
3912 if (ret < 0)
3913 return ret;
3914
3915 ret = wl1271_build_qos_null_data(wl, wl12xx_wlvif_to_vif(wlvif));
3916 if (ret < 0)
3917 return ret;
3918
3919 wlcore_set_ssid(wl, wlvif);
3920
3921 set_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
3922
3923 return 0;
3924}
3925
3926static int wlcore_clear_bssid(struct wl1271 *wl, struct wl12xx_vif *wlvif)
3927{
3928 int ret;
3929
3930 /* revert back to minimum rates for the current band */
3931 wl1271_set_band_rate(wl, wlvif);
3932 wlvif->basic_rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
3933
3934 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
3935 if (ret < 0)
3936 return ret;
3937
3938 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
3939 test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags)) {
3940 ret = wl12xx_cmd_role_stop_sta(wl, wlvif);
3941 if (ret < 0)
3942 return ret;
3943 }
3944
3945 clear_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
3946 return 0;
3947}
e78a287a
AN
3948/* STA/IBSS mode changes */
3949static void wl1271_bss_info_changed_sta(struct wl1271 *wl,
3950 struct ieee80211_vif *vif,
3951 struct ieee80211_bss_conf *bss_conf,
3952 u32 changed)
3953{
87fbcb0f 3954 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3230f35e 3955 bool do_join = false;
536129c8 3956 bool is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
227e81e1 3957 bool ibss_joined = false;
72c2d9e5 3958 u32 sta_rate_set = 0;
e78a287a 3959 int ret;
2d6e4e76 3960 struct ieee80211_sta *sta;
a100885d
AN
3961 bool sta_exists = false;
3962 struct ieee80211_sta_ht_cap sta_ht_cap;
e78a287a
AN
3963
3964 if (is_ibss) {
3965 ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf,
3966 changed);
3967 if (ret < 0)
3968 goto out;
e0d8bbf0
JO
3969 }
3970
227e81e1
EP
3971 if (changed & BSS_CHANGED_IBSS) {
3972 if (bss_conf->ibss_joined) {
eee514e3 3973 set_bit(WLVIF_FLAG_IBSS_JOINED, &wlvif->flags);
227e81e1
EP
3974 ibss_joined = true;
3975 } else {
3230f35e
EP
3976 wlcore_unset_assoc(wl, wlvif);
3977 wl12xx_cmd_role_stop_sta(wl, wlvif);
227e81e1
EP
3978 }
3979 }
3980
3981 if ((changed & BSS_CHANGED_BEACON_INT) && ibss_joined)
e78a287a
AN
3982 do_join = true;
3983
3984 /* Need to update the SSID (for filtering etc) */
227e81e1 3985 if ((changed & BSS_CHANGED_BEACON) && ibss_joined)
e78a287a
AN
3986 do_join = true;
3987
227e81e1 3988 if ((changed & BSS_CHANGED_BEACON_ENABLED) && ibss_joined) {
5da11dcd
JO
3989 wl1271_debug(DEBUG_ADHOC, "ad-hoc beaconing: %s",
3990 bss_conf->enable_beacon ? "enabled" : "disabled");
3991
5da11dcd
JO
3992 do_join = true;
3993 }
3994
48af2eb0 3995 if (changed & BSS_CHANGED_CQM) {
00236aed
JO
3996 bool enable = false;
3997 if (bss_conf->cqm_rssi_thold)
3998 enable = true;
0603d891 3999 ret = wl1271_acx_rssi_snr_trigger(wl, wlvif, enable,
00236aed
JO
4000 bss_conf->cqm_rssi_thold,
4001 bss_conf->cqm_rssi_hyst);
4002 if (ret < 0)
4003 goto out;
04324d99 4004 wlvif->rssi_thold = bss_conf->cqm_rssi_thold;
00236aed
JO
4005 }
4006
ec87011a
EP
4007 if (changed & (BSS_CHANGED_BSSID | BSS_CHANGED_HT |
4008 BSS_CHANGED_ASSOC)) {
0f9c8250
AN
4009 rcu_read_lock();
4010 sta = ieee80211_find_sta(vif, bss_conf->bssid);
ef08d028
LC
4011 if (sta) {
4012 u8 *rx_mask = sta->ht_cap.mcs.rx_mask;
4013
4014 /* save the supp_rates of the ap */
4015 sta_rate_set = sta->supp_rates[wlvif->band];
4016 if (sta->ht_cap.ht_supported)
4017 sta_rate_set |=
4018 (rx_mask[0] << HW_HT_RATES_OFFSET) |
4019 (rx_mask[1] << HW_MIMO_RATES_OFFSET);
4020 sta_ht_cap = sta->ht_cap;
4021 sta_exists = true;
4022 }
4023
0f9c8250 4024 rcu_read_unlock();
72c2d9e5 4025 }
72c2d9e5 4026
3230f35e
EP
4027 if (changed & BSS_CHANGED_BSSID) {
4028 if (!is_zero_ether_addr(bss_conf->bssid)) {
4029 ret = wlcore_set_bssid(wl, wlvif, bss_conf,
4030 sta_rate_set);
f5fc0f86 4031 if (ret < 0)
e78a287a 4032 goto out;
f5fc0f86 4033
3230f35e
EP
4034 /* Need to update the BSSID (for filtering etc) */
4035 do_join = true;
d94cd297 4036 } else {
3230f35e 4037 ret = wlcore_clear_bssid(wl, wlvif);
6ccbb92e 4038 if (ret < 0)
e78a287a 4039 goto out;
f5fc0f86 4040 }
f5fc0f86
LC
4041 }
4042
d192d268
EP
4043 if (changed & BSS_CHANGED_IBSS) {
4044 wl1271_debug(DEBUG_ADHOC, "ibss_joined: %d",
4045 bss_conf->ibss_joined);
4046
4047 if (bss_conf->ibss_joined) {
4048 u32 rates = bss_conf->basic_rates;
87fbcb0f 4049 wlvif->basic_rate_set =
af7fbb28 4050 wl1271_tx_enabled_rates_get(wl, rates,
1b92f15e 4051 wlvif->band);
d2d66c56 4052 wlvif->basic_rate =
87fbcb0f
EP
4053 wl1271_tx_min_rate_get(wl,
4054 wlvif->basic_rate_set);
d192d268 4055
06b660e1 4056 /* by default, use 11b + OFDM rates */
30d0c8fd
EP
4057 wlvif->rate_set = CONF_TX_IBSS_DEFAULT_RATES;
4058 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
d192d268
EP
4059 if (ret < 0)
4060 goto out;
4061 }
4062 }
4063
0603d891 4064 ret = wl1271_bss_erp_info_changed(wl, vif, bss_conf, changed);
e78a287a
AN
4065 if (ret < 0)
4066 goto out;
f5fc0f86 4067
8bf29b0e 4068 if (do_join) {
3230f35e 4069 ret = wlcore_join(wl, wlvif);
8bf29b0e
JO
4070 if (ret < 0) {
4071 wl1271_warning("cmd join failed %d", ret);
e78a287a 4072 goto out;
8bf29b0e 4073 }
3230f35e 4074 }
251c177f 4075
3230f35e
EP
4076 if (changed & BSS_CHANGED_ASSOC) {
4077 if (bss_conf->assoc) {
ec87011a
EP
4078 ret = wlcore_set_assoc(wl, wlvif, bss_conf,
4079 sta_rate_set);
251c177f
EP
4080 if (ret < 0)
4081 goto out;
4082
9fd6f21b
EP
4083 if (test_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags))
4084 wl12xx_set_authorized(wl, wlvif);
3230f35e
EP
4085 } else {
4086 wlcore_unset_assoc(wl, wlvif);
251c177f 4087 }
c1899554
JO
4088 }
4089
518b680a
EP
4090 if (changed & BSS_CHANGED_PS) {
4091 if ((bss_conf->ps) &&
4092 test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) &&
4093 !test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags)) {
4094 int ps_mode;
4095 char *ps_mode_str;
4096
4097 if (wl->conf.conn.forced_ps) {
4098 ps_mode = STATION_POWER_SAVE_MODE;
4099 ps_mode_str = "forced";
4100 } else {
4101 ps_mode = STATION_AUTO_PS_MODE;
4102 ps_mode_str = "auto";
4103 }
4104
4105 wl1271_debug(DEBUG_PSM, "%s ps enabled", ps_mode_str);
4106
4107 ret = wl1271_ps_set_mode(wl, wlvif, ps_mode);
251c177f 4108 if (ret < 0)
518b680a
EP
4109 wl1271_warning("enter %s ps failed %d",
4110 ps_mode_str, ret);
4111 } else if (!bss_conf->ps &&
4112 test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags)) {
4113 wl1271_debug(DEBUG_PSM, "auto ps disabled");
4114
4115 ret = wl1271_ps_set_mode(wl, wlvif,
4116 STATION_ACTIVE_MODE);
4117 if (ret < 0)
4118 wl1271_warning("exit auto ps failed %d", ret);
251c177f 4119 }
c1899554
JO
4120 }
4121
0b932ab9 4122 /* Handle new association with HT. Do this after join. */
58321b29
EP
4123 if (sta_exists &&
4124 (changed & BSS_CHANGED_HT)) {
4125 bool enabled =
aaabee8b 4126 bss_conf->chandef.width != NL80211_CHAN_WIDTH_20_NOHT;
58321b29
EP
4127
4128 ret = wl1271_acx_set_ht_capabilities(wl,
4129 &sta_ht_cap,
4130 enabled,
4131 wlvif->sta.hlid);
4132 if (ret < 0) {
4133 wl1271_warning("Set ht cap failed %d", ret);
4134 goto out;
4135
0f9c8250 4136 }
58321b29
EP
4137
4138 if (enabled) {
4139 ret = wl1271_acx_set_ht_information(wl, wlvif,
4140 bss_conf->ht_operation_mode);
0f9c8250 4141 if (ret < 0) {
58321b29 4142 wl1271_warning("Set ht information failed %d",
0f9c8250
AN
4143 ret);
4144 goto out;
4145 }
4146 }
4147 }
4148
76a74c8a
EP
4149 /* Handle arp filtering. Done after join. */
4150 if ((changed & BSS_CHANGED_ARP_FILTER) ||
4151 (!is_ibss && (changed & BSS_CHANGED_QOS))) {
4152 __be32 addr = bss_conf->arp_addr_list[0];
4153 wlvif->sta.qos = bss_conf->qos;
4154 WARN_ON(wlvif->bss_type != BSS_TYPE_STA_BSS);
4155
4156 if (bss_conf->arp_addr_cnt == 1 &&
4157 bss_conf->arp_filter_enabled) {
4158 wlvif->ip_addr = addr;
4159 /*
4160 * The template should have been configured only upon
4161 * association. however, it seems that the correct ip
4162 * isn't being set (when sending), so we have to
4163 * reconfigure the template upon every ip change.
4164 */
4165 ret = wl1271_cmd_build_arp_rsp(wl, wlvif);
4166 if (ret < 0) {
4167 wl1271_warning("build arp rsp failed: %d", ret);
4168 goto out;
4169 }
4170
4171 ret = wl1271_acx_arp_ip_filter(wl, wlvif,
4172 (ACX_ARP_FILTER_ARP_FILTERING |
4173 ACX_ARP_FILTER_AUTO_ARP),
4174 addr);
4175 } else {
4176 wlvif->ip_addr = 0;
4177 ret = wl1271_acx_arp_ip_filter(wl, wlvif, 0, addr);
4178 }
4179
4180 if (ret < 0)
4181 goto out;
4182 }
4183
e78a287a
AN
4184out:
4185 return;
4186}
4187
4188static void wl1271_op_bss_info_changed(struct ieee80211_hw *hw,
4189 struct ieee80211_vif *vif,
4190 struct ieee80211_bss_conf *bss_conf,
4191 u32 changed)
4192{
4193 struct wl1271 *wl = hw->priv;
536129c8
EP
4194 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4195 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
e78a287a
AN
4196 int ret;
4197
d3f5a1b5
EP
4198 wl1271_debug(DEBUG_MAC80211, "mac80211 bss info role %d changed 0x%x",
4199 wlvif->role_id, (int)changed);
e78a287a 4200
6b8bf5bc
AN
4201 /*
4202 * make sure to cancel pending disconnections if our association
4203 * state changed
4204 */
4205 if (!is_ap && (changed & BSS_CHANGED_ASSOC))
c50a2825 4206 cancel_delayed_work_sync(&wlvif->connection_loss_work);
6b8bf5bc 4207
b515d83a
EP
4208 if (is_ap && (changed & BSS_CHANGED_BEACON_ENABLED) &&
4209 !bss_conf->enable_beacon)
4210 wl1271_tx_flush(wl);
4211
e78a287a
AN
4212 mutex_lock(&wl->mutex);
4213
4cc53383 4214 if (unlikely(wl->state != WLCORE_STATE_ON))
e78a287a
AN
4215 goto out;
4216
10c8cd01
EP
4217 if (unlikely(!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)))
4218 goto out;
4219
a620865e 4220 ret = wl1271_ps_elp_wakeup(wl);
e78a287a
AN
4221 if (ret < 0)
4222 goto out;
4223
4224 if (is_ap)
4225 wl1271_bss_info_changed_ap(wl, vif, bss_conf, changed);
4226 else
4227 wl1271_bss_info_changed_sta(wl, vif, bss_conf, changed);
4228
f5fc0f86
LC
4229 wl1271_ps_elp_sleep(wl);
4230
4231out:
4232 mutex_unlock(&wl->mutex);
4233}
4234
b6970ee5
EP
4235static int wlcore_op_add_chanctx(struct ieee80211_hw *hw,
4236 struct ieee80211_chanctx_conf *ctx)
4237{
4238 wl1271_debug(DEBUG_MAC80211, "mac80211 add chanctx %d (type %d)",
aaabee8b
LC
4239 ieee80211_frequency_to_channel(ctx->def.chan->center_freq),
4240 cfg80211_get_chandef_type(&ctx->def));
b6970ee5
EP
4241 return 0;
4242}
4243
4244static void wlcore_op_remove_chanctx(struct ieee80211_hw *hw,
4245 struct ieee80211_chanctx_conf *ctx)
4246{
4247 wl1271_debug(DEBUG_MAC80211, "mac80211 remove chanctx %d (type %d)",
aaabee8b
LC
4248 ieee80211_frequency_to_channel(ctx->def.chan->center_freq),
4249 cfg80211_get_chandef_type(&ctx->def));
b6970ee5
EP
4250}
4251
4252static void wlcore_op_change_chanctx(struct ieee80211_hw *hw,
4253 struct ieee80211_chanctx_conf *ctx,
4254 u32 changed)
4255{
4256 wl1271_debug(DEBUG_MAC80211,
4257 "mac80211 change chanctx %d (type %d) changed 0x%x",
aaabee8b
LC
4258 ieee80211_frequency_to_channel(ctx->def.chan->center_freq),
4259 cfg80211_get_chandef_type(&ctx->def), changed);
b6970ee5
EP
4260}
4261
4262static int wlcore_op_assign_vif_chanctx(struct ieee80211_hw *hw,
4263 struct ieee80211_vif *vif,
4264 struct ieee80211_chanctx_conf *ctx)
4265{
4266 struct wl1271 *wl = hw->priv;
4267 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4268 int channel = ieee80211_frequency_to_channel(
aaabee8b 4269 ctx->def.chan->center_freq);
b6970ee5
EP
4270
4271 wl1271_debug(DEBUG_MAC80211,
4272 "mac80211 assign chanctx (role %d) %d (type %d)",
aaabee8b 4273 wlvif->role_id, channel, cfg80211_get_chandef_type(&ctx->def));
b6970ee5
EP
4274
4275 mutex_lock(&wl->mutex);
4276
aaabee8b 4277 wlvif->band = ctx->def.chan->band;
b6970ee5 4278 wlvif->channel = channel;
aaabee8b 4279 wlvif->channel_type = cfg80211_get_chandef_type(&ctx->def);
b6970ee5
EP
4280
4281 /* update default rates according to the band */
4282 wl1271_set_band_rate(wl, wlvif);
4283
4284 mutex_unlock(&wl->mutex);
4285
4286 return 0;
4287}
4288
4289static void wlcore_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
4290 struct ieee80211_vif *vif,
4291 struct ieee80211_chanctx_conf *ctx)
4292{
4293 struct wl1271 *wl = hw->priv;
4294 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4295
4296 wl1271_debug(DEBUG_MAC80211,
4297 "mac80211 unassign chanctx (role %d) %d (type %d)",
4298 wlvif->role_id,
aaabee8b
LC
4299 ieee80211_frequency_to_channel(ctx->def.chan->center_freq),
4300 cfg80211_get_chandef_type(&ctx->def));
b6970ee5
EP
4301
4302 wl1271_tx_flush(wl);
4303}
4304
8a3a3c85
EP
4305static int wl1271_op_conf_tx(struct ieee80211_hw *hw,
4306 struct ieee80211_vif *vif, u16 queue,
c6999d83
KV
4307 const struct ieee80211_tx_queue_params *params)
4308{
4309 struct wl1271 *wl = hw->priv;
0603d891 4310 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4695dc91 4311 u8 ps_scheme;
488fc540 4312 int ret = 0;
c6999d83
KV
4313
4314 mutex_lock(&wl->mutex);
4315
4316 wl1271_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
4317
4695dc91
KV
4318 if (params->uapsd)
4319 ps_scheme = CONF_PS_SCHEME_UPSD_TRIGGER;
4320 else
4321 ps_scheme = CONF_PS_SCHEME_LEGACY;
4322
5b37ddfe 4323 if (!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
c1b193eb 4324 goto out;
488fc540 4325
c1b193eb
EP
4326 ret = wl1271_ps_elp_wakeup(wl);
4327 if (ret < 0)
4328 goto out;
488fc540 4329
c1b193eb
EP
4330 /*
4331 * the txop is confed in units of 32us by the mac80211,
4332 * we need us
4333 */
0603d891 4334 ret = wl1271_acx_ac_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
c1b193eb
EP
4335 params->cw_min, params->cw_max,
4336 params->aifs, params->txop << 5);
4337 if (ret < 0)
4338 goto out_sleep;
4339
0603d891 4340 ret = wl1271_acx_tid_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
c1b193eb
EP
4341 CONF_CHANNEL_TYPE_EDCF,
4342 wl1271_tx_get_queue(queue),
4343 ps_scheme, CONF_ACK_POLICY_LEGACY,
4344 0, 0);
c82c1dde
KV
4345
4346out_sleep:
c1b193eb 4347 wl1271_ps_elp_sleep(wl);
c6999d83
KV
4348
4349out:
4350 mutex_unlock(&wl->mutex);
4351
4352 return ret;
4353}
4354
37a41b4a
EP
4355static u64 wl1271_op_get_tsf(struct ieee80211_hw *hw,
4356 struct ieee80211_vif *vif)
bbbb538e
JO
4357{
4358
4359 struct wl1271 *wl = hw->priv;
9c531149 4360 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
bbbb538e
JO
4361 u64 mactime = ULLONG_MAX;
4362 int ret;
4363
4364 wl1271_debug(DEBUG_MAC80211, "mac80211 get tsf");
4365
4366 mutex_lock(&wl->mutex);
4367
4cc53383 4368 if (unlikely(wl->state != WLCORE_STATE_ON))
f8d9802f
JO
4369 goto out;
4370
a620865e 4371 ret = wl1271_ps_elp_wakeup(wl);
bbbb538e
JO
4372 if (ret < 0)
4373 goto out;
4374
9c531149 4375 ret = wl12xx_acx_tsf_info(wl, wlvif, &mactime);
bbbb538e
JO
4376 if (ret < 0)
4377 goto out_sleep;
4378
4379out_sleep:
4380 wl1271_ps_elp_sleep(wl);
4381
4382out:
4383 mutex_unlock(&wl->mutex);
4384 return mactime;
4385}
f5fc0f86 4386
ece550d0
JL
4387static int wl1271_op_get_survey(struct ieee80211_hw *hw, int idx,
4388 struct survey_info *survey)
4389{
ece550d0 4390 struct ieee80211_conf *conf = &hw->conf;
b739a42c 4391
ece550d0
JL
4392 if (idx != 0)
4393 return -ENOENT;
b739a42c 4394
ece550d0 4395 survey->channel = conf->channel;
add779a0 4396 survey->filled = 0;
ece550d0
JL
4397 return 0;
4398}
4399
409622ec 4400static int wl1271_allocate_sta(struct wl1271 *wl,
c7ffb902
EP
4401 struct wl12xx_vif *wlvif,
4402 struct ieee80211_sta *sta)
f84f7d78
AN
4403{
4404 struct wl1271_station *wl_sta;
c7ffb902 4405 int ret;
f84f7d78 4406
c7ffb902
EP
4407
4408 if (wl->active_sta_count >= AP_MAX_STATIONS) {
f84f7d78
AN
4409 wl1271_warning("could not allocate HLID - too much stations");
4410 return -EBUSY;
4411 }
4412
4413 wl_sta = (struct wl1271_station *)sta->drv_priv;
c7ffb902
EP
4414 ret = wl12xx_allocate_link(wl, wlvif, &wl_sta->hlid);
4415 if (ret < 0) {
4416 wl1271_warning("could not allocate HLID - too many links");
4417 return -EBUSY;
4418 }
4419
4420 set_bit(wl_sta->hlid, wlvif->ap.sta_hlid_map);
b622d992 4421 memcpy(wl->links[wl_sta->hlid].addr, sta->addr, ETH_ALEN);
da03209e 4422 wl->active_sta_count++;
f84f7d78
AN
4423 return 0;
4424}
4425
c7ffb902 4426void wl1271_free_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 hlid)
f84f7d78 4427{
c7ffb902 4428 if (!test_bit(hlid, wlvif->ap.sta_hlid_map))
f1acea9a
AN
4429 return;
4430
c7ffb902 4431 clear_bit(hlid, wlvif->ap.sta_hlid_map);
b622d992
AN
4432 __clear_bit(hlid, &wl->ap_ps_map);
4433 __clear_bit(hlid, (unsigned long *)&wl->ap_fw_ps_map);
c7ffb902 4434 wl12xx_free_link(wl, wlvif, &hlid);
da03209e 4435 wl->active_sta_count--;
55df5afb
AN
4436
4437 /*
4438 * rearm the tx watchdog when the last STA is freed - give the FW a
4439 * chance to return STA-buffered packets before complaining.
4440 */
4441 if (wl->active_sta_count == 0)
4442 wl12xx_rearm_tx_watchdog_locked(wl);
f84f7d78
AN
4443}
4444
2d6cf2b5
EP
4445static int wl12xx_sta_add(struct wl1271 *wl,
4446 struct wl12xx_vif *wlvif,
4447 struct ieee80211_sta *sta)
f84f7d78 4448{
c7ffb902 4449 struct wl1271_station *wl_sta;
f84f7d78
AN
4450 int ret = 0;
4451 u8 hlid;
4452
f84f7d78
AN
4453 wl1271_debug(DEBUG_MAC80211, "mac80211 add sta %d", (int)sta->aid);
4454
c7ffb902 4455 ret = wl1271_allocate_sta(wl, wlvif, sta);
f84f7d78 4456 if (ret < 0)
2d6cf2b5 4457 return ret;
f84f7d78 4458
c7ffb902
EP
4459 wl_sta = (struct wl1271_station *)sta->drv_priv;
4460 hlid = wl_sta->hlid;
4461
1b92f15e 4462 ret = wl12xx_cmd_add_peer(wl, wlvif, sta, hlid);
f84f7d78 4463 if (ret < 0)
2d6cf2b5 4464 wl1271_free_sta(wl, wlvif, hlid);
f84f7d78 4465
2d6cf2b5
EP
4466 return ret;
4467}
b67476ef 4468
2d6cf2b5
EP
4469static int wl12xx_sta_remove(struct wl1271 *wl,
4470 struct wl12xx_vif *wlvif,
4471 struct ieee80211_sta *sta)
4472{
4473 struct wl1271_station *wl_sta;
4474 int ret = 0, id;
0b932ab9 4475
2d6cf2b5
EP
4476 wl1271_debug(DEBUG_MAC80211, "mac80211 remove sta %d", (int)sta->aid);
4477
4478 wl_sta = (struct wl1271_station *)sta->drv_priv;
4479 id = wl_sta->hlid;
4480 if (WARN_ON(!test_bit(id, wlvif->ap.sta_hlid_map)))
4481 return -EINVAL;
f84f7d78 4482
2d6cf2b5 4483 ret = wl12xx_cmd_remove_peer(wl, wl_sta->hlid);
409622ec 4484 if (ret < 0)
2d6cf2b5 4485 return ret;
409622ec 4486
2d6cf2b5 4487 wl1271_free_sta(wl, wlvif, wl_sta->hlid);
f84f7d78
AN
4488 return ret;
4489}
4490
426001a6
EP
4491static void wlcore_roc_if_possible(struct wl1271 *wl,
4492 struct wl12xx_vif *wlvif)
4493{
4494 if (find_first_bit(wl->roc_map,
4495 WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES)
4496 return;
4497
4498 if (WARN_ON(wlvif->role_id == WL12XX_INVALID_ROLE_ID))
4499 return;
4500
4501 wl12xx_roc(wl, wlvif, wlvif->role_id, wlvif->band, wlvif->channel);
4502}
4503
4504static void wlcore_update_inconn_sta(struct wl1271 *wl,
4505 struct wl12xx_vif *wlvif,
4506 struct wl1271_station *wl_sta,
4507 bool in_connection)
4508{
4509 if (in_connection) {
4510 if (WARN_ON(wl_sta->in_connection))
4511 return;
4512 wl_sta->in_connection = true;
4513 if (!wlvif->inconn_count++)
4514 wlcore_roc_if_possible(wl, wlvif);
4515 } else {
4516 if (!wl_sta->in_connection)
4517 return;
4518
4519 wl_sta->in_connection = false;
4520 wlvif->inconn_count--;
4521 if (WARN_ON(wlvif->inconn_count < 0))
4522 return;
4523
4524 if (!wlvif->inconn_count)
4525 if (test_bit(wlvif->role_id, wl->roc_map))
4526 wl12xx_croc(wl, wlvif->role_id);
4527 }
4528}
4529
2d6cf2b5
EP
4530static int wl12xx_update_sta_state(struct wl1271 *wl,
4531 struct wl12xx_vif *wlvif,
4532 struct ieee80211_sta *sta,
4533 enum ieee80211_sta_state old_state,
4534 enum ieee80211_sta_state new_state)
f84f7d78 4535{
f84f7d78 4536 struct wl1271_station *wl_sta;
2d6cf2b5
EP
4537 u8 hlid;
4538 bool is_ap = wlvif->bss_type == BSS_TYPE_AP_BSS;
4539 bool is_sta = wlvif->bss_type == BSS_TYPE_STA_BSS;
4540 int ret;
f84f7d78 4541
2d6cf2b5
EP
4542 wl_sta = (struct wl1271_station *)sta->drv_priv;
4543 hlid = wl_sta->hlid;
f84f7d78 4544
2d6cf2b5
EP
4545 /* Add station (AP mode) */
4546 if (is_ap &&
4547 old_state == IEEE80211_STA_NOTEXIST &&
29936266
EP
4548 new_state == IEEE80211_STA_NONE) {
4549 ret = wl12xx_sta_add(wl, wlvif, sta);
4550 if (ret)
4551 return ret;
426001a6
EP
4552
4553 wlcore_update_inconn_sta(wl, wlvif, wl_sta, true);
29936266 4554 }
2d6cf2b5
EP
4555
4556 /* Remove station (AP mode) */
4557 if (is_ap &&
4558 old_state == IEEE80211_STA_NONE &&
4559 new_state == IEEE80211_STA_NOTEXIST) {
4560 /* must not fail */
4561 wl12xx_sta_remove(wl, wlvif, sta);
426001a6
EP
4562
4563 wlcore_update_inconn_sta(wl, wlvif, wl_sta, false);
2d6cf2b5 4564 }
f84f7d78 4565
2d6cf2b5
EP
4566 /* Authorize station (AP mode) */
4567 if (is_ap &&
4568 new_state == IEEE80211_STA_AUTHORIZED) {
d50529c0 4569 ret = wl12xx_cmd_set_peer_state(wl, wlvif, hlid);
2d6cf2b5
EP
4570 if (ret < 0)
4571 return ret;
f84f7d78 4572
2d6cf2b5
EP
4573 ret = wl1271_acx_set_ht_capabilities(wl, &sta->ht_cap, true,
4574 hlid);
29936266
EP
4575 if (ret)
4576 return ret;
426001a6
EP
4577
4578 wlcore_update_inconn_sta(wl, wlvif, wl_sta, false);
2d6cf2b5 4579 }
f84f7d78 4580
9fd6f21b
EP
4581 /* Authorize station */
4582 if (is_sta &&
4583 new_state == IEEE80211_STA_AUTHORIZED) {
4584 set_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags);
29936266
EP
4585 ret = wl12xx_set_authorized(wl, wlvif);
4586 if (ret)
4587 return ret;
9fd6f21b
EP
4588 }
4589
4590 if (is_sta &&
4591 old_state == IEEE80211_STA_AUTHORIZED &&
4592 new_state == IEEE80211_STA_ASSOC) {
4593 clear_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags);
3230f35e 4594 clear_bit(WLVIF_FLAG_STA_STATE_SENT, &wlvif->flags);
9fd6f21b
EP
4595 }
4596
29936266
EP
4597 /* clear ROCs on failure or authorization */
4598 if (is_sta &&
4599 (new_state == IEEE80211_STA_AUTHORIZED ||
4600 new_state == IEEE80211_STA_NOTEXIST)) {
4601 if (test_bit(wlvif->role_id, wl->roc_map))
4602 wl12xx_croc(wl, wlvif->role_id);
9fd6f21b
EP
4603 }
4604
29936266
EP
4605 if (is_sta &&
4606 old_state == IEEE80211_STA_NOTEXIST &&
4607 new_state == IEEE80211_STA_NONE) {
4608 if (find_first_bit(wl->roc_map,
4609 WL12XX_MAX_ROLES) >= WL12XX_MAX_ROLES) {
4610 WARN_ON(wlvif->role_id == WL12XX_INVALID_ROLE_ID);
4611 wl12xx_roc(wl, wlvif, wlvif->role_id,
4612 wlvif->band, wlvif->channel);
4613 }
4614 }
2d6cf2b5
EP
4615 return 0;
4616}
4617
4618static int wl12xx_op_sta_state(struct ieee80211_hw *hw,
4619 struct ieee80211_vif *vif,
4620 struct ieee80211_sta *sta,
4621 enum ieee80211_sta_state old_state,
4622 enum ieee80211_sta_state new_state)
4623{
4624 struct wl1271 *wl = hw->priv;
4625 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4626 int ret;
4627
4628 wl1271_debug(DEBUG_MAC80211, "mac80211 sta %d state=%d->%d",
4629 sta->aid, old_state, new_state);
4630
4631 mutex_lock(&wl->mutex);
4632
4cc53383 4633 if (unlikely(wl->state != WLCORE_STATE_ON)) {
2d6cf2b5 4634 ret = -EBUSY;
f84f7d78 4635 goto out;
2d6cf2b5 4636 }
f84f7d78 4637
a620865e 4638 ret = wl1271_ps_elp_wakeup(wl);
f84f7d78
AN
4639 if (ret < 0)
4640 goto out;
4641
2d6cf2b5 4642 ret = wl12xx_update_sta_state(wl, wlvif, sta, old_state, new_state);
f84f7d78 4643
f84f7d78 4644 wl1271_ps_elp_sleep(wl);
f84f7d78
AN
4645out:
4646 mutex_unlock(&wl->mutex);
2d6cf2b5
EP
4647 if (new_state < old_state)
4648 return 0;
f84f7d78
AN
4649 return ret;
4650}
4651
4623ec7d
LC
4652static int wl1271_op_ampdu_action(struct ieee80211_hw *hw,
4653 struct ieee80211_vif *vif,
4654 enum ieee80211_ampdu_mlme_action action,
4655 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
4656 u8 buf_size)
bbba3e68
LS
4657{
4658 struct wl1271 *wl = hw->priv;
536129c8 4659 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
bbba3e68 4660 int ret;
0f9c8250
AN
4661 u8 hlid, *ba_bitmap;
4662
4663 wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu action %d tid %d", action,
4664 tid);
4665
4666 /* sanity check - the fields in FW are only 8bits wide */
4667 if (WARN_ON(tid > 0xFF))
4668 return -ENOTSUPP;
bbba3e68
LS
4669
4670 mutex_lock(&wl->mutex);
4671
4cc53383 4672 if (unlikely(wl->state != WLCORE_STATE_ON)) {
bbba3e68
LS
4673 ret = -EAGAIN;
4674 goto out;
4675 }
4676
536129c8 4677 if (wlvif->bss_type == BSS_TYPE_STA_BSS) {
154da67c 4678 hlid = wlvif->sta.hlid;
d0802abd 4679 ba_bitmap = &wlvif->sta.ba_rx_bitmap;
536129c8 4680 } else if (wlvif->bss_type == BSS_TYPE_AP_BSS) {
0f9c8250
AN
4681 struct wl1271_station *wl_sta;
4682
4683 wl_sta = (struct wl1271_station *)sta->drv_priv;
4684 hlid = wl_sta->hlid;
4685 ba_bitmap = &wl->links[hlid].ba_bitmap;
4686 } else {
4687 ret = -EINVAL;
4688 goto out;
4689 }
4690
a620865e 4691 ret = wl1271_ps_elp_wakeup(wl);
bbba3e68
LS
4692 if (ret < 0)
4693 goto out;
4694
70559a06
SL
4695 wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu: Rx tid %d action %d",
4696 tid, action);
4697
bbba3e68
LS
4698 switch (action) {
4699 case IEEE80211_AMPDU_RX_START:
d0802abd 4700 if (!wlvif->ba_support || !wlvif->ba_allowed) {
bbba3e68 4701 ret = -ENOTSUPP;
0f9c8250
AN
4702 break;
4703 }
4704
4705 if (wl->ba_rx_session_count >= RX_BA_MAX_SESSIONS) {
4706 ret = -EBUSY;
4707 wl1271_error("exceeded max RX BA sessions");
4708 break;
4709 }
4710
4711 if (*ba_bitmap & BIT(tid)) {
4712 ret = -EINVAL;
4713 wl1271_error("cannot enable RX BA session on active "
4714 "tid: %d", tid);
4715 break;
4716 }
4717
4718 ret = wl12xx_acx_set_ba_receiver_session(wl, tid, *ssn, true,
4719 hlid);
4720 if (!ret) {
4721 *ba_bitmap |= BIT(tid);
4722 wl->ba_rx_session_count++;
bbba3e68
LS
4723 }
4724 break;
4725
4726 case IEEE80211_AMPDU_RX_STOP:
0f9c8250 4727 if (!(*ba_bitmap & BIT(tid))) {
c954910b
AN
4728 /*
4729 * this happens on reconfig - so only output a debug
4730 * message for now, and don't fail the function.
4731 */
4732 wl1271_debug(DEBUG_MAC80211,
4733 "no active RX BA session on tid: %d",
0f9c8250 4734 tid);
c954910b 4735 ret = 0;
0f9c8250
AN
4736 break;
4737 }
4738
4739 ret = wl12xx_acx_set_ba_receiver_session(wl, tid, 0, false,
4740 hlid);
4741 if (!ret) {
4742 *ba_bitmap &= ~BIT(tid);
4743 wl->ba_rx_session_count--;
4744 }
bbba3e68
LS
4745 break;
4746
4747 /*
4748 * The BA initiator session management in FW independently.
4749 * Falling break here on purpose for all TX APDU commands.
4750 */
4751 case IEEE80211_AMPDU_TX_START:
4752 case IEEE80211_AMPDU_TX_STOP:
4753 case IEEE80211_AMPDU_TX_OPERATIONAL:
4754 ret = -EINVAL;
4755 break;
4756
4757 default:
4758 wl1271_error("Incorrect ampdu action id=%x\n", action);
4759 ret = -EINVAL;
4760 }
4761
4762 wl1271_ps_elp_sleep(wl);
4763
4764out:
4765 mutex_unlock(&wl->mutex);
4766
4767 return ret;
4768}
4769
af7fbb28
EP
4770static int wl12xx_set_bitrate_mask(struct ieee80211_hw *hw,
4771 struct ieee80211_vif *vif,
4772 const struct cfg80211_bitrate_mask *mask)
4773{
83587505 4774 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
af7fbb28 4775 struct wl1271 *wl = hw->priv;
d6fa37c9 4776 int i, ret = 0;
af7fbb28
EP
4777
4778 wl1271_debug(DEBUG_MAC80211, "mac80211 set_bitrate_mask 0x%x 0x%x",
4779 mask->control[NL80211_BAND_2GHZ].legacy,
4780 mask->control[NL80211_BAND_5GHZ].legacy);
4781
4782 mutex_lock(&wl->mutex);
4783
091185d6 4784 for (i = 0; i < WLCORE_NUM_BANDS; i++)
83587505 4785 wlvif->bitrate_masks[i] =
af7fbb28
EP
4786 wl1271_tx_enabled_rates_get(wl,
4787 mask->control[i].legacy,
4788 i);
d6fa37c9 4789
4cc53383 4790 if (unlikely(wl->state != WLCORE_STATE_ON))
d6fa37c9
EP
4791 goto out;
4792
4793 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
4794 !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
4795
4796 ret = wl1271_ps_elp_wakeup(wl);
4797 if (ret < 0)
4798 goto out;
4799
4800 wl1271_set_band_rate(wl, wlvif);
4801 wlvif->basic_rate =
4802 wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
4803 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
4804
4805 wl1271_ps_elp_sleep(wl);
4806 }
4807out:
af7fbb28
EP
4808 mutex_unlock(&wl->mutex);
4809
d6fa37c9 4810 return ret;
af7fbb28
EP
4811}
4812
6d158ff3
SL
4813static void wl12xx_op_channel_switch(struct ieee80211_hw *hw,
4814 struct ieee80211_channel_switch *ch_switch)
4815{
4816 struct wl1271 *wl = hw->priv;
52630c5d 4817 struct wl12xx_vif *wlvif;
6d158ff3
SL
4818 int ret;
4819
4820 wl1271_debug(DEBUG_MAC80211, "mac80211 channel switch");
4821
b9239b66
AN
4822 wl1271_tx_flush(wl);
4823
6d158ff3
SL
4824 mutex_lock(&wl->mutex);
4825
4cc53383 4826 if (unlikely(wl->state == WLCORE_STATE_OFF)) {
6e8cd331
EP
4827 wl12xx_for_each_wlvif_sta(wl, wlvif) {
4828 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
4829 ieee80211_chswitch_done(vif, false);
4830 }
4831 goto out;
4cc53383
IY
4832 } else if (unlikely(wl->state != WLCORE_STATE_ON)) {
4833 goto out;
6d158ff3
SL
4834 }
4835
4836 ret = wl1271_ps_elp_wakeup(wl);
4837 if (ret < 0)
4838 goto out;
4839
52630c5d
EP
4840 /* TODO: change mac80211 to pass vif as param */
4841 wl12xx_for_each_wlvif_sta(wl, wlvif) {
c50a2825
EP
4842 unsigned long delay_usec;
4843
fcab1890 4844 ret = wl->ops->channel_switch(wl, wlvif, ch_switch);
c50a2825
EP
4845 if (ret)
4846 goto out_sleep;
6d158ff3 4847
c50a2825
EP
4848 set_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags);
4849
4850 /* indicate failure 5 seconds after channel switch time */
4851 delay_usec = ieee80211_tu_to_usec(wlvif->beacon_int) *
4852 ch_switch->count;
4853 ieee80211_queue_delayed_work(hw, &wlvif->channel_switch_work,
4854 usecs_to_jiffies(delay_usec) +
4855 msecs_to_jiffies(5000));
52630c5d 4856 }
6d158ff3 4857
c50a2825 4858out_sleep:
6d158ff3
SL
4859 wl1271_ps_elp_sleep(wl);
4860
4861out:
4862 mutex_unlock(&wl->mutex);
4863}
4864
d8ae5a25
EP
4865static void wlcore_op_flush(struct ieee80211_hw *hw, bool drop)
4866{
4867 struct wl1271 *wl = hw->priv;
4868
4869 wl1271_tx_flush(wl);
4870}
4871
dabf37db
EP
4872static int wlcore_op_remain_on_channel(struct ieee80211_hw *hw,
4873 struct ieee80211_vif *vif,
4874 struct ieee80211_channel *chan,
dabf37db
EP
4875 int duration)
4876{
4877 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4878 struct wl1271 *wl = hw->priv;
4879 int channel, ret = 0;
4880
4881 channel = ieee80211_frequency_to_channel(chan->center_freq);
4882
4883 wl1271_debug(DEBUG_MAC80211, "mac80211 roc %d (%d)",
4884 channel, wlvif->role_id);
4885
4886 mutex_lock(&wl->mutex);
4887
4888 if (unlikely(wl->state != WLCORE_STATE_ON))
4889 goto out;
4890
4891 /* return EBUSY if we can't ROC right now */
4892 if (WARN_ON(wl->roc_vif ||
4893 find_first_bit(wl->roc_map,
4894 WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES)) {
4895 ret = -EBUSY;
4896 goto out;
4897 }
4898
4899 ret = wl1271_ps_elp_wakeup(wl);
4900 if (ret < 0)
4901 goto out;
4902
4903 ret = wl12xx_start_dev(wl, wlvif, chan->band, channel);
4904 if (ret < 0)
4905 goto out_sleep;
4906
4907 wl->roc_vif = vif;
4908 ieee80211_queue_delayed_work(hw, &wl->roc_complete_work,
4909 msecs_to_jiffies(duration));
4910out_sleep:
4911 wl1271_ps_elp_sleep(wl);
4912out:
4913 mutex_unlock(&wl->mutex);
4914 return ret;
4915}
4916
4917static int __wlcore_roc_completed(struct wl1271 *wl)
4918{
4919 struct wl12xx_vif *wlvif;
4920 int ret;
4921
4922 /* already completed */
4923 if (unlikely(!wl->roc_vif))
4924 return 0;
4925
4926 wlvif = wl12xx_vif_to_data(wl->roc_vif);
4927
4928 if (!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
4929 return -EBUSY;
4930
4931 ret = wl12xx_stop_dev(wl, wlvif);
4932 if (ret < 0)
4933 return ret;
4934
4935 wl->roc_vif = NULL;
4936
4937 return 0;
4938}
4939
4940static int wlcore_roc_completed(struct wl1271 *wl)
4941{
4942 int ret;
4943
4944 wl1271_debug(DEBUG_MAC80211, "roc complete");
4945
4946 mutex_lock(&wl->mutex);
4947
4948 if (unlikely(wl->state != WLCORE_STATE_ON)) {
4949 ret = -EBUSY;
4950 goto out;
4951 }
4952
4953 ret = wl1271_ps_elp_wakeup(wl);
4954 if (ret < 0)
4955 goto out;
4956
4957 ret = __wlcore_roc_completed(wl);
4958
4959 wl1271_ps_elp_sleep(wl);
4960out:
4961 mutex_unlock(&wl->mutex);
4962
4963 return ret;
4964}
4965
4966static void wlcore_roc_complete_work(struct work_struct *work)
4967{
4968 struct delayed_work *dwork;
4969 struct wl1271 *wl;
4970 int ret;
4971
4972 dwork = container_of(work, struct delayed_work, work);
4973 wl = container_of(dwork, struct wl1271, roc_complete_work);
4974
4975 ret = wlcore_roc_completed(wl);
4976 if (!ret)
4977 ieee80211_remain_on_channel_expired(wl->hw);
4978}
4979
4980static int wlcore_op_cancel_remain_on_channel(struct ieee80211_hw *hw)
4981{
4982 struct wl1271 *wl = hw->priv;
4983
4984 wl1271_debug(DEBUG_MAC80211, "mac80211 croc");
4985
4986 /* TODO: per-vif */
4987 wl1271_tx_flush(wl);
4988
4989 /*
4990 * we can't just flush_work here, because it might deadlock
4991 * (as we might get called from the same workqueue)
4992 */
4993 cancel_delayed_work_sync(&wl->roc_complete_work);
4994 wlcore_roc_completed(wl);
4995
4996 return 0;
4997}
4998
5f9b6777
AN
4999static void wlcore_op_sta_rc_update(struct ieee80211_hw *hw,
5000 struct ieee80211_vif *vif,
5001 struct ieee80211_sta *sta,
5002 u32 changed)
5003{
5004 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5005 struct wl1271 *wl = hw->priv;
5006
5007 wlcore_hw_sta_rc_update(wl, wlvif, sta, changed);
5008}
5009
33437893
AN
5010static bool wl1271_tx_frames_pending(struct ieee80211_hw *hw)
5011{
5012 struct wl1271 *wl = hw->priv;
5013 bool ret = false;
5014
5015 mutex_lock(&wl->mutex);
5016
4cc53383 5017 if (unlikely(wl->state != WLCORE_STATE_ON))
33437893
AN
5018 goto out;
5019
5020 /* packets are considered pending if in the TX queue or the FW */
f1a46384 5021 ret = (wl1271_tx_total_queue_count(wl) > 0) || (wl->tx_frames_cnt > 0);
33437893
AN
5022out:
5023 mutex_unlock(&wl->mutex);
5024
5025 return ret;
5026}
5027
f5fc0f86
LC
5028/* can't be const, mac80211 writes to this */
5029static struct ieee80211_rate wl1271_rates[] = {
5030 { .bitrate = 10,
2b60100b
JO
5031 .hw_value = CONF_HW_BIT_RATE_1MBPS,
5032 .hw_value_short = CONF_HW_BIT_RATE_1MBPS, },
f5fc0f86 5033 { .bitrate = 20,
2b60100b
JO
5034 .hw_value = CONF_HW_BIT_RATE_2MBPS,
5035 .hw_value_short = CONF_HW_BIT_RATE_2MBPS,
f5fc0f86
LC
5036 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
5037 { .bitrate = 55,
2b60100b
JO
5038 .hw_value = CONF_HW_BIT_RATE_5_5MBPS,
5039 .hw_value_short = CONF_HW_BIT_RATE_5_5MBPS,
f5fc0f86
LC
5040 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
5041 { .bitrate = 110,
2b60100b
JO
5042 .hw_value = CONF_HW_BIT_RATE_11MBPS,
5043 .hw_value_short = CONF_HW_BIT_RATE_11MBPS,
f5fc0f86
LC
5044 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
5045 { .bitrate = 60,
2b60100b
JO
5046 .hw_value = CONF_HW_BIT_RATE_6MBPS,
5047 .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
f5fc0f86 5048 { .bitrate = 90,
2b60100b
JO
5049 .hw_value = CONF_HW_BIT_RATE_9MBPS,
5050 .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
f5fc0f86 5051 { .bitrate = 120,
2b60100b
JO
5052 .hw_value = CONF_HW_BIT_RATE_12MBPS,
5053 .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
f5fc0f86 5054 { .bitrate = 180,
2b60100b
JO
5055 .hw_value = CONF_HW_BIT_RATE_18MBPS,
5056 .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
f5fc0f86 5057 { .bitrate = 240,
2b60100b
JO
5058 .hw_value = CONF_HW_BIT_RATE_24MBPS,
5059 .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
f5fc0f86 5060 { .bitrate = 360,
2b60100b
JO
5061 .hw_value = CONF_HW_BIT_RATE_36MBPS,
5062 .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
f5fc0f86 5063 { .bitrate = 480,
2b60100b
JO
5064 .hw_value = CONF_HW_BIT_RATE_48MBPS,
5065 .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
f5fc0f86 5066 { .bitrate = 540,
2b60100b
JO
5067 .hw_value = CONF_HW_BIT_RATE_54MBPS,
5068 .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
f5fc0f86
LC
5069};
5070
fa97f46b 5071/* can't be const, mac80211 writes to this */
f5fc0f86 5072static struct ieee80211_channel wl1271_channels[] = {
a2d0e3f1 5073 { .hw_value = 1, .center_freq = 2412, .max_power = 25 },
fa21c7a9 5074 { .hw_value = 2, .center_freq = 2417, .max_power = 25 },
fa97f46b
JO
5075 { .hw_value = 3, .center_freq = 2422, .max_power = 25 },
5076 { .hw_value = 4, .center_freq = 2427, .max_power = 25 },
5077 { .hw_value = 5, .center_freq = 2432, .max_power = 25 },
fa21c7a9 5078 { .hw_value = 6, .center_freq = 2437, .max_power = 25 },
fa97f46b
JO
5079 { .hw_value = 7, .center_freq = 2442, .max_power = 25 },
5080 { .hw_value = 8, .center_freq = 2447, .max_power = 25 },
5081 { .hw_value = 9, .center_freq = 2452, .max_power = 25 },
fa21c7a9 5082 { .hw_value = 10, .center_freq = 2457, .max_power = 25 },
fa97f46b
JO
5083 { .hw_value = 11, .center_freq = 2462, .max_power = 25 },
5084 { .hw_value = 12, .center_freq = 2467, .max_power = 25 },
5085 { .hw_value = 13, .center_freq = 2472, .max_power = 25 },
6c89b7b2 5086 { .hw_value = 14, .center_freq = 2484, .max_power = 25 },
f5fc0f86
LC
5087};
5088
5089/* can't be const, mac80211 writes to this */
5090static struct ieee80211_supported_band wl1271_band_2ghz = {
5091 .channels = wl1271_channels,
5092 .n_channels = ARRAY_SIZE(wl1271_channels),
5093 .bitrates = wl1271_rates,
5094 .n_bitrates = ARRAY_SIZE(wl1271_rates),
5095};
5096
1ebec3d7
TP
5097/* 5 GHz data rates for WL1273 */
5098static struct ieee80211_rate wl1271_rates_5ghz[] = {
5099 { .bitrate = 60,
5100 .hw_value = CONF_HW_BIT_RATE_6MBPS,
5101 .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
5102 { .bitrate = 90,
5103 .hw_value = CONF_HW_BIT_RATE_9MBPS,
5104 .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
5105 { .bitrate = 120,
5106 .hw_value = CONF_HW_BIT_RATE_12MBPS,
5107 .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
5108 { .bitrate = 180,
5109 .hw_value = CONF_HW_BIT_RATE_18MBPS,
5110 .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
5111 { .bitrate = 240,
5112 .hw_value = CONF_HW_BIT_RATE_24MBPS,
5113 .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
5114 { .bitrate = 360,
5115 .hw_value = CONF_HW_BIT_RATE_36MBPS,
5116 .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
5117 { .bitrate = 480,
5118 .hw_value = CONF_HW_BIT_RATE_48MBPS,
5119 .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
5120 { .bitrate = 540,
5121 .hw_value = CONF_HW_BIT_RATE_54MBPS,
5122 .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
5123};
5124
fa97f46b 5125/* 5 GHz band channels for WL1273 */
1ebec3d7 5126static struct ieee80211_channel wl1271_channels_5ghz[] = {
6cfa5cff
AN
5127 { .hw_value = 7, .center_freq = 5035, .max_power = 25 },
5128 { .hw_value = 8, .center_freq = 5040, .max_power = 25 },
5129 { .hw_value = 9, .center_freq = 5045, .max_power = 25 },
5130 { .hw_value = 11, .center_freq = 5055, .max_power = 25 },
5131 { .hw_value = 12, .center_freq = 5060, .max_power = 25 },
5132 { .hw_value = 16, .center_freq = 5080, .max_power = 25 },
5133 { .hw_value = 34, .center_freq = 5170, .max_power = 25 },
5134 { .hw_value = 36, .center_freq = 5180, .max_power = 25 },
5135 { .hw_value = 38, .center_freq = 5190, .max_power = 25 },
5136 { .hw_value = 40, .center_freq = 5200, .max_power = 25 },
5137 { .hw_value = 42, .center_freq = 5210, .max_power = 25 },
5138 { .hw_value = 44, .center_freq = 5220, .max_power = 25 },
5139 { .hw_value = 46, .center_freq = 5230, .max_power = 25 },
5140 { .hw_value = 48, .center_freq = 5240, .max_power = 25 },
5141 { .hw_value = 52, .center_freq = 5260, .max_power = 25 },
5142 { .hw_value = 56, .center_freq = 5280, .max_power = 25 },
5143 { .hw_value = 60, .center_freq = 5300, .max_power = 25 },
5144 { .hw_value = 64, .center_freq = 5320, .max_power = 25 },
5145 { .hw_value = 100, .center_freq = 5500, .max_power = 25 },
5146 { .hw_value = 104, .center_freq = 5520, .max_power = 25 },
5147 { .hw_value = 108, .center_freq = 5540, .max_power = 25 },
5148 { .hw_value = 112, .center_freq = 5560, .max_power = 25 },
5149 { .hw_value = 116, .center_freq = 5580, .max_power = 25 },
5150 { .hw_value = 120, .center_freq = 5600, .max_power = 25 },
5151 { .hw_value = 124, .center_freq = 5620, .max_power = 25 },
5152 { .hw_value = 128, .center_freq = 5640, .max_power = 25 },
5153 { .hw_value = 132, .center_freq = 5660, .max_power = 25 },
5154 { .hw_value = 136, .center_freq = 5680, .max_power = 25 },
5155 { .hw_value = 140, .center_freq = 5700, .max_power = 25 },
5156 { .hw_value = 149, .center_freq = 5745, .max_power = 25 },
5157 { .hw_value = 153, .center_freq = 5765, .max_power = 25 },
5158 { .hw_value = 157, .center_freq = 5785, .max_power = 25 },
5159 { .hw_value = 161, .center_freq = 5805, .max_power = 25 },
5160 { .hw_value = 165, .center_freq = 5825, .max_power = 25 },
1ebec3d7
TP
5161};
5162
1ebec3d7
TP
5163static struct ieee80211_supported_band wl1271_band_5ghz = {
5164 .channels = wl1271_channels_5ghz,
5165 .n_channels = ARRAY_SIZE(wl1271_channels_5ghz),
5166 .bitrates = wl1271_rates_5ghz,
5167 .n_bitrates = ARRAY_SIZE(wl1271_rates_5ghz),
f876bb9a
JO
5168};
5169
f5fc0f86
LC
5170static const struct ieee80211_ops wl1271_ops = {
5171 .start = wl1271_op_start,
c24ec83b 5172 .stop = wlcore_op_stop,
f5fc0f86
LC
5173 .add_interface = wl1271_op_add_interface,
5174 .remove_interface = wl1271_op_remove_interface,
c0fad1b7 5175 .change_interface = wl12xx_op_change_interface,
f634a4e7 5176#ifdef CONFIG_PM
402e4861
EP
5177 .suspend = wl1271_op_suspend,
5178 .resume = wl1271_op_resume,
f634a4e7 5179#endif
f5fc0f86 5180 .config = wl1271_op_config,
c87dec9f 5181 .prepare_multicast = wl1271_op_prepare_multicast,
f5fc0f86
LC
5182 .configure_filter = wl1271_op_configure_filter,
5183 .tx = wl1271_op_tx,
a1c597f2 5184 .set_key = wlcore_op_set_key,
f5fc0f86 5185 .hw_scan = wl1271_op_hw_scan,
73ecce31 5186 .cancel_hw_scan = wl1271_op_cancel_hw_scan,
33c2c06c
LC
5187 .sched_scan_start = wl1271_op_sched_scan_start,
5188 .sched_scan_stop = wl1271_op_sched_scan_stop,
f5fc0f86 5189 .bss_info_changed = wl1271_op_bss_info_changed,
68d069c4 5190 .set_frag_threshold = wl1271_op_set_frag_threshold,
f5fc0f86 5191 .set_rts_threshold = wl1271_op_set_rts_threshold,
c6999d83 5192 .conf_tx = wl1271_op_conf_tx,
bbbb538e 5193 .get_tsf = wl1271_op_get_tsf,
ece550d0 5194 .get_survey = wl1271_op_get_survey,
2d6cf2b5 5195 .sta_state = wl12xx_op_sta_state,
bbba3e68 5196 .ampdu_action = wl1271_op_ampdu_action,
33437893 5197 .tx_frames_pending = wl1271_tx_frames_pending,
af7fbb28 5198 .set_bitrate_mask = wl12xx_set_bitrate_mask,
6d158ff3 5199 .channel_switch = wl12xx_op_channel_switch,
d8ae5a25 5200 .flush = wlcore_op_flush,
dabf37db
EP
5201 .remain_on_channel = wlcore_op_remain_on_channel,
5202 .cancel_remain_on_channel = wlcore_op_cancel_remain_on_channel,
b6970ee5
EP
5203 .add_chanctx = wlcore_op_add_chanctx,
5204 .remove_chanctx = wlcore_op_remove_chanctx,
5205 .change_chanctx = wlcore_op_change_chanctx,
5206 .assign_vif_chanctx = wlcore_op_assign_vif_chanctx,
5207 .unassign_vif_chanctx = wlcore_op_unassign_vif_chanctx,
5f9b6777 5208 .sta_rc_update = wlcore_op_sta_rc_update,
c8c90873 5209 CFG80211_TESTMODE_CMD(wl1271_tm_cmd)
f5fc0f86
LC
5210};
5211
f876bb9a 5212
43a8bc5a 5213u8 wlcore_rate_to_idx(struct wl1271 *wl, u8 rate, enum ieee80211_band band)
f876bb9a
JO
5214{
5215 u8 idx;
5216
43a8bc5a 5217 BUG_ON(band >= 2);
f876bb9a 5218
43a8bc5a 5219 if (unlikely(rate >= wl->hw_tx_rate_tbl_size)) {
f876bb9a
JO
5220 wl1271_error("Illegal RX rate from HW: %d", rate);
5221 return 0;
5222 }
5223
43a8bc5a 5224 idx = wl->band_rate_to_idx[band][rate];
f876bb9a
JO
5225 if (unlikely(idx == CONF_HW_RXTX_RATE_UNSUPPORTED)) {
5226 wl1271_error("Unsupported RX rate from HW: %d", rate);
5227 return 0;
5228 }
5229
5230 return idx;
5231}
5232
7fc3a864
JO
5233static ssize_t wl1271_sysfs_show_bt_coex_state(struct device *dev,
5234 struct device_attribute *attr,
5235 char *buf)
5236{
5237 struct wl1271 *wl = dev_get_drvdata(dev);
5238 ssize_t len;
5239
2f63b011 5240 len = PAGE_SIZE;
7fc3a864
JO
5241
5242 mutex_lock(&wl->mutex);
5243 len = snprintf(buf, len, "%d\n\n0 - off\n1 - on\n",
5244 wl->sg_enabled);
5245 mutex_unlock(&wl->mutex);
5246
5247 return len;
5248
5249}
5250
5251static ssize_t wl1271_sysfs_store_bt_coex_state(struct device *dev,
5252 struct device_attribute *attr,
5253 const char *buf, size_t count)
5254{
5255 struct wl1271 *wl = dev_get_drvdata(dev);
5256 unsigned long res;
5257 int ret;
5258
6277ed65 5259 ret = kstrtoul(buf, 10, &res);
7fc3a864
JO
5260 if (ret < 0) {
5261 wl1271_warning("incorrect value written to bt_coex_mode");
5262 return count;
5263 }
5264
5265 mutex_lock(&wl->mutex);
5266
5267 res = !!res;
5268
5269 if (res == wl->sg_enabled)
5270 goto out;
5271
5272 wl->sg_enabled = res;
5273
4cc53383 5274 if (unlikely(wl->state != WLCORE_STATE_ON))
7fc3a864
JO
5275 goto out;
5276
a620865e 5277 ret = wl1271_ps_elp_wakeup(wl);
7fc3a864
JO
5278 if (ret < 0)
5279 goto out;
5280
5281 wl1271_acx_sg_enable(wl, wl->sg_enabled);
5282 wl1271_ps_elp_sleep(wl);
5283
5284 out:
5285 mutex_unlock(&wl->mutex);
5286 return count;
5287}
5288
5289static DEVICE_ATTR(bt_coex_state, S_IRUGO | S_IWUSR,
5290 wl1271_sysfs_show_bt_coex_state,
5291 wl1271_sysfs_store_bt_coex_state);
5292
d717fd61
JO
5293static ssize_t wl1271_sysfs_show_hw_pg_ver(struct device *dev,
5294 struct device_attribute *attr,
5295 char *buf)
5296{
5297 struct wl1271 *wl = dev_get_drvdata(dev);
5298 ssize_t len;
5299
2f63b011 5300 len = PAGE_SIZE;
d717fd61
JO
5301
5302 mutex_lock(&wl->mutex);
5303 if (wl->hw_pg_ver >= 0)
5304 len = snprintf(buf, len, "%d\n", wl->hw_pg_ver);
5305 else
5306 len = snprintf(buf, len, "n/a\n");
5307 mutex_unlock(&wl->mutex);
5308
5309 return len;
5310}
5311
6f07b72a 5312static DEVICE_ATTR(hw_pg_ver, S_IRUGO,
d717fd61
JO
5313 wl1271_sysfs_show_hw_pg_ver, NULL);
5314
95dac04f
IY
5315static ssize_t wl1271_sysfs_read_fwlog(struct file *filp, struct kobject *kobj,
5316 struct bin_attribute *bin_attr,
5317 char *buffer, loff_t pos, size_t count)
5318{
5319 struct device *dev = container_of(kobj, struct device, kobj);
5320 struct wl1271 *wl = dev_get_drvdata(dev);
5321 ssize_t len;
5322 int ret;
5323
5324 ret = mutex_lock_interruptible(&wl->mutex);
5325 if (ret < 0)
5326 return -ERESTARTSYS;
5327
5328 /* Let only one thread read the log at a time, blocking others */
5329 while (wl->fwlog_size == 0) {
5330 DEFINE_WAIT(wait);
5331
5332 prepare_to_wait_exclusive(&wl->fwlog_waitq,
5333 &wait,
5334 TASK_INTERRUPTIBLE);
5335
5336 if (wl->fwlog_size != 0) {
5337 finish_wait(&wl->fwlog_waitq, &wait);
5338 break;
5339 }
5340
5341 mutex_unlock(&wl->mutex);
5342
5343 schedule();
5344 finish_wait(&wl->fwlog_waitq, &wait);
5345
5346 if (signal_pending(current))
5347 return -ERESTARTSYS;
5348
5349 ret = mutex_lock_interruptible(&wl->mutex);
5350 if (ret < 0)
5351 return -ERESTARTSYS;
5352 }
5353
5354 /* Check if the fwlog is still valid */
5355 if (wl->fwlog_size < 0) {
5356 mutex_unlock(&wl->mutex);
5357 return 0;
5358 }
5359
5360 /* Seeking is not supported - old logs are not kept. Disregard pos. */
5361 len = min(count, (size_t)wl->fwlog_size);
5362 wl->fwlog_size -= len;
5363 memcpy(buffer, wl->fwlog, len);
5364
5365 /* Make room for new messages */
5366 memmove(wl->fwlog, wl->fwlog + len, wl->fwlog_size);
5367
5368 mutex_unlock(&wl->mutex);
5369
5370 return len;
5371}
5372
5373static struct bin_attribute fwlog_attr = {
5374 .attr = {.name = "fwlog", .mode = S_IRUSR},
5375 .read = wl1271_sysfs_read_fwlog,
5376};
5377
f4afbed9 5378static void wl12xx_derive_mac_addresses(struct wl1271 *wl, u32 oui, u32 nic)
5e037e74
LC
5379{
5380 int i;
5381
f4afbed9
AN
5382 wl1271_debug(DEBUG_PROBE, "base address: oui %06x nic %06x",
5383 oui, nic);
5e037e74 5384
f4afbed9 5385 if (nic + WLCORE_NUM_MAC_ADDRESSES - wl->num_mac_addr > 0xffffff)
5e037e74
LC
5386 wl1271_warning("NIC part of the MAC address wraps around!");
5387
f4afbed9 5388 for (i = 0; i < wl->num_mac_addr; i++) {
5e037e74
LC
5389 wl->addresses[i].addr[0] = (u8)(oui >> 16);
5390 wl->addresses[i].addr[1] = (u8)(oui >> 8);
5391 wl->addresses[i].addr[2] = (u8) oui;
5392 wl->addresses[i].addr[3] = (u8)(nic >> 16);
5393 wl->addresses[i].addr[4] = (u8)(nic >> 8);
5394 wl->addresses[i].addr[5] = (u8) nic;
5395 nic++;
5396 }
5397
f4afbed9
AN
5398 /* we may be one address short at the most */
5399 WARN_ON(wl->num_mac_addr + 1 < WLCORE_NUM_MAC_ADDRESSES);
5400
5401 /*
5402 * turn on the LAA bit in the first address and use it as
5403 * the last address.
5404 */
5405 if (wl->num_mac_addr < WLCORE_NUM_MAC_ADDRESSES) {
5406 int idx = WLCORE_NUM_MAC_ADDRESSES - 1;
5407 memcpy(&wl->addresses[idx], &wl->addresses[0],
5408 sizeof(wl->addresses[0]));
5409 /* LAA bit */
5410 wl->addresses[idx].addr[2] |= BIT(1);
5411 }
5412
5413 wl->hw->wiphy->n_addresses = WLCORE_NUM_MAC_ADDRESSES;
5e037e74
LC
5414 wl->hw->wiphy->addresses = wl->addresses;
5415}
5416
30c5dbd1
LC
5417static int wl12xx_get_hw_info(struct wl1271 *wl)
5418{
5419 int ret;
30c5dbd1
LC
5420
5421 ret = wl12xx_set_power_on(wl);
5422 if (ret < 0)
4fb4e0be 5423 return ret;
30c5dbd1 5424
6134323f
IY
5425 ret = wlcore_read_reg(wl, REG_CHIP_ID_B, &wl->chip.id);
5426 if (ret < 0)
5427 goto out;
30c5dbd1 5428
00782136
LC
5429 wl->fuse_oui_addr = 0;
5430 wl->fuse_nic_addr = 0;
30c5dbd1 5431
6134323f
IY
5432 ret = wl->ops->get_pg_ver(wl, &wl->hw_pg_ver);
5433 if (ret < 0)
5434 goto out;
30c5dbd1 5435
30d9b4a5 5436 if (wl->ops->get_mac)
6134323f 5437 ret = wl->ops->get_mac(wl);
5e037e74 5438
30c5dbd1 5439out:
6134323f 5440 wl1271_power_off(wl);
30c5dbd1
LC
5441 return ret;
5442}
5443
4b32a2c9 5444static int wl1271_register_hw(struct wl1271 *wl)
f5fc0f86
LC
5445{
5446 int ret;
5e037e74 5447 u32 oui_addr = 0, nic_addr = 0;
f5fc0f86
LC
5448
5449 if (wl->mac80211_registered)
5450 return 0;
5451
6f8d6b20 5452 if (wl->nvs_len >= 12) {
bc765bf3
SL
5453 /* NOTE: The wl->nvs->nvs element must be first, in
5454 * order to simplify the casting, we assume it is at
5455 * the beginning of the wl->nvs structure.
5456 */
5457 u8 *nvs_ptr = (u8 *)wl->nvs;
31d26ec6 5458
5e037e74
LC
5459 oui_addr =
5460 (nvs_ptr[11] << 16) + (nvs_ptr[10] << 8) + nvs_ptr[6];
5461 nic_addr =
5462 (nvs_ptr[5] << 16) + (nvs_ptr[4] << 8) + nvs_ptr[3];
5463 }
5464
5465 /* if the MAC address is zeroed in the NVS derive from fuse */
5466 if (oui_addr == 0 && nic_addr == 0) {
5467 oui_addr = wl->fuse_oui_addr;
5468 /* fuse has the BD_ADDR, the WLAN addresses are the next two */
5469 nic_addr = wl->fuse_nic_addr + 1;
31d26ec6
AN
5470 }
5471
f4afbed9 5472 wl12xx_derive_mac_addresses(wl, oui_addr, nic_addr);
f5fc0f86
LC
5473
5474 ret = ieee80211_register_hw(wl->hw);
5475 if (ret < 0) {
5476 wl1271_error("unable to register mac80211 hw: %d", ret);
30c5dbd1 5477 goto out;
f5fc0f86
LC
5478 }
5479
5480 wl->mac80211_registered = true;
5481
d60080ae
EP
5482 wl1271_debugfs_init(wl);
5483
f5fc0f86
LC
5484 wl1271_notice("loaded");
5485
30c5dbd1
LC
5486out:
5487 return ret;
f5fc0f86
LC
5488}
5489
4b32a2c9 5490static void wl1271_unregister_hw(struct wl1271 *wl)
3b56dd6a 5491{
3fcdab70 5492 if (wl->plt)
f3df1331 5493 wl1271_plt_stop(wl);
4ae3fa87 5494
3b56dd6a
TP
5495 ieee80211_unregister_hw(wl->hw);
5496 wl->mac80211_registered = false;
5497
5498}
3b56dd6a 5499
bcab320b
EP
5500static const struct ieee80211_iface_limit wlcore_iface_limits[] = {
5501 {
e7a6ba29 5502 .max = 3,
bcab320b
EP
5503 .types = BIT(NL80211_IFTYPE_STATION),
5504 },
5505 {
5506 .max = 1,
5507 .types = BIT(NL80211_IFTYPE_AP) |
5508 BIT(NL80211_IFTYPE_P2P_GO) |
5509 BIT(NL80211_IFTYPE_P2P_CLIENT),
5510 },
5511};
5512
de40750f 5513static struct ieee80211_iface_combination
bcab320b
EP
5514wlcore_iface_combinations[] = {
5515 {
e7a6ba29 5516 .max_interfaces = 3,
bcab320b
EP
5517 .limits = wlcore_iface_limits,
5518 .n_limits = ARRAY_SIZE(wlcore_iface_limits),
5519 },
5520};
5521
4b32a2c9 5522static int wl1271_init_ieee80211(struct wl1271 *wl)
f5fc0f86 5523{
7a55724e
JO
5524 static const u32 cipher_suites[] = {
5525 WLAN_CIPHER_SUITE_WEP40,
5526 WLAN_CIPHER_SUITE_WEP104,
5527 WLAN_CIPHER_SUITE_TKIP,
5528 WLAN_CIPHER_SUITE_CCMP,
5529 WL1271_CIPHER_SUITE_GEM,
5530 };
5531
2c0133a4
AN
5532 /* The tx descriptor buffer */
5533 wl->hw->extra_tx_headroom = sizeof(struct wl1271_tx_hw_descr);
5534
5535 if (wl->quirks & WLCORE_QUIRK_TKIP_HEADER_SPACE)
5536 wl->hw->extra_tx_headroom += WL1271_EXTRA_SPACE_TKIP;
f5fc0f86
LC
5537
5538 /* unit us */
5539 /* FIXME: find a proper value */
5540 wl->hw->channel_change_time = 10000;
50c500ad 5541 wl->hw->max_listen_interval = wl->conf.conn.max_listen_interval;
f5fc0f86
LC
5542
5543 wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
0a34332f 5544 IEEE80211_HW_SUPPORTS_PS |
f1d63a59 5545 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
4695dc91 5546 IEEE80211_HW_SUPPORTS_UAPSD |
a9af092b 5547 IEEE80211_HW_HAS_RATE_CONTROL |
00236aed 5548 IEEE80211_HW_CONNECTION_MONITOR |
25eaea30 5549 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
fcd23b63 5550 IEEE80211_HW_SPECTRUM_MGMT |
93f8c8e0
AN
5551 IEEE80211_HW_AP_LINK_PS |
5552 IEEE80211_HW_AMPDU_AGGREGATION |
79aba1ba
EP
5553 IEEE80211_HW_TX_AMPDU_SETUP_IN_HW |
5554 IEEE80211_HW_SCAN_WHILE_IDLE;
f5fc0f86 5555
7a55724e
JO
5556 wl->hw->wiphy->cipher_suites = cipher_suites;
5557 wl->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
5558
e0d8bbf0 5559 wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
045c745f
EP
5560 BIT(NL80211_IFTYPE_ADHOC) | BIT(NL80211_IFTYPE_AP) |
5561 BIT(NL80211_IFTYPE_P2P_CLIENT) | BIT(NL80211_IFTYPE_P2P_GO);
f5fc0f86 5562 wl->hw->wiphy->max_scan_ssids = 1;
221737d2
LC
5563 wl->hw->wiphy->max_sched_scan_ssids = 16;
5564 wl->hw->wiphy->max_match_sets = 16;
ea559b46
GE
5565 /*
5566 * Maximum length of elements in scanning probe request templates
5567 * should be the maximum length possible for a template, without
5568 * the IEEE80211 header of the template
5569 */
c08e371a 5570 wl->hw->wiphy->max_scan_ie_len = WL1271_CMD_TEMPL_MAX_SIZE -
ea559b46 5571 sizeof(struct ieee80211_header);
a8aaaf53 5572
c08e371a 5573 wl->hw->wiphy->max_sched_scan_ie_len = WL1271_CMD_TEMPL_MAX_SIZE -
c9e79a47
LC
5574 sizeof(struct ieee80211_header);
5575
dabf37db
EP
5576 wl->hw->wiphy->max_remain_on_channel_duration = 5000;
5577
81ddbb5c
JB
5578 wl->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD |
5579 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
1ec23f7f 5580
4a31c11c
LC
5581 /* make sure all our channels fit in the scanned_ch bitmask */
5582 BUILD_BUG_ON(ARRAY_SIZE(wl1271_channels) +
5583 ARRAY_SIZE(wl1271_channels_5ghz) >
5584 WL1271_MAX_CHANNELS);
a8aaaf53
LC
5585 /*
5586 * We keep local copies of the band structs because we need to
5587 * modify them on a per-device basis.
5588 */
5589 memcpy(&wl->bands[IEEE80211_BAND_2GHZ], &wl1271_band_2ghz,
5590 sizeof(wl1271_band_2ghz));
bfb92ca1
EP
5591 memcpy(&wl->bands[IEEE80211_BAND_2GHZ].ht_cap,
5592 &wl->ht_cap[IEEE80211_BAND_2GHZ],
5593 sizeof(*wl->ht_cap));
a8aaaf53
LC
5594 memcpy(&wl->bands[IEEE80211_BAND_5GHZ], &wl1271_band_5ghz,
5595 sizeof(wl1271_band_5ghz));
bfb92ca1
EP
5596 memcpy(&wl->bands[IEEE80211_BAND_5GHZ].ht_cap,
5597 &wl->ht_cap[IEEE80211_BAND_5GHZ],
5598 sizeof(*wl->ht_cap));
a8aaaf53
LC
5599
5600 wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
5601 &wl->bands[IEEE80211_BAND_2GHZ];
5602 wl->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
5603 &wl->bands[IEEE80211_BAND_5GHZ];
1ebec3d7 5604
12bd8949 5605 wl->hw->queues = 4;
31627dc5 5606 wl->hw->max_rates = 1;
12bd8949 5607
b7417d93
JO
5608 wl->hw->wiphy->reg_notifier = wl1271_reg_notify;
5609
9c1b190b
AN
5610 /* the FW answers probe-requests in AP-mode */
5611 wl->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
5612 wl->hw->wiphy->probe_resp_offload =
5613 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
5614 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
5615 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
5616
bcab320b 5617 /* allowed interface combinations */
de40750f 5618 wlcore_iface_combinations[0].num_different_channels = wl->num_channels;
bcab320b
EP
5619 wl->hw->wiphy->iface_combinations = wlcore_iface_combinations;
5620 wl->hw->wiphy->n_iface_combinations =
5621 ARRAY_SIZE(wlcore_iface_combinations);
5622
a390e85c 5623 SET_IEEE80211_DEV(wl->hw, wl->dev);
f5fc0f86 5624
f84f7d78 5625 wl->hw->sta_data_size = sizeof(struct wl1271_station);
87fbcb0f 5626 wl->hw->vif_data_size = sizeof(struct wl12xx_vif);
f84f7d78 5627
ba421f8f 5628 wl->hw->max_rx_aggregation_subframes = wl->conf.ht.rx_ba_win_size;
4c9cfa78 5629
f5fc0f86
LC
5630 return 0;
5631}
5632
f5fc0f86 5633#define WL1271_DEFAULT_CHANNEL 0
c332a4b8 5634
c50a2825
EP
5635struct ieee80211_hw *wlcore_alloc_hw(size_t priv_size, u32 aggr_buf_size,
5636 u32 mbox_size)
f5fc0f86 5637{
f5fc0f86
LC
5638 struct ieee80211_hw *hw;
5639 struct wl1271 *wl;
a8c0ddb5 5640 int i, j, ret;
1f37cbc9 5641 unsigned int order;
f5fc0f86 5642
c7ffb902 5643 BUILD_BUG_ON(AP_MAX_STATIONS > WL12XX_MAX_LINKS);
f80c2d12 5644
f5fc0f86
LC
5645 hw = ieee80211_alloc_hw(sizeof(*wl), &wl1271_ops);
5646 if (!hw) {
5647 wl1271_error("could not alloc ieee80211_hw");
a1dd8187 5648 ret = -ENOMEM;
3b56dd6a
TP
5649 goto err_hw_alloc;
5650 }
5651
f5fc0f86
LC
5652 wl = hw->priv;
5653 memset(wl, 0, sizeof(*wl));
5654
96e0c683
AN
5655 wl->priv = kzalloc(priv_size, GFP_KERNEL);
5656 if (!wl->priv) {
5657 wl1271_error("could not alloc wl priv");
5658 ret = -ENOMEM;
5659 goto err_priv_alloc;
5660 }
5661
87627214 5662 INIT_LIST_HEAD(&wl->wlvif_list);
01c09162 5663
f5fc0f86 5664 wl->hw = hw;
f5fc0f86 5665
a8c0ddb5 5666 for (i = 0; i < NUM_TX_QUEUES; i++)
c7ffb902 5667 for (j = 0; j < WL12XX_MAX_LINKS; j++)
a8c0ddb5
AN
5668 skb_queue_head_init(&wl->links[j].tx_queue[i]);
5669
a620865e
IY
5670 skb_queue_head_init(&wl->deferred_rx_queue);
5671 skb_queue_head_init(&wl->deferred_tx_queue);
5672
37b70a81 5673 INIT_DELAYED_WORK(&wl->elp_work, wl1271_elp_work);
a620865e 5674 INIT_WORK(&wl->netstack_work, wl1271_netstack_work);
117b38d0
JO
5675 INIT_WORK(&wl->tx_work, wl1271_tx_work);
5676 INIT_WORK(&wl->recovery_work, wl1271_recovery_work);
5677 INIT_DELAYED_WORK(&wl->scan_complete_work, wl1271_scan_complete_work);
dabf37db 5678 INIT_DELAYED_WORK(&wl->roc_complete_work, wlcore_roc_complete_work);
55df5afb 5679 INIT_DELAYED_WORK(&wl->tx_watchdog_work, wl12xx_tx_watchdog_work);
77ddaa10 5680
92ef8960
EP
5681 wl->freezable_wq = create_freezable_workqueue("wl12xx_wq");
5682 if (!wl->freezable_wq) {
5683 ret = -ENOMEM;
5684 goto err_hw;
5685 }
5686
f5fc0f86 5687 wl->channel = WL1271_DEFAULT_CHANNEL;
f5fc0f86 5688 wl->rx_counter = 0;
f5fc0f86 5689 wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
8a5a37a6 5690 wl->band = IEEE80211_BAND_2GHZ;
83d08d3f 5691 wl->channel_type = NL80211_CHAN_NO_HT;
830fb67b 5692 wl->flags = 0;
7fc3a864 5693 wl->sg_enabled = true;
66340e5b 5694 wl->sleep_auth = WL1271_PSM_ILLEGAL;
c108c905 5695 wl->recovery_count = 0;
d717fd61 5696 wl->hw_pg_ver = -1;
b622d992
AN
5697 wl->ap_ps_map = 0;
5698 wl->ap_fw_ps_map = 0;
606ea9fa 5699 wl->quirks = 0;
341b7cde 5700 wl->platform_quirks = 0;
f4df1bd5 5701 wl->system_hlid = WL12XX_SYSTEM_HLID;
da03209e 5702 wl->active_sta_count = 0;
95dac04f
IY
5703 wl->fwlog_size = 0;
5704 init_waitqueue_head(&wl->fwlog_waitq);
f5fc0f86 5705
f4df1bd5
EP
5706 /* The system link is always allocated */
5707 __set_bit(WL12XX_SYSTEM_HLID, wl->links_map);
5708
25eeb9e3 5709 memset(wl->tx_frames_map, 0, sizeof(wl->tx_frames_map));
72b0624f 5710 for (i = 0; i < wl->num_tx_desc; i++)
f5fc0f86
LC
5711 wl->tx_frames[i] = NULL;
5712
5713 spin_lock_init(&wl->wl_lock);
5714
4cc53383 5715 wl->state = WLCORE_STATE_OFF;
3fcdab70 5716 wl->fw_type = WL12XX_FW_TYPE_NONE;
f5fc0f86 5717 mutex_init(&wl->mutex);
2c38849f 5718 mutex_init(&wl->flush_mutex);
6f8d6b20 5719 init_completion(&wl->nvs_loading_complete);
f5fc0f86 5720
26a309c7 5721 order = get_order(aggr_buf_size);
1f37cbc9
IY
5722 wl->aggr_buf = (u8 *)__get_free_pages(GFP_KERNEL, order);
5723 if (!wl->aggr_buf) {
5724 ret = -ENOMEM;
92ef8960 5725 goto err_wq;
1f37cbc9 5726 }
26a309c7 5727 wl->aggr_buf_size = aggr_buf_size;
1f37cbc9 5728
990f5de7
IY
5729 wl->dummy_packet = wl12xx_alloc_dummy_packet(wl);
5730 if (!wl->dummy_packet) {
5731 ret = -ENOMEM;
5732 goto err_aggr;
5733 }
5734
95dac04f
IY
5735 /* Allocate one page for the FW log */
5736 wl->fwlog = (u8 *)get_zeroed_page(GFP_KERNEL);
5737 if (!wl->fwlog) {
5738 ret = -ENOMEM;
5739 goto err_dummy_packet;
5740 }
5741
c50a2825
EP
5742 wl->mbox_size = mbox_size;
5743 wl->mbox = kmalloc(wl->mbox_size, GFP_KERNEL | GFP_DMA);
690142e9
MG
5744 if (!wl->mbox) {
5745 ret = -ENOMEM;
5746 goto err_fwlog;
5747 }
5748
c332a4b8 5749 return hw;
a1dd8187 5750
690142e9
MG
5751err_fwlog:
5752 free_page((unsigned long)wl->fwlog);
5753
990f5de7
IY
5754err_dummy_packet:
5755 dev_kfree_skb(wl->dummy_packet);
5756
1f37cbc9
IY
5757err_aggr:
5758 free_pages((unsigned long)wl->aggr_buf, order);
5759
92ef8960
EP
5760err_wq:
5761 destroy_workqueue(wl->freezable_wq);
5762
a1dd8187 5763err_hw:
3b56dd6a 5764 wl1271_debugfs_exit(wl);
96e0c683
AN
5765 kfree(wl->priv);
5766
5767err_priv_alloc:
3b56dd6a
TP
5768 ieee80211_free_hw(hw);
5769
5770err_hw_alloc:
a1dd8187 5771
a1dd8187 5772 return ERR_PTR(ret);
c332a4b8 5773}
ffeb501c 5774EXPORT_SYMBOL_GPL(wlcore_alloc_hw);
c332a4b8 5775
ffeb501c 5776int wlcore_free_hw(struct wl1271 *wl)
c332a4b8 5777{
95dac04f
IY
5778 /* Unblock any fwlog readers */
5779 mutex_lock(&wl->mutex);
5780 wl->fwlog_size = -1;
5781 wake_up_interruptible_all(&wl->fwlog_waitq);
5782 mutex_unlock(&wl->mutex);
5783
f79f890c 5784 device_remove_bin_file(wl->dev, &fwlog_attr);
6f07b72a 5785
f79f890c 5786 device_remove_file(wl->dev, &dev_attr_hw_pg_ver);
6f07b72a 5787
f79f890c 5788 device_remove_file(wl->dev, &dev_attr_bt_coex_state);
a8e27820 5789 kfree(wl->mbox);
95dac04f 5790 free_page((unsigned long)wl->fwlog);
990f5de7 5791 dev_kfree_skb(wl->dummy_packet);
26a309c7 5792 free_pages((unsigned long)wl->aggr_buf, get_order(wl->aggr_buf_size));
c332a4b8
TP
5793
5794 wl1271_debugfs_exit(wl);
5795
c332a4b8
TP
5796 vfree(wl->fw);
5797 wl->fw = NULL;
3fcdab70 5798 wl->fw_type = WL12XX_FW_TYPE_NONE;
c332a4b8
TP
5799 kfree(wl->nvs);
5800 wl->nvs = NULL;
5801
0afd04e5 5802 kfree(wl->fw_status_1);
c332a4b8 5803 kfree(wl->tx_res_if);
92ef8960 5804 destroy_workqueue(wl->freezable_wq);
c332a4b8 5805
96e0c683 5806 kfree(wl->priv);
c332a4b8
TP
5807 ieee80211_free_hw(wl->hw);
5808
5809 return 0;
5810}
ffeb501c 5811EXPORT_SYMBOL_GPL(wlcore_free_hw);
50b3eb4b 5812
a390e85c
FB
5813static irqreturn_t wl12xx_hardirq(int irq, void *cookie)
5814{
5815 struct wl1271 *wl = cookie;
5816 unsigned long flags;
5817
5818 wl1271_debug(DEBUG_IRQ, "IRQ");
5819
5820 /* complete the ELP completion */
5821 spin_lock_irqsave(&wl->wl_lock, flags);
5822 set_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
5823 if (wl->elp_compl) {
5824 complete(wl->elp_compl);
5825 wl->elp_compl = NULL;
5826 }
5827
5828 if (test_bit(WL1271_FLAG_SUSPENDED, &wl->flags)) {
5829 /* don't enqueue a work right now. mark it as pending */
5830 set_bit(WL1271_FLAG_PENDING_WORK, &wl->flags);
5831 wl1271_debug(DEBUG_IRQ, "should not enqueue work");
5832 disable_irq_nosync(wl->irq);
5833 pm_wakeup_event(wl->dev, 0);
5834 spin_unlock_irqrestore(&wl->wl_lock, flags);
5835 return IRQ_HANDLED;
5836 }
5837 spin_unlock_irqrestore(&wl->wl_lock, flags);
5838
5839 return IRQ_WAKE_THREAD;
5840}
5841
6f8d6b20 5842static void wlcore_nvs_cb(const struct firmware *fw, void *context)
ce2a217c 5843{
6f8d6b20
IY
5844 struct wl1271 *wl = context;
5845 struct platform_device *pdev = wl->pdev;
a390e85c 5846 struct wl12xx_platform_data *pdata = pdev->dev.platform_data;
a390e85c 5847 unsigned long irqflags;
ffeb501c 5848 int ret;
a390e85c 5849
6f8d6b20
IY
5850 if (fw) {
5851 wl->nvs = kmemdup(fw->data, fw->size, GFP_KERNEL);
5852 if (!wl->nvs) {
5853 wl1271_error("Could not allocate nvs data");
5854 goto out;
5855 }
5856 wl->nvs_len = fw->size;
5857 } else {
5858 wl1271_debug(DEBUG_BOOT, "Could not get nvs file %s",
5859 WL12XX_NVS_NAME);
5860 wl->nvs = NULL;
5861 wl->nvs_len = 0;
a390e85c
FB
5862 }
5863
3992eb2b
IY
5864 ret = wl->ops->setup(wl);
5865 if (ret < 0)
6f8d6b20 5866 goto out_free_nvs;
3992eb2b 5867
72b0624f
AN
5868 BUG_ON(wl->num_tx_desc > WLCORE_MAX_TX_DESCRIPTORS);
5869
e87288f0
LC
5870 /* adjust some runtime configuration parameters */
5871 wlcore_adjust_conf(wl);
5872
a390e85c 5873 wl->irq = platform_get_irq(pdev, 0);
a390e85c
FB
5874 wl->platform_quirks = pdata->platform_quirks;
5875 wl->set_power = pdata->set_power;
a390e85c
FB
5876 wl->if_ops = pdata->ops;
5877
a390e85c
FB
5878 if (wl->platform_quirks & WL12XX_PLATFORM_QUIRK_EDGE_IRQ)
5879 irqflags = IRQF_TRIGGER_RISING;
5880 else
5881 irqflags = IRQF_TRIGGER_HIGH | IRQF_ONESHOT;
5882
b5b45b3c 5883 ret = request_threaded_irq(wl->irq, wl12xx_hardirq, wlcore_irq,
a390e85c
FB
5884 irqflags,
5885 pdev->name, wl);
5886 if (ret < 0) {
5887 wl1271_error("request_irq() failed: %d", ret);
6f8d6b20 5888 goto out_free_nvs;
a390e85c
FB
5889 }
5890
dfb89c56 5891#ifdef CONFIG_PM
a390e85c
FB
5892 ret = enable_irq_wake(wl->irq);
5893 if (!ret) {
5894 wl->irq_wake_enabled = true;
5895 device_init_wakeup(wl->dev, 1);
b95d7cef 5896 if (pdata->pwr_in_suspend) {
ffeb501c 5897 wl->hw->wiphy->wowlan.flags = WIPHY_WOWLAN_ANY;
b95d7cef
ES
5898 wl->hw->wiphy->wowlan.n_patterns =
5899 WL1271_MAX_RX_FILTERS;
5900 wl->hw->wiphy->wowlan.pattern_min_len = 1;
5901 wl->hw->wiphy->wowlan.pattern_max_len =
5902 WL1271_RX_FILTER_MAX_PATTERN_SIZE;
5903 }
a390e85c 5904 }
dfb89c56 5905#endif
a390e85c
FB
5906 disable_irq(wl->irq);
5907
4afc37a0
LC
5908 ret = wl12xx_get_hw_info(wl);
5909 if (ret < 0) {
5910 wl1271_error("couldn't get hw info");
8b425e62 5911 goto out_irq;
4afc37a0
LC
5912 }
5913
5914 ret = wl->ops->identify_chip(wl);
5915 if (ret < 0)
8b425e62 5916 goto out_irq;
4afc37a0 5917
a390e85c
FB
5918 ret = wl1271_init_ieee80211(wl);
5919 if (ret)
5920 goto out_irq;
5921
5922 ret = wl1271_register_hw(wl);
5923 if (ret)
5924 goto out_irq;
5925
f79f890c
FB
5926 /* Create sysfs file to control bt coex state */
5927 ret = device_create_file(wl->dev, &dev_attr_bt_coex_state);
5928 if (ret < 0) {
5929 wl1271_error("failed to create sysfs file bt_coex_state");
8b425e62 5930 goto out_unreg;
f79f890c
FB
5931 }
5932
5933 /* Create sysfs file to get HW PG version */
5934 ret = device_create_file(wl->dev, &dev_attr_hw_pg_ver);
5935 if (ret < 0) {
5936 wl1271_error("failed to create sysfs file hw_pg_ver");
5937 goto out_bt_coex_state;
5938 }
5939
5940 /* Create sysfs file for the FW log */
5941 ret = device_create_bin_file(wl->dev, &fwlog_attr);
5942 if (ret < 0) {
5943 wl1271_error("failed to create sysfs file fwlog");
5944 goto out_hw_pg_ver;
5945 }
5946
6f8d6b20 5947 wl->initialized = true;
ffeb501c 5948 goto out;
a390e85c 5949
f79f890c
FB
5950out_hw_pg_ver:
5951 device_remove_file(wl->dev, &dev_attr_hw_pg_ver);
5952
5953out_bt_coex_state:
5954 device_remove_file(wl->dev, &dev_attr_bt_coex_state);
5955
8b425e62
LC
5956out_unreg:
5957 wl1271_unregister_hw(wl);
5958
a390e85c
FB
5959out_irq:
5960 free_irq(wl->irq, wl);
5961
6f8d6b20
IY
5962out_free_nvs:
5963 kfree(wl->nvs);
5964
a390e85c 5965out:
6f8d6b20
IY
5966 release_firmware(fw);
5967 complete_all(&wl->nvs_loading_complete);
5968}
5969
5970int __devinit wlcore_probe(struct wl1271 *wl, struct platform_device *pdev)
5971{
5972 int ret;
5973
5974 if (!wl->ops || !wl->ptable)
5975 return -EINVAL;
5976
5977 wl->dev = &pdev->dev;
5978 wl->pdev = pdev;
5979 platform_set_drvdata(pdev, wl);
5980
5981 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
5982 WL12XX_NVS_NAME, &pdev->dev, GFP_KERNEL,
5983 wl, wlcore_nvs_cb);
5984 if (ret < 0) {
5985 wl1271_error("request_firmware_nowait failed: %d", ret);
5986 complete_all(&wl->nvs_loading_complete);
5987 }
5988
a390e85c 5989 return ret;
ce2a217c 5990}
b2ba99ff 5991EXPORT_SYMBOL_GPL(wlcore_probe);
ce2a217c 5992
b2ba99ff 5993int __devexit wlcore_remove(struct platform_device *pdev)
ce2a217c 5994{
a390e85c
FB
5995 struct wl1271 *wl = platform_get_drvdata(pdev);
5996
6f8d6b20
IY
5997 wait_for_completion(&wl->nvs_loading_complete);
5998 if (!wl->initialized)
5999 return 0;
6000
a390e85c
FB
6001 if (wl->irq_wake_enabled) {
6002 device_init_wakeup(wl->dev, 0);
6003 disable_irq_wake(wl->irq);
6004 }
6005 wl1271_unregister_hw(wl);
6006 free_irq(wl->irq, wl);
ffeb501c 6007 wlcore_free_hw(wl);
a390e85c 6008
ce2a217c
FB
6009 return 0;
6010}
b2ba99ff 6011EXPORT_SYMBOL_GPL(wlcore_remove);
ce2a217c 6012
491bbd6b 6013u32 wl12xx_debug_level = DEBUG_NONE;
17c1755c 6014EXPORT_SYMBOL_GPL(wl12xx_debug_level);
491bbd6b 6015module_param_named(debug_level, wl12xx_debug_level, uint, S_IRUSR | S_IWUSR);
17c1755c
EP
6016MODULE_PARM_DESC(debug_level, "wl12xx debugging level");
6017
95dac04f 6018module_param_named(fwlog, fwlog_param, charp, 0);
2c882fa4 6019MODULE_PARM_DESC(fwlog,
95dac04f
IY
6020 "FW logger options: continuous, ondemand, dbgpins or disable");
6021
7230341f 6022module_param(bug_on_recovery, int, S_IRUSR | S_IWUSR);
2a5bff09
EP
6023MODULE_PARM_DESC(bug_on_recovery, "BUG() on fw recovery");
6024
7230341f 6025module_param(no_recovery, int, S_IRUSR | S_IWUSR);
34785be5
AN
6026MODULE_PARM_DESC(no_recovery, "Prevent HW recovery. FW will remain stuck.");
6027
50b3eb4b 6028MODULE_LICENSE("GPL");
b1a48cab 6029MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
50b3eb4b 6030MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
0635ad45 6031MODULE_FIRMWARE(WL12XX_NVS_NAME);