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