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