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