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