1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/delay.h>
5 #include "nitrox_dev.h"
6 #include "nitrox_hal.h"
7 #include "nitrox_common.h"
8 #include "nitrox_isr.h"
11 * num_vfs_valid - validate VF count
12 * @num_vfs: number of VF(s)
14 static inline bool num_vfs_valid(int num_vfs)
30 static inline enum vf_mode num_vfs_to_mode(int num_vfs)
32 enum vf_mode mode = 0;
36 mode = __NDEV_MODE_PF;
39 mode = __NDEV_MODE_VF16;
42 mode = __NDEV_MODE_VF32;
45 mode = __NDEV_MODE_VF64;
48 mode = __NDEV_MODE_VF128;
55 static void nitrox_pf_cleanup(struct nitrox_device *ndev)
57 /* PF has no queues in SR-IOV mode */
58 atomic_set(&ndev->state, __NDEV_NOT_READY);
59 /* unregister crypto algorithms */
60 nitrox_crypto_unregister();
62 /* cleanup PF resources */
63 nitrox_unregister_interrupts(ndev);
64 nitrox_common_sw_cleanup(ndev);
68 * nitrox_pf_reinit - re-initialize PF resources once SR-IOV is disabled
69 * @ndev: NITROX device
71 static int nitrox_pf_reinit(struct nitrox_device *ndev)
75 /* allocate resources for PF */
76 err = nitrox_common_sw_init(ndev);
80 err = nitrox_register_interrupts(ndev);
82 nitrox_common_sw_cleanup(ndev);
86 /* configure the packet queues */
87 nitrox_config_pkt_input_rings(ndev);
88 nitrox_config_pkt_solicit_ports(ndev);
90 /* set device to ready state */
91 atomic_set(&ndev->state, __NDEV_READY);
93 /* register crypto algorithms */
94 return nitrox_crypto_register();
97 static int nitrox_sriov_init(struct nitrox_device *ndev)
99 /* register interrupts for PF in SR-IOV */
100 return nitrox_sriov_register_interupts(ndev);
103 static void nitrox_sriov_cleanup(struct nitrox_device *ndev)
105 /* unregister interrupts for PF in SR-IOV */
106 nitrox_sriov_unregister_interrupts(ndev);
109 static int nitrox_sriov_enable(struct pci_dev *pdev, int num_vfs)
111 struct nitrox_device *ndev = pci_get_drvdata(pdev);
114 if (!num_vfs_valid(num_vfs)) {
115 dev_err(DEV(ndev), "Invalid num_vfs %d\n", num_vfs);
119 if (pci_num_vf(pdev) == num_vfs)
122 err = pci_enable_sriov(pdev, num_vfs);
124 dev_err(DEV(ndev), "failed to enable PCI sriov %d\n", err);
127 dev_info(DEV(ndev), "Enabled VF(s) %d\n", num_vfs);
129 ndev->iov.num_vfs = num_vfs;
130 ndev->mode = num_vfs_to_mode(num_vfs);
131 /* set bit in flags */
132 set_bit(__NDEV_SRIOV_BIT, &ndev->flags);
134 /* cleanup PF resources */
135 nitrox_pf_cleanup(ndev);
137 /* PF SR-IOV mode initialization */
138 err = nitrox_sriov_init(ndev);
142 config_nps_core_vfcfg_mode(ndev, ndev->mode);
146 pci_disable_sriov(pdev);
147 /* clear bit in flags */
148 clear_bit(__NDEV_SRIOV_BIT, &ndev->flags);
149 ndev->iov.num_vfs = 0;
150 ndev->mode = __NDEV_MODE_PF;
151 /* reset back to working mode in PF */
152 nitrox_pf_reinit(ndev);
156 static int nitrox_sriov_disable(struct pci_dev *pdev)
158 struct nitrox_device *ndev = pci_get_drvdata(pdev);
160 if (!test_bit(__NDEV_SRIOV_BIT, &ndev->flags))
163 if (pci_vfs_assigned(pdev)) {
164 dev_warn(DEV(ndev), "VFs are attached to VM. Can't disable SR-IOV\n");
167 pci_disable_sriov(pdev);
168 /* clear bit in flags */
169 clear_bit(__NDEV_SRIOV_BIT, &ndev->flags);
171 ndev->iov.num_vfs = 0;
172 ndev->mode = __NDEV_MODE_PF;
174 /* cleanup PF SR-IOV resources */
175 nitrox_sriov_cleanup(ndev);
177 config_nps_core_vfcfg_mode(ndev, ndev->mode);
179 return nitrox_pf_reinit(ndev);
182 int nitrox_sriov_configure(struct pci_dev *pdev, int num_vfs)
185 return nitrox_sriov_disable(pdev);
187 return nitrox_sriov_enable(pdev, num_vfs);