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