wil6210: target reset flow update
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / ath / wil6210 / main.c
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 wil_sta_info *sta = &wil->sta[cid];
65
66         sta->data_port_open = false;
67         if (sta->status != wil_sta_unused) {
68                 wmi_disconnect_sta(wil, sta->addr, WLAN_REASON_DEAUTH_LEAVING);
69                 sta->status = wil_sta_unused;
70         }
71
72         for (i = 0; i < WIL_STA_TID_NUM; i++) {
73                 struct wil_tid_ampdu_rx *r = sta->tid_rx[i];
74                 sta->tid_rx[i] = NULL;
75                 wil_tid_ampdu_rx_free(wil, r);
76         }
77         for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++) {
78                 if (wil->vring2cid_tid[i][0] == cid)
79                         wil_vring_fini_tx(wil, i);
80         }
81         memset(&sta->stats, 0, sizeof(sta->stats));
82 }
83
84 static void _wil6210_disconnect(struct wil6210_priv *wil, void *bssid)
85 {
86         int cid = -ENOENT;
87         struct net_device *ndev = wil_to_ndev(wil);
88         struct wireless_dev *wdev = wil->wdev;
89
90         might_sleep();
91         if (bssid) {
92                 cid = wil_find_cid(wil, bssid);
93                 wil_dbg_misc(wil, "%s(%pM, CID %d)\n", __func__, bssid, cid);
94         } else {
95                 wil_dbg_misc(wil, "%s(all)\n", __func__);
96         }
97
98         if (cid >= 0) /* disconnect 1 peer */
99                 wil_disconnect_cid(wil, cid);
100         else /* disconnect all */
101                 for (cid = 0; cid < WIL6210_MAX_CID; cid++)
102                         wil_disconnect_cid(wil, cid);
103
104         /* link state */
105         switch (wdev->iftype) {
106         case NL80211_IFTYPE_STATION:
107         case NL80211_IFTYPE_P2P_CLIENT:
108                 wil_link_off(wil);
109                 if (test_bit(wil_status_fwconnected, &wil->status)) {
110                         clear_bit(wil_status_fwconnected, &wil->status);
111                         cfg80211_disconnected(ndev,
112                                               WLAN_STATUS_UNSPECIFIED_FAILURE,
113                                               NULL, 0, GFP_KERNEL);
114                 } else if (test_bit(wil_status_fwconnecting, &wil->status)) {
115                         cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
116                                                 WLAN_STATUS_UNSPECIFIED_FAILURE,
117                                                 GFP_KERNEL);
118                 }
119                 clear_bit(wil_status_fwconnecting, &wil->status);
120                 break;
121         default:
122                 /* AP-like interface and monitor:
123                  * never scan, always connected
124                  */
125                 if (bssid)
126                         cfg80211_del_sta(ndev, bssid, GFP_KERNEL);
127                 break;
128         }
129 }
130
131 static void wil_disconnect_worker(struct work_struct *work)
132 {
133         struct wil6210_priv *wil = container_of(work,
134                         struct wil6210_priv, disconnect_worker);
135
136         _wil6210_disconnect(wil, NULL);
137 }
138
139 static void wil_connect_timer_fn(ulong x)
140 {
141         struct wil6210_priv *wil = (void *)x;
142
143         wil_dbg_misc(wil, "Connect timeout\n");
144
145         /* reschedule to thread context - disconnect won't
146          * run from atomic context
147          */
148         schedule_work(&wil->disconnect_worker);
149 }
150
151 static void wil_fw_error_worker(struct work_struct *work)
152 {
153         struct wil6210_priv *wil = container_of(work,
154                         struct wil6210_priv, fw_error_worker);
155         struct wireless_dev *wdev = wil->wdev;
156
157         wil_dbg_misc(wil, "fw error worker\n");
158
159         if (no_fw_recovery)
160                 return;
161
162         mutex_lock(&wil->mutex);
163         switch (wdev->iftype) {
164         case NL80211_IFTYPE_STATION:
165         case NL80211_IFTYPE_P2P_CLIENT:
166         case NL80211_IFTYPE_MONITOR:
167                 wil_info(wil, "fw error recovery started...\n");
168                 wil_reset(wil);
169
170                 /* need to re-allocate Rx ring after reset */
171                 wil_rx_init(wil);
172                 break;
173         case NL80211_IFTYPE_AP:
174         case NL80211_IFTYPE_P2P_GO:
175                 /* recovery in these modes is done by upper layers */
176                 break;
177         default:
178                 break;
179         }
180         mutex_unlock(&wil->mutex);
181 }
182
183 static int wil_find_free_vring(struct wil6210_priv *wil)
184 {
185         int i;
186         for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
187                 if (!wil->vring_tx[i].va)
188                         return i;
189         }
190         return -EINVAL;
191 }
192
193 static void wil_connect_worker(struct work_struct *work)
194 {
195         int rc;
196         struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
197                                                 connect_worker);
198         int cid = wil->pending_connect_cid;
199         int ringid = wil_find_free_vring(wil);
200
201         if (cid < 0) {
202                 wil_err(wil, "No connection pending\n");
203                 return;
204         }
205
206         wil_dbg_wmi(wil, "Configure for connection CID %d\n", cid);
207
208         rc = wil_vring_init_tx(wil, ringid, WIL6210_TX_RING_SIZE, cid, 0);
209         wil->pending_connect_cid = -1;
210         if (rc == 0) {
211                 wil->sta[cid].status = wil_sta_connected;
212                 wil_link_on(wil);
213         } else {
214                 wil->sta[cid].status = wil_sta_unused;
215         }
216 }
217
218 int wil_priv_init(struct wil6210_priv *wil)
219 {
220         wil_dbg_misc(wil, "%s()\n", __func__);
221
222         memset(wil->sta, 0, sizeof(wil->sta));
223
224         mutex_init(&wil->mutex);
225         mutex_init(&wil->wmi_mutex);
226
227         init_completion(&wil->wmi_ready);
228
229         wil->pending_connect_cid = -1;
230         setup_timer(&wil->connect_timer, wil_connect_timer_fn, (ulong)wil);
231
232         INIT_WORK(&wil->connect_worker, wil_connect_worker);
233         INIT_WORK(&wil->disconnect_worker, wil_disconnect_worker);
234         INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
235         INIT_WORK(&wil->fw_error_worker, wil_fw_error_worker);
236
237         INIT_LIST_HEAD(&wil->pending_wmi_ev);
238         spin_lock_init(&wil->wmi_ev_lock);
239
240         wil->wmi_wq = create_singlethread_workqueue(WIL_NAME"_wmi");
241         if (!wil->wmi_wq)
242                 return -EAGAIN;
243
244         wil->wmi_wq_conn = create_singlethread_workqueue(WIL_NAME"_connect");
245         if (!wil->wmi_wq_conn) {
246                 destroy_workqueue(wil->wmi_wq);
247                 return -EAGAIN;
248         }
249
250         return 0;
251 }
252
253 void wil6210_disconnect(struct wil6210_priv *wil, void *bssid)
254 {
255         del_timer_sync(&wil->connect_timer);
256         _wil6210_disconnect(wil, bssid);
257 }
258
259 void wil_priv_deinit(struct wil6210_priv *wil)
260 {
261         cancel_work_sync(&wil->disconnect_worker);
262         cancel_work_sync(&wil->fw_error_worker);
263         wil6210_disconnect(wil, NULL);
264         wmi_event_flush(wil);
265         destroy_workqueue(wil->wmi_wq_conn);
266         destroy_workqueue(wil->wmi_wq);
267 }
268
269 static void wil_target_reset(struct wil6210_priv *wil)
270 {
271         int delay = 0;
272         u32 hw_state;
273         u32 rev_id;
274
275         wil_dbg_misc(wil, "Resetting...\n");
276
277         /* register read */
278 #define R(a) ioread32(wil->csr + HOSTADDR(a))
279         /* register write */
280 #define W(a, v) iowrite32(v, wil->csr + HOSTADDR(a))
281         /* register set = read, OR, write */
282 #define S(a, v) W(a, R(a) | v)
283         /* register clear = read, AND with inverted, write */
284 #define C(a, v) W(a, R(a) & ~v)
285
286         wil->hw_version = R(RGF_USER_FW_REV_ID);
287         rev_id = wil->hw_version & 0xff;
288         /* hpal_perst_from_pad_src_n_mask */
289         S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(6));
290         /* car_perst_rst_src_n_mask */
291         S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(7));
292
293         W(RGF_USER_MAC_CPU_0,  BIT(1)); /* mac_cpu_man_rst */
294         W(RGF_USER_USER_CPU_0, BIT(1)); /* user_cpu_man_rst */
295
296         W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
297         W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
298         W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000170);
299         W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FC00);
300
301         W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
302         W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
303         W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
304         W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
305
306         W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000001);
307         if (rev_id == 1) {
308                 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00000080);
309         } else {
310                 W(RGF_PCIE_LOS_COUNTER_CTL, BIT(6) | BIT(8));
311                 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000);
312         }
313         W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
314
315         /* wait until device ready */
316         do {
317                 msleep(1);
318                 hw_state = R(RGF_USER_HW_MACHINE_STATE);
319                 if (delay++ > 100) {
320                         wil_err(wil, "Reset not completed, hw_state 0x%08x\n",
321                                 hw_state);
322                         return;
323                 }
324         } while (hw_state != HW_MACHINE_BOOT_DONE);
325
326         if (rev_id == 2)
327                 W(RGF_PCIE_LOS_COUNTER_CTL, BIT(8));
328
329         C(RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
330
331         wil_dbg_misc(wil, "Reset completed in %d ms\n", delay);
332
333 #undef R
334 #undef W
335 #undef S
336 #undef C
337 }
338
339 void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
340 {
341         le32_to_cpus(&r->base);
342         le16_to_cpus(&r->entry_size);
343         le16_to_cpus(&r->size);
344         le32_to_cpus(&r->tail);
345         le32_to_cpus(&r->head);
346 }
347
348 static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
349 {
350         ulong to = msecs_to_jiffies(1000);
351         ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
352         if (0 == left) {
353                 wil_err(wil, "Firmware not ready\n");
354                 return -ETIME;
355         } else {
356                 wil_dbg_misc(wil, "FW ready after %d ms\n",
357                              jiffies_to_msecs(to-left));
358         }
359         return 0;
360 }
361
362 /*
363  * We reset all the structures, and we reset the UMAC.
364  * After calling this routine, you're expected to reload
365  * the firmware.
366  */
367 int wil_reset(struct wil6210_priv *wil)
368 {
369         int rc;
370
371         wil->status = 0; /* prevent NAPI from being scheduled */
372         if (test_bit(wil_status_napi_en, &wil->status)) {
373                 napi_synchronize(&wil->napi_rx);
374                 napi_synchronize(&wil->napi_tx);
375         }
376
377         if (wil->scan_request) {
378                 wil_dbg_misc(wil, "Abort scan_request 0x%p\n",
379                              wil->scan_request);
380                 cfg80211_scan_done(wil->scan_request, true);
381                 wil->scan_request = NULL;
382         }
383
384         cancel_work_sync(&wil->disconnect_worker);
385         wil6210_disconnect(wil, NULL);
386
387         wil6210_disable_irq(wil);
388
389         wmi_event_flush(wil);
390
391         flush_workqueue(wil->wmi_wq_conn);
392         flush_workqueue(wil->wmi_wq);
393
394         /* TODO: put MAC in reset */
395         wil_target_reset(wil);
396
397         wil_rx_fini(wil);
398
399         /* init after reset */
400         wil->pending_connect_cid = -1;
401         reinit_completion(&wil->wmi_ready);
402
403         /* TODO: release MAC reset */
404         wil6210_enable_irq(wil);
405
406         /* we just started MAC, wait for FW ready */
407         rc = wil_wait_for_fw_ready(wil);
408
409         return rc;
410 }
411
412 void wil_fw_error_recovery(struct wil6210_priv *wil)
413 {
414         wil_dbg_misc(wil, "starting fw error recovery\n");
415         schedule_work(&wil->fw_error_worker);
416 }
417
418 void wil_link_on(struct wil6210_priv *wil)
419 {
420         struct net_device *ndev = wil_to_ndev(wil);
421
422         wil_dbg_misc(wil, "%s()\n", __func__);
423
424         netif_carrier_on(ndev);
425         netif_tx_wake_all_queues(ndev);
426 }
427
428 void wil_link_off(struct wil6210_priv *wil)
429 {
430         struct net_device *ndev = wil_to_ndev(wil);
431
432         wil_dbg_misc(wil, "%s()\n", __func__);
433
434         netif_tx_stop_all_queues(ndev);
435         netif_carrier_off(ndev);
436 }
437
438 static int __wil_up(struct wil6210_priv *wil)
439 {
440         struct net_device *ndev = wil_to_ndev(wil);
441         struct wireless_dev *wdev = wil->wdev;
442         int rc;
443
444         rc = wil_reset(wil);
445         if (rc)
446                 return rc;
447
448         /* Rx VRING. After MAC and beacon */
449         rc = wil_rx_init(wil);
450         if (rc)
451                 return rc;
452
453         switch (wdev->iftype) {
454         case NL80211_IFTYPE_STATION:
455                 wil_dbg_misc(wil, "type: STATION\n");
456                 ndev->type = ARPHRD_ETHER;
457                 break;
458         case NL80211_IFTYPE_AP:
459                 wil_dbg_misc(wil, "type: AP\n");
460                 ndev->type = ARPHRD_ETHER;
461                 break;
462         case NL80211_IFTYPE_P2P_CLIENT:
463                 wil_dbg_misc(wil, "type: P2P_CLIENT\n");
464                 ndev->type = ARPHRD_ETHER;
465                 break;
466         case NL80211_IFTYPE_P2P_GO:
467                 wil_dbg_misc(wil, "type: P2P_GO\n");
468                 ndev->type = ARPHRD_ETHER;
469                 break;
470         case NL80211_IFTYPE_MONITOR:
471                 wil_dbg_misc(wil, "type: Monitor\n");
472                 ndev->type = ARPHRD_IEEE80211_RADIOTAP;
473                 /* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
474                 break;
475         default:
476                 return -EOPNOTSUPP;
477         }
478
479         /* MAC address - pre-requisite for other commands */
480         wmi_set_mac_address(wil, ndev->dev_addr);
481
482
483         napi_enable(&wil->napi_rx);
484         napi_enable(&wil->napi_tx);
485         set_bit(wil_status_napi_en, &wil->status);
486
487         return 0;
488 }
489
490 int wil_up(struct wil6210_priv *wil)
491 {
492         int rc;
493
494         mutex_lock(&wil->mutex);
495         rc = __wil_up(wil);
496         mutex_unlock(&wil->mutex);
497
498         return rc;
499 }
500
501 static int __wil_down(struct wil6210_priv *wil)
502 {
503         clear_bit(wil_status_napi_en, &wil->status);
504         napi_disable(&wil->napi_rx);
505         napi_disable(&wil->napi_tx);
506
507         if (wil->scan_request) {
508                 cfg80211_scan_done(wil->scan_request, true);
509                 wil->scan_request = NULL;
510         }
511
512         wil6210_disconnect(wil, NULL);
513         wil_rx_fini(wil);
514
515         return 0;
516 }
517
518 int wil_down(struct wil6210_priv *wil)
519 {
520         int rc;
521
522         mutex_lock(&wil->mutex);
523         rc = __wil_down(wil);
524         mutex_unlock(&wil->mutex);
525
526         return rc;
527 }
528
529 int wil_find_cid(struct wil6210_priv *wil, const u8 *mac)
530 {
531         int i;
532         int rc = -ENOENT;
533
534         for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
535                 if ((wil->sta[i].status != wil_sta_unused) &&
536                     ether_addr_equal(wil->sta[i].addr, mac)) {
537                         rc = i;
538                         break;
539                 }
540         }
541
542         return rc;
543 }