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