1 // SPDX-License-Identifier: ISC
3 * Copyright (c) 2012-2017 Qualcomm Atheros, Inc.
4 * Copyright (c) 2018-2019, The Linux Foundation. All rights reserved.
7 #include <linux/etherdevice.h>
8 #include <linux/rtnetlink.h>
12 bool wil_has_other_active_ifaces(struct wil6210_priv
*wil
,
13 struct net_device
*ndev
, bool up
, bool ok
)
16 struct wil6210_vif
*vif
;
17 struct net_device
*ndev_i
;
19 for (i
= 0; i
< GET_MAX_VIFS(wil
); i
++) {
22 ndev_i
= vif_to_ndev(vif
);
24 if ((up
&& (ndev_i
->flags
& IFF_UP
)) ||
25 (ok
&& netif_carrier_ok(ndev_i
)))
33 bool wil_has_active_ifaces(struct wil6210_priv
*wil
, bool up
, bool ok
)
35 /* use NULL ndev argument to check all interfaces */
36 return wil_has_other_active_ifaces(wil
, NULL
, up
, ok
);
39 static int wil_open(struct net_device
*ndev
)
41 struct wil6210_priv
*wil
= ndev_to_wil(ndev
);
44 wil_dbg_misc(wil
, "open\n");
47 test_bit(WMI_FW_CAPABILITY_WMI_ONLY
, wil
->fw_capabilities
)) {
48 wil_err(wil
, "while in debug_fw or wmi_only mode\n");
52 if (!wil_has_other_active_ifaces(wil
, ndev
, true, false)) {
53 wil_dbg_misc(wil
, "open, first iface\n");
54 rc
= wil_pm_runtime_get(wil
);
60 wil_pm_runtime_put(wil
);
66 static int wil_stop(struct net_device
*ndev
)
68 struct wil6210_priv
*wil
= ndev_to_wil(ndev
);
71 wil_dbg_misc(wil
, "stop\n");
73 if (!wil_has_other_active_ifaces(wil
, ndev
, true, false)) {
74 wil_dbg_misc(wil
, "stop, last iface\n");
77 wil_pm_runtime_put(wil
);
83 static const struct net_device_ops wil_netdev_ops
= {
86 .ndo_start_xmit
= wil_start_xmit
,
87 .ndo_set_mac_address
= eth_mac_addr
,
88 .ndo_validate_addr
= eth_validate_addr
,
91 static int wil6210_netdev_poll_rx(struct napi_struct
*napi
, int budget
)
93 struct wil6210_priv
*wil
= container_of(napi
, struct wil6210_priv
,
98 wil_rx_handle(wil
, "a
);
99 done
= budget
- quota
;
102 napi_complete_done(napi
, done
);
103 wil6210_unmask_irq_rx(wil
);
104 wil_dbg_txrx(wil
, "NAPI RX complete\n");
107 wil_dbg_txrx(wil
, "NAPI RX poll(%d) done %d\n", budget
, done
);
112 static int wil6210_netdev_poll_rx_edma(struct napi_struct
*napi
, int budget
)
114 struct wil6210_priv
*wil
= container_of(napi
, struct wil6210_priv
,
119 wil_rx_handle_edma(wil
, "a
);
120 done
= budget
- quota
;
123 napi_complete_done(napi
, done
);
124 wil6210_unmask_irq_rx_edma(wil
);
125 wil_dbg_txrx(wil
, "NAPI RX complete\n");
128 wil_dbg_txrx(wil
, "NAPI RX poll(%d) done %d\n", budget
, done
);
133 static int wil6210_netdev_poll_tx(struct napi_struct
*napi
, int budget
)
135 struct wil6210_priv
*wil
= container_of(napi
, struct wil6210_priv
,
140 /* always process ALL Tx complete, regardless budget - it is fast */
141 for (i
= 0; i
< WIL6210_MAX_TX_RINGS
; i
++) {
142 struct wil_ring
*ring
= &wil
->ring_tx
[i
];
143 struct wil_ring_tx_data
*txdata
= &wil
->ring_tx_data
[i
];
144 struct wil6210_vif
*vif
;
146 if (!ring
->va
|| !txdata
->enabled
||
147 txdata
->mid
>= GET_MAX_VIFS(wil
))
150 vif
= wil
->vifs
[txdata
->mid
];
151 if (unlikely(!vif
)) {
152 wil_dbg_txrx(wil
, "Invalid MID %d\n", txdata
->mid
);
156 tx_done
+= wil_tx_complete(vif
, i
);
159 if (tx_done
< budget
) {
161 wil6210_unmask_irq_tx(wil
);
162 wil_dbg_txrx(wil
, "NAPI TX complete\n");
165 wil_dbg_txrx(wil
, "NAPI TX poll(%d) done %d\n", budget
, tx_done
);
167 return min(tx_done
, budget
);
170 static int wil6210_netdev_poll_tx_edma(struct napi_struct
*napi
, int budget
)
172 struct wil6210_priv
*wil
= container_of(napi
, struct wil6210_priv
,
175 /* There is only one status TX ring */
176 struct wil_status_ring
*sring
= &wil
->srings
[wil
->tx_sring_idx
];
181 tx_done
= wil_tx_sring_handler(wil
, sring
);
183 if (tx_done
< budget
) {
185 wil6210_unmask_irq_tx_edma(wil
);
186 wil_dbg_txrx(wil
, "NAPI TX complete\n");
189 wil_dbg_txrx(wil
, "NAPI TX poll(%d) done %d\n", budget
, tx_done
);
191 return min(tx_done
, budget
);
194 static void wil_dev_setup(struct net_device
*dev
)
197 dev
->max_mtu
= mtu_max
;
198 dev
->tx_queue_len
= WIL_TX_Q_LEN_DEFAULT
;
201 static void wil_vif_deinit(struct wil6210_vif
*vif
)
203 del_timer_sync(&vif
->scan_timer
);
204 del_timer_sync(&vif
->p2p
.discovery_timer
);
205 cancel_work_sync(&vif
->disconnect_worker
);
206 cancel_work_sync(&vif
->p2p
.discovery_expired_work
);
207 cancel_work_sync(&vif
->p2p
.delayed_listen_work
);
208 wil_probe_client_flush(vif
);
209 cancel_work_sync(&vif
->probe_client_worker
);
210 cancel_work_sync(&vif
->enable_tx_key_worker
);
213 void wil_vif_free(struct wil6210_vif
*vif
)
215 struct net_device
*ndev
= vif_to_ndev(vif
);
221 static void wil_ndev_destructor(struct net_device
*ndev
)
223 struct wil6210_vif
*vif
= ndev_to_vif(ndev
);
228 static void wil_connect_timer_fn(struct timer_list
*t
)
230 struct wil6210_vif
*vif
= from_timer(vif
, t
, connect_timer
);
231 struct wil6210_priv
*wil
= vif_to_wil(vif
);
234 wil_err(wil
, "Connect timeout detected, disconnect station\n");
236 /* reschedule to thread context - disconnect won't
237 * run from atomic context.
238 * queue on wmi_wq to prevent race with connect event.
240 q
= queue_work(wil
->wmi_wq
, &vif
->disconnect_worker
);
241 wil_dbg_wmi(wil
, "queue_work of disconnect_worker -> %d\n", q
);
244 static void wil_scan_timer_fn(struct timer_list
*t
)
246 struct wil6210_vif
*vif
= from_timer(vif
, t
, scan_timer
);
247 struct wil6210_priv
*wil
= vif_to_wil(vif
);
249 clear_bit(wil_status_fwready
, wil
->status
);
250 wil_err(wil
, "Scan timeout detected, start fw error recovery\n");
251 wil_fw_error_recovery(wil
);
254 static void wil_p2p_discovery_timer_fn(struct timer_list
*t
)
256 struct wil6210_vif
*vif
= from_timer(vif
, t
, p2p
.discovery_timer
);
257 struct wil6210_priv
*wil
= vif_to_wil(vif
);
259 wil_dbg_misc(wil
, "p2p_discovery_timer_fn\n");
261 schedule_work(&vif
->p2p
.discovery_expired_work
);
264 static void wil_vif_init(struct wil6210_vif
*vif
)
266 vif
->bcast_ring
= -1;
268 mutex_init(&vif
->probe_client_mutex
);
270 timer_setup(&vif
->connect_timer
, wil_connect_timer_fn
, 0);
271 timer_setup(&vif
->scan_timer
, wil_scan_timer_fn
, 0);
272 timer_setup(&vif
->p2p
.discovery_timer
, wil_p2p_discovery_timer_fn
, 0);
274 INIT_WORK(&vif
->probe_client_worker
, wil_probe_client_worker
);
275 INIT_WORK(&vif
->disconnect_worker
, wil_disconnect_worker
);
276 INIT_WORK(&vif
->p2p
.discovery_expired_work
, wil_p2p_listen_expired
);
277 INIT_WORK(&vif
->p2p
.delayed_listen_work
, wil_p2p_delayed_listen_work
);
278 INIT_WORK(&vif
->enable_tx_key_worker
, wil_enable_tx_key_worker
);
280 INIT_LIST_HEAD(&vif
->probe_client_pending
);
282 vif
->net_queue_stopped
= 1;
285 static u8
wil_vif_find_free_mid(struct wil6210_priv
*wil
)
289 for (i
= 0; i
< GET_MAX_VIFS(wil
); i
++) {
298 wil_vif_alloc(struct wil6210_priv
*wil
, const char *name
,
299 unsigned char name_assign_type
, enum nl80211_iftype iftype
)
301 struct net_device
*ndev
;
302 struct wireless_dev
*wdev
;
303 struct wil6210_vif
*vif
;
306 mid
= wil_vif_find_free_mid(wil
);
308 wil_err(wil
, "no available virtual interface\n");
309 return ERR_PTR(-EINVAL
);
312 ndev
= alloc_netdev(sizeof(*vif
), name
, name_assign_type
,
315 dev_err(wil_to_dev(wil
), "alloc_netdev failed\n");
316 return ERR_PTR(-ENOMEM
);
319 wil
->main_ndev
= ndev
;
321 ndev
->priv_destructor
= wil_ndev_destructor
;
322 ndev
->needs_free_netdev
= true;
325 vif
= ndev_to_vif(ndev
);
332 wdev
->wiphy
= wil
->wiphy
;
333 wdev
->iftype
= iftype
;
335 ndev
->netdev_ops
= &wil_netdev_ops
;
336 wil_set_ethtoolops(ndev
);
337 ndev
->ieee80211_ptr
= wdev
;
338 ndev
->hw_features
= NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
|
339 NETIF_F_SG
| NETIF_F_GRO
|
340 NETIF_F_TSO
| NETIF_F_TSO6
;
342 ndev
->features
|= ndev
->hw_features
;
343 SET_NETDEV_DEV(ndev
, wiphy_dev(wdev
->wiphy
));
348 void *wil_if_alloc(struct device
*dev
)
350 struct wil6210_priv
*wil
;
351 struct wil6210_vif
*vif
;
354 wil
= wil_cfg80211_init(dev
);
356 dev_err(dev
, "wil_cfg80211_init failed\n");
360 rc
= wil_priv_init(wil
);
362 dev_err(dev
, "wil_priv_init failed\n");
366 wil_dbg_misc(wil
, "if_alloc\n");
368 vif
= wil_vif_alloc(wil
, "wlan%d", NET_NAME_UNKNOWN
,
369 NL80211_IFTYPE_STATION
);
371 dev_err(dev
, "wil_vif_alloc failed\n");
376 wil
->radio_wdev
= vif_to_wdev(vif
);
381 wil_priv_deinit(wil
);
384 wil_cfg80211_deinit(wil
);
389 void wil_if_free(struct wil6210_priv
*wil
)
391 struct net_device
*ndev
= wil
->main_ndev
;
393 wil_dbg_misc(wil
, "if_free\n");
398 wil_priv_deinit(wil
);
400 wil
->main_ndev
= NULL
;
401 wil_ndev_destructor(ndev
);
404 wil_cfg80211_deinit(wil
);
407 int wil_vif_add(struct wil6210_priv
*wil
, struct wil6210_vif
*vif
)
409 struct net_device
*ndev
= vif_to_ndev(vif
);
410 struct wireless_dev
*wdev
= vif_to_wdev(vif
);
411 bool any_active
= wil_has_active_ifaces(wil
, true, false);
416 if (wil
->vifs
[vif
->mid
]) {
417 dev_err(&ndev
->dev
, "VIF with mid %d already in use\n",
421 if (any_active
&& vif
->mid
!= 0) {
422 rc
= wmi_port_allocate(wil
, vif
->mid
, ndev
->dev_addr
,
427 rc
= cfg80211_register_netdevice(ndev
);
429 dev_err(&ndev
->dev
, "Failed to register netdev: %d\n", rc
);
430 if (any_active
&& vif
->mid
!= 0)
431 wmi_port_delete(wil
, vif
->mid
);
435 wil
->vifs
[vif
->mid
] = vif
;
439 int wil_if_add(struct wil6210_priv
*wil
)
441 struct wiphy
*wiphy
= wil
->wiphy
;
442 struct net_device
*ndev
= wil
->main_ndev
;
443 struct wil6210_vif
*vif
= ndev_to_vif(ndev
);
446 wil_dbg_misc(wil
, "entered");
448 strscpy(wiphy
->fw_version
, wil
->fw_version
, sizeof(wiphy
->fw_version
));
450 rc
= wiphy_register(wiphy
);
452 wil_err(wil
, "failed to register wiphy, err %d\n", rc
);
456 wil
->napi_ndev
= alloc_netdev_dummy(0);
457 if (!wil
->napi_ndev
) {
458 wil_err(wil
, "failed to allocate dummy netdev");
462 if (wil
->use_enhanced_dma_hw
) {
463 netif_napi_add(wil
->napi_ndev
, &wil
->napi_rx
,
464 wil6210_netdev_poll_rx_edma
);
465 netif_napi_add_tx(wil
->napi_ndev
,
466 &wil
->napi_tx
, wil6210_netdev_poll_tx_edma
);
468 netif_napi_add(wil
->napi_ndev
, &wil
->napi_rx
,
469 wil6210_netdev_poll_rx
);
470 netif_napi_add_tx(wil
->napi_ndev
,
471 &wil
->napi_tx
, wil6210_netdev_poll_tx
);
474 wil_update_net_queues_bh(wil
, vif
, NULL
, true);
478 rc
= wil_vif_add(wil
, vif
);
487 free_netdev(wil
->napi_ndev
);
489 wiphy_unregister(wiphy
);
493 void wil_vif_remove(struct wil6210_priv
*wil
, u8 mid
)
495 struct wil6210_vif
*vif
;
496 struct net_device
*ndev
;
497 bool any_active
= wil_has_active_ifaces(wil
, true, false);
500 if (mid
>= GET_MAX_VIFS(wil
)) {
501 wil_err(wil
, "invalid MID: %d\n", mid
);
505 vif
= wil
->vifs
[mid
];
507 wil_err(wil
, "MID %d not registered\n", mid
);
511 mutex_lock(&wil
->mutex
);
512 wil6210_disconnect(vif
, NULL
, WLAN_REASON_DEAUTH_LEAVING
);
513 mutex_unlock(&wil
->mutex
);
515 ndev
= vif_to_ndev(vif
);
516 /* during unregister_netdevice cfg80211_leave may perform operations
517 * such as stop AP, disconnect, so we only clear the VIF afterwards
519 cfg80211_unregister_netdevice(ndev
);
521 if (any_active
&& vif
->mid
!= 0)
522 wmi_port_delete(wil
, vif
->mid
);
524 /* make sure no one is accessing the VIF before removing */
525 mutex_lock(&wil
->vif_mutex
);
526 wil
->vifs
[mid
] = NULL
;
527 /* ensure NAPI code will see the NULL VIF */
529 if (test_bit(wil_status_napi_en
, wil
->status
)) {
530 napi_synchronize(&wil
->napi_rx
);
531 napi_synchronize(&wil
->napi_tx
);
533 mutex_unlock(&wil
->vif_mutex
);
535 flush_work(&wil
->wmi_event_worker
);
536 del_timer_sync(&vif
->connect_timer
);
537 cancel_work_sync(&vif
->disconnect_worker
);
538 wil_probe_client_flush(vif
);
539 cancel_work_sync(&vif
->probe_client_worker
);
540 cancel_work_sync(&vif
->enable_tx_key_worker
);
541 /* for VIFs, ndev will be freed by destructor after RTNL is unlocked.
542 * the main interface will be freed in wil_if_free, we need to keep it
543 * a bit longer so logging macros will work.
547 void wil_if_remove(struct wil6210_priv
*wil
)
549 struct net_device
*ndev
= wil
->main_ndev
;
550 struct wireless_dev
*wdev
= ndev
->ieee80211_ptr
;
551 struct wiphy
*wiphy
= wdev
->wiphy
;
553 wil_dbg_misc(wil
, "if_remove\n");
557 wil_vif_remove(wil
, 0);
561 netif_napi_del(&wil
->napi_tx
);
562 netif_napi_del(&wil
->napi_rx
);
564 free_netdev(wil
->napi_ndev
);
566 wiphy_unregister(wiphy
);