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