@@ -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)
17251765static 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;
0 commit comments