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