xref: /linux/drivers/net/wireless/ti/wlcore/main.c (revision 4b99990cdf9560e8a071640baf19f312e6ae02f4)
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 		goto out;
3730 	}
3731 
3732 	pm_runtime_put_autosuspend(wl->dev);
3733 out:
3734 	mutex_unlock(&wl->mutex);
3735 }
3736 
3737 static int wl1271_op_hw_scan(struct ieee80211_hw *hw,
3738 			     struct ieee80211_vif *vif,
3739 			     struct ieee80211_scan_request *hw_req)
3740 {
3741 	struct cfg80211_scan_request *req = &hw_req->req;
3742 	struct wl1271 *wl = hw->priv;
3743 	int ret;
3744 	u8 *ssid = NULL;
3745 	size_t len = 0;
3746 
3747 	wl1271_debug(DEBUG_MAC80211, "mac80211 hw scan");
3748 
3749 	if (req->n_ssids) {
3750 		ssid = req->ssids[0].ssid;
3751 		len = req->ssids[0].ssid_len;
3752 	}
3753 
3754 	mutex_lock(&wl->mutex);
3755 
3756 	if (unlikely(wl->state != WLCORE_STATE_ON)) {
3757 		/*
3758 		 * We cannot return -EBUSY here because cfg80211 will expect
3759 		 * a call to ieee80211_scan_completed if we do - in this case
3760 		 * there won't be any call.
3761 		 */
3762 		ret = -EAGAIN;
3763 		goto out;
3764 	}
3765 
3766 	ret = pm_runtime_resume_and_get(wl->dev);
3767 	if (ret < 0)
3768 		goto out;
3769 
3770 	/* fail if there is any role in ROC */
3771 	if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES) {
3772 		/* don't allow scanning right now */
3773 		ret = -EBUSY;
3774 		goto out_sleep;
3775 	}
3776 
3777 	ret = wlcore_scan(hw->priv, vif, ssid, len, req);
3778 out_sleep:
3779 	pm_runtime_put_autosuspend(wl->dev);
3780 out:
3781 	mutex_unlock(&wl->mutex);
3782 
3783 	return ret;
3784 }
3785 
3786 static void wl1271_op_cancel_hw_scan(struct ieee80211_hw *hw,
3787 				     struct ieee80211_vif *vif)
3788 {
3789 	struct wl1271 *wl = hw->priv;
3790 	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3791 	struct cfg80211_scan_info info = {
3792 		.aborted = true,
3793 	};
3794 	int ret;
3795 
3796 	wl1271_debug(DEBUG_MAC80211, "mac80211 cancel hw scan");
3797 
3798 	mutex_lock(&wl->mutex);
3799 
3800 	if (unlikely(wl->state != WLCORE_STATE_ON))
3801 		goto out;
3802 
3803 	if (wl->scan.state == WL1271_SCAN_STATE_IDLE)
3804 		goto out;
3805 
3806 	ret = pm_runtime_resume_and_get(wl->dev);
3807 	if (ret < 0)
3808 		goto out;
3809 
3810 	if (wl->scan.state != WL1271_SCAN_STATE_DONE) {
3811 		ret = wl->ops->scan_stop(wl, wlvif);
3812 		if (ret < 0)
3813 			goto out_sleep;
3814 	}
3815 
3816 	/*
3817 	 * Rearm the tx watchdog just before idling scan. This
3818 	 * prevents just-finished scans from triggering the watchdog
3819 	 */
3820 	wl12xx_rearm_tx_watchdog_locked(wl);
3821 
3822 	wl->scan.state = WL1271_SCAN_STATE_IDLE;
3823 	memset(wl->scan.scanned_ch, 0, sizeof(wl->scan.scanned_ch));
3824 	wl->scan_wlvif = NULL;
3825 	wl->scan.req = NULL;
3826 	ieee80211_scan_completed(wl->hw, &info);
3827 
3828 out_sleep:
3829 	pm_runtime_put_autosuspend(wl->dev);
3830 out:
3831 	mutex_unlock(&wl->mutex);
3832 
3833 	cancel_delayed_work_sync(&wl->scan_complete_work);
3834 }
3835 
3836 static int wl1271_op_sched_scan_start(struct ieee80211_hw *hw,
3837 				      struct ieee80211_vif *vif,
3838 				      struct cfg80211_sched_scan_request *req,
3839 				      struct ieee80211_scan_ies *ies)
3840 {
3841 	struct wl1271 *wl = hw->priv;
3842 	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3843 	int ret;
3844 
3845 	wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_start");
3846 
3847 	mutex_lock(&wl->mutex);
3848 
3849 	if (unlikely(wl->state != WLCORE_STATE_ON)) {
3850 		ret = -EAGAIN;
3851 		goto out;
3852 	}
3853 
3854 	ret = pm_runtime_resume_and_get(wl->dev);
3855 	if (ret < 0)
3856 		goto out;
3857 
3858 	ret = wl->ops->sched_scan_start(wl, wlvif, req, ies);
3859 	if (ret < 0)
3860 		goto out_sleep;
3861 
3862 	wl->sched_vif = wlvif;
3863 
3864 out_sleep:
3865 	pm_runtime_put_autosuspend(wl->dev);
3866 out:
3867 	mutex_unlock(&wl->mutex);
3868 	return ret;
3869 }
3870 
3871 static int wl1271_op_sched_scan_stop(struct ieee80211_hw *hw,
3872 				     struct ieee80211_vif *vif)
3873 {
3874 	struct wl1271 *wl = hw->priv;
3875 	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3876 	int ret;
3877 
3878 	wl1271_debug(DEBUG_MAC80211, "wl1271_op_sched_scan_stop");
3879 
3880 	mutex_lock(&wl->mutex);
3881 
3882 	if (unlikely(wl->state != WLCORE_STATE_ON))
3883 		goto out;
3884 
3885 	ret = pm_runtime_resume_and_get(wl->dev);
3886 	if (ret < 0)
3887 		goto out;
3888 
3889 	wl->ops->sched_scan_stop(wl, wlvif);
3890 
3891 	pm_runtime_put_autosuspend(wl->dev);
3892 out:
3893 	mutex_unlock(&wl->mutex);
3894 
3895 	return 0;
3896 }
3897 
3898 static int wl1271_op_set_frag_threshold(struct ieee80211_hw *hw,
3899 					int radio_idx, u32 value)
3900 {
3901 	struct wl1271 *wl = hw->priv;
3902 	int ret = 0;
3903 
3904 	mutex_lock(&wl->mutex);
3905 
3906 	if (unlikely(wl->state != WLCORE_STATE_ON)) {
3907 		ret = -EAGAIN;
3908 		goto out;
3909 	}
3910 
3911 	ret = pm_runtime_resume_and_get(wl->dev);
3912 	if (ret < 0)
3913 		goto out;
3914 
3915 	ret = wl1271_acx_frag_threshold(wl, value);
3916 	if (ret < 0)
3917 		wl1271_warning("wl1271_op_set_frag_threshold failed: %d", ret);
3918 
3919 	pm_runtime_put_autosuspend(wl->dev);
3920 
3921 out:
3922 	mutex_unlock(&wl->mutex);
3923 
3924 	return ret;
3925 }
3926 
3927 static int wl1271_op_set_rts_threshold(struct ieee80211_hw *hw, int radio_idx,
3928 				       u32 value)
3929 {
3930 	struct wl1271 *wl = hw->priv;
3931 	struct wl12xx_vif *wlvif;
3932 	int ret = 0;
3933 
3934 	mutex_lock(&wl->mutex);
3935 
3936 	if (unlikely(wl->state != WLCORE_STATE_ON)) {
3937 		ret = -EAGAIN;
3938 		goto out;
3939 	}
3940 
3941 	ret = pm_runtime_resume_and_get(wl->dev);
3942 	if (ret < 0)
3943 		goto out;
3944 
3945 	wl12xx_for_each_wlvif(wl, wlvif) {
3946 		ret = wl1271_acx_rts_threshold(wl, wlvif, value);
3947 		if (ret < 0)
3948 			wl1271_warning("set rts threshold failed: %d", ret);
3949 	}
3950 	pm_runtime_put_autosuspend(wl->dev);
3951 
3952 out:
3953 	mutex_unlock(&wl->mutex);
3954 
3955 	return ret;
3956 }
3957 
3958 static void wl12xx_remove_ie(struct sk_buff *skb, u8 eid, int ieoffset)
3959 {
3960 	int len;
3961 	const u8 *next, *end = skb->data + skb->len;
3962 	u8 *ie = (u8 *)cfg80211_find_ie(eid, skb->data + ieoffset,
3963 					skb->len - ieoffset);
3964 	if (!ie)
3965 		return;
3966 	len = ie[1] + 2;
3967 	next = ie + len;
3968 	memmove(ie, next, end - next);
3969 	skb_trim(skb, skb->len - len);
3970 }
3971 
3972 static void wl12xx_remove_vendor_ie(struct sk_buff *skb,
3973 					    unsigned int oui, u8 oui_type,
3974 					    int ieoffset)
3975 {
3976 	int len;
3977 	const u8 *next, *end = skb->data + skb->len;
3978 	u8 *ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
3979 					       skb->data + ieoffset,
3980 					       skb->len - ieoffset);
3981 	if (!ie)
3982 		return;
3983 	len = ie[1] + 2;
3984 	next = ie + len;
3985 	memmove(ie, next, end - next);
3986 	skb_trim(skb, skb->len - len);
3987 }
3988 
3989 static int wl1271_ap_set_probe_resp_tmpl(struct wl1271 *wl, u32 rates,
3990 					 struct ieee80211_vif *vif)
3991 {
3992 	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
3993 	struct sk_buff *skb;
3994 	int ret;
3995 
3996 	skb = ieee80211_proberesp_get(wl->hw, vif);
3997 	if (!skb)
3998 		return -EOPNOTSUPP;
3999 
4000 	ret = wl1271_cmd_template_set(wl, wlvif->role_id,
4001 				      CMD_TEMPL_AP_PROBE_RESPONSE,
4002 				      skb->data,
4003 				      skb->len, 0,
4004 				      rates);
4005 	dev_kfree_skb(skb);
4006 
4007 	if (ret < 0)
4008 		goto out;
4009 
4010 	wl1271_debug(DEBUG_AP, "probe response updated");
4011 	set_bit(WLVIF_FLAG_AP_PROBE_RESP_SET, &wlvif->flags);
4012 
4013 out:
4014 	return ret;
4015 }
4016 
4017 static int wl1271_ap_set_probe_resp_tmpl_legacy(struct wl1271 *wl,
4018 					     struct ieee80211_vif *vif,
4019 					     u8 *probe_rsp_data,
4020 					     size_t probe_rsp_len,
4021 					     u32 rates)
4022 {
4023 	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4024 	u8 probe_rsp_templ[WL1271_CMD_TEMPL_MAX_SIZE];
4025 	int ssid_ie_offset, ie_offset, templ_len;
4026 	const u8 *ptr;
4027 
4028 	/* no need to change probe response if the SSID is set correctly */
4029 	if (wlvif->ssid_len > 0)
4030 		return wl1271_cmd_template_set(wl, wlvif->role_id,
4031 					       CMD_TEMPL_AP_PROBE_RESPONSE,
4032 					       probe_rsp_data,
4033 					       probe_rsp_len, 0,
4034 					       rates);
4035 
4036 	if (probe_rsp_len + vif->cfg.ssid_len > WL1271_CMD_TEMPL_MAX_SIZE) {
4037 		wl1271_error("probe_rsp template too big");
4038 		return -EINVAL;
4039 	}
4040 
4041 	/* start searching from IE offset */
4042 	ie_offset = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
4043 
4044 	ptr = cfg80211_find_ie(WLAN_EID_SSID, probe_rsp_data + ie_offset,
4045 			       probe_rsp_len - ie_offset);
4046 	if (!ptr) {
4047 		wl1271_error("No SSID in beacon!");
4048 		return -EINVAL;
4049 	}
4050 
4051 	ssid_ie_offset = ptr - probe_rsp_data;
4052 	ptr += (ptr[1] + 2);
4053 
4054 	memcpy(probe_rsp_templ, probe_rsp_data, ssid_ie_offset);
4055 
4056 	/* insert SSID from bss_conf */
4057 	probe_rsp_templ[ssid_ie_offset] = WLAN_EID_SSID;
4058 	probe_rsp_templ[ssid_ie_offset + 1] = vif->cfg.ssid_len;
4059 	memcpy(probe_rsp_templ + ssid_ie_offset + 2,
4060 	       vif->cfg.ssid, vif->cfg.ssid_len);
4061 	templ_len = ssid_ie_offset + 2 + vif->cfg.ssid_len;
4062 
4063 	memcpy(probe_rsp_templ + ssid_ie_offset + 2 + vif->cfg.ssid_len,
4064 	       ptr, probe_rsp_len - (ptr - probe_rsp_data));
4065 	templ_len += probe_rsp_len - (ptr - probe_rsp_data);
4066 
4067 	return wl1271_cmd_template_set(wl, wlvif->role_id,
4068 				       CMD_TEMPL_AP_PROBE_RESPONSE,
4069 				       probe_rsp_templ,
4070 				       templ_len, 0,
4071 				       rates);
4072 }
4073 
4074 static int wl1271_bss_erp_info_changed(struct wl1271 *wl,
4075 				       struct ieee80211_vif *vif,
4076 				       struct ieee80211_bss_conf *bss_conf,
4077 				       u32 changed)
4078 {
4079 	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4080 	int ret = 0;
4081 
4082 	if (changed & BSS_CHANGED_ERP_SLOT) {
4083 		if (bss_conf->use_short_slot)
4084 			ret = wl1271_acx_slot(wl, wlvif, SLOT_TIME_SHORT);
4085 		else
4086 			ret = wl1271_acx_slot(wl, wlvif, SLOT_TIME_LONG);
4087 		if (ret < 0) {
4088 			wl1271_warning("Set slot time failed %d", ret);
4089 			goto out;
4090 		}
4091 	}
4092 
4093 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4094 		if (bss_conf->use_short_preamble)
4095 			wl1271_acx_set_preamble(wl, wlvif, ACX_PREAMBLE_SHORT);
4096 		else
4097 			wl1271_acx_set_preamble(wl, wlvif, ACX_PREAMBLE_LONG);
4098 	}
4099 
4100 	if (changed & BSS_CHANGED_ERP_CTS_PROT) {
4101 		if (bss_conf->use_cts_prot)
4102 			ret = wl1271_acx_cts_protect(wl, wlvif,
4103 						     CTSPROTECT_ENABLE);
4104 		else
4105 			ret = wl1271_acx_cts_protect(wl, wlvif,
4106 						     CTSPROTECT_DISABLE);
4107 		if (ret < 0) {
4108 			wl1271_warning("Set ctsprotect failed %d", ret);
4109 			goto out;
4110 		}
4111 	}
4112 
4113 out:
4114 	return ret;
4115 }
4116 
4117 static int wlcore_set_beacon_template(struct wl1271 *wl,
4118 				      struct ieee80211_vif *vif,
4119 				      bool is_ap)
4120 {
4121 	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4122 	struct ieee80211_hdr *hdr;
4123 	u32 min_rate;
4124 	int ret;
4125 	int ieoffset = offsetof(struct ieee80211_mgmt, u.beacon.variable);
4126 	struct sk_buff *beacon = ieee80211_beacon_get(wl->hw, vif, 0);
4127 	u16 tmpl_id;
4128 
4129 	if (!beacon) {
4130 		ret = -EINVAL;
4131 		goto out;
4132 	}
4133 
4134 	wl1271_debug(DEBUG_MASTER, "beacon updated");
4135 
4136 	ret = wl1271_ssid_set(wlvif, beacon, ieoffset);
4137 	if (ret < 0) {
4138 		dev_kfree_skb(beacon);
4139 		goto out;
4140 	}
4141 	min_rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
4142 	tmpl_id = is_ap ? CMD_TEMPL_AP_BEACON :
4143 		CMD_TEMPL_BEACON;
4144 	ret = wl1271_cmd_template_set(wl, wlvif->role_id, tmpl_id,
4145 				      beacon->data,
4146 				      beacon->len, 0,
4147 				      min_rate);
4148 	if (ret < 0) {
4149 		dev_kfree_skb(beacon);
4150 		goto out;
4151 	}
4152 
4153 	wlvif->wmm_enabled =
4154 		cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
4155 					WLAN_OUI_TYPE_MICROSOFT_WMM,
4156 					beacon->data + ieoffset,
4157 					beacon->len - ieoffset);
4158 
4159 	/*
4160 	 * In case we already have a probe-resp beacon set explicitly
4161 	 * by usermode, don't use the beacon data.
4162 	 */
4163 	if (test_bit(WLVIF_FLAG_AP_PROBE_RESP_SET, &wlvif->flags))
4164 		goto end_bcn;
4165 
4166 	/* remove TIM ie from probe response */
4167 	wl12xx_remove_ie(beacon, WLAN_EID_TIM, ieoffset);
4168 
4169 	/*
4170 	 * remove p2p ie from probe response.
4171 	 * the fw reponds to probe requests that don't include
4172 	 * the p2p ie. probe requests with p2p ie will be passed,
4173 	 * and will be responded by the supplicant (the spec
4174 	 * forbids including the p2p ie when responding to probe
4175 	 * requests that didn't include it).
4176 	 */
4177 	wl12xx_remove_vendor_ie(beacon, WLAN_OUI_WFA,
4178 				WLAN_OUI_TYPE_WFA_P2P, ieoffset);
4179 
4180 	hdr = (struct ieee80211_hdr *) beacon->data;
4181 	hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
4182 					 IEEE80211_STYPE_PROBE_RESP);
4183 	if (is_ap)
4184 		ret = wl1271_ap_set_probe_resp_tmpl_legacy(wl, vif,
4185 							   beacon->data,
4186 							   beacon->len,
4187 							   min_rate);
4188 	else
4189 		ret = wl1271_cmd_template_set(wl, wlvif->role_id,
4190 					      CMD_TEMPL_PROBE_RESPONSE,
4191 					      beacon->data,
4192 					      beacon->len, 0,
4193 					      min_rate);
4194 end_bcn:
4195 	dev_kfree_skb(beacon);
4196 	if (ret < 0)
4197 		goto out;
4198 
4199 out:
4200 	return ret;
4201 }
4202 
4203 static int wl1271_bss_beacon_info_changed(struct wl1271 *wl,
4204 					  struct ieee80211_vif *vif,
4205 					  struct ieee80211_bss_conf *bss_conf,
4206 					  u32 changed)
4207 {
4208 	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4209 	bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
4210 	int ret = 0;
4211 
4212 	if (changed & BSS_CHANGED_BEACON_INT) {
4213 		wl1271_debug(DEBUG_MASTER, "beacon interval updated: %d",
4214 			bss_conf->beacon_int);
4215 
4216 		wlvif->beacon_int = bss_conf->beacon_int;
4217 	}
4218 
4219 	if ((changed & BSS_CHANGED_AP_PROBE_RESP) && is_ap) {
4220 		u32 rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
4221 
4222 		wl1271_ap_set_probe_resp_tmpl(wl, rate, vif);
4223 	}
4224 
4225 	if (changed & BSS_CHANGED_BEACON) {
4226 		ret = wlcore_set_beacon_template(wl, vif, is_ap);
4227 		if (ret < 0)
4228 			goto out;
4229 
4230 		if (test_and_clear_bit(WLVIF_FLAG_BEACON_DISABLED,
4231 				       &wlvif->flags)) {
4232 			ret = wlcore_hw_dfs_master_restart(wl, wlvif);
4233 			if (ret < 0)
4234 				goto out;
4235 		}
4236 	}
4237 out:
4238 	if (ret != 0)
4239 		wl1271_error("beacon info change failed: %d", ret);
4240 	return ret;
4241 }
4242 
4243 /* AP mode changes */
4244 static void wl1271_bss_info_changed_ap(struct wl1271 *wl,
4245 				       struct ieee80211_vif *vif,
4246 				       struct ieee80211_bss_conf *bss_conf,
4247 				       u32 changed)
4248 {
4249 	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4250 	int ret = 0;
4251 
4252 	if (changed & BSS_CHANGED_BASIC_RATES) {
4253 		u32 rates = bss_conf->basic_rates;
4254 
4255 		wlvif->basic_rate_set = wl1271_tx_enabled_rates_get(wl, rates,
4256 								 wlvif->band);
4257 		wlvif->basic_rate = wl1271_tx_min_rate_get(wl,
4258 							wlvif->basic_rate_set);
4259 
4260 		ret = wl1271_init_ap_rates(wl, wlvif);
4261 		if (ret < 0) {
4262 			wl1271_error("AP rate policy change failed %d", ret);
4263 			goto out;
4264 		}
4265 
4266 		ret = wl1271_ap_init_templates(wl, vif);
4267 		if (ret < 0)
4268 			goto out;
4269 
4270 		/* No need to set probe resp template for mesh */
4271 		if (!ieee80211_vif_is_mesh(vif)) {
4272 			ret = wl1271_ap_set_probe_resp_tmpl(wl,
4273 							    wlvif->basic_rate,
4274 							    vif);
4275 			if (ret < 0)
4276 				goto out;
4277 		}
4278 
4279 		ret = wlcore_set_beacon_template(wl, vif, true);
4280 		if (ret < 0)
4281 			goto out;
4282 	}
4283 
4284 	ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf, changed);
4285 	if (ret < 0)
4286 		goto out;
4287 
4288 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
4289 		if (bss_conf->enable_beacon) {
4290 			if (!test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
4291 				ret = wl12xx_cmd_role_start_ap(wl, wlvif);
4292 				if (ret < 0)
4293 					goto out;
4294 
4295 				ret = wl1271_ap_init_hwenc(wl, wlvif);
4296 				if (ret < 0)
4297 					goto out;
4298 
4299 				set_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags);
4300 				wl1271_debug(DEBUG_AP, "started AP");
4301 			}
4302 		} else {
4303 			if (test_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags)) {
4304 				/*
4305 				 * AP might be in ROC in case we have just
4306 				 * sent auth reply. handle it.
4307 				 */
4308 				if (test_bit(wlvif->role_id, wl->roc_map))
4309 					wl12xx_croc(wl, wlvif->role_id);
4310 
4311 				ret = wl12xx_cmd_role_stop_ap(wl, wlvif);
4312 				if (ret < 0)
4313 					goto out;
4314 
4315 				clear_bit(WLVIF_FLAG_AP_STARTED, &wlvif->flags);
4316 				clear_bit(WLVIF_FLAG_AP_PROBE_RESP_SET,
4317 					  &wlvif->flags);
4318 				wl1271_debug(DEBUG_AP, "stopped AP");
4319 			}
4320 		}
4321 	}
4322 
4323 	ret = wl1271_bss_erp_info_changed(wl, vif, bss_conf, changed);
4324 	if (ret < 0)
4325 		goto out;
4326 
4327 	/* Handle HT information change */
4328 	if ((changed & BSS_CHANGED_HT) &&
4329 	    (bss_conf->chanreq.oper.width != NL80211_CHAN_WIDTH_20_NOHT)) {
4330 		ret = wl1271_acx_set_ht_information(wl, wlvif,
4331 					bss_conf->ht_operation_mode);
4332 		if (ret < 0) {
4333 			wl1271_warning("Set ht information failed %d", ret);
4334 			goto out;
4335 		}
4336 	}
4337 
4338 out:
4339 	return;
4340 }
4341 
4342 static int wlcore_set_bssid(struct wl1271 *wl, struct wl12xx_vif *wlvif,
4343 			    struct ieee80211_vif *vif, u32 sta_rate_set)
4344 {
4345 	struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
4346 	u32 rates;
4347 	int ret;
4348 
4349 	wl1271_debug(DEBUG_MAC80211,
4350 	     "changed_bssid: %pM, aid: %d, bcn_int: %d, brates: 0x%x sta_rate_set: 0x%x",
4351 	     bss_conf->bssid, vif->cfg.aid,
4352 	     bss_conf->beacon_int,
4353 	     bss_conf->basic_rates, sta_rate_set);
4354 
4355 	wlvif->beacon_int = bss_conf->beacon_int;
4356 	rates = bss_conf->basic_rates;
4357 	wlvif->basic_rate_set =
4358 		wl1271_tx_enabled_rates_get(wl, rates,
4359 					    wlvif->band);
4360 	wlvif->basic_rate =
4361 		wl1271_tx_min_rate_get(wl,
4362 				       wlvif->basic_rate_set);
4363 
4364 	if (sta_rate_set)
4365 		wlvif->rate_set =
4366 			wl1271_tx_enabled_rates_get(wl,
4367 						sta_rate_set,
4368 						wlvif->band);
4369 
4370 	/* we only support sched_scan while not connected */
4371 	if (wl->sched_vif == wlvif)
4372 		wl->ops->sched_scan_stop(wl, wlvif);
4373 
4374 	ret = wl1271_acx_sta_rate_policies(wl, wlvif);
4375 	if (ret < 0)
4376 		return ret;
4377 
4378 	ret = wl12xx_cmd_build_null_data(wl, wlvif);
4379 	if (ret < 0)
4380 		return ret;
4381 
4382 	ret = wl1271_build_qos_null_data(wl, wl12xx_wlvif_to_vif(wlvif));
4383 	if (ret < 0)
4384 		return ret;
4385 
4386 	wlcore_set_ssid(wl, wlvif);
4387 
4388 	set_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
4389 
4390 	return 0;
4391 }
4392 
4393 static int wlcore_clear_bssid(struct wl1271 *wl, struct wl12xx_vif *wlvif)
4394 {
4395 	int ret;
4396 
4397 	/* revert back to minimum rates for the current band */
4398 	wl1271_set_band_rate(wl, wlvif);
4399 	wlvif->basic_rate = wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
4400 
4401 	ret = wl1271_acx_sta_rate_policies(wl, wlvif);
4402 	if (ret < 0)
4403 		return ret;
4404 
4405 	if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
4406 	    test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags)) {
4407 		ret = wl12xx_cmd_role_stop_sta(wl, wlvif);
4408 		if (ret < 0)
4409 			return ret;
4410 	}
4411 
4412 	clear_bit(WLVIF_FLAG_IN_USE, &wlvif->flags);
4413 	return 0;
4414 }
4415 /* STA/IBSS mode changes */
4416 static void wl1271_bss_info_changed_sta(struct wl1271 *wl,
4417 					struct ieee80211_vif *vif,
4418 					struct ieee80211_bss_conf *bss_conf,
4419 					u32 changed)
4420 {
4421 	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4422 	bool do_join = false;
4423 	bool is_ibss = (wlvif->bss_type == BSS_TYPE_IBSS);
4424 	bool ibss_joined = false;
4425 	u32 sta_rate_set = 0;
4426 	int ret;
4427 	struct ieee80211_sta *sta;
4428 	bool sta_exists = false;
4429 	struct ieee80211_sta_ht_cap sta_ht_cap;
4430 
4431 	if (is_ibss) {
4432 		ret = wl1271_bss_beacon_info_changed(wl, vif, bss_conf,
4433 						     changed);
4434 		if (ret < 0)
4435 			goto out;
4436 	}
4437 
4438 	if (changed & BSS_CHANGED_IBSS) {
4439 		if (vif->cfg.ibss_joined) {
4440 			set_bit(WLVIF_FLAG_IBSS_JOINED, &wlvif->flags);
4441 			ibss_joined = true;
4442 		} else {
4443 			wlcore_unset_assoc(wl, wlvif);
4444 			wl12xx_cmd_role_stop_sta(wl, wlvif);
4445 		}
4446 	}
4447 
4448 	if ((changed & BSS_CHANGED_BEACON_INT) && ibss_joined)
4449 		do_join = true;
4450 
4451 	/* Need to update the SSID (for filtering etc) */
4452 	if ((changed & BSS_CHANGED_BEACON) && ibss_joined)
4453 		do_join = true;
4454 
4455 	if ((changed & BSS_CHANGED_BEACON_ENABLED) && ibss_joined) {
4456 		wl1271_debug(DEBUG_ADHOC, "ad-hoc beaconing: %s",
4457 			     bss_conf->enable_beacon ? "enabled" : "disabled");
4458 
4459 		do_join = true;
4460 	}
4461 
4462 	if (changed & BSS_CHANGED_IDLE && !is_ibss)
4463 		wl1271_sta_handle_idle(wl, wlvif, vif->cfg.idle);
4464 
4465 	if (changed & BSS_CHANGED_CQM) {
4466 		bool enable = false;
4467 		if (bss_conf->cqm_rssi_thold)
4468 			enable = true;
4469 		ret = wl1271_acx_rssi_snr_trigger(wl, wlvif, enable,
4470 						  bss_conf->cqm_rssi_thold,
4471 						  bss_conf->cqm_rssi_hyst);
4472 		if (ret < 0)
4473 			goto out;
4474 		wlvif->rssi_thold = bss_conf->cqm_rssi_thold;
4475 	}
4476 
4477 	if (changed & (BSS_CHANGED_BSSID | BSS_CHANGED_HT |
4478 		       BSS_CHANGED_ASSOC)) {
4479 		rcu_read_lock();
4480 		sta = ieee80211_find_sta(vif, bss_conf->bssid);
4481 		if (sta) {
4482 			u8 *rx_mask = sta->deflink.ht_cap.mcs.rx_mask;
4483 
4484 			/* save the supp_rates of the ap */
4485 			sta_rate_set = sta->deflink.supp_rates[wlvif->band];
4486 			if (sta->deflink.ht_cap.ht_supported)
4487 				sta_rate_set |=
4488 					(rx_mask[0] << HW_HT_RATES_OFFSET) |
4489 					(rx_mask[1] << HW_MIMO_RATES_OFFSET);
4490 			sta_ht_cap = sta->deflink.ht_cap;
4491 			sta_exists = true;
4492 		}
4493 
4494 		rcu_read_unlock();
4495 	}
4496 
4497 	if (changed & BSS_CHANGED_BSSID) {
4498 		if (!is_zero_ether_addr(bss_conf->bssid)) {
4499 			ret = wlcore_set_bssid(wl, wlvif, vif,
4500 					       sta_rate_set);
4501 			if (ret < 0)
4502 				goto out;
4503 
4504 			/* Need to update the BSSID (for filtering etc) */
4505 			do_join = true;
4506 		} else {
4507 			ret = wlcore_clear_bssid(wl, wlvif);
4508 			if (ret < 0)
4509 				goto out;
4510 		}
4511 	}
4512 
4513 	if (changed & BSS_CHANGED_IBSS) {
4514 		wl1271_debug(DEBUG_ADHOC, "ibss_joined: %d",
4515 			     vif->cfg.ibss_joined);
4516 
4517 		if (vif->cfg.ibss_joined) {
4518 			u32 rates = bss_conf->basic_rates;
4519 			wlvif->basic_rate_set =
4520 				wl1271_tx_enabled_rates_get(wl, rates,
4521 							    wlvif->band);
4522 			wlvif->basic_rate =
4523 				wl1271_tx_min_rate_get(wl,
4524 						       wlvif->basic_rate_set);
4525 
4526 			/* by default, use 11b + OFDM rates */
4527 			wlvif->rate_set = CONF_TX_IBSS_DEFAULT_RATES;
4528 			ret = wl1271_acx_sta_rate_policies(wl, wlvif);
4529 			if (ret < 0)
4530 				goto out;
4531 		}
4532 	}
4533 
4534 	if ((changed & BSS_CHANGED_BEACON_INFO) && bss_conf->dtim_period) {
4535 		/* enable beacon filtering */
4536 		ret = wl1271_acx_beacon_filter_opt(wl, wlvif, true);
4537 		if (ret < 0)
4538 			goto out;
4539 	}
4540 
4541 	ret = wl1271_bss_erp_info_changed(wl, vif, bss_conf, changed);
4542 	if (ret < 0)
4543 		goto out;
4544 
4545 	if (do_join) {
4546 		ret = wlcore_join(wl, wlvif);
4547 		if (ret < 0) {
4548 			wl1271_warning("cmd join failed %d", ret);
4549 			goto out;
4550 		}
4551 	}
4552 
4553 	if (changed & BSS_CHANGED_ASSOC) {
4554 		if (vif->cfg.assoc) {
4555 			ret = wlcore_set_assoc(wl, wlvif, bss_conf,
4556 					       sta_rate_set);
4557 			if (ret < 0)
4558 				goto out;
4559 
4560 			if (test_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags))
4561 				wl12xx_set_authorized(wl, wlvif);
4562 		} else {
4563 			wlcore_unset_assoc(wl, wlvif);
4564 		}
4565 	}
4566 
4567 	if (changed & BSS_CHANGED_PS) {
4568 		if (vif->cfg.ps &&
4569 		    test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags) &&
4570 		    !test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags)) {
4571 			int ps_mode;
4572 			char *ps_mode_str;
4573 
4574 			if (wl->conf.conn.forced_ps) {
4575 				ps_mode = STATION_POWER_SAVE_MODE;
4576 				ps_mode_str = "forced";
4577 			} else {
4578 				ps_mode = STATION_AUTO_PS_MODE;
4579 				ps_mode_str = "auto";
4580 			}
4581 
4582 			wl1271_debug(DEBUG_PSM, "%s ps enabled", ps_mode_str);
4583 
4584 			ret = wl1271_ps_set_mode(wl, wlvif, ps_mode);
4585 			if (ret < 0)
4586 				wl1271_warning("enter %s ps failed %d",
4587 					       ps_mode_str, ret);
4588 		} else if (!vif->cfg.ps &&
4589 			   test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags)) {
4590 			wl1271_debug(DEBUG_PSM, "auto ps disabled");
4591 
4592 			ret = wl1271_ps_set_mode(wl, wlvif,
4593 						 STATION_ACTIVE_MODE);
4594 			if (ret < 0)
4595 				wl1271_warning("exit auto ps failed %d", ret);
4596 		}
4597 	}
4598 
4599 	/* Handle new association with HT. Do this after join. */
4600 	if (sta_exists) {
4601 		bool enabled =
4602 			bss_conf->chanreq.oper.width != NL80211_CHAN_WIDTH_20_NOHT;
4603 
4604 		ret = wlcore_hw_set_peer_cap(wl,
4605 					     &sta_ht_cap,
4606 					     enabled,
4607 					     wlvif->rate_set,
4608 					     wlvif->sta.hlid);
4609 		if (ret < 0) {
4610 			wl1271_warning("Set ht cap failed %d", ret);
4611 			goto out;
4612 
4613 		}
4614 
4615 		if (enabled) {
4616 			ret = wl1271_acx_set_ht_information(wl, wlvif,
4617 						bss_conf->ht_operation_mode);
4618 			if (ret < 0) {
4619 				wl1271_warning("Set ht information failed %d",
4620 					       ret);
4621 				goto out;
4622 			}
4623 		}
4624 	}
4625 
4626 	/* Handle arp filtering. Done after join. */
4627 	if ((changed & BSS_CHANGED_ARP_FILTER) ||
4628 	    (!is_ibss && (changed & BSS_CHANGED_QOS))) {
4629 		__be32 addr = vif->cfg.arp_addr_list[0];
4630 		wlvif->sta.qos = bss_conf->qos;
4631 		WARN_ON(wlvif->bss_type != BSS_TYPE_STA_BSS);
4632 
4633 		if (vif->cfg.arp_addr_cnt == 1 && vif->cfg.assoc) {
4634 			wlvif->ip_addr = addr;
4635 			/*
4636 			 * The template should have been configured only upon
4637 			 * association. however, it seems that the correct ip
4638 			 * isn't being set (when sending), so we have to
4639 			 * reconfigure the template upon every ip change.
4640 			 */
4641 			ret = wl1271_cmd_build_arp_rsp(wl, wlvif);
4642 			if (ret < 0) {
4643 				wl1271_warning("build arp rsp failed: %d", ret);
4644 				goto out;
4645 			}
4646 
4647 			ret = wl1271_acx_arp_ip_filter(wl, wlvif,
4648 				(ACX_ARP_FILTER_ARP_FILTERING |
4649 				 ACX_ARP_FILTER_AUTO_ARP),
4650 				addr);
4651 		} else {
4652 			wlvif->ip_addr = 0;
4653 			ret = wl1271_acx_arp_ip_filter(wl, wlvif, 0, addr);
4654 		}
4655 
4656 		if (ret < 0)
4657 			goto out;
4658 	}
4659 
4660 out:
4661 	return;
4662 }
4663 
4664 static void wl1271_op_bss_info_changed(struct ieee80211_hw *hw,
4665 				       struct ieee80211_vif *vif,
4666 				       struct ieee80211_bss_conf *bss_conf,
4667 				       u64 changed)
4668 {
4669 	struct wl1271 *wl = hw->priv;
4670 	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4671 	bool is_ap = (wlvif->bss_type == BSS_TYPE_AP_BSS);
4672 	int ret;
4673 
4674 	wl1271_debug(DEBUG_MAC80211, "mac80211 bss info role %d changed 0x%x",
4675 		     wlvif->role_id, (int)changed);
4676 
4677 	/*
4678 	 * make sure to cancel pending disconnections if our association
4679 	 * state changed
4680 	 */
4681 	if (!is_ap && (changed & BSS_CHANGED_ASSOC))
4682 		cancel_delayed_work_sync(&wlvif->connection_loss_work);
4683 
4684 	if (is_ap && (changed & BSS_CHANGED_BEACON_ENABLED) &&
4685 	    !bss_conf->enable_beacon)
4686 		wl1271_tx_flush(wl);
4687 
4688 	mutex_lock(&wl->mutex);
4689 
4690 	if (unlikely(wl->state != WLCORE_STATE_ON))
4691 		goto out;
4692 
4693 	if (unlikely(!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)))
4694 		goto out;
4695 
4696 	ret = pm_runtime_resume_and_get(wl->dev);
4697 	if (ret < 0)
4698 		goto out;
4699 
4700 	if ((changed & BSS_CHANGED_TXPOWER) &&
4701 	    bss_conf->txpower != wlvif->power_level) {
4702 
4703 		ret = wl1271_acx_tx_power(wl, wlvif, bss_conf->txpower);
4704 		if (ret < 0)
4705 			goto out;
4706 
4707 		wlvif->power_level = bss_conf->txpower;
4708 	}
4709 
4710 	if (is_ap)
4711 		wl1271_bss_info_changed_ap(wl, vif, bss_conf, changed);
4712 	else
4713 		wl1271_bss_info_changed_sta(wl, vif, bss_conf, changed);
4714 
4715 	pm_runtime_put_autosuspend(wl->dev);
4716 
4717 out:
4718 	mutex_unlock(&wl->mutex);
4719 }
4720 
4721 static int wlcore_op_add_chanctx(struct ieee80211_hw *hw,
4722 				 struct ieee80211_chanctx_conf *ctx)
4723 {
4724 	wl1271_debug(DEBUG_MAC80211, "mac80211 add chanctx %d (type %d)",
4725 		     ieee80211_frequency_to_channel(ctx->def.chan->center_freq),
4726 		     cfg80211_get_chandef_type(&ctx->def));
4727 	return 0;
4728 }
4729 
4730 static void wlcore_op_remove_chanctx(struct ieee80211_hw *hw,
4731 				     struct ieee80211_chanctx_conf *ctx)
4732 {
4733 	wl1271_debug(DEBUG_MAC80211, "mac80211 remove chanctx %d (type %d)",
4734 		     ieee80211_frequency_to_channel(ctx->def.chan->center_freq),
4735 		     cfg80211_get_chandef_type(&ctx->def));
4736 }
4737 
4738 static void wlcore_op_change_chanctx(struct ieee80211_hw *hw,
4739 				     struct ieee80211_chanctx_conf *ctx,
4740 				     u32 changed)
4741 {
4742 	struct wl1271 *wl = hw->priv;
4743 	struct wl12xx_vif *wlvif;
4744 	int ret;
4745 	int channel = ieee80211_frequency_to_channel(
4746 		ctx->def.chan->center_freq);
4747 
4748 	wl1271_debug(DEBUG_MAC80211,
4749 		     "mac80211 change chanctx %d (type %d) changed 0x%x",
4750 		     channel, cfg80211_get_chandef_type(&ctx->def), changed);
4751 
4752 	mutex_lock(&wl->mutex);
4753 
4754 	ret = pm_runtime_resume_and_get(wl->dev);
4755 	if (ret < 0)
4756 		goto out;
4757 
4758 	wl12xx_for_each_wlvif(wl, wlvif) {
4759 		struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
4760 
4761 		rcu_read_lock();
4762 		if (rcu_access_pointer(vif->bss_conf.chanctx_conf) != ctx) {
4763 			rcu_read_unlock();
4764 			continue;
4765 		}
4766 		rcu_read_unlock();
4767 
4768 		/* start radar if needed */
4769 		if (changed & IEEE80211_CHANCTX_CHANGE_RADAR &&
4770 		    wlvif->bss_type == BSS_TYPE_AP_BSS &&
4771 		    ctx->radar_enabled && !wlvif->radar_enabled &&
4772 		    ctx->def.chan->dfs_state == NL80211_DFS_USABLE) {
4773 			wl1271_debug(DEBUG_MAC80211, "Start radar detection");
4774 			wlcore_hw_set_cac(wl, wlvif, true);
4775 			wlvif->radar_enabled = true;
4776 		}
4777 	}
4778 
4779 	pm_runtime_put_autosuspend(wl->dev);
4780 out:
4781 	mutex_unlock(&wl->mutex);
4782 }
4783 
4784 static int wlcore_op_assign_vif_chanctx(struct ieee80211_hw *hw,
4785 					struct ieee80211_vif *vif,
4786 					struct ieee80211_bss_conf *link_conf,
4787 					struct ieee80211_chanctx_conf *ctx)
4788 {
4789 	struct wl1271 *wl = hw->priv;
4790 	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4791 	int channel = ieee80211_frequency_to_channel(
4792 		ctx->def.chan->center_freq);
4793 	int ret = -EINVAL;
4794 
4795 	wl1271_debug(DEBUG_MAC80211,
4796 		     "mac80211 assign chanctx (role %d) %d (type %d) (radar %d dfs_state %d)",
4797 		     wlvif->role_id, channel,
4798 		     cfg80211_get_chandef_type(&ctx->def),
4799 		     ctx->radar_enabled, ctx->def.chan->dfs_state);
4800 
4801 	mutex_lock(&wl->mutex);
4802 
4803 	if (unlikely(wl->state != WLCORE_STATE_ON))
4804 		goto out;
4805 
4806 	if (unlikely(!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)))
4807 		goto out;
4808 
4809 	ret = pm_runtime_resume_and_get(wl->dev);
4810 	if (ret < 0)
4811 		goto out;
4812 
4813 	wlvif->band = ctx->def.chan->band;
4814 	wlvif->channel = channel;
4815 	wlvif->channel_type = cfg80211_get_chandef_type(&ctx->def);
4816 
4817 	/* update default rates according to the band */
4818 	wl1271_set_band_rate(wl, wlvif);
4819 
4820 	if (ctx->radar_enabled &&
4821 	    ctx->def.chan->dfs_state == NL80211_DFS_USABLE) {
4822 		wl1271_debug(DEBUG_MAC80211, "Start radar detection");
4823 		wlcore_hw_set_cac(wl, wlvif, true);
4824 		wlvif->radar_enabled = true;
4825 	}
4826 
4827 	pm_runtime_put_autosuspend(wl->dev);
4828 out:
4829 	mutex_unlock(&wl->mutex);
4830 
4831 	return 0;
4832 }
4833 
4834 static void wlcore_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
4835 					   struct ieee80211_vif *vif,
4836 					   struct ieee80211_bss_conf *link_conf,
4837 					   struct ieee80211_chanctx_conf *ctx)
4838 {
4839 	struct wl1271 *wl = hw->priv;
4840 	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4841 	int ret;
4842 
4843 	wl1271_debug(DEBUG_MAC80211,
4844 		     "mac80211 unassign chanctx (role %d) %d (type %d)",
4845 		     wlvif->role_id,
4846 		     ieee80211_frequency_to_channel(ctx->def.chan->center_freq),
4847 		     cfg80211_get_chandef_type(&ctx->def));
4848 
4849 	wl1271_tx_flush(wl);
4850 
4851 	mutex_lock(&wl->mutex);
4852 
4853 	if (unlikely(wl->state != WLCORE_STATE_ON))
4854 		goto out;
4855 
4856 	if (unlikely(!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags)))
4857 		goto out;
4858 
4859 	ret = pm_runtime_resume_and_get(wl->dev);
4860 	if (ret < 0)
4861 		goto out;
4862 
4863 	if (wlvif->radar_enabled) {
4864 		wl1271_debug(DEBUG_MAC80211, "Stop radar detection");
4865 		wlcore_hw_set_cac(wl, wlvif, false);
4866 		wlvif->radar_enabled = false;
4867 	}
4868 
4869 	pm_runtime_put_autosuspend(wl->dev);
4870 out:
4871 	mutex_unlock(&wl->mutex);
4872 }
4873 
4874 static int __wlcore_switch_vif_chan(struct wl1271 *wl,
4875 				    struct wl12xx_vif *wlvif,
4876 				    struct ieee80211_chanctx_conf *new_ctx)
4877 {
4878 	int channel = ieee80211_frequency_to_channel(
4879 		new_ctx->def.chan->center_freq);
4880 
4881 	wl1271_debug(DEBUG_MAC80211,
4882 		     "switch vif (role %d) %d -> %d chan_type: %d",
4883 		     wlvif->role_id, wlvif->channel, channel,
4884 		     cfg80211_get_chandef_type(&new_ctx->def));
4885 
4886 	if (WARN_ON_ONCE(wlvif->bss_type != BSS_TYPE_AP_BSS))
4887 		return 0;
4888 
4889 	WARN_ON(!test_bit(WLVIF_FLAG_BEACON_DISABLED, &wlvif->flags));
4890 
4891 	if (wlvif->radar_enabled) {
4892 		wl1271_debug(DEBUG_MAC80211, "Stop radar detection");
4893 		wlcore_hw_set_cac(wl, wlvif, false);
4894 		wlvif->radar_enabled = false;
4895 	}
4896 
4897 	wlvif->band = new_ctx->def.chan->band;
4898 	wlvif->channel = channel;
4899 	wlvif->channel_type = cfg80211_get_chandef_type(&new_ctx->def);
4900 
4901 	/* start radar if needed */
4902 	if (new_ctx->radar_enabled) {
4903 		wl1271_debug(DEBUG_MAC80211, "Start radar detection");
4904 		wlcore_hw_set_cac(wl, wlvif, true);
4905 		wlvif->radar_enabled = true;
4906 	}
4907 
4908 	return 0;
4909 }
4910 
4911 static int
4912 wlcore_op_switch_vif_chanctx(struct ieee80211_hw *hw,
4913 			     struct ieee80211_vif_chanctx_switch *vifs,
4914 			     int n_vifs,
4915 			     enum ieee80211_chanctx_switch_mode mode)
4916 {
4917 	struct wl1271 *wl = hw->priv;
4918 	int i, ret;
4919 
4920 	wl1271_debug(DEBUG_MAC80211,
4921 		     "mac80211 switch chanctx n_vifs %d mode %d",
4922 		     n_vifs, mode);
4923 
4924 	mutex_lock(&wl->mutex);
4925 
4926 	ret = pm_runtime_resume_and_get(wl->dev);
4927 	if (ret < 0)
4928 		goto out;
4929 
4930 	for (i = 0; i < n_vifs; i++) {
4931 		struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vifs[i].vif);
4932 
4933 		ret = __wlcore_switch_vif_chan(wl, wlvif, vifs[i].new_ctx);
4934 		if (ret)
4935 			goto out_sleep;
4936 	}
4937 out_sleep:
4938 	pm_runtime_put_autosuspend(wl->dev);
4939 out:
4940 	mutex_unlock(&wl->mutex);
4941 
4942 	return 0;
4943 }
4944 
4945 static int wl1271_op_conf_tx(struct ieee80211_hw *hw,
4946 			     struct ieee80211_vif *vif,
4947 			     unsigned int link_id, u16 queue,
4948 			     const struct ieee80211_tx_queue_params *params)
4949 {
4950 	struct wl1271 *wl = hw->priv;
4951 	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
4952 	u8 ps_scheme;
4953 	int ret = 0;
4954 
4955 	if (wlcore_is_p2p_mgmt(wlvif))
4956 		return 0;
4957 
4958 	mutex_lock(&wl->mutex);
4959 
4960 	wl1271_debug(DEBUG_MAC80211, "mac80211 conf tx %d", queue);
4961 
4962 	if (params->uapsd)
4963 		ps_scheme = CONF_PS_SCHEME_UPSD_TRIGGER;
4964 	else
4965 		ps_scheme = CONF_PS_SCHEME_LEGACY;
4966 
4967 	if (!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
4968 		goto out;
4969 
4970 	ret = pm_runtime_resume_and_get(wl->dev);
4971 	if (ret < 0)
4972 		goto out;
4973 
4974 	/*
4975 	 * the txop is confed in units of 32us by the mac80211,
4976 	 * we need us
4977 	 */
4978 	ret = wl1271_acx_ac_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
4979 				params->cw_min, params->cw_max,
4980 				params->aifs, params->txop << 5);
4981 	if (ret < 0)
4982 		goto out_sleep;
4983 
4984 	ret = wl1271_acx_tid_cfg(wl, wlvif, wl1271_tx_get_queue(queue),
4985 				 CONF_CHANNEL_TYPE_EDCF,
4986 				 wl1271_tx_get_queue(queue),
4987 				 ps_scheme, CONF_ACK_POLICY_LEGACY,
4988 				 0, 0);
4989 
4990 out_sleep:
4991 	pm_runtime_put_autosuspend(wl->dev);
4992 
4993 out:
4994 	mutex_unlock(&wl->mutex);
4995 
4996 	return ret;
4997 }
4998 
4999 static u64 wl1271_op_get_tsf(struct ieee80211_hw *hw,
5000 			     struct ieee80211_vif *vif)
5001 {
5002 
5003 	struct wl1271 *wl = hw->priv;
5004 	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5005 	u64 mactime = ULLONG_MAX;
5006 	int ret;
5007 
5008 	wl1271_debug(DEBUG_MAC80211, "mac80211 get tsf");
5009 
5010 	mutex_lock(&wl->mutex);
5011 
5012 	if (unlikely(wl->state != WLCORE_STATE_ON))
5013 		goto out;
5014 
5015 	ret = pm_runtime_resume_and_get(wl->dev);
5016 	if (ret < 0)
5017 		goto out;
5018 
5019 	ret = wl12xx_acx_tsf_info(wl, wlvif, &mactime);
5020 	if (ret < 0)
5021 		goto out_sleep;
5022 
5023 out_sleep:
5024 	pm_runtime_put_autosuspend(wl->dev);
5025 
5026 out:
5027 	mutex_unlock(&wl->mutex);
5028 	return mactime;
5029 }
5030 
5031 static int wl1271_op_get_survey(struct ieee80211_hw *hw, int idx,
5032 				struct survey_info *survey)
5033 {
5034 	struct ieee80211_conf *conf = &hw->conf;
5035 
5036 	if (idx != 0)
5037 		return -ENOENT;
5038 
5039 	survey->channel = conf->chandef.chan;
5040 	survey->filled = 0;
5041 	return 0;
5042 }
5043 
5044 static int wl1271_allocate_sta(struct wl1271 *wl,
5045 			     struct wl12xx_vif *wlvif,
5046 			     struct ieee80211_sta *sta)
5047 {
5048 	struct wl1271_station *wl_sta;
5049 	int ret;
5050 
5051 
5052 	if (wl->active_sta_count >= wl->max_ap_stations) {
5053 		wl1271_warning("could not allocate HLID - too much stations");
5054 		return -EBUSY;
5055 	}
5056 
5057 	wl_sta = (struct wl1271_station *)sta->drv_priv;
5058 	ret = wl12xx_allocate_link(wl, wlvif, &wl_sta->hlid);
5059 	if (ret < 0) {
5060 		wl1271_warning("could not allocate HLID - too many links");
5061 		return -EBUSY;
5062 	}
5063 
5064 	/* use the previous security seq, if this is a recovery/resume */
5065 	wl->links[wl_sta->hlid].total_freed_pkts = wl_sta->total_freed_pkts;
5066 
5067 	set_bit(wl_sta->hlid, wlvif->ap.sta_hlid_map);
5068 	memcpy(wl->links[wl_sta->hlid].addr, sta->addr, ETH_ALEN);
5069 	wl->active_sta_count++;
5070 	return 0;
5071 }
5072 
5073 void wl1271_free_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 hlid)
5074 {
5075 	if (!test_bit(hlid, wlvif->ap.sta_hlid_map))
5076 		return;
5077 
5078 	clear_bit(hlid, wlvif->ap.sta_hlid_map);
5079 	__clear_bit(hlid, &wl->ap_ps_map);
5080 	__clear_bit(hlid, &wl->ap_fw_ps_map);
5081 
5082 	/*
5083 	 * save the last used PN in the private part of iee80211_sta,
5084 	 * in case of recovery/suspend
5085 	 */
5086 	wlcore_save_freed_pkts_addr(wl, wlvif, hlid, wl->links[hlid].addr);
5087 
5088 	wl12xx_free_link(wl, wlvif, &hlid);
5089 	wl->active_sta_count--;
5090 
5091 	/*
5092 	 * rearm the tx watchdog when the last STA is freed - give the FW a
5093 	 * chance to return STA-buffered packets before complaining.
5094 	 */
5095 	if (wl->active_sta_count == 0)
5096 		wl12xx_rearm_tx_watchdog_locked(wl);
5097 }
5098 
5099 static int wl12xx_sta_add(struct wl1271 *wl,
5100 			  struct wl12xx_vif *wlvif,
5101 			  struct ieee80211_sta *sta)
5102 {
5103 	struct wl1271_station *wl_sta;
5104 	int ret = 0;
5105 	u8 hlid;
5106 
5107 	wl1271_debug(DEBUG_MAC80211, "mac80211 add sta %d", (int)sta->aid);
5108 
5109 	ret = wl1271_allocate_sta(wl, wlvif, sta);
5110 	if (ret < 0)
5111 		return ret;
5112 
5113 	wl_sta = (struct wl1271_station *)sta->drv_priv;
5114 	hlid = wl_sta->hlid;
5115 
5116 	ret = wl12xx_cmd_add_peer(wl, wlvif, sta, hlid);
5117 	if (ret < 0)
5118 		wl1271_free_sta(wl, wlvif, hlid);
5119 
5120 	return ret;
5121 }
5122 
5123 static int wl12xx_sta_remove(struct wl1271 *wl,
5124 			     struct wl12xx_vif *wlvif,
5125 			     struct ieee80211_sta *sta)
5126 {
5127 	struct wl1271_station *wl_sta;
5128 	int ret = 0, id;
5129 
5130 	wl1271_debug(DEBUG_MAC80211, "mac80211 remove sta %d", (int)sta->aid);
5131 
5132 	wl_sta = (struct wl1271_station *)sta->drv_priv;
5133 	id = wl_sta->hlid;
5134 	if (WARN_ON(!test_bit(id, wlvif->ap.sta_hlid_map)))
5135 		return -EINVAL;
5136 
5137 	ret = wl12xx_cmd_remove_peer(wl, wlvif, wl_sta->hlid);
5138 	if (ret < 0)
5139 		return ret;
5140 
5141 	wl1271_free_sta(wl, wlvif, wl_sta->hlid);
5142 	return ret;
5143 }
5144 
5145 static void wlcore_roc_if_possible(struct wl1271 *wl,
5146 				   struct wl12xx_vif *wlvif)
5147 {
5148 	if (find_first_bit(wl->roc_map,
5149 			   WL12XX_MAX_ROLES) < WL12XX_MAX_ROLES)
5150 		return;
5151 
5152 	if (WARN_ON(wlvif->role_id == WL12XX_INVALID_ROLE_ID))
5153 		return;
5154 
5155 	wl12xx_roc(wl, wlvif, wlvif->role_id, wlvif->band, wlvif->channel);
5156 }
5157 
5158 /*
5159  * when wl_sta is NULL, we treat this call as if coming from a
5160  * pending auth reply.
5161  * wl->mutex must be taken and the FW must be awake when the call
5162  * takes place.
5163  */
5164 void wlcore_update_inconn_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif,
5165 			      struct wl1271_station *wl_sta, bool in_conn)
5166 {
5167 	if (in_conn) {
5168 		if (WARN_ON(wl_sta && wl_sta->in_connection))
5169 			return;
5170 
5171 		if (!wlvif->ap_pending_auth_reply &&
5172 		    !wlvif->inconn_count)
5173 			wlcore_roc_if_possible(wl, wlvif);
5174 
5175 		if (wl_sta) {
5176 			wl_sta->in_connection = true;
5177 			wlvif->inconn_count++;
5178 		} else {
5179 			wlvif->ap_pending_auth_reply = true;
5180 		}
5181 	} else {
5182 		if (wl_sta && !wl_sta->in_connection)
5183 			return;
5184 
5185 		if (WARN_ON(!wl_sta && !wlvif->ap_pending_auth_reply))
5186 			return;
5187 
5188 		if (WARN_ON(wl_sta && !wlvif->inconn_count))
5189 			return;
5190 
5191 		if (wl_sta) {
5192 			wl_sta->in_connection = false;
5193 			wlvif->inconn_count--;
5194 		} else {
5195 			wlvif->ap_pending_auth_reply = false;
5196 		}
5197 
5198 		if (!wlvif->inconn_count && !wlvif->ap_pending_auth_reply &&
5199 		    test_bit(wlvif->role_id, wl->roc_map))
5200 			wl12xx_croc(wl, wlvif->role_id);
5201 	}
5202 }
5203 
5204 static int wl12xx_update_sta_state(struct wl1271 *wl,
5205 				   struct wl12xx_vif *wlvif,
5206 				   struct ieee80211_sta *sta,
5207 				   enum ieee80211_sta_state old_state,
5208 				   enum ieee80211_sta_state new_state)
5209 {
5210 	struct wl1271_station *wl_sta;
5211 	bool is_ap = wlvif->bss_type == BSS_TYPE_AP_BSS;
5212 	bool is_sta = wlvif->bss_type == BSS_TYPE_STA_BSS;
5213 	int ret;
5214 
5215 	wl_sta = (struct wl1271_station *)sta->drv_priv;
5216 
5217 	/* Add station (AP mode) */
5218 	if (is_ap &&
5219 	    old_state == IEEE80211_STA_AUTH &&
5220 	    new_state == IEEE80211_STA_ASSOC) {
5221 		ret = wl12xx_sta_add(wl, wlvif, sta);
5222 		if (ret)
5223 			return ret;
5224 
5225 		wl_sta->fw_added = true;
5226 
5227 		wlcore_update_inconn_sta(wl, wlvif, wl_sta, true);
5228 	}
5229 
5230 	/* Remove station (AP mode) */
5231 	if (is_ap &&
5232 	    old_state == IEEE80211_STA_ASSOC &&
5233 	    new_state == IEEE80211_STA_AUTH) {
5234 		wl_sta->fw_added = false;
5235 
5236 		/* must not fail */
5237 		wl12xx_sta_remove(wl, wlvif, sta);
5238 
5239 		wlcore_update_inconn_sta(wl, wlvif, wl_sta, false);
5240 	}
5241 
5242 	/* Authorize station (AP mode) */
5243 	if (is_ap &&
5244 	    new_state == IEEE80211_STA_AUTHORIZED) {
5245 		ret = wl12xx_cmd_set_peer_state(wl, wlvif, wl_sta->hlid);
5246 		if (ret < 0)
5247 			return ret;
5248 
5249 		ret = wl1271_acx_set_ht_capabilities(wl, &sta->deflink.ht_cap,
5250 						     true,
5251 						     wl_sta->hlid);
5252 		if (ret)
5253 			return ret;
5254 
5255 		wlcore_update_inconn_sta(wl, wlvif, wl_sta, false);
5256 	}
5257 
5258 	/* Authorize station */
5259 	if (is_sta &&
5260 	    new_state == IEEE80211_STA_AUTHORIZED) {
5261 		set_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags);
5262 		ret = wl12xx_set_authorized(wl, wlvif);
5263 		if (ret)
5264 			return ret;
5265 	}
5266 
5267 	if (is_sta &&
5268 	    old_state == IEEE80211_STA_AUTHORIZED &&
5269 	    new_state == IEEE80211_STA_ASSOC) {
5270 		clear_bit(WLVIF_FLAG_STA_AUTHORIZED, &wlvif->flags);
5271 		clear_bit(WLVIF_FLAG_STA_STATE_SENT, &wlvif->flags);
5272 	}
5273 
5274 	/* save seq number on disassoc (suspend) */
5275 	if (is_sta &&
5276 	    old_state == IEEE80211_STA_ASSOC &&
5277 	    new_state == IEEE80211_STA_AUTH) {
5278 		wlcore_save_freed_pkts(wl, wlvif, wlvif->sta.hlid, sta);
5279 		wlvif->total_freed_pkts = 0;
5280 	}
5281 
5282 	/* restore seq number on assoc (resume) */
5283 	if (is_sta &&
5284 	    old_state == IEEE80211_STA_AUTH &&
5285 	    new_state == IEEE80211_STA_ASSOC) {
5286 		wlvif->total_freed_pkts = wl_sta->total_freed_pkts;
5287 	}
5288 
5289 	/* clear ROCs on failure or authorization */
5290 	if (is_sta &&
5291 	    (new_state == IEEE80211_STA_AUTHORIZED ||
5292 	     new_state == IEEE80211_STA_NOTEXIST)) {
5293 		if (test_bit(wlvif->role_id, wl->roc_map))
5294 			wl12xx_croc(wl, wlvif->role_id);
5295 	}
5296 
5297 	if (is_sta &&
5298 	    old_state == IEEE80211_STA_NOTEXIST &&
5299 	    new_state == IEEE80211_STA_NONE) {
5300 		if (find_first_bit(wl->roc_map,
5301 				   WL12XX_MAX_ROLES) >= WL12XX_MAX_ROLES) {
5302 			WARN_ON(wlvif->role_id == WL12XX_INVALID_ROLE_ID);
5303 			wl12xx_roc(wl, wlvif, wlvif->role_id,
5304 				   wlvif->band, wlvif->channel);
5305 		}
5306 	}
5307 	return 0;
5308 }
5309 
5310 static int wl12xx_op_sta_state(struct ieee80211_hw *hw,
5311 			       struct ieee80211_vif *vif,
5312 			       struct ieee80211_sta *sta,
5313 			       enum ieee80211_sta_state old_state,
5314 			       enum ieee80211_sta_state new_state)
5315 {
5316 	struct wl1271 *wl = hw->priv;
5317 	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5318 	int ret;
5319 
5320 	wl1271_debug(DEBUG_MAC80211, "mac80211 sta %d state=%d->%d",
5321 		     sta->aid, old_state, new_state);
5322 
5323 	mutex_lock(&wl->mutex);
5324 
5325 	if (unlikely(wl->state != WLCORE_STATE_ON)) {
5326 		ret = -EBUSY;
5327 		goto out;
5328 	}
5329 
5330 	ret = pm_runtime_resume_and_get(wl->dev);
5331 	if (ret < 0)
5332 		goto out;
5333 
5334 	ret = wl12xx_update_sta_state(wl, wlvif, sta, old_state, new_state);
5335 
5336 	pm_runtime_put_autosuspend(wl->dev);
5337 out:
5338 	mutex_unlock(&wl->mutex);
5339 	if (new_state < old_state)
5340 		return 0;
5341 	return ret;
5342 }
5343 
5344 static int wl1271_op_ampdu_action(struct ieee80211_hw *hw,
5345 				  struct ieee80211_vif *vif,
5346 				  struct ieee80211_ampdu_params *params)
5347 {
5348 	struct wl1271 *wl = hw->priv;
5349 	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5350 	int ret;
5351 	u8 hlid, *ba_bitmap;
5352 	struct ieee80211_sta *sta = params->sta;
5353 	enum ieee80211_ampdu_mlme_action action = params->action;
5354 	u16 tid = params->tid;
5355 	u16 *ssn = &params->ssn;
5356 
5357 	wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu action %d tid %d", action,
5358 		     tid);
5359 
5360 	/* sanity check - the fields in FW are only 8bits wide */
5361 	if (WARN_ON(tid > 0xFF))
5362 		return -ENOTSUPP;
5363 
5364 	mutex_lock(&wl->mutex);
5365 
5366 	if (unlikely(wl->state != WLCORE_STATE_ON)) {
5367 		ret = -EAGAIN;
5368 		goto out;
5369 	}
5370 
5371 	if (wlvif->bss_type == BSS_TYPE_STA_BSS) {
5372 		hlid = wlvif->sta.hlid;
5373 	} else if (wlvif->bss_type == BSS_TYPE_AP_BSS) {
5374 		struct wl1271_station *wl_sta;
5375 
5376 		wl_sta = (struct wl1271_station *)sta->drv_priv;
5377 		hlid = wl_sta->hlid;
5378 	} else {
5379 		ret = -EINVAL;
5380 		goto out;
5381 	}
5382 
5383 	ba_bitmap = &wl->links[hlid].ba_bitmap;
5384 
5385 	ret = pm_runtime_resume_and_get(wl->dev);
5386 	if (ret < 0)
5387 		goto out;
5388 
5389 	wl1271_debug(DEBUG_MAC80211, "mac80211 ampdu: Rx tid %d action %d",
5390 		     tid, action);
5391 
5392 	switch (action) {
5393 	case IEEE80211_AMPDU_RX_START:
5394 		if (!wlvif->ba_support || !wlvif->ba_allowed) {
5395 			ret = -ENOTSUPP;
5396 			break;
5397 		}
5398 
5399 		if (wl->ba_rx_session_count >= wl->ba_rx_session_count_max) {
5400 			ret = -EBUSY;
5401 			wl1271_debug(DEBUG_RX, "exceeded max RX BA sessions");
5402 			break;
5403 		}
5404 
5405 		if (*ba_bitmap & BIT(tid)) {
5406 			ret = -EINVAL;
5407 			wl1271_error("cannot enable RX BA session on active "
5408 				     "tid: %d", tid);
5409 			break;
5410 		}
5411 
5412 		ret = wl12xx_acx_set_ba_receiver_session(wl, tid, *ssn, true,
5413 				hlid,
5414 				params->buf_size);
5415 
5416 		if (!ret) {
5417 			*ba_bitmap |= BIT(tid);
5418 			wl->ba_rx_session_count++;
5419 		}
5420 		break;
5421 
5422 	case IEEE80211_AMPDU_RX_STOP:
5423 		if (!(*ba_bitmap & BIT(tid))) {
5424 			/*
5425 			 * this happens on reconfig - so only output a debug
5426 			 * message for now, and don't fail the function.
5427 			 */
5428 			wl1271_debug(DEBUG_MAC80211,
5429 				     "no active RX BA session on tid: %d",
5430 				     tid);
5431 			ret = 0;
5432 			break;
5433 		}
5434 
5435 		ret = wl12xx_acx_set_ba_receiver_session(wl, tid, 0, false,
5436 							 hlid, 0);
5437 		if (!ret) {
5438 			*ba_bitmap &= ~BIT(tid);
5439 			wl->ba_rx_session_count--;
5440 		}
5441 		break;
5442 
5443 	/*
5444 	 * The BA initiator session management in FW independently.
5445 	 * Falling break here on purpose for all TX APDU commands.
5446 	 */
5447 	case IEEE80211_AMPDU_TX_START:
5448 	case IEEE80211_AMPDU_TX_STOP_CONT:
5449 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
5450 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
5451 	case IEEE80211_AMPDU_TX_OPERATIONAL:
5452 		ret = -EINVAL;
5453 		break;
5454 
5455 	default:
5456 		wl1271_error("Incorrect ampdu action id=%x\n", action);
5457 		ret = -EINVAL;
5458 	}
5459 
5460 	pm_runtime_put_autosuspend(wl->dev);
5461 
5462 out:
5463 	mutex_unlock(&wl->mutex);
5464 
5465 	return ret;
5466 }
5467 
5468 static int wl12xx_set_bitrate_mask(struct ieee80211_hw *hw,
5469 				   struct ieee80211_vif *vif,
5470 				   const struct cfg80211_bitrate_mask *mask)
5471 {
5472 	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5473 	struct wl1271 *wl = hw->priv;
5474 	int i, ret = 0;
5475 
5476 	wl1271_debug(DEBUG_MAC80211, "mac80211 set_bitrate_mask 0x%x 0x%x",
5477 		mask->control[NL80211_BAND_2GHZ].legacy,
5478 		mask->control[NL80211_BAND_5GHZ].legacy);
5479 
5480 	mutex_lock(&wl->mutex);
5481 
5482 	for (i = 0; i < WLCORE_NUM_BANDS; i++)
5483 		wlvif->bitrate_masks[i] =
5484 			wl1271_tx_enabled_rates_get(wl,
5485 						    mask->control[i].legacy,
5486 						    i);
5487 
5488 	if (unlikely(wl->state != WLCORE_STATE_ON))
5489 		goto out;
5490 
5491 	if (wlvif->bss_type == BSS_TYPE_STA_BSS &&
5492 	    !test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
5493 
5494 		ret = pm_runtime_resume_and_get(wl->dev);
5495 		if (ret < 0)
5496 			goto out;
5497 
5498 		wl1271_set_band_rate(wl, wlvif);
5499 		wlvif->basic_rate =
5500 			wl1271_tx_min_rate_get(wl, wlvif->basic_rate_set);
5501 		ret = wl1271_acx_sta_rate_policies(wl, wlvif);
5502 
5503 		pm_runtime_put_autosuspend(wl->dev);
5504 	}
5505 out:
5506 	mutex_unlock(&wl->mutex);
5507 
5508 	return ret;
5509 }
5510 
5511 static void wl12xx_op_channel_switch(struct ieee80211_hw *hw,
5512 				     struct ieee80211_vif *vif,
5513 				     struct ieee80211_channel_switch *ch_switch)
5514 {
5515 	struct wl1271 *wl = hw->priv;
5516 	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5517 	int ret;
5518 
5519 	wl1271_debug(DEBUG_MAC80211, "mac80211 channel switch");
5520 
5521 	wl1271_tx_flush(wl);
5522 
5523 	mutex_lock(&wl->mutex);
5524 
5525 	if (unlikely(wl->state == WLCORE_STATE_OFF)) {
5526 		if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
5527 			ieee80211_chswitch_done(vif, false, 0);
5528 		goto out;
5529 	} else if (unlikely(wl->state != WLCORE_STATE_ON)) {
5530 		goto out;
5531 	}
5532 
5533 	ret = pm_runtime_resume_and_get(wl->dev);
5534 	if (ret < 0)
5535 		goto out;
5536 
5537 	/* TODO: change mac80211 to pass vif as param */
5538 
5539 	if (test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags)) {
5540 		unsigned long delay_usec;
5541 
5542 		ret = wl->ops->channel_switch(wl, wlvif, ch_switch);
5543 		if (ret)
5544 			goto out_sleep;
5545 
5546 		set_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags);
5547 
5548 		/* indicate failure 5 seconds after channel switch time */
5549 		delay_usec = ieee80211_tu_to_usec(wlvif->beacon_int) *
5550 			ch_switch->count;
5551 		ieee80211_queue_delayed_work(hw, &wlvif->channel_switch_work,
5552 					     usecs_to_jiffies(delay_usec) +
5553 					     msecs_to_jiffies(5000));
5554 	}
5555 
5556 out_sleep:
5557 	pm_runtime_put_autosuspend(wl->dev);
5558 
5559 out:
5560 	mutex_unlock(&wl->mutex);
5561 }
5562 
5563 static const void *wlcore_get_beacon_ie(struct wl1271 *wl,
5564 					struct wl12xx_vif *wlvif,
5565 					u8 eid)
5566 {
5567 	int ieoffset = offsetof(struct ieee80211_mgmt, u.beacon.variable);
5568 	struct sk_buff *beacon =
5569 		ieee80211_beacon_get(wl->hw, wl12xx_wlvif_to_vif(wlvif), 0);
5570 
5571 	if (!beacon)
5572 		return NULL;
5573 
5574 	return cfg80211_find_ie(eid,
5575 				beacon->data + ieoffset,
5576 				beacon->len - ieoffset);
5577 }
5578 
5579 static int wlcore_get_csa_count(struct wl1271 *wl, struct wl12xx_vif *wlvif,
5580 				u8 *csa_count)
5581 {
5582 	const u8 *ie;
5583 	const struct ieee80211_channel_sw_ie *ie_csa;
5584 
5585 	ie = wlcore_get_beacon_ie(wl, wlvif, WLAN_EID_CHANNEL_SWITCH);
5586 	if (!ie)
5587 		return -EINVAL;
5588 
5589 	ie_csa = (struct ieee80211_channel_sw_ie *)&ie[2];
5590 	*csa_count = ie_csa->count;
5591 
5592 	return 0;
5593 }
5594 
5595 static void wlcore_op_channel_switch_beacon(struct ieee80211_hw *hw,
5596 					    struct ieee80211_vif *vif,
5597 					    struct cfg80211_chan_def *chandef)
5598 {
5599 	struct wl1271 *wl = hw->priv;
5600 	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5601 	struct ieee80211_channel_switch ch_switch = {
5602 		.block_tx = true,
5603 		.chandef = *chandef,
5604 	};
5605 	int ret;
5606 
5607 	wl1271_debug(DEBUG_MAC80211,
5608 		     "mac80211 channel switch beacon (role %d)",
5609 		     wlvif->role_id);
5610 
5611 	ret = wlcore_get_csa_count(wl, wlvif, &ch_switch.count);
5612 	if (ret < 0) {
5613 		wl1271_error("error getting beacon (for CSA counter)");
5614 		return;
5615 	}
5616 
5617 	mutex_lock(&wl->mutex);
5618 
5619 	if (unlikely(wl->state != WLCORE_STATE_ON)) {
5620 		ret = -EBUSY;
5621 		goto out;
5622 	}
5623 
5624 	ret = pm_runtime_resume_and_get(wl->dev);
5625 	if (ret < 0)
5626 		goto out;
5627 
5628 	ret = wl->ops->channel_switch(wl, wlvif, &ch_switch);
5629 	if (ret)
5630 		goto out_sleep;
5631 
5632 	set_bit(WLVIF_FLAG_CS_PROGRESS, &wlvif->flags);
5633 
5634 out_sleep:
5635 	pm_runtime_put_autosuspend(wl->dev);
5636 out:
5637 	mutex_unlock(&wl->mutex);
5638 }
5639 
5640 static void wlcore_op_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5641 			    u32 queues, bool drop)
5642 {
5643 	struct wl1271 *wl = hw->priv;
5644 
5645 	wl1271_tx_flush(wl);
5646 }
5647 
5648 static int wlcore_op_remain_on_channel(struct ieee80211_hw *hw,
5649 				       struct ieee80211_vif *vif,
5650 				       struct ieee80211_channel *chan,
5651 				       int duration,
5652 				       enum ieee80211_roc_type type)
5653 {
5654 	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5655 	struct wl1271 *wl = hw->priv;
5656 	int channel, active_roc, ret = 0;
5657 
5658 	channel = ieee80211_frequency_to_channel(chan->center_freq);
5659 
5660 	wl1271_debug(DEBUG_MAC80211, "mac80211 roc %d (%d)",
5661 		     channel, wlvif->role_id);
5662 
5663 	mutex_lock(&wl->mutex);
5664 
5665 	if (unlikely(wl->state != WLCORE_STATE_ON))
5666 		goto out;
5667 
5668 	/* return EBUSY if we can't ROC right now */
5669 	active_roc = find_first_bit(wl->roc_map, WL12XX_MAX_ROLES);
5670 	if (wl->roc_vif || active_roc < WL12XX_MAX_ROLES) {
5671 		wl1271_warning("active roc on role %d", active_roc);
5672 		ret = -EBUSY;
5673 		goto out;
5674 	}
5675 
5676 	ret = pm_runtime_resume_and_get(wl->dev);
5677 	if (ret < 0)
5678 		goto out;
5679 
5680 	ret = wl12xx_start_dev(wl, wlvif, chan->band, channel);
5681 	if (ret < 0)
5682 		goto out_sleep;
5683 
5684 	wl->roc_vif = vif;
5685 	ieee80211_queue_delayed_work(hw, &wl->roc_complete_work,
5686 				     msecs_to_jiffies(duration));
5687 out_sleep:
5688 	pm_runtime_put_autosuspend(wl->dev);
5689 out:
5690 	mutex_unlock(&wl->mutex);
5691 	return ret;
5692 }
5693 
5694 static int __wlcore_roc_completed(struct wl1271 *wl)
5695 {
5696 	struct wl12xx_vif *wlvif;
5697 	int ret;
5698 
5699 	/* already completed */
5700 	if (unlikely(!wl->roc_vif))
5701 		return 0;
5702 
5703 	wlvif = wl12xx_vif_to_data(wl->roc_vif);
5704 
5705 	if (!test_bit(WLVIF_FLAG_INITIALIZED, &wlvif->flags))
5706 		return -EBUSY;
5707 
5708 	ret = wl12xx_stop_dev(wl, wlvif);
5709 	if (ret < 0)
5710 		return ret;
5711 
5712 	wl->roc_vif = NULL;
5713 
5714 	return 0;
5715 }
5716 
5717 static int wlcore_roc_completed(struct wl1271 *wl)
5718 {
5719 	int ret;
5720 
5721 	wl1271_debug(DEBUG_MAC80211, "roc complete");
5722 
5723 	mutex_lock(&wl->mutex);
5724 
5725 	if (unlikely(wl->state != WLCORE_STATE_ON)) {
5726 		ret = -EBUSY;
5727 		goto out;
5728 	}
5729 
5730 	ret = pm_runtime_resume_and_get(wl->dev);
5731 	if (ret < 0)
5732 		goto out;
5733 
5734 	ret = __wlcore_roc_completed(wl);
5735 
5736 	pm_runtime_put_autosuspend(wl->dev);
5737 out:
5738 	mutex_unlock(&wl->mutex);
5739 
5740 	return ret;
5741 }
5742 
5743 static void wlcore_roc_complete_work(struct work_struct *work)
5744 {
5745 	struct delayed_work *dwork;
5746 	struct wl1271 *wl;
5747 	int ret;
5748 
5749 	dwork = to_delayed_work(work);
5750 	wl = container_of(dwork, struct wl1271, roc_complete_work);
5751 
5752 	ret = wlcore_roc_completed(wl);
5753 	if (!ret)
5754 		ieee80211_remain_on_channel_expired(wl->hw);
5755 }
5756 
5757 static int wlcore_op_cancel_remain_on_channel(struct ieee80211_hw *hw,
5758 					      struct ieee80211_vif *vif)
5759 {
5760 	struct wl1271 *wl = hw->priv;
5761 
5762 	wl1271_debug(DEBUG_MAC80211, "mac80211 croc");
5763 
5764 	/* TODO: per-vif */
5765 	wl1271_tx_flush(wl);
5766 
5767 	/*
5768 	 * we can't just flush_work here, because it might deadlock
5769 	 * (as we might get called from the same workqueue)
5770 	 */
5771 	cancel_delayed_work_sync(&wl->roc_complete_work);
5772 	wlcore_roc_completed(wl);
5773 
5774 	return 0;
5775 }
5776 
5777 static void wlcore_op_sta_rc_update(struct ieee80211_hw *hw,
5778 				    struct ieee80211_vif *vif,
5779 				    struct ieee80211_link_sta *link_sta,
5780 				    u32 changed)
5781 {
5782 	struct ieee80211_sta *sta = link_sta->sta;
5783 	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5784 
5785 	wl1271_debug(DEBUG_MAC80211, "mac80211 sta_rc_update");
5786 
5787 	if (!(changed & IEEE80211_RC_BW_CHANGED))
5788 		return;
5789 
5790 	/* this callback is atomic, so schedule a new work */
5791 	wlvif->rc_update_bw = sta->deflink.bandwidth;
5792 	memcpy(&wlvif->rc_ht_cap, &sta->deflink.ht_cap,
5793 	       sizeof(sta->deflink.ht_cap));
5794 	ieee80211_queue_work(hw, &wlvif->rc_update_work);
5795 }
5796 
5797 static void wlcore_op_sta_statistics(struct ieee80211_hw *hw,
5798 				     struct ieee80211_vif *vif,
5799 				     struct ieee80211_sta *sta,
5800 				     struct station_info *sinfo)
5801 {
5802 	struct wl1271 *wl = hw->priv;
5803 	struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
5804 	s8 rssi_dbm;
5805 	int ret;
5806 
5807 	wl1271_debug(DEBUG_MAC80211, "mac80211 get_rssi");
5808 
5809 	mutex_lock(&wl->mutex);
5810 
5811 	if (unlikely(wl->state != WLCORE_STATE_ON))
5812 		goto out;
5813 
5814 	ret = pm_runtime_resume_and_get(wl->dev);
5815 	if (ret < 0)
5816 		goto out_sleep;
5817 
5818 	ret = wlcore_acx_average_rssi(wl, wlvif, &rssi_dbm);
5819 	if (ret < 0)
5820 		goto out_sleep;
5821 
5822 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
5823 	sinfo->signal = rssi_dbm;
5824 
5825 out_sleep:
5826 	pm_runtime_put_autosuspend(wl->dev);
5827 
5828 out:
5829 	mutex_unlock(&wl->mutex);
5830 }
5831 
5832 static u32 wlcore_op_get_expected_throughput(struct ieee80211_hw *hw,
5833 					     struct ieee80211_sta *sta)
5834 {
5835 	struct wl1271_station *wl_sta = (struct wl1271_station *)sta->drv_priv;
5836 	struct wl1271 *wl = hw->priv;
5837 	u8 hlid = wl_sta->hlid;
5838 
5839 	/* return in units of Kbps */
5840 	return (wl->links[hlid].fw_rate_mbps * 1000);
5841 }
5842 
5843 static bool wl1271_tx_frames_pending(struct ieee80211_hw *hw)
5844 {
5845 	struct wl1271 *wl = hw->priv;
5846 	bool ret = false;
5847 
5848 	mutex_lock(&wl->mutex);
5849 
5850 	if (unlikely(wl->state != WLCORE_STATE_ON))
5851 		goto out;
5852 
5853 	/* packets are considered pending if in the TX queue or the FW */
5854 	ret = (wl1271_tx_total_queue_count(wl) > 0) || (wl->tx_frames_cnt > 0);
5855 out:
5856 	mutex_unlock(&wl->mutex);
5857 
5858 	return ret;
5859 }
5860 
5861 /* can't be const, mac80211 writes to this */
5862 static struct ieee80211_rate wl1271_rates[] = {
5863 	{ .bitrate = 10,
5864 	  .hw_value = CONF_HW_BIT_RATE_1MBPS,
5865 	  .hw_value_short = CONF_HW_BIT_RATE_1MBPS, },
5866 	{ .bitrate = 20,
5867 	  .hw_value = CONF_HW_BIT_RATE_2MBPS,
5868 	  .hw_value_short = CONF_HW_BIT_RATE_2MBPS,
5869 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
5870 	{ .bitrate = 55,
5871 	  .hw_value = CONF_HW_BIT_RATE_5_5MBPS,
5872 	  .hw_value_short = CONF_HW_BIT_RATE_5_5MBPS,
5873 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
5874 	{ .bitrate = 110,
5875 	  .hw_value = CONF_HW_BIT_RATE_11MBPS,
5876 	  .hw_value_short = CONF_HW_BIT_RATE_11MBPS,
5877 	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
5878 	{ .bitrate = 60,
5879 	  .hw_value = CONF_HW_BIT_RATE_6MBPS,
5880 	  .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
5881 	{ .bitrate = 90,
5882 	  .hw_value = CONF_HW_BIT_RATE_9MBPS,
5883 	  .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
5884 	{ .bitrate = 120,
5885 	  .hw_value = CONF_HW_BIT_RATE_12MBPS,
5886 	  .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
5887 	{ .bitrate = 180,
5888 	  .hw_value = CONF_HW_BIT_RATE_18MBPS,
5889 	  .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
5890 	{ .bitrate = 240,
5891 	  .hw_value = CONF_HW_BIT_RATE_24MBPS,
5892 	  .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
5893 	{ .bitrate = 360,
5894 	 .hw_value = CONF_HW_BIT_RATE_36MBPS,
5895 	 .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
5896 	{ .bitrate = 480,
5897 	  .hw_value = CONF_HW_BIT_RATE_48MBPS,
5898 	  .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
5899 	{ .bitrate = 540,
5900 	  .hw_value = CONF_HW_BIT_RATE_54MBPS,
5901 	  .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
5902 };
5903 
5904 /* can't be const, mac80211 writes to this */
5905 static struct ieee80211_channel wl1271_channels[] = {
5906 	{ .hw_value = 1, .center_freq = 2412, .max_power = WLCORE_MAX_TXPWR },
5907 	{ .hw_value = 2, .center_freq = 2417, .max_power = WLCORE_MAX_TXPWR },
5908 	{ .hw_value = 3, .center_freq = 2422, .max_power = WLCORE_MAX_TXPWR },
5909 	{ .hw_value = 4, .center_freq = 2427, .max_power = WLCORE_MAX_TXPWR },
5910 	{ .hw_value = 5, .center_freq = 2432, .max_power = WLCORE_MAX_TXPWR },
5911 	{ .hw_value = 6, .center_freq = 2437, .max_power = WLCORE_MAX_TXPWR },
5912 	{ .hw_value = 7, .center_freq = 2442, .max_power = WLCORE_MAX_TXPWR },
5913 	{ .hw_value = 8, .center_freq = 2447, .max_power = WLCORE_MAX_TXPWR },
5914 	{ .hw_value = 9, .center_freq = 2452, .max_power = WLCORE_MAX_TXPWR },
5915 	{ .hw_value = 10, .center_freq = 2457, .max_power = WLCORE_MAX_TXPWR },
5916 	{ .hw_value = 11, .center_freq = 2462, .max_power = WLCORE_MAX_TXPWR },
5917 	{ .hw_value = 12, .center_freq = 2467, .max_power = WLCORE_MAX_TXPWR },
5918 	{ .hw_value = 13, .center_freq = 2472, .max_power = WLCORE_MAX_TXPWR },
5919 	{ .hw_value = 14, .center_freq = 2484, .max_power = WLCORE_MAX_TXPWR },
5920 };
5921 
5922 /* can't be const, mac80211 writes to this */
5923 static struct ieee80211_supported_band wl1271_band_2ghz = {
5924 	.channels = wl1271_channels,
5925 	.n_channels = ARRAY_SIZE(wl1271_channels),
5926 	.bitrates = wl1271_rates,
5927 	.n_bitrates = ARRAY_SIZE(wl1271_rates),
5928 };
5929 
5930 /* 5 GHz data rates for WL1273 */
5931 static struct ieee80211_rate wl1271_rates_5ghz[] = {
5932 	{ .bitrate = 60,
5933 	  .hw_value = CONF_HW_BIT_RATE_6MBPS,
5934 	  .hw_value_short = CONF_HW_BIT_RATE_6MBPS, },
5935 	{ .bitrate = 90,
5936 	  .hw_value = CONF_HW_BIT_RATE_9MBPS,
5937 	  .hw_value_short = CONF_HW_BIT_RATE_9MBPS, },
5938 	{ .bitrate = 120,
5939 	  .hw_value = CONF_HW_BIT_RATE_12MBPS,
5940 	  .hw_value_short = CONF_HW_BIT_RATE_12MBPS, },
5941 	{ .bitrate = 180,
5942 	  .hw_value = CONF_HW_BIT_RATE_18MBPS,
5943 	  .hw_value_short = CONF_HW_BIT_RATE_18MBPS, },
5944 	{ .bitrate = 240,
5945 	  .hw_value = CONF_HW_BIT_RATE_24MBPS,
5946 	  .hw_value_short = CONF_HW_BIT_RATE_24MBPS, },
5947 	{ .bitrate = 360,
5948 	 .hw_value = CONF_HW_BIT_RATE_36MBPS,
5949 	 .hw_value_short = CONF_HW_BIT_RATE_36MBPS, },
5950 	{ .bitrate = 480,
5951 	  .hw_value = CONF_HW_BIT_RATE_48MBPS,
5952 	  .hw_value_short = CONF_HW_BIT_RATE_48MBPS, },
5953 	{ .bitrate = 540,
5954 	  .hw_value = CONF_HW_BIT_RATE_54MBPS,
5955 	  .hw_value_short = CONF_HW_BIT_RATE_54MBPS, },
5956 };
5957 
5958 /* 5 GHz band channels for WL1273 */
5959 static struct ieee80211_channel wl1271_channels_5ghz[] = {
5960 	{ .hw_value = 8, .center_freq = 5040, .max_power = WLCORE_MAX_TXPWR },
5961 	{ .hw_value = 12, .center_freq = 5060, .max_power = WLCORE_MAX_TXPWR },
5962 	{ .hw_value = 16, .center_freq = 5080, .max_power = WLCORE_MAX_TXPWR },
5963 	{ .hw_value = 34, .center_freq = 5170, .max_power = WLCORE_MAX_TXPWR },
5964 	{ .hw_value = 36, .center_freq = 5180, .max_power = WLCORE_MAX_TXPWR },
5965 	{ .hw_value = 38, .center_freq = 5190, .max_power = WLCORE_MAX_TXPWR },
5966 	{ .hw_value = 40, .center_freq = 5200, .max_power = WLCORE_MAX_TXPWR },
5967 	{ .hw_value = 42, .center_freq = 5210, .max_power = WLCORE_MAX_TXPWR },
5968 	{ .hw_value = 44, .center_freq = 5220, .max_power = WLCORE_MAX_TXPWR },
5969 	{ .hw_value = 46, .center_freq = 5230, .max_power = WLCORE_MAX_TXPWR },
5970 	{ .hw_value = 48, .center_freq = 5240, .max_power = WLCORE_MAX_TXPWR },
5971 	{ .hw_value = 52, .center_freq = 5260, .max_power = WLCORE_MAX_TXPWR },
5972 	{ .hw_value = 56, .center_freq = 5280, .max_power = WLCORE_MAX_TXPWR },
5973 	{ .hw_value = 60, .center_freq = 5300, .max_power = WLCORE_MAX_TXPWR },
5974 	{ .hw_value = 64, .center_freq = 5320, .max_power = WLCORE_MAX_TXPWR },
5975 	{ .hw_value = 100, .center_freq = 5500, .max_power = WLCORE_MAX_TXPWR },
5976 	{ .hw_value = 104, .center_freq = 5520, .max_power = WLCORE_MAX_TXPWR },
5977 	{ .hw_value = 108, .center_freq = 5540, .max_power = WLCORE_MAX_TXPWR },
5978 	{ .hw_value = 112, .center_freq = 5560, .max_power = WLCORE_MAX_TXPWR },
5979 	{ .hw_value = 116, .center_freq = 5580, .max_power = WLCORE_MAX_TXPWR },
5980 	{ .hw_value = 120, .center_freq = 5600, .max_power = WLCORE_MAX_TXPWR },
5981 	{ .hw_value = 124, .center_freq = 5620, .max_power = WLCORE_MAX_TXPWR },
5982 	{ .hw_value = 128, .center_freq = 5640, .max_power = WLCORE_MAX_TXPWR },
5983 	{ .hw_value = 132, .center_freq = 5660, .max_power = WLCORE_MAX_TXPWR },
5984 	{ .hw_value = 136, .center_freq = 5680, .max_power = WLCORE_MAX_TXPWR },
5985 	{ .hw_value = 140, .center_freq = 5700, .max_power = WLCORE_MAX_TXPWR },
5986 	{ .hw_value = 149, .center_freq = 5745, .max_power = WLCORE_MAX_TXPWR },
5987 	{ .hw_value = 153, .center_freq = 5765, .max_power = WLCORE_MAX_TXPWR },
5988 	{ .hw_value = 157, .center_freq = 5785, .max_power = WLCORE_MAX_TXPWR },
5989 	{ .hw_value = 161, .center_freq = 5805, .max_power = WLCORE_MAX_TXPWR },
5990 	{ .hw_value = 165, .center_freq = 5825, .max_power = WLCORE_MAX_TXPWR },
5991 };
5992 
5993 static struct ieee80211_supported_band wl1271_band_5ghz = {
5994 	.channels = wl1271_channels_5ghz,
5995 	.n_channels = ARRAY_SIZE(wl1271_channels_5ghz),
5996 	.bitrates = wl1271_rates_5ghz,
5997 	.n_bitrates = ARRAY_SIZE(wl1271_rates_5ghz),
5998 };
5999 
6000 static const struct ieee80211_ops wl1271_ops = {
6001 	.start = wl1271_op_start,
6002 	.stop = wlcore_op_stop,
6003 	.add_interface = wl1271_op_add_interface,
6004 	.remove_interface = wl1271_op_remove_interface,
6005 	.change_interface = wl12xx_op_change_interface,
6006 #ifdef CONFIG_PM
6007 	.suspend = wl1271_op_suspend,
6008 	.resume = wl1271_op_resume,
6009 #endif
6010 	.config = wl1271_op_config,
6011 	.prepare_multicast = wl1271_op_prepare_multicast,
6012 	.configure_filter = wl1271_op_configure_filter,
6013 	.tx = wl1271_op_tx,
6014 	.wake_tx_queue = ieee80211_handle_wake_tx_queue,
6015 	.set_key = wlcore_op_set_key,
6016 	.hw_scan = wl1271_op_hw_scan,
6017 	.cancel_hw_scan = wl1271_op_cancel_hw_scan,
6018 	.sched_scan_start = wl1271_op_sched_scan_start,
6019 	.sched_scan_stop = wl1271_op_sched_scan_stop,
6020 	.bss_info_changed = wl1271_op_bss_info_changed,
6021 	.set_frag_threshold = wl1271_op_set_frag_threshold,
6022 	.set_rts_threshold = wl1271_op_set_rts_threshold,
6023 	.conf_tx = wl1271_op_conf_tx,
6024 	.get_tsf = wl1271_op_get_tsf,
6025 	.get_survey = wl1271_op_get_survey,
6026 	.sta_state = wl12xx_op_sta_state,
6027 	.ampdu_action = wl1271_op_ampdu_action,
6028 	.tx_frames_pending = wl1271_tx_frames_pending,
6029 	.set_bitrate_mask = wl12xx_set_bitrate_mask,
6030 	.set_default_unicast_key = wl1271_op_set_default_key_idx,
6031 	.channel_switch = wl12xx_op_channel_switch,
6032 	.channel_switch_beacon = wlcore_op_channel_switch_beacon,
6033 	.flush = wlcore_op_flush,
6034 	.remain_on_channel = wlcore_op_remain_on_channel,
6035 	.cancel_remain_on_channel = wlcore_op_cancel_remain_on_channel,
6036 	.add_chanctx = wlcore_op_add_chanctx,
6037 	.remove_chanctx = wlcore_op_remove_chanctx,
6038 	.change_chanctx = wlcore_op_change_chanctx,
6039 	.assign_vif_chanctx = wlcore_op_assign_vif_chanctx,
6040 	.unassign_vif_chanctx = wlcore_op_unassign_vif_chanctx,
6041 	.switch_vif_chanctx = wlcore_op_switch_vif_chanctx,
6042 	.link_sta_rc_update = wlcore_op_sta_rc_update,
6043 	.sta_statistics = wlcore_op_sta_statistics,
6044 	.get_expected_throughput = wlcore_op_get_expected_throughput,
6045 	CFG80211_TESTMODE_CMD(wl1271_tm_cmd)
6046 };
6047 
6048 
6049 u8 wlcore_rate_to_idx(struct wl1271 *wl, u8 rate, enum nl80211_band band)
6050 {
6051 	u8 idx;
6052 
6053 	BUG_ON(band >= 2);
6054 
6055 	if (unlikely(rate >= wl->hw_tx_rate_tbl_size)) {
6056 		wl1271_error("Illegal RX rate from HW: %d", rate);
6057 		return 0;
6058 	}
6059 
6060 	idx = wl->band_rate_to_idx[band][rate];
6061 	if (unlikely(idx == CONF_HW_RXTX_RATE_UNSUPPORTED)) {
6062 		wl1271_error("Unsupported RX rate from HW: %d", rate);
6063 		return 0;
6064 	}
6065 
6066 	return idx;
6067 }
6068 
6069 static void wl12xx_derive_mac_addresses(struct wl1271 *wl, u32 oui, u32 nic)
6070 {
6071 	int i;
6072 
6073 	wl1271_debug(DEBUG_PROBE, "base address: oui %06x nic %06x",
6074 		     oui, nic);
6075 
6076 	if (nic + WLCORE_NUM_MAC_ADDRESSES - wl->num_mac_addr > 0xffffff)
6077 		wl1271_warning("NIC part of the MAC address wraps around!");
6078 
6079 	for (i = 0; i < wl->num_mac_addr; i++) {
6080 		wl->addresses[i].addr[0] = (u8)(oui >> 16);
6081 		wl->addresses[i].addr[1] = (u8)(oui >> 8);
6082 		wl->addresses[i].addr[2] = (u8) oui;
6083 		wl->addresses[i].addr[3] = (u8)(nic >> 16);
6084 		wl->addresses[i].addr[4] = (u8)(nic >> 8);
6085 		wl->addresses[i].addr[5] = (u8) nic;
6086 		nic++;
6087 	}
6088 
6089 	/* we may be one address short at the most */
6090 	WARN_ON(wl->num_mac_addr + 1 < WLCORE_NUM_MAC_ADDRESSES);
6091 
6092 	/*
6093 	 * turn on the LAA bit in the first address and use it as
6094 	 * the last address.
6095 	 */
6096 	if (wl->num_mac_addr < WLCORE_NUM_MAC_ADDRESSES) {
6097 		int idx = WLCORE_NUM_MAC_ADDRESSES - 1;
6098 		memcpy(&wl->addresses[idx], &wl->addresses[0],
6099 		       sizeof(wl->addresses[0]));
6100 		/* LAA bit */
6101 		wl->addresses[idx].addr[0] |= BIT(1);
6102 	}
6103 
6104 	wl->hw->wiphy->n_addresses = WLCORE_NUM_MAC_ADDRESSES;
6105 	wl->hw->wiphy->addresses = wl->addresses;
6106 }
6107 
6108 static int wl12xx_get_hw_info(struct wl1271 *wl)
6109 {
6110 	int ret;
6111 
6112 	ret = wlcore_read_reg(wl, REG_CHIP_ID_B, &wl->chip.id);
6113 	if (ret < 0)
6114 		goto out;
6115 
6116 	wl->fuse_oui_addr = 0;
6117 	wl->fuse_nic_addr = 0;
6118 
6119 	ret = wl->ops->get_pg_ver(wl, &wl->hw_pg_ver);
6120 	if (ret < 0)
6121 		goto out;
6122 
6123 	if (wl->ops->get_mac)
6124 		ret = wl->ops->get_mac(wl);
6125 
6126 out:
6127 	return ret;
6128 }
6129 
6130 static int wl1271_register_hw(struct wl1271 *wl)
6131 {
6132 	int ret;
6133 	u32 oui_addr = 0, nic_addr = 0;
6134 	struct platform_device *pdev = wl->pdev;
6135 	struct wlcore_platdev_data *pdev_data = dev_get_platdata(&pdev->dev);
6136 
6137 	if (wl->mac80211_registered)
6138 		return 0;
6139 
6140 	if (wl->nvs_len >= 12) {
6141 		/* NOTE: The wl->nvs->nvs element must be first, in
6142 		 * order to simplify the casting, we assume it is at
6143 		 * the beginning of the wl->nvs structure.
6144 		 */
6145 		u8 *nvs_ptr = (u8 *)wl->nvs;
6146 
6147 		oui_addr =
6148 			(nvs_ptr[11] << 16) + (nvs_ptr[10] << 8) + nvs_ptr[6];
6149 		nic_addr =
6150 			(nvs_ptr[5] << 16) + (nvs_ptr[4] << 8) + nvs_ptr[3];
6151 	}
6152 
6153 	/* if the MAC address is zeroed in the NVS derive from fuse */
6154 	if (oui_addr == 0 && nic_addr == 0) {
6155 		oui_addr = wl->fuse_oui_addr;
6156 		/* fuse has the BD_ADDR, the WLAN addresses are the next two */
6157 		nic_addr = wl->fuse_nic_addr + 1;
6158 	}
6159 
6160 	if (oui_addr == 0xdeadbe && nic_addr == 0xef0000) {
6161 		wl1271_warning("Detected unconfigured mac address in nvs, derive from fuse instead.");
6162 		if (!strcmp(pdev_data->family->name, "wl18xx")) {
6163 			wl1271_warning("This default nvs file can be removed from the file system");
6164 		} else {
6165 			wl1271_warning("Your device performance is not optimized.");
6166 			wl1271_warning("Please use the calibrator tool to configure your device.");
6167 		}
6168 
6169 		if (wl->fuse_oui_addr == 0 && wl->fuse_nic_addr == 0) {
6170 			wl1271_warning("Fuse mac address is zero. using random mac");
6171 			/* Use TI oui and a random nic */
6172 			oui_addr = WLCORE_TI_OUI_ADDRESS;
6173 			nic_addr = get_random_u32();
6174 		} else {
6175 			oui_addr = wl->fuse_oui_addr;
6176 			/* fuse has the BD_ADDR, the WLAN addresses are the next two */
6177 			nic_addr = wl->fuse_nic_addr + 1;
6178 		}
6179 	}
6180 
6181 	wl12xx_derive_mac_addresses(wl, oui_addr, nic_addr);
6182 
6183 	ret = ieee80211_register_hw(wl->hw);
6184 	if (ret < 0) {
6185 		wl1271_error("unable to register mac80211 hw: %d", ret);
6186 		goto out;
6187 	}
6188 
6189 	wl->mac80211_registered = true;
6190 
6191 	wl1271_debugfs_init(wl);
6192 
6193 	wl1271_notice("loaded");
6194 
6195 out:
6196 	return ret;
6197 }
6198 
6199 static void wl1271_unregister_hw(struct wl1271 *wl)
6200 {
6201 	if (wl->plt)
6202 		wl1271_plt_stop(wl);
6203 
6204 	ieee80211_unregister_hw(wl->hw);
6205 	wl->mac80211_registered = false;
6206 
6207 }
6208 
6209 static int wl1271_init_ieee80211(struct wl1271 *wl)
6210 {
6211 	int i;
6212 
6213 	/* The tx descriptor buffer */
6214 	wl->hw->extra_tx_headroom = sizeof(struct wl1271_tx_hw_descr);
6215 
6216 	if (wl->quirks & WLCORE_QUIRK_TKIP_HEADER_SPACE)
6217 		wl->hw->extra_tx_headroom += WL1271_EXTRA_SPACE_TKIP;
6218 
6219 	/* unit us */
6220 	/* FIXME: find a proper value */
6221 	wl->hw->max_listen_interval = wl->conf.conn.max_listen_interval;
6222 
6223 	ieee80211_hw_set(wl->hw, SUPPORT_FAST_XMIT);
6224 	ieee80211_hw_set(wl->hw, CHANCTX_STA_CSA);
6225 	ieee80211_hw_set(wl->hw, SUPPORTS_PER_STA_GTK);
6226 	ieee80211_hw_set(wl->hw, QUEUE_CONTROL);
6227 	ieee80211_hw_set(wl->hw, TX_AMPDU_SETUP_IN_HW);
6228 	ieee80211_hw_set(wl->hw, AMPDU_AGGREGATION);
6229 	ieee80211_hw_set(wl->hw, AP_LINK_PS);
6230 	ieee80211_hw_set(wl->hw, SPECTRUM_MGMT);
6231 	ieee80211_hw_set(wl->hw, REPORTS_TX_ACK_STATUS);
6232 	ieee80211_hw_set(wl->hw, CONNECTION_MONITOR);
6233 	ieee80211_hw_set(wl->hw, HAS_RATE_CONTROL);
6234 	ieee80211_hw_set(wl->hw, SUPPORTS_DYNAMIC_PS);
6235 	ieee80211_hw_set(wl->hw, SIGNAL_DBM);
6236 	ieee80211_hw_set(wl->hw, SUPPORTS_PS);
6237 	ieee80211_hw_set(wl->hw, SUPPORTS_TX_FRAG);
6238 
6239 	wl->hw->wiphy->cipher_suites = cipher_suites;
6240 	wl->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
6241 
6242 	wl->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
6243 					 BIT(NL80211_IFTYPE_AP) |
6244 					 BIT(NL80211_IFTYPE_P2P_DEVICE) |
6245 					 BIT(NL80211_IFTYPE_P2P_CLIENT) |
6246 #ifdef CONFIG_MAC80211_MESH
6247 					 BIT(NL80211_IFTYPE_MESH_POINT) |
6248 #endif
6249 					 BIT(NL80211_IFTYPE_P2P_GO);
6250 
6251 	wl->hw->wiphy->max_scan_ssids = 1;
6252 	wl->hw->wiphy->max_sched_scan_ssids = 16;
6253 	wl->hw->wiphy->max_match_sets = 16;
6254 	/*
6255 	 * Maximum length of elements in scanning probe request templates
6256 	 * should be the maximum length possible for a template, without
6257 	 * the IEEE80211 header of the template
6258 	 */
6259 	wl->hw->wiphy->max_scan_ie_len = WL1271_CMD_TEMPL_MAX_SIZE -
6260 			sizeof(struct ieee80211_header);
6261 
6262 	wl->hw->wiphy->max_sched_scan_reqs = 1;
6263 	wl->hw->wiphy->max_sched_scan_ie_len = WL1271_CMD_TEMPL_MAX_SIZE -
6264 		sizeof(struct ieee80211_header);
6265 
6266 	wl->hw->wiphy->max_remain_on_channel_duration = 30000;
6267 
6268 	wl->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD |
6269 				WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL |
6270 				WIPHY_FLAG_HAS_CHANNEL_SWITCH |
6271 				WIPHY_FLAG_IBSS_RSN;
6272 
6273 	wl->hw->wiphy->features |= NL80211_FEATURE_AP_SCAN;
6274 
6275 	/* make sure all our channels fit in the scanned_ch bitmask */
6276 	BUILD_BUG_ON(ARRAY_SIZE(wl1271_channels) +
6277 		     ARRAY_SIZE(wl1271_channels_5ghz) >
6278 		     WL1271_MAX_CHANNELS);
6279 	/*
6280 	* clear channel flags from the previous usage
6281 	* and restore max_power & max_antenna_gain values.
6282 	*/
6283 	for (i = 0; i < ARRAY_SIZE(wl1271_channels); i++) {
6284 		wl1271_band_2ghz.channels[i].flags = 0;
6285 		wl1271_band_2ghz.channels[i].max_power = WLCORE_MAX_TXPWR;
6286 		wl1271_band_2ghz.channels[i].max_antenna_gain = 0;
6287 	}
6288 
6289 	for (i = 0; i < ARRAY_SIZE(wl1271_channels_5ghz); i++) {
6290 		wl1271_band_5ghz.channels[i].flags = 0;
6291 		wl1271_band_5ghz.channels[i].max_power = WLCORE_MAX_TXPWR;
6292 		wl1271_band_5ghz.channels[i].max_antenna_gain = 0;
6293 	}
6294 
6295 	/*
6296 	 * We keep local copies of the band structs because we need to
6297 	 * modify them on a per-device basis.
6298 	 */
6299 	memcpy(&wl->bands[NL80211_BAND_2GHZ], &wl1271_band_2ghz,
6300 	       sizeof(wl1271_band_2ghz));
6301 	memcpy(&wl->bands[NL80211_BAND_2GHZ].ht_cap,
6302 	       &wl->ht_cap[NL80211_BAND_2GHZ],
6303 	       sizeof(*wl->ht_cap));
6304 	memcpy(&wl->bands[NL80211_BAND_5GHZ], &wl1271_band_5ghz,
6305 	       sizeof(wl1271_band_5ghz));
6306 	memcpy(&wl->bands[NL80211_BAND_5GHZ].ht_cap,
6307 	       &wl->ht_cap[NL80211_BAND_5GHZ],
6308 	       sizeof(*wl->ht_cap));
6309 
6310 	wl->hw->wiphy->bands[NL80211_BAND_2GHZ] =
6311 		&wl->bands[NL80211_BAND_2GHZ];
6312 	wl->hw->wiphy->bands[NL80211_BAND_5GHZ] =
6313 		&wl->bands[NL80211_BAND_5GHZ];
6314 
6315 	/*
6316 	 * allow 4 queues per mac address we support +
6317 	 * 1 cab queue per mac + one global offchannel Tx queue
6318 	 */
6319 	wl->hw->queues = (NUM_TX_QUEUES + 1) * WLCORE_NUM_MAC_ADDRESSES + 1;
6320 
6321 	/* the last queue is the offchannel queue */
6322 	wl->hw->offchannel_tx_hw_queue = wl->hw->queues - 1;
6323 	wl->hw->max_rates = 1;
6324 
6325 	wl->hw->wiphy->reg_notifier = wl1271_reg_notify;
6326 
6327 	/* the FW answers probe-requests in AP-mode */
6328 	wl->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
6329 	wl->hw->wiphy->probe_resp_offload =
6330 		NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
6331 		NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
6332 		NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
6333 
6334 	/* allowed interface combinations */
6335 	wl->hw->wiphy->iface_combinations = wl->iface_combinations;
6336 	wl->hw->wiphy->n_iface_combinations = wl->n_iface_combinations;
6337 
6338 	/* register vendor commands */
6339 	wlcore_set_vendor_commands(wl->hw->wiphy);
6340 
6341 	SET_IEEE80211_DEV(wl->hw, wl->dev);
6342 
6343 	wl->hw->sta_data_size = sizeof(struct wl1271_station);
6344 	wl->hw->vif_data_size = sizeof(struct wl12xx_vif);
6345 
6346 	wl->hw->max_rx_aggregation_subframes = wl->conf.ht.rx_ba_win_size;
6347 
6348 	return 0;
6349 }
6350 
6351 struct ieee80211_hw *wlcore_alloc_hw(size_t priv_size, u32 aggr_buf_size,
6352 				     u32 mbox_size)
6353 {
6354 	struct ieee80211_hw *hw;
6355 	struct wl1271 *wl;
6356 	int i, j, ret;
6357 	unsigned int order;
6358 
6359 	hw = ieee80211_alloc_hw(sizeof(*wl), &wl1271_ops);
6360 	if (!hw) {
6361 		wl1271_error("could not alloc ieee80211_hw");
6362 		ret = -ENOMEM;
6363 		goto err_hw_alloc;
6364 	}
6365 
6366 	wl = hw->priv;
6367 	memset(wl, 0, sizeof(*wl));
6368 
6369 	wl->priv = kzalloc(priv_size, GFP_KERNEL);
6370 	if (!wl->priv) {
6371 		wl1271_error("could not alloc wl priv");
6372 		ret = -ENOMEM;
6373 		goto err_priv_alloc;
6374 	}
6375 
6376 	INIT_LIST_HEAD(&wl->wlvif_list);
6377 
6378 	wl->hw = hw;
6379 
6380 	/*
6381 	 * wl->num_links is not configured yet, so just use WLCORE_MAX_LINKS.
6382 	 * we don't allocate any additional resource here, so that's fine.
6383 	 */
6384 	for (i = 0; i < NUM_TX_QUEUES; i++)
6385 		for (j = 0; j < WLCORE_MAX_LINKS; j++)
6386 			skb_queue_head_init(&wl->links[j].tx_queue[i]);
6387 
6388 	skb_queue_head_init(&wl->deferred_rx_queue);
6389 	skb_queue_head_init(&wl->deferred_tx_queue);
6390 
6391 	INIT_WORK(&wl->netstack_work, wl1271_netstack_work);
6392 	INIT_WORK(&wl->tx_work, wl1271_tx_work);
6393 	INIT_WORK(&wl->recovery_work, wl1271_recovery_work);
6394 	INIT_DELAYED_WORK(&wl->scan_complete_work, wl1271_scan_complete_work);
6395 	INIT_DELAYED_WORK(&wl->roc_complete_work, wlcore_roc_complete_work);
6396 	INIT_DELAYED_WORK(&wl->tx_watchdog_work, wl12xx_tx_watchdog_work);
6397 
6398 	wl->freezable_wq = create_freezable_workqueue("wl12xx_wq");
6399 	if (!wl->freezable_wq) {
6400 		ret = -ENOMEM;
6401 		goto err_hw;
6402 	}
6403 
6404 	wl->channel = 0;
6405 	wl->rx_counter = 0;
6406 	wl->power_level = WL1271_DEFAULT_POWER_LEVEL;
6407 	wl->band = NL80211_BAND_2GHZ;
6408 	wl->channel_type = NL80211_CHAN_NO_HT;
6409 	wl->flags = 0;
6410 	wl->sg_enabled = true;
6411 	wl->sleep_auth = WL1271_PSM_ILLEGAL;
6412 	wl->recovery_count = 0;
6413 	wl->hw_pg_ver = -1;
6414 	wl->ap_ps_map = 0;
6415 	wl->ap_fw_ps_map = 0;
6416 	wl->quirks = 0;
6417 	wl->system_hlid = WL12XX_SYSTEM_HLID;
6418 	wl->active_sta_count = 0;
6419 	wl->active_link_count = 0;
6420 	wl->fwlog_size = 0;
6421 
6422 	/* The system link is always allocated */
6423 	__set_bit(WL12XX_SYSTEM_HLID, wl->links_map);
6424 
6425 	memset(wl->tx_frames_map, 0, sizeof(wl->tx_frames_map));
6426 	for (i = 0; i < wl->num_tx_desc; i++)
6427 		wl->tx_frames[i] = NULL;
6428 
6429 	spin_lock_init(&wl->wl_lock);
6430 
6431 	wl->state = WLCORE_STATE_OFF;
6432 	wl->fw_type = WL12XX_FW_TYPE_NONE;
6433 	mutex_init(&wl->mutex);
6434 	mutex_init(&wl->flush_mutex);
6435 	init_completion(&wl->nvs_loading_complete);
6436 
6437 	order = get_order(aggr_buf_size);
6438 	wl->aggr_buf = (u8 *)__get_free_pages(GFP_KERNEL, order);
6439 	if (!wl->aggr_buf) {
6440 		ret = -ENOMEM;
6441 		goto err_wq;
6442 	}
6443 	wl->aggr_buf_size = aggr_buf_size;
6444 
6445 	wl->dummy_packet = wl12xx_alloc_dummy_packet(wl);
6446 	if (!wl->dummy_packet) {
6447 		ret = -ENOMEM;
6448 		goto err_aggr;
6449 	}
6450 
6451 	/* Allocate one page for the FW log */
6452 	wl->fwlog = (u8 *)get_zeroed_page(GFP_KERNEL);
6453 	if (!wl->fwlog) {
6454 		ret = -ENOMEM;
6455 		goto err_dummy_packet;
6456 	}
6457 
6458 	wl->mbox_size = mbox_size;
6459 	wl->mbox = kmalloc(wl->mbox_size, GFP_KERNEL | GFP_DMA);
6460 	if (!wl->mbox) {
6461 		ret = -ENOMEM;
6462 		goto err_fwlog;
6463 	}
6464 
6465 	wl->buffer_32 = kmalloc_obj(*wl->buffer_32);
6466 	if (!wl->buffer_32) {
6467 		ret = -ENOMEM;
6468 		goto err_mbox;
6469 	}
6470 
6471 	return hw;
6472 
6473 err_mbox:
6474 	kfree(wl->mbox);
6475 
6476 err_fwlog:
6477 	free_page((unsigned long)wl->fwlog);
6478 
6479 err_dummy_packet:
6480 	dev_kfree_skb(wl->dummy_packet);
6481 
6482 err_aggr:
6483 	free_pages((unsigned long)wl->aggr_buf, order);
6484 
6485 err_wq:
6486 	destroy_workqueue(wl->freezable_wq);
6487 
6488 err_hw:
6489 	wl1271_debugfs_exit(wl);
6490 	kfree(wl->priv);
6491 
6492 err_priv_alloc:
6493 	ieee80211_free_hw(hw);
6494 
6495 err_hw_alloc:
6496 
6497 	return ERR_PTR(ret);
6498 }
6499 EXPORT_SYMBOL_GPL(wlcore_alloc_hw);
6500 
6501 int wlcore_free_hw(struct wl1271 *wl)
6502 {
6503 	/* Unblock any fwlog readers */
6504 	mutex_lock(&wl->mutex);
6505 	wl->fwlog_size = -1;
6506 	mutex_unlock(&wl->mutex);
6507 
6508 	wlcore_sysfs_free(wl);
6509 
6510 	kfree(wl->buffer_32);
6511 	kfree(wl->mbox);
6512 	free_page((unsigned long)wl->fwlog);
6513 	dev_kfree_skb(wl->dummy_packet);
6514 	free_pages((unsigned long)wl->aggr_buf, get_order(wl->aggr_buf_size));
6515 
6516 	wl1271_debugfs_exit(wl);
6517 
6518 	vfree(wl->fw);
6519 	wl->fw = NULL;
6520 	wl->fw_type = WL12XX_FW_TYPE_NONE;
6521 	kfree(wl->nvs);
6522 	wl->nvs = NULL;
6523 
6524 	kfree(wl->raw_fw_status);
6525 	kfree(wl->fw_status);
6526 	kfree(wl->tx_res_if);
6527 	destroy_workqueue(wl->freezable_wq);
6528 
6529 	kfree(wl->priv);
6530 	ieee80211_free_hw(wl->hw);
6531 
6532 	return 0;
6533 }
6534 EXPORT_SYMBOL_GPL(wlcore_free_hw);
6535 
6536 #ifdef CONFIG_PM
6537 static const struct wiphy_wowlan_support wlcore_wowlan_support = {
6538 	.flags = WIPHY_WOWLAN_ANY,
6539 	.n_patterns = WL1271_MAX_RX_FILTERS,
6540 	.pattern_min_len = 1,
6541 	.pattern_max_len = WL1271_RX_FILTER_MAX_PATTERN_SIZE,
6542 };
6543 #endif
6544 
6545 static irqreturn_t wlcore_hardirq(int irq, void *cookie)
6546 {
6547 	return IRQ_WAKE_THREAD;
6548 }
6549 
6550 static void wlcore_nvs_cb(const struct firmware *fw, void *context)
6551 {
6552 	struct wl1271 *wl = context;
6553 	struct platform_device *pdev = wl->pdev;
6554 	struct wlcore_platdev_data *pdev_data = dev_get_platdata(&pdev->dev);
6555 	struct resource *res;
6556 
6557 	int ret;
6558 	irq_handler_t hardirq_fn = NULL;
6559 
6560 	if (fw) {
6561 		wl->nvs = kmemdup(fw->data, fw->size, GFP_KERNEL);
6562 		if (!wl->nvs) {
6563 			wl1271_error("Could not allocate nvs data");
6564 			goto out;
6565 		}
6566 		wl->nvs_len = fw->size;
6567 	} else if (pdev_data->family->nvs_name) {
6568 		wl1271_debug(DEBUG_BOOT, "Could not get nvs file %s",
6569 			     pdev_data->family->nvs_name);
6570 		wl->nvs = NULL;
6571 		wl->nvs_len = 0;
6572 	} else {
6573 		wl->nvs = NULL;
6574 		wl->nvs_len = 0;
6575 	}
6576 
6577 	ret = wl->ops->setup(wl);
6578 	if (ret < 0)
6579 		goto out_free_nvs;
6580 
6581 	BUG_ON(wl->num_tx_desc > WLCORE_MAX_TX_DESCRIPTORS);
6582 
6583 	/* adjust some runtime configuration parameters */
6584 	wlcore_adjust_conf(wl);
6585 
6586 	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
6587 	if (!res) {
6588 		wl1271_error("Could not get IRQ resource");
6589 		goto out_free_nvs;
6590 	}
6591 
6592 	wl->irq = res->start;
6593 	wl->irq_flags = res->flags & IRQF_TRIGGER_MASK;
6594 	wl->if_ops = pdev_data->if_ops;
6595 
6596 	if (wl->irq_flags & (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING))
6597 		hardirq_fn = wlcore_hardirq;
6598 	else
6599 		wl->irq_flags |= IRQF_ONESHOT;
6600 
6601 	ret = wl12xx_set_power_on(wl);
6602 	if (ret < 0)
6603 		goto out_free_nvs;
6604 
6605 	ret = wl12xx_get_hw_info(wl);
6606 	if (ret < 0) {
6607 		wl1271_error("couldn't get hw info");
6608 		wl1271_power_off(wl);
6609 		goto out_free_nvs;
6610 	}
6611 
6612 	ret = request_threaded_irq(wl->irq, hardirq_fn, wlcore_irq,
6613 				   wl->irq_flags, pdev->name, wl);
6614 	if (ret < 0) {
6615 		wl1271_error("interrupt configuration failed");
6616 		wl1271_power_off(wl);
6617 		goto out_free_nvs;
6618 	}
6619 
6620 #ifdef CONFIG_PM
6621 	device_init_wakeup(wl->dev, true);
6622 
6623 	ret = enable_irq_wake(wl->irq);
6624 	if (!ret) {
6625 		wl->irq_wake_enabled = true;
6626 		if (pdev_data->pwr_in_suspend)
6627 			wl->hw->wiphy->wowlan = &wlcore_wowlan_support;
6628 	}
6629 
6630 	res = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
6631 	if (res) {
6632 		wl->wakeirq = res->start;
6633 		wl->wakeirq_flags = res->flags & IRQF_TRIGGER_MASK;
6634 		ret = dev_pm_set_dedicated_wake_irq(wl->dev, wl->wakeirq);
6635 		if (ret)
6636 			wl->wakeirq = -ENODEV;
6637 	} else {
6638 		wl->wakeirq = -ENODEV;
6639 	}
6640 #endif
6641 	disable_irq(wl->irq);
6642 	wl1271_power_off(wl);
6643 
6644 	ret = wl->ops->identify_chip(wl);
6645 	if (ret < 0)
6646 		goto out_irq;
6647 
6648 	ret = wl1271_init_ieee80211(wl);
6649 	if (ret)
6650 		goto out_irq;
6651 
6652 	ret = wl1271_register_hw(wl);
6653 	if (ret)
6654 		goto out_irq;
6655 
6656 	ret = wlcore_sysfs_init(wl);
6657 	if (ret)
6658 		goto out_unreg;
6659 
6660 	wl->initialized = true;
6661 	goto out;
6662 
6663 out_unreg:
6664 	wl1271_unregister_hw(wl);
6665 
6666 out_irq:
6667 	if (wl->wakeirq >= 0)
6668 		dev_pm_clear_wake_irq(wl->dev);
6669 	device_init_wakeup(wl->dev, false);
6670 	free_irq(wl->irq, wl);
6671 
6672 out_free_nvs:
6673 	kfree(wl->nvs);
6674 
6675 out:
6676 	release_firmware(fw);
6677 	complete_all(&wl->nvs_loading_complete);
6678 }
6679 
6680 static int __maybe_unused wlcore_runtime_suspend(struct device *dev)
6681 {
6682 	struct wl1271 *wl = dev_get_drvdata(dev);
6683 	struct wl12xx_vif *wlvif;
6684 	int error;
6685 
6686 	/* We do not enter elp sleep in PLT mode */
6687 	if (wl->plt)
6688 		return 0;
6689 
6690 	/* Nothing to do if no ELP mode requested */
6691 	if (wl->sleep_auth != WL1271_PSM_ELP)
6692 		return 0;
6693 
6694 	wl12xx_for_each_wlvif(wl, wlvif) {
6695 		if (!test_bit(WLVIF_FLAG_IN_PS, &wlvif->flags) &&
6696 		    test_bit(WLVIF_FLAG_IN_USE, &wlvif->flags))
6697 			return -EBUSY;
6698 	}
6699 
6700 	wl1271_debug(DEBUG_PSM, "chip to elp");
6701 	error = wlcore_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG, ELPCTRL_SLEEP);
6702 	if (error < 0) {
6703 		wl12xx_queue_recovery_work(wl);
6704 
6705 		return error;
6706 	}
6707 
6708 	set_bit(WL1271_FLAG_IN_ELP, &wl->flags);
6709 
6710 	return 0;
6711 }
6712 
6713 static int __maybe_unused wlcore_runtime_resume(struct device *dev)
6714 {
6715 	struct wl1271 *wl = dev_get_drvdata(dev);
6716 	DECLARE_COMPLETION_ONSTACK(compl);
6717 	unsigned long flags;
6718 	int ret;
6719 	unsigned long start_time = jiffies;
6720 	bool recovery = false;
6721 
6722 	/* Nothing to do if no ELP mode requested */
6723 	if (!test_bit(WL1271_FLAG_IN_ELP, &wl->flags))
6724 		return 0;
6725 
6726 	wl1271_debug(DEBUG_PSM, "waking up chip from elp");
6727 
6728 	spin_lock_irqsave(&wl->wl_lock, flags);
6729 	wl->elp_compl = &compl;
6730 	spin_unlock_irqrestore(&wl->wl_lock, flags);
6731 
6732 	ret = wlcore_raw_write32(wl, HW_ACCESS_ELP_CTRL_REG, ELPCTRL_WAKE_UP);
6733 	if (ret < 0) {
6734 		recovery = true;
6735 	} else if (!test_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags)) {
6736 		ret = wait_for_completion_timeout(&compl,
6737 			msecs_to_jiffies(WL1271_WAKEUP_TIMEOUT));
6738 		if (ret == 0) {
6739 			wl1271_warning("ELP wakeup timeout!");
6740 			recovery = true;
6741 		}
6742 	}
6743 
6744 	spin_lock_irqsave(&wl->wl_lock, flags);
6745 	wl->elp_compl = NULL;
6746 	spin_unlock_irqrestore(&wl->wl_lock, flags);
6747 	clear_bit(WL1271_FLAG_IN_ELP, &wl->flags);
6748 
6749 	if (recovery) {
6750 		set_bit(WL1271_FLAG_INTENDED_FW_RECOVERY, &wl->flags);
6751 		wl12xx_queue_recovery_work(wl);
6752 	} else {
6753 		wl1271_debug(DEBUG_PSM, "wakeup time: %u ms",
6754 			     jiffies_to_msecs(jiffies - start_time));
6755 	}
6756 
6757 	return 0;
6758 }
6759 
6760 static const struct dev_pm_ops wlcore_pm_ops = {
6761 	SET_RUNTIME_PM_OPS(wlcore_runtime_suspend,
6762 			   wlcore_runtime_resume,
6763 			   NULL)
6764 };
6765 
6766 int wlcore_probe(struct wl1271 *wl, struct platform_device *pdev)
6767 {
6768 	struct wlcore_platdev_data *pdev_data = dev_get_platdata(&pdev->dev);
6769 	const char *nvs_name;
6770 	int ret = 0;
6771 
6772 	if (!wl->ops || !wl->ptable || !pdev_data)
6773 		return -EINVAL;
6774 
6775 	wl->dev = &pdev->dev;
6776 	wl->pdev = pdev;
6777 	platform_set_drvdata(pdev, wl);
6778 
6779 	if (pdev_data->family && pdev_data->family->nvs_name) {
6780 		nvs_name = pdev_data->family->nvs_name;
6781 		ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_UEVENT,
6782 					      nvs_name, &pdev->dev, GFP_KERNEL,
6783 					      wl, wlcore_nvs_cb);
6784 		if (ret < 0) {
6785 			wl1271_error("request_firmware_nowait failed for %s: %d",
6786 				     nvs_name, ret);
6787 			complete_all(&wl->nvs_loading_complete);
6788 		}
6789 	} else {
6790 		wlcore_nvs_cb(NULL, wl);
6791 	}
6792 
6793 	wl->dev->driver->pm = &wlcore_pm_ops;
6794 	pm_runtime_set_autosuspend_delay(wl->dev, 50);
6795 	pm_runtime_use_autosuspend(wl->dev);
6796 	pm_runtime_enable(wl->dev);
6797 
6798 	return ret;
6799 }
6800 EXPORT_SYMBOL_GPL(wlcore_probe);
6801 
6802 void wlcore_remove(struct platform_device *pdev)
6803 {
6804 	struct wlcore_platdev_data *pdev_data = dev_get_platdata(&pdev->dev);
6805 	struct wl1271 *wl = platform_get_drvdata(pdev);
6806 	int error;
6807 
6808 	error = pm_runtime_get_sync(wl->dev);
6809 	if (error < 0)
6810 		dev_warn(wl->dev, "PM runtime failed: %i\n", error);
6811 
6812 	wl->dev->driver->pm = NULL;
6813 
6814 	if (pdev_data->family && pdev_data->family->nvs_name)
6815 		wait_for_completion(&wl->nvs_loading_complete);
6816 	if (!wl->initialized)
6817 		return;
6818 
6819 	if (wl->wakeirq >= 0) {
6820 		dev_pm_clear_wake_irq(wl->dev);
6821 		wl->wakeirq = -ENODEV;
6822 	}
6823 
6824 	device_init_wakeup(wl->dev, false);
6825 
6826 	if (wl->irq_wake_enabled)
6827 		disable_irq_wake(wl->irq);
6828 
6829 	wl1271_unregister_hw(wl);
6830 
6831 	pm_runtime_put_sync(wl->dev);
6832 	pm_runtime_dont_use_autosuspend(wl->dev);
6833 	pm_runtime_disable(wl->dev);
6834 
6835 	free_irq(wl->irq, wl);
6836 	wlcore_free_hw(wl);
6837 }
6838 EXPORT_SYMBOL_GPL(wlcore_remove);
6839 
6840 u32 wl12xx_debug_level = DEBUG_NONE;
6841 EXPORT_SYMBOL_GPL(wl12xx_debug_level);
6842 module_param_named(debug_level, wl12xx_debug_level, uint, 0600);
6843 MODULE_PARM_DESC(debug_level, "wl12xx debugging level");
6844 
6845 module_param_named(fwlog, fwlog_param, charp, 0);
6846 MODULE_PARM_DESC(fwlog,
6847 		 "FW logger options: continuous, dbgpins or disable");
6848 
6849 module_param(fwlog_mem_blocks, int, 0600);
6850 MODULE_PARM_DESC(fwlog_mem_blocks, "fwlog mem_blocks");
6851 
6852 module_param(bug_on_recovery, int, 0600);
6853 MODULE_PARM_DESC(bug_on_recovery, "BUG() on fw recovery");
6854 
6855 module_param(no_recovery, int, 0600);
6856 MODULE_PARM_DESC(no_recovery, "Prevent HW recovery. FW will remain stuck.");
6857 
6858 MODULE_DESCRIPTION("TI WLAN core driver");
6859 MODULE_LICENSE("GPL");
6860 MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
6861 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
6862