GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / net / wireless / ath / wil6210 / netdev.c
1 /*
2  * Copyright (c) 2012-2017 Qualcomm Atheros, Inc.
3  * Copyright (c) 2018, The Linux Foundation. All rights reserved.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 #include <linux/etherdevice.h>
19 #include <linux/rtnetlink.h>
20 #include "wil6210.h"
21 #include "txrx.h"
22
23 bool wil_has_other_active_ifaces(struct wil6210_priv *wil,
24                                  struct net_device *ndev, bool up, bool ok)
25 {
26         int i;
27         struct wil6210_vif *vif;
28         struct net_device *ndev_i;
29
30         for (i = 0; i < wil->max_vifs; i++) {
31                 vif = wil->vifs[i];
32                 if (vif) {
33                         ndev_i = vif_to_ndev(vif);
34                         if (ndev_i != ndev)
35                                 if ((up && (ndev_i->flags & IFF_UP)) ||
36                                     (ok && netif_carrier_ok(ndev_i)))
37                                         return true;
38                 }
39         }
40
41         return false;
42 }
43
44 bool wil_has_active_ifaces(struct wil6210_priv *wil, bool up, bool ok)
45 {
46         /* use NULL ndev argument to check all interfaces */
47         return wil_has_other_active_ifaces(wil, NULL, up, ok);
48 }
49
50 static int wil_open(struct net_device *ndev)
51 {
52         struct wil6210_priv *wil = ndev_to_wil(ndev);
53         int rc = 0;
54
55         wil_dbg_misc(wil, "open\n");
56
57         if (debug_fw ||
58             test_bit(WMI_FW_CAPABILITY_WMI_ONLY, wil->fw_capabilities)) {
59                 wil_err(wil, "while in debug_fw or wmi_only mode\n");
60                 return -EINVAL;
61         }
62
63         if (!wil_has_other_active_ifaces(wil, ndev, true, false)) {
64                 wil_dbg_misc(wil, "open, first iface\n");
65                 rc = wil_pm_runtime_get(wil);
66                 if (rc < 0)
67                         return rc;
68
69                 rc = wil_up(wil);
70                 if (rc)
71                         wil_pm_runtime_put(wil);
72         }
73
74         return rc;
75 }
76
77 static int wil_stop(struct net_device *ndev)
78 {
79         struct wil6210_priv *wil = ndev_to_wil(ndev);
80         int rc = 0;
81
82         wil_dbg_misc(wil, "stop\n");
83
84         if (!wil_has_other_active_ifaces(wil, ndev, true, false)) {
85                 wil_dbg_misc(wil, "stop, last iface\n");
86                 rc = wil_down(wil);
87                 if (!rc)
88                         wil_pm_runtime_put(wil);
89         }
90
91         return rc;
92 }
93
94 static const struct net_device_ops wil_netdev_ops = {
95         .ndo_open               = wil_open,
96         .ndo_stop               = wil_stop,
97         .ndo_start_xmit         = wil_start_xmit,
98         .ndo_set_mac_address    = eth_mac_addr,
99         .ndo_validate_addr      = eth_validate_addr,
100 };
101
102 static int wil6210_netdev_poll_rx(struct napi_struct *napi, int budget)
103 {
104         struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
105                                                 napi_rx);
106         int quota = budget;
107         int done;
108
109         wil_rx_handle(wil, &quota);
110         done = budget - quota;
111
112         if (done < budget) {
113                 napi_complete_done(napi, done);
114                 wil6210_unmask_irq_rx(wil);
115                 wil_dbg_txrx(wil, "NAPI RX complete\n");
116         }
117
118         wil_dbg_txrx(wil, "NAPI RX poll(%d) done %d\n", budget, done);
119
120         return done;
121 }
122
123 static int wil6210_netdev_poll_rx_edma(struct napi_struct *napi, int budget)
124 {
125         struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
126                                                 napi_rx);
127         int quota = budget;
128         int done;
129
130         wil_rx_handle_edma(wil, &quota);
131         done = budget - quota;
132
133         if (done < budget) {
134                 napi_complete_done(napi, done);
135                 wil6210_unmask_irq_rx_edma(wil);
136                 wil_dbg_txrx(wil, "NAPI RX complete\n");
137         }
138
139         wil_dbg_txrx(wil, "NAPI RX poll(%d) done %d\n", budget, done);
140
141         return done;
142 }
143
144 static int wil6210_netdev_poll_tx(struct napi_struct *napi, int budget)
145 {
146         struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
147                                                 napi_tx);
148         int tx_done = 0;
149         uint i;
150
151         /* always process ALL Tx complete, regardless budget - it is fast */
152         for (i = 0; i < WIL6210_MAX_TX_RINGS; i++) {
153                 struct wil_ring *ring = &wil->ring_tx[i];
154                 struct wil_ring_tx_data *txdata = &wil->ring_tx_data[i];
155                 struct wil6210_vif *vif;
156
157                 if (!ring->va || !txdata->enabled ||
158                     txdata->mid >= wil->max_vifs)
159                         continue;
160
161                 vif = wil->vifs[txdata->mid];
162                 if (unlikely(!vif)) {
163                         wil_dbg_txrx(wil, "Invalid MID %d\n", txdata->mid);
164                         continue;
165                 }
166
167                 tx_done += wil_tx_complete(vif, i);
168         }
169
170         if (tx_done < budget) {
171                 napi_complete(napi);
172                 wil6210_unmask_irq_tx(wil);
173                 wil_dbg_txrx(wil, "NAPI TX complete\n");
174         }
175
176         wil_dbg_txrx(wil, "NAPI TX poll(%d) done %d\n", budget, tx_done);
177
178         return min(tx_done, budget);
179 }
180
181 static int wil6210_netdev_poll_tx_edma(struct napi_struct *napi, int budget)
182 {
183         struct wil6210_priv *wil = container_of(napi, struct wil6210_priv,
184                                                 napi_tx);
185         int tx_done;
186         /* There is only one status TX ring */
187         struct wil_status_ring *sring = &wil->srings[wil->tx_sring_idx];
188
189         if (!sring->va)
190                 return 0;
191
192         tx_done = wil_tx_sring_handler(wil, sring);
193
194         if (tx_done < budget) {
195                 napi_complete(napi);
196                 wil6210_unmask_irq_tx_edma(wil);
197                 wil_dbg_txrx(wil, "NAPI TX complete\n");
198         }
199
200         wil_dbg_txrx(wil, "NAPI TX poll(%d) done %d\n", budget, tx_done);
201
202         return min(tx_done, budget);
203 }
204
205 static void wil_dev_setup(struct net_device *dev)
206 {
207         ether_setup(dev);
208         dev->max_mtu = mtu_max;
209         dev->tx_queue_len = WIL_TX_Q_LEN_DEFAULT;
210 }
211
212 static void wil_vif_deinit(struct wil6210_vif *vif)
213 {
214         del_timer_sync(&vif->scan_timer);
215         del_timer_sync(&vif->p2p.discovery_timer);
216         cancel_work_sync(&vif->disconnect_worker);
217         cancel_work_sync(&vif->p2p.discovery_expired_work);
218         cancel_work_sync(&vif->p2p.delayed_listen_work);
219         wil_probe_client_flush(vif);
220         cancel_work_sync(&vif->probe_client_worker);
221 }
222
223 void wil_vif_free(struct wil6210_vif *vif)
224 {
225         struct net_device *ndev = vif_to_ndev(vif);
226
227         wil_vif_deinit(vif);
228         free_netdev(ndev);
229 }
230
231 static void wil_ndev_destructor(struct net_device *ndev)
232 {
233         struct wil6210_vif *vif = ndev_to_vif(ndev);
234
235         wil_vif_deinit(vif);
236 }
237
238 static void wil_connect_timer_fn(struct timer_list *t)
239 {
240         struct wil6210_vif *vif = from_timer(vif, t, connect_timer);
241         struct wil6210_priv *wil = vif_to_wil(vif);
242         bool q;
243
244         wil_err(wil, "Connect timeout detected, disconnect station\n");
245
246         /* reschedule to thread context - disconnect won't
247          * run from atomic context.
248          * queue on wmi_wq to prevent race with connect event.
249          */
250         q = queue_work(wil->wmi_wq, &vif->disconnect_worker);
251         wil_dbg_wmi(wil, "queue_work of disconnect_worker -> %d\n", q);
252 }
253
254 static void wil_scan_timer_fn(struct timer_list *t)
255 {
256         struct wil6210_vif *vif = from_timer(vif, t, scan_timer);
257         struct wil6210_priv *wil = vif_to_wil(vif);
258
259         clear_bit(wil_status_fwready, wil->status);
260         wil_err(wil, "Scan timeout detected, start fw error recovery\n");
261         wil_fw_error_recovery(wil);
262 }
263
264 static void wil_p2p_discovery_timer_fn(struct timer_list *t)
265 {
266         struct wil6210_vif *vif = from_timer(vif, t, p2p.discovery_timer);
267         struct wil6210_priv *wil = vif_to_wil(vif);
268
269         wil_dbg_misc(wil, "p2p_discovery_timer_fn\n");
270
271         schedule_work(&vif->p2p.discovery_expired_work);
272 }
273
274 static void wil_vif_init(struct wil6210_vif *vif)
275 {
276         vif->bcast_ring = -1;
277
278         mutex_init(&vif->probe_client_mutex);
279
280         timer_setup(&vif->connect_timer, wil_connect_timer_fn, 0);
281         timer_setup(&vif->scan_timer, wil_scan_timer_fn, 0);
282         timer_setup(&vif->p2p.discovery_timer, wil_p2p_discovery_timer_fn, 0);
283
284         INIT_WORK(&vif->probe_client_worker, wil_probe_client_worker);
285         INIT_WORK(&vif->disconnect_worker, wil_disconnect_worker);
286         INIT_WORK(&vif->p2p.delayed_listen_work, wil_p2p_delayed_listen_work);
287
288         INIT_LIST_HEAD(&vif->probe_client_pending);
289
290         vif->net_queue_stopped = 1;
291 }
292
293 static u8 wil_vif_find_free_mid(struct wil6210_priv *wil)
294 {
295         u8 i;
296
297         for (i = 0; i < wil->max_vifs; i++) {
298                 if (!wil->vifs[i])
299                         return i;
300         }
301
302         return U8_MAX;
303 }
304
305 struct wil6210_vif *
306 wil_vif_alloc(struct wil6210_priv *wil, const char *name,
307               unsigned char name_assign_type, enum nl80211_iftype iftype)
308 {
309         struct net_device *ndev;
310         struct wireless_dev *wdev;
311         struct wil6210_vif *vif;
312         u8 mid;
313
314         mid = wil_vif_find_free_mid(wil);
315         if (mid == U8_MAX) {
316                 wil_err(wil, "no available virtual interface\n");
317                 return ERR_PTR(-EINVAL);
318         }
319
320         ndev = alloc_netdev(sizeof(*vif), name, name_assign_type,
321                             wil_dev_setup);
322         if (!ndev) {
323                 dev_err(wil_to_dev(wil), "alloc_netdev failed\n");
324                 return ERR_PTR(-ENOMEM);
325         }
326         if (mid == 0) {
327                 wil->main_ndev = ndev;
328         } else {
329                 ndev->priv_destructor = wil_ndev_destructor;
330                 ndev->needs_free_netdev = true;
331         }
332
333         vif = ndev_to_vif(ndev);
334         vif->ndev = ndev;
335         vif->wil = wil;
336         vif->mid = mid;
337         wil_vif_init(vif);
338
339         wdev = &vif->wdev;
340         wdev->wiphy = wil->wiphy;
341         wdev->iftype = iftype;
342
343         ndev->netdev_ops = &wil_netdev_ops;
344         wil_set_ethtoolops(ndev);
345         ndev->ieee80211_ptr = wdev;
346         ndev->hw_features = NETIF_F_HW_CSUM | NETIF_F_RXCSUM |
347                             NETIF_F_SG | NETIF_F_GRO |
348                             NETIF_F_TSO | NETIF_F_TSO6 |
349                             NETIF_F_RXHASH;
350
351         ndev->features |= ndev->hw_features;
352         SET_NETDEV_DEV(ndev, wiphy_dev(wdev->wiphy));
353         wdev->netdev = ndev;
354         return vif;
355 }
356
357 void *wil_if_alloc(struct device *dev)
358 {
359         struct wil6210_priv *wil;
360         struct wil6210_vif *vif;
361         int rc = 0;
362
363         wil = wil_cfg80211_init(dev);
364         if (IS_ERR(wil)) {
365                 dev_err(dev, "wil_cfg80211_init failed\n");
366                 return wil;
367         }
368
369         rc = wil_priv_init(wil);
370         if (rc) {
371                 dev_err(dev, "wil_priv_init failed\n");
372                 goto out_cfg;
373         }
374
375         wil_dbg_misc(wil, "if_alloc\n");
376
377         vif = wil_vif_alloc(wil, "wlan%d", NET_NAME_UNKNOWN,
378                             NL80211_IFTYPE_STATION);
379         if (IS_ERR(vif)) {
380                 dev_err(dev, "wil_vif_alloc failed\n");
381                 rc = -ENOMEM;
382                 goto out_priv;
383         }
384
385         wil->radio_wdev = vif_to_wdev(vif);
386
387         return wil;
388
389 out_priv:
390         wil_priv_deinit(wil);
391
392 out_cfg:
393         wil_cfg80211_deinit(wil);
394
395         return ERR_PTR(rc);
396 }
397
398 void wil_if_free(struct wil6210_priv *wil)
399 {
400         struct net_device *ndev = wil->main_ndev;
401
402         wil_dbg_misc(wil, "if_free\n");
403
404         if (!ndev)
405                 return;
406
407         wil_priv_deinit(wil);
408
409         wil->main_ndev = NULL;
410         wil_ndev_destructor(ndev);
411         free_netdev(ndev);
412
413         wil_cfg80211_deinit(wil);
414 }
415
416 int wil_vif_add(struct wil6210_priv *wil, struct wil6210_vif *vif)
417 {
418         struct net_device *ndev = vif_to_ndev(vif);
419         struct wireless_dev *wdev = vif_to_wdev(vif);
420         bool any_active = wil_has_active_ifaces(wil, true, false);
421         int rc;
422
423         ASSERT_RTNL();
424
425         if (wil->vifs[vif->mid]) {
426                 dev_err(&ndev->dev, "VIF with mid %d already in use\n",
427                         vif->mid);
428                 return -EEXIST;
429         }
430         if (any_active && vif->mid != 0) {
431                 rc = wmi_port_allocate(wil, vif->mid, ndev->dev_addr,
432                                        wdev->iftype);
433                 if (rc)
434                         return rc;
435         }
436         rc = register_netdevice(ndev);
437         if (rc < 0) {
438                 dev_err(&ndev->dev, "Failed to register netdev: %d\n", rc);
439                 if (any_active && vif->mid != 0)
440                         wmi_port_delete(wil, vif->mid);
441                 return rc;
442         }
443
444         wil->vifs[vif->mid] = vif;
445         return 0;
446 }
447
448 int wil_if_add(struct wil6210_priv *wil)
449 {
450         struct wiphy *wiphy = wil->wiphy;
451         struct net_device *ndev = wil->main_ndev;
452         struct wil6210_vif *vif = ndev_to_vif(ndev);
453         int rc;
454
455         wil_dbg_misc(wil, "entered");
456
457         strlcpy(wiphy->fw_version, wil->fw_version, sizeof(wiphy->fw_version));
458
459         rc = wiphy_register(wiphy);
460         if (rc < 0) {
461                 wil_err(wil, "failed to register wiphy, err %d\n", rc);
462                 return rc;
463         }
464
465         init_dummy_netdev(&wil->napi_ndev);
466         if (wil->use_enhanced_dma_hw) {
467                 netif_napi_add(&wil->napi_ndev, &wil->napi_rx,
468                                wil6210_netdev_poll_rx_edma,
469                                WIL6210_NAPI_BUDGET);
470                 netif_tx_napi_add(&wil->napi_ndev,
471                                   &wil->napi_tx, wil6210_netdev_poll_tx_edma,
472                                   WIL6210_NAPI_BUDGET);
473         } else {
474                 netif_napi_add(&wil->napi_ndev, &wil->napi_rx,
475                                wil6210_netdev_poll_rx,
476                                WIL6210_NAPI_BUDGET);
477                 netif_tx_napi_add(&wil->napi_ndev,
478                                   &wil->napi_tx, wil6210_netdev_poll_tx,
479                                   WIL6210_NAPI_BUDGET);
480         }
481
482         wil_update_net_queues_bh(wil, vif, NULL, true);
483
484         rtnl_lock();
485         rc = wil_vif_add(wil, vif);
486         rtnl_unlock();
487         if (rc < 0)
488                 goto out_wiphy;
489
490         return 0;
491
492 out_wiphy:
493         wiphy_unregister(wiphy);
494         return rc;
495 }
496
497 void wil_vif_remove(struct wil6210_priv *wil, u8 mid)
498 {
499         struct wil6210_vif *vif;
500         struct net_device *ndev;
501         bool any_active = wil_has_active_ifaces(wil, true, false);
502
503         ASSERT_RTNL();
504         if (mid >= wil->max_vifs) {
505                 wil_err(wil, "invalid MID: %d\n", mid);
506                 return;
507         }
508
509         vif = wil->vifs[mid];
510         if (!vif) {
511                 wil_err(wil, "MID %d not registered\n", mid);
512                 return;
513         }
514
515         mutex_lock(&wil->mutex);
516         wil6210_disconnect(vif, NULL, WLAN_REASON_DEAUTH_LEAVING, false);
517         mutex_unlock(&wil->mutex);
518
519         ndev = vif_to_ndev(vif);
520         /* during unregister_netdevice cfg80211_leave may perform operations
521          * such as stop AP, disconnect, so we only clear the VIF afterwards
522          */
523         unregister_netdevice(ndev);
524
525         if (any_active && vif->mid != 0)
526                 wmi_port_delete(wil, vif->mid);
527
528         /* make sure no one is accessing the VIF before removing */
529         mutex_lock(&wil->vif_mutex);
530         wil->vifs[mid] = NULL;
531         /* ensure NAPI code will see the NULL VIF */
532         wmb();
533         if (test_bit(wil_status_napi_en, wil->status)) {
534                 napi_synchronize(&wil->napi_rx);
535                 napi_synchronize(&wil->napi_tx);
536         }
537         mutex_unlock(&wil->vif_mutex);
538
539         flush_work(&wil->wmi_event_worker);
540         del_timer_sync(&vif->connect_timer);
541         cancel_work_sync(&vif->disconnect_worker);
542         wil_probe_client_flush(vif);
543         cancel_work_sync(&vif->probe_client_worker);
544         /* for VIFs, ndev will be freed by destructor after RTNL is unlocked.
545          * the main interface will be freed in wil_if_free, we need to keep it
546          * a bit longer so logging macros will work.
547          */
548 }
549
550 void wil_if_remove(struct wil6210_priv *wil)
551 {
552         struct net_device *ndev = wil->main_ndev;
553         struct wireless_dev *wdev = ndev->ieee80211_ptr;
554
555         wil_dbg_misc(wil, "if_remove\n");
556
557         rtnl_lock();
558         wil_vif_remove(wil, 0);
559         rtnl_unlock();
560
561         netif_napi_del(&wil->napi_tx);
562         netif_napi_del(&wil->napi_rx);
563
564         wiphy_unregister(wdev->wiphy);
565 }