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 netdev_tx_t
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 static int qtnf_netdev_set_mac_address(struct net_device *ndev, void *addr)
183 {
184 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
185 	struct sockaddr *sa = addr;
186 	int ret;
187 	unsigned char old_addr[ETH_ALEN];
188 
189 	memcpy(old_addr, sa->sa_data, sizeof(old_addr));
190 
191 	ret = eth_mac_addr(ndev, sa);
192 	if (ret)
193 		return ret;
194 
195 	qtnf_scan_done(vif->mac, true);
196 
197 	ret = qtnf_cmd_send_change_intf_type(vif, vif->wdev.iftype,
198 					     sa->sa_data);
199 
200 	if (ret)
201 		memcpy(ndev->dev_addr, old_addr, ETH_ALEN);
202 
203 	return ret;
204 }
205 
206 /* Network device ops handlers */
207 const struct net_device_ops qtnf_netdev_ops = {
208 	.ndo_open = qtnf_netdev_open,
209 	.ndo_stop = qtnf_netdev_close,
210 	.ndo_start_xmit = qtnf_netdev_hard_start_xmit,
211 	.ndo_tx_timeout = qtnf_netdev_tx_timeout,
212 	.ndo_get_stats64 = qtnf_netdev_get_stats64,
213 	.ndo_set_mac_address = qtnf_netdev_set_mac_address,
214 };
215 
216 static int qtnf_mac_init_single_band(struct wiphy *wiphy,
217 				     struct qtnf_wmac *mac,
218 				     enum nl80211_band band)
219 {
220 	int ret;
221 
222 	wiphy->bands[band] = kzalloc(sizeof(*wiphy->bands[band]), GFP_KERNEL);
223 	if (!wiphy->bands[band])
224 		return -ENOMEM;
225 
226 	wiphy->bands[band]->band = band;
227 
228 	ret = qtnf_cmd_band_info_get(mac, wiphy->bands[band]);
229 	if (ret) {
230 		pr_err("MAC%u: band %u: failed to get chans info: %d\n",
231 		       mac->macid, band, ret);
232 		return ret;
233 	}
234 
235 	qtnf_band_init_rates(wiphy->bands[band]);
236 
237 	return 0;
238 }
239 
240 static int qtnf_mac_init_bands(struct qtnf_wmac *mac)
241 {
242 	struct wiphy *wiphy = priv_to_wiphy(mac);
243 	int ret = 0;
244 
245 	if (mac->macinfo.bands_cap & QLINK_BAND_2GHZ) {
246 		ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_2GHZ);
247 		if (ret)
248 			goto out;
249 	}
250 
251 	if (mac->macinfo.bands_cap & QLINK_BAND_5GHZ) {
252 		ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_5GHZ);
253 		if (ret)
254 			goto out;
255 	}
256 
257 	if (mac->macinfo.bands_cap & QLINK_BAND_60GHZ)
258 		ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_60GHZ);
259 
260 out:
261 	return ret;
262 }
263 
264 struct qtnf_vif *qtnf_mac_get_free_vif(struct qtnf_wmac *mac)
265 {
266 	struct qtnf_vif *vif;
267 	int i;
268 
269 	for (i = 0; i < QTNF_MAX_INTF; i++) {
270 		vif = &mac->iflist[i];
271 		if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)
272 			return vif;
273 	}
274 
275 	return NULL;
276 }
277 
278 struct qtnf_vif *qtnf_mac_get_base_vif(struct qtnf_wmac *mac)
279 {
280 	struct qtnf_vif *vif;
281 
282 	vif = &mac->iflist[QTNF_PRIMARY_VIF_IDX];
283 
284 	if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)
285 		return NULL;
286 
287 	return vif;
288 }
289 
290 void qtnf_mac_iface_comb_free(struct qtnf_wmac *mac)
291 {
292 	struct ieee80211_iface_combination *comb;
293 	int i;
294 
295 	if (mac->macinfo.if_comb) {
296 		for (i = 0; i < mac->macinfo.n_if_comb; i++) {
297 			comb = &mac->macinfo.if_comb[i];
298 			kfree(comb->limits);
299 			comb->limits = NULL;
300 		}
301 
302 		kfree(mac->macinfo.if_comb);
303 		mac->macinfo.if_comb = NULL;
304 	}
305 }
306 
307 static void qtnf_vif_reset_handler(struct work_struct *work)
308 {
309 	struct qtnf_vif *vif = container_of(work, struct qtnf_vif, reset_work);
310 
311 	rtnl_lock();
312 
313 	if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED) {
314 		rtnl_unlock();
315 		return;
316 	}
317 
318 	/* stop tx completely */
319 	netif_tx_stop_all_queues(vif->netdev);
320 	if (netif_carrier_ok(vif->netdev))
321 		netif_carrier_off(vif->netdev);
322 
323 	qtnf_cfg80211_vif_reset(vif);
324 
325 	rtnl_unlock();
326 }
327 
328 static void qtnf_mac_init_primary_intf(struct qtnf_wmac *mac)
329 {
330 	struct qtnf_vif *vif = &mac->iflist[QTNF_PRIMARY_VIF_IDX];
331 
332 	vif->wdev.iftype = NL80211_IFTYPE_STATION;
333 	vif->bss_priority = QTNF_DEF_BSS_PRIORITY;
334 	vif->wdev.wiphy = priv_to_wiphy(mac);
335 	INIT_WORK(&vif->reset_work, qtnf_vif_reset_handler);
336 	vif->cons_tx_timeout_cnt = 0;
337 }
338 
339 static void qtnf_mac_scan_finish(struct qtnf_wmac *mac, bool aborted)
340 {
341 	struct cfg80211_scan_info info = {
342 		.aborted = aborted,
343 	};
344 
345 	mutex_lock(&mac->mac_lock);
346 
347 	if (mac->scan_req) {
348 		cfg80211_scan_done(mac->scan_req, &info);
349 		mac->scan_req = NULL;
350 	}
351 
352 	mutex_unlock(&mac->mac_lock);
353 }
354 
355 void qtnf_scan_done(struct qtnf_wmac *mac, bool aborted)
356 {
357 	cancel_delayed_work_sync(&mac->scan_timeout);
358 	qtnf_mac_scan_finish(mac, aborted);
359 }
360 
361 static void qtnf_mac_scan_timeout(struct work_struct *work)
362 {
363 	struct qtnf_wmac *mac =
364 		container_of(work, struct qtnf_wmac, scan_timeout.work);
365 
366 	pr_warn("MAC%d: scan timed out\n", mac->macid);
367 	qtnf_mac_scan_finish(mac, true);
368 }
369 
370 static struct qtnf_wmac *qtnf_core_mac_alloc(struct qtnf_bus *bus,
371 					     unsigned int macid)
372 {
373 	struct wiphy *wiphy;
374 	struct qtnf_wmac *mac;
375 	unsigned int i;
376 
377 	wiphy = qtnf_wiphy_allocate(bus);
378 	if (!wiphy)
379 		return ERR_PTR(-ENOMEM);
380 
381 	mac = wiphy_priv(wiphy);
382 
383 	mac->macid = macid;
384 	mac->bus = bus;
385 
386 	for (i = 0; i < QTNF_MAX_INTF; i++) {
387 		memset(&mac->iflist[i], 0, sizeof(struct qtnf_vif));
388 		mac->iflist[i].wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
389 		mac->iflist[i].mac = mac;
390 		mac->iflist[i].vifid = i;
391 		qtnf_sta_list_init(&mac->iflist[i].sta_list);
392 		mutex_init(&mac->mac_lock);
393 		INIT_DELAYED_WORK(&mac->scan_timeout, qtnf_mac_scan_timeout);
394 		mac->iflist[i].stats64 =
395 			netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
396 		if (!mac->iflist[i].stats64)
397 			pr_warn("VIF%u.%u: per cpu stats allocation failed\n",
398 				macid, i);
399 	}
400 
401 	qtnf_mac_init_primary_intf(mac);
402 	bus->mac[macid] = mac;
403 
404 	return mac;
405 }
406 
407 static const struct ethtool_ops qtnf_ethtool_ops = {
408 	.get_drvinfo = cfg80211_get_drvinfo,
409 };
410 
411 int qtnf_core_net_attach(struct qtnf_wmac *mac, struct qtnf_vif *vif,
412 			 const char *name, unsigned char name_assign_type)
413 {
414 	struct wiphy *wiphy = priv_to_wiphy(mac);
415 	struct net_device *dev;
416 	void *qdev_vif;
417 	int ret;
418 
419 	dev = alloc_netdev_mqs(sizeof(struct qtnf_vif *), name,
420 			       name_assign_type, ether_setup, 1, 1);
421 	if (!dev) {
422 		vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
423 		return -ENOMEM;
424 	}
425 
426 	vif->netdev = dev;
427 
428 	dev->netdev_ops = &qtnf_netdev_ops;
429 	dev->needs_free_netdev = true;
430 	dev_net_set(dev, wiphy_net(wiphy));
431 	dev->ieee80211_ptr = &vif->wdev;
432 	ether_addr_copy(dev->dev_addr, vif->mac_addr);
433 	SET_NETDEV_DEV(dev, wiphy_dev(wiphy));
434 	dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
435 	dev->watchdog_timeo = QTNF_DEF_WDOG_TIMEOUT;
436 	dev->tx_queue_len = 100;
437 	dev->ethtool_ops = &qtnf_ethtool_ops;
438 
439 	qdev_vif = netdev_priv(dev);
440 	*((void **)qdev_vif) = vif;
441 
442 	SET_NETDEV_DEV(dev, mac->bus->dev);
443 
444 	ret = register_netdevice(dev);
445 	if (ret) {
446 		free_netdev(dev);
447 		vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
448 	}
449 
450 	return ret;
451 }
452 
453 static void qtnf_core_mac_detach(struct qtnf_bus *bus, unsigned int macid)
454 {
455 	struct qtnf_wmac *mac;
456 	struct wiphy *wiphy;
457 	struct qtnf_vif *vif;
458 	unsigned int i;
459 	enum nl80211_band band;
460 
461 	mac = bus->mac[macid];
462 
463 	if (!mac)
464 		return;
465 
466 	wiphy = priv_to_wiphy(mac);
467 
468 	for (i = 0; i < QTNF_MAX_INTF; i++) {
469 		vif = &mac->iflist[i];
470 		rtnl_lock();
471 		if (vif->netdev &&
472 		    vif->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED) {
473 			qtnf_virtual_intf_cleanup(vif->netdev);
474 			qtnf_del_virtual_intf(wiphy, &vif->wdev);
475 		}
476 		rtnl_unlock();
477 		qtnf_sta_list_free(&vif->sta_list);
478 		free_percpu(vif->stats64);
479 	}
480 
481 	if (mac->wiphy_registered)
482 		wiphy_unregister(wiphy);
483 
484 	for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; ++band) {
485 		if (!wiphy->bands[band])
486 			continue;
487 
488 		kfree(wiphy->bands[band]->channels);
489 		wiphy->bands[band]->n_channels = 0;
490 
491 		kfree(wiphy->bands[band]);
492 		wiphy->bands[band] = NULL;
493 	}
494 
495 	qtnf_mac_iface_comb_free(mac);
496 	kfree(mac->macinfo.extended_capabilities);
497 	kfree(mac->macinfo.extended_capabilities_mask);
498 	wiphy_free(wiphy);
499 	bus->mac[macid] = NULL;
500 }
501 
502 static int qtnf_core_mac_attach(struct qtnf_bus *bus, unsigned int macid)
503 {
504 	struct qtnf_wmac *mac;
505 	struct qtnf_vif *vif;
506 	int ret;
507 
508 	if (!(bus->hw_info.mac_bitmap & BIT(macid))) {
509 		pr_info("MAC%u is not active in FW\n", macid);
510 		return 0;
511 	}
512 
513 	mac = qtnf_core_mac_alloc(bus, macid);
514 	if (IS_ERR(mac)) {
515 		pr_err("MAC%u allocation failed\n", macid);
516 		return PTR_ERR(mac);
517 	}
518 
519 	ret = qtnf_cmd_get_mac_info(mac);
520 	if (ret) {
521 		pr_err("MAC%u: failed to get info\n", macid);
522 		goto error;
523 	}
524 
525 	vif = qtnf_mac_get_base_vif(mac);
526 	if (!vif) {
527 		pr_err("MAC%u: primary VIF is not ready\n", macid);
528 		ret = -EFAULT;
529 		goto error;
530 	}
531 
532 	ret = qtnf_cmd_send_add_intf(vif, vif->wdev.iftype, vif->mac_addr);
533 	if (ret) {
534 		pr_err("MAC%u: failed to add VIF\n", macid);
535 		goto error;
536 	}
537 
538 	ret = qtnf_cmd_send_get_phy_params(mac);
539 	if (ret) {
540 		pr_err("MAC%u: failed to get PHY settings\n", macid);
541 		goto error;
542 	}
543 
544 	ret = qtnf_mac_init_bands(mac);
545 	if (ret) {
546 		pr_err("MAC%u: failed to init bands\n", macid);
547 		goto error;
548 	}
549 
550 	ret = qtnf_wiphy_register(&bus->hw_info, mac);
551 	if (ret) {
552 		pr_err("MAC%u: wiphy registration failed\n", macid);
553 		goto error;
554 	}
555 
556 	mac->wiphy_registered = 1;
557 
558 	rtnl_lock();
559 
560 	ret = qtnf_core_net_attach(mac, vif, "wlan%d", NET_NAME_ENUM);
561 	rtnl_unlock();
562 
563 	if (ret) {
564 		pr_err("MAC%u: failed to attach netdev\n", macid);
565 		vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
566 		vif->netdev = NULL;
567 		goto error;
568 	}
569 
570 	pr_debug("MAC%u initialized\n", macid);
571 
572 	return 0;
573 
574 error:
575 	qtnf_core_mac_detach(bus, macid);
576 	return ret;
577 }
578 
579 int qtnf_core_attach(struct qtnf_bus *bus)
580 {
581 	unsigned int i;
582 	int ret;
583 
584 	qtnf_trans_init(bus);
585 
586 	bus->fw_state = QTNF_FW_STATE_BOOT_DONE;
587 	qtnf_bus_data_rx_start(bus);
588 
589 	bus->workqueue = alloc_ordered_workqueue("QTNF_BUS", 0);
590 	if (!bus->workqueue) {
591 		pr_err("failed to alloc main workqueue\n");
592 		ret = -ENOMEM;
593 		goto error;
594 	}
595 
596 	INIT_WORK(&bus->event_work, qtnf_event_work_handler);
597 
598 	ret = qtnf_cmd_send_init_fw(bus);
599 	if (ret) {
600 		pr_err("failed to init FW: %d\n", ret);
601 		goto error;
602 	}
603 
604 	bus->fw_state = QTNF_FW_STATE_ACTIVE;
605 
606 	ret = qtnf_cmd_get_hw_info(bus);
607 	if (ret) {
608 		pr_err("failed to get HW info: %d\n", ret);
609 		goto error;
610 	}
611 
612 	if (bus->hw_info.ql_proto_ver != QLINK_PROTO_VER) {
613 		pr_err("qlink version mismatch %u != %u\n",
614 		       QLINK_PROTO_VER, bus->hw_info.ql_proto_ver);
615 		ret = -EPROTONOSUPPORT;
616 		goto error;
617 	}
618 
619 	if (bus->hw_info.num_mac > QTNF_MAX_MAC) {
620 		pr_err("no support for number of MACs=%u\n",
621 		       bus->hw_info.num_mac);
622 		ret = -ERANGE;
623 		goto error;
624 	}
625 
626 	for (i = 0; i < bus->hw_info.num_mac; i++) {
627 		ret = qtnf_core_mac_attach(bus, i);
628 
629 		if (ret) {
630 			pr_err("MAC%u: attach failed: %d\n", i, ret);
631 			goto error;
632 		}
633 	}
634 
635 	return 0;
636 
637 error:
638 	qtnf_core_detach(bus);
639 
640 	return ret;
641 }
642 EXPORT_SYMBOL_GPL(qtnf_core_attach);
643 
644 void qtnf_core_detach(struct qtnf_bus *bus)
645 {
646 	unsigned int macid;
647 
648 	qtnf_bus_data_rx_stop(bus);
649 
650 	for (macid = 0; macid < QTNF_MAX_MAC; macid++)
651 		qtnf_core_mac_detach(bus, macid);
652 
653 	if (bus->fw_state == QTNF_FW_STATE_ACTIVE)
654 		qtnf_cmd_send_deinit_fw(bus);
655 
656 	bus->fw_state = QTNF_FW_STATE_DETACHED;
657 
658 	if (bus->workqueue) {
659 		flush_workqueue(bus->workqueue);
660 		destroy_workqueue(bus->workqueue);
661 	}
662 
663 	kfree(bus->hw_info.rd);
664 	bus->hw_info.rd = NULL;
665 
666 	qtnf_trans_free(bus);
667 }
668 EXPORT_SYMBOL_GPL(qtnf_core_detach);
669 
670 static inline int qtnf_is_frame_meta_magic_valid(struct qtnf_frame_meta_info *m)
671 {
672 	return m->magic_s == 0xAB && m->magic_e == 0xBA;
673 }
674 
675 struct net_device *qtnf_classify_skb(struct qtnf_bus *bus, struct sk_buff *skb)
676 {
677 	struct qtnf_frame_meta_info *meta;
678 	struct net_device *ndev = NULL;
679 	struct qtnf_wmac *mac;
680 	struct qtnf_vif *vif;
681 
682 	meta = (struct qtnf_frame_meta_info *)
683 		(skb_tail_pointer(skb) - sizeof(*meta));
684 
685 	if (unlikely(!qtnf_is_frame_meta_magic_valid(meta))) {
686 		pr_err_ratelimited("invalid magic 0x%x:0x%x\n",
687 				   meta->magic_s, meta->magic_e);
688 		goto out;
689 	}
690 
691 	if (unlikely(meta->macid >= QTNF_MAX_MAC)) {
692 		pr_err_ratelimited("invalid mac(%u)\n", meta->macid);
693 		goto out;
694 	}
695 
696 	if (unlikely(meta->ifidx >= QTNF_MAX_INTF)) {
697 		pr_err_ratelimited("invalid vif(%u)\n", meta->ifidx);
698 		goto out;
699 	}
700 
701 	mac = bus->mac[meta->macid];
702 
703 	if (unlikely(!mac)) {
704 		pr_err_ratelimited("mac(%d) does not exist\n", meta->macid);
705 		goto out;
706 	}
707 
708 	vif = &mac->iflist[meta->ifidx];
709 
710 	if (unlikely(vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)) {
711 		pr_err_ratelimited("vif(%u) does not exists\n", meta->ifidx);
712 		goto out;
713 	}
714 
715 	ndev = vif->netdev;
716 
717 	if (unlikely(!ndev)) {
718 		pr_err_ratelimited("netdev for wlan%u.%u does not exists\n",
719 				   meta->macid, meta->ifidx);
720 		goto out;
721 	}
722 
723 	__skb_trim(skb, skb->len - sizeof(*meta));
724 
725 out:
726 	return ndev;
727 }
728 EXPORT_SYMBOL_GPL(qtnf_classify_skb);
729 
730 void qtnf_wake_all_queues(struct net_device *ndev)
731 {
732 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
733 	struct qtnf_wmac *mac;
734 	struct qtnf_bus *bus;
735 	int macid;
736 	int i;
737 
738 	if (unlikely(!vif || !vif->mac || !vif->mac->bus))
739 		return;
740 
741 	bus = vif->mac->bus;
742 
743 	for (macid = 0; macid < QTNF_MAX_MAC; macid++) {
744 		if (!(bus->hw_info.mac_bitmap & BIT(macid)))
745 			continue;
746 
747 		mac = bus->mac[macid];
748 		for (i = 0; i < QTNF_MAX_INTF; i++) {
749 			vif = &mac->iflist[i];
750 			if (vif->netdev && netif_queue_stopped(vif->netdev))
751 				netif_tx_wake_all_queues(vif->netdev);
752 		}
753 	}
754 }
755 EXPORT_SYMBOL_GPL(qtnf_wake_all_queues);
756 
757 void qtnf_update_rx_stats(struct net_device *ndev, const struct sk_buff *skb)
758 {
759 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
760 	struct pcpu_sw_netstats *stats64;
761 
762 	if (unlikely(!vif || !vif->stats64)) {
763 		ndev->stats.rx_packets++;
764 		ndev->stats.rx_bytes += skb->len;
765 		return;
766 	}
767 
768 	stats64 = this_cpu_ptr(vif->stats64);
769 
770 	u64_stats_update_begin(&stats64->syncp);
771 	stats64->rx_packets++;
772 	stats64->rx_bytes += skb->len;
773 	u64_stats_update_end(&stats64->syncp);
774 }
775 EXPORT_SYMBOL_GPL(qtnf_update_rx_stats);
776 
777 void qtnf_update_tx_stats(struct net_device *ndev, const struct sk_buff *skb)
778 {
779 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
780 	struct pcpu_sw_netstats *stats64;
781 
782 	if (unlikely(!vif || !vif->stats64)) {
783 		ndev->stats.tx_packets++;
784 		ndev->stats.tx_bytes += skb->len;
785 		return;
786 	}
787 
788 	stats64 = this_cpu_ptr(vif->stats64);
789 
790 	u64_stats_update_begin(&stats64->syncp);
791 	stats64->tx_packets++;
792 	stats64->tx_bytes += skb->len;
793 	u64_stats_update_end(&stats64->syncp);
794 }
795 EXPORT_SYMBOL_GPL(qtnf_update_tx_stats);
796 
797 MODULE_AUTHOR("Quantenna Communications");
798 MODULE_DESCRIPTION("Quantenna 802.11 wireless LAN FullMAC driver.");
799 MODULE_LICENSE("GPL");
800