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"
9 #include "nitrox_mbx.h"
12 * num_vfs_valid - validate VF count
13 * @num_vfs: number of VF(s)
15 static inline bool num_vfs_valid(int num_vfs
)
31 static inline enum vf_mode
num_vfs_to_mode(int num_vfs
)
33 enum vf_mode mode
= 0;
37 mode
= __NDEV_MODE_PF
;
40 mode
= __NDEV_MODE_VF16
;
43 mode
= __NDEV_MODE_VF32
;
46 mode
= __NDEV_MODE_VF64
;
49 mode
= __NDEV_MODE_VF128
;
56 static inline int vf_mode_to_nr_queues(enum vf_mode mode
)
62 nr_queues
= MAX_PF_QUEUES
;
64 case __NDEV_MODE_VF16
:
67 case __NDEV_MODE_VF32
:
70 case __NDEV_MODE_VF64
:
73 case __NDEV_MODE_VF128
:
81 static void nitrox_pf_cleanup(struct nitrox_device
*ndev
)
83 /* PF has no queues in SR-IOV mode */
84 atomic_set(&ndev
->state
, __NDEV_NOT_READY
);
85 /* unregister crypto algorithms */
86 nitrox_crypto_unregister();
88 /* cleanup PF resources */
89 nitrox_unregister_interrupts(ndev
);
90 nitrox_common_sw_cleanup(ndev
);
94 * nitrox_pf_reinit - re-initialize PF resources once SR-IOV is disabled
95 * @ndev: NITROX device
97 static int nitrox_pf_reinit(struct nitrox_device
*ndev
)
101 /* allocate resources for PF */
102 err
= nitrox_common_sw_init(ndev
);
106 err
= nitrox_register_interrupts(ndev
);
108 nitrox_common_sw_cleanup(ndev
);
112 /* configure the AQM queues */
113 nitrox_config_aqm_rings(ndev
);
115 /* configure the packet queues */
116 nitrox_config_pkt_input_rings(ndev
);
117 nitrox_config_pkt_solicit_ports(ndev
);
119 /* set device to ready state */
120 atomic_set(&ndev
->state
, __NDEV_READY
);
122 /* register crypto algorithms */
123 return nitrox_crypto_register();
126 static void nitrox_sriov_cleanup(struct nitrox_device
*ndev
)
128 /* unregister interrupts for PF in SR-IOV */
129 nitrox_sriov_unregister_interrupts(ndev
);
130 nitrox_mbox_cleanup(ndev
);
133 static int nitrox_sriov_init(struct nitrox_device
*ndev
)
137 /* register interrupts for PF in SR-IOV */
138 ret
= nitrox_sriov_register_interupts(ndev
);
142 ret
= nitrox_mbox_init(ndev
);
144 goto sriov_init_fail
;
149 nitrox_sriov_cleanup(ndev
);
153 static int nitrox_sriov_enable(struct pci_dev
*pdev
, int num_vfs
)
155 struct nitrox_device
*ndev
= pci_get_drvdata(pdev
);
158 if (!num_vfs_valid(num_vfs
)) {
159 dev_err(DEV(ndev
), "Invalid num_vfs %d\n", num_vfs
);
163 if (pci_num_vf(pdev
) == num_vfs
)
166 err
= pci_enable_sriov(pdev
, num_vfs
);
168 dev_err(DEV(ndev
), "failed to enable PCI sriov %d\n", err
);
171 dev_info(DEV(ndev
), "Enabled VF(s) %d\n", num_vfs
);
173 ndev
->mode
= num_vfs_to_mode(num_vfs
);
174 ndev
->iov
.num_vfs
= num_vfs
;
175 ndev
->iov
.max_vf_queues
= vf_mode_to_nr_queues(ndev
->mode
);
176 /* set bit in flags */
177 set_bit(__NDEV_SRIOV_BIT
, &ndev
->flags
);
179 /* cleanup PF resources */
180 nitrox_pf_cleanup(ndev
);
182 /* PF SR-IOV mode initialization */
183 err
= nitrox_sriov_init(ndev
);
187 config_nps_core_vfcfg_mode(ndev
, ndev
->mode
);
191 pci_disable_sriov(pdev
);
192 /* clear bit in flags */
193 clear_bit(__NDEV_SRIOV_BIT
, &ndev
->flags
);
194 ndev
->iov
.num_vfs
= 0;
195 ndev
->mode
= __NDEV_MODE_PF
;
196 /* reset back to working mode in PF */
197 nitrox_pf_reinit(ndev
);
201 static int nitrox_sriov_disable(struct pci_dev
*pdev
)
203 struct nitrox_device
*ndev
= pci_get_drvdata(pdev
);
205 if (!test_bit(__NDEV_SRIOV_BIT
, &ndev
->flags
))
208 if (pci_vfs_assigned(pdev
)) {
209 dev_warn(DEV(ndev
), "VFs are attached to VM. Can't disable SR-IOV\n");
212 pci_disable_sriov(pdev
);
213 /* clear bit in flags */
214 clear_bit(__NDEV_SRIOV_BIT
, &ndev
->flags
);
216 ndev
->iov
.num_vfs
= 0;
217 ndev
->iov
.max_vf_queues
= 0;
218 ndev
->mode
= __NDEV_MODE_PF
;
220 /* cleanup PF SR-IOV resources */
221 nitrox_sriov_cleanup(ndev
);
223 config_nps_core_vfcfg_mode(ndev
, ndev
->mode
);
225 return nitrox_pf_reinit(ndev
);
228 int nitrox_sriov_configure(struct pci_dev
*pdev
, int num_vfs
)
231 return nitrox_sriov_disable(pdev
);
233 return nitrox_sriov_enable(pdev
, num_vfs
);