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].in_use = false;
493 				hw->tnl.tbl[hw->tnl.count].marked = false;
494 				hw->tnl.tbl[hw->tnl.count].boost_addr = val;
495 				hw->tnl.tbl[hw->tnl.count].port = 0;
496 				hw->tnl.count++;
497 				break;
498 			}
499 		}
500 
501 		label_name = ice_enum_labels(NULL, 0, &state, &val);
502 	}
503 
504 	/* Cache the appropriate boost TCAM entry pointers */
505 	for (i = 0; i < hw->tnl.count; i++) {
506 		ice_find_boost_entry(ice_seg, hw->tnl.tbl[i].boost_addr,
507 				     &hw->tnl.tbl[i].boost_entry);
508 		if (hw->tnl.tbl[i].boost_entry)
509 			hw->tnl.tbl[i].valid = true;
510 	}
511 }
512 
513 /* Key creation */
514 
515 #define ICE_DC_KEY	0x1	/* don't care */
516 #define ICE_DC_KEYINV	0x1
517 #define ICE_NM_KEY	0x0	/* never match */
518 #define ICE_NM_KEYINV	0x0
519 #define ICE_0_KEY	0x1	/* match 0 */
520 #define ICE_0_KEYINV	0x0
521 #define ICE_1_KEY	0x0	/* match 1 */
522 #define ICE_1_KEYINV	0x1
523 
524 /**
525  * ice_gen_key_word - generate 16-bits of a key/mask word
526  * @val: the value
527  * @valid: valid bits mask (change only the valid bits)
528  * @dont_care: don't care mask
529  * @nvr_mtch: never match mask
530  * @key: pointer to an array of where the resulting key portion
531  * @key_inv: pointer to an array of where the resulting key invert portion
532  *
533  * This function generates 16-bits from a 8-bit value, an 8-bit don't care mask
534  * and an 8-bit never match mask. The 16-bits of output are divided into 8 bits
535  * of key and 8 bits of key invert.
536  *
537  *     '0' =    b01, always match a 0 bit
538  *     '1' =    b10, always match a 1 bit
539  *     '?' =    b11, don't care bit (always matches)
540  *     '~' =    b00, never match bit
541  *
542  * Input:
543  *          val:         b0  1  0  1  0  1
544  *          dont_care:   b0  0  1  1  0  0
545  *          never_mtch:  b0  0  0  0  1  1
546  *          ------------------------------
547  * Result:  key:        b01 10 11 11 00 00
548  */
549 static enum ice_status
550 ice_gen_key_word(u8 val, u8 valid, u8 dont_care, u8 nvr_mtch, u8 *key,
551 		 u8 *key_inv)
552 {
553 	u8 in_key = *key, in_key_inv = *key_inv;
554 	u8 i;
555 
556 	/* 'dont_care' and 'nvr_mtch' masks cannot overlap */
557 	if ((dont_care ^ nvr_mtch) != (dont_care | nvr_mtch))
558 		return ICE_ERR_CFG;
559 
560 	*key = 0;
561 	*key_inv = 0;
562 
563 	/* encode the 8 bits into 8-bit key and 8-bit key invert */
564 	for (i = 0; i < 8; i++) {
565 		*key >>= 1;
566 		*key_inv >>= 1;
567 
568 		if (!(valid & 0x1)) { /* change only valid bits */
569 			*key |= (in_key & 0x1) << 7;
570 			*key_inv |= (in_key_inv & 0x1) << 7;
571 		} else if (dont_care & 0x1) { /* don't care bit */
572 			*key |= ICE_DC_KEY << 7;
573 			*key_inv |= ICE_DC_KEYINV << 7;
574 		} else if (nvr_mtch & 0x1) { /* never match bit */
575 			*key |= ICE_NM_KEY << 7;
576 			*key_inv |= ICE_NM_KEYINV << 7;
577 		} else if (val & 0x01) { /* exact 1 match */
578 			*key |= ICE_1_KEY << 7;
579 			*key_inv |= ICE_1_KEYINV << 7;
580 		} else { /* exact 0 match */
581 			*key |= ICE_0_KEY << 7;
582 			*key_inv |= ICE_0_KEYINV << 7;
583 		}
584 
585 		dont_care >>= 1;
586 		nvr_mtch >>= 1;
587 		valid >>= 1;
588 		val >>= 1;
589 		in_key >>= 1;
590 		in_key_inv >>= 1;
591 	}
592 
593 	return 0;
594 }
595 
596 /**
597  * ice_bits_max_set - determine if the number of bits set is within a maximum
598  * @mask: pointer to the byte array which is the mask
599  * @size: the number of bytes in the mask
600  * @max: the max number of set bits
601  *
602  * This function determines if there are at most 'max' number of bits set in an
603  * array. Returns true if the number for bits set is <= max or will return false
604  * otherwise.
605  */
606 static bool ice_bits_max_set(const u8 *mask, u16 size, u16 max)
607 {
608 	u16 count = 0;
609 	u16 i;
610 
611 	/* check each byte */
612 	for (i = 0; i < size; i++) {
613 		/* if 0, go to next byte */
614 		if (!mask[i])
615 			continue;
616 
617 		/* We know there is at least one set bit in this byte because of
618 		 * the above check; if we already have found 'max' number of
619 		 * bits set, then we can return failure now.
620 		 */
621 		if (count == max)
622 			return false;
623 
624 		/* count the bits in this byte, checking threshold */
625 		count += hweight8(mask[i]);
626 		if (count > max)
627 			return false;
628 	}
629 
630 	return true;
631 }
632 
633 /**
634  * ice_set_key - generate a variable sized key with multiples of 16-bits
635  * @key: pointer to where the key will be stored
636  * @size: the size of the complete key in bytes (must be even)
637  * @val: array of 8-bit values that makes up the value portion of the key
638  * @upd: array of 8-bit masks that determine what key portion to update
639  * @dc: array of 8-bit masks that make up the don't care mask
640  * @nm: array of 8-bit masks that make up the never match mask
641  * @off: the offset of the first byte in the key to update
642  * @len: the number of bytes in the key update
643  *
644  * This function generates a key from a value, a don't care mask and a never
645  * match mask.
646  * upd, dc, and nm are optional parameters, and can be NULL:
647  *	upd == NULL --> udp mask is all 1's (update all bits)
648  *	dc == NULL --> dc mask is all 0's (no don't care bits)
649  *	nm == NULL --> nm mask is all 0's (no never match bits)
650  */
651 static enum ice_status
652 ice_set_key(u8 *key, u16 size, u8 *val, u8 *upd, u8 *dc, u8 *nm, u16 off,
653 	    u16 len)
654 {
655 	u16 half_size;
656 	u16 i;
657 
658 	/* size must be a multiple of 2 bytes. */
659 	if (size % 2)
660 		return ICE_ERR_CFG;
661 
662 	half_size = size / 2;
663 	if (off + len > half_size)
664 		return ICE_ERR_CFG;
665 
666 	/* Make sure at most one bit is set in the never match mask. Having more
667 	 * than one never match mask bit set will cause HW to consume excessive
668 	 * power otherwise; this is a power management efficiency check.
669 	 */
670 #define ICE_NVR_MTCH_BITS_MAX	1
671 	if (nm && !ice_bits_max_set(nm, len, ICE_NVR_MTCH_BITS_MAX))
672 		return ICE_ERR_CFG;
673 
674 	for (i = 0; i < len; i++)
675 		if (ice_gen_key_word(val[i], upd ? upd[i] : 0xff,
676 				     dc ? dc[i] : 0, nm ? nm[i] : 0,
677 				     key + off + i, key + half_size + off + i))
678 			return ICE_ERR_CFG;
679 
680 	return 0;
681 }
682 
683 /**
684  * ice_acquire_global_cfg_lock
685  * @hw: pointer to the HW structure
686  * @access: access type (read or write)
687  *
688  * This function will request ownership of the global config lock for reading
689  * or writing of the package. When attempting to obtain write access, the
690  * caller must check for the following two return values:
691  *
692  * ICE_SUCCESS        - Means the caller has acquired the global config lock
693  *                      and can perform writing of the package.
694  * ICE_ERR_AQ_NO_WORK - Indicates another driver has already written the
695  *                      package or has found that no update was necessary; in
696  *                      this case, the caller can just skip performing any
697  *                      update of the package.
698  */
699 static enum ice_status
700 ice_acquire_global_cfg_lock(struct ice_hw *hw,
701 			    enum ice_aq_res_access_type access)
702 {
703 	enum ice_status status;
704 
705 	status = ice_acquire_res(hw, ICE_GLOBAL_CFG_LOCK_RES_ID, access,
706 				 ICE_GLOBAL_CFG_LOCK_TIMEOUT);
707 
708 	if (!status)
709 		mutex_lock(&ice_global_cfg_lock_sw);
710 	else if (status == ICE_ERR_AQ_NO_WORK)
711 		ice_debug(hw, ICE_DBG_PKG,
712 			  "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->format_ver.major, pkg_hdr->format_ver.minor,
868 		  pkg_hdr->format_ver.update, pkg_hdr->format_ver.draft);
869 
870 	/* Search all package segments for the requested segment type */
871 	for (i = 0; i < le32_to_cpu(pkg_hdr->seg_count); i++) {
872 		struct ice_generic_seg_hdr *seg;
873 
874 		seg = (struct ice_generic_seg_hdr *)
875 			((u8 *)pkg_hdr + le32_to_cpu(pkg_hdr->seg_offset[i]));
876 
877 		if (le32_to_cpu(seg->seg_type) == seg_type)
878 			return seg;
879 	}
880 
881 	return NULL;
882 }
883 
884 /**
885  * ice_update_pkg
886  * @hw: pointer to the hardware structure
887  * @bufs: pointer to an array of buffers
888  * @count: the number of buffers in the array
889  *
890  * Obtains change lock and updates package.
891  */
892 static enum ice_status
893 ice_update_pkg(struct ice_hw *hw, struct ice_buf *bufs, u32 count)
894 {
895 	enum ice_status status;
896 	u32 offset, info, i;
897 
898 	status = ice_acquire_change_lock(hw, ICE_RES_WRITE);
899 	if (status)
900 		return status;
901 
902 	for (i = 0; i < count; i++) {
903 		struct ice_buf_hdr *bh = (struct ice_buf_hdr *)(bufs + i);
904 		bool last = ((i + 1) == count);
905 
906 		status = ice_aq_update_pkg(hw, bh, le16_to_cpu(bh->data_end),
907 					   last, &offset, &info, NULL);
908 
909 		if (status) {
910 			ice_debug(hw, ICE_DBG_PKG,
911 				  "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,
990 				  "Pkg download failed: err %d off %d inf %d\n",
991 				  status, offset, info);
992 
993 			break;
994 		}
995 
996 		if (last)
997 			break;
998 	}
999 
1000 	ice_release_global_cfg_lock(hw);
1001 
1002 	return status;
1003 }
1004 
1005 /**
1006  * ice_aq_get_pkg_info_list
1007  * @hw: pointer to the hardware structure
1008  * @pkg_info: the buffer which will receive the information list
1009  * @buf_size: the size of the pkg_info information buffer
1010  * @cd: pointer to command details structure or NULL
1011  *
1012  * Get Package Info List (0x0C43)
1013  */
1014 static enum ice_status
1015 ice_aq_get_pkg_info_list(struct ice_hw *hw,
1016 			 struct ice_aqc_get_pkg_info_resp *pkg_info,
1017 			 u16 buf_size, struct ice_sq_cd *cd)
1018 {
1019 	struct ice_aq_desc desc;
1020 
1021 	ice_fill_dflt_direct_cmd_desc(&desc, ice_aqc_opc_get_pkg_info_list);
1022 
1023 	return ice_aq_send_cmd(hw, &desc, pkg_info, buf_size, cd);
1024 }
1025 
1026 /**
1027  * ice_download_pkg
1028  * @hw: pointer to the hardware structure
1029  * @ice_seg: pointer to the segment of the package to be downloaded
1030  *
1031  * Handles the download of a complete package.
1032  */
1033 static enum ice_status
1034 ice_download_pkg(struct ice_hw *hw, struct ice_seg *ice_seg)
1035 {
1036 	struct ice_buf_table *ice_buf_tbl;
1037 
1038 	ice_debug(hw, ICE_DBG_PKG, "Segment version: %d.%d.%d.%d\n",
1039 		  ice_seg->hdr.seg_ver.major, ice_seg->hdr.seg_ver.minor,
1040 		  ice_seg->hdr.seg_ver.update, ice_seg->hdr.seg_ver.draft);
1041 
1042 	ice_debug(hw, ICE_DBG_PKG, "Seg: type 0x%X, size %d, name %s\n",
1043 		  le32_to_cpu(ice_seg->hdr.seg_type),
1044 		  le32_to_cpu(ice_seg->hdr.seg_size), ice_seg->hdr.seg_name);
1045 
1046 	ice_buf_tbl = ice_find_buf_table(ice_seg);
1047 
1048 	ice_debug(hw, ICE_DBG_PKG, "Seg buf count: %d\n",
1049 		  le32_to_cpu(ice_buf_tbl->buf_count));
1050 
1051 	return ice_dwnld_cfg_bufs(hw, ice_buf_tbl->buf_array,
1052 				  le32_to_cpu(ice_buf_tbl->buf_count));
1053 }
1054 
1055 /**
1056  * ice_init_pkg_info
1057  * @hw: pointer to the hardware structure
1058  * @pkg_hdr: pointer to the driver's package hdr
1059  *
1060  * Saves off the package details into the HW structure.
1061  */
1062 static enum ice_status
1063 ice_init_pkg_info(struct ice_hw *hw, struct ice_pkg_hdr *pkg_hdr)
1064 {
1065 	struct ice_global_metadata_seg *meta_seg;
1066 	struct ice_generic_seg_hdr *seg_hdr;
1067 
1068 	if (!pkg_hdr)
1069 		return ICE_ERR_PARAM;
1070 
1071 	meta_seg = (struct ice_global_metadata_seg *)
1072 		   ice_find_seg_in_pkg(hw, SEGMENT_TYPE_METADATA, pkg_hdr);
1073 	if (meta_seg) {
1074 		hw->pkg_ver = meta_seg->pkg_ver;
1075 		memcpy(hw->pkg_name, meta_seg->pkg_name, sizeof(hw->pkg_name));
1076 
1077 		ice_debug(hw, ICE_DBG_PKG, "Pkg: %d.%d.%d.%d, %s\n",
1078 			  meta_seg->pkg_ver.major, meta_seg->pkg_ver.minor,
1079 			  meta_seg->pkg_ver.update, meta_seg->pkg_ver.draft,
1080 			  meta_seg->pkg_name);
1081 	} else {
1082 		ice_debug(hw, ICE_DBG_INIT,
1083 			  "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_ver;
1090 		memcpy(hw->ice_pkg_name, seg_hdr->seg_name,
1091 		       sizeof(hw->ice_pkg_name));
1092 
1093 		ice_debug(hw, ICE_DBG_PKG, "Ice Pkg: %d.%d.%d.%d, %s\n",
1094 			  seg_hdr->seg_ver.major, seg_hdr->seg_ver.minor,
1095 			  seg_hdr->seg_ver.update, seg_hdr->seg_ver.draft,
1096 			  seg_hdr->seg_name);
1097 	} else {
1098 		ice_debug(hw, ICE_DBG_INIT,
1099 			  "Did not find ice segment in driver package\n");
1100 		return ICE_ERR_CFG;
1101 	}
1102 
1103 	return 0;
1104 }
1105 
1106 /**
1107  * ice_get_pkg_info
1108  * @hw: pointer to the hardware structure
1109  *
1110  * Store details of the package currently loaded in HW into the HW structure.
1111  */
1112 static enum ice_status ice_get_pkg_info(struct ice_hw *hw)
1113 {
1114 	struct ice_aqc_get_pkg_info_resp *pkg_info;
1115 	enum ice_status status;
1116 	u16 size;
1117 	u32 i;
1118 
1119 	size = sizeof(*pkg_info) + (sizeof(pkg_info->pkg_info[0]) *
1120 				    (ICE_PKG_CNT - 1));
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 			memcpy(hw->active_pkg_name,
1138 			       pkg_info->pkg_info[i].name,
1139 			       sizeof(hw->active_pkg_name));
1140 			hw->active_pkg_in_nvm = pkg_info->pkg_info[i].is_in_nvm;
1141 		}
1142 		if (pkg_info->pkg_info[i].is_active_at_boot)
1143 			flags[place++] = 'B';
1144 		if (pkg_info->pkg_info[i].is_modified)
1145 			flags[place++] = 'M';
1146 		if (pkg_info->pkg_info[i].is_in_nvm)
1147 			flags[place++] = 'N';
1148 
1149 		ice_debug(hw, ICE_DBG_PKG, "Pkg[%d]: %d.%d.%d.%d,%s,%s\n",
1150 			  i, pkg_info->pkg_info[i].ver.major,
1151 			  pkg_info->pkg_info[i].ver.minor,
1152 			  pkg_info->pkg_info[i].ver.update,
1153 			  pkg_info->pkg_info[i].ver.draft,
1154 			  pkg_info->pkg_info[i].name, flags);
1155 	}
1156 
1157 init_pkg_free_alloc:
1158 	kfree(pkg_info);
1159 
1160 	return status;
1161 }
1162 
1163 /**
1164  * ice_verify_pkg - verify package
1165  * @pkg: pointer to the package buffer
1166  * @len: size of the package buffer
1167  *
1168  * Verifies various attributes of the package file, including length, format
1169  * version, and the requirement of at least one segment.
1170  */
1171 static enum ice_status ice_verify_pkg(struct ice_pkg_hdr *pkg, u32 len)
1172 {
1173 	u32 seg_count;
1174 	u32 i;
1175 
1176 	if (len < sizeof(*pkg))
1177 		return ICE_ERR_BUF_TOO_SHORT;
1178 
1179 	if (pkg->format_ver.major != ICE_PKG_FMT_VER_MAJ ||
1180 	    pkg->format_ver.minor != ICE_PKG_FMT_VER_MNR ||
1181 	    pkg->format_ver.update != ICE_PKG_FMT_VER_UPD ||
1182 	    pkg->format_ver.draft != ICE_PKG_FMT_VER_DFT)
1183 		return ICE_ERR_CFG;
1184 
1185 	/* pkg must have at least one segment */
1186 	seg_count = le32_to_cpu(pkg->seg_count);
1187 	if (seg_count < 1)
1188 		return ICE_ERR_CFG;
1189 
1190 	/* make sure segment array fits in package length */
1191 	if (len < sizeof(*pkg) + ((seg_count - 1) * sizeof(pkg->seg_offset)))
1192 		return ICE_ERR_BUF_TOO_SHORT;
1193 
1194 	/* all segments must fit within length */
1195 	for (i = 0; i < seg_count; i++) {
1196 		u32 off = le32_to_cpu(pkg->seg_offset[i]);
1197 		struct ice_generic_seg_hdr *seg;
1198 
1199 		/* segment header must fit */
1200 		if (len < off + sizeof(*seg))
1201 			return ICE_ERR_BUF_TOO_SHORT;
1202 
1203 		seg = (struct ice_generic_seg_hdr *)((u8 *)pkg + off);
1204 
1205 		/* segment body must fit */
1206 		if (len < off + le32_to_cpu(seg->seg_size))
1207 			return ICE_ERR_BUF_TOO_SHORT;
1208 	}
1209 
1210 	return 0;
1211 }
1212 
1213 /**
1214  * ice_free_seg - free package segment pointer
1215  * @hw: pointer to the hardware structure
1216  *
1217  * Frees the package segment pointer in the proper manner, depending on if the
1218  * segment was allocated or just the passed in pointer was stored.
1219  */
1220 void ice_free_seg(struct ice_hw *hw)
1221 {
1222 	if (hw->pkg_copy) {
1223 		devm_kfree(ice_hw_to_dev(hw), hw->pkg_copy);
1224 		hw->pkg_copy = NULL;
1225 		hw->pkg_size = 0;
1226 	}
1227 	hw->seg = NULL;
1228 }
1229 
1230 /**
1231  * ice_init_pkg_regs - initialize additional package registers
1232  * @hw: pointer to the hardware structure
1233  */
1234 static void ice_init_pkg_regs(struct ice_hw *hw)
1235 {
1236 #define ICE_SW_BLK_INP_MASK_L 0xFFFFFFFF
1237 #define ICE_SW_BLK_INP_MASK_H 0x0000FFFF
1238 #define ICE_SW_BLK_IDX	0
1239 
1240 	/* setup Switch block input mask, which is 48-bits in two parts */
1241 	wr32(hw, GL_PREEXT_L2_PMASK0(ICE_SW_BLK_IDX), ICE_SW_BLK_INP_MASK_L);
1242 	wr32(hw, GL_PREEXT_L2_PMASK1(ICE_SW_BLK_IDX), ICE_SW_BLK_INP_MASK_H);
1243 }
1244 
1245 /**
1246  * ice_chk_pkg_version - check package version for compatibility with driver
1247  * @pkg_ver: pointer to a version structure to check
1248  *
1249  * Check to make sure that the package about to be downloaded is compatible with
1250  * the driver. To be compatible, the major and minor components of the package
1251  * version must match our ICE_PKG_SUPP_VER_MAJ and ICE_PKG_SUPP_VER_MNR
1252  * definitions.
1253  */
1254 static enum ice_status ice_chk_pkg_version(struct ice_pkg_ver *pkg_ver)
1255 {
1256 	if (pkg_ver->major != ICE_PKG_SUPP_VER_MAJ ||
1257 	    pkg_ver->minor != ICE_PKG_SUPP_VER_MNR)
1258 		return ICE_ERR_NOT_SUPPORTED;
1259 
1260 	return 0;
1261 }
1262 
1263 /**
1264  * ice_init_pkg - initialize/download package
1265  * @hw: pointer to the hardware structure
1266  * @buf: pointer to the package buffer
1267  * @len: size of the package buffer
1268  *
1269  * This function initializes a package. The package contains HW tables
1270  * required to do packet processing. First, the function extracts package
1271  * information such as version. Then it finds the ice configuration segment
1272  * within the package; this function then saves a copy of the segment pointer
1273  * within the supplied package buffer. Next, the function will cache any hints
1274  * from the package, followed by downloading the package itself. Note, that if
1275  * a previous PF driver has already downloaded the package successfully, then
1276  * the current driver will not have to download the package again.
1277  *
1278  * The local package contents will be used to query default behavior and to
1279  * update specific sections of the HW's version of the package (e.g. to update
1280  * the parse graph to understand new protocols).
1281  *
1282  * This function stores a pointer to the package buffer memory, and it is
1283  * expected that the supplied buffer will not be freed immediately. If the
1284  * package buffer needs to be freed, such as when read from a file, use
1285  * ice_copy_and_init_pkg() instead of directly calling ice_init_pkg() in this
1286  * case.
1287  */
1288 enum ice_status ice_init_pkg(struct ice_hw *hw, u8 *buf, u32 len)
1289 {
1290 	struct ice_pkg_hdr *pkg;
1291 	enum ice_status status;
1292 	struct ice_seg *seg;
1293 
1294 	if (!buf || !len)
1295 		return ICE_ERR_PARAM;
1296 
1297 	pkg = (struct ice_pkg_hdr *)buf;
1298 	status = ice_verify_pkg(pkg, len);
1299 	if (status) {
1300 		ice_debug(hw, ICE_DBG_INIT, "failed to verify pkg (err: %d)\n",
1301 			  status);
1302 		return status;
1303 	}
1304 
1305 	/* initialize package info */
1306 	status = ice_init_pkg_info(hw, pkg);
1307 	if (status)
1308 		return status;
1309 
1310 	/* before downloading the package, check package version for
1311 	 * compatibility with driver
1312 	 */
1313 	status = ice_chk_pkg_version(&hw->pkg_ver);
1314 	if (status)
1315 		return status;
1316 
1317 	/* find segment in given package */
1318 	seg = (struct ice_seg *)ice_find_seg_in_pkg(hw, SEGMENT_TYPE_ICE, pkg);
1319 	if (!seg) {
1320 		ice_debug(hw, ICE_DBG_INIT, "no ice segment in package.\n");
1321 		return ICE_ERR_CFG;
1322 	}
1323 
1324 	/* initialize package hints and then download package */
1325 	ice_init_pkg_hints(hw, seg);
1326 	status = ice_download_pkg(hw, seg);
1327 	if (status == ICE_ERR_AQ_NO_WORK) {
1328 		ice_debug(hw, ICE_DBG_INIT,
1329 			  "package previously loaded - no work.\n");
1330 		status = 0;
1331 	}
1332 
1333 	/* Get information on the package currently loaded in HW, then make sure
1334 	 * the driver is compatible with this version.
1335 	 */
1336 	if (!status) {
1337 		status = ice_get_pkg_info(hw);
1338 		if (!status)
1339 			status = ice_chk_pkg_version(&hw->active_pkg_ver);
1340 	}
1341 
1342 	if (!status) {
1343 		hw->seg = seg;
1344 		/* on successful package download update other required
1345 		 * registers to support the package and fill HW tables
1346 		 * with package content.
1347 		 */
1348 		ice_init_pkg_regs(hw);
1349 		ice_fill_blk_tbls(hw);
1350 	} else {
1351 		ice_debug(hw, ICE_DBG_INIT, "package load failed, %d\n",
1352 			  status);
1353 	}
1354 
1355 	return status;
1356 }
1357 
1358 /**
1359  * ice_copy_and_init_pkg - initialize/download a copy of the package
1360  * @hw: pointer to the hardware structure
1361  * @buf: pointer to the package buffer
1362  * @len: size of the package buffer
1363  *
1364  * This function copies the package buffer, and then calls ice_init_pkg() to
1365  * initialize the copied package contents.
1366  *
1367  * The copying is necessary if the package buffer supplied is constant, or if
1368  * the memory may disappear shortly after calling this function.
1369  *
1370  * If the package buffer resides in the data segment and can be modified, the
1371  * caller is free to use ice_init_pkg() instead of ice_copy_and_init_pkg().
1372  *
1373  * However, if the package buffer needs to be copied first, such as when being
1374  * read from a file, the caller should use ice_copy_and_init_pkg().
1375  *
1376  * This function will first copy the package buffer, before calling
1377  * ice_init_pkg(). The caller is free to immediately destroy the original
1378  * package buffer, as the new copy will be managed by this function and
1379  * related routines.
1380  */
1381 enum ice_status ice_copy_and_init_pkg(struct ice_hw *hw, const u8 *buf, u32 len)
1382 {
1383 	enum ice_status status;
1384 	u8 *buf_copy;
1385 
1386 	if (!buf || !len)
1387 		return ICE_ERR_PARAM;
1388 
1389 	buf_copy = devm_kmemdup(ice_hw_to_dev(hw), buf, len, GFP_KERNEL);
1390 
1391 	status = ice_init_pkg(hw, buf_copy, len);
1392 	if (status) {
1393 		/* Free the copy, since we failed to initialize the package */
1394 		devm_kfree(ice_hw_to_dev(hw), buf_copy);
1395 	} else {
1396 		/* Track the copied pkg so we can free it later */
1397 		hw->pkg_copy = buf_copy;
1398 		hw->pkg_size = len;
1399 	}
1400 
1401 	return status;
1402 }
1403 
1404 /**
1405  * ice_pkg_buf_alloc
1406  * @hw: pointer to the HW structure
1407  *
1408  * Allocates a package buffer and returns a pointer to the buffer header.
1409  * Note: all package contents must be in Little Endian form.
1410  */
1411 static struct ice_buf_build *ice_pkg_buf_alloc(struct ice_hw *hw)
1412 {
1413 	struct ice_buf_build *bld;
1414 	struct ice_buf_hdr *buf;
1415 
1416 	bld = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*bld), GFP_KERNEL);
1417 	if (!bld)
1418 		return NULL;
1419 
1420 	buf = (struct ice_buf_hdr *)bld;
1421 	buf->data_end = cpu_to_le16(offsetof(struct ice_buf_hdr,
1422 					     section_entry));
1423 	return bld;
1424 }
1425 
1426 /**
1427  * ice_pkg_buf_free
1428  * @hw: pointer to the HW structure
1429  * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
1430  *
1431  * Frees a package buffer
1432  */
1433 static void ice_pkg_buf_free(struct ice_hw *hw, struct ice_buf_build *bld)
1434 {
1435 	devm_kfree(ice_hw_to_dev(hw), bld);
1436 }
1437 
1438 /**
1439  * ice_pkg_buf_reserve_section
1440  * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
1441  * @count: the number of sections to reserve
1442  *
1443  * Reserves one or more section table entries in a package buffer. This routine
1444  * can be called multiple times as long as they are made before calling
1445  * ice_pkg_buf_alloc_section(). Once ice_pkg_buf_alloc_section()
1446  * is called once, the number of sections that can be allocated will not be able
1447  * to be increased; not using all reserved sections is fine, but this will
1448  * result in some wasted space in the buffer.
1449  * Note: all package contents must be in Little Endian form.
1450  */
1451 static enum ice_status
1452 ice_pkg_buf_reserve_section(struct ice_buf_build *bld, u16 count)
1453 {
1454 	struct ice_buf_hdr *buf;
1455 	u16 section_count;
1456 	u16 data_end;
1457 
1458 	if (!bld)
1459 		return ICE_ERR_PARAM;
1460 
1461 	buf = (struct ice_buf_hdr *)&bld->buf;
1462 
1463 	/* already an active section, can't increase table size */
1464 	section_count = le16_to_cpu(buf->section_count);
1465 	if (section_count > 0)
1466 		return ICE_ERR_CFG;
1467 
1468 	if (bld->reserved_section_table_entries + count > ICE_MAX_S_COUNT)
1469 		return ICE_ERR_CFG;
1470 	bld->reserved_section_table_entries += count;
1471 
1472 	data_end = le16_to_cpu(buf->data_end) +
1473 		   (count * sizeof(buf->section_entry[0]));
1474 	buf->data_end = cpu_to_le16(data_end);
1475 
1476 	return 0;
1477 }
1478 
1479 /**
1480  * ice_pkg_buf_alloc_section
1481  * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
1482  * @type: the section type value
1483  * @size: the size of the section to reserve (in bytes)
1484  *
1485  * Reserves memory in the buffer for a section's content and updates the
1486  * buffers' status accordingly. This routine returns a pointer to the first
1487  * byte of the section start within the buffer, which is used to fill in the
1488  * section contents.
1489  * Note: all package contents must be in Little Endian form.
1490  */
1491 static void *
1492 ice_pkg_buf_alloc_section(struct ice_buf_build *bld, u32 type, u16 size)
1493 {
1494 	struct ice_buf_hdr *buf;
1495 	u16 sect_count;
1496 	u16 data_end;
1497 
1498 	if (!bld || !type || !size)
1499 		return NULL;
1500 
1501 	buf = (struct ice_buf_hdr *)&bld->buf;
1502 
1503 	/* check for enough space left in buffer */
1504 	data_end = le16_to_cpu(buf->data_end);
1505 
1506 	/* section start must align on 4 byte boundary */
1507 	data_end = ALIGN(data_end, 4);
1508 
1509 	if ((data_end + size) > ICE_MAX_S_DATA_END)
1510 		return NULL;
1511 
1512 	/* check for more available section table entries */
1513 	sect_count = le16_to_cpu(buf->section_count);
1514 	if (sect_count < bld->reserved_section_table_entries) {
1515 		void *section_ptr = ((u8 *)buf) + data_end;
1516 
1517 		buf->section_entry[sect_count].offset = cpu_to_le16(data_end);
1518 		buf->section_entry[sect_count].size = cpu_to_le16(size);
1519 		buf->section_entry[sect_count].type = cpu_to_le32(type);
1520 
1521 		data_end += size;
1522 		buf->data_end = cpu_to_le16(data_end);
1523 
1524 		buf->section_count = cpu_to_le16(sect_count + 1);
1525 		return section_ptr;
1526 	}
1527 
1528 	/* no free section table entries */
1529 	return NULL;
1530 }
1531 
1532 /**
1533  * ice_pkg_buf_get_active_sections
1534  * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
1535  *
1536  * Returns the number of active sections. Before using the package buffer
1537  * in an update package command, the caller should make sure that there is at
1538  * least one active section - otherwise, the buffer is not legal and should
1539  * not be used.
1540  * Note: all package contents must be in Little Endian form.
1541  */
1542 static u16 ice_pkg_buf_get_active_sections(struct ice_buf_build *bld)
1543 {
1544 	struct ice_buf_hdr *buf;
1545 
1546 	if (!bld)
1547 		return 0;
1548 
1549 	buf = (struct ice_buf_hdr *)&bld->buf;
1550 	return le16_to_cpu(buf->section_count);
1551 }
1552 
1553 /**
1554  * ice_pkg_buf
1555  * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
1556  *
1557  * Return a pointer to the buffer's header
1558  */
1559 static struct ice_buf *ice_pkg_buf(struct ice_buf_build *bld)
1560 {
1561 	if (!bld)
1562 		return NULL;
1563 
1564 	return &bld->buf;
1565 }
1566 
1567 /**
1568  * ice_tunnel_port_in_use_hlpr - helper function to determine tunnel usage
1569  * @hw: pointer to the HW structure
1570  * @port: port to search for
1571  * @index: optionally returns index
1572  *
1573  * Returns whether a port is already in use as a tunnel, and optionally its
1574  * index
1575  */
1576 static bool ice_tunnel_port_in_use_hlpr(struct ice_hw *hw, u16 port, u16 *index)
1577 {
1578 	u16 i;
1579 
1580 	for (i = 0; i < hw->tnl.count && i < ICE_TUNNEL_MAX_ENTRIES; i++)
1581 		if (hw->tnl.tbl[i].in_use && hw->tnl.tbl[i].port == port) {
1582 			if (index)
1583 				*index = i;
1584 			return true;
1585 		}
1586 
1587 	return false;
1588 }
1589 
1590 /**
1591  * ice_tunnel_port_in_use
1592  * @hw: pointer to the HW structure
1593  * @port: port to search for
1594  * @index: optionally returns index
1595  *
1596  * Returns whether a port is already in use as a tunnel, and optionally its
1597  * index
1598  */
1599 bool ice_tunnel_port_in_use(struct ice_hw *hw, u16 port, u16 *index)
1600 {
1601 	bool res;
1602 
1603 	mutex_lock(&hw->tnl_lock);
1604 	res = ice_tunnel_port_in_use_hlpr(hw, port, index);
1605 	mutex_unlock(&hw->tnl_lock);
1606 
1607 	return res;
1608 }
1609 
1610 /**
1611  * ice_find_free_tunnel_entry
1612  * @hw: pointer to the HW structure
1613  * @type: tunnel type
1614  * @index: optionally returns index
1615  *
1616  * Returns whether there is a free tunnel entry, and optionally its index
1617  */
1618 static bool
1619 ice_find_free_tunnel_entry(struct ice_hw *hw, enum ice_tunnel_type type,
1620 			   u16 *index)
1621 {
1622 	u16 i;
1623 
1624 	for (i = 0; i < hw->tnl.count && i < ICE_TUNNEL_MAX_ENTRIES; i++)
1625 		if (hw->tnl.tbl[i].valid && !hw->tnl.tbl[i].in_use &&
1626 		    hw->tnl.tbl[i].type == type) {
1627 			if (index)
1628 				*index = i;
1629 			return true;
1630 		}
1631 
1632 	return false;
1633 }
1634 
1635 /**
1636  * ice_get_open_tunnel_port - retrieve an open tunnel port
1637  * @hw: pointer to the HW structure
1638  * @type: tunnel type (TNL_ALL will return any open port)
1639  * @port: returns open port
1640  */
1641 bool
1642 ice_get_open_tunnel_port(struct ice_hw *hw, enum ice_tunnel_type type,
1643 			 u16 *port)
1644 {
1645 	bool res = false;
1646 	u16 i;
1647 
1648 	mutex_lock(&hw->tnl_lock);
1649 
1650 	for (i = 0; i < hw->tnl.count && i < ICE_TUNNEL_MAX_ENTRIES; i++)
1651 		if (hw->tnl.tbl[i].valid && hw->tnl.tbl[i].in_use &&
1652 		    (type == TNL_ALL || hw->tnl.tbl[i].type == type)) {
1653 			*port = hw->tnl.tbl[i].port;
1654 			res = true;
1655 			break;
1656 		}
1657 
1658 	mutex_unlock(&hw->tnl_lock);
1659 
1660 	return res;
1661 }
1662 
1663 /**
1664  * ice_create_tunnel
1665  * @hw: pointer to the HW structure
1666  * @type: type of tunnel
1667  * @port: port of tunnel to create
1668  *
1669  * Create a tunnel by updating the parse graph in the parser. We do that by
1670  * creating a package buffer with the tunnel info and issuing an update package
1671  * command.
1672  */
1673 enum ice_status
1674 ice_create_tunnel(struct ice_hw *hw, enum ice_tunnel_type type, u16 port)
1675 {
1676 	struct ice_boost_tcam_section *sect_rx, *sect_tx;
1677 	enum ice_status status = ICE_ERR_MAX_LIMIT;
1678 	struct ice_buf_build *bld;
1679 	u16 index;
1680 
1681 	mutex_lock(&hw->tnl_lock);
1682 
1683 	if (ice_tunnel_port_in_use_hlpr(hw, port, &index)) {
1684 		hw->tnl.tbl[index].ref++;
1685 		status = 0;
1686 		goto ice_create_tunnel_end;
1687 	}
1688 
1689 	if (!ice_find_free_tunnel_entry(hw, type, &index)) {
1690 		status = ICE_ERR_OUT_OF_RANGE;
1691 		goto ice_create_tunnel_end;
1692 	}
1693 
1694 	bld = ice_pkg_buf_alloc(hw);
1695 	if (!bld) {
1696 		status = ICE_ERR_NO_MEMORY;
1697 		goto ice_create_tunnel_end;
1698 	}
1699 
1700 	/* allocate 2 sections, one for Rx parser, one for Tx parser */
1701 	if (ice_pkg_buf_reserve_section(bld, 2))
1702 		goto ice_create_tunnel_err;
1703 
1704 	sect_rx = ice_pkg_buf_alloc_section(bld, ICE_SID_RXPARSER_BOOST_TCAM,
1705 					    sizeof(*sect_rx));
1706 	if (!sect_rx)
1707 		goto ice_create_tunnel_err;
1708 	sect_rx->count = cpu_to_le16(1);
1709 
1710 	sect_tx = ice_pkg_buf_alloc_section(bld, ICE_SID_TXPARSER_BOOST_TCAM,
1711 					    sizeof(*sect_tx));
1712 	if (!sect_tx)
1713 		goto ice_create_tunnel_err;
1714 	sect_tx->count = cpu_to_le16(1);
1715 
1716 	/* copy original boost entry to update package buffer */
1717 	memcpy(sect_rx->tcam, hw->tnl.tbl[index].boost_entry,
1718 	       sizeof(*sect_rx->tcam));
1719 
1720 	/* over-write the never-match dest port key bits with the encoded port
1721 	 * bits
1722 	 */
1723 	ice_set_key((u8 *)&sect_rx->tcam[0].key, sizeof(sect_rx->tcam[0].key),
1724 		    (u8 *)&port, NULL, NULL, NULL,
1725 		    (u16)offsetof(struct ice_boost_key_value, hv_dst_port_key),
1726 		    sizeof(sect_rx->tcam[0].key.key.hv_dst_port_key));
1727 
1728 	/* exact copy of entry to Tx section entry */
1729 	memcpy(sect_tx->tcam, sect_rx->tcam, sizeof(*sect_tx->tcam));
1730 
1731 	status = ice_update_pkg(hw, ice_pkg_buf(bld), 1);
1732 	if (!status) {
1733 		hw->tnl.tbl[index].port = port;
1734 		hw->tnl.tbl[index].in_use = true;
1735 		hw->tnl.tbl[index].ref = 1;
1736 	}
1737 
1738 ice_create_tunnel_err:
1739 	ice_pkg_buf_free(hw, bld);
1740 
1741 ice_create_tunnel_end:
1742 	mutex_unlock(&hw->tnl_lock);
1743 
1744 	return status;
1745 }
1746 
1747 /**
1748  * ice_destroy_tunnel
1749  * @hw: pointer to the HW structure
1750  * @port: port of tunnel to destroy (ignored if the all parameter is true)
1751  * @all: flag that states to destroy all tunnels
1752  *
1753  * Destroys a tunnel or all tunnels by creating an update package buffer
1754  * targeting the specific updates requested and then performing an update
1755  * package.
1756  */
1757 enum ice_status ice_destroy_tunnel(struct ice_hw *hw, u16 port, bool all)
1758 {
1759 	struct ice_boost_tcam_section *sect_rx, *sect_tx;
1760 	enum ice_status status = ICE_ERR_MAX_LIMIT;
1761 	struct ice_buf_build *bld;
1762 	u16 count = 0;
1763 	u16 index;
1764 	u16 size;
1765 	u16 i;
1766 
1767 	mutex_lock(&hw->tnl_lock);
1768 
1769 	if (!all && ice_tunnel_port_in_use_hlpr(hw, port, &index))
1770 		if (hw->tnl.tbl[index].ref > 1) {
1771 			hw->tnl.tbl[index].ref--;
1772 			status = 0;
1773 			goto ice_destroy_tunnel_end;
1774 		}
1775 
1776 	/* determine count */
1777 	for (i = 0; i < hw->tnl.count && i < ICE_TUNNEL_MAX_ENTRIES; i++)
1778 		if (hw->tnl.tbl[i].valid && hw->tnl.tbl[i].in_use &&
1779 		    (all || hw->tnl.tbl[i].port == port))
1780 			count++;
1781 
1782 	if (!count) {
1783 		status = ICE_ERR_PARAM;
1784 		goto ice_destroy_tunnel_end;
1785 	}
1786 
1787 	/* size of section - there is at least one entry */
1788 	size = struct_size(sect_rx, tcam, count - 1);
1789 
1790 	bld = ice_pkg_buf_alloc(hw);
1791 	if (!bld) {
1792 		status = ICE_ERR_NO_MEMORY;
1793 		goto ice_destroy_tunnel_end;
1794 	}
1795 
1796 	/* allocate 2 sections, one for Rx parser, one for Tx parser */
1797 	if (ice_pkg_buf_reserve_section(bld, 2))
1798 		goto ice_destroy_tunnel_err;
1799 
1800 	sect_rx = ice_pkg_buf_alloc_section(bld, ICE_SID_RXPARSER_BOOST_TCAM,
1801 					    size);
1802 	if (!sect_rx)
1803 		goto ice_destroy_tunnel_err;
1804 	sect_rx->count = cpu_to_le16(1);
1805 
1806 	sect_tx = ice_pkg_buf_alloc_section(bld, ICE_SID_TXPARSER_BOOST_TCAM,
1807 					    size);
1808 	if (!sect_tx)
1809 		goto ice_destroy_tunnel_err;
1810 	sect_tx->count = cpu_to_le16(1);
1811 
1812 	/* copy original boost entry to update package buffer, one copy to Rx
1813 	 * section, another copy to the Tx section
1814 	 */
1815 	for (i = 0; i < hw->tnl.count && i < ICE_TUNNEL_MAX_ENTRIES; i++)
1816 		if (hw->tnl.tbl[i].valid && hw->tnl.tbl[i].in_use &&
1817 		    (all || hw->tnl.tbl[i].port == port)) {
1818 			memcpy(sect_rx->tcam + i, hw->tnl.tbl[i].boost_entry,
1819 			       sizeof(*sect_rx->tcam));
1820 			memcpy(sect_tx->tcam + i, hw->tnl.tbl[i].boost_entry,
1821 			       sizeof(*sect_tx->tcam));
1822 			hw->tnl.tbl[i].marked = true;
1823 		}
1824 
1825 	status = ice_update_pkg(hw, ice_pkg_buf(bld), 1);
1826 	if (!status)
1827 		for (i = 0; i < hw->tnl.count &&
1828 		     i < ICE_TUNNEL_MAX_ENTRIES; i++)
1829 			if (hw->tnl.tbl[i].marked) {
1830 				hw->tnl.tbl[i].ref = 0;
1831 				hw->tnl.tbl[i].port = 0;
1832 				hw->tnl.tbl[i].in_use = false;
1833 				hw->tnl.tbl[i].marked = false;
1834 			}
1835 
1836 ice_destroy_tunnel_err:
1837 	ice_pkg_buf_free(hw, bld);
1838 
1839 ice_destroy_tunnel_end:
1840 	mutex_unlock(&hw->tnl_lock);
1841 
1842 	return status;
1843 }
1844 
1845 /* PTG Management */
1846 
1847 /**
1848  * ice_ptg_find_ptype - Search for packet type group using packet type (ptype)
1849  * @hw: pointer to the hardware structure
1850  * @blk: HW block
1851  * @ptype: the ptype to search for
1852  * @ptg: pointer to variable that receives the PTG
1853  *
1854  * This function will search the PTGs for a particular ptype, returning the
1855  * PTG ID that contains it through the PTG parameter, with the value of
1856  * ICE_DEFAULT_PTG (0) meaning it is part the default PTG.
1857  */
1858 static enum ice_status
1859 ice_ptg_find_ptype(struct ice_hw *hw, enum ice_block blk, u16 ptype, u8 *ptg)
1860 {
1861 	if (ptype >= ICE_XLT1_CNT || !ptg)
1862 		return ICE_ERR_PARAM;
1863 
1864 	*ptg = hw->blk[blk].xlt1.ptypes[ptype].ptg;
1865 	return 0;
1866 }
1867 
1868 /**
1869  * ice_ptg_alloc_val - Allocates a new packet type group ID by value
1870  * @hw: pointer to the hardware structure
1871  * @blk: HW block
1872  * @ptg: the PTG to allocate
1873  *
1874  * This function allocates a given packet type group ID specified by the PTG
1875  * parameter.
1876  */
1877 static void ice_ptg_alloc_val(struct ice_hw *hw, enum ice_block blk, u8 ptg)
1878 {
1879 	hw->blk[blk].xlt1.ptg_tbl[ptg].in_use = true;
1880 }
1881 
1882 /**
1883  * ice_ptg_remove_ptype - Removes ptype from a particular packet type group
1884  * @hw: pointer to the hardware structure
1885  * @blk: HW block
1886  * @ptype: the ptype to remove
1887  * @ptg: the PTG to remove the ptype from
1888  *
1889  * This function will remove the ptype from the specific PTG, and move it to
1890  * the default PTG (ICE_DEFAULT_PTG).
1891  */
1892 static enum ice_status
1893 ice_ptg_remove_ptype(struct ice_hw *hw, enum ice_block blk, u16 ptype, u8 ptg)
1894 {
1895 	struct ice_ptg_ptype **ch;
1896 	struct ice_ptg_ptype *p;
1897 
1898 	if (ptype > ICE_XLT1_CNT - 1)
1899 		return ICE_ERR_PARAM;
1900 
1901 	if (!hw->blk[blk].xlt1.ptg_tbl[ptg].in_use)
1902 		return ICE_ERR_DOES_NOT_EXIST;
1903 
1904 	/* Should not happen if .in_use is set, bad config */
1905 	if (!hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype)
1906 		return ICE_ERR_CFG;
1907 
1908 	/* find the ptype within this PTG, and bypass the link over it */
1909 	p = hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype;
1910 	ch = &hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype;
1911 	while (p) {
1912 		if (ptype == (p - hw->blk[blk].xlt1.ptypes)) {
1913 			*ch = p->next_ptype;
1914 			break;
1915 		}
1916 
1917 		ch = &p->next_ptype;
1918 		p = p->next_ptype;
1919 	}
1920 
1921 	hw->blk[blk].xlt1.ptypes[ptype].ptg = ICE_DEFAULT_PTG;
1922 	hw->blk[blk].xlt1.ptypes[ptype].next_ptype = NULL;
1923 
1924 	return 0;
1925 }
1926 
1927 /**
1928  * ice_ptg_add_mv_ptype - Adds/moves ptype to a particular packet type group
1929  * @hw: pointer to the hardware structure
1930  * @blk: HW block
1931  * @ptype: the ptype to add or move
1932  * @ptg: the PTG to add or move the ptype to
1933  *
1934  * This function will either add or move a ptype to a particular PTG depending
1935  * on if the ptype is already part of another group. Note that using a
1936  * a destination PTG ID of ICE_DEFAULT_PTG (0) will move the ptype to the
1937  * default PTG.
1938  */
1939 static enum ice_status
1940 ice_ptg_add_mv_ptype(struct ice_hw *hw, enum ice_block blk, u16 ptype, u8 ptg)
1941 {
1942 	enum ice_status status;
1943 	u8 original_ptg;
1944 
1945 	if (ptype > ICE_XLT1_CNT - 1)
1946 		return ICE_ERR_PARAM;
1947 
1948 	if (!hw->blk[blk].xlt1.ptg_tbl[ptg].in_use && ptg != ICE_DEFAULT_PTG)
1949 		return ICE_ERR_DOES_NOT_EXIST;
1950 
1951 	status = ice_ptg_find_ptype(hw, blk, ptype, &original_ptg);
1952 	if (status)
1953 		return status;
1954 
1955 	/* Is ptype already in the correct PTG? */
1956 	if (original_ptg == ptg)
1957 		return 0;
1958 
1959 	/* Remove from original PTG and move back to the default PTG */
1960 	if (original_ptg != ICE_DEFAULT_PTG)
1961 		ice_ptg_remove_ptype(hw, blk, ptype, original_ptg);
1962 
1963 	/* Moving to default PTG? Then we're done with this request */
1964 	if (ptg == ICE_DEFAULT_PTG)
1965 		return 0;
1966 
1967 	/* Add ptype to PTG at beginning of list */
1968 	hw->blk[blk].xlt1.ptypes[ptype].next_ptype =
1969 		hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype;
1970 	hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype =
1971 		&hw->blk[blk].xlt1.ptypes[ptype];
1972 
1973 	hw->blk[blk].xlt1.ptypes[ptype].ptg = ptg;
1974 	hw->blk[blk].xlt1.t[ptype] = ptg;
1975 
1976 	return 0;
1977 }
1978 
1979 /* Block / table size info */
1980 struct ice_blk_size_details {
1981 	u16 xlt1;			/* # XLT1 entries */
1982 	u16 xlt2;			/* # XLT2 entries */
1983 	u16 prof_tcam;			/* # profile ID TCAM entries */
1984 	u16 prof_id;			/* # profile IDs */
1985 	u8 prof_cdid_bits;		/* # CDID one-hot bits used in key */
1986 	u16 prof_redir;			/* # profile redirection entries */
1987 	u16 es;				/* # extraction sequence entries */
1988 	u16 fvw;			/* # field vector words */
1989 	u8 overwrite;			/* overwrite existing entries allowed */
1990 	u8 reverse;			/* reverse FV order */
1991 };
1992 
1993 static const struct ice_blk_size_details blk_sizes[ICE_BLK_COUNT] = {
1994 	/**
1995 	 * Table Definitions
1996 	 * XLT1 - Number of entries in XLT1 table
1997 	 * XLT2 - Number of entries in XLT2 table
1998 	 * TCAM - Number of entries Profile ID TCAM table
1999 	 * CDID - Control Domain ID of the hardware block
2000 	 * PRED - Number of entries in the Profile Redirection Table
2001 	 * FV   - Number of entries in the Field Vector
2002 	 * FVW  - Width (in WORDs) of the Field Vector
2003 	 * OVR  - Overwrite existing table entries
2004 	 * REV  - Reverse FV
2005 	 */
2006 	/*          XLT1        , XLT2        ,TCAM, PID,CDID,PRED,   FV, FVW */
2007 	/*          Overwrite   , Reverse FV */
2008 	/* SW  */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 256,   0,  256, 256,  48,
2009 		    false, false },
2010 	/* ACL */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 128,   0,  128, 128,  32,
2011 		    false, false },
2012 	/* FD  */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 128,   0,  128, 128,  24,
2013 		    false, true  },
2014 	/* RSS */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 128,   0,  128, 128,  24,
2015 		    true,  true  },
2016 	/* PE  */ { ICE_XLT1_CNT, ICE_XLT2_CNT,  64,  32,   0,   32,  32,  24,
2017 		    false, false },
2018 };
2019 
2020 enum ice_sid_all {
2021 	ICE_SID_XLT1_OFF = 0,
2022 	ICE_SID_XLT2_OFF,
2023 	ICE_SID_PR_OFF,
2024 	ICE_SID_PR_REDIR_OFF,
2025 	ICE_SID_ES_OFF,
2026 	ICE_SID_OFF_COUNT,
2027 };
2028 
2029 /* Characteristic handling */
2030 
2031 /**
2032  * ice_match_prop_lst - determine if properties of two lists match
2033  * @list1: first properties list
2034  * @list2: second properties list
2035  *
2036  * Count, cookies and the order must match in order to be considered equivalent.
2037  */
2038 static bool
2039 ice_match_prop_lst(struct list_head *list1, struct list_head *list2)
2040 {
2041 	struct ice_vsig_prof *tmp1;
2042 	struct ice_vsig_prof *tmp2;
2043 	u16 chk_count = 0;
2044 	u16 count = 0;
2045 
2046 	/* compare counts */
2047 	list_for_each_entry(tmp1, list1, list)
2048 		count++;
2049 	list_for_each_entry(tmp2, list2, list)
2050 		chk_count++;
2051 	if (!count || count != chk_count)
2052 		return false;
2053 
2054 	tmp1 = list_first_entry(list1, struct ice_vsig_prof, list);
2055 	tmp2 = list_first_entry(list2, struct ice_vsig_prof, list);
2056 
2057 	/* profile cookies must compare, and in the exact same order to take
2058 	 * into account priority
2059 	 */
2060 	while (count--) {
2061 		if (tmp2->profile_cookie != tmp1->profile_cookie)
2062 			return false;
2063 
2064 		tmp1 = list_next_entry(tmp1, list);
2065 		tmp2 = list_next_entry(tmp2, list);
2066 	}
2067 
2068 	return true;
2069 }
2070 
2071 /* VSIG Management */
2072 
2073 /**
2074  * ice_vsig_find_vsi - find a VSIG that contains a specified VSI
2075  * @hw: pointer to the hardware structure
2076  * @blk: HW block
2077  * @vsi: VSI of interest
2078  * @vsig: pointer to receive the VSI group
2079  *
2080  * This function will lookup the VSI entry in the XLT2 list and return
2081  * the VSI group its associated with.
2082  */
2083 static enum ice_status
2084 ice_vsig_find_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 *vsig)
2085 {
2086 	if (!vsig || vsi >= ICE_MAX_VSI)
2087 		return ICE_ERR_PARAM;
2088 
2089 	/* As long as there's a default or valid VSIG associated with the input
2090 	 * VSI, the functions returns a success. Any handling of VSIG will be
2091 	 * done by the following add, update or remove functions.
2092 	 */
2093 	*vsig = hw->blk[blk].xlt2.vsis[vsi].vsig;
2094 
2095 	return 0;
2096 }
2097 
2098 /**
2099  * ice_vsig_alloc_val - allocate a new VSIG by value
2100  * @hw: pointer to the hardware structure
2101  * @blk: HW block
2102  * @vsig: the VSIG to allocate
2103  *
2104  * This function will allocate a given VSIG specified by the VSIG parameter.
2105  */
2106 static u16 ice_vsig_alloc_val(struct ice_hw *hw, enum ice_block blk, u16 vsig)
2107 {
2108 	u16 idx = vsig & ICE_VSIG_IDX_M;
2109 
2110 	if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use) {
2111 		INIT_LIST_HEAD(&hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst);
2112 		hw->blk[blk].xlt2.vsig_tbl[idx].in_use = true;
2113 	}
2114 
2115 	return ICE_VSIG_VALUE(idx, hw->pf_id);
2116 }
2117 
2118 /**
2119  * ice_vsig_alloc - Finds a free entry and allocates a new VSIG
2120  * @hw: pointer to the hardware structure
2121  * @blk: HW block
2122  *
2123  * This function will iterate through the VSIG list and mark the first
2124  * unused entry for the new VSIG entry as used and return that value.
2125  */
2126 static u16 ice_vsig_alloc(struct ice_hw *hw, enum ice_block blk)
2127 {
2128 	u16 i;
2129 
2130 	for (i = 1; i < ICE_MAX_VSIGS; i++)
2131 		if (!hw->blk[blk].xlt2.vsig_tbl[i].in_use)
2132 			return ice_vsig_alloc_val(hw, blk, i);
2133 
2134 	return ICE_DEFAULT_VSIG;
2135 }
2136 
2137 /**
2138  * ice_find_dup_props_vsig - find VSI group with a specified set of properties
2139  * @hw: pointer to the hardware structure
2140  * @blk: HW block
2141  * @chs: characteristic list
2142  * @vsig: returns the VSIG with the matching profiles, if found
2143  *
2144  * Each VSIG is associated with a characteristic set; i.e. all VSIs under
2145  * a group have the same characteristic set. To check if there exists a VSIG
2146  * which has the same characteristics as the input characteristics; this
2147  * function will iterate through the XLT2 list and return the VSIG that has a
2148  * matching configuration. In order to make sure that priorities are accounted
2149  * for, the list must match exactly, including the order in which the
2150  * characteristics are listed.
2151  */
2152 static enum ice_status
2153 ice_find_dup_props_vsig(struct ice_hw *hw, enum ice_block blk,
2154 			struct list_head *chs, u16 *vsig)
2155 {
2156 	struct ice_xlt2 *xlt2 = &hw->blk[blk].xlt2;
2157 	u16 i;
2158 
2159 	for (i = 0; i < xlt2->count; i++)
2160 		if (xlt2->vsig_tbl[i].in_use &&
2161 		    ice_match_prop_lst(chs, &xlt2->vsig_tbl[i].prop_lst)) {
2162 			*vsig = ICE_VSIG_VALUE(i, hw->pf_id);
2163 			return 0;
2164 		}
2165 
2166 	return ICE_ERR_DOES_NOT_EXIST;
2167 }
2168 
2169 /**
2170  * ice_vsig_free - free VSI group
2171  * @hw: pointer to the hardware structure
2172  * @blk: HW block
2173  * @vsig: VSIG to remove
2174  *
2175  * The function will remove all VSIs associated with the input VSIG and move
2176  * them to the DEFAULT_VSIG and mark the VSIG available.
2177  */
2178 static enum ice_status
2179 ice_vsig_free(struct ice_hw *hw, enum ice_block blk, u16 vsig)
2180 {
2181 	struct ice_vsig_prof *dtmp, *del;
2182 	struct ice_vsig_vsi *vsi_cur;
2183 	u16 idx;
2184 
2185 	idx = vsig & ICE_VSIG_IDX_M;
2186 	if (idx >= ICE_MAX_VSIGS)
2187 		return ICE_ERR_PARAM;
2188 
2189 	if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use)
2190 		return ICE_ERR_DOES_NOT_EXIST;
2191 
2192 	hw->blk[blk].xlt2.vsig_tbl[idx].in_use = false;
2193 
2194 	vsi_cur = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
2195 	/* If the VSIG has at least 1 VSI then iterate through the
2196 	 * list and remove the VSIs before deleting the group.
2197 	 */
2198 	if (vsi_cur) {
2199 		/* remove all vsis associated with this VSIG XLT2 entry */
2200 		do {
2201 			struct ice_vsig_vsi *tmp = vsi_cur->next_vsi;
2202 
2203 			vsi_cur->vsig = ICE_DEFAULT_VSIG;
2204 			vsi_cur->changed = 1;
2205 			vsi_cur->next_vsi = NULL;
2206 			vsi_cur = tmp;
2207 		} while (vsi_cur);
2208 
2209 		/* NULL terminate head of VSI list */
2210 		hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi = NULL;
2211 	}
2212 
2213 	/* free characteristic list */
2214 	list_for_each_entry_safe(del, dtmp,
2215 				 &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
2216 				 list) {
2217 		list_del(&del->list);
2218 		devm_kfree(ice_hw_to_dev(hw), del);
2219 	}
2220 
2221 	/* if VSIG characteristic list was cleared for reset
2222 	 * re-initialize the list head
2223 	 */
2224 	INIT_LIST_HEAD(&hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst);
2225 
2226 	return 0;
2227 }
2228 
2229 /**
2230  * ice_vsig_remove_vsi - remove VSI from VSIG
2231  * @hw: pointer to the hardware structure
2232  * @blk: HW block
2233  * @vsi: VSI to remove
2234  * @vsig: VSI group to remove from
2235  *
2236  * The function will remove the input VSI from its VSI group and move it
2237  * to the DEFAULT_VSIG.
2238  */
2239 static enum ice_status
2240 ice_vsig_remove_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 vsig)
2241 {
2242 	struct ice_vsig_vsi **vsi_head, *vsi_cur, *vsi_tgt;
2243 	u16 idx;
2244 
2245 	idx = vsig & ICE_VSIG_IDX_M;
2246 
2247 	if (vsi >= ICE_MAX_VSI || idx >= ICE_MAX_VSIGS)
2248 		return ICE_ERR_PARAM;
2249 
2250 	if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use)
2251 		return ICE_ERR_DOES_NOT_EXIST;
2252 
2253 	/* entry already in default VSIG, don't have to remove */
2254 	if (idx == ICE_DEFAULT_VSIG)
2255 		return 0;
2256 
2257 	vsi_head = &hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
2258 	if (!(*vsi_head))
2259 		return ICE_ERR_CFG;
2260 
2261 	vsi_tgt = &hw->blk[blk].xlt2.vsis[vsi];
2262 	vsi_cur = (*vsi_head);
2263 
2264 	/* iterate the VSI list, skip over the entry to be removed */
2265 	while (vsi_cur) {
2266 		if (vsi_tgt == vsi_cur) {
2267 			(*vsi_head) = vsi_cur->next_vsi;
2268 			break;
2269 		}
2270 		vsi_head = &vsi_cur->next_vsi;
2271 		vsi_cur = vsi_cur->next_vsi;
2272 	}
2273 
2274 	/* verify if VSI was removed from group list */
2275 	if (!vsi_cur)
2276 		return ICE_ERR_DOES_NOT_EXIST;
2277 
2278 	vsi_cur->vsig = ICE_DEFAULT_VSIG;
2279 	vsi_cur->changed = 1;
2280 	vsi_cur->next_vsi = NULL;
2281 
2282 	return 0;
2283 }
2284 
2285 /**
2286  * ice_vsig_add_mv_vsi - add or move a VSI to a VSI group
2287  * @hw: pointer to the hardware structure
2288  * @blk: HW block
2289  * @vsi: VSI to move
2290  * @vsig: destination VSI group
2291  *
2292  * This function will move or add the input VSI to the target VSIG.
2293  * The function will find the original VSIG the VSI belongs to and
2294  * move the entry to the DEFAULT_VSIG, update the original VSIG and
2295  * then move entry to the new VSIG.
2296  */
2297 static enum ice_status
2298 ice_vsig_add_mv_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 vsig)
2299 {
2300 	struct ice_vsig_vsi *tmp;
2301 	enum ice_status status;
2302 	u16 orig_vsig, idx;
2303 
2304 	idx = vsig & ICE_VSIG_IDX_M;
2305 
2306 	if (vsi >= ICE_MAX_VSI || idx >= ICE_MAX_VSIGS)
2307 		return ICE_ERR_PARAM;
2308 
2309 	/* if VSIG not in use and VSIG is not default type this VSIG
2310 	 * doesn't exist.
2311 	 */
2312 	if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use &&
2313 	    vsig != ICE_DEFAULT_VSIG)
2314 		return ICE_ERR_DOES_NOT_EXIST;
2315 
2316 	status = ice_vsig_find_vsi(hw, blk, vsi, &orig_vsig);
2317 	if (status)
2318 		return status;
2319 
2320 	/* no update required if vsigs match */
2321 	if (orig_vsig == vsig)
2322 		return 0;
2323 
2324 	if (orig_vsig != ICE_DEFAULT_VSIG) {
2325 		/* remove entry from orig_vsig and add to default VSIG */
2326 		status = ice_vsig_remove_vsi(hw, blk, vsi, orig_vsig);
2327 		if (status)
2328 			return status;
2329 	}
2330 
2331 	if (idx == ICE_DEFAULT_VSIG)
2332 		return 0;
2333 
2334 	/* Create VSI entry and add VSIG and prop_mask values */
2335 	hw->blk[blk].xlt2.vsis[vsi].vsig = vsig;
2336 	hw->blk[blk].xlt2.vsis[vsi].changed = 1;
2337 
2338 	/* Add new entry to the head of the VSIG list */
2339 	tmp = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
2340 	hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi =
2341 		&hw->blk[blk].xlt2.vsis[vsi];
2342 	hw->blk[blk].xlt2.vsis[vsi].next_vsi = tmp;
2343 	hw->blk[blk].xlt2.t[vsi] = vsig;
2344 
2345 	return 0;
2346 }
2347 
2348 /**
2349  * ice_find_prof_id - find profile ID for a given field vector
2350  * @hw: pointer to the hardware structure
2351  * @blk: HW block
2352  * @fv: field vector to search for
2353  * @prof_id: receives the profile ID
2354  */
2355 static enum ice_status
2356 ice_find_prof_id(struct ice_hw *hw, enum ice_block blk,
2357 		 struct ice_fv_word *fv, u8 *prof_id)
2358 {
2359 	struct ice_es *es = &hw->blk[blk].es;
2360 	u16 off;
2361 	u8 i;
2362 
2363 	/* For FD, we don't want to re-use a existed profile with the same
2364 	 * field vector and mask. This will cause rule interference.
2365 	 */
2366 	if (blk == ICE_BLK_FD)
2367 		return ICE_ERR_DOES_NOT_EXIST;
2368 
2369 	for (i = 0; i < (u8)es->count; i++) {
2370 		off = i * es->fvw;
2371 
2372 		if (memcmp(&es->t[off], fv, es->fvw * sizeof(*fv)))
2373 			continue;
2374 
2375 		*prof_id = i;
2376 		return 0;
2377 	}
2378 
2379 	return ICE_ERR_DOES_NOT_EXIST;
2380 }
2381 
2382 /**
2383  * ice_prof_id_rsrc_type - get profile ID resource type for a block type
2384  * @blk: the block type
2385  * @rsrc_type: pointer to variable to receive the resource type
2386  */
2387 static bool ice_prof_id_rsrc_type(enum ice_block blk, u16 *rsrc_type)
2388 {
2389 	switch (blk) {
2390 	case ICE_BLK_FD:
2391 		*rsrc_type = ICE_AQC_RES_TYPE_FD_PROF_BLDR_PROFID;
2392 		break;
2393 	case ICE_BLK_RSS:
2394 		*rsrc_type = ICE_AQC_RES_TYPE_HASH_PROF_BLDR_PROFID;
2395 		break;
2396 	default:
2397 		return false;
2398 	}
2399 	return true;
2400 }
2401 
2402 /**
2403  * ice_tcam_ent_rsrc_type - get TCAM entry resource type for a block type
2404  * @blk: the block type
2405  * @rsrc_type: pointer to variable to receive the resource type
2406  */
2407 static bool ice_tcam_ent_rsrc_type(enum ice_block blk, u16 *rsrc_type)
2408 {
2409 	switch (blk) {
2410 	case ICE_BLK_FD:
2411 		*rsrc_type = ICE_AQC_RES_TYPE_FD_PROF_BLDR_TCAM;
2412 		break;
2413 	case ICE_BLK_RSS:
2414 		*rsrc_type = ICE_AQC_RES_TYPE_HASH_PROF_BLDR_TCAM;
2415 		break;
2416 	default:
2417 		return false;
2418 	}
2419 	return true;
2420 }
2421 
2422 /**
2423  * ice_alloc_tcam_ent - allocate hardware TCAM entry
2424  * @hw: pointer to the HW struct
2425  * @blk: the block to allocate the TCAM for
2426  * @tcam_idx: pointer to variable to receive the TCAM entry
2427  *
2428  * This function allocates a new entry in a Profile ID TCAM for a specific
2429  * block.
2430  */
2431 static enum ice_status
2432 ice_alloc_tcam_ent(struct ice_hw *hw, enum ice_block blk, u16 *tcam_idx)
2433 {
2434 	u16 res_type;
2435 
2436 	if (!ice_tcam_ent_rsrc_type(blk, &res_type))
2437 		return ICE_ERR_PARAM;
2438 
2439 	return ice_alloc_hw_res(hw, res_type, 1, true, tcam_idx);
2440 }
2441 
2442 /**
2443  * ice_free_tcam_ent - free hardware TCAM entry
2444  * @hw: pointer to the HW struct
2445  * @blk: the block from which to free the TCAM entry
2446  * @tcam_idx: the TCAM entry to free
2447  *
2448  * This function frees an entry in a Profile ID TCAM for a specific block.
2449  */
2450 static enum ice_status
2451 ice_free_tcam_ent(struct ice_hw *hw, enum ice_block blk, u16 tcam_idx)
2452 {
2453 	u16 res_type;
2454 
2455 	if (!ice_tcam_ent_rsrc_type(blk, &res_type))
2456 		return ICE_ERR_PARAM;
2457 
2458 	return ice_free_hw_res(hw, res_type, 1, &tcam_idx);
2459 }
2460 
2461 /**
2462  * ice_alloc_prof_id - allocate profile ID
2463  * @hw: pointer to the HW struct
2464  * @blk: the block to allocate the profile ID for
2465  * @prof_id: pointer to variable to receive the profile ID
2466  *
2467  * This function allocates a new profile ID, which also corresponds to a Field
2468  * Vector (Extraction Sequence) entry.
2469  */
2470 static enum ice_status
2471 ice_alloc_prof_id(struct ice_hw *hw, enum ice_block blk, u8 *prof_id)
2472 {
2473 	enum ice_status status;
2474 	u16 res_type;
2475 	u16 get_prof;
2476 
2477 	if (!ice_prof_id_rsrc_type(blk, &res_type))
2478 		return ICE_ERR_PARAM;
2479 
2480 	status = ice_alloc_hw_res(hw, res_type, 1, false, &get_prof);
2481 	if (!status)
2482 		*prof_id = (u8)get_prof;
2483 
2484 	return status;
2485 }
2486 
2487 /**
2488  * ice_free_prof_id - free profile ID
2489  * @hw: pointer to the HW struct
2490  * @blk: the block from which to free the profile ID
2491  * @prof_id: the profile ID to free
2492  *
2493  * This function frees a profile ID, which also corresponds to a Field Vector.
2494  */
2495 static enum ice_status
2496 ice_free_prof_id(struct ice_hw *hw, enum ice_block blk, u8 prof_id)
2497 {
2498 	u16 tmp_prof_id = (u16)prof_id;
2499 	u16 res_type;
2500 
2501 	if (!ice_prof_id_rsrc_type(blk, &res_type))
2502 		return ICE_ERR_PARAM;
2503 
2504 	return ice_free_hw_res(hw, res_type, 1, &tmp_prof_id);
2505 }
2506 
2507 /**
2508  * ice_prof_inc_ref - increment reference count for profile
2509  * @hw: pointer to the HW struct
2510  * @blk: the block from which to free the profile ID
2511  * @prof_id: the profile ID for which to increment the reference count
2512  */
2513 static enum ice_status
2514 ice_prof_inc_ref(struct ice_hw *hw, enum ice_block blk, u8 prof_id)
2515 {
2516 	if (prof_id > hw->blk[blk].es.count)
2517 		return ICE_ERR_PARAM;
2518 
2519 	hw->blk[blk].es.ref_count[prof_id]++;
2520 
2521 	return 0;
2522 }
2523 
2524 /**
2525  * ice_write_es - write an extraction sequence to hardware
2526  * @hw: pointer to the HW struct
2527  * @blk: the block in which to write the extraction sequence
2528  * @prof_id: the profile ID to write
2529  * @fv: pointer to the extraction sequence to write - NULL to clear extraction
2530  */
2531 static void
2532 ice_write_es(struct ice_hw *hw, enum ice_block blk, u8 prof_id,
2533 	     struct ice_fv_word *fv)
2534 {
2535 	u16 off;
2536 
2537 	off = prof_id * hw->blk[blk].es.fvw;
2538 	if (!fv) {
2539 		memset(&hw->blk[blk].es.t[off], 0,
2540 		       hw->blk[blk].es.fvw * sizeof(*fv));
2541 		hw->blk[blk].es.written[prof_id] = false;
2542 	} else {
2543 		memcpy(&hw->blk[blk].es.t[off], fv,
2544 		       hw->blk[blk].es.fvw * sizeof(*fv));
2545 	}
2546 }
2547 
2548 /**
2549  * ice_prof_dec_ref - decrement reference count for profile
2550  * @hw: pointer to the HW struct
2551  * @blk: the block from which to free the profile ID
2552  * @prof_id: the profile ID for which to decrement the reference count
2553  */
2554 static enum ice_status
2555 ice_prof_dec_ref(struct ice_hw *hw, enum ice_block blk, u8 prof_id)
2556 {
2557 	if (prof_id > hw->blk[blk].es.count)
2558 		return ICE_ERR_PARAM;
2559 
2560 	if (hw->blk[blk].es.ref_count[prof_id] > 0) {
2561 		if (!--hw->blk[blk].es.ref_count[prof_id]) {
2562 			ice_write_es(hw, blk, prof_id, NULL);
2563 			return ice_free_prof_id(hw, blk, prof_id);
2564 		}
2565 	}
2566 
2567 	return 0;
2568 }
2569 
2570 /* Block / table section IDs */
2571 static const u32 ice_blk_sids[ICE_BLK_COUNT][ICE_SID_OFF_COUNT] = {
2572 	/* SWITCH */
2573 	{	ICE_SID_XLT1_SW,
2574 		ICE_SID_XLT2_SW,
2575 		ICE_SID_PROFID_TCAM_SW,
2576 		ICE_SID_PROFID_REDIR_SW,
2577 		ICE_SID_FLD_VEC_SW
2578 	},
2579 
2580 	/* ACL */
2581 	{	ICE_SID_XLT1_ACL,
2582 		ICE_SID_XLT2_ACL,
2583 		ICE_SID_PROFID_TCAM_ACL,
2584 		ICE_SID_PROFID_REDIR_ACL,
2585 		ICE_SID_FLD_VEC_ACL
2586 	},
2587 
2588 	/* FD */
2589 	{	ICE_SID_XLT1_FD,
2590 		ICE_SID_XLT2_FD,
2591 		ICE_SID_PROFID_TCAM_FD,
2592 		ICE_SID_PROFID_REDIR_FD,
2593 		ICE_SID_FLD_VEC_FD
2594 	},
2595 
2596 	/* RSS */
2597 	{	ICE_SID_XLT1_RSS,
2598 		ICE_SID_XLT2_RSS,
2599 		ICE_SID_PROFID_TCAM_RSS,
2600 		ICE_SID_PROFID_REDIR_RSS,
2601 		ICE_SID_FLD_VEC_RSS
2602 	},
2603 
2604 	/* PE */
2605 	{	ICE_SID_XLT1_PE,
2606 		ICE_SID_XLT2_PE,
2607 		ICE_SID_PROFID_TCAM_PE,
2608 		ICE_SID_PROFID_REDIR_PE,
2609 		ICE_SID_FLD_VEC_PE
2610 	}
2611 };
2612 
2613 /**
2614  * ice_init_sw_xlt1_db - init software XLT1 database from HW tables
2615  * @hw: pointer to the hardware structure
2616  * @blk: the HW block to initialize
2617  */
2618 static void ice_init_sw_xlt1_db(struct ice_hw *hw, enum ice_block blk)
2619 {
2620 	u16 pt;
2621 
2622 	for (pt = 0; pt < hw->blk[blk].xlt1.count; pt++) {
2623 		u8 ptg;
2624 
2625 		ptg = hw->blk[blk].xlt1.t[pt];
2626 		if (ptg != ICE_DEFAULT_PTG) {
2627 			ice_ptg_alloc_val(hw, blk, ptg);
2628 			ice_ptg_add_mv_ptype(hw, blk, pt, ptg);
2629 		}
2630 	}
2631 }
2632 
2633 /**
2634  * ice_init_sw_xlt2_db - init software XLT2 database from HW tables
2635  * @hw: pointer to the hardware structure
2636  * @blk: the HW block to initialize
2637  */
2638 static void ice_init_sw_xlt2_db(struct ice_hw *hw, enum ice_block blk)
2639 {
2640 	u16 vsi;
2641 
2642 	for (vsi = 0; vsi < hw->blk[blk].xlt2.count; vsi++) {
2643 		u16 vsig;
2644 
2645 		vsig = hw->blk[blk].xlt2.t[vsi];
2646 		if (vsig) {
2647 			ice_vsig_alloc_val(hw, blk, vsig);
2648 			ice_vsig_add_mv_vsi(hw, blk, vsi, vsig);
2649 			/* no changes at this time, since this has been
2650 			 * initialized from the original package
2651 			 */
2652 			hw->blk[blk].xlt2.vsis[vsi].changed = 0;
2653 		}
2654 	}
2655 }
2656 
2657 /**
2658  * ice_init_sw_db - init software database from HW tables
2659  * @hw: pointer to the hardware structure
2660  */
2661 static void ice_init_sw_db(struct ice_hw *hw)
2662 {
2663 	u16 i;
2664 
2665 	for (i = 0; i < ICE_BLK_COUNT; i++) {
2666 		ice_init_sw_xlt1_db(hw, (enum ice_block)i);
2667 		ice_init_sw_xlt2_db(hw, (enum ice_block)i);
2668 	}
2669 }
2670 
2671 /**
2672  * ice_fill_tbl - Reads content of a single table type into database
2673  * @hw: pointer to the hardware structure
2674  * @block_id: Block ID of the table to copy
2675  * @sid: Section ID of the table to copy
2676  *
2677  * Will attempt to read the entire content of a given table of a single block
2678  * into the driver database. We assume that the buffer will always
2679  * be as large or larger than the data contained in the package. If
2680  * this condition is not met, there is most likely an error in the package
2681  * contents.
2682  */
2683 static void ice_fill_tbl(struct ice_hw *hw, enum ice_block block_id, u32 sid)
2684 {
2685 	u32 dst_len, sect_len, offset = 0;
2686 	struct ice_prof_redir_section *pr;
2687 	struct ice_prof_id_section *pid;
2688 	struct ice_xlt1_section *xlt1;
2689 	struct ice_xlt2_section *xlt2;
2690 	struct ice_sw_fv_section *es;
2691 	struct ice_pkg_enum state;
2692 	u8 *src, *dst;
2693 	void *sect;
2694 
2695 	/* if the HW segment pointer is null then the first iteration of
2696 	 * ice_pkg_enum_section() will fail. In this case the HW tables will
2697 	 * not be filled and return success.
2698 	 */
2699 	if (!hw->seg) {
2700 		ice_debug(hw, ICE_DBG_PKG, "hw->seg is NULL, tables are not filled\n");
2701 		return;
2702 	}
2703 
2704 	memset(&state, 0, sizeof(state));
2705 
2706 	sect = ice_pkg_enum_section(hw->seg, &state, sid);
2707 
2708 	while (sect) {
2709 		switch (sid) {
2710 		case ICE_SID_XLT1_SW:
2711 		case ICE_SID_XLT1_FD:
2712 		case ICE_SID_XLT1_RSS:
2713 		case ICE_SID_XLT1_ACL:
2714 		case ICE_SID_XLT1_PE:
2715 			xlt1 = (struct ice_xlt1_section *)sect;
2716 			src = xlt1->value;
2717 			sect_len = le16_to_cpu(xlt1->count) *
2718 				sizeof(*hw->blk[block_id].xlt1.t);
2719 			dst = hw->blk[block_id].xlt1.t;
2720 			dst_len = hw->blk[block_id].xlt1.count *
2721 				sizeof(*hw->blk[block_id].xlt1.t);
2722 			break;
2723 		case ICE_SID_XLT2_SW:
2724 		case ICE_SID_XLT2_FD:
2725 		case ICE_SID_XLT2_RSS:
2726 		case ICE_SID_XLT2_ACL:
2727 		case ICE_SID_XLT2_PE:
2728 			xlt2 = (struct ice_xlt2_section *)sect;
2729 			src = (__force u8 *)xlt2->value;
2730 			sect_len = le16_to_cpu(xlt2->count) *
2731 				sizeof(*hw->blk[block_id].xlt2.t);
2732 			dst = (u8 *)hw->blk[block_id].xlt2.t;
2733 			dst_len = hw->blk[block_id].xlt2.count *
2734 				sizeof(*hw->blk[block_id].xlt2.t);
2735 			break;
2736 		case ICE_SID_PROFID_TCAM_SW:
2737 		case ICE_SID_PROFID_TCAM_FD:
2738 		case ICE_SID_PROFID_TCAM_RSS:
2739 		case ICE_SID_PROFID_TCAM_ACL:
2740 		case ICE_SID_PROFID_TCAM_PE:
2741 			pid = (struct ice_prof_id_section *)sect;
2742 			src = (u8 *)pid->entry;
2743 			sect_len = le16_to_cpu(pid->count) *
2744 				sizeof(*hw->blk[block_id].prof.t);
2745 			dst = (u8 *)hw->blk[block_id].prof.t;
2746 			dst_len = hw->blk[block_id].prof.count *
2747 				sizeof(*hw->blk[block_id].prof.t);
2748 			break;
2749 		case ICE_SID_PROFID_REDIR_SW:
2750 		case ICE_SID_PROFID_REDIR_FD:
2751 		case ICE_SID_PROFID_REDIR_RSS:
2752 		case ICE_SID_PROFID_REDIR_ACL:
2753 		case ICE_SID_PROFID_REDIR_PE:
2754 			pr = (struct ice_prof_redir_section *)sect;
2755 			src = pr->redir_value;
2756 			sect_len = le16_to_cpu(pr->count) *
2757 				sizeof(*hw->blk[block_id].prof_redir.t);
2758 			dst = hw->blk[block_id].prof_redir.t;
2759 			dst_len = hw->blk[block_id].prof_redir.count *
2760 				sizeof(*hw->blk[block_id].prof_redir.t);
2761 			break;
2762 		case ICE_SID_FLD_VEC_SW:
2763 		case ICE_SID_FLD_VEC_FD:
2764 		case ICE_SID_FLD_VEC_RSS:
2765 		case ICE_SID_FLD_VEC_ACL:
2766 		case ICE_SID_FLD_VEC_PE:
2767 			es = (struct ice_sw_fv_section *)sect;
2768 			src = (u8 *)es->fv;
2769 			sect_len = (u32)(le16_to_cpu(es->count) *
2770 					 hw->blk[block_id].es.fvw) *
2771 				sizeof(*hw->blk[block_id].es.t);
2772 			dst = (u8 *)hw->blk[block_id].es.t;
2773 			dst_len = (u32)(hw->blk[block_id].es.count *
2774 					hw->blk[block_id].es.fvw) *
2775 				sizeof(*hw->blk[block_id].es.t);
2776 			break;
2777 		default:
2778 			return;
2779 		}
2780 
2781 		/* if the section offset exceeds destination length, terminate
2782 		 * table fill.
2783 		 */
2784 		if (offset > dst_len)
2785 			return;
2786 
2787 		/* if the sum of section size and offset exceed destination size
2788 		 * then we are out of bounds of the HW table size for that PF.
2789 		 * Changing section length to fill the remaining table space
2790 		 * of that PF.
2791 		 */
2792 		if ((offset + sect_len) > dst_len)
2793 			sect_len = dst_len - offset;
2794 
2795 		memcpy(dst + offset, src, sect_len);
2796 		offset += sect_len;
2797 		sect = ice_pkg_enum_section(NULL, &state, sid);
2798 	}
2799 }
2800 
2801 /**
2802  * ice_fill_blk_tbls - Read package context for tables
2803  * @hw: pointer to the hardware structure
2804  *
2805  * Reads the current package contents and populates the driver
2806  * database with the data iteratively for all advanced feature
2807  * blocks. Assume that the HW tables have been allocated.
2808  */
2809 void ice_fill_blk_tbls(struct ice_hw *hw)
2810 {
2811 	u8 i;
2812 
2813 	for (i = 0; i < ICE_BLK_COUNT; i++) {
2814 		enum ice_block blk_id = (enum ice_block)i;
2815 
2816 		ice_fill_tbl(hw, blk_id, hw->blk[blk_id].xlt1.sid);
2817 		ice_fill_tbl(hw, blk_id, hw->blk[blk_id].xlt2.sid);
2818 		ice_fill_tbl(hw, blk_id, hw->blk[blk_id].prof.sid);
2819 		ice_fill_tbl(hw, blk_id, hw->blk[blk_id].prof_redir.sid);
2820 		ice_fill_tbl(hw, blk_id, hw->blk[blk_id].es.sid);
2821 	}
2822 
2823 	ice_init_sw_db(hw);
2824 }
2825 
2826 /**
2827  * ice_free_prof_map - free profile map
2828  * @hw: pointer to the hardware structure
2829  * @blk_idx: HW block index
2830  */
2831 static void ice_free_prof_map(struct ice_hw *hw, u8 blk_idx)
2832 {
2833 	struct ice_es *es = &hw->blk[blk_idx].es;
2834 	struct ice_prof_map *del, *tmp;
2835 
2836 	mutex_lock(&es->prof_map_lock);
2837 	list_for_each_entry_safe(del, tmp, &es->prof_map, list) {
2838 		list_del(&del->list);
2839 		devm_kfree(ice_hw_to_dev(hw), del);
2840 	}
2841 	INIT_LIST_HEAD(&es->prof_map);
2842 	mutex_unlock(&es->prof_map_lock);
2843 }
2844 
2845 /**
2846  * ice_free_flow_profs - free flow profile entries
2847  * @hw: pointer to the hardware structure
2848  * @blk_idx: HW block index
2849  */
2850 static void ice_free_flow_profs(struct ice_hw *hw, u8 blk_idx)
2851 {
2852 	struct ice_flow_prof *p, *tmp;
2853 
2854 	mutex_lock(&hw->fl_profs_locks[blk_idx]);
2855 	list_for_each_entry_safe(p, tmp, &hw->fl_profs[blk_idx], l_entry) {
2856 		struct ice_flow_entry *e, *t;
2857 
2858 		list_for_each_entry_safe(e, t, &p->entries, l_entry)
2859 			ice_flow_rem_entry(hw, (enum ice_block)blk_idx,
2860 					   ICE_FLOW_ENTRY_HNDL(e));
2861 
2862 		list_del(&p->l_entry);
2863 		devm_kfree(ice_hw_to_dev(hw), p);
2864 	}
2865 	mutex_unlock(&hw->fl_profs_locks[blk_idx]);
2866 
2867 	/* if driver is in reset and tables are being cleared
2868 	 * re-initialize the flow profile list heads
2869 	 */
2870 	INIT_LIST_HEAD(&hw->fl_profs[blk_idx]);
2871 }
2872 
2873 /**
2874  * ice_free_vsig_tbl - free complete VSIG table entries
2875  * @hw: pointer to the hardware structure
2876  * @blk: the HW block on which to free the VSIG table entries
2877  */
2878 static void ice_free_vsig_tbl(struct ice_hw *hw, enum ice_block blk)
2879 {
2880 	u16 i;
2881 
2882 	if (!hw->blk[blk].xlt2.vsig_tbl)
2883 		return;
2884 
2885 	for (i = 1; i < ICE_MAX_VSIGS; i++)
2886 		if (hw->blk[blk].xlt2.vsig_tbl[i].in_use)
2887 			ice_vsig_free(hw, blk, i);
2888 }
2889 
2890 /**
2891  * ice_free_hw_tbls - free hardware table memory
2892  * @hw: pointer to the hardware structure
2893  */
2894 void ice_free_hw_tbls(struct ice_hw *hw)
2895 {
2896 	struct ice_rss_cfg *r, *rt;
2897 	u8 i;
2898 
2899 	for (i = 0; i < ICE_BLK_COUNT; i++) {
2900 		if (hw->blk[i].is_list_init) {
2901 			struct ice_es *es = &hw->blk[i].es;
2902 
2903 			ice_free_prof_map(hw, i);
2904 			mutex_destroy(&es->prof_map_lock);
2905 
2906 			ice_free_flow_profs(hw, i);
2907 			mutex_destroy(&hw->fl_profs_locks[i]);
2908 
2909 			hw->blk[i].is_list_init = false;
2910 		}
2911 		ice_free_vsig_tbl(hw, (enum ice_block)i);
2912 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt1.ptypes);
2913 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt1.ptg_tbl);
2914 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt1.t);
2915 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt2.t);
2916 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt2.vsig_tbl);
2917 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt2.vsis);
2918 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].prof.t);
2919 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].prof_redir.t);
2920 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].es.t);
2921 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].es.ref_count);
2922 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].es.written);
2923 	}
2924 
2925 	list_for_each_entry_safe(r, rt, &hw->rss_list_head, l_entry) {
2926 		list_del(&r->l_entry);
2927 		devm_kfree(ice_hw_to_dev(hw), r);
2928 	}
2929 	mutex_destroy(&hw->rss_locks);
2930 	memset(hw->blk, 0, sizeof(hw->blk));
2931 }
2932 
2933 /**
2934  * ice_init_flow_profs - init flow profile locks and list heads
2935  * @hw: pointer to the hardware structure
2936  * @blk_idx: HW block index
2937  */
2938 static void ice_init_flow_profs(struct ice_hw *hw, u8 blk_idx)
2939 {
2940 	mutex_init(&hw->fl_profs_locks[blk_idx]);
2941 	INIT_LIST_HEAD(&hw->fl_profs[blk_idx]);
2942 }
2943 
2944 /**
2945  * ice_clear_hw_tbls - clear HW tables and flow profiles
2946  * @hw: pointer to the hardware structure
2947  */
2948 void ice_clear_hw_tbls(struct ice_hw *hw)
2949 {
2950 	u8 i;
2951 
2952 	for (i = 0; i < ICE_BLK_COUNT; i++) {
2953 		struct ice_prof_redir *prof_redir = &hw->blk[i].prof_redir;
2954 		struct ice_prof_tcam *prof = &hw->blk[i].prof;
2955 		struct ice_xlt1 *xlt1 = &hw->blk[i].xlt1;
2956 		struct ice_xlt2 *xlt2 = &hw->blk[i].xlt2;
2957 		struct ice_es *es = &hw->blk[i].es;
2958 
2959 		if (hw->blk[i].is_list_init) {
2960 			ice_free_prof_map(hw, i);
2961 			ice_free_flow_profs(hw, i);
2962 		}
2963 
2964 		ice_free_vsig_tbl(hw, (enum ice_block)i);
2965 
2966 		memset(xlt1->ptypes, 0, xlt1->count * sizeof(*xlt1->ptypes));
2967 		memset(xlt1->ptg_tbl, 0,
2968 		       ICE_MAX_PTGS * sizeof(*xlt1->ptg_tbl));
2969 		memset(xlt1->t, 0, xlt1->count * sizeof(*xlt1->t));
2970 
2971 		memset(xlt2->vsis, 0, xlt2->count * sizeof(*xlt2->vsis));
2972 		memset(xlt2->vsig_tbl, 0,
2973 		       xlt2->count * sizeof(*xlt2->vsig_tbl));
2974 		memset(xlt2->t, 0, xlt2->count * sizeof(*xlt2->t));
2975 
2976 		memset(prof->t, 0, prof->count * sizeof(*prof->t));
2977 		memset(prof_redir->t, 0,
2978 		       prof_redir->count * sizeof(*prof_redir->t));
2979 
2980 		memset(es->t, 0, es->count * sizeof(*es->t));
2981 		memset(es->ref_count, 0, es->count * sizeof(*es->ref_count));
2982 		memset(es->written, 0, es->count * sizeof(*es->written));
2983 	}
2984 }
2985 
2986 /**
2987  * ice_init_hw_tbls - init hardware table memory
2988  * @hw: pointer to the hardware structure
2989  */
2990 enum ice_status ice_init_hw_tbls(struct ice_hw *hw)
2991 {
2992 	u8 i;
2993 
2994 	mutex_init(&hw->rss_locks);
2995 	INIT_LIST_HEAD(&hw->rss_list_head);
2996 	for (i = 0; i < ICE_BLK_COUNT; i++) {
2997 		struct ice_prof_redir *prof_redir = &hw->blk[i].prof_redir;
2998 		struct ice_prof_tcam *prof = &hw->blk[i].prof;
2999 		struct ice_xlt1 *xlt1 = &hw->blk[i].xlt1;
3000 		struct ice_xlt2 *xlt2 = &hw->blk[i].xlt2;
3001 		struct ice_es *es = &hw->blk[i].es;
3002 		u16 j;
3003 
3004 		if (hw->blk[i].is_list_init)
3005 			continue;
3006 
3007 		ice_init_flow_profs(hw, i);
3008 		mutex_init(&es->prof_map_lock);
3009 		INIT_LIST_HEAD(&es->prof_map);
3010 		hw->blk[i].is_list_init = true;
3011 
3012 		hw->blk[i].overwrite = blk_sizes[i].overwrite;
3013 		es->reverse = blk_sizes[i].reverse;
3014 
3015 		xlt1->sid = ice_blk_sids[i][ICE_SID_XLT1_OFF];
3016 		xlt1->count = blk_sizes[i].xlt1;
3017 
3018 		xlt1->ptypes = devm_kcalloc(ice_hw_to_dev(hw), xlt1->count,
3019 					    sizeof(*xlt1->ptypes), GFP_KERNEL);
3020 
3021 		if (!xlt1->ptypes)
3022 			goto err;
3023 
3024 		xlt1->ptg_tbl = devm_kcalloc(ice_hw_to_dev(hw), ICE_MAX_PTGS,
3025 					     sizeof(*xlt1->ptg_tbl),
3026 					     GFP_KERNEL);
3027 
3028 		if (!xlt1->ptg_tbl)
3029 			goto err;
3030 
3031 		xlt1->t = devm_kcalloc(ice_hw_to_dev(hw), xlt1->count,
3032 				       sizeof(*xlt1->t), GFP_KERNEL);
3033 		if (!xlt1->t)
3034 			goto err;
3035 
3036 		xlt2->sid = ice_blk_sids[i][ICE_SID_XLT2_OFF];
3037 		xlt2->count = blk_sizes[i].xlt2;
3038 
3039 		xlt2->vsis = devm_kcalloc(ice_hw_to_dev(hw), xlt2->count,
3040 					  sizeof(*xlt2->vsis), GFP_KERNEL);
3041 
3042 		if (!xlt2->vsis)
3043 			goto err;
3044 
3045 		xlt2->vsig_tbl = devm_kcalloc(ice_hw_to_dev(hw), xlt2->count,
3046 					      sizeof(*xlt2->vsig_tbl),
3047 					      GFP_KERNEL);
3048 		if (!xlt2->vsig_tbl)
3049 			goto err;
3050 
3051 		for (j = 0; j < xlt2->count; j++)
3052 			INIT_LIST_HEAD(&xlt2->vsig_tbl[j].prop_lst);
3053 
3054 		xlt2->t = devm_kcalloc(ice_hw_to_dev(hw), xlt2->count,
3055 				       sizeof(*xlt2->t), GFP_KERNEL);
3056 		if (!xlt2->t)
3057 			goto err;
3058 
3059 		prof->sid = ice_blk_sids[i][ICE_SID_PR_OFF];
3060 		prof->count = blk_sizes[i].prof_tcam;
3061 		prof->max_prof_id = blk_sizes[i].prof_id;
3062 		prof->cdid_bits = blk_sizes[i].prof_cdid_bits;
3063 		prof->t = devm_kcalloc(ice_hw_to_dev(hw), prof->count,
3064 				       sizeof(*prof->t), GFP_KERNEL);
3065 
3066 		if (!prof->t)
3067 			goto err;
3068 
3069 		prof_redir->sid = ice_blk_sids[i][ICE_SID_PR_REDIR_OFF];
3070 		prof_redir->count = blk_sizes[i].prof_redir;
3071 		prof_redir->t = devm_kcalloc(ice_hw_to_dev(hw),
3072 					     prof_redir->count,
3073 					     sizeof(*prof_redir->t),
3074 					     GFP_KERNEL);
3075 
3076 		if (!prof_redir->t)
3077 			goto err;
3078 
3079 		es->sid = ice_blk_sids[i][ICE_SID_ES_OFF];
3080 		es->count = blk_sizes[i].es;
3081 		es->fvw = blk_sizes[i].fvw;
3082 		es->t = devm_kcalloc(ice_hw_to_dev(hw),
3083 				     (u32)(es->count * es->fvw),
3084 				     sizeof(*es->t), GFP_KERNEL);
3085 		if (!es->t)
3086 			goto err;
3087 
3088 		es->ref_count = devm_kcalloc(ice_hw_to_dev(hw), es->count,
3089 					     sizeof(*es->ref_count),
3090 					     GFP_KERNEL);
3091 
3092 		es->written = devm_kcalloc(ice_hw_to_dev(hw), es->count,
3093 					   sizeof(*es->written), GFP_KERNEL);
3094 		if (!es->ref_count)
3095 			goto err;
3096 	}
3097 	return 0;
3098 
3099 err:
3100 	ice_free_hw_tbls(hw);
3101 	return ICE_ERR_NO_MEMORY;
3102 }
3103 
3104 /**
3105  * ice_prof_gen_key - generate profile ID key
3106  * @hw: pointer to the HW struct
3107  * @blk: the block in which to write profile ID to
3108  * @ptg: packet type group (PTG) portion of key
3109  * @vsig: VSIG portion of key
3110  * @cdid: CDID portion of key
3111  * @flags: flag portion of key
3112  * @vl_msk: valid mask
3113  * @dc_msk: don't care mask
3114  * @nm_msk: never match mask
3115  * @key: output of profile ID key
3116  */
3117 static enum ice_status
3118 ice_prof_gen_key(struct ice_hw *hw, enum ice_block blk, u8 ptg, u16 vsig,
3119 		 u8 cdid, u16 flags, u8 vl_msk[ICE_TCAM_KEY_VAL_SZ],
3120 		 u8 dc_msk[ICE_TCAM_KEY_VAL_SZ], u8 nm_msk[ICE_TCAM_KEY_VAL_SZ],
3121 		 u8 key[ICE_TCAM_KEY_SZ])
3122 {
3123 	struct ice_prof_id_key inkey;
3124 
3125 	inkey.xlt1 = ptg;
3126 	inkey.xlt2_cdid = cpu_to_le16(vsig);
3127 	inkey.flags = cpu_to_le16(flags);
3128 
3129 	switch (hw->blk[blk].prof.cdid_bits) {
3130 	case 0:
3131 		break;
3132 	case 2:
3133 #define ICE_CD_2_M 0xC000U
3134 #define ICE_CD_2_S 14
3135 		inkey.xlt2_cdid &= ~cpu_to_le16(ICE_CD_2_M);
3136 		inkey.xlt2_cdid |= cpu_to_le16(BIT(cdid) << ICE_CD_2_S);
3137 		break;
3138 	case 4:
3139 #define ICE_CD_4_M 0xF000U
3140 #define ICE_CD_4_S 12
3141 		inkey.xlt2_cdid &= ~cpu_to_le16(ICE_CD_4_M);
3142 		inkey.xlt2_cdid |= cpu_to_le16(BIT(cdid) << ICE_CD_4_S);
3143 		break;
3144 	case 8:
3145 #define ICE_CD_8_M 0xFF00U
3146 #define ICE_CD_8_S 16
3147 		inkey.xlt2_cdid &= ~cpu_to_le16(ICE_CD_8_M);
3148 		inkey.xlt2_cdid |= cpu_to_le16(BIT(cdid) << ICE_CD_8_S);
3149 		break;
3150 	default:
3151 		ice_debug(hw, ICE_DBG_PKG, "Error in profile config\n");
3152 		break;
3153 	}
3154 
3155 	return ice_set_key(key, ICE_TCAM_KEY_SZ, (u8 *)&inkey, vl_msk, dc_msk,
3156 			   nm_msk, 0, ICE_TCAM_KEY_SZ / 2);
3157 }
3158 
3159 /**
3160  * ice_tcam_write_entry - write TCAM entry
3161  * @hw: pointer to the HW struct
3162  * @blk: the block in which to write profile ID to
3163  * @idx: the entry index to write to
3164  * @prof_id: profile ID
3165  * @ptg: packet type group (PTG) portion of key
3166  * @vsig: VSIG portion of key
3167  * @cdid: CDID portion of key
3168  * @flags: flag portion of key
3169  * @vl_msk: valid mask
3170  * @dc_msk: don't care mask
3171  * @nm_msk: never match mask
3172  */
3173 static enum ice_status
3174 ice_tcam_write_entry(struct ice_hw *hw, enum ice_block blk, u16 idx,
3175 		     u8 prof_id, u8 ptg, u16 vsig, u8 cdid, u16 flags,
3176 		     u8 vl_msk[ICE_TCAM_KEY_VAL_SZ],
3177 		     u8 dc_msk[ICE_TCAM_KEY_VAL_SZ],
3178 		     u8 nm_msk[ICE_TCAM_KEY_VAL_SZ])
3179 {
3180 	struct ice_prof_tcam_entry;
3181 	enum ice_status status;
3182 
3183 	status = ice_prof_gen_key(hw, blk, ptg, vsig, cdid, flags, vl_msk,
3184 				  dc_msk, nm_msk, hw->blk[blk].prof.t[idx].key);
3185 	if (!status) {
3186 		hw->blk[blk].prof.t[idx].addr = cpu_to_le16(idx);
3187 		hw->blk[blk].prof.t[idx].prof_id = prof_id;
3188 	}
3189 
3190 	return status;
3191 }
3192 
3193 /**
3194  * ice_vsig_get_ref - returns number of VSIs belong to a VSIG
3195  * @hw: pointer to the hardware structure
3196  * @blk: HW block
3197  * @vsig: VSIG to query
3198  * @refs: pointer to variable to receive the reference count
3199  */
3200 static enum ice_status
3201 ice_vsig_get_ref(struct ice_hw *hw, enum ice_block blk, u16 vsig, u16 *refs)
3202 {
3203 	u16 idx = vsig & ICE_VSIG_IDX_M;
3204 	struct ice_vsig_vsi *ptr;
3205 
3206 	*refs = 0;
3207 
3208 	if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use)
3209 		return ICE_ERR_DOES_NOT_EXIST;
3210 
3211 	ptr = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
3212 	while (ptr) {
3213 		(*refs)++;
3214 		ptr = ptr->next_vsi;
3215 	}
3216 
3217 	return 0;
3218 }
3219 
3220 /**
3221  * ice_has_prof_vsig - check to see if VSIG has a specific profile
3222  * @hw: pointer to the hardware structure
3223  * @blk: HW block
3224  * @vsig: VSIG to check against
3225  * @hdl: profile handle
3226  */
3227 static bool
3228 ice_has_prof_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig, u64 hdl)
3229 {
3230 	u16 idx = vsig & ICE_VSIG_IDX_M;
3231 	struct ice_vsig_prof *ent;
3232 
3233 	list_for_each_entry(ent, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
3234 			    list)
3235 		if (ent->profile_cookie == hdl)
3236 			return true;
3237 
3238 	ice_debug(hw, ICE_DBG_INIT,
3239 		  "Characteristic list for VSI group %d not found.\n",
3240 		  vsig);
3241 	return false;
3242 }
3243 
3244 /**
3245  * ice_prof_bld_es - build profile ID extraction sequence changes
3246  * @hw: pointer to the HW struct
3247  * @blk: hardware block
3248  * @bld: the update package buffer build to add to
3249  * @chgs: the list of changes to make in hardware
3250  */
3251 static enum ice_status
3252 ice_prof_bld_es(struct ice_hw *hw, enum ice_block blk,
3253 		struct ice_buf_build *bld, struct list_head *chgs)
3254 {
3255 	u16 vec_size = hw->blk[blk].es.fvw * sizeof(struct ice_fv_word);
3256 	struct ice_chs_chg *tmp;
3257 
3258 	list_for_each_entry(tmp, chgs, list_entry)
3259 		if (tmp->type == ICE_PTG_ES_ADD && tmp->add_prof) {
3260 			u16 off = tmp->prof_id * hw->blk[blk].es.fvw;
3261 			struct ice_pkg_es *p;
3262 			u32 id;
3263 
3264 			id = ice_sect_id(blk, ICE_VEC_TBL);
3265 			p = (struct ice_pkg_es *)
3266 				ice_pkg_buf_alloc_section(bld, id, sizeof(*p) +
3267 							  vec_size -
3268 							  sizeof(p->es[0]));
3269 
3270 			if (!p)
3271 				return ICE_ERR_MAX_LIMIT;
3272 
3273 			p->count = cpu_to_le16(1);
3274 			p->offset = cpu_to_le16(tmp->prof_id);
3275 
3276 			memcpy(p->es, &hw->blk[blk].es.t[off], vec_size);
3277 		}
3278 
3279 	return 0;
3280 }
3281 
3282 /**
3283  * ice_prof_bld_tcam - build profile ID TCAM changes
3284  * @hw: pointer to the HW struct
3285  * @blk: hardware block
3286  * @bld: the update package buffer build to add to
3287  * @chgs: the list of changes to make in hardware
3288  */
3289 static enum ice_status
3290 ice_prof_bld_tcam(struct ice_hw *hw, enum ice_block blk,
3291 		  struct ice_buf_build *bld, struct list_head *chgs)
3292 {
3293 	struct ice_chs_chg *tmp;
3294 
3295 	list_for_each_entry(tmp, chgs, list_entry)
3296 		if (tmp->type == ICE_TCAM_ADD && tmp->add_tcam_idx) {
3297 			struct ice_prof_id_section *p;
3298 			u32 id;
3299 
3300 			id = ice_sect_id(blk, ICE_PROF_TCAM);
3301 			p = (struct ice_prof_id_section *)
3302 				ice_pkg_buf_alloc_section(bld, id, sizeof(*p));
3303 
3304 			if (!p)
3305 				return ICE_ERR_MAX_LIMIT;
3306 
3307 			p->count = cpu_to_le16(1);
3308 			p->entry[0].addr = cpu_to_le16(tmp->tcam_idx);
3309 			p->entry[0].prof_id = tmp->prof_id;
3310 
3311 			memcpy(p->entry[0].key,
3312 			       &hw->blk[blk].prof.t[tmp->tcam_idx].key,
3313 			       sizeof(hw->blk[blk].prof.t->key));
3314 		}
3315 
3316 	return 0;
3317 }
3318 
3319 /**
3320  * ice_prof_bld_xlt1 - build XLT1 changes
3321  * @blk: hardware block
3322  * @bld: the update package buffer build to add to
3323  * @chgs: the list of changes to make in hardware
3324  */
3325 static enum ice_status
3326 ice_prof_bld_xlt1(enum ice_block blk, struct ice_buf_build *bld,
3327 		  struct list_head *chgs)
3328 {
3329 	struct ice_chs_chg *tmp;
3330 
3331 	list_for_each_entry(tmp, chgs, list_entry)
3332 		if (tmp->type == ICE_PTG_ES_ADD && tmp->add_ptg) {
3333 			struct ice_xlt1_section *p;
3334 			u32 id;
3335 
3336 			id = ice_sect_id(blk, ICE_XLT1);
3337 			p = (struct ice_xlt1_section *)
3338 				ice_pkg_buf_alloc_section(bld, id, sizeof(*p));
3339 
3340 			if (!p)
3341 				return ICE_ERR_MAX_LIMIT;
3342 
3343 			p->count = cpu_to_le16(1);
3344 			p->offset = cpu_to_le16(tmp->ptype);
3345 			p->value[0] = tmp->ptg;
3346 		}
3347 
3348 	return 0;
3349 }
3350 
3351 /**
3352  * ice_prof_bld_xlt2 - build XLT2 changes
3353  * @blk: hardware block
3354  * @bld: the update package buffer build to add to
3355  * @chgs: the list of changes to make in hardware
3356  */
3357 static enum ice_status
3358 ice_prof_bld_xlt2(enum ice_block blk, struct ice_buf_build *bld,
3359 		  struct list_head *chgs)
3360 {
3361 	struct ice_chs_chg *tmp;
3362 
3363 	list_for_each_entry(tmp, chgs, list_entry) {
3364 		struct ice_xlt2_section *p;
3365 		u32 id;
3366 
3367 		switch (tmp->type) {
3368 		case ICE_VSIG_ADD:
3369 		case ICE_VSI_MOVE:
3370 		case ICE_VSIG_REM:
3371 			id = ice_sect_id(blk, ICE_XLT2);
3372 			p = (struct ice_xlt2_section *)
3373 				ice_pkg_buf_alloc_section(bld, id, sizeof(*p));
3374 
3375 			if (!p)
3376 				return ICE_ERR_MAX_LIMIT;
3377 
3378 			p->count = cpu_to_le16(1);
3379 			p->offset = cpu_to_le16(tmp->vsi);
3380 			p->value[0] = cpu_to_le16(tmp->vsig);
3381 			break;
3382 		default:
3383 			break;
3384 		}
3385 	}
3386 
3387 	return 0;
3388 }
3389 
3390 /**
3391  * ice_upd_prof_hw - update hardware using the change list
3392  * @hw: pointer to the HW struct
3393  * @blk: hardware block
3394  * @chgs: the list of changes to make in hardware
3395  */
3396 static enum ice_status
3397 ice_upd_prof_hw(struct ice_hw *hw, enum ice_block blk,
3398 		struct list_head *chgs)
3399 {
3400 	struct ice_buf_build *b;
3401 	struct ice_chs_chg *tmp;
3402 	enum ice_status status;
3403 	u16 pkg_sects;
3404 	u16 xlt1 = 0;
3405 	u16 xlt2 = 0;
3406 	u16 tcam = 0;
3407 	u16 es = 0;
3408 	u16 sects;
3409 
3410 	/* count number of sections we need */
3411 	list_for_each_entry(tmp, chgs, list_entry) {
3412 		switch (tmp->type) {
3413 		case ICE_PTG_ES_ADD:
3414 			if (tmp->add_ptg)
3415 				xlt1++;
3416 			if (tmp->add_prof)
3417 				es++;
3418 			break;
3419 		case ICE_TCAM_ADD:
3420 			tcam++;
3421 			break;
3422 		case ICE_VSIG_ADD:
3423 		case ICE_VSI_MOVE:
3424 		case ICE_VSIG_REM:
3425 			xlt2++;
3426 			break;
3427 		default:
3428 			break;
3429 		}
3430 	}
3431 	sects = xlt1 + xlt2 + tcam + es;
3432 
3433 	if (!sects)
3434 		return 0;
3435 
3436 	/* Build update package buffer */
3437 	b = ice_pkg_buf_alloc(hw);
3438 	if (!b)
3439 		return ICE_ERR_NO_MEMORY;
3440 
3441 	status = ice_pkg_buf_reserve_section(b, sects);
3442 	if (status)
3443 		goto error_tmp;
3444 
3445 	/* Preserve order of table update: ES, TCAM, PTG, VSIG */
3446 	if (es) {
3447 		status = ice_prof_bld_es(hw, blk, b, chgs);
3448 		if (status)
3449 			goto error_tmp;
3450 	}
3451 
3452 	if (tcam) {
3453 		status = ice_prof_bld_tcam(hw, blk, b, chgs);
3454 		if (status)
3455 			goto error_tmp;
3456 	}
3457 
3458 	if (xlt1) {
3459 		status = ice_prof_bld_xlt1(blk, b, chgs);
3460 		if (status)
3461 			goto error_tmp;
3462 	}
3463 
3464 	if (xlt2) {
3465 		status = ice_prof_bld_xlt2(blk, b, chgs);
3466 		if (status)
3467 			goto error_tmp;
3468 	}
3469 
3470 	/* After package buffer build check if the section count in buffer is
3471 	 * non-zero and matches the number of sections detected for package
3472 	 * update.
3473 	 */
3474 	pkg_sects = ice_pkg_buf_get_active_sections(b);
3475 	if (!pkg_sects || pkg_sects != sects) {
3476 		status = ICE_ERR_INVAL_SIZE;
3477 		goto error_tmp;
3478 	}
3479 
3480 	/* update package */
3481 	status = ice_update_pkg(hw, ice_pkg_buf(b), 1);
3482 	if (status == ICE_ERR_AQ_ERROR)
3483 		ice_debug(hw, ICE_DBG_INIT, "Unable to update HW profile\n");
3484 
3485 error_tmp:
3486 	ice_pkg_buf_free(hw, b);
3487 	return status;
3488 }
3489 
3490 /**
3491  * ice_update_fd_mask - set Flow Director Field Vector mask for a profile
3492  * @hw: pointer to the HW struct
3493  * @prof_id: profile ID
3494  * @mask_sel: mask select
3495  *
3496  * This function enable any of the masks selected by the mask select parameter
3497  * for the profile specified.
3498  */
3499 static void ice_update_fd_mask(struct ice_hw *hw, u16 prof_id, u32 mask_sel)
3500 {
3501 	wr32(hw, GLQF_FDMASK_SEL(prof_id), mask_sel);
3502 
3503 	ice_debug(hw, ICE_DBG_INIT, "fd mask(%d): %x = %x\n", prof_id,
3504 		  GLQF_FDMASK_SEL(prof_id), mask_sel);
3505 }
3506 
3507 struct ice_fd_src_dst_pair {
3508 	u8 prot_id;
3509 	u8 count;
3510 	u16 off;
3511 };
3512 
3513 static const struct ice_fd_src_dst_pair ice_fd_pairs[] = {
3514 	/* These are defined in pairs */
3515 	{ ICE_PROT_IPV4_OF_OR_S, 2, 12 },
3516 	{ ICE_PROT_IPV4_OF_OR_S, 2, 16 },
3517 
3518 	{ ICE_PROT_IPV4_IL, 2, 12 },
3519 	{ ICE_PROT_IPV4_IL, 2, 16 },
3520 
3521 	{ ICE_PROT_IPV6_OF_OR_S, 8, 8 },
3522 	{ ICE_PROT_IPV6_OF_OR_S, 8, 24 },
3523 
3524 	{ ICE_PROT_IPV6_IL, 8, 8 },
3525 	{ ICE_PROT_IPV6_IL, 8, 24 },
3526 
3527 	{ ICE_PROT_TCP_IL, 1, 0 },
3528 	{ ICE_PROT_TCP_IL, 1, 2 },
3529 
3530 	{ ICE_PROT_UDP_OF, 1, 0 },
3531 	{ ICE_PROT_UDP_OF, 1, 2 },
3532 
3533 	{ ICE_PROT_UDP_IL_OR_S, 1, 0 },
3534 	{ ICE_PROT_UDP_IL_OR_S, 1, 2 },
3535 
3536 	{ ICE_PROT_SCTP_IL, 1, 0 },
3537 	{ ICE_PROT_SCTP_IL, 1, 2 }
3538 };
3539 
3540 #define ICE_FD_SRC_DST_PAIR_COUNT	ARRAY_SIZE(ice_fd_pairs)
3541 
3542 /**
3543  * ice_update_fd_swap - set register appropriately for a FD FV extraction
3544  * @hw: pointer to the HW struct
3545  * @prof_id: profile ID
3546  * @es: extraction sequence (length of array is determined by the block)
3547  */
3548 static enum ice_status
3549 ice_update_fd_swap(struct ice_hw *hw, u16 prof_id, struct ice_fv_word *es)
3550 {
3551 	DECLARE_BITMAP(pair_list, ICE_FD_SRC_DST_PAIR_COUNT);
3552 	u8 pair_start[ICE_FD_SRC_DST_PAIR_COUNT] = { 0 };
3553 #define ICE_FD_FV_NOT_FOUND (-2)
3554 	s8 first_free = ICE_FD_FV_NOT_FOUND;
3555 	u8 used[ICE_MAX_FV_WORDS] = { 0 };
3556 	s8 orig_free, si;
3557 	u32 mask_sel = 0;
3558 	u8 i, j, k;
3559 
3560 	bitmap_zero(pair_list, ICE_FD_SRC_DST_PAIR_COUNT);
3561 
3562 	/* This code assumes that the Flow Director field vectors are assigned
3563 	 * from the end of the FV indexes working towards the zero index, that
3564 	 * only complete fields will be included and will be consecutive, and
3565 	 * that there are no gaps between valid indexes.
3566 	 */
3567 
3568 	/* Determine swap fields present */
3569 	for (i = 0; i < hw->blk[ICE_BLK_FD].es.fvw; i++) {
3570 		/* Find the first free entry, assuming right to left population.
3571 		 * This is where we can start adding additional pairs if needed.
3572 		 */
3573 		if (first_free == ICE_FD_FV_NOT_FOUND && es[i].prot_id !=
3574 		    ICE_PROT_INVALID)
3575 			first_free = i - 1;
3576 
3577 		for (j = 0; j < ICE_FD_SRC_DST_PAIR_COUNT; j++)
3578 			if (es[i].prot_id == ice_fd_pairs[j].prot_id &&
3579 			    es[i].off == ice_fd_pairs[j].off) {
3580 				set_bit(j, pair_list);
3581 				pair_start[j] = i;
3582 			}
3583 	}
3584 
3585 	orig_free = first_free;
3586 
3587 	/* determine missing swap fields that need to be added */
3588 	for (i = 0; i < ICE_FD_SRC_DST_PAIR_COUNT; i += 2) {
3589 		u8 bit1 = test_bit(i + 1, pair_list);
3590 		u8 bit0 = test_bit(i, pair_list);
3591 
3592 		if (bit0 ^ bit1) {
3593 			u8 index;
3594 
3595 			/* add the appropriate 'paired' entry */
3596 			if (!bit0)
3597 				index = i;
3598 			else
3599 				index = i + 1;
3600 
3601 			/* check for room */
3602 			if (first_free + 1 < (s8)ice_fd_pairs[index].count)
3603 				return ICE_ERR_MAX_LIMIT;
3604 
3605 			/* place in extraction sequence */
3606 			for (k = 0; k < ice_fd_pairs[index].count; k++) {
3607 				es[first_free - k].prot_id =
3608 					ice_fd_pairs[index].prot_id;
3609 				es[first_free - k].off =
3610 					ice_fd_pairs[index].off + (k * 2);
3611 
3612 				if (k > first_free)
3613 					return ICE_ERR_OUT_OF_RANGE;
3614 
3615 				/* keep track of non-relevant fields */
3616 				mask_sel |= BIT(first_free - k);
3617 			}
3618 
3619 			pair_start[index] = first_free;
3620 			first_free -= ice_fd_pairs[index].count;
3621 		}
3622 	}
3623 
3624 	/* fill in the swap array */
3625 	si = hw->blk[ICE_BLK_FD].es.fvw - 1;
3626 	while (si >= 0) {
3627 		u8 indexes_used = 1;
3628 
3629 		/* assume flat at this index */
3630 #define ICE_SWAP_VALID	0x80
3631 		used[si] = si | ICE_SWAP_VALID;
3632 
3633 		if (orig_free == ICE_FD_FV_NOT_FOUND || si <= orig_free) {
3634 			si -= indexes_used;
3635 			continue;
3636 		}
3637 
3638 		/* check for a swap location */
3639 		for (j = 0; j < ICE_FD_SRC_DST_PAIR_COUNT; j++)
3640 			if (es[si].prot_id == ice_fd_pairs[j].prot_id &&
3641 			    es[si].off == ice_fd_pairs[j].off) {
3642 				u8 idx;
3643 
3644 				/* determine the appropriate matching field */
3645 				idx = j + ((j % 2) ? -1 : 1);
3646 
3647 				indexes_used = ice_fd_pairs[idx].count;
3648 				for (k = 0; k < indexes_used; k++) {
3649 					used[si - k] = (pair_start[idx] - k) |
3650 						ICE_SWAP_VALID;
3651 				}
3652 
3653 				break;
3654 			}
3655 
3656 		si -= indexes_used;
3657 	}
3658 
3659 	/* for each set of 4 swap and 4 inset indexes, write the appropriate
3660 	 * register
3661 	 */
3662 	for (j = 0; j < hw->blk[ICE_BLK_FD].es.fvw / 4; j++) {
3663 		u32 raw_swap = 0;
3664 		u32 raw_in = 0;
3665 
3666 		for (k = 0; k < 4; k++) {
3667 			u8 idx;
3668 
3669 			idx = (j * 4) + k;
3670 			if (used[idx] && !(mask_sel & BIT(idx))) {
3671 				raw_swap |= used[idx] << (k * BITS_PER_BYTE);
3672 #define ICE_INSET_DFLT 0x9f
3673 				raw_in |= ICE_INSET_DFLT << (k * BITS_PER_BYTE);
3674 			}
3675 		}
3676 
3677 		/* write the appropriate swap register set */
3678 		wr32(hw, GLQF_FDSWAP(prof_id, j), raw_swap);
3679 
3680 		ice_debug(hw, ICE_DBG_INIT, "swap wr(%d, %d): %x = %08x\n",
3681 			  prof_id, j, GLQF_FDSWAP(prof_id, j), raw_swap);
3682 
3683 		/* write the appropriate inset register set */
3684 		wr32(hw, GLQF_FDINSET(prof_id, j), raw_in);
3685 
3686 		ice_debug(hw, ICE_DBG_INIT, "inset wr(%d, %d): %x = %08x\n",
3687 			  prof_id, j, GLQF_FDINSET(prof_id, j), raw_in);
3688 	}
3689 
3690 	/* initially clear the mask select for this profile */
3691 	ice_update_fd_mask(hw, prof_id, 0);
3692 
3693 	return 0;
3694 }
3695 
3696 /**
3697  * ice_add_prof - add profile
3698  * @hw: pointer to the HW struct
3699  * @blk: hardware block
3700  * @id: profile tracking ID
3701  * @ptypes: array of bitmaps indicating ptypes (ICE_FLOW_PTYPE_MAX bits)
3702  * @es: extraction sequence (length of array is determined by the block)
3703  *
3704  * This function registers a profile, which matches a set of PTGs with a
3705  * particular extraction sequence. While the hardware profile is allocated
3706  * it will not be written until the first call to ice_add_flow that specifies
3707  * the ID value used here.
3708  */
3709 enum ice_status
3710 ice_add_prof(struct ice_hw *hw, enum ice_block blk, u64 id, u8 ptypes[],
3711 	     struct ice_fv_word *es)
3712 {
3713 	u32 bytes = DIV_ROUND_UP(ICE_FLOW_PTYPE_MAX, BITS_PER_BYTE);
3714 	DECLARE_BITMAP(ptgs_used, ICE_XLT1_CNT);
3715 	struct ice_prof_map *prof;
3716 	enum ice_status status;
3717 	u8 byte = 0;
3718 	u8 prof_id;
3719 
3720 	bitmap_zero(ptgs_used, ICE_XLT1_CNT);
3721 
3722 	mutex_lock(&hw->blk[blk].es.prof_map_lock);
3723 
3724 	/* search for existing profile */
3725 	status = ice_find_prof_id(hw, blk, es, &prof_id);
3726 	if (status) {
3727 		/* allocate profile ID */
3728 		status = ice_alloc_prof_id(hw, blk, &prof_id);
3729 		if (status)
3730 			goto err_ice_add_prof;
3731 		if (blk == ICE_BLK_FD) {
3732 			/* For Flow Director block, the extraction sequence may
3733 			 * need to be altered in the case where there are paired
3734 			 * fields that have no match. This is necessary because
3735 			 * for Flow Director, src and dest fields need to paired
3736 			 * for filter programming and these values are swapped
3737 			 * during Tx.
3738 			 */
3739 			status = ice_update_fd_swap(hw, prof_id, es);
3740 			if (status)
3741 				goto err_ice_add_prof;
3742 		}
3743 
3744 		/* and write new es */
3745 		ice_write_es(hw, blk, prof_id, es);
3746 	}
3747 
3748 	ice_prof_inc_ref(hw, blk, prof_id);
3749 
3750 	/* add profile info */
3751 	prof = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*prof), GFP_KERNEL);
3752 	if (!prof) {
3753 		status = ICE_ERR_NO_MEMORY;
3754 		goto err_ice_add_prof;
3755 	}
3756 
3757 	prof->profile_cookie = id;
3758 	prof->prof_id = prof_id;
3759 	prof->ptg_cnt = 0;
3760 	prof->context = 0;
3761 
3762 	/* build list of ptgs */
3763 	while (bytes && prof->ptg_cnt < ICE_MAX_PTG_PER_PROFILE) {
3764 		u8 bit;
3765 
3766 		if (!ptypes[byte]) {
3767 			bytes--;
3768 			byte++;
3769 			continue;
3770 		}
3771 
3772 		/* Examine 8 bits per byte */
3773 		for_each_set_bit(bit, (unsigned long *)&ptypes[byte],
3774 				 BITS_PER_BYTE) {
3775 			u16 ptype;
3776 			u8 ptg;
3777 			u8 m;
3778 
3779 			ptype = byte * BITS_PER_BYTE + bit;
3780 
3781 			/* The package should place all ptypes in a non-zero
3782 			 * PTG, so the following call should never fail.
3783 			 */
3784 			if (ice_ptg_find_ptype(hw, blk, ptype, &ptg))
3785 				continue;
3786 
3787 			/* If PTG is already added, skip and continue */
3788 			if (test_bit(ptg, ptgs_used))
3789 				continue;
3790 
3791 			set_bit(ptg, ptgs_used);
3792 			prof->ptg[prof->ptg_cnt] = ptg;
3793 
3794 			if (++prof->ptg_cnt >= ICE_MAX_PTG_PER_PROFILE)
3795 				break;
3796 
3797 			/* nothing left in byte, then exit */
3798 			m = ~(u8)((1 << (bit + 1)) - 1);
3799 			if (!(ptypes[byte] & m))
3800 				break;
3801 		}
3802 
3803 		bytes--;
3804 		byte++;
3805 	}
3806 
3807 	list_add(&prof->list, &hw->blk[blk].es.prof_map);
3808 	status = 0;
3809 
3810 err_ice_add_prof:
3811 	mutex_unlock(&hw->blk[blk].es.prof_map_lock);
3812 	return status;
3813 }
3814 
3815 /**
3816  * ice_search_prof_id_low - Search for a profile tracking ID low level
3817  * @hw: pointer to the HW struct
3818  * @blk: hardware block
3819  * @id: profile tracking ID
3820  *
3821  * This will search for a profile tracking ID which was previously added. This
3822  * version assumes that the caller has already acquired the prof map lock.
3823  */
3824 static struct ice_prof_map *
3825 ice_search_prof_id_low(struct ice_hw *hw, enum ice_block blk, u64 id)
3826 {
3827 	struct ice_prof_map *entry = NULL;
3828 	struct ice_prof_map *map;
3829 
3830 	list_for_each_entry(map, &hw->blk[blk].es.prof_map, list)
3831 		if (map->profile_cookie == id) {
3832 			entry = map;
3833 			break;
3834 		}
3835 
3836 	return entry;
3837 }
3838 
3839 /**
3840  * ice_search_prof_id - Search for a profile tracking ID
3841  * @hw: pointer to the HW struct
3842  * @blk: hardware block
3843  * @id: profile tracking ID
3844  *
3845  * This will search for a profile tracking ID which was previously added.
3846  */
3847 static struct ice_prof_map *
3848 ice_search_prof_id(struct ice_hw *hw, enum ice_block blk, u64 id)
3849 {
3850 	struct ice_prof_map *entry;
3851 
3852 	mutex_lock(&hw->blk[blk].es.prof_map_lock);
3853 	entry = ice_search_prof_id_low(hw, blk, id);
3854 	mutex_unlock(&hw->blk[blk].es.prof_map_lock);
3855 
3856 	return entry;
3857 }
3858 
3859 /**
3860  * ice_vsig_prof_id_count - count profiles in a VSIG
3861  * @hw: pointer to the HW struct
3862  * @blk: hardware block
3863  * @vsig: VSIG to remove the profile from
3864  */
3865 static u16
3866 ice_vsig_prof_id_count(struct ice_hw *hw, enum ice_block blk, u16 vsig)
3867 {
3868 	u16 idx = vsig & ICE_VSIG_IDX_M, count = 0;
3869 	struct ice_vsig_prof *p;
3870 
3871 	list_for_each_entry(p, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
3872 			    list)
3873 		count++;
3874 
3875 	return count;
3876 }
3877 
3878 /**
3879  * ice_rel_tcam_idx - release a TCAM index
3880  * @hw: pointer to the HW struct
3881  * @blk: hardware block
3882  * @idx: the index to release
3883  */
3884 static enum ice_status
3885 ice_rel_tcam_idx(struct ice_hw *hw, enum ice_block blk, u16 idx)
3886 {
3887 	/* Masks to invoke a never match entry */
3888 	u8 vl_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3889 	u8 dc_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFE, 0xFF, 0xFF, 0xFF, 0xFF };
3890 	u8 nm_msk[ICE_TCAM_KEY_VAL_SZ] = { 0x01, 0x00, 0x00, 0x00, 0x00 };
3891 	enum ice_status status;
3892 
3893 	/* write the TCAM entry */
3894 	status = ice_tcam_write_entry(hw, blk, idx, 0, 0, 0, 0, 0, vl_msk,
3895 				      dc_msk, nm_msk);
3896 	if (status)
3897 		return status;
3898 
3899 	/* release the TCAM entry */
3900 	status = ice_free_tcam_ent(hw, blk, idx);
3901 
3902 	return status;
3903 }
3904 
3905 /**
3906  * ice_rem_prof_id - remove one profile from a VSIG
3907  * @hw: pointer to the HW struct
3908  * @blk: hardware block
3909  * @prof: pointer to profile structure to remove
3910  */
3911 static enum ice_status
3912 ice_rem_prof_id(struct ice_hw *hw, enum ice_block blk,
3913 		struct ice_vsig_prof *prof)
3914 {
3915 	enum ice_status status;
3916 	u16 i;
3917 
3918 	for (i = 0; i < prof->tcam_count; i++)
3919 		if (prof->tcam[i].in_use) {
3920 			prof->tcam[i].in_use = false;
3921 			status = ice_rel_tcam_idx(hw, blk,
3922 						  prof->tcam[i].tcam_idx);
3923 			if (status)
3924 				return ICE_ERR_HW_TABLE;
3925 		}
3926 
3927 	return 0;
3928 }
3929 
3930 /**
3931  * ice_rem_vsig - remove VSIG
3932  * @hw: pointer to the HW struct
3933  * @blk: hardware block
3934  * @vsig: the VSIG to remove
3935  * @chg: the change list
3936  */
3937 static enum ice_status
3938 ice_rem_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig,
3939 	     struct list_head *chg)
3940 {
3941 	u16 idx = vsig & ICE_VSIG_IDX_M;
3942 	struct ice_vsig_vsi *vsi_cur;
3943 	struct ice_vsig_prof *d, *t;
3944 	enum ice_status status;
3945 
3946 	/* remove TCAM entries */
3947 	list_for_each_entry_safe(d, t,
3948 				 &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
3949 				 list) {
3950 		status = ice_rem_prof_id(hw, blk, d);
3951 		if (status)
3952 			return status;
3953 
3954 		list_del(&d->list);
3955 		devm_kfree(ice_hw_to_dev(hw), d);
3956 	}
3957 
3958 	/* Move all VSIS associated with this VSIG to the default VSIG */
3959 	vsi_cur = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
3960 	/* If the VSIG has at least 1 VSI then iterate through the list
3961 	 * and remove the VSIs before deleting the group.
3962 	 */
3963 	if (vsi_cur)
3964 		do {
3965 			struct ice_vsig_vsi *tmp = vsi_cur->next_vsi;
3966 			struct ice_chs_chg *p;
3967 
3968 			p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p),
3969 					 GFP_KERNEL);
3970 			if (!p)
3971 				return ICE_ERR_NO_MEMORY;
3972 
3973 			p->type = ICE_VSIG_REM;
3974 			p->orig_vsig = vsig;
3975 			p->vsig = ICE_DEFAULT_VSIG;
3976 			p->vsi = vsi_cur - hw->blk[blk].xlt2.vsis;
3977 
3978 			list_add(&p->list_entry, chg);
3979 
3980 			vsi_cur = tmp;
3981 		} while (vsi_cur);
3982 
3983 	return ice_vsig_free(hw, blk, vsig);
3984 }
3985 
3986 /**
3987  * ice_rem_prof_id_vsig - remove a specific profile from a VSIG
3988  * @hw: pointer to the HW struct
3989  * @blk: hardware block
3990  * @vsig: VSIG to remove the profile from
3991  * @hdl: profile handle indicating which profile to remove
3992  * @chg: list to receive a record of changes
3993  */
3994 static enum ice_status
3995 ice_rem_prof_id_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig, u64 hdl,
3996 		     struct list_head *chg)
3997 {
3998 	u16 idx = vsig & ICE_VSIG_IDX_M;
3999 	struct ice_vsig_prof *p, *t;
4000 	enum ice_status status;
4001 
4002 	list_for_each_entry_safe(p, t,
4003 				 &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
4004 				 list)
4005 		if (p->profile_cookie == hdl) {
4006 			if (ice_vsig_prof_id_count(hw, blk, vsig) == 1)
4007 				/* this is the last profile, remove the VSIG */
4008 				return ice_rem_vsig(hw, blk, vsig, chg);
4009 
4010 			status = ice_rem_prof_id(hw, blk, p);
4011 			if (!status) {
4012 				list_del(&p->list);
4013 				devm_kfree(ice_hw_to_dev(hw), p);
4014 			}
4015 			return status;
4016 		}
4017 
4018 	return ICE_ERR_DOES_NOT_EXIST;
4019 }
4020 
4021 /**
4022  * ice_rem_flow_all - remove all flows with a particular profile
4023  * @hw: pointer to the HW struct
4024  * @blk: hardware block
4025  * @id: profile tracking ID
4026  */
4027 static enum ice_status
4028 ice_rem_flow_all(struct ice_hw *hw, enum ice_block blk, u64 id)
4029 {
4030 	struct ice_chs_chg *del, *tmp;
4031 	enum ice_status status;
4032 	struct list_head chg;
4033 	u16 i;
4034 
4035 	INIT_LIST_HEAD(&chg);
4036 
4037 	for (i = 1; i < ICE_MAX_VSIGS; i++)
4038 		if (hw->blk[blk].xlt2.vsig_tbl[i].in_use) {
4039 			if (ice_has_prof_vsig(hw, blk, i, id)) {
4040 				status = ice_rem_prof_id_vsig(hw, blk, i, id,
4041 							      &chg);
4042 				if (status)
4043 					goto err_ice_rem_flow_all;
4044 			}
4045 		}
4046 
4047 	status = ice_upd_prof_hw(hw, blk, &chg);
4048 
4049 err_ice_rem_flow_all:
4050 	list_for_each_entry_safe(del, tmp, &chg, list_entry) {
4051 		list_del(&del->list_entry);
4052 		devm_kfree(ice_hw_to_dev(hw), del);
4053 	}
4054 
4055 	return status;
4056 }
4057 
4058 /**
4059  * ice_rem_prof - remove profile
4060  * @hw: pointer to the HW struct
4061  * @blk: hardware block
4062  * @id: profile tracking ID
4063  *
4064  * This will remove the profile specified by the ID parameter, which was
4065  * previously created through ice_add_prof. If any existing entries
4066  * are associated with this profile, they will be removed as well.
4067  */
4068 enum ice_status ice_rem_prof(struct ice_hw *hw, enum ice_block blk, u64 id)
4069 {
4070 	struct ice_prof_map *pmap;
4071 	enum ice_status status;
4072 
4073 	mutex_lock(&hw->blk[blk].es.prof_map_lock);
4074 
4075 	pmap = ice_search_prof_id_low(hw, blk, id);
4076 	if (!pmap) {
4077 		status = ICE_ERR_DOES_NOT_EXIST;
4078 		goto err_ice_rem_prof;
4079 	}
4080 
4081 	/* remove all flows with this profile */
4082 	status = ice_rem_flow_all(hw, blk, pmap->profile_cookie);
4083 	if (status)
4084 		goto err_ice_rem_prof;
4085 
4086 	/* dereference profile, and possibly remove */
4087 	ice_prof_dec_ref(hw, blk, pmap->prof_id);
4088 
4089 	list_del(&pmap->list);
4090 	devm_kfree(ice_hw_to_dev(hw), pmap);
4091 
4092 err_ice_rem_prof:
4093 	mutex_unlock(&hw->blk[blk].es.prof_map_lock);
4094 	return status;
4095 }
4096 
4097 /**
4098  * ice_get_prof - get profile
4099  * @hw: pointer to the HW struct
4100  * @blk: hardware block
4101  * @hdl: profile handle
4102  * @chg: change list
4103  */
4104 static enum ice_status
4105 ice_get_prof(struct ice_hw *hw, enum ice_block blk, u64 hdl,
4106 	     struct list_head *chg)
4107 {
4108 	struct ice_prof_map *map;
4109 	struct ice_chs_chg *p;
4110 	u16 i;
4111 
4112 	/* Get the details on the profile specified by the handle ID */
4113 	map = ice_search_prof_id(hw, blk, hdl);
4114 	if (!map)
4115 		return ICE_ERR_DOES_NOT_EXIST;
4116 
4117 	for (i = 0; i < map->ptg_cnt; i++)
4118 		if (!hw->blk[blk].es.written[map->prof_id]) {
4119 			/* add ES to change list */
4120 			p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p),
4121 					 GFP_KERNEL);
4122 			if (!p)
4123 				goto err_ice_get_prof;
4124 
4125 			p->type = ICE_PTG_ES_ADD;
4126 			p->ptype = 0;
4127 			p->ptg = map->ptg[i];
4128 			p->add_ptg = 0;
4129 
4130 			p->add_prof = 1;
4131 			p->prof_id = map->prof_id;
4132 
4133 			hw->blk[blk].es.written[map->prof_id] = true;
4134 
4135 			list_add(&p->list_entry, chg);
4136 		}
4137 
4138 	return 0;
4139 
4140 err_ice_get_prof:
4141 	/* let caller clean up the change list */
4142 	return ICE_ERR_NO_MEMORY;
4143 }
4144 
4145 /**
4146  * ice_get_profs_vsig - get a copy of the list of profiles from a VSIG
4147  * @hw: pointer to the HW struct
4148  * @blk: hardware block
4149  * @vsig: VSIG from which to copy the list
4150  * @lst: output list
4151  *
4152  * This routine makes a copy of the list of profiles in the specified VSIG.
4153  */
4154 static enum ice_status
4155 ice_get_profs_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig,
4156 		   struct list_head *lst)
4157 {
4158 	struct ice_vsig_prof *ent1, *ent2;
4159 	u16 idx = vsig & ICE_VSIG_IDX_M;
4160 
4161 	list_for_each_entry(ent1, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
4162 			    list) {
4163 		struct ice_vsig_prof *p;
4164 
4165 		/* copy to the input list */
4166 		p = devm_kmemdup(ice_hw_to_dev(hw), ent1, sizeof(*p),
4167 				 GFP_KERNEL);
4168 		if (!p)
4169 			goto err_ice_get_profs_vsig;
4170 
4171 		list_add_tail(&p->list, lst);
4172 	}
4173 
4174 	return 0;
4175 
4176 err_ice_get_profs_vsig:
4177 	list_for_each_entry_safe(ent1, ent2, lst, list) {
4178 		list_del(&ent1->list);
4179 		devm_kfree(ice_hw_to_dev(hw), ent1);
4180 	}
4181 
4182 	return ICE_ERR_NO_MEMORY;
4183 }
4184 
4185 /**
4186  * ice_add_prof_to_lst - add profile entry to a list
4187  * @hw: pointer to the HW struct
4188  * @blk: hardware block
4189  * @lst: the list to be added to
4190  * @hdl: profile handle of entry to add
4191  */
4192 static enum ice_status
4193 ice_add_prof_to_lst(struct ice_hw *hw, enum ice_block blk,
4194 		    struct list_head *lst, u64 hdl)
4195 {
4196 	struct ice_prof_map *map;
4197 	struct ice_vsig_prof *p;
4198 	u16 i;
4199 
4200 	map = ice_search_prof_id(hw, blk, hdl);
4201 	if (!map)
4202 		return ICE_ERR_DOES_NOT_EXIST;
4203 
4204 	p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p), GFP_KERNEL);
4205 	if (!p)
4206 		return ICE_ERR_NO_MEMORY;
4207 
4208 	p->profile_cookie = map->profile_cookie;
4209 	p->prof_id = map->prof_id;
4210 	p->tcam_count = map->ptg_cnt;
4211 
4212 	for (i = 0; i < map->ptg_cnt; i++) {
4213 		p->tcam[i].prof_id = map->prof_id;
4214 		p->tcam[i].tcam_idx = ICE_INVALID_TCAM;
4215 		p->tcam[i].ptg = map->ptg[i];
4216 	}
4217 
4218 	list_add(&p->list, lst);
4219 
4220 	return 0;
4221 }
4222 
4223 /**
4224  * ice_move_vsi - move VSI to another VSIG
4225  * @hw: pointer to the HW struct
4226  * @blk: hardware block
4227  * @vsi: the VSI to move
4228  * @vsig: the VSIG to move the VSI to
4229  * @chg: the change list
4230  */
4231 static enum ice_status
4232 ice_move_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 vsig,
4233 	     struct list_head *chg)
4234 {
4235 	enum ice_status status;
4236 	struct ice_chs_chg *p;
4237 	u16 orig_vsig;
4238 
4239 	p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p), GFP_KERNEL);
4240 	if (!p)
4241 		return ICE_ERR_NO_MEMORY;
4242 
4243 	status = ice_vsig_find_vsi(hw, blk, vsi, &orig_vsig);
4244 	if (!status)
4245 		status = ice_vsig_add_mv_vsi(hw, blk, vsi, vsig);
4246 
4247 	if (status) {
4248 		devm_kfree(ice_hw_to_dev(hw), p);
4249 		return status;
4250 	}
4251 
4252 	p->type = ICE_VSI_MOVE;
4253 	p->vsi = vsi;
4254 	p->orig_vsig = orig_vsig;
4255 	p->vsig = vsig;
4256 
4257 	list_add(&p->list_entry, chg);
4258 
4259 	return 0;
4260 }
4261 
4262 /**
4263  * ice_rem_chg_tcam_ent - remove a specific TCAM entry from change list
4264  * @hw: pointer to the HW struct
4265  * @idx: the index of the TCAM entry to remove
4266  * @chg: the list of change structures to search
4267  */
4268 static void
4269 ice_rem_chg_tcam_ent(struct ice_hw *hw, u16 idx, struct list_head *chg)
4270 {
4271 	struct ice_chs_chg *pos, *tmp;
4272 
4273 	list_for_each_entry_safe(tmp, pos, chg, list_entry)
4274 		if (tmp->type == ICE_TCAM_ADD && tmp->tcam_idx == idx) {
4275 			list_del(&tmp->list_entry);
4276 			devm_kfree(ice_hw_to_dev(hw), tmp);
4277 		}
4278 }
4279 
4280 /**
4281  * ice_prof_tcam_ena_dis - add enable or disable TCAM change
4282  * @hw: pointer to the HW struct
4283  * @blk: hardware block
4284  * @enable: true to enable, false to disable
4285  * @vsig: the VSIG of the TCAM entry
4286  * @tcam: pointer the TCAM info structure of the TCAM to disable
4287  * @chg: the change list
4288  *
4289  * This function appends an enable or disable TCAM entry in the change log
4290  */
4291 static enum ice_status
4292 ice_prof_tcam_ena_dis(struct ice_hw *hw, enum ice_block blk, bool enable,
4293 		      u16 vsig, struct ice_tcam_inf *tcam,
4294 		      struct list_head *chg)
4295 {
4296 	enum ice_status status;
4297 	struct ice_chs_chg *p;
4298 
4299 	u8 vl_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4300 	u8 dc_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0x00, 0x00, 0x00 };
4301 	u8 nm_msk[ICE_TCAM_KEY_VAL_SZ] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
4302 
4303 	/* if disabling, free the TCAM */
4304 	if (!enable) {
4305 		status = ice_rel_tcam_idx(hw, blk, tcam->tcam_idx);
4306 
4307 		/* if we have already created a change for this TCAM entry, then
4308 		 * we need to remove that entry, in order to prevent writing to
4309 		 * a TCAM entry we no longer will have ownership of.
4310 		 */
4311 		ice_rem_chg_tcam_ent(hw, tcam->tcam_idx, chg);
4312 		tcam->tcam_idx = 0;
4313 		tcam->in_use = 0;
4314 		return status;
4315 	}
4316 
4317 	/* for re-enabling, reallocate a TCAM */
4318 	status = ice_alloc_tcam_ent(hw, blk, &tcam->tcam_idx);
4319 	if (status)
4320 		return status;
4321 
4322 	/* add TCAM to change list */
4323 	p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p), GFP_KERNEL);
4324 	if (!p)
4325 		return ICE_ERR_NO_MEMORY;
4326 
4327 	status = ice_tcam_write_entry(hw, blk, tcam->tcam_idx, tcam->prof_id,
4328 				      tcam->ptg, vsig, 0, 0, vl_msk, dc_msk,
4329 				      nm_msk);
4330 	if (status)
4331 		goto err_ice_prof_tcam_ena_dis;
4332 
4333 	tcam->in_use = 1;
4334 
4335 	p->type = ICE_TCAM_ADD;
4336 	p->add_tcam_idx = true;
4337 	p->prof_id = tcam->prof_id;
4338 	p->ptg = tcam->ptg;
4339 	p->vsig = 0;
4340 	p->tcam_idx = tcam->tcam_idx;
4341 
4342 	/* log change */
4343 	list_add(&p->list_entry, chg);
4344 
4345 	return 0;
4346 
4347 err_ice_prof_tcam_ena_dis:
4348 	devm_kfree(ice_hw_to_dev(hw), p);
4349 	return status;
4350 }
4351 
4352 /**
4353  * ice_adj_prof_priorities - adjust profile based on priorities
4354  * @hw: pointer to the HW struct
4355  * @blk: hardware block
4356  * @vsig: the VSIG for which to adjust profile priorities
4357  * @chg: the change list
4358  */
4359 static enum ice_status
4360 ice_adj_prof_priorities(struct ice_hw *hw, enum ice_block blk, u16 vsig,
4361 			struct list_head *chg)
4362 {
4363 	DECLARE_BITMAP(ptgs_used, ICE_XLT1_CNT);
4364 	struct ice_vsig_prof *t;
4365 	enum ice_status status;
4366 	u16 idx;
4367 
4368 	bitmap_zero(ptgs_used, ICE_XLT1_CNT);
4369 	idx = vsig & ICE_VSIG_IDX_M;
4370 
4371 	/* Priority is based on the order in which the profiles are added. The
4372 	 * newest added profile has highest priority and the oldest added
4373 	 * profile has the lowest priority. Since the profile property list for
4374 	 * a VSIG is sorted from newest to oldest, this code traverses the list
4375 	 * in order and enables the first of each PTG that it finds (that is not
4376 	 * already enabled); it also disables any duplicate PTGs that it finds
4377 	 * in the older profiles (that are currently enabled).
4378 	 */
4379 
4380 	list_for_each_entry(t, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
4381 			    list) {
4382 		u16 i;
4383 
4384 		for (i = 0; i < t->tcam_count; i++) {
4385 			/* Scan the priorities from newest to oldest.
4386 			 * Make sure that the newest profiles take priority.
4387 			 */
4388 			if (test_bit(t->tcam[i].ptg, ptgs_used) &&
4389 			    t->tcam[i].in_use) {
4390 				/* need to mark this PTG as never match, as it
4391 				 * was already in use and therefore duplicate
4392 				 * (and lower priority)
4393 				 */
4394 				status = ice_prof_tcam_ena_dis(hw, blk, false,
4395 							       vsig,
4396 							       &t->tcam[i],
4397 							       chg);
4398 				if (status)
4399 					return status;
4400 			} else if (!test_bit(t->tcam[i].ptg, ptgs_used) &&
4401 				   !t->tcam[i].in_use) {
4402 				/* need to enable this PTG, as it in not in use
4403 				 * and not enabled (highest priority)
4404 				 */
4405 				status = ice_prof_tcam_ena_dis(hw, blk, true,
4406 							       vsig,
4407 							       &t->tcam[i],
4408 							       chg);
4409 				if (status)
4410 					return status;
4411 			}
4412 
4413 			/* keep track of used ptgs */
4414 			set_bit(t->tcam[i].ptg, ptgs_used);
4415 		}
4416 	}
4417 
4418 	return 0;
4419 }
4420 
4421 /**
4422  * ice_add_prof_id_vsig - add profile to VSIG
4423  * @hw: pointer to the HW struct
4424  * @blk: hardware block
4425  * @vsig: the VSIG to which this profile is to be added
4426  * @hdl: the profile handle indicating the profile to add
4427  * @rev: true to add entries to the end of the list
4428  * @chg: the change list
4429  */
4430 static enum ice_status
4431 ice_add_prof_id_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig, u64 hdl,
4432 		     bool rev, struct list_head *chg)
4433 {
4434 	/* Masks that ignore flags */
4435 	u8 vl_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
4436 	u8 dc_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0x00, 0x00, 0x00 };
4437 	u8 nm_msk[ICE_TCAM_KEY_VAL_SZ] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
4438 	struct ice_prof_map *map;
4439 	struct ice_vsig_prof *t;
4440 	struct ice_chs_chg *p;
4441 	u16 vsig_idx, i;
4442 
4443 	/* Get the details on the profile specified by the handle ID */
4444 	map = ice_search_prof_id(hw, blk, hdl);
4445 	if (!map)
4446 		return ICE_ERR_DOES_NOT_EXIST;
4447 
4448 	/* Error, if this VSIG already has this profile */
4449 	if (ice_has_prof_vsig(hw, blk, vsig, hdl))
4450 		return ICE_ERR_ALREADY_EXISTS;
4451 
4452 	/* new VSIG profile structure */
4453 	t = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*t), GFP_KERNEL);
4454 	if (!t)
4455 		return ICE_ERR_NO_MEMORY;
4456 
4457 	t->profile_cookie = map->profile_cookie;
4458 	t->prof_id = map->prof_id;
4459 	t->tcam_count = map->ptg_cnt;
4460 
4461 	/* create TCAM entries */
4462 	for (i = 0; i < map->ptg_cnt; i++) {
4463 		enum ice_status status;
4464 		u16 tcam_idx;
4465 
4466 		/* add TCAM to change list */
4467 		p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p), GFP_KERNEL);
4468 		if (!p)
4469 			goto err_ice_add_prof_id_vsig;
4470 
4471 		/* allocate the TCAM entry index */
4472 		status = ice_alloc_tcam_ent(hw, blk, &tcam_idx);
4473 		if (status) {
4474 			devm_kfree(ice_hw_to_dev(hw), p);
4475 			goto err_ice_add_prof_id_vsig;
4476 		}
4477 
4478 		t->tcam[i].ptg = map->ptg[i];
4479 		t->tcam[i].prof_id = map->prof_id;
4480 		t->tcam[i].tcam_idx = tcam_idx;
4481 		t->tcam[i].in_use = true;
4482 
4483 		p->type = ICE_TCAM_ADD;
4484 		p->add_tcam_idx = true;
4485 		p->prof_id = t->tcam[i].prof_id;
4486 		p->ptg = t->tcam[i].ptg;
4487 		p->vsig = vsig;
4488 		p->tcam_idx = t->tcam[i].tcam_idx;
4489 
4490 		/* write the TCAM entry */
4491 		status = ice_tcam_write_entry(hw, blk, t->tcam[i].tcam_idx,
4492 					      t->tcam[i].prof_id,
4493 					      t->tcam[i].ptg, vsig, 0, 0,
4494 					      vl_msk, dc_msk, nm_msk);
4495 		if (status) {
4496 			devm_kfree(ice_hw_to_dev(hw), p);
4497 			goto err_ice_add_prof_id_vsig;
4498 		}
4499 
4500 		/* log change */
4501 		list_add(&p->list_entry, chg);
4502 	}
4503 
4504 	/* add profile to VSIG */
4505 	vsig_idx = vsig & ICE_VSIG_IDX_M;
4506 	if (rev)
4507 		list_add_tail(&t->list,
4508 			      &hw->blk[blk].xlt2.vsig_tbl[vsig_idx].prop_lst);
4509 	else
4510 		list_add(&t->list,
4511 			 &hw->blk[blk].xlt2.vsig_tbl[vsig_idx].prop_lst);
4512 
4513 	return 0;
4514 
4515 err_ice_add_prof_id_vsig:
4516 	/* let caller clean up the change list */
4517 	devm_kfree(ice_hw_to_dev(hw), t);
4518 	return ICE_ERR_NO_MEMORY;
4519 }
4520 
4521 /**
4522  * ice_create_prof_id_vsig - add a new VSIG with a single profile
4523  * @hw: pointer to the HW struct
4524  * @blk: hardware block
4525  * @vsi: the initial VSI that will be in VSIG
4526  * @hdl: the profile handle of the profile that will be added to the VSIG
4527  * @chg: the change list
4528  */
4529 static enum ice_status
4530 ice_create_prof_id_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsi, u64 hdl,
4531 			struct list_head *chg)
4532 {
4533 	enum ice_status status;
4534 	struct ice_chs_chg *p;
4535 	u16 new_vsig;
4536 
4537 	p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p), GFP_KERNEL);
4538 	if (!p)
4539 		return ICE_ERR_NO_MEMORY;
4540 
4541 	new_vsig = ice_vsig_alloc(hw, blk);
4542 	if (!new_vsig) {
4543 		status = ICE_ERR_HW_TABLE;
4544 		goto err_ice_create_prof_id_vsig;
4545 	}
4546 
4547 	status = ice_move_vsi(hw, blk, vsi, new_vsig, chg);
4548 	if (status)
4549 		goto err_ice_create_prof_id_vsig;
4550 
4551 	status = ice_add_prof_id_vsig(hw, blk, new_vsig, hdl, false, chg);
4552 	if (status)
4553 		goto err_ice_create_prof_id_vsig;
4554 
4555 	p->type = ICE_VSIG_ADD;
4556 	p->vsi = vsi;
4557 	p->orig_vsig = ICE_DEFAULT_VSIG;
4558 	p->vsig = new_vsig;
4559 
4560 	list_add(&p->list_entry, chg);
4561 
4562 	return 0;
4563 
4564 err_ice_create_prof_id_vsig:
4565 	/* let caller clean up the change list */
4566 	devm_kfree(ice_hw_to_dev(hw), p);
4567 	return status;
4568 }
4569 
4570 /**
4571  * ice_create_vsig_from_lst - create a new VSIG with a list of profiles
4572  * @hw: pointer to the HW struct
4573  * @blk: hardware block
4574  * @vsi: the initial VSI that will be in VSIG
4575  * @lst: the list of profile that will be added to the VSIG
4576  * @new_vsig: return of new VSIG
4577  * @chg: the change list
4578  */
4579 static enum ice_status
4580 ice_create_vsig_from_lst(struct ice_hw *hw, enum ice_block blk, u16 vsi,
4581 			 struct list_head *lst, u16 *new_vsig,
4582 			 struct list_head *chg)
4583 {
4584 	struct ice_vsig_prof *t;
4585 	enum ice_status status;
4586 	u16 vsig;
4587 
4588 	vsig = ice_vsig_alloc(hw, blk);
4589 	if (!vsig)
4590 		return ICE_ERR_HW_TABLE;
4591 
4592 	status = ice_move_vsi(hw, blk, vsi, vsig, chg);
4593 	if (status)
4594 		return status;
4595 
4596 	list_for_each_entry(t, lst, list) {
4597 		/* Reverse the order here since we are copying the list */
4598 		status = ice_add_prof_id_vsig(hw, blk, vsig, t->profile_cookie,
4599 					      true, chg);
4600 		if (status)
4601 			return status;
4602 	}
4603 
4604 	*new_vsig = vsig;
4605 
4606 	return 0;
4607 }
4608 
4609 /**
4610  * ice_find_prof_vsig - find a VSIG with a specific profile handle
4611  * @hw: pointer to the HW struct
4612  * @blk: hardware block
4613  * @hdl: the profile handle of the profile to search for
4614  * @vsig: returns the VSIG with the matching profile
4615  */
4616 static bool
4617 ice_find_prof_vsig(struct ice_hw *hw, enum ice_block blk, u64 hdl, u16 *vsig)
4618 {
4619 	struct ice_vsig_prof *t;
4620 	enum ice_status status;
4621 	struct list_head lst;
4622 
4623 	INIT_LIST_HEAD(&lst);
4624 
4625 	t = kzalloc(sizeof(*t), GFP_KERNEL);
4626 	if (!t)
4627 		return false;
4628 
4629 	t->profile_cookie = hdl;
4630 	list_add(&t->list, &lst);
4631 
4632 	status = ice_find_dup_props_vsig(hw, blk, &lst, vsig);
4633 
4634 	list_del(&t->list);
4635 	kfree(t);
4636 
4637 	return !status;
4638 }
4639 
4640 /**
4641  * ice_add_prof_id_flow - add profile flow
4642  * @hw: pointer to the HW struct
4643  * @blk: hardware block
4644  * @vsi: the VSI to enable with the profile specified by ID
4645  * @hdl: profile handle
4646  *
4647  * Calling this function will update the hardware tables to enable the
4648  * profile indicated by the ID parameter for the VSIs specified in the VSI
4649  * array. Once successfully called, the flow will be enabled.
4650  */
4651 enum ice_status
4652 ice_add_prof_id_flow(struct ice_hw *hw, enum ice_block blk, u16 vsi, u64 hdl)
4653 {
4654 	struct ice_vsig_prof *tmp1, *del1;
4655 	struct ice_chs_chg *tmp, *del;
4656 	struct list_head union_lst;
4657 	enum ice_status status;
4658 	struct list_head chg;
4659 	u16 vsig;
4660 
4661 	INIT_LIST_HEAD(&union_lst);
4662 	INIT_LIST_HEAD(&chg);
4663 
4664 	/* Get profile */
4665 	status = ice_get_prof(hw, blk, hdl, &chg);
4666 	if (status)
4667 		return status;
4668 
4669 	/* determine if VSI is already part of a VSIG */
4670 	status = ice_vsig_find_vsi(hw, blk, vsi, &vsig);
4671 	if (!status && vsig) {
4672 		bool only_vsi;
4673 		u16 or_vsig;
4674 		u16 ref;
4675 
4676 		/* found in VSIG */
4677 		or_vsig = vsig;
4678 
4679 		/* make sure that there is no overlap/conflict between the new
4680 		 * characteristics and the existing ones; we don't support that
4681 		 * scenario
4682 		 */
4683 		if (ice_has_prof_vsig(hw, blk, vsig, hdl)) {
4684 			status = ICE_ERR_ALREADY_EXISTS;
4685 			goto err_ice_add_prof_id_flow;
4686 		}
4687 
4688 		/* last VSI in the VSIG? */
4689 		status = ice_vsig_get_ref(hw, blk, vsig, &ref);
4690 		if (status)
4691 			goto err_ice_add_prof_id_flow;
4692 		only_vsi = (ref == 1);
4693 
4694 		/* create a union of the current profiles and the one being
4695 		 * added
4696 		 */
4697 		status = ice_get_profs_vsig(hw, blk, vsig, &union_lst);
4698 		if (status)
4699 			goto err_ice_add_prof_id_flow;
4700 
4701 		status = ice_add_prof_to_lst(hw, blk, &union_lst, hdl);
4702 		if (status)
4703 			goto err_ice_add_prof_id_flow;
4704 
4705 		/* search for an existing VSIG with an exact charc match */
4706 		status = ice_find_dup_props_vsig(hw, blk, &union_lst, &vsig);
4707 		if (!status) {
4708 			/* move VSI to the VSIG that matches */
4709 			status = ice_move_vsi(hw, blk, vsi, vsig, &chg);
4710 			if (status)
4711 				goto err_ice_add_prof_id_flow;
4712 
4713 			/* VSI has been moved out of or_vsig. If the or_vsig had
4714 			 * only that VSI it is now empty and can be removed.
4715 			 */
4716 			if (only_vsi) {
4717 				status = ice_rem_vsig(hw, blk, or_vsig, &chg);
4718 				if (status)
4719 					goto err_ice_add_prof_id_flow;
4720 			}
4721 		} else if (only_vsi) {
4722 			/* If the original VSIG only contains one VSI, then it
4723 			 * will be the requesting VSI. In this case the VSI is
4724 			 * not sharing entries and we can simply add the new
4725 			 * profile to the VSIG.
4726 			 */
4727 			status = ice_add_prof_id_vsig(hw, blk, vsig, hdl, false,
4728 						      &chg);
4729 			if (status)
4730 				goto err_ice_add_prof_id_flow;
4731 
4732 			/* Adjust priorities */
4733 			status = ice_adj_prof_priorities(hw, blk, vsig, &chg);
4734 			if (status)
4735 				goto err_ice_add_prof_id_flow;
4736 		} else {
4737 			/* No match, so we need a new VSIG */
4738 			status = ice_create_vsig_from_lst(hw, blk, vsi,
4739 							  &union_lst, &vsig,
4740 							  &chg);
4741 			if (status)
4742 				goto err_ice_add_prof_id_flow;
4743 
4744 			/* Adjust priorities */
4745 			status = ice_adj_prof_priorities(hw, blk, vsig, &chg);
4746 			if (status)
4747 				goto err_ice_add_prof_id_flow;
4748 		}
4749 	} else {
4750 		/* need to find or add a VSIG */
4751 		/* search for an existing VSIG with an exact charc match */
4752 		if (ice_find_prof_vsig(hw, blk, hdl, &vsig)) {
4753 			/* found an exact match */
4754 			/* add or move VSI to the VSIG that matches */
4755 			status = ice_move_vsi(hw, blk, vsi, vsig, &chg);
4756 			if (status)
4757 				goto err_ice_add_prof_id_flow;
4758 		} else {
4759 			/* we did not find an exact match */
4760 			/* we need to add a VSIG */
4761 			status = ice_create_prof_id_vsig(hw, blk, vsi, hdl,
4762 							 &chg);
4763 			if (status)
4764 				goto err_ice_add_prof_id_flow;
4765 		}
4766 	}
4767 
4768 	/* update hardware */
4769 	if (!status)
4770 		status = ice_upd_prof_hw(hw, blk, &chg);
4771 
4772 err_ice_add_prof_id_flow:
4773 	list_for_each_entry_safe(del, tmp, &chg, list_entry) {
4774 		list_del(&del->list_entry);
4775 		devm_kfree(ice_hw_to_dev(hw), del);
4776 	}
4777 
4778 	list_for_each_entry_safe(del1, tmp1, &union_lst, list) {
4779 		list_del(&del1->list);
4780 		devm_kfree(ice_hw_to_dev(hw), del1);
4781 	}
4782 
4783 	return status;
4784 }
4785 
4786 /**
4787  * ice_rem_prof_from_list - remove a profile from list
4788  * @hw: pointer to the HW struct
4789  * @lst: list to remove the profile from
4790  * @hdl: the profile handle indicating the profile to remove
4791  */
4792 static enum ice_status
4793 ice_rem_prof_from_list(struct ice_hw *hw, struct list_head *lst, u64 hdl)
4794 {
4795 	struct ice_vsig_prof *ent, *tmp;
4796 
4797 	list_for_each_entry_safe(ent, tmp, lst, list)
4798 		if (ent->profile_cookie == hdl) {
4799 			list_del(&ent->list);
4800 			devm_kfree(ice_hw_to_dev(hw), ent);
4801 			return 0;
4802 		}
4803 
4804 	return ICE_ERR_DOES_NOT_EXIST;
4805 }
4806 
4807 /**
4808  * ice_rem_prof_id_flow - remove flow
4809  * @hw: pointer to the HW struct
4810  * @blk: hardware block
4811  * @vsi: the VSI from which to remove the profile specified by ID
4812  * @hdl: profile tracking handle
4813  *
4814  * Calling this function will update the hardware tables to remove the
4815  * profile indicated by the ID parameter for the VSIs specified in the VSI
4816  * array. Once successfully called, the flow will be disabled.
4817  */
4818 enum ice_status
4819 ice_rem_prof_id_flow(struct ice_hw *hw, enum ice_block blk, u16 vsi, u64 hdl)
4820 {
4821 	struct ice_vsig_prof *tmp1, *del1;
4822 	struct ice_chs_chg *tmp, *del;
4823 	struct list_head chg, copy;
4824 	enum ice_status status;
4825 	u16 vsig;
4826 
4827 	INIT_LIST_HEAD(&copy);
4828 	INIT_LIST_HEAD(&chg);
4829 
4830 	/* determine if VSI is already part of a VSIG */
4831 	status = ice_vsig_find_vsi(hw, blk, vsi, &vsig);
4832 	if (!status && vsig) {
4833 		bool last_profile;
4834 		bool only_vsi;
4835 		u16 ref;
4836 
4837 		/* found in VSIG */
4838 		last_profile = ice_vsig_prof_id_count(hw, blk, vsig) == 1;
4839 		status = ice_vsig_get_ref(hw, blk, vsig, &ref);
4840 		if (status)
4841 			goto err_ice_rem_prof_id_flow;
4842 		only_vsi = (ref == 1);
4843 
4844 		if (only_vsi) {
4845 			/* If the original VSIG only contains one reference,
4846 			 * which will be the requesting VSI, then the VSI is not
4847 			 * sharing entries and we can simply remove the specific
4848 			 * characteristics from the VSIG.
4849 			 */
4850 
4851 			if (last_profile) {
4852 				/* If there are no profiles left for this VSIG,
4853 				 * then simply remove the the VSIG.
4854 				 */
4855 				status = ice_rem_vsig(hw, blk, vsig, &chg);
4856 				if (status)
4857 					goto err_ice_rem_prof_id_flow;
4858 			} else {
4859 				status = ice_rem_prof_id_vsig(hw, blk, vsig,
4860 							      hdl, &chg);
4861 				if (status)
4862 					goto err_ice_rem_prof_id_flow;
4863 
4864 				/* Adjust priorities */
4865 				status = ice_adj_prof_priorities(hw, blk, vsig,
4866 								 &chg);
4867 				if (status)
4868 					goto err_ice_rem_prof_id_flow;
4869 			}
4870 
4871 		} else {
4872 			/* Make a copy of the VSIG's list of Profiles */
4873 			status = ice_get_profs_vsig(hw, blk, vsig, &copy);
4874 			if (status)
4875 				goto err_ice_rem_prof_id_flow;
4876 
4877 			/* Remove specified profile entry from the list */
4878 			status = ice_rem_prof_from_list(hw, &copy, hdl);
4879 			if (status)
4880 				goto err_ice_rem_prof_id_flow;
4881 
4882 			if (list_empty(&copy)) {
4883 				status = ice_move_vsi(hw, blk, vsi,
4884 						      ICE_DEFAULT_VSIG, &chg);
4885 				if (status)
4886 					goto err_ice_rem_prof_id_flow;
4887 
4888 			} else if (!ice_find_dup_props_vsig(hw, blk, &copy,
4889 							    &vsig)) {
4890 				/* found an exact match */
4891 				/* add or move VSI to the VSIG that matches */
4892 				/* Search for a VSIG with a matching profile
4893 				 * list
4894 				 */
4895 
4896 				/* Found match, move VSI to the matching VSIG */
4897 				status = ice_move_vsi(hw, blk, vsi, vsig, &chg);
4898 				if (status)
4899 					goto err_ice_rem_prof_id_flow;
4900 			} else {
4901 				/* since no existing VSIG supports this
4902 				 * characteristic pattern, we need to create a
4903 				 * new VSIG and TCAM entries
4904 				 */
4905 				status = ice_create_vsig_from_lst(hw, blk, vsi,
4906 								  &copy, &vsig,
4907 								  &chg);
4908 				if (status)
4909 					goto err_ice_rem_prof_id_flow;
4910 
4911 				/* Adjust priorities */
4912 				status = ice_adj_prof_priorities(hw, blk, vsig,
4913 								 &chg);
4914 				if (status)
4915 					goto err_ice_rem_prof_id_flow;
4916 			}
4917 		}
4918 	} else {
4919 		status = ICE_ERR_DOES_NOT_EXIST;
4920 	}
4921 
4922 	/* update hardware tables */
4923 	if (!status)
4924 		status = ice_upd_prof_hw(hw, blk, &chg);
4925 
4926 err_ice_rem_prof_id_flow:
4927 	list_for_each_entry_safe(del, tmp, &chg, list_entry) {
4928 		list_del(&del->list_entry);
4929 		devm_kfree(ice_hw_to_dev(hw), del);
4930 	}
4931 
4932 	list_for_each_entry_safe(del1, tmp1, &copy, list) {
4933 		list_del(&del1->list);
4934 		devm_kfree(ice_hw_to_dev(hw), del1);
4935 	}
4936 
4937 	return status;
4938 }
4939