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