2 * Copyright (c) 2012-2014 Qualcomm Atheros, Inc.
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.
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.
17 #include <linux/moduleparam.h>
18 #include <linux/if_arp.h>
19 #include <linux/etherdevice.h>
24 static bool no_fw_recovery
;
25 module_param(no_fw_recovery
, bool, S_IRUGO
| S_IWUSR
);
26 MODULE_PARM_DESC(no_fw_recovery
, " disable FW error recovery");
28 #define RST_DELAY (20) /* msec, for loop in @wil_target_reset */
29 #define RST_COUNT (1 + 1000/RST_DELAY) /* round up to be above 1 sec total */
32 * Due to a hardware issue,
33 * one has to read/write to/from NIC in 32-bit chunks;
34 * regular memcpy_fromio and siblings will
35 * not work on 64-bit platform - it uses 64-bit transactions
37 * Force 32-bit transactions to enable NIC on 64-bit platforms
39 * To avoid byte swap on big endian host, __raw_{read|write}l
40 * should be used - {read|write}l would swap bytes to provide
41 * little endian on PCI value in host endianness.
43 void wil_memcpy_fromio_32(void *dst
, const volatile void __iomem
*src
,
47 const volatile u32 __iomem
*s
= src
;
49 /* size_t is unsigned, if (count%4 != 0) it will wrap */
50 for (count
+= 4; count
> 4; count
-= 4)
51 *d
++ = __raw_readl(s
++);
54 void wil_memcpy_toio_32(volatile void __iomem
*dst
, const void *src
,
57 volatile u32 __iomem
*d
= dst
;
60 for (count
+= 4; count
> 4; count
-= 4)
61 __raw_writel(*s
++, d
++);
64 static void wil_disconnect_cid(struct wil6210_priv
*wil
, int cid
)
67 struct net_device
*ndev
= wil_to_ndev(wil
);
68 struct wireless_dev
*wdev
= wil
->wdev
;
69 struct wil_sta_info
*sta
= &wil
->sta
[cid
];
70 wil_dbg_misc(wil
, "%s(CID %d, status %d)\n", __func__
, cid
,
73 sta
->data_port_open
= false;
74 if (sta
->status
!= wil_sta_unused
) {
75 wmi_disconnect_sta(wil
, sta
->addr
, WLAN_REASON_DEAUTH_LEAVING
);
76 switch (wdev
->iftype
) {
77 case NL80211_IFTYPE_AP
:
78 case NL80211_IFTYPE_P2P_GO
:
79 /* AP-like interface */
80 cfg80211_del_sta(ndev
, sta
->addr
, GFP_KERNEL
);
85 sta
->status
= wil_sta_unused
;
88 for (i
= 0; i
< WIL_STA_TID_NUM
; i
++) {
89 struct wil_tid_ampdu_rx
*r
= sta
->tid_rx
[i
];
90 sta
->tid_rx
[i
] = NULL
;
91 wil_tid_ampdu_rx_free(wil
, r
);
93 for (i
= 0; i
< ARRAY_SIZE(wil
->vring_tx
); i
++) {
94 if (wil
->vring2cid_tid
[i
][0] == cid
)
95 wil_vring_fini_tx(wil
, i
);
97 memset(&sta
->stats
, 0, sizeof(sta
->stats
));
100 static void _wil6210_disconnect(struct wil6210_priv
*wil
, const u8
*bssid
)
103 struct net_device
*ndev
= wil_to_ndev(wil
);
104 struct wireless_dev
*wdev
= wil
->wdev
;
108 cid
= wil_find_cid(wil
, bssid
);
109 wil_dbg_misc(wil
, "%s(%pM, CID %d)\n", __func__
, bssid
, cid
);
111 wil_dbg_misc(wil
, "%s(all)\n", __func__
);
114 if (cid
>= 0) /* disconnect 1 peer */
115 wil_disconnect_cid(wil
, cid
);
116 else /* disconnect all */
117 for (cid
= 0; cid
< WIL6210_MAX_CID
; cid
++)
118 wil_disconnect_cid(wil
, cid
);
121 switch (wdev
->iftype
) {
122 case NL80211_IFTYPE_STATION
:
123 case NL80211_IFTYPE_P2P_CLIENT
:
125 if (test_bit(wil_status_fwconnected
, &wil
->status
)) {
126 clear_bit(wil_status_fwconnected
, &wil
->status
);
127 cfg80211_disconnected(ndev
,
128 WLAN_STATUS_UNSPECIFIED_FAILURE
,
129 NULL
, 0, GFP_KERNEL
);
130 } else if (test_bit(wil_status_fwconnecting
, &wil
->status
)) {
131 cfg80211_connect_result(ndev
, bssid
, NULL
, 0, NULL
, 0,
132 WLAN_STATUS_UNSPECIFIED_FAILURE
,
135 clear_bit(wil_status_fwconnecting
, &wil
->status
);
142 static void wil_disconnect_worker(struct work_struct
*work
)
144 struct wil6210_priv
*wil
= container_of(work
,
145 struct wil6210_priv
, disconnect_worker
);
147 mutex_lock(&wil
->mutex
);
148 _wil6210_disconnect(wil
, NULL
);
149 mutex_unlock(&wil
->mutex
);
152 static void wil_connect_timer_fn(ulong x
)
154 struct wil6210_priv
*wil
= (void *)x
;
156 wil_dbg_misc(wil
, "Connect timeout\n");
158 /* reschedule to thread context - disconnect won't
159 * run from atomic context
161 schedule_work(&wil
->disconnect_worker
);
164 static void wil_scan_timer_fn(ulong x
)
166 struct wil6210_priv
*wil
= (void *)x
;
168 clear_bit(wil_status_fwready
, &wil
->status
);
169 wil_err(wil
, "Scan timeout detected, start fw error recovery\n");
170 schedule_work(&wil
->fw_error_worker
);
173 static void wil_fw_error_worker(struct work_struct
*work
)
175 struct wil6210_priv
*wil
= container_of(work
,
176 struct wil6210_priv
, fw_error_worker
);
177 struct wireless_dev
*wdev
= wil
->wdev
;
179 wil_dbg_misc(wil
, "fw error worker\n");
184 /* increment @recovery_count if less then WIL6210_FW_RECOVERY_TO
185 * passed since last recovery attempt
187 if (time_is_after_jiffies(wil
->last_fw_recovery
+
188 WIL6210_FW_RECOVERY_TO
))
189 wil
->recovery_count
++;
191 wil
->recovery_count
= 1; /* fw was alive for a long time */
193 if (wil
->recovery_count
> WIL6210_FW_RECOVERY_RETRIES
) {
194 wil_err(wil
, "too many recovery attempts (%d), giving up\n",
195 wil
->recovery_count
);
199 wil
->last_fw_recovery
= jiffies
;
201 mutex_lock(&wil
->mutex
);
202 switch (wdev
->iftype
) {
203 case NL80211_IFTYPE_STATION
:
204 case NL80211_IFTYPE_P2P_CLIENT
:
205 case NL80211_IFTYPE_MONITOR
:
206 wil_info(wil
, "fw error recovery started (try %d)...\n",
207 wil
->recovery_count
);
210 /* need to re-allocate Rx ring after reset */
213 case NL80211_IFTYPE_AP
:
214 case NL80211_IFTYPE_P2P_GO
:
215 /* recovery in these modes is done by upper layers */
220 mutex_unlock(&wil
->mutex
);
223 static int wil_find_free_vring(struct wil6210_priv
*wil
)
226 for (i
= 0; i
< WIL6210_MAX_TX_RINGS
; i
++) {
227 if (!wil
->vring_tx
[i
].va
)
233 static void wil_connect_worker(struct work_struct
*work
)
236 struct wil6210_priv
*wil
= container_of(work
, struct wil6210_priv
,
238 int cid
= wil
->pending_connect_cid
;
239 int ringid
= wil_find_free_vring(wil
);
242 wil_err(wil
, "No connection pending\n");
246 wil_dbg_wmi(wil
, "Configure for connection CID %d\n", cid
);
248 rc
= wil_vring_init_tx(wil
, ringid
, WIL6210_TX_RING_SIZE
, cid
, 0);
249 wil
->pending_connect_cid
= -1;
251 wil
->sta
[cid
].status
= wil_sta_connected
;
254 wil
->sta
[cid
].status
= wil_sta_unused
;
258 int wil_priv_init(struct wil6210_priv
*wil
)
260 wil_dbg_misc(wil
, "%s()\n", __func__
);
262 memset(wil
->sta
, 0, sizeof(wil
->sta
));
264 mutex_init(&wil
->mutex
);
265 mutex_init(&wil
->wmi_mutex
);
267 init_completion(&wil
->wmi_ready
);
269 wil
->pending_connect_cid
= -1;
270 setup_timer(&wil
->connect_timer
, wil_connect_timer_fn
, (ulong
)wil
);
271 setup_timer(&wil
->scan_timer
, wil_scan_timer_fn
, (ulong
)wil
);
273 INIT_WORK(&wil
->connect_worker
, wil_connect_worker
);
274 INIT_WORK(&wil
->disconnect_worker
, wil_disconnect_worker
);
275 INIT_WORK(&wil
->wmi_event_worker
, wmi_event_worker
);
276 INIT_WORK(&wil
->fw_error_worker
, wil_fw_error_worker
);
278 INIT_LIST_HEAD(&wil
->pending_wmi_ev
);
279 spin_lock_init(&wil
->wmi_ev_lock
);
281 wil
->wmi_wq
= create_singlethread_workqueue(WIL_NAME
"_wmi");
285 wil
->wmi_wq_conn
= create_singlethread_workqueue(WIL_NAME
"_connect");
286 if (!wil
->wmi_wq_conn
) {
287 destroy_workqueue(wil
->wmi_wq
);
291 wil
->last_fw_recovery
= jiffies
;
296 void wil6210_disconnect(struct wil6210_priv
*wil
, const u8
*bssid
)
298 del_timer_sync(&wil
->connect_timer
);
299 _wil6210_disconnect(wil
, bssid
);
302 void wil_priv_deinit(struct wil6210_priv
*wil
)
304 del_timer_sync(&wil
->scan_timer
);
305 cancel_work_sync(&wil
->disconnect_worker
);
306 cancel_work_sync(&wil
->fw_error_worker
);
307 mutex_lock(&wil
->mutex
);
308 wil6210_disconnect(wil
, NULL
);
309 mutex_unlock(&wil
->mutex
);
310 wmi_event_flush(wil
);
311 destroy_workqueue(wil
->wmi_wq_conn
);
312 destroy_workqueue(wil
->wmi_wq
);
315 static int wil_target_reset(struct wil6210_priv
*wil
)
320 bool is_sparrow
= (wil
->board
->board
== WIL_BOARD_SPARROW
);
322 wil_dbg_misc(wil
, "Resetting \"%s\"...\n", wil
->board
->name
);
325 #define R(a) ioread32(wil->csr + HOSTADDR(a))
327 #define W(a, v) iowrite32(v, wil->csr + HOSTADDR(a))
328 /* register set = read, OR, write */
329 #define S(a, v) W(a, R(a) | v)
330 /* register clear = read, AND with inverted, write */
331 #define C(a, v) W(a, R(a) & ~v)
333 wmb(); /* If host reorder writes here -> race in NIC */
334 W(RGF_USER_MAC_CPU_0
, BIT(1)); /* mac_cpu_man_rst */
335 wil
->hw_version
= R(RGF_USER_FW_REV_ID
);
336 rev_id
= wil
->hw_version
& 0xff;
338 /* Clear MAC link up */
339 S(RGF_HP_CTRL
, BIT(15));
340 /* hpal_perst_from_pad_src_n_mask */
341 S(RGF_USER_CLKS_CTL_SW_RST_MASK_0
, BIT(6));
342 /* car_perst_rst_src_n_mask */
343 S(RGF_USER_CLKS_CTL_SW_RST_MASK_0
, BIT(7));
344 wmb(); /* order is important here */
347 W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0
, 0x3ff81f);
348 wmb(); /* order is important here */
351 W(RGF_USER_USER_CPU_0
, BIT(1)); /* user_cpu_man_rst */
352 wmb(); /* If host reorder writes here -> race in NIC */
353 W(RGF_USER_MAC_CPU_0
, BIT(1)); /* mac_cpu_man_rst */
354 wmb(); /* order is important here */
356 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2
, 0xFE000000);
357 W(RGF_USER_CLKS_CTL_SW_RST_VEC_1
, 0x0000003F);
358 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3
, is_sparrow
? 0x000000B0 : 0x00000170);
359 W(RGF_USER_CLKS_CTL_SW_RST_VEC_0
, 0xFFE7FC00);
360 wmb(); /* order is important here */
363 W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0
, 0x0);
364 wmb(); /* order is important here */
367 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3
, 0);
368 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2
, 0);
369 W(RGF_USER_CLKS_CTL_SW_RST_VEC_1
, 0);
370 W(RGF_USER_CLKS_CTL_SW_RST_VEC_0
, 0);
371 wmb(); /* order is important here */
374 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3
, 0x00000003);
375 /* reset A2 PCIE AHB */
376 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2
, 0x00008000);
379 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3
, 0x00000001);
381 /* reset A1 BOTH PCIE AHB & PCIE RGF */
382 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2
, 0x00000080);
384 W(RGF_PCIE_LOS_COUNTER_CTL
, BIT(6) | BIT(8));
385 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2
, 0x00008000);
390 /* TODO: check order here!!! Erez code is different */
391 W(RGF_USER_CLKS_CTL_SW_RST_VEC_0
, 0);
392 wmb(); /* order is important here */
394 /* wait until device ready. typical time is 200..250 msec */
397 hw_state
= R(RGF_USER_HW_MACHINE_STATE
);
398 if (delay
++ > RST_COUNT
) {
399 wil_err(wil
, "Reset not completed, hw_state 0x%08x\n",
403 } while (hw_state
!= HW_MACHINE_BOOT_DONE
);
405 /* TODO: Erez check rev_id != 1 */
406 if (!is_sparrow
&& (rev_id
!= 1))
407 W(RGF_PCIE_LOS_COUNTER_CTL
, BIT(8));
409 C(RGF_USER_CLKS_CTL_0
, BIT_USER_CLKS_RST_PWGD
);
410 wmb(); /* order is important here */
412 wil_dbg_misc(wil
, "Reset completed in %d ms\n", delay
* RST_DELAY
);
421 void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring
*r
)
423 le32_to_cpus(&r
->base
);
424 le16_to_cpus(&r
->entry_size
);
425 le16_to_cpus(&r
->size
);
426 le32_to_cpus(&r
->tail
);
427 le32_to_cpus(&r
->head
);
430 static int wil_wait_for_fw_ready(struct wil6210_priv
*wil
)
432 ulong to
= msecs_to_jiffies(1000);
433 ulong left
= wait_for_completion_timeout(&wil
->wmi_ready
, to
);
435 wil_err(wil
, "Firmware not ready\n");
438 wil_info(wil
, "FW ready after %d ms. HW version 0x%08x\n",
439 jiffies_to_msecs(to
-left
), wil
->hw_version
);
445 * We reset all the structures, and we reset the UMAC.
446 * After calling this routine, you're expected to reload
449 int wil_reset(struct wil6210_priv
*wil
)
453 WARN_ON(!mutex_is_locked(&wil
->mutex
));
455 cancel_work_sync(&wil
->disconnect_worker
);
456 wil6210_disconnect(wil
, NULL
);
458 wil
->status
= 0; /* prevent NAPI from being scheduled */
459 if (test_bit(wil_status_napi_en
, &wil
->status
)) {
460 napi_synchronize(&wil
->napi_rx
);
463 if (wil
->scan_request
) {
464 wil_dbg_misc(wil
, "Abort scan_request 0x%p\n",
466 del_timer_sync(&wil
->scan_timer
);
467 cfg80211_scan_done(wil
->scan_request
, true);
468 wil
->scan_request
= NULL
;
471 wil6210_disable_irq(wil
);
473 wmi_event_flush(wil
);
475 flush_workqueue(wil
->wmi_wq_conn
);
476 flush_workqueue(wil
->wmi_wq
);
478 rc
= wil_target_reset(wil
);
484 /* init after reset */
485 wil
->pending_connect_cid
= -1;
486 reinit_completion(&wil
->wmi_ready
);
488 /* TODO: release MAC reset */
489 wil6210_enable_irq(wil
);
491 /* we just started MAC, wait for FW ready */
492 rc
= wil_wait_for_fw_ready(wil
);
497 void wil_fw_error_recovery(struct wil6210_priv
*wil
)
499 wil_dbg_misc(wil
, "starting fw error recovery\n");
500 schedule_work(&wil
->fw_error_worker
);
503 void wil_link_on(struct wil6210_priv
*wil
)
505 struct net_device
*ndev
= wil_to_ndev(wil
);
507 wil_dbg_misc(wil
, "%s()\n", __func__
);
509 netif_carrier_on(ndev
);
510 wil_dbg_misc(wil
, "netif_tx_wake : link on\n");
511 netif_tx_wake_all_queues(ndev
);
514 void wil_link_off(struct wil6210_priv
*wil
)
516 struct net_device
*ndev
= wil_to_ndev(wil
);
518 wil_dbg_misc(wil
, "%s()\n", __func__
);
520 netif_tx_stop_all_queues(ndev
);
521 wil_dbg_misc(wil
, "netif_tx_stop : link off\n");
522 netif_carrier_off(ndev
);
525 static int __wil_up(struct wil6210_priv
*wil
)
527 struct net_device
*ndev
= wil_to_ndev(wil
);
528 struct wireless_dev
*wdev
= wil
->wdev
;
531 WARN_ON(!mutex_is_locked(&wil
->mutex
));
537 /* Rx VRING. After MAC and beacon */
538 rc
= wil_rx_init(wil
);
542 switch (wdev
->iftype
) {
543 case NL80211_IFTYPE_STATION
:
544 wil_dbg_misc(wil
, "type: STATION\n");
545 ndev
->type
= ARPHRD_ETHER
;
547 case NL80211_IFTYPE_AP
:
548 wil_dbg_misc(wil
, "type: AP\n");
549 ndev
->type
= ARPHRD_ETHER
;
551 case NL80211_IFTYPE_P2P_CLIENT
:
552 wil_dbg_misc(wil
, "type: P2P_CLIENT\n");
553 ndev
->type
= ARPHRD_ETHER
;
555 case NL80211_IFTYPE_P2P_GO
:
556 wil_dbg_misc(wil
, "type: P2P_GO\n");
557 ndev
->type
= ARPHRD_ETHER
;
559 case NL80211_IFTYPE_MONITOR
:
560 wil_dbg_misc(wil
, "type: Monitor\n");
561 ndev
->type
= ARPHRD_IEEE80211_RADIOTAP
;
562 /* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
568 /* MAC address - pre-requisite for other commands */
569 wmi_set_mac_address(wil
, ndev
->dev_addr
);
572 napi_enable(&wil
->napi_rx
);
573 napi_enable(&wil
->napi_tx
);
574 set_bit(wil_status_napi_en
, &wil
->status
);
579 int wil_up(struct wil6210_priv
*wil
)
583 mutex_lock(&wil
->mutex
);
585 mutex_unlock(&wil
->mutex
);
590 static int __wil_down(struct wil6210_priv
*wil
)
592 WARN_ON(!mutex_is_locked(&wil
->mutex
));
594 clear_bit(wil_status_napi_en
, &wil
->status
);
595 napi_disable(&wil
->napi_rx
);
596 napi_disable(&wil
->napi_tx
);
598 if (wil
->scan_request
) {
599 wil_dbg_misc(wil
, "Abort scan_request 0x%p\n",
601 del_timer_sync(&wil
->scan_timer
);
602 cfg80211_scan_done(wil
->scan_request
, true);
603 wil
->scan_request
= NULL
;
606 wil6210_disconnect(wil
, NULL
);
612 int wil_down(struct wil6210_priv
*wil
)
616 mutex_lock(&wil
->mutex
);
617 rc
= __wil_down(wil
);
618 mutex_unlock(&wil
->mutex
);
623 int wil_find_cid(struct wil6210_priv
*wil
, const u8
*mac
)
628 for (i
= 0; i
< ARRAY_SIZE(wil
->sta
); i
++) {
629 if ((wil
->sta
[i
].status
!= wil_sta_unused
) &&
630 ether_addr_equal(wil
->sta
[i
].addr
, mac
)) {