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  * @lkups: list of protocol types
1875  * @bm: bitmap of field vectors to consider
1876  * @fv_list: Head of a list
1877  *
1878  * Finds all the field vector entries from switch block that contain
1879  * a given protocol ID and offset and returns a list of structures of type
1880  * "ice_sw_fv_list_entry". Every structure in the list has a field vector
1881  * definition and profile ID information
1882  * NOTE: The caller of the function is responsible for freeing the memory
1883  * allocated for every list entry.
1884  */
1885 int
1886 ice_get_sw_fv_list(struct ice_hw *hw, struct ice_prot_lkup_ext *lkups,
1887 		   unsigned long *bm, struct list_head *fv_list)
1888 {
1889 	struct ice_sw_fv_list_entry *fvl;
1890 	struct ice_sw_fv_list_entry *tmp;
1891 	struct ice_pkg_enum state;
1892 	struct ice_seg *ice_seg;
1893 	struct ice_fv *fv;
1894 	u32 offset;
1895 
1896 	memset(&state, 0, sizeof(state));
1897 
1898 	if (!lkups->n_val_words || !hw->seg)
1899 		return -EINVAL;
1900 
1901 	ice_seg = hw->seg;
1902 	do {
1903 		u16 i;
1904 
1905 		fv = ice_pkg_enum_entry(ice_seg, &state, ICE_SID_FLD_VEC_SW,
1906 					&offset, ice_sw_fv_handler);
1907 		if (!fv)
1908 			break;
1909 		ice_seg = NULL;
1910 
1911 		/* If field vector is not in the bitmap list, then skip this
1912 		 * profile.
1913 		 */
1914 		if (!test_bit((u16)offset, bm))
1915 			continue;
1916 
1917 		for (i = 0; i < lkups->n_val_words; i++) {
1918 			int j;
1919 
1920 			for (j = 0; j < hw->blk[ICE_BLK_SW].es.fvw; j++)
1921 				if (fv->ew[j].prot_id ==
1922 				    lkups->fv_words[i].prot_id &&
1923 				    fv->ew[j].off == lkups->fv_words[i].off)
1924 					break;
1925 			if (j >= hw->blk[ICE_BLK_SW].es.fvw)
1926 				break;
1927 			if (i + 1 == lkups->n_val_words) {
1928 				fvl = devm_kzalloc(ice_hw_to_dev(hw),
1929 						   sizeof(*fvl), GFP_KERNEL);
1930 				if (!fvl)
1931 					goto err;
1932 				fvl->fv_ptr = fv;
1933 				fvl->profile_id = offset;
1934 				list_add(&fvl->list_entry, fv_list);
1935 				break;
1936 			}
1937 		}
1938 	} while (fv);
1939 	if (list_empty(fv_list))
1940 		return -EIO;
1941 	return 0;
1942 
1943 err:
1944 	list_for_each_entry_safe(fvl, tmp, fv_list, list_entry) {
1945 		list_del(&fvl->list_entry);
1946 		devm_kfree(ice_hw_to_dev(hw), fvl);
1947 	}
1948 
1949 	return -ENOMEM;
1950 }
1951 
1952 /**
1953  * ice_init_prof_result_bm - Initialize the profile result index bitmap
1954  * @hw: pointer to hardware structure
1955  */
1956 void ice_init_prof_result_bm(struct ice_hw *hw)
1957 {
1958 	struct ice_pkg_enum state;
1959 	struct ice_seg *ice_seg;
1960 	struct ice_fv *fv;
1961 
1962 	memset(&state, 0, sizeof(state));
1963 
1964 	if (!hw->seg)
1965 		return;
1966 
1967 	ice_seg = hw->seg;
1968 	do {
1969 		u32 off;
1970 		u16 i;
1971 
1972 		fv = ice_pkg_enum_entry(ice_seg, &state, ICE_SID_FLD_VEC_SW,
1973 					&off, ice_sw_fv_handler);
1974 		ice_seg = NULL;
1975 		if (!fv)
1976 			break;
1977 
1978 		bitmap_zero(hw->switch_info->prof_res_bm[off],
1979 			    ICE_MAX_FV_WORDS);
1980 
1981 		/* Determine empty field vector indices, these can be
1982 		 * used for recipe results. Skip index 0, since it is
1983 		 * always used for Switch ID.
1984 		 */
1985 		for (i = 1; i < ICE_MAX_FV_WORDS; i++)
1986 			if (fv->ew[i].prot_id == ICE_PROT_INVALID &&
1987 			    fv->ew[i].off == ICE_FV_OFFSET_INVAL)
1988 				set_bit(i, hw->switch_info->prof_res_bm[off]);
1989 	} while (fv);
1990 }
1991 
1992 /**
1993  * ice_pkg_buf_free
1994  * @hw: pointer to the HW structure
1995  * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
1996  *
1997  * Frees a package buffer
1998  */
1999 void ice_pkg_buf_free(struct ice_hw *hw, struct ice_buf_build *bld)
2000 {
2001 	devm_kfree(ice_hw_to_dev(hw), bld);
2002 }
2003 
2004 /**
2005  * ice_pkg_buf_reserve_section
2006  * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
2007  * @count: the number of sections to reserve
2008  *
2009  * Reserves one or more section table entries in a package buffer. This routine
2010  * can be called multiple times as long as they are made before calling
2011  * ice_pkg_buf_alloc_section(). Once ice_pkg_buf_alloc_section()
2012  * is called once, the number of sections that can be allocated will not be able
2013  * to be increased; not using all reserved sections is fine, but this will
2014  * result in some wasted space in the buffer.
2015  * Note: all package contents must be in Little Endian form.
2016  */
2017 static int
2018 ice_pkg_buf_reserve_section(struct ice_buf_build *bld, u16 count)
2019 {
2020 	struct ice_buf_hdr *buf;
2021 	u16 section_count;
2022 	u16 data_end;
2023 
2024 	if (!bld)
2025 		return -EINVAL;
2026 
2027 	buf = (struct ice_buf_hdr *)&bld->buf;
2028 
2029 	/* already an active section, can't increase table size */
2030 	section_count = le16_to_cpu(buf->section_count);
2031 	if (section_count > 0)
2032 		return -EIO;
2033 
2034 	if (bld->reserved_section_table_entries + count > ICE_MAX_S_COUNT)
2035 		return -EIO;
2036 	bld->reserved_section_table_entries += count;
2037 
2038 	data_end = le16_to_cpu(buf->data_end) +
2039 		flex_array_size(buf, section_entry, count);
2040 	buf->data_end = cpu_to_le16(data_end);
2041 
2042 	return 0;
2043 }
2044 
2045 /**
2046  * ice_pkg_buf_alloc_section
2047  * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
2048  * @type: the section type value
2049  * @size: the size of the section to reserve (in bytes)
2050  *
2051  * Reserves memory in the buffer for a section's content and updates the
2052  * buffers' status accordingly. This routine returns a pointer to the first
2053  * byte of the section start within the buffer, which is used to fill in the
2054  * section contents.
2055  * Note: all package contents must be in Little Endian form.
2056  */
2057 static void *
2058 ice_pkg_buf_alloc_section(struct ice_buf_build *bld, u32 type, u16 size)
2059 {
2060 	struct ice_buf_hdr *buf;
2061 	u16 sect_count;
2062 	u16 data_end;
2063 
2064 	if (!bld || !type || !size)
2065 		return NULL;
2066 
2067 	buf = (struct ice_buf_hdr *)&bld->buf;
2068 
2069 	/* check for enough space left in buffer */
2070 	data_end = le16_to_cpu(buf->data_end);
2071 
2072 	/* section start must align on 4 byte boundary */
2073 	data_end = ALIGN(data_end, 4);
2074 
2075 	if ((data_end + size) > ICE_MAX_S_DATA_END)
2076 		return NULL;
2077 
2078 	/* check for more available section table entries */
2079 	sect_count = le16_to_cpu(buf->section_count);
2080 	if (sect_count < bld->reserved_section_table_entries) {
2081 		void *section_ptr = ((u8 *)buf) + data_end;
2082 
2083 		buf->section_entry[sect_count].offset = cpu_to_le16(data_end);
2084 		buf->section_entry[sect_count].size = cpu_to_le16(size);
2085 		buf->section_entry[sect_count].type = cpu_to_le32(type);
2086 
2087 		data_end += size;
2088 		buf->data_end = cpu_to_le16(data_end);
2089 
2090 		buf->section_count = cpu_to_le16(sect_count + 1);
2091 		return section_ptr;
2092 	}
2093 
2094 	/* no free section table entries */
2095 	return NULL;
2096 }
2097 
2098 /**
2099  * ice_pkg_buf_alloc_single_section
2100  * @hw: pointer to the HW structure
2101  * @type: the section type value
2102  * @size: the size of the section to reserve (in bytes)
2103  * @section: returns pointer to the section
2104  *
2105  * Allocates a package buffer with a single section.
2106  * Note: all package contents must be in Little Endian form.
2107  */
2108 struct ice_buf_build *
2109 ice_pkg_buf_alloc_single_section(struct ice_hw *hw, u32 type, u16 size,
2110 				 void **section)
2111 {
2112 	struct ice_buf_build *buf;
2113 
2114 	if (!section)
2115 		return NULL;
2116 
2117 	buf = ice_pkg_buf_alloc(hw);
2118 	if (!buf)
2119 		return NULL;
2120 
2121 	if (ice_pkg_buf_reserve_section(buf, 1))
2122 		goto ice_pkg_buf_alloc_single_section_err;
2123 
2124 	*section = ice_pkg_buf_alloc_section(buf, type, size);
2125 	if (!*section)
2126 		goto ice_pkg_buf_alloc_single_section_err;
2127 
2128 	return buf;
2129 
2130 ice_pkg_buf_alloc_single_section_err:
2131 	ice_pkg_buf_free(hw, buf);
2132 	return NULL;
2133 }
2134 
2135 /**
2136  * ice_pkg_buf_get_active_sections
2137  * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
2138  *
2139  * Returns the number of active sections. Before using the package buffer
2140  * in an update package command, the caller should make sure that there is at
2141  * least one active section - otherwise, the buffer is not legal and should
2142  * not be used.
2143  * Note: all package contents must be in Little Endian form.
2144  */
2145 static u16 ice_pkg_buf_get_active_sections(struct ice_buf_build *bld)
2146 {
2147 	struct ice_buf_hdr *buf;
2148 
2149 	if (!bld)
2150 		return 0;
2151 
2152 	buf = (struct ice_buf_hdr *)&bld->buf;
2153 	return le16_to_cpu(buf->section_count);
2154 }
2155 
2156 /**
2157  * ice_pkg_buf
2158  * @bld: pointer to pkg build (allocated by ice_pkg_buf_alloc())
2159  *
2160  * Return a pointer to the buffer's header
2161  */
2162 struct ice_buf *ice_pkg_buf(struct ice_buf_build *bld)
2163 {
2164 	if (!bld)
2165 		return NULL;
2166 
2167 	return &bld->buf;
2168 }
2169 
2170 /**
2171  * ice_get_open_tunnel_port - retrieve an open tunnel port
2172  * @hw: pointer to the HW structure
2173  * @port: returns open port
2174  * @type: type of tunnel, can be TNL_LAST if it doesn't matter
2175  */
2176 bool
2177 ice_get_open_tunnel_port(struct ice_hw *hw, u16 *port,
2178 			 enum ice_tunnel_type type)
2179 {
2180 	bool res = false;
2181 	u16 i;
2182 
2183 	mutex_lock(&hw->tnl_lock);
2184 
2185 	for (i = 0; i < hw->tnl.count && i < ICE_TUNNEL_MAX_ENTRIES; i++)
2186 		if (hw->tnl.tbl[i].valid && hw->tnl.tbl[i].port &&
2187 		    (type == TNL_LAST || type == hw->tnl.tbl[i].type)) {
2188 			*port = hw->tnl.tbl[i].port;
2189 			res = true;
2190 			break;
2191 		}
2192 
2193 	mutex_unlock(&hw->tnl_lock);
2194 
2195 	return res;
2196 }
2197 
2198 /**
2199  * ice_upd_dvm_boost_entry
2200  * @hw: pointer to the HW structure
2201  * @entry: pointer to double vlan boost entry info
2202  */
2203 static int
2204 ice_upd_dvm_boost_entry(struct ice_hw *hw, struct ice_dvm_entry *entry)
2205 {
2206 	struct ice_boost_tcam_section *sect_rx, *sect_tx;
2207 	int status = -ENOSPC;
2208 	struct ice_buf_build *bld;
2209 	u8 val, dc, nm;
2210 
2211 	bld = ice_pkg_buf_alloc(hw);
2212 	if (!bld)
2213 		return -ENOMEM;
2214 
2215 	/* allocate 2 sections, one for Rx parser, one for Tx parser */
2216 	if (ice_pkg_buf_reserve_section(bld, 2))
2217 		goto ice_upd_dvm_boost_entry_err;
2218 
2219 	sect_rx = ice_pkg_buf_alloc_section(bld, ICE_SID_RXPARSER_BOOST_TCAM,
2220 					    struct_size(sect_rx, tcam, 1));
2221 	if (!sect_rx)
2222 		goto ice_upd_dvm_boost_entry_err;
2223 	sect_rx->count = cpu_to_le16(1);
2224 
2225 	sect_tx = ice_pkg_buf_alloc_section(bld, ICE_SID_TXPARSER_BOOST_TCAM,
2226 					    struct_size(sect_tx, tcam, 1));
2227 	if (!sect_tx)
2228 		goto ice_upd_dvm_boost_entry_err;
2229 	sect_tx->count = cpu_to_le16(1);
2230 
2231 	/* copy original boost entry to update package buffer */
2232 	memcpy(sect_rx->tcam, entry->boost_entry, sizeof(*sect_rx->tcam));
2233 
2234 	/* re-write the don't care and never match bits accordingly */
2235 	if (entry->enable) {
2236 		/* all bits are don't care */
2237 		val = 0x00;
2238 		dc = 0xFF;
2239 		nm = 0x00;
2240 	} else {
2241 		/* disable, one never match bit, the rest are don't care */
2242 		val = 0x00;
2243 		dc = 0xF7;
2244 		nm = 0x08;
2245 	}
2246 
2247 	ice_set_key((u8 *)&sect_rx->tcam[0].key, sizeof(sect_rx->tcam[0].key),
2248 		    &val, NULL, &dc, &nm, 0, sizeof(u8));
2249 
2250 	/* exact copy of entry to Tx section entry */
2251 	memcpy(sect_tx->tcam, sect_rx->tcam, sizeof(*sect_tx->tcam));
2252 
2253 	status = ice_update_pkg_no_lock(hw, ice_pkg_buf(bld), 1);
2254 
2255 ice_upd_dvm_boost_entry_err:
2256 	ice_pkg_buf_free(hw, bld);
2257 
2258 	return status;
2259 }
2260 
2261 /**
2262  * ice_set_dvm_boost_entries
2263  * @hw: pointer to the HW structure
2264  *
2265  * Enable double vlan by updating the appropriate boost tcam entries.
2266  */
2267 int ice_set_dvm_boost_entries(struct ice_hw *hw)
2268 {
2269 	int status;
2270 	u16 i;
2271 
2272 	for (i = 0; i < hw->dvm_upd.count; i++) {
2273 		status = ice_upd_dvm_boost_entry(hw, &hw->dvm_upd.tbl[i]);
2274 		if (status)
2275 			return status;
2276 	}
2277 
2278 	return 0;
2279 }
2280 
2281 /**
2282  * ice_tunnel_idx_to_entry - convert linear index to the sparse one
2283  * @hw: pointer to the HW structure
2284  * @type: type of tunnel
2285  * @idx: linear index
2286  *
2287  * Stack assumes we have 2 linear tables with indexes [0, count_valid),
2288  * but really the port table may be sprase, and types are mixed, so convert
2289  * the stack index into the device index.
2290  */
2291 static u16 ice_tunnel_idx_to_entry(struct ice_hw *hw, enum ice_tunnel_type type,
2292 				   u16 idx)
2293 {
2294 	u16 i;
2295 
2296 	for (i = 0; i < hw->tnl.count && i < ICE_TUNNEL_MAX_ENTRIES; i++)
2297 		if (hw->tnl.tbl[i].valid &&
2298 		    hw->tnl.tbl[i].type == type &&
2299 		    idx-- == 0)
2300 			return i;
2301 
2302 	WARN_ON_ONCE(1);
2303 	return 0;
2304 }
2305 
2306 /**
2307  * ice_create_tunnel
2308  * @hw: pointer to the HW structure
2309  * @index: device table entry
2310  * @type: type of tunnel
2311  * @port: port of tunnel to create
2312  *
2313  * Create a tunnel by updating the parse graph in the parser. We do that by
2314  * creating a package buffer with the tunnel info and issuing an update package
2315  * command.
2316  */
2317 static int
2318 ice_create_tunnel(struct ice_hw *hw, u16 index,
2319 		  enum ice_tunnel_type type, u16 port)
2320 {
2321 	struct ice_boost_tcam_section *sect_rx, *sect_tx;
2322 	struct ice_buf_build *bld;
2323 	int status = -ENOSPC;
2324 
2325 	mutex_lock(&hw->tnl_lock);
2326 
2327 	bld = ice_pkg_buf_alloc(hw);
2328 	if (!bld) {
2329 		status = -ENOMEM;
2330 		goto ice_create_tunnel_end;
2331 	}
2332 
2333 	/* allocate 2 sections, one for Rx parser, one for Tx parser */
2334 	if (ice_pkg_buf_reserve_section(bld, 2))
2335 		goto ice_create_tunnel_err;
2336 
2337 	sect_rx = ice_pkg_buf_alloc_section(bld, ICE_SID_RXPARSER_BOOST_TCAM,
2338 					    struct_size(sect_rx, tcam, 1));
2339 	if (!sect_rx)
2340 		goto ice_create_tunnel_err;
2341 	sect_rx->count = cpu_to_le16(1);
2342 
2343 	sect_tx = ice_pkg_buf_alloc_section(bld, ICE_SID_TXPARSER_BOOST_TCAM,
2344 					    struct_size(sect_tx, tcam, 1));
2345 	if (!sect_tx)
2346 		goto ice_create_tunnel_err;
2347 	sect_tx->count = cpu_to_le16(1);
2348 
2349 	/* copy original boost entry to update package buffer */
2350 	memcpy(sect_rx->tcam, hw->tnl.tbl[index].boost_entry,
2351 	       sizeof(*sect_rx->tcam));
2352 
2353 	/* over-write the never-match dest port key bits with the encoded port
2354 	 * bits
2355 	 */
2356 	ice_set_key((u8 *)&sect_rx->tcam[0].key, sizeof(sect_rx->tcam[0].key),
2357 		    (u8 *)&port, NULL, NULL, NULL,
2358 		    (u16)offsetof(struct ice_boost_key_value, hv_dst_port_key),
2359 		    sizeof(sect_rx->tcam[0].key.key.hv_dst_port_key));
2360 
2361 	/* exact copy of entry to Tx section entry */
2362 	memcpy(sect_tx->tcam, sect_rx->tcam, sizeof(*sect_tx->tcam));
2363 
2364 	status = ice_update_pkg(hw, ice_pkg_buf(bld), 1);
2365 	if (!status)
2366 		hw->tnl.tbl[index].port = port;
2367 
2368 ice_create_tunnel_err:
2369 	ice_pkg_buf_free(hw, bld);
2370 
2371 ice_create_tunnel_end:
2372 	mutex_unlock(&hw->tnl_lock);
2373 
2374 	return status;
2375 }
2376 
2377 /**
2378  * ice_destroy_tunnel
2379  * @hw: pointer to the HW structure
2380  * @index: device table entry
2381  * @type: type of tunnel
2382  * @port: port of tunnel to destroy (ignored if the all parameter is true)
2383  *
2384  * Destroys a tunnel or all tunnels by creating an update package buffer
2385  * targeting the specific updates requested and then performing an update
2386  * package.
2387  */
2388 static int
2389 ice_destroy_tunnel(struct ice_hw *hw, u16 index, enum ice_tunnel_type type,
2390 		   u16 port)
2391 {
2392 	struct ice_boost_tcam_section *sect_rx, *sect_tx;
2393 	struct ice_buf_build *bld;
2394 	int status = -ENOSPC;
2395 
2396 	mutex_lock(&hw->tnl_lock);
2397 
2398 	if (WARN_ON(!hw->tnl.tbl[index].valid ||
2399 		    hw->tnl.tbl[index].type != type ||
2400 		    hw->tnl.tbl[index].port != port)) {
2401 		status = -EIO;
2402 		goto ice_destroy_tunnel_end;
2403 	}
2404 
2405 	bld = ice_pkg_buf_alloc(hw);
2406 	if (!bld) {
2407 		status = -ENOMEM;
2408 		goto ice_destroy_tunnel_end;
2409 	}
2410 
2411 	/* allocate 2 sections, one for Rx parser, one for Tx parser */
2412 	if (ice_pkg_buf_reserve_section(bld, 2))
2413 		goto ice_destroy_tunnel_err;
2414 
2415 	sect_rx = ice_pkg_buf_alloc_section(bld, ICE_SID_RXPARSER_BOOST_TCAM,
2416 					    struct_size(sect_rx, tcam, 1));
2417 	if (!sect_rx)
2418 		goto ice_destroy_tunnel_err;
2419 	sect_rx->count = cpu_to_le16(1);
2420 
2421 	sect_tx = ice_pkg_buf_alloc_section(bld, ICE_SID_TXPARSER_BOOST_TCAM,
2422 					    struct_size(sect_tx, tcam, 1));
2423 	if (!sect_tx)
2424 		goto ice_destroy_tunnel_err;
2425 	sect_tx->count = cpu_to_le16(1);
2426 
2427 	/* copy original boost entry to update package buffer, one copy to Rx
2428 	 * section, another copy to the Tx section
2429 	 */
2430 	memcpy(sect_rx->tcam, hw->tnl.tbl[index].boost_entry,
2431 	       sizeof(*sect_rx->tcam));
2432 	memcpy(sect_tx->tcam, hw->tnl.tbl[index].boost_entry,
2433 	       sizeof(*sect_tx->tcam));
2434 
2435 	status = ice_update_pkg(hw, ice_pkg_buf(bld), 1);
2436 	if (!status)
2437 		hw->tnl.tbl[index].port = 0;
2438 
2439 ice_destroy_tunnel_err:
2440 	ice_pkg_buf_free(hw, bld);
2441 
2442 ice_destroy_tunnel_end:
2443 	mutex_unlock(&hw->tnl_lock);
2444 
2445 	return status;
2446 }
2447 
2448 int ice_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
2449 			    unsigned int idx, struct udp_tunnel_info *ti)
2450 {
2451 	struct ice_netdev_priv *np = netdev_priv(netdev);
2452 	struct ice_vsi *vsi = np->vsi;
2453 	struct ice_pf *pf = vsi->back;
2454 	enum ice_tunnel_type tnl_type;
2455 	int status;
2456 	u16 index;
2457 
2458 	tnl_type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? TNL_VXLAN : TNL_GENEVE;
2459 	index = ice_tunnel_idx_to_entry(&pf->hw, tnl_type, idx);
2460 
2461 	status = ice_create_tunnel(&pf->hw, index, tnl_type, ntohs(ti->port));
2462 	if (status) {
2463 		netdev_err(netdev, "Error adding UDP tunnel - %d\n",
2464 			   status);
2465 		return -EIO;
2466 	}
2467 
2468 	udp_tunnel_nic_set_port_priv(netdev, table, idx, index);
2469 	return 0;
2470 }
2471 
2472 int ice_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
2473 			      unsigned int idx, struct udp_tunnel_info *ti)
2474 {
2475 	struct ice_netdev_priv *np = netdev_priv(netdev);
2476 	struct ice_vsi *vsi = np->vsi;
2477 	struct ice_pf *pf = vsi->back;
2478 	enum ice_tunnel_type tnl_type;
2479 	int status;
2480 
2481 	tnl_type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? TNL_VXLAN : TNL_GENEVE;
2482 
2483 	status = ice_destroy_tunnel(&pf->hw, ti->hw_priv, tnl_type,
2484 				    ntohs(ti->port));
2485 	if (status) {
2486 		netdev_err(netdev, "Error removing UDP tunnel - %d\n",
2487 			   status);
2488 		return -EIO;
2489 	}
2490 
2491 	return 0;
2492 }
2493 
2494 /**
2495  * ice_find_prot_off - find prot ID and offset pair, based on prof and FV index
2496  * @hw: pointer to the hardware structure
2497  * @blk: hardware block
2498  * @prof: profile ID
2499  * @fv_idx: field vector word index
2500  * @prot: variable to receive the protocol ID
2501  * @off: variable to receive the protocol offset
2502  */
2503 int
2504 ice_find_prot_off(struct ice_hw *hw, enum ice_block blk, u8 prof, u16 fv_idx,
2505 		  u8 *prot, u16 *off)
2506 {
2507 	struct ice_fv_word *fv_ext;
2508 
2509 	if (prof >= hw->blk[blk].es.count)
2510 		return -EINVAL;
2511 
2512 	if (fv_idx >= hw->blk[blk].es.fvw)
2513 		return -EINVAL;
2514 
2515 	fv_ext = hw->blk[blk].es.t + (prof * hw->blk[blk].es.fvw);
2516 
2517 	*prot = fv_ext[fv_idx].prot_id;
2518 	*off = fv_ext[fv_idx].off;
2519 
2520 	return 0;
2521 }
2522 
2523 /* PTG Management */
2524 
2525 /**
2526  * ice_ptg_find_ptype - Search for packet type group using packet type (ptype)
2527  * @hw: pointer to the hardware structure
2528  * @blk: HW block
2529  * @ptype: the ptype to search for
2530  * @ptg: pointer to variable that receives the PTG
2531  *
2532  * This function will search the PTGs for a particular ptype, returning the
2533  * PTG ID that contains it through the PTG parameter, with the value of
2534  * ICE_DEFAULT_PTG (0) meaning it is part the default PTG.
2535  */
2536 static int
2537 ice_ptg_find_ptype(struct ice_hw *hw, enum ice_block blk, u16 ptype, u8 *ptg)
2538 {
2539 	if (ptype >= ICE_XLT1_CNT || !ptg)
2540 		return -EINVAL;
2541 
2542 	*ptg = hw->blk[blk].xlt1.ptypes[ptype].ptg;
2543 	return 0;
2544 }
2545 
2546 /**
2547  * ice_ptg_alloc_val - Allocates a new packet type group ID by value
2548  * @hw: pointer to the hardware structure
2549  * @blk: HW block
2550  * @ptg: the PTG to allocate
2551  *
2552  * This function allocates a given packet type group ID specified by the PTG
2553  * parameter.
2554  */
2555 static void ice_ptg_alloc_val(struct ice_hw *hw, enum ice_block blk, u8 ptg)
2556 {
2557 	hw->blk[blk].xlt1.ptg_tbl[ptg].in_use = true;
2558 }
2559 
2560 /**
2561  * ice_ptg_remove_ptype - Removes ptype from a particular packet type group
2562  * @hw: pointer to the hardware structure
2563  * @blk: HW block
2564  * @ptype: the ptype to remove
2565  * @ptg: the PTG to remove the ptype from
2566  *
2567  * This function will remove the ptype from the specific PTG, and move it to
2568  * the default PTG (ICE_DEFAULT_PTG).
2569  */
2570 static int
2571 ice_ptg_remove_ptype(struct ice_hw *hw, enum ice_block blk, u16 ptype, u8 ptg)
2572 {
2573 	struct ice_ptg_ptype **ch;
2574 	struct ice_ptg_ptype *p;
2575 
2576 	if (ptype > ICE_XLT1_CNT - 1)
2577 		return -EINVAL;
2578 
2579 	if (!hw->blk[blk].xlt1.ptg_tbl[ptg].in_use)
2580 		return -ENOENT;
2581 
2582 	/* Should not happen if .in_use is set, bad config */
2583 	if (!hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype)
2584 		return -EIO;
2585 
2586 	/* find the ptype within this PTG, and bypass the link over it */
2587 	p = hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype;
2588 	ch = &hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype;
2589 	while (p) {
2590 		if (ptype == (p - hw->blk[blk].xlt1.ptypes)) {
2591 			*ch = p->next_ptype;
2592 			break;
2593 		}
2594 
2595 		ch = &p->next_ptype;
2596 		p = p->next_ptype;
2597 	}
2598 
2599 	hw->blk[blk].xlt1.ptypes[ptype].ptg = ICE_DEFAULT_PTG;
2600 	hw->blk[blk].xlt1.ptypes[ptype].next_ptype = NULL;
2601 
2602 	return 0;
2603 }
2604 
2605 /**
2606  * ice_ptg_add_mv_ptype - Adds/moves ptype to a particular packet type group
2607  * @hw: pointer to the hardware structure
2608  * @blk: HW block
2609  * @ptype: the ptype to add or move
2610  * @ptg: the PTG to add or move the ptype to
2611  *
2612  * This function will either add or move a ptype to a particular PTG depending
2613  * on if the ptype is already part of another group. Note that using a
2614  * a destination PTG ID of ICE_DEFAULT_PTG (0) will move the ptype to the
2615  * default PTG.
2616  */
2617 static int
2618 ice_ptg_add_mv_ptype(struct ice_hw *hw, enum ice_block blk, u16 ptype, u8 ptg)
2619 {
2620 	u8 original_ptg;
2621 	int status;
2622 
2623 	if (ptype > ICE_XLT1_CNT - 1)
2624 		return -EINVAL;
2625 
2626 	if (!hw->blk[blk].xlt1.ptg_tbl[ptg].in_use && ptg != ICE_DEFAULT_PTG)
2627 		return -ENOENT;
2628 
2629 	status = ice_ptg_find_ptype(hw, blk, ptype, &original_ptg);
2630 	if (status)
2631 		return status;
2632 
2633 	/* Is ptype already in the correct PTG? */
2634 	if (original_ptg == ptg)
2635 		return 0;
2636 
2637 	/* Remove from original PTG and move back to the default PTG */
2638 	if (original_ptg != ICE_DEFAULT_PTG)
2639 		ice_ptg_remove_ptype(hw, blk, ptype, original_ptg);
2640 
2641 	/* Moving to default PTG? Then we're done with this request */
2642 	if (ptg == ICE_DEFAULT_PTG)
2643 		return 0;
2644 
2645 	/* Add ptype to PTG at beginning of list */
2646 	hw->blk[blk].xlt1.ptypes[ptype].next_ptype =
2647 		hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype;
2648 	hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype =
2649 		&hw->blk[blk].xlt1.ptypes[ptype];
2650 
2651 	hw->blk[blk].xlt1.ptypes[ptype].ptg = ptg;
2652 	hw->blk[blk].xlt1.t[ptype] = ptg;
2653 
2654 	return 0;
2655 }
2656 
2657 /* Block / table size info */
2658 struct ice_blk_size_details {
2659 	u16 xlt1;			/* # XLT1 entries */
2660 	u16 xlt2;			/* # XLT2 entries */
2661 	u16 prof_tcam;			/* # profile ID TCAM entries */
2662 	u16 prof_id;			/* # profile IDs */
2663 	u8 prof_cdid_bits;		/* # CDID one-hot bits used in key */
2664 	u16 prof_redir;			/* # profile redirection entries */
2665 	u16 es;				/* # extraction sequence entries */
2666 	u16 fvw;			/* # field vector words */
2667 	u8 overwrite;			/* overwrite existing entries allowed */
2668 	u8 reverse;			/* reverse FV order */
2669 };
2670 
2671 static const struct ice_blk_size_details blk_sizes[ICE_BLK_COUNT] = {
2672 	/**
2673 	 * Table Definitions
2674 	 * XLT1 - Number of entries in XLT1 table
2675 	 * XLT2 - Number of entries in XLT2 table
2676 	 * TCAM - Number of entries Profile ID TCAM table
2677 	 * CDID - Control Domain ID of the hardware block
2678 	 * PRED - Number of entries in the Profile Redirection Table
2679 	 * FV   - Number of entries in the Field Vector
2680 	 * FVW  - Width (in WORDs) of the Field Vector
2681 	 * OVR  - Overwrite existing table entries
2682 	 * REV  - Reverse FV
2683 	 */
2684 	/*          XLT1        , XLT2        ,TCAM, PID,CDID,PRED,   FV, FVW */
2685 	/*          Overwrite   , Reverse FV */
2686 	/* SW  */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 256,   0,  256, 256,  48,
2687 		    false, false },
2688 	/* ACL */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 128,   0,  128, 128,  32,
2689 		    false, false },
2690 	/* FD  */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 128,   0,  128, 128,  24,
2691 		    false, true  },
2692 	/* RSS */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 128,   0,  128, 128,  24,
2693 		    true,  true  },
2694 	/* PE  */ { ICE_XLT1_CNT, ICE_XLT2_CNT,  64,  32,   0,   32,  32,  24,
2695 		    false, false },
2696 };
2697 
2698 enum ice_sid_all {
2699 	ICE_SID_XLT1_OFF = 0,
2700 	ICE_SID_XLT2_OFF,
2701 	ICE_SID_PR_OFF,
2702 	ICE_SID_PR_REDIR_OFF,
2703 	ICE_SID_ES_OFF,
2704 	ICE_SID_OFF_COUNT,
2705 };
2706 
2707 /* Characteristic handling */
2708 
2709 /**
2710  * ice_match_prop_lst - determine if properties of two lists match
2711  * @list1: first properties list
2712  * @list2: second properties list
2713  *
2714  * Count, cookies and the order must match in order to be considered equivalent.
2715  */
2716 static bool
2717 ice_match_prop_lst(struct list_head *list1, struct list_head *list2)
2718 {
2719 	struct ice_vsig_prof *tmp1;
2720 	struct ice_vsig_prof *tmp2;
2721 	u16 chk_count = 0;
2722 	u16 count = 0;
2723 
2724 	/* compare counts */
2725 	list_for_each_entry(tmp1, list1, list)
2726 		count++;
2727 	list_for_each_entry(tmp2, list2, list)
2728 		chk_count++;
2729 	/* cppcheck-suppress knownConditionTrueFalse */
2730 	if (!count || count != chk_count)
2731 		return false;
2732 
2733 	tmp1 = list_first_entry(list1, struct ice_vsig_prof, list);
2734 	tmp2 = list_first_entry(list2, struct ice_vsig_prof, list);
2735 
2736 	/* profile cookies must compare, and in the exact same order to take
2737 	 * into account priority
2738 	 */
2739 	while (count--) {
2740 		if (tmp2->profile_cookie != tmp1->profile_cookie)
2741 			return false;
2742 
2743 		tmp1 = list_next_entry(tmp1, list);
2744 		tmp2 = list_next_entry(tmp2, list);
2745 	}
2746 
2747 	return true;
2748 }
2749 
2750 /* VSIG Management */
2751 
2752 /**
2753  * ice_vsig_find_vsi - find a VSIG that contains a specified VSI
2754  * @hw: pointer to the hardware structure
2755  * @blk: HW block
2756  * @vsi: VSI of interest
2757  * @vsig: pointer to receive the VSI group
2758  *
2759  * This function will lookup the VSI entry in the XLT2 list and return
2760  * the VSI group its associated with.
2761  */
2762 static int
2763 ice_vsig_find_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 *vsig)
2764 {
2765 	if (!vsig || vsi >= ICE_MAX_VSI)
2766 		return -EINVAL;
2767 
2768 	/* As long as there's a default or valid VSIG associated with the input
2769 	 * VSI, the functions returns a success. Any handling of VSIG will be
2770 	 * done by the following add, update or remove functions.
2771 	 */
2772 	*vsig = hw->blk[blk].xlt2.vsis[vsi].vsig;
2773 
2774 	return 0;
2775 }
2776 
2777 /**
2778  * ice_vsig_alloc_val - allocate a new VSIG by value
2779  * @hw: pointer to the hardware structure
2780  * @blk: HW block
2781  * @vsig: the VSIG to allocate
2782  *
2783  * This function will allocate a given VSIG specified by the VSIG parameter.
2784  */
2785 static u16 ice_vsig_alloc_val(struct ice_hw *hw, enum ice_block blk, u16 vsig)
2786 {
2787 	u16 idx = vsig & ICE_VSIG_IDX_M;
2788 
2789 	if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use) {
2790 		INIT_LIST_HEAD(&hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst);
2791 		hw->blk[blk].xlt2.vsig_tbl[idx].in_use = true;
2792 	}
2793 
2794 	return ICE_VSIG_VALUE(idx, hw->pf_id);
2795 }
2796 
2797 /**
2798  * ice_vsig_alloc - Finds a free entry and allocates a new VSIG
2799  * @hw: pointer to the hardware structure
2800  * @blk: HW block
2801  *
2802  * This function will iterate through the VSIG list and mark the first
2803  * unused entry for the new VSIG entry as used and return that value.
2804  */
2805 static u16 ice_vsig_alloc(struct ice_hw *hw, enum ice_block blk)
2806 {
2807 	u16 i;
2808 
2809 	for (i = 1; i < ICE_MAX_VSIGS; i++)
2810 		if (!hw->blk[blk].xlt2.vsig_tbl[i].in_use)
2811 			return ice_vsig_alloc_val(hw, blk, i);
2812 
2813 	return ICE_DEFAULT_VSIG;
2814 }
2815 
2816 /**
2817  * ice_find_dup_props_vsig - find VSI group with a specified set of properties
2818  * @hw: pointer to the hardware structure
2819  * @blk: HW block
2820  * @chs: characteristic list
2821  * @vsig: returns the VSIG with the matching profiles, if found
2822  *
2823  * Each VSIG is associated with a characteristic set; i.e. all VSIs under
2824  * a group have the same characteristic set. To check if there exists a VSIG
2825  * which has the same characteristics as the input characteristics; this
2826  * function will iterate through the XLT2 list and return the VSIG that has a
2827  * matching configuration. In order to make sure that priorities are accounted
2828  * for, the list must match exactly, including the order in which the
2829  * characteristics are listed.
2830  */
2831 static int
2832 ice_find_dup_props_vsig(struct ice_hw *hw, enum ice_block blk,
2833 			struct list_head *chs, u16 *vsig)
2834 {
2835 	struct ice_xlt2 *xlt2 = &hw->blk[blk].xlt2;
2836 	u16 i;
2837 
2838 	for (i = 0; i < xlt2->count; i++)
2839 		if (xlt2->vsig_tbl[i].in_use &&
2840 		    ice_match_prop_lst(chs, &xlt2->vsig_tbl[i].prop_lst)) {
2841 			*vsig = ICE_VSIG_VALUE(i, hw->pf_id);
2842 			return 0;
2843 		}
2844 
2845 	return -ENOENT;
2846 }
2847 
2848 /**
2849  * ice_vsig_free - free VSI group
2850  * @hw: pointer to the hardware structure
2851  * @blk: HW block
2852  * @vsig: VSIG to remove
2853  *
2854  * The function will remove all VSIs associated with the input VSIG and move
2855  * them to the DEFAULT_VSIG and mark the VSIG available.
2856  */
2857 static int ice_vsig_free(struct ice_hw *hw, enum ice_block blk, u16 vsig)
2858 {
2859 	struct ice_vsig_prof *dtmp, *del;
2860 	struct ice_vsig_vsi *vsi_cur;
2861 	u16 idx;
2862 
2863 	idx = vsig & ICE_VSIG_IDX_M;
2864 	if (idx >= ICE_MAX_VSIGS)
2865 		return -EINVAL;
2866 
2867 	if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use)
2868 		return -ENOENT;
2869 
2870 	hw->blk[blk].xlt2.vsig_tbl[idx].in_use = false;
2871 
2872 	vsi_cur = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
2873 	/* If the VSIG has at least 1 VSI then iterate through the
2874 	 * list and remove the VSIs before deleting the group.
2875 	 */
2876 	if (vsi_cur) {
2877 		/* remove all vsis associated with this VSIG XLT2 entry */
2878 		do {
2879 			struct ice_vsig_vsi *tmp = vsi_cur->next_vsi;
2880 
2881 			vsi_cur->vsig = ICE_DEFAULT_VSIG;
2882 			vsi_cur->changed = 1;
2883 			vsi_cur->next_vsi = NULL;
2884 			vsi_cur = tmp;
2885 		} while (vsi_cur);
2886 
2887 		/* NULL terminate head of VSI list */
2888 		hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi = NULL;
2889 	}
2890 
2891 	/* free characteristic list */
2892 	list_for_each_entry_safe(del, dtmp,
2893 				 &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
2894 				 list) {
2895 		list_del(&del->list);
2896 		devm_kfree(ice_hw_to_dev(hw), del);
2897 	}
2898 
2899 	/* if VSIG characteristic list was cleared for reset
2900 	 * re-initialize the list head
2901 	 */
2902 	INIT_LIST_HEAD(&hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst);
2903 
2904 	return 0;
2905 }
2906 
2907 /**
2908  * ice_vsig_remove_vsi - remove VSI from VSIG
2909  * @hw: pointer to the hardware structure
2910  * @blk: HW block
2911  * @vsi: VSI to remove
2912  * @vsig: VSI group to remove from
2913  *
2914  * The function will remove the input VSI from its VSI group and move it
2915  * to the DEFAULT_VSIG.
2916  */
2917 static int
2918 ice_vsig_remove_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 vsig)
2919 {
2920 	struct ice_vsig_vsi **vsi_head, *vsi_cur, *vsi_tgt;
2921 	u16 idx;
2922 
2923 	idx = vsig & ICE_VSIG_IDX_M;
2924 
2925 	if (vsi >= ICE_MAX_VSI || idx >= ICE_MAX_VSIGS)
2926 		return -EINVAL;
2927 
2928 	if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use)
2929 		return -ENOENT;
2930 
2931 	/* entry already in default VSIG, don't have to remove */
2932 	if (idx == ICE_DEFAULT_VSIG)
2933 		return 0;
2934 
2935 	vsi_head = &hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
2936 	if (!(*vsi_head))
2937 		return -EIO;
2938 
2939 	vsi_tgt = &hw->blk[blk].xlt2.vsis[vsi];
2940 	vsi_cur = (*vsi_head);
2941 
2942 	/* iterate the VSI list, skip over the entry to be removed */
2943 	while (vsi_cur) {
2944 		if (vsi_tgt == vsi_cur) {
2945 			(*vsi_head) = vsi_cur->next_vsi;
2946 			break;
2947 		}
2948 		vsi_head = &vsi_cur->next_vsi;
2949 		vsi_cur = vsi_cur->next_vsi;
2950 	}
2951 
2952 	/* verify if VSI was removed from group list */
2953 	if (!vsi_cur)
2954 		return -ENOENT;
2955 
2956 	vsi_cur->vsig = ICE_DEFAULT_VSIG;
2957 	vsi_cur->changed = 1;
2958 	vsi_cur->next_vsi = NULL;
2959 
2960 	return 0;
2961 }
2962 
2963 /**
2964  * ice_vsig_add_mv_vsi - add or move a VSI to a VSI group
2965  * @hw: pointer to the hardware structure
2966  * @blk: HW block
2967  * @vsi: VSI to move
2968  * @vsig: destination VSI group
2969  *
2970  * This function will move or add the input VSI to the target VSIG.
2971  * The function will find the original VSIG the VSI belongs to and
2972  * move the entry to the DEFAULT_VSIG, update the original VSIG and
2973  * then move entry to the new VSIG.
2974  */
2975 static int
2976 ice_vsig_add_mv_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 vsig)
2977 {
2978 	struct ice_vsig_vsi *tmp;
2979 	u16 orig_vsig, idx;
2980 	int status;
2981 
2982 	idx = vsig & ICE_VSIG_IDX_M;
2983 
2984 	if (vsi >= ICE_MAX_VSI || idx >= ICE_MAX_VSIGS)
2985 		return -EINVAL;
2986 
2987 	/* if VSIG not in use and VSIG is not default type this VSIG
2988 	 * doesn't exist.
2989 	 */
2990 	if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use &&
2991 	    vsig != ICE_DEFAULT_VSIG)
2992 		return -ENOENT;
2993 
2994 	status = ice_vsig_find_vsi(hw, blk, vsi, &orig_vsig);
2995 	if (status)
2996 		return status;
2997 
2998 	/* no update required if vsigs match */
2999 	if (orig_vsig == vsig)
3000 		return 0;
3001 
3002 	if (orig_vsig != ICE_DEFAULT_VSIG) {
3003 		/* remove entry from orig_vsig and add to default VSIG */
3004 		status = ice_vsig_remove_vsi(hw, blk, vsi, orig_vsig);
3005 		if (status)
3006 			return status;
3007 	}
3008 
3009 	if (idx == ICE_DEFAULT_VSIG)
3010 		return 0;
3011 
3012 	/* Create VSI entry and add VSIG and prop_mask values */
3013 	hw->blk[blk].xlt2.vsis[vsi].vsig = vsig;
3014 	hw->blk[blk].xlt2.vsis[vsi].changed = 1;
3015 
3016 	/* Add new entry to the head of the VSIG list */
3017 	tmp = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
3018 	hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi =
3019 		&hw->blk[blk].xlt2.vsis[vsi];
3020 	hw->blk[blk].xlt2.vsis[vsi].next_vsi = tmp;
3021 	hw->blk[blk].xlt2.t[vsi] = vsig;
3022 
3023 	return 0;
3024 }
3025 
3026 /**
3027  * ice_prof_has_mask_idx - determine if profile index masking is identical
3028  * @hw: pointer to the hardware structure
3029  * @blk: HW block
3030  * @prof: profile to check
3031  * @idx: profile index to check
3032  * @mask: mask to match
3033  */
3034 static bool
3035 ice_prof_has_mask_idx(struct ice_hw *hw, enum ice_block blk, u8 prof, u16 idx,
3036 		      u16 mask)
3037 {
3038 	bool expect_no_mask = false;
3039 	bool found = false;
3040 	bool match = false;
3041 	u16 i;
3042 
3043 	/* If mask is 0x0000 or 0xffff, then there is no masking */
3044 	if (mask == 0 || mask == 0xffff)
3045 		expect_no_mask = true;
3046 
3047 	/* Scan the enabled masks on this profile, for the specified idx */
3048 	for (i = hw->blk[blk].masks.first; i < hw->blk[blk].masks.first +
3049 	     hw->blk[blk].masks.count; i++)
3050 		if (hw->blk[blk].es.mask_ena[prof] & BIT(i))
3051 			if (hw->blk[blk].masks.masks[i].in_use &&
3052 			    hw->blk[blk].masks.masks[i].idx == idx) {
3053 				found = true;
3054 				if (hw->blk[blk].masks.masks[i].mask == mask)
3055 					match = true;
3056 				break;
3057 			}
3058 
3059 	if (expect_no_mask) {
3060 		if (found)
3061 			return false;
3062 	} else {
3063 		if (!match)
3064 			return false;
3065 	}
3066 
3067 	return true;
3068 }
3069 
3070 /**
3071  * ice_prof_has_mask - determine if profile masking is identical
3072  * @hw: pointer to the hardware structure
3073  * @blk: HW block
3074  * @prof: profile to check
3075  * @masks: masks to match
3076  */
3077 static bool
3078 ice_prof_has_mask(struct ice_hw *hw, enum ice_block blk, u8 prof, u16 *masks)
3079 {
3080 	u16 i;
3081 
3082 	/* es->mask_ena[prof] will have the mask */
3083 	for (i = 0; i < hw->blk[blk].es.fvw; i++)
3084 		if (!ice_prof_has_mask_idx(hw, blk, prof, i, masks[i]))
3085 			return false;
3086 
3087 	return true;
3088 }
3089 
3090 /**
3091  * ice_find_prof_id_with_mask - find profile ID for a given field vector
3092  * @hw: pointer to the hardware structure
3093  * @blk: HW block
3094  * @fv: field vector to search for
3095  * @masks: masks for FV
3096  * @prof_id: receives the profile ID
3097  */
3098 static int
3099 ice_find_prof_id_with_mask(struct ice_hw *hw, enum ice_block blk,
3100 			   struct ice_fv_word *fv, u16 *masks, u8 *prof_id)
3101 {
3102 	struct ice_es *es = &hw->blk[blk].es;
3103 	u8 i;
3104 
3105 	/* For FD, we don't want to re-use a existed profile with the same
3106 	 * field vector and mask. This will cause rule interference.
3107 	 */
3108 	if (blk == ICE_BLK_FD)
3109 		return -ENOENT;
3110 
3111 	for (i = 0; i < (u8)es->count; i++) {
3112 		u16 off = i * es->fvw;
3113 
3114 		if (memcmp(&es->t[off], fv, es->fvw * sizeof(*fv)))
3115 			continue;
3116 
3117 		/* check if masks settings are the same for this profile */
3118 		if (masks && !ice_prof_has_mask(hw, blk, i, masks))
3119 			continue;
3120 
3121 		*prof_id = i;
3122 		return 0;
3123 	}
3124 
3125 	return -ENOENT;
3126 }
3127 
3128 /**
3129  * ice_prof_id_rsrc_type - get profile ID resource type for a block type
3130  * @blk: the block type
3131  * @rsrc_type: pointer to variable to receive the resource type
3132  */
3133 static bool ice_prof_id_rsrc_type(enum ice_block blk, u16 *rsrc_type)
3134 {
3135 	switch (blk) {
3136 	case ICE_BLK_FD:
3137 		*rsrc_type = ICE_AQC_RES_TYPE_FD_PROF_BLDR_PROFID;
3138 		break;
3139 	case ICE_BLK_RSS:
3140 		*rsrc_type = ICE_AQC_RES_TYPE_HASH_PROF_BLDR_PROFID;
3141 		break;
3142 	default:
3143 		return false;
3144 	}
3145 	return true;
3146 }
3147 
3148 /**
3149  * ice_tcam_ent_rsrc_type - get TCAM entry resource type for a block type
3150  * @blk: the block type
3151  * @rsrc_type: pointer to variable to receive the resource type
3152  */
3153 static bool ice_tcam_ent_rsrc_type(enum ice_block blk, u16 *rsrc_type)
3154 {
3155 	switch (blk) {
3156 	case ICE_BLK_FD:
3157 		*rsrc_type = ICE_AQC_RES_TYPE_FD_PROF_BLDR_TCAM;
3158 		break;
3159 	case ICE_BLK_RSS:
3160 		*rsrc_type = ICE_AQC_RES_TYPE_HASH_PROF_BLDR_TCAM;
3161 		break;
3162 	default:
3163 		return false;
3164 	}
3165 	return true;
3166 }
3167 
3168 /**
3169  * ice_alloc_tcam_ent - allocate hardware TCAM entry
3170  * @hw: pointer to the HW struct
3171  * @blk: the block to allocate the TCAM for
3172  * @btm: true to allocate from bottom of table, false to allocate from top
3173  * @tcam_idx: pointer to variable to receive the TCAM entry
3174  *
3175  * This function allocates a new entry in a Profile ID TCAM for a specific
3176  * block.
3177  */
3178 static int
3179 ice_alloc_tcam_ent(struct ice_hw *hw, enum ice_block blk, bool btm,
3180 		   u16 *tcam_idx)
3181 {
3182 	u16 res_type;
3183 
3184 	if (!ice_tcam_ent_rsrc_type(blk, &res_type))
3185 		return -EINVAL;
3186 
3187 	return ice_alloc_hw_res(hw, res_type, 1, btm, tcam_idx);
3188 }
3189 
3190 /**
3191  * ice_free_tcam_ent - free hardware TCAM entry
3192  * @hw: pointer to the HW struct
3193  * @blk: the block from which to free the TCAM entry
3194  * @tcam_idx: the TCAM entry to free
3195  *
3196  * This function frees an entry in a Profile ID TCAM for a specific block.
3197  */
3198 static int
3199 ice_free_tcam_ent(struct ice_hw *hw, enum ice_block blk, u16 tcam_idx)
3200 {
3201 	u16 res_type;
3202 
3203 	if (!ice_tcam_ent_rsrc_type(blk, &res_type))
3204 		return -EINVAL;
3205 
3206 	return ice_free_hw_res(hw, res_type, 1, &tcam_idx);
3207 }
3208 
3209 /**
3210  * ice_alloc_prof_id - allocate profile ID
3211  * @hw: pointer to the HW struct
3212  * @blk: the block to allocate the profile ID for
3213  * @prof_id: pointer to variable to receive the profile ID
3214  *
3215  * This function allocates a new profile ID, which also corresponds to a Field
3216  * Vector (Extraction Sequence) entry.
3217  */
3218 static int ice_alloc_prof_id(struct ice_hw *hw, enum ice_block blk, u8 *prof_id)
3219 {
3220 	u16 res_type;
3221 	u16 get_prof;
3222 	int status;
3223 
3224 	if (!ice_prof_id_rsrc_type(blk, &res_type))
3225 		return -EINVAL;
3226 
3227 	status = ice_alloc_hw_res(hw, res_type, 1, false, &get_prof);
3228 	if (!status)
3229 		*prof_id = (u8)get_prof;
3230 
3231 	return status;
3232 }
3233 
3234 /**
3235  * ice_free_prof_id - free profile ID
3236  * @hw: pointer to the HW struct
3237  * @blk: the block from which to free the profile ID
3238  * @prof_id: the profile ID to free
3239  *
3240  * This function frees a profile ID, which also corresponds to a Field Vector.
3241  */
3242 static int ice_free_prof_id(struct ice_hw *hw, enum ice_block blk, u8 prof_id)
3243 {
3244 	u16 tmp_prof_id = (u16)prof_id;
3245 	u16 res_type;
3246 
3247 	if (!ice_prof_id_rsrc_type(blk, &res_type))
3248 		return -EINVAL;
3249 
3250 	return ice_free_hw_res(hw, res_type, 1, &tmp_prof_id);
3251 }
3252 
3253 /**
3254  * ice_prof_inc_ref - increment reference count for profile
3255  * @hw: pointer to the HW struct
3256  * @blk: the block from which to free the profile ID
3257  * @prof_id: the profile ID for which to increment the reference count
3258  */
3259 static int ice_prof_inc_ref(struct ice_hw *hw, enum ice_block blk, u8 prof_id)
3260 {
3261 	if (prof_id > hw->blk[blk].es.count)
3262 		return -EINVAL;
3263 
3264 	hw->blk[blk].es.ref_count[prof_id]++;
3265 
3266 	return 0;
3267 }
3268 
3269 /**
3270  * ice_write_prof_mask_reg - write profile mask register
3271  * @hw: pointer to the HW struct
3272  * @blk: hardware block
3273  * @mask_idx: mask index
3274  * @idx: index of the FV which will use the mask
3275  * @mask: the 16-bit mask
3276  */
3277 static void
3278 ice_write_prof_mask_reg(struct ice_hw *hw, enum ice_block blk, u16 mask_idx,
3279 			u16 idx, u16 mask)
3280 {
3281 	u32 offset;
3282 	u32 val;
3283 
3284 	switch (blk) {
3285 	case ICE_BLK_RSS:
3286 		offset = GLQF_HMASK(mask_idx);
3287 		val = (idx << GLQF_HMASK_MSK_INDEX_S) & GLQF_HMASK_MSK_INDEX_M;
3288 		val |= (mask << GLQF_HMASK_MASK_S) & GLQF_HMASK_MASK_M;
3289 		break;
3290 	case ICE_BLK_FD:
3291 		offset = GLQF_FDMASK(mask_idx);
3292 		val = (idx << GLQF_FDMASK_MSK_INDEX_S) & GLQF_FDMASK_MSK_INDEX_M;
3293 		val |= (mask << GLQF_FDMASK_MASK_S) & GLQF_FDMASK_MASK_M;
3294 		break;
3295 	default:
3296 		ice_debug(hw, ICE_DBG_PKG, "No profile masks for block %d\n",
3297 			  blk);
3298 		return;
3299 	}
3300 
3301 	wr32(hw, offset, val);
3302 	ice_debug(hw, ICE_DBG_PKG, "write mask, blk %d (%d): %x = %x\n",
3303 		  blk, idx, offset, val);
3304 }
3305 
3306 /**
3307  * ice_write_prof_mask_enable_res - write profile mask enable register
3308  * @hw: pointer to the HW struct
3309  * @blk: hardware block
3310  * @prof_id: profile ID
3311  * @enable_mask: enable mask
3312  */
3313 static void
3314 ice_write_prof_mask_enable_res(struct ice_hw *hw, enum ice_block blk,
3315 			       u16 prof_id, u32 enable_mask)
3316 {
3317 	u32 offset;
3318 
3319 	switch (blk) {
3320 	case ICE_BLK_RSS:
3321 		offset = GLQF_HMASK_SEL(prof_id);
3322 		break;
3323 	case ICE_BLK_FD:
3324 		offset = GLQF_FDMASK_SEL(prof_id);
3325 		break;
3326 	default:
3327 		ice_debug(hw, ICE_DBG_PKG, "No profile masks for block %d\n",
3328 			  blk);
3329 		return;
3330 	}
3331 
3332 	wr32(hw, offset, enable_mask);
3333 	ice_debug(hw, ICE_DBG_PKG, "write mask enable, blk %d (%d): %x = %x\n",
3334 		  blk, prof_id, offset, enable_mask);
3335 }
3336 
3337 /**
3338  * ice_init_prof_masks - initial prof masks
3339  * @hw: pointer to the HW struct
3340  * @blk: hardware block
3341  */
3342 static void ice_init_prof_masks(struct ice_hw *hw, enum ice_block blk)
3343 {
3344 	u16 per_pf;
3345 	u16 i;
3346 
3347 	mutex_init(&hw->blk[blk].masks.lock);
3348 
3349 	per_pf = ICE_PROF_MASK_COUNT / hw->dev_caps.num_funcs;
3350 
3351 	hw->blk[blk].masks.count = per_pf;
3352 	hw->blk[blk].masks.first = hw->pf_id * per_pf;
3353 
3354 	memset(hw->blk[blk].masks.masks, 0, sizeof(hw->blk[blk].masks.masks));
3355 
3356 	for (i = hw->blk[blk].masks.first;
3357 	     i < hw->blk[blk].masks.first + hw->blk[blk].masks.count; i++)
3358 		ice_write_prof_mask_reg(hw, blk, i, 0, 0);
3359 }
3360 
3361 /**
3362  * ice_init_all_prof_masks - initialize all prof masks
3363  * @hw: pointer to the HW struct
3364  */
3365 static void ice_init_all_prof_masks(struct ice_hw *hw)
3366 {
3367 	ice_init_prof_masks(hw, ICE_BLK_RSS);
3368 	ice_init_prof_masks(hw, ICE_BLK_FD);
3369 }
3370 
3371 /**
3372  * ice_alloc_prof_mask - allocate profile mask
3373  * @hw: pointer to the HW struct
3374  * @blk: hardware block
3375  * @idx: index of FV which will use the mask
3376  * @mask: the 16-bit mask
3377  * @mask_idx: variable to receive the mask index
3378  */
3379 static int
3380 ice_alloc_prof_mask(struct ice_hw *hw, enum ice_block blk, u16 idx, u16 mask,
3381 		    u16 *mask_idx)
3382 {
3383 	bool found_unused = false, found_copy = false;
3384 	u16 unused_idx = 0, copy_idx = 0;
3385 	int status = -ENOSPC;
3386 	u16 i;
3387 
3388 	if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD)
3389 		return -EINVAL;
3390 
3391 	mutex_lock(&hw->blk[blk].masks.lock);
3392 
3393 	for (i = hw->blk[blk].masks.first;
3394 	     i < hw->blk[blk].masks.first + hw->blk[blk].masks.count; i++)
3395 		if (hw->blk[blk].masks.masks[i].in_use) {
3396 			/* if mask is in use and it exactly duplicates the
3397 			 * desired mask and index, then in can be reused
3398 			 */
3399 			if (hw->blk[blk].masks.masks[i].mask == mask &&
3400 			    hw->blk[blk].masks.masks[i].idx == idx) {
3401 				found_copy = true;
3402 				copy_idx = i;
3403 				break;
3404 			}
3405 		} else {
3406 			/* save off unused index, but keep searching in case
3407 			 * there is an exact match later on
3408 			 */
3409 			if (!found_unused) {
3410 				found_unused = true;
3411 				unused_idx = i;
3412 			}
3413 		}
3414 
3415 	if (found_copy)
3416 		i = copy_idx;
3417 	else if (found_unused)
3418 		i = unused_idx;
3419 	else
3420 		goto err_ice_alloc_prof_mask;
3421 
3422 	/* update mask for a new entry */
3423 	if (found_unused) {
3424 		hw->blk[blk].masks.masks[i].in_use = true;
3425 		hw->blk[blk].masks.masks[i].mask = mask;
3426 		hw->blk[blk].masks.masks[i].idx = idx;
3427 		hw->blk[blk].masks.masks[i].ref = 0;
3428 		ice_write_prof_mask_reg(hw, blk, i, idx, mask);
3429 	}
3430 
3431 	hw->blk[blk].masks.masks[i].ref++;
3432 	*mask_idx = i;
3433 	status = 0;
3434 
3435 err_ice_alloc_prof_mask:
3436 	mutex_unlock(&hw->blk[blk].masks.lock);
3437 
3438 	return status;
3439 }
3440 
3441 /**
3442  * ice_free_prof_mask - free profile mask
3443  * @hw: pointer to the HW struct
3444  * @blk: hardware block
3445  * @mask_idx: index of mask
3446  */
3447 static int
3448 ice_free_prof_mask(struct ice_hw *hw, enum ice_block blk, u16 mask_idx)
3449 {
3450 	if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD)
3451 		return -EINVAL;
3452 
3453 	if (!(mask_idx >= hw->blk[blk].masks.first &&
3454 	      mask_idx < hw->blk[blk].masks.first + hw->blk[blk].masks.count))
3455 		return -ENOENT;
3456 
3457 	mutex_lock(&hw->blk[blk].masks.lock);
3458 
3459 	if (!hw->blk[blk].masks.masks[mask_idx].in_use)
3460 		goto exit_ice_free_prof_mask;
3461 
3462 	if (hw->blk[blk].masks.masks[mask_idx].ref > 1) {
3463 		hw->blk[blk].masks.masks[mask_idx].ref--;
3464 		goto exit_ice_free_prof_mask;
3465 	}
3466 
3467 	/* remove mask */
3468 	hw->blk[blk].masks.masks[mask_idx].in_use = false;
3469 	hw->blk[blk].masks.masks[mask_idx].mask = 0;
3470 	hw->blk[blk].masks.masks[mask_idx].idx = 0;
3471 
3472 	/* update mask as unused entry */
3473 	ice_debug(hw, ICE_DBG_PKG, "Free mask, blk %d, mask %d\n", blk,
3474 		  mask_idx);
3475 	ice_write_prof_mask_reg(hw, blk, mask_idx, 0, 0);
3476 
3477 exit_ice_free_prof_mask:
3478 	mutex_unlock(&hw->blk[blk].masks.lock);
3479 
3480 	return 0;
3481 }
3482 
3483 /**
3484  * ice_free_prof_masks - free all profile masks for a profile
3485  * @hw: pointer to the HW struct
3486  * @blk: hardware block
3487  * @prof_id: profile ID
3488  */
3489 static int
3490 ice_free_prof_masks(struct ice_hw *hw, enum ice_block blk, u16 prof_id)
3491 {
3492 	u32 mask_bm;
3493 	u16 i;
3494 
3495 	if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD)
3496 		return -EINVAL;
3497 
3498 	mask_bm = hw->blk[blk].es.mask_ena[prof_id];
3499 	for (i = 0; i < BITS_PER_BYTE * sizeof(mask_bm); i++)
3500 		if (mask_bm & BIT(i))
3501 			ice_free_prof_mask(hw, blk, i);
3502 
3503 	return 0;
3504 }
3505 
3506 /**
3507  * ice_shutdown_prof_masks - releases lock for masking
3508  * @hw: pointer to the HW struct
3509  * @blk: hardware block
3510  *
3511  * This should be called before unloading the driver
3512  */
3513 static void ice_shutdown_prof_masks(struct ice_hw *hw, enum ice_block blk)
3514 {
3515 	u16 i;
3516 
3517 	mutex_lock(&hw->blk[blk].masks.lock);
3518 
3519 	for (i = hw->blk[blk].masks.first;
3520 	     i < hw->blk[blk].masks.first + hw->blk[blk].masks.count; i++) {
3521 		ice_write_prof_mask_reg(hw, blk, i, 0, 0);
3522 
3523 		hw->blk[blk].masks.masks[i].in_use = false;
3524 		hw->blk[blk].masks.masks[i].idx = 0;
3525 		hw->blk[blk].masks.masks[i].mask = 0;
3526 	}
3527 
3528 	mutex_unlock(&hw->blk[blk].masks.lock);
3529 	mutex_destroy(&hw->blk[blk].masks.lock);
3530 }
3531 
3532 /**
3533  * ice_shutdown_all_prof_masks - releases all locks for masking
3534  * @hw: pointer to the HW struct
3535  *
3536  * This should be called before unloading the driver
3537  */
3538 static void ice_shutdown_all_prof_masks(struct ice_hw *hw)
3539 {
3540 	ice_shutdown_prof_masks(hw, ICE_BLK_RSS);
3541 	ice_shutdown_prof_masks(hw, ICE_BLK_FD);
3542 }
3543 
3544 /**
3545  * ice_update_prof_masking - set registers according to masking
3546  * @hw: pointer to the HW struct
3547  * @blk: hardware block
3548  * @prof_id: profile ID
3549  * @masks: masks
3550  */
3551 static int
3552 ice_update_prof_masking(struct ice_hw *hw, enum ice_block blk, u16 prof_id,
3553 			u16 *masks)
3554 {
3555 	bool err = false;
3556 	u32 ena_mask = 0;
3557 	u16 idx;
3558 	u16 i;
3559 
3560 	/* Only support FD and RSS masking, otherwise nothing to be done */
3561 	if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD)
3562 		return 0;
3563 
3564 	for (i = 0; i < hw->blk[blk].es.fvw; i++)
3565 		if (masks[i] && masks[i] != 0xFFFF) {
3566 			if (!ice_alloc_prof_mask(hw, blk, i, masks[i], &idx)) {
3567 				ena_mask |= BIT(idx);
3568 			} else {
3569 				/* not enough bitmaps */
3570 				err = true;
3571 				break;
3572 			}
3573 		}
3574 
3575 	if (err) {
3576 		/* free any bitmaps we have allocated */
3577 		for (i = 0; i < BITS_PER_BYTE * sizeof(ena_mask); i++)
3578 			if (ena_mask & BIT(i))
3579 				ice_free_prof_mask(hw, blk, i);
3580 
3581 		return -EIO;
3582 	}
3583 
3584 	/* enable the masks for this profile */
3585 	ice_write_prof_mask_enable_res(hw, blk, prof_id, ena_mask);
3586 
3587 	/* store enabled masks with profile so that they can be freed later */
3588 	hw->blk[blk].es.mask_ena[prof_id] = ena_mask;
3589 
3590 	return 0;
3591 }
3592 
3593 /**
3594  * ice_write_es - write an extraction sequence to hardware
3595  * @hw: pointer to the HW struct
3596  * @blk: the block in which to write the extraction sequence
3597  * @prof_id: the profile ID to write
3598  * @fv: pointer to the extraction sequence to write - NULL to clear extraction
3599  */
3600 static void
3601 ice_write_es(struct ice_hw *hw, enum ice_block blk, u8 prof_id,
3602 	     struct ice_fv_word *fv)
3603 {
3604 	u16 off;
3605 
3606 	off = prof_id * hw->blk[blk].es.fvw;
3607 	if (!fv) {
3608 		memset(&hw->blk[blk].es.t[off], 0,
3609 		       hw->blk[blk].es.fvw * sizeof(*fv));
3610 		hw->blk[blk].es.written[prof_id] = false;
3611 	} else {
3612 		memcpy(&hw->blk[blk].es.t[off], fv,
3613 		       hw->blk[blk].es.fvw * sizeof(*fv));
3614 	}
3615 }
3616 
3617 /**
3618  * ice_prof_dec_ref - decrement reference count for profile
3619  * @hw: pointer to the HW struct
3620  * @blk: the block from which to free the profile ID
3621  * @prof_id: the profile ID for which to decrement the reference count
3622  */
3623 static int
3624 ice_prof_dec_ref(struct ice_hw *hw, enum ice_block blk, u8 prof_id)
3625 {
3626 	if (prof_id > hw->blk[blk].es.count)
3627 		return -EINVAL;
3628 
3629 	if (hw->blk[blk].es.ref_count[prof_id] > 0) {
3630 		if (!--hw->blk[blk].es.ref_count[prof_id]) {
3631 			ice_write_es(hw, blk, prof_id, NULL);
3632 			ice_free_prof_masks(hw, blk, prof_id);
3633 			return ice_free_prof_id(hw, blk, prof_id);
3634 		}
3635 	}
3636 
3637 	return 0;
3638 }
3639 
3640 /* Block / table section IDs */
3641 static const u32 ice_blk_sids[ICE_BLK_COUNT][ICE_SID_OFF_COUNT] = {
3642 	/* SWITCH */
3643 	{	ICE_SID_XLT1_SW,
3644 		ICE_SID_XLT2_SW,
3645 		ICE_SID_PROFID_TCAM_SW,
3646 		ICE_SID_PROFID_REDIR_SW,
3647 		ICE_SID_FLD_VEC_SW
3648 	},
3649 
3650 	/* ACL */
3651 	{	ICE_SID_XLT1_ACL,
3652 		ICE_SID_XLT2_ACL,
3653 		ICE_SID_PROFID_TCAM_ACL,
3654 		ICE_SID_PROFID_REDIR_ACL,
3655 		ICE_SID_FLD_VEC_ACL
3656 	},
3657 
3658 	/* FD */
3659 	{	ICE_SID_XLT1_FD,
3660 		ICE_SID_XLT2_FD,
3661 		ICE_SID_PROFID_TCAM_FD,
3662 		ICE_SID_PROFID_REDIR_FD,
3663 		ICE_SID_FLD_VEC_FD
3664 	},
3665 
3666 	/* RSS */
3667 	{	ICE_SID_XLT1_RSS,
3668 		ICE_SID_XLT2_RSS,
3669 		ICE_SID_PROFID_TCAM_RSS,
3670 		ICE_SID_PROFID_REDIR_RSS,
3671 		ICE_SID_FLD_VEC_RSS
3672 	},
3673 
3674 	/* PE */
3675 	{	ICE_SID_XLT1_PE,
3676 		ICE_SID_XLT2_PE,
3677 		ICE_SID_PROFID_TCAM_PE,
3678 		ICE_SID_PROFID_REDIR_PE,
3679 		ICE_SID_FLD_VEC_PE
3680 	}
3681 };
3682 
3683 /**
3684  * ice_init_sw_xlt1_db - init software XLT1 database from HW tables
3685  * @hw: pointer to the hardware structure
3686  * @blk: the HW block to initialize
3687  */
3688 static void ice_init_sw_xlt1_db(struct ice_hw *hw, enum ice_block blk)
3689 {
3690 	u16 pt;
3691 
3692 	for (pt = 0; pt < hw->blk[blk].xlt1.count; pt++) {
3693 		u8 ptg;
3694 
3695 		ptg = hw->blk[blk].xlt1.t[pt];
3696 		if (ptg != ICE_DEFAULT_PTG) {
3697 			ice_ptg_alloc_val(hw, blk, ptg);
3698 			ice_ptg_add_mv_ptype(hw, blk, pt, ptg);
3699 		}
3700 	}
3701 }
3702 
3703 /**
3704  * ice_init_sw_xlt2_db - init software XLT2 database from HW tables
3705  * @hw: pointer to the hardware structure
3706  * @blk: the HW block to initialize
3707  */
3708 static void ice_init_sw_xlt2_db(struct ice_hw *hw, enum ice_block blk)
3709 {
3710 	u16 vsi;
3711 
3712 	for (vsi = 0; vsi < hw->blk[blk].xlt2.count; vsi++) {
3713 		u16 vsig;
3714 
3715 		vsig = hw->blk[blk].xlt2.t[vsi];
3716 		if (vsig) {
3717 			ice_vsig_alloc_val(hw, blk, vsig);
3718 			ice_vsig_add_mv_vsi(hw, blk, vsi, vsig);
3719 			/* no changes at this time, since this has been
3720 			 * initialized from the original package
3721 			 */
3722 			hw->blk[blk].xlt2.vsis[vsi].changed = 0;
3723 		}
3724 	}
3725 }
3726 
3727 /**
3728  * ice_init_sw_db - init software database from HW tables
3729  * @hw: pointer to the hardware structure
3730  */
3731 static void ice_init_sw_db(struct ice_hw *hw)
3732 {
3733 	u16 i;
3734 
3735 	for (i = 0; i < ICE_BLK_COUNT; i++) {
3736 		ice_init_sw_xlt1_db(hw, (enum ice_block)i);
3737 		ice_init_sw_xlt2_db(hw, (enum ice_block)i);
3738 	}
3739 }
3740 
3741 /**
3742  * ice_fill_tbl - Reads content of a single table type into database
3743  * @hw: pointer to the hardware structure
3744  * @block_id: Block ID of the table to copy
3745  * @sid: Section ID of the table to copy
3746  *
3747  * Will attempt to read the entire content of a given table of a single block
3748  * into the driver database. We assume that the buffer will always
3749  * be as large or larger than the data contained in the package. If
3750  * this condition is not met, there is most likely an error in the package
3751  * contents.
3752  */
3753 static void ice_fill_tbl(struct ice_hw *hw, enum ice_block block_id, u32 sid)
3754 {
3755 	u32 dst_len, sect_len, offset = 0;
3756 	struct ice_prof_redir_section *pr;
3757 	struct ice_prof_id_section *pid;
3758 	struct ice_xlt1_section *xlt1;
3759 	struct ice_xlt2_section *xlt2;
3760 	struct ice_sw_fv_section *es;
3761 	struct ice_pkg_enum state;
3762 	u8 *src, *dst;
3763 	void *sect;
3764 
3765 	/* if the HW segment pointer is null then the first iteration of
3766 	 * ice_pkg_enum_section() will fail. In this case the HW tables will
3767 	 * not be filled and return success.
3768 	 */
3769 	if (!hw->seg) {
3770 		ice_debug(hw, ICE_DBG_PKG, "hw->seg is NULL, tables are not filled\n");
3771 		return;
3772 	}
3773 
3774 	memset(&state, 0, sizeof(state));
3775 
3776 	sect = ice_pkg_enum_section(hw->seg, &state, sid);
3777 
3778 	while (sect) {
3779 		switch (sid) {
3780 		case ICE_SID_XLT1_SW:
3781 		case ICE_SID_XLT1_FD:
3782 		case ICE_SID_XLT1_RSS:
3783 		case ICE_SID_XLT1_ACL:
3784 		case ICE_SID_XLT1_PE:
3785 			xlt1 = sect;
3786 			src = xlt1->value;
3787 			sect_len = le16_to_cpu(xlt1->count) *
3788 				sizeof(*hw->blk[block_id].xlt1.t);
3789 			dst = hw->blk[block_id].xlt1.t;
3790 			dst_len = hw->blk[block_id].xlt1.count *
3791 				sizeof(*hw->blk[block_id].xlt1.t);
3792 			break;
3793 		case ICE_SID_XLT2_SW:
3794 		case ICE_SID_XLT2_FD:
3795 		case ICE_SID_XLT2_RSS:
3796 		case ICE_SID_XLT2_ACL:
3797 		case ICE_SID_XLT2_PE:
3798 			xlt2 = sect;
3799 			src = (__force u8 *)xlt2->value;
3800 			sect_len = le16_to_cpu(xlt2->count) *
3801 				sizeof(*hw->blk[block_id].xlt2.t);
3802 			dst = (u8 *)hw->blk[block_id].xlt2.t;
3803 			dst_len = hw->blk[block_id].xlt2.count *
3804 				sizeof(*hw->blk[block_id].xlt2.t);
3805 			break;
3806 		case ICE_SID_PROFID_TCAM_SW:
3807 		case ICE_SID_PROFID_TCAM_FD:
3808 		case ICE_SID_PROFID_TCAM_RSS:
3809 		case ICE_SID_PROFID_TCAM_ACL:
3810 		case ICE_SID_PROFID_TCAM_PE:
3811 			pid = sect;
3812 			src = (u8 *)pid->entry;
3813 			sect_len = le16_to_cpu(pid->count) *
3814 				sizeof(*hw->blk[block_id].prof.t);
3815 			dst = (u8 *)hw->blk[block_id].prof.t;
3816 			dst_len = hw->blk[block_id].prof.count *
3817 				sizeof(*hw->blk[block_id].prof.t);
3818 			break;
3819 		case ICE_SID_PROFID_REDIR_SW:
3820 		case ICE_SID_PROFID_REDIR_FD:
3821 		case ICE_SID_PROFID_REDIR_RSS:
3822 		case ICE_SID_PROFID_REDIR_ACL:
3823 		case ICE_SID_PROFID_REDIR_PE:
3824 			pr = sect;
3825 			src = pr->redir_value;
3826 			sect_len = le16_to_cpu(pr->count) *
3827 				sizeof(*hw->blk[block_id].prof_redir.t);
3828 			dst = hw->blk[block_id].prof_redir.t;
3829 			dst_len = hw->blk[block_id].prof_redir.count *
3830 				sizeof(*hw->blk[block_id].prof_redir.t);
3831 			break;
3832 		case ICE_SID_FLD_VEC_SW:
3833 		case ICE_SID_FLD_VEC_FD:
3834 		case ICE_SID_FLD_VEC_RSS:
3835 		case ICE_SID_FLD_VEC_ACL:
3836 		case ICE_SID_FLD_VEC_PE:
3837 			es = sect;
3838 			src = (u8 *)es->fv;
3839 			sect_len = (u32)(le16_to_cpu(es->count) *
3840 					 hw->blk[block_id].es.fvw) *
3841 				sizeof(*hw->blk[block_id].es.t);
3842 			dst = (u8 *)hw->blk[block_id].es.t;
3843 			dst_len = (u32)(hw->blk[block_id].es.count *
3844 					hw->blk[block_id].es.fvw) *
3845 				sizeof(*hw->blk[block_id].es.t);
3846 			break;
3847 		default:
3848 			return;
3849 		}
3850 
3851 		/* if the section offset exceeds destination length, terminate
3852 		 * table fill.
3853 		 */
3854 		if (offset > dst_len)
3855 			return;
3856 
3857 		/* if the sum of section size and offset exceed destination size
3858 		 * then we are out of bounds of the HW table size for that PF.
3859 		 * Changing section length to fill the remaining table space
3860 		 * of that PF.
3861 		 */
3862 		if ((offset + sect_len) > dst_len)
3863 			sect_len = dst_len - offset;
3864 
3865 		memcpy(dst + offset, src, sect_len);
3866 		offset += sect_len;
3867 		sect = ice_pkg_enum_section(NULL, &state, sid);
3868 	}
3869 }
3870 
3871 /**
3872  * ice_fill_blk_tbls - Read package context for tables
3873  * @hw: pointer to the hardware structure
3874  *
3875  * Reads the current package contents and populates the driver
3876  * database with the data iteratively for all advanced feature
3877  * blocks. Assume that the HW tables have been allocated.
3878  */
3879 void ice_fill_blk_tbls(struct ice_hw *hw)
3880 {
3881 	u8 i;
3882 
3883 	for (i = 0; i < ICE_BLK_COUNT; i++) {
3884 		enum ice_block blk_id = (enum ice_block)i;
3885 
3886 		ice_fill_tbl(hw, blk_id, hw->blk[blk_id].xlt1.sid);
3887 		ice_fill_tbl(hw, blk_id, hw->blk[blk_id].xlt2.sid);
3888 		ice_fill_tbl(hw, blk_id, hw->blk[blk_id].prof.sid);
3889 		ice_fill_tbl(hw, blk_id, hw->blk[blk_id].prof_redir.sid);
3890 		ice_fill_tbl(hw, blk_id, hw->blk[blk_id].es.sid);
3891 	}
3892 
3893 	ice_init_sw_db(hw);
3894 }
3895 
3896 /**
3897  * ice_free_prof_map - free profile map
3898  * @hw: pointer to the hardware structure
3899  * @blk_idx: HW block index
3900  */
3901 static void ice_free_prof_map(struct ice_hw *hw, u8 blk_idx)
3902 {
3903 	struct ice_es *es = &hw->blk[blk_idx].es;
3904 	struct ice_prof_map *del, *tmp;
3905 
3906 	mutex_lock(&es->prof_map_lock);
3907 	list_for_each_entry_safe(del, tmp, &es->prof_map, list) {
3908 		list_del(&del->list);
3909 		devm_kfree(ice_hw_to_dev(hw), del);
3910 	}
3911 	INIT_LIST_HEAD(&es->prof_map);
3912 	mutex_unlock(&es->prof_map_lock);
3913 }
3914 
3915 /**
3916  * ice_free_flow_profs - free flow profile entries
3917  * @hw: pointer to the hardware structure
3918  * @blk_idx: HW block index
3919  */
3920 static void ice_free_flow_profs(struct ice_hw *hw, u8 blk_idx)
3921 {
3922 	struct ice_flow_prof *p, *tmp;
3923 
3924 	mutex_lock(&hw->fl_profs_locks[blk_idx]);
3925 	list_for_each_entry_safe(p, tmp, &hw->fl_profs[blk_idx], l_entry) {
3926 		struct ice_flow_entry *e, *t;
3927 
3928 		list_for_each_entry_safe(e, t, &p->entries, l_entry)
3929 			ice_flow_rem_entry(hw, (enum ice_block)blk_idx,
3930 					   ICE_FLOW_ENTRY_HNDL(e));
3931 
3932 		list_del(&p->l_entry);
3933 
3934 		mutex_destroy(&p->entries_lock);
3935 		devm_kfree(ice_hw_to_dev(hw), p);
3936 	}
3937 	mutex_unlock(&hw->fl_profs_locks[blk_idx]);
3938 
3939 	/* if driver is in reset and tables are being cleared
3940 	 * re-initialize the flow profile list heads
3941 	 */
3942 	INIT_LIST_HEAD(&hw->fl_profs[blk_idx]);
3943 }
3944 
3945 /**
3946  * ice_free_vsig_tbl - free complete VSIG table entries
3947  * @hw: pointer to the hardware structure
3948  * @blk: the HW block on which to free the VSIG table entries
3949  */
3950 static void ice_free_vsig_tbl(struct ice_hw *hw, enum ice_block blk)
3951 {
3952 	u16 i;
3953 
3954 	if (!hw->blk[blk].xlt2.vsig_tbl)
3955 		return;
3956 
3957 	for (i = 1; i < ICE_MAX_VSIGS; i++)
3958 		if (hw->blk[blk].xlt2.vsig_tbl[i].in_use)
3959 			ice_vsig_free(hw, blk, i);
3960 }
3961 
3962 /**
3963  * ice_free_hw_tbls - free hardware table memory
3964  * @hw: pointer to the hardware structure
3965  */
3966 void ice_free_hw_tbls(struct ice_hw *hw)
3967 {
3968 	struct ice_rss_cfg *r, *rt;
3969 	u8 i;
3970 
3971 	for (i = 0; i < ICE_BLK_COUNT; i++) {
3972 		if (hw->blk[i].is_list_init) {
3973 			struct ice_es *es = &hw->blk[i].es;
3974 
3975 			ice_free_prof_map(hw, i);
3976 			mutex_destroy(&es->prof_map_lock);
3977 
3978 			ice_free_flow_profs(hw, i);
3979 			mutex_destroy(&hw->fl_profs_locks[i]);
3980 
3981 			hw->blk[i].is_list_init = false;
3982 		}
3983 		ice_free_vsig_tbl(hw, (enum ice_block)i);
3984 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt1.ptypes);
3985 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt1.ptg_tbl);
3986 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt1.t);
3987 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt2.t);
3988 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt2.vsig_tbl);
3989 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt2.vsis);
3990 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].prof.t);
3991 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].prof_redir.t);
3992 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].es.t);
3993 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].es.ref_count);
3994 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].es.written);
3995 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].es.mask_ena);
3996 	}
3997 
3998 	list_for_each_entry_safe(r, rt, &hw->rss_list_head, l_entry) {
3999 		list_del(&r->l_entry);
4000 		devm_kfree(ice_hw_to_dev(hw), r);
4001 	}
4002 	mutex_destroy(&hw->rss_locks);
4003 	ice_shutdown_all_prof_masks(hw);
4004 	memset(hw->blk, 0, sizeof(hw->blk));
4005 }
4006 
4007 /**
4008  * ice_init_flow_profs - init flow profile locks and list heads
4009  * @hw: pointer to the hardware structure
4010  * @blk_idx: HW block index
4011  */
4012 static void ice_init_flow_profs(struct ice_hw *hw, u8 blk_idx)
4013 {
4014 	mutex_init(&hw->fl_profs_locks[blk_idx]);
4015 	INIT_LIST_HEAD(&hw->fl_profs[blk_idx]);
4016 }
4017 
4018 /**
4019  * ice_clear_hw_tbls - clear HW tables and flow profiles
4020  * @hw: pointer to the hardware structure
4021  */
4022 void ice_clear_hw_tbls(struct ice_hw *hw)
4023 {
4024 	u8 i;
4025 
4026 	for (i = 0; i < ICE_BLK_COUNT; i++) {
4027 		struct ice_prof_redir *prof_redir = &hw->blk[i].prof_redir;
4028 		struct ice_prof_tcam *prof = &hw->blk[i].prof;
4029 		struct ice_xlt1 *xlt1 = &hw->blk[i].xlt1;
4030 		struct ice_xlt2 *xlt2 = &hw->blk[i].xlt2;
4031 		struct ice_es *es = &hw->blk[i].es;
4032 
4033 		if (hw->blk[i].is_list_init) {
4034 			ice_free_prof_map(hw, i);
4035 			ice_free_flow_profs(hw, i);
4036 		}
4037 
4038 		ice_free_vsig_tbl(hw, (enum ice_block)i);
4039 
4040 		memset(xlt1->ptypes, 0, xlt1->count * sizeof(*xlt1->ptypes));
4041 		memset(xlt1->ptg_tbl, 0,
4042 		       ICE_MAX_PTGS * sizeof(*xlt1->ptg_tbl));
4043 		memset(xlt1->t, 0, xlt1->count * sizeof(*xlt1->t));
4044 
4045 		memset(xlt2->vsis, 0, xlt2->count * sizeof(*xlt2->vsis));
4046 		memset(xlt2->vsig_tbl, 0,
4047 		       xlt2->count * sizeof(*xlt2->vsig_tbl));
4048 		memset(xlt2->t, 0, xlt2->count * sizeof(*xlt2->t));
4049 
4050 		memset(prof->t, 0, prof->count * sizeof(*prof->t));
4051 		memset(prof_redir->t, 0,
4052 		       prof_redir->count * sizeof(*prof_redir->t));
4053 
4054 		memset(es->t, 0, es->count * sizeof(*es->t) * es->fvw);
4055 		memset(es->ref_count, 0, es->count * sizeof(*es->ref_count));
4056 		memset(es->written, 0, es->count * sizeof(*es->written));
4057 		memset(es->mask_ena, 0, es->count * sizeof(*es->mask_ena));
4058 	}
4059 }
4060 
4061 /**
4062  * ice_init_hw_tbls - init hardware table memory
4063  * @hw: pointer to the hardware structure
4064  */
4065 int ice_init_hw_tbls(struct ice_hw *hw)
4066 {
4067 	u8 i;
4068 
4069 	mutex_init(&hw->rss_locks);
4070 	INIT_LIST_HEAD(&hw->rss_list_head);
4071 	ice_init_all_prof_masks(hw);
4072 	for (i = 0; i < ICE_BLK_COUNT; i++) {
4073 		struct ice_prof_redir *prof_redir = &hw->blk[i].prof_redir;
4074 		struct ice_prof_tcam *prof = &hw->blk[i].prof;
4075 		struct ice_xlt1 *xlt1 = &hw->blk[i].xlt1;
4076 		struct ice_xlt2 *xlt2 = &hw->blk[i].xlt2;
4077 		struct ice_es *es = &hw->blk[i].es;
4078 		u16 j;
4079 
4080 		if (hw->blk[i].is_list_init)
4081 			continue;
4082 
4083 		ice_init_flow_profs(hw, i);
4084 		mutex_init(&es->prof_map_lock);
4085 		INIT_LIST_HEAD(&es->prof_map);
4086 		hw->blk[i].is_list_init = true;
4087 
4088 		hw->blk[i].overwrite = blk_sizes[i].overwrite;
4089 		es->reverse = blk_sizes[i].reverse;
4090 
4091 		xlt1->sid = ice_blk_sids[i][ICE_SID_XLT1_OFF];
4092 		xlt1->count = blk_sizes[i].xlt1;
4093 
4094 		xlt1->ptypes = devm_kcalloc(ice_hw_to_dev(hw), xlt1->count,
4095 					    sizeof(*xlt1->ptypes), GFP_KERNEL);
4096 
4097 		if (!xlt1->ptypes)
4098 			goto err;
4099 
4100 		xlt1->ptg_tbl = devm_kcalloc(ice_hw_to_dev(hw), ICE_MAX_PTGS,
4101 					     sizeof(*xlt1->ptg_tbl),
4102 					     GFP_KERNEL);
4103 
4104 		if (!xlt1->ptg_tbl)
4105 			goto err;
4106 
4107 		xlt1->t = devm_kcalloc(ice_hw_to_dev(hw), xlt1->count,
4108 				       sizeof(*xlt1->t), GFP_KERNEL);
4109 		if (!xlt1->t)
4110 			goto err;
4111 
4112 		xlt2->sid = ice_blk_sids[i][ICE_SID_XLT2_OFF];
4113 		xlt2->count = blk_sizes[i].xlt2;
4114 
4115 		xlt2->vsis = devm_kcalloc(ice_hw_to_dev(hw), xlt2->count,
4116 					  sizeof(*xlt2->vsis), GFP_KERNEL);
4117 
4118 		if (!xlt2->vsis)
4119 			goto err;
4120 
4121 		xlt2->vsig_tbl = devm_kcalloc(ice_hw_to_dev(hw), xlt2->count,
4122 					      sizeof(*xlt2->vsig_tbl),
4123 					      GFP_KERNEL);
4124 		if (!xlt2->vsig_tbl)
4125 			goto err;
4126 
4127 		for (j = 0; j < xlt2->count; j++)
4128 			INIT_LIST_HEAD(&xlt2->vsig_tbl[j].prop_lst);
4129 
4130 		xlt2->t = devm_kcalloc(ice_hw_to_dev(hw), xlt2->count,
4131 				       sizeof(*xlt2->t), GFP_KERNEL);
4132 		if (!xlt2->t)
4133 			goto err;
4134 
4135 		prof->sid = ice_blk_sids[i][ICE_SID_PR_OFF];
4136 		prof->count = blk_sizes[i].prof_tcam;
4137 		prof->max_prof_id = blk_sizes[i].prof_id;
4138 		prof->cdid_bits = blk_sizes[i].prof_cdid_bits;
4139 		prof->t = devm_kcalloc(ice_hw_to_dev(hw), prof->count,
4140 				       sizeof(*prof->t), GFP_KERNEL);
4141 
4142 		if (!prof->t)
4143 			goto err;
4144 
4145 		prof_redir->sid = ice_blk_sids[i][ICE_SID_PR_REDIR_OFF];
4146 		prof_redir->count = blk_sizes[i].prof_redir;
4147 		prof_redir->t = devm_kcalloc(ice_hw_to_dev(hw),
4148 					     prof_redir->count,
4149 					     sizeof(*prof_redir->t),
4150 					     GFP_KERNEL);
4151 
4152 		if (!prof_redir->t)
4153 			goto err;
4154 
4155 		es->sid = ice_blk_sids[i][ICE_SID_ES_OFF];
4156 		es->count = blk_sizes[i].es;
4157 		es->fvw = blk_sizes[i].fvw;
4158 		es->t = devm_kcalloc(ice_hw_to_dev(hw),
4159 				     (u32)(es->count * es->fvw),
4160 				     sizeof(*es->t), GFP_KERNEL);
4161 		if (!es->t)
4162 			goto err;
4163 
4164 		es->ref_count = devm_kcalloc(ice_hw_to_dev(hw), es->count,
4165 					     sizeof(*es->ref_count),
4166 					     GFP_KERNEL);
4167 		if (!es->ref_count)
4168 			goto err;
4169 
4170 		es->written = devm_kcalloc(ice_hw_to_dev(hw), es->count,
4171 					   sizeof(*es->written), GFP_KERNEL);
4172 		if (!es->written)
4173 			goto err;
4174 
4175 		es->mask_ena = devm_kcalloc(ice_hw_to_dev(hw), es->count,
4176 					    sizeof(*es->mask_ena), GFP_KERNEL);
4177 		if (!es->mask_ena)
4178 			goto err;
4179 	}
4180 	return 0;
4181 
4182 err:
4183 	ice_free_hw_tbls(hw);
4184 	return -ENOMEM;
4185 }
4186 
4187 /**
4188  * ice_prof_gen_key - generate profile ID key
4189  * @hw: pointer to the HW struct
4190  * @blk: the block in which to write profile ID to
4191  * @ptg: packet type group (PTG) portion of key
4192  * @vsig: VSIG portion of key
4193  * @cdid: CDID portion of key
4194  * @flags: flag portion of key
4195  * @vl_msk: valid mask
4196  * @dc_msk: don't care mask
4197  * @nm_msk: never match mask
4198  * @key: output of profile ID key
4199  */
4200 static int
4201 ice_prof_gen_key(struct ice_hw *hw, enum ice_block blk, u8 ptg, u16 vsig,
4202 		 u8 cdid, u16 flags, u8 vl_msk[ICE_TCAM_KEY_VAL_SZ],
4203 		 u8 dc_msk[ICE_TCAM_KEY_VAL_SZ], u8 nm_msk[ICE_TCAM_KEY_VAL_SZ],
4204 		 u8 key[ICE_TCAM_KEY_SZ])
4205 {
4206 	struct ice_prof_id_key inkey;
4207 
4208 	inkey.xlt1 = ptg;
4209 	inkey.xlt2_cdid = cpu_to_le16(vsig);
4210 	inkey.flags = cpu_to_le16(flags);
4211 
4212 	switch (hw->blk[blk].prof.cdid_bits) {
4213 	case 0:
4214 		break;
4215 	case 2:
4216 #define ICE_CD_2_M 0xC000U
4217 #define ICE_CD_2_S 14
4218 		inkey.xlt2_cdid &= ~cpu_to_le16(ICE_CD_2_M);
4219 		inkey.xlt2_cdid |= cpu_to_le16(BIT(cdid) << ICE_CD_2_S);
4220 		break;
4221 	case 4:
4222 #define ICE_CD_4_M 0xF000U
4223 #define ICE_CD_4_S 12
4224 		inkey.xlt2_cdid &= ~cpu_to_le16(ICE_CD_4_M);
4225 		inkey.xlt2_cdid |= cpu_to_le16(BIT(cdid) << ICE_CD_4_S);
4226 		break;
4227 	case 8:
4228 #define ICE_CD_8_M 0xFF00U
4229 #define ICE_CD_8_S 16
4230 		inkey.xlt2_cdid &= ~cpu_to_le16(ICE_CD_8_M);
4231 		inkey.xlt2_cdid |= cpu_to_le16(BIT(cdid) << ICE_CD_8_S);
4232 		break;
4233 	default:
4234 		ice_debug(hw, ICE_DBG_PKG, "Error in profile config\n");
4235 		break;
4236 	}
4237 
4238 	return ice_set_key(key, ICE_TCAM_KEY_SZ, (u8 *)&inkey, vl_msk, dc_msk,
4239 			   nm_msk, 0, ICE_TCAM_KEY_SZ / 2);
4240 }
4241 
4242 /**
4243  * ice_tcam_write_entry - write TCAM entry
4244  * @hw: pointer to the HW struct
4245  * @blk: the block in which to write profile ID to
4246  * @idx: the entry index to write to
4247  * @prof_id: profile ID
4248  * @ptg: packet type group (PTG) portion of key
4249  * @vsig: VSIG portion of key
4250  * @cdid: CDID portion of key
4251  * @flags: flag portion of key
4252  * @vl_msk: valid mask
4253  * @dc_msk: don't care mask
4254  * @nm_msk: never match mask
4255  */
4256 static int
4257 ice_tcam_write_entry(struct ice_hw *hw, enum ice_block blk, u16 idx,
4258 		     u8 prof_id, u8 ptg, u16 vsig, u8 cdid, u16 flags,
4259 		     u8 vl_msk[ICE_TCAM_KEY_VAL_SZ],
4260 		     u8 dc_msk[ICE_TCAM_KEY_VAL_SZ],
4261 		     u8 nm_msk[ICE_TCAM_KEY_VAL_SZ])
4262 {
4263 	struct ice_prof_tcam_entry;
4264 	int status;
4265 
4266 	status = ice_prof_gen_key(hw, blk, ptg, vsig, cdid, flags, vl_msk,
4267 				  dc_msk, nm_msk, hw->blk[blk].prof.t[idx].key);
4268 	if (!status) {
4269 		hw->blk[blk].prof.t[idx].addr = cpu_to_le16(idx);
4270 		hw->blk[blk].prof.t[idx].prof_id = prof_id;
4271 	}
4272 
4273 	return status;
4274 }
4275 
4276 /**
4277  * ice_vsig_get_ref - returns number of VSIs belong to a VSIG
4278  * @hw: pointer to the hardware structure
4279  * @blk: HW block
4280  * @vsig: VSIG to query
4281  * @refs: pointer to variable to receive the reference count
4282  */
4283 static int
4284 ice_vsig_get_ref(struct ice_hw *hw, enum ice_block blk, u16 vsig, u16 *refs)
4285 {
4286 	u16 idx = vsig & ICE_VSIG_IDX_M;
4287 	struct ice_vsig_vsi *ptr;
4288 
4289 	*refs = 0;
4290 
4291 	if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use)
4292 		return -ENOENT;
4293 
4294 	ptr = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
4295 	while (ptr) {
4296 		(*refs)++;
4297 		ptr = ptr->next_vsi;
4298 	}
4299 
4300 	return 0;
4301 }
4302 
4303 /**
4304  * ice_has_prof_vsig - check to see if VSIG has a specific profile
4305  * @hw: pointer to the hardware structure
4306  * @blk: HW block
4307  * @vsig: VSIG to check against
4308  * @hdl: profile handle
4309  */
4310 static bool
4311 ice_has_prof_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig, u64 hdl)
4312 {
4313 	u16 idx = vsig & ICE_VSIG_IDX_M;
4314 	struct ice_vsig_prof *ent;
4315 
4316 	list_for_each_entry(ent, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
4317 			    list)
4318 		if (ent->profile_cookie == hdl)
4319 			return true;
4320 
4321 	ice_debug(hw, ICE_DBG_INIT, "Characteristic list for VSI group %d not found.\n",
4322 		  vsig);
4323 	return false;
4324 }
4325 
4326 /**
4327  * ice_prof_bld_es - build profile ID extraction sequence changes
4328  * @hw: pointer to the HW struct
4329  * @blk: hardware block
4330  * @bld: the update package buffer build to add to
4331  * @chgs: the list of changes to make in hardware
4332  */
4333 static int
4334 ice_prof_bld_es(struct ice_hw *hw, enum ice_block blk,
4335 		struct ice_buf_build *bld, struct list_head *chgs)
4336 {
4337 	u16 vec_size = hw->blk[blk].es.fvw * sizeof(struct ice_fv_word);
4338 	struct ice_chs_chg *tmp;
4339 
4340 	list_for_each_entry(tmp, chgs, list_entry)
4341 		if (tmp->type == ICE_PTG_ES_ADD && tmp->add_prof) {
4342 			u16 off = tmp->prof_id * hw->blk[blk].es.fvw;
4343 			struct ice_pkg_es *p;
4344 			u32 id;
4345 
4346 			id = ice_sect_id(blk, ICE_VEC_TBL);
4347 			p = ice_pkg_buf_alloc_section(bld, id,
4348 						      struct_size(p, es, 1) +
4349 						      vec_size -
4350 						      sizeof(p->es[0]));
4351 
4352 			if (!p)
4353 				return -ENOSPC;
4354 
4355 			p->count = cpu_to_le16(1);
4356 			p->offset = cpu_to_le16(tmp->prof_id);
4357 
4358 			memcpy(p->es, &hw->blk[blk].es.t[off], vec_size);
4359 		}
4360 
4361 	return 0;
4362 }
4363 
4364 /**
4365  * ice_prof_bld_tcam - build profile ID TCAM changes
4366  * @hw: pointer to the HW struct
4367  * @blk: hardware block
4368  * @bld: the update package buffer build to add to
4369  * @chgs: the list of changes to make in hardware
4370  */
4371 static int
4372 ice_prof_bld_tcam(struct ice_hw *hw, enum ice_block blk,
4373 		  struct ice_buf_build *bld, struct list_head *chgs)
4374 {
4375 	struct ice_chs_chg *tmp;
4376 
4377 	list_for_each_entry(tmp, chgs, list_entry)
4378 		if (tmp->type == ICE_TCAM_ADD && tmp->add_tcam_idx) {
4379 			struct ice_prof_id_section *p;
4380 			u32 id;
4381 
4382 			id = ice_sect_id(blk, ICE_PROF_TCAM);
4383 			p = ice_pkg_buf_alloc_section(bld, id,
4384 						      struct_size(p, entry, 1));
4385 
4386 			if (!p)
4387 				return -ENOSPC;
4388 
4389 			p->count = cpu_to_le16(1);
4390 			p->entry[0].addr = cpu_to_le16(tmp->tcam_idx);
4391 			p->entry[0].prof_id = tmp->prof_id;
4392 
4393 			memcpy(p->entry[0].key,
4394 			       &hw->blk[blk].prof.t[tmp->tcam_idx].key,
4395 			       sizeof(hw->blk[blk].prof.t->key));
4396 		}
4397 
4398 	return 0;
4399 }
4400 
4401 /**
4402  * ice_prof_bld_xlt1 - build XLT1 changes
4403  * @blk: hardware block
4404  * @bld: the update package buffer build to add to
4405  * @chgs: the list of changes to make in hardware
4406  */
4407 static int
4408 ice_prof_bld_xlt1(enum ice_block blk, struct ice_buf_build *bld,
4409 		  struct list_head *chgs)
4410 {
4411 	struct ice_chs_chg *tmp;
4412 
4413 	list_for_each_entry(tmp, chgs, list_entry)
4414 		if (tmp->type == ICE_PTG_ES_ADD && tmp->add_ptg) {
4415 			struct ice_xlt1_section *p;
4416 			u32 id;
4417 
4418 			id = ice_sect_id(blk, ICE_XLT1);
4419 			p = ice_pkg_buf_alloc_section(bld, id,
4420 						      struct_size(p, value, 1));
4421 
4422 			if (!p)
4423 				return -ENOSPC;
4424 
4425 			p->count = cpu_to_le16(1);
4426 			p->offset = cpu_to_le16(tmp->ptype);
4427 			p->value[0] = tmp->ptg;
4428 		}
4429 
4430 	return 0;
4431 }
4432 
4433 /**
4434  * ice_prof_bld_xlt2 - build XLT2 changes
4435  * @blk: hardware block
4436  * @bld: the update package buffer build to add to
4437  * @chgs: the list of changes to make in hardware
4438  */
4439 static int
4440 ice_prof_bld_xlt2(enum ice_block blk, struct ice_buf_build *bld,
4441 		  struct list_head *chgs)
4442 {
4443 	struct ice_chs_chg *tmp;
4444 
4445 	list_for_each_entry(tmp, chgs, list_entry) {
4446 		struct ice_xlt2_section *p;
4447 		u32 id;
4448 
4449 		switch (tmp->type) {
4450 		case ICE_VSIG_ADD:
4451 		case ICE_VSI_MOVE:
4452 		case ICE_VSIG_REM:
4453 			id = ice_sect_id(blk, ICE_XLT2);
4454 			p = ice_pkg_buf_alloc_section(bld, id,
4455 						      struct_size(p, value, 1));
4456 
4457 			if (!p)
4458 				return -ENOSPC;
4459 
4460 			p->count = cpu_to_le16(1);
4461 			p->offset = cpu_to_le16(tmp->vsi);
4462 			p->value[0] = cpu_to_le16(tmp->vsig);
4463 			break;
4464 		default:
4465 			break;
4466 		}
4467 	}
4468 
4469 	return 0;
4470 }
4471 
4472 /**
4473  * ice_upd_prof_hw - update hardware using the change list
4474  * @hw: pointer to the HW struct
4475  * @blk: hardware block
4476  * @chgs: the list of changes to make in hardware
4477  */
4478 static int
4479 ice_upd_prof_hw(struct ice_hw *hw, enum ice_block blk,
4480 		struct list_head *chgs)
4481 {
4482 	struct ice_buf_build *b;
4483 	struct ice_chs_chg *tmp;
4484 	u16 pkg_sects;
4485 	u16 xlt1 = 0;
4486 	u16 xlt2 = 0;
4487 	u16 tcam = 0;
4488 	u16 es = 0;
4489 	int status;
4490 	u16 sects;
4491 
4492 	/* count number of sections we need */
4493 	list_for_each_entry(tmp, chgs, list_entry) {
4494 		switch (tmp->type) {
4495 		case ICE_PTG_ES_ADD:
4496 			if (tmp->add_ptg)
4497 				xlt1++;
4498 			if (tmp->add_prof)
4499 				es++;
4500 			break;
4501 		case ICE_TCAM_ADD:
4502 			tcam++;
4503 			break;
4504 		case ICE_VSIG_ADD:
4505 		case ICE_VSI_MOVE:
4506 		case ICE_VSIG_REM:
4507 			xlt2++;
4508 			break;
4509 		default:
4510 			break;
4511 		}
4512 	}
4513 	sects = xlt1 + xlt2 + tcam + es;
4514 
4515 	if (!sects)
4516 		return 0;
4517 
4518 	/* Build update package buffer */
4519 	b = ice_pkg_buf_alloc(hw);
4520 	if (!b)
4521 		return -ENOMEM;
4522 
4523 	status = ice_pkg_buf_reserve_section(b, sects);
4524 	if (status)
4525 		goto error_tmp;
4526 
4527 	/* Preserve order of table update: ES, TCAM, PTG, VSIG */
4528 	if (es) {
4529 		status = ice_prof_bld_es(hw, blk, b, chgs);
4530 		if (status)
4531 			goto error_tmp;
4532 	}
4533 
4534 	if (tcam) {
4535 		status = ice_prof_bld_tcam(hw, blk, b, chgs);
4536 		if (status)
4537 			goto error_tmp;
4538 	}
4539 
4540 	if (xlt1) {
4541 		status = ice_prof_bld_xlt1(blk, b, chgs);
4542 		if (status)
4543 			goto error_tmp;
4544 	}
4545 
4546 	if (xlt2) {
4547 		status = ice_prof_bld_xlt2(blk, b, chgs);
4548 		if (status)
4549 			goto error_tmp;
4550 	}
4551 
4552 	/* After package buffer build check if the section count in buffer is
4553 	 * non-zero and matches the number of sections detected for package
4554 	 * update.
4555 	 */
4556 	pkg_sects = ice_pkg_buf_get_active_sections(b);
4557 	if (!pkg_sects || pkg_sects != sects) {
4558 		status = -EINVAL;
4559 		goto error_tmp;
4560 	}
4561 
4562 	/* update package */
4563 	status = ice_update_pkg(hw, ice_pkg_buf(b), 1);
4564 	if (status == -EIO)
4565 		ice_debug(hw, ICE_DBG_INIT, "Unable to update HW profile\n");
4566 
4567 error_tmp:
4568 	ice_pkg_buf_free(hw, b);
4569 	return status;
4570 }
4571 
4572 /**
4573  * ice_update_fd_mask - set Flow Director Field Vector mask for a profile
4574  * @hw: pointer to the HW struct
4575  * @prof_id: profile ID
4576  * @mask_sel: mask select
4577  *
4578  * This function enable any of the masks selected by the mask select parameter
4579  * for the profile specified.
4580  */
4581 static void ice_update_fd_mask(struct ice_hw *hw, u16 prof_id, u32 mask_sel)
4582 {
4583 	wr32(hw, GLQF_FDMASK_SEL(prof_id), mask_sel);
4584 
4585 	ice_debug(hw, ICE_DBG_INIT, "fd mask(%d): %x = %x\n", prof_id,
4586 		  GLQF_FDMASK_SEL(prof_id), mask_sel);
4587 }
4588 
4589 struct ice_fd_src_dst_pair {
4590 	u8 prot_id;
4591 	u8 count;
4592 	u16 off;
4593 };
4594 
4595 static const struct ice_fd_src_dst_pair ice_fd_pairs[] = {
4596 	/* These are defined in pairs */
4597 	{ ICE_PROT_IPV4_OF_OR_S, 2, 12 },
4598 	{ ICE_PROT_IPV4_OF_OR_S, 2, 16 },
4599 
4600 	{ ICE_PROT_IPV4_IL, 2, 12 },
4601 	{ ICE_PROT_IPV4_IL, 2, 16 },
4602 
4603 	{ ICE_PROT_IPV6_OF_OR_S, 8, 8 },
4604 	{ ICE_PROT_IPV6_OF_OR_S, 8, 24 },
4605 
4606 	{ ICE_PROT_IPV6_IL, 8, 8 },
4607 	{ ICE_PROT_IPV6_IL, 8, 24 },
4608 
4609 	{ ICE_PROT_TCP_IL, 1, 0 },
4610 	{ ICE_PROT_TCP_IL, 1, 2 },
4611 
4612 	{ ICE_PROT_UDP_OF, 1, 0 },
4613 	{ ICE_PROT_UDP_OF, 1, 2 },
4614 
4615 	{ ICE_PROT_UDP_IL_OR_S, 1, 0 },
4616 	{ ICE_PROT_UDP_IL_OR_S, 1, 2 },
4617 
4618 	{ ICE_PROT_SCTP_IL, 1, 0 },
4619 	{ ICE_PROT_SCTP_IL, 1, 2 }
4620 };
4621 
4622 #define ICE_FD_SRC_DST_PAIR_COUNT	ARRAY_SIZE(ice_fd_pairs)
4623 
4624 /**
4625  * ice_update_fd_swap - set register appropriately for a FD FV extraction
4626  * @hw: pointer to the HW struct
4627  * @prof_id: profile ID
4628  * @es: extraction sequence (length of array is determined by the block)
4629  */
4630 static int
4631 ice_update_fd_swap(struct ice_hw *hw, u16 prof_id, struct ice_fv_word *es)
4632 {
4633 	DECLARE_BITMAP(pair_list, ICE_FD_SRC_DST_PAIR_COUNT);
4634 	u8 pair_start[ICE_FD_SRC_DST_PAIR_COUNT] = { 0 };
4635 #define ICE_FD_FV_NOT_FOUND (-2)
4636 	s8 first_free = ICE_FD_FV_NOT_FOUND;
4637 	u8 used[ICE_MAX_FV_WORDS] = { 0 };
4638 	s8 orig_free, si;
4639 	u32 mask_sel = 0;
4640 	u8 i, j, k;
4641 
4642 	bitmap_zero(pair_list, ICE_FD_SRC_DST_PAIR_COUNT);
4643 
4644 	/* This code assumes that the Flow Director field vectors are assigned
4645 	 * from the end of the FV indexes working towards the zero index, that
4646 	 * only complete fields will be included and will be consecutive, and
4647 	 * that there are no gaps between valid indexes.
4648 	 */
4649 
4650 	/* Determine swap fields present */
4651 	for (i = 0; i < hw->blk[ICE_BLK_FD].es.fvw; i++) {
4652 		/* Find the first free entry, assuming right to left population.
4653 		 * This is where we can start adding additional pairs if needed.
4654 		 */
4655 		if (first_free == ICE_FD_FV_NOT_FOUND && es[i].prot_id !=
4656 		    ICE_PROT_INVALID)
4657 			first_free = i - 1;
4658 
4659 		for (j = 0; j < ICE_FD_SRC_DST_PAIR_COUNT; j++)
4660 			if (es[i].prot_id == ice_fd_pairs[j].prot_id &&
4661 			    es[i].off == ice_fd_pairs[j].off) {
4662 				__set_bit(j, pair_list);
4663 				pair_start[j] = i;
4664 			}
4665 	}
4666 
4667 	orig_free = first_free;
4668 
4669 	/* determine missing swap fields that need to be added */
4670 	for (i = 0; i < ICE_FD_SRC_DST_PAIR_COUNT; i += 2) {
4671 		u8 bit1 = test_bit(i + 1, pair_list);
4672 		u8 bit0 = test_bit(i, pair_list);
4673 
4674 		if (bit0 ^ bit1) {
4675 			u8 index;
4676 
4677 			/* add the appropriate 'paired' entry */
4678 			if (!bit0)
4679 				index = i;
4680 			else
4681 				index = i + 1;
4682 
4683 			/* check for room */
4684 			if (first_free + 1 < (s8)ice_fd_pairs[index].count)
4685 				return -ENOSPC;
4686 
4687 			/* place in extraction sequence */
4688 			for (k = 0; k < ice_fd_pairs[index].count; k++) {
4689 				es[first_free - k].prot_id =
4690 					ice_fd_pairs[index].prot_id;
4691 				es[first_free - k].off =
4692 					ice_fd_pairs[index].off + (k * 2);
4693 
4694 				if (k > first_free)
4695 					return -EIO;
4696 
4697 				/* keep track of non-relevant fields */
4698 				mask_sel |= BIT(first_free - k);
4699 			}
4700 
4701 			pair_start[index] = first_free;
4702 			first_free -= ice_fd_pairs[index].count;
4703 		}
4704 	}
4705 
4706 	/* fill in the swap array */
4707 	si = hw->blk[ICE_BLK_FD].es.fvw - 1;
4708 	while (si >= 0) {
4709 		u8 indexes_used = 1;
4710 
4711 		/* assume flat at this index */
4712 #define ICE_SWAP_VALID	0x80
4713 		used[si] = si | ICE_SWAP_VALID;
4714 
4715 		if (orig_free == ICE_FD_FV_NOT_FOUND || si <= orig_free) {
4716 			si -= indexes_used;
4717 			continue;
4718 		}
4719 
4720 		/* check for a swap location */
4721 		for (j = 0; j < ICE_FD_SRC_DST_PAIR_COUNT; j++)
4722 			if (es[si].prot_id == ice_fd_pairs[j].prot_id &&
4723 			    es[si].off == ice_fd_pairs[j].off) {
4724 				u8 idx;
4725 
4726 				/* determine the appropriate matching field */
4727 				idx = j + ((j % 2) ? -1 : 1);
4728 
4729 				indexes_used = ice_fd_pairs[idx].count;
4730 				for (k = 0; k < indexes_used; k++) {
4731 					used[si - k] = (pair_start[idx] - k) |
4732 						ICE_SWAP_VALID;
4733 				}
4734 
4735 				break;
4736 			}
4737 
4738 		si -= indexes_used;
4739 	}
4740 
4741 	/* for each set of 4 swap and 4 inset indexes, write the appropriate
4742 	 * register
4743 	 */
4744 	for (j = 0; j < hw->blk[ICE_BLK_FD].es.fvw / 4; j++) {
4745 		u32 raw_swap = 0;
4746 		u32 raw_in = 0;
4747 
4748 		for (k = 0; k < 4; k++) {
4749 			u8 idx;
4750 
4751 			idx = (j * 4) + k;
4752 			if (used[idx] && !(mask_sel & BIT(idx))) {
4753 				raw_swap |= used[idx] << (k * BITS_PER_BYTE);
4754 #define ICE_INSET_DFLT 0x9f
4755 				raw_in |= ICE_INSET_DFLT << (k * BITS_PER_BYTE);
4756 			}
4757 		}
4758 
4759 		/* write the appropriate swap register set */
4760 		wr32(hw, GLQF_FDSWAP(prof_id, j), raw_swap);
4761 
4762 		ice_debug(hw, ICE_DBG_INIT, "swap wr(%d, %d): %x = %08x\n",
4763 			  prof_id, j, GLQF_FDSWAP(prof_id, j), raw_swap);
4764 
4765 		/* write the appropriate inset register set */
4766 		wr32(hw, GLQF_FDINSET(prof_id, j), raw_in);
4767 
4768 		ice_debug(hw, ICE_DBG_INIT, "inset wr(%d, %d): %x = %08x\n",
4769 			  prof_id, j, GLQF_FDINSET(prof_id, j), raw_in);
4770 	}
4771 
4772 	/* initially clear the mask select for this profile */
4773 	ice_update_fd_mask(hw, prof_id, 0);
4774 
4775 	return 0;
4776 }
4777 
4778 /* The entries here needs to match the order of enum ice_ptype_attrib */
4779 static const struct ice_ptype_attrib_info ice_ptype_attributes[] = {
4780 	{ ICE_GTP_PDU_EH,	ICE_GTP_PDU_FLAG_MASK },
4781 	{ ICE_GTP_SESSION,	ICE_GTP_FLAGS_MASK },
4782 	{ ICE_GTP_DOWNLINK,	ICE_GTP_FLAGS_MASK },
4783 	{ ICE_GTP_UPLINK,	ICE_GTP_FLAGS_MASK },
4784 };
4785 
4786 /**
4787  * ice_get_ptype_attrib_info - get PTYPE attribute information
4788  * @type: attribute type
4789  * @info: pointer to variable to the attribute information
4790  */
4791 static void
4792 ice_get_ptype_attrib_info(enum ice_ptype_attrib_type type,
4793 			  struct ice_ptype_attrib_info *info)
4794 {
4795 	*info = ice_ptype_attributes[type];
4796 }
4797 
4798 /**
4799  * ice_add_prof_attrib - add any PTG with attributes to profile
4800  * @prof: pointer to the profile to which PTG entries will be added
4801  * @ptg: PTG to be added
4802  * @ptype: PTYPE that needs to be looked up
4803  * @attr: array of attributes that will be considered
4804  * @attr_cnt: number of elements in the attribute array
4805  */
4806 static int
4807 ice_add_prof_attrib(struct ice_prof_map *prof, u8 ptg, u16 ptype,
4808 		    const struct ice_ptype_attributes *attr, u16 attr_cnt)
4809 {
4810 	bool found = false;
4811 	u16 i;
4812 
4813 	for (i = 0; i < attr_cnt; i++)
4814 		if (attr[i].ptype == ptype) {
4815 			found = true;
4816 
4817 			prof->ptg[prof->ptg_cnt] = ptg;
4818 			ice_get_ptype_attrib_info(attr[i].attrib,
4819 						  &prof->attr[prof->ptg_cnt]);
4820 
4821 			if (++prof->ptg_cnt >= ICE_MAX_PTG_PER_PROFILE)
4822 				return -ENOSPC;
4823 		}
4824 
4825 	if (!found)
4826 		return -ENOENT;
4827 
4828 	return 0;
4829 }
4830 
4831 /**
4832  * ice_add_prof - add profile
4833  * @hw: pointer to the HW struct
4834  * @blk: hardware block
4835  * @id: profile tracking ID
4836  * @ptypes: array of bitmaps indicating ptypes (ICE_FLOW_PTYPE_MAX bits)
4837  * @attr: array of attributes
4838  * @attr_cnt: number of elements in attr array
4839  * @es: extraction sequence (length of array is determined by the block)
4840  * @masks: mask for extraction sequence
4841  *
4842  * This function registers a profile, which matches a set of PTYPES with a
4843  * particular extraction sequence. While the hardware profile is allocated
4844  * it will not be written until the first call to ice_add_flow that specifies
4845  * the ID value used here.
4846  */
4847 int
4848 ice_add_prof(struct ice_hw *hw, enum ice_block blk, u64 id, u8 ptypes[],
4849 	     const struct ice_ptype_attributes *attr, u16 attr_cnt,
4850 	     struct ice_fv_word *es, u16 *masks)
4851 {
4852 	u32 bytes = DIV_ROUND_UP(ICE_FLOW_PTYPE_MAX, BITS_PER_BYTE);
4853 	DECLARE_BITMAP(ptgs_used, ICE_XLT1_CNT);
4854 	struct ice_prof_map *prof;
4855 	u8 byte = 0;
4856 	u8 prof_id;
4857 	int status;
4858 
4859 	bitmap_zero(ptgs_used, ICE_XLT1_CNT);
4860 
4861 	mutex_lock(&hw->blk[blk].es.prof_map_lock);
4862 
4863 	/* search for existing profile */
4864 	status = ice_find_prof_id_with_mask(hw, blk, es, masks, &prof_id);
4865 	if (status) {
4866 		/* allocate profile ID */
4867 		status = ice_alloc_prof_id(hw, blk, &prof_id);
4868 		if (status)
4869 			goto err_ice_add_prof;
4870 		if (blk == ICE_BLK_FD) {
4871 			/* For Flow Director block, the extraction sequence may
4872 			 * need to be altered in the case where there are paired
4873 			 * fields that have no match. This is necessary because
4874 			 * for Flow Director, src and dest fields need to paired
4875 			 * for filter programming and these values are swapped
4876 			 * during Tx.
4877 			 */
4878 			status = ice_update_fd_swap(hw, prof_id, es);
4879 			if (status)
4880 				goto err_ice_add_prof;
4881 		}
4882 		status = ice_update_prof_masking(hw, blk, prof_id, masks);
4883 		if (status)
4884 			goto err_ice_add_prof;
4885 
4886 		/* and write new es */
4887 		ice_write_es(hw, blk, prof_id, es);
4888 	}
4889 
4890 	ice_prof_inc_ref(hw, blk, prof_id);
4891 
4892 	/* add profile info */
4893 	prof = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*prof), GFP_KERNEL);
4894 	if (!prof) {
4895 		status = -ENOMEM;
4896 		goto err_ice_add_prof;
4897 	}
4898 
4899 	prof->profile_cookie = id;
4900 	prof->prof_id = prof_id;
4901 	prof->ptg_cnt = 0;
4902 	prof->context = 0;
4903 
4904 	/* build list of ptgs */
4905 	while (bytes && prof->ptg_cnt < ICE_MAX_PTG_PER_PROFILE) {
4906 		u8 bit;
4907 
4908 		if (!ptypes[byte]) {
4909 			bytes--;
4910 			byte++;
4911 			continue;
4912 		}
4913 
4914 		/* Examine 8 bits per byte */
4915 		for_each_set_bit(bit, (unsigned long *)&ptypes[byte],
4916 				 BITS_PER_BYTE) {
4917 			u16 ptype;
4918 			u8 ptg;
4919 
4920 			ptype = byte * BITS_PER_BYTE + bit;
4921 
4922 			/* The package should place all ptypes in a non-zero
4923 			 * PTG, so the following call should never fail.
4924 			 */
4925 			if (ice_ptg_find_ptype(hw, blk, ptype, &ptg))
4926 				continue;
4927 
4928 			/* If PTG is already added, skip and continue */
4929 			if (test_bit(ptg, ptgs_used))
4930 				continue;
4931 
4932 			__set_bit(ptg, ptgs_used);
4933 			/* Check to see there are any attributes for
4934 			 * this PTYPE, and add them if found.
4935 			 */
4936 			status = ice_add_prof_attrib(prof, ptg, ptype,
4937 						     attr, attr_cnt);
4938 			if (status == -ENOSPC)
4939 				break;
4940 			if (status) {
4941 				/* This is simple a PTYPE/PTG with no
4942 				 * attribute
4943 				 */
4944 				prof->ptg[prof->ptg_cnt] = ptg;
4945 				prof->attr[prof->ptg_cnt].flags = 0;
4946 				prof->attr[prof->ptg_cnt].mask = 0;
4947 
4948 				if (++prof->ptg_cnt >=
4949 				    ICE_MAX_PTG_PER_PROFILE)
4950 					break;
4951 			}
4952 		}
4953 
4954 		bytes--;
4955 		byte++;
4956 	}
4957 
4958 	list_add(&prof->list, &hw->blk[blk].es.prof_map);
4959 	status = 0;
4960 
4961 err_ice_add_prof:
4962 	mutex_unlock(&hw->blk[blk].es.prof_map_lock);
4963 	return status;
4964 }
4965 
4966 /**
4967  * ice_search_prof_id - Search for a profile tracking ID
4968  * @hw: pointer to the HW struct
4969  * @blk: hardware block
4970  * @id: profile tracking ID
4971  *
4972  * This will search for a profile tracking ID which was previously added.
4973  * The profile map lock should be held before calling this function.
4974  */
4975 static struct ice_prof_map *
4976 ice_search_prof_id(struct ice_hw *hw, enum ice_block blk, u64 id)
4977 {
4978 	struct ice_prof_map *entry = NULL;
4979 	struct ice_prof_map *map;
4980 
4981 	list_for_each_entry(map, &hw->blk[blk].es.prof_map, list)
4982 		if (map->profile_cookie == id) {
4983 			entry = map;
4984 			break;
4985 		}
4986 
4987 	return entry;
4988 }
4989 
4990 /**
4991  * ice_vsig_prof_id_count - count profiles in a VSIG
4992  * @hw: pointer to the HW struct
4993  * @blk: hardware block
4994  * @vsig: VSIG to remove the profile from
4995  */
4996 static u16
4997 ice_vsig_prof_id_count(struct ice_hw *hw, enum ice_block blk, u16 vsig)
4998 {
4999 	u16 idx = vsig & ICE_VSIG_IDX_M, count = 0;
5000 	struct ice_vsig_prof *p;
5001 
5002 	list_for_each_entry(p, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
5003 			    list)
5004 		count++;
5005 
5006 	return count;
5007 }
5008 
5009 /**
5010  * ice_rel_tcam_idx - release a TCAM index
5011  * @hw: pointer to the HW struct
5012  * @blk: hardware block
5013  * @idx: the index to release
5014  */
5015 static int ice_rel_tcam_idx(struct ice_hw *hw, enum ice_block blk, u16 idx)
5016 {
5017 	/* Masks to invoke a never match entry */
5018 	u8 vl_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
5019 	u8 dc_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFE, 0xFF, 0xFF, 0xFF, 0xFF };
5020 	u8 nm_msk[ICE_TCAM_KEY_VAL_SZ] = { 0x01, 0x00, 0x00, 0x00, 0x00 };
5021 	int status;
5022 
5023 	/* write the TCAM entry */
5024 	status = ice_tcam_write_entry(hw, blk, idx, 0, 0, 0, 0, 0, vl_msk,
5025 				      dc_msk, nm_msk);
5026 	if (status)
5027 		return status;
5028 
5029 	/* release the TCAM entry */
5030 	status = ice_free_tcam_ent(hw, blk, idx);
5031 
5032 	return status;
5033 }
5034 
5035 /**
5036  * ice_rem_prof_id - remove one profile from a VSIG
5037  * @hw: pointer to the HW struct
5038  * @blk: hardware block
5039  * @prof: pointer to profile structure to remove
5040  */
5041 static int
5042 ice_rem_prof_id(struct ice_hw *hw, enum ice_block blk,
5043 		struct ice_vsig_prof *prof)
5044 {
5045 	int status;
5046 	u16 i;
5047 
5048 	for (i = 0; i < prof->tcam_count; i++)
5049 		if (prof->tcam[i].in_use) {
5050 			prof->tcam[i].in_use = false;
5051 			status = ice_rel_tcam_idx(hw, blk,
5052 						  prof->tcam[i].tcam_idx);
5053 			if (status)
5054 				return -EIO;
5055 		}
5056 
5057 	return 0;
5058 }
5059 
5060 /**
5061  * ice_rem_vsig - remove VSIG
5062  * @hw: pointer to the HW struct
5063  * @blk: hardware block
5064  * @vsig: the VSIG to remove
5065  * @chg: the change list
5066  */
5067 static int
5068 ice_rem_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig,
5069 	     struct list_head *chg)
5070 {
5071 	u16 idx = vsig & ICE_VSIG_IDX_M;
5072 	struct ice_vsig_vsi *vsi_cur;
5073 	struct ice_vsig_prof *d, *t;
5074 	int status;
5075 
5076 	/* remove TCAM entries */
5077 	list_for_each_entry_safe(d, t,
5078 				 &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
5079 				 list) {
5080 		status = ice_rem_prof_id(hw, blk, d);
5081 		if (status)
5082 			return status;
5083 
5084 		list_del(&d->list);
5085 		devm_kfree(ice_hw_to_dev(hw), d);
5086 	}
5087 
5088 	/* Move all VSIS associated with this VSIG to the default VSIG */
5089 	vsi_cur = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
5090 	/* If the VSIG has at least 1 VSI then iterate through the list
5091 	 * and remove the VSIs before deleting the group.
5092 	 */
5093 	if (vsi_cur)
5094 		do {
5095 			struct ice_vsig_vsi *tmp = vsi_cur->next_vsi;
5096 			struct ice_chs_chg *p;
5097 
5098 			p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p),
5099 					 GFP_KERNEL);
5100 			if (!p)
5101 				return -ENOMEM;
5102 
5103 			p->type = ICE_VSIG_REM;
5104 			p->orig_vsig = vsig;
5105 			p->vsig = ICE_DEFAULT_VSIG;
5106 			p->vsi = vsi_cur - hw->blk[blk].xlt2.vsis;
5107 
5108 			list_add(&p->list_entry, chg);
5109 
5110 			vsi_cur = tmp;
5111 		} while (vsi_cur);
5112 
5113 	return ice_vsig_free(hw, blk, vsig);
5114 }
5115 
5116 /**
5117  * ice_rem_prof_id_vsig - remove a specific profile from a VSIG
5118  * @hw: pointer to the HW struct
5119  * @blk: hardware block
5120  * @vsig: VSIG to remove the profile from
5121  * @hdl: profile handle indicating which profile to remove
5122  * @chg: list to receive a record of changes
5123  */
5124 static int
5125 ice_rem_prof_id_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig, u64 hdl,
5126 		     struct list_head *chg)
5127 {
5128 	u16 idx = vsig & ICE_VSIG_IDX_M;
5129 	struct ice_vsig_prof *p, *t;
5130 	int status;
5131 
5132 	list_for_each_entry_safe(p, t,
5133 				 &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
5134 				 list)
5135 		if (p->profile_cookie == hdl) {
5136 			if (ice_vsig_prof_id_count(hw, blk, vsig) == 1)
5137 				/* this is the last profile, remove the VSIG */
5138 				return ice_rem_vsig(hw, blk, vsig, chg);
5139 
5140 			status = ice_rem_prof_id(hw, blk, p);
5141 			if (!status) {
5142 				list_del(&p->list);
5143 				devm_kfree(ice_hw_to_dev(hw), p);
5144 			}
5145 			return status;
5146 		}
5147 
5148 	return -ENOENT;
5149 }
5150 
5151 /**
5152  * ice_rem_flow_all - remove all flows with a particular profile
5153  * @hw: pointer to the HW struct
5154  * @blk: hardware block
5155  * @id: profile tracking ID
5156  */
5157 static int ice_rem_flow_all(struct ice_hw *hw, enum ice_block blk, u64 id)
5158 {
5159 	struct ice_chs_chg *del, *tmp;
5160 	struct list_head chg;
5161 	int status;
5162 	u16 i;
5163 
5164 	INIT_LIST_HEAD(&chg);
5165 
5166 	for (i = 1; i < ICE_MAX_VSIGS; i++)
5167 		if (hw->blk[blk].xlt2.vsig_tbl[i].in_use) {
5168 			if (ice_has_prof_vsig(hw, blk, i, id)) {
5169 				status = ice_rem_prof_id_vsig(hw, blk, i, id,
5170 							      &chg);
5171 				if (status)
5172 					goto err_ice_rem_flow_all;
5173 			}
5174 		}
5175 
5176 	status = ice_upd_prof_hw(hw, blk, &chg);
5177 
5178 err_ice_rem_flow_all:
5179 	list_for_each_entry_safe(del, tmp, &chg, list_entry) {
5180 		list_del(&del->list_entry);
5181 		devm_kfree(ice_hw_to_dev(hw), del);
5182 	}
5183 
5184 	return status;
5185 }
5186 
5187 /**
5188  * ice_rem_prof - remove profile
5189  * @hw: pointer to the HW struct
5190  * @blk: hardware block
5191  * @id: profile tracking ID
5192  *
5193  * This will remove the profile specified by the ID parameter, which was
5194  * previously created through ice_add_prof. If any existing entries
5195  * are associated with this profile, they will be removed as well.
5196  */
5197 int ice_rem_prof(struct ice_hw *hw, enum ice_block blk, u64 id)
5198 {
5199 	struct ice_prof_map *pmap;
5200 	int status;
5201 
5202 	mutex_lock(&hw->blk[blk].es.prof_map_lock);
5203 
5204 	pmap = ice_search_prof_id(hw, blk, id);
5205 	if (!pmap) {
5206 		status = -ENOENT;
5207 		goto err_ice_rem_prof;
5208 	}
5209 
5210 	/* remove all flows with this profile */
5211 	status = ice_rem_flow_all(hw, blk, pmap->profile_cookie);
5212 	if (status)
5213 		goto err_ice_rem_prof;
5214 
5215 	/* dereference profile, and possibly remove */
5216 	ice_prof_dec_ref(hw, blk, pmap->prof_id);
5217 
5218 	list_del(&pmap->list);
5219 	devm_kfree(ice_hw_to_dev(hw), pmap);
5220 
5221 err_ice_rem_prof:
5222 	mutex_unlock(&hw->blk[blk].es.prof_map_lock);
5223 	return status;
5224 }
5225 
5226 /**
5227  * ice_get_prof - get profile
5228  * @hw: pointer to the HW struct
5229  * @blk: hardware block
5230  * @hdl: profile handle
5231  * @chg: change list
5232  */
5233 static int
5234 ice_get_prof(struct ice_hw *hw, enum ice_block blk, u64 hdl,
5235 	     struct list_head *chg)
5236 {
5237 	struct ice_prof_map *map;
5238 	struct ice_chs_chg *p;
5239 	int status = 0;
5240 	u16 i;
5241 
5242 	mutex_lock(&hw->blk[blk].es.prof_map_lock);
5243 	/* Get the details on the profile specified by the handle ID */
5244 	map = ice_search_prof_id(hw, blk, hdl);
5245 	if (!map) {
5246 		status = -ENOENT;
5247 		goto err_ice_get_prof;
5248 	}
5249 
5250 	for (i = 0; i < map->ptg_cnt; i++)
5251 		if (!hw->blk[blk].es.written[map->prof_id]) {
5252 			/* add ES to change list */
5253 			p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p),
5254 					 GFP_KERNEL);
5255 			if (!p) {
5256 				status = -ENOMEM;
5257 				goto err_ice_get_prof;
5258 			}
5259 
5260 			p->type = ICE_PTG_ES_ADD;
5261 			p->ptype = 0;
5262 			p->ptg = map->ptg[i];
5263 			p->add_ptg = 0;
5264 
5265 			p->add_prof = 1;
5266 			p->prof_id = map->prof_id;
5267 
5268 			hw->blk[blk].es.written[map->prof_id] = true;
5269 
5270 			list_add(&p->list_entry, chg);
5271 		}
5272 
5273 err_ice_get_prof:
5274 	mutex_unlock(&hw->blk[blk].es.prof_map_lock);
5275 	/* let caller clean up the change list */
5276 	return status;
5277 }
5278 
5279 /**
5280  * ice_get_profs_vsig - get a copy of the list of profiles from a VSIG
5281  * @hw: pointer to the HW struct
5282  * @blk: hardware block
5283  * @vsig: VSIG from which to copy the list
5284  * @lst: output list
5285  *
5286  * This routine makes a copy of the list of profiles in the specified VSIG.
5287  */
5288 static int
5289 ice_get_profs_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig,
5290 		   struct list_head *lst)
5291 {
5292 	struct ice_vsig_prof *ent1, *ent2;
5293 	u16 idx = vsig & ICE_VSIG_IDX_M;
5294 
5295 	list_for_each_entry(ent1, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
5296 			    list) {
5297 		struct ice_vsig_prof *p;
5298 
5299 		/* copy to the input list */
5300 		p = devm_kmemdup(ice_hw_to_dev(hw), ent1, sizeof(*p),
5301 				 GFP_KERNEL);
5302 		if (!p)
5303 			goto err_ice_get_profs_vsig;
5304 
5305 		list_add_tail(&p->list, lst);
5306 	}
5307 
5308 	return 0;
5309 
5310 err_ice_get_profs_vsig:
5311 	list_for_each_entry_safe(ent1, ent2, lst, list) {
5312 		list_del(&ent1->list);
5313 		devm_kfree(ice_hw_to_dev(hw), ent1);
5314 	}
5315 
5316 	return -ENOMEM;
5317 }
5318 
5319 /**
5320  * ice_add_prof_to_lst - add profile entry to a list
5321  * @hw: pointer to the HW struct
5322  * @blk: hardware block
5323  * @lst: the list to be added to
5324  * @hdl: profile handle of entry to add
5325  */
5326 static int
5327 ice_add_prof_to_lst(struct ice_hw *hw, enum ice_block blk,
5328 		    struct list_head *lst, u64 hdl)
5329 {
5330 	struct ice_prof_map *map;
5331 	struct ice_vsig_prof *p;
5332 	int status = 0;
5333 	u16 i;
5334 
5335 	mutex_lock(&hw->blk[blk].es.prof_map_lock);
5336 	map = ice_search_prof_id(hw, blk, hdl);
5337 	if (!map) {
5338 		status = -ENOENT;
5339 		goto err_ice_add_prof_to_lst;
5340 	}
5341 
5342 	p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p), GFP_KERNEL);
5343 	if (!p) {
5344 		status = -ENOMEM;
5345 		goto err_ice_add_prof_to_lst;
5346 	}
5347 
5348 	p->profile_cookie = map->profile_cookie;
5349 	p->prof_id = map->prof_id;
5350 	p->tcam_count = map->ptg_cnt;
5351 
5352 	for (i = 0; i < map->ptg_cnt; i++) {
5353 		p->tcam[i].prof_id = map->prof_id;
5354 		p->tcam[i].tcam_idx = ICE_INVALID_TCAM;
5355 		p->tcam[i].ptg = map->ptg[i];
5356 	}
5357 
5358 	list_add(&p->list, lst);
5359 
5360 err_ice_add_prof_to_lst:
5361 	mutex_unlock(&hw->blk[blk].es.prof_map_lock);
5362 	return status;
5363 }
5364 
5365 /**
5366  * ice_move_vsi - move VSI to another VSIG
5367  * @hw: pointer to the HW struct
5368  * @blk: hardware block
5369  * @vsi: the VSI to move
5370  * @vsig: the VSIG to move the VSI to
5371  * @chg: the change list
5372  */
5373 static int
5374 ice_move_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 vsig,
5375 	     struct list_head *chg)
5376 {
5377 	struct ice_chs_chg *p;
5378 	u16 orig_vsig;
5379 	int status;
5380 
5381 	p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p), GFP_KERNEL);
5382 	if (!p)
5383 		return -ENOMEM;
5384 
5385 	status = ice_vsig_find_vsi(hw, blk, vsi, &orig_vsig);
5386 	if (!status)
5387 		status = ice_vsig_add_mv_vsi(hw, blk, vsi, vsig);
5388 
5389 	if (status) {
5390 		devm_kfree(ice_hw_to_dev(hw), p);
5391 		return status;
5392 	}
5393 
5394 	p->type = ICE_VSI_MOVE;
5395 	p->vsi = vsi;
5396 	p->orig_vsig = orig_vsig;
5397 	p->vsig = vsig;
5398 
5399 	list_add(&p->list_entry, chg);
5400 
5401 	return 0;
5402 }
5403 
5404 /**
5405  * ice_rem_chg_tcam_ent - remove a specific TCAM entry from change list
5406  * @hw: pointer to the HW struct
5407  * @idx: the index of the TCAM entry to remove
5408  * @chg: the list of change structures to search
5409  */
5410 static void
5411 ice_rem_chg_tcam_ent(struct ice_hw *hw, u16 idx, struct list_head *chg)
5412 {
5413 	struct ice_chs_chg *pos, *tmp;
5414 
5415 	list_for_each_entry_safe(tmp, pos, chg, list_entry)
5416 		if (tmp->type == ICE_TCAM_ADD && tmp->tcam_idx == idx) {
5417 			list_del(&tmp->list_entry);
5418 			devm_kfree(ice_hw_to_dev(hw), tmp);
5419 		}
5420 }
5421 
5422 /**
5423  * ice_prof_tcam_ena_dis - add enable or disable TCAM change
5424  * @hw: pointer to the HW struct
5425  * @blk: hardware block
5426  * @enable: true to enable, false to disable
5427  * @vsig: the VSIG of the TCAM entry
5428  * @tcam: pointer the TCAM info structure of the TCAM to disable
5429  * @chg: the change list
5430  *
5431  * This function appends an enable or disable TCAM entry in the change log
5432  */
5433 static int
5434 ice_prof_tcam_ena_dis(struct ice_hw *hw, enum ice_block blk, bool enable,
5435 		      u16 vsig, struct ice_tcam_inf *tcam,
5436 		      struct list_head *chg)
5437 {
5438 	struct ice_chs_chg *p;
5439 	int status;
5440 
5441 	u8 vl_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
5442 	u8 dc_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0x00, 0x00, 0x00 };
5443 	u8 nm_msk[ICE_TCAM_KEY_VAL_SZ] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
5444 
5445 	/* if disabling, free the TCAM */
5446 	if (!enable) {
5447 		status = ice_rel_tcam_idx(hw, blk, tcam->tcam_idx);
5448 
5449 		/* if we have already created a change for this TCAM entry, then
5450 		 * we need to remove that entry, in order to prevent writing to
5451 		 * a TCAM entry we no longer will have ownership of.
5452 		 */
5453 		ice_rem_chg_tcam_ent(hw, tcam->tcam_idx, chg);
5454 		tcam->tcam_idx = 0;
5455 		tcam->in_use = 0;
5456 		return status;
5457 	}
5458 
5459 	/* for re-enabling, reallocate a TCAM */
5460 	/* for entries with empty attribute masks, allocate entry from
5461 	 * the bottom of the TCAM table; otherwise, allocate from the
5462 	 * top of the table in order to give it higher priority
5463 	 */
5464 	status = ice_alloc_tcam_ent(hw, blk, tcam->attr.mask == 0,
5465 				    &tcam->tcam_idx);
5466 	if (status)
5467 		return status;
5468 
5469 	/* add TCAM to change list */
5470 	p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p), GFP_KERNEL);
5471 	if (!p)
5472 		return -ENOMEM;
5473 
5474 	status = ice_tcam_write_entry(hw, blk, tcam->tcam_idx, tcam->prof_id,
5475 				      tcam->ptg, vsig, 0, tcam->attr.flags,
5476 				      vl_msk, dc_msk, nm_msk);
5477 	if (status)
5478 		goto err_ice_prof_tcam_ena_dis;
5479 
5480 	tcam->in_use = 1;
5481 
5482 	p->type = ICE_TCAM_ADD;
5483 	p->add_tcam_idx = true;
5484 	p->prof_id = tcam->prof_id;
5485 	p->ptg = tcam->ptg;
5486 	p->vsig = 0;
5487 	p->tcam_idx = tcam->tcam_idx;
5488 
5489 	/* log change */
5490 	list_add(&p->list_entry, chg);
5491 
5492 	return 0;
5493 
5494 err_ice_prof_tcam_ena_dis:
5495 	devm_kfree(ice_hw_to_dev(hw), p);
5496 	return status;
5497 }
5498 
5499 /**
5500  * ice_adj_prof_priorities - adjust profile based on priorities
5501  * @hw: pointer to the HW struct
5502  * @blk: hardware block
5503  * @vsig: the VSIG for which to adjust profile priorities
5504  * @chg: the change list
5505  */
5506 static int
5507 ice_adj_prof_priorities(struct ice_hw *hw, enum ice_block blk, u16 vsig,
5508 			struct list_head *chg)
5509 {
5510 	DECLARE_BITMAP(ptgs_used, ICE_XLT1_CNT);
5511 	struct ice_vsig_prof *t;
5512 	int status;
5513 	u16 idx;
5514 
5515 	bitmap_zero(ptgs_used, ICE_XLT1_CNT);
5516 	idx = vsig & ICE_VSIG_IDX_M;
5517 
5518 	/* Priority is based on the order in which the profiles are added. The
5519 	 * newest added profile has highest priority and the oldest added
5520 	 * profile has the lowest priority. Since the profile property list for
5521 	 * a VSIG is sorted from newest to oldest, this code traverses the list
5522 	 * in order and enables the first of each PTG that it finds (that is not
5523 	 * already enabled); it also disables any duplicate PTGs that it finds
5524 	 * in the older profiles (that are currently enabled).
5525 	 */
5526 
5527 	list_for_each_entry(t, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
5528 			    list) {
5529 		u16 i;
5530 
5531 		for (i = 0; i < t->tcam_count; i++) {
5532 			/* Scan the priorities from newest to oldest.
5533 			 * Make sure that the newest profiles take priority.
5534 			 */
5535 			if (test_bit(t->tcam[i].ptg, ptgs_used) &&
5536 			    t->tcam[i].in_use) {
5537 				/* need to mark this PTG as never match, as it
5538 				 * was already in use and therefore duplicate
5539 				 * (and lower priority)
5540 				 */
5541 				status = ice_prof_tcam_ena_dis(hw, blk, false,
5542 							       vsig,
5543 							       &t->tcam[i],
5544 							       chg);
5545 				if (status)
5546 					return status;
5547 			} else if (!test_bit(t->tcam[i].ptg, ptgs_used) &&
5548 				   !t->tcam[i].in_use) {
5549 				/* need to enable this PTG, as it in not in use
5550 				 * and not enabled (highest priority)
5551 				 */
5552 				status = ice_prof_tcam_ena_dis(hw, blk, true,
5553 							       vsig,
5554 							       &t->tcam[i],
5555 							       chg);
5556 				if (status)
5557 					return status;
5558 			}
5559 
5560 			/* keep track of used ptgs */
5561 			__set_bit(t->tcam[i].ptg, ptgs_used);
5562 		}
5563 	}
5564 
5565 	return 0;
5566 }
5567 
5568 /**
5569  * ice_add_prof_id_vsig - add profile to VSIG
5570  * @hw: pointer to the HW struct
5571  * @blk: hardware block
5572  * @vsig: the VSIG to which this profile is to be added
5573  * @hdl: the profile handle indicating the profile to add
5574  * @rev: true to add entries to the end of the list
5575  * @chg: the change list
5576  */
5577 static int
5578 ice_add_prof_id_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig, u64 hdl,
5579 		     bool rev, struct list_head *chg)
5580 {
5581 	/* Masks that ignore flags */
5582 	u8 vl_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
5583 	u8 dc_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0x00, 0x00, 0x00 };
5584 	u8 nm_msk[ICE_TCAM_KEY_VAL_SZ] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
5585 	struct ice_prof_map *map;
5586 	struct ice_vsig_prof *t;
5587 	struct ice_chs_chg *p;
5588 	u16 vsig_idx, i;
5589 	int status = 0;
5590 
5591 	/* Error, if this VSIG already has this profile */
5592 	if (ice_has_prof_vsig(hw, blk, vsig, hdl))
5593 		return -EEXIST;
5594 
5595 	/* new VSIG profile structure */
5596 	t = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*t), GFP_KERNEL);
5597 	if (!t)
5598 		return -ENOMEM;
5599 
5600 	mutex_lock(&hw->blk[blk].es.prof_map_lock);
5601 	/* Get the details on the profile specified by the handle ID */
5602 	map = ice_search_prof_id(hw, blk, hdl);
5603 	if (!map) {
5604 		status = -ENOENT;
5605 		goto err_ice_add_prof_id_vsig;
5606 	}
5607 
5608 	t->profile_cookie = map->profile_cookie;
5609 	t->prof_id = map->prof_id;
5610 	t->tcam_count = map->ptg_cnt;
5611 
5612 	/* create TCAM entries */
5613 	for (i = 0; i < map->ptg_cnt; i++) {
5614 		u16 tcam_idx;
5615 
5616 		/* add TCAM to change list */
5617 		p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p), GFP_KERNEL);
5618 		if (!p) {
5619 			status = -ENOMEM;
5620 			goto err_ice_add_prof_id_vsig;
5621 		}
5622 
5623 		/* allocate the TCAM entry index */
5624 		/* for entries with empty attribute masks, allocate entry from
5625 		 * the bottom of the TCAM table; otherwise, allocate from the
5626 		 * top of the table in order to give it higher priority
5627 		 */
5628 		status = ice_alloc_tcam_ent(hw, blk, map->attr[i].mask == 0,
5629 					    &tcam_idx);
5630 		if (status) {
5631 			devm_kfree(ice_hw_to_dev(hw), p);
5632 			goto err_ice_add_prof_id_vsig;
5633 		}
5634 
5635 		t->tcam[i].ptg = map->ptg[i];
5636 		t->tcam[i].prof_id = map->prof_id;
5637 		t->tcam[i].tcam_idx = tcam_idx;
5638 		t->tcam[i].attr = map->attr[i];
5639 		t->tcam[i].in_use = true;
5640 
5641 		p->type = ICE_TCAM_ADD;
5642 		p->add_tcam_idx = true;
5643 		p->prof_id = t->tcam[i].prof_id;
5644 		p->ptg = t->tcam[i].ptg;
5645 		p->vsig = vsig;
5646 		p->tcam_idx = t->tcam[i].tcam_idx;
5647 
5648 		/* write the TCAM entry */
5649 		status = ice_tcam_write_entry(hw, blk, t->tcam[i].tcam_idx,
5650 					      t->tcam[i].prof_id,
5651 					      t->tcam[i].ptg, vsig, 0, 0,
5652 					      vl_msk, dc_msk, nm_msk);
5653 		if (status) {
5654 			devm_kfree(ice_hw_to_dev(hw), p);
5655 			goto err_ice_add_prof_id_vsig;
5656 		}
5657 
5658 		/* log change */
5659 		list_add(&p->list_entry, chg);
5660 	}
5661 
5662 	/* add profile to VSIG */
5663 	vsig_idx = vsig & ICE_VSIG_IDX_M;
5664 	if (rev)
5665 		list_add_tail(&t->list,
5666 			      &hw->blk[blk].xlt2.vsig_tbl[vsig_idx].prop_lst);
5667 	else
5668 		list_add(&t->list,
5669 			 &hw->blk[blk].xlt2.vsig_tbl[vsig_idx].prop_lst);
5670 
5671 	mutex_unlock(&hw->blk[blk].es.prof_map_lock);
5672 	return status;
5673 
5674 err_ice_add_prof_id_vsig:
5675 	mutex_unlock(&hw->blk[blk].es.prof_map_lock);
5676 	/* let caller clean up the change list */
5677 	devm_kfree(ice_hw_to_dev(hw), t);
5678 	return status;
5679 }
5680 
5681 /**
5682  * ice_create_prof_id_vsig - add a new VSIG with a single profile
5683  * @hw: pointer to the HW struct
5684  * @blk: hardware block
5685  * @vsi: the initial VSI that will be in VSIG
5686  * @hdl: the profile handle of the profile that will be added to the VSIG
5687  * @chg: the change list
5688  */
5689 static int
5690 ice_create_prof_id_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsi, u64 hdl,
5691 			struct list_head *chg)
5692 {
5693 	struct ice_chs_chg *p;
5694 	u16 new_vsig;
5695 	int status;
5696 
5697 	p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p), GFP_KERNEL);
5698 	if (!p)
5699 		return -ENOMEM;
5700 
5701 	new_vsig = ice_vsig_alloc(hw, blk);
5702 	if (!new_vsig) {
5703 		status = -EIO;
5704 		goto err_ice_create_prof_id_vsig;
5705 	}
5706 
5707 	status = ice_move_vsi(hw, blk, vsi, new_vsig, chg);
5708 	if (status)
5709 		goto err_ice_create_prof_id_vsig;
5710 
5711 	status = ice_add_prof_id_vsig(hw, blk, new_vsig, hdl, false, chg);
5712 	if (status)
5713 		goto err_ice_create_prof_id_vsig;
5714 
5715 	p->type = ICE_VSIG_ADD;
5716 	p->vsi = vsi;
5717 	p->orig_vsig = ICE_DEFAULT_VSIG;
5718 	p->vsig = new_vsig;
5719 
5720 	list_add(&p->list_entry, chg);
5721 
5722 	return 0;
5723 
5724 err_ice_create_prof_id_vsig:
5725 	/* let caller clean up the change list */
5726 	devm_kfree(ice_hw_to_dev(hw), p);
5727 	return status;
5728 }
5729 
5730 /**
5731  * ice_create_vsig_from_lst - create a new VSIG with a list of profiles
5732  * @hw: pointer to the HW struct
5733  * @blk: hardware block
5734  * @vsi: the initial VSI that will be in VSIG
5735  * @lst: the list of profile that will be added to the VSIG
5736  * @new_vsig: return of new VSIG
5737  * @chg: the change list
5738  */
5739 static int
5740 ice_create_vsig_from_lst(struct ice_hw *hw, enum ice_block blk, u16 vsi,
5741 			 struct list_head *lst, u16 *new_vsig,
5742 			 struct list_head *chg)
5743 {
5744 	struct ice_vsig_prof *t;
5745 	int status;
5746 	u16 vsig;
5747 
5748 	vsig = ice_vsig_alloc(hw, blk);
5749 	if (!vsig)
5750 		return -EIO;
5751 
5752 	status = ice_move_vsi(hw, blk, vsi, vsig, chg);
5753 	if (status)
5754 		return status;
5755 
5756 	list_for_each_entry(t, lst, list) {
5757 		/* Reverse the order here since we are copying the list */
5758 		status = ice_add_prof_id_vsig(hw, blk, vsig, t->profile_cookie,
5759 					      true, chg);
5760 		if (status)
5761 			return status;
5762 	}
5763 
5764 	*new_vsig = vsig;
5765 
5766 	return 0;
5767 }
5768 
5769 /**
5770  * ice_find_prof_vsig - find a VSIG with a specific profile handle
5771  * @hw: pointer to the HW struct
5772  * @blk: hardware block
5773  * @hdl: the profile handle of the profile to search for
5774  * @vsig: returns the VSIG with the matching profile
5775  */
5776 static bool
5777 ice_find_prof_vsig(struct ice_hw *hw, enum ice_block blk, u64 hdl, u16 *vsig)
5778 {
5779 	struct ice_vsig_prof *t;
5780 	struct list_head lst;
5781 	int status;
5782 
5783 	INIT_LIST_HEAD(&lst);
5784 
5785 	t = kzalloc(sizeof(*t), GFP_KERNEL);
5786 	if (!t)
5787 		return false;
5788 
5789 	t->profile_cookie = hdl;
5790 	list_add(&t->list, &lst);
5791 
5792 	status = ice_find_dup_props_vsig(hw, blk, &lst, vsig);
5793 
5794 	list_del(&t->list);
5795 	kfree(t);
5796 
5797 	return !status;
5798 }
5799 
5800 /**
5801  * ice_add_prof_id_flow - add profile flow
5802  * @hw: pointer to the HW struct
5803  * @blk: hardware block
5804  * @vsi: the VSI to enable with the profile specified by ID
5805  * @hdl: profile handle
5806  *
5807  * Calling this function will update the hardware tables to enable the
5808  * profile indicated by the ID parameter for the VSIs specified in the VSI
5809  * array. Once successfully called, the flow will be enabled.
5810  */
5811 int
5812 ice_add_prof_id_flow(struct ice_hw *hw, enum ice_block blk, u16 vsi, u64 hdl)
5813 {
5814 	struct ice_vsig_prof *tmp1, *del1;
5815 	struct ice_chs_chg *tmp, *del;
5816 	struct list_head union_lst;
5817 	struct list_head chg;
5818 	int status;
5819 	u16 vsig;
5820 
5821 	INIT_LIST_HEAD(&union_lst);
5822 	INIT_LIST_HEAD(&chg);
5823 
5824 	/* Get profile */
5825 	status = ice_get_prof(hw, blk, hdl, &chg);
5826 	if (status)
5827 		return status;
5828 
5829 	/* determine if VSI is already part of a VSIG */
5830 	status = ice_vsig_find_vsi(hw, blk, vsi, &vsig);
5831 	if (!status && vsig) {
5832 		bool only_vsi;
5833 		u16 or_vsig;
5834 		u16 ref;
5835 
5836 		/* found in VSIG */
5837 		or_vsig = vsig;
5838 
5839 		/* make sure that there is no overlap/conflict between the new
5840 		 * characteristics and the existing ones; we don't support that
5841 		 * scenario
5842 		 */
5843 		if (ice_has_prof_vsig(hw, blk, vsig, hdl)) {
5844 			status = -EEXIST;
5845 			goto err_ice_add_prof_id_flow;
5846 		}
5847 
5848 		/* last VSI in the VSIG? */
5849 		status = ice_vsig_get_ref(hw, blk, vsig, &ref);
5850 		if (status)
5851 			goto err_ice_add_prof_id_flow;
5852 		only_vsi = (ref == 1);
5853 
5854 		/* create a union of the current profiles and the one being
5855 		 * added
5856 		 */
5857 		status = ice_get_profs_vsig(hw, blk, vsig, &union_lst);
5858 		if (status)
5859 			goto err_ice_add_prof_id_flow;
5860 
5861 		status = ice_add_prof_to_lst(hw, blk, &union_lst, hdl);
5862 		if (status)
5863 			goto err_ice_add_prof_id_flow;
5864 
5865 		/* search for an existing VSIG with an exact charc match */
5866 		status = ice_find_dup_props_vsig(hw, blk, &union_lst, &vsig);
5867 		if (!status) {
5868 			/* move VSI to the VSIG that matches */
5869 			status = ice_move_vsi(hw, blk, vsi, vsig, &chg);
5870 			if (status)
5871 				goto err_ice_add_prof_id_flow;
5872 
5873 			/* VSI has been moved out of or_vsig. If the or_vsig had
5874 			 * only that VSI it is now empty and can be removed.
5875 			 */
5876 			if (only_vsi) {
5877 				status = ice_rem_vsig(hw, blk, or_vsig, &chg);
5878 				if (status)
5879 					goto err_ice_add_prof_id_flow;
5880 			}
5881 		} else if (only_vsi) {
5882 			/* If the original VSIG only contains one VSI, then it
5883 			 * will be the requesting VSI. In this case the VSI is
5884 			 * not sharing entries and we can simply add the new
5885 			 * profile to the VSIG.
5886 			 */
5887 			status = ice_add_prof_id_vsig(hw, blk, vsig, hdl, false,
5888 						      &chg);
5889 			if (status)
5890 				goto err_ice_add_prof_id_flow;
5891 
5892 			/* Adjust priorities */
5893 			status = ice_adj_prof_priorities(hw, blk, vsig, &chg);
5894 			if (status)
5895 				goto err_ice_add_prof_id_flow;
5896 		} else {
5897 			/* No match, so we need a new VSIG */
5898 			status = ice_create_vsig_from_lst(hw, blk, vsi,
5899 							  &union_lst, &vsig,
5900 							  &chg);
5901 			if (status)
5902 				goto err_ice_add_prof_id_flow;
5903 
5904 			/* Adjust priorities */
5905 			status = ice_adj_prof_priorities(hw, blk, vsig, &chg);
5906 			if (status)
5907 				goto err_ice_add_prof_id_flow;
5908 		}
5909 	} else {
5910 		/* need to find or add a VSIG */
5911 		/* search for an existing VSIG with an exact charc match */
5912 		if (ice_find_prof_vsig(hw, blk, hdl, &vsig)) {
5913 			/* found an exact match */
5914 			/* add or move VSI to the VSIG that matches */
5915 			status = ice_move_vsi(hw, blk, vsi, vsig, &chg);
5916 			if (status)
5917 				goto err_ice_add_prof_id_flow;
5918 		} else {
5919 			/* we did not find an exact match */
5920 			/* we need to add a VSIG */
5921 			status = ice_create_prof_id_vsig(hw, blk, vsi, hdl,
5922 							 &chg);
5923 			if (status)
5924 				goto err_ice_add_prof_id_flow;
5925 		}
5926 	}
5927 
5928 	/* update hardware */
5929 	if (!status)
5930 		status = ice_upd_prof_hw(hw, blk, &chg);
5931 
5932 err_ice_add_prof_id_flow:
5933 	list_for_each_entry_safe(del, tmp, &chg, list_entry) {
5934 		list_del(&del->list_entry);
5935 		devm_kfree(ice_hw_to_dev(hw), del);
5936 	}
5937 
5938 	list_for_each_entry_safe(del1, tmp1, &union_lst, list) {
5939 		list_del(&del1->list);
5940 		devm_kfree(ice_hw_to_dev(hw), del1);
5941 	}
5942 
5943 	return status;
5944 }
5945 
5946 /**
5947  * ice_rem_prof_from_list - remove a profile from list
5948  * @hw: pointer to the HW struct
5949  * @lst: list to remove the profile from
5950  * @hdl: the profile handle indicating the profile to remove
5951  */
5952 static int
5953 ice_rem_prof_from_list(struct ice_hw *hw, struct list_head *lst, u64 hdl)
5954 {
5955 	struct ice_vsig_prof *ent, *tmp;
5956 
5957 	list_for_each_entry_safe(ent, tmp, lst, list)
5958 		if (ent->profile_cookie == hdl) {
5959 			list_del(&ent->list);
5960 			devm_kfree(ice_hw_to_dev(hw), ent);
5961 			return 0;
5962 		}
5963 
5964 	return -ENOENT;
5965 }
5966 
5967 /**
5968  * ice_rem_prof_id_flow - remove flow
5969  * @hw: pointer to the HW struct
5970  * @blk: hardware block
5971  * @vsi: the VSI from which to remove the profile specified by ID
5972  * @hdl: profile tracking handle
5973  *
5974  * Calling this function will update the hardware tables to remove the
5975  * profile indicated by the ID parameter for the VSIs specified in the VSI
5976  * array. Once successfully called, the flow will be disabled.
5977  */
5978 int
5979 ice_rem_prof_id_flow(struct ice_hw *hw, enum ice_block blk, u16 vsi, u64 hdl)
5980 {
5981 	struct ice_vsig_prof *tmp1, *del1;
5982 	struct ice_chs_chg *tmp, *del;
5983 	struct list_head chg, copy;
5984 	int status;
5985 	u16 vsig;
5986 
5987 	INIT_LIST_HEAD(&copy);
5988 	INIT_LIST_HEAD(&chg);
5989 
5990 	/* determine if VSI is already part of a VSIG */
5991 	status = ice_vsig_find_vsi(hw, blk, vsi, &vsig);
5992 	if (!status && vsig) {
5993 		bool last_profile;
5994 		bool only_vsi;
5995 		u16 ref;
5996 
5997 		/* found in VSIG */
5998 		last_profile = ice_vsig_prof_id_count(hw, blk, vsig) == 1;
5999 		status = ice_vsig_get_ref(hw, blk, vsig, &ref);
6000 		if (status)
6001 			goto err_ice_rem_prof_id_flow;
6002 		only_vsi = (ref == 1);
6003 
6004 		if (only_vsi) {
6005 			/* If the original VSIG only contains one reference,
6006 			 * which will be the requesting VSI, then the VSI is not
6007 			 * sharing entries and we can simply remove the specific
6008 			 * characteristics from the VSIG.
6009 			 */
6010 
6011 			if (last_profile) {
6012 				/* If there are no profiles left for this VSIG,
6013 				 * then simply remove the VSIG.
6014 				 */
6015 				status = ice_rem_vsig(hw, blk, vsig, &chg);
6016 				if (status)
6017 					goto err_ice_rem_prof_id_flow;
6018 			} else {
6019 				status = ice_rem_prof_id_vsig(hw, blk, vsig,
6020 							      hdl, &chg);
6021 				if (status)
6022 					goto err_ice_rem_prof_id_flow;
6023 
6024 				/* Adjust priorities */
6025 				status = ice_adj_prof_priorities(hw, blk, vsig,
6026 								 &chg);
6027 				if (status)
6028 					goto err_ice_rem_prof_id_flow;
6029 			}
6030 
6031 		} else {
6032 			/* Make a copy of the VSIG's list of Profiles */
6033 			status = ice_get_profs_vsig(hw, blk, vsig, &copy);
6034 			if (status)
6035 				goto err_ice_rem_prof_id_flow;
6036 
6037 			/* Remove specified profile entry from the list */
6038 			status = ice_rem_prof_from_list(hw, &copy, hdl);
6039 			if (status)
6040 				goto err_ice_rem_prof_id_flow;
6041 
6042 			if (list_empty(&copy)) {
6043 				status = ice_move_vsi(hw, blk, vsi,
6044 						      ICE_DEFAULT_VSIG, &chg);
6045 				if (status)
6046 					goto err_ice_rem_prof_id_flow;
6047 
6048 			} else if (!ice_find_dup_props_vsig(hw, blk, &copy,
6049 							    &vsig)) {
6050 				/* found an exact match */
6051 				/* add or move VSI to the VSIG that matches */
6052 				/* Search for a VSIG with a matching profile
6053 				 * list
6054 				 */
6055 
6056 				/* Found match, move VSI to the matching VSIG */
6057 				status = ice_move_vsi(hw, blk, vsi, vsig, &chg);
6058 				if (status)
6059 					goto err_ice_rem_prof_id_flow;
6060 			} else {
6061 				/* since no existing VSIG supports this
6062 				 * characteristic pattern, we need to create a
6063 				 * new VSIG and TCAM entries
6064 				 */
6065 				status = ice_create_vsig_from_lst(hw, blk, vsi,
6066 								  &copy, &vsig,
6067 								  &chg);
6068 				if (status)
6069 					goto err_ice_rem_prof_id_flow;
6070 
6071 				/* Adjust priorities */
6072 				status = ice_adj_prof_priorities(hw, blk, vsig,
6073 								 &chg);
6074 				if (status)
6075 					goto err_ice_rem_prof_id_flow;
6076 			}
6077 		}
6078 	} else {
6079 		status = -ENOENT;
6080 	}
6081 
6082 	/* update hardware tables */
6083 	if (!status)
6084 		status = ice_upd_prof_hw(hw, blk, &chg);
6085 
6086 err_ice_rem_prof_id_flow:
6087 	list_for_each_entry_safe(del, tmp, &chg, list_entry) {
6088 		list_del(&del->list_entry);
6089 		devm_kfree(ice_hw_to_dev(hw), del);
6090 	}
6091 
6092 	list_for_each_entry_safe(del1, tmp1, &copy, list) {
6093 		list_del(&del1->list);
6094 		devm_kfree(ice_hw_to_dev(hw), del1);
6095 	}
6096 
6097 	return status;
6098 }
6099