blob: dc220537de99220d1756da72e4500909805b1679 [file] [log] [blame]
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08001/*
Vladimir Kondratiev02525a72014-08-06 10:31:51 +03002 * Copyright (c) 2012-2014 Qualcomm Atheros, Inc.
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -08003 *
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
Vladimir Kondratieved6f9dc2014-03-17 15:34:19 +020024static bool no_fw_recovery;
25module_param(no_fw_recovery, bool, S_IRUGO | S_IWUSR);
26MODULE_PARM_DESC(no_fw_recovery, " disable FW error recovery");
27
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080028/*
29 * Due to a hardware issue,
30 * one has to read/write to/from NIC in 32-bit chunks;
31 * regular memcpy_fromio and siblings will
32 * not work on 64-bit platform - it uses 64-bit transactions
33 *
34 * Force 32-bit transactions to enable NIC on 64-bit platforms
35 *
36 * To avoid byte swap on big endian host, __raw_{read|write}l
37 * should be used - {read|write}l would swap bytes to provide
38 * little endian on PCI value in host endianness.
39 */
40void wil_memcpy_fromio_32(void *dst, const volatile void __iomem *src,
41 size_t count)
42{
43 u32 *d = dst;
44 const volatile u32 __iomem *s = src;
45
46 /* size_t is unsigned, if (count%4 != 0) it will wrap */
47 for (count += 4; count > 4; count -= 4)
48 *d++ = __raw_readl(s++);
49}
50
51void wil_memcpy_toio_32(volatile void __iomem *dst, const void *src,
52 size_t count)
53{
54 volatile u32 __iomem *d = dst;
55 const u32 *s = src;
56
57 for (count += 4; count > 4; count -= 4)
58 __raw_writel(*s++, d++);
59}
60
Vladimir Kondratiev91886b02014-02-27 16:20:50 +020061static void wil_disconnect_cid(struct wil6210_priv *wil, int cid)
62{
63 uint i;
Vladimir Kondratievfc58f682014-06-16 19:37:16 +030064 struct net_device *ndev = wil_to_ndev(wil);
65 struct wireless_dev *wdev = wil->wdev;
Vladimir Kondratiev91886b02014-02-27 16:20:50 +020066 struct wil_sta_info *sta = &wil->sta[cid];
Vladimir Kondratievfc58f682014-06-16 19:37:16 +030067 wil_dbg_misc(wil, "%s(CID %d, status %d)\n", __func__, cid,
68 sta->status);
Vladimir Kondratiev4d55a0a2014-02-27 16:20:54 +020069
Vladimir Kondratieve58c9f72014-03-17 15:34:06 +020070 sta->data_port_open = false;
Vladimir Kondratiev4d55a0a2014-02-27 16:20:54 +020071 if (sta->status != wil_sta_unused) {
72 wmi_disconnect_sta(wil, sta->addr, WLAN_REASON_DEAUTH_LEAVING);
Vladimir Kondratievfc58f682014-06-16 19:37:16 +030073 switch (wdev->iftype) {
74 case NL80211_IFTYPE_AP:
75 case NL80211_IFTYPE_P2P_GO:
76 /* AP-like interface */
77 cfg80211_del_sta(ndev, sta->addr, GFP_KERNEL);
78 break;
79 default:
80 break;
81 }
Vladimir Kondratiev4d55a0a2014-02-27 16:20:54 +020082 sta->status = wil_sta_unused;
83 }
84
Vladimir Kondratiev91886b02014-02-27 16:20:50 +020085 for (i = 0; i < WIL_STA_TID_NUM; i++) {
86 struct wil_tid_ampdu_rx *r = sta->tid_rx[i];
87 sta->tid_rx[i] = NULL;
88 wil_tid_ampdu_rx_free(wil, r);
89 }
90 for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++) {
91 if (wil->vring2cid_tid[i][0] == cid)
92 wil_vring_fini_tx(wil, i);
93 }
94 memset(&sta->stats, 0, sizeof(sta->stats));
95}
96
Johannes Berg3b3a0162014-05-19 17:19:31 +020097static void _wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -080098{
Vladimir Kondratiev91886b02014-02-27 16:20:50 +020099 int cid = -ENOENT;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800100 struct net_device *ndev = wil_to_ndev(wil);
Vladimir Kondratiev91886b02014-02-27 16:20:50 +0200101 struct wireless_dev *wdev = wil->wdev;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800102
Vladimir Kondratiev91886b02014-02-27 16:20:50 +0200103 might_sleep();
104 if (bssid) {
105 cid = wil_find_cid(wil, bssid);
106 wil_dbg_misc(wil, "%s(%pM, CID %d)\n", __func__, bssid, cid);
107 } else {
108 wil_dbg_misc(wil, "%s(all)\n", __func__);
109 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800110
Vladimir Kondratiev91886b02014-02-27 16:20:50 +0200111 if (cid >= 0) /* disconnect 1 peer */
112 wil_disconnect_cid(wil, cid);
113 else /* disconnect all */
114 for (cid = 0; cid < WIL6210_MAX_CID; cid++)
115 wil_disconnect_cid(wil, cid);
116
117 /* link state */
118 switch (wdev->iftype) {
119 case NL80211_IFTYPE_STATION:
120 case NL80211_IFTYPE_P2P_CLIENT:
121 wil_link_off(wil);
122 if (test_bit(wil_status_fwconnected, &wil->status)) {
123 clear_bit(wil_status_fwconnected, &wil->status);
124 cfg80211_disconnected(ndev,
125 WLAN_STATUS_UNSPECIFIED_FAILURE,
126 NULL, 0, GFP_KERNEL);
127 } else if (test_bit(wil_status_fwconnecting, &wil->status)) {
128 cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
129 WLAN_STATUS_UNSPECIFIED_FAILURE,
130 GFP_KERNEL);
Vladimir Kondratievb4490f42014-02-27 16:20:44 +0200131 }
Vladimir Kondratiev91886b02014-02-27 16:20:50 +0200132 clear_bit(wil_status_fwconnecting, &wil->status);
Vladimir Kondratiev91886b02014-02-27 16:20:50 +0200133 break;
134 default:
Vladimir Kondratiev91886b02014-02-27 16:20:50 +0200135 break;
Vladimir Kondratievb4490f42014-02-27 16:20:44 +0200136 }
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800137}
138
139static void wil_disconnect_worker(struct work_struct *work)
140{
141 struct wil6210_priv *wil = container_of(work,
142 struct wil6210_priv, disconnect_worker);
143
Vladimir Kondratiev097638a2014-03-17 15:34:25 +0200144 mutex_lock(&wil->mutex);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800145 _wil6210_disconnect(wil, NULL);
Vladimir Kondratiev097638a2014-03-17 15:34:25 +0200146 mutex_unlock(&wil->mutex);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800147}
148
149static void wil_connect_timer_fn(ulong x)
150{
151 struct wil6210_priv *wil = (void *)x;
152
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200153 wil_dbg_misc(wil, "Connect timeout\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800154
155 /* reschedule to thread context - disconnect won't
156 * run from atomic context
157 */
158 schedule_work(&wil->disconnect_worker);
159}
160
Vladimir Kondratiev047e5d72014-05-27 14:45:48 +0300161static void wil_scan_timer_fn(ulong x)
162{
163 struct wil6210_priv *wil = (void *)x;
164
165 clear_bit(wil_status_fwready, &wil->status);
166 wil_err(wil, "Scan timeout detected, start fw error recovery\n");
167 schedule_work(&wil->fw_error_worker);
168}
169
Vladimir Kondratieved6f9dc2014-03-17 15:34:19 +0200170static void wil_fw_error_worker(struct work_struct *work)
171{
172 struct wil6210_priv *wil = container_of(work,
173 struct wil6210_priv, fw_error_worker);
174 struct wireless_dev *wdev = wil->wdev;
175
176 wil_dbg_misc(wil, "fw error worker\n");
177
178 if (no_fw_recovery)
179 return;
180
Vladimir Kondratievfc219ee2014-05-27 14:45:45 +0300181 /* increment @recovery_count if less then WIL6210_FW_RECOVERY_TO
182 * passed since last recovery attempt
183 */
184 if (time_is_after_jiffies(wil->last_fw_recovery +
185 WIL6210_FW_RECOVERY_TO))
186 wil->recovery_count++;
187 else
188 wil->recovery_count = 1; /* fw was alive for a long time */
189
190 if (wil->recovery_count > WIL6210_FW_RECOVERY_RETRIES) {
191 wil_err(wil, "too many recovery attempts (%d), giving up\n",
192 wil->recovery_count);
193 return;
194 }
195
196 wil->last_fw_recovery = jiffies;
197
Vladimir Kondratiev9c3bde52014-03-17 15:34:21 +0200198 mutex_lock(&wil->mutex);
Vladimir Kondratieved6f9dc2014-03-17 15:34:19 +0200199 switch (wdev->iftype) {
200 case NL80211_IFTYPE_STATION:
201 case NL80211_IFTYPE_P2P_CLIENT:
202 case NL80211_IFTYPE_MONITOR:
Vladimir Kondratievfc219ee2014-05-27 14:45:45 +0300203 wil_info(wil, "fw error recovery started (try %d)...\n",
204 wil->recovery_count);
Vladimir Kondratieved6f9dc2014-03-17 15:34:19 +0200205 wil_reset(wil);
206
207 /* need to re-allocate Rx ring after reset */
208 wil_rx_init(wil);
209 break;
210 case NL80211_IFTYPE_AP:
211 case NL80211_IFTYPE_P2P_GO:
212 /* recovery in these modes is done by upper layers */
213 break;
214 default:
215 break;
216 }
Vladimir Kondratiev9c3bde52014-03-17 15:34:21 +0200217 mutex_unlock(&wil->mutex);
Vladimir Kondratieved6f9dc2014-03-17 15:34:19 +0200218}
219
Vladimir Kondratiev9a177382014-02-27 16:20:45 +0200220static int wil_find_free_vring(struct wil6210_priv *wil)
221{
222 int i;
223 for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
224 if (!wil->vring_tx[i].va)
225 return i;
226 }
227 return -EINVAL;
228}
229
Vladimir Kondratievd81079f2013-03-13 14:12:43 +0200230static void wil_connect_worker(struct work_struct *work)
231{
232 int rc;
233 struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
234 connect_worker);
235 int cid = wil->pending_connect_cid;
Vladimir Kondratiev9a177382014-02-27 16:20:45 +0200236 int ringid = wil_find_free_vring(wil);
Vladimir Kondratievd81079f2013-03-13 14:12:43 +0200237
238 if (cid < 0) {
239 wil_err(wil, "No connection pending\n");
240 return;
241 }
242
243 wil_dbg_wmi(wil, "Configure for connection CID %d\n", cid);
244
Vladimir Kondratiev9a177382014-02-27 16:20:45 +0200245 rc = wil_vring_init_tx(wil, ringid, WIL6210_TX_RING_SIZE, cid, 0);
Vladimir Kondratievd81079f2013-03-13 14:12:43 +0200246 wil->pending_connect_cid = -1;
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200247 if (rc == 0) {
248 wil->sta[cid].status = wil_sta_connected;
Vladimir Kondratievd81079f2013-03-13 14:12:43 +0200249 wil_link_on(wil);
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200250 } else {
251 wil->sta[cid].status = wil_sta_unused;
252 }
Vladimir Kondratievd81079f2013-03-13 14:12:43 +0200253}
254
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800255int wil_priv_init(struct wil6210_priv *wil)
256{
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200257 wil_dbg_misc(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800258
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200259 memset(wil->sta, 0, sizeof(wil->sta));
260
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800261 mutex_init(&wil->mutex);
262 mutex_init(&wil->wmi_mutex);
263
264 init_completion(&wil->wmi_ready);
265
266 wil->pending_connect_cid = -1;
267 setup_timer(&wil->connect_timer, wil_connect_timer_fn, (ulong)wil);
Vladimir Kondratiev047e5d72014-05-27 14:45:48 +0300268 setup_timer(&wil->scan_timer, wil_scan_timer_fn, (ulong)wil);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800269
Vladimir Kondratievd81079f2013-03-13 14:12:43 +0200270 INIT_WORK(&wil->connect_worker, wil_connect_worker);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800271 INIT_WORK(&wil->disconnect_worker, wil_disconnect_worker);
272 INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
Vladimir Kondratieved6f9dc2014-03-17 15:34:19 +0200273 INIT_WORK(&wil->fw_error_worker, wil_fw_error_worker);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800274
275 INIT_LIST_HEAD(&wil->pending_wmi_ev);
276 spin_lock_init(&wil->wmi_ev_lock);
277
278 wil->wmi_wq = create_singlethread_workqueue(WIL_NAME"_wmi");
279 if (!wil->wmi_wq)
280 return -EAGAIN;
281
282 wil->wmi_wq_conn = create_singlethread_workqueue(WIL_NAME"_connect");
283 if (!wil->wmi_wq_conn) {
284 destroy_workqueue(wil->wmi_wq);
285 return -EAGAIN;
286 }
287
Vladimir Kondratievfc219ee2014-05-27 14:45:45 +0300288 wil->last_fw_recovery = jiffies;
289
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800290 return 0;
291}
292
Johannes Berg3b3a0162014-05-19 17:19:31 +0200293void wil6210_disconnect(struct wil6210_priv *wil, const u8 *bssid)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800294{
295 del_timer_sync(&wil->connect_timer);
296 _wil6210_disconnect(wil, bssid);
297}
298
299void wil_priv_deinit(struct wil6210_priv *wil)
300{
Vladimir Kondratiev047e5d72014-05-27 14:45:48 +0300301 del_timer_sync(&wil->scan_timer);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800302 cancel_work_sync(&wil->disconnect_worker);
Vladimir Kondratieved6f9dc2014-03-17 15:34:19 +0200303 cancel_work_sync(&wil->fw_error_worker);
Vladimir Kondratiev097638a2014-03-17 15:34:25 +0200304 mutex_lock(&wil->mutex);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800305 wil6210_disconnect(wil, NULL);
Vladimir Kondratiev097638a2014-03-17 15:34:25 +0200306 mutex_unlock(&wil->mutex);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800307 wmi_event_flush(wil);
308 destroy_workqueue(wil->wmi_wq_conn);
309 destroy_workqueue(wil->wmi_wq);
310}
311
312static void wil_target_reset(struct wil6210_priv *wil)
313{
Vladimir Kondratiev98a65b52014-03-17 15:34:12 +0200314 int delay = 0;
Vladimir Kondratievd28bcc32014-03-17 15:34:23 +0200315 u32 hw_state;
Vladimir Kondratiev36b10a72014-03-17 15:34:10 +0200316 u32 rev_id;
Vladimir Kondratiev65082812014-07-14 09:49:37 +0300317 bool is_sparrow = (wil->board->board == WIL_BOARD_SPARROW);
Vladimir Kondratiev36b10a72014-03-17 15:34:10 +0200318
Vladimir Kondratiev65082812014-07-14 09:49:37 +0300319 wil_dbg_misc(wil, "Resetting \"%s\"...\n", wil->board->name);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800320
Vladimir Kondratiev36b10a72014-03-17 15:34:10 +0200321 /* register read */
322#define R(a) ioread32(wil->csr + HOSTADDR(a))
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800323 /* register write */
324#define W(a, v) iowrite32(v, wil->csr + HOSTADDR(a))
325 /* register set = read, OR, write */
Vladimir Kondratiev972072a2014-03-17 15:34:15 +0200326#define S(a, v) W(a, R(a) | v)
327 /* register clear = read, AND with inverted, write */
328#define C(a, v) W(a, R(a) & ~v)
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800329
Vladimir Kondratievcc9e4a22014-08-06 10:31:50 +0300330 wmb(); /* If host reorder writes here -> race in NIC */
331 W(RGF_USER_MAC_CPU_0, BIT(1)); /* mac_cpu_man_rst */
Vladimir Kondratiev17123992014-03-17 15:34:14 +0200332 wil->hw_version = R(RGF_USER_FW_REV_ID);
Vladimir Kondratiev36b10a72014-03-17 15:34:10 +0200333 rev_id = wil->hw_version & 0xff;
Vladimir Kondratiev65082812014-07-14 09:49:37 +0300334
335 /* Clear MAC link up */
336 S(RGF_HP_CTRL, BIT(15));
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800337 /* hpal_perst_from_pad_src_n_mask */
338 S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(6));
339 /* car_perst_rst_src_n_mask */
340 S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(7));
Vladimir Kondratiev260e6952014-03-17 15:34:24 +0200341 wmb(); /* order is important here */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800342
Vladimir Kondratiev65082812014-07-14 09:49:37 +0300343 if (is_sparrow) {
344 W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x3ff81f);
345 wmb(); /* order is important here */
346 }
347
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800348 W(RGF_USER_USER_CPU_0, BIT(1)); /* user_cpu_man_rst */
Vladimir Kondratievcc9e4a22014-08-06 10:31:50 +0300349 wmb(); /* If host reorder writes here -> race in NIC */
350 W(RGF_USER_MAC_CPU_0, BIT(1)); /* mac_cpu_man_rst */
Vladimir Kondratiev260e6952014-03-17 15:34:24 +0200351 wmb(); /* order is important here */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800352
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800353 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
354 W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
Vladimir Kondratiev65082812014-07-14 09:49:37 +0300355 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, is_sparrow ? 0x000000B0 : 0x00000170);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800356 W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FC00);
Vladimir Kondratiev260e6952014-03-17 15:34:24 +0200357 wmb(); /* order is important here */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800358
Vladimir Kondratiev65082812014-07-14 09:49:37 +0300359 if (is_sparrow) {
360 W(RGF_USER_CLKS_CTL_EXT_SW_RST_VEC_0, 0x0);
361 wmb(); /* order is important here */
362 }
363
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800364 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
365 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
366 W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
367 W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
Vladimir Kondratiev260e6952014-03-17 15:34:24 +0200368 wmb(); /* order is important here */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800369
Vladimir Kondratiev65082812014-07-14 09:49:37 +0300370 if (is_sparrow) {
371 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000003);
372 /* reset A2 PCIE AHB */
Vladimir Kondratiev36b10a72014-03-17 15:34:10 +0200373 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000);
Vladimir Kondratiev65082812014-07-14 09:49:37 +0300374
375 } else {
376 W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000001);
377 if (rev_id == 1) {
378 /* reset A1 BOTH PCIE AHB & PCIE RGF */
379 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00000080);
380 } else {
381 W(RGF_PCIE_LOS_COUNTER_CTL, BIT(6) | BIT(8));
382 W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00008000);
383 }
384
Vladimir Kondratiev36b10a72014-03-17 15:34:10 +0200385 }
Vladimir Kondratiev65082812014-07-14 09:49:37 +0300386
387 /* TODO: check order here!!! Erez code is different */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800388 W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
Vladimir Kondratiev260e6952014-03-17 15:34:24 +0200389 wmb(); /* order is important here */
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800390
Vladimir Kondratievd28bcc32014-03-17 15:34:23 +0200391 /* wait until device ready */
Vladimir Kondratiev36b10a72014-03-17 15:34:10 +0200392 do {
393 msleep(1);
Vladimir Kondratievd28bcc32014-03-17 15:34:23 +0200394 hw_state = R(RGF_USER_HW_MACHINE_STATE);
Vladimir Kondratiev98a65b52014-03-17 15:34:12 +0200395 if (delay++ > 100) {
Vladimir Kondratievd28bcc32014-03-17 15:34:23 +0200396 wil_err(wil, "Reset not completed, hw_state 0x%08x\n",
397 hw_state);
Vladimir Kondratiev36b10a72014-03-17 15:34:10 +0200398 return;
399 }
Vladimir Kondratievd28bcc32014-03-17 15:34:23 +0200400 } while (hw_state != HW_MACHINE_BOOT_DONE);
Vladimir Kondratiev36b10a72014-03-17 15:34:10 +0200401
Vladimir Kondratiev65082812014-07-14 09:49:37 +0300402 /* TODO: Erez check rev_id != 1 */
403 if (!is_sparrow && (rev_id != 1))
Vladimir Kondratiev17123992014-03-17 15:34:14 +0200404 W(RGF_PCIE_LOS_COUNTER_CTL, BIT(8));
Vladimir Kondratiev36b10a72014-03-17 15:34:10 +0200405
Vladimir Kondratiev972072a2014-03-17 15:34:15 +0200406 C(RGF_USER_CLKS_CTL_0, BIT_USER_CLKS_RST_PWGD);
Vladimir Kondratiev260e6952014-03-17 15:34:24 +0200407 wmb(); /* order is important here */
Vladimir Kondratiev972072a2014-03-17 15:34:15 +0200408
Vladimir Kondratiev98a65b52014-03-17 15:34:12 +0200409 wil_dbg_misc(wil, "Reset completed in %d ms\n", delay);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800410
Vladimir Kondratiev36b10a72014-03-17 15:34:10 +0200411#undef R
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800412#undef W
413#undef S
Vladimir Kondratiev972072a2014-03-17 15:34:15 +0200414#undef C
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800415}
416
417void wil_mbox_ring_le2cpus(struct wil6210_mbox_ring *r)
418{
419 le32_to_cpus(&r->base);
420 le16_to_cpus(&r->entry_size);
421 le16_to_cpus(&r->size);
422 le32_to_cpus(&r->tail);
423 le32_to_cpus(&r->head);
424}
425
426static int wil_wait_for_fw_ready(struct wil6210_priv *wil)
427{
428 ulong to = msecs_to_jiffies(1000);
429 ulong left = wait_for_completion_timeout(&wil->wmi_ready, to);
430 if (0 == left) {
431 wil_err(wil, "Firmware not ready\n");
432 return -ETIME;
433 } else {
Vladimir Kondratiev15e23122014-04-08 11:36:16 +0300434 wil_info(wil, "FW ready after %d ms. HW version 0x%08x\n",
435 jiffies_to_msecs(to-left), wil->hw_version);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800436 }
437 return 0;
438}
439
440/*
441 * We reset all the structures, and we reset the UMAC.
442 * After calling this routine, you're expected to reload
443 * the firmware.
444 */
445int wil_reset(struct wil6210_priv *wil)
446{
447 int rc;
448
Vladimir Kondratiev097638a2014-03-17 15:34:25 +0200449 WARN_ON(!mutex_is_locked(&wil->mutex));
450
451 cancel_work_sync(&wil->disconnect_worker);
452 wil6210_disconnect(wil, NULL);
453
Vladimir Kondratiev0fef1812014-03-17 15:34:18 +0200454 wil->status = 0; /* prevent NAPI from being scheduled */
455 if (test_bit(wil_status_napi_en, &wil->status)) {
456 napi_synchronize(&wil->napi_rx);
Vladimir Kondratiev0fef1812014-03-17 15:34:18 +0200457 }
458
Vladimir Kondratieved6f9dc2014-03-17 15:34:19 +0200459 if (wil->scan_request) {
460 wil_dbg_misc(wil, "Abort scan_request 0x%p\n",
461 wil->scan_request);
Vladimir Kondratiev047e5d72014-05-27 14:45:48 +0300462 del_timer_sync(&wil->scan_timer);
Vladimir Kondratieved6f9dc2014-03-17 15:34:19 +0200463 cfg80211_scan_done(wil->scan_request, true);
464 wil->scan_request = NULL;
465 }
466
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800467 wil6210_disable_irq(wil);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800468
Vladimir Kondratieve08b5902013-01-28 18:31:05 +0200469 wmi_event_flush(wil);
470
471 flush_workqueue(wil->wmi_wq_conn);
472 flush_workqueue(wil->wmi_wq);
473
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800474 /* TODO: put MAC in reset */
475 wil_target_reset(wil);
476
Vladimir Kondratiev8bf6adb2014-03-17 15:34:17 +0200477 wil_rx_fini(wil);
478
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800479 /* init after reset */
480 wil->pending_connect_cid = -1;
Wolfram Sang16735d02013-11-14 14:32:02 -0800481 reinit_completion(&wil->wmi_ready);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800482
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800483 /* TODO: release MAC reset */
484 wil6210_enable_irq(wil);
485
486 /* we just started MAC, wait for FW ready */
487 rc = wil_wait_for_fw_ready(wil);
488
489 return rc;
490}
491
Vladimir Kondratieved6f9dc2014-03-17 15:34:19 +0200492void wil_fw_error_recovery(struct wil6210_priv *wil)
493{
494 wil_dbg_misc(wil, "starting fw error recovery\n");
495 schedule_work(&wil->fw_error_worker);
496}
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800497
498void wil_link_on(struct wil6210_priv *wil)
499{
500 struct net_device *ndev = wil_to_ndev(wil);
501
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200502 wil_dbg_misc(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800503
504 netif_carrier_on(ndev);
Vladimir Kondratiev55f8f682014-06-16 19:37:23 +0300505 wil_dbg_misc(wil, "netif_tx_wake : link on\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800506 netif_tx_wake_all_queues(ndev);
507}
508
509void wil_link_off(struct wil6210_priv *wil)
510{
511 struct net_device *ndev = wil_to_ndev(wil);
512
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200513 wil_dbg_misc(wil, "%s()\n", __func__);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800514
515 netif_tx_stop_all_queues(ndev);
Vladimir Kondratiev55f8f682014-06-16 19:37:23 +0300516 wil_dbg_misc(wil, "netif_tx_stop : link off\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800517 netif_carrier_off(ndev);
518}
519
520static int __wil_up(struct wil6210_priv *wil)
521{
522 struct net_device *ndev = wil_to_ndev(wil);
523 struct wireless_dev *wdev = wil->wdev;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800524 int rc;
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800525
Vladimir Kondratiev097638a2014-03-17 15:34:25 +0200526 WARN_ON(!mutex_is_locked(&wil->mutex));
527
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800528 rc = wil_reset(wil);
529 if (rc)
530 return rc;
531
Vladimir Kondratieve31b2562013-06-09 09:12:55 +0300532 /* Rx VRING. After MAC and beacon */
533 rc = wil_rx_init(wil);
534 if (rc)
535 return rc;
536
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800537 switch (wdev->iftype) {
538 case NL80211_IFTYPE_STATION:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200539 wil_dbg_misc(wil, "type: STATION\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800540 ndev->type = ARPHRD_ETHER;
541 break;
542 case NL80211_IFTYPE_AP:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200543 wil_dbg_misc(wil, "type: AP\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800544 ndev->type = ARPHRD_ETHER;
545 break;
546 case NL80211_IFTYPE_P2P_CLIENT:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200547 wil_dbg_misc(wil, "type: P2P_CLIENT\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800548 ndev->type = ARPHRD_ETHER;
549 break;
550 case NL80211_IFTYPE_P2P_GO:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200551 wil_dbg_misc(wil, "type: P2P_GO\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800552 ndev->type = ARPHRD_ETHER;
553 break;
554 case NL80211_IFTYPE_MONITOR:
Vladimir Kondratiev77438822013-01-28 18:31:06 +0200555 wil_dbg_misc(wil, "type: Monitor\n");
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800556 ndev->type = ARPHRD_IEEE80211_RADIOTAP;
557 /* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
558 break;
559 default:
560 return -EOPNOTSUPP;
561 }
562
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800563 /* MAC address - pre-requisite for other commands */
564 wmi_set_mac_address(wil, ndev->dev_addr);
565
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800566
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300567 napi_enable(&wil->napi_rx);
568 napi_enable(&wil->napi_tx);
Vladimir Kondratiev0fef1812014-03-17 15:34:18 +0200569 set_bit(wil_status_napi_en, &wil->status);
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300570
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800571 return 0;
572}
573
574int wil_up(struct wil6210_priv *wil)
575{
576 int rc;
577
578 mutex_lock(&wil->mutex);
579 rc = __wil_up(wil);
580 mutex_unlock(&wil->mutex);
581
582 return rc;
583}
584
585static int __wil_down(struct wil6210_priv *wil)
586{
Vladimir Kondratiev097638a2014-03-17 15:34:25 +0200587 WARN_ON(!mutex_is_locked(&wil->mutex));
588
Vladimir Kondratiev0fef1812014-03-17 15:34:18 +0200589 clear_bit(wil_status_napi_en, &wil->status);
Vladimir Kondratieve0287c42013-05-12 14:43:36 +0300590 napi_disable(&wil->napi_rx);
591 napi_disable(&wil->napi_tx);
592
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800593 if (wil->scan_request) {
Vladimir Kondratiev2a91d7d2014-06-16 19:37:11 +0300594 wil_dbg_misc(wil, "Abort scan_request 0x%p\n",
595 wil->scan_request);
Vladimir Kondratiev047e5d72014-05-27 14:45:48 +0300596 del_timer_sync(&wil->scan_timer);
Vladimir Kondratiev2be7d222012-12-20 13:13:19 -0800597 cfg80211_scan_done(wil->scan_request, true);
598 wil->scan_request = NULL;
599 }
600
601 wil6210_disconnect(wil, NULL);
602 wil_rx_fini(wil);
603
604 return 0;
605}
606
607int wil_down(struct wil6210_priv *wil)
608{
609 int rc;
610
611 mutex_lock(&wil->mutex);
612 rc = __wil_down(wil);
613 mutex_unlock(&wil->mutex);
614
615 return rc;
616}
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200617
618int wil_find_cid(struct wil6210_priv *wil, const u8 *mac)
619{
620 int i;
621 int rc = -ENOENT;
622
623 for (i = 0; i < ARRAY_SIZE(wil->sta); i++) {
624 if ((wil->sta[i].status != wil_sta_unused) &&
Vladimir Kondratiev108d1eb2014-02-27 16:20:53 +0200625 ether_addr_equal(wil->sta[i].addr, mac)) {
Vladimir Kondratiev3df2cd362014-02-27 16:20:43 +0200626 rc = i;
627 break;
628 }
629 }
630
631 return rc;
632}