Linux-libre 4.19.123-gnu
[librecmc/linux-libre.git] / drivers / net / wireless / ath / wil6210 / main.c
1 /*
2  * Copyright (c) 2012-2017 Qualcomm Atheros, Inc.
3  * Copyright (c) 2018, The Linux Foundation. All rights reserved.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 #include <linux/moduleparam.h>
19 #include <linux/if_arp.h>
20 #include <linux/etherdevice.h>
21
22 #include "wil6210.h"
23 #include "txrx.h"
24 #include "txrx_edma.h"
25 #include "wmi.h"
26 #include "boot_loader.h"
27
28 #define WAIT_FOR_HALP_VOTE_MS 100
29 #define WAIT_FOR_SCAN_ABORT_MS 1000
30 #define WIL_DEFAULT_NUM_RX_STATUS_RINGS 1
31 #define WIL_BOARD_FILE_MAX_NAMELEN 128
32
33 bool debug_fw; /* = false; */
34 module_param(debug_fw, bool, 0444);
35 MODULE_PARM_DESC(debug_fw, " do not perform card reset. For FW debug");
36
37 static u8 oob_mode;
38 module_param(oob_mode, byte, 0444);
39 MODULE_PARM_DESC(oob_mode,
40                  " enable out of the box (OOB) mode in FW, for diagnostics and certification");
41
42 bool no_fw_recovery;
43 module_param(no_fw_recovery, bool, 0644);
44 MODULE_PARM_DESC(no_fw_recovery, " disable automatic FW error recovery");
45
46 /* if not set via modparam, will be set to default value of 1/8 of
47  * rx ring size during init flow
48  */
49 unsigned short rx_ring_overflow_thrsh = WIL6210_RX_HIGH_TRSH_INIT;
50 module_param(rx_ring_overflow_thrsh, ushort, 0444);
51 MODULE_PARM_DESC(rx_ring_overflow_thrsh,
52                  " RX ring overflow threshold in descriptors.");
53
54 /* We allow allocation of more than 1 page buffers to support large packets.
55  * It is suboptimal behavior performance wise in case MTU above page size.
56  */
57 unsigned int mtu_max = TXRX_BUF_LEN_DEFAULT - WIL_MAX_MPDU_OVERHEAD;
58 static int mtu_max_set(const char *val, const struct kernel_param *kp)
59 {
60         int ret;
61
62         /* sets mtu_max directly. no need to restore it in case of
63          * illegal value since we assume this will fail insmod
64          */
65         ret = param_set_uint(val, kp);
66         if (ret)
67                 return ret;
68
69         if (mtu_max < 68 || mtu_max > WIL_MAX_ETH_MTU)
70                 ret = -EINVAL;
71
72         return ret;
73 }
74
75 static const struct kernel_param_ops mtu_max_ops = {
76         .set = mtu_max_set,
77         .get = param_get_uint,
78 };
79
80 module_param_cb(mtu_max, &mtu_max_ops, &mtu_max, 0444);
81 MODULE_PARM_DESC(mtu_max, " Max MTU value.");
82
83 static uint rx_ring_order = WIL_RX_RING_SIZE_ORDER_DEFAULT;
84 static uint tx_ring_order = WIL_TX_RING_SIZE_ORDER_DEFAULT;
85 static uint bcast_ring_order = WIL_BCAST_RING_SIZE_ORDER_DEFAULT;
86
87 static int ring_order_set(const char *val, const struct kernel_param *kp)
88 {
89         int ret;
90         uint x;
91
92         ret = kstrtouint(val, 0, &x);
93         if (ret)
94                 return ret;
95
96         if ((x < WIL_RING_SIZE_ORDER_MIN) || (x > WIL_RING_SIZE_ORDER_MAX))
97                 return -EINVAL;
98
99         *((uint *)kp->arg) = x;
100
101         return 0;
102 }
103
104 static const struct kernel_param_ops ring_order_ops = {
105         .set = ring_order_set,
106         .get = param_get_uint,
107 };
108
109 module_param_cb(rx_ring_order, &ring_order_ops, &rx_ring_order, 0444);
110 MODULE_PARM_DESC(rx_ring_order, " Rx ring order; size = 1 << order");
111 module_param_cb(tx_ring_order, &ring_order_ops, &tx_ring_order, 0444);
112 MODULE_PARM_DESC(tx_ring_order, " Tx ring order; size = 1 << order");
113 module_param_cb(bcast_ring_order, &ring_order_ops, &bcast_ring_order, 0444);
114 MODULE_PARM_DESC(bcast_ring_order, " Bcast ring order; size = 1 << order");
115
116 enum {
117         WIL_BOOT_ERR,
118         WIL_BOOT_VANILLA,
119         WIL_BOOT_PRODUCTION,
120         WIL_BOOT_DEVELOPMENT,
121 };
122
123 enum {
124         WIL_SIG_STATUS_VANILLA = 0x0,
125         WIL_SIG_STATUS_DEVELOPMENT = 0x1,
126         WIL_SIG_STATUS_PRODUCTION = 0x2,
127         WIL_SIG_STATUS_CORRUPTED_PRODUCTION = 0x3,
128 };
129
130 #define RST_DELAY (20) /* msec, for loop in @wil_wait_device_ready */
131 #define RST_COUNT (1 + 1000/RST_DELAY) /* round up to be above 1 sec total */
132
133 #define PMU_READY_DELAY_MS (4) /* ms, for sleep in @wil_wait_device_ready */
134
135 #define OTP_HW_DELAY (200) /* usec, loop in @wil_wait_device_ready_talyn_mb */
136 /* round up to be above 2 ms total */
137 #define OTP_HW_COUNT (1 + 2000 / OTP_HW_DELAY)
138
139 /*
140  * Due to a hardware issue,
141  * one has to read/write to/from NIC in 32-bit chunks;
142  * regular memcpy_fromio and siblings will
143  * not work on 64-bit platform - it uses 64-bit transactions
144  *
145  * Force 32-bit transactions to enable NIC on 64-bit platforms
146  *
147  * To avoid byte swap on big endian host, __raw_{read|write}l
148  * should be used - {read|write}l would swap bytes to provide
149  * little endian on PCI value in host endianness.
150  */
151 void wil_memcpy_fromio_32(void *dst, const volatile void __iomem *src,
152                           size_t count)
153 {
154         u32 *d = dst;
155         const volatile u32 __iomem *s = src;
156
157         for (; count >= 4; count -= 4)
158                 *d++ = __raw_readl(s++);
159
160         if (unlikely(count)) {
161                 /* count can be 1..3 */
162                 u32 tmp = __raw_readl(s);
163
164                 memcpy(d, &tmp, count);
165         }
166 }
167
168 void wil_memcpy_toio_32(volatile void __iomem *dst, const void *src,
169                         size_t count)
170 {
171         volatile u32 __iomem *d = dst;
172         const u32 *s = src;
173
174         for (; count >= 4; count -= 4)
175                 __raw_writel(*s++, d++);
176
177         if (unlikely(count)) {
178                 /* count can be 1..3 */
179                 u32 tmp = 0;
180
181                 memcpy(&tmp, s, count);
182                 __raw_writel(tmp, d);
183         }
184 }
185
186 static void wil_ring_fini_tx(struct wil6210_priv *wil, int id)
187 {
188         struct wil_ring *ring = &wil->ring_tx[id];
189         struct wil_ring_tx_data *txdata = &wil->ring_tx_data[id];
190
191         lockdep_assert_held(&wil->mutex);
192
193         if (!ring->va)
194                 return;
195
196         wil_dbg_misc(wil, "vring_fini_tx: id=%d\n", id);
197
198         spin_lock_bh(&txdata->lock);
199         txdata->dot1x_open = false;
200         txdata->mid = U8_MAX;
201         txdata->enabled = 0; /* no Tx can be in progress or start anew */
202         spin_unlock_bh(&txdata->lock);
203         /* napi_synchronize waits for completion of the current NAPI but will
204          * not prevent the next NAPI run.
205          * Add a memory barrier to guarantee that txdata->enabled is zeroed
206          * before napi_synchronize so that the next scheduled NAPI will not
207          * handle this vring
208          */
209         wmb();
210         /* make sure NAPI won't touch this vring */
211         if (test_bit(wil_status_napi_en, wil->status))
212                 napi_synchronize(&wil->napi_tx);
213
214         wil->txrx_ops.ring_fini_tx(wil, ring);
215 }
216
217 static void wil_disconnect_cid(struct wil6210_vif *vif, int cid,
218                                u16 reason_code, bool from_event)
219 __acquires(&sta->tid_rx_lock) __releases(&sta->tid_rx_lock)
220 {
221         uint i;
222         struct wil6210_priv *wil = vif_to_wil(vif);
223         struct net_device *ndev = vif_to_ndev(vif);
224         struct wireless_dev *wdev = vif_to_wdev(vif);
225         struct wil_sta_info *sta = &wil->sta[cid];
226         int min_ring_id = wil_get_min_tx_ring_id(wil);
227
228         might_sleep();
229         wil_dbg_misc(wil, "disconnect_cid: CID %d, MID %d, status %d\n",
230                      cid, sta->mid, sta->status);
231         /* inform upper/lower layers */
232         if (sta->status != wil_sta_unused) {
233                 if (vif->mid != sta->mid) {
234                         wil_err(wil, "STA MID mismatch with VIF MID(%d)\n",
235                                 vif->mid);
236                         /* let FW override sta->mid but be more strict with
237                          * user space requests
238                          */
239                         if (!from_event)
240                                 return;
241                 }
242                 if (!from_event) {
243                         bool del_sta = (wdev->iftype == NL80211_IFTYPE_AP) ?
244                                                 disable_ap_sme : false;
245                         wmi_disconnect_sta(vif, sta->addr, reason_code,
246                                            true, del_sta);
247                 }
248
249                 switch (wdev->iftype) {
250                 case NL80211_IFTYPE_AP:
251                 case NL80211_IFTYPE_P2P_GO:
252                         /* AP-like interface */
253                         cfg80211_del_sta(ndev, sta->addr, GFP_KERNEL);
254                         break;
255                 default:
256                         break;
257                 }
258                 sta->status = wil_sta_unused;
259                 sta->mid = U8_MAX;
260         }
261         /* reorder buffers */
262         for (i = 0; i < WIL_STA_TID_NUM; i++) {
263                 struct wil_tid_ampdu_rx *r;
264
265                 spin_lock_bh(&sta->tid_rx_lock);
266
267                 r = sta->tid_rx[i];
268                 sta->tid_rx[i] = NULL;
269                 wil_tid_ampdu_rx_free(wil, r);
270
271                 spin_unlock_bh(&sta->tid_rx_lock);
272         }
273         /* crypto context */
274         memset(sta->tid_crypto_rx, 0, sizeof(sta->tid_crypto_rx));
275         memset(&sta->group_crypto_rx, 0, sizeof(sta->group_crypto_rx));
276         /* release vrings */
277         for (i = min_ring_id; i < ARRAY_SIZE(wil->ring_tx); i++) {
278                 if (wil->ring2cid_tid[i][0] == cid)
279                         wil_ring_fini_tx(wil, i);
280         }
281         /* statistics */
282         memset(&sta->stats, 0, sizeof(sta->stats));
283         sta->stats.tx_latency_min_us = U32_MAX;
284 }
285
286 static bool wil_vif_is_connected(struct wil6210_priv *wil, u8 mid)
287 {
288         int i;
289
290         for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
291                 if (wil->sta[i].mid == mid &&
292                     wil->sta[i].status == wil_sta_connected)
293                         return true;
294         }
295
296         return false;
297 }
298
299 static void _wil6210_disconnect(struct wil6210_vif *vif, const u8 *bssid,
300                                 u16 reason_code, bool from_event)
301 {
302         struct wil6210_priv *wil = vif_to_wil(vif);
303         int cid = -ENOENT;
304         struct net_device *ndev;
305         struct wireless_dev *wdev;
306
307         if (unlikely(!vif))
308                 return;
309
310         ndev = vif_to_ndev(vif);
311         wdev = vif_to_wdev(vif);
312
313         might_sleep();
314         wil_info(wil, "bssid=%pM, reason=%d, ev%s\n", bssid,
315                  reason_code, from_event ? "+" : "-");
316
317         /* Cases are:
318          * - disconnect single STA, still connected
319          * - disconnect single STA, already disconnected
320          * - disconnect all
321          *
322          * For "disconnect all", there are 3 options:
323          * - bssid == NULL
324          * - bssid is broadcast address (ff:ff:ff:ff:ff:ff)
325          * - bssid is our MAC address
326          */
327         if (bssid && !is_broadcast_ether_addr(bssid) &&
328             !ether_addr_equal_unaligned(ndev->dev_addr, bssid)) {
329                 cid = wil_find_cid(wil, vif->mid, bssid);
330                 wil_dbg_misc(wil, "Disconnect %pM, CID=%d, reason=%d\n",
331                              bssid, cid, reason_code);
332                 if (cid >= 0) /* disconnect 1 peer */
333                         wil_disconnect_cid(vif, cid, reason_code, from_event);
334         } else { /* all */
335                 wil_dbg_misc(wil, "Disconnect all\n");
336                 for (cid = 0; cid < WIL6210_MAX_CID; cid++)
337                         wil_disconnect_cid(vif, cid, reason_code, from_event);
338         }
339
340         /* link state */
341         switch (wdev->iftype) {
342         case NL80211_IFTYPE_STATION:
343         case NL80211_IFTYPE_P2P_CLIENT:
344                 wil_bcast_fini(vif);
345                 wil_update_net_queues_bh(wil, vif, NULL, true);
346                 netif_carrier_off(ndev);
347                 if (!wil_has_other_active_ifaces(wil, ndev, false, true))
348                         wil6210_bus_request(wil, WIL_DEFAULT_BUS_REQUEST_KBPS);
349
350                 if (test_and_clear_bit(wil_vif_fwconnected, vif->status)) {
351                         atomic_dec(&wil->connected_vifs);
352                         cfg80211_disconnected(ndev, reason_code,
353                                               NULL, 0,
354                                               vif->locally_generated_disc,
355                                               GFP_KERNEL);
356                         vif->locally_generated_disc = false;
357                 } else if (test_bit(wil_vif_fwconnecting, vif->status)) {
358                         cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
359                                                 WLAN_STATUS_UNSPECIFIED_FAILURE,
360                                                 GFP_KERNEL);
361                         vif->bss = NULL;
362                 }
363                 clear_bit(wil_vif_fwconnecting, vif->status);
364                 break;
365         case NL80211_IFTYPE_AP:
366         case NL80211_IFTYPE_P2P_GO:
367                 if (!wil_vif_is_connected(wil, vif->mid)) {
368                         wil_update_net_queues_bh(wil, vif, NULL, true);
369                         if (test_and_clear_bit(wil_vif_fwconnected,
370                                                vif->status))
371                                 atomic_dec(&wil->connected_vifs);
372                 } else {
373                         wil_update_net_queues_bh(wil, vif, NULL, false);
374                 }
375                 break;
376         default:
377                 break;
378         }
379 }
380
381 void wil_disconnect_worker(struct work_struct *work)
382 {
383         struct wil6210_vif *vif = container_of(work,
384                         struct wil6210_vif, disconnect_worker);
385         struct wil6210_priv *wil = vif_to_wil(vif);
386         struct net_device *ndev = vif_to_ndev(vif);
387         int rc;
388         struct {
389                 struct wmi_cmd_hdr wmi;
390                 struct wmi_disconnect_event evt;
391         } __packed reply;
392
393         if (test_bit(wil_vif_fwconnected, vif->status))
394                 /* connect succeeded after all */
395                 return;
396
397         if (!test_bit(wil_vif_fwconnecting, vif->status))
398                 /* already disconnected */
399                 return;
400
401         memset(&reply, 0, sizeof(reply));
402
403         rc = wmi_call(wil, WMI_DISCONNECT_CMDID, vif->mid, NULL, 0,
404                       WMI_DISCONNECT_EVENTID, &reply, sizeof(reply),
405                       WIL6210_DISCONNECT_TO_MS);
406         if (rc) {
407                 wil_err(wil, "disconnect error %d\n", rc);
408                 return;
409         }
410
411         wil_update_net_queues_bh(wil, vif, NULL, true);
412         netif_carrier_off(ndev);
413         cfg80211_connect_result(ndev, NULL, NULL, 0, NULL, 0,
414                                 WLAN_STATUS_UNSPECIFIED_FAILURE, GFP_KERNEL);
415         clear_bit(wil_vif_fwconnecting, vif->status);
416 }
417
418 static int wil_wait_for_recovery(struct wil6210_priv *wil)
419 {
420         if (wait_event_interruptible(wil->wq, wil->recovery_state !=
421                                      fw_recovery_pending)) {
422                 wil_err(wil, "Interrupt, canceling recovery\n");
423                 return -ERESTARTSYS;
424         }
425         if (wil->recovery_state != fw_recovery_running) {
426                 wil_info(wil, "Recovery cancelled\n");
427                 return -EINTR;
428         }
429         wil_info(wil, "Proceed with recovery\n");
430         return 0;
431 }
432
433 void wil_set_recovery_state(struct wil6210_priv *wil, int state)
434 {
435         wil_dbg_misc(wil, "set_recovery_state: %d -> %d\n",
436                      wil->recovery_state, state);
437
438         wil->recovery_state = state;
439         wake_up_interruptible(&wil->wq);
440 }
441
442 bool wil_is_recovery_blocked(struct wil6210_priv *wil)
443 {
444         return no_fw_recovery && (wil->recovery_state == fw_recovery_pending);
445 }
446
447 static void wil_fw_error_worker(struct work_struct *work)
448 {
449         struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
450                                                 fw_error_worker);
451         struct net_device *ndev = wil->main_ndev;
452         struct wireless_dev *wdev;
453
454         wil_dbg_misc(wil, "fw error worker\n");
455
456         if (!ndev || !(ndev->flags & IFF_UP)) {
457                 wil_info(wil, "No recovery - interface is down\n");
458                 return;
459         }
460         wdev = ndev->ieee80211_ptr;
461
462         /* increment @recovery_count if less then WIL6210_FW_RECOVERY_TO
463          * passed since last recovery attempt
464          */
465         if (time_is_after_jiffies(wil->last_fw_recovery +
466                                   WIL6210_FW_RECOVERY_TO))
467                 wil->recovery_count++;
468         else
469                 wil->recovery_count = 1; /* fw was alive for a long time */
470
471         if (wil->recovery_count > WIL6210_FW_RECOVERY_RETRIES) {
472                 wil_err(wil, "too many recovery attempts (%d), giving up\n",
473                         wil->recovery_count);
474                 return;
475         }
476
477         wil->last_fw_recovery = jiffies;
478
479         wil_info(wil, "fw error recovery requested (try %d)...\n",
480                  wil->recovery_count);
481         if (!no_fw_recovery)
482                 wil->recovery_state = fw_recovery_running;
483         if (wil_wait_for_recovery(wil) != 0)
484                 return;
485
486         mutex_lock(&wil->mutex);
487         /* Needs adaptation for multiple VIFs
488          * need to go over all VIFs and consider the appropriate
489          * recovery.
490          */
491         switch (wdev->iftype) {
492         case NL80211_IFTYPE_STATION:
493         case NL80211_IFTYPE_P2P_CLIENT:
494         case NL80211_IFTYPE_MONITOR:
495                 /* silent recovery, upper layers will see disconnect */
496                 __wil_down(wil);
497                 __wil_up(wil);
498                 break;
499         case NL80211_IFTYPE_AP:
500         case NL80211_IFTYPE_P2P_GO:
501                 wil_info(wil, "No recovery for AP-like interface\n");
502                 /* recovery in these modes is done by upper layers */
503                 break;
504         default:
505                 wil_err(wil, "No recovery - unknown interface type %d\n",
506                         wdev->iftype);
507                 break;
508         }
509         mutex_unlock(&wil->mutex);
510 }
511
512 static int wil_find_free_ring(struct wil6210_priv *wil)
513 {
514         int i;
515         int min_ring_id = wil_get_min_tx_ring_id(wil);
516
517         for (i = min_ring_id; i < WIL6210_MAX_TX_RINGS; i++) {
518                 if (!wil->ring_tx[i].va)
519                         return i;
520         }
521         return -EINVAL;
522 }
523
524 int wil_ring_init_tx(struct wil6210_vif *vif, int cid)
525 {
526         struct wil6210_priv *wil = vif_to_wil(vif);
527         int rc = -EINVAL, ringid;
528
529         if (cid < 0) {
530                 wil_err(wil, "No connection pending\n");
531                 goto out;
532         }
533         ringid = wil_find_free_ring(wil);
534         if (ringid < 0) {
535                 wil_err(wil, "No free vring found\n");
536                 goto out;
537         }
538
539         wil_dbg_wmi(wil, "Configure for connection CID %d MID %d ring %d\n",
540                     cid, vif->mid, ringid);
541
542         rc = wil->txrx_ops.ring_init_tx(vif, ringid, 1 << tx_ring_order,
543                                         cid, 0);
544         if (rc)
545                 wil_err(wil, "init TX for CID %d MID %d vring %d failed\n",
546                         cid, vif->mid, ringid);
547
548 out:
549         return rc;
550 }
551
552 int wil_bcast_init(struct wil6210_vif *vif)
553 {
554         struct wil6210_priv *wil = vif_to_wil(vif);
555         int ri = vif->bcast_ring, rc;
556
557         if (ri >= 0 && wil->ring_tx[ri].va)
558                 return 0;
559
560         ri = wil_find_free_ring(wil);
561         if (ri < 0)
562                 return ri;
563
564         vif->bcast_ring = ri;
565         rc = wil->txrx_ops.ring_init_bcast(vif, ri, 1 << bcast_ring_order);
566         if (rc)
567                 vif->bcast_ring = -1;
568
569         return rc;
570 }
571
572 void wil_bcast_fini(struct wil6210_vif *vif)
573 {
574         struct wil6210_priv *wil = vif_to_wil(vif);
575         int ri = vif->bcast_ring;
576
577         if (ri < 0)
578                 return;
579
580         vif->bcast_ring = -1;
581         wil_ring_fini_tx(wil, ri);
582 }
583
584 void wil_bcast_fini_all(struct wil6210_priv *wil)
585 {
586         int i;
587         struct wil6210_vif *vif;
588
589         for (i = 0; i < wil->max_vifs; i++) {
590                 vif = wil->vifs[i];
591                 if (vif)
592                         wil_bcast_fini(vif);
593         }
594 }
595
596 int wil_priv_init(struct wil6210_priv *wil)
597 {
598         uint i;
599
600         wil_dbg_misc(wil, "priv_init\n");
601
602         memset(wil->sta, 0, sizeof(wil->sta));
603         for (i = 0; i < WIL6210_MAX_CID; i++) {
604                 spin_lock_init(&wil->sta[i].tid_rx_lock);
605                 wil->sta[i].mid = U8_MAX;
606         }
607
608         for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
609                 spin_lock_init(&wil->ring_tx_data[i].lock);
610                 wil->ring2cid_tid[i][0] = WIL6210_MAX_CID;
611         }
612
613         mutex_init(&wil->mutex);
614         mutex_init(&wil->vif_mutex);
615         mutex_init(&wil->wmi_mutex);
616         mutex_init(&wil->halp.lock);
617
618         init_completion(&wil->wmi_ready);
619         init_completion(&wil->wmi_call);
620         init_completion(&wil->halp.comp);
621
622         INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
623         INIT_WORK(&wil->fw_error_worker, wil_fw_error_worker);
624
625         INIT_LIST_HEAD(&wil->pending_wmi_ev);
626         spin_lock_init(&wil->wmi_ev_lock);
627         spin_lock_init(&wil->net_queue_lock);
628         init_waitqueue_head(&wil->wq);
629
630         wil->wmi_wq = create_singlethread_workqueue(WIL_NAME "_wmi");
631         if (!wil->wmi_wq)
632                 return -EAGAIN;
633
634         wil->wq_service = create_singlethread_workqueue(WIL_NAME "_service");
635         if (!wil->wq_service)
636                 goto out_wmi_wq;
637
638         wil->last_fw_recovery = jiffies;
639         wil->tx_interframe_timeout = WIL6210_ITR_TX_INTERFRAME_TIMEOUT_DEFAULT;
640         wil->rx_interframe_timeout = WIL6210_ITR_RX_INTERFRAME_TIMEOUT_DEFAULT;
641         wil->tx_max_burst_duration = WIL6210_ITR_TX_MAX_BURST_DURATION_DEFAULT;
642         wil->rx_max_burst_duration = WIL6210_ITR_RX_MAX_BURST_DURATION_DEFAULT;
643
644         if (rx_ring_overflow_thrsh == WIL6210_RX_HIGH_TRSH_INIT)
645                 rx_ring_overflow_thrsh = WIL6210_RX_HIGH_TRSH_DEFAULT;
646
647         wil->ps_profile =  WMI_PS_PROFILE_TYPE_DEFAULT;
648
649         wil->wakeup_trigger = WMI_WAKEUP_TRIGGER_UCAST |
650                               WMI_WAKEUP_TRIGGER_BCAST;
651         memset(&wil->suspend_stats, 0, sizeof(wil->suspend_stats));
652         wil->ring_idle_trsh = 16;
653
654         wil->reply_mid = U8_MAX;
655         wil->max_vifs = 1;
656
657         /* edma configuration can be updated via debugfs before allocation */
658         wil->num_rx_status_rings = WIL_DEFAULT_NUM_RX_STATUS_RINGS;
659         wil->tx_status_ring_order = WIL_TX_SRING_SIZE_ORDER_DEFAULT;
660
661         /* Rx status ring size should be bigger than the number of RX buffers
662          * in order to prevent backpressure on the status ring, which may
663          * cause HW freeze.
664          */
665         wil->rx_status_ring_order = WIL_RX_SRING_SIZE_ORDER_DEFAULT;
666         /* Number of RX buffer IDs should be bigger than the RX descriptor
667          * ring size as in HW reorder flow, the HW can consume additional
668          * buffers before releasing the previous ones.
669          */
670         wil->rx_buff_id_count = WIL_RX_BUFF_ARR_SIZE_DEFAULT;
671
672         wil->amsdu_en = 1;
673
674         return 0;
675
676 out_wmi_wq:
677         destroy_workqueue(wil->wmi_wq);
678
679         return -EAGAIN;
680 }
681
682 void wil6210_bus_request(struct wil6210_priv *wil, u32 kbps)
683 {
684         if (wil->platform_ops.bus_request) {
685                 wil->bus_request_kbps = kbps;
686                 wil->platform_ops.bus_request(wil->platform_handle, kbps);
687         }
688 }
689
690 /**
691  * wil6210_disconnect - disconnect one connection
692  * @vif: virtual interface context
693  * @bssid: peer to disconnect, NULL to disconnect all
694  * @reason_code: Reason code for the Disassociation frame
695  * @from_event: whether is invoked from FW event handler
696  *
697  * Disconnect and release associated resources. If invoked not from the
698  * FW event handler, issue WMI command(s) to trigger MAC disconnect.
699  */
700 void wil6210_disconnect(struct wil6210_vif *vif, const u8 *bssid,
701                         u16 reason_code, bool from_event)
702 {
703         struct wil6210_priv *wil = vif_to_wil(vif);
704
705         wil_dbg_misc(wil, "disconnect\n");
706
707         del_timer_sync(&vif->connect_timer);
708         _wil6210_disconnect(vif, bssid, reason_code, from_event);
709 }
710
711 void wil_priv_deinit(struct wil6210_priv *wil)
712 {
713         wil_dbg_misc(wil, "priv_deinit\n");
714
715         wil_set_recovery_state(wil, fw_recovery_idle);
716         cancel_work_sync(&wil->fw_error_worker);
717         wmi_event_flush(wil);
718         destroy_workqueue(wil->wq_service);
719         destroy_workqueue(wil->wmi_wq);
720 }
721
722 static void wil_shutdown_bl(struct wil6210_priv *wil)
723 {
724         u32 val;
725
726         wil_s(wil, RGF_USER_BL +
727               offsetof(struct bl_dedicated_registers_v1,
728                        bl_shutdown_handshake), BL_SHUTDOWN_HS_GRTD);
729
730         usleep_range(100, 150);
731
732         val = wil_r(wil, RGF_USER_BL +
733                     offsetof(struct bl_dedicated_registers_v1,
734                              bl_shutdown_handshake));
735         if (val & BL_SHUTDOWN_HS_RTD) {
736                 wil_dbg_misc(wil, "BL is ready for halt\n");
737                 return;
738         }
739
740         wil_err(wil, "BL did not report ready for halt\n");
741 }
742
743 /* this format is used by ARC embedded CPU for instruction memory */
744 static inline u32 ARC_me_imm32(u32 d)
745 {
746         return ((d & 0xffff0000) >> 16) | ((d & 0x0000ffff) << 16);
747 }
748
749 /* defines access to interrupt vectors for wil_freeze_bl */
750 #define ARC_IRQ_VECTOR_OFFSET(N)        ((N) * 8)
751 /* ARC long jump instruction */
752 #define ARC_JAL_INST                    (0x20200f80)
753
754 static void wil_freeze_bl(struct wil6210_priv *wil)
755 {
756         u32 jal, upc, saved;
757         u32 ivt3 = ARC_IRQ_VECTOR_OFFSET(3);
758
759         jal = wil_r(wil, wil->iccm_base + ivt3);
760         if (jal != ARC_me_imm32(ARC_JAL_INST)) {
761                 wil_dbg_misc(wil, "invalid IVT entry found, skipping\n");
762                 return;
763         }
764
765         /* prevent the target from entering deep sleep
766          * and disabling memory access
767          */
768         saved = wil_r(wil, RGF_USER_USAGE_8);
769         wil_w(wil, RGF_USER_USAGE_8, saved | BIT_USER_PREVENT_DEEP_SLEEP);
770         usleep_range(20, 25); /* let the BL process the bit */
771
772         /* redirect to endless loop in the INT_L1 context and let it trap */
773         wil_w(wil, wil->iccm_base + ivt3 + 4, ARC_me_imm32(ivt3));
774         usleep_range(20, 25); /* let the BL get into the trap */
775
776         /* verify the BL is frozen */
777         upc = wil_r(wil, RGF_USER_CPU_PC);
778         if (upc < ivt3 || (upc > (ivt3 + 8)))
779                 wil_dbg_misc(wil, "BL freeze failed, PC=0x%08X\n", upc);
780
781         wil_w(wil, RGF_USER_USAGE_8, saved);
782 }
783
784 static void wil_bl_prepare_halt(struct wil6210_priv *wil)
785 {
786         u32 tmp, ver;
787
788         /* before halting device CPU driver must make sure BL is not accessing
789          * host memory. This is done differently depending on BL version:
790          * 1. For very old BL versions the procedure is skipped
791          * (not supported).
792          * 2. For old BL version we use a special trick to freeze the BL
793          * 3. For new BL versions we shutdown the BL using handshake procedure.
794          */
795         tmp = wil_r(wil, RGF_USER_BL +
796                     offsetof(struct bl_dedicated_registers_v0,
797                              boot_loader_struct_version));
798         if (!tmp) {
799                 wil_dbg_misc(wil, "old BL, skipping halt preparation\n");
800                 return;
801         }
802
803         tmp = wil_r(wil, RGF_USER_BL +
804                     offsetof(struct bl_dedicated_registers_v1,
805                              bl_shutdown_handshake));
806         ver = BL_SHUTDOWN_HS_PROT_VER(tmp);
807
808         if (ver > 0)
809                 wil_shutdown_bl(wil);
810         else
811                 wil_freeze_bl(wil);
812 }
813
814 static inline void wil_halt_cpu(struct wil6210_priv *wil)
815 {
816         if (wil->hw_version >= HW_VER_TALYN_MB) {
817                 wil_w(wil, RGF_USER_USER_CPU_0_TALYN_MB,
818                       BIT_USER_USER_CPU_MAN_RST);
819                 wil_w(wil, RGF_USER_MAC_CPU_0_TALYN_MB,
820                       BIT_USER_MAC_CPU_MAN_RST);
821         } else {
822                 wil_w(wil, RGF_USER_USER_CPU_0, BIT_USER_USER_CPU_MAN_RST);
823                 wil_w(wil, RGF_USER_MAC_CPU_0,  BIT_USER_MAC_CPU_MAN_RST);
824         }
825 }
826
827 static inline void wil_release_cpu(struct wil6210_priv *wil)
828 {
829         /* Start CPU */
830         if (wil->hw_version >= HW_VER_TALYN_MB)
831                 wil_w(wil, RGF_USER_USER_CPU_0_TALYN_MB, 1);
832         else
833                 wil_w(wil, RGF_USER_USER_CPU_0, 1);
834 }
835
836 static void wil_set_oob_mode(struct wil6210_priv *wil, u8 mode)
837 {
838         wil_info(wil, "oob_mode to %d\n", mode);
839         switch (mode) {
840         case 0:
841                 wil_c(wil, RGF_USER_USAGE_6, BIT_USER_OOB_MODE |
842                       BIT_USER_OOB_R2_MODE);
843                 break;
844         case 1:
845                 wil_c(wil, RGF_USER_USAGE_6, BIT_USER_OOB_R2_MODE);
846                 wil_s(wil, RGF_USER_USAGE_6, BIT_USER_OOB_MODE);
847                 break;
848         case 2:
849                 wil_c(wil, RGF_USER_USAGE_6, BIT_USER_OOB_MODE);
850                 wil_s(wil, RGF_USER_USAGE_6, BIT_USER_OOB_R2_MODE);
851                 break;
852         default:
853                 wil_err(wil, "invalid oob_mode: %d\n", mode);
854         }
855 }
856
857 static int wil_wait_device_ready(struct wil6210_priv *wil, int no_flash)
858 {
859         int delay = 0;
860         u32 x, x1 = 0;
861
862         /* wait until device ready. */
863         if (no_flash) {
864                 msleep(PMU_READY_DELAY_MS);
865
866                 wil_dbg_misc(wil, "Reset completed\n");
867         } else {
868                 do {
869                         msleep(RST_DELAY);
870                         x = wil_r(wil, RGF_USER_BL +
871                                   offsetof(struct bl_dedicated_registers_v0,
872                                            boot_loader_ready));
873                         if (x1 != x) {
874                                 wil_dbg_misc(wil, "BL.ready 0x%08x => 0x%08x\n",
875                                              x1, x);
876                                 x1 = x;
877                         }
878                         if (delay++ > RST_COUNT) {
879                                 wil_err(wil, "Reset not completed, bl.ready 0x%08x\n",
880                                         x);
881                                 return -ETIME;
882                         }
883                 } while (x != BL_READY);
884
885                 wil_dbg_misc(wil, "Reset completed in %d ms\n",
886                              delay * RST_DELAY);
887         }
888
889         return 0;
890 }
891
892 static int wil_wait_device_ready_talyn_mb(struct wil6210_priv *wil)
893 {
894         u32 otp_hw;
895         u8 signature_status;
896         bool otp_signature_err;
897         bool hw_section_done;
898         u32 otp_qc_secured;
899         int delay = 0;
900
901         /* Wait for OTP signature test to complete */
902         usleep_range(2000, 2200);
903
904         wil->boot_config = WIL_BOOT_ERR;
905
906         /* Poll until OTP signature status is valid.
907          * In vanilla and development modes, when signature test is complete
908          * HW sets BIT_OTP_SIGNATURE_ERR_TALYN_MB.
909          * In production mode BIT_OTP_SIGNATURE_ERR_TALYN_MB remains 0, poll
910          * for signature status change to 2 or 3.
911          */
912         do {
913                 otp_hw = wil_r(wil, RGF_USER_OTP_HW_RD_MACHINE_1);
914                 signature_status = WIL_GET_BITS(otp_hw, 8, 9);
915                 otp_signature_err = otp_hw & BIT_OTP_SIGNATURE_ERR_TALYN_MB;
916
917                 if (otp_signature_err &&
918                     signature_status == WIL_SIG_STATUS_VANILLA) {
919                         wil->boot_config = WIL_BOOT_VANILLA;
920                         break;
921                 }
922                 if (otp_signature_err &&
923                     signature_status == WIL_SIG_STATUS_DEVELOPMENT) {
924                         wil->boot_config = WIL_BOOT_DEVELOPMENT;
925                         break;
926                 }
927                 if (!otp_signature_err &&
928                     signature_status == WIL_SIG_STATUS_PRODUCTION) {
929                         wil->boot_config = WIL_BOOT_PRODUCTION;
930                         break;
931                 }
932                 if  (!otp_signature_err &&
933                      signature_status ==
934                      WIL_SIG_STATUS_CORRUPTED_PRODUCTION) {
935                         /* Unrecognized OTP signature found. Possibly a
936                          * corrupted production signature, access control
937                          * is applied as in production mode, therefore
938                          * do not fail
939                          */
940                         wil->boot_config = WIL_BOOT_PRODUCTION;
941                         break;
942                 }
943                 if (delay++ > OTP_HW_COUNT)
944                         break;
945
946                 usleep_range(OTP_HW_DELAY, OTP_HW_DELAY + 10);
947         } while (!otp_signature_err && signature_status == 0);
948
949         if (wil->boot_config == WIL_BOOT_ERR) {
950                 wil_err(wil,
951                         "invalid boot config, signature_status %d otp_signature_err %d\n",
952                         signature_status, otp_signature_err);
953                 return -ETIME;
954         }
955
956         wil_dbg_misc(wil,
957                      "signature test done in %d usec, otp_hw 0x%x, boot_config %d\n",
958                      delay * OTP_HW_DELAY, otp_hw, wil->boot_config);
959
960         if (wil->boot_config == WIL_BOOT_VANILLA)
961                 /* Assuming not SPI boot (currently not supported) */
962                 goto out;
963
964         hw_section_done = otp_hw & BIT_OTP_HW_SECTION_DONE_TALYN_MB;
965         delay = 0;
966
967         while (!hw_section_done) {
968                 msleep(RST_DELAY);
969
970                 otp_hw = wil_r(wil, RGF_USER_OTP_HW_RD_MACHINE_1);
971                 hw_section_done = otp_hw & BIT_OTP_HW_SECTION_DONE_TALYN_MB;
972
973                 if (delay++ > RST_COUNT) {
974                         wil_err(wil, "TO waiting for hw_section_done\n");
975                         return -ETIME;
976                 }
977         }
978
979         wil_dbg_misc(wil, "HW section done in %d ms\n", delay * RST_DELAY);
980
981         otp_qc_secured = wil_r(wil, RGF_OTP_QC_SECURED);
982         wil->secured_boot = otp_qc_secured & BIT_BOOT_FROM_ROM ? 1 : 0;
983         wil_dbg_misc(wil, "secured boot is %sabled\n",
984                      wil->secured_boot ? "en" : "dis");
985
986 out:
987         wil_dbg_misc(wil, "Reset completed\n");
988
989         return 0;
990 }
991
992 static int wil_target_reset(struct wil6210_priv *wil, int no_flash)
993 {
994         u32 x;
995         int rc;
996
997         wil_dbg_misc(wil, "Resetting \"%s\"...\n", wil->hw_name);
998
999         if (wil->hw_version < HW_VER_TALYN) {
1000                 /* Clear MAC link up */
1001                 wil_s(wil, RGF_HP_CTRL, BIT(15));
1002                 wil_s(wil, RGF_USER_CLKS_CTL_SW_RST_MASK_0,
1003                       BIT_HPAL_PERST_FROM_PAD);
1004                 wil_s(wil, RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_CAR_PERST_RST);
1005         }
1006
1007         wil_halt_cpu(wil);
1008
1009         if (!no_flash) {
1010                 /* clear all boot loader "ready" bits */
1011                 wil_w(wil, RGF_USER_BL +
1012                       offsetof(struct bl_dedicated_registers_v0,
1013                                boot_loader_ready), 0);
1014                 /* this should be safe to write even with old BLs */
1015                 wil_w(wil, RGF_USER_BL +
1016                       offsetof(struct bl_dedicated_registers_v1,
1017                                bl_shutdown_handshake), 0);
1018         }
1019         /* Clear Fw Download notification */
1020         wil_c(wil, RGF_USER_USAGE_6, BIT(0));
1021
1022         wil_s(wil, RGF_CAF_OSC_CONTROL, BIT_CAF_OSC_XTAL_EN);
1023         /* XTAL stabilization should take about 3ms */
1024         usleep_range(5000, 7000);
1025         x = wil_r(wil, RGF_CAF_PLL_LOCK_STATUS);
1026         if (!(x & BIT_CAF_OSC_DIG_XTAL_STABLE)) {
1027                 wil_err(wil, "Xtal stabilization timeout\n"
1028                         "RGF_CAF_PLL_LOCK_STATUS = 0x%08x\n", x);
1029                 return -ETIME;
1030         }
1031         /* switch 10k to XTAL*/
1032         wil_c(wil, RGF_USER_SPARROW_M_4, BIT_SPARROW_M_4_SEL_SLEEP_OR_REF);
1033         /* 40 MHz */
1034         wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_CAR_AHB_SW_SEL);
1035
1036         wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x3ff81f);
1037         wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0xf);
1038
1039         if (wil->hw_version >= HW_VER_TALYN_MB) {
1040                 wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x7e000000);
1041                 wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003f);
1042                 wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0xc00000f0);
1043                 wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xffe7fe00);
1044         } else {
1045                 wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xfe000000);
1046                 wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003f);
1047                 wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x000000f0);
1048                 wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xffe7fe00);
1049         }
1050
1051         wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x0);
1052         wil_w(wil, RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0x0);
1053
1054         wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
1055         wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
1056         wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
1057         wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
1058
1059         wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000003);
1060         /* reset A2 PCIE AHB */
1061         wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000);
1062
1063         wil_w(wil, RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
1064
1065         if (wil->hw_version == HW_VER_TALYN_MB)
1066                 rc = wil_wait_device_ready_talyn_mb(wil);
1067         else
1068                 rc = wil_wait_device_ready(wil, no_flash);
1069         if (rc)
1070                 return rc;
1071
1072         wil_c(wil, RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
1073
1074         /* enable fix for HW bug related to the SA/DA swap in AP Rx */
1075         wil_s(wil, RGF_DMA_OFUL_NID_0, BIT_DMA_OFUL_NID_0_RX_EXT_TR_EN |
1076               BIT_DMA_OFUL_NID_0_RX_EXT_A3_SRC);
1077
1078         if (wil->hw_version < HW_VER_TALYN_MB && no_flash) {
1079                 /* Reset OTP HW vectors to fit 40MHz */
1080                 wil_w(wil, RGF_USER_XPM_IFC_RD_TIME1, 0x60001);
1081                 wil_w(wil, RGF_USER_XPM_IFC_RD_TIME2, 0x20027);
1082                 wil_w(wil, RGF_USER_XPM_IFC_RD_TIME3, 0x1);
1083                 wil_w(wil, RGF_USER_XPM_IFC_RD_TIME4, 0x20027);
1084                 wil_w(wil, RGF_USER_XPM_IFC_RD_TIME5, 0x30003);
1085                 wil_w(wil, RGF_USER_XPM_IFC_RD_TIME6, 0x20002);
1086                 wil_w(wil, RGF_USER_XPM_IFC_RD_TIME7, 0x60001);
1087                 wil_w(wil, RGF_USER_XPM_IFC_RD_TIME8, 0x60001);
1088                 wil_w(wil, RGF_USER_XPM_IFC_RD_TIME9, 0x60001);
1089                 wil_w(wil, RGF_USER_XPM_IFC_RD_TIME10, 0x60001);
1090                 wil_w(wil, RGF_USER_XPM_RD_DOUT_SAMPLE_TIME, 0x57);
1091         }
1092
1093         return 0;
1094 }
1095
1096 static void wil_collect_fw_info(struct wil6210_priv *wil)
1097 {
1098         struct wiphy *wiphy = wil_to_wiphy(wil);
1099         u8 retry_short;
1100         int rc;
1101
1102         wil_refresh_fw_capabilities(wil);
1103
1104         rc = wmi_get_mgmt_retry(wil, &retry_short);
1105         if (!rc) {
1106                 wiphy->retry_short = retry_short;
1107                 wil_dbg_misc(wil, "FW retry_short: %d\n", retry_short);
1108         }
1109 }
1110
1111 void wil_refresh_fw_capabilities(struct wil6210_priv *wil)
1112 {
1113         struct wiphy *wiphy = wil_to_wiphy(wil);
1114         int features;
1115
1116         wil->keep_radio_on_during_sleep =
1117                 test_bit(WIL_PLATFORM_CAPA_RADIO_ON_IN_SUSPEND,
1118                          wil->platform_capa) &&
1119                 test_bit(WMI_FW_CAPABILITY_D3_SUSPEND, wil->fw_capabilities);
1120
1121         wil_info(wil, "keep_radio_on_during_sleep (%d)\n",
1122                  wil->keep_radio_on_during_sleep);
1123
1124         if (test_bit(WMI_FW_CAPABILITY_RSSI_REPORTING, wil->fw_capabilities))
1125                 wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
1126         else
1127                 wiphy->signal_type = CFG80211_SIGNAL_TYPE_UNSPEC;
1128
1129         if (test_bit(WMI_FW_CAPABILITY_PNO, wil->fw_capabilities)) {
1130                 wiphy->max_sched_scan_reqs = 1;
1131                 wiphy->max_sched_scan_ssids = WMI_MAX_PNO_SSID_NUM;
1132                 wiphy->max_match_sets = WMI_MAX_PNO_SSID_NUM;
1133                 wiphy->max_sched_scan_ie_len = WMI_MAX_IE_LEN;
1134                 wiphy->max_sched_scan_plans = WMI_MAX_PLANS_NUM;
1135         }
1136
1137         if (test_bit(WMI_FW_CAPABILITY_TX_REQ_EXT, wil->fw_capabilities))
1138                 wiphy->flags |= WIPHY_FLAG_OFFCHAN_TX;
1139
1140         if (wil->platform_ops.set_features) {
1141                 features = (test_bit(WMI_FW_CAPABILITY_REF_CLOCK_CONTROL,
1142                                      wil->fw_capabilities) &&
1143                             test_bit(WIL_PLATFORM_CAPA_EXT_CLK,
1144                                      wil->platform_capa)) ?
1145                         BIT(WIL_PLATFORM_FEATURE_FW_EXT_CLK_CONTROL) : 0;
1146
1147                 if (wil->n_msi == 3)
1148                         features |= BIT(WIL_PLATFORM_FEATURE_TRIPLE_MSI);
1149
1150                 wil->platform_ops.set_features(wil->platform_handle, features);
1151         }
1152
1153         if (test_bit(WMI_FW_CAPABILITY_BACK_WIN_SIZE_64,
1154                      wil->fw_capabilities)) {
1155                 wil->max_agg_wsize = WIL_MAX_AGG_WSIZE_64;
1156                 wil->max_ampdu_size = WIL_MAX_AMPDU_SIZE_128;
1157         } else {
1158                 wil->max_agg_wsize = WIL_MAX_AGG_WSIZE;
1159                 wil->max_ampdu_size = WIL_MAX_AMPDU_SIZE;
1160         }
1161 }
1162
1163 void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
1164 {
1165         le32_to_cpus(&r->base);
1166         le16_to_cpus(&r->entry_size);
1167         le16_to_cpus(&r->size);
1168         le32_to_cpus(&r->tail);
1169         le32_to_cpus(&r->head);
1170 }
1171
1172 /* construct actual board file name to use */
1173 void wil_get_board_file(struct wil6210_priv *wil, char *buf, size_t len)
1174 {
1175         const char *board_file;
1176         const char *wil_talyn_fw_name = ftm_mode ? WIL_FW_NAME_FTM_TALYN :
1177                               WIL_FW_NAME_TALYN;
1178
1179         if (wil->board_file) {
1180                 board_file = wil->board_file;
1181         } else {
1182                 /* If specific FW file is used for Talyn,
1183                  * use specific board file
1184                  */
1185                 if (strcmp(wil->wil_fw_name, wil_talyn_fw_name) == 0)
1186                         board_file = WIL_BRD_NAME_TALYN;
1187                 else
1188                         board_file = WIL_BOARD_FILE_NAME;
1189         }
1190
1191         strlcpy(buf, board_file, len);
1192 }
1193
1194 static int wil_get_bl_info(struct wil6210_priv *wil)
1195 {
1196         struct net_device *ndev = wil->main_ndev;
1197         struct wiphy *wiphy = wil_to_wiphy(wil);
1198         union {
1199                 struct bl_dedicated_registers_v0 bl0;
1200                 struct bl_dedicated_registers_v1 bl1;
1201         } bl;
1202         u32 bl_ver;
1203         u8 *mac;
1204         u16 rf_status;
1205
1206         wil_memcpy_fromio_32(&bl, wil->csr + HOSTADDR(RGF_USER_BL),
1207                              sizeof(bl));
1208         bl_ver = le32_to_cpu(bl.bl0.boot_loader_struct_version);
1209         mac = bl.bl0.mac_address;
1210
1211         if (bl_ver == 0) {
1212                 le32_to_cpus(&bl.bl0.rf_type);
1213                 le32_to_cpus(&bl.bl0.baseband_type);
1214                 rf_status = 0; /* actually, unknown */
1215                 wil_info(wil,
1216                          "Boot Loader struct v%d: MAC = %pM RF = 0x%08x bband = 0x%08x\n",
1217                          bl_ver, mac,
1218                          bl.bl0.rf_type, bl.bl0.baseband_type);
1219                 wil_info(wil, "Boot Loader build unknown for struct v0\n");
1220         } else {
1221                 le16_to_cpus(&bl.bl1.rf_type);
1222                 rf_status = le16_to_cpu(bl.bl1.rf_status);
1223                 le32_to_cpus(&bl.bl1.baseband_type);
1224                 le16_to_cpus(&bl.bl1.bl_version_subminor);
1225                 le16_to_cpus(&bl.bl1.bl_version_build);
1226                 wil_info(wil,
1227                          "Boot Loader struct v%d: MAC = %pM RF = 0x%04x (status 0x%04x) bband = 0x%08x\n",
1228                          bl_ver, mac,
1229                          bl.bl1.rf_type, rf_status,
1230                          bl.bl1.baseband_type);
1231                 wil_info(wil, "Boot Loader build %d.%d.%d.%d\n",
1232                          bl.bl1.bl_version_major, bl.bl1.bl_version_minor,
1233                          bl.bl1.bl_version_subminor, bl.bl1.bl_version_build);
1234         }
1235
1236         if (!is_valid_ether_addr(mac)) {
1237                 wil_err(wil, "BL: Invalid MAC %pM\n", mac);
1238                 return -EINVAL;
1239         }
1240
1241         ether_addr_copy(ndev->perm_addr, mac);
1242         ether_addr_copy(wiphy->perm_addr, mac);
1243         if (!is_valid_ether_addr(ndev->dev_addr))
1244                 ether_addr_copy(ndev->dev_addr, mac);
1245
1246         if (rf_status) {/* bad RF cable? */
1247                 wil_err(wil, "RF communication error 0x%04x",
1248                         rf_status);
1249                 return -EAGAIN;
1250         }
1251
1252         return 0;
1253 }
1254
1255 static void wil_bl_crash_info(struct wil6210_priv *wil, bool is_err)
1256 {
1257         u32 bl_assert_code, bl_assert_blink, bl_magic_number;
1258         u32 bl_ver = wil_r(wil, RGF_USER_BL +
1259                            offsetof(struct bl_dedicated_registers_v0,
1260                                     boot_loader_struct_version));
1261
1262         if (bl_ver < 2)
1263                 return;
1264
1265         bl_assert_code = wil_r(wil, RGF_USER_BL +
1266                                offsetof(struct bl_dedicated_registers_v1,
1267                                         bl_assert_code));
1268         bl_assert_blink = wil_r(wil, RGF_USER_BL +
1269                                 offsetof(struct bl_dedicated_registers_v1,
1270                                          bl_assert_blink));
1271         bl_magic_number = wil_r(wil, RGF_USER_BL +
1272                                 offsetof(struct bl_dedicated_registers_v1,
1273                                          bl_magic_number));
1274
1275         if (is_err) {
1276                 wil_err(wil,
1277                         "BL assert code 0x%08x blink 0x%08x magic 0x%08x\n",
1278                         bl_assert_code, bl_assert_blink, bl_magic_number);
1279         } else {
1280                 wil_dbg_misc(wil,
1281                              "BL assert code 0x%08x blink 0x%08x magic 0x%08x\n",
1282                              bl_assert_code, bl_assert_blink, bl_magic_number);
1283         }
1284 }
1285
1286 static int wil_get_otp_info(struct wil6210_priv *wil)
1287 {
1288         struct net_device *ndev = wil->main_ndev;
1289         struct wiphy *wiphy = wil_to_wiphy(wil);
1290         u8 mac[8];
1291         int mac_addr;
1292
1293         if (wil->hw_version >= HW_VER_TALYN_MB)
1294                 mac_addr = RGF_OTP_MAC_TALYN_MB;
1295         else
1296                 mac_addr = RGF_OTP_MAC;
1297
1298         wil_memcpy_fromio_32(mac, wil->csr + HOSTADDR(mac_addr),
1299                              sizeof(mac));
1300         if (!is_valid_ether_addr(mac)) {
1301                 wil_err(wil, "Invalid MAC %pM\n", mac);
1302                 return -EINVAL;
1303         }
1304
1305         ether_addr_copy(ndev->perm_addr, mac);
1306         ether_addr_copy(wiphy->perm_addr, mac);
1307         if (!is_valid_ether_addr(ndev->dev_addr))
1308                 ether_addr_copy(ndev->dev_addr, mac);
1309
1310         return 0;
1311 }
1312
1313 static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
1314 {
1315         ulong to = msecs_to_jiffies(2000);
1316         ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
1317
1318         if (0 == left) {
1319                 wil_err(wil, "Firmware not ready\n");
1320                 return -ETIME;
1321         } else {
1322                 wil_info(wil, "FW ready after %d ms. HW version 0x%08x\n",
1323                          jiffies_to_msecs(to-left), wil->hw_version);
1324         }
1325         return 0;
1326 }
1327
1328 void wil_abort_scan(struct wil6210_vif *vif, bool sync)
1329 {
1330         struct wil6210_priv *wil = vif_to_wil(vif);
1331         int rc;
1332         struct cfg80211_scan_info info = {
1333                 .aborted = true,
1334         };
1335
1336         lockdep_assert_held(&wil->vif_mutex);
1337
1338         if (!vif->scan_request)
1339                 return;
1340
1341         wil_dbg_misc(wil, "Abort scan_request 0x%p\n", vif->scan_request);
1342         del_timer_sync(&vif->scan_timer);
1343         mutex_unlock(&wil->vif_mutex);
1344         rc = wmi_abort_scan(vif);
1345         if (!rc && sync)
1346                 wait_event_interruptible_timeout(wil->wq, !vif->scan_request,
1347                                                  msecs_to_jiffies(
1348                                                  WAIT_FOR_SCAN_ABORT_MS));
1349
1350         mutex_lock(&wil->vif_mutex);
1351         if (vif->scan_request) {
1352                 cfg80211_scan_done(vif->scan_request, &info);
1353                 vif->scan_request = NULL;
1354         }
1355 }
1356
1357 void wil_abort_scan_all_vifs(struct wil6210_priv *wil, bool sync)
1358 {
1359         int i;
1360
1361         lockdep_assert_held(&wil->vif_mutex);
1362
1363         for (i = 0; i < wil->max_vifs; i++) {
1364                 struct wil6210_vif *vif = wil->vifs[i];
1365
1366                 if (vif)
1367                         wil_abort_scan(vif, sync);
1368         }
1369 }
1370
1371 int wil_ps_update(struct wil6210_priv *wil, enum wmi_ps_profile_type ps_profile)
1372 {
1373         int rc;
1374
1375         if (!test_bit(WMI_FW_CAPABILITY_PS_CONFIG, wil->fw_capabilities)) {
1376                 wil_err(wil, "set_power_mgmt not supported\n");
1377                 return -EOPNOTSUPP;
1378         }
1379
1380         rc  = wmi_ps_dev_profile_cfg(wil, ps_profile);
1381         if (rc)
1382                 wil_err(wil, "wmi_ps_dev_profile_cfg failed (%d)\n", rc);
1383         else
1384                 wil->ps_profile = ps_profile;
1385
1386         return rc;
1387 }
1388
1389 static void wil_pre_fw_config(struct wil6210_priv *wil)
1390 {
1391         /* Mark FW as loaded from host */
1392         wil_s(wil, RGF_USER_USAGE_6, 1);
1393
1394         /* clear any interrupts which on-card-firmware
1395          * may have set
1396          */
1397         wil6210_clear_irq(wil);
1398         /* CAF_ICR - clear and mask */
1399         /* it is W1C, clear by writing back same value */
1400         if (wil->hw_version < HW_VER_TALYN_MB) {
1401                 wil_s(wil, RGF_CAF_ICR + offsetof(struct RGF_ICR, ICR), 0);
1402                 wil_w(wil, RGF_CAF_ICR + offsetof(struct RGF_ICR, IMV), ~0);
1403         } else {
1404                 wil_s(wil,
1405                       RGF_CAF_ICR_TALYN_MB + offsetof(struct RGF_ICR, ICR), 0);
1406                 wil_w(wil, RGF_CAF_ICR_TALYN_MB +
1407                       offsetof(struct RGF_ICR, IMV), ~0);
1408         }
1409         /* clear PAL_UNIT_ICR (potential D0->D3 leftover)
1410          * In Talyn-MB host cannot access this register due to
1411          * access control, hence PAL_UNIT_ICR is cleared by the FW
1412          */
1413         if (wil->hw_version < HW_VER_TALYN_MB)
1414                 wil_s(wil, RGF_PAL_UNIT_ICR + offsetof(struct RGF_ICR, ICR),
1415                       0);
1416
1417         if (wil->fw_calib_result > 0) {
1418                 __le32 val = cpu_to_le32(wil->fw_calib_result |
1419                                                 (CALIB_RESULT_SIGNATURE << 8));
1420                 wil_w(wil, RGF_USER_FW_CALIB_RESULT, (u32 __force)val);
1421         }
1422 }
1423
1424 static int wil_restore_vifs(struct wil6210_priv *wil)
1425 {
1426         struct wil6210_vif *vif;
1427         struct net_device *ndev;
1428         struct wireless_dev *wdev;
1429         int i, rc;
1430
1431         for (i = 0; i < wil->max_vifs; i++) {
1432                 vif = wil->vifs[i];
1433                 if (!vif)
1434                         continue;
1435                 vif->ap_isolate = 0;
1436                 if (vif->mid) {
1437                         ndev = vif_to_ndev(vif);
1438                         wdev = vif_to_wdev(vif);
1439                         rc = wmi_port_allocate(wil, vif->mid, ndev->dev_addr,
1440                                                wdev->iftype);
1441                         if (rc) {
1442                                 wil_err(wil, "fail to restore VIF %d type %d, rc %d\n",
1443                                         i, wdev->iftype, rc);
1444                                 return rc;
1445                         }
1446                 }
1447         }
1448
1449         return 0;
1450 }
1451
1452 /*
1453  * We reset all the structures, and we reset the UMAC.
1454  * After calling this routine, you're expected to reload
1455  * the firmware.
1456  */
1457 int wil_reset(struct wil6210_priv *wil, bool load_fw)
1458 {
1459         int rc, i;
1460         unsigned long status_flags = BIT(wil_status_resetting);
1461         int no_flash;
1462         struct wil6210_vif *vif;
1463
1464         wil_dbg_misc(wil, "reset\n");
1465
1466         WARN_ON(!mutex_is_locked(&wil->mutex));
1467         WARN_ON(test_bit(wil_status_napi_en, wil->status));
1468
1469         if (debug_fw) {
1470                 static const u8 mac[ETH_ALEN] = {
1471                         0x00, 0xde, 0xad, 0x12, 0x34, 0x56,
1472                 };
1473                 struct net_device *ndev = wil->main_ndev;
1474
1475                 ether_addr_copy(ndev->perm_addr, mac);
1476                 ether_addr_copy(ndev->dev_addr, ndev->perm_addr);
1477                 return 0;
1478         }
1479
1480         if (wil->hw_version == HW_VER_UNKNOWN)
1481                 return -ENODEV;
1482
1483         if (test_bit(WIL_PLATFORM_CAPA_T_PWR_ON_0, wil->platform_capa)) {
1484                 wil_dbg_misc(wil, "Notify FW to set T_POWER_ON=0\n");
1485                 wil_s(wil, RGF_USER_USAGE_8, BIT_USER_SUPPORT_T_POWER_ON_0);
1486         }
1487
1488         if (test_bit(WIL_PLATFORM_CAPA_EXT_CLK, wil->platform_capa)) {
1489                 wil_dbg_misc(wil, "Notify FW on ext clock configuration\n");
1490                 wil_s(wil, RGF_USER_USAGE_8, BIT_USER_EXT_CLK);
1491         }
1492
1493         if (wil->platform_ops.notify) {
1494                 rc = wil->platform_ops.notify(wil->platform_handle,
1495                                               WIL_PLATFORM_EVT_PRE_RESET);
1496                 if (rc)
1497                         wil_err(wil, "PRE_RESET platform notify failed, rc %d\n",
1498                                 rc);
1499         }
1500
1501         set_bit(wil_status_resetting, wil->status);
1502         if (test_bit(wil_status_collecting_dumps, wil->status)) {
1503                 /* Device collects crash dump, cancel the reset.
1504                  * following crash dump collection, reset would take place.
1505                  */
1506                 wil_dbg_misc(wil, "reject reset while collecting crash dump\n");
1507                 rc = -EBUSY;
1508                 goto out;
1509         }
1510
1511         mutex_lock(&wil->vif_mutex);
1512         wil_abort_scan_all_vifs(wil, false);
1513         mutex_unlock(&wil->vif_mutex);
1514
1515         for (i = 0; i < wil->max_vifs; i++) {
1516                 vif = wil->vifs[i];
1517                 if (vif) {
1518                         cancel_work_sync(&vif->disconnect_worker);
1519                         wil6210_disconnect(vif, NULL,
1520                                            WLAN_REASON_DEAUTH_LEAVING, false);
1521                 }
1522         }
1523         wil_bcast_fini_all(wil);
1524
1525         /* Disable device led before reset*/
1526         wmi_led_cfg(wil, false);
1527
1528         /* prevent NAPI from being scheduled and prevent wmi commands */
1529         mutex_lock(&wil->wmi_mutex);
1530         if (test_bit(wil_status_suspending, wil->status))
1531                 status_flags |= BIT(wil_status_suspending);
1532         bitmap_and(wil->status, wil->status, &status_flags,
1533                    wil_status_last);
1534         wil_dbg_misc(wil, "wil->status (0x%lx)\n", *wil->status);
1535         mutex_unlock(&wil->wmi_mutex);
1536
1537         wil_mask_irq(wil);
1538
1539         wmi_event_flush(wil);
1540
1541         flush_workqueue(wil->wq_service);
1542         flush_workqueue(wil->wmi_wq);
1543
1544         no_flash = test_bit(hw_capa_no_flash, wil->hw_capa);
1545         if (!no_flash)
1546                 wil_bl_crash_info(wil, false);
1547         wil_disable_irq(wil);
1548         rc = wil_target_reset(wil, no_flash);
1549         wil6210_clear_irq(wil);
1550         wil_enable_irq(wil);
1551         wil->txrx_ops.rx_fini(wil);
1552         wil->txrx_ops.tx_fini(wil);
1553         if (rc) {
1554                 if (!no_flash)
1555                         wil_bl_crash_info(wil, true);
1556                 goto out;
1557         }
1558
1559         if (no_flash) {
1560                 rc = wil_get_otp_info(wil);
1561         } else {
1562                 rc = wil_get_bl_info(wil);
1563                 if (rc == -EAGAIN && !load_fw)
1564                         /* ignore RF error if not going up */
1565                         rc = 0;
1566         }
1567         if (rc)
1568                 goto out;
1569
1570         wil_set_oob_mode(wil, oob_mode);
1571         if (load_fw) {
1572                 char board_file[WIL_BOARD_FILE_MAX_NAMELEN];
1573
1574                 if  (wil->secured_boot) {
1575                         wil_err(wil, "secured boot is not supported\n");
1576                         return -ENOTSUPP;
1577                 }
1578
1579                 board_file[0] = '\0';
1580                 wil_get_board_file(wil, board_file, sizeof(board_file));
1581                 wil_info(wil, "Use firmware <%s> + board <%s>\n",
1582                          wil->wil_fw_name, board_file);
1583
1584                 if (!no_flash)
1585                         wil_bl_prepare_halt(wil);
1586
1587                 wil_halt_cpu(wil);
1588                 memset(wil->fw_version, 0, sizeof(wil->fw_version));
1589                 /* Loading f/w from the file */
1590                 rc = wil_request_firmware(wil, wil->wil_fw_name, true);
1591                 if (rc)
1592                         goto out;
1593                 if (wil->brd_file_addr)
1594                         rc = wil_request_board(wil, board_file);
1595                 else
1596                         rc = wil_request_firmware(wil, board_file, true);
1597                 if (rc)
1598                         goto out;
1599
1600                 wil_pre_fw_config(wil);
1601                 wil_release_cpu(wil);
1602         }
1603
1604         /* init after reset */
1605         reinit_completion(&wil->wmi_ready);
1606         reinit_completion(&wil->wmi_call);
1607         reinit_completion(&wil->halp.comp);
1608
1609         clear_bit(wil_status_resetting, wil->status);
1610
1611         if (load_fw) {
1612                 wil_unmask_irq(wil);
1613
1614                 /* we just started MAC, wait for FW ready */
1615                 rc = wil_wait_for_fw_ready(wil);
1616                 if (rc)
1617                         return rc;
1618
1619                 /* check FW is responsive */
1620                 rc = wmi_echo(wil);
1621                 if (rc) {
1622                         wil_err(wil, "wmi_echo failed, rc %d\n", rc);
1623                         return rc;
1624                 }
1625
1626                 wil->txrx_ops.configure_interrupt_moderation(wil);
1627
1628                 /* Enable OFU rdy valid bug fix, to prevent hang in oful34_rx
1629                  * while there is back-pressure from Host during RX
1630                  */
1631                 if (wil->hw_version >= HW_VER_TALYN_MB)
1632                         wil_s(wil, RGF_DMA_MISC_CTL,
1633                               BIT_OFUL34_RDY_VALID_BUG_FIX_EN);
1634
1635                 rc = wil_restore_vifs(wil);
1636                 if (rc) {
1637                         wil_err(wil, "failed to restore vifs, rc %d\n", rc);
1638                         return rc;
1639                 }
1640
1641                 wil_collect_fw_info(wil);
1642
1643                 if (wil->ps_profile != WMI_PS_PROFILE_TYPE_DEFAULT)
1644                         wil_ps_update(wil, wil->ps_profile);
1645
1646                 if (wil->platform_ops.notify) {
1647                         rc = wil->platform_ops.notify(wil->platform_handle,
1648                                                       WIL_PLATFORM_EVT_FW_RDY);
1649                         if (rc) {
1650                                 wil_err(wil, "FW_RDY notify failed, rc %d\n",
1651                                         rc);
1652                                 rc = 0;
1653                         }
1654                 }
1655         }
1656
1657         return rc;
1658
1659 out:
1660         clear_bit(wil_status_resetting, wil->status);
1661         return rc;
1662 }
1663
1664 void wil_fw_error_recovery(struct wil6210_priv *wil)
1665 {
1666         wil_dbg_misc(wil, "starting fw error recovery\n");
1667
1668         if (test_bit(wil_status_resetting, wil->status)) {
1669                 wil_info(wil, "Reset already in progress\n");
1670                 return;
1671         }
1672
1673         wil->recovery_state = fw_recovery_pending;
1674         schedule_work(&wil->fw_error_worker);
1675 }
1676
1677 int __wil_up(struct wil6210_priv *wil)
1678 {
1679         struct net_device *ndev = wil->main_ndev;
1680         struct wireless_dev *wdev = ndev->ieee80211_ptr;
1681         int rc;
1682
1683         WARN_ON(!mutex_is_locked(&wil->mutex));
1684
1685         rc = wil_reset(wil, true);
1686         if (rc)
1687                 return rc;
1688
1689         /* Rx RING. After MAC and beacon */
1690         rc = wil->txrx_ops.rx_init(wil, rx_ring_order);
1691         if (rc)
1692                 return rc;
1693
1694         rc = wil->txrx_ops.tx_init(wil);
1695         if (rc)
1696                 return rc;
1697
1698         switch (wdev->iftype) {
1699         case NL80211_IFTYPE_STATION:
1700                 wil_dbg_misc(wil, "type: STATION\n");
1701                 ndev->type = ARPHRD_ETHER;
1702                 break;
1703         case NL80211_IFTYPE_AP:
1704                 wil_dbg_misc(wil, "type: AP\n");
1705                 ndev->type = ARPHRD_ETHER;
1706                 break;
1707         case NL80211_IFTYPE_P2P_CLIENT:
1708                 wil_dbg_misc(wil, "type: P2P_CLIENT\n");
1709                 ndev->type = ARPHRD_ETHER;
1710                 break;
1711         case NL80211_IFTYPE_P2P_GO:
1712                 wil_dbg_misc(wil, "type: P2P_GO\n");
1713                 ndev->type = ARPHRD_ETHER;
1714                 break;
1715         case NL80211_IFTYPE_MONITOR:
1716                 wil_dbg_misc(wil, "type: Monitor\n");
1717                 ndev->type = ARPHRD_IEEE80211_RADIOTAP;
1718                 /* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
1719                 break;
1720         default:
1721                 return -EOPNOTSUPP;
1722         }
1723
1724         /* MAC address - pre-requisite for other commands */
1725         wmi_set_mac_address(wil, ndev->dev_addr);
1726
1727         wil_dbg_misc(wil, "NAPI enable\n");
1728         napi_enable(&wil->napi_rx);
1729         napi_enable(&wil->napi_tx);
1730         set_bit(wil_status_napi_en, wil->status);
1731
1732         wil6210_bus_request(wil, WIL_DEFAULT_BUS_REQUEST_KBPS);
1733
1734         return 0;
1735 }
1736
1737 int wil_up(struct wil6210_priv *wil)
1738 {
1739         int rc;
1740
1741         wil_dbg_misc(wil, "up\n");
1742
1743         mutex_lock(&wil->mutex);
1744         rc = __wil_up(wil);
1745         mutex_unlock(&wil->mutex);
1746
1747         return rc;
1748 }
1749
1750 int __wil_down(struct wil6210_priv *wil)
1751 {
1752         WARN_ON(!mutex_is_locked(&wil->mutex));
1753
1754         set_bit(wil_status_resetting, wil->status);
1755
1756         wil6210_bus_request(wil, 0);
1757
1758         wil_disable_irq(wil);
1759         if (test_and_clear_bit(wil_status_napi_en, wil->status)) {
1760                 napi_disable(&wil->napi_rx);
1761                 napi_disable(&wil->napi_tx);
1762                 wil_dbg_misc(wil, "NAPI disable\n");
1763         }
1764         wil_enable_irq(wil);
1765
1766         mutex_lock(&wil->vif_mutex);
1767         wil_p2p_stop_radio_operations(wil);
1768         wil_abort_scan_all_vifs(wil, false);
1769         mutex_unlock(&wil->vif_mutex);
1770
1771         return wil_reset(wil, false);
1772 }
1773
1774 int wil_down(struct wil6210_priv *wil)
1775 {
1776         int rc;
1777
1778         wil_dbg_misc(wil, "down\n");
1779
1780         wil_set_recovery_state(wil, fw_recovery_idle);
1781         mutex_lock(&wil->mutex);
1782         rc = __wil_down(wil);
1783         mutex_unlock(&wil->mutex);
1784
1785         return rc;
1786 }
1787
1788 int wil_find_cid(struct wil6210_priv *wil, u8 mid, const u8 *mac)
1789 {
1790         int i;
1791         int rc = -ENOENT;
1792
1793         for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
1794                 if (wil->sta[i].mid == mid &&
1795                     wil->sta[i].status != wil_sta_unused &&
1796                     ether_addr_equal(wil->sta[i].addr, mac)) {
1797                         rc = i;
1798                         break;
1799                 }
1800         }
1801
1802         return rc;
1803 }
1804
1805 void wil_halp_vote(struct wil6210_priv *wil)
1806 {
1807         unsigned long rc;
1808         unsigned long to_jiffies = msecs_to_jiffies(WAIT_FOR_HALP_VOTE_MS);
1809
1810         mutex_lock(&wil->halp.lock);
1811
1812         wil_dbg_irq(wil, "halp_vote: start, HALP ref_cnt (%d)\n",
1813                     wil->halp.ref_cnt);
1814
1815         if (++wil->halp.ref_cnt == 1) {
1816                 reinit_completion(&wil->halp.comp);
1817                 /* mark to IRQ context to handle HALP ICR */
1818                 wil->halp.handle_icr = true;
1819                 wil6210_set_halp(wil);
1820                 rc = wait_for_completion_timeout(&wil->halp.comp, to_jiffies);
1821                 if (!rc) {
1822                         wil_err(wil, "HALP vote timed out\n");
1823                         /* Mask HALP as done in case the interrupt is raised */
1824                         wil->halp.handle_icr = false;
1825                         wil6210_mask_halp(wil);
1826                 } else {
1827                         wil_dbg_irq(wil,
1828                                     "halp_vote: HALP vote completed after %d ms\n",
1829                                     jiffies_to_msecs(to_jiffies - rc));
1830                 }
1831         }
1832
1833         wil_dbg_irq(wil, "halp_vote: end, HALP ref_cnt (%d)\n",
1834                     wil->halp.ref_cnt);
1835
1836         mutex_unlock(&wil->halp.lock);
1837 }
1838
1839 void wil_halp_unvote(struct wil6210_priv *wil)
1840 {
1841         WARN_ON(wil->halp.ref_cnt == 0);
1842
1843         mutex_lock(&wil->halp.lock);
1844
1845         wil_dbg_irq(wil, "halp_unvote: start, HALP ref_cnt (%d)\n",
1846                     wil->halp.ref_cnt);
1847
1848         if (--wil->halp.ref_cnt == 0) {
1849                 wil6210_clear_halp(wil);
1850                 wil_dbg_irq(wil, "HALP unvote\n");
1851         }
1852
1853         wil_dbg_irq(wil, "halp_unvote:end, HALP ref_cnt (%d)\n",
1854                     wil->halp.ref_cnt);
1855
1856         mutex_unlock(&wil->halp.lock);
1857 }
1858
1859 void wil_init_txrx_ops(struct wil6210_priv *wil)
1860 {
1861         if (wil->use_enhanced_dma_hw)
1862                 wil_init_txrx_ops_edma(wil);
1863         else
1864                 wil_init_txrx_ops_legacy_dma(wil);
1865 }