octeontx2-pf: add support for ndo_set_vf_trust
authorHariprasad Kelam <hkelam@marvell.com>
Fri, 11 Jun 2021 09:42:05 +0000 (15:12 +0530)
committerDavid S. Miller <davem@davemloft.net>
Fri, 11 Jun 2021 20:21:11 +0000 (13:21 -0700)
Add support for setting a VF as a trusted VF by PF admin. Trusted VF
feature allows VFs to perform priviliged operations such as enabling
VF promiscuous mode, all-multicast mode and changing the VF MAC address
even if it was assigned by PF.

Signed-off-by: Hariprasad Kelam <hkelam@marvell.com>
Signed-off-by: Naveen Mamindlapalli <naveenm@marvell.com>
Signed-off-by: Sunil Kovvuri Goutham <Sunil.Goutham@marvell.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/ethernet/marvell/octeontx2/nic/otx2_common.h
drivers/net/ethernet/marvell/octeontx2/nic/otx2_pf.c

index 45730d0..543aee7 100644 (file)
@@ -223,6 +223,11 @@ struct otx2_hw {
        u64                     *nix_lmt_base;
 };
 
+enum vfperm {
+       OTX2_RESET_VF_PERM,
+       OTX2_TRUSTED_VF,
+};
+
 struct otx2_vf_config {
        struct otx2_nic *pf;
        struct delayed_work link_event_work;
@@ -230,6 +235,7 @@ struct otx2_vf_config {
        u8 mac[ETH_ALEN];
        u16 vlan;
        int tx_vtag_idx;
+       bool trusted;
 };
 
 struct flr_work {
index dcc6b74..82b53e7 100644 (file)
@@ -39,6 +39,8 @@ MODULE_DESCRIPTION(DRV_STRING);
 MODULE_LICENSE("GPL v2");
 MODULE_DEVICE_TABLE(pci, otx2_pf_id_table);
 
+static void otx2_vf_link_event_task(struct work_struct *work);
+
 enum {
        TYPE_PFAF,
        TYPE_PFVF,
@@ -2046,7 +2048,7 @@ static int otx2_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
        if (!netif_running(netdev))
                return -EAGAIN;
 
-       if (vf >= pci_num_vf(pdev))
+       if (vf >= pf->total_vfs)
                return -EINVAL;
 
        if (!is_valid_ether_addr(mac))
@@ -2057,7 +2059,8 @@ static int otx2_set_vf_mac(struct net_device *netdev, int vf, u8 *mac)
 
        ret = otx2_do_set_vf_mac(pf, vf, mac);
        if (ret == 0)
-               dev_info(&pdev->dev, "Reload VF driver to apply the changes\n");
+               dev_info(&pdev->dev,
+                        "Load/Reload VF driver\n");
 
        return ret;
 }
@@ -2243,10 +2246,63 @@ static int otx2_get_vf_config(struct net_device *netdev, int vf,
        ivi->vf = vf;
        ether_addr_copy(ivi->mac, config->mac);
        ivi->vlan = config->vlan;
+       ivi->trusted = config->trusted;
 
        return 0;
 }
 
+static int otx2_set_vf_permissions(struct otx2_nic *pf, int vf,
+                                  int req_perm)
+{
+       struct set_vf_perm *req;
+       int rc;
+
+       mutex_lock(&pf->mbox.lock);
+       req = otx2_mbox_alloc_msg_set_vf_perm(&pf->mbox);
+       if (!req) {
+               rc = -ENOMEM;
+               goto out;
+       }
+
+       /* Let AF reset VF permissions as sriov is disabled */
+       if (req_perm == OTX2_RESET_VF_PERM) {
+               req->flags |= RESET_VF_PERM;
+       } else if (req_perm == OTX2_TRUSTED_VF) {
+               if (pf->vf_configs[vf].trusted)
+                       req->flags |= VF_TRUSTED;
+       }
+
+       req->vf = vf;
+       rc = otx2_sync_mbox_msg(&pf->mbox);
+out:
+       mutex_unlock(&pf->mbox.lock);
+       return rc;
+}
+
+static int otx2_ndo_set_vf_trust(struct net_device *netdev, int vf,
+                                bool enable)
+{
+       struct otx2_nic *pf = netdev_priv(netdev);
+       struct pci_dev *pdev = pf->pdev;
+       int rc;
+
+       if (vf >= pci_num_vf(pdev))
+               return -EINVAL;
+
+       if (pf->vf_configs[vf].trusted == enable)
+               return 0;
+
+       pf->vf_configs[vf].trusted = enable;
+       rc = otx2_set_vf_permissions(pf, vf, OTX2_TRUSTED_VF);
+
+       if (rc)
+               pf->vf_configs[vf].trusted = !enable;
+       else
+               netdev_info(pf->netdev, "VF %d is %strusted\n",
+                           vf, enable ? "" : "not ");
+       return rc;
+}
+
 static const struct net_device_ops otx2_netdev_ops = {
        .ndo_open               = otx2_open,
        .ndo_stop               = otx2_stop,
@@ -2263,6 +2319,7 @@ static const struct net_device_ops otx2_netdev_ops = {
        .ndo_set_vf_vlan        = otx2_set_vf_vlan,
        .ndo_get_vf_config      = otx2_get_vf_config,
        .ndo_setup_tc           = otx2_setup_tc,
+       .ndo_set_vf_trust       = otx2_ndo_set_vf_trust,
 };
 
 static int otx2_wq_init(struct otx2_nic *pf)
@@ -2317,6 +2374,40 @@ static int otx2_realloc_msix_vectors(struct otx2_nic *pf)
        return otx2_register_mbox_intr(pf, false);
 }
 
+static int otx2_sriov_vfcfg_init(struct otx2_nic *pf)
+{
+       int i;
+
+       pf->vf_configs = devm_kcalloc(pf->dev, pf->total_vfs,
+                                     sizeof(struct otx2_vf_config),
+                                     GFP_KERNEL);
+       if (!pf->vf_configs)
+               return -ENOMEM;
+
+       for (i = 0; i < pf->total_vfs; i++) {
+               pf->vf_configs[i].pf = pf;
+               pf->vf_configs[i].intf_down = true;
+               pf->vf_configs[i].trusted = false;
+               INIT_DELAYED_WORK(&pf->vf_configs[i].link_event_work,
+                                 otx2_vf_link_event_task);
+       }
+
+       return 0;
+}
+
+static void otx2_sriov_vfcfg_cleanup(struct otx2_nic *pf)
+{
+       int i;
+
+       if (!pf->vf_configs)
+               return;
+
+       for (i = 0; i < pf->total_vfs; i++) {
+               cancel_delayed_work_sync(&pf->vf_configs[i].link_event_work);
+               otx2_set_vf_permissions(pf, i, OTX2_RESET_VF_PERM);
+       }
+}
+
 static int otx2_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 {
        struct device *dev = &pdev->dev;
@@ -2511,6 +2602,11 @@ static int otx2_probe(struct pci_dev *pdev, const struct pci_device_id *id)
        if (err)
                goto err_mcam_flow_del;
 
+       /* Initialize SR-IOV resources */
+       err = otx2_sriov_vfcfg_init(pf);
+       if (err)
+               goto err_pf_sriov_init;
+
        /* Enable link notifications */
        otx2_cgx_config_linkevents(pf, true);
 
@@ -2520,6 +2616,8 @@ static int otx2_probe(struct pci_dev *pdev, const struct pci_device_id *id)
 
        return 0;
 
+err_pf_sriov_init:
+       otx2_shutdown_tc(pf);
 err_mcam_flow_del:
        otx2_mcam_flow_del(pf);
 err_unreg_netdev:
@@ -2578,7 +2676,7 @@ static int otx2_sriov_enable(struct pci_dev *pdev, int numvfs)
 {
        struct net_device *netdev = pci_get_drvdata(pdev);
        struct otx2_nic *pf = netdev_priv(netdev);
-       int ret, i;
+       int ret;
 
        /* Init PF <=> VF mailbox stuff */
        ret = otx2_pfvf_mbox_init(pf, numvfs);
@@ -2589,23 +2687,9 @@ static int otx2_sriov_enable(struct pci_dev *pdev, int numvfs)
        if (ret)
                goto free_mbox;
 
-       pf->vf_configs = kcalloc(numvfs, sizeof(struct otx2_vf_config),
-                                GFP_KERNEL);
-       if (!pf->vf_configs) {
-               ret = -ENOMEM;
-               goto free_intr;
-       }
-
-       for (i = 0; i < numvfs; i++) {
-               pf->vf_configs[i].pf = pf;
-               pf->vf_configs[i].intf_down = true;
-               INIT_DELAYED_WORK(&pf->vf_configs[i].link_event_work,
-                                 otx2_vf_link_event_task);
-       }
-
        ret = otx2_pf_flr_init(pf, numvfs);
        if (ret)
-               goto free_configs;
+               goto free_intr;
 
        ret = otx2_register_flr_me_intr(pf, numvfs);
        if (ret)
@@ -2620,8 +2704,6 @@ free_flr_intr:
        otx2_disable_flr_me_intr(pf);
 free_flr:
        otx2_flr_wq_destroy(pf);
-free_configs:
-       kfree(pf->vf_configs);
 free_intr:
        otx2_disable_pfvf_mbox_intr(pf, numvfs);
 free_mbox:
@@ -2634,17 +2716,12 @@ static int otx2_sriov_disable(struct pci_dev *pdev)
        struct net_device *netdev = pci_get_drvdata(pdev);
        struct otx2_nic *pf = netdev_priv(netdev);
        int numvfs = pci_num_vf(pdev);
-       int i;
 
        if (!numvfs)
                return 0;
 
        pci_disable_sriov(pdev);
 
-       for (i = 0; i < pci_num_vf(pdev); i++)
-               cancel_delayed_work_sync(&pf->vf_configs[i].link_event_work);
-       kfree(pf->vf_configs);
-
        otx2_disable_flr_me_intr(pf);
        otx2_flr_wq_destroy(pf);
        otx2_disable_pfvf_mbox_intr(pf, numvfs);
@@ -2684,6 +2761,7 @@ static void otx2_remove(struct pci_dev *pdev)
 
        unregister_netdev(netdev);
        otx2_sriov_disable(pf->pdev);
+       otx2_sriov_vfcfg_cleanup(pf);
        if (pf->otx2_wq)
                destroy_workqueue(pf->otx2_wq);