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