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