1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* Copyright (C) 2018-2021, Intel Corporation. */ 3 4 #ifndef _ICE_VF_LIB_H_ 5 #define _ICE_VF_LIB_H_ 6 7 #include <linux/types.h> 8 #include <linux/hashtable.h> 9 #include <linux/bitmap.h> 10 #include <linux/mutex.h> 11 #include <linux/pci.h> 12 #include <net/devlink.h> 13 #include <linux/avf/virtchnl.h> 14 #include "ice_type.h" 15 #include "ice_flow.h" 16 #include "ice_virtchnl_fdir.h" 17 #include "ice_vsi_vlan_ops.h" 18 19 #define ICE_MAX_SRIOV_VFS 256 20 21 /* VF resource constraints */ 22 #define ICE_MAX_RSS_QS_PER_VF 16 23 24 struct ice_pf; 25 struct ice_vf; 26 struct ice_virtchnl_ops; 27 28 /* VF capabilities */ 29 enum ice_virtchnl_cap { 30 ICE_VIRTCHNL_VF_CAP_PRIVILEGE = 0, 31 }; 32 33 /* Specific VF states */ 34 enum ice_vf_states { 35 ICE_VF_STATE_INIT = 0, /* PF is initializing VF */ 36 ICE_VF_STATE_ACTIVE, /* VF resources are allocated for use */ 37 ICE_VF_STATE_QS_ENA, /* VF queue(s) enabled */ 38 ICE_VF_STATE_DIS, 39 ICE_VF_STATE_MC_PROMISC, 40 ICE_VF_STATE_UC_PROMISC, 41 ICE_VF_STATES_NBITS 42 }; 43 44 struct ice_time_mac { 45 unsigned long time_modified; 46 u8 addr[ETH_ALEN]; 47 }; 48 49 /* VF MDD events print structure */ 50 struct ice_mdd_vf_events { 51 u16 count; /* total count of Rx|Tx events */ 52 /* count number of the last printed event */ 53 u16 last_printed; 54 }; 55 56 /* Structure to store fdir fv entry */ 57 struct ice_fdir_prof_info { 58 struct ice_parser_profile prof; 59 u64 fdir_active_cnt; 60 }; 61 62 struct ice_vf_qs_bw { 63 u32 committed; 64 u32 peak; 65 u16 queue_id; 66 u8 tc; 67 }; 68 69 /* VF operations */ 70 struct ice_vf_ops { 71 enum ice_disq_rst_src reset_type; 72 void (*free)(struct ice_vf *vf); 73 void (*clear_reset_state)(struct ice_vf *vf); 74 void (*clear_mbx_register)(struct ice_vf *vf); 75 void (*trigger_reset_register)(struct ice_vf *vf, bool is_vflr); 76 bool (*poll_reset_status)(struct ice_vf *vf); 77 void (*clear_reset_trigger)(struct ice_vf *vf); 78 void (*irq_close)(struct ice_vf *vf); 79 void (*post_vsi_rebuild)(struct ice_vf *vf); 80 }; 81 82 /* Virtchnl/SR-IOV config info */ 83 struct ice_vfs { 84 DECLARE_HASHTABLE(table, 8); /* table of VF entries */ 85 struct mutex table_lock; /* Lock for protecting the hash table */ 86 u16 num_supported; /* max supported VFs on this PF */ 87 u16 num_qps_per; /* number of queue pairs per VF */ 88 u16 num_msix_per; /* default MSI-X vectors per VF */ 89 unsigned long last_printed_mdd_jiffies; /* MDD message rate limit */ 90 }; 91 92 /* VF information structure */ 93 struct ice_vf { 94 struct hlist_node entry; 95 struct rcu_head rcu; 96 struct kref refcnt; 97 struct ice_pf *pf; 98 struct pci_dev *vfdev; 99 /* Used during virtchnl message handling and NDO ops against the VF 100 * that will trigger a VFR 101 */ 102 struct mutex cfg_lock; 103 104 u16 vf_id; /* VF ID in the PF space */ 105 u16 lan_vsi_idx; /* index into PF struct */ 106 u16 ctrl_vsi_idx; 107 struct ice_vf_fdir fdir; 108 struct ice_fdir_prof_info fdir_prof_info[ICE_MAX_PTGS]; 109 /* first vector index of this VF in the PF space */ 110 int first_vector_idx; 111 struct ice_sw *vf_sw_id; /* switch ID the VF VSIs connect to */ 112 struct virtchnl_version_info vf_ver; 113 u32 driver_caps; /* reported by VF driver */ 114 u8 dev_lan_addr[ETH_ALEN]; 115 u8 hw_lan_addr[ETH_ALEN]; 116 struct ice_time_mac legacy_last_added_umac; 117 DECLARE_BITMAP(txq_ena, ICE_MAX_RSS_QS_PER_VF); 118 DECLARE_BITMAP(rxq_ena, ICE_MAX_RSS_QS_PER_VF); 119 struct ice_vlan port_vlan_info; /* Port VLAN ID, QoS, and TPID */ 120 struct virtchnl_vlan_caps vlan_v2_caps; 121 struct ice_mbx_vf_info mbx_info; 122 u8 pf_set_mac:1; /* VF MAC address set by VMM admin */ 123 u8 trusted:1; 124 u8 spoofchk:1; 125 u8 link_forced:1; 126 u8 link_up:1; /* only valid if VF link is forced */ 127 u8 lldp_tx_ena:1; 128 129 u32 ptp_caps; 130 131 unsigned int min_tx_rate; /* Minimum Tx bandwidth limit in Mbps */ 132 unsigned int max_tx_rate; /* Maximum Tx bandwidth limit in Mbps */ 133 DECLARE_BITMAP(vf_states, ICE_VF_STATES_NBITS); /* VF runtime states */ 134 135 unsigned long vf_caps; /* VF's adv. capabilities */ 136 u8 num_req_qs; /* num of queue pairs requested by VF */ 137 u16 num_mac; 138 u16 num_mac_lldp; 139 u16 num_vf_qs; /* num of queue configured per VF */ 140 u8 vlan_strip_ena; /* Outer and Inner VLAN strip enable */ 141 #define ICE_INNER_VLAN_STRIP_ENA BIT(0) 142 #define ICE_OUTER_VLAN_STRIP_ENA BIT(1) 143 struct ice_mdd_vf_events mdd_rx_events; 144 struct ice_mdd_vf_events mdd_tx_events; 145 DECLARE_BITMAP(opcodes_allowlist, VIRTCHNL_OP_MAX); 146 147 unsigned long repr_id; 148 const struct ice_virtchnl_ops *virtchnl_ops; 149 const struct ice_vf_ops *vf_ops; 150 151 /* devlink port data */ 152 struct devlink_port devlink_port; 153 154 u16 lldp_recipe_id; 155 u16 lldp_rule_id; 156 157 u16 num_msix; /* num of MSI-X configured on this VF */ 158 struct ice_vf_qs_bw qs_bw[ICE_MAX_RSS_QS_PER_VF]; 159 }; 160 161 /* Flags for controlling behavior of ice_reset_vf */ 162 enum ice_vf_reset_flags { 163 ICE_VF_RESET_VFLR = BIT(0), /* Indicate a VFLR reset */ 164 ICE_VF_RESET_NOTIFY = BIT(1), /* Notify VF prior to reset */ 165 ICE_VF_RESET_LOCK = BIT(2), /* Acquire the VF cfg_lock */ 166 }; 167 168 static inline u16 ice_vf_get_port_vlan_id(struct ice_vf *vf) 169 { 170 return vf->port_vlan_info.vid; 171 } 172 173 static inline u8 ice_vf_get_port_vlan_prio(struct ice_vf *vf) 174 { 175 return vf->port_vlan_info.prio; 176 } 177 178 static inline bool ice_vf_is_port_vlan_ena(struct ice_vf *vf) 179 { 180 return (ice_vf_get_port_vlan_id(vf) || ice_vf_get_port_vlan_prio(vf)); 181 } 182 183 static inline u16 ice_vf_get_port_vlan_tpid(struct ice_vf *vf) 184 { 185 return vf->port_vlan_info.tpid; 186 } 187 188 static inline bool ice_vf_is_lldp_ena(struct ice_vf *vf) 189 { 190 return vf->num_mac_lldp && vf->trusted; 191 } 192 193 /* VF Hash Table access functions 194 * 195 * These functions provide abstraction for interacting with the VF hash table. 196 * In general, direct access to the hash table should be avoided outside of 197 * these functions where possible. 198 * 199 * The VF entries in the hash table are protected by reference counting to 200 * track lifetime of accesses from the table. The ice_get_vf_by_id() function 201 * obtains a reference to the VF structure which must be dropped by using 202 * ice_put_vf(). 203 */ 204 205 /** 206 * ice_for_each_vf - Iterate over each VF entry 207 * @pf: pointer to the PF private structure 208 * @bkt: bucket index used for iteration 209 * @vf: pointer to the VF entry currently being processed in the loop 210 * 211 * The bkt variable is an unsigned integer iterator used to traverse the VF 212 * entries. It is *not* guaranteed to be the VF's vf_id. Do not assume it is. 213 * Use vf->vf_id to get the id number if needed. 214 * 215 * The caller is expected to be under the table_lock mutex for the entire 216 * loop. Use this iterator if your loop is long or if it might sleep. 217 */ 218 #define ice_for_each_vf(pf, bkt, vf) \ 219 hash_for_each((pf)->vfs.table, (bkt), (vf), entry) 220 221 /** 222 * ice_for_each_vf_rcu - Iterate over each VF entry protected by RCU 223 * @pf: pointer to the PF private structure 224 * @bkt: bucket index used for iteration 225 * @vf: pointer to the VF entry currently being processed in the loop 226 * 227 * The bkt variable is an unsigned integer iterator used to traverse the VF 228 * entries. It is *not* guaranteed to be the VF's vf_id. Do not assume it is. 229 * Use vf->vf_id to get the id number if needed. 230 * 231 * The caller is expected to be under rcu_read_lock() for the entire loop. 232 * Only use this iterator if your loop is short and you can guarantee it does 233 * not sleep. 234 */ 235 #define ice_for_each_vf_rcu(pf, bkt, vf) \ 236 hash_for_each_rcu((pf)->vfs.table, (bkt), (vf), entry) 237 238 #ifdef CONFIG_PCI_IOV 239 struct ice_vf *ice_get_vf_by_id(struct ice_pf *pf, u16 vf_id); 240 void ice_put_vf(struct ice_vf *vf); 241 bool ice_has_vfs(struct ice_pf *pf); 242 u16 ice_get_num_vfs(struct ice_pf *pf); 243 struct ice_vsi *ice_get_vf_vsi(struct ice_vf *vf); 244 bool ice_is_vf_disabled(struct ice_vf *vf); 245 int ice_check_vf_ready_for_cfg(struct ice_vf *vf); 246 void ice_set_vf_state_dis(struct ice_vf *vf); 247 bool ice_is_any_vf_in_unicast_promisc(struct ice_pf *pf); 248 void 249 ice_vf_get_promisc_masks(struct ice_vf *vf, struct ice_vsi *vsi, 250 u8 *ucast_m, u8 *mcast_m); 251 int 252 ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m); 253 int 254 ice_vf_clear_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m); 255 int ice_reset_vf(struct ice_vf *vf, u32 flags); 256 void ice_reset_all_vfs(struct ice_pf *pf); 257 struct ice_vsi *ice_get_vf_ctrl_vsi(struct ice_pf *pf, struct ice_vsi *vsi); 258 void ice_vf_update_mac_lldp_num(struct ice_vf *vf, struct ice_vsi *vsi, 259 bool incr); 260 #else /* CONFIG_PCI_IOV */ 261 static inline struct ice_vf *ice_get_vf_by_id(struct ice_pf *pf, u16 vf_id) 262 { 263 return NULL; 264 } 265 266 static inline void ice_put_vf(struct ice_vf *vf) 267 { 268 } 269 270 static inline bool ice_has_vfs(struct ice_pf *pf) 271 { 272 return false; 273 } 274 275 static inline u16 ice_get_num_vfs(struct ice_pf *pf) 276 { 277 return 0; 278 } 279 280 static inline struct ice_vsi *ice_get_vf_vsi(struct ice_vf *vf) 281 { 282 return NULL; 283 } 284 285 static inline bool ice_is_vf_disabled(struct ice_vf *vf) 286 { 287 return true; 288 } 289 290 static inline int ice_check_vf_ready_for_cfg(struct ice_vf *vf) 291 { 292 return -EOPNOTSUPP; 293 } 294 295 static inline void ice_set_vf_state_dis(struct ice_vf *vf) 296 { 297 } 298 299 static inline bool ice_is_any_vf_in_unicast_promisc(struct ice_pf *pf) 300 { 301 return false; 302 } 303 304 static inline int 305 ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m) 306 { 307 return -EOPNOTSUPP; 308 } 309 310 static inline int 311 ice_vf_clear_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m) 312 { 313 return -EOPNOTSUPP; 314 } 315 316 static inline int ice_reset_vf(struct ice_vf *vf, u32 flags) 317 { 318 return 0; 319 } 320 321 static inline void ice_reset_all_vfs(struct ice_pf *pf) 322 { 323 } 324 325 static inline struct ice_vsi * 326 ice_get_vf_ctrl_vsi(struct ice_pf *pf, struct ice_vsi *vsi) 327 { 328 return NULL; 329 } 330 #endif /* !CONFIG_PCI_IOV */ 331 332 #endif /* _ICE_VF_LIB_H_ */ 333