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(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);
90 static void wil_disconnect_worker(struct work_struct *work)
92 struct wil6210_priv *wil = container_of(work,
93 struct wil6210_priv, disconnect_worker);
95 _wil6210_disconnect(wil, NULL);
98 static void wil_connect_timer_fn(ulong x)
100 struct wil6210_priv *wil = (void *)x;
102 wil_dbg(wil, "Connect timeout\n");
104 /* reschedule to thread context - disconnect won't
105 * run from atomic context
107 schedule_work(&wil->disconnect_worker);
110 int wil_priv_init(struct wil6210_priv *wil)
112 wil_dbg(wil, "%s()\n", __func__);
114 mutex_init(&wil->mutex);
115 mutex_init(&wil->wmi_mutex);
117 init_completion(&wil->wmi_ready);
119 wil->pending_connect_cid = -1;
120 setup_timer(&wil->connect_timer, wil_connect_timer_fn, (ulong)wil);
122 INIT_WORK(&wil->wmi_connect_worker, wmi_connect_worker);
123 INIT_WORK(&wil->disconnect_worker, wil_disconnect_worker);
124 INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
126 INIT_LIST_HEAD(&wil->pending_wmi_ev);
127 spin_lock_init(&wil->wmi_ev_lock);
129 wil->wmi_wq = create_singlethread_workqueue(WIL_NAME"_wmi");
133 wil->wmi_wq_conn = create_singlethread_workqueue(WIL_NAME"_connect");
134 if (!wil->wmi_wq_conn) {
135 destroy_workqueue(wil->wmi_wq);
139 /* make shadow copy of registers that should not change on run time */
140 wil_memcpy_fromio_32(&wil->mbox_ctl, wil->csr + HOST_MBOX,
141 sizeof(struct wil6210_mbox_ctl));
142 wil_mbox_ring_le2cpus(&wil->mbox_ctl.rx);
143 wil_mbox_ring_le2cpus(&wil->mbox_ctl.tx);
148 void wil6210_disconnect(struct wil6210_priv *wil, void *bssid)
150 del_timer_sync(&wil->connect_timer);
151 _wil6210_disconnect(wil, bssid);
154 void wil_priv_deinit(struct wil6210_priv *wil)
156 cancel_work_sync(&wil->disconnect_worker);
157 wil6210_disconnect(wil, NULL);
158 wmi_event_flush(wil);
159 destroy_workqueue(wil->wmi_wq_conn);
160 destroy_workqueue(wil->wmi_wq);
163 static void wil_target_reset(struct wil6210_priv *wil)
165 wil_dbg(wil, "Resetting...\n");
168 #define W(a, v) iowrite32(v, wil->csr + HOSTADDR(a))
169 /* register set = read, OR, write */
170 #define S(a, v) iowrite32(ioread32(wil->csr + HOSTADDR(a)) | v, \
171 wil->csr + HOSTADDR(a))
173 /* hpal_perst_from_pad_src_n_mask */
174 S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(6));
175 /* car_perst_rst_src_n_mask */
176 S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(7));
178 W(RGF_USER_MAC_CPU_0, BIT(1)); /* mac_cpu_man_rst */
179 W(RGF_USER_USER_CPU_0, BIT(1)); /* user_cpu_man_rst */
183 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
184 W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
185 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000170);
186 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);
201 W(RGF_USER_USER_CPU_0, BIT(0)); /* user_cpu_man_de_rst */
205 wil_dbg(wil, "Reset completed\n");
211 void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
213 le32_to_cpus(&r->base);
214 le16_to_cpus(&r->entry_size);
215 le16_to_cpus(&r->size);
216 le32_to_cpus(&r->tail);
217 le32_to_cpus(&r->head);
220 static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
222 ulong to = msecs_to_jiffies(1000);
223 ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
225 wil_err(wil, "Firmware not ready\n");
228 wil_dbg(wil, "FW ready after %d ms\n",
229 jiffies_to_msecs(to-left));
235 * We reset all the structures, and we reset the UMAC.
236 * After calling this routine, you're expected to reload
239 int wil_reset(struct wil6210_priv *wil)
243 cancel_work_sync(&wil->disconnect_worker);
244 wil6210_disconnect(wil, NULL);
246 wmi_event_flush(wil);
248 flush_workqueue(wil->wmi_wq);
249 flush_workqueue(wil->wmi_wq_conn);
251 wil6210_disable_irq(wil);
254 /* TODO: put MAC in reset */
255 wil_target_reset(wil);
257 /* init after reset */
258 wil->pending_connect_cid = -1;
259 INIT_COMPLETION(wil->wmi_ready);
261 /* make shadow copy of registers that should not change on run time */
262 wil_memcpy_fromio_32(&wil->mbox_ctl, wil->csr + HOST_MBOX,
263 sizeof(struct wil6210_mbox_ctl));
264 wil_mbox_ring_le2cpus(&wil->mbox_ctl.rx);
265 wil_mbox_ring_le2cpus(&wil->mbox_ctl.tx);
267 /* TODO: release MAC reset */
268 wil6210_enable_irq(wil);
270 /* we just started MAC, wait for FW ready */
271 rc = wil_wait_for_fw_ready(wil);
277 void wil_link_on(struct wil6210_priv *wil)
279 struct net_device *ndev = wil_to_ndev(wil);
281 wil_dbg(wil, "%s()\n", __func__);
283 netif_carrier_on(ndev);
284 netif_tx_wake_all_queues(ndev);
287 void wil_link_off(struct wil6210_priv *wil)
289 struct net_device *ndev = wil_to_ndev(wil);
291 wil_dbg(wil, "%s()\n", __func__);
293 netif_tx_stop_all_queues(ndev);
294 netif_carrier_off(ndev);
297 static int __wil_up(struct wil6210_priv *wil)
299 struct net_device *ndev = wil_to_ndev(wil);
300 struct wireless_dev *wdev = wil->wdev;
301 struct ieee80211_channel *channel = wdev->preset_chandef.chan;
304 u16 wmi_nettype = wil_iftype_nl2wmi(wdev->iftype);
310 /* FIXME Firmware works now in PBSS mode(ToDS=0, FromDS=0) */
311 wmi_nettype = wil_iftype_nl2wmi(NL80211_IFTYPE_ADHOC);
312 switch (wdev->iftype) {
313 case NL80211_IFTYPE_STATION:
314 wil_dbg(wil, "type: STATION\n");
316 ndev->type = ARPHRD_ETHER;
318 case NL80211_IFTYPE_AP:
319 wil_dbg(wil, "type: AP\n");
321 ndev->type = ARPHRD_ETHER;
323 case NL80211_IFTYPE_P2P_CLIENT:
324 wil_dbg(wil, "type: P2P_CLIENT\n");
326 ndev->type = ARPHRD_ETHER;
328 case NL80211_IFTYPE_P2P_GO:
329 wil_dbg(wil, "type: P2P_GO\n");
331 ndev->type = ARPHRD_ETHER;
333 case NL80211_IFTYPE_MONITOR:
334 wil_dbg(wil, "type: Monitor\n");
336 ndev->type = ARPHRD_IEEE80211_RADIOTAP;
337 /* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
343 /* Apply profile in the following order: */
344 /* SSID and channel for the AP */
345 switch (wdev->iftype) {
346 case NL80211_IFTYPE_AP:
347 case NL80211_IFTYPE_P2P_GO:
348 if (wdev->ssid_len == 0) {
349 wil_err(wil, "SSID not set\n");
352 wmi_set_ssid(wil, wdev->ssid_len, wdev->ssid);
354 wmi_set_channel(wil, channel->hw_value);
360 /* MAC address - pre-requisite for other commands */
361 wmi_set_mac_address(wil, ndev->dev_addr);
363 /* Set up beaconing if required. */
364 rc = wmi_set_bcon(wil, bi, wmi_nettype);
368 /* Rx VRING. After MAC and beacon */
374 int wil_up(struct wil6210_priv *wil)
378 mutex_lock(&wil->mutex);
380 mutex_unlock(&wil->mutex);
385 static int __wil_down(struct wil6210_priv *wil)
387 if (wil->scan_request) {
388 cfg80211_scan_done(wil->scan_request, true);
389 wil->scan_request = NULL;
392 wil6210_disconnect(wil, NULL);
398 int wil_down(struct wil6210_priv *wil)
402 mutex_lock(&wil->mutex);
403 rc = __wil_down(wil);
404 mutex_unlock(&wil->mutex);