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