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