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 		if (pdev)
451 			platform_device_unregister(pdev);
452 		return ERR_PTR(-ENOMEM);
453 	}
454 
455 	mac = wiphy_priv(wiphy);
456 
457 	mac->macid = macid;
458 	mac->pdev = pdev;
459 	mac->bus = bus;
460 	mutex_init(&mac->mac_lock);
461 	INIT_DELAYED_WORK(&mac->scan_timeout, qtnf_mac_scan_timeout);
462 
463 	for (i = 0; i < QTNF_MAX_INTF; i++) {
464 		vif = &mac->iflist[i];
465 
466 		memset(vif, 0, sizeof(*vif));
467 		vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
468 		vif->mac = mac;
469 		vif->vifid = i;
470 		qtnf_sta_list_init(&vif->sta_list);
471 		INIT_WORK(&vif->high_pri_tx_work, qtnf_vif_send_data_high_pri);
472 		skb_queue_head_init(&vif->high_pri_tx_queue);
473 		vif->stats64 = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
474 		if (!vif->stats64)
475 			pr_warn("VIF%u.%u: per cpu stats allocation failed\n",
476 				macid, i);
477 	}
478 
479 	qtnf_mac_init_primary_intf(mac);
480 	bus->mac[macid] = mac;
481 
482 	return mac;
483 }
484 
485 static const struct ethtool_ops qtnf_ethtool_ops = {
486 	.get_drvinfo = cfg80211_get_drvinfo,
487 };
488 
489 int qtnf_core_net_attach(struct qtnf_wmac *mac, struct qtnf_vif *vif,
490 			 const char *name, unsigned char name_assign_type)
491 {
492 	struct wiphy *wiphy = priv_to_wiphy(mac);
493 	struct net_device *dev;
494 	void *qdev_vif;
495 	int ret;
496 
497 	dev = alloc_netdev_mqs(sizeof(struct qtnf_vif *), name,
498 			       name_assign_type, ether_setup, 1, 1);
499 	if (!dev)
500 		return -ENOMEM;
501 
502 	vif->netdev = dev;
503 
504 	dev->netdev_ops = &qtnf_netdev_ops;
505 	dev->needs_free_netdev = true;
506 	dev_net_set(dev, wiphy_net(wiphy));
507 	dev->ieee80211_ptr = &vif->wdev;
508 	ether_addr_copy(dev->dev_addr, vif->mac_addr);
509 	dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
510 	dev->watchdog_timeo = QTNF_DEF_WDOG_TIMEOUT;
511 	dev->tx_queue_len = 100;
512 	dev->ethtool_ops = &qtnf_ethtool_ops;
513 
514 	if (qtnf_hwcap_is_set(&mac->bus->hw_info, QLINK_HW_CAPAB_HW_BRIDGE))
515 		dev->needed_tailroom = sizeof(struct qtnf_frame_meta_info);
516 
517 	qdev_vif = netdev_priv(dev);
518 	*((void **)qdev_vif) = vif;
519 
520 	SET_NETDEV_DEV(dev, wiphy_dev(wiphy));
521 
522 	ret = register_netdevice(dev);
523 	if (ret) {
524 		free_netdev(dev);
525 		vif->netdev = NULL;
526 	}
527 
528 	return ret;
529 }
530 
531 static void qtnf_core_mac_detach(struct qtnf_bus *bus, unsigned int macid)
532 {
533 	struct qtnf_wmac *mac;
534 	struct wiphy *wiphy;
535 	struct qtnf_vif *vif;
536 	unsigned int i;
537 	enum nl80211_band band;
538 
539 	mac = bus->mac[macid];
540 
541 	if (!mac)
542 		return;
543 
544 	wiphy = priv_to_wiphy(mac);
545 
546 	for (i = 0; i < QTNF_MAX_INTF; i++) {
547 		vif = &mac->iflist[i];
548 		rtnl_lock();
549 		if (vif->netdev &&
550 		    vif->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED) {
551 			qtnf_virtual_intf_cleanup(vif->netdev);
552 			qtnf_del_virtual_intf(wiphy, &vif->wdev);
553 		}
554 		rtnl_unlock();
555 		qtnf_sta_list_free(&vif->sta_list);
556 		free_percpu(vif->stats64);
557 	}
558 
559 	if (mac->wiphy_registered)
560 		wiphy_unregister(wiphy);
561 
562 	for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; ++band) {
563 		if (!wiphy->bands[band])
564 			continue;
565 
566 		kfree(wiphy->bands[band]->iftype_data);
567 		wiphy->bands[band]->n_iftype_data = 0;
568 
569 		kfree(wiphy->bands[band]->channels);
570 		wiphy->bands[band]->n_channels = 0;
571 
572 		kfree(wiphy->bands[band]);
573 		wiphy->bands[band] = NULL;
574 	}
575 
576 	platform_device_unregister(mac->pdev);
577 	qtnf_mac_iface_comb_free(mac);
578 	qtnf_mac_ext_caps_free(mac);
579 	kfree(mac->macinfo.wowlan);
580 	kfree(mac->rd);
581 	mac->rd = NULL;
582 	wiphy_free(wiphy);
583 	bus->mac[macid] = NULL;
584 }
585 
586 static int qtnf_core_mac_attach(struct qtnf_bus *bus, unsigned int macid)
587 {
588 	struct qtnf_wmac *mac;
589 	struct qtnf_vif *vif;
590 	int ret;
591 
592 	if (!(bus->hw_info.mac_bitmap & BIT(macid))) {
593 		pr_info("MAC%u is not active in FW\n", macid);
594 		return 0;
595 	}
596 
597 	mac = qtnf_core_mac_alloc(bus, macid);
598 	if (IS_ERR(mac)) {
599 		pr_err("MAC%u allocation failed\n", macid);
600 		return PTR_ERR(mac);
601 	}
602 
603 	vif = qtnf_mac_get_base_vif(mac);
604 	if (!vif) {
605 		pr_err("MAC%u: primary VIF is not ready\n", macid);
606 		ret = -EFAULT;
607 		goto error;
608 	}
609 
610 	ret = qtnf_cmd_send_add_intf(vif, vif->wdev.iftype,
611 				     vif->wdev.use_4addr, vif->mac_addr);
612 	if (ret) {
613 		pr_err("MAC%u: failed to add VIF\n", macid);
614 		goto error;
615 	}
616 
617 	ret = qtnf_cmd_get_mac_info(mac);
618 	if (ret) {
619 		pr_err("MAC%u: failed to get MAC info\n", macid);
620 		goto error_del_vif;
621 	}
622 
623 	/* Use MAC address of the first active radio as a unique device ID */
624 	if (is_zero_ether_addr(mac->bus->hw_id))
625 		ether_addr_copy(mac->bus->hw_id, mac->macaddr);
626 
627 	ret = qtnf_mac_init_bands(mac);
628 	if (ret) {
629 		pr_err("MAC%u: failed to init bands\n", macid);
630 		goto error_del_vif;
631 	}
632 
633 	ret = qtnf_wiphy_register(&bus->hw_info, mac);
634 	if (ret) {
635 		pr_err("MAC%u: wiphy registration failed\n", macid);
636 		goto error_del_vif;
637 	}
638 
639 	mac->wiphy_registered = 1;
640 
641 	rtnl_lock();
642 
643 	ret = qtnf_core_net_attach(mac, vif, "wlan%d", NET_NAME_ENUM);
644 	rtnl_unlock();
645 
646 	if (ret) {
647 		pr_err("MAC%u: failed to attach netdev\n", macid);
648 		goto error_del_vif;
649 	}
650 
651 	if (qtnf_hwcap_is_set(&bus->hw_info, QLINK_HW_CAPAB_HW_BRIDGE)) {
652 		ret = qtnf_cmd_netdev_changeupper(vif, vif->netdev->ifindex);
653 		if (ret)
654 			goto error;
655 	}
656 
657 	pr_debug("MAC%u initialized\n", macid);
658 
659 	return 0;
660 
661 error_del_vif:
662 	qtnf_cmd_send_del_intf(vif);
663 	vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
664 error:
665 	qtnf_core_mac_detach(bus, macid);
666 	return ret;
667 }
668 
669 bool qtnf_netdev_is_qtn(const struct net_device *ndev)
670 {
671 	return ndev->netdev_ops == &qtnf_netdev_ops;
672 }
673 
674 static int qtnf_check_br_ports(struct net_device *dev, void *data)
675 {
676 	struct net_device *ndev = data;
677 
678 	if (dev != ndev && netdev_port_same_parent_id(dev, ndev))
679 		return -ENOTSUPP;
680 
681 	return 0;
682 }
683 
684 static int qtnf_core_netdevice_event(struct notifier_block *nb,
685 				     unsigned long event, void *ptr)
686 {
687 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
688 	const struct netdev_notifier_changeupper_info *info;
689 	struct net_device *brdev;
690 	struct qtnf_vif *vif;
691 	struct qtnf_bus *bus;
692 	int br_domain;
693 	int ret = 0;
694 
695 	if (!qtnf_netdev_is_qtn(ndev))
696 		return NOTIFY_DONE;
697 
698 	if (!net_eq(dev_net(ndev), &init_net))
699 		return NOTIFY_OK;
700 
701 	vif = qtnf_netdev_get_priv(ndev);
702 	bus = vif->mac->bus;
703 
704 	switch (event) {
705 	case NETDEV_CHANGEUPPER:
706 		info = ptr;
707 		brdev = info->upper_dev;
708 
709 		if (!netif_is_bridge_master(brdev))
710 			break;
711 
712 		pr_debug("[VIF%u.%u] change bridge: %s %s\n",
713 			 vif->mac->macid, vif->vifid, netdev_name(brdev),
714 			 info->linking ? "add" : "del");
715 
716 		if (IS_ENABLED(CONFIG_NET_SWITCHDEV) &&
717 		    qtnf_hwcap_is_set(&bus->hw_info,
718 				      QLINK_HW_CAPAB_HW_BRIDGE)) {
719 			if (info->linking)
720 				br_domain = brdev->ifindex;
721 			else
722 				br_domain = ndev->ifindex;
723 
724 			ret = qtnf_cmd_netdev_changeupper(vif, br_domain);
725 		} else {
726 			ret = netdev_walk_all_lower_dev(brdev,
727 							qtnf_check_br_ports,
728 							ndev);
729 		}
730 
731 		break;
732 	default:
733 		break;
734 	}
735 
736 	return notifier_from_errno(ret);
737 }
738 
739 int qtnf_core_attach(struct qtnf_bus *bus)
740 {
741 	unsigned int i;
742 	int ret;
743 
744 	qtnf_trans_init(bus);
745 	qtnf_bus_data_rx_start(bus);
746 
747 	bus->workqueue = alloc_ordered_workqueue("QTNF_BUS", 0);
748 	if (!bus->workqueue) {
749 		pr_err("failed to alloc main workqueue\n");
750 		ret = -ENOMEM;
751 		goto error;
752 	}
753 
754 	bus->hprio_workqueue = alloc_workqueue("QTNF_HPRI", WQ_HIGHPRI, 0);
755 	if (!bus->hprio_workqueue) {
756 		pr_err("failed to alloc high prio workqueue\n");
757 		ret = -ENOMEM;
758 		goto error;
759 	}
760 
761 	INIT_WORK(&bus->event_work, qtnf_event_work_handler);
762 
763 	ret = qtnf_cmd_send_init_fw(bus);
764 	if (ret) {
765 		pr_err("failed to init FW: %d\n", ret);
766 		goto error;
767 	}
768 
769 	if (QLINK_VER_MAJOR(bus->hw_info.ql_proto_ver) !=
770 	    QLINK_PROTO_VER_MAJOR) {
771 		pr_err("qlink driver vs FW version mismatch: %u vs %u\n",
772 		       QLINK_PROTO_VER_MAJOR,
773 		       QLINK_VER_MAJOR(bus->hw_info.ql_proto_ver));
774 		ret = -EPROTONOSUPPORT;
775 		goto error;
776 	}
777 
778 	bus->fw_state = QTNF_FW_STATE_ACTIVE;
779 	ret = qtnf_cmd_get_hw_info(bus);
780 	if (ret) {
781 		pr_err("failed to get HW info: %d\n", ret);
782 		goto error;
783 	}
784 
785 	if (qtnf_hwcap_is_set(&bus->hw_info, QLINK_HW_CAPAB_HW_BRIDGE) &&
786 	    bus->bus_ops->data_tx_use_meta_set)
787 		bus->bus_ops->data_tx_use_meta_set(bus, true);
788 
789 	if (bus->hw_info.num_mac > QTNF_MAX_MAC) {
790 		pr_err("no support for number of MACs=%u\n",
791 		       bus->hw_info.num_mac);
792 		ret = -ERANGE;
793 		goto error;
794 	}
795 
796 	for (i = 0; i < bus->hw_info.num_mac; i++) {
797 		ret = qtnf_core_mac_attach(bus, i);
798 
799 		if (ret) {
800 			pr_err("MAC%u: attach failed: %d\n", i, ret);
801 			goto error;
802 		}
803 	}
804 
805 	bus->netdev_nb.notifier_call = qtnf_core_netdevice_event;
806 	ret = register_netdevice_notifier(&bus->netdev_nb);
807 	if (ret) {
808 		pr_err("failed to register netdev notifier: %d\n", ret);
809 		goto error;
810 	}
811 
812 	bus->fw_state = QTNF_FW_STATE_RUNNING;
813 	return 0;
814 
815 error:
816 	qtnf_core_detach(bus);
817 	return ret;
818 }
819 EXPORT_SYMBOL_GPL(qtnf_core_attach);
820 
821 void qtnf_core_detach(struct qtnf_bus *bus)
822 {
823 	unsigned int macid;
824 
825 	unregister_netdevice_notifier(&bus->netdev_nb);
826 	qtnf_bus_data_rx_stop(bus);
827 
828 	for (macid = 0; macid < QTNF_MAX_MAC; macid++)
829 		qtnf_core_mac_detach(bus, macid);
830 
831 	if (qtnf_fw_is_up(bus))
832 		qtnf_cmd_send_deinit_fw(bus);
833 
834 	bus->fw_state = QTNF_FW_STATE_DETACHED;
835 
836 	if (bus->workqueue) {
837 		flush_workqueue(bus->workqueue);
838 		destroy_workqueue(bus->workqueue);
839 		bus->workqueue = NULL;
840 	}
841 
842 	if (bus->hprio_workqueue) {
843 		flush_workqueue(bus->hprio_workqueue);
844 		destroy_workqueue(bus->hprio_workqueue);
845 		bus->hprio_workqueue = NULL;
846 	}
847 
848 	qtnf_trans_free(bus);
849 }
850 EXPORT_SYMBOL_GPL(qtnf_core_detach);
851 
852 static inline int qtnf_is_frame_meta_magic_valid(struct qtnf_frame_meta_info *m)
853 {
854 	return m->magic_s == HBM_FRAME_META_MAGIC_PATTERN_S &&
855 		m->magic_e == HBM_FRAME_META_MAGIC_PATTERN_E;
856 }
857 
858 struct net_device *qtnf_classify_skb(struct qtnf_bus *bus, struct sk_buff *skb)
859 {
860 	struct qtnf_frame_meta_info *meta;
861 	struct net_device *ndev = NULL;
862 	struct qtnf_wmac *mac;
863 	struct qtnf_vif *vif;
864 
865 	if (unlikely(bus->fw_state != QTNF_FW_STATE_RUNNING))
866 		return NULL;
867 
868 	meta = (struct qtnf_frame_meta_info *)
869 		(skb_tail_pointer(skb) - sizeof(*meta));
870 
871 	if (unlikely(!qtnf_is_frame_meta_magic_valid(meta))) {
872 		pr_err_ratelimited("invalid magic 0x%x:0x%x\n",
873 				   meta->magic_s, meta->magic_e);
874 		goto out;
875 	}
876 
877 	if (unlikely(meta->macid >= QTNF_MAX_MAC)) {
878 		pr_err_ratelimited("invalid mac(%u)\n", meta->macid);
879 		goto out;
880 	}
881 
882 	if (unlikely(meta->ifidx >= QTNF_MAX_INTF)) {
883 		pr_err_ratelimited("invalid vif(%u)\n", meta->ifidx);
884 		goto out;
885 	}
886 
887 	mac = bus->mac[meta->macid];
888 
889 	if (unlikely(!mac)) {
890 		pr_err_ratelimited("mac(%d) does not exist\n", meta->macid);
891 		goto out;
892 	}
893 
894 	vif = &mac->iflist[meta->ifidx];
895 
896 	if (unlikely(vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)) {
897 		pr_err_ratelimited("vif(%u) does not exists\n", meta->ifidx);
898 		goto out;
899 	}
900 
901 	ndev = vif->netdev;
902 
903 	if (unlikely(!ndev)) {
904 		pr_err_ratelimited("netdev for wlan%u.%u does not exists\n",
905 				   meta->macid, meta->ifidx);
906 		goto out;
907 	}
908 
909 	__skb_trim(skb, skb->len - sizeof(*meta));
910 	/* Firmware always handles packets that require flooding */
911 	qtnfmac_switch_mark_skb_flooded(skb);
912 
913 out:
914 	return ndev;
915 }
916 EXPORT_SYMBOL_GPL(qtnf_classify_skb);
917 
918 void qtnf_wake_all_queues(struct net_device *ndev)
919 {
920 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
921 	struct qtnf_wmac *mac;
922 	struct qtnf_bus *bus;
923 	int macid;
924 	int i;
925 
926 	if (unlikely(!vif || !vif->mac || !vif->mac->bus))
927 		return;
928 
929 	bus = vif->mac->bus;
930 
931 	for (macid = 0; macid < QTNF_MAX_MAC; macid++) {
932 		if (!(bus->hw_info.mac_bitmap & BIT(macid)))
933 			continue;
934 
935 		mac = bus->mac[macid];
936 		for (i = 0; i < QTNF_MAX_INTF; i++) {
937 			vif = &mac->iflist[i];
938 			if (vif->netdev && netif_queue_stopped(vif->netdev))
939 				netif_tx_wake_all_queues(vif->netdev);
940 		}
941 	}
942 }
943 EXPORT_SYMBOL_GPL(qtnf_wake_all_queues);
944 
945 void qtnf_update_rx_stats(struct net_device *ndev, const struct sk_buff *skb)
946 {
947 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
948 	struct pcpu_sw_netstats *stats64;
949 
950 	if (unlikely(!vif || !vif->stats64)) {
951 		ndev->stats.rx_packets++;
952 		ndev->stats.rx_bytes += skb->len;
953 		return;
954 	}
955 
956 	stats64 = this_cpu_ptr(vif->stats64);
957 
958 	u64_stats_update_begin(&stats64->syncp);
959 	stats64->rx_packets++;
960 	stats64->rx_bytes += skb->len;
961 	u64_stats_update_end(&stats64->syncp);
962 }
963 EXPORT_SYMBOL_GPL(qtnf_update_rx_stats);
964 
965 void qtnf_update_tx_stats(struct net_device *ndev, const struct sk_buff *skb)
966 {
967 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
968 	struct pcpu_sw_netstats *stats64;
969 
970 	if (unlikely(!vif || !vif->stats64)) {
971 		ndev->stats.tx_packets++;
972 		ndev->stats.tx_bytes += skb->len;
973 		return;
974 	}
975 
976 	stats64 = this_cpu_ptr(vif->stats64);
977 
978 	u64_stats_update_begin(&stats64->syncp);
979 	stats64->tx_packets++;
980 	stats64->tx_bytes += skb->len;
981 	u64_stats_update_end(&stats64->syncp);
982 }
983 EXPORT_SYMBOL_GPL(qtnf_update_tx_stats);
984 
985 struct dentry *qtnf_get_debugfs_dir(void)
986 {
987 	return qtnf_debugfs_dir;
988 }
989 EXPORT_SYMBOL_GPL(qtnf_get_debugfs_dir);
990 
991 static int __init qtnf_core_register(void)
992 {
993 	qtnf_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
994 
995 	if (IS_ERR(qtnf_debugfs_dir))
996 		qtnf_debugfs_dir = NULL;
997 
998 	return 0;
999 }
1000 
1001 static void __exit qtnf_core_exit(void)
1002 {
1003 	debugfs_remove(qtnf_debugfs_dir);
1004 }
1005 
1006 module_init(qtnf_core_register);
1007 module_exit(qtnf_core_exit);
1008 
1009 MODULE_AUTHOR("Quantenna Communications");
1010 MODULE_DESCRIPTION("Quantenna 802.11 wireless LAN FullMAC driver.");
1011 MODULE_LICENSE("GPL");
1012