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