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