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