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