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