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