Merge git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
[linux-2.6-microblaze.git] / drivers / net / ethernet / intel / ice / ice_virtchnl_pf.h
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 #include "ice_vsi_vlan_ops.h"
9
10 /* Restrict number of MAC Addr and VLAN that non-trusted VF can programmed */
11 #define ICE_MAX_VLAN_PER_VF             8
12 /* MAC filters: 1 is reserved for the VF's default/perm_addr/LAA MAC, 1 for
13  * broadcast, and 16 for additional unicast/multicast filters
14  */
15 #define ICE_MAX_MACADDR_PER_VF          18
16
17 /* Malicious Driver Detection */
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 /* VF Hash Table access functions
42  *
43  * These functions provide abstraction for interacting with the VF hash table.
44  * In general, direct access to the hash table should be avoided outside of
45  * these functions where possible.
46  *
47  * The VF entries in the hash table are protected by reference counting to
48  * track lifetime of accesses from the table. The ice_get_vf_by_id() function
49  * obtains a reference to the VF structure which must be dropped by using
50  * ice_put_vf().
51  */
52
53 /**
54  * ice_for_each_vf - Iterate over each VF entry
55  * @pf: pointer to the PF private structure
56  * @bkt: bucket index used for iteration
57  * @vf: pointer to the VF entry currently being processed in the loop.
58  *
59  * The bkt variable is an unsigned integer iterator used to traverse the VF
60  * entries. It is *not* guaranteed to be the VF's vf_id. Do not assume it is.
61  * Use vf->vf_id to get the id number if needed.
62  *
63  * The caller is expected to be under the table_lock mutex for the entire
64  * loop. Use this iterator if your loop is long or if it might sleep.
65  */
66 #define ice_for_each_vf(pf, bkt, vf) \
67         hash_for_each((pf)->vfs.table, (bkt), (vf), entry)
68
69 /**
70  * ice_for_each_vf_rcu - Iterate over each VF entry protected by RCU
71  * @pf: pointer to the PF private structure
72  * @bkt: bucket index used for iteration
73  * @vf: pointer to the VF entry currently being processed in the loop.
74  *
75  * The bkt variable is an unsigned integer iterator used to traverse the VF
76  * entries. It is *not* guaranteed to be the VF's vf_id. Do not assume it is.
77  * Use vf->vf_id to get the id number if needed.
78  *
79  * The caller is expected to be under rcu_read_lock() for the entire loop.
80  * Only use this iterator if your loop is short and you can guarantee it does
81  * not sleep.
82  */
83 #define ice_for_each_vf_rcu(pf, bkt, vf) \
84         hash_for_each_rcu((pf)->vfs.table, (bkt), (vf), entry)
85
86 /* Specific VF states */
87 enum ice_vf_states {
88         ICE_VF_STATE_INIT = 0,          /* PF is initializing VF */
89         ICE_VF_STATE_ACTIVE,            /* VF resources are allocated for use */
90         ICE_VF_STATE_QS_ENA,            /* VF queue(s) enabled */
91         ICE_VF_STATE_DIS,
92         ICE_VF_STATE_MC_PROMISC,
93         ICE_VF_STATE_UC_PROMISC,
94         ICE_VF_STATES_NBITS
95 };
96
97 /* VF capabilities */
98 enum ice_virtchnl_cap {
99         ICE_VIRTCHNL_VF_CAP_L2 = 0,
100         ICE_VIRTCHNL_VF_CAP_PRIVILEGE,
101 };
102
103 struct ice_time_mac {
104         unsigned long time_modified;
105         u8 addr[ETH_ALEN];
106 };
107
108 /* VF MDD events print structure */
109 struct ice_mdd_vf_events {
110         u16 count;                      /* total count of Rx|Tx events */
111         /* count number of the last printed event */
112         u16 last_printed;
113 };
114
115 struct ice_vf;
116
117 struct ice_vc_vf_ops {
118         int (*get_ver_msg)(struct ice_vf *vf, u8 *msg);
119         int (*get_vf_res_msg)(struct ice_vf *vf, u8 *msg);
120         void (*reset_vf)(struct ice_vf *vf);
121         int (*add_mac_addr_msg)(struct ice_vf *vf, u8 *msg);
122         int (*del_mac_addr_msg)(struct ice_vf *vf, u8 *msg);
123         int (*cfg_qs_msg)(struct ice_vf *vf, u8 *msg);
124         int (*ena_qs_msg)(struct ice_vf *vf, u8 *msg);
125         int (*dis_qs_msg)(struct ice_vf *vf, u8 *msg);
126         int (*request_qs_msg)(struct ice_vf *vf, u8 *msg);
127         int (*cfg_irq_map_msg)(struct ice_vf *vf, u8 *msg);
128         int (*config_rss_key)(struct ice_vf *vf, u8 *msg);
129         int (*config_rss_lut)(struct ice_vf *vf, u8 *msg);
130         int (*get_stats_msg)(struct ice_vf *vf, u8 *msg);
131         int (*cfg_promiscuous_mode_msg)(struct ice_vf *vf, u8 *msg);
132         int (*add_vlan_msg)(struct ice_vf *vf, u8 *msg);
133         int (*remove_vlan_msg)(struct ice_vf *vf, u8 *msg);
134         int (*ena_vlan_stripping)(struct ice_vf *vf);
135         int (*dis_vlan_stripping)(struct ice_vf *vf);
136         int (*handle_rss_cfg_msg)(struct ice_vf *vf, u8 *msg, bool add);
137         int (*add_fdir_fltr_msg)(struct ice_vf *vf, u8 *msg);
138         int (*del_fdir_fltr_msg)(struct ice_vf *vf, u8 *msg);
139         int (*get_offload_vlan_v2_caps)(struct ice_vf *vf);
140         int (*add_vlan_v2_msg)(struct ice_vf *vf, u8 *msg);
141         int (*remove_vlan_v2_msg)(struct ice_vf *vf, u8 *msg);
142         int (*ena_vlan_stripping_v2_msg)(struct ice_vf *vf, u8 *msg);
143         int (*dis_vlan_stripping_v2_msg)(struct ice_vf *vf, u8 *msg);
144         int (*ena_vlan_insertion_v2_msg)(struct ice_vf *vf, u8 *msg);
145         int (*dis_vlan_insertion_v2_msg)(struct ice_vf *vf, u8 *msg);
146 };
147
148 /* Virtchnl/SR-IOV config info */
149 struct ice_vfs {
150         DECLARE_HASHTABLE(table, 8);    /* table of VF entries */
151         struct mutex table_lock;        /* Lock for protecting the hash table */
152         u16 num_supported;              /* max supported VFs on this PF */
153         u16 num_qps_per;                /* number of queue pairs per VF */
154         u16 num_msix_per;               /* number of MSI-X vectors per VF */
155         unsigned long last_printed_mdd_jiffies; /* MDD message rate limit */
156         DECLARE_BITMAP(malvfs, ICE_MAX_VF_COUNT); /* malicious VF indicator */
157 };
158
159 /* VF information structure */
160 struct ice_vf {
161         struct hlist_node entry;
162         struct rcu_head rcu;
163         struct kref refcnt;
164         struct ice_pf *pf;
165
166         /* Used during virtchnl message handling and NDO ops against the VF
167          * that will trigger a VFR
168          */
169         struct mutex cfg_lock;
170
171         u16 vf_id;                      /* VF ID in the PF space */
172         u16 lan_vsi_idx;                /* index into PF struct */
173         u16 ctrl_vsi_idx;
174         struct ice_vf_fdir fdir;
175         /* first vector index of this VF in the PF space */
176         int first_vector_idx;
177         struct ice_sw *vf_sw_id;        /* switch ID the VF VSIs connect to */
178         struct virtchnl_version_info vf_ver;
179         u32 driver_caps;                /* reported by VF driver */
180         struct virtchnl_ether_addr dev_lan_addr;
181         struct virtchnl_ether_addr hw_lan_addr;
182         struct ice_time_mac legacy_last_added_umac;
183         DECLARE_BITMAP(txq_ena, ICE_MAX_RSS_QS_PER_VF);
184         DECLARE_BITMAP(rxq_ena, ICE_MAX_RSS_QS_PER_VF);
185         struct ice_vlan port_vlan_info; /* Port VLAN ID, QoS, and TPID */
186         struct virtchnl_vlan_caps vlan_v2_caps;
187         u8 pf_set_mac:1;                /* VF MAC address set by VMM admin */
188         u8 trusted:1;
189         u8 spoofchk:1;
190         u8 link_forced:1;
191         u8 link_up:1;                   /* only valid if VF link is forced */
192         /* VSI indices - actual VSI pointers are maintained in the PF structure
193          * When assigned, these will be non-zero, because VSI 0 is always
194          * the main LAN VSI for the PF.
195          */
196         u16 lan_vsi_num;                /* ID as used by firmware */
197         unsigned int min_tx_rate;       /* Minimum Tx bandwidth limit in Mbps */
198         unsigned int max_tx_rate;       /* Maximum Tx bandwidth limit in Mbps */
199         DECLARE_BITMAP(vf_states, ICE_VF_STATES_NBITS); /* VF runtime states */
200
201         unsigned long vf_caps;          /* VF's adv. capabilities */
202         u8 num_req_qs;                  /* num of queue pairs requested by VF */
203         u16 num_mac;
204         u16 num_vf_qs;                  /* num of queue configured per VF */
205         struct ice_mdd_vf_events mdd_rx_events;
206         struct ice_mdd_vf_events mdd_tx_events;
207         DECLARE_BITMAP(opcodes_allowlist, VIRTCHNL_OP_MAX);
208
209         struct ice_repr *repr;
210
211         struct ice_vc_vf_ops vc_ops;
212
213         /* devlink port data */
214         struct devlink_port devlink_port;
215 };
216
217 #ifdef CONFIG_PCI_IOV
218 struct ice_vf *ice_get_vf_by_id(struct ice_pf *pf, u16 vf_id);
219 void ice_put_vf(struct ice_vf *vf);
220 bool ice_has_vfs(struct ice_pf *pf);
221 u16 ice_get_num_vfs(struct ice_pf *pf);
222 struct ice_vsi *ice_get_vf_vsi(struct ice_vf *vf);
223 void ice_process_vflr_event(struct ice_pf *pf);
224 int ice_sriov_configure(struct pci_dev *pdev, int num_vfs);
225 int ice_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac);
226 int
227 ice_get_vf_cfg(struct net_device *netdev, int vf_id, struct ifla_vf_info *ivi);
228
229 void ice_free_vfs(struct ice_pf *pf);
230 void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event);
231 void ice_vc_notify_link_state(struct ice_pf *pf);
232 void ice_vc_notify_reset(struct ice_pf *pf);
233 void ice_vc_notify_vf_link_state(struct ice_vf *vf);
234 void ice_vc_change_ops_to_repr(struct ice_vc_vf_ops *ops);
235 void ice_vc_set_dflt_vf_ops(struct ice_vc_vf_ops *ops);
236 bool ice_reset_all_vfs(struct ice_pf *pf, bool is_vflr);
237 bool ice_reset_vf(struct ice_vf *vf, bool is_vflr);
238 void ice_restore_all_vfs_msi_state(struct pci_dev *pdev);
239 bool
240 ice_is_malicious_vf(struct ice_pf *pf, struct ice_rq_event_info *event,
241                     u16 num_msg_proc, u16 num_msg_pending);
242
243 int
244 ice_set_vf_port_vlan(struct net_device *netdev, int vf_id, u16 vlan_id, u8 qos,
245                      __be16 vlan_proto);
246
247 int
248 ice_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
249               int max_tx_rate);
250
251 int ice_set_vf_trust(struct net_device *netdev, int vf_id, bool trusted);
252
253 int ice_set_vf_link_state(struct net_device *netdev, int vf_id, int link_state);
254
255 int ice_check_vf_ready_for_cfg(struct ice_vf *vf);
256
257 bool ice_is_vf_disabled(struct ice_vf *vf);
258
259 int ice_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool ena);
260
261 int ice_calc_vf_reg_idx(struct ice_vf *vf, struct ice_q_vector *q_vector);
262
263 void ice_set_vf_state_qs_dis(struct ice_vf *vf);
264 int
265 ice_get_vf_stats(struct net_device *netdev, int vf_id,
266                  struct ifla_vf_stats *vf_stats);
267 bool ice_is_any_vf_in_promisc(struct ice_pf *pf);
268 void
269 ice_vf_lan_overflow_event(struct ice_pf *pf, struct ice_rq_event_info *event);
270 void ice_print_vfs_mdd_events(struct ice_pf *pf);
271 void ice_print_vf_rx_mdd_event(struct ice_vf *vf);
272 bool
273 ice_vc_validate_pattern(struct ice_vf *vf, struct virtchnl_proto_hdrs *proto);
274 struct ice_vsi *ice_vf_ctrl_vsi_setup(struct ice_vf *vf);
275 int
276 ice_vc_send_msg_to_vf(struct ice_vf *vf, u32 v_opcode,
277                       enum virtchnl_status_code v_retval, u8 *msg, u16 msglen);
278 bool ice_vc_isvalid_vsi_id(struct ice_vf *vf, u16 vsi_id);
279 bool ice_vf_is_port_vlan_ena(struct ice_vf *vf);
280 #else /* CONFIG_PCI_IOV */
281 static inline struct ice_vf *ice_get_vf_by_id(struct ice_pf *pf, u16 vf_id)
282 {
283         return NULL;
284 }
285
286 static inline void ice_put_vf(struct ice_vf *vf)
287 {
288 }
289
290 static inline bool ice_has_vfs(struct ice_pf *pf)
291 {
292         return false;
293 }
294
295 static inline u16 ice_get_num_vfs(struct ice_pf *pf)
296 {
297         return 0;
298 }
299
300 static inline void ice_process_vflr_event(struct ice_pf *pf) { }
301 static inline void ice_free_vfs(struct ice_pf *pf) { }
302 static inline
303 void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event) { }
304 static inline void ice_vc_notify_link_state(struct ice_pf *pf) { }
305 static inline void ice_vc_notify_reset(struct ice_pf *pf) { }
306 static inline void ice_vc_notify_vf_link_state(struct ice_vf *vf) { }
307 static inline void ice_vc_change_ops_to_repr(struct ice_vc_vf_ops *ops) { }
308 static inline void ice_vc_set_dflt_vf_ops(struct ice_vc_vf_ops *ops) { }
309 static inline void ice_set_vf_state_qs_dis(struct ice_vf *vf) { }
310 static inline
311 void ice_vf_lan_overflow_event(struct ice_pf *pf, struct ice_rq_event_info *event) { }
312 static inline void ice_print_vfs_mdd_events(struct ice_pf *pf) { }
313 static inline void ice_print_vf_rx_mdd_event(struct ice_vf *vf) { }
314 static inline void ice_restore_all_vfs_msi_state(struct pci_dev *pdev) { }
315
316 static inline int ice_check_vf_ready_for_cfg(struct ice_vf *vf)
317 {
318         return -EOPNOTSUPP;
319 }
320
321 static inline bool ice_is_vf_disabled(struct ice_vf *vf)
322 {
323         return true;
324 }
325
326 static inline struct ice_vsi *ice_get_vf_vsi(struct ice_vf *vf)
327 {
328         return NULL;
329 }
330
331 static inline bool
332 ice_is_malicious_vf(struct ice_pf __always_unused *pf,
333                     struct ice_rq_event_info __always_unused *event,
334                     u16 __always_unused num_msg_proc,
335                     u16 __always_unused num_msg_pending)
336 {
337         return false;
338 }
339
340 static inline bool
341 ice_reset_all_vfs(struct ice_pf __always_unused *pf,
342                   bool __always_unused is_vflr)
343 {
344         return true;
345 }
346
347 static inline bool
348 ice_reset_vf(struct ice_vf __always_unused *vf, bool __always_unused is_vflr)
349 {
350         return true;
351 }
352
353 static inline int
354 ice_sriov_configure(struct pci_dev __always_unused *pdev,
355                     int __always_unused num_vfs)
356 {
357         return -EOPNOTSUPP;
358 }
359
360 static inline int
361 ice_set_vf_mac(struct net_device __always_unused *netdev,
362                int __always_unused vf_id, u8 __always_unused *mac)
363 {
364         return -EOPNOTSUPP;
365 }
366
367 static inline int
368 ice_get_vf_cfg(struct net_device __always_unused *netdev,
369                int __always_unused vf_id,
370                struct ifla_vf_info __always_unused *ivi)
371 {
372         return -EOPNOTSUPP;
373 }
374
375 static inline int
376 ice_set_vf_trust(struct net_device __always_unused *netdev,
377                  int __always_unused vf_id, bool __always_unused trusted)
378 {
379         return -EOPNOTSUPP;
380 }
381
382 static inline int
383 ice_set_vf_port_vlan(struct net_device __always_unused *netdev,
384                      int __always_unused vf_id, u16 __always_unused vid,
385                      u8 __always_unused qos, __be16 __always_unused v_proto)
386 {
387         return -EOPNOTSUPP;
388 }
389
390 static inline int
391 ice_set_vf_spoofchk(struct net_device __always_unused *netdev,
392                     int __always_unused vf_id, bool __always_unused ena)
393 {
394         return -EOPNOTSUPP;
395 }
396
397 static inline int
398 ice_set_vf_link_state(struct net_device __always_unused *netdev,
399                       int __always_unused vf_id, int __always_unused link_state)
400 {
401         return -EOPNOTSUPP;
402 }
403
404 static inline int
405 ice_set_vf_bw(struct net_device __always_unused *netdev,
406               int __always_unused vf_id, int __always_unused min_tx_rate,
407               int __always_unused max_tx_rate)
408 {
409         return -EOPNOTSUPP;
410 }
411
412 static inline int
413 ice_calc_vf_reg_idx(struct ice_vf __always_unused *vf,
414                     struct ice_q_vector __always_unused *q_vector)
415 {
416         return 0;
417 }
418
419 static inline int
420 ice_get_vf_stats(struct net_device __always_unused *netdev,
421                  int __always_unused vf_id,
422                  struct ifla_vf_stats __always_unused *vf_stats)
423 {
424         return -EOPNOTSUPP;
425 }
426
427 static inline bool ice_is_any_vf_in_promisc(struct ice_pf __always_unused *pf)
428 {
429         return false;
430 }
431
432 static inline bool ice_vf_is_port_vlan_ena(struct ice_vf __always_unused *vf)
433 {
434         return false;
435 }
436 #endif /* CONFIG_PCI_IOV */
437 #endif /* _ICE_VIRTCHNL_PF_H_ */