Skip to content

Commit fb916db

Browse files
jacob-kelleranguy11
authored andcommitted
ice: introduce VF accessor functions
Before we switch the VF data structure storage mechanism to a hash, introduce new accessor functions to define the new interface. * ice_get_vf_by_id is a function used to obtain a reference to a VF from the table based on its VF ID * ice_has_vfs is used to quickly check if any VFs are configured * ice_get_num_vfs is used to get an exact count of how many VFs are configured We can drop the old ice_validate_vf_id function, since every caller was just going to immediately access the VF table to get a reference anyways. This way we simply use the single ice_get_vf_by_id to both validate the VF ID is within range and that there exists a VF with that ID. This change enables us to more easily convert the codebase to the hash table since most callers now properly use the interface. Signed-off-by: Jacob Keller <[email protected]> Tested-by: Konrad Jankowski <[email protected]> Signed-off-by: Tony Nguyen <[email protected]>
1 parent 000773c commit fb916db

File tree

5 files changed

+115
-53
lines changed

5 files changed

+115
-53
lines changed

drivers/net/ethernet/intel/ice/ice_eswitch.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,10 @@ static void ice_eswitch_remap_rings_to_vectors(struct ice_pf *pf)
182182
struct ice_repr *repr;
183183
struct ice_vf *vf;
184184

185-
if (WARN_ON(q_id >= pf->vfs.num_alloc))
185+
vf = ice_get_vf_by_id(pf, q_id);
186+
if (WARN_ON(!vf))
186187
continue;
187188

