1 /*
2  * Copyright (c) 2012 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 
24 static bool no_fw_recovery;
25 module_param(no_fw_recovery, bool, S_IRUGO | S_IWUSR);
26 MODULE_PARM_DESC(no_fw_recovery, " disable FW error recovery");
27 
28 /*
29  * Due to a hardware issue,
30  * one has to read/write to/from NIC in 32-bit chunks;
31  * regular memcpy_fromio and siblings will
32  * not work on 64-bit platform - it uses 64-bit transactions
33  *
34  * Force 32-bit transactions to enable NIC on 64-bit platforms
35  *
36  * To avoid byte swap on big endian host, __raw_{read|write}l
37  * should be used - {read|write}l would swap bytes to provide
38  * little endian on PCI value in host endianness.
39  */
40 void wil_memcpy_fromio_32(void *dst, const volatile void __iomem *src,
41 			  size_t count)
42 {
43 	u32 *d = dst;
44 	const volatile u32 __iomem *s = src;
45 
46 	/* size_t is unsigned, if (count%4 != 0) it will wrap */
47 	for (count += 4; count > 4; count -= 4)
48 		*d++ = __raw_readl(s++);
49 }
50 
51 void wil_memcpy_toio_32(volatile void __iomem *dst, const void *src,
52 			size_t count)
53 {
54 	volatile u32 __iomem *d = dst;
55 	const u32 *s = src;
56 
57 	for (count += 4; count > 4; count -= 4)
58 		__raw_writel(*s++, d++);
59 }
60 
61 static void wil_disconnect_cid(struct wil6210_priv *wil, int cid)
62 {
63 	uint i;
64 	struct net_device *ndev = wil_to_ndev(wil);
65 	struct wireless_dev *wdev = wil->wdev;
66 	struct wil_sta_info *sta = &wil->sta[cid];
67 	wil_dbg_misc(wil, "%s(CID %d, status %d)\n", __func__, cid,
68 		     sta->status);
69 
70 	sta->data_port_open = false;
71 	if (sta->status != wil_sta_unused) {
72 		wmi_disconnect_sta(wil, sta->addr, WLAN_REASON_DEAUTH_LEAVING);
73 		switch (wdev->iftype) {
74 		case NL80211_IFTYPE_AP:
75 		case NL80211_IFTYPE_P2P_GO:
76 			/* AP-like interface */
77 			cfg80211_del_sta(ndev, sta->addr, GFP_KERNEL);
78 			break;
79 		default:
80 			break;
81 		}
82 		sta->status = wil_sta_unused;
83 	}
84 
85 	for (i = 0; i < WIL_STA_TID_NUM; i++) {
86 		struct wil_tid_ampdu_rx *r = sta->tid_rx[i];
87 		sta->tid_rx[i] = NULL;
88 		wil_tid_ampdu_rx_free(wil, r);
89 	}
90 	for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++) {
91 		if (wil->vring2cid_tid[i][0] == cid)
92 			wil_vring_fini_tx(wil, i);
93 	}
94 	memset(&sta->stats, 0, sizeof(sta->stats));
95 }
96 
97 static void _wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid)
98 {
99 	int cid = -ENOENT;
100 	struct net_device *ndev = wil_to_ndev(wil);
101 	struct wireless_dev *wdev = wil->wdev;
102 
103 	might_sleep();
104 	if (bssid) {
105 		cid = wil_find_cid(wil, bssid);
106 		wil_dbg_misc(wil, "%s(%pM, CID %d)\n", __func__, bssid, cid);
107 	} else {
108 		wil_dbg_misc(wil, "%s(all)\n", __func__);
109 	}
110 
111 	if (cid >= 0) /* disconnect 1 peer */
112 		wil_disconnect_cid(wil, cid);
113 	else /* disconnect all */
114 		for (cid = 0; cid < WIL6210_MAX_CID; cid++)
115 			wil_disconnect_cid(wil, cid);
116 
117 	/* link state */
118 	switch (wdev->iftype) {
119 	case NL80211_IFTYPE_STATION:
120 	case NL80211_IFTYPE_P2P_CLIENT:
121 		wil_link_off(wil);
122 		if (test_bit(wil_status_fwconnected, &wil->status)) {
123 			clear_bit(wil_status_fwconnected, &wil->status);
124 			cfg80211_disconnected(ndev,
125 					      WLAN_STATUS_UNSPECIFIED_FAILURE,
126 					      NULL, 0, GFP_KERNEL);
127 		} else if (test_bit(wil_status_fwconnecting, &wil->status)) {
128 			cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
129 						WLAN_STATUS_UNSPECIFIED_FAILURE,
130 						GFP_KERNEL);
131 		}
132 		clear_bit(wil_status_fwconnecting, &wil->status);
133 		break;
134 	default:
135 		break;
136 	}
137 }
138 
139 static void wil_disconnect_worker(struct work_struct *work)
140 {
141 	struct wil6210_priv *wil = container_of(work,
142 			struct wil6210_priv, disconnect_worker);
143 
144 	mutex_lock(&wil->mutex);
145 	_wil6210_disconnect(wil, NULL);
146 	mutex_unlock(&wil->mutex);
147 }
148 
149 static void wil_connect_timer_fn(ulong x)
150 {
151 	struct wil6210_priv *wil = (void *)x;
152 
153 	wil_dbg_misc(wil, "Connect timeout\n");
154 
155 	/* reschedule to thread context - disconnect won't
156 	 * run from atomic context
157 	 */
158 	schedule_work(&wil->disconnect_worker);
159 }
160 
161 static void wil_scan_timer_fn(ulong x)
162 {
163 	struct wil6210_priv *wil = (void *)x;
164 
165 	clear_bit(wil_status_fwready, &wil->status);
166 	wil_err(wil, "Scan timeout detected, start fw error recovery\n");
167 	schedule_work(&wil->fw_error_worker);
168 }
169 
170 static void wil_fw_error_worker(struct work_struct *work)
171 {
172 	struct wil6210_priv *wil = container_of(work,
173 			struct wil6210_priv, fw_error_worker);
174 	struct wireless_dev *wdev = wil->wdev;
175 
176 	wil_dbg_misc(wil, "fw error worker\n");
177 
178 	if (no_fw_recovery)
179 		return;
180 
181 	/* increment @recovery_count if less then WIL6210_FW_RECOVERY_TO
182 	 * passed since last recovery attempt
183 	 */
184 	if (time_is_after_jiffies(wil->last_fw_recovery +
185 				  WIL6210_FW_RECOVERY_TO))
186 		wil->recovery_count++;
187 	else
188 		wil->recovery_count = 1; /* fw was alive for a long time */
189 
190 	if (wil->recovery_count > WIL6210_FW_RECOVERY_RETRIES) {
191 		wil_err(wil, "too many recovery attempts (%d), giving up\n",
192 			wil->recovery_count);
193 		return;
194 	}
195 
196 	wil->last_fw_recovery = jiffies;
197 
198 	mutex_lock(&wil->mutex);
199 	switch (wdev->iftype) {
200 	case NL80211_IFTYPE_STATION:
201 	case NL80211_IFTYPE_P2P_CLIENT:
202 	case NL80211_IFTYPE_MONITOR:
203 		wil_info(wil, "fw error recovery started (try %d)...\n",
204 			 wil->recovery_count);
205 		wil_reset(wil);
206 
207 		/* need to re-allocate Rx ring after reset */
208 		wil_rx_init(wil);
209 		break;
210 	case NL80211_IFTYPE_AP:
211 	case NL80211_IFTYPE_P2P_GO:
212 		/* recovery in these modes is done by upper layers */
213 		break;
214 	default:
215 		break;
216 	}
217 	mutex_unlock(&wil->mutex);
218 }
219 
220 static int wil_find_free_vring(struct wil6210_priv *wil)
221 {
222 	int i;
223 	for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
224 		if (!wil->vring_tx[i].va)
225 			return i;
226 	}
227 	return -EINVAL;
228 }
229 
230 static void wil_connect_worker(struct work_struct *work)
231 {
232 	int rc;
233 	struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
234 						connect_worker);
235 	int cid = wil->pending_connect_cid;
236 	int ringid = wil_find_free_vring(wil);
237 
238 	if (cid < 0) {
239 		wil_err(wil, "No connection pending\n");
240 		return;
241 	}
242 
243 	wil_dbg_wmi(wil, "Configure for connection CID %d\n", cid);
244 
245 	rc = wil_vring_init_tx(wil, ringid, WIL6210_TX_RING_SIZE, cid, 0);
246 	wil->pending_connect_cid = -1;
247 	if (rc == 0) {
248 		wil->sta[cid].status = wil_sta_connected;
249 		wil_link_on(wil);
250 	} else {
251 		wil->sta[cid].status = wil_sta_unused;
252 	}
253 }
254 
255 int wil_priv_init(struct wil6210_priv *wil)
256 {
257 	wil_dbg_misc(wil, "%s()\n", __func__);
258 
259 	memset(wil->sta, 0, sizeof(wil->sta));
260 
261 	mutex_init(&wil->mutex);
262 	mutex_init(&wil->wmi_mutex);
263 
264 	init_completion(&wil->wmi_ready);
265 
266 	wil->pending_connect_cid = -1;
267 	setup_timer(&wil->connect_timer, wil_connect_timer_fn, (ulong)wil);
268 	setup_timer(&wil->scan_timer, wil_scan_timer_fn, (ulong)wil);
269 
270 	INIT_WORK(&wil->connect_worker, wil_connect_worker);
271 	INIT_WORK(&wil->disconnect_worker, wil_disconnect_worker);
272 	INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
273 	INIT_WORK(&wil->fw_error_worker, wil_fw_error_worker);
274 
275 	INIT_LIST_HEAD(&wil->pending_wmi_ev);
276 	spin_lock_init(&wil->wmi_ev_lock);
277 
278 	wil->wmi_wq = create_singlethread_workqueue(WIL_NAME"_wmi");
279 	if (!wil->wmi_wq)
280 		return -EAGAIN;
281 
282 	wil->wmi_wq_conn = create_singlethread_workqueue(WIL_NAME"_connect");
283 	if (!wil->wmi_wq_conn) {
284 		destroy_workqueue(wil->wmi_wq);
285 		return -EAGAIN;
286 	}
287 
288 	wil->last_fw_recovery = jiffies;
289 
290 	return 0;
291 }
292 
293 void wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid)
294 {
295 	del_timer_sync(&wil->connect_timer);
296 	_wil6210_disconnect(wil, bssid);
297 }
298 
299 void wil_priv_deinit(struct wil6210_priv *wil)
300 {
301 	del_timer_sync(&wil->scan_timer);
302 	cancel_work_sync(&wil->disconnect_worker);
303 	cancel_work_sync(&wil->fw_error_worker);
304 	mutex_lock(&wil->mutex);
305 	wil6210_disconnect(wil, NULL);
306 	mutex_unlock(&wil->mutex);
307 	wmi_event_flush(wil);
308 	destroy_workqueue(wil->wmi_wq_conn);
309 	destroy_workqueue(wil->wmi_wq);
310 }
311 
312 static void wil_target_reset(struct wil6210_priv *wil)
313 {
314 	int delay = 0;
315 	u32 hw_state;
316 	u32 rev_id;
317 
318 	wil_dbg_misc(wil, "Resetting...\n");
319 
320 	/* register read */
321 #define R(a) ioread32(wil->csr + HOSTADDR(a))
322 	/* register write */
323 #define W(a, v) iowrite32(v, wil->csr + HOSTADDR(a))
324 	/* register set = read, OR, write */
325 #define S(a, v) W(a, R(a) | v)
326 	/* register clear = read, AND with inverted, write */
327 #define C(a, v) W(a, R(a) & ~v)
328 
329 	wil->hw_version = R(RGF_USER_FW_REV_ID);
330 	rev_id = wil->hw_version & 0xff;
331 	/* hpal_perst_from_pad_src_n_mask */
332 	S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(6));
333 	/* car_perst_rst_src_n_mask */
334 	S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(7));
335 	wmb(); /* order is important here */
336 
337 	W(RGF_USER_MAC_CPU_0,  BIT(1)); /* mac_cpu_man_rst */
338 	W(RGF_USER_USER_CPU_0, BIT(1)); /* user_cpu_man_rst */
339 	wmb(); /* order is important here */
340 
341 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
342 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
343 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000170);
344 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FC00);
345 	wmb(); /* order is important here */
346 
347 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
348 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
349 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
350 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
351 	wmb(); /* order is important here */
352 
353 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000001);
354 	if (rev_id == 1) {
355 		W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00000080);
356 	} else {
357 		W(RGF_PCIE_LOS_COUNTER_CTL, BIT(6) | BIT(8));
358 		W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000);
359 	}
360 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
361 	wmb(); /* order is important here */
362 
363 	/* wait until device ready */
364 	do {
365 		msleep(1);
366 		hw_state = R(RGF_USER_HW_MACHINE_STATE);
367 		if (delay++ > 100) {
368 			wil_err(wil, "Reset not completed, hw_state 0x%08x\n",
369 				hw_state);
370 			return;
371 		}
372 	} while (hw_state != HW_MACHINE_BOOT_DONE);
373 
374 	if (rev_id == 2)
375 		W(RGF_PCIE_LOS_COUNTER_CTL, BIT(8));
376 
377 	C(RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
378 	wmb(); /* order is important here */
379 
380 	wil_dbg_misc(wil, "Reset completed in %d ms\n", delay);
381 
382 #undef R
383 #undef W
384 #undef S
385 #undef C
386 }
387 
388 void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
389 {
390 	le32_to_cpus(&r->base);
391 	le16_to_cpus(&r->entry_size);
392 	le16_to_cpus(&r->size);
393 	le32_to_cpus(&r->tail);
394 	le32_to_cpus(&r->head);
395 }
396 
397 static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
398 {
399 	ulong to = msecs_to_jiffies(1000);
400 	ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
401 	if (0 == left) {
402 		wil_err(wil, "Firmware not ready\n");
403 		return -ETIME;
404 	} else {
405 		wil_info(wil, "FW ready after %d ms. HW version 0x%08x\n",
406 			 jiffies_to_msecs(to-left), wil->hw_version);
407 	}
408 	return 0;
409 }
410 
411 /*
412  * We reset all the structures, and we reset the UMAC.
413  * After calling this routine, you're expected to reload
414  * the firmware.
415  */
416 int wil_reset(struct wil6210_priv *wil)
417 {
418 	int rc;
419 
420 	WARN_ON(!mutex_is_locked(&wil->mutex));
421 
422 	cancel_work_sync(&wil->disconnect_worker);
423 	wil6210_disconnect(wil, NULL);
424 
425 	wil->status = 0; /* prevent NAPI from being scheduled */
426 	if (test_bit(wil_status_napi_en, &wil->status)) {
427 		napi_synchronize(&wil->napi_rx);
428 	}
429 
430 	if (wil->scan_request) {
431 		wil_dbg_misc(wil, "Abort scan_request 0x%p\n",
432 			     wil->scan_request);
433 		del_timer_sync(&wil->scan_timer);
434 		cfg80211_scan_done(wil->scan_request, true);
435 		wil->scan_request = NULL;
436 	}
437 
438 	wil6210_disable_irq(wil);
439 
440 	wmi_event_flush(wil);
441 
442 	flush_workqueue(wil->wmi_wq_conn);
443 	flush_workqueue(wil->wmi_wq);
444 
445 	/* TODO: put MAC in reset */
446 	wil_target_reset(wil);
447 
448 	wil_rx_fini(wil);
449 
450 	/* init after reset */
451 	wil->pending_connect_cid = -1;
452 	reinit_completion(&wil->wmi_ready);
453 
454 	/* TODO: release MAC reset */
455 	wil6210_enable_irq(wil);
456 
457 	/* we just started MAC, wait for FW ready */
458 	rc = wil_wait_for_fw_ready(wil);
459 
460 	return rc;
461 }
462 
463 void wil_fw_error_recovery(struct wil6210_priv *wil)
464 {
465 	wil_dbg_misc(wil, "starting fw error recovery\n");
466 	schedule_work(&wil->fw_error_worker);
467 }
468 
469 void wil_link_on(struct wil6210_priv *wil)
470 {
471 	struct net_device *ndev = wil_to_ndev(wil);
472 
473 	wil_dbg_misc(wil, "%s()\n", __func__);
474 
475 	netif_carrier_on(ndev);
476 	netif_tx_wake_all_queues(ndev);
477 }
478 
479 void wil_link_off(struct wil6210_priv *wil)
480 {
481 	struct net_device *ndev = wil_to_ndev(wil);
482 
483 	wil_dbg_misc(wil, "%s()\n", __func__);
484 
485 	netif_tx_stop_all_queues(ndev);
486 	netif_carrier_off(ndev);
487 }
488 
489 static int __wil_up(struct wil6210_priv *wil)
490 {
491 	struct net_device *ndev = wil_to_ndev(wil);
492 	struct wireless_dev *wdev = wil->wdev;
493 	int rc;
494 
495 	WARN_ON(!mutex_is_locked(&wil->mutex));
496 
497 	rc = wil_reset(wil);
498 	if (rc)
499 		return rc;
500 
501 	/* Rx VRING. After MAC and beacon */
502 	rc = wil_rx_init(wil);
503 	if (rc)
504 		return rc;
505 
506 	switch (wdev->iftype) {
507 	case NL80211_IFTYPE_STATION:
508 		wil_dbg_misc(wil, "type: STATION\n");
509 		ndev->type = ARPHRD_ETHER;
510 		break;
511 	case NL80211_IFTYPE_AP:
512 		wil_dbg_misc(wil, "type: AP\n");
513 		ndev->type = ARPHRD_ETHER;
514 		break;
515 	case NL80211_IFTYPE_P2P_CLIENT:
516 		wil_dbg_misc(wil, "type: P2P_CLIENT\n");
517 		ndev->type = ARPHRD_ETHER;
518 		break;
519 	case NL80211_IFTYPE_P2P_GO:
520 		wil_dbg_misc(wil, "type: P2P_GO\n");
521 		ndev->type = ARPHRD_ETHER;
522 		break;
523 	case NL80211_IFTYPE_MONITOR:
524 		wil_dbg_misc(wil, "type: Monitor\n");
525 		ndev->type = ARPHRD_IEEE80211_RADIOTAP;
526 		/* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
527 		break;
528 	default:
529 		return -EOPNOTSUPP;
530 	}
531 
532 	/* MAC address - pre-requisite for other commands */
533 	wmi_set_mac_address(wil, ndev->dev_addr);
534 
535 
536 	napi_enable(&wil->napi_rx);
537 	napi_enable(&wil->napi_tx);
538 	set_bit(wil_status_napi_en, &wil->status);
539 
540 	return 0;
541 }
542 
543 int wil_up(struct wil6210_priv *wil)
544 {
545 	int rc;
546 
547 	mutex_lock(&wil->mutex);
548 	rc = __wil_up(wil);
549 	mutex_unlock(&wil->mutex);
550 
551 	return rc;
552 }
553 
554 static int __wil_down(struct wil6210_priv *wil)
555 {
556 	WARN_ON(!mutex_is_locked(&wil->mutex));
557 
558 	clear_bit(wil_status_napi_en, &wil->status);
559 	napi_disable(&wil->napi_rx);
560 	napi_disable(&wil->napi_tx);
561 
562 	if (wil->scan_request) {
563 		wil_dbg_misc(wil, "Abort scan_request 0x%p\n",
564 			     wil->scan_request);
565 		del_timer_sync(&wil->scan_timer);
566 		cfg80211_scan_done(wil->scan_request, true);
567 		wil->scan_request = NULL;
568 	}
569 
570 	wil6210_disconnect(wil, NULL);
571 	wil_rx_fini(wil);
572 
573 	return 0;
574 }
575 
576 int wil_down(struct wil6210_priv *wil)
577 {
578 	int rc;
579 
580 	mutex_lock(&wil->mutex);
581 	rc = __wil_down(wil);
582 	mutex_unlock(&wil->mutex);
583 
584 	return rc;
585 }
586 
587 int wil_find_cid(struct wil6210_priv *wil, const u8 *mac)
588 {
589 	int i;
590 	int rc = -ENOENT;
591 
592 	for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
593 		if ((wil->sta[i].status != wil_sta_unused) &&
594 		    ether_addr_equal(wil->sta[i].addr, mac)) {
595 			rc = i;
596 			break;
597 		}
598 	}
599 
600 	return rc;
601 }
602