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