xref: /freebsd/sys/dev/ice/ice_switch.h (revision 848ee2a3a8b47c9905fc51fefcf60eb371edbb98)
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*  Copyright (c) 2021, Intel Corporation
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above copyright notice,
9  *      this list of conditions and the following disclaimer.
10  *
11  *   2. Redistributions in binary form must reproduce the above copyright
12  *      notice, this list of conditions and the following disclaimer in the
13  *      documentation and/or other materials provided with the distribution.
14  *
15  *   3. Neither the name of the Intel Corporation nor the names of its
16  *      contributors may be used to endorse or promote products derived from
17  *      this software without specific prior written permission.
18  *
19  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  *  POSSIBILITY OF SUCH DAMAGE.
30  */
31 /*$FreeBSD$*/
32 
33 #ifndef _ICE_SWITCH_H_
34 #define _ICE_SWITCH_H_
35 
36 #include "ice_common.h"
37 #include "ice_protocol_type.h"
38 
39 #define ICE_SW_CFG_MAX_BUF_LEN 2048
40 #define ICE_MAX_SW 256
41 #define ICE_DFLT_VSI_INVAL 0xff
42 #define ICE_FLTR_RX BIT(0)
43 #define ICE_FLTR_TX BIT(1)
44 #define ICE_FLTR_TX_RX (ICE_FLTR_RX | ICE_FLTR_TX)
45 
46 #define DUMMY_ETH_HDR_LEN		16
47 #define ICE_SW_RULE_RX_TX_ETH_HDR_SIZE \
48 	(offsetof(struct ice_aqc_sw_rules_elem, pdata.lkup_tx_rx.hdr) + \
49 	 (DUMMY_ETH_HDR_LEN * \
50 	  sizeof(((struct ice_sw_rule_lkup_rx_tx *)0)->hdr[0])))
51 #define ICE_SW_RULE_RX_TX_NO_HDR_SIZE \
52 	(offsetof(struct ice_aqc_sw_rules_elem, pdata.lkup_tx_rx.hdr))
53 #define ICE_SW_RULE_LG_ACT_SIZE(n) \
54 	(offsetof(struct ice_aqc_sw_rules_elem, pdata.lg_act.act) + \
55 	 ((n) * sizeof(((struct ice_sw_rule_lg_act *)0)->act[0])))
56 #define ICE_SW_RULE_VSI_LIST_SIZE(n) \
57 	(offsetof(struct ice_aqc_sw_rules_elem, pdata.vsi_list.vsi) + \
58 	 ((n) * sizeof(((struct ice_sw_rule_vsi_list *)0)->vsi[0])))
59 
60 /* Worst case buffer length for ice_aqc_opc_get_res_alloc */
61 #define ICE_MAX_RES_TYPES 0x80
62 #define ICE_AQ_GET_RES_ALLOC_BUF_LEN \
63 	(ICE_MAX_RES_TYPES * sizeof(struct ice_aqc_get_res_resp_elem))
64 
65 #define ICE_VSI_INVAL_ID 0xFFFF
66 #define ICE_INVAL_Q_HANDLE 0xFFFF
67 
68 /* VSI context structure for add/get/update/free operations */
69 struct ice_vsi_ctx {
70 	u16 vsi_num;
71 	u16 vsis_allocd;
72 	u16 vsis_unallocated;
73 	u16 flags;
74 	struct ice_aqc_vsi_props info;
75 	struct ice_sched_vsi_info sched;
76 	u8 alloc_from_pool;
77 	u8 vf_num;
78 	u16 num_lan_q_entries[ICE_MAX_TRAFFIC_CLASS];
79 	struct ice_q_ctx *lan_q_ctx[ICE_MAX_TRAFFIC_CLASS];
80 };
81 
82 /* This is to be used by add/update mirror rule Admin Queue command */
83 struct ice_mir_rule_buf {
84 	u16 vsi_idx; /* VSI index */
85 
86 	/* For each VSI, user can specify whether corresponding VSI
87 	 * should be added/removed to/from mirror rule
88 	 *
89 	 * add mirror rule: this should always be TRUE.
90 	 * update mirror rule:  add(true) or remove(false) VSI to/from
91 	 * mirror rule
92 	 */
93 	u8 add;
94 };
95 
96 /* Switch recipe ID enum values are specific to hardware */
97 enum ice_sw_lkup_type {
98 	ICE_SW_LKUP_ETHERTYPE = 0,
99 	ICE_SW_LKUP_MAC = 1,
100 	ICE_SW_LKUP_MAC_VLAN = 2,
101 	ICE_SW_LKUP_PROMISC = 3,
102 	ICE_SW_LKUP_VLAN = 4,
103 	ICE_SW_LKUP_DFLT = 5,
104 	ICE_SW_LKUP_ETHERTYPE_MAC = 8,
105 	ICE_SW_LKUP_PROMISC_VLAN = 9,
106 	ICE_SW_LKUP_LAST
107 };
108 
109 /* type of filter src ID */
110 enum ice_src_id {
111 	ICE_SRC_ID_UNKNOWN = 0,
112 	ICE_SRC_ID_VSI,
113 	ICE_SRC_ID_QUEUE,
114 	ICE_SRC_ID_LPORT,
115 };
116 
117 struct ice_fltr_info {
118 	/* Look up information: how to look up packet */
119 	enum ice_sw_lkup_type lkup_type;
120 	/* Forward action: filter action to do after lookup */
121 	enum ice_sw_fwd_act_type fltr_act;
122 	/* rule ID returned by firmware once filter rule is created */
123 	u16 fltr_rule_id;
124 	u16 flag;
125 
126 	/* Source VSI for LOOKUP_TX or source port for LOOKUP_RX */
127 	u16 src;
128 	enum ice_src_id src_id;
129 
130 	union {
131 		struct {
132 			u8 mac_addr[ETH_ALEN];
133 		} mac;
134 		struct {
135 			u8 mac_addr[ETH_ALEN];
136 			u16 vlan_id;
137 		} mac_vlan;
138 		struct {
139 			u16 vlan_id;
140 			u16 tpid;
141 			u8 tpid_valid;
142 		} vlan;
143 		/* Set lkup_type as ICE_SW_LKUP_ETHERTYPE
144 		 * if just using ethertype as filter. Set lkup_type as
145 		 * ICE_SW_LKUP_ETHERTYPE_MAC if MAC also needs to be
146 		 * passed in as filter.
147 		 */
148 		struct {
149 			u16 ethertype;
150 			u8 mac_addr[ETH_ALEN]; /* optional */
151 		} ethertype_mac;
152 	} l_data; /* Make sure to zero out the memory of l_data before using
153 		   * it or only set the data associated with lookup match
154 		   * rest everything should be zero
155 		   */
156 
157 	/* Depending on filter action */
158 	union {
159 		/* queue ID in case of ICE_FWD_TO_Q and starting
160 		 * queue ID in case of ICE_FWD_TO_QGRP.
161 		 */
162 		u16 q_id:11;
163 		u16 hw_vsi_id:10;
164 		u16 vsi_list_id:10;
165 	} fwd_id;
166 
167 	/* Sw VSI handle */
168 	u16 vsi_handle;
169 
170 	/* Set to num_queues if action is ICE_FWD_TO_QGRP. This field
171 	 * determines the range of queues the packet needs to be forwarded to.
172 	 * Note that qgrp_size must be set to a power of 2.
173 	 */
174 	u8 qgrp_size;
175 
176 	/* Rule creations populate these indicators basing on the switch type */
177 	u8 lb_en;	/* Indicate if packet can be looped back */
178 	u8 lan_en;	/* Indicate if packet can be forwarded to the uplink */
179 };
180 
181 struct ice_adv_lkup_elem {
182 	enum ice_protocol_type type;
183 	union ice_prot_hdr h_u;	/* Header values */
184 	union ice_prot_hdr m_u;	/* Mask of header values to match */
185 };
186 
187 struct ice_sw_act_ctrl {
188 	/* Source VSI for LOOKUP_TX or source port for LOOKUP_RX */
189 	u16 src;
190 	u16 flag;
191 	enum ice_sw_fwd_act_type fltr_act;
192 	/* Depending on filter action */
193 	union {
194 		/* This is a queue ID in case of ICE_FWD_TO_Q and starting
195 		 * queue ID in case of ICE_FWD_TO_QGRP.
196 		 */
197 		u16 q_id:11;
198 		u16 vsi_id:10;
199 		u16 hw_vsi_id:10;
200 		u16 vsi_list_id:10;
201 	} fwd_id;
202 	/* software VSI handle */
203 	u16 vsi_handle;
204 	u8 qgrp_size;
205 };
206 
207 struct ice_rule_query_data {
208 	/* Recipe ID for which the requested rule was added */
209 	u16 rid;
210 	/* Rule ID that was added or is supposed to be removed */
211 	u16 rule_id;
212 	/* vsi_handle for which Rule was added or is supposed to be removed */
213 	u16 vsi_handle;
214 };
215 
216 struct ice_adv_rule_info {
217 	enum ice_sw_tunnel_type tun_type;
218 	struct ice_sw_act_ctrl sw_act;
219 	u32 priority;
220 	u8 rx; /* true means LOOKUP_RX otherwise LOOKUP_TX */
221 	u16 fltr_rule_id;
222 };
223 
224 /* A collection of one or more four word recipe */
225 struct ice_sw_recipe {
226 	/* For a chained recipe the root recipe is what should be used for
227 	 * programming rules
228 	 */
229 	u8 is_root;
230 	u8 root_rid;
231 	u8 recp_created;
232 
233 	/* Number of extraction words */
234 	u8 n_ext_words;
235 	/* Protocol ID and Offset pair (extraction word) to describe the
236 	 * recipe
237 	 */
238 	struct ice_fv_word ext_words[ICE_MAX_CHAIN_WORDS];
239 	u16 word_masks[ICE_MAX_CHAIN_WORDS];
240 
241 	/* if this recipe is a collection of other recipe */
242 	u8 big_recp;
243 
244 	/* if this recipe is part of another bigger recipe then chain index
245 	 * corresponding to this recipe
246 	 */
247 	u8 chain_idx;
248 
249 	/* if this recipe is a collection of other recipe then count of other
250 	 * recipes and recipe IDs of those recipes
251 	 */
252 	u8 n_grp_count;
253 
254 	/* Bit map specifying the IDs associated with this group of recipe */
255 	ice_declare_bitmap(r_bitmap, ICE_MAX_NUM_RECIPES);
256 
257 	enum ice_sw_tunnel_type tun_type;
258 
259 	/* List of type ice_fltr_mgmt_list_entry or adv_rule */
260 	u8 adv_rule;
261 	struct LIST_HEAD_TYPE filt_rules;
262 	struct LIST_HEAD_TYPE filt_replay_rules;
263 
264 	struct ice_lock filt_rule_lock;	/* protect filter rule structure */
265 
266 	/* Profiles this recipe should be associated with */
267 	struct LIST_HEAD_TYPE fv_list;
268 
269 	/* Profiles this recipe is associated with */
270 	u8 num_profs, *prof_ids;
271 
272 	/* Bit map for possible result indexes */
273 	ice_declare_bitmap(res_idxs, ICE_MAX_FV_WORDS);
274 
275 	/* This allows user to specify the recipe priority.
276 	 * For now, this becomes 'fwd_priority' when recipe
277 	 * is created, usually recipes can have 'fwd' and 'join'
278 	 * priority.
279 	 */
280 	u8 priority;
281 
282 	struct LIST_HEAD_TYPE rg_list;
283 
284 	/* AQ buffer associated with this recipe */
285 	struct ice_aqc_recipe_data_elem *root_buf;
286 	/* This struct saves the fv_words for a given lookup */
287 	struct ice_prot_lkup_ext lkup_exts;
288 };
289 
290 /* Bookkeeping structure to hold bitmap of VSIs corresponding to VSI list ID */
291 struct ice_vsi_list_map_info {
292 	struct LIST_ENTRY_TYPE list_entry;
293 	ice_declare_bitmap(vsi_map, ICE_MAX_VSI);
294 	u16 vsi_list_id;
295 	/* counter to track how many rules are reusing this VSI list */
296 	u16 ref_cnt;
297 };
298 
299 struct ice_fltr_list_entry {
300 	struct LIST_ENTRY_TYPE list_entry;
301 	enum ice_status status;
302 	struct ice_fltr_info fltr_info;
303 };
304 
305 /**
306  * enum ice_fltr_marker - Marker for syncing OS and driver filter lists
307  * @ICE_FLTR_NOT_FOUND: initial state, indicates filter has not been found
308  * @ICE_FLTR_FOUND: set when a filter has been found in both lists
309  *
310  * This enumeration is used to help sync an operating system provided filter
311  * list with the filters previously added.
312  *
313  * This is required for FreeBSD because the operating system does not provide
314  * individual indications of whether a filter has been added or deleted, but
315  * instead just notifies the driver with the entire new list.
316  *
317  * To use this marker state, the driver shall initially reset all filters to
318  * the ICE_FLTR_NOT_FOUND state. Then, for each filter in the OS list, it
319  * shall search the driver list for the filter. If found, the filter state
320  * will be set to ICE_FLTR_FOUND. If not found, that filter will be added.
321  * Finally, the driver shall search the internal filter list for all filters
322  * still marked as ICE_FLTR_NOT_FOUND and remove them.
323  */
324 enum ice_fltr_marker {
325 	ICE_FLTR_NOT_FOUND,
326 	ICE_FLTR_FOUND,
327 };
328 
329 /* This defines an entry in the list that maintains MAC or VLAN membership
330  * to HW list mapping, since multiple VSIs can subscribe to the same MAC or
331  * VLAN. As an optimization the VSI list should be created only when a
332  * second VSI becomes a subscriber to the same MAC address. VSI lists are always
333  * used for VLAN membership.
334  */
335 struct ice_fltr_mgmt_list_entry {
336 	/* back pointer to VSI list ID to VSI list mapping */
337 	struct ice_vsi_list_map_info *vsi_list_info;
338 	u16 vsi_count;
339 #define ICE_INVAL_LG_ACT_INDEX 0xffff
340 	u16 lg_act_idx;
341 #define ICE_INVAL_SW_MARKER_ID 0xffff
342 	u16 sw_marker_id;
343 	struct LIST_ENTRY_TYPE list_entry;
344 	struct ice_fltr_info fltr_info;
345 #define ICE_INVAL_COUNTER_ID 0xff
346 	u8 counter_index;
347 	enum ice_fltr_marker marker;
348 };
349 
350 struct ice_adv_fltr_mgmt_list_entry {
351 	struct LIST_ENTRY_TYPE list_entry;
352 
353 	struct ice_adv_lkup_elem *lkups;
354 	struct ice_adv_rule_info rule_info;
355 	u16 lkups_cnt;
356 	struct ice_vsi_list_map_info *vsi_list_info;
357 	u16 vsi_count;
358 };
359 
360 enum ice_promisc_flags {
361 	ICE_PROMISC_UCAST_RX = 0x1,
362 	ICE_PROMISC_UCAST_TX = 0x2,
363 	ICE_PROMISC_MCAST_RX = 0x4,
364 	ICE_PROMISC_MCAST_TX = 0x8,
365 	ICE_PROMISC_BCAST_RX = 0x10,
366 	ICE_PROMISC_BCAST_TX = 0x20,
367 	ICE_PROMISC_VLAN_RX = 0x40,
368 	ICE_PROMISC_VLAN_TX = 0x80,
369 };
370 
371 /* VSI related commands */
372 enum ice_status
373 ice_aq_add_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
374 	       struct ice_sq_cd *cd);
375 enum ice_status
376 ice_aq_free_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
377 		bool keep_vsi_alloc, struct ice_sq_cd *cd);
378 enum ice_status
379 ice_aq_update_vsi(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
380 		  struct ice_sq_cd *cd);
381 enum ice_status
382 ice_add_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
383 	    struct ice_sq_cd *cd);
384 enum ice_status
385 ice_free_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
386 	     bool keep_vsi_alloc, struct ice_sq_cd *cd);
387 enum ice_status
388 ice_update_vsi(struct ice_hw *hw, u16 vsi_handle, struct ice_vsi_ctx *vsi_ctx,
389 	       struct ice_sq_cd *cd);
390 struct ice_vsi_ctx *ice_get_vsi_ctx(struct ice_hw *hw, u16 vsi_handle);
391 void ice_clear_all_vsi_ctx(struct ice_hw *hw);
392 enum ice_status
393 ice_aq_get_vsi_params(struct ice_hw *hw, struct ice_vsi_ctx *vsi_ctx,
394 		      struct ice_sq_cd *cd);
395 enum ice_status
396 ice_aq_add_update_mir_rule(struct ice_hw *hw, u16 rule_type, u16 dest_vsi,
397 			   u16 count, struct ice_mir_rule_buf *mr_buf,
398 			   struct ice_sq_cd *cd, u16 *rule_id);
399 enum ice_status
400 ice_aq_delete_mir_rule(struct ice_hw *hw, u16 rule_id, bool keep_allocd,
401 		       struct ice_sq_cd *cd);
402 enum ice_status
403 ice_aq_get_storm_ctrl(struct ice_hw *hw, u32 *bcast_thresh, u32 *mcast_thresh,
404 		      u32 *ctl_bitmask);
405 enum ice_status
406 ice_aq_set_storm_ctrl(struct ice_hw *hw, u32 bcast_thresh, u32 mcast_thresh,
407 		      u32 ctl_bitmask);
408 /* Switch config */
409 enum ice_status ice_get_initial_sw_cfg(struct ice_hw *hw);
410 
411 enum ice_status
412 ice_alloc_vlan_res_counter(struct ice_hw *hw, u16 *counter_id);
413 enum ice_status
414 ice_free_vlan_res_counter(struct ice_hw *hw, u16 counter_id);
415 
416 enum ice_status ice_update_sw_rule_bridge_mode(struct ice_hw *hw);
417 enum ice_status ice_alloc_rss_global_lut(struct ice_hw *hw, bool shared_res, u16 *global_lut_id);
418 enum ice_status ice_free_rss_global_lut(struct ice_hw *hw, u16 global_lut_id);
419 enum ice_status
420 ice_alloc_sw(struct ice_hw *hw, bool ena_stats, bool shared_res, u16 *sw_id,
421 	     u16 *counter_id);
422 enum ice_status
423 ice_free_sw(struct ice_hw *hw, u16 sw_id, u16 counter_id);
424 enum ice_status
425 ice_aq_get_res_alloc(struct ice_hw *hw, u16 *num_entries,
426 		     struct ice_aqc_get_res_resp_elem *buf, u16 buf_size,
427 		     struct ice_sq_cd *cd);
428 enum ice_status
429 ice_aq_get_res_descs(struct ice_hw *hw, u16 num_entries,
430 		     struct ice_aqc_res_elem *buf, u16 buf_size, u16 res_type,
431 		     bool res_shared, u16 *desc_id, struct ice_sq_cd *cd);
432 enum ice_status
433 ice_add_vlan(struct ice_hw *hw, struct LIST_HEAD_TYPE *m_list);
434 enum ice_status
435 ice_remove_vlan(struct ice_hw *hw, struct LIST_HEAD_TYPE *v_list);
436 void ice_rem_all_sw_rules_info(struct ice_hw *hw);
437 enum ice_status ice_add_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *m_lst);
438 enum ice_status ice_remove_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *m_lst);
439 enum ice_status
440 ice_add_eth_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *em_list);
441 enum ice_status
442 ice_remove_eth_mac(struct ice_hw *hw, struct LIST_HEAD_TYPE *em_list);
443 
444 enum ice_status
445 ice_add_mac_with_sw_marker(struct ice_hw *hw, struct ice_fltr_info *f_info,
446 			   u16 sw_marker);
447 enum ice_status
448 ice_add_mac_with_counter(struct ice_hw *hw, struct ice_fltr_info *f_info);
449 void ice_remove_vsi_fltr(struct ice_hw *hw, u16 vsi_handle);
450 
451 /* Promisc/defport setup for VSIs */
452 enum ice_status
453 ice_cfg_dflt_vsi(struct ice_port_info *pi, u16 vsi_handle, bool set,
454 		 u8 direction);
455 enum ice_status
456 ice_set_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
457 		    u16 vid);
458 enum ice_status
459 ice_clear_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
460 		      u16 vid);
461 enum ice_status
462 ice_set_vlan_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 promisc_mask,
463 			 bool rm_vlan_promisc);
464 
465 /* Get VSIs Promisc/defport settings */
466 enum ice_status
467 ice_get_vsi_promisc(struct ice_hw *hw, u16 vsi_handle, u8 *promisc_mask,
468 		    u16 *vid);
469 enum ice_status
470 ice_get_vsi_vlan_promisc(struct ice_hw *hw, u16 vsi_handle, u8 *promisc_mask,
471 			 u16 *vid);
472 
473 enum ice_status ice_replay_all_fltr(struct ice_hw *hw);
474 
475 enum ice_status
476 ice_init_def_sw_recp(struct ice_hw *hw, struct ice_sw_recipe **recp_list);
477 u16 ice_get_hw_vsi_num(struct ice_hw *hw, u16 vsi_handle);
478 bool ice_is_vsi_valid(struct ice_hw *hw, u16 vsi_handle);
479 
480 enum ice_status
481 ice_replay_vsi_all_fltr(struct ice_hw *hw, struct ice_port_info *pi,
482 			u16 vsi_handle);
483 void ice_rm_sw_replay_rule_info(struct ice_hw *hw, struct ice_switch_info *sw);
484 void ice_rm_all_sw_replay_rule_info(struct ice_hw *hw);
485 #endif /* _ICE_SWITCH_H_ */
486