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,
675 			       struct netdev_nested_priv *priv)
676 {
677 	struct net_device *ndev = (struct net_device *)priv->data;
678 
679 	if (dev != ndev && netdev_port_same_parent_id(dev, ndev))
680 		return -ENOTSUPP;
681 
682 	return 0;
683 }
684 
685 static int qtnf_core_netdevice_event(struct notifier_block *nb,
686 				     unsigned long event, void *ptr)
687 {
688 	struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
689 	const struct netdev_notifier_changeupper_info *info;
690 	struct netdev_nested_priv priv = {
691 		.data = (void *)ndev,
692 	};
693 	struct net_device *brdev;
694 	struct qtnf_vif *vif;
695 	struct qtnf_bus *bus;
696 	int br_domain;
697 	int ret = 0;
698 
699 	if (!qtnf_netdev_is_qtn(ndev))
700 		return NOTIFY_DONE;
701 
702 	if (!net_eq(dev_net(ndev), &init_net))
703 		return NOTIFY_OK;
704 
705 	vif = qtnf_netdev_get_priv(ndev);
706 	bus = vif->mac->bus;
707 
708 	switch (event) {
709 	case NETDEV_CHANGEUPPER:
710 		info = ptr;
711 		brdev = info->upper_dev;
712 
713 		if (!netif_is_bridge_master(brdev))
714 			break;
715 
716 		pr_debug("[VIF%u.%u] change bridge: %s %s\n",
717 			 vif->mac->macid, vif->vifid, netdev_name(brdev),
718 			 info->linking ? "add" : "del");
719 
720 		if (IS_ENABLED(CONFIG_NET_SWITCHDEV) &&
721 		    qtnf_hwcap_is_set(&bus->hw_info,
722 				      QLINK_HW_CAPAB_HW_BRIDGE)) {
723 			if (info->linking)
724 				br_domain = brdev->ifindex;
725 			else
726 				br_domain = ndev->ifindex;
727 
728 			ret = qtnf_cmd_netdev_changeupper(vif, br_domain);
729 		} else {
730 			ret = netdev_walk_all_lower_dev(brdev,
731 							qtnf_check_br_ports,
732 							&priv);
733 		}
734 
735 		break;
736 	default:
737 		break;
738 	}
739 
740 	return notifier_from_errno(ret);
741 }
742 
743 int qtnf_core_attach(struct qtnf_bus *bus)
744 {
745 	unsigned int i;
746 	int ret;
747 
748 	qtnf_trans_init(bus);
749 	qtnf_bus_data_rx_start(bus);
750 
751 	bus->workqueue = alloc_ordered_workqueue("QTNF_BUS", 0);
752 	if (!bus->workqueue) {
753 		pr_err("failed to alloc main workqueue\n");
754 		ret = -ENOMEM;
755 		goto error;
756 	}
757 
758 	bus->hprio_workqueue = alloc_workqueue("QTNF_HPRI", WQ_HIGHPRI, 0);
759 	if (!bus->hprio_workqueue) {
760 		pr_err("failed to alloc high prio workqueue\n");
761 		ret = -ENOMEM;
762 		goto error;
763 	}
764 
765 	INIT_WORK(&bus->event_work, qtnf_event_work_handler);
766 
767 	ret = qtnf_cmd_send_init_fw(bus);
768 	if (ret) {
769 		pr_err("failed to init FW: %d\n", ret);
770 		goto error;
771 	}
772 
773 	if (QLINK_VER_MAJOR(bus->hw_info.ql_proto_ver) !=
774 	    QLINK_PROTO_VER_MAJOR) {
775 		pr_err("qlink driver vs FW version mismatch: %u vs %u\n",
776 		       QLINK_PROTO_VER_MAJOR,
777 		       QLINK_VER_MAJOR(bus->hw_info.ql_proto_ver));
778 		ret = -EPROTONOSUPPORT;
779 		goto error;
780 	}
781 
782 	bus->fw_state = QTNF_FW_STATE_ACTIVE;
783 	ret = qtnf_cmd_get_hw_info(bus);
784 	if (ret) {
785 		pr_err("failed to get HW info: %d\n", ret);
786 		goto error;
787 	}
788 
789 	if (qtnf_hwcap_is_set(&bus->hw_info, QLINK_HW_CAPAB_HW_BRIDGE) &&
790 	    bus->bus_ops->data_tx_use_meta_set)
791 		bus->bus_ops->data_tx_use_meta_set(bus, true);
792 
793 	if (bus->hw_info.num_mac > QTNF_MAX_MAC) {
794 		pr_err("no support for number of MACs=%u\n",
795 		       bus->hw_info.num_mac);
796 		ret = -ERANGE;
797 		goto error;
798 	}
799 
800 	for (i = 0; i < bus->hw_info.num_mac; i++) {
801 		ret = qtnf_core_mac_attach(bus, i);
802 
803 		if (ret) {
804 			pr_err("MAC%u: attach failed: %d\n", i, ret);
805 			goto error;
806 		}
807 	}
808 
809 	bus->netdev_nb.notifier_call = qtnf_core_netdevice_event;
810 	ret = register_netdevice_notifier(&bus->netdev_nb);
811 	if (ret) {
812 		pr_err("failed to register netdev notifier: %d\n", ret);
813 		goto error;
814 	}
815 
816 	bus->fw_state = QTNF_FW_STATE_RUNNING;
817 	return 0;
818 
819 error:
820 	qtnf_core_detach(bus);
821 	return ret;
822 }
823 EXPORT_SYMBOL_GPL(qtnf_core_attach);
824 
825 void qtnf_core_detach(struct qtnf_bus *bus)
826 {
827 	unsigned int macid;
828 
829 	unregister_netdevice_notifier(&bus->netdev_nb);
830 	qtnf_bus_data_rx_stop(bus);
831 
832 	for (macid = 0; macid < QTNF_MAX_MAC; macid++)
833 		qtnf_core_mac_detach(bus, macid);
834 
835 	if (qtnf_fw_is_up(bus))
836 		qtnf_cmd_send_deinit_fw(bus);
837 
838 	bus->fw_state = QTNF_FW_STATE_DETACHED;
839 
840 	if (bus->workqueue) {
841 		flush_workqueue(bus->workqueue);
842 		destroy_workqueue(bus->workqueue);
843 		bus->workqueue = NULL;
844 	}
845 
846 	if (bus->hprio_workqueue) {
847 		flush_workqueue(bus->hprio_workqueue);
848 		destroy_workqueue(bus->hprio_workqueue);
849 		bus->hprio_workqueue = NULL;
850 	}
851 
852 	qtnf_trans_free(bus);
853 }
854 EXPORT_SYMBOL_GPL(qtnf_core_detach);
855 
856 static inline int qtnf_is_frame_meta_magic_valid(struct qtnf_frame_meta_info *m)
857 {
858 	return m->magic_s == HBM_FRAME_META_MAGIC_PATTERN_S &&
859 		m->magic_e == HBM_FRAME_META_MAGIC_PATTERN_E;
860 }
861 
862 struct net_device *qtnf_classify_skb(struct qtnf_bus *bus, struct sk_buff *skb)
863 {
864 	struct qtnf_frame_meta_info *meta;
865 	struct net_device *ndev = NULL;
866 	struct qtnf_wmac *mac;
867 	struct qtnf_vif *vif;
868 
869 	if (unlikely(bus->fw_state != QTNF_FW_STATE_RUNNING))
870 		return NULL;
871 
872 	meta = (struct qtnf_frame_meta_info *)
873 		(skb_tail_pointer(skb) - sizeof(*meta));
874 
875 	if (unlikely(!qtnf_is_frame_meta_magic_valid(meta))) {
876 		pr_err_ratelimited("invalid magic 0x%x:0x%x\n",
877 				   meta->magic_s, meta->magic_e);
878 		goto out;
879 	}
880 
881 	if (unlikely(meta->macid >= QTNF_MAX_MAC)) {
882 		pr_err_ratelimited("invalid mac(%u)\n", meta->macid);
883 		goto out;
884 	}
885 
886 	if (unlikely(meta->ifidx >= QTNF_MAX_INTF)) {
887 		pr_err_ratelimited("invalid vif(%u)\n", meta->ifidx);
888 		goto out;
889 	}
890 
891 	mac = bus->mac[meta->macid];
892 
893 	if (unlikely(!mac)) {
894 		pr_err_ratelimited("mac(%d) does not exist\n", meta->macid);
895 		goto out;
896 	}
897 
898 	vif = &mac->iflist[meta->ifidx];
899 
900 	if (unlikely(vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)) {
901 		pr_err_ratelimited("vif(%u) does not exists\n", meta->ifidx);
902 		goto out;
903 	}
904 
905 	ndev = vif->netdev;
906 
907 	if (unlikely(!ndev)) {
908 		pr_err_ratelimited("netdev for wlan%u.%u does not exists\n",
909 				   meta->macid, meta->ifidx);
910 		goto out;
911 	}
912 
913 	__skb_trim(skb, skb->len - sizeof(*meta));
914 	/* Firmware always handles packets that require flooding */
915 	qtnfmac_switch_mark_skb_flooded(skb);
916 
917 out:
918 	return ndev;
919 }
920 EXPORT_SYMBOL_GPL(qtnf_classify_skb);
921 
922 void qtnf_wake_all_queues(struct net_device *ndev)
923 {
924 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
925 	struct qtnf_wmac *mac;
926 	struct qtnf_bus *bus;
927 	int macid;
928 	int i;
929 
930 	if (unlikely(!vif || !vif->mac || !vif->mac->bus))
931 		return;
932 
933 	bus = vif->mac->bus;
934 
935 	for (macid = 0; macid < QTNF_MAX_MAC; macid++) {
936 		if (!(bus->hw_info.mac_bitmap & BIT(macid)))
937 			continue;
938 
939 		mac = bus->mac[macid];
940 		for (i = 0; i < QTNF_MAX_INTF; i++) {
941 			vif = &mac->iflist[i];
942 			if (vif->netdev && netif_queue_stopped(vif->netdev))
943 				netif_tx_wake_all_queues(vif->netdev);
944 		}
945 	}
946 }
947 EXPORT_SYMBOL_GPL(qtnf_wake_all_queues);
948 
949 void qtnf_update_rx_stats(struct net_device *ndev, const struct sk_buff *skb)
950 {
951 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
952 	struct pcpu_sw_netstats *stats64;
953 
954 	if (unlikely(!vif || !vif->stats64)) {
955 		ndev->stats.rx_packets++;
956 		ndev->stats.rx_bytes += skb->len;
957 		return;
958 	}
959 
960 	stats64 = this_cpu_ptr(vif->stats64);
961 
962 	u64_stats_update_begin(&stats64->syncp);
963 	stats64->rx_packets++;
964 	stats64->rx_bytes += skb->len;
965 	u64_stats_update_end(&stats64->syncp);
966 }
967 EXPORT_SYMBOL_GPL(qtnf_update_rx_stats);
968 
969 void qtnf_update_tx_stats(struct net_device *ndev, const struct sk_buff *skb)
970 {
971 	struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
972 	struct pcpu_sw_netstats *stats64;
973 
974 	if (unlikely(!vif || !vif->stats64)) {
975 		ndev->stats.tx_packets++;
976 		ndev->stats.tx_bytes += skb->len;
977 		return;
978 	}
979 
980 	stats64 = this_cpu_ptr(vif->stats64);
981 
982 	u64_stats_update_begin(&stats64->syncp);
983 	stats64->tx_packets++;
984 	stats64->tx_bytes += skb->len;
985 	u64_stats_update_end(&stats64->syncp);
986 }
987 EXPORT_SYMBOL_GPL(qtnf_update_tx_stats);
988 
989 struct dentry *qtnf_get_debugfs_dir(void)
990 {
991 	return qtnf_debugfs_dir;
992 }
993 EXPORT_SYMBOL_GPL(qtnf_get_debugfs_dir);
994 
995 static int __init qtnf_core_register(void)
996 {
997 	qtnf_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
998 
999 	if (IS_ERR(qtnf_debugfs_dir))
1000 		qtnf_debugfs_dir = NULL;
1001 
1002 	return 0;
1003 }
1004 
1005 static void __exit qtnf_core_exit(void)
1006 {
1007 	debugfs_remove(qtnf_debugfs_dir);
1008 }
1009 
1010 module_init(qtnf_core_register);
1011 module_exit(qtnf_core_exit);
1012 
1013 MODULE_AUTHOR("Quantenna Communications");
1014 MODULE_DESCRIPTION("Quantenna 802.11 wireless LAN FullMAC driver.");
1015 MODULE_LICENSE("GPL");
1016