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