a17278f4090ac22970a1ba64b6b43de60181ff8e
[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         u32 rev_id;
506         bool is_sparrow = (wil->board->board == WIL_BOARD_SPARROW);
507
508         wil_dbg_misc(wil, "Resetting \"%s\"...\n", wil->board->name);
509
510         wil->hw_version = R(RGF_USER_FW_REV_ID);
511         rev_id = wil->hw_version & 0xff;
512
513         /* Clear MAC link up */
514         S(RGF_HP_CTRL, BIT(15));
515         S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_HPAL_PERST_FROM_PAD);
516         S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_CAR_PERST_RST);
517
518         wil_halt_cpu(wil);
519
520         /* Clear Fw Download notification */
521         C(RGF_USER_USAGE_6, BIT(0));
522
523         if (is_sparrow) {
524                 S(RGF_CAF_OSC_CONTROL, BIT_CAF_OSC_XTAL_EN);
525                 /* XTAL stabilization should take about 3ms */
526                 usleep_range(5000, 7000);
527                 x = R(RGF_CAF_PLL_LOCK_STATUS);
528                 if (!(x & BIT_CAF_OSC_DIG_XTAL_STABLE)) {
529                         wil_err(wil, "Xtal stabilization timeout\n"
530                                 "RGF_CAF_PLL_LOCK_STATUS = 0x%08x\n", x);
531                         return -ETIME;
532                 }
533                 /* switch 10k to XTAL*/
534                 C(RGF_USER_SPARROW_M_4, BIT_SPARROW_M_4_SEL_SLEEP_OR_REF);
535                 /* 40 MHz */
536                 C(RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_CAR_AHB_SW_SEL);
537
538                 W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x3ff81f);
539                 W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0xf);
540         }
541
542         W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
543         W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
544         W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, is_sparrow ? 0x000000f0 : 0x00000170);
545         W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FE00);
546
547         if (is_sparrow) {
548                 W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x0);
549                 W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0x0);
550         }
551
552         W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
553         W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
554         W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
555         W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
556
557         if (is_sparrow) {
558                 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000003);
559                 /* reset A2 PCIE AHB */
560                 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000);
561         } else {
562                 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000001);
563                 if (rev_id == 1) {
564                         /* reset A1 BOTH PCIE AHB & PCIE RGF */
565                         W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00000080);
566                 } else {
567                         W(RGF_PCIE_LOS_COUNTER_CTL, BIT(6) | BIT(8));
568                         W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000);
569                 }
570         }
571
572         /* TODO: check order here!!! Erez code is different */
573         W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
574
575         /* wait until device ready. typical time is 200..250 msec */
576         do {
577                 msleep(RST_DELAY);
578                 x = R(RGF_USER_HW_MACHINE_STATE);
579                 if (delay++ > RST_COUNT) {
580                         wil_err(wil, "Reset not completed, hw_state 0x%08x\n",
581                                 x);
582                         return -ETIME;
583                 }
584         } while (x != HW_MACHINE_BOOT_DONE);
585
586         /* TODO: Erez check rev_id != 1 */
587         if (!is_sparrow && (rev_id != 1))
588                 W(RGF_PCIE_LOS_COUNTER_CTL, BIT(8));
589
590         C(RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
591
592         wil_dbg_misc(wil, "Reset completed in %d ms\n", delay * RST_DELAY);
593         return 0;
594 }
595
596 /**
597  * wil_set_itr_trsh: - apply interrupt coalescing params
598  */
599 void wil_set_itr_trsh(struct wil6210_priv *wil)
600 {
601         /* disable, use usec resolution */
602         W(RGF_DMA_ITR_CNT_CRL, BIT_DMA_ITR_CNT_CRL_EXT_TICK);
603
604         /* disable interrupt moderation for monitor
605          * to get better timestamp precision
606          */
607         if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR)
608                 return;
609
610         wil_info(wil, "set ITR_TRSH = %d usec\n", wil->itr_trsh);
611         W(RGF_DMA_ITR_CNT_TRSH, wil->itr_trsh);
612         W(RGF_DMA_ITR_CNT_CRL, BIT_DMA_ITR_CNT_CRL_EN |
613           BIT_DMA_ITR_CNT_CRL_EXT_TICK); /* start it */
614 }
615
616 #undef R
617 #undef W
618 #undef S
619 #undef C
620
621 void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
622 {
623         le32_to_cpus(&r->base);
624         le16_to_cpus(&r->entry_size);
625         le16_to_cpus(&r->size);
626         le32_to_cpus(&r->tail);
627         le32_to_cpus(&r->head);
628 }
629
630 static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
631 {
632         ulong to = msecs_to_jiffies(1000);
633         ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
634
635         if (0 == left) {
636                 wil_err(wil, "Firmware not ready\n");
637                 return -ETIME;
638         } else {
639                 wil_info(wil, "FW ready after %d ms. HW version 0x%08x\n",
640                          jiffies_to_msecs(to-left), wil->hw_version);
641         }
642         return 0;
643 }
644
645 /*
646  * We reset all the structures, and we reset the UMAC.
647  * After calling this routine, you're expected to reload
648  * the firmware.
649  */
650 int wil_reset(struct wil6210_priv *wil)
651 {
652         int rc;
653
654         wil_dbg_misc(wil, "%s()\n", __func__);
655
656         WARN_ON(!mutex_is_locked(&wil->mutex));
657         WARN_ON(test_bit(wil_status_napi_en, wil->status));
658
659         cancel_work_sync(&wil->disconnect_worker);
660         wil6210_disconnect(wil, NULL, WLAN_REASON_DEAUTH_LEAVING, false);
661
662         /* prevent NAPI from being scheduled */
663         bitmap_zero(wil->status, wil_status_last);
664
665         if (wil->scan_request) {
666                 wil_dbg_misc(wil, "Abort scan_request 0x%p\n",
667                              wil->scan_request);
668                 del_timer_sync(&wil->scan_timer);
669                 cfg80211_scan_done(wil->scan_request, true);
670                 wil->scan_request = NULL;
671         }
672
673         wil_mask_irq(wil);
674
675         wmi_event_flush(wil);
676
677         flush_workqueue(wil->wq_service);
678         flush_workqueue(wil->wmi_wq);
679
680         rc = wil_target_reset(wil);
681         wil_rx_fini(wil);
682         if (rc)
683                 return rc;
684
685         if (!no_fw_load) {
686                 wil_info(wil, "Use firmware <%s>\n", WIL_FW_NAME);
687                 wil_halt_cpu(wil);
688                 /* Loading f/w from the file */
689                 rc = wil_request_firmware(wil, WIL_FW_NAME);
690                 if (rc)
691                         return rc;
692
693                 /* clear any interrupts which on-card-firmware may have set */
694                 wil6210_clear_irq(wil);
695                 { /* CAF_ICR - clear and mask */
696                         u32 a = HOSTADDR(RGF_CAF_ICR) +
697                                 offsetof(struct RGF_ICR, ICR);
698                         u32 m = HOSTADDR(RGF_CAF_ICR) +
699                                 offsetof(struct RGF_ICR, IMV);
700                         u32 icr = ioread32(wil->csr + a);
701
702                         iowrite32(icr, wil->csr + a); /* W1C */
703                         iowrite32(~0, wil->csr + m);
704                         wmb(); /* wait for completion */
705                 }
706                 wil_release_cpu(wil);
707         } else {
708                 wil_info(wil, "Use firmware from on-card flash\n");
709         }
710
711         /* init after reset */
712         wil->pending_connect_cid = -1;
713         reinit_completion(&wil->wmi_ready);
714         reinit_completion(&wil->wmi_call);
715
716         wil_unmask_irq(wil);
717
718         /* we just started MAC, wait for FW ready */
719         rc = wil_wait_for_fw_ready(wil);
720
721         return rc;
722 }
723
724 void wil_fw_error_recovery(struct wil6210_priv *wil)
725 {
726         wil_dbg_misc(wil, "starting fw error recovery\n");
727         wil->recovery_state = fw_recovery_pending;
728         schedule_work(&wil->fw_error_worker);
729 }
730
731 void wil_link_on(struct wil6210_priv *wil)
732 {
733         struct net_device *ndev = wil_to_ndev(wil);
734
735         wil_dbg_misc(wil, "%s()\n", __func__);
736
737         netif_carrier_on(ndev);
738         wil_dbg_misc(wil, "netif_tx_wake : link on\n");
739         netif_tx_wake_all_queues(ndev);
740 }
741
742 void wil_link_off(struct wil6210_priv *wil)
743 {
744         struct net_device *ndev = wil_to_ndev(wil);
745
746         wil_dbg_misc(wil, "%s()\n", __func__);
747
748         netif_tx_stop_all_queues(ndev);
749         wil_dbg_misc(wil, "netif_tx_stop : link off\n");
750         netif_carrier_off(ndev);
751 }
752
753 int __wil_up(struct wil6210_priv *wil)
754 {
755         struct net_device *ndev = wil_to_ndev(wil);
756         struct wireless_dev *wdev = wil->wdev;
757         int rc;
758
759         WARN_ON(!mutex_is_locked(&wil->mutex));
760
761         rc = wil_reset(wil);
762         if (rc)
763                 return rc;
764
765         /* Rx VRING. After MAC and beacon */
766         rc = wil_rx_init(wil, 1 << rx_ring_order);
767         if (rc)
768                 return rc;
769
770         switch (wdev->iftype) {
771         case NL80211_IFTYPE_STATION:
772                 wil_dbg_misc(wil, "type: STATION\n");
773                 ndev->type = ARPHRD_ETHER;
774                 break;
775         case NL80211_IFTYPE_AP:
776                 wil_dbg_misc(wil, "type: AP\n");
777                 ndev->type = ARPHRD_ETHER;
778                 break;
779         case NL80211_IFTYPE_P2P_CLIENT:
780                 wil_dbg_misc(wil, "type: P2P_CLIENT\n");
781                 ndev->type = ARPHRD_ETHER;
782                 break;
783         case NL80211_IFTYPE_P2P_GO:
784                 wil_dbg_misc(wil, "type: P2P_GO\n");
785                 ndev->type = ARPHRD_ETHER;
786                 break;
787         case NL80211_IFTYPE_MONITOR:
788                 wil_dbg_misc(wil, "type: Monitor\n");
789                 ndev->type = ARPHRD_IEEE80211_RADIOTAP;
790                 /* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
791                 break;
792         default:
793                 return -EOPNOTSUPP;
794         }
795
796         /* MAC address - pre-requisite for other commands */
797         wmi_set_mac_address(wil, ndev->dev_addr);
798
799         wil_dbg_misc(wil, "NAPI enable\n");
800         napi_enable(&wil->napi_rx);
801         napi_enable(&wil->napi_tx);
802         set_bit(wil_status_napi_en, wil->status);
803
804         if (wil->platform_ops.bus_request)
805                 wil->platform_ops.bus_request(wil->platform_handle,
806                                               WIL_MAX_BUS_REQUEST_KBPS);
807
808         return 0;
809 }
810
811 int wil_up(struct wil6210_priv *wil)
812 {
813         int rc;
814
815         wil_dbg_misc(wil, "%s()\n", __func__);
816
817         mutex_lock(&wil->mutex);
818         rc = __wil_up(wil);
819         mutex_unlock(&wil->mutex);
820
821         return rc;
822 }
823
824 int __wil_down(struct wil6210_priv *wil)
825 {
826         int iter = WAIT_FOR_DISCONNECT_TIMEOUT_MS /
827                         WAIT_FOR_DISCONNECT_INTERVAL_MS;
828
829         WARN_ON(!mutex_is_locked(&wil->mutex));
830
831         if (wil->platform_ops.bus_request)
832                 wil->platform_ops.bus_request(wil->platform_handle, 0);
833
834         wil_disable_irq(wil);
835         if (test_and_clear_bit(wil_status_napi_en, wil->status)) {
836                 napi_disable(&wil->napi_rx);
837                 napi_disable(&wil->napi_tx);
838                 wil_dbg_misc(wil, "NAPI disable\n");
839         }
840         wil_enable_irq(wil);
841
842         if (wil->scan_request) {
843                 wil_dbg_misc(wil, "Abort scan_request 0x%p\n",
844                              wil->scan_request);
845                 del_timer_sync(&wil->scan_timer);
846                 cfg80211_scan_done(wil->scan_request, true);
847                 wil->scan_request = NULL;
848         }
849
850         if (test_bit(wil_status_fwconnected, wil->status) ||
851             test_bit(wil_status_fwconnecting, wil->status))
852                 wmi_send(wil, WMI_DISCONNECT_CMDID, NULL, 0);
853
854         /* make sure wil is idle (not connected) */
855         mutex_unlock(&wil->mutex);
856         while (iter--) {
857                 int idle = !test_bit(wil_status_fwconnected, wil->status) &&
858                            !test_bit(wil_status_fwconnecting, wil->status);
859                 if (idle)
860                         break;
861                 msleep(WAIT_FOR_DISCONNECT_INTERVAL_MS);
862         }
863         mutex_lock(&wil->mutex);
864
865         if (!iter)
866                 wil_err(wil, "timeout waiting for idle FW/HW\n");
867
868         wil_rx_fini(wil);
869
870         return 0;
871 }
872
873 int wil_down(struct wil6210_priv *wil)
874 {
875         int rc;
876
877         wil_dbg_misc(wil, "%s()\n", __func__);
878
879         wil_set_recovery_state(wil, fw_recovery_idle);
880         mutex_lock(&wil->mutex);
881         rc = __wil_down(wil);
882         mutex_unlock(&wil->mutex);
883
884         return rc;
885 }
886
887 int wil_find_cid(struct wil6210_priv *wil, const u8 *mac)
888 {
889         int i;
890         int rc = -ENOENT;
891
892         for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
893                 if ((wil->sta[i].status != wil_sta_unused) &&
894                     ether_addr_equal(wil->sta[i].addr, mac)) {
895                         rc = i;
896                         break;
897                 }
898         }
899
900         return rc;
901 }