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