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