• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define RST_DELAY (20) /* msec, for loop in @wil_target_reset */
42 #define RST_COUNT (1 + 1000/RST_DELAY) /* round up to be above 1 sec total */
43 
44 /*
45  * Due to a hardware issue,
46  * one has to read/write to/from NIC in 32-bit chunks;
47  * regular memcpy_fromio and siblings will
48  * not work on 64-bit platform - it uses 64-bit transactions
49  *
50  * Force 32-bit transactions to enable NIC on 64-bit platforms
51  *
52  * To avoid byte swap on big endian host, __raw_{read|write}l
53  * should be used - {read|write}l would swap bytes to provide
54  * little endian on PCI value in host endianness.
55  */
wil_memcpy_fromio_32(void * dst,const volatile void __iomem * src,size_t count)56 void wil_memcpy_fromio_32(void *dst, const volatile void __iomem *src,
57 			  size_t count)
58 {
59 	u32 *d = dst;
60 	const volatile u32 __iomem *s = src;
61 
62 	/* size_t is unsigned, if (count%4 != 0) it will wrap */
63 	for (count += 4; count > 4; count -= 4)
64 		*d++ = __raw_readl(s++);
65 }
66 
wil_memcpy_toio_32(volatile void __iomem * dst,const void * src,size_t count)67 void wil_memcpy_toio_32(volatile void __iomem *dst, const void *src,
68 			size_t count)
69 {
70 	volatile u32 __iomem *d = dst;
71 	const u32 *s = src;
72 
73 	for (count += 4; count > 4; count -= 4)
74 		__raw_writel(*s++, d++);
75 }
76 
wil_disconnect_cid(struct wil6210_priv * wil,int cid)77 static void wil_disconnect_cid(struct wil6210_priv *wil, int cid)
78 {
79 	uint i;
80 	struct net_device *ndev = wil_to_ndev(wil);
81 	struct wireless_dev *wdev = wil->wdev;
82 	struct wil_sta_info *sta = &wil->sta[cid];
83 
84 	wil_dbg_misc(wil, "%s(CID %d, status %d)\n", __func__, cid,
85 		     sta->status);
86 
87 	sta->data_port_open = false;
88 	if (sta->status != wil_sta_unused) {
89 		wmi_disconnect_sta(wil, sta->addr, WLAN_REASON_DEAUTH_LEAVING);
90 		switch (wdev->iftype) {
91 		case NL80211_IFTYPE_AP:
92 		case NL80211_IFTYPE_P2P_GO:
93 			/* AP-like interface */
94 			cfg80211_del_sta(ndev, sta->addr, GFP_KERNEL);
95 			break;
96 		default:
97 			break;
98 		}
99 		sta->status = wil_sta_unused;
100 	}
101 
102 	for (i = 0; i < WIL_STA_TID_NUM; i++) {
103 		struct wil_tid_ampdu_rx *r;
104 		unsigned long flags;
105 
106 		spin_lock_irqsave(&sta->tid_rx_lock, flags);
107 
108 		r = sta->tid_rx[i];
109 		sta->tid_rx[i] = NULL;
110 		wil_tid_ampdu_rx_free(wil, r);
111 
112 		spin_unlock_irqrestore(&sta->tid_rx_lock, flags);
113 	}
114 	for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++) {
115 		if (wil->vring2cid_tid[i][0] == cid)
116 			wil_vring_fini_tx(wil, i);
117 	}
118 	memset(&sta->stats, 0, sizeof(sta->stats));
119 }
120 
_wil6210_disconnect(struct wil6210_priv * wil,const u8 * bssid)121 static void _wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid)
122 {
123 	int cid = -ENOENT;
124 	struct net_device *ndev = wil_to_ndev(wil);
125 	struct wireless_dev *wdev = wil->wdev;
126 
127 	might_sleep();
128 	if (bssid) {
129 		cid = wil_find_cid(wil, bssid);
130 		wil_dbg_misc(wil, "%s(%pM, CID %d)\n", __func__, bssid, cid);
131 	} else {
132 		wil_dbg_misc(wil, "%s(all)\n", __func__);
133 	}
134 
135 	if (cid >= 0) /* disconnect 1 peer */
136 		wil_disconnect_cid(wil, cid);
137 	else /* disconnect all */
138 		for (cid = 0; cid < WIL6210_MAX_CID; cid++)
139 			wil_disconnect_cid(wil, cid);
140 
141 	/* link state */
142 	switch (wdev->iftype) {
143 	case NL80211_IFTYPE_STATION:
144 	case NL80211_IFTYPE_P2P_CLIENT:
145 		wil_link_off(wil);
146 		if (test_bit(wil_status_fwconnected, &wil->status)) {
147 			clear_bit(wil_status_fwconnected, &wil->status);
148 			cfg80211_disconnected(ndev,
149 					      WLAN_STATUS_UNSPECIFIED_FAILURE,
150 					      NULL, 0, GFP_KERNEL);
151 		} else if (test_bit(wil_status_fwconnecting, &wil->status)) {
152 			cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
153 						WLAN_STATUS_UNSPECIFIED_FAILURE,
154 						GFP_KERNEL);
155 		}
156 		clear_bit(wil_status_fwconnecting, &wil->status);
157 		break;
158 	default:
159 		break;
160 	}
161 }
162 
wil_disconnect_worker(struct work_struct * work)163 static void wil_disconnect_worker(struct work_struct *work)
164 {
165 	struct wil6210_priv *wil = container_of(work,
166 			struct wil6210_priv, disconnect_worker);
167 
168 	mutex_lock(&wil->mutex);
169 	_wil6210_disconnect(wil, NULL);
170 	mutex_unlock(&wil->mutex);
171 }
172 
wil_connect_timer_fn(ulong x)173 static void wil_connect_timer_fn(ulong x)
174 {
175 	struct wil6210_priv *wil = (void *)x;
176 
177 	wil_dbg_misc(wil, "Connect timeout\n");
178 
179 	/* reschedule to thread context - disconnect won't
180 	 * run from atomic context
181 	 */
182 	schedule_work(&wil->disconnect_worker);
183 }
184 
wil_scan_timer_fn(ulong x)185 static void wil_scan_timer_fn(ulong x)
186 {
187 	struct wil6210_priv *wil = (void *)x;
188 
189 	clear_bit(wil_status_fwready, &wil->status);
190 	wil_err(wil, "Scan timeout detected, start fw error recovery\n");
191 	schedule_work(&wil->fw_error_worker);
192 }
193 
wil_wait_for_recovery(struct wil6210_priv * wil)194 static int wil_wait_for_recovery(struct wil6210_priv *wil)
195 {
196 	if (wait_event_interruptible(wil->wq, wil->recovery_state !=
197 				     fw_recovery_pending)) {
198 		wil_err(wil, "Interrupt, canceling recovery\n");
199 		return -ERESTARTSYS;
200 	}
201 	if (wil->recovery_state != fw_recovery_running) {
202 		wil_info(wil, "Recovery cancelled\n");
203 		return -EINTR;
204 	}
205 	wil_info(wil, "Proceed with recovery\n");
206 	return 0;
207 }
208 
wil_set_recovery_state(struct wil6210_priv * wil,int state)209 void wil_set_recovery_state(struct wil6210_priv *wil, int state)
210 {
211 	wil_dbg_misc(wil, "%s(%d -> %d)\n", __func__,
212 		     wil->recovery_state, state);
213 
214 	wil->recovery_state = state;
215 	wake_up_interruptible(&wil->wq);
216 }
217 
wil_fw_error_worker(struct work_struct * work)218 static void wil_fw_error_worker(struct work_struct *work)
219 {
220 	struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
221 						fw_error_worker);
222 	struct wireless_dev *wdev = wil->wdev;
223 
224 	wil_dbg_misc(wil, "fw error worker\n");
225 
226 	/* increment @recovery_count if less then WIL6210_FW_RECOVERY_TO
227 	 * passed since last recovery attempt
228 	 */
229 	if (time_is_after_jiffies(wil->last_fw_recovery +
230 				  WIL6210_FW_RECOVERY_TO))
231 		wil->recovery_count++;
232 	else
233 		wil->recovery_count = 1; /* fw was alive for a long time */
234 
235 	if (wil->recovery_count > WIL6210_FW_RECOVERY_RETRIES) {
236 		wil_err(wil, "too many recovery attempts (%d), giving up\n",
237 			wil->recovery_count);
238 		return;
239 	}
240 
241 	wil->last_fw_recovery = jiffies;
242 
243 	wil_info(wil, "fw error recovery requested (try %d)...\n",
244 		 wil->recovery_count);
245 	if (!no_fw_recovery)
246 		wil->recovery_state = fw_recovery_running;
247 	if (wil_wait_for_recovery(wil) != 0)
248 		return;
249 
250 	mutex_lock(&wil->mutex);
251 	switch (wdev->iftype) {
252 	case NL80211_IFTYPE_STATION:
253 	case NL80211_IFTYPE_P2P_CLIENT:
254 	case NL80211_IFTYPE_MONITOR:
255 		/* silent recovery, upper layers will see disconnect */
256 		__wil_down(wil);
257 		__wil_up(wil);
258 		break;
259 	case NL80211_IFTYPE_AP:
260 	case NL80211_IFTYPE_P2P_GO:
261 		/* recovery in these modes is done by upper layers */
262 		break;
263 	default:
264 		break;
265 	}
266 	mutex_unlock(&wil->mutex);
267 }
268 
wil_find_free_vring(struct wil6210_priv * wil)269 static int wil_find_free_vring(struct wil6210_priv *wil)
270 {
271 	int i;
272 
273 	for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
274 		if (!wil->vring_tx[i].va)
275 			return i;
276 	}
277 	return -EINVAL;
278 }
279 
wil_connect_worker(struct work_struct * work)280 static void wil_connect_worker(struct work_struct *work)
281 {
282 	int rc;
283 	struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
284 						connect_worker);
285 	int cid = wil->pending_connect_cid;
286 	int ringid = wil_find_free_vring(wil);
287 
288 	if (cid < 0) {
289 		wil_err(wil, "No connection pending\n");
290 		return;
291 	}
292 
293 	wil_dbg_wmi(wil, "Configure for connection CID %d\n", cid);
294 
295 	rc = wil_vring_init_tx(wil, ringid, WIL6210_TX_RING_SIZE, cid, 0);
296 	wil->pending_connect_cid = -1;
297 	if (rc == 0) {
298 		wil->sta[cid].status = wil_sta_connected;
299 		wil_link_on(wil);
300 	} else {
301 		wil->sta[cid].status = wil_sta_unused;
302 	}
303 }
304 
wil_priv_init(struct wil6210_priv * wil)305 int wil_priv_init(struct wil6210_priv *wil)
306 {
307 	uint i;
308 
309 	wil_dbg_misc(wil, "%s()\n", __func__);
310 
311 	memset(wil->sta, 0, sizeof(wil->sta));
312 	for (i = 0; i < WIL6210_MAX_CID; i++)
313 		spin_lock_init(&wil->sta[i].tid_rx_lock);
314 
315 	mutex_init(&wil->mutex);
316 	mutex_init(&wil->wmi_mutex);
317 
318 	init_completion(&wil->wmi_ready);
319 	init_completion(&wil->wmi_call);
320 
321 	wil->pending_connect_cid = -1;
322 	setup_timer(&wil->connect_timer, wil_connect_timer_fn, (ulong)wil);
323 	setup_timer(&wil->scan_timer, wil_scan_timer_fn, (ulong)wil);
324 
325 	INIT_WORK(&wil->connect_worker, wil_connect_worker);
326 	INIT_WORK(&wil->disconnect_worker, wil_disconnect_worker);
327 	INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
328 	INIT_WORK(&wil->fw_error_worker, wil_fw_error_worker);
329 
330 	INIT_LIST_HEAD(&wil->pending_wmi_ev);
331 	spin_lock_init(&wil->wmi_ev_lock);
332 	init_waitqueue_head(&wil->wq);
333 
334 	wil->wmi_wq = create_singlethread_workqueue(WIL_NAME"_wmi");
335 	if (!wil->wmi_wq)
336 		return -EAGAIN;
337 
338 	wil->wmi_wq_conn = create_singlethread_workqueue(WIL_NAME"_connect");
339 	if (!wil->wmi_wq_conn) {
340 		destroy_workqueue(wil->wmi_wq);
341 		return -EAGAIN;
342 	}
343 
344 	wil->last_fw_recovery = jiffies;
345 	wil->itr_trsh = itr_trsh;
346 
347 	return 0;
348 }
349 
wil6210_disconnect(struct wil6210_priv * wil,const u8 * bssid)350 void wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid)
351 {
352 	wil_dbg_misc(wil, "%s()\n", __func__);
353 
354 	del_timer_sync(&wil->connect_timer);
355 	_wil6210_disconnect(wil, bssid);
356 }
357 
wil_priv_deinit(struct wil6210_priv * wil)358 void wil_priv_deinit(struct wil6210_priv *wil)
359 {
360 	wil_dbg_misc(wil, "%s()\n", __func__);
361 
362 	wil_set_recovery_state(wil, fw_recovery_idle);
363 	del_timer_sync(&wil->scan_timer);
364 	cancel_work_sync(&wil->disconnect_worker);
365 	cancel_work_sync(&wil->fw_error_worker);
366 	mutex_lock(&wil->mutex);
367 	wil6210_disconnect(wil, NULL);
368 	mutex_unlock(&wil->mutex);
369 	wmi_event_flush(wil);
370 	destroy_workqueue(wil->wmi_wq_conn);
371 	destroy_workqueue(wil->wmi_wq);
372 }
373 
374 /* target operations */
375 /* register read */
376 #define R(a) ioread32(wil->csr + HOSTADDR(a))
377 /* register write. wmb() to make sure it is completed */
378 #define W(a, v) do { iowrite32(v, wil->csr + HOSTADDR(a)); wmb(); } while (0)
379 /* register set = read, OR, write */
380 #define S(a, v) W(a, R(a) | v)
381 /* register clear = read, AND with inverted, write */
382 #define C(a, v) W(a, R(a) & ~v)
383 
wil_halt_cpu(struct wil6210_priv * wil)384 static inline void wil_halt_cpu(struct wil6210_priv *wil)
385 {
386 	W(RGF_USER_USER_CPU_0, BIT_USER_USER_CPU_MAN_RST);
387 	W(RGF_USER_MAC_CPU_0,  BIT_USER_MAC_CPU_MAN_RST);
388 }
389 
wil_release_cpu(struct wil6210_priv * wil)390 static inline void wil_release_cpu(struct wil6210_priv *wil)
391 {
392 	/* Start CPU */
393 	W(RGF_USER_USER_CPU_0, 1);
394 }
395 
wil_target_reset(struct wil6210_priv * wil)396 static int wil_target_reset(struct wil6210_priv *wil)
397 {
398 	int delay = 0;
399 	u32 hw_state;
400 	u32 rev_id;
401 	bool is_sparrow = (wil->board->board == WIL_BOARD_SPARROW);
402 
403 	wil_dbg_misc(wil, "Resetting \"%s\"...\n", wil->board->name);
404 
405 	wil->hw_version = R(RGF_USER_FW_REV_ID);
406 	rev_id = wil->hw_version & 0xff;
407 
408 	/* Clear MAC link up */
409 	S(RGF_HP_CTRL, BIT(15));
410 	S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_HPAL_PERST_FROM_PAD);
411 	S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_CAR_PERST_RST);
412 
413 	wil_halt_cpu(wil);
414 	C(RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_CAR_AHB_SW_SEL); /* 40 MHz */
415 
416 	if (is_sparrow) {
417 		W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x3ff81f);
418 		W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0xf);
419 	}
420 
421 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
422 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
423 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, is_sparrow ? 0x000000f0 : 0x00000170);
424 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FE00);
425 
426 	if (is_sparrow) {
427 		W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x0);
428 		W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0x0);
429 	}
430 
431 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
432 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
433 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
434 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
435 
436 	if (is_sparrow) {
437 		W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000003);
438 		/* reset A2 PCIE AHB */
439 		W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000);
440 	} else {
441 		W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000001);
442 		if (rev_id == 1) {
443 			/* reset A1 BOTH PCIE AHB & PCIE RGF */
444 			W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00000080);
445 		} else {
446 			W(RGF_PCIE_LOS_COUNTER_CTL, BIT(6) | BIT(8));
447 			W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000);
448 		}
449 	}
450 
451 	/* TODO: check order here!!! Erez code is different */
452 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
453 
454 	/* wait until device ready. typical time is 200..250 msec */
455 	do {
456 		msleep(RST_DELAY);
457 		hw_state = R(RGF_USER_HW_MACHINE_STATE);
458 		if (delay++ > RST_COUNT) {
459 			wil_err(wil, "Reset not completed, hw_state 0x%08x\n",
460 				hw_state);
461 			return -ETIME;
462 		}
463 	} while (hw_state != HW_MACHINE_BOOT_DONE);
464 
465 	/* TODO: Erez check rev_id != 1 */
466 	if (!is_sparrow && (rev_id != 1))
467 		W(RGF_PCIE_LOS_COUNTER_CTL, BIT(8));
468 
469 	C(RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
470 
471 	wil_dbg_misc(wil, "Reset completed in %d ms\n", delay * RST_DELAY);
472 	return 0;
473 }
474 
475 /**
476  * wil_set_itr_trsh: - apply interrupt coalescing params
477  */
wil_set_itr_trsh(struct wil6210_priv * wil)478 void wil_set_itr_trsh(struct wil6210_priv *wil)
479 {
480 	/* disable, use usec resolution */
481 	W(RGF_DMA_ITR_CNT_CRL, BIT_DMA_ITR_CNT_CRL_EXT_TICK);
482 
483 	/* disable interrupt moderation for monitor
484 	 * to get better timestamp precision
485 	 */
486 	if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR)
487 		return;
488 
489 	wil_info(wil, "set ITR_TRSH = %d usec\n", wil->itr_trsh);
490 	W(RGF_DMA_ITR_CNT_TRSH, wil->itr_trsh);
491 	W(RGF_DMA_ITR_CNT_CRL, BIT_DMA_ITR_CNT_CRL_EN |
492 	  BIT_DMA_ITR_CNT_CRL_EXT_TICK); /* start it */
493 }
494 
495 #undef R
496 #undef W
497 #undef S
498 #undef C
499 
wil_mbox_ring_le2cpus(struct wil6210_mbox_ring * r)500 void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
501 {
502 	le32_to_cpus(&r->base);
503 	le16_to_cpus(&r->entry_size);
504 	le16_to_cpus(&r->size);
505 	le32_to_cpus(&r->tail);
506 	le32_to_cpus(&r->head);
507 }
508 
wil_wait_for_fw_ready(struct wil6210_priv * wil)509 static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
510 {
511 	ulong to = msecs_to_jiffies(1000);
512 	ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
513 
514 	if (0 == left) {
515 		wil_err(wil, "Firmware not ready\n");
516 		return -ETIME;
517 	} else {
518 		wil_info(wil, "FW ready after %d ms. HW version 0x%08x\n",
519 			 jiffies_to_msecs(to-left), wil->hw_version);
520 	}
521 	return 0;
522 }
523 
524 /*
525  * We reset all the structures, and we reset the UMAC.
526  * After calling this routine, you're expected to reload
527  * the firmware.
528  */
wil_reset(struct wil6210_priv * wil)529 int wil_reset(struct wil6210_priv *wil)
530 {
531 	int rc;
532 
533 	wil_dbg_misc(wil, "%s()\n", __func__);
534 
535 	WARN_ON(!mutex_is_locked(&wil->mutex));
536 	WARN_ON(test_bit(wil_status_napi_en, &wil->status));
537 
538 	cancel_work_sync(&wil->disconnect_worker);
539 	wil6210_disconnect(wil, NULL);
540 
541 	wil->status = 0; /* prevent NAPI from being scheduled */
542 
543 	if (wil->scan_request) {
544 		wil_dbg_misc(wil, "Abort scan_request 0x%p\n",
545 			     wil->scan_request);
546 		del_timer_sync(&wil->scan_timer);
547 		cfg80211_scan_done(wil->scan_request, true);
548 		wil->scan_request = NULL;
549 	}
550 
551 	wil_mask_irq(wil);
552 
553 	wmi_event_flush(wil);
554 
555 	flush_workqueue(wil->wmi_wq_conn);
556 	flush_workqueue(wil->wmi_wq);
557 
558 	rc = wil_target_reset(wil);
559 	wil_rx_fini(wil);
560 	if (rc)
561 		return rc;
562 
563 	if (!no_fw_load) {
564 		wil_info(wil, "Use firmware <%s>\n", WIL_FW_NAME);
565 		wil_halt_cpu(wil);
566 		/* Loading f/w from the file */
567 		rc = wil_request_firmware(wil, WIL_FW_NAME);
568 		if (rc)
569 			return rc;
570 
571 		/* clear any interrupts which on-card-firmware may have set */
572 		wil6210_clear_irq(wil);
573 		{ /* CAF_ICR - clear and mask */
574 			u32 a = HOSTADDR(RGF_CAF_ICR) +
575 				offsetof(struct RGF_ICR, ICR);
576 			u32 m = HOSTADDR(RGF_CAF_ICR) +
577 				offsetof(struct RGF_ICR, IMV);
578 			u32 icr = ioread32(wil->csr + a);
579 
580 			iowrite32(icr, wil->csr + a); /* W1C */
581 			iowrite32(~0, wil->csr + m);
582 			wmb(); /* wait for completion */
583 		}
584 		wil_release_cpu(wil);
585 	} else {
586 		wil_info(wil, "Use firmware from on-card flash\n");
587 	}
588 
589 	/* init after reset */
590 	wil->pending_connect_cid = -1;
591 	reinit_completion(&wil->wmi_ready);
592 	reinit_completion(&wil->wmi_call);
593 
594 	wil_unmask_irq(wil);
595 
596 	/* we just started MAC, wait for FW ready */
597 	rc = wil_wait_for_fw_ready(wil);
598 
599 	return rc;
600 }
601 
wil_fw_error_recovery(struct wil6210_priv * wil)602 void wil_fw_error_recovery(struct wil6210_priv *wil)
603 {
604 	wil_dbg_misc(wil, "starting fw error recovery\n");
605 	wil->recovery_state = fw_recovery_pending;
606 	schedule_work(&wil->fw_error_worker);
607 }
608 
wil_link_on(struct wil6210_priv * wil)609 void wil_link_on(struct wil6210_priv *wil)
610 {
611 	struct net_device *ndev = wil_to_ndev(wil);
612 
613 	wil_dbg_misc(wil, "%s()\n", __func__);
614 
615 	netif_carrier_on(ndev);
616 	wil_dbg_misc(wil, "netif_tx_wake : link on\n");
617 	netif_tx_wake_all_queues(ndev);
618 }
619 
wil_link_off(struct wil6210_priv * wil)620 void wil_link_off(struct wil6210_priv *wil)
621 {
622 	struct net_device *ndev = wil_to_ndev(wil);
623 
624 	wil_dbg_misc(wil, "%s()\n", __func__);
625 
626 	netif_tx_stop_all_queues(ndev);
627 	wil_dbg_misc(wil, "netif_tx_stop : link off\n");
628 	netif_carrier_off(ndev);
629 }
630 
__wil_up(struct wil6210_priv * wil)631 int __wil_up(struct wil6210_priv *wil)
632 {
633 	struct net_device *ndev = wil_to_ndev(wil);
634 	struct wireless_dev *wdev = wil->wdev;
635 	int rc;
636 
637 	WARN_ON(!mutex_is_locked(&wil->mutex));
638 
639 	rc = wil_reset(wil);
640 	if (rc)
641 		return rc;
642 
643 	/* Rx VRING. After MAC and beacon */
644 	rc = wil_rx_init(wil);
645 	if (rc)
646 		return rc;
647 
648 	switch (wdev->iftype) {
649 	case NL80211_IFTYPE_STATION:
650 		wil_dbg_misc(wil, "type: STATION\n");
651 		ndev->type = ARPHRD_ETHER;
652 		break;
653 	case NL80211_IFTYPE_AP:
654 		wil_dbg_misc(wil, "type: AP\n");
655 		ndev->type = ARPHRD_ETHER;
656 		break;
657 	case NL80211_IFTYPE_P2P_CLIENT:
658 		wil_dbg_misc(wil, "type: P2P_CLIENT\n");
659 		ndev->type = ARPHRD_ETHER;
660 		break;
661 	case NL80211_IFTYPE_P2P_GO:
662 		wil_dbg_misc(wil, "type: P2P_GO\n");
663 		ndev->type = ARPHRD_ETHER;
664 		break;
665 	case NL80211_IFTYPE_MONITOR:
666 		wil_dbg_misc(wil, "type: Monitor\n");
667 		ndev->type = ARPHRD_IEEE80211_RADIOTAP;
668 		/* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
669 		break;
670 	default:
671 		return -EOPNOTSUPP;
672 	}
673 
674 	/* MAC address - pre-requisite for other commands */
675 	wmi_set_mac_address(wil, ndev->dev_addr);
676 
677 	wil_dbg_misc(wil, "NAPI enable\n");
678 	napi_enable(&wil->napi_rx);
679 	napi_enable(&wil->napi_tx);
680 	set_bit(wil_status_napi_en, &wil->status);
681 
682 	if (wil->platform_ops.bus_request)
683 		wil->platform_ops.bus_request(wil->platform_handle,
684 					      WIL_MAX_BUS_REQUEST_KBPS);
685 
686 	return 0;
687 }
688 
wil_up(struct wil6210_priv * wil)689 int wil_up(struct wil6210_priv *wil)
690 {
691 	int rc;
692 
693 	wil_dbg_misc(wil, "%s()\n", __func__);
694 
695 	mutex_lock(&wil->mutex);
696 	rc = __wil_up(wil);
697 	mutex_unlock(&wil->mutex);
698 
699 	return rc;
700 }
701 
__wil_down(struct wil6210_priv * wil)702 int __wil_down(struct wil6210_priv *wil)
703 {
704 	int iter = WAIT_FOR_DISCONNECT_TIMEOUT_MS /
705 			WAIT_FOR_DISCONNECT_INTERVAL_MS;
706 
707 	WARN_ON(!mutex_is_locked(&wil->mutex));
708 
709 	if (wil->platform_ops.bus_request)
710 		wil->platform_ops.bus_request(wil->platform_handle, 0);
711 
712 	wil_disable_irq(wil);
713 	if (test_and_clear_bit(wil_status_napi_en, &wil->status)) {
714 		napi_disable(&wil->napi_rx);
715 		napi_disable(&wil->napi_tx);
716 		wil_dbg_misc(wil, "NAPI disable\n");
717 	}
718 	wil_enable_irq(wil);
719 
720 	if (wil->scan_request) {
721 		wil_dbg_misc(wil, "Abort scan_request 0x%p\n",
722 			     wil->scan_request);
723 		del_timer_sync(&wil->scan_timer);
724 		cfg80211_scan_done(wil->scan_request, true);
725 		wil->scan_request = NULL;
726 	}
727 
728 	if (test_bit(wil_status_fwconnected, &wil->status) ||
729 	    test_bit(wil_status_fwconnecting, &wil->status))
730 		wmi_send(wil, WMI_DISCONNECT_CMDID, NULL, 0);
731 
732 	/* make sure wil is idle (not connected) */
733 	mutex_unlock(&wil->mutex);
734 	while (iter--) {
735 		int idle = !test_bit(wil_status_fwconnected, &wil->status) &&
736 			   !test_bit(wil_status_fwconnecting, &wil->status);
737 		if (idle)
738 			break;
739 		msleep(WAIT_FOR_DISCONNECT_INTERVAL_MS);
740 	}
741 	mutex_lock(&wil->mutex);
742 
743 	if (!iter)
744 		wil_err(wil, "timeout waiting for idle FW/HW\n");
745 
746 	wil_rx_fini(wil);
747 
748 	return 0;
749 }
750 
wil_down(struct wil6210_priv * wil)751 int wil_down(struct wil6210_priv *wil)
752 {
753 	int rc;
754 
755 	wil_dbg_misc(wil, "%s()\n", __func__);
756 
757 	wil_set_recovery_state(wil, fw_recovery_idle);
758 	mutex_lock(&wil->mutex);
759 	rc = __wil_down(wil);
760 	mutex_unlock(&wil->mutex);
761 
762 	return rc;
763 }
764 
wil_find_cid(struct wil6210_priv * wil,const u8 * mac)765 int wil_find_cid(struct wil6210_priv *wil, const u8 *mac)
766 {
767 	int i;
768 	int rc = -ENOENT;
769 
770 	for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
771 		if ((wil->sta[i].status != wil_sta_unused) &&
772 		    ether_addr_equal(wil->sta[i].addr, mac)) {
773 			rc = i;
774 			break;
775 		}
776 	}
777 
778 	return rc;
779 }
780