1 /*
2 * This file is part of wlcore
3 *
4 * Copyright (C) 2008-2010 Nokia Corporation
5 * Copyright (C) 2011-2013 Texas Instruments Inc.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19 * 02110-1301 USA
20 *
21 */
22
23 #include <linux/module.h>
24 #include <linux/firmware.h>
25 #include <linux/etherdevice.h>
26 #include <linux/vmalloc.h>
27 #include <linux/interrupt.h>
28 #include <linux/irq.h>
29
30 #include "wlcore.h"
31 #include "debug.h"
32 #include "wl12xx_80211.h"
33 #include "io.h"
34 #include "tx.h"
35 #include "ps.h"
36 #include "init.h"
37 #include "debugfs.h"
38 #include "testmode.h"
39 #include "vendor_cmd.h"
40 #include "scan.h"
41 #include "hw_ops.h"
42 #include "sysfs.h"
43
44 #define WL1271_BOOT_RETRIES 3
45
46 static char *fwlog_param;
47 static int fwlog_mem_blocks = -1;
48 static int bug_on_recovery = -1;
49 static int no_recovery = -1;
50
51 static void __wl1271_op_remove_interface(struct wl1271 *wl,
52 struct ieee80211_vif *vif,
53 bool reset_tx_queues);
54 static void wlcore_op_stop_locked(struct wl1271 *wl);
55 static void wl1271_free_ap_keys(struct wl1271 *wl, struct wl12xx_vif *wlvif);
56
wl12xx_set_authorized(struct wl1271 * wl,struct wl12xx_vif * wlvif)57 static int wl12xx_set_authorized(struct wl1271 *wl, struct wl12xx_vif *wlvif)
58 {
59 int ret;
60
61 if (WARN_ON(wlvif->bss_type != BSS_TYPE_STA_BSS))
62 return -EINVAL;
63
64 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
65 return 0;
66
67 if (test_and_set_bit(WLVIF_FLAG_STA_STATE_SENT, &wlvif->flags))
68 return 0;
69
70 ret = wl12xx_cmd_set_peer_state(wl, wlvif, wlvif->sta.hlid);
71 if (ret < 0)
72 return ret;
73
74 wl1271_info("Association completed.");
75 return 0;
76 }
77
wl1271_reg_notify(struct wiphy * wiphy,struct regulatory_request * request)78 static void wl1271_reg_notify(struct wiphy *wiphy,
79 struct regulatory_request *request)
80 {
81 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
82 struct wl1271 *wl = hw->priv;
83
84 /* copy the current dfs region */
85 if (request)
86 wl->dfs_region = request->dfs_region;
87
88 wlcore_regdomain_config(wl);
89 }
90
wl1271_set_rx_streaming(struct wl1271 * wl,struct wl12xx_vif * wlvif,bool enable)91 static int wl1271_set_rx_streaming(struct wl1271 *wl, struct wl12xx_vif *wlvif,
92 bool enable)
93 {
94 int ret = 0;
95
96 /* we should hold wl->mutex */
97 ret = wl1271_acx_ps_rx_streaming(wl, wlvif, enable);
98 if (ret < 0)
99 goto out;
100
101 if (enable)
102 set_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags);
103 else
104 clear_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags);
105 out:
106 return ret;
107 }
108
109 /*
110 * this function is being called when the rx_streaming interval
111 * has beed changed or rx_streaming should be disabled
112 */
wl1271_recalc_rx_streaming(struct wl1271 * wl,struct wl12xx_vif * wlvif)113 int wl1271_recalc_rx_streaming(struct wl1271 *wl, struct wl12xx_vif *wlvif)
114 {
115 int ret = 0;
116 int period = wl->conf.rx_streaming.interval;
117
118 /* don't reconfigure if rx_streaming is disabled */
119 if (!test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags))
120 goto out;
121
122 /* reconfigure/disable according to new streaming_period */
123 if (period &&
124 test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) &&
125 (wl->conf.rx_streaming.always ||
126 test_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags)))
127 ret = wl1271_set_rx_streaming(wl, wlvif, true);
128 else {
129 ret = wl1271_set_rx_streaming(wl, wlvif, false);
130 /* don't cancel_work_sync since we might deadlock */
131 del_timer_sync(&wlvif->rx_streaming_timer);
132 }
133 out:
134 return ret;
135 }
136
wl1271_rx_streaming_enable_work(struct work_struct * work)137 static void wl1271_rx_streaming_enable_work(struct work_struct *work)
138 {
139 int ret;
140 struct wl12xx_vif *wlvif = container_of(work, struct wl12xx_vif,
141 rx_streaming_enable_work);
142 struct wl1271 *wl = wlvif->wl;
143
144 mutex_lock(&wl->mutex);
145
146 if (test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags) ||
147 !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) ||
148 (!wl->conf.rx_streaming.always &&
149 !test_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags)))
150 goto out;
151
152 if (!wl->conf.rx_streaming.interval)
153 goto out;
154
155 ret = wl1271_ps_elp_wakeup(wl);
156 if (ret < 0)
157 goto out;
158
159 ret = wl1271_set_rx_streaming(wl, wlvif, true);
160 if (ret < 0)
161 goto out_sleep;
162
163 /* stop it after some time of inactivity */
164 mod_timer(&wlvif->rx_streaming_timer,
165 jiffies + msecs_to_jiffies(wl->conf.rx_streaming.duration));
166
167 out_sleep:
168 wl1271_ps_elp_sleep(wl);
169 out:
170 mutex_unlock(&wl->mutex);
171 }
172
wl1271_rx_streaming_disable_work(struct work_struct * work)173 static void wl1271_rx_streaming_disable_work(struct work_struct *work)
174 {
175 int ret;
176 struct wl12xx_vif *wlvif = container_of(work, struct wl12xx_vif,
177 rx_streaming_disable_work);
178 struct wl1271 *wl = wlvif->wl;
179
180 mutex_lock(&wl->mutex);
181
182 if (!test_bit(WLVIF_FLAG_RX_STREAMING_STARTED, &wlvif->flags))
183 goto out;
184
185 ret = wl1271_ps_elp_wakeup(wl);
186 if (ret < 0)
187 goto out;
188
189 ret = wl1271_set_rx_streaming(wl, wlvif, false);
190 if (ret)
191 goto out_sleep;
192
193 out_sleep:
194 wl1271_ps_elp_sleep(wl);
195 out:
196 mutex_unlock(&wl->mutex);
197 }
198
wl1271_rx_streaming_timer(unsigned long data)199 static void wl1271_rx_streaming_timer(unsigned long data)
200 {
201 struct wl12xx_vif *wlvif = (struct wl12xx_vif *)data;
202 struct wl1271 *wl = wlvif->wl;
203 ieee80211_queue_work(wl->hw, &wlvif->rx_streaming_disable_work);
204 }
205
206 /* wl->mutex must be taken */
wl12xx_rearm_tx_watchdog_locked(struct wl1271 * wl)207 void wl12xx_rearm_tx_watchdog_locked(struct wl1271 *wl)
208 {
209 /* if the watchdog is not armed, don't do anything */
210 if (wl->tx_allocated_blocks == 0)
211 return;
212
213 cancel_delayed_work(&wl->tx_watchdog_work);
214 ieee80211_queue_delayed_work(wl->hw, &wl->tx_watchdog_work,
215 msecs_to_jiffies(wl->conf.tx.tx_watchdog_timeout));
216 }
217
wlcore_rc_update_work(struct work_struct * work)218 static void wlcore_rc_update_work(struct work_struct *work)
219 {
220 int ret;
221 struct wl12xx_vif *wlvif = container_of(work, struct wl12xx_vif,
222 rc_update_work);
223 struct wl1271 *wl = wlvif->wl;
224 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
225
226 mutex_lock(&wl->mutex);
227
228 if (unlikely(wl->state != WLCORE_STATE_ON))
229 goto out;
230
231 ret = wl1271_ps_elp_wakeup(wl);
232 if (ret < 0)
233 goto out;
234
235 if (ieee80211_vif_is_mesh(vif)) {
236 ret = wl1271_acx_set_ht_capabilities(wl, &wlvif->rc_ht_cap,
237 true, wlvif->sta.hlid);
238 if (ret < 0)
239 goto out_sleep;
240 } else {
241 wlcore_hw_sta_rc_update(wl, wlvif);
242 }
243
244 out_sleep:
245 wl1271_ps_elp_sleep(wl);
246 out:
247 mutex_unlock(&wl->mutex);
248 }
249
wl12xx_tx_watchdog_work(struct work_struct * work)250 static void wl12xx_tx_watchdog_work(struct work_struct *work)
251 {
252 struct delayed_work *dwork;
253 struct wl1271 *wl;
254
255 dwork = to_delayed_work(work);
256 wl = container_of(dwork, struct wl1271, tx_watchdog_work);
257
258 mutex_lock(&wl->mutex);
259
260 if (unlikely(wl->state != WLCORE_STATE_ON))
261 goto out;
262
263 /* Tx went out in the meantime - everything is ok */
264 if (unlikely(wl->tx_allocated_blocks == 0))
265 goto out;
266
267 /*
268 * if a ROC is in progress, we might not have any Tx for a long
269 * time (e.g. pending Tx on the non-ROC channels)
270 */
271 if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES) {
272 wl1271_debug(DEBUG_TX, "No Tx (in FW) for %d ms due to ROC",
273 wl->conf.tx.tx_watchdog_timeout);
274 wl12xx_rearm_tx_watchdog_locked(wl);
275 goto out;
276 }
277
278 /*
279 * if a scan is in progress, we might not have any Tx for a long
280 * time
281 */
282 if (wl->scan.state != WL1271_SCAN_STATE_IDLE) {
283 wl1271_debug(DEBUG_TX, "No Tx (in FW) for %d ms due to scan",
284 wl->conf.tx.tx_watchdog_timeout);
285 wl12xx_rearm_tx_watchdog_locked(wl);
286 goto out;
287 }
288
289 /*
290 * AP might cache a frame for a long time for a sleeping station,
291 * so rearm the timer if there's an AP interface with stations. If
292 * Tx is genuinely stuck we will most hopefully discover it when all
293 * stations are removed due to inactivity.
294 */
295 if (wl->active_sta_count) {
296 wl1271_debug(DEBUG_TX, "No Tx (in FW) for %d ms. AP has "
297 " %d stations",
298 wl->conf.tx.tx_watchdog_timeout,
299 wl->active_sta_count);
300 wl12xx_rearm_tx_watchdog_locked(wl);
301 goto out;
302 }
303
304 wl1271_error("Tx stuck (in FW) for %d ms. Starting recovery",
305 wl->conf.tx.tx_watchdog_timeout);
306 wl12xx_queue_recovery_work(wl);
307
308 out:
309 mutex_unlock(&wl->mutex);
310 }
311
wlcore_adjust_conf(struct wl1271 * wl)312 static void wlcore_adjust_conf(struct wl1271 *wl)
313 {
314
315 if (fwlog_param) {
316 if (!strcmp(fwlog_param, "continuous")) {
317 wl->conf.fwlog.mode = WL12XX_FWLOG_CONTINUOUS;
318 wl->conf.fwlog.output = WL12XX_FWLOG_OUTPUT_HOST;
319 } else if (!strcmp(fwlog_param, "dbgpins")) {
320 wl->conf.fwlog.mode = WL12XX_FWLOG_CONTINUOUS;
321 wl->conf.fwlog.output = WL12XX_FWLOG_OUTPUT_DBG_PINS;
322 } else if (!strcmp(fwlog_param, "disable")) {
323 wl->conf.fwlog.mem_blocks = 0;
324 wl->conf.fwlog.output = WL12XX_FWLOG_OUTPUT_NONE;
325 } else {
326 wl1271_error("Unknown fwlog parameter %s", fwlog_param);
327 }
328 }
329
330 if (bug_on_recovery != -1)
331 wl->conf.recovery.bug_on_recovery = (u8) bug_on_recovery;
332
333 if (no_recovery != -1)
334 wl->conf.recovery.no_recovery = (u8) no_recovery;
335 }
336
wl12xx_irq_ps_regulate_link(struct wl1271 * wl,struct wl12xx_vif * wlvif,u8 hlid,u8 tx_pkts)337 static void wl12xx_irq_ps_regulate_link(struct wl1271 *wl,
338 struct wl12xx_vif *wlvif,
339 u8 hlid, u8 tx_pkts)
340 {
341 bool fw_ps;
342
343 fw_ps = test_bit(hlid, &wl->ap_fw_ps_map);
344
345 /*
346 * Wake up from high level PS if the STA is asleep with too little
347 * packets in FW or if the STA is awake.
348 */
349 if (!fw_ps || tx_pkts < WL1271_PS_STA_MAX_PACKETS)
350 wl12xx_ps_link_end(wl, wlvif, hlid);
351
352 /*
353 * Start high-level PS if the STA is asleep with enough blocks in FW.
354 * Make an exception if this is the only connected link. In this
355 * case FW-memory congestion is less of a problem.
356 * Note that a single connected STA means 2*ap_count + 1 active links,
357 * since we must account for the global and broadcast AP links
358 * for each AP. The "fw_ps" check assures us the other link is a STA
359 * connected to the AP. Otherwise the FW would not set the PSM bit.
360 */
361 else if (wl->active_link_count > (wl->ap_count*2 + 1) && fw_ps &&
362 tx_pkts >= WL1271_PS_STA_MAX_PACKETS)
363 wl12xx_ps_link_start(wl, wlvif, hlid, true);
364 }
365
wl12xx_irq_update_links_status(struct wl1271 * wl,struct wl12xx_vif * wlvif,struct wl_fw_status * status)366 static void wl12xx_irq_update_links_status(struct wl1271 *wl,
367 struct wl12xx_vif *wlvif,
368 struct wl_fw_status *status)
369 {
370 unsigned long cur_fw_ps_map;
371 u8 hlid;
372
373 cur_fw_ps_map = status->link_ps_bitmap;
374 if (wl->ap_fw_ps_map != cur_fw_ps_map) {
375 wl1271_debug(DEBUG_PSM,
376 "link ps prev 0x%lx cur 0x%lx changed 0x%lx",
377 wl->ap_fw_ps_map, cur_fw_ps_map,
378 wl->ap_fw_ps_map ^ cur_fw_ps_map);
379
380 wl->ap_fw_ps_map = cur_fw_ps_map;
381 }
382
383 for_each_set_bit(hlid, wlvif->ap.sta_hlid_map, wl->num_links)
384 wl12xx_irq_ps_regulate_link(wl, wlvif, hlid,
385 wl->links[hlid].allocated_pkts);
386 }
387
wlcore_fw_status(struct wl1271 * wl,struct wl_fw_status * status)388 static int wlcore_fw_status(struct wl1271 *wl, struct wl_fw_status *status)
389 {
390 struct wl12xx_vif *wlvif;
391 struct timespec ts;
392 u32 old_tx_blk_count = wl->tx_blocks_available;
393 int avail, freed_blocks;
394 int i;
395 int ret;
396 struct wl1271_link *lnk;
397
398 ret = wlcore_raw_read_data(wl, REG_RAW_FW_STATUS_ADDR,
399 wl->raw_fw_status,
400 wl->fw_status_len, false);
401 if (ret < 0)
402 return ret;
403
404 wlcore_hw_convert_fw_status(wl, wl->raw_fw_status, wl->fw_status);
405
406 wl1271_debug(DEBUG_IRQ, "intr: 0x%x (fw_rx_counter = %d, "
407 "drv_rx_counter = %d, tx_results_counter = %d)",
408 status->intr,
409 status->fw_rx_counter,
410 status->drv_rx_counter,
411 status->tx_results_counter);
412
413 for (i = 0; i < NUM_TX_QUEUES; i++) {
414 /* prevent wrap-around in freed-packets counter */
415 wl->tx_allocated_pkts[i] -=
416 (status->counters.tx_released_pkts[i] -
417 wl->tx_pkts_freed[i]) & 0xff;
418
419 wl->tx_pkts_freed[i] = status->counters.tx_released_pkts[i];
420 }
421
422
423 for_each_set_bit(i, wl->links_map, wl->num_links) {
424 u8 diff;
425 lnk = &wl->links[i];
426
427 /* prevent wrap-around in freed-packets counter */
428 diff = (status->counters.tx_lnk_free_pkts[i] -
429 lnk->prev_freed_pkts) & 0xff;
430
431 if (diff == 0)
432 continue;
433
434 lnk->allocated_pkts -= diff;
435 lnk->prev_freed_pkts = status->counters.tx_lnk_free_pkts[i];
436
437 /* accumulate the prev_freed_pkts counter */
438 lnk->total_freed_pkts += diff;
439 }
440
441 /* prevent wrap-around in total blocks counter */
442 if (likely(wl->tx_blocks_freed <= status->total_released_blks))
443 freed_blocks = status->total_released_blks -
444 wl->tx_blocks_freed;
445 else
446 freed_blocks = 0x100000000LL - wl->tx_blocks_freed +
447 status->total_released_blks;
448
449 wl->tx_blocks_freed = status->total_released_blks;
450
451 wl->tx_allocated_blocks -= freed_blocks;
452
453 /*
454 * If the FW freed some blocks:
455 * If we still have allocated blocks - re-arm the timer, Tx is
456 * not stuck. Otherwise, cancel the timer (no Tx currently).
457 */
458 if (freed_blocks) {
459 if (wl->tx_allocated_blocks)
460 wl12xx_rearm_tx_watchdog_locked(wl);
461 else
462 cancel_delayed_work(&wl->tx_watchdog_work);
463 }
464
465 avail = status->tx_total - wl->tx_allocated_blocks;
466
467 /*
468 * The FW might change the total number of TX memblocks before
469 * we get a notification about blocks being released. Thus, the
470 * available blocks calculation might yield a temporary result
471 * which is lower than the actual available blocks. Keeping in
472 * mind that only blocks that were allocated can be moved from
473 * TX to RX, tx_blocks_available should never decrease here.
474 */
475 wl->tx_blocks_available = max((int)wl->tx_blocks_available,
476 avail);
477
478 /* if more blocks are available now, tx work can be scheduled */
479 if (wl->tx_blocks_available > old_tx_blk_count)
480 clear_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags);
481
482 /* for AP update num of allocated TX blocks per link and ps status */
483 wl12xx_for_each_wlvif_ap(wl, wlvif) {
484 wl12xx_irq_update_links_status(wl, wlvif, status);
485 }
486
487 /* update the host-chipset time offset */
488 getnstimeofday(&ts);
489 wl->time_offset = (timespec_to_ns(&ts) >> 10) -
490 (s64)(status->fw_localtime);
491
492 wl->fw_fast_lnk_map = status->link_fast_bitmap;
493
494 return 0;
495 }
496
wl1271_flush_deferred_work(struct wl1271 * wl)497 static void wl1271_flush_deferred_work(struct wl1271 *wl)
498 {
499 struct sk_buff *skb;
500
501 /* Pass all received frames to the network stack */
502 while ((skb = skb_dequeue(&wl->deferred_rx_queue)))
503 ieee80211_rx_ni(wl->hw, skb);
504
505 /* Return sent skbs to the network stack */
506 while ((skb = skb_dequeue(&wl->deferred_tx_queue)))
507 ieee80211_tx_status_ni(wl->hw, skb);
508 }
509
wl1271_netstack_work(struct work_struct * work)510 static void wl1271_netstack_work(struct work_struct *work)
511 {
512 struct wl1271 *wl =
513 container_of(work, struct wl1271, netstack_work);
514
515 do {
516 wl1271_flush_deferred_work(wl);
517 } while (skb_queue_len(&wl->deferred_rx_queue));
518 }
519
520 #define WL1271_IRQ_MAX_LOOPS 256
521
wlcore_irq_locked(struct wl1271 * wl)522 static int wlcore_irq_locked(struct wl1271 *wl)
523 {
524 int ret = 0;
525 u32 intr;
526 int loopcount = WL1271_IRQ_MAX_LOOPS;
527 bool done = false;
528 unsigned int defer_count;
529 unsigned long flags;
530
531 /*
532 * In case edge triggered interrupt must be used, we cannot iterate
533 * more than once without introducing race conditions with the hardirq.
534 */
535 if (wl->irq_flags & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING))
536 loopcount = 1;
537
538 wl1271_debug(DEBUG_IRQ, "IRQ work");
539
540 if (unlikely(wl->state != WLCORE_STATE_ON))
541 goto out;
542
543 ret = wl1271_ps_elp_wakeup(wl);
544 if (ret < 0)
545 goto out;
546
547 while (!done && loopcount--) {
548 /*
549 * In order to avoid a race with the hardirq, clear the flag
550 * before acknowledging the chip. Since the mutex is held,
551 * wl1271_ps_elp_wakeup cannot be called concurrently.
552 */
553 clear_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
554 smp_mb__after_atomic();
555
556 ret = wlcore_fw_status(wl, wl->fw_status);
557 if (ret < 0)
558 goto out;
559
560 wlcore_hw_tx_immediate_compl(wl);
561
562 intr = wl->fw_status->intr;
563 intr &= WLCORE_ALL_INTR_MASK;
564 if (!intr) {
565 done = true;
566 continue;
567 }
568
569 if (unlikely(intr & WL1271_ACX_INTR_WATCHDOG)) {
570 wl1271_error("HW watchdog interrupt received! starting recovery.");
571 wl->watchdog_recovery = true;
572 ret = -EIO;
573
574 /* restarting the chip. ignore any other interrupt. */
575 goto out;
576 }
577
578 if (unlikely(intr & WL1271_ACX_SW_INTR_WATCHDOG)) {
579 wl1271_error("SW watchdog interrupt received! "
580 "starting recovery.");
581 wl->watchdog_recovery = true;
582 ret = -EIO;
583
584 /* restarting the chip. ignore any other interrupt. */
585 goto out;
586 }
587
588 if (likely(intr & WL1271_ACX_INTR_DATA)) {
589 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_DATA");
590
591 ret = wlcore_rx(wl, wl->fw_status);
592 if (ret < 0)
593 goto out;
594
595 /* Check if any tx blocks were freed */
596 spin_lock_irqsave(&wl->wl_lock, flags);
597 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
598 wl1271_tx_total_queue_count(wl) > 0) {
599 spin_unlock_irqrestore(&wl->wl_lock, flags);
600 /*
601 * In order to avoid starvation of the TX path,
602 * call the work function directly.
603 */
604 ret = wlcore_tx_work_locked(wl);
605 if (ret < 0)
606 goto out;
607 } else {
608 spin_unlock_irqrestore(&wl->wl_lock, flags);
609 }
610
611 /* check for tx results */
612 ret = wlcore_hw_tx_delayed_compl(wl);
613 if (ret < 0)
614 goto out;
615
616 /* Make sure the deferred queues don't get too long */
617 defer_count = skb_queue_len(&wl->deferred_tx_queue) +
618 skb_queue_len(&wl->deferred_rx_queue);
619 if (defer_count > WL1271_DEFERRED_QUEUE_LIMIT)
620 wl1271_flush_deferred_work(wl);
621 }
622
623 if (intr & WL1271_ACX_INTR_EVENT_A) {
624 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_EVENT_A");
625 ret = wl1271_event_handle(wl, 0);
626 if (ret < 0)
627 goto out;
628 }
629
630 if (intr & WL1271_ACX_INTR_EVENT_B) {
631 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_EVENT_B");
632 ret = wl1271_event_handle(wl, 1);
633 if (ret < 0)
634 goto out;
635 }
636
637 if (intr & WL1271_ACX_INTR_INIT_COMPLETE)
638 wl1271_debug(DEBUG_IRQ,
639 "WL1271_ACX_INTR_INIT_COMPLETE");
640
641 if (intr & WL1271_ACX_INTR_HW_AVAILABLE)
642 wl1271_debug(DEBUG_IRQ, "WL1271_ACX_INTR_HW_AVAILABLE");
643 }
644
645 wl1271_ps_elp_sleep(wl);
646
647 out:
648 return ret;
649 }
650
wlcore_irq(int irq,void * cookie)651 static irqreturn_t wlcore_irq(int irq, void *cookie)
652 {
653 int ret;
654 unsigned long flags;
655 struct wl1271 *wl = cookie;
656
657 /* complete the ELP completion */
658 spin_lock_irqsave(&wl->wl_lock, flags);
659 set_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
660 if (wl->elp_compl) {
661 complete(wl->elp_compl);
662 wl->elp_compl = NULL;
663 }
664
665 if (test_bit(WL1271_FLAG_SUSPENDED, &wl->flags)) {
666 /* don't enqueue a work right now. mark it as pending */
667 set_bit(WL1271_FLAG_PENDING_WORK, &wl->flags);
668 wl1271_debug(DEBUG_IRQ, "should not enqueue work");
669 disable_irq_nosync(wl->irq);
670 pm_wakeup_event(wl->dev, 0);
671 spin_unlock_irqrestore(&wl->wl_lock, flags);
672 return IRQ_HANDLED;
673 }
674 spin_unlock_irqrestore(&wl->wl_lock, flags);
675
676 /* TX might be handled here, avoid redundant work */
677 set_bit(WL1271_FLAG_TX_PENDING, &wl->flags);
678 cancel_work_sync(&wl->tx_work);
679
680 mutex_lock(&wl->mutex);
681
682 ret = wlcore_irq_locked(wl);
683 if (ret)
684 wl12xx_queue_recovery_work(wl);
685
686 spin_lock_irqsave(&wl->wl_lock, flags);
687 /* In case TX was not handled here, queue TX work */
688 clear_bit(WL1271_FLAG_TX_PENDING, &wl->flags);
689 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
690 wl1271_tx_total_queue_count(wl) > 0)
691 ieee80211_queue_work(wl->hw, &wl->tx_work);
692 spin_unlock_irqrestore(&wl->wl_lock, flags);
693
694 mutex_unlock(&wl->mutex);
695
696 return IRQ_HANDLED;
697 }
698
699 struct vif_counter_data {
700 u8 counter;
701
702 struct ieee80211_vif *cur_vif;
703 bool cur_vif_running;
704 };
705
wl12xx_vif_count_iter(void * data,u8 * mac,struct ieee80211_vif * vif)706 static void wl12xx_vif_count_iter(void *data, u8 *mac,
707 struct ieee80211_vif *vif)
708 {
709 struct vif_counter_data *counter = data;
710
711 counter->counter++;
712 if (counter->cur_vif == vif)
713 counter->cur_vif_running = true;
714 }
715
716 /* caller must not hold wl->mutex, as it might deadlock */
wl12xx_get_vif_count(struct ieee80211_hw * hw,struct ieee80211_vif * cur_vif,struct vif_counter_data * data)717 static void wl12xx_get_vif_count(struct ieee80211_hw *hw,
718 struct ieee80211_vif *cur_vif,
719 struct vif_counter_data *data)
720 {
721 memset(data, 0, sizeof(*data));
722 data->cur_vif = cur_vif;
723
724 ieee80211_iterate_active_interfaces(hw, IEEE80211_IFACE_ITER_RESUME_ALL,
725 wl12xx_vif_count_iter, data);
726 }
727
wl12xx_fetch_firmware(struct wl1271 * wl,bool plt)728 static int wl12xx_fetch_firmware(struct wl1271 *wl, bool plt)
729 {
730 const struct firmware *fw;
731 const char *fw_name;
732 enum wl12xx_fw_type fw_type;
733 int ret;
734
735 if (plt) {
736 fw_type = WL12XX_FW_TYPE_PLT;
737 fw_name = wl->plt_fw_name;
738 } else {
739 /*
740 * we can't call wl12xx_get_vif_count() here because
741 * wl->mutex is taken, so use the cached last_vif_count value
742 */
743 if (wl->last_vif_count > 1 && wl->mr_fw_name) {
744 fw_type = WL12XX_FW_TYPE_MULTI;
745 fw_name = wl->mr_fw_name;
746 } else {
747 fw_type = WL12XX_FW_TYPE_NORMAL;
748 fw_name = wl->sr_fw_name;
749 }
750 }
751
752 if (wl->fw_type == fw_type)
753 return 0;
754
755 wl1271_debug(DEBUG_BOOT, "booting firmware %s", fw_name);
756
757 ret = request_firmware(&fw, fw_name, wl->dev);
758
759 if (ret < 0) {
760 wl1271_error("could not get firmware %s: %d", fw_name, ret);
761 return ret;
762 }
763
764 if (fw->size % 4) {
765 wl1271_error("firmware size is not multiple of 32 bits: %zu",
766 fw->size);
767 ret = -EILSEQ;
768 goto out;
769 }
770
771 vfree(wl->fw);
772 wl->fw_type = WL12XX_FW_TYPE_NONE;
773 wl->fw_len = fw->size;
774 wl->fw = vmalloc(wl->fw_len);
775
776 if (!wl->fw) {
777 wl1271_error("could not allocate memory for the firmware");
778 ret = -ENOMEM;
779 goto out;
780 }
781
782 memcpy(wl->fw, fw->data, wl->fw_len);
783 ret = 0;
784 wl->fw_type = fw_type;
785 out:
786 release_firmware(fw);
787
788 return ret;
789 }
790
wl12xx_queue_recovery_work(struct wl1271 * wl)791 void wl12xx_queue_recovery_work(struct wl1271 *wl)
792 {
793 /* Avoid a recursive recovery */
794 if (wl->state == WLCORE_STATE_ON) {
795 WARN_ON(!test_bit(WL1271_FLAG_INTENDED_FW_RECOVERY,
796 &wl->flags));
797
798 wl->state = WLCORE_STATE_RESTARTING;
799 set_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags);
800 wl1271_ps_elp_wakeup(wl);
801 wlcore_disable_interrupts_nosync(wl);
802 ieee80211_queue_work(wl->hw, &wl->recovery_work);
803 }
804 }
805
wl12xx_copy_fwlog(struct wl1271 * wl,u8 * memblock,size_t maxlen)806 size_t wl12xx_copy_fwlog(struct wl1271 *wl, u8 *memblock, size_t maxlen)
807 {
808 size_t len;
809
810 /* Make sure we have enough room */
811 len = min_t(size_t, maxlen, PAGE_SIZE - wl->fwlog_size);
812
813 /* Fill the FW log file, consumed by the sysfs fwlog entry */
814 memcpy(wl->fwlog + wl->fwlog_size, memblock, len);
815 wl->fwlog_size += len;
816
817 return len;
818 }
819
wl12xx_read_fwlog_panic(struct wl1271 * wl)820 static void wl12xx_read_fwlog_panic(struct wl1271 *wl)
821 {
822 u32 end_of_log = 0;
823
824 if (wl->quirks & WLCORE_QUIRK_FWLOG_NOT_IMPLEMENTED)
825 return;
826
827 wl1271_info("Reading FW panic log");
828
829 /*
830 * Make sure the chip is awake and the logger isn't active.
831 * Do not send a stop fwlog command if the fw is hanged or if
832 * dbgpins are used (due to some fw bug).
833 */
834 if (wl1271_ps_elp_wakeup(wl))
835 return;
836 if (!wl->watchdog_recovery &&
837 wl->conf.fwlog.output != WL12XX_FWLOG_OUTPUT_DBG_PINS)
838 wl12xx_cmd_stop_fwlog(wl);
839
840 /* Traverse the memory blocks linked list */
841 do {
842 end_of_log = wlcore_event_fw_logger(wl);
843 if (end_of_log == 0) {
844 msleep(100);
845 end_of_log = wlcore_event_fw_logger(wl);
846 }
847 } while (end_of_log != 0);
848 }
849
wlcore_save_freed_pkts(struct wl1271 * wl,struct wl12xx_vif * wlvif,u8 hlid,struct ieee80211_sta * sta)850 static void wlcore_save_freed_pkts(struct wl1271 *wl, struct wl12xx_vif *wlvif,
851 u8 hlid, struct ieee80211_sta *sta)
852 {
853 struct wl1271_station *wl_sta;
854 u32 sqn_recovery_padding = WL1271_TX_SQN_POST_RECOVERY_PADDING;
855
856 wl_sta = (void *)sta->drv_priv;
857 wl_sta->total_freed_pkts = wl->links[hlid].total_freed_pkts;
858
859 /*
860 * increment the initial seq number on recovery to account for
861 * transmitted packets that we haven't yet got in the FW status
862 */
863 if (wlvif->encryption_type == KEY_GEM)
864 sqn_recovery_padding = WL1271_TX_SQN_POST_RECOVERY_PADDING_GEM;
865
866 if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags))
867 wl_sta->total_freed_pkts += sqn_recovery_padding;
868 }
869
wlcore_save_freed_pkts_addr(struct wl1271 * wl,struct wl12xx_vif * wlvif,u8 hlid,const u8 * addr)870 static void wlcore_save_freed_pkts_addr(struct wl1271 *wl,
871 struct wl12xx_vif *wlvif,
872 u8 hlid, const u8 *addr)
873 {
874 struct ieee80211_sta *sta;
875 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
876
877 if (WARN_ON(hlid == WL12XX_INVALID_LINK_ID ||
878 is_zero_ether_addr(addr)))
879 return;
880
881 rcu_read_lock();
882 sta = ieee80211_find_sta(vif, addr);
883 if (sta)
884 wlcore_save_freed_pkts(wl, wlvif, hlid, sta);
885 rcu_read_unlock();
886 }
887
wlcore_print_recovery(struct wl1271 * wl)888 static void wlcore_print_recovery(struct wl1271 *wl)
889 {
890 u32 pc = 0;
891 u32 hint_sts = 0;
892 int ret;
893
894 wl1271_info("Hardware recovery in progress. FW ver: %s",
895 wl->chip.fw_ver_str);
896
897 /* change partitions momentarily so we can read the FW pc */
898 ret = wlcore_set_partition(wl, &wl->ptable[PART_BOOT]);
899 if (ret < 0)
900 return;
901
902 ret = wlcore_read_reg(wl, REG_PC_ON_RECOVERY, &pc);
903 if (ret < 0)
904 return;
905
906 ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &hint_sts);
907 if (ret < 0)
908 return;
909
910 wl1271_info("pc: 0x%x, hint_sts: 0x%08x count: %d",
911 pc, hint_sts, ++wl->recovery_count);
912
913 wlcore_set_partition(wl, &wl->ptable[PART_WORK]);
914 }
915
916
wl1271_recovery_work(struct work_struct * work)917 static void wl1271_recovery_work(struct work_struct *work)
918 {
919 struct wl1271 *wl =
920 container_of(work, struct wl1271, recovery_work);
921 struct wl12xx_vif *wlvif;
922 struct ieee80211_vif *vif;
923
924 mutex_lock(&wl->mutex);
925
926 if (wl->state == WLCORE_STATE_OFF || wl->plt)
927 goto out_unlock;
928
929 if (!test_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags)) {
930 if (wl->conf.fwlog.output == WL12XX_FWLOG_OUTPUT_HOST)
931 wl12xx_read_fwlog_panic(wl);
932 wlcore_print_recovery(wl);
933 }
934
935 BUG_ON(wl->conf.recovery.bug_on_recovery &&
936 !test_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags));
937
938 if (wl->conf.recovery.no_recovery) {
939 wl1271_info("No recovery (chosen on module load). Fw will remain stuck.");
940 goto out_unlock;
941 }
942
943 /* Prevent spurious TX during FW restart */
944 wlcore_stop_queues(wl, WLCORE_QUEUE_STOP_REASON_FW_RESTART);
945
946 /* reboot the chipset */
947 while (!list_empty(&wl->wlvif_list)) {
948 wlvif = list_first_entry(&wl->wlvif_list,
949 struct wl12xx_vif, list);
950 vif = wl12xx_wlvif_to_vif(wlvif);
951
952 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
953 test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
954 wlcore_save_freed_pkts_addr(wl, wlvif, wlvif->sta.hlid,
955 vif->bss_conf.bssid);
956 }
957
958 __wl1271_op_remove_interface(wl, vif, false);
959 }
960
961 wlcore_op_stop_locked(wl);
962
963 ieee80211_restart_hw(wl->hw);
964
965 /*
966 * Its safe to enable TX now - the queues are stopped after a request
967 * to restart the HW.
968 */
969 wlcore_wake_queues(wl, WLCORE_QUEUE_STOP_REASON_FW_RESTART);
970
971 out_unlock:
972 wl->watchdog_recovery = false;
973 clear_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags);
974 mutex_unlock(&wl->mutex);
975 }
976
wlcore_fw_wakeup(struct wl1271 * wl)977 static int wlcore_fw_wakeup(struct wl1271 *wl)
978 {
979 return wlcore_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG, ELPCTRL_WAKE_UP);
980 }
981
wl1271_setup(struct wl1271 * wl)982 static int wl1271_setup(struct wl1271 *wl)
983 {
984 wl->raw_fw_status = kzalloc(wl->fw_status_len, GFP_KERNEL);
985 if (!wl->raw_fw_status)
986 goto err;
987
988 wl->fw_status = kzalloc(sizeof(*wl->fw_status), GFP_KERNEL);
989 if (!wl->fw_status)
990 goto err;
991
992 wl->tx_res_if = kzalloc(sizeof(*wl->tx_res_if), GFP_KERNEL);
993 if (!wl->tx_res_if)
994 goto err;
995
996 return 0;
997 err:
998 kfree(wl->fw_status);
999 kfree(wl->raw_fw_status);
1000 return -ENOMEM;
1001 }
1002
wl12xx_set_power_on(struct wl1271 * wl)1003 static int wl12xx_set_power_on(struct wl1271 *wl)
1004 {
1005 int ret;
1006
1007 msleep(WL1271_PRE_POWER_ON_SLEEP);
1008 ret = wl1271_power_on(wl);
1009 if (ret < 0)
1010 goto out;
1011 msleep(WL1271_POWER_ON_SLEEP);
1012 wl1271_io_reset(wl);
1013 wl1271_io_init(wl);
1014
1015 ret = wlcore_set_partition(wl, &wl->ptable[PART_BOOT]);
1016 if (ret < 0)
1017 goto fail;
1018
1019 /* ELP module wake up */
1020 ret = wlcore_fw_wakeup(wl);
1021 if (ret < 0)
1022 goto fail;
1023
1024 out:
1025 return ret;
1026
1027 fail:
1028 wl1271_power_off(wl);
1029 return ret;
1030 }
1031
wl12xx_chip_wakeup(struct wl1271 * wl,bool plt)1032 static int wl12xx_chip_wakeup(struct wl1271 *wl, bool plt)
1033 {
1034 int ret = 0;
1035
1036 ret = wl12xx_set_power_on(wl);
1037 if (ret < 0)
1038 goto out;
1039
1040 /*
1041 * For wl127x based devices we could use the default block
1042 * size (512 bytes), but due to a bug in the sdio driver, we
1043 * need to set it explicitly after the chip is powered on. To
1044 * simplify the code and since the performance impact is
1045 * negligible, we use the same block size for all different
1046 * chip types.
1047 *
1048 * Check if the bus supports blocksize alignment and, if it
1049 * doesn't, make sure we don't have the quirk.
1050 */
1051 if (!wl1271_set_block_size(wl))
1052 wl->quirks &= ~WLCORE_QUIRK_TX_BLOCKSIZE_ALIGN;
1053
1054 /* TODO: make sure the lower driver has set things up correctly */
1055
1056 ret = wl1271_setup(wl);
1057 if (ret < 0)
1058 goto out;
1059
1060 ret = wl12xx_fetch_firmware(wl, plt);
1061 if (ret < 0) {
1062 kfree(wl->fw_status);
1063 kfree(wl->raw_fw_status);
1064 kfree(wl->tx_res_if);
1065 }
1066
1067 out:
1068 return ret;
1069 }
1070
wl1271_plt_start(struct wl1271 * wl,const enum plt_mode plt_mode)1071 int wl1271_plt_start(struct wl1271 *wl, const enum plt_mode plt_mode)
1072 {
1073 int retries = WL1271_BOOT_RETRIES;
1074 struct wiphy *wiphy = wl->hw->wiphy;
1075
1076 static const char* const PLT_MODE[] = {
1077 "PLT_OFF",
1078 "PLT_ON",
1079 "PLT_FEM_DETECT",
1080 "PLT_CHIP_AWAKE"
1081 };
1082
1083 int ret;
1084
1085 mutex_lock(&wl->mutex);
1086
1087 wl1271_notice("power up");
1088
1089 if (wl->state != WLCORE_STATE_OFF) {
1090 wl1271_error("cannot go into PLT state because not "
1091 "in off state: %d", wl->state);
1092 ret = -EBUSY;
1093 goto out;
1094 }
1095
1096 /* Indicate to lower levels that we are now in PLT mode */
1097 wl->plt = true;
1098 wl->plt_mode = plt_mode;
1099
1100 while (retries) {
1101 retries--;
1102 ret = wl12xx_chip_wakeup(wl, true);
1103 if (ret < 0)
1104 goto power_off;
1105
1106 if (plt_mode != PLT_CHIP_AWAKE) {
1107 ret = wl->ops->plt_init(wl);
1108 if (ret < 0)
1109 goto power_off;
1110 }
1111
1112 wl->state = WLCORE_STATE_ON;
1113 wl1271_notice("firmware booted in PLT mode %s (%s)",
1114 PLT_MODE[plt_mode],
1115 wl->chip.fw_ver_str);
1116
1117 /* update hw/fw version info in wiphy struct */
1118 wiphy->hw_version = wl->chip.id;
1119 strncpy(wiphy->fw_version, wl->chip.fw_ver_str,
1120 sizeof(wiphy->fw_version));
1121
1122 goto out;
1123
1124 power_off:
1125 wl1271_power_off(wl);
1126 }
1127
1128 wl->plt = false;
1129 wl->plt_mode = PLT_OFF;
1130
1131 wl1271_error("firmware boot in PLT mode failed despite %d retries",
1132 WL1271_BOOT_RETRIES);
1133 out:
1134 mutex_unlock(&wl->mutex);
1135
1136 return ret;
1137 }
1138
wl1271_plt_stop(struct wl1271 * wl)1139 int wl1271_plt_stop(struct wl1271 *wl)
1140 {
1141 int ret = 0;
1142
1143 wl1271_notice("power down");
1144
1145 /*
1146 * Interrupts must be disabled before setting the state to OFF.
1147 * Otherwise, the interrupt handler might be called and exit without
1148 * reading the interrupt status.
1149 */
1150 wlcore_disable_interrupts(wl);
1151 mutex_lock(&wl->mutex);
1152 if (!wl->plt) {
1153 mutex_unlock(&wl->mutex);
1154
1155 /*
1156 * This will not necessarily enable interrupts as interrupts
1157 * may have been disabled when op_stop was called. It will,
1158 * however, balance the above call to disable_interrupts().
1159 */
1160 wlcore_enable_interrupts(wl);
1161
1162 wl1271_error("cannot power down because not in PLT "
1163 "state: %d", wl->state);
1164 ret = -EBUSY;
1165 goto out;
1166 }
1167
1168 mutex_unlock(&wl->mutex);
1169
1170 wl1271_flush_deferred_work(wl);
1171 cancel_work_sync(&wl->netstack_work);
1172 cancel_work_sync(&wl->recovery_work);
1173 cancel_delayed_work_sync(&wl->elp_work);
1174 cancel_delayed_work_sync(&wl->tx_watchdog_work);
1175
1176 mutex_lock(&wl->mutex);
1177 wl1271_power_off(wl);
1178 wl->flags = 0;
1179 wl->sleep_auth = WL1271_PSM_ILLEGAL;
1180 wl->state = WLCORE_STATE_OFF;
1181 wl->plt = false;
1182 wl->plt_mode = PLT_OFF;
1183 wl->rx_counter = 0;
1184 mutex_unlock(&wl->mutex);
1185
1186 out:
1187 return ret;
1188 }
1189
wl1271_op_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)1190 static void wl1271_op_tx(struct ieee80211_hw *hw,
1191 struct ieee80211_tx_control *control,
1192 struct sk_buff *skb)
1193 {
1194 struct wl1271 *wl = hw->priv;
1195 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1196 struct ieee80211_vif *vif = info->control.vif;
1197 struct wl12xx_vif *wlvif = NULL;
1198 unsigned long flags;
1199 int q, mapping;
1200 u8 hlid;
1201
1202 if (!vif) {
1203 wl1271_debug(DEBUG_TX, "DROP skb with no vif");
1204 ieee80211_free_txskb(hw, skb);
1205 return;
1206 }
1207
1208 wlvif = wl12xx_vif_to_data(vif);
1209 mapping = skb_get_queue_mapping(skb);
1210 q = wl1271_tx_get_queue(mapping);
1211
1212 hlid = wl12xx_tx_get_hlid(wl, wlvif, skb, control->sta);
1213
1214 spin_lock_irqsave(&wl->wl_lock, flags);
1215
1216 /*
1217 * drop the packet if the link is invalid or the queue is stopped
1218 * for any reason but watermark. Watermark is a "soft"-stop so we
1219 * allow these packets through.
1220 */
1221 if (hlid == WL12XX_INVALID_LINK_ID ||
1222 (!test_bit(hlid, wlvif->links_map)) ||
1223 (wlcore_is_queue_stopped_locked(wl, wlvif, q) &&
1224 !wlcore_is_queue_stopped_by_reason_locked(wl, wlvif, q,
1225 WLCORE_QUEUE_STOP_REASON_WATERMARK))) {
1226 wl1271_debug(DEBUG_TX, "DROP skb hlid %d q %d", hlid, q);
1227 ieee80211_free_txskb(hw, skb);
1228 goto out;
1229 }
1230
1231 wl1271_debug(DEBUG_TX, "queue skb hlid %d q %d len %d",
1232 hlid, q, skb->len);
1233 skb_queue_tail(&wl->links[hlid].tx_queue[q], skb);
1234
1235 wl->tx_queue_count[q]++;
1236 wlvif->tx_queue_count[q]++;
1237
1238 /*
1239 * The workqueue is slow to process the tx_queue and we need stop
1240 * the queue here, otherwise the queue will get too long.
1241 */
1242 if (wlvif->tx_queue_count[q] >= WL1271_TX_QUEUE_HIGH_WATERMARK &&
1243 !wlcore_is_queue_stopped_by_reason_locked(wl, wlvif, q,
1244 WLCORE_QUEUE_STOP_REASON_WATERMARK)) {
1245 wl1271_debug(DEBUG_TX, "op_tx: stopping queues for q %d", q);
1246 wlcore_stop_queue_locked(wl, wlvif, q,
1247 WLCORE_QUEUE_STOP_REASON_WATERMARK);
1248 }
1249
1250 /*
1251 * The chip specific setup must run before the first TX packet -
1252 * before that, the tx_work will not be initialized!
1253 */
1254
1255 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags) &&
1256 !test_bit(WL1271_FLAG_TX_PENDING, &wl->flags))
1257 ieee80211_queue_work(wl->hw, &wl->tx_work);
1258
1259 out:
1260 spin_unlock_irqrestore(&wl->wl_lock, flags);
1261 }
1262
wl1271_tx_dummy_packet(struct wl1271 * wl)1263 int wl1271_tx_dummy_packet(struct wl1271 *wl)
1264 {
1265 unsigned long flags;
1266 int q;
1267
1268 /* no need to queue a new dummy packet if one is already pending */
1269 if (test_bit(WL1271_FLAG_DUMMY_PACKET_PENDING, &wl->flags))
1270 return 0;
1271
1272 q = wl1271_tx_get_queue(skb_get_queue_mapping(wl->dummy_packet));
1273
1274 spin_lock_irqsave(&wl->wl_lock, flags);
1275 set_bit(WL1271_FLAG_DUMMY_PACKET_PENDING, &wl->flags);
1276 wl->tx_queue_count[q]++;
1277 spin_unlock_irqrestore(&wl->wl_lock, flags);
1278
1279 /* The FW is low on RX memory blocks, so send the dummy packet asap */
1280 if (!test_bit(WL1271_FLAG_FW_TX_BUSY, &wl->flags))
1281 return wlcore_tx_work_locked(wl);
1282
1283 /*
1284 * If the FW TX is busy, TX work will be scheduled by the threaded
1285 * interrupt handler function
1286 */
1287 return 0;
1288 }
1289
1290 /*
1291 * The size of the dummy packet should be at least 1400 bytes. However, in
1292 * order to minimize the number of bus transactions, aligning it to 512 bytes
1293 * boundaries could be beneficial, performance wise
1294 */
1295 #define TOTAL_TX_DUMMY_PACKET_SIZE (ALIGN(1400, 512))
1296
wl12xx_alloc_dummy_packet(struct wl1271 * wl)1297 static struct sk_buff *wl12xx_alloc_dummy_packet(struct wl1271 *wl)
1298 {
1299 struct sk_buff *skb;
1300 struct ieee80211_hdr_3addr *hdr;
1301 unsigned int dummy_packet_size;
1302
1303 dummy_packet_size = TOTAL_TX_DUMMY_PACKET_SIZE -
1304 sizeof(struct wl1271_tx_hw_descr) - sizeof(*hdr);
1305
1306 skb = dev_alloc_skb(TOTAL_TX_DUMMY_PACKET_SIZE);
1307 if (!skb) {
1308 wl1271_warning("Failed to allocate a dummy packet skb");
1309 return NULL;
1310 }
1311
1312 skb_reserve(skb, sizeof(struct wl1271_tx_hw_descr));
1313
1314 hdr = skb_put_zero(skb, sizeof(*hdr));
1315 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
1316 IEEE80211_STYPE_NULLFUNC |
1317 IEEE80211_FCTL_TODS);
1318
1319 skb_put_zero(skb, dummy_packet_size);
1320
1321 /* Dummy packets require the TID to be management */
1322 skb->priority = WL1271_TID_MGMT;
1323
1324 /* Initialize all fields that might be used */
1325 skb_set_queue_mapping(skb, 0);
1326 memset(IEEE80211_SKB_CB(skb), 0, sizeof(struct ieee80211_tx_info));
1327
1328 return skb;
1329 }
1330
1331
1332 #ifdef CONFIG_PM
1333 static int
wl1271_validate_wowlan_pattern(struct cfg80211_pkt_pattern * p)1334 wl1271_validate_wowlan_pattern(struct cfg80211_pkt_pattern *p)
1335 {
1336 int num_fields = 0, in_field = 0, fields_size = 0;
1337 int i, pattern_len = 0;
1338
1339 if (!p->mask) {
1340 wl1271_warning("No mask in WoWLAN pattern");
1341 return -EINVAL;
1342 }
1343
1344 /*
1345 * The pattern is broken up into segments of bytes at different offsets
1346 * that need to be checked by the FW filter. Each segment is called
1347 * a field in the FW API. We verify that the total number of fields
1348 * required for this pattern won't exceed FW limits (8)
1349 * as well as the total fields buffer won't exceed the FW limit.
1350 * Note that if there's a pattern which crosses Ethernet/IP header
1351 * boundary a new field is required.
1352 */
1353 for (i = 0; i < p->pattern_len; i++) {
1354 if (test_bit(i, (unsigned long *)p->mask)) {
1355 if (!in_field) {
1356 in_field = 1;
1357 pattern_len = 1;
1358 } else {
1359 if (i == WL1271_RX_FILTER_ETH_HEADER_SIZE) {
1360 num_fields++;
1361 fields_size += pattern_len +
1362 RX_FILTER_FIELD_OVERHEAD;
1363 pattern_len = 1;
1364 } else
1365 pattern_len++;
1366 }
1367 } else {
1368 if (in_field) {
1369 in_field = 0;
1370 fields_size += pattern_len +
1371 RX_FILTER_FIELD_OVERHEAD;
1372 num_fields++;
1373 }
1374 }
1375 }
1376
1377 if (in_field) {
1378 fields_size += pattern_len + RX_FILTER_FIELD_OVERHEAD;
1379 num_fields++;
1380 }
1381
1382 if (num_fields > WL1271_RX_FILTER_MAX_FIELDS) {
1383 wl1271_warning("RX Filter too complex. Too many segments");
1384 return -EINVAL;
1385 }
1386
1387 if (fields_size > WL1271_RX_FILTER_MAX_FIELDS_SIZE) {
1388 wl1271_warning("RX filter pattern is too big");
1389 return -E2BIG;
1390 }
1391
1392 return 0;
1393 }
1394
wl1271_rx_filter_alloc(void)1395 struct wl12xx_rx_filter *wl1271_rx_filter_alloc(void)
1396 {
1397 return kzalloc(sizeof(struct wl12xx_rx_filter), GFP_KERNEL);
1398 }
1399
wl1271_rx_filter_free(struct wl12xx_rx_filter * filter)1400 void wl1271_rx_filter_free(struct wl12xx_rx_filter *filter)
1401 {
1402 int i;
1403
1404 if (filter == NULL)
1405 return;
1406
1407 for (i = 0; i < filter->num_fields; i++)
1408 kfree(filter->fields[i].pattern);
1409
1410 kfree(filter);
1411 }
1412
wl1271_rx_filter_alloc_field(struct wl12xx_rx_filter * filter,u16 offset,u8 flags,const u8 * pattern,u8 len)1413 int wl1271_rx_filter_alloc_field(struct wl12xx_rx_filter *filter,
1414 u16 offset, u8 flags,
1415 const u8 *pattern, u8 len)
1416 {
1417 struct wl12xx_rx_filter_field *field;
1418
1419 if (filter->num_fields == WL1271_RX_FILTER_MAX_FIELDS) {
1420 wl1271_warning("Max fields per RX filter. can't alloc another");
1421 return -EINVAL;
1422 }
1423
1424 field = &filter->fields[filter->num_fields];
1425
1426 field->pattern = kzalloc(len, GFP_KERNEL);
1427 if (!field->pattern) {
1428 wl1271_warning("Failed to allocate RX filter pattern");
1429 return -ENOMEM;
1430 }
1431
1432 filter->num_fields++;
1433
1434 field->offset = cpu_to_le16(offset);
1435 field->flags = flags;
1436 field->len = len;
1437 memcpy(field->pattern, pattern, len);
1438
1439 return 0;
1440 }
1441
wl1271_rx_filter_get_fields_size(struct wl12xx_rx_filter * filter)1442 int wl1271_rx_filter_get_fields_size(struct wl12xx_rx_filter *filter)
1443 {
1444 int i, fields_size = 0;
1445
1446 for (i = 0; i < filter->num_fields; i++)
1447 fields_size += filter->fields[i].len +
1448 sizeof(struct wl12xx_rx_filter_field) -
1449 sizeof(u8 *);
1450
1451 return fields_size;
1452 }
1453
wl1271_rx_filter_flatten_fields(struct wl12xx_rx_filter * filter,u8 * buf)1454 void wl1271_rx_filter_flatten_fields(struct wl12xx_rx_filter *filter,
1455 u8 *buf)
1456 {
1457 int i;
1458 struct wl12xx_rx_filter_field *field;
1459
1460 for (i = 0; i < filter->num_fields; i++) {
1461 field = (struct wl12xx_rx_filter_field *)buf;
1462
1463 field->offset = filter->fields[i].offset;
1464 field->flags = filter->fields[i].flags;
1465 field->len = filter->fields[i].len;
1466
1467 memcpy(&field->pattern, filter->fields[i].pattern, field->len);
1468 buf += sizeof(struct wl12xx_rx_filter_field) -
1469 sizeof(u8 *) + field->len;
1470 }
1471 }
1472
1473 /*
1474 * Allocates an RX filter returned through f
1475 * which needs to be freed using rx_filter_free()
1476 */
1477 static int
wl1271_convert_wowlan_pattern_to_rx_filter(struct cfg80211_pkt_pattern * p,struct wl12xx_rx_filter ** f)1478 wl1271_convert_wowlan_pattern_to_rx_filter(struct cfg80211_pkt_pattern *p,
1479 struct wl12xx_rx_filter **f)
1480 {
1481 int i, j, ret = 0;
1482 struct wl12xx_rx_filter *filter;
1483 u16 offset;
1484 u8 flags, len;
1485
1486 filter = wl1271_rx_filter_alloc();
1487 if (!filter) {
1488 wl1271_warning("Failed to alloc rx filter");
1489 ret = -ENOMEM;
1490 goto err;
1491 }
1492
1493 i = 0;
1494 while (i < p->pattern_len) {
1495 if (!test_bit(i, (unsigned long *)p->mask)) {
1496 i++;
1497 continue;
1498 }
1499
1500 for (j = i; j < p->pattern_len; j++) {
1501 if (!test_bit(j, (unsigned long *)p->mask))
1502 break;
1503
1504 if (i < WL1271_RX_FILTER_ETH_HEADER_SIZE &&
1505 j >= WL1271_RX_FILTER_ETH_HEADER_SIZE)
1506 break;
1507 }
1508
1509 if (i < WL1271_RX_FILTER_ETH_HEADER_SIZE) {
1510 offset = i;
1511 flags = WL1271_RX_FILTER_FLAG_ETHERNET_HEADER;
1512 } else {
1513 offset = i - WL1271_RX_FILTER_ETH_HEADER_SIZE;
1514 flags = WL1271_RX_FILTER_FLAG_IP_HEADER;
1515 }
1516
1517 len = j - i;
1518
1519 ret = wl1271_rx_filter_alloc_field(filter,
1520 offset,
1521 flags,
1522 &p->pattern[i], len);
1523 if (ret)
1524 goto err;
1525
1526 i = j;
1527 }
1528
1529 filter->action = FILTER_SIGNAL;
1530
1531 *f = filter;
1532 return 0;
1533
1534 err:
1535 wl1271_rx_filter_free(filter);
1536 *f = NULL;
1537
1538 return ret;
1539 }
1540
wl1271_configure_wowlan(struct wl1271 * wl,struct cfg80211_wowlan * wow)1541 static int wl1271_configure_wowlan(struct wl1271 *wl,
1542 struct cfg80211_wowlan *wow)
1543 {
1544 int i, ret;
1545
1546 if (!wow || wow->any || !wow->n_patterns) {
1547 ret = wl1271_acx_default_rx_filter_enable(wl, 0,
1548 FILTER_SIGNAL);
1549 if (ret)
1550 goto out;
1551
1552 ret = wl1271_rx_filter_clear_all(wl);
1553 if (ret)
1554 goto out;
1555
1556 return 0;
1557 }
1558
1559 if (WARN_ON(wow->n_patterns > WL1271_MAX_RX_FILTERS))
1560 return -EINVAL;
1561
1562 /* Validate all incoming patterns before clearing current FW state */
1563 for (i = 0; i < wow->n_patterns; i++) {
1564 ret = wl1271_validate_wowlan_pattern(&wow->patterns[i]);
1565 if (ret) {
1566 wl1271_warning("Bad wowlan pattern %d", i);
1567 return ret;
1568 }
1569 }
1570
1571 ret = wl1271_acx_default_rx_filter_enable(wl, 0, FILTER_SIGNAL);
1572 if (ret)
1573 goto out;
1574
1575 ret = wl1271_rx_filter_clear_all(wl);
1576 if (ret)
1577 goto out;
1578
1579 /* Translate WoWLAN patterns into filters */
1580 for (i = 0; i < wow->n_patterns; i++) {
1581 struct cfg80211_pkt_pattern *p;
1582 struct wl12xx_rx_filter *filter = NULL;
1583
1584 p = &wow->patterns[i];
1585
1586 ret = wl1271_convert_wowlan_pattern_to_rx_filter(p, &filter);
1587 if (ret) {
1588 wl1271_warning("Failed to create an RX filter from "
1589 "wowlan pattern %d", i);
1590 goto out;
1591 }
1592
1593 ret = wl1271_rx_filter_enable(wl, i, 1, filter);
1594
1595 wl1271_rx_filter_free(filter);
1596 if (ret)
1597 goto out;
1598 }
1599
1600 ret = wl1271_acx_default_rx_filter_enable(wl, 1, FILTER_DROP);
1601
1602 out:
1603 return ret;
1604 }
1605
wl1271_configure_suspend_sta(struct wl1271 * wl,struct wl12xx_vif * wlvif,struct cfg80211_wowlan * wow)1606 static int wl1271_configure_suspend_sta(struct wl1271 *wl,
1607 struct wl12xx_vif *wlvif,
1608 struct cfg80211_wowlan *wow)
1609 {
1610 int ret = 0;
1611
1612 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
1613 goto out;
1614
1615 ret = wl1271_configure_wowlan(wl, wow);
1616 if (ret < 0)
1617 goto out;
1618
1619 if ((wl->conf.conn.suspend_wake_up_event ==
1620 wl->conf.conn.wake_up_event) &&
1621 (wl->conf.conn.suspend_listen_interval ==
1622 wl->conf.conn.listen_interval))
1623 goto out;
1624
1625 ret = wl1271_acx_wake_up_conditions(wl, wlvif,
1626 wl->conf.conn.suspend_wake_up_event,
1627 wl->conf.conn.suspend_listen_interval);
1628
1629 if (ret < 0)
1630 wl1271_error("suspend: set wake up conditions failed: %d", ret);
1631 out:
1632 return ret;
1633
1634 }
1635
wl1271_configure_suspend_ap(struct wl1271 * wl,struct wl12xx_vif * wlvif,struct cfg80211_wowlan * wow)1636 static int wl1271_configure_suspend_ap(struct wl1271 *wl,
1637 struct wl12xx_vif *wlvif,
1638 struct cfg80211_wowlan *wow)
1639 {
1640 int ret = 0;
1641
1642 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags))
1643 goto out;
1644
1645 ret = wl1271_acx_beacon_filter_opt(wl, wlvif, true);
1646 if (ret < 0)
1647 goto out;
1648
1649 ret = wl1271_configure_wowlan(wl, wow);
1650 if (ret < 0)
1651 goto out;
1652
1653 out:
1654 return ret;
1655
1656 }
1657
wl1271_configure_suspend(struct wl1271 * wl,struct wl12xx_vif * wlvif,struct cfg80211_wowlan * wow)1658 static int wl1271_configure_suspend(struct wl1271 *wl,
1659 struct wl12xx_vif *wlvif,
1660 struct cfg80211_wowlan *wow)
1661 {
1662 if (wlvif->bss_type == BSS_TYPE_STA_BSS)
1663 return wl1271_configure_suspend_sta(wl, wlvif, wow);
1664 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
1665 return wl1271_configure_suspend_ap(wl, wlvif, wow);
1666 return 0;
1667 }
1668
wl1271_configure_resume(struct wl1271 * wl,struct wl12xx_vif * wlvif)1669 static void wl1271_configure_resume(struct wl1271 *wl, struct wl12xx_vif *wlvif)
1670 {
1671 int ret = 0;
1672 bool is_ap = wlvif->bss_type == BSS_TYPE_AP_BSS;
1673 bool is_sta = wlvif->bss_type == BSS_TYPE_STA_BSS;
1674
1675 if ((!is_ap) && (!is_sta))
1676 return;
1677
1678 if ((is_sta && !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) ||
1679 (is_ap && !test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)))
1680 return;
1681
1682 wl1271_configure_wowlan(wl, NULL);
1683
1684 if (is_sta) {
1685 if ((wl->conf.conn.suspend_wake_up_event ==
1686 wl->conf.conn.wake_up_event) &&
1687 (wl->conf.conn.suspend_listen_interval ==
1688 wl->conf.conn.listen_interval))
1689 return;
1690
1691 ret = wl1271_acx_wake_up_conditions(wl, wlvif,
1692 wl->conf.conn.wake_up_event,
1693 wl->conf.conn.listen_interval);
1694
1695 if (ret < 0)
1696 wl1271_error("resume: wake up conditions failed: %d",
1697 ret);
1698
1699 } else if (is_ap) {
1700 ret = wl1271_acx_beacon_filter_opt(wl, wlvif, false);
1701 }
1702 }
1703
wl1271_op_suspend(struct ieee80211_hw * hw,struct cfg80211_wowlan * wow)1704 static int wl1271_op_suspend(struct ieee80211_hw *hw,
1705 struct cfg80211_wowlan *wow)
1706 {
1707 struct wl1271 *wl = hw->priv;
1708 struct wl12xx_vif *wlvif;
1709 int ret;
1710
1711 wl1271_debug(DEBUG_MAC80211, "mac80211 suspend wow=%d", !!wow);
1712 WARN_ON(!wow);
1713
1714 /* we want to perform the recovery before suspending */
1715 if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags)) {
1716 wl1271_warning("postponing suspend to perform recovery");
1717 return -EBUSY;
1718 }
1719
1720 wl1271_tx_flush(wl);
1721
1722 mutex_lock(&wl->mutex);
1723
1724 ret = wl1271_ps_elp_wakeup(wl);
1725 if (ret < 0) {
1726 mutex_unlock(&wl->mutex);
1727 return ret;
1728 }
1729
1730 wl->wow_enabled = true;
1731 wl12xx_for_each_wlvif(wl, wlvif) {
1732 if (wlcore_is_p2p_mgmt(wlvif))
1733 continue;
1734
1735 ret = wl1271_configure_suspend(wl, wlvif, wow);
1736 if (ret < 0) {
1737 mutex_unlock(&wl->mutex);
1738 wl1271_warning("couldn't prepare device to suspend");
1739 return ret;
1740 }
1741 }
1742
1743 /* disable fast link flow control notifications from FW */
1744 ret = wlcore_hw_interrupt_notify(wl, false);
1745 if (ret < 0)
1746 goto out_sleep;
1747
1748 /* if filtering is enabled, configure the FW to drop all RX BA frames */
1749 ret = wlcore_hw_rx_ba_filter(wl,
1750 !!wl->conf.conn.suspend_rx_ba_activity);
1751 if (ret < 0)
1752 goto out_sleep;
1753
1754 out_sleep:
1755 wl1271_ps_elp_sleep(wl);
1756 mutex_unlock(&wl->mutex);
1757
1758 if (ret < 0) {
1759 wl1271_warning("couldn't prepare device to suspend");
1760 return ret;
1761 }
1762
1763 /* flush any remaining work */
1764 wl1271_debug(DEBUG_MAC80211, "flushing remaining works");
1765
1766 /*
1767 * disable and re-enable interrupts in order to flush
1768 * the threaded_irq
1769 */
1770 wlcore_disable_interrupts(wl);
1771
1772 /*
1773 * set suspended flag to avoid triggering a new threaded_irq
1774 * work. no need for spinlock as interrupts are disabled.
1775 */
1776 set_bit(WL1271_FLAG_SUSPENDED, &wl->flags);
1777
1778 wlcore_enable_interrupts(wl);
1779 flush_work(&wl->tx_work);
1780 flush_delayed_work(&wl->elp_work);
1781
1782 /*
1783 * Cancel the watchdog even if above tx_flush failed. We will detect
1784 * it on resume anyway.
1785 */
1786 cancel_delayed_work(&wl->tx_watchdog_work);
1787
1788 return 0;
1789 }
1790
wl1271_op_resume(struct ieee80211_hw * hw)1791 static int wl1271_op_resume(struct ieee80211_hw *hw)
1792 {
1793 struct wl1271 *wl = hw->priv;
1794 struct wl12xx_vif *wlvif;
1795 unsigned long flags;
1796 bool run_irq_work = false, pending_recovery;
1797 int ret;
1798
1799 wl1271_debug(DEBUG_MAC80211, "mac80211 resume wow=%d",
1800 wl->wow_enabled);
1801 WARN_ON(!wl->wow_enabled);
1802
1803 /*
1804 * re-enable irq_work enqueuing, and call irq_work directly if
1805 * there is a pending work.
1806 */
1807 spin_lock_irqsave(&wl->wl_lock, flags);
1808 clear_bit(WL1271_FLAG_SUSPENDED, &wl->flags);
1809 if (test_and_clear_bit(WL1271_FLAG_PENDING_WORK, &wl->flags))
1810 run_irq_work = true;
1811 spin_unlock_irqrestore(&wl->wl_lock, flags);
1812
1813 mutex_lock(&wl->mutex);
1814
1815 /* test the recovery flag before calling any SDIO functions */
1816 pending_recovery = test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS,
1817 &wl->flags);
1818
1819 if (run_irq_work) {
1820 wl1271_debug(DEBUG_MAC80211,
1821 "run postponed irq_work directly");
1822
1823 /* don't talk to the HW if recovery is pending */
1824 if (!pending_recovery) {
1825 ret = wlcore_irq_locked(wl);
1826 if (ret)
1827 wl12xx_queue_recovery_work(wl);
1828 }
1829
1830 wlcore_enable_interrupts(wl);
1831 }
1832
1833 if (pending_recovery) {
1834 wl1271_warning("queuing forgotten recovery on resume");
1835 ieee80211_queue_work(wl->hw, &wl->recovery_work);
1836 goto out_sleep;
1837 }
1838
1839 ret = wl1271_ps_elp_wakeup(wl);
1840 if (ret < 0)
1841 goto out;
1842
1843 wl12xx_for_each_wlvif(wl, wlvif) {
1844 if (wlcore_is_p2p_mgmt(wlvif))
1845 continue;
1846
1847 wl1271_configure_resume(wl, wlvif);
1848 }
1849
1850 ret = wlcore_hw_interrupt_notify(wl, true);
1851 if (ret < 0)
1852 goto out_sleep;
1853
1854 /* if filtering is enabled, configure the FW to drop all RX BA frames */
1855 ret = wlcore_hw_rx_ba_filter(wl, false);
1856 if (ret < 0)
1857 goto out_sleep;
1858
1859 out_sleep:
1860 wl1271_ps_elp_sleep(wl);
1861
1862 out:
1863 wl->wow_enabled = false;
1864
1865 /*
1866 * Set a flag to re-init the watchdog on the first Tx after resume.
1867 * That way we avoid possible conditions where Tx-complete interrupts
1868 * fail to arrive and we perform a spurious recovery.
1869 */
1870 set_bit(WL1271_FLAG_REINIT_TX_WDOG, &wl->flags);
1871 mutex_unlock(&wl->mutex);
1872
1873 return 0;
1874 }
1875 #endif
1876
wl1271_op_start(struct ieee80211_hw * hw)1877 static int wl1271_op_start(struct ieee80211_hw *hw)
1878 {
1879 wl1271_debug(DEBUG_MAC80211, "mac80211 start");
1880
1881 /*
1882 * We have to delay the booting of the hardware because
1883 * we need to know the local MAC address before downloading and
1884 * initializing the firmware. The MAC address cannot be changed
1885 * after boot, and without the proper MAC address, the firmware
1886 * will not function properly.
1887 *
1888 * The MAC address is first known when the corresponding interface
1889 * is added. That is where we will initialize the hardware.
1890 */
1891
1892 return 0;
1893 }
1894
wlcore_op_stop_locked(struct wl1271 * wl)1895 static void wlcore_op_stop_locked(struct wl1271 *wl)
1896 {
1897 int i;
1898
1899 if (wl->state == WLCORE_STATE_OFF) {
1900 if (test_and_clear_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS,
1901 &wl->flags))
1902 wlcore_enable_interrupts(wl);
1903
1904 return;
1905 }
1906
1907 /*
1908 * this must be before the cancel_work calls below, so that the work
1909 * functions don't perform further work.
1910 */
1911 wl->state = WLCORE_STATE_OFF;
1912
1913 /*
1914 * Use the nosync variant to disable interrupts, so the mutex could be
1915 * held while doing so without deadlocking.
1916 */
1917 wlcore_disable_interrupts_nosync(wl);
1918
1919 mutex_unlock(&wl->mutex);
1920
1921 wlcore_synchronize_interrupts(wl);
1922 if (!test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags))
1923 cancel_work_sync(&wl->recovery_work);
1924 wl1271_flush_deferred_work(wl);
1925 cancel_delayed_work_sync(&wl->scan_complete_work);
1926 cancel_work_sync(&wl->netstack_work);
1927 cancel_work_sync(&wl->tx_work);
1928 cancel_delayed_work_sync(&wl->elp_work);
1929 cancel_delayed_work_sync(&wl->tx_watchdog_work);
1930
1931 /* let's notify MAC80211 about the remaining pending TX frames */
1932 mutex_lock(&wl->mutex);
1933 wl12xx_tx_reset(wl);
1934
1935 wl1271_power_off(wl);
1936 /*
1937 * In case a recovery was scheduled, interrupts were disabled to avoid
1938 * an interrupt storm. Now that the power is down, it is safe to
1939 * re-enable interrupts to balance the disable depth
1940 */
1941 if (test_and_clear_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags))
1942 wlcore_enable_interrupts(wl);
1943
1944 wl->band = NL80211_BAND_2GHZ;
1945
1946 wl->rx_counter = 0;
1947 wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
1948 wl->channel_type = NL80211_CHAN_NO_HT;
1949 wl->tx_blocks_available = 0;
1950 wl->tx_allocated_blocks = 0;
1951 wl->tx_results_count = 0;
1952 wl->tx_packets_count = 0;
1953 wl->time_offset = 0;
1954 wl->ap_fw_ps_map = 0;
1955 wl->ap_ps_map = 0;
1956 wl->sleep_auth = WL1271_PSM_ILLEGAL;
1957 memset(wl->roles_map, 0, sizeof(wl->roles_map));
1958 memset(wl->links_map, 0, sizeof(wl->links_map));
1959 memset(wl->roc_map, 0, sizeof(wl->roc_map));
1960 memset(wl->session_ids, 0, sizeof(wl->session_ids));
1961 memset(wl->rx_filter_enabled, 0, sizeof(wl->rx_filter_enabled));
1962 wl->active_sta_count = 0;
1963 wl->active_link_count = 0;
1964
1965 /* The system link is always allocated */
1966 wl->links[WL12XX_SYSTEM_HLID].allocated_pkts = 0;
1967 wl->links[WL12XX_SYSTEM_HLID].prev_freed_pkts = 0;
1968 __set_bit(WL12XX_SYSTEM_HLID, wl->links_map);
1969
1970 /*
1971 * this is performed after the cancel_work calls and the associated
1972 * mutex_lock, so that wl1271_op_add_interface does not accidentally
1973 * get executed before all these vars have been reset.
1974 */
1975 wl->flags = 0;
1976
1977 wl->tx_blocks_freed = 0;
1978
1979 for (i = 0; i < NUM_TX_QUEUES; i++) {
1980 wl->tx_pkts_freed[i] = 0;
1981 wl->tx_allocated_pkts[i] = 0;
1982 }
1983
1984 wl1271_debugfs_reset(wl);
1985
1986 kfree(wl->raw_fw_status);
1987 wl->raw_fw_status = NULL;
1988 kfree(wl->fw_status);
1989 wl->fw_status = NULL;
1990 kfree(wl->tx_res_if);
1991 wl->tx_res_if = NULL;
1992 kfree(wl->target_mem_map);
1993 wl->target_mem_map = NULL;
1994
1995 /*
1996 * FW channels must be re-calibrated after recovery,
1997 * save current Reg-Domain channel configuration and clear it.
1998 */
1999 memcpy(wl->reg_ch_conf_pending, wl->reg_ch_conf_last,
2000 sizeof(wl->reg_ch_conf_pending));
2001 memset(wl->reg_ch_conf_last, 0, sizeof(wl->reg_ch_conf_last));
2002 }
2003
wlcore_op_stop(struct ieee80211_hw * hw)2004 static void wlcore_op_stop(struct ieee80211_hw *hw)
2005 {
2006 struct wl1271 *wl = hw->priv;
2007
2008 wl1271_debug(DEBUG_MAC80211, "mac80211 stop");
2009
2010 mutex_lock(&wl->mutex);
2011
2012 wlcore_op_stop_locked(wl);
2013
2014 mutex_unlock(&wl->mutex);
2015 }
2016
wlcore_channel_switch_work(struct work_struct * work)2017 static void wlcore_channel_switch_work(struct work_struct *work)
2018 {
2019 struct delayed_work *dwork;
2020 struct wl1271 *wl;
2021 struct ieee80211_vif *vif;
2022 struct wl12xx_vif *wlvif;
2023 int ret;
2024
2025 dwork = to_delayed_work(work);
2026 wlvif = container_of(dwork, struct wl12xx_vif, channel_switch_work);
2027 wl = wlvif->wl;
2028
2029 wl1271_info("channel switch failed (role_id: %d).", wlvif->role_id);
2030
2031 mutex_lock(&wl->mutex);
2032
2033 if (unlikely(wl->state != WLCORE_STATE_ON))
2034 goto out;
2035
2036 /* check the channel switch is still ongoing */
2037 if (!test_and_clear_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags))
2038 goto out;
2039
2040 vif = wl12xx_wlvif_to_vif(wlvif);
2041 ieee80211_chswitch_done(vif, false);
2042
2043 ret = wl1271_ps_elp_wakeup(wl);
2044 if (ret < 0)
2045 goto out;
2046
2047 wl12xx_cmd_stop_channel_switch(wl, wlvif);
2048
2049 wl1271_ps_elp_sleep(wl);
2050 out:
2051 mutex_unlock(&wl->mutex);
2052 }
2053
wlcore_connection_loss_work(struct work_struct * work)2054 static void wlcore_connection_loss_work(struct work_struct *work)
2055 {
2056 struct delayed_work *dwork;
2057 struct wl1271 *wl;
2058 struct ieee80211_vif *vif;
2059 struct wl12xx_vif *wlvif;
2060
2061 dwork = to_delayed_work(work);
2062 wlvif = container_of(dwork, struct wl12xx_vif, connection_loss_work);
2063 wl = wlvif->wl;
2064
2065 wl1271_info("Connection loss work (role_id: %d).", wlvif->role_id);
2066
2067 mutex_lock(&wl->mutex);
2068
2069 if (unlikely(wl->state != WLCORE_STATE_ON))
2070 goto out;
2071
2072 /* Call mac80211 connection loss */
2073 if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
2074 goto out;
2075
2076 vif = wl12xx_wlvif_to_vif(wlvif);
2077 ieee80211_connection_loss(vif);
2078 out:
2079 mutex_unlock(&wl->mutex);
2080 }
2081
wlcore_pending_auth_complete_work(struct work_struct * work)2082 static void wlcore_pending_auth_complete_work(struct work_struct *work)
2083 {
2084 struct delayed_work *dwork;
2085 struct wl1271 *wl;
2086 struct wl12xx_vif *wlvif;
2087 unsigned long time_spare;
2088 int ret;
2089
2090 dwork = to_delayed_work(work);
2091 wlvif = container_of(dwork, struct wl12xx_vif,
2092 pending_auth_complete_work);
2093 wl = wlvif->wl;
2094
2095 mutex_lock(&wl->mutex);
2096
2097 if (unlikely(wl->state != WLCORE_STATE_ON))
2098 goto out;
2099
2100 /*
2101 * Make sure a second really passed since the last auth reply. Maybe
2102 * a second auth reply arrived while we were stuck on the mutex.
2103 * Check for a little less than the timeout to protect from scheduler
2104 * irregularities.
2105 */
2106 time_spare = jiffies +
2107 msecs_to_jiffies(WLCORE_PEND_AUTH_ROC_TIMEOUT - 50);
2108 if (!time_after(time_spare, wlvif->pending_auth_reply_time))
2109 goto out;
2110
2111 ret = wl1271_ps_elp_wakeup(wl);
2112 if (ret < 0)
2113 goto out;
2114
2115 /* cancel the ROC if active */
2116 wlcore_update_inconn_sta(wl, wlvif, NULL, false);
2117
2118 wl1271_ps_elp_sleep(wl);
2119 out:
2120 mutex_unlock(&wl->mutex);
2121 }
2122
wl12xx_allocate_rate_policy(struct wl1271 * wl,u8 * idx)2123 static int wl12xx_allocate_rate_policy(struct wl1271 *wl, u8 *idx)
2124 {
2125 u8 policy = find_first_zero_bit(wl->rate_policies_map,
2126 WL12XX_MAX_RATE_POLICIES);
2127 if (policy >= WL12XX_MAX_RATE_POLICIES)
2128 return -EBUSY;
2129
2130 __set_bit(policy, wl->rate_policies_map);
2131 *idx = policy;
2132 return 0;
2133 }
2134
wl12xx_free_rate_policy(struct wl1271 * wl,u8 * idx)2135 static void wl12xx_free_rate_policy(struct wl1271 *wl, u8 *idx)
2136 {
2137 if (WARN_ON(*idx >= WL12XX_MAX_RATE_POLICIES))
2138 return;
2139
2140 __clear_bit(*idx, wl->rate_policies_map);
2141 *idx = WL12XX_MAX_RATE_POLICIES;
2142 }
2143
wlcore_allocate_klv_template(struct wl1271 * wl,u8 * idx)2144 static int wlcore_allocate_klv_template(struct wl1271 *wl, u8 *idx)
2145 {
2146 u8 policy = find_first_zero_bit(wl->klv_templates_map,
2147 WLCORE_MAX_KLV_TEMPLATES);
2148 if (policy >= WLCORE_MAX_KLV_TEMPLATES)
2149 return -EBUSY;
2150
2151 __set_bit(policy, wl->klv_templates_map);
2152 *idx = policy;
2153 return 0;
2154 }
2155
wlcore_free_klv_template(struct wl1271 * wl,u8 * idx)2156 static void wlcore_free_klv_template(struct wl1271 *wl, u8 *idx)
2157 {
2158 if (WARN_ON(*idx >= WLCORE_MAX_KLV_TEMPLATES))
2159 return;
2160
2161 __clear_bit(*idx, wl->klv_templates_map);
2162 *idx = WLCORE_MAX_KLV_TEMPLATES;
2163 }
2164
wl12xx_get_role_type(struct wl1271 * wl,struct wl12xx_vif * wlvif)2165 static u8 wl12xx_get_role_type(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2166 {
2167 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
2168
2169 switch (wlvif->bss_type) {
2170 case BSS_TYPE_AP_BSS:
2171 if (wlvif->p2p)
2172 return WL1271_ROLE_P2P_GO;
2173 else if (ieee80211_vif_is_mesh(vif))
2174 return WL1271_ROLE_MESH_POINT;
2175 else
2176 return WL1271_ROLE_AP;
2177
2178 case BSS_TYPE_STA_BSS:
2179 if (wlvif->p2p)
2180 return WL1271_ROLE_P2P_CL;
2181 else
2182 return WL1271_ROLE_STA;
2183
2184 case BSS_TYPE_IBSS:
2185 return WL1271_ROLE_IBSS;
2186
2187 default:
2188 wl1271_error("invalid bss_type: %d", wlvif->bss_type);
2189 }
2190 return WL12XX_INVALID_ROLE_TYPE;
2191 }
2192
wl12xx_init_vif_data(struct wl1271 * wl,struct ieee80211_vif * vif)2193 static int wl12xx_init_vif_data(struct wl1271 *wl, struct ieee80211_vif *vif)
2194 {
2195 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2196 int i;
2197
2198 /* clear everything but the persistent data */
2199 memset(wlvif, 0, offsetof(struct wl12xx_vif, persistent));
2200
2201 switch (ieee80211_vif_type_p2p(vif)) {
2202 case NL80211_IFTYPE_P2P_CLIENT:
2203 wlvif->p2p = 1;
2204 /* fall-through */
2205 case NL80211_IFTYPE_STATION:
2206 case NL80211_IFTYPE_P2P_DEVICE:
2207 wlvif->bss_type = BSS_TYPE_STA_BSS;
2208 break;
2209 case NL80211_IFTYPE_ADHOC:
2210 wlvif->bss_type = BSS_TYPE_IBSS;
2211 break;
2212 case NL80211_IFTYPE_P2P_GO:
2213 wlvif->p2p = 1;
2214 /* fall-through */
2215 case NL80211_IFTYPE_AP:
2216 case NL80211_IFTYPE_MESH_POINT:
2217 wlvif->bss_type = BSS_TYPE_AP_BSS;
2218 break;
2219 default:
2220 wlvif->bss_type = MAX_BSS_TYPE;
2221 return -EOPNOTSUPP;
2222 }
2223
2224 wlvif->role_id = WL12XX_INVALID_ROLE_ID;
2225 wlvif->dev_role_id = WL12XX_INVALID_ROLE_ID;
2226 wlvif->dev_hlid = WL12XX_INVALID_LINK_ID;
2227
2228 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2229 wlvif->bss_type == BSS_TYPE_IBSS) {
2230 /* init sta/ibss data */
2231 wlvif->sta.hlid = WL12XX_INVALID_LINK_ID;
2232 wl12xx_allocate_rate_policy(wl, &wlvif->sta.basic_rate_idx);
2233 wl12xx_allocate_rate_policy(wl, &wlvif->sta.ap_rate_idx);
2234 wl12xx_allocate_rate_policy(wl, &wlvif->sta.p2p_rate_idx);
2235 wlcore_allocate_klv_template(wl, &wlvif->sta.klv_template_id);
2236 wlvif->basic_rate_set = CONF_TX_RATE_MASK_BASIC;
2237 wlvif->basic_rate = CONF_TX_RATE_MASK_BASIC;
2238 wlvif->rate_set = CONF_TX_RATE_MASK_BASIC;
2239 } else {
2240 /* init ap data */
2241 wlvif->ap.bcast_hlid = WL12XX_INVALID_LINK_ID;
2242 wlvif->ap.global_hlid = WL12XX_INVALID_LINK_ID;
2243 wl12xx_allocate_rate_policy(wl, &wlvif->ap.mgmt_rate_idx);
2244 wl12xx_allocate_rate_policy(wl, &wlvif->ap.bcast_rate_idx);
2245 for (i = 0; i < CONF_TX_MAX_AC_COUNT; i++)
2246 wl12xx_allocate_rate_policy(wl,
2247 &wlvif->ap.ucast_rate_idx[i]);
2248 wlvif->basic_rate_set = CONF_TX_ENABLED_RATES;
2249 /*
2250 * TODO: check if basic_rate shouldn't be
2251 * wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
2252 * instead (the same thing for STA above).
2253 */
2254 wlvif->basic_rate = CONF_TX_ENABLED_RATES;
2255 /* TODO: this seems to be used only for STA, check it */
2256 wlvif->rate_set = CONF_TX_ENABLED_RATES;
2257 }
2258
2259 wlvif->bitrate_masks[NL80211_BAND_2GHZ] = wl->conf.tx.basic_rate;
2260 wlvif->bitrate_masks[NL80211_BAND_5GHZ] = wl->conf.tx.basic_rate_5;
2261 wlvif->beacon_int = WL1271_DEFAULT_BEACON_INT;
2262
2263 /*
2264 * mac80211 configures some values globally, while we treat them
2265 * per-interface. thus, on init, we have to copy them from wl
2266 */
2267 wlvif->band = wl->band;
2268 wlvif->channel = wl->channel;
2269 wlvif->power_level = wl->power_level;
2270 wlvif->channel_type = wl->channel_type;
2271
2272 INIT_WORK(&wlvif->rx_streaming_enable_work,
2273 wl1271_rx_streaming_enable_work);
2274 INIT_WORK(&wlvif->rx_streaming_disable_work,
2275 wl1271_rx_streaming_disable_work);
2276 INIT_WORK(&wlvif->rc_update_work, wlcore_rc_update_work);
2277 INIT_DELAYED_WORK(&wlvif->channel_switch_work,
2278 wlcore_channel_switch_work);
2279 INIT_DELAYED_WORK(&wlvif->connection_loss_work,
2280 wlcore_connection_loss_work);
2281 INIT_DELAYED_WORK(&wlvif->pending_auth_complete_work,
2282 wlcore_pending_auth_complete_work);
2283 INIT_LIST_HEAD(&wlvif->list);
2284
2285 setup_timer(&wlvif->rx_streaming_timer, wl1271_rx_streaming_timer,
2286 (unsigned long) wlvif);
2287 return 0;
2288 }
2289
wl12xx_init_fw(struct wl1271 * wl)2290 static int wl12xx_init_fw(struct wl1271 *wl)
2291 {
2292 int retries = WL1271_BOOT_RETRIES;
2293 bool booted = false;
2294 struct wiphy *wiphy = wl->hw->wiphy;
2295 int ret;
2296
2297 while (retries) {
2298 retries--;
2299 ret = wl12xx_chip_wakeup(wl, false);
2300 if (ret < 0)
2301 goto power_off;
2302
2303 ret = wl->ops->boot(wl);
2304 if (ret < 0)
2305 goto power_off;
2306
2307 ret = wl1271_hw_init(wl);
2308 if (ret < 0)
2309 goto irq_disable;
2310
2311 booted = true;
2312 break;
2313
2314 irq_disable:
2315 mutex_unlock(&wl->mutex);
2316 /* Unlocking the mutex in the middle of handling is
2317 inherently unsafe. In this case we deem it safe to do,
2318 because we need to let any possibly pending IRQ out of
2319 the system (and while we are WLCORE_STATE_OFF the IRQ
2320 work function will not do anything.) Also, any other
2321 possible concurrent operations will fail due to the
2322 current state, hence the wl1271 struct should be safe. */
2323 wlcore_disable_interrupts(wl);
2324 wl1271_flush_deferred_work(wl);
2325 cancel_work_sync(&wl->netstack_work);
2326 mutex_lock(&wl->mutex);
2327 power_off:
2328 wl1271_power_off(wl);
2329 }
2330
2331 if (!booted) {
2332 wl1271_error("firmware boot failed despite %d retries",
2333 WL1271_BOOT_RETRIES);
2334 goto out;
2335 }
2336
2337 wl1271_info("firmware booted (%s)", wl->chip.fw_ver_str);
2338
2339 /* update hw/fw version info in wiphy struct */
2340 wiphy->hw_version = wl->chip.id;
2341 strncpy(wiphy->fw_version, wl->chip.fw_ver_str,
2342 sizeof(wiphy->fw_version));
2343
2344 /*
2345 * Now we know if 11a is supported (info from the NVS), so disable
2346 * 11a channels if not supported
2347 */
2348 if (!wl->enable_11a)
2349 wiphy->bands[NL80211_BAND_5GHZ]->n_channels = 0;
2350
2351 wl1271_debug(DEBUG_MAC80211, "11a is %ssupported",
2352 wl->enable_11a ? "" : "not ");
2353
2354 wl->state = WLCORE_STATE_ON;
2355 out:
2356 return ret;
2357 }
2358
wl12xx_dev_role_started(struct wl12xx_vif * wlvif)2359 static bool wl12xx_dev_role_started(struct wl12xx_vif *wlvif)
2360 {
2361 return wlvif->dev_hlid != WL12XX_INVALID_LINK_ID;
2362 }
2363
2364 /*
2365 * Check whether a fw switch (i.e. moving from one loaded
2366 * fw to another) is needed. This function is also responsible
2367 * for updating wl->last_vif_count, so it must be called before
2368 * loading a non-plt fw (so the correct fw (single-role/multi-role)
2369 * will be used).
2370 */
wl12xx_need_fw_change(struct wl1271 * wl,struct vif_counter_data vif_counter_data,bool add)2371 static bool wl12xx_need_fw_change(struct wl1271 *wl,
2372 struct vif_counter_data vif_counter_data,
2373 bool add)
2374 {
2375 enum wl12xx_fw_type current_fw = wl->fw_type;
2376 u8 vif_count = vif_counter_data.counter;
2377
2378 if (test_bit(WL1271_FLAG_VIF_CHANGE_IN_PROGRESS, &wl->flags))
2379 return false;
2380
2381 /* increase the vif count if this is a new vif */
2382 if (add && !vif_counter_data.cur_vif_running)
2383 vif_count++;
2384
2385 wl->last_vif_count = vif_count;
2386
2387 /* no need for fw change if the device is OFF */
2388 if (wl->state == WLCORE_STATE_OFF)
2389 return false;
2390
2391 /* no need for fw change if a single fw is used */
2392 if (!wl->mr_fw_name)
2393 return false;
2394
2395 if (vif_count > 1 && current_fw == WL12XX_FW_TYPE_NORMAL)
2396 return true;
2397 if (vif_count <= 1 && current_fw == WL12XX_FW_TYPE_MULTI)
2398 return true;
2399
2400 return false;
2401 }
2402
2403 /*
2404 * Enter "forced psm". Make sure the sta is in psm against the ap,
2405 * to make the fw switch a bit more disconnection-persistent.
2406 */
wl12xx_force_active_psm(struct wl1271 * wl)2407 static void wl12xx_force_active_psm(struct wl1271 *wl)
2408 {
2409 struct wl12xx_vif *wlvif;
2410
2411 wl12xx_for_each_wlvif_sta(wl, wlvif) {
2412 wl1271_ps_set_mode(wl, wlvif, STATION_POWER_SAVE_MODE);
2413 }
2414 }
2415
2416 struct wlcore_hw_queue_iter_data {
2417 unsigned long hw_queue_map[BITS_TO_LONGS(WLCORE_NUM_MAC_ADDRESSES)];
2418 /* current vif */
2419 struct ieee80211_vif *vif;
2420 /* is the current vif among those iterated */
2421 bool cur_running;
2422 };
2423
wlcore_hw_queue_iter(void * data,u8 * mac,struct ieee80211_vif * vif)2424 static void wlcore_hw_queue_iter(void *data, u8 *mac,
2425 struct ieee80211_vif *vif)
2426 {
2427 struct wlcore_hw_queue_iter_data *iter_data = data;
2428
2429 if (vif->type == NL80211_IFTYPE_P2P_DEVICE ||
2430 WARN_ON_ONCE(vif->hw_queue[0] == IEEE80211_INVAL_HW_QUEUE))
2431 return;
2432
2433 if (iter_data->cur_running || vif == iter_data->vif) {
2434 iter_data->cur_running = true;
2435 return;
2436 }
2437
2438 __set_bit(vif->hw_queue[0] / NUM_TX_QUEUES, iter_data->hw_queue_map);
2439 }
2440
wlcore_allocate_hw_queue_base(struct wl1271 * wl,struct wl12xx_vif * wlvif)2441 static int wlcore_allocate_hw_queue_base(struct wl1271 *wl,
2442 struct wl12xx_vif *wlvif)
2443 {
2444 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
2445 struct wlcore_hw_queue_iter_data iter_data = {};
2446 int i, q_base;
2447
2448 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
2449 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
2450 return 0;
2451 }
2452
2453 iter_data.vif = vif;
2454
2455 /* mark all bits taken by active interfaces */
2456 ieee80211_iterate_active_interfaces_atomic(wl->hw,
2457 IEEE80211_IFACE_ITER_RESUME_ALL,
2458 wlcore_hw_queue_iter, &iter_data);
2459
2460 /* the current vif is already running in mac80211 (resume/recovery) */
2461 if (iter_data.cur_running) {
2462 wlvif->hw_queue_base = vif->hw_queue[0];
2463 wl1271_debug(DEBUG_MAC80211,
2464 "using pre-allocated hw queue base %d",
2465 wlvif->hw_queue_base);
2466
2467 /* interface type might have changed type */
2468 goto adjust_cab_queue;
2469 }
2470
2471 q_base = find_first_zero_bit(iter_data.hw_queue_map,
2472 WLCORE_NUM_MAC_ADDRESSES);
2473 if (q_base >= WLCORE_NUM_MAC_ADDRESSES)
2474 return -EBUSY;
2475
2476 wlvif->hw_queue_base = q_base * NUM_TX_QUEUES;
2477 wl1271_debug(DEBUG_MAC80211, "allocating hw queue base: %d",
2478 wlvif->hw_queue_base);
2479
2480 for (i = 0; i < NUM_TX_QUEUES; i++) {
2481 wl->queue_stop_reasons[wlvif->hw_queue_base + i] = 0;
2482 /* register hw queues in mac80211 */
2483 vif->hw_queue[i] = wlvif->hw_queue_base + i;
2484 }
2485
2486 adjust_cab_queue:
2487 /* the last places are reserved for cab queues per interface */
2488 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
2489 vif->cab_queue = NUM_TX_QUEUES * WLCORE_NUM_MAC_ADDRESSES +
2490 wlvif->hw_queue_base / NUM_TX_QUEUES;
2491 else
2492 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
2493
2494 return 0;
2495 }
2496
wl1271_op_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)2497 static int wl1271_op_add_interface(struct ieee80211_hw *hw,
2498 struct ieee80211_vif *vif)
2499 {
2500 struct wl1271 *wl = hw->priv;
2501 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2502 struct vif_counter_data vif_count;
2503 int ret = 0;
2504 u8 role_type;
2505
2506 if (wl->plt) {
2507 wl1271_error("Adding Interface not allowed while in PLT mode");
2508 return -EBUSY;
2509 }
2510
2511 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
2512 IEEE80211_VIF_SUPPORTS_UAPSD |
2513 IEEE80211_VIF_SUPPORTS_CQM_RSSI;
2514
2515 wl1271_debug(DEBUG_MAC80211, "mac80211 add interface type %d mac %pM",
2516 ieee80211_vif_type_p2p(vif), vif->addr);
2517
2518 wl12xx_get_vif_count(hw, vif, &vif_count);
2519
2520 mutex_lock(&wl->mutex);
2521 ret = wl1271_ps_elp_wakeup(wl);
2522 if (ret < 0)
2523 goto out_unlock;
2524
2525 /*
2526 * in some very corner case HW recovery scenarios its possible to
2527 * get here before __wl1271_op_remove_interface is complete, so
2528 * opt out if that is the case.
2529 */
2530 if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags) ||
2531 test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)) {
2532 ret = -EBUSY;
2533 goto out;
2534 }
2535
2536
2537 ret = wl12xx_init_vif_data(wl, vif);
2538 if (ret < 0)
2539 goto out;
2540
2541 wlvif->wl = wl;
2542 role_type = wl12xx_get_role_type(wl, wlvif);
2543 if (role_type == WL12XX_INVALID_ROLE_TYPE) {
2544 ret = -EINVAL;
2545 goto out;
2546 }
2547
2548 ret = wlcore_allocate_hw_queue_base(wl, wlvif);
2549 if (ret < 0)
2550 goto out;
2551
2552 if (wl12xx_need_fw_change(wl, vif_count, true)) {
2553 wl12xx_force_active_psm(wl);
2554 set_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags);
2555 mutex_unlock(&wl->mutex);
2556 wl1271_recovery_work(&wl->recovery_work);
2557 return 0;
2558 }
2559
2560 /*
2561 * TODO: after the nvs issue will be solved, move this block
2562 * to start(), and make sure here the driver is ON.
2563 */
2564 if (wl->state == WLCORE_STATE_OFF) {
2565 /*
2566 * we still need this in order to configure the fw
2567 * while uploading the nvs
2568 */
2569 memcpy(wl->addresses[0].addr, vif->addr, ETH_ALEN);
2570
2571 ret = wl12xx_init_fw(wl);
2572 if (ret < 0)
2573 goto out;
2574 }
2575
2576 if (!wlcore_is_p2p_mgmt(wlvif)) {
2577 ret = wl12xx_cmd_role_enable(wl, vif->addr,
2578 role_type, &wlvif->role_id);
2579 if (ret < 0)
2580 goto out;
2581
2582 ret = wl1271_init_vif_specific(wl, vif);
2583 if (ret < 0)
2584 goto out;
2585
2586 } else {
2587 ret = wl12xx_cmd_role_enable(wl, vif->addr, WL1271_ROLE_DEVICE,
2588 &wlvif->dev_role_id);
2589 if (ret < 0)
2590 goto out;
2591
2592 /* needed mainly for configuring rate policies */
2593 ret = wl1271_sta_hw_init(wl, wlvif);
2594 if (ret < 0)
2595 goto out;
2596 }
2597
2598 list_add(&wlvif->list, &wl->wlvif_list);
2599 set_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags);
2600
2601 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
2602 wl->ap_count++;
2603 else
2604 wl->sta_count++;
2605 out:
2606 wl1271_ps_elp_sleep(wl);
2607 out_unlock:
2608 mutex_unlock(&wl->mutex);
2609
2610 return ret;
2611 }
2612
__wl1271_op_remove_interface(struct wl1271 * wl,struct ieee80211_vif * vif,bool reset_tx_queues)2613 static void __wl1271_op_remove_interface(struct wl1271 *wl,
2614 struct ieee80211_vif *vif,
2615 bool reset_tx_queues)
2616 {
2617 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2618 int i, ret;
2619 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
2620
2621 wl1271_debug(DEBUG_MAC80211, "mac80211 remove interface");
2622
2623 if (!test_and_clear_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
2624 return;
2625
2626 /* because of hardware recovery, we may get here twice */
2627 if (wl->state == WLCORE_STATE_OFF)
2628 return;
2629
2630 wl1271_info("down");
2631
2632 if (wl->scan.state != WL1271_SCAN_STATE_IDLE &&
2633 wl->scan_wlvif == wlvif) {
2634 struct cfg80211_scan_info info = {
2635 .aborted = true,
2636 };
2637
2638 /*
2639 * Rearm the tx watchdog just before idling scan. This
2640 * prevents just-finished scans from triggering the watchdog
2641 */
2642 wl12xx_rearm_tx_watchdog_locked(wl);
2643
2644 wl->scan.state = WL1271_SCAN_STATE_IDLE;
2645 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
2646 wl->scan_wlvif = NULL;
2647 wl->scan.req = NULL;
2648 ieee80211_scan_completed(wl->hw, &info);
2649 }
2650
2651 if (wl->sched_vif == wlvif)
2652 wl->sched_vif = NULL;
2653
2654 if (wl->roc_vif == vif) {
2655 wl->roc_vif = NULL;
2656 ieee80211_remain_on_channel_expired(wl->hw);
2657 }
2658
2659 if (!test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags)) {
2660 /* disable active roles */
2661 ret = wl1271_ps_elp_wakeup(wl);
2662 if (ret < 0)
2663 goto deinit;
2664
2665 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2666 wlvif->bss_type == BSS_TYPE_IBSS) {
2667 if (wl12xx_dev_role_started(wlvif))
2668 wl12xx_stop_dev(wl, wlvif);
2669 }
2670
2671 if (!wlcore_is_p2p_mgmt(wlvif)) {
2672 ret = wl12xx_cmd_role_disable(wl, &wlvif->role_id);
2673 if (ret < 0)
2674 goto deinit;
2675 } else {
2676 ret = wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
2677 if (ret < 0)
2678 goto deinit;
2679 }
2680
2681 wl1271_ps_elp_sleep(wl);
2682 }
2683 deinit:
2684 wl12xx_tx_reset_wlvif(wl, wlvif);
2685
2686 /* clear all hlids (except system_hlid) */
2687 wlvif->dev_hlid = WL12XX_INVALID_LINK_ID;
2688
2689 if (wlvif->bss_type == BSS_TYPE_STA_BSS ||
2690 wlvif->bss_type == BSS_TYPE_IBSS) {
2691 wlvif->sta.hlid = WL12XX_INVALID_LINK_ID;
2692 wl12xx_free_rate_policy(wl, &wlvif->sta.basic_rate_idx);
2693 wl12xx_free_rate_policy(wl, &wlvif->sta.ap_rate_idx);
2694 wl12xx_free_rate_policy(wl, &wlvif->sta.p2p_rate_idx);
2695 wlcore_free_klv_template(wl, &wlvif->sta.klv_template_id);
2696 } else {
2697 wlvif->ap.bcast_hlid = WL12XX_INVALID_LINK_ID;
2698 wlvif->ap.global_hlid = WL12XX_INVALID_LINK_ID;
2699 wl12xx_free_rate_policy(wl, &wlvif->ap.mgmt_rate_idx);
2700 wl12xx_free_rate_policy(wl, &wlvif->ap.bcast_rate_idx);
2701 for (i = 0; i < CONF_TX_MAX_AC_COUNT; i++)
2702 wl12xx_free_rate_policy(wl,
2703 &wlvif->ap.ucast_rate_idx[i]);
2704 wl1271_free_ap_keys(wl, wlvif);
2705 }
2706
2707 dev_kfree_skb(wlvif->probereq);
2708 wlvif->probereq = NULL;
2709 if (wl->last_wlvif == wlvif)
2710 wl->last_wlvif = NULL;
2711 list_del(&wlvif->list);
2712 memset(wlvif->ap.sta_hlid_map, 0, sizeof(wlvif->ap.sta_hlid_map));
2713 wlvif->role_id = WL12XX_INVALID_ROLE_ID;
2714 wlvif->dev_role_id = WL12XX_INVALID_ROLE_ID;
2715
2716 if (is_ap)
2717 wl->ap_count--;
2718 else
2719 wl->sta_count--;
2720
2721 /*
2722 * Last AP, have more stations. Configure sleep auth according to STA.
2723 * Don't do thin on unintended recovery.
2724 */
2725 if (test_bit(WL1271_FLAG_RECOVERY_IN_PROGRESS, &wl->flags) &&
2726 !test_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags))
2727 goto unlock;
2728
2729 if (wl->ap_count == 0 && is_ap) {
2730 /* mask ap events */
2731 wl->event_mask &= ~wl->ap_event_mask;
2732 wl1271_event_unmask(wl);
2733 }
2734
2735 if (wl->ap_count == 0 && is_ap && wl->sta_count) {
2736 u8 sta_auth = wl->conf.conn.sta_sleep_auth;
2737 /* Configure for power according to debugfs */
2738 if (sta_auth != WL1271_PSM_ILLEGAL)
2739 wl1271_acx_sleep_auth(wl, sta_auth);
2740 /* Configure for ELP power saving */
2741 else
2742 wl1271_acx_sleep_auth(wl, WL1271_PSM_ELP);
2743 }
2744
2745 unlock:
2746 mutex_unlock(&wl->mutex);
2747
2748 del_timer_sync(&wlvif->rx_streaming_timer);
2749 cancel_work_sync(&wlvif->rx_streaming_enable_work);
2750 cancel_work_sync(&wlvif->rx_streaming_disable_work);
2751 cancel_work_sync(&wlvif->rc_update_work);
2752 cancel_delayed_work_sync(&wlvif->connection_loss_work);
2753 cancel_delayed_work_sync(&wlvif->channel_switch_work);
2754 cancel_delayed_work_sync(&wlvif->pending_auth_complete_work);
2755
2756 mutex_lock(&wl->mutex);
2757 }
2758
wl1271_op_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)2759 static void wl1271_op_remove_interface(struct ieee80211_hw *hw,
2760 struct ieee80211_vif *vif)
2761 {
2762 struct wl1271 *wl = hw->priv;
2763 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
2764 struct wl12xx_vif *iter;
2765 struct vif_counter_data vif_count;
2766
2767 wl12xx_get_vif_count(hw, vif, &vif_count);
2768 mutex_lock(&wl->mutex);
2769
2770 if (wl->state == WLCORE_STATE_OFF ||
2771 !test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
2772 goto out;
2773
2774 /*
2775 * wl->vif can be null here if someone shuts down the interface
2776 * just when hardware recovery has been started.
2777 */
2778 wl12xx_for_each_wlvif(wl, iter) {
2779 if (iter != wlvif)
2780 continue;
2781
2782 __wl1271_op_remove_interface(wl, vif, true);
2783 break;
2784 }
2785 WARN_ON(iter != wlvif);
2786 if (wl12xx_need_fw_change(wl, vif_count, false)) {
2787 wl12xx_force_active_psm(wl);
2788 set_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags);
2789 wl12xx_queue_recovery_work(wl);
2790 }
2791 out:
2792 mutex_unlock(&wl->mutex);
2793 }
2794
wl12xx_op_change_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif,enum nl80211_iftype new_type,bool p2p)2795 static int wl12xx_op_change_interface(struct ieee80211_hw *hw,
2796 struct ieee80211_vif *vif,
2797 enum nl80211_iftype new_type, bool p2p)
2798 {
2799 struct wl1271 *wl = hw->priv;
2800 int ret;
2801
2802 set_bit(WL1271_FLAG_VIF_CHANGE_IN_PROGRESS, &wl->flags);
2803 wl1271_op_remove_interface(hw, vif);
2804
2805 vif->type = new_type;
2806 vif->p2p = p2p;
2807 ret = wl1271_op_add_interface(hw, vif);
2808
2809 clear_bit(WL1271_FLAG_VIF_CHANGE_IN_PROGRESS, &wl->flags);
2810 return ret;
2811 }
2812
wlcore_join(struct wl1271 * wl,struct wl12xx_vif * wlvif)2813 static int wlcore_join(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2814 {
2815 int ret;
2816 bool is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
2817
2818 /*
2819 * One of the side effects of the JOIN command is that is clears
2820 * WPA/WPA2 keys from the chipset. Performing a JOIN while associated
2821 * to a WPA/WPA2 access point will therefore kill the data-path.
2822 * Currently the only valid scenario for JOIN during association
2823 * is on roaming, in which case we will also be given new keys.
2824 * Keep the below message for now, unless it starts bothering
2825 * users who really like to roam a lot :)
2826 */
2827 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
2828 wl1271_info("JOIN while associated.");
2829
2830 /* clear encryption type */
2831 wlvif->encryption_type = KEY_NONE;
2832
2833 if (is_ibss)
2834 ret = wl12xx_cmd_role_start_ibss(wl, wlvif);
2835 else {
2836 if (wl->quirks & WLCORE_QUIRK_START_STA_FAILS) {
2837 /*
2838 * TODO: this is an ugly workaround for wl12xx fw
2839 * bug - we are not able to tx/rx after the first
2840 * start_sta, so make dummy start+stop calls,
2841 * and then call start_sta again.
2842 * this should be fixed in the fw.
2843 */
2844 wl12xx_cmd_role_start_sta(wl, wlvif);
2845 wl12xx_cmd_role_stop_sta(wl, wlvif);
2846 }
2847
2848 ret = wl12xx_cmd_role_start_sta(wl, wlvif);
2849 }
2850
2851 return ret;
2852 }
2853
wl1271_ssid_set(struct wl12xx_vif * wlvif,struct sk_buff * skb,int offset)2854 static int wl1271_ssid_set(struct wl12xx_vif *wlvif, struct sk_buff *skb,
2855 int offset)
2856 {
2857 u8 ssid_len;
2858 const u8 *ptr = cfg80211_find_ie(WLAN_EID_SSID, skb->data + offset,
2859 skb->len - offset);
2860
2861 if (!ptr) {
2862 wl1271_error("No SSID in IEs!");
2863 return -ENOENT;
2864 }
2865
2866 ssid_len = ptr[1];
2867 if (ssid_len > IEEE80211_MAX_SSID_LEN) {
2868 wl1271_error("SSID is too long!");
2869 return -EINVAL;
2870 }
2871
2872 wlvif->ssid_len = ssid_len;
2873 memcpy(wlvif->ssid, ptr+2, ssid_len);
2874 return 0;
2875 }
2876
wlcore_set_ssid(struct wl1271 * wl,struct wl12xx_vif * wlvif)2877 static int wlcore_set_ssid(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2878 {
2879 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
2880 struct sk_buff *skb;
2881 int ieoffset;
2882
2883 /* we currently only support setting the ssid from the ap probe req */
2884 if (wlvif->bss_type != BSS_TYPE_STA_BSS)
2885 return -EINVAL;
2886
2887 skb = ieee80211_ap_probereq_get(wl->hw, vif);
2888 if (!skb)
2889 return -EINVAL;
2890
2891 ieoffset = offsetof(struct ieee80211_mgmt,
2892 u.probe_req.variable);
2893 wl1271_ssid_set(wlvif, skb, ieoffset);
2894 dev_kfree_skb(skb);
2895
2896 return 0;
2897 }
2898
wlcore_set_assoc(struct wl1271 * wl,struct wl12xx_vif * wlvif,struct ieee80211_bss_conf * bss_conf,u32 sta_rate_set)2899 static int wlcore_set_assoc(struct wl1271 *wl, struct wl12xx_vif *wlvif,
2900 struct ieee80211_bss_conf *bss_conf,
2901 u32 sta_rate_set)
2902 {
2903 int ieoffset;
2904 int ret;
2905
2906 wlvif->aid = bss_conf->aid;
2907 wlvif->channel_type = cfg80211_get_chandef_type(&bss_conf->chandef);
2908 wlvif->beacon_int = bss_conf->beacon_int;
2909 wlvif->wmm_enabled = bss_conf->qos;
2910
2911 set_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags);
2912
2913 /*
2914 * with wl1271, we don't need to update the
2915 * beacon_int and dtim_period, because the firmware
2916 * updates it by itself when the first beacon is
2917 * received after a join.
2918 */
2919 ret = wl1271_cmd_build_ps_poll(wl, wlvif, wlvif->aid);
2920 if (ret < 0)
2921 return ret;
2922
2923 /*
2924 * Get a template for hardware connection maintenance
2925 */
2926 dev_kfree_skb(wlvif->probereq);
2927 wlvif->probereq = wl1271_cmd_build_ap_probe_req(wl,
2928 wlvif,
2929 NULL);
2930 ieoffset = offsetof(struct ieee80211_mgmt,
2931 u.probe_req.variable);
2932 wl1271_ssid_set(wlvif, wlvif->probereq, ieoffset);
2933
2934 /* enable the connection monitoring feature */
2935 ret = wl1271_acx_conn_monit_params(wl, wlvif, true);
2936 if (ret < 0)
2937 return ret;
2938
2939 /*
2940 * The join command disable the keep-alive mode, shut down its process,
2941 * and also clear the template config, so we need to reset it all after
2942 * the join. The acx_aid starts the keep-alive process, and the order
2943 * of the commands below is relevant.
2944 */
2945 ret = wl1271_acx_keep_alive_mode(wl, wlvif, true);
2946 if (ret < 0)
2947 return ret;
2948
2949 ret = wl1271_acx_aid(wl, wlvif, wlvif->aid);
2950 if (ret < 0)
2951 return ret;
2952
2953 ret = wl12xx_cmd_build_klv_null_data(wl, wlvif);
2954 if (ret < 0)
2955 return ret;
2956
2957 ret = wl1271_acx_keep_alive_config(wl, wlvif,
2958 wlvif->sta.klv_template_id,
2959 ACX_KEEP_ALIVE_TPL_VALID);
2960 if (ret < 0)
2961 return ret;
2962
2963 /*
2964 * The default fw psm configuration is AUTO, while mac80211 default
2965 * setting is off (ACTIVE), so sync the fw with the correct value.
2966 */
2967 ret = wl1271_ps_set_mode(wl, wlvif, STATION_ACTIVE_MODE);
2968 if (ret < 0)
2969 return ret;
2970
2971 if (sta_rate_set) {
2972 wlvif->rate_set =
2973 wl1271_tx_enabled_rates_get(wl,
2974 sta_rate_set,
2975 wlvif->band);
2976 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
2977 if (ret < 0)
2978 return ret;
2979 }
2980
2981 return ret;
2982 }
2983
wlcore_unset_assoc(struct wl1271 * wl,struct wl12xx_vif * wlvif)2984 static int wlcore_unset_assoc(struct wl1271 *wl, struct wl12xx_vif *wlvif)
2985 {
2986 int ret;
2987 bool sta = wlvif->bss_type == BSS_TYPE_STA_BSS;
2988
2989 /* make sure we are connected (sta) joined */
2990 if (sta &&
2991 !test_and_clear_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
2992 return false;
2993
2994 /* make sure we are joined (ibss) */
2995 if (!sta &&
2996 test_and_clear_bit(WLVIF_FLAG_IBSS_JOINED, &wlvif->flags))
2997 return false;
2998
2999 if (sta) {
3000 /* use defaults when not associated */
3001 wlvif->aid = 0;
3002
3003 /* free probe-request template */
3004 dev_kfree_skb(wlvif->probereq);
3005 wlvif->probereq = NULL;
3006
3007 /* disable connection monitor features */
3008 ret = wl1271_acx_conn_monit_params(wl, wlvif, false);
3009 if (ret < 0)
3010 return ret;
3011
3012 /* Disable the keep-alive feature */
3013 ret = wl1271_acx_keep_alive_mode(wl, wlvif, false);
3014 if (ret < 0)
3015 return ret;
3016
3017 /* disable beacon filtering */
3018 ret = wl1271_acx_beacon_filter_opt(wl, wlvif, false);
3019 if (ret < 0)
3020 return ret;
3021 }
3022
3023 if (test_and_clear_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags)) {
3024 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
3025
3026 wl12xx_cmd_stop_channel_switch(wl, wlvif);
3027 ieee80211_chswitch_done(vif, false);
3028 cancel_delayed_work(&wlvif->channel_switch_work);
3029 }
3030
3031 /* invalidate keep-alive template */
3032 wl1271_acx_keep_alive_config(wl, wlvif,
3033 wlvif->sta.klv_template_id,
3034 ACX_KEEP_ALIVE_TPL_INVALID);
3035
3036 return 0;
3037 }
3038
wl1271_set_band_rate(struct wl1271 * wl,struct wl12xx_vif * wlvif)3039 static void wl1271_set_band_rate(struct wl1271 *wl, struct wl12xx_vif *wlvif)
3040 {
3041 wlvif->basic_rate_set = wlvif->bitrate_masks[wlvif->band];
3042 wlvif->rate_set = wlvif->basic_rate_set;
3043 }
3044
wl1271_sta_handle_idle(struct wl1271 * wl,struct wl12xx_vif * wlvif,bool idle)3045 static void wl1271_sta_handle_idle(struct wl1271 *wl, struct wl12xx_vif *wlvif,
3046 bool idle)
3047 {
3048 bool cur_idle = !test_bit(WLVIF_FLAG_ACTIVE, &wlvif->flags);
3049
3050 if (idle == cur_idle)
3051 return;
3052
3053 if (idle) {
3054 clear_bit(WLVIF_FLAG_ACTIVE, &wlvif->flags);
3055 } else {
3056 /* The current firmware only supports sched_scan in idle */
3057 if (wl->sched_vif == wlvif)
3058 wl->ops->sched_scan_stop(wl, wlvif);
3059
3060 set_bit(WLVIF_FLAG_ACTIVE, &wlvif->flags);
3061 }
3062 }
3063
wl12xx_config_vif(struct wl1271 * wl,struct wl12xx_vif * wlvif,struct ieee80211_conf * conf,u32 changed)3064 static int wl12xx_config_vif(struct wl1271 *wl, struct wl12xx_vif *wlvif,
3065 struct ieee80211_conf *conf, u32 changed)
3066 {
3067 int ret;
3068
3069 if (wlcore_is_p2p_mgmt(wlvif))
3070 return 0;
3071
3072 if (conf->power_level != wlvif->power_level) {
3073 ret = wl1271_acx_tx_power(wl, wlvif, conf->power_level);
3074 if (ret < 0)
3075 return ret;
3076
3077 wlvif->power_level = conf->power_level;
3078 }
3079
3080 return 0;
3081 }
3082
wl1271_op_config(struct ieee80211_hw * hw,u32 changed)3083 static int wl1271_op_config(struct ieee80211_hw *hw, u32 changed)
3084 {
3085 struct wl1271 *wl = hw->priv;
3086 struct wl12xx_vif *wlvif;
3087 struct ieee80211_conf *conf = &hw->conf;
3088 int ret = 0;
3089
3090 wl1271_debug(DEBUG_MAC80211, "mac80211 config psm %s power %d %s"
3091 " changed 0x%x",
3092 conf->flags & IEEE80211_CONF_PS ? "on" : "off",
3093 conf->power_level,
3094 conf->flags & IEEE80211_CONF_IDLE ? "idle" : "in use",
3095 changed);
3096
3097 mutex_lock(&wl->mutex);
3098
3099 if (changed & IEEE80211_CONF_CHANGE_POWER)
3100 wl->power_level = conf->power_level;
3101
3102 if (unlikely(wl->state != WLCORE_STATE_ON))
3103 goto out;
3104
3105 ret = wl1271_ps_elp_wakeup(wl);
3106 if (ret < 0)
3107 goto out;
3108
3109 /* configure each interface */
3110 wl12xx_for_each_wlvif(wl, wlvif) {
3111 ret = wl12xx_config_vif(wl, wlvif, conf, changed);
3112 if (ret < 0)
3113 goto out_sleep;
3114 }
3115
3116 out_sleep:
3117 wl1271_ps_elp_sleep(wl);
3118
3119 out:
3120 mutex_unlock(&wl->mutex);
3121
3122 return ret;
3123 }
3124
3125 struct wl1271_filter_params {
3126 bool enabled;
3127 int mc_list_length;
3128 u8 mc_list[ACX_MC_ADDRESS_GROUP_MAX][ETH_ALEN];
3129 };
3130
wl1271_op_prepare_multicast(struct ieee80211_hw * hw,struct netdev_hw_addr_list * mc_list)3131 static u64 wl1271_op_prepare_multicast(struct ieee80211_hw *hw,
3132 struct netdev_hw_addr_list *mc_list)
3133 {
3134 struct wl1271_filter_params *fp;
3135 struct netdev_hw_addr *ha;
3136
3137 fp = kzalloc(sizeof(*fp), GFP_ATOMIC);
3138 if (!fp) {
3139 wl1271_error("Out of memory setting filters.");
3140 return 0;
3141 }
3142
3143 /* update multicast filtering parameters */
3144 fp->mc_list_length = 0;
3145 if (netdev_hw_addr_list_count(mc_list) > ACX_MC_ADDRESS_GROUP_MAX) {
3146 fp->enabled = false;
3147 } else {
3148 fp->enabled = true;
3149 netdev_hw_addr_list_for_each(ha, mc_list) {
3150 memcpy(fp->mc_list[fp->mc_list_length],
3151 ha->addr, ETH_ALEN);
3152 fp->mc_list_length++;
3153 }
3154 }
3155
3156 return (u64)(unsigned long)fp;
3157 }
3158
3159 #define WL1271_SUPPORTED_FILTERS (FIF_ALLMULTI | \
3160 FIF_FCSFAIL | \
3161 FIF_BCN_PRBRESP_PROMISC | \
3162 FIF_CONTROL | \
3163 FIF_OTHER_BSS)
3164
wl1271_op_configure_filter(struct ieee80211_hw * hw,unsigned int changed,unsigned int * total,u64 multicast)3165 static void wl1271_op_configure_filter(struct ieee80211_hw *hw,
3166 unsigned int changed,
3167 unsigned int *total, u64 multicast)
3168 {
3169 struct wl1271_filter_params *fp = (void *)(unsigned long)multicast;
3170 struct wl1271 *wl = hw->priv;
3171 struct wl12xx_vif *wlvif;
3172
3173 int ret;
3174
3175 wl1271_debug(DEBUG_MAC80211, "mac80211 configure filter changed %x"
3176 " total %x", changed, *total);
3177
3178 mutex_lock(&wl->mutex);
3179
3180 *total &= WL1271_SUPPORTED_FILTERS;
3181 changed &= WL1271_SUPPORTED_FILTERS;
3182
3183 if (unlikely(wl->state != WLCORE_STATE_ON))
3184 goto out;
3185
3186 ret = wl1271_ps_elp_wakeup(wl);
3187 if (ret < 0)
3188 goto out;
3189
3190 wl12xx_for_each_wlvif(wl, wlvif) {
3191 if (wlcore_is_p2p_mgmt(wlvif))
3192 continue;
3193
3194 if (wlvif->bss_type != BSS_TYPE_AP_BSS) {
3195 if (*total & FIF_ALLMULTI)
3196 ret = wl1271_acx_group_address_tbl(wl, wlvif,
3197 false,
3198 NULL, 0);
3199 else if (fp)
3200 ret = wl1271_acx_group_address_tbl(wl, wlvif,
3201 fp->enabled,
3202 fp->mc_list,
3203 fp->mc_list_length);
3204 if (ret < 0)
3205 goto out_sleep;
3206 }
3207
3208 /*
3209 * If interface in AP mode and created with allmulticast then disable
3210 * the firmware filters so that all multicast packets are passed
3211 * This is mandatory for MDNS based discovery protocols
3212 */
3213 if (wlvif->bss_type == BSS_TYPE_AP_BSS) {
3214 if (*total & FIF_ALLMULTI) {
3215 ret = wl1271_acx_group_address_tbl(wl, wlvif,
3216 false,
3217 NULL, 0);
3218 if (ret < 0)
3219 goto out_sleep;
3220 }
3221 }
3222 }
3223
3224 /*
3225 * the fw doesn't provide an api to configure the filters. instead,
3226 * the filters configuration is based on the active roles / ROC
3227 * state.
3228 */
3229
3230 out_sleep:
3231 wl1271_ps_elp_sleep(wl);
3232
3233 out:
3234 mutex_unlock(&wl->mutex);
3235 kfree(fp);
3236 }
3237
wl1271_record_ap_key(struct wl1271 * wl,struct wl12xx_vif * wlvif,u8 id,u8 key_type,u8 key_size,const u8 * key,u8 hlid,u32 tx_seq_32,u16 tx_seq_16)3238 static int wl1271_record_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
3239 u8 id, u8 key_type, u8 key_size,
3240 const u8 *key, u8 hlid, u32 tx_seq_32,
3241 u16 tx_seq_16)
3242 {
3243 struct wl1271_ap_key *ap_key;
3244 int i;
3245
3246 wl1271_debug(DEBUG_CRYPT, "record ap key id %d", (int)id);
3247
3248 if (key_size > MAX_KEY_SIZE)
3249 return -EINVAL;
3250
3251 /*
3252 * Find next free entry in ap_keys. Also check we are not replacing
3253 * an existing key.
3254 */
3255 for (i = 0; i < MAX_NUM_KEYS; i++) {
3256 if (wlvif->ap.recorded_keys[i] == NULL)
3257 break;
3258
3259 if (wlvif->ap.recorded_keys[i]->id == id) {
3260 wl1271_warning("trying to record key replacement");
3261 return -EINVAL;
3262 }
3263 }
3264
3265 if (i == MAX_NUM_KEYS)
3266 return -EBUSY;
3267
3268 ap_key = kzalloc(sizeof(*ap_key), GFP_KERNEL);
3269 if (!ap_key)
3270 return -ENOMEM;
3271
3272 ap_key->id = id;
3273 ap_key->key_type = key_type;
3274 ap_key->key_size = key_size;
3275 memcpy(ap_key->key, key, key_size);
3276 ap_key->hlid = hlid;
3277 ap_key->tx_seq_32 = tx_seq_32;
3278 ap_key->tx_seq_16 = tx_seq_16;
3279
3280 wlvif->ap.recorded_keys[i] = ap_key;
3281 return 0;
3282 }
3283
wl1271_free_ap_keys(struct wl1271 * wl,struct wl12xx_vif * wlvif)3284 static void wl1271_free_ap_keys(struct wl1271 *wl, struct wl12xx_vif *wlvif)
3285 {
3286 int i;
3287
3288 for (i = 0; i < MAX_NUM_KEYS; i++) {
3289 kfree(wlvif->ap.recorded_keys[i]);
3290 wlvif->ap.recorded_keys[i] = NULL;
3291 }
3292 }
3293
wl1271_ap_init_hwenc(struct wl1271 * wl,struct wl12xx_vif * wlvif)3294 static int wl1271_ap_init_hwenc(struct wl1271 *wl, struct wl12xx_vif *wlvif)
3295 {
3296 int i, ret = 0;
3297 struct wl1271_ap_key *key;
3298 bool wep_key_added = false;
3299
3300 for (i = 0; i < MAX_NUM_KEYS; i++) {
3301 u8 hlid;
3302 if (wlvif->ap.recorded_keys[i] == NULL)
3303 break;
3304
3305 key = wlvif->ap.recorded_keys[i];
3306 hlid = key->hlid;
3307 if (hlid == WL12XX_INVALID_LINK_ID)
3308 hlid = wlvif->ap.bcast_hlid;
3309
3310 ret = wl1271_cmd_set_ap_key(wl, wlvif, KEY_ADD_OR_REPLACE,
3311 key->id, key->key_type,
3312 key->key_size, key->key,
3313 hlid, key->tx_seq_32,
3314 key->tx_seq_16);
3315 if (ret < 0)
3316 goto out;
3317
3318 if (key->key_type == KEY_WEP)
3319 wep_key_added = true;
3320 }
3321
3322 if (wep_key_added) {
3323 ret = wl12xx_cmd_set_default_wep_key(wl, wlvif->default_key,
3324 wlvif->ap.bcast_hlid);
3325 if (ret < 0)
3326 goto out;
3327 }
3328
3329 out:
3330 wl1271_free_ap_keys(wl, wlvif);
3331 return ret;
3332 }
3333
wl1271_set_key(struct wl1271 * wl,struct wl12xx_vif * wlvif,u16 action,u8 id,u8 key_type,u8 key_size,const u8 * key,u32 tx_seq_32,u16 tx_seq_16,struct ieee80211_sta * sta)3334 static int wl1271_set_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
3335 u16 action, u8 id, u8 key_type,
3336 u8 key_size, const u8 *key, u32 tx_seq_32,
3337 u16 tx_seq_16, struct ieee80211_sta *sta)
3338 {
3339 int ret;
3340 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
3341
3342 if (is_ap) {
3343 struct wl1271_station *wl_sta;
3344 u8 hlid;
3345
3346 if (sta) {
3347 wl_sta = (struct wl1271_station *)sta->drv_priv;
3348 hlid = wl_sta->hlid;
3349 } else {
3350 hlid = wlvif->ap.bcast_hlid;
3351 }
3352
3353 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
3354 /*
3355 * We do not support removing keys after AP shutdown.
3356 * Pretend we do to make mac80211 happy.
3357 */
3358 if (action != KEY_ADD_OR_REPLACE)
3359 return 0;
3360
3361 ret = wl1271_record_ap_key(wl, wlvif, id,
3362 key_type, key_size,
3363 key, hlid, tx_seq_32,
3364 tx_seq_16);
3365 } else {
3366 ret = wl1271_cmd_set_ap_key(wl, wlvif, action,
3367 id, key_type, key_size,
3368 key, hlid, tx_seq_32,
3369 tx_seq_16);
3370 }
3371
3372 if (ret < 0)
3373 return ret;
3374 } else {
3375 const u8 *addr;
3376 static const u8 bcast_addr[ETH_ALEN] = {
3377 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
3378 };
3379
3380 addr = sta ? sta->addr : bcast_addr;
3381
3382 if (is_zero_ether_addr(addr)) {
3383 /* We dont support TX only encryption */
3384 return -EOPNOTSUPP;
3385 }
3386
3387 /* The wl1271 does not allow to remove unicast keys - they
3388 will be cleared automatically on next CMD_JOIN. Ignore the
3389 request silently, as we dont want the mac80211 to emit
3390 an error message. */
3391 if (action == KEY_REMOVE && !is_broadcast_ether_addr(addr))
3392 return 0;
3393
3394 /* don't remove key if hlid was already deleted */
3395 if (action == KEY_REMOVE &&
3396 wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
3397 return 0;
3398
3399 ret = wl1271_cmd_set_sta_key(wl, wlvif, action,
3400 id, key_type, key_size,
3401 key, addr, tx_seq_32,
3402 tx_seq_16);
3403 if (ret < 0)
3404 return ret;
3405
3406 }
3407
3408 return 0;
3409 }
3410
wlcore_op_set_key(struct ieee80211_hw * hw,enum set_key_cmd cmd,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key_conf)3411 static int wlcore_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
3412 struct ieee80211_vif *vif,
3413 struct ieee80211_sta *sta,
3414 struct ieee80211_key_conf *key_conf)
3415 {
3416 struct wl1271 *wl = hw->priv;
3417 int ret;
3418 bool might_change_spare =
3419 key_conf->cipher == WL1271_CIPHER_SUITE_GEM ||
3420 key_conf->cipher == WLAN_CIPHER_SUITE_TKIP;
3421
3422 if (might_change_spare) {
3423 /*
3424 * stop the queues and flush to ensure the next packets are
3425 * in sync with FW spare block accounting
3426 */
3427 wlcore_stop_queues(wl, WLCORE_QUEUE_STOP_REASON_SPARE_BLK);
3428 wl1271_tx_flush(wl);
3429 }
3430
3431 mutex_lock(&wl->mutex);
3432
3433 if (unlikely(wl->state != WLCORE_STATE_ON)) {
3434 ret = -EAGAIN;
3435 goto out_wake_queues;
3436 }
3437
3438 ret = wl1271_ps_elp_wakeup(wl);
3439 if (ret < 0)
3440 goto out_wake_queues;
3441
3442 ret = wlcore_hw_set_key(wl, cmd, vif, sta, key_conf);
3443
3444 wl1271_ps_elp_sleep(wl);
3445
3446 out_wake_queues:
3447 if (might_change_spare)
3448 wlcore_wake_queues(wl, WLCORE_QUEUE_STOP_REASON_SPARE_BLK);
3449
3450 mutex_unlock(&wl->mutex);
3451
3452 return ret;
3453 }
3454
wlcore_set_key(struct wl1271 * wl,enum set_key_cmd cmd,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key_conf)3455 int wlcore_set_key(struct wl1271 *wl, enum set_key_cmd cmd,
3456 struct ieee80211_vif *vif,
3457 struct ieee80211_sta *sta,
3458 struct ieee80211_key_conf *key_conf)
3459 {
3460 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3461 int ret;
3462 u32 tx_seq_32 = 0;
3463 u16 tx_seq_16 = 0;
3464 u8 key_type;
3465 u8 hlid;
3466
3467 wl1271_debug(DEBUG_MAC80211, "mac80211 set key");
3468
3469 wl1271_debug(DEBUG_CRYPT, "CMD: 0x%x sta: %p", cmd, sta);
3470 wl1271_debug(DEBUG_CRYPT, "Key: algo:0x%x, id:%d, len:%d flags 0x%x",
3471 key_conf->cipher, key_conf->keyidx,
3472 key_conf->keylen, key_conf->flags);
3473 wl1271_dump(DEBUG_CRYPT, "KEY: ", key_conf->key, key_conf->keylen);
3474
3475 if (wlvif->bss_type == BSS_TYPE_AP_BSS)
3476 if (sta) {
3477 struct wl1271_station *wl_sta = (void *)sta->drv_priv;
3478 hlid = wl_sta->hlid;
3479 } else {
3480 hlid = wlvif->ap.bcast_hlid;
3481 }
3482 else
3483 hlid = wlvif->sta.hlid;
3484
3485 if (hlid != WL12XX_INVALID_LINK_ID) {
3486 u64 tx_seq = wl->links[hlid].total_freed_pkts;
3487 tx_seq_32 = WL1271_TX_SECURITY_HI32(tx_seq);
3488 tx_seq_16 = WL1271_TX_SECURITY_LO16(tx_seq);
3489 }
3490
3491 switch (key_conf->cipher) {
3492 case WLAN_CIPHER_SUITE_WEP40:
3493 case WLAN_CIPHER_SUITE_WEP104:
3494 key_type = KEY_WEP;
3495
3496 key_conf->hw_key_idx = key_conf->keyidx;
3497 break;
3498 case WLAN_CIPHER_SUITE_TKIP:
3499 key_type = KEY_TKIP;
3500 key_conf->hw_key_idx = key_conf->keyidx;
3501 break;
3502 case WLAN_CIPHER_SUITE_CCMP:
3503 key_type = KEY_AES;
3504 key_conf->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3505 break;
3506 case WL1271_CIPHER_SUITE_GEM:
3507 key_type = KEY_GEM;
3508 break;
3509 default:
3510 wl1271_error("Unknown key algo 0x%x", key_conf->cipher);
3511
3512 return -EOPNOTSUPP;
3513 }
3514
3515 switch (cmd) {
3516 case SET_KEY:
3517 ret = wl1271_set_key(wl, wlvif, KEY_ADD_OR_REPLACE,
3518 key_conf->keyidx, key_type,
3519 key_conf->keylen, key_conf->key,
3520 tx_seq_32, tx_seq_16, sta);
3521 if (ret < 0) {
3522 wl1271_error("Could not add or replace key");
3523 return ret;
3524 }
3525
3526 /*
3527 * reconfiguring arp response if the unicast (or common)
3528 * encryption key type was changed
3529 */
3530 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
3531 (sta || key_type == KEY_WEP) &&
3532 wlvif->encryption_type != key_type) {
3533 wlvif->encryption_type = key_type;
3534 ret = wl1271_cmd_build_arp_rsp(wl, wlvif);
3535 if (ret < 0) {
3536 wl1271_warning("build arp rsp failed: %d", ret);
3537 return ret;
3538 }
3539 }
3540 break;
3541
3542 case DISABLE_KEY:
3543 ret = wl1271_set_key(wl, wlvif, KEY_REMOVE,
3544 key_conf->keyidx, key_type,
3545 key_conf->keylen, key_conf->key,
3546 0, 0, sta);
3547 if (ret < 0) {
3548 wl1271_error("Could not remove key");
3549 return ret;
3550 }
3551 break;
3552
3553 default:
3554 wl1271_error("Unsupported key cmd 0x%x", cmd);
3555 return -EOPNOTSUPP;
3556 }
3557
3558 return ret;
3559 }
3560 EXPORT_SYMBOL_GPL(wlcore_set_key);
3561
wl1271_op_set_default_key_idx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,int key_idx)3562 static void wl1271_op_set_default_key_idx(struct ieee80211_hw *hw,
3563 struct ieee80211_vif *vif,
3564 int key_idx)
3565 {
3566 struct wl1271 *wl = hw->priv;
3567 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3568 int ret;
3569
3570 wl1271_debug(DEBUG_MAC80211, "mac80211 set default key idx %d",
3571 key_idx);
3572
3573 /* we don't handle unsetting of default key */
3574 if (key_idx == -1)
3575 return;
3576
3577 mutex_lock(&wl->mutex);
3578
3579 if (unlikely(wl->state != WLCORE_STATE_ON)) {
3580 ret = -EAGAIN;
3581 goto out_unlock;
3582 }
3583
3584 ret = wl1271_ps_elp_wakeup(wl);
3585 if (ret < 0)
3586 goto out_unlock;
3587
3588 wlvif->default_key = key_idx;
3589
3590 /* the default WEP key needs to be configured at least once */
3591 if (wlvif->encryption_type == KEY_WEP) {
3592 ret = wl12xx_cmd_set_default_wep_key(wl,
3593 key_idx,
3594 wlvif->sta.hlid);
3595 if (ret < 0)
3596 goto out_sleep;
3597 }
3598
3599 out_sleep:
3600 wl1271_ps_elp_sleep(wl);
3601
3602 out_unlock:
3603 mutex_unlock(&wl->mutex);
3604 }
3605
wlcore_regdomain_config(struct wl1271 * wl)3606 void wlcore_regdomain_config(struct wl1271 *wl)
3607 {
3608 int ret;
3609
3610 if (!(wl->quirks & WLCORE_QUIRK_REGDOMAIN_CONF))
3611 return;
3612
3613 mutex_lock(&wl->mutex);
3614
3615 if (unlikely(wl->state != WLCORE_STATE_ON))
3616 goto out;
3617
3618 ret = wl1271_ps_elp_wakeup(wl);
3619 if (ret < 0)
3620 goto out;
3621
3622 ret = wlcore_cmd_regdomain_config_locked(wl);
3623 if (ret < 0) {
3624 wl12xx_queue_recovery_work(wl);
3625 goto out;
3626 }
3627
3628 wl1271_ps_elp_sleep(wl);
3629 out:
3630 mutex_unlock(&wl->mutex);
3631 }
3632
wl1271_op_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_scan_request * hw_req)3633 static int wl1271_op_hw_scan(struct ieee80211_hw *hw,
3634 struct ieee80211_vif *vif,
3635 struct ieee80211_scan_request *hw_req)
3636 {
3637 struct cfg80211_scan_request *req = &hw_req->req;
3638 struct wl1271 *wl = hw->priv;
3639 int ret;
3640 u8 *ssid = NULL;
3641 size_t len = 0;
3642
3643 wl1271_debug(DEBUG_MAC80211, "mac80211 hw scan");
3644
3645 if (req->n_ssids) {
3646 ssid = req->ssids[0].ssid;
3647 len = req->ssids[0].ssid_len;
3648 }
3649
3650 mutex_lock(&wl->mutex);
3651
3652 if (unlikely(wl->state != WLCORE_STATE_ON)) {
3653 /*
3654 * We cannot return -EBUSY here because cfg80211 will expect
3655 * a call to ieee80211_scan_completed if we do - in this case
3656 * there won't be any call.
3657 */
3658 ret = -EAGAIN;
3659 goto out;
3660 }
3661
3662 ret = wl1271_ps_elp_wakeup(wl);
3663 if (ret < 0)
3664 goto out;
3665
3666 /* fail if there is any role in ROC */
3667 if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES) {
3668 /* don't allow scanning right now */
3669 ret = -EBUSY;
3670 goto out_sleep;
3671 }
3672
3673 ret = wlcore_scan(hw->priv, vif, ssid, len, req);
3674 out_sleep:
3675 wl1271_ps_elp_sleep(wl);
3676 out:
3677 mutex_unlock(&wl->mutex);
3678
3679 return ret;
3680 }
3681
wl1271_op_cancel_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif)3682 static void wl1271_op_cancel_hw_scan(struct ieee80211_hw *hw,
3683 struct ieee80211_vif *vif)
3684 {
3685 struct wl1271 *wl = hw->priv;
3686 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3687 struct cfg80211_scan_info info = {
3688 .aborted = true,
3689 };
3690 int ret;
3691
3692 wl1271_debug(DEBUG_MAC80211, "mac80211 cancel hw scan");
3693
3694 mutex_lock(&wl->mutex);
3695
3696 if (unlikely(wl->state != WLCORE_STATE_ON))
3697 goto out;
3698
3699 if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
3700 goto out;
3701
3702 ret = wl1271_ps_elp_wakeup(wl);
3703 if (ret < 0)
3704 goto out;
3705
3706 if (wl->scan.state != WL1271_SCAN_STATE_DONE) {
3707 ret = wl->ops->scan_stop(wl, wlvif);
3708 if (ret < 0)
3709 goto out_sleep;
3710 }
3711
3712 /*
3713 * Rearm the tx watchdog just before idling scan. This
3714 * prevents just-finished scans from triggering the watchdog
3715 */
3716 wl12xx_rearm_tx_watchdog_locked(wl);
3717
3718 wl->scan.state = WL1271_SCAN_STATE_IDLE;
3719 memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
3720 wl->scan_wlvif = NULL;
3721 wl->scan.req = NULL;
3722 ieee80211_scan_completed(wl->hw, &info);
3723
3724 out_sleep:
3725 wl1271_ps_elp_sleep(wl);
3726 out:
3727 mutex_unlock(&wl->mutex);
3728
3729 cancel_delayed_work_sync(&wl->scan_complete_work);
3730 }
3731
wl1271_op_sched_scan_start(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_sched_scan_request * req,struct ieee80211_scan_ies * ies)3732 static int wl1271_op_sched_scan_start(struct ieee80211_hw *hw,
3733 struct ieee80211_vif *vif,
3734 struct cfg80211_sched_scan_request *req,
3735 struct ieee80211_scan_ies *ies)
3736 {
3737 struct wl1271 *wl = hw->priv;
3738 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3739 int ret;
3740
3741 wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_start");
3742
3743 mutex_lock(&wl->mutex);
3744
3745 if (unlikely(wl->state != WLCORE_STATE_ON)) {
3746 ret = -EAGAIN;
3747 goto out;
3748 }
3749
3750 ret = wl1271_ps_elp_wakeup(wl);
3751 if (ret < 0)
3752 goto out;
3753
3754 ret = wl->ops->sched_scan_start(wl, wlvif, req, ies);
3755 if (ret < 0)
3756 goto out_sleep;
3757
3758 wl->sched_vif = wlvif;
3759
3760 out_sleep:
3761 wl1271_ps_elp_sleep(wl);
3762 out:
3763 mutex_unlock(&wl->mutex);
3764 return ret;
3765 }
3766
wl1271_op_sched_scan_stop(struct ieee80211_hw * hw,struct ieee80211_vif * vif)3767 static int wl1271_op_sched_scan_stop(struct ieee80211_hw *hw,
3768 struct ieee80211_vif *vif)
3769 {
3770 struct wl1271 *wl = hw->priv;
3771 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3772 int ret;
3773
3774 wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_stop");
3775
3776 mutex_lock(&wl->mutex);
3777
3778 if (unlikely(wl->state != WLCORE_STATE_ON))
3779 goto out;
3780
3781 ret = wl1271_ps_elp_wakeup(wl);
3782 if (ret < 0)
3783 goto out;
3784
3785 wl->ops->sched_scan_stop(wl, wlvif);
3786
3787 wl1271_ps_elp_sleep(wl);
3788 out:
3789 mutex_unlock(&wl->mutex);
3790
3791 return 0;
3792 }
3793
wl1271_op_set_frag_threshold(struct ieee80211_hw * hw,u32 value)3794 static int wl1271_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
3795 {
3796 struct wl1271 *wl = hw->priv;
3797 int ret = 0;
3798
3799 mutex_lock(&wl->mutex);
3800
3801 if (unlikely(wl->state != WLCORE_STATE_ON)) {
3802 ret = -EAGAIN;
3803 goto out;
3804 }
3805
3806 ret = wl1271_ps_elp_wakeup(wl);
3807 if (ret < 0)
3808 goto out;
3809
3810 ret = wl1271_acx_frag_threshold(wl, value);
3811 if (ret < 0)
3812 wl1271_warning("wl1271_op_set_frag_threshold failed: %d", ret);
3813
3814 wl1271_ps_elp_sleep(wl);
3815
3816 out:
3817 mutex_unlock(&wl->mutex);
3818
3819 return ret;
3820 }
3821
wl1271_op_set_rts_threshold(struct ieee80211_hw * hw,u32 value)3822 static int wl1271_op_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3823 {
3824 struct wl1271 *wl = hw->priv;
3825 struct wl12xx_vif *wlvif;
3826 int ret = 0;
3827
3828 mutex_lock(&wl->mutex);
3829
3830 if (unlikely(wl->state != WLCORE_STATE_ON)) {
3831 ret = -EAGAIN;
3832 goto out;
3833 }
3834
3835 ret = wl1271_ps_elp_wakeup(wl);
3836 if (ret < 0)
3837 goto out;
3838
3839 wl12xx_for_each_wlvif(wl, wlvif) {
3840 ret = wl1271_acx_rts_threshold(wl, wlvif, value);
3841 if (ret < 0)
3842 wl1271_warning("set rts threshold failed: %d", ret);
3843 }
3844 wl1271_ps_elp_sleep(wl);
3845
3846 out:
3847 mutex_unlock(&wl->mutex);
3848
3849 return ret;
3850 }
3851
wl12xx_remove_ie(struct sk_buff * skb,u8 eid,int ieoffset)3852 static void wl12xx_remove_ie(struct sk_buff *skb, u8 eid, int ieoffset)
3853 {
3854 int len;
3855 const u8 *next, *end = skb->data + skb->len;
3856 u8 *ie = (u8 *)cfg80211_find_ie(eid, skb->data + ieoffset,
3857 skb->len - ieoffset);
3858 if (!ie)
3859 return;
3860 len = ie[1] + 2;
3861 next = ie + len;
3862 memmove(ie, next, end - next);
3863 skb_trim(skb, skb->len - len);
3864 }
3865
wl12xx_remove_vendor_ie(struct sk_buff * skb,unsigned int oui,u8 oui_type,int ieoffset)3866 static void wl12xx_remove_vendor_ie(struct sk_buff *skb,
3867 unsigned int oui, u8 oui_type,
3868 int ieoffset)
3869 {
3870 int len;
3871 const u8 *next, *end = skb->data + skb->len;
3872 u8 *ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
3873 skb->data + ieoffset,
3874 skb->len - ieoffset);
3875 if (!ie)
3876 return;
3877 len = ie[1] + 2;
3878 next = ie + len;
3879 memmove(ie, next, end - next);
3880 skb_trim(skb, skb->len - len);
3881 }
3882
wl1271_ap_set_probe_resp_tmpl(struct wl1271 * wl,u32 rates,struct ieee80211_vif * vif)3883 static int wl1271_ap_set_probe_resp_tmpl(struct wl1271 *wl, u32 rates,
3884 struct ieee80211_vif *vif)
3885 {
3886 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3887 struct sk_buff *skb;
3888 int ret;
3889
3890 skb = ieee80211_proberesp_get(wl->hw, vif);
3891 if (!skb)
3892 return -EOPNOTSUPP;
3893
3894 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
3895 CMD_TEMPL_AP_PROBE_RESPONSE,
3896 skb->data,
3897 skb->len, 0,
3898 rates);
3899 dev_kfree_skb(skb);
3900
3901 if (ret < 0)
3902 goto out;
3903
3904 wl1271_debug(DEBUG_AP, "probe response updated");
3905 set_bit(WLVIF_FLAG_AP_PROBE_RESP_SET, &wlvif->flags);
3906
3907 out:
3908 return ret;
3909 }
3910
wl1271_ap_set_probe_resp_tmpl_legacy(struct wl1271 * wl,struct ieee80211_vif * vif,u8 * probe_rsp_data,size_t probe_rsp_len,u32 rates)3911 static int wl1271_ap_set_probe_resp_tmpl_legacy(struct wl1271 *wl,
3912 struct ieee80211_vif *vif,
3913 u8 *probe_rsp_data,
3914 size_t probe_rsp_len,
3915 u32 rates)
3916 {
3917 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3918 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
3919 u8 probe_rsp_templ[WL1271_CMD_TEMPL_MAX_SIZE];
3920 int ssid_ie_offset, ie_offset, templ_len;
3921 const u8 *ptr;
3922
3923 /* no need to change probe response if the SSID is set correctly */
3924 if (wlvif->ssid_len > 0)
3925 return wl1271_cmd_template_set(wl, wlvif->role_id,
3926 CMD_TEMPL_AP_PROBE_RESPONSE,
3927 probe_rsp_data,
3928 probe_rsp_len, 0,
3929 rates);
3930
3931 if (probe_rsp_len + bss_conf->ssid_len > WL1271_CMD_TEMPL_MAX_SIZE) {
3932 wl1271_error("probe_rsp template too big");
3933 return -EINVAL;
3934 }
3935
3936 /* start searching from IE offset */
3937 ie_offset = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
3938
3939 ptr = cfg80211_find_ie(WLAN_EID_SSID, probe_rsp_data + ie_offset,
3940 probe_rsp_len - ie_offset);
3941 if (!ptr) {
3942 wl1271_error("No SSID in beacon!");
3943 return -EINVAL;
3944 }
3945
3946 ssid_ie_offset = ptr - probe_rsp_data;
3947 ptr += (ptr[1] + 2);
3948
3949 memcpy(probe_rsp_templ, probe_rsp_data, ssid_ie_offset);
3950
3951 /* insert SSID from bss_conf */
3952 probe_rsp_templ[ssid_ie_offset] = WLAN_EID_SSID;
3953 probe_rsp_templ[ssid_ie_offset + 1] = bss_conf->ssid_len;
3954 memcpy(probe_rsp_templ + ssid_ie_offset + 2,
3955 bss_conf->ssid, bss_conf->ssid_len);
3956 templ_len = ssid_ie_offset + 2 + bss_conf->ssid_len;
3957
3958 memcpy(probe_rsp_templ + ssid_ie_offset + 2 + bss_conf->ssid_len,
3959 ptr, probe_rsp_len - (ptr - probe_rsp_data));
3960 templ_len += probe_rsp_len - (ptr - probe_rsp_data);
3961
3962 return wl1271_cmd_template_set(wl, wlvif->role_id,
3963 CMD_TEMPL_AP_PROBE_RESPONSE,
3964 probe_rsp_templ,
3965 templ_len, 0,
3966 rates);
3967 }
3968
wl1271_bss_erp_info_changed(struct wl1271 * wl,struct ieee80211_vif * vif,struct ieee80211_bss_conf * bss_conf,u32 changed)3969 static int wl1271_bss_erp_info_changed(struct wl1271 *wl,
3970 struct ieee80211_vif *vif,
3971 struct ieee80211_bss_conf *bss_conf,
3972 u32 changed)
3973 {
3974 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3975 int ret = 0;
3976
3977 if (changed & BSS_CHANGED_ERP_SLOT) {
3978 if (bss_conf->use_short_slot)
3979 ret = wl1271_acx_slot(wl, wlvif, SLOT_TIME_SHORT);
3980 else
3981 ret = wl1271_acx_slot(wl, wlvif, SLOT_TIME_LONG);
3982 if (ret < 0) {
3983 wl1271_warning("Set slot time failed %d", ret);
3984 goto out;
3985 }
3986 }
3987
3988 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3989 if (bss_conf->use_short_preamble)
3990 wl1271_acx_set_preamble(wl, wlvif, ACX_PREAMBLE_SHORT);
3991 else
3992 wl1271_acx_set_preamble(wl, wlvif, ACX_PREAMBLE_LONG);
3993 }
3994
3995 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
3996 if (bss_conf->use_cts_prot)
3997 ret = wl1271_acx_cts_protect(wl, wlvif,
3998 CTSPROTECT_ENABLE);
3999 else
4000 ret = wl1271_acx_cts_protect(wl, wlvif,
4001 CTSPROTECT_DISABLE);
4002 if (ret < 0) {
4003 wl1271_warning("Set ctsprotect failed %d", ret);
4004 goto out;
4005 }
4006 }
4007
4008 out:
4009 return ret;
4010 }
4011
wlcore_set_beacon_template(struct wl1271 * wl,struct ieee80211_vif * vif,bool is_ap)4012 static int wlcore_set_beacon_template(struct wl1271 *wl,
4013 struct ieee80211_vif *vif,
4014 bool is_ap)
4015 {
4016 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4017 struct ieee80211_hdr *hdr;
4018 u32 min_rate;
4019 int ret;
4020 int ieoffset = offsetof(struct ieee80211_mgmt, u.beacon.variable);
4021 struct sk_buff *beacon = ieee80211_beacon_get(wl->hw, vif);
4022 u16 tmpl_id;
4023
4024 if (!beacon) {
4025 ret = -EINVAL;
4026 goto out;
4027 }
4028
4029 wl1271_debug(DEBUG_MASTER, "beacon updated");
4030
4031 ret = wl1271_ssid_set(wlvif, beacon, ieoffset);
4032 if (ret < 0) {
4033 dev_kfree_skb(beacon);
4034 goto out;
4035 }
4036 min_rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
4037 tmpl_id = is_ap ? CMD_TEMPL_AP_BEACON :
4038 CMD_TEMPL_BEACON;
4039 ret = wl1271_cmd_template_set(wl, wlvif->role_id, tmpl_id,
4040 beacon->data,
4041 beacon->len, 0,
4042 min_rate);
4043 if (ret < 0) {
4044 dev_kfree_skb(beacon);
4045 goto out;
4046 }
4047
4048 wlvif->wmm_enabled =
4049 cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
4050 WLAN_OUI_TYPE_MICROSOFT_WMM,
4051 beacon->data + ieoffset,
4052 beacon->len - ieoffset);
4053
4054 /*
4055 * In case we already have a probe-resp beacon set explicitly
4056 * by usermode, don't use the beacon data.
4057 */
4058 if (test_bit(WLVIF_FLAG_AP_PROBE_RESP_SET, &wlvif->flags))
4059 goto end_bcn;
4060
4061 /* remove TIM ie from probe response */
4062 wl12xx_remove_ie(beacon, WLAN_EID_TIM, ieoffset);
4063
4064 /*
4065 * remove p2p ie from probe response.
4066 * the fw reponds to probe requests that don't include
4067 * the p2p ie. probe requests with p2p ie will be passed,
4068 * and will be responded by the supplicant (the spec
4069 * forbids including the p2p ie when responding to probe
4070 * requests that didn't include it).
4071 */
4072 wl12xx_remove_vendor_ie(beacon, WLAN_OUI_WFA,
4073 WLAN_OUI_TYPE_WFA_P2P, ieoffset);
4074
4075 hdr = (struct ieee80211_hdr *) beacon->data;
4076 hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4077 IEEE80211_STYPE_PROBE_RESP);
4078 if (is_ap)
4079 ret = wl1271_ap_set_probe_resp_tmpl_legacy(wl, vif,
4080 beacon->data,
4081 beacon->len,
4082 min_rate);
4083 else
4084 ret = wl1271_cmd_template_set(wl, wlvif->role_id,
4085 CMD_TEMPL_PROBE_RESPONSE,
4086 beacon->data,
4087 beacon->len, 0,
4088 min_rate);
4089 end_bcn:
4090 dev_kfree_skb(beacon);
4091 if (ret < 0)
4092 goto out;
4093
4094 out:
4095 return ret;
4096 }
4097
wl1271_bss_beacon_info_changed(struct wl1271 * wl,struct ieee80211_vif * vif,struct ieee80211_bss_conf * bss_conf,u32 changed)4098 static int wl1271_bss_beacon_info_changed(struct wl1271 *wl,
4099 struct ieee80211_vif *vif,
4100 struct ieee80211_bss_conf *bss_conf,
4101 u32 changed)
4102 {
4103 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4104 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
4105 int ret = 0;
4106
4107 if (changed & BSS_CHANGED_BEACON_INT) {
4108 wl1271_debug(DEBUG_MASTER, "beacon interval updated: %d",
4109 bss_conf->beacon_int);
4110
4111 wlvif->beacon_int = bss_conf->beacon_int;
4112 }
4113
4114 if ((changed & BSS_CHANGED_AP_PROBE_RESP) && is_ap) {
4115 u32 rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
4116
4117 wl1271_ap_set_probe_resp_tmpl(wl, rate, vif);
4118 }
4119
4120 if (changed & BSS_CHANGED_BEACON) {
4121 ret = wlcore_set_beacon_template(wl, vif, is_ap);
4122 if (ret < 0)
4123 goto out;
4124
4125 if (test_and_clear_bit(WLVIF_FLAG_BEACON_DISABLED,
4126 &wlvif->flags)) {
4127 ret = wlcore_hw_dfs_master_restart(wl, wlvif);
4128 if (ret < 0)
4129 goto out;
4130 }
4131 }
4132 out:
4133 if (ret != 0)
4134 wl1271_error("beacon info change failed: %d", ret);
4135 return ret;
4136 }
4137
4138 /* AP mode changes */
wl1271_bss_info_changed_ap(struct wl1271 * wl,struct ieee80211_vif * vif,struct ieee80211_bss_conf * bss_conf,u32 changed)4139 static void wl1271_bss_info_changed_ap(struct wl1271 *wl,
4140 struct ieee80211_vif *vif,
4141 struct ieee80211_bss_conf *bss_conf,
4142 u32 changed)
4143 {
4144 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4145 int ret = 0;
4146
4147 if (changed & BSS_CHANGED_BASIC_RATES) {
4148 u32 rates = bss_conf->basic_rates;
4149
4150 wlvif->basic_rate_set = wl1271_tx_enabled_rates_get(wl, rates,
4151 wlvif->band);
4152 wlvif->basic_rate = wl1271_tx_min_rate_get(wl,
4153 wlvif->basic_rate_set);
4154
4155 ret = wl1271_init_ap_rates(wl, wlvif);
4156 if (ret < 0) {
4157 wl1271_error("AP rate policy change failed %d", ret);
4158 goto out;
4159 }
4160
4161 ret = wl1271_ap_init_templates(wl, vif);
4162 if (ret < 0)
4163 goto out;
4164
4165 /* No need to set probe resp template for mesh */
4166 if (!ieee80211_vif_is_mesh(vif)) {
4167 ret = wl1271_ap_set_probe_resp_tmpl(wl,
4168 wlvif->basic_rate,
4169 vif);
4170 if (ret < 0)
4171 goto out;
4172 }
4173
4174 ret = wlcore_set_beacon_template(wl, vif, true);
4175 if (ret < 0)
4176 goto out;
4177 }
4178
4179 ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf, changed);
4180 if (ret < 0)
4181 goto out;
4182
4183 if (changed & BSS_CHANGED_BEACON_ENABLED) {
4184 if (bss_conf->enable_beacon) {
4185 if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
4186 ret = wl12xx_cmd_role_start_ap(wl, wlvif);
4187 if (ret < 0)
4188 goto out;
4189
4190 ret = wl1271_ap_init_hwenc(wl, wlvif);
4191 if (ret < 0)
4192 goto out;
4193
4194 set_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags);
4195 wl1271_debug(DEBUG_AP, "started AP");
4196 }
4197 } else {
4198 if (test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
4199 /*
4200 * AP might be in ROC in case we have just
4201 * sent auth reply. handle it.
4202 */
4203 if (test_bit(wlvif->role_id, wl->roc_map))
4204 wl12xx_croc(wl, wlvif->role_id);
4205
4206 ret = wl12xx_cmd_role_stop_ap(wl, wlvif);
4207 if (ret < 0)
4208 goto out;
4209
4210 clear_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags);
4211 clear_bit(WLVIF_FLAG_AP_PROBE_RESP_SET,
4212 &wlvif->flags);
4213 wl1271_debug(DEBUG_AP, "stopped AP");
4214 }
4215 }
4216 }
4217
4218 ret = wl1271_bss_erp_info_changed(wl, vif, bss_conf, changed);
4219 if (ret < 0)
4220 goto out;
4221
4222 /* Handle HT information change */
4223 if ((changed & BSS_CHANGED_HT) &&
4224 (bss_conf->chandef.width != NL80211_CHAN_WIDTH_20_NOHT)) {
4225 ret = wl1271_acx_set_ht_information(wl, wlvif,
4226 bss_conf->ht_operation_mode);
4227 if (ret < 0) {
4228 wl1271_warning("Set ht information failed %d", ret);
4229 goto out;
4230 }
4231 }
4232
4233 out:
4234 return;
4235 }
4236
wlcore_set_bssid(struct wl1271 * wl,struct wl12xx_vif * wlvif,struct ieee80211_bss_conf * bss_conf,u32 sta_rate_set)4237 static int wlcore_set_bssid(struct wl1271 *wl, struct wl12xx_vif *wlvif,
4238 struct ieee80211_bss_conf *bss_conf,
4239 u32 sta_rate_set)
4240 {
4241 u32 rates;
4242 int ret;
4243
4244 wl1271_debug(DEBUG_MAC80211,
4245 "changed_bssid: %pM, aid: %d, bcn_int: %d, brates: 0x%x sta_rate_set: 0x%x",
4246 bss_conf->bssid, bss_conf->aid,
4247 bss_conf->beacon_int,
4248 bss_conf->basic_rates, sta_rate_set);
4249
4250 wlvif->beacon_int = bss_conf->beacon_int;
4251 rates = bss_conf->basic_rates;
4252 wlvif->basic_rate_set =
4253 wl1271_tx_enabled_rates_get(wl, rates,
4254 wlvif->band);
4255 wlvif->basic_rate =
4256 wl1271_tx_min_rate_get(wl,
4257 wlvif->basic_rate_set);
4258
4259 if (sta_rate_set)
4260 wlvif->rate_set =
4261 wl1271_tx_enabled_rates_get(wl,
4262 sta_rate_set,
4263 wlvif->band);
4264
4265 /* we only support sched_scan while not connected */
4266 if (wl->sched_vif == wlvif)
4267 wl->ops->sched_scan_stop(wl, wlvif);
4268
4269 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
4270 if (ret < 0)
4271 return ret;
4272
4273 ret = wl12xx_cmd_build_null_data(wl, wlvif);
4274 if (ret < 0)
4275 return ret;
4276
4277 ret = wl1271_build_qos_null_data(wl, wl12xx_wlvif_to_vif(wlvif));
4278 if (ret < 0)
4279 return ret;
4280
4281 wlcore_set_ssid(wl, wlvif);
4282
4283 set_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
4284
4285 return 0;
4286 }
4287
wlcore_clear_bssid(struct wl1271 * wl,struct wl12xx_vif * wlvif)4288 static int wlcore_clear_bssid(struct wl1271 *wl, struct wl12xx_vif *wlvif)
4289 {
4290 int ret;
4291
4292 /* revert back to minimum rates for the current band */
4293 wl1271_set_band_rate(wl, wlvif);
4294 wlvif->basic_rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
4295
4296 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
4297 if (ret < 0)
4298 return ret;
4299
4300 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
4301 test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags)) {
4302 ret = wl12xx_cmd_role_stop_sta(wl, wlvif);
4303 if (ret < 0)
4304 return ret;
4305 }
4306
4307 clear_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
4308 return 0;
4309 }
4310 /* STA/IBSS mode changes */
wl1271_bss_info_changed_sta(struct wl1271 * wl,struct ieee80211_vif * vif,struct ieee80211_bss_conf * bss_conf,u32 changed)4311 static void wl1271_bss_info_changed_sta(struct wl1271 *wl,
4312 struct ieee80211_vif *vif,
4313 struct ieee80211_bss_conf *bss_conf,
4314 u32 changed)
4315 {
4316 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4317 bool do_join = false;
4318 bool is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
4319 bool ibss_joined = false;
4320 u32 sta_rate_set = 0;
4321 int ret;
4322 struct ieee80211_sta *sta;
4323 bool sta_exists = false;
4324 struct ieee80211_sta_ht_cap sta_ht_cap;
4325
4326 if (is_ibss) {
4327 ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf,
4328 changed);
4329 if (ret < 0)
4330 goto out;
4331 }
4332
4333 if (changed & BSS_CHANGED_IBSS) {
4334 if (bss_conf->ibss_joined) {
4335 set_bit(WLVIF_FLAG_IBSS_JOINED, &wlvif->flags);
4336 ibss_joined = true;
4337 } else {
4338 wlcore_unset_assoc(wl, wlvif);
4339 wl12xx_cmd_role_stop_sta(wl, wlvif);
4340 }
4341 }
4342
4343 if ((changed & BSS_CHANGED_BEACON_INT) && ibss_joined)
4344 do_join = true;
4345
4346 /* Need to update the SSID (for filtering etc) */
4347 if ((changed & BSS_CHANGED_BEACON) && ibss_joined)
4348 do_join = true;
4349
4350 if ((changed & BSS_CHANGED_BEACON_ENABLED) && ibss_joined) {
4351 wl1271_debug(DEBUG_ADHOC, "ad-hoc beaconing: %s",
4352 bss_conf->enable_beacon ? "enabled" : "disabled");
4353
4354 do_join = true;
4355 }
4356
4357 if (changed & BSS_CHANGED_IDLE && !is_ibss)
4358 wl1271_sta_handle_idle(wl, wlvif, bss_conf->idle);
4359
4360 if (changed & BSS_CHANGED_CQM) {
4361 bool enable = false;
4362 if (bss_conf->cqm_rssi_thold)
4363 enable = true;
4364 ret = wl1271_acx_rssi_snr_trigger(wl, wlvif, enable,
4365 bss_conf->cqm_rssi_thold,
4366 bss_conf->cqm_rssi_hyst);
4367 if (ret < 0)
4368 goto out;
4369 wlvif->rssi_thold = bss_conf->cqm_rssi_thold;
4370 }
4371
4372 if (changed & (BSS_CHANGED_BSSID | BSS_CHANGED_HT |
4373 BSS_CHANGED_ASSOC)) {
4374 rcu_read_lock();
4375 sta = ieee80211_find_sta(vif, bss_conf->bssid);
4376 if (sta) {
4377 u8 *rx_mask = sta->ht_cap.mcs.rx_mask;
4378
4379 /* save the supp_rates of the ap */
4380 sta_rate_set = sta->supp_rates[wlvif->band];
4381 if (sta->ht_cap.ht_supported)
4382 sta_rate_set |=
4383 (rx_mask[0] << HW_HT_RATES_OFFSET) |
4384 (rx_mask[1] << HW_MIMO_RATES_OFFSET);
4385 sta_ht_cap = sta->ht_cap;
4386 sta_exists = true;
4387 }
4388
4389 rcu_read_unlock();
4390 }
4391
4392 if (changed & BSS_CHANGED_BSSID) {
4393 if (!is_zero_ether_addr(bss_conf->bssid)) {
4394 ret = wlcore_set_bssid(wl, wlvif, bss_conf,
4395 sta_rate_set);
4396 if (ret < 0)
4397 goto out;
4398
4399 /* Need to update the BSSID (for filtering etc) */
4400 do_join = true;
4401 } else {
4402 ret = wlcore_clear_bssid(wl, wlvif);
4403 if (ret < 0)
4404 goto out;
4405 }
4406 }
4407
4408 if (changed & BSS_CHANGED_IBSS) {
4409 wl1271_debug(DEBUG_ADHOC, "ibss_joined: %d",
4410 bss_conf->ibss_joined);
4411
4412 if (bss_conf->ibss_joined) {
4413 u32 rates = bss_conf->basic_rates;
4414 wlvif->basic_rate_set =
4415 wl1271_tx_enabled_rates_get(wl, rates,
4416 wlvif->band);
4417 wlvif->basic_rate =
4418 wl1271_tx_min_rate_get(wl,
4419 wlvif->basic_rate_set);
4420
4421 /* by default, use 11b + OFDM rates */
4422 wlvif->rate_set = CONF_TX_IBSS_DEFAULT_RATES;
4423 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
4424 if (ret < 0)
4425 goto out;
4426 }
4427 }
4428
4429 if ((changed & BSS_CHANGED_BEACON_INFO) && bss_conf->dtim_period) {
4430 /* enable beacon filtering */
4431 ret = wl1271_acx_beacon_filter_opt(wl, wlvif, true);
4432 if (ret < 0)
4433 goto out;
4434 }
4435
4436 ret = wl1271_bss_erp_info_changed(wl, vif, bss_conf, changed);
4437 if (ret < 0)
4438 goto out;
4439
4440 if (do_join) {
4441 ret = wlcore_join(wl, wlvif);
4442 if (ret < 0) {
4443 wl1271_warning("cmd join failed %d", ret);
4444 goto out;
4445 }
4446 }
4447
4448 if (changed & BSS_CHANGED_ASSOC) {
4449 if (bss_conf->assoc) {
4450 ret = wlcore_set_assoc(wl, wlvif, bss_conf,
4451 sta_rate_set);
4452 if (ret < 0)
4453 goto out;
4454
4455 if (test_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags))
4456 wl12xx_set_authorized(wl, wlvif);
4457 } else {
4458 wlcore_unset_assoc(wl, wlvif);
4459 }
4460 }
4461
4462 if (changed & BSS_CHANGED_PS) {
4463 if ((bss_conf->ps) &&
4464 test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) &&
4465 !test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags)) {
4466 int ps_mode;
4467 char *ps_mode_str;
4468
4469 if (wl->conf.conn.forced_ps) {
4470 ps_mode = STATION_POWER_SAVE_MODE;
4471 ps_mode_str = "forced";
4472 } else {
4473 ps_mode = STATION_AUTO_PS_MODE;
4474 ps_mode_str = "auto";
4475 }
4476
4477 wl1271_debug(DEBUG_PSM, "%s ps enabled", ps_mode_str);
4478
4479 ret = wl1271_ps_set_mode(wl, wlvif, ps_mode);
4480 if (ret < 0)
4481 wl1271_warning("enter %s ps failed %d",
4482 ps_mode_str, ret);
4483 } else if (!bss_conf->ps &&
4484 test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags)) {
4485 wl1271_debug(DEBUG_PSM, "auto ps disabled");
4486
4487 ret = wl1271_ps_set_mode(wl, wlvif,
4488 STATION_ACTIVE_MODE);
4489 if (ret < 0)
4490 wl1271_warning("exit auto ps failed %d", ret);
4491 }
4492 }
4493
4494 /* Handle new association with HT. Do this after join. */
4495 if (sta_exists) {
4496 bool enabled =
4497 bss_conf->chandef.width != NL80211_CHAN_WIDTH_20_NOHT;
4498
4499 ret = wlcore_hw_set_peer_cap(wl,
4500 &sta_ht_cap,
4501 enabled,
4502 wlvif->rate_set,
4503 wlvif->sta.hlid);
4504 if (ret < 0) {
4505 wl1271_warning("Set ht cap failed %d", ret);
4506 goto out;
4507
4508 }
4509
4510 if (enabled) {
4511 ret = wl1271_acx_set_ht_information(wl, wlvif,
4512 bss_conf->ht_operation_mode);
4513 if (ret < 0) {
4514 wl1271_warning("Set ht information failed %d",
4515 ret);
4516 goto out;
4517 }
4518 }
4519 }
4520
4521 /* Handle arp filtering. Done after join. */
4522 if ((changed & BSS_CHANGED_ARP_FILTER) ||
4523 (!is_ibss && (changed & BSS_CHANGED_QOS))) {
4524 __be32 addr = bss_conf->arp_addr_list[0];
4525 wlvif->sta.qos = bss_conf->qos;
4526 WARN_ON(wlvif->bss_type != BSS_TYPE_STA_BSS);
4527
4528 if (bss_conf->arp_addr_cnt == 1 && bss_conf->assoc) {
4529 wlvif->ip_addr = addr;
4530 /*
4531 * The template should have been configured only upon
4532 * association. however, it seems that the correct ip
4533 * isn't being set (when sending), so we have to
4534 * reconfigure the template upon every ip change.
4535 */
4536 ret = wl1271_cmd_build_arp_rsp(wl, wlvif);
4537 if (ret < 0) {
4538 wl1271_warning("build arp rsp failed: %d", ret);
4539 goto out;
4540 }
4541
4542 ret = wl1271_acx_arp_ip_filter(wl, wlvif,
4543 (ACX_ARP_FILTER_ARP_FILTERING |
4544 ACX_ARP_FILTER_AUTO_ARP),
4545 addr);
4546 } else {
4547 wlvif->ip_addr = 0;
4548 ret = wl1271_acx_arp_ip_filter(wl, wlvif, 0, addr);
4549 }
4550
4551 if (ret < 0)
4552 goto out;
4553 }
4554
4555 out:
4556 return;
4557 }
4558
wl1271_op_bss_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * bss_conf,u32 changed)4559 static void wl1271_op_bss_info_changed(struct ieee80211_hw *hw,
4560 struct ieee80211_vif *vif,
4561 struct ieee80211_bss_conf *bss_conf,
4562 u32 changed)
4563 {
4564 struct wl1271 *wl = hw->priv;
4565 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4566 bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
4567 int ret;
4568
4569 wl1271_debug(DEBUG_MAC80211, "mac80211 bss info role %d changed 0x%x",
4570 wlvif->role_id, (int)changed);
4571
4572 /*
4573 * make sure to cancel pending disconnections if our association
4574 * state changed
4575 */
4576 if (!is_ap && (changed & BSS_CHANGED_ASSOC))
4577 cancel_delayed_work_sync(&wlvif->connection_loss_work);
4578
4579 if (is_ap && (changed & BSS_CHANGED_BEACON_ENABLED) &&
4580 !bss_conf->enable_beacon)
4581 wl1271_tx_flush(wl);
4582
4583 mutex_lock(&wl->mutex);
4584
4585 if (unlikely(wl->state != WLCORE_STATE_ON))
4586 goto out;
4587
4588 if (unlikely(!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)))
4589 goto out;
4590
4591 ret = wl1271_ps_elp_wakeup(wl);
4592 if (ret < 0)
4593 goto out;
4594
4595 if ((changed & BSS_CHANGED_TXPOWER) &&
4596 bss_conf->txpower != wlvif->power_level) {
4597
4598 ret = wl1271_acx_tx_power(wl, wlvif, bss_conf->txpower);
4599 if (ret < 0)
4600 goto out;
4601
4602 wlvif->power_level = bss_conf->txpower;
4603 }
4604
4605 if (is_ap)
4606 wl1271_bss_info_changed_ap(wl, vif, bss_conf, changed);
4607 else
4608 wl1271_bss_info_changed_sta(wl, vif, bss_conf, changed);
4609
4610 wl1271_ps_elp_sleep(wl);
4611
4612 out:
4613 mutex_unlock(&wl->mutex);
4614 }
4615
wlcore_op_add_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)4616 static int wlcore_op_add_chanctx(struct ieee80211_hw *hw,
4617 struct ieee80211_chanctx_conf *ctx)
4618 {
4619 wl1271_debug(DEBUG_MAC80211, "mac80211 add chanctx %d (type %d)",
4620 ieee80211_frequency_to_channel(ctx->def.chan->center_freq),
4621 cfg80211_get_chandef_type(&ctx->def));
4622 return 0;
4623 }
4624
wlcore_op_remove_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)4625 static void wlcore_op_remove_chanctx(struct ieee80211_hw *hw,
4626 struct ieee80211_chanctx_conf *ctx)
4627 {
4628 wl1271_debug(DEBUG_MAC80211, "mac80211 remove chanctx %d (type %d)",
4629 ieee80211_frequency_to_channel(ctx->def.chan->center_freq),
4630 cfg80211_get_chandef_type(&ctx->def));
4631 }
4632
wlcore_op_change_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx,u32 changed)4633 static void wlcore_op_change_chanctx(struct ieee80211_hw *hw,
4634 struct ieee80211_chanctx_conf *ctx,
4635 u32 changed)
4636 {
4637 struct wl1271 *wl = hw->priv;
4638 struct wl12xx_vif *wlvif;
4639 int ret;
4640 int channel = ieee80211_frequency_to_channel(
4641 ctx->def.chan->center_freq);
4642
4643 wl1271_debug(DEBUG_MAC80211,
4644 "mac80211 change chanctx %d (type %d) changed 0x%x",
4645 channel, cfg80211_get_chandef_type(&ctx->def), changed);
4646
4647 mutex_lock(&wl->mutex);
4648
4649 ret = wl1271_ps_elp_wakeup(wl);
4650 if (ret < 0)
4651 goto out;
4652
4653 wl12xx_for_each_wlvif(wl, wlvif) {
4654 struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
4655
4656 rcu_read_lock();
4657 if (rcu_access_pointer(vif->chanctx_conf) != ctx) {
4658 rcu_read_unlock();
4659 continue;
4660 }
4661 rcu_read_unlock();
4662
4663 /* start radar if needed */
4664 if (changed & IEEE80211_CHANCTX_CHANGE_RADAR &&
4665 wlvif->bss_type == BSS_TYPE_AP_BSS &&
4666 ctx->radar_enabled && !wlvif->radar_enabled &&
4667 ctx->def.chan->dfs_state == NL80211_DFS_USABLE) {
4668 wl1271_debug(DEBUG_MAC80211, "Start radar detection");
4669 wlcore_hw_set_cac(wl, wlvif, true);
4670 wlvif->radar_enabled = true;
4671 }
4672 }
4673
4674 wl1271_ps_elp_sleep(wl);
4675 out:
4676 mutex_unlock(&wl->mutex);
4677 }
4678
wlcore_op_assign_vif_chanctx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_chanctx_conf * ctx)4679 static int wlcore_op_assign_vif_chanctx(struct ieee80211_hw *hw,
4680 struct ieee80211_vif *vif,
4681 struct ieee80211_chanctx_conf *ctx)
4682 {
4683 struct wl1271 *wl = hw->priv;
4684 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4685 int channel = ieee80211_frequency_to_channel(
4686 ctx->def.chan->center_freq);
4687 int ret = -EINVAL;
4688
4689 wl1271_debug(DEBUG_MAC80211,
4690 "mac80211 assign chanctx (role %d) %d (type %d) (radar %d dfs_state %d)",
4691 wlvif->role_id, channel,
4692 cfg80211_get_chandef_type(&ctx->def),
4693 ctx->radar_enabled, ctx->def.chan->dfs_state);
4694
4695 mutex_lock(&wl->mutex);
4696
4697 if (unlikely(wl->state != WLCORE_STATE_ON))
4698 goto out;
4699
4700 if (unlikely(!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)))
4701 goto out;
4702
4703 ret = wl1271_ps_elp_wakeup(wl);
4704 if (ret < 0)
4705 goto out;
4706
4707 wlvif->band = ctx->def.chan->band;
4708 wlvif->channel = channel;
4709 wlvif->channel_type = cfg80211_get_chandef_type(&ctx->def);
4710
4711 /* update default rates according to the band */
4712 wl1271_set_band_rate(wl, wlvif);
4713
4714 if (ctx->radar_enabled &&
4715 ctx->def.chan->dfs_state == NL80211_DFS_USABLE) {
4716 wl1271_debug(DEBUG_MAC80211, "Start radar detection");
4717 wlcore_hw_set_cac(wl, wlvif, true);
4718 wlvif->radar_enabled = true;
4719 }
4720
4721 wl1271_ps_elp_sleep(wl);
4722 out:
4723 mutex_unlock(&wl->mutex);
4724
4725 return 0;
4726 }
4727
wlcore_op_unassign_vif_chanctx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_chanctx_conf * ctx)4728 static void wlcore_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
4729 struct ieee80211_vif *vif,
4730 struct ieee80211_chanctx_conf *ctx)
4731 {
4732 struct wl1271 *wl = hw->priv;
4733 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4734 int ret;
4735
4736 wl1271_debug(DEBUG_MAC80211,
4737 "mac80211 unassign chanctx (role %d) %d (type %d)",
4738 wlvif->role_id,
4739 ieee80211_frequency_to_channel(ctx->def.chan->center_freq),
4740 cfg80211_get_chandef_type(&ctx->def));
4741
4742 wl1271_tx_flush(wl);
4743
4744 mutex_lock(&wl->mutex);
4745
4746 if (unlikely(wl->state != WLCORE_STATE_ON))
4747 goto out;
4748
4749 if (unlikely(!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)))
4750 goto out;
4751
4752 ret = wl1271_ps_elp_wakeup(wl);
4753 if (ret < 0)
4754 goto out;
4755
4756 if (wlvif->radar_enabled) {
4757 wl1271_debug(DEBUG_MAC80211, "Stop radar detection");
4758 wlcore_hw_set_cac(wl, wlvif, false);
4759 wlvif->radar_enabled = false;
4760 }
4761
4762 wl1271_ps_elp_sleep(wl);
4763 out:
4764 mutex_unlock(&wl->mutex);
4765 }
4766
__wlcore_switch_vif_chan(struct wl1271 * wl,struct wl12xx_vif * wlvif,struct ieee80211_chanctx_conf * new_ctx)4767 static int __wlcore_switch_vif_chan(struct wl1271 *wl,
4768 struct wl12xx_vif *wlvif,
4769 struct ieee80211_chanctx_conf *new_ctx)
4770 {
4771 int channel = ieee80211_frequency_to_channel(
4772 new_ctx->def.chan->center_freq);
4773
4774 wl1271_debug(DEBUG_MAC80211,
4775 "switch vif (role %d) %d -> %d chan_type: %d",
4776 wlvif->role_id, wlvif->channel, channel,
4777 cfg80211_get_chandef_type(&new_ctx->def));
4778
4779 if (WARN_ON_ONCE(wlvif->bss_type != BSS_TYPE_AP_BSS))
4780 return 0;
4781
4782 WARN_ON(!test_bit(WLVIF_FLAG_BEACON_DISABLED, &wlvif->flags));
4783
4784 if (wlvif->radar_enabled) {
4785 wl1271_debug(DEBUG_MAC80211, "Stop radar detection");
4786 wlcore_hw_set_cac(wl, wlvif, false);
4787 wlvif->radar_enabled = false;
4788 }
4789
4790 wlvif->band = new_ctx->def.chan->band;
4791 wlvif->channel = channel;
4792 wlvif->channel_type = cfg80211_get_chandef_type(&new_ctx->def);
4793
4794 /* start radar if needed */
4795 if (new_ctx->radar_enabled) {
4796 wl1271_debug(DEBUG_MAC80211, "Start radar detection");
4797 wlcore_hw_set_cac(wl, wlvif, true);
4798 wlvif->radar_enabled = true;
4799 }
4800
4801 return 0;
4802 }
4803
4804 static int
wlcore_op_switch_vif_chanctx(struct ieee80211_hw * hw,struct ieee80211_vif_chanctx_switch * vifs,int n_vifs,enum ieee80211_chanctx_switch_mode mode)4805 wlcore_op_switch_vif_chanctx(struct ieee80211_hw *hw,
4806 struct ieee80211_vif_chanctx_switch *vifs,
4807 int n_vifs,
4808 enum ieee80211_chanctx_switch_mode mode)
4809 {
4810 struct wl1271 *wl = hw->priv;
4811 int i, ret;
4812
4813 wl1271_debug(DEBUG_MAC80211,
4814 "mac80211 switch chanctx n_vifs %d mode %d",
4815 n_vifs, mode);
4816
4817 mutex_lock(&wl->mutex);
4818
4819 ret = wl1271_ps_elp_wakeup(wl);
4820 if (ret < 0)
4821 goto out;
4822
4823 for (i = 0; i < n_vifs; i++) {
4824 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vifs[i].vif);
4825
4826 ret = __wlcore_switch_vif_chan(wl, wlvif, vifs[i].new_ctx);
4827 if (ret)
4828 goto out_sleep;
4829 }
4830 out_sleep:
4831 wl1271_ps_elp_sleep(wl);
4832 out:
4833 mutex_unlock(&wl->mutex);
4834
4835 return 0;
4836 }
4837
wl1271_op_conf_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u16 queue,const struct ieee80211_tx_queue_params * params)4838 static int wl1271_op_conf_tx(struct ieee80211_hw *hw,
4839 struct ieee80211_vif *vif, u16 queue,
4840 const struct ieee80211_tx_queue_params *params)
4841 {
4842 struct wl1271 *wl = hw->priv;
4843 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4844 u8 ps_scheme;
4845 int ret = 0;
4846
4847 if (wlcore_is_p2p_mgmt(wlvif))
4848 return 0;
4849
4850 mutex_lock(&wl->mutex);
4851
4852 wl1271_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
4853
4854 if (params->uapsd)
4855 ps_scheme = CONF_PS_SCHEME_UPSD_TRIGGER;
4856 else
4857 ps_scheme = CONF_PS_SCHEME_LEGACY;
4858
4859 if (!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
4860 goto out;
4861
4862 ret = wl1271_ps_elp_wakeup(wl);
4863 if (ret < 0)
4864 goto out;
4865
4866 /*
4867 * the txop is confed in units of 32us by the mac80211,
4868 * we need us
4869 */
4870 ret = wl1271_acx_ac_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
4871 params->cw_min, params->cw_max,
4872 params->aifs, params->txop << 5);
4873 if (ret < 0)
4874 goto out_sleep;
4875
4876 ret = wl1271_acx_tid_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
4877 CONF_CHANNEL_TYPE_EDCF,
4878 wl1271_tx_get_queue(queue),
4879 ps_scheme, CONF_ACK_POLICY_LEGACY,
4880 0, 0);
4881
4882 out_sleep:
4883 wl1271_ps_elp_sleep(wl);
4884
4885 out:
4886 mutex_unlock(&wl->mutex);
4887
4888 return ret;
4889 }
4890
wl1271_op_get_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif)4891 static u64 wl1271_op_get_tsf(struct ieee80211_hw *hw,
4892 struct ieee80211_vif *vif)
4893 {
4894
4895 struct wl1271 *wl = hw->priv;
4896 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4897 u64 mactime = ULLONG_MAX;
4898 int ret;
4899
4900 wl1271_debug(DEBUG_MAC80211, "mac80211 get tsf");
4901
4902 mutex_lock(&wl->mutex);
4903
4904 if (unlikely(wl->state != WLCORE_STATE_ON))
4905 goto out;
4906
4907 ret = wl1271_ps_elp_wakeup(wl);
4908 if (ret < 0)
4909 goto out;
4910
4911 ret = wl12xx_acx_tsf_info(wl, wlvif, &mactime);
4912 if (ret < 0)
4913 goto out_sleep;
4914
4915 out_sleep:
4916 wl1271_ps_elp_sleep(wl);
4917
4918 out:
4919 mutex_unlock(&wl->mutex);
4920 return mactime;
4921 }
4922
wl1271_op_get_survey(struct ieee80211_hw * hw,int idx,struct survey_info * survey)4923 static int wl1271_op_get_survey(struct ieee80211_hw *hw, int idx,
4924 struct survey_info *survey)
4925 {
4926 struct ieee80211_conf *conf = &hw->conf;
4927
4928 if (idx != 0)
4929 return -ENOENT;
4930
4931 survey->channel = conf->chandef.chan;
4932 survey->filled = 0;
4933 return 0;
4934 }
4935
wl1271_allocate_sta(struct wl1271 * wl,struct wl12xx_vif * wlvif,struct ieee80211_sta * sta)4936 static int wl1271_allocate_sta(struct wl1271 *wl,
4937 struct wl12xx_vif *wlvif,
4938 struct ieee80211_sta *sta)
4939 {
4940 struct wl1271_station *wl_sta;
4941 int ret;
4942
4943
4944 if (wl->active_sta_count >= wl->max_ap_stations) {
4945 wl1271_warning("could not allocate HLID - too much stations");
4946 return -EBUSY;
4947 }
4948
4949 wl_sta = (struct wl1271_station *)sta->drv_priv;
4950 ret = wl12xx_allocate_link(wl, wlvif, &wl_sta->hlid);
4951 if (ret < 0) {
4952 wl1271_warning("could not allocate HLID - too many links");
4953 return -EBUSY;
4954 }
4955
4956 /* use the previous security seq, if this is a recovery/resume */
4957 wl->links[wl_sta->hlid].total_freed_pkts = wl_sta->total_freed_pkts;
4958
4959 set_bit(wl_sta->hlid, wlvif->ap.sta_hlid_map);
4960 memcpy(wl->links[wl_sta->hlid].addr, sta->addr, ETH_ALEN);
4961 wl->active_sta_count++;
4962 return 0;
4963 }
4964
wl1271_free_sta(struct wl1271 * wl,struct wl12xx_vif * wlvif,u8 hlid)4965 void wl1271_free_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 hlid)
4966 {
4967 if (!test_bit(hlid, wlvif->ap.sta_hlid_map))
4968 return;
4969
4970 clear_bit(hlid, wlvif->ap.sta_hlid_map);
4971 __clear_bit(hlid, &wl->ap_ps_map);
4972 __clear_bit(hlid, &wl->ap_fw_ps_map);
4973
4974 /*
4975 * save the last used PN in the private part of iee80211_sta,
4976 * in case of recovery/suspend
4977 */
4978 wlcore_save_freed_pkts_addr(wl, wlvif, hlid, wl->links[hlid].addr);
4979
4980 wl12xx_free_link(wl, wlvif, &hlid);
4981 wl->active_sta_count--;
4982
4983 /*
4984 * rearm the tx watchdog when the last STA is freed - give the FW a
4985 * chance to return STA-buffered packets before complaining.
4986 */
4987 if (wl->active_sta_count == 0)
4988 wl12xx_rearm_tx_watchdog_locked(wl);
4989 }
4990
wl12xx_sta_add(struct wl1271 * wl,struct wl12xx_vif * wlvif,struct ieee80211_sta * sta)4991 static int wl12xx_sta_add(struct wl1271 *wl,
4992 struct wl12xx_vif *wlvif,
4993 struct ieee80211_sta *sta)
4994 {
4995 struct wl1271_station *wl_sta;
4996 int ret = 0;
4997 u8 hlid;
4998
4999 wl1271_debug(DEBUG_MAC80211, "mac80211 add sta %d", (int)sta->aid);
5000
5001 ret = wl1271_allocate_sta(wl, wlvif, sta);
5002 if (ret < 0)
5003 return ret;
5004
5005 wl_sta = (struct wl1271_station *)sta->drv_priv;
5006 hlid = wl_sta->hlid;
5007
5008 ret = wl12xx_cmd_add_peer(wl, wlvif, sta, hlid);
5009 if (ret < 0)
5010 wl1271_free_sta(wl, wlvif, hlid);
5011
5012 return ret;
5013 }
5014
wl12xx_sta_remove(struct wl1271 * wl,struct wl12xx_vif * wlvif,struct ieee80211_sta * sta)5015 static int wl12xx_sta_remove(struct wl1271 *wl,
5016 struct wl12xx_vif *wlvif,
5017 struct ieee80211_sta *sta)
5018 {
5019 struct wl1271_station *wl_sta;
5020 int ret = 0, id;
5021
5022 wl1271_debug(DEBUG_MAC80211, "mac80211 remove sta %d", (int)sta->aid);
5023
5024 wl_sta = (struct wl1271_station *)sta->drv_priv;
5025 id = wl_sta->hlid;
5026 if (WARN_ON(!test_bit(id, wlvif->ap.sta_hlid_map)))
5027 return -EINVAL;
5028
5029 ret = wl12xx_cmd_remove_peer(wl, wlvif, wl_sta->hlid);
5030 if (ret < 0)
5031 return ret;
5032
5033 wl1271_free_sta(wl, wlvif, wl_sta->hlid);
5034 return ret;
5035 }
5036
wlcore_roc_if_possible(struct wl1271 * wl,struct wl12xx_vif * wlvif)5037 static void wlcore_roc_if_possible(struct wl1271 *wl,
5038 struct wl12xx_vif *wlvif)
5039 {
5040 if (find_first_bit(wl->roc_map,
5041 WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES)
5042 return;
5043
5044 if (WARN_ON(wlvif->role_id == WL12XX_INVALID_ROLE_ID))
5045 return;
5046
5047 wl12xx_roc(wl, wlvif, wlvif->role_id, wlvif->band, wlvif->channel);
5048 }
5049
5050 /*
5051 * when wl_sta is NULL, we treat this call as if coming from a
5052 * pending auth reply.
5053 * wl->mutex must be taken and the FW must be awake when the call
5054 * takes place.
5055 */
wlcore_update_inconn_sta(struct wl1271 * wl,struct wl12xx_vif * wlvif,struct wl1271_station * wl_sta,bool in_conn)5056 void wlcore_update_inconn_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif,
5057 struct wl1271_station *wl_sta, bool in_conn)
5058 {
5059 if (in_conn) {
5060 if (WARN_ON(wl_sta && wl_sta->in_connection))
5061 return;
5062
5063 if (!wlvif->ap_pending_auth_reply &&
5064 !wlvif->inconn_count)
5065 wlcore_roc_if_possible(wl, wlvif);
5066
5067 if (wl_sta) {
5068 wl_sta->in_connection = true;
5069 wlvif->inconn_count++;
5070 } else {
5071 wlvif->ap_pending_auth_reply = true;
5072 }
5073 } else {
5074 if (wl_sta && !wl_sta->in_connection)
5075 return;
5076
5077 if (WARN_ON(!wl_sta && !wlvif->ap_pending_auth_reply))
5078 return;
5079
5080 if (WARN_ON(wl_sta && !wlvif->inconn_count))
5081 return;
5082
5083 if (wl_sta) {
5084 wl_sta->in_connection = false;
5085 wlvif->inconn_count--;
5086 } else {
5087 wlvif->ap_pending_auth_reply = false;
5088 }
5089
5090 if (!wlvif->inconn_count && !wlvif->ap_pending_auth_reply &&
5091 test_bit(wlvif->role_id, wl->roc_map))
5092 wl12xx_croc(wl, wlvif->role_id);
5093 }
5094 }
5095
wl12xx_update_sta_state(struct wl1271 * wl,struct wl12xx_vif * wlvif,struct ieee80211_sta * sta,enum ieee80211_sta_state old_state,enum ieee80211_sta_state new_state)5096 static int wl12xx_update_sta_state(struct wl1271 *wl,
5097 struct wl12xx_vif *wlvif,
5098 struct ieee80211_sta *sta,
5099 enum ieee80211_sta_state old_state,
5100 enum ieee80211_sta_state new_state)
5101 {
5102 struct wl1271_station *wl_sta;
5103 bool is_ap = wlvif->bss_type == BSS_TYPE_AP_BSS;
5104 bool is_sta = wlvif->bss_type == BSS_TYPE_STA_BSS;
5105 int ret;
5106
5107 wl_sta = (struct wl1271_station *)sta->drv_priv;
5108
5109 /* Add station (AP mode) */
5110 if (is_ap &&
5111 old_state == IEEE80211_STA_NOTEXIST &&
5112 new_state == IEEE80211_STA_NONE) {
5113 ret = wl12xx_sta_add(wl, wlvif, sta);
5114 if (ret)
5115 return ret;
5116
5117 wlcore_update_inconn_sta(wl, wlvif, wl_sta, true);
5118 }
5119
5120 /* Remove station (AP mode) */
5121 if (is_ap &&
5122 old_state == IEEE80211_STA_NONE &&
5123 new_state == IEEE80211_STA_NOTEXIST) {
5124 /* must not fail */
5125 wl12xx_sta_remove(wl, wlvif, sta);
5126
5127 wlcore_update_inconn_sta(wl, wlvif, wl_sta, false);
5128 }
5129
5130 /* Authorize station (AP mode) */
5131 if (is_ap &&
5132 new_state == IEEE80211_STA_AUTHORIZED) {
5133 ret = wl12xx_cmd_set_peer_state(wl, wlvif, wl_sta->hlid);
5134 if (ret < 0)
5135 return ret;
5136
5137 /* reconfigure rates */
5138 ret = wl12xx_cmd_add_peer(wl, wlvif, sta, wl_sta->hlid);
5139 if (ret < 0)
5140 return ret;
5141
5142 ret = wl1271_acx_set_ht_capabilities(wl, &sta->ht_cap, true,
5143 wl_sta->hlid);
5144 if (ret)
5145 return ret;
5146
5147 wlcore_update_inconn_sta(wl, wlvif, wl_sta, false);
5148 }
5149
5150 /* Authorize station */
5151 if (is_sta &&
5152 new_state == IEEE80211_STA_AUTHORIZED) {
5153 set_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags);
5154 ret = wl12xx_set_authorized(wl, wlvif);
5155 if (ret)
5156 return ret;
5157 }
5158
5159 if (is_sta &&
5160 old_state == IEEE80211_STA_AUTHORIZED &&
5161 new_state == IEEE80211_STA_ASSOC) {
5162 clear_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags);
5163 clear_bit(WLVIF_FLAG_STA_STATE_SENT, &wlvif->flags);
5164 }
5165
5166 /* save seq number on disassoc (suspend) */
5167 if (is_sta &&
5168 old_state == IEEE80211_STA_ASSOC &&
5169 new_state == IEEE80211_STA_AUTH) {
5170 wlcore_save_freed_pkts(wl, wlvif, wlvif->sta.hlid, sta);
5171 wlvif->total_freed_pkts = 0;
5172 }
5173
5174 /* restore seq number on assoc (resume) */
5175 if (is_sta &&
5176 old_state == IEEE80211_STA_AUTH &&
5177 new_state == IEEE80211_STA_ASSOC) {
5178 wlvif->total_freed_pkts = wl_sta->total_freed_pkts;
5179 }
5180
5181 /* clear ROCs on failure or authorization */
5182 if (is_sta &&
5183 (new_state == IEEE80211_STA_AUTHORIZED ||
5184 new_state == IEEE80211_STA_NOTEXIST)) {
5185 if (test_bit(wlvif->role_id, wl->roc_map))
5186 wl12xx_croc(wl, wlvif->role_id);
5187 }
5188
5189 if (is_sta &&
5190 old_state == IEEE80211_STA_NOTEXIST &&
5191 new_state == IEEE80211_STA_NONE) {
5192 if (find_first_bit(wl->roc_map,
5193 WL12XX_MAX_ROLES) >= WL12XX_MAX_ROLES) {
5194 WARN_ON(wlvif->role_id == WL12XX_INVALID_ROLE_ID);
5195 wl12xx_roc(wl, wlvif, wlvif->role_id,
5196 wlvif->band, wlvif->channel);
5197 }
5198 }
5199 return 0;
5200 }
5201
wl12xx_op_sta_state(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,enum ieee80211_sta_state old_state,enum ieee80211_sta_state new_state)5202 static int wl12xx_op_sta_state(struct ieee80211_hw *hw,
5203 struct ieee80211_vif *vif,
5204 struct ieee80211_sta *sta,
5205 enum ieee80211_sta_state old_state,
5206 enum ieee80211_sta_state new_state)
5207 {
5208 struct wl1271 *wl = hw->priv;
5209 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5210 int ret;
5211
5212 wl1271_debug(DEBUG_MAC80211, "mac80211 sta %d state=%d->%d",
5213 sta->aid, old_state, new_state);
5214
5215 mutex_lock(&wl->mutex);
5216
5217 if (unlikely(wl->state != WLCORE_STATE_ON)) {
5218 ret = -EBUSY;
5219 goto out;
5220 }
5221
5222 ret = wl1271_ps_elp_wakeup(wl);
5223 if (ret < 0)
5224 goto out;
5225
5226 ret = wl12xx_update_sta_state(wl, wlvif, sta, old_state, new_state);
5227
5228 wl1271_ps_elp_sleep(wl);
5229 out:
5230 mutex_unlock(&wl->mutex);
5231 if (new_state < old_state)
5232 return 0;
5233 return ret;
5234 }
5235
wl1271_op_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)5236 static int wl1271_op_ampdu_action(struct ieee80211_hw *hw,
5237 struct ieee80211_vif *vif,
5238 struct ieee80211_ampdu_params *params)
5239 {
5240 struct wl1271 *wl = hw->priv;
5241 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5242 int ret;
5243 u8 hlid, *ba_bitmap;
5244 struct ieee80211_sta *sta = params->sta;
5245 enum ieee80211_ampdu_mlme_action action = params->action;
5246 u16 tid = params->tid;
5247 u16 *ssn = ¶ms->ssn;
5248
5249 wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu action %d tid %d", action,
5250 tid);
5251
5252 /* sanity check - the fields in FW are only 8bits wide */
5253 if (WARN_ON(tid > 0xFF))
5254 return -ENOTSUPP;
5255
5256 mutex_lock(&wl->mutex);
5257
5258 if (unlikely(wl->state != WLCORE_STATE_ON)) {
5259 ret = -EAGAIN;
5260 goto out;
5261 }
5262
5263 if (wlvif->bss_type == BSS_TYPE_STA_BSS) {
5264 hlid = wlvif->sta.hlid;
5265 } else if (wlvif->bss_type == BSS_TYPE_AP_BSS) {
5266 struct wl1271_station *wl_sta;
5267
5268 wl_sta = (struct wl1271_station *)sta->drv_priv;
5269 hlid = wl_sta->hlid;
5270 } else {
5271 ret = -EINVAL;
5272 goto out;
5273 }
5274
5275 ba_bitmap = &wl->links[hlid].ba_bitmap;
5276
5277 ret = wl1271_ps_elp_wakeup(wl);
5278 if (ret < 0)
5279 goto out;
5280
5281 wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu: Rx tid %d action %d",
5282 tid, action);
5283
5284 switch (action) {
5285 case IEEE80211_AMPDU_RX_START:
5286 if (!wlvif->ba_support || !wlvif->ba_allowed) {
5287 ret = -ENOTSUPP;
5288 break;
5289 }
5290
5291 if (wl->ba_rx_session_count >= wl->ba_rx_session_count_max) {
5292 ret = -EBUSY;
5293 wl1271_error("exceeded max RX BA sessions");
5294 break;
5295 }
5296
5297 if (*ba_bitmap & BIT(tid)) {
5298 ret = -EINVAL;
5299 wl1271_error("cannot enable RX BA session on active "
5300 "tid: %d", tid);
5301 break;
5302 }
5303
5304 ret = wl12xx_acx_set_ba_receiver_session(wl, tid, *ssn, true,
5305 hlid,
5306 params->buf_size);
5307
5308 if (!ret) {
5309 *ba_bitmap |= BIT(tid);
5310 wl->ba_rx_session_count++;
5311 }
5312 break;
5313
5314 case IEEE80211_AMPDU_RX_STOP:
5315 if (!(*ba_bitmap & BIT(tid))) {
5316 /*
5317 * this happens on reconfig - so only output a debug
5318 * message for now, and don't fail the function.
5319 */
5320 wl1271_debug(DEBUG_MAC80211,
5321 "no active RX BA session on tid: %d",
5322 tid);
5323 ret = 0;
5324 break;
5325 }
5326
5327 ret = wl12xx_acx_set_ba_receiver_session(wl, tid, 0, false,
5328 hlid, 0);
5329 if (!ret) {
5330 *ba_bitmap &= ~BIT(tid);
5331 wl->ba_rx_session_count--;
5332 }
5333 break;
5334
5335 /*
5336 * The BA initiator session management in FW independently.
5337 * Falling break here on purpose for all TX APDU commands.
5338 */
5339 case IEEE80211_AMPDU_TX_START:
5340 case IEEE80211_AMPDU_TX_STOP_CONT:
5341 case IEEE80211_AMPDU_TX_STOP_FLUSH:
5342 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
5343 case IEEE80211_AMPDU_TX_OPERATIONAL:
5344 ret = -EINVAL;
5345 break;
5346
5347 default:
5348 wl1271_error("Incorrect ampdu action id=%x\n", action);
5349 ret = -EINVAL;
5350 }
5351
5352 wl1271_ps_elp_sleep(wl);
5353
5354 out:
5355 mutex_unlock(&wl->mutex);
5356
5357 return ret;
5358 }
5359
wl12xx_set_bitrate_mask(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const struct cfg80211_bitrate_mask * mask)5360 static int wl12xx_set_bitrate_mask(struct ieee80211_hw *hw,
5361 struct ieee80211_vif *vif,
5362 const struct cfg80211_bitrate_mask *mask)
5363 {
5364 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5365 struct wl1271 *wl = hw->priv;
5366 int i, ret = 0;
5367
5368 wl1271_debug(DEBUG_MAC80211, "mac80211 set_bitrate_mask 0x%x 0x%x",
5369 mask->control[NL80211_BAND_2GHZ].legacy,
5370 mask->control[NL80211_BAND_5GHZ].legacy);
5371
5372 mutex_lock(&wl->mutex);
5373
5374 for (i = 0; i < WLCORE_NUM_BANDS; i++)
5375 wlvif->bitrate_masks[i] =
5376 wl1271_tx_enabled_rates_get(wl,
5377 mask->control[i].legacy,
5378 i);
5379
5380 if (unlikely(wl->state != WLCORE_STATE_ON))
5381 goto out;
5382
5383 if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
5384 !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
5385
5386 ret = wl1271_ps_elp_wakeup(wl);
5387 if (ret < 0)
5388 goto out;
5389
5390 wl1271_set_band_rate(wl, wlvif);
5391 wlvif->basic_rate =
5392 wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
5393 ret = wl1271_acx_sta_rate_policies(wl, wlvif);
5394
5395 wl1271_ps_elp_sleep(wl);
5396 }
5397 out:
5398 mutex_unlock(&wl->mutex);
5399
5400 return ret;
5401 }
5402
wl12xx_op_channel_switch(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_channel_switch * ch_switch)5403 static void wl12xx_op_channel_switch(struct ieee80211_hw *hw,
5404 struct ieee80211_vif *vif,
5405 struct ieee80211_channel_switch *ch_switch)
5406 {
5407 struct wl1271 *wl = hw->priv;
5408 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5409 int ret;
5410
5411 wl1271_debug(DEBUG_MAC80211, "mac80211 channel switch");
5412
5413 wl1271_tx_flush(wl);
5414
5415 mutex_lock(&wl->mutex);
5416
5417 if (unlikely(wl->state == WLCORE_STATE_OFF)) {
5418 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
5419 ieee80211_chswitch_done(vif, false);
5420 goto out;
5421 } else if (unlikely(wl->state != WLCORE_STATE_ON)) {
5422 goto out;
5423 }
5424
5425 ret = wl1271_ps_elp_wakeup(wl);
5426 if (ret < 0)
5427 goto out;
5428
5429 /* TODO: change mac80211 to pass vif as param */
5430
5431 if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
5432 unsigned long delay_usec;
5433
5434 ret = wl->ops->channel_switch(wl, wlvif, ch_switch);
5435 if (ret)
5436 goto out_sleep;
5437
5438 set_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags);
5439
5440 /* indicate failure 5 seconds after channel switch time */
5441 delay_usec = ieee80211_tu_to_usec(wlvif->beacon_int) *
5442 ch_switch->count;
5443 ieee80211_queue_delayed_work(hw, &wlvif->channel_switch_work,
5444 usecs_to_jiffies(delay_usec) +
5445 msecs_to_jiffies(5000));
5446 }
5447
5448 out_sleep:
5449 wl1271_ps_elp_sleep(wl);
5450
5451 out:
5452 mutex_unlock(&wl->mutex);
5453 }
5454
wlcore_get_beacon_ie(struct wl1271 * wl,struct wl12xx_vif * wlvif,u8 eid)5455 static const void *wlcore_get_beacon_ie(struct wl1271 *wl,
5456 struct wl12xx_vif *wlvif,
5457 u8 eid)
5458 {
5459 int ieoffset = offsetof(struct ieee80211_mgmt, u.beacon.variable);
5460 struct sk_buff *beacon =
5461 ieee80211_beacon_get(wl->hw, wl12xx_wlvif_to_vif(wlvif));
5462
5463 if (!beacon)
5464 return NULL;
5465
5466 return cfg80211_find_ie(eid,
5467 beacon->data + ieoffset,
5468 beacon->len - ieoffset);
5469 }
5470
wlcore_get_csa_count(struct wl1271 * wl,struct wl12xx_vif * wlvif,u8 * csa_count)5471 static int wlcore_get_csa_count(struct wl1271 *wl, struct wl12xx_vif *wlvif,
5472 u8 *csa_count)
5473 {
5474 const u8 *ie;
5475 const struct ieee80211_channel_sw_ie *ie_csa;
5476
5477 ie = wlcore_get_beacon_ie(wl, wlvif, WLAN_EID_CHANNEL_SWITCH);
5478 if (!ie)
5479 return -EINVAL;
5480
5481 ie_csa = (struct ieee80211_channel_sw_ie *)&ie[2];
5482 *csa_count = ie_csa->count;
5483
5484 return 0;
5485 }
5486
wlcore_op_channel_switch_beacon(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_chan_def * chandef)5487 static void wlcore_op_channel_switch_beacon(struct ieee80211_hw *hw,
5488 struct ieee80211_vif *vif,
5489 struct cfg80211_chan_def *chandef)
5490 {
5491 struct wl1271 *wl = hw->priv;
5492 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5493 struct ieee80211_channel_switch ch_switch = {
5494 .block_tx = true,
5495 .chandef = *chandef,
5496 };
5497 int ret;
5498
5499 wl1271_debug(DEBUG_MAC80211,
5500 "mac80211 channel switch beacon (role %d)",
5501 wlvif->role_id);
5502
5503 ret = wlcore_get_csa_count(wl, wlvif, &ch_switch.count);
5504 if (ret < 0) {
5505 wl1271_error("error getting beacon (for CSA counter)");
5506 return;
5507 }
5508
5509 mutex_lock(&wl->mutex);
5510
5511 if (unlikely(wl->state != WLCORE_STATE_ON)) {
5512 ret = -EBUSY;
5513 goto out;
5514 }
5515
5516 ret = wl1271_ps_elp_wakeup(wl);
5517 if (ret < 0)
5518 goto out;
5519
5520 ret = wl->ops->channel_switch(wl, wlvif, &ch_switch);
5521 if (ret)
5522 goto out_sleep;
5523
5524 set_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags);
5525
5526 out_sleep:
5527 wl1271_ps_elp_sleep(wl);
5528 out:
5529 mutex_unlock(&wl->mutex);
5530 }
5531
wlcore_op_flush(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 queues,bool drop)5532 static void wlcore_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5533 u32 queues, bool drop)
5534 {
5535 struct wl1271 *wl = hw->priv;
5536
5537 wl1271_tx_flush(wl);
5538 }
5539
wlcore_op_remain_on_channel(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_channel * chan,int duration,enum ieee80211_roc_type type)5540 static int wlcore_op_remain_on_channel(struct ieee80211_hw *hw,
5541 struct ieee80211_vif *vif,
5542 struct ieee80211_channel *chan,
5543 int duration,
5544 enum ieee80211_roc_type type)
5545 {
5546 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5547 struct wl1271 *wl = hw->priv;
5548 int channel, active_roc, ret = 0;
5549
5550 channel = ieee80211_frequency_to_channel(chan->center_freq);
5551
5552 wl1271_debug(DEBUG_MAC80211, "mac80211 roc %d (%d)",
5553 channel, wlvif->role_id);
5554
5555 mutex_lock(&wl->mutex);
5556
5557 if (unlikely(wl->state != WLCORE_STATE_ON))
5558 goto out;
5559
5560 /* return EBUSY if we can't ROC right now */
5561 active_roc = find_first_bit(wl->roc_map, WL12XX_MAX_ROLES);
5562 if (wl->roc_vif || active_roc < WL12XX_MAX_ROLES) {
5563 wl1271_warning("active roc on role %d", active_roc);
5564 ret = -EBUSY;
5565 goto out;
5566 }
5567
5568 ret = wl1271_ps_elp_wakeup(wl);
5569 if (ret < 0)
5570 goto out;
5571
5572 ret = wl12xx_start_dev(wl, wlvif, chan->band, channel);
5573 if (ret < 0)
5574 goto out_sleep;
5575
5576 wl->roc_vif = vif;
5577 ieee80211_queue_delayed_work(hw, &wl->roc_complete_work,
5578 msecs_to_jiffies(duration));
5579 out_sleep:
5580 wl1271_ps_elp_sleep(wl);
5581 out:
5582 mutex_unlock(&wl->mutex);
5583 return ret;
5584 }
5585
__wlcore_roc_completed(struct wl1271 * wl)5586 static int __wlcore_roc_completed(struct wl1271 *wl)
5587 {
5588 struct wl12xx_vif *wlvif;
5589 int ret;
5590
5591 /* already completed */
5592 if (unlikely(!wl->roc_vif))
5593 return 0;
5594
5595 wlvif = wl12xx_vif_to_data(wl->roc_vif);
5596
5597 if (!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
5598 return -EBUSY;
5599
5600 ret = wl12xx_stop_dev(wl, wlvif);
5601 if (ret < 0)
5602 return ret;
5603
5604 wl->roc_vif = NULL;
5605
5606 return 0;
5607 }
5608
wlcore_roc_completed(struct wl1271 * wl)5609 static int wlcore_roc_completed(struct wl1271 *wl)
5610 {
5611 int ret;
5612
5613 wl1271_debug(DEBUG_MAC80211, "roc complete");
5614
5615 mutex_lock(&wl->mutex);
5616
5617 if (unlikely(wl->state != WLCORE_STATE_ON)) {
5618 ret = -EBUSY;
5619 goto out;
5620 }
5621
5622 ret = wl1271_ps_elp_wakeup(wl);
5623 if (ret < 0)
5624 goto out;
5625
5626 ret = __wlcore_roc_completed(wl);
5627
5628 wl1271_ps_elp_sleep(wl);
5629 out:
5630 mutex_unlock(&wl->mutex);
5631
5632 return ret;
5633 }
5634
wlcore_roc_complete_work(struct work_struct * work)5635 static void wlcore_roc_complete_work(struct work_struct *work)
5636 {
5637 struct delayed_work *dwork;
5638 struct wl1271 *wl;
5639 int ret;
5640
5641 dwork = to_delayed_work(work);
5642 wl = container_of(dwork, struct wl1271, roc_complete_work);
5643
5644 ret = wlcore_roc_completed(wl);
5645 if (!ret)
5646 ieee80211_remain_on_channel_expired(wl->hw);
5647 }
5648
wlcore_op_cancel_remain_on_channel(struct ieee80211_hw * hw)5649 static int wlcore_op_cancel_remain_on_channel(struct ieee80211_hw *hw)
5650 {
5651 struct wl1271 *wl = hw->priv;
5652
5653 wl1271_debug(DEBUG_MAC80211, "mac80211 croc");
5654
5655 /* TODO: per-vif */
5656 wl1271_tx_flush(wl);
5657
5658 /*
5659 * we can't just flush_work here, because it might deadlock
5660 * (as we might get called from the same workqueue)
5661 */
5662 cancel_delayed_work_sync(&wl->roc_complete_work);
5663 wlcore_roc_completed(wl);
5664
5665 return 0;
5666 }
5667
wlcore_op_sta_rc_update(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u32 changed)5668 static void wlcore_op_sta_rc_update(struct ieee80211_hw *hw,
5669 struct ieee80211_vif *vif,
5670 struct ieee80211_sta *sta,
5671 u32 changed)
5672 {
5673 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5674
5675 wl1271_debug(DEBUG_MAC80211, "mac80211 sta_rc_update");
5676
5677 if (!(changed & IEEE80211_RC_BW_CHANGED))
5678 return;
5679
5680 /* this callback is atomic, so schedule a new work */
5681 wlvif->rc_update_bw = sta->bandwidth;
5682 memcpy(&wlvif->rc_ht_cap, &sta->ht_cap, sizeof(sta->ht_cap));
5683 ieee80211_queue_work(hw, &wlvif->rc_update_work);
5684 }
5685
wlcore_op_sta_statistics(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct station_info * sinfo)5686 static void wlcore_op_sta_statistics(struct ieee80211_hw *hw,
5687 struct ieee80211_vif *vif,
5688 struct ieee80211_sta *sta,
5689 struct station_info *sinfo)
5690 {
5691 struct wl1271 *wl = hw->priv;
5692 struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5693 s8 rssi_dbm;
5694 int ret;
5695
5696 wl1271_debug(DEBUG_MAC80211, "mac80211 get_rssi");
5697
5698 mutex_lock(&wl->mutex);
5699
5700 if (unlikely(wl->state != WLCORE_STATE_ON))
5701 goto out;
5702
5703 ret = wl1271_ps_elp_wakeup(wl);
5704 if (ret < 0)
5705 goto out_sleep;
5706
5707 ret = wlcore_acx_average_rssi(wl, wlvif, &rssi_dbm);
5708 if (ret < 0)
5709 goto out_sleep;
5710
5711 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL);
5712 sinfo->signal = rssi_dbm;
5713
5714 out_sleep:
5715 wl1271_ps_elp_sleep(wl);
5716
5717 out:
5718 mutex_unlock(&wl->mutex);
5719 }
5720
wlcore_op_get_expected_throughput(struct ieee80211_hw * hw,struct ieee80211_sta * sta)5721 static u32 wlcore_op_get_expected_throughput(struct ieee80211_hw *hw,
5722 struct ieee80211_sta *sta)
5723 {
5724 struct wl1271_station *wl_sta = (struct wl1271_station *)sta->drv_priv;
5725 struct wl1271 *wl = hw->priv;
5726 u8 hlid = wl_sta->hlid;
5727
5728 /* return in units of Kbps */
5729 return (wl->links[hlid].fw_rate_mbps * 1000);
5730 }
5731
wl1271_tx_frames_pending(struct ieee80211_hw * hw)5732 static bool wl1271_tx_frames_pending(struct ieee80211_hw *hw)
5733 {
5734 struct wl1271 *wl = hw->priv;
5735 bool ret = false;
5736
5737 mutex_lock(&wl->mutex);
5738
5739 if (unlikely(wl->state != WLCORE_STATE_ON))
5740 goto out;
5741
5742 /* packets are considered pending if in the TX queue or the FW */
5743 ret = (wl1271_tx_total_queue_count(wl) > 0) || (wl->tx_frames_cnt > 0);
5744 out:
5745 mutex_unlock(&wl->mutex);
5746
5747 return ret;
5748 }
5749
5750 /* can't be const, mac80211 writes to this */
5751 static struct ieee80211_rate wl1271_rates[] = {
5752 { .bitrate = 10,
5753 .hw_value = CONF_HW_BIT_RATE_1MBPS,
5754 .hw_value_short = CONF_HW_BIT_RATE_1MBPS, },
5755 { .bitrate = 20,
5756 .hw_value = CONF_HW_BIT_RATE_2MBPS,
5757 .hw_value_short = CONF_HW_BIT_RATE_2MBPS,
5758 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
5759 { .bitrate = 55,
5760 .hw_value = CONF_HW_BIT_RATE_5_5MBPS,
5761 .hw_value_short = CONF_HW_BIT_RATE_5_5MBPS,
5762 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
5763 { .bitrate = 110,
5764 .hw_value = CONF_HW_BIT_RATE_11MBPS,
5765 .hw_value_short = CONF_HW_BIT_RATE_11MBPS,
5766 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
5767 { .bitrate = 60,
5768 .hw_value = CONF_HW_BIT_RATE_6MBPS,
5769 .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
5770 { .bitrate = 90,
5771 .hw_value = CONF_HW_BIT_RATE_9MBPS,
5772 .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
5773 { .bitrate = 120,
5774 .hw_value = CONF_HW_BIT_RATE_12MBPS,
5775 .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
5776 { .bitrate = 180,
5777 .hw_value = CONF_HW_BIT_RATE_18MBPS,
5778 .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
5779 { .bitrate = 240,
5780 .hw_value = CONF_HW_BIT_RATE_24MBPS,
5781 .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
5782 { .bitrate = 360,
5783 .hw_value = CONF_HW_BIT_RATE_36MBPS,
5784 .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
5785 { .bitrate = 480,
5786 .hw_value = CONF_HW_BIT_RATE_48MBPS,
5787 .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
5788 { .bitrate = 540,
5789 .hw_value = CONF_HW_BIT_RATE_54MBPS,
5790 .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
5791 };
5792
5793 /* can't be const, mac80211 writes to this */
5794 static struct ieee80211_channel wl1271_channels[] = {
5795 { .hw_value = 1, .center_freq = 2412, .max_power = WLCORE_MAX_TXPWR },
5796 { .hw_value = 2, .center_freq = 2417, .max_power = WLCORE_MAX_TXPWR },
5797 { .hw_value = 3, .center_freq = 2422, .max_power = WLCORE_MAX_TXPWR },
5798 { .hw_value = 4, .center_freq = 2427, .max_power = WLCORE_MAX_TXPWR },
5799 { .hw_value = 5, .center_freq = 2432, .max_power = WLCORE_MAX_TXPWR },
5800 { .hw_value = 6, .center_freq = 2437, .max_power = WLCORE_MAX_TXPWR },
5801 { .hw_value = 7, .center_freq = 2442, .max_power = WLCORE_MAX_TXPWR },
5802 { .hw_value = 8, .center_freq = 2447, .max_power = WLCORE_MAX_TXPWR },
5803 { .hw_value = 9, .center_freq = 2452, .max_power = WLCORE_MAX_TXPWR },
5804 { .hw_value = 10, .center_freq = 2457, .max_power = WLCORE_MAX_TXPWR },
5805 { .hw_value = 11, .center_freq = 2462, .max_power = WLCORE_MAX_TXPWR },
5806 { .hw_value = 12, .center_freq = 2467, .max_power = WLCORE_MAX_TXPWR },
5807 { .hw_value = 13, .center_freq = 2472, .max_power = WLCORE_MAX_TXPWR },
5808 { .hw_value = 14, .center_freq = 2484, .max_power = WLCORE_MAX_TXPWR },
5809 };
5810
5811 /* can't be const, mac80211 writes to this */
5812 static struct ieee80211_supported_band wl1271_band_2ghz = {
5813 .channels = wl1271_channels,
5814 .n_channels = ARRAY_SIZE(wl1271_channels),
5815 .bitrates = wl1271_rates,
5816 .n_bitrates = ARRAY_SIZE(wl1271_rates),
5817 };
5818
5819 /* 5 GHz data rates for WL1273 */
5820 static struct ieee80211_rate wl1271_rates_5ghz[] = {
5821 { .bitrate = 60,
5822 .hw_value = CONF_HW_BIT_RATE_6MBPS,
5823 .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
5824 { .bitrate = 90,
5825 .hw_value = CONF_HW_BIT_RATE_9MBPS,
5826 .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
5827 { .bitrate = 120,
5828 .hw_value = CONF_HW_BIT_RATE_12MBPS,
5829 .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
5830 { .bitrate = 180,
5831 .hw_value = CONF_HW_BIT_RATE_18MBPS,
5832 .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
5833 { .bitrate = 240,
5834 .hw_value = CONF_HW_BIT_RATE_24MBPS,
5835 .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
5836 { .bitrate = 360,
5837 .hw_value = CONF_HW_BIT_RATE_36MBPS,
5838 .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
5839 { .bitrate = 480,
5840 .hw_value = CONF_HW_BIT_RATE_48MBPS,
5841 .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
5842 { .bitrate = 540,
5843 .hw_value = CONF_HW_BIT_RATE_54MBPS,
5844 .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
5845 };
5846
5847 /* 5 GHz band channels for WL1273 */
5848 static struct ieee80211_channel wl1271_channels_5ghz[] = {
5849 { .hw_value = 8, .center_freq = 5040, .max_power = WLCORE_MAX_TXPWR },
5850 { .hw_value = 12, .center_freq = 5060, .max_power = WLCORE_MAX_TXPWR },
5851 { .hw_value = 16, .center_freq = 5080, .max_power = WLCORE_MAX_TXPWR },
5852 { .hw_value = 34, .center_freq = 5170, .max_power = WLCORE_MAX_TXPWR },
5853 { .hw_value = 36, .center_freq = 5180, .max_power = WLCORE_MAX_TXPWR },
5854 { .hw_value = 38, .center_freq = 5190, .max_power = WLCORE_MAX_TXPWR },
5855 { .hw_value = 40, .center_freq = 5200, .max_power = WLCORE_MAX_TXPWR },
5856 { .hw_value = 42, .center_freq = 5210, .max_power = WLCORE_MAX_TXPWR },
5857 { .hw_value = 44, .center_freq = 5220, .max_power = WLCORE_MAX_TXPWR },
5858 { .hw_value = 46, .center_freq = 5230, .max_power = WLCORE_MAX_TXPWR },
5859 { .hw_value = 48, .center_freq = 5240, .max_power = WLCORE_MAX_TXPWR },
5860 { .hw_value = 52, .center_freq = 5260, .max_power = WLCORE_MAX_TXPWR },
5861 { .hw_value = 56, .center_freq = 5280, .max_power = WLCORE_MAX_TXPWR },
5862 { .hw_value = 60, .center_freq = 5300, .max_power = WLCORE_MAX_TXPWR },
5863 { .hw_value = 64, .center_freq = 5320, .max_power = WLCORE_MAX_TXPWR },
5864 { .hw_value = 100, .center_freq = 5500, .max_power = WLCORE_MAX_TXPWR },
5865 { .hw_value = 104, .center_freq = 5520, .max_power = WLCORE_MAX_TXPWR },
5866 { .hw_value = 108, .center_freq = 5540, .max_power = WLCORE_MAX_TXPWR },
5867 { .hw_value = 112, .center_freq = 5560, .max_power = WLCORE_MAX_TXPWR },
5868 { .hw_value = 116, .center_freq = 5580, .max_power = WLCORE_MAX_TXPWR },
5869 { .hw_value = 120, .center_freq = 5600, .max_power = WLCORE_MAX_TXPWR },
5870 { .hw_value = 124, .center_freq = 5620, .max_power = WLCORE_MAX_TXPWR },
5871 { .hw_value = 128, .center_freq = 5640, .max_power = WLCORE_MAX_TXPWR },
5872 { .hw_value = 132, .center_freq = 5660, .max_power = WLCORE_MAX_TXPWR },
5873 { .hw_value = 136, .center_freq = 5680, .max_power = WLCORE_MAX_TXPWR },
5874 { .hw_value = 140, .center_freq = 5700, .max_power = WLCORE_MAX_TXPWR },
5875 { .hw_value = 149, .center_freq = 5745, .max_power = WLCORE_MAX_TXPWR },
5876 { .hw_value = 153, .center_freq = 5765, .max_power = WLCORE_MAX_TXPWR },
5877 { .hw_value = 157, .center_freq = 5785, .max_power = WLCORE_MAX_TXPWR },
5878 { .hw_value = 161, .center_freq = 5805, .max_power = WLCORE_MAX_TXPWR },
5879 { .hw_value = 165, .center_freq = 5825, .max_power = WLCORE_MAX_TXPWR },
5880 };
5881
5882 static struct ieee80211_supported_band wl1271_band_5ghz = {
5883 .channels = wl1271_channels_5ghz,
5884 .n_channels = ARRAY_SIZE(wl1271_channels_5ghz),
5885 .bitrates = wl1271_rates_5ghz,
5886 .n_bitrates = ARRAY_SIZE(wl1271_rates_5ghz),
5887 };
5888
5889 static const struct ieee80211_ops wl1271_ops = {
5890 .start = wl1271_op_start,
5891 .stop = wlcore_op_stop,
5892 .add_interface = wl1271_op_add_interface,
5893 .remove_interface = wl1271_op_remove_interface,
5894 .change_interface = wl12xx_op_change_interface,
5895 #ifdef CONFIG_PM
5896 .suspend = wl1271_op_suspend,
5897 .resume = wl1271_op_resume,
5898 #endif
5899 .config = wl1271_op_config,
5900 .prepare_multicast = wl1271_op_prepare_multicast,
5901 .configure_filter = wl1271_op_configure_filter,
5902 .tx = wl1271_op_tx,
5903 .set_key = wlcore_op_set_key,
5904 .hw_scan = wl1271_op_hw_scan,
5905 .cancel_hw_scan = wl1271_op_cancel_hw_scan,
5906 .sched_scan_start = wl1271_op_sched_scan_start,
5907 .sched_scan_stop = wl1271_op_sched_scan_stop,
5908 .bss_info_changed = wl1271_op_bss_info_changed,
5909 .set_frag_threshold = wl1271_op_set_frag_threshold,
5910 .set_rts_threshold = wl1271_op_set_rts_threshold,
5911 .conf_tx = wl1271_op_conf_tx,
5912 .get_tsf = wl1271_op_get_tsf,
5913 .get_survey = wl1271_op_get_survey,
5914 .sta_state = wl12xx_op_sta_state,
5915 .ampdu_action = wl1271_op_ampdu_action,
5916 .tx_frames_pending = wl1271_tx_frames_pending,
5917 .set_bitrate_mask = wl12xx_set_bitrate_mask,
5918 .set_default_unicast_key = wl1271_op_set_default_key_idx,
5919 .channel_switch = wl12xx_op_channel_switch,
5920 .channel_switch_beacon = wlcore_op_channel_switch_beacon,
5921 .flush = wlcore_op_flush,
5922 .remain_on_channel = wlcore_op_remain_on_channel,
5923 .cancel_remain_on_channel = wlcore_op_cancel_remain_on_channel,
5924 .add_chanctx = wlcore_op_add_chanctx,
5925 .remove_chanctx = wlcore_op_remove_chanctx,
5926 .change_chanctx = wlcore_op_change_chanctx,
5927 .assign_vif_chanctx = wlcore_op_assign_vif_chanctx,
5928 .unassign_vif_chanctx = wlcore_op_unassign_vif_chanctx,
5929 .switch_vif_chanctx = wlcore_op_switch_vif_chanctx,
5930 .sta_rc_update = wlcore_op_sta_rc_update,
5931 .sta_statistics = wlcore_op_sta_statistics,
5932 .get_expected_throughput = wlcore_op_get_expected_throughput,
5933 CFG80211_TESTMODE_CMD(wl1271_tm_cmd)
5934 };
5935
5936
wlcore_rate_to_idx(struct wl1271 * wl,u8 rate,enum nl80211_band band)5937 u8 wlcore_rate_to_idx(struct wl1271 *wl, u8 rate, enum nl80211_band band)
5938 {
5939 u8 idx;
5940
5941 BUG_ON(band >= 2);
5942
5943 if (unlikely(rate >= wl->hw_tx_rate_tbl_size)) {
5944 wl1271_error("Illegal RX rate from HW: %d", rate);
5945 return 0;
5946 }
5947
5948 idx = wl->band_rate_to_idx[band][rate];
5949 if (unlikely(idx == CONF_HW_RXTX_RATE_UNSUPPORTED)) {
5950 wl1271_error("Unsupported RX rate from HW: %d", rate);
5951 return 0;
5952 }
5953
5954 return idx;
5955 }
5956
wl12xx_derive_mac_addresses(struct wl1271 * wl,u32 oui,u32 nic)5957 static void wl12xx_derive_mac_addresses(struct wl1271 *wl, u32 oui, u32 nic)
5958 {
5959 int i;
5960
5961 wl1271_debug(DEBUG_PROBE, "base address: oui %06x nic %06x",
5962 oui, nic);
5963
5964 if (nic + WLCORE_NUM_MAC_ADDRESSES - wl->num_mac_addr > 0xffffff)
5965 wl1271_warning("NIC part of the MAC address wraps around!");
5966
5967 for (i = 0; i < wl->num_mac_addr; i++) {
5968 wl->addresses[i].addr[0] = (u8)(oui >> 16);
5969 wl->addresses[i].addr[1] = (u8)(oui >> 8);
5970 wl->addresses[i].addr[2] = (u8) oui;
5971 wl->addresses[i].addr[3] = (u8)(nic >> 16);
5972 wl->addresses[i].addr[4] = (u8)(nic >> 8);
5973 wl->addresses[i].addr[5] = (u8) nic;
5974 nic++;
5975 }
5976
5977 /* we may be one address short at the most */
5978 WARN_ON(wl->num_mac_addr + 1 < WLCORE_NUM_MAC_ADDRESSES);
5979
5980 /*
5981 * turn on the LAA bit in the first address and use it as
5982 * the last address.
5983 */
5984 if (wl->num_mac_addr < WLCORE_NUM_MAC_ADDRESSES) {
5985 int idx = WLCORE_NUM_MAC_ADDRESSES - 1;
5986 memcpy(&wl->addresses[idx], &wl->addresses[0],
5987 sizeof(wl->addresses[0]));
5988 /* LAA bit */
5989 wl->addresses[idx].addr[0] |= BIT(1);
5990 }
5991
5992 wl->hw->wiphy->n_addresses = WLCORE_NUM_MAC_ADDRESSES;
5993 wl->hw->wiphy->addresses = wl->addresses;
5994 }
5995
wl12xx_get_hw_info(struct wl1271 * wl)5996 static int wl12xx_get_hw_info(struct wl1271 *wl)
5997 {
5998 int ret;
5999
6000 ret = wlcore_read_reg(wl, REG_CHIP_ID_B, &wl->chip.id);
6001 if (ret < 0)
6002 goto out;
6003
6004 wl->fuse_oui_addr = 0;
6005 wl->fuse_nic_addr = 0;
6006
6007 ret = wl->ops->get_pg_ver(wl, &wl->hw_pg_ver);
6008 if (ret < 0)
6009 goto out;
6010
6011 if (wl->ops->get_mac)
6012 ret = wl->ops->get_mac(wl);
6013
6014 out:
6015 return ret;
6016 }
6017
wl1271_register_hw(struct wl1271 * wl)6018 static int wl1271_register_hw(struct wl1271 *wl)
6019 {
6020 int ret;
6021 u32 oui_addr = 0, nic_addr = 0;
6022 struct platform_device *pdev = wl->pdev;
6023 struct wlcore_platdev_data *pdev_data = dev_get_platdata(&pdev->dev);
6024
6025 if (wl->mac80211_registered)
6026 return 0;
6027
6028 if (wl->nvs_len >= 12) {
6029 /* NOTE: The wl->nvs->nvs element must be first, in
6030 * order to simplify the casting, we assume it is at
6031 * the beginning of the wl->nvs structure.
6032 */
6033 u8 *nvs_ptr = (u8 *)wl->nvs;
6034
6035 oui_addr =
6036 (nvs_ptr[11] << 16) + (nvs_ptr[10] << 8) + nvs_ptr[6];
6037 nic_addr =
6038 (nvs_ptr[5] << 16) + (nvs_ptr[4] << 8) + nvs_ptr[3];
6039 }
6040
6041 /* if the MAC address is zeroed in the NVS derive from fuse */
6042 if (oui_addr == 0 && nic_addr == 0) {
6043 oui_addr = wl->fuse_oui_addr;
6044 /* fuse has the BD_ADDR, the WLAN addresses are the next two */
6045 nic_addr = wl->fuse_nic_addr + 1;
6046 }
6047
6048 if (oui_addr == 0xdeadbe && nic_addr == 0xef0000) {
6049 wl1271_warning("Detected unconfigured mac address in nvs, derive from fuse instead.\n");
6050 if (!strcmp(pdev_data->family->name, "wl18xx")) {
6051 wl1271_warning("This default nvs file can be removed from the file system\n");
6052 } else {
6053 wl1271_warning("Your device performance is not optimized.\n");
6054 wl1271_warning("Please use the calibrator tool to configure your device.\n");
6055 }
6056
6057 if (wl->fuse_oui_addr == 0 && wl->fuse_nic_addr == 0) {
6058 wl1271_warning("Fuse mac address is zero. using random mac\n");
6059 /* Use TI oui and a random nic */
6060 oui_addr = WLCORE_TI_OUI_ADDRESS;
6061 nic_addr = get_random_int();
6062 } else {
6063 oui_addr = wl->fuse_oui_addr;
6064 /* fuse has the BD_ADDR, the WLAN addresses are the next two */
6065 nic_addr = wl->fuse_nic_addr + 1;
6066 }
6067 }
6068
6069 wl12xx_derive_mac_addresses(wl, oui_addr, nic_addr);
6070
6071 ret = ieee80211_register_hw(wl->hw);
6072 if (ret < 0) {
6073 wl1271_error("unable to register mac80211 hw: %d", ret);
6074 goto out;
6075 }
6076
6077 wl->mac80211_registered = true;
6078
6079 wl1271_debugfs_init(wl);
6080
6081 wl1271_notice("loaded");
6082
6083 out:
6084 return ret;
6085 }
6086
wl1271_unregister_hw(struct wl1271 * wl)6087 static void wl1271_unregister_hw(struct wl1271 *wl)
6088 {
6089 if (wl->plt)
6090 wl1271_plt_stop(wl);
6091
6092 ieee80211_unregister_hw(wl->hw);
6093 wl->mac80211_registered = false;
6094
6095 }
6096
wl1271_init_ieee80211(struct wl1271 * wl)6097 static int wl1271_init_ieee80211(struct wl1271 *wl)
6098 {
6099 int i;
6100 static const u32 cipher_suites[] = {
6101 WLAN_CIPHER_SUITE_WEP40,
6102 WLAN_CIPHER_SUITE_WEP104,
6103 WLAN_CIPHER_SUITE_TKIP,
6104 WLAN_CIPHER_SUITE_CCMP,
6105 WL1271_CIPHER_SUITE_GEM,
6106 };
6107
6108 /* The tx descriptor buffer */
6109 wl->hw->extra_tx_headroom = sizeof(struct wl1271_tx_hw_descr);
6110
6111 if (wl->quirks & WLCORE_QUIRK_TKIP_HEADER_SPACE)
6112 wl->hw->extra_tx_headroom += WL1271_EXTRA_SPACE_TKIP;
6113
6114 /* unit us */
6115 /* FIXME: find a proper value */
6116 wl->hw->max_listen_interval = wl->conf.conn.max_listen_interval;
6117
6118 ieee80211_hw_set(wl->hw, SUPPORT_FAST_XMIT);
6119 ieee80211_hw_set(wl->hw, CHANCTX_STA_CSA);
6120 ieee80211_hw_set(wl->hw, QUEUE_CONTROL);
6121 ieee80211_hw_set(wl->hw, TX_AMPDU_SETUP_IN_HW);
6122 ieee80211_hw_set(wl->hw, AMPDU_AGGREGATION);
6123 ieee80211_hw_set(wl->hw, AP_LINK_PS);
6124 ieee80211_hw_set(wl->hw, SPECTRUM_MGMT);
6125 ieee80211_hw_set(wl->hw, REPORTS_TX_ACK_STATUS);
6126 ieee80211_hw_set(wl->hw, CONNECTION_MONITOR);
6127 ieee80211_hw_set(wl->hw, HAS_RATE_CONTROL);
6128 ieee80211_hw_set(wl->hw, SUPPORTS_DYNAMIC_PS);
6129 ieee80211_hw_set(wl->hw, SIGNAL_DBM);
6130 ieee80211_hw_set(wl->hw, SUPPORTS_PS);
6131 ieee80211_hw_set(wl->hw, SUPPORTS_TX_FRAG);
6132
6133 wl->hw->wiphy->cipher_suites = cipher_suites;
6134 wl->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
6135
6136 wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
6137 BIT(NL80211_IFTYPE_AP) |
6138 BIT(NL80211_IFTYPE_P2P_DEVICE) |
6139 BIT(NL80211_IFTYPE_P2P_CLIENT) |
6140 #ifdef CONFIG_MAC80211_MESH
6141 BIT(NL80211_IFTYPE_MESH_POINT) |
6142 #endif
6143 BIT(NL80211_IFTYPE_P2P_GO);
6144
6145 wl->hw->wiphy->max_scan_ssids = 1;
6146 wl->hw->wiphy->max_sched_scan_ssids = 16;
6147 wl->hw->wiphy->max_match_sets = 16;
6148 /*
6149 * Maximum length of elements in scanning probe request templates
6150 * should be the maximum length possible for a template, without
6151 * the IEEE80211 header of the template
6152 */
6153 wl->hw->wiphy->max_scan_ie_len = WL1271_CMD_TEMPL_MAX_SIZE -
6154 sizeof(struct ieee80211_header);
6155
6156 wl->hw->wiphy->max_sched_scan_reqs = 1;
6157 wl->hw->wiphy->max_sched_scan_ie_len = WL1271_CMD_TEMPL_MAX_SIZE -
6158 sizeof(struct ieee80211_header);
6159
6160 wl->hw->wiphy->max_remain_on_channel_duration = 30000;
6161
6162 wl->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD |
6163 WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
6164 WIPHY_FLAG_HAS_CHANNEL_SWITCH;
6165
6166 wl->hw->wiphy->features |= NL80211_FEATURE_AP_SCAN;
6167
6168 /* make sure all our channels fit in the scanned_ch bitmask */
6169 BUILD_BUG_ON(ARRAY_SIZE(wl1271_channels) +
6170 ARRAY_SIZE(wl1271_channels_5ghz) >
6171 WL1271_MAX_CHANNELS);
6172 /*
6173 * clear channel flags from the previous usage
6174 * and restore max_power & max_antenna_gain values.
6175 */
6176 for (i = 0; i < ARRAY_SIZE(wl1271_channels); i++) {
6177 wl1271_band_2ghz.channels[i].flags = 0;
6178 wl1271_band_2ghz.channels[i].max_power = WLCORE_MAX_TXPWR;
6179 wl1271_band_2ghz.channels[i].max_antenna_gain = 0;
6180 }
6181
6182 for (i = 0; i < ARRAY_SIZE(wl1271_channels_5ghz); i++) {
6183 wl1271_band_5ghz.channels[i].flags = 0;
6184 wl1271_band_5ghz.channels[i].max_power = WLCORE_MAX_TXPWR;
6185 wl1271_band_5ghz.channels[i].max_antenna_gain = 0;
6186 }
6187
6188 /*
6189 * We keep local copies of the band structs because we need to
6190 * modify them on a per-device basis.
6191 */
6192 memcpy(&wl->bands[NL80211_BAND_2GHZ], &wl1271_band_2ghz,
6193 sizeof(wl1271_band_2ghz));
6194 memcpy(&wl->bands[NL80211_BAND_2GHZ].ht_cap,
6195 &wl->ht_cap[NL80211_BAND_2GHZ],
6196 sizeof(*wl->ht_cap));
6197 memcpy(&wl->bands[NL80211_BAND_5GHZ], &wl1271_band_5ghz,
6198 sizeof(wl1271_band_5ghz));
6199 memcpy(&wl->bands[NL80211_BAND_5GHZ].ht_cap,
6200 &wl->ht_cap[NL80211_BAND_5GHZ],
6201 sizeof(*wl->ht_cap));
6202
6203 wl->hw->wiphy->bands[NL80211_BAND_2GHZ] =
6204 &wl->bands[NL80211_BAND_2GHZ];
6205 wl->hw->wiphy->bands[NL80211_BAND_5GHZ] =
6206 &wl->bands[NL80211_BAND_5GHZ];
6207
6208 /*
6209 * allow 4 queues per mac address we support +
6210 * 1 cab queue per mac + one global offchannel Tx queue
6211 */
6212 wl->hw->queues = (NUM_TX_QUEUES + 1) * WLCORE_NUM_MAC_ADDRESSES + 1;
6213
6214 /* the last queue is the offchannel queue */
6215 wl->hw->offchannel_tx_hw_queue = wl->hw->queues - 1;
6216 wl->hw->max_rates = 1;
6217
6218 wl->hw->wiphy->reg_notifier = wl1271_reg_notify;
6219
6220 /* the FW answers probe-requests in AP-mode */
6221 wl->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
6222 wl->hw->wiphy->probe_resp_offload =
6223 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
6224 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
6225 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
6226
6227 /* allowed interface combinations */
6228 wl->hw->wiphy->iface_combinations = wl->iface_combinations;
6229 wl->hw->wiphy->n_iface_combinations = wl->n_iface_combinations;
6230
6231 /* register vendor commands */
6232 wlcore_set_vendor_commands(wl->hw->wiphy);
6233
6234 SET_IEEE80211_DEV(wl->hw, wl->dev);
6235
6236 wl->hw->sta_data_size = sizeof(struct wl1271_station);
6237 wl->hw->vif_data_size = sizeof(struct wl12xx_vif);
6238
6239 wl->hw->max_rx_aggregation_subframes = wl->conf.ht.rx_ba_win_size;
6240
6241 return 0;
6242 }
6243
wlcore_alloc_hw(size_t priv_size,u32 aggr_buf_size,u32 mbox_size)6244 struct ieee80211_hw *wlcore_alloc_hw(size_t priv_size, u32 aggr_buf_size,
6245 u32 mbox_size)
6246 {
6247 struct ieee80211_hw *hw;
6248 struct wl1271 *wl;
6249 int i, j, ret;
6250 unsigned int order;
6251
6252 hw = ieee80211_alloc_hw(sizeof(*wl), &wl1271_ops);
6253 if (!hw) {
6254 wl1271_error("could not alloc ieee80211_hw");
6255 ret = -ENOMEM;
6256 goto err_hw_alloc;
6257 }
6258
6259 wl = hw->priv;
6260 memset(wl, 0, sizeof(*wl));
6261
6262 wl->priv = kzalloc(priv_size, GFP_KERNEL);
6263 if (!wl->priv) {
6264 wl1271_error("could not alloc wl priv");
6265 ret = -ENOMEM;
6266 goto err_priv_alloc;
6267 }
6268
6269 INIT_LIST_HEAD(&wl->wlvif_list);
6270
6271 wl->hw = hw;
6272
6273 /*
6274 * wl->num_links is not configured yet, so just use WLCORE_MAX_LINKS.
6275 * we don't allocate any additional resource here, so that's fine.
6276 */
6277 for (i = 0; i < NUM_TX_QUEUES; i++)
6278 for (j = 0; j < WLCORE_MAX_LINKS; j++)
6279 skb_queue_head_init(&wl->links[j].tx_queue[i]);
6280
6281 skb_queue_head_init(&wl->deferred_rx_queue);
6282 skb_queue_head_init(&wl->deferred_tx_queue);
6283
6284 INIT_DELAYED_WORK(&wl->elp_work, wl1271_elp_work);
6285 INIT_WORK(&wl->netstack_work, wl1271_netstack_work);
6286 INIT_WORK(&wl->tx_work, wl1271_tx_work);
6287 INIT_WORK(&wl->recovery_work, wl1271_recovery_work);
6288 INIT_DELAYED_WORK(&wl->scan_complete_work, wl1271_scan_complete_work);
6289 INIT_DELAYED_WORK(&wl->roc_complete_work, wlcore_roc_complete_work);
6290 INIT_DELAYED_WORK(&wl->tx_watchdog_work, wl12xx_tx_watchdog_work);
6291
6292 wl->freezable_wq = create_freezable_workqueue("wl12xx_wq");
6293 if (!wl->freezable_wq) {
6294 ret = -ENOMEM;
6295 goto err_hw;
6296 }
6297
6298 wl->channel = 0;
6299 wl->rx_counter = 0;
6300 wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
6301 wl->band = NL80211_BAND_2GHZ;
6302 wl->channel_type = NL80211_CHAN_NO_HT;
6303 wl->flags = 0;
6304 wl->sg_enabled = true;
6305 wl->sleep_auth = WL1271_PSM_ILLEGAL;
6306 wl->recovery_count = 0;
6307 wl->hw_pg_ver = -1;
6308 wl->ap_ps_map = 0;
6309 wl->ap_fw_ps_map = 0;
6310 wl->quirks = 0;
6311 wl->system_hlid = WL12XX_SYSTEM_HLID;
6312 wl->active_sta_count = 0;
6313 wl->active_link_count = 0;
6314 wl->fwlog_size = 0;
6315
6316 /* The system link is always allocated */
6317 __set_bit(WL12XX_SYSTEM_HLID, wl->links_map);
6318
6319 memset(wl->tx_frames_map, 0, sizeof(wl->tx_frames_map));
6320 for (i = 0; i < wl->num_tx_desc; i++)
6321 wl->tx_frames[i] = NULL;
6322
6323 spin_lock_init(&wl->wl_lock);
6324
6325 wl->state = WLCORE_STATE_OFF;
6326 wl->fw_type = WL12XX_FW_TYPE_NONE;
6327 mutex_init(&wl->mutex);
6328 mutex_init(&wl->flush_mutex);
6329 init_completion(&wl->nvs_loading_complete);
6330
6331 order = get_order(aggr_buf_size);
6332 wl->aggr_buf = (u8 *)__get_free_pages(GFP_KERNEL, order);
6333 if (!wl->aggr_buf) {
6334 ret = -ENOMEM;
6335 goto err_wq;
6336 }
6337 wl->aggr_buf_size = aggr_buf_size;
6338
6339 wl->dummy_packet = wl12xx_alloc_dummy_packet(wl);
6340 if (!wl->dummy_packet) {
6341 ret = -ENOMEM;
6342 goto err_aggr;
6343 }
6344
6345 /* Allocate one page for the FW log */
6346 wl->fwlog = (u8 *)get_zeroed_page(GFP_KERNEL);
6347 if (!wl->fwlog) {
6348 ret = -ENOMEM;
6349 goto err_dummy_packet;
6350 }
6351
6352 wl->mbox_size = mbox_size;
6353 wl->mbox = kmalloc(wl->mbox_size, GFP_KERNEL | GFP_DMA);
6354 if (!wl->mbox) {
6355 ret = -ENOMEM;
6356 goto err_fwlog;
6357 }
6358
6359 wl->buffer_32 = kmalloc(sizeof(*wl->buffer_32), GFP_KERNEL);
6360 if (!wl->buffer_32) {
6361 ret = -ENOMEM;
6362 goto err_mbox;
6363 }
6364
6365 return hw;
6366
6367 err_mbox:
6368 kfree(wl->mbox);
6369
6370 err_fwlog:
6371 free_page((unsigned long)wl->fwlog);
6372
6373 err_dummy_packet:
6374 dev_kfree_skb(wl->dummy_packet);
6375
6376 err_aggr:
6377 free_pages((unsigned long)wl->aggr_buf, order);
6378
6379 err_wq:
6380 destroy_workqueue(wl->freezable_wq);
6381
6382 err_hw:
6383 wl1271_debugfs_exit(wl);
6384 kfree(wl->priv);
6385
6386 err_priv_alloc:
6387 ieee80211_free_hw(hw);
6388
6389 err_hw_alloc:
6390
6391 return ERR_PTR(ret);
6392 }
6393 EXPORT_SYMBOL_GPL(wlcore_alloc_hw);
6394
wlcore_free_hw(struct wl1271 * wl)6395 int wlcore_free_hw(struct wl1271 *wl)
6396 {
6397 /* Unblock any fwlog readers */
6398 mutex_lock(&wl->mutex);
6399 wl->fwlog_size = -1;
6400 mutex_unlock(&wl->mutex);
6401
6402 wlcore_sysfs_free(wl);
6403
6404 kfree(wl->buffer_32);
6405 kfree(wl->mbox);
6406 free_page((unsigned long)wl->fwlog);
6407 dev_kfree_skb(wl->dummy_packet);
6408 free_pages((unsigned long)wl->aggr_buf, get_order(wl->aggr_buf_size));
6409
6410 wl1271_debugfs_exit(wl);
6411
6412 vfree(wl->fw);
6413 wl->fw = NULL;
6414 wl->fw_type = WL12XX_FW_TYPE_NONE;
6415 kfree(wl->nvs);
6416 wl->nvs = NULL;
6417
6418 kfree(wl->raw_fw_status);
6419 kfree(wl->fw_status);
6420 kfree(wl->tx_res_if);
6421 destroy_workqueue(wl->freezable_wq);
6422
6423 kfree(wl->priv);
6424 ieee80211_free_hw(wl->hw);
6425
6426 return 0;
6427 }
6428 EXPORT_SYMBOL_GPL(wlcore_free_hw);
6429
6430 #ifdef CONFIG_PM
6431 static const struct wiphy_wowlan_support wlcore_wowlan_support = {
6432 .flags = WIPHY_WOWLAN_ANY,
6433 .n_patterns = WL1271_MAX_RX_FILTERS,
6434 .pattern_min_len = 1,
6435 .pattern_max_len = WL1271_RX_FILTER_MAX_PATTERN_SIZE,
6436 };
6437 #endif
6438
wlcore_hardirq(int irq,void * cookie)6439 static irqreturn_t wlcore_hardirq(int irq, void *cookie)
6440 {
6441 return IRQ_WAKE_THREAD;
6442 }
6443
wlcore_nvs_cb(const struct firmware * fw,void * context)6444 static void wlcore_nvs_cb(const struct firmware *fw, void *context)
6445 {
6446 struct wl1271 *wl = context;
6447 struct platform_device *pdev = wl->pdev;
6448 struct wlcore_platdev_data *pdev_data = dev_get_platdata(&pdev->dev);
6449 struct resource *res;
6450
6451 int ret;
6452 irq_handler_t hardirq_fn = NULL;
6453
6454 if (fw) {
6455 wl->nvs = kmemdup(fw->data, fw->size, GFP_KERNEL);
6456 if (!wl->nvs) {
6457 wl1271_error("Could not allocate nvs data");
6458 goto out;
6459 }
6460 wl->nvs_len = fw->size;
6461 } else if (pdev_data->family->nvs_name) {
6462 wl1271_debug(DEBUG_BOOT, "Could not get nvs file %s",
6463 pdev_data->family->nvs_name);
6464 wl->nvs = NULL;
6465 wl->nvs_len = 0;
6466 } else {
6467 wl->nvs = NULL;
6468 wl->nvs_len = 0;
6469 }
6470
6471 ret = wl->ops->setup(wl);
6472 if (ret < 0)
6473 goto out_free_nvs;
6474
6475 BUG_ON(wl->num_tx_desc > WLCORE_MAX_TX_DESCRIPTORS);
6476
6477 /* adjust some runtime configuration parameters */
6478 wlcore_adjust_conf(wl);
6479
6480 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
6481 if (!res) {
6482 wl1271_error("Could not get IRQ resource");
6483 goto out_free_nvs;
6484 }
6485
6486 wl->irq = res->start;
6487 wl->irq_flags = res->flags & IRQF_TRIGGER_MASK;
6488 wl->if_ops = pdev_data->if_ops;
6489
6490 if (wl->irq_flags & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING))
6491 hardirq_fn = wlcore_hardirq;
6492 else
6493 wl->irq_flags |= IRQF_ONESHOT;
6494
6495 ret = wl12xx_set_power_on(wl);
6496 if (ret < 0)
6497 goto out_free_nvs;
6498
6499 ret = wl12xx_get_hw_info(wl);
6500 if (ret < 0) {
6501 wl1271_error("couldn't get hw info");
6502 wl1271_power_off(wl);
6503 goto out_free_nvs;
6504 }
6505
6506 ret = request_threaded_irq(wl->irq, hardirq_fn, wlcore_irq,
6507 wl->irq_flags, pdev->name, wl);
6508 if (ret < 0) {
6509 wl1271_error("interrupt configuration failed");
6510 wl1271_power_off(wl);
6511 goto out_free_nvs;
6512 }
6513
6514 #ifdef CONFIG_PM
6515 ret = enable_irq_wake(wl->irq);
6516 if (!ret) {
6517 wl->irq_wake_enabled = true;
6518 device_init_wakeup(wl->dev, 1);
6519 if (pdev_data->pwr_in_suspend)
6520 wl->hw->wiphy->wowlan = &wlcore_wowlan_support;
6521 }
6522 #endif
6523 disable_irq(wl->irq);
6524 wl1271_power_off(wl);
6525
6526 ret = wl->ops->identify_chip(wl);
6527 if (ret < 0)
6528 goto out_irq;
6529
6530 ret = wl1271_init_ieee80211(wl);
6531 if (ret)
6532 goto out_irq;
6533
6534 ret = wl1271_register_hw(wl);
6535 if (ret)
6536 goto out_irq;
6537
6538 ret = wlcore_sysfs_init(wl);
6539 if (ret)
6540 goto out_unreg;
6541
6542 wl->initialized = true;
6543 goto out;
6544
6545 out_unreg:
6546 wl1271_unregister_hw(wl);
6547
6548 out_irq:
6549 free_irq(wl->irq, wl);
6550
6551 out_free_nvs:
6552 kfree(wl->nvs);
6553
6554 out:
6555 release_firmware(fw);
6556 complete_all(&wl->nvs_loading_complete);
6557 }
6558
wlcore_probe(struct wl1271 * wl,struct platform_device * pdev)6559 int wlcore_probe(struct wl1271 *wl, struct platform_device *pdev)
6560 {
6561 struct wlcore_platdev_data *pdev_data = dev_get_platdata(&pdev->dev);
6562 const char *nvs_name;
6563 int ret = 0;
6564
6565 if (!wl->ops || !wl->ptable || !pdev_data)
6566 return -EINVAL;
6567
6568 wl->dev = &pdev->dev;
6569 wl->pdev = pdev;
6570 platform_set_drvdata(pdev, wl);
6571
6572 if (pdev_data->family && pdev_data->family->nvs_name) {
6573 nvs_name = pdev_data->family->nvs_name;
6574 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
6575 nvs_name, &pdev->dev, GFP_KERNEL,
6576 wl, wlcore_nvs_cb);
6577 if (ret < 0) {
6578 wl1271_error("request_firmware_nowait failed for %s: %d",
6579 nvs_name, ret);
6580 complete_all(&wl->nvs_loading_complete);
6581 }
6582 } else {
6583 wlcore_nvs_cb(NULL, wl);
6584 }
6585
6586 return ret;
6587 }
6588 EXPORT_SYMBOL_GPL(wlcore_probe);
6589
wlcore_remove(struct platform_device * pdev)6590 int wlcore_remove(struct platform_device *pdev)
6591 {
6592 struct wlcore_platdev_data *pdev_data = dev_get_platdata(&pdev->dev);
6593 struct wl1271 *wl = platform_get_drvdata(pdev);
6594
6595 if (pdev_data->family && pdev_data->family->nvs_name)
6596 wait_for_completion(&wl->nvs_loading_complete);
6597 if (!wl->initialized)
6598 return 0;
6599
6600 if (wl->irq_wake_enabled) {
6601 device_init_wakeup(wl->dev, 0);
6602 disable_irq_wake(wl->irq);
6603 }
6604 wl1271_unregister_hw(wl);
6605 free_irq(wl->irq, wl);
6606 wlcore_free_hw(wl);
6607
6608 return 0;
6609 }
6610 EXPORT_SYMBOL_GPL(wlcore_remove);
6611
6612 u32 wl12xx_debug_level = DEBUG_NONE;
6613 EXPORT_SYMBOL_GPL(wl12xx_debug_level);
6614 module_param_named(debug_level, wl12xx_debug_level, uint, S_IRUSR | S_IWUSR);
6615 MODULE_PARM_DESC(debug_level, "wl12xx debugging level");
6616
6617 module_param_named(fwlog, fwlog_param, charp, 0);
6618 MODULE_PARM_DESC(fwlog,
6619 "FW logger options: continuous, dbgpins or disable");
6620
6621 module_param(fwlog_mem_blocks, int, S_IRUSR | S_IWUSR);
6622 MODULE_PARM_DESC(fwlog_mem_blocks, "fwlog mem_blocks");
6623
6624 module_param(bug_on_recovery, int, S_IRUSR | S_IWUSR);
6625 MODULE_PARM_DESC(bug_on_recovery, "BUG() on fw recovery");
6626
6627 module_param(no_recovery, int, S_IRUSR | S_IWUSR);
6628 MODULE_PARM_DESC(no_recovery, "Prevent HW recovery. FW will remain stuck.");
6629
6630 MODULE_LICENSE("GPL");
6631 MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
6632 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
6633