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