Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /* Copyright (c) 2015-2016 Quantenna Communications. All rights reserved. */
0003 
0004 #include <linux/kernel.h>
0005 #include <linux/module.h>
0006 #include <linux/if_ether.h>
0007 #include <linux/nospec.h>
0008 
0009 #include "core.h"
0010 #include "bus.h"
0011 #include "trans.h"
0012 #include "commands.h"
0013 #include "cfg80211.h"
0014 #include "event.h"
0015 #include "util.h"
0016 #include "switchdev.h"
0017 
0018 #define QTNF_PRIMARY_VIF_IDX    0
0019 
0020 static bool slave_radar = true;
0021 module_param(slave_radar, bool, 0644);
0022 MODULE_PARM_DESC(slave_radar, "set 0 to disable radar detection in slave mode");
0023 
0024 static bool dfs_offload;
0025 module_param(dfs_offload, bool, 0644);
0026 MODULE_PARM_DESC(dfs_offload, "set 1 to enable DFS offload to firmware");
0027 
0028 static struct dentry *qtnf_debugfs_dir;
0029 
0030 bool qtnf_slave_radar_get(void)
0031 {
0032     return slave_radar;
0033 }
0034 
0035 bool qtnf_dfs_offload_get(void)
0036 {
0037     return dfs_offload;
0038 }
0039 
0040 struct qtnf_wmac *qtnf_core_get_mac(const struct qtnf_bus *bus, u8 macid)
0041 {
0042     struct qtnf_wmac *mac = NULL;
0043 
0044     if (macid >= QTNF_MAX_MAC) {
0045         pr_err("invalid MAC index %u\n", macid);
0046         return NULL;
0047     }
0048 
0049     macid = array_index_nospec(macid, QTNF_MAX_MAC);
0050     mac = bus->mac[macid];
0051 
0052     if (unlikely(!mac)) {
0053         pr_err("MAC%u: not initialized\n", macid);
0054         return NULL;
0055     }
0056 
0057     return mac;
0058 }
0059 
0060 /* Netdev handler for open.
0061  */
0062 static int qtnf_netdev_open(struct net_device *ndev)
0063 {
0064     netif_carrier_off(ndev);
0065     qtnf_netdev_updown(ndev, 1);
0066     return 0;
0067 }
0068 
0069 /* Netdev handler for close.
0070  */
0071 static int qtnf_netdev_close(struct net_device *ndev)
0072 {
0073     netif_carrier_off(ndev);
0074     qtnf_virtual_intf_cleanup(ndev);
0075     qtnf_netdev_updown(ndev, 0);
0076     return 0;
0077 }
0078 
0079 static void qtnf_packet_send_hi_pri(struct sk_buff *skb)
0080 {
0081     struct qtnf_vif *vif = qtnf_netdev_get_priv(skb->dev);
0082 
0083     skb_queue_tail(&vif->high_pri_tx_queue, skb);
0084     queue_work(vif->mac->bus->hprio_workqueue, &vif->high_pri_tx_work);
0085 }
0086 
0087 /* Netdev handler for data transmission.
0088  */
0089 static netdev_tx_t
0090 qtnf_netdev_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev)
0091 {
0092     struct qtnf_vif *vif;
0093     struct qtnf_wmac *mac;
0094 
0095     vif = qtnf_netdev_get_priv(ndev);
0096 
0097     if (unlikely(skb->dev != ndev)) {
0098         pr_err_ratelimited("invalid skb->dev");
0099         dev_kfree_skb_any(skb);
0100         return 0;
0101     }
0102 
0103     if (unlikely(vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)) {
0104         pr_err_ratelimited("%s: VIF not initialized\n", ndev->name);
0105         dev_kfree_skb_any(skb);
0106         return 0;
0107     }
0108 
0109     mac = vif->mac;
0110     if (unlikely(!mac)) {
0111         pr_err_ratelimited("%s: NULL mac pointer", ndev->name);
0112         dev_kfree_skb_any(skb);
0113         return 0;
0114     }
0115 
0116     if (!skb->len || (skb->len > ETH_FRAME_LEN)) {
0117         pr_err_ratelimited("%s: invalid skb len %d\n", ndev->name,
0118                    skb->len);
0119         dev_kfree_skb_any(skb);
0120         ndev->stats.tx_dropped++;
0121         return 0;
0122     }
0123 
0124     /* tx path is enabled: reset vif timeout */
0125     vif->cons_tx_timeout_cnt = 0;
0126 
0127     if (unlikely(skb->protocol == htons(ETH_P_PAE))) {
0128         qtnf_packet_send_hi_pri(skb);
0129         dev_sw_netstats_tx_add(ndev, 1, skb->len);
0130         return NETDEV_TX_OK;
0131     }
0132 
0133     return qtnf_bus_data_tx(mac->bus, skb, mac->macid, vif->vifid);
0134 }
0135 
0136 /* Netdev handler for transmission timeout.
0137  */
0138 static void qtnf_netdev_tx_timeout(struct net_device *ndev, unsigned int txqueue)
0139 {
0140     struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
0141     struct qtnf_wmac *mac;
0142     struct qtnf_bus *bus;
0143 
0144     if (unlikely(!vif || !vif->mac || !vif->mac->bus))
0145         return;
0146 
0147     mac = vif->mac;
0148     bus = mac->bus;
0149 
0150     pr_warn("VIF%u.%u: Tx timeout- %lu\n", mac->macid, vif->vifid, jiffies);
0151 
0152     qtnf_bus_data_tx_timeout(bus, ndev);
0153     ndev->stats.tx_errors++;
0154 
0155     if (++vif->cons_tx_timeout_cnt > QTNF_TX_TIMEOUT_TRSHLD) {
0156         pr_err("Tx timeout threshold exceeded !\n");
0157         pr_err("schedule interface %s reset !\n", netdev_name(ndev));
0158         queue_work(bus->workqueue, &vif->reset_work);
0159     }
0160 }
0161 
0162 static int qtnf_netdev_set_mac_address(struct net_device *ndev, void *addr)
0163 {
0164     struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
0165     struct sockaddr *sa = addr;
0166     int ret;
0167     unsigned char old_addr[ETH_ALEN];
0168 
0169     memcpy(old_addr, sa->sa_data, sizeof(old_addr));
0170 
0171     ret = eth_mac_addr(ndev, sa);
0172     if (ret)
0173         return ret;
0174 
0175     qtnf_scan_done(vif->mac, true);
0176 
0177     ret = qtnf_cmd_send_change_intf_type(vif, vif->wdev.iftype,
0178                          vif->wdev.use_4addr,
0179                          sa->sa_data);
0180 
0181     if (ret)
0182         eth_hw_addr_set(ndev, old_addr);
0183 
0184     return ret;
0185 }
0186 
0187 static int qtnf_netdev_port_parent_id(struct net_device *ndev,
0188                       struct netdev_phys_item_id *ppid)
0189 {
0190     const struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
0191     const struct qtnf_bus *bus = vif->mac->bus;
0192 
0193     ppid->id_len = sizeof(bus->hw_id);
0194     memcpy(&ppid->id, bus->hw_id, ppid->id_len);
0195 
0196     return 0;
0197 }
0198 
0199 static int qtnf_netdev_alloc_pcpu_stats(struct net_device *dev)
0200 {
0201     dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
0202 
0203     return dev->tstats ? 0 : -ENOMEM;
0204 }
0205 
0206 static void qtnf_netdev_free_pcpu_stats(struct net_device *dev)
0207 {
0208     free_percpu(dev->tstats);
0209 }
0210 
0211 /* Network device ops handlers */
0212 const struct net_device_ops qtnf_netdev_ops = {
0213     .ndo_init = qtnf_netdev_alloc_pcpu_stats,
0214     .ndo_uninit = qtnf_netdev_free_pcpu_stats,
0215     .ndo_open = qtnf_netdev_open,
0216     .ndo_stop = qtnf_netdev_close,
0217     .ndo_start_xmit = qtnf_netdev_hard_start_xmit,
0218     .ndo_tx_timeout = qtnf_netdev_tx_timeout,
0219     .ndo_get_stats64 = dev_get_tstats64,
0220     .ndo_set_mac_address = qtnf_netdev_set_mac_address,
0221     .ndo_get_port_parent_id = qtnf_netdev_port_parent_id,
0222 };
0223 
0224 static int qtnf_mac_init_single_band(struct wiphy *wiphy,
0225                      struct qtnf_wmac *mac,
0226                      enum nl80211_band band)
0227 {
0228     int ret;
0229 
0230     wiphy->bands[band] = kzalloc(sizeof(*wiphy->bands[band]), GFP_KERNEL);
0231     if (!wiphy->bands[band])
0232         return -ENOMEM;
0233 
0234     wiphy->bands[band]->band = band;
0235 
0236     ret = qtnf_cmd_band_info_get(mac, wiphy->bands[band]);
0237     if (ret) {
0238         pr_err("MAC%u: band %u: failed to get chans info: %d\n",
0239                mac->macid, band, ret);
0240         return ret;
0241     }
0242 
0243     qtnf_band_init_rates(wiphy->bands[band]);
0244 
0245     return 0;
0246 }
0247 
0248 static int qtnf_mac_init_bands(struct qtnf_wmac *mac)
0249 {
0250     struct wiphy *wiphy = priv_to_wiphy(mac);
0251     int ret = 0;
0252 
0253     if (mac->macinfo.bands_cap & QLINK_BAND_2GHZ) {
0254         ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_2GHZ);
0255         if (ret)
0256             goto out;
0257     }
0258 
0259     if (mac->macinfo.bands_cap & QLINK_BAND_5GHZ) {
0260         ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_5GHZ);
0261         if (ret)
0262             goto out;
0263     }
0264 
0265     if (mac->macinfo.bands_cap & QLINK_BAND_60GHZ)
0266         ret = qtnf_mac_init_single_band(wiphy, mac, NL80211_BAND_60GHZ);
0267 
0268 out:
0269     return ret;
0270 }
0271 
0272 struct qtnf_vif *qtnf_mac_get_free_vif(struct qtnf_wmac *mac)
0273 {
0274     struct qtnf_vif *vif;
0275     int i;
0276 
0277     for (i = 0; i < QTNF_MAX_INTF; i++) {
0278         vif = &mac->iflist[i];
0279         if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)
0280             return vif;
0281     }
0282 
0283     return NULL;
0284 }
0285 
0286 struct qtnf_vif *qtnf_mac_get_base_vif(struct qtnf_wmac *mac)
0287 {
0288     struct qtnf_vif *vif;
0289 
0290     vif = &mac->iflist[QTNF_PRIMARY_VIF_IDX];
0291 
0292     if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)
0293         return NULL;
0294 
0295     return vif;
0296 }
0297 
0298 void qtnf_mac_iface_comb_free(struct qtnf_wmac *mac)
0299 {
0300     struct ieee80211_iface_combination *comb;
0301     int i;
0302 
0303     if (mac->macinfo.if_comb) {
0304         for (i = 0; i < mac->macinfo.n_if_comb; i++) {
0305             comb = &mac->macinfo.if_comb[i];
0306             kfree(comb->limits);
0307             comb->limits = NULL;
0308         }
0309 
0310         kfree(mac->macinfo.if_comb);
0311         mac->macinfo.if_comb = NULL;
0312     }
0313 }
0314 
0315 void qtnf_mac_ext_caps_free(struct qtnf_wmac *mac)
0316 {
0317     if (mac->macinfo.extended_capabilities_len) {
0318         kfree(mac->macinfo.extended_capabilities);
0319         mac->macinfo.extended_capabilities = NULL;
0320 
0321         kfree(mac->macinfo.extended_capabilities_mask);
0322         mac->macinfo.extended_capabilities_mask = NULL;
0323 
0324         mac->macinfo.extended_capabilities_len = 0;
0325     }
0326 }
0327 
0328 static void qtnf_vif_reset_handler(struct work_struct *work)
0329 {
0330     struct qtnf_vif *vif = container_of(work, struct qtnf_vif, reset_work);
0331 
0332     rtnl_lock();
0333 
0334     if (vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED) {
0335         rtnl_unlock();
0336         return;
0337     }
0338 
0339     /* stop tx completely */
0340     netif_tx_stop_all_queues(vif->netdev);
0341     if (netif_carrier_ok(vif->netdev))
0342         netif_carrier_off(vif->netdev);
0343 
0344     qtnf_cfg80211_vif_reset(vif);
0345 
0346     rtnl_unlock();
0347 }
0348 
0349 static void qtnf_mac_init_primary_intf(struct qtnf_wmac *mac)
0350 {
0351     struct qtnf_vif *vif = &mac->iflist[QTNF_PRIMARY_VIF_IDX];
0352 
0353     vif->wdev.iftype = NL80211_IFTYPE_STATION;
0354     vif->bss_priority = QTNF_DEF_BSS_PRIORITY;
0355     vif->wdev.wiphy = priv_to_wiphy(mac);
0356     INIT_WORK(&vif->reset_work, qtnf_vif_reset_handler);
0357     vif->cons_tx_timeout_cnt = 0;
0358 }
0359 
0360 static void qtnf_mac_scan_finish(struct qtnf_wmac *mac, bool aborted)
0361 {
0362     struct cfg80211_scan_info info = {
0363         .aborted = aborted,
0364     };
0365 
0366     mutex_lock(&mac->mac_lock);
0367 
0368     if (mac->scan_req) {
0369         cfg80211_scan_done(mac->scan_req, &info);
0370         mac->scan_req = NULL;
0371     }
0372 
0373     mutex_unlock(&mac->mac_lock);
0374 }
0375 
0376 void qtnf_scan_done(struct qtnf_wmac *mac, bool aborted)
0377 {
0378     cancel_delayed_work_sync(&mac->scan_timeout);
0379     qtnf_mac_scan_finish(mac, aborted);
0380 }
0381 
0382 static void qtnf_mac_scan_timeout(struct work_struct *work)
0383 {
0384     struct qtnf_wmac *mac =
0385         container_of(work, struct qtnf_wmac, scan_timeout.work);
0386 
0387     pr_warn("MAC%d: scan timed out\n", mac->macid);
0388     qtnf_mac_scan_finish(mac, true);
0389 }
0390 
0391 static void qtnf_vif_send_data_high_pri(struct work_struct *work)
0392 {
0393     struct qtnf_vif *vif =
0394         container_of(work, struct qtnf_vif, high_pri_tx_work);
0395     struct sk_buff *skb;
0396 
0397     if (!vif->netdev ||
0398         vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)
0399         return;
0400 
0401     while ((skb = skb_dequeue(&vif->high_pri_tx_queue))) {
0402         qtnf_cmd_send_frame(vif, 0, QLINK_FRAME_TX_FLAG_8023,
0403                     0, skb->data, skb->len);
0404         dev_kfree_skb_any(skb);
0405     }
0406 }
0407 
0408 static struct qtnf_wmac *qtnf_core_mac_alloc(struct qtnf_bus *bus,
0409                          unsigned int macid)
0410 {
0411     struct platform_device *pdev = NULL;
0412     struct qtnf_wmac *mac;
0413     struct qtnf_vif *vif;
0414     struct wiphy *wiphy;
0415     unsigned int i;
0416 
0417     if (bus->hw_info.num_mac > 1) {
0418         pdev = platform_device_register_data(bus->dev,
0419                              dev_name(bus->dev),
0420                              macid, NULL, 0);
0421         if (IS_ERR(pdev))
0422             return ERR_PTR(-EINVAL);
0423     }
0424 
0425     wiphy = qtnf_wiphy_allocate(bus, pdev);
0426     if (!wiphy) {
0427         if (pdev)
0428             platform_device_unregister(pdev);
0429         return ERR_PTR(-ENOMEM);
0430     }
0431 
0432     mac = wiphy_priv(wiphy);
0433 
0434     mac->macid = macid;
0435     mac->pdev = pdev;
0436     mac->bus = bus;
0437     mutex_init(&mac->mac_lock);
0438     INIT_DELAYED_WORK(&mac->scan_timeout, qtnf_mac_scan_timeout);
0439 
0440     for (i = 0; i < QTNF_MAX_INTF; i++) {
0441         vif = &mac->iflist[i];
0442 
0443         memset(vif, 0, sizeof(*vif));
0444         vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
0445         vif->mac = mac;
0446         vif->vifid = i;
0447         qtnf_sta_list_init(&vif->sta_list);
0448         INIT_WORK(&vif->high_pri_tx_work, qtnf_vif_send_data_high_pri);
0449         skb_queue_head_init(&vif->high_pri_tx_queue);
0450     }
0451 
0452     qtnf_mac_init_primary_intf(mac);
0453     bus->mac[macid] = mac;
0454 
0455     return mac;
0456 }
0457 
0458 static const struct ethtool_ops qtnf_ethtool_ops = {
0459     .get_drvinfo = cfg80211_get_drvinfo,
0460 };
0461 
0462 int qtnf_core_net_attach(struct qtnf_wmac *mac, struct qtnf_vif *vif,
0463              const char *name, unsigned char name_assign_type)
0464 {
0465     struct wiphy *wiphy = priv_to_wiphy(mac);
0466     struct net_device *dev;
0467     void *qdev_vif;
0468     int ret;
0469 
0470     dev = alloc_netdev_mqs(sizeof(struct qtnf_vif *), name,
0471                    name_assign_type, ether_setup, 1, 1);
0472     if (!dev)
0473         return -ENOMEM;
0474 
0475     vif->netdev = dev;
0476 
0477     dev->netdev_ops = &qtnf_netdev_ops;
0478     dev->needs_free_netdev = true;
0479     dev_net_set(dev, wiphy_net(wiphy));
0480     dev->ieee80211_ptr = &vif->wdev;
0481     eth_hw_addr_set(dev, vif->mac_addr);
0482     dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
0483     dev->watchdog_timeo = QTNF_DEF_WDOG_TIMEOUT;
0484     dev->tx_queue_len = 100;
0485     dev->ethtool_ops = &qtnf_ethtool_ops;
0486 
0487     if (qtnf_hwcap_is_set(&mac->bus->hw_info, QLINK_HW_CAPAB_HW_BRIDGE))
0488         dev->needed_tailroom = sizeof(struct qtnf_frame_meta_info);
0489 
0490     qdev_vif = netdev_priv(dev);
0491     *((void **)qdev_vif) = vif;
0492 
0493     SET_NETDEV_DEV(dev, wiphy_dev(wiphy));
0494 
0495     ret = cfg80211_register_netdevice(dev);
0496     if (ret) {
0497         free_netdev(dev);
0498         vif->netdev = NULL;
0499     }
0500 
0501     return ret;
0502 }
0503 
0504 static void qtnf_core_mac_detach(struct qtnf_bus *bus, unsigned int macid)
0505 {
0506     struct qtnf_wmac *mac;
0507     struct wiphy *wiphy;
0508     struct qtnf_vif *vif;
0509     unsigned int i;
0510     enum nl80211_band band;
0511 
0512     mac = bus->mac[macid];
0513 
0514     if (!mac)
0515         return;
0516 
0517     wiphy = priv_to_wiphy(mac);
0518 
0519     for (i = 0; i < QTNF_MAX_INTF; i++) {
0520         vif = &mac->iflist[i];
0521         rtnl_lock();
0522         if (vif->netdev &&
0523             vif->wdev.iftype != NL80211_IFTYPE_UNSPECIFIED) {
0524             qtnf_virtual_intf_cleanup(vif->netdev);
0525             qtnf_del_virtual_intf(wiphy, &vif->wdev);
0526         }
0527         rtnl_unlock();
0528         qtnf_sta_list_free(&vif->sta_list);
0529     }
0530 
0531     if (mac->wiphy_registered)
0532         wiphy_unregister(wiphy);
0533 
0534     for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; ++band) {
0535         if (!wiphy->bands[band])
0536             continue;
0537 
0538         kfree(wiphy->bands[band]->iftype_data);
0539         wiphy->bands[band]->n_iftype_data = 0;
0540 
0541         kfree(wiphy->bands[band]->channels);
0542         wiphy->bands[band]->n_channels = 0;
0543 
0544         kfree(wiphy->bands[band]);
0545         wiphy->bands[band] = NULL;
0546     }
0547 
0548     platform_device_unregister(mac->pdev);
0549     qtnf_mac_iface_comb_free(mac);
0550     qtnf_mac_ext_caps_free(mac);
0551     kfree(mac->macinfo.wowlan);
0552     kfree(mac->rd);
0553     mac->rd = NULL;
0554     wiphy_free(wiphy);
0555     bus->mac[macid] = NULL;
0556 }
0557 
0558 static int qtnf_core_mac_attach(struct qtnf_bus *bus, unsigned int macid)
0559 {
0560     struct qtnf_wmac *mac;
0561     struct qtnf_vif *vif;
0562     int ret;
0563 
0564     if (!(bus->hw_info.mac_bitmap & BIT(macid))) {
0565         pr_info("MAC%u is not active in FW\n", macid);
0566         return 0;
0567     }
0568 
0569     mac = qtnf_core_mac_alloc(bus, macid);
0570     if (IS_ERR(mac)) {
0571         pr_err("MAC%u allocation failed\n", macid);
0572         return PTR_ERR(mac);
0573     }
0574 
0575     vif = qtnf_mac_get_base_vif(mac);
0576     if (!vif) {
0577         pr_err("MAC%u: primary VIF is not ready\n", macid);
0578         ret = -EFAULT;
0579         goto error;
0580     }
0581 
0582     ret = qtnf_cmd_send_add_intf(vif, vif->wdev.iftype,
0583                      vif->wdev.use_4addr, vif->mac_addr);
0584     if (ret) {
0585         pr_err("MAC%u: failed to add VIF\n", macid);
0586         goto error;
0587     }
0588 
0589     ret = qtnf_cmd_get_mac_info(mac);
0590     if (ret) {
0591         pr_err("MAC%u: failed to get MAC info\n", macid);
0592         goto error_del_vif;
0593     }
0594 
0595     /* Use MAC address of the first active radio as a unique device ID */
0596     if (is_zero_ether_addr(mac->bus->hw_id))
0597         ether_addr_copy(mac->bus->hw_id, mac->macaddr);
0598 
0599     ret = qtnf_mac_init_bands(mac);
0600     if (ret) {
0601         pr_err("MAC%u: failed to init bands\n", macid);
0602         goto error_del_vif;
0603     }
0604 
0605     ret = qtnf_wiphy_register(&bus->hw_info, mac);
0606     if (ret) {
0607         pr_err("MAC%u: wiphy registration failed\n", macid);
0608         goto error_del_vif;
0609     }
0610 
0611     mac->wiphy_registered = 1;
0612 
0613     rtnl_lock();
0614     wiphy_lock(priv_to_wiphy(mac));
0615     ret = qtnf_core_net_attach(mac, vif, "wlan%d", NET_NAME_ENUM);
0616     wiphy_unlock(priv_to_wiphy(mac));
0617     rtnl_unlock();
0618 
0619     if (ret) {
0620         pr_err("MAC%u: failed to attach netdev\n", macid);
0621         goto error_del_vif;
0622     }
0623 
0624     if (qtnf_hwcap_is_set(&bus->hw_info, QLINK_HW_CAPAB_HW_BRIDGE)) {
0625         ret = qtnf_cmd_netdev_changeupper(vif, vif->netdev->ifindex);
0626         if (ret)
0627             goto error;
0628     }
0629 
0630     pr_debug("MAC%u initialized\n", macid);
0631 
0632     return 0;
0633 
0634 error_del_vif:
0635     qtnf_cmd_send_del_intf(vif);
0636     vif->wdev.iftype = NL80211_IFTYPE_UNSPECIFIED;
0637 error:
0638     qtnf_core_mac_detach(bus, macid);
0639     return ret;
0640 }
0641 
0642 bool qtnf_netdev_is_qtn(const struct net_device *ndev)
0643 {
0644     return ndev->netdev_ops == &qtnf_netdev_ops;
0645 }
0646 
0647 static int qtnf_check_br_ports(struct net_device *dev,
0648                    struct netdev_nested_priv *priv)
0649 {
0650     struct net_device *ndev = (struct net_device *)priv->data;
0651 
0652     if (dev != ndev && netdev_port_same_parent_id(dev, ndev))
0653         return -ENOTSUPP;
0654 
0655     return 0;
0656 }
0657 
0658 static int qtnf_core_netdevice_event(struct notifier_block *nb,
0659                      unsigned long event, void *ptr)
0660 {
0661     struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
0662     const struct netdev_notifier_changeupper_info *info;
0663     struct netdev_nested_priv priv = {
0664         .data = (void *)ndev,
0665     };
0666     struct net_device *brdev;
0667     struct qtnf_vif *vif;
0668     struct qtnf_bus *bus;
0669     int br_domain;
0670     int ret = 0;
0671 
0672     if (!qtnf_netdev_is_qtn(ndev))
0673         return NOTIFY_DONE;
0674 
0675     if (!net_eq(dev_net(ndev), &init_net))
0676         return NOTIFY_OK;
0677 
0678     vif = qtnf_netdev_get_priv(ndev);
0679     bus = vif->mac->bus;
0680 
0681     switch (event) {
0682     case NETDEV_CHANGEUPPER:
0683         info = ptr;
0684         brdev = info->upper_dev;
0685 
0686         if (!netif_is_bridge_master(brdev))
0687             break;
0688 
0689         pr_debug("[VIF%u.%u] change bridge: %s %s\n",
0690              vif->mac->macid, vif->vifid, netdev_name(brdev),
0691              info->linking ? "add" : "del");
0692 
0693         if (IS_ENABLED(CONFIG_NET_SWITCHDEV) &&
0694             qtnf_hwcap_is_set(&bus->hw_info,
0695                       QLINK_HW_CAPAB_HW_BRIDGE)) {
0696             if (info->linking)
0697                 br_domain = brdev->ifindex;
0698             else
0699                 br_domain = ndev->ifindex;
0700 
0701             ret = qtnf_cmd_netdev_changeupper(vif, br_domain);
0702         } else {
0703             ret = netdev_walk_all_lower_dev(brdev,
0704                             qtnf_check_br_ports,
0705                             &priv);
0706         }
0707 
0708         break;
0709     default:
0710         break;
0711     }
0712 
0713     return notifier_from_errno(ret);
0714 }
0715 
0716 int qtnf_core_attach(struct qtnf_bus *bus)
0717 {
0718     unsigned int i;
0719     int ret;
0720 
0721     qtnf_trans_init(bus);
0722     qtnf_bus_data_rx_start(bus);
0723 
0724     bus->workqueue = alloc_ordered_workqueue("QTNF_BUS", 0);
0725     if (!bus->workqueue) {
0726         pr_err("failed to alloc main workqueue\n");
0727         ret = -ENOMEM;
0728         goto error;
0729     }
0730 
0731     bus->hprio_workqueue = alloc_workqueue("QTNF_HPRI", WQ_HIGHPRI, 0);
0732     if (!bus->hprio_workqueue) {
0733         pr_err("failed to alloc high prio workqueue\n");
0734         ret = -ENOMEM;
0735         goto error;
0736     }
0737 
0738     INIT_WORK(&bus->event_work, qtnf_event_work_handler);
0739 
0740     ret = qtnf_cmd_send_init_fw(bus);
0741     if (ret) {
0742         pr_err("failed to init FW: %d\n", ret);
0743         goto error;
0744     }
0745 
0746     if (QLINK_VER_MAJOR(bus->hw_info.ql_proto_ver) !=
0747         QLINK_PROTO_VER_MAJOR) {
0748         pr_err("qlink driver vs FW version mismatch: %u vs %u\n",
0749                QLINK_PROTO_VER_MAJOR,
0750                QLINK_VER_MAJOR(bus->hw_info.ql_proto_ver));
0751         ret = -EPROTONOSUPPORT;
0752         goto error;
0753     }
0754 
0755     bus->fw_state = QTNF_FW_STATE_ACTIVE;
0756     ret = qtnf_cmd_get_hw_info(bus);
0757     if (ret) {
0758         pr_err("failed to get HW info: %d\n", ret);
0759         goto error;
0760     }
0761 
0762     if (qtnf_hwcap_is_set(&bus->hw_info, QLINK_HW_CAPAB_HW_BRIDGE) &&
0763         bus->bus_ops->data_tx_use_meta_set)
0764         bus->bus_ops->data_tx_use_meta_set(bus, true);
0765 
0766     if (bus->hw_info.num_mac > QTNF_MAX_MAC) {
0767         pr_err("no support for number of MACs=%u\n",
0768                bus->hw_info.num_mac);
0769         ret = -ERANGE;
0770         goto error;
0771     }
0772 
0773     for (i = 0; i < bus->hw_info.num_mac; i++) {
0774         ret = qtnf_core_mac_attach(bus, i);
0775 
0776         if (ret) {
0777             pr_err("MAC%u: attach failed: %d\n", i, ret);
0778             goto error;
0779         }
0780     }
0781 
0782     bus->netdev_nb.notifier_call = qtnf_core_netdevice_event;
0783     ret = register_netdevice_notifier(&bus->netdev_nb);
0784     if (ret) {
0785         pr_err("failed to register netdev notifier: %d\n", ret);
0786         goto error;
0787     }
0788 
0789     bus->fw_state = QTNF_FW_STATE_RUNNING;
0790     return 0;
0791 
0792 error:
0793     qtnf_core_detach(bus);
0794     return ret;
0795 }
0796 EXPORT_SYMBOL_GPL(qtnf_core_attach);
0797 
0798 void qtnf_core_detach(struct qtnf_bus *bus)
0799 {
0800     unsigned int macid;
0801 
0802     unregister_netdevice_notifier(&bus->netdev_nb);
0803     qtnf_bus_data_rx_stop(bus);
0804 
0805     for (macid = 0; macid < QTNF_MAX_MAC; macid++)
0806         qtnf_core_mac_detach(bus, macid);
0807 
0808     if (qtnf_fw_is_up(bus))
0809         qtnf_cmd_send_deinit_fw(bus);
0810 
0811     bus->fw_state = QTNF_FW_STATE_DETACHED;
0812 
0813     if (bus->workqueue) {
0814         destroy_workqueue(bus->workqueue);
0815         bus->workqueue = NULL;
0816     }
0817 
0818     if (bus->hprio_workqueue) {
0819         destroy_workqueue(bus->hprio_workqueue);
0820         bus->hprio_workqueue = NULL;
0821     }
0822 
0823     qtnf_trans_free(bus);
0824 }
0825 EXPORT_SYMBOL_GPL(qtnf_core_detach);
0826 
0827 static inline int qtnf_is_frame_meta_magic_valid(struct qtnf_frame_meta_info *m)
0828 {
0829     return m->magic_s == HBM_FRAME_META_MAGIC_PATTERN_S &&
0830         m->magic_e == HBM_FRAME_META_MAGIC_PATTERN_E;
0831 }
0832 
0833 struct net_device *qtnf_classify_skb(struct qtnf_bus *bus, struct sk_buff *skb)
0834 {
0835     struct qtnf_frame_meta_info *meta;
0836     struct net_device *ndev = NULL;
0837     struct qtnf_wmac *mac;
0838     struct qtnf_vif *vif;
0839 
0840     if (unlikely(bus->fw_state != QTNF_FW_STATE_RUNNING))
0841         return NULL;
0842 
0843     meta = (struct qtnf_frame_meta_info *)
0844         (skb_tail_pointer(skb) - sizeof(*meta));
0845 
0846     if (unlikely(!qtnf_is_frame_meta_magic_valid(meta))) {
0847         pr_err_ratelimited("invalid magic 0x%x:0x%x\n",
0848                    meta->magic_s, meta->magic_e);
0849         goto out;
0850     }
0851 
0852     if (unlikely(meta->macid >= QTNF_MAX_MAC)) {
0853         pr_err_ratelimited("invalid mac(%u)\n", meta->macid);
0854         goto out;
0855     }
0856 
0857     if (unlikely(meta->ifidx >= QTNF_MAX_INTF)) {
0858         pr_err_ratelimited("invalid vif(%u)\n", meta->ifidx);
0859         goto out;
0860     }
0861 
0862     mac = bus->mac[meta->macid];
0863 
0864     if (unlikely(!mac)) {
0865         pr_err_ratelimited("mac(%d) does not exist\n", meta->macid);
0866         goto out;
0867     }
0868 
0869     vif = &mac->iflist[meta->ifidx];
0870 
0871     if (unlikely(vif->wdev.iftype == NL80211_IFTYPE_UNSPECIFIED)) {
0872         pr_err_ratelimited("vif(%u) does not exists\n", meta->ifidx);
0873         goto out;
0874     }
0875 
0876     ndev = vif->netdev;
0877 
0878     if (unlikely(!ndev)) {
0879         pr_err_ratelimited("netdev for wlan%u.%u does not exists\n",
0880                    meta->macid, meta->ifidx);
0881         goto out;
0882     }
0883 
0884     __skb_trim(skb, skb->len - sizeof(*meta));
0885     /* Firmware always handles packets that require flooding */
0886     qtnfmac_switch_mark_skb_flooded(skb);
0887 
0888 out:
0889     return ndev;
0890 }
0891 EXPORT_SYMBOL_GPL(qtnf_classify_skb);
0892 
0893 void qtnf_wake_all_queues(struct net_device *ndev)
0894 {
0895     struct qtnf_vif *vif = qtnf_netdev_get_priv(ndev);
0896     struct qtnf_wmac *mac;
0897     struct qtnf_bus *bus;
0898     int macid;
0899     int i;
0900 
0901     if (unlikely(!vif || !vif->mac || !vif->mac->bus))
0902         return;
0903 
0904     bus = vif->mac->bus;
0905 
0906     for (macid = 0; macid < QTNF_MAX_MAC; macid++) {
0907         if (!(bus->hw_info.mac_bitmap & BIT(macid)))
0908             continue;
0909 
0910         mac = bus->mac[macid];
0911         for (i = 0; i < QTNF_MAX_INTF; i++) {
0912             vif = &mac->iflist[i];
0913             if (vif->netdev && netif_queue_stopped(vif->netdev))
0914                 netif_tx_wake_all_queues(vif->netdev);
0915         }
0916     }
0917 }
0918 EXPORT_SYMBOL_GPL(qtnf_wake_all_queues);
0919 
0920 struct dentry *qtnf_get_debugfs_dir(void)
0921 {
0922     return qtnf_debugfs_dir;
0923 }
0924 EXPORT_SYMBOL_GPL(qtnf_get_debugfs_dir);
0925 
0926 static int __init qtnf_core_register(void)
0927 {
0928     qtnf_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
0929 
0930     if (IS_ERR(qtnf_debugfs_dir))
0931         qtnf_debugfs_dir = NULL;
0932 
0933     return 0;
0934 }
0935 
0936 static void __exit qtnf_core_exit(void)
0937 {
0938     debugfs_remove(qtnf_debugfs_dir);
0939 }
0940 
0941 module_init(qtnf_core_register);
0942 module_exit(qtnf_core_exit);
0943 
0944 MODULE_AUTHOR("Quantenna Communications");
0945 MODULE_DESCRIPTION("Quantenna 802.11 wireless LAN FullMAC driver.");
0946 MODULE_LICENSE("GPL");