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 #include <linux/nospec.h>
8 
9 #include "core.h"
10 #include "bus.h"
11 #include "trans.h"
12 #include "commands.h"
13 #include "cfg80211.h"
14 #include "event.h"
15 #include "util.h"
16 #include "switchdev.h"
17 
18 #define QTNF_DMP_MAX_LEN 48
19 #define QTNF_PRIMARY_VIF_IDX	0
20 
21 static bool slave_radar = true;
22 module_param(slave_radar, bool, 0644);
23 MODULE_PARM_DESC(slave_radar, "set 0 to disable radar detection in slave mode");
24 
25 static bool dfs_offload;
26 module_param(dfs_offload, bool, 0644);
27 MODULE_PARM_DESC(dfs_offload, "set 1 to enable DFS offload to firmware");
28 
29 static struct dentry *qtnf_debugfs_dir;
30 
31 bool qtnf_slave_radar_get(void)
32 {
33 	return slave_radar;
34 }
35 
36 bool qtnf_dfs_offload_get(void)
37 {
38 	return dfs_offload;
39 }
40 
41 struct qtnf_wmac *qtnf_core_get_mac(const struct qtnf_bus *bus, u8 macid)
42 {
43 	struct qtnf_wmac *mac = NULL;
44 
45 	if (macid >= QTNF_MAX_MAC) {
46 		pr_err("invalid MAC index %u\n", macid);
47 		return NULL;
48 	}
49 
50 	macid = array_index_nospec(macid, QTNF_MAX_MAC);
51 	mac = bus->mac[macid];
52 
53 	if (unlikely(!mac)) {
54 		pr_err("MAC%u: not initialized\n", macid);
55 		return NULL;
56 	}
57 
58 	return mac;
59 }
60 
61 /* Netdev handler for open.
62  */
63 static int qtnf_netdev_open(struct net_device *ndev)
64 {
65 	netif_carrier_off(ndev);
66 	qtnf_netdev_updown(ndev, 1);
67 	return 0;
68 }
69 
70 /* Netdev handler for close.
71  */
72 static int qtnf_netdev_close(struct net_device *ndev)
73 {
74 	netif_carrier_off(ndev);
75 	qtnf_virtual_intf_cleanup(ndev);
76 	qtnf_netdev_updown(ndev, 0);
77 	return 0;
78 }
79 
80 static void qtnf_packet_send_hi_pri(struct sk_buff *skb)
81 {
82 	struct qtnf_vif *vif = qtnf_netdev_get_priv(skb->dev);
83 
84 	skb_queue_tail(&vif->high_pri_tx_queue, skb);
85 	queue_work(vif->mac->bus->hprio_workqueue, &vif->high_pri_tx_work);
86 }
87 
88 /* Netdev handler for data transmission.
89  */
90 static netdev_tx_t
91 qtnf_netdev_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev)
92 {
93 	struct qtnf_vif *vif;
94 	struct qtnf_wmac *mac;
95 
96 	vif = qtnf_netdev_get_priv(ndev);
97 
98 	if (unlikely(skb->dev != ndev)) {
99 		pr_err_ratelimited("invalid skb->dev");
100 		dev_kfree_skb_any(skb);
101 		return 0;
102 	}
103 
104 	if (unlikely(vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)) {
105 		pr_err_ratelimited("%s: VIF not initialized\n", ndev->name);
106 		dev_kfree_skb_any(skb);
107 		return 0;
108 	}
109 
110 	mac = vif->mac;
111 	if (unlikely(!mac)) {
112 		pr_err_ratelimited("%s: NULL mac pointer", ndev->name);
113 		dev_kfree_skb_any(skb);
114 		return 0;
115 	}
116 
117 	if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
118 		pr_err_ratelimited("%s: invalid skb len %d\n", ndev->name,
119 				   skb->len);
120 		dev_kfree_skb_any(skb);
121 		ndev->stats.tx_dropped++;
122 		return 0;
123 	}
124 
125 	/* tx path is enabled: reset vif timeout */
126 	vif->cons_tx_timeout_cnt = 0;
127 
128 	if (unlikely(skb->protocol == htons(ETH_P_PAE))) {
129 		qtnf_packet_send_hi_pri(skb);
130 		qtnf_update_tx_stats(ndev, skb);
131 		return NETDEV_TX_OK;
132 	}
133 
134 	return qtnf_bus_data_tx(mac->bus, skb, mac->macid, vif->vifid);
135 }
136 
137 /* Netdev handler for getting stats.
138  */
139 static void qtnf_netdev_get_stats64(struct net_device *ndev,
140 				    struct rtnl_link_stats64 *stats)
141 {
142 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
143 	unsigned int start;
144 	int cpu;
145 
146 	netdev_stats_to_stats64(stats, &ndev->stats);
147 
148 	if (!vif->stats64)
149 		return;
150 
151 	for_each_possible_cpu(cpu) {
152 		struct pcpu_sw_netstats *stats64;
153 		u64 rx_packets, rx_bytes;
154 		u64 tx_packets, tx_bytes;
155 
156 		stats64 = per_cpu_ptr(vif->stats64, cpu);
157 
158 		do {
159 			start = u64_stats_fetch_begin_irq(&stats64->syncp);
160 			rx_packets = stats64->rx_packets;
161 			rx_bytes = stats64->rx_bytes;
162 			tx_packets = stats64->tx_packets;
163 			tx_bytes = stats64->tx_bytes;
164 		} while (u64_stats_fetch_retry_irq(&stats64->syncp, start));
165 
166 		stats->rx_packets += rx_packets;
167 		stats->rx_bytes += rx_bytes;
168 		stats->tx_packets += tx_packets;
169 		stats->tx_bytes += tx_bytes;
170 	}
171 }
172 
173 /* Netdev handler for transmission timeout.
174  */
175 static void qtnf_netdev_tx_timeout(struct net_device *ndev, unsigned int txqueue)
176 {
177 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
178 	struct qtnf_wmac *mac;
179 	struct qtnf_bus *bus;
180 
181 	if (unlikely(!vif || !vif->mac || !vif->mac->bus))
182 		return;
183 
184 	mac = vif->mac;
185 	bus = mac->bus;
186 
187 	pr_warn("VIF%u.%u: Tx timeout- %lu\n", mac->macid, vif->vifid, jiffies);
188 
189 	qtnf_bus_data_tx_timeout(bus, ndev);
190 	ndev->stats.tx_errors++;
191 
192 	if (++vif->cons_tx_timeout_cnt > QTNF_TX_TIMEOUT_TRSHLD) {
193 		pr_err("Tx timeout threshold exceeded !\n");
194 		pr_err("schedule interface %s reset !\n", netdev_name(ndev));
195 		queue_work(bus->workqueue, &vif->reset_work);
196 	}
197 }
198 
199 static int qtnf_netdev_set_mac_address(struct net_device *ndev, void *addr)
200 {
201 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
202 	struct sockaddr *sa = addr;
203 	int ret;
204 	unsigned char old_addr[ETH_ALEN];
205 
206 	memcpy(old_addr, sa->sa_data, sizeof(old_addr));
207 
208 	ret = eth_mac_addr(ndev, sa);
209 	if (ret)
210 		return ret;
211 
212 	qtnf_scan_done(vif->mac, true);
213 
214 	ret = qtnf_cmd_send_change_intf_type(vif, vif->wdev.iftype,
215 					     vif->wdev.use_4addr,
216 					     sa->sa_data);
217 
218 	if (ret)
219 		memcpy(ndev->dev_addr, old_addr, ETH_ALEN);
220 
221 	return ret;
222 }
223 
224 static int qtnf_netdev_port_parent_id(struct net_device *ndev,
225 				      struct netdev_phys_item_id *ppid)
226 {
227 	const struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
228 	const struct qtnf_bus *bus = vif->mac->bus;
229 
230 	ppid->id_len = sizeof(bus->hw_id);
231 	memcpy(&ppid->id, bus->hw_id, ppid->id_len);
232 
233 	return 0;
234 }
235 
236 /* Network device ops handlers */
237 const struct net_device_ops qtnf_netdev_ops = {
238 	.ndo_open = qtnf_netdev_open,
239 	.ndo_stop = qtnf_netdev_close,
240 	.ndo_start_xmit = qtnf_netdev_hard_start_xmit,
241 	.ndo_tx_timeout = qtnf_netdev_tx_timeout,
242 	.ndo_get_stats64 = qtnf_netdev_get_stats64,
243 	.ndo_set_mac_address = qtnf_netdev_set_mac_address,
244 	.ndo_get_port_parent_id = qtnf_netdev_port_parent_id,
245 };
246 
247 static int qtnf_mac_init_single_band(struct wiphy *wiphy,
248 				     struct qtnf_wmac *mac,
249 				     enum nl80211_band band)
250 {
251 	int ret;
252 
253 	wiphy->bands[band] = kzalloc(sizeof(*wiphy->bands[band]), GFP_KERNEL);
254 	if (!wiphy->bands[band])
255 		return -ENOMEM;
256 
257 	wiphy->bands[band]->band = band;
258 
259 	ret = qtnf_cmd_band_info_get(mac, wiphy->bands[band]);
260 	if (ret) {
261 		pr_err("MAC%u: band %u: failed to get chans info: %d\n",
262 		       mac->macid, band, ret);
263 		return ret;
264 	}
265 
266 	qtnf_band_init_rates(wiphy->bands[band]);
267 
268 	return 0;
269 }
270 
271 static int qtnf_mac_init_bands(struct qtnf_wmac *mac)
272 {
273 	struct wiphy *wiphy = priv_to_wiphy(mac);
274 	int ret = 0;
275 
276 	if (mac->macinfo.bands_cap & QLINK_BAND_2GHZ) {
277 		ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_2GHZ);
278 		if (ret)
279 			goto out;
280 	}
281 
282 	if (mac->macinfo.bands_cap & QLINK_BAND_5GHZ) {
283 		ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_5GHZ);
284 		if (ret)
285 			goto out;
286 	}
287 
288 	if (mac->macinfo.bands_cap & QLINK_BAND_60GHZ)
289 		ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_60GHZ);
290 
291 out:
292 	return ret;
293 }
294 
295 struct qtnf_vif *qtnf_mac_get_free_vif(struct qtnf_wmac *mac)
296 {
297 	struct qtnf_vif *vif;
298 	int i;
299 
300 	for (i = 0; i < QTNF_MAX_INTF; i++) {
301 		vif = &mac->iflist[i];
302 		if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)
303 			return vif;
304 	}
305 
306 	return NULL;
307 }
308 
309 struct qtnf_vif *qtnf_mac_get_base_vif(struct qtnf_wmac *mac)
310 {
311 	struct qtnf_vif *vif;
312 
313 	vif = &mac->iflist[QTNF_PRIMARY_VIF_IDX];
314 
315 	if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)
316 		return NULL;
317 
318 	return vif;
319 }
320 
321 void qtnf_mac_iface_comb_free(struct qtnf_wmac *mac)
322 {
323 	struct ieee80211_iface_combination *comb;
324 	int i;
325 
326 	if (mac->macinfo.if_comb) {
327 		for (i = 0; i < mac->macinfo.n_if_comb; i++) {
328 			comb = &mac->macinfo.if_comb[i];
329 			kfree(comb->limits);
330 			comb->limits = NULL;
331 		}
332 
333 		kfree(mac->macinfo.if_comb);
334 		mac->macinfo.if_comb = NULL;
335 	}
336 }
337 
338 void qtnf_mac_ext_caps_free(struct qtnf_wmac *mac)
339 {
340 	if (mac->macinfo.extended_capabilities_len) {
341 		kfree(mac->macinfo.extended_capabilities);
342 		mac->macinfo.extended_capabilities = NULL;
343 
344 		kfree(mac->macinfo.extended_capabilities_mask);
345 		mac->macinfo.extended_capabilities_mask = NULL;
346 
347 		mac->macinfo.extended_capabilities_len = 0;
348 	}
349 }
350 
351 static void qtnf_vif_reset_handler(struct work_struct *work)
352 {
353 	struct qtnf_vif *vif = container_of(work, struct qtnf_vif, reset_work);
354 
355 	rtnl_lock();
356 
357 	if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED) {
358 		rtnl_unlock();
359 		return;
360 	}
361 
362 	/* stop tx completely */
363 	netif_tx_stop_all_queues(vif->netdev);
364 	if (netif_carrier_ok(vif->netdev))
365 		netif_carrier_off(vif->netdev);
366 
367 	qtnf_cfg80211_vif_reset(vif);
368 
369 	rtnl_unlock();
370 }
371 
372 static void qtnf_mac_init_primary_intf(struct qtnf_wmac *mac)
373 {
374 	struct qtnf_vif *vif = &mac->iflist[QTNF_PRIMARY_VIF_IDX];
375 
376 	vif->wdev.iftype = NL80211_IFTYPE_STATION;
377 	vif->bss_priority = QTNF_DEF_BSS_PRIORITY;
378 	vif->wdev.wiphy = priv_to_wiphy(mac);
379 	INIT_WORK(&vif->reset_work, qtnf_vif_reset_handler);
380 	vif->cons_tx_timeout_cnt = 0;
381 }
382 
383 static void qtnf_mac_scan_finish(struct qtnf_wmac *mac, bool aborted)
384 {
385 	struct cfg80211_scan_info info = {
386 		.aborted = aborted,
387 	};
388 
389 	mutex_lock(&mac->mac_lock);
390 
391 	if (mac->scan_req) {
392 		cfg80211_scan_done(mac->scan_req, &info);
393 		mac->scan_req = NULL;
394 	}
395 
396 	mutex_unlock(&mac->mac_lock);
397 }
398 
399 void qtnf_scan_done(struct qtnf_wmac *mac, bool aborted)
400 {
401 	cancel_delayed_work_sync(&mac->scan_timeout);
402 	qtnf_mac_scan_finish(mac, aborted);
403 }
404 
405 static void qtnf_mac_scan_timeout(struct work_struct *work)
406 {
407 	struct qtnf_wmac *mac =
408 		container_of(work, struct qtnf_wmac, scan_timeout.work);
409 
410 	pr_warn("MAC%d: scan timed out\n", mac->macid);
411 	qtnf_mac_scan_finish(mac, true);
412 }
413 
414 static void qtnf_vif_send_data_high_pri(struct work_struct *work)
415 {
416 	struct qtnf_vif *vif =
417 		container_of(work, struct qtnf_vif, high_pri_tx_work);
418 	struct sk_buff *skb;
419 
420 	if (!vif->netdev ||
421 	    vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)
422 		return;
423 
424 	while ((skb = skb_dequeue(&vif->high_pri_tx_queue))) {
425 		qtnf_cmd_send_frame(vif, 0, QLINK_FRAME_TX_FLAG_8023,
426 				    0, skb->data, skb->len);
427 		dev_kfree_skb_any(skb);
428 	}
429 }
430 
431 static struct qtnf_wmac *qtnf_core_mac_alloc(struct qtnf_bus *bus,
432 					     unsigned int macid)
433 {
434 	struct platform_device *pdev = NULL;
435 	struct qtnf_wmac *mac;
436 	struct qtnf_vif *vif;
437 	struct wiphy *wiphy;
438 	unsigned int i;
439 
440 	if (bus->hw_info.num_mac > 1) {
441 		pdev = platform_device_register_data(bus->dev,
442 						     dev_name(bus->dev),
443 						     macid, NULL, 0);
444 		if (IS_ERR(pdev))
445 			return ERR_PTR(-EINVAL);
446 	}
447 
448 	wiphy = qtnf_wiphy_allocate(bus, pdev);
449 	if (!wiphy)
450 		return ERR_PTR(-ENOMEM);
451 
452 	mac = wiphy_priv(wiphy);
453 
454 	mac->macid = macid;
455 	mac->pdev = pdev;
456 	mac->bus = bus;
457 	mutex_init(&mac->mac_lock);
458 	INIT_DELAYED_WORK(&mac->scan_timeout, qtnf_mac_scan_timeout);
459 
460 	for (i = 0; i < QTNF_MAX_INTF; i++) {
461 		vif = &mac->iflist[i];
462 
463 		memset(vif, 0, sizeof(*vif));
464 		vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
465 		vif->mac = mac;
466 		vif->vifid = i;
467 		qtnf_sta_list_init(&vif->sta_list);
468 		INIT_WORK(&vif->high_pri_tx_work, qtnf_vif_send_data_high_pri);
469 		skb_queue_head_init(&vif->high_pri_tx_queue);
470 		vif->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
471 		if (!vif->stats64)
472 			pr_warn("VIF%u.%u: per cpu stats allocation failed\n",
473 				macid, i);
474 	}
475 
476 	qtnf_mac_init_primary_intf(mac);
477 	bus->mac[macid] = mac;
478 
479 	return mac;
480 }
481 
482 static const struct ethtool_ops qtnf_ethtool_ops = {
483 	.get_drvinfo = cfg80211_get_drvinfo,
484 };
485 
486 int qtnf_core_net_attach(struct qtnf_wmac *mac, struct qtnf_vif *vif,
487 			 const char *name, unsigned char name_assign_type)
488 {
489 	struct wiphy *wiphy = priv_to_wiphy(mac);
490 	struct net_device *dev;
491 	void *qdev_vif;
492 	int ret;
493 
494 	dev = alloc_netdev_mqs(sizeof(struct qtnf_vif *), name,
495 			       name_assign_type, ether_setup, 1, 1);
496 	if (!dev)
497 		return -ENOMEM;
498 
499 	vif->netdev = dev;
500 
501 	dev->netdev_ops = &qtnf_netdev_ops;
502 	dev->needs_free_netdev = true;
503 	dev_net_set(dev, wiphy_net(wiphy));
504 	dev->ieee80211_ptr = &vif->wdev;
505 	ether_addr_copy(dev->dev_addr, vif->mac_addr);
506 	dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
507 	dev->watchdog_timeo = QTNF_DEF_WDOG_TIMEOUT;
508 	dev->tx_queue_len = 100;
509 	dev->ethtool_ops = &qtnf_ethtool_ops;
510 
511 	if (qtnf_hwcap_is_set(&mac->bus->hw_info, QLINK_HW_CAPAB_HW_BRIDGE))
512 		dev->needed_tailroom = sizeof(struct qtnf_frame_meta_info);
513 
514 	qdev_vif = netdev_priv(dev);
515 	*((void **)qdev_vif) = vif;
516 
517 	SET_NETDEV_DEV(dev, wiphy_dev(wiphy));
518 
519 	ret = register_netdevice(dev);
520 	if (ret) {
521 		free_netdev(dev);
522 		vif->netdev = NULL;
523 	}
524 
525 	return ret;
526 }
527 
528 static void qtnf_core_mac_detach(struct qtnf_bus *bus, unsigned int macid)
529 {
530 	struct qtnf_wmac *mac;
531 	struct wiphy *wiphy;
532 	struct qtnf_vif *vif;
533 	unsigned int i;
534 	enum nl80211_band band;
535 
536 	mac = bus->mac[macid];
537 
538 	if (!mac)
539 		return;
540 
541 	wiphy = priv_to_wiphy(mac);
542 
543 	for (i = 0; i < QTNF_MAX_INTF; i++) {
544 		vif = &mac->iflist[i];
545 		rtnl_lock();
546 		if (vif->netdev &&
547 		    vif->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED) {
548 			qtnf_virtual_intf_cleanup(vif->netdev);
549 			qtnf_del_virtual_intf(wiphy, &vif->wdev);
550 		}
551 		rtnl_unlock();
552 		qtnf_sta_list_free(&vif->sta_list);
553 		free_percpu(vif->stats64);
554 	}
555 
556 	if (mac->wiphy_registered)
557 		wiphy_unregister(wiphy);
558 
559 	for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; ++band) {
560 		if (!wiphy->bands[band])
561 			continue;
562 
563 		kfree(wiphy->bands[band]->iftype_data);
564 		wiphy->bands[band]->n_iftype_data = 0;
565 
566 		kfree(wiphy->bands[band]->channels);
567 		wiphy->bands[band]->n_channels = 0;
568 
569 		kfree(wiphy->bands[band]);
570 		wiphy->bands[band] = NULL;
571 	}
572 
573 	platform_device_unregister(mac->pdev);
574 	qtnf_mac_iface_comb_free(mac);
575 	qtnf_mac_ext_caps_free(mac);
576 	kfree(mac->macinfo.wowlan);
577 	kfree(mac->rd);
578 	mac->rd = NULL;
579 	wiphy_free(wiphy);
580 	bus->mac[macid] = NULL;
581 }
582 
583 static int qtnf_core_mac_attach(struct qtnf_bus *bus, unsigned int macid)
584 {
585 	struct qtnf_wmac *mac;
586 	struct qtnf_vif *vif;
587 	int ret;
588 
589 	if (!(bus->hw_info.mac_bitmap & BIT(macid))) {
590 		pr_info("MAC%u is not active in FW\n", macid);
591 		return 0;
592 	}
593 
594 	mac = qtnf_core_mac_alloc(bus, macid);
595 	if (IS_ERR(mac)) {
596 		pr_err("MAC%u allocation failed\n", macid);
597 		return PTR_ERR(mac);
598 	}
599 
600 	vif = qtnf_mac_get_base_vif(mac);
601 	if (!vif) {
602 		pr_err("MAC%u: primary VIF is not ready\n", macid);
603 		ret = -EFAULT;
604 		goto error;
605 	}
606 
607 	ret = qtnf_cmd_send_add_intf(vif, vif->wdev.iftype,
608 				     vif->wdev.use_4addr, vif->mac_addr);
609 	if (ret) {
610 		pr_err("MAC%u: failed to add VIF\n", macid);
611 		goto error;
612 	}
613 
614 	ret = qtnf_cmd_get_mac_info(mac);
615 	if (ret) {
616 		pr_err("MAC%u: failed to get MAC info\n", macid);
617 		goto error_del_vif;
618 	}
619 
620 	/* Use MAC address of the first active radio as a unique device ID */
621 	if (is_zero_ether_addr(mac->bus->hw_id))
622 		ether_addr_copy(mac->bus->hw_id, mac->macaddr);
623 
624 	ret = qtnf_mac_init_bands(mac);
625 	if (ret) {
626 		pr_err("MAC%u: failed to init bands\n", macid);
627 		goto error_del_vif;
628 	}
629 
630 	ret = qtnf_wiphy_register(&bus->hw_info, mac);
631 	if (ret) {
632 		pr_err("MAC%u: wiphy registration failed\n", macid);
633 		goto error_del_vif;
634 	}
635 
636 	mac->wiphy_registered = 1;
637 
638 	rtnl_lock();
639 
640 	ret = qtnf_core_net_attach(mac, vif, "wlan%d", NET_NAME_ENUM);
641 	rtnl_unlock();
642 
643 	if (ret) {
644 		pr_err("MAC%u: failed to attach netdev\n", macid);
645 		goto error_del_vif;
646 	}
647 
648 	if (qtnf_hwcap_is_set(&bus->hw_info, QLINK_HW_CAPAB_HW_BRIDGE)) {
649 		ret = qtnf_cmd_netdev_changeupper(vif, vif->netdev->ifindex);
650 		if (ret)
651 			goto error;
652 	}
653 
654 	pr_debug("MAC%u initialized\n", macid);
655 
656 	return 0;
657 
658 error_del_vif:
659 	qtnf_cmd_send_del_intf(vif);
660 	vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
661 error:
662 	qtnf_core_mac_detach(bus, macid);
663 	return ret;
664 }
665 
666 bool qtnf_netdev_is_qtn(const struct net_device *ndev)
667 {
668 	return ndev->netdev_ops == &qtnf_netdev_ops;
669 }
670 
671 static int qtnf_check_br_ports(struct net_device *dev, void *data)
672 {
673 	struct net_device *ndev = data;
674 
675 	if (dev != ndev && netdev_port_same_parent_id(dev, ndev))
676 		return -ENOTSUPP;
677 
678 	return 0;
679 }
680 
681 static int qtnf_core_netdevice_event(struct notifier_block *nb,
682 				     unsigned long event, void *ptr)
683 {
684 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
685 	const struct netdev_notifier_changeupper_info *info;
686 	struct net_device *brdev;
687 	struct qtnf_vif *vif;
688 	struct qtnf_bus *bus;
689 	int br_domain;
690 	int ret = 0;
691 
692 	if (!qtnf_netdev_is_qtn(ndev))
693 		return NOTIFY_DONE;
694 
695 	if (!net_eq(dev_net(ndev), &init_net))
696 		return NOTIFY_OK;
697 
698 	vif = qtnf_netdev_get_priv(ndev);
699 	bus = vif->mac->bus;
700 
701 	switch (event) {
702 	case NETDEV_CHANGEUPPER:
703 		info = ptr;
704 		brdev = info->upper_dev;
705 
706 		if (!netif_is_bridge_master(brdev))
707 			break;
708 
709 		pr_debug("[VIF%u.%u] change bridge: %s %s\n",
710 			 vif->mac->macid, vif->vifid, netdev_name(brdev),
711 			 info->linking ? "add" : "del");
712 
713 		if (IS_ENABLED(CONFIG_NET_SWITCHDEV) &&
714 		    qtnf_hwcap_is_set(&bus->hw_info,
715 				      QLINK_HW_CAPAB_HW_BRIDGE)) {
716 			if (info->linking)
717 				br_domain = brdev->ifindex;
718 			else
719 				br_domain = ndev->ifindex;
720 
721 			ret = qtnf_cmd_netdev_changeupper(vif, br_domain);
722 		} else {
723 			ret = netdev_walk_all_lower_dev(brdev,
724 							qtnf_check_br_ports,
725 							ndev);
726 		}
727 
728 		break;
729 	default:
730 		break;
731 	}
732 
733 	return notifier_from_errno(ret);
734 }
735 
736 int qtnf_core_attach(struct qtnf_bus *bus)
737 {
738 	unsigned int i;
739 	int ret;
740 
741 	qtnf_trans_init(bus);
742 	qtnf_bus_data_rx_start(bus);
743 
744 	bus->workqueue = alloc_ordered_workqueue("QTNF_BUS", 0);
745 	if (!bus->workqueue) {
746 		pr_err("failed to alloc main workqueue\n");
747 		ret = -ENOMEM;
748 		goto error;
749 	}
750 
751 	bus->hprio_workqueue = alloc_workqueue("QTNF_HPRI", WQ_HIGHPRI, 0);
752 	if (!bus->hprio_workqueue) {
753 		pr_err("failed to alloc high prio workqueue\n");
754 		ret = -ENOMEM;
755 		goto error;
756 	}
757 
758 	INIT_WORK(&bus->event_work, qtnf_event_work_handler);
759 
760 	ret = qtnf_cmd_send_init_fw(bus);
761 	if (ret) {
762 		pr_err("failed to init FW: %d\n", ret);
763 		goto error;
764 	}
765 
766 	if (QLINK_VER_MAJOR(bus->hw_info.ql_proto_ver) !=
767 	    QLINK_PROTO_VER_MAJOR) {
768 		pr_err("qlink driver vs FW version mismatch: %u vs %u\n",
769 		       QLINK_PROTO_VER_MAJOR,
770 		       QLINK_VER_MAJOR(bus->hw_info.ql_proto_ver));
771 		ret = -EPROTONOSUPPORT;
772 		goto error;
773 	}
774 
775 	bus->fw_state = QTNF_FW_STATE_ACTIVE;
776 	ret = qtnf_cmd_get_hw_info(bus);
777 	if (ret) {
778 		pr_err("failed to get HW info: %d\n", ret);
779 		goto error;
780 	}
781 
782 	if (qtnf_hwcap_is_set(&bus->hw_info, QLINK_HW_CAPAB_HW_BRIDGE) &&
783 	    bus->bus_ops->data_tx_use_meta_set)
784 		bus->bus_ops->data_tx_use_meta_set(bus, true);
785 
786 	if (bus->hw_info.num_mac > QTNF_MAX_MAC) {
787 		pr_err("no support for number of MACs=%u\n",
788 		       bus->hw_info.num_mac);
789 		ret = -ERANGE;
790 		goto error;
791 	}
792 
793 	for (i = 0; i < bus->hw_info.num_mac; i++) {
794 		ret = qtnf_core_mac_attach(bus, i);
795 
796 		if (ret) {
797 			pr_err("MAC%u: attach failed: %d\n", i, ret);
798 			goto error;
799 		}
800 	}
801 
802 	bus->netdev_nb.notifier_call = qtnf_core_netdevice_event;
803 	ret = register_netdevice_notifier(&bus->netdev_nb);
804 	if (ret) {
805 		pr_err("failed to register netdev notifier: %d\n", ret);
806 		goto error;
807 	}
808 
809 	bus->fw_state = QTNF_FW_STATE_RUNNING;
810 	return 0;
811 
812 error:
813 	qtnf_core_detach(bus);
814 	return ret;
815 }
816 EXPORT_SYMBOL_GPL(qtnf_core_attach);
817 
818 void qtnf_core_detach(struct qtnf_bus *bus)
819 {
820 	unsigned int macid;
821 
822 	unregister_netdevice_notifier(&bus->netdev_nb);
823 	qtnf_bus_data_rx_stop(bus);
824 
825 	for (macid = 0; macid < QTNF_MAX_MAC; macid++)
826 		qtnf_core_mac_detach(bus, macid);
827 
828 	if (qtnf_fw_is_up(bus))
829 		qtnf_cmd_send_deinit_fw(bus);
830 
831 	bus->fw_state = QTNF_FW_STATE_DETACHED;
832 
833 	if (bus->workqueue) {
834 		flush_workqueue(bus->workqueue);
835 		destroy_workqueue(bus->workqueue);
836 		bus->workqueue = NULL;
837 	}
838 
839 	if (bus->hprio_workqueue) {
840 		flush_workqueue(bus->hprio_workqueue);
841 		destroy_workqueue(bus->hprio_workqueue);
842 		bus->hprio_workqueue = NULL;
843 	}
844 
845 	qtnf_trans_free(bus);
846 }
847 EXPORT_SYMBOL_GPL(qtnf_core_detach);
848 
849 static inline int qtnf_is_frame_meta_magic_valid(struct qtnf_frame_meta_info *m)
850 {
851 	return m->magic_s == HBM_FRAME_META_MAGIC_PATTERN_S &&
852 		m->magic_e == HBM_FRAME_META_MAGIC_PATTERN_E;
853 }
854 
855 struct net_device *qtnf_classify_skb(struct qtnf_bus *bus, struct sk_buff *skb)
856 {
857 	struct qtnf_frame_meta_info *meta;
858 	struct net_device *ndev = NULL;
859 	struct qtnf_wmac *mac;
860 	struct qtnf_vif *vif;
861 
862 	if (unlikely(bus->fw_state != QTNF_FW_STATE_RUNNING))
863 		return NULL;
864 
865 	meta = (struct qtnf_frame_meta_info *)
866 		(skb_tail_pointer(skb) - sizeof(*meta));
867 
868 	if (unlikely(!qtnf_is_frame_meta_magic_valid(meta))) {
869 		pr_err_ratelimited("invalid magic 0x%x:0x%x\n",
870 				   meta->magic_s, meta->magic_e);
871 		goto out;
872 	}
873 
874 	if (unlikely(meta->macid >= QTNF_MAX_MAC)) {
875 		pr_err_ratelimited("invalid mac(%u)\n", meta->macid);
876 		goto out;
877 	}
878 
879 	if (unlikely(meta->ifidx >= QTNF_MAX_INTF)) {
880 		pr_err_ratelimited("invalid vif(%u)\n", meta->ifidx);
881 		goto out;
882 	}
883 
884 	mac = bus->mac[meta->macid];
885 
886 	if (unlikely(!mac)) {
887 		pr_err_ratelimited("mac(%d) does not exist\n", meta->macid);
888 		goto out;
889 	}
890 
891 	vif = &mac->iflist[meta->ifidx];
892 
893 	if (unlikely(vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)) {
894 		pr_err_ratelimited("vif(%u) does not exists\n", meta->ifidx);
895 		goto out;
896 	}
897 
898 	ndev = vif->netdev;
899 
900 	if (unlikely(!ndev)) {
901 		pr_err_ratelimited("netdev for wlan%u.%u does not exists\n",
902 				   meta->macid, meta->ifidx);
903 		goto out;
904 	}
905 
906 	__skb_trim(skb, skb->len - sizeof(*meta));
907 	/* Firmware always handles packets that require flooding */
908 	qtnfmac_switch_mark_skb_flooded(skb);
909 
910 out:
911 	return ndev;
912 }
913 EXPORT_SYMBOL_GPL(qtnf_classify_skb);
914 
915 void qtnf_wake_all_queues(struct net_device *ndev)
916 {
917 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
918 	struct qtnf_wmac *mac;
919 	struct qtnf_bus *bus;
920 	int macid;
921 	int i;
922 
923 	if (unlikely(!vif || !vif->mac || !vif->mac->bus))
924 		return;
925 
926 	bus = vif->mac->bus;
927 
928 	for (macid = 0; macid < QTNF_MAX_MAC; macid++) {
929 		if (!(bus->hw_info.mac_bitmap & BIT(macid)))
930 			continue;
931 
932 		mac = bus->mac[macid];
933 		for (i = 0; i < QTNF_MAX_INTF; i++) {
934 			vif = &mac->iflist[i];
935 			if (vif->netdev && netif_queue_stopped(vif->netdev))
936 				netif_tx_wake_all_queues(vif->netdev);
937 		}
938 	}
939 }
940 EXPORT_SYMBOL_GPL(qtnf_wake_all_queues);
941 
942 void qtnf_update_rx_stats(struct net_device *ndev, const struct sk_buff *skb)
943 {
944 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
945 	struct pcpu_sw_netstats *stats64;
946 
947 	if (unlikely(!vif || !vif->stats64)) {
948 		ndev->stats.rx_packets++;
949 		ndev->stats.rx_bytes += skb->len;
950 		return;
951 	}
952 
953 	stats64 = this_cpu_ptr(vif->stats64);
954 
955 	u64_stats_update_begin(&stats64->syncp);
956 	stats64->rx_packets++;
957 	stats64->rx_bytes += skb->len;
958 	u64_stats_update_end(&stats64->syncp);
959 }
960 EXPORT_SYMBOL_GPL(qtnf_update_rx_stats);
961 
962 void qtnf_update_tx_stats(struct net_device *ndev, const struct sk_buff *skb)
963 {
964 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
965 	struct pcpu_sw_netstats *stats64;
966 
967 	if (unlikely(!vif || !vif->stats64)) {
968 		ndev->stats.tx_packets++;
969 		ndev->stats.tx_bytes += skb->len;
970 		return;
971 	}
972 
973 	stats64 = this_cpu_ptr(vif->stats64);
974 
975 	u64_stats_update_begin(&stats64->syncp);
976 	stats64->tx_packets++;
977 	stats64->tx_bytes += skb->len;
978 	u64_stats_update_end(&stats64->syncp);
979 }
980 EXPORT_SYMBOL_GPL(qtnf_update_tx_stats);
981 
982 struct dentry *qtnf_get_debugfs_dir(void)
983 {
984 	return qtnf_debugfs_dir;
985 }
986 EXPORT_SYMBOL_GPL(qtnf_get_debugfs_dir);
987 
988 static int __init qtnf_core_register(void)
989 {
990 	qtnf_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
991 
992 	if (IS_ERR(qtnf_debugfs_dir))
993 		qtnf_debugfs_dir = NULL;
994 
995 	return 0;
996 }
997 
998 static void __exit qtnf_core_exit(void)
999 {
1000 	debugfs_remove(qtnf_debugfs_dir);
1001 }
1002 
1003 module_init(qtnf_core_register);
1004 module_exit(qtnf_core_exit);
1005 
1006 MODULE_AUTHOR("Quantenna Communications");
1007 MODULE_DESCRIPTION("Quantenna 802.11 wireless LAN FullMAC driver.");
1008 MODULE_LICENSE("GPL");
1009