1 /*
2  * Copyright (c) 2012-2017 Qualcomm Atheros, Inc.
3  * Copyright (c) 2018, 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 
22 #include "wil6210.h"
23 #include "txrx.h"
24 #include "wmi.h"
25 #include "boot_loader.h"
26 
27 #define WAIT_FOR_HALP_VOTE_MS 100
28 #define WAIT_FOR_SCAN_ABORT_MS 1000
29 
30 bool debug_fw; /* = false; */
31 module_param(debug_fw, bool, 0444);
32 MODULE_PARM_DESC(debug_fw, " do not perform card reset. For FW debug");
33 
34 static u8 oob_mode;
35 module_param(oob_mode, byte, 0444);
36 MODULE_PARM_DESC(oob_mode,
37 		 " enable out of the box (OOB) mode in FW, for diagnostics and certification");
38 
39 bool no_fw_recovery;
40 module_param(no_fw_recovery, bool, 0644);
41 MODULE_PARM_DESC(no_fw_recovery, " disable automatic FW error recovery");
42 
43 /* if not set via modparam, will be set to default value of 1/8 of
44  * rx ring size during init flow
45  */
46 unsigned short rx_ring_overflow_thrsh = WIL6210_RX_HIGH_TRSH_INIT;
47 module_param(rx_ring_overflow_thrsh, ushort, 0444);
48 MODULE_PARM_DESC(rx_ring_overflow_thrsh,
49 		 " RX ring overflow threshold in descriptors.");
50 
51 /* We allow allocation of more than 1 page buffers to support large packets.
52  * It is suboptimal behavior performance wise in case MTU above page size.
53  */
54 unsigned int mtu_max = TXRX_BUF_LEN_DEFAULT - WIL_MAX_MPDU_OVERHEAD;
55 static int mtu_max_set(const char *val, const struct kernel_param *kp)
56 {
57 	int ret;
58 
59 	/* sets mtu_max directly. no need to restore it in case of
60 	 * illegal value since we assume this will fail insmod
61 	 */
62 	ret = param_set_uint(val, kp);
63 	if (ret)
64 		return ret;
65 
66 	if (mtu_max < 68 || mtu_max > WIL_MAX_ETH_MTU)
67 		ret = -EINVAL;
68 
69 	return ret;
70 }
71 
72 static const struct kernel_param_ops mtu_max_ops = {
73 	.set = mtu_max_set,
74 	.get = param_get_uint,
75 };
76 
77 module_param_cb(mtu_max, &mtu_max_ops, &mtu_max, 0444);
78 MODULE_PARM_DESC(mtu_max, " Max MTU value.");
79 
80 static uint rx_ring_order = WIL_RX_RING_SIZE_ORDER_DEFAULT;
81 static uint tx_ring_order = WIL_TX_RING_SIZE_ORDER_DEFAULT;
82 static uint bcast_ring_order = WIL_BCAST_RING_SIZE_ORDER_DEFAULT;
83 
84 static int ring_order_set(const char *val, const struct kernel_param *kp)
85 {
86 	int ret;
87 	uint x;
88 
89 	ret = kstrtouint(val, 0, &x);
90 	if (ret)
91 		return ret;
92 
93 	if ((x < WIL_RING_SIZE_ORDER_MIN) || (x > WIL_RING_SIZE_ORDER_MAX))
94 		return -EINVAL;
95 
96 	*((uint *)kp->arg) = x;
97 
98 	return 0;
99 }
100 
101 static const struct kernel_param_ops ring_order_ops = {
102 	.set = ring_order_set,
103 	.get = param_get_uint,
104 };
105 
106 module_param_cb(rx_ring_order, &ring_order_ops, &rx_ring_order, 0444);
107 MODULE_PARM_DESC(rx_ring_order, " Rx ring order; size = 1 << order");
108 module_param_cb(tx_ring_order, &ring_order_ops, &tx_ring_order, 0444);
109 MODULE_PARM_DESC(tx_ring_order, " Tx ring order; size = 1 << order");
110 module_param_cb(bcast_ring_order, &ring_order_ops, &bcast_ring_order, 0444);
111 MODULE_PARM_DESC(bcast_ring_order, " Bcast ring order; size = 1 << order");
112 
113 #define RST_DELAY (20) /* msec, for loop in @wil_target_reset */
114 #define RST_COUNT (1 + 1000/RST_DELAY) /* round up to be above 1 sec total */
115 
116 /*
117  * Due to a hardware issue,
118  * one has to read/write to/from NIC in 32-bit chunks;
119  * regular memcpy_fromio and siblings will
120  * not work on 64-bit platform - it uses 64-bit transactions
121  *
122  * Force 32-bit transactions to enable NIC on 64-bit platforms
123  *
124  * To avoid byte swap on big endian host, __raw_{read|write}l
125  * should be used - {read|write}l would swap bytes to provide
126  * little endian on PCI value in host endianness.
127  */
128 void wil_memcpy_fromio_32(void *dst, const volatile void __iomem *src,
129 			  size_t count)
130 {
131 	u32 *d = dst;
132 	const volatile u32 __iomem *s = src;
133 
134 	for (; count >= 4; count -= 4)
135 		*d++ = __raw_readl(s++);
136 
137 	if (unlikely(count)) {
138 		/* count can be 1..3 */
139 		u32 tmp = __raw_readl(s);
140 
141 		memcpy(d, &tmp, count);
142 	}
143 }
144 
145 void wil_memcpy_toio_32(volatile void __iomem *dst, const void *src,
146 			size_t count)
147 {
148 	volatile u32 __iomem *d = dst;
149 	const u32 *s = src;
150 
151 	for (; count >= 4; count -= 4)
152 		__raw_writel(*s++, d++);
153 
154 	if (unlikely(count)) {
155 		/* count can be 1..3 */
156 		u32 tmp = 0;
157 
158 		memcpy(&tmp, s, count);
159 		__raw_writel(tmp, d);
160 	}
161 }
162 
163 static void wil_disconnect_cid(struct wil6210_vif *vif, int cid,
164 			       u16 reason_code, bool from_event)
165 __acquires(&sta->tid_rx_lock) __releases(&sta->tid_rx_lock)
166 {
167 	uint i;
168 	struct wil6210_priv *wil = vif_to_wil(vif);
169 	struct net_device *ndev = vif_to_ndev(vif);
170 	struct wireless_dev *wdev = vif_to_wdev(vif);
171 	struct wil_sta_info *sta = &wil->sta[cid];
172 
173 	might_sleep();
174 	wil_dbg_misc(wil, "disconnect_cid: CID %d, MID %d, status %d\n",
175 		     cid, sta->mid, sta->status);
176 	/* inform upper/lower layers */
177 	if (sta->status != wil_sta_unused) {
178 		if (vif->mid != sta->mid) {
179 			wil_err(wil, "STA MID mismatch with VIF MID(%d)\n",
180 				vif->mid);
181 			/* let FW override sta->mid but be more strict with
182 			 * user space requests
183 			 */
184 			if (!from_event)
185 				return;
186 		}
187 		if (!from_event) {
188 			bool del_sta = (wdev->iftype == NL80211_IFTYPE_AP) ?
189 						disable_ap_sme : false;
190 			wmi_disconnect_sta(vif, sta->addr, reason_code,
191 					   true, del_sta);
192 		}
193 
194 		switch (wdev->iftype) {
195 		case NL80211_IFTYPE_AP:
196 		case NL80211_IFTYPE_P2P_GO:
197 			/* AP-like interface */
198 			cfg80211_del_sta(ndev, sta->addr, GFP_KERNEL);
199 			break;
200 		default:
201 			break;
202 		}
203 		sta->status = wil_sta_unused;
204 		sta->mid = U8_MAX;
205 	}
206 	/* reorder buffers */
207 	for (i = 0; i < WIL_STA_TID_NUM; i++) {
208 		struct wil_tid_ampdu_rx *r;
209 
210 		spin_lock_bh(&sta->tid_rx_lock);
211 
212 		r = sta->tid_rx[i];
213 		sta->tid_rx[i] = NULL;
214 		wil_tid_ampdu_rx_free(wil, r);
215 
216 		spin_unlock_bh(&sta->tid_rx_lock);
217 	}
218 	/* crypto context */
219 	memset(sta->tid_crypto_rx, 0, sizeof(sta->tid_crypto_rx));
220 	memset(&sta->group_crypto_rx, 0, sizeof(sta->group_crypto_rx));
221 	/* release vrings */
222 	for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++) {
223 		if (wil->vring2cid_tid[i][0] == cid)
224 			wil_vring_fini_tx(wil, i);
225 	}
226 	/* statistics */
227 	memset(&sta->stats, 0, sizeof(sta->stats));
228 }
229 
230 static bool wil_vif_is_connected(struct wil6210_priv *wil, u8 mid)
231 {
232 	int i;
233 
234 	for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
235 		if (wil->sta[i].mid == mid &&
236 		    wil->sta[i].status == wil_sta_connected)
237 			return true;
238 	}
239 
240 	return false;
241 }
242 
243 static void _wil6210_disconnect(struct wil6210_vif *vif, const u8 *bssid,
244 				u16 reason_code, bool from_event)
245 {
246 	struct wil6210_priv *wil = vif_to_wil(vif);
247 	int cid = -ENOENT;
248 	struct net_device *ndev;
249 	struct wireless_dev *wdev;
250 
251 	if (unlikely(!vif))
252 		return;
253 
254 	ndev = vif_to_ndev(vif);
255 	wdev = vif_to_wdev(vif);
256 
257 	might_sleep();
258 	wil_info(wil, "bssid=%pM, reason=%d, ev%s\n", bssid,
259 		 reason_code, from_event ? "+" : "-");
260 
261 	/* Cases are:
262 	 * - disconnect single STA, still connected
263 	 * - disconnect single STA, already disconnected
264 	 * - disconnect all
265 	 *
266 	 * For "disconnect all", there are 3 options:
267 	 * - bssid == NULL
268 	 * - bssid is broadcast address (ff:ff:ff:ff:ff:ff)
269 	 * - bssid is our MAC address
270 	 */
271 	if (bssid && !is_broadcast_ether_addr(bssid) &&
272 	    !ether_addr_equal_unaligned(ndev->dev_addr, bssid)) {
273 		cid = wil_find_cid(wil, vif->mid, bssid);
274 		wil_dbg_misc(wil, "Disconnect %pM, CID=%d, reason=%d\n",
275 			     bssid, cid, reason_code);
276 		if (cid >= 0) /* disconnect 1 peer */
277 			wil_disconnect_cid(vif, cid, reason_code, from_event);
278 	} else { /* all */
279 		wil_dbg_misc(wil, "Disconnect all\n");
280 		for (cid = 0; cid < WIL6210_MAX_CID; cid++)
281 			wil_disconnect_cid(vif, cid, reason_code, from_event);
282 	}
283 
284 	/* link state */
285 	switch (wdev->iftype) {
286 	case NL80211_IFTYPE_STATION:
287 	case NL80211_IFTYPE_P2P_CLIENT:
288 		wil_bcast_fini(vif);
289 		wil_update_net_queues_bh(wil, vif, NULL, true);
290 		netif_carrier_off(ndev);
291 		if (!wil_has_other_active_ifaces(wil, ndev, false, true))
292 			wil6210_bus_request(wil, WIL_DEFAULT_BUS_REQUEST_KBPS);
293 
294 		if (test_and_clear_bit(wil_vif_fwconnected, vif->status)) {
295 			atomic_dec(&wil->connected_vifs);
296 			cfg80211_disconnected(ndev, reason_code,
297 					      NULL, 0,
298 					      vif->locally_generated_disc,
299 					      GFP_KERNEL);
300 			vif->locally_generated_disc = false;
301 		} else if (test_bit(wil_vif_fwconnecting, vif->status)) {
302 			cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
303 						WLAN_STATUS_UNSPECIFIED_FAILURE,
304 						GFP_KERNEL);
305 			vif->bss = NULL;
306 		}
307 		clear_bit(wil_vif_fwconnecting, vif->status);
308 		break;
309 	case NL80211_IFTYPE_AP:
310 	case NL80211_IFTYPE_P2P_GO:
311 		if (!wil_vif_is_connected(wil, vif->mid)) {
312 			wil_update_net_queues_bh(wil, vif, NULL, true);
313 			if (test_and_clear_bit(wil_vif_fwconnected,
314 					       vif->status))
315 				atomic_dec(&wil->connected_vifs);
316 		} else {
317 			wil_update_net_queues_bh(wil, vif, NULL, false);
318 		}
319 		break;
320 	default:
321 		break;
322 	}
323 }
324 
325 void wil_disconnect_worker(struct work_struct *work)
326 {
327 	struct wil6210_vif *vif = container_of(work,
328 			struct wil6210_vif, disconnect_worker);
329 	struct wil6210_priv *wil = vif_to_wil(vif);
330 	struct net_device *ndev = vif_to_ndev(vif);
331 	int rc;
332 	struct {
333 		struct wmi_cmd_hdr wmi;
334 		struct wmi_disconnect_event evt;
335 	} __packed reply;
336 
337 	if (test_bit(wil_vif_fwconnected, vif->status))
338 		/* connect succeeded after all */
339 		return;
340 
341 	if (!test_bit(wil_vif_fwconnecting, vif->status))
342 		/* already disconnected */
343 		return;
344 
345 	rc = wmi_call(wil, WMI_DISCONNECT_CMDID, vif->mid, NULL, 0,
346 		      WMI_DISCONNECT_EVENTID, &reply, sizeof(reply),
347 		      WIL6210_DISCONNECT_TO_MS);
348 	if (rc) {
349 		wil_err(wil, "disconnect error %d\n", rc);
350 		return;
351 	}
352 
353 	wil_update_net_queues_bh(wil, vif, NULL, true);
354 	netif_carrier_off(ndev);
355 	cfg80211_connect_result(ndev, NULL, NULL, 0, NULL, 0,
356 				WLAN_STATUS_UNSPECIFIED_FAILURE, GFP_KERNEL);
357 	clear_bit(wil_vif_fwconnecting, vif->status);
358 }
359 
360 static int wil_wait_for_recovery(struct wil6210_priv *wil)
361 {
362 	if (wait_event_interruptible(wil->wq, wil->recovery_state !=
363 				     fw_recovery_pending)) {
364 		wil_err(wil, "Interrupt, canceling recovery\n");
365 		return -ERESTARTSYS;
366 	}
367 	if (wil->recovery_state != fw_recovery_running) {
368 		wil_info(wil, "Recovery cancelled\n");
369 		return -EINTR;
370 	}
371 	wil_info(wil, "Proceed with recovery\n");
372 	return 0;
373 }
374 
375 void wil_set_recovery_state(struct wil6210_priv *wil, int state)
376 {
377 	wil_dbg_misc(wil, "set_recovery_state: %d -> %d\n",
378 		     wil->recovery_state, state);
379 
380 	wil->recovery_state = state;
381 	wake_up_interruptible(&wil->wq);
382 }
383 
384 bool wil_is_recovery_blocked(struct wil6210_priv *wil)
385 {
386 	return no_fw_recovery && (wil->recovery_state == fw_recovery_pending);
387 }
388 
389 static void wil_fw_error_worker(struct work_struct *work)
390 {
391 	struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
392 						fw_error_worker);
393 	struct net_device *ndev = wil->main_ndev;
394 	struct wireless_dev *wdev;
395 
396 	wil_dbg_misc(wil, "fw error worker\n");
397 
398 	if (!ndev || !(ndev->flags & IFF_UP)) {
399 		wil_info(wil, "No recovery - interface is down\n");
400 		return;
401 	}
402 	wdev = ndev->ieee80211_ptr;
403 
404 	/* increment @recovery_count if less then WIL6210_FW_RECOVERY_TO
405 	 * passed since last recovery attempt
406 	 */
407 	if (time_is_after_jiffies(wil->last_fw_recovery +
408 				  WIL6210_FW_RECOVERY_TO))
409 		wil->recovery_count++;
410 	else
411 		wil->recovery_count = 1; /* fw was alive for a long time */
412 
413 	if (wil->recovery_count > WIL6210_FW_RECOVERY_RETRIES) {
414 		wil_err(wil, "too many recovery attempts (%d), giving up\n",
415 			wil->recovery_count);
416 		return;
417 	}
418 
419 	wil->last_fw_recovery = jiffies;
420 
421 	wil_info(wil, "fw error recovery requested (try %d)...\n",
422 		 wil->recovery_count);
423 	if (!no_fw_recovery)
424 		wil->recovery_state = fw_recovery_running;
425 	if (wil_wait_for_recovery(wil) != 0)
426 		return;
427 
428 	mutex_lock(&wil->mutex);
429 	/* Needs adaptation for multiple VIFs
430 	 * need to go over all VIFs and consider the appropriate
431 	 * recovery.
432 	 */
433 	switch (wdev->iftype) {
434 	case NL80211_IFTYPE_STATION:
435 	case NL80211_IFTYPE_P2P_CLIENT:
436 	case NL80211_IFTYPE_MONITOR:
437 		/* silent recovery, upper layers will see disconnect */
438 		__wil_down(wil);
439 		__wil_up(wil);
440 		break;
441 	case NL80211_IFTYPE_AP:
442 	case NL80211_IFTYPE_P2P_GO:
443 		wil_info(wil, "No recovery for AP-like interface\n");
444 		/* recovery in these modes is done by upper layers */
445 		break;
446 	default:
447 		wil_err(wil, "No recovery - unknown interface type %d\n",
448 			wdev->iftype);
449 		break;
450 	}
451 	mutex_unlock(&wil->mutex);
452 }
453 
454 static int wil_find_free_vring(struct wil6210_priv *wil)
455 {
456 	int i;
457 
458 	for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
459 		if (!wil->vring_tx[i].va)
460 			return i;
461 	}
462 	return -EINVAL;
463 }
464 
465 int wil_tx_init(struct wil6210_vif *vif, int cid)
466 {
467 	struct wil6210_priv *wil = vif_to_wil(vif);
468 	int rc = -EINVAL, ringid;
469 
470 	if (cid < 0) {
471 		wil_err(wil, "No connection pending\n");
472 		goto out;
473 	}
474 	ringid = wil_find_free_vring(wil);
475 	if (ringid < 0) {
476 		wil_err(wil, "No free vring found\n");
477 		goto out;
478 	}
479 
480 	wil_dbg_wmi(wil, "Configure for connection CID %d MID %d vring %d\n",
481 		    cid, vif->mid, ringid);
482 
483 	rc = wil_vring_init_tx(vif, ringid, 1 << tx_ring_order, cid, 0);
484 	if (rc)
485 		wil_err(wil, "init TX for CID %d MID %d vring %d failed\n",
486 			cid, vif->mid, ringid);
487 
488 out:
489 	return rc;
490 }
491 
492 int wil_bcast_init(struct wil6210_vif *vif)
493 {
494 	struct wil6210_priv *wil = vif_to_wil(vif);
495 	int ri = vif->bcast_vring, rc;
496 
497 	if ((ri >= 0) && wil->vring_tx[ri].va)
498 		return 0;
499 
500 	ri = wil_find_free_vring(wil);
501 	if (ri < 0)
502 		return ri;
503 
504 	vif->bcast_vring = ri;
505 	rc = wil_vring_init_bcast(vif, ri, 1 << bcast_ring_order);
506 	if (rc)
507 		vif->bcast_vring = -1;
508 
509 	return rc;
510 }
511 
512 void wil_bcast_fini(struct wil6210_vif *vif)
513 {
514 	struct wil6210_priv *wil = vif_to_wil(vif);
515 	int ri = vif->bcast_vring;
516 
517 	if (ri < 0)
518 		return;
519 
520 	vif->bcast_vring = -1;
521 	wil_vring_fini_tx(wil, ri);
522 }
523 
524 void wil_bcast_fini_all(struct wil6210_priv *wil)
525 {
526 	int i;
527 	struct wil6210_vif *vif;
528 
529 	for (i = 0; i < wil->max_vifs; i++) {
530 		vif = wil->vifs[i];
531 		if (vif)
532 			wil_bcast_fini(vif);
533 	}
534 }
535 
536 int wil_priv_init(struct wil6210_priv *wil)
537 {
538 	uint i;
539 
540 	wil_dbg_misc(wil, "priv_init\n");
541 
542 	memset(wil->sta, 0, sizeof(wil->sta));
543 	for (i = 0; i < WIL6210_MAX_CID; i++) {
544 		spin_lock_init(&wil->sta[i].tid_rx_lock);
545 		wil->sta[i].mid = U8_MAX;
546 	}
547 
548 	for (i = 0; i < WIL6210_MAX_TX_RINGS; i++)
549 		spin_lock_init(&wil->vring_tx_data[i].lock);
550 
551 	mutex_init(&wil->mutex);
552 	mutex_init(&wil->vif_mutex);
553 	mutex_init(&wil->wmi_mutex);
554 	mutex_init(&wil->halp.lock);
555 
556 	init_completion(&wil->wmi_ready);
557 	init_completion(&wil->wmi_call);
558 	init_completion(&wil->halp.comp);
559 
560 	INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
561 	INIT_WORK(&wil->fw_error_worker, wil_fw_error_worker);
562 
563 	INIT_LIST_HEAD(&wil->pending_wmi_ev);
564 	spin_lock_init(&wil->wmi_ev_lock);
565 	spin_lock_init(&wil->net_queue_lock);
566 	init_waitqueue_head(&wil->wq);
567 
568 	wil->wmi_wq = create_singlethread_workqueue(WIL_NAME "_wmi");
569 	if (!wil->wmi_wq)
570 		return -EAGAIN;
571 
572 	wil->wq_service = create_singlethread_workqueue(WIL_NAME "_service");
573 	if (!wil->wq_service)
574 		goto out_wmi_wq;
575 
576 	wil->last_fw_recovery = jiffies;
577 	wil->tx_interframe_timeout = WIL6210_ITR_TX_INTERFRAME_TIMEOUT_DEFAULT;
578 	wil->rx_interframe_timeout = WIL6210_ITR_RX_INTERFRAME_TIMEOUT_DEFAULT;
579 	wil->tx_max_burst_duration = WIL6210_ITR_TX_MAX_BURST_DURATION_DEFAULT;
580 	wil->rx_max_burst_duration = WIL6210_ITR_RX_MAX_BURST_DURATION_DEFAULT;
581 
582 	if (rx_ring_overflow_thrsh == WIL6210_RX_HIGH_TRSH_INIT)
583 		rx_ring_overflow_thrsh = WIL6210_RX_HIGH_TRSH_DEFAULT;
584 
585 	wil->ps_profile =  WMI_PS_PROFILE_TYPE_DEFAULT;
586 
587 	wil->wakeup_trigger = WMI_WAKEUP_TRIGGER_UCAST |
588 			      WMI_WAKEUP_TRIGGER_BCAST;
589 	memset(&wil->suspend_stats, 0, sizeof(wil->suspend_stats));
590 	wil->vring_idle_trsh = 16;
591 
592 	wil->reply_mid = U8_MAX;
593 	wil->max_vifs = 1;
594 
595 	return 0;
596 
597 out_wmi_wq:
598 	destroy_workqueue(wil->wmi_wq);
599 
600 	return -EAGAIN;
601 }
602 
603 void wil6210_bus_request(struct wil6210_priv *wil, u32 kbps)
604 {
605 	if (wil->platform_ops.bus_request) {
606 		wil->bus_request_kbps = kbps;
607 		wil->platform_ops.bus_request(wil->platform_handle, kbps);
608 	}
609 }
610 
611 /**
612  * wil6210_disconnect - disconnect one connection
613  * @vif: virtual interface context
614  * @bssid: peer to disconnect, NULL to disconnect all
615  * @reason_code: Reason code for the Disassociation frame
616  * @from_event: whether is invoked from FW event handler
617  *
618  * Disconnect and release associated resources. If invoked not from the
619  * FW event handler, issue WMI command(s) to trigger MAC disconnect.
620  */
621 void wil6210_disconnect(struct wil6210_vif *vif, const u8 *bssid,
622 			u16 reason_code, bool from_event)
623 {
624 	struct wil6210_priv *wil = vif_to_wil(vif);
625 
626 	wil_dbg_misc(wil, "disconnect\n");
627 
628 	del_timer_sync(&vif->connect_timer);
629 	_wil6210_disconnect(vif, bssid, reason_code, from_event);
630 }
631 
632 void wil_priv_deinit(struct wil6210_priv *wil)
633 {
634 	wil_dbg_misc(wil, "priv_deinit\n");
635 
636 	wil_set_recovery_state(wil, fw_recovery_idle);
637 	cancel_work_sync(&wil->fw_error_worker);
638 	wmi_event_flush(wil);
639 	destroy_workqueue(wil->wq_service);
640 	destroy_workqueue(wil->wmi_wq);
641 }
642 
643 static void wil_shutdown_bl(struct wil6210_priv *wil)
644 {
645 	u32 val;
646 
647 	wil_s(wil, RGF_USER_BL +
648 	      offsetof(struct bl_dedicated_registers_v1,
649 		       bl_shutdown_handshake), BL_SHUTDOWN_HS_GRTD);
650 
651 	usleep_range(100, 150);
652 
653 	val = wil_r(wil, RGF_USER_BL +
654 		    offsetof(struct bl_dedicated_registers_v1,
655 			     bl_shutdown_handshake));
656 	if (val & BL_SHUTDOWN_HS_RTD) {
657 		wil_dbg_misc(wil, "BL is ready for halt\n");
658 		return;
659 	}
660 
661 	wil_err(wil, "BL did not report ready for halt\n");
662 }
663 
664 /* this format is used by ARC embedded CPU for instruction memory */
665 static inline u32 ARC_me_imm32(u32 d)
666 {
667 	return ((d & 0xffff0000) >> 16) | ((d & 0x0000ffff) << 16);
668 }
669 
670 /* defines access to interrupt vectors for wil_freeze_bl */
671 #define ARC_IRQ_VECTOR_OFFSET(N)	((N) * 8)
672 /* ARC long jump instruction */
673 #define ARC_JAL_INST			(0x20200f80)
674 
675 static void wil_freeze_bl(struct wil6210_priv *wil)
676 {
677 	u32 jal, upc, saved;
678 	u32 ivt3 = ARC_IRQ_VECTOR_OFFSET(3);
679 
680 	jal = wil_r(wil, wil->iccm_base + ivt3);
681 	if (jal != ARC_me_imm32(ARC_JAL_INST)) {
682 		wil_dbg_misc(wil, "invalid IVT entry found, skipping\n");
683 		return;
684 	}
685 
686 	/* prevent the target from entering deep sleep
687 	 * and disabling memory access
688 	 */
689 	saved = wil_r(wil, RGF_USER_USAGE_8);
690 	wil_w(wil, RGF_USER_USAGE_8, saved | BIT_USER_PREVENT_DEEP_SLEEP);
691 	usleep_range(20, 25); /* let the BL process the bit */
692 
693 	/* redirect to endless loop in the INT_L1 context and let it trap */
694 	wil_w(wil, wil->iccm_base + ivt3 + 4, ARC_me_imm32(ivt3));
695 	usleep_range(20, 25); /* let the BL get into the trap */
696 
697 	/* verify the BL is frozen */
698 	upc = wil_r(wil, RGF_USER_CPU_PC);
699 	if (upc < ivt3 || (upc > (ivt3 + 8)))
700 		wil_dbg_misc(wil, "BL freeze failed, PC=0x%08X\n", upc);
701 
702 	wil_w(wil, RGF_USER_USAGE_8, saved);
703 }
704 
705 static void wil_bl_prepare_halt(struct wil6210_priv *wil)
706 {
707 	u32 tmp, ver;
708 
709 	/* before halting device CPU driver must make sure BL is not accessing
710 	 * host memory. This is done differently depending on BL version:
711 	 * 1. For very old BL versions the procedure is skipped
712 	 * (not supported).
713 	 * 2. For old BL version we use a special trick to freeze the BL
714 	 * 3. For new BL versions we shutdown the BL using handshake procedure.
715 	 */
716 	tmp = wil_r(wil, RGF_USER_BL +
717 		    offsetof(struct bl_dedicated_registers_v0,
718 			     boot_loader_struct_version));
719 	if (!tmp) {
720 		wil_dbg_misc(wil, "old BL, skipping halt preparation\n");
721 		return;
722 	}
723 
724 	tmp = wil_r(wil, RGF_USER_BL +
725 		    offsetof(struct bl_dedicated_registers_v1,
726 			     bl_shutdown_handshake));
727 	ver = BL_SHUTDOWN_HS_PROT_VER(tmp);
728 
729 	if (ver > 0)
730 		wil_shutdown_bl(wil);
731 	else
732 		wil_freeze_bl(wil);
733 }
734 
735 static inline void wil_halt_cpu(struct wil6210_priv *wil)
736 {
737 	wil_w(wil, RGF_USER_USER_CPU_0, BIT_USER_USER_CPU_MAN_RST);
738 	wil_w(wil, RGF_USER_MAC_CPU_0,  BIT_USER_MAC_CPU_MAN_RST);
739 }
740 
741 static inline void wil_release_cpu(struct wil6210_priv *wil)
742 {
743 	/* Start CPU */
744 	wil_w(wil, RGF_USER_USER_CPU_0, 1);
745 }
746 
747 static void wil_set_oob_mode(struct wil6210_priv *wil, u8 mode)
748 {
749 	wil_info(wil, "oob_mode to %d\n", mode);
750 	switch (mode) {
751 	case 0:
752 		wil_c(wil, RGF_USER_USAGE_6, BIT_USER_OOB_MODE |
753 		      BIT_USER_OOB_R2_MODE);
754 		break;
755 	case 1:
756 		wil_c(wil, RGF_USER_USAGE_6, BIT_USER_OOB_R2_MODE);
757 		wil_s(wil, RGF_USER_USAGE_6, BIT_USER_OOB_MODE);
758 		break;
759 	case 2:
760 		wil_c(wil, RGF_USER_USAGE_6, BIT_USER_OOB_MODE);
761 		wil_s(wil, RGF_USER_USAGE_6, BIT_USER_OOB_R2_MODE);
762 		break;
763 	default:
764 		wil_err(wil, "invalid oob_mode: %d\n", mode);
765 	}
766 }
767 
768 static int wil_target_reset(struct wil6210_priv *wil, int no_flash)
769 {
770 	int delay = 0;
771 	u32 x, x1 = 0;
772 
773 	wil_dbg_misc(wil, "Resetting \"%s\"...\n", wil->hw_name);
774 
775 	/* Clear MAC link up */
776 	wil_s(wil, RGF_HP_CTRL, BIT(15));
777 	wil_s(wil, RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_HPAL_PERST_FROM_PAD);
778 	wil_s(wil, RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_CAR_PERST_RST);
779 
780 	wil_halt_cpu(wil);
781 
782 	if (!no_flash) {
783 		/* clear all boot loader "ready" bits */
784 		wil_w(wil, RGF_USER_BL +
785 		      offsetof(struct bl_dedicated_registers_v0,
786 			       boot_loader_ready), 0);
787 		/* this should be safe to write even with old BLs */
788 		wil_w(wil, RGF_USER_BL +
789 		      offsetof(struct bl_dedicated_registers_v1,
790 			       bl_shutdown_handshake), 0);
791 	}
792 	/* Clear Fw Download notification */
793 	wil_c(wil, RGF_USER_USAGE_6, BIT(0));
794 
795 	wil_s(wil, RGF_CAF_OSC_CONTROL, BIT_CAF_OSC_XTAL_EN);
796 	/* XTAL stabilization should take about 3ms */
797 	usleep_range(5000, 7000);
798 	x = wil_r(wil, RGF_CAF_PLL_LOCK_STATUS);
799 	if (!(x & BIT_CAF_OSC_DIG_XTAL_STABLE)) {
800 		wil_err(wil, "Xtal stabilization timeout\n"
801 			"RGF_CAF_PLL_LOCK_STATUS = 0x%08x\n", x);
802 		return -ETIME;
803 	}
804 	/* switch 10k to XTAL*/
805 	wil_c(wil, RGF_USER_SPARROW_M_4, BIT_SPARROW_M_4_SEL_SLEEP_OR_REF);
806 	/* 40 MHz */
807 	wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_CAR_AHB_SW_SEL);
808 
809 	wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x3ff81f);
810 	wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0xf);
811 
812 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
813 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
814 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x000000f0);
815 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FE00);
816 
817 	wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x0);
818 	wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0x0);
819 
820 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
821 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
822 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
823 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
824 
825 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000003);
826 	/* reset A2 PCIE AHB */
827 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000);
828 
829 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
830 
831 	/* wait until device ready. typical time is 20..80 msec */
832 	if (no_flash)
833 		do {
834 			msleep(RST_DELAY);
835 			x = wil_r(wil, USER_EXT_USER_PMU_3);
836 			if (delay++ > RST_COUNT) {
837 				wil_err(wil, "Reset not completed, PMU_3 0x%08x\n",
838 					x);
839 				return -ETIME;
840 			}
841 		} while ((x & BIT_PMU_DEVICE_RDY) == 0);
842 	else
843 		do {
844 			msleep(RST_DELAY);
845 			x = wil_r(wil, RGF_USER_BL +
846 				  offsetof(struct bl_dedicated_registers_v0,
847 					   boot_loader_ready));
848 			if (x1 != x) {
849 				wil_dbg_misc(wil, "BL.ready 0x%08x => 0x%08x\n",
850 					     x1, x);
851 				x1 = x;
852 			}
853 			if (delay++ > RST_COUNT) {
854 				wil_err(wil, "Reset not completed, bl.ready 0x%08x\n",
855 					x);
856 				return -ETIME;
857 			}
858 		} while (x != BL_READY);
859 
860 	wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
861 
862 	/* enable fix for HW bug related to the SA/DA swap in AP Rx */
863 	wil_s(wil, RGF_DMA_OFUL_NID_0, BIT_DMA_OFUL_NID_0_RX_EXT_TR_EN |
864 	      BIT_DMA_OFUL_NID_0_RX_EXT_A3_SRC);
865 
866 	if (no_flash) {
867 		/* Reset OTP HW vectors to fit 40MHz */
868 		wil_w(wil, RGF_USER_XPM_IFC_RD_TIME1, 0x60001);
869 		wil_w(wil, RGF_USER_XPM_IFC_RD_TIME2, 0x20027);
870 		wil_w(wil, RGF_USER_XPM_IFC_RD_TIME3, 0x1);
871 		wil_w(wil, RGF_USER_XPM_IFC_RD_TIME4, 0x20027);
872 		wil_w(wil, RGF_USER_XPM_IFC_RD_TIME5, 0x30003);
873 		wil_w(wil, RGF_USER_XPM_IFC_RD_TIME6, 0x20002);
874 		wil_w(wil, RGF_USER_XPM_IFC_RD_TIME7, 0x60001);
875 		wil_w(wil, RGF_USER_XPM_IFC_RD_TIME8, 0x60001);
876 		wil_w(wil, RGF_USER_XPM_IFC_RD_TIME9, 0x60001);
877 		wil_w(wil, RGF_USER_XPM_IFC_RD_TIME10, 0x60001);
878 		wil_w(wil, RGF_USER_XPM_RD_DOUT_SAMPLE_TIME, 0x57);
879 	}
880 
881 	wil_dbg_misc(wil, "Reset completed in %d ms\n", delay * RST_DELAY);
882 	return 0;
883 }
884 
885 static void wil_collect_fw_info(struct wil6210_priv *wil)
886 {
887 	struct wiphy *wiphy = wil_to_wiphy(wil);
888 	u8 retry_short;
889 	int rc;
890 
891 	wil_refresh_fw_capabilities(wil);
892 
893 	rc = wmi_get_mgmt_retry(wil, &retry_short);
894 	if (!rc) {
895 		wiphy->retry_short = retry_short;
896 		wil_dbg_misc(wil, "FW retry_short: %d\n", retry_short);
897 	}
898 }
899 
900 void wil_refresh_fw_capabilities(struct wil6210_priv *wil)
901 {
902 	struct wiphy *wiphy = wil_to_wiphy(wil);
903 	int features;
904 
905 	wil->keep_radio_on_during_sleep =
906 		test_bit(WIL_PLATFORM_CAPA_RADIO_ON_IN_SUSPEND,
907 			 wil->platform_capa) &&
908 		test_bit(WMI_FW_CAPABILITY_D3_SUSPEND, wil->fw_capabilities);
909 
910 	wil_info(wil, "keep_radio_on_during_sleep (%d)\n",
911 		 wil->keep_radio_on_during_sleep);
912 
913 	if (test_bit(WMI_FW_CAPABILITY_RSSI_REPORTING, wil->fw_capabilities))
914 		wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
915 	else
916 		wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
917 
918 	if (test_bit(WMI_FW_CAPABILITY_PNO, wil->fw_capabilities)) {
919 		wiphy->max_sched_scan_reqs = 1;
920 		wiphy->max_sched_scan_ssids = WMI_MAX_PNO_SSID_NUM;
921 		wiphy->max_match_sets = WMI_MAX_PNO_SSID_NUM;
922 		wiphy->max_sched_scan_ie_len = WMI_MAX_IE_LEN;
923 		wiphy->max_sched_scan_plans = WMI_MAX_PLANS_NUM;
924 	}
925 
926 	if (wil->platform_ops.set_features) {
927 		features = (test_bit(WMI_FW_CAPABILITY_REF_CLOCK_CONTROL,
928 				     wil->fw_capabilities) &&
929 			    test_bit(WIL_PLATFORM_CAPA_EXT_CLK,
930 				     wil->platform_capa)) ?
931 			BIT(WIL_PLATFORM_FEATURE_FW_EXT_CLK_CONTROL) : 0;
932 
933 		wil->platform_ops.set_features(wil->platform_handle, features);
934 	}
935 }
936 
937 void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
938 {
939 	le32_to_cpus(&r->base);
940 	le16_to_cpus(&r->entry_size);
941 	le16_to_cpus(&r->size);
942 	le32_to_cpus(&r->tail);
943 	le32_to_cpus(&r->head);
944 }
945 
946 static int wil_get_bl_info(struct wil6210_priv *wil)
947 {
948 	struct net_device *ndev = wil->main_ndev;
949 	struct wiphy *wiphy = wil_to_wiphy(wil);
950 	union {
951 		struct bl_dedicated_registers_v0 bl0;
952 		struct bl_dedicated_registers_v1 bl1;
953 	} bl;
954 	u32 bl_ver;
955 	u8 *mac;
956 	u16 rf_status;
957 
958 	wil_memcpy_fromio_32(&bl, wil->csr + HOSTADDR(RGF_USER_BL),
959 			     sizeof(bl));
960 	bl_ver = le32_to_cpu(bl.bl0.boot_loader_struct_version);
961 	mac = bl.bl0.mac_address;
962 
963 	if (bl_ver == 0) {
964 		le32_to_cpus(&bl.bl0.rf_type);
965 		le32_to_cpus(&bl.bl0.baseband_type);
966 		rf_status = 0; /* actually, unknown */
967 		wil_info(wil,
968 			 "Boot Loader struct v%d: MAC = %pM RF = 0x%08x bband = 0x%08x\n",
969 			 bl_ver, mac,
970 			 bl.bl0.rf_type, bl.bl0.baseband_type);
971 		wil_info(wil, "Boot Loader build unknown for struct v0\n");
972 	} else {
973 		le16_to_cpus(&bl.bl1.rf_type);
974 		rf_status = le16_to_cpu(bl.bl1.rf_status);
975 		le32_to_cpus(&bl.bl1.baseband_type);
976 		le16_to_cpus(&bl.bl1.bl_version_subminor);
977 		le16_to_cpus(&bl.bl1.bl_version_build);
978 		wil_info(wil,
979 			 "Boot Loader struct v%d: MAC = %pM RF = 0x%04x (status 0x%04x) bband = 0x%08x\n",
980 			 bl_ver, mac,
981 			 bl.bl1.rf_type, rf_status,
982 			 bl.bl1.baseband_type);
983 		wil_info(wil, "Boot Loader build %d.%d.%d.%d\n",
984 			 bl.bl1.bl_version_major, bl.bl1.bl_version_minor,
985 			 bl.bl1.bl_version_subminor, bl.bl1.bl_version_build);
986 	}
987 
988 	if (!is_valid_ether_addr(mac)) {
989 		wil_err(wil, "BL: Invalid MAC %pM\n", mac);
990 		return -EINVAL;
991 	}
992 
993 	ether_addr_copy(ndev->perm_addr, mac);
994 	ether_addr_copy(wiphy->perm_addr, mac);
995 	if (!is_valid_ether_addr(ndev->dev_addr))
996 		ether_addr_copy(ndev->dev_addr, mac);
997 
998 	if (rf_status) {/* bad RF cable? */
999 		wil_err(wil, "RF communication error 0x%04x",
1000 			rf_status);
1001 		return -EAGAIN;
1002 	}
1003 
1004 	return 0;
1005 }
1006 
1007 static void wil_bl_crash_info(struct wil6210_priv *wil, bool is_err)
1008 {
1009 	u32 bl_assert_code, bl_assert_blink, bl_magic_number;
1010 	u32 bl_ver = wil_r(wil, RGF_USER_BL +
1011 			   offsetof(struct bl_dedicated_registers_v0,
1012 				    boot_loader_struct_version));
1013 
1014 	if (bl_ver < 2)
1015 		return;
1016 
1017 	bl_assert_code = wil_r(wil, RGF_USER_BL +
1018 			       offsetof(struct bl_dedicated_registers_v1,
1019 					bl_assert_code));
1020 	bl_assert_blink = wil_r(wil, RGF_USER_BL +
1021 				offsetof(struct bl_dedicated_registers_v1,
1022 					 bl_assert_blink));
1023 	bl_magic_number = wil_r(wil, RGF_USER_BL +
1024 				offsetof(struct bl_dedicated_registers_v1,
1025 					 bl_magic_number));
1026 
1027 	if (is_err) {
1028 		wil_err(wil,
1029 			"BL assert code 0x%08x blink 0x%08x magic 0x%08x\n",
1030 			bl_assert_code, bl_assert_blink, bl_magic_number);
1031 	} else {
1032 		wil_dbg_misc(wil,
1033 			     "BL assert code 0x%08x blink 0x%08x magic 0x%08x\n",
1034 			     bl_assert_code, bl_assert_blink, bl_magic_number);
1035 	}
1036 }
1037 
1038 static int wil_get_otp_info(struct wil6210_priv *wil)
1039 {
1040 	struct net_device *ndev = wil->main_ndev;
1041 	struct wiphy *wiphy = wil_to_wiphy(wil);
1042 	u8 mac[8];
1043 
1044 	wil_memcpy_fromio_32(mac, wil->csr + HOSTADDR(RGF_OTP_MAC),
1045 			     sizeof(mac));
1046 	if (!is_valid_ether_addr(mac)) {
1047 		wil_err(wil, "Invalid MAC %pM\n", mac);
1048 		return -EINVAL;
1049 	}
1050 
1051 	ether_addr_copy(ndev->perm_addr, mac);
1052 	ether_addr_copy(wiphy->perm_addr, mac);
1053 	if (!is_valid_ether_addr(ndev->dev_addr))
1054 		ether_addr_copy(ndev->dev_addr, mac);
1055 
1056 	return 0;
1057 }
1058 
1059 static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
1060 {
1061 	ulong to = msecs_to_jiffies(1000);
1062 	ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
1063 
1064 	if (0 == left) {
1065 		wil_err(wil, "Firmware not ready\n");
1066 		return -ETIME;
1067 	} else {
1068 		wil_info(wil, "FW ready after %d ms. HW version 0x%08x\n",
1069 			 jiffies_to_msecs(to-left), wil->hw_version);
1070 	}
1071 	return 0;
1072 }
1073 
1074 void wil_abort_scan(struct wil6210_vif *vif, bool sync)
1075 {
1076 	struct wil6210_priv *wil = vif_to_wil(vif);
1077 	int rc;
1078 	struct cfg80211_scan_info info = {
1079 		.aborted = true,
1080 	};
1081 
1082 	lockdep_assert_held(&wil->vif_mutex);
1083 
1084 	if (!vif->scan_request)
1085 		return;
1086 
1087 	wil_dbg_misc(wil, "Abort scan_request 0x%p\n", vif->scan_request);
1088 	del_timer_sync(&vif->scan_timer);
1089 	mutex_unlock(&wil->vif_mutex);
1090 	rc = wmi_abort_scan(vif);
1091 	if (!rc && sync)
1092 		wait_event_interruptible_timeout(wil->wq, !vif->scan_request,
1093 						 msecs_to_jiffies(
1094 						 WAIT_FOR_SCAN_ABORT_MS));
1095 
1096 	mutex_lock(&wil->vif_mutex);
1097 	if (vif->scan_request) {
1098 		cfg80211_scan_done(vif->scan_request, &info);
1099 		vif->scan_request = NULL;
1100 	}
1101 }
1102 
1103 void wil_abort_scan_all_vifs(struct wil6210_priv *wil, bool sync)
1104 {
1105 	int i;
1106 
1107 	lockdep_assert_held(&wil->vif_mutex);
1108 
1109 	for (i = 0; i < wil->max_vifs; i++) {
1110 		struct wil6210_vif *vif = wil->vifs[i];
1111 
1112 		if (vif)
1113 			wil_abort_scan(vif, sync);
1114 	}
1115 }
1116 
1117 int wil_ps_update(struct wil6210_priv *wil, enum wmi_ps_profile_type ps_profile)
1118 {
1119 	int rc;
1120 
1121 	if (!test_bit(WMI_FW_CAPABILITY_PS_CONFIG, wil->fw_capabilities)) {
1122 		wil_err(wil, "set_power_mgmt not supported\n");
1123 		return -EOPNOTSUPP;
1124 	}
1125 
1126 	rc  = wmi_ps_dev_profile_cfg(wil, ps_profile);
1127 	if (rc)
1128 		wil_err(wil, "wmi_ps_dev_profile_cfg failed (%d)\n", rc);
1129 	else
1130 		wil->ps_profile = ps_profile;
1131 
1132 	return rc;
1133 }
1134 
1135 static void wil_pre_fw_config(struct wil6210_priv *wil)
1136 {
1137 	/* Mark FW as loaded from host */
1138 	wil_s(wil, RGF_USER_USAGE_6, 1);
1139 
1140 	/* clear any interrupts which on-card-firmware
1141 	 * may have set
1142 	 */
1143 	wil6210_clear_irq(wil);
1144 	/* CAF_ICR - clear and mask */
1145 	/* it is W1C, clear by writing back same value */
1146 	wil_s(wil, RGF_CAF_ICR + offsetof(struct RGF_ICR, ICR), 0);
1147 	wil_w(wil, RGF_CAF_ICR + offsetof(struct RGF_ICR, IMV), ~0);
1148 	/* clear PAL_UNIT_ICR (potential D0->D3 leftover) */
1149 	wil_s(wil, RGF_PAL_UNIT_ICR + offsetof(struct RGF_ICR, ICR), 0);
1150 
1151 	if (wil->fw_calib_result > 0) {
1152 		__le32 val = cpu_to_le32(wil->fw_calib_result |
1153 						(CALIB_RESULT_SIGNATURE << 8));
1154 		wil_w(wil, RGF_USER_FW_CALIB_RESULT, (u32 __force)val);
1155 	}
1156 }
1157 
1158 static int wil_restore_vifs(struct wil6210_priv *wil)
1159 {
1160 	struct wil6210_vif *vif;
1161 	struct net_device *ndev;
1162 	struct wireless_dev *wdev;
1163 	int i, rc;
1164 
1165 	for (i = 0; i < wil->max_vifs; i++) {
1166 		vif = wil->vifs[i];
1167 		if (!vif)
1168 			continue;
1169 		vif->ap_isolate = 0;
1170 		if (vif->mid) {
1171 			ndev = vif_to_ndev(vif);
1172 			wdev = vif_to_wdev(vif);
1173 			rc = wmi_port_allocate(wil, vif->mid, ndev->dev_addr,
1174 					       wdev->iftype);
1175 			if (rc) {
1176 				wil_err(wil, "fail to restore VIF %d type %d, rc %d\n",
1177 					i, wdev->iftype, rc);
1178 				return rc;
1179 			}
1180 		}
1181 	}
1182 
1183 	return 0;
1184 }
1185 
1186 /*
1187  * We reset all the structures, and we reset the UMAC.
1188  * After calling this routine, you're expected to reload
1189  * the firmware.
1190  */
1191 int wil_reset(struct wil6210_priv *wil, bool load_fw)
1192 {
1193 	int rc, i;
1194 	unsigned long status_flags = BIT(wil_status_resetting);
1195 	int no_flash;
1196 	struct wil6210_vif *vif;
1197 
1198 	wil_dbg_misc(wil, "reset\n");
1199 
1200 	WARN_ON(!mutex_is_locked(&wil->mutex));
1201 	WARN_ON(test_bit(wil_status_napi_en, wil->status));
1202 
1203 	if (debug_fw) {
1204 		static const u8 mac[ETH_ALEN] = {
1205 			0x00, 0xde, 0xad, 0x12, 0x34, 0x56,
1206 		};
1207 		struct net_device *ndev = wil->main_ndev;
1208 
1209 		ether_addr_copy(ndev->perm_addr, mac);
1210 		ether_addr_copy(ndev->dev_addr, ndev->perm_addr);
1211 		return 0;
1212 	}
1213 
1214 	if (wil->hw_version == HW_VER_UNKNOWN)
1215 		return -ENODEV;
1216 
1217 	if (test_bit(WIL_PLATFORM_CAPA_T_PWR_ON_0, wil->platform_capa)) {
1218 		wil_dbg_misc(wil, "Notify FW to set T_POWER_ON=0\n");
1219 		wil_s(wil, RGF_USER_USAGE_8, BIT_USER_SUPPORT_T_POWER_ON_0);
1220 	}
1221 
1222 	if (test_bit(WIL_PLATFORM_CAPA_EXT_CLK, wil->platform_capa)) {
1223 		wil_dbg_misc(wil, "Notify FW on ext clock configuration\n");
1224 		wil_s(wil, RGF_USER_USAGE_8, BIT_USER_EXT_CLK);
1225 	}
1226 
1227 	if (wil->platform_ops.notify) {
1228 		rc = wil->platform_ops.notify(wil->platform_handle,
1229 					      WIL_PLATFORM_EVT_PRE_RESET);
1230 		if (rc)
1231 			wil_err(wil, "PRE_RESET platform notify failed, rc %d\n",
1232 				rc);
1233 	}
1234 
1235 	set_bit(wil_status_resetting, wil->status);
1236 	if (test_bit(wil_status_collecting_dumps, wil->status)) {
1237 		/* Device collects crash dump, cancel the reset.
1238 		 * following crash dump collection, reset would take place.
1239 		 */
1240 		wil_dbg_misc(wil, "reject reset while collecting crash dump\n");
1241 		rc = -EBUSY;
1242 		goto out;
1243 	}
1244 
1245 	mutex_lock(&wil->vif_mutex);
1246 	wil_abort_scan_all_vifs(wil, false);
1247 	mutex_unlock(&wil->vif_mutex);
1248 
1249 	for (i = 0; i < wil->max_vifs; i++) {
1250 		vif = wil->vifs[i];
1251 		if (vif) {
1252 			cancel_work_sync(&vif->disconnect_worker);
1253 			wil6210_disconnect(vif, NULL,
1254 					   WLAN_REASON_DEAUTH_LEAVING, false);
1255 		}
1256 	}
1257 	wil_bcast_fini_all(wil);
1258 
1259 	/* Disable device led before reset*/
1260 	wmi_led_cfg(wil, false);
1261 
1262 	/* prevent NAPI from being scheduled and prevent wmi commands */
1263 	mutex_lock(&wil->wmi_mutex);
1264 	if (test_bit(wil_status_suspending, wil->status))
1265 		status_flags |= BIT(wil_status_suspending);
1266 	bitmap_and(wil->status, wil->status, &status_flags,
1267 		   wil_status_last);
1268 	wil_dbg_misc(wil, "wil->status (0x%lx)\n", *wil->status);
1269 	mutex_unlock(&wil->wmi_mutex);
1270 
1271 	wil_mask_irq(wil);
1272 
1273 	wmi_event_flush(wil);
1274 
1275 	flush_workqueue(wil->wq_service);
1276 	flush_workqueue(wil->wmi_wq);
1277 
1278 	no_flash = test_bit(hw_capa_no_flash, wil->hw_capa);
1279 	if (!no_flash)
1280 		wil_bl_crash_info(wil, false);
1281 	wil_disable_irq(wil);
1282 	rc = wil_target_reset(wil, no_flash);
1283 	wil6210_clear_irq(wil);
1284 	wil_enable_irq(wil);
1285 	wil_rx_fini(wil);
1286 	if (rc) {
1287 		if (!no_flash)
1288 			wil_bl_crash_info(wil, true);
1289 		goto out;
1290 	}
1291 
1292 	if (no_flash) {
1293 		rc = wil_get_otp_info(wil);
1294 	} else {
1295 		rc = wil_get_bl_info(wil);
1296 		if (rc == -EAGAIN && !load_fw)
1297 			/* ignore RF error if not going up */
1298 			rc = 0;
1299 	}
1300 	if (rc)
1301 		goto out;
1302 
1303 	wil_set_oob_mode(wil, oob_mode);
1304 	if (load_fw) {
1305 		wil_info(wil, "Use firmware <%s> + board <%s>\n",
1306 			 wil->wil_fw_name, WIL_BOARD_FILE_NAME);
1307 
1308 		if (!no_flash)
1309 			wil_bl_prepare_halt(wil);
1310 
1311 		wil_halt_cpu(wil);
1312 		memset(wil->fw_version, 0, sizeof(wil->fw_version));
1313 		/* Loading f/w from the file */
1314 		rc = wil_request_firmware(wil, wil->wil_fw_name, true);
1315 		if (rc)
1316 			goto out;
1317 		if (wil->brd_file_addr)
1318 			rc = wil_request_board(wil, WIL_BOARD_FILE_NAME);
1319 		else
1320 			rc = wil_request_firmware(wil,
1321 						  WIL_BOARD_FILE_NAME,
1322 						  true);
1323 		if (rc)
1324 			goto out;
1325 
1326 		wil_pre_fw_config(wil);
1327 		wil_release_cpu(wil);
1328 	}
1329 
1330 	/* init after reset */
1331 	reinit_completion(&wil->wmi_ready);
1332 	reinit_completion(&wil->wmi_call);
1333 	reinit_completion(&wil->halp.comp);
1334 
1335 	clear_bit(wil_status_resetting, wil->status);
1336 
1337 	if (load_fw) {
1338 		wil_configure_interrupt_moderation(wil);
1339 		wil_unmask_irq(wil);
1340 
1341 		/* we just started MAC, wait for FW ready */
1342 		rc = wil_wait_for_fw_ready(wil);
1343 		if (rc)
1344 			return rc;
1345 
1346 		/* check FW is responsive */
1347 		rc = wmi_echo(wil);
1348 		if (rc) {
1349 			wil_err(wil, "wmi_echo failed, rc %d\n", rc);
1350 			return rc;
1351 		}
1352 
1353 		rc = wil_restore_vifs(wil);
1354 		if (rc) {
1355 			wil_err(wil, "failed to restore vifs, rc %d\n", rc);
1356 			return rc;
1357 		}
1358 
1359 		wil_collect_fw_info(wil);
1360 
1361 		if (wil->ps_profile != WMI_PS_PROFILE_TYPE_DEFAULT)
1362 			wil_ps_update(wil, wil->ps_profile);
1363 
1364 		if (wil->platform_ops.notify) {
1365 			rc = wil->platform_ops.notify(wil->platform_handle,
1366 						      WIL_PLATFORM_EVT_FW_RDY);
1367 			if (rc) {
1368 				wil_err(wil, "FW_RDY notify failed, rc %d\n",
1369 					rc);
1370 				rc = 0;
1371 			}
1372 		}
1373 	}
1374 
1375 	return rc;
1376 
1377 out:
1378 	clear_bit(wil_status_resetting, wil->status);
1379 	return rc;
1380 }
1381 
1382 void wil_fw_error_recovery(struct wil6210_priv *wil)
1383 {
1384 	wil_dbg_misc(wil, "starting fw error recovery\n");
1385 
1386 	if (test_bit(wil_status_resetting, wil->status)) {
1387 		wil_info(wil, "Reset already in progress\n");
1388 		return;
1389 	}
1390 
1391 	wil->recovery_state = fw_recovery_pending;
1392 	schedule_work(&wil->fw_error_worker);
1393 }
1394 
1395 int __wil_up(struct wil6210_priv *wil)
1396 {
1397 	struct net_device *ndev = wil->main_ndev;
1398 	struct wireless_dev *wdev = ndev->ieee80211_ptr;
1399 	int rc;
1400 
1401 	WARN_ON(!mutex_is_locked(&wil->mutex));
1402 
1403 	rc = wil_reset(wil, true);
1404 	if (rc)
1405 		return rc;
1406 
1407 	/* Rx VRING. After MAC and beacon */
1408 	rc = wil_rx_init(wil, 1 << rx_ring_order);
1409 	if (rc)
1410 		return rc;
1411 
1412 	switch (wdev->iftype) {
1413 	case NL80211_IFTYPE_STATION:
1414 		wil_dbg_misc(wil, "type: STATION\n");
1415 		ndev->type = ARPHRD_ETHER;
1416 		break;
1417 	case NL80211_IFTYPE_AP:
1418 		wil_dbg_misc(wil, "type: AP\n");
1419 		ndev->type = ARPHRD_ETHER;
1420 		break;
1421 	case NL80211_IFTYPE_P2P_CLIENT:
1422 		wil_dbg_misc(wil, "type: P2P_CLIENT\n");
1423 		ndev->type = ARPHRD_ETHER;
1424 		break;
1425 	case NL80211_IFTYPE_P2P_GO:
1426 		wil_dbg_misc(wil, "type: P2P_GO\n");
1427 		ndev->type = ARPHRD_ETHER;
1428 		break;
1429 	case NL80211_IFTYPE_MONITOR:
1430 		wil_dbg_misc(wil, "type: Monitor\n");
1431 		ndev->type = ARPHRD_IEEE80211_RADIOTAP;
1432 		/* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
1433 		break;
1434 	default:
1435 		return -EOPNOTSUPP;
1436 	}
1437 
1438 	/* MAC address - pre-requisite for other commands */
1439 	wmi_set_mac_address(wil, ndev->dev_addr);
1440 
1441 	wil_dbg_misc(wil, "NAPI enable\n");
1442 	napi_enable(&wil->napi_rx);
1443 	napi_enable(&wil->napi_tx);
1444 	set_bit(wil_status_napi_en, wil->status);
1445 
1446 	wil6210_bus_request(wil, WIL_DEFAULT_BUS_REQUEST_KBPS);
1447 
1448 	return 0;
1449 }
1450 
1451 int wil_up(struct wil6210_priv *wil)
1452 {
1453 	int rc;
1454 
1455 	wil_dbg_misc(wil, "up\n");
1456 
1457 	mutex_lock(&wil->mutex);
1458 	rc = __wil_up(wil);
1459 	mutex_unlock(&wil->mutex);
1460 
1461 	return rc;
1462 }
1463 
1464 int __wil_down(struct wil6210_priv *wil)
1465 {
1466 	WARN_ON(!mutex_is_locked(&wil->mutex));
1467 
1468 	set_bit(wil_status_resetting, wil->status);
1469 
1470 	wil6210_bus_request(wil, 0);
1471 
1472 	wil_disable_irq(wil);
1473 	if (test_and_clear_bit(wil_status_napi_en, wil->status)) {
1474 		napi_disable(&wil->napi_rx);
1475 		napi_disable(&wil->napi_tx);
1476 		wil_dbg_misc(wil, "NAPI disable\n");
1477 	}
1478 	wil_enable_irq(wil);
1479 
1480 	mutex_lock(&wil->vif_mutex);
1481 	wil_p2p_stop_radio_operations(wil);
1482 	wil_abort_scan_all_vifs(wil, false);
1483 	mutex_unlock(&wil->vif_mutex);
1484 
1485 	return wil_reset(wil, false);
1486 }
1487 
1488 int wil_down(struct wil6210_priv *wil)
1489 {
1490 	int rc;
1491 
1492 	wil_dbg_misc(wil, "down\n");
1493 
1494 	wil_set_recovery_state(wil, fw_recovery_idle);
1495 	mutex_lock(&wil->mutex);
1496 	rc = __wil_down(wil);
1497 	mutex_unlock(&wil->mutex);
1498 
1499 	return rc;
1500 }
1501 
1502 int wil_find_cid(struct wil6210_priv *wil, u8 mid, const u8 *mac)
1503 {
1504 	int i;
1505 	int rc = -ENOENT;
1506 
1507 	for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
1508 		if (wil->sta[i].mid == mid &&
1509 		    wil->sta[i].status != wil_sta_unused &&
1510 		    ether_addr_equal(wil->sta[i].addr, mac)) {
1511 			rc = i;
1512 			break;
1513 		}
1514 	}
1515 
1516 	return rc;
1517 }
1518 
1519 void wil_halp_vote(struct wil6210_priv *wil)
1520 {
1521 	unsigned long rc;
1522 	unsigned long to_jiffies = msecs_to_jiffies(WAIT_FOR_HALP_VOTE_MS);
1523 
1524 	mutex_lock(&wil->halp.lock);
1525 
1526 	wil_dbg_irq(wil, "halp_vote: start, HALP ref_cnt (%d)\n",
1527 		    wil->halp.ref_cnt);
1528 
1529 	if (++wil->halp.ref_cnt == 1) {
1530 		reinit_completion(&wil->halp.comp);
1531 		wil6210_set_halp(wil);
1532 		rc = wait_for_completion_timeout(&wil->halp.comp, to_jiffies);
1533 		if (!rc) {
1534 			wil_err(wil, "HALP vote timed out\n");
1535 			/* Mask HALP as done in case the interrupt is raised */
1536 			wil6210_mask_halp(wil);
1537 		} else {
1538 			wil_dbg_irq(wil,
1539 				    "halp_vote: HALP vote completed after %d ms\n",
1540 				    jiffies_to_msecs(to_jiffies - rc));
1541 		}
1542 	}
1543 
1544 	wil_dbg_irq(wil, "halp_vote: end, HALP ref_cnt (%d)\n",
1545 		    wil->halp.ref_cnt);
1546 
1547 	mutex_unlock(&wil->halp.lock);
1548 }
1549 
1550 void wil_halp_unvote(struct wil6210_priv *wil)
1551 {
1552 	WARN_ON(wil->halp.ref_cnt == 0);
1553 
1554 	mutex_lock(&wil->halp.lock);
1555 
1556 	wil_dbg_irq(wil, "halp_unvote: start, HALP ref_cnt (%d)\n",
1557 		    wil->halp.ref_cnt);
1558 
1559 	if (--wil->halp.ref_cnt == 0) {
1560 		wil6210_clear_halp(wil);
1561 		wil_dbg_irq(wil, "HALP unvote\n");
1562 	}
1563 
1564 	wil_dbg_irq(wil, "halp_unvote:end, HALP ref_cnt (%d)\n",
1565 		    wil->halp.ref_cnt);
1566 
1567 	mutex_unlock(&wil->halp.lock);
1568 }
1569