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