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 struct qtnf_wmac *qtnf_core_mac_alloc(struct qtnf_bus *bus,
372 					     unsigned int macid)
373 {
374 	struct qtnf_vif *vif;
375 	struct wiphy *wiphy;
376 	struct qtnf_wmac *mac;
377 	unsigned int i;
378 
379 	wiphy = qtnf_wiphy_allocate(bus);
380 	if (!wiphy)
381 		return ERR_PTR(-ENOMEM);
382 
383 	mac = wiphy_priv(wiphy);
384 
385 	mac->macid = macid;
386 	mac->bus = bus;
387 	mutex_init(&mac->mac_lock);
388 	INIT_DELAYED_WORK(&mac->scan_timeout, qtnf_mac_scan_timeout);
389 
390 	for (i = 0; i < QTNF_MAX_INTF; i++) {
391 		vif = &mac->iflist[i];
392 
393 		memset(vif, 0, sizeof(*vif));
394 		vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
395 		vif->mac = mac;
396 		vif->vifid = i;
397 		qtnf_sta_list_init(&vif->sta_list);
398 
399 		vif->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
400 		if (!vif->stats64)
401 			pr_warn("VIF%u.%u: per cpu stats allocation failed\n",
402 				macid, i);
403 	}
404 
405 	qtnf_mac_init_primary_intf(mac);
406 	bus->mac[macid] = mac;
407 
408 	return mac;
409 }
410 
411 static const struct ethtool_ops qtnf_ethtool_ops = {
412 	.get_drvinfo = cfg80211_get_drvinfo,
413 };
414 
415 int qtnf_core_net_attach(struct qtnf_wmac *mac, struct qtnf_vif *vif,
416 			 const char *name, unsigned char name_assign_type)
417 {
418 	struct wiphy *wiphy = priv_to_wiphy(mac);
419 	struct net_device *dev;
420 	void *qdev_vif;
421 	int ret;
422 
423 	dev = alloc_netdev_mqs(sizeof(struct qtnf_vif *), name,
424 			       name_assign_type, ether_setup, 1, 1);
425 	if (!dev) {
426 		vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
427 		return -ENOMEM;
428 	}
429 
430 	vif->netdev = dev;
431 
432 	dev->netdev_ops = &qtnf_netdev_ops;
433 	dev->needs_free_netdev = true;
434 	dev_net_set(dev, wiphy_net(wiphy));
435 	dev->ieee80211_ptr = &vif->wdev;
436 	ether_addr_copy(dev->dev_addr, vif->mac_addr);
437 	SET_NETDEV_DEV(dev, wiphy_dev(wiphy));
438 	dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
439 	dev->watchdog_timeo = QTNF_DEF_WDOG_TIMEOUT;
440 	dev->tx_queue_len = 100;
441 	dev->ethtool_ops = &qtnf_ethtool_ops;
442 
443 	qdev_vif = netdev_priv(dev);
444 	*((void **)qdev_vif) = vif;
445 
446 	SET_NETDEV_DEV(dev, mac->bus->dev);
447 
448 	ret = register_netdevice(dev);
449 	if (ret) {
450 		free_netdev(dev);
451 		vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
452 	}
453 
454 	return ret;
455 }
456 
457 static void qtnf_core_mac_detach(struct qtnf_bus *bus, unsigned int macid)
458 {
459 	struct qtnf_wmac *mac;
460 	struct wiphy *wiphy;
461 	struct qtnf_vif *vif;
462 	unsigned int i;
463 	enum nl80211_band band;
464 
465 	mac = bus->mac[macid];
466 
467 	if (!mac)
468 		return;
469 
470 	wiphy = priv_to_wiphy(mac);
471 
472 	for (i = 0; i < QTNF_MAX_INTF; i++) {
473 		vif = &mac->iflist[i];
474 		rtnl_lock();
475 		if (vif->netdev &&
476 		    vif->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED) {
477 			qtnf_virtual_intf_cleanup(vif->netdev);
478 			qtnf_del_virtual_intf(wiphy, &vif->wdev);
479 		}
480 		rtnl_unlock();
481 		qtnf_sta_list_free(&vif->sta_list);
482 		free_percpu(vif->stats64);
483 	}
484 
485 	if (mac->wiphy_registered)
486 		wiphy_unregister(wiphy);
487 
488 	for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; ++band) {
489 		if (!wiphy->bands[band])
490 			continue;
491 
492 		kfree(wiphy->bands[band]->channels);
493 		wiphy->bands[band]->n_channels = 0;
494 
495 		kfree(wiphy->bands[band]);
496 		wiphy->bands[band] = NULL;
497 	}
498 
499 	qtnf_mac_iface_comb_free(mac);
500 	qtnf_mac_ext_caps_free(mac);
501 	kfree(mac->macinfo.wowlan);
502 	wiphy_free(wiphy);
503 	bus->mac[macid] = NULL;
504 }
505 
506 static int qtnf_core_mac_attach(struct qtnf_bus *bus, unsigned int macid)
507 {
508 	struct qtnf_wmac *mac;
509 	struct qtnf_vif *vif;
510 	int ret;
511 
512 	if (!(bus->hw_info.mac_bitmap & BIT(macid))) {
513 		pr_info("MAC%u is not active in FW\n", macid);
514 		return 0;
515 	}
516 
517 	mac = qtnf_core_mac_alloc(bus, macid);
518 	if (IS_ERR(mac)) {
519 		pr_err("MAC%u allocation failed\n", macid);
520 		return PTR_ERR(mac);
521 	}
522 
523 	ret = qtnf_cmd_get_mac_info(mac);
524 	if (ret) {
525 		pr_err("MAC%u: failed to get info\n", macid);
526 		goto error;
527 	}
528 
529 	vif = qtnf_mac_get_base_vif(mac);
530 	if (!vif) {
531 		pr_err("MAC%u: primary VIF is not ready\n", macid);
532 		ret = -EFAULT;
533 		goto error;
534 	}
535 
536 	ret = qtnf_cmd_send_add_intf(vif, vif->wdev.iftype,
537 				     vif->wdev.use_4addr, vif->mac_addr);
538 	if (ret) {
539 		pr_err("MAC%u: failed to add VIF\n", macid);
540 		goto error;
541 	}
542 
543 	ret = qtnf_cmd_send_get_phy_params(mac);
544 	if (ret) {
545 		pr_err("MAC%u: failed to get PHY settings\n", macid);
546 		goto error;
547 	}
548 
549 	ret = qtnf_mac_init_bands(mac);
550 	if (ret) {
551 		pr_err("MAC%u: failed to init bands\n", macid);
552 		goto error;
553 	}
554 
555 	ret = qtnf_wiphy_register(&bus->hw_info, mac);
556 	if (ret) {
557 		pr_err("MAC%u: wiphy registration failed\n", macid);
558 		goto error;
559 	}
560 
561 	mac->wiphy_registered = 1;
562 
563 	rtnl_lock();
564 
565 	ret = qtnf_core_net_attach(mac, vif, "wlan%d", NET_NAME_ENUM);
566 	rtnl_unlock();
567 
568 	if (ret) {
569 		pr_err("MAC%u: failed to attach netdev\n", macid);
570 		vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
571 		vif->netdev = NULL;
572 		goto error;
573 	}
574 
575 	pr_debug("MAC%u initialized\n", macid);
576 
577 	return 0;
578 
579 error:
580 	qtnf_core_mac_detach(bus, macid);
581 	return ret;
582 }
583 
584 int qtnf_core_attach(struct qtnf_bus *bus)
585 {
586 	unsigned int i;
587 	int ret;
588 
589 	qtnf_trans_init(bus);
590 
591 	bus->fw_state = QTNF_FW_STATE_BOOT_DONE;
592 	qtnf_bus_data_rx_start(bus);
593 
594 	bus->workqueue = alloc_ordered_workqueue("QTNF_BUS", 0);
595 	if (!bus->workqueue) {
596 		pr_err("failed to alloc main workqueue\n");
597 		ret = -ENOMEM;
598 		goto error;
599 	}
600 
601 	INIT_WORK(&bus->event_work, qtnf_event_work_handler);
602 
603 	ret = qtnf_cmd_send_init_fw(bus);
604 	if (ret) {
605 		pr_err("failed to init FW: %d\n", ret);
606 		goto error;
607 	}
608 
609 	bus->fw_state = QTNF_FW_STATE_ACTIVE;
610 
611 	ret = qtnf_cmd_get_hw_info(bus);
612 	if (ret) {
613 		pr_err("failed to get HW info: %d\n", ret);
614 		goto error;
615 	}
616 
617 	if (bus->hw_info.ql_proto_ver != QLINK_PROTO_VER) {
618 		pr_err("qlink version mismatch %u != %u\n",
619 		       QLINK_PROTO_VER, bus->hw_info.ql_proto_ver);
620 		ret = -EPROTONOSUPPORT;
621 		goto error;
622 	}
623 
624 	if (bus->hw_info.num_mac > QTNF_MAX_MAC) {
625 		pr_err("no support for number of MACs=%u\n",
626 		       bus->hw_info.num_mac);
627 		ret = -ERANGE;
628 		goto error;
629 	}
630 
631 	for (i = 0; i < bus->hw_info.num_mac; i++) {
632 		ret = qtnf_core_mac_attach(bus, i);
633 
634 		if (ret) {
635 			pr_err("MAC%u: attach failed: %d\n", i, ret);
636 			goto error;
637 		}
638 	}
639 
640 	return 0;
641 
642 error:
643 	qtnf_core_detach(bus);
644 
645 	return ret;
646 }
647 EXPORT_SYMBOL_GPL(qtnf_core_attach);
648 
649 void qtnf_core_detach(struct qtnf_bus *bus)
650 {
651 	unsigned int macid;
652 
653 	qtnf_bus_data_rx_stop(bus);
654 
655 	for (macid = 0; macid < QTNF_MAX_MAC; macid++)
656 		qtnf_core_mac_detach(bus, macid);
657 
658 	if (bus->fw_state == QTNF_FW_STATE_ACTIVE)
659 		qtnf_cmd_send_deinit_fw(bus);
660 
661 	bus->fw_state = QTNF_FW_STATE_DETACHED;
662 
663 	if (bus->workqueue) {
664 		flush_workqueue(bus->workqueue);
665 		destroy_workqueue(bus->workqueue);
666 	}
667 
668 	kfree(bus->hw_info.rd);
669 	bus->hw_info.rd = NULL;
670 
671 	qtnf_trans_free(bus);
672 }
673 EXPORT_SYMBOL_GPL(qtnf_core_detach);
674 
675 static inline int qtnf_is_frame_meta_magic_valid(struct qtnf_frame_meta_info *m)
676 {
677 	return m->magic_s == 0xAB && m->magic_e == 0xBA;
678 }
679 
680 struct net_device *qtnf_classify_skb(struct qtnf_bus *bus, struct sk_buff *skb)
681 {
682 	struct qtnf_frame_meta_info *meta;
683 	struct net_device *ndev = NULL;
684 	struct qtnf_wmac *mac;
685 	struct qtnf_vif *vif;
686 
687 	meta = (struct qtnf_frame_meta_info *)
688 		(skb_tail_pointer(skb) - sizeof(*meta));
689 
690 	if (unlikely(!qtnf_is_frame_meta_magic_valid(meta))) {
691 		pr_err_ratelimited("invalid magic 0x%x:0x%x\n",
692 				   meta->magic_s, meta->magic_e);
693 		goto out;
694 	}
695 
696 	if (unlikely(meta->macid >= QTNF_MAX_MAC)) {
697 		pr_err_ratelimited("invalid mac(%u)\n", meta->macid);
698 		goto out;
699 	}
700 
701 	if (unlikely(meta->ifidx >= QTNF_MAX_INTF)) {
702 		pr_err_ratelimited("invalid vif(%u)\n", meta->ifidx);
703 		goto out;
704 	}
705 
706 	mac = bus->mac[meta->macid];
707 
708 	if (unlikely(!mac)) {
709 		pr_err_ratelimited("mac(%d) does not exist\n", meta->macid);
710 		goto out;
711 	}
712 
713 	vif = &mac->iflist[meta->ifidx];
714 
715 	if (unlikely(vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)) {
716 		pr_err_ratelimited("vif(%u) does not exists\n", meta->ifidx);
717 		goto out;
718 	}
719 
720 	ndev = vif->netdev;
721 
722 	if (unlikely(!ndev)) {
723 		pr_err_ratelimited("netdev for wlan%u.%u does not exists\n",
724 				   meta->macid, meta->ifidx);
725 		goto out;
726 	}
727 
728 	__skb_trim(skb, skb->len - sizeof(*meta));
729 
730 out:
731 	return ndev;
732 }
733 EXPORT_SYMBOL_GPL(qtnf_classify_skb);
734 
735 void qtnf_wake_all_queues(struct net_device *ndev)
736 {
737 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
738 	struct qtnf_wmac *mac;
739 	struct qtnf_bus *bus;
740 	int macid;
741 	int i;
742 
743 	if (unlikely(!vif || !vif->mac || !vif->mac->bus))
744 		return;
745 
746 	bus = vif->mac->bus;
747 
748 	for (macid = 0; macid < QTNF_MAX_MAC; macid++) {
749 		if (!(bus->hw_info.mac_bitmap & BIT(macid)))
750 			continue;
751 
752 		mac = bus->mac[macid];
753 		for (i = 0; i < QTNF_MAX_INTF; i++) {
754 			vif = &mac->iflist[i];
755 			if (vif->netdev && netif_queue_stopped(vif->netdev))
756 				netif_tx_wake_all_queues(vif->netdev);
757 		}
758 	}
759 }
760 EXPORT_SYMBOL_GPL(qtnf_wake_all_queues);
761 
762 void qtnf_update_rx_stats(struct net_device *ndev, const struct sk_buff *skb)
763 {
764 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
765 	struct pcpu_sw_netstats *stats64;
766 
767 	if (unlikely(!vif || !vif->stats64)) {
768 		ndev->stats.rx_packets++;
769 		ndev->stats.rx_bytes += skb->len;
770 		return;
771 	}
772 
773 	stats64 = this_cpu_ptr(vif->stats64);
774 
775 	u64_stats_update_begin(&stats64->syncp);
776 	stats64->rx_packets++;
777 	stats64->rx_bytes += skb->len;
778 	u64_stats_update_end(&stats64->syncp);
779 }
780 EXPORT_SYMBOL_GPL(qtnf_update_rx_stats);
781 
782 void qtnf_update_tx_stats(struct net_device *ndev, const struct sk_buff *skb)
783 {
784 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
785 	struct pcpu_sw_netstats *stats64;
786 
787 	if (unlikely(!vif || !vif->stats64)) {
788 		ndev->stats.tx_packets++;
789 		ndev->stats.tx_bytes += skb->len;
790 		return;
791 	}
792 
793 	stats64 = this_cpu_ptr(vif->stats64);
794 
795 	u64_stats_update_begin(&stats64->syncp);
796 	stats64->tx_packets++;
797 	stats64->tx_bytes += skb->len;
798 	u64_stats_update_end(&stats64->syncp);
799 }
800 EXPORT_SYMBOL_GPL(qtnf_update_tx_stats);
801 
802 MODULE_AUTHOR("Quantenna Communications");
803 MODULE_DESCRIPTION("Quantenna 802.11 wireless LAN FullMAC driver.");
804 MODULE_LICENSE("GPL");
805