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
23struct ice_pf;
24struct ice_vf;
25struct ice_virtchnl_ops;
26
27/* VF capabilities */
28enum ice_virtchnl_cap {
29	ICE_VIRTCHNL_VF_CAP_PRIVILEGE = 0,
30};
31
32/* Specific VF states */
33enum 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
43struct ice_time_mac {
44	unsigned long time_modified;
45	u8 addr[ETH_ALEN];
46};
47
48/* VF MDD events print structure */
49struct 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 */
56struct ice_vf_ops {
57	enum ice_disq_rst_src reset_type;
58	void (*free)(struct ice_vf *vf);
59	void (*clear_reset_state)(struct ice_vf *vf);
60	void (*clear_mbx_register)(struct ice_vf *vf);
61	void (*trigger_reset_register)(struct ice_vf *vf, bool is_vflr);
62	bool (*poll_reset_status)(struct ice_vf *vf);
63	void (*clear_reset_trigger)(struct ice_vf *vf);
64	void (*irq_close)(struct ice_vf *vf);
65	void (*post_vsi_rebuild)(struct ice_vf *vf);
66};
67
68/* Virtchnl/SR-IOV config info */
69struct ice_vfs {
70	DECLARE_HASHTABLE(table, 8);	/* table of VF entries */
71	struct mutex table_lock;	/* Lock for protecting the hash table */
72	u16 num_supported;		/* max supported VFs on this PF */
73	u16 num_qps_per;		/* number of queue pairs per VF */
74	u16 num_msix_per;		/* default MSI-X vectors per VF */
75	unsigned long last_printed_mdd_jiffies;	/* MDD message rate limit */
76};
77
78/* VF information structure */
79struct ice_vf {
80	struct hlist_node entry;
81	struct rcu_head rcu;
82	struct kref refcnt;
83	struct ice_pf *pf;
84	struct pci_dev *vfdev;
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	u8 dev_lan_addr[ETH_ALEN];
100	u8 hw_lan_addr[ETH_ALEN];
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	struct ice_mbx_vf_info mbx_info;
107	u8 pf_set_mac:1;		/* VF MAC address set by VMM admin */
108	u8 trusted:1;
109	u8 spoofchk:1;
110	u8 link_forced:1;
111	u8 link_up:1;			/* only valid if VF link is forced */
112	unsigned int min_tx_rate;	/* Minimum Tx bandwidth limit in Mbps */
113	unsigned int max_tx_rate;	/* Maximum Tx bandwidth limit in Mbps */
114	DECLARE_BITMAP(vf_states, ICE_VF_STATES_NBITS);	/* VF runtime states */
115
116	unsigned long vf_caps;		/* VF's adv. capabilities */
117	u8 num_req_qs;			/* num of queue pairs requested by VF */
118	u16 num_mac;
119	u16 num_vf_qs;			/* num of queue configured per VF */
120	u8 vlan_strip_ena;		/* Outer and Inner VLAN strip enable */
121#define ICE_INNER_VLAN_STRIP_ENA	BIT(0)
122#define ICE_OUTER_VLAN_STRIP_ENA	BIT(1)
123	struct ice_mdd_vf_events mdd_rx_events;
124	struct ice_mdd_vf_events mdd_tx_events;
125	DECLARE_BITMAP(opcodes_allowlist, VIRTCHNL_OP_MAX);
126
127	unsigned long repr_id;
128	const struct ice_virtchnl_ops *virtchnl_ops;
129	const struct ice_vf_ops *vf_ops;
130
131	/* devlink port data */
132	struct devlink_port devlink_port;
133
134	u16 num_msix;			/* num of MSI-X configured on this VF */
135};
136
137/* Flags for controlling behavior of ice_reset_vf */
138enum ice_vf_reset_flags {
139	ICE_VF_RESET_VFLR = BIT(0), /* Indicate a VFLR reset */
140	ICE_VF_RESET_NOTIFY = BIT(1), /* Notify VF prior to reset */
141	ICE_VF_RESET_LOCK = BIT(2), /* Acquire the VF cfg_lock */
142};
143
144static inline u16 ice_vf_get_port_vlan_id(struct ice_vf *vf)
145{
146	return vf->port_vlan_info.vid;
147}
148
149static inline u8 ice_vf_get_port_vlan_prio(struct ice_vf *vf)
150{
151	return vf->port_vlan_info.prio;
152}
153
154static inline bool ice_vf_is_port_vlan_ena(struct ice_vf *vf)
155{
156	return (ice_vf_get_port_vlan_id(vf) || ice_vf_get_port_vlan_prio(vf));
157}
158
159static inline u16 ice_vf_get_port_vlan_tpid(struct ice_vf *vf)
160{
161	return vf->port_vlan_info.tpid;
162}
163
164/* VF Hash Table access functions
165 *
166 * These functions provide abstraction for interacting with the VF hash table.
167 * In general, direct access to the hash table should be avoided outside of
168 * these functions where possible.
169 *
170 * The VF entries in the hash table are protected by reference counting to
171 * track lifetime of accesses from the table. The ice_get_vf_by_id() function
172 * obtains a reference to the VF structure which must be dropped by using
173 * ice_put_vf().
174 */
175
176/**
177 * ice_for_each_vf - Iterate over each VF entry
178 * @pf: pointer to the PF private structure
179 * @bkt: bucket index used for iteration
180 * @vf: pointer to the VF entry currently being processed in the loop
181 *
182 * The bkt variable is an unsigned integer iterator used to traverse the VF
183 * entries. It is *not* guaranteed to be the VF's vf_id. Do not assume it is.
184 * Use vf->vf_id to get the id number if needed.
185 *
186 * The caller is expected to be under the table_lock mutex for the entire
187 * loop. Use this iterator if your loop is long or if it might sleep.
188 */
189#define ice_for_each_vf(pf, bkt, vf) \
190	hash_for_each((pf)->vfs.table, (bkt), (vf), entry)
191
192/**
193 * ice_for_each_vf_rcu - Iterate over each VF entry protected by RCU
194 * @pf: pointer to the PF private structure
195 * @bkt: bucket index used for iteration
196 * @vf: pointer to the VF entry currently being processed in the loop
197 *
198 * The bkt variable is an unsigned integer iterator used to traverse the VF
199 * entries. It is *not* guaranteed to be the VF's vf_id. Do not assume it is.
200 * Use vf->vf_id to get the id number if needed.
201 *
202 * The caller is expected to be under rcu_read_lock() for the entire loop.
203 * Only use this iterator if your loop is short and you can guarantee it does
204 * not sleep.
205 */
206#define ice_for_each_vf_rcu(pf, bkt, vf) \
207	hash_for_each_rcu((pf)->vfs.table, (bkt), (vf), entry)
208
209#ifdef CONFIG_PCI_IOV
210struct ice_vf *ice_get_vf_by_id(struct ice_pf *pf, u16 vf_id);
211void ice_put_vf(struct ice_vf *vf);
212bool ice_has_vfs(struct ice_pf *pf);
213u16 ice_get_num_vfs(struct ice_pf *pf);
214struct ice_vsi *ice_get_vf_vsi(struct ice_vf *vf);
215bool ice_is_vf_disabled(struct ice_vf *vf);
216int ice_check_vf_ready_for_cfg(struct ice_vf *vf);
217void ice_set_vf_state_dis(struct ice_vf *vf);
218bool ice_is_any_vf_in_unicast_promisc(struct ice_pf *pf);
219void
220ice_vf_get_promisc_masks(struct ice_vf *vf, struct ice_vsi *vsi,
221			 u8 *ucast_m, u8 *mcast_m);
222int
223ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m);
224int
225ice_vf_clear_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m);
226int ice_reset_vf(struct ice_vf *vf, u32 flags);
227void ice_reset_all_vfs(struct ice_pf *pf);
228struct ice_vsi *ice_get_vf_ctrl_vsi(struct ice_pf *pf, struct ice_vsi *vsi);
229#else /* CONFIG_PCI_IOV */
230static inline struct ice_vf *ice_get_vf_by_id(struct ice_pf *pf, u16 vf_id)
231{
232	return NULL;
233}
234
235static inline void ice_put_vf(struct ice_vf *vf)
236{
237}
238
239static inline bool ice_has_vfs(struct ice_pf *pf)
240{
241	return false;
242}
243
244static inline u16 ice_get_num_vfs(struct ice_pf *pf)
245{
246	return 0;
247}
248
249static inline struct ice_vsi *ice_get_vf_vsi(struct ice_vf *vf)
250{
251	return NULL;
252}
253
254static inline bool ice_is_vf_disabled(struct ice_vf *vf)
255{
256	return true;
257}
258
259static inline int ice_check_vf_ready_for_cfg(struct ice_vf *vf)
260{
261	return -EOPNOTSUPP;
262}
263
264static inline void ice_set_vf_state_dis(struct ice_vf *vf)
265{
266}
267
268static inline bool ice_is_any_vf_in_unicast_promisc(struct ice_pf *pf)
269{
270	return false;
271}
272
273static inline int
274ice_vf_set_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m)
275{
276	return -EOPNOTSUPP;
277}
278
279static inline int
280ice_vf_clear_vsi_promisc(struct ice_vf *vf, struct ice_vsi *vsi, u8 promisc_m)
281{
282	return -EOPNOTSUPP;
283}
284
285static inline int ice_reset_vf(struct ice_vf *vf, u32 flags)
286{
287	return 0;
288}
289
290static inline void ice_reset_all_vfs(struct ice_pf *pf)
291{
292}
293
294static inline struct ice_vsi *
295ice_get_vf_ctrl_vsi(struct ice_pf *pf, struct ice_vsi *vsi)
296{
297	return NULL;
298}
299#endif /* !CONFIG_PCI_IOV */
300
301#endif /* _ICE_VF_LIB_H_ */
302