Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / net / wireless / quantenna / qtnfmac / core.c
blob825b05dd3271bda11aca37ee0ed279dc061a446f
1 // SPDX-License-Identifier: GPL-2.0+
2 /* Copyright (c) 2015-2016 Quantenna Communications. All rights reserved. */
4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/if_ether.h>
7 #include <linux/nospec.h>
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"
18 #define QTNF_PRIMARY_VIF_IDX 0
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");
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");
28 static struct dentry *qtnf_debugfs_dir;
30 bool qtnf_slave_radar_get(void)
32 return slave_radar;
35 bool qtnf_dfs_offload_get(void)
37 return dfs_offload;
40 struct qtnf_wmac *qtnf_core_get_mac(const struct qtnf_bus *bus, u8 macid)
42 struct qtnf_wmac *mac = NULL;
44 if (macid >= QTNF_MAX_MAC) {
45 pr_err("invalid MAC index %u\n", macid);
46 return NULL;
49 macid = array_index_nospec(macid, QTNF_MAX_MAC);
50 mac = bus->mac[macid];
52 if (unlikely(!mac)) {
53 pr_err("MAC%u: not initialized\n", macid);
54 return NULL;
57 return mac;
60 /* Netdev handler for open.
62 static int qtnf_netdev_open(struct net_device *ndev)
64 netif_carrier_off(ndev);
65 qtnf_netdev_updown(ndev, 1);
66 return 0;
69 /* Netdev handler for close.
71 static int qtnf_netdev_close(struct net_device *ndev)
73 netif_carrier_off(ndev);
74 qtnf_virtual_intf_cleanup(ndev);
75 qtnf_netdev_updown(ndev, 0);
76 return 0;
79 static void qtnf_packet_send_hi_pri(struct sk_buff *skb)
81 struct qtnf_vif *vif = qtnf_netdev_get_priv(skb->dev);
83 skb_queue_tail(&vif->high_pri_tx_queue, skb);
84 queue_work(vif->mac->bus->hprio_workqueue, &vif->high_pri_tx_work);
87 /* Netdev handler for data transmission.
89 static netdev_tx_t
90 qtnf_netdev_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev)
92 struct qtnf_vif *vif;
93 struct qtnf_wmac *mac;
95 vif = qtnf_netdev_get_priv(ndev);
97 if (unlikely(skb->dev != ndev)) {
98 pr_err_ratelimited("invalid skb->dev");
99 dev_kfree_skb_any(skb);
100 return 0;
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;
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;
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;
124 /* tx path is enabled: reset vif timeout */
125 vif->cons_tx_timeout_cnt = 0;
127 if (unlikely(skb->protocol == htons(ETH_P_PAE))) {
128 qtnf_packet_send_hi_pri(skb);
129 dev_sw_netstats_tx_add(ndev, 1, skb->len);
130 return NETDEV_TX_OK;
133 return qtnf_bus_data_tx(mac->bus, skb, mac->macid, vif->vifid);
136 /* Netdev handler for transmission timeout.
138 static void qtnf_netdev_tx_timeout(struct net_device *ndev, unsigned int txqueue)
140 struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
141 struct qtnf_wmac *mac;
142 struct qtnf_bus *bus;
144 if (unlikely(!vif || !vif->mac || !vif->mac->bus))
145 return;
147 mac = vif->mac;
148 bus = mac->bus;
150 pr_warn("VIF%u.%u: Tx timeout- %lu\n", mac->macid, vif->vifid, jiffies);
152 qtnf_bus_data_tx_timeout(bus, ndev);
153 ndev->stats.tx_errors++;
155 if (++vif->cons_tx_timeout_cnt > QTNF_TX_TIMEOUT_TRSHLD) {
156 pr_err("Tx timeout threshold exceeded !\n");
157 pr_err("schedule interface %s reset !\n", netdev_name(ndev));
158 queue_work(bus->workqueue, &vif->reset_work);
162 static int qtnf_netdev_set_mac_address(struct net_device *ndev, void *addr)
164 struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
165 struct sockaddr *sa = addr;
166 int ret;
167 unsigned char old_addr[ETH_ALEN];
169 memcpy(old_addr, sa->sa_data, sizeof(old_addr));
171 ret = eth_mac_addr(ndev, sa);
172 if (ret)
173 return ret;
175 qtnf_scan_done(vif->mac, true);
177 ret = qtnf_cmd_send_change_intf_type(vif, vif->wdev.iftype,
178 vif->wdev.use_4addr,
179 sa->sa_data);
181 if (ret)
182 eth_hw_addr_set(ndev, old_addr);
184 return ret;
187 static int qtnf_netdev_port_parent_id(struct net_device *ndev,
188 struct netdev_phys_item_id *ppid)
190 const struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
191 const struct qtnf_bus *bus = vif->mac->bus;
193 ppid->id_len = sizeof(bus->hw_id);
194 memcpy(&ppid->id, bus->hw_id, ppid->id_len);
196 return 0;
199 /* Network device ops handlers */
200 const struct net_device_ops qtnf_netdev_ops = {
201 .ndo_open = qtnf_netdev_open,
202 .ndo_stop = qtnf_netdev_close,
203 .ndo_start_xmit = qtnf_netdev_hard_start_xmit,
204 .ndo_tx_timeout = qtnf_netdev_tx_timeout,
205 .ndo_set_mac_address = qtnf_netdev_set_mac_address,
206 .ndo_get_port_parent_id = qtnf_netdev_port_parent_id,
209 static int qtnf_mac_init_single_band(struct wiphy *wiphy,
210 struct qtnf_wmac *mac,
211 enum nl80211_band band)
213 int ret;
215 wiphy->bands[band] = kzalloc(sizeof(*wiphy->bands[band]), GFP_KERNEL);
216 if (!wiphy->bands[band])
217 return -ENOMEM;
219 wiphy->bands[band]->band = band;
221 ret = qtnf_cmd_band_info_get(mac, wiphy->bands[band]);
222 if (ret) {
223 pr_err("MAC%u: band %u: failed to get chans info: %d\n",
224 mac->macid, band, ret);
225 return ret;
228 qtnf_band_init_rates(wiphy->bands[band]);
230 return 0;
233 static int qtnf_mac_init_bands(struct qtnf_wmac *mac)
235 struct wiphy *wiphy = priv_to_wiphy(mac);
236 int ret = 0;
238 if (mac->macinfo.bands_cap & QLINK_BAND_2GHZ) {
239 ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_2GHZ);
240 if (ret)
241 goto out;
244 if (mac->macinfo.bands_cap & QLINK_BAND_5GHZ) {
245 ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_5GHZ);
246 if (ret)
247 goto out;
250 if (mac->macinfo.bands_cap & QLINK_BAND_60GHZ)
251 ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_60GHZ);
253 out:
254 return ret;
257 struct qtnf_vif *qtnf_mac_get_free_vif(struct qtnf_wmac *mac)
259 struct qtnf_vif *vif;
260 int i;
262 for (i = 0; i < QTNF_MAX_INTF; i++) {
263 vif = &mac->iflist[i];
264 if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)
265 return vif;
268 return NULL;
271 struct qtnf_vif *qtnf_mac_get_base_vif(struct qtnf_wmac *mac)
273 struct qtnf_vif *vif;
275 vif = &mac->iflist[QTNF_PRIMARY_VIF_IDX];
277 if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)
278 return NULL;
280 return vif;
283 void qtnf_mac_iface_comb_free(struct qtnf_wmac *mac)
285 struct ieee80211_iface_combination *comb;
286 int i;
288 if (mac->macinfo.if_comb) {
289 for (i = 0; i < mac->macinfo.n_if_comb; i++) {
290 comb = &mac->macinfo.if_comb[i];
291 kfree(comb->limits);
292 comb->limits = NULL;
295 kfree(mac->macinfo.if_comb);
296 mac->macinfo.if_comb = NULL;
300 void qtnf_mac_ext_caps_free(struct qtnf_wmac *mac)
302 if (mac->macinfo.extended_capabilities_len) {
303 kfree(mac->macinfo.extended_capabilities);
304 mac->macinfo.extended_capabilities = NULL;
306 kfree(mac->macinfo.extended_capabilities_mask);
307 mac->macinfo.extended_capabilities_mask = NULL;
309 mac->macinfo.extended_capabilities_len = 0;
313 static void qtnf_vif_reset_handler(struct work_struct *work)
315 struct qtnf_vif *vif = container_of(work, struct qtnf_vif, reset_work);
317 rtnl_lock();
319 if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED) {
320 rtnl_unlock();
321 return;
324 /* stop tx completely */
325 netif_tx_stop_all_queues(vif->netdev);
326 if (netif_carrier_ok(vif->netdev))
327 netif_carrier_off(vif->netdev);
329 qtnf_cfg80211_vif_reset(vif);
331 rtnl_unlock();
334 static void qtnf_mac_init_primary_intf(struct qtnf_wmac *mac)
336 struct qtnf_vif *vif = &mac->iflist[QTNF_PRIMARY_VIF_IDX];
338 vif->wdev.iftype = NL80211_IFTYPE_STATION;
339 vif->bss_priority = QTNF_DEF_BSS_PRIORITY;
340 vif->wdev.wiphy = priv_to_wiphy(mac);
341 INIT_WORK(&vif->reset_work, qtnf_vif_reset_handler);
342 vif->cons_tx_timeout_cnt = 0;
345 static void qtnf_mac_scan_finish(struct qtnf_wmac *mac, bool aborted)
347 struct cfg80211_scan_info info = {
348 .aborted = aborted,
351 mutex_lock(&mac->mac_lock);
353 if (mac->scan_req) {
354 cfg80211_scan_done(mac->scan_req, &info);
355 mac->scan_req = NULL;
358 mutex_unlock(&mac->mac_lock);
361 void qtnf_scan_done(struct qtnf_wmac *mac, bool aborted)
363 cancel_delayed_work_sync(&mac->scan_timeout);
364 qtnf_mac_scan_finish(mac, aborted);
367 static void qtnf_mac_scan_timeout(struct work_struct *work)
369 struct qtnf_wmac *mac =
370 container_of(work, struct qtnf_wmac, scan_timeout.work);
372 pr_warn("MAC%d: scan timed out\n", mac->macid);
373 qtnf_mac_scan_finish(mac, true);
376 static void qtnf_vif_send_data_high_pri(struct work_struct *work)
378 struct qtnf_vif *vif =
379 container_of(work, struct qtnf_vif, high_pri_tx_work);
380 struct sk_buff *skb;
382 if (!vif->netdev ||
383 vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)
384 return;
386 while ((skb = skb_dequeue(&vif->high_pri_tx_queue))) {
387 qtnf_cmd_send_frame(vif, 0, QLINK_FRAME_TX_FLAG_8023,
388 0, skb->data, skb->len);
389 dev_kfree_skb_any(skb);
393 static struct qtnf_wmac *qtnf_core_mac_alloc(struct qtnf_bus *bus,
394 unsigned int macid)
396 struct platform_device *pdev = NULL;
397 struct qtnf_wmac *mac;
398 struct qtnf_vif *vif;
399 struct wiphy *wiphy;
400 unsigned int i;
402 if (bus->hw_info.num_mac > 1) {
403 pdev = platform_device_register_data(bus->dev,
404 dev_name(bus->dev),
405 macid, NULL, 0);
406 if (IS_ERR(pdev))
407 return ERR_PTR(-EINVAL);
410 wiphy = qtnf_wiphy_allocate(bus, pdev);
411 if (!wiphy) {
412 if (pdev)
413 platform_device_unregister(pdev);
414 return ERR_PTR(-ENOMEM);
417 mac = wiphy_priv(wiphy);
419 mac->macid = macid;
420 mac->pdev = pdev;
421 mac->bus = bus;
422 mutex_init(&mac->mac_lock);
423 INIT_DELAYED_WORK(&mac->scan_timeout, qtnf_mac_scan_timeout);
425 for (i = 0; i < QTNF_MAX_INTF; i++) {
426 vif = &mac->iflist[i];
428 memset(vif, 0, sizeof(*vif));
429 vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
430 vif->mac = mac;
431 vif->vifid = i;
432 qtnf_sta_list_init(&vif->sta_list);
433 INIT_WORK(&vif->high_pri_tx_work, qtnf_vif_send_data_high_pri);
434 skb_queue_head_init(&vif->high_pri_tx_queue);
437 qtnf_mac_init_primary_intf(mac);
438 bus->mac[macid] = mac;
440 return mac;
443 static const struct ethtool_ops qtnf_ethtool_ops = {
444 .get_drvinfo = cfg80211_get_drvinfo,
447 int qtnf_core_net_attach(struct qtnf_wmac *mac, struct qtnf_vif *vif,
448 const char *name, unsigned char name_assign_type)
450 struct wiphy *wiphy = priv_to_wiphy(mac);
451 struct net_device *dev;
452 void *qdev_vif;
453 int ret;
455 dev = alloc_netdev_mqs(sizeof(struct qtnf_vif *), name,
456 name_assign_type, ether_setup, 1, 1);
457 if (!dev)
458 return -ENOMEM;
460 vif->netdev = dev;
462 dev->netdev_ops = &qtnf_netdev_ops;
463 dev->needs_free_netdev = true;
464 dev_net_set(dev, wiphy_net(wiphy));
465 dev->ieee80211_ptr = &vif->wdev;
466 eth_hw_addr_set(dev, vif->mac_addr);
467 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
468 dev->watchdog_timeo = QTNF_DEF_WDOG_TIMEOUT;
469 dev->tx_queue_len = 100;
470 dev->ethtool_ops = &qtnf_ethtool_ops;
471 dev->pcpu_stat_type = NETDEV_PCPU_STAT_TSTATS;
473 if (qtnf_hwcap_is_set(&mac->bus->hw_info, QLINK_HW_CAPAB_HW_BRIDGE))
474 dev->needed_tailroom = sizeof(struct qtnf_frame_meta_info);
476 qdev_vif = netdev_priv(dev);
477 *((void **)qdev_vif) = vif;
479 SET_NETDEV_DEV(dev, wiphy_dev(wiphy));
481 ret = cfg80211_register_netdevice(dev);
482 if (ret) {
483 free_netdev(dev);
484 vif->netdev = NULL;
487 return ret;
490 static void qtnf_core_mac_detach(struct qtnf_bus *bus, unsigned int macid)
492 struct qtnf_wmac *mac;
493 struct wiphy *wiphy;
494 struct qtnf_vif *vif;
495 unsigned int i;
496 enum nl80211_band band;
498 mac = bus->mac[macid];
500 if (!mac)
501 return;
503 wiphy = priv_to_wiphy(mac);
505 for (i = 0; i < QTNF_MAX_INTF; i++) {
506 vif = &mac->iflist[i];
507 rtnl_lock();
508 if (vif->netdev &&
509 vif->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED) {
510 qtnf_virtual_intf_cleanup(vif->netdev);
511 qtnf_del_virtual_intf(wiphy, &vif->wdev);
513 rtnl_unlock();
514 qtnf_sta_list_free(&vif->sta_list);
517 if (mac->wiphy_registered)
518 wiphy_unregister(wiphy);
520 for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; ++band) {
521 if (!wiphy->bands[band])
522 continue;
524 kfree((__force void *)wiphy->bands[band]->iftype_data);
525 wiphy->bands[band]->n_iftype_data = 0;
527 kfree(wiphy->bands[band]->channels);
528 wiphy->bands[band]->n_channels = 0;
530 kfree(wiphy->bands[band]);
531 wiphy->bands[band] = NULL;
534 platform_device_unregister(mac->pdev);
535 qtnf_mac_iface_comb_free(mac);
536 qtnf_mac_ext_caps_free(mac);
537 kfree(mac->macinfo.wowlan);
538 kfree(mac->rd);
539 mac->rd = NULL;
540 wiphy_free(wiphy);
541 bus->mac[macid] = NULL;
544 static int qtnf_core_mac_attach(struct qtnf_bus *bus, unsigned int macid)
546 struct qtnf_wmac *mac;
547 struct qtnf_vif *vif;
548 int ret;
550 if (!(bus->hw_info.mac_bitmap & BIT(macid))) {
551 pr_info("MAC%u is not active in FW\n", macid);
552 return 0;
555 mac = qtnf_core_mac_alloc(bus, macid);
556 if (IS_ERR(mac)) {
557 pr_err("MAC%u allocation failed\n", macid);
558 return PTR_ERR(mac);
561 vif = qtnf_mac_get_base_vif(mac);
562 if (!vif) {
563 pr_err("MAC%u: primary VIF is not ready\n", macid);
564 ret = -EFAULT;
565 goto error;
568 ret = qtnf_cmd_send_add_intf(vif, vif->wdev.iftype,
569 vif->wdev.use_4addr, vif->mac_addr);
570 if (ret) {
571 pr_err("MAC%u: failed to add VIF\n", macid);
572 goto error;
575 ret = qtnf_cmd_get_mac_info(mac);
576 if (ret) {
577 pr_err("MAC%u: failed to get MAC info\n", macid);
578 goto error_del_vif;
581 /* Use MAC address of the first active radio as a unique device ID */
582 if (is_zero_ether_addr(mac->bus->hw_id))
583 ether_addr_copy(mac->bus->hw_id, mac->macaddr);
585 ret = qtnf_mac_init_bands(mac);
586 if (ret) {
587 pr_err("MAC%u: failed to init bands\n", macid);
588 goto error_del_vif;
591 ret = qtnf_wiphy_register(&bus->hw_info, mac);
592 if (ret) {
593 pr_err("MAC%u: wiphy registration failed\n", macid);
594 goto error_del_vif;
597 mac->wiphy_registered = 1;
599 rtnl_lock();
600 wiphy_lock(priv_to_wiphy(mac));
601 ret = qtnf_core_net_attach(mac, vif, "wlan%d", NET_NAME_ENUM);
602 wiphy_unlock(priv_to_wiphy(mac));
603 rtnl_unlock();
605 if (ret) {
606 pr_err("MAC%u: failed to attach netdev\n", macid);
607 goto error_del_vif;
610 if (qtnf_hwcap_is_set(&bus->hw_info, QLINK_HW_CAPAB_HW_BRIDGE)) {
611 ret = qtnf_cmd_netdev_changeupper(vif, vif->netdev->ifindex);
612 if (ret)
613 goto error;
616 pr_debug("MAC%u initialized\n", macid);
618 return 0;
620 error_del_vif:
621 qtnf_cmd_send_del_intf(vif);
622 vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
623 error:
624 qtnf_core_mac_detach(bus, macid);
625 return ret;
628 bool qtnf_netdev_is_qtn(const struct net_device *ndev)
630 return ndev->netdev_ops == &qtnf_netdev_ops;
633 static int qtnf_check_br_ports(struct net_device *dev,
634 struct netdev_nested_priv *priv)
636 struct net_device *ndev = (struct net_device *)priv->data;
638 if (dev != ndev && netdev_port_same_parent_id(dev, ndev))
639 return -ENOTSUPP;
641 return 0;
644 static int qtnf_core_netdevice_event(struct notifier_block *nb,
645 unsigned long event, void *ptr)
647 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
648 const struct netdev_notifier_changeupper_info *info;
649 struct netdev_nested_priv priv = {
650 .data = (void *)ndev,
652 struct net_device *brdev;
653 struct qtnf_vif *vif;
654 struct qtnf_bus *bus;
655 int br_domain;
656 int ret = 0;
658 if (!qtnf_netdev_is_qtn(ndev))
659 return NOTIFY_DONE;
661 if (!net_eq(dev_net(ndev), &init_net))
662 return NOTIFY_OK;
664 vif = qtnf_netdev_get_priv(ndev);
665 bus = vif->mac->bus;
667 switch (event) {
668 case NETDEV_CHANGEUPPER:
669 info = ptr;
670 brdev = info->upper_dev;
672 if (!netif_is_bridge_master(brdev))
673 break;
675 pr_debug("[VIF%u.%u] change bridge: %s %s\n",
676 vif->mac->macid, vif->vifid, netdev_name(brdev),
677 info->linking ? "add" : "del");
679 if (IS_ENABLED(CONFIG_NET_SWITCHDEV) &&
680 qtnf_hwcap_is_set(&bus->hw_info,
681 QLINK_HW_CAPAB_HW_BRIDGE)) {
682 if (info->linking)
683 br_domain = brdev->ifindex;
684 else
685 br_domain = ndev->ifindex;
687 ret = qtnf_cmd_netdev_changeupper(vif, br_domain);
688 } else {
689 ret = netdev_walk_all_lower_dev(brdev,
690 qtnf_check_br_ports,
691 &priv);
694 break;
695 default:
696 break;
699 return notifier_from_errno(ret);
702 int qtnf_core_attach(struct qtnf_bus *bus)
704 unsigned int i;
705 int ret;
707 qtnf_trans_init(bus);
708 qtnf_bus_data_rx_start(bus);
710 bus->workqueue = alloc_ordered_workqueue("QTNF_BUS", 0);
711 if (!bus->workqueue) {
712 pr_err("failed to alloc main workqueue\n");
713 ret = -ENOMEM;
714 goto error;
717 bus->hprio_workqueue = alloc_workqueue("QTNF_HPRI", WQ_HIGHPRI, 0);
718 if (!bus->hprio_workqueue) {
719 pr_err("failed to alloc high prio workqueue\n");
720 ret = -ENOMEM;
721 goto error;
724 INIT_WORK(&bus->event_work, qtnf_event_work_handler);
726 ret = qtnf_cmd_send_init_fw(bus);
727 if (ret) {
728 pr_err("failed to init FW: %d\n", ret);
729 goto error;
732 if (QLINK_VER_MAJOR(bus->hw_info.ql_proto_ver) !=
733 QLINK_PROTO_VER_MAJOR) {
734 pr_err("qlink driver vs FW version mismatch: %u vs %u\n",
735 QLINK_PROTO_VER_MAJOR,
736 QLINK_VER_MAJOR(bus->hw_info.ql_proto_ver));
737 ret = -EPROTONOSUPPORT;
738 goto error;
741 bus->fw_state = QTNF_FW_STATE_ACTIVE;
742 ret = qtnf_cmd_get_hw_info(bus);
743 if (ret) {
744 pr_err("failed to get HW info: %d\n", ret);
745 goto error;
748 if (qtnf_hwcap_is_set(&bus->hw_info, QLINK_HW_CAPAB_HW_BRIDGE) &&
749 bus->bus_ops->data_tx_use_meta_set)
750 bus->bus_ops->data_tx_use_meta_set(bus, true);
752 if (bus->hw_info.num_mac > QTNF_MAX_MAC) {
753 pr_err("no support for number of MACs=%u\n",
754 bus->hw_info.num_mac);
755 ret = -ERANGE;
756 goto error;
759 for (i = 0; i < bus->hw_info.num_mac; i++) {
760 ret = qtnf_core_mac_attach(bus, i);
762 if (ret) {
763 pr_err("MAC%u: attach failed: %d\n", i, ret);
764 goto error;
768 bus->netdev_nb.notifier_call = qtnf_core_netdevice_event;
769 ret = register_netdevice_notifier(&bus->netdev_nb);
770 if (ret) {
771 pr_err("failed to register netdev notifier: %d\n", ret);
772 goto error;
775 bus->fw_state = QTNF_FW_STATE_RUNNING;
776 return 0;
778 error:
779 qtnf_core_detach(bus);
780 return ret;
782 EXPORT_SYMBOL_GPL(qtnf_core_attach);
784 void qtnf_core_detach(struct qtnf_bus *bus)
786 unsigned int macid;
788 unregister_netdevice_notifier(&bus->netdev_nb);
789 qtnf_bus_data_rx_stop(bus);
791 for (macid = 0; macid < QTNF_MAX_MAC; macid++)
792 qtnf_core_mac_detach(bus, macid);
794 if (qtnf_fw_is_up(bus))
795 qtnf_cmd_send_deinit_fw(bus);
797 bus->fw_state = QTNF_FW_STATE_DETACHED;
799 if (bus->workqueue) {
800 destroy_workqueue(bus->workqueue);
801 bus->workqueue = NULL;
804 if (bus->hprio_workqueue) {
805 destroy_workqueue(bus->hprio_workqueue);
806 bus->hprio_workqueue = NULL;
809 qtnf_trans_free(bus);
811 EXPORT_SYMBOL_GPL(qtnf_core_detach);
813 static inline int qtnf_is_frame_meta_magic_valid(struct qtnf_frame_meta_info *m)
815 return m->magic_s == HBM_FRAME_META_MAGIC_PATTERN_S &&
816 m->magic_e == HBM_FRAME_META_MAGIC_PATTERN_E;
819 struct net_device *qtnf_classify_skb(struct qtnf_bus *bus, struct sk_buff *skb)
821 struct qtnf_frame_meta_info *meta;
822 struct net_device *ndev = NULL;
823 struct qtnf_wmac *mac;
824 struct qtnf_vif *vif;
826 if (unlikely(bus->fw_state != QTNF_FW_STATE_RUNNING))
827 return NULL;
829 meta = (struct qtnf_frame_meta_info *)
830 (skb_tail_pointer(skb) - sizeof(*meta));
832 if (unlikely(!qtnf_is_frame_meta_magic_valid(meta))) {
833 pr_err_ratelimited("invalid magic 0x%x:0x%x\n",
834 meta->magic_s, meta->magic_e);
835 goto out;
838 if (unlikely(meta->macid >= QTNF_MAX_MAC)) {
839 pr_err_ratelimited("invalid mac(%u)\n", meta->macid);
840 goto out;
843 if (unlikely(meta->ifidx >= QTNF_MAX_INTF)) {
844 pr_err_ratelimited("invalid vif(%u)\n", meta->ifidx);
845 goto out;
848 mac = bus->mac[meta->macid];
850 if (unlikely(!mac)) {
851 pr_err_ratelimited("mac(%d) does not exist\n", meta->macid);
852 goto out;
855 vif = &mac->iflist[meta->ifidx];
857 if (unlikely(vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)) {
858 pr_err_ratelimited("vif(%u) does not exists\n", meta->ifidx);
859 goto out;
862 ndev = vif->netdev;
864 if (unlikely(!ndev)) {
865 pr_err_ratelimited("netdev for wlan%u.%u does not exists\n",
866 meta->macid, meta->ifidx);
867 goto out;
870 __skb_trim(skb, skb->len - sizeof(*meta));
871 /* Firmware always handles packets that require flooding */
872 qtnfmac_switch_mark_skb_flooded(skb);
874 out:
875 return ndev;
877 EXPORT_SYMBOL_GPL(qtnf_classify_skb);
879 void qtnf_wake_all_queues(struct net_device *ndev)
881 struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
882 struct qtnf_wmac *mac;
883 struct qtnf_bus *bus;
884 int macid;
885 int i;
887 if (unlikely(!vif || !vif->mac || !vif->mac->bus))
888 return;
890 bus = vif->mac->bus;
892 for (macid = 0; macid < QTNF_MAX_MAC; macid++) {
893 if (!(bus->hw_info.mac_bitmap & BIT(macid)))
894 continue;
896 mac = bus->mac[macid];
897 for (i = 0; i < QTNF_MAX_INTF; i++) {
898 vif = &mac->iflist[i];
899 if (vif->netdev && netif_queue_stopped(vif->netdev))
900 netif_tx_wake_all_queues(vif->netdev);
904 EXPORT_SYMBOL_GPL(qtnf_wake_all_queues);
906 struct dentry *qtnf_get_debugfs_dir(void)
908 return qtnf_debugfs_dir;
910 EXPORT_SYMBOL_GPL(qtnf_get_debugfs_dir);
912 static int __init qtnf_core_register(void)
914 qtnf_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
916 if (IS_ERR(qtnf_debugfs_dir))
917 qtnf_debugfs_dir = NULL;
919 return 0;
922 static void __exit qtnf_core_exit(void)
924 debugfs_remove(qtnf_debugfs_dir);
927 module_init(qtnf_core_register);
928 module_exit(qtnf_core_exit);
930 MODULE_AUTHOR("Quantenna Communications");
931 MODULE_DESCRIPTION("Quantenna 802.11 wireless LAN FullMAC driver.");
932 MODULE_LICENSE("GPL");