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