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