]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/wireless/ath/wil6210/main.c
Merge tag 'master-2014-11-04' of git://git.kernel.org/pub/scm/linux/kernel/git/linvil...
[karo-tx-linux.git] / drivers / net / wireless / ath / wil6210 / main.c
1 /*
2  * Copyright (c) 2012-2014 Qualcomm Atheros, Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <linux/moduleparam.h>
18 #include <linux/if_arp.h>
19 #include <linux/etherdevice.h>
20
21 #include "wil6210.h"
22 #include "txrx.h"
23 #include "wmi.h"
24
25 #define WAIT_FOR_DISCONNECT_TIMEOUT_MS 2000
26 #define WAIT_FOR_DISCONNECT_INTERVAL_MS 10
27
28 bool no_fw_recovery;
29 module_param(no_fw_recovery, bool, S_IRUGO | S_IWUSR);
30 MODULE_PARM_DESC(no_fw_recovery, " disable automatic FW error recovery");
31
32 static bool no_fw_load = true;
33 module_param(no_fw_load, bool, S_IRUGO | S_IWUSR);
34 MODULE_PARM_DESC(no_fw_load, " do not download FW, use one in on-card flash.");
35
36 static unsigned int itr_trsh = WIL6210_ITR_TRSH_DEFAULT;
37
38 module_param(itr_trsh, uint, S_IRUGO);
39 MODULE_PARM_DESC(itr_trsh, " Interrupt moderation threshold, usecs.");
40
41 /* We allow allocation of more than 1 page buffers to support large packets.
42  * It is suboptimal behavior performance wise in case MTU above page size.
43  */
44 unsigned int mtu_max = TXRX_BUF_LEN_DEFAULT - ETH_HLEN;
45 static int mtu_max_set(const char *val, const struct kernel_param *kp)
46 {
47         int ret;
48
49         /* sets mtu_max directly. no need to restore it in case of
50          * illegal value since we assume this will fail insmod
51          */
52         ret = param_set_uint(val, kp);
53         if (ret)
54                 return ret;
55
56         if (mtu_max < 68 || mtu_max > IEEE80211_MAX_DATA_LEN_DMG)
57                 ret = -EINVAL;
58
59         return ret;
60 }
61
62 static struct kernel_param_ops mtu_max_ops = {
63         .set = mtu_max_set,
64         .get = param_get_uint,
65 };
66
67 module_param_cb(mtu_max, &mtu_max_ops, &mtu_max, S_IRUGO);
68 MODULE_PARM_DESC(mtu_max, " Max MTU value.");
69
70 #define RST_DELAY (20) /* msec, for loop in @wil_target_reset */
71 #define RST_COUNT (1 + 1000/RST_DELAY) /* round up to be above 1 sec total */
72
73 /*
74  * Due to a hardware issue,
75  * one has to read/write to/from NIC in 32-bit chunks;
76  * regular memcpy_fromio and siblings will
77  * not work on 64-bit platform - it uses 64-bit transactions
78  *
79  * Force 32-bit transactions to enable NIC on 64-bit platforms
80  *
81  * To avoid byte swap on big endian host, __raw_{read|write}l
82  * should be used - {read|write}l would swap bytes to provide
83  * little endian on PCI value in host endianness.
84  */
85 void wil_memcpy_fromio_32(void *dst, const volatile void __iomem *src,
86                           size_t count)
87 {
88         u32 *d = dst;
89         const volatile u32 __iomem *s = src;
90
91         /* size_t is unsigned, if (count%4 != 0) it will wrap */
92         for (count += 4; count > 4; count -= 4)
93                 *d++ = __raw_readl(s++);
94 }
95
96 void wil_memcpy_toio_32(volatile void __iomem *dst, const void *src,
97                         size_t count)
98 {
99         volatile u32 __iomem *d = dst;
100         const u32 *s = src;
101
102         for (count += 4; count > 4; count -= 4)
103                 __raw_writel(*s++, d++);
104 }
105
106 static void wil_disconnect_cid(struct wil6210_priv *wil, int cid,
107                                bool from_event)
108 {
109         uint i;
110         struct net_device *ndev = wil_to_ndev(wil);
111         struct wireless_dev *wdev = wil->wdev;
112         struct wil_sta_info *sta = &wil->sta[cid];
113
114         wil_dbg_misc(wil, "%s(CID %d, status %d)\n", __func__, cid,
115                      sta->status);
116
117         sta->data_port_open = false;
118         if (sta->status != wil_sta_unused) {
119                 if (!from_event)
120                         wmi_disconnect_sta(wil, sta->addr,
121                                            WLAN_REASON_DEAUTH_LEAVING);
122
123                 switch (wdev->iftype) {
124                 case NL80211_IFTYPE_AP:
125                 case NL80211_IFTYPE_P2P_GO:
126                         /* AP-like interface */
127                         cfg80211_del_sta(ndev, sta->addr, GFP_KERNEL);
128                         break;
129                 default:
130                         break;
131                 }
132                 sta->status = wil_sta_unused;
133         }
134
135         for (i = 0; i < WIL_STA_TID_NUM; i++) {
136                 struct wil_tid_ampdu_rx *r;
137                 unsigned long flags;
138
139                 spin_lock_irqsave(&sta->tid_rx_lock, flags);
140
141                 r = sta->tid_rx[i];
142                 sta->tid_rx[i] = NULL;
143                 wil_tid_ampdu_rx_free(wil, r);
144
145                 spin_unlock_irqrestore(&sta->tid_rx_lock, flags);
146         }
147         for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++) {
148                 if (wil->vring2cid_tid[i][0] == cid)
149                         wil_vring_fini_tx(wil, i);
150         }
151         memset(&sta->stats, 0, sizeof(sta->stats));
152 }
153
154 static void _wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid,
155                                 bool from_event)
156 {
157         int cid = -ENOENT;
158         struct net_device *ndev = wil_to_ndev(wil);
159         struct wireless_dev *wdev = wil->wdev;
160
161         might_sleep();
162         if (bssid) {
163                 cid = wil_find_cid(wil, bssid);
164                 wil_dbg_misc(wil, "%s(%pM, CID %d)\n", __func__, bssid, cid);
165         } else {
166                 wil_dbg_misc(wil, "%s(all)\n", __func__);
167         }
168
169         if (cid >= 0) /* disconnect 1 peer */
170                 wil_disconnect_cid(wil, cid, from_event);
171         else /* disconnect all */
172                 for (cid = 0; cid < WIL6210_MAX_CID; cid++)
173                         wil_disconnect_cid(wil, cid, from_event);
174
175         /* link state */
176         switch (wdev->iftype) {
177         case NL80211_IFTYPE_STATION:
178         case NL80211_IFTYPE_P2P_CLIENT:
179                 wil_link_off(wil);
180                 if (test_bit(wil_status_fwconnected, &wil->status)) {
181                         clear_bit(wil_status_fwconnected, &wil->status);
182                         cfg80211_disconnected(ndev,
183                                               WLAN_STATUS_UNSPECIFIED_FAILURE,
184                                               NULL, 0, GFP_KERNEL);
185                 } else if (test_bit(wil_status_fwconnecting, &wil->status)) {
186                         cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
187                                                 WLAN_STATUS_UNSPECIFIED_FAILURE,
188                                                 GFP_KERNEL);
189                 }
190                 clear_bit(wil_status_fwconnecting, &wil->status);
191                 break;
192         default:
193                 break;
194         }
195 }
196
197 static void wil_disconnect_worker(struct work_struct *work)
198 {
199         struct wil6210_priv *wil = container_of(work,
200                         struct wil6210_priv, disconnect_worker);
201
202         mutex_lock(&wil->mutex);
203         _wil6210_disconnect(wil, NULL, false);
204         mutex_unlock(&wil->mutex);
205 }
206
207 static void wil_connect_timer_fn(ulong x)
208 {
209         struct wil6210_priv *wil = (void *)x;
210
211         wil_dbg_misc(wil, "Connect timeout\n");
212
213         /* reschedule to thread context - disconnect won't
214          * run from atomic context
215          */
216         schedule_work(&wil->disconnect_worker);
217 }
218
219 static void wil_scan_timer_fn(ulong x)
220 {
221         struct wil6210_priv *wil = (void *)x;
222
223         clear_bit(wil_status_fwready, &wil->status);
224         wil_err(wil, "Scan timeout detected, start fw error recovery\n");
225         schedule_work(&wil->fw_error_worker);
226 }
227
228 static int wil_wait_for_recovery(struct wil6210_priv *wil)
229 {
230         if (wait_event_interruptible(wil->wq, wil->recovery_state !=
231                                      fw_recovery_pending)) {
232                 wil_err(wil, "Interrupt, canceling recovery\n");
233                 return -ERESTARTSYS;
234         }
235         if (wil->recovery_state != fw_recovery_running) {
236                 wil_info(wil, "Recovery cancelled\n");
237                 return -EINTR;
238         }
239         wil_info(wil, "Proceed with recovery\n");
240         return 0;
241 }
242
243 void wil_set_recovery_state(struct wil6210_priv *wil, int state)
244 {
245         wil_dbg_misc(wil, "%s(%d -> %d)\n", __func__,
246                      wil->recovery_state, state);
247
248         wil->recovery_state = state;
249         wake_up_interruptible(&wil->wq);
250 }
251
252 static void wil_fw_error_worker(struct work_struct *work)
253 {
254         struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
255                                                 fw_error_worker);
256         struct wireless_dev *wdev = wil->wdev;
257
258         wil_dbg_misc(wil, "fw error worker\n");
259
260         if (!netif_running(wil_to_ndev(wil))) {
261                 wil_info(wil, "No recovery - interface is down\n");
262                 return;
263         }
264
265         /* increment @recovery_count if less then WIL6210_FW_RECOVERY_TO
266          * passed since last recovery attempt
267          */
268         if (time_is_after_jiffies(wil->last_fw_recovery +
269                                   WIL6210_FW_RECOVERY_TO))
270                 wil->recovery_count++;
271         else
272                 wil->recovery_count = 1; /* fw was alive for a long time */
273
274         if (wil->recovery_count > WIL6210_FW_RECOVERY_RETRIES) {
275                 wil_err(wil, "too many recovery attempts (%d), giving up\n",
276                         wil->recovery_count);
277                 return;
278         }
279
280         wil->last_fw_recovery = jiffies;
281
282         mutex_lock(&wil->mutex);
283         switch (wdev->iftype) {
284         case NL80211_IFTYPE_STATION:
285         case NL80211_IFTYPE_P2P_CLIENT:
286         case NL80211_IFTYPE_MONITOR:
287                 wil_info(wil, "fw error recovery requested (try %d)...\n",
288                          wil->recovery_count);
289                 if (!no_fw_recovery)
290                         wil->recovery_state = fw_recovery_running;
291                 if (0 != wil_wait_for_recovery(wil))
292                         break;
293
294                 __wil_down(wil);
295                 __wil_up(wil);
296                 break;
297         case NL80211_IFTYPE_AP:
298         case NL80211_IFTYPE_P2P_GO:
299                 wil_info(wil, "No recovery for AP-like interface\n");
300                 /* recovery in these modes is done by upper layers */
301                 break;
302         default:
303                 wil_err(wil, "No recovery - unknown interface type %d\n",
304                         wdev->iftype);
305                 break;
306         }
307         mutex_unlock(&wil->mutex);
308 }
309
310 static int wil_find_free_vring(struct wil6210_priv *wil)
311 {
312         int i;
313
314         for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
315                 if (!wil->vring_tx[i].va)
316                         return i;
317         }
318         return -EINVAL;
319 }
320
321 static void wil_connect_worker(struct work_struct *work)
322 {
323         int rc;
324         struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
325                                                 connect_worker);
326         int cid = wil->pending_connect_cid;
327         int ringid = wil_find_free_vring(wil);
328
329         if (cid < 0) {
330                 wil_err(wil, "No connection pending\n");
331                 return;
332         }
333
334         wil_dbg_wmi(wil, "Configure for connection CID %d\n", cid);
335
336         rc = wil_vring_init_tx(wil, ringid, WIL6210_TX_RING_SIZE, cid, 0);
337         wil->pending_connect_cid = -1;
338         if (rc == 0) {
339                 wil->sta[cid].status = wil_sta_connected;
340                 wil_link_on(wil);
341         } else {
342                 wil->sta[cid].status = wil_sta_unused;
343         }
344 }
345
346 int wil_priv_init(struct wil6210_priv *wil)
347 {
348         uint i;
349
350         wil_dbg_misc(wil, "%s()\n", __func__);
351
352         memset(wil->sta, 0, sizeof(wil->sta));
353         for (i = 0; i < WIL6210_MAX_CID; i++)
354                 spin_lock_init(&wil->sta[i].tid_rx_lock);
355
356         mutex_init(&wil->mutex);
357         mutex_init(&wil->wmi_mutex);
358
359         init_completion(&wil->wmi_ready);
360         init_completion(&wil->wmi_call);
361
362         wil->pending_connect_cid = -1;
363         setup_timer(&wil->connect_timer, wil_connect_timer_fn, (ulong)wil);
364         setup_timer(&wil->scan_timer, wil_scan_timer_fn, (ulong)wil);
365
366         INIT_WORK(&wil->connect_worker, wil_connect_worker);
367         INIT_WORK(&wil->disconnect_worker, wil_disconnect_worker);
368         INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
369         INIT_WORK(&wil->fw_error_worker, wil_fw_error_worker);
370
371         INIT_LIST_HEAD(&wil->pending_wmi_ev);
372         spin_lock_init(&wil->wmi_ev_lock);
373         init_waitqueue_head(&wil->wq);
374
375         wil->wmi_wq = create_singlethread_workqueue(WIL_NAME"_wmi");
376         if (!wil->wmi_wq)
377                 return -EAGAIN;
378
379         wil->wmi_wq_conn = create_singlethread_workqueue(WIL_NAME"_connect");
380         if (!wil->wmi_wq_conn) {
381                 destroy_workqueue(wil->wmi_wq);
382                 return -EAGAIN;
383         }
384
385         wil->last_fw_recovery = jiffies;
386         wil->itr_trsh = itr_trsh;
387
388         return 0;
389 }
390
391 /**
392  * wil6210_disconnect - disconnect one connection
393  * @wil: driver context
394  * @bssid: peer to disconnect, NULL to disconnect all
395  * @from_event: whether is invoked from FW event handler
396  *
397  * Disconnect and release associated resources. If invoked not from the
398  * FW event handler, issue WMI command(s) to trigger MAC disconnect.
399  */
400 void wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid,
401                         bool from_event)
402 {
403         wil_dbg_misc(wil, "%s()\n", __func__);
404
405         del_timer_sync(&wil->connect_timer);
406         _wil6210_disconnect(wil, bssid, from_event);
407 }
408
409 void wil_priv_deinit(struct wil6210_priv *wil)
410 {
411         wil_dbg_misc(wil, "%s()\n", __func__);
412
413         wil_set_recovery_state(wil, fw_recovery_idle);
414         del_timer_sync(&wil->scan_timer);
415         cancel_work_sync(&wil->disconnect_worker);
416         cancel_work_sync(&wil->fw_error_worker);
417         mutex_lock(&wil->mutex);
418         wil6210_disconnect(wil, NULL, false);
419         mutex_unlock(&wil->mutex);
420         wmi_event_flush(wil);
421         destroy_workqueue(wil->wmi_wq_conn);
422         destroy_workqueue(wil->wmi_wq);
423 }
424
425 /* target operations */
426 /* register read */
427 #define R(a) ioread32(wil->csr + HOSTADDR(a))
428 /* register write. wmb() to make sure it is completed */
429 #define W(a, v) do { iowrite32(v, wil->csr + HOSTADDR(a)); wmb(); } while (0)
430 /* register set = read, OR, write */
431 #define S(a, v) W(a, R(a) | v)
432 /* register clear = read, AND with inverted, write */
433 #define C(a, v) W(a, R(a) & ~v)
434
435 static inline void wil_halt_cpu(struct wil6210_priv *wil)
436 {
437         W(RGF_USER_USER_CPU_0, BIT_USER_USER_CPU_MAN_RST);
438         W(RGF_USER_MAC_CPU_0,  BIT_USER_MAC_CPU_MAN_RST);
439 }
440
441 static inline void wil_release_cpu(struct wil6210_priv *wil)
442 {
443         /* Start CPU */
444         W(RGF_USER_USER_CPU_0, 1);
445 }
446
447 static int wil_target_reset(struct wil6210_priv *wil)
448 {
449         int delay = 0;
450         u32 x;
451         u32 rev_id;
452         bool is_sparrow = (wil->board->board == WIL_BOARD_SPARROW);
453
454         wil_dbg_misc(wil, "Resetting \"%s\"...\n", wil->board->name);
455
456         wil->hw_version = R(RGF_USER_FW_REV_ID);
457         rev_id = wil->hw_version & 0xff;
458
459         /* Clear MAC link up */
460         S(RGF_HP_CTRL, BIT(15));
461         S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_HPAL_PERST_FROM_PAD);
462         S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_CAR_PERST_RST);
463
464         wil_halt_cpu(wil);
465
466         if (is_sparrow) {
467                 S(RGF_CAF_OSC_CONTROL, BIT_CAF_OSC_XTAL_EN);
468                 /* XTAL stabilization should take about 3ms */
469                 usleep_range(5000, 7000);
470                 x = R(RGF_CAF_PLL_LOCK_STATUS);
471                 if (!(x & BIT_CAF_OSC_DIG_XTAL_STABLE)) {
472                         wil_err(wil, "Xtal stabilization timeout\n"
473                                 "RGF_CAF_PLL_LOCK_STATUS = 0x%08x\n", x);
474                         return -ETIME;
475                 }
476                 /* switch 10k to XTAL*/
477                 C(RGF_USER_SPARROW_M_4, BIT_SPARROW_M_4_SEL_SLEEP_OR_REF);
478                 /* 40 MHz */
479                 C(RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_CAR_AHB_SW_SEL);
480
481                 W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x3ff81f);
482                 W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0xf);
483         }
484
485         W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
486         W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
487         W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, is_sparrow ? 0x000000f0 : 0x00000170);
488         W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FE00);
489
490         if (is_sparrow) {
491                 W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x0);
492                 W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0x0);
493         }
494
495         W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
496         W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
497         W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
498         W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
499
500         if (is_sparrow) {
501                 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000003);
502                 /* reset A2 PCIE AHB */
503                 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000);
504         } else {
505                 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000001);
506                 if (rev_id == 1) {
507                         /* reset A1 BOTH PCIE AHB & PCIE RGF */
508                         W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00000080);
509                 } else {
510                         W(RGF_PCIE_LOS_COUNTER_CTL, BIT(6) | BIT(8));
511                         W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000);
512                 }
513         }
514
515         /* TODO: check order here!!! Erez code is different */
516         W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
517
518         /* wait until device ready. typical time is 200..250 msec */
519         do {
520                 msleep(RST_DELAY);
521                 x = R(RGF_USER_HW_MACHINE_STATE);
522                 if (delay++ > RST_COUNT) {
523                         wil_err(wil, "Reset not completed, hw_state 0x%08x\n",
524                                 x);
525                         return -ETIME;
526                 }
527         } while (x != HW_MACHINE_BOOT_DONE);
528
529         /* TODO: Erez check rev_id != 1 */
530         if (!is_sparrow && (rev_id != 1))
531                 W(RGF_PCIE_LOS_COUNTER_CTL, BIT(8));
532
533         C(RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
534
535         wil_dbg_misc(wil, "Reset completed in %d ms\n", delay * RST_DELAY);
536         return 0;
537 }
538
539 /**
540  * wil_set_itr_trsh: - apply interrupt coalescing params
541  */
542 void wil_set_itr_trsh(struct wil6210_priv *wil)
543 {
544         /* disable, use usec resolution */
545         W(RGF_DMA_ITR_CNT_CRL, BIT_DMA_ITR_CNT_CRL_EXT_TICK);
546
547         /* disable interrupt moderation for monitor
548          * to get better timestamp precision
549          */
550         if (wil->wdev->iftype == NL80211_IFTYPE_MONITOR)
551                 return;
552
553         wil_info(wil, "set ITR_TRSH = %d usec\n", wil->itr_trsh);
554         W(RGF_DMA_ITR_CNT_TRSH, wil->itr_trsh);
555         W(RGF_DMA_ITR_CNT_CRL, BIT_DMA_ITR_CNT_CRL_EN |
556           BIT_DMA_ITR_CNT_CRL_EXT_TICK); /* start it */
557 }
558
559 #undef R
560 #undef W
561 #undef S
562 #undef C
563
564 void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
565 {
566         le32_to_cpus(&r->base);
567         le16_to_cpus(&r->entry_size);
568         le16_to_cpus(&r->size);
569         le32_to_cpus(&r->tail);
570         le32_to_cpus(&r->head);
571 }
572
573 static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
574 {
575         ulong to = msecs_to_jiffies(1000);
576         ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
577
578         if (0 == left) {
579                 wil_err(wil, "Firmware not ready\n");
580                 return -ETIME;
581         } else {
582                 wil_info(wil, "FW ready after %d ms. HW version 0x%08x\n",
583                          jiffies_to_msecs(to-left), wil->hw_version);
584         }
585         return 0;
586 }
587
588 /*
589  * We reset all the structures, and we reset the UMAC.
590  * After calling this routine, you're expected to reload
591  * the firmware.
592  */
593 int wil_reset(struct wil6210_priv *wil)
594 {
595         int rc;
596
597         wil_dbg_misc(wil, "%s()\n", __func__);
598
599         WARN_ON(!mutex_is_locked(&wil->mutex));
600         WARN_ON(test_bit(wil_status_napi_en, &wil->status));
601
602         cancel_work_sync(&wil->disconnect_worker);
603         wil6210_disconnect(wil, NULL, false);
604
605         wil->status = 0; /* prevent NAPI from being scheduled */
606
607         if (wil->scan_request) {
608                 wil_dbg_misc(wil, "Abort scan_request 0x%p\n",
609                              wil->scan_request);
610                 del_timer_sync(&wil->scan_timer);
611                 cfg80211_scan_done(wil->scan_request, true);
612                 wil->scan_request = NULL;
613         }
614
615         wil_mask_irq(wil);
616
617         wmi_event_flush(wil);
618
619         flush_workqueue(wil->wmi_wq_conn);
620         flush_workqueue(wil->wmi_wq);
621
622         rc = wil_target_reset(wil);
623         wil_rx_fini(wil);
624         if (rc)
625                 return rc;
626
627         if (!no_fw_load) {
628                 wil_info(wil, "Use firmware <%s>\n", WIL_FW_NAME);
629                 wil_halt_cpu(wil);
630                 /* Loading f/w from the file */
631                 rc = wil_request_firmware(wil, WIL_FW_NAME);
632                 if (rc)
633                         return rc;
634
635                 /* clear any interrupts which on-card-firmware may have set */
636                 wil6210_clear_irq(wil);
637                 { /* CAF_ICR - clear and mask */
638                         u32 a = HOSTADDR(RGF_CAF_ICR) +
639                                 offsetof(struct RGF_ICR, ICR);
640                         u32 m = HOSTADDR(RGF_CAF_ICR) +
641                                 offsetof(struct RGF_ICR, IMV);
642                         u32 icr = ioread32(wil->csr + a);
643
644                         iowrite32(icr, wil->csr + a); /* W1C */
645                         iowrite32(~0, wil->csr + m);
646                         wmb(); /* wait for completion */
647                 }
648                 wil_release_cpu(wil);
649         } else {
650                 wil_info(wil, "Use firmware from on-card flash\n");
651         }
652
653         /* init after reset */
654         wil->pending_connect_cid = -1;
655         reinit_completion(&wil->wmi_ready);
656         reinit_completion(&wil->wmi_call);
657
658         wil_unmask_irq(wil);
659
660         /* we just started MAC, wait for FW ready */
661         rc = wil_wait_for_fw_ready(wil);
662
663         return rc;
664 }
665
666 void wil_fw_error_recovery(struct wil6210_priv *wil)
667 {
668         wil_dbg_misc(wil, "starting fw error recovery\n");
669         wil->recovery_state = fw_recovery_pending;
670         schedule_work(&wil->fw_error_worker);
671 }
672
673 void wil_link_on(struct wil6210_priv *wil)
674 {
675         struct net_device *ndev = wil_to_ndev(wil);
676
677         wil_dbg_misc(wil, "%s()\n", __func__);
678
679         netif_carrier_on(ndev);
680         wil_dbg_misc(wil, "netif_tx_wake : link on\n");
681         netif_tx_wake_all_queues(ndev);
682 }
683
684 void wil_link_off(struct wil6210_priv *wil)
685 {
686         struct net_device *ndev = wil_to_ndev(wil);
687
688         wil_dbg_misc(wil, "%s()\n", __func__);
689
690         netif_tx_stop_all_queues(ndev);
691         wil_dbg_misc(wil, "netif_tx_stop : link off\n");
692         netif_carrier_off(ndev);
693 }
694
695 int __wil_up(struct wil6210_priv *wil)
696 {
697         struct net_device *ndev = wil_to_ndev(wil);
698         struct wireless_dev *wdev = wil->wdev;
699         int rc;
700
701         WARN_ON(!mutex_is_locked(&wil->mutex));
702
703         rc = wil_reset(wil);
704         if (rc)
705                 return rc;
706
707         /* Rx VRING. After MAC and beacon */
708         rc = wil_rx_init(wil);
709         if (rc)
710                 return rc;
711
712         switch (wdev->iftype) {
713         case NL80211_IFTYPE_STATION:
714                 wil_dbg_misc(wil, "type: STATION\n");
715                 ndev->type = ARPHRD_ETHER;
716                 break;
717         case NL80211_IFTYPE_AP:
718                 wil_dbg_misc(wil, "type: AP\n");
719                 ndev->type = ARPHRD_ETHER;
720                 break;
721         case NL80211_IFTYPE_P2P_CLIENT:
722                 wil_dbg_misc(wil, "type: P2P_CLIENT\n");
723                 ndev->type = ARPHRD_ETHER;
724                 break;
725         case NL80211_IFTYPE_P2P_GO:
726                 wil_dbg_misc(wil, "type: P2P_GO\n");
727                 ndev->type = ARPHRD_ETHER;
728                 break;
729         case NL80211_IFTYPE_MONITOR:
730                 wil_dbg_misc(wil, "type: Monitor\n");
731                 ndev->type = ARPHRD_IEEE80211_RADIOTAP;
732                 /* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
733                 break;
734         default:
735                 return -EOPNOTSUPP;
736         }
737
738         /* MAC address - pre-requisite for other commands */
739         wmi_set_mac_address(wil, ndev->dev_addr);
740
741         wil_dbg_misc(wil, "NAPI enable\n");
742         napi_enable(&wil->napi_rx);
743         napi_enable(&wil->napi_tx);
744         set_bit(wil_status_napi_en, &wil->status);
745
746         if (wil->platform_ops.bus_request)
747                 wil->platform_ops.bus_request(wil->platform_handle,
748                                               WIL_MAX_BUS_REQUEST_KBPS);
749
750         return 0;
751 }
752
753 int wil_up(struct wil6210_priv *wil)
754 {
755         int rc;
756
757         wil_dbg_misc(wil, "%s()\n", __func__);
758
759         mutex_lock(&wil->mutex);
760         rc = __wil_up(wil);
761         mutex_unlock(&wil->mutex);
762
763         return rc;
764 }
765
766 int __wil_down(struct wil6210_priv *wil)
767 {
768         int iter = WAIT_FOR_DISCONNECT_TIMEOUT_MS /
769                         WAIT_FOR_DISCONNECT_INTERVAL_MS;
770
771         WARN_ON(!mutex_is_locked(&wil->mutex));
772
773         if (wil->platform_ops.bus_request)
774                 wil->platform_ops.bus_request(wil->platform_handle, 0);
775
776         wil_disable_irq(wil);
777         if (test_and_clear_bit(wil_status_napi_en, &wil->status)) {
778                 napi_disable(&wil->napi_rx);
779                 napi_disable(&wil->napi_tx);
780                 wil_dbg_misc(wil, "NAPI disable\n");
781         }
782         wil_enable_irq(wil);
783
784         if (wil->scan_request) {
785                 wil_dbg_misc(wil, "Abort scan_request 0x%p\n",
786                              wil->scan_request);
787                 del_timer_sync(&wil->scan_timer);
788                 cfg80211_scan_done(wil->scan_request, true);
789                 wil->scan_request = NULL;
790         }
791
792         if (test_bit(wil_status_fwconnected, &wil->status) ||
793             test_bit(wil_status_fwconnecting, &wil->status))
794                 wmi_send(wil, WMI_DISCONNECT_CMDID, NULL, 0);
795
796         /* make sure wil is idle (not connected) */
797         mutex_unlock(&wil->mutex);
798         while (iter--) {
799                 int idle = !test_bit(wil_status_fwconnected, &wil->status) &&
800                            !test_bit(wil_status_fwconnecting, &wil->status);
801                 if (idle)
802                         break;
803                 msleep(WAIT_FOR_DISCONNECT_INTERVAL_MS);
804         }
805         mutex_lock(&wil->mutex);
806
807         if (!iter)
808                 wil_err(wil, "timeout waiting for idle FW/HW\n");
809
810         wil_rx_fini(wil);
811
812         return 0;
813 }
814
815 int wil_down(struct wil6210_priv *wil)
816 {
817         int rc;
818
819         wil_dbg_misc(wil, "%s()\n", __func__);
820
821         wil_set_recovery_state(wil, fw_recovery_idle);
822         mutex_lock(&wil->mutex);
823         rc = __wil_down(wil);
824         mutex_unlock(&wil->mutex);
825
826         return rc;
827 }
828
829 int wil_find_cid(struct wil6210_priv *wil, const u8 *mac)
830 {
831         int i;
832         int rc = -ENOENT;
833
834         for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
835                 if ((wil->sta[i].status != wil_sta_unused) &&
836                     ether_addr_equal(wil->sta[i].addr, mac)) {
837                         rc = i;
838                         break;
839                 }
840         }
841
842         return rc;
843 }