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