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