xref: /openbmc/linux/drivers/net/wireless/ath/wil6210/main.c (revision 9dae47aba0a055f761176d9297371d5bb24289ec)
1 /*
2  * Copyright (c) 2012-2017 Qualcomm Atheros, Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <linux/moduleparam.h>
18 #include <linux/if_arp.h>
19 #include <linux/etherdevice.h>
20 
21 #include "wil6210.h"
22 #include "txrx.h"
23 #include "wmi.h"
24 #include "boot_loader.h"
25 
26 #define WAIT_FOR_HALP_VOTE_MS 100
27 #define WAIT_FOR_SCAN_ABORT_MS 1000
28 
29 bool debug_fw; /* = false; */
30 module_param(debug_fw, bool, 0444);
31 MODULE_PARM_DESC(debug_fw, " do not perform card reset. For FW debug");
32 
33 static u8 oob_mode;
34 module_param(oob_mode, byte, 0444);
35 MODULE_PARM_DESC(oob_mode,
36 		 " enable out of the box (OOB) mode in FW, for diagnostics and certification");
37 
38 bool no_fw_recovery;
39 module_param(no_fw_recovery, bool, 0644);
40 MODULE_PARM_DESC(no_fw_recovery, " disable automatic FW error recovery");
41 
42 /* if not set via modparam, will be set to default value of 1/8 of
43  * rx ring size during init flow
44  */
45 unsigned short rx_ring_overflow_thrsh = WIL6210_RX_HIGH_TRSH_INIT;
46 module_param(rx_ring_overflow_thrsh, ushort, 0444);
47 MODULE_PARM_DESC(rx_ring_overflow_thrsh,
48 		 " RX ring overflow threshold in descriptors.");
49 
50 /* We allow allocation of more than 1 page buffers to support large packets.
51  * It is suboptimal behavior performance wise in case MTU above page size.
52  */
53 unsigned int mtu_max = TXRX_BUF_LEN_DEFAULT - WIL_MAX_MPDU_OVERHEAD;
54 static int mtu_max_set(const char *val, const struct kernel_param *kp)
55 {
56 	int ret;
57 
58 	/* sets mtu_max directly. no need to restore it in case of
59 	 * illegal value since we assume this will fail insmod
60 	 */
61 	ret = param_set_uint(val, kp);
62 	if (ret)
63 		return ret;
64 
65 	if (mtu_max < 68 || mtu_max > WIL_MAX_ETH_MTU)
66 		ret = -EINVAL;
67 
68 	return ret;
69 }
70 
71 static const struct kernel_param_ops mtu_max_ops = {
72 	.set = mtu_max_set,
73 	.get = param_get_uint,
74 };
75 
76 module_param_cb(mtu_max, &mtu_max_ops, &mtu_max, 0444);
77 MODULE_PARM_DESC(mtu_max, " Max MTU value.");
78 
79 static uint rx_ring_order = WIL_RX_RING_SIZE_ORDER_DEFAULT;
80 static uint tx_ring_order = WIL_TX_RING_SIZE_ORDER_DEFAULT;
81 static uint bcast_ring_order = WIL_BCAST_RING_SIZE_ORDER_DEFAULT;
82 
83 static int ring_order_set(const char *val, const struct kernel_param *kp)
84 {
85 	int ret;
86 	uint x;
87 
88 	ret = kstrtouint(val, 0, &x);
89 	if (ret)
90 		return ret;
91 
92 	if ((x < WIL_RING_SIZE_ORDER_MIN) || (x > WIL_RING_SIZE_ORDER_MAX))
93 		return -EINVAL;
94 
95 	*((uint *)kp->arg) = x;
96 
97 	return 0;
98 }
99 
100 static const struct kernel_param_ops ring_order_ops = {
101 	.set = ring_order_set,
102 	.get = param_get_uint,
103 };
104 
105 module_param_cb(rx_ring_order, &ring_order_ops, &rx_ring_order, 0444);
106 MODULE_PARM_DESC(rx_ring_order, " Rx ring order; size = 1 << order");
107 module_param_cb(tx_ring_order, &ring_order_ops, &tx_ring_order, 0444);
108 MODULE_PARM_DESC(tx_ring_order, " Tx ring order; size = 1 << order");
109 module_param_cb(bcast_ring_order, &ring_order_ops, &bcast_ring_order, 0444);
110 MODULE_PARM_DESC(bcast_ring_order, " Bcast ring order; size = 1 << order");
111 
112 #define RST_DELAY (20) /* msec, for loop in @wil_target_reset */
113 #define RST_COUNT (1 + 1000/RST_DELAY) /* round up to be above 1 sec total */
114 
115 /*
116  * Due to a hardware issue,
117  * one has to read/write to/from NIC in 32-bit chunks;
118  * regular memcpy_fromio and siblings will
119  * not work on 64-bit platform - it uses 64-bit transactions
120  *
121  * Force 32-bit transactions to enable NIC on 64-bit platforms
122  *
123  * To avoid byte swap on big endian host, __raw_{read|write}l
124  * should be used - {read|write}l would swap bytes to provide
125  * little endian on PCI value in host endianness.
126  */
127 void wil_memcpy_fromio_32(void *dst, const volatile void __iomem *src,
128 			  size_t count)
129 {
130 	u32 *d = dst;
131 	const volatile u32 __iomem *s = src;
132 
133 	for (; count >= 4; count -= 4)
134 		*d++ = __raw_readl(s++);
135 
136 	if (unlikely(count)) {
137 		/* count can be 1..3 */
138 		u32 tmp = __raw_readl(s);
139 
140 		memcpy(d, &tmp, count);
141 	}
142 }
143 
144 void wil_memcpy_toio_32(volatile void __iomem *dst, const void *src,
145 			size_t count)
146 {
147 	volatile u32 __iomem *d = dst;
148 	const u32 *s = src;
149 
150 	for (; count >= 4; count -= 4)
151 		__raw_writel(*s++, d++);
152 
153 	if (unlikely(count)) {
154 		/* count can be 1..3 */
155 		u32 tmp = 0;
156 
157 		memcpy(&tmp, s, count);
158 		__raw_writel(tmp, d);
159 	}
160 }
161 
162 static void wil_disconnect_cid(struct wil6210_priv *wil, int cid,
163 			       u16 reason_code, bool from_event)
164 __acquires(&sta->tid_rx_lock) __releases(&sta->tid_rx_lock)
165 {
166 	uint i;
167 	struct net_device *ndev = wil_to_ndev(wil);
168 	struct wireless_dev *wdev = wil->wdev;
169 	struct wil_sta_info *sta = &wil->sta[cid];
170 
171 	might_sleep();
172 	wil_dbg_misc(wil, "disconnect_cid: CID %d, status %d\n",
173 		     cid, sta->status);
174 	/* inform upper/lower layers */
175 	if (sta->status != wil_sta_unused) {
176 		if (!from_event) {
177 			bool del_sta = (wdev->iftype == NL80211_IFTYPE_AP) ?
178 						disable_ap_sme : false;
179 			wmi_disconnect_sta(wil, sta->addr, reason_code,
180 					   true, del_sta);
181 		}
182 
183 		switch (wdev->iftype) {
184 		case NL80211_IFTYPE_AP:
185 		case NL80211_IFTYPE_P2P_GO:
186 			/* AP-like interface */
187 			cfg80211_del_sta(ndev, sta->addr, GFP_KERNEL);
188 			break;
189 		default:
190 			break;
191 		}
192 		sta->status = wil_sta_unused;
193 	}
194 	/* reorder buffers */
195 	for (i = 0; i < WIL_STA_TID_NUM; i++) {
196 		struct wil_tid_ampdu_rx *r;
197 
198 		spin_lock_bh(&sta->tid_rx_lock);
199 
200 		r = sta->tid_rx[i];
201 		sta->tid_rx[i] = NULL;
202 		wil_tid_ampdu_rx_free(wil, r);
203 
204 		spin_unlock_bh(&sta->tid_rx_lock);
205 	}
206 	/* crypto context */
207 	memset(sta->tid_crypto_rx, 0, sizeof(sta->tid_crypto_rx));
208 	memset(&sta->group_crypto_rx, 0, sizeof(sta->group_crypto_rx));
209 	/* release vrings */
210 	for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++) {
211 		if (wil->vring2cid_tid[i][0] == cid)
212 			wil_vring_fini_tx(wil, i);
213 	}
214 	/* statistics */
215 	memset(&sta->stats, 0, sizeof(sta->stats));
216 }
217 
218 static bool wil_is_connected(struct wil6210_priv *wil)
219 {
220 	int i;
221 
222 	for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
223 		if (wil->sta[i].status == wil_sta_connected)
224 			return true;
225 	}
226 
227 	return false;
228 }
229 
230 static void _wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid,
231 				u16 reason_code, bool from_event)
232 {
233 	int cid = -ENOENT;
234 	struct net_device *ndev = wil_to_ndev(wil);
235 	struct wireless_dev *wdev = wil->wdev;
236 
237 	if (unlikely(!ndev))
238 		return;
239 
240 	might_sleep();
241 	wil_info(wil, "bssid=%pM, reason=%d, ev%s\n", bssid,
242 		 reason_code, from_event ? "+" : "-");
243 
244 	/* Cases are:
245 	 * - disconnect single STA, still connected
246 	 * - disconnect single STA, already disconnected
247 	 * - disconnect all
248 	 *
249 	 * For "disconnect all", there are 3 options:
250 	 * - bssid == NULL
251 	 * - bssid is broadcast address (ff:ff:ff:ff:ff:ff)
252 	 * - bssid is our MAC address
253 	 */
254 	if (bssid && !is_broadcast_ether_addr(bssid) &&
255 	    !ether_addr_equal_unaligned(ndev->dev_addr, bssid)) {
256 		cid = wil_find_cid(wil, bssid);
257 		wil_dbg_misc(wil, "Disconnect %pM, CID=%d, reason=%d\n",
258 			     bssid, cid, reason_code);
259 		if (cid >= 0) /* disconnect 1 peer */
260 			wil_disconnect_cid(wil, cid, reason_code, from_event);
261 	} else { /* all */
262 		wil_dbg_misc(wil, "Disconnect all\n");
263 		for (cid = 0; cid < WIL6210_MAX_CID; cid++)
264 			wil_disconnect_cid(wil, cid, reason_code, from_event);
265 	}
266 
267 	/* link state */
268 	switch (wdev->iftype) {
269 	case NL80211_IFTYPE_STATION:
270 	case NL80211_IFTYPE_P2P_CLIENT:
271 		wil_bcast_fini(wil);
272 		wil_update_net_queues_bh(wil, NULL, true);
273 		netif_carrier_off(ndev);
274 		wil6210_bus_request(wil, WIL_DEFAULT_BUS_REQUEST_KBPS);
275 
276 		if (test_bit(wil_status_fwconnected, wil->status)) {
277 			clear_bit(wil_status_fwconnected, wil->status);
278 			cfg80211_disconnected(ndev, reason_code,
279 					      NULL, 0,
280 					      wil->locally_generated_disc,
281 					      GFP_KERNEL);
282 			wil->locally_generated_disc = false;
283 		} else if (test_bit(wil_status_fwconnecting, wil->status)) {
284 			cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
285 						WLAN_STATUS_UNSPECIFIED_FAILURE,
286 						GFP_KERNEL);
287 			wil->bss = NULL;
288 		}
289 		clear_bit(wil_status_fwconnecting, wil->status);
290 		break;
291 	case NL80211_IFTYPE_AP:
292 	case NL80211_IFTYPE_P2P_GO:
293 		if (!wil_is_connected(wil)) {
294 			wil_update_net_queues_bh(wil, NULL, true);
295 			clear_bit(wil_status_fwconnected, wil->status);
296 		} else {
297 			wil_update_net_queues_bh(wil, NULL, false);
298 		}
299 		break;
300 	default:
301 		break;
302 	}
303 }
304 
305 static void wil_disconnect_worker(struct work_struct *work)
306 {
307 	struct wil6210_priv *wil = container_of(work,
308 			struct wil6210_priv, disconnect_worker);
309 	struct net_device *ndev = wil_to_ndev(wil);
310 	int rc;
311 	struct {
312 		struct wmi_cmd_hdr wmi;
313 		struct wmi_disconnect_event evt;
314 	} __packed reply;
315 
316 	if (test_bit(wil_status_fwconnected, wil->status))
317 		/* connect succeeded after all */
318 		return;
319 
320 	if (!test_bit(wil_status_fwconnecting, wil->status))
321 		/* already disconnected */
322 		return;
323 
324 	rc = wmi_call(wil, WMI_DISCONNECT_CMDID, NULL, 0,
325 		      WMI_DISCONNECT_EVENTID, &reply, sizeof(reply),
326 		      WIL6210_DISCONNECT_TO_MS);
327 	if (rc) {
328 		wil_err(wil, "disconnect error %d\n", rc);
329 		return;
330 	}
331 
332 	wil_update_net_queues_bh(wil, NULL, true);
333 	netif_carrier_off(ndev);
334 	cfg80211_connect_result(ndev, NULL, NULL, 0, NULL, 0,
335 				WLAN_STATUS_UNSPECIFIED_FAILURE, GFP_KERNEL);
336 	clear_bit(wil_status_fwconnecting, wil->status);
337 }
338 
339 static void wil_connect_timer_fn(struct timer_list *t)
340 {
341 	struct wil6210_priv *wil = from_timer(wil, t, connect_timer);
342 	bool q;
343 
344 	wil_err(wil, "Connect timeout detected, disconnect station\n");
345 
346 	/* reschedule to thread context - disconnect won't
347 	 * run from atomic context.
348 	 * queue on wmi_wq to prevent race with connect event.
349 	 */
350 	q = queue_work(wil->wmi_wq, &wil->disconnect_worker);
351 	wil_dbg_wmi(wil, "queue_work of disconnect_worker -> %d\n", q);
352 }
353 
354 static void wil_scan_timer_fn(struct timer_list *t)
355 {
356 	struct wil6210_priv *wil = from_timer(wil, t, scan_timer);
357 
358 	clear_bit(wil_status_fwready, wil->status);
359 	wil_err(wil, "Scan timeout detected, start fw error recovery\n");
360 	wil_fw_error_recovery(wil);
361 }
362 
363 static int wil_wait_for_recovery(struct wil6210_priv *wil)
364 {
365 	if (wait_event_interruptible(wil->wq, wil->recovery_state !=
366 				     fw_recovery_pending)) {
367 		wil_err(wil, "Interrupt, canceling recovery\n");
368 		return -ERESTARTSYS;
369 	}
370 	if (wil->recovery_state != fw_recovery_running) {
371 		wil_info(wil, "Recovery cancelled\n");
372 		return -EINTR;
373 	}
374 	wil_info(wil, "Proceed with recovery\n");
375 	return 0;
376 }
377 
378 void wil_set_recovery_state(struct wil6210_priv *wil, int state)
379 {
380 	wil_dbg_misc(wil, "set_recovery_state: %d -> %d\n",
381 		     wil->recovery_state, state);
382 
383 	wil->recovery_state = state;
384 	wake_up_interruptible(&wil->wq);
385 }
386 
387 bool wil_is_recovery_blocked(struct wil6210_priv *wil)
388 {
389 	return no_fw_recovery && (wil->recovery_state == fw_recovery_pending);
390 }
391 
392 static void wil_fw_error_worker(struct work_struct *work)
393 {
394 	struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
395 						fw_error_worker);
396 	struct wireless_dev *wdev = wil->wdev;
397 	struct net_device *ndev = wil_to_ndev(wil);
398 
399 	wil_dbg_misc(wil, "fw error worker\n");
400 
401 	if (!(ndev->flags & IFF_UP)) {
402 		wil_info(wil, "No recovery - interface is down\n");
403 		return;
404 	}
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 	switch (wdev->iftype) {
432 	case NL80211_IFTYPE_STATION:
433 	case NL80211_IFTYPE_P2P_CLIENT:
434 	case NL80211_IFTYPE_MONITOR:
435 		/* silent recovery, upper layers will see disconnect */
436 		__wil_down(wil);
437 		__wil_up(wil);
438 		break;
439 	case NL80211_IFTYPE_AP:
440 	case NL80211_IFTYPE_P2P_GO:
441 		wil_info(wil, "No recovery for AP-like interface\n");
442 		/* recovery in these modes is done by upper layers */
443 		break;
444 	default:
445 		wil_err(wil, "No recovery - unknown interface type %d\n",
446 			wdev->iftype);
447 		break;
448 	}
449 	mutex_unlock(&wil->mutex);
450 }
451 
452 static int wil_find_free_vring(struct wil6210_priv *wil)
453 {
454 	int i;
455 
456 	for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
457 		if (!wil->vring_tx[i].va)
458 			return i;
459 	}
460 	return -EINVAL;
461 }
462 
463 int wil_tx_init(struct wil6210_priv *wil, int cid)
464 {
465 	int rc = -EINVAL, ringid;
466 
467 	if (cid < 0) {
468 		wil_err(wil, "No connection pending\n");
469 		goto out;
470 	}
471 	ringid = wil_find_free_vring(wil);
472 	if (ringid < 0) {
473 		wil_err(wil, "No free vring found\n");
474 		goto out;
475 	}
476 
477 	wil_dbg_wmi(wil, "Configure for connection CID %d vring %d\n",
478 		    cid, ringid);
479 
480 	rc = wil_vring_init_tx(wil, ringid, 1 << tx_ring_order, cid, 0);
481 	if (rc)
482 		wil_err(wil, "wil_vring_init_tx for CID %d vring %d failed\n",
483 			cid, ringid);
484 
485 out:
486 	return rc;
487 }
488 
489 int wil_bcast_init(struct wil6210_priv *wil)
490 {
491 	int ri = wil->bcast_vring, rc;
492 
493 	if ((ri >= 0) && wil->vring_tx[ri].va)
494 		return 0;
495 
496 	ri = wil_find_free_vring(wil);
497 	if (ri < 0)
498 		return ri;
499 
500 	wil->bcast_vring = ri;
501 	rc = wil_vring_init_bcast(wil, ri, 1 << bcast_ring_order);
502 	if (rc)
503 		wil->bcast_vring = -1;
504 
505 	return rc;
506 }
507 
508 void wil_bcast_fini(struct wil6210_priv *wil)
509 {
510 	int ri = wil->bcast_vring;
511 
512 	if (ri < 0)
513 		return;
514 
515 	wil->bcast_vring = -1;
516 	wil_vring_fini_tx(wil, ri);
517 }
518 
519 int wil_priv_init(struct wil6210_priv *wil)
520 {
521 	uint i;
522 
523 	wil_dbg_misc(wil, "priv_init\n");
524 
525 	memset(wil->sta, 0, sizeof(wil->sta));
526 	for (i = 0; i < WIL6210_MAX_CID; i++)
527 		spin_lock_init(&wil->sta[i].tid_rx_lock);
528 
529 	for (i = 0; i < WIL6210_MAX_TX_RINGS; i++)
530 		spin_lock_init(&wil->vring_tx_data[i].lock);
531 
532 	mutex_init(&wil->mutex);
533 	mutex_init(&wil->wmi_mutex);
534 	mutex_init(&wil->probe_client_mutex);
535 	mutex_init(&wil->p2p_wdev_mutex);
536 	mutex_init(&wil->halp.lock);
537 
538 	init_completion(&wil->wmi_ready);
539 	init_completion(&wil->wmi_call);
540 	init_completion(&wil->halp.comp);
541 
542 	wil->bcast_vring = -1;
543 	timer_setup(&wil->connect_timer, wil_connect_timer_fn, 0);
544 	timer_setup(&wil->scan_timer, wil_scan_timer_fn, 0);
545 	timer_setup(&wil->p2p.discovery_timer, wil_p2p_discovery_timer_fn, 0);
546 
547 	INIT_WORK(&wil->disconnect_worker, wil_disconnect_worker);
548 	INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
549 	INIT_WORK(&wil->fw_error_worker, wil_fw_error_worker);
550 	INIT_WORK(&wil->probe_client_worker, wil_probe_client_worker);
551 	INIT_WORK(&wil->p2p.delayed_listen_work, wil_p2p_delayed_listen_work);
552 
553 	INIT_LIST_HEAD(&wil->pending_wmi_ev);
554 	INIT_LIST_HEAD(&wil->probe_client_pending);
555 	spin_lock_init(&wil->wmi_ev_lock);
556 	spin_lock_init(&wil->net_queue_lock);
557 	wil->net_queue_stopped = 1;
558 	init_waitqueue_head(&wil->wq);
559 
560 	wil->wmi_wq = create_singlethread_workqueue(WIL_NAME "_wmi");
561 	if (!wil->wmi_wq)
562 		return -EAGAIN;
563 
564 	wil->wq_service = create_singlethread_workqueue(WIL_NAME "_service");
565 	if (!wil->wq_service)
566 		goto out_wmi_wq;
567 
568 	wil->last_fw_recovery = jiffies;
569 	wil->tx_interframe_timeout = WIL6210_ITR_TX_INTERFRAME_TIMEOUT_DEFAULT;
570 	wil->rx_interframe_timeout = WIL6210_ITR_RX_INTERFRAME_TIMEOUT_DEFAULT;
571 	wil->tx_max_burst_duration = WIL6210_ITR_TX_MAX_BURST_DURATION_DEFAULT;
572 	wil->rx_max_burst_duration = WIL6210_ITR_RX_MAX_BURST_DURATION_DEFAULT;
573 
574 	if (rx_ring_overflow_thrsh == WIL6210_RX_HIGH_TRSH_INIT)
575 		rx_ring_overflow_thrsh = WIL6210_RX_HIGH_TRSH_DEFAULT;
576 
577 	wil->ps_profile =  WMI_PS_PROFILE_TYPE_DEFAULT;
578 
579 	wil->wakeup_trigger = WMI_WAKEUP_TRIGGER_UCAST |
580 			      WMI_WAKEUP_TRIGGER_BCAST;
581 	memset(&wil->suspend_stats, 0, sizeof(wil->suspend_stats));
582 	wil->vring_idle_trsh = 16;
583 
584 	return 0;
585 
586 out_wmi_wq:
587 	destroy_workqueue(wil->wmi_wq);
588 
589 	return -EAGAIN;
590 }
591 
592 void wil6210_bus_request(struct wil6210_priv *wil, u32 kbps)
593 {
594 	if (wil->platform_ops.bus_request) {
595 		wil->bus_request_kbps = kbps;
596 		wil->platform_ops.bus_request(wil->platform_handle, kbps);
597 	}
598 }
599 
600 /**
601  * wil6210_disconnect - disconnect one connection
602  * @wil: driver context
603  * @bssid: peer to disconnect, NULL to disconnect all
604  * @reason_code: Reason code for the Disassociation frame
605  * @from_event: whether is invoked from FW event handler
606  *
607  * Disconnect and release associated resources. If invoked not from the
608  * FW event handler, issue WMI command(s) to trigger MAC disconnect.
609  */
610 void wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid,
611 			u16 reason_code, bool from_event)
612 {
613 	wil_dbg_misc(wil, "disconnect\n");
614 
615 	del_timer_sync(&wil->connect_timer);
616 	_wil6210_disconnect(wil, bssid, reason_code, from_event);
617 }
618 
619 void wil_priv_deinit(struct wil6210_priv *wil)
620 {
621 	wil_dbg_misc(wil, "priv_deinit\n");
622 
623 	wil_set_recovery_state(wil, fw_recovery_idle);
624 	del_timer_sync(&wil->scan_timer);
625 	del_timer_sync(&wil->p2p.discovery_timer);
626 	cancel_work_sync(&wil->disconnect_worker);
627 	cancel_work_sync(&wil->fw_error_worker);
628 	cancel_work_sync(&wil->p2p.discovery_expired_work);
629 	cancel_work_sync(&wil->p2p.delayed_listen_work);
630 	mutex_lock(&wil->mutex);
631 	wil6210_disconnect(wil, NULL, WLAN_REASON_DEAUTH_LEAVING, false);
632 	mutex_unlock(&wil->mutex);
633 	wmi_event_flush(wil);
634 	wil_probe_client_flush(wil);
635 	cancel_work_sync(&wil->probe_client_worker);
636 	destroy_workqueue(wil->wq_service);
637 	destroy_workqueue(wil->wmi_wq);
638 }
639 
640 static inline void wil_halt_cpu(struct wil6210_priv *wil)
641 {
642 	wil_w(wil, RGF_USER_USER_CPU_0, BIT_USER_USER_CPU_MAN_RST);
643 	wil_w(wil, RGF_USER_MAC_CPU_0,  BIT_USER_MAC_CPU_MAN_RST);
644 }
645 
646 static inline void wil_release_cpu(struct wil6210_priv *wil)
647 {
648 	/* Start CPU */
649 	wil_w(wil, RGF_USER_USER_CPU_0, 1);
650 }
651 
652 static void wil_set_oob_mode(struct wil6210_priv *wil, u8 mode)
653 {
654 	wil_info(wil, "oob_mode to %d\n", mode);
655 	switch (mode) {
656 	case 0:
657 		wil_c(wil, RGF_USER_USAGE_6, BIT_USER_OOB_MODE |
658 		      BIT_USER_OOB_R2_MODE);
659 		break;
660 	case 1:
661 		wil_c(wil, RGF_USER_USAGE_6, BIT_USER_OOB_R2_MODE);
662 		wil_s(wil, RGF_USER_USAGE_6, BIT_USER_OOB_MODE);
663 		break;
664 	case 2:
665 		wil_c(wil, RGF_USER_USAGE_6, BIT_USER_OOB_MODE);
666 		wil_s(wil, RGF_USER_USAGE_6, BIT_USER_OOB_R2_MODE);
667 		break;
668 	default:
669 		wil_err(wil, "invalid oob_mode: %d\n", mode);
670 	}
671 }
672 
673 static int wil_target_reset(struct wil6210_priv *wil)
674 {
675 	int delay = 0;
676 	u32 x, x1 = 0;
677 
678 	wil_dbg_misc(wil, "Resetting \"%s\"...\n", wil->hw_name);
679 
680 	/* Clear MAC link up */
681 	wil_s(wil, RGF_HP_CTRL, BIT(15));
682 	wil_s(wil, RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_HPAL_PERST_FROM_PAD);
683 	wil_s(wil, RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_CAR_PERST_RST);
684 
685 	wil_halt_cpu(wil);
686 
687 	/* clear all boot loader "ready" bits */
688 	wil_w(wil, RGF_USER_BL +
689 	      offsetof(struct bl_dedicated_registers_v0, boot_loader_ready), 0);
690 	/* Clear Fw Download notification */
691 	wil_c(wil, RGF_USER_USAGE_6, BIT(0));
692 
693 	wil_s(wil, RGF_CAF_OSC_CONTROL, BIT_CAF_OSC_XTAL_EN);
694 	/* XTAL stabilization should take about 3ms */
695 	usleep_range(5000, 7000);
696 	x = wil_r(wil, RGF_CAF_PLL_LOCK_STATUS);
697 	if (!(x & BIT_CAF_OSC_DIG_XTAL_STABLE)) {
698 		wil_err(wil, "Xtal stabilization timeout\n"
699 			"RGF_CAF_PLL_LOCK_STATUS = 0x%08x\n", x);
700 		return -ETIME;
701 	}
702 	/* switch 10k to XTAL*/
703 	wil_c(wil, RGF_USER_SPARROW_M_4, BIT_SPARROW_M_4_SEL_SLEEP_OR_REF);
704 	/* 40 MHz */
705 	wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_CAR_AHB_SW_SEL);
706 
707 	wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x3ff81f);
708 	wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0xf);
709 
710 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
711 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
712 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x000000f0);
713 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FE00);
714 
715 	wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x0);
716 	wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0x0);
717 
718 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
719 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
720 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
721 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
722 
723 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000003);
724 	/* reset A2 PCIE AHB */
725 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000);
726 
727 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
728 
729 	/* wait until device ready. typical time is 20..80 msec */
730 	do {
731 		msleep(RST_DELAY);
732 		x = wil_r(wil, RGF_USER_BL +
733 			  offsetof(struct bl_dedicated_registers_v0,
734 				   boot_loader_ready));
735 		if (x1 != x) {
736 			wil_dbg_misc(wil, "BL.ready 0x%08x => 0x%08x\n", x1, x);
737 			x1 = x;
738 		}
739 		if (delay++ > RST_COUNT) {
740 			wil_err(wil, "Reset not completed, bl.ready 0x%08x\n",
741 				x);
742 			return -ETIME;
743 		}
744 	} while (x != BL_READY);
745 
746 	wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
747 
748 	/* enable fix for HW bug related to the SA/DA swap in AP Rx */
749 	wil_s(wil, RGF_DMA_OFUL_NID_0, BIT_DMA_OFUL_NID_0_RX_EXT_TR_EN |
750 	      BIT_DMA_OFUL_NID_0_RX_EXT_A3_SRC);
751 
752 	wil_dbg_misc(wil, "Reset completed in %d ms\n", delay * RST_DELAY);
753 	return 0;
754 }
755 
756 static void wil_collect_fw_info(struct wil6210_priv *wil)
757 {
758 	struct wiphy *wiphy = wil_to_wiphy(wil);
759 	u8 retry_short;
760 	int rc;
761 
762 	wil_refresh_fw_capabilities(wil);
763 
764 	rc = wmi_get_mgmt_retry(wil, &retry_short);
765 	if (!rc) {
766 		wiphy->retry_short = retry_short;
767 		wil_dbg_misc(wil, "FW retry_short: %d\n", retry_short);
768 	}
769 }
770 
771 void wil_refresh_fw_capabilities(struct wil6210_priv *wil)
772 {
773 	struct wiphy *wiphy = wil_to_wiphy(wil);
774 
775 	wil->keep_radio_on_during_sleep =
776 		wil->platform_ops.keep_radio_on_during_sleep &&
777 		wil->platform_ops.keep_radio_on_during_sleep(
778 			wil->platform_handle) &&
779 		test_bit(WMI_FW_CAPABILITY_D3_SUSPEND, wil->fw_capabilities);
780 
781 	wil_info(wil, "keep_radio_on_during_sleep (%d)\n",
782 		 wil->keep_radio_on_during_sleep);
783 
784 	if (test_bit(WMI_FW_CAPABILITY_RSSI_REPORTING, wil->fw_capabilities))
785 		wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
786 	else
787 		wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
788 }
789 
790 void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
791 {
792 	le32_to_cpus(&r->base);
793 	le16_to_cpus(&r->entry_size);
794 	le16_to_cpus(&r->size);
795 	le32_to_cpus(&r->tail);
796 	le32_to_cpus(&r->head);
797 }
798 
799 static int wil_get_bl_info(struct wil6210_priv *wil)
800 {
801 	struct net_device *ndev = wil_to_ndev(wil);
802 	struct wiphy *wiphy = wil_to_wiphy(wil);
803 	union {
804 		struct bl_dedicated_registers_v0 bl0;
805 		struct bl_dedicated_registers_v1 bl1;
806 	} bl;
807 	u32 bl_ver;
808 	u8 *mac;
809 	u16 rf_status;
810 
811 	wil_memcpy_fromio_32(&bl, wil->csr + HOSTADDR(RGF_USER_BL),
812 			     sizeof(bl));
813 	bl_ver = le32_to_cpu(bl.bl0.boot_loader_struct_version);
814 	mac = bl.bl0.mac_address;
815 
816 	if (bl_ver == 0) {
817 		le32_to_cpus(&bl.bl0.rf_type);
818 		le32_to_cpus(&bl.bl0.baseband_type);
819 		rf_status = 0; /* actually, unknown */
820 		wil_info(wil,
821 			 "Boot Loader struct v%d: MAC = %pM RF = 0x%08x bband = 0x%08x\n",
822 			 bl_ver, mac,
823 			 bl.bl0.rf_type, bl.bl0.baseband_type);
824 		wil_info(wil, "Boot Loader build unknown for struct v0\n");
825 	} else {
826 		le16_to_cpus(&bl.bl1.rf_type);
827 		rf_status = le16_to_cpu(bl.bl1.rf_status);
828 		le32_to_cpus(&bl.bl1.baseband_type);
829 		le16_to_cpus(&bl.bl1.bl_version_subminor);
830 		le16_to_cpus(&bl.bl1.bl_version_build);
831 		wil_info(wil,
832 			 "Boot Loader struct v%d: MAC = %pM RF = 0x%04x (status 0x%04x) bband = 0x%08x\n",
833 			 bl_ver, mac,
834 			 bl.bl1.rf_type, rf_status,
835 			 bl.bl1.baseband_type);
836 		wil_info(wil, "Boot Loader build %d.%d.%d.%d\n",
837 			 bl.bl1.bl_version_major, bl.bl1.bl_version_minor,
838 			 bl.bl1.bl_version_subminor, bl.bl1.bl_version_build);
839 	}
840 
841 	if (!is_valid_ether_addr(mac)) {
842 		wil_err(wil, "BL: Invalid MAC %pM\n", mac);
843 		return -EINVAL;
844 	}
845 
846 	ether_addr_copy(ndev->perm_addr, mac);
847 	ether_addr_copy(wiphy->perm_addr, mac);
848 	if (!is_valid_ether_addr(ndev->dev_addr))
849 		ether_addr_copy(ndev->dev_addr, mac);
850 
851 	if (rf_status) {/* bad RF cable? */
852 		wil_err(wil, "RF communication error 0x%04x",
853 			rf_status);
854 		return -EAGAIN;
855 	}
856 
857 	return 0;
858 }
859 
860 static void wil_bl_crash_info(struct wil6210_priv *wil, bool is_err)
861 {
862 	u32 bl_assert_code, bl_assert_blink, bl_magic_number;
863 	u32 bl_ver = wil_r(wil, RGF_USER_BL +
864 			   offsetof(struct bl_dedicated_registers_v0,
865 				    boot_loader_struct_version));
866 
867 	if (bl_ver < 2)
868 		return;
869 
870 	bl_assert_code = wil_r(wil, RGF_USER_BL +
871 			       offsetof(struct bl_dedicated_registers_v1,
872 					bl_assert_code));
873 	bl_assert_blink = wil_r(wil, RGF_USER_BL +
874 				offsetof(struct bl_dedicated_registers_v1,
875 					 bl_assert_blink));
876 	bl_magic_number = wil_r(wil, RGF_USER_BL +
877 				offsetof(struct bl_dedicated_registers_v1,
878 					 bl_magic_number));
879 
880 	if (is_err) {
881 		wil_err(wil,
882 			"BL assert code 0x%08x blink 0x%08x magic 0x%08x\n",
883 			bl_assert_code, bl_assert_blink, bl_magic_number);
884 	} else {
885 		wil_dbg_misc(wil,
886 			     "BL assert code 0x%08x blink 0x%08x magic 0x%08x\n",
887 			     bl_assert_code, bl_assert_blink, bl_magic_number);
888 	}
889 }
890 
891 static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
892 {
893 	ulong to = msecs_to_jiffies(1000);
894 	ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
895 
896 	if (0 == left) {
897 		wil_err(wil, "Firmware not ready\n");
898 		return -ETIME;
899 	} else {
900 		wil_info(wil, "FW ready after %d ms. HW version 0x%08x\n",
901 			 jiffies_to_msecs(to-left), wil->hw_version);
902 	}
903 	return 0;
904 }
905 
906 void wil_abort_scan(struct wil6210_priv *wil, bool sync)
907 {
908 	int rc;
909 	struct cfg80211_scan_info info = {
910 		.aborted = true,
911 	};
912 
913 	lockdep_assert_held(&wil->p2p_wdev_mutex);
914 
915 	if (!wil->scan_request)
916 		return;
917 
918 	wil_dbg_misc(wil, "Abort scan_request 0x%p\n", wil->scan_request);
919 	del_timer_sync(&wil->scan_timer);
920 	mutex_unlock(&wil->p2p_wdev_mutex);
921 	rc = wmi_abort_scan(wil);
922 	if (!rc && sync)
923 		wait_event_interruptible_timeout(wil->wq, !wil->scan_request,
924 						 msecs_to_jiffies(
925 						 WAIT_FOR_SCAN_ABORT_MS));
926 
927 	mutex_lock(&wil->p2p_wdev_mutex);
928 	if (wil->scan_request) {
929 		cfg80211_scan_done(wil->scan_request, &info);
930 		wil->scan_request = NULL;
931 	}
932 }
933 
934 int wil_ps_update(struct wil6210_priv *wil, enum wmi_ps_profile_type ps_profile)
935 {
936 	int rc;
937 
938 	if (!test_bit(WMI_FW_CAPABILITY_PS_CONFIG, wil->fw_capabilities)) {
939 		wil_err(wil, "set_power_mgmt not supported\n");
940 		return -EOPNOTSUPP;
941 	}
942 
943 	rc  = wmi_ps_dev_profile_cfg(wil, ps_profile);
944 	if (rc)
945 		wil_err(wil, "wmi_ps_dev_profile_cfg failed (%d)\n", rc);
946 	else
947 		wil->ps_profile = ps_profile;
948 
949 	return rc;
950 }
951 
952 static void wil_pre_fw_config(struct wil6210_priv *wil)
953 {
954 	/* Mark FW as loaded from host */
955 	wil_s(wil, RGF_USER_USAGE_6, 1);
956 
957 	/* clear any interrupts which on-card-firmware
958 	 * may have set
959 	 */
960 	wil6210_clear_irq(wil);
961 	/* CAF_ICR - clear and mask */
962 	/* it is W1C, clear by writing back same value */
963 	wil_s(wil, RGF_CAF_ICR + offsetof(struct RGF_ICR, ICR), 0);
964 	wil_w(wil, RGF_CAF_ICR + offsetof(struct RGF_ICR, IMV), ~0);
965 	/* clear PAL_UNIT_ICR (potential D0->D3 leftover) */
966 	wil_s(wil, RGF_PAL_UNIT_ICR + offsetof(struct RGF_ICR, ICR), 0);
967 
968 	if (wil->fw_calib_result > 0) {
969 		__le32 val = cpu_to_le32(wil->fw_calib_result |
970 						(CALIB_RESULT_SIGNATURE << 8));
971 		wil_w(wil, RGF_USER_FW_CALIB_RESULT, (u32 __force)val);
972 	}
973 }
974 
975 /*
976  * We reset all the structures, and we reset the UMAC.
977  * After calling this routine, you're expected to reload
978  * the firmware.
979  */
980 int wil_reset(struct wil6210_priv *wil, bool load_fw)
981 {
982 	int rc;
983 
984 	wil_dbg_misc(wil, "reset\n");
985 
986 	WARN_ON(!mutex_is_locked(&wil->mutex));
987 	WARN_ON(test_bit(wil_status_napi_en, wil->status));
988 
989 	if (debug_fw) {
990 		static const u8 mac[ETH_ALEN] = {
991 			0x00, 0xde, 0xad, 0x12, 0x34, 0x56,
992 		};
993 		struct net_device *ndev = wil_to_ndev(wil);
994 
995 		ether_addr_copy(ndev->perm_addr, mac);
996 		ether_addr_copy(ndev->dev_addr, ndev->perm_addr);
997 		return 0;
998 	}
999 
1000 	if (wil->hw_version == HW_VER_UNKNOWN)
1001 		return -ENODEV;
1002 
1003 	if (wil->platform_ops.notify) {
1004 		rc = wil->platform_ops.notify(wil->platform_handle,
1005 					      WIL_PLATFORM_EVT_PRE_RESET);
1006 		if (rc)
1007 			wil_err(wil, "PRE_RESET platform notify failed, rc %d\n",
1008 				rc);
1009 	}
1010 
1011 	set_bit(wil_status_resetting, wil->status);
1012 
1013 	cancel_work_sync(&wil->disconnect_worker);
1014 	wil6210_disconnect(wil, NULL, WLAN_REASON_DEAUTH_LEAVING, false);
1015 	wil_bcast_fini(wil);
1016 
1017 	/* Disable device led before reset*/
1018 	wmi_led_cfg(wil, false);
1019 
1020 	mutex_lock(&wil->p2p_wdev_mutex);
1021 	wil_abort_scan(wil, false);
1022 	mutex_unlock(&wil->p2p_wdev_mutex);
1023 
1024 	/* prevent NAPI from being scheduled and prevent wmi commands */
1025 	mutex_lock(&wil->wmi_mutex);
1026 	bitmap_zero(wil->status, wil_status_last);
1027 	mutex_unlock(&wil->wmi_mutex);
1028 
1029 	wil_mask_irq(wil);
1030 
1031 	wmi_event_flush(wil);
1032 
1033 	flush_workqueue(wil->wq_service);
1034 	flush_workqueue(wil->wmi_wq);
1035 
1036 	wil_bl_crash_info(wil, false);
1037 	wil_disable_irq(wil);
1038 	rc = wil_target_reset(wil);
1039 	wil6210_clear_irq(wil);
1040 	wil_enable_irq(wil);
1041 	wil_rx_fini(wil);
1042 	if (rc) {
1043 		wil_bl_crash_info(wil, true);
1044 		return rc;
1045 	}
1046 
1047 	rc = wil_get_bl_info(wil);
1048 	if (rc == -EAGAIN && !load_fw) /* ignore RF error if not going up */
1049 		rc = 0;
1050 	if (rc)
1051 		return rc;
1052 
1053 	wil_set_oob_mode(wil, oob_mode);
1054 	if (load_fw) {
1055 		wil_info(wil, "Use firmware <%s> + board <%s>\n",
1056 			 wil->wil_fw_name, WIL_BOARD_FILE_NAME);
1057 
1058 		wil_halt_cpu(wil);
1059 		memset(wil->fw_version, 0, sizeof(wil->fw_version));
1060 		/* Loading f/w from the file */
1061 		rc = wil_request_firmware(wil, wil->wil_fw_name, true);
1062 		if (rc)
1063 			return rc;
1064 		rc = wil_request_firmware(wil, WIL_BOARD_FILE_NAME, true);
1065 		if (rc)
1066 			return rc;
1067 
1068 		wil_pre_fw_config(wil);
1069 		wil_release_cpu(wil);
1070 	}
1071 
1072 	/* init after reset */
1073 	wil->ap_isolate = 0;
1074 	reinit_completion(&wil->wmi_ready);
1075 	reinit_completion(&wil->wmi_call);
1076 	reinit_completion(&wil->halp.comp);
1077 
1078 	if (load_fw) {
1079 		wil_configure_interrupt_moderation(wil);
1080 		wil_unmask_irq(wil);
1081 
1082 		/* we just started MAC, wait for FW ready */
1083 		rc = wil_wait_for_fw_ready(wil);
1084 		if (rc)
1085 			return rc;
1086 
1087 		/* check FW is responsive */
1088 		rc = wmi_echo(wil);
1089 		if (rc) {
1090 			wil_err(wil, "wmi_echo failed, rc %d\n", rc);
1091 			return rc;
1092 		}
1093 
1094 		wil_collect_fw_info(wil);
1095 
1096 		if (wil->ps_profile != WMI_PS_PROFILE_TYPE_DEFAULT)
1097 			wil_ps_update(wil, wil->ps_profile);
1098 
1099 		if (wil->platform_ops.notify) {
1100 			rc = wil->platform_ops.notify(wil->platform_handle,
1101 						      WIL_PLATFORM_EVT_FW_RDY);
1102 			if (rc) {
1103 				wil_err(wil, "FW_RDY notify failed, rc %d\n",
1104 					rc);
1105 				rc = 0;
1106 			}
1107 		}
1108 	}
1109 
1110 	return rc;
1111 }
1112 
1113 void wil_fw_error_recovery(struct wil6210_priv *wil)
1114 {
1115 	wil_dbg_misc(wil, "starting fw error recovery\n");
1116 
1117 	if (test_bit(wil_status_resetting, wil->status)) {
1118 		wil_info(wil, "Reset already in progress\n");
1119 		return;
1120 	}
1121 
1122 	wil->recovery_state = fw_recovery_pending;
1123 	schedule_work(&wil->fw_error_worker);
1124 }
1125 
1126 int __wil_up(struct wil6210_priv *wil)
1127 {
1128 	struct net_device *ndev = wil_to_ndev(wil);
1129 	struct wireless_dev *wdev = wil->wdev;
1130 	int rc;
1131 
1132 	WARN_ON(!mutex_is_locked(&wil->mutex));
1133 
1134 	rc = wil_reset(wil, true);
1135 	if (rc)
1136 		return rc;
1137 
1138 	/* Rx VRING. After MAC and beacon */
1139 	rc = wil_rx_init(wil, 1 << rx_ring_order);
1140 	if (rc)
1141 		return rc;
1142 
1143 	switch (wdev->iftype) {
1144 	case NL80211_IFTYPE_STATION:
1145 		wil_dbg_misc(wil, "type: STATION\n");
1146 		ndev->type = ARPHRD_ETHER;
1147 		break;
1148 	case NL80211_IFTYPE_AP:
1149 		wil_dbg_misc(wil, "type: AP\n");
1150 		ndev->type = ARPHRD_ETHER;
1151 		break;
1152 	case NL80211_IFTYPE_P2P_CLIENT:
1153 		wil_dbg_misc(wil, "type: P2P_CLIENT\n");
1154 		ndev->type = ARPHRD_ETHER;
1155 		break;
1156 	case NL80211_IFTYPE_P2P_GO:
1157 		wil_dbg_misc(wil, "type: P2P_GO\n");
1158 		ndev->type = ARPHRD_ETHER;
1159 		break;
1160 	case NL80211_IFTYPE_MONITOR:
1161 		wil_dbg_misc(wil, "type: Monitor\n");
1162 		ndev->type = ARPHRD_IEEE80211_RADIOTAP;
1163 		/* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
1164 		break;
1165 	default:
1166 		return -EOPNOTSUPP;
1167 	}
1168 
1169 	/* MAC address - pre-requisite for other commands */
1170 	wmi_set_mac_address(wil, ndev->dev_addr);
1171 
1172 	wil_dbg_misc(wil, "NAPI enable\n");
1173 	napi_enable(&wil->napi_rx);
1174 	napi_enable(&wil->napi_tx);
1175 	set_bit(wil_status_napi_en, wil->status);
1176 
1177 	wil6210_bus_request(wil, WIL_DEFAULT_BUS_REQUEST_KBPS);
1178 
1179 	return 0;
1180 }
1181 
1182 int wil_up(struct wil6210_priv *wil)
1183 {
1184 	int rc;
1185 
1186 	wil_dbg_misc(wil, "up\n");
1187 
1188 	mutex_lock(&wil->mutex);
1189 	rc = __wil_up(wil);
1190 	mutex_unlock(&wil->mutex);
1191 
1192 	return rc;
1193 }
1194 
1195 int __wil_down(struct wil6210_priv *wil)
1196 {
1197 	WARN_ON(!mutex_is_locked(&wil->mutex));
1198 
1199 	set_bit(wil_status_resetting, wil->status);
1200 
1201 	wil6210_bus_request(wil, 0);
1202 
1203 	wil_disable_irq(wil);
1204 	if (test_and_clear_bit(wil_status_napi_en, wil->status)) {
1205 		napi_disable(&wil->napi_rx);
1206 		napi_disable(&wil->napi_tx);
1207 		wil_dbg_misc(wil, "NAPI disable\n");
1208 	}
1209 	wil_enable_irq(wil);
1210 
1211 	mutex_lock(&wil->p2p_wdev_mutex);
1212 	wil_p2p_stop_radio_operations(wil);
1213 	wil_abort_scan(wil, false);
1214 	mutex_unlock(&wil->p2p_wdev_mutex);
1215 
1216 	wil_reset(wil, false);
1217 
1218 	return 0;
1219 }
1220 
1221 int wil_down(struct wil6210_priv *wil)
1222 {
1223 	int rc;
1224 
1225 	wil_dbg_misc(wil, "down\n");
1226 
1227 	wil_set_recovery_state(wil, fw_recovery_idle);
1228 	mutex_lock(&wil->mutex);
1229 	rc = __wil_down(wil);
1230 	mutex_unlock(&wil->mutex);
1231 
1232 	return rc;
1233 }
1234 
1235 int wil_find_cid(struct wil6210_priv *wil, const u8 *mac)
1236 {
1237 	int i;
1238 	int rc = -ENOENT;
1239 
1240 	for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
1241 		if ((wil->sta[i].status != wil_sta_unused) &&
1242 		    ether_addr_equal(wil->sta[i].addr, mac)) {
1243 			rc = i;
1244 			break;
1245 		}
1246 	}
1247 
1248 	return rc;
1249 }
1250 
1251 void wil_halp_vote(struct wil6210_priv *wil)
1252 {
1253 	unsigned long rc;
1254 	unsigned long to_jiffies = msecs_to_jiffies(WAIT_FOR_HALP_VOTE_MS);
1255 
1256 	mutex_lock(&wil->halp.lock);
1257 
1258 	wil_dbg_irq(wil, "halp_vote: start, HALP ref_cnt (%d)\n",
1259 		    wil->halp.ref_cnt);
1260 
1261 	if (++wil->halp.ref_cnt == 1) {
1262 		reinit_completion(&wil->halp.comp);
1263 		wil6210_set_halp(wil);
1264 		rc = wait_for_completion_timeout(&wil->halp.comp, to_jiffies);
1265 		if (!rc) {
1266 			wil_err(wil, "HALP vote timed out\n");
1267 			/* Mask HALP as done in case the interrupt is raised */
1268 			wil6210_mask_halp(wil);
1269 		} else {
1270 			wil_dbg_irq(wil,
1271 				    "halp_vote: HALP vote completed after %d ms\n",
1272 				    jiffies_to_msecs(to_jiffies - rc));
1273 		}
1274 	}
1275 
1276 	wil_dbg_irq(wil, "halp_vote: end, HALP ref_cnt (%d)\n",
1277 		    wil->halp.ref_cnt);
1278 
1279 	mutex_unlock(&wil->halp.lock);
1280 }
1281 
1282 void wil_halp_unvote(struct wil6210_priv *wil)
1283 {
1284 	WARN_ON(wil->halp.ref_cnt == 0);
1285 
1286 	mutex_lock(&wil->halp.lock);
1287 
1288 	wil_dbg_irq(wil, "halp_unvote: start, HALP ref_cnt (%d)\n",
1289 		    wil->halp.ref_cnt);
1290 
1291 	if (--wil->halp.ref_cnt == 0) {
1292 		wil6210_clear_halp(wil);
1293 		wil_dbg_irq(wil, "HALP unvote\n");
1294 	}
1295 
1296 	wil_dbg_irq(wil, "halp_unvote:end, HALP ref_cnt (%d)\n",
1297 		    wil->halp.ref_cnt);
1298 
1299 	mutex_unlock(&wil->halp.lock);
1300 }
1301