2 * Copyright (c) 2012 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");
29 * Due to a hardware issue,
30 * one has to read/write to/from NIC in 32-bit chunks;
31 * regular memcpy_fromio and siblings will
32 * not work on 64-bit platform - it uses 64-bit transactions
34 * Force 32-bit transactions to enable NIC on 64-bit platforms
36 * To avoid byte swap on big endian host, __raw_{read|write}l
37 * should be used - {read|write}l would swap bytes to provide
38 * little endian on PCI value in host endianness.
40 void wil_memcpy_fromio_32(void *dst, const volatile void __iomem *src,
44 const volatile u32 __iomem *s = src;
46 /* size_t is unsigned, if (count%4 != 0) it will wrap */
47 for (count += 4; count > 4; count -= 4)
48 *d++ = __raw_readl(s++);
51 void wil_memcpy_toio_32(volatile void __iomem *dst, const void *src,
54 volatile u32 __iomem *d = dst;
57 for (count += 4; count > 4; count -= 4)
58 __raw_writel(*s++, d++);
61 static void wil_disconnect_cid(struct wil6210_priv *wil, int cid)
64 struct wil_sta_info *sta = &wil->sta[cid];
66 sta->data_port_open = false;
67 if (sta->status != wil_sta_unused) {
68 wmi_disconnect_sta(wil, sta->addr, WLAN_REASON_DEAUTH_LEAVING);
69 sta->status = wil_sta_unused;
72 for (i = 0; i < WIL_STA_TID_NUM; i++) {
73 struct wil_tid_ampdu_rx *r = sta->tid_rx[i];
74 sta->tid_rx[i] = NULL;
75 wil_tid_ampdu_rx_free(wil, r);
77 for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++) {
78 if (wil->vring2cid_tid[i][0] == cid)
79 wil_vring_fini_tx(wil, i);
81 memset(&sta->stats, 0, sizeof(sta->stats));
84 static void _wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid)
87 struct net_device *ndev = wil_to_ndev(wil);
88 struct wireless_dev *wdev = wil->wdev;
92 cid = wil_find_cid(wil, bssid);
93 wil_dbg_misc(wil, "%s(%pM, CID %d)\n", __func__, bssid, cid);
95 wil_dbg_misc(wil, "%s(all)\n", __func__);
98 if (cid >= 0) /* disconnect 1 peer */
99 wil_disconnect_cid(wil, cid);
100 else /* disconnect all */
101 for (cid = 0; cid < WIL6210_MAX_CID; cid++)
102 wil_disconnect_cid(wil, cid);
105 switch (wdev->iftype) {
106 case NL80211_IFTYPE_STATION:
107 case NL80211_IFTYPE_P2P_CLIENT:
109 if (test_bit(wil_status_fwconnected, &wil->status)) {
110 clear_bit(wil_status_fwconnected, &wil->status);
111 cfg80211_disconnected(ndev,
112 WLAN_STATUS_UNSPECIFIED_FAILURE,
113 NULL, 0, GFP_KERNEL);
114 } else if (test_bit(wil_status_fwconnecting, &wil->status)) {
115 cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
116 WLAN_STATUS_UNSPECIFIED_FAILURE,
119 clear_bit(wil_status_fwconnecting, &wil->status);
122 /* AP-like interface and monitor:
123 * never scan, always connected
126 cfg80211_del_sta(ndev, bssid, GFP_KERNEL);
131 static void wil_disconnect_worker(struct work_struct *work)
133 struct wil6210_priv *wil = container_of(work,
134 struct wil6210_priv, disconnect_worker);
136 mutex_lock(&wil->mutex);
137 _wil6210_disconnect(wil, NULL);
138 mutex_unlock(&wil->mutex);
141 static void wil_connect_timer_fn(ulong x)
143 struct wil6210_priv *wil = (void *)x;
145 wil_dbg_misc(wil, "Connect timeout\n");
147 /* reschedule to thread context - disconnect won't
148 * run from atomic context
150 schedule_work(&wil->disconnect_worker);
153 static void wil_fw_error_worker(struct work_struct *work)
155 struct wil6210_priv *wil = container_of(work,
156 struct wil6210_priv, fw_error_worker);
157 struct wireless_dev *wdev = wil->wdev;
159 wil_dbg_misc(wil, "fw error worker\n");
164 /* increment @recovery_count if less then WIL6210_FW_RECOVERY_TO
165 * passed since last recovery attempt
167 if (time_is_after_jiffies(wil->last_fw_recovery +
168 WIL6210_FW_RECOVERY_TO))
169 wil->recovery_count++;
171 wil->recovery_count = 1; /* fw was alive for a long time */
173 if (wil->recovery_count > WIL6210_FW_RECOVERY_RETRIES) {
174 wil_err(wil, "too many recovery attempts (%d), giving up\n",
175 wil->recovery_count);
179 wil->last_fw_recovery = jiffies;
181 mutex_lock(&wil->mutex);
182 switch (wdev->iftype) {
183 case NL80211_IFTYPE_STATION:
184 case NL80211_IFTYPE_P2P_CLIENT:
185 case NL80211_IFTYPE_MONITOR:
186 wil_info(wil, "fw error recovery started (try %d)...\n",
187 wil->recovery_count);
190 /* need to re-allocate Rx ring after reset */
193 case NL80211_IFTYPE_AP:
194 case NL80211_IFTYPE_P2P_GO:
195 /* recovery in these modes is done by upper layers */
200 mutex_unlock(&wil->mutex);
203 static int wil_find_free_vring(struct wil6210_priv *wil)
206 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
207 if (!wil->vring_tx[i].va)
213 static void wil_connect_worker(struct work_struct *work)
216 struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
218 int cid = wil->pending_connect_cid;
219 int ringid = wil_find_free_vring(wil);
222 wil_err(wil, "No connection pending\n");
226 wil_dbg_wmi(wil, "Configure for connection CID %d\n", cid);
228 rc = wil_vring_init_tx(wil, ringid, WIL6210_TX_RING_SIZE, cid, 0);
229 wil->pending_connect_cid = -1;
231 wil->sta[cid].status = wil_sta_connected;
234 wil->sta[cid].status = wil_sta_unused;
238 int wil_priv_init(struct wil6210_priv *wil)
240 wil_dbg_misc(wil, "%s()\n", __func__);
242 memset(wil->sta, 0, sizeof(wil->sta));
244 mutex_init(&wil->mutex);
245 mutex_init(&wil->wmi_mutex);
247 init_completion(&wil->wmi_ready);
249 wil->pending_connect_cid = -1;
250 setup_timer(&wil->connect_timer, wil_connect_timer_fn, (ulong)wil);
252 INIT_WORK(&wil->connect_worker, wil_connect_worker);
253 INIT_WORK(&wil->disconnect_worker, wil_disconnect_worker);
254 INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
255 INIT_WORK(&wil->fw_error_worker, wil_fw_error_worker);
257 INIT_LIST_HEAD(&wil->pending_wmi_ev);
258 spin_lock_init(&wil->wmi_ev_lock);
260 wil->wmi_wq = create_singlethread_workqueue(WIL_NAME"_wmi");
264 wil->wmi_wq_conn = create_singlethread_workqueue(WIL_NAME"_connect");
265 if (!wil->wmi_wq_conn) {
266 destroy_workqueue(wil->wmi_wq);
270 wil->last_fw_recovery = jiffies;
275 void wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid)
277 del_timer_sync(&wil->connect_timer);
278 _wil6210_disconnect(wil, bssid);
281 void wil_priv_deinit(struct wil6210_priv *wil)
283 cancel_work_sync(&wil->disconnect_worker);
284 cancel_work_sync(&wil->fw_error_worker);
285 mutex_lock(&wil->mutex);
286 wil6210_disconnect(wil, NULL);
287 mutex_unlock(&wil->mutex);
288 wmi_event_flush(wil);
289 destroy_workqueue(wil->wmi_wq_conn);
290 destroy_workqueue(wil->wmi_wq);
293 static void wil_target_reset(struct wil6210_priv *wil)
299 wil_dbg_misc(wil, "Resetting...\n");
302 #define R(a) ioread32(wil->csr + HOSTADDR(a))
304 #define W(a, v) iowrite32(v, wil->csr + HOSTADDR(a))
305 /* register set = read, OR, write */
306 #define S(a, v) W(a, R(a) | v)
307 /* register clear = read, AND with inverted, write */
308 #define C(a, v) W(a, R(a) & ~v)
310 wil->hw_version = R(RGF_USER_FW_REV_ID);
311 rev_id = wil->hw_version & 0xff;
312 /* hpal_perst_from_pad_src_n_mask */
313 S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(6));
314 /* car_perst_rst_src_n_mask */
315 S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(7));
316 wmb(); /* order is important here */
318 W(RGF_USER_MAC_CPU_0, BIT(1)); /* mac_cpu_man_rst */
319 W(RGF_USER_USER_CPU_0, BIT(1)); /* user_cpu_man_rst */
320 wmb(); /* order is important here */
322 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
323 W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
324 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000170);
325 W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FC00);
326 wmb(); /* order is important here */
328 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
329 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
330 W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
331 W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
332 wmb(); /* order is important here */
334 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000001);
336 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00000080);
338 W(RGF_PCIE_LOS_COUNTER_CTL, BIT(6) | BIT(8));
339 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000);
341 W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
342 wmb(); /* order is important here */
344 /* wait until device ready */
347 hw_state = R(RGF_USER_HW_MACHINE_STATE);
349 wil_err(wil, "Reset not completed, hw_state 0x%08x\n",
353 } while (hw_state != HW_MACHINE_BOOT_DONE);
356 W(RGF_PCIE_LOS_COUNTER_CTL, BIT(8));
358 C(RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
359 wmb(); /* order is important here */
361 wil_dbg_misc(wil, "Reset completed in %d ms\n", delay);
369 void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
371 le32_to_cpus(&r->base);
372 le16_to_cpus(&r->entry_size);
373 le16_to_cpus(&r->size);
374 le32_to_cpus(&r->tail);
375 le32_to_cpus(&r->head);
378 static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
380 ulong to = msecs_to_jiffies(1000);
381 ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
383 wil_err(wil, "Firmware not ready\n");
386 wil_info(wil, "FW ready after %d ms. HW version 0x%08x\n",
387 jiffies_to_msecs(to-left), wil->hw_version);
393 * We reset all the structures, and we reset the UMAC.
394 * After calling this routine, you're expected to reload
397 int wil_reset(struct wil6210_priv *wil)
401 WARN_ON(!mutex_is_locked(&wil->mutex));
403 cancel_work_sync(&wil->disconnect_worker);
404 wil6210_disconnect(wil, NULL);
406 wil->status = 0; /* prevent NAPI from being scheduled */
407 if (test_bit(wil_status_napi_en, &wil->status)) {
408 napi_synchronize(&wil->napi_rx);
411 if (wil->scan_request) {
412 wil_dbg_misc(wil, "Abort scan_request 0x%p\n",
414 cfg80211_scan_done(wil->scan_request, true);
415 wil->scan_request = NULL;
418 wil6210_disable_irq(wil);
420 wmi_event_flush(wil);
422 flush_workqueue(wil->wmi_wq_conn);
423 flush_workqueue(wil->wmi_wq);
425 /* TODO: put MAC in reset */
426 wil_target_reset(wil);
430 /* init after reset */
431 wil->pending_connect_cid = -1;
432 reinit_completion(&wil->wmi_ready);
434 /* TODO: release MAC reset */
435 wil6210_enable_irq(wil);
437 /* we just started MAC, wait for FW ready */
438 rc = wil_wait_for_fw_ready(wil);
443 void wil_fw_error_recovery(struct wil6210_priv *wil)
445 wil_dbg_misc(wil, "starting fw error recovery\n");
446 schedule_work(&wil->fw_error_worker);
449 void wil_link_on(struct wil6210_priv *wil)
451 struct net_device *ndev = wil_to_ndev(wil);
453 wil_dbg_misc(wil, "%s()\n", __func__);
455 netif_carrier_on(ndev);
456 netif_tx_wake_all_queues(ndev);
459 void wil_link_off(struct wil6210_priv *wil)
461 struct net_device *ndev = wil_to_ndev(wil);
463 wil_dbg_misc(wil, "%s()\n", __func__);
465 netif_tx_stop_all_queues(ndev);
466 netif_carrier_off(ndev);
469 static int __wil_up(struct wil6210_priv *wil)
471 struct net_device *ndev = wil_to_ndev(wil);
472 struct wireless_dev *wdev = wil->wdev;
475 WARN_ON(!mutex_is_locked(&wil->mutex));
481 /* Rx VRING. After MAC and beacon */
482 rc = wil_rx_init(wil);
486 switch (wdev->iftype) {
487 case NL80211_IFTYPE_STATION:
488 wil_dbg_misc(wil, "type: STATION\n");
489 ndev->type = ARPHRD_ETHER;
491 case NL80211_IFTYPE_AP:
492 wil_dbg_misc(wil, "type: AP\n");
493 ndev->type = ARPHRD_ETHER;
495 case NL80211_IFTYPE_P2P_CLIENT:
496 wil_dbg_misc(wil, "type: P2P_CLIENT\n");
497 ndev->type = ARPHRD_ETHER;
499 case NL80211_IFTYPE_P2P_GO:
500 wil_dbg_misc(wil, "type: P2P_GO\n");
501 ndev->type = ARPHRD_ETHER;
503 case NL80211_IFTYPE_MONITOR:
504 wil_dbg_misc(wil, "type: Monitor\n");
505 ndev->type = ARPHRD_IEEE80211_RADIOTAP;
506 /* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
512 /* MAC address - pre-requisite for other commands */
513 wmi_set_mac_address(wil, ndev->dev_addr);
516 napi_enable(&wil->napi_rx);
517 napi_enable(&wil->napi_tx);
518 set_bit(wil_status_napi_en, &wil->status);
523 int wil_up(struct wil6210_priv *wil)
527 mutex_lock(&wil->mutex);
529 mutex_unlock(&wil->mutex);
534 static int __wil_down(struct wil6210_priv *wil)
536 WARN_ON(!mutex_is_locked(&wil->mutex));
538 clear_bit(wil_status_napi_en, &wil->status);
539 napi_disable(&wil->napi_rx);
540 napi_disable(&wil->napi_tx);
542 if (wil->scan_request) {
543 cfg80211_scan_done(wil->scan_request, true);
544 wil->scan_request = NULL;
547 wil6210_disconnect(wil, NULL);
553 int wil_down(struct wil6210_priv *wil)
557 mutex_lock(&wil->mutex);
558 rc = __wil_down(wil);
559 mutex_unlock(&wil->mutex);
564 int wil_find_cid(struct wil6210_priv *wil, const u8 *mac)
569 for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
570 if ((wil->sta[i].status != wil_sta_unused) &&
571 ether_addr_equal(wil->sta[i].addr, mac)) {