blob: 5079e49442057cfa95e296977febee823a5173f1 [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 Kondratiev9a177382014-02-27 16:20:45 +0200110static int wil_find_free_vring(struct wil6210_priv *wil)
111{
112 int i;
113 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
114 if (!wil->vring_tx[i].va)
115 return i;
116 }
117 return -EINVAL;
118}
119
Vladimir Kondratievd81079f2013-03-13 14:12:43 +0200120static void wil_connect_worker(struct work_struct *work)
121{
122 int rc;
123 struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
124 connect_worker);
125 int cid = wil->pending_connect_cid;
Vladimir Kondratiev9a177382014-02-27 16:20:45 +0200126 int ringid = wil_find_free_vring(wil);
Vladimir Kondratievd81079f2013-03-13 14:12:43 +0200127
128 if (cid < 0) {
129 wil_err(wil, "No connection pending\n");
130 return;
131 }
132
133 wil_dbg_wmi(wil, "Configure for connection CID %d\n", cid);
134
Vladimir Kondratiev9a177382014-02-27 16:20:45 +0200135 rc = wil_vring_init_tx(wil, ringid, WIL6210_TX_RING_SIZE, cid, 0);
Vladimir Kondratievd81079f2013-03-13 14:12:43 +0200136 wil->pending_connect_cid = -1;
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200137 if (rc == 0) {
138 wil->sta[cid].status = wil_sta_connected;
Vladimir Kondratievd81079f2013-03-13 14:12:43 +0200139 wil_link_on(wil);
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200140 } else {
141 wil->sta[cid].status = wil_sta_unused;
142 }
Vladimir Kondratievd81079f2013-03-13 14:12:43 +0200143}
144
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800145int wil_priv_init(struct wil6210_priv *wil)
146{
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200147 wil_dbg_misc(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800148
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200149 memset(wil->sta, 0, sizeof(wil->sta));
150
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800151 mutex_init(&wil->mutex);
152 mutex_init(&wil->wmi_mutex);
153
154 init_completion(&wil->wmi_ready);
155
156 wil->pending_connect_cid = -1;
157 setup_timer(&wil->connect_timer, wil_connect_timer_fn, (ulong)wil);
158
Vladimir Kondratievd81079f2013-03-13 14:12:43 +0200159 INIT_WORK(&wil->connect_worker, wil_connect_worker);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800160 INIT_WORK(&wil->disconnect_worker, wil_disconnect_worker);
161 INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
162
163 INIT_LIST_HEAD(&wil->pending_wmi_ev);
164 spin_lock_init(&wil->wmi_ev_lock);
165
166 wil->wmi_wq = create_singlethread_workqueue(WIL_NAME"_wmi");
167 if (!wil->wmi_wq)
168 return -EAGAIN;
169
170 wil->wmi_wq_conn = create_singlethread_workqueue(WIL_NAME"_connect");
171 if (!wil->wmi_wq_conn) {
172 destroy_workqueue(wil->wmi_wq);
173 return -EAGAIN;
174 }
175
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800176 return 0;
177}
178
179void wil6210_disconnect(struct wil6210_priv *wil, void *bssid)
180{
181 del_timer_sync(&wil->connect_timer);
182 _wil6210_disconnect(wil, bssid);
183}
184
185void wil_priv_deinit(struct wil6210_priv *wil)
186{
187 cancel_work_sync(&wil->disconnect_worker);
188 wil6210_disconnect(wil, NULL);
189 wmi_event_flush(wil);
190 destroy_workqueue(wil->wmi_wq_conn);
191 destroy_workqueue(wil->wmi_wq);
192}
193
194static void wil_target_reset(struct wil6210_priv *wil)
195{
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200196 wil_dbg_misc(wil, "Resetting...\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800197
198 /* register write */
199#define W(a, v) iowrite32(v, wil->csr + HOSTADDR(a))
200 /* register set = read, OR, write */
201#define S(a, v) iowrite32(ioread32(wil->csr + HOSTADDR(a)) | v, \
202 wil->csr + HOSTADDR(a))
203
204 /* hpal_perst_from_pad_src_n_mask */
205 S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(6));
206 /* car_perst_rst_src_n_mask */
207 S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(7));
208
209 W(RGF_USER_MAC_CPU_0, BIT(1)); /* mac_cpu_man_rst */
210 W(RGF_USER_USER_CPU_0, BIT(1)); /* user_cpu_man_rst */
211
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800212 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
213 W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
214 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000170);
215 W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FC00);
216
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800217 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
218 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
219 W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
220 W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
221
222 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000001);
223 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00000080);
224 W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
225
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200226 wil_dbg_misc(wil, "Reset completed\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800227
228#undef W
229#undef S
230}
231
232void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
233{
234 le32_to_cpus(&r->base);
235 le16_to_cpus(&r->entry_size);
236 le16_to_cpus(&r->size);
237 le32_to_cpus(&r->tail);
238 le32_to_cpus(&r->head);
239}
240
241static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
242{
243 ulong to = msecs_to_jiffies(1000);
244 ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
245 if (0 == left) {
246 wil_err(wil, "Firmware not ready\n");
247 return -ETIME;
248 } else {
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200249 wil_dbg_misc(wil, "FW ready after %d ms\n",
Vladimir Kondratiev2057ebb2013-01-28 18:30:58 +0200250 jiffies_to_msecs(to-left));
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800251 }
252 return 0;
253}
254
255/*
256 * We reset all the structures, and we reset the UMAC.
257 * After calling this routine, you're expected to reload
258 * the firmware.
259 */
260int wil_reset(struct wil6210_priv *wil)
261{
262 int rc;
263
264 cancel_work_sync(&wil->disconnect_worker);
265 wil6210_disconnect(wil, NULL);
266
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800267 wil6210_disable_irq(wil);
268 wil->status = 0;
269
Vladimir Kondratieve08b5902013-01-28 18:31:05 +0200270 wmi_event_flush(wil);
271
272 flush_workqueue(wil->wmi_wq_conn);
273 flush_workqueue(wil->wmi_wq);
274
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800275 /* TODO: put MAC in reset */
276 wil_target_reset(wil);
277
278 /* init after reset */
279 wil->pending_connect_cid = -1;
Wolfram Sang16735d02013-11-14 14:32:02 -0800280 reinit_completion(&wil->wmi_ready);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800281
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800282 /* TODO: release MAC reset */
283 wil6210_enable_irq(wil);
284
285 /* we just started MAC, wait for FW ready */
286 rc = wil_wait_for_fw_ready(wil);
287
288 return rc;
289}
290
291
292void wil_link_on(struct wil6210_priv *wil)
293{
294 struct net_device *ndev = wil_to_ndev(wil);
295
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200296 wil_dbg_misc(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800297
298 netif_carrier_on(ndev);
299 netif_tx_wake_all_queues(ndev);
300}
301
302void wil_link_off(struct wil6210_priv *wil)
303{
304 struct net_device *ndev = wil_to_ndev(wil);
305
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200306 wil_dbg_misc(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800307
308 netif_tx_stop_all_queues(ndev);
309 netif_carrier_off(ndev);
310}
311
312static int __wil_up(struct wil6210_priv *wil)
313{
314 struct net_device *ndev = wil_to_ndev(wil);
315 struct wireless_dev *wdev = wil->wdev;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800316 int rc;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800317
318 rc = wil_reset(wil);
319 if (rc)
320 return rc;
321
Vladimir Kondratieve31b2562013-06-09 09:12:55 +0300322 /* Rx VRING. After MAC and beacon */
323 rc = wil_rx_init(wil);
324 if (rc)
325 return rc;
326
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800327 switch (wdev->iftype) {
328 case NL80211_IFTYPE_STATION:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200329 wil_dbg_misc(wil, "type: STATION\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800330 ndev->type = ARPHRD_ETHER;
331 break;
332 case NL80211_IFTYPE_AP:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200333 wil_dbg_misc(wil, "type: AP\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800334 ndev->type = ARPHRD_ETHER;
335 break;
336 case NL80211_IFTYPE_P2P_CLIENT:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200337 wil_dbg_misc(wil, "type: P2P_CLIENT\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800338 ndev->type = ARPHRD_ETHER;
339 break;
340 case NL80211_IFTYPE_P2P_GO:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200341 wil_dbg_misc(wil, "type: P2P_GO\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800342 ndev->type = ARPHRD_ETHER;
343 break;
344 case NL80211_IFTYPE_MONITOR:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200345 wil_dbg_misc(wil, "type: Monitor\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800346 ndev->type = ARPHRD_IEEE80211_RADIOTAP;
347 /* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
348 break;
349 default:
350 return -EOPNOTSUPP;
351 }
352
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800353 /* MAC address - pre-requisite for other commands */
354 wmi_set_mac_address(wil, ndev->dev_addr);
355
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800356
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300357 napi_enable(&wil->napi_rx);
358 napi_enable(&wil->napi_tx);
359
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800360 return 0;
361}
362
363int wil_up(struct wil6210_priv *wil)
364{
365 int rc;
366
367 mutex_lock(&wil->mutex);
368 rc = __wil_up(wil);
369 mutex_unlock(&wil->mutex);
370
371 return rc;
372}
373
374static int __wil_down(struct wil6210_priv *wil)
375{
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300376 napi_disable(&wil->napi_rx);
377 napi_disable(&wil->napi_tx);
378
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800379 if (wil->scan_request) {
380 cfg80211_scan_done(wil->scan_request, true);
381 wil->scan_request = NULL;
382 }
383
384 wil6210_disconnect(wil, NULL);
385 wil_rx_fini(wil);
386
387 return 0;
388}
389
390int wil_down(struct wil6210_priv *wil)
391{
392 int rc;
393
394 mutex_lock(&wil->mutex);
395 rc = __wil_down(wil);
396 mutex_unlock(&wil->mutex);
397
398 return rc;
399}
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200400
401int wil_find_cid(struct wil6210_priv *wil, const u8 *mac)
402{
403 int i;
404 int rc = -ENOENT;
405
406 for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
407 if ((wil->sta[i].status != wil_sta_unused) &&
408 (0 == memcmp(wil->sta[i].addr, mac, ETH_ALEN))) {
409 rc = i;
410 break;
411 }
412 }
413
414 return rc;
415}