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