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