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