blob: 38906f1bc769c3345e3bd8bb8ec14f73adfc09d9 [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"
Vladimir Kondratievb4490f42014-02-27 16:20:44 +020021#include "txrx.h"
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080022
23/*
24 * Due to a hardware issue,
25 * one has to read/write to/from NIC in 32-bit chunks;
26 * regular memcpy_fromio and siblings will
27 * not work on 64-bit platform - it uses 64-bit transactions
28 *
29 * Force 32-bit transactions to enable NIC on 64-bit platforms
30 *
31 * To avoid byte swap on big endian host, __raw_{read|write}l
32 * should be used - {read|write}l would swap bytes to provide
33 * little endian on PCI value in host endianness.
34 */
35void wil_memcpy_fromio_32(void *dst, const volatile void __iomem *src,
36 size_t count)
37{
38 u32 *d = dst;
39 const volatile u32 __iomem *s = src;
40
41 /* size_t is unsigned, if (count%4 != 0) it will wrap */
42 for (count += 4; count > 4; count -= 4)
43 *d++ = __raw_readl(s++);
44}
45
46void wil_memcpy_toio_32(volatile void __iomem *dst, const void *src,
47 size_t count)
48{
49 volatile u32 __iomem *d = dst;
50 const u32 *s = src;
51
52 for (count += 4; count > 4; count -= 4)
53 __raw_writel(*s++, d++);
54}
55
56static void _wil6210_disconnect(struct wil6210_priv *wil, void *bssid)
57{
Vladimir Kondratievb4490f42014-02-27 16:20:44 +020058 uint i, cid;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080059 struct net_device *ndev = wil_to_ndev(wil);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080060
Vladimir Kondratiev77438822013-01-28 18:31:06 +020061 wil_dbg_misc(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080062
Vladimir Kondratievb4490f42014-02-27 16:20:44 +020063 for (cid = 0; cid < WIL6210_MAX_CID; cid++) {
64 struct wil_sta_info *sta = &wil->sta[cid];
65 for (i = 0; i < WIL_STA_TID_NUM; i++) {
66 struct wil_tid_ampdu_rx *r = sta->tid_rx[i];
67 sta->tid_rx[i] = NULL;
68 wil_tid_ampdu_rx_free(wil, r);
69 }
70 }
71
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080072 wil_link_off(wil);
Vladimir Kondratievb338f742013-05-28 15:17:53 +030073 if (test_bit(wil_status_fwconnected, &wil->status)) {
74 clear_bit(wil_status_fwconnected, &wil->status);
75 cfg80211_disconnected(ndev,
76 WLAN_STATUS_UNSPECIFIED_FAILURE,
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080077 NULL, 0, GFP_KERNEL);
Vladimir Kondratievb338f742013-05-28 15:17:53 +030078 } else if (test_bit(wil_status_fwconnecting, &wil->status)) {
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080079 cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
80 WLAN_STATUS_UNSPECIFIED_FAILURE,
81 GFP_KERNEL);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080082 }
Vladimir Kondratievb338f742013-05-28 15:17:53 +030083 clear_bit(wil_status_fwconnecting, &wil->status);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080084 for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++)
85 wil_vring_fini_tx(wil, i);
Vladimir Kondratievb98917d2013-01-28 18:31:03 +020086
87 clear_bit(wil_status_dontscan, &wil->status);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080088}
89
90static void wil_disconnect_worker(struct work_struct *work)
91{
92 struct wil6210_priv *wil = container_of(work,
93 struct wil6210_priv, disconnect_worker);
94
95 _wil6210_disconnect(wil, NULL);
96}
97
98static void wil_connect_timer_fn(ulong x)
99{
100 struct wil6210_priv *wil = (void *)x;
101
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200102 wil_dbg_misc(wil, "Connect timeout\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800103
104 /* reschedule to thread context - disconnect won't
105 * run from atomic context
106 */
107 schedule_work(&wil->disconnect_worker);
108}
109
Vladimir Kondratievd81079f2013-03-13 14:12:43 +0200110static void wil_connect_worker(struct work_struct *work)
111{
112 int rc;
113 struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
114 connect_worker);
115 int cid = wil->pending_connect_cid;
116
117 if (cid < 0) {
118 wil_err(wil, "No connection pending\n");
119 return;
120 }
121
122 wil_dbg_wmi(wil, "Configure for connection CID %d\n", cid);
123
124 rc = wil_vring_init_tx(wil, 0, WIL6210_TX_RING_SIZE, cid, 0);
125 wil->pending_connect_cid = -1;
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200126 if (rc == 0) {
127 wil->sta[cid].status = wil_sta_connected;
Vladimir Kondratievd81079f2013-03-13 14:12:43 +0200128 wil_link_on(wil);
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200129 } else {
130 wil->sta[cid].status = wil_sta_unused;
131 }
Vladimir Kondratievd81079f2013-03-13 14:12:43 +0200132}
133
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800134int wil_priv_init(struct wil6210_priv *wil)
135{
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200136 wil_dbg_misc(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800137
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200138 memset(wil->sta, 0, sizeof(wil->sta));
139
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800140 mutex_init(&wil->mutex);
141 mutex_init(&wil->wmi_mutex);
142
143 init_completion(&wil->wmi_ready);
144
145 wil->pending_connect_cid = -1;
146 setup_timer(&wil->connect_timer, wil_connect_timer_fn, (ulong)wil);
147
Vladimir Kondratievd81079f2013-03-13 14:12:43 +0200148 INIT_WORK(&wil->connect_worker, wil_connect_worker);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800149 INIT_WORK(&wil->disconnect_worker, wil_disconnect_worker);
150 INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
151
152 INIT_LIST_HEAD(&wil->pending_wmi_ev);
153 spin_lock_init(&wil->wmi_ev_lock);
154
155 wil->wmi_wq = create_singlethread_workqueue(WIL_NAME"_wmi");
156 if (!wil->wmi_wq)
157 return -EAGAIN;
158
159 wil->wmi_wq_conn = create_singlethread_workqueue(WIL_NAME"_connect");
160 if (!wil->wmi_wq_conn) {
161 destroy_workqueue(wil->wmi_wq);
162 return -EAGAIN;
163 }
164
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800165 return 0;
166}
167
168void wil6210_disconnect(struct wil6210_priv *wil, void *bssid)
169{
170 del_timer_sync(&wil->connect_timer);
171 _wil6210_disconnect(wil, bssid);
172}
173
174void wil_priv_deinit(struct wil6210_priv *wil)
175{
176 cancel_work_sync(&wil->disconnect_worker);
177 wil6210_disconnect(wil, NULL);
178 wmi_event_flush(wil);
179 destroy_workqueue(wil->wmi_wq_conn);
180 destroy_workqueue(wil->wmi_wq);
181}
182
183static void wil_target_reset(struct wil6210_priv *wil)
184{
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200185 wil_dbg_misc(wil, "Resetting...\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800186
187 /* register write */
188#define W(a, v) iowrite32(v, wil->csr + HOSTADDR(a))
189 /* register set = read, OR, write */
190#define S(a, v) iowrite32(ioread32(wil->csr + HOSTADDR(a)) | v, \
191 wil->csr + HOSTADDR(a))
192
193 /* hpal_perst_from_pad_src_n_mask */
194 S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(6));
195 /* car_perst_rst_src_n_mask */
196 S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(7));
197
198 W(RGF_USER_MAC_CPU_0, BIT(1)); /* mac_cpu_man_rst */
199 W(RGF_USER_USER_CPU_0, BIT(1)); /* user_cpu_man_rst */
200
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800201 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
202 W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
203 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000170);
204 W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FC00);
205
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800206 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
207 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
208 W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
209 W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
210
211 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000001);
212 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00000080);
213 W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
214
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200215 wil_dbg_misc(wil, "Reset completed\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800216
217#undef W
218#undef S
219}
220
221void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
222{
223 le32_to_cpus(&r->base);
224 le16_to_cpus(&r->entry_size);
225 le16_to_cpus(&r->size);
226 le32_to_cpus(&r->tail);
227 le32_to_cpus(&r->head);
228}
229
230static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
231{
232 ulong to = msecs_to_jiffies(1000);
233 ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
234 if (0 == left) {
235 wil_err(wil, "Firmware not ready\n");
236 return -ETIME;
237 } else {
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200238 wil_dbg_misc(wil, "FW ready after %d ms\n",
Vladimir Kondratiev2057ebb2013-01-28 18:30:58 +0200239 jiffies_to_msecs(to-left));
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800240 }
241 return 0;
242}
243
244/*
245 * We reset all the structures, and we reset the UMAC.
246 * After calling this routine, you're expected to reload
247 * the firmware.
248 */
249int wil_reset(struct wil6210_priv *wil)
250{
251 int rc;
252
253 cancel_work_sync(&wil->disconnect_worker);
254 wil6210_disconnect(wil, NULL);
255
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800256 wil6210_disable_irq(wil);
257 wil->status = 0;
258
Vladimir Kondratieve08b5902013-01-28 18:31:05 +0200259 wmi_event_flush(wil);
260
261 flush_workqueue(wil->wmi_wq_conn);
262 flush_workqueue(wil->wmi_wq);
263
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800264 /* TODO: put MAC in reset */
265 wil_target_reset(wil);
266
267 /* init after reset */
268 wil->pending_connect_cid = -1;
Wolfram Sang16735d02013-11-14 14:32:02 -0800269 reinit_completion(&wil->wmi_ready);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800270
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800271 /* TODO: release MAC reset */
272 wil6210_enable_irq(wil);
273
274 /* we just started MAC, wait for FW ready */
275 rc = wil_wait_for_fw_ready(wil);
276
277 return rc;
278}
279
280
281void wil_link_on(struct wil6210_priv *wil)
282{
283 struct net_device *ndev = wil_to_ndev(wil);
284
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200285 wil_dbg_misc(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800286
287 netif_carrier_on(ndev);
288 netif_tx_wake_all_queues(ndev);
289}
290
291void wil_link_off(struct wil6210_priv *wil)
292{
293 struct net_device *ndev = wil_to_ndev(wil);
294
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200295 wil_dbg_misc(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800296
297 netif_tx_stop_all_queues(ndev);
298 netif_carrier_off(ndev);
299}
300
301static int __wil_up(struct wil6210_priv *wil)
302{
303 struct net_device *ndev = wil_to_ndev(wil);
304 struct wireless_dev *wdev = wil->wdev;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800305 int rc;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800306
307 rc = wil_reset(wil);
308 if (rc)
309 return rc;
310
Vladimir Kondratieve31b2562013-06-09 09:12:55 +0300311 /* Rx VRING. After MAC and beacon */
312 rc = wil_rx_init(wil);
313 if (rc)
314 return rc;
315
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800316 switch (wdev->iftype) {
317 case NL80211_IFTYPE_STATION:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200318 wil_dbg_misc(wil, "type: STATION\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800319 ndev->type = ARPHRD_ETHER;
320 break;
321 case NL80211_IFTYPE_AP:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200322 wil_dbg_misc(wil, "type: AP\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800323 ndev->type = ARPHRD_ETHER;
324 break;
325 case NL80211_IFTYPE_P2P_CLIENT:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200326 wil_dbg_misc(wil, "type: P2P_CLIENT\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800327 ndev->type = ARPHRD_ETHER;
328 break;
329 case NL80211_IFTYPE_P2P_GO:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200330 wil_dbg_misc(wil, "type: P2P_GO\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800331 ndev->type = ARPHRD_ETHER;
332 break;
333 case NL80211_IFTYPE_MONITOR:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200334 wil_dbg_misc(wil, "type: Monitor\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800335 ndev->type = ARPHRD_IEEE80211_RADIOTAP;
336 /* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
337 break;
338 default:
339 return -EOPNOTSUPP;
340 }
341
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800342 /* MAC address - pre-requisite for other commands */
343 wmi_set_mac_address(wil, ndev->dev_addr);
344
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800345
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300346 napi_enable(&wil->napi_rx);
347 napi_enable(&wil->napi_tx);
348
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800349 return 0;
350}
351
352int wil_up(struct wil6210_priv *wil)
353{
354 int rc;
355
356 mutex_lock(&wil->mutex);
357 rc = __wil_up(wil);
358 mutex_unlock(&wil->mutex);
359
360 return rc;
361}
362
363static int __wil_down(struct wil6210_priv *wil)
364{
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300365 napi_disable(&wil->napi_rx);
366 napi_disable(&wil->napi_tx);
367
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800368 if (wil->scan_request) {
369 cfg80211_scan_done(wil->scan_request, true);
370 wil->scan_request = NULL;
371 }
372
373 wil6210_disconnect(wil, NULL);
374 wil_rx_fini(wil);
375
376 return 0;
377}
378
379int wil_down(struct wil6210_priv *wil)
380{
381 int rc;
382
383 mutex_lock(&wil->mutex);
384 rc = __wil_down(wil);
385 mutex_unlock(&wil->mutex);
386
387 return rc;
388}
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200389
390int wil_find_cid(struct wil6210_priv *wil, const u8 *mac)
391{
392 int i;
393 int rc = -ENOENT;
394
395 for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
396 if ((wil->sta[i].status != wil_sta_unused) &&
397 (0 == memcmp(wil->sta[i].addr, mac, ETH_ALEN))) {
398 rc = i;
399 break;
400 }
401 }
402
403 return rc;
404}