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