1109aba47SJacob Keller /* SPDX-License-Identifier: GPL-2.0 */
2109aba47SJacob Keller /* Copyright (C) 2018-2021, Intel Corporation. */
3109aba47SJacob Keller 
4109aba47SJacob Keller #ifndef _ICE_VF_LIB_H_
5109aba47SJacob Keller #define _ICE_VF_LIB_H_
6109aba47SJacob Keller 
7109aba47SJacob Keller #include <linux/types.h>
8109aba47SJacob Keller #include <linux/hashtable.h>
9109aba47SJacob Keller #include <linux/bitmap.h>
10109aba47SJacob Keller #include <linux/mutex.h>
11109aba47SJacob Keller #include <linux/pci.h>
12109aba47SJacob Keller #include <net/devlink.h>
13109aba47SJacob Keller #include <linux/avf/virtchnl.h>
14109aba47SJacob Keller #include "ice_type.h"
15109aba47SJacob Keller #include "ice_virtchnl_fdir.h"
16109aba47SJacob Keller #include "ice_vsi_vlan_ops.h"
17109aba47SJacob Keller 
18109aba47SJacob Keller #define ICE_MAX_SRIOV_VFS		256
19109aba47SJacob Keller 
20109aba47SJacob Keller /* VF resource constraints */
21109aba47SJacob Keller #define ICE_MAX_RSS_QS_PER_VF	16
22109aba47SJacob Keller 
23109aba47SJacob Keller struct ice_pf;
24109aba47SJacob Keller struct ice_vf;
25109aba47SJacob Keller struct ice_virtchnl_ops;
26109aba47SJacob Keller 
27109aba47SJacob Keller /* VF capabilities */
28109aba47SJacob Keller enum ice_virtchnl_cap {
29109aba47SJacob Keller 	ICE_VIRTCHNL_VF_CAP_PRIVILEGE = 0,
30109aba47SJacob Keller };
31109aba47SJacob Keller 
32109aba47SJacob Keller /* Specific VF states */
33109aba47SJacob Keller enum ice_vf_states {
34109aba47SJacob Keller 	ICE_VF_STATE_INIT = 0,		/* PF is initializing VF */
35109aba47SJacob Keller 	ICE_VF_STATE_ACTIVE,		/* VF resources are allocated for use */
36109aba47SJacob Keller 	ICE_VF_STATE_QS_ENA,		/* VF queue(s) enabled */
37109aba47SJacob Keller 	ICE_VF_STATE_DIS,
38109aba47SJacob Keller 	ICE_VF_STATE_MC_PROMISC,
39109aba47SJacob Keller 	ICE_VF_STATE_UC_PROMISC,
40109aba47SJacob Keller 	ICE_VF_STATES_NBITS
41109aba47SJacob Keller };
42109aba47SJacob Keller 
43109aba47SJacob Keller struct ice_time_mac {
44109aba47SJacob Keller 	unsigned long time_modified;
45109aba47SJacob Keller 	u8 addr[ETH_ALEN];
46109aba47SJacob Keller };
47109aba47SJacob Keller 
48109aba47SJacob Keller /* VF MDD events print structure */
49109aba47SJacob Keller struct ice_mdd_vf_events {
50109aba47SJacob Keller 	u16 count;			/* total count of Rx|Tx events */
51109aba47SJacob Keller 	/* count number of the last printed event */
52109aba47SJacob Keller 	u16 last_printed;
53109aba47SJacob Keller };
54109aba47SJacob Keller 
559c6f7878SJacob Keller /* VF operations */
569c6f7878SJacob Keller struct ice_vf_ops {
579c6f7878SJacob Keller 	enum ice_disq_rst_src reset_type;
589c6f7878SJacob Keller 	void (*free)(struct ice_vf *vf);
599c6f7878SJacob Keller 	void (*clear_mbx_register)(struct ice_vf *vf);
609c6f7878SJacob Keller 	void (*trigger_reset_register)(struct ice_vf *vf, bool is_vflr);
619c6f7878SJacob Keller 	bool (*poll_reset_status)(struct ice_vf *vf);
629c6f7878SJacob Keller 	void (*clear_reset_trigger)(struct ice_vf *vf);
639c6f7878SJacob Keller 	int (*vsi_rebuild)(struct ice_vf *vf);
649c6f7878SJacob Keller 	void (*post_vsi_rebuild)(struct ice_vf *vf);
659c6f7878SJacob Keller };
669c6f7878SJacob Keller 
67109aba47SJacob Keller /* Virtchnl/SR-IOV config info */
68109aba47SJacob Keller struct ice_vfs {
69109aba47SJacob Keller 	DECLARE_HASHTABLE(table, 8);	/* table of VF entries */
70109aba47SJacob Keller 	struct mutex table_lock;	/* Lock for protecting the hash table */
71109aba47SJacob Keller 	u16 num_supported;		/* max supported VFs on this PF */
72109aba47SJacob Keller 	u16 num_qps_per;		/* number of queue pairs per VF */
73109aba47SJacob Keller 	u16 num_msix_per;		/* number of MSI-X vectors per VF */
74109aba47SJacob Keller 	unsigned long last_printed_mdd_jiffies;	/* MDD message rate limit */
75109aba47SJacob Keller 	DECLARE_BITMAP(malvfs, ICE_MAX_SRIOV_VFS); /* malicious VF indicator */
76109aba47SJacob Keller };
77109aba47SJacob Keller 
78109aba47SJacob Keller /* VF information structure */
79109aba47SJacob Keller struct ice_vf {
80109aba47SJacob Keller 	struct hlist_node entry;
81109aba47SJacob Keller 	struct rcu_head rcu;
82109aba47SJacob Keller 	struct kref refcnt;
83109aba47SJacob Keller 	struct ice_pf *pf;
84109aba47SJacob Keller 
85109aba47SJacob Keller 	/* Used during virtchnl message handling and NDO ops against the VF
86109aba47SJacob Keller 	 * that will trigger a VFR
87109aba47SJacob Keller 	 */
88109aba47SJacob Keller 	struct mutex cfg_lock;
89109aba47SJacob Keller 
90109aba47SJacob Keller 	u16 vf_id;			/* VF ID in the PF space */
91109aba47SJacob Keller 	u16 lan_vsi_idx;		/* index into PF struct */
92109aba47SJacob Keller 	u16 ctrl_vsi_idx;
93109aba47SJacob Keller 	struct ice_vf_fdir fdir;
94109aba47SJacob Keller 	/* first vector index of this VF in the PF space */
95109aba47SJacob Keller 	int first_vector_idx;
96109aba47SJacob Keller 	struct ice_sw *vf_sw_id;	/* switch ID the VF VSIs connect to */
97109aba47SJacob Keller 	struct virtchnl_version_info vf_ver;
98109aba47SJacob Keller 	u32 driver_caps;		/* reported by VF driver */
99109aba47SJacob Keller 	struct virtchnl_ether_addr dev_lan_addr;
100109aba47SJacob Keller 	struct virtchnl_ether_addr hw_lan_addr;
101109aba47SJacob Keller 	struct ice_time_mac legacy_last_added_umac;
102109aba47SJacob Keller 	DECLARE_BITMAP(txq_ena, ICE_MAX_RSS_QS_PER_VF);
103109aba47SJacob Keller 	DECLARE_BITMAP(rxq_ena, ICE_MAX_RSS_QS_PER_VF);
104109aba47SJacob Keller 	struct ice_vlan port_vlan_info;	/* Port VLAN ID, QoS, and TPID */
105109aba47SJacob Keller 	struct virtchnl_vlan_caps vlan_v2_caps;
106109aba47SJacob Keller 	u8 pf_set_mac:1;		/* VF MAC address set by VMM admin */
107109aba47SJacob Keller 	u8 trusted:1;
108109aba47SJacob Keller 	u8 spoofchk:1;
109109aba47SJacob Keller 	u8 link_forced:1;
110109aba47SJacob Keller 	u8 link_up:1;			/* only valid if VF link is forced */
111109aba47SJacob Keller 	/* VSI indices - actual VSI pointers are maintained in the PF structure
112109aba47SJacob Keller 	 * When assigned, these will be non-zero, because VSI 0 is always
113109aba47SJacob Keller 	 * the main LAN VSI for the PF.
114109aba47SJacob Keller 	 */
115109aba47SJacob Keller 	u16 lan_vsi_num;		/* ID as used by firmware */
116109aba47SJacob Keller 	unsigned int min_tx_rate;	/* Minimum Tx bandwidth limit in Mbps */
117109aba47SJacob Keller 	unsigned int max_tx_rate;	/* Maximum Tx bandwidth limit in Mbps */
118109aba47SJacob Keller 	DECLARE_BITMAP(vf_states, ICE_VF_STATES_NBITS);	/* VF runtime states */
119109aba47SJacob Keller 
120109aba47SJacob Keller 	unsigned long vf_caps;		/* VF's adv. capabilities */
121109aba47SJacob Keller 	u8 num_req_qs;			/* num of queue pairs requested by VF */
122109aba47SJacob Keller 	u16 num_mac;
123109aba47SJacob Keller 	u16 num_vf_qs;			/* num of queue configured per VF */
124109aba47SJacob Keller 	struct ice_mdd_vf_events mdd_rx_events;
125109aba47SJacob Keller 	struct ice_mdd_vf_events mdd_tx_events;
126109aba47SJacob Keller 	DECLARE_BITMAP(opcodes_allowlist, VIRTCHNL_OP_MAX);
127109aba47SJacob Keller 
128109aba47SJacob Keller 	struct ice_repr *repr;
129109aba47SJacob Keller 	const struct ice_virtchnl_ops *virtchnl_ops;
1309c6f7878SJacob Keller 	const struct ice_vf_ops *vf_ops;
131109aba47SJacob Keller 
132109aba47SJacob Keller 	/* devlink port data */
133109aba47SJacob Keller 	struct devlink_port devlink_port;
134109aba47SJacob Keller };
135109aba47SJacob Keller 
1367eb517e4SJacob Keller /* Flags for controlling behavior of ice_reset_vf */
1377eb517e4SJacob Keller enum ice_vf_reset_flags {
1387eb517e4SJacob Keller 	ICE_VF_RESET_VFLR = BIT(0), /* Indicate a VFLR reset */
1399dbb33daSJacob Keller 	ICE_VF_RESET_NOTIFY = BIT(1), /* Notify VF prior to reset */
140f5f085c0SJacob Keller 	ICE_VF_RESET_LOCK = BIT(2), /* Acquire the VF cfg_lock */
1417eb517e4SJacob Keller };
1427eb517e4SJacob Keller 
143109aba47SJacob Keller static inline u16 ice_vf_get_port_vlan_id(struct ice_vf *vf)
144109aba47SJacob Keller {
145109aba47SJacob Keller 	return vf->port_vlan_info.vid;
146109aba47SJacob Keller }
147109aba47SJacob Keller 
148109aba47SJacob Keller static inline u8 ice_vf_get_port_vlan_prio(struct ice_vf *vf)
149109aba47SJacob Keller {
150109aba47SJacob Keller 	return vf->port_vlan_info.prio;
151109aba47SJacob Keller }
152109aba47SJacob Keller 
153109aba47SJacob Keller static inline bool ice_vf_is_port_vlan_ena(struct ice_vf *vf)
154109aba47SJacob Keller {
155109aba47SJacob Keller 	return (ice_vf_get_port_vlan_id(vf) || ice_vf_get_port_vlan_prio(vf));
156109aba47SJacob Keller }
157109aba47SJacob Keller 
158109aba47SJacob Keller static inline u16 ice_vf_get_port_vlan_tpid(struct ice_vf *vf)
159109aba47SJacob Keller {
160109aba47SJacob Keller 	return vf->port_vlan_info.tpid;
161109aba47SJacob Keller }
162109aba47SJacob Keller 
163109aba47SJacob Keller /* VF Hash Table access functions
164109aba47SJacob Keller  *
165109aba47SJacob Keller  * These functions provide abstraction for interacting with the VF hash table.
166109aba47SJacob Keller  * In general, direct access to the hash table should be avoided outside of
167109aba47SJacob Keller  * these functions where possible.
168109aba47SJacob Keller  *
169109aba47SJacob Keller  * The VF entries in the hash table are protected by reference counting to
170109aba47SJacob Keller  * track lifetime of accesses from the table. The ice_get_vf_by_id() function
171109aba47SJacob Keller  * obtains a reference to the VF structure which must be dropped by using
172109aba47SJacob Keller  * ice_put_vf().
173109aba47SJacob Keller  */
174109aba47SJacob Keller 
175109aba47SJacob Keller /**
176109aba47SJacob Keller  * ice_for_each_vf - Iterate over each VF entry
177109aba47SJacob Keller  * @pf: pointer to the PF private structure
178109aba47SJacob Keller  * @bkt: bucket index used for iteration
179*4eaf1797SJacob Keller  * @vf: pointer to the VF entry currently being processed in the loop
180109aba47SJacob Keller  *
181109aba47SJacob Keller  * The bkt variable is an unsigned integer iterator used to traverse the VF
182109aba47SJacob Keller  * entries. It is *not* guaranteed to be the VF's vf_id. Do not assume it is.
183109aba47SJacob Keller  * Use vf->vf_id to get the id number if needed.
184109aba47SJacob Keller  *
185109aba47SJacob Keller  * The caller is expected to be under the table_lock mutex for the entire
186109aba47SJacob Keller  * loop. Use this iterator if your loop is long or if it might sleep.
187109aba47SJacob Keller  */
188109aba47SJacob Keller #define ice_for_each_vf(pf, bkt, vf) \
189109aba47SJacob Keller 	hash_for_each((pf)->vfs.table, (bkt), (vf), entry)
190109aba47SJacob Keller 
191109aba47SJacob Keller /**
192109aba47SJacob Keller  * ice_for_each_vf_rcu - Iterate over each VF entry protected by RCU
193109aba47SJacob Keller  * @pf: pointer to the PF private structure
194109aba47SJacob Keller  * @bkt: bucket index used for iteration
195*4eaf1797SJacob Keller  * @vf: pointer to the VF entry currently being processed in the loop
196109aba47SJacob Keller  *
197109aba47SJacob Keller  * The bkt variable is an unsigned integer iterator used to traverse the VF
198109aba47SJacob Keller  * entries. It is *not* guaranteed to be the VF's vf_id. Do not assume it is.
199109aba47SJacob Keller  * Use vf->vf_id to get the id number if needed.
200109aba47SJacob Keller  *
201109aba47SJacob Keller  * The caller is expected to be under rcu_read_lock() for the entire loop.
202109aba47SJacob Keller  * Only use this iterator if your loop is short and you can guarantee it does
203109aba47SJacob Keller  * not sleep.
204109aba47SJacob Keller  */
205109aba47SJacob Keller #define ice_for_each_vf_rcu(pf, bkt, vf) \
206109aba47SJacob Keller 	hash_for_each_rcu((pf)->vfs.table, (bkt), (vf), entry)
207109aba47SJacob Keller 
208109aba47SJacob Keller #ifdef CONFIG_PCI_IOV
209109aba47SJacob Keller struct ice_vf *ice_get_vf_by_id(struct ice_pf *pf, u16 vf_id);
210109aba47SJacob Keller void ice_put_vf(struct ice_vf *vf);
211109aba47SJacob Keller bool ice_has_vfs(struct ice_pf *pf);
212109aba47SJacob Keller u16 ice_get_num_vfs(struct ice_pf *pf);
213109aba47SJacob Keller struct ice_vsi *ice_get_vf_vsi(struct ice_vf *vf);
214109aba47SJacob Keller bool ice_is_vf_disabled(struct ice_vf *vf);
215109aba47SJacob Keller int ice_check_vf_ready_for_cfg(struct ice_vf *vf);
216109aba47SJacob Keller void ice_set_vf_state_qs_dis(struct ice_vf *vf);
217109aba47SJacob Keller bool ice_is_any_vf_in_promisc(struct ice_pf *pf);
218109aba47SJacob Keller int
219109aba47SJacob Keller ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m);
220109aba47SJacob Keller int
221109aba47SJacob Keller ice_vf_clear_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m);
2227eb517e4SJacob Keller int ice_reset_vf(struct ice_vf *vf, u32 flags);
223fe99d1c0SJacob Keller void ice_reset_all_vfs(struct ice_pf *pf);
224109aba47SJacob Keller #else /* CONFIG_PCI_IOV */
225109aba47SJacob Keller static inline struct ice_vf *ice_get_vf_by_id(struct ice_pf *pf, u16 vf_id)
226109aba47SJacob Keller {
227109aba47SJacob Keller 	return NULL;
228109aba47SJacob Keller }
229109aba47SJacob Keller 
230109aba47SJacob Keller static inline void ice_put_vf(struct ice_vf *vf)
231109aba47SJacob Keller {
232109aba47SJacob Keller }
233109aba47SJacob Keller 
234109aba47SJacob Keller static inline bool ice_has_vfs(struct ice_pf *pf)
235109aba47SJacob Keller {
236109aba47SJacob Keller 	return false;
237109aba47SJacob Keller }
238109aba47SJacob Keller 
239109aba47SJacob Keller static inline u16 ice_get_num_vfs(struct ice_pf *pf)
240109aba47SJacob Keller {
241109aba47SJacob Keller 	return 0;
242109aba47SJacob Keller }
243109aba47SJacob Keller 
244109aba47SJacob Keller static inline struct ice_vsi *ice_get_vf_vsi(struct ice_vf *vf)
245109aba47SJacob Keller {
246109aba47SJacob Keller 	return NULL;
247109aba47SJacob Keller }
248109aba47SJacob Keller 
249109aba47SJacob Keller static inline bool ice_is_vf_disabled(struct ice_vf *vf)
250109aba47SJacob Keller {
251109aba47SJacob Keller 	return true;
252109aba47SJacob Keller }
253109aba47SJacob Keller 
254109aba47SJacob Keller static inline int ice_check_vf_ready_for_cfg(struct ice_vf *vf)
255109aba47SJacob Keller {
256109aba47SJacob Keller 	return -EOPNOTSUPP;
257109aba47SJacob Keller }
258109aba47SJacob Keller 
259109aba47SJacob Keller static inline void ice_set_vf_state_qs_dis(struct ice_vf *vf)
260109aba47SJacob Keller {
261109aba47SJacob Keller }
262109aba47SJacob Keller 
263109aba47SJacob Keller static inline bool ice_is_any_vf_in_promisc(struct ice_pf *pf)
264109aba47SJacob Keller {
265109aba47SJacob Keller 	return false;
266109aba47SJacob Keller }
267109aba47SJacob Keller 
268109aba47SJacob Keller static inline int
269109aba47SJacob Keller ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m)
270109aba47SJacob Keller {
271109aba47SJacob Keller 	return -EOPNOTSUPP;
272109aba47SJacob Keller }
273109aba47SJacob Keller 
274109aba47SJacob Keller static inline int
275109aba47SJacob Keller ice_vf_clear_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m)
276109aba47SJacob Keller {
277109aba47SJacob Keller 	return -EOPNOTSUPP;
278109aba47SJacob Keller }
27916686d7fSJacob Keller 
2807eb517e4SJacob Keller static inline int ice_reset_vf(struct ice_vf *vf, u32 flags)
28116686d7fSJacob Keller {
2824fe193ccSJacob Keller 	return 0;
28316686d7fSJacob Keller }
28416686d7fSJacob Keller 
285fe99d1c0SJacob Keller static inline void ice_reset_all_vfs(struct ice_pf *pf)
28616686d7fSJacob Keller {
28716686d7fSJacob Keller }
288109aba47SJacob Keller #endif /* !CONFIG_PCI_IOV */
289109aba47SJacob Keller 
290109aba47SJacob Keller #endif /* _ICE_VF_LIB_H_ */
291