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