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/moduleparam.h>
18 #include <linux/if_arp.h>
19 
20 #include "wil6210.h"
21 
22 /*
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
27  *
28  * Force 32-bit transactions to enable NIC on 64-bit platforms
29  *
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.
33  */
34 void wil_memcpy_fromio_32(void *dst, const volatile void __iomem *src,
35 			  size_t count)
36 {
37 	u32 *d = dst;
38 	const volatile u32 __iomem *s = src;
39 
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++);
43 }
44 
45 void wil_memcpy_toio_32(volatile void __iomem *dst, const void *src,
46 			size_t count)
47 {
48 	volatile u32 __iomem *d = dst;
49 	const u32 *s = src;
50 
51 	for (count += 4; count > 4; count -= 4)
52 		__raw_writel(*s++, d++);
53 }
54 
55 static void _wil6210_disconnect(struct wil6210_priv *wil, void *bssid)
56 {
57 	uint i;
58 	struct net_device *ndev = wil_to_ndev(wil);
59 
60 	wil_dbg_misc(wil, "%s()\n", __func__);
61 
62 	wil_link_off(wil);
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,
67 				      NULL, 0, GFP_KERNEL);
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,
71 					GFP_KERNEL);
72 	}
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);
76 
77 	clear_bit(wil_status_dontscan, &wil->status);
78 }
79 
80 static void wil_disconnect_worker(struct work_struct *work)
81 {
82 	struct wil6210_priv *wil = container_of(work,
83 			struct wil6210_priv, disconnect_worker);
84 
85 	_wil6210_disconnect(wil, NULL);
86 }
87 
88 static void wil_connect_timer_fn(ulong x)
89 {
90 	struct wil6210_priv *wil = (void *)x;
91 
92 	wil_dbg_misc(wil, "Connect timeout\n");
93 
94 	/* reschedule to thread context - disconnect won't
95 	 * run from atomic context
96 	 */
97 	schedule_work(&wil->disconnect_worker);
98 }
99 
100 static void wil_connect_worker(struct work_struct *work)
101 {
102 	int rc;
103 	struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
104 						connect_worker);
105 	int cid = wil->pending_connect_cid;
106 
107 	if (cid < 0) {
108 		wil_err(wil, "No connection pending\n");
109 		return;
110 	}
111 
112 	wil_dbg_wmi(wil, "Configure for connection CID %d\n", cid);
113 
114 	rc = wil_vring_init_tx(wil, 0, WIL6210_TX_RING_SIZE, cid, 0);
115 	wil->pending_connect_cid = -1;
116 	if (rc == 0)
117 		wil_link_on(wil);
118 }
119 
120 int wil_priv_init(struct wil6210_priv *wil)
121 {
122 	wil_dbg_misc(wil, "%s()\n", __func__);
123 
124 	mutex_init(&wil->mutex);
125 	mutex_init(&wil->wmi_mutex);
126 
127 	init_completion(&wil->wmi_ready);
128 
129 	wil->pending_connect_cid = -1;
130 	setup_timer(&wil->connect_timer, wil_connect_timer_fn, (ulong)wil);
131 
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);
135 
136 	INIT_LIST_HEAD(&wil->pending_wmi_ev);
137 	spin_lock_init(&wil->wmi_ev_lock);
138 
139 	wil->wmi_wq = create_singlethread_workqueue(WIL_NAME"_wmi");
140 	if (!wil->wmi_wq)
141 		return -EAGAIN;
142 
143 	wil->wmi_wq_conn = create_singlethread_workqueue(WIL_NAME"_connect");
144 	if (!wil->wmi_wq_conn) {
145 		destroy_workqueue(wil->wmi_wq);
146 		return -EAGAIN;
147 	}
148 
149 	return 0;
150 }
151 
152 void wil6210_disconnect(struct wil6210_priv *wil, void *bssid)
153 {
154 	del_timer_sync(&wil->connect_timer);
155 	_wil6210_disconnect(wil, bssid);
156 }
157 
158 void wil_priv_deinit(struct wil6210_priv *wil)
159 {
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);
165 }
166 
167 static void wil_target_reset(struct wil6210_priv *wil)
168 {
169 	wil_dbg_misc(wil, "Resetting...\n");
170 
171 	/* register write */
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))
176 
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));
181 
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 */
184 
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);
189 
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);
194 
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);
198 
199 	wil_dbg_misc(wil, "Reset completed\n");
200 
201 #undef W
202 #undef S
203 }
204 
205 void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
206 {
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);
212 }
213 
214 static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
215 {
216 	ulong to = msecs_to_jiffies(1000);
217 	ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
218 	if (0 == left) {
219 		wil_err(wil, "Firmware not ready\n");
220 		return -ETIME;
221 	} else {
222 		wil_dbg_misc(wil, "FW ready after %d ms\n",
223 			     jiffies_to_msecs(to-left));
224 	}
225 	return 0;
226 }
227 
228 /*
229  * We reset all the structures, and we reset the UMAC.
230  * After calling this routine, you're expected to reload
231  * the firmware.
232  */
233 int wil_reset(struct wil6210_priv *wil)
234 {
235 	int rc;
236 
237 	cancel_work_sync(&wil->disconnect_worker);
238 	wil6210_disconnect(wil, NULL);
239 
240 	wil6210_disable_irq(wil);
241 	wil->status = 0;
242 
243 	wmi_event_flush(wil);
244 
245 	flush_workqueue(wil->wmi_wq_conn);
246 	flush_workqueue(wil->wmi_wq);
247 
248 	/* TODO: put MAC in reset */
249 	wil_target_reset(wil);
250 
251 	/* init after reset */
252 	wil->pending_connect_cid = -1;
253 	reinit_completion(&wil->wmi_ready);
254 
255 	/* TODO: release MAC reset */
256 	wil6210_enable_irq(wil);
257 
258 	/* we just started MAC, wait for FW ready */
259 	rc = wil_wait_for_fw_ready(wil);
260 
261 	return rc;
262 }
263 
264 
265 void wil_link_on(struct wil6210_priv *wil)
266 {
267 	struct net_device *ndev = wil_to_ndev(wil);
268 
269 	wil_dbg_misc(wil, "%s()\n", __func__);
270 
271 	netif_carrier_on(ndev);
272 	netif_tx_wake_all_queues(ndev);
273 }
274 
275 void wil_link_off(struct wil6210_priv *wil)
276 {
277 	struct net_device *ndev = wil_to_ndev(wil);
278 
279 	wil_dbg_misc(wil, "%s()\n", __func__);
280 
281 	netif_tx_stop_all_queues(ndev);
282 	netif_carrier_off(ndev);
283 }
284 
285 static int __wil_up(struct wil6210_priv *wil)
286 {
287 	struct net_device *ndev = wil_to_ndev(wil);
288 	struct wireless_dev *wdev = wil->wdev;
289 	int rc;
290 
291 	rc = wil_reset(wil);
292 	if (rc)
293 		return rc;
294 
295 	/* Rx VRING. After MAC and beacon */
296 	rc = wil_rx_init(wil);
297 	if (rc)
298 		return rc;
299 
300 	switch (wdev->iftype) {
301 	case NL80211_IFTYPE_STATION:
302 		wil_dbg_misc(wil, "type: STATION\n");
303 		ndev->type = ARPHRD_ETHER;
304 		break;
305 	case NL80211_IFTYPE_AP:
306 		wil_dbg_misc(wil, "type: AP\n");
307 		ndev->type = ARPHRD_ETHER;
308 		break;
309 	case NL80211_IFTYPE_P2P_CLIENT:
310 		wil_dbg_misc(wil, "type: P2P_CLIENT\n");
311 		ndev->type = ARPHRD_ETHER;
312 		break;
313 	case NL80211_IFTYPE_P2P_GO:
314 		wil_dbg_misc(wil, "type: P2P_GO\n");
315 		ndev->type = ARPHRD_ETHER;
316 		break;
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 ? */
321 		break;
322 	default:
323 		return -EOPNOTSUPP;
324 	}
325 
326 	/* MAC address - pre-requisite for other commands */
327 	wmi_set_mac_address(wil, ndev->dev_addr);
328 
329 
330 	napi_enable(&wil->napi_rx);
331 	napi_enable(&wil->napi_tx);
332 
333 	return 0;
334 }
335 
336 int wil_up(struct wil6210_priv *wil)
337 {
338 	int rc;
339 
340 	mutex_lock(&wil->mutex);
341 	rc = __wil_up(wil);
342 	mutex_unlock(&wil->mutex);
343 
344 	return rc;
345 }
346 
347 static int __wil_down(struct wil6210_priv *wil)
348 {
349 	napi_disable(&wil->napi_rx);
350 	napi_disable(&wil->napi_tx);
351 
352 	if (wil->scan_request) {
353 		cfg80211_scan_done(wil->scan_request, true);
354 		wil->scan_request = NULL;
355 	}
356 
357 	wil6210_disconnect(wil, NULL);
358 	wil_rx_fini(wil);
359 
360 	return 0;
361 }
362 
363 int wil_down(struct wil6210_priv *wil)
364 {
365 	int rc;
366 
367 	mutex_lock(&wil->mutex);
368 	rc = __wil_down(wil);
369 	mutex_unlock(&wil->mutex);
370 
371 	return rc;
372 }
373