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