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