wil6210: headers clean-up
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / ath / wil6210 / main.c
1 /*
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
17 #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  */
34 void 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
45 void 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
55 static void _wil6210_disconnect(struct wil6210_priv *wil, void *bssid)
56 {
57         uint i;
58         struct net_device *ndev = wil_to_ndev(wil);
59         struct wireless_dev *wdev = wil->wdev;
60
61         wil_dbg_misc(wil, "%s()\n", __func__);
62
63         wil_link_off(wil);
64         clear_bit(wil_status_fwconnected, &wil->status);
65
66         switch (wdev->sme_state) {
67         case CFG80211_SME_CONNECTED:
68                 cfg80211_disconnected(ndev, WLAN_STATUS_UNSPECIFIED_FAILURE,
69                                       NULL, 0, GFP_KERNEL);
70                 break;
71         case CFG80211_SME_CONNECTING:
72                 cfg80211_connect_result(ndev, bssid, NULL, 0, NULL, 0,
73                                         WLAN_STATUS_UNSPECIFIED_FAILURE,
74                                         GFP_KERNEL);
75                 break;
76         default:
77                 break;
78         }
79
80         for (i = 0; i < ARRAY_SIZE(wil->vring_tx); i++)
81                 wil_vring_fini_tx(wil, i);
82
83         clear_bit(wil_status_dontscan, &wil->status);
84 }
85
86 static void wil_disconnect_worker(struct work_struct *work)
87 {
88         struct wil6210_priv *wil = container_of(work,
89                         struct wil6210_priv, disconnect_worker);
90
91         _wil6210_disconnect(wil, NULL);
92 }
93
94 static void wil_connect_timer_fn(ulong x)
95 {
96         struct wil6210_priv *wil = (void *)x;
97
98         wil_dbg_misc(wil, "Connect timeout\n");
99
100         /* reschedule to thread context - disconnect won't
101          * run from atomic context
102          */
103         schedule_work(&wil->disconnect_worker);
104 }
105
106 static void wil_cache_mbox_regs(struct wil6210_priv *wil)
107 {
108         /* make shadow copy of registers that should not change on run time */
109         wil_memcpy_fromio_32(&wil->mbox_ctl, wil->csr + HOST_MBOX,
110                              sizeof(struct wil6210_mbox_ctl));
111         wil_mbox_ring_le2cpus(&wil->mbox_ctl.rx);
112         wil_mbox_ring_le2cpus(&wil->mbox_ctl.tx);
113 }
114
115 static void wil_connect_worker(struct work_struct *work)
116 {
117         int rc;
118         struct wil6210_priv *wil = container_of(work, struct wil6210_priv,
119                                                 connect_worker);
120         int cid = wil->pending_connect_cid;
121
122         if (cid < 0) {
123                 wil_err(wil, "No connection pending\n");
124                 return;
125         }
126
127         wil_dbg_wmi(wil, "Configure for connection CID %d\n", cid);
128
129         rc = wil_vring_init_tx(wil, 0, WIL6210_TX_RING_SIZE, cid, 0);
130         wil->pending_connect_cid = -1;
131         if (rc == 0)
132                 wil_link_on(wil);
133 }
134
135 int wil_priv_init(struct wil6210_priv *wil)
136 {
137         wil_dbg_misc(wil, "%s()\n", __func__);
138
139         mutex_init(&wil->mutex);
140         mutex_init(&wil->wmi_mutex);
141
142         init_completion(&wil->wmi_ready);
143
144         wil->pending_connect_cid = -1;
145         setup_timer(&wil->connect_timer, wil_connect_timer_fn, (ulong)wil);
146
147         INIT_WORK(&wil->connect_worker, wil_connect_worker);
148         INIT_WORK(&wil->disconnect_worker, wil_disconnect_worker);
149         INIT_WORK(&wil->wmi_event_worker, wmi_event_worker);
150
151         INIT_LIST_HEAD(&wil->pending_wmi_ev);
152         spin_lock_init(&wil->wmi_ev_lock);
153
154         wil->wmi_wq = create_singlethread_workqueue(WIL_NAME"_wmi");
155         if (!wil->wmi_wq)
156                 return -EAGAIN;
157
158         wil->wmi_wq_conn = create_singlethread_workqueue(WIL_NAME"_connect");
159         if (!wil->wmi_wq_conn) {
160                 destroy_workqueue(wil->wmi_wq);
161                 return -EAGAIN;
162         }
163
164         wil_cache_mbox_regs(wil);
165
166         return 0;
167 }
168
169 void wil6210_disconnect(struct wil6210_priv *wil, void *bssid)
170 {
171         del_timer_sync(&wil->connect_timer);
172         _wil6210_disconnect(wil, bssid);
173 }
174
175 void wil_priv_deinit(struct wil6210_priv *wil)
176 {
177         cancel_work_sync(&wil->disconnect_worker);
178         wil6210_disconnect(wil, NULL);
179         wmi_event_flush(wil);
180         destroy_workqueue(wil->wmi_wq_conn);
181         destroy_workqueue(wil->wmi_wq);
182 }
183
184 static void wil_target_reset(struct wil6210_priv *wil)
185 {
186         wil_dbg_misc(wil, "Resetting...\n");
187
188         /* register write */
189 #define W(a, v) iowrite32(v, wil->csr + HOSTADDR(a))
190         /* register set = read, OR, write */
191 #define S(a, v) iowrite32(ioread32(wil->csr + HOSTADDR(a)) | v, \
192                 wil->csr + HOSTADDR(a))
193
194         /* hpal_perst_from_pad_src_n_mask */
195         S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(6));
196         /* car_perst_rst_src_n_mask */
197         S(RGF_USER_CLKS_CTL_SW_RST_MASK_0, BIT(7));
198
199         W(RGF_USER_MAC_CPU_0,  BIT(1)); /* mac_cpu_man_rst */
200         W(RGF_USER_USER_CPU_0, BIT(1)); /* user_cpu_man_rst */
201
202         msleep(100);
203
204         W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0xFE000000);
205         W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0x0000003F);
206         W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000170);
207         W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0xFFE7FC00);
208
209         msleep(100);
210
211         W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0);
212         W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0);
213         W(RGF_USER_CLKS_CTL_SW_RST_VEC_1, 0);
214         W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
215
216         W(RGF_USER_CLKS_CTL_SW_RST_VEC_3, 0x00000001);
217         W(RGF_USER_CLKS_CTL_SW_RST_VEC_2, 0x00000080);
218         W(RGF_USER_CLKS_CTL_SW_RST_VEC_0, 0);
219
220         msleep(2000);
221
222         W(RGF_USER_USER_CPU_0, BIT(0)); /* user_cpu_man_de_rst */
223
224         msleep(2000);
225
226         wil_dbg_misc(wil, "Reset completed\n");
227
228 #undef W
229 #undef S
230 }
231
232 void 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
241 static 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 {
249                 wil_dbg_misc(wil, "FW ready after %d ms\n",
250                              jiffies_to_msecs(to-left));
251         }
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  */
260 int wil_reset(struct wil6210_priv *wil)
261 {
262         int rc;
263
264         cancel_work_sync(&wil->disconnect_worker);
265         wil6210_disconnect(wil, NULL);
266
267         wil6210_disable_irq(wil);
268         wil->status = 0;
269
270         wmi_event_flush(wil);
271
272         flush_workqueue(wil->wmi_wq_conn);
273         flush_workqueue(wil->wmi_wq);
274
275         /* TODO: put MAC in reset */
276         wil_target_reset(wil);
277
278         /* init after reset */
279         wil->pending_connect_cid = -1;
280         INIT_COMPLETION(wil->wmi_ready);
281
282         wil_cache_mbox_regs(wil);
283
284         /* TODO: release MAC reset */
285         wil6210_enable_irq(wil);
286
287         /* we just started MAC, wait for FW ready */
288         rc = wil_wait_for_fw_ready(wil);
289
290         return rc;
291 }
292
293
294 void wil_link_on(struct wil6210_priv *wil)
295 {
296         struct net_device *ndev = wil_to_ndev(wil);
297
298         wil_dbg_misc(wil, "%s()\n", __func__);
299
300         netif_carrier_on(ndev);
301         netif_tx_wake_all_queues(ndev);
302 }
303
304 void wil_link_off(struct wil6210_priv *wil)
305 {
306         struct net_device *ndev = wil_to_ndev(wil);
307
308         wil_dbg_misc(wil, "%s()\n", __func__);
309
310         netif_tx_stop_all_queues(ndev);
311         netif_carrier_off(ndev);
312 }
313
314 static int __wil_up(struct wil6210_priv *wil)
315 {
316         struct net_device *ndev = wil_to_ndev(wil);
317         struct wireless_dev *wdev = wil->wdev;
318         struct ieee80211_channel *channel = wdev->preset_chandef.chan;
319         int rc;
320         int bi;
321         u16 wmi_nettype = wil_iftype_nl2wmi(wdev->iftype);
322
323         rc = wil_reset(wil);
324         if (rc)
325                 return rc;
326
327         /* FIXME Firmware works now in PBSS mode(ToDS=0, FromDS=0) */
328         wmi_nettype = wil_iftype_nl2wmi(NL80211_IFTYPE_ADHOC);
329         switch (wdev->iftype) {
330         case NL80211_IFTYPE_STATION:
331                 wil_dbg_misc(wil, "type: STATION\n");
332                 bi = 0;
333                 ndev->type = ARPHRD_ETHER;
334                 break;
335         case NL80211_IFTYPE_AP:
336                 wil_dbg_misc(wil, "type: AP\n");
337                 bi = 100;
338                 ndev->type = ARPHRD_ETHER;
339                 break;
340         case NL80211_IFTYPE_P2P_CLIENT:
341                 wil_dbg_misc(wil, "type: P2P_CLIENT\n");
342                 bi = 0;
343                 ndev->type = ARPHRD_ETHER;
344                 break;
345         case NL80211_IFTYPE_P2P_GO:
346                 wil_dbg_misc(wil, "type: P2P_GO\n");
347                 bi = 100;
348                 ndev->type = ARPHRD_ETHER;
349                 break;
350         case NL80211_IFTYPE_MONITOR:
351                 wil_dbg_misc(wil, "type: Monitor\n");
352                 bi = 0;
353                 ndev->type = ARPHRD_IEEE80211_RADIOTAP;
354                 /* ARPHRD_IEEE80211 or ARPHRD_IEEE80211_RADIOTAP ? */
355                 break;
356         default:
357                 return -EOPNOTSUPP;
358         }
359
360         /* Apply profile in the following order: */
361         /* SSID and channel for the AP */
362         switch (wdev->iftype) {
363         case NL80211_IFTYPE_AP:
364         case NL80211_IFTYPE_P2P_GO:
365                 if (wdev->ssid_len == 0) {
366                         wil_err(wil, "SSID not set\n");
367                         return -EINVAL;
368                 }
369                 wmi_set_ssid(wil, wdev->ssid_len, wdev->ssid);
370                 if (channel)
371                         wmi_set_channel(wil, channel->hw_value);
372                 break;
373         default:
374                 break;
375         }
376
377         /* MAC address - pre-requisite for other commands */
378         wmi_set_mac_address(wil, ndev->dev_addr);
379
380         /* Set up beaconing if required. */
381         rc = wmi_set_bcon(wil, bi, wmi_nettype);
382         if (rc)
383                 return rc;
384
385         /* Rx VRING. After MAC and beacon */
386         wil_rx_init(wil);
387
388         return 0;
389 }
390
391 int wil_up(struct wil6210_priv *wil)
392 {
393         int rc;
394
395         mutex_lock(&wil->mutex);
396         rc = __wil_up(wil);
397         mutex_unlock(&wil->mutex);
398
399         return rc;
400 }
401
402 static int __wil_down(struct wil6210_priv *wil)
403 {
404         if (wil->scan_request) {
405                 cfg80211_scan_done(wil->scan_request, true);
406                 wil->scan_request = NULL;
407         }
408
409         wil6210_disconnect(wil, NULL);
410         wil_rx_fini(wil);
411
412         return 0;
413 }
414
415 int wil_down(struct wil6210_priv *wil)
416 {
417         int rc;
418
419         mutex_lock(&wil->mutex);
420         rc = __wil_down(wil);
421         mutex_unlock(&wil->mutex);
422
423         return rc;
424 }