1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (C) 2018-2021, Intel Corporation. */
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>
16 #include "ice_virtchnl_fdir.h"
17 #include "ice_vsi_vlan_ops.h"
19 #define ICE_MAX_SRIOV_VFS 256
21 /* VF resource constraints */
22 #define ICE_MAX_RSS_QS_PER_VF 16
26 struct ice_virtchnl_ops;
29 enum ice_virtchnl_cap {
30 ICE_VIRTCHNL_VF_CAP_PRIVILEGE = 0,
33 /* Specific 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 */
39 ICE_VF_STATE_MC_PROMISC,
40 ICE_VF_STATE_UC_PROMISC,
45 unsigned long time_modified;
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 */
56 /* Structure to store fdir fv entry */
57 struct ice_fdir_prof_info {
58 struct ice_parser_profile prof;
64 enum ice_disq_rst_src reset_type;
65 void (*free)(struct ice_vf *vf);
66 void (*clear_reset_state)(struct ice_vf *vf);
67 void (*clear_mbx_register)(struct ice_vf *vf);
68 void (*trigger_reset_register)(struct ice_vf *vf, bool is_vflr);
69 bool (*poll_reset_status)(struct ice_vf *vf);
70 void (*clear_reset_trigger)(struct ice_vf *vf);
71 void (*irq_close)(struct ice_vf *vf);
72 void (*post_vsi_rebuild)(struct ice_vf *vf);
75 /* Virtchnl/SR-IOV config info */
77 DECLARE_HASHTABLE(table, 8); /* table of VF entries */
78 struct mutex table_lock; /* Lock for protecting the hash table */
79 u16 num_supported; /* max supported VFs on this PF */
80 u16 num_qps_per; /* number of queue pairs per VF */
81 u16 num_msix_per; /* default MSI-X vectors per VF */
82 unsigned long last_printed_mdd_jiffies; /* MDD message rate limit */
85 /* VF information structure */
87 struct hlist_node entry;
91 struct pci_dev *vfdev;
92 /* Used during virtchnl message handling and NDO ops against the VF
93 * that will trigger a VFR
95 struct mutex cfg_lock;
97 u16 vf_id; /* VF ID in the PF space */
98 u16 lan_vsi_idx; /* index into PF struct */
100 struct ice_vf_fdir fdir;
101 struct ice_fdir_prof_info fdir_prof_info[ICE_MAX_PTGS];
102 /* first vector index of this VF in the PF space */
103 int first_vector_idx;
104 struct ice_sw *vf_sw_id; /* switch ID the VF VSIs connect to */
105 struct virtchnl_version_info vf_ver;
106 u32 driver_caps; /* reported by VF driver */
107 u8 dev_lan_addr[ETH_ALEN];
108 u8 hw_lan_addr[ETH_ALEN];
109 struct ice_time_mac legacy_last_added_umac;
110 DECLARE_BITMAP(txq_ena, ICE_MAX_RSS_QS_PER_VF);
111 DECLARE_BITMAP(rxq_ena, ICE_MAX_RSS_QS_PER_VF);
112 struct ice_vlan port_vlan_info; /* Port VLAN ID, QoS, and TPID */
113 struct virtchnl_vlan_caps vlan_v2_caps;
114 struct ice_mbx_vf_info mbx_info;
115 u8 pf_set_mac:1; /* VF MAC address set by VMM admin */
119 u8 link_up:1; /* only valid if VF link is forced */
120 unsigned int min_tx_rate; /* Minimum Tx bandwidth limit in Mbps */
121 unsigned int max_tx_rate; /* Maximum Tx bandwidth limit in Mbps */
122 DECLARE_BITMAP(vf_states, ICE_VF_STATES_NBITS); /* VF runtime states */
124 unsigned long vf_caps; /* VF's adv. capabilities */
125 u8 num_req_qs; /* num of queue pairs requested by VF */
127 u16 num_vf_qs; /* num of queue configured per VF */
128 u8 vlan_strip_ena; /* Outer and Inner VLAN strip enable */
129 #define ICE_INNER_VLAN_STRIP_ENA BIT(0)
130 #define ICE_OUTER_VLAN_STRIP_ENA BIT(1)
131 struct ice_mdd_vf_events mdd_rx_events;
132 struct ice_mdd_vf_events mdd_tx_events;
133 DECLARE_BITMAP(opcodes_allowlist, VIRTCHNL_OP_MAX);
135 unsigned long repr_id;
136 const struct ice_virtchnl_ops *virtchnl_ops;
137 const struct ice_vf_ops *vf_ops;
139 /* devlink port data */
140 struct devlink_port devlink_port;
142 u16 num_msix; /* num of MSI-X configured on this VF */
145 /* Flags for controlling behavior of ice_reset_vf */
146 enum ice_vf_reset_flags {
147 ICE_VF_RESET_VFLR = BIT(0), /* Indicate a VFLR reset */
148 ICE_VF_RESET_NOTIFY = BIT(1), /* Notify VF prior to reset */
149 ICE_VF_RESET_LOCK = BIT(2), /* Acquire the VF cfg_lock */
152 static inline u16 ice_vf_get_port_vlan_id(struct ice_vf *vf)
154 return vf->port_vlan_info.vid;
157 static inline u8 ice_vf_get_port_vlan_prio(struct ice_vf *vf)
159 return vf->port_vlan_info.prio;
162 static inline bool ice_vf_is_port_vlan_ena(struct ice_vf *vf)
164 return (ice_vf_get_port_vlan_id(vf) || ice_vf_get_port_vlan_prio(vf));
167 static inline u16 ice_vf_get_port_vlan_tpid(struct ice_vf *vf)
169 return vf->port_vlan_info.tpid;
172 /* VF Hash Table access functions
174 * These functions provide abstraction for interacting with the VF hash table.
175 * In general, direct access to the hash table should be avoided outside of
176 * these functions where possible.
178 * The VF entries in the hash table are protected by reference counting to
179 * track lifetime of accesses from the table. The ice_get_vf_by_id() function
180 * obtains a reference to the VF structure which must be dropped by using
185 * ice_for_each_vf - Iterate over each VF entry
186 * @pf: pointer to the PF private structure
187 * @bkt: bucket index used for iteration
188 * @vf: pointer to the VF entry currently being processed in the loop
190 * The bkt variable is an unsigned integer iterator used to traverse the VF
191 * entries. It is *not* guaranteed to be the VF's vf_id. Do not assume it is.
192 * Use vf->vf_id to get the id number if needed.
194 * The caller is expected to be under the table_lock mutex for the entire
195 * loop. Use this iterator if your loop is long or if it might sleep.
197 #define ice_for_each_vf(pf, bkt, vf) \
198 hash_for_each((pf)->vfs.table, (bkt), (vf), entry)
201 * ice_for_each_vf_rcu - Iterate over each VF entry protected by RCU
202 * @pf: pointer to the PF private structure
203 * @bkt: bucket index used for iteration
204 * @vf: pointer to the VF entry currently being processed in the loop
206 * The bkt variable is an unsigned integer iterator used to traverse the VF
207 * entries. It is *not* guaranteed to be the VF's vf_id. Do not assume it is.
208 * Use vf->vf_id to get the id number if needed.
210 * The caller is expected to be under rcu_read_lock() for the entire loop.
211 * Only use this iterator if your loop is short and you can guarantee it does
214 #define ice_for_each_vf_rcu(pf, bkt, vf) \
215 hash_for_each_rcu((pf)->vfs.table, (bkt), (vf), entry)
217 #ifdef CONFIG_PCI_IOV
218 struct ice_vf *ice_get_vf_by_id(struct ice_pf *pf, u16 vf_id);
219 void ice_put_vf(struct ice_vf *vf);
220 bool ice_has_vfs(struct ice_pf *pf);
221 u16 ice_get_num_vfs(struct ice_pf *pf);
222 struct ice_vsi *ice_get_vf_vsi(struct ice_vf *vf);
223 bool ice_is_vf_disabled(struct ice_vf *vf);
224 int ice_check_vf_ready_for_cfg(struct ice_vf *vf);
225 void ice_set_vf_state_dis(struct ice_vf *vf);
226 bool ice_is_any_vf_in_unicast_promisc(struct ice_pf *pf);
228 ice_vf_get_promisc_masks(struct ice_vf *vf, struct ice_vsi *vsi,
229 u8 *ucast_m, u8 *mcast_m);
231 ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m);
233 ice_vf_clear_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m);
234 int ice_reset_vf(struct ice_vf *vf, u32 flags);
235 void ice_reset_all_vfs(struct ice_pf *pf);
236 struct ice_vsi *ice_get_vf_ctrl_vsi(struct ice_pf *pf, struct ice_vsi *vsi);
237 #else /* CONFIG_PCI_IOV */
238 static inline struct ice_vf *ice_get_vf_by_id(struct ice_pf *pf, u16 vf_id)
243 static inline void ice_put_vf(struct ice_vf *vf)
247 static inline bool ice_has_vfs(struct ice_pf *pf)
252 static inline u16 ice_get_num_vfs(struct ice_pf *pf)
257 static inline struct ice_vsi *ice_get_vf_vsi(struct ice_vf *vf)
262 static inline bool ice_is_vf_disabled(struct ice_vf *vf)
267 static inline int ice_check_vf_ready_for_cfg(struct ice_vf *vf)
272 static inline void ice_set_vf_state_dis(struct ice_vf *vf)
276 static inline bool ice_is_any_vf_in_unicast_promisc(struct ice_pf *pf)
282 ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m)
288 ice_vf_clear_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m)
293 static inline int ice_reset_vf(struct ice_vf *vf, u32 flags)
298 static inline void ice_reset_all_vfs(struct ice_pf *pf)
302 static inline struct ice_vsi *
303 ice_get_vf_ctrl_vsi(struct ice_pf *pf, struct ice_vsi *vsi)
307 #endif /* !CONFIG_PCI_IOV */
309 #endif /* _ICE_VF_LIB_H_ */