wlcore: avoid using goto in normal code flow
[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
86 wl1271_info("Association completed.");
87 return 0;
88}
c2c192ac 89
b7417d93 90static int wl1271_reg_notify(struct wiphy *wiphy,
573c67cf
LC
91 struct regulatory_request *request)
92{
b7417d93
JO
93 struct ieee80211_supported_band *band;
94 struct ieee80211_channel *ch;
95 int i;
96
97 band = wiphy->bands[IEEE80211_BAND_5GHZ];
98 for (i = 0; i < band->n_channels; i++) {
99 ch = &band->channels[i];
100 if (ch->flags & IEEE80211_CHAN_DISABLED)
101 continue;
102
103 if (ch->flags & IEEE80211_CHAN_RADAR)
104 ch->flags |= IEEE80211_CHAN_NO_IBSS |
105 IEEE80211_CHAN_PASSIVE_SCAN;
106
107 }
108
109 return 0;
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
678 ieee80211_iterate_active_interfaces(hw,
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]);
42ec1f82 2038 wlvif->basic_rate_set = CONF_TX_ENABLED_RATES;
15e05bc0
LC
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 */
42ec1f82 2044 wlvif->basic_rate = CONF_TX_ENABLED_RATES;
15e05bc0 2045 /* TODO: this seems to be used only for STA, check it */
42ec1f82 2046 wlvif->rate_set = CONF_TX_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
3230f35e 2469static int wlcore_join(struct wl1271 *wl, struct wl12xx_vif *wlvif)
82429d32
JO
2470{
2471 int ret;
536129c8 2472 bool is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
82429d32 2473
69e5434c
JO
2474 /*
2475 * One of the side effects of the JOIN command is that is clears
2476 * WPA/WPA2 keys from the chipset. Performing a JOIN while associated
2477 * to a WPA/WPA2 access point will therefore kill the data-path.
8bf69aae
OBC
2478 * Currently the only valid scenario for JOIN during association
2479 * is on roaming, in which case we will also be given new keys.
2480 * Keep the below message for now, unless it starts bothering
2481 * users who really like to roam a lot :)
69e5434c 2482 */
ba8447f6 2483 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
69e5434c
JO
2484 wl1271_info("JOIN while associated.");
2485
5ec8a448
EP
2486 /* clear encryption type */
2487 wlvif->encryption_type = KEY_NONE;
2488
227e81e1 2489 if (is_ibss)
87fbcb0f 2490 ret = wl12xx_cmd_role_start_ibss(wl, wlvif);
18eab430
EP
2491 else {
2492 if (wl->quirks & WLCORE_QUIRK_START_STA_FAILS) {
2493 /*
2494 * TODO: this is an ugly workaround for wl12xx fw
2495 * bug - we are not able to tx/rx after the first
2496 * start_sta, so make dummy start+stop calls,
2497 * and then call start_sta again.
2498 * this should be fixed in the fw.
2499 */
2500 wl12xx_cmd_role_start_sta(wl, wlvif);
2501 wl12xx_cmd_role_stop_sta(wl, wlvif);
2502 }
2503
87fbcb0f 2504 ret = wl12xx_cmd_role_start_sta(wl, wlvif);
18eab430 2505 }
3230f35e
EP
2506
2507 return ret;
2508}
2509
2510static int wl1271_ssid_set(struct wl12xx_vif *wlvif, struct sk_buff *skb,
2511 int offset)
2512{
2513 u8 ssid_len;
2514 const u8 *ptr = cfg80211_find_ie(WLAN_EID_SSID, skb->data + offset,
2515 skb->len - offset);
2516
2517 if (!ptr) {
2518 wl1271_error("No SSID in IEs!");
2519 return -ENOENT;
2520 }
2521
2522 ssid_len = ptr[1];
2523 if (ssid_len > IEEE80211_MAX_SSID_LEN) {
2524 wl1271_error("SSID is too long!");
2525 return -EINVAL;
2526 }
2527
2528 wlvif->ssid_len = ssid_len;
2529 memcpy(wlvif->ssid, ptr+2, ssid_len);
2530 return 0;
2531}
2532
2533static int wlcore_set_ssid(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2534{
2535 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
2536 struct sk_buff *skb;
2537 int ieoffset;
2538
2539 /* we currently only support setting the ssid from the ap probe req */
2540 if (wlvif->bss_type != BSS_TYPE_STA_BSS)
2541 return -EINVAL;
2542
2543 skb = ieee80211_ap_probereq_get(wl->hw, vif);
2544 if (!skb)
2545 return -EINVAL;
2546
2547 ieoffset = offsetof(struct ieee80211_mgmt,
2548 u.probe_req.variable);
2549 wl1271_ssid_set(wlvif, skb, ieoffset);
2550 dev_kfree_skb(skb);
2551
2552 return 0;
2553}
2554
2555static int wlcore_set_assoc(struct wl1271 *wl, struct wl12xx_vif *wlvif,
ec87011a
EP
2556 struct ieee80211_bss_conf *bss_conf,
2557 u32 sta_rate_set)
3230f35e
EP
2558{
2559 int ieoffset;
2560 int ret;
2561
2562 wlvif->aid = bss_conf->aid;
2563 wlvif->channel_type = bss_conf->channel_type;
2564 wlvif->beacon_int = bss_conf->beacon_int;
2565
2566 set_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags);
2567
2568 /*
2569 * with wl1271, we don't need to update the
2570 * beacon_int and dtim_period, because the firmware
2571 * updates it by itself when the first beacon is
2572 * received after a join.
2573 */
2574 ret = wl1271_cmd_build_ps_poll(wl, wlvif, wlvif->aid);
82429d32 2575 if (ret < 0)
3230f35e 2576 return ret;
82429d32 2577
3230f35e
EP
2578 /*
2579 * Get a template for hardware connection maintenance
2580 */
2581 dev_kfree_skb(wlvif->probereq);
2582 wlvif->probereq = wl1271_cmd_build_ap_probe_req(wl,
2583 wlvif,
2584 NULL);
2585 ieoffset = offsetof(struct ieee80211_mgmt,
2586 u.probe_req.variable);
2587 wl1271_ssid_set(wlvif, wlvif->probereq, ieoffset);
2588
2589 /* enable the connection monitoring feature */
2590 ret = wl1271_acx_conn_monit_params(wl, wlvif, true);
2591 if (ret < 0)
2592 return ret;
82429d32
JO
2593
2594 /*
2595 * The join command disable the keep-alive mode, shut down its process,
2596 * and also clear the template config, so we need to reset it all after
2597 * the join. The acx_aid starts the keep-alive process, and the order
2598 * of the commands below is relevant.
2599 */
0603d891 2600 ret = wl1271_acx_keep_alive_mode(wl, wlvif, true);
82429d32 2601 if (ret < 0)
3230f35e 2602 return ret;
82429d32 2603
0603d891 2604 ret = wl1271_acx_aid(wl, wlvif, wlvif->aid);
82429d32 2605 if (ret < 0)
3230f35e 2606 return ret;
82429d32 2607
d2d66c56 2608 ret = wl12xx_cmd_build_klv_null_data(wl, wlvif);
82429d32 2609 if (ret < 0)
3230f35e 2610 return ret;
82429d32 2611
0603d891 2612 ret = wl1271_acx_keep_alive_config(wl, wlvif,
001e39a8 2613 wlvif->sta.klv_template_id,
82429d32
JO
2614 ACX_KEEP_ALIVE_TPL_VALID);
2615 if (ret < 0)
3230f35e 2616 return ret;
82429d32 2617
6c7b5194
EP
2618 /*
2619 * The default fw psm configuration is AUTO, while mac80211 default
2620 * setting is off (ACTIVE), so sync the fw with the correct value.
2621 */
2622 ret = wl1271_ps_set_mode(wl, wlvif, STATION_ACTIVE_MODE);
ec87011a
EP
2623 if (ret < 0)
2624 return ret;
2625
2626 if (sta_rate_set) {
2627 wlvif->rate_set =
2628 wl1271_tx_enabled_rates_get(wl,
2629 sta_rate_set,
2630 wlvif->band);
2631 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
2632 if (ret < 0)
2633 return ret;
2634 }
6c7b5194 2635
82429d32
JO
2636 return ret;
2637}
2638
3230f35e 2639static int wlcore_unset_assoc(struct wl1271 *wl, struct wl12xx_vif *wlvif)
c7f43e45
LC
2640{
2641 int ret;
3230f35e
EP
2642 bool sta = wlvif->bss_type == BSS_TYPE_STA_BSS;
2643
2644 /* make sure we are connected (sta) joined */
2645 if (sta &&
2646 !test_and_clear_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
2647 return false;
2648
2649 /* make sure we are joined (ibss) */
2650 if (!sta &&
2651 test_and_clear_bit(WLVIF_FLAG_IBSS_JOINED, &wlvif->flags))
2652 return false;
2653
2654 if (sta) {
2655 /* use defaults when not associated */
2656 wlvif->aid = 0;
2657
2658 /* free probe-request template */
2659 dev_kfree_skb(wlvif->probereq);
2660 wlvif->probereq = NULL;
2661
2662 /* disable connection monitor features */
2663 ret = wl1271_acx_conn_monit_params(wl, wlvif, false);
2664 if (ret < 0)
2665 return ret;
2666
2667 /* Disable the keep-alive feature */
2668 ret = wl1271_acx_keep_alive_mode(wl, wlvif, false);
2669 if (ret < 0)
2670 return ret;
2671 }
c7f43e45 2672
52630c5d 2673 if (test_and_clear_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags)) {
6e8cd331
EP
2674 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
2675
6d158ff3 2676 wl12xx_cmd_stop_channel_switch(wl);
6e8cd331 2677 ieee80211_chswitch_done(vif, false);
6d158ff3
SL
2678 }
2679
4137c17c
EP
2680 /* invalidate keep-alive template */
2681 wl1271_acx_keep_alive_config(wl, wlvif,
001e39a8 2682 wlvif->sta.klv_template_id,
4137c17c
EP
2683 ACX_KEEP_ALIVE_TPL_INVALID);
2684
b992c682 2685 /* reset TX security counters on a clean disconnect */
48e93e40
EP
2686 wlvif->tx_security_last_seq_lsb = 0;
2687 wlvif->tx_security_seq = 0;
b992c682 2688
3230f35e 2689 return 0;
c7f43e45
LC
2690}
2691
87fbcb0f 2692static void wl1271_set_band_rate(struct wl1271 *wl, struct wl12xx_vif *wlvif)
ebba60c6 2693{
1b92f15e 2694 wlvif->basic_rate_set = wlvif->bitrate_masks[wlvif->band];
30d0c8fd 2695 wlvif->rate_set = wlvif->basic_rate_set;
ebba60c6
JO
2696}
2697
9f259c4e
EP
2698static int wl12xx_config_vif(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2699 struct ieee80211_conf *conf, u32 changed)
f5fc0f86 2700{
9f259c4e 2701 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
b6970ee5 2702 int ret;
ebba60c6 2703
d18da7fc
ES
2704 if ((changed & IEEE80211_CONF_CHANGE_PS) && !is_ap) {
2705
2706 if ((conf->flags & IEEE80211_CONF_PS) &&
2707 test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) &&
5c0dc2fc 2708 !test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags)) {
d18da7fc 2709
5c0dc2fc
ES
2710 int ps_mode;
2711 char *ps_mode_str;
2712
2713 if (wl->conf.conn.forced_ps) {
2714 ps_mode = STATION_POWER_SAVE_MODE;
2715 ps_mode_str = "forced";
2716 } else {
2717 ps_mode = STATION_AUTO_PS_MODE;
2718 ps_mode_str = "auto";
2719 }
2720
2721 wl1271_debug(DEBUG_PSM, "%s ps enabled", ps_mode_str);
2722
2723 ret = wl1271_ps_set_mode(wl, wlvif, ps_mode);
f5fc0f86 2724
d18da7fc 2725 if (ret < 0)
5c0dc2fc
ES
2726 wl1271_warning("enter %s ps failed %d",
2727 ps_mode_str, ret);
f5fc0f86 2728
d18da7fc 2729 } else if (!(conf->flags & IEEE80211_CONF_PS) &&
5c0dc2fc 2730 test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags)) {
d18da7fc
ES
2731
2732 wl1271_debug(DEBUG_PSM, "auto ps disabled");
f5fc0f86 2733
0603d891 2734 ret = wl1271_ps_set_mode(wl, wlvif,
248a0018 2735 STATION_ACTIVE_MODE);
d18da7fc
ES
2736 if (ret < 0)
2737 wl1271_warning("exit auto ps failed %d", ret);
2738 }
f5fc0f86
LC
2739 }
2740
6bd65029 2741 if (conf->power_level != wlvif->power_level) {
0603d891 2742 ret = wl1271_acx_tx_power(wl, wlvif, conf->power_level);
f5fc0f86 2743 if (ret < 0)
9f259c4e 2744 return ret;
f5fc0f86 2745
6bd65029 2746 wlvif->power_level = conf->power_level;
f5fc0f86
LC
2747 }
2748
9f259c4e
EP
2749 return 0;
2750}
2751
2752static int wl1271_op_config(struct ieee80211_hw *hw, u32 changed)
2753{
2754 struct wl1271 *wl = hw->priv;
2755 struct wl12xx_vif *wlvif;
2756 struct ieee80211_conf *conf = &hw->conf;
b6970ee5 2757 int ret = 0;
9f259c4e 2758
b6970ee5 2759 wl1271_debug(DEBUG_MAC80211, "mac80211 config psm %s power %d %s"
9f259c4e 2760 " changed 0x%x",
9f259c4e
EP
2761 conf->flags & IEEE80211_CONF_PS ? "on" : "off",
2762 conf->power_level,
2763 conf->flags & IEEE80211_CONF_IDLE ? "idle" : "in use",
2764 changed);
2765
9f259c4e
EP
2766 mutex_lock(&wl->mutex);
2767
9f259c4e
EP
2768 if (changed & IEEE80211_CONF_CHANGE_POWER)
2769 wl->power_level = conf->power_level;
2770
4cc53383 2771 if (unlikely(wl->state != WLCORE_STATE_ON))
9f259c4e
EP
2772 goto out;
2773
2774 ret = wl1271_ps_elp_wakeup(wl);
2775 if (ret < 0)
2776 goto out;
2777
2778 /* configure each interface */
2779 wl12xx_for_each_wlvif(wl, wlvif) {
2780 ret = wl12xx_config_vif(wl, wlvif, conf, changed);
2781 if (ret < 0)
2782 goto out_sleep;
2783 }
2784
f5fc0f86
LC
2785out_sleep:
2786 wl1271_ps_elp_sleep(wl);
2787
2788out:
2789 mutex_unlock(&wl->mutex);
2790
2791 return ret;
2792}
2793
b54853f1
JO
2794struct wl1271_filter_params {
2795 bool enabled;
2796 int mc_list_length;
2797 u8 mc_list[ACX_MC_ADDRESS_GROUP_MAX][ETH_ALEN];
2798};
2799
22bedad3
JP
2800static u64 wl1271_op_prepare_multicast(struct ieee80211_hw *hw,
2801 struct netdev_hw_addr_list *mc_list)
c87dec9f 2802{
c87dec9f 2803 struct wl1271_filter_params *fp;
22bedad3 2804 struct netdev_hw_addr *ha;
c87dec9f 2805
74441130 2806 fp = kzalloc(sizeof(*fp), GFP_ATOMIC);
c87dec9f
JO
2807 if (!fp) {
2808 wl1271_error("Out of memory setting filters.");
2809 return 0;
2810 }
2811
2812 /* update multicast filtering parameters */
c87dec9f 2813 fp->mc_list_length = 0;
22bedad3
JP
2814 if (netdev_hw_addr_list_count(mc_list) > ACX_MC_ADDRESS_GROUP_MAX) {
2815 fp->enabled = false;
2816 } else {
2817 fp->enabled = true;
2818 netdev_hw_addr_list_for_each(ha, mc_list) {
c87dec9f 2819 memcpy(fp->mc_list[fp->mc_list_length],
22bedad3 2820 ha->addr, ETH_ALEN);
c87dec9f 2821 fp->mc_list_length++;
22bedad3 2822 }
c87dec9f
JO
2823 }
2824
b54853f1 2825 return (u64)(unsigned long)fp;
c87dec9f 2826}
f5fc0f86 2827
b54853f1
JO
2828#define WL1271_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
2829 FIF_ALLMULTI | \
2830 FIF_FCSFAIL | \
2831 FIF_BCN_PRBRESP_PROMISC | \
2832 FIF_CONTROL | \
2833 FIF_OTHER_BSS)
2834
f5fc0f86
LC
2835static void wl1271_op_configure_filter(struct ieee80211_hw *hw,
2836 unsigned int changed,
c87dec9f 2837 unsigned int *total, u64 multicast)
f5fc0f86 2838{
b54853f1 2839 struct wl1271_filter_params *fp = (void *)(unsigned long)multicast;
f5fc0f86 2840 struct wl1271 *wl = hw->priv;
6e8cd331 2841 struct wl12xx_vif *wlvif;
536129c8 2842
b54853f1 2843 int ret;
f5fc0f86 2844
7d057869
AN
2845 wl1271_debug(DEBUG_MAC80211, "mac80211 configure filter changed %x"
2846 " total %x", changed, *total);
f5fc0f86 2847
b54853f1
JO
2848 mutex_lock(&wl->mutex);
2849
2c10bb9c
SD
2850 *total &= WL1271_SUPPORTED_FILTERS;
2851 changed &= WL1271_SUPPORTED_FILTERS;
2852
4cc53383 2853 if (unlikely(wl->state != WLCORE_STATE_ON))
b54853f1
JO
2854 goto out;
2855
a620865e 2856 ret = wl1271_ps_elp_wakeup(wl);
b54853f1
JO
2857 if (ret < 0)
2858 goto out;
2859
6e8cd331
EP
2860 wl12xx_for_each_wlvif(wl, wlvif) {
2861 if (wlvif->bss_type != BSS_TYPE_AP_BSS) {
2862 if (*total & FIF_ALLMULTI)
2863 ret = wl1271_acx_group_address_tbl(wl, wlvif,
2864 false,
2865 NULL, 0);
2866 else if (fp)
2867 ret = wl1271_acx_group_address_tbl(wl, wlvif,
2868 fp->enabled,
2869 fp->mc_list,
2870 fp->mc_list_length);
2871 if (ret < 0)
2872 goto out_sleep;
2873 }
7d057869 2874 }
f5fc0f86 2875
08c1d1c7
EP
2876 /*
2877 * the fw doesn't provide an api to configure the filters. instead,
2878 * the filters configuration is based on the active roles / ROC
2879 * state.
2880 */
b54853f1
JO
2881
2882out_sleep:
2883 wl1271_ps_elp_sleep(wl);
2884
2885out:
2886 mutex_unlock(&wl->mutex);
14b228a0 2887 kfree(fp);
f5fc0f86
LC
2888}
2889
170d0e67
EP
2890static int wl1271_record_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2891 u8 id, u8 key_type, u8 key_size,
2892 const u8 *key, u8 hlid, u32 tx_seq_32,
2893 u16 tx_seq_16)
7f179b46
AN
2894{
2895 struct wl1271_ap_key *ap_key;
2896 int i;
2897
2898 wl1271_debug(DEBUG_CRYPT, "record ap key id %d", (int)id);
2899
2900 if (key_size > MAX_KEY_SIZE)
2901 return -EINVAL;
2902
2903 /*
2904 * Find next free entry in ap_keys. Also check we are not replacing
2905 * an existing key.
2906 */
2907 for (i = 0; i < MAX_NUM_KEYS; i++) {
170d0e67 2908 if (wlvif->ap.recorded_keys[i] == NULL)
7f179b46
AN
2909 break;
2910
170d0e67 2911 if (wlvif->ap.recorded_keys[i]->id == id) {
7f179b46
AN
2912 wl1271_warning("trying to record key replacement");
2913 return -EINVAL;
2914 }
2915 }
2916
2917 if (i == MAX_NUM_KEYS)
2918 return -EBUSY;
2919
2920 ap_key = kzalloc(sizeof(*ap_key), GFP_KERNEL);
2921 if (!ap_key)
2922 return -ENOMEM;
2923
2924 ap_key->id = id;
2925 ap_key->key_type = key_type;
2926 ap_key->key_size = key_size;
2927 memcpy(ap_key->key, key, key_size);
2928 ap_key->hlid = hlid;
2929 ap_key->tx_seq_32 = tx_seq_32;
2930 ap_key->tx_seq_16 = tx_seq_16;
2931
170d0e67 2932 wlvif->ap.recorded_keys[i] = ap_key;
7f179b46
AN
2933 return 0;
2934}
2935
170d0e67 2936static void wl1271_free_ap_keys(struct wl1271 *wl, struct wl12xx_vif *wlvif)
7f179b46
AN
2937{
2938 int i;
2939
2940 for (i = 0; i < MAX_NUM_KEYS; i++) {
170d0e67
EP
2941 kfree(wlvif->ap.recorded_keys[i]);
2942 wlvif->ap.recorded_keys[i] = NULL;
7f179b46
AN
2943 }
2944}
2945
a8ab39a4 2946static int wl1271_ap_init_hwenc(struct wl1271 *wl, struct wl12xx_vif *wlvif)
7f179b46
AN
2947{
2948 int i, ret = 0;
2949 struct wl1271_ap_key *key;
2950 bool wep_key_added = false;
2951
2952 for (i = 0; i < MAX_NUM_KEYS; i++) {
7f97b487 2953 u8 hlid;
170d0e67 2954 if (wlvif->ap.recorded_keys[i] == NULL)
7f179b46
AN
2955 break;
2956
170d0e67 2957 key = wlvif->ap.recorded_keys[i];
7f97b487
EP
2958 hlid = key->hlid;
2959 if (hlid == WL12XX_INVALID_LINK_ID)
a8ab39a4 2960 hlid = wlvif->ap.bcast_hlid;
7f97b487 2961
a8ab39a4 2962 ret = wl1271_cmd_set_ap_key(wl, wlvif, KEY_ADD_OR_REPLACE,
7f179b46
AN
2963 key->id, key->key_type,
2964 key->key_size, key->key,
7f97b487 2965 hlid, key->tx_seq_32,
7f179b46
AN
2966 key->tx_seq_16);
2967 if (ret < 0)
2968 goto out;
2969
2970 if (key->key_type == KEY_WEP)
2971 wep_key_added = true;
2972 }
2973
2974 if (wep_key_added) {
f75c753f 2975 ret = wl12xx_cmd_set_default_wep_key(wl, wlvif->default_key,
a8ab39a4 2976 wlvif->ap.bcast_hlid);
7f179b46
AN
2977 if (ret < 0)
2978 goto out;
2979 }
2980
2981out:
170d0e67 2982 wl1271_free_ap_keys(wl, wlvif);
7f179b46
AN
2983 return ret;
2984}
2985
536129c8
EP
2986static int wl1271_set_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2987 u16 action, u8 id, u8 key_type,
7f179b46
AN
2988 u8 key_size, const u8 *key, u32 tx_seq_32,
2989 u16 tx_seq_16, struct ieee80211_sta *sta)
2990{
2991 int ret;
536129c8 2992 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
7f179b46
AN
2993
2994 if (is_ap) {
2995 struct wl1271_station *wl_sta;
2996 u8 hlid;
2997
2998 if (sta) {
2999 wl_sta = (struct wl1271_station *)sta->drv_priv;
3000 hlid = wl_sta->hlid;
3001 } else {
a8ab39a4 3002 hlid = wlvif->ap.bcast_hlid;
7f179b46
AN
3003 }
3004
53d40d0b 3005 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
7f179b46
AN
3006 /*
3007 * We do not support removing keys after AP shutdown.
3008 * Pretend we do to make mac80211 happy.
3009 */
3010 if (action != KEY_ADD_OR_REPLACE)
3011 return 0;
3012
170d0e67 3013 ret = wl1271_record_ap_key(wl, wlvif, id,
7f179b46
AN
3014 key_type, key_size,
3015 key, hlid, tx_seq_32,
3016 tx_seq_16);
3017 } else {
a8ab39a4 3018 ret = wl1271_cmd_set_ap_key(wl, wlvif, action,
7f179b46
AN
3019 id, key_type, key_size,
3020 key, hlid, tx_seq_32,
3021 tx_seq_16);
3022 }
3023
3024 if (ret < 0)
3025 return ret;
3026 } else {
3027 const u8 *addr;
3028 static const u8 bcast_addr[ETH_ALEN] = {
3029 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
3030 };
3031
3032 addr = sta ? sta->addr : bcast_addr;
3033
3034 if (is_zero_ether_addr(addr)) {
3035 /* We dont support TX only encryption */
3036 return -EOPNOTSUPP;
3037 }
3038
3039 /* The wl1271 does not allow to remove unicast keys - they
3040 will be cleared automatically on next CMD_JOIN. Ignore the
3041 request silently, as we dont want the mac80211 to emit
3042 an error message. */
3043 if (action == KEY_REMOVE && !is_broadcast_ether_addr(addr))
3044 return 0;
3045
010d3d30
EP
3046 /* don't remove key if hlid was already deleted */
3047 if (action == KEY_REMOVE &&
154da67c 3048 wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
010d3d30
EP
3049 return 0;
3050
a8ab39a4 3051 ret = wl1271_cmd_set_sta_key(wl, wlvif, action,
7f179b46
AN
3052 id, key_type, key_size,
3053 key, addr, tx_seq_32,
3054 tx_seq_16);
3055 if (ret < 0)
3056 return ret;
3057
3058 /* the default WEP key needs to be configured at least once */
3059 if (key_type == KEY_WEP) {
c690ec81 3060 ret = wl12xx_cmd_set_default_wep_key(wl,
f75c753f
EP
3061 wlvif->default_key,
3062 wlvif->sta.hlid);
7f179b46
AN
3063 if (ret < 0)
3064 return ret;
3065 }
3066 }
3067
3068 return 0;
3069}
3070
a1c597f2 3071static int wlcore_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
f5fc0f86
LC
3072 struct ieee80211_vif *vif,
3073 struct ieee80211_sta *sta,
3074 struct ieee80211_key_conf *key_conf)
3075{
3076 struct wl1271 *wl = hw->priv;
af390f4d
EP
3077 int ret;
3078 bool might_change_spare =
3079 key_conf->cipher == WL1271_CIPHER_SUITE_GEM ||
3080 key_conf->cipher == WLAN_CIPHER_SUITE_TKIP;
3081
3082 if (might_change_spare) {
3083 /*
3084 * stop the queues and flush to ensure the next packets are
3085 * in sync with FW spare block accounting
3086 */
3087 mutex_lock(&wl->mutex);
3088 wlcore_stop_queues(wl, WLCORE_QUEUE_STOP_REASON_SPARE_BLK);
3089 mutex_unlock(&wl->mutex);
3090
3091 wl1271_tx_flush(wl);
3092 }
3093
3094 mutex_lock(&wl->mutex);
3095
3096 if (unlikely(wl->state != WLCORE_STATE_ON)) {
3097 ret = -EAGAIN;
3098 goto out_wake_queues;
3099 }
a1c597f2 3100
af390f4d
EP
3101 ret = wl1271_ps_elp_wakeup(wl);
3102 if (ret < 0)
3103 goto out_wake_queues;
3104
3105 ret = wlcore_hw_set_key(wl, cmd, vif, sta, key_conf);
3106
3107 wl1271_ps_elp_sleep(wl);
3108
3109out_wake_queues:
3110 if (might_change_spare)
3111 wlcore_wake_queues(wl, WLCORE_QUEUE_STOP_REASON_SPARE_BLK);
3112
3113 mutex_unlock(&wl->mutex);
3114
3115 return ret;
a1c597f2
AN
3116}
3117
3118int wlcore_set_key(struct wl1271 *wl, enum set_key_cmd cmd,
3119 struct ieee80211_vif *vif,
3120 struct ieee80211_sta *sta,
3121 struct ieee80211_key_conf *key_conf)
3122{
536129c8 3123 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
f5fc0f86 3124 int ret;
ac4e4ce5
JO
3125 u32 tx_seq_32 = 0;
3126 u16 tx_seq_16 = 0;
f5fc0f86
LC
3127 u8 key_type;
3128
f5fc0f86
LC
3129 wl1271_debug(DEBUG_MAC80211, "mac80211 set key");
3130
7f179b46 3131 wl1271_debug(DEBUG_CRYPT, "CMD: 0x%x sta: %p", cmd, sta);
f5fc0f86 3132 wl1271_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
97359d12 3133 key_conf->cipher, key_conf->keyidx,
f5fc0f86
LC
3134 key_conf->keylen, key_conf->flags);
3135 wl1271_dump(DEBUG_CRYPT, "KEY: ", key_conf->key, key_conf->keylen);
3136
97359d12
JB
3137 switch (key_conf->cipher) {
3138 case WLAN_CIPHER_SUITE_WEP40:
3139 case WLAN_CIPHER_SUITE_WEP104:
f5fc0f86
LC
3140 key_type = KEY_WEP;
3141
3142 key_conf->hw_key_idx = key_conf->keyidx;
3143 break;
97359d12 3144 case WLAN_CIPHER_SUITE_TKIP:
f5fc0f86
LC
3145 key_type = KEY_TKIP;
3146
3147 key_conf->hw_key_idx = key_conf->keyidx;
48e93e40
EP
3148 tx_seq_32 = WL1271_TX_SECURITY_HI32(wlvif->tx_security_seq);
3149 tx_seq_16 = WL1271_TX_SECURITY_LO16(wlvif->tx_security_seq);
f5fc0f86 3150 break;
97359d12 3151 case WLAN_CIPHER_SUITE_CCMP:
f5fc0f86
LC
3152 key_type = KEY_AES;
3153
12d4b975 3154 key_conf->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
48e93e40
EP
3155 tx_seq_32 = WL1271_TX_SECURITY_HI32(wlvif->tx_security_seq);
3156 tx_seq_16 = WL1271_TX_SECURITY_LO16(wlvif->tx_security_seq);
f5fc0f86 3157 break;
7a55724e
JO
3158 case WL1271_CIPHER_SUITE_GEM:
3159 key_type = KEY_GEM;
48e93e40
EP
3160 tx_seq_32 = WL1271_TX_SECURITY_HI32(wlvif->tx_security_seq);
3161 tx_seq_16 = WL1271_TX_SECURITY_LO16(wlvif->tx_security_seq);
7a55724e 3162 break;
f5fc0f86 3163 default:
97359d12 3164 wl1271_error("Unknown key algo 0x%x", key_conf->cipher);
f5fc0f86 3165
af390f4d 3166 return -EOPNOTSUPP;
f5fc0f86
LC
3167 }
3168
3169 switch (cmd) {
3170 case SET_KEY:
536129c8 3171 ret = wl1271_set_key(wl, wlvif, KEY_ADD_OR_REPLACE,
7f179b46
AN
3172 key_conf->keyidx, key_type,
3173 key_conf->keylen, key_conf->key,
3174 tx_seq_32, tx_seq_16, sta);
f5fc0f86
LC
3175 if (ret < 0) {
3176 wl1271_error("Could not add or replace key");
af390f4d 3177 return ret;
f5fc0f86 3178 }
5ec8a448
EP
3179
3180 /*
3181 * reconfiguring arp response if the unicast (or common)
3182 * encryption key type was changed
3183 */
3184 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
3185 (sta || key_type == KEY_WEP) &&
3186 wlvif->encryption_type != key_type) {
3187 wlvif->encryption_type = key_type;
3188 ret = wl1271_cmd_build_arp_rsp(wl, wlvif);
3189 if (ret < 0) {
3190 wl1271_warning("build arp rsp failed: %d", ret);
af390f4d 3191 return ret;
5ec8a448
EP
3192 }
3193 }
f5fc0f86
LC
3194 break;
3195
3196 case DISABLE_KEY:
536129c8 3197 ret = wl1271_set_key(wl, wlvif, KEY_REMOVE,
7f179b46
AN
3198 key_conf->keyidx, key_type,
3199 key_conf->keylen, key_conf->key,
3200 0, 0, sta);
f5fc0f86
LC
3201 if (ret < 0) {
3202 wl1271_error("Could not remove key");
af390f4d 3203 return ret;
f5fc0f86
LC
3204 }
3205 break;
3206
3207 default:
3208 wl1271_error("Unsupported key cmd 0x%x", cmd);
af390f4d 3209 return -EOPNOTSUPP;
f5fc0f86
LC
3210 }
3211
f5fc0f86
LC
3212 return ret;
3213}
a1c597f2 3214EXPORT_SYMBOL_GPL(wlcore_set_key);
f5fc0f86
LC
3215
3216static int wl1271_op_hw_scan(struct ieee80211_hw *hw,
a060bbfe 3217 struct ieee80211_vif *vif,
f5fc0f86
LC
3218 struct cfg80211_scan_request *req)
3219{
3220 struct wl1271 *wl = hw->priv;
3221 int ret;
3222 u8 *ssid = NULL;
abb0b3bf 3223 size_t len = 0;
f5fc0f86
LC
3224
3225 wl1271_debug(DEBUG_MAC80211, "mac80211 hw scan");
3226
3227 if (req->n_ssids) {
3228 ssid = req->ssids[0].ssid;
abb0b3bf 3229 len = req->ssids[0].ssid_len;
f5fc0f86
LC
3230 }
3231
3232 mutex_lock(&wl->mutex);
3233
4cc53383 3234 if (unlikely(wl->state != WLCORE_STATE_ON)) {
b739a42c
JO
3235 /*
3236 * We cannot return -EBUSY here because cfg80211 will expect
3237 * a call to ieee80211_scan_completed if we do - in this case
3238 * there won't be any call.
3239 */
3240 ret = -EAGAIN;
3241 goto out;
3242 }
3243
a620865e 3244 ret = wl1271_ps_elp_wakeup(wl);
f5fc0f86
LC
3245 if (ret < 0)
3246 goto out;
3247
97fd311a
EP
3248 /* fail if there is any role in ROC */
3249 if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES) {
92e712da
EP
3250 /* don't allow scanning right now */
3251 ret = -EBUSY;
3252 goto out_sleep;
3253 }
3254
784f694d 3255 ret = wl1271_scan(hw->priv, vif, ssid, len, req);
251c177f 3256out_sleep:
f5fc0f86 3257 wl1271_ps_elp_sleep(wl);
f5fc0f86
LC
3258out:
3259 mutex_unlock(&wl->mutex);
3260
3261 return ret;
3262}
3263
73ecce31
EP
3264static void wl1271_op_cancel_hw_scan(struct ieee80211_hw *hw,
3265 struct ieee80211_vif *vif)
3266{
3267 struct wl1271 *wl = hw->priv;
3268 int ret;
3269
3270 wl1271_debug(DEBUG_MAC80211, "mac80211 cancel hw scan");
3271
3272 mutex_lock(&wl->mutex);
3273
4cc53383 3274 if (unlikely(wl->state != WLCORE_STATE_ON))
73ecce31
EP
3275 goto out;
3276
3277 if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
3278 goto out;
3279
3280 ret = wl1271_ps_elp_wakeup(wl);
3281 if (ret < 0)
3282 goto out;
3283
3284 if (wl->scan.state != WL1271_SCAN_STATE_DONE) {
3285 ret = wl1271_scan_stop(wl);
3286 if (ret < 0)
3287 goto out_sleep;
3288 }
55df5afb
AN
3289
3290 /*
3291 * Rearm the tx watchdog just before idling scan. This
3292 * prevents just-finished scans from triggering the watchdog
3293 */
3294 wl12xx_rearm_tx_watchdog_locked(wl);
3295
73ecce31
EP
3296 wl->scan.state = WL1271_SCAN_STATE_IDLE;
3297 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
784f694d 3298 wl->scan_vif = NULL;
73ecce31
EP
3299 wl->scan.req = NULL;
3300 ieee80211_scan_completed(wl->hw, true);
3301
3302out_sleep:
3303 wl1271_ps_elp_sleep(wl);
3304out:
3305 mutex_unlock(&wl->mutex);
3306
3307 cancel_delayed_work_sync(&wl->scan_complete_work);
3308}
3309
33c2c06c
LC
3310static int wl1271_op_sched_scan_start(struct ieee80211_hw *hw,
3311 struct ieee80211_vif *vif,
3312 struct cfg80211_sched_scan_request *req,
3313 struct ieee80211_sched_scan_ies *ies)
3314{
3315 struct wl1271 *wl = hw->priv;
536129c8 3316 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
33c2c06c
LC
3317 int ret;
3318
3319 wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_start");
3320
3321 mutex_lock(&wl->mutex);
3322
4cc53383 3323 if (unlikely(wl->state != WLCORE_STATE_ON)) {
9e0dc890
PF
3324 ret = -EAGAIN;
3325 goto out;
3326 }
3327
33c2c06c
LC
3328 ret = wl1271_ps_elp_wakeup(wl);
3329 if (ret < 0)
3330 goto out;
3331
536129c8 3332 ret = wl1271_scan_sched_scan_config(wl, wlvif, req, ies);
33c2c06c
LC
3333 if (ret < 0)
3334 goto out_sleep;
3335
536129c8 3336 ret = wl1271_scan_sched_scan_start(wl, wlvif);
33c2c06c
LC
3337 if (ret < 0)
3338 goto out_sleep;
3339
3340 wl->sched_scanning = true;
3341
3342out_sleep:
3343 wl1271_ps_elp_sleep(wl);
3344out:
3345 mutex_unlock(&wl->mutex);
3346 return ret;
3347}
3348
3349static void wl1271_op_sched_scan_stop(struct ieee80211_hw *hw,
3350 struct ieee80211_vif *vif)
3351{
3352 struct wl1271 *wl = hw->priv;
78f85f50 3353 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
33c2c06c
LC
3354 int ret;
3355
3356 wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_stop");
3357
3358 mutex_lock(&wl->mutex);
3359
4cc53383 3360 if (unlikely(wl->state != WLCORE_STATE_ON))
9e0dc890
PF
3361 goto out;
3362
33c2c06c
LC
3363 ret = wl1271_ps_elp_wakeup(wl);
3364 if (ret < 0)
3365 goto out;
3366
78f85f50 3367 wl1271_scan_sched_scan_stop(wl, wlvif);
33c2c06c
LC
3368
3369 wl1271_ps_elp_sleep(wl);
3370out:
3371 mutex_unlock(&wl->mutex);
3372}
3373
68d069c4
AN
3374static int wl1271_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
3375{
3376 struct wl1271 *wl = hw->priv;
3377 int ret = 0;
3378
3379 mutex_lock(&wl->mutex);
3380
4cc53383 3381 if (unlikely(wl->state != WLCORE_STATE_ON)) {
68d069c4
AN
3382 ret = -EAGAIN;
3383 goto out;
3384 }
3385
a620865e 3386 ret = wl1271_ps_elp_wakeup(wl);
68d069c4
AN
3387 if (ret < 0)
3388 goto out;
3389
5f704d18 3390 ret = wl1271_acx_frag_threshold(wl, value);
68d069c4
AN
3391 if (ret < 0)
3392 wl1271_warning("wl1271_op_set_frag_threshold failed: %d", ret);
3393
3394 wl1271_ps_elp_sleep(wl);
3395
3396out:
3397 mutex_unlock(&wl->mutex);
3398
3399 return ret;
3400}
3401
f5fc0f86
LC
3402static int wl1271_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3403{
3404 struct wl1271 *wl = hw->priv;
6e8cd331 3405 struct wl12xx_vif *wlvif;
aecb0565 3406 int ret = 0;
f5fc0f86
LC
3407
3408 mutex_lock(&wl->mutex);
3409
4cc53383 3410 if (unlikely(wl->state != WLCORE_STATE_ON)) {
f8d9802f 3411 ret = -EAGAIN;
aecb0565 3412 goto out;
f8d9802f 3413 }
aecb0565 3414
a620865e 3415 ret = wl1271_ps_elp_wakeup(wl);
f5fc0f86
LC
3416 if (ret < 0)
3417 goto out;
3418
6e8cd331
EP
3419 wl12xx_for_each_wlvif(wl, wlvif) {
3420 ret = wl1271_acx_rts_threshold(wl, wlvif, value);
3421 if (ret < 0)
3422 wl1271_warning("set rts threshold failed: %d", ret);
3423 }
f5fc0f86
LC
3424 wl1271_ps_elp_sleep(wl);
3425
3426out:
3427 mutex_unlock(&wl->mutex);
3428
3429 return ret;
3430}
3431
d48055d9
EP
3432static void wl12xx_remove_ie(struct sk_buff *skb, u8 eid, int ieoffset)
3433{
3434 int len;
3435 const u8 *next, *end = skb->data + skb->len;
3436 u8 *ie = (u8 *)cfg80211_find_ie(eid, skb->data + ieoffset,
3437 skb->len - ieoffset);
3438 if (!ie)
3439 return;
3440 len = ie[1] + 2;
3441 next = ie + len;
3442 memmove(ie, next, end - next);
3443 skb_trim(skb, skb->len - len);
3444}
3445
26b4bf2e
EP
3446static void wl12xx_remove_vendor_ie(struct sk_buff *skb,
3447 unsigned int oui, u8 oui_type,
3448 int ieoffset)
3449{
3450 int len;
3451 const u8 *next, *end = skb->data + skb->len;
3452 u8 *ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
3453 skb->data + ieoffset,
3454 skb->len - ieoffset);
3455 if (!ie)
3456 return;
3457 len = ie[1] + 2;
3458 next = ie + len;
3459 memmove(ie, next, end - next);
3460 skb_trim(skb, skb->len - len);
3461}
3462
341f2c11
AN
3463static int wl1271_ap_set_probe_resp_tmpl(struct wl1271 *wl, u32 rates,
3464 struct ieee80211_vif *vif)
560f0024 3465{
cdaac628 3466 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
560f0024
AN
3467 struct sk_buff *skb;
3468 int ret;
3469
341f2c11 3470 skb = ieee80211_proberesp_get(wl->hw, vif);
560f0024 3471 if (!skb)
341f2c11 3472 return -EOPNOTSUPP;
560f0024 3473
cdaac628 3474 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
560f0024
AN
3475 CMD_TEMPL_AP_PROBE_RESPONSE,
3476 skb->data,
3477 skb->len, 0,
3478 rates);
560f0024 3479 dev_kfree_skb(skb);
62c2e579
LC
3480
3481 if (ret < 0)
3482 goto out;
3483
3484 wl1271_debug(DEBUG_AP, "probe response updated");
3485 set_bit(WLVIF_FLAG_AP_PROBE_RESP_SET, &wlvif->flags);
3486
3487out:
560f0024
AN
3488 return ret;
3489}
3490
3491static int wl1271_ap_set_probe_resp_tmpl_legacy(struct wl1271 *wl,
3492 struct ieee80211_vif *vif,
3493 u8 *probe_rsp_data,
3494 size_t probe_rsp_len,
3495 u32 rates)
68eaaf6e 3496{
1fe9f161
EP
3497 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3498 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
68eaaf6e
AN
3499 u8 probe_rsp_templ[WL1271_CMD_TEMPL_MAX_SIZE];
3500 int ssid_ie_offset, ie_offset, templ_len;
3501 const u8 *ptr;
3502
3503 /* no need to change probe response if the SSID is set correctly */
1fe9f161 3504 if (wlvif->ssid_len > 0)
cdaac628 3505 return wl1271_cmd_template_set(wl, wlvif->role_id,
68eaaf6e
AN
3506 CMD_TEMPL_AP_PROBE_RESPONSE,
3507 probe_rsp_data,
3508 probe_rsp_len, 0,
3509 rates);
3510
3511 if (probe_rsp_len + bss_conf->ssid_len > WL1271_CMD_TEMPL_MAX_SIZE) {
3512 wl1271_error("probe_rsp template too big");
3513 return -EINVAL;
3514 }
3515
3516 /* start searching from IE offset */
3517 ie_offset = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
3518
3519 ptr = cfg80211_find_ie(WLAN_EID_SSID, probe_rsp_data + ie_offset,
3520 probe_rsp_len - ie_offset);
3521 if (!ptr) {
3522 wl1271_error("No SSID in beacon!");
3523 return -EINVAL;
3524 }
3525
3526 ssid_ie_offset = ptr - probe_rsp_data;
3527 ptr += (ptr[1] + 2);
3528
3529 memcpy(probe_rsp_templ, probe_rsp_data, ssid_ie_offset);
3530
3531 /* insert SSID from bss_conf */
3532 probe_rsp_templ[ssid_ie_offset] = WLAN_EID_SSID;
3533 probe_rsp_templ[ssid_ie_offset + 1] = bss_conf->ssid_len;
3534 memcpy(probe_rsp_templ + ssid_ie_offset + 2,
3535 bss_conf->ssid, bss_conf->ssid_len);
3536 templ_len = ssid_ie_offset + 2 + bss_conf->ssid_len;
3537
3538 memcpy(probe_rsp_templ + ssid_ie_offset + 2 + bss_conf->ssid_len,
3539 ptr, probe_rsp_len - (ptr - probe_rsp_data));
3540 templ_len += probe_rsp_len - (ptr - probe_rsp_data);
3541
cdaac628 3542 return wl1271_cmd_template_set(wl, wlvif->role_id,
68eaaf6e
AN
3543 CMD_TEMPL_AP_PROBE_RESPONSE,
3544 probe_rsp_templ,
3545 templ_len, 0,
3546 rates);
3547}
3548
e78a287a 3549static int wl1271_bss_erp_info_changed(struct wl1271 *wl,
0603d891 3550 struct ieee80211_vif *vif,
f5fc0f86
LC
3551 struct ieee80211_bss_conf *bss_conf,
3552 u32 changed)
3553{
0603d891 3554 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
e78a287a 3555 int ret = 0;
f5fc0f86 3556
e78a287a
AN
3557 if (changed & BSS_CHANGED_ERP_SLOT) {
3558 if (bss_conf->use_short_slot)
0603d891 3559 ret = wl1271_acx_slot(wl, wlvif, SLOT_TIME_SHORT);
e78a287a 3560 else
0603d891 3561 ret = wl1271_acx_slot(wl, wlvif, SLOT_TIME_LONG);
e78a287a
AN
3562 if (ret < 0) {
3563 wl1271_warning("Set slot time failed %d", ret);
3564 goto out;
3565 }
3566 }
f5fc0f86 3567
e78a287a
AN
3568 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3569 if (bss_conf->use_short_preamble)
0603d891 3570 wl1271_acx_set_preamble(wl, wlvif, ACX_PREAMBLE_SHORT);
e78a287a 3571 else
0603d891 3572 wl1271_acx_set_preamble(wl, wlvif, ACX_PREAMBLE_LONG);
e78a287a 3573 }
f5fc0f86 3574
e78a287a
AN
3575 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
3576 if (bss_conf->use_cts_prot)
0603d891
EP
3577 ret = wl1271_acx_cts_protect(wl, wlvif,
3578 CTSPROTECT_ENABLE);
e78a287a 3579 else
0603d891
EP
3580 ret = wl1271_acx_cts_protect(wl, wlvif,
3581 CTSPROTECT_DISABLE);
e78a287a
AN
3582 if (ret < 0) {
3583 wl1271_warning("Set ctsprotect failed %d", ret);
3584 goto out;
3585 }
3586 }
f8d9802f 3587
e78a287a
AN
3588out:
3589 return ret;
3590}
f5fc0f86 3591
62c2e579
LC
3592static int wlcore_set_beacon_template(struct wl1271 *wl,
3593 struct ieee80211_vif *vif,
3594 bool is_ap)
3595{
3596 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3597 struct ieee80211_hdr *hdr;
3598 u32 min_rate;
3599 int ret;
3600 int ieoffset = offsetof(struct ieee80211_mgmt,
3601 u.beacon.variable);
3602 struct sk_buff *beacon = ieee80211_beacon_get(wl->hw, vif);
3603 u16 tmpl_id;
3604
3605 if (!beacon) {
3606 ret = -EINVAL;
3607 goto out;
3608 }
3609
3610 wl1271_debug(DEBUG_MASTER, "beacon updated");
3611
3230f35e 3612 ret = wl1271_ssid_set(wlvif, beacon, ieoffset);
62c2e579
LC
3613 if (ret < 0) {
3614 dev_kfree_skb(beacon);
3615 goto out;
3616 }
3617 min_rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
3618 tmpl_id = is_ap ? CMD_TEMPL_AP_BEACON :
3619 CMD_TEMPL_BEACON;
3620 ret = wl1271_cmd_template_set(wl, wlvif->role_id, tmpl_id,
3621 beacon->data,
3622 beacon->len, 0,
3623 min_rate);
3624 if (ret < 0) {
3625 dev_kfree_skb(beacon);
3626 goto out;
3627 }
3628
3629 /*
3630 * In case we already have a probe-resp beacon set explicitly
3631 * by usermode, don't use the beacon data.
3632 */
3633 if (test_bit(WLVIF_FLAG_AP_PROBE_RESP_SET, &wlvif->flags))
3634 goto end_bcn;
3635
3636 /* remove TIM ie from probe response */
3637 wl12xx_remove_ie(beacon, WLAN_EID_TIM, ieoffset);
3638
3639 /*
3640 * remove p2p ie from probe response.
3641 * the fw reponds to probe requests that don't include
3642 * the p2p ie. probe requests with p2p ie will be passed,
3643 * and will be responded by the supplicant (the spec
3644 * forbids including the p2p ie when responding to probe
3645 * requests that didn't include it).
3646 */
3647 wl12xx_remove_vendor_ie(beacon, WLAN_OUI_WFA,
3648 WLAN_OUI_TYPE_WFA_P2P, ieoffset);
3649
3650 hdr = (struct ieee80211_hdr *) beacon->data;
3651 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
3652 IEEE80211_STYPE_PROBE_RESP);
3653 if (is_ap)
3654 ret = wl1271_ap_set_probe_resp_tmpl_legacy(wl, vif,
3655 beacon->data,
3656 beacon->len,
3657 min_rate);
3658 else
3659 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
3660 CMD_TEMPL_PROBE_RESPONSE,
3661 beacon->data,
3662 beacon->len, 0,
3663 min_rate);
3664end_bcn:
3665 dev_kfree_skb(beacon);
3666 if (ret < 0)
3667 goto out;
3668
3669out:
3670 return ret;
3671}
3672
e78a287a
AN
3673static int wl1271_bss_beacon_info_changed(struct wl1271 *wl,
3674 struct ieee80211_vif *vif,
3675 struct ieee80211_bss_conf *bss_conf,
3676 u32 changed)
3677{
87fbcb0f 3678 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
536129c8 3679 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
e78a287a
AN
3680 int ret = 0;
3681
3682 if ((changed & BSS_CHANGED_BEACON_INT)) {
3683 wl1271_debug(DEBUG_MASTER, "beacon interval updated: %d",
60e84c2e
JO
3684 bss_conf->beacon_int);
3685
6a899796 3686 wlvif->beacon_int = bss_conf->beacon_int;
60e84c2e
JO
3687 }
3688
560f0024
AN
3689 if ((changed & BSS_CHANGED_AP_PROBE_RESP) && is_ap) {
3690 u32 rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
62c2e579
LC
3691
3692 wl1271_ap_set_probe_resp_tmpl(wl, rate, vif);
560f0024
AN
3693 }
3694
e78a287a 3695 if ((changed & BSS_CHANGED_BEACON)) {
62c2e579 3696 ret = wlcore_set_beacon_template(wl, vif, is_ap);
e78a287a
AN
3697 if (ret < 0)
3698 goto out;
3699 }
3700
3701out:
560f0024
AN
3702 if (ret != 0)
3703 wl1271_error("beacon info change failed: %d", ret);
e78a287a
AN
3704 return ret;
3705}
3706
3707/* AP mode changes */
3708static void wl1271_bss_info_changed_ap(struct wl1271 *wl,
3709 struct ieee80211_vif *vif,
3710 struct ieee80211_bss_conf *bss_conf,
3711 u32 changed)
3712{
87fbcb0f 3713 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
e78a287a 3714 int ret = 0;
e0d8bbf0 3715
b6970ee5 3716 if (changed & BSS_CHANGED_BASIC_RATES) {
e78a287a 3717 u32 rates = bss_conf->basic_rates;
5da11dcd 3718
87fbcb0f 3719 wlvif->basic_rate_set = wl1271_tx_enabled_rates_get(wl, rates,
1b92f15e 3720 wlvif->band);
d2d66c56 3721 wlvif->basic_rate = wl1271_tx_min_rate_get(wl,
87fbcb0f 3722 wlvif->basic_rate_set);
70f47424 3723
87fbcb0f 3724 ret = wl1271_init_ap_rates(wl, wlvif);
e78a287a 3725 if (ret < 0) {
70f47424 3726 wl1271_error("AP rate policy change failed %d", ret);
e78a287a
AN
3727 goto out;
3728 }
c45a85b5 3729
784f694d 3730 ret = wl1271_ap_init_templates(wl, vif);
c45a85b5
AN
3731 if (ret < 0)
3732 goto out;
62c2e579
LC
3733
3734 ret = wl1271_ap_set_probe_resp_tmpl(wl, wlvif->basic_rate, vif);
3735 if (ret < 0)
3736 goto out;
3737
3738 ret = wlcore_set_beacon_template(wl, vif, true);
3739 if (ret < 0)
3740 goto out;
e78a287a 3741 }
2f6724b2 3742
e78a287a
AN
3743 ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf, changed);
3744 if (ret < 0)
3745 goto out;
30240fc7 3746
e78a287a
AN
3747 if ((changed & BSS_CHANGED_BEACON_ENABLED)) {
3748 if (bss_conf->enable_beacon) {
53d40d0b 3749 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
87fbcb0f 3750 ret = wl12xx_cmd_role_start_ap(wl, wlvif);
e78a287a
AN
3751 if (ret < 0)
3752 goto out;
e0d8bbf0 3753
a8ab39a4 3754 ret = wl1271_ap_init_hwenc(wl, wlvif);
7f179b46
AN
3755 if (ret < 0)
3756 goto out;
cf42039f 3757
53d40d0b 3758 set_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags);
cf42039f 3759 wl1271_debug(DEBUG_AP, "started AP");
e0d8bbf0 3760 }
e78a287a 3761 } else {
53d40d0b 3762 if (test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
0603d891 3763 ret = wl12xx_cmd_role_stop_ap(wl, wlvif);
e78a287a
AN
3764 if (ret < 0)
3765 goto out;
e0d8bbf0 3766
53d40d0b 3767 clear_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags);
560f0024
AN
3768 clear_bit(WLVIF_FLAG_AP_PROBE_RESP_SET,
3769 &wlvif->flags);
e78a287a
AN
3770 wl1271_debug(DEBUG_AP, "stopped AP");
3771 }
3772 }
3773 }
e0d8bbf0 3774
0603d891 3775 ret = wl1271_bss_erp_info_changed(wl, vif, bss_conf, changed);
e78a287a
AN
3776 if (ret < 0)
3777 goto out;
0b932ab9
AN
3778
3779 /* Handle HT information change */
3780 if ((changed & BSS_CHANGED_HT) &&
3781 (bss_conf->channel_type != NL80211_CHAN_NO_HT)) {
0603d891 3782 ret = wl1271_acx_set_ht_information(wl, wlvif,
0b932ab9
AN
3783 bss_conf->ht_operation_mode);
3784 if (ret < 0) {
3785 wl1271_warning("Set ht information failed %d", ret);
3786 goto out;
3787 }
3788 }
3789
e78a287a
AN
3790out:
3791 return;
3792}
8bf29b0e 3793
3230f35e
EP
3794static int wlcore_set_bssid(struct wl1271 *wl, struct wl12xx_vif *wlvif,
3795 struct ieee80211_bss_conf *bss_conf,
3796 u32 sta_rate_set)
3797{
3798 u32 rates;
3799 int ret;
3800
3801 wl1271_debug(DEBUG_MAC80211,
3802 "changed_bssid: %pM, aid: %d, bcn_int: %d, brates: 0x%x sta_rate_set: 0x%x",
3803 bss_conf->bssid, bss_conf->aid,
3804 bss_conf->beacon_int,
3805 bss_conf->basic_rates, sta_rate_set);
3806
3807 wlvif->beacon_int = bss_conf->beacon_int;
3808 rates = bss_conf->basic_rates;
3809 wlvif->basic_rate_set =
3810 wl1271_tx_enabled_rates_get(wl, rates,
3811 wlvif->band);
3812 wlvif->basic_rate =
3813 wl1271_tx_min_rate_get(wl,
3814 wlvif->basic_rate_set);
3815
3816 if (sta_rate_set)
3817 wlvif->rate_set =
3818 wl1271_tx_enabled_rates_get(wl,
3819 sta_rate_set,
3820 wlvif->band);
3821
3822 /* we only support sched_scan while not connected */
3823 if (wl->sched_scanning) {
3824 wl1271_scan_sched_scan_stop(wl, wlvif);
3825 ieee80211_sched_scan_stopped(wl->hw);
3826 }
3827
3828 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
3829 if (ret < 0)
3830 return ret;
3831
3832 ret = wl12xx_cmd_build_null_data(wl, wlvif);
3833 if (ret < 0)
3834 return ret;
3835
3836 ret = wl1271_build_qos_null_data(wl, wl12xx_wlvif_to_vif(wlvif));
3837 if (ret < 0)
3838 return ret;
3839
3840 wlcore_set_ssid(wl, wlvif);
3841
3842 set_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
3843
3844 return 0;
3845}
3846
3847static int wlcore_clear_bssid(struct wl1271 *wl, struct wl12xx_vif *wlvif)
3848{
3849 int ret;
3850
3851 /* revert back to minimum rates for the current band */
3852 wl1271_set_band_rate(wl, wlvif);
3853 wlvif->basic_rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
3854
3855 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
3856 if (ret < 0)
3857 return ret;
3858
3859 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
3860 test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags)) {
3861 ret = wl12xx_cmd_role_stop_sta(wl, wlvif);
3862 if (ret < 0)
3863 return ret;
3864 }
3865
3866 clear_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
3867 return 0;
3868}
e78a287a
AN
3869/* STA/IBSS mode changes */
3870static void wl1271_bss_info_changed_sta(struct wl1271 *wl,
3871 struct ieee80211_vif *vif,
3872 struct ieee80211_bss_conf *bss_conf,
3873 u32 changed)
3874{
87fbcb0f 3875 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3230f35e 3876 bool do_join = false;
536129c8 3877 bool is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
227e81e1 3878 bool ibss_joined = false;
72c2d9e5 3879 u32 sta_rate_set = 0;
e78a287a 3880 int ret;
2d6e4e76 3881 struct ieee80211_sta *sta;
a100885d
AN
3882 bool sta_exists = false;
3883 struct ieee80211_sta_ht_cap sta_ht_cap;
e78a287a
AN
3884
3885 if (is_ibss) {
3886 ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf,
3887 changed);
3888 if (ret < 0)
3889 goto out;
e0d8bbf0
JO
3890 }
3891
227e81e1
EP
3892 if (changed & BSS_CHANGED_IBSS) {
3893 if (bss_conf->ibss_joined) {
eee514e3 3894 set_bit(WLVIF_FLAG_IBSS_JOINED, &wlvif->flags);
227e81e1
EP
3895 ibss_joined = true;
3896 } else {
3230f35e
EP
3897 wlcore_unset_assoc(wl, wlvif);
3898 wl12xx_cmd_role_stop_sta(wl, wlvif);
227e81e1
EP
3899 }
3900 }
3901
3902 if ((changed & BSS_CHANGED_BEACON_INT) && ibss_joined)
e78a287a
AN
3903 do_join = true;
3904
3905 /* Need to update the SSID (for filtering etc) */
227e81e1 3906 if ((changed & BSS_CHANGED_BEACON) && ibss_joined)
e78a287a
AN
3907 do_join = true;
3908
227e81e1 3909 if ((changed & BSS_CHANGED_BEACON_ENABLED) && ibss_joined) {
5da11dcd
JO
3910 wl1271_debug(DEBUG_ADHOC, "ad-hoc beaconing: %s",
3911 bss_conf->enable_beacon ? "enabled" : "disabled");
3912
5da11dcd
JO
3913 do_join = true;
3914 }
3915
e78a287a 3916 if ((changed & BSS_CHANGED_CQM)) {
00236aed
JO
3917 bool enable = false;
3918 if (bss_conf->cqm_rssi_thold)
3919 enable = true;
0603d891 3920 ret = wl1271_acx_rssi_snr_trigger(wl, wlvif, enable,
00236aed
JO
3921 bss_conf->cqm_rssi_thold,
3922 bss_conf->cqm_rssi_hyst);
3923 if (ret < 0)
3924 goto out;
04324d99 3925 wlvif->rssi_thold = bss_conf->cqm_rssi_thold;
00236aed
JO
3926 }
3927
ec87011a
EP
3928 if (changed & (BSS_CHANGED_BSSID | BSS_CHANGED_HT |
3929 BSS_CHANGED_ASSOC)) {
0f9c8250
AN
3930 rcu_read_lock();
3931 sta = ieee80211_find_sta(vif, bss_conf->bssid);
ef08d028
LC
3932 if (sta) {
3933 u8 *rx_mask = sta->ht_cap.mcs.rx_mask;
3934
3935 /* save the supp_rates of the ap */
3936 sta_rate_set = sta->supp_rates[wlvif->band];
3937 if (sta->ht_cap.ht_supported)
3938 sta_rate_set |=
3939 (rx_mask[0] << HW_HT_RATES_OFFSET) |
3940 (rx_mask[1] << HW_MIMO_RATES_OFFSET);
3941 sta_ht_cap = sta->ht_cap;
3942 sta_exists = true;
3943 }
3944
0f9c8250 3945 rcu_read_unlock();
72c2d9e5 3946 }
72c2d9e5 3947
3230f35e
EP
3948 if (changed & BSS_CHANGED_BSSID) {
3949 if (!is_zero_ether_addr(bss_conf->bssid)) {
3950 ret = wlcore_set_bssid(wl, wlvif, bss_conf,
3951 sta_rate_set);
f5fc0f86 3952 if (ret < 0)
e78a287a 3953 goto out;
f5fc0f86 3954
3230f35e
EP
3955 /* Need to update the BSSID (for filtering etc) */
3956 do_join = true;
d94cd297 3957 } else {
3230f35e 3958 ret = wlcore_clear_bssid(wl, wlvif);
6ccbb92e 3959 if (ret < 0)
e78a287a 3960 goto out;
f5fc0f86 3961 }
f5fc0f86
LC
3962 }
3963
d192d268
EP
3964 if (changed & BSS_CHANGED_IBSS) {
3965 wl1271_debug(DEBUG_ADHOC, "ibss_joined: %d",
3966 bss_conf->ibss_joined);
3967
3968 if (bss_conf->ibss_joined) {
3969 u32 rates = bss_conf->basic_rates;
87fbcb0f 3970 wlvif->basic_rate_set =
af7fbb28 3971 wl1271_tx_enabled_rates_get(wl, rates,
1b92f15e 3972 wlvif->band);
d2d66c56 3973 wlvif->basic_rate =
87fbcb0f
EP
3974 wl1271_tx_min_rate_get(wl,
3975 wlvif->basic_rate_set);
d192d268 3976
06b660e1 3977 /* by default, use 11b + OFDM rates */
30d0c8fd
EP
3978 wlvif->rate_set = CONF_TX_IBSS_DEFAULT_RATES;
3979 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
d192d268
EP
3980 if (ret < 0)
3981 goto out;
3982 }
3983 }
3984
0603d891 3985 ret = wl1271_bss_erp_info_changed(wl, vif, bss_conf, changed);
e78a287a
AN
3986 if (ret < 0)
3987 goto out;
f5fc0f86 3988
8bf29b0e 3989 if (do_join) {
3230f35e 3990 ret = wlcore_join(wl, wlvif);
8bf29b0e
JO
3991 if (ret < 0) {
3992 wl1271_warning("cmd join failed %d", ret);
e78a287a 3993 goto out;
8bf29b0e 3994 }
3230f35e
EP
3995 }
3996
3997 if (changed & BSS_CHANGED_ASSOC) {
3998 if (bss_conf->assoc) {
ec87011a
EP
3999 ret = wlcore_set_assoc(wl, wlvif, bss_conf,
4000 sta_rate_set);
251c177f
EP
4001 if (ret < 0)
4002 goto out;
3230f35e
EP
4003
4004 if (test_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags))
4005 wl12xx_set_authorized(wl, wlvif);
4006 } else {
4007 wlcore_unset_assoc(wl, wlvif);
251c177f 4008 }
c1899554
JO
4009 }
4010
0b932ab9 4011 /* Handle new association with HT. Do this after join. */
58321b29
EP
4012 if (sta_exists &&
4013 (changed & BSS_CHANGED_HT)) {
4014 bool enabled =
4015 bss_conf->channel_type != NL80211_CHAN_NO_HT;
4016
4017 ret = wl1271_acx_set_ht_capabilities(wl,
4018 &sta_ht_cap,
4019 enabled,
4020 wlvif->sta.hlid);
4021 if (ret < 0) {
4022 wl1271_warning("Set ht cap failed %d", ret);
4023 goto out;
4024
0f9c8250 4025 }
58321b29
EP
4026
4027 if (enabled) {
4028 ret = wl1271_acx_set_ht_information(wl, wlvif,
4029 bss_conf->ht_operation_mode);
0f9c8250 4030 if (ret < 0) {
58321b29 4031 wl1271_warning("Set ht information failed %d",
0f9c8250
AN
4032 ret);
4033 goto out;
4034 }
4035 }
4036 }
4037
76a74c8a
EP
4038 /* Handle arp filtering. Done after join. */
4039 if ((changed & BSS_CHANGED_ARP_FILTER) ||
4040 (!is_ibss && (changed & BSS_CHANGED_QOS))) {
4041 __be32 addr = bss_conf->arp_addr_list[0];
4042 wlvif->sta.qos = bss_conf->qos;
4043 WARN_ON(wlvif->bss_type != BSS_TYPE_STA_BSS);
4044
4045 if (bss_conf->arp_addr_cnt == 1 &&
4046 bss_conf->arp_filter_enabled) {
4047 wlvif->ip_addr = addr;
4048 /*
4049 * The template should have been configured only upon
4050 * association. however, it seems that the correct ip
4051 * isn't being set (when sending), so we have to
4052 * reconfigure the template upon every ip change.
4053 */
4054 ret = wl1271_cmd_build_arp_rsp(wl, wlvif);
4055 if (ret < 0) {
4056 wl1271_warning("build arp rsp failed: %d", ret);
4057 goto out;
4058 }
4059
4060 ret = wl1271_acx_arp_ip_filter(wl, wlvif,
4061 (ACX_ARP_FILTER_ARP_FILTERING |
4062 ACX_ARP_FILTER_AUTO_ARP),
4063 addr);
4064 } else {
4065 wlvif->ip_addr = 0;
4066 ret = wl1271_acx_arp_ip_filter(wl, wlvif, 0, addr);
4067 }
4068
4069 if (ret < 0)
4070 goto out;
4071 }
4072
e78a287a
AN
4073out:
4074 return;
4075}
4076
4077static void wl1271_op_bss_info_changed(struct ieee80211_hw *hw,
4078 struct ieee80211_vif *vif,
4079 struct ieee80211_bss_conf *bss_conf,
4080 u32 changed)
4081{
4082 struct wl1271 *wl = hw->priv;
536129c8
EP
4083 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4084 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
e78a287a
AN
4085 int ret;
4086
d3f5a1b5
EP
4087 wl1271_debug(DEBUG_MAC80211, "mac80211 bss info role %d changed 0x%x",
4088 wlvif->role_id, (int)changed);
e78a287a 4089
6b8bf5bc
AN
4090 /*
4091 * make sure to cancel pending disconnections if our association
4092 * state changed
4093 */
4094 if (!is_ap && (changed & BSS_CHANGED_ASSOC))
4095 cancel_delayed_work_sync(&wl->connection_loss_work);
4096
b515d83a
EP
4097 if (is_ap && (changed & BSS_CHANGED_BEACON_ENABLED) &&
4098 !bss_conf->enable_beacon)
4099 wl1271_tx_flush(wl);
4100
e78a287a
AN
4101 mutex_lock(&wl->mutex);
4102
4cc53383 4103 if (unlikely(wl->state != WLCORE_STATE_ON))
e78a287a
AN
4104 goto out;
4105
10c8cd01
EP
4106 if (unlikely(!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)))
4107 goto out;
4108
a620865e 4109 ret = wl1271_ps_elp_wakeup(wl);
e78a287a
AN
4110 if (ret < 0)
4111 goto out;
4112
4113 if (is_ap)
4114 wl1271_bss_info_changed_ap(wl, vif, bss_conf, changed);
4115 else
4116 wl1271_bss_info_changed_sta(wl, vif, bss_conf, changed);
4117
f5fc0f86
LC
4118 wl1271_ps_elp_sleep(wl);
4119
4120out:
4121 mutex_unlock(&wl->mutex);
4122}
4123
b6970ee5
EP
4124static int wlcore_op_add_chanctx(struct ieee80211_hw *hw,
4125 struct ieee80211_chanctx_conf *ctx)
4126{
4127 wl1271_debug(DEBUG_MAC80211, "mac80211 add chanctx %d (type %d)",
4128 ieee80211_frequency_to_channel(ctx->channel->center_freq),
4129 ctx->channel_type);
4130 return 0;
4131}
4132
4133static void wlcore_op_remove_chanctx(struct ieee80211_hw *hw,
4134 struct ieee80211_chanctx_conf *ctx)
4135{
4136 wl1271_debug(DEBUG_MAC80211, "mac80211 remove chanctx %d (type %d)",
4137 ieee80211_frequency_to_channel(ctx->channel->center_freq),
4138 ctx->channel_type);
4139}
4140
4141static void wlcore_op_change_chanctx(struct ieee80211_hw *hw,
4142 struct ieee80211_chanctx_conf *ctx,
4143 u32 changed)
4144{
4145 wl1271_debug(DEBUG_MAC80211,
4146 "mac80211 change chanctx %d (type %d) changed 0x%x",
4147 ieee80211_frequency_to_channel(ctx->channel->center_freq),
4148 ctx->channel_type, changed);
4149}
4150
4151static int wlcore_op_assign_vif_chanctx(struct ieee80211_hw *hw,
4152 struct ieee80211_vif *vif,
4153 struct ieee80211_chanctx_conf *ctx)
4154{
4155 struct wl1271 *wl = hw->priv;
4156 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4157 int channel = ieee80211_frequency_to_channel(
4158 ctx->channel->center_freq);
4159
4160 wl1271_debug(DEBUG_MAC80211,
4161 "mac80211 assign chanctx (role %d) %d (type %d)",
4162 wlvif->role_id, channel, ctx->channel_type);
4163
4164 mutex_lock(&wl->mutex);
4165
4166 wlvif->band = ctx->channel->band;
4167 wlvif->channel = channel;
4168 wlvif->channel_type = ctx->channel_type;
4169
4170 /* update default rates according to the band */
4171 wl1271_set_band_rate(wl, wlvif);
4172
4173 mutex_unlock(&wl->mutex);
4174
4175 return 0;
4176}
4177
4178static void wlcore_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
4179 struct ieee80211_vif *vif,
4180 struct ieee80211_chanctx_conf *ctx)
4181{
4182 struct wl1271 *wl = hw->priv;
4183 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4184
4185 wl1271_debug(DEBUG_MAC80211,
4186 "mac80211 unassign chanctx (role %d) %d (type %d)",
4187 wlvif->role_id,
4188 ieee80211_frequency_to_channel(ctx->channel->center_freq),
4189 ctx->channel_type);
4190
4191 wl1271_tx_flush(wl);
4192}
4193
8a3a3c85
EP
4194static int wl1271_op_conf_tx(struct ieee80211_hw *hw,
4195 struct ieee80211_vif *vif, u16 queue,
c6999d83
KV
4196 const struct ieee80211_tx_queue_params *params)
4197{
4198 struct wl1271 *wl = hw->priv;
0603d891 4199 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4695dc91 4200 u8 ps_scheme;
488fc540 4201 int ret = 0;
c6999d83
KV
4202
4203 mutex_lock(&wl->mutex);
4204
4205 wl1271_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
4206
4695dc91
KV
4207 if (params->uapsd)
4208 ps_scheme = CONF_PS_SCHEME_UPSD_TRIGGER;
4209 else
4210 ps_scheme = CONF_PS_SCHEME_LEGACY;
4211
5b37ddfe 4212 if (!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
c1b193eb 4213 goto out;
488fc540 4214
c1b193eb
EP
4215 ret = wl1271_ps_elp_wakeup(wl);
4216 if (ret < 0)
4217 goto out;
488fc540 4218
c1b193eb
EP
4219 /*
4220 * the txop is confed in units of 32us by the mac80211,
4221 * we need us
4222 */
0603d891 4223 ret = wl1271_acx_ac_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
c1b193eb
EP
4224 params->cw_min, params->cw_max,
4225 params->aifs, params->txop << 5);
4226 if (ret < 0)
4227 goto out_sleep;
4228
0603d891 4229 ret = wl1271_acx_tid_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
c1b193eb
EP
4230 CONF_CHANNEL_TYPE_EDCF,
4231 wl1271_tx_get_queue(queue),
4232 ps_scheme, CONF_ACK_POLICY_LEGACY,
4233 0, 0);
c82c1dde
KV
4234
4235out_sleep:
c1b193eb 4236 wl1271_ps_elp_sleep(wl);
c6999d83
KV
4237
4238out:
4239 mutex_unlock(&wl->mutex);
4240
4241 return ret;
4242}
4243
37a41b4a
EP
4244static u64 wl1271_op_get_tsf(struct ieee80211_hw *hw,
4245 struct ieee80211_vif *vif)
bbbb538e
JO
4246{
4247
4248 struct wl1271 *wl = hw->priv;
9c531149 4249 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
bbbb538e
JO
4250 u64 mactime = ULLONG_MAX;
4251 int ret;
4252
4253 wl1271_debug(DEBUG_MAC80211, "mac80211 get tsf");
4254
4255 mutex_lock(&wl->mutex);
4256
4cc53383 4257 if (unlikely(wl->state != WLCORE_STATE_ON))
f8d9802f
JO
4258 goto out;
4259
a620865e 4260 ret = wl1271_ps_elp_wakeup(wl);
bbbb538e
JO
4261 if (ret < 0)
4262 goto out;
4263
9c531149 4264 ret = wl12xx_acx_tsf_info(wl, wlvif, &mactime);
bbbb538e
JO
4265 if (ret < 0)
4266 goto out_sleep;
4267
4268out_sleep:
4269 wl1271_ps_elp_sleep(wl);
4270
4271out:
4272 mutex_unlock(&wl->mutex);
4273 return mactime;
4274}
f5fc0f86 4275
ece550d0
JL
4276static int wl1271_op_get_survey(struct ieee80211_hw *hw, int idx,
4277 struct survey_info *survey)
4278{
ece550d0 4279 struct ieee80211_conf *conf = &hw->conf;
b739a42c 4280
ece550d0
JL
4281 if (idx != 0)
4282 return -ENOENT;
b739a42c 4283
ece550d0 4284 survey->channel = conf->channel;
add779a0 4285 survey->filled = 0;
ece550d0
JL
4286 return 0;
4287}
4288
409622ec 4289static int wl1271_allocate_sta(struct wl1271 *wl,
c7ffb902
EP
4290 struct wl12xx_vif *wlvif,
4291 struct ieee80211_sta *sta)
f84f7d78
AN
4292{
4293 struct wl1271_station *wl_sta;
c7ffb902 4294 int ret;
f84f7d78 4295
c7ffb902
EP
4296
4297 if (wl->active_sta_count >= AP_MAX_STATIONS) {
f84f7d78
AN
4298 wl1271_warning("could not allocate HLID - too much stations");
4299 return -EBUSY;
4300 }
4301
4302 wl_sta = (struct wl1271_station *)sta->drv_priv;
c7ffb902
EP
4303 ret = wl12xx_allocate_link(wl, wlvif, &wl_sta->hlid);
4304 if (ret < 0) {
4305 wl1271_warning("could not allocate HLID - too many links");
4306 return -EBUSY;
4307 }
4308
4309 set_bit(wl_sta->hlid, wlvif->ap.sta_hlid_map);
b622d992 4310 memcpy(wl->links[wl_sta->hlid].addr, sta->addr, ETH_ALEN);
da03209e 4311 wl->active_sta_count++;
f84f7d78
AN
4312 return 0;
4313}
4314
c7ffb902 4315void wl1271_free_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 hlid)
f84f7d78 4316{
c7ffb902 4317 if (!test_bit(hlid, wlvif->ap.sta_hlid_map))
f1acea9a
AN
4318 return;
4319
c7ffb902 4320 clear_bit(hlid, wlvif->ap.sta_hlid_map);
b622d992 4321 memset(wl->links[hlid].addr, 0, ETH_ALEN);
0f9c8250 4322 wl->links[hlid].ba_bitmap = 0;
b622d992
AN
4323 __clear_bit(hlid, &wl->ap_ps_map);
4324 __clear_bit(hlid, (unsigned long *)&wl->ap_fw_ps_map);
c7ffb902 4325 wl12xx_free_link(wl, wlvif, &hlid);
da03209e 4326 wl->active_sta_count--;
55df5afb
AN
4327
4328 /*
4329 * rearm the tx watchdog when the last STA is freed - give the FW a
4330 * chance to return STA-buffered packets before complaining.
4331 */
4332 if (wl->active_sta_count == 0)
4333 wl12xx_rearm_tx_watchdog_locked(wl);
f84f7d78
AN
4334}
4335
2d6cf2b5
EP
4336static int wl12xx_sta_add(struct wl1271 *wl,
4337 struct wl12xx_vif *wlvif,
4338 struct ieee80211_sta *sta)
f84f7d78 4339{
c7ffb902 4340 struct wl1271_station *wl_sta;
f84f7d78
AN
4341 int ret = 0;
4342 u8 hlid;
4343
f84f7d78
AN
4344 wl1271_debug(DEBUG_MAC80211, "mac80211 add sta %d", (int)sta->aid);
4345
c7ffb902 4346 ret = wl1271_allocate_sta(wl, wlvif, sta);
f84f7d78 4347 if (ret < 0)
2d6cf2b5 4348 return ret;
f84f7d78 4349
c7ffb902
EP
4350 wl_sta = (struct wl1271_station *)sta->drv_priv;
4351 hlid = wl_sta->hlid;
4352
1b92f15e 4353 ret = wl12xx_cmd_add_peer(wl, wlvif, sta, hlid);
f84f7d78 4354 if (ret < 0)
2d6cf2b5 4355 wl1271_free_sta(wl, wlvif, hlid);
f84f7d78 4356
2d6cf2b5
EP
4357 return ret;
4358}
b67476ef 4359
2d6cf2b5
EP
4360static int wl12xx_sta_remove(struct wl1271 *wl,
4361 struct wl12xx_vif *wlvif,
4362 struct ieee80211_sta *sta)
4363{
4364 struct wl1271_station *wl_sta;
4365 int ret = 0, id;
0b932ab9 4366
2d6cf2b5
EP
4367 wl1271_debug(DEBUG_MAC80211, "mac80211 remove sta %d", (int)sta->aid);
4368
4369 wl_sta = (struct wl1271_station *)sta->drv_priv;
4370 id = wl_sta->hlid;
4371 if (WARN_ON(!test_bit(id, wlvif->ap.sta_hlid_map)))
4372 return -EINVAL;
f84f7d78 4373
2d6cf2b5 4374 ret = wl12xx_cmd_remove_peer(wl, wl_sta->hlid);
409622ec 4375 if (ret < 0)
2d6cf2b5 4376 return ret;
409622ec 4377
2d6cf2b5 4378 wl1271_free_sta(wl, wlvif, wl_sta->hlid);
f84f7d78
AN
4379 return ret;
4380}
4381
2d6cf2b5
EP
4382static int wl12xx_update_sta_state(struct wl1271 *wl,
4383 struct wl12xx_vif *wlvif,
4384 struct ieee80211_sta *sta,
4385 enum ieee80211_sta_state old_state,
4386 enum ieee80211_sta_state new_state)
f84f7d78 4387{
f84f7d78 4388 struct wl1271_station *wl_sta;
2d6cf2b5
EP
4389 u8 hlid;
4390 bool is_ap = wlvif->bss_type == BSS_TYPE_AP_BSS;
4391 bool is_sta = wlvif->bss_type == BSS_TYPE_STA_BSS;
4392 int ret;
f84f7d78 4393
2d6cf2b5
EP
4394 wl_sta = (struct wl1271_station *)sta->drv_priv;
4395 hlid = wl_sta->hlid;
f84f7d78 4396
2d6cf2b5
EP
4397 /* Add station (AP mode) */
4398 if (is_ap &&
4399 old_state == IEEE80211_STA_NOTEXIST &&
29936266
EP
4400 new_state == IEEE80211_STA_NONE) {
4401 ret = wl12xx_sta_add(wl, wlvif, sta);
4402 if (ret)
4403 return ret;
4404 }
2d6cf2b5
EP
4405
4406 /* Remove station (AP mode) */
4407 if (is_ap &&
4408 old_state == IEEE80211_STA_NONE &&
4409 new_state == IEEE80211_STA_NOTEXIST) {
4410 /* must not fail */
4411 wl12xx_sta_remove(wl, wlvif, sta);
2d6cf2b5 4412 }
f84f7d78 4413
2d6cf2b5
EP
4414 /* Authorize station (AP mode) */
4415 if (is_ap &&
4416 new_state == IEEE80211_STA_AUTHORIZED) {
4417 ret = wl12xx_cmd_set_peer_state(wl, hlid);
4418 if (ret < 0)
4419 return ret;
f84f7d78 4420
2d6cf2b5
EP
4421 ret = wl1271_acx_set_ht_capabilities(wl, &sta->ht_cap, true,
4422 hlid);
29936266
EP
4423 if (ret)
4424 return ret;
2d6cf2b5 4425 }
f84f7d78 4426
9fd6f21b
EP
4427 /* Authorize station */
4428 if (is_sta &&
4429 new_state == IEEE80211_STA_AUTHORIZED) {
4430 set_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags);
29936266
EP
4431 ret = wl12xx_set_authorized(wl, wlvif);
4432 if (ret)
4433 return ret;
9fd6f21b
EP
4434 }
4435
4436 if (is_sta &&
4437 old_state == IEEE80211_STA_AUTHORIZED &&
4438 new_state == IEEE80211_STA_ASSOC) {
4439 clear_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags);
3230f35e 4440 clear_bit(WLVIF_FLAG_STA_STATE_SENT, &wlvif->flags);
9fd6f21b
EP
4441 }
4442
29936266
EP
4443 /* clear ROCs on failure or authorization */
4444 if (is_sta &&
4445 (new_state == IEEE80211_STA_AUTHORIZED ||
4446 new_state == IEEE80211_STA_NOTEXIST)) {
4447 if (test_bit(wlvif->role_id, wl->roc_map))
4448 wl12xx_croc(wl, wlvif->role_id);
4449 }
4450
4451 if (is_sta &&
4452 old_state == IEEE80211_STA_NOTEXIST &&
4453 new_state == IEEE80211_STA_NONE) {
4454 if (find_first_bit(wl->roc_map,
4455 WL12XX_MAX_ROLES) >= WL12XX_MAX_ROLES) {
4456 WARN_ON(wlvif->role_id == WL12XX_INVALID_ROLE_ID);
4457 wl12xx_roc(wl, wlvif, wlvif->role_id,
4458 wlvif->band, wlvif->channel);
4459 }
4460 }
2d6cf2b5
EP
4461 return 0;
4462}
4463
4464static int wl12xx_op_sta_state(struct ieee80211_hw *hw,
4465 struct ieee80211_vif *vif,
4466 struct ieee80211_sta *sta,
4467 enum ieee80211_sta_state old_state,
4468 enum ieee80211_sta_state new_state)
4469{
4470 struct wl1271 *wl = hw->priv;
4471 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4472 int ret;
4473
4474 wl1271_debug(DEBUG_MAC80211, "mac80211 sta %d state=%d->%d",
4475 sta->aid, old_state, new_state);
4476
4477 mutex_lock(&wl->mutex);
4478
4cc53383 4479 if (unlikely(wl->state != WLCORE_STATE_ON)) {
2d6cf2b5 4480 ret = -EBUSY;
f84f7d78 4481 goto out;
2d6cf2b5 4482 }
f84f7d78 4483
a620865e 4484 ret = wl1271_ps_elp_wakeup(wl);
f84f7d78
AN
4485 if (ret < 0)
4486 goto out;
4487
2d6cf2b5 4488 ret = wl12xx_update_sta_state(wl, wlvif, sta, old_state, new_state);
f84f7d78 4489
f84f7d78 4490 wl1271_ps_elp_sleep(wl);
f84f7d78
AN
4491out:
4492 mutex_unlock(&wl->mutex);
2d6cf2b5
EP
4493 if (new_state < old_state)
4494 return 0;
f84f7d78
AN
4495 return ret;
4496}
4497
4623ec7d
LC
4498static int wl1271_op_ampdu_action(struct ieee80211_hw *hw,
4499 struct ieee80211_vif *vif,
4500 enum ieee80211_ampdu_mlme_action action,
4501 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
4502 u8 buf_size)
bbba3e68
LS
4503{
4504 struct wl1271 *wl = hw->priv;
536129c8 4505 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
bbba3e68 4506 int ret;
0f9c8250
AN
4507 u8 hlid, *ba_bitmap;
4508
4509 wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu action %d tid %d", action,
4510 tid);
4511
4512 /* sanity check - the fields in FW are only 8bits wide */
4513 if (WARN_ON(tid > 0xFF))
4514 return -ENOTSUPP;
bbba3e68
LS
4515
4516 mutex_lock(&wl->mutex);
4517
4cc53383 4518 if (unlikely(wl->state != WLCORE_STATE_ON)) {
bbba3e68
LS
4519 ret = -EAGAIN;
4520 goto out;
4521 }
4522
536129c8 4523 if (wlvif->bss_type == BSS_TYPE_STA_BSS) {
154da67c 4524 hlid = wlvif->sta.hlid;
d0802abd 4525 ba_bitmap = &wlvif->sta.ba_rx_bitmap;
536129c8 4526 } else if (wlvif->bss_type == BSS_TYPE_AP_BSS) {
0f9c8250
AN
4527 struct wl1271_station *wl_sta;
4528
4529 wl_sta = (struct wl1271_station *)sta->drv_priv;
4530 hlid = wl_sta->hlid;
4531 ba_bitmap = &wl->links[hlid].ba_bitmap;
4532 } else {
4533 ret = -EINVAL;
4534 goto out;
4535 }
4536
a620865e 4537 ret = wl1271_ps_elp_wakeup(wl);
bbba3e68
LS
4538 if (ret < 0)
4539 goto out;
4540
70559a06
SL
4541 wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu: Rx tid %d action %d",
4542 tid, action);
4543
bbba3e68
LS
4544 switch (action) {
4545 case IEEE80211_AMPDU_RX_START:
d0802abd 4546 if (!wlvif->ba_support || !wlvif->ba_allowed) {
bbba3e68 4547 ret = -ENOTSUPP;
0f9c8250
AN
4548 break;
4549 }
4550
4551 if (wl->ba_rx_session_count >= RX_BA_MAX_SESSIONS) {
4552 ret = -EBUSY;
4553 wl1271_error("exceeded max RX BA sessions");
4554 break;
4555 }
4556
4557 if (*ba_bitmap & BIT(tid)) {
4558 ret = -EINVAL;
4559 wl1271_error("cannot enable RX BA session on active "
4560 "tid: %d", tid);
4561 break;
4562 }
4563
4564 ret = wl12xx_acx_set_ba_receiver_session(wl, tid, *ssn, true,
4565 hlid);
4566 if (!ret) {
4567 *ba_bitmap |= BIT(tid);
4568 wl->ba_rx_session_count++;
bbba3e68
LS
4569 }
4570 break;
4571
4572 case IEEE80211_AMPDU_RX_STOP:
0f9c8250 4573 if (!(*ba_bitmap & BIT(tid))) {
c954910b
AN
4574 /*
4575 * this happens on reconfig - so only output a debug
4576 * message for now, and don't fail the function.
4577 */
4578 wl1271_debug(DEBUG_MAC80211,
4579 "no active RX BA session on tid: %d",
0f9c8250 4580 tid);
c954910b 4581 ret = 0;
0f9c8250
AN
4582 break;
4583 }
4584
4585 ret = wl12xx_acx_set_ba_receiver_session(wl, tid, 0, false,
4586 hlid);
4587 if (!ret) {
4588 *ba_bitmap &= ~BIT(tid);
4589 wl->ba_rx_session_count--;
4590 }
bbba3e68
LS
4591 break;
4592
4593 /*
4594 * The BA initiator session management in FW independently.
4595 * Falling break here on purpose for all TX APDU commands.
4596 */
4597 case IEEE80211_AMPDU_TX_START:
4598 case IEEE80211_AMPDU_TX_STOP:
4599 case IEEE80211_AMPDU_TX_OPERATIONAL:
4600 ret = -EINVAL;
4601 break;
4602
4603 default:
4604 wl1271_error("Incorrect ampdu action id=%x\n", action);
4605 ret = -EINVAL;
4606 }
4607
4608 wl1271_ps_elp_sleep(wl);
4609
4610out:
4611 mutex_unlock(&wl->mutex);
4612
4613 return ret;
4614}
4615
af7fbb28
EP
4616static int wl12xx_set_bitrate_mask(struct ieee80211_hw *hw,
4617 struct ieee80211_vif *vif,
4618 const struct cfg80211_bitrate_mask *mask)
4619{
83587505 4620 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
af7fbb28 4621 struct wl1271 *wl = hw->priv;
d6fa37c9 4622 int i, ret = 0;
af7fbb28
EP
4623
4624 wl1271_debug(DEBUG_MAC80211, "mac80211 set_bitrate_mask 0x%x 0x%x",
4625 mask->control[NL80211_BAND_2GHZ].legacy,
4626 mask->control[NL80211_BAND_5GHZ].legacy);
4627
4628 mutex_lock(&wl->mutex);
4629
091185d6 4630 for (i = 0; i < WLCORE_NUM_BANDS; i++)
83587505 4631 wlvif->bitrate_masks[i] =
af7fbb28
EP
4632 wl1271_tx_enabled_rates_get(wl,
4633 mask->control[i].legacy,
4634 i);
d6fa37c9 4635
4cc53383 4636 if (unlikely(wl->state != WLCORE_STATE_ON))
d6fa37c9
EP
4637 goto out;
4638
4639 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
4640 !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
4641
4642 ret = wl1271_ps_elp_wakeup(wl);
4643 if (ret < 0)
4644 goto out;
4645
4646 wl1271_set_band_rate(wl, wlvif);
4647 wlvif->basic_rate =
4648 wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
4649 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
4650
4651 wl1271_ps_elp_sleep(wl);
4652 }
4653out:
af7fbb28
EP
4654 mutex_unlock(&wl->mutex);
4655
d6fa37c9 4656 return ret;
af7fbb28
EP
4657}
4658
6d158ff3
SL
4659static void wl12xx_op_channel_switch(struct ieee80211_hw *hw,
4660 struct ieee80211_channel_switch *ch_switch)
4661{
4662 struct wl1271 *wl = hw->priv;
52630c5d 4663 struct wl12xx_vif *wlvif;
6d158ff3
SL
4664 int ret;
4665
4666 wl1271_debug(DEBUG_MAC80211, "mac80211 channel switch");
4667
b9239b66
AN
4668 wl1271_tx_flush(wl);
4669
6d158ff3
SL
4670 mutex_lock(&wl->mutex);
4671
4cc53383 4672 if (unlikely(wl->state == WLCORE_STATE_OFF)) {
6e8cd331
EP
4673 wl12xx_for_each_wlvif_sta(wl, wlvif) {
4674 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
4675 ieee80211_chswitch_done(vif, false);
4676 }
4677 goto out;
4cc53383
IY
4678 } else if (unlikely(wl->state != WLCORE_STATE_ON)) {
4679 goto out;
6d158ff3
SL
4680 }
4681
4682 ret = wl1271_ps_elp_wakeup(wl);
4683 if (ret < 0)
4684 goto out;
4685
52630c5d
EP
4686 /* TODO: change mac80211 to pass vif as param */
4687 wl12xx_for_each_wlvif_sta(wl, wlvif) {
8332f0f6 4688 ret = wl12xx_cmd_channel_switch(wl, wlvif, ch_switch);
6d158ff3 4689
52630c5d
EP
4690 if (!ret)
4691 set_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags);
4692 }
6d158ff3
SL
4693
4694 wl1271_ps_elp_sleep(wl);
4695
4696out:
4697 mutex_unlock(&wl->mutex);
4698}
4699
d8ae5a25
EP
4700static void wlcore_op_flush(struct ieee80211_hw *hw, bool drop)
4701{
4702 struct wl1271 *wl = hw->priv;
4703
4704 wl1271_tx_flush(wl);
4705}
4706
dabf37db
EP
4707static int wlcore_op_remain_on_channel(struct ieee80211_hw *hw,
4708 struct ieee80211_vif *vif,
4709 struct ieee80211_channel *chan,
4710 enum nl80211_channel_type channel_type,
4711 int duration)
4712{
4713 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4714 struct wl1271 *wl = hw->priv;
4715 int channel, ret = 0;
4716
4717 channel = ieee80211_frequency_to_channel(chan->center_freq);
4718
4719 wl1271_debug(DEBUG_MAC80211, "mac80211 roc %d (%d)",
4720 channel, wlvif->role_id);
4721
4722 mutex_lock(&wl->mutex);
4723
4724 if (unlikely(wl->state != WLCORE_STATE_ON))
4725 goto out;
4726
4727 /* return EBUSY if we can't ROC right now */
4728 if (WARN_ON(wl->roc_vif ||
4729 find_first_bit(wl->roc_map,
4730 WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES)) {
4731 ret = -EBUSY;
4732 goto out;
4733 }
4734
4735 ret = wl1271_ps_elp_wakeup(wl);
4736 if (ret < 0)
4737 goto out;
4738
4739 ret = wl12xx_start_dev(wl, wlvif, chan->band, channel);
4740 if (ret < 0)
4741 goto out_sleep;
4742
4743 wl->roc_vif = vif;
4744 ieee80211_queue_delayed_work(hw, &wl->roc_complete_work,
4745 msecs_to_jiffies(duration));
4746out_sleep:
4747 wl1271_ps_elp_sleep(wl);
4748out:
4749 mutex_unlock(&wl->mutex);
4750 return ret;
4751}
4752
4753static int __wlcore_roc_completed(struct wl1271 *wl)
4754{
4755 struct wl12xx_vif *wlvif;
4756 int ret;
4757
4758 /* already completed */
4759 if (unlikely(!wl->roc_vif))
4760 return 0;
4761
4762 wlvif = wl12xx_vif_to_data(wl->roc_vif);
4763
4764 if (!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
4765 return -EBUSY;
4766
4767 ret = wl12xx_stop_dev(wl, wlvif);
4768 if (ret < 0)
4769 return ret;
4770
4771 wl->roc_vif = NULL;
4772
4773 return 0;
4774}
4775
4776static int wlcore_roc_completed(struct wl1271 *wl)
4777{
4778 int ret;
4779
4780 wl1271_debug(DEBUG_MAC80211, "roc complete");
4781
4782 mutex_lock(&wl->mutex);
4783
4784 if (unlikely(wl->state != WLCORE_STATE_ON)) {
4785 ret = -EBUSY;
4786 goto out;
4787 }
4788
4789 ret = wl1271_ps_elp_wakeup(wl);
4790 if (ret < 0)
4791 goto out;
4792
4793 ret = __wlcore_roc_completed(wl);
4794
4795 wl1271_ps_elp_sleep(wl);
4796out:
4797 mutex_unlock(&wl->mutex);
4798
4799 return ret;
4800}
4801
4802static void wlcore_roc_complete_work(struct work_struct *work)
4803{
4804 struct delayed_work *dwork;
4805 struct wl1271 *wl;
4806 int ret;
4807
4808 dwork = container_of(work, struct delayed_work, work);
4809 wl = container_of(dwork, struct wl1271, roc_complete_work);
4810
4811 ret = wlcore_roc_completed(wl);
4812 if (!ret)
4813 ieee80211_remain_on_channel_expired(wl->hw);
4814}
4815
4816static int wlcore_op_cancel_remain_on_channel(struct ieee80211_hw *hw)
4817{
4818 struct wl1271 *wl = hw->priv;
4819
4820 wl1271_debug(DEBUG_MAC80211, "mac80211 croc");
4821
4822 /* TODO: per-vif */
4823 wl1271_tx_flush(wl);
4824
4825 /*
4826 * we can't just flush_work here, because it might deadlock
4827 * (as we might get called from the same workqueue)
4828 */
4829 cancel_delayed_work_sync(&wl->roc_complete_work);
4830 wlcore_roc_completed(wl);
4831
4832 return 0;
4833}
4834
33437893
AN
4835static bool wl1271_tx_frames_pending(struct ieee80211_hw *hw)
4836{
4837 struct wl1271 *wl = hw->priv;
4838 bool ret = false;
4839
4840 mutex_lock(&wl->mutex);
4841
4cc53383 4842 if (unlikely(wl->state != WLCORE_STATE_ON))
33437893
AN
4843 goto out;
4844
4845 /* packets are considered pending if in the TX queue or the FW */
f1a46384 4846 ret = (wl1271_tx_total_queue_count(wl) > 0) || (wl->tx_frames_cnt > 0);
33437893
AN
4847out:
4848 mutex_unlock(&wl->mutex);
4849
4850 return ret;
4851}
4852
f5fc0f86
LC
4853/* can't be const, mac80211 writes to this */
4854static struct ieee80211_rate wl1271_rates[] = {
4855 { .bitrate = 10,
2b60100b
JO
4856 .hw_value = CONF_HW_BIT_RATE_1MBPS,
4857 .hw_value_short = CONF_HW_BIT_RATE_1MBPS, },
f5fc0f86 4858 { .bitrate = 20,
2b60100b
JO
4859 .hw_value = CONF_HW_BIT_RATE_2MBPS,
4860 .hw_value_short = CONF_HW_BIT_RATE_2MBPS,
f5fc0f86
LC
4861 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
4862 { .bitrate = 55,
2b60100b
JO
4863 .hw_value = CONF_HW_BIT_RATE_5_5MBPS,
4864 .hw_value_short = CONF_HW_BIT_RATE_5_5MBPS,
f5fc0f86
LC
4865 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
4866 { .bitrate = 110,
2b60100b
JO
4867 .hw_value = CONF_HW_BIT_RATE_11MBPS,
4868 .hw_value_short = CONF_HW_BIT_RATE_11MBPS,
f5fc0f86
LC
4869 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
4870 { .bitrate = 60,
2b60100b
JO
4871 .hw_value = CONF_HW_BIT_RATE_6MBPS,
4872 .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
f5fc0f86 4873 { .bitrate = 90,
2b60100b
JO
4874 .hw_value = CONF_HW_BIT_RATE_9MBPS,
4875 .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
f5fc0f86 4876 { .bitrate = 120,
2b60100b
JO
4877 .hw_value = CONF_HW_BIT_RATE_12MBPS,
4878 .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
f5fc0f86 4879 { .bitrate = 180,
2b60100b
JO
4880 .hw_value = CONF_HW_BIT_RATE_18MBPS,
4881 .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
f5fc0f86 4882 { .bitrate = 240,
2b60100b
JO
4883 .hw_value = CONF_HW_BIT_RATE_24MBPS,
4884 .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
f5fc0f86 4885 { .bitrate = 360,
2b60100b
JO
4886 .hw_value = CONF_HW_BIT_RATE_36MBPS,
4887 .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
f5fc0f86 4888 { .bitrate = 480,
2b60100b
JO
4889 .hw_value = CONF_HW_BIT_RATE_48MBPS,
4890 .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
f5fc0f86 4891 { .bitrate = 540,
2b60100b
JO
4892 .hw_value = CONF_HW_BIT_RATE_54MBPS,
4893 .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
f5fc0f86
LC
4894};
4895
fa97f46b 4896/* can't be const, mac80211 writes to this */
f5fc0f86 4897static struct ieee80211_channel wl1271_channels[] = {
a2d0e3f1 4898 { .hw_value = 1, .center_freq = 2412, .max_power = 25 },
fa21c7a9 4899 { .hw_value = 2, .center_freq = 2417, .max_power = 25 },
fa97f46b
JO
4900 { .hw_value = 3, .center_freq = 2422, .max_power = 25 },
4901 { .hw_value = 4, .center_freq = 2427, .max_power = 25 },
4902 { .hw_value = 5, .center_freq = 2432, .max_power = 25 },
fa21c7a9 4903 { .hw_value = 6, .center_freq = 2437, .max_power = 25 },
fa97f46b
JO
4904 { .hw_value = 7, .center_freq = 2442, .max_power = 25 },
4905 { .hw_value = 8, .center_freq = 2447, .max_power = 25 },
4906 { .hw_value = 9, .center_freq = 2452, .max_power = 25 },
fa21c7a9 4907 { .hw_value = 10, .center_freq = 2457, .max_power = 25 },
fa97f46b
JO
4908 { .hw_value = 11, .center_freq = 2462, .max_power = 25 },
4909 { .hw_value = 12, .center_freq = 2467, .max_power = 25 },
4910 { .hw_value = 13, .center_freq = 2472, .max_power = 25 },
6c89b7b2 4911 { .hw_value = 14, .center_freq = 2484, .max_power = 25 },
f5fc0f86
LC
4912};
4913
4914/* can't be const, mac80211 writes to this */
4915static struct ieee80211_supported_band wl1271_band_2ghz = {
4916 .channels = wl1271_channels,
4917 .n_channels = ARRAY_SIZE(wl1271_channels),
4918 .bitrates = wl1271_rates,
4919 .n_bitrates = ARRAY_SIZE(wl1271_rates),
4920};
4921
1ebec3d7
TP
4922/* 5 GHz data rates for WL1273 */
4923static struct ieee80211_rate wl1271_rates_5ghz[] = {
4924 { .bitrate = 60,
4925 .hw_value = CONF_HW_BIT_RATE_6MBPS,
4926 .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
4927 { .bitrate = 90,
4928 .hw_value = CONF_HW_BIT_RATE_9MBPS,
4929 .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
4930 { .bitrate = 120,
4931 .hw_value = CONF_HW_BIT_RATE_12MBPS,
4932 .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
4933 { .bitrate = 180,
4934 .hw_value = CONF_HW_BIT_RATE_18MBPS,
4935 .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
4936 { .bitrate = 240,
4937 .hw_value = CONF_HW_BIT_RATE_24MBPS,
4938 .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
4939 { .bitrate = 360,
4940 .hw_value = CONF_HW_BIT_RATE_36MBPS,
4941 .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
4942 { .bitrate = 480,
4943 .hw_value = CONF_HW_BIT_RATE_48MBPS,
4944 .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
4945 { .bitrate = 540,
4946 .hw_value = CONF_HW_BIT_RATE_54MBPS,
4947 .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
4948};
4949
fa97f46b 4950/* 5 GHz band channels for WL1273 */
1ebec3d7 4951static struct ieee80211_channel wl1271_channels_5ghz[] = {
6cfa5cff
AN
4952 { .hw_value = 7, .center_freq = 5035, .max_power = 25 },
4953 { .hw_value = 8, .center_freq = 5040, .max_power = 25 },
4954 { .hw_value = 9, .center_freq = 5045, .max_power = 25 },
4955 { .hw_value = 11, .center_freq = 5055, .max_power = 25 },
4956 { .hw_value = 12, .center_freq = 5060, .max_power = 25 },
4957 { .hw_value = 16, .center_freq = 5080, .max_power = 25 },
4958 { .hw_value = 34, .center_freq = 5170, .max_power = 25 },
4959 { .hw_value = 36, .center_freq = 5180, .max_power = 25 },
4960 { .hw_value = 38, .center_freq = 5190, .max_power = 25 },
4961 { .hw_value = 40, .center_freq = 5200, .max_power = 25 },
4962 { .hw_value = 42, .center_freq = 5210, .max_power = 25 },
4963 { .hw_value = 44, .center_freq = 5220, .max_power = 25 },
4964 { .hw_value = 46, .center_freq = 5230, .max_power = 25 },
4965 { .hw_value = 48, .center_freq = 5240, .max_power = 25 },
4966 { .hw_value = 52, .center_freq = 5260, .max_power = 25 },
4967 { .hw_value = 56, .center_freq = 5280, .max_power = 25 },
4968 { .hw_value = 60, .center_freq = 5300, .max_power = 25 },
4969 { .hw_value = 64, .center_freq = 5320, .max_power = 25 },
4970 { .hw_value = 100, .center_freq = 5500, .max_power = 25 },
4971 { .hw_value = 104, .center_freq = 5520, .max_power = 25 },
4972 { .hw_value = 108, .center_freq = 5540, .max_power = 25 },
4973 { .hw_value = 112, .center_freq = 5560, .max_power = 25 },
4974 { .hw_value = 116, .center_freq = 5580, .max_power = 25 },
4975 { .hw_value = 120, .center_freq = 5600, .max_power = 25 },
4976 { .hw_value = 124, .center_freq = 5620, .max_power = 25 },
4977 { .hw_value = 128, .center_freq = 5640, .max_power = 25 },
4978 { .hw_value = 132, .center_freq = 5660, .max_power = 25 },
4979 { .hw_value = 136, .center_freq = 5680, .max_power = 25 },
4980 { .hw_value = 140, .center_freq = 5700, .max_power = 25 },
4981 { .hw_value = 149, .center_freq = 5745, .max_power = 25 },
4982 { .hw_value = 153, .center_freq = 5765, .max_power = 25 },
4983 { .hw_value = 157, .center_freq = 5785, .max_power = 25 },
4984 { .hw_value = 161, .center_freq = 5805, .max_power = 25 },
4985 { .hw_value = 165, .center_freq = 5825, .max_power = 25 },
1ebec3d7
TP
4986};
4987
1ebec3d7
TP
4988static struct ieee80211_supported_band wl1271_band_5ghz = {
4989 .channels = wl1271_channels_5ghz,
4990 .n_channels = ARRAY_SIZE(wl1271_channels_5ghz),
4991 .bitrates = wl1271_rates_5ghz,
4992 .n_bitrates = ARRAY_SIZE(wl1271_rates_5ghz),
f876bb9a
JO
4993};
4994
f5fc0f86
LC
4995static const struct ieee80211_ops wl1271_ops = {
4996 .start = wl1271_op_start,
c24ec83b 4997 .stop = wlcore_op_stop,
f5fc0f86
LC
4998 .add_interface = wl1271_op_add_interface,
4999 .remove_interface = wl1271_op_remove_interface,
c0fad1b7 5000 .change_interface = wl12xx_op_change_interface,
f634a4e7 5001#ifdef CONFIG_PM
402e4861
EP
5002 .suspend = wl1271_op_suspend,
5003 .resume = wl1271_op_resume,
f634a4e7 5004#endif
f5fc0f86 5005 .config = wl1271_op_config,
c87dec9f 5006 .prepare_multicast = wl1271_op_prepare_multicast,
f5fc0f86
LC
5007 .configure_filter = wl1271_op_configure_filter,
5008 .tx = wl1271_op_tx,
a1c597f2 5009 .set_key = wlcore_op_set_key,
f5fc0f86 5010 .hw_scan = wl1271_op_hw_scan,
73ecce31 5011 .cancel_hw_scan = wl1271_op_cancel_hw_scan,
33c2c06c
LC
5012 .sched_scan_start = wl1271_op_sched_scan_start,
5013 .sched_scan_stop = wl1271_op_sched_scan_stop,
f5fc0f86 5014 .bss_info_changed = wl1271_op_bss_info_changed,
68d069c4 5015 .set_frag_threshold = wl1271_op_set_frag_threshold,
f5fc0f86 5016 .set_rts_threshold = wl1271_op_set_rts_threshold,
c6999d83 5017 .conf_tx = wl1271_op_conf_tx,
bbbb538e 5018 .get_tsf = wl1271_op_get_tsf,
ece550d0 5019 .get_survey = wl1271_op_get_survey,
2d6cf2b5 5020 .sta_state = wl12xx_op_sta_state,
bbba3e68 5021 .ampdu_action = wl1271_op_ampdu_action,
33437893 5022 .tx_frames_pending = wl1271_tx_frames_pending,
af7fbb28 5023 .set_bitrate_mask = wl12xx_set_bitrate_mask,
6d158ff3 5024 .channel_switch = wl12xx_op_channel_switch,
d8ae5a25 5025 .flush = wlcore_op_flush,
dabf37db
EP
5026 .remain_on_channel = wlcore_op_remain_on_channel,
5027 .cancel_remain_on_channel = wlcore_op_cancel_remain_on_channel,
b6970ee5
EP
5028 .add_chanctx = wlcore_op_add_chanctx,
5029 .remove_chanctx = wlcore_op_remove_chanctx,
5030 .change_chanctx = wlcore_op_change_chanctx,
5031 .assign_vif_chanctx = wlcore_op_assign_vif_chanctx,
5032 .unassign_vif_chanctx = wlcore_op_unassign_vif_chanctx,
c8c90873 5033 CFG80211_TESTMODE_CMD(wl1271_tm_cmd)
f5fc0f86
LC
5034};
5035
f876bb9a 5036
43a8bc5a 5037u8 wlcore_rate_to_idx(struct wl1271 *wl, u8 rate, enum ieee80211_band band)
f876bb9a
JO
5038{
5039 u8 idx;
5040
43a8bc5a 5041 BUG_ON(band >= 2);
f876bb9a 5042
43a8bc5a 5043 if (unlikely(rate >= wl->hw_tx_rate_tbl_size)) {
f876bb9a
JO
5044 wl1271_error("Illegal RX rate from HW: %d", rate);
5045 return 0;
5046 }
5047
43a8bc5a 5048 idx = wl->band_rate_to_idx[band][rate];
f876bb9a
JO
5049 if (unlikely(idx == CONF_HW_RXTX_RATE_UNSUPPORTED)) {
5050 wl1271_error("Unsupported RX rate from HW: %d", rate);
5051 return 0;
5052 }
5053
5054 return idx;
5055}
5056
7fc3a864
JO
5057static ssize_t wl1271_sysfs_show_bt_coex_state(struct device *dev,
5058 struct device_attribute *attr,
5059 char *buf)
5060{
5061 struct wl1271 *wl = dev_get_drvdata(dev);
5062 ssize_t len;
5063
2f63b011 5064 len = PAGE_SIZE;
7fc3a864
JO
5065
5066 mutex_lock(&wl->mutex);
5067 len = snprintf(buf, len, "%d\n\n0 - off\n1 - on\n",
5068 wl->sg_enabled);
5069 mutex_unlock(&wl->mutex);
5070
5071 return len;
5072
5073}
5074
5075static ssize_t wl1271_sysfs_store_bt_coex_state(struct device *dev,
5076 struct device_attribute *attr,
5077 const char *buf, size_t count)
5078{
5079 struct wl1271 *wl = dev_get_drvdata(dev);
5080 unsigned long res;
5081 int ret;
5082
6277ed65 5083 ret = kstrtoul(buf, 10, &res);
7fc3a864
JO
5084 if (ret < 0) {
5085 wl1271_warning("incorrect value written to bt_coex_mode");
5086 return count;
5087 }
5088
5089 mutex_lock(&wl->mutex);
5090
5091 res = !!res;
5092
5093 if (res == wl->sg_enabled)
5094 goto out;
5095
5096 wl->sg_enabled = res;
5097
4cc53383 5098 if (unlikely(wl->state != WLCORE_STATE_ON))
7fc3a864
JO
5099 goto out;
5100
a620865e 5101 ret = wl1271_ps_elp_wakeup(wl);
7fc3a864
JO
5102 if (ret < 0)
5103 goto out;
5104
5105 wl1271_acx_sg_enable(wl, wl->sg_enabled);
5106 wl1271_ps_elp_sleep(wl);
5107
5108 out:
5109 mutex_unlock(&wl->mutex);
5110 return count;
5111}
5112
5113static DEVICE_ATTR(bt_coex_state, S_IRUGO | S_IWUSR,
5114 wl1271_sysfs_show_bt_coex_state,
5115 wl1271_sysfs_store_bt_coex_state);
5116
d717fd61
JO
5117static ssize_t wl1271_sysfs_show_hw_pg_ver(struct device *dev,
5118 struct device_attribute *attr,
5119 char *buf)
5120{
5121 struct wl1271 *wl = dev_get_drvdata(dev);
5122 ssize_t len;
5123
2f63b011 5124 len = PAGE_SIZE;
d717fd61
JO
5125
5126 mutex_lock(&wl->mutex);
5127 if (wl->hw_pg_ver >= 0)
5128 len = snprintf(buf, len, "%d\n", wl->hw_pg_ver);
5129 else
5130 len = snprintf(buf, len, "n/a\n");
5131 mutex_unlock(&wl->mutex);
5132
5133 return len;
5134}
5135
6f07b72a 5136static DEVICE_ATTR(hw_pg_ver, S_IRUGO,
d717fd61
JO
5137 wl1271_sysfs_show_hw_pg_ver, NULL);
5138
95dac04f
IY
5139static ssize_t wl1271_sysfs_read_fwlog(struct file *filp, struct kobject *kobj,
5140 struct bin_attribute *bin_attr,
5141 char *buffer, loff_t pos, size_t count)
5142{
5143 struct device *dev = container_of(kobj, struct device, kobj);
5144 struct wl1271 *wl = dev_get_drvdata(dev);
5145 ssize_t len;
5146 int ret;
5147
5148 ret = mutex_lock_interruptible(&wl->mutex);
5149 if (ret < 0)
5150 return -ERESTARTSYS;
5151
5152 /* Let only one thread read the log at a time, blocking others */
5153 while (wl->fwlog_size == 0) {
5154 DEFINE_WAIT(wait);
5155
5156 prepare_to_wait_exclusive(&wl->fwlog_waitq,
5157 &wait,
5158 TASK_INTERRUPTIBLE);
5159
5160 if (wl->fwlog_size != 0) {
5161 finish_wait(&wl->fwlog_waitq, &wait);
5162 break;
5163 }
5164
5165 mutex_unlock(&wl->mutex);
5166
5167 schedule();
5168 finish_wait(&wl->fwlog_waitq, &wait);
5169
5170 if (signal_pending(current))
5171 return -ERESTARTSYS;
5172
5173 ret = mutex_lock_interruptible(&wl->mutex);
5174 if (ret < 0)
5175 return -ERESTARTSYS;
5176 }
5177
5178 /* Check if the fwlog is still valid */
5179 if (wl->fwlog_size < 0) {
5180 mutex_unlock(&wl->mutex);
5181 return 0;
5182 }
5183
5184 /* Seeking is not supported - old logs are not kept. Disregard pos. */
5185 len = min(count, (size_t)wl->fwlog_size);
5186 wl->fwlog_size -= len;
5187 memcpy(buffer, wl->fwlog, len);
5188
5189 /* Make room for new messages */
5190 memmove(wl->fwlog, wl->fwlog + len, wl->fwlog_size);
5191
5192 mutex_unlock(&wl->mutex);
5193
5194 return len;
5195}
5196
5197static struct bin_attribute fwlog_attr = {
5198 .attr = {.name = "fwlog", .mode = S_IRUSR},
5199 .read = wl1271_sysfs_read_fwlog,
5200};
5201
22479972 5202static void wl1271_connection_loss_work(struct work_struct *work)
5f561f68
BM
5203{
5204 struct delayed_work *dwork;
5205 struct wl1271 *wl;
5206 struct ieee80211_vif *vif;
5207 struct wl12xx_vif *wlvif;
5208
5209 dwork = container_of(work, struct delayed_work, work);
5210 wl = container_of(dwork, struct wl1271, connection_loss_work);
5211
5212 wl1271_info("Connection loss work.");
5213
5214 mutex_lock(&wl->mutex);
5215
4cc53383 5216 if (unlikely(wl->state != WLCORE_STATE_ON))
5f561f68
BM
5217 goto out;
5218
5219 /* Call mac80211 connection loss */
5220 wl12xx_for_each_wlvif_sta(wl, wlvif) {
5221 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
5222 goto out;
5223 vif = wl12xx_wlvif_to_vif(wlvif);
5224 ieee80211_connection_loss(vif);
5225 }
5226out:
5227 mutex_unlock(&wl->mutex);
5228}
5229
f4afbed9 5230static void wl12xx_derive_mac_addresses(struct wl1271 *wl, u32 oui, u32 nic)
5e037e74
LC
5231{
5232 int i;
5233
f4afbed9
AN
5234 wl1271_debug(DEBUG_PROBE, "base address: oui %06x nic %06x",
5235 oui, nic);
5e037e74 5236
f4afbed9 5237 if (nic + WLCORE_NUM_MAC_ADDRESSES - wl->num_mac_addr > 0xffffff)
5e037e74
LC
5238 wl1271_warning("NIC part of the MAC address wraps around!");
5239
f4afbed9 5240 for (i = 0; i < wl->num_mac_addr; i++) {
5e037e74
LC
5241 wl->addresses[i].addr[0] = (u8)(oui >> 16);
5242 wl->addresses[i].addr[1] = (u8)(oui >> 8);
5243 wl->addresses[i].addr[2] = (u8) oui;
5244 wl->addresses[i].addr[3] = (u8)(nic >> 16);
5245 wl->addresses[i].addr[4] = (u8)(nic >> 8);
5246 wl->addresses[i].addr[5] = (u8) nic;
5247 nic++;
5248 }
5249
f4afbed9
AN
5250 /* we may be one address short at the most */
5251 WARN_ON(wl->num_mac_addr + 1 < WLCORE_NUM_MAC_ADDRESSES);
5252
5253 /*
5254 * turn on the LAA bit in the first address and use it as
5255 * the last address.
5256 */
5257 if (wl->num_mac_addr < WLCORE_NUM_MAC_ADDRESSES) {
5258 int idx = WLCORE_NUM_MAC_ADDRESSES - 1;
5259 memcpy(&wl->addresses[idx], &wl->addresses[0],
5260 sizeof(wl->addresses[0]));
5261 /* LAA bit */
5262 wl->addresses[idx].addr[2] |= BIT(1);
5263 }
5264
5265 wl->hw->wiphy->n_addresses = WLCORE_NUM_MAC_ADDRESSES;
5e037e74
LC
5266 wl->hw->wiphy->addresses = wl->addresses;
5267}
5268
30c5dbd1
LC
5269static int wl12xx_get_hw_info(struct wl1271 *wl)
5270{
5271 int ret;
30c5dbd1
LC
5272
5273 ret = wl12xx_set_power_on(wl);
5274 if (ret < 0)
4fb4e0be 5275 return ret;
30c5dbd1 5276
6134323f
IY
5277 ret = wlcore_read_reg(wl, REG_CHIP_ID_B, &wl->chip.id);
5278 if (ret < 0)
5279 goto out;
30c5dbd1 5280
00782136
LC
5281 wl->fuse_oui_addr = 0;
5282 wl->fuse_nic_addr = 0;
30c5dbd1 5283
6134323f
IY
5284 ret = wl->ops->get_pg_ver(wl, &wl->hw_pg_ver);
5285 if (ret < 0)
5286 goto out;
30c5dbd1 5287
30d9b4a5 5288 if (wl->ops->get_mac)
6134323f 5289 ret = wl->ops->get_mac(wl);
5e037e74 5290
30c5dbd1 5291out:
6134323f 5292 wl1271_power_off(wl);
30c5dbd1
LC
5293 return ret;
5294}
5295
4b32a2c9 5296static int wl1271_register_hw(struct wl1271 *wl)
f5fc0f86
LC
5297{
5298 int ret;
5e037e74 5299 u32 oui_addr = 0, nic_addr = 0;
f5fc0f86
LC
5300
5301 if (wl->mac80211_registered)
5302 return 0;
5303
6f8d6b20 5304 if (wl->nvs_len >= 12) {
bc765bf3
SL
5305 /* NOTE: The wl->nvs->nvs element must be first, in
5306 * order to simplify the casting, we assume it is at
5307 * the beginning of the wl->nvs structure.
5308 */
5309 u8 *nvs_ptr = (u8 *)wl->nvs;
31d26ec6 5310
5e037e74
LC
5311 oui_addr =
5312 (nvs_ptr[11] << 16) + (nvs_ptr[10] << 8) + nvs_ptr[6];
5313 nic_addr =
5314 (nvs_ptr[5] << 16) + (nvs_ptr[4] << 8) + nvs_ptr[3];
5315 }
5316
5317 /* if the MAC address is zeroed in the NVS derive from fuse */
5318 if (oui_addr == 0 && nic_addr == 0) {
5319 oui_addr = wl->fuse_oui_addr;
5320 /* fuse has the BD_ADDR, the WLAN addresses are the next two */
5321 nic_addr = wl->fuse_nic_addr + 1;
31d26ec6
AN
5322 }
5323
f4afbed9 5324 wl12xx_derive_mac_addresses(wl, oui_addr, nic_addr);
f5fc0f86
LC
5325
5326 ret = ieee80211_register_hw(wl->hw);
5327 if (ret < 0) {
5328 wl1271_error("unable to register mac80211 hw: %d", ret);
30c5dbd1 5329 goto out;
f5fc0f86
LC
5330 }
5331
5332 wl->mac80211_registered = true;
5333
d60080ae
EP
5334 wl1271_debugfs_init(wl);
5335
f5fc0f86
LC
5336 wl1271_notice("loaded");
5337
30c5dbd1
LC
5338out:
5339 return ret;
f5fc0f86
LC
5340}
5341
4b32a2c9 5342static void wl1271_unregister_hw(struct wl1271 *wl)
3b56dd6a 5343{
3fcdab70 5344 if (wl->plt)
f3df1331 5345 wl1271_plt_stop(wl);
4ae3fa87 5346
3b56dd6a
TP
5347 ieee80211_unregister_hw(wl->hw);
5348 wl->mac80211_registered = false;
5349
5350}
3b56dd6a 5351
bcab320b
EP
5352static const struct ieee80211_iface_limit wlcore_iface_limits[] = {
5353 {
e7a6ba29 5354 .max = 3,
bcab320b
EP
5355 .types = BIT(NL80211_IFTYPE_STATION),
5356 },
5357 {
5358 .max = 1,
5359 .types = BIT(NL80211_IFTYPE_AP) |
5360 BIT(NL80211_IFTYPE_P2P_GO) |
5361 BIT(NL80211_IFTYPE_P2P_CLIENT),
5362 },
5363};
5364
5365static const struct ieee80211_iface_combination
5366wlcore_iface_combinations[] = {
5367 {
5368 .num_different_channels = 1,
e7a6ba29 5369 .max_interfaces = 3,
bcab320b
EP
5370 .limits = wlcore_iface_limits,
5371 .n_limits = ARRAY_SIZE(wlcore_iface_limits),
5372 },
5373};
5374
4b32a2c9 5375static int wl1271_init_ieee80211(struct wl1271 *wl)
f5fc0f86 5376{
7a55724e
JO
5377 static const u32 cipher_suites[] = {
5378 WLAN_CIPHER_SUITE_WEP40,
5379 WLAN_CIPHER_SUITE_WEP104,
5380 WLAN_CIPHER_SUITE_TKIP,
5381 WLAN_CIPHER_SUITE_CCMP,
5382 WL1271_CIPHER_SUITE_GEM,
5383 };
5384
2c0133a4
AN
5385 /* The tx descriptor buffer */
5386 wl->hw->extra_tx_headroom = sizeof(struct wl1271_tx_hw_descr);
5387
5388 if (wl->quirks & WLCORE_QUIRK_TKIP_HEADER_SPACE)
5389 wl->hw->extra_tx_headroom += WL1271_EXTRA_SPACE_TKIP;
f5fc0f86
LC
5390
5391 /* unit us */
5392 /* FIXME: find a proper value */
5393 wl->hw->channel_change_time = 10000;
50c500ad 5394 wl->hw->max_listen_interval = wl->conf.conn.max_listen_interval;
f5fc0f86
LC
5395
5396 wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
0a34332f 5397 IEEE80211_HW_SUPPORTS_PS |
f1d63a59 5398 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
4695dc91 5399 IEEE80211_HW_SUPPORTS_UAPSD |
a9af092b 5400 IEEE80211_HW_HAS_RATE_CONTROL |
00236aed 5401 IEEE80211_HW_CONNECTION_MONITOR |
25eaea30 5402 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
fcd23b63 5403 IEEE80211_HW_SPECTRUM_MGMT |
93f8c8e0
AN
5404 IEEE80211_HW_AP_LINK_PS |
5405 IEEE80211_HW_AMPDU_AGGREGATION |
79aba1ba
EP
5406 IEEE80211_HW_TX_AMPDU_SETUP_IN_HW |
5407 IEEE80211_HW_SCAN_WHILE_IDLE;
f5fc0f86 5408
7a55724e
JO
5409 wl->hw->wiphy->cipher_suites = cipher_suites;
5410 wl->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
5411
e0d8bbf0 5412 wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
045c745f
EP
5413 BIT(NL80211_IFTYPE_ADHOC) | BIT(NL80211_IFTYPE_AP) |
5414 BIT(NL80211_IFTYPE_P2P_CLIENT) | BIT(NL80211_IFTYPE_P2P_GO);
f5fc0f86 5415 wl->hw->wiphy->max_scan_ssids = 1;
221737d2
LC
5416 wl->hw->wiphy->max_sched_scan_ssids = 16;
5417 wl->hw->wiphy->max_match_sets = 16;
ea559b46
GE
5418 /*
5419 * Maximum length of elements in scanning probe request templates
5420 * should be the maximum length possible for a template, without
5421 * the IEEE80211 header of the template
5422 */
c08e371a 5423 wl->hw->wiphy->max_scan_ie_len = WL1271_CMD_TEMPL_MAX_SIZE -
ea559b46 5424 sizeof(struct ieee80211_header);
a8aaaf53 5425
c08e371a 5426 wl->hw->wiphy->max_sched_scan_ie_len = WL1271_CMD_TEMPL_MAX_SIZE -
c9e79a47
LC
5427 sizeof(struct ieee80211_header);
5428
dabf37db
EP
5429 wl->hw->wiphy->max_remain_on_channel_duration = 5000;
5430
81ddbb5c
JB
5431 wl->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD |
5432 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
1ec23f7f 5433
4a31c11c
LC
5434 /* make sure all our channels fit in the scanned_ch bitmask */
5435 BUILD_BUG_ON(ARRAY_SIZE(wl1271_channels) +
5436 ARRAY_SIZE(wl1271_channels_5ghz) >
5437 WL1271_MAX_CHANNELS);
a8aaaf53
LC
5438 /*
5439 * We keep local copies of the band structs because we need to
5440 * modify them on a per-device basis.
5441 */
5442 memcpy(&wl->bands[IEEE80211_BAND_2GHZ], &wl1271_band_2ghz,
5443 sizeof(wl1271_band_2ghz));
bfb92ca1
EP
5444 memcpy(&wl->bands[IEEE80211_BAND_2GHZ].ht_cap,
5445 &wl->ht_cap[IEEE80211_BAND_2GHZ],
5446 sizeof(*wl->ht_cap));
a8aaaf53
LC
5447 memcpy(&wl->bands[IEEE80211_BAND_5GHZ], &wl1271_band_5ghz,
5448 sizeof(wl1271_band_5ghz));
bfb92ca1
EP
5449 memcpy(&wl->bands[IEEE80211_BAND_5GHZ].ht_cap,
5450 &wl->ht_cap[IEEE80211_BAND_5GHZ],
5451 sizeof(*wl->ht_cap));
a8aaaf53
LC
5452
5453 wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
5454 &wl->bands[IEEE80211_BAND_2GHZ];
5455 wl->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
5456 &wl->bands[IEEE80211_BAND_5GHZ];
1ebec3d7 5457
12bd8949 5458 wl->hw->queues = 4;
31627dc5 5459 wl->hw->max_rates = 1;
12bd8949 5460
b7417d93
JO
5461 wl->hw->wiphy->reg_notifier = wl1271_reg_notify;
5462
9c1b190b
AN
5463 /* the FW answers probe-requests in AP-mode */
5464 wl->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
5465 wl->hw->wiphy->probe_resp_offload =
5466 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
5467 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
5468 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
5469
bcab320b
EP
5470 /* allowed interface combinations */
5471 wl->hw->wiphy->iface_combinations = wlcore_iface_combinations;
5472 wl->hw->wiphy->n_iface_combinations =
5473 ARRAY_SIZE(wlcore_iface_combinations);
5474
a390e85c 5475 SET_IEEE80211_DEV(wl->hw, wl->dev);
f5fc0f86 5476
f84f7d78 5477 wl->hw->sta_data_size = sizeof(struct wl1271_station);
87fbcb0f 5478 wl->hw->vif_data_size = sizeof(struct wl12xx_vif);
f84f7d78 5479
ba421f8f 5480 wl->hw->max_rx_aggregation_subframes = wl->conf.ht.rx_ba_win_size;
4c9cfa78 5481
f5fc0f86
LC
5482 return 0;
5483}
5484
f5fc0f86 5485#define WL1271_DEFAULT_CHANNEL 0
c332a4b8 5486
26a309c7 5487struct ieee80211_hw *wlcore_alloc_hw(size_t priv_size, u32 aggr_buf_size)
f5fc0f86 5488{
f5fc0f86
LC
5489 struct ieee80211_hw *hw;
5490 struct wl1271 *wl;
a8c0ddb5 5491 int i, j, ret;
1f37cbc9 5492 unsigned int order;
f5fc0f86 5493
c7ffb902 5494 BUILD_BUG_ON(AP_MAX_STATIONS > WL12XX_MAX_LINKS);
f80c2d12 5495
f5fc0f86
LC
5496 hw = ieee80211_alloc_hw(sizeof(*wl), &wl1271_ops);
5497 if (!hw) {
5498 wl1271_error("could not alloc ieee80211_hw");
a1dd8187 5499 ret = -ENOMEM;
3b56dd6a
TP
5500 goto err_hw_alloc;
5501 }
5502
f5fc0f86
LC
5503 wl = hw->priv;
5504 memset(wl, 0, sizeof(*wl));
5505
96e0c683
AN
5506 wl->priv = kzalloc(priv_size, GFP_KERNEL);
5507 if (!wl->priv) {
5508 wl1271_error("could not alloc wl priv");
5509 ret = -ENOMEM;
5510 goto err_priv_alloc;
5511 }
5512
87627214 5513 INIT_LIST_HEAD(&wl->wlvif_list);
01c09162 5514
f5fc0f86 5515 wl->hw = hw;
f5fc0f86 5516
a8c0ddb5 5517 for (i = 0; i < NUM_TX_QUEUES; i++)
c7ffb902 5518 for (j = 0; j < WL12XX_MAX_LINKS; j++)
a8c0ddb5
AN
5519 skb_queue_head_init(&wl->links[j].tx_queue[i]);
5520
a620865e
IY
5521 skb_queue_head_init(&wl->deferred_rx_queue);
5522 skb_queue_head_init(&wl->deferred_tx_queue);
5523
37b70a81 5524 INIT_DELAYED_WORK(&wl->elp_work, wl1271_elp_work);
a620865e 5525 INIT_WORK(&wl->netstack_work, wl1271_netstack_work);
117b38d0
JO
5526 INIT_WORK(&wl->tx_work, wl1271_tx_work);
5527 INIT_WORK(&wl->recovery_work, wl1271_recovery_work);
5528 INIT_DELAYED_WORK(&wl->scan_complete_work, wl1271_scan_complete_work);
dabf37db 5529 INIT_DELAYED_WORK(&wl->roc_complete_work, wlcore_roc_complete_work);
55df5afb 5530 INIT_DELAYED_WORK(&wl->tx_watchdog_work, wl12xx_tx_watchdog_work);
5f561f68
BM
5531 INIT_DELAYED_WORK(&wl->connection_loss_work,
5532 wl1271_connection_loss_work);
77ddaa10 5533
92ef8960
EP
5534 wl->freezable_wq = create_freezable_workqueue("wl12xx_wq");
5535 if (!wl->freezable_wq) {
5536 ret = -ENOMEM;
5537 goto err_hw;
5538 }
5539
f5fc0f86 5540 wl->channel = WL1271_DEFAULT_CHANNEL;
f5fc0f86 5541 wl->rx_counter = 0;
f5fc0f86 5542 wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
8a5a37a6 5543 wl->band = IEEE80211_BAND_2GHZ;
83d08d3f 5544 wl->channel_type = NL80211_CHAN_NO_HT;
830fb67b 5545 wl->flags = 0;
7fc3a864 5546 wl->sg_enabled = true;
66340e5b 5547 wl->sleep_auth = WL1271_PSM_ILLEGAL;
d717fd61 5548 wl->hw_pg_ver = -1;
b622d992
AN
5549 wl->ap_ps_map = 0;
5550 wl->ap_fw_ps_map = 0;
606ea9fa 5551 wl->quirks = 0;
341b7cde 5552 wl->platform_quirks = 0;
33c2c06c 5553 wl->sched_scanning = false;
f4df1bd5 5554 wl->system_hlid = WL12XX_SYSTEM_HLID;
da03209e 5555 wl->active_sta_count = 0;
95dac04f
IY
5556 wl->fwlog_size = 0;
5557 init_waitqueue_head(&wl->fwlog_waitq);
f5fc0f86 5558
f4df1bd5
EP
5559 /* The system link is always allocated */
5560 __set_bit(WL12XX_SYSTEM_HLID, wl->links_map);
5561
25eeb9e3 5562 memset(wl->tx_frames_map, 0, sizeof(wl->tx_frames_map));
72b0624f 5563 for (i = 0; i < wl->num_tx_desc; i++)
f5fc0f86
LC
5564 wl->tx_frames[i] = NULL;
5565
5566 spin_lock_init(&wl->wl_lock);
5567
4cc53383 5568 wl->state = WLCORE_STATE_OFF;
3fcdab70 5569 wl->fw_type = WL12XX_FW_TYPE_NONE;
f5fc0f86 5570 mutex_init(&wl->mutex);
2c38849f 5571 mutex_init(&wl->flush_mutex);
6f8d6b20 5572 init_completion(&wl->nvs_loading_complete);
f5fc0f86 5573
26a309c7 5574 order = get_order(aggr_buf_size);
1f37cbc9
IY
5575 wl->aggr_buf = (u8 *)__get_free_pages(GFP_KERNEL, order);
5576 if (!wl->aggr_buf) {
5577 ret = -ENOMEM;
92ef8960 5578 goto err_wq;
1f37cbc9 5579 }
26a309c7 5580 wl->aggr_buf_size = aggr_buf_size;
1f37cbc9 5581
990f5de7
IY
5582 wl->dummy_packet = wl12xx_alloc_dummy_packet(wl);
5583 if (!wl->dummy_packet) {
5584 ret = -ENOMEM;
5585 goto err_aggr;
5586 }
5587
95dac04f
IY
5588 /* Allocate one page for the FW log */
5589 wl->fwlog = (u8 *)get_zeroed_page(GFP_KERNEL);
5590 if (!wl->fwlog) {
5591 ret = -ENOMEM;
5592 goto err_dummy_packet;
5593 }
5594
fd492ed7 5595 wl->mbox = kmalloc(sizeof(*wl->mbox), GFP_KERNEL | GFP_DMA);
690142e9
MG
5596 if (!wl->mbox) {
5597 ret = -ENOMEM;
5598 goto err_fwlog;
5599 }
5600
c332a4b8 5601 return hw;
a1dd8187 5602
690142e9
MG
5603err_fwlog:
5604 free_page((unsigned long)wl->fwlog);
5605
990f5de7
IY
5606err_dummy_packet:
5607 dev_kfree_skb(wl->dummy_packet);
5608
1f37cbc9
IY
5609err_aggr:
5610 free_pages((unsigned long)wl->aggr_buf, order);
5611
92ef8960
EP
5612err_wq:
5613 destroy_workqueue(wl->freezable_wq);
5614
a1dd8187 5615err_hw:
3b56dd6a 5616 wl1271_debugfs_exit(wl);
96e0c683
AN
5617 kfree(wl->priv);
5618
5619err_priv_alloc:
3b56dd6a
TP
5620 ieee80211_free_hw(hw);
5621
5622err_hw_alloc:
a1dd8187 5623
a1dd8187 5624 return ERR_PTR(ret);
c332a4b8 5625}
ffeb501c 5626EXPORT_SYMBOL_GPL(wlcore_alloc_hw);
c332a4b8 5627
ffeb501c 5628int wlcore_free_hw(struct wl1271 *wl)
c332a4b8 5629{
95dac04f
IY
5630 /* Unblock any fwlog readers */
5631 mutex_lock(&wl->mutex);
5632 wl->fwlog_size = -1;
5633 wake_up_interruptible_all(&wl->fwlog_waitq);
5634 mutex_unlock(&wl->mutex);
5635
f79f890c 5636 device_remove_bin_file(wl->dev, &fwlog_attr);
6f07b72a 5637
f79f890c 5638 device_remove_file(wl->dev, &dev_attr_hw_pg_ver);
6f07b72a 5639
f79f890c 5640 device_remove_file(wl->dev, &dev_attr_bt_coex_state);
a8e27820 5641 kfree(wl->mbox);
95dac04f 5642 free_page((unsigned long)wl->fwlog);
990f5de7 5643 dev_kfree_skb(wl->dummy_packet);
26a309c7 5644 free_pages((unsigned long)wl->aggr_buf, get_order(wl->aggr_buf_size));
c332a4b8
TP
5645
5646 wl1271_debugfs_exit(wl);
5647
c332a4b8
TP
5648 vfree(wl->fw);
5649 wl->fw = NULL;
3fcdab70 5650 wl->fw_type = WL12XX_FW_TYPE_NONE;
c332a4b8
TP
5651 kfree(wl->nvs);
5652 wl->nvs = NULL;
5653
0afd04e5 5654 kfree(wl->fw_status_1);
c332a4b8 5655 kfree(wl->tx_res_if);
92ef8960 5656 destroy_workqueue(wl->freezable_wq);
c332a4b8 5657
96e0c683 5658 kfree(wl->priv);
c332a4b8
TP
5659 ieee80211_free_hw(wl->hw);
5660
5661 return 0;
5662}
ffeb501c 5663EXPORT_SYMBOL_GPL(wlcore_free_hw);
50b3eb4b 5664
a390e85c
FB
5665static irqreturn_t wl12xx_hardirq(int irq, void *cookie)
5666{
5667 struct wl1271 *wl = cookie;
5668 unsigned long flags;
5669
5670 wl1271_debug(DEBUG_IRQ, "IRQ");
5671
5672 /* complete the ELP completion */
5673 spin_lock_irqsave(&wl->wl_lock, flags);
5674 set_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
5675 if (wl->elp_compl) {
5676 complete(wl->elp_compl);
5677 wl->elp_compl = NULL;
5678 }
5679
5680 if (test_bit(WL1271_FLAG_SUSPENDED, &wl->flags)) {
5681 /* don't enqueue a work right now. mark it as pending */
5682 set_bit(WL1271_FLAG_PENDING_WORK, &wl->flags);
5683 wl1271_debug(DEBUG_IRQ, "should not enqueue work");
5684 disable_irq_nosync(wl->irq);
5685 pm_wakeup_event(wl->dev, 0);
5686 spin_unlock_irqrestore(&wl->wl_lock, flags);
5687 return IRQ_HANDLED;
5688 }
5689 spin_unlock_irqrestore(&wl->wl_lock, flags);
5690
5691 return IRQ_WAKE_THREAD;
5692}
5693
6f8d6b20 5694static void wlcore_nvs_cb(const struct firmware *fw, void *context)
ce2a217c 5695{
6f8d6b20
IY
5696 struct wl1271 *wl = context;
5697 struct platform_device *pdev = wl->pdev;
a390e85c 5698 struct wl12xx_platform_data *pdata = pdev->dev.platform_data;
a390e85c 5699 unsigned long irqflags;
ffeb501c 5700 int ret;
a390e85c 5701
6f8d6b20
IY
5702 if (fw) {
5703 wl->nvs = kmemdup(fw->data, fw->size, GFP_KERNEL);
5704 if (!wl->nvs) {
5705 wl1271_error("Could not allocate nvs data");
5706 goto out;
5707 }
5708 wl->nvs_len = fw->size;
5709 } else {
5710 wl1271_debug(DEBUG_BOOT, "Could not get nvs file %s",
5711 WL12XX_NVS_NAME);
5712 wl->nvs = NULL;
5713 wl->nvs_len = 0;
a390e85c
FB
5714 }
5715
3992eb2b
IY
5716 ret = wl->ops->setup(wl);
5717 if (ret < 0)
6f8d6b20 5718 goto out_free_nvs;
3992eb2b 5719
72b0624f
AN
5720 BUG_ON(wl->num_tx_desc > WLCORE_MAX_TX_DESCRIPTORS);
5721
e87288f0
LC
5722 /* adjust some runtime configuration parameters */
5723 wlcore_adjust_conf(wl);
5724
a390e85c 5725 wl->irq = platform_get_irq(pdev, 0);
a390e85c
FB
5726 wl->platform_quirks = pdata->platform_quirks;
5727 wl->set_power = pdata->set_power;
a390e85c
FB
5728 wl->if_ops = pdata->ops;
5729
a390e85c
FB
5730 if (wl->platform_quirks & WL12XX_PLATFORM_QUIRK_EDGE_IRQ)
5731 irqflags = IRQF_TRIGGER_RISING;
5732 else
5733 irqflags = IRQF_TRIGGER_HIGH | IRQF_ONESHOT;
5734
b5b45b3c 5735 ret = request_threaded_irq(wl->irq, wl12xx_hardirq, wlcore_irq,
a390e85c
FB
5736 irqflags,
5737 pdev->name, wl);
5738 if (ret < 0) {
5739 wl1271_error("request_irq() failed: %d", ret);
6f8d6b20 5740 goto out_free_nvs;
a390e85c
FB
5741 }
5742
dfb89c56 5743#ifdef CONFIG_PM
a390e85c
FB
5744 ret = enable_irq_wake(wl->irq);
5745 if (!ret) {
5746 wl->irq_wake_enabled = true;
5747 device_init_wakeup(wl->dev, 1);
b95d7cef 5748 if (pdata->pwr_in_suspend) {
ffeb501c 5749 wl->hw->wiphy->wowlan.flags = WIPHY_WOWLAN_ANY;
b95d7cef
ES
5750 wl->hw->wiphy->wowlan.n_patterns =
5751 WL1271_MAX_RX_FILTERS;
5752 wl->hw->wiphy->wowlan.pattern_min_len = 1;
5753 wl->hw->wiphy->wowlan.pattern_max_len =
5754 WL1271_RX_FILTER_MAX_PATTERN_SIZE;
5755 }
a390e85c 5756 }
dfb89c56 5757#endif
a390e85c
FB
5758 disable_irq(wl->irq);
5759
4afc37a0
LC
5760 ret = wl12xx_get_hw_info(wl);
5761 if (ret < 0) {
5762 wl1271_error("couldn't get hw info");
8b425e62 5763 goto out_irq;
4afc37a0
LC
5764 }
5765
5766 ret = wl->ops->identify_chip(wl);
5767 if (ret < 0)
8b425e62 5768 goto out_irq;
4afc37a0 5769
a390e85c
FB
5770 ret = wl1271_init_ieee80211(wl);
5771 if (ret)
5772 goto out_irq;
5773
5774 ret = wl1271_register_hw(wl);
5775 if (ret)
5776 goto out_irq;
5777
f79f890c
FB
5778 /* Create sysfs file to control bt coex state */
5779 ret = device_create_file(wl->dev, &dev_attr_bt_coex_state);
5780 if (ret < 0) {
5781 wl1271_error("failed to create sysfs file bt_coex_state");
8b425e62 5782 goto out_unreg;
f79f890c
FB
5783 }
5784
5785 /* Create sysfs file to get HW PG version */
5786 ret = device_create_file(wl->dev, &dev_attr_hw_pg_ver);
5787 if (ret < 0) {
5788 wl1271_error("failed to create sysfs file hw_pg_ver");
5789 goto out_bt_coex_state;
5790 }
5791
5792 /* Create sysfs file for the FW log */
5793 ret = device_create_bin_file(wl->dev, &fwlog_attr);
5794 if (ret < 0) {
5795 wl1271_error("failed to create sysfs file fwlog");
5796 goto out_hw_pg_ver;
5797 }
5798
6f8d6b20 5799 wl->initialized = true;
ffeb501c 5800 goto out;
a390e85c 5801
f79f890c
FB
5802out_hw_pg_ver:
5803 device_remove_file(wl->dev, &dev_attr_hw_pg_ver);
5804
5805out_bt_coex_state:
5806 device_remove_file(wl->dev, &dev_attr_bt_coex_state);
5807
8b425e62
LC
5808out_unreg:
5809 wl1271_unregister_hw(wl);
5810
a390e85c
FB
5811out_irq:
5812 free_irq(wl->irq, wl);
5813
6f8d6b20
IY
5814out_free_nvs:
5815 kfree(wl->nvs);
5816
a390e85c 5817out:
6f8d6b20
IY
5818 release_firmware(fw);
5819 complete_all(&wl->nvs_loading_complete);
5820}
5821
5822int __devinit wlcore_probe(struct wl1271 *wl, struct platform_device *pdev)
5823{
5824 int ret;
5825
5826 if (!wl->ops || !wl->ptable)
5827 return -EINVAL;
5828
5829 wl->dev = &pdev->dev;
5830 wl->pdev = pdev;
5831 platform_set_drvdata(pdev, wl);
5832
5833 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
5834 WL12XX_NVS_NAME, &pdev->dev, GFP_KERNEL,
5835 wl, wlcore_nvs_cb);
5836 if (ret < 0) {
5837 wl1271_error("request_firmware_nowait failed: %d", ret);
5838 complete_all(&wl->nvs_loading_complete);
5839 }
5840
a390e85c 5841 return ret;
ce2a217c 5842}
b2ba99ff 5843EXPORT_SYMBOL_GPL(wlcore_probe);
ce2a217c 5844
b2ba99ff 5845int __devexit wlcore_remove(struct platform_device *pdev)
ce2a217c 5846{
a390e85c
FB
5847 struct wl1271 *wl = platform_get_drvdata(pdev);
5848
6f8d6b20
IY
5849 wait_for_completion(&wl->nvs_loading_complete);
5850 if (!wl->initialized)
5851 return 0;
5852
a390e85c
FB
5853 if (wl->irq_wake_enabled) {
5854 device_init_wakeup(wl->dev, 0);
5855 disable_irq_wake(wl->irq);
5856 }
5857 wl1271_unregister_hw(wl);
5858 free_irq(wl->irq, wl);
ffeb501c 5859 wlcore_free_hw(wl);
a390e85c 5860
ce2a217c
FB
5861 return 0;
5862}
b2ba99ff 5863EXPORT_SYMBOL_GPL(wlcore_remove);
ce2a217c 5864
491bbd6b 5865u32 wl12xx_debug_level = DEBUG_NONE;
17c1755c 5866EXPORT_SYMBOL_GPL(wl12xx_debug_level);
491bbd6b 5867module_param_named(debug_level, wl12xx_debug_level, uint, S_IRUSR | S_IWUSR);
17c1755c
EP
5868MODULE_PARM_DESC(debug_level, "wl12xx debugging level");
5869
95dac04f 5870module_param_named(fwlog, fwlog_param, charp, 0);
2c882fa4 5871MODULE_PARM_DESC(fwlog,
95dac04f
IY
5872 "FW logger options: continuous, ondemand, dbgpins or disable");
5873
2a5bff09
EP
5874module_param(bug_on_recovery, bool, S_IRUSR | S_IWUSR);
5875MODULE_PARM_DESC(bug_on_recovery, "BUG() on fw recovery");
5876
34785be5
AN
5877module_param(no_recovery, bool, S_IRUSR | S_IWUSR);
5878MODULE_PARM_DESC(no_recovery, "Prevent HW recovery. FW will remain stuck.");
5879
50b3eb4b 5880MODULE_LICENSE("GPL");
b1a48cab 5881MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
50b3eb4b 5882MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
0635ad45 5883MODULE_FIRMWARE(WL12XX_NVS_NAME);
This page took 0.929157 seconds and 5 git commands to generate.