1 /*
2  * Copyright (c) 2012-2017 Qualcomm Atheros, Inc.
3  * Copyright (c) 2018-2019, The Linux Foundation. All rights reserved.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <linux/moduleparam.h>
19 #include <linux/if_arp.h>
20 #include <linux/etherdevice.h>
21 #include <linux/rtnetlink.h>
22 
23 #include "wil6210.h"
24 #include "txrx.h"
25 #include "txrx_edma.h"
26 #include "wmi.h"
27 #include "boot_loader.h"
28 
29 #define WAIT_FOR_HALP_VOTE_MS 100
30 #define WAIT_FOR_SCAN_ABORT_MS 1000
31 #define WIL_DEFAULT_NUM_RX_STATUS_RINGS 1
32 #define WIL_BOARD_FILE_MAX_NAMELEN 128
33 
34 bool debug_fw; /* = false; */
35 module_param(debug_fw, bool, 0444);
36 MODULE_PARM_DESC(debug_fw, " do not perform card reset. For FW debug");
37 
38 static u8 oob_mode;
39 module_param(oob_mode, byte, 0444);
40 MODULE_PARM_DESC(oob_mode,
41 		 " enable out of the box (OOB) mode in FW, for diagnostics and certification");
42 
43 bool no_fw_recovery;
44 module_param(no_fw_recovery, bool, 0644);
45 MODULE_PARM_DESC(no_fw_recovery, " disable automatic FW error recovery");
46 
47 /* if not set via modparam, will be set to default value of 1/8 of
48  * rx ring size during init flow
49  */
50 unsigned short rx_ring_overflow_thrsh = WIL6210_RX_HIGH_TRSH_INIT;
51 module_param(rx_ring_overflow_thrsh, ushort, 0444);
52 MODULE_PARM_DESC(rx_ring_overflow_thrsh,
53 		 " RX ring overflow threshold in descriptors.");
54 
55 /* We allow allocation of more than 1 page buffers to support large packets.
56  * It is suboptimal behavior performance wise in case MTU above page size.
57  */
58 unsigned int mtu_max = TXRX_BUF_LEN_DEFAULT - WIL_MAX_MPDU_OVERHEAD;
59 static int mtu_max_set(const char *val, const struct kernel_param *kp)
60 {
61 	int ret;
62 
63 	/* sets mtu_max directly. no need to restore it in case of
64 	 * illegal value since we assume this will fail insmod
65 	 */
66 	ret = param_set_uint(val, kp);
67 	if (ret)
68 		return ret;
69 
70 	if (mtu_max < 68 || mtu_max > WIL_MAX_ETH_MTU)
71 		ret = -EINVAL;
72 
73 	return ret;
74 }
75 
76 static const struct kernel_param_ops mtu_max_ops = {
77 	.set = mtu_max_set,
78 	.get = param_get_uint,
79 };
80 
81 module_param_cb(mtu_max, &mtu_max_ops, &mtu_max, 0444);
82 MODULE_PARM_DESC(mtu_max, " Max MTU value.");
83 
84 static uint rx_ring_order;
85 static uint tx_ring_order = WIL_TX_RING_SIZE_ORDER_DEFAULT;
86 static uint bcast_ring_order = WIL_BCAST_RING_SIZE_ORDER_DEFAULT;
87 
88 static int ring_order_set(const char *val, const struct kernel_param *kp)
89 {
90 	int ret;
91 	uint x;
92 
93 	ret = kstrtouint(val, 0, &x);
94 	if (ret)
95 		return ret;
96 
97 	if ((x < WIL_RING_SIZE_ORDER_MIN) || (x > WIL_RING_SIZE_ORDER_MAX))
98 		return -EINVAL;
99 
100 	*((uint *)kp->arg) = x;
101 
102 	return 0;
103 }
104 
105 static const struct kernel_param_ops ring_order_ops = {
106 	.set = ring_order_set,
107 	.get = param_get_uint,
108 };
109 
110 module_param_cb(rx_ring_order, &ring_order_ops, &rx_ring_order, 0444);
111 MODULE_PARM_DESC(rx_ring_order, " Rx ring order; size = 1 << order");
112 module_param_cb(tx_ring_order, &ring_order_ops, &tx_ring_order, 0444);
113 MODULE_PARM_DESC(tx_ring_order, " Tx ring order; size = 1 << order");
114 module_param_cb(bcast_ring_order, &ring_order_ops, &bcast_ring_order, 0444);
115 MODULE_PARM_DESC(bcast_ring_order, " Bcast ring order; size = 1 << order");
116 
117 enum {
118 	WIL_BOOT_ERR,
119 	WIL_BOOT_VANILLA,
120 	WIL_BOOT_PRODUCTION,
121 	WIL_BOOT_DEVELOPMENT,
122 };
123 
124 enum {
125 	WIL_SIG_STATUS_VANILLA = 0x0,
126 	WIL_SIG_STATUS_DEVELOPMENT = 0x1,
127 	WIL_SIG_STATUS_PRODUCTION = 0x2,
128 	WIL_SIG_STATUS_CORRUPTED_PRODUCTION = 0x3,
129 };
130 
131 #define RST_DELAY (20) /* msec, for loop in @wil_wait_device_ready */
132 #define RST_COUNT (1 + 1000/RST_DELAY) /* round up to be above 1 sec total */
133 
134 #define PMU_READY_DELAY_MS (4) /* ms, for sleep in @wil_wait_device_ready */
135 
136 #define OTP_HW_DELAY (200) /* usec, loop in @wil_wait_device_ready_talyn_mb */
137 /* round up to be above 2 ms total */
138 #define OTP_HW_COUNT (1 + 2000 / OTP_HW_DELAY)
139 
140 /*
141  * Due to a hardware issue,
142  * one has to read/write to/from NIC in 32-bit chunks;
143  * regular memcpy_fromio and siblings will
144  * not work on 64-bit platform - it uses 64-bit transactions
145  *
146  * Force 32-bit transactions to enable NIC on 64-bit platforms
147  *
148  * To avoid byte swap on big endian host, __raw_{read|write}l
149  * should be used - {read|write}l would swap bytes to provide
150  * little endian on PCI value in host endianness.
151  */
152 void wil_memcpy_fromio_32(void *dst, const volatile void __iomem *src,
153 			  size_t count)
154 {
155 	u32 *d = dst;
156 	const volatile u32 __iomem *s = src;
157 
158 	for (; count >= 4; count -= 4)
159 		*d++ = __raw_readl(s++);
160 
161 	if (unlikely(count)) {
162 		/* count can be 1..3 */
163 		u32 tmp = __raw_readl(s);
164 
165 		memcpy(d, &tmp, count);
166 	}
167 }
168 
169 void wil_memcpy_toio_32(volatile void __iomem *dst, const void *src,
170 			size_t count)
171 {
172 	volatile u32 __iomem *d = dst;
173 	const u32 *s = src;
174 
175 	for (; count >= 4; count -= 4)
176 		__raw_writel(*s++, d++);
177 
178 	if (unlikely(count)) {
179 		/* count can be 1..3 */
180 		u32 tmp = 0;
181 
182 		memcpy(&tmp, s, count);
183 		__raw_writel(tmp, d);
184 	}
185 }
186 
187 static void wil_ring_fini_tx(struct wil6210_priv *wil, int id)
188 {
189 	struct wil_ring *ring = &wil->ring_tx[id];
190 	struct wil_ring_tx_data *txdata = &wil->ring_tx_data[id];
191 
192 	lockdep_assert_held(&wil->mutex);
193 
194 	if (!ring->va)
195 		return;
196 
197 	wil_dbg_misc(wil, "vring_fini_tx: id=%d\n", id);
198 
199 	spin_lock_bh(&txdata->lock);
200 	txdata->dot1x_open = false;
201 	txdata->mid = U8_MAX;
202 	txdata->enabled = 0; /* no Tx can be in progress or start anew */
203 	spin_unlock_bh(&txdata->lock);
204 	/* napi_synchronize waits for completion of the current NAPI but will
205 	 * not prevent the next NAPI run.
206 	 * Add a memory barrier to guarantee that txdata->enabled is zeroed
207 	 * before napi_synchronize so that the next scheduled NAPI will not
208 	 * handle this vring
209 	 */
210 	wmb();
211 	/* make sure NAPI won't touch this vring */
212 	if (test_bit(wil_status_napi_en, wil->status))
213 		napi_synchronize(&wil->napi_tx);
214 
215 	wil->txrx_ops.ring_fini_tx(wil, ring);
216 }
217 
218 static bool wil_vif_is_connected(struct wil6210_priv *wil, u8 mid)
219 {
220 	int i;
221 
222 	for (i = 0; i < max_assoc_sta; i++) {
223 		if (wil->sta[i].mid == mid &&
224 		    wil->sta[i].status == wil_sta_connected)
225 			return true;
226 	}
227 
228 	return false;
229 }
230 
231 static void wil_disconnect_cid_complete(struct wil6210_vif *vif, int cid,
232 					u16 reason_code)
233 __acquires(&sta->tid_rx_lock) __releases(&sta->tid_rx_lock)
234 {
235 	uint i;
236 	struct wil6210_priv *wil = vif_to_wil(vif);
237 	struct net_device *ndev = vif_to_ndev(vif);
238 	struct wireless_dev *wdev = vif_to_wdev(vif);
239 	struct wil_sta_info *sta = &wil->sta[cid];
240 	int min_ring_id = wil_get_min_tx_ring_id(wil);
241 
242 	might_sleep();
243 	wil_dbg_misc(wil,
244 		     "disconnect_cid_complete: CID %d, MID %d, status %d\n",
245 		     cid, sta->mid, sta->status);
246 	/* inform upper layers */
247 	if (sta->status != wil_sta_unused) {
248 		if (vif->mid != sta->mid) {
249 			wil_err(wil, "STA MID mismatch with VIF MID(%d)\n",
250 				vif->mid);
251 		}
252 
253 		switch (wdev->iftype) {
254 		case NL80211_IFTYPE_AP:
255 		case NL80211_IFTYPE_P2P_GO:
256 			/* AP-like interface */
257 			cfg80211_del_sta(ndev, sta->addr, GFP_KERNEL);
258 			break;
259 		default:
260 			break;
261 		}
262 		sta->status = wil_sta_unused;
263 		sta->mid = U8_MAX;
264 	}
265 	/* reorder buffers */
266 	for (i = 0; i < WIL_STA_TID_NUM; i++) {
267 		struct wil_tid_ampdu_rx *r;
268 
269 		spin_lock_bh(&sta->tid_rx_lock);
270 
271 		r = sta->tid_rx[i];
272 		sta->tid_rx[i] = NULL;
273 		wil_tid_ampdu_rx_free(wil, r);
274 
275 		spin_unlock_bh(&sta->tid_rx_lock);
276 	}
277 	/* crypto context */
278 	memset(sta->tid_crypto_rx, 0, sizeof(sta->tid_crypto_rx));
279 	memset(&sta->group_crypto_rx, 0, sizeof(sta->group_crypto_rx));
280 	/* release vrings */
281 	for (i = min_ring_id; i < ARRAY_SIZE(wil->ring_tx); i++) {
282 		if (wil->ring2cid_tid[i][0] == cid)
283 			wil_ring_fini_tx(wil, i);
284 	}
285 	/* statistics */
286 	memset(&sta->stats, 0, sizeof(sta->stats));
287 	sta->stats.tx_latency_min_us = U32_MAX;
288 }
289 
290 static void _wil6210_disconnect_complete(struct wil6210_vif *vif,
291 					 const u8 *bssid, u16 reason_code)
292 {
293 	struct wil6210_priv *wil = vif_to_wil(vif);
294 	int cid = -ENOENT;
295 	struct net_device *ndev;
296 	struct wireless_dev *wdev;
297 
298 	ndev = vif_to_ndev(vif);
299 	wdev = vif_to_wdev(vif);
300 
301 	might_sleep();
302 	wil_info(wil, "disconnect_complete: bssid=%pM, reason=%d\n",
303 		 bssid, reason_code);
304 
305 	/* Cases are:
306 	 * - disconnect single STA, still connected
307 	 * - disconnect single STA, already disconnected
308 	 * - disconnect all
309 	 *
310 	 * For "disconnect all", there are 3 options:
311 	 * - bssid == NULL
312 	 * - bssid is broadcast address (ff:ff:ff:ff:ff:ff)
313 	 * - bssid is our MAC address
314 	 */
315 	if (bssid && !is_broadcast_ether_addr(bssid) &&
316 	    !ether_addr_equal_unaligned(ndev->dev_addr, bssid)) {
317 		cid = wil_find_cid(wil, vif->mid, bssid);
318 		wil_dbg_misc(wil,
319 			     "Disconnect complete %pM, CID=%d, reason=%d\n",
320 			     bssid, cid, reason_code);
321 		if (cid >= 0) /* disconnect 1 peer */
322 			wil_disconnect_cid_complete(vif, cid, reason_code);
323 	} else { /* all */
324 		wil_dbg_misc(wil, "Disconnect complete all\n");
325 		for (cid = 0; cid < max_assoc_sta; cid++)
326 			wil_disconnect_cid_complete(vif, cid, reason_code);
327 	}
328 
329 	/* link state */
330 	switch (wdev->iftype) {
331 	case NL80211_IFTYPE_STATION:
332 	case NL80211_IFTYPE_P2P_CLIENT:
333 		wil_bcast_fini(vif);
334 		wil_update_net_queues_bh(wil, vif, NULL, true);
335 		netif_carrier_off(ndev);
336 		if (!wil_has_other_active_ifaces(wil, ndev, false, true))
337 			wil6210_bus_request(wil, WIL_DEFAULT_BUS_REQUEST_KBPS);
338 
339 		if (test_and_clear_bit(wil_vif_fwconnected, vif->status)) {
340 			atomic_dec(&wil->connected_vifs);
341 			cfg80211_disconnected(ndev, reason_code,
342 					      NULL, 0,
343 					      vif->locally_generated_disc,
344 					      GFP_KERNEL);
345 			vif->locally_generated_disc = false;
346 		} else if (test_bit(wil_vif_fwconnecting, vif->status)) {
347 			cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
348 						WLAN_STATUS_UNSPECIFIED_FAILURE,
349 						GFP_KERNEL);
350 			vif->bss = NULL;
351 		}
352 		clear_bit(wil_vif_fwconnecting, vif->status);
353 		clear_bit(wil_vif_ft_roam, vif->status);
354 
355 		break;
356 	case NL80211_IFTYPE_AP:
357 	case NL80211_IFTYPE_P2P_GO:
358 		if (!wil_vif_is_connected(wil, vif->mid)) {
359 			wil_update_net_queues_bh(wil, vif, NULL, true);
360 			if (test_and_clear_bit(wil_vif_fwconnected,
361 					       vif->status))
362 				atomic_dec(&wil->connected_vifs);
363 		} else {
364 			wil_update_net_queues_bh(wil, vif, NULL, false);
365 		}
366 		break;
367 	default:
368 		break;
369 	}
370 }
371 
372 static int wil_disconnect_cid(struct wil6210_vif *vif, int cid,
373 			      u16 reason_code)
374 {
375 	struct wil6210_priv *wil = vif_to_wil(vif);
376 	struct wireless_dev *wdev = vif_to_wdev(vif);
377 	struct wil_sta_info *sta = &wil->sta[cid];
378 	bool del_sta = false;
379 
380 	might_sleep();
381 	wil_dbg_misc(wil, "disconnect_cid: CID %d, MID %d, status %d\n",
382 		     cid, sta->mid, sta->status);
383 
384 	if (sta->status == wil_sta_unused)
385 		return 0;
386 
387 	if (vif->mid != sta->mid) {
388 		wil_err(wil, "STA MID mismatch with VIF MID(%d)\n", vif->mid);
389 		return -EINVAL;
390 	}
391 
392 	/* inform lower layers */
393 	if (wdev->iftype == NL80211_IFTYPE_AP && disable_ap_sme)
394 		del_sta = true;
395 
396 	/* disconnect by sending command disconnect/del_sta and wait
397 	 * synchronously for WMI_DISCONNECT_EVENTID event.
398 	 */
399 	return wmi_disconnect_sta(vif, sta->addr, reason_code, del_sta);
400 }
401 
402 static void _wil6210_disconnect(struct wil6210_vif *vif, const u8 *bssid,
403 				u16 reason_code)
404 {
405 	struct wil6210_priv *wil;
406 	struct net_device *ndev;
407 	int cid = -ENOENT;
408 
409 	if (unlikely(!vif))
410 		return;
411 
412 	wil = vif_to_wil(vif);
413 	ndev = vif_to_ndev(vif);
414 
415 	might_sleep();
416 	wil_info(wil, "disconnect bssid=%pM, reason=%d\n", bssid, reason_code);
417 
418 	/* Cases are:
419 	 * - disconnect single STA, still connected
420 	 * - disconnect single STA, already disconnected
421 	 * - disconnect all
422 	 *
423 	 * For "disconnect all", there are 3 options:
424 	 * - bssid == NULL
425 	 * - bssid is broadcast address (ff:ff:ff:ff:ff:ff)
426 	 * - bssid is our MAC address
427 	 */
428 	if (bssid && !is_broadcast_ether_addr(bssid) &&
429 	    !ether_addr_equal_unaligned(ndev->dev_addr, bssid)) {
430 		cid = wil_find_cid(wil, vif->mid, bssid);
431 		wil_dbg_misc(wil, "Disconnect %pM, CID=%d, reason=%d\n",
432 			     bssid, cid, reason_code);
433 		if (cid >= 0) /* disconnect 1 peer */
434 			wil_disconnect_cid(vif, cid, reason_code);
435 	} else { /* all */
436 		wil_dbg_misc(wil, "Disconnect all\n");
437 		for (cid = 0; cid < max_assoc_sta; cid++)
438 			wil_disconnect_cid(vif, cid, reason_code);
439 	}
440 
441 	/* call event handler manually after processing wmi_call,
442 	 * to avoid deadlock - disconnect event handler acquires
443 	 * wil->mutex while it is already held here
444 	 */
445 	_wil6210_disconnect_complete(vif, bssid, reason_code);
446 }
447 
448 void wil_disconnect_worker(struct work_struct *work)
449 {
450 	struct wil6210_vif *vif = container_of(work,
451 			struct wil6210_vif, disconnect_worker);
452 	struct wil6210_priv *wil = vif_to_wil(vif);
453 	struct net_device *ndev = vif_to_ndev(vif);
454 	int rc;
455 	struct {
456 		struct wmi_cmd_hdr wmi;
457 		struct wmi_disconnect_event evt;
458 	} __packed reply;
459 
460 	if (test_bit(wil_vif_fwconnected, vif->status))
461 		/* connect succeeded after all */
462 		return;
463 
464 	if (!test_bit(wil_vif_fwconnecting, vif->status))
465 		/* already disconnected */
466 		return;
467 
468 	memset(&reply, 0, sizeof(reply));
469 
470 	rc = wmi_call(wil, WMI_DISCONNECT_CMDID, vif->mid, NULL, 0,
471 		      WMI_DISCONNECT_EVENTID, &reply, sizeof(reply),
472 		      WIL6210_DISCONNECT_TO_MS);
473 	if (rc) {
474 		wil_err(wil, "disconnect error %d\n", rc);
475 		return;
476 	}
477 
478 	wil_update_net_queues_bh(wil, vif, NULL, true);
479 	netif_carrier_off(ndev);
480 	cfg80211_connect_result(ndev, NULL, NULL, 0, NULL, 0,
481 				WLAN_STATUS_UNSPECIFIED_FAILURE, GFP_KERNEL);
482 	clear_bit(wil_vif_fwconnecting, vif->status);
483 }
484 
485 static int wil_wait_for_recovery(struct wil6210_priv *wil)
486 {
487 	if (wait_event_interruptible(wil->wq, wil->recovery_state !=
488 				     fw_recovery_pending)) {
489 		wil_err(wil, "Interrupt, canceling recovery\n");
490 		return -ERESTARTSYS;
491 	}
492 	if (wil->recovery_state != fw_recovery_running) {
493 		wil_info(wil, "Recovery cancelled\n");
494 		return -EINTR;
495 	}
496 	wil_info(wil, "Proceed with recovery\n");
497 	return 0;
498 }
499 
500 void wil_set_recovery_state(struct wil6210_priv *wil, int state)
501 {
502 	wil_dbg_misc(wil, "set_recovery_state: %d -> %d\n",
503 		     wil->recovery_state, state);
504 
505 	wil->recovery_state = state;
506 	wake_up_interruptible(&wil->wq);
507 }
508 
509 bool wil_is_recovery_blocked(struct wil6210_priv *wil)
510 {
511 	return no_fw_recovery && (wil->recovery_state == fw_recovery_pending);
512 }
513 
514 static void wil_fw_error_worker(struct work_struct *work)
515 {
516 	struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
517 						fw_error_worker);
518 	struct net_device *ndev = wil->main_ndev;
519 	struct wireless_dev *wdev;
520 
521 	wil_dbg_misc(wil, "fw error worker\n");
522 
523 	if (!ndev || !(ndev->flags & IFF_UP)) {
524 		wil_info(wil, "No recovery - interface is down\n");
525 		return;
526 	}
527 	wdev = ndev->ieee80211_ptr;
528 
529 	/* increment @recovery_count if less then WIL6210_FW_RECOVERY_TO
530 	 * passed since last recovery attempt
531 	 */
532 	if (time_is_after_jiffies(wil->last_fw_recovery +
533 				  WIL6210_FW_RECOVERY_TO))
534 		wil->recovery_count++;
535 	else
536 		wil->recovery_count = 1; /* fw was alive for a long time */
537 
538 	if (wil->recovery_count > WIL6210_FW_RECOVERY_RETRIES) {
539 		wil_err(wil, "too many recovery attempts (%d), giving up\n",
540 			wil->recovery_count);
541 		return;
542 	}
543 
544 	wil->last_fw_recovery = jiffies;
545 
546 	wil_info(wil, "fw error recovery requested (try %d)...\n",
547 		 wil->recovery_count);
548 	if (!no_fw_recovery)
549 		wil->recovery_state = fw_recovery_running;
550 	if (wil_wait_for_recovery(wil) != 0)
551 		return;
552 
553 	rtnl_lock();
554 	mutex_lock(&wil->mutex);
555 	/* Needs adaptation for multiple VIFs
556 	 * need to go over all VIFs and consider the appropriate
557 	 * recovery because each one can have different iftype.
558 	 */
559 	switch (wdev->iftype) {
560 	case NL80211_IFTYPE_STATION:
561 	case NL80211_IFTYPE_P2P_CLIENT:
562 	case NL80211_IFTYPE_MONITOR:
563 		/* silent recovery, upper layers will see disconnect */
564 		__wil_down(wil);
565 		__wil_up(wil);
566 		break;
567 	case NL80211_IFTYPE_AP:
568 	case NL80211_IFTYPE_P2P_GO:
569 		if (no_fw_recovery) /* upper layers do recovery */
570 			break;
571 		/* silent recovery, upper layers will see disconnect */
572 		__wil_down(wil);
573 		__wil_up(wil);
574 		mutex_unlock(&wil->mutex);
575 		wil_cfg80211_ap_recovery(wil);
576 		mutex_lock(&wil->mutex);
577 		wil_info(wil, "... completed\n");
578 		break;
579 	default:
580 		wil_err(wil, "No recovery - unknown interface type %d\n",
581 			wdev->iftype);
582 		break;
583 	}
584 
585 	mutex_unlock(&wil->mutex);
586 	rtnl_unlock();
587 }
588 
589 static int wil_find_free_ring(struct wil6210_priv *wil)
590 {
591 	int i;
592 	int min_ring_id = wil_get_min_tx_ring_id(wil);
593 
594 	for (i = min_ring_id; i < WIL6210_MAX_TX_RINGS; i++) {
595 		if (!wil->ring_tx[i].va)
596 			return i;
597 	}
598 	return -EINVAL;
599 }
600 
601 int wil_ring_init_tx(struct wil6210_vif *vif, int cid)
602 {
603 	struct wil6210_priv *wil = vif_to_wil(vif);
604 	int rc = -EINVAL, ringid;
605 
606 	if (cid < 0) {
607 		wil_err(wil, "No connection pending\n");
608 		goto out;
609 	}
610 	ringid = wil_find_free_ring(wil);
611 	if (ringid < 0) {
612 		wil_err(wil, "No free vring found\n");
613 		goto out;
614 	}
615 
616 	wil_dbg_wmi(wil, "Configure for connection CID %d MID %d ring %d\n",
617 		    cid, vif->mid, ringid);
618 
619 	rc = wil->txrx_ops.ring_init_tx(vif, ringid, 1 << tx_ring_order,
620 					cid, 0);
621 	if (rc)
622 		wil_err(wil, "init TX for CID %d MID %d vring %d failed\n",
623 			cid, vif->mid, ringid);
624 
625 out:
626 	return rc;
627 }
628 
629 int wil_bcast_init(struct wil6210_vif *vif)
630 {
631 	struct wil6210_priv *wil = vif_to_wil(vif);
632 	int ri = vif->bcast_ring, rc;
633 
634 	if (ri >= 0 && wil->ring_tx[ri].va)
635 		return 0;
636 
637 	ri = wil_find_free_ring(wil);
638 	if (ri < 0)
639 		return ri;
640 
641 	vif->bcast_ring = ri;
642 	rc = wil->txrx_ops.ring_init_bcast(vif, ri, 1 << bcast_ring_order);
643 	if (rc)
644 		vif->bcast_ring = -1;
645 
646 	return rc;
647 }
648 
649 void wil_bcast_fini(struct wil6210_vif *vif)
650 {
651 	struct wil6210_priv *wil = vif_to_wil(vif);
652 	int ri = vif->bcast_ring;
653 
654 	if (ri < 0)
655 		return;
656 
657 	vif->bcast_ring = -1;
658 	wil_ring_fini_tx(wil, ri);
659 }
660 
661 void wil_bcast_fini_all(struct wil6210_priv *wil)
662 {
663 	int i;
664 	struct wil6210_vif *vif;
665 
666 	for (i = 0; i < wil->max_vifs; i++) {
667 		vif = wil->vifs[i];
668 		if (vif)
669 			wil_bcast_fini(vif);
670 	}
671 }
672 
673 int wil_priv_init(struct wil6210_priv *wil)
674 {
675 	uint i;
676 
677 	wil_dbg_misc(wil, "priv_init\n");
678 
679 	memset(wil->sta, 0, sizeof(wil->sta));
680 	for (i = 0; i < WIL6210_MAX_CID; i++) {
681 		spin_lock_init(&wil->sta[i].tid_rx_lock);
682 		wil->sta[i].mid = U8_MAX;
683 	}
684 
685 	for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
686 		spin_lock_init(&wil->ring_tx_data[i].lock);
687 		wil->ring2cid_tid[i][0] = WIL6210_MAX_CID;
688 	}
689 
690 	mutex_init(&wil->mutex);
691 	mutex_init(&wil->vif_mutex);
692 	mutex_init(&wil->wmi_mutex);
693 	mutex_init(&wil->halp.lock);
694 
695 	init_completion(&wil->wmi_ready);
696 	init_completion(&wil->wmi_call);
697 	init_completion(&wil->halp.comp);
698 
699 	INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
700 	INIT_WORK(&wil->fw_error_worker, wil_fw_error_worker);
701 
702 	INIT_LIST_HEAD(&wil->pending_wmi_ev);
703 	spin_lock_init(&wil->wmi_ev_lock);
704 	spin_lock_init(&wil->net_queue_lock);
705 	init_waitqueue_head(&wil->wq);
706 
707 	wil->wmi_wq = create_singlethread_workqueue(WIL_NAME "_wmi");
708 	if (!wil->wmi_wq)
709 		return -EAGAIN;
710 
711 	wil->wq_service = create_singlethread_workqueue(WIL_NAME "_service");
712 	if (!wil->wq_service)
713 		goto out_wmi_wq;
714 
715 	wil->last_fw_recovery = jiffies;
716 	wil->tx_interframe_timeout = WIL6210_ITR_TX_INTERFRAME_TIMEOUT_DEFAULT;
717 	wil->rx_interframe_timeout = WIL6210_ITR_RX_INTERFRAME_TIMEOUT_DEFAULT;
718 	wil->tx_max_burst_duration = WIL6210_ITR_TX_MAX_BURST_DURATION_DEFAULT;
719 	wil->rx_max_burst_duration = WIL6210_ITR_RX_MAX_BURST_DURATION_DEFAULT;
720 
721 	if (rx_ring_overflow_thrsh == WIL6210_RX_HIGH_TRSH_INIT)
722 		rx_ring_overflow_thrsh = WIL6210_RX_HIGH_TRSH_DEFAULT;
723 
724 	wil->ps_profile =  WMI_PS_PROFILE_TYPE_DEFAULT;
725 
726 	wil->wakeup_trigger = WMI_WAKEUP_TRIGGER_UCAST |
727 			      WMI_WAKEUP_TRIGGER_BCAST;
728 	memset(&wil->suspend_stats, 0, sizeof(wil->suspend_stats));
729 	wil->ring_idle_trsh = 16;
730 
731 	wil->reply_mid = U8_MAX;
732 	wil->max_vifs = 1;
733 
734 	/* edma configuration can be updated via debugfs before allocation */
735 	wil->num_rx_status_rings = WIL_DEFAULT_NUM_RX_STATUS_RINGS;
736 	wil->tx_status_ring_order = WIL_TX_SRING_SIZE_ORDER_DEFAULT;
737 
738 	/* Rx status ring size should be bigger than the number of RX buffers
739 	 * in order to prevent backpressure on the status ring, which may
740 	 * cause HW freeze.
741 	 */
742 	wil->rx_status_ring_order = WIL_RX_SRING_SIZE_ORDER_DEFAULT;
743 	/* Number of RX buffer IDs should be bigger than the RX descriptor
744 	 * ring size as in HW reorder flow, the HW can consume additional
745 	 * buffers before releasing the previous ones.
746 	 */
747 	wil->rx_buff_id_count = WIL_RX_BUFF_ARR_SIZE_DEFAULT;
748 
749 	wil->amsdu_en = 1;
750 
751 	return 0;
752 
753 out_wmi_wq:
754 	destroy_workqueue(wil->wmi_wq);
755 
756 	return -EAGAIN;
757 }
758 
759 void wil6210_bus_request(struct wil6210_priv *wil, u32 kbps)
760 {
761 	if (wil->platform_ops.bus_request) {
762 		wil->bus_request_kbps = kbps;
763 		wil->platform_ops.bus_request(wil->platform_handle, kbps);
764 	}
765 }
766 
767 /**
768  * wil6210_disconnect - disconnect one connection
769  * @vif: virtual interface context
770  * @bssid: peer to disconnect, NULL to disconnect all
771  * @reason_code: Reason code for the Disassociation frame
772  *
773  * Disconnect and release associated resources. Issue WMI
774  * command(s) to trigger MAC disconnect. When command was issued
775  * successfully, call the wil6210_disconnect_complete function
776  * to handle the event synchronously
777  */
778 void wil6210_disconnect(struct wil6210_vif *vif, const u8 *bssid,
779 			u16 reason_code)
780 {
781 	struct wil6210_priv *wil = vif_to_wil(vif);
782 
783 	wil_dbg_misc(wil, "disconnecting\n");
784 
785 	del_timer_sync(&vif->connect_timer);
786 	_wil6210_disconnect(vif, bssid, reason_code);
787 }
788 
789 /**
790  * wil6210_disconnect_complete - handle disconnect event
791  * @vif: virtual interface context
792  * @bssid: peer to disconnect, NULL to disconnect all
793  * @reason_code: Reason code for the Disassociation frame
794  *
795  * Release associated resources and indicate upper layers the
796  * connection is terminated.
797  */
798 void wil6210_disconnect_complete(struct wil6210_vif *vif, const u8 *bssid,
799 				 u16 reason_code)
800 {
801 	struct wil6210_priv *wil = vif_to_wil(vif);
802 
803 	wil_dbg_misc(wil, "got disconnect\n");
804 
805 	del_timer_sync(&vif->connect_timer);
806 	_wil6210_disconnect_complete(vif, bssid, reason_code);
807 }
808 
809 void wil_priv_deinit(struct wil6210_priv *wil)
810 {
811 	wil_dbg_misc(wil, "priv_deinit\n");
812 
813 	wil_set_recovery_state(wil, fw_recovery_idle);
814 	cancel_work_sync(&wil->fw_error_worker);
815 	wmi_event_flush(wil);
816 	destroy_workqueue(wil->wq_service);
817 	destroy_workqueue(wil->wmi_wq);
818 }
819 
820 static void wil_shutdown_bl(struct wil6210_priv *wil)
821 {
822 	u32 val;
823 
824 	wil_s(wil, RGF_USER_BL +
825 	      offsetof(struct bl_dedicated_registers_v1,
826 		       bl_shutdown_handshake), BL_SHUTDOWN_HS_GRTD);
827 
828 	usleep_range(100, 150);
829 
830 	val = wil_r(wil, RGF_USER_BL +
831 		    offsetof(struct bl_dedicated_registers_v1,
832 			     bl_shutdown_handshake));
833 	if (val & BL_SHUTDOWN_HS_RTD) {
834 		wil_dbg_misc(wil, "BL is ready for halt\n");
835 		return;
836 	}
837 
838 	wil_err(wil, "BL did not report ready for halt\n");
839 }
840 
841 /* this format is used by ARC embedded CPU for instruction memory */
842 static inline u32 ARC_me_imm32(u32 d)
843 {
844 	return ((d & 0xffff0000) >> 16) | ((d & 0x0000ffff) << 16);
845 }
846 
847 /* defines access to interrupt vectors for wil_freeze_bl */
848 #define ARC_IRQ_VECTOR_OFFSET(N)	((N) * 8)
849 /* ARC long jump instruction */
850 #define ARC_JAL_INST			(0x20200f80)
851 
852 static void wil_freeze_bl(struct wil6210_priv *wil)
853 {
854 	u32 jal, upc, saved;
855 	u32 ivt3 = ARC_IRQ_VECTOR_OFFSET(3);
856 
857 	jal = wil_r(wil, wil->iccm_base + ivt3);
858 	if (jal != ARC_me_imm32(ARC_JAL_INST)) {
859 		wil_dbg_misc(wil, "invalid IVT entry found, skipping\n");
860 		return;
861 	}
862 
863 	/* prevent the target from entering deep sleep
864 	 * and disabling memory access
865 	 */
866 	saved = wil_r(wil, RGF_USER_USAGE_8);
867 	wil_w(wil, RGF_USER_USAGE_8, saved | BIT_USER_PREVENT_DEEP_SLEEP);
868 	usleep_range(20, 25); /* let the BL process the bit */
869 
870 	/* redirect to endless loop in the INT_L1 context and let it trap */
871 	wil_w(wil, wil->iccm_base + ivt3 + 4, ARC_me_imm32(ivt3));
872 	usleep_range(20, 25); /* let the BL get into the trap */
873 
874 	/* verify the BL is frozen */
875 	upc = wil_r(wil, RGF_USER_CPU_PC);
876 	if (upc < ivt3 || (upc > (ivt3 + 8)))
877 		wil_dbg_misc(wil, "BL freeze failed, PC=0x%08X\n", upc);
878 
879 	wil_w(wil, RGF_USER_USAGE_8, saved);
880 }
881 
882 static void wil_bl_prepare_halt(struct wil6210_priv *wil)
883 {
884 	u32 tmp, ver;
885 
886 	/* before halting device CPU driver must make sure BL is not accessing
887 	 * host memory. This is done differently depending on BL version:
888 	 * 1. For very old BL versions the procedure is skipped
889 	 * (not supported).
890 	 * 2. For old BL version we use a special trick to freeze the BL
891 	 * 3. For new BL versions we shutdown the BL using handshake procedure.
892 	 */
893 	tmp = wil_r(wil, RGF_USER_BL +
894 		    offsetof(struct bl_dedicated_registers_v0,
895 			     boot_loader_struct_version));
896 	if (!tmp) {
897 		wil_dbg_misc(wil, "old BL, skipping halt preparation\n");
898 		return;
899 	}
900 
901 	tmp = wil_r(wil, RGF_USER_BL +
902 		    offsetof(struct bl_dedicated_registers_v1,
903 			     bl_shutdown_handshake));
904 	ver = BL_SHUTDOWN_HS_PROT_VER(tmp);
905 
906 	if (ver > 0)
907 		wil_shutdown_bl(wil);
908 	else
909 		wil_freeze_bl(wil);
910 }
911 
912 static inline void wil_halt_cpu(struct wil6210_priv *wil)
913 {
914 	if (wil->hw_version >= HW_VER_TALYN_MB) {
915 		wil_w(wil, RGF_USER_USER_CPU_0_TALYN_MB,
916 		      BIT_USER_USER_CPU_MAN_RST);
917 		wil_w(wil, RGF_USER_MAC_CPU_0_TALYN_MB,
918 		      BIT_USER_MAC_CPU_MAN_RST);
919 	} else {
920 		wil_w(wil, RGF_USER_USER_CPU_0, BIT_USER_USER_CPU_MAN_RST);
921 		wil_w(wil, RGF_USER_MAC_CPU_0,  BIT_USER_MAC_CPU_MAN_RST);
922 	}
923 }
924 
925 static inline void wil_release_cpu(struct wil6210_priv *wil)
926 {
927 	/* Start CPU */
928 	if (wil->hw_version >= HW_VER_TALYN_MB)
929 		wil_w(wil, RGF_USER_USER_CPU_0_TALYN_MB, 1);
930 	else
931 		wil_w(wil, RGF_USER_USER_CPU_0, 1);
932 }
933 
934 static void wil_set_oob_mode(struct wil6210_priv *wil, u8 mode)
935 {
936 	wil_info(wil, "oob_mode to %d\n", mode);
937 	switch (mode) {
938 	case 0:
939 		wil_c(wil, RGF_USER_USAGE_6, BIT_USER_OOB_MODE |
940 		      BIT_USER_OOB_R2_MODE);
941 		break;
942 	case 1:
943 		wil_c(wil, RGF_USER_USAGE_6, BIT_USER_OOB_R2_MODE);
944 		wil_s(wil, RGF_USER_USAGE_6, BIT_USER_OOB_MODE);
945 		break;
946 	case 2:
947 		wil_c(wil, RGF_USER_USAGE_6, BIT_USER_OOB_MODE);
948 		wil_s(wil, RGF_USER_USAGE_6, BIT_USER_OOB_R2_MODE);
949 		break;
950 	default:
951 		wil_err(wil, "invalid oob_mode: %d\n", mode);
952 	}
953 }
954 
955 static int wil_wait_device_ready(struct wil6210_priv *wil, int no_flash)
956 {
957 	int delay = 0;
958 	u32 x, x1 = 0;
959 
960 	/* wait until device ready. */
961 	if (no_flash) {
962 		msleep(PMU_READY_DELAY_MS);
963 
964 		wil_dbg_misc(wil, "Reset completed\n");
965 	} else {
966 		do {
967 			msleep(RST_DELAY);
968 			x = wil_r(wil, RGF_USER_BL +
969 				  offsetof(struct bl_dedicated_registers_v0,
970 					   boot_loader_ready));
971 			if (x1 != x) {
972 				wil_dbg_misc(wil, "BL.ready 0x%08x => 0x%08x\n",
973 					     x1, x);
974 				x1 = x;
975 			}
976 			if (delay++ > RST_COUNT) {
977 				wil_err(wil, "Reset not completed, bl.ready 0x%08x\n",
978 					x);
979 				return -ETIME;
980 			}
981 		} while (x != BL_READY);
982 
983 		wil_dbg_misc(wil, "Reset completed in %d ms\n",
984 			     delay * RST_DELAY);
985 	}
986 
987 	return 0;
988 }
989 
990 static int wil_wait_device_ready_talyn_mb(struct wil6210_priv *wil)
991 {
992 	u32 otp_hw;
993 	u8 signature_status;
994 	bool otp_signature_err;
995 	bool hw_section_done;
996 	u32 otp_qc_secured;
997 	int delay = 0;
998 
999 	/* Wait for OTP signature test to complete */
1000 	usleep_range(2000, 2200);
1001 
1002 	wil->boot_config = WIL_BOOT_ERR;
1003 
1004 	/* Poll until OTP signature status is valid.
1005 	 * In vanilla and development modes, when signature test is complete
1006 	 * HW sets BIT_OTP_SIGNATURE_ERR_TALYN_MB.
1007 	 * In production mode BIT_OTP_SIGNATURE_ERR_TALYN_MB remains 0, poll
1008 	 * for signature status change to 2 or 3.
1009 	 */
1010 	do {
1011 		otp_hw = wil_r(wil, RGF_USER_OTP_HW_RD_MACHINE_1);
1012 		signature_status = WIL_GET_BITS(otp_hw, 8, 9);
1013 		otp_signature_err = otp_hw & BIT_OTP_SIGNATURE_ERR_TALYN_MB;
1014 
1015 		if (otp_signature_err &&
1016 		    signature_status == WIL_SIG_STATUS_VANILLA) {
1017 			wil->boot_config = WIL_BOOT_VANILLA;
1018 			break;
1019 		}
1020 		if (otp_signature_err &&
1021 		    signature_status == WIL_SIG_STATUS_DEVELOPMENT) {
1022 			wil->boot_config = WIL_BOOT_DEVELOPMENT;
1023 			break;
1024 		}
1025 		if (!otp_signature_err &&
1026 		    signature_status == WIL_SIG_STATUS_PRODUCTION) {
1027 			wil->boot_config = WIL_BOOT_PRODUCTION;
1028 			break;
1029 		}
1030 		if  (!otp_signature_err &&
1031 		     signature_status ==
1032 		     WIL_SIG_STATUS_CORRUPTED_PRODUCTION) {
1033 			/* Unrecognized OTP signature found. Possibly a
1034 			 * corrupted production signature, access control
1035 			 * is applied as in production mode, therefore
1036 			 * do not fail
1037 			 */
1038 			wil->boot_config = WIL_BOOT_PRODUCTION;
1039 			break;
1040 		}
1041 		if (delay++ > OTP_HW_COUNT)
1042 			break;
1043 
1044 		usleep_range(OTP_HW_DELAY, OTP_HW_DELAY + 10);
1045 	} while (!otp_signature_err && signature_status == 0);
1046 
1047 	if (wil->boot_config == WIL_BOOT_ERR) {
1048 		wil_err(wil,
1049 			"invalid boot config, signature_status %d otp_signature_err %d\n",
1050 			signature_status, otp_signature_err);
1051 		return -ETIME;
1052 	}
1053 
1054 	wil_dbg_misc(wil,
1055 		     "signature test done in %d usec, otp_hw 0x%x, boot_config %d\n",
1056 		     delay * OTP_HW_DELAY, otp_hw, wil->boot_config);
1057 
1058 	if (wil->boot_config == WIL_BOOT_VANILLA)
1059 		/* Assuming not SPI boot (currently not supported) */
1060 		goto out;
1061 
1062 	hw_section_done = otp_hw & BIT_OTP_HW_SECTION_DONE_TALYN_MB;
1063 	delay = 0;
1064 
1065 	while (!hw_section_done) {
1066 		msleep(RST_DELAY);
1067 
1068 		otp_hw = wil_r(wil, RGF_USER_OTP_HW_RD_MACHINE_1);
1069 		hw_section_done = otp_hw & BIT_OTP_HW_SECTION_DONE_TALYN_MB;
1070 
1071 		if (delay++ > RST_COUNT) {
1072 			wil_err(wil, "TO waiting for hw_section_done\n");
1073 			return -ETIME;
1074 		}
1075 	}
1076 
1077 	wil_dbg_misc(wil, "HW section done in %d ms\n", delay * RST_DELAY);
1078 
1079 	otp_qc_secured = wil_r(wil, RGF_OTP_QC_SECURED);
1080 	wil->secured_boot = otp_qc_secured & BIT_BOOT_FROM_ROM ? 1 : 0;
1081 	wil_dbg_misc(wil, "secured boot is %sabled\n",
1082 		     wil->secured_boot ? "en" : "dis");
1083 
1084 out:
1085 	wil_dbg_misc(wil, "Reset completed\n");
1086 
1087 	return 0;
1088 }
1089 
1090 static int wil_target_reset(struct wil6210_priv *wil, int no_flash)
1091 {
1092 	u32 x;
1093 	int rc;
1094 
1095 	wil_dbg_misc(wil, "Resetting \"%s\"...\n", wil->hw_name);
1096 
1097 	if (wil->hw_version < HW_VER_TALYN) {
1098 		/* Clear MAC link up */
1099 		wil_s(wil, RGF_HP_CTRL, BIT(15));
1100 		wil_s(wil, RGF_USER_CLKS_CTL_SW_RST_MASK_0,
1101 		      BIT_HPAL_PERST_FROM_PAD);
1102 		wil_s(wil, RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_CAR_PERST_RST);
1103 	}
1104 
1105 	wil_halt_cpu(wil);
1106 
1107 	if (!no_flash) {
1108 		/* clear all boot loader "ready" bits */
1109 		wil_w(wil, RGF_USER_BL +
1110 		      offsetof(struct bl_dedicated_registers_v0,
1111 			       boot_loader_ready), 0);
1112 		/* this should be safe to write even with old BLs */
1113 		wil_w(wil, RGF_USER_BL +
1114 		      offsetof(struct bl_dedicated_registers_v1,
1115 			       bl_shutdown_handshake), 0);
1116 	}
1117 	/* Clear Fw Download notification */
1118 	wil_c(wil, RGF_USER_USAGE_6, BIT(0));
1119 
1120 	wil_s(wil, RGF_CAF_OSC_CONTROL, BIT_CAF_OSC_XTAL_EN);
1121 	/* XTAL stabilization should take about 3ms */
1122 	usleep_range(5000, 7000);
1123 	x = wil_r(wil, RGF_CAF_PLL_LOCK_STATUS);
1124 	if (!(x & BIT_CAF_OSC_DIG_XTAL_STABLE)) {
1125 		wil_err(wil, "Xtal stabilization timeout\n"
1126 			"RGF_CAF_PLL_LOCK_STATUS = 0x%08x\n", x);
1127 		return -ETIME;
1128 	}
1129 	/* switch 10k to XTAL*/
1130 	wil_c(wil, RGF_USER_SPARROW_M_4, BIT_SPARROW_M_4_SEL_SLEEP_OR_REF);
1131 	/* 40 MHz */
1132 	wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_CAR_AHB_SW_SEL);
1133 
1134 	wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x3ff81f);
1135 	wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0xf);
1136 
1137 	if (wil->hw_version >= HW_VER_TALYN_MB) {
1138 		wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x7e000000);
1139 		wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003f);
1140 		wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0xc00000f0);
1141 		wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xffe7fe00);
1142 	} else {
1143 		wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xfe000000);
1144 		wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003f);
1145 		wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x000000f0);
1146 		wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xffe7fe00);
1147 	}
1148 
1149 	wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x0);
1150 	wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0x0);
1151 
1152 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
1153 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
1154 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
1155 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
1156 
1157 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000003);
1158 	/* reset A2 PCIE AHB */
1159 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000);
1160 
1161 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
1162 
1163 	if (wil->hw_version == HW_VER_TALYN_MB)
1164 		rc = wil_wait_device_ready_talyn_mb(wil);
1165 	else
1166 		rc = wil_wait_device_ready(wil, no_flash);
1167 	if (rc)
1168 		return rc;
1169 
1170 	wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
1171 
1172 	/* enable fix for HW bug related to the SA/DA swap in AP Rx */
1173 	wil_s(wil, RGF_DMA_OFUL_NID_0, BIT_DMA_OFUL_NID_0_RX_EXT_TR_EN |
1174 	      BIT_DMA_OFUL_NID_0_RX_EXT_A3_SRC);
1175 
1176 	if (wil->hw_version < HW_VER_TALYN_MB && no_flash) {
1177 		/* Reset OTP HW vectors to fit 40MHz */
1178 		wil_w(wil, RGF_USER_XPM_IFC_RD_TIME1, 0x60001);
1179 		wil_w(wil, RGF_USER_XPM_IFC_RD_TIME2, 0x20027);
1180 		wil_w(wil, RGF_USER_XPM_IFC_RD_TIME3, 0x1);
1181 		wil_w(wil, RGF_USER_XPM_IFC_RD_TIME4, 0x20027);
1182 		wil_w(wil, RGF_USER_XPM_IFC_RD_TIME5, 0x30003);
1183 		wil_w(wil, RGF_USER_XPM_IFC_RD_TIME6, 0x20002);
1184 		wil_w(wil, RGF_USER_XPM_IFC_RD_TIME7, 0x60001);
1185 		wil_w(wil, RGF_USER_XPM_IFC_RD_TIME8, 0x60001);
1186 		wil_w(wil, RGF_USER_XPM_IFC_RD_TIME9, 0x60001);
1187 		wil_w(wil, RGF_USER_XPM_IFC_RD_TIME10, 0x60001);
1188 		wil_w(wil, RGF_USER_XPM_RD_DOUT_SAMPLE_TIME, 0x57);
1189 	}
1190 
1191 	return 0;
1192 }
1193 
1194 static void wil_collect_fw_info(struct wil6210_priv *wil)
1195 {
1196 	struct wiphy *wiphy = wil_to_wiphy(wil);
1197 	u8 retry_short;
1198 	int rc;
1199 
1200 	wil_refresh_fw_capabilities(wil);
1201 
1202 	rc = wmi_get_mgmt_retry(wil, &retry_short);
1203 	if (!rc) {
1204 		wiphy->retry_short = retry_short;
1205 		wil_dbg_misc(wil, "FW retry_short: %d\n", retry_short);
1206 	}
1207 }
1208 
1209 void wil_refresh_fw_capabilities(struct wil6210_priv *wil)
1210 {
1211 	struct wiphy *wiphy = wil_to_wiphy(wil);
1212 	int features;
1213 
1214 	wil->keep_radio_on_during_sleep =
1215 		test_bit(WIL_PLATFORM_CAPA_RADIO_ON_IN_SUSPEND,
1216 			 wil->platform_capa) &&
1217 		test_bit(WMI_FW_CAPABILITY_D3_SUSPEND, wil->fw_capabilities);
1218 
1219 	wil_info(wil, "keep_radio_on_during_sleep (%d)\n",
1220 		 wil->keep_radio_on_during_sleep);
1221 
1222 	if (test_bit(WMI_FW_CAPABILITY_RSSI_REPORTING, wil->fw_capabilities))
1223 		wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
1224 	else
1225 		wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
1226 
1227 	if (test_bit(WMI_FW_CAPABILITY_PNO, wil->fw_capabilities)) {
1228 		wiphy->max_sched_scan_reqs = 1;
1229 		wiphy->max_sched_scan_ssids = WMI_MAX_PNO_SSID_NUM;
1230 		wiphy->max_match_sets = WMI_MAX_PNO_SSID_NUM;
1231 		wiphy->max_sched_scan_ie_len = WMI_MAX_IE_LEN;
1232 		wiphy->max_sched_scan_plans = WMI_MAX_PLANS_NUM;
1233 	}
1234 
1235 	if (test_bit(WMI_FW_CAPABILITY_TX_REQ_EXT, wil->fw_capabilities))
1236 		wiphy->flags |= WIPHY_FLAG_OFFCHAN_TX;
1237 
1238 	if (wil->platform_ops.set_features) {
1239 		features = (test_bit(WMI_FW_CAPABILITY_REF_CLOCK_CONTROL,
1240 				     wil->fw_capabilities) &&
1241 			    test_bit(WIL_PLATFORM_CAPA_EXT_CLK,
1242 				     wil->platform_capa)) ?
1243 			BIT(WIL_PLATFORM_FEATURE_FW_EXT_CLK_CONTROL) : 0;
1244 
1245 		if (wil->n_msi == 3)
1246 			features |= BIT(WIL_PLATFORM_FEATURE_TRIPLE_MSI);
1247 
1248 		wil->platform_ops.set_features(wil->platform_handle, features);
1249 	}
1250 
1251 	if (test_bit(WMI_FW_CAPABILITY_BACK_WIN_SIZE_64,
1252 		     wil->fw_capabilities)) {
1253 		wil->max_agg_wsize = WIL_MAX_AGG_WSIZE_64;
1254 		wil->max_ampdu_size = WIL_MAX_AMPDU_SIZE_128;
1255 	} else {
1256 		wil->max_agg_wsize = WIL_MAX_AGG_WSIZE;
1257 		wil->max_ampdu_size = WIL_MAX_AMPDU_SIZE;
1258 	}
1259 
1260 	update_supported_bands(wil);
1261 }
1262 
1263 void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
1264 {
1265 	le32_to_cpus(&r->base);
1266 	le16_to_cpus(&r->entry_size);
1267 	le16_to_cpus(&r->size);
1268 	le32_to_cpus(&r->tail);
1269 	le32_to_cpus(&r->head);
1270 }
1271 
1272 /* construct actual board file name to use */
1273 void wil_get_board_file(struct wil6210_priv *wil, char *buf, size_t len)
1274 {
1275 	const char *board_file;
1276 	const char *wil_talyn_fw_name = ftm_mode ? WIL_FW_NAME_FTM_TALYN :
1277 			      WIL_FW_NAME_TALYN;
1278 
1279 	if (wil->board_file) {
1280 		board_file = wil->board_file;
1281 	} else {
1282 		/* If specific FW file is used for Talyn,
1283 		 * use specific board file
1284 		 */
1285 		if (strcmp(wil->wil_fw_name, wil_talyn_fw_name) == 0)
1286 			board_file = WIL_BRD_NAME_TALYN;
1287 		else
1288 			board_file = WIL_BOARD_FILE_NAME;
1289 	}
1290 
1291 	strlcpy(buf, board_file, len);
1292 }
1293 
1294 static int wil_get_bl_info(struct wil6210_priv *wil)
1295 {
1296 	struct net_device *ndev = wil->main_ndev;
1297 	struct wiphy *wiphy = wil_to_wiphy(wil);
1298 	union {
1299 		struct bl_dedicated_registers_v0 bl0;
1300 		struct bl_dedicated_registers_v1 bl1;
1301 	} bl;
1302 	u32 bl_ver;
1303 	u8 *mac;
1304 	u16 rf_status;
1305 
1306 	wil_memcpy_fromio_32(&bl, wil->csr + HOSTADDR(RGF_USER_BL),
1307 			     sizeof(bl));
1308 	bl_ver = le32_to_cpu(bl.bl0.boot_loader_struct_version);
1309 	mac = bl.bl0.mac_address;
1310 
1311 	if (bl_ver == 0) {
1312 		le32_to_cpus(&bl.bl0.rf_type);
1313 		le32_to_cpus(&bl.bl0.baseband_type);
1314 		rf_status = 0; /* actually, unknown */
1315 		wil_info(wil,
1316 			 "Boot Loader struct v%d: MAC = %pM RF = 0x%08x bband = 0x%08x\n",
1317 			 bl_ver, mac,
1318 			 bl.bl0.rf_type, bl.bl0.baseband_type);
1319 		wil_info(wil, "Boot Loader build unknown for struct v0\n");
1320 	} else {
1321 		le16_to_cpus(&bl.bl1.rf_type);
1322 		rf_status = le16_to_cpu(bl.bl1.rf_status);
1323 		le32_to_cpus(&bl.bl1.baseband_type);
1324 		le16_to_cpus(&bl.bl1.bl_version_subminor);
1325 		le16_to_cpus(&bl.bl1.bl_version_build);
1326 		wil_info(wil,
1327 			 "Boot Loader struct v%d: MAC = %pM RF = 0x%04x (status 0x%04x) bband = 0x%08x\n",
1328 			 bl_ver, mac,
1329 			 bl.bl1.rf_type, rf_status,
1330 			 bl.bl1.baseband_type);
1331 		wil_info(wil, "Boot Loader build %d.%d.%d.%d\n",
1332 			 bl.bl1.bl_version_major, bl.bl1.bl_version_minor,
1333 			 bl.bl1.bl_version_subminor, bl.bl1.bl_version_build);
1334 	}
1335 
1336 	if (!is_valid_ether_addr(mac)) {
1337 		wil_err(wil, "BL: Invalid MAC %pM\n", mac);
1338 		return -EINVAL;
1339 	}
1340 
1341 	ether_addr_copy(ndev->perm_addr, mac);
1342 	ether_addr_copy(wiphy->perm_addr, mac);
1343 	if (!is_valid_ether_addr(ndev->dev_addr))
1344 		ether_addr_copy(ndev->dev_addr, mac);
1345 
1346 	if (rf_status) {/* bad RF cable? */
1347 		wil_err(wil, "RF communication error 0x%04x",
1348 			rf_status);
1349 		return -EAGAIN;
1350 	}
1351 
1352 	return 0;
1353 }
1354 
1355 static void wil_bl_crash_info(struct wil6210_priv *wil, bool is_err)
1356 {
1357 	u32 bl_assert_code, bl_assert_blink, bl_magic_number;
1358 	u32 bl_ver = wil_r(wil, RGF_USER_BL +
1359 			   offsetof(struct bl_dedicated_registers_v0,
1360 				    boot_loader_struct_version));
1361 
1362 	if (bl_ver < 2)
1363 		return;
1364 
1365 	bl_assert_code = wil_r(wil, RGF_USER_BL +
1366 			       offsetof(struct bl_dedicated_registers_v1,
1367 					bl_assert_code));
1368 	bl_assert_blink = wil_r(wil, RGF_USER_BL +
1369 				offsetof(struct bl_dedicated_registers_v1,
1370 					 bl_assert_blink));
1371 	bl_magic_number = wil_r(wil, RGF_USER_BL +
1372 				offsetof(struct bl_dedicated_registers_v1,
1373 					 bl_magic_number));
1374 
1375 	if (is_err) {
1376 		wil_err(wil,
1377 			"BL assert code 0x%08x blink 0x%08x magic 0x%08x\n",
1378 			bl_assert_code, bl_assert_blink, bl_magic_number);
1379 	} else {
1380 		wil_dbg_misc(wil,
1381 			     "BL assert code 0x%08x blink 0x%08x magic 0x%08x\n",
1382 			     bl_assert_code, bl_assert_blink, bl_magic_number);
1383 	}
1384 }
1385 
1386 static int wil_get_otp_info(struct wil6210_priv *wil)
1387 {
1388 	struct net_device *ndev = wil->main_ndev;
1389 	struct wiphy *wiphy = wil_to_wiphy(wil);
1390 	u8 mac[8];
1391 	int mac_addr;
1392 
1393 	if (wil->hw_version >= HW_VER_TALYN_MB)
1394 		mac_addr = RGF_OTP_MAC_TALYN_MB;
1395 	else
1396 		mac_addr = RGF_OTP_MAC;
1397 
1398 	wil_memcpy_fromio_32(mac, wil->csr + HOSTADDR(mac_addr),
1399 			     sizeof(mac));
1400 	if (!is_valid_ether_addr(mac)) {
1401 		wil_err(wil, "Invalid MAC %pM\n", mac);
1402 		return -EINVAL;
1403 	}
1404 
1405 	ether_addr_copy(ndev->perm_addr, mac);
1406 	ether_addr_copy(wiphy->perm_addr, mac);
1407 	if (!is_valid_ether_addr(ndev->dev_addr))
1408 		ether_addr_copy(ndev->dev_addr, mac);
1409 
1410 	return 0;
1411 }
1412 
1413 static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
1414 {
1415 	ulong to = msecs_to_jiffies(2000);
1416 	ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
1417 
1418 	if (0 == left) {
1419 		wil_err(wil, "Firmware not ready\n");
1420 		return -ETIME;
1421 	} else {
1422 		wil_info(wil, "FW ready after %d ms. HW version 0x%08x\n",
1423 			 jiffies_to_msecs(to-left), wil->hw_version);
1424 	}
1425 	return 0;
1426 }
1427 
1428 void wil_abort_scan(struct wil6210_vif *vif, bool sync)
1429 {
1430 	struct wil6210_priv *wil = vif_to_wil(vif);
1431 	int rc;
1432 	struct cfg80211_scan_info info = {
1433 		.aborted = true,
1434 	};
1435 
1436 	lockdep_assert_held(&wil->vif_mutex);
1437 
1438 	if (!vif->scan_request)
1439 		return;
1440 
1441 	wil_dbg_misc(wil, "Abort scan_request 0x%p\n", vif->scan_request);
1442 	del_timer_sync(&vif->scan_timer);
1443 	mutex_unlock(&wil->vif_mutex);
1444 	rc = wmi_abort_scan(vif);
1445 	if (!rc && sync)
1446 		wait_event_interruptible_timeout(wil->wq, !vif->scan_request,
1447 						 msecs_to_jiffies(
1448 						 WAIT_FOR_SCAN_ABORT_MS));
1449 
1450 	mutex_lock(&wil->vif_mutex);
1451 	if (vif->scan_request) {
1452 		cfg80211_scan_done(vif->scan_request, &info);
1453 		vif->scan_request = NULL;
1454 	}
1455 }
1456 
1457 void wil_abort_scan_all_vifs(struct wil6210_priv *wil, bool sync)
1458 {
1459 	int i;
1460 
1461 	lockdep_assert_held(&wil->vif_mutex);
1462 
1463 	for (i = 0; i < wil->max_vifs; i++) {
1464 		struct wil6210_vif *vif = wil->vifs[i];
1465 
1466 		if (vif)
1467 			wil_abort_scan(vif, sync);
1468 	}
1469 }
1470 
1471 int wil_ps_update(struct wil6210_priv *wil, enum wmi_ps_profile_type ps_profile)
1472 {
1473 	int rc;
1474 
1475 	if (!test_bit(WMI_FW_CAPABILITY_PS_CONFIG, wil->fw_capabilities)) {
1476 		wil_err(wil, "set_power_mgmt not supported\n");
1477 		return -EOPNOTSUPP;
1478 	}
1479 
1480 	rc  = wmi_ps_dev_profile_cfg(wil, ps_profile);
1481 	if (rc)
1482 		wil_err(wil, "wmi_ps_dev_profile_cfg failed (%d)\n", rc);
1483 	else
1484 		wil->ps_profile = ps_profile;
1485 
1486 	return rc;
1487 }
1488 
1489 static void wil_pre_fw_config(struct wil6210_priv *wil)
1490 {
1491 	/* Mark FW as loaded from host */
1492 	wil_s(wil, RGF_USER_USAGE_6, 1);
1493 
1494 	/* clear any interrupts which on-card-firmware
1495 	 * may have set
1496 	 */
1497 	wil6210_clear_irq(wil);
1498 	/* CAF_ICR - clear and mask */
1499 	/* it is W1C, clear by writing back same value */
1500 	if (wil->hw_version < HW_VER_TALYN_MB) {
1501 		wil_s(wil, RGF_CAF_ICR + offsetof(struct RGF_ICR, ICR), 0);
1502 		wil_w(wil, RGF_CAF_ICR + offsetof(struct RGF_ICR, IMV), ~0);
1503 	} else {
1504 		wil_s(wil,
1505 		      RGF_CAF_ICR_TALYN_MB + offsetof(struct RGF_ICR, ICR), 0);
1506 		wil_w(wil, RGF_CAF_ICR_TALYN_MB +
1507 		      offsetof(struct RGF_ICR, IMV), ~0);
1508 	}
1509 	/* clear PAL_UNIT_ICR (potential D0->D3 leftover)
1510 	 * In Talyn-MB host cannot access this register due to
1511 	 * access control, hence PAL_UNIT_ICR is cleared by the FW
1512 	 */
1513 	if (wil->hw_version < HW_VER_TALYN_MB)
1514 		wil_s(wil, RGF_PAL_UNIT_ICR + offsetof(struct RGF_ICR, ICR),
1515 		      0);
1516 
1517 	if (wil->fw_calib_result > 0) {
1518 		__le32 val = cpu_to_le32(wil->fw_calib_result |
1519 						(CALIB_RESULT_SIGNATURE << 8));
1520 		wil_w(wil, RGF_USER_FW_CALIB_RESULT, (u32 __force)val);
1521 	}
1522 }
1523 
1524 static int wil_restore_vifs(struct wil6210_priv *wil)
1525 {
1526 	struct wil6210_vif *vif;
1527 	struct net_device *ndev;
1528 	struct wireless_dev *wdev;
1529 	int i, rc;
1530 
1531 	for (i = 0; i < wil->max_vifs; i++) {
1532 		vif = wil->vifs[i];
1533 		if (!vif)
1534 			continue;
1535 		vif->ap_isolate = 0;
1536 		if (vif->mid) {
1537 			ndev = vif_to_ndev(vif);
1538 			wdev = vif_to_wdev(vif);
1539 			rc = wmi_port_allocate(wil, vif->mid, ndev->dev_addr,
1540 					       wdev->iftype);
1541 			if (rc) {
1542 				wil_err(wil, "fail to restore VIF %d type %d, rc %d\n",
1543 					i, wdev->iftype, rc);
1544 				return rc;
1545 			}
1546 		}
1547 	}
1548 
1549 	return 0;
1550 }
1551 
1552 /*
1553  * We reset all the structures, and we reset the UMAC.
1554  * After calling this routine, you're expected to reload
1555  * the firmware.
1556  */
1557 int wil_reset(struct wil6210_priv *wil, bool load_fw)
1558 {
1559 	int rc, i;
1560 	unsigned long status_flags = BIT(wil_status_resetting);
1561 	int no_flash;
1562 	struct wil6210_vif *vif;
1563 
1564 	wil_dbg_misc(wil, "reset\n");
1565 
1566 	WARN_ON(!mutex_is_locked(&wil->mutex));
1567 	WARN_ON(test_bit(wil_status_napi_en, wil->status));
1568 
1569 	if (debug_fw) {
1570 		static const u8 mac[ETH_ALEN] = {
1571 			0x00, 0xde, 0xad, 0x12, 0x34, 0x56,
1572 		};
1573 		struct net_device *ndev = wil->main_ndev;
1574 
1575 		ether_addr_copy(ndev->perm_addr, mac);
1576 		ether_addr_copy(ndev->dev_addr, ndev->perm_addr);
1577 		return 0;
1578 	}
1579 
1580 	if (wil->hw_version == HW_VER_UNKNOWN)
1581 		return -ENODEV;
1582 
1583 	if (test_bit(WIL_PLATFORM_CAPA_T_PWR_ON_0, wil->platform_capa)) {
1584 		wil_dbg_misc(wil, "Notify FW to set T_POWER_ON=0\n");
1585 		wil_s(wil, RGF_USER_USAGE_8, BIT_USER_SUPPORT_T_POWER_ON_0);
1586 	}
1587 
1588 	if (test_bit(WIL_PLATFORM_CAPA_EXT_CLK, wil->platform_capa)) {
1589 		wil_dbg_misc(wil, "Notify FW on ext clock configuration\n");
1590 		wil_s(wil, RGF_USER_USAGE_8, BIT_USER_EXT_CLK);
1591 	}
1592 
1593 	if (wil->platform_ops.notify) {
1594 		rc = wil->platform_ops.notify(wil->platform_handle,
1595 					      WIL_PLATFORM_EVT_PRE_RESET);
1596 		if (rc)
1597 			wil_err(wil, "PRE_RESET platform notify failed, rc %d\n",
1598 				rc);
1599 	}
1600 
1601 	set_bit(wil_status_resetting, wil->status);
1602 	if (test_bit(wil_status_collecting_dumps, wil->status)) {
1603 		/* Device collects crash dump, cancel the reset.
1604 		 * following crash dump collection, reset would take place.
1605 		 */
1606 		wil_dbg_misc(wil, "reject reset while collecting crash dump\n");
1607 		rc = -EBUSY;
1608 		goto out;
1609 	}
1610 
1611 	mutex_lock(&wil->vif_mutex);
1612 	wil_abort_scan_all_vifs(wil, false);
1613 	mutex_unlock(&wil->vif_mutex);
1614 
1615 	for (i = 0; i < wil->max_vifs; i++) {
1616 		vif = wil->vifs[i];
1617 		if (vif) {
1618 			cancel_work_sync(&vif->disconnect_worker);
1619 			wil6210_disconnect(vif, NULL,
1620 					   WLAN_REASON_DEAUTH_LEAVING);
1621 		}
1622 	}
1623 	wil_bcast_fini_all(wil);
1624 
1625 	/* Disable device led before reset*/
1626 	wmi_led_cfg(wil, false);
1627 
1628 	/* prevent NAPI from being scheduled and prevent wmi commands */
1629 	mutex_lock(&wil->wmi_mutex);
1630 	if (test_bit(wil_status_suspending, wil->status))
1631 		status_flags |= BIT(wil_status_suspending);
1632 	bitmap_and(wil->status, wil->status, &status_flags,
1633 		   wil_status_last);
1634 	wil_dbg_misc(wil, "wil->status (0x%lx)\n", *wil->status);
1635 	mutex_unlock(&wil->wmi_mutex);
1636 
1637 	wil_mask_irq(wil);
1638 
1639 	wmi_event_flush(wil);
1640 
1641 	flush_workqueue(wil->wq_service);
1642 	flush_workqueue(wil->wmi_wq);
1643 
1644 	no_flash = test_bit(hw_capa_no_flash, wil->hw_capa);
1645 	if (!no_flash)
1646 		wil_bl_crash_info(wil, false);
1647 	wil_disable_irq(wil);
1648 	rc = wil_target_reset(wil, no_flash);
1649 	wil6210_clear_irq(wil);
1650 	wil_enable_irq(wil);
1651 	wil->txrx_ops.rx_fini(wil);
1652 	wil->txrx_ops.tx_fini(wil);
1653 	if (rc) {
1654 		if (!no_flash)
1655 			wil_bl_crash_info(wil, true);
1656 		goto out;
1657 	}
1658 
1659 	if (no_flash) {
1660 		rc = wil_get_otp_info(wil);
1661 	} else {
1662 		rc = wil_get_bl_info(wil);
1663 		if (rc == -EAGAIN && !load_fw)
1664 			/* ignore RF error if not going up */
1665 			rc = 0;
1666 	}
1667 	if (rc)
1668 		goto out;
1669 
1670 	wil_set_oob_mode(wil, oob_mode);
1671 	if (load_fw) {
1672 		char board_file[WIL_BOARD_FILE_MAX_NAMELEN];
1673 
1674 		if  (wil->secured_boot) {
1675 			wil_err(wil, "secured boot is not supported\n");
1676 			return -ENOTSUPP;
1677 		}
1678 
1679 		board_file[0] = '\0';
1680 		wil_get_board_file(wil, board_file, sizeof(board_file));
1681 		wil_info(wil, "Use firmware <%s> + board <%s>\n",
1682 			 wil->wil_fw_name, board_file);
1683 
1684 		if (!no_flash)
1685 			wil_bl_prepare_halt(wil);
1686 
1687 		wil_halt_cpu(wil);
1688 		memset(wil->fw_version, 0, sizeof(wil->fw_version));
1689 		/* Loading f/w from the file */
1690 		rc = wil_request_firmware(wil, wil->wil_fw_name, true);
1691 		if (rc)
1692 			goto out;
1693 		if (wil->brd_file_addr)
1694 			rc = wil_request_board(wil, board_file);
1695 		else
1696 			rc = wil_request_firmware(wil, board_file, true);
1697 		if (rc)
1698 			goto out;
1699 
1700 		wil_pre_fw_config(wil);
1701 		wil_release_cpu(wil);
1702 	}
1703 
1704 	/* init after reset */
1705 	reinit_completion(&wil->wmi_ready);
1706 	reinit_completion(&wil->wmi_call);
1707 	reinit_completion(&wil->halp.comp);
1708 
1709 	clear_bit(wil_status_resetting, wil->status);
1710 
1711 	if (load_fw) {
1712 		wil_unmask_irq(wil);
1713 
1714 		/* we just started MAC, wait for FW ready */
1715 		rc = wil_wait_for_fw_ready(wil);
1716 		if (rc)
1717 			return rc;
1718 
1719 		/* check FW is responsive */
1720 		rc = wmi_echo(wil);
1721 		if (rc) {
1722 			wil_err(wil, "wmi_echo failed, rc %d\n", rc);
1723 			return rc;
1724 		}
1725 
1726 		wil->txrx_ops.configure_interrupt_moderation(wil);
1727 
1728 		/* Enable OFU rdy valid bug fix, to prevent hang in oful34_rx
1729 		 * while there is back-pressure from Host during RX
1730 		 */
1731 		if (wil->hw_version >= HW_VER_TALYN_MB)
1732 			wil_s(wil, RGF_DMA_MISC_CTL,
1733 			      BIT_OFUL34_RDY_VALID_BUG_FIX_EN);
1734 
1735 		rc = wil_restore_vifs(wil);
1736 		if (rc) {
1737 			wil_err(wil, "failed to restore vifs, rc %d\n", rc);
1738 			return rc;
1739 		}
1740 
1741 		wil_collect_fw_info(wil);
1742 
1743 		if (wil->ps_profile != WMI_PS_PROFILE_TYPE_DEFAULT)
1744 			wil_ps_update(wil, wil->ps_profile);
1745 
1746 		if (wil->platform_ops.notify) {
1747 			rc = wil->platform_ops.notify(wil->platform_handle,
1748 						      WIL_PLATFORM_EVT_FW_RDY);
1749 			if (rc) {
1750 				wil_err(wil, "FW_RDY notify failed, rc %d\n",
1751 					rc);
1752 				rc = 0;
1753 			}
1754 		}
1755 	}
1756 
1757 	return rc;
1758 
1759 out:
1760 	clear_bit(wil_status_resetting, wil->status);
1761 	return rc;
1762 }
1763 
1764 void wil_fw_error_recovery(struct wil6210_priv *wil)
1765 {
1766 	wil_dbg_misc(wil, "starting fw error recovery\n");
1767 
1768 	if (test_bit(wil_status_resetting, wil->status)) {
1769 		wil_info(wil, "Reset already in progress\n");
1770 		return;
1771 	}
1772 
1773 	wil->recovery_state = fw_recovery_pending;
1774 	schedule_work(&wil->fw_error_worker);
1775 }
1776 
1777 int __wil_up(struct wil6210_priv *wil)
1778 {
1779 	struct net_device *ndev = wil->main_ndev;
1780 	struct wireless_dev *wdev = ndev->ieee80211_ptr;
1781 	int rc;
1782 
1783 	WARN_ON(!mutex_is_locked(&wil->mutex));
1784 
1785 	rc = wil_reset(wil, true);
1786 	if (rc)
1787 		return rc;
1788 
1789 	/* Rx RING. After MAC and beacon */
1790 	if (rx_ring_order == 0)
1791 		rx_ring_order = wil->hw_version < HW_VER_TALYN_MB ?
1792 			WIL_RX_RING_SIZE_ORDER_DEFAULT :
1793 			WIL_RX_RING_SIZE_ORDER_TALYN_DEFAULT;
1794 
1795 	rc = wil->txrx_ops.rx_init(wil, rx_ring_order);
1796 	if (rc)
1797 		return rc;
1798 
1799 	rc = wil->txrx_ops.tx_init(wil);
1800 	if (rc)
1801 		return rc;
1802 
1803 	switch (wdev->iftype) {
1804 	case NL80211_IFTYPE_STATION:
1805 		wil_dbg_misc(wil, "type: STATION\n");
1806 		ndev->type = ARPHRD_ETHER;
1807 		break;
1808 	case NL80211_IFTYPE_AP:
1809 		wil_dbg_misc(wil, "type: AP\n");
1810 		ndev->type = ARPHRD_ETHER;
1811 		break;
1812 	case NL80211_IFTYPE_P2P_CLIENT:
1813 		wil_dbg_misc(wil, "type: P2P_CLIENT\n");
1814 		ndev->type = ARPHRD_ETHER;
1815 		break;
1816 	case NL80211_IFTYPE_P2P_GO:
1817 		wil_dbg_misc(wil, "type: P2P_GO\n");
1818 		ndev->type = ARPHRD_ETHER;
1819 		break;
1820 	case NL80211_IFTYPE_MONITOR:
1821 		wil_dbg_misc(wil, "type: Monitor\n");
1822 		ndev->type = ARPHRD_IEEE80211_RADIOTAP;
1823 		/* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
1824 		break;
1825 	default:
1826 		return -EOPNOTSUPP;
1827 	}
1828 
1829 	/* MAC address - pre-requisite for other commands */
1830 	wmi_set_mac_address(wil, ndev->dev_addr);
1831 
1832 	wil_dbg_misc(wil, "NAPI enable\n");
1833 	napi_enable(&wil->napi_rx);
1834 	napi_enable(&wil->napi_tx);
1835 	set_bit(wil_status_napi_en, wil->status);
1836 
1837 	wil6210_bus_request(wil, WIL_DEFAULT_BUS_REQUEST_KBPS);
1838 
1839 	return 0;
1840 }
1841 
1842 int wil_up(struct wil6210_priv *wil)
1843 {
1844 	int rc;
1845 
1846 	wil_dbg_misc(wil, "up\n");
1847 
1848 	mutex_lock(&wil->mutex);
1849 	rc = __wil_up(wil);
1850 	mutex_unlock(&wil->mutex);
1851 
1852 	return rc;
1853 }
1854 
1855 int __wil_down(struct wil6210_priv *wil)
1856 {
1857 	WARN_ON(!mutex_is_locked(&wil->mutex));
1858 
1859 	set_bit(wil_status_resetting, wil->status);
1860 
1861 	wil6210_bus_request(wil, 0);
1862 
1863 	wil_disable_irq(wil);
1864 	if (test_and_clear_bit(wil_status_napi_en, wil->status)) {
1865 		napi_disable(&wil->napi_rx);
1866 		napi_disable(&wil->napi_tx);
1867 		wil_dbg_misc(wil, "NAPI disable\n");
1868 	}
1869 	wil_enable_irq(wil);
1870 
1871 	mutex_lock(&wil->vif_mutex);
1872 	wil_p2p_stop_radio_operations(wil);
1873 	wil_abort_scan_all_vifs(wil, false);
1874 	mutex_unlock(&wil->vif_mutex);
1875 
1876 	return wil_reset(wil, false);
1877 }
1878 
1879 int wil_down(struct wil6210_priv *wil)
1880 {
1881 	int rc;
1882 
1883 	wil_dbg_misc(wil, "down\n");
1884 
1885 	wil_set_recovery_state(wil, fw_recovery_idle);
1886 	mutex_lock(&wil->mutex);
1887 	rc = __wil_down(wil);
1888 	mutex_unlock(&wil->mutex);
1889 
1890 	return rc;
1891 }
1892 
1893 int wil_find_cid(struct wil6210_priv *wil, u8 mid, const u8 *mac)
1894 {
1895 	int i;
1896 	int rc = -ENOENT;
1897 
1898 	for (i = 0; i < max_assoc_sta; i++) {
1899 		if (wil->sta[i].mid == mid &&
1900 		    wil->sta[i].status != wil_sta_unused &&
1901 		    ether_addr_equal(wil->sta[i].addr, mac)) {
1902 			rc = i;
1903 			break;
1904 		}
1905 	}
1906 
1907 	return rc;
1908 }
1909 
1910 void wil_halp_vote(struct wil6210_priv *wil)
1911 {
1912 	unsigned long rc;
1913 	unsigned long to_jiffies = msecs_to_jiffies(WAIT_FOR_HALP_VOTE_MS);
1914 
1915 	mutex_lock(&wil->halp.lock);
1916 
1917 	wil_dbg_irq(wil, "halp_vote: start, HALP ref_cnt (%d)\n",
1918 		    wil->halp.ref_cnt);
1919 
1920 	if (++wil->halp.ref_cnt == 1) {
1921 		reinit_completion(&wil->halp.comp);
1922 		/* mark to IRQ context to handle HALP ICR */
1923 		wil->halp.handle_icr = true;
1924 		wil6210_set_halp(wil);
1925 		rc = wait_for_completion_timeout(&wil->halp.comp, to_jiffies);
1926 		if (!rc) {
1927 			wil_err(wil, "HALP vote timed out\n");
1928 			/* Mask HALP as done in case the interrupt is raised */
1929 			wil->halp.handle_icr = false;
1930 			wil6210_mask_halp(wil);
1931 		} else {
1932 			wil_dbg_irq(wil,
1933 				    "halp_vote: HALP vote completed after %d ms\n",
1934 				    jiffies_to_msecs(to_jiffies - rc));
1935 		}
1936 	}
1937 
1938 	wil_dbg_irq(wil, "halp_vote: end, HALP ref_cnt (%d)\n",
1939 		    wil->halp.ref_cnt);
1940 
1941 	mutex_unlock(&wil->halp.lock);
1942 }
1943 
1944 void wil_halp_unvote(struct wil6210_priv *wil)
1945 {
1946 	WARN_ON(wil->halp.ref_cnt == 0);
1947 
1948 	mutex_lock(&wil->halp.lock);
1949 
1950 	wil_dbg_irq(wil, "halp_unvote: start, HALP ref_cnt (%d)\n",
1951 		    wil->halp.ref_cnt);
1952 
1953 	if (--wil->halp.ref_cnt == 0) {
1954 		wil6210_clear_halp(wil);
1955 		wil_dbg_irq(wil, "HALP unvote\n");
1956 	}
1957 
1958 	wil_dbg_irq(wil, "halp_unvote:end, HALP ref_cnt (%d)\n",
1959 		    wil->halp.ref_cnt);
1960 
1961 	mutex_unlock(&wil->halp.lock);
1962 }
1963 
1964 void wil_init_txrx_ops(struct wil6210_priv *wil)
1965 {
1966 	if (wil->use_enhanced_dma_hw)
1967 		wil_init_txrx_ops_edma(wil);
1968 	else
1969 		wil_init_txrx_ops_legacy_dma(wil);
1970 }
1971