188-
vf = &pf->vfs.table[q_id];
189189
repr = vf->repr;
190190
q_vector = repr->q_vector;
191191
tx_ring = vsi->tx_rings[q_id];
@@ -535,7 +535,7 @@ ice_eswitch_mode_set(struct devlink *devlink, u16 mode,
535535
if (pf->eswitch_mode == mode)
536536
return 0;
537537

538-
if (pf->vfs.num_alloc) {
538+
if (ice_has_vfs(pf)) {
539539
dev_info(ice_pf_to_dev(pf), "Changing eswitch mode is allowed only if there is no VFs created");
540540
NL_SET_ERR_MSG_MOD(extack, "Changing eswitch mode is allowed only if there is no VFs created");
541541
return -EOPNOTSUPP;

drivers/net/ethernet/intel/ice/ice_ethtool.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,7 @@ static int ice_set_priv_flags(struct net_device *netdev, u32 flags)
12971297
}
12981298

12991299
if (test_bit(ICE_FLAG_VF_VLAN_PRUNING, change_flags) &&
1300-
pf->vfs.num_alloc) {
1300+
ice_has_vfs(pf)) {
13011301
dev_err(dev, "vf-vlan-pruning: VLAN pruning cannot be changed while VFs are active.\n");
13021302
/* toggle bit back to previous state */
13031303
change_bit(ICE_FLAG_VF_VLAN_PRUNING, pf->flags);

drivers/net/ethernet/intel/ice/ice_lib.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ static void ice_vsi_set_num_qs(struct ice_vsi *vsi, struct ice_vf *vf)
215215
/* The number of queues for ctrl VSI is equal to number of VFs.
216216
* Each ring is associated to the corresponding VF_PR netdev.
217217
*/
218-
vsi->alloc_txq = pf->vfs.num_alloc;
219-
vsi->alloc_rxq = pf->vfs.num_alloc;
218+
vsi->alloc_txq = ice_get_num_vfs(pf);
219+
vsi->alloc_rxq = vsi->alloc_txq;
220220
vsi->num_q_vectors = 1;
221221
break;
222222
case ICE_VSI_VF:

drivers/net/ethernet/intel/ice/ice_virtchnl_pf.c

Lines changed: 84 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -175,18 +175,58 @@ struct ice_vsi *ice_get_vf_vsi(struct ice_vf *vf)
175175
}
176176

177177
/**
178-
* ice_validate_vf_id - helper to check if VF ID is valid
179-
* @pf: pointer to the PF structure
180-
* @vf_id: the ID of the VF to check
178+
* ice_get_vf_by_id - Get pointer to VF by ID
179+
* @pf: the PF private structure
180+
* @vf_id: the VF ID to locate
181+
*
182+
* Locate and return a pointer to the VF structure associated with a given ID.
183+
* Returns NULL if the ID does not have a valid VF structure associated with
184+
* it.
181185
*/
182-
static int ice_validate_vf_id(struct ice_pf *pf, u16 vf_id)
186+
struct ice_vf *ice_get_vf_by_id(struct ice_pf *pf, u16 vf_id)
183187
{
184-
/* vf_id range is only valid for 0-255, and should always be unsigned */
188+
if (!pf->vfs.table) {
189+
dev_err(ice_pf_to_dev(pf), "VF table not allocated\n");
190+
return NULL;
191+
}
192+
185193
if (vf_id >= pf->vfs.num_alloc) {
186-
dev_err(ice_pf_to_dev(pf), "Invalid VF ID: %u\n", vf_id);
187-
return -EINVAL;
194+
dev_err(ice_pf_to_dev(pf), "Out of range VF ID: %u\n",
195+
vf_id);
196+
return NULL;
188197
}
189-
return 0;
198+
199+
return &pf->vfs.table[vf_id];
200+
}
201+
202+
/**
203+
* ice_has_vfs - Return true if the PF has any associated VFs
204+
* @pf: the PF private structure
205+
*
206+
* Return whether or not the PF has any allocated VFs.
207+
*
208+
* Note that this function only guarantees that there are no VFs at the point
209+
* of calling it. It does not guarantee that no more VFs will be added.
210+
*/
211+
bool ice_has_vfs(struct ice_pf *pf)
212+
{
213+
return pf->vfs.table && pf->vfs.num_alloc > 0;
214+
}
215+
216+
/**
217+
* ice_get_num_vfs - Get number of allocated VFs
218+
* @pf: the PF private structure
219+
*
220+
* Return the total number of allocated VFs. NOTE: VF IDs are not guaranteed
221+
* to be contiguous. Do not assume that a VF ID is guaranteed to be less than
222+
* the output of this function.
223+
*/
224+
u16 ice_get_num_vfs(struct ice_pf *pf)
225+
{
226+
if (!pf->vfs.table)
227+
return 0;
228+
229+
return pf->vfs.num_alloc;
190230
}
191231

192232
/**
@@ -503,7 +543,7 @@ void ice_free_vfs(struct ice_pf *pf)
503543
struct ice_vf *vf;
504544
unsigned int bkt;
505545

506-
if (!vfs->table)
546+
if (!ice_has_vfs(pf))
507547
return;
508548

509549
ice_eswitch_release(pf);
@@ -1464,7 +1504,7 @@ bool ice_reset_all_vfs(struct ice_pf *pf, bool is_vflr)
14641504
unsigned int bkt;
14651505

14661506
/* If we don't have any VFs, then there is nothing to reset */
1467-
if (!pf->vfs.num_alloc)
1507+
if (!ice_has_vfs(pf))
14681508
return false;
14691509

14701510
/* clear all malicious info if the VFs are getting reset */
@@ -1709,7 +1749,7 @@ void ice_vc_notify_reset(struct ice_pf *pf)
17091749
{
17101750
struct virtchnl_pf_event pfe;
17111751

1712-
if (!pf->vfs.num_alloc)
1752+
if (!ice_has_vfs(pf))
17131753
return;
17141754

17151755
pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
@@ -1725,14 +1765,7 @@ void ice_vc_notify_reset(struct ice_pf *pf)
17251765
static void ice_vc_notify_vf_reset(struct ice_vf *vf)
17261766
{
17271767
struct virtchnl_pf_event pfe;
1728-
struct ice_pf *pf;
1729-
1730-
if (!vf)
1731-
return;
1732-
1733-
pf = vf->pf;
1734-
if (ice_validate_vf_id(pf, vf->vf_id))
1735-
return;
1768+
struct ice_pf *pf = vf->pf;
17361769

17371770
/* Bail out if VF is in disabled state, neither initialized, nor active
17381771
* state - otherwise proceed with notifications
@@ -2097,7 +2130,7 @@ void ice_process_vflr_event(struct ice_pf *pf)
20972130
u32 reg;
20982131

20992132
if (!test_and_clear_bit(ICE_VFLR_EVENT_PENDING, pf->state) ||
2100-
!pf->vfs.num_alloc)
2133+
!ice_has_vfs(pf))
21012134
return;
21022135

21032136
ice_for_each_vf(pf, bkt, vf) {
@@ -2968,10 +3001,11 @@ int ice_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool ena)
29683001
int ret;
29693002

29703003
dev = ice_pf_to_dev(pf);
2971-
if (ice_validate_vf_id(pf, vf_id))
3004+
3005+
vf = ice_get_vf_by_id(pf, vf_id);
3006+
if (!vf)
29723007
return -EINVAL;
29733008

2974-
vf = &pf->vfs.table[vf_id];
29753009
ret = ice_check_vf_ready_for_cfg(vf);
29763010
if (ret)
29773011
return ret;
@@ -4159,8 +4193,6 @@ ice_set_vf_port_vlan(struct net_device *netdev, int vf_id, u16 vlan_id, u8 qos,
41594193
int ret;
41604194

41614195
dev = ice_pf_to_dev(pf);
4162-
if (ice_validate_vf_id(pf, vf_id))
4163-
return -EINVAL;
41644196

41654197
if (vlan_id >= VLAN_N_VID || qos > 7) {
41664198
dev_err(dev, "Invalid Port VLAN parameters for VF %d, ID %d, QoS %d\n",
@@ -4174,7 +4206,10 @@ ice_set_vf_port_vlan(struct net_device *netdev, int vf_id, u16 vlan_id, u8 qos,
41744206
return -EPROTONOSUPPORT;
41754207
}
41764208

4177-
vf = &pf->vfs.table[vf_id];
4209+
vf = ice_get_vf_by_id(pf, vf_id);
4210+
if (!vf)
4211+
return -EINVAL;
4212+
41784213
ret = ice_check_vf_ready_for_cfg(vf);
41794214
if (ret)
41804215
return ret;
@@ -5722,14 +5757,14 @@ void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event)
57225757
int err = 0;
57235758

57245759
dev = ice_pf_to_dev(pf);
5725-
if (ice_validate_vf_id(pf, vf_id)) {
5760+
5761+
vf = ice_get_vf_by_id(pf, vf_id);
5762+
if (!vf) {
57265763
dev_err(dev, "Unable to locate VF for message from VF ID %d, opcode %d, len %d\n",
57275764
vf_id, v_opcode, msglen);
57285765
return;
57295766
}
57305767

5731-
vf = &pf->vfs.table[vf_id];
5732-
57335768
/* Check if VF is disabled. */
57345769
if (test_bit(ICE_VF_STATE_DIS, vf->vf_states)) {
57355770
err = -EPERM;
@@ -5898,14 +5933,15 @@ ice_get_vf_cfg(struct net_device *netdev, int vf_id, struct ifla_vf_info *ivi)
58985933
{
58995934
struct ice_pf *pf = ice_netdev_to_pf(netdev);
59005935
struct ice_vf *vf;
5936+
int ret;
59015937

5902-
if (ice_validate_vf_id(pf, vf_id))
5938+
vf = ice_get_vf_by_id(pf, vf_id);
5939+
if (!vf)
59035940
return -EINVAL;
59045941

5905-
vf = &pf->vfs.table[vf_id];
5906-
5907-
if (ice_check_vf_init(pf, vf))
5908-
return -EBUSY;
5942+
ret = ice_check_vf_ready_for_cfg(vf);
5943+
if (ret)
5944+
return ret;
59095945

59105946
ivi->vf = vf_id;
59115947
ether_addr_copy(ivi->mac, vf->hw_lan_addr.addr);
@@ -5976,15 +6012,15 @@ int ice_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
59766012
struct ice_vf *vf;
59776013
int ret;
59786014

5979-
if (ice_validate_vf_id(pf, vf_id))
5980-
return -EINVAL;
5981-
59826015
if (is_multicast_ether_addr(mac)) {
59836016
netdev_err(netdev, "%pM not a valid unicast address\n", mac);
59846017
return -EINVAL;
59856018
}
59866019

5987-
vf = &pf->vfs.table[vf_id];
6020+
vf = ice_get_vf_by_id(pf, vf_id);
6021+
if (!vf)
6022+
return -EINVAL;
6023+
59886024
/* nothing left to do, unicast MAC already set */
59896025
if (ether_addr_equal(vf->dev_lan_addr.addr, mac) &&
59906026
ether_addr_equal(vf->hw_lan_addr.addr, mac))
@@ -6043,10 +6079,10 @@ int ice_set_vf_trust(struct net_device *netdev, int vf_id, bool trusted)
60436079
return -EOPNOTSUPP;
60446080
}
60456081

6046-
if (ice_validate_vf_id(pf, vf_id))
6082+
vf = ice_get_vf_by_id(pf, vf_id);
6083+
if (!vf)
60476084
return -EINVAL;
60486085

6049-
vf = &pf->vfs.table[vf_id];
60506086
ret = ice_check_vf_ready_for_cfg(vf);
60516087
if (ret)
60526088
return ret;
@@ -6081,10 +6117,10 @@ int ice_set_vf_link_state(struct net_device *netdev, int vf_id, int link_state)
60816117
struct ice_vf *vf;
60826118
int ret;
60836119

6084-
if (ice_validate_vf_id(pf, vf_id))
6120+
vf = ice_get_vf_by_id(pf, vf_id);
6121+
if (!vf)
60856122
return -EINVAL;
60866123

6087-
vf = &pf->vfs.table[vf_id];
60886124
ret = ice_check_vf_ready_for_cfg(vf);
60896125
if (ret)
60906126
return ret;
@@ -6176,10 +6212,11 @@ ice_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
61766212
int ret;
61776213

61786214
dev = ice_pf_to_dev(pf);
6179-
if (ice_validate_vf_id(pf, vf_id))
6215+
6216+
vf = ice_get_vf_by_id(pf, vf_id);
6217+
if (!vf)
61806218
return -EINVAL;
61816219

6182-
vf = &pf->vfs.table[vf_id];
61836220
ret = ice_check_vf_ready_for_cfg(vf);
61846221
if (ret)
61856222
return ret;
@@ -6243,10 +6280,10 @@ int ice_get_vf_stats(struct net_device *netdev, int vf_id,
62436280
struct ice_vf *vf;
62446281
int ret;
62456282

6246-
if (ice_validate_vf_id(pf, vf_id))
6283+
vf = ice_get_vf_by_id(pf, vf_id);
6284+
if (!vf)
62476285
return -EINVAL;
62486286

6249-
vf = &pf->vfs.table[vf_id];
62506287
ret = ice_check_vf_ready_for_cfg(vf);
62516288
if (ret)
62526289
return ret;
@@ -6384,10 +6421,10 @@ ice_is_malicious_vf(struct ice_pf *pf, struct ice_rq_event_info *event,
63846421
struct ice_vf *vf;
63856422
int status;
63866423

6387-
if (ice_validate_vf_id(pf, vf_id))
6424+
vf = ice_get_vf_by_id(pf, vf_id);
6425+
if (!vf)
63886426
return false;
63896427

6390-
vf = &pf->vfs.table[vf_id];
63916428
/* Check if VF is disabled. */
63926429
if (test_bit(ICE_VF_STATE_DIS, vf->vf_states))
63936430
return false;

drivers/net/ethernet/intel/ice/ice_virtchnl_pf.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@
3939
#define ICE_MAX_VF_RESET_TRIES 40
4040
#define ICE_MAX_VF_RESET_SLEEP_MS 20
4141

42+
/* VF Hash Table access functions
43+
*
44+
* These functions provide abstraction for interacting with the VF hash table.
45+
* In general, direct access to the hash table should be avoided outside of
46+
* these functions where possible.
47+
*/
48+
4249
/**
4350
* ice_for_each_vf - Iterate over each VF entry
4451
* @pf: pointer to the PF private structure
@@ -185,6 +192,9 @@ struct ice_vf {
185192
};
186193

187194
#ifdef CONFIG_PCI_IOV
195+
struct ice_vf *ice_get_vf_by_id(struct ice_pf *pf, u16 vf_id);
196+
bool ice_has_vfs(struct ice_pf *pf);
197+
u16 ice_get_num_vfs(struct ice_pf *pf);
188198
struct ice_vsi *ice_get_vf_vsi(struct ice_vf *vf);
189199
void ice_process_vflr_event(struct ice_pf *pf);
190200
int ice_sriov_configure(struct pci_dev *pdev, int num_vfs);
@@ -244,6 +254,21 @@ ice_vc_send_msg_to_vf(struct ice_vf *vf, u32 v_opcode,
244254
bool ice_vc_isvalid_vsi_id(struct ice_vf *vf, u16 vsi_id);
245255
bool ice_vf_is_port_vlan_ena(struct ice_vf *vf);
246256
#else /* CONFIG_PCI_IOV */
257+
static inline struct ice_vf *ice_get_vf_by_id(struct ice_pf *pf, u16 vf_id)
258+
{
259+
return NULL;
260+
}
261+
262+
static inline bool ice_has_vfs(struct ice_pf *pf)
263+
{
264+
return false;
265+
}
266+
267+
static inline u16 ice_get_num_vfs(struct ice_pf *pf)
268+
{
269+
return 0;
270+
}
271+
247272
static inline void ice_process_vflr_event(struct ice_pf *pf) { }
248273
static inline void ice_free_vfs(struct ice_pf *pf) { }
249274
static inline

0 commit comments

Comments
 (0)