1 // SPDX-License-Identifier: GPL-2.0+
2 /* Copyright (c) 2015-2016 Quantenna Communications. All rights reserved. */
3 
4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/if_ether.h>
7 
8 #include "core.h"
9 #include "bus.h"
10 #include "trans.h"
11 #include "commands.h"
12 #include "cfg80211.h"
13 #include "event.h"
14 #include "util.h"
15 
16 #define QTNF_DMP_MAX_LEN 48
17 #define QTNF_PRIMARY_VIF_IDX	0
18 
19 struct qtnf_frame_meta_info {
20 	u8 magic_s;
21 	u8 ifidx;
22 	u8 macid;
23 	u8 magic_e;
24 } __packed;
25 
26 struct qtnf_wmac *qtnf_core_get_mac(const struct qtnf_bus *bus, u8 macid)
27 {
28 	struct qtnf_wmac *mac = NULL;
29 
30 	if (unlikely(macid >= QTNF_MAX_MAC)) {
31 		pr_err("invalid MAC index %u\n", macid);
32 		return NULL;
33 	}
34 
35 	mac = bus->mac[macid];
36 
37 	if (unlikely(!mac)) {
38 		pr_err("MAC%u: not initialized\n", macid);
39 		return NULL;
40 	}
41 
42 	return mac;
43 }
44 
45 /* Netdev handler for open.
46  */
47 static int qtnf_netdev_open(struct net_device *ndev)
48 {
49 	netif_carrier_off(ndev);
50 	qtnf_netdev_updown(ndev, 1);
51 	return 0;
52 }
53 
54 /* Netdev handler for close.
55  */
56 static int qtnf_netdev_close(struct net_device *ndev)
57 {
58 	netif_carrier_off(ndev);
59 	qtnf_virtual_intf_cleanup(ndev);
60 	qtnf_netdev_updown(ndev, 0);
61 	return 0;
62 }
63 
64 /* Netdev handler for data transmission.
65  */
66 static netdev_tx_t
67 qtnf_netdev_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev)
68 {
69 	struct qtnf_vif *vif;
70 	struct qtnf_wmac *mac;
71 
72 	vif = qtnf_netdev_get_priv(ndev);
73 
74 	if (unlikely(skb->dev != ndev)) {
75 		pr_err_ratelimited("invalid skb->dev");
76 		dev_kfree_skb_any(skb);
77 		return 0;
78 	}
79 
80 	if (unlikely(vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)) {
81 		pr_err_ratelimited("%s: VIF not initialized\n", ndev->name);
82 		dev_kfree_skb_any(skb);
83 		return 0;
84 	}
85 
86 	mac = vif->mac;
87 	if (unlikely(!mac)) {
88 		pr_err_ratelimited("%s: NULL mac pointer", ndev->name);
89 		dev_kfree_skb_any(skb);
90 		return 0;
91 	}
92 
93 	if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
94 		pr_err_ratelimited("%s: invalid skb len %d\n", ndev->name,
95 				   skb->len);
96 		dev_kfree_skb_any(skb);
97 		ndev->stats.tx_dropped++;
98 		return 0;
99 	}
100 
101 	/* tx path is enabled: reset vif timeout */
102 	vif->cons_tx_timeout_cnt = 0;
103 
104 	return qtnf_bus_data_tx(mac->bus, skb);
105 }
106 
107 /* Netdev handler for getting stats.
108  */
109 static void qtnf_netdev_get_stats64(struct net_device *ndev,
110 				    struct rtnl_link_stats64 *stats)
111 {
112 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
113 	unsigned int start;
114 	int cpu;
115 
116 	netdev_stats_to_stats64(stats, &ndev->stats);
117 
118 	if (!vif->stats64)
119 		return;
120 
121 	for_each_possible_cpu(cpu) {
122 		struct pcpu_sw_netstats *stats64;
123 		u64 rx_packets, rx_bytes;
124 		u64 tx_packets, tx_bytes;
125 
126 		stats64 = per_cpu_ptr(vif->stats64, cpu);
127 
128 		do {
129 			start = u64_stats_fetch_begin_irq(&stats64->syncp);
130 			rx_packets = stats64->rx_packets;
131 			rx_bytes = stats64->rx_bytes;
132 			tx_packets = stats64->tx_packets;
133 			tx_bytes = stats64->tx_bytes;
134 		} while (u64_stats_fetch_retry_irq(&stats64->syncp, start));
135 
136 		stats->rx_packets += rx_packets;
137 		stats->rx_bytes += rx_bytes;
138 		stats->tx_packets += tx_packets;
139 		stats->tx_bytes += tx_bytes;
140 	}
141 }
142 
143 /* Netdev handler for transmission timeout.
144  */
145 static void qtnf_netdev_tx_timeout(struct net_device *ndev)
146 {
147 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
148 	struct qtnf_wmac *mac;
149 	struct qtnf_bus *bus;
150 
151 	if (unlikely(!vif || !vif->mac || !vif->mac->bus))
152 		return;
153 
154 	mac = vif->mac;
155 	bus = mac->bus;
156 
157 	pr_warn("VIF%u.%u: Tx timeout- %lu\n", mac->macid, vif->vifid, jiffies);
158 
159 	qtnf_bus_data_tx_timeout(bus, ndev);
160 	ndev->stats.tx_errors++;
161 
162 	if (++vif->cons_tx_timeout_cnt > QTNF_TX_TIMEOUT_TRSHLD) {
163 		pr_err("Tx timeout threshold exceeded !\n");
164 		pr_err("schedule interface %s reset !\n", netdev_name(ndev));
165 		queue_work(bus->workqueue, &vif->reset_work);
166 	}
167 }
168 
169 static int qtnf_netdev_set_mac_address(struct net_device *ndev, void *addr)
170 {
171 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
172 	struct sockaddr *sa = addr;
173 	int ret;
174 	unsigned char old_addr[ETH_ALEN];
175 
176 	memcpy(old_addr, sa->sa_data, sizeof(old_addr));
177 
178 	ret = eth_mac_addr(ndev, sa);
179 	if (ret)
180 		return ret;
181 
182 	qtnf_scan_done(vif->mac, true);
183 
184 	ret = qtnf_cmd_send_change_intf_type(vif, vif->wdev.iftype,
185 					     vif->wdev.use_4addr,
186 					     sa->sa_data);
187 
188 	if (ret)
189 		memcpy(ndev->dev_addr, old_addr, ETH_ALEN);
190 
191 	return ret;
192 }
193 
194 /* Network device ops handlers */
195 const struct net_device_ops qtnf_netdev_ops = {
196 	.ndo_open = qtnf_netdev_open,
197 	.ndo_stop = qtnf_netdev_close,
198 	.ndo_start_xmit = qtnf_netdev_hard_start_xmit,
199 	.ndo_tx_timeout = qtnf_netdev_tx_timeout,
200 	.ndo_get_stats64 = qtnf_netdev_get_stats64,
201 	.ndo_set_mac_address = qtnf_netdev_set_mac_address,
202 };
203 
204 static int qtnf_mac_init_single_band(struct wiphy *wiphy,
205 				     struct qtnf_wmac *mac,
206 				     enum nl80211_band band)
207 {
208 	int ret;
209 
210 	wiphy->bands[band] = kzalloc(sizeof(*wiphy->bands[band]), GFP_KERNEL);
211 	if (!wiphy->bands[band])
212 		return -ENOMEM;
213 
214 	wiphy->bands[band]->band = band;
215 
216 	ret = qtnf_cmd_band_info_get(mac, wiphy->bands[band]);
217 	if (ret) {
218 		pr_err("MAC%u: band %u: failed to get chans info: %d\n",
219 		       mac->macid, band, ret);
220 		return ret;
221 	}
222 
223 	qtnf_band_init_rates(wiphy->bands[band]);
224 
225 	return 0;
226 }
227 
228 static int qtnf_mac_init_bands(struct qtnf_wmac *mac)
229 {
230 	struct wiphy *wiphy = priv_to_wiphy(mac);
231 	int ret = 0;
232 
233 	if (mac->macinfo.bands_cap & QLINK_BAND_2GHZ) {
234 		ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_2GHZ);
235 		if (ret)
236 			goto out;
237 	}
238 
239 	if (mac->macinfo.bands_cap & QLINK_BAND_5GHZ) {
240 		ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_5GHZ);
241 		if (ret)
242 			goto out;
243 	}
244 
245 	if (mac->macinfo.bands_cap & QLINK_BAND_60GHZ)
246 		ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_60GHZ);
247 
248 out:
249 	return ret;
250 }
251 
252 struct qtnf_vif *qtnf_mac_get_free_vif(struct qtnf_wmac *mac)
253 {
254 	struct qtnf_vif *vif;
255 	int i;
256 
257 	for (i = 0; i < QTNF_MAX_INTF; i++) {
258 		vif = &mac->iflist[i];
259 		if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)
260 			return vif;
261 	}
262 
263 	return NULL;
264 }
265 
266 struct qtnf_vif *qtnf_mac_get_base_vif(struct qtnf_wmac *mac)
267 {
268 	struct qtnf_vif *vif;
269 
270 	vif = &mac->iflist[QTNF_PRIMARY_VIF_IDX];
271 
272 	if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)
273 		return NULL;
274 
275 	return vif;
276 }
277 
278 void qtnf_mac_iface_comb_free(struct qtnf_wmac *mac)
279 {
280 	struct ieee80211_iface_combination *comb;
281 	int i;
282 
283 	if (mac->macinfo.if_comb) {
284 		for (i = 0; i < mac->macinfo.n_if_comb; i++) {
285 			comb = &mac->macinfo.if_comb[i];
286 			kfree(comb->limits);
287 			comb->limits = NULL;
288 		}
289 
290 		kfree(mac->macinfo.if_comb);
291 		mac->macinfo.if_comb = NULL;
292 	}
293 }
294 
295 void qtnf_mac_ext_caps_free(struct qtnf_wmac *mac)
296 {
297 	if (mac->macinfo.extended_capabilities_len) {
298 		kfree(mac->macinfo.extended_capabilities);
299 		mac->macinfo.extended_capabilities = NULL;
300 
301 		kfree(mac->macinfo.extended_capabilities_mask);
302 		mac->macinfo.extended_capabilities_mask = NULL;
303 
304 		mac->macinfo.extended_capabilities_len = 0;
305 	}
306 }
307 
308 static void qtnf_vif_reset_handler(struct work_struct *work)
309 {
310 	struct qtnf_vif *vif = container_of(work, struct qtnf_vif, reset_work);
311 
312 	rtnl_lock();
313 
314 	if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED) {
315 		rtnl_unlock();
316 		return;
317 	}
318 
319 	/* stop tx completely */
320 	netif_tx_stop_all_queues(vif->netdev);
321 	if (netif_carrier_ok(vif->netdev))
322 		netif_carrier_off(vif->netdev);
323 
324 	qtnf_cfg80211_vif_reset(vif);
325 
326 	rtnl_unlock();
327 }
328 
329 static void qtnf_mac_init_primary_intf(struct qtnf_wmac *mac)
330 {
331 	struct qtnf_vif *vif = &mac->iflist[QTNF_PRIMARY_VIF_IDX];
332 
333 	vif->wdev.iftype = NL80211_IFTYPE_STATION;
334 	vif->bss_priority = QTNF_DEF_BSS_PRIORITY;
335 	vif->wdev.wiphy = priv_to_wiphy(mac);
336 	INIT_WORK(&vif->reset_work, qtnf_vif_reset_handler);
337 	vif->cons_tx_timeout_cnt = 0;
338 }
339 
340 static void qtnf_mac_scan_finish(struct qtnf_wmac *mac, bool aborted)
341 {
342 	struct cfg80211_scan_info info = {
343 		.aborted = aborted,
344 	};
345 
346 	mutex_lock(&mac->mac_lock);
347 
348 	if (mac->scan_req) {
349 		cfg80211_scan_done(mac->scan_req, &info);
350 		mac->scan_req = NULL;
351 	}
352 
353 	mutex_unlock(&mac->mac_lock);
354 }
355 
356 void qtnf_scan_done(struct qtnf_wmac *mac, bool aborted)
357 {
358 	cancel_delayed_work_sync(&mac->scan_timeout);
359 	qtnf_mac_scan_finish(mac, aborted);
360 }
361 
362 static void qtnf_mac_scan_timeout(struct work_struct *work)
363 {
364 	struct qtnf_wmac *mac =
365 		container_of(work, struct qtnf_wmac, scan_timeout.work);
366 
367 	pr_warn("MAC%d: scan timed out\n", mac->macid);
368 	qtnf_mac_scan_finish(mac, true);
369 }
370 
371 static void qtnf_vif_send_data_high_pri(struct work_struct *work)
372 {
373 	struct qtnf_vif *vif =
374 		container_of(work, struct qtnf_vif, high_pri_tx_work);
375 	struct sk_buff *skb;
376 
377 	if (!vif->netdev ||
378 	    vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)
379 		return;
380 
381 	while ((skb = skb_dequeue(&vif->high_pri_tx_queue))) {
382 		qtnf_cmd_send_frame(vif, 0, QLINK_FRAME_TX_FLAG_8023,
383 				    0, skb->data, skb->len);
384 		dev_kfree_skb_any(skb);
385 	}
386 }
387 
388 static struct qtnf_wmac *qtnf_core_mac_alloc(struct qtnf_bus *bus,
389 					     unsigned int macid)
390 {
391 	struct qtnf_vif *vif;
392 	struct wiphy *wiphy;
393 	struct qtnf_wmac *mac;
394 	unsigned int i;
395 
396 	wiphy = qtnf_wiphy_allocate(bus);
397 	if (!wiphy)
398 		return ERR_PTR(-ENOMEM);
399 
400 	mac = wiphy_priv(wiphy);
401 
402 	mac->macid = macid;
403 	mac->bus = bus;
404 	mutex_init(&mac->mac_lock);
405 	INIT_DELAYED_WORK(&mac->scan_timeout, qtnf_mac_scan_timeout);
406 
407 	for (i = 0; i < QTNF_MAX_INTF; i++) {
408 		vif = &mac->iflist[i];
409 
410 		memset(vif, 0, sizeof(*vif));
411 		vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
412 		vif->mac = mac;
413 		vif->vifid = i;
414 		qtnf_sta_list_init(&vif->sta_list);
415 		INIT_WORK(&vif->high_pri_tx_work, qtnf_vif_send_data_high_pri);
416 		skb_queue_head_init(&vif->high_pri_tx_queue);
417 		vif->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
418 		if (!vif->stats64)
419 			pr_warn("VIF%u.%u: per cpu stats allocation failed\n",
420 				macid, i);
421 	}
422 
423 	qtnf_mac_init_primary_intf(mac);
424 	bus->mac[macid] = mac;
425 
426 	return mac;
427 }
428 
429 static const struct ethtool_ops qtnf_ethtool_ops = {
430 	.get_drvinfo = cfg80211_get_drvinfo,
431 };
432 
433 int qtnf_core_net_attach(struct qtnf_wmac *mac, struct qtnf_vif *vif,
434 			 const char *name, unsigned char name_assign_type)
435 {
436 	struct wiphy *wiphy = priv_to_wiphy(mac);
437 	struct net_device *dev;
438 	void *qdev_vif;
439 	int ret;
440 
441 	dev = alloc_netdev_mqs(sizeof(struct qtnf_vif *), name,
442 			       name_assign_type, ether_setup, 1, 1);
443 	if (!dev) {
444 		vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
445 		return -ENOMEM;
446 	}
447 
448 	vif->netdev = dev;
449 
450 	dev->netdev_ops = &qtnf_netdev_ops;
451 	dev->needs_free_netdev = true;
452 	dev_net_set(dev, wiphy_net(wiphy));
453 	dev->ieee80211_ptr = &vif->wdev;
454 	ether_addr_copy(dev->dev_addr, vif->mac_addr);
455 	SET_NETDEV_DEV(dev, wiphy_dev(wiphy));
456 	dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
457 	dev->watchdog_timeo = QTNF_DEF_WDOG_TIMEOUT;
458 	dev->tx_queue_len = 100;
459 	dev->ethtool_ops = &qtnf_ethtool_ops;
460 
461 	qdev_vif = netdev_priv(dev);
462 	*((void **)qdev_vif) = vif;
463 
464 	SET_NETDEV_DEV(dev, mac->bus->dev);
465 
466 	ret = register_netdevice(dev);
467 	if (ret) {
468 		free_netdev(dev);
469 		vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
470 	}
471 
472 	return ret;
473 }
474 
475 static void qtnf_core_mac_detach(struct qtnf_bus *bus, unsigned int macid)
476 {
477 	struct qtnf_wmac *mac;
478 	struct wiphy *wiphy;
479 	struct qtnf_vif *vif;
480 	unsigned int i;
481 	enum nl80211_band band;
482 
483 	mac = bus->mac[macid];
484 
485 	if (!mac)
486 		return;
487 
488 	wiphy = priv_to_wiphy(mac);
489 
490 	for (i = 0; i < QTNF_MAX_INTF; i++) {
491 		vif = &mac->iflist[i];
492 		rtnl_lock();
493 		if (vif->netdev &&
494 		    vif->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED) {
495 			qtnf_virtual_intf_cleanup(vif->netdev);
496 			qtnf_del_virtual_intf(wiphy, &vif->wdev);
497 		}
498 		rtnl_unlock();
499 		qtnf_sta_list_free(&vif->sta_list);
500 		free_percpu(vif->stats64);
501 	}
502 
503 	if (mac->wiphy_registered)
504 		wiphy_unregister(wiphy);
505 
506 	for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; ++band) {
507 		if (!wiphy->bands[band])
508 			continue;
509 
510 		kfree(wiphy->bands[band]->channels);
511 		wiphy->bands[band]->n_channels = 0;
512 
513 		kfree(wiphy->bands[band]);
514 		wiphy->bands[band] = NULL;
515 	}
516 
517 	qtnf_mac_iface_comb_free(mac);
518 	qtnf_mac_ext_caps_free(mac);
519 	kfree(mac->macinfo.wowlan);
520 	kfree(mac->rd);
521 	mac->rd = NULL;
522 	wiphy_free(wiphy);
523 	bus->mac[macid] = NULL;
524 }
525 
526 static int qtnf_core_mac_attach(struct qtnf_bus *bus, unsigned int macid)
527 {
528 	struct qtnf_wmac *mac;
529 	struct qtnf_vif *vif;
530 	int ret;
531 
532 	if (!(bus->hw_info.mac_bitmap & BIT(macid))) {
533 		pr_info("MAC%u is not active in FW\n", macid);
534 		return 0;
535 	}
536 
537 	mac = qtnf_core_mac_alloc(bus, macid);
538 	if (IS_ERR(mac)) {
539 		pr_err("MAC%u allocation failed\n", macid);
540 		return PTR_ERR(mac);
541 	}
542 
543 	ret = qtnf_cmd_get_mac_info(mac);
544 	if (ret) {
545 		pr_err("MAC%u: failed to get info\n", macid);
546 		goto error;
547 	}
548 
549 	vif = qtnf_mac_get_base_vif(mac);
550 	if (!vif) {
551 		pr_err("MAC%u: primary VIF is not ready\n", macid);
552 		ret = -EFAULT;
553 		goto error;
554 	}
555 
556 	ret = qtnf_cmd_send_add_intf(vif, vif->wdev.iftype,
557 				     vif->wdev.use_4addr, vif->mac_addr);
558 	if (ret) {
559 		pr_err("MAC%u: failed to add VIF\n", macid);
560 		goto error;
561 	}
562 
563 	ret = qtnf_cmd_send_get_phy_params(mac);
564 	if (ret) {
565 		pr_err("MAC%u: failed to get PHY settings\n", macid);
566 		goto error;
567 	}
568 
569 	ret = qtnf_mac_init_bands(mac);
570 	if (ret) {
571 		pr_err("MAC%u: failed to init bands\n", macid);
572 		goto error;
573 	}
574 
575 	ret = qtnf_wiphy_register(&bus->hw_info, mac);
576 	if (ret) {
577 		pr_err("MAC%u: wiphy registration failed\n", macid);
578 		goto error;
579 	}
580 
581 	mac->wiphy_registered = 1;
582 
583 	rtnl_lock();
584 
585 	ret = qtnf_core_net_attach(mac, vif, "wlan%d", NET_NAME_ENUM);
586 	rtnl_unlock();
587 
588 	if (ret) {
589 		pr_err("MAC%u: failed to attach netdev\n", macid);
590 		vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
591 		vif->netdev = NULL;
592 		goto error;
593 	}
594 
595 	pr_debug("MAC%u initialized\n", macid);
596 
597 	return 0;
598 
599 error:
600 	qtnf_core_mac_detach(bus, macid);
601 	return ret;
602 }
603 
604 int qtnf_core_attach(struct qtnf_bus *bus)
605 {
606 	unsigned int i;
607 	int ret;
608 
609 	qtnf_trans_init(bus);
610 	qtnf_bus_data_rx_start(bus);
611 
612 	bus->workqueue = alloc_ordered_workqueue("QTNF_BUS", 0);
613 	if (!bus->workqueue) {
614 		pr_err("failed to alloc main workqueue\n");
615 		ret = -ENOMEM;
616 		goto error;
617 	}
618 
619 	bus->hprio_workqueue = alloc_workqueue("QTNF_HPRI", WQ_HIGHPRI, 0);
620 	if (!bus->hprio_workqueue) {
621 		pr_err("failed to alloc high prio workqueue\n");
622 		ret = -ENOMEM;
623 		goto error;
624 	}
625 
626 	INIT_WORK(&bus->event_work, qtnf_event_work_handler);
627 
628 	ret = qtnf_cmd_send_init_fw(bus);
629 	if (ret) {
630 		pr_err("failed to init FW: %d\n", ret);
631 		goto error;
632 	}
633 
634 	bus->fw_state = QTNF_FW_STATE_ACTIVE;
635 	ret = qtnf_cmd_get_hw_info(bus);
636 	if (ret) {
637 		pr_err("failed to get HW info: %d\n", ret);
638 		goto error;
639 	}
640 
641 	if (bus->hw_info.ql_proto_ver != QLINK_PROTO_VER) {
642 		pr_err("qlink version mismatch %u != %u\n",
643 		       QLINK_PROTO_VER, bus->hw_info.ql_proto_ver);
644 		ret = -EPROTONOSUPPORT;
645 		goto error;
646 	}
647 
648 	if (bus->hw_info.num_mac > QTNF_MAX_MAC) {
649 		pr_err("no support for number of MACs=%u\n",
650 		       bus->hw_info.num_mac);
651 		ret = -ERANGE;
652 		goto error;
653 	}
654 
655 	for (i = 0; i < bus->hw_info.num_mac; i++) {
656 		ret = qtnf_core_mac_attach(bus, i);
657 
658 		if (ret) {
659 			pr_err("MAC%u: attach failed: %d\n", i, ret);
660 			goto error;
661 		}
662 	}
663 
664 	bus->fw_state = QTNF_FW_STATE_RUNNING;
665 	return 0;
666 
667 error:
668 	qtnf_core_detach(bus);
669 	return ret;
670 }
671 EXPORT_SYMBOL_GPL(qtnf_core_attach);
672 
673 void qtnf_core_detach(struct qtnf_bus *bus)
674 {
675 	unsigned int macid;
676 
677 	qtnf_bus_data_rx_stop(bus);
678 
679 	for (macid = 0; macid < QTNF_MAX_MAC; macid++)
680 		qtnf_core_mac_detach(bus, macid);
681 
682 	if (qtnf_fw_is_up(bus))
683 		qtnf_cmd_send_deinit_fw(bus);
684 
685 	bus->fw_state = QTNF_FW_STATE_DETACHED;
686 
687 	if (bus->workqueue) {
688 		flush_workqueue(bus->workqueue);
689 		destroy_workqueue(bus->workqueue);
690 		bus->workqueue = NULL;
691 	}
692 
693 	if (bus->hprio_workqueue) {
694 		flush_workqueue(bus->hprio_workqueue);
695 		destroy_workqueue(bus->hprio_workqueue);
696 		bus->hprio_workqueue = NULL;
697 	}
698 
699 	qtnf_trans_free(bus);
700 }
701 EXPORT_SYMBOL_GPL(qtnf_core_detach);
702 
703 static inline int qtnf_is_frame_meta_magic_valid(struct qtnf_frame_meta_info *m)
704 {
705 	return m->magic_s == 0xAB && m->magic_e == 0xBA;
706 }
707 
708 struct net_device *qtnf_classify_skb(struct qtnf_bus *bus, struct sk_buff *skb)
709 {
710 	struct qtnf_frame_meta_info *meta;
711 	struct net_device *ndev = NULL;
712 	struct qtnf_wmac *mac;
713 	struct qtnf_vif *vif;
714 
715 	if (unlikely(bus->fw_state != QTNF_FW_STATE_RUNNING))
716 		return NULL;
717 
718 	meta = (struct qtnf_frame_meta_info *)
719 		(skb_tail_pointer(skb) - sizeof(*meta));
720 
721 	if (unlikely(!qtnf_is_frame_meta_magic_valid(meta))) {
722 		pr_err_ratelimited("invalid magic 0x%x:0x%x\n",
723 				   meta->magic_s, meta->magic_e);
724 		goto out;
725 	}
726 
727 	if (unlikely(meta->macid >= QTNF_MAX_MAC)) {
728 		pr_err_ratelimited("invalid mac(%u)\n", meta->macid);
729 		goto out;
730 	}
731 
732 	if (unlikely(meta->ifidx >= QTNF_MAX_INTF)) {
733 		pr_err_ratelimited("invalid vif(%u)\n", meta->ifidx);
734 		goto out;
735 	}
736 
737 	mac = bus->mac[meta->macid];
738 
739 	if (unlikely(!mac)) {
740 		pr_err_ratelimited("mac(%d) does not exist\n", meta->macid);
741 		goto out;
742 	}
743 
744 	vif = &mac->iflist[meta->ifidx];
745 
746 	if (unlikely(vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)) {
747 		pr_err_ratelimited("vif(%u) does not exists\n", meta->ifidx);
748 		goto out;
749 	}
750 
751 	ndev = vif->netdev;
752 
753 	if (unlikely(!ndev)) {
754 		pr_err_ratelimited("netdev for wlan%u.%u does not exists\n",
755 				   meta->macid, meta->ifidx);
756 		goto out;
757 	}
758 
759 	__skb_trim(skb, skb->len - sizeof(*meta));
760 
761 out:
762 	return ndev;
763 }
764 EXPORT_SYMBOL_GPL(qtnf_classify_skb);
765 
766 void qtnf_wake_all_queues(struct net_device *ndev)
767 {
768 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
769 	struct qtnf_wmac *mac;
770 	struct qtnf_bus *bus;
771 	int macid;
772 	int i;
773 
774 	if (unlikely(!vif || !vif->mac || !vif->mac->bus))
775 		return;
776 
777 	bus = vif->mac->bus;
778 
779 	for (macid = 0; macid < QTNF_MAX_MAC; macid++) {
780 		if (!(bus->hw_info.mac_bitmap & BIT(macid)))
781 			continue;
782 
783 		mac = bus->mac[macid];
784 		for (i = 0; i < QTNF_MAX_INTF; i++) {
785 			vif = &mac->iflist[i];
786 			if (vif->netdev && netif_queue_stopped(vif->netdev))
787 				netif_tx_wake_all_queues(vif->netdev);
788 		}
789 	}
790 }
791 EXPORT_SYMBOL_GPL(qtnf_wake_all_queues);
792 
793 void qtnf_update_rx_stats(struct net_device *ndev, const struct sk_buff *skb)
794 {
795 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
796 	struct pcpu_sw_netstats *stats64;
797 
798 	if (unlikely(!vif || !vif->stats64)) {
799 		ndev->stats.rx_packets++;
800 		ndev->stats.rx_bytes += skb->len;
801 		return;
802 	}
803 
804 	stats64 = this_cpu_ptr(vif->stats64);
805 
806 	u64_stats_update_begin(&stats64->syncp);
807 	stats64->rx_packets++;
808 	stats64->rx_bytes += skb->len;
809 	u64_stats_update_end(&stats64->syncp);
810 }
811 EXPORT_SYMBOL_GPL(qtnf_update_rx_stats);
812 
813 void qtnf_update_tx_stats(struct net_device *ndev, const struct sk_buff *skb)
814 {
815 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
816 	struct pcpu_sw_netstats *stats64;
817 
818 	if (unlikely(!vif || !vif->stats64)) {
819 		ndev->stats.tx_packets++;
820 		ndev->stats.tx_bytes += skb->len;
821 		return;
822 	}
823 
824 	stats64 = this_cpu_ptr(vif->stats64);
825 
826 	u64_stats_update_begin(&stats64->syncp);
827 	stats64->tx_packets++;
828 	stats64->tx_bytes += skb->len;
829 	u64_stats_update_end(&stats64->syncp);
830 }
831 EXPORT_SYMBOL_GPL(qtnf_update_tx_stats);
832 
833 void qtnf_packet_send_hi_pri(struct sk_buff *skb)
834 {
835 	struct qtnf_vif *vif = qtnf_netdev_get_priv(skb->dev);
836 
837 	skb_queue_tail(&vif->high_pri_tx_queue, skb);
838 	queue_work(vif->mac->bus->hprio_workqueue, &vif->high_pri_tx_work);
839 }
840 EXPORT_SYMBOL_GPL(qtnf_packet_send_hi_pri);
841 
842 MODULE_AUTHOR("Quantenna Communications");
843 MODULE_DESCRIPTION("Quantenna 802.11 wireless LAN FullMAC driver.");
844 MODULE_LICENSE("GPL");
845