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