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