1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2018, Intel Corporation. */
3 
4 #ifndef _ICE_VIRTCHNL_PF_H_
5 #define _ICE_VIRTCHNL_PF_H_
6 #include "ice.h"
7 #include "ice_virtchnl_fdir.h"
8 
9 /* Restrict number of MAC Addr and VLAN that non-trusted VF can programmed */
10 #define ICE_MAX_VLAN_PER_VF		8
11 /* MAC filters: 1 is reserved for the VF's default/perm_addr/LAA MAC, 1 for
12  * broadcast, and 16 for additional unicast/multicast filters
13  */
14 #define ICE_MAX_MACADDR_PER_VF		18
15 
16 /* Malicious Driver Detection */
17 #define ICE_DFLT_NUM_INVAL_MSGS_ALLOWED		10
18 #define ICE_MDD_EVENTS_THRESHOLD		30
19 
20 /* Static VF transaction/status register def */
21 #define VF_DEVICE_STATUS		0xAA
22 #define VF_TRANS_PENDING_M		0x20
23 
24 /* wait defines for polling PF_PCI_CIAD register status */
25 #define ICE_PCI_CIAD_WAIT_COUNT		100
26 #define ICE_PCI_CIAD_WAIT_DELAY_US	1
27 
28 /* VF resource constraints */
29 #define ICE_MAX_VF_COUNT		256
30 #define ICE_MIN_QS_PER_VF		1
31 #define ICE_NONQ_VECS_VF		1
32 #define ICE_MAX_SCATTER_QS_PER_VF	16
33 #define ICE_MAX_RSS_QS_PER_VF		16
34 #define ICE_NUM_VF_MSIX_MED		17
35 #define ICE_NUM_VF_MSIX_SMALL		5
36 #define ICE_NUM_VF_MSIX_MULTIQ_MIN	3
37 #define ICE_MIN_INTR_PER_VF		(ICE_MIN_QS_PER_VF + 1)
38 #define ICE_MAX_VF_RESET_TRIES		40
39 #define ICE_MAX_VF_RESET_SLEEP_MS	20
40 
41 #define ice_for_each_vf(pf, i) \
42 	for ((i) = 0; (i) < (pf)->num_alloc_vfs; (i)++)
43 
44 /* Specific VF states */
45 enum ice_vf_states {
46 	ICE_VF_STATE_INIT = 0,		/* PF is initializing VF */
47 	ICE_VF_STATE_ACTIVE,		/* VF resources are allocated for use */
48 	ICE_VF_STATE_QS_ENA,		/* VF queue(s) enabled */
49 	ICE_VF_STATE_DIS,
50 	ICE_VF_STATE_MC_PROMISC,
51 	ICE_VF_STATE_UC_PROMISC,
52 	ICE_VF_STATES_NBITS
53 };
54 
55 /* VF capabilities */
56 enum ice_virtchnl_cap {
57 	ICE_VIRTCHNL_VF_CAP_L2 = 0,
58 	ICE_VIRTCHNL_VF_CAP_PRIVILEGE,
59 };
60 
61 struct ice_time_mac {
62 	unsigned long time_modified;
63 	u8 addr[ETH_ALEN];
64 };
65 
66 /* VF MDD events print structure */
67 struct ice_mdd_vf_events {
68 	u16 count;			/* total count of Rx|Tx events */
69 	/* count number of the last printed event */
70 	u16 last_printed;
71 };
72 
73 struct ice_vf;
74 
75 struct ice_vc_vf_ops {
76 	int (*get_ver_msg)(struct ice_vf *vf, u8 *msg);
77 	int (*get_vf_res_msg)(struct ice_vf *vf, u8 *msg);
78 	void (*reset_vf)(struct ice_vf *vf);
79 	int (*add_mac_addr_msg)(struct ice_vf *vf, u8 *msg);
80 	int (*del_mac_addr_msg)(struct ice_vf *vf, u8 *msg);
81 	int (*cfg_qs_msg)(struct ice_vf *vf, u8 *msg);
82 	int (*ena_qs_msg)(struct ice_vf *vf, u8 *msg);
83 	int (*dis_qs_msg)(struct ice_vf *vf, u8 *msg);
84 	int (*request_qs_msg)(struct ice_vf *vf, u8 *msg);
85 	int (*cfg_irq_map_msg)(struct ice_vf *vf, u8 *msg);
86 	int (*config_rss_key)(struct ice_vf *vf, u8 *msg);
87 	int (*config_rss_lut)(struct ice_vf *vf, u8 *msg);
88 	int (*get_stats_msg)(struct ice_vf *vf, u8 *msg);
89 	int (*cfg_promiscuous_mode_msg)(struct ice_vf *vf, u8 *msg);
90 	int (*add_vlan_msg)(struct ice_vf *vf, u8 *msg);
91 	int (*remove_vlan_msg)(struct ice_vf *vf, u8 *msg);
92 	int (*ena_vlan_stripping)(struct ice_vf *vf);
93 	int (*dis_vlan_stripping)(struct ice_vf *vf);
94 	int (*handle_rss_cfg_msg)(struct ice_vf *vf, u8 *msg, bool add);
95 	int (*add_fdir_fltr_msg)(struct ice_vf *vf, u8 *msg);
96 	int (*del_fdir_fltr_msg)(struct ice_vf *vf, u8 *msg);
97 };
98 
99 /* VF information structure */
100 struct ice_vf {
101 	struct ice_pf *pf;
102 
103 	/* Used during virtchnl message handling and NDO ops against the VF
104 	 * that will trigger a VFR
105 	 */
106 	struct mutex cfg_lock;
107 
108 	u16 vf_id;			/* VF ID in the PF space */
109 	u16 lan_vsi_idx;		/* index into PF struct */
110 	u16 ctrl_vsi_idx;
111 	struct ice_vf_fdir fdir;
112 	/* first vector index of this VF in the PF space */
113 	int first_vector_idx;
114 	struct ice_sw *vf_sw_id;	/* switch ID the VF VSIs connect to */
115 	struct virtchnl_version_info vf_ver;
116 	u32 driver_caps;		/* reported by VF driver */
117 	struct virtchnl_ether_addr dev_lan_addr;
118 	struct virtchnl_ether_addr hw_lan_addr;
119 	struct ice_time_mac legacy_last_added_umac;
120 	DECLARE_BITMAP(txq_ena, ICE_MAX_RSS_QS_PER_VF);
121 	DECLARE_BITMAP(rxq_ena, ICE_MAX_RSS_QS_PER_VF);
122 	u16 port_vlan_info;		/* Port VLAN ID and QoS */
123 	u8 pf_set_mac:1;		/* VF MAC address set by VMM admin */
124 	u8 trusted:1;
125 	u8 spoofchk:1;
126 	u8 link_forced:1;
127 	u8 link_up:1;			/* only valid if VF link is forced */
128 	/* VSI indices - actual VSI pointers are maintained in the PF structure
129 	 * When assigned, these will be non-zero, because VSI 0 is always
130 	 * the main LAN VSI for the PF.
131 	 */
132 	u16 lan_vsi_num;		/* ID as used by firmware */
133 	unsigned int min_tx_rate;	/* Minimum Tx bandwidth limit in Mbps */
134 	unsigned int max_tx_rate;	/* Maximum Tx bandwidth limit in Mbps */
135 	DECLARE_BITMAP(vf_states, ICE_VF_STATES_NBITS);	/* VF runtime states */
136 
137 	u64 num_inval_msgs;		/* number of continuous invalid msgs */
138 	u64 num_valid_msgs;		/* number of valid msgs detected */
139 	unsigned long vf_caps;		/* VF's adv. capabilities */
140 	u8 num_req_qs;			/* num of queue pairs requested by VF */
141 	u16 num_mac;
142 	u16 num_vf_qs;			/* num of queue configured per VF */
143 	struct ice_mdd_vf_events mdd_rx_events;
144 	struct ice_mdd_vf_events mdd_tx_events;
145 	DECLARE_BITMAP(opcodes_allowlist, VIRTCHNL_OP_MAX);
146 
147 	struct ice_repr *repr;
148 
149 	struct ice_vc_vf_ops vc_ops;
150 
151 	/* devlink port data */
152 	struct devlink_port devlink_port;
153 };
154 
155 #ifdef CONFIG_PCI_IOV
156 struct ice_vsi *ice_get_vf_vsi(struct ice_vf *vf);
157 void ice_process_vflr_event(struct ice_pf *pf);
158 int ice_sriov_configure(struct pci_dev *pdev, int num_vfs);
159 int ice_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac);
160 int
161 ice_get_vf_cfg(struct net_device *netdev, int vf_id, struct ifla_vf_info *ivi);
162 
163 void ice_free_vfs(struct ice_pf *pf);
164 void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event);
165 void ice_vc_notify_link_state(struct ice_pf *pf);
166 void ice_vc_notify_reset(struct ice_pf *pf);
167 void ice_vc_notify_vf_link_state(struct ice_vf *vf);
168 void ice_vc_change_ops_to_repr(struct ice_vc_vf_ops *ops);
169 void ice_vc_set_dflt_vf_ops(struct ice_vc_vf_ops *ops);
170 bool ice_reset_all_vfs(struct ice_pf *pf, bool is_vflr);
171 bool ice_reset_vf(struct ice_vf *vf, bool is_vflr);
172 void ice_restore_all_vfs_msi_state(struct pci_dev *pdev);
173 bool
174 ice_is_malicious_vf(struct ice_pf *pf, struct ice_rq_event_info *event,
175 		    u16 num_msg_proc, u16 num_msg_pending);
176 
177 int
178 ice_set_vf_port_vlan(struct net_device *netdev, int vf_id, u16 vlan_id, u8 qos,
179 		     __be16 vlan_proto);
180 
181 int
182 ice_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
183 	      int max_tx_rate);
184 
185 int ice_set_vf_trust(struct net_device *netdev, int vf_id, bool trusted);
186 
187 int ice_set_vf_link_state(struct net_device *netdev, int vf_id, int link_state);
188 
189 int ice_check_vf_ready_for_cfg(struct ice_vf *vf);
190 
191 bool ice_is_vf_disabled(struct ice_vf *vf);
192 
193 int ice_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool ena);
194 
195 int ice_calc_vf_reg_idx(struct ice_vf *vf, struct ice_q_vector *q_vector);
196 
197 void ice_set_vf_state_qs_dis(struct ice_vf *vf);
198 int
199 ice_get_vf_stats(struct net_device *netdev, int vf_id,
200 		 struct ifla_vf_stats *vf_stats);
201 bool ice_is_any_vf_in_promisc(struct ice_pf *pf);
202 void
203 ice_vf_lan_overflow_event(struct ice_pf *pf, struct ice_rq_event_info *event);
204 void ice_print_vfs_mdd_events(struct ice_pf *pf);
205 void ice_print_vf_rx_mdd_event(struct ice_vf *vf);
206 struct ice_vsi *ice_vf_ctrl_vsi_setup(struct ice_vf *vf);
207 int
208 ice_vc_send_msg_to_vf(struct ice_vf *vf, u32 v_opcode,
209 		      enum virtchnl_status_code v_retval, u8 *msg, u16 msglen);
210 bool ice_vc_isvalid_vsi_id(struct ice_vf *vf, u16 vsi_id);
211 #else /* CONFIG_PCI_IOV */
ice_process_vflr_event(struct ice_pf * pf)212 static inline void ice_process_vflr_event(struct ice_pf *pf) { }
ice_free_vfs(struct ice_pf * pf)213 static inline void ice_free_vfs(struct ice_pf *pf) { }
214 static inline
ice_vc_process_vf_msg(struct ice_pf * pf,struct ice_rq_event_info * event)215 void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event) { }
ice_vc_notify_link_state(struct ice_pf * pf)216 static inline void ice_vc_notify_link_state(struct ice_pf *pf) { }
ice_vc_notify_reset(struct ice_pf * pf)217 static inline void ice_vc_notify_reset(struct ice_pf *pf) { }
ice_vc_notify_vf_link_state(struct ice_vf * vf)218 static inline void ice_vc_notify_vf_link_state(struct ice_vf *vf) { }
ice_vc_change_ops_to_repr(struct ice_vc_vf_ops * ops)219 static inline void ice_vc_change_ops_to_repr(struct ice_vc_vf_ops *ops) { }
ice_vc_set_dflt_vf_ops(struct ice_vc_vf_ops * ops)220 static inline void ice_vc_set_dflt_vf_ops(struct ice_vc_vf_ops *ops) { }
ice_set_vf_state_qs_dis(struct ice_vf * vf)221 static inline void ice_set_vf_state_qs_dis(struct ice_vf *vf) { }
222 static inline
ice_vf_lan_overflow_event(struct ice_pf * pf,struct ice_rq_event_info * event)223 void ice_vf_lan_overflow_event(struct ice_pf *pf, struct ice_rq_event_info *event) { }
ice_print_vfs_mdd_events(struct ice_pf * pf)224 static inline void ice_print_vfs_mdd_events(struct ice_pf *pf) { }
ice_print_vf_rx_mdd_event(struct ice_vf * vf)225 static inline void ice_print_vf_rx_mdd_event(struct ice_vf *vf) { }
ice_restore_all_vfs_msi_state(struct pci_dev * pdev)226 static inline void ice_restore_all_vfs_msi_state(struct pci_dev *pdev) { }
227 
ice_check_vf_ready_for_cfg(struct ice_vf * vf)228 static inline int ice_check_vf_ready_for_cfg(struct ice_vf *vf)
229 {
230 	return -EOPNOTSUPP;
231 }
232 
ice_is_vf_disabled(struct ice_vf * vf)233 static inline bool ice_is_vf_disabled(struct ice_vf *vf)
234 {
235 	return true;
236 }
237 
ice_get_vf_vsi(struct ice_vf * vf)238 static inline struct ice_vsi *ice_get_vf_vsi(struct ice_vf *vf)
239 {
240 	return NULL;
241 }
242 
243 static inline bool
ice_is_malicious_vf(struct ice_pf __always_unused * pf,struct ice_rq_event_info __always_unused * event,u16 __always_unused num_msg_proc,u16 __always_unused num_msg_pending)244 ice_is_malicious_vf(struct ice_pf __always_unused *pf,
245 		    struct ice_rq_event_info __always_unused *event,
246 		    u16 __always_unused num_msg_proc,
247 		    u16 __always_unused num_msg_pending)
248 {
249 	return false;
250 }
251 
252 static inline bool
ice_reset_all_vfs(struct ice_pf __always_unused * pf,bool __always_unused is_vflr)253 ice_reset_all_vfs(struct ice_pf __always_unused *pf,
254 		  bool __always_unused is_vflr)
255 {
256 	return true;
257 }
258 
259 static inline bool
ice_reset_vf(struct ice_vf __always_unused * vf,bool __always_unused is_vflr)260 ice_reset_vf(struct ice_vf __always_unused *vf, bool __always_unused is_vflr)
261 {
262 	return true;
263 }
264 
265 static inline int
ice_sriov_configure(struct pci_dev __always_unused * pdev,int __always_unused num_vfs)266 ice_sriov_configure(struct pci_dev __always_unused *pdev,
267 		    int __always_unused num_vfs)
268 {
269 	return -EOPNOTSUPP;
270 }
271 
272 static inline int
ice_set_vf_mac(struct net_device __always_unused * netdev,int __always_unused vf_id,u8 __always_unused * mac)273 ice_set_vf_mac(struct net_device __always_unused *netdev,
274 	       int __always_unused vf_id, u8 __always_unused *mac)
275 {
276 	return -EOPNOTSUPP;
277 }
278 
279 static inline int
ice_get_vf_cfg(struct net_device __always_unused * netdev,int __always_unused vf_id,struct ifla_vf_info __always_unused * ivi)280 ice_get_vf_cfg(struct net_device __always_unused *netdev,
281 	       int __always_unused vf_id,
282 	       struct ifla_vf_info __always_unused *ivi)
283 {
284 	return -EOPNOTSUPP;
285 }
286 
287 static inline int
ice_set_vf_trust(struct net_device __always_unused * netdev,int __always_unused vf_id,bool __always_unused trusted)288 ice_set_vf_trust(struct net_device __always_unused *netdev,
289 		 int __always_unused vf_id, bool __always_unused trusted)
290 {
291 	return -EOPNOTSUPP;
292 }
293 
294 static inline int
ice_set_vf_port_vlan(struct net_device __always_unused * netdev,int __always_unused vf_id,u16 __always_unused vid,u8 __always_unused qos,__be16 __always_unused v_proto)295 ice_set_vf_port_vlan(struct net_device __always_unused *netdev,
296 		     int __always_unused vf_id, u16 __always_unused vid,
297 		     u8 __always_unused qos, __be16 __always_unused v_proto)
298 {
299 	return -EOPNOTSUPP;
300 }
301 
302 static inline int
ice_set_vf_spoofchk(struct net_device __always_unused * netdev,int __always_unused vf_id,bool __always_unused ena)303 ice_set_vf_spoofchk(struct net_device __always_unused *netdev,
304 		    int __always_unused vf_id, bool __always_unused ena)
305 {
306 	return -EOPNOTSUPP;
307 }
308 
309 static inline int
ice_set_vf_link_state(struct net_device __always_unused * netdev,int __always_unused vf_id,int __always_unused link_state)310 ice_set_vf_link_state(struct net_device __always_unused *netdev,
311 		      int __always_unused vf_id, int __always_unused link_state)
312 {
313 	return -EOPNOTSUPP;
314 }
315 
316 static inline int
ice_set_vf_bw(struct net_device __always_unused * netdev,int __always_unused vf_id,int __always_unused min_tx_rate,int __always_unused max_tx_rate)317 ice_set_vf_bw(struct net_device __always_unused *netdev,
318 	      int __always_unused vf_id, int __always_unused min_tx_rate,
319 	      int __always_unused max_tx_rate)
320 {
321 	return -EOPNOTSUPP;
322 }
323 
324 static inline int
ice_calc_vf_reg_idx(struct ice_vf __always_unused * vf,struct ice_q_vector __always_unused * q_vector)325 ice_calc_vf_reg_idx(struct ice_vf __always_unused *vf,
326 		    struct ice_q_vector __always_unused *q_vector)
327 {
328 	return 0;
329 }
330 
331 static inline int
ice_get_vf_stats(struct net_device __always_unused * netdev,int __always_unused vf_id,struct ifla_vf_stats __always_unused * vf_stats)332 ice_get_vf_stats(struct net_device __always_unused *netdev,
333 		 int __always_unused vf_id,
334 		 struct ifla_vf_stats __always_unused *vf_stats)
335 {
336 	return -EOPNOTSUPP;
337 }
338 
ice_is_any_vf_in_promisc(struct ice_pf __always_unused * pf)339 static inline bool ice_is_any_vf_in_promisc(struct ice_pf __always_unused *pf)
340 {
341 	return false;
342 }
343 #endif /* CONFIG_PCI_IOV */
344 #endif /* _ICE_VIRTCHNL_PF_H_ */
345