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 "virt/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 enum ice_hash_ip_ctx_type { 57 ICE_HASH_IP_CTX_IP = 0, 58 ICE_HASH_IP_CTX_IP_ESP, 59 ICE_HASH_IP_CTX_IP_UDP_ESP, 60 ICE_HASH_IP_CTX_IP_AH, 61 ICE_HASH_IP_CTX_IP_PFCP, 62 ICE_HASH_IP_CTX_IP_UDP, 63 ICE_HASH_IP_CTX_IP_TCP, 64 ICE_HASH_IP_CTX_IP_SCTP, 65 ICE_HASH_IP_CTX_MAX, 66 }; 67 68 struct ice_vf_hash_ip_ctx { 69 struct ice_rss_hash_cfg ctx[ICE_HASH_IP_CTX_MAX]; 70 }; 71 72 enum ice_hash_gtpu_ctx_type { 73 ICE_HASH_GTPU_CTX_EH_IP = 0, 74 ICE_HASH_GTPU_CTX_EH_IP_UDP, 75 ICE_HASH_GTPU_CTX_EH_IP_TCP, 76 ICE_HASH_GTPU_CTX_UP_IP, 77 ICE_HASH_GTPU_CTX_UP_IP_UDP, 78 ICE_HASH_GTPU_CTX_UP_IP_TCP, 79 ICE_HASH_GTPU_CTX_DW_IP, 80 ICE_HASH_GTPU_CTX_DW_IP_UDP, 81 ICE_HASH_GTPU_CTX_DW_IP_TCP, 82 ICE_HASH_GTPU_CTX_MAX, 83 }; 84 85 struct ice_vf_hash_gtpu_ctx { 86 struct ice_rss_hash_cfg ctx[ICE_HASH_GTPU_CTX_MAX]; 87 }; 88 89 struct ice_vf_hash_ctx { 90 struct ice_vf_hash_ip_ctx v4; 91 struct ice_vf_hash_ip_ctx v6; 92 struct ice_vf_hash_gtpu_ctx ipv4; 93 struct ice_vf_hash_gtpu_ctx ipv6; 94 }; 95 96 /* Structure to store fdir fv entry */ 97 struct ice_fdir_prof_info { 98 struct ice_parser_profile prof; 99 u64 fdir_active_cnt; 100 }; 101 102 struct ice_vf_qs_bw { 103 u32 committed; 104 u32 peak; 105 u16 queue_id; 106 u8 tc; 107 }; 108 109 /* Structure to store RSS field vector entry */ 110 struct ice_rss_prof_info { 111 struct ice_parser_profile prof; 112 bool symm; 113 }; 114 115 /* VF operations */ 116 struct ice_vf_ops { 117 enum ice_disq_rst_src reset_type; 118 void (*free)(struct ice_vf *vf); 119 void (*clear_reset_state)(struct ice_vf *vf); 120 void (*clear_mbx_register)(struct ice_vf *vf); 121 void (*trigger_reset_register)(struct ice_vf *vf, bool is_vflr); 122 bool (*poll_reset_status)(struct ice_vf *vf); 123 void (*clear_reset_trigger)(struct ice_vf *vf); 124 void (*irq_close)(struct ice_vf *vf); 125 void (*post_vsi_rebuild)(struct ice_vf *vf); 126 }; 127 128 /* Virtchnl/SR-IOV config info */ 129 struct ice_vfs { 130 DECLARE_HASHTABLE(table, 8); /* table of VF entries */ 131 struct mutex table_lock; /* Lock for protecting the hash table */ 132 u16 num_supported; /* max supported VFs on this PF */ 133 u16 num_qps_per; /* number of queue pairs per VF */ 134 u16 num_msix_per; /* default MSI-X vectors per VF */ 135 unsigned long last_printed_mdd_jiffies; /* MDD message rate limit */ 136 }; 137 138 /* VF information structure */ 139 struct ice_vf { 140 struct hlist_node entry; 141 struct rcu_head rcu; 142 struct kref refcnt; 143 struct ice_pf *pf; 144 struct pci_dev *vfdev; 145 /* Used during virtchnl message handling and NDO ops against the VF 146 * that will trigger a VFR 147 */ 148 struct mutex cfg_lock; 149 150 u16 vf_id; /* VF ID in the PF space */ 151 u16 lan_vsi_idx; /* index into PF struct */ 152 u16 ctrl_vsi_idx; 153 struct ice_vf_fdir fdir; 154 struct ice_fdir_prof_info fdir_prof_info[ICE_MAX_PTGS]; 155 struct ice_rss_prof_info rss_prof_info[ICE_MAX_PTGS]; 156 struct ice_vf_hash_ctx hash_ctx; 157 u64 rss_hashcfg; /* RSS hash configuration */ 158 struct ice_sw *vf_sw_id; /* switch ID the VF VSIs connect to */ 159 struct virtchnl_version_info vf_ver; 160 u32 driver_caps; /* reported by VF driver */ 161 u8 dev_lan_addr[ETH_ALEN]; 162 u8 hw_lan_addr[ETH_ALEN]; 163 struct ice_time_mac legacy_last_added_umac; 164 DECLARE_BITMAP(txq_ena, ICE_MAX_RSS_QS_PER_VF); 165 DECLARE_BITMAP(rxq_ena, ICE_MAX_RSS_QS_PER_VF); 166 struct ice_vlan port_vlan_info; /* Port VLAN ID, QoS, and TPID */ 167 struct virtchnl_vlan_caps vlan_v2_caps; 168 struct ice_mbx_vf_info mbx_info; 169 u8 pf_set_mac:1; /* VF MAC address set by VMM admin */ 170 u8 trusted:1; 171 u8 spoofchk:1; 172 u8 link_forced:1; 173 u8 link_up:1; /* only valid if VF link is forced */ 174 u8 lldp_tx_ena:1; 175 176 u16 num_msix; /* num of MSI-X configured on this VF */ 177 178 u32 ptp_caps; 179 180 unsigned int min_tx_rate; /* Minimum Tx bandwidth limit in Mbps */ 181 unsigned int max_tx_rate; /* Maximum Tx bandwidth limit in Mbps */ 182 /* first vector index of this VF in the PF space */ 183 int first_vector_idx; 184 DECLARE_BITMAP(vf_states, ICE_VF_STATES_NBITS); /* VF runtime states */ 185 186 unsigned long vf_caps; /* VF's adv. capabilities */ 187 u8 num_req_qs; /* num of queue pairs requested by VF */ 188 u16 num_mac; 189 u16 num_mac_lldp; 190 u16 num_vf_qs; /* num of queue configured per VF */ 191 u8 vlan_strip_ena; /* Outer and Inner VLAN strip enable */ 192 #define ICE_INNER_VLAN_STRIP_ENA BIT(0) 193 #define ICE_OUTER_VLAN_STRIP_ENA BIT(1) 194 struct ice_mdd_vf_events mdd_rx_events; 195 struct ice_mdd_vf_events mdd_tx_events; 196 DECLARE_BITMAP(opcodes_allowlist, VIRTCHNL_OP_MAX); 197 198 unsigned long repr_id; 199 const struct ice_virtchnl_ops *virtchnl_ops; 200 const struct ice_vf_ops *vf_ops; 201 202 /* devlink port data */ 203 struct devlink_port devlink_port; 204 205 u16 lldp_recipe_id; 206 u16 lldp_rule_id; 207 208 struct ice_vf_qs_bw qs_bw[ICE_MAX_RSS_QS_PER_VF]; 209 }; 210 211 /* Flags for controlling behavior of ice_reset_vf */ 212 enum ice_vf_reset_flags { 213 ICE_VF_RESET_VFLR = BIT(0), /* Indicate a VFLR reset */ 214 ICE_VF_RESET_NOTIFY = BIT(1), /* Notify VF prior to reset */ 215 ICE_VF_RESET_LOCK = BIT(2), /* Acquire the VF cfg_lock */ 216 }; 217 218 static inline u16 ice_vf_get_port_vlan_id(struct ice_vf *vf) 219 { 220 return vf->port_vlan_info.vid; 221 } 222 223 static inline u8 ice_vf_get_port_vlan_prio(struct ice_vf *vf) 224 { 225 return vf->port_vlan_info.prio; 226 } 227 228 static inline bool ice_vf_is_port_vlan_ena(struct ice_vf *vf) 229 { 230 return (ice_vf_get_port_vlan_id(vf) || ice_vf_get_port_vlan_prio(vf)); 231 } 232 233 static inline u16 ice_vf_get_port_vlan_tpid(struct ice_vf *vf) 234 { 235 return vf->port_vlan_info.tpid; 236 } 237 238 static inline bool ice_vf_is_lldp_ena(struct ice_vf *vf) 239 { 240 return vf->num_mac_lldp && vf->trusted; 241 } 242 243 /* VF Hash Table access functions 244 * 245 * These functions provide abstraction for interacting with the VF hash table. 246 * In general, direct access to the hash table should be avoided outside of 247 * these functions where possible. 248 * 249 * The VF entries in the hash table are protected by reference counting to 250 * track lifetime of accesses from the table. The ice_get_vf_by_id() function 251 * obtains a reference to the VF structure which must be dropped by using 252 * ice_put_vf(). 253 */ 254 255 /** 256 * ice_for_each_vf - Iterate over each VF entry 257 * @pf: pointer to the PF private structure 258 * @bkt: bucket index used for iteration 259 * @vf: pointer to the VF entry currently being processed in the loop 260 * 261 * The bkt variable is an unsigned integer iterator used to traverse the VF 262 * entries. It is *not* guaranteed to be the VF's vf_id. Do not assume it is. 263 * Use vf->vf_id to get the id number if needed. 264 * 265 * The caller is expected to be under the table_lock mutex for the entire 266 * loop. Use this iterator if your loop is long or if it might sleep. 267 */ 268 #define ice_for_each_vf(pf, bkt, vf) \ 269 hash_for_each((pf)->vfs.table, (bkt), (vf), entry) 270 271 /** 272 * ice_for_each_vf_rcu - Iterate over each VF entry protected by RCU 273 * @pf: pointer to the PF private structure 274 * @bkt: bucket index used for iteration 275 * @vf: pointer to the VF entry currently being processed in the loop 276 * 277 * The bkt variable is an unsigned integer iterator used to traverse the VF 278 * entries. It is *not* guaranteed to be the VF's vf_id. Do not assume it is. 279 * Use vf->vf_id to get the id number if needed. 280 * 281 * The caller is expected to be under rcu_read_lock() for the entire loop. 282 * Only use this iterator if your loop is short and you can guarantee it does 283 * not sleep. 284 */ 285 #define ice_for_each_vf_rcu(pf, bkt, vf) \ 286 hash_for_each_rcu((pf)->vfs.table, (bkt), (vf), entry) 287 288 #ifdef CONFIG_PCI_IOV 289 struct ice_vf *ice_get_vf_by_id(struct ice_pf *pf, u16 vf_id); 290 291 static inline struct ice_vf *ice_get_vf_by_dev(struct ice_pf *pf, 292 struct pci_dev *vf_dev) 293 { 294 int vf_id = pci_iov_vf_id(vf_dev); 295 296 if (vf_id < 0) 297 return NULL; 298 299 return ice_get_vf_by_id(pf, pci_iov_vf_id(vf_dev)); 300 } 301 302 void ice_put_vf(struct ice_vf *vf); 303 bool ice_has_vfs(struct ice_pf *pf); 304 u16 ice_get_num_vfs(struct ice_pf *pf); 305 struct ice_vsi *ice_get_vf_vsi(struct ice_vf *vf); 306 bool ice_is_vf_disabled(struct ice_vf *vf); 307 int ice_check_vf_ready_for_cfg(struct ice_vf *vf); 308 void ice_set_vf_state_dis(struct ice_vf *vf); 309 bool ice_is_any_vf_in_unicast_promisc(struct ice_pf *pf); 310 void 311 ice_vf_get_promisc_masks(struct ice_vf *vf, struct ice_vsi *vsi, 312 u8 *ucast_m, u8 *mcast_m); 313 int 314 ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m); 315 int 316 ice_vf_clear_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m); 317 int ice_reset_vf(struct ice_vf *vf, u32 flags); 318 void ice_reset_all_vfs(struct ice_pf *pf); 319 struct ice_vsi *ice_get_vf_ctrl_vsi(struct ice_pf *pf, struct ice_vsi *vsi); 320 void ice_vf_update_mac_lldp_num(struct ice_vf *vf, struct ice_vsi *vsi, 321 bool incr); 322 #else /* CONFIG_PCI_IOV */ 323 static inline struct ice_vf *ice_get_vf_by_id(struct ice_pf *pf, u16 vf_id) 324 { 325 return NULL; 326 } 327 328 static inline struct ice_vf *ice_get_vf_by_dev(struct ice_pf *pf, 329 struct pci_dev *vf_dev) 330 { 331 return NULL; 332 } 333 334 static inline void ice_put_vf(struct ice_vf *vf) 335 { 336 } 337 338 static inline bool ice_has_vfs(struct ice_pf *pf) 339 { 340 return false; 341 } 342 343 static inline u16 ice_get_num_vfs(struct ice_pf *pf) 344 { 345 return 0; 346 } 347 348 static inline struct ice_vsi *ice_get_vf_vsi(struct ice_vf *vf) 349 { 350 return NULL; 351 } 352 353 static inline bool ice_is_vf_disabled(struct ice_vf *vf) 354 { 355 return true; 356 } 357 358 static inline int ice_check_vf_ready_for_cfg(struct ice_vf *vf) 359 { 360 return -EOPNOTSUPP; 361 } 362 363 static inline void ice_set_vf_state_dis(struct ice_vf *vf) 364 { 365 } 366 367 static inline bool ice_is_any_vf_in_unicast_promisc(struct ice_pf *pf) 368 { 369 return false; 370 } 371 372 static inline int 373 ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m) 374 { 375 return -EOPNOTSUPP; 376 } 377 378 static inline int 379 ice_vf_clear_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m) 380 { 381 return -EOPNOTSUPP; 382 } 383 384 static inline int ice_reset_vf(struct ice_vf *vf, u32 flags) 385 { 386 return 0; 387 } 388 389 static inline void ice_reset_all_vfs(struct ice_pf *pf) 390 { 391 } 392 393 static inline struct ice_vsi * 394 ice_get_vf_ctrl_vsi(struct ice_pf *pf, struct ice_vsi *vsi) 395 { 396 return NULL; 397 } 398 #endif /* !CONFIG_PCI_IOV */ 399 400 #endif /* _ICE_VF_LIB_H_ */ 401