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