1 /*
2  * Copyright (c) 2012-2016 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 bool debug_fw; /* = false; */
27 module_param(debug_fw, bool, S_IRUGO);
28 MODULE_PARM_DESC(debug_fw, " do not perform card reset. For FW debug");
29 
30 static bool oob_mode;
31 module_param(oob_mode, bool, S_IRUGO);
32 MODULE_PARM_DESC(oob_mode,
33 		 " enable out of the box (OOB) mode in FW, for diagnostics and certification");
34 
35 bool no_fw_recovery;
36 module_param(no_fw_recovery, bool, S_IRUGO | S_IWUSR);
37 MODULE_PARM_DESC(no_fw_recovery, " disable automatic FW error recovery");
38 
39 /* if not set via modparam, will be set to default value of 1/8 of
40  * rx ring size during init flow
41  */
42 unsigned short rx_ring_overflow_thrsh = WIL6210_RX_HIGH_TRSH_INIT;
43 module_param(rx_ring_overflow_thrsh, ushort, S_IRUGO);
44 MODULE_PARM_DESC(rx_ring_overflow_thrsh,
45 		 " RX ring overflow threshold in descriptors.");
46 
47 /* We allow allocation of more than 1 page buffers to support large packets.
48  * It is suboptimal behavior performance wise in case MTU above page size.
49  */
50 unsigned int mtu_max = TXRX_BUF_LEN_DEFAULT - WIL_MAX_MPDU_OVERHEAD;
51 static int mtu_max_set(const char *val, const struct kernel_param *kp)
52 {
53 	int ret;
54 
55 	/* sets mtu_max directly. no need to restore it in case of
56 	 * illegal value since we assume this will fail insmod
57 	 */
58 	ret = param_set_uint(val, kp);
59 	if (ret)
60 		return ret;
61 
62 	if (mtu_max < 68 || mtu_max > WIL_MAX_ETH_MTU)
63 		ret = -EINVAL;
64 
65 	return ret;
66 }
67 
68 static const struct kernel_param_ops mtu_max_ops = {
69 	.set = mtu_max_set,
70 	.get = param_get_uint,
71 };
72 
73 module_param_cb(mtu_max, &mtu_max_ops, &mtu_max, S_IRUGO);
74 MODULE_PARM_DESC(mtu_max, " Max MTU value.");
75 
76 static uint rx_ring_order = WIL_RX_RING_SIZE_ORDER_DEFAULT;
77 static uint tx_ring_order = WIL_TX_RING_SIZE_ORDER_DEFAULT;
78 static uint bcast_ring_order = WIL_BCAST_RING_SIZE_ORDER_DEFAULT;
79 
80 static int ring_order_set(const char *val, const struct kernel_param *kp)
81 {
82 	int ret;
83 	uint x;
84 
85 	ret = kstrtouint(val, 0, &x);
86 	if (ret)
87 		return ret;
88 
89 	if ((x < WIL_RING_SIZE_ORDER_MIN) || (x > WIL_RING_SIZE_ORDER_MAX))
90 		return -EINVAL;
91 
92 	*((uint *)kp->arg) = x;
93 
94 	return 0;
95 }
96 
97 static const struct kernel_param_ops ring_order_ops = {
98 	.set = ring_order_set,
99 	.get = param_get_uint,
100 };
101 
102 module_param_cb(rx_ring_order, &ring_order_ops, &rx_ring_order, S_IRUGO);
103 MODULE_PARM_DESC(rx_ring_order, " Rx ring order; size = 1 << order");
104 module_param_cb(tx_ring_order, &ring_order_ops, &tx_ring_order, S_IRUGO);
105 MODULE_PARM_DESC(tx_ring_order, " Tx ring order; size = 1 << order");
106 module_param_cb(bcast_ring_order, &ring_order_ops, &bcast_ring_order, S_IRUGO);
107 MODULE_PARM_DESC(bcast_ring_order, " Bcast ring order; size = 1 << order");
108 
109 #define RST_DELAY (20) /* msec, for loop in @wil_target_reset */
110 #define RST_COUNT (1 + 1000/RST_DELAY) /* round up to be above 1 sec total */
111 
112 /*
113  * Due to a hardware issue,
114  * one has to read/write to/from NIC in 32-bit chunks;
115  * regular memcpy_fromio and siblings will
116  * not work on 64-bit platform - it uses 64-bit transactions
117  *
118  * Force 32-bit transactions to enable NIC on 64-bit platforms
119  *
120  * To avoid byte swap on big endian host, __raw_{read|write}l
121  * should be used - {read|write}l would swap bytes to provide
122  * little endian on PCI value in host endianness.
123  */
124 void wil_memcpy_fromio_32(void *dst, const volatile void __iomem *src,
125 			  size_t count)
126 {
127 	u32 *d = dst;
128 	const volatile u32 __iomem *s = src;
129 
130 	/* size_t is unsigned, if (count%4 != 0) it will wrap */
131 	for (count += 4; count > 4; count -= 4)
132 		*d++ = __raw_readl(s++);
133 }
134 
135 void wil_memcpy_toio_32(volatile void __iomem *dst, const void *src,
136 			size_t count)
137 {
138 	volatile u32 __iomem *d = dst;
139 	const u32 *s = src;
140 
141 	for (count += 4; count > 4; count -= 4)
142 		__raw_writel(*s++, d++);
143 }
144 
145 static void wil_disconnect_cid(struct wil6210_priv *wil, int cid,
146 			       u16 reason_code, bool from_event)
147 __acquires(&sta->tid_rx_lock) __releases(&sta->tid_rx_lock)
148 {
149 	uint i;
150 	struct net_device *ndev = wil_to_ndev(wil);
151 	struct wireless_dev *wdev = wil->wdev;
152 	struct wil_sta_info *sta = &wil->sta[cid];
153 
154 	might_sleep();
155 	wil_dbg_misc(wil, "%s(CID %d, status %d)\n", __func__, cid,
156 		     sta->status);
157 	/* inform upper/lower layers */
158 	if (sta->status != wil_sta_unused) {
159 		if (!from_event)
160 			wmi_disconnect_sta(wil, sta->addr, reason_code, true);
161 
162 		switch (wdev->iftype) {
163 		case NL80211_IFTYPE_AP:
164 		case NL80211_IFTYPE_P2P_GO:
165 			/* AP-like interface */
166 			cfg80211_del_sta(ndev, sta->addr, GFP_KERNEL);
167 			break;
168 		default:
169 			break;
170 		}
171 		sta->status = wil_sta_unused;
172 	}
173 	/* reorder buffers */
174 	for (i = 0; i < WIL_STA_TID_NUM; i++) {
175 		struct wil_tid_ampdu_rx *r;
176 
177 		spin_lock_bh(&sta->tid_rx_lock);
178 
179 		r = sta->tid_rx[i];
180 		sta->tid_rx[i] = NULL;
181 		wil_tid_ampdu_rx_free(wil, r);
182 
183 		spin_unlock_bh(&sta->tid_rx_lock);
184 	}
185 	/* crypto context */
186 	memset(sta->tid_crypto_rx, 0, sizeof(sta->tid_crypto_rx));
187 	memset(&sta->group_crypto_rx, 0, sizeof(sta->group_crypto_rx));
188 	/* release vrings */
189 	for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++) {
190 		if (wil->vring2cid_tid[i][0] == cid)
191 			wil_vring_fini_tx(wil, i);
192 	}
193 	/* statistics */
194 	memset(&sta->stats, 0, sizeof(sta->stats));
195 }
196 
197 static void _wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid,
198 				u16 reason_code, bool from_event)
199 {
200 	int cid = -ENOENT;
201 	struct net_device *ndev = wil_to_ndev(wil);
202 	struct wireless_dev *wdev = wil->wdev;
203 
204 	might_sleep();
205 	wil_info(wil, "%s(bssid=%pM, reason=%d, ev%s)\n", __func__, bssid,
206 		 reason_code, from_event ? "+" : "-");
207 
208 	/* Cases are:
209 	 * - disconnect single STA, still connected
210 	 * - disconnect single STA, already disconnected
211 	 * - disconnect all
212 	 *
213 	 * For "disconnect all", there are 3 options:
214 	 * - bssid == NULL
215 	 * - bssid is broadcast address (ff:ff:ff:ff:ff:ff)
216 	 * - bssid is our MAC address
217 	 */
218 	if (bssid && !is_broadcast_ether_addr(bssid) &&
219 	    !ether_addr_equal_unaligned(ndev->dev_addr, bssid)) {
220 		cid = wil_find_cid(wil, bssid);
221 		wil_dbg_misc(wil, "Disconnect %pM, CID=%d, reason=%d\n",
222 			     bssid, cid, reason_code);
223 		if (cid >= 0) /* disconnect 1 peer */
224 			wil_disconnect_cid(wil, cid, reason_code, from_event);
225 	} else { /* all */
226 		wil_dbg_misc(wil, "Disconnect all\n");
227 		for (cid = 0; cid < WIL6210_MAX_CID; cid++)
228 			wil_disconnect_cid(wil, cid, reason_code, from_event);
229 	}
230 
231 	/* link state */
232 	switch (wdev->iftype) {
233 	case NL80211_IFTYPE_STATION:
234 	case NL80211_IFTYPE_P2P_CLIENT:
235 		wil_bcast_fini(wil);
236 		netif_tx_stop_all_queues(ndev);
237 		netif_carrier_off(ndev);
238 
239 		if (test_bit(wil_status_fwconnected, wil->status)) {
240 			clear_bit(wil_status_fwconnected, wil->status);
241 			cfg80211_disconnected(ndev, reason_code,
242 					      NULL, 0, false, GFP_KERNEL);
243 		} else if (test_bit(wil_status_fwconnecting, wil->status)) {
244 			cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
245 						WLAN_STATUS_UNSPECIFIED_FAILURE,
246 						GFP_KERNEL);
247 		}
248 		clear_bit(wil_status_fwconnecting, wil->status);
249 		break;
250 	default:
251 		break;
252 	}
253 }
254 
255 static void wil_disconnect_worker(struct work_struct *work)
256 {
257 	struct wil6210_priv *wil = container_of(work,
258 			struct wil6210_priv, disconnect_worker);
259 
260 	mutex_lock(&wil->mutex);
261 	_wil6210_disconnect(wil, NULL, WLAN_REASON_UNSPECIFIED, false);
262 	mutex_unlock(&wil->mutex);
263 }
264 
265 static void wil_connect_timer_fn(ulong x)
266 {
267 	struct wil6210_priv *wil = (void *)x;
268 	bool q;
269 
270 	wil_err(wil, "Connect timeout detected, disconnect station\n");
271 
272 	/* reschedule to thread context - disconnect won't
273 	 * run from atomic context.
274 	 * queue on wmi_wq to prevent race with connect event.
275 	 */
276 	q = queue_work(wil->wmi_wq, &wil->disconnect_worker);
277 	wil_dbg_wmi(wil, "queue_work of disconnect_worker -> %d\n", q);
278 }
279 
280 static void wil_scan_timer_fn(ulong x)
281 {
282 	struct wil6210_priv *wil = (void *)x;
283 
284 	clear_bit(wil_status_fwready, wil->status);
285 	wil_err(wil, "Scan timeout detected, start fw error recovery\n");
286 	wil_fw_error_recovery(wil);
287 }
288 
289 static int wil_wait_for_recovery(struct wil6210_priv *wil)
290 {
291 	if (wait_event_interruptible(wil->wq, wil->recovery_state !=
292 				     fw_recovery_pending)) {
293 		wil_err(wil, "Interrupt, canceling recovery\n");
294 		return -ERESTARTSYS;
295 	}
296 	if (wil->recovery_state != fw_recovery_running) {
297 		wil_info(wil, "Recovery cancelled\n");
298 		return -EINTR;
299 	}
300 	wil_info(wil, "Proceed with recovery\n");
301 	return 0;
302 }
303 
304 void wil_set_recovery_state(struct wil6210_priv *wil, int state)
305 {
306 	wil_dbg_misc(wil, "%s(%d -> %d)\n", __func__,
307 		     wil->recovery_state, state);
308 
309 	wil->recovery_state = state;
310 	wake_up_interruptible(&wil->wq);
311 }
312 
313 bool wil_is_recovery_blocked(struct wil6210_priv *wil)
314 {
315 	return no_fw_recovery && (wil->recovery_state == fw_recovery_pending);
316 }
317 
318 static void wil_fw_error_worker(struct work_struct *work)
319 {
320 	struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
321 						fw_error_worker);
322 	struct wireless_dev *wdev = wil->wdev;
323 
324 	wil_dbg_misc(wil, "fw error worker\n");
325 
326 	if (!netif_running(wil_to_ndev(wil))) {
327 		wil_info(wil, "No recovery - interface is down\n");
328 		return;
329 	}
330 
331 	/* increment @recovery_count if less then WIL6210_FW_RECOVERY_TO
332 	 * passed since last recovery attempt
333 	 */
334 	if (time_is_after_jiffies(wil->last_fw_recovery +
335 				  WIL6210_FW_RECOVERY_TO))
336 		wil->recovery_count++;
337 	else
338 		wil->recovery_count = 1; /* fw was alive for a long time */
339 
340 	if (wil->recovery_count > WIL6210_FW_RECOVERY_RETRIES) {
341 		wil_err(wil, "too many recovery attempts (%d), giving up\n",
342 			wil->recovery_count);
343 		return;
344 	}
345 
346 	wil->last_fw_recovery = jiffies;
347 
348 	mutex_lock(&wil->mutex);
349 	switch (wdev->iftype) {
350 	case NL80211_IFTYPE_STATION:
351 	case NL80211_IFTYPE_P2P_CLIENT:
352 	case NL80211_IFTYPE_MONITOR:
353 		wil_info(wil, "fw error recovery requested (try %d)...\n",
354 			 wil->recovery_count);
355 		if (!no_fw_recovery)
356 			wil->recovery_state = fw_recovery_running;
357 		if (0 != wil_wait_for_recovery(wil))
358 			break;
359 
360 		__wil_down(wil);
361 		__wil_up(wil);
362 		break;
363 	case NL80211_IFTYPE_AP:
364 	case NL80211_IFTYPE_P2P_GO:
365 		wil_info(wil, "No recovery for AP-like interface\n");
366 		/* recovery in these modes is done by upper layers */
367 		break;
368 	default:
369 		wil_err(wil, "No recovery - unknown interface type %d\n",
370 			wdev->iftype);
371 		break;
372 	}
373 	mutex_unlock(&wil->mutex);
374 }
375 
376 static int wil_find_free_vring(struct wil6210_priv *wil)
377 {
378 	int i;
379 
380 	for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
381 		if (!wil->vring_tx[i].va)
382 			return i;
383 	}
384 	return -EINVAL;
385 }
386 
387 int wil_tx_init(struct wil6210_priv *wil, int cid)
388 {
389 	int rc = -EINVAL, ringid;
390 
391 	if (cid < 0) {
392 		wil_err(wil, "No connection pending\n");
393 		goto out;
394 	}
395 	ringid = wil_find_free_vring(wil);
396 	if (ringid < 0) {
397 		wil_err(wil, "No free vring found\n");
398 		goto out;
399 	}
400 
401 	wil_dbg_wmi(wil, "Configure for connection CID %d vring %d\n",
402 		    cid, ringid);
403 
404 	rc = wil_vring_init_tx(wil, ringid, 1 << tx_ring_order, cid, 0);
405 	if (rc)
406 		wil_err(wil, "wil_vring_init_tx for CID %d vring %d failed\n",
407 			cid, ringid);
408 
409 out:
410 	return rc;
411 }
412 
413 int wil_bcast_init(struct wil6210_priv *wil)
414 {
415 	int ri = wil->bcast_vring, rc;
416 
417 	if ((ri >= 0) && wil->vring_tx[ri].va)
418 		return 0;
419 
420 	ri = wil_find_free_vring(wil);
421 	if (ri < 0)
422 		return ri;
423 
424 	wil->bcast_vring = ri;
425 	rc = wil_vring_init_bcast(wil, ri, 1 << bcast_ring_order);
426 	if (rc)
427 		wil->bcast_vring = -1;
428 
429 	return rc;
430 }
431 
432 void wil_bcast_fini(struct wil6210_priv *wil)
433 {
434 	int ri = wil->bcast_vring;
435 
436 	if (ri < 0)
437 		return;
438 
439 	wil->bcast_vring = -1;
440 	wil_vring_fini_tx(wil, ri);
441 }
442 
443 int wil_priv_init(struct wil6210_priv *wil)
444 {
445 	uint i;
446 
447 	wil_dbg_misc(wil, "%s()\n", __func__);
448 
449 	memset(wil->sta, 0, sizeof(wil->sta));
450 	for (i = 0; i < WIL6210_MAX_CID; i++)
451 		spin_lock_init(&wil->sta[i].tid_rx_lock);
452 
453 	for (i = 0; i < WIL6210_MAX_TX_RINGS; i++)
454 		spin_lock_init(&wil->vring_tx_data[i].lock);
455 
456 	mutex_init(&wil->mutex);
457 	mutex_init(&wil->wmi_mutex);
458 	mutex_init(&wil->probe_client_mutex);
459 	mutex_init(&wil->p2p_wdev_mutex);
460 
461 	init_completion(&wil->wmi_ready);
462 	init_completion(&wil->wmi_call);
463 
464 	wil->bcast_vring = -1;
465 	setup_timer(&wil->connect_timer, wil_connect_timer_fn, (ulong)wil);
466 	setup_timer(&wil->scan_timer, wil_scan_timer_fn, (ulong)wil);
467 	setup_timer(&wil->p2p.discovery_timer, wil_p2p_discovery_timer_fn,
468 		    (ulong)wil);
469 
470 	INIT_WORK(&wil->disconnect_worker, wil_disconnect_worker);
471 	INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
472 	INIT_WORK(&wil->fw_error_worker, wil_fw_error_worker);
473 	INIT_WORK(&wil->probe_client_worker, wil_probe_client_worker);
474 
475 	INIT_LIST_HEAD(&wil->pending_wmi_ev);
476 	INIT_LIST_HEAD(&wil->probe_client_pending);
477 	spin_lock_init(&wil->wmi_ev_lock);
478 	init_waitqueue_head(&wil->wq);
479 
480 	wil->wmi_wq = create_singlethread_workqueue(WIL_NAME "_wmi");
481 	if (!wil->wmi_wq)
482 		return -EAGAIN;
483 
484 	wil->wq_service = create_singlethread_workqueue(WIL_NAME "_service");
485 	if (!wil->wq_service)
486 		goto out_wmi_wq;
487 
488 	wil->last_fw_recovery = jiffies;
489 	wil->tx_interframe_timeout = WIL6210_ITR_TX_INTERFRAME_TIMEOUT_DEFAULT;
490 	wil->rx_interframe_timeout = WIL6210_ITR_RX_INTERFRAME_TIMEOUT_DEFAULT;
491 	wil->tx_max_burst_duration = WIL6210_ITR_TX_MAX_BURST_DURATION_DEFAULT;
492 	wil->rx_max_burst_duration = WIL6210_ITR_RX_MAX_BURST_DURATION_DEFAULT;
493 
494 	if (rx_ring_overflow_thrsh == WIL6210_RX_HIGH_TRSH_INIT)
495 		rx_ring_overflow_thrsh = WIL6210_RX_HIGH_TRSH_DEFAULT;
496 	return 0;
497 
498 out_wmi_wq:
499 	destroy_workqueue(wil->wmi_wq);
500 
501 	return -EAGAIN;
502 }
503 
504 /**
505  * wil6210_disconnect - disconnect one connection
506  * @wil: driver context
507  * @bssid: peer to disconnect, NULL to disconnect all
508  * @reason_code: Reason code for the Disassociation frame
509  * @from_event: whether is invoked from FW event handler
510  *
511  * Disconnect and release associated resources. If invoked not from the
512  * FW event handler, issue WMI command(s) to trigger MAC disconnect.
513  */
514 void wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid,
515 			u16 reason_code, bool from_event)
516 {
517 	wil_dbg_misc(wil, "%s()\n", __func__);
518 
519 	del_timer_sync(&wil->connect_timer);
520 	_wil6210_disconnect(wil, bssid, reason_code, from_event);
521 }
522 
523 void wil_priv_deinit(struct wil6210_priv *wil)
524 {
525 	wil_dbg_misc(wil, "%s()\n", __func__);
526 
527 	wil_set_recovery_state(wil, fw_recovery_idle);
528 	del_timer_sync(&wil->scan_timer);
529 	del_timer_sync(&wil->p2p.discovery_timer);
530 	cancel_work_sync(&wil->disconnect_worker);
531 	cancel_work_sync(&wil->fw_error_worker);
532 	cancel_work_sync(&wil->p2p.discovery_expired_work);
533 	mutex_lock(&wil->mutex);
534 	wil6210_disconnect(wil, NULL, WLAN_REASON_DEAUTH_LEAVING, false);
535 	mutex_unlock(&wil->mutex);
536 	wmi_event_flush(wil);
537 	wil_probe_client_flush(wil);
538 	cancel_work_sync(&wil->probe_client_worker);
539 	destroy_workqueue(wil->wq_service);
540 	destroy_workqueue(wil->wmi_wq);
541 }
542 
543 static inline void wil_halt_cpu(struct wil6210_priv *wil)
544 {
545 	wil_w(wil, RGF_USER_USER_CPU_0, BIT_USER_USER_CPU_MAN_RST);
546 	wil_w(wil, RGF_USER_MAC_CPU_0,  BIT_USER_MAC_CPU_MAN_RST);
547 }
548 
549 static inline void wil_release_cpu(struct wil6210_priv *wil)
550 {
551 	/* Start CPU */
552 	wil_w(wil, RGF_USER_USER_CPU_0, 1);
553 }
554 
555 static void wil_set_oob_mode(struct wil6210_priv *wil, bool enable)
556 {
557 	wil_info(wil, "%s: enable=%d\n", __func__, enable);
558 	if (enable) {
559 		wil_s(wil, RGF_USER_USAGE_6, BIT_USER_OOB_MODE);
560 	} else {
561 		wil_c(wil, RGF_USER_USAGE_6, BIT_USER_OOB_MODE);
562 	}
563 }
564 
565 static int wil_target_reset(struct wil6210_priv *wil)
566 {
567 	int delay = 0;
568 	u32 x, x1 = 0;
569 
570 	wil_dbg_misc(wil, "Resetting \"%s\"...\n", wil->hw_name);
571 
572 	/* Clear MAC link up */
573 	wil_s(wil, RGF_HP_CTRL, BIT(15));
574 	wil_s(wil, RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_HPAL_PERST_FROM_PAD);
575 	wil_s(wil, RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_CAR_PERST_RST);
576 
577 	wil_halt_cpu(wil);
578 
579 	/* clear all boot loader "ready" bits */
580 	wil_w(wil, RGF_USER_BL +
581 	      offsetof(struct bl_dedicated_registers_v0, boot_loader_ready), 0);
582 	/* Clear Fw Download notification */
583 	wil_c(wil, RGF_USER_USAGE_6, BIT(0));
584 
585 	wil_s(wil, RGF_CAF_OSC_CONTROL, BIT_CAF_OSC_XTAL_EN);
586 	/* XTAL stabilization should take about 3ms */
587 	usleep_range(5000, 7000);
588 	x = wil_r(wil, RGF_CAF_PLL_LOCK_STATUS);
589 	if (!(x & BIT_CAF_OSC_DIG_XTAL_STABLE)) {
590 		wil_err(wil, "Xtal stabilization timeout\n"
591 			"RGF_CAF_PLL_LOCK_STATUS = 0x%08x\n", x);
592 		return -ETIME;
593 	}
594 	/* switch 10k to XTAL*/
595 	wil_c(wil, RGF_USER_SPARROW_M_4, BIT_SPARROW_M_4_SEL_SLEEP_OR_REF);
596 	/* 40 MHz */
597 	wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_CAR_AHB_SW_SEL);
598 
599 	wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x3ff81f);
600 	wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0xf);
601 
602 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
603 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
604 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x000000f0);
605 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FE00);
606 
607 	wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x0);
608 	wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0x0);
609 
610 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
611 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
612 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
613 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
614 
615 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000003);
616 	/* reset A2 PCIE AHB */
617 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000);
618 
619 	wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
620 
621 	/* wait until device ready. typical time is 20..80 msec */
622 	do {
623 		msleep(RST_DELAY);
624 		x = wil_r(wil, RGF_USER_BL +
625 			  offsetof(struct bl_dedicated_registers_v0,
626 				   boot_loader_ready));
627 		if (x1 != x) {
628 			wil_dbg_misc(wil, "BL.ready 0x%08x => 0x%08x\n", x1, x);
629 			x1 = x;
630 		}
631 		if (delay++ > RST_COUNT) {
632 			wil_err(wil, "Reset not completed, bl.ready 0x%08x\n",
633 				x);
634 			return -ETIME;
635 		}
636 	} while (x != BL_READY);
637 
638 	wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
639 
640 	/* enable fix for HW bug related to the SA/DA swap in AP Rx */
641 	wil_s(wil, RGF_DMA_OFUL_NID_0, BIT_DMA_OFUL_NID_0_RX_EXT_TR_EN |
642 	      BIT_DMA_OFUL_NID_0_RX_EXT_A3_SRC);
643 
644 	wil_dbg_misc(wil, "Reset completed in %d ms\n", delay * RST_DELAY);
645 	return 0;
646 }
647 
648 void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
649 {
650 	le32_to_cpus(&r->base);
651 	le16_to_cpus(&r->entry_size);
652 	le16_to_cpus(&r->size);
653 	le32_to_cpus(&r->tail);
654 	le32_to_cpus(&r->head);
655 }
656 
657 static int wil_get_bl_info(struct wil6210_priv *wil)
658 {
659 	struct net_device *ndev = wil_to_ndev(wil);
660 	struct wiphy *wiphy = wil_to_wiphy(wil);
661 	union {
662 		struct bl_dedicated_registers_v0 bl0;
663 		struct bl_dedicated_registers_v1 bl1;
664 	} bl;
665 	u32 bl_ver;
666 	u8 *mac;
667 	u16 rf_status;
668 
669 	wil_memcpy_fromio_32(&bl, wil->csr + HOSTADDR(RGF_USER_BL),
670 			     sizeof(bl));
671 	bl_ver = le32_to_cpu(bl.bl0.boot_loader_struct_version);
672 	mac = bl.bl0.mac_address;
673 
674 	if (bl_ver == 0) {
675 		le32_to_cpus(&bl.bl0.rf_type);
676 		le32_to_cpus(&bl.bl0.baseband_type);
677 		rf_status = 0; /* actually, unknown */
678 		wil_info(wil,
679 			 "Boot Loader struct v%d: MAC = %pM RF = 0x%08x bband = 0x%08x\n",
680 			 bl_ver, mac,
681 			 bl.bl0.rf_type, bl.bl0.baseband_type);
682 		wil_info(wil, "Boot Loader build unknown for struct v0\n");
683 	} else {
684 		le16_to_cpus(&bl.bl1.rf_type);
685 		rf_status = le16_to_cpu(bl.bl1.rf_status);
686 		le32_to_cpus(&bl.bl1.baseband_type);
687 		le16_to_cpus(&bl.bl1.bl_version_subminor);
688 		le16_to_cpus(&bl.bl1.bl_version_build);
689 		wil_info(wil,
690 			 "Boot Loader struct v%d: MAC = %pM RF = 0x%04x (status 0x%04x) bband = 0x%08x\n",
691 			 bl_ver, mac,
692 			 bl.bl1.rf_type, rf_status,
693 			 bl.bl1.baseband_type);
694 		wil_info(wil, "Boot Loader build %d.%d.%d.%d\n",
695 			 bl.bl1.bl_version_major, bl.bl1.bl_version_minor,
696 			 bl.bl1.bl_version_subminor, bl.bl1.bl_version_build);
697 	}
698 
699 	if (!is_valid_ether_addr(mac)) {
700 		wil_err(wil, "BL: Invalid MAC %pM\n", mac);
701 		return -EINVAL;
702 	}
703 
704 	ether_addr_copy(ndev->perm_addr, mac);
705 	ether_addr_copy(wiphy->perm_addr, mac);
706 	if (!is_valid_ether_addr(ndev->dev_addr))
707 		ether_addr_copy(ndev->dev_addr, mac);
708 
709 	if (rf_status) {/* bad RF cable? */
710 		wil_err(wil, "RF communication error 0x%04x",
711 			rf_status);
712 		return -EAGAIN;
713 	}
714 
715 	return 0;
716 }
717 
718 static void wil_bl_crash_info(struct wil6210_priv *wil, bool is_err)
719 {
720 	u32 bl_assert_code, bl_assert_blink, bl_magic_number;
721 	u32 bl_ver = wil_r(wil, RGF_USER_BL +
722 			   offsetof(struct bl_dedicated_registers_v0,
723 				    boot_loader_struct_version));
724 
725 	if (bl_ver < 2)
726 		return;
727 
728 	bl_assert_code = wil_r(wil, RGF_USER_BL +
729 			       offsetof(struct bl_dedicated_registers_v1,
730 					bl_assert_code));
731 	bl_assert_blink = wil_r(wil, RGF_USER_BL +
732 				offsetof(struct bl_dedicated_registers_v1,
733 					 bl_assert_blink));
734 	bl_magic_number = wil_r(wil, RGF_USER_BL +
735 				offsetof(struct bl_dedicated_registers_v1,
736 					 bl_magic_number));
737 
738 	if (is_err) {
739 		wil_err(wil,
740 			"BL assert code 0x%08x blink 0x%08x magic 0x%08x\n",
741 			bl_assert_code, bl_assert_blink, bl_magic_number);
742 	} else {
743 		wil_dbg_misc(wil,
744 			     "BL assert code 0x%08x blink 0x%08x magic 0x%08x\n",
745 			     bl_assert_code, bl_assert_blink, bl_magic_number);
746 	}
747 }
748 
749 static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
750 {
751 	ulong to = msecs_to_jiffies(1000);
752 	ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
753 
754 	if (0 == left) {
755 		wil_err(wil, "Firmware not ready\n");
756 		return -ETIME;
757 	} else {
758 		wil_info(wil, "FW ready after %d ms. HW version 0x%08x\n",
759 			 jiffies_to_msecs(to-left), wil->hw_version);
760 	}
761 	return 0;
762 }
763 
764 /*
765  * We reset all the structures, and we reset the UMAC.
766  * After calling this routine, you're expected to reload
767  * the firmware.
768  */
769 int wil_reset(struct wil6210_priv *wil, bool load_fw)
770 {
771 	int rc;
772 
773 	wil_dbg_misc(wil, "%s()\n", __func__);
774 
775 	WARN_ON(!mutex_is_locked(&wil->mutex));
776 	WARN_ON(test_bit(wil_status_napi_en, wil->status));
777 
778 	if (debug_fw) {
779 		static const u8 mac[ETH_ALEN] = {
780 			0x00, 0xde, 0xad, 0x12, 0x34, 0x56,
781 		};
782 		struct net_device *ndev = wil_to_ndev(wil);
783 
784 		ether_addr_copy(ndev->perm_addr, mac);
785 		ether_addr_copy(ndev->dev_addr, ndev->perm_addr);
786 		return 0;
787 	}
788 
789 	if (wil->hw_version == HW_VER_UNKNOWN)
790 		return -ENODEV;
791 
792 	if (wil->platform_ops.notify) {
793 		rc = wil->platform_ops.notify(wil->platform_handle,
794 					      WIL_PLATFORM_EVT_PRE_RESET);
795 		if (rc)
796 			wil_err(wil,
797 				"%s: PRE_RESET platform notify failed, rc %d\n",
798 				__func__, rc);
799 	}
800 
801 	set_bit(wil_status_resetting, wil->status);
802 
803 	cancel_work_sync(&wil->disconnect_worker);
804 	wil6210_disconnect(wil, NULL, WLAN_REASON_DEAUTH_LEAVING, false);
805 	wil_bcast_fini(wil);
806 
807 	/* prevent NAPI from being scheduled and prevent wmi commands */
808 	mutex_lock(&wil->wmi_mutex);
809 	bitmap_zero(wil->status, wil_status_last);
810 	mutex_unlock(&wil->wmi_mutex);
811 
812 	if (wil->scan_request) {
813 		wil_dbg_misc(wil, "Abort scan_request 0x%p\n",
814 			     wil->scan_request);
815 		del_timer_sync(&wil->scan_timer);
816 		cfg80211_scan_done(wil->scan_request, true);
817 		wil->scan_request = NULL;
818 	}
819 
820 	wil_mask_irq(wil);
821 
822 	wmi_event_flush(wil);
823 
824 	flush_workqueue(wil->wq_service);
825 	flush_workqueue(wil->wmi_wq);
826 
827 	wil_bl_crash_info(wil, false);
828 	rc = wil_target_reset(wil);
829 	wil_rx_fini(wil);
830 	if (rc) {
831 		wil_bl_crash_info(wil, true);
832 		return rc;
833 	}
834 
835 	rc = wil_get_bl_info(wil);
836 	if (rc == -EAGAIN && !load_fw) /* ignore RF error if not going up */
837 		rc = 0;
838 	if (rc)
839 		return rc;
840 
841 	wil_set_oob_mode(wil, oob_mode);
842 	if (load_fw) {
843 		wil_info(wil, "Use firmware <%s> + board <%s>\n", WIL_FW_NAME,
844 			 WIL_FW2_NAME);
845 
846 		wil_halt_cpu(wil);
847 		/* Loading f/w from the file */
848 		rc = wil_request_firmware(wil, WIL_FW_NAME);
849 		if (rc)
850 			return rc;
851 		rc = wil_request_firmware(wil, WIL_FW2_NAME);
852 		if (rc)
853 			return rc;
854 
855 		/* Mark FW as loaded from host */
856 		wil_s(wil, RGF_USER_USAGE_6, 1);
857 
858 		/* clear any interrupts which on-card-firmware
859 		 * may have set
860 		 */
861 		wil6210_clear_irq(wil);
862 		/* CAF_ICR - clear and mask */
863 		/* it is W1C, clear by writing back same value */
864 		wil_s(wil, RGF_CAF_ICR + offsetof(struct RGF_ICR, ICR), 0);
865 		wil_w(wil, RGF_CAF_ICR + offsetof(struct RGF_ICR, IMV), ~0);
866 
867 		wil_release_cpu(wil);
868 	}
869 
870 	/* init after reset */
871 	wil->ap_isolate = 0;
872 	reinit_completion(&wil->wmi_ready);
873 	reinit_completion(&wil->wmi_call);
874 
875 	if (load_fw) {
876 		wil_configure_interrupt_moderation(wil);
877 		wil_unmask_irq(wil);
878 
879 		/* we just started MAC, wait for FW ready */
880 		rc = wil_wait_for_fw_ready(wil);
881 		if (rc)
882 			return rc;
883 
884 		/* check FW is responsive */
885 		rc = wmi_echo(wil);
886 		if (rc) {
887 			wil_err(wil, "%s: wmi_echo failed, rc %d\n",
888 				__func__, rc);
889 			return rc;
890 		}
891 
892 		if (wil->platform_ops.notify) {
893 			rc = wil->platform_ops.notify(wil->platform_handle,
894 						      WIL_PLATFORM_EVT_FW_RDY);
895 			if (rc) {
896 				wil_err(wil,
897 					"%s: FW_RDY notify failed, rc %d\n",
898 					__func__, rc);
899 				rc = 0;
900 			}
901 		}
902 	}
903 
904 	return rc;
905 }
906 
907 void wil_fw_error_recovery(struct wil6210_priv *wil)
908 {
909 	wil_dbg_misc(wil, "starting fw error recovery\n");
910 
911 	if (test_bit(wil_status_resetting, wil->status)) {
912 		wil_info(wil, "Reset already in progress\n");
913 		return;
914 	}
915 
916 	wil->recovery_state = fw_recovery_pending;
917 	schedule_work(&wil->fw_error_worker);
918 }
919 
920 int __wil_up(struct wil6210_priv *wil)
921 {
922 	struct net_device *ndev = wil_to_ndev(wil);
923 	struct wireless_dev *wdev = wil->wdev;
924 	int rc;
925 
926 	WARN_ON(!mutex_is_locked(&wil->mutex));
927 
928 	rc = wil_reset(wil, true);
929 	if (rc)
930 		return rc;
931 
932 	/* Rx VRING. After MAC and beacon */
933 	rc = wil_rx_init(wil, 1 << rx_ring_order);
934 	if (rc)
935 		return rc;
936 
937 	switch (wdev->iftype) {
938 	case NL80211_IFTYPE_STATION:
939 		wil_dbg_misc(wil, "type: STATION\n");
940 		ndev->type = ARPHRD_ETHER;
941 		break;
942 	case NL80211_IFTYPE_AP:
943 		wil_dbg_misc(wil, "type: AP\n");
944 		ndev->type = ARPHRD_ETHER;
945 		break;
946 	case NL80211_IFTYPE_P2P_CLIENT:
947 		wil_dbg_misc(wil, "type: P2P_CLIENT\n");
948 		ndev->type = ARPHRD_ETHER;
949 		break;
950 	case NL80211_IFTYPE_P2P_GO:
951 		wil_dbg_misc(wil, "type: P2P_GO\n");
952 		ndev->type = ARPHRD_ETHER;
953 		break;
954 	case NL80211_IFTYPE_MONITOR:
955 		wil_dbg_misc(wil, "type: Monitor\n");
956 		ndev->type = ARPHRD_IEEE80211_RADIOTAP;
957 		/* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
958 		break;
959 	default:
960 		return -EOPNOTSUPP;
961 	}
962 
963 	/* MAC address - pre-requisite for other commands */
964 	wmi_set_mac_address(wil, ndev->dev_addr);
965 
966 	wil_dbg_misc(wil, "NAPI enable\n");
967 	napi_enable(&wil->napi_rx);
968 	napi_enable(&wil->napi_tx);
969 	set_bit(wil_status_napi_en, wil->status);
970 
971 	if (wil->platform_ops.bus_request)
972 		wil->platform_ops.bus_request(wil->platform_handle,
973 					      WIL_MAX_BUS_REQUEST_KBPS);
974 
975 	return 0;
976 }
977 
978 int wil_up(struct wil6210_priv *wil)
979 {
980 	int rc;
981 
982 	wil_dbg_misc(wil, "%s()\n", __func__);
983 
984 	mutex_lock(&wil->mutex);
985 	rc = __wil_up(wil);
986 	mutex_unlock(&wil->mutex);
987 
988 	return rc;
989 }
990 
991 int __wil_down(struct wil6210_priv *wil)
992 {
993 	int rc;
994 
995 	WARN_ON(!mutex_is_locked(&wil->mutex));
996 
997 	if (wil->platform_ops.bus_request)
998 		wil->platform_ops.bus_request(wil->platform_handle, 0);
999 
1000 	wil_disable_irq(wil);
1001 	if (test_and_clear_bit(wil_status_napi_en, wil->status)) {
1002 		napi_disable(&wil->napi_rx);
1003 		napi_disable(&wil->napi_tx);
1004 		wil_dbg_misc(wil, "NAPI disable\n");
1005 	}
1006 	wil_enable_irq(wil);
1007 
1008 	(void)wil_p2p_stop_discovery(wil);
1009 
1010 	if (wil->scan_request) {
1011 		wil_dbg_misc(wil, "Abort scan_request 0x%p\n",
1012 			     wil->scan_request);
1013 		del_timer_sync(&wil->scan_timer);
1014 		cfg80211_scan_done(wil->scan_request, true);
1015 		wil->scan_request = NULL;
1016 	}
1017 
1018 	if (test_bit(wil_status_fwconnected, wil->status) ||
1019 	    test_bit(wil_status_fwconnecting, wil->status)) {
1020 
1021 		mutex_unlock(&wil->mutex);
1022 		rc = wmi_call(wil, WMI_DISCONNECT_CMDID, NULL, 0,
1023 			      WMI_DISCONNECT_EVENTID, NULL, 0,
1024 			      WIL6210_DISCONNECT_TO_MS);
1025 		mutex_lock(&wil->mutex);
1026 		if (rc)
1027 			wil_err(wil, "timeout waiting for disconnect\n");
1028 	}
1029 
1030 	wil_reset(wil, false);
1031 
1032 	return 0;
1033 }
1034 
1035 int wil_down(struct wil6210_priv *wil)
1036 {
1037 	int rc;
1038 
1039 	wil_dbg_misc(wil, "%s()\n", __func__);
1040 
1041 	wil_set_recovery_state(wil, fw_recovery_idle);
1042 	mutex_lock(&wil->mutex);
1043 	rc = __wil_down(wil);
1044 	mutex_unlock(&wil->mutex);
1045 
1046 	return rc;
1047 }
1048 
1049 int wil_find_cid(struct wil6210_priv *wil, const u8 *mac)
1050 {
1051 	int i;
1052 	int rc = -ENOENT;
1053 
1054 	for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
1055 		if ((wil->sta[i].status != wil_sta_unused) &&
1056 		    ether_addr_equal(wil->sta[i].addr, mac)) {
1057 			rc = i;
1058 			break;
1059 		}
1060 	}
1061 
1062 	return rc;
1063 }
1064