]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/wireless/ath/wil6210/main.c
Merge tag 'wireless-drivers-next-for-davem-2015-06-03' of git://git.kernel.org/pub...
[karo-tx-linux.git] / drivers / net / wireless / ath / wil6210 / main.c
1 /*
2  * Copyright (c) 2012-2015 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 debug_fw; /* = false; */
29 module_param(debug_fw, bool, S_IRUGO);
30 MODULE_PARM_DESC(debug_fw, " do not perform card reset. For FW debug");
31
32 bool no_fw_recovery;
33 module_param(no_fw_recovery, bool, S_IRUGO | S_IWUSR);
34 MODULE_PARM_DESC(no_fw_recovery, " disable automatic FW error recovery");
35
36 /* if not set via modparam, will be set to default value of 1/8 of
37  * rx ring size during init flow
38  */
39 unsigned short rx_ring_overflow_thrsh = WIL6210_RX_HIGH_TRSH_INIT;
40 module_param(rx_ring_overflow_thrsh, ushort, S_IRUGO);
41 MODULE_PARM_DESC(rx_ring_overflow_thrsh,
42                  " RX ring overflow threshold in descriptors.");
43
44 /* We allow allocation of more than 1 page buffers to support large packets.
45  * It is suboptimal behavior performance wise in case MTU above page size.
46  */
47 unsigned int mtu_max = TXRX_BUF_LEN_DEFAULT - WIL_MAX_MPDU_OVERHEAD;
48 static int mtu_max_set(const char *val, const struct kernel_param *kp)
49 {
50         int ret;
51
52         /* sets mtu_max directly. no need to restore it in case of
53          * illegal value since we assume this will fail insmod
54          */
55         ret = param_set_uint(val, kp);
56         if (ret)
57                 return ret;
58
59         if (mtu_max < 68 || mtu_max > WIL_MAX_ETH_MTU)
60                 ret = -EINVAL;
61
62         return ret;
63 }
64
65 static struct kernel_param_ops mtu_max_ops = {
66         .set = mtu_max_set,
67         .get = param_get_uint,
68 };
69
70 module_param_cb(mtu_max, &mtu_max_ops, &mtu_max, S_IRUGO);
71 MODULE_PARM_DESC(mtu_max, " Max MTU value.");
72
73 static uint rx_ring_order = WIL_RX_RING_SIZE_ORDER_DEFAULT;
74 static uint tx_ring_order = WIL_TX_RING_SIZE_ORDER_DEFAULT;
75 static uint bcast_ring_order = WIL_BCAST_RING_SIZE_ORDER_DEFAULT;
76
77 static int ring_order_set(const char *val, const struct kernel_param *kp)
78 {
79         int ret;
80         uint x;
81
82         ret = kstrtouint(val, 0, &x);
83         if (ret)
84                 return ret;
85
86         if ((x < WIL_RING_SIZE_ORDER_MIN) || (x > WIL_RING_SIZE_ORDER_MAX))
87                 return -EINVAL;
88
89         *((uint *)kp->arg) = x;
90
91         return 0;
92 }
93
94 static struct kernel_param_ops ring_order_ops = {
95         .set = ring_order_set,
96         .get = param_get_uint,
97 };
98
99 module_param_cb(rx_ring_order, &ring_order_ops, &rx_ring_order, S_IRUGO);
100 MODULE_PARM_DESC(rx_ring_order, " Rx ring order; size = 1 << order");
101 module_param_cb(tx_ring_order, &ring_order_ops, &tx_ring_order, S_IRUGO);
102 MODULE_PARM_DESC(tx_ring_order, " Tx ring order; size = 1 << order");
103
104 #define RST_DELAY (20) /* msec, for loop in @wil_target_reset */
105 #define RST_COUNT (1 + 1000/RST_DELAY) /* round up to be above 1 sec total */
106
107 /*
108  * Due to a hardware issue,
109  * one has to read/write to/from NIC in 32-bit chunks;
110  * regular memcpy_fromio and siblings will
111  * not work on 64-bit platform - it uses 64-bit transactions
112  *
113  * Force 32-bit transactions to enable NIC on 64-bit platforms
114  *
115  * To avoid byte swap on big endian host, __raw_{read|write}l
116  * should be used - {read|write}l would swap bytes to provide
117  * little endian on PCI value in host endianness.
118  */
119 void wil_memcpy_fromio_32(void *dst, const volatile void __iomem *src,
120                           size_t count)
121 {
122         u32 *d = dst;
123         const volatile u32 __iomem *s = src;
124
125         /* size_t is unsigned, if (count%4 != 0) it will wrap */
126         for (count += 4; count > 4; count -= 4)
127                 *d++ = __raw_readl(s++);
128 }
129
130 void wil_memcpy_toio_32(volatile void __iomem *dst, const void *src,
131                         size_t count)
132 {
133         volatile u32 __iomem *d = dst;
134         const u32 *s = src;
135
136         for (count += 4; count > 4; count -= 4)
137                 __raw_writel(*s++, d++);
138 }
139
140 static void wil_disconnect_cid(struct wil6210_priv *wil, int cid,
141                                u16 reason_code, bool from_event)
142 __acquires(&sta->tid_rx_lock) __releases(&sta->tid_rx_lock)
143 {
144         uint i;
145         struct net_device *ndev = wil_to_ndev(wil);
146         struct wireless_dev *wdev = wil->wdev;
147         struct wil_sta_info *sta = &wil->sta[cid];
148
149         might_sleep();
150         wil_dbg_misc(wil, "%s(CID %d, status %d)\n", __func__, cid,
151                      sta->status);
152
153         if (sta->status != wil_sta_unused) {
154                 if (!from_event)
155                         wmi_disconnect_sta(wil, sta->addr, reason_code);
156
157                 switch (wdev->iftype) {
158                 case NL80211_IFTYPE_AP:
159                 case NL80211_IFTYPE_P2P_GO:
160                         /* AP-like interface */
161                         cfg80211_del_sta(ndev, sta->addr, GFP_KERNEL);
162                         break;
163                 default:
164                         break;
165                 }
166                 sta->status = wil_sta_unused;
167         }
168
169         for (i = 0; i < WIL_STA_TID_NUM; i++) {
170                 struct wil_tid_ampdu_rx *r;
171
172                 spin_lock_bh(&sta->tid_rx_lock);
173
174                 r = sta->tid_rx[i];
175                 sta->tid_rx[i] = NULL;
176                 wil_tid_ampdu_rx_free(wil, r);
177
178                 spin_unlock_bh(&sta->tid_rx_lock);
179         }
180         for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++) {
181                 if (wil->vring2cid_tid[i][0] == cid)
182                         wil_vring_fini_tx(wil, i);
183         }
184         memset(&sta->stats, 0, sizeof(sta->stats));
185 }
186
187 static void _wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid,
188                                 u16 reason_code, bool from_event)
189 {
190         int cid = -ENOENT;
191         struct net_device *ndev = wil_to_ndev(wil);
192         struct wireless_dev *wdev = wil->wdev;
193
194         might_sleep();
195         wil_dbg_misc(wil, "%s(bssid=%pM, reason=%d, ev%s)\n", __func__, bssid,
196                      reason_code, from_event ? "+" : "-");
197
198         /* Cases are:
199          * - disconnect single STA, still connected
200          * - disconnect single STA, already disconnected
201          * - disconnect all
202          *
203          * For "disconnect all", there are 2 options:
204          * - bssid == NULL
205          * - bssid is our MAC address
206          */
207         if (bssid && memcmp(ndev->dev_addr, bssid, ETH_ALEN)) {
208                 cid = wil_find_cid(wil, bssid);
209                 wil_dbg_misc(wil, "Disconnect %pM, CID=%d, reason=%d\n",
210                              bssid, cid, reason_code);
211                 if (cid >= 0) /* disconnect 1 peer */
212                         wil_disconnect_cid(wil, cid, reason_code, from_event);
213         } else { /* all */
214                 wil_dbg_misc(wil, "Disconnect all\n");
215                 for (cid = 0; cid < WIL6210_MAX_CID; cid++)
216                         wil_disconnect_cid(wil, cid, reason_code, from_event);
217         }
218
219         /* link state */
220         switch (wdev->iftype) {
221         case NL80211_IFTYPE_STATION:
222         case NL80211_IFTYPE_P2P_CLIENT:
223                 wil_bcast_fini(wil);
224                 netif_tx_stop_all_queues(ndev);
225                 netif_carrier_off(ndev);
226
227                 if (test_bit(wil_status_fwconnected, wil->status)) {
228                         clear_bit(wil_status_fwconnected, wil->status);
229                         cfg80211_disconnected(ndev, reason_code,
230                                               NULL, 0, false, GFP_KERNEL);
231                 } else if (test_bit(wil_status_fwconnecting, wil->status)) {
232                         cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
233                                                 WLAN_STATUS_UNSPECIFIED_FAILURE,
234                                                 GFP_KERNEL);
235                 }
236                 clear_bit(wil_status_fwconnecting, wil->status);
237                 break;
238         default:
239                 break;
240         }
241 }
242
243 static void wil_disconnect_worker(struct work_struct *work)
244 {
245         struct wil6210_priv *wil = container_of(work,
246                         struct wil6210_priv, disconnect_worker);
247
248         mutex_lock(&wil->mutex);
249         _wil6210_disconnect(wil, NULL, WLAN_REASON_UNSPECIFIED, false);
250         mutex_unlock(&wil->mutex);
251 }
252
253 static void wil_connect_timer_fn(ulong x)
254 {
255         struct wil6210_priv *wil = (void *)x;
256
257         wil_dbg_misc(wil, "Connect timeout\n");
258
259         /* reschedule to thread context - disconnect won't
260          * run from atomic context
261          */
262         schedule_work(&wil->disconnect_worker);
263 }
264
265 static void wil_scan_timer_fn(ulong x)
266 {
267         struct wil6210_priv *wil = (void *)x;
268
269         clear_bit(wil_status_fwready, wil->status);
270         wil_err(wil, "Scan timeout detected, start fw error recovery\n");
271         wil->recovery_state = fw_recovery_pending;
272         schedule_work(&wil->fw_error_worker);
273 }
274
275 static int wil_wait_for_recovery(struct wil6210_priv *wil)
276 {
277         if (wait_event_interruptible(wil->wq, wil->recovery_state !=
278                                      fw_recovery_pending)) {
279                 wil_err(wil, "Interrupt, canceling recovery\n");
280                 return -ERESTARTSYS;
281         }
282         if (wil->recovery_state != fw_recovery_running) {
283                 wil_info(wil, "Recovery cancelled\n");
284                 return -EINTR;
285         }
286         wil_info(wil, "Proceed with recovery\n");
287         return 0;
288 }
289
290 void wil_set_recovery_state(struct wil6210_priv *wil, int state)
291 {
292         wil_dbg_misc(wil, "%s(%d -> %d)\n", __func__,
293                      wil->recovery_state, state);
294
295         wil->recovery_state = state;
296         wake_up_interruptible(&wil->wq);
297 }
298
299 static void wil_fw_error_worker(struct work_struct *work)
300 {
301         struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
302                                                 fw_error_worker);
303         struct wireless_dev *wdev = wil->wdev;
304
305         wil_dbg_misc(wil, "fw error worker\n");
306
307         if (!netif_running(wil_to_ndev(wil))) {
308                 wil_info(wil, "No recovery - interface is down\n");
309                 return;
310         }
311
312         /* increment @recovery_count if less then WIL6210_FW_RECOVERY_TO
313          * passed since last recovery attempt
314          */
315         if (time_is_after_jiffies(wil->last_fw_recovery +
316                                   WIL6210_FW_RECOVERY_TO))
317                 wil->recovery_count++;
318         else
319                 wil->recovery_count = 1; /* fw was alive for a long time */
320
321         if (wil->recovery_count > WIL6210_FW_RECOVERY_RETRIES) {
322                 wil_err(wil, "too many recovery attempts (%d), giving up\n",
323                         wil->recovery_count);
324                 return;
325         }
326
327         wil->last_fw_recovery = jiffies;
328
329         mutex_lock(&wil->mutex);
330         switch (wdev->iftype) {
331         case NL80211_IFTYPE_STATION:
332         case NL80211_IFTYPE_P2P_CLIENT:
333         case NL80211_IFTYPE_MONITOR:
334                 wil_info(wil, "fw error recovery requested (try %d)...\n",
335                          wil->recovery_count);
336                 if (!no_fw_recovery)
337                         wil->recovery_state = fw_recovery_running;
338                 if (0 != wil_wait_for_recovery(wil))
339                         break;
340
341                 __wil_down(wil);
342                 __wil_up(wil);
343                 break;
344         case NL80211_IFTYPE_AP:
345         case NL80211_IFTYPE_P2P_GO:
346                 wil_info(wil, "No recovery for AP-like interface\n");
347                 /* recovery in these modes is done by upper layers */
348                 break;
349         default:
350                 wil_err(wil, "No recovery - unknown interface type %d\n",
351                         wdev->iftype);
352                 break;
353         }
354         mutex_unlock(&wil->mutex);
355 }
356
357 static int wil_find_free_vring(struct wil6210_priv *wil)
358 {
359         int i;
360
361         for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
362                 if (!wil->vring_tx[i].va)
363                         return i;
364         }
365         return -EINVAL;
366 }
367
368 int wil_bcast_init(struct wil6210_priv *wil)
369 {
370         int ri = wil->bcast_vring, rc;
371
372         if ((ri >= 0) && wil->vring_tx[ri].va)
373                 return 0;
374
375         ri = wil_find_free_vring(wil);
376         if (ri < 0)
377                 return ri;
378
379         wil->bcast_vring = ri;
380         rc = wil_vring_init_bcast(wil, ri, 1 << bcast_ring_order);
381         if (rc)
382                 wil->bcast_vring = -1;
383
384         return rc;
385 }
386
387 void wil_bcast_fini(struct wil6210_priv *wil)
388 {
389         int ri = wil->bcast_vring;
390
391         if (ri < 0)
392                 return;
393
394         wil->bcast_vring = -1;
395         wil_vring_fini_tx(wil, ri);
396 }
397
398 static void wil_connect_worker(struct work_struct *work)
399 {
400         int rc;
401         struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
402                                                 connect_worker);
403         struct net_device *ndev = wil_to_ndev(wil);
404
405         int cid = wil->pending_connect_cid;
406         int ringid = wil_find_free_vring(wil);
407
408         if (cid < 0) {
409                 wil_err(wil, "No connection pending\n");
410                 return;
411         }
412
413         wil_dbg_wmi(wil, "Configure for connection CID %d\n", cid);
414
415         rc = wil_vring_init_tx(wil, ringid, 1 << tx_ring_order, cid, 0);
416         wil->pending_connect_cid = -1;
417         if (rc == 0) {
418                 wil->sta[cid].status = wil_sta_connected;
419                 netif_tx_wake_all_queues(ndev);
420         } else {
421                 wil->sta[cid].status = wil_sta_unused;
422         }
423 }
424
425 int wil_priv_init(struct wil6210_priv *wil)
426 {
427         uint i;
428
429         wil_dbg_misc(wil, "%s()\n", __func__);
430
431         memset(wil->sta, 0, sizeof(wil->sta));
432         for (i = 0; i < WIL6210_MAX_CID; i++)
433                 spin_lock_init(&wil->sta[i].tid_rx_lock);
434
435         mutex_init(&wil->mutex);
436         mutex_init(&wil->wmi_mutex);
437         mutex_init(&wil->back_rx_mutex);
438         mutex_init(&wil->back_tx_mutex);
439         mutex_init(&wil->probe_client_mutex);
440
441         init_completion(&wil->wmi_ready);
442         init_completion(&wil->wmi_call);
443
444         wil->pending_connect_cid = -1;
445         wil->bcast_vring = -1;
446         setup_timer(&wil->connect_timer, wil_connect_timer_fn, (ulong)wil);
447         setup_timer(&wil->scan_timer, wil_scan_timer_fn, (ulong)wil);
448
449         INIT_WORK(&wil->connect_worker, wil_connect_worker);
450         INIT_WORK(&wil->disconnect_worker, wil_disconnect_worker);
451         INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
452         INIT_WORK(&wil->fw_error_worker, wil_fw_error_worker);
453         INIT_WORK(&wil->back_rx_worker, wil_back_rx_worker);
454         INIT_WORK(&wil->back_tx_worker, wil_back_tx_worker);
455         INIT_WORK(&wil->probe_client_worker, wil_probe_client_worker);
456
457         INIT_LIST_HEAD(&wil->pending_wmi_ev);
458         INIT_LIST_HEAD(&wil->back_rx_pending);
459         INIT_LIST_HEAD(&wil->back_tx_pending);
460         INIT_LIST_HEAD(&wil->probe_client_pending);
461         spin_lock_init(&wil->wmi_ev_lock);
462         init_waitqueue_head(&wil->wq);
463
464         wil->wmi_wq = create_singlethread_workqueue(WIL_NAME "_wmi");
465         if (!wil->wmi_wq)
466                 return -EAGAIN;
467
468         wil->wq_service = create_singlethread_workqueue(WIL_NAME "_service");
469         if (!wil->wq_service)
470                 goto out_wmi_wq;
471
472         wil->last_fw_recovery = jiffies;
473         wil->tx_interframe_timeout = WIL6210_ITR_TX_INTERFRAME_TIMEOUT_DEFAULT;
474         wil->rx_interframe_timeout = WIL6210_ITR_RX_INTERFRAME_TIMEOUT_DEFAULT;
475         wil->tx_max_burst_duration = WIL6210_ITR_TX_MAX_BURST_DURATION_DEFAULT;
476         wil->rx_max_burst_duration = WIL6210_ITR_RX_MAX_BURST_DURATION_DEFAULT;
477
478         if (rx_ring_overflow_thrsh == WIL6210_RX_HIGH_TRSH_INIT)
479                 rx_ring_overflow_thrsh = WIL6210_RX_HIGH_TRSH_DEFAULT;
480         return 0;
481
482 out_wmi_wq:
483         destroy_workqueue(wil->wmi_wq);
484
485         return -EAGAIN;
486 }
487
488 /**
489  * wil6210_disconnect - disconnect one connection
490  * @wil: driver context
491  * @bssid: peer to disconnect, NULL to disconnect all
492  * @reason_code: Reason code for the Disassociation frame
493  * @from_event: whether is invoked from FW event handler
494  *
495  * Disconnect and release associated resources. If invoked not from the
496  * FW event handler, issue WMI command(s) to trigger MAC disconnect.
497  */
498 void wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid,
499                         u16 reason_code, bool from_event)
500 {
501         wil_dbg_misc(wil, "%s()\n", __func__);
502
503         del_timer_sync(&wil->connect_timer);
504         _wil6210_disconnect(wil, bssid, reason_code, from_event);
505 }
506
507 void wil_priv_deinit(struct wil6210_priv *wil)
508 {
509         wil_dbg_misc(wil, "%s()\n", __func__);
510
511         wil_set_recovery_state(wil, fw_recovery_idle);
512         del_timer_sync(&wil->scan_timer);
513         cancel_work_sync(&wil->disconnect_worker);
514         cancel_work_sync(&wil->fw_error_worker);
515         mutex_lock(&wil->mutex);
516         wil6210_disconnect(wil, NULL, WLAN_REASON_DEAUTH_LEAVING, false);
517         mutex_unlock(&wil->mutex);
518         wmi_event_flush(wil);
519         wil_back_rx_flush(wil);
520         cancel_work_sync(&wil->back_rx_worker);
521         wil_back_tx_flush(wil);
522         cancel_work_sync(&wil->back_tx_worker);
523         wil_probe_client_flush(wil);
524         cancel_work_sync(&wil->probe_client_worker);
525         destroy_workqueue(wil->wq_service);
526         destroy_workqueue(wil->wmi_wq);
527 }
528
529 /* target operations */
530 /* register read */
531 #define R(a) ioread32(wil->csr + HOSTADDR(a))
532 /* register write. wmb() to make sure it is completed */
533 #define W(a, v) do { iowrite32(v, wil->csr + HOSTADDR(a)); wmb(); } while (0)
534 /* register set = read, OR, write */
535 #define S(a, v) W(a, R(a) | v)
536 /* register clear = read, AND with inverted, write */
537 #define C(a, v) W(a, R(a) & ~v)
538
539 static inline void wil_halt_cpu(struct wil6210_priv *wil)
540 {
541         W(RGF_USER_USER_CPU_0, BIT_USER_USER_CPU_MAN_RST);
542         W(RGF_USER_MAC_CPU_0,  BIT_USER_MAC_CPU_MAN_RST);
543 }
544
545 static inline void wil_release_cpu(struct wil6210_priv *wil)
546 {
547         /* Start CPU */
548         W(RGF_USER_USER_CPU_0, 1);
549 }
550
551 static int wil_target_reset(struct wil6210_priv *wil)
552 {
553         int delay = 0;
554         u32 x, x1 = 0;
555
556         wil_dbg_misc(wil, "Resetting \"%s\"...\n", wil->hw_name);
557
558         /* Clear MAC link up */
559         S(RGF_HP_CTRL, BIT(15));
560         S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_HPAL_PERST_FROM_PAD);
561         S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_CAR_PERST_RST);
562
563         wil_halt_cpu(wil);
564
565         /* clear all boot loader "ready" bits */
566         W(RGF_USER_BL + offsetof(struct RGF_BL, ready), 0);
567         /* Clear Fw Download notification */
568         C(RGF_USER_USAGE_6, BIT(0));
569
570         S(RGF_CAF_OSC_CONTROL, BIT_CAF_OSC_XTAL_EN);
571         /* XTAL stabilization should take about 3ms */
572         usleep_range(5000, 7000);
573         x = R(RGF_CAF_PLL_LOCK_STATUS);
574         if (!(x & BIT_CAF_OSC_DIG_XTAL_STABLE)) {
575                 wil_err(wil, "Xtal stabilization timeout\n"
576                         "RGF_CAF_PLL_LOCK_STATUS = 0x%08x\n", x);
577                 return -ETIME;
578         }
579         /* switch 10k to XTAL*/
580         C(RGF_USER_SPARROW_M_4, BIT_SPARROW_M_4_SEL_SLEEP_OR_REF);
581         /* 40 MHz */
582         C(RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_CAR_AHB_SW_SEL);
583
584         W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x3ff81f);
585         W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0xf);
586
587         W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
588         W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
589         W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x000000f0);
590         W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FE00);
591
592         W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x0);
593         W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0x0);
594
595         W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
596         W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
597         W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
598         W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
599
600         W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000003);
601         W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000); /* reset A2 PCIE AHB */
602
603         W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
604
605         /* wait until device ready. typical time is 20..80 msec */
606         do {
607                 msleep(RST_DELAY);
608                 x = R(RGF_USER_BL + offsetof(struct RGF_BL, ready));
609                 if (x1 != x) {
610                         wil_dbg_misc(wil, "BL.ready 0x%08x => 0x%08x\n", x1, x);
611                         x1 = x;
612                 }
613                 if (delay++ > RST_COUNT) {
614                         wil_err(wil, "Reset not completed, bl.ready 0x%08x\n",
615                                 x);
616                         return -ETIME;
617                 }
618         } while (x != BIT_BL_READY);
619
620         C(RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
621
622         /* enable fix for HW bug related to the SA/DA swap in AP Rx */
623         S(RGF_DMA_OFUL_NID_0, BIT_DMA_OFUL_NID_0_RX_EXT_TR_EN |
624           BIT_DMA_OFUL_NID_0_RX_EXT_A3_SRC);
625
626         wil_dbg_misc(wil, "Reset completed in %d ms\n", delay * RST_DELAY);
627         return 0;
628 }
629
630 void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
631 {
632         le32_to_cpus(&r->base);
633         le16_to_cpus(&r->entry_size);
634         le16_to_cpus(&r->size);
635         le32_to_cpus(&r->tail);
636         le32_to_cpus(&r->head);
637 }
638
639 static int wil_get_bl_info(struct wil6210_priv *wil)
640 {
641         struct net_device *ndev = wil_to_ndev(wil);
642         struct RGF_BL bl;
643
644         wil_memcpy_fromio_32(&bl, wil->csr + HOSTADDR(RGF_USER_BL), sizeof(bl));
645         le32_to_cpus(&bl.ready);
646         le32_to_cpus(&bl.version);
647         le32_to_cpus(&bl.rf_type);
648         le32_to_cpus(&bl.baseband_type);
649
650         if (!is_valid_ether_addr(bl.mac_address)) {
651                 wil_err(wil, "BL: Invalid MAC %pM\n", bl.mac_address);
652                 return -EINVAL;
653         }
654
655         ether_addr_copy(ndev->perm_addr, bl.mac_address);
656         if (!is_valid_ether_addr(ndev->dev_addr))
657                 ether_addr_copy(ndev->dev_addr, bl.mac_address);
658         wil_info(wil,
659                  "Boot Loader: ver = %d MAC = %pM RF = 0x%08x bband = 0x%08x\n",
660                  bl.version, bl.mac_address, bl.rf_type, bl.baseband_type);
661
662         return 0;
663 }
664
665 static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
666 {
667         ulong to = msecs_to_jiffies(1000);
668         ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
669
670         if (0 == left) {
671                 wil_err(wil, "Firmware not ready\n");
672                 return -ETIME;
673         } else {
674                 wil_info(wil, "FW ready after %d ms. HW version 0x%08x\n",
675                          jiffies_to_msecs(to-left), wil->hw_version);
676         }
677         return 0;
678 }
679
680 /*
681  * We reset all the structures, and we reset the UMAC.
682  * After calling this routine, you're expected to reload
683  * the firmware.
684  */
685 int wil_reset(struct wil6210_priv *wil, bool load_fw)
686 {
687         int rc;
688
689         wil_dbg_misc(wil, "%s()\n", __func__);
690
691         if (wil->hw_version == HW_VER_UNKNOWN)
692                 return -ENODEV;
693
694         WARN_ON(!mutex_is_locked(&wil->mutex));
695         WARN_ON(test_bit(wil_status_napi_en, wil->status));
696
697         if (debug_fw) {
698                 static const u8 mac[ETH_ALEN] = {
699                         0x00, 0xde, 0xad, 0x12, 0x34, 0x56,
700                 };
701                 struct net_device *ndev = wil_to_ndev(wil);
702
703                 ether_addr_copy(ndev->perm_addr, mac);
704                 ether_addr_copy(ndev->dev_addr, ndev->perm_addr);
705                 return 0;
706         }
707
708         cancel_work_sync(&wil->disconnect_worker);
709         wil6210_disconnect(wil, NULL, WLAN_REASON_DEAUTH_LEAVING, false);
710         wil_bcast_fini(wil);
711
712         /* prevent NAPI from being scheduled */
713         bitmap_zero(wil->status, wil_status_last);
714
715         if (wil->scan_request) {
716                 wil_dbg_misc(wil, "Abort scan_request 0x%p\n",
717                              wil->scan_request);
718                 del_timer_sync(&wil->scan_timer);
719                 cfg80211_scan_done(wil->scan_request, true);
720                 wil->scan_request = NULL;
721         }
722
723         wil_mask_irq(wil);
724
725         wmi_event_flush(wil);
726
727         flush_workqueue(wil->wq_service);
728         flush_workqueue(wil->wmi_wq);
729
730         rc = wil_target_reset(wil);
731         wil_rx_fini(wil);
732         if (rc)
733                 return rc;
734
735         rc = wil_get_bl_info(wil);
736         if (rc)
737                 return rc;
738
739         if (load_fw) {
740                 wil_info(wil, "Use firmware <%s> + board <%s>\n", WIL_FW_NAME,
741                          WIL_FW2_NAME);
742
743                 wil_halt_cpu(wil);
744                 /* Loading f/w from the file */
745                 rc = wil_request_firmware(wil, WIL_FW_NAME);
746                 if (rc)
747                         return rc;
748                 rc = wil_request_firmware(wil, WIL_FW2_NAME);
749                 if (rc)
750                         return rc;
751
752                 /* Mark FW as loaded from host */
753                 S(RGF_USER_USAGE_6, 1);
754
755                 /* clear any interrupts which on-card-firmware
756                  * may have set
757                  */
758                 wil6210_clear_irq(wil);
759                 /* CAF_ICR - clear and mask */
760                 /* it is W1C, clear by writing back same value */
761                 S(RGF_CAF_ICR + offsetof(struct RGF_ICR, ICR), 0);
762                 W(RGF_CAF_ICR + offsetof(struct RGF_ICR, IMV), ~0);
763
764                 wil_release_cpu(wil);
765         }
766
767         /* init after reset */
768         wil->pending_connect_cid = -1;
769         wil->ap_isolate = 0;
770         reinit_completion(&wil->wmi_ready);
771         reinit_completion(&wil->wmi_call);
772
773         if (load_fw) {
774                 wil_configure_interrupt_moderation(wil);
775                 wil_unmask_irq(wil);
776
777                 /* we just started MAC, wait for FW ready */
778                 rc = wil_wait_for_fw_ready(wil);
779                 if (rc == 0) /* check FW is responsive */
780                         rc = wmi_echo(wil);
781         }
782
783         return rc;
784 }
785
786 #undef R
787 #undef W
788 #undef S
789 #undef C
790
791 void wil_fw_error_recovery(struct wil6210_priv *wil)
792 {
793         wil_dbg_misc(wil, "starting fw error recovery\n");
794         wil->recovery_state = fw_recovery_pending;
795         schedule_work(&wil->fw_error_worker);
796 }
797
798 int __wil_up(struct wil6210_priv *wil)
799 {
800         struct net_device *ndev = wil_to_ndev(wil);
801         struct wireless_dev *wdev = wil->wdev;
802         int rc;
803
804         WARN_ON(!mutex_is_locked(&wil->mutex));
805
806         rc = wil_reset(wil, true);
807         if (rc)
808                 return rc;
809
810         /* Rx VRING. After MAC and beacon */
811         rc = wil_rx_init(wil, 1 << rx_ring_order);
812         if (rc)
813                 return rc;
814
815         switch (wdev->iftype) {
816         case NL80211_IFTYPE_STATION:
817                 wil_dbg_misc(wil, "type: STATION\n");
818                 ndev->type = ARPHRD_ETHER;
819                 break;
820         case NL80211_IFTYPE_AP:
821                 wil_dbg_misc(wil, "type: AP\n");
822                 ndev->type = ARPHRD_ETHER;
823                 break;
824         case NL80211_IFTYPE_P2P_CLIENT:
825                 wil_dbg_misc(wil, "type: P2P_CLIENT\n");
826                 ndev->type = ARPHRD_ETHER;
827                 break;
828         case NL80211_IFTYPE_P2P_GO:
829                 wil_dbg_misc(wil, "type: P2P_GO\n");
830                 ndev->type = ARPHRD_ETHER;
831                 break;
832         case NL80211_IFTYPE_MONITOR:
833                 wil_dbg_misc(wil, "type: Monitor\n");
834                 ndev->type = ARPHRD_IEEE80211_RADIOTAP;
835                 /* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
836                 break;
837         default:
838                 return -EOPNOTSUPP;
839         }
840
841         /* MAC address - pre-requisite for other commands */
842         wmi_set_mac_address(wil, ndev->dev_addr);
843
844         wil_dbg_misc(wil, "NAPI enable\n");
845         napi_enable(&wil->napi_rx);
846         napi_enable(&wil->napi_tx);
847         set_bit(wil_status_napi_en, wil->status);
848
849         if (wil->platform_ops.bus_request)
850                 wil->platform_ops.bus_request(wil->platform_handle,
851                                               WIL_MAX_BUS_REQUEST_KBPS);
852
853         return 0;
854 }
855
856 int wil_up(struct wil6210_priv *wil)
857 {
858         int rc;
859
860         wil_dbg_misc(wil, "%s()\n", __func__);
861
862         mutex_lock(&wil->mutex);
863         rc = __wil_up(wil);
864         mutex_unlock(&wil->mutex);
865
866         return rc;
867 }
868
869 int __wil_down(struct wil6210_priv *wil)
870 {
871         int iter = WAIT_FOR_DISCONNECT_TIMEOUT_MS /
872                         WAIT_FOR_DISCONNECT_INTERVAL_MS;
873
874         WARN_ON(!mutex_is_locked(&wil->mutex));
875
876         if (wil->platform_ops.bus_request)
877                 wil->platform_ops.bus_request(wil->platform_handle, 0);
878
879         wil_disable_irq(wil);
880         if (test_and_clear_bit(wil_status_napi_en, wil->status)) {
881                 napi_disable(&wil->napi_rx);
882                 napi_disable(&wil->napi_tx);
883                 wil_dbg_misc(wil, "NAPI disable\n");
884         }
885         wil_enable_irq(wil);
886
887         if (wil->scan_request) {
888                 wil_dbg_misc(wil, "Abort scan_request 0x%p\n",
889                              wil->scan_request);
890                 del_timer_sync(&wil->scan_timer);
891                 cfg80211_scan_done(wil->scan_request, true);
892                 wil->scan_request = NULL;
893         }
894
895         if (test_bit(wil_status_fwconnected, wil->status) ||
896             test_bit(wil_status_fwconnecting, wil->status))
897                 wmi_send(wil, WMI_DISCONNECT_CMDID, NULL, 0);
898
899         /* make sure wil is idle (not connected) */
900         mutex_unlock(&wil->mutex);
901         while (iter--) {
902                 int idle = !test_bit(wil_status_fwconnected, wil->status) &&
903                            !test_bit(wil_status_fwconnecting, wil->status);
904                 if (idle)
905                         break;
906                 msleep(WAIT_FOR_DISCONNECT_INTERVAL_MS);
907         }
908         mutex_lock(&wil->mutex);
909
910         if (!iter)
911                 wil_err(wil, "timeout waiting for idle FW/HW\n");
912
913         wil_reset(wil, false);
914
915         return 0;
916 }
917
918 int wil_down(struct wil6210_priv *wil)
919 {
920         int rc;
921
922         wil_dbg_misc(wil, "%s()\n", __func__);
923
924         wil_set_recovery_state(wil, fw_recovery_idle);
925         mutex_lock(&wil->mutex);
926         rc = __wil_down(wil);
927         mutex_unlock(&wil->mutex);
928
929         return rc;
930 }
931
932 int wil_find_cid(struct wil6210_priv *wil, const u8 *mac)
933 {
934         int i;
935         int rc = -ENOENT;
936
937         for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
938                 if ((wil->sta[i].status != wil_sta_unused) &&
939                     ether_addr_equal(wil->sta[i].addr, mac)) {
940                         rc = i;
941                         break;
942                 }
943         }
944
945         return rc;
946 }