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