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