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);
59fa4a15c8SJacob Keller 	void (*clear_reset_state)(struct ice_vf *vf);
609c6f7878SJacob Keller 	void (*clear_mbx_register)(struct ice_vf *vf);
619c6f7878SJacob Keller 	void (*trigger_reset_register)(struct ice_vf *vf, bool is_vflr);
629c6f7878SJacob Keller 	bool (*poll_reset_status)(struct ice_vf *vf);
639c6f7878SJacob Keller 	void (*clear_reset_trigger)(struct ice_vf *vf);
64537dfe06SJacob Keller 	void (*irq_close)(struct ice_vf *vf);
655531bb85SJacob Keller 	int (*create_vsi)(struct ice_vf *vf);
669c6f7878SJacob Keller 	void (*post_vsi_rebuild)(struct ice_vf *vf);
679c6f7878SJacob Keller };
689c6f7878SJacob Keller 
69109aba47SJacob Keller /* Virtchnl/SR-IOV config info */
70109aba47SJacob Keller struct ice_vfs {
71109aba47SJacob Keller 	DECLARE_HASHTABLE(table, 8);	/* table of VF entries */
72109aba47SJacob Keller 	struct mutex table_lock;	/* Lock for protecting the hash table */
73109aba47SJacob Keller 	u16 num_supported;		/* max supported VFs on this PF */
74109aba47SJacob Keller 	u16 num_qps_per;		/* number of queue pairs per VF */
75109aba47SJacob Keller 	u16 num_msix_per;		/* number of MSI-X vectors per VF */
76109aba47SJacob Keller 	unsigned long last_printed_mdd_jiffies;	/* MDD message rate limit */
77109aba47SJacob Keller };
78109aba47SJacob Keller 
79109aba47SJacob Keller /* VF information structure */
80109aba47SJacob Keller struct ice_vf {
81109aba47SJacob Keller 	struct hlist_node entry;
82109aba47SJacob Keller 	struct rcu_head rcu;
83109aba47SJacob Keller 	struct kref refcnt;
84109aba47SJacob Keller 	struct ice_pf *pf;
85109aba47SJacob Keller 
86109aba47SJacob Keller 	/* Used during virtchnl message handling and NDO ops against the VF
87109aba47SJacob Keller 	 * that will trigger a VFR
88109aba47SJacob Keller 	 */
89109aba47SJacob Keller 	struct mutex cfg_lock;
90109aba47SJacob Keller 
91109aba47SJacob Keller 	u16 vf_id;			/* VF ID in the PF space */
92109aba47SJacob Keller 	u16 lan_vsi_idx;		/* index into PF struct */
93109aba47SJacob Keller 	u16 ctrl_vsi_idx;
94109aba47SJacob Keller 	struct ice_vf_fdir fdir;
95109aba47SJacob Keller 	/* first vector index of this VF in the PF space */
96109aba47SJacob Keller 	int first_vector_idx;
97109aba47SJacob Keller 	struct ice_sw *vf_sw_id;	/* switch ID the VF VSIs connect to */
98109aba47SJacob Keller 	struct virtchnl_version_info vf_ver;
99109aba47SJacob Keller 	u32 driver_caps;		/* reported by VF driver */
100e0645311SJacob Keller 	u8 dev_lan_addr[ETH_ALEN];
101e0645311SJacob Keller 	u8 hw_lan_addr[ETH_ALEN];
102109aba47SJacob Keller 	struct ice_time_mac legacy_last_added_umac;
103109aba47SJacob Keller 	DECLARE_BITMAP(txq_ena, ICE_MAX_RSS_QS_PER_VF);
104109aba47SJacob Keller 	DECLARE_BITMAP(rxq_ena, ICE_MAX_RSS_QS_PER_VF);
105109aba47SJacob Keller 	struct ice_vlan port_vlan_info;	/* Port VLAN ID, QoS, and TPID */
106109aba47SJacob Keller 	struct virtchnl_vlan_caps vlan_v2_caps;
107e4eaf893SJacob Keller 	struct ice_mbx_vf_info mbx_info;
108109aba47SJacob Keller 	u8 pf_set_mac:1;		/* VF MAC address set by VMM admin */
109109aba47SJacob Keller 	u8 trusted:1;
110109aba47SJacob Keller 	u8 spoofchk:1;
111109aba47SJacob Keller 	u8 link_forced:1;
112109aba47SJacob Keller 	u8 link_up:1;			/* only valid if VF link is forced */
113109aba47SJacob Keller 	/* VSI indices - actual VSI pointers are maintained in the PF structure
114109aba47SJacob Keller 	 * When assigned, these will be non-zero, because VSI 0 is always
115109aba47SJacob Keller 	 * the main LAN VSI for the PF.
116109aba47SJacob Keller 	 */
117109aba47SJacob Keller 	u16 lan_vsi_num;		/* ID as used by firmware */
118109aba47SJacob Keller 	unsigned int min_tx_rate;	/* Minimum Tx bandwidth limit in Mbps */
119109aba47SJacob Keller 	unsigned int max_tx_rate;	/* Maximum Tx bandwidth limit in Mbps */
120109aba47SJacob Keller 	DECLARE_BITMAP(vf_states, ICE_VF_STATES_NBITS);	/* VF runtime states */
121109aba47SJacob Keller 
122109aba47SJacob Keller 	unsigned long vf_caps;		/* VF's adv. capabilities */
123109aba47SJacob Keller 	u8 num_req_qs;			/* num of queue pairs requested by VF */
124109aba47SJacob Keller 	u16 num_mac;
125109aba47SJacob Keller 	u16 num_vf_qs;			/* num of queue configured per VF */
126109aba47SJacob Keller 	struct ice_mdd_vf_events mdd_rx_events;
127109aba47SJacob Keller 	struct ice_mdd_vf_events mdd_tx_events;
128109aba47SJacob Keller 	DECLARE_BITMAP(opcodes_allowlist, VIRTCHNL_OP_MAX);
129109aba47SJacob Keller 
130109aba47SJacob Keller 	struct ice_repr *repr;
131109aba47SJacob Keller 	const struct ice_virtchnl_ops *virtchnl_ops;
1329c6f7878SJacob Keller 	const struct ice_vf_ops *vf_ops;
133109aba47SJacob Keller 
134109aba47SJacob Keller 	/* devlink port data */
135109aba47SJacob Keller 	struct devlink_port devlink_port;
136109aba47SJacob Keller };
137109aba47SJacob Keller 
1387eb517e4SJacob Keller /* Flags for controlling behavior of ice_reset_vf */
1397eb517e4SJacob Keller enum ice_vf_reset_flags {
1407eb517e4SJacob Keller 	ICE_VF_RESET_VFLR = BIT(0), /* Indicate a VFLR reset */
1419dbb33daSJacob Keller 	ICE_VF_RESET_NOTIFY = BIT(1), /* Notify VF prior to reset */
142f5f085c0SJacob Keller 	ICE_VF_RESET_LOCK = BIT(2), /* Acquire the VF cfg_lock */
1437eb517e4SJacob Keller };
1447eb517e4SJacob Keller 
ice_vf_get_port_vlan_id(struct ice_vf * vf)145109aba47SJacob Keller static inline u16 ice_vf_get_port_vlan_id(struct ice_vf *vf)
146109aba47SJacob Keller {
147109aba47SJacob Keller 	return vf->port_vlan_info.vid;
148109aba47SJacob Keller }
149109aba47SJacob Keller 
ice_vf_get_port_vlan_prio(struct ice_vf * vf)150109aba47SJacob Keller static inline u8 ice_vf_get_port_vlan_prio(struct ice_vf *vf)
151109aba47SJacob Keller {
152109aba47SJacob Keller 	return vf->port_vlan_info.prio;
153109aba47SJacob Keller }
154109aba47SJacob Keller 
ice_vf_is_port_vlan_ena(struct ice_vf * vf)155109aba47SJacob Keller static inline bool ice_vf_is_port_vlan_ena(struct ice_vf *vf)
156109aba47SJacob Keller {
157109aba47SJacob Keller 	return (ice_vf_get_port_vlan_id(vf) || ice_vf_get_port_vlan_prio(vf));
158109aba47SJacob Keller }
159109aba47SJacob Keller 
ice_vf_get_port_vlan_tpid(struct ice_vf * vf)160109aba47SJacob Keller static inline u16 ice_vf_get_port_vlan_tpid(struct ice_vf *vf)
161109aba47SJacob Keller {
162109aba47SJacob Keller 	return vf->port_vlan_info.tpid;
163109aba47SJacob Keller }
164109aba47SJacob Keller 
165109aba47SJacob Keller /* VF Hash Table access functions
166109aba47SJacob Keller  *
167109aba47SJacob Keller  * These functions provide abstraction for interacting with the VF hash table.
168109aba47SJacob Keller  * In general, direct access to the hash table should be avoided outside of
169109aba47SJacob Keller  * these functions where possible.
170109aba47SJacob Keller  *
171109aba47SJacob Keller  * The VF entries in the hash table are protected by reference counting to
172109aba47SJacob Keller  * track lifetime of accesses from the table. The ice_get_vf_by_id() function
173109aba47SJacob Keller  * obtains a reference to the VF structure which must be dropped by using
174109aba47SJacob Keller  * ice_put_vf().
175109aba47SJacob Keller  */
176109aba47SJacob Keller 
177109aba47SJacob Keller /**
178109aba47SJacob Keller  * ice_for_each_vf - Iterate over each VF entry
179109aba47SJacob Keller  * @pf: pointer to the PF private structure
180109aba47SJacob Keller  * @bkt: bucket index used for iteration
1814eaf1797SJacob Keller  * @vf: pointer to the VF entry currently being processed in the loop
182109aba47SJacob Keller  *
183109aba47SJacob Keller  * The bkt variable is an unsigned integer iterator used to traverse the VF
184109aba47SJacob Keller  * entries. It is *not* guaranteed to be the VF's vf_id. Do not assume it is.
185109aba47SJacob Keller  * Use vf->vf_id to get the id number if needed.
186109aba47SJacob Keller  *
187109aba47SJacob Keller  * The caller is expected to be under the table_lock mutex for the entire
188109aba47SJacob Keller  * loop. Use this iterator if your loop is long or if it might sleep.
189109aba47SJacob Keller  */
190109aba47SJacob Keller #define ice_for_each_vf(pf, bkt, vf) \
191109aba47SJacob Keller 	hash_for_each((pf)->vfs.table, (bkt), (vf), entry)
192109aba47SJacob Keller 
193109aba47SJacob Keller /**
194109aba47SJacob Keller  * ice_for_each_vf_rcu - Iterate over each VF entry protected by RCU
195109aba47SJacob Keller  * @pf: pointer to the PF private structure
196109aba47SJacob Keller  * @bkt: bucket index used for iteration
1974eaf1797SJacob Keller  * @vf: pointer to the VF entry currently being processed in the loop
198109aba47SJacob Keller  *
199109aba47SJacob Keller  * The bkt variable is an unsigned integer iterator used to traverse the VF
200109aba47SJacob Keller  * entries. It is *not* guaranteed to be the VF's vf_id. Do not assume it is.
201109aba47SJacob Keller  * Use vf->vf_id to get the id number if needed.
202109aba47SJacob Keller  *
203109aba47SJacob Keller  * The caller is expected to be under rcu_read_lock() for the entire loop.
204109aba47SJacob Keller  * Only use this iterator if your loop is short and you can guarantee it does
205109aba47SJacob Keller  * not sleep.
206109aba47SJacob Keller  */
207109aba47SJacob Keller #define ice_for_each_vf_rcu(pf, bkt, vf) \
208109aba47SJacob Keller 	hash_for_each_rcu((pf)->vfs.table, (bkt), (vf), entry)
209109aba47SJacob Keller 
210109aba47SJacob Keller #ifdef CONFIG_PCI_IOV
211109aba47SJacob Keller struct ice_vf *ice_get_vf_by_id(struct ice_pf *pf, u16 vf_id);
212109aba47SJacob Keller void ice_put_vf(struct ice_vf *vf);
213109aba47SJacob Keller bool ice_has_vfs(struct ice_pf *pf);
214109aba47SJacob Keller u16 ice_get_num_vfs(struct ice_pf *pf);
215109aba47SJacob Keller struct ice_vsi *ice_get_vf_vsi(struct ice_vf *vf);
216109aba47SJacob Keller bool ice_is_vf_disabled(struct ice_vf *vf);
217109aba47SJacob Keller int ice_check_vf_ready_for_cfg(struct ice_vf *vf);
218fa4a15c8SJacob Keller void ice_set_vf_state_dis(struct ice_vf *vf);
219d7393425SMichal Wilczynski bool ice_is_any_vf_in_unicast_promisc(struct ice_pf *pf);
220a419526dSMichal Wilczynski void
221a419526dSMichal Wilczynski ice_vf_get_promisc_masks(struct ice_vf *vf, struct ice_vsi *vsi,
222a419526dSMichal Wilczynski 			 u8 *ucast_m, u8 *mcast_m);
223109aba47SJacob Keller int
224109aba47SJacob Keller ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m);
225109aba47SJacob Keller int
226109aba47SJacob Keller ice_vf_clear_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m);
2277eb517e4SJacob Keller int ice_reset_vf(struct ice_vf *vf, u32 flags);
228fe99d1c0SJacob Keller void ice_reset_all_vfs(struct ice_pf *pf);
229*369bb5a2SPiotr Raczynski struct ice_vsi *ice_get_vf_ctrl_vsi(struct ice_pf *pf, struct ice_vsi *vsi);
230109aba47SJacob Keller #else /* CONFIG_PCI_IOV */
ice_get_vf_by_id(struct ice_pf * pf,u16 vf_id)231109aba47SJacob Keller static inline struct ice_vf *ice_get_vf_by_id(struct ice_pf *pf, u16 vf_id)
232109aba47SJacob Keller {
233109aba47SJacob Keller 	return NULL;
234109aba47SJacob Keller }
235109aba47SJacob Keller 
ice_put_vf(struct ice_vf * vf)236109aba47SJacob Keller static inline void ice_put_vf(struct ice_vf *vf)
237109aba47SJacob Keller {
238109aba47SJacob Keller }
239109aba47SJacob Keller 
ice_has_vfs(struct ice_pf * pf)240109aba47SJacob Keller static inline bool ice_has_vfs(struct ice_pf *pf)
241109aba47SJacob Keller {
242109aba47SJacob Keller 	return false;
243109aba47SJacob Keller }
244109aba47SJacob Keller 
ice_get_num_vfs(struct ice_pf * pf)245109aba47SJacob Keller static inline u16 ice_get_num_vfs(struct ice_pf *pf)
246109aba47SJacob Keller {
247109aba47SJacob Keller 	return 0;
248109aba47SJacob Keller }
249109aba47SJacob Keller 
ice_get_vf_vsi(struct ice_vf * vf)250109aba47SJacob Keller static inline struct ice_vsi *ice_get_vf_vsi(struct ice_vf *vf)
251109aba47SJacob Keller {
252109aba47SJacob Keller 	return NULL;
253109aba47SJacob Keller }
254109aba47SJacob Keller 
ice_is_vf_disabled(struct ice_vf * vf)255109aba47SJacob Keller static inline bool ice_is_vf_disabled(struct ice_vf *vf)
256109aba47SJacob Keller {
257109aba47SJacob Keller 	return true;
258109aba47SJacob Keller }
259109aba47SJacob Keller 
ice_check_vf_ready_for_cfg(struct ice_vf * vf)260109aba47SJacob Keller static inline int ice_check_vf_ready_for_cfg(struct ice_vf *vf)
261109aba47SJacob Keller {
262109aba47SJacob Keller 	return -EOPNOTSUPP;
263109aba47SJacob Keller }
264109aba47SJacob Keller 
ice_set_vf_state_dis(struct ice_vf * vf)265fa4a15c8SJacob Keller static inline void ice_set_vf_state_dis(struct ice_vf *vf)
266109aba47SJacob Keller {
267109aba47SJacob Keller }
268109aba47SJacob Keller 
ice_is_any_vf_in_unicast_promisc(struct ice_pf * pf)269d7393425SMichal Wilczynski static inline bool ice_is_any_vf_in_unicast_promisc(struct ice_pf *pf)
270109aba47SJacob Keller {
271109aba47SJacob Keller 	return false;
272109aba47SJacob Keller }
273109aba47SJacob Keller 
274109aba47SJacob Keller static inline int
ice_vf_set_vsi_promisc(struct ice_vf * vf,struct ice_vsi * vsi,u8 promisc_m)275109aba47SJacob Keller ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m)
276109aba47SJacob Keller {
277109aba47SJacob Keller 	return -EOPNOTSUPP;
278109aba47SJacob Keller }
279109aba47SJacob Keller 
280109aba47SJacob Keller static inline int
ice_vf_clear_vsi_promisc(struct ice_vf * vf,struct ice_vsi * vsi,u8 promisc_m)281109aba47SJacob Keller ice_vf_clear_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m)
282109aba47SJacob Keller {
283109aba47SJacob Keller 	return -EOPNOTSUPP;
284109aba47SJacob Keller }
28516686d7fSJacob Keller 
ice_reset_vf(struct ice_vf * vf,u32 flags)2867eb517e4SJacob Keller static inline int ice_reset_vf(struct ice_vf *vf, u32 flags)
28716686d7fSJacob Keller {
2884fe193ccSJacob Keller 	return 0;
28916686d7fSJacob Keller }
29016686d7fSJacob Keller 
ice_reset_all_vfs(struct ice_pf * pf)291fe99d1c0SJacob Keller static inline void ice_reset_all_vfs(struct ice_pf *pf)
29216686d7fSJacob Keller {
29316686d7fSJacob Keller }
294*369bb5a2SPiotr Raczynski 
295*369bb5a2SPiotr Raczynski static inline struct ice_vsi *
ice_get_vf_ctrl_vsi(struct ice_pf * pf,struct ice_vsi * vsi)296*369bb5a2SPiotr Raczynski ice_get_vf_ctrl_vsi(struct ice_pf *pf, struct ice_vsi *vsi)
297*369bb5a2SPiotr Raczynski {
298*369bb5a2SPiotr Raczynski 	return NULL;
299*369bb5a2SPiotr Raczynski }
300109aba47SJacob Keller #endif /* !CONFIG_PCI_IOV */
301109aba47SJacob Keller 
302109aba47SJacob Keller #endif /* _ICE_VF_LIB_H_ */
303