blob: a0ea135efd11a3a138cc95ae719d902d7cc56f22 [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>
Vladimir Kondratiev108d1eb2014-02-27 16:20:53 +020019#include <linux/etherdevice.h>
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080020
21#include "wil6210.h"
Vladimir Kondratievb4490f42014-02-27 16:20:44 +020022#include "txrx.h"
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080023
24/*
25 * Due to a hardware issue,
26 * one has to read/write to/from NIC in 32-bit chunks;
27 * regular memcpy_fromio and siblings will
28 * not work on 64-bit platform - it uses 64-bit transactions
29 *
30 * Force 32-bit transactions to enable NIC on 64-bit platforms
31 *
32 * To avoid byte swap on big endian host, __raw_{read|write}l
33 * should be used - {read|write}l would swap bytes to provide
34 * little endian on PCI value in host endianness.
35 */
36void wil_memcpy_fromio_32(void *dst, const volatile void __iomem *src,
37 size_t count)
38{
39 u32 *d = dst;
40 const volatile u32 __iomem *s = src;
41
42 /* size_t is unsigned, if (count%4 != 0) it will wrap */
43 for (count += 4; count > 4; count -= 4)
44 *d++ = __raw_readl(s++);
45}
46
47void wil_memcpy_toio_32(volatile void __iomem *dst, const void *src,
48 size_t count)
49{
50 volatile u32 __iomem *d = dst;
51 const u32 *s = src;
52
53 for (count += 4; count > 4; count -= 4)
54 __raw_writel(*s++, d++);
55}
56
Vladimir Kondratiev91886b02014-02-27 16:20:50 +020057static void wil_disconnect_cid(struct wil6210_priv *wil, int cid)
58{
59 uint i;
60 struct wil_sta_info *sta = &wil->sta[cid];
61 for (i = 0; i < WIL_STA_TID_NUM; i++) {
62 struct wil_tid_ampdu_rx *r = sta->tid_rx[i];
63 sta->tid_rx[i] = NULL;
64 wil_tid_ampdu_rx_free(wil, r);
65 }
66 for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++) {
67 if (wil->vring2cid_tid[i][0] == cid)
68 wil_vring_fini_tx(wil, i);
69 }
70 memset(&sta->stats, 0, sizeof(sta->stats));
71}
72
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080073static void _wil6210_disconnect(struct wil6210_priv *wil, void *bssid)
74{
Vladimir Kondratiev91886b02014-02-27 16:20:50 +020075 int cid = -ENOENT;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080076 struct net_device *ndev = wil_to_ndev(wil);
Vladimir Kondratiev91886b02014-02-27 16:20:50 +020077 struct wireless_dev *wdev = wil->wdev;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080078
Vladimir Kondratiev91886b02014-02-27 16:20:50 +020079 might_sleep();
80 if (bssid) {
81 cid = wil_find_cid(wil, bssid);
82 wil_dbg_misc(wil, "%s(%pM, CID %d)\n", __func__, bssid, cid);
83 } else {
84 wil_dbg_misc(wil, "%s(all)\n", __func__);
85 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080086
Vladimir Kondratiev91886b02014-02-27 16:20:50 +020087 if (cid >= 0) /* disconnect 1 peer */
88 wil_disconnect_cid(wil, cid);
89 else /* disconnect all */
90 for (cid = 0; cid < WIL6210_MAX_CID; cid++)
91 wil_disconnect_cid(wil, cid);
92
93 /* link state */
94 switch (wdev->iftype) {
95 case NL80211_IFTYPE_STATION:
96 case NL80211_IFTYPE_P2P_CLIENT:
97 wil_link_off(wil);
98 if (test_bit(wil_status_fwconnected, &wil->status)) {
99 clear_bit(wil_status_fwconnected, &wil->status);
100 cfg80211_disconnected(ndev,
101 WLAN_STATUS_UNSPECIFIED_FAILURE,
102 NULL, 0, GFP_KERNEL);
103 } else if (test_bit(wil_status_fwconnecting, &wil->status)) {
104 cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
105 WLAN_STATUS_UNSPECIFIED_FAILURE,
106 GFP_KERNEL);
Vladimir Kondratievb4490f42014-02-27 16:20:44 +0200107 }
Vladimir Kondratiev91886b02014-02-27 16:20:50 +0200108 clear_bit(wil_status_fwconnecting, &wil->status);
109 wil_dbg_misc(wil, "clear_bit(wil_status_dontscan)\n");
110 clear_bit(wil_status_dontscan, &wil->status);
111 break;
112 default:
113 /* AP-like interface and monitor:
114 * never scan, always connected
115 */
116 if (bssid)
117 cfg80211_del_sta(ndev, bssid, GFP_KERNEL);
118 break;
Vladimir Kondratievb4490f42014-02-27 16:20:44 +0200119 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800120}
121
122static void wil_disconnect_worker(struct work_struct *work)
123{
124 struct wil6210_priv *wil = container_of(work,
125 struct wil6210_priv, disconnect_worker);
126
127 _wil6210_disconnect(wil, NULL);
128}
129
130static void wil_connect_timer_fn(ulong x)
131{
132 struct wil6210_priv *wil = (void *)x;
133
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200134 wil_dbg_misc(wil, "Connect timeout\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800135
136 /* reschedule to thread context - disconnect won't
137 * run from atomic context
138 */
139 schedule_work(&wil->disconnect_worker);
140}
141
Vladimir Kondratiev9a177382014-02-27 16:20:45 +0200142static int wil_find_free_vring(struct wil6210_priv *wil)
143{
144 int i;
145 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
146 if (!wil->vring_tx[i].va)
147 return i;
148 }
149 return -EINVAL;
150}
151
Vladimir Kondratievd81079f2013-03-13 14:12:43 +0200152static void wil_connect_worker(struct work_struct *work)
153{
154 int rc;
155 struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
156 connect_worker);
157 int cid = wil->pending_connect_cid;
Vladimir Kondratiev9a177382014-02-27 16:20:45 +0200158 int ringid = wil_find_free_vring(wil);
Vladimir Kondratievd81079f2013-03-13 14:12:43 +0200159
160 if (cid < 0) {
161 wil_err(wil, "No connection pending\n");
162 return;
163 }
164
165 wil_dbg_wmi(wil, "Configure for connection CID %d\n", cid);
166
Vladimir Kondratiev9a177382014-02-27 16:20:45 +0200167 rc = wil_vring_init_tx(wil, ringid, WIL6210_TX_RING_SIZE, cid, 0);
Vladimir Kondratievd81079f2013-03-13 14:12:43 +0200168 wil->pending_connect_cid = -1;
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200169 if (rc == 0) {
170 wil->sta[cid].status = wil_sta_connected;
Vladimir Kondratievd81079f2013-03-13 14:12:43 +0200171 wil_link_on(wil);
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200172 } else {
173 wil->sta[cid].status = wil_sta_unused;
174 }
Vladimir Kondratievd81079f2013-03-13 14:12:43 +0200175}
176
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800177int wil_priv_init(struct wil6210_priv *wil)
178{
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200179 wil_dbg_misc(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800180
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200181 memset(wil->sta, 0, sizeof(wil->sta));
182
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800183 mutex_init(&wil->mutex);
184 mutex_init(&wil->wmi_mutex);
185
186 init_completion(&wil->wmi_ready);
187
188 wil->pending_connect_cid = -1;
189 setup_timer(&wil->connect_timer, wil_connect_timer_fn, (ulong)wil);
190
Vladimir Kondratievd81079f2013-03-13 14:12:43 +0200191 INIT_WORK(&wil->connect_worker, wil_connect_worker);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800192 INIT_WORK(&wil->disconnect_worker, wil_disconnect_worker);
193 INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
194
195 INIT_LIST_HEAD(&wil->pending_wmi_ev);
196 spin_lock_init(&wil->wmi_ev_lock);
197
198 wil->wmi_wq = create_singlethread_workqueue(WIL_NAME"_wmi");
199 if (!wil->wmi_wq)
200 return -EAGAIN;
201
202 wil->wmi_wq_conn = create_singlethread_workqueue(WIL_NAME"_connect");
203 if (!wil->wmi_wq_conn) {
204 destroy_workqueue(wil->wmi_wq);
205 return -EAGAIN;
206 }
207
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800208 return 0;
209}
210
211void wil6210_disconnect(struct wil6210_priv *wil, void *bssid)
212{
213 del_timer_sync(&wil->connect_timer);
214 _wil6210_disconnect(wil, bssid);
215}
216
217void wil_priv_deinit(struct wil6210_priv *wil)
218{
219 cancel_work_sync(&wil->disconnect_worker);
220 wil6210_disconnect(wil, NULL);
221 wmi_event_flush(wil);
222 destroy_workqueue(wil->wmi_wq_conn);
223 destroy_workqueue(wil->wmi_wq);
224}
225
226static void wil_target_reset(struct wil6210_priv *wil)
227{
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200228 wil_dbg_misc(wil, "Resetting...\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800229
230 /* register write */
231#define W(a, v) iowrite32(v, wil->csr + HOSTADDR(a))
232 /* register set = read, OR, write */
233#define S(a, v) iowrite32(ioread32(wil->csr + HOSTADDR(a)) | v, \
234 wil->csr + HOSTADDR(a))
235
236 /* hpal_perst_from_pad_src_n_mask */
237 S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(6));
238 /* car_perst_rst_src_n_mask */
239 S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(7));
240
241 W(RGF_USER_MAC_CPU_0, BIT(1)); /* mac_cpu_man_rst */
242 W(RGF_USER_USER_CPU_0, BIT(1)); /* user_cpu_man_rst */
243
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800244 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
245 W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
246 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000170);
247 W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FC00);
248
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800249 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
250 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
251 W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
252 W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
253
254 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000001);
255 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00000080);
256 W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
257
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200258 wil_dbg_misc(wil, "Reset completed\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800259
260#undef W
261#undef S
262}
263
264void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
265{
266 le32_to_cpus(&r->base);
267 le16_to_cpus(&r->entry_size);
268 le16_to_cpus(&r->size);
269 le32_to_cpus(&r->tail);
270 le32_to_cpus(&r->head);
271}
272
273static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
274{
275 ulong to = msecs_to_jiffies(1000);
276 ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
277 if (0 == left) {
278 wil_err(wil, "Firmware not ready\n");
279 return -ETIME;
280 } else {
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200281 wil_dbg_misc(wil, "FW ready after %d ms\n",
Vladimir Kondratiev2057ebb2013-01-28 18:30:58 +0200282 jiffies_to_msecs(to-left));
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800283 }
284 return 0;
285}
286
287/*
288 * We reset all the structures, and we reset the UMAC.
289 * After calling this routine, you're expected to reload
290 * the firmware.
291 */
292int wil_reset(struct wil6210_priv *wil)
293{
294 int rc;
295
296 cancel_work_sync(&wil->disconnect_worker);
297 wil6210_disconnect(wil, NULL);
298
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800299 wil6210_disable_irq(wil);
300 wil->status = 0;
301
Vladimir Kondratieve08b5902013-01-28 18:31:05 +0200302 wmi_event_flush(wil);
303
304 flush_workqueue(wil->wmi_wq_conn);
305 flush_workqueue(wil->wmi_wq);
306
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800307 /* TODO: put MAC in reset */
308 wil_target_reset(wil);
309
310 /* init after reset */
311 wil->pending_connect_cid = -1;
Wolfram Sang16735d02013-11-14 14:32:02 -0800312 reinit_completion(&wil->wmi_ready);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800313
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800314 /* TODO: release MAC reset */
315 wil6210_enable_irq(wil);
316
317 /* we just started MAC, wait for FW ready */
318 rc = wil_wait_for_fw_ready(wil);
319
320 return rc;
321}
322
323
324void wil_link_on(struct wil6210_priv *wil)
325{
326 struct net_device *ndev = wil_to_ndev(wil);
327
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200328 wil_dbg_misc(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800329
330 netif_carrier_on(ndev);
331 netif_tx_wake_all_queues(ndev);
332}
333
334void wil_link_off(struct wil6210_priv *wil)
335{
336 struct net_device *ndev = wil_to_ndev(wil);
337
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200338 wil_dbg_misc(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800339
340 netif_tx_stop_all_queues(ndev);
341 netif_carrier_off(ndev);
342}
343
344static int __wil_up(struct wil6210_priv *wil)
345{
346 struct net_device *ndev = wil_to_ndev(wil);
347 struct wireless_dev *wdev = wil->wdev;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800348 int rc;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800349
350 rc = wil_reset(wil);
351 if (rc)
352 return rc;
353
Vladimir Kondratieve31b2562013-06-09 09:12:55 +0300354 /* Rx VRING. After MAC and beacon */
355 rc = wil_rx_init(wil);
356 if (rc)
357 return rc;
358
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800359 switch (wdev->iftype) {
360 case NL80211_IFTYPE_STATION:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200361 wil_dbg_misc(wil, "type: STATION\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800362 ndev->type = ARPHRD_ETHER;
363 break;
364 case NL80211_IFTYPE_AP:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200365 wil_dbg_misc(wil, "type: AP\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800366 ndev->type = ARPHRD_ETHER;
367 break;
368 case NL80211_IFTYPE_P2P_CLIENT:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200369 wil_dbg_misc(wil, "type: P2P_CLIENT\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800370 ndev->type = ARPHRD_ETHER;
371 break;
372 case NL80211_IFTYPE_P2P_GO:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200373 wil_dbg_misc(wil, "type: P2P_GO\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800374 ndev->type = ARPHRD_ETHER;
375 break;
376 case NL80211_IFTYPE_MONITOR:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200377 wil_dbg_misc(wil, "type: Monitor\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800378 ndev->type = ARPHRD_IEEE80211_RADIOTAP;
379 /* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
380 break;
381 default:
382 return -EOPNOTSUPP;
383 }
384
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800385 /* MAC address - pre-requisite for other commands */
386 wmi_set_mac_address(wil, ndev->dev_addr);
387
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800388
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300389 napi_enable(&wil->napi_rx);
390 napi_enable(&wil->napi_tx);
391
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800392 return 0;
393}
394
395int wil_up(struct wil6210_priv *wil)
396{
397 int rc;
398
399 mutex_lock(&wil->mutex);
400 rc = __wil_up(wil);
401 mutex_unlock(&wil->mutex);
402
403 return rc;
404}
405
406static int __wil_down(struct wil6210_priv *wil)
407{
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300408 napi_disable(&wil->napi_rx);
409 napi_disable(&wil->napi_tx);
410
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800411 if (wil->scan_request) {
412 cfg80211_scan_done(wil->scan_request, true);
413 wil->scan_request = NULL;
414 }
415
416 wil6210_disconnect(wil, NULL);
417 wil_rx_fini(wil);
418
419 return 0;
420}
421
422int wil_down(struct wil6210_priv *wil)
423{
424 int rc;
425
426 mutex_lock(&wil->mutex);
427 rc = __wil_down(wil);
428 mutex_unlock(&wil->mutex);
429
430 return rc;
431}
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200432
433int wil_find_cid(struct wil6210_priv *wil, const u8 *mac)
434{
435 int i;
436 int rc = -ENOENT;
437
438 for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
439 if ((wil->sta[i].status != wil_sta_unused) &&
Vladimir Kondratiev108d1eb2014-02-27 16:20:53 +0200440 ether_addr_equal(wil->sta[i].addr, mac)) {
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200441 rc = i;
442 break;
443 }
444 }
445
446 return rc;
447}