xref: /openbmc/linux/drivers/net/wireless/ath/wil6210/main.c (revision 840ef8b7cc584a23c4f9d05352f4dbaf8e56e5ab)
1 /*
2  * Copyright (c) 2012 Qualcomm Atheros, Inc.
3  *
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.
7  *
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.
15  */
16 
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>
25 
26 #include "wil6210.h"
27 
28 /*
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
33  *
34  * Force 32-bit transactions to enable NIC on 64-bit platforms
35  *
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.
39  */
40 void wil_memcpy_fromio_32(void *dst, const volatile void __iomem *src,
41 			  size_t count)
42 {
43 	u32 *d = dst;
44 	const volatile u32 __iomem *s = src;
45 
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++);
49 }
50 
51 void wil_memcpy_toio_32(volatile void __iomem *dst, const void *src,
52 			size_t count)
53 {
54 	volatile u32 __iomem *d = dst;
55 	const u32 *s = src;
56 
57 	for (count += 4; count > 4; count -= 4)
58 		__raw_writel(*s++, d++);
59 }
60 
61 static void _wil6210_disconnect(struct wil6210_priv *wil, void *bssid)
62 {
63 	uint i;
64 	struct net_device *ndev = wil_to_ndev(wil);
65 	struct wireless_dev *wdev = wil->wdev;
66 
67 	wil_dbg_misc(wil, "%s()\n", __func__);
68 
69 	wil_link_off(wil);
70 	clear_bit(wil_status_fwconnected, &wil->status);
71 
72 	switch (wdev->sme_state) {
73 	case CFG80211_SME_CONNECTED:
74 		cfg80211_disconnected(ndev, WLAN_STATUS_UNSPECIFIED_FAILURE,
75 				      NULL, 0, GFP_KERNEL);
76 		break;
77 	case CFG80211_SME_CONNECTING:
78 		cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
79 					WLAN_STATUS_UNSPECIFIED_FAILURE,
80 					GFP_KERNEL);
81 		break;
82 	default:
83 		break;
84 	}
85 
86 	for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++)
87 		wil_vring_fini_tx(wil, i);
88 
89 	clear_bit(wil_status_dontscan, &wil->status);
90 }
91 
92 static void wil_disconnect_worker(struct work_struct *work)
93 {
94 	struct wil6210_priv *wil = container_of(work,
95 			struct wil6210_priv, disconnect_worker);
96 
97 	_wil6210_disconnect(wil, NULL);
98 }
99 
100 static void wil_connect_timer_fn(ulong x)
101 {
102 	struct wil6210_priv *wil = (void *)x;
103 
104 	wil_dbg_misc(wil, "Connect timeout\n");
105 
106 	/* reschedule to thread context - disconnect won't
107 	 * run from atomic context
108 	 */
109 	schedule_work(&wil->disconnect_worker);
110 }
111 
112 static void wil_cache_mbox_regs(struct wil6210_priv *wil)
113 {
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);
119 }
120 
121 int wil_priv_init(struct wil6210_priv *wil)
122 {
123 	wil_dbg_misc(wil, "%s()\n", __func__);
124 
125 	mutex_init(&wil->mutex);
126 	mutex_init(&wil->wmi_mutex);
127 
128 	init_completion(&wil->wmi_ready);
129 
130 	wil->pending_connect_cid = -1;
131 	setup_timer(&wil->connect_timer, wil_connect_timer_fn, (ulong)wil);
132 
133 	INIT_WORK(&wil->wmi_connect_worker, wmi_connect_worker);
134 	INIT_WORK(&wil->disconnect_worker, wil_disconnect_worker);
135 	INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
136 
137 	INIT_LIST_HEAD(&wil->pending_wmi_ev);
138 	spin_lock_init(&wil->wmi_ev_lock);
139 
140 	wil->wmi_wq = create_singlethread_workqueue(WIL_NAME"_wmi");
141 	if (!wil->wmi_wq)
142 		return -EAGAIN;
143 
144 	wil->wmi_wq_conn = create_singlethread_workqueue(WIL_NAME"_connect");
145 	if (!wil->wmi_wq_conn) {
146 		destroy_workqueue(wil->wmi_wq);
147 		return -EAGAIN;
148 	}
149 
150 	wil_cache_mbox_regs(wil);
151 
152 	return 0;
153 }
154 
155 void wil6210_disconnect(struct wil6210_priv *wil, void *bssid)
156 {
157 	del_timer_sync(&wil->connect_timer);
158 	_wil6210_disconnect(wil, bssid);
159 }
160 
161 void wil_priv_deinit(struct wil6210_priv *wil)
162 {
163 	cancel_work_sync(&wil->disconnect_worker);
164 	wil6210_disconnect(wil, NULL);
165 	wmi_event_flush(wil);
166 	destroy_workqueue(wil->wmi_wq_conn);
167 	destroy_workqueue(wil->wmi_wq);
168 }
169 
170 static void wil_target_reset(struct wil6210_priv *wil)
171 {
172 	wil_dbg_misc(wil, "Resetting...\n");
173 
174 	/* register write */
175 #define W(a, v) iowrite32(v, wil->csr + HOSTADDR(a))
176 	/* register set = read, OR, write */
177 #define S(a, v) iowrite32(ioread32(wil->csr + HOSTADDR(a)) | v, \
178 		wil->csr + HOSTADDR(a))
179 
180 	/* hpal_perst_from_pad_src_n_mask */
181 	S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(6));
182 	/* car_perst_rst_src_n_mask */
183 	S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(7));
184 
185 	W(RGF_USER_MAC_CPU_0,  BIT(1)); /* mac_cpu_man_rst */
186 	W(RGF_USER_USER_CPU_0, BIT(1)); /* user_cpu_man_rst */
187 
188 	msleep(100);
189 
190 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
191 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
192 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000170);
193 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FC00);
194 
195 	msleep(100);
196 
197 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
198 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
199 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
200 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
201 
202 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000001);
203 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00000080);
204 	W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
205 
206 	msleep(2000);
207 
208 	W(RGF_USER_USER_CPU_0, BIT(0)); /* user_cpu_man_de_rst */
209 
210 	msleep(2000);
211 
212 	wil_dbg_misc(wil, "Reset completed\n");
213 
214 #undef W
215 #undef S
216 }
217 
218 void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
219 {
220 	le32_to_cpus(&r->base);
221 	le16_to_cpus(&r->entry_size);
222 	le16_to_cpus(&r->size);
223 	le32_to_cpus(&r->tail);
224 	le32_to_cpus(&r->head);
225 }
226 
227 static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
228 {
229 	ulong to = msecs_to_jiffies(1000);
230 	ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
231 	if (0 == left) {
232 		wil_err(wil, "Firmware not ready\n");
233 		return -ETIME;
234 	} else {
235 		wil_dbg_misc(wil, "FW ready after %d ms\n",
236 			     jiffies_to_msecs(to-left));
237 	}
238 	return 0;
239 }
240 
241 /*
242  * We reset all the structures, and we reset the UMAC.
243  * After calling this routine, you're expected to reload
244  * the firmware.
245  */
246 int wil_reset(struct wil6210_priv *wil)
247 {
248 	int rc;
249 
250 	cancel_work_sync(&wil->disconnect_worker);
251 	wil6210_disconnect(wil, NULL);
252 
253 	wil6210_disable_irq(wil);
254 	wil->status = 0;
255 
256 	wmi_event_flush(wil);
257 
258 	flush_workqueue(wil->wmi_wq_conn);
259 	flush_workqueue(wil->wmi_wq);
260 
261 	/* TODO: put MAC in reset */
262 	wil_target_reset(wil);
263 
264 	/* init after reset */
265 	wil->pending_connect_cid = -1;
266 	INIT_COMPLETION(wil->wmi_ready);
267 
268 	wil_cache_mbox_regs(wil);
269 
270 	/* TODO: release MAC reset */
271 	wil6210_enable_irq(wil);
272 
273 	/* we just started MAC, wait for FW ready */
274 	rc = wil_wait_for_fw_ready(wil);
275 
276 	return rc;
277 }
278 
279 
280 void wil_link_on(struct wil6210_priv *wil)
281 {
282 	struct net_device *ndev = wil_to_ndev(wil);
283 
284 	wil_dbg_misc(wil, "%s()\n", __func__);
285 
286 	netif_carrier_on(ndev);
287 	netif_tx_wake_all_queues(ndev);
288 }
289 
290 void wil_link_off(struct wil6210_priv *wil)
291 {
292 	struct net_device *ndev = wil_to_ndev(wil);
293 
294 	wil_dbg_misc(wil, "%s()\n", __func__);
295 
296 	netif_tx_stop_all_queues(ndev);
297 	netif_carrier_off(ndev);
298 }
299 
300 static int __wil_up(struct wil6210_priv *wil)
301 {
302 	struct net_device *ndev = wil_to_ndev(wil);
303 	struct wireless_dev *wdev = wil->wdev;
304 	struct ieee80211_channel *channel = wdev->preset_chandef.chan;
305 	int rc;
306 	int bi;
307 	u16 wmi_nettype = wil_iftype_nl2wmi(wdev->iftype);
308 
309 	rc = wil_reset(wil);
310 	if (rc)
311 		return rc;
312 
313 	/* FIXME Firmware works now in PBSS mode(ToDS=0, FromDS=0) */
314 	wmi_nettype = wil_iftype_nl2wmi(NL80211_IFTYPE_ADHOC);
315 	switch (wdev->iftype) {
316 	case NL80211_IFTYPE_STATION:
317 		wil_dbg_misc(wil, "type: STATION\n");
318 		bi = 0;
319 		ndev->type = ARPHRD_ETHER;
320 		break;
321 	case NL80211_IFTYPE_AP:
322 		wil_dbg_misc(wil, "type: AP\n");
323 		bi = 100;
324 		ndev->type = ARPHRD_ETHER;
325 		break;
326 	case NL80211_IFTYPE_P2P_CLIENT:
327 		wil_dbg_misc(wil, "type: P2P_CLIENT\n");
328 		bi = 0;
329 		ndev->type = ARPHRD_ETHER;
330 		break;
331 	case NL80211_IFTYPE_P2P_GO:
332 		wil_dbg_misc(wil, "type: P2P_GO\n");
333 		bi = 100;
334 		ndev->type = ARPHRD_ETHER;
335 		break;
336 	case NL80211_IFTYPE_MONITOR:
337 		wil_dbg_misc(wil, "type: Monitor\n");
338 		bi = 0;
339 		ndev->type = ARPHRD_IEEE80211_RADIOTAP;
340 		/* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
341 		break;
342 	default:
343 		return -EOPNOTSUPP;
344 	}
345 
346 	/* Apply profile in the following order: */
347 	/* SSID and channel for the AP */
348 	switch (wdev->iftype) {
349 	case NL80211_IFTYPE_AP:
350 	case NL80211_IFTYPE_P2P_GO:
351 		if (wdev->ssid_len == 0) {
352 			wil_err(wil, "SSID not set\n");
353 			return -EINVAL;
354 		}
355 		wmi_set_ssid(wil, wdev->ssid_len, wdev->ssid);
356 		if (channel)
357 			wmi_set_channel(wil, channel->hw_value);
358 		break;
359 	default:
360 		break;
361 	}
362 
363 	/* MAC address - pre-requisite for other commands */
364 	wmi_set_mac_address(wil, ndev->dev_addr);
365 
366 	/* Set up beaconing if required. */
367 	rc = wmi_set_bcon(wil, bi, wmi_nettype);
368 	if (rc)
369 		return rc;
370 
371 	/* Rx VRING. After MAC and beacon */
372 	wil_rx_init(wil);
373 
374 	return 0;
375 }
376 
377 int wil_up(struct wil6210_priv *wil)
378 {
379 	int rc;
380 
381 	mutex_lock(&wil->mutex);
382 	rc = __wil_up(wil);
383 	mutex_unlock(&wil->mutex);
384 
385 	return rc;
386 }
387 
388 static int __wil_down(struct wil6210_priv *wil)
389 {
390 	if (wil->scan_request) {
391 		cfg80211_scan_done(wil->scan_request, true);
392 		wil->scan_request = NULL;
393 	}
394 
395 	wil6210_disconnect(wil, NULL);
396 	wil_rx_fini(wil);
397 
398 	return 0;
399 }
400 
401 int wil_down(struct wil6210_priv *wil)
402 {
403 	int rc;
404 
405 	mutex_lock(&wil->mutex);
406 	rc = __wil_down(wil);
407 	mutex_unlock(&wil->mutex);
408 
409 	return rc;
410 }
411