blob: c97b864667c57db00226daa95e5e53ece033f1aa [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;
253 INIT_COMPLETION(wil->wmi_ready);
254
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;
289 struct ieee80211_channel *channel = wdev->preset_chandef.chan;
290 int rc;
291 int bi;
292 u16 wmi_nettype = wil_iftype_nl2wmi(wdev->iftype);
293
294 rc = wil_reset(wil);
295 if (rc)
296 return rc;
297
298 /* FIXME Firmware works now in PBSS mode(ToDS=0, FromDS=0) */
299 wmi_nettype = wil_iftype_nl2wmi(NL80211_IFTYPE_ADHOC);
300 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 bi = 0;
304 ndev->type = ARPHRD_ETHER;
305 break;
306 case NL80211_IFTYPE_AP:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200307 wil_dbg_misc(wil, "type: AP\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800308 bi = 100;
309 ndev->type = ARPHRD_ETHER;
310 break;
311 case NL80211_IFTYPE_P2P_CLIENT:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200312 wil_dbg_misc(wil, "type: P2P_CLIENT\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800313 bi = 0;
314 ndev->type = ARPHRD_ETHER;
315 break;
316 case NL80211_IFTYPE_P2P_GO:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200317 wil_dbg_misc(wil, "type: P2P_GO\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800318 bi = 100;
319 ndev->type = ARPHRD_ETHER;
320 break;
321 case NL80211_IFTYPE_MONITOR:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200322 wil_dbg_misc(wil, "type: Monitor\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800323 bi = 0;
324 ndev->type = ARPHRD_IEEE80211_RADIOTAP;
325 /* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
326 break;
327 default:
328 return -EOPNOTSUPP;
329 }
330
331 /* Apply profile in the following order: */
332 /* SSID and channel for the AP */
333 switch (wdev->iftype) {
334 case NL80211_IFTYPE_AP:
335 case NL80211_IFTYPE_P2P_GO:
336 if (wdev->ssid_len == 0) {
337 wil_err(wil, "SSID not set\n");
338 return -EINVAL;
339 }
Vladimir Kondratievb8023172013-03-13 14:12:50 +0200340 rc = wmi_set_ssid(wil, wdev->ssid_len, wdev->ssid);
341 if (rc)
342 return rc;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800343 break;
344 default:
Vladimir Kondratievafda8bb2013-01-28 18:31:07 +0200345 break;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800346 }
347
348 /* MAC address - pre-requisite for other commands */
349 wmi_set_mac_address(wil, ndev->dev_addr);
350
351 /* Set up beaconing if required. */
Vladimir Kondratievb8023172013-03-13 14:12:50 +0200352 if (bi > 0) {
353 rc = wmi_pcp_start(wil, bi, wmi_nettype,
354 (channel ? channel->hw_value : 0));
355 if (rc)
356 return rc;
357 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800358
359 /* Rx VRING. After MAC and beacon */
360 wil_rx_init(wil);
361
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300362 napi_enable(&wil->napi_rx);
363 napi_enable(&wil->napi_tx);
364
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800365 return 0;
366}
367
368int wil_up(struct wil6210_priv *wil)
369{
370 int rc;
371
372 mutex_lock(&wil->mutex);
373 rc = __wil_up(wil);
374 mutex_unlock(&wil->mutex);
375
376 return rc;
377}
378
379static int __wil_down(struct wil6210_priv *wil)
380{
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300381 napi_disable(&wil->napi_rx);
382 napi_disable(&wil->napi_tx);
383
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800384 if (wil->scan_request) {
385 cfg80211_scan_done(wil->scan_request, true);
386 wil->scan_request = NULL;
387 }
388
389 wil6210_disconnect(wil, NULL);
390 wil_rx_fini(wil);
391
392 return 0;
393}
394
395int wil_down(struct wil6210_priv *wil)
396{
397 int rc;
398
399 mutex_lock(&wil->mutex);
400 rc = __wil_down(wil);
401 mutex_unlock(&wil->mutex);
402
403 return rc;
404}