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