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