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>
23 * Due to a hardware issue,
24 * one has to read/write to/from NIC in 32-bit chunks;
25 * regular memcpy_fromio and siblings will
26 * not work on 64-bit platform - it uses 64-bit transactions
28 * Force 32-bit transactions to enable NIC on 64-bit platforms
30 * To avoid byte swap on big endian host, __raw_{read|write}l
31 * should be used - {read|write}l would swap bytes to provide
32 * little endian on PCI value in host endianness.
34 void wil_memcpy_fromio_32(void *dst, const volatile void __iomem *src,
38 const volatile u32 __iomem *s = src;
40 /* size_t is unsigned, if (count%4 != 0) it will wrap */
41 for (count += 4; count > 4; count -= 4)
42 *d++ = __raw_readl(s++);
45 void wil_memcpy_toio_32(volatile void __iomem *dst, const void *src,
48 volatile u32 __iomem *d = dst;
51 for (count += 4; count > 4; count -= 4)
52 __raw_writel(*s++, d++);
55 static void _wil6210_disconnect(struct wil6210_priv *wil, void *bssid)
58 struct net_device *ndev = wil_to_ndev(wil);
60 wil_dbg_misc(wil, "%s()\n", __func__);
63 if (test_bit(wil_status_fwconnected, &wil->status)) {
64 clear_bit(wil_status_fwconnected, &wil->status);
65 cfg80211_disconnected(ndev,
66 WLAN_STATUS_UNSPECIFIED_FAILURE,
68 } else if (test_bit(wil_status_fwconnecting, &wil->status)) {
69 cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
70 WLAN_STATUS_UNSPECIFIED_FAILURE,
73 clear_bit(wil_status_fwconnecting, &wil->status);
74 for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++)
75 wil_vring_fini_tx(wil, i);
77 clear_bit(wil_status_dontscan, &wil->status);
80 static void wil_disconnect_worker(struct work_struct *work)
82 struct wil6210_priv *wil = container_of(work,
83 struct wil6210_priv, disconnect_worker);
85 _wil6210_disconnect(wil, NULL);
88 static void wil_connect_timer_fn(ulong x)
90 struct wil6210_priv *wil = (void *)x;
92 wil_dbg_misc(wil, "Connect timeout\n");
94 /* reschedule to thread context - disconnect won't
95 * run from atomic context
97 schedule_work(&wil->disconnect_worker);
100 static void wil_connect_worker(struct work_struct *work)
103 struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
105 int cid = wil->pending_connect_cid;
108 wil_err(wil, "No connection pending\n");
112 wil_dbg_wmi(wil, "Configure for connection CID %d\n", cid);
114 rc = wil_vring_init_tx(wil, 0, WIL6210_TX_RING_SIZE, cid, 0);
115 wil->pending_connect_cid = -1;
120 int wil_priv_init(struct wil6210_priv *wil)
122 wil_dbg_misc(wil, "%s()\n", __func__);
124 mutex_init(&wil->mutex);
125 mutex_init(&wil->wmi_mutex);
127 init_completion(&wil->wmi_ready);
129 wil->pending_connect_cid = -1;
130 setup_timer(&wil->connect_timer, wil_connect_timer_fn, (ulong)wil);
132 INIT_WORK(&wil->connect_worker, wil_connect_worker);
133 INIT_WORK(&wil->disconnect_worker, wil_disconnect_worker);
134 INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
136 INIT_LIST_HEAD(&wil->pending_wmi_ev);
137 spin_lock_init(&wil->wmi_ev_lock);
139 wil->wmi_wq = create_singlethread_workqueue(WIL_NAME"_wmi");
143 wil->wmi_wq_conn = create_singlethread_workqueue(WIL_NAME"_connect");
144 if (!wil->wmi_wq_conn) {
145 destroy_workqueue(wil->wmi_wq);
152 void wil6210_disconnect(struct wil6210_priv *wil, void *bssid)
154 del_timer_sync(&wil->connect_timer);
155 _wil6210_disconnect(wil, bssid);
158 void wil_priv_deinit(struct wil6210_priv *wil)
160 cancel_work_sync(&wil->disconnect_worker);
161 wil6210_disconnect(wil, NULL);
162 wmi_event_flush(wil);
163 destroy_workqueue(wil->wmi_wq_conn);
164 destroy_workqueue(wil->wmi_wq);
167 static void wil_target_reset(struct wil6210_priv *wil)
169 wil_dbg_misc(wil, "Resetting...\n");
172 #define W(a, v) iowrite32(v, wil->csr + HOSTADDR(a))
173 /* register set = read, OR, write */
174 #define S(a, v) iowrite32(ioread32(wil->csr + HOSTADDR(a)) | v, \
175 wil->csr + HOSTADDR(a))
177 /* hpal_perst_from_pad_src_n_mask */
178 S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(6));
179 /* car_perst_rst_src_n_mask */
180 S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(7));
182 W(RGF_USER_MAC_CPU_0, BIT(1)); /* mac_cpu_man_rst */
183 W(RGF_USER_USER_CPU_0, BIT(1)); /* user_cpu_man_rst */
185 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
186 W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
187 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000170);
188 W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FC00);
190 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
191 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
192 W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
193 W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
195 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000001);
196 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00000080);
197 W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
199 wil_dbg_misc(wil, "Reset completed\n");
205 void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
207 le32_to_cpus(&r->base);
208 le16_to_cpus(&r->entry_size);
209 le16_to_cpus(&r->size);
210 le32_to_cpus(&r->tail);
211 le32_to_cpus(&r->head);
214 static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
216 ulong to = msecs_to_jiffies(1000);
217 ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
219 wil_err(wil, "Firmware not ready\n");
222 wil_dbg_misc(wil, "FW ready after %d ms\n",
223 jiffies_to_msecs(to-left));
229 * We reset all the structures, and we reset the UMAC.
230 * After calling this routine, you're expected to reload
233 int wil_reset(struct wil6210_priv *wil)
237 cancel_work_sync(&wil->disconnect_worker);
238 wil6210_disconnect(wil, NULL);
240 wil6210_disable_irq(wil);
243 wmi_event_flush(wil);
245 flush_workqueue(wil->wmi_wq_conn);
246 flush_workqueue(wil->wmi_wq);
248 /* TODO: put MAC in reset */
249 wil_target_reset(wil);
251 /* init after reset */
252 wil->pending_connect_cid = -1;
253 INIT_COMPLETION(wil->wmi_ready);
255 /* TODO: release MAC reset */
256 wil6210_enable_irq(wil);
258 /* we just started MAC, wait for FW ready */
259 rc = wil_wait_for_fw_ready(wil);
265 void wil_link_on(struct wil6210_priv *wil)
267 struct net_device *ndev = wil_to_ndev(wil);
269 wil_dbg_misc(wil, "%s()\n", __func__);
271 netif_carrier_on(ndev);
272 netif_tx_wake_all_queues(ndev);
275 void wil_link_off(struct wil6210_priv *wil)
277 struct net_device *ndev = wil_to_ndev(wil);
279 wil_dbg_misc(wil, "%s()\n", __func__);
281 netif_tx_stop_all_queues(ndev);
282 netif_carrier_off(ndev);
285 static int __wil_up(struct wil6210_priv *wil)
287 struct net_device *ndev = wil_to_ndev(wil);
288 struct wireless_dev *wdev = wil->wdev;
295 /* Rx VRING. After MAC and beacon */
296 rc = wil_rx_init(wil);
300 switch (wdev->iftype) {
301 case NL80211_IFTYPE_STATION:
302 wil_dbg_misc(wil, "type: STATION\n");
303 ndev->type = ARPHRD_ETHER;
305 case NL80211_IFTYPE_AP:
306 wil_dbg_misc(wil, "type: AP\n");
307 ndev->type = ARPHRD_ETHER;
309 case NL80211_IFTYPE_P2P_CLIENT:
310 wil_dbg_misc(wil, "type: P2P_CLIENT\n");
311 ndev->type = ARPHRD_ETHER;
313 case NL80211_IFTYPE_P2P_GO:
314 wil_dbg_misc(wil, "type: P2P_GO\n");
315 ndev->type = ARPHRD_ETHER;
317 case NL80211_IFTYPE_MONITOR:
318 wil_dbg_misc(wil, "type: Monitor\n");
319 ndev->type = ARPHRD_IEEE80211_RADIOTAP;
320 /* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
326 /* MAC address - pre-requisite for other commands */
327 wmi_set_mac_address(wil, ndev->dev_addr);
330 napi_enable(&wil->napi_rx);
331 napi_enable(&wil->napi_tx);
336 int wil_up(struct wil6210_priv *wil)
340 mutex_lock(&wil->mutex);
342 mutex_unlock(&wil->mutex);
347 static int __wil_down(struct wil6210_priv *wil)
349 napi_disable(&wil->napi_rx);
350 napi_disable(&wil->napi_tx);
352 if (wil->scan_request) {
353 cfg80211_scan_done(wil->scan_request, true);
354 wil->scan_request = NULL;
357 wil6210_disconnect(wil, NULL);
363 int wil_down(struct wil6210_priv *wil)
367 mutex_lock(&wil->mutex);
368 rc = __wil_down(wil);
369 mutex_unlock(&wil->mutex);