21667e0c3d1444345299d8e43182198551639b62
[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 static bool no_fw_recovery;
29 module_param(no_fw_recovery, bool, S_IRUGO | S_IWUSR);
30 MODULE_PARM_DESC(no_fw_recovery, " disable 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 #define RST_DELAY (20) /* msec, for loop in @wil_target_reset */
37 #define RST_COUNT (1 + 1000/RST_DELAY) /* round up to be above 1 sec total */
38
39 /*
40  * Due to a hardware issue,
41  * one has to read/write to/from NIC in 32-bit chunks;
42  * regular memcpy_fromio and siblings will
43  * not work on 64-bit platform - it uses 64-bit transactions
44  *
45  * Force 32-bit transactions to enable NIC on 64-bit platforms
46  *
47  * To avoid byte swap on big endian host, __raw_{read|write}l
48  * should be used - {read|write}l would swap bytes to provide
49  * little endian on PCI value in host endianness.
50  */
51 void wil_memcpy_fromio_32(void *dst, const volatile void __iomem *src,
52                           size_t count)
53 {
54         u32 *d = dst;
55         const volatile u32 __iomem *s = src;
56
57         /* size_t is unsigned, if (count%4 != 0) it will wrap */
58         for (count += 4; count > 4; count -= 4)
59                 *d++ = __raw_readl(s++);
60 }
61
62 void wil_memcpy_toio_32(volatile void __iomem *dst, const void *src,
63                         size_t count)
64 {
65         volatile u32 __iomem *d = dst;
66         const u32 *s = src;
67
68         for (count += 4; count > 4; count -= 4)
69                 __raw_writel(*s++, d++);
70 }
71
72 static void wil_disconnect_cid(struct wil6210_priv *wil, int cid)
73 {
74         uint i;
75         struct net_device *ndev = wil_to_ndev(wil);
76         struct wireless_dev *wdev = wil->wdev;
77         struct wil_sta_info *sta = &wil->sta[cid];
78
79         wil_dbg_misc(wil, "%s(CID %d, status %d)\n", __func__, cid,
80                      sta->status);
81
82         sta->data_port_open = false;
83         if (sta->status != wil_sta_unused) {
84                 wmi_disconnect_sta(wil, sta->addr, WLAN_REASON_DEAUTH_LEAVING);
85                 switch (wdev->iftype) {
86                 case NL80211_IFTYPE_AP:
87                 case NL80211_IFTYPE_P2P_GO:
88                         /* AP-like interface */
89                         cfg80211_del_sta(ndev, sta->addr, GFP_KERNEL);
90                         break;
91                 default:
92                         break;
93                 }
94                 sta->status = wil_sta_unused;
95         }
96
97         for (i = 0; i < WIL_STA_TID_NUM; i++) {
98                 struct wil_tid_ampdu_rx *r;
99                 unsigned long flags;
100
101                 spin_lock_irqsave(&sta->tid_rx_lock, flags);
102
103                 r = sta->tid_rx[i];
104                 sta->tid_rx[i] = NULL;
105                 wil_tid_ampdu_rx_free(wil, r);
106
107                 spin_unlock_irqrestore(&sta->tid_rx_lock, flags);
108         }
109         for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++) {
110                 if (wil->vring2cid_tid[i][0] == cid)
111                         wil_vring_fini_tx(wil, i);
112         }
113         memset(&sta->stats, 0, sizeof(sta->stats));
114 }
115
116 static void _wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid)
117 {
118         int cid = -ENOENT;
119         struct net_device *ndev = wil_to_ndev(wil);
120         struct wireless_dev *wdev = wil->wdev;
121
122         might_sleep();
123         if (bssid) {
124                 cid = wil_find_cid(wil, bssid);
125                 wil_dbg_misc(wil, "%s(%pM, CID %d)\n", __func__, bssid, cid);
126         } else {
127                 wil_dbg_misc(wil, "%s(all)\n", __func__);
128         }
129
130         if (cid >= 0) /* disconnect 1 peer */
131                 wil_disconnect_cid(wil, cid);
132         else /* disconnect all */
133                 for (cid = 0; cid < WIL6210_MAX_CID; cid++)
134                         wil_disconnect_cid(wil, cid);
135
136         /* link state */
137         switch (wdev->iftype) {
138         case NL80211_IFTYPE_STATION:
139         case NL80211_IFTYPE_P2P_CLIENT:
140                 wil_link_off(wil);
141                 if (test_bit(wil_status_fwconnected, &wil->status)) {
142                         clear_bit(wil_status_fwconnected, &wil->status);
143                         cfg80211_disconnected(ndev,
144                                               WLAN_STATUS_UNSPECIFIED_FAILURE,
145                                               NULL, 0, GFP_KERNEL);
146                 } else if (test_bit(wil_status_fwconnecting, &wil->status)) {
147                         cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
148                                                 WLAN_STATUS_UNSPECIFIED_FAILURE,
149                                                 GFP_KERNEL);
150                 }
151                 clear_bit(wil_status_fwconnecting, &wil->status);
152                 break;
153         default:
154                 break;
155         }
156 }
157
158 static void wil_disconnect_worker(struct work_struct *work)
159 {
160         struct wil6210_priv *wil = container_of(work,
161                         struct wil6210_priv, disconnect_worker);
162
163         mutex_lock(&wil->mutex);
164         _wil6210_disconnect(wil, NULL);
165         mutex_unlock(&wil->mutex);
166 }
167
168 static void wil_connect_timer_fn(ulong x)
169 {
170         struct wil6210_priv *wil = (void *)x;
171
172         wil_dbg_misc(wil, "Connect timeout\n");
173
174         /* reschedule to thread context - disconnect won't
175          * run from atomic context
176          */
177         schedule_work(&wil->disconnect_worker);
178 }
179
180 static void wil_scan_timer_fn(ulong x)
181 {
182         struct wil6210_priv *wil = (void *)x;
183
184         clear_bit(wil_status_fwready, &wil->status);
185         wil_err(wil, "Scan timeout detected, start fw error recovery\n");
186         schedule_work(&wil->fw_error_worker);
187 }
188
189 static void wil_fw_error_worker(struct work_struct *work)
190 {
191         struct wil6210_priv *wil = container_of(work,
192                         struct wil6210_priv, fw_error_worker);
193         struct wireless_dev *wdev = wil->wdev;
194
195         wil_dbg_misc(wil, "fw error worker\n");
196
197         if (no_fw_recovery)
198                 return;
199
200         /* increment @recovery_count if less then WIL6210_FW_RECOVERY_TO
201          * passed since last recovery attempt
202          */
203         if (time_is_after_jiffies(wil->last_fw_recovery +
204                                   WIL6210_FW_RECOVERY_TO))
205                 wil->recovery_count++;
206         else
207                 wil->recovery_count = 1; /* fw was alive for a long time */
208
209         if (wil->recovery_count > WIL6210_FW_RECOVERY_RETRIES) {
210                 wil_err(wil, "too many recovery attempts (%d), giving up\n",
211                         wil->recovery_count);
212                 return;
213         }
214
215         wil->last_fw_recovery = jiffies;
216
217         mutex_lock(&wil->mutex);
218         switch (wdev->iftype) {
219         case NL80211_IFTYPE_STATION:
220         case NL80211_IFTYPE_P2P_CLIENT:
221         case NL80211_IFTYPE_MONITOR:
222                 wil_info(wil, "fw error recovery started (try %d)...\n",
223                          wil->recovery_count);
224                 __wil_down(wil);
225                 __wil_up(wil);
226                 break;
227         case NL80211_IFTYPE_AP:
228         case NL80211_IFTYPE_P2P_GO:
229                 /* recovery in these modes is done by upper layers */
230                 break;
231         default:
232                 break;
233         }
234         mutex_unlock(&wil->mutex);
235 }
236
237 static int wil_find_free_vring(struct wil6210_priv *wil)
238 {
239         int i;
240
241         for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
242                 if (!wil->vring_tx[i].va)
243                         return i;
244         }
245         return -EINVAL;
246 }
247
248 static void wil_connect_worker(struct work_struct *work)
249 {
250         int rc;
251         struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
252                                                 connect_worker);
253         int cid = wil->pending_connect_cid;
254         int ringid = wil_find_free_vring(wil);
255
256         if (cid < 0) {
257                 wil_err(wil, "No connection pending\n");
258                 return;
259         }
260
261         wil_dbg_wmi(wil, "Configure for connection CID %d\n", cid);
262
263         rc = wil_vring_init_tx(wil, ringid, WIL6210_TX_RING_SIZE, cid, 0);
264         wil->pending_connect_cid = -1;
265         if (rc == 0) {
266                 wil->sta[cid].status = wil_sta_connected;
267                 wil_link_on(wil);
268         } else {
269                 wil->sta[cid].status = wil_sta_unused;
270         }
271 }
272
273 int wil_priv_init(struct wil6210_priv *wil)
274 {
275         uint i;
276
277         wil_dbg_misc(wil, "%s()\n", __func__);
278
279         memset(wil->sta, 0, sizeof(wil->sta));
280         for (i = 0; i < WIL6210_MAX_CID; i++)
281                 spin_lock_init(&wil->sta[i].tid_rx_lock);
282
283         mutex_init(&wil->mutex);
284         mutex_init(&wil->wmi_mutex);
285
286         init_completion(&wil->wmi_ready);
287         init_completion(&wil->wmi_call);
288
289         wil->pending_connect_cid = -1;
290         setup_timer(&wil->connect_timer, wil_connect_timer_fn, (ulong)wil);
291         setup_timer(&wil->scan_timer, wil_scan_timer_fn, (ulong)wil);
292
293         INIT_WORK(&wil->connect_worker, wil_connect_worker);
294         INIT_WORK(&wil->disconnect_worker, wil_disconnect_worker);
295         INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
296         INIT_WORK(&wil->fw_error_worker, wil_fw_error_worker);
297
298         INIT_LIST_HEAD(&wil->pending_wmi_ev);
299         spin_lock_init(&wil->wmi_ev_lock);
300
301         wil->wmi_wq = create_singlethread_workqueue(WIL_NAME"_wmi");
302         if (!wil->wmi_wq)
303                 return -EAGAIN;
304
305         wil->wmi_wq_conn = create_singlethread_workqueue(WIL_NAME"_connect");
306         if (!wil->wmi_wq_conn) {
307                 destroy_workqueue(wil->wmi_wq);
308                 return -EAGAIN;
309         }
310
311         wil->last_fw_recovery = jiffies;
312
313         return 0;
314 }
315
316 void wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid)
317 {
318         wil_dbg_misc(wil, "%s()\n", __func__);
319
320         del_timer_sync(&wil->connect_timer);
321         _wil6210_disconnect(wil, bssid);
322 }
323
324 void wil_priv_deinit(struct wil6210_priv *wil)
325 {
326         wil_dbg_misc(wil, "%s()\n", __func__);
327
328         del_timer_sync(&wil->scan_timer);
329         cancel_work_sync(&wil->disconnect_worker);
330         cancel_work_sync(&wil->fw_error_worker);
331         mutex_lock(&wil->mutex);
332         wil6210_disconnect(wil, NULL);
333         mutex_unlock(&wil->mutex);
334         wmi_event_flush(wil);
335         destroy_workqueue(wil->wmi_wq_conn);
336         destroy_workqueue(wil->wmi_wq);
337 }
338
339 /* target operations */
340 /* register read */
341 #define R(a) ioread32(wil->csr + HOSTADDR(a))
342 /* register write. wmb() to make sure it is completed */
343 #define W(a, v) do { iowrite32(v, wil->csr + HOSTADDR(a)); wmb(); } while (0)
344 /* register set = read, OR, write */
345 #define S(a, v) W(a, R(a) | v)
346 /* register clear = read, AND with inverted, write */
347 #define C(a, v) W(a, R(a) & ~v)
348
349 static inline void wil_halt_cpu(struct wil6210_priv *wil)
350 {
351         W(RGF_USER_USER_CPU_0, BIT_USER_USER_CPU_MAN_RST);
352         W(RGF_USER_MAC_CPU_0,  BIT_USER_MAC_CPU_MAN_RST);
353 }
354
355 static inline void wil_release_cpu(struct wil6210_priv *wil)
356 {
357         /* Start CPU */
358         W(RGF_USER_USER_CPU_0, 1);
359 }
360
361 static int wil_target_reset(struct wil6210_priv *wil)
362 {
363         int delay = 0;
364         u32 hw_state;
365         u32 rev_id;
366         bool is_sparrow = (wil->board->board == WIL_BOARD_SPARROW);
367
368         wil_dbg_misc(wil, "Resetting \"%s\"...\n", wil->board->name);
369
370         wil->hw_version = R(RGF_USER_FW_REV_ID);
371         rev_id = wil->hw_version & 0xff;
372
373         /* Clear MAC link up */
374         S(RGF_HP_CTRL, BIT(15));
375         S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_HPAL_PERST_FROM_PAD);
376         S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_CAR_PERST_RST);
377
378         wil_halt_cpu(wil);
379         C(RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_CAR_AHB_SW_SEL); /* 40 MHz */
380
381         if (is_sparrow) {
382                 W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x3ff81f);
383                 W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0xf);
384         }
385
386         W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
387         W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
388         W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, is_sparrow ? 0x000000f0 : 0x00000170);
389         W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FE00);
390
391         if (is_sparrow) {
392                 W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x0);
393                 W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0x0);
394         }
395
396         W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
397         W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
398         W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
399         W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
400
401         if (is_sparrow) {
402                 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000003);
403                 /* reset A2 PCIE AHB */
404                 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000);
405         } else {
406                 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000001);
407                 if (rev_id == 1) {
408                         /* reset A1 BOTH PCIE AHB & PCIE RGF */
409                         W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00000080);
410                 } else {
411                         W(RGF_PCIE_LOS_COUNTER_CTL, BIT(6) | BIT(8));
412                         W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000);
413                 }
414         }
415
416         /* TODO: check order here!!! Erez code is different */
417         W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
418
419         /* wait until device ready. typical time is 200..250 msec */
420         do {
421                 msleep(RST_DELAY);
422                 hw_state = R(RGF_USER_HW_MACHINE_STATE);
423                 if (delay++ > RST_COUNT) {
424                         wil_err(wil, "Reset not completed, hw_state 0x%08x\n",
425                                 hw_state);
426                         return -ETIME;
427                 }
428         } while (hw_state != HW_MACHINE_BOOT_DONE);
429
430         /* TODO: Erez check rev_id != 1 */
431         if (!is_sparrow && (rev_id != 1))
432                 W(RGF_PCIE_LOS_COUNTER_CTL, BIT(8));
433
434         C(RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
435
436         wil_dbg_misc(wil, "Reset completed in %d ms\n", delay * RST_DELAY);
437         return 0;
438 }
439
440 #undef R
441 #undef W
442 #undef S
443 #undef C
444
445 void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
446 {
447         le32_to_cpus(&r->base);
448         le16_to_cpus(&r->entry_size);
449         le16_to_cpus(&r->size);
450         le32_to_cpus(&r->tail);
451         le32_to_cpus(&r->head);
452 }
453
454 static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
455 {
456         ulong to = msecs_to_jiffies(1000);
457         ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
458
459         if (0 == left) {
460                 wil_err(wil, "Firmware not ready\n");
461                 return -ETIME;
462         } else {
463                 wil_info(wil, "FW ready after %d ms. HW version 0x%08x\n",
464                          jiffies_to_msecs(to-left), wil->hw_version);
465         }
466         return 0;
467 }
468
469 /*
470  * We reset all the structures, and we reset the UMAC.
471  * After calling this routine, you're expected to reload
472  * the firmware.
473  */
474 int wil_reset(struct wil6210_priv *wil)
475 {
476         int rc;
477
478         wil_dbg_misc(wil, "%s()\n", __func__);
479
480         WARN_ON(!mutex_is_locked(&wil->mutex));
481         WARN_ON(test_bit(wil_status_napi_en, &wil->status));
482
483         cancel_work_sync(&wil->disconnect_worker);
484         wil6210_disconnect(wil, NULL);
485
486         wil->status = 0; /* prevent NAPI from being scheduled */
487
488         if (wil->scan_request) {
489                 wil_dbg_misc(wil, "Abort scan_request 0x%p\n",
490                              wil->scan_request);
491                 del_timer_sync(&wil->scan_timer);
492                 cfg80211_scan_done(wil->scan_request, true);
493                 wil->scan_request = NULL;
494         }
495
496         wil_mask_irq(wil);
497
498         wmi_event_flush(wil);
499
500         flush_workqueue(wil->wmi_wq_conn);
501         flush_workqueue(wil->wmi_wq);
502
503         rc = wil_target_reset(wil);
504         wil_rx_fini(wil);
505         if (rc)
506                 return rc;
507
508         if (!no_fw_load) {
509                 wil_info(wil, "Use firmware <%s>\n", WIL_FW_NAME);
510                 wil_halt_cpu(wil);
511                 /* Loading f/w from the file */
512                 rc = wil_request_firmware(wil, WIL_FW_NAME);
513                 if (rc)
514                         return rc;
515
516                 /* clear any interrupts which on-card-firmware may have set */
517                 wil6210_clear_irq(wil);
518                 { /* CAF_ICR - clear and mask */
519                         u32 a = HOSTADDR(RGF_CAF_ICR) +
520                                 offsetof(struct RGF_ICR, ICR);
521                         u32 m = HOSTADDR(RGF_CAF_ICR) +
522                                 offsetof(struct RGF_ICR, IMV);
523                         u32 icr = ioread32(wil->csr + a);
524
525                         iowrite32(icr, wil->csr + a); /* W1C */
526                         iowrite32(~0, wil->csr + m);
527                         wmb(); /* wait for completion */
528                 }
529                 wil_release_cpu(wil);
530         } else {
531                 wil_info(wil, "Use firmware from on-card flash\n");
532         }
533
534         /* init after reset */
535         wil->pending_connect_cid = -1;
536         reinit_completion(&wil->wmi_ready);
537         reinit_completion(&wil->wmi_call);
538
539         wil_unmask_irq(wil);
540
541         /* we just started MAC, wait for FW ready */
542         rc = wil_wait_for_fw_ready(wil);
543
544         return rc;
545 }
546
547 void wil_fw_error_recovery(struct wil6210_priv *wil)
548 {
549         wil_dbg_misc(wil, "starting fw error recovery\n");
550         schedule_work(&wil->fw_error_worker);
551 }
552
553 void wil_link_on(struct wil6210_priv *wil)
554 {
555         struct net_device *ndev = wil_to_ndev(wil);
556
557         wil_dbg_misc(wil, "%s()\n", __func__);
558
559         netif_carrier_on(ndev);
560         wil_dbg_misc(wil, "netif_tx_wake : link on\n");
561         netif_tx_wake_all_queues(ndev);
562 }
563
564 void wil_link_off(struct wil6210_priv *wil)
565 {
566         struct net_device *ndev = wil_to_ndev(wil);
567
568         wil_dbg_misc(wil, "%s()\n", __func__);
569
570         netif_tx_stop_all_queues(ndev);
571         wil_dbg_misc(wil, "netif_tx_stop : link off\n");
572         netif_carrier_off(ndev);
573 }
574
575 int __wil_up(struct wil6210_priv *wil)
576 {
577         struct net_device *ndev = wil_to_ndev(wil);
578         struct wireless_dev *wdev = wil->wdev;
579         int rc;
580
581         WARN_ON(!mutex_is_locked(&wil->mutex));
582
583         rc = wil_reset(wil);
584         if (rc)
585                 return rc;
586
587         /* Rx VRING. After MAC and beacon */
588         rc = wil_rx_init(wil);
589         if (rc)
590                 return rc;
591
592         switch (wdev->iftype) {
593         case NL80211_IFTYPE_STATION:
594                 wil_dbg_misc(wil, "type: STATION\n");
595                 ndev->type = ARPHRD_ETHER;
596                 break;
597         case NL80211_IFTYPE_AP:
598                 wil_dbg_misc(wil, "type: AP\n");
599                 ndev->type = ARPHRD_ETHER;
600                 break;
601         case NL80211_IFTYPE_P2P_CLIENT:
602                 wil_dbg_misc(wil, "type: P2P_CLIENT\n");
603                 ndev->type = ARPHRD_ETHER;
604                 break;
605         case NL80211_IFTYPE_P2P_GO:
606                 wil_dbg_misc(wil, "type: P2P_GO\n");
607                 ndev->type = ARPHRD_ETHER;
608                 break;
609         case NL80211_IFTYPE_MONITOR:
610                 wil_dbg_misc(wil, "type: Monitor\n");
611                 ndev->type = ARPHRD_IEEE80211_RADIOTAP;
612                 /* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
613                 break;
614         default:
615                 return -EOPNOTSUPP;
616         }
617
618         /* MAC address - pre-requisite for other commands */
619         wmi_set_mac_address(wil, ndev->dev_addr);
620
621         wil_dbg_misc(wil, "NAPI enable\n");
622         napi_enable(&wil->napi_rx);
623         napi_enable(&wil->napi_tx);
624         set_bit(wil_status_napi_en, &wil->status);
625
626         if (wil->platform_ops.bus_request)
627                 wil->platform_ops.bus_request(wil->platform_handle,
628                                               WIL_MAX_BUS_REQUEST_KBPS);
629
630         return 0;
631 }
632
633 int wil_up(struct wil6210_priv *wil)
634 {
635         int rc;
636
637         wil_dbg_misc(wil, "%s()\n", __func__);
638
639         mutex_lock(&wil->mutex);
640         rc = __wil_up(wil);
641         mutex_unlock(&wil->mutex);
642
643         return rc;
644 }
645
646 int __wil_down(struct wil6210_priv *wil)
647 {
648         int iter = WAIT_FOR_DISCONNECT_TIMEOUT_MS /
649                         WAIT_FOR_DISCONNECT_INTERVAL_MS;
650
651         WARN_ON(!mutex_is_locked(&wil->mutex));
652
653         if (wil->platform_ops.bus_request)
654                 wil->platform_ops.bus_request(wil->platform_handle, 0);
655
656         wil_disable_irq(wil);
657         if (test_and_clear_bit(wil_status_napi_en, &wil->status)) {
658                 napi_disable(&wil->napi_rx);
659                 napi_disable(&wil->napi_tx);
660                 wil_dbg_misc(wil, "NAPI disable\n");
661         }
662         wil_enable_irq(wil);
663
664         if (wil->scan_request) {
665                 wil_dbg_misc(wil, "Abort scan_request 0x%p\n",
666                              wil->scan_request);
667                 del_timer_sync(&wil->scan_timer);
668                 cfg80211_scan_done(wil->scan_request, true);
669                 wil->scan_request = NULL;
670         }
671
672         if (test_bit(wil_status_fwconnected, &wil->status) ||
673             test_bit(wil_status_fwconnecting, &wil->status))
674                 wmi_send(wil, WMI_DISCONNECT_CMDID, NULL, 0);
675
676         /* make sure wil is idle (not connected) */
677         mutex_unlock(&wil->mutex);
678         while (iter--) {
679                 int idle = !test_bit(wil_status_fwconnected, &wil->status) &&
680                            !test_bit(wil_status_fwconnecting, &wil->status);
681                 if (idle)
682                         break;
683                 msleep(WAIT_FOR_DISCONNECT_INTERVAL_MS);
684         }
685         mutex_lock(&wil->mutex);
686
687         if (!iter)
688                 wil_err(wil, "timeout waiting for idle FW/HW\n");
689
690         wil_rx_fini(wil);
691
692         return 0;
693 }
694
695 int wil_down(struct wil6210_priv *wil)
696 {
697         int rc;
698
699         wil_dbg_misc(wil, "%s()\n", __func__);
700
701         mutex_lock(&wil->mutex);
702         rc = __wil_down(wil);
703         mutex_unlock(&wil->mutex);
704
705         return rc;
706 }
707
708 int wil_find_cid(struct wil6210_priv *wil, const u8 *mac)
709 {
710         int i;
711         int rc = -ENOENT;
712
713         for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
714                 if ((wil->sta[i].status != wil_sta_unused) &&
715                     ether_addr_equal(wil->sta[i].addr, mac)) {
716                         rc = i;
717                         break;
718                 }
719         }
720
721         return rc;
722 }