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/kernel.h>
18 #include <linux/netdevice.h>
19 #include <linux/sched.h>
20 #include <linux/ieee80211.h>
21 #include <linux/wireless.h>
22 #include <linux/slab.h>
23 #include <linux/moduleparam.h>
24 #include <linux/if_arp.h>
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 _wil6210_disconnect(struct wil6210_priv *wil, void *bssid)
64 struct net_device *ndev = wil_to_ndev(wil);
65 struct wireless_dev *wdev = wil->wdev;
67 wil_dbg_misc(wil, "%s()\n", __func__);
70 clear_bit(wil_status_fwconnected, &wil->status);
72 switch (wdev->sme_state) {
73 case CFG80211_SME_CONNECTED:
74 cfg80211_disconnected(ndev, WLAN_STATUS_UNSPECIFIED_FAILURE,
77 case CFG80211_SME_CONNECTING:
78 cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
79 WLAN_STATUS_UNSPECIFIED_FAILURE,
86 for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++)
87 wil_vring_fini_tx(wil, i);
89 clear_bit(wil_status_dontscan, &wil->status);
92 static void wil_disconnect_worker(struct work_struct *work)
94 struct wil6210_priv *wil = container_of(work,
95 struct wil6210_priv, disconnect_worker);
97 _wil6210_disconnect(wil, NULL);
100 static void wil_connect_timer_fn(ulong x)
102 struct wil6210_priv *wil = (void *)x;
104 wil_dbg_misc(wil, "Connect timeout\n");
106 /* reschedule to thread context - disconnect won't
107 * run from atomic context
109 schedule_work(&wil->disconnect_worker);
112 static void wil_cache_mbox_regs(struct wil6210_priv *wil)
114 /* make shadow copy of registers that should not change on run time */
115 wil_memcpy_fromio_32(&wil->mbox_ctl, wil->csr + HOST_MBOX,
116 sizeof(struct wil6210_mbox_ctl));
117 wil_mbox_ring_le2cpus(&wil->mbox_ctl.rx);
118 wil_mbox_ring_le2cpus(&wil->mbox_ctl.tx);
121 static void wil_connect_worker(struct work_struct *work)
124 struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
126 int cid = wil->pending_connect_cid;
129 wil_err(wil, "No connection pending\n");
133 wil_dbg_wmi(wil, "Configure for connection CID %d\n", cid);
135 rc = wil_vring_init_tx(wil, 0, WIL6210_TX_RING_SIZE, cid, 0);
136 wil->pending_connect_cid = -1;
141 int wil_priv_init(struct wil6210_priv *wil)
143 wil_dbg_misc(wil, "%s()\n", __func__);
145 mutex_init(&wil->mutex);
146 mutex_init(&wil->wmi_mutex);
148 init_completion(&wil->wmi_ready);
150 wil->pending_connect_cid = -1;
151 setup_timer(&wil->connect_timer, wil_connect_timer_fn, (ulong)wil);
153 INIT_WORK(&wil->connect_worker, wil_connect_worker);
154 INIT_WORK(&wil->disconnect_worker, wil_disconnect_worker);
155 INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
157 INIT_LIST_HEAD(&wil->pending_wmi_ev);
158 spin_lock_init(&wil->wmi_ev_lock);
160 wil->wmi_wq = create_singlethread_workqueue(WIL_NAME"_wmi");
164 wil->wmi_wq_conn = create_singlethread_workqueue(WIL_NAME"_connect");
165 if (!wil->wmi_wq_conn) {
166 destroy_workqueue(wil->wmi_wq);
170 wil_cache_mbox_regs(wil);
175 void wil6210_disconnect(struct wil6210_priv *wil, void *bssid)
177 del_timer_sync(&wil->connect_timer);
178 _wil6210_disconnect(wil, bssid);
181 void wil_priv_deinit(struct wil6210_priv *wil)
183 cancel_work_sync(&wil->disconnect_worker);
184 wil6210_disconnect(wil, NULL);
185 wmi_event_flush(wil);
186 destroy_workqueue(wil->wmi_wq_conn);
187 destroy_workqueue(wil->wmi_wq);
190 static void wil_target_reset(struct wil6210_priv *wil)
192 wil_dbg_misc(wil, "Resetting...\n");
195 #define W(a, v) iowrite32(v, wil->csr + HOSTADDR(a))
196 /* register set = read, OR, write */
197 #define S(a, v) iowrite32(ioread32(wil->csr + HOSTADDR(a)) | v, \
198 wil->csr + HOSTADDR(a))
200 /* hpal_perst_from_pad_src_n_mask */
201 S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(6));
202 /* car_perst_rst_src_n_mask */
203 S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(7));
205 W(RGF_USER_MAC_CPU_0, BIT(1)); /* mac_cpu_man_rst */
206 W(RGF_USER_USER_CPU_0, BIT(1)); /* user_cpu_man_rst */
210 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
211 W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
212 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000170);
213 W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FC00);
217 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
218 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
219 W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
220 W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
222 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000001);
223 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00000080);
224 W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
228 W(RGF_USER_USER_CPU_0, BIT(0)); /* user_cpu_man_de_rst */
232 wil_dbg_misc(wil, "Reset completed\n");
238 void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
240 le32_to_cpus(&r->base);
241 le16_to_cpus(&r->entry_size);
242 le16_to_cpus(&r->size);
243 le32_to_cpus(&r->tail);
244 le32_to_cpus(&r->head);
247 static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
249 ulong to = msecs_to_jiffies(1000);
250 ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
252 wil_err(wil, "Firmware not ready\n");
255 wil_dbg_misc(wil, "FW ready after %d ms\n",
256 jiffies_to_msecs(to-left));
262 * We reset all the structures, and we reset the UMAC.
263 * After calling this routine, you're expected to reload
266 int wil_reset(struct wil6210_priv *wil)
270 cancel_work_sync(&wil->disconnect_worker);
271 wil6210_disconnect(wil, NULL);
273 wil6210_disable_irq(wil);
276 wmi_event_flush(wil);
278 flush_workqueue(wil->wmi_wq_conn);
279 flush_workqueue(wil->wmi_wq);
281 /* TODO: put MAC in reset */
282 wil_target_reset(wil);
284 /* init after reset */
285 wil->pending_connect_cid = -1;
286 INIT_COMPLETION(wil->wmi_ready);
288 wil_cache_mbox_regs(wil);
290 /* TODO: release MAC reset */
291 wil6210_enable_irq(wil);
293 /* we just started MAC, wait for FW ready */
294 rc = wil_wait_for_fw_ready(wil);
300 void wil_link_on(struct wil6210_priv *wil)
302 struct net_device *ndev = wil_to_ndev(wil);
304 wil_dbg_misc(wil, "%s()\n", __func__);
306 netif_carrier_on(ndev);
307 netif_tx_wake_all_queues(ndev);
310 void wil_link_off(struct wil6210_priv *wil)
312 struct net_device *ndev = wil_to_ndev(wil);
314 wil_dbg_misc(wil, "%s()\n", __func__);
316 netif_tx_stop_all_queues(ndev);
317 netif_carrier_off(ndev);
320 static int __wil_up(struct wil6210_priv *wil)
322 struct net_device *ndev = wil_to_ndev(wil);
323 struct wireless_dev *wdev = wil->wdev;
324 struct ieee80211_channel *channel = wdev->preset_chandef.chan;
327 u16 wmi_nettype = wil_iftype_nl2wmi(wdev->iftype);
333 /* FIXME Firmware works now in PBSS mode(ToDS=0, FromDS=0) */
334 wmi_nettype = wil_iftype_nl2wmi(NL80211_IFTYPE_ADHOC);
335 switch (wdev->iftype) {
336 case NL80211_IFTYPE_STATION:
337 wil_dbg_misc(wil, "type: STATION\n");
339 ndev->type = ARPHRD_ETHER;
341 case NL80211_IFTYPE_AP:
342 wil_dbg_misc(wil, "type: AP\n");
344 ndev->type = ARPHRD_ETHER;
346 case NL80211_IFTYPE_P2P_CLIENT:
347 wil_dbg_misc(wil, "type: P2P_CLIENT\n");
349 ndev->type = ARPHRD_ETHER;
351 case NL80211_IFTYPE_P2P_GO:
352 wil_dbg_misc(wil, "type: P2P_GO\n");
354 ndev->type = ARPHRD_ETHER;
356 case NL80211_IFTYPE_MONITOR:
357 wil_dbg_misc(wil, "type: Monitor\n");
359 ndev->type = ARPHRD_IEEE80211_RADIOTAP;
360 /* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
366 /* Apply profile in the following order: */
367 /* SSID and channel for the AP */
368 switch (wdev->iftype) {
369 case NL80211_IFTYPE_AP:
370 case NL80211_IFTYPE_P2P_GO:
371 if (wdev->ssid_len == 0) {
372 wil_err(wil, "SSID not set\n");
375 wmi_set_ssid(wil, wdev->ssid_len, wdev->ssid);
377 wmi_set_channel(wil, channel->hw_value);
383 /* MAC address - pre-requisite for other commands */
384 wmi_set_mac_address(wil, ndev->dev_addr);
386 /* Set up beaconing if required. */
387 rc = wmi_set_bcon(wil, bi, wmi_nettype);
391 /* Rx VRING. After MAC and beacon */
397 int wil_up(struct wil6210_priv *wil)
401 mutex_lock(&wil->mutex);
403 mutex_unlock(&wil->mutex);
408 static int __wil_down(struct wil6210_priv *wil)
410 if (wil->scan_request) {
411 cfg80211_scan_done(wil->scan_request, true);
412 wil->scan_request = NULL;
415 wil6210_disconnect(wil, NULL);
421 int wil_down(struct wil6210_priv *wil)
425 mutex_lock(&wil->mutex);
426 rc = __wil_down(wil);
427 mutex_unlock(&wil->mutex);