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