blob: fd30cddd58821f9c602a3bba0fcb91bbfc88aeef [file] [log] [blame]
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001/*
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
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080017#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 */
34void 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
45void 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
55static void _wil6210_disconnect(struct wil6210_priv *wil, void *bssid)
56{
57 uint i;
58 struct net_device *ndev = wil_to_ndev(wil);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080059
Vladimir Kondratiev77438822013-01-28 18:31:06 +020060 wil_dbg_misc(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080061
62 wil_link_off(wil);
Vladimir Kondratievb338f742013-05-28 15:17:53 +030063 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,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080067 NULL, 0, GFP_KERNEL);
Vladimir Kondratievb338f742013-05-28 15:17:53 +030068 } else if (test_bit(wil_status_fwconnecting, &wil->status)) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080069 cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
70 WLAN_STATUS_UNSPECIFIED_FAILURE,
71 GFP_KERNEL);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080072 }
Vladimir Kondratievb338f742013-05-28 15:17:53 +030073 clear_bit(wil_status_fwconnecting, &wil->status);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080074 for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++)
75 wil_vring_fini_tx(wil, i);
Vladimir Kondratievb98917d2013-01-28 18:31:03 +020076
77 clear_bit(wil_status_dontscan, &wil->status);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080078}
79
80static 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
88static void wil_connect_timer_fn(ulong x)
89{
90 struct wil6210_priv *wil = (void *)x;
91
Vladimir Kondratiev77438822013-01-28 18:31:06 +020092 wil_dbg_misc(wil, "Connect timeout\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080093
94 /* reschedule to thread context - disconnect won't
95 * run from atomic context
96 */
97 schedule_work(&wil->disconnect_worker);
98}
99
Vladimir Kondratievd81079f2013-03-13 14:12:43 +0200100static 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
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800120int wil_priv_init(struct wil6210_priv *wil)
121{
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200122 wil_dbg_misc(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800123
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
Vladimir Kondratievd81079f2013-03-13 14:12:43 +0200132 INIT_WORK(&wil->connect_worker, wil_connect_worker);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800133 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
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800149 return 0;
150}
151
152void wil6210_disconnect(struct wil6210_priv *wil, void *bssid)
153{
154 del_timer_sync(&wil->connect_timer);
155 _wil6210_disconnect(wil, bssid);
156}
157
158void 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
167static void wil_target_reset(struct wil6210_priv *wil)
168{
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200169 wil_dbg_misc(wil, "Resetting...\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800170
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
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800185 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
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800190 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
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200199 wil_dbg_misc(wil, "Reset completed\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800200
201#undef W
202#undef S
203}
204
205void 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
214static 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 {
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200222 wil_dbg_misc(wil, "FW ready after %d ms\n",
Vladimir Kondratiev2057ebb2013-01-28 18:30:58 +0200223 jiffies_to_msecs(to-left));
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800224 }
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 */
233int wil_reset(struct wil6210_priv *wil)
234{
235 int rc;
236
237 cancel_work_sync(&wil->disconnect_worker);
238 wil6210_disconnect(wil, NULL);
239
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800240 wil6210_disable_irq(wil);
241 wil->status = 0;
242
Vladimir Kondratieve08b5902013-01-28 18:31:05 +0200243 wmi_event_flush(wil);
244
245 flush_workqueue(wil->wmi_wq_conn);
246 flush_workqueue(wil->wmi_wq);
247
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800248 /* TODO: put MAC in reset */
249 wil_target_reset(wil);
250
251 /* init after reset */
252 wil->pending_connect_cid = -1;
Wolfram Sang16735d02013-11-14 14:32:02 -0800253 reinit_completion(&wil->wmi_ready);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800254
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800255 /* 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
265void wil_link_on(struct wil6210_priv *wil)
266{
267 struct net_device *ndev = wil_to_ndev(wil);
268
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200269 wil_dbg_misc(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800270
271 netif_carrier_on(ndev);
272 netif_tx_wake_all_queues(ndev);
273}
274
275void wil_link_off(struct wil6210_priv *wil)
276{
277 struct net_device *ndev = wil_to_ndev(wil);
278
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200279 wil_dbg_misc(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800280
281 netif_tx_stop_all_queues(ndev);
282 netif_carrier_off(ndev);
283}
284
285static int __wil_up(struct wil6210_priv *wil)
286{
287 struct net_device *ndev = wil_to_ndev(wil);
288 struct wireless_dev *wdev = wil->wdev;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800289 int rc;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800290
291 rc = wil_reset(wil);
292 if (rc)
293 return rc;
294
Vladimir Kondratieve31b2562013-06-09 09:12:55 +0300295 /* Rx VRING. After MAC and beacon */
296 rc = wil_rx_init(wil);
297 if (rc)
298 return rc;
299
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800300 switch (wdev->iftype) {
301 case NL80211_IFTYPE_STATION:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200302 wil_dbg_misc(wil, "type: STATION\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800303 ndev->type = ARPHRD_ETHER;
304 break;
305 case NL80211_IFTYPE_AP:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200306 wil_dbg_misc(wil, "type: AP\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800307 ndev->type = ARPHRD_ETHER;
308 break;
309 case NL80211_IFTYPE_P2P_CLIENT:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200310 wil_dbg_misc(wil, "type: P2P_CLIENT\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800311 ndev->type = ARPHRD_ETHER;
312 break;
313 case NL80211_IFTYPE_P2P_GO:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200314 wil_dbg_misc(wil, "type: P2P_GO\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800315 ndev->type = ARPHRD_ETHER;
316 break;
317 case NL80211_IFTYPE_MONITOR:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200318 wil_dbg_misc(wil, "type: Monitor\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800319 ndev->type = ARPHRD_IEEE80211_RADIOTAP;
320 /* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
321 break;
322 default:
323 return -EOPNOTSUPP;
324 }
325
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800326 /* MAC address - pre-requisite for other commands */
327 wmi_set_mac_address(wil, ndev->dev_addr);
328
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800329
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300330 napi_enable(&wil->napi_rx);
331 napi_enable(&wil->napi_tx);
332
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800333 return 0;
334}
335
336int 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
347static int __wil_down(struct wil6210_priv *wil)
348{
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300349 napi_disable(&wil->napi_rx);
350 napi_disable(&wil->napi_tx);
351
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800352 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
363int 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}