wil6210: re-submit Rx frames to the wireless media if appropriate
[deliverable/linux.git] / drivers / net / wireless / ath / wil6210 / main.c
CommitLineData
2be7d22f 1/*
1f80af2e 2 * Copyright (c) 2012-2015 Qualcomm Atheros, Inc.
2be7d22f
VK
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
2be7d22f
VK
17#include <linux/moduleparam.h>
18#include <linux/if_arp.h>
108d1eb6 19#include <linux/etherdevice.h>
2be7d22f
VK
20
21#include "wil6210.h"
b4490f42 22#include "txrx.h"
f172b563
DL
23#include "wmi.h"
24
25#define WAIT_FOR_DISCONNECT_TIMEOUT_MS 2000
26#define WAIT_FOR_DISCONNECT_INTERVAL_MS 10
2be7d22f 27
c33407a8 28bool no_fw_recovery;
ed6f9dc6 29module_param(no_fw_recovery, bool, S_IRUGO | S_IWUSR);
c33407a8 30MODULE_PARM_DESC(no_fw_recovery, " disable automatic FW error recovery");
ed6f9dc6 31
ab954628
VK
32/* if not set via modparam, will be set to default value of 1/8 of
33 * rx ring size during init flow
34 */
35unsigned short rx_ring_overflow_thrsh = WIL6210_RX_HIGH_TRSH_INIT;
36module_param(rx_ring_overflow_thrsh, ushort, S_IRUGO);
37MODULE_PARM_DESC(rx_ring_overflow_thrsh,
38 " RX ring overflow threshold in descriptors.");
b6b1b0ec 39
9a06bec9
VK
40/* We allow allocation of more than 1 page buffers to support large packets.
41 * It is suboptimal behavior performance wise in case MTU above page size.
42 */
c44690a1 43unsigned int mtu_max = TXRX_BUF_LEN_DEFAULT - WIL_MAX_MPDU_OVERHEAD;
9a06bec9
VK
44static int mtu_max_set(const char *val, const struct kernel_param *kp)
45{
46 int ret;
47
48 /* sets mtu_max directly. no need to restore it in case of
49 * illegal value since we assume this will fail insmod
50 */
51 ret = param_set_uint(val, kp);
52 if (ret)
53 return ret;
54
4590d812 55 if (mtu_max < 68 || mtu_max > WIL_MAX_ETH_MTU)
9a06bec9
VK
56 ret = -EINVAL;
57
58 return ret;
59}
60
61static struct kernel_param_ops mtu_max_ops = {
62 .set = mtu_max_set,
63 .get = param_get_uint,
64};
65
66module_param_cb(mtu_max, &mtu_max_ops, &mtu_max, S_IRUGO);
67MODULE_PARM_DESC(mtu_max, " Max MTU value.");
68
d3762b40
VK
69static uint rx_ring_order = WIL_RX_RING_SIZE_ORDER_DEFAULT;
70static uint tx_ring_order = WIL_TX_RING_SIZE_ORDER_DEFAULT;
71
72static int ring_order_set(const char *val, const struct kernel_param *kp)
73{
74 int ret;
75 uint x;
76
77 ret = kstrtouint(val, 0, &x);
78 if (ret)
79 return ret;
80
81 if ((x < WIL_RING_SIZE_ORDER_MIN) || (x > WIL_RING_SIZE_ORDER_MAX))
82 return -EINVAL;
83
84 *((uint *)kp->arg) = x;
85
86 return 0;
87}
88
89static struct kernel_param_ops ring_order_ops = {
90 .set = ring_order_set,
91 .get = param_get_uint,
92};
93
94module_param_cb(rx_ring_order, &ring_order_ops, &rx_ring_order, S_IRUGO);
95MODULE_PARM_DESC(rx_ring_order, " Rx ring order; size = 1 << order");
96module_param_cb(tx_ring_order, &ring_order_ops, &tx_ring_order, S_IRUGO);
97MODULE_PARM_DESC(tx_ring_order, " Tx ring order; size = 1 << order");
98
520d68e7
VK
99#define RST_DELAY (20) /* msec, for loop in @wil_target_reset */
100#define RST_COUNT (1 + 1000/RST_DELAY) /* round up to be above 1 sec total */
101
2be7d22f
VK
102/*
103 * Due to a hardware issue,
104 * one has to read/write to/from NIC in 32-bit chunks;
105 * regular memcpy_fromio and siblings will
106 * not work on 64-bit platform - it uses 64-bit transactions
107 *
108 * Force 32-bit transactions to enable NIC on 64-bit platforms
109 *
110 * To avoid byte swap on big endian host, __raw_{read|write}l
111 * should be used - {read|write}l would swap bytes to provide
112 * little endian on PCI value in host endianness.
113 */
114void wil_memcpy_fromio_32(void *dst, const volatile void __iomem *src,
115 size_t count)
116{
117 u32 *d = dst;
118 const volatile u32 __iomem *s = src;
119
120 /* size_t is unsigned, if (count%4 != 0) it will wrap */
121 for (count += 4; count > 4; count -= 4)
122 *d++ = __raw_readl(s++);
123}
124
125void wil_memcpy_toio_32(volatile void __iomem *dst, const void *src,
126 size_t count)
127{
128 volatile u32 __iomem *d = dst;
129 const u32 *s = src;
130
131 for (count += 4; count > 4; count -= 4)
132 __raw_writel(*s++, d++);
133}
134
b516fcc5 135static void wil_disconnect_cid(struct wil6210_priv *wil, int cid,
4821e6d8 136 u16 reason_code, bool from_event)
bd33273b 137__acquires(&sta->tid_rx_lock) __releases(&sta->tid_rx_lock)
91886b0b
VK
138{
139 uint i;
fc58f681
VK
140 struct net_device *ndev = wil_to_ndev(wil);
141 struct wireless_dev *wdev = wil->wdev;
91886b0b 142 struct wil_sta_info *sta = &wil->sta[cid];
8fe59627 143
bd33273b 144 might_sleep();
fc58f681
VK
145 wil_dbg_misc(wil, "%s(CID %d, status %d)\n", __func__, cid,
146 sta->status);
4d55a0a1 147
e58c9f70 148 sta->data_port_open = false;
4d55a0a1 149 if (sta->status != wil_sta_unused) {
b516fcc5 150 if (!from_event)
4821e6d8 151 wmi_disconnect_sta(wil, sta->addr, reason_code);
b516fcc5 152
fc58f681
VK
153 switch (wdev->iftype) {
154 case NL80211_IFTYPE_AP:
155 case NL80211_IFTYPE_P2P_GO:
156 /* AP-like interface */
157 cfg80211_del_sta(ndev, sta->addr, GFP_KERNEL);
158 break;
159 default:
160 break;
161 }
4d55a0a1
VK
162 sta->status = wil_sta_unused;
163 }
164
91886b0b 165 for (i = 0; i < WIL_STA_TID_NUM; i++) {
ec81b5ad 166 struct wil_tid_ampdu_rx *r;
ec81b5ad 167
bd33273b 168 spin_lock_bh(&sta->tid_rx_lock);
ec81b5ad
DL
169
170 r = sta->tid_rx[i];
91886b0b
VK
171 sta->tid_rx[i] = NULL;
172 wil_tid_ampdu_rx_free(wil, r);
ec81b5ad 173
bd33273b 174 spin_unlock_bh(&sta->tid_rx_lock);
91886b0b
VK
175 }
176 for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++) {
177 if (wil->vring2cid_tid[i][0] == cid)
178 wil_vring_fini_tx(wil, i);
179 }
180 memset(&sta->stats, 0, sizeof(sta->stats));
181}
182
b516fcc5 183static void _wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid,
4821e6d8 184 u16 reason_code, bool from_event)
2be7d22f 185{
91886b0b 186 int cid = -ENOENT;
2be7d22f 187 struct net_device *ndev = wil_to_ndev(wil);
91886b0b
VK
188 struct wireless_dev *wdev = wil->wdev;
189
190 might_sleep();
100106d7
VK
191 wil_dbg_misc(wil, "%s(bssid=%pM, reason=%d, ev%s)\n", __func__, bssid,
192 reason_code, from_event ? "+" : "-");
193
194 /* Cases are:
195 * - disconnect single STA, still connected
196 * - disconnect single STA, already disconnected
197 * - disconnect all
198 *
199 * For "disconnect all", there are 2 options:
200 * - bssid == NULL
201 * - bssid is our MAC address
202 */
203 if (bssid && memcmp(ndev->dev_addr, bssid, ETH_ALEN)) {
91886b0b 204 cid = wil_find_cid(wil, bssid);
100106d7
VK
205 wil_dbg_misc(wil, "Disconnect %pM, CID=%d, reason=%d\n",
206 bssid, cid, reason_code);
207 if (cid >= 0) /* disconnect 1 peer */
208 wil_disconnect_cid(wil, cid, reason_code, from_event);
209 } else { /* all */
210 wil_dbg_misc(wil, "Disconnect all\n");
91886b0b 211 for (cid = 0; cid < WIL6210_MAX_CID; cid++)
4821e6d8 212 wil_disconnect_cid(wil, cid, reason_code, from_event);
100106d7 213 }
91886b0b
VK
214
215 /* link state */
216 switch (wdev->iftype) {
217 case NL80211_IFTYPE_STATION:
218 case NL80211_IFTYPE_P2P_CLIENT:
c5e96c91
DL
219 netif_tx_stop_all_queues(ndev);
220 netif_carrier_off(ndev);
221
9419b6a2
VK
222 if (test_bit(wil_status_fwconnected, wil->status)) {
223 clear_bit(wil_status_fwconnected, wil->status);
4821e6d8 224 cfg80211_disconnected(ndev, reason_code,
91886b0b 225 NULL, 0, GFP_KERNEL);
9419b6a2 226 } else if (test_bit(wil_status_fwconnecting, wil->status)) {
91886b0b
VK
227 cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
228 WLAN_STATUS_UNSPECIFIED_FAILURE,
229 GFP_KERNEL);
230 }
9419b6a2 231 clear_bit(wil_status_fwconnecting, wil->status);
91886b0b
VK
232 break;
233 default:
91886b0b 234 break;
2be7d22f 235 }
2be7d22f
VK
236}
237
238static void wil_disconnect_worker(struct work_struct *work)
239{
240 struct wil6210_priv *wil = container_of(work,
241 struct wil6210_priv, disconnect_worker);
242
097638a0 243 mutex_lock(&wil->mutex);
4821e6d8 244 _wil6210_disconnect(wil, NULL, WLAN_REASON_UNSPECIFIED, false);
097638a0 245 mutex_unlock(&wil->mutex);
2be7d22f
VK
246}
247
248static void wil_connect_timer_fn(ulong x)
249{
250 struct wil6210_priv *wil = (void *)x;
251
7743882d 252 wil_dbg_misc(wil, "Connect timeout\n");
2be7d22f
VK
253
254 /* reschedule to thread context - disconnect won't
255 * run from atomic context
256 */
257 schedule_work(&wil->disconnect_worker);
258}
259
047e5d74
VK
260static void wil_scan_timer_fn(ulong x)
261{
262 struct wil6210_priv *wil = (void *)x;
263
9419b6a2 264 clear_bit(wil_status_fwready, wil->status);
047e5d74 265 wil_err(wil, "Scan timeout detected, start fw error recovery\n");
60abbb6e 266 wil->recovery_state = fw_recovery_pending;
047e5d74
VK
267 schedule_work(&wil->fw_error_worker);
268}
269
c33407a8
VK
270static int wil_wait_for_recovery(struct wil6210_priv *wil)
271{
272 if (wait_event_interruptible(wil->wq, wil->recovery_state !=
273 fw_recovery_pending)) {
274 wil_err(wil, "Interrupt, canceling recovery\n");
275 return -ERESTARTSYS;
276 }
277 if (wil->recovery_state != fw_recovery_running) {
278 wil_info(wil, "Recovery cancelled\n");
279 return -EINTR;
280 }
281 wil_info(wil, "Proceed with recovery\n");
282 return 0;
283}
284
285void wil_set_recovery_state(struct wil6210_priv *wil, int state)
286{
287 wil_dbg_misc(wil, "%s(%d -> %d)\n", __func__,
288 wil->recovery_state, state);
289
290 wil->recovery_state = state;
291 wake_up_interruptible(&wil->wq);
292}
293
ed6f9dc6
VK
294static void wil_fw_error_worker(struct work_struct *work)
295{
c33407a8
VK
296 struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
297 fw_error_worker);
ed6f9dc6
VK
298 struct wireless_dev *wdev = wil->wdev;
299
300 wil_dbg_misc(wil, "fw error worker\n");
301
cded9369
VK
302 if (!netif_running(wil_to_ndev(wil))) {
303 wil_info(wil, "No recovery - interface is down\n");
304 return;
305 }
306
fc219eed
VK
307 /* increment @recovery_count if less then WIL6210_FW_RECOVERY_TO
308 * passed since last recovery attempt
309 */
310 if (time_is_after_jiffies(wil->last_fw_recovery +
311 WIL6210_FW_RECOVERY_TO))
312 wil->recovery_count++;
313 else
314 wil->recovery_count = 1; /* fw was alive for a long time */
315
316 if (wil->recovery_count > WIL6210_FW_RECOVERY_RETRIES) {
317 wil_err(wil, "too many recovery attempts (%d), giving up\n",
318 wil->recovery_count);
319 return;
320 }
321
322 wil->last_fw_recovery = jiffies;
323
9c3bde56 324 mutex_lock(&wil->mutex);
ed6f9dc6
VK
325 switch (wdev->iftype) {
326 case NL80211_IFTYPE_STATION:
327 case NL80211_IFTYPE_P2P_CLIENT:
328 case NL80211_IFTYPE_MONITOR:
c33407a8 329 wil_info(wil, "fw error recovery requested (try %d)...\n",
fc219eed 330 wil->recovery_count);
c33407a8
VK
331 if (!no_fw_recovery)
332 wil->recovery_state = fw_recovery_running;
333 if (0 != wil_wait_for_recovery(wil))
334 break;
335
73d839ae
VK
336 __wil_down(wil);
337 __wil_up(wil);
ed6f9dc6
VK
338 break;
339 case NL80211_IFTYPE_AP:
340 case NL80211_IFTYPE_P2P_GO:
e240537b 341 wil_info(wil, "No recovery for AP-like interface\n");
ed6f9dc6
VK
342 /* recovery in these modes is done by upper layers */
343 break;
344 default:
e240537b
VK
345 wil_err(wil, "No recovery - unknown interface type %d\n",
346 wdev->iftype);
ed6f9dc6
VK
347 break;
348 }
9c3bde56 349 mutex_unlock(&wil->mutex);
ed6f9dc6
VK
350}
351
9a177384
VK
352static int wil_find_free_vring(struct wil6210_priv *wil)
353{
354 int i;
8fe59627 355
9a177384
VK
356 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
357 if (!wil->vring_tx[i].va)
358 return i;
359 }
360 return -EINVAL;
361}
362
d81079f1
VK
363static void wil_connect_worker(struct work_struct *work)
364{
365 int rc;
366 struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
367 connect_worker);
c5e96c91
DL
368 struct net_device *ndev = wil_to_ndev(wil);
369
d81079f1 370 int cid = wil->pending_connect_cid;
9a177384 371 int ringid = wil_find_free_vring(wil);
d81079f1
VK
372
373 if (cid < 0) {
374 wil_err(wil, "No connection pending\n");
375 return;
376 }
377
378 wil_dbg_wmi(wil, "Configure for connection CID %d\n", cid);
379
d3762b40 380 rc = wil_vring_init_tx(wil, ringid, 1 << tx_ring_order, cid, 0);
d81079f1 381 wil->pending_connect_cid = -1;
3df2cd36
VK
382 if (rc == 0) {
383 wil->sta[cid].status = wil_sta_connected;
c5e96c91 384 netif_tx_wake_all_queues(ndev);
3df2cd36
VK
385 } else {
386 wil->sta[cid].status = wil_sta_unused;
387 }
d81079f1
VK
388}
389
2be7d22f
VK
390int wil_priv_init(struct wil6210_priv *wil)
391{
ec81b5ad
DL
392 uint i;
393
7743882d 394 wil_dbg_misc(wil, "%s()\n", __func__);
2be7d22f 395
3df2cd36 396 memset(wil->sta, 0, sizeof(wil->sta));
ec81b5ad
DL
397 for (i = 0; i < WIL6210_MAX_CID; i++)
398 spin_lock_init(&wil->sta[i].tid_rx_lock);
3df2cd36 399
2be7d22f
VK
400 mutex_init(&wil->mutex);
401 mutex_init(&wil->wmi_mutex);
3277213f 402 mutex_init(&wil->back_rx_mutex);
3a124ed6 403 mutex_init(&wil->back_tx_mutex);
40822a90 404 mutex_init(&wil->probe_client_mutex);
2be7d22f
VK
405
406 init_completion(&wil->wmi_ready);
59502647 407 init_completion(&wil->wmi_call);
2be7d22f
VK
408
409 wil->pending_connect_cid = -1;
410 setup_timer(&wil->connect_timer, wil_connect_timer_fn, (ulong)wil);
047e5d74 411 setup_timer(&wil->scan_timer, wil_scan_timer_fn, (ulong)wil);
2be7d22f 412
d81079f1 413 INIT_WORK(&wil->connect_worker, wil_connect_worker);
2be7d22f
VK
414 INIT_WORK(&wil->disconnect_worker, wil_disconnect_worker);
415 INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
ed6f9dc6 416 INIT_WORK(&wil->fw_error_worker, wil_fw_error_worker);
3277213f 417 INIT_WORK(&wil->back_rx_worker, wil_back_rx_worker);
3a124ed6 418 INIT_WORK(&wil->back_tx_worker, wil_back_tx_worker);
40822a90 419 INIT_WORK(&wil->probe_client_worker, wil_probe_client_worker);
2be7d22f
VK
420
421 INIT_LIST_HEAD(&wil->pending_wmi_ev);
3277213f 422 INIT_LIST_HEAD(&wil->back_rx_pending);
3a124ed6 423 INIT_LIST_HEAD(&wil->back_tx_pending);
40822a90 424 INIT_LIST_HEAD(&wil->probe_client_pending);
2be7d22f 425 spin_lock_init(&wil->wmi_ev_lock);
c33407a8 426 init_waitqueue_head(&wil->wq);
2be7d22f 427
3277213f 428 wil->wmi_wq = create_singlethread_workqueue(WIL_NAME "_wmi");
2be7d22f
VK
429 if (!wil->wmi_wq)
430 return -EAGAIN;
431
3277213f
VK
432 wil->wq_service = create_singlethread_workqueue(WIL_NAME "_service");
433 if (!wil->wq_service)
434 goto out_wmi_wq;
2be7d22f 435
fc219eed 436 wil->last_fw_recovery = jiffies;
1f80af2e
VS
437 wil->tx_interframe_timeout = WIL6210_ITR_TX_INTERFRAME_TIMEOUT_DEFAULT;
438 wil->rx_interframe_timeout = WIL6210_ITR_RX_INTERFRAME_TIMEOUT_DEFAULT;
439 wil->tx_max_burst_duration = WIL6210_ITR_TX_MAX_BURST_DURATION_DEFAULT;
440 wil->rx_max_burst_duration = WIL6210_ITR_RX_MAX_BURST_DURATION_DEFAULT;
fc219eed 441
ab954628
VK
442 if (rx_ring_overflow_thrsh == WIL6210_RX_HIGH_TRSH_INIT)
443 rx_ring_overflow_thrsh = WIL6210_RX_HIGH_TRSH_DEFAULT;
2be7d22f 444 return 0;
3277213f
VK
445
446out_wmi_wq:
447 destroy_workqueue(wil->wmi_wq);
448
449 return -EAGAIN;
2be7d22f
VK
450}
451
b516fcc5
VK
452/**
453 * wil6210_disconnect - disconnect one connection
454 * @wil: driver context
455 * @bssid: peer to disconnect, NULL to disconnect all
4821e6d8 456 * @reason_code: Reason code for the Disassociation frame
b516fcc5
VK
457 * @from_event: whether is invoked from FW event handler
458 *
459 * Disconnect and release associated resources. If invoked not from the
460 * FW event handler, issue WMI command(s) to trigger MAC disconnect.
461 */
462void wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid,
4821e6d8 463 u16 reason_code, bool from_event)
2be7d22f 464{
9cf10d62
VK
465 wil_dbg_misc(wil, "%s()\n", __func__);
466
2be7d22f 467 del_timer_sync(&wil->connect_timer);
4821e6d8 468 _wil6210_disconnect(wil, bssid, reason_code, from_event);
2be7d22f
VK
469}
470
471void wil_priv_deinit(struct wil6210_priv *wil)
472{
9cf10d62
VK
473 wil_dbg_misc(wil, "%s()\n", __func__);
474
c33407a8 475 wil_set_recovery_state(wil, fw_recovery_idle);
047e5d74 476 del_timer_sync(&wil->scan_timer);
2be7d22f 477 cancel_work_sync(&wil->disconnect_worker);
ed6f9dc6 478 cancel_work_sync(&wil->fw_error_worker);
097638a0 479 mutex_lock(&wil->mutex);
4821e6d8 480 wil6210_disconnect(wil, NULL, WLAN_REASON_DEAUTH_LEAVING, false);
097638a0 481 mutex_unlock(&wil->mutex);
2be7d22f 482 wmi_event_flush(wil);
3277213f
VK
483 wil_back_rx_flush(wil);
484 cancel_work_sync(&wil->back_rx_worker);
3a124ed6
VK
485 wil_back_tx_flush(wil);
486 cancel_work_sync(&wil->back_tx_worker);
40822a90
VK
487 wil_probe_client_flush(wil);
488 cancel_work_sync(&wil->probe_client_worker);
3277213f 489 destroy_workqueue(wil->wq_service);
2be7d22f
VK
490 destroy_workqueue(wil->wmi_wq);
491}
492
151a9706
VK
493/* target operations */
494/* register read */
495#define R(a) ioread32(wil->csr + HOSTADDR(a))
496/* register write. wmb() to make sure it is completed */
497#define W(a, v) do { iowrite32(v, wil->csr + HOSTADDR(a)); wmb(); } while (0)
498/* register set = read, OR, write */
499#define S(a, v) W(a, R(a) | v)
500/* register clear = read, AND with inverted, write */
501#define C(a, v) W(a, R(a) & ~v)
502
503static inline void wil_halt_cpu(struct wil6210_priv *wil)
504{
505 W(RGF_USER_USER_CPU_0, BIT_USER_USER_CPU_MAN_RST);
506 W(RGF_USER_MAC_CPU_0, BIT_USER_MAC_CPU_MAN_RST);
507}
508
509static inline void wil_release_cpu(struct wil6210_priv *wil)
510{
511 /* Start CPU */
512 W(RGF_USER_USER_CPU_0, 1);
513}
514
bbb2adc7 515static int wil_target_reset(struct wil6210_priv *wil)
2be7d22f 516{
98a65b59 517 int delay = 0;
48516298 518 u32 x;
36b10a72 519
1aeda13b 520 wil_dbg_misc(wil, "Resetting \"%s\"...\n", wil->hw_name);
6508281b
VK
521
522 /* Clear MAC link up */
523 S(RGF_HP_CTRL, BIT(15));
151a9706
VK
524 S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_HPAL_PERST_FROM_PAD);
525 S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT_CAR_PERST_RST);
526
527 wil_halt_cpu(wil);
2be7d22f 528
2cd0f021
VK
529 /* clear all boot loader "ready" bits */
530 W(RGF_USER_BL + offsetof(struct RGF_BL, ready), 0);
cce47711
VK
531 /* Clear Fw Download notification */
532 C(RGF_USER_USAGE_6, BIT(0));
533
9a5511b5
VK
534 S(RGF_CAF_OSC_CONTROL, BIT_CAF_OSC_XTAL_EN);
535 /* XTAL stabilization should take about 3ms */
536 usleep_range(5000, 7000);
537 x = R(RGF_CAF_PLL_LOCK_STATUS);
538 if (!(x & BIT_CAF_OSC_DIG_XTAL_STABLE)) {
539 wil_err(wil, "Xtal stabilization timeout\n"
540 "RGF_CAF_PLL_LOCK_STATUS = 0x%08x\n", x);
541 return -ETIME;
6508281b 542 }
9a5511b5
VK
543 /* switch 10k to XTAL*/
544 C(RGF_USER_SPARROW_M_4, BIT_SPARROW_M_4_SEL_SLEEP_OR_REF);
545 /* 40 MHz */
546 C(RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_CAR_AHB_SW_SEL);
547
548 W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x3ff81f);
549 W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0xf);
6508281b 550
2be7d22f
VK
551 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
552 W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
9a5511b5 553 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x000000f0);
151a9706 554 W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FE00);
2be7d22f 555
9a5511b5
VK
556 W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x0);
557 W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_1, 0x0);
6508281b 558
2be7d22f
VK
559 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
560 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
561 W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
562 W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
563
9a5511b5
VK
564 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000003);
565 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000); /* reset A2 PCIE AHB */
6508281b 566
2be7d22f
VK
567 W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
568
2cd0f021 569 /* wait until device ready. typical time is 20..80 msec */
36b10a72 570 do {
520d68e7 571 msleep(RST_DELAY);
2cd0f021 572 x = R(RGF_USER_BL + offsetof(struct RGF_BL, ready));
520d68e7 573 if (delay++ > RST_COUNT) {
2cd0f021 574 wil_err(wil, "Reset not completed, bl.ready 0x%08x\n",
48516298 575 x);
bbb2adc7 576 return -ETIME;
36b10a72 577 }
2cd0f021 578 } while (!(x & BIT_BL_READY));
36b10a72 579
972072aa
VK
580 C(RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
581
e3351277
VK
582 /* enable fix for HW bug related to the SA/DA swap in AP Rx */
583 S(RGF_DMA_OFUL_NID_0, BIT_DMA_OFUL_NID_0_RX_EXT_TR_EN |
584 BIT_DMA_OFUL_NID_0_RX_EXT_A3_SRC);
585
520d68e7 586 wil_dbg_misc(wil, "Reset completed in %d ms\n", delay * RST_DELAY);
bbb2adc7 587 return 0;
151a9706 588}
2be7d22f 589
2be7d22f
VK
590void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
591{
592 le32_to_cpus(&r->base);
593 le16_to_cpus(&r->entry_size);
594 le16_to_cpus(&r->size);
595 le32_to_cpus(&r->tail);
596 le32_to_cpus(&r->head);
597}
598
2cd0f021
VK
599static int wil_get_bl_info(struct wil6210_priv *wil)
600{
601 struct net_device *ndev = wil_to_ndev(wil);
602 struct RGF_BL bl;
603
604 wil_memcpy_fromio_32(&bl, wil->csr + HOSTADDR(RGF_USER_BL), sizeof(bl));
605 le32_to_cpus(&bl.ready);
606 le32_to_cpus(&bl.version);
607 le32_to_cpus(&bl.rf_type);
608 le32_to_cpus(&bl.baseband_type);
609
610 if (!is_valid_ether_addr(bl.mac_address)) {
611 wil_err(wil, "BL: Invalid MAC %pM\n", bl.mac_address);
612 return -EINVAL;
613 }
614
615 ether_addr_copy(ndev->perm_addr, bl.mac_address);
616 if (!is_valid_ether_addr(ndev->dev_addr))
617 ether_addr_copy(ndev->dev_addr, bl.mac_address);
618 wil_info(wil,
619 "Boot Loader: ver = %d MAC = %pM RF = 0x%08x bband = 0x%08x\n",
620 bl.version, bl.mac_address, bl.rf_type, bl.baseband_type);
621
622 return 0;
623}
624
2be7d22f
VK
625static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
626{
627 ulong to = msecs_to_jiffies(1000);
628 ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
8fe59627 629
2be7d22f
VK
630 if (0 == left) {
631 wil_err(wil, "Firmware not ready\n");
632 return -ETIME;
633 } else {
15e23124
VK
634 wil_info(wil, "FW ready after %d ms. HW version 0x%08x\n",
635 jiffies_to_msecs(to-left), wil->hw_version);
2be7d22f
VK
636 }
637 return 0;
638}
639
640/*
641 * We reset all the structures, and we reset the UMAC.
642 * After calling this routine, you're expected to reload
643 * the firmware.
644 */
2cd0f021 645int wil_reset(struct wil6210_priv *wil, bool load_fw)
2be7d22f
VK
646{
647 int rc;
648
9cf10d62
VK
649 wil_dbg_misc(wil, "%s()\n", __func__);
650
1aeda13b
VK
651 if (wil->hw_version == HW_VER_UNKNOWN)
652 return -ENODEV;
653
097638a0 654 WARN_ON(!mutex_is_locked(&wil->mutex));
9419b6a2 655 WARN_ON(test_bit(wil_status_napi_en, wil->status));
097638a0
VK
656
657 cancel_work_sync(&wil->disconnect_worker);
4821e6d8 658 wil6210_disconnect(wil, NULL, WLAN_REASON_DEAUTH_LEAVING, false);
097638a0 659
9419b6a2
VK
660 /* prevent NAPI from being scheduled */
661 bitmap_zero(wil->status, wil_status_last);
0fef1818 662
ed6f9dc6
VK
663 if (wil->scan_request) {
664 wil_dbg_misc(wil, "Abort scan_request 0x%p\n",
665 wil->scan_request);
047e5d74 666 del_timer_sync(&wil->scan_timer);
ed6f9dc6
VK
667 cfg80211_scan_done(wil->scan_request, true);
668 wil->scan_request = NULL;
669 }
670
e4dbb093 671 wil_mask_irq(wil);
e08b5906 672
2be7d22f
VK
673 wmi_event_flush(wil);
674
3277213f 675 flush_workqueue(wil->wq_service);
e08b5906 676 flush_workqueue(wil->wmi_wq);
2be7d22f 677
bbb2adc7 678 rc = wil_target_reset(wil);
8bf6adb9 679 wil_rx_fini(wil);
bbb2adc7
VK
680 if (rc)
681 return rc;
682
2cd0f021
VK
683 rc = wil_get_bl_info(wil);
684 if (rc)
685 return rc;
686
687 if (load_fw) {
688 wil_info(wil, "Use firmware <%s> + board <%s>\n", WIL_FW_NAME,
689 WIL_FW2_NAME);
690
151a9706
VK
691 wil_halt_cpu(wil);
692 /* Loading f/w from the file */
693 rc = wil_request_firmware(wil, WIL_FW_NAME);
694 if (rc)
695 return rc;
2cd0f021
VK
696 rc = wil_request_firmware(wil, WIL_FW2_NAME);
697 if (rc)
698 return rc;
699
700 /* Mark FW as loaded from host */
701 S(RGF_USER_USAGE_6, 1);
151a9706 702
2cd0f021
VK
703 /* clear any interrupts which on-card-firmware
704 * may have set
705 */
151a9706 706 wil6210_clear_irq(wil);
2cd0f021
VK
707 /* CAF_ICR - clear and mask */
708 /* it is W1C, clear by writing back same value */
709 S(RGF_CAF_ICR + offsetof(struct RGF_ICR, ICR), 0);
710 W(RGF_CAF_ICR + offsetof(struct RGF_ICR, IMV), ~0);
711
151a9706 712 wil_release_cpu(wil);
151a9706 713 }
8bf6adb9 714
2be7d22f
VK
715 /* init after reset */
716 wil->pending_connect_cid = -1;
16735d02 717 reinit_completion(&wil->wmi_ready);
59502647 718 reinit_completion(&wil->wmi_call);
2be7d22f 719
2cd0f021
VK
720 if (load_fw) {
721 wil_configure_interrupt_moderation(wil);
722 wil_unmask_irq(wil);
2be7d22f 723
2cd0f021
VK
724 /* we just started MAC, wait for FW ready */
725 rc = wil_wait_for_fw_ready(wil);
726 }
2be7d22f
VK
727
728 return rc;
729}
730
2cd0f021
VK
731#undef R
732#undef W
733#undef S
734#undef C
735
ed6f9dc6
VK
736void wil_fw_error_recovery(struct wil6210_priv *wil)
737{
738 wil_dbg_misc(wil, "starting fw error recovery\n");
c33407a8 739 wil->recovery_state = fw_recovery_pending;
ed6f9dc6
VK
740 schedule_work(&wil->fw_error_worker);
741}
2be7d22f 742
73d839ae 743int __wil_up(struct wil6210_priv *wil)
2be7d22f
VK
744{
745 struct net_device *ndev = wil_to_ndev(wil);
746 struct wireless_dev *wdev = wil->wdev;
2be7d22f 747 int rc;
2be7d22f 748
097638a0
VK
749 WARN_ON(!mutex_is_locked(&wil->mutex));
750
2cd0f021 751 rc = wil_reset(wil, true);
2be7d22f
VK
752 if (rc)
753 return rc;
754
e31b2562 755 /* Rx VRING. After MAC and beacon */
d3762b40 756 rc = wil_rx_init(wil, 1 << rx_ring_order);
e31b2562
VK
757 if (rc)
758 return rc;
759
2be7d22f
VK
760 switch (wdev->iftype) {
761 case NL80211_IFTYPE_STATION:
7743882d 762 wil_dbg_misc(wil, "type: STATION\n");
2be7d22f
VK
763 ndev->type = ARPHRD_ETHER;
764 break;
765 case NL80211_IFTYPE_AP:
7743882d 766 wil_dbg_misc(wil, "type: AP\n");
2be7d22f
VK
767 ndev->type = ARPHRD_ETHER;
768 break;
769 case NL80211_IFTYPE_P2P_CLIENT:
7743882d 770 wil_dbg_misc(wil, "type: P2P_CLIENT\n");
2be7d22f
VK
771 ndev->type = ARPHRD_ETHER;
772 break;
773 case NL80211_IFTYPE_P2P_GO:
7743882d 774 wil_dbg_misc(wil, "type: P2P_GO\n");
2be7d22f
VK
775 ndev->type = ARPHRD_ETHER;
776 break;
777 case NL80211_IFTYPE_MONITOR:
7743882d 778 wil_dbg_misc(wil, "type: Monitor\n");
2be7d22f
VK
779 ndev->type = ARPHRD_IEEE80211_RADIOTAP;
780 /* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
781 break;
782 default:
783 return -EOPNOTSUPP;
784 }
785
2be7d22f
VK
786 /* MAC address - pre-requisite for other commands */
787 wmi_set_mac_address(wil, ndev->dev_addr);
788
73d839ae 789 wil_dbg_misc(wil, "NAPI enable\n");
e0287c4a
VK
790 napi_enable(&wil->napi_rx);
791 napi_enable(&wil->napi_tx);
9419b6a2 792 set_bit(wil_status_napi_en, wil->status);
e0287c4a 793
f772ebfb
VK
794 if (wil->platform_ops.bus_request)
795 wil->platform_ops.bus_request(wil->platform_handle,
796 WIL_MAX_BUS_REQUEST_KBPS);
797
2be7d22f
VK
798 return 0;
799}
800
801int wil_up(struct wil6210_priv *wil)
802{
803 int rc;
804
9cf10d62
VK
805 wil_dbg_misc(wil, "%s()\n", __func__);
806
2be7d22f
VK
807 mutex_lock(&wil->mutex);
808 rc = __wil_up(wil);
809 mutex_unlock(&wil->mutex);
810
811 return rc;
812}
813
73d839ae 814int __wil_down(struct wil6210_priv *wil)
2be7d22f 815{
f172b563
DL
816 int iter = WAIT_FOR_DISCONNECT_TIMEOUT_MS /
817 WAIT_FOR_DISCONNECT_INTERVAL_MS;
818
097638a0
VK
819 WARN_ON(!mutex_is_locked(&wil->mutex));
820
f772ebfb
VK
821 if (wil->platform_ops.bus_request)
822 wil->platform_ops.bus_request(wil->platform_handle, 0);
823
73d839ae 824 wil_disable_irq(wil);
9419b6a2 825 if (test_and_clear_bit(wil_status_napi_en, wil->status)) {
73d839ae
VK
826 napi_disable(&wil->napi_rx);
827 napi_disable(&wil->napi_tx);
828 wil_dbg_misc(wil, "NAPI disable\n");
829 }
830 wil_enable_irq(wil);
e0287c4a 831
2be7d22f 832 if (wil->scan_request) {
2a91d7d0
VK
833 wil_dbg_misc(wil, "Abort scan_request 0x%p\n",
834 wil->scan_request);
047e5d74 835 del_timer_sync(&wil->scan_timer);
2be7d22f
VK
836 cfg80211_scan_done(wil->scan_request, true);
837 wil->scan_request = NULL;
838 }
839
9419b6a2
VK
840 if (test_bit(wil_status_fwconnected, wil->status) ||
841 test_bit(wil_status_fwconnecting, wil->status))
f172b563
DL
842 wmi_send(wil, WMI_DISCONNECT_CMDID, NULL, 0);
843
844 /* make sure wil is idle (not connected) */
845 mutex_unlock(&wil->mutex);
846 while (iter--) {
9419b6a2
VK
847 int idle = !test_bit(wil_status_fwconnected, wil->status) &&
848 !test_bit(wil_status_fwconnecting, wil->status);
f172b563
DL
849 if (idle)
850 break;
851 msleep(WAIT_FOR_DISCONNECT_INTERVAL_MS);
852 }
853 mutex_lock(&wil->mutex);
854
855 if (!iter)
856 wil_err(wil, "timeout waiting for idle FW/HW\n");
857
2cd0f021 858 wil_reset(wil, false);
2be7d22f
VK
859
860 return 0;
861}
862
863int wil_down(struct wil6210_priv *wil)
864{
865 int rc;
866
9cf10d62
VK
867 wil_dbg_misc(wil, "%s()\n", __func__);
868
c33407a8 869 wil_set_recovery_state(wil, fw_recovery_idle);
2be7d22f
VK
870 mutex_lock(&wil->mutex);
871 rc = __wil_down(wil);
872 mutex_unlock(&wil->mutex);
873
874 return rc;
875}
3df2cd36
VK
876
877int wil_find_cid(struct wil6210_priv *wil, const u8 *mac)
878{
879 int i;
880 int rc = -ENOENT;
881
882 for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
883 if ((wil->sta[i].status != wil_sta_unused) &&
108d1eb6 884 ether_addr_equal(wil->sta[i].addr, mac)) {
3df2cd36
VK
885 rc = i;
886 break;
887 }
888 }
889
890 return rc;
891}
This page took 0.231969 seconds and 5 git commands to generate.