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