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