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