wlcore: update commands enum to new fw api
[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
48af2eb0 3682 if (changed & BSS_CHANGED_BEACON_INT) {
e78a287a 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
48af2eb0 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
48af2eb0 3747 if (changed & BSS_CHANGED_BEACON_ENABLED) {
e78a287a 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 */
598b262a 3823 if (wl->sched_scanning)
3230f35e 3824 wl1271_scan_sched_scan_stop(wl, wlvif);
3230f35e
EP
3825
3826 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
3827 if (ret < 0)
3828 return ret;
3829
3830 ret = wl12xx_cmd_build_null_data(wl, wlvif);
3831 if (ret < 0)
3832 return ret;
3833
3834 ret = wl1271_build_qos_null_data(wl, wl12xx_wlvif_to_vif(wlvif));
3835 if (ret < 0)
3836 return ret;
3837
3838 wlcore_set_ssid(wl, wlvif);
3839
3840 set_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
3841
3842 return 0;
3843}
3844
3845static int wlcore_clear_bssid(struct wl1271 *wl, struct wl12xx_vif *wlvif)
3846{
3847 int ret;
3848
3849 /* revert back to minimum rates for the current band */
3850 wl1271_set_band_rate(wl, wlvif);
3851 wlvif->basic_rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
3852
3853 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
3854 if (ret < 0)
3855 return ret;
3856
3857 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
3858 test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags)) {
3859 ret = wl12xx_cmd_role_stop_sta(wl, wlvif);
3860 if (ret < 0)
3861 return ret;
3862 }
3863
3864 clear_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
3865 return 0;
3866}
e78a287a
AN
3867/* STA/IBSS mode changes */
3868static void wl1271_bss_info_changed_sta(struct wl1271 *wl,
3869 struct ieee80211_vif *vif,
3870 struct ieee80211_bss_conf *bss_conf,
3871 u32 changed)
3872{
87fbcb0f 3873 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3230f35e 3874 bool do_join = false;
536129c8 3875 bool is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
227e81e1 3876 bool ibss_joined = false;
72c2d9e5 3877 u32 sta_rate_set = 0;
e78a287a 3878 int ret;
2d6e4e76 3879 struct ieee80211_sta *sta;
a100885d
AN
3880 bool sta_exists = false;
3881 struct ieee80211_sta_ht_cap sta_ht_cap;
e78a287a
AN
3882
3883 if (is_ibss) {
3884 ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf,
3885 changed);
3886 if (ret < 0)
3887 goto out;
e0d8bbf0
JO
3888 }
3889
227e81e1
EP
3890 if (changed & BSS_CHANGED_IBSS) {
3891 if (bss_conf->ibss_joined) {
eee514e3 3892 set_bit(WLVIF_FLAG_IBSS_JOINED, &wlvif->flags);
227e81e1
EP
3893 ibss_joined = true;
3894 } else {
3230f35e
EP
3895 wlcore_unset_assoc(wl, wlvif);
3896 wl12xx_cmd_role_stop_sta(wl, wlvif);
227e81e1
EP
3897 }
3898 }
3899
3900 if ((changed & BSS_CHANGED_BEACON_INT) && ibss_joined)
e78a287a
AN
3901 do_join = true;
3902
3903 /* Need to update the SSID (for filtering etc) */
227e81e1 3904 if ((changed & BSS_CHANGED_BEACON) && ibss_joined)
e78a287a
AN
3905 do_join = true;
3906
227e81e1 3907 if ((changed & BSS_CHANGED_BEACON_ENABLED) && ibss_joined) {
5da11dcd
JO
3908 wl1271_debug(DEBUG_ADHOC, "ad-hoc beaconing: %s",
3909 bss_conf->enable_beacon ? "enabled" : "disabled");
3910
5da11dcd
JO
3911 do_join = true;
3912 }
3913
48af2eb0 3914 if (changed & BSS_CHANGED_CQM) {
00236aed
JO
3915 bool enable = false;
3916 if (bss_conf->cqm_rssi_thold)
3917 enable = true;
0603d891 3918 ret = wl1271_acx_rssi_snr_trigger(wl, wlvif, enable,
00236aed
JO
3919 bss_conf->cqm_rssi_thold,
3920 bss_conf->cqm_rssi_hyst);
3921 if (ret < 0)
3922 goto out;
04324d99 3923 wlvif->rssi_thold = bss_conf->cqm_rssi_thold;
00236aed
JO
3924 }
3925
ec87011a
EP
3926 if (changed & (BSS_CHANGED_BSSID | BSS_CHANGED_HT |
3927 BSS_CHANGED_ASSOC)) {
0f9c8250
AN
3928 rcu_read_lock();
3929 sta = ieee80211_find_sta(vif, bss_conf->bssid);
ef08d028
LC
3930 if (sta) {
3931 u8 *rx_mask = sta->ht_cap.mcs.rx_mask;
3932
3933 /* save the supp_rates of the ap */
3934 sta_rate_set = sta->supp_rates[wlvif->band];
3935 if (sta->ht_cap.ht_supported)
3936 sta_rate_set |=
3937 (rx_mask[0] << HW_HT_RATES_OFFSET) |
3938 (rx_mask[1] << HW_MIMO_RATES_OFFSET);
3939 sta_ht_cap = sta->ht_cap;
3940 sta_exists = true;
3941 }
3942
0f9c8250 3943 rcu_read_unlock();
72c2d9e5 3944 }
72c2d9e5 3945
3230f35e
EP
3946 if (changed & BSS_CHANGED_BSSID) {
3947 if (!is_zero_ether_addr(bss_conf->bssid)) {
3948 ret = wlcore_set_bssid(wl, wlvif, bss_conf,
3949 sta_rate_set);
f5fc0f86 3950 if (ret < 0)
e78a287a 3951 goto out;
f5fc0f86 3952
3230f35e
EP
3953 /* Need to update the BSSID (for filtering etc) */
3954 do_join = true;
d94cd297 3955 } else {
3230f35e 3956 ret = wlcore_clear_bssid(wl, wlvif);
6ccbb92e 3957 if (ret < 0)
e78a287a 3958 goto out;
f5fc0f86 3959 }
f5fc0f86
LC
3960 }
3961
d192d268
EP
3962 if (changed & BSS_CHANGED_IBSS) {
3963 wl1271_debug(DEBUG_ADHOC, "ibss_joined: %d",
3964 bss_conf->ibss_joined);
3965
3966 if (bss_conf->ibss_joined) {
3967 u32 rates = bss_conf->basic_rates;
87fbcb0f 3968 wlvif->basic_rate_set =
af7fbb28 3969 wl1271_tx_enabled_rates_get(wl, rates,
1b92f15e 3970 wlvif->band);
d2d66c56 3971 wlvif->basic_rate =
87fbcb0f
EP
3972 wl1271_tx_min_rate_get(wl,
3973 wlvif->basic_rate_set);
d192d268 3974
06b660e1 3975 /* by default, use 11b + OFDM rates */
30d0c8fd
EP
3976 wlvif->rate_set = CONF_TX_IBSS_DEFAULT_RATES;
3977 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
d192d268
EP
3978 if (ret < 0)
3979 goto out;
3980 }
3981 }
3982
0603d891 3983 ret = wl1271_bss_erp_info_changed(wl, vif, bss_conf, changed);
e78a287a
AN
3984 if (ret < 0)
3985 goto out;
f5fc0f86 3986
8bf29b0e 3987 if (do_join) {
3230f35e 3988 ret = wlcore_join(wl, wlvif);
8bf29b0e
JO
3989 if (ret < 0) {
3990 wl1271_warning("cmd join failed %d", ret);
e78a287a 3991 goto out;
8bf29b0e 3992 }
3230f35e
EP
3993 }
3994
3995 if (changed & BSS_CHANGED_ASSOC) {
3996 if (bss_conf->assoc) {
ec87011a
EP
3997 ret = wlcore_set_assoc(wl, wlvif, bss_conf,
3998 sta_rate_set);
251c177f
EP
3999 if (ret < 0)
4000 goto out;
3230f35e
EP
4001
4002 if (test_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags))
4003 wl12xx_set_authorized(wl, wlvif);
4004 } else {
4005 wlcore_unset_assoc(wl, wlvif);
251c177f 4006 }
c1899554
JO
4007 }
4008
0b932ab9 4009 /* Handle new association with HT. Do this after join. */
58321b29
EP
4010 if (sta_exists &&
4011 (changed & BSS_CHANGED_HT)) {
4012 bool enabled =
4013 bss_conf->channel_type != NL80211_CHAN_NO_HT;
4014
4015 ret = wl1271_acx_set_ht_capabilities(wl,
4016 &sta_ht_cap,
4017 enabled,
4018 wlvif->sta.hlid);
4019 if (ret < 0) {
4020 wl1271_warning("Set ht cap failed %d", ret);
4021 goto out;
4022
0f9c8250 4023 }
58321b29
EP
4024
4025 if (enabled) {
4026 ret = wl1271_acx_set_ht_information(wl, wlvif,
4027 bss_conf->ht_operation_mode);
0f9c8250 4028 if (ret < 0) {
58321b29 4029 wl1271_warning("Set ht information failed %d",
0f9c8250
AN
4030 ret);
4031 goto out;
4032 }
4033 }
4034 }
4035
76a74c8a
EP
4036 /* Handle arp filtering. Done after join. */
4037 if ((changed & BSS_CHANGED_ARP_FILTER) ||
4038 (!is_ibss && (changed & BSS_CHANGED_QOS))) {
4039 __be32 addr = bss_conf->arp_addr_list[0];
4040 wlvif->sta.qos = bss_conf->qos;
4041 WARN_ON(wlvif->bss_type != BSS_TYPE_STA_BSS);
4042
4043 if (bss_conf->arp_addr_cnt == 1 &&
4044 bss_conf->arp_filter_enabled) {
4045 wlvif->ip_addr = addr;
4046 /*
4047 * The template should have been configured only upon
4048 * association. however, it seems that the correct ip
4049 * isn't being set (when sending), so we have to
4050 * reconfigure the template upon every ip change.
4051 */
4052 ret = wl1271_cmd_build_arp_rsp(wl, wlvif);
4053 if (ret < 0) {
4054 wl1271_warning("build arp rsp failed: %d", ret);
4055 goto out;
4056 }
4057
4058 ret = wl1271_acx_arp_ip_filter(wl, wlvif,
4059 (ACX_ARP_FILTER_ARP_FILTERING |
4060 ACX_ARP_FILTER_AUTO_ARP),
4061 addr);
4062 } else {
4063 wlvif->ip_addr = 0;
4064 ret = wl1271_acx_arp_ip_filter(wl, wlvif, 0, addr);
4065 }
4066
4067 if (ret < 0)
4068 goto out;
4069 }
4070
e78a287a
AN
4071out:
4072 return;
4073}
4074
4075static void wl1271_op_bss_info_changed(struct ieee80211_hw *hw,
4076 struct ieee80211_vif *vif,
4077 struct ieee80211_bss_conf *bss_conf,
4078 u32 changed)
4079{
4080 struct wl1271 *wl = hw->priv;
536129c8
EP
4081 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4082 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
e78a287a
AN
4083 int ret;
4084
d3f5a1b5
EP
4085 wl1271_debug(DEBUG_MAC80211, "mac80211 bss info role %d changed 0x%x",
4086 wlvif->role_id, (int)changed);
e78a287a 4087
6b8bf5bc
AN
4088 /*
4089 * make sure to cancel pending disconnections if our association
4090 * state changed
4091 */
4092 if (!is_ap && (changed & BSS_CHANGED_ASSOC))
4093 cancel_delayed_work_sync(&wl->connection_loss_work);
4094
b515d83a
EP
4095 if (is_ap && (changed & BSS_CHANGED_BEACON_ENABLED) &&
4096 !bss_conf->enable_beacon)
4097 wl1271_tx_flush(wl);
4098
e78a287a
AN
4099 mutex_lock(&wl->mutex);
4100
4cc53383 4101 if (unlikely(wl->state != WLCORE_STATE_ON))
e78a287a
AN
4102 goto out;
4103
10c8cd01
EP
4104 if (unlikely(!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)))
4105 goto out;
4106
a620865e 4107 ret = wl1271_ps_elp_wakeup(wl);
e78a287a
AN
4108 if (ret < 0)
4109 goto out;
4110
4111 if (is_ap)
4112 wl1271_bss_info_changed_ap(wl, vif, bss_conf, changed);
4113 else
4114 wl1271_bss_info_changed_sta(wl, vif, bss_conf, changed);
4115
f5fc0f86
LC
4116 wl1271_ps_elp_sleep(wl);
4117
4118out:
4119 mutex_unlock(&wl->mutex);
4120}
4121
b6970ee5
EP
4122static int wlcore_op_add_chanctx(struct ieee80211_hw *hw,
4123 struct ieee80211_chanctx_conf *ctx)
4124{
4125 wl1271_debug(DEBUG_MAC80211, "mac80211 add chanctx %d (type %d)",
4126 ieee80211_frequency_to_channel(ctx->channel->center_freq),
4127 ctx->channel_type);
4128 return 0;
4129}
4130
4131static void wlcore_op_remove_chanctx(struct ieee80211_hw *hw,
4132 struct ieee80211_chanctx_conf *ctx)
4133{
4134 wl1271_debug(DEBUG_MAC80211, "mac80211 remove chanctx %d (type %d)",
4135 ieee80211_frequency_to_channel(ctx->channel->center_freq),
4136 ctx->channel_type);
4137}
4138
4139static void wlcore_op_change_chanctx(struct ieee80211_hw *hw,
4140 struct ieee80211_chanctx_conf *ctx,
4141 u32 changed)
4142{
4143 wl1271_debug(DEBUG_MAC80211,
4144 "mac80211 change chanctx %d (type %d) changed 0x%x",
4145 ieee80211_frequency_to_channel(ctx->channel->center_freq),
4146 ctx->channel_type, changed);
4147}
4148
4149static int wlcore_op_assign_vif_chanctx(struct ieee80211_hw *hw,
4150 struct ieee80211_vif *vif,
4151 struct ieee80211_chanctx_conf *ctx)
4152{
4153 struct wl1271 *wl = hw->priv;
4154 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4155 int channel = ieee80211_frequency_to_channel(
4156 ctx->channel->center_freq);
4157
4158 wl1271_debug(DEBUG_MAC80211,
4159 "mac80211 assign chanctx (role %d) %d (type %d)",
4160 wlvif->role_id, channel, ctx->channel_type);
4161
4162 mutex_lock(&wl->mutex);
4163
4164 wlvif->band = ctx->channel->band;
4165 wlvif->channel = channel;
4166 wlvif->channel_type = ctx->channel_type;
4167
4168 /* update default rates according to the band */
4169 wl1271_set_band_rate(wl, wlvif);
4170
4171 mutex_unlock(&wl->mutex);
4172
4173 return 0;
4174}
4175
4176static void wlcore_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
4177 struct ieee80211_vif *vif,
4178 struct ieee80211_chanctx_conf *ctx)
4179{
4180 struct wl1271 *wl = hw->priv;
4181 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4182
4183 wl1271_debug(DEBUG_MAC80211,
4184 "mac80211 unassign chanctx (role %d) %d (type %d)",
4185 wlvif->role_id,
4186 ieee80211_frequency_to_channel(ctx->channel->center_freq),
4187 ctx->channel_type);
4188
4189 wl1271_tx_flush(wl);
4190}
4191
8a3a3c85
EP
4192static int wl1271_op_conf_tx(struct ieee80211_hw *hw,
4193 struct ieee80211_vif *vif, u16 queue,
c6999d83
KV
4194 const struct ieee80211_tx_queue_params *params)
4195{
4196 struct wl1271 *wl = hw->priv;
0603d891 4197 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4695dc91 4198 u8 ps_scheme;
488fc540 4199 int ret = 0;
c6999d83
KV
4200
4201 mutex_lock(&wl->mutex);
4202
4203 wl1271_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
4204
4695dc91
KV
4205 if (params->uapsd)
4206 ps_scheme = CONF_PS_SCHEME_UPSD_TRIGGER;
4207 else
4208 ps_scheme = CONF_PS_SCHEME_LEGACY;
4209
5b37ddfe 4210 if (!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
c1b193eb 4211 goto out;
488fc540 4212
c1b193eb
EP
4213 ret = wl1271_ps_elp_wakeup(wl);
4214 if (ret < 0)
4215 goto out;
488fc540 4216
c1b193eb
EP
4217 /*
4218 * the txop is confed in units of 32us by the mac80211,
4219 * we need us
4220 */
0603d891 4221 ret = wl1271_acx_ac_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
c1b193eb
EP
4222 params->cw_min, params->cw_max,
4223 params->aifs, params->txop << 5);
4224 if (ret < 0)
4225 goto out_sleep;
4226
0603d891 4227 ret = wl1271_acx_tid_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
c1b193eb
EP
4228 CONF_CHANNEL_TYPE_EDCF,
4229 wl1271_tx_get_queue(queue),
4230 ps_scheme, CONF_ACK_POLICY_LEGACY,
4231 0, 0);
c82c1dde
KV
4232
4233out_sleep:
c1b193eb 4234 wl1271_ps_elp_sleep(wl);
c6999d83
KV
4235
4236out:
4237 mutex_unlock(&wl->mutex);
4238
4239 return ret;
4240}
4241
37a41b4a
EP
4242static u64 wl1271_op_get_tsf(struct ieee80211_hw *hw,
4243 struct ieee80211_vif *vif)
bbbb538e
JO
4244{
4245
4246 struct wl1271 *wl = hw->priv;
9c531149 4247 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
bbbb538e
JO
4248 u64 mactime = ULLONG_MAX;
4249 int ret;
4250
4251 wl1271_debug(DEBUG_MAC80211, "mac80211 get tsf");
4252
4253 mutex_lock(&wl->mutex);
4254
4cc53383 4255 if (unlikely(wl->state != WLCORE_STATE_ON))
f8d9802f
JO
4256 goto out;
4257
a620865e 4258 ret = wl1271_ps_elp_wakeup(wl);
bbbb538e
JO
4259 if (ret < 0)
4260 goto out;
4261
9c531149 4262 ret = wl12xx_acx_tsf_info(wl, wlvif, &mactime);
bbbb538e
JO
4263 if (ret < 0)
4264 goto out_sleep;
4265
4266out_sleep:
4267 wl1271_ps_elp_sleep(wl);
4268
4269out:
4270 mutex_unlock(&wl->mutex);
4271 return mactime;
4272}
f5fc0f86 4273
ece550d0
JL
4274static int wl1271_op_get_survey(struct ieee80211_hw *hw, int idx,
4275 struct survey_info *survey)
4276{
ece550d0 4277 struct ieee80211_conf *conf = &hw->conf;
b739a42c 4278
ece550d0
JL
4279 if (idx != 0)
4280 return -ENOENT;
b739a42c 4281
ece550d0 4282 survey->channel = conf->channel;
add779a0 4283 survey->filled = 0;
ece550d0
JL
4284 return 0;
4285}
4286
409622ec 4287static int wl1271_allocate_sta(struct wl1271 *wl,
c7ffb902
EP
4288 struct wl12xx_vif *wlvif,
4289 struct ieee80211_sta *sta)
f84f7d78
AN
4290{
4291 struct wl1271_station *wl_sta;
c7ffb902 4292 int ret;
f84f7d78 4293
c7ffb902
EP
4294
4295 if (wl->active_sta_count >= AP_MAX_STATIONS) {
f84f7d78
AN
4296 wl1271_warning("could not allocate HLID - too much stations");
4297 return -EBUSY;
4298 }
4299
4300 wl_sta = (struct wl1271_station *)sta->drv_priv;
c7ffb902
EP
4301 ret = wl12xx_allocate_link(wl, wlvif, &wl_sta->hlid);
4302 if (ret < 0) {
4303 wl1271_warning("could not allocate HLID - too many links");
4304 return -EBUSY;
4305 }
4306
4307 set_bit(wl_sta->hlid, wlvif->ap.sta_hlid_map);
b622d992 4308 memcpy(wl->links[wl_sta->hlid].addr, sta->addr, ETH_ALEN);
da03209e 4309 wl->active_sta_count++;
f84f7d78
AN
4310 return 0;
4311}
4312
c7ffb902 4313void wl1271_free_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 hlid)
f84f7d78 4314{
c7ffb902 4315 if (!test_bit(hlid, wlvif->ap.sta_hlid_map))
f1acea9a
AN
4316 return;
4317
c7ffb902 4318 clear_bit(hlid, wlvif->ap.sta_hlid_map);
b622d992 4319 memset(wl->links[hlid].addr, 0, ETH_ALEN);
0f9c8250 4320 wl->links[hlid].ba_bitmap = 0;
b622d992
AN
4321 __clear_bit(hlid, &wl->ap_ps_map);
4322 __clear_bit(hlid, (unsigned long *)&wl->ap_fw_ps_map);
c7ffb902 4323 wl12xx_free_link(wl, wlvif, &hlid);
da03209e 4324 wl->active_sta_count--;
55df5afb
AN
4325
4326 /*
4327 * rearm the tx watchdog when the last STA is freed - give the FW a
4328 * chance to return STA-buffered packets before complaining.
4329 */
4330 if (wl->active_sta_count == 0)
4331 wl12xx_rearm_tx_watchdog_locked(wl);
f84f7d78
AN
4332}
4333
2d6cf2b5
EP
4334static int wl12xx_sta_add(struct wl1271 *wl,
4335 struct wl12xx_vif *wlvif,
4336 struct ieee80211_sta *sta)
f84f7d78 4337{
c7ffb902 4338 struct wl1271_station *wl_sta;
f84f7d78
AN
4339 int ret = 0;
4340 u8 hlid;
4341
f84f7d78
AN
4342 wl1271_debug(DEBUG_MAC80211, "mac80211 add sta %d", (int)sta->aid);
4343
c7ffb902 4344 ret = wl1271_allocate_sta(wl, wlvif, sta);
f84f7d78 4345 if (ret < 0)
2d6cf2b5 4346 return ret;
f84f7d78 4347
c7ffb902
EP
4348 wl_sta = (struct wl1271_station *)sta->drv_priv;
4349 hlid = wl_sta->hlid;
4350
1b92f15e 4351 ret = wl12xx_cmd_add_peer(wl, wlvif, sta, hlid);
f84f7d78 4352 if (ret < 0)
2d6cf2b5 4353 wl1271_free_sta(wl, wlvif, hlid);
f84f7d78 4354
2d6cf2b5
EP
4355 return ret;
4356}
b67476ef 4357
2d6cf2b5
EP
4358static int wl12xx_sta_remove(struct wl1271 *wl,
4359 struct wl12xx_vif *wlvif,
4360 struct ieee80211_sta *sta)
4361{
4362 struct wl1271_station *wl_sta;
4363 int ret = 0, id;
0b932ab9 4364
2d6cf2b5
EP
4365 wl1271_debug(DEBUG_MAC80211, "mac80211 remove sta %d", (int)sta->aid);
4366
4367 wl_sta = (struct wl1271_station *)sta->drv_priv;
4368 id = wl_sta->hlid;
4369 if (WARN_ON(!test_bit(id, wlvif->ap.sta_hlid_map)))
4370 return -EINVAL;
f84f7d78 4371
2d6cf2b5 4372 ret = wl12xx_cmd_remove_peer(wl, wl_sta->hlid);
409622ec 4373 if (ret < 0)
2d6cf2b5 4374 return ret;
409622ec 4375
2d6cf2b5 4376 wl1271_free_sta(wl, wlvif, wl_sta->hlid);
f84f7d78
AN
4377 return ret;
4378}
4379
2d6cf2b5
EP
4380static int wl12xx_update_sta_state(struct wl1271 *wl,
4381 struct wl12xx_vif *wlvif,
4382 struct ieee80211_sta *sta,
4383 enum ieee80211_sta_state old_state,
4384 enum ieee80211_sta_state new_state)
f84f7d78 4385{
f84f7d78 4386 struct wl1271_station *wl_sta;
2d6cf2b5
EP
4387 u8 hlid;
4388 bool is_ap = wlvif->bss_type == BSS_TYPE_AP_BSS;
4389 bool is_sta = wlvif->bss_type == BSS_TYPE_STA_BSS;
4390 int ret;
f84f7d78 4391
2d6cf2b5
EP
4392 wl_sta = (struct wl1271_station *)sta->drv_priv;
4393 hlid = wl_sta->hlid;
f84f7d78 4394
2d6cf2b5
EP
4395 /* Add station (AP mode) */
4396 if (is_ap &&
4397 old_state == IEEE80211_STA_NOTEXIST &&
29936266
EP
4398 new_state == IEEE80211_STA_NONE) {
4399 ret = wl12xx_sta_add(wl, wlvif, sta);
4400 if (ret)
4401 return ret;
4402 }
2d6cf2b5
EP
4403
4404 /* Remove station (AP mode) */
4405 if (is_ap &&
4406 old_state == IEEE80211_STA_NONE &&
4407 new_state == IEEE80211_STA_NOTEXIST) {
4408 /* must not fail */
4409 wl12xx_sta_remove(wl, wlvif, sta);
2d6cf2b5 4410 }
f84f7d78 4411
2d6cf2b5
EP
4412 /* Authorize station (AP mode) */
4413 if (is_ap &&
4414 new_state == IEEE80211_STA_AUTHORIZED) {
4415 ret = wl12xx_cmd_set_peer_state(wl, hlid);
4416 if (ret < 0)
4417 return ret;
f84f7d78 4418
2d6cf2b5
EP
4419 ret = wl1271_acx_set_ht_capabilities(wl, &sta->ht_cap, true,
4420 hlid);
29936266
EP
4421 if (ret)
4422 return ret;
2d6cf2b5 4423 }
f84f7d78 4424
9fd6f21b
EP
4425 /* Authorize station */
4426 if (is_sta &&
4427 new_state == IEEE80211_STA_AUTHORIZED) {
4428 set_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags);
29936266
EP
4429 ret = wl12xx_set_authorized(wl, wlvif);
4430 if (ret)
4431 return ret;
9fd6f21b
EP
4432 }
4433
4434 if (is_sta &&
4435 old_state == IEEE80211_STA_AUTHORIZED &&
4436 new_state == IEEE80211_STA_ASSOC) {
4437 clear_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags);
3230f35e 4438 clear_bit(WLVIF_FLAG_STA_STATE_SENT, &wlvif->flags);
9fd6f21b
EP
4439 }
4440
29936266
EP
4441 /* clear ROCs on failure or authorization */
4442 if (is_sta &&
4443 (new_state == IEEE80211_STA_AUTHORIZED ||
4444 new_state == IEEE80211_STA_NOTEXIST)) {
4445 if (test_bit(wlvif->role_id, wl->roc_map))
4446 wl12xx_croc(wl, wlvif->role_id);
4447 }
4448
4449 if (is_sta &&
4450 old_state == IEEE80211_STA_NOTEXIST &&
4451 new_state == IEEE80211_STA_NONE) {
4452 if (find_first_bit(wl->roc_map,
4453 WL12XX_MAX_ROLES) >= WL12XX_MAX_ROLES) {
4454 WARN_ON(wlvif->role_id == WL12XX_INVALID_ROLE_ID);
4455 wl12xx_roc(wl, wlvif, wlvif->role_id,
4456 wlvif->band, wlvif->channel);
4457 }
4458 }
2d6cf2b5
EP
4459 return 0;
4460}
4461
4462static int wl12xx_op_sta_state(struct ieee80211_hw *hw,
4463 struct ieee80211_vif *vif,
4464 struct ieee80211_sta *sta,
4465 enum ieee80211_sta_state old_state,
4466 enum ieee80211_sta_state new_state)
4467{
4468 struct wl1271 *wl = hw->priv;
4469 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4470 int ret;
4471
4472 wl1271_debug(DEBUG_MAC80211, "mac80211 sta %d state=%d->%d",
4473 sta->aid, old_state, new_state);
4474
4475 mutex_lock(&wl->mutex);
4476
4cc53383 4477 if (unlikely(wl->state != WLCORE_STATE_ON)) {
2d6cf2b5 4478 ret = -EBUSY;
f84f7d78 4479 goto out;
2d6cf2b5 4480 }
f84f7d78 4481
a620865e 4482 ret = wl1271_ps_elp_wakeup(wl);
f84f7d78
AN
4483 if (ret < 0)
4484 goto out;
4485
2d6cf2b5 4486 ret = wl12xx_update_sta_state(wl, wlvif, sta, old_state, new_state);
f84f7d78 4487
f84f7d78 4488 wl1271_ps_elp_sleep(wl);
f84f7d78
AN
4489out:
4490 mutex_unlock(&wl->mutex);
2d6cf2b5
EP
4491 if (new_state < old_state)
4492 return 0;
f84f7d78
AN
4493 return ret;
4494}
4495
4623ec7d
LC
4496static int wl1271_op_ampdu_action(struct ieee80211_hw *hw,
4497 struct ieee80211_vif *vif,
4498 enum ieee80211_ampdu_mlme_action action,
4499 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
4500 u8 buf_size)
bbba3e68
LS
4501{
4502 struct wl1271 *wl = hw->priv;
536129c8 4503 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
bbba3e68 4504 int ret;
0f9c8250
AN
4505 u8 hlid, *ba_bitmap;
4506
4507 wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu action %d tid %d", action,
4508 tid);
4509
4510 /* sanity check - the fields in FW are only 8bits wide */
4511 if (WARN_ON(tid > 0xFF))
4512 return -ENOTSUPP;
bbba3e68
LS
4513
4514 mutex_lock(&wl->mutex);
4515
4cc53383 4516 if (unlikely(wl->state != WLCORE_STATE_ON)) {
bbba3e68
LS
4517 ret = -EAGAIN;
4518 goto out;
4519 }
4520
536129c8 4521 if (wlvif->bss_type == BSS_TYPE_STA_BSS) {
154da67c 4522 hlid = wlvif->sta.hlid;
d0802abd 4523 ba_bitmap = &wlvif->sta.ba_rx_bitmap;
536129c8 4524 } else if (wlvif->bss_type == BSS_TYPE_AP_BSS) {
0f9c8250
AN
4525 struct wl1271_station *wl_sta;
4526
4527 wl_sta = (struct wl1271_station *)sta->drv_priv;
4528 hlid = wl_sta->hlid;
4529 ba_bitmap = &wl->links[hlid].ba_bitmap;
4530 } else {
4531 ret = -EINVAL;
4532 goto out;
4533 }
4534
a620865e 4535 ret = wl1271_ps_elp_wakeup(wl);
bbba3e68
LS
4536 if (ret < 0)
4537 goto out;
4538
70559a06
SL
4539 wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu: Rx tid %d action %d",
4540 tid, action);
4541
bbba3e68
LS
4542 switch (action) {
4543 case IEEE80211_AMPDU_RX_START:
d0802abd 4544 if (!wlvif->ba_support || !wlvif->ba_allowed) {
bbba3e68 4545 ret = -ENOTSUPP;
0f9c8250
AN
4546 break;
4547 }
4548
4549 if (wl->ba_rx_session_count >= RX_BA_MAX_SESSIONS) {
4550 ret = -EBUSY;
4551 wl1271_error("exceeded max RX BA sessions");
4552 break;
4553 }
4554
4555 if (*ba_bitmap & BIT(tid)) {
4556 ret = -EINVAL;
4557 wl1271_error("cannot enable RX BA session on active "
4558 "tid: %d", tid);
4559 break;
4560 }
4561
4562 ret = wl12xx_acx_set_ba_receiver_session(wl, tid, *ssn, true,
4563 hlid);
4564 if (!ret) {
4565 *ba_bitmap |= BIT(tid);
4566 wl->ba_rx_session_count++;
bbba3e68
LS
4567 }
4568 break;
4569
4570 case IEEE80211_AMPDU_RX_STOP:
0f9c8250 4571 if (!(*ba_bitmap & BIT(tid))) {
c954910b
AN
4572 /*
4573 * this happens on reconfig - so only output a debug
4574 * message for now, and don't fail the function.
4575 */
4576 wl1271_debug(DEBUG_MAC80211,
4577 "no active RX BA session on tid: %d",
0f9c8250 4578 tid);
c954910b 4579 ret = 0;
0f9c8250
AN
4580 break;
4581 }
4582
4583 ret = wl12xx_acx_set_ba_receiver_session(wl, tid, 0, false,
4584 hlid);
4585 if (!ret) {
4586 *ba_bitmap &= ~BIT(tid);
4587 wl->ba_rx_session_count--;
4588 }
bbba3e68
LS
4589 break;
4590
4591 /*
4592 * The BA initiator session management in FW independently.
4593 * Falling break here on purpose for all TX APDU commands.
4594 */
4595 case IEEE80211_AMPDU_TX_START:
4596 case IEEE80211_AMPDU_TX_STOP:
4597 case IEEE80211_AMPDU_TX_OPERATIONAL:
4598 ret = -EINVAL;
4599 break;
4600
4601 default:
4602 wl1271_error("Incorrect ampdu action id=%x\n", action);
4603 ret = -EINVAL;
4604 }
4605
4606 wl1271_ps_elp_sleep(wl);
4607
4608out:
4609 mutex_unlock(&wl->mutex);
4610
4611 return ret;
4612}
4613
af7fbb28
EP
4614static int wl12xx_set_bitrate_mask(struct ieee80211_hw *hw,
4615 struct ieee80211_vif *vif,
4616 const struct cfg80211_bitrate_mask *mask)
4617{
83587505 4618 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
af7fbb28 4619 struct wl1271 *wl = hw->priv;
d6fa37c9 4620 int i, ret = 0;
af7fbb28
EP
4621
4622 wl1271_debug(DEBUG_MAC80211, "mac80211 set_bitrate_mask 0x%x 0x%x",
4623 mask->control[NL80211_BAND_2GHZ].legacy,
4624 mask->control[NL80211_BAND_5GHZ].legacy);
4625
4626 mutex_lock(&wl->mutex);
4627
091185d6 4628 for (i = 0; i < WLCORE_NUM_BANDS; i++)
83587505 4629 wlvif->bitrate_masks[i] =
af7fbb28
EP
4630 wl1271_tx_enabled_rates_get(wl,
4631 mask->control[i].legacy,
4632 i);
d6fa37c9 4633
4cc53383 4634 if (unlikely(wl->state != WLCORE_STATE_ON))
d6fa37c9
EP
4635 goto out;
4636
4637 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
4638 !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
4639
4640 ret = wl1271_ps_elp_wakeup(wl);
4641 if (ret < 0)
4642 goto out;
4643
4644 wl1271_set_band_rate(wl, wlvif);
4645 wlvif->basic_rate =
4646 wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
4647 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
4648
4649 wl1271_ps_elp_sleep(wl);
4650 }
4651out:
af7fbb28
EP
4652 mutex_unlock(&wl->mutex);
4653
d6fa37c9 4654 return ret;
af7fbb28
EP
4655}
4656
6d158ff3
SL
4657static void wl12xx_op_channel_switch(struct ieee80211_hw *hw,
4658 struct ieee80211_channel_switch *ch_switch)
4659{
4660 struct wl1271 *wl = hw->priv;
52630c5d 4661 struct wl12xx_vif *wlvif;
6d158ff3
SL
4662 int ret;
4663
4664 wl1271_debug(DEBUG_MAC80211, "mac80211 channel switch");
4665
b9239b66
AN
4666 wl1271_tx_flush(wl);
4667
6d158ff3
SL
4668 mutex_lock(&wl->mutex);
4669
4cc53383 4670 if (unlikely(wl->state == WLCORE_STATE_OFF)) {
6e8cd331
EP
4671 wl12xx_for_each_wlvif_sta(wl, wlvif) {
4672 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
4673 ieee80211_chswitch_done(vif, false);
4674 }
4675 goto out;
4cc53383
IY
4676 } else if (unlikely(wl->state != WLCORE_STATE_ON)) {
4677 goto out;
6d158ff3
SL
4678 }
4679
4680 ret = wl1271_ps_elp_wakeup(wl);
4681 if (ret < 0)
4682 goto out;
4683
52630c5d
EP
4684 /* TODO: change mac80211 to pass vif as param */
4685 wl12xx_for_each_wlvif_sta(wl, wlvif) {
8332f0f6 4686 ret = wl12xx_cmd_channel_switch(wl, wlvif, ch_switch);
6d158ff3 4687
52630c5d
EP
4688 if (!ret)
4689 set_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags);
4690 }
6d158ff3
SL
4691
4692 wl1271_ps_elp_sleep(wl);
4693
4694out:
4695 mutex_unlock(&wl->mutex);
4696}
4697
d8ae5a25
EP
4698static void wlcore_op_flush(struct ieee80211_hw *hw, bool drop)
4699{
4700 struct wl1271 *wl = hw->priv;
4701
4702 wl1271_tx_flush(wl);
4703}
4704
dabf37db
EP
4705static int wlcore_op_remain_on_channel(struct ieee80211_hw *hw,
4706 struct ieee80211_vif *vif,
4707 struct ieee80211_channel *chan,
4708 enum nl80211_channel_type channel_type,
4709 int duration)
4710{
4711 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4712 struct wl1271 *wl = hw->priv;
4713 int channel, ret = 0;
4714
4715 channel = ieee80211_frequency_to_channel(chan->center_freq);
4716
4717 wl1271_debug(DEBUG_MAC80211, "mac80211 roc %d (%d)",
4718 channel, wlvif->role_id);
4719
4720 mutex_lock(&wl->mutex);
4721
4722 if (unlikely(wl->state != WLCORE_STATE_ON))
4723 goto out;
4724
4725 /* return EBUSY if we can't ROC right now */
4726 if (WARN_ON(wl->roc_vif ||
4727 find_first_bit(wl->roc_map,
4728 WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES)) {
4729 ret = -EBUSY;
4730 goto out;
4731 }
4732
4733 ret = wl1271_ps_elp_wakeup(wl);
4734 if (ret < 0)
4735 goto out;
4736
4737 ret = wl12xx_start_dev(wl, wlvif, chan->band, channel);
4738 if (ret < 0)
4739 goto out_sleep;
4740
4741 wl->roc_vif = vif;
4742 ieee80211_queue_delayed_work(hw, &wl->roc_complete_work,
4743 msecs_to_jiffies(duration));
4744out_sleep:
4745 wl1271_ps_elp_sleep(wl);
4746out:
4747 mutex_unlock(&wl->mutex);
4748 return ret;
4749}
4750
4751static int __wlcore_roc_completed(struct wl1271 *wl)
4752{
4753 struct wl12xx_vif *wlvif;
4754 int ret;
4755
4756 /* already completed */
4757 if (unlikely(!wl->roc_vif))
4758 return 0;
4759
4760 wlvif = wl12xx_vif_to_data(wl->roc_vif);
4761
4762 if (!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
4763 return -EBUSY;
4764
4765 ret = wl12xx_stop_dev(wl, wlvif);
4766 if (ret < 0)
4767 return ret;
4768
4769 wl->roc_vif = NULL;
4770
4771 return 0;
4772}
4773
4774static int wlcore_roc_completed(struct wl1271 *wl)
4775{
4776 int ret;
4777
4778 wl1271_debug(DEBUG_MAC80211, "roc complete");
4779
4780 mutex_lock(&wl->mutex);
4781
4782 if (unlikely(wl->state != WLCORE_STATE_ON)) {
4783 ret = -EBUSY;
4784 goto out;
4785 }
4786
4787 ret = wl1271_ps_elp_wakeup(wl);
4788 if (ret < 0)
4789 goto out;
4790
4791 ret = __wlcore_roc_completed(wl);
4792
4793 wl1271_ps_elp_sleep(wl);
4794out:
4795 mutex_unlock(&wl->mutex);
4796
4797 return ret;
4798}
4799
4800static void wlcore_roc_complete_work(struct work_struct *work)
4801{
4802 struct delayed_work *dwork;
4803 struct wl1271 *wl;
4804 int ret;
4805
4806 dwork = container_of(work, struct delayed_work, work);
4807 wl = container_of(dwork, struct wl1271, roc_complete_work);
4808
4809 ret = wlcore_roc_completed(wl);
4810 if (!ret)
4811 ieee80211_remain_on_channel_expired(wl->hw);
4812}
4813
4814static int wlcore_op_cancel_remain_on_channel(struct ieee80211_hw *hw)
4815{
4816 struct wl1271 *wl = hw->priv;
4817
4818 wl1271_debug(DEBUG_MAC80211, "mac80211 croc");
4819
4820 /* TODO: per-vif */
4821 wl1271_tx_flush(wl);
4822
4823 /*
4824 * we can't just flush_work here, because it might deadlock
4825 * (as we might get called from the same workqueue)
4826 */
4827 cancel_delayed_work_sync(&wl->roc_complete_work);
4828 wlcore_roc_completed(wl);
4829
4830 return 0;
4831}
4832
33437893
AN
4833static bool wl1271_tx_frames_pending(struct ieee80211_hw *hw)
4834{
4835 struct wl1271 *wl = hw->priv;
4836 bool ret = false;
4837
4838 mutex_lock(&wl->mutex);
4839
4cc53383 4840 if (unlikely(wl->state != WLCORE_STATE_ON))
33437893
AN
4841 goto out;
4842
4843 /* packets are considered pending if in the TX queue or the FW */
f1a46384 4844 ret = (wl1271_tx_total_queue_count(wl) > 0) || (wl->tx_frames_cnt > 0);
33437893
AN
4845out:
4846 mutex_unlock(&wl->mutex);
4847
4848 return ret;
4849}
4850
f5fc0f86
LC
4851/* can't be const, mac80211 writes to this */
4852static struct ieee80211_rate wl1271_rates[] = {
4853 { .bitrate = 10,
2b60100b
JO
4854 .hw_value = CONF_HW_BIT_RATE_1MBPS,
4855 .hw_value_short = CONF_HW_BIT_RATE_1MBPS, },
f5fc0f86 4856 { .bitrate = 20,
2b60100b
JO
4857 .hw_value = CONF_HW_BIT_RATE_2MBPS,
4858 .hw_value_short = CONF_HW_BIT_RATE_2MBPS,
f5fc0f86
LC
4859 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
4860 { .bitrate = 55,
2b60100b
JO
4861 .hw_value = CONF_HW_BIT_RATE_5_5MBPS,
4862 .hw_value_short = CONF_HW_BIT_RATE_5_5MBPS,
f5fc0f86
LC
4863 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
4864 { .bitrate = 110,
2b60100b
JO
4865 .hw_value = CONF_HW_BIT_RATE_11MBPS,
4866 .hw_value_short = CONF_HW_BIT_RATE_11MBPS,
f5fc0f86
LC
4867 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
4868 { .bitrate = 60,
2b60100b
JO
4869 .hw_value = CONF_HW_BIT_RATE_6MBPS,
4870 .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
f5fc0f86 4871 { .bitrate = 90,
2b60100b
JO
4872 .hw_value = CONF_HW_BIT_RATE_9MBPS,
4873 .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
f5fc0f86 4874 { .bitrate = 120,
2b60100b
JO
4875 .hw_value = CONF_HW_BIT_RATE_12MBPS,
4876 .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
f5fc0f86 4877 { .bitrate = 180,
2b60100b
JO
4878 .hw_value = CONF_HW_BIT_RATE_18MBPS,
4879 .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
f5fc0f86 4880 { .bitrate = 240,
2b60100b
JO
4881 .hw_value = CONF_HW_BIT_RATE_24MBPS,
4882 .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
f5fc0f86 4883 { .bitrate = 360,
2b60100b
JO
4884 .hw_value = CONF_HW_BIT_RATE_36MBPS,
4885 .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
f5fc0f86 4886 { .bitrate = 480,
2b60100b
JO
4887 .hw_value = CONF_HW_BIT_RATE_48MBPS,
4888 .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
f5fc0f86 4889 { .bitrate = 540,
2b60100b
JO
4890 .hw_value = CONF_HW_BIT_RATE_54MBPS,
4891 .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
f5fc0f86
LC
4892};
4893
fa97f46b 4894/* can't be const, mac80211 writes to this */
f5fc0f86 4895static struct ieee80211_channel wl1271_channels[] = {
a2d0e3f1 4896 { .hw_value = 1, .center_freq = 2412, .max_power = 25 },
fa21c7a9 4897 { .hw_value = 2, .center_freq = 2417, .max_power = 25 },
fa97f46b
JO
4898 { .hw_value = 3, .center_freq = 2422, .max_power = 25 },
4899 { .hw_value = 4, .center_freq = 2427, .max_power = 25 },
4900 { .hw_value = 5, .center_freq = 2432, .max_power = 25 },
fa21c7a9 4901 { .hw_value = 6, .center_freq = 2437, .max_power = 25 },
fa97f46b
JO
4902 { .hw_value = 7, .center_freq = 2442, .max_power = 25 },
4903 { .hw_value = 8, .center_freq = 2447, .max_power = 25 },
4904 { .hw_value = 9, .center_freq = 2452, .max_power = 25 },
fa21c7a9 4905 { .hw_value = 10, .center_freq = 2457, .max_power = 25 },
fa97f46b
JO
4906 { .hw_value = 11, .center_freq = 2462, .max_power = 25 },
4907 { .hw_value = 12, .center_freq = 2467, .max_power = 25 },
4908 { .hw_value = 13, .center_freq = 2472, .max_power = 25 },
6c89b7b2 4909 { .hw_value = 14, .center_freq = 2484, .max_power = 25 },
f5fc0f86
LC
4910};
4911
4912/* can't be const, mac80211 writes to this */
4913static struct ieee80211_supported_band wl1271_band_2ghz = {
4914 .channels = wl1271_channels,
4915 .n_channels = ARRAY_SIZE(wl1271_channels),
4916 .bitrates = wl1271_rates,
4917 .n_bitrates = ARRAY_SIZE(wl1271_rates),
4918};
4919
1ebec3d7
TP
4920/* 5 GHz data rates for WL1273 */
4921static struct ieee80211_rate wl1271_rates_5ghz[] = {
4922 { .bitrate = 60,
4923 .hw_value = CONF_HW_BIT_RATE_6MBPS,
4924 .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
4925 { .bitrate = 90,
4926 .hw_value = CONF_HW_BIT_RATE_9MBPS,
4927 .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
4928 { .bitrate = 120,
4929 .hw_value = CONF_HW_BIT_RATE_12MBPS,
4930 .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
4931 { .bitrate = 180,
4932 .hw_value = CONF_HW_BIT_RATE_18MBPS,
4933 .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
4934 { .bitrate = 240,
4935 .hw_value = CONF_HW_BIT_RATE_24MBPS,
4936 .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
4937 { .bitrate = 360,
4938 .hw_value = CONF_HW_BIT_RATE_36MBPS,
4939 .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
4940 { .bitrate = 480,
4941 .hw_value = CONF_HW_BIT_RATE_48MBPS,
4942 .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
4943 { .bitrate = 540,
4944 .hw_value = CONF_HW_BIT_RATE_54MBPS,
4945 .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
4946};
4947
fa97f46b 4948/* 5 GHz band channels for WL1273 */
1ebec3d7 4949static struct ieee80211_channel wl1271_channels_5ghz[] = {
6cfa5cff
AN
4950 { .hw_value = 7, .center_freq = 5035, .max_power = 25 },
4951 { .hw_value = 8, .center_freq = 5040, .max_power = 25 },
4952 { .hw_value = 9, .center_freq = 5045, .max_power = 25 },
4953 { .hw_value = 11, .center_freq = 5055, .max_power = 25 },
4954 { .hw_value = 12, .center_freq = 5060, .max_power = 25 },
4955 { .hw_value = 16, .center_freq = 5080, .max_power = 25 },
4956 { .hw_value = 34, .center_freq = 5170, .max_power = 25 },
4957 { .hw_value = 36, .center_freq = 5180, .max_power = 25 },
4958 { .hw_value = 38, .center_freq = 5190, .max_power = 25 },
4959 { .hw_value = 40, .center_freq = 5200, .max_power = 25 },
4960 { .hw_value = 42, .center_freq = 5210, .max_power = 25 },
4961 { .hw_value = 44, .center_freq = 5220, .max_power = 25 },
4962 { .hw_value = 46, .center_freq = 5230, .max_power = 25 },
4963 { .hw_value = 48, .center_freq = 5240, .max_power = 25 },
4964 { .hw_value = 52, .center_freq = 5260, .max_power = 25 },
4965 { .hw_value = 56, .center_freq = 5280, .max_power = 25 },
4966 { .hw_value = 60, .center_freq = 5300, .max_power = 25 },
4967 { .hw_value = 64, .center_freq = 5320, .max_power = 25 },
4968 { .hw_value = 100, .center_freq = 5500, .max_power = 25 },
4969 { .hw_value = 104, .center_freq = 5520, .max_power = 25 },
4970 { .hw_value = 108, .center_freq = 5540, .max_power = 25 },
4971 { .hw_value = 112, .center_freq = 5560, .max_power = 25 },
4972 { .hw_value = 116, .center_freq = 5580, .max_power = 25 },
4973 { .hw_value = 120, .center_freq = 5600, .max_power = 25 },
4974 { .hw_value = 124, .center_freq = 5620, .max_power = 25 },
4975 { .hw_value = 128, .center_freq = 5640, .max_power = 25 },
4976 { .hw_value = 132, .center_freq = 5660, .max_power = 25 },
4977 { .hw_value = 136, .center_freq = 5680, .max_power = 25 },
4978 { .hw_value = 140, .center_freq = 5700, .max_power = 25 },
4979 { .hw_value = 149, .center_freq = 5745, .max_power = 25 },
4980 { .hw_value = 153, .center_freq = 5765, .max_power = 25 },
4981 { .hw_value = 157, .center_freq = 5785, .max_power = 25 },
4982 { .hw_value = 161, .center_freq = 5805, .max_power = 25 },
4983 { .hw_value = 165, .center_freq = 5825, .max_power = 25 },
1ebec3d7
TP
4984};
4985
1ebec3d7
TP
4986static struct ieee80211_supported_band wl1271_band_5ghz = {
4987 .channels = wl1271_channels_5ghz,
4988 .n_channels = ARRAY_SIZE(wl1271_channels_5ghz),
4989 .bitrates = wl1271_rates_5ghz,
4990 .n_bitrates = ARRAY_SIZE(wl1271_rates_5ghz),
f876bb9a
JO
4991};
4992
f5fc0f86
LC
4993static const struct ieee80211_ops wl1271_ops = {
4994 .start = wl1271_op_start,
c24ec83b 4995 .stop = wlcore_op_stop,
f5fc0f86
LC
4996 .add_interface = wl1271_op_add_interface,
4997 .remove_interface = wl1271_op_remove_interface,
c0fad1b7 4998 .change_interface = wl12xx_op_change_interface,
f634a4e7 4999#ifdef CONFIG_PM
402e4861
EP
5000 .suspend = wl1271_op_suspend,
5001 .resume = wl1271_op_resume,
f634a4e7 5002#endif
f5fc0f86 5003 .config = wl1271_op_config,
c87dec9f 5004 .prepare_multicast = wl1271_op_prepare_multicast,
f5fc0f86
LC
5005 .configure_filter = wl1271_op_configure_filter,
5006 .tx = wl1271_op_tx,
a1c597f2 5007 .set_key = wlcore_op_set_key,
f5fc0f86 5008 .hw_scan = wl1271_op_hw_scan,
73ecce31 5009 .cancel_hw_scan = wl1271_op_cancel_hw_scan,
33c2c06c
LC
5010 .sched_scan_start = wl1271_op_sched_scan_start,
5011 .sched_scan_stop = wl1271_op_sched_scan_stop,
f5fc0f86 5012 .bss_info_changed = wl1271_op_bss_info_changed,
68d069c4 5013 .set_frag_threshold = wl1271_op_set_frag_threshold,
f5fc0f86 5014 .set_rts_threshold = wl1271_op_set_rts_threshold,
c6999d83 5015 .conf_tx = wl1271_op_conf_tx,
bbbb538e 5016 .get_tsf = wl1271_op_get_tsf,
ece550d0 5017 .get_survey = wl1271_op_get_survey,
2d6cf2b5 5018 .sta_state = wl12xx_op_sta_state,
bbba3e68 5019 .ampdu_action = wl1271_op_ampdu_action,
33437893 5020 .tx_frames_pending = wl1271_tx_frames_pending,
af7fbb28 5021 .set_bitrate_mask = wl12xx_set_bitrate_mask,
6d158ff3 5022 .channel_switch = wl12xx_op_channel_switch,
d8ae5a25 5023 .flush = wlcore_op_flush,
dabf37db
EP
5024 .remain_on_channel = wlcore_op_remain_on_channel,
5025 .cancel_remain_on_channel = wlcore_op_cancel_remain_on_channel,
b6970ee5
EP
5026 .add_chanctx = wlcore_op_add_chanctx,
5027 .remove_chanctx = wlcore_op_remove_chanctx,
5028 .change_chanctx = wlcore_op_change_chanctx,
5029 .assign_vif_chanctx = wlcore_op_assign_vif_chanctx,
5030 .unassign_vif_chanctx = wlcore_op_unassign_vif_chanctx,
c8c90873 5031 CFG80211_TESTMODE_CMD(wl1271_tm_cmd)
f5fc0f86
LC
5032};
5033
f876bb9a 5034
43a8bc5a 5035u8 wlcore_rate_to_idx(struct wl1271 *wl, u8 rate, enum ieee80211_band band)
f876bb9a
JO
5036{
5037 u8 idx;
5038
43a8bc5a 5039 BUG_ON(band >= 2);
f876bb9a 5040
43a8bc5a 5041 if (unlikely(rate >= wl->hw_tx_rate_tbl_size)) {
f876bb9a
JO
5042 wl1271_error("Illegal RX rate from HW: %d", rate);
5043 return 0;
5044 }
5045
43a8bc5a 5046 idx = wl->band_rate_to_idx[band][rate];
f876bb9a
JO
5047 if (unlikely(idx == CONF_HW_RXTX_RATE_UNSUPPORTED)) {
5048 wl1271_error("Unsupported RX rate from HW: %d", rate);
5049 return 0;
5050 }
5051
5052 return idx;
5053}
5054
7fc3a864
JO
5055static ssize_t wl1271_sysfs_show_bt_coex_state(struct device *dev,
5056 struct device_attribute *attr,
5057 char *buf)
5058{
5059 struct wl1271 *wl = dev_get_drvdata(dev);
5060 ssize_t len;
5061
2f63b011 5062 len = PAGE_SIZE;
7fc3a864
JO
5063
5064 mutex_lock(&wl->mutex);
5065 len = snprintf(buf, len, "%d\n\n0 - off\n1 - on\n",
5066 wl->sg_enabled);
5067 mutex_unlock(&wl->mutex);
5068
5069 return len;
5070
5071}
5072
5073static ssize_t wl1271_sysfs_store_bt_coex_state(struct device *dev,
5074 struct device_attribute *attr,
5075 const char *buf, size_t count)
5076{
5077 struct wl1271 *wl = dev_get_drvdata(dev);
5078 unsigned long res;
5079 int ret;
5080
6277ed65 5081 ret = kstrtoul(buf, 10, &res);
7fc3a864
JO
5082 if (ret < 0) {
5083 wl1271_warning("incorrect value written to bt_coex_mode");
5084 return count;
5085 }
5086
5087 mutex_lock(&wl->mutex);
5088
5089 res = !!res;
5090
5091 if (res == wl->sg_enabled)
5092 goto out;
5093
5094 wl->sg_enabled = res;
5095
4cc53383 5096 if (unlikely(wl->state != WLCORE_STATE_ON))
7fc3a864
JO
5097 goto out;
5098
a620865e 5099 ret = wl1271_ps_elp_wakeup(wl);
7fc3a864
JO
5100 if (ret < 0)
5101 goto out;
5102
5103 wl1271_acx_sg_enable(wl, wl->sg_enabled);
5104 wl1271_ps_elp_sleep(wl);
5105
5106 out:
5107 mutex_unlock(&wl->mutex);
5108 return count;
5109}
5110
5111static DEVICE_ATTR(bt_coex_state, S_IRUGO | S_IWUSR,
5112 wl1271_sysfs_show_bt_coex_state,
5113 wl1271_sysfs_store_bt_coex_state);
5114
d717fd61
JO
5115static ssize_t wl1271_sysfs_show_hw_pg_ver(struct device *dev,
5116 struct device_attribute *attr,
5117 char *buf)
5118{
5119 struct wl1271 *wl = dev_get_drvdata(dev);
5120 ssize_t len;
5121
2f63b011 5122 len = PAGE_SIZE;
d717fd61
JO
5123
5124 mutex_lock(&wl->mutex);
5125 if (wl->hw_pg_ver >= 0)
5126 len = snprintf(buf, len, "%d\n", wl->hw_pg_ver);
5127 else
5128 len = snprintf(buf, len, "n/a\n");
5129 mutex_unlock(&wl->mutex);
5130
5131 return len;
5132}
5133
6f07b72a 5134static DEVICE_ATTR(hw_pg_ver, S_IRUGO,
d717fd61
JO
5135 wl1271_sysfs_show_hw_pg_ver, NULL);
5136
95dac04f
IY
5137static ssize_t wl1271_sysfs_read_fwlog(struct file *filp, struct kobject *kobj,
5138 struct bin_attribute *bin_attr,
5139 char *buffer, loff_t pos, size_t count)
5140{
5141 struct device *dev = container_of(kobj, struct device, kobj);
5142 struct wl1271 *wl = dev_get_drvdata(dev);
5143 ssize_t len;
5144 int ret;
5145
5146 ret = mutex_lock_interruptible(&wl->mutex);
5147 if (ret < 0)
5148 return -ERESTARTSYS;
5149
5150 /* Let only one thread read the log at a time, blocking others */
5151 while (wl->fwlog_size == 0) {
5152 DEFINE_WAIT(wait);
5153
5154 prepare_to_wait_exclusive(&wl->fwlog_waitq,
5155 &wait,
5156 TASK_INTERRUPTIBLE);
5157
5158 if (wl->fwlog_size != 0) {
5159 finish_wait(&wl->fwlog_waitq, &wait);
5160 break;
5161 }
5162
5163 mutex_unlock(&wl->mutex);
5164
5165 schedule();
5166 finish_wait(&wl->fwlog_waitq, &wait);
5167
5168 if (signal_pending(current))
5169 return -ERESTARTSYS;
5170
5171 ret = mutex_lock_interruptible(&wl->mutex);
5172 if (ret < 0)
5173 return -ERESTARTSYS;
5174 }
5175
5176 /* Check if the fwlog is still valid */
5177 if (wl->fwlog_size < 0) {
5178 mutex_unlock(&wl->mutex);
5179 return 0;
5180 }
5181
5182 /* Seeking is not supported - old logs are not kept. Disregard pos. */
5183 len = min(count, (size_t)wl->fwlog_size);
5184 wl->fwlog_size -= len;
5185 memcpy(buffer, wl->fwlog, len);
5186
5187 /* Make room for new messages */
5188 memmove(wl->fwlog, wl->fwlog + len, wl->fwlog_size);
5189
5190 mutex_unlock(&wl->mutex);
5191
5192 return len;
5193}
5194
5195static struct bin_attribute fwlog_attr = {
5196 .attr = {.name = "fwlog", .mode = S_IRUSR},
5197 .read = wl1271_sysfs_read_fwlog,
5198};
5199
22479972 5200static void wl1271_connection_loss_work(struct work_struct *work)
5f561f68
BM
5201{
5202 struct delayed_work *dwork;
5203 struct wl1271 *wl;
5204 struct ieee80211_vif *vif;
5205 struct wl12xx_vif *wlvif;
5206
5207 dwork = container_of(work, struct delayed_work, work);
5208 wl = container_of(dwork, struct wl1271, connection_loss_work);
5209
5210 wl1271_info("Connection loss work.");
5211
5212 mutex_lock(&wl->mutex);
5213
4cc53383 5214 if (unlikely(wl->state != WLCORE_STATE_ON))
5f561f68
BM
5215 goto out;
5216
5217 /* Call mac80211 connection loss */
5218 wl12xx_for_each_wlvif_sta(wl, wlvif) {
5219 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
5220 goto out;
5221 vif = wl12xx_wlvif_to_vif(wlvif);
5222 ieee80211_connection_loss(vif);
5223 }
5224out:
5225 mutex_unlock(&wl->mutex);
5226}
5227
f4afbed9 5228static void wl12xx_derive_mac_addresses(struct wl1271 *wl, u32 oui, u32 nic)
5e037e74
LC
5229{
5230 int i;
5231
f4afbed9
AN
5232 wl1271_debug(DEBUG_PROBE, "base address: oui %06x nic %06x",
5233 oui, nic);
5e037e74 5234
f4afbed9 5235 if (nic + WLCORE_NUM_MAC_ADDRESSES - wl->num_mac_addr > 0xffffff)
5e037e74
LC
5236 wl1271_warning("NIC part of the MAC address wraps around!");
5237
f4afbed9 5238 for (i = 0; i < wl->num_mac_addr; i++) {
5e037e74
LC
5239 wl->addresses[i].addr[0] = (u8)(oui >> 16);
5240 wl->addresses[i].addr[1] = (u8)(oui >> 8);
5241 wl->addresses[i].addr[2] = (u8) oui;
5242 wl->addresses[i].addr[3] = (u8)(nic >> 16);
5243 wl->addresses[i].addr[4] = (u8)(nic >> 8);
5244 wl->addresses[i].addr[5] = (u8) nic;
5245 nic++;
5246 }
5247
f4afbed9
AN
5248 /* we may be one address short at the most */
5249 WARN_ON(wl->num_mac_addr + 1 < WLCORE_NUM_MAC_ADDRESSES);
5250
5251 /*
5252 * turn on the LAA bit in the first address and use it as
5253 * the last address.
5254 */
5255 if (wl->num_mac_addr < WLCORE_NUM_MAC_ADDRESSES) {
5256 int idx = WLCORE_NUM_MAC_ADDRESSES - 1;
5257 memcpy(&wl->addresses[idx], &wl->addresses[0],
5258 sizeof(wl->addresses[0]));
5259 /* LAA bit */
5260 wl->addresses[idx].addr[2] |= BIT(1);
5261 }
5262
5263 wl->hw->wiphy->n_addresses = WLCORE_NUM_MAC_ADDRESSES;
5e037e74
LC
5264 wl->hw->wiphy->addresses = wl->addresses;
5265}
5266
30c5dbd1
LC
5267static int wl12xx_get_hw_info(struct wl1271 *wl)
5268{
5269 int ret;
30c5dbd1
LC
5270
5271 ret = wl12xx_set_power_on(wl);
5272 if (ret < 0)
4fb4e0be 5273 return ret;
30c5dbd1 5274
6134323f
IY
5275 ret = wlcore_read_reg(wl, REG_CHIP_ID_B, &wl->chip.id);
5276 if (ret < 0)
5277 goto out;
30c5dbd1 5278
00782136
LC
5279 wl->fuse_oui_addr = 0;
5280 wl->fuse_nic_addr = 0;
30c5dbd1 5281
6134323f
IY
5282 ret = wl->ops->get_pg_ver(wl, &wl->hw_pg_ver);
5283 if (ret < 0)
5284 goto out;
30c5dbd1 5285
30d9b4a5 5286 if (wl->ops->get_mac)
6134323f 5287 ret = wl->ops->get_mac(wl);
5e037e74 5288
30c5dbd1 5289out:
6134323f 5290 wl1271_power_off(wl);
30c5dbd1
LC
5291 return ret;
5292}
5293
4b32a2c9 5294static int wl1271_register_hw(struct wl1271 *wl)
f5fc0f86
LC
5295{
5296 int ret;
5e037e74 5297 u32 oui_addr = 0, nic_addr = 0;
f5fc0f86
LC
5298
5299 if (wl->mac80211_registered)
5300 return 0;
5301
6f8d6b20 5302 if (wl->nvs_len >= 12) {
bc765bf3
SL
5303 /* NOTE: The wl->nvs->nvs element must be first, in
5304 * order to simplify the casting, we assume it is at
5305 * the beginning of the wl->nvs structure.
5306 */
5307 u8 *nvs_ptr = (u8 *)wl->nvs;
31d26ec6 5308
5e037e74
LC
5309 oui_addr =
5310 (nvs_ptr[11] << 16) + (nvs_ptr[10] << 8) + nvs_ptr[6];
5311 nic_addr =
5312 (nvs_ptr[5] << 16) + (nvs_ptr[4] << 8) + nvs_ptr[3];
5313 }
5314
5315 /* if the MAC address is zeroed in the NVS derive from fuse */
5316 if (oui_addr == 0 && nic_addr == 0) {
5317 oui_addr = wl->fuse_oui_addr;
5318 /* fuse has the BD_ADDR, the WLAN addresses are the next two */
5319 nic_addr = wl->fuse_nic_addr + 1;
31d26ec6
AN
5320 }
5321
f4afbed9 5322 wl12xx_derive_mac_addresses(wl, oui_addr, nic_addr);
f5fc0f86
LC
5323
5324 ret = ieee80211_register_hw(wl->hw);
5325 if (ret < 0) {
5326 wl1271_error("unable to register mac80211 hw: %d", ret);
30c5dbd1 5327 goto out;
f5fc0f86
LC
5328 }
5329
5330 wl->mac80211_registered = true;
5331
d60080ae
EP
5332 wl1271_debugfs_init(wl);
5333
f5fc0f86
LC
5334 wl1271_notice("loaded");
5335
30c5dbd1
LC
5336out:
5337 return ret;
f5fc0f86
LC
5338}
5339
4b32a2c9 5340static void wl1271_unregister_hw(struct wl1271 *wl)
3b56dd6a 5341{
3fcdab70 5342 if (wl->plt)
f3df1331 5343 wl1271_plt_stop(wl);
4ae3fa87 5344
3b56dd6a
TP
5345 ieee80211_unregister_hw(wl->hw);
5346 wl->mac80211_registered = false;
5347
5348}
3b56dd6a 5349
bcab320b
EP
5350static const struct ieee80211_iface_limit wlcore_iface_limits[] = {
5351 {
e7a6ba29 5352 .max = 3,
bcab320b
EP
5353 .types = BIT(NL80211_IFTYPE_STATION),
5354 },
5355 {
5356 .max = 1,
5357 .types = BIT(NL80211_IFTYPE_AP) |
5358 BIT(NL80211_IFTYPE_P2P_GO) |
5359 BIT(NL80211_IFTYPE_P2P_CLIENT),
5360 },
5361};
5362
5363static const struct ieee80211_iface_combination
5364wlcore_iface_combinations[] = {
5365 {
5366 .num_different_channels = 1,
e7a6ba29 5367 .max_interfaces = 3,
bcab320b
EP
5368 .limits = wlcore_iface_limits,
5369 .n_limits = ARRAY_SIZE(wlcore_iface_limits),
5370 },
5371};
5372
4b32a2c9 5373static int wl1271_init_ieee80211(struct wl1271 *wl)
f5fc0f86 5374{
7a55724e
JO
5375 static const u32 cipher_suites[] = {
5376 WLAN_CIPHER_SUITE_WEP40,
5377 WLAN_CIPHER_SUITE_WEP104,
5378 WLAN_CIPHER_SUITE_TKIP,
5379 WLAN_CIPHER_SUITE_CCMP,
5380 WL1271_CIPHER_SUITE_GEM,
5381 };
5382
2c0133a4
AN
5383 /* The tx descriptor buffer */
5384 wl->hw->extra_tx_headroom = sizeof(struct wl1271_tx_hw_descr);
5385
5386 if (wl->quirks & WLCORE_QUIRK_TKIP_HEADER_SPACE)
5387 wl->hw->extra_tx_headroom += WL1271_EXTRA_SPACE_TKIP;
f5fc0f86
LC
5388
5389 /* unit us */
5390 /* FIXME: find a proper value */
5391 wl->hw->channel_change_time = 10000;
50c500ad 5392 wl->hw->max_listen_interval = wl->conf.conn.max_listen_interval;
f5fc0f86
LC
5393
5394 wl->hw->flags = IEEE80211_HW_SIGNAL_DBM |
0a34332f 5395 IEEE80211_HW_SUPPORTS_PS |
f1d63a59 5396 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
4695dc91 5397 IEEE80211_HW_SUPPORTS_UAPSD |
a9af092b 5398 IEEE80211_HW_HAS_RATE_CONTROL |
00236aed 5399 IEEE80211_HW_CONNECTION_MONITOR |
25eaea30 5400 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
fcd23b63 5401 IEEE80211_HW_SPECTRUM_MGMT |
93f8c8e0
AN
5402 IEEE80211_HW_AP_LINK_PS |
5403 IEEE80211_HW_AMPDU_AGGREGATION |
79aba1ba
EP
5404 IEEE80211_HW_TX_AMPDU_SETUP_IN_HW |
5405 IEEE80211_HW_SCAN_WHILE_IDLE;
f5fc0f86 5406
7a55724e
JO
5407 wl->hw->wiphy->cipher_suites = cipher_suites;
5408 wl->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
5409
e0d8bbf0 5410 wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
045c745f
EP
5411 BIT(NL80211_IFTYPE_ADHOC) | BIT(NL80211_IFTYPE_AP) |
5412 BIT(NL80211_IFTYPE_P2P_CLIENT) | BIT(NL80211_IFTYPE_P2P_GO);
f5fc0f86 5413 wl->hw->wiphy->max_scan_ssids = 1;
221737d2
LC
5414 wl->hw->wiphy->max_sched_scan_ssids = 16;
5415 wl->hw->wiphy->max_match_sets = 16;
ea559b46
GE
5416 /*
5417 * Maximum length of elements in scanning probe request templates
5418 * should be the maximum length possible for a template, without
5419 * the IEEE80211 header of the template
5420 */
c08e371a 5421 wl->hw->wiphy->max_scan_ie_len = WL1271_CMD_TEMPL_MAX_SIZE -
ea559b46 5422 sizeof(struct ieee80211_header);
a8aaaf53 5423
c08e371a 5424 wl->hw->wiphy->max_sched_scan_ie_len = WL1271_CMD_TEMPL_MAX_SIZE -
c9e79a47
LC
5425 sizeof(struct ieee80211_header);
5426
dabf37db
EP
5427 wl->hw->wiphy->max_remain_on_channel_duration = 5000;
5428
81ddbb5c
JB
5429 wl->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD |
5430 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
1ec23f7f 5431
4a31c11c
LC
5432 /* make sure all our channels fit in the scanned_ch bitmask */
5433 BUILD_BUG_ON(ARRAY_SIZE(wl1271_channels) +
5434 ARRAY_SIZE(wl1271_channels_5ghz) >
5435 WL1271_MAX_CHANNELS);
a8aaaf53
LC
5436 /*
5437 * We keep local copies of the band structs because we need to
5438 * modify them on a per-device basis.
5439 */
5440 memcpy(&wl->bands[IEEE80211_BAND_2GHZ], &wl1271_band_2ghz,
5441 sizeof(wl1271_band_2ghz));
bfb92ca1
EP
5442 memcpy(&wl->bands[IEEE80211_BAND_2GHZ].ht_cap,
5443 &wl->ht_cap[IEEE80211_BAND_2GHZ],
5444 sizeof(*wl->ht_cap));
a8aaaf53
LC
5445 memcpy(&wl->bands[IEEE80211_BAND_5GHZ], &wl1271_band_5ghz,
5446 sizeof(wl1271_band_5ghz));
bfb92ca1
EP
5447 memcpy(&wl->bands[IEEE80211_BAND_5GHZ].ht_cap,
5448 &wl->ht_cap[IEEE80211_BAND_5GHZ],
5449 sizeof(*wl->ht_cap));
a8aaaf53
LC
5450
5451 wl->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
5452 &wl->bands[IEEE80211_BAND_2GHZ];
5453 wl->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
5454 &wl->bands[IEEE80211_BAND_5GHZ];
1ebec3d7 5455
12bd8949 5456 wl->hw->queues = 4;
31627dc5 5457 wl->hw->max_rates = 1;
12bd8949 5458
b7417d93
JO
5459 wl->hw->wiphy->reg_notifier = wl1271_reg_notify;
5460
9c1b190b
AN
5461 /* the FW answers probe-requests in AP-mode */
5462 wl->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
5463 wl->hw->wiphy->probe_resp_offload =
5464 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
5465 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
5466 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
5467
bcab320b
EP
5468 /* allowed interface combinations */
5469 wl->hw->wiphy->iface_combinations = wlcore_iface_combinations;
5470 wl->hw->wiphy->n_iface_combinations =
5471 ARRAY_SIZE(wlcore_iface_combinations);
5472
a390e85c 5473 SET_IEEE80211_DEV(wl->hw, wl->dev);
f5fc0f86 5474
f84f7d78 5475 wl->hw->sta_data_size = sizeof(struct wl1271_station);
87fbcb0f 5476 wl->hw->vif_data_size = sizeof(struct wl12xx_vif);
f84f7d78 5477
ba421f8f 5478 wl->hw->max_rx_aggregation_subframes = wl->conf.ht.rx_ba_win_size;
4c9cfa78 5479
f5fc0f86
LC
5480 return 0;
5481}
5482
f5fc0f86 5483#define WL1271_DEFAULT_CHANNEL 0
c332a4b8 5484
26a309c7 5485struct ieee80211_hw *wlcore_alloc_hw(size_t priv_size, u32 aggr_buf_size)
f5fc0f86 5486{
f5fc0f86
LC
5487 struct ieee80211_hw *hw;
5488 struct wl1271 *wl;
a8c0ddb5 5489 int i, j, ret;
1f37cbc9 5490 unsigned int order;
f5fc0f86 5491
c7ffb902 5492 BUILD_BUG_ON(AP_MAX_STATIONS > WL12XX_MAX_LINKS);
f80c2d12 5493
f5fc0f86
LC
5494 hw = ieee80211_alloc_hw(sizeof(*wl), &wl1271_ops);
5495 if (!hw) {
5496 wl1271_error("could not alloc ieee80211_hw");
a1dd8187 5497 ret = -ENOMEM;
3b56dd6a
TP
5498 goto err_hw_alloc;
5499 }
5500
f5fc0f86
LC
5501 wl = hw->priv;
5502 memset(wl, 0, sizeof(*wl));
5503
96e0c683
AN
5504 wl->priv = kzalloc(priv_size, GFP_KERNEL);
5505 if (!wl->priv) {
5506 wl1271_error("could not alloc wl priv");
5507 ret = -ENOMEM;
5508 goto err_priv_alloc;
5509 }
5510
87627214 5511 INIT_LIST_HEAD(&wl->wlvif_list);
01c09162 5512
f5fc0f86 5513 wl->hw = hw;
f5fc0f86 5514
a8c0ddb5 5515 for (i = 0; i < NUM_TX_QUEUES; i++)
c7ffb902 5516 for (j = 0; j < WL12XX_MAX_LINKS; j++)
a8c0ddb5
AN
5517 skb_queue_head_init(&wl->links[j].tx_queue[i]);
5518
a620865e
IY
5519 skb_queue_head_init(&wl->deferred_rx_queue);
5520 skb_queue_head_init(&wl->deferred_tx_queue);
5521
37b70a81 5522 INIT_DELAYED_WORK(&wl->elp_work, wl1271_elp_work);
a620865e 5523 INIT_WORK(&wl->netstack_work, wl1271_netstack_work);
117b38d0
JO
5524 INIT_WORK(&wl->tx_work, wl1271_tx_work);
5525 INIT_WORK(&wl->recovery_work, wl1271_recovery_work);
5526 INIT_DELAYED_WORK(&wl->scan_complete_work, wl1271_scan_complete_work);
dabf37db 5527 INIT_DELAYED_WORK(&wl->roc_complete_work, wlcore_roc_complete_work);
55df5afb 5528 INIT_DELAYED_WORK(&wl->tx_watchdog_work, wl12xx_tx_watchdog_work);
5f561f68
BM
5529 INIT_DELAYED_WORK(&wl->connection_loss_work,
5530 wl1271_connection_loss_work);
77ddaa10 5531
92ef8960
EP
5532 wl->freezable_wq = create_freezable_workqueue("wl12xx_wq");
5533 if (!wl->freezable_wq) {
5534 ret = -ENOMEM;
5535 goto err_hw;
5536 }
5537
f5fc0f86 5538 wl->channel = WL1271_DEFAULT_CHANNEL;
f5fc0f86 5539 wl->rx_counter = 0;
f5fc0f86 5540 wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
8a5a37a6 5541 wl->band = IEEE80211_BAND_2GHZ;
83d08d3f 5542 wl->channel_type = NL80211_CHAN_NO_HT;
830fb67b 5543 wl->flags = 0;
7fc3a864 5544 wl->sg_enabled = true;
66340e5b 5545 wl->sleep_auth = WL1271_PSM_ILLEGAL;
d717fd61 5546 wl->hw_pg_ver = -1;
b622d992
AN
5547 wl->ap_ps_map = 0;
5548 wl->ap_fw_ps_map = 0;
606ea9fa 5549 wl->quirks = 0;
341b7cde 5550 wl->platform_quirks = 0;
33c2c06c 5551 wl->sched_scanning = false;
f4df1bd5 5552 wl->system_hlid = WL12XX_SYSTEM_HLID;
da03209e 5553 wl->active_sta_count = 0;
95dac04f
IY
5554 wl->fwlog_size = 0;
5555 init_waitqueue_head(&wl->fwlog_waitq);
f5fc0f86 5556
f4df1bd5
EP
5557 /* The system link is always allocated */
5558 __set_bit(WL12XX_SYSTEM_HLID, wl->links_map);
5559
25eeb9e3 5560 memset(wl->tx_frames_map, 0, sizeof(wl->tx_frames_map));
72b0624f 5561 for (i = 0; i < wl->num_tx_desc; i++)
f5fc0f86
LC
5562 wl->tx_frames[i] = NULL;
5563
5564 spin_lock_init(&wl->wl_lock);
5565
4cc53383 5566 wl->state = WLCORE_STATE_OFF;
3fcdab70 5567 wl->fw_type = WL12XX_FW_TYPE_NONE;
f5fc0f86 5568 mutex_init(&wl->mutex);
2c38849f 5569 mutex_init(&wl->flush_mutex);
6f8d6b20 5570 init_completion(&wl->nvs_loading_complete);
f5fc0f86 5571
26a309c7 5572 order = get_order(aggr_buf_size);
1f37cbc9
IY
5573 wl->aggr_buf = (u8 *)__get_free_pages(GFP_KERNEL, order);
5574 if (!wl->aggr_buf) {
5575 ret = -ENOMEM;
92ef8960 5576 goto err_wq;
1f37cbc9 5577 }
26a309c7 5578 wl->aggr_buf_size = aggr_buf_size;
1f37cbc9 5579
990f5de7
IY
5580 wl->dummy_packet = wl12xx_alloc_dummy_packet(wl);
5581 if (!wl->dummy_packet) {
5582 ret = -ENOMEM;
5583 goto err_aggr;
5584 }
5585
95dac04f
IY
5586 /* Allocate one page for the FW log */
5587 wl->fwlog = (u8 *)get_zeroed_page(GFP_KERNEL);
5588 if (!wl->fwlog) {
5589 ret = -ENOMEM;
5590 goto err_dummy_packet;
5591 }
5592
fd492ed7 5593 wl->mbox = kmalloc(sizeof(*wl->mbox), GFP_KERNEL | GFP_DMA);
690142e9
MG
5594 if (!wl->mbox) {
5595 ret = -ENOMEM;
5596 goto err_fwlog;
5597 }
5598
c332a4b8 5599 return hw;
a1dd8187 5600
690142e9
MG
5601err_fwlog:
5602 free_page((unsigned long)wl->fwlog);
5603
990f5de7
IY
5604err_dummy_packet:
5605 dev_kfree_skb(wl->dummy_packet);
5606
1f37cbc9
IY
5607err_aggr:
5608 free_pages((unsigned long)wl->aggr_buf, order);
5609
92ef8960
EP
5610err_wq:
5611 destroy_workqueue(wl->freezable_wq);
5612
a1dd8187 5613err_hw:
3b56dd6a 5614 wl1271_debugfs_exit(wl);
96e0c683
AN
5615 kfree(wl->priv);
5616
5617err_priv_alloc:
3b56dd6a
TP
5618 ieee80211_free_hw(hw);
5619
5620err_hw_alloc:
a1dd8187 5621
a1dd8187 5622 return ERR_PTR(ret);
c332a4b8 5623}
ffeb501c 5624EXPORT_SYMBOL_GPL(wlcore_alloc_hw);
c332a4b8 5625
ffeb501c 5626int wlcore_free_hw(struct wl1271 *wl)
c332a4b8 5627{
95dac04f
IY
5628 /* Unblock any fwlog readers */
5629 mutex_lock(&wl->mutex);
5630 wl->fwlog_size = -1;
5631 wake_up_interruptible_all(&wl->fwlog_waitq);
5632 mutex_unlock(&wl->mutex);
5633
f79f890c 5634 device_remove_bin_file(wl->dev, &fwlog_attr);
6f07b72a 5635
f79f890c 5636 device_remove_file(wl->dev, &dev_attr_hw_pg_ver);
6f07b72a 5637
f79f890c 5638 device_remove_file(wl->dev, &dev_attr_bt_coex_state);
a8e27820 5639 kfree(wl->mbox);
95dac04f 5640 free_page((unsigned long)wl->fwlog);
990f5de7 5641 dev_kfree_skb(wl->dummy_packet);
26a309c7 5642 free_pages((unsigned long)wl->aggr_buf, get_order(wl->aggr_buf_size));
c332a4b8
TP
5643
5644 wl1271_debugfs_exit(wl);
5645
c332a4b8
TP
5646 vfree(wl->fw);
5647 wl->fw = NULL;
3fcdab70 5648 wl->fw_type = WL12XX_FW_TYPE_NONE;
c332a4b8
TP
5649 kfree(wl->nvs);
5650 wl->nvs = NULL;
5651
0afd04e5 5652 kfree(wl->fw_status_1);
c332a4b8 5653 kfree(wl->tx_res_if);
92ef8960 5654 destroy_workqueue(wl->freezable_wq);
c332a4b8 5655
96e0c683 5656 kfree(wl->priv);
c332a4b8
TP
5657 ieee80211_free_hw(wl->hw);
5658
5659 return 0;
5660}
ffeb501c 5661EXPORT_SYMBOL_GPL(wlcore_free_hw);
50b3eb4b 5662
a390e85c
FB
5663static irqreturn_t wl12xx_hardirq(int irq, void *cookie)
5664{
5665 struct wl1271 *wl = cookie;
5666 unsigned long flags;
5667
5668 wl1271_debug(DEBUG_IRQ, "IRQ");
5669
5670 /* complete the ELP completion */
5671 spin_lock_irqsave(&wl->wl_lock, flags);
5672 set_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
5673 if (wl->elp_compl) {
5674 complete(wl->elp_compl);
5675 wl->elp_compl = NULL;
5676 }
5677
5678 if (test_bit(WL1271_FLAG_SUSPENDED, &wl->flags)) {
5679 /* don't enqueue a work right now. mark it as pending */
5680 set_bit(WL1271_FLAG_PENDING_WORK, &wl->flags);
5681 wl1271_debug(DEBUG_IRQ, "should not enqueue work");
5682 disable_irq_nosync(wl->irq);
5683 pm_wakeup_event(wl->dev, 0);
5684 spin_unlock_irqrestore(&wl->wl_lock, flags);
5685 return IRQ_HANDLED;
5686 }
5687 spin_unlock_irqrestore(&wl->wl_lock, flags);
5688
5689 return IRQ_WAKE_THREAD;
5690}
5691
6f8d6b20 5692static void wlcore_nvs_cb(const struct firmware *fw, void *context)
ce2a217c 5693{
6f8d6b20
IY
5694 struct wl1271 *wl = context;
5695 struct platform_device *pdev = wl->pdev;
a390e85c 5696 struct wl12xx_platform_data *pdata = pdev->dev.platform_data;
a390e85c 5697 unsigned long irqflags;
ffeb501c 5698 int ret;
a390e85c 5699
6f8d6b20
IY
5700 if (fw) {
5701 wl->nvs = kmemdup(fw->data, fw->size, GFP_KERNEL);
5702 if (!wl->nvs) {
5703 wl1271_error("Could not allocate nvs data");
5704 goto out;
5705 }
5706 wl->nvs_len = fw->size;
5707 } else {
5708 wl1271_debug(DEBUG_BOOT, "Could not get nvs file %s",
5709 WL12XX_NVS_NAME);
5710 wl->nvs = NULL;
5711 wl->nvs_len = 0;
a390e85c
FB
5712 }
5713
3992eb2b
IY
5714 ret = wl->ops->setup(wl);
5715 if (ret < 0)
6f8d6b20 5716 goto out_free_nvs;
3992eb2b 5717
72b0624f
AN
5718 BUG_ON(wl->num_tx_desc > WLCORE_MAX_TX_DESCRIPTORS);
5719
e87288f0
LC
5720 /* adjust some runtime configuration parameters */
5721 wlcore_adjust_conf(wl);
5722
a390e85c 5723 wl->irq = platform_get_irq(pdev, 0);
a390e85c
FB
5724 wl->platform_quirks = pdata->platform_quirks;
5725 wl->set_power = pdata->set_power;
a390e85c
FB
5726 wl->if_ops = pdata->ops;
5727
a390e85c
FB
5728 if (wl->platform_quirks & WL12XX_PLATFORM_QUIRK_EDGE_IRQ)
5729 irqflags = IRQF_TRIGGER_RISING;
5730 else
5731 irqflags = IRQF_TRIGGER_HIGH | IRQF_ONESHOT;
5732
b5b45b3c 5733 ret = request_threaded_irq(wl->irq, wl12xx_hardirq, wlcore_irq,
a390e85c
FB
5734 irqflags,
5735 pdev->name, wl);
5736 if (ret < 0) {
5737 wl1271_error("request_irq() failed: %d", ret);
6f8d6b20 5738 goto out_free_nvs;
a390e85c
FB
5739 }
5740
dfb89c56 5741#ifdef CONFIG_PM
a390e85c
FB
5742 ret = enable_irq_wake(wl->irq);
5743 if (!ret) {
5744 wl->irq_wake_enabled = true;
5745 device_init_wakeup(wl->dev, 1);
b95d7cef 5746 if (pdata->pwr_in_suspend) {
ffeb501c 5747 wl->hw->wiphy->wowlan.flags = WIPHY_WOWLAN_ANY;
b95d7cef
ES
5748 wl->hw->wiphy->wowlan.n_patterns =
5749 WL1271_MAX_RX_FILTERS;
5750 wl->hw->wiphy->wowlan.pattern_min_len = 1;
5751 wl->hw->wiphy->wowlan.pattern_max_len =
5752 WL1271_RX_FILTER_MAX_PATTERN_SIZE;
5753 }
a390e85c 5754 }
dfb89c56 5755#endif
a390e85c
FB
5756 disable_irq(wl->irq);
5757
4afc37a0
LC
5758 ret = wl12xx_get_hw_info(wl);
5759 if (ret < 0) {
5760 wl1271_error("couldn't get hw info");
8b425e62 5761 goto out_irq;
4afc37a0
LC
5762 }
5763
5764 ret = wl->ops->identify_chip(wl);
5765 if (ret < 0)
8b425e62 5766 goto out_irq;
4afc37a0 5767
a390e85c
FB
5768 ret = wl1271_init_ieee80211(wl);
5769 if (ret)
5770 goto out_irq;
5771
5772 ret = wl1271_register_hw(wl);
5773 if (ret)
5774 goto out_irq;
5775
f79f890c
FB
5776 /* Create sysfs file to control bt coex state */
5777 ret = device_create_file(wl->dev, &dev_attr_bt_coex_state);
5778 if (ret < 0) {
5779 wl1271_error("failed to create sysfs file bt_coex_state");
8b425e62 5780 goto out_unreg;
f79f890c
FB
5781 }
5782
5783 /* Create sysfs file to get HW PG version */
5784 ret = device_create_file(wl->dev, &dev_attr_hw_pg_ver);
5785 if (ret < 0) {
5786 wl1271_error("failed to create sysfs file hw_pg_ver");
5787 goto out_bt_coex_state;
5788 }
5789
5790 /* Create sysfs file for the FW log */
5791 ret = device_create_bin_file(wl->dev, &fwlog_attr);
5792 if (ret < 0) {
5793 wl1271_error("failed to create sysfs file fwlog");
5794 goto out_hw_pg_ver;
5795 }
5796
6f8d6b20 5797 wl->initialized = true;
ffeb501c 5798 goto out;
a390e85c 5799
f79f890c
FB
5800out_hw_pg_ver:
5801 device_remove_file(wl->dev, &dev_attr_hw_pg_ver);
5802
5803out_bt_coex_state:
5804 device_remove_file(wl->dev, &dev_attr_bt_coex_state);
5805
8b425e62
LC
5806out_unreg:
5807 wl1271_unregister_hw(wl);
5808
a390e85c
FB
5809out_irq:
5810 free_irq(wl->irq, wl);
5811
6f8d6b20
IY
5812out_free_nvs:
5813 kfree(wl->nvs);
5814
a390e85c 5815out:
6f8d6b20
IY
5816 release_firmware(fw);
5817 complete_all(&wl->nvs_loading_complete);
5818}
5819
5820int __devinit wlcore_probe(struct wl1271 *wl, struct platform_device *pdev)
5821{
5822 int ret;
5823
5824 if (!wl->ops || !wl->ptable)
5825 return -EINVAL;
5826
5827 wl->dev = &pdev->dev;
5828 wl->pdev = pdev;
5829 platform_set_drvdata(pdev, wl);
5830
5831 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
5832 WL12XX_NVS_NAME, &pdev->dev, GFP_KERNEL,
5833 wl, wlcore_nvs_cb);
5834 if (ret < 0) {
5835 wl1271_error("request_firmware_nowait failed: %d", ret);
5836 complete_all(&wl->nvs_loading_complete);
5837 }
5838
a390e85c 5839 return ret;
ce2a217c 5840}
b2ba99ff 5841EXPORT_SYMBOL_GPL(wlcore_probe);
ce2a217c 5842
b2ba99ff 5843int __devexit wlcore_remove(struct platform_device *pdev)
ce2a217c 5844{
a390e85c
FB
5845 struct wl1271 *wl = platform_get_drvdata(pdev);
5846
6f8d6b20
IY
5847 wait_for_completion(&wl->nvs_loading_complete);
5848 if (!wl->initialized)
5849 return 0;
5850
a390e85c
FB
5851 if (wl->irq_wake_enabled) {
5852 device_init_wakeup(wl->dev, 0);
5853 disable_irq_wake(wl->irq);
5854 }
5855 wl1271_unregister_hw(wl);
5856 free_irq(wl->irq, wl);
ffeb501c 5857 wlcore_free_hw(wl);
a390e85c 5858
ce2a217c
FB
5859 return 0;
5860}
b2ba99ff 5861EXPORT_SYMBOL_GPL(wlcore_remove);
ce2a217c 5862
491bbd6b 5863u32 wl12xx_debug_level = DEBUG_NONE;
17c1755c 5864EXPORT_SYMBOL_GPL(wl12xx_debug_level);
491bbd6b 5865module_param_named(debug_level, wl12xx_debug_level, uint, S_IRUSR | S_IWUSR);
17c1755c
EP
5866MODULE_PARM_DESC(debug_level, "wl12xx debugging level");
5867
95dac04f 5868module_param_named(fwlog, fwlog_param, charp, 0);
2c882fa4 5869MODULE_PARM_DESC(fwlog,
95dac04f
IY
5870 "FW logger options: continuous, ondemand, dbgpins or disable");
5871
2a5bff09
EP
5872module_param(bug_on_recovery, bool, S_IRUSR | S_IWUSR);
5873MODULE_PARM_DESC(bug_on_recovery, "BUG() on fw recovery");
5874
34785be5
AN
5875module_param(no_recovery, bool, S_IRUSR | S_IWUSR);
5876MODULE_PARM_DESC(no_recovery, "Prevent HW recovery. FW will remain stuck.");
5877
50b3eb4b 5878MODULE_LICENSE("GPL");
b1a48cab 5879MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
50b3eb4b 5880MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
0635ad45 5881MODULE_FIRMWARE(WL12XX_NVS_NAME);
This page took 1.009463 seconds and 5 git commands to generate.