xref: /dpdk/drivers/net/ice/base/ice_flex_pipe.c (revision bb07d96d)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2001-2021 Intel Corporation
3  */
4 
5 #include "ice_common.h"
6 #include "ice_flex_pipe.h"
7 #include "ice_protocol_type.h"
8 #include "ice_flow.h"
9 
10 /* For supporting double VLAN mode, it is necessary to enable or disable certain
11  * boost tcam entries. The metadata labels names that match the following
12  * prefixes will be saved to allow enabling double VLAN mode.
13  */
14 #define ICE_DVM_PRE	"BOOST_MAC_VLAN_DVM"	/* enable these entries */
15 #define ICE_SVM_PRE	"BOOST_MAC_VLAN_SVM"	/* disable these entries */
16 
17 /* To support tunneling entries by PF, the package will append the PF number to
18  * the label; for example TNL_VXLAN_PF0, TNL_VXLAN_PF1, TNL_VXLAN_PF2, etc.
19  */
20 #define ICE_TNL_PRE	"TNL_"
21 static const struct ice_tunnel_type_scan tnls[] = {
22 	{ TNL_VXLAN,		"TNL_VXLAN_PF" },
23 	{ TNL_GENEVE,		"TNL_GENEVE_PF" },
24 	{ TNL_ECPRI,		"TNL_UDP_ECPRI_PF" },
25 	{ TNL_LAST,		"" }
26 };
27 
28 static const u32 ice_sect_lkup[ICE_BLK_COUNT][ICE_SECT_COUNT] = {
29 	/* SWITCH */
30 	{
31 		ICE_SID_XLT0_SW,
32 		ICE_SID_XLT_KEY_BUILDER_SW,
33 		ICE_SID_XLT1_SW,
34 		ICE_SID_XLT2_SW,
35 		ICE_SID_PROFID_TCAM_SW,
36 		ICE_SID_PROFID_REDIR_SW,
37 		ICE_SID_FLD_VEC_SW,
38 		ICE_SID_CDID_KEY_BUILDER_SW,
39 		ICE_SID_CDID_REDIR_SW
40 	},
41 
42 	/* ACL */
43 	{
44 		ICE_SID_XLT0_ACL,
45 		ICE_SID_XLT_KEY_BUILDER_ACL,
46 		ICE_SID_XLT1_ACL,
47 		ICE_SID_XLT2_ACL,
48 		ICE_SID_PROFID_TCAM_ACL,
49 		ICE_SID_PROFID_REDIR_ACL,
50 		ICE_SID_FLD_VEC_ACL,
51 		ICE_SID_CDID_KEY_BUILDER_ACL,
52 		ICE_SID_CDID_REDIR_ACL
53 	},
54 
55 	/* FD */
56 	{
57 		ICE_SID_XLT0_FD,
58 		ICE_SID_XLT_KEY_BUILDER_FD,
59 		ICE_SID_XLT1_FD,
60 		ICE_SID_XLT2_FD,
61 		ICE_SID_PROFID_TCAM_FD,
62 		ICE_SID_PROFID_REDIR_FD,
63 		ICE_SID_FLD_VEC_FD,
64 		ICE_SID_CDID_KEY_BUILDER_FD,
65 		ICE_SID_CDID_REDIR_FD
66 	},
67 
68 	/* RSS */
69 	{
70 		ICE_SID_XLT0_RSS,
71 		ICE_SID_XLT_KEY_BUILDER_RSS,
72 		ICE_SID_XLT1_RSS,
73 		ICE_SID_XLT2_RSS,
74 		ICE_SID_PROFID_TCAM_RSS,
75 		ICE_SID_PROFID_REDIR_RSS,
76 		ICE_SID_FLD_VEC_RSS,
77 		ICE_SID_CDID_KEY_BUILDER_RSS,
78 		ICE_SID_CDID_REDIR_RSS
79 	},
80 
81 	/* PE */
82 	{
83 		ICE_SID_XLT0_PE,
84 		ICE_SID_XLT_KEY_BUILDER_PE,
85 		ICE_SID_XLT1_PE,
86 		ICE_SID_XLT2_PE,
87 		ICE_SID_PROFID_TCAM_PE,
88 		ICE_SID_PROFID_REDIR_PE,
89 		ICE_SID_FLD_VEC_PE,
90 		ICE_SID_CDID_KEY_BUILDER_PE,
91 		ICE_SID_CDID_REDIR_PE
92 	}
93 };
94 
95 /**
96  * ice_sect_id - returns section ID
97  * @blk: block type
98  * @sect: section type
99  *
100  * This helper function returns the proper section ID given a block type and a
101  * section type.
102  */
103 static u32 ice_sect_id(enum ice_block blk, enum ice_sect sect)
104 {
105 	return ice_sect_lkup[blk][sect];
106 }
107 
108 /**
109  * ice_pkg_val_buf
110  * @buf: pointer to the ice buffer
111  *
112  * This helper function validates a buffer's header.
113  */
114 static struct ice_buf_hdr *ice_pkg_val_buf(struct ice_buf *buf)
115 {
116 	struct ice_buf_hdr *hdr;
117 	u16 section_count;
118 	u16 data_end;
119 
120 	hdr = (struct ice_buf_hdr *)buf->buf;
121 	/* verify data */
122 	section_count = LE16_TO_CPU(hdr->section_count);
123 	if (section_count < ICE_MIN_S_COUNT || section_count > ICE_MAX_S_COUNT)
124 		return NULL;
125 
126 	data_end = LE16_TO_CPU(hdr->data_end);
127 	if (data_end < ICE_MIN_S_DATA_END || data_end > ICE_MAX_S_DATA_END)
128 		return NULL;
129 
130 	return hdr;
131 }
132 
133 /**
134  * ice_find_buf_table
135  * @ice_seg: pointer to the ice segment
136  *
137  * Returns the address of the buffer table within the ice segment.
138  */
139 static struct ice_buf_table *ice_find_buf_table(struct ice_seg *ice_seg)
140 {
141 	struct ice_nvm_table *nvms;
142 
143 	nvms = (struct ice_nvm_table *)
144 		(ice_seg->device_table +
145 		 LE32_TO_CPU(ice_seg->device_table_count));
146 
147 	return (_FORCE_ struct ice_buf_table *)
148 		(nvms->vers + LE32_TO_CPU(nvms->table_count));
149 }
150 
151 /**
152  * ice_pkg_enum_buf
153  * @ice_seg: pointer to the ice segment (or NULL on subsequent calls)
154  * @state: pointer to the enum state
155  *
156  * This function will enumerate all the buffers in the ice segment. The first
157  * call is made with the ice_seg parameter non-NULL; on subsequent calls,
158  * ice_seg is set to NULL which continues the enumeration. When the function
159  * returns a NULL pointer, then the end of the buffers has been reached, or an
160  * unexpected value has been detected (for example an invalid section count or
161  * an invalid buffer end value).
162  */
163 static struct ice_buf_hdr *
164 ice_pkg_enum_buf(struct ice_seg *ice_seg, struct ice_pkg_enum *state)
165 {
166 	if (ice_seg) {
167 		state->buf_table = ice_find_buf_table(ice_seg);
168 		if (!state->buf_table)
169 			return NULL;
170 
171 		state->buf_idx = 0;
172 		return ice_pkg_val_buf(state->buf_table->buf_array);
173 	}
174 
175 	if (++state->buf_idx < LE32_TO_CPU(state->buf_table->buf_count))
176 		return ice_pkg_val_buf(state->buf_table->buf_array +
177 				       state->buf_idx);
178 	else
179 		return NULL;
180 }
181 
182 /**
183  * ice_pkg_advance_sect
184  * @ice_seg: pointer to the ice segment (or NULL on subsequent calls)
185  * @state: pointer to the enum state
186  *
187  * This helper function will advance the section within the ice segment,
188  * also advancing the buffer if needed.
189  */
190 static bool
191 ice_pkg_advance_sect(struct ice_seg *ice_seg, struct ice_pkg_enum *state)
192 {
193 	if (!ice_seg && !state->buf)
194 		return false;
195 
196 	if (!ice_seg && state->buf)
197 		if (++state->sect_idx < LE16_TO_CPU(state->buf->section_count))
198 			return true;
199 
200 	state->buf = ice_pkg_enum_buf(ice_seg, state);
201 	if (!state->buf)
202 		return false;
203 
204 	/* start of new buffer, reset section index */
205 	state->sect_idx = 0;
206 	return true;
207 }
208 
209 /**
210  * ice_pkg_enum_section
211  * @ice_seg: pointer to the ice segment (or NULL on subsequent calls)
212  * @state: pointer to the enum state
213  * @sect_type: section type to enumerate
214  *
215  * This function will enumerate all the sections of a particular type in the
216  * ice segment. The first call is made with the ice_seg parameter non-NULL;
217  * on subsequent calls, ice_seg is set to NULL which continues the enumeration.
218  * When the function returns a NULL pointer, then the end of the matching
219  * sections has been reached.
220  */
221 static void *
222 ice_pkg_enum_section(struct ice_seg *ice_seg, struct ice_pkg_enum *state,
223 		     u32 sect_type)
224 {
225 	u16 offset, size;
226 
227 	if (ice_seg)
228 		state->type = sect_type;
229 
230 	if (!ice_pkg_advance_sect(ice_seg, state))
231 		return NULL;
232 
233 	/* scan for next matching section */
234 	while (state->buf->section_entry[state->sect_idx].type !=
235 	       CPU_TO_LE32(state->type))
236 		if (!ice_pkg_advance_sect(NULL, state))
237 			return NULL;
238 
239 	/* validate section */
240 	offset = LE16_TO_CPU(state->buf->section_entry[state->sect_idx].offset);
241 	if (offset < ICE_MIN_S_OFF || offset > ICE_MAX_S_OFF)
242 		return NULL;
243 
244 	size = LE16_TO_CPU(state->buf->section_entry[state->sect_idx].size);
245 	if (size < ICE_MIN_S_SZ || size > ICE_MAX_S_SZ)
246 		return NULL;
247 
248 	/* make sure the section fits in the buffer */
249 	if (offset + size > ICE_PKG_BUF_SIZE)
250 		return NULL;
251 
252 	state->sect_type =
253 		LE32_TO_CPU(state->buf->section_entry[state->sect_idx].type);
254 
255 	/* calc pointer to this section */
256 	state->sect = ((u8 *)state->buf) +
257 		LE16_TO_CPU(state->buf->section_entry[state->sect_idx].offset);
258 
259 	return state->sect;
260 }
261 
262 /**
263  * ice_pkg_enum_entry
264  * @ice_seg: pointer to the ice segment (or NULL on subsequent calls)
265  * @state: pointer to the enum state
266  * @sect_type: section type to enumerate
267  * @offset: pointer to variable that receives the offset in the table (optional)
268  * @handler: function that handles access to the entries into the section type
269  *
270  * This function will enumerate all the entries in particular section type in
271  * the ice segment. The first call is made with the ice_seg parameter non-NULL;
272  * on subsequent calls, ice_seg is set to NULL which continues the enumeration.
273  * When the function returns a NULL pointer, then the end of the entries has
274  * been reached.
275  *
276  * Since each section may have a different header and entry size, the handler
277  * function is needed to determine the number and location entries in each
278  * section.
279  *
280  * The offset parameter is optional, but should be used for sections that
281  * contain an offset for each section table. For such cases, the section handler
282  * function must return the appropriate offset + index to give the absolution
283  * offset for each entry. For example, if the base for a section's header
284  * indicates a base offset of 10, and the index for the entry is 2, then
285  * section handler function should set the offset to 10 + 2 = 12.
286  */
287 static void *
288 ice_pkg_enum_entry(struct ice_seg *ice_seg, struct ice_pkg_enum *state,
289 		   u32 sect_type, u32 *offset,
290 		   void *(*handler)(u32 sect_type, void *section,
291 				    u32 index, u32 *offset))
292 {
293 	void *entry;
294 
295 	if (ice_seg) {
296 		if (!handler)
297 			return NULL;
298 
299 		if (!ice_pkg_enum_section(ice_seg, state, sect_type))
300 			return NULL;
301 
302 		state->entry_idx = 0;
303 		state->handler = handler;
304 	} else {
305 		state->entry_idx++;
306 	}
307 
308 	if (!state->handler)
309 		return NULL;
310 
311 	/* get entry */
312 	entry = state->handler(state->sect_type, state->sect, state->entry_idx,
313 			       offset);
314 	if (!entry) {
315 		/* end of a section, look for another section of this type */
316 		if (!ice_pkg_enum_section(NULL, state, 0))
317 			return NULL;
318 
319 		state->entry_idx = 0;
320 		entry = state->handler(state->sect_type, state->sect,
321 				       state->entry_idx, offset);
322 	}
323 
324 	return entry;
325 }
326 
327 /**
328  * ice_hw_ptype_ena - check if the PTYPE is enabled or not
329  * @hw: pointer to the HW structure
330  * @ptype: the hardware PTYPE
331  */
332 bool ice_hw_ptype_ena(struct ice_hw *hw, u16 ptype)
333 {
334 	return ptype < ICE_FLOW_PTYPE_MAX &&
335 	       ice_is_bit_set(hw->hw_ptype, ptype);
336 }
337 
338 /**
339  * ice_marker_ptype_tcam_handler
340  * @sect_type: section type
341  * @section: pointer to section
342  * @index: index of the Marker PType TCAM entry to be returned
343  * @offset: pointer to receive absolute offset, always 0 for ptype TCAM sections
344  *
345  * This is a callback function that can be passed to ice_pkg_enum_entry.
346  * Handles enumeration of individual Marker PType TCAM entries.
347  */
348 static void *
349 ice_marker_ptype_tcam_handler(u32 sect_type, void *section, u32 index,
350 			      u32 *offset)
351 {
352 	struct ice_marker_ptype_tcam_section *marker_ptype;
353 
354 	if (!section)
355 		return NULL;
356 
357 	if (sect_type != ICE_SID_RXPARSER_MARKER_PTYPE)
358 		return NULL;
359 
360 	/* cppcheck-suppress nullPointer */
361 	if (index > ICE_MAX_MARKER_PTYPE_TCAMS_IN_BUF)
362 		return NULL;
363 
364 	if (offset)
365 		*offset = 0;
366 
367 	marker_ptype = (struct ice_marker_ptype_tcam_section *)section;
368 	if (index >= LE16_TO_CPU(marker_ptype->count))
369 		return NULL;
370 
371 	return marker_ptype->tcam + index;
372 }
373 
374 /**
375  * ice_fill_hw_ptype - fill the enabled PTYPE bit information
376  * @hw: pointer to the HW structure
377  */
378 static void
379 ice_fill_hw_ptype(struct ice_hw *hw)
380 {
381 	struct ice_marker_ptype_tcam_entry *tcam;
382 	struct ice_seg *seg = hw->seg;
383 	struct ice_pkg_enum state;
384 
385 	ice_zero_bitmap(hw->hw_ptype, ICE_FLOW_PTYPE_MAX);
386 	if (!seg)
387 		return;
388 
389 	ice_memset(&state, 0, sizeof(state), ICE_NONDMA_MEM);
390 
391 	do {
392 		tcam = (struct ice_marker_ptype_tcam_entry *)
393 			ice_pkg_enum_entry(seg, &state,
394 					   ICE_SID_RXPARSER_MARKER_PTYPE, NULL,
395 					   ice_marker_ptype_tcam_handler);
396 		if (tcam &&
397 		    LE16_TO_CPU(tcam->addr) < ICE_MARKER_PTYPE_TCAM_ADDR_MAX &&
398 		    LE16_TO_CPU(tcam->ptype) < ICE_FLOW_PTYPE_MAX)
399 			ice_set_bit(LE16_TO_CPU(tcam->ptype), hw->hw_ptype);
400 
401 		seg = NULL;
402 	} while (tcam);
403 }
404 
405 /**
406  * ice_boost_tcam_handler
407  * @sect_type: section type
408  * @section: pointer to section
409  * @index: index of the boost TCAM entry to be returned
410  * @offset: pointer to receive absolute offset, always 0 for boost TCAM sections
411  *
412  * This is a callback function that can be passed to ice_pkg_enum_entry.
413  * Handles enumeration of individual boost TCAM entries.
414  */
415 static void *
416 ice_boost_tcam_handler(u32 sect_type, void *section, u32 index, u32 *offset)
417 {
418 	struct ice_boost_tcam_section *boost;
419 
420 	if (!section)
421 		return NULL;
422 
423 	if (sect_type != ICE_SID_RXPARSER_BOOST_TCAM)
424 		return NULL;
425 
426 	if (index > ICE_MAX_BST_TCAMS_IN_BUF)
427 		return NULL;
428 
429 	if (offset)
430 		*offset = 0;
431 
432 	boost = (struct ice_boost_tcam_section *)section;
433 	if (index >= LE16_TO_CPU(boost->count))
434 		return NULL;
435 
436 	return boost->tcam + index;
437 }
438 
439 /**
440  * ice_find_boost_entry
441  * @ice_seg: pointer to the ice segment (non-NULL)
442  * @addr: Boost TCAM address of entry to search for
443  * @entry: returns pointer to the entry
444  *
445  * Finds a particular Boost TCAM entry and returns a pointer to that entry
446  * if it is found. The ice_seg parameter must not be NULL since the first call
447  * to ice_pkg_enum_entry requires a pointer to an actual ice_segment structure.
448  */
449 static enum ice_status
450 ice_find_boost_entry(struct ice_seg *ice_seg, u16 addr,
451 		     struct ice_boost_tcam_entry **entry)
452 {
453 	struct ice_boost_tcam_entry *tcam;
454 	struct ice_pkg_enum state;
455 
456 	ice_memset(&state, 0, sizeof(state), ICE_NONDMA_MEM);
457 
458 	if (!ice_seg)
459 		return ICE_ERR_PARAM;
460 
461 	do {
462 		tcam = (struct ice_boost_tcam_entry *)
463 		       ice_pkg_enum_entry(ice_seg, &state,
464 					  ICE_SID_RXPARSER_BOOST_TCAM, NULL,
465 					  ice_boost_tcam_handler);
466 		if (tcam && LE16_TO_CPU(tcam->addr) == addr) {
467 			*entry = tcam;
468 			return ICE_SUCCESS;
469 		}
470 
471 		ice_seg = NULL;
472 	} while (tcam);
473 
474 	*entry = NULL;
475 	return ICE_ERR_CFG;
476 }
477 
478 /**
479  * ice_label_enum_handler
480  * @sect_type: section type
481  * @section: pointer to section
482  * @index: index of the label entry to be returned
483  * @offset: pointer to receive absolute offset, always zero for label sections
484  *
485  * This is a callback function that can be passed to ice_pkg_enum_entry.
486  * Handles enumeration of individual label entries.
487  */
488 static void *
489 ice_label_enum_handler(u32 __ALWAYS_UNUSED sect_type, void *section, u32 index,
490 		       u32 *offset)
491 {
492 	struct ice_label_section *labels;
493 
494 	if (!section)
495 		return NULL;
496 
497 	if (index > ICE_MAX_LABELS_IN_BUF)
498 		return NULL;
499 
500 	if (offset)
501 		*offset = 0;
502 
503 	labels = (struct ice_label_section *)section;
504 	if (index >= LE16_TO_CPU(labels->count))
505 		return NULL;
506 
507 	return labels->label + index;
508 }
509 
510 /**
511  * ice_enum_labels
512  * @ice_seg: pointer to the ice segment (NULL on subsequent calls)
513  * @type: the section type that will contain the label (0 on subsequent calls)
514  * @state: ice_pkg_enum structure that will hold the state of the enumeration
515  * @value: pointer to a value that will return the label's value if found
516  *
517  * Enumerates a list of labels in the package. The caller will call
518  * ice_enum_labels(ice_seg, type, ...) to start the enumeration, then call
519  * ice_enum_labels(NULL, 0, ...) to continue. When the function returns a NULL
520  * the end of the list has been reached.
521  */
522 static char *
523 ice_enum_labels(struct ice_seg *ice_seg, u32 type, struct ice_pkg_enum *state,
524 		u16 *value)
525 {
526 	struct ice_label *label;
527 
528 	/* Check for valid label section on first call */
529 	if (type && !(type >= ICE_SID_LBL_FIRST && type <= ICE_SID_LBL_LAST))
530 		return NULL;
531 
532 	label = (struct ice_label *)ice_pkg_enum_entry(ice_seg, state, type,
533 						       NULL,
534 						       ice_label_enum_handler);
535 	if (!label)
536 		return NULL;
537 
538 	*value = LE16_TO_CPU(label->value);
539 	return label->name;
540 }
541 
542 /**
543  * ice_add_tunnel_hint
544  * @hw: pointer to the HW structure
545  * @label_name: label text
546  * @val: value of the tunnel port boost entry
547  */
548 static void ice_add_tunnel_hint(struct ice_hw *hw, char *label_name, u16 val)
549 {
550 	if (hw->tnl.count < ICE_TUNNEL_MAX_ENTRIES) {
551 		u16 i;
552 
553 		for (i = 0; tnls[i].type != TNL_LAST; i++) {
554 			size_t len = strlen(tnls[i].label_prefix);
555 
556 			/* Look for matching label start, before continuing */
557 			if (strncmp(label_name, tnls[i].label_prefix, len))
558 				continue;
559 
560 			/* Make sure this label matches our PF. Note that the PF
561 			 * character ('0' - '7') will be located where our
562 			 * prefix string's null terminator is located.
563 			 */
564 			if ((label_name[len] - '0') == hw->pf_id) {
565 				hw->tnl.tbl[hw->tnl.count].type = tnls[i].type;
566 				hw->tnl.tbl[hw->tnl.count].valid = false;
567 				hw->tnl.tbl[hw->tnl.count].in_use = false;
568 				hw->tnl.tbl[hw->tnl.count].marked = false;
569 				hw->tnl.tbl[hw->tnl.count].boost_addr = val;
570 				hw->tnl.tbl[hw->tnl.count].port = 0;
571 				hw->tnl.count++;
572 				break;
573 			}
574 		}
575 	}
576 }
577 
578 /**
579  * ice_add_dvm_hint
580  * @hw: pointer to the HW structure
581  * @val: value of the boost entry
582  * @enable: true if entry needs to be enabled, or false if needs to be disabled
583  */
584 static void ice_add_dvm_hint(struct ice_hw *hw, u16 val, bool enable)
585 {
586 	if (hw->dvm_upd.count < ICE_DVM_MAX_ENTRIES) {
587 		hw->dvm_upd.tbl[hw->dvm_upd.count].boost_addr = val;
588 		hw->dvm_upd.tbl[hw->dvm_upd.count].enable = enable;
589 		hw->dvm_upd.count++;
590 	}
591 }
592 
593 /**
594  * ice_init_pkg_hints
595  * @hw: pointer to the HW structure
596  * @ice_seg: pointer to the segment of the package scan (non-NULL)
597  *
598  * This function will scan the package and save off relevant information
599  * (hints or metadata) for driver use. The ice_seg parameter must not be NULL
600  * since the first call to ice_enum_labels requires a pointer to an actual
601  * ice_seg structure.
602  */
603 static void ice_init_pkg_hints(struct ice_hw *hw, struct ice_seg *ice_seg)
604 {
605 	struct ice_pkg_enum state;
606 	char *label_name;
607 	u16 val;
608 	int i;
609 
610 	ice_memset(&hw->tnl, 0, sizeof(hw->tnl), ICE_NONDMA_MEM);
611 	ice_memset(&state, 0, sizeof(state), ICE_NONDMA_MEM);
612 
613 	if (!ice_seg)
614 		return;
615 
616 	label_name = ice_enum_labels(ice_seg, ICE_SID_LBL_RXPARSER_TMEM, &state,
617 				     &val);
618 
619 	while (label_name) {
620 		if (!strncmp(label_name, ICE_TNL_PRE, strlen(ICE_TNL_PRE)))
621 			/* check for a tunnel entry */
622 			ice_add_tunnel_hint(hw, label_name, val);
623 
624 		/* check for a dvm mode entry */
625 		else if (!strncmp(label_name, ICE_DVM_PRE, strlen(ICE_DVM_PRE)))
626 			ice_add_dvm_hint(hw, val, true);
627 
628 		/* check for a svm mode entry */
629 		else if (!strncmp(label_name, ICE_SVM_PRE, strlen(ICE_SVM_PRE)))
630 			ice_add_dvm_hint(hw, val, false);
631 
632 		label_name = ice_enum_labels(NULL, 0, &state, &val);
633 	}
634 
635 	/* Cache the appropriate boost TCAM entry pointers for tunnels */
636 	for (i = 0; i < hw->tnl.count; i++) {
637 		ice_find_boost_entry(ice_seg, hw->tnl.tbl[i].boost_addr,
638 				     &hw->tnl.tbl[i].boost_entry);
639 		if (hw->tnl.tbl[i].boost_entry)
640 			hw->tnl.tbl[i].valid = true;
641 	}
642 
643 	/* Cache the appropriate boost TCAM entry pointers for DVM and SVM */
644 	for (i = 0; i < hw->dvm_upd.count; i++)
645 		ice_find_boost_entry(ice_seg, hw->dvm_upd.tbl[i].boost_addr,
646 				     &hw->dvm_upd.tbl[i].boost_entry);
647 }
648 
649 /* Key creation */
650 
651 #define ICE_DC_KEY	0x1	/* don't care */
652 #define ICE_DC_KEYINV	0x1
653 #define ICE_NM_KEY	0x0	/* never match */
654 #define ICE_NM_KEYINV	0x0
655 #define ICE_0_KEY	0x1	/* match 0 */
656 #define ICE_0_KEYINV	0x0
657 #define ICE_1_KEY	0x0	/* match 1 */
658 #define ICE_1_KEYINV	0x1
659 
660 /**
661  * ice_gen_key_word - generate 16-bits of a key/mask word
662  * @val: the value
663  * @valid: valid bits mask (change only the valid bits)
664  * @dont_care: don't care mask
665  * @nvr_mtch: never match mask
666  * @key: pointer to an array of where the resulting key portion
667  * @key_inv: pointer to an array of where the resulting key invert portion
668  *
669  * This function generates 16-bits from a 8-bit value, an 8-bit don't care mask
670  * and an 8-bit never match mask. The 16-bits of output are divided into 8 bits
671  * of key and 8 bits of key invert.
672  *
673  *     '0' =    b01, always match a 0 bit
674  *     '1' =    b10, always match a 1 bit
675  *     '?' =    b11, don't care bit (always matches)
676  *     '~' =    b00, never match bit
677  *
678  * Input:
679  *          val:         b0  1  0  1  0  1
680  *          dont_care:   b0  0  1  1  0  0
681  *          never_mtch:  b0  0  0  0  1  1
682  *          ------------------------------
683  * Result:  key:        b01 10 11 11 00 00
684  */
685 static enum ice_status
686 ice_gen_key_word(u8 val, u8 valid, u8 dont_care, u8 nvr_mtch, u8 *key,
687 		 u8 *key_inv)
688 {
689 	u8 in_key = *key, in_key_inv = *key_inv;
690 	u8 i;
691 
692 	/* 'dont_care' and 'nvr_mtch' masks cannot overlap */
693 	if ((dont_care ^ nvr_mtch) != (dont_care | nvr_mtch))
694 		return ICE_ERR_CFG;
695 
696 	*key = 0;
697 	*key_inv = 0;
698 
699 	/* encode the 8 bits into 8-bit key and 8-bit key invert */
700 	for (i = 0; i < 8; i++) {
701 		*key >>= 1;
702 		*key_inv >>= 1;
703 
704 		if (!(valid & 0x1)) { /* change only valid bits */
705 			*key |= (in_key & 0x1) << 7;
706 			*key_inv |= (in_key_inv & 0x1) << 7;
707 		} else if (dont_care & 0x1) { /* don't care bit */
708 			*key |= ICE_DC_KEY << 7;
709 			*key_inv |= ICE_DC_KEYINV << 7;
710 		} else if (nvr_mtch & 0x1) { /* never match bit */
711 			*key |= ICE_NM_KEY << 7;
712 			*key_inv |= ICE_NM_KEYINV << 7;
713 		} else if (val & 0x01) { /* exact 1 match */
714 			*key |= ICE_1_KEY << 7;
715 			*key_inv |= ICE_1_KEYINV << 7;
716 		} else { /* exact 0 match */
717 			*key |= ICE_0_KEY << 7;
718 			*key_inv |= ICE_0_KEYINV << 7;
719 		}
720 
721 		dont_care >>= 1;
722 		nvr_mtch >>= 1;
723 		valid >>= 1;
724 		val >>= 1;
725 		in_key >>= 1;
726 		in_key_inv >>= 1;
727 	}
728 
729 	return ICE_SUCCESS;
730 }
731 
732 /**
733  * ice_bits_max_set - determine if the number of bits set is within a maximum
734  * @mask: pointer to the byte array which is the mask
735  * @size: the number of bytes in the mask
736  * @max: the max number of set bits
737  *
738  * This function determines if there are at most 'max' number of bits set in an
739  * array. Returns true if the number for bits set is <= max or will return false
740  * otherwise.
741  */
742 static bool ice_bits_max_set(const u8 *mask, u16 size, u16 max)
743 {
744 	u16 count = 0;
745 	u16 i;
746 
747 	/* check each byte */
748 	for (i = 0; i < size; i++) {
749 		/* if 0, go to next byte */
750 		if (!mask[i])
751 			continue;
752 
753 		/* We know there is at least one set bit in this byte because of
754 		 * the above check; if we already have found 'max' number of
755 		 * bits set, then we can return failure now.
756 		 */
757 		if (count == max)
758 			return false;
759 
760 		/* count the bits in this byte, checking threshold */
761 		count += ice_hweight8(mask[i]);
762 		if (count > max)
763 			return false;
764 	}
765 
766 	return true;
767 }
768 
769 /**
770  * ice_set_key - generate a variable sized key with multiples of 16-bits
771  * @key: pointer to where the key will be stored
772  * @size: the size of the complete key in bytes (must be even)
773  * @val: array of 8-bit values that makes up the value portion of the key
774  * @upd: array of 8-bit masks that determine what key portion to update
775  * @dc: array of 8-bit masks that make up the don't care mask
776  * @nm: array of 8-bit masks that make up the never match mask
777  * @off: the offset of the first byte in the key to update
778  * @len: the number of bytes in the key update
779  *
780  * This function generates a key from a value, a don't care mask and a never
781  * match mask.
782  * upd, dc, and nm are optional parameters, and can be NULL:
783  *	upd == NULL --> upd mask is all 1's (update all bits)
784  *	dc == NULL --> dc mask is all 0's (no don't care bits)
785  *	nm == NULL --> nm mask is all 0's (no never match bits)
786  */
787 enum ice_status
788 ice_set_key(u8 *key, u16 size, u8 *val, u8 *upd, u8 *dc, u8 *nm, u16 off,
789 	    u16 len)
790 {
791 	u16 half_size;
792 	u16 i;
793 
794 	/* size must be a multiple of 2 bytes. */
795 	if (size % 2)
796 		return ICE_ERR_CFG;
797 	half_size = size / 2;
798 
799 	if (off + len > half_size)
800 		return ICE_ERR_CFG;
801 
802 	/* Make sure at most one bit is set in the never match mask. Having more
803 	 * than one never match mask bit set will cause HW to consume excessive
804 	 * power otherwise; this is a power management efficiency check.
805 	 */
806 #define ICE_NVR_MTCH_BITS_MAX	1
807 	if (nm && !ice_bits_max_set(nm, len, ICE_NVR_MTCH_BITS_MAX))
808 		return ICE_ERR_CFG;
809 
810 	for (i = 0; i < len; i++)
811 		if (ice_gen_key_word(val[i], upd ? upd[i] : 0xff,
812 				     dc ? dc[i] : 0, nm ? nm[i] : 0,
813 				     key + off + i, key + half_size + off + i))
814 			return ICE_ERR_CFG;
815 
816 	return ICE_SUCCESS;
817 }
818 
819 /**
820  * ice_acquire_global_cfg_lock
821  * @hw: pointer to the HW structure
822  * @access: access type (read or write)
823  *
824  * This function will request ownership of the global config lock for reading
825  * or writing of the package. When attempting to obtain write access, the
826  * caller must check for the following two return values:
827  *
828  * ICE_SUCCESS        - Means the caller has acquired the global config lock
829  *                      and can perform writing of the package.
830  * ICE_ERR_AQ_NO_WORK - Indicates another driver has already written the
831  *                      package or has found that no update was necessary; in
832  *                      this case, the caller can just skip performing any
833  *                      update of the package.
834  */
835 static enum ice_status
836 ice_acquire_global_cfg_lock(struct ice_hw *hw,
837 			    enum ice_aq_res_access_type access)
838 {
839 	enum ice_status status;
840 
841 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
842 
843 	status = ice_acquire_res(hw, ICE_GLOBAL_CFG_LOCK_RES_ID, access,
844 				 ICE_GLOBAL_CFG_LOCK_TIMEOUT);
845 
846 	if (status == ICE_ERR_AQ_NO_WORK)
847 		ice_debug(hw, ICE_DBG_PKG, "Global config lock: No work to do\n");
848 
849 	return status;
850 }
851 
852 /**
853  * ice_release_global_cfg_lock
854  * @hw: pointer to the HW structure
855  *
856  * This function will release the global config lock.
857  */
858 static void ice_release_global_cfg_lock(struct ice_hw *hw)
859 {
860 	ice_release_res(hw, ICE_GLOBAL_CFG_LOCK_RES_ID);
861 }
862 
863 /**
864  * ice_acquire_change_lock
865  * @hw: pointer to the HW structure
866  * @access: access type (read or write)
867  *
868  * This function will request ownership of the change lock.
869  */
870 enum ice_status
871 ice_acquire_change_lock(struct ice_hw *hw, enum ice_aq_res_access_type access)
872 {
873 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
874 
875 	return ice_acquire_res(hw, ICE_CHANGE_LOCK_RES_ID, access,
876 			       ICE_CHANGE_LOCK_TIMEOUT);
877 }
878 
879 /**
880  * ice_release_change_lock
881  * @hw: pointer to the HW structure
882  *
883  * This function will release the change lock using the proper Admin Command.
884  */
885 void ice_release_change_lock(struct ice_hw *hw)
886 {
887 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
888 
889 	ice_release_res(hw, ICE_CHANGE_LOCK_RES_ID);
890 }
891 
892 /**
893  * ice_aq_download_pkg
894  * @hw: pointer to the hardware structure
895  * @pkg_buf: the package buffer to transfer
896  * @buf_size: the size of the package buffer
897  * @last_buf: last buffer indicator
898  * @error_offset: returns error offset
899  * @error_info: returns error information
900  * @cd: pointer to command details structure or NULL
901  *
902  * Download Package (0x0C40)
903  */
904 static enum ice_status
905 ice_aq_download_pkg(struct ice_hw *hw, struct ice_buf_hdr *pkg_buf,
906 		    u16 buf_size, bool last_buf, u32 *error_offset,
907 		    u32 *error_info, struct ice_sq_cd *cd)
908 {
909 	struct ice_aqc_download_pkg *cmd;
910 	struct ice_aq_desc desc;
911 	enum ice_status status;
912 
913 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
914 
915 	if (error_offset)
916 		*error_offset = 0;
917 	if (error_info)
918 		*error_info = 0;
919 
920 	cmd = &desc.params.download_pkg;
921 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_download_pkg);
922 	desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
923 
924 	if (last_buf)
925 		cmd->flags |= ICE_AQC_DOWNLOAD_PKG_LAST_BUF;
926 
927 	status = ice_aq_send_cmd(hw, &desc, pkg_buf, buf_size, cd);
928 	if (status == ICE_ERR_AQ_ERROR) {
929 		/* Read error from buffer only when the FW returned an error */
930 		struct ice_aqc_download_pkg_resp *resp;
931 
932 		resp = (struct ice_aqc_download_pkg_resp *)pkg_buf;
933 		if (error_offset)
934 			*error_offset = LE32_TO_CPU(resp->error_offset);
935 		if (error_info)
936 			*error_info = LE32_TO_CPU(resp->error_info);
937 	}
938 
939 	return status;
940 }
941 
942 /**
943  * ice_aq_upload_section
944  * @hw: pointer to the hardware structure
945  * @pkg_buf: the package buffer which will receive the section
946  * @buf_size: the size of the package buffer
947  * @cd: pointer to command details structure or NULL
948  *
949  * Upload Section (0x0C41)
950  */
951 enum ice_status
952 ice_aq_upload_section(struct ice_hw *hw, struct ice_buf_hdr *pkg_buf,
953 		      u16 buf_size, struct ice_sq_cd *cd)
954 {
955 	struct ice_aq_desc desc;
956 
957 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
958 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_upload_section);
959 	desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
960 
961 	return ice_aq_send_cmd(hw, &desc, pkg_buf, buf_size, cd);
962 }
963 
964 /**
965  * ice_aq_update_pkg
966  * @hw: pointer to the hardware structure
967  * @pkg_buf: the package cmd buffer
968  * @buf_size: the size of the package cmd buffer
969  * @last_buf: last buffer indicator
970  * @error_offset: returns error offset
971  * @error_info: returns error information
972  * @cd: pointer to command details structure or NULL
973  *
974  * Update Package (0x0C42)
975  */
976 static enum ice_status
977 ice_aq_update_pkg(struct ice_hw *hw, struct ice_buf_hdr *pkg_buf, u16 buf_size,
978 		  bool last_buf, u32 *error_offset, u32 *error_info,
979 		  struct ice_sq_cd *cd)
980 {
981 	struct ice_aqc_download_pkg *cmd;
982 	struct ice_aq_desc desc;
983 	enum ice_status status;
984 
985 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
986 
987 	if (error_offset)
988 		*error_offset = 0;
989 	if (error_info)
990 		*error_info = 0;
991 
992 	cmd = &desc.params.download_pkg;
993 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_update_pkg);
994 	desc.flags |= CPU_TO_LE16(ICE_AQ_FLAG_RD);
995 
996 	if (last_buf)
997 		cmd->flags |= ICE_AQC_DOWNLOAD_PKG_LAST_BUF;
998 
999 	status = ice_aq_send_cmd(hw, &desc, pkg_buf, buf_size, cd);
1000 	if (status == ICE_ERR_AQ_ERROR) {
1001 		/* Read error from buffer only when the FW returned an error */
1002 		struct ice_aqc_download_pkg_resp *resp;
1003 
1004 		resp = (struct ice_aqc_download_pkg_resp *)pkg_buf;
1005 		if (error_offset)
1006 			*error_offset = LE32_TO_CPU(resp->error_offset);
1007 		if (error_info)
1008 			*error_info = LE32_TO_CPU(resp->error_info);
1009 	}
1010 
1011 	return status;
1012 }
1013 
1014 /**
1015  * ice_find_seg_in_pkg
1016  * @hw: pointer to the hardware structure
1017  * @seg_type: the segment type to search for (i.e., SEGMENT_TYPE_CPK)
1018  * @pkg_hdr: pointer to the package header to be searched
1019  *
1020  * This function searches a package file for a particular segment type. On
1021  * success it returns a pointer to the segment header, otherwise it will
1022  * return NULL.
1023  */
1024 static struct ice_generic_seg_hdr *
1025 ice_find_seg_in_pkg(struct ice_hw *hw, u32 seg_type,
1026 		    struct ice_pkg_hdr *pkg_hdr)
1027 {
1028 	u32 i;
1029 
1030 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1031 	ice_debug(hw, ICE_DBG_PKG, "Package format version: %d.%d.%d.%d\n",
1032 		  pkg_hdr->pkg_format_ver.major, pkg_hdr->pkg_format_ver.minor,
1033 		  pkg_hdr->pkg_format_ver.update,
1034 		  pkg_hdr->pkg_format_ver.draft);
1035 
1036 	/* Search all package segments for the requested segment type */
1037 	for (i = 0; i < LE32_TO_CPU(pkg_hdr->seg_count); i++) {
1038 		struct ice_generic_seg_hdr *seg;
1039 
1040 		seg = (struct ice_generic_seg_hdr *)
1041 			((u8 *)pkg_hdr + LE32_TO_CPU(pkg_hdr->seg_offset[i]));
1042 
1043 		if (LE32_TO_CPU(seg->seg_type) == seg_type)
1044 			return seg;
1045 	}
1046 
1047 	return NULL;
1048 }
1049 
1050 /**
1051  * ice_update_pkg_no_lock
1052  * @hw: pointer to the hardware structure
1053  * @bufs: pointer to an array of buffers
1054  * @count: the number of buffers in the array
1055  */
1056 static enum ice_status
1057 ice_update_pkg_no_lock(struct ice_hw *hw, struct ice_buf *bufs, u32 count)
1058 {
1059 	enum ice_status status = ICE_SUCCESS;
1060 	u32 i;
1061 
1062 	for (i = 0; i < count; i++) {
1063 		struct ice_buf_hdr *bh = (struct ice_buf_hdr *)(bufs + i);
1064 		bool last = ((i + 1) == count);
1065 		u32 offset, info;
1066 
1067 		status = ice_aq_update_pkg(hw, bh, LE16_TO_CPU(bh->data_end),
1068 					   last, &offset, &info, NULL);
1069 
1070 		if (status) {
1071 			ice_debug(hw, ICE_DBG_PKG, "Update pkg failed: err %d off %d inf %d\n",
1072 				  status, offset, info);
1073 			break;
1074 		}
1075 	}
1076 
1077 	return status;
1078 }
1079 
1080 /**
1081  * ice_update_pkg
1082  * @hw: pointer to the hardware structure
1083  * @bufs: pointer to an array of buffers
1084  * @count: the number of buffers in the array
1085  *
1086  * Obtains change lock and updates package.
1087  */
1088 enum ice_status
1089 ice_update_pkg(struct ice_hw *hw, struct ice_buf *bufs, u32 count)
1090 {
1091 	enum ice_status status;
1092 
1093 	status = ice_acquire_change_lock(hw, ICE_RES_WRITE);
1094 	if (status)
1095 		return status;
1096 
1097 	status = ice_update_pkg_no_lock(hw, bufs, count);
1098 
1099 	ice_release_change_lock(hw);
1100 
1101 	return status;
1102 }
1103 
1104 /**
1105  * ice_dwnld_cfg_bufs
1106  * @hw: pointer to the hardware structure
1107  * @bufs: pointer to an array of buffers
1108  * @count: the number of buffers in the array
1109  *
1110  * Obtains global config lock and downloads the package configuration buffers
1111  * to the firmware. Metadata buffers are skipped, and the first metadata buffer
1112  * found indicates that the rest of the buffers are all metadata buffers.
1113  */
1114 static enum ice_status
1115 ice_dwnld_cfg_bufs(struct ice_hw *hw, struct ice_buf *bufs, u32 count)
1116 {
1117 	enum ice_status status;
1118 	struct ice_buf_hdr *bh;
1119 	u32 offset, info, i;
1120 
1121 	if (!bufs || !count)
1122 		return ICE_ERR_PARAM;
1123 
1124 	/* If the first buffer's first section has its metadata bit set
1125 	 * then there are no buffers to be downloaded, and the operation is
1126 	 * considered a success.
1127 	 */
1128 	bh = (struct ice_buf_hdr *)bufs;
1129 	if (LE32_TO_CPU(bh->section_entry[0].type) & ICE_METADATA_BUF)
1130 		return ICE_SUCCESS;
1131 
1132 	/* reset pkg_dwnld_status in case this function is called in the
1133 	 * reset/rebuild flow
1134 	 */
1135 	hw->pkg_dwnld_status = ICE_AQ_RC_OK;
1136 
1137 	status = ice_acquire_global_cfg_lock(hw, ICE_RES_WRITE);
1138 	if (status) {
1139 		if (status == ICE_ERR_AQ_NO_WORK)
1140 			hw->pkg_dwnld_status = ICE_AQ_RC_EEXIST;
1141 		else
1142 			hw->pkg_dwnld_status = hw->adminq.sq_last_status;
1143 		return status;
1144 	}
1145 
1146 	for (i = 0; i < count; i++) {
1147 		bool last = ((i + 1) == count);
1148 
1149 		if (!last) {
1150 			/* check next buffer for metadata flag */
1151 			bh = (struct ice_buf_hdr *)(bufs + i + 1);
1152 
1153 			/* A set metadata flag in the next buffer will signal
1154 			 * that the current buffer will be the last buffer
1155 			 * downloaded
1156 			 */
1157 			if (LE16_TO_CPU(bh->section_count))
1158 				if (LE32_TO_CPU(bh->section_entry[0].type) &
1159 				    ICE_METADATA_BUF)
1160 					last = true;
1161 		}
1162 
1163 		bh = (struct ice_buf_hdr *)(bufs + i);
1164 
1165 		status = ice_aq_download_pkg(hw, bh, ICE_PKG_BUF_SIZE, last,
1166 					     &offset, &info, NULL);
1167 
1168 		/* Save AQ status from download package */
1169 		hw->pkg_dwnld_status = hw->adminq.sq_last_status;
1170 		if (status) {
1171 			ice_debug(hw, ICE_DBG_PKG, "Pkg download failed: err %d off %d inf %d\n",
1172 				  status, offset, info);
1173 			break;
1174 		}
1175 
1176 		if (last)
1177 			break;
1178 	}
1179 
1180 	if (!status) {
1181 		status = ice_set_vlan_mode(hw);
1182 		if (status)
1183 			ice_debug(hw, ICE_DBG_PKG, "Failed to set VLAN mode: err %d\n",
1184 				  status);
1185 	}
1186 
1187 	ice_release_global_cfg_lock(hw);
1188 
1189 	return status;
1190 }
1191 
1192 /**
1193  * ice_aq_get_pkg_info_list
1194  * @hw: pointer to the hardware structure
1195  * @pkg_info: the buffer which will receive the information list
1196  * @buf_size: the size of the pkg_info information buffer
1197  * @cd: pointer to command details structure or NULL
1198  *
1199  * Get Package Info List (0x0C43)
1200  */
1201 static enum ice_status
1202 ice_aq_get_pkg_info_list(struct ice_hw *hw,
1203 			 struct ice_aqc_get_pkg_info_resp *pkg_info,
1204 			 u16 buf_size, struct ice_sq_cd *cd)
1205 {
1206 	struct ice_aq_desc desc;
1207 
1208 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1209 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_pkg_info_list);
1210 
1211 	return ice_aq_send_cmd(hw, &desc, pkg_info, buf_size, cd);
1212 }
1213 
1214 /**
1215  * ice_download_pkg
1216  * @hw: pointer to the hardware structure
1217  * @ice_seg: pointer to the segment of the package to be downloaded
1218  *
1219  * Handles the download of a complete package.
1220  */
1221 static enum ice_status
1222 ice_download_pkg(struct ice_hw *hw, struct ice_seg *ice_seg)
1223 {
1224 	struct ice_buf_table *ice_buf_tbl;
1225 	enum ice_status status;
1226 
1227 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1228 	ice_debug(hw, ICE_DBG_PKG, "Segment format version: %d.%d.%d.%d\n",
1229 		  ice_seg->hdr.seg_format_ver.major,
1230 		  ice_seg->hdr.seg_format_ver.minor,
1231 		  ice_seg->hdr.seg_format_ver.update,
1232 		  ice_seg->hdr.seg_format_ver.draft);
1233 
1234 	ice_debug(hw, ICE_DBG_PKG, "Seg: type 0x%X, size %d, name %s\n",
1235 		  LE32_TO_CPU(ice_seg->hdr.seg_type),
1236 		  LE32_TO_CPU(ice_seg->hdr.seg_size), ice_seg->hdr.seg_id);
1237 
1238 	ice_buf_tbl = ice_find_buf_table(ice_seg);
1239 
1240 	ice_debug(hw, ICE_DBG_PKG, "Seg buf count: %d\n",
1241 		  LE32_TO_CPU(ice_buf_tbl->buf_count));
1242 
1243 	status = ice_dwnld_cfg_bufs(hw, ice_buf_tbl->buf_array,
1244 				    LE32_TO_CPU(ice_buf_tbl->buf_count));
1245 
1246 	ice_cache_vlan_mode(hw);
1247 
1248 	return status;
1249 }
1250 
1251 /**
1252  * ice_init_pkg_info
1253  * @hw: pointer to the hardware structure
1254  * @pkg_hdr: pointer to the driver's package hdr
1255  *
1256  * Saves off the package details into the HW structure.
1257  */
1258 static enum ice_status
1259 ice_init_pkg_info(struct ice_hw *hw, struct ice_pkg_hdr *pkg_hdr)
1260 {
1261 	struct ice_generic_seg_hdr *seg_hdr;
1262 
1263 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1264 	if (!pkg_hdr)
1265 		return ICE_ERR_PARAM;
1266 
1267 	seg_hdr = (struct ice_generic_seg_hdr *)
1268 		ice_find_seg_in_pkg(hw, SEGMENT_TYPE_ICE, pkg_hdr);
1269 	if (seg_hdr) {
1270 		struct ice_meta_sect *meta;
1271 		struct ice_pkg_enum state;
1272 
1273 		ice_memset(&state, 0, sizeof(state), ICE_NONDMA_MEM);
1274 
1275 		/* Get package information from the Metadata Section */
1276 		meta = (struct ice_meta_sect *)
1277 			ice_pkg_enum_section((struct ice_seg *)seg_hdr, &state,
1278 					     ICE_SID_METADATA);
1279 		if (!meta) {
1280 			ice_debug(hw, ICE_DBG_INIT, "Did not find ice metadata section in package\n");
1281 			return ICE_ERR_CFG;
1282 		}
1283 
1284 		hw->pkg_ver = meta->ver;
1285 		ice_memcpy(hw->pkg_name, meta->name, sizeof(meta->name),
1286 			   ICE_NONDMA_TO_NONDMA);
1287 
1288 		ice_debug(hw, ICE_DBG_PKG, "Pkg: %d.%d.%d.%d, %s\n",
1289 			  meta->ver.major, meta->ver.minor, meta->ver.update,
1290 			  meta->ver.draft, meta->name);
1291 
1292 		hw->ice_seg_fmt_ver = seg_hdr->seg_format_ver;
1293 		ice_memcpy(hw->ice_seg_id, seg_hdr->seg_id,
1294 			   sizeof(hw->ice_seg_id), ICE_NONDMA_TO_NONDMA);
1295 
1296 		ice_debug(hw, ICE_DBG_PKG, "Ice Seg: %d.%d.%d.%d, %s\n",
1297 			  seg_hdr->seg_format_ver.major,
1298 			  seg_hdr->seg_format_ver.minor,
1299 			  seg_hdr->seg_format_ver.update,
1300 			  seg_hdr->seg_format_ver.draft,
1301 			  seg_hdr->seg_id);
1302 	} else {
1303 		ice_debug(hw, ICE_DBG_INIT, "Did not find ice segment in driver package\n");
1304 		return ICE_ERR_CFG;
1305 	}
1306 
1307 	return ICE_SUCCESS;
1308 }
1309 
1310 /**
1311  * ice_get_pkg_info
1312  * @hw: pointer to the hardware structure
1313  *
1314  * Store details of the package currently loaded in HW into the HW structure.
1315  */
1316 static enum ice_status ice_get_pkg_info(struct ice_hw *hw)
1317 {
1318 	struct ice_aqc_get_pkg_info_resp *pkg_info;
1319 	enum ice_status status;
1320 	u16 size;
1321 	u32 i;
1322 
1323 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1324 
1325 	size = ice_struct_size(pkg_info, pkg_info, ICE_PKG_CNT);
1326 	pkg_info = (struct ice_aqc_get_pkg_info_resp *)ice_malloc(hw, size);
1327 	if (!pkg_info)
1328 		return ICE_ERR_NO_MEMORY;
1329 
1330 	status = ice_aq_get_pkg_info_list(hw, pkg_info, size, NULL);
1331 	if (status)
1332 		goto init_pkg_free_alloc;
1333 
1334 	for (i = 0; i < LE32_TO_CPU(pkg_info->count); i++) {
1335 #define ICE_PKG_FLAG_COUNT	4
1336 		char flags[ICE_PKG_FLAG_COUNT + 1] = { 0 };
1337 		u8 place = 0;
1338 
1339 		if (pkg_info->pkg_info[i].is_active) {
1340 			flags[place++] = 'A';
1341 			hw->active_pkg_ver = pkg_info->pkg_info[i].ver;
1342 			hw->active_track_id =
1343 				LE32_TO_CPU(pkg_info->pkg_info[i].track_id);
1344 			ice_memcpy(hw->active_pkg_name,
1345 				   pkg_info->pkg_info[i].name,
1346 				   sizeof(pkg_info->pkg_info[i].name),
1347 				   ICE_NONDMA_TO_NONDMA);
1348 			hw->active_pkg_in_nvm = pkg_info->pkg_info[i].is_in_nvm;
1349 		}
1350 		if (pkg_info->pkg_info[i].is_active_at_boot)
1351 			flags[place++] = 'B';
1352 		if (pkg_info->pkg_info[i].is_modified)
1353 			flags[place++] = 'M';
1354 		if (pkg_info->pkg_info[i].is_in_nvm)
1355 			flags[place++] = 'N';
1356 
1357 		ice_debug(hw, ICE_DBG_PKG, "Pkg[%d]: %d.%d.%d.%d,%s,%s\n",
1358 			  i, pkg_info->pkg_info[i].ver.major,
1359 			  pkg_info->pkg_info[i].ver.minor,
1360 			  pkg_info->pkg_info[i].ver.update,
1361 			  pkg_info->pkg_info[i].ver.draft,
1362 			  pkg_info->pkg_info[i].name, flags);
1363 	}
1364 
1365 init_pkg_free_alloc:
1366 	ice_free(hw, pkg_info);
1367 
1368 	return status;
1369 }
1370 
1371 /**
1372  * ice_verify_pkg - verify package
1373  * @pkg: pointer to the package buffer
1374  * @len: size of the package buffer
1375  *
1376  * Verifies various attributes of the package file, including length, format
1377  * version, and the requirement of at least one segment.
1378  */
1379 static enum ice_status ice_verify_pkg(struct ice_pkg_hdr *pkg, u32 len)
1380 {
1381 	u32 seg_count;
1382 	u32 i;
1383 
1384 	if (len < ice_struct_size(pkg, seg_offset, 1))
1385 		return ICE_ERR_BUF_TOO_SHORT;
1386 
1387 	if (pkg->pkg_format_ver.major != ICE_PKG_FMT_VER_MAJ ||
1388 	    pkg->pkg_format_ver.minor != ICE_PKG_FMT_VER_MNR ||
1389 	    pkg->pkg_format_ver.update != ICE_PKG_FMT_VER_UPD ||
1390 	    pkg->pkg_format_ver.draft != ICE_PKG_FMT_VER_DFT)
1391 		return ICE_ERR_CFG;
1392 
1393 	/* pkg must have at least one segment */
1394 	seg_count = LE32_TO_CPU(pkg->seg_count);
1395 	if (seg_count < 1)
1396 		return ICE_ERR_CFG;
1397 
1398 	/* make sure segment array fits in package length */
1399 	if (len < ice_struct_size(pkg, seg_offset, seg_count))
1400 		return ICE_ERR_BUF_TOO_SHORT;
1401 
1402 	/* all segments must fit within length */
1403 	for (i = 0; i < seg_count; i++) {
1404 		u32 off = LE32_TO_CPU(pkg->seg_offset[i]);
1405 		struct ice_generic_seg_hdr *seg;
1406 
1407 		/* segment header must fit */
1408 		if (len < off + sizeof(*seg))
1409 			return ICE_ERR_BUF_TOO_SHORT;
1410 
1411 		seg = (struct ice_generic_seg_hdr *)((u8 *)pkg + off);
1412 
1413 		/* segment body must fit */
1414 		if (len < off + LE32_TO_CPU(seg->seg_size))
1415 			return ICE_ERR_BUF_TOO_SHORT;
1416 	}
1417 
1418 	return ICE_SUCCESS;
1419 }
1420 
1421 /**
1422  * ice_free_seg - free package segment pointer
1423  * @hw: pointer to the hardware structure
1424  *
1425  * Frees the package segment pointer in the proper manner, depending on if the
1426  * segment was allocated or just the passed in pointer was stored.
1427  */
1428 void ice_free_seg(struct ice_hw *hw)
1429 {
1430 	if (hw->pkg_copy) {
1431 		ice_free(hw, hw->pkg_copy);
1432 		hw->pkg_copy = NULL;
1433 		hw->pkg_size = 0;
1434 	}
1435 	hw->seg = NULL;
1436 }
1437 
1438 /**
1439  * ice_init_pkg_regs - initialize additional package registers
1440  * @hw: pointer to the hardware structure
1441  */
1442 static void ice_init_pkg_regs(struct ice_hw *hw)
1443 {
1444 #define ICE_SW_BLK_INP_MASK_L 0xFFFFFFFF
1445 #define ICE_SW_BLK_INP_MASK_H 0x0000FFFF
1446 #define ICE_SW_BLK_IDX	0
1447 	if (hw->dcf_enabled)
1448 		return;
1449 
1450 	/* setup Switch block input mask, which is 48-bits in two parts */
1451 	wr32(hw, GL_PREEXT_L2_PMASK0(ICE_SW_BLK_IDX), ICE_SW_BLK_INP_MASK_L);
1452 	wr32(hw, GL_PREEXT_L2_PMASK1(ICE_SW_BLK_IDX), ICE_SW_BLK_INP_MASK_H);
1453 }
1454 
1455 /**
1456  * ice_chk_pkg_version - check package version for compatibility with driver
1457  * @pkg_ver: pointer to a version structure to check
1458  *
1459  * Check to make sure that the package about to be downloaded is compatible with
1460  * the driver. To be compatible, the major and minor components of the package
1461  * version must match our ICE_PKG_SUPP_VER_MAJ and ICE_PKG_SUPP_VER_MNR
1462  * definitions.
1463  */
1464 static enum ice_status ice_chk_pkg_version(struct ice_pkg_ver *pkg_ver)
1465 {
1466 	if (pkg_ver->major != ICE_PKG_SUPP_VER_MAJ ||
1467 	    pkg_ver->minor != ICE_PKG_SUPP_VER_MNR)
1468 		return ICE_ERR_NOT_SUPPORTED;
1469 
1470 	return ICE_SUCCESS;
1471 }
1472 
1473 /**
1474  * ice_chk_pkg_compat
1475  * @hw: pointer to the hardware structure
1476  * @ospkg: pointer to the package hdr
1477  * @seg: pointer to the package segment hdr
1478  *
1479  * This function checks the package version compatibility with driver and NVM
1480  */
1481 static enum ice_status
1482 ice_chk_pkg_compat(struct ice_hw *hw, struct ice_pkg_hdr *ospkg,
1483 		   struct ice_seg **seg)
1484 {
1485 	struct ice_aqc_get_pkg_info_resp *pkg;
1486 	enum ice_status status;
1487 	u16 size;
1488 	u32 i;
1489 
1490 	ice_debug(hw, ICE_DBG_TRACE, "%s\n", __func__);
1491 
1492 	/* Check package version compatibility */
1493 	status = ice_chk_pkg_version(&hw->pkg_ver);
1494 	if (status) {
1495 		ice_debug(hw, ICE_DBG_INIT, "Package version check failed.\n");
1496 		return status;
1497 	}
1498 
1499 	/* find ICE segment in given package */
1500 	*seg = (struct ice_seg *)ice_find_seg_in_pkg(hw, SEGMENT_TYPE_ICE,
1501 						     ospkg);
1502 	if (!*seg) {
1503 		ice_debug(hw, ICE_DBG_INIT, "no ice segment in package.\n");
1504 		return ICE_ERR_CFG;
1505 	}
1506 
1507 	/* Check if FW is compatible with the OS package */
1508 	size = ice_struct_size(pkg, pkg_info, ICE_PKG_CNT);
1509 	pkg = (struct ice_aqc_get_pkg_info_resp *)ice_malloc(hw, size);
1510 	if (!pkg)
1511 		return ICE_ERR_NO_MEMORY;
1512 
1513 	status = ice_aq_get_pkg_info_list(hw, pkg, size, NULL);
1514 	if (status)
1515 		goto fw_ddp_compat_free_alloc;
1516 
1517 	for (i = 0; i < LE32_TO_CPU(pkg->count); i++) {
1518 		/* loop till we find the NVM package */
1519 		if (!pkg->pkg_info[i].is_in_nvm)
1520 			continue;
1521 		if ((*seg)->hdr.seg_format_ver.major !=
1522 			pkg->pkg_info[i].ver.major ||
1523 		    (*seg)->hdr.seg_format_ver.minor >
1524 			pkg->pkg_info[i].ver.minor) {
1525 			status = ICE_ERR_FW_DDP_MISMATCH;
1526 			ice_debug(hw, ICE_DBG_INIT, "OS package is not compatible with NVM.\n");
1527 		}
1528 		/* done processing NVM package so break */
1529 		break;
1530 	}
1531 fw_ddp_compat_free_alloc:
1532 	ice_free(hw, pkg);
1533 	return status;
1534 }
1535 
1536 /**
1537  * ice_sw_fv_handler
1538  * @sect_type: section type
1539  * @section: pointer to section
1540  * @index: index of the field vector entry to be returned
1541  * @offset: ptr to variable that receives the offset in the field vector table
1542  *
1543  * This is a callback function that can be passed to ice_pkg_enum_entry.
1544  * This function treats the given section as of type ice_sw_fv_section and
1545  * enumerates offset field. "offset" is an index into the field vector table.
1546  */
1547 static void *
1548 ice_sw_fv_handler(u32 sect_type, void *section, u32 index, u32 *offset)
1549 {
1550 	struct ice_sw_fv_section *fv_section =
1551 		(struct ice_sw_fv_section *)section;
1552 
1553 	if (!section || sect_type != ICE_SID_FLD_VEC_SW)
1554 		return NULL;
1555 	if (index >= LE16_TO_CPU(fv_section->count))
1556 		return NULL;
1557 	if (offset)
1558 		/* "index" passed in to this function is relative to a given
1559 		 * 4k block. To get to the true index into the field vector
1560 		 * table need to add the relative index to the base_offset
1561 		 * field of this section
1562 		 */
1563 		*offset = LE16_TO_CPU(fv_section->base_offset) + index;
1564 	return fv_section->fv + index;
1565 }
1566 
1567 /**
1568  * ice_get_prof_index_max - get the max profile index for used profile
1569  * @hw: pointer to the HW struct
1570  *
1571  * Calling this function will get the max profile index for used profile
1572  * and store the index number in struct ice_switch_info *switch_info
1573  * in hw for following use.
1574  */
1575 static int ice_get_prof_index_max(struct ice_hw *hw)
1576 {
1577 	u16 prof_index = 0, j, max_prof_index = 0;
1578 	struct ice_pkg_enum state;
1579 	struct ice_seg *ice_seg;
1580 	bool flag = false;
1581 	struct ice_fv *fv;
1582 	u32 offset;
1583 
1584 	ice_memset(&state, 0, sizeof(state), ICE_NONDMA_MEM);
1585 
1586 	if (!hw->seg)
1587 		return ICE_ERR_PARAM;
1588 
1589 	ice_seg = hw->seg;
1590 
1591 	do {
1592 		fv = (struct ice_fv *)
1593 			ice_pkg_enum_entry(ice_seg, &state, ICE_SID_FLD_VEC_SW,
1594 					   &offset, ice_sw_fv_handler);
1595 		if (!fv)
1596 			break;
1597 		ice_seg = NULL;
1598 
1599 		/* in the profile that not be used, the prot_id is set to 0xff
1600 		 * and the off is set to 0x1ff for all the field vectors.
1601 		 */
1602 		for (j = 0; j < hw->blk[ICE_BLK_SW].es.fvw; j++)
1603 			if (fv->ew[j].prot_id != ICE_PROT_INVALID ||
1604 			    fv->ew[j].off != ICE_FV_OFFSET_INVAL)
1605 				flag = true;
1606 		if (flag && prof_index > max_prof_index)
1607 			max_prof_index = prof_index;
1608 
1609 		prof_index++;
1610 		flag = false;
1611 	} while (fv);
1612 
1613 	hw->switch_info->max_used_prof_index = max_prof_index;
1614 
1615 	return ICE_SUCCESS;
1616 }
1617 
1618 /**
1619  * ice_init_pkg - initialize/download package
1620  * @hw: pointer to the hardware structure
1621  * @buf: pointer to the package buffer
1622  * @len: size of the package buffer
1623  *
1624  * This function initializes a package. The package contains HW tables
1625  * required to do packet processing. First, the function extracts package
1626  * information such as version. Then it finds the ice configuration segment
1627  * within the package; this function then saves a copy of the segment pointer
1628  * within the supplied package buffer. Next, the function will cache any hints
1629  * from the package, followed by downloading the package itself. Note, that if
1630  * a previous PF driver has already downloaded the package successfully, then
1631  * the current driver will not have to download the package again.
1632  *
1633  * The local package contents will be used to query default behavior and to
1634  * update specific sections of the HW's version of the package (e.g. to update
1635  * the parse graph to understand new protocols).
1636  *
1637  * This function stores a pointer to the package buffer memory, and it is
1638  * expected that the supplied buffer will not be freed immediately. If the
1639  * package buffer needs to be freed, such as when read from a file, use
1640  * ice_copy_and_init_pkg() instead of directly calling ice_init_pkg() in this
1641  * case.
1642  */
1643 enum ice_status ice_init_pkg(struct ice_hw *hw, u8 *buf, u32 len)
1644 {
1645 	struct ice_pkg_hdr *pkg;
1646 	enum ice_status status;
1647 	struct ice_seg *seg;
1648 
1649 	if (!buf || !len)
1650 		return ICE_ERR_PARAM;
1651 
1652 	pkg = (struct ice_pkg_hdr *)buf;
1653 	status = ice_verify_pkg(pkg, len);
1654 	if (status) {
1655 		ice_debug(hw, ICE_DBG_INIT, "failed to verify pkg (err: %d)\n",
1656 			  status);
1657 		return status;
1658 	}
1659 
1660 	/* initialize package info */
1661 	status = ice_init_pkg_info(hw, pkg);
1662 	if (status)
1663 		return status;
1664 
1665 	/* before downloading the package, check package version for
1666 	 * compatibility with driver
1667 	 */
1668 	status = ice_chk_pkg_compat(hw, pkg, &seg);
1669 	if (status)
1670 		return status;
1671 
1672 	/* initialize package hints and then download package */
1673 	ice_init_pkg_hints(hw, seg);
1674 	status = ice_download_pkg(hw, seg);
1675 	if (status == ICE_ERR_AQ_NO_WORK) {
1676 		ice_debug(hw, ICE_DBG_INIT, "package previously loaded - no work.\n");
1677 		status = ICE_SUCCESS;
1678 	}
1679 
1680 	/* Get information on the package currently loaded in HW, then make sure
1681 	 * the driver is compatible with this version.
1682 	 */
1683 	if (!status) {
1684 		status = ice_get_pkg_info(hw);
1685 		if (!status)
1686 			status = ice_chk_pkg_version(&hw->active_pkg_ver);
1687 	}
1688 
1689 	if (!status) {
1690 		hw->seg = seg;
1691 		/* on successful package download update other required
1692 		 * registers to support the package and fill HW tables
1693 		 * with package content.
1694 		 */
1695 		ice_init_pkg_regs(hw);
1696 		ice_fill_blk_tbls(hw);
1697 		ice_fill_hw_ptype(hw);
1698 		ice_get_prof_index_max(hw);
1699 	} else {
1700 		ice_debug(hw, ICE_DBG_INIT, "package load failed, %d\n",
1701 			  status);
1702 	}
1703 
1704 	return status;
1705 }
1706 
1707 /**
1708  * ice_copy_and_init_pkg - initialize/download a copy of the package
1709  * @hw: pointer to the hardware structure
1710  * @buf: pointer to the package buffer
1711  * @len: size of the package buffer
1712  *
1713  * This function copies the package buffer, and then calls ice_init_pkg() to
1714  * initialize the copied package contents.
1715  *
1716  * The copying is necessary if the package buffer supplied is constant, or if
1717  * the memory may disappear shortly after calling this function.
1718  *
1719  * If the package buffer resides in the data segment and can be modified, the
1720  * caller is free to use ice_init_pkg() instead of ice_copy_and_init_pkg().
1721  *
1722  * However, if the package buffer needs to be copied first, such as when being
1723  * read from a file, the caller should use ice_copy_and_init_pkg().
1724  *
1725  * This function will first copy the package buffer, before calling
1726  * ice_init_pkg(). The caller is free to immediately destroy the original
1727  * package buffer, as the new copy will be managed by this function and
1728  * related routines.
1729  */
1730 enum ice_status ice_copy_and_init_pkg(struct ice_hw *hw, const u8 *buf, u32 len)
1731 {
1732 	enum ice_status status;
1733 	u8 *buf_copy;
1734 
1735 	if (!buf || !len)
1736 		return ICE_ERR_PARAM;
1737 
1738 	buf_copy = (u8 *)ice_memdup(hw, buf, len, ICE_NONDMA_TO_NONDMA);
1739 
1740 	status = ice_init_pkg(hw, buf_copy, len);
1741 	if (status) {
1742 		/* Free the copy, since we failed to initialize the package */
1743 		ice_free(hw, buf_copy);
1744 	} else {
1745 		/* Track the copied pkg so we can free it later */
1746 		hw->pkg_copy = buf_copy;
1747 		hw->pkg_size = len;
1748 	}
1749 
1750 	return status;
1751 }
1752 
1753 /**
1754  * ice_pkg_buf_alloc
1755  * @hw: pointer to the HW structure
1756  *
1757  * Allocates a package buffer and returns a pointer to the buffer header.
1758  * Note: all package contents must be in Little Endian form.
1759  */
1760 static struct ice_buf_build *ice_pkg_buf_alloc(struct ice_hw *hw)
1761 {
1762 	struct ice_buf_build *bld;
1763 	struct ice_buf_hdr *buf;
1764 
1765 	bld = (struct ice_buf_build *)ice_malloc(hw, sizeof(*bld));
1766 	if (!bld)
1767 		return NULL;
1768 
1769 	buf = (struct ice_buf_hdr *)bld;
1770 	buf->data_end = CPU_TO_LE16(offsetof(struct ice_buf_hdr,
1771 					     section_entry));
1772 	return bld;
1773 }
1774 
1775 /**
1776  * ice_get_sw_prof_type - determine switch profile type
1777  * @hw: pointer to the HW structure
1778  * @fv: pointer to the switch field vector
1779  */
1780 static enum ice_prof_type
1781 ice_get_sw_prof_type(struct ice_hw *hw, struct ice_fv *fv)
1782 {
1783 	u16 i;
1784 
1785 	for (i = 0; i < hw->blk[ICE_BLK_SW].es.fvw; i++) {
1786 		/* UDP tunnel will have UDP_OF protocol ID and VNI offset */
1787 		if (fv->ew[i].prot_id == (u8)ICE_PROT_UDP_OF &&
1788 		    fv->ew[i].off == ICE_VNI_OFFSET)
1789 			return ICE_PROF_TUN_UDP;
1790 
1791 		/* GRE tunnel will have GRE protocol */
1792 		if (fv->ew[i].prot_id == (u8)ICE_PROT_GRE_OF)
1793 			return ICE_PROF_TUN_GRE;
1794 
1795 		/* PPPOE tunnel will have PPPOE protocol */
1796 		if (fv->ew[i].prot_id == (u8)ICE_PROT_PPPOE)
1797 			return ICE_PROF_TUN_PPPOE;
1798 	}
1799 
1800 	return ICE_PROF_NON_TUN;
1801 }
1802 
1803 /**
1804  * ice_get_sw_fv_bitmap - Get switch field vector bitmap based on profile type
1805  * @hw: pointer to hardware structure
1806  * @req_profs: type of profiles requested
1807  * @bm: pointer to memory for returning the bitmap of field vectors
1808  */
1809 void
1810 ice_get_sw_fv_bitmap(struct ice_hw *hw, enum ice_prof_type req_profs,
1811 		     ice_bitmap_t *bm)
1812 {
1813 	struct ice_pkg_enum state;
1814 	struct ice_seg *ice_seg;
1815 	struct ice_fv *fv;
1816 
1817 	if (req_profs == ICE_PROF_ALL) {
1818 		ice_bitmap_set(bm, 0, ICE_MAX_NUM_PROFILES);
1819 		return;
1820 	}
1821 
1822 	ice_memset(&state, 0, sizeof(state), ICE_NONDMA_MEM);
1823 	ice_zero_bitmap(bm, ICE_MAX_NUM_PROFILES);
1824 	ice_seg = hw->seg;
1825 	do {
1826 		enum ice_prof_type prof_type;
1827 		u32 offset;
1828 
1829 		fv = (struct ice_fv *)
1830 			ice_pkg_enum_entry(ice_seg, &state, ICE_SID_FLD_VEC_SW,
1831 					   &offset, ice_sw_fv_handler);
1832 		ice_seg = NULL;
1833 
1834 		if (fv) {
1835 			/* Determine field vector type */
1836 			prof_type = ice_get_sw_prof_type(hw, fv);
1837 
1838 			if (req_profs & prof_type)
1839 				ice_set_bit((u16)offset, bm);
1840 		}
1841 	} while (fv);
1842 }
1843 
1844 /**
1845  * ice_get_sw_fv_list
1846  * @hw: pointer to the HW structure
1847  * @prot_ids: field vector to search for with a given protocol ID
1848  * @ids_cnt: lookup/protocol count
1849  * @bm: bitmap of field vectors to consider
1850  * @fv_list: Head of a list
1851  *
1852  * Finds all the field vector entries from switch block that contain
1853  * a given protocol ID and returns a list of structures of type
1854  * "ice_sw_fv_list_entry". Every structure in the list has a field vector
1855  * definition and profile ID information
1856  * NOTE: The caller of the function is responsible for freeing the memory
1857  * allocated for every list entry.
1858  */
1859 enum ice_status
1860 ice_get_sw_fv_list(struct ice_hw *hw, u8 *prot_ids, u16 ids_cnt,
1861 		   ice_bitmap_t *bm, struct LIST_HEAD_TYPE *fv_list)
1862 {
1863 	struct ice_sw_fv_list_entry *fvl;
1864 	struct ice_sw_fv_list_entry *tmp;
1865 	struct ice_pkg_enum state;
1866 	struct ice_seg *ice_seg;
1867 	struct ice_fv *fv;
1868 	u32 offset;
1869 
1870 	ice_memset(&state, 0, sizeof(state), ICE_NONDMA_MEM);
1871 
1872 	if (!ids_cnt || !hw->seg)
1873 		return ICE_ERR_PARAM;
1874 
1875 	ice_seg = hw->seg;
1876 	do {
1877 		u16 i;
1878 
1879 		fv = (struct ice_fv *)
1880 			ice_pkg_enum_entry(ice_seg, &state, ICE_SID_FLD_VEC_SW,
1881 					   &offset, ice_sw_fv_handler);
1882 		if (!fv)
1883 			break;
1884 		ice_seg = NULL;
1885 
1886 		/* If field vector is not in the bitmap list, then skip this
1887 		 * profile.
1888 		 */
1889 		if (!ice_is_bit_set(bm, (u16)offset))
1890 			continue;
1891 
1892 		for (i = 0; i < ids_cnt; i++) {
1893 			int j;
1894 
1895 			/* This code assumes that if a switch field vector line
1896 			 * has a matching protocol, then this line will contain
1897 			 * the entries necessary to represent every field in
1898 			 * that protocol header.
1899 			 */
1900 			for (j = 0; j < hw->blk[ICE_BLK_SW].es.fvw; j++)
1901 				if (fv->ew[j].prot_id == prot_ids[i])
1902 					break;
1903 			if (j >= hw->blk[ICE_BLK_SW].es.fvw)
1904 				break;
1905 			if (i + 1 == ids_cnt) {
1906 				fvl = (struct ice_sw_fv_list_entry *)
1907 					ice_malloc(hw, sizeof(*fvl));
1908 				if (!fvl)
1909 					goto err;
1910 				fvl->fv_ptr = fv;
1911 				fvl->profile_id = offset;
1912 				LIST_ADD(&fvl->list_entry, fv_list);
1913 				break;
1914 			}
1915 		}
1916 	} while (fv);
1917 	if (LIST_EMPTY(fv_list))
1918 		return ICE_ERR_CFG;
1919 	return ICE_SUCCESS;
1920 
1921 err:
1922 	LIST_FOR_EACH_ENTRY_SAFE(fvl, tmp, fv_list, ice_sw_fv_list_entry,
1923 				 list_entry) {
1924 		LIST_DEL(&fvl->list_entry);
1925 		ice_free(hw, fvl);
1926 	}
1927 
1928 	return ICE_ERR_NO_MEMORY;
1929 }
1930 
1931 /**
1932  * ice_init_prof_result_bm - Initialize the profile result index bitmap
1933  * @hw: pointer to hardware structure
1934  */
1935 void ice_init_prof_result_bm(struct ice_hw *hw)
1936 {
1937 	struct ice_pkg_enum state;
1938 	struct ice_seg *ice_seg;
1939 	struct ice_fv *fv;
1940 
1941 	ice_memset(&state, 0, sizeof(state), ICE_NONDMA_MEM);
1942 
1943 	if (!hw->seg)
1944 		return;
1945 
1946 	ice_seg = hw->seg;
1947 	do {
1948 		u32 off;
1949 		u16 i;
1950 
1951 		fv = (struct ice_fv *)
1952 			ice_pkg_enum_entry(ice_seg, &state, ICE_SID_FLD_VEC_SW,
1953 					   &off, ice_sw_fv_handler);
1954 		ice_seg = NULL;
1955 		if (!fv)
1956 			break;
1957 
1958 		ice_zero_bitmap(hw->switch_info->prof_res_bm[off],
1959 				ICE_MAX_FV_WORDS);
1960 
1961 		/* Determine empty field vector indices, these can be
1962 		 * used for recipe results. Skip index 0, since it is
1963 		 * always used for Switch ID.
1964 		 */
1965 		for (i = 1; i < ICE_MAX_FV_WORDS; i++)
1966 			if (fv->ew[i].prot_id == ICE_PROT_INVALID &&
1967 			    fv->ew[i].off == ICE_FV_OFFSET_INVAL)
1968 				ice_set_bit(i,
1969 					    hw->switch_info->prof_res_bm[off]);
1970 	} while (fv);
1971 }
1972 
1973 /**
1974  * ice_pkg_buf_free
1975  * @hw: pointer to the HW structure
1976  * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
1977  *
1978  * Frees a package buffer
1979  */
1980 void ice_pkg_buf_free(struct ice_hw *hw, struct ice_buf_build *bld)
1981 {
1982 	ice_free(hw, bld);
1983 }
1984 
1985 /**
1986  * ice_pkg_buf_reserve_section
1987  * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
1988  * @count: the number of sections to reserve
1989  *
1990  * Reserves one or more section table entries in a package buffer. This routine
1991  * can be called multiple times as long as they are made before calling
1992  * ice_pkg_buf_alloc_section(). Once ice_pkg_buf_alloc_section()
1993  * is called once, the number of sections that can be allocated will not be able
1994  * to be increased; not using all reserved sections is fine, but this will
1995  * result in some wasted space in the buffer.
1996  * Note: all package contents must be in Little Endian form.
1997  */
1998 static enum ice_status
1999 ice_pkg_buf_reserve_section(struct ice_buf_build *bld, u16 count)
2000 {
2001 	struct ice_buf_hdr *buf;
2002 	u16 section_count;
2003 	u16 data_end;
2004 
2005 	if (!bld)
2006 		return ICE_ERR_PARAM;
2007 
2008 	buf = (struct ice_buf_hdr *)&bld->buf;
2009 
2010 	/* already an active section, can't increase table size */
2011 	section_count = LE16_TO_CPU(buf->section_count);
2012 	if (section_count > 0)
2013 		return ICE_ERR_CFG;
2014 
2015 	if (bld->reserved_section_table_entries + count > ICE_MAX_S_COUNT)
2016 		return ICE_ERR_CFG;
2017 	bld->reserved_section_table_entries += count;
2018 
2019 	data_end = LE16_TO_CPU(buf->data_end) +
2020 		FLEX_ARRAY_SIZE(buf, section_entry, count);
2021 	buf->data_end = CPU_TO_LE16(data_end);
2022 
2023 	return ICE_SUCCESS;
2024 }
2025 
2026 /**
2027  * ice_pkg_buf_alloc_section
2028  * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
2029  * @type: the section type value
2030  * @size: the size of the section to reserve (in bytes)
2031  *
2032  * Reserves memory in the buffer for a section's content and updates the
2033  * buffers' status accordingly. This routine returns a pointer to the first
2034  * byte of the section start within the buffer, which is used to fill in the
2035  * section contents.
2036  * Note: all package contents must be in Little Endian form.
2037  */
2038 static void *
2039 ice_pkg_buf_alloc_section(struct ice_buf_build *bld, u32 type, u16 size)
2040 {
2041 	struct ice_buf_hdr *buf;
2042 	u16 sect_count;
2043 	u16 data_end;
2044 
2045 	if (!bld || !type || !size)
2046 		return NULL;
2047 
2048 	buf = (struct ice_buf_hdr *)&bld->buf;
2049 
2050 	/* check for enough space left in buffer */
2051 	data_end = LE16_TO_CPU(buf->data_end);
2052 
2053 	/* section start must align on 4 byte boundary */
2054 	data_end = ICE_ALIGN(data_end, 4);
2055 
2056 	if ((data_end + size) > ICE_MAX_S_DATA_END)
2057 		return NULL;
2058 
2059 	/* check for more available section table entries */
2060 	sect_count = LE16_TO_CPU(buf->section_count);
2061 	if (sect_count < bld->reserved_section_table_entries) {
2062 		void *section_ptr = ((u8 *)buf) + data_end;
2063 
2064 		buf->section_entry[sect_count].offset = CPU_TO_LE16(data_end);
2065 		buf->section_entry[sect_count].size = CPU_TO_LE16(size);
2066 		buf->section_entry[sect_count].type = CPU_TO_LE32(type);
2067 
2068 		data_end += size;
2069 		buf->data_end = CPU_TO_LE16(data_end);
2070 
2071 		buf->section_count = CPU_TO_LE16(sect_count + 1);
2072 		return section_ptr;
2073 	}
2074 
2075 	/* no free section table entries */
2076 	return NULL;
2077 }
2078 
2079 /**
2080  * ice_pkg_buf_alloc_single_section
2081  * @hw: pointer to the HW structure
2082  * @type: the section type value
2083  * @size: the size of the section to reserve (in bytes)
2084  * @section: returns pointer to the section
2085  *
2086  * Allocates a package buffer with a single section.
2087  * Note: all package contents must be in Little Endian form.
2088  */
2089 struct ice_buf_build *
2090 ice_pkg_buf_alloc_single_section(struct ice_hw *hw, u32 type, u16 size,
2091 				 void **section)
2092 {
2093 	struct ice_buf_build *buf;
2094 
2095 	if (!section)
2096 		return NULL;
2097 
2098 	buf = ice_pkg_buf_alloc(hw);
2099 	if (!buf)
2100 		return NULL;
2101 
2102 	if (ice_pkg_buf_reserve_section(buf, 1))
2103 		goto ice_pkg_buf_alloc_single_section_err;
2104 
2105 	*section = ice_pkg_buf_alloc_section(buf, type, size);
2106 	if (!*section)
2107 		goto ice_pkg_buf_alloc_single_section_err;
2108 
2109 	return buf;
2110 
2111 ice_pkg_buf_alloc_single_section_err:
2112 	ice_pkg_buf_free(hw, buf);
2113 	return NULL;
2114 }
2115 
2116 /**
2117  * ice_pkg_buf_get_active_sections
2118  * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
2119  *
2120  * Returns the number of active sections. Before using the package buffer
2121  * in an update package command, the caller should make sure that there is at
2122  * least one active section - otherwise, the buffer is not legal and should
2123  * not be used.
2124  * Note: all package contents must be in Little Endian form.
2125  */
2126 static u16 ice_pkg_buf_get_active_sections(struct ice_buf_build *bld)
2127 {
2128 	struct ice_buf_hdr *buf;
2129 
2130 	if (!bld)
2131 		return 0;
2132 
2133 	buf = (struct ice_buf_hdr *)&bld->buf;
2134 	return LE16_TO_CPU(buf->section_count);
2135 }
2136 
2137 /**
2138  * ice_pkg_buf
2139  * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
2140  *
2141  * Return a pointer to the buffer's header
2142  */
2143 struct ice_buf *ice_pkg_buf(struct ice_buf_build *bld)
2144 {
2145 	if (!bld)
2146 		return NULL;
2147 
2148 	return &bld->buf;
2149 }
2150 
2151 /**
2152  * ice_tunnel_port_in_use_hlpr - helper function to determine tunnel usage
2153  * @hw: pointer to the HW structure
2154  * @port: port to search for
2155  * @index: optionally returns index
2156  *
2157  * Returns whether a port is already in use as a tunnel, and optionally its
2158  * index
2159  */
2160 static bool ice_tunnel_port_in_use_hlpr(struct ice_hw *hw, u16 port, u16 *index)
2161 {
2162 	u16 i;
2163 
2164 	for (i = 0; i < hw->tnl.count && i < ICE_TUNNEL_MAX_ENTRIES; i++)
2165 		if (hw->tnl.tbl[i].in_use && hw->tnl.tbl[i].port == port) {
2166 			if (index)
2167 				*index = i;
2168 			return true;
2169 		}
2170 
2171 	return false;
2172 }
2173 
2174 /**
2175  * ice_tunnel_port_in_use
2176  * @hw: pointer to the HW structure
2177  * @port: port to search for
2178  * @index: optionally returns index
2179  *
2180  * Returns whether a port is already in use as a tunnel, and optionally its
2181  * index
2182  */
2183 bool ice_tunnel_port_in_use(struct ice_hw *hw, u16 port, u16 *index)
2184 {
2185 	bool res;
2186 
2187 	ice_acquire_lock(&hw->tnl_lock);
2188 	res = ice_tunnel_port_in_use_hlpr(hw, port, index);
2189 	ice_release_lock(&hw->tnl_lock);
2190 
2191 	return res;
2192 }
2193 
2194 /**
2195  * ice_tunnel_get_type
2196  * @hw: pointer to the HW structure
2197  * @port: port to search for
2198  * @type: returns tunnel index
2199  *
2200  * For a given port number, will return the type of tunnel.
2201  */
2202 bool
2203 ice_tunnel_get_type(struct ice_hw *hw, u16 port, enum ice_tunnel_type *type)
2204 {
2205 	bool res = false;
2206 	u16 i;
2207 
2208 	ice_acquire_lock(&hw->tnl_lock);
2209 
2210 	for (i = 0; i < hw->tnl.count && i < ICE_TUNNEL_MAX_ENTRIES; i++)
2211 		if (hw->tnl.tbl[i].in_use && hw->tnl.tbl[i].port == port) {
2212 			*type = hw->tnl.tbl[i].type;
2213 			res = true;
2214 			break;
2215 		}
2216 
2217 	ice_release_lock(&hw->tnl_lock);
2218 
2219 	return res;
2220 }
2221 
2222 /**
2223  * ice_find_free_tunnel_entry
2224  * @hw: pointer to the HW structure
2225  * @type: tunnel type
2226  * @index: optionally returns index
2227  *
2228  * Returns whether there is a free tunnel entry, and optionally its index
2229  */
2230 static bool
2231 ice_find_free_tunnel_entry(struct ice_hw *hw, enum ice_tunnel_type type,
2232 			   u16 *index)
2233 {
2234 	u16 i;
2235 
2236 	for (i = 0; i < hw->tnl.count && i < ICE_TUNNEL_MAX_ENTRIES; i++)
2237 		if (hw->tnl.tbl[i].valid && !hw->tnl.tbl[i].in_use &&
2238 		    hw->tnl.tbl[i].type == type) {
2239 			if (index)
2240 				*index = i;
2241 			return true;
2242 		}
2243 
2244 	return false;
2245 }
2246 
2247 /**
2248  * ice_get_open_tunnel_port - retrieve an open tunnel port
2249  * @hw: pointer to the HW structure
2250  * @type: tunnel type (TNL_ALL will return any open port)
2251  * @port: returns open port
2252  */
2253 bool
2254 ice_get_open_tunnel_port(struct ice_hw *hw, enum ice_tunnel_type type,
2255 			 u16 *port)
2256 {
2257 	bool res = false;
2258 	u16 i;
2259 
2260 	ice_acquire_lock(&hw->tnl_lock);
2261 
2262 	for (i = 0; i < hw->tnl.count && i < ICE_TUNNEL_MAX_ENTRIES; i++)
2263 		if (hw->tnl.tbl[i].valid && hw->tnl.tbl[i].in_use &&
2264 		    (type == TNL_ALL || hw->tnl.tbl[i].type == type)) {
2265 			*port = hw->tnl.tbl[i].port;
2266 			res = true;
2267 			break;
2268 		}
2269 
2270 	ice_release_lock(&hw->tnl_lock);
2271 
2272 	return res;
2273 }
2274 
2275 /**
2276  * ice_upd_dvm_boost_entry
2277  * @hw: pointer to the HW structure
2278  * @entry: pointer to double vlan boost entry info
2279  */
2280 static enum ice_status
2281 ice_upd_dvm_boost_entry(struct ice_hw *hw, struct ice_dvm_entry *entry)
2282 {
2283 	struct ice_boost_tcam_section *sect_rx, *sect_tx;
2284 	enum ice_status status = ICE_ERR_MAX_LIMIT;
2285 	struct ice_buf_build *bld;
2286 	u8 val, dc, nm;
2287 
2288 	bld = ice_pkg_buf_alloc(hw);
2289 	if (!bld)
2290 		return ICE_ERR_NO_MEMORY;
2291 
2292 	/* allocate 2 sections, one for Rx parser, one for Tx parser */
2293 	if (ice_pkg_buf_reserve_section(bld, 2))
2294 		goto ice_upd_dvm_boost_entry_err;
2295 
2296 	sect_rx = (struct ice_boost_tcam_section *)
2297 		ice_pkg_buf_alloc_section(bld, ICE_SID_RXPARSER_BOOST_TCAM,
2298 					  ice_struct_size(sect_rx, tcam, 1));
2299 	if (!sect_rx)
2300 		goto ice_upd_dvm_boost_entry_err;
2301 	sect_rx->count = CPU_TO_LE16(1);
2302 
2303 	sect_tx = (struct ice_boost_tcam_section *)
2304 		ice_pkg_buf_alloc_section(bld, ICE_SID_TXPARSER_BOOST_TCAM,
2305 					  ice_struct_size(sect_tx, tcam, 1));
2306 	if (!sect_tx)
2307 		goto ice_upd_dvm_boost_entry_err;
2308 	sect_tx->count = CPU_TO_LE16(1);
2309 
2310 	/* copy original boost entry to update package buffer */
2311 	ice_memcpy(sect_rx->tcam, entry->boost_entry, sizeof(*sect_rx->tcam),
2312 		   ICE_NONDMA_TO_NONDMA);
2313 
2314 	/* re-write the don't care and never match bits accordingly */
2315 	if (entry->enable) {
2316 		/* all bits are don't care */
2317 		val = 0x00;
2318 		dc = 0xFF;
2319 		nm = 0x00;
2320 	} else {
2321 		/* disable, one never match bit, the rest are don't care */
2322 		val = 0x00;
2323 		dc = 0xF7;
2324 		nm = 0x08;
2325 	}
2326 
2327 	ice_set_key((u8 *)&sect_rx->tcam[0].key, sizeof(sect_rx->tcam[0].key),
2328 		    &val, NULL, &dc, &nm, 0, sizeof(u8));
2329 
2330 	/* exact copy of entry to Tx section entry */
2331 	ice_memcpy(sect_tx->tcam, sect_rx->tcam, sizeof(*sect_tx->tcam),
2332 		   ICE_NONDMA_TO_NONDMA);
2333 
2334 	status = ice_update_pkg_no_lock(hw, ice_pkg_buf(bld), 1);
2335 
2336 ice_upd_dvm_boost_entry_err:
2337 	ice_pkg_buf_free(hw, bld);
2338 
2339 	return status;
2340 }
2341 
2342 /**
2343  * ice_set_dvm_boost_entries
2344  * @hw: pointer to the HW structure
2345  *
2346  * Enable double vlan by updating the appropriate boost tcam entries.
2347  */
2348 enum ice_status ice_set_dvm_boost_entries(struct ice_hw *hw)
2349 {
2350 	enum ice_status status;
2351 	u16 i;
2352 
2353 	for (i = 0; i < hw->dvm_upd.count; i++) {
2354 		status = ice_upd_dvm_boost_entry(hw, &hw->dvm_upd.tbl[i]);
2355 		if (status)
2356 			return status;
2357 	}
2358 
2359 	return ICE_SUCCESS;
2360 }
2361 
2362 /**
2363  * ice_create_tunnel
2364  * @hw: pointer to the HW structure
2365  * @type: type of tunnel
2366  * @port: port of tunnel to create
2367  *
2368  * Create a tunnel by updating the parse graph in the parser. We do that by
2369  * creating a package buffer with the tunnel info and issuing an update package
2370  * command.
2371  */
2372 enum ice_status
2373 ice_create_tunnel(struct ice_hw *hw, enum ice_tunnel_type type, u16 port)
2374 {
2375 	struct ice_boost_tcam_section *sect_rx, *sect_tx;
2376 	enum ice_status status = ICE_ERR_MAX_LIMIT;
2377 	struct ice_buf_build *bld;
2378 	u16 index;
2379 
2380 	ice_acquire_lock(&hw->tnl_lock);
2381 
2382 	if (ice_tunnel_port_in_use_hlpr(hw, port, &index)) {
2383 		hw->tnl.tbl[index].ref++;
2384 		status = ICE_SUCCESS;
2385 		goto ice_create_tunnel_end;
2386 	}
2387 
2388 	if (!ice_find_free_tunnel_entry(hw, type, &index)) {
2389 		status = ICE_ERR_OUT_OF_RANGE;
2390 		goto ice_create_tunnel_end;
2391 	}
2392 
2393 	bld = ice_pkg_buf_alloc(hw);
2394 	if (!bld) {
2395 		status = ICE_ERR_NO_MEMORY;
2396 		goto ice_create_tunnel_end;
2397 	}
2398 
2399 	/* allocate 2 sections, one for Rx parser, one for Tx parser */
2400 	if (ice_pkg_buf_reserve_section(bld, 2))
2401 		goto ice_create_tunnel_err;
2402 
2403 	sect_rx = (struct ice_boost_tcam_section *)
2404 		ice_pkg_buf_alloc_section(bld, ICE_SID_RXPARSER_BOOST_TCAM,
2405 					  ice_struct_size(sect_rx, tcam, 1));
2406 	if (!sect_rx)
2407 		goto ice_create_tunnel_err;
2408 	sect_rx->count = CPU_TO_LE16(1);
2409 
2410 	sect_tx = (struct ice_boost_tcam_section *)
2411 		ice_pkg_buf_alloc_section(bld, ICE_SID_TXPARSER_BOOST_TCAM,
2412 					  ice_struct_size(sect_tx, tcam, 1));
2413 	if (!sect_tx)
2414 		goto ice_create_tunnel_err;
2415 	sect_tx->count = CPU_TO_LE16(1);
2416 
2417 	/* copy original boost entry to update package buffer */
2418 	ice_memcpy(sect_rx->tcam, hw->tnl.tbl[index].boost_entry,
2419 		   sizeof(*sect_rx->tcam), ICE_NONDMA_TO_NONDMA);
2420 
2421 	/* over-write the never-match dest port key bits with the encoded port
2422 	 * bits
2423 	 */
2424 	ice_set_key((u8 *)&sect_rx->tcam[0].key, sizeof(sect_rx->tcam[0].key),
2425 		    (u8 *)&port, NULL, NULL, NULL,
2426 		    (u16)offsetof(struct ice_boost_key_value, hv_dst_port_key),
2427 		    sizeof(sect_rx->tcam[0].key.key.hv_dst_port_key));
2428 
2429 	/* exact copy of entry to Tx section entry */
2430 	ice_memcpy(sect_tx->tcam, sect_rx->tcam, sizeof(*sect_tx->tcam),
2431 		   ICE_NONDMA_TO_NONDMA);
2432 
2433 	status = ice_update_pkg(hw, ice_pkg_buf(bld), 1);
2434 	if (!status) {
2435 		hw->tnl.tbl[index].port = port;
2436 		hw->tnl.tbl[index].in_use = true;
2437 		hw->tnl.tbl[index].ref = 1;
2438 	}
2439 
2440 ice_create_tunnel_err:
2441 	ice_pkg_buf_free(hw, bld);
2442 
2443 ice_create_tunnel_end:
2444 	ice_release_lock(&hw->tnl_lock);
2445 
2446 	return status;
2447 }
2448 
2449 /**
2450  * ice_destroy_tunnel
2451  * @hw: pointer to the HW structure
2452  * @port: port of tunnel to destroy (ignored if the all parameter is true)
2453  * @all: flag that states to destroy all tunnels
2454  *
2455  * Destroys a tunnel or all tunnels by creating an update package buffer
2456  * targeting the specific updates requested and then performing an update
2457  * package.
2458  */
2459 enum ice_status ice_destroy_tunnel(struct ice_hw *hw, u16 port, bool all)
2460 {
2461 	struct ice_boost_tcam_section *sect_rx, *sect_tx;
2462 	enum ice_status status = ICE_ERR_MAX_LIMIT;
2463 	struct ice_buf_build *bld;
2464 	u16 count = 0;
2465 	u16 index;
2466 	u16 size;
2467 	u16 i, j;
2468 
2469 	ice_acquire_lock(&hw->tnl_lock);
2470 
2471 	if (!all && ice_tunnel_port_in_use_hlpr(hw, port, &index))
2472 		if (hw->tnl.tbl[index].ref > 1) {
2473 			hw->tnl.tbl[index].ref--;
2474 			status = ICE_SUCCESS;
2475 			goto ice_destroy_tunnel_end;
2476 		}
2477 
2478 	/* determine count */
2479 	for (i = 0; i < hw->tnl.count && i < ICE_TUNNEL_MAX_ENTRIES; i++)
2480 		if (hw->tnl.tbl[i].valid && hw->tnl.tbl[i].in_use &&
2481 		    (all || hw->tnl.tbl[i].port == port))
2482 			count++;
2483 
2484 	if (!count) {
2485 		status = ICE_ERR_PARAM;
2486 		goto ice_destroy_tunnel_end;
2487 	}
2488 
2489 	/* size of section - there is at least one entry */
2490 	size = ice_struct_size(sect_rx, tcam, count);
2491 
2492 	bld = ice_pkg_buf_alloc(hw);
2493 	if (!bld) {
2494 		status = ICE_ERR_NO_MEMORY;
2495 		goto ice_destroy_tunnel_end;
2496 	}
2497 
2498 	/* allocate 2 sections, one for Rx parser, one for Tx parser */
2499 	if (ice_pkg_buf_reserve_section(bld, 2))
2500 		goto ice_destroy_tunnel_err;
2501 
2502 	sect_rx = (struct ice_boost_tcam_section *)
2503 		ice_pkg_buf_alloc_section(bld, ICE_SID_RXPARSER_BOOST_TCAM,
2504 					  size);
2505 	if (!sect_rx)
2506 		goto ice_destroy_tunnel_err;
2507 	sect_rx->count = CPU_TO_LE16(count);
2508 
2509 	sect_tx = (struct ice_boost_tcam_section *)
2510 		ice_pkg_buf_alloc_section(bld, ICE_SID_TXPARSER_BOOST_TCAM,
2511 					  size);
2512 	if (!sect_tx)
2513 		goto ice_destroy_tunnel_err;
2514 	sect_tx->count = CPU_TO_LE16(count);
2515 
2516 	/* copy original boost entry to update package buffer, one copy to Rx
2517 	 * section, another copy to the Tx section
2518 	 */
2519 	for (i = 0, j = 0; i < hw->tnl.count && i < ICE_TUNNEL_MAX_ENTRIES; i++)
2520 		if (hw->tnl.tbl[i].valid && hw->tnl.tbl[i].in_use &&
2521 		    (all || hw->tnl.tbl[i].port == port)) {
2522 			ice_memcpy(sect_rx->tcam + j,
2523 				   hw->tnl.tbl[i].boost_entry,
2524 				   sizeof(*sect_rx->tcam),
2525 				   ICE_NONDMA_TO_NONDMA);
2526 			ice_memcpy(sect_tx->tcam + j,
2527 				   hw->tnl.tbl[i].boost_entry,
2528 				   sizeof(*sect_tx->tcam),
2529 				   ICE_NONDMA_TO_NONDMA);
2530 			hw->tnl.tbl[i].marked = true;
2531 			j++;
2532 		}
2533 
2534 	status = ice_update_pkg(hw, ice_pkg_buf(bld), 1);
2535 	if (!status)
2536 		for (i = 0; i < hw->tnl.count &&
2537 		     i < ICE_TUNNEL_MAX_ENTRIES; i++)
2538 			if (hw->tnl.tbl[i].marked) {
2539 				hw->tnl.tbl[i].ref = 0;
2540 				hw->tnl.tbl[i].port = 0;
2541 				hw->tnl.tbl[i].in_use = false;
2542 				hw->tnl.tbl[i].marked = false;
2543 			}
2544 
2545 ice_destroy_tunnel_err:
2546 	ice_pkg_buf_free(hw, bld);
2547 
2548 ice_destroy_tunnel_end:
2549 	ice_release_lock(&hw->tnl_lock);
2550 
2551 	return status;
2552 }
2553 
2554 /**
2555  * ice_find_prot_off - find prot ID and offset pair, based on prof and FV index
2556  * @hw: pointer to the hardware structure
2557  * @blk: hardware block
2558  * @prof: profile ID
2559  * @fv_idx: field vector word index
2560  * @prot: variable to receive the protocol ID
2561  * @off: variable to receive the protocol offset
2562  */
2563 enum ice_status
2564 ice_find_prot_off(struct ice_hw *hw, enum ice_block blk, u8 prof, u16 fv_idx,
2565 		  u8 *prot, u16 *off)
2566 {
2567 	struct ice_fv_word *fv_ext;
2568 
2569 	if (prof >= hw->blk[blk].es.count)
2570 		return ICE_ERR_PARAM;
2571 
2572 	if (fv_idx >= hw->blk[blk].es.fvw)
2573 		return ICE_ERR_PARAM;
2574 
2575 	fv_ext = hw->blk[blk].es.t + (prof * hw->blk[blk].es.fvw);
2576 
2577 	*prot = fv_ext[fv_idx].prot_id;
2578 	*off = fv_ext[fv_idx].off;
2579 
2580 	return ICE_SUCCESS;
2581 }
2582 
2583 /* PTG Management */
2584 
2585 /**
2586  * ice_ptg_find_ptype - Search for packet type group using packet type (ptype)
2587  * @hw: pointer to the hardware structure
2588  * @blk: HW block
2589  * @ptype: the ptype to search for
2590  * @ptg: pointer to variable that receives the PTG
2591  *
2592  * This function will search the PTGs for a particular ptype, returning the
2593  * PTG ID that contains it through the PTG parameter, with the value of
2594  * ICE_DEFAULT_PTG (0) meaning it is part the default PTG.
2595  */
2596 static enum ice_status
2597 ice_ptg_find_ptype(struct ice_hw *hw, enum ice_block blk, u16 ptype, u8 *ptg)
2598 {
2599 	if (ptype >= ICE_XLT1_CNT || !ptg)
2600 		return ICE_ERR_PARAM;
2601 
2602 	*ptg = hw->blk[blk].xlt1.ptypes[ptype].ptg;
2603 	return ICE_SUCCESS;
2604 }
2605 
2606 /**
2607  * ice_ptg_alloc_val - Allocates a new packet type group ID by value
2608  * @hw: pointer to the hardware structure
2609  * @blk: HW block
2610  * @ptg: the PTG to allocate
2611  *
2612  * This function allocates a given packet type group ID specified by the PTG
2613  * parameter.
2614  */
2615 static void ice_ptg_alloc_val(struct ice_hw *hw, enum ice_block blk, u8 ptg)
2616 {
2617 	hw->blk[blk].xlt1.ptg_tbl[ptg].in_use = true;
2618 }
2619 
2620 /**
2621  * ice_ptg_remove_ptype - Removes ptype from a particular packet type group
2622  * @hw: pointer to the hardware structure
2623  * @blk: HW block
2624  * @ptype: the ptype to remove
2625  * @ptg: the PTG to remove the ptype from
2626  *
2627  * This function will remove the ptype from the specific PTG, and move it to
2628  * the default PTG (ICE_DEFAULT_PTG).
2629  */
2630 static enum ice_status
2631 ice_ptg_remove_ptype(struct ice_hw *hw, enum ice_block blk, u16 ptype, u8 ptg)
2632 {
2633 	struct ice_ptg_ptype **ch;
2634 	struct ice_ptg_ptype *p;
2635 
2636 	if (ptype > ICE_XLT1_CNT - 1)
2637 		return ICE_ERR_PARAM;
2638 
2639 	if (!hw->blk[blk].xlt1.ptg_tbl[ptg].in_use)
2640 		return ICE_ERR_DOES_NOT_EXIST;
2641 
2642 	/* Should not happen if .in_use is set, bad config */
2643 	if (!hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype)
2644 		return ICE_ERR_CFG;
2645 
2646 	/* find the ptype within this PTG, and bypass the link over it */
2647 	p = hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype;
2648 	ch = &hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype;
2649 	while (p) {
2650 		if (ptype == (p - hw->blk[blk].xlt1.ptypes)) {
2651 			*ch = p->next_ptype;
2652 			break;
2653 		}
2654 
2655 		ch = &p->next_ptype;
2656 		p = p->next_ptype;
2657 	}
2658 
2659 	hw->blk[blk].xlt1.ptypes[ptype].ptg = ICE_DEFAULT_PTG;
2660 	hw->blk[blk].xlt1.ptypes[ptype].next_ptype = NULL;
2661 
2662 	return ICE_SUCCESS;
2663 }
2664 
2665 /**
2666  * ice_ptg_add_mv_ptype - Adds/moves ptype to a particular packet type group
2667  * @hw: pointer to the hardware structure
2668  * @blk: HW block
2669  * @ptype: the ptype to add or move
2670  * @ptg: the PTG to add or move the ptype to
2671  *
2672  * This function will either add or move a ptype to a particular PTG depending
2673  * on if the ptype is already part of another group. Note that using a
2674  * a destination PTG ID of ICE_DEFAULT_PTG (0) will move the ptype to the
2675  * default PTG.
2676  */
2677 static enum ice_status
2678 ice_ptg_add_mv_ptype(struct ice_hw *hw, enum ice_block blk, u16 ptype, u8 ptg)
2679 {
2680 	enum ice_status status;
2681 	u8 original_ptg;
2682 
2683 	if (ptype > ICE_XLT1_CNT - 1)
2684 		return ICE_ERR_PARAM;
2685 
2686 	if (!hw->blk[blk].xlt1.ptg_tbl[ptg].in_use && ptg != ICE_DEFAULT_PTG)
2687 		return ICE_ERR_DOES_NOT_EXIST;
2688 
2689 	status = ice_ptg_find_ptype(hw, blk, ptype, &original_ptg);
2690 	if (status)
2691 		return status;
2692 
2693 	/* Is ptype already in the correct PTG? */
2694 	if (original_ptg == ptg)
2695 		return ICE_SUCCESS;
2696 
2697 	/* Remove from original PTG and move back to the default PTG */
2698 	if (original_ptg != ICE_DEFAULT_PTG)
2699 		ice_ptg_remove_ptype(hw, blk, ptype, original_ptg);
2700 
2701 	/* Moving to default PTG? Then we're done with this request */
2702 	if (ptg == ICE_DEFAULT_PTG)
2703 		return ICE_SUCCESS;
2704 
2705 	/* Add ptype to PTG at beginning of list */
2706 	hw->blk[blk].xlt1.ptypes[ptype].next_ptype =
2707 		hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype;
2708 	hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype =
2709 		&hw->blk[blk].xlt1.ptypes[ptype];
2710 
2711 	hw->blk[blk].xlt1.ptypes[ptype].ptg = ptg;
2712 	hw->blk[blk].xlt1.t[ptype] = ptg;
2713 
2714 	return ICE_SUCCESS;
2715 }
2716 
2717 /* Block / table size info */
2718 struct ice_blk_size_details {
2719 	u16 xlt1;			/* # XLT1 entries */
2720 	u16 xlt2;			/* # XLT2 entries */
2721 	u16 prof_tcam;			/* # profile ID TCAM entries */
2722 	u16 prof_id;			/* # profile IDs */
2723 	u8 prof_cdid_bits;		/* # CDID one-hot bits used in key */
2724 	u16 prof_redir;			/* # profile redirection entries */
2725 	u16 es;				/* # extraction sequence entries */
2726 	u16 fvw;			/* # field vector words */
2727 	u8 overwrite;			/* overwrite existing entries allowed */
2728 	u8 reverse;			/* reverse FV order */
2729 };
2730 
2731 static const struct ice_blk_size_details blk_sizes[ICE_BLK_COUNT] = {
2732 	/**
2733 	 * Table Definitions
2734 	 * XLT1 - Number of entries in XLT1 table
2735 	 * XLT2 - Number of entries in XLT2 table
2736 	 * TCAM - Number of entries Profile ID TCAM table
2737 	 * CDID - Control Domain ID of the hardware block
2738 	 * PRED - Number of entries in the Profile Redirection Table
2739 	 * FV   - Number of entries in the Field Vector
2740 	 * FVW  - Width (in WORDs) of the Field Vector
2741 	 * OVR  - Overwrite existing table entries
2742 	 * REV  - Reverse FV
2743 	 */
2744 	/*          XLT1        , XLT2        ,TCAM, PID,CDID,PRED,   FV, FVW */
2745 	/*          Overwrite   , Reverse FV */
2746 	/* SW  */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 256,   0,  256, 256,  48,
2747 		    false, false },
2748 	/* ACL */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 128,   0,  128, 128,  32,
2749 		    false, false },
2750 	/* FD  */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 128,   0,  128, 128,  24,
2751 		    false, true  },
2752 	/* RSS */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 128,   0,  128, 128,  24,
2753 		    true,  true  },
2754 	/* PE  */ { ICE_XLT1_CNT, ICE_XLT2_CNT,  64,  32,   0,   32,  32,  24,
2755 		    false, false },
2756 };
2757 
2758 enum ice_sid_all {
2759 	ICE_SID_XLT1_OFF = 0,
2760 	ICE_SID_XLT2_OFF,
2761 	ICE_SID_PR_OFF,
2762 	ICE_SID_PR_REDIR_OFF,
2763 	ICE_SID_ES_OFF,
2764 	ICE_SID_OFF_COUNT,
2765 };
2766 
2767 /* Characteristic handling */
2768 
2769 /**
2770  * ice_match_prop_lst - determine if properties of two lists match
2771  * @list1: first properties list
2772  * @list2: second properties list
2773  *
2774  * Count, cookies and the order must match in order to be considered equivalent.
2775  */
2776 static bool
2777 ice_match_prop_lst(struct LIST_HEAD_TYPE *list1, struct LIST_HEAD_TYPE *list2)
2778 {
2779 	struct ice_vsig_prof *tmp1;
2780 	struct ice_vsig_prof *tmp2;
2781 	u16 chk_count = 0;
2782 	u16 count = 0;
2783 
2784 	/* compare counts */
2785 	LIST_FOR_EACH_ENTRY(tmp1, list1, ice_vsig_prof, list)
2786 		count++;
2787 	LIST_FOR_EACH_ENTRY(tmp2, list2, ice_vsig_prof, list)
2788 		chk_count++;
2789 	if (!count || count != chk_count)
2790 		return false;
2791 
2792 	tmp1 = LIST_FIRST_ENTRY(list1, struct ice_vsig_prof, list);
2793 	tmp2 = LIST_FIRST_ENTRY(list2, struct ice_vsig_prof, list);
2794 
2795 	/* profile cookies must compare, and in the exact same order to take
2796 	 * into account priority
2797 	 */
2798 	while (count--) {
2799 		if (tmp2->profile_cookie != tmp1->profile_cookie)
2800 			return false;
2801 
2802 		tmp1 = LIST_NEXT_ENTRY(tmp1, struct ice_vsig_prof, list);
2803 		tmp2 = LIST_NEXT_ENTRY(tmp2, struct ice_vsig_prof, list);
2804 	}
2805 
2806 	return true;
2807 }
2808 
2809 /* VSIG Management */
2810 
2811 /**
2812  * ice_vsig_find_vsi - find a VSIG that contains a specified VSI
2813  * @hw: pointer to the hardware structure
2814  * @blk: HW block
2815  * @vsi: VSI of interest
2816  * @vsig: pointer to receive the VSI group
2817  *
2818  * This function will lookup the VSI entry in the XLT2 list and return
2819  * the VSI group its associated with.
2820  */
2821 enum ice_status
2822 ice_vsig_find_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 *vsig)
2823 {
2824 	if (!vsig || vsi >= ICE_MAX_VSI)
2825 		return ICE_ERR_PARAM;
2826 
2827 	/* As long as there's a default or valid VSIG associated with the input
2828 	 * VSI, the functions returns a success. Any handling of VSIG will be
2829 	 * done by the following add, update or remove functions.
2830 	 */
2831 	*vsig = hw->blk[blk].xlt2.vsis[vsi].vsig;
2832 
2833 	return ICE_SUCCESS;
2834 }
2835 
2836 /**
2837  * ice_vsig_alloc_val - allocate a new VSIG by value
2838  * @hw: pointer to the hardware structure
2839  * @blk: HW block
2840  * @vsig: the VSIG to allocate
2841  *
2842  * This function will allocate a given VSIG specified by the VSIG parameter.
2843  */
2844 static u16 ice_vsig_alloc_val(struct ice_hw *hw, enum ice_block blk, u16 vsig)
2845 {
2846 	u16 idx = vsig & ICE_VSIG_IDX_M;
2847 
2848 	if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use) {
2849 		INIT_LIST_HEAD(&hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst);
2850 		hw->blk[blk].xlt2.vsig_tbl[idx].in_use = true;
2851 	}
2852 
2853 	return ICE_VSIG_VALUE(idx, hw->pf_id);
2854 }
2855 
2856 /**
2857  * ice_vsig_alloc - Finds a free entry and allocates a new VSIG
2858  * @hw: pointer to the hardware structure
2859  * @blk: HW block
2860  *
2861  * This function will iterate through the VSIG list and mark the first
2862  * unused entry for the new VSIG entry as used and return that value.
2863  */
2864 static u16 ice_vsig_alloc(struct ice_hw *hw, enum ice_block blk)
2865 {
2866 	u16 i;
2867 
2868 	for (i = 1; i < ICE_MAX_VSIGS; i++)
2869 		if (!hw->blk[blk].xlt2.vsig_tbl[i].in_use)
2870 			return ice_vsig_alloc_val(hw, blk, i);
2871 
2872 	return ICE_DEFAULT_VSIG;
2873 }
2874 
2875 /**
2876  * ice_find_dup_props_vsig - find VSI group with a specified set of properties
2877  * @hw: pointer to the hardware structure
2878  * @blk: HW block
2879  * @chs: characteristic list
2880  * @vsig: returns the VSIG with the matching profiles, if found
2881  *
2882  * Each VSIG is associated with a characteristic set; i.e. all VSIs under
2883  * a group have the same characteristic set. To check if there exists a VSIG
2884  * which has the same characteristics as the input characteristics; this
2885  * function will iterate through the XLT2 list and return the VSIG that has a
2886  * matching configuration. In order to make sure that priorities are accounted
2887  * for, the list must match exactly, including the order in which the
2888  * characteristics are listed.
2889  */
2890 static enum ice_status
2891 ice_find_dup_props_vsig(struct ice_hw *hw, enum ice_block blk,
2892 			struct LIST_HEAD_TYPE *chs, u16 *vsig)
2893 {
2894 	struct ice_xlt2 *xlt2 = &hw->blk[blk].xlt2;
2895 	u16 i;
2896 
2897 	for (i = 0; i < xlt2->count; i++)
2898 		if (xlt2->vsig_tbl[i].in_use &&
2899 		    ice_match_prop_lst(chs, &xlt2->vsig_tbl[i].prop_lst)) {
2900 			*vsig = ICE_VSIG_VALUE(i, hw->pf_id);
2901 			return ICE_SUCCESS;
2902 		}
2903 
2904 	return ICE_ERR_DOES_NOT_EXIST;
2905 }
2906 
2907 /**
2908  * ice_vsig_free - free VSI group
2909  * @hw: pointer to the hardware structure
2910  * @blk: HW block
2911  * @vsig: VSIG to remove
2912  *
2913  * The function will remove all VSIs associated with the input VSIG and move
2914  * them to the DEFAULT_VSIG and mark the VSIG available.
2915  */
2916 static enum ice_status
2917 ice_vsig_free(struct ice_hw *hw, enum ice_block blk, u16 vsig)
2918 {
2919 	struct ice_vsig_prof *dtmp, *del;
2920 	struct ice_vsig_vsi *vsi_cur;
2921 	u16 idx;
2922 
2923 	idx = vsig & ICE_VSIG_IDX_M;
2924 	if (idx >= ICE_MAX_VSIGS)
2925 		return ICE_ERR_PARAM;
2926 
2927 	if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use)
2928 		return ICE_ERR_DOES_NOT_EXIST;
2929 
2930 	hw->blk[blk].xlt2.vsig_tbl[idx].in_use = false;
2931 
2932 	vsi_cur = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
2933 	/* If the VSIG has at least 1 VSI then iterate through the
2934 	 * list and remove the VSIs before deleting the group.
2935 	 */
2936 	if (vsi_cur) {
2937 		/* remove all vsis associated with this VSIG XLT2 entry */
2938 		do {
2939 			struct ice_vsig_vsi *tmp = vsi_cur->next_vsi;
2940 
2941 			vsi_cur->vsig = ICE_DEFAULT_VSIG;
2942 			vsi_cur->changed = 1;
2943 			vsi_cur->next_vsi = NULL;
2944 			vsi_cur = tmp;
2945 		} while (vsi_cur);
2946 
2947 		/* NULL terminate head of VSI list */
2948 		hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi = NULL;
2949 	}
2950 
2951 	/* free characteristic list */
2952 	LIST_FOR_EACH_ENTRY_SAFE(del, dtmp,
2953 				 &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
2954 				 ice_vsig_prof, list) {
2955 		LIST_DEL(&del->list);
2956 		ice_free(hw, del);
2957 	}
2958 
2959 	/* if VSIG characteristic list was cleared for reset
2960 	 * re-initialize the list head
2961 	 */
2962 	INIT_LIST_HEAD(&hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst);
2963 
2964 	return ICE_SUCCESS;
2965 }
2966 
2967 /**
2968  * ice_vsig_remove_vsi - remove VSI from VSIG
2969  * @hw: pointer to the hardware structure
2970  * @blk: HW block
2971  * @vsi: VSI to remove
2972  * @vsig: VSI group to remove from
2973  *
2974  * The function will remove the input VSI from its VSI group and move it
2975  * to the DEFAULT_VSIG.
2976  */
2977 static enum ice_status
2978 ice_vsig_remove_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 vsig)
2979 {
2980 	struct ice_vsig_vsi **vsi_head, *vsi_cur, *vsi_tgt;
2981 	u16 idx;
2982 
2983 	idx = vsig & ICE_VSIG_IDX_M;
2984 
2985 	if (vsi >= ICE_MAX_VSI || idx >= ICE_MAX_VSIGS)
2986 		return ICE_ERR_PARAM;
2987 
2988 	if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use)
2989 		return ICE_ERR_DOES_NOT_EXIST;
2990 
2991 	/* entry already in default VSIG, don't have to remove */
2992 	if (idx == ICE_DEFAULT_VSIG)
2993 		return ICE_SUCCESS;
2994 
2995 	vsi_head = &hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
2996 	if (!(*vsi_head))
2997 		return ICE_ERR_CFG;
2998 
2999 	vsi_tgt = &hw->blk[blk].xlt2.vsis[vsi];
3000 	vsi_cur = (*vsi_head);
3001 
3002 	/* iterate the VSI list, skip over the entry to be removed */
3003 	while (vsi_cur) {
3004 		if (vsi_tgt == vsi_cur) {
3005 			(*vsi_head) = vsi_cur->next_vsi;
3006 			break;
3007 		}
3008 		vsi_head = &vsi_cur->next_vsi;
3009 		vsi_cur = vsi_cur->next_vsi;
3010 	}
3011 
3012 	/* verify if VSI was removed from group list */
3013 	if (!vsi_cur)
3014 		return ICE_ERR_DOES_NOT_EXIST;
3015 
3016 	vsi_cur->vsig = ICE_DEFAULT_VSIG;
3017 	vsi_cur->changed = 1;
3018 	vsi_cur->next_vsi = NULL;
3019 
3020 	return ICE_SUCCESS;
3021 }
3022 
3023 /**
3024  * ice_vsig_add_mv_vsi - add or move a VSI to a VSI group
3025  * @hw: pointer to the hardware structure
3026  * @blk: HW block
3027  * @vsi: VSI to move
3028  * @vsig: destination VSI group
3029  *
3030  * This function will move or add the input VSI to the target VSIG.
3031  * The function will find the original VSIG the VSI belongs to and
3032  * move the entry to the DEFAULT_VSIG, update the original VSIG and
3033  * then move entry to the new VSIG.
3034  */
3035 static enum ice_status
3036 ice_vsig_add_mv_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 vsig)
3037 {
3038 	struct ice_vsig_vsi *tmp;
3039 	enum ice_status status;
3040 	u16 orig_vsig, idx;
3041 
3042 	idx = vsig & ICE_VSIG_IDX_M;
3043 
3044 	if (vsi >= ICE_MAX_VSI || idx >= ICE_MAX_VSIGS)
3045 		return ICE_ERR_PARAM;
3046 
3047 	/* if VSIG not in use and VSIG is not default type this VSIG
3048 	 * doesn't exist.
3049 	 */
3050 	if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use &&
3051 	    vsig != ICE_DEFAULT_VSIG)
3052 		return ICE_ERR_DOES_NOT_EXIST;
3053 
3054 	status = ice_vsig_find_vsi(hw, blk, vsi, &orig_vsig);
3055 	if (status)
3056 		return status;
3057 
3058 	/* no update required if vsigs match */
3059 	if (orig_vsig == vsig)
3060 		return ICE_SUCCESS;
3061 
3062 	if (orig_vsig != ICE_DEFAULT_VSIG) {
3063 		/* remove entry from orig_vsig and add to default VSIG */
3064 		status = ice_vsig_remove_vsi(hw, blk, vsi, orig_vsig);
3065 		if (status)
3066 			return status;
3067 	}
3068 
3069 	if (idx == ICE_DEFAULT_VSIG)
3070 		return ICE_SUCCESS;
3071 
3072 	/* Create VSI entry and add VSIG and prop_mask values */
3073 	hw->blk[blk].xlt2.vsis[vsi].vsig = vsig;
3074 	hw->blk[blk].xlt2.vsis[vsi].changed = 1;
3075 
3076 	/* Add new entry to the head of the VSIG list */
3077 	tmp = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
3078 	hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi =
3079 		&hw->blk[blk].xlt2.vsis[vsi];
3080 	hw->blk[blk].xlt2.vsis[vsi].next_vsi = tmp;
3081 	hw->blk[blk].xlt2.t[vsi] = vsig;
3082 
3083 	return ICE_SUCCESS;
3084 }
3085 
3086 /**
3087  * ice_prof_has_mask_idx - determine if profile index masking is identical
3088  * @hw: pointer to the hardware structure
3089  * @blk: HW block
3090  * @prof: profile to check
3091  * @idx: profile index to check
3092  * @mask: mask to match
3093  */
3094 static bool
3095 ice_prof_has_mask_idx(struct ice_hw *hw, enum ice_block blk, u8 prof, u16 idx,
3096 		      u16 mask)
3097 {
3098 	bool expect_no_mask = false;
3099 	bool found = false;
3100 	bool match = false;
3101 	u16 i;
3102 
3103 	/* If mask is 0x0000 or 0xffff, then there is no masking */
3104 	if (mask == 0 || mask == 0xffff)
3105 		expect_no_mask = true;
3106 
3107 	/* Scan the enabled masks on this profile, for the specified idx */
3108 	for (i = hw->blk[blk].masks.first; i < hw->blk[blk].masks.first +
3109 	     hw->blk[blk].masks.count; i++)
3110 		if (hw->blk[blk].es.mask_ena[prof] & BIT(i))
3111 			if (hw->blk[blk].masks.masks[i].in_use &&
3112 			    hw->blk[blk].masks.masks[i].idx == idx) {
3113 				found = true;
3114 				if (hw->blk[blk].masks.masks[i].mask == mask)
3115 					match = true;
3116 				break;
3117 			}
3118 
3119 	if (expect_no_mask) {
3120 		if (found)
3121 			return false;
3122 	} else {
3123 		if (!match)
3124 			return false;
3125 	}
3126 
3127 	return true;
3128 }
3129 
3130 /**
3131  * ice_prof_has_mask - determine if profile masking is identical
3132  * @hw: pointer to the hardware structure
3133  * @blk: HW block
3134  * @prof: profile to check
3135  * @masks: masks to match
3136  */
3137 static bool
3138 ice_prof_has_mask(struct ice_hw *hw, enum ice_block blk, u8 prof, u16 *masks)
3139 {
3140 	u16 i;
3141 
3142 	/* es->mask_ena[prof] will have the mask */
3143 	for (i = 0; i < hw->blk[blk].es.fvw; i++)
3144 		if (!ice_prof_has_mask_idx(hw, blk, prof, i, masks[i]))
3145 			return false;
3146 
3147 	return true;
3148 }
3149 
3150 /**
3151  * ice_find_prof_id_with_mask - find profile ID for a given field vector
3152  * @hw: pointer to the hardware structure
3153  * @blk: HW block
3154  * @fv: field vector to search for
3155  * @masks: masks for fv
3156  * @prof_id: receives the profile ID
3157  */
3158 static enum ice_status
3159 ice_find_prof_id_with_mask(struct ice_hw *hw, enum ice_block blk,
3160 			   struct ice_fv_word *fv, u16 *masks, u8 *prof_id)
3161 {
3162 	struct ice_es *es = &hw->blk[blk].es;
3163 	u8 i;
3164 
3165 	/* For FD and RSS, we don't want to re-use an existed profile with the
3166 	 * same field vector and mask. This will cause rule interference.
3167 	 */
3168 	if (blk == ICE_BLK_FD || blk == ICE_BLK_RSS)
3169 		return ICE_ERR_DOES_NOT_EXIST;
3170 
3171 	for (i = 0; i < (u8)es->count; i++) {
3172 		u16 off = i * es->fvw;
3173 
3174 		if (memcmp(&es->t[off], fv, es->fvw * sizeof(*fv)))
3175 			continue;
3176 
3177 		/* check if masks settings are the same for this profile */
3178 		if (masks && !ice_prof_has_mask(hw, blk, i, masks))
3179 			continue;
3180 
3181 		*prof_id = i;
3182 		return ICE_SUCCESS;
3183 	}
3184 
3185 	return ICE_ERR_DOES_NOT_EXIST;
3186 }
3187 
3188 /**
3189  * ice_prof_id_rsrc_type - get profile ID resource type for a block type
3190  * @blk: the block type
3191  * @rsrc_type: pointer to variable to receive the resource type
3192  */
3193 static bool ice_prof_id_rsrc_type(enum ice_block blk, u16 *rsrc_type)
3194 {
3195 	switch (blk) {
3196 	case ICE_BLK_SW:
3197 		*rsrc_type = ICE_AQC_RES_TYPE_SWITCH_PROF_BLDR_PROFID;
3198 		break;
3199 	case ICE_BLK_ACL:
3200 		*rsrc_type = ICE_AQC_RES_TYPE_ACL_PROF_BLDR_PROFID;
3201 		break;
3202 	case ICE_BLK_FD:
3203 		*rsrc_type = ICE_AQC_RES_TYPE_FD_PROF_BLDR_PROFID;
3204 		break;
3205 	case ICE_BLK_RSS:
3206 		*rsrc_type = ICE_AQC_RES_TYPE_HASH_PROF_BLDR_PROFID;
3207 		break;
3208 	case ICE_BLK_PE:
3209 		*rsrc_type = ICE_AQC_RES_TYPE_QHASH_PROF_BLDR_PROFID;
3210 		break;
3211 	default:
3212 		return false;
3213 	}
3214 	return true;
3215 }
3216 
3217 /**
3218  * ice_tcam_ent_rsrc_type - get TCAM entry resource type for a block type
3219  * @blk: the block type
3220  * @rsrc_type: pointer to variable to receive the resource type
3221  */
3222 static bool ice_tcam_ent_rsrc_type(enum ice_block blk, u16 *rsrc_type)
3223 {
3224 	switch (blk) {
3225 	case ICE_BLK_SW:
3226 		*rsrc_type = ICE_AQC_RES_TYPE_SWITCH_PROF_BLDR_TCAM;
3227 		break;
3228 	case ICE_BLK_ACL:
3229 		*rsrc_type = ICE_AQC_RES_TYPE_ACL_PROF_BLDR_TCAM;
3230 		break;
3231 	case ICE_BLK_FD:
3232 		*rsrc_type = ICE_AQC_RES_TYPE_FD_PROF_BLDR_TCAM;
3233 		break;
3234 	case ICE_BLK_RSS:
3235 		*rsrc_type = ICE_AQC_RES_TYPE_HASH_PROF_BLDR_TCAM;
3236 		break;
3237 	case ICE_BLK_PE:
3238 		*rsrc_type = ICE_AQC_RES_TYPE_QHASH_PROF_BLDR_TCAM;
3239 		break;
3240 	default:
3241 		return false;
3242 	}
3243 	return true;
3244 }
3245 
3246 /**
3247  * ice_alloc_tcam_ent - allocate hardware TCAM entry
3248  * @hw: pointer to the HW struct
3249  * @blk: the block to allocate the TCAM for
3250  * @btm: true to allocate from bottom of table, false to allocate from top
3251  * @tcam_idx: pointer to variable to receive the TCAM entry
3252  *
3253  * This function allocates a new entry in a Profile ID TCAM for a specific
3254  * block.
3255  */
3256 static enum ice_status
3257 ice_alloc_tcam_ent(struct ice_hw *hw, enum ice_block blk, bool btm,
3258 		   u16 *tcam_idx)
3259 {
3260 	u16 res_type;
3261 
3262 	if (!ice_tcam_ent_rsrc_type(blk, &res_type))
3263 		return ICE_ERR_PARAM;
3264 
3265 	return ice_alloc_hw_res(hw, res_type, 1, btm, tcam_idx);
3266 }
3267 
3268 /**
3269  * ice_free_tcam_ent - free hardware TCAM entry
3270  * @hw: pointer to the HW struct
3271  * @blk: the block from which to free the TCAM entry
3272  * @tcam_idx: the TCAM entry to free
3273  *
3274  * This function frees an entry in a Profile ID TCAM for a specific block.
3275  */
3276 static enum ice_status
3277 ice_free_tcam_ent(struct ice_hw *hw, enum ice_block blk, u16 tcam_idx)
3278 {
3279 	u16 res_type;
3280 
3281 	if (!ice_tcam_ent_rsrc_type(blk, &res_type))
3282 		return ICE_ERR_PARAM;
3283 
3284 	return ice_free_hw_res(hw, res_type, 1, &tcam_idx);
3285 }
3286 
3287 /**
3288  * ice_alloc_prof_id - allocate profile ID
3289  * @hw: pointer to the HW struct
3290  * @blk: the block to allocate the profile ID for
3291  * @prof_id: pointer to variable to receive the profile ID
3292  *
3293  * This function allocates a new profile ID, which also corresponds to a Field
3294  * Vector (Extraction Sequence) entry.
3295  */
3296 static enum ice_status
3297 ice_alloc_prof_id(struct ice_hw *hw, enum ice_block blk, u8 *prof_id)
3298 {
3299 	enum ice_status status;
3300 	u16 res_type;
3301 	u16 get_prof;
3302 
3303 	if (!ice_prof_id_rsrc_type(blk, &res_type))
3304 		return ICE_ERR_PARAM;
3305 
3306 	status = ice_alloc_hw_res(hw, res_type, 1, false, &get_prof);
3307 	if (!status)
3308 		*prof_id = (u8)get_prof;
3309 
3310 	return status;
3311 }
3312 
3313 /**
3314  * ice_free_prof_id - free profile ID
3315  * @hw: pointer to the HW struct
3316  * @blk: the block from which to free the profile ID
3317  * @prof_id: the profile ID to free
3318  *
3319  * This function frees a profile ID, which also corresponds to a Field Vector.
3320  */
3321 static enum ice_status
3322 ice_free_prof_id(struct ice_hw *hw, enum ice_block blk, u8 prof_id)
3323 {
3324 	u16 tmp_prof_id = (u16)prof_id;
3325 	u16 res_type;
3326 
3327 	if (!ice_prof_id_rsrc_type(blk, &res_type))
3328 		return ICE_ERR_PARAM;
3329 
3330 	return ice_free_hw_res(hw, res_type, 1, &tmp_prof_id);
3331 }
3332 
3333 /**
3334  * ice_prof_inc_ref - increment reference count for profile
3335  * @hw: pointer to the HW struct
3336  * @blk: the block from which to free the profile ID
3337  * @prof_id: the profile ID for which to increment the reference count
3338  */
3339 static enum ice_status
3340 ice_prof_inc_ref(struct ice_hw *hw, enum ice_block blk, u8 prof_id)
3341 {
3342 	if (prof_id > hw->blk[blk].es.count)
3343 		return ICE_ERR_PARAM;
3344 
3345 	hw->blk[blk].es.ref_count[prof_id]++;
3346 
3347 	return ICE_SUCCESS;
3348 }
3349 
3350 /**
3351  * ice_write_prof_mask_reg - write profile mask register
3352  * @hw: pointer to the HW struct
3353  * @blk: hardware block
3354  * @mask_idx: mask index
3355  * @idx: index of the FV which will use the mask
3356  * @mask: the 16-bit mask
3357  */
3358 static void
3359 ice_write_prof_mask_reg(struct ice_hw *hw, enum ice_block blk, u16 mask_idx,
3360 			u16 idx, u16 mask)
3361 {
3362 	u32 offset;
3363 	u32 val;
3364 
3365 	switch (blk) {
3366 	case ICE_BLK_RSS:
3367 		offset = GLQF_HMASK(mask_idx);
3368 		val = (idx << GLQF_HMASK_MSK_INDEX_S) &
3369 			GLQF_HMASK_MSK_INDEX_M;
3370 		val |= (mask << GLQF_HMASK_MASK_S) & GLQF_HMASK_MASK_M;
3371 		break;
3372 	case ICE_BLK_FD:
3373 		offset = GLQF_FDMASK(mask_idx);
3374 		val = (idx << GLQF_FDMASK_MSK_INDEX_S) &
3375 			GLQF_FDMASK_MSK_INDEX_M;
3376 		val |= (mask << GLQF_FDMASK_MASK_S) &
3377 			GLQF_FDMASK_MASK_M;
3378 		break;
3379 	default:
3380 		ice_debug(hw, ICE_DBG_PKG, "No profile masks for block %d\n",
3381 			  blk);
3382 		return;
3383 	}
3384 
3385 	wr32(hw, offset, val);
3386 	ice_debug(hw, ICE_DBG_PKG, "write mask, blk %d (%d): %x = %x\n",
3387 		  blk, idx, offset, val);
3388 }
3389 
3390 /**
3391  * ice_write_prof_mask_enable_res - write profile mask enable register
3392  * @hw: pointer to the HW struct
3393  * @blk: hardware block
3394  * @prof_id: profile ID
3395  * @enable_mask: enable mask
3396  */
3397 static void
3398 ice_write_prof_mask_enable_res(struct ice_hw *hw, enum ice_block blk,
3399 			       u16 prof_id, u32 enable_mask)
3400 {
3401 	u32 offset;
3402 
3403 	switch (blk) {
3404 	case ICE_BLK_RSS:
3405 		offset = GLQF_HMASK_SEL(prof_id);
3406 		break;
3407 	case ICE_BLK_FD:
3408 		offset = GLQF_FDMASK_SEL(prof_id);
3409 		break;
3410 	default:
3411 		ice_debug(hw, ICE_DBG_PKG, "No profile masks for block %d\n",
3412 			  blk);
3413 		return;
3414 	}
3415 
3416 	wr32(hw, offset, enable_mask);
3417 	ice_debug(hw, ICE_DBG_PKG, "write mask enable, blk %d (%d): %x = %x\n",
3418 		  blk, prof_id, offset, enable_mask);
3419 }
3420 
3421 /**
3422  * ice_init_prof_masks - initial prof masks
3423  * @hw: pointer to the HW struct
3424  * @blk: hardware block
3425  */
3426 static void ice_init_prof_masks(struct ice_hw *hw, enum ice_block blk)
3427 {
3428 	u16 per_pf;
3429 	u16 i;
3430 
3431 	ice_init_lock(&hw->blk[blk].masks.lock);
3432 
3433 	per_pf = ICE_PROF_MASK_COUNT / hw->dev_caps.num_funcs;
3434 
3435 	hw->blk[blk].masks.count = per_pf;
3436 	hw->blk[blk].masks.first = hw->pf_id * per_pf;
3437 
3438 	ice_memset(hw->blk[blk].masks.masks, 0,
3439 		   sizeof(hw->blk[blk].masks.masks), ICE_NONDMA_MEM);
3440 
3441 	for (i = hw->blk[blk].masks.first;
3442 	     i < hw->blk[blk].masks.first + hw->blk[blk].masks.count; i++)
3443 		ice_write_prof_mask_reg(hw, blk, i, 0, 0);
3444 }
3445 
3446 /**
3447  * ice_init_all_prof_masks - initial all prof masks
3448  * @hw: pointer to the HW struct
3449  */
3450 void ice_init_all_prof_masks(struct ice_hw *hw)
3451 {
3452 	ice_init_prof_masks(hw, ICE_BLK_RSS);
3453 	ice_init_prof_masks(hw, ICE_BLK_FD);
3454 }
3455 
3456 /**
3457  * ice_alloc_prof_mask - allocate profile mask
3458  * @hw: pointer to the HW struct
3459  * @blk: hardware block
3460  * @idx: index of FV which will use the mask
3461  * @mask: the 16-bit mask
3462  * @mask_idx: variable to receive the mask index
3463  */
3464 static enum ice_status
3465 ice_alloc_prof_mask(struct ice_hw *hw, enum ice_block blk, u16 idx, u16 mask,
3466 		    u16 *mask_idx)
3467 {
3468 	bool found_unused = false, found_copy = false;
3469 	enum ice_status status = ICE_ERR_MAX_LIMIT;
3470 	u16 unused_idx = 0, copy_idx = 0;
3471 	u16 i;
3472 
3473 	if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD)
3474 		return ICE_ERR_PARAM;
3475 
3476 	ice_acquire_lock(&hw->blk[blk].masks.lock);
3477 
3478 	for (i = hw->blk[blk].masks.first;
3479 	     i < hw->blk[blk].masks.first + hw->blk[blk].masks.count; i++)
3480 		if (hw->blk[blk].masks.masks[i].in_use) {
3481 			/* if mask is in use and it exactly duplicates the
3482 			 * desired mask and index, then in can be reused
3483 			 */
3484 			if (hw->blk[blk].masks.masks[i].mask == mask &&
3485 			    hw->blk[blk].masks.masks[i].idx == idx) {
3486 				found_copy = true;
3487 				copy_idx = i;
3488 				break;
3489 			}
3490 		} else {
3491 			/* save off unused index, but keep searching in case
3492 			 * there is an exact match later on
3493 			 */
3494 			if (!found_unused) {
3495 				found_unused = true;
3496 				unused_idx = i;
3497 			}
3498 		}
3499 
3500 	if (found_copy)
3501 		i = copy_idx;
3502 	else if (found_unused)
3503 		i = unused_idx;
3504 	else
3505 		goto err_ice_alloc_prof_mask;
3506 
3507 	/* update mask for a new entry */
3508 	if (found_unused) {
3509 		hw->blk[blk].masks.masks[i].in_use = true;
3510 		hw->blk[blk].masks.masks[i].mask = mask;
3511 		hw->blk[blk].masks.masks[i].idx = idx;
3512 		hw->blk[blk].masks.masks[i].ref = 0;
3513 		ice_write_prof_mask_reg(hw, blk, i, idx, mask);
3514 	}
3515 
3516 	hw->blk[blk].masks.masks[i].ref++;
3517 	*mask_idx = i;
3518 	status = ICE_SUCCESS;
3519 
3520 err_ice_alloc_prof_mask:
3521 	ice_release_lock(&hw->blk[blk].masks.lock);
3522 
3523 	return status;
3524 }
3525 
3526 /**
3527  * ice_free_prof_mask - free profile mask
3528  * @hw: pointer to the HW struct
3529  * @blk: hardware block
3530  * @mask_idx: index of mask
3531  */
3532 static enum ice_status
3533 ice_free_prof_mask(struct ice_hw *hw, enum ice_block blk, u16 mask_idx)
3534 {
3535 	if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD)
3536 		return ICE_ERR_PARAM;
3537 
3538 	if (!(mask_idx >= hw->blk[blk].masks.first &&
3539 	      mask_idx < hw->blk[blk].masks.first + hw->blk[blk].masks.count))
3540 		return ICE_ERR_DOES_NOT_EXIST;
3541 
3542 	ice_acquire_lock(&hw->blk[blk].masks.lock);
3543 
3544 	if (!hw->blk[blk].masks.masks[mask_idx].in_use)
3545 		goto exit_ice_free_prof_mask;
3546 
3547 	if (hw->blk[blk].masks.masks[mask_idx].ref > 1) {
3548 		hw->blk[blk].masks.masks[mask_idx].ref--;
3549 		goto exit_ice_free_prof_mask;
3550 	}
3551 
3552 	/* remove mask */
3553 	hw->blk[blk].masks.masks[mask_idx].in_use = false;
3554 	hw->blk[blk].masks.masks[mask_idx].mask = 0;
3555 	hw->blk[blk].masks.masks[mask_idx].idx = 0;
3556 
3557 	/* update mask as unused entry */
3558 	ice_debug(hw, ICE_DBG_PKG, "Free mask, blk %d, mask %d\n", blk,
3559 		  mask_idx);
3560 	ice_write_prof_mask_reg(hw, blk, mask_idx, 0, 0);
3561 
3562 exit_ice_free_prof_mask:
3563 	ice_release_lock(&hw->blk[blk].masks.lock);
3564 
3565 	return ICE_SUCCESS;
3566 }
3567 
3568 /**
3569  * ice_free_prof_masks - free all profile masks for a profile
3570  * @hw: pointer to the HW struct
3571  * @blk: hardware block
3572  * @prof_id: profile ID
3573  */
3574 static enum ice_status
3575 ice_free_prof_masks(struct ice_hw *hw, enum ice_block blk, u16 prof_id)
3576 {
3577 	u32 mask_bm;
3578 	u16 i;
3579 
3580 	if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD)
3581 		return ICE_ERR_PARAM;
3582 
3583 	mask_bm = hw->blk[blk].es.mask_ena[prof_id];
3584 	for (i = 0; i < BITS_PER_BYTE * sizeof(mask_bm); i++)
3585 		if (mask_bm & BIT(i))
3586 			ice_free_prof_mask(hw, blk, i);
3587 
3588 	return ICE_SUCCESS;
3589 }
3590 
3591 /**
3592  * ice_shutdown_prof_masks - releases lock for masking
3593  * @hw: pointer to the HW struct
3594  * @blk: hardware block
3595  *
3596  * This should be called before unloading the driver
3597  */
3598 static void ice_shutdown_prof_masks(struct ice_hw *hw, enum ice_block blk)
3599 {
3600 	u16 i;
3601 
3602 	ice_acquire_lock(&hw->blk[blk].masks.lock);
3603 
3604 	for (i = hw->blk[blk].masks.first;
3605 	     i < hw->blk[blk].masks.first + hw->blk[blk].masks.count; i++) {
3606 		ice_write_prof_mask_reg(hw, blk, i, 0, 0);
3607 
3608 		hw->blk[blk].masks.masks[i].in_use = false;
3609 		hw->blk[blk].masks.masks[i].idx = 0;
3610 		hw->blk[blk].masks.masks[i].mask = 0;
3611 	}
3612 
3613 	ice_release_lock(&hw->blk[blk].masks.lock);
3614 	ice_destroy_lock(&hw->blk[blk].masks.lock);
3615 }
3616 
3617 /**
3618  * ice_shutdown_all_prof_masks - releases all locks for masking
3619  * @hw: pointer to the HW struct
3620  *
3621  * This should be called before unloading the driver
3622  */
3623 void ice_shutdown_all_prof_masks(struct ice_hw *hw)
3624 {
3625 	ice_shutdown_prof_masks(hw, ICE_BLK_RSS);
3626 	ice_shutdown_prof_masks(hw, ICE_BLK_FD);
3627 }
3628 
3629 /**
3630  * ice_update_prof_masking - set registers according to masking
3631  * @hw: pointer to the HW struct
3632  * @blk: hardware block
3633  * @prof_id: profile ID
3634  * @masks: masks
3635  */
3636 static enum ice_status
3637 ice_update_prof_masking(struct ice_hw *hw, enum ice_block blk, u16 prof_id,
3638 			u16 *masks)
3639 {
3640 	bool err = false;
3641 	u32 ena_mask = 0;
3642 	u16 idx;
3643 	u16 i;
3644 
3645 	/* Only support FD and RSS masking, otherwise nothing to be done */
3646 	if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD)
3647 		return ICE_SUCCESS;
3648 
3649 	for (i = 0; i < hw->blk[blk].es.fvw; i++)
3650 		if (masks[i] && masks[i] != 0xFFFF) {
3651 			if (!ice_alloc_prof_mask(hw, blk, i, masks[i], &idx)) {
3652 				ena_mask |= BIT(idx);
3653 			} else {
3654 				/* not enough bitmaps */
3655 				err = true;
3656 				break;
3657 			}
3658 		}
3659 
3660 	if (err) {
3661 		/* free any bitmaps we have allocated */
3662 		for (i = 0; i < BITS_PER_BYTE * sizeof(ena_mask); i++)
3663 			if (ena_mask & BIT(i))
3664 				ice_free_prof_mask(hw, blk, i);
3665 
3666 		return ICE_ERR_OUT_OF_RANGE;
3667 	}
3668 
3669 	/* enable the masks for this profile */
3670 	ice_write_prof_mask_enable_res(hw, blk, prof_id, ena_mask);
3671 
3672 	/* store enabled masks with profile so that they can be freed later */
3673 	hw->blk[blk].es.mask_ena[prof_id] = ena_mask;
3674 
3675 	return ICE_SUCCESS;
3676 }
3677 
3678 /**
3679  * ice_write_es - write an extraction sequence to hardware
3680  * @hw: pointer to the HW struct
3681  * @blk: the block in which to write the extraction sequence
3682  * @prof_id: the profile ID to write
3683  * @fv: pointer to the extraction sequence to write - NULL to clear extraction
3684  */
3685 static void
3686 ice_write_es(struct ice_hw *hw, enum ice_block blk, u8 prof_id,
3687 	     struct ice_fv_word *fv)
3688 {
3689 	u16 off;
3690 
3691 	off = prof_id * hw->blk[blk].es.fvw;
3692 	if (!fv) {
3693 		ice_memset(&hw->blk[blk].es.t[off], 0, hw->blk[blk].es.fvw *
3694 			   sizeof(*fv), ICE_NONDMA_MEM);
3695 		hw->blk[blk].es.written[prof_id] = false;
3696 	} else {
3697 		ice_memcpy(&hw->blk[blk].es.t[off], fv, hw->blk[blk].es.fvw *
3698 			   sizeof(*fv), ICE_NONDMA_TO_NONDMA);
3699 	}
3700 }
3701 
3702 /**
3703  * ice_prof_dec_ref - decrement reference count for profile
3704  * @hw: pointer to the HW struct
3705  * @blk: the block from which to free the profile ID
3706  * @prof_id: the profile ID for which to decrement the reference count
3707  */
3708 static enum ice_status
3709 ice_prof_dec_ref(struct ice_hw *hw, enum ice_block blk, u8 prof_id)
3710 {
3711 	if (prof_id > hw->blk[blk].es.count)
3712 		return ICE_ERR_PARAM;
3713 
3714 	if (hw->blk[blk].es.ref_count[prof_id] > 0) {
3715 		if (!--hw->blk[blk].es.ref_count[prof_id]) {
3716 			ice_write_es(hw, blk, prof_id, NULL);
3717 			ice_free_prof_masks(hw, blk, prof_id);
3718 			return ice_free_prof_id(hw, blk, prof_id);
3719 		}
3720 	}
3721 
3722 	return ICE_SUCCESS;
3723 }
3724 
3725 /* Block / table section IDs */
3726 static const u32 ice_blk_sids[ICE_BLK_COUNT][ICE_SID_OFF_COUNT] = {
3727 	/* SWITCH */
3728 	{	ICE_SID_XLT1_SW,
3729 		ICE_SID_XLT2_SW,
3730 		ICE_SID_PROFID_TCAM_SW,
3731 		ICE_SID_PROFID_REDIR_SW,
3732 		ICE_SID_FLD_VEC_SW
3733 	},
3734 
3735 	/* ACL */
3736 	{	ICE_SID_XLT1_ACL,
3737 		ICE_SID_XLT2_ACL,
3738 		ICE_SID_PROFID_TCAM_ACL,
3739 		ICE_SID_PROFID_REDIR_ACL,
3740 		ICE_SID_FLD_VEC_ACL
3741 	},
3742 
3743 	/* FD */
3744 	{	ICE_SID_XLT1_FD,
3745 		ICE_SID_XLT2_FD,
3746 		ICE_SID_PROFID_TCAM_FD,
3747 		ICE_SID_PROFID_REDIR_FD,
3748 		ICE_SID_FLD_VEC_FD
3749 	},
3750 
3751 	/* RSS */
3752 	{	ICE_SID_XLT1_RSS,
3753 		ICE_SID_XLT2_RSS,
3754 		ICE_SID_PROFID_TCAM_RSS,
3755 		ICE_SID_PROFID_REDIR_RSS,
3756 		ICE_SID_FLD_VEC_RSS
3757 	},
3758 
3759 	/* PE */
3760 	{	ICE_SID_XLT1_PE,
3761 		ICE_SID_XLT2_PE,
3762 		ICE_SID_PROFID_TCAM_PE,
3763 		ICE_SID_PROFID_REDIR_PE,
3764 		ICE_SID_FLD_VEC_PE
3765 	}
3766 };
3767 
3768 /**
3769  * ice_init_sw_xlt1_db - init software XLT1 database from HW tables
3770  * @hw: pointer to the hardware structure
3771  * @blk: the HW block to initialize
3772  */
3773 static void ice_init_sw_xlt1_db(struct ice_hw *hw, enum ice_block blk)
3774 {
3775 	u16 pt;
3776 
3777 	for (pt = 0; pt < hw->blk[blk].xlt1.count; pt++) {
3778 		u8 ptg;
3779 
3780 		ptg = hw->blk[blk].xlt1.t[pt];
3781 		if (ptg != ICE_DEFAULT_PTG) {
3782 			ice_ptg_alloc_val(hw, blk, ptg);
3783 			ice_ptg_add_mv_ptype(hw, blk, pt, ptg);
3784 		}
3785 	}
3786 }
3787 
3788 /**
3789  * ice_init_sw_xlt2_db - init software XLT2 database from HW tables
3790  * @hw: pointer to the hardware structure
3791  * @blk: the HW block to initialize
3792  */
3793 static void ice_init_sw_xlt2_db(struct ice_hw *hw, enum ice_block blk)
3794 {
3795 	u16 vsi;
3796 
3797 	for (vsi = 0; vsi < hw->blk[blk].xlt2.count; vsi++) {
3798 		u16 vsig;
3799 
3800 		vsig = hw->blk[blk].xlt2.t[vsi];
3801 		if (vsig) {
3802 			ice_vsig_alloc_val(hw, blk, vsig);
3803 			ice_vsig_add_mv_vsi(hw, blk, vsi, vsig);
3804 			/* no changes at this time, since this has been
3805 			 * initialized from the original package
3806 			 */
3807 			hw->blk[blk].xlt2.vsis[vsi].changed = 0;
3808 		}
3809 	}
3810 }
3811 
3812 /**
3813  * ice_init_sw_db - init software database from HW tables
3814  * @hw: pointer to the hardware structure
3815  */
3816 static void ice_init_sw_db(struct ice_hw *hw)
3817 {
3818 	u16 i;
3819 
3820 	for (i = 0; i < ICE_BLK_COUNT; i++) {
3821 		ice_init_sw_xlt1_db(hw, (enum ice_block)i);
3822 		ice_init_sw_xlt2_db(hw, (enum ice_block)i);
3823 	}
3824 }
3825 
3826 /**
3827  * ice_fill_tbl - Reads content of a single table type into database
3828  * @hw: pointer to the hardware structure
3829  * @block_id: Block ID of the table to copy
3830  * @sid: Section ID of the table to copy
3831  *
3832  * Will attempt to read the entire content of a given table of a single block
3833  * into the driver database. We assume that the buffer will always
3834  * be as large or larger than the data contained in the package. If
3835  * this condition is not met, there is most likely an error in the package
3836  * contents.
3837  */
3838 static void ice_fill_tbl(struct ice_hw *hw, enum ice_block block_id, u32 sid)
3839 {
3840 	u32 dst_len, sect_len, offset = 0;
3841 	struct ice_prof_redir_section *pr;
3842 	struct ice_prof_id_section *pid;
3843 	struct ice_xlt1_section *xlt1;
3844 	struct ice_xlt2_section *xlt2;
3845 	struct ice_sw_fv_section *es;
3846 	struct ice_pkg_enum state;
3847 	u8 *src, *dst;
3848 	void *sect;
3849 
3850 	/* if the HW segment pointer is null then the first iteration of
3851 	 * ice_pkg_enum_section() will fail. In this case the HW tables will
3852 	 * not be filled and return success.
3853 	 */
3854 	if (!hw->seg) {
3855 		ice_debug(hw, ICE_DBG_PKG, "hw->seg is NULL, tables are not filled\n");
3856 		return;
3857 	}
3858 
3859 	ice_memset(&state, 0, sizeof(state), ICE_NONDMA_MEM);
3860 
3861 	sect = ice_pkg_enum_section(hw->seg, &state, sid);
3862 
3863 	while (sect) {
3864 		switch (sid) {
3865 		case ICE_SID_XLT1_SW:
3866 		case ICE_SID_XLT1_FD:
3867 		case ICE_SID_XLT1_RSS:
3868 		case ICE_SID_XLT1_ACL:
3869 		case ICE_SID_XLT1_PE:
3870 			xlt1 = (struct ice_xlt1_section *)sect;
3871 			src = xlt1->value;
3872 			sect_len = LE16_TO_CPU(xlt1->count) *
3873 				sizeof(*hw->blk[block_id].xlt1.t);
3874 			dst = hw->blk[block_id].xlt1.t;
3875 			dst_len = hw->blk[block_id].xlt1.count *
3876 				sizeof(*hw->blk[block_id].xlt1.t);
3877 			break;
3878 		case ICE_SID_XLT2_SW:
3879 		case ICE_SID_XLT2_FD:
3880 		case ICE_SID_XLT2_RSS:
3881 		case ICE_SID_XLT2_ACL:
3882 		case ICE_SID_XLT2_PE:
3883 			xlt2 = (struct ice_xlt2_section *)sect;
3884 			src = (_FORCE_ u8 *)xlt2->value;
3885 			sect_len = LE16_TO_CPU(xlt2->count) *
3886 				sizeof(*hw->blk[block_id].xlt2.t);
3887 			dst = (u8 *)hw->blk[block_id].xlt2.t;
3888 			dst_len = hw->blk[block_id].xlt2.count *
3889 				sizeof(*hw->blk[block_id].xlt2.t);
3890 			break;
3891 		case ICE_SID_PROFID_TCAM_SW:
3892 		case ICE_SID_PROFID_TCAM_FD:
3893 		case ICE_SID_PROFID_TCAM_RSS:
3894 		case ICE_SID_PROFID_TCAM_ACL:
3895 		case ICE_SID_PROFID_TCAM_PE:
3896 			pid = (struct ice_prof_id_section *)sect;
3897 			src = (u8 *)pid->entry;
3898 			sect_len = LE16_TO_CPU(pid->count) *
3899 				sizeof(*hw->blk[block_id].prof.t);
3900 			dst = (u8 *)hw->blk[block_id].prof.t;
3901 			dst_len = hw->blk[block_id].prof.count *
3902 				sizeof(*hw->blk[block_id].prof.t);
3903 			break;
3904 		case ICE_SID_PROFID_REDIR_SW:
3905 		case ICE_SID_PROFID_REDIR_FD:
3906 		case ICE_SID_PROFID_REDIR_RSS:
3907 		case ICE_SID_PROFID_REDIR_ACL:
3908 		case ICE_SID_PROFID_REDIR_PE:
3909 			pr = (struct ice_prof_redir_section *)sect;
3910 			src = pr->redir_value;
3911 			sect_len = LE16_TO_CPU(pr->count) *
3912 				sizeof(*hw->blk[block_id].prof_redir.t);
3913 			dst = hw->blk[block_id].prof_redir.t;
3914 			dst_len = hw->blk[block_id].prof_redir.count *
3915 				sizeof(*hw->blk[block_id].prof_redir.t);
3916 			break;
3917 		case ICE_SID_FLD_VEC_SW:
3918 		case ICE_SID_FLD_VEC_FD:
3919 		case ICE_SID_FLD_VEC_RSS:
3920 		case ICE_SID_FLD_VEC_ACL:
3921 		case ICE_SID_FLD_VEC_PE:
3922 			es = (struct ice_sw_fv_section *)sect;
3923 			src = (u8 *)es->fv;
3924 			sect_len = (u32)(LE16_TO_CPU(es->count) *
3925 					 hw->blk[block_id].es.fvw) *
3926 				sizeof(*hw->blk[block_id].es.t);
3927 			dst = (u8 *)hw->blk[block_id].es.t;
3928 			dst_len = (u32)(hw->blk[block_id].es.count *
3929 					hw->blk[block_id].es.fvw) *
3930 				sizeof(*hw->blk[block_id].es.t);
3931 			break;
3932 		default:
3933 			return;
3934 		}
3935 
3936 		/* if the section offset exceeds destination length, terminate
3937 		 * table fill.
3938 		 */
3939 		if (offset > dst_len)
3940 			return;
3941 
3942 		/* if the sum of section size and offset exceed destination size
3943 		 * then we are out of bounds of the HW table size for that PF.
3944 		 * Changing section length to fill the remaining table space
3945 		 * of that PF.
3946 		 */
3947 		if ((offset + sect_len) > dst_len)
3948 			sect_len = dst_len - offset;
3949 
3950 		ice_memcpy(dst + offset, src, sect_len, ICE_NONDMA_TO_NONDMA);
3951 		offset += sect_len;
3952 		sect = ice_pkg_enum_section(NULL, &state, sid);
3953 	}
3954 }
3955 
3956 /**
3957  * ice_fill_blk_tbls - Read package context for tables
3958  * @hw: pointer to the hardware structure
3959  *
3960  * Reads the current package contents and populates the driver
3961  * database with the data iteratively for all advanced feature
3962  * blocks. Assume that the HW tables have been allocated.
3963  */
3964 void ice_fill_blk_tbls(struct ice_hw *hw)
3965 {
3966 	u8 i;
3967 
3968 	for (i = 0; i < ICE_BLK_COUNT; i++) {
3969 		enum ice_block blk_id = (enum ice_block)i;
3970 
3971 		ice_fill_tbl(hw, blk_id, hw->blk[blk_id].xlt1.sid);
3972 		ice_fill_tbl(hw, blk_id, hw->blk[blk_id].xlt2.sid);
3973 		ice_fill_tbl(hw, blk_id, hw->blk[blk_id].prof.sid);
3974 		ice_fill_tbl(hw, blk_id, hw->blk[blk_id].prof_redir.sid);
3975 		ice_fill_tbl(hw, blk_id, hw->blk[blk_id].es.sid);
3976 	}
3977 
3978 	ice_init_sw_db(hw);
3979 }
3980 
3981 /**
3982  * ice_free_prof_map - free profile map
3983  * @hw: pointer to the hardware structure
3984  * @blk_idx: HW block index
3985  */
3986 static void ice_free_prof_map(struct ice_hw *hw, u8 blk_idx)
3987 {
3988 	struct ice_es *es = &hw->blk[blk_idx].es;
3989 	struct ice_prof_map *del, *tmp;
3990 
3991 	ice_acquire_lock(&es->prof_map_lock);
3992 	LIST_FOR_EACH_ENTRY_SAFE(del, tmp, &es->prof_map,
3993 				 ice_prof_map, list) {
3994 		LIST_DEL(&del->list);
3995 		ice_free(hw, del);
3996 	}
3997 	INIT_LIST_HEAD(&es->prof_map);
3998 	ice_release_lock(&es->prof_map_lock);
3999 }
4000 
4001 /**
4002  * ice_free_flow_profs - free flow profile entries
4003  * @hw: pointer to the hardware structure
4004  * @blk_idx: HW block index
4005  */
4006 static void ice_free_flow_profs(struct ice_hw *hw, u8 blk_idx)
4007 {
4008 	struct ice_flow_prof *p, *tmp;
4009 
4010 	ice_acquire_lock(&hw->fl_profs_locks[blk_idx]);
4011 	LIST_FOR_EACH_ENTRY_SAFE(p, tmp, &hw->fl_profs[blk_idx],
4012 				 ice_flow_prof, l_entry) {
4013 		struct ice_flow_entry *e, *t;
4014 
4015 		LIST_FOR_EACH_ENTRY_SAFE(e, t, &p->entries,
4016 					 ice_flow_entry, l_entry)
4017 			ice_flow_rem_entry(hw, (enum ice_block)blk_idx,
4018 					   ICE_FLOW_ENTRY_HNDL(e));
4019 
4020 		LIST_DEL(&p->l_entry);
4021 		if (p->acts)
4022 			ice_free(hw, p->acts);
4023 
4024 		ice_destroy_lock(&p->entries_lock);
4025 		ice_free(hw, p);
4026 	}
4027 	ice_release_lock(&hw->fl_profs_locks[blk_idx]);
4028 
4029 	/* if driver is in reset and tables are being cleared
4030 	 * re-initialize the flow profile list heads
4031 	 */
4032 	INIT_LIST_HEAD(&hw->fl_profs[blk_idx]);
4033 }
4034 
4035 /**
4036  * ice_free_vsig_tbl - free complete VSIG table entries
4037  * @hw: pointer to the hardware structure
4038  * @blk: the HW block on which to free the VSIG table entries
4039  */
4040 static void ice_free_vsig_tbl(struct ice_hw *hw, enum ice_block blk)
4041 {
4042 	u16 i;
4043 
4044 	if (!hw->blk[blk].xlt2.vsig_tbl)
4045 		return;
4046 
4047 	for (i = 1; i < ICE_MAX_VSIGS; i++)
4048 		if (hw->blk[blk].xlt2.vsig_tbl[i].in_use)
4049 			ice_vsig_free(hw, blk, i);
4050 }
4051 
4052 /**
4053  * ice_free_hw_tbls - free hardware table memory
4054  * @hw: pointer to the hardware structure
4055  */
4056 void ice_free_hw_tbls(struct ice_hw *hw)
4057 {
4058 	struct ice_rss_cfg *r, *rt;
4059 	u8 i;
4060 
4061 	for (i = 0; i < ICE_BLK_COUNT; i++) {
4062 		if (hw->blk[i].is_list_init) {
4063 			struct ice_es *es = &hw->blk[i].es;
4064 
4065 			ice_free_prof_map(hw, i);
4066 			ice_destroy_lock(&es->prof_map_lock);
4067 			ice_free_flow_profs(hw, i);
4068 			ice_destroy_lock(&hw->fl_profs_locks[i]);
4069 
4070 			hw->blk[i].is_list_init = false;
4071 		}
4072 		ice_free_vsig_tbl(hw, (enum ice_block)i);
4073 		ice_free(hw, hw->blk[i].xlt1.ptypes);
4074 		ice_free(hw, hw->blk[i].xlt1.ptg_tbl);
4075 		ice_free(hw, hw->blk[i].xlt1.t);
4076 		ice_free(hw, hw->blk[i].xlt2.t);
4077 		ice_free(hw, hw->blk[i].xlt2.vsig_tbl);
4078 		ice_free(hw, hw->blk[i].xlt2.vsis);
4079 		ice_free(hw, hw->blk[i].prof.t);
4080 		ice_free(hw, hw->blk[i].prof_redir.t);
4081 		ice_free(hw, hw->blk[i].es.t);
4082 		ice_free(hw, hw->blk[i].es.ref_count);
4083 		ice_free(hw, hw->blk[i].es.written);
4084 		ice_free(hw, hw->blk[i].es.mask_ena);
4085 	}
4086 
4087 	LIST_FOR_EACH_ENTRY_SAFE(r, rt, &hw->rss_list_head,
4088 				 ice_rss_cfg, l_entry) {
4089 		LIST_DEL(&r->l_entry);
4090 		ice_free(hw, r);
4091 	}
4092 	ice_destroy_lock(&hw->rss_locks);
4093 	if (!hw->dcf_enabled)
4094 		ice_shutdown_all_prof_masks(hw);
4095 	ice_memset(hw->blk, 0, sizeof(hw->blk), ICE_NONDMA_MEM);
4096 }
4097 
4098 /**
4099  * ice_init_flow_profs - init flow profile locks and list heads
4100  * @hw: pointer to the hardware structure
4101  * @blk_idx: HW block index
4102  */
4103 static void ice_init_flow_profs(struct ice_hw *hw, u8 blk_idx)
4104 {
4105 	ice_init_lock(&hw->fl_profs_locks[blk_idx]);
4106 	INIT_LIST_HEAD(&hw->fl_profs[blk_idx]);
4107 }
4108 
4109 /**
4110  * ice_clear_hw_tbls - clear HW tables and flow profiles
4111  * @hw: pointer to the hardware structure
4112  */
4113 void ice_clear_hw_tbls(struct ice_hw *hw)
4114 {
4115 	u8 i;
4116 
4117 	for (i = 0; i < ICE_BLK_COUNT; i++) {
4118 		struct ice_prof_redir *prof_redir = &hw->blk[i].prof_redir;
4119 		struct ice_prof_tcam *prof = &hw->blk[i].prof;
4120 		struct ice_xlt1 *xlt1 = &hw->blk[i].xlt1;
4121 		struct ice_xlt2 *xlt2 = &hw->blk[i].xlt2;
4122 		struct ice_es *es = &hw->blk[i].es;
4123 
4124 		if (hw->blk[i].is_list_init) {
4125 			ice_free_prof_map(hw, i);
4126 			ice_free_flow_profs(hw, i);
4127 		}
4128 
4129 		ice_free_vsig_tbl(hw, (enum ice_block)i);
4130 
4131 		ice_memset(xlt1->ptypes, 0, xlt1->count * sizeof(*xlt1->ptypes),
4132 			   ICE_NONDMA_MEM);
4133 		ice_memset(xlt1->ptg_tbl, 0,
4134 			   ICE_MAX_PTGS * sizeof(*xlt1->ptg_tbl),
4135 			   ICE_NONDMA_MEM);
4136 		ice_memset(xlt1->t, 0, xlt1->count * sizeof(*xlt1->t),
4137 			   ICE_NONDMA_MEM);
4138 
4139 		ice_memset(xlt2->vsis, 0, xlt2->count * sizeof(*xlt2->vsis),
4140 			   ICE_NONDMA_MEM);
4141 		ice_memset(xlt2->vsig_tbl, 0,
4142 			   xlt2->count * sizeof(*xlt2->vsig_tbl),
4143 			   ICE_NONDMA_MEM);
4144 		ice_memset(xlt2->t, 0, xlt2->count * sizeof(*xlt2->t),
4145 			   ICE_NONDMA_MEM);
4146 
4147 		ice_memset(prof->t, 0, prof->count * sizeof(*prof->t),
4148 			   ICE_NONDMA_MEM);
4149 		ice_memset(prof_redir->t, 0,
4150 			   prof_redir->count * sizeof(*prof_redir->t),
4151 			   ICE_NONDMA_MEM);
4152 
4153 		ice_memset(es->t, 0, es->count * sizeof(*es->t) * es->fvw,
4154 			   ICE_NONDMA_MEM);
4155 		ice_memset(es->ref_count, 0, es->count * sizeof(*es->ref_count),
4156 			   ICE_NONDMA_MEM);
4157 		ice_memset(es->written, 0, es->count * sizeof(*es->written),
4158 			   ICE_NONDMA_MEM);
4159 		ice_memset(es->mask_ena, 0, es->count * sizeof(*es->mask_ena),
4160 			   ICE_NONDMA_MEM);
4161 	}
4162 }
4163 
4164 /**
4165  * ice_init_hw_tbls - init hardware table memory
4166  * @hw: pointer to the hardware structure
4167  */
4168 enum ice_status ice_init_hw_tbls(struct ice_hw *hw)
4169 {
4170 	u8 i;
4171 
4172 	ice_init_lock(&hw->rss_locks);
4173 	INIT_LIST_HEAD(&hw->rss_list_head);
4174 	if (!hw->dcf_enabled)
4175 		ice_init_all_prof_masks(hw);
4176 	for (i = 0; i < ICE_BLK_COUNT; i++) {
4177 		struct ice_prof_redir *prof_redir = &hw->blk[i].prof_redir;
4178 		struct ice_prof_tcam *prof = &hw->blk[i].prof;
4179 		struct ice_xlt1 *xlt1 = &hw->blk[i].xlt1;
4180 		struct ice_xlt2 *xlt2 = &hw->blk[i].xlt2;
4181 		struct ice_es *es = &hw->blk[i].es;
4182 		u16 j;
4183 
4184 		if (hw->blk[i].is_list_init)
4185 			continue;
4186 
4187 		ice_init_flow_profs(hw, i);
4188 		ice_init_lock(&es->prof_map_lock);
4189 		INIT_LIST_HEAD(&es->prof_map);
4190 		hw->blk[i].is_list_init = true;
4191 
4192 		hw->blk[i].overwrite = blk_sizes[i].overwrite;
4193 		es->reverse = blk_sizes[i].reverse;
4194 
4195 		xlt1->sid = ice_blk_sids[i][ICE_SID_XLT1_OFF];
4196 		xlt1->count = blk_sizes[i].xlt1;
4197 
4198 		xlt1->ptypes = (struct ice_ptg_ptype *)
4199 			ice_calloc(hw, xlt1->count, sizeof(*xlt1->ptypes));
4200 
4201 		if (!xlt1->ptypes)
4202 			goto err;
4203 
4204 		xlt1->ptg_tbl = (struct ice_ptg_entry *)
4205 			ice_calloc(hw, ICE_MAX_PTGS, sizeof(*xlt1->ptg_tbl));
4206 
4207 		if (!xlt1->ptg_tbl)
4208 			goto err;
4209 
4210 		xlt1->t = (u8 *)ice_calloc(hw, xlt1->count, sizeof(*xlt1->t));
4211 		if (!xlt1->t)
4212 			goto err;
4213 
4214 		xlt2->sid = ice_blk_sids[i][ICE_SID_XLT2_OFF];
4215 		xlt2->count = blk_sizes[i].xlt2;
4216 
4217 		xlt2->vsis = (struct ice_vsig_vsi *)
4218 			ice_calloc(hw, xlt2->count, sizeof(*xlt2->vsis));
4219 
4220 		if (!xlt2->vsis)
4221 			goto err;
4222 
4223 		xlt2->vsig_tbl = (struct ice_vsig_entry *)
4224 			ice_calloc(hw, xlt2->count, sizeof(*xlt2->vsig_tbl));
4225 		if (!xlt2->vsig_tbl)
4226 			goto err;
4227 
4228 		for (j = 0; j < xlt2->count; j++)
4229 			INIT_LIST_HEAD(&xlt2->vsig_tbl[j].prop_lst);
4230 
4231 		xlt2->t = (u16 *)ice_calloc(hw, xlt2->count, sizeof(*xlt2->t));
4232 		if (!xlt2->t)
4233 			goto err;
4234 
4235 		prof->sid = ice_blk_sids[i][ICE_SID_PR_OFF];
4236 		prof->count = blk_sizes[i].prof_tcam;
4237 		prof->max_prof_id = blk_sizes[i].prof_id;
4238 		prof->cdid_bits = blk_sizes[i].prof_cdid_bits;
4239 		prof->t = (struct ice_prof_tcam_entry *)
4240 			ice_calloc(hw, prof->count, sizeof(*prof->t));
4241 
4242 		if (!prof->t)
4243 			goto err;
4244 
4245 		prof_redir->sid = ice_blk_sids[i][ICE_SID_PR_REDIR_OFF];
4246 		prof_redir->count = blk_sizes[i].prof_redir;
4247 		prof_redir->t = (u8 *)ice_calloc(hw, prof_redir->count,
4248 						 sizeof(*prof_redir->t));
4249 
4250 		if (!prof_redir->t)
4251 			goto err;
4252 
4253 		es->sid = ice_blk_sids[i][ICE_SID_ES_OFF];
4254 		es->count = blk_sizes[i].es;
4255 		es->fvw = blk_sizes[i].fvw;
4256 		es->t = (struct ice_fv_word *)
4257 			ice_calloc(hw, (u32)(es->count * es->fvw),
4258 				   sizeof(*es->t));
4259 		if (!es->t)
4260 			goto err;
4261 
4262 		es->ref_count = (u16 *)
4263 			ice_calloc(hw, es->count, sizeof(*es->ref_count));
4264 
4265 		if (!es->ref_count)
4266 			goto err;
4267 
4268 		es->written = (u8 *)
4269 			ice_calloc(hw, es->count, sizeof(*es->written));
4270 
4271 		if (!es->written)
4272 			goto err;
4273 
4274 		es->mask_ena = (u32 *)
4275 			ice_calloc(hw, es->count, sizeof(*es->mask_ena));
4276 
4277 		if (!es->mask_ena)
4278 			goto err;
4279 	}
4280 	return ICE_SUCCESS;
4281 
4282 err:
4283 	ice_free_hw_tbls(hw);
4284 	return ICE_ERR_NO_MEMORY;
4285 }
4286 
4287 /**
4288  * ice_prof_gen_key - generate profile ID key
4289  * @hw: pointer to the HW struct
4290  * @blk: the block in which to write profile ID to
4291  * @ptg: packet type group (PTG) portion of key
4292  * @vsig: VSIG portion of key
4293  * @cdid: CDID portion of key
4294  * @flags: flag portion of key
4295  * @vl_msk: valid mask
4296  * @dc_msk: don't care mask
4297  * @nm_msk: never match mask
4298  * @key: output of profile ID key
4299  */
4300 static enum ice_status
4301 ice_prof_gen_key(struct ice_hw *hw, enum ice_block blk, u8 ptg, u16 vsig,
4302 		 u8 cdid, u16 flags, u8 vl_msk[ICE_TCAM_KEY_VAL_SZ],
4303 		 u8 dc_msk[ICE_TCAM_KEY_VAL_SZ], u8 nm_msk[ICE_TCAM_KEY_VAL_SZ],
4304 		 u8 key[ICE_TCAM_KEY_SZ])
4305 {
4306 	struct ice_prof_id_key inkey;
4307 
4308 	inkey.xlt1 = ptg;
4309 	inkey.xlt2_cdid = CPU_TO_LE16(vsig);
4310 	inkey.flags = CPU_TO_LE16(flags);
4311 
4312 	switch (hw->blk[blk].prof.cdid_bits) {
4313 	case 0:
4314 		break;
4315 	case 2:
4316 #define ICE_CD_2_M 0xC000U
4317 #define ICE_CD_2_S 14
4318 		inkey.xlt2_cdid &= ~CPU_TO_LE16(ICE_CD_2_M);
4319 		inkey.xlt2_cdid |= CPU_TO_LE16(BIT(cdid) << ICE_CD_2_S);
4320 		break;
4321 	case 4:
4322 #define ICE_CD_4_M 0xF000U
4323 #define ICE_CD_4_S 12
4324 		inkey.xlt2_cdid &= ~CPU_TO_LE16(ICE_CD_4_M);
4325 		inkey.xlt2_cdid |= CPU_TO_LE16(BIT(cdid) << ICE_CD_4_S);
4326 		break;
4327 	case 8:
4328 #define ICE_CD_8_M 0xFF00U
4329 #define ICE_CD_8_S 16
4330 		inkey.xlt2_cdid &= ~CPU_TO_LE16(ICE_CD_8_M);
4331 		inkey.xlt2_cdid |= CPU_TO_LE16(BIT(cdid) << ICE_CD_8_S);
4332 		break;
4333 	default:
4334 		ice_debug(hw, ICE_DBG_PKG, "Error in profile config\n");
4335 		break;
4336 	}
4337 
4338 	return ice_set_key(key, ICE_TCAM_KEY_SZ, (u8 *)&inkey, vl_msk, dc_msk,
4339 			   nm_msk, 0, ICE_TCAM_KEY_SZ / 2);
4340 }
4341 
4342 /**
4343  * ice_tcam_write_entry - write TCAM entry
4344  * @hw: pointer to the HW struct
4345  * @blk: the block in which to write profile ID to
4346  * @idx: the entry index to write to
4347  * @prof_id: profile ID
4348  * @ptg: packet type group (PTG) portion of key
4349  * @vsig: VSIG portion of key
4350  * @cdid: CDID portion of key
4351  * @flags: flag portion of key
4352  * @vl_msk: valid mask
4353  * @dc_msk: don't care mask
4354  * @nm_msk: never match mask
4355  */
4356 static enum ice_status
4357 ice_tcam_write_entry(struct ice_hw *hw, enum ice_block blk, u16 idx,
4358 		     u8 prof_id, u8 ptg, u16 vsig, u8 cdid, u16 flags,
4359 		     u8 vl_msk[ICE_TCAM_KEY_VAL_SZ],
4360 		     u8 dc_msk[ICE_TCAM_KEY_VAL_SZ],
4361 		     u8 nm_msk[ICE_TCAM_KEY_VAL_SZ])
4362 {
4363 	struct ice_prof_tcam_entry;
4364 	enum ice_status status;
4365 
4366 	status = ice_prof_gen_key(hw, blk, ptg, vsig, cdid, flags, vl_msk,
4367 				  dc_msk, nm_msk, hw->blk[blk].prof.t[idx].key);
4368 	if (!status) {
4369 		hw->blk[blk].prof.t[idx].addr = CPU_TO_LE16(idx);
4370 		hw->blk[blk].prof.t[idx].prof_id = prof_id;
4371 	}
4372 
4373 	return status;
4374 }
4375 
4376 /**
4377  * ice_vsig_get_ref - returns number of VSIs belong to a VSIG
4378  * @hw: pointer to the hardware structure
4379  * @blk: HW block
4380  * @vsig: VSIG to query
4381  * @refs: pointer to variable to receive the reference count
4382  */
4383 static enum ice_status
4384 ice_vsig_get_ref(struct ice_hw *hw, enum ice_block blk, u16 vsig, u16 *refs)
4385 {
4386 	u16 idx = vsig & ICE_VSIG_IDX_M;
4387 	struct ice_vsig_vsi *ptr;
4388 
4389 	*refs = 0;
4390 
4391 	if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use)
4392 		return ICE_ERR_DOES_NOT_EXIST;
4393 
4394 	ptr = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
4395 	while (ptr) {
4396 		(*refs)++;
4397 		ptr = ptr->next_vsi;
4398 	}
4399 
4400 	return ICE_SUCCESS;
4401 }
4402 
4403 /**
4404  * ice_has_prof_vsig - check to see if VSIG has a specific profile
4405  * @hw: pointer to the hardware structure
4406  * @blk: HW block
4407  * @vsig: VSIG to check against
4408  * @hdl: profile handle
4409  */
4410 static bool
4411 ice_has_prof_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig, u64 hdl)
4412 {
4413 	u16 idx = vsig & ICE_VSIG_IDX_M;
4414 	struct ice_vsig_prof *ent;
4415 
4416 	LIST_FOR_EACH_ENTRY(ent, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
4417 			    ice_vsig_prof, list)
4418 		if (ent->profile_cookie == hdl)
4419 			return true;
4420 
4421 	ice_debug(hw, ICE_DBG_INIT, "Characteristic list for VSI group %d not found.\n",
4422 		  vsig);
4423 	return false;
4424 }
4425 
4426 /**
4427  * ice_prof_bld_es - build profile ID extraction sequence changes
4428  * @hw: pointer to the HW struct
4429  * @blk: hardware block
4430  * @bld: the update package buffer build to add to
4431  * @chgs: the list of changes to make in hardware
4432  */
4433 static enum ice_status
4434 ice_prof_bld_es(struct ice_hw *hw, enum ice_block blk,
4435 		struct ice_buf_build *bld, struct LIST_HEAD_TYPE *chgs)
4436 {
4437 	u16 vec_size = hw->blk[blk].es.fvw * sizeof(struct ice_fv_word);
4438 	struct ice_chs_chg *tmp;
4439 
4440 	LIST_FOR_EACH_ENTRY(tmp, chgs, ice_chs_chg, list_entry)
4441 		if (tmp->type == ICE_PTG_ES_ADD && tmp->add_prof) {
4442 			u16 off = tmp->prof_id * hw->blk[blk].es.fvw;
4443 			struct ice_pkg_es *p;
4444 			u32 id;
4445 
4446 			id = ice_sect_id(blk, ICE_VEC_TBL);
4447 			p = (struct ice_pkg_es *)
4448 				ice_pkg_buf_alloc_section(bld, id,
4449 							  ice_struct_size(p, es,
4450 									  1) +
4451 							  vec_size -
4452 							  sizeof(p->es[0]));
4453 
4454 			if (!p)
4455 				return ICE_ERR_MAX_LIMIT;
4456 
4457 			p->count = CPU_TO_LE16(1);
4458 			p->offset = CPU_TO_LE16(tmp->prof_id);
4459 
4460 			ice_memcpy(p->es, &hw->blk[blk].es.t[off], vec_size,
4461 				   ICE_NONDMA_TO_NONDMA);
4462 		}
4463 
4464 	return ICE_SUCCESS;
4465 }
4466 
4467 /**
4468  * ice_prof_bld_tcam - build profile ID TCAM changes
4469  * @hw: pointer to the HW struct
4470  * @blk: hardware block
4471  * @bld: the update package buffer build to add to
4472  * @chgs: the list of changes to make in hardware
4473  */
4474 static enum ice_status
4475 ice_prof_bld_tcam(struct ice_hw *hw, enum ice_block blk,
4476 		  struct ice_buf_build *bld, struct LIST_HEAD_TYPE *chgs)
4477 {
4478 	struct ice_chs_chg *tmp;
4479 
4480 	LIST_FOR_EACH_ENTRY(tmp, chgs, ice_chs_chg, list_entry)
4481 		if (tmp->type == ICE_TCAM_ADD && tmp->add_tcam_idx) {
4482 			struct ice_prof_id_section *p;
4483 			u32 id;
4484 
4485 			id = ice_sect_id(blk, ICE_PROF_TCAM);
4486 			p = (struct ice_prof_id_section *)
4487 				ice_pkg_buf_alloc_section(bld, id,
4488 							  ice_struct_size(p,
4489 									  entry,
4490 									  1));
4491 
4492 			if (!p)
4493 				return ICE_ERR_MAX_LIMIT;
4494 
4495 			p->count = CPU_TO_LE16(1);
4496 			p->entry[0].addr = CPU_TO_LE16(tmp->tcam_idx);
4497 			p->entry[0].prof_id = tmp->prof_id;
4498 
4499 			ice_memcpy(p->entry[0].key,
4500 				   &hw->blk[blk].prof.t[tmp->tcam_idx].key,
4501 				   sizeof(hw->blk[blk].prof.t->key),
4502 				   ICE_NONDMA_TO_NONDMA);
4503 		}
4504 
4505 	return ICE_SUCCESS;
4506 }
4507 
4508 /**
4509  * ice_prof_bld_xlt1 - build XLT1 changes
4510  * @blk: hardware block
4511  * @bld: the update package buffer build to add to
4512  * @chgs: the list of changes to make in hardware
4513  */
4514 static enum ice_status
4515 ice_prof_bld_xlt1(enum ice_block blk, struct ice_buf_build *bld,
4516 		  struct LIST_HEAD_TYPE *chgs)
4517 {
4518 	struct ice_chs_chg *tmp;
4519 
4520 	LIST_FOR_EACH_ENTRY(tmp, chgs, ice_chs_chg, list_entry)
4521 		if (tmp->type == ICE_PTG_ES_ADD && tmp->add_ptg) {
4522 			struct ice_xlt1_section *p;
4523 			u32 id;
4524 
4525 			id = ice_sect_id(blk, ICE_XLT1);
4526 			p = (struct ice_xlt1_section *)
4527 				ice_pkg_buf_alloc_section(bld, id,
4528 							  ice_struct_size(p,
4529 									  value,
4530 									  1));
4531 
4532 			if (!p)
4533 				return ICE_ERR_MAX_LIMIT;
4534 
4535 			p->count = CPU_TO_LE16(1);
4536 			p->offset = CPU_TO_LE16(tmp->ptype);
4537 			p->value[0] = tmp->ptg;
4538 		}
4539 
4540 	return ICE_SUCCESS;
4541 }
4542 
4543 /**
4544  * ice_prof_bld_xlt2 - build XLT2 changes
4545  * @blk: hardware block
4546  * @bld: the update package buffer build to add to
4547  * @chgs: the list of changes to make in hardware
4548  */
4549 static enum ice_status
4550 ice_prof_bld_xlt2(enum ice_block blk, struct ice_buf_build *bld,
4551 		  struct LIST_HEAD_TYPE *chgs)
4552 {
4553 	struct ice_chs_chg *tmp;
4554 
4555 	LIST_FOR_EACH_ENTRY(tmp, chgs, ice_chs_chg, list_entry) {
4556 		struct ice_xlt2_section *p;
4557 		u32 id;
4558 
4559 		switch (tmp->type) {
4560 		case ICE_VSIG_ADD:
4561 		case ICE_VSI_MOVE:
4562 		case ICE_VSIG_REM:
4563 			id = ice_sect_id(blk, ICE_XLT2);
4564 			p = (struct ice_xlt2_section *)
4565 				ice_pkg_buf_alloc_section(bld, id,
4566 							  ice_struct_size(p,
4567 									  value,
4568 									  1));
4569 
4570 			if (!p)
4571 				return ICE_ERR_MAX_LIMIT;
4572 
4573 			p->count = CPU_TO_LE16(1);
4574 			p->offset = CPU_TO_LE16(tmp->vsi);
4575 			p->value[0] = CPU_TO_LE16(tmp->vsig);
4576 			break;
4577 		default:
4578 			break;
4579 		}
4580 	}
4581 
4582 	return ICE_SUCCESS;
4583 }
4584 
4585 /**
4586  * ice_upd_prof_hw - update hardware using the change list
4587  * @hw: pointer to the HW struct
4588  * @blk: hardware block
4589  * @chgs: the list of changes to make in hardware
4590  */
4591 static enum ice_status
4592 ice_upd_prof_hw(struct ice_hw *hw, enum ice_block blk,
4593 		struct LIST_HEAD_TYPE *chgs)
4594 {
4595 	struct ice_buf_build *b;
4596 	struct ice_chs_chg *tmp;
4597 	enum ice_status status;
4598 	u16 pkg_sects;
4599 	u16 xlt1 = 0;
4600 	u16 xlt2 = 0;
4601 	u16 tcam = 0;
4602 	u16 es = 0;
4603 	u16 sects;
4604 
4605 	/* count number of sections we need */
4606 	LIST_FOR_EACH_ENTRY(tmp, chgs, ice_chs_chg, list_entry) {
4607 		switch (tmp->type) {
4608 		case ICE_PTG_ES_ADD:
4609 			if (tmp->add_ptg)
4610 				xlt1++;
4611 			if (tmp->add_prof)
4612 				es++;
4613 			break;
4614 		case ICE_TCAM_ADD:
4615 			tcam++;
4616 			break;
4617 		case ICE_VSIG_ADD:
4618 		case ICE_VSI_MOVE:
4619 		case ICE_VSIG_REM:
4620 			xlt2++;
4621 			break;
4622 		default:
4623 			break;
4624 		}
4625 	}
4626 	sects = xlt1 + xlt2 + tcam + es;
4627 
4628 	if (!sects)
4629 		return ICE_SUCCESS;
4630 
4631 	/* Build update package buffer */
4632 	b = ice_pkg_buf_alloc(hw);
4633 	if (!b)
4634 		return ICE_ERR_NO_MEMORY;
4635 
4636 	status = ice_pkg_buf_reserve_section(b, sects);
4637 	if (status)
4638 		goto error_tmp;
4639 
4640 	/* Preserve order of table update: ES, TCAM, PTG, VSIG */
4641 	if (es) {
4642 		status = ice_prof_bld_es(hw, blk, b, chgs);
4643 		if (status)
4644 			goto error_tmp;
4645 	}
4646 
4647 	if (tcam) {
4648 		status = ice_prof_bld_tcam(hw, blk, b, chgs);
4649 		if (status)
4650 			goto error_tmp;
4651 	}
4652 
4653 	if (xlt1) {
4654 		status = ice_prof_bld_xlt1(blk, b, chgs);
4655 		if (status)
4656 			goto error_tmp;
4657 	}
4658 
4659 	if (xlt2) {
4660 		status = ice_prof_bld_xlt2(blk, b, chgs);
4661 		if (status)
4662 			goto error_tmp;
4663 	}
4664 
4665 	/* After package buffer build check if the section count in buffer is
4666 	 * non-zero and matches the number of sections detected for package
4667 	 * update.
4668 	 */
4669 	pkg_sects = ice_pkg_buf_get_active_sections(b);
4670 	if (!pkg_sects || pkg_sects != sects) {
4671 		status = ICE_ERR_INVAL_SIZE;
4672 		goto error_tmp;
4673 	}
4674 
4675 	/* update package */
4676 	status = ice_update_pkg(hw, ice_pkg_buf(b), 1);
4677 	if (status == ICE_ERR_AQ_ERROR)
4678 		ice_debug(hw, ICE_DBG_INIT, "Unable to update HW profile\n");
4679 
4680 error_tmp:
4681 	ice_pkg_buf_free(hw, b);
4682 	return status;
4683 }
4684 
4685 /**
4686  * ice_update_fd_mask - set Flow Director Field Vector mask for a profile
4687  * @hw: pointer to the HW struct
4688  * @prof_id: profile ID
4689  * @mask_sel: mask select
4690  *
4691  * This function enable any of the masks selected by the mask select parameter
4692  * for the profile specified.
4693  */
4694 static void ice_update_fd_mask(struct ice_hw *hw, u16 prof_id, u32 mask_sel)
4695 {
4696 	wr32(hw, GLQF_FDMASK_SEL(prof_id), mask_sel);
4697 
4698 	ice_debug(hw, ICE_DBG_INIT, "fd mask(%d): %x = %x\n", prof_id,
4699 		  GLQF_FDMASK_SEL(prof_id), mask_sel);
4700 }
4701 
4702 struct ice_fd_src_dst_pair {
4703 	u8 prot_id;
4704 	u8 count;
4705 	u16 off;
4706 };
4707 
4708 static const struct ice_fd_src_dst_pair ice_fd_pairs[] = {
4709 	/* These are defined in pairs */
4710 	{ ICE_PROT_IPV4_OF_OR_S, 2, 12 },
4711 	{ ICE_PROT_IPV4_OF_OR_S, 2, 16 },
4712 
4713 	{ ICE_PROT_IPV4_IL, 2, 12 },
4714 	{ ICE_PROT_IPV4_IL, 2, 16 },
4715 
4716 	{ ICE_PROT_IPV6_OF_OR_S, 8, 8 },
4717 	{ ICE_PROT_IPV6_OF_OR_S, 8, 24 },
4718 
4719 	{ ICE_PROT_IPV6_IL, 8, 8 },
4720 	{ ICE_PROT_IPV6_IL, 8, 24 },
4721 
4722 	{ ICE_PROT_TCP_IL, 1, 0 },
4723 	{ ICE_PROT_TCP_IL, 1, 2 },
4724 
4725 	{ ICE_PROT_UDP_OF, 1, 0 },
4726 	{ ICE_PROT_UDP_OF, 1, 2 },
4727 
4728 	{ ICE_PROT_UDP_IL_OR_S, 1, 0 },
4729 	{ ICE_PROT_UDP_IL_OR_S, 1, 2 },
4730 
4731 	{ ICE_PROT_SCTP_IL, 1, 0 },
4732 	{ ICE_PROT_SCTP_IL, 1, 2 }
4733 };
4734 
4735 #define ICE_FD_SRC_DST_PAIR_COUNT	ARRAY_SIZE(ice_fd_pairs)
4736 
4737 /**
4738  * ice_update_fd_swap - set register appropriately for a FD FV extraction
4739  * @hw: pointer to the HW struct
4740  * @prof_id: profile ID
4741  * @es: extraction sequence (length of array is determined by the block)
4742  */
4743 static enum ice_status
4744 ice_update_fd_swap(struct ice_hw *hw, u16 prof_id, struct ice_fv_word *es)
4745 {
4746 	ice_declare_bitmap(pair_list, ICE_FD_SRC_DST_PAIR_COUNT);
4747 	u8 pair_start[ICE_FD_SRC_DST_PAIR_COUNT] = { 0 };
4748 #define ICE_FD_FV_NOT_FOUND (-2)
4749 	s8 first_free = ICE_FD_FV_NOT_FOUND;
4750 	u8 used[ICE_MAX_FV_WORDS] = { 0 };
4751 	s8 orig_free, si;
4752 	u32 mask_sel = 0;
4753 	u8 i, j, k;
4754 
4755 	ice_zero_bitmap(pair_list, ICE_FD_SRC_DST_PAIR_COUNT);
4756 
4757 	/* This code assumes that the Flow Director field vectors are assigned
4758 	 * from the end of the FV indexes working towards the zero index, that
4759 	 * only complete fields will be included and will be consecutive, and
4760 	 * that there are no gaps between valid indexes.
4761 	 */
4762 
4763 	/* Determine swap fields present */
4764 	for (i = 0; i < hw->blk[ICE_BLK_FD].es.fvw; i++) {
4765 		/* Find the first free entry, assuming right to left population.
4766 		 * This is where we can start adding additional pairs if needed.
4767 		 */
4768 		if (first_free == ICE_FD_FV_NOT_FOUND && es[i].prot_id !=
4769 		    ICE_PROT_INVALID)
4770 			first_free = i - 1;
4771 
4772 		for (j = 0; j < ICE_FD_SRC_DST_PAIR_COUNT; j++)
4773 			if (es[i].prot_id == ice_fd_pairs[j].prot_id &&
4774 			    es[i].off == ice_fd_pairs[j].off) {
4775 				ice_set_bit(j, pair_list);
4776 				pair_start[j] = i;
4777 			}
4778 	}
4779 
4780 	orig_free = first_free;
4781 
4782 	/* determine missing swap fields that need to be added */
4783 	for (i = 0; i < ICE_FD_SRC_DST_PAIR_COUNT; i += 2) {
4784 		u8 bit1 = ice_is_bit_set(pair_list, i + 1);
4785 		u8 bit0 = ice_is_bit_set(pair_list, i);
4786 
4787 		if (bit0 ^ bit1) {
4788 			u8 index;
4789 
4790 			/* add the appropriate 'paired' entry */
4791 			if (!bit0)
4792 				index = i;
4793 			else
4794 				index = i + 1;
4795 
4796 			/* check for room */
4797 			if (first_free + 1 < (s8)ice_fd_pairs[index].count)
4798 				return ICE_ERR_MAX_LIMIT;
4799 
4800 			/* place in extraction sequence */
4801 			for (k = 0; k < ice_fd_pairs[index].count; k++) {
4802 				es[first_free - k].prot_id =
4803 					ice_fd_pairs[index].prot_id;
4804 				es[first_free - k].off =
4805 					ice_fd_pairs[index].off + (k * 2);
4806 
4807 				if (k > first_free)
4808 					return ICE_ERR_OUT_OF_RANGE;
4809 
4810 				/* keep track of non-relevant fields */
4811 				mask_sel |= BIT(first_free - k);
4812 			}
4813 
4814 			pair_start[index] = first_free;
4815 			first_free -= ice_fd_pairs[index].count;
4816 		}
4817 	}
4818 
4819 	/* fill in the swap array */
4820 	si = hw->blk[ICE_BLK_FD].es.fvw - 1;
4821 	while (si >= 0) {
4822 		u8 indexes_used = 1;
4823 
4824 		/* assume flat at this index */
4825 #define ICE_SWAP_VALID	0x80
4826 		used[si] = si | ICE_SWAP_VALID;
4827 
4828 		if (orig_free == ICE_FD_FV_NOT_FOUND || si <= orig_free) {
4829 			si -= indexes_used;
4830 			continue;
4831 		}
4832 
4833 		/* check for a swap location */
4834 		for (j = 0; j < ICE_FD_SRC_DST_PAIR_COUNT; j++)
4835 			if (es[si].prot_id == ice_fd_pairs[j].prot_id &&
4836 			    es[si].off == ice_fd_pairs[j].off) {
4837 				u8 idx;
4838 
4839 				/* determine the appropriate matching field */
4840 				idx = j + ((j % 2) ? -1 : 1);
4841 
4842 				indexes_used = ice_fd_pairs[idx].count;
4843 				for (k = 0; k < indexes_used; k++) {
4844 					used[si - k] = (pair_start[idx] - k) |
4845 						ICE_SWAP_VALID;
4846 				}
4847 
4848 				break;
4849 			}
4850 
4851 		si -= indexes_used;
4852 	}
4853 
4854 	/* for each set of 4 swap and 4 inset indexes, write the appropriate
4855 	 * register
4856 	 */
4857 	for (j = 0; j < hw->blk[ICE_BLK_FD].es.fvw / 4; j++) {
4858 		u32 raw_swap = 0;
4859 		u32 raw_in = 0;
4860 
4861 		for (k = 0; k < 4; k++) {
4862 			u8 idx;
4863 
4864 			idx = (j * 4) + k;
4865 			if (used[idx] && !(mask_sel & BIT(idx))) {
4866 				raw_swap |= used[idx] << (k * BITS_PER_BYTE);
4867 #define ICE_INSET_DFLT 0x9f
4868 				raw_in |= ICE_INSET_DFLT << (k * BITS_PER_BYTE);
4869 			}
4870 		}
4871 
4872 		/* write the appropriate swap register set */
4873 		wr32(hw, GLQF_FDSWAP(prof_id, j), raw_swap);
4874 
4875 		ice_debug(hw, ICE_DBG_INIT, "swap wr(%d, %d): %x = %08x\n",
4876 			  prof_id, j, GLQF_FDSWAP(prof_id, j), raw_swap);
4877 
4878 		/* write the appropriate inset register set */
4879 		wr32(hw, GLQF_FDINSET(prof_id, j), raw_in);
4880 
4881 		ice_debug(hw, ICE_DBG_INIT, "inset wr(%d, %d): %x = %08x\n",
4882 			  prof_id, j, GLQF_FDINSET(prof_id, j), raw_in);
4883 	}
4884 
4885 	/* initially clear the mask select for this profile */
4886 	ice_update_fd_mask(hw, prof_id, 0);
4887 
4888 	return ICE_SUCCESS;
4889 }
4890 
4891 /* The entries here needs to match the order of enum ice_ptype_attrib */
4892 static const struct ice_ptype_attrib_info ice_ptype_attributes[] = {
4893 	{ ICE_GTP_PDU_EH,	ICE_GTP_PDU_FLAG_MASK },
4894 	{ ICE_GTP_SESSION,	ICE_GTP_FLAGS_MASK },
4895 	{ ICE_GTP_DOWNLINK,	ICE_GTP_FLAGS_MASK },
4896 	{ ICE_GTP_UPLINK,	ICE_GTP_FLAGS_MASK },
4897 };
4898 
4899 /**
4900  * ice_get_ptype_attrib_info - get ptype attribute information
4901  * @type: attribute type
4902  * @info: pointer to variable to the attribute information
4903  */
4904 static void
4905 ice_get_ptype_attrib_info(enum ice_ptype_attrib_type type,
4906 			  struct ice_ptype_attrib_info *info)
4907 {
4908 	*info = ice_ptype_attributes[type];
4909 }
4910 
4911 /**
4912  * ice_add_prof_attrib - add any PTG with attributes to profile
4913  * @prof: pointer to the profile to which PTG entries will be added
4914  * @ptg: PTG to be added
4915  * @ptype: PTYPE that needs to be looked up
4916  * @attr: array of attributes that will be considered
4917  * @attr_cnt: number of elements in the attribute array
4918  */
4919 static enum ice_status
4920 ice_add_prof_attrib(struct ice_prof_map *prof, u8 ptg, u16 ptype,
4921 		    const struct ice_ptype_attributes *attr, u16 attr_cnt)
4922 {
4923 	bool found = false;
4924 	u16 i;
4925 
4926 	for (i = 0; i < attr_cnt; i++) {
4927 		if (attr[i].ptype == ptype) {
4928 			found = true;
4929 
4930 			prof->ptg[prof->ptg_cnt] = ptg;
4931 			ice_get_ptype_attrib_info(attr[i].attrib,
4932 						  &prof->attr[prof->ptg_cnt]);
4933 
4934 			if (++prof->ptg_cnt >= ICE_MAX_PTG_PER_PROFILE)
4935 				return ICE_ERR_MAX_LIMIT;
4936 		}
4937 	}
4938 
4939 	if (!found)
4940 		return ICE_ERR_DOES_NOT_EXIST;
4941 
4942 	return ICE_SUCCESS;
4943 }
4944 
4945 /**
4946  * ice_add_prof - add profile
4947  * @hw: pointer to the HW struct
4948  * @blk: hardware block
4949  * @id: profile tracking ID
4950  * @ptypes: array of bitmaps indicating ptypes (ICE_FLOW_PTYPE_MAX bits)
4951  * @attr: array of attributes
4952  * @attr_cnt: number of elements in attrib array
4953  * @es: extraction sequence (length of array is determined by the block)
4954  * @masks: mask for extraction sequence
4955  *
4956  * This function registers a profile, which matches a set of PTYPES with a
4957  * particular extraction sequence. While the hardware profile is allocated
4958  * it will not be written until the first call to ice_add_flow that specifies
4959  * the ID value used here.
4960  */
4961 enum ice_status
4962 ice_add_prof(struct ice_hw *hw, enum ice_block blk, u64 id, u8 ptypes[],
4963 	     const struct ice_ptype_attributes *attr, u16 attr_cnt,
4964 	     struct ice_fv_word *es, u16 *masks)
4965 {
4966 	u32 bytes = DIVIDE_AND_ROUND_UP(ICE_FLOW_PTYPE_MAX, BITS_PER_BYTE);
4967 	ice_declare_bitmap(ptgs_used, ICE_XLT1_CNT);
4968 	struct ice_prof_map *prof;
4969 	enum ice_status status;
4970 	u8 byte = 0;
4971 	u8 prof_id;
4972 
4973 	ice_zero_bitmap(ptgs_used, ICE_XLT1_CNT);
4974 
4975 	ice_acquire_lock(&hw->blk[blk].es.prof_map_lock);
4976 
4977 	/* search for existing profile */
4978 	status = ice_find_prof_id_with_mask(hw, blk, es, masks, &prof_id);
4979 	if (status) {
4980 		/* allocate profile ID */
4981 		status = ice_alloc_prof_id(hw, blk, &prof_id);
4982 		if (status)
4983 			goto err_ice_add_prof;
4984 		if (blk == ICE_BLK_FD) {
4985 			/* For Flow Director block, the extraction sequence may
4986 			 * need to be altered in the case where there are paired
4987 			 * fields that have no match. This is necessary because
4988 			 * for Flow Director, src and dest fields need to paired
4989 			 * for filter programming and these values are swapped
4990 			 * during Tx.
4991 			 */
4992 			status = ice_update_fd_swap(hw, prof_id, es);
4993 			if (status)
4994 				goto err_ice_add_prof;
4995 		}
4996 		status = ice_update_prof_masking(hw, blk, prof_id, masks);
4997 		if (status)
4998 			goto err_ice_add_prof;
4999 
5000 		/* and write new es */
5001 		ice_write_es(hw, blk, prof_id, es);
5002 	}
5003 
5004 	ice_prof_inc_ref(hw, blk, prof_id);
5005 
5006 	/* add profile info */
5007 
5008 	prof = (struct ice_prof_map *)ice_malloc(hw, sizeof(*prof));
5009 	if (!prof)
5010 		goto err_ice_add_prof;
5011 
5012 	prof->profile_cookie = id;
5013 	prof->prof_id = prof_id;
5014 	prof->ptg_cnt = 0;
5015 	prof->context = 0;
5016 
5017 	/* build list of ptgs */
5018 	while (bytes && prof->ptg_cnt < ICE_MAX_PTG_PER_PROFILE) {
5019 		u8 bit;
5020 
5021 		if (!ptypes[byte]) {
5022 			bytes--;
5023 			byte++;
5024 			continue;
5025 		}
5026 
5027 		/* Examine 8 bits per byte */
5028 		ice_for_each_set_bit(bit, (ice_bitmap_t *)&ptypes[byte],
5029 				     BITS_PER_BYTE) {
5030 			u16 ptype;
5031 			u8 ptg;
5032 
5033 			ptype = byte * BITS_PER_BYTE + bit;
5034 
5035 			/* The package should place all ptypes in a non-zero
5036 			 * PTG, so the following call should never fail.
5037 			 */
5038 			if (ice_ptg_find_ptype(hw, blk, ptype, &ptg))
5039 				continue;
5040 
5041 			/* If PTG is already added, skip and continue */
5042 			if (ice_is_bit_set(ptgs_used, ptg))
5043 				continue;
5044 
5045 			ice_set_bit(ptg, ptgs_used);
5046 			/* Check to see there are any attributes for this
5047 			 * ptype, and add them if found.
5048 			 */
5049 			status = ice_add_prof_attrib(prof, ptg, ptype, attr,
5050 						     attr_cnt);
5051 			if (status == ICE_ERR_MAX_LIMIT)
5052 				break;
5053 			if (status) {
5054 				/* This is simple a ptype/PTG with no
5055 				 * attribute
5056 				 */
5057 				prof->ptg[prof->ptg_cnt] = ptg;
5058 				prof->attr[prof->ptg_cnt].flags = 0;
5059 				prof->attr[prof->ptg_cnt].mask = 0;
5060 
5061 				if (++prof->ptg_cnt >= ICE_MAX_PTG_PER_PROFILE)
5062 					break;
5063 			}
5064 		}
5065 
5066 		bytes--;
5067 		byte++;
5068 	}
5069 
5070 	LIST_ADD(&prof->list, &hw->blk[blk].es.prof_map);
5071 	status = ICE_SUCCESS;
5072 
5073 err_ice_add_prof:
5074 	ice_release_lock(&hw->blk[blk].es.prof_map_lock);
5075 	return status;
5076 }
5077 
5078 /**
5079  * ice_search_prof_id - Search for a profile tracking ID
5080  * @hw: pointer to the HW struct
5081  * @blk: hardware block
5082  * @id: profile tracking ID
5083  *
5084  * This will search for a profile tracking ID which was previously added.
5085  * The profile map lock should be held before calling this function.
5086  */
5087 struct ice_prof_map *
5088 ice_search_prof_id(struct ice_hw *hw, enum ice_block blk, u64 id)
5089 {
5090 	struct ice_prof_map *entry = NULL;
5091 	struct ice_prof_map *map;
5092 
5093 	LIST_FOR_EACH_ENTRY(map, &hw->blk[blk].es.prof_map, ice_prof_map, list)
5094 		if (map->profile_cookie == id) {
5095 			entry = map;
5096 			break;
5097 		}
5098 
5099 	return entry;
5100 }
5101 
5102 /**
5103  * ice_vsig_prof_id_count - count profiles in a VSIG
5104  * @hw: pointer to the HW struct
5105  * @blk: hardware block
5106  * @vsig: VSIG to remove the profile from
5107  */
5108 static u16
5109 ice_vsig_prof_id_count(struct ice_hw *hw, enum ice_block blk, u16 vsig)
5110 {
5111 	u16 idx = vsig & ICE_VSIG_IDX_M, count = 0;
5112 	struct ice_vsig_prof *p;
5113 
5114 	LIST_FOR_EACH_ENTRY(p, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
5115 			    ice_vsig_prof, list)
5116 		count++;
5117 
5118 	return count;
5119 }
5120 
5121 /**
5122  * ice_rel_tcam_idx - release a TCAM index
5123  * @hw: pointer to the HW struct
5124  * @blk: hardware block
5125  * @idx: the index to release
5126  */
5127 static enum ice_status
5128 ice_rel_tcam_idx(struct ice_hw *hw, enum ice_block blk, u16 idx)
5129 {
5130 	/* Masks to invoke a never match entry */
5131 	u8 vl_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
5132 	u8 dc_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFE, 0xFF, 0xFF, 0xFF, 0xFF };
5133 	u8 nm_msk[ICE_TCAM_KEY_VAL_SZ] = { 0x01, 0x00, 0x00, 0x00, 0x00 };
5134 	enum ice_status status;
5135 
5136 	/* write the TCAM entry */
5137 	status = ice_tcam_write_entry(hw, blk, idx, 0, 0, 0, 0, 0, vl_msk,
5138 				      dc_msk, nm_msk);
5139 	if (status)
5140 		return status;
5141 
5142 	/* release the TCAM entry */
5143 	status = ice_free_tcam_ent(hw, blk, idx);
5144 
5145 	return status;
5146 }
5147 
5148 /**
5149  * ice_rem_prof_id - remove one profile from a VSIG
5150  * @hw: pointer to the HW struct
5151  * @blk: hardware block
5152  * @prof: pointer to profile structure to remove
5153  */
5154 static enum ice_status
5155 ice_rem_prof_id(struct ice_hw *hw, enum ice_block blk,
5156 		struct ice_vsig_prof *prof)
5157 {
5158 	enum ice_status status;
5159 	u16 i;
5160 
5161 	for (i = 0; i < prof->tcam_count; i++)
5162 		if (prof->tcam[i].in_use) {
5163 			prof->tcam[i].in_use = false;
5164 			status = ice_rel_tcam_idx(hw, blk,
5165 						  prof->tcam[i].tcam_idx);
5166 			if (status)
5167 				return ICE_ERR_HW_TABLE;
5168 		}
5169 
5170 	return ICE_SUCCESS;
5171 }
5172 
5173 /**
5174  * ice_rem_vsig - remove VSIG
5175  * @hw: pointer to the HW struct
5176  * @blk: hardware block
5177  * @vsig: the VSIG to remove
5178  * @chg: the change list
5179  */
5180 static enum ice_status
5181 ice_rem_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig,
5182 	     struct LIST_HEAD_TYPE *chg)
5183 {
5184 	u16 idx = vsig & ICE_VSIG_IDX_M;
5185 	struct ice_vsig_vsi *vsi_cur;
5186 	struct ice_vsig_prof *d, *t;
5187 	enum ice_status status;
5188 
5189 	/* remove TCAM entries */
5190 	LIST_FOR_EACH_ENTRY_SAFE(d, t,
5191 				 &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
5192 				 ice_vsig_prof, list) {
5193 		status = ice_rem_prof_id(hw, blk, d);
5194 		if (status)
5195 			return status;
5196 
5197 		LIST_DEL(&d->list);
5198 		ice_free(hw, d);
5199 	}
5200 
5201 	/* Move all VSIS associated with this VSIG to the default VSIG */
5202 	vsi_cur = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
5203 	/* If the VSIG has at least 1 VSI then iterate through the list
5204 	 * and remove the VSIs before deleting the group.
5205 	 */
5206 	if (vsi_cur)
5207 		do {
5208 			struct ice_vsig_vsi *tmp = vsi_cur->next_vsi;
5209 			struct ice_chs_chg *p;
5210 
5211 			p = (struct ice_chs_chg *)ice_malloc(hw, sizeof(*p));
5212 			if (!p)
5213 				return ICE_ERR_NO_MEMORY;
5214 
5215 			p->type = ICE_VSIG_REM;
5216 			p->orig_vsig = vsig;
5217 			p->vsig = ICE_DEFAULT_VSIG;
5218 			p->vsi = vsi_cur - hw->blk[blk].xlt2.vsis;
5219 
5220 			LIST_ADD(&p->list_entry, chg);
5221 
5222 			vsi_cur = tmp;
5223 		} while (vsi_cur);
5224 
5225 	return ice_vsig_free(hw, blk, vsig);
5226 }
5227 
5228 /**
5229  * ice_rem_prof_id_vsig - remove a specific profile from a VSIG
5230  * @hw: pointer to the HW struct
5231  * @blk: hardware block
5232  * @vsig: VSIG to remove the profile from
5233  * @hdl: profile handle indicating which profile to remove
5234  * @chg: list to receive a record of changes
5235  */
5236 static enum ice_status
5237 ice_rem_prof_id_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig, u64 hdl,
5238 		     struct LIST_HEAD_TYPE *chg)
5239 {
5240 	u16 idx = vsig & ICE_VSIG_IDX_M;
5241 	struct ice_vsig_prof *p, *t;
5242 	enum ice_status status;
5243 
5244 	LIST_FOR_EACH_ENTRY_SAFE(p, t,
5245 				 &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
5246 				 ice_vsig_prof, list)
5247 		if (p->profile_cookie == hdl) {
5248 			if (ice_vsig_prof_id_count(hw, blk, vsig) == 1)
5249 				/* this is the last profile, remove the VSIG */
5250 				return ice_rem_vsig(hw, blk, vsig, chg);
5251 
5252 			status = ice_rem_prof_id(hw, blk, p);
5253 			if (!status) {
5254 				LIST_DEL(&p->list);
5255 				ice_free(hw, p);
5256 			}
5257 			return status;
5258 		}
5259 
5260 	return ICE_ERR_DOES_NOT_EXIST;
5261 }
5262 
5263 /**
5264  * ice_rem_flow_all - remove all flows with a particular profile
5265  * @hw: pointer to the HW struct
5266  * @blk: hardware block
5267  * @id: profile tracking ID
5268  */
5269 static enum ice_status
5270 ice_rem_flow_all(struct ice_hw *hw, enum ice_block blk, u64 id)
5271 {
5272 	struct ice_chs_chg *del, *tmp;
5273 	struct LIST_HEAD_TYPE chg;
5274 	enum ice_status status;
5275 	u16 i;
5276 
5277 	INIT_LIST_HEAD(&chg);
5278 
5279 	for (i = 1; i < ICE_MAX_VSIGS; i++)
5280 		if (hw->blk[blk].xlt2.vsig_tbl[i].in_use) {
5281 			if (ice_has_prof_vsig(hw, blk, i, id)) {
5282 				status = ice_rem_prof_id_vsig(hw, blk, i, id,
5283 							      &chg);
5284 				if (status)
5285 					goto err_ice_rem_flow_all;
5286 			}
5287 		}
5288 
5289 	status = ice_upd_prof_hw(hw, blk, &chg);
5290 
5291 err_ice_rem_flow_all:
5292 	LIST_FOR_EACH_ENTRY_SAFE(del, tmp, &chg, ice_chs_chg, list_entry) {
5293 		LIST_DEL(&del->list_entry);
5294 		ice_free(hw, del);
5295 	}
5296 
5297 	return status;
5298 }
5299 
5300 /**
5301  * ice_rem_prof - remove profile
5302  * @hw: pointer to the HW struct
5303  * @blk: hardware block
5304  * @id: profile tracking ID
5305  *
5306  * This will remove the profile specified by the ID parameter, which was
5307  * previously created through ice_add_prof. If any existing entries
5308  * are associated with this profile, they will be removed as well.
5309  */
5310 enum ice_status ice_rem_prof(struct ice_hw *hw, enum ice_block blk, u64 id)
5311 {
5312 	struct ice_prof_map *pmap;
5313 	enum ice_status status;
5314 
5315 	ice_acquire_lock(&hw->blk[blk].es.prof_map_lock);
5316 
5317 	pmap = ice_search_prof_id(hw, blk, id);
5318 	if (!pmap) {
5319 		status = ICE_ERR_DOES_NOT_EXIST;
5320 		goto err_ice_rem_prof;
5321 	}
5322 
5323 	/* remove all flows with this profile */
5324 	status = ice_rem_flow_all(hw, blk, pmap->profile_cookie);
5325 	if (status)
5326 		goto err_ice_rem_prof;
5327 
5328 	/* dereference profile, and possibly remove */
5329 	ice_prof_dec_ref(hw, blk, pmap->prof_id);
5330 
5331 	LIST_DEL(&pmap->list);
5332 	ice_free(hw, pmap);
5333 
5334 err_ice_rem_prof:
5335 	ice_release_lock(&hw->blk[blk].es.prof_map_lock);
5336 	return status;
5337 }
5338 
5339 /**
5340  * ice_get_prof - get profile
5341  * @hw: pointer to the HW struct
5342  * @blk: hardware block
5343  * @hdl: profile handle
5344  * @chg: change list
5345  */
5346 static enum ice_status
5347 ice_get_prof(struct ice_hw *hw, enum ice_block blk, u64 hdl,
5348 	     struct LIST_HEAD_TYPE *chg)
5349 {
5350 	enum ice_status status = ICE_SUCCESS;
5351 	struct ice_prof_map *map;
5352 	struct ice_chs_chg *p;
5353 	u16 i;
5354 
5355 	ice_acquire_lock(&hw->blk[blk].es.prof_map_lock);
5356 	/* Get the details on the profile specified by the handle ID */
5357 	map = ice_search_prof_id(hw, blk, hdl);
5358 	if (!map) {
5359 		status = ICE_ERR_DOES_NOT_EXIST;
5360 		goto err_ice_get_prof;
5361 	}
5362 
5363 	for (i = 0; i < map->ptg_cnt; i++)
5364 		if (!hw->blk[blk].es.written[map->prof_id]) {
5365 			/* add ES to change list */
5366 			p = (struct ice_chs_chg *)ice_malloc(hw, sizeof(*p));
5367 			if (!p) {
5368 				status = ICE_ERR_NO_MEMORY;
5369 				goto err_ice_get_prof;
5370 			}
5371 
5372 			p->type = ICE_PTG_ES_ADD;
5373 			p->ptype = 0;
5374 			p->ptg = map->ptg[i];
5375 			p->attr = map->attr[i];
5376 			p->add_ptg = 0;
5377 
5378 			p->add_prof = 1;
5379 			p->prof_id = map->prof_id;
5380 
5381 			hw->blk[blk].es.written[map->prof_id] = true;
5382 
5383 			LIST_ADD(&p->list_entry, chg);
5384 		}
5385 
5386 err_ice_get_prof:
5387 	ice_release_lock(&hw->blk[blk].es.prof_map_lock);
5388 	/* let caller clean up the change list */
5389 	return status;
5390 }
5391 
5392 /**
5393  * ice_get_profs_vsig - get a copy of the list of profiles from a VSIG
5394  * @hw: pointer to the HW struct
5395  * @blk: hardware block
5396  * @vsig: VSIG from which to copy the list
5397  * @lst: output list
5398  *
5399  * This routine makes a copy of the list of profiles in the specified VSIG.
5400  */
5401 static enum ice_status
5402 ice_get_profs_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig,
5403 		   struct LIST_HEAD_TYPE *lst)
5404 {
5405 	struct ice_vsig_prof *ent1, *ent2;
5406 	u16 idx = vsig & ICE_VSIG_IDX_M;
5407 
5408 	LIST_FOR_EACH_ENTRY(ent1, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
5409 			    ice_vsig_prof, list) {
5410 		struct ice_vsig_prof *p;
5411 
5412 		/* copy to the input list */
5413 		p = (struct ice_vsig_prof *)ice_memdup(hw, ent1, sizeof(*p),
5414 						       ICE_NONDMA_TO_NONDMA);
5415 		if (!p)
5416 			goto err_ice_get_profs_vsig;
5417 
5418 		LIST_ADD_TAIL(&p->list, lst);
5419 	}
5420 
5421 	return ICE_SUCCESS;
5422 
5423 err_ice_get_profs_vsig:
5424 	LIST_FOR_EACH_ENTRY_SAFE(ent1, ent2, lst, ice_vsig_prof, list) {
5425 		LIST_DEL(&ent1->list);
5426 		ice_free(hw, ent1);
5427 	}
5428 
5429 	return ICE_ERR_NO_MEMORY;
5430 }
5431 
5432 /**
5433  * ice_add_prof_to_lst - add profile entry to a list
5434  * @hw: pointer to the HW struct
5435  * @blk: hardware block
5436  * @lst: the list to be added to
5437  * @hdl: profile handle of entry to add
5438  */
5439 static enum ice_status
5440 ice_add_prof_to_lst(struct ice_hw *hw, enum ice_block blk,
5441 		    struct LIST_HEAD_TYPE *lst, u64 hdl)
5442 {
5443 	enum ice_status status = ICE_SUCCESS;
5444 	struct ice_prof_map *map;
5445 	struct ice_vsig_prof *p;
5446 	u16 i;
5447 
5448 	ice_acquire_lock(&hw->blk[blk].es.prof_map_lock);
5449 	map = ice_search_prof_id(hw, blk, hdl);
5450 	if (!map) {
5451 		status = ICE_ERR_DOES_NOT_EXIST;
5452 		goto err_ice_add_prof_to_lst;
5453 	}
5454 
5455 	p = (struct ice_vsig_prof *)ice_malloc(hw, sizeof(*p));
5456 	if (!p) {
5457 		status = ICE_ERR_NO_MEMORY;
5458 		goto err_ice_add_prof_to_lst;
5459 	}
5460 
5461 	p->profile_cookie = map->profile_cookie;
5462 	p->prof_id = map->prof_id;
5463 	p->tcam_count = map->ptg_cnt;
5464 
5465 	for (i = 0; i < map->ptg_cnt; i++) {
5466 		p->tcam[i].prof_id = map->prof_id;
5467 		p->tcam[i].tcam_idx = ICE_INVALID_TCAM;
5468 		p->tcam[i].ptg = map->ptg[i];
5469 		p->tcam[i].attr = map->attr[i];
5470 	}
5471 
5472 	LIST_ADD(&p->list, lst);
5473 
5474 err_ice_add_prof_to_lst:
5475 	ice_release_lock(&hw->blk[blk].es.prof_map_lock);
5476 	return status;
5477 }
5478 
5479 /**
5480  * ice_move_vsi - move VSI to another VSIG
5481  * @hw: pointer to the HW struct
5482  * @blk: hardware block
5483  * @vsi: the VSI to move
5484  * @vsig: the VSIG to move the VSI to
5485  * @chg: the change list
5486  */
5487 static enum ice_status
5488 ice_move_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 vsig,
5489 	     struct LIST_HEAD_TYPE *chg)
5490 {
5491 	enum ice_status status;
5492 	struct ice_chs_chg *p;
5493 	u16 orig_vsig;
5494 
5495 	p = (struct ice_chs_chg *)ice_malloc(hw, sizeof(*p));
5496 	if (!p)
5497 		return ICE_ERR_NO_MEMORY;
5498 
5499 	status = ice_vsig_find_vsi(hw, blk, vsi, &orig_vsig);
5500 	if (!status)
5501 		status = ice_vsig_add_mv_vsi(hw, blk, vsi, vsig);
5502 
5503 	if (status) {
5504 		ice_free(hw, p);
5505 		return status;
5506 	}
5507 
5508 	p->type = ICE_VSI_MOVE;
5509 	p->vsi = vsi;
5510 	p->orig_vsig = orig_vsig;
5511 	p->vsig = vsig;
5512 
5513 	LIST_ADD(&p->list_entry, chg);
5514 
5515 	return ICE_SUCCESS;
5516 }
5517 
5518 /**
5519  * ice_set_tcam_flags - set TCAM flag don't care mask
5520  * @mask: mask for flags
5521  * @dc_mask: pointer to the don't care mask
5522  */
5523 static void ice_set_tcam_flags(u16 mask, u8 dc_mask[ICE_TCAM_KEY_VAL_SZ])
5524 {
5525 	u16 *flag_word;
5526 
5527 	/* flags are lowest u16 */
5528 	flag_word = (u16 *)dc_mask;
5529 	*flag_word = ~mask;
5530 }
5531 
5532 /**
5533  * ice_rem_chg_tcam_ent - remove a specific TCAM entry from change list
5534  * @hw: pointer to the HW struct
5535  * @idx: the index of the TCAM entry to remove
5536  * @chg: the list of change structures to search
5537  */
5538 static void
5539 ice_rem_chg_tcam_ent(struct ice_hw *hw, u16 idx, struct LIST_HEAD_TYPE *chg)
5540 {
5541 	struct ice_chs_chg *pos, *tmp;
5542 
5543 	LIST_FOR_EACH_ENTRY_SAFE(tmp, pos, chg, ice_chs_chg, list_entry)
5544 		if (tmp->type == ICE_TCAM_ADD && tmp->tcam_idx == idx) {
5545 			LIST_DEL(&tmp->list_entry);
5546 			ice_free(hw, tmp);
5547 		}
5548 }
5549 
5550 /**
5551  * ice_prof_tcam_ena_dis - add enable or disable TCAM change
5552  * @hw: pointer to the HW struct
5553  * @blk: hardware block
5554  * @enable: true to enable, false to disable
5555  * @vsig: the VSIG of the TCAM entry
5556  * @tcam: pointer the TCAM info structure of the TCAM to disable
5557  * @chg: the change list
5558  *
5559  * This function appends an enable or disable TCAM entry in the change log
5560  */
5561 static enum ice_status
5562 ice_prof_tcam_ena_dis(struct ice_hw *hw, enum ice_block blk, bool enable,
5563 		      u16 vsig, struct ice_tcam_inf *tcam,
5564 		      struct LIST_HEAD_TYPE *chg)
5565 {
5566 	enum ice_status status;
5567 	struct ice_chs_chg *p;
5568 
5569 	u8 vl_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
5570 	u8 dc_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0x00, 0x00, 0x00 };
5571 	u8 nm_msk[ICE_TCAM_KEY_VAL_SZ] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
5572 
5573 	/* if disabling, free the TCAM */
5574 	if (!enable) {
5575 		status = ice_rel_tcam_idx(hw, blk, tcam->tcam_idx);
5576 
5577 		/* if we have already created a change for this TCAM entry, then
5578 		 * we need to remove that entry, in order to prevent writing to
5579 		 * a TCAM entry we no longer will have ownership of.
5580 		 */
5581 		ice_rem_chg_tcam_ent(hw, tcam->tcam_idx, chg);
5582 		tcam->tcam_idx = 0;
5583 		tcam->in_use = 0;
5584 		return status;
5585 	}
5586 
5587 	/* for re-enabling, reallocate a TCAM */
5588 	/* for entries with empty attribute masks, allocate entry from
5589 	 * the bottom of the TCAM table; otherwise, allocate from the
5590 	 * top of the table in order to give it higher priority
5591 	 */
5592 	status = ice_alloc_tcam_ent(hw, blk, tcam->attr.mask == 0,
5593 				    &tcam->tcam_idx);
5594 	if (status)
5595 		return status;
5596 
5597 	/* add TCAM to change list */
5598 	p = (struct ice_chs_chg *)ice_malloc(hw, sizeof(*p));
5599 	if (!p)
5600 		return ICE_ERR_NO_MEMORY;
5601 
5602 	/* set don't care masks for TCAM flags */
5603 	ice_set_tcam_flags(tcam->attr.mask, dc_msk);
5604 
5605 	status = ice_tcam_write_entry(hw, blk, tcam->tcam_idx, tcam->prof_id,
5606 				      tcam->ptg, vsig, 0, tcam->attr.flags,
5607 				      vl_msk, dc_msk, nm_msk);
5608 	if (status)
5609 		goto err_ice_prof_tcam_ena_dis;
5610 
5611 	tcam->in_use = 1;
5612 
5613 	p->type = ICE_TCAM_ADD;
5614 	p->add_tcam_idx = true;
5615 	p->prof_id = tcam->prof_id;
5616 	p->ptg = tcam->ptg;
5617 	p->vsig = 0;
5618 	p->tcam_idx = tcam->tcam_idx;
5619 
5620 	/* log change */
5621 	LIST_ADD(&p->list_entry, chg);
5622 
5623 	return ICE_SUCCESS;
5624 
5625 err_ice_prof_tcam_ena_dis:
5626 	ice_free(hw, p);
5627 	return status;
5628 }
5629 
5630 /**
5631  * ice_ptg_attr_in_use - determine if PTG and attribute pair is in use
5632  * @ptg_attr: pointer to the PTG and attribute pair to check
5633  * @ptgs_used: bitmap that denotes which PTGs are in use
5634  * @attr_used: array of PTG and attributes pairs already used
5635  * @attr_cnt: count of entries in the attr_used array
5636  */
5637 static bool
5638 ice_ptg_attr_in_use(struct ice_tcam_inf *ptg_attr, ice_bitmap_t *ptgs_used,
5639 		    struct ice_tcam_inf *attr_used[], u16 attr_cnt)
5640 {
5641 	u16 i;
5642 
5643 	if (!ice_is_bit_set(ptgs_used, ptg_attr->ptg))
5644 		return false;
5645 
5646 	/* the PTG is used, so now look for correct attributes */
5647 	for (i = 0; i < attr_cnt; i++)
5648 		if (attr_used[i]->ptg == ptg_attr->ptg &&
5649 		    attr_used[i]->attr.flags == ptg_attr->attr.flags &&
5650 		    attr_used[i]->attr.mask == ptg_attr->attr.mask)
5651 			return true;
5652 
5653 	return false;
5654 }
5655 
5656 /**
5657  * ice_adj_prof_priorities - adjust profile based on priorities
5658  * @hw: pointer to the HW struct
5659  * @blk: hardware block
5660  * @vsig: the VSIG for which to adjust profile priorities
5661  * @chg: the change list
5662  */
5663 static enum ice_status
5664 ice_adj_prof_priorities(struct ice_hw *hw, enum ice_block blk, u16 vsig,
5665 			struct LIST_HEAD_TYPE *chg)
5666 {
5667 	ice_declare_bitmap(ptgs_used, ICE_XLT1_CNT);
5668 	struct ice_tcam_inf **attr_used;
5669 	enum ice_status status = ICE_SUCCESS;
5670 	struct ice_vsig_prof *t;
5671 	u16 attr_used_cnt = 0;
5672 	u16 idx;
5673 
5674 #define ICE_MAX_PTG_ATTRS	1024
5675 	attr_used = (struct ice_tcam_inf **)ice_calloc(hw, ICE_MAX_PTG_ATTRS,
5676 						       sizeof(*attr_used));
5677 	if (!attr_used)
5678 		return ICE_ERR_NO_MEMORY;
5679 
5680 	ice_zero_bitmap(ptgs_used, ICE_XLT1_CNT);
5681 	idx = vsig & ICE_VSIG_IDX_M;
5682 
5683 	/* Priority is based on the order in which the profiles are added. The
5684 	 * newest added profile has highest priority and the oldest added
5685 	 * profile has the lowest priority. Since the profile property list for
5686 	 * a VSIG is sorted from newest to oldest, this code traverses the list
5687 	 * in order and enables the first of each PTG that it finds (that is not
5688 	 * already enabled); it also disables any duplicate PTGs that it finds
5689 	 * in the older profiles (that are currently enabled).
5690 	 */
5691 
5692 	LIST_FOR_EACH_ENTRY(t, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
5693 			    ice_vsig_prof, list) {
5694 		u16 i;
5695 
5696 		for (i = 0; i < t->tcam_count; i++) {
5697 			bool used;
5698 
5699 			/* Scan the priorities from newest to oldest.
5700 			 * Make sure that the newest profiles take priority.
5701 			 */
5702 			used = ice_ptg_attr_in_use(&t->tcam[i], ptgs_used,
5703 						   attr_used, attr_used_cnt);
5704 
5705 			if (used && t->tcam[i].in_use) {
5706 				/* need to mark this PTG as never match, as it
5707 				 * was already in use and therefore duplicate
5708 				 * (and lower priority)
5709 				 */
5710 				status = ice_prof_tcam_ena_dis(hw, blk, false,
5711 							       vsig,
5712 							       &t->tcam[i],
5713 							       chg);
5714 				if (status)
5715 					goto err_ice_adj_prof_priorities;
5716 			} else if (!used && !t->tcam[i].in_use) {
5717 				/* need to enable this PTG, as it in not in use
5718 				 * and not enabled (highest priority)
5719 				 */
5720 				status = ice_prof_tcam_ena_dis(hw, blk, true,
5721 							       vsig,
5722 							       &t->tcam[i],
5723 							       chg);
5724 				if (status)
5725 					goto err_ice_adj_prof_priorities;
5726 			}
5727 
5728 			/* keep track of used ptgs */
5729 			ice_set_bit(t->tcam[i].ptg, ptgs_used);
5730 			if (attr_used_cnt < ICE_MAX_PTG_ATTRS)
5731 				attr_used[attr_used_cnt++] = &t->tcam[i];
5732 			else
5733 				ice_debug(hw, ICE_DBG_INIT, "Warn: ICE_MAX_PTG_ATTRS exceeded\n");
5734 		}
5735 	}
5736 
5737 err_ice_adj_prof_priorities:
5738 	ice_free(hw, attr_used);
5739 	return status;
5740 }
5741 
5742 /**
5743  * ice_add_prof_id_vsig - add profile to VSIG
5744  * @hw: pointer to the HW struct
5745  * @blk: hardware block
5746  * @vsig: the VSIG to which this profile is to be added
5747  * @hdl: the profile handle indicating the profile to add
5748  * @rev: true to add entries to the end of the list
5749  * @chg: the change list
5750  */
5751 static enum ice_status
5752 ice_add_prof_id_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig, u64 hdl,
5753 		     bool rev, struct LIST_HEAD_TYPE *chg)
5754 {
5755 	/* Masks that ignore flags */
5756 	u8 vl_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
5757 	u8 dc_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0x00, 0x00, 0x00 };
5758 	u8 nm_msk[ICE_TCAM_KEY_VAL_SZ] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
5759 	enum ice_status status = ICE_SUCCESS;
5760 	struct ice_prof_map *map;
5761 	struct ice_vsig_prof *t;
5762 	struct ice_chs_chg *p;
5763 	u16 vsig_idx, i;
5764 
5765 	/* Error, if this VSIG already has this profile */
5766 	if (ice_has_prof_vsig(hw, blk, vsig, hdl))
5767 		return ICE_ERR_ALREADY_EXISTS;
5768 
5769 	/* new VSIG profile structure */
5770 	t = (struct ice_vsig_prof *)ice_malloc(hw, sizeof(*t));
5771 	if (!t)
5772 		return ICE_ERR_NO_MEMORY;
5773 
5774 	ice_acquire_lock(&hw->blk[blk].es.prof_map_lock);
5775 	/* Get the details on the profile specified by the handle ID */
5776 	map = ice_search_prof_id(hw, blk, hdl);
5777 	if (!map) {
5778 		status = ICE_ERR_DOES_NOT_EXIST;
5779 		goto err_ice_add_prof_id_vsig;
5780 	}
5781 
5782 	t->profile_cookie = map->profile_cookie;
5783 	t->prof_id = map->prof_id;
5784 	t->tcam_count = map->ptg_cnt;
5785 
5786 	/* create TCAM entries */
5787 	for (i = 0; i < map->ptg_cnt; i++) {
5788 		u16 tcam_idx;
5789 
5790 		/* add TCAM to change list */
5791 		p = (struct ice_chs_chg *)ice_malloc(hw, sizeof(*p));
5792 		if (!p) {
5793 			status = ICE_ERR_NO_MEMORY;
5794 			goto err_ice_add_prof_id_vsig;
5795 		}
5796 
5797 		/* allocate the TCAM entry index */
5798 		/* for entries with empty attribute masks, allocate entry from
5799 		 * the bottom of the TCAM table; otherwise, allocate from the
5800 		 * top of the table in order to give it higher priority
5801 		 */
5802 		status = ice_alloc_tcam_ent(hw, blk, map->attr[i].mask == 0,
5803 					    &tcam_idx);
5804 		if (status) {
5805 			ice_free(hw, p);
5806 			goto err_ice_add_prof_id_vsig;
5807 		}
5808 
5809 		t->tcam[i].ptg = map->ptg[i];
5810 		t->tcam[i].prof_id = map->prof_id;
5811 		t->tcam[i].tcam_idx = tcam_idx;
5812 		t->tcam[i].attr = map->attr[i];
5813 		t->tcam[i].in_use = true;
5814 
5815 		p->type = ICE_TCAM_ADD;
5816 		p->add_tcam_idx = true;
5817 		p->prof_id = t->tcam[i].prof_id;
5818 		p->ptg = t->tcam[i].ptg;
5819 		p->vsig = vsig;
5820 		p->tcam_idx = t->tcam[i].tcam_idx;
5821 
5822 		/* set don't care masks for TCAM flags */
5823 		ice_set_tcam_flags(t->tcam[i].attr.mask, dc_msk);
5824 
5825 		/* write the TCAM entry */
5826 		status = ice_tcam_write_entry(hw, blk, t->tcam[i].tcam_idx,
5827 					      t->tcam[i].prof_id,
5828 					      t->tcam[i].ptg, vsig, 0,
5829 					      t->tcam[i].attr.flags, vl_msk,
5830 					      dc_msk, nm_msk);
5831 		if (status) {
5832 			ice_free(hw, p);
5833 			goto err_ice_add_prof_id_vsig;
5834 		}
5835 
5836 		/* log change */
5837 		LIST_ADD(&p->list_entry, chg);
5838 	}
5839 
5840 	/* add profile to VSIG */
5841 	vsig_idx = vsig & ICE_VSIG_IDX_M;
5842 	if (rev)
5843 		LIST_ADD_TAIL(&t->list,
5844 			      &hw->blk[blk].xlt2.vsig_tbl[vsig_idx].prop_lst);
5845 	else
5846 		LIST_ADD(&t->list,
5847 			 &hw->blk[blk].xlt2.vsig_tbl[vsig_idx].prop_lst);
5848 
5849 	ice_release_lock(&hw->blk[blk].es.prof_map_lock);
5850 	return status;
5851 
5852 err_ice_add_prof_id_vsig:
5853 	ice_release_lock(&hw->blk[blk].es.prof_map_lock);
5854 	/* let caller clean up the change list */
5855 	ice_free(hw, t);
5856 	return status;
5857 }
5858 
5859 /**
5860  * ice_create_prof_id_vsig - add a new VSIG with a single profile
5861  * @hw: pointer to the HW struct
5862  * @blk: hardware block
5863  * @vsi: the initial VSI that will be in VSIG
5864  * @hdl: the profile handle of the profile that will be added to the VSIG
5865  * @chg: the change list
5866  */
5867 static enum ice_status
5868 ice_create_prof_id_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsi, u64 hdl,
5869 			struct LIST_HEAD_TYPE *chg)
5870 {
5871 	enum ice_status status;
5872 	struct ice_chs_chg *p;
5873 	u16 new_vsig;
5874 
5875 	p = (struct ice_chs_chg *)ice_malloc(hw, sizeof(*p));
5876 	if (!p)
5877 		return ICE_ERR_NO_MEMORY;
5878 
5879 	new_vsig = ice_vsig_alloc(hw, blk);
5880 	if (!new_vsig) {
5881 		status = ICE_ERR_HW_TABLE;
5882 		goto err_ice_create_prof_id_vsig;
5883 	}
5884 
5885 	status = ice_move_vsi(hw, blk, vsi, new_vsig, chg);
5886 	if (status)
5887 		goto err_ice_create_prof_id_vsig;
5888 
5889 	status = ice_add_prof_id_vsig(hw, blk, new_vsig, hdl, false, chg);
5890 	if (status)
5891 		goto err_ice_create_prof_id_vsig;
5892 
5893 	p->type = ICE_VSIG_ADD;
5894 	p->vsi = vsi;
5895 	p->orig_vsig = ICE_DEFAULT_VSIG;
5896 	p->vsig = new_vsig;
5897 
5898 	LIST_ADD(&p->list_entry, chg);
5899 
5900 	return ICE_SUCCESS;
5901 
5902 err_ice_create_prof_id_vsig:
5903 	/* let caller clean up the change list */
5904 	ice_free(hw, p);
5905 	return status;
5906 }
5907 
5908 /**
5909  * ice_create_vsig_from_lst - create a new VSIG with a list of profiles
5910  * @hw: pointer to the HW struct
5911  * @blk: hardware block
5912  * @vsi: the initial VSI that will be in VSIG
5913  * @lst: the list of profile that will be added to the VSIG
5914  * @new_vsig: return of new VSIG
5915  * @chg: the change list
5916  */
5917 static enum ice_status
5918 ice_create_vsig_from_lst(struct ice_hw *hw, enum ice_block blk, u16 vsi,
5919 			 struct LIST_HEAD_TYPE *lst, u16 *new_vsig,
5920 			 struct LIST_HEAD_TYPE *chg)
5921 {
5922 	struct ice_vsig_prof *t;
5923 	enum ice_status status;
5924 	u16 vsig;
5925 
5926 	vsig = ice_vsig_alloc(hw, blk);
5927 	if (!vsig)
5928 		return ICE_ERR_HW_TABLE;
5929 
5930 	status = ice_move_vsi(hw, blk, vsi, vsig, chg);
5931 	if (status)
5932 		return status;
5933 
5934 	LIST_FOR_EACH_ENTRY(t, lst, ice_vsig_prof, list) {
5935 		/* Reverse the order here since we are copying the list */
5936 		status = ice_add_prof_id_vsig(hw, blk, vsig, t->profile_cookie,
5937 					      true, chg);
5938 		if (status)
5939 			return status;
5940 	}
5941 
5942 	*new_vsig = vsig;
5943 
5944 	return ICE_SUCCESS;
5945 }
5946 
5947 /**
5948  * ice_find_prof_vsig - find a VSIG with a specific profile handle
5949  * @hw: pointer to the HW struct
5950  * @blk: hardware block
5951  * @hdl: the profile handle of the profile to search for
5952  * @vsig: returns the VSIG with the matching profile
5953  */
5954 static bool
5955 ice_find_prof_vsig(struct ice_hw *hw, enum ice_block blk, u64 hdl, u16 *vsig)
5956 {
5957 	struct ice_vsig_prof *t;
5958 	struct LIST_HEAD_TYPE lst;
5959 	enum ice_status status;
5960 
5961 	INIT_LIST_HEAD(&lst);
5962 
5963 	t = (struct ice_vsig_prof *)ice_malloc(hw, sizeof(*t));
5964 	if (!t)
5965 		return false;
5966 
5967 	t->profile_cookie = hdl;
5968 	LIST_ADD(&t->list, &lst);
5969 
5970 	status = ice_find_dup_props_vsig(hw, blk, &lst, vsig);
5971 
5972 	LIST_DEL(&t->list);
5973 	ice_free(hw, t);
5974 
5975 	return status == ICE_SUCCESS;
5976 }
5977 
5978 /**
5979  * ice_add_vsi_flow - add VSI flow
5980  * @hw: pointer to the HW struct
5981  * @blk: hardware block
5982  * @vsi: input VSI
5983  * @vsig: target VSIG to include the input VSI
5984  *
5985  * Calling this function will add the VSI to a given VSIG and
5986  * update the HW tables accordingly. This call can be used to
5987  * add multiple VSIs to a VSIG if we know beforehand that those
5988  * VSIs have the same characteristics of the VSIG. This will
5989  * save time in generating a new VSIG and TCAMs till a match is
5990  * found and subsequent rollback when a matching VSIG is found.
5991  */
5992 enum ice_status
5993 ice_add_vsi_flow(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 vsig)
5994 {
5995 	struct ice_chs_chg *tmp, *del;
5996 	struct LIST_HEAD_TYPE chg;
5997 	enum ice_status status;
5998 
5999 	/* if target VSIG is default the move is invalid */
6000 	if ((vsig & ICE_VSIG_IDX_M) == ICE_DEFAULT_VSIG)
6001 		return ICE_ERR_PARAM;
6002 
6003 	INIT_LIST_HEAD(&chg);
6004 
6005 	/* move VSI to the VSIG that matches */
6006 	status = ice_move_vsi(hw, blk, vsi, vsig, &chg);
6007 	/* update hardware if success */
6008 	if (!status)
6009 		status = ice_upd_prof_hw(hw, blk, &chg);
6010 
6011 	LIST_FOR_EACH_ENTRY_SAFE(del, tmp, &chg, ice_chs_chg, list_entry) {
6012 		LIST_DEL(&del->list_entry);
6013 		ice_free(hw, del);
6014 	}
6015 
6016 	return status;
6017 }
6018 
6019 /**
6020  * ice_add_prof_id_flow - add profile flow
6021  * @hw: pointer to the HW struct
6022  * @blk: hardware block
6023  * @vsi: the VSI to enable with the profile specified by ID
6024  * @hdl: profile handle
6025  *
6026  * Calling this function will update the hardware tables to enable the
6027  * profile indicated by the ID parameter for the VSIs specified in the VSI
6028  * array. Once successfully called, the flow will be enabled.
6029  */
6030 enum ice_status
6031 ice_add_prof_id_flow(struct ice_hw *hw, enum ice_block blk, u16 vsi, u64 hdl)
6032 {
6033 	struct ice_vsig_prof *tmp1, *del1;
6034 	struct LIST_HEAD_TYPE union_lst;
6035 	struct ice_chs_chg *tmp, *del;
6036 	struct LIST_HEAD_TYPE chg;
6037 	enum ice_status status;
6038 	u16 vsig;
6039 
6040 	INIT_LIST_HEAD(&union_lst);
6041 	INIT_LIST_HEAD(&chg);
6042 
6043 	/* Get profile */
6044 	status = ice_get_prof(hw, blk, hdl, &chg);
6045 	if (status)
6046 		return status;
6047 
6048 	/* determine if VSI is already part of a VSIG */
6049 	status = ice_vsig_find_vsi(hw, blk, vsi, &vsig);
6050 	if (!status && vsig) {
6051 		bool only_vsi;
6052 		u16 or_vsig;
6053 		u16 ref;
6054 
6055 		/* found in VSIG */
6056 		or_vsig = vsig;
6057 
6058 		/* make sure that there is no overlap/conflict between the new
6059 		 * characteristics and the existing ones; we don't support that
6060 		 * scenario
6061 		 */
6062 		if (ice_has_prof_vsig(hw, blk, vsig, hdl)) {
6063 			status = ICE_ERR_ALREADY_EXISTS;
6064 			goto err_ice_add_prof_id_flow;
6065 		}
6066 
6067 		/* last VSI in the VSIG? */
6068 		status = ice_vsig_get_ref(hw, blk, vsig, &ref);
6069 		if (status)
6070 			goto err_ice_add_prof_id_flow;
6071 		only_vsi = (ref == 1);
6072 
6073 		/* create a union of the current profiles and the one being
6074 		 * added
6075 		 */
6076 		status = ice_get_profs_vsig(hw, blk, vsig, &union_lst);
6077 		if (status)
6078 			goto err_ice_add_prof_id_flow;
6079 
6080 		status = ice_add_prof_to_lst(hw, blk, &union_lst, hdl);
6081 		if (status)
6082 			goto err_ice_add_prof_id_flow;
6083 
6084 		/* search for an existing VSIG with an exact charc match */
6085 		status = ice_find_dup_props_vsig(hw, blk, &union_lst, &vsig);
6086 		if (!status) {
6087 			/* move VSI to the VSIG that matches */
6088 			status = ice_move_vsi(hw, blk, vsi, vsig, &chg);
6089 			if (status)
6090 				goto err_ice_add_prof_id_flow;
6091 
6092 			/* VSI has been moved out of or_vsig. If the or_vsig had
6093 			 * only that VSI it is now empty and can be removed.
6094 			 */
6095 			if (only_vsi) {
6096 				status = ice_rem_vsig(hw, blk, or_vsig, &chg);
6097 				if (status)
6098 					goto err_ice_add_prof_id_flow;
6099 			}
6100 		} else if (only_vsi) {
6101 			/* If the original VSIG only contains one VSI, then it
6102 			 * will be the requesting VSI. In this case the VSI is
6103 			 * not sharing entries and we can simply add the new
6104 			 * profile to the VSIG.
6105 			 */
6106 			status = ice_add_prof_id_vsig(hw, blk, vsig, hdl, false,
6107 						      &chg);
6108 			if (status)
6109 				goto err_ice_add_prof_id_flow;
6110 
6111 			/* Adjust priorities */
6112 			status = ice_adj_prof_priorities(hw, blk, vsig, &chg);
6113 			if (status)
6114 				goto err_ice_add_prof_id_flow;
6115 		} else {
6116 			/* No match, so we need a new VSIG */
6117 			status = ice_create_vsig_from_lst(hw, blk, vsi,
6118 							  &union_lst, &vsig,
6119 							  &chg);
6120 			if (status)
6121 				goto err_ice_add_prof_id_flow;
6122 
6123 			/* Adjust priorities */
6124 			status = ice_adj_prof_priorities(hw, blk, vsig, &chg);
6125 			if (status)
6126 				goto err_ice_add_prof_id_flow;
6127 		}
6128 	} else {
6129 		/* need to find or add a VSIG */
6130 		/* search for an existing VSIG with an exact charc match */
6131 		if (ice_find_prof_vsig(hw, blk, hdl, &vsig)) {
6132 			/* found an exact match */
6133 			/* add or move VSI to the VSIG that matches */
6134 			status = ice_move_vsi(hw, blk, vsi, vsig, &chg);
6135 			if (status)
6136 				goto err_ice_add_prof_id_flow;
6137 		} else {
6138 			/* we did not find an exact match */
6139 			/* we need to add a VSIG */
6140 			status = ice_create_prof_id_vsig(hw, blk, vsi, hdl,
6141 							 &chg);
6142 			if (status)
6143 				goto err_ice_add_prof_id_flow;
6144 		}
6145 	}
6146 
6147 	/* update hardware */
6148 	if (!status)
6149 		status = ice_upd_prof_hw(hw, blk, &chg);
6150 
6151 err_ice_add_prof_id_flow:
6152 	LIST_FOR_EACH_ENTRY_SAFE(del, tmp, &chg, ice_chs_chg, list_entry) {
6153 		LIST_DEL(&del->list_entry);
6154 		ice_free(hw, del);
6155 	}
6156 
6157 	LIST_FOR_EACH_ENTRY_SAFE(del1, tmp1, &union_lst, ice_vsig_prof, list) {
6158 		LIST_DEL(&del1->list);
6159 		ice_free(hw, del1);
6160 	}
6161 
6162 	return status;
6163 }
6164 
6165 /**
6166  * ice_rem_prof_from_list - remove a profile from list
6167  * @hw: pointer to the HW struct
6168  * @lst: list to remove the profile from
6169  * @hdl: the profile handle indicating the profile to remove
6170  */
6171 static enum ice_status
6172 ice_rem_prof_from_list(struct ice_hw *hw, struct LIST_HEAD_TYPE *lst, u64 hdl)
6173 {
6174 	struct ice_vsig_prof *ent, *tmp;
6175 
6176 	LIST_FOR_EACH_ENTRY_SAFE(ent, tmp, lst, ice_vsig_prof, list)
6177 		if (ent->profile_cookie == hdl) {
6178 			LIST_DEL(&ent->list);
6179 			ice_free(hw, ent);
6180 			return ICE_SUCCESS;
6181 		}
6182 
6183 	return ICE_ERR_DOES_NOT_EXIST;
6184 }
6185 
6186 /**
6187  * ice_rem_prof_id_flow - remove flow
6188  * @hw: pointer to the HW struct
6189  * @blk: hardware block
6190  * @vsi: the VSI from which to remove the profile specified by ID
6191  * @hdl: profile tracking handle
6192  *
6193  * Calling this function will update the hardware tables to remove the
6194  * profile indicated by the ID parameter for the VSIs specified in the VSI
6195  * array. Once successfully called, the flow will be disabled.
6196  */
6197 enum ice_status
6198 ice_rem_prof_id_flow(struct ice_hw *hw, enum ice_block blk, u16 vsi, u64 hdl)
6199 {
6200 	struct ice_vsig_prof *tmp1, *del1;
6201 	struct LIST_HEAD_TYPE chg, copy;
6202 	struct ice_chs_chg *tmp, *del;
6203 	enum ice_status status;
6204 	u16 vsig;
6205 
6206 	INIT_LIST_HEAD(&copy);
6207 	INIT_LIST_HEAD(&chg);
6208 
6209 	/* determine if VSI is already part of a VSIG */
6210 	status = ice_vsig_find_vsi(hw, blk, vsi, &vsig);
6211 	if (!status && vsig) {
6212 		bool last_profile;
6213 		bool only_vsi;
6214 		u16 ref;
6215 
6216 		/* found in VSIG */
6217 		last_profile = ice_vsig_prof_id_count(hw, blk, vsig) == 1;
6218 		status = ice_vsig_get_ref(hw, blk, vsig, &ref);
6219 		if (status)
6220 			goto err_ice_rem_prof_id_flow;
6221 		only_vsi = (ref == 1);
6222 
6223 		if (only_vsi) {
6224 			/* If the original VSIG only contains one reference,
6225 			 * which will be the requesting VSI, then the VSI is not
6226 			 * sharing entries and we can simply remove the specific
6227 			 * characteristics from the VSIG.
6228 			 */
6229 
6230 			if (last_profile) {
6231 				/* If there are no profiles left for this VSIG,
6232 				 * then simply remove the VSIG.
6233 				 */
6234 				status = ice_rem_vsig(hw, blk, vsig, &chg);
6235 				if (status)
6236 					goto err_ice_rem_prof_id_flow;
6237 			} else {
6238 				status = ice_rem_prof_id_vsig(hw, blk, vsig,
6239 							      hdl, &chg);
6240 				if (status)
6241 					goto err_ice_rem_prof_id_flow;
6242 
6243 				/* Adjust priorities */
6244 				status = ice_adj_prof_priorities(hw, blk, vsig,
6245 								 &chg);
6246 				if (status)
6247 					goto err_ice_rem_prof_id_flow;
6248 			}
6249 
6250 		} else {
6251 			/* Make a copy of the VSIG's list of Profiles */
6252 			status = ice_get_profs_vsig(hw, blk, vsig, &copy);
6253 			if (status)
6254 				goto err_ice_rem_prof_id_flow;
6255 
6256 			/* Remove specified profile entry from the list */
6257 			status = ice_rem_prof_from_list(hw, &copy, hdl);
6258 			if (status)
6259 				goto err_ice_rem_prof_id_flow;
6260 
6261 			if (LIST_EMPTY(&copy)) {
6262 				status = ice_move_vsi(hw, blk, vsi,
6263 						      ICE_DEFAULT_VSIG, &chg);
6264 				if (status)
6265 					goto err_ice_rem_prof_id_flow;
6266 
6267 			} else if (!ice_find_dup_props_vsig(hw, blk, &copy,
6268 							    &vsig)) {
6269 				/* found an exact match */
6270 				/* add or move VSI to the VSIG that matches */
6271 				/* Search for a VSIG with a matching profile
6272 				 * list
6273 				 */
6274 
6275 				/* Found match, move VSI to the matching VSIG */
6276 				status = ice_move_vsi(hw, blk, vsi, vsig, &chg);
6277 				if (status)
6278 					goto err_ice_rem_prof_id_flow;
6279 			} else {
6280 				/* since no existing VSIG supports this
6281 				 * characteristic pattern, we need to create a
6282 				 * new VSIG and TCAM entries
6283 				 */
6284 				status = ice_create_vsig_from_lst(hw, blk, vsi,
6285 								  &copy, &vsig,
6286 								  &chg);
6287 				if (status)
6288 					goto err_ice_rem_prof_id_flow;
6289 
6290 				/* Adjust priorities */
6291 				status = ice_adj_prof_priorities(hw, blk, vsig,
6292 								 &chg);
6293 				if (status)
6294 					goto err_ice_rem_prof_id_flow;
6295 			}
6296 		}
6297 	} else {
6298 		status = ICE_ERR_DOES_NOT_EXIST;
6299 	}
6300 
6301 	/* update hardware tables */
6302 	if (!status)
6303 		status = ice_upd_prof_hw(hw, blk, &chg);
6304 
6305 err_ice_rem_prof_id_flow:
6306 	LIST_FOR_EACH_ENTRY_SAFE(del, tmp, &chg, ice_chs_chg, list_entry) {
6307 		LIST_DEL(&del->list_entry);
6308 		ice_free(hw, del);
6309 	}
6310 
6311 	LIST_FOR_EACH_ENTRY_SAFE(del1, tmp1, &copy, ice_vsig_prof, list) {
6312 		LIST_DEL(&del1->list);
6313 		ice_free(hw, del1);
6314 	}
6315 
6316 	return status;
6317 }
6318