xref: /linux/drivers/net/ethernet/intel/ice/ice_flex_pipe.c (revision b1f5921a99ac8dedadf1f2599486b2ca9e01cc0f)
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 static const u32 ice_sect_lkup[ICE_BLK_COUNT][ICE_SECT_COUNT] = {
10 	/* SWITCH */
11 	{
12 		ICE_SID_XLT0_SW,
13 		ICE_SID_XLT_KEY_BUILDER_SW,
14 		ICE_SID_XLT1_SW,
15 		ICE_SID_XLT2_SW,
16 		ICE_SID_PROFID_TCAM_SW,
17 		ICE_SID_PROFID_REDIR_SW,
18 		ICE_SID_FLD_VEC_SW,
19 		ICE_SID_CDID_KEY_BUILDER_SW,
20 		ICE_SID_CDID_REDIR_SW
21 	},
22 
23 	/* ACL */
24 	{
25 		ICE_SID_XLT0_ACL,
26 		ICE_SID_XLT_KEY_BUILDER_ACL,
27 		ICE_SID_XLT1_ACL,
28 		ICE_SID_XLT2_ACL,
29 		ICE_SID_PROFID_TCAM_ACL,
30 		ICE_SID_PROFID_REDIR_ACL,
31 		ICE_SID_FLD_VEC_ACL,
32 		ICE_SID_CDID_KEY_BUILDER_ACL,
33 		ICE_SID_CDID_REDIR_ACL
34 	},
35 
36 	/* FD */
37 	{
38 		ICE_SID_XLT0_FD,
39 		ICE_SID_XLT_KEY_BUILDER_FD,
40 		ICE_SID_XLT1_FD,
41 		ICE_SID_XLT2_FD,
42 		ICE_SID_PROFID_TCAM_FD,
43 		ICE_SID_PROFID_REDIR_FD,
44 		ICE_SID_FLD_VEC_FD,
45 		ICE_SID_CDID_KEY_BUILDER_FD,
46 		ICE_SID_CDID_REDIR_FD
47 	},
48 
49 	/* RSS */
50 	{
51 		ICE_SID_XLT0_RSS,
52 		ICE_SID_XLT_KEY_BUILDER_RSS,
53 		ICE_SID_XLT1_RSS,
54 		ICE_SID_XLT2_RSS,
55 		ICE_SID_PROFID_TCAM_RSS,
56 		ICE_SID_PROFID_REDIR_RSS,
57 		ICE_SID_FLD_VEC_RSS,
58 		ICE_SID_CDID_KEY_BUILDER_RSS,
59 		ICE_SID_CDID_REDIR_RSS
60 	},
61 
62 	/* PE */
63 	{
64 		ICE_SID_XLT0_PE,
65 		ICE_SID_XLT_KEY_BUILDER_PE,
66 		ICE_SID_XLT1_PE,
67 		ICE_SID_XLT2_PE,
68 		ICE_SID_PROFID_TCAM_PE,
69 		ICE_SID_PROFID_REDIR_PE,
70 		ICE_SID_FLD_VEC_PE,
71 		ICE_SID_CDID_KEY_BUILDER_PE,
72 		ICE_SID_CDID_REDIR_PE
73 	}
74 };
75 
76 /**
77  * ice_sect_id - returns section ID
78  * @blk: block type
79  * @sect: section type
80  *
81  * This helper function returns the proper section ID given a block type and a
82  * section type.
83  */
84 static u32 ice_sect_id(enum ice_block blk, enum ice_sect sect)
85 {
86 	return ice_sect_lkup[blk][sect];
87 }
88 
89 /**
90  * ice_hw_ptype_ena - check if the PTYPE is enabled or not
91  * @hw: pointer to the HW structure
92  * @ptype: the hardware PTYPE
93  */
94 bool ice_hw_ptype_ena(struct ice_hw *hw, u16 ptype)
95 {
96 	return ptype < ICE_FLOW_PTYPE_MAX &&
97 	       test_bit(ptype, hw->hw_ptype);
98 }
99 
100 /* Key creation */
101 
102 #define ICE_DC_KEY	0x1	/* don't care */
103 #define ICE_DC_KEYINV	0x1
104 #define ICE_NM_KEY	0x0	/* never match */
105 #define ICE_NM_KEYINV	0x0
106 #define ICE_0_KEY	0x1	/* match 0 */
107 #define ICE_0_KEYINV	0x0
108 #define ICE_1_KEY	0x0	/* match 1 */
109 #define ICE_1_KEYINV	0x1
110 
111 /**
112  * ice_gen_key_word - generate 16-bits of a key/mask word
113  * @val: the value
114  * @valid: valid bits mask (change only the valid bits)
115  * @dont_care: don't care mask
116  * @nvr_mtch: never match mask
117  * @key: pointer to an array of where the resulting key portion
118  * @key_inv: pointer to an array of where the resulting key invert portion
119  *
120  * This function generates 16-bits from a 8-bit value, an 8-bit don't care mask
121  * and an 8-bit never match mask. The 16-bits of output are divided into 8 bits
122  * of key and 8 bits of key invert.
123  *
124  *     '0' =    b01, always match a 0 bit
125  *     '1' =    b10, always match a 1 bit
126  *     '?' =    b11, don't care bit (always matches)
127  *     '~' =    b00, never match bit
128  *
129  * Input:
130  *          val:         b0  1  0  1  0  1
131  *          dont_care:   b0  0  1  1  0  0
132  *          never_mtch:  b0  0  0  0  1  1
133  *          ------------------------------
134  * Result:  key:        b01 10 11 11 00 00
135  */
136 static int
137 ice_gen_key_word(u8 val, u8 valid, u8 dont_care, u8 nvr_mtch, u8 *key,
138 		 u8 *key_inv)
139 {
140 	u8 in_key = *key, in_key_inv = *key_inv;
141 	u8 i;
142 
143 	/* 'dont_care' and 'nvr_mtch' masks cannot overlap */
144 	if ((dont_care ^ nvr_mtch) != (dont_care | nvr_mtch))
145 		return -EIO;
146 
147 	*key = 0;
148 	*key_inv = 0;
149 
150 	/* encode the 8 bits into 8-bit key and 8-bit key invert */
151 	for (i = 0; i < 8; i++) {
152 		*key >>= 1;
153 		*key_inv >>= 1;
154 
155 		if (!(valid & 0x1)) { /* change only valid bits */
156 			*key |= (in_key & 0x1) << 7;
157 			*key_inv |= (in_key_inv & 0x1) << 7;
158 		} else if (dont_care & 0x1) { /* don't care bit */
159 			*key |= ICE_DC_KEY << 7;
160 			*key_inv |= ICE_DC_KEYINV << 7;
161 		} else if (nvr_mtch & 0x1) { /* never match bit */
162 			*key |= ICE_NM_KEY << 7;
163 			*key_inv |= ICE_NM_KEYINV << 7;
164 		} else if (val & 0x01) { /* exact 1 match */
165 			*key |= ICE_1_KEY << 7;
166 			*key_inv |= ICE_1_KEYINV << 7;
167 		} else { /* exact 0 match */
168 			*key |= ICE_0_KEY << 7;
169 			*key_inv |= ICE_0_KEYINV << 7;
170 		}
171 
172 		dont_care >>= 1;
173 		nvr_mtch >>= 1;
174 		valid >>= 1;
175 		val >>= 1;
176 		in_key >>= 1;
177 		in_key_inv >>= 1;
178 	}
179 
180 	return 0;
181 }
182 
183 /**
184  * ice_bits_max_set - determine if the number of bits set is within a maximum
185  * @mask: pointer to the byte array which is the mask
186  * @size: the number of bytes in the mask
187  * @max: the max number of set bits
188  *
189  * This function determines if there are at most 'max' number of bits set in an
190  * array. Returns true if the number for bits set is <= max or will return false
191  * otherwise.
192  */
193 static bool ice_bits_max_set(const u8 *mask, u16 size, u16 max)
194 {
195 	u16 count = 0;
196 	u16 i;
197 
198 	/* check each byte */
199 	for (i = 0; i < size; i++) {
200 		/* if 0, go to next byte */
201 		if (!mask[i])
202 			continue;
203 
204 		/* We know there is at least one set bit in this byte because of
205 		 * the above check; if we already have found 'max' number of
206 		 * bits set, then we can return failure now.
207 		 */
208 		if (count == max)
209 			return false;
210 
211 		/* count the bits in this byte, checking threshold */
212 		count += hweight8(mask[i]);
213 		if (count > max)
214 			return false;
215 	}
216 
217 	return true;
218 }
219 
220 /**
221  * ice_set_key - generate a variable sized key with multiples of 16-bits
222  * @key: pointer to where the key will be stored
223  * @size: the size of the complete key in bytes (must be even)
224  * @val: array of 8-bit values that makes up the value portion of the key
225  * @upd: array of 8-bit masks that determine what key portion to update
226  * @dc: array of 8-bit masks that make up the don't care mask
227  * @nm: array of 8-bit masks that make up the never match mask
228  * @off: the offset of the first byte in the key to update
229  * @len: the number of bytes in the key update
230  *
231  * This function generates a key from a value, a don't care mask and a never
232  * match mask.
233  * upd, dc, and nm are optional parameters, and can be NULL:
234  *	upd == NULL --> upd mask is all 1's (update all bits)
235  *	dc == NULL --> dc mask is all 0's (no don't care bits)
236  *	nm == NULL --> nm mask is all 0's (no never match bits)
237  */
238 static int
239 ice_set_key(u8 *key, u16 size, u8 *val, u8 *upd, u8 *dc, u8 *nm, u16 off,
240 	    u16 len)
241 {
242 	u16 half_size;
243 	u16 i;
244 
245 	/* size must be a multiple of 2 bytes. */
246 	if (size % 2)
247 		return -EIO;
248 
249 	half_size = size / 2;
250 	if (off + len > half_size)
251 		return -EIO;
252 
253 	/* Make sure at most one bit is set in the never match mask. Having more
254 	 * than one never match mask bit set will cause HW to consume excessive
255 	 * power otherwise; this is a power management efficiency check.
256 	 */
257 #define ICE_NVR_MTCH_BITS_MAX	1
258 	if (nm && !ice_bits_max_set(nm, len, ICE_NVR_MTCH_BITS_MAX))
259 		return -EIO;
260 
261 	for (i = 0; i < len; i++)
262 		if (ice_gen_key_word(val[i], upd ? upd[i] : 0xff,
263 				     dc ? dc[i] : 0, nm ? nm[i] : 0,
264 				     key + off + i, key + half_size + off + i))
265 			return -EIO;
266 
267 	return 0;
268 }
269 
270 /**
271  * ice_acquire_change_lock
272  * @hw: pointer to the HW structure
273  * @access: access type (read or write)
274  *
275  * This function will request ownership of the change lock.
276  */
277 int
278 ice_acquire_change_lock(struct ice_hw *hw, enum ice_aq_res_access_type access)
279 {
280 	return ice_acquire_res(hw, ICE_CHANGE_LOCK_RES_ID, access,
281 			       ICE_CHANGE_LOCK_TIMEOUT);
282 }
283 
284 /**
285  * ice_release_change_lock
286  * @hw: pointer to the HW structure
287  *
288  * This function will release the change lock using the proper Admin Command.
289  */
290 void ice_release_change_lock(struct ice_hw *hw)
291 {
292 	ice_release_res(hw, ICE_CHANGE_LOCK_RES_ID);
293 }
294 
295 /**
296  * ice_get_open_tunnel_port - retrieve an open tunnel port
297  * @hw: pointer to the HW structure
298  * @port: returns open port
299  * @type: type of tunnel, can be TNL_LAST if it doesn't matter
300  */
301 bool
302 ice_get_open_tunnel_port(struct ice_hw *hw, u16 *port,
303 			 enum ice_tunnel_type type)
304 {
305 	bool res = false;
306 	u16 i;
307 
308 	mutex_lock(&hw->tnl_lock);
309 
310 	for (i = 0; i < hw->tnl.count && i < ICE_TUNNEL_MAX_ENTRIES; i++)
311 		if (hw->tnl.tbl[i].valid && hw->tnl.tbl[i].port &&
312 		    (type == TNL_LAST || type == hw->tnl.tbl[i].type)) {
313 			*port = hw->tnl.tbl[i].port;
314 			res = true;
315 			break;
316 		}
317 
318 	mutex_unlock(&hw->tnl_lock);
319 
320 	return res;
321 }
322 
323 /**
324  * ice_upd_dvm_boost_entry
325  * @hw: pointer to the HW structure
326  * @entry: pointer to double vlan boost entry info
327  */
328 static int
329 ice_upd_dvm_boost_entry(struct ice_hw *hw, struct ice_dvm_entry *entry)
330 {
331 	struct ice_boost_tcam_section *sect_rx, *sect_tx;
332 	int status = -ENOSPC;
333 	struct ice_buf_build *bld;
334 	u8 val, dc, nm;
335 
336 	bld = ice_pkg_buf_alloc(hw);
337 	if (!bld)
338 		return -ENOMEM;
339 
340 	/* allocate 2 sections, one for Rx parser, one for Tx parser */
341 	if (ice_pkg_buf_reserve_section(bld, 2))
342 		goto ice_upd_dvm_boost_entry_err;
343 
344 	sect_rx = ice_pkg_buf_alloc_section(bld, ICE_SID_RXPARSER_BOOST_TCAM,
345 					    struct_size(sect_rx, tcam, 1));
346 	if (!sect_rx)
347 		goto ice_upd_dvm_boost_entry_err;
348 	sect_rx->count = cpu_to_le16(1);
349 
350 	sect_tx = ice_pkg_buf_alloc_section(bld, ICE_SID_TXPARSER_BOOST_TCAM,
351 					    struct_size(sect_tx, tcam, 1));
352 	if (!sect_tx)
353 		goto ice_upd_dvm_boost_entry_err;
354 	sect_tx->count = cpu_to_le16(1);
355 
356 	/* copy original boost entry to update package buffer */
357 	memcpy(sect_rx->tcam, entry->boost_entry, sizeof(*sect_rx->tcam));
358 
359 	/* re-write the don't care and never match bits accordingly */
360 	if (entry->enable) {
361 		/* all bits are don't care */
362 		val = 0x00;
363 		dc = 0xFF;
364 		nm = 0x00;
365 	} else {
366 		/* disable, one never match bit, the rest are don't care */
367 		val = 0x00;
368 		dc = 0xF7;
369 		nm = 0x08;
370 	}
371 
372 	ice_set_key((u8 *)&sect_rx->tcam[0].key, sizeof(sect_rx->tcam[0].key),
373 		    &val, NULL, &dc, &nm, 0, sizeof(u8));
374 
375 	/* exact copy of entry to Tx section entry */
376 	memcpy(sect_tx->tcam, sect_rx->tcam, sizeof(*sect_tx->tcam));
377 
378 	status = ice_update_pkg_no_lock(hw, ice_pkg_buf(bld), 1);
379 
380 ice_upd_dvm_boost_entry_err:
381 	ice_pkg_buf_free(hw, bld);
382 
383 	return status;
384 }
385 
386 /**
387  * ice_set_dvm_boost_entries
388  * @hw: pointer to the HW structure
389  *
390  * Enable double vlan by updating the appropriate boost tcam entries.
391  */
392 int ice_set_dvm_boost_entries(struct ice_hw *hw)
393 {
394 	u16 i;
395 
396 	for (i = 0; i < hw->dvm_upd.count; i++) {
397 		int status;
398 
399 		status = ice_upd_dvm_boost_entry(hw, &hw->dvm_upd.tbl[i]);
400 		if (status)
401 			return status;
402 	}
403 
404 	return 0;
405 }
406 
407 /**
408  * ice_tunnel_idx_to_entry - convert linear index to the sparse one
409  * @hw: pointer to the HW structure
410  * @type: type of tunnel
411  * @idx: linear index
412  *
413  * Stack assumes we have 2 linear tables with indexes [0, count_valid),
414  * but really the port table may be sprase, and types are mixed, so convert
415  * the stack index into the device index.
416  */
417 static u16 ice_tunnel_idx_to_entry(struct ice_hw *hw, enum ice_tunnel_type type,
418 				   u16 idx)
419 {
420 	u16 i;
421 
422 	for (i = 0; i < hw->tnl.count && i < ICE_TUNNEL_MAX_ENTRIES; i++)
423 		if (hw->tnl.tbl[i].valid &&
424 		    hw->tnl.tbl[i].type == type &&
425 		    idx-- == 0)
426 			return i;
427 
428 	WARN_ON_ONCE(1);
429 	return 0;
430 }
431 
432 /**
433  * ice_create_tunnel
434  * @hw: pointer to the HW structure
435  * @index: device table entry
436  * @type: type of tunnel
437  * @port: port of tunnel to create
438  *
439  * Create a tunnel by updating the parse graph in the parser. We do that by
440  * creating a package buffer with the tunnel info and issuing an update package
441  * command.
442  */
443 static int
444 ice_create_tunnel(struct ice_hw *hw, u16 index,
445 		  enum ice_tunnel_type type, u16 port)
446 {
447 	struct ice_boost_tcam_section *sect_rx, *sect_tx;
448 	struct ice_buf_build *bld;
449 	int status = -ENOSPC;
450 
451 	mutex_lock(&hw->tnl_lock);
452 
453 	bld = ice_pkg_buf_alloc(hw);
454 	if (!bld) {
455 		status = -ENOMEM;
456 		goto ice_create_tunnel_end;
457 	}
458 
459 	/* allocate 2 sections, one for Rx parser, one for Tx parser */
460 	if (ice_pkg_buf_reserve_section(bld, 2))
461 		goto ice_create_tunnel_err;
462 
463 	sect_rx = ice_pkg_buf_alloc_section(bld, ICE_SID_RXPARSER_BOOST_TCAM,
464 					    struct_size(sect_rx, tcam, 1));
465 	if (!sect_rx)
466 		goto ice_create_tunnel_err;
467 	sect_rx->count = cpu_to_le16(1);
468 
469 	sect_tx = ice_pkg_buf_alloc_section(bld, ICE_SID_TXPARSER_BOOST_TCAM,
470 					    struct_size(sect_tx, tcam, 1));
471 	if (!sect_tx)
472 		goto ice_create_tunnel_err;
473 	sect_tx->count = cpu_to_le16(1);
474 
475 	/* copy original boost entry to update package buffer */
476 	memcpy(sect_rx->tcam, hw->tnl.tbl[index].boost_entry,
477 	       sizeof(*sect_rx->tcam));
478 
479 	/* over-write the never-match dest port key bits with the encoded port
480 	 * bits
481 	 */
482 	ice_set_key((u8 *)&sect_rx->tcam[0].key, sizeof(sect_rx->tcam[0].key),
483 		    (u8 *)&port, NULL, NULL, NULL,
484 		    (u16)offsetof(struct ice_boost_key_value, hv_dst_port_key),
485 		    sizeof(sect_rx->tcam[0].key.key.hv_dst_port_key));
486 
487 	/* exact copy of entry to Tx section entry */
488 	memcpy(sect_tx->tcam, sect_rx->tcam, sizeof(*sect_tx->tcam));
489 
490 	status = ice_update_pkg(hw, ice_pkg_buf(bld), 1);
491 	if (!status)
492 		hw->tnl.tbl[index].port = port;
493 
494 ice_create_tunnel_err:
495 	ice_pkg_buf_free(hw, bld);
496 
497 ice_create_tunnel_end:
498 	mutex_unlock(&hw->tnl_lock);
499 
500 	return status;
501 }
502 
503 /**
504  * ice_destroy_tunnel
505  * @hw: pointer to the HW structure
506  * @index: device table entry
507  * @type: type of tunnel
508  * @port: port of tunnel to destroy (ignored if the all parameter is true)
509  *
510  * Destroys a tunnel or all tunnels by creating an update package buffer
511  * targeting the specific updates requested and then performing an update
512  * package.
513  */
514 static int
515 ice_destroy_tunnel(struct ice_hw *hw, u16 index, enum ice_tunnel_type type,
516 		   u16 port)
517 {
518 	struct ice_boost_tcam_section *sect_rx, *sect_tx;
519 	struct ice_buf_build *bld;
520 	int status = -ENOSPC;
521 
522 	mutex_lock(&hw->tnl_lock);
523 
524 	if (WARN_ON(!hw->tnl.tbl[index].valid ||
525 		    hw->tnl.tbl[index].type != type ||
526 		    hw->tnl.tbl[index].port != port)) {
527 		status = -EIO;
528 		goto ice_destroy_tunnel_end;
529 	}
530 
531 	bld = ice_pkg_buf_alloc(hw);
532 	if (!bld) {
533 		status = -ENOMEM;
534 		goto ice_destroy_tunnel_end;
535 	}
536 
537 	/* allocate 2 sections, one for Rx parser, one for Tx parser */
538 	if (ice_pkg_buf_reserve_section(bld, 2))
539 		goto ice_destroy_tunnel_err;
540 
541 	sect_rx = ice_pkg_buf_alloc_section(bld, ICE_SID_RXPARSER_BOOST_TCAM,
542 					    struct_size(sect_rx, tcam, 1));
543 	if (!sect_rx)
544 		goto ice_destroy_tunnel_err;
545 	sect_rx->count = cpu_to_le16(1);
546 
547 	sect_tx = ice_pkg_buf_alloc_section(bld, ICE_SID_TXPARSER_BOOST_TCAM,
548 					    struct_size(sect_tx, tcam, 1));
549 	if (!sect_tx)
550 		goto ice_destroy_tunnel_err;
551 	sect_tx->count = cpu_to_le16(1);
552 
553 	/* copy original boost entry to update package buffer, one copy to Rx
554 	 * section, another copy to the Tx section
555 	 */
556 	memcpy(sect_rx->tcam, hw->tnl.tbl[index].boost_entry,
557 	       sizeof(*sect_rx->tcam));
558 	memcpy(sect_tx->tcam, hw->tnl.tbl[index].boost_entry,
559 	       sizeof(*sect_tx->tcam));
560 
561 	status = ice_update_pkg(hw, ice_pkg_buf(bld), 1);
562 	if (!status)
563 		hw->tnl.tbl[index].port = 0;
564 
565 ice_destroy_tunnel_err:
566 	ice_pkg_buf_free(hw, bld);
567 
568 ice_destroy_tunnel_end:
569 	mutex_unlock(&hw->tnl_lock);
570 
571 	return status;
572 }
573 
574 int ice_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
575 			    unsigned int idx, struct udp_tunnel_info *ti)
576 {
577 	struct ice_netdev_priv *np = netdev_priv(netdev);
578 	struct ice_vsi *vsi = np->vsi;
579 	struct ice_pf *pf = vsi->back;
580 	enum ice_tunnel_type tnl_type;
581 	int status;
582 	u16 index;
583 
584 	tnl_type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? TNL_VXLAN : TNL_GENEVE;
585 	index = ice_tunnel_idx_to_entry(&pf->hw, tnl_type, idx);
586 
587 	status = ice_create_tunnel(&pf->hw, index, tnl_type, ntohs(ti->port));
588 	if (status) {
589 		netdev_err(netdev, "Error adding UDP tunnel - %d\n",
590 			   status);
591 		return -EIO;
592 	}
593 
594 	udp_tunnel_nic_set_port_priv(netdev, table, idx, index);
595 	return 0;
596 }
597 
598 int ice_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
599 			      unsigned int idx, struct udp_tunnel_info *ti)
600 {
601 	struct ice_netdev_priv *np = netdev_priv(netdev);
602 	struct ice_vsi *vsi = np->vsi;
603 	struct ice_pf *pf = vsi->back;
604 	enum ice_tunnel_type tnl_type;
605 	int status;
606 
607 	tnl_type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? TNL_VXLAN : TNL_GENEVE;
608 
609 	status = ice_destroy_tunnel(&pf->hw, ti->hw_priv, tnl_type,
610 				    ntohs(ti->port));
611 	if (status) {
612 		netdev_err(netdev, "Error removing UDP tunnel - %d\n",
613 			   status);
614 		return -EIO;
615 	}
616 
617 	return 0;
618 }
619 
620 /**
621  * ice_find_prot_off - find prot ID and offset pair, based on prof and FV index
622  * @hw: pointer to the hardware structure
623  * @blk: hardware block
624  * @prof: profile ID
625  * @fv_idx: field vector word index
626  * @prot: variable to receive the protocol ID
627  * @off: variable to receive the protocol offset
628  */
629 int
630 ice_find_prot_off(struct ice_hw *hw, enum ice_block blk, u8 prof, u16 fv_idx,
631 		  u8 *prot, u16 *off)
632 {
633 	struct ice_fv_word *fv_ext;
634 
635 	if (prof >= hw->blk[blk].es.count)
636 		return -EINVAL;
637 
638 	if (fv_idx >= hw->blk[blk].es.fvw)
639 		return -EINVAL;
640 
641 	fv_ext = hw->blk[blk].es.t + (prof * hw->blk[blk].es.fvw);
642 
643 	*prot = fv_ext[fv_idx].prot_id;
644 	*off = fv_ext[fv_idx].off;
645 
646 	return 0;
647 }
648 
649 /* PTG Management */
650 
651 /**
652  * ice_ptg_find_ptype - Search for packet type group using packet type (ptype)
653  * @hw: pointer to the hardware structure
654  * @blk: HW block
655  * @ptype: the ptype to search for
656  * @ptg: pointer to variable that receives the PTG
657  *
658  * This function will search the PTGs for a particular ptype, returning the
659  * PTG ID that contains it through the PTG parameter, with the value of
660  * ICE_DEFAULT_PTG (0) meaning it is part the default PTG.
661  */
662 static int
663 ice_ptg_find_ptype(struct ice_hw *hw, enum ice_block blk, u16 ptype, u8 *ptg)
664 {
665 	if (ptype >= ICE_XLT1_CNT || !ptg)
666 		return -EINVAL;
667 
668 	*ptg = hw->blk[blk].xlt1.ptypes[ptype].ptg;
669 	return 0;
670 }
671 
672 /**
673  * ice_ptg_alloc_val - Allocates a new packet type group ID by value
674  * @hw: pointer to the hardware structure
675  * @blk: HW block
676  * @ptg: the PTG to allocate
677  *
678  * This function allocates a given packet type group ID specified by the PTG
679  * parameter.
680  */
681 static void ice_ptg_alloc_val(struct ice_hw *hw, enum ice_block blk, u8 ptg)
682 {
683 	hw->blk[blk].xlt1.ptg_tbl[ptg].in_use = true;
684 }
685 
686 /**
687  * ice_ptg_remove_ptype - Removes ptype from a particular packet type group
688  * @hw: pointer to the hardware structure
689  * @blk: HW block
690  * @ptype: the ptype to remove
691  * @ptg: the PTG to remove the ptype from
692  *
693  * This function will remove the ptype from the specific PTG, and move it to
694  * the default PTG (ICE_DEFAULT_PTG).
695  */
696 static int
697 ice_ptg_remove_ptype(struct ice_hw *hw, enum ice_block blk, u16 ptype, u8 ptg)
698 {
699 	struct ice_ptg_ptype **ch;
700 	struct ice_ptg_ptype *p;
701 
702 	if (ptype > ICE_XLT1_CNT - 1)
703 		return -EINVAL;
704 
705 	if (!hw->blk[blk].xlt1.ptg_tbl[ptg].in_use)
706 		return -ENOENT;
707 
708 	/* Should not happen if .in_use is set, bad config */
709 	if (!hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype)
710 		return -EIO;
711 
712 	/* find the ptype within this PTG, and bypass the link over it */
713 	p = hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype;
714 	ch = &hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype;
715 	while (p) {
716 		if (ptype == (p - hw->blk[blk].xlt1.ptypes)) {
717 			*ch = p->next_ptype;
718 			break;
719 		}
720 
721 		ch = &p->next_ptype;
722 		p = p->next_ptype;
723 	}
724 
725 	hw->blk[blk].xlt1.ptypes[ptype].ptg = ICE_DEFAULT_PTG;
726 	hw->blk[blk].xlt1.ptypes[ptype].next_ptype = NULL;
727 
728 	return 0;
729 }
730 
731 /**
732  * ice_ptg_add_mv_ptype - Adds/moves ptype to a particular packet type group
733  * @hw: pointer to the hardware structure
734  * @blk: HW block
735  * @ptype: the ptype to add or move
736  * @ptg: the PTG to add or move the ptype to
737  *
738  * This function will either add or move a ptype to a particular PTG depending
739  * on if the ptype is already part of another group. Note that using a
740  * destination PTG ID of ICE_DEFAULT_PTG (0) will move the ptype to the
741  * default PTG.
742  */
743 static int
744 ice_ptg_add_mv_ptype(struct ice_hw *hw, enum ice_block blk, u16 ptype, u8 ptg)
745 {
746 	u8 original_ptg;
747 	int status;
748 
749 	if (ptype > ICE_XLT1_CNT - 1)
750 		return -EINVAL;
751 
752 	if (!hw->blk[blk].xlt1.ptg_tbl[ptg].in_use && ptg != ICE_DEFAULT_PTG)
753 		return -ENOENT;
754 
755 	status = ice_ptg_find_ptype(hw, blk, ptype, &original_ptg);
756 	if (status)
757 		return status;
758 
759 	/* Is ptype already in the correct PTG? */
760 	if (original_ptg == ptg)
761 		return 0;
762 
763 	/* Remove from original PTG and move back to the default PTG */
764 	if (original_ptg != ICE_DEFAULT_PTG)
765 		ice_ptg_remove_ptype(hw, blk, ptype, original_ptg);
766 
767 	/* Moving to default PTG? Then we're done with this request */
768 	if (ptg == ICE_DEFAULT_PTG)
769 		return 0;
770 
771 	/* Add ptype to PTG at beginning of list */
772 	hw->blk[blk].xlt1.ptypes[ptype].next_ptype =
773 		hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype;
774 	hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype =
775 		&hw->blk[blk].xlt1.ptypes[ptype];
776 
777 	hw->blk[blk].xlt1.ptypes[ptype].ptg = ptg;
778 	hw->blk[blk].xlt1.t[ptype] = ptg;
779 
780 	return 0;
781 }
782 
783 /* Block / table size info */
784 struct ice_blk_size_details {
785 	u16 xlt1;			/* # XLT1 entries */
786 	u16 xlt2;			/* # XLT2 entries */
787 	u16 prof_tcam;			/* # profile ID TCAM entries */
788 	u16 prof_id;			/* # profile IDs */
789 	u8 prof_cdid_bits;		/* # CDID one-hot bits used in key */
790 	u16 prof_redir;			/* # profile redirection entries */
791 	u16 es;				/* # extraction sequence entries */
792 	u16 fvw;			/* # field vector words */
793 	u8 overwrite;			/* overwrite existing entries allowed */
794 	u8 reverse;			/* reverse FV order */
795 };
796 
797 static const struct ice_blk_size_details blk_sizes[ICE_BLK_COUNT] = {
798 	/**
799 	 * Table Definitions
800 	 * XLT1 - Number of entries in XLT1 table
801 	 * XLT2 - Number of entries in XLT2 table
802 	 * TCAM - Number of entries Profile ID TCAM table
803 	 * CDID - Control Domain ID of the hardware block
804 	 * PRED - Number of entries in the Profile Redirection Table
805 	 * FV   - Number of entries in the Field Vector
806 	 * FVW  - Width (in WORDs) of the Field Vector
807 	 * OVR  - Overwrite existing table entries
808 	 * REV  - Reverse FV
809 	 */
810 	/*          XLT1        , XLT2        ,TCAM, PID,CDID,PRED,   FV, FVW */
811 	/*          Overwrite   , Reverse FV */
812 	/* SW  */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 256,   0,  256, 256,  48,
813 		    false, false },
814 	/* ACL */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 128,   0,  128, 128,  32,
815 		    false, false },
816 	/* FD  */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 128,   0,  128, 128,  24,
817 		    false, true  },
818 	/* RSS */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 128,   0,  128, 128,  24,
819 		    true,  true  },
820 	/* PE  */ { ICE_XLT1_CNT, ICE_XLT2_CNT,  64,  32,   0,   32,  32,  24,
821 		    false, false },
822 };
823 
824 enum ice_sid_all {
825 	ICE_SID_XLT1_OFF = 0,
826 	ICE_SID_XLT2_OFF,
827 	ICE_SID_PR_OFF,
828 	ICE_SID_PR_REDIR_OFF,
829 	ICE_SID_ES_OFF,
830 	ICE_SID_OFF_COUNT,
831 };
832 
833 /* Characteristic handling */
834 
835 /**
836  * ice_match_prop_lst - determine if properties of two lists match
837  * @list1: first properties list
838  * @list2: second properties list
839  *
840  * Count, cookies and the order must match in order to be considered equivalent.
841  */
842 static bool
843 ice_match_prop_lst(struct list_head *list1, struct list_head *list2)
844 {
845 	struct ice_vsig_prof *tmp1;
846 	struct ice_vsig_prof *tmp2;
847 	u16 chk_count = 0;
848 	u16 count = 0;
849 
850 	/* compare counts */
851 	list_for_each_entry(tmp1, list1, list)
852 		count++;
853 	list_for_each_entry(tmp2, list2, list)
854 		chk_count++;
855 	if (!count || count != chk_count)
856 		return false;
857 
858 	tmp1 = list_first_entry(list1, struct ice_vsig_prof, list);
859 	tmp2 = list_first_entry(list2, struct ice_vsig_prof, list);
860 
861 	/* profile cookies must compare, and in the exact same order to take
862 	 * into account priority
863 	 */
864 	while (count--) {
865 		if (tmp2->profile_cookie != tmp1->profile_cookie)
866 			return false;
867 
868 		tmp1 = list_next_entry(tmp1, list);
869 		tmp2 = list_next_entry(tmp2, list);
870 	}
871 
872 	return true;
873 }
874 
875 /* VSIG Management */
876 
877 /**
878  * ice_vsig_find_vsi - find a VSIG that contains a specified VSI
879  * @hw: pointer to the hardware structure
880  * @blk: HW block
881  * @vsi: VSI of interest
882  * @vsig: pointer to receive the VSI group
883  *
884  * This function will lookup the VSI entry in the XLT2 list and return
885  * the VSI group its associated with.
886  */
887 static int
888 ice_vsig_find_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 *vsig)
889 {
890 	if (!vsig || vsi >= ICE_MAX_VSI)
891 		return -EINVAL;
892 
893 	/* As long as there's a default or valid VSIG associated with the input
894 	 * VSI, the functions returns a success. Any handling of VSIG will be
895 	 * done by the following add, update or remove functions.
896 	 */
897 	*vsig = hw->blk[blk].xlt2.vsis[vsi].vsig;
898 
899 	return 0;
900 }
901 
902 /**
903  * ice_vsig_alloc_val - allocate a new VSIG by value
904  * @hw: pointer to the hardware structure
905  * @blk: HW block
906  * @vsig: the VSIG to allocate
907  *
908  * This function will allocate a given VSIG specified by the VSIG parameter.
909  */
910 static u16 ice_vsig_alloc_val(struct ice_hw *hw, enum ice_block blk, u16 vsig)
911 {
912 	u16 idx = vsig & ICE_VSIG_IDX_M;
913 
914 	if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use) {
915 		INIT_LIST_HEAD(&hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst);
916 		hw->blk[blk].xlt2.vsig_tbl[idx].in_use = true;
917 	}
918 
919 	return ICE_VSIG_VALUE(idx, hw->pf_id);
920 }
921 
922 /**
923  * ice_vsig_alloc - Finds a free entry and allocates a new VSIG
924  * @hw: pointer to the hardware structure
925  * @blk: HW block
926  *
927  * This function will iterate through the VSIG list and mark the first
928  * unused entry for the new VSIG entry as used and return that value.
929  */
930 static u16 ice_vsig_alloc(struct ice_hw *hw, enum ice_block blk)
931 {
932 	u16 i;
933 
934 	for (i = 1; i < ICE_MAX_VSIGS; i++)
935 		if (!hw->blk[blk].xlt2.vsig_tbl[i].in_use)
936 			return ice_vsig_alloc_val(hw, blk, i);
937 
938 	return ICE_DEFAULT_VSIG;
939 }
940 
941 /**
942  * ice_find_dup_props_vsig - find VSI group with a specified set of properties
943  * @hw: pointer to the hardware structure
944  * @blk: HW block
945  * @chs: characteristic list
946  * @vsig: returns the VSIG with the matching profiles, if found
947  *
948  * Each VSIG is associated with a characteristic set; i.e. all VSIs under
949  * a group have the same characteristic set. To check if there exists a VSIG
950  * which has the same characteristics as the input characteristics; this
951  * function will iterate through the XLT2 list and return the VSIG that has a
952  * matching configuration. In order to make sure that priorities are accounted
953  * for, the list must match exactly, including the order in which the
954  * characteristics are listed.
955  */
956 static int
957 ice_find_dup_props_vsig(struct ice_hw *hw, enum ice_block blk,
958 			struct list_head *chs, u16 *vsig)
959 {
960 	struct ice_xlt2 *xlt2 = &hw->blk[blk].xlt2;
961 	u16 i;
962 
963 	for (i = 0; i < xlt2->count; i++)
964 		if (xlt2->vsig_tbl[i].in_use &&
965 		    ice_match_prop_lst(chs, &xlt2->vsig_tbl[i].prop_lst)) {
966 			*vsig = ICE_VSIG_VALUE(i, hw->pf_id);
967 			return 0;
968 		}
969 
970 	return -ENOENT;
971 }
972 
973 /**
974  * ice_vsig_free - free VSI group
975  * @hw: pointer to the hardware structure
976  * @blk: HW block
977  * @vsig: VSIG to remove
978  *
979  * The function will remove all VSIs associated with the input VSIG and move
980  * them to the DEFAULT_VSIG and mark the VSIG available.
981  */
982 static int ice_vsig_free(struct ice_hw *hw, enum ice_block blk, u16 vsig)
983 {
984 	struct ice_vsig_prof *dtmp, *del;
985 	struct ice_vsig_vsi *vsi_cur;
986 	u16 idx;
987 
988 	idx = vsig & ICE_VSIG_IDX_M;
989 	if (idx >= ICE_MAX_VSIGS)
990 		return -EINVAL;
991 
992 	if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use)
993 		return -ENOENT;
994 
995 	hw->blk[blk].xlt2.vsig_tbl[idx].in_use = false;
996 
997 	vsi_cur = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
998 	/* If the VSIG has at least 1 VSI then iterate through the
999 	 * list and remove the VSIs before deleting the group.
1000 	 */
1001 	if (vsi_cur) {
1002 		/* remove all vsis associated with this VSIG XLT2 entry */
1003 		do {
1004 			struct ice_vsig_vsi *tmp = vsi_cur->next_vsi;
1005 
1006 			vsi_cur->vsig = ICE_DEFAULT_VSIG;
1007 			vsi_cur->changed = 1;
1008 			vsi_cur->next_vsi = NULL;
1009 			vsi_cur = tmp;
1010 		} while (vsi_cur);
1011 
1012 		/* NULL terminate head of VSI list */
1013 		hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi = NULL;
1014 	}
1015 
1016 	/* free characteristic list */
1017 	list_for_each_entry_safe(del, dtmp,
1018 				 &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
1019 				 list) {
1020 		list_del(&del->list);
1021 		devm_kfree(ice_hw_to_dev(hw), del);
1022 	}
1023 
1024 	/* if VSIG characteristic list was cleared for reset
1025 	 * re-initialize the list head
1026 	 */
1027 	INIT_LIST_HEAD(&hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst);
1028 
1029 	return 0;
1030 }
1031 
1032 /**
1033  * ice_vsig_remove_vsi - remove VSI from VSIG
1034  * @hw: pointer to the hardware structure
1035  * @blk: HW block
1036  * @vsi: VSI to remove
1037  * @vsig: VSI group to remove from
1038  *
1039  * The function will remove the input VSI from its VSI group and move it
1040  * to the DEFAULT_VSIG.
1041  */
1042 static int
1043 ice_vsig_remove_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 vsig)
1044 {
1045 	struct ice_vsig_vsi **vsi_head, *vsi_cur, *vsi_tgt;
1046 	u16 idx;
1047 
1048 	idx = vsig & ICE_VSIG_IDX_M;
1049 
1050 	if (vsi >= ICE_MAX_VSI || idx >= ICE_MAX_VSIGS)
1051 		return -EINVAL;
1052 
1053 	if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use)
1054 		return -ENOENT;
1055 
1056 	/* entry already in default VSIG, don't have to remove */
1057 	if (idx == ICE_DEFAULT_VSIG)
1058 		return 0;
1059 
1060 	vsi_head = &hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
1061 	if (!(*vsi_head))
1062 		return -EIO;
1063 
1064 	vsi_tgt = &hw->blk[blk].xlt2.vsis[vsi];
1065 	vsi_cur = (*vsi_head);
1066 
1067 	/* iterate the VSI list, skip over the entry to be removed */
1068 	while (vsi_cur) {
1069 		if (vsi_tgt == vsi_cur) {
1070 			(*vsi_head) = vsi_cur->next_vsi;
1071 			break;
1072 		}
1073 		vsi_head = &vsi_cur->next_vsi;
1074 		vsi_cur = vsi_cur->next_vsi;
1075 	}
1076 
1077 	/* verify if VSI was removed from group list */
1078 	if (!vsi_cur)
1079 		return -ENOENT;
1080 
1081 	vsi_cur->vsig = ICE_DEFAULT_VSIG;
1082 	vsi_cur->changed = 1;
1083 	vsi_cur->next_vsi = NULL;
1084 
1085 	return 0;
1086 }
1087 
1088 /**
1089  * ice_vsig_add_mv_vsi - add or move a VSI to a VSI group
1090  * @hw: pointer to the hardware structure
1091  * @blk: HW block
1092  * @vsi: VSI to move
1093  * @vsig: destination VSI group
1094  *
1095  * This function will move or add the input VSI to the target VSIG.
1096  * The function will find the original VSIG the VSI belongs to and
1097  * move the entry to the DEFAULT_VSIG, update the original VSIG and
1098  * then move entry to the new VSIG.
1099  */
1100 static int
1101 ice_vsig_add_mv_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 vsig)
1102 {
1103 	struct ice_vsig_vsi *tmp;
1104 	u16 orig_vsig, idx;
1105 	int status;
1106 
1107 	idx = vsig & ICE_VSIG_IDX_M;
1108 
1109 	if (vsi >= ICE_MAX_VSI || idx >= ICE_MAX_VSIGS)
1110 		return -EINVAL;
1111 
1112 	/* if VSIG not in use and VSIG is not default type this VSIG
1113 	 * doesn't exist.
1114 	 */
1115 	if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use &&
1116 	    vsig != ICE_DEFAULT_VSIG)
1117 		return -ENOENT;
1118 
1119 	status = ice_vsig_find_vsi(hw, blk, vsi, &orig_vsig);
1120 	if (status)
1121 		return status;
1122 
1123 	/* no update required if vsigs match */
1124 	if (orig_vsig == vsig)
1125 		return 0;
1126 
1127 	if (orig_vsig != ICE_DEFAULT_VSIG) {
1128 		/* remove entry from orig_vsig and add to default VSIG */
1129 		status = ice_vsig_remove_vsi(hw, blk, vsi, orig_vsig);
1130 		if (status)
1131 			return status;
1132 	}
1133 
1134 	if (idx == ICE_DEFAULT_VSIG)
1135 		return 0;
1136 
1137 	/* Create VSI entry and add VSIG and prop_mask values */
1138 	hw->blk[blk].xlt2.vsis[vsi].vsig = vsig;
1139 	hw->blk[blk].xlt2.vsis[vsi].changed = 1;
1140 
1141 	/* Add new entry to the head of the VSIG list */
1142 	tmp = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
1143 	hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi =
1144 		&hw->blk[blk].xlt2.vsis[vsi];
1145 	hw->blk[blk].xlt2.vsis[vsi].next_vsi = tmp;
1146 	hw->blk[blk].xlt2.t[vsi] = vsig;
1147 
1148 	return 0;
1149 }
1150 
1151 /**
1152  * ice_prof_has_mask_idx - determine if profile index masking is identical
1153  * @hw: pointer to the hardware structure
1154  * @blk: HW block
1155  * @prof: profile to check
1156  * @idx: profile index to check
1157  * @mask: mask to match
1158  */
1159 static bool
1160 ice_prof_has_mask_idx(struct ice_hw *hw, enum ice_block blk, u8 prof, u16 idx,
1161 		      u16 mask)
1162 {
1163 	bool expect_no_mask = false;
1164 	bool found = false;
1165 	bool match = false;
1166 	u16 i;
1167 
1168 	/* If mask is 0x0000 or 0xffff, then there is no masking */
1169 	if (mask == 0 || mask == 0xffff)
1170 		expect_no_mask = true;
1171 
1172 	/* Scan the enabled masks on this profile, for the specified idx */
1173 	for (i = hw->blk[blk].masks.first; i < hw->blk[blk].masks.first +
1174 	     hw->blk[blk].masks.count; i++)
1175 		if (hw->blk[blk].es.mask_ena[prof] & BIT(i))
1176 			if (hw->blk[blk].masks.masks[i].in_use &&
1177 			    hw->blk[blk].masks.masks[i].idx == idx) {
1178 				found = true;
1179 				if (hw->blk[blk].masks.masks[i].mask == mask)
1180 					match = true;
1181 				break;
1182 			}
1183 
1184 	if (expect_no_mask) {
1185 		if (found)
1186 			return false;
1187 	} else {
1188 		if (!match)
1189 			return false;
1190 	}
1191 
1192 	return true;
1193 }
1194 
1195 /**
1196  * ice_prof_has_mask - determine if profile masking is identical
1197  * @hw: pointer to the hardware structure
1198  * @blk: HW block
1199  * @prof: profile to check
1200  * @masks: masks to match
1201  */
1202 static bool
1203 ice_prof_has_mask(struct ice_hw *hw, enum ice_block blk, u8 prof, u16 *masks)
1204 {
1205 	u16 i;
1206 
1207 	/* es->mask_ena[prof] will have the mask */
1208 	for (i = 0; i < hw->blk[blk].es.fvw; i++)
1209 		if (!ice_prof_has_mask_idx(hw, blk, prof, i, masks[i]))
1210 			return false;
1211 
1212 	return true;
1213 }
1214 
1215 /**
1216  * ice_find_prof_id_with_mask - find profile ID for a given field vector
1217  * @hw: pointer to the hardware structure
1218  * @blk: HW block
1219  * @fv: field vector to search for
1220  * @masks: masks for FV
1221  * @prof_id: receives the profile ID
1222  */
1223 static int
1224 ice_find_prof_id_with_mask(struct ice_hw *hw, enum ice_block blk,
1225 			   struct ice_fv_word *fv, u16 *masks, u8 *prof_id)
1226 {
1227 	struct ice_es *es = &hw->blk[blk].es;
1228 	u8 i;
1229 
1230 	/* For FD, we don't want to re-use a existed profile with the same
1231 	 * field vector and mask. This will cause rule interference.
1232 	 */
1233 	if (blk == ICE_BLK_FD)
1234 		return -ENOENT;
1235 
1236 	for (i = 0; i < (u8)es->count; i++) {
1237 		u16 off = i * es->fvw;
1238 
1239 		if (memcmp(&es->t[off], fv, es->fvw * sizeof(*fv)))
1240 			continue;
1241 
1242 		/* check if masks settings are the same for this profile */
1243 		if (masks && !ice_prof_has_mask(hw, blk, i, masks))
1244 			continue;
1245 
1246 		*prof_id = i;
1247 		return 0;
1248 	}
1249 
1250 	return -ENOENT;
1251 }
1252 
1253 /**
1254  * ice_prof_id_rsrc_type - get profile ID resource type for a block type
1255  * @blk: the block type
1256  * @rsrc_type: pointer to variable to receive the resource type
1257  */
1258 static bool ice_prof_id_rsrc_type(enum ice_block blk, u16 *rsrc_type)
1259 {
1260 	switch (blk) {
1261 	case ICE_BLK_FD:
1262 		*rsrc_type = ICE_AQC_RES_TYPE_FD_PROF_BLDR_PROFID;
1263 		break;
1264 	case ICE_BLK_RSS:
1265 		*rsrc_type = ICE_AQC_RES_TYPE_HASH_PROF_BLDR_PROFID;
1266 		break;
1267 	default:
1268 		return false;
1269 	}
1270 	return true;
1271 }
1272 
1273 /**
1274  * ice_tcam_ent_rsrc_type - get TCAM entry resource type for a block type
1275  * @blk: the block type
1276  * @rsrc_type: pointer to variable to receive the resource type
1277  */
1278 static bool ice_tcam_ent_rsrc_type(enum ice_block blk, u16 *rsrc_type)
1279 {
1280 	switch (blk) {
1281 	case ICE_BLK_FD:
1282 		*rsrc_type = ICE_AQC_RES_TYPE_FD_PROF_BLDR_TCAM;
1283 		break;
1284 	case ICE_BLK_RSS:
1285 		*rsrc_type = ICE_AQC_RES_TYPE_HASH_PROF_BLDR_TCAM;
1286 		break;
1287 	default:
1288 		return false;
1289 	}
1290 	return true;
1291 }
1292 
1293 /**
1294  * ice_alloc_tcam_ent - allocate hardware TCAM entry
1295  * @hw: pointer to the HW struct
1296  * @blk: the block to allocate the TCAM for
1297  * @btm: true to allocate from bottom of table, false to allocate from top
1298  * @tcam_idx: pointer to variable to receive the TCAM entry
1299  *
1300  * This function allocates a new entry in a Profile ID TCAM for a specific
1301  * block.
1302  */
1303 static int
1304 ice_alloc_tcam_ent(struct ice_hw *hw, enum ice_block blk, bool btm,
1305 		   u16 *tcam_idx)
1306 {
1307 	u16 res_type;
1308 
1309 	if (!ice_tcam_ent_rsrc_type(blk, &res_type))
1310 		return -EINVAL;
1311 
1312 	return ice_alloc_hw_res(hw, res_type, 1, btm, tcam_idx);
1313 }
1314 
1315 /**
1316  * ice_free_tcam_ent - free hardware TCAM entry
1317  * @hw: pointer to the HW struct
1318  * @blk: the block from which to free the TCAM entry
1319  * @tcam_idx: the TCAM entry to free
1320  *
1321  * This function frees an entry in a Profile ID TCAM for a specific block.
1322  */
1323 static int
1324 ice_free_tcam_ent(struct ice_hw *hw, enum ice_block blk, u16 tcam_idx)
1325 {
1326 	u16 res_type;
1327 
1328 	if (!ice_tcam_ent_rsrc_type(blk, &res_type))
1329 		return -EINVAL;
1330 
1331 	return ice_free_hw_res(hw, res_type, 1, &tcam_idx);
1332 }
1333 
1334 /**
1335  * ice_alloc_prof_id - allocate profile ID
1336  * @hw: pointer to the HW struct
1337  * @blk: the block to allocate the profile ID for
1338  * @prof_id: pointer to variable to receive the profile ID
1339  *
1340  * This function allocates a new profile ID, which also corresponds to a Field
1341  * Vector (Extraction Sequence) entry.
1342  */
1343 static int ice_alloc_prof_id(struct ice_hw *hw, enum ice_block blk, u8 *prof_id)
1344 {
1345 	u16 res_type;
1346 	u16 get_prof;
1347 	int status;
1348 
1349 	if (!ice_prof_id_rsrc_type(blk, &res_type))
1350 		return -EINVAL;
1351 
1352 	status = ice_alloc_hw_res(hw, res_type, 1, false, &get_prof);
1353 	if (!status)
1354 		*prof_id = (u8)get_prof;
1355 
1356 	return status;
1357 }
1358 
1359 /**
1360  * ice_free_prof_id - free profile ID
1361  * @hw: pointer to the HW struct
1362  * @blk: the block from which to free the profile ID
1363  * @prof_id: the profile ID to free
1364  *
1365  * This function frees a profile ID, which also corresponds to a Field Vector.
1366  */
1367 static int ice_free_prof_id(struct ice_hw *hw, enum ice_block blk, u8 prof_id)
1368 {
1369 	u16 tmp_prof_id = (u16)prof_id;
1370 	u16 res_type;
1371 
1372 	if (!ice_prof_id_rsrc_type(blk, &res_type))
1373 		return -EINVAL;
1374 
1375 	return ice_free_hw_res(hw, res_type, 1, &tmp_prof_id);
1376 }
1377 
1378 /**
1379  * ice_prof_inc_ref - increment reference count for profile
1380  * @hw: pointer to the HW struct
1381  * @blk: the block from which to free the profile ID
1382  * @prof_id: the profile ID for which to increment the reference count
1383  */
1384 static int ice_prof_inc_ref(struct ice_hw *hw, enum ice_block blk, u8 prof_id)
1385 {
1386 	if (prof_id > hw->blk[blk].es.count)
1387 		return -EINVAL;
1388 
1389 	hw->blk[blk].es.ref_count[prof_id]++;
1390 
1391 	return 0;
1392 }
1393 
1394 /**
1395  * ice_write_prof_mask_reg - write profile mask register
1396  * @hw: pointer to the HW struct
1397  * @blk: hardware block
1398  * @mask_idx: mask index
1399  * @idx: index of the FV which will use the mask
1400  * @mask: the 16-bit mask
1401  */
1402 static void
1403 ice_write_prof_mask_reg(struct ice_hw *hw, enum ice_block blk, u16 mask_idx,
1404 			u16 idx, u16 mask)
1405 {
1406 	u32 offset;
1407 	u32 val;
1408 
1409 	switch (blk) {
1410 	case ICE_BLK_RSS:
1411 		offset = GLQF_HMASK(mask_idx);
1412 		val = (idx << GLQF_HMASK_MSK_INDEX_S) & GLQF_HMASK_MSK_INDEX_M;
1413 		val |= (mask << GLQF_HMASK_MASK_S) & GLQF_HMASK_MASK_M;
1414 		break;
1415 	case ICE_BLK_FD:
1416 		offset = GLQF_FDMASK(mask_idx);
1417 		val = (idx << GLQF_FDMASK_MSK_INDEX_S) & GLQF_FDMASK_MSK_INDEX_M;
1418 		val |= (mask << GLQF_FDMASK_MASK_S) & GLQF_FDMASK_MASK_M;
1419 		break;
1420 	default:
1421 		ice_debug(hw, ICE_DBG_PKG, "No profile masks for block %d\n",
1422 			  blk);
1423 		return;
1424 	}
1425 
1426 	wr32(hw, offset, val);
1427 	ice_debug(hw, ICE_DBG_PKG, "write mask, blk %d (%d): %x = %x\n",
1428 		  blk, idx, offset, val);
1429 }
1430 
1431 /**
1432  * ice_write_prof_mask_enable_res - write profile mask enable register
1433  * @hw: pointer to the HW struct
1434  * @blk: hardware block
1435  * @prof_id: profile ID
1436  * @enable_mask: enable mask
1437  */
1438 static void
1439 ice_write_prof_mask_enable_res(struct ice_hw *hw, enum ice_block blk,
1440 			       u16 prof_id, u32 enable_mask)
1441 {
1442 	u32 offset;
1443 
1444 	switch (blk) {
1445 	case ICE_BLK_RSS:
1446 		offset = GLQF_HMASK_SEL(prof_id);
1447 		break;
1448 	case ICE_BLK_FD:
1449 		offset = GLQF_FDMASK_SEL(prof_id);
1450 		break;
1451 	default:
1452 		ice_debug(hw, ICE_DBG_PKG, "No profile masks for block %d\n",
1453 			  blk);
1454 		return;
1455 	}
1456 
1457 	wr32(hw, offset, enable_mask);
1458 	ice_debug(hw, ICE_DBG_PKG, "write mask enable, blk %d (%d): %x = %x\n",
1459 		  blk, prof_id, offset, enable_mask);
1460 }
1461 
1462 /**
1463  * ice_init_prof_masks - initial prof masks
1464  * @hw: pointer to the HW struct
1465  * @blk: hardware block
1466  */
1467 static void ice_init_prof_masks(struct ice_hw *hw, enum ice_block blk)
1468 {
1469 	u16 per_pf;
1470 	u16 i;
1471 
1472 	mutex_init(&hw->blk[blk].masks.lock);
1473 
1474 	per_pf = ICE_PROF_MASK_COUNT / hw->dev_caps.num_funcs;
1475 
1476 	hw->blk[blk].masks.count = per_pf;
1477 	hw->blk[blk].masks.first = hw->pf_id * per_pf;
1478 
1479 	memset(hw->blk[blk].masks.masks, 0, sizeof(hw->blk[blk].masks.masks));
1480 
1481 	for (i = hw->blk[blk].masks.first;
1482 	     i < hw->blk[blk].masks.first + hw->blk[blk].masks.count; i++)
1483 		ice_write_prof_mask_reg(hw, blk, i, 0, 0);
1484 }
1485 
1486 /**
1487  * ice_init_all_prof_masks - initialize all prof masks
1488  * @hw: pointer to the HW struct
1489  */
1490 static void ice_init_all_prof_masks(struct ice_hw *hw)
1491 {
1492 	ice_init_prof_masks(hw, ICE_BLK_RSS);
1493 	ice_init_prof_masks(hw, ICE_BLK_FD);
1494 }
1495 
1496 /**
1497  * ice_alloc_prof_mask - allocate profile mask
1498  * @hw: pointer to the HW struct
1499  * @blk: hardware block
1500  * @idx: index of FV which will use the mask
1501  * @mask: the 16-bit mask
1502  * @mask_idx: variable to receive the mask index
1503  */
1504 static int
1505 ice_alloc_prof_mask(struct ice_hw *hw, enum ice_block blk, u16 idx, u16 mask,
1506 		    u16 *mask_idx)
1507 {
1508 	bool found_unused = false, found_copy = false;
1509 	u16 unused_idx = 0, copy_idx = 0;
1510 	int status = -ENOSPC;
1511 	u16 i;
1512 
1513 	if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD)
1514 		return -EINVAL;
1515 
1516 	mutex_lock(&hw->blk[blk].masks.lock);
1517 
1518 	for (i = hw->blk[blk].masks.first;
1519 	     i < hw->blk[blk].masks.first + hw->blk[blk].masks.count; i++)
1520 		if (hw->blk[blk].masks.masks[i].in_use) {
1521 			/* if mask is in use and it exactly duplicates the
1522 			 * desired mask and index, then in can be reused
1523 			 */
1524 			if (hw->blk[blk].masks.masks[i].mask == mask &&
1525 			    hw->blk[blk].masks.masks[i].idx == idx) {
1526 				found_copy = true;
1527 				copy_idx = i;
1528 				break;
1529 			}
1530 		} else {
1531 			/* save off unused index, but keep searching in case
1532 			 * there is an exact match later on
1533 			 */
1534 			if (!found_unused) {
1535 				found_unused = true;
1536 				unused_idx = i;
1537 			}
1538 		}
1539 
1540 	if (found_copy)
1541 		i = copy_idx;
1542 	else if (found_unused)
1543 		i = unused_idx;
1544 	else
1545 		goto err_ice_alloc_prof_mask;
1546 
1547 	/* update mask for a new entry */
1548 	if (found_unused) {
1549 		hw->blk[blk].masks.masks[i].in_use = true;
1550 		hw->blk[blk].masks.masks[i].mask = mask;
1551 		hw->blk[blk].masks.masks[i].idx = idx;
1552 		hw->blk[blk].masks.masks[i].ref = 0;
1553 		ice_write_prof_mask_reg(hw, blk, i, idx, mask);
1554 	}
1555 
1556 	hw->blk[blk].masks.masks[i].ref++;
1557 	*mask_idx = i;
1558 	status = 0;
1559 
1560 err_ice_alloc_prof_mask:
1561 	mutex_unlock(&hw->blk[blk].masks.lock);
1562 
1563 	return status;
1564 }
1565 
1566 /**
1567  * ice_free_prof_mask - free profile mask
1568  * @hw: pointer to the HW struct
1569  * @blk: hardware block
1570  * @mask_idx: index of mask
1571  */
1572 static int
1573 ice_free_prof_mask(struct ice_hw *hw, enum ice_block blk, u16 mask_idx)
1574 {
1575 	if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD)
1576 		return -EINVAL;
1577 
1578 	if (!(mask_idx >= hw->blk[blk].masks.first &&
1579 	      mask_idx < hw->blk[blk].masks.first + hw->blk[blk].masks.count))
1580 		return -ENOENT;
1581 
1582 	mutex_lock(&hw->blk[blk].masks.lock);
1583 
1584 	if (!hw->blk[blk].masks.masks[mask_idx].in_use)
1585 		goto exit_ice_free_prof_mask;
1586 
1587 	if (hw->blk[blk].masks.masks[mask_idx].ref > 1) {
1588 		hw->blk[blk].masks.masks[mask_idx].ref--;
1589 		goto exit_ice_free_prof_mask;
1590 	}
1591 
1592 	/* remove mask */
1593 	hw->blk[blk].masks.masks[mask_idx].in_use = false;
1594 	hw->blk[blk].masks.masks[mask_idx].mask = 0;
1595 	hw->blk[blk].masks.masks[mask_idx].idx = 0;
1596 
1597 	/* update mask as unused entry */
1598 	ice_debug(hw, ICE_DBG_PKG, "Free mask, blk %d, mask %d\n", blk,
1599 		  mask_idx);
1600 	ice_write_prof_mask_reg(hw, blk, mask_idx, 0, 0);
1601 
1602 exit_ice_free_prof_mask:
1603 	mutex_unlock(&hw->blk[blk].masks.lock);
1604 
1605 	return 0;
1606 }
1607 
1608 /**
1609  * ice_free_prof_masks - free all profile masks for a profile
1610  * @hw: pointer to the HW struct
1611  * @blk: hardware block
1612  * @prof_id: profile ID
1613  */
1614 static int
1615 ice_free_prof_masks(struct ice_hw *hw, enum ice_block blk, u16 prof_id)
1616 {
1617 	u32 mask_bm;
1618 	u16 i;
1619 
1620 	if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD)
1621 		return -EINVAL;
1622 
1623 	mask_bm = hw->blk[blk].es.mask_ena[prof_id];
1624 	for (i = 0; i < BITS_PER_BYTE * sizeof(mask_bm); i++)
1625 		if (mask_bm & BIT(i))
1626 			ice_free_prof_mask(hw, blk, i);
1627 
1628 	return 0;
1629 }
1630 
1631 /**
1632  * ice_shutdown_prof_masks - releases lock for masking
1633  * @hw: pointer to the HW struct
1634  * @blk: hardware block
1635  *
1636  * This should be called before unloading the driver
1637  */
1638 static void ice_shutdown_prof_masks(struct ice_hw *hw, enum ice_block blk)
1639 {
1640 	u16 i;
1641 
1642 	mutex_lock(&hw->blk[blk].masks.lock);
1643 
1644 	for (i = hw->blk[blk].masks.first;
1645 	     i < hw->blk[blk].masks.first + hw->blk[blk].masks.count; i++) {
1646 		ice_write_prof_mask_reg(hw, blk, i, 0, 0);
1647 
1648 		hw->blk[blk].masks.masks[i].in_use = false;
1649 		hw->blk[blk].masks.masks[i].idx = 0;
1650 		hw->blk[blk].masks.masks[i].mask = 0;
1651 	}
1652 
1653 	mutex_unlock(&hw->blk[blk].masks.lock);
1654 	mutex_destroy(&hw->blk[blk].masks.lock);
1655 }
1656 
1657 /**
1658  * ice_shutdown_all_prof_masks - releases all locks for masking
1659  * @hw: pointer to the HW struct
1660  *
1661  * This should be called before unloading the driver
1662  */
1663 static void ice_shutdown_all_prof_masks(struct ice_hw *hw)
1664 {
1665 	ice_shutdown_prof_masks(hw, ICE_BLK_RSS);
1666 	ice_shutdown_prof_masks(hw, ICE_BLK_FD);
1667 }
1668 
1669 /**
1670  * ice_update_prof_masking - set registers according to masking
1671  * @hw: pointer to the HW struct
1672  * @blk: hardware block
1673  * @prof_id: profile ID
1674  * @masks: masks
1675  */
1676 static int
1677 ice_update_prof_masking(struct ice_hw *hw, enum ice_block blk, u16 prof_id,
1678 			u16 *masks)
1679 {
1680 	bool err = false;
1681 	u32 ena_mask = 0;
1682 	u16 idx;
1683 	u16 i;
1684 
1685 	/* Only support FD and RSS masking, otherwise nothing to be done */
1686 	if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD)
1687 		return 0;
1688 
1689 	for (i = 0; i < hw->blk[blk].es.fvw; i++)
1690 		if (masks[i] && masks[i] != 0xFFFF) {
1691 			if (!ice_alloc_prof_mask(hw, blk, i, masks[i], &idx)) {
1692 				ena_mask |= BIT(idx);
1693 			} else {
1694 				/* not enough bitmaps */
1695 				err = true;
1696 				break;
1697 			}
1698 		}
1699 
1700 	if (err) {
1701 		/* free any bitmaps we have allocated */
1702 		for (i = 0; i < BITS_PER_BYTE * sizeof(ena_mask); i++)
1703 			if (ena_mask & BIT(i))
1704 				ice_free_prof_mask(hw, blk, i);
1705 
1706 		return -EIO;
1707 	}
1708 
1709 	/* enable the masks for this profile */
1710 	ice_write_prof_mask_enable_res(hw, blk, prof_id, ena_mask);
1711 
1712 	/* store enabled masks with profile so that they can be freed later */
1713 	hw->blk[blk].es.mask_ena[prof_id] = ena_mask;
1714 
1715 	return 0;
1716 }
1717 
1718 /**
1719  * ice_write_es - write an extraction sequence to hardware
1720  * @hw: pointer to the HW struct
1721  * @blk: the block in which to write the extraction sequence
1722  * @prof_id: the profile ID to write
1723  * @fv: pointer to the extraction sequence to write - NULL to clear extraction
1724  */
1725 static void
1726 ice_write_es(struct ice_hw *hw, enum ice_block blk, u8 prof_id,
1727 	     struct ice_fv_word *fv)
1728 {
1729 	u16 off;
1730 
1731 	off = prof_id * hw->blk[blk].es.fvw;
1732 	if (!fv) {
1733 		memset(&hw->blk[blk].es.t[off], 0,
1734 		       hw->blk[blk].es.fvw * sizeof(*fv));
1735 		hw->blk[blk].es.written[prof_id] = false;
1736 	} else {
1737 		memcpy(&hw->blk[blk].es.t[off], fv,
1738 		       hw->blk[blk].es.fvw * sizeof(*fv));
1739 	}
1740 }
1741 
1742 /**
1743  * ice_prof_dec_ref - decrement reference count for profile
1744  * @hw: pointer to the HW struct
1745  * @blk: the block from which to free the profile ID
1746  * @prof_id: the profile ID for which to decrement the reference count
1747  */
1748 static int
1749 ice_prof_dec_ref(struct ice_hw *hw, enum ice_block blk, u8 prof_id)
1750 {
1751 	if (prof_id > hw->blk[blk].es.count)
1752 		return -EINVAL;
1753 
1754 	if (hw->blk[blk].es.ref_count[prof_id] > 0) {
1755 		if (!--hw->blk[blk].es.ref_count[prof_id]) {
1756 			ice_write_es(hw, blk, prof_id, NULL);
1757 			ice_free_prof_masks(hw, blk, prof_id);
1758 			return ice_free_prof_id(hw, blk, prof_id);
1759 		}
1760 	}
1761 
1762 	return 0;
1763 }
1764 
1765 /* Block / table section IDs */
1766 static const u32 ice_blk_sids[ICE_BLK_COUNT][ICE_SID_OFF_COUNT] = {
1767 	/* SWITCH */
1768 	{	ICE_SID_XLT1_SW,
1769 		ICE_SID_XLT2_SW,
1770 		ICE_SID_PROFID_TCAM_SW,
1771 		ICE_SID_PROFID_REDIR_SW,
1772 		ICE_SID_FLD_VEC_SW
1773 	},
1774 
1775 	/* ACL */
1776 	{	ICE_SID_XLT1_ACL,
1777 		ICE_SID_XLT2_ACL,
1778 		ICE_SID_PROFID_TCAM_ACL,
1779 		ICE_SID_PROFID_REDIR_ACL,
1780 		ICE_SID_FLD_VEC_ACL
1781 	},
1782 
1783 	/* FD */
1784 	{	ICE_SID_XLT1_FD,
1785 		ICE_SID_XLT2_FD,
1786 		ICE_SID_PROFID_TCAM_FD,
1787 		ICE_SID_PROFID_REDIR_FD,
1788 		ICE_SID_FLD_VEC_FD
1789 	},
1790 
1791 	/* RSS */
1792 	{	ICE_SID_XLT1_RSS,
1793 		ICE_SID_XLT2_RSS,
1794 		ICE_SID_PROFID_TCAM_RSS,
1795 		ICE_SID_PROFID_REDIR_RSS,
1796 		ICE_SID_FLD_VEC_RSS
1797 	},
1798 
1799 	/* PE */
1800 	{	ICE_SID_XLT1_PE,
1801 		ICE_SID_XLT2_PE,
1802 		ICE_SID_PROFID_TCAM_PE,
1803 		ICE_SID_PROFID_REDIR_PE,
1804 		ICE_SID_FLD_VEC_PE
1805 	}
1806 };
1807 
1808 /**
1809  * ice_init_sw_xlt1_db - init software XLT1 database from HW tables
1810  * @hw: pointer to the hardware structure
1811  * @blk: the HW block to initialize
1812  */
1813 static void ice_init_sw_xlt1_db(struct ice_hw *hw, enum ice_block blk)
1814 {
1815 	u16 pt;
1816 
1817 	for (pt = 0; pt < hw->blk[blk].xlt1.count; pt++) {
1818 		u8 ptg;
1819 
1820 		ptg = hw->blk[blk].xlt1.t[pt];
1821 		if (ptg != ICE_DEFAULT_PTG) {
1822 			ice_ptg_alloc_val(hw, blk, ptg);
1823 			ice_ptg_add_mv_ptype(hw, blk, pt, ptg);
1824 		}
1825 	}
1826 }
1827 
1828 /**
1829  * ice_init_sw_xlt2_db - init software XLT2 database from HW tables
1830  * @hw: pointer to the hardware structure
1831  * @blk: the HW block to initialize
1832  */
1833 static void ice_init_sw_xlt2_db(struct ice_hw *hw, enum ice_block blk)
1834 {
1835 	u16 vsi;
1836 
1837 	for (vsi = 0; vsi < hw->blk[blk].xlt2.count; vsi++) {
1838 		u16 vsig;
1839 
1840 		vsig = hw->blk[blk].xlt2.t[vsi];
1841 		if (vsig) {
1842 			ice_vsig_alloc_val(hw, blk, vsig);
1843 			ice_vsig_add_mv_vsi(hw, blk, vsi, vsig);
1844 			/* no changes at this time, since this has been
1845 			 * initialized from the original package
1846 			 */
1847 			hw->blk[blk].xlt2.vsis[vsi].changed = 0;
1848 		}
1849 	}
1850 }
1851 
1852 /**
1853  * ice_init_sw_db - init software database from HW tables
1854  * @hw: pointer to the hardware structure
1855  */
1856 static void ice_init_sw_db(struct ice_hw *hw)
1857 {
1858 	u16 i;
1859 
1860 	for (i = 0; i < ICE_BLK_COUNT; i++) {
1861 		ice_init_sw_xlt1_db(hw, (enum ice_block)i);
1862 		ice_init_sw_xlt2_db(hw, (enum ice_block)i);
1863 	}
1864 }
1865 
1866 /**
1867  * ice_fill_tbl - Reads content of a single table type into database
1868  * @hw: pointer to the hardware structure
1869  * @block_id: Block ID of the table to copy
1870  * @sid: Section ID of the table to copy
1871  *
1872  * Will attempt to read the entire content of a given table of a single block
1873  * into the driver database. We assume that the buffer will always
1874  * be as large or larger than the data contained in the package. If
1875  * this condition is not met, there is most likely an error in the package
1876  * contents.
1877  */
1878 static void ice_fill_tbl(struct ice_hw *hw, enum ice_block block_id, u32 sid)
1879 {
1880 	u32 dst_len, sect_len, offset = 0;
1881 	struct ice_prof_redir_section *pr;
1882 	struct ice_prof_id_section *pid;
1883 	struct ice_xlt1_section *xlt1;
1884 	struct ice_xlt2_section *xlt2;
1885 	struct ice_sw_fv_section *es;
1886 	struct ice_pkg_enum state;
1887 	u8 *src, *dst;
1888 	void *sect;
1889 
1890 	/* if the HW segment pointer is null then the first iteration of
1891 	 * ice_pkg_enum_section() will fail. In this case the HW tables will
1892 	 * not be filled and return success.
1893 	 */
1894 	if (!hw->seg) {
1895 		ice_debug(hw, ICE_DBG_PKG, "hw->seg is NULL, tables are not filled\n");
1896 		return;
1897 	}
1898 
1899 	memset(&state, 0, sizeof(state));
1900 
1901 	sect = ice_pkg_enum_section(hw->seg, &state, sid);
1902 
1903 	while (sect) {
1904 		switch (sid) {
1905 		case ICE_SID_XLT1_SW:
1906 		case ICE_SID_XLT1_FD:
1907 		case ICE_SID_XLT1_RSS:
1908 		case ICE_SID_XLT1_ACL:
1909 		case ICE_SID_XLT1_PE:
1910 			xlt1 = sect;
1911 			src = xlt1->value;
1912 			sect_len = le16_to_cpu(xlt1->count) *
1913 				sizeof(*hw->blk[block_id].xlt1.t);
1914 			dst = hw->blk[block_id].xlt1.t;
1915 			dst_len = hw->blk[block_id].xlt1.count *
1916 				sizeof(*hw->blk[block_id].xlt1.t);
1917 			break;
1918 		case ICE_SID_XLT2_SW:
1919 		case ICE_SID_XLT2_FD:
1920 		case ICE_SID_XLT2_RSS:
1921 		case ICE_SID_XLT2_ACL:
1922 		case ICE_SID_XLT2_PE:
1923 			xlt2 = sect;
1924 			src = (__force u8 *)xlt2->value;
1925 			sect_len = le16_to_cpu(xlt2->count) *
1926 				sizeof(*hw->blk[block_id].xlt2.t);
1927 			dst = (u8 *)hw->blk[block_id].xlt2.t;
1928 			dst_len = hw->blk[block_id].xlt2.count *
1929 				sizeof(*hw->blk[block_id].xlt2.t);
1930 			break;
1931 		case ICE_SID_PROFID_TCAM_SW:
1932 		case ICE_SID_PROFID_TCAM_FD:
1933 		case ICE_SID_PROFID_TCAM_RSS:
1934 		case ICE_SID_PROFID_TCAM_ACL:
1935 		case ICE_SID_PROFID_TCAM_PE:
1936 			pid = sect;
1937 			src = (u8 *)pid->entry;
1938 			sect_len = le16_to_cpu(pid->count) *
1939 				sizeof(*hw->blk[block_id].prof.t);
1940 			dst = (u8 *)hw->blk[block_id].prof.t;
1941 			dst_len = hw->blk[block_id].prof.count *
1942 				sizeof(*hw->blk[block_id].prof.t);
1943 			break;
1944 		case ICE_SID_PROFID_REDIR_SW:
1945 		case ICE_SID_PROFID_REDIR_FD:
1946 		case ICE_SID_PROFID_REDIR_RSS:
1947 		case ICE_SID_PROFID_REDIR_ACL:
1948 		case ICE_SID_PROFID_REDIR_PE:
1949 			pr = sect;
1950 			src = pr->redir_value;
1951 			sect_len = le16_to_cpu(pr->count) *
1952 				sizeof(*hw->blk[block_id].prof_redir.t);
1953 			dst = hw->blk[block_id].prof_redir.t;
1954 			dst_len = hw->blk[block_id].prof_redir.count *
1955 				sizeof(*hw->blk[block_id].prof_redir.t);
1956 			break;
1957 		case ICE_SID_FLD_VEC_SW:
1958 		case ICE_SID_FLD_VEC_FD:
1959 		case ICE_SID_FLD_VEC_RSS:
1960 		case ICE_SID_FLD_VEC_ACL:
1961 		case ICE_SID_FLD_VEC_PE:
1962 			es = sect;
1963 			src = (u8 *)es->fv;
1964 			sect_len = (u32)(le16_to_cpu(es->count) *
1965 					 hw->blk[block_id].es.fvw) *
1966 				sizeof(*hw->blk[block_id].es.t);
1967 			dst = (u8 *)hw->blk[block_id].es.t;
1968 			dst_len = (u32)(hw->blk[block_id].es.count *
1969 					hw->blk[block_id].es.fvw) *
1970 				sizeof(*hw->blk[block_id].es.t);
1971 			break;
1972 		default:
1973 			return;
1974 		}
1975 
1976 		/* if the section offset exceeds destination length, terminate
1977 		 * table fill.
1978 		 */
1979 		if (offset > dst_len)
1980 			return;
1981 
1982 		/* if the sum of section size and offset exceed destination size
1983 		 * then we are out of bounds of the HW table size for that PF.
1984 		 * Changing section length to fill the remaining table space
1985 		 * of that PF.
1986 		 */
1987 		if ((offset + sect_len) > dst_len)
1988 			sect_len = dst_len - offset;
1989 
1990 		memcpy(dst + offset, src, sect_len);
1991 		offset += sect_len;
1992 		sect = ice_pkg_enum_section(NULL, &state, sid);
1993 	}
1994 }
1995 
1996 /**
1997  * ice_fill_blk_tbls - Read package context for tables
1998  * @hw: pointer to the hardware structure
1999  *
2000  * Reads the current package contents and populates the driver
2001  * database with the data iteratively for all advanced feature
2002  * blocks. Assume that the HW tables have been allocated.
2003  */
2004 void ice_fill_blk_tbls(struct ice_hw *hw)
2005 {
2006 	u8 i;
2007 
2008 	for (i = 0; i < ICE_BLK_COUNT; i++) {
2009 		enum ice_block blk_id = (enum ice_block)i;
2010 
2011 		ice_fill_tbl(hw, blk_id, hw->blk[blk_id].xlt1.sid);
2012 		ice_fill_tbl(hw, blk_id, hw->blk[blk_id].xlt2.sid);
2013 		ice_fill_tbl(hw, blk_id, hw->blk[blk_id].prof.sid);
2014 		ice_fill_tbl(hw, blk_id, hw->blk[blk_id].prof_redir.sid);
2015 		ice_fill_tbl(hw, blk_id, hw->blk[blk_id].es.sid);
2016 	}
2017 
2018 	ice_init_sw_db(hw);
2019 }
2020 
2021 /**
2022  * ice_free_prof_map - free profile map
2023  * @hw: pointer to the hardware structure
2024  * @blk_idx: HW block index
2025  */
2026 static void ice_free_prof_map(struct ice_hw *hw, u8 blk_idx)
2027 {
2028 	struct ice_es *es = &hw->blk[blk_idx].es;
2029 	struct ice_prof_map *del, *tmp;
2030 
2031 	mutex_lock(&es->prof_map_lock);
2032 	list_for_each_entry_safe(del, tmp, &es->prof_map, list) {
2033 		list_del(&del->list);
2034 		devm_kfree(ice_hw_to_dev(hw), del);
2035 	}
2036 	INIT_LIST_HEAD(&es->prof_map);
2037 	mutex_unlock(&es->prof_map_lock);
2038 }
2039 
2040 /**
2041  * ice_free_flow_profs - free flow profile entries
2042  * @hw: pointer to the hardware structure
2043  * @blk_idx: HW block index
2044  */
2045 static void ice_free_flow_profs(struct ice_hw *hw, u8 blk_idx)
2046 {
2047 	struct ice_flow_prof *p, *tmp;
2048 
2049 	mutex_lock(&hw->fl_profs_locks[blk_idx]);
2050 	list_for_each_entry_safe(p, tmp, &hw->fl_profs[blk_idx], l_entry) {
2051 		struct ice_flow_entry *e, *t;
2052 
2053 		list_for_each_entry_safe(e, t, &p->entries, l_entry)
2054 			ice_flow_rem_entry(hw, (enum ice_block)blk_idx,
2055 					   ICE_FLOW_ENTRY_HNDL(e));
2056 
2057 		list_del(&p->l_entry);
2058 
2059 		mutex_destroy(&p->entries_lock);
2060 		devm_kfree(ice_hw_to_dev(hw), p);
2061 	}
2062 	mutex_unlock(&hw->fl_profs_locks[blk_idx]);
2063 
2064 	/* if driver is in reset and tables are being cleared
2065 	 * re-initialize the flow profile list heads
2066 	 */
2067 	INIT_LIST_HEAD(&hw->fl_profs[blk_idx]);
2068 }
2069 
2070 /**
2071  * ice_free_vsig_tbl - free complete VSIG table entries
2072  * @hw: pointer to the hardware structure
2073  * @blk: the HW block on which to free the VSIG table entries
2074  */
2075 static void ice_free_vsig_tbl(struct ice_hw *hw, enum ice_block blk)
2076 {
2077 	u16 i;
2078 
2079 	if (!hw->blk[blk].xlt2.vsig_tbl)
2080 		return;
2081 
2082 	for (i = 1; i < ICE_MAX_VSIGS; i++)
2083 		if (hw->blk[blk].xlt2.vsig_tbl[i].in_use)
2084 			ice_vsig_free(hw, blk, i);
2085 }
2086 
2087 /**
2088  * ice_free_hw_tbls - free hardware table memory
2089  * @hw: pointer to the hardware structure
2090  */
2091 void ice_free_hw_tbls(struct ice_hw *hw)
2092 {
2093 	struct ice_rss_cfg *r, *rt;
2094 	u8 i;
2095 
2096 	for (i = 0; i < ICE_BLK_COUNT; i++) {
2097 		if (hw->blk[i].is_list_init) {
2098 			struct ice_es *es = &hw->blk[i].es;
2099 
2100 			ice_free_prof_map(hw, i);
2101 			mutex_destroy(&es->prof_map_lock);
2102 
2103 			ice_free_flow_profs(hw, i);
2104 			mutex_destroy(&hw->fl_profs_locks[i]);
2105 
2106 			hw->blk[i].is_list_init = false;
2107 		}
2108 		ice_free_vsig_tbl(hw, (enum ice_block)i);
2109 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt1.ptypes);
2110 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt1.ptg_tbl);
2111 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt1.t);
2112 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt2.t);
2113 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt2.vsig_tbl);
2114 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt2.vsis);
2115 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].prof.t);
2116 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].prof_redir.t);
2117 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].es.t);
2118 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].es.ref_count);
2119 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].es.written);
2120 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].es.mask_ena);
2121 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].prof_id.id);
2122 	}
2123 
2124 	list_for_each_entry_safe(r, rt, &hw->rss_list_head, l_entry) {
2125 		list_del(&r->l_entry);
2126 		devm_kfree(ice_hw_to_dev(hw), r);
2127 	}
2128 	mutex_destroy(&hw->rss_locks);
2129 	ice_shutdown_all_prof_masks(hw);
2130 	memset(hw->blk, 0, sizeof(hw->blk));
2131 }
2132 
2133 /**
2134  * ice_init_flow_profs - init flow profile locks and list heads
2135  * @hw: pointer to the hardware structure
2136  * @blk_idx: HW block index
2137  */
2138 static void ice_init_flow_profs(struct ice_hw *hw, u8 blk_idx)
2139 {
2140 	mutex_init(&hw->fl_profs_locks[blk_idx]);
2141 	INIT_LIST_HEAD(&hw->fl_profs[blk_idx]);
2142 }
2143 
2144 /**
2145  * ice_clear_hw_tbls - clear HW tables and flow profiles
2146  * @hw: pointer to the hardware structure
2147  */
2148 void ice_clear_hw_tbls(struct ice_hw *hw)
2149 {
2150 	u8 i;
2151 
2152 	for (i = 0; i < ICE_BLK_COUNT; i++) {
2153 		struct ice_prof_redir *prof_redir = &hw->blk[i].prof_redir;
2154 		struct ice_prof_id *prof_id = &hw->blk[i].prof_id;
2155 		struct ice_prof_tcam *prof = &hw->blk[i].prof;
2156 		struct ice_xlt1 *xlt1 = &hw->blk[i].xlt1;
2157 		struct ice_xlt2 *xlt2 = &hw->blk[i].xlt2;
2158 		struct ice_es *es = &hw->blk[i].es;
2159 
2160 		if (hw->blk[i].is_list_init) {
2161 			ice_free_prof_map(hw, i);
2162 			ice_free_flow_profs(hw, i);
2163 		}
2164 
2165 		ice_free_vsig_tbl(hw, (enum ice_block)i);
2166 
2167 		memset(xlt1->ptypes, 0, xlt1->count * sizeof(*xlt1->ptypes));
2168 		memset(xlt1->ptg_tbl, 0,
2169 		       ICE_MAX_PTGS * sizeof(*xlt1->ptg_tbl));
2170 		memset(xlt1->t, 0, xlt1->count * sizeof(*xlt1->t));
2171 
2172 		memset(xlt2->vsis, 0, xlt2->count * sizeof(*xlt2->vsis));
2173 		memset(xlt2->vsig_tbl, 0,
2174 		       xlt2->count * sizeof(*xlt2->vsig_tbl));
2175 		memset(xlt2->t, 0, xlt2->count * sizeof(*xlt2->t));
2176 
2177 		memset(prof->t, 0, prof->count * sizeof(*prof->t));
2178 		memset(prof_redir->t, 0,
2179 		       prof_redir->count * sizeof(*prof_redir->t));
2180 
2181 		memset(es->t, 0, es->count * sizeof(*es->t) * es->fvw);
2182 		memset(es->ref_count, 0, es->count * sizeof(*es->ref_count));
2183 		memset(es->written, 0, es->count * sizeof(*es->written));
2184 		memset(es->mask_ena, 0, es->count * sizeof(*es->mask_ena));
2185 
2186 		memset(prof_id->id, 0, prof_id->count * sizeof(*prof_id->id));
2187 	}
2188 }
2189 
2190 /**
2191  * ice_init_hw_tbls - init hardware table memory
2192  * @hw: pointer to the hardware structure
2193  */
2194 int ice_init_hw_tbls(struct ice_hw *hw)
2195 {
2196 	u8 i;
2197 
2198 	mutex_init(&hw->rss_locks);
2199 	INIT_LIST_HEAD(&hw->rss_list_head);
2200 	ice_init_all_prof_masks(hw);
2201 	for (i = 0; i < ICE_BLK_COUNT; i++) {
2202 		struct ice_prof_redir *prof_redir = &hw->blk[i].prof_redir;
2203 		struct ice_prof_id *prof_id = &hw->blk[i].prof_id;
2204 		struct ice_prof_tcam *prof = &hw->blk[i].prof;
2205 		struct ice_xlt1 *xlt1 = &hw->blk[i].xlt1;
2206 		struct ice_xlt2 *xlt2 = &hw->blk[i].xlt2;
2207 		struct ice_es *es = &hw->blk[i].es;
2208 		u16 j;
2209 
2210 		if (hw->blk[i].is_list_init)
2211 			continue;
2212 
2213 		ice_init_flow_profs(hw, i);
2214 		mutex_init(&es->prof_map_lock);
2215 		INIT_LIST_HEAD(&es->prof_map);
2216 		hw->blk[i].is_list_init = true;
2217 
2218 		hw->blk[i].overwrite = blk_sizes[i].overwrite;
2219 		es->reverse = blk_sizes[i].reverse;
2220 
2221 		xlt1->sid = ice_blk_sids[i][ICE_SID_XLT1_OFF];
2222 		xlt1->count = blk_sizes[i].xlt1;
2223 
2224 		xlt1->ptypes = devm_kcalloc(ice_hw_to_dev(hw), xlt1->count,
2225 					    sizeof(*xlt1->ptypes), GFP_KERNEL);
2226 
2227 		if (!xlt1->ptypes)
2228 			goto err;
2229 
2230 		xlt1->ptg_tbl = devm_kcalloc(ice_hw_to_dev(hw), ICE_MAX_PTGS,
2231 					     sizeof(*xlt1->ptg_tbl),
2232 					     GFP_KERNEL);
2233 
2234 		if (!xlt1->ptg_tbl)
2235 			goto err;
2236 
2237 		xlt1->t = devm_kcalloc(ice_hw_to_dev(hw), xlt1->count,
2238 				       sizeof(*xlt1->t), GFP_KERNEL);
2239 		if (!xlt1->t)
2240 			goto err;
2241 
2242 		xlt2->sid = ice_blk_sids[i][ICE_SID_XLT2_OFF];
2243 		xlt2->count = blk_sizes[i].xlt2;
2244 
2245 		xlt2->vsis = devm_kcalloc(ice_hw_to_dev(hw), xlt2->count,
2246 					  sizeof(*xlt2->vsis), GFP_KERNEL);
2247 
2248 		if (!xlt2->vsis)
2249 			goto err;
2250 
2251 		xlt2->vsig_tbl = devm_kcalloc(ice_hw_to_dev(hw), xlt2->count,
2252 					      sizeof(*xlt2->vsig_tbl),
2253 					      GFP_KERNEL);
2254 		if (!xlt2->vsig_tbl)
2255 			goto err;
2256 
2257 		for (j = 0; j < xlt2->count; j++)
2258 			INIT_LIST_HEAD(&xlt2->vsig_tbl[j].prop_lst);
2259 
2260 		xlt2->t = devm_kcalloc(ice_hw_to_dev(hw), xlt2->count,
2261 				       sizeof(*xlt2->t), GFP_KERNEL);
2262 		if (!xlt2->t)
2263 			goto err;
2264 
2265 		prof->sid = ice_blk_sids[i][ICE_SID_PR_OFF];
2266 		prof->count = blk_sizes[i].prof_tcam;
2267 		prof->max_prof_id = blk_sizes[i].prof_id;
2268 		prof->cdid_bits = blk_sizes[i].prof_cdid_bits;
2269 		prof->t = devm_kcalloc(ice_hw_to_dev(hw), prof->count,
2270 				       sizeof(*prof->t), GFP_KERNEL);
2271 
2272 		if (!prof->t)
2273 			goto err;
2274 
2275 		prof_redir->sid = ice_blk_sids[i][ICE_SID_PR_REDIR_OFF];
2276 		prof_redir->count = blk_sizes[i].prof_redir;
2277 		prof_redir->t = devm_kcalloc(ice_hw_to_dev(hw),
2278 					     prof_redir->count,
2279 					     sizeof(*prof_redir->t),
2280 					     GFP_KERNEL);
2281 
2282 		if (!prof_redir->t)
2283 			goto err;
2284 
2285 		es->sid = ice_blk_sids[i][ICE_SID_ES_OFF];
2286 		es->count = blk_sizes[i].es;
2287 		es->fvw = blk_sizes[i].fvw;
2288 		es->t = devm_kcalloc(ice_hw_to_dev(hw),
2289 				     (u32)(es->count * es->fvw),
2290 				     sizeof(*es->t), GFP_KERNEL);
2291 		if (!es->t)
2292 			goto err;
2293 
2294 		es->ref_count = devm_kcalloc(ice_hw_to_dev(hw), es->count,
2295 					     sizeof(*es->ref_count),
2296 					     GFP_KERNEL);
2297 		if (!es->ref_count)
2298 			goto err;
2299 
2300 		es->written = devm_kcalloc(ice_hw_to_dev(hw), es->count,
2301 					   sizeof(*es->written), GFP_KERNEL);
2302 		if (!es->written)
2303 			goto err;
2304 
2305 		es->mask_ena = devm_kcalloc(ice_hw_to_dev(hw), es->count,
2306 					    sizeof(*es->mask_ena), GFP_KERNEL);
2307 		if (!es->mask_ena)
2308 			goto err;
2309 
2310 		prof_id->count = blk_sizes[i].prof_id;
2311 		prof_id->id = devm_kcalloc(ice_hw_to_dev(hw), prof_id->count,
2312 					   sizeof(*prof_id->id), GFP_KERNEL);
2313 		if (!prof_id->id)
2314 			goto err;
2315 	}
2316 	return 0;
2317 
2318 err:
2319 	ice_free_hw_tbls(hw);
2320 	return -ENOMEM;
2321 }
2322 
2323 /**
2324  * ice_prof_gen_key - generate profile ID key
2325  * @hw: pointer to the HW struct
2326  * @blk: the block in which to write profile ID to
2327  * @ptg: packet type group (PTG) portion of key
2328  * @vsig: VSIG portion of key
2329  * @cdid: CDID portion of key
2330  * @flags: flag portion of key
2331  * @vl_msk: valid mask
2332  * @dc_msk: don't care mask
2333  * @nm_msk: never match mask
2334  * @key: output of profile ID key
2335  */
2336 static int
2337 ice_prof_gen_key(struct ice_hw *hw, enum ice_block blk, u8 ptg, u16 vsig,
2338 		 u8 cdid, u16 flags, u8 vl_msk[ICE_TCAM_KEY_VAL_SZ],
2339 		 u8 dc_msk[ICE_TCAM_KEY_VAL_SZ], u8 nm_msk[ICE_TCAM_KEY_VAL_SZ],
2340 		 u8 key[ICE_TCAM_KEY_SZ])
2341 {
2342 	struct ice_prof_id_key inkey;
2343 
2344 	inkey.xlt1 = ptg;
2345 	inkey.xlt2_cdid = cpu_to_le16(vsig);
2346 	inkey.flags = cpu_to_le16(flags);
2347 
2348 	switch (hw->blk[blk].prof.cdid_bits) {
2349 	case 0:
2350 		break;
2351 	case 2:
2352 #define ICE_CD_2_M 0xC000U
2353 #define ICE_CD_2_S 14
2354 		inkey.xlt2_cdid &= ~cpu_to_le16(ICE_CD_2_M);
2355 		inkey.xlt2_cdid |= cpu_to_le16(BIT(cdid) << ICE_CD_2_S);
2356 		break;
2357 	case 4:
2358 #define ICE_CD_4_M 0xF000U
2359 #define ICE_CD_4_S 12
2360 		inkey.xlt2_cdid &= ~cpu_to_le16(ICE_CD_4_M);
2361 		inkey.xlt2_cdid |= cpu_to_le16(BIT(cdid) << ICE_CD_4_S);
2362 		break;
2363 	case 8:
2364 #define ICE_CD_8_M 0xFF00U
2365 #define ICE_CD_8_S 16
2366 		inkey.xlt2_cdid &= ~cpu_to_le16(ICE_CD_8_M);
2367 		inkey.xlt2_cdid |= cpu_to_le16(BIT(cdid) << ICE_CD_8_S);
2368 		break;
2369 	default:
2370 		ice_debug(hw, ICE_DBG_PKG, "Error in profile config\n");
2371 		break;
2372 	}
2373 
2374 	return ice_set_key(key, ICE_TCAM_KEY_SZ, (u8 *)&inkey, vl_msk, dc_msk,
2375 			   nm_msk, 0, ICE_TCAM_KEY_SZ / 2);
2376 }
2377 
2378 /**
2379  * ice_tcam_write_entry - write TCAM entry
2380  * @hw: pointer to the HW struct
2381  * @blk: the block in which to write profile ID to
2382  * @idx: the entry index to write to
2383  * @prof_id: profile ID
2384  * @ptg: packet type group (PTG) portion of key
2385  * @vsig: VSIG portion of key
2386  * @cdid: CDID portion of key
2387  * @flags: flag portion of key
2388  * @vl_msk: valid mask
2389  * @dc_msk: don't care mask
2390  * @nm_msk: never match mask
2391  */
2392 static int
2393 ice_tcam_write_entry(struct ice_hw *hw, enum ice_block blk, u16 idx,
2394 		     u8 prof_id, u8 ptg, u16 vsig, u8 cdid, u16 flags,
2395 		     u8 vl_msk[ICE_TCAM_KEY_VAL_SZ],
2396 		     u8 dc_msk[ICE_TCAM_KEY_VAL_SZ],
2397 		     u8 nm_msk[ICE_TCAM_KEY_VAL_SZ])
2398 {
2399 	struct ice_prof_tcam_entry;
2400 	int status;
2401 
2402 	status = ice_prof_gen_key(hw, blk, ptg, vsig, cdid, flags, vl_msk,
2403 				  dc_msk, nm_msk, hw->blk[blk].prof.t[idx].key);
2404 	if (!status) {
2405 		hw->blk[blk].prof.t[idx].addr = cpu_to_le16(idx);
2406 		hw->blk[blk].prof.t[idx].prof_id = prof_id;
2407 	}
2408 
2409 	return status;
2410 }
2411 
2412 /**
2413  * ice_vsig_get_ref - returns number of VSIs belong to a VSIG
2414  * @hw: pointer to the hardware structure
2415  * @blk: HW block
2416  * @vsig: VSIG to query
2417  * @refs: pointer to variable to receive the reference count
2418  */
2419 static int
2420 ice_vsig_get_ref(struct ice_hw *hw, enum ice_block blk, u16 vsig, u16 *refs)
2421 {
2422 	u16 idx = vsig & ICE_VSIG_IDX_M;
2423 	struct ice_vsig_vsi *ptr;
2424 
2425 	*refs = 0;
2426 
2427 	if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use)
2428 		return -ENOENT;
2429 
2430 	ptr = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
2431 	while (ptr) {
2432 		(*refs)++;
2433 		ptr = ptr->next_vsi;
2434 	}
2435 
2436 	return 0;
2437 }
2438 
2439 /**
2440  * ice_has_prof_vsig - check to see if VSIG has a specific profile
2441  * @hw: pointer to the hardware structure
2442  * @blk: HW block
2443  * @vsig: VSIG to check against
2444  * @hdl: profile handle
2445  */
2446 static bool
2447 ice_has_prof_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig, u64 hdl)
2448 {
2449 	u16 idx = vsig & ICE_VSIG_IDX_M;
2450 	struct ice_vsig_prof *ent;
2451 
2452 	list_for_each_entry(ent, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
2453 			    list)
2454 		if (ent->profile_cookie == hdl)
2455 			return true;
2456 
2457 	ice_debug(hw, ICE_DBG_INIT, "Characteristic list for VSI group %d not found.\n",
2458 		  vsig);
2459 	return false;
2460 }
2461 
2462 /**
2463  * ice_prof_bld_es - build profile ID extraction sequence changes
2464  * @hw: pointer to the HW struct
2465  * @blk: hardware block
2466  * @bld: the update package buffer build to add to
2467  * @chgs: the list of changes to make in hardware
2468  */
2469 static int
2470 ice_prof_bld_es(struct ice_hw *hw, enum ice_block blk,
2471 		struct ice_buf_build *bld, struct list_head *chgs)
2472 {
2473 	u16 vec_size = hw->blk[blk].es.fvw * sizeof(struct ice_fv_word);
2474 	struct ice_chs_chg *tmp;
2475 
2476 	list_for_each_entry(tmp, chgs, list_entry)
2477 		if (tmp->type == ICE_PTG_ES_ADD && tmp->add_prof) {
2478 			u16 off = tmp->prof_id * hw->blk[blk].es.fvw;
2479 			struct ice_pkg_es *p;
2480 			u32 id;
2481 
2482 			id = ice_sect_id(blk, ICE_VEC_TBL);
2483 			p = ice_pkg_buf_alloc_section(bld, id,
2484 						      struct_size(p, es, 1) +
2485 						      vec_size -
2486 						      sizeof(p->es[0]));
2487 
2488 			if (!p)
2489 				return -ENOSPC;
2490 
2491 			p->count = cpu_to_le16(1);
2492 			p->offset = cpu_to_le16(tmp->prof_id);
2493 
2494 			memcpy(p->es, &hw->blk[blk].es.t[off], vec_size);
2495 		}
2496 
2497 	return 0;
2498 }
2499 
2500 /**
2501  * ice_prof_bld_tcam - build profile ID TCAM changes
2502  * @hw: pointer to the HW struct
2503  * @blk: hardware block
2504  * @bld: the update package buffer build to add to
2505  * @chgs: the list of changes to make in hardware
2506  */
2507 static int
2508 ice_prof_bld_tcam(struct ice_hw *hw, enum ice_block blk,
2509 		  struct ice_buf_build *bld, struct list_head *chgs)
2510 {
2511 	struct ice_chs_chg *tmp;
2512 
2513 	list_for_each_entry(tmp, chgs, list_entry)
2514 		if (tmp->type == ICE_TCAM_ADD && tmp->add_tcam_idx) {
2515 			struct ice_prof_id_section *p;
2516 			u32 id;
2517 
2518 			id = ice_sect_id(blk, ICE_PROF_TCAM);
2519 			p = ice_pkg_buf_alloc_section(bld, id,
2520 						      struct_size(p, entry, 1));
2521 
2522 			if (!p)
2523 				return -ENOSPC;
2524 
2525 			p->count = cpu_to_le16(1);
2526 			p->entry[0].addr = cpu_to_le16(tmp->tcam_idx);
2527 			p->entry[0].prof_id = tmp->prof_id;
2528 
2529 			memcpy(p->entry[0].key,
2530 			       &hw->blk[blk].prof.t[tmp->tcam_idx].key,
2531 			       sizeof(hw->blk[blk].prof.t->key));
2532 		}
2533 
2534 	return 0;
2535 }
2536 
2537 /**
2538  * ice_prof_bld_xlt1 - build XLT1 changes
2539  * @blk: hardware block
2540  * @bld: the update package buffer build to add to
2541  * @chgs: the list of changes to make in hardware
2542  */
2543 static int
2544 ice_prof_bld_xlt1(enum ice_block blk, struct ice_buf_build *bld,
2545 		  struct list_head *chgs)
2546 {
2547 	struct ice_chs_chg *tmp;
2548 
2549 	list_for_each_entry(tmp, chgs, list_entry)
2550 		if (tmp->type == ICE_PTG_ES_ADD && tmp->add_ptg) {
2551 			struct ice_xlt1_section *p;
2552 			u32 id;
2553 
2554 			id = ice_sect_id(blk, ICE_XLT1);
2555 			p = ice_pkg_buf_alloc_section(bld, id,
2556 						      struct_size(p, value, 1));
2557 
2558 			if (!p)
2559 				return -ENOSPC;
2560 
2561 			p->count = cpu_to_le16(1);
2562 			p->offset = cpu_to_le16(tmp->ptype);
2563 			p->value[0] = tmp->ptg;
2564 		}
2565 
2566 	return 0;
2567 }
2568 
2569 /**
2570  * ice_prof_bld_xlt2 - build XLT2 changes
2571  * @blk: hardware block
2572  * @bld: the update package buffer build to add to
2573  * @chgs: the list of changes to make in hardware
2574  */
2575 static int
2576 ice_prof_bld_xlt2(enum ice_block blk, struct ice_buf_build *bld,
2577 		  struct list_head *chgs)
2578 {
2579 	struct ice_chs_chg *tmp;
2580 
2581 	list_for_each_entry(tmp, chgs, list_entry) {
2582 		struct ice_xlt2_section *p;
2583 		u32 id;
2584 
2585 		switch (tmp->type) {
2586 		case ICE_VSIG_ADD:
2587 		case ICE_VSI_MOVE:
2588 		case ICE_VSIG_REM:
2589 			id = ice_sect_id(blk, ICE_XLT2);
2590 			p = ice_pkg_buf_alloc_section(bld, id,
2591 						      struct_size(p, value, 1));
2592 
2593 			if (!p)
2594 				return -ENOSPC;
2595 
2596 			p->count = cpu_to_le16(1);
2597 			p->offset = cpu_to_le16(tmp->vsi);
2598 			p->value[0] = cpu_to_le16(tmp->vsig);
2599 			break;
2600 		default:
2601 			break;
2602 		}
2603 	}
2604 
2605 	return 0;
2606 }
2607 
2608 /**
2609  * ice_upd_prof_hw - update hardware using the change list
2610  * @hw: pointer to the HW struct
2611  * @blk: hardware block
2612  * @chgs: the list of changes to make in hardware
2613  */
2614 static int
2615 ice_upd_prof_hw(struct ice_hw *hw, enum ice_block blk,
2616 		struct list_head *chgs)
2617 {
2618 	struct ice_buf_build *b;
2619 	struct ice_chs_chg *tmp;
2620 	u16 pkg_sects;
2621 	u16 xlt1 = 0;
2622 	u16 xlt2 = 0;
2623 	u16 tcam = 0;
2624 	u16 es = 0;
2625 	int status;
2626 	u16 sects;
2627 
2628 	/* count number of sections we need */
2629 	list_for_each_entry(tmp, chgs, list_entry) {
2630 		switch (tmp->type) {
2631 		case ICE_PTG_ES_ADD:
2632 			if (tmp->add_ptg)
2633 				xlt1++;
2634 			if (tmp->add_prof)
2635 				es++;
2636 			break;
2637 		case ICE_TCAM_ADD:
2638 			tcam++;
2639 			break;
2640 		case ICE_VSIG_ADD:
2641 		case ICE_VSI_MOVE:
2642 		case ICE_VSIG_REM:
2643 			xlt2++;
2644 			break;
2645 		default:
2646 			break;
2647 		}
2648 	}
2649 	sects = xlt1 + xlt2 + tcam + es;
2650 
2651 	if (!sects)
2652 		return 0;
2653 
2654 	/* Build update package buffer */
2655 	b = ice_pkg_buf_alloc(hw);
2656 	if (!b)
2657 		return -ENOMEM;
2658 
2659 	status = ice_pkg_buf_reserve_section(b, sects);
2660 	if (status)
2661 		goto error_tmp;
2662 
2663 	/* Preserve order of table update: ES, TCAM, PTG, VSIG */
2664 	if (es) {
2665 		status = ice_prof_bld_es(hw, blk, b, chgs);
2666 		if (status)
2667 			goto error_tmp;
2668 	}
2669 
2670 	if (tcam) {
2671 		status = ice_prof_bld_tcam(hw, blk, b, chgs);
2672 		if (status)
2673 			goto error_tmp;
2674 	}
2675 
2676 	if (xlt1) {
2677 		status = ice_prof_bld_xlt1(blk, b, chgs);
2678 		if (status)
2679 			goto error_tmp;
2680 	}
2681 
2682 	if (xlt2) {
2683 		status = ice_prof_bld_xlt2(blk, b, chgs);
2684 		if (status)
2685 			goto error_tmp;
2686 	}
2687 
2688 	/* After package buffer build check if the section count in buffer is
2689 	 * non-zero and matches the number of sections detected for package
2690 	 * update.
2691 	 */
2692 	pkg_sects = ice_pkg_buf_get_active_sections(b);
2693 	if (!pkg_sects || pkg_sects != sects) {
2694 		status = -EINVAL;
2695 		goto error_tmp;
2696 	}
2697 
2698 	/* update package */
2699 	status = ice_update_pkg(hw, ice_pkg_buf(b), 1);
2700 	if (status == -EIO)
2701 		ice_debug(hw, ICE_DBG_INIT, "Unable to update HW profile\n");
2702 
2703 error_tmp:
2704 	ice_pkg_buf_free(hw, b);
2705 	return status;
2706 }
2707 
2708 /**
2709  * ice_update_fd_mask - set Flow Director Field Vector mask for a profile
2710  * @hw: pointer to the HW struct
2711  * @prof_id: profile ID
2712  * @mask_sel: mask select
2713  *
2714  * This function enable any of the masks selected by the mask select parameter
2715  * for the profile specified.
2716  */
2717 static void ice_update_fd_mask(struct ice_hw *hw, u16 prof_id, u32 mask_sel)
2718 {
2719 	wr32(hw, GLQF_FDMASK_SEL(prof_id), mask_sel);
2720 
2721 	ice_debug(hw, ICE_DBG_INIT, "fd mask(%d): %x = %x\n", prof_id,
2722 		  GLQF_FDMASK_SEL(prof_id), mask_sel);
2723 }
2724 
2725 struct ice_fd_src_dst_pair {
2726 	u8 prot_id;
2727 	u8 count;
2728 	u16 off;
2729 };
2730 
2731 static const struct ice_fd_src_dst_pair ice_fd_pairs[] = {
2732 	/* These are defined in pairs */
2733 	{ ICE_PROT_IPV4_OF_OR_S, 2, 12 },
2734 	{ ICE_PROT_IPV4_OF_OR_S, 2, 16 },
2735 
2736 	{ ICE_PROT_IPV4_IL, 2, 12 },
2737 	{ ICE_PROT_IPV4_IL, 2, 16 },
2738 
2739 	{ ICE_PROT_IPV6_OF_OR_S, 8, 8 },
2740 	{ ICE_PROT_IPV6_OF_OR_S, 8, 24 },
2741 
2742 	{ ICE_PROT_IPV6_IL, 8, 8 },
2743 	{ ICE_PROT_IPV6_IL, 8, 24 },
2744 
2745 	{ ICE_PROT_TCP_IL, 1, 0 },
2746 	{ ICE_PROT_TCP_IL, 1, 2 },
2747 
2748 	{ ICE_PROT_UDP_OF, 1, 0 },
2749 	{ ICE_PROT_UDP_OF, 1, 2 },
2750 
2751 	{ ICE_PROT_UDP_IL_OR_S, 1, 0 },
2752 	{ ICE_PROT_UDP_IL_OR_S, 1, 2 },
2753 
2754 	{ ICE_PROT_SCTP_IL, 1, 0 },
2755 	{ ICE_PROT_SCTP_IL, 1, 2 }
2756 };
2757 
2758 #define ICE_FD_SRC_DST_PAIR_COUNT	ARRAY_SIZE(ice_fd_pairs)
2759 
2760 /**
2761  * ice_update_fd_swap - set register appropriately for a FD FV extraction
2762  * @hw: pointer to the HW struct
2763  * @prof_id: profile ID
2764  * @es: extraction sequence (length of array is determined by the block)
2765  */
2766 static int
2767 ice_update_fd_swap(struct ice_hw *hw, u16 prof_id, struct ice_fv_word *es)
2768 {
2769 	DECLARE_BITMAP(pair_list, ICE_FD_SRC_DST_PAIR_COUNT);
2770 	u8 pair_start[ICE_FD_SRC_DST_PAIR_COUNT] = { 0 };
2771 #define ICE_FD_FV_NOT_FOUND (-2)
2772 	s8 first_free = ICE_FD_FV_NOT_FOUND;
2773 	u8 used[ICE_MAX_FV_WORDS] = { 0 };
2774 	s8 orig_free, si;
2775 	u32 mask_sel = 0;
2776 	u8 i, j, k;
2777 
2778 	bitmap_zero(pair_list, ICE_FD_SRC_DST_PAIR_COUNT);
2779 
2780 	/* This code assumes that the Flow Director field vectors are assigned
2781 	 * from the end of the FV indexes working towards the zero index, that
2782 	 * only complete fields will be included and will be consecutive, and
2783 	 * that there are no gaps between valid indexes.
2784 	 */
2785 
2786 	/* Determine swap fields present */
2787 	for (i = 0; i < hw->blk[ICE_BLK_FD].es.fvw; i++) {
2788 		/* Find the first free entry, assuming right to left population.
2789 		 * This is where we can start adding additional pairs if needed.
2790 		 */
2791 		if (first_free == ICE_FD_FV_NOT_FOUND && es[i].prot_id !=
2792 		    ICE_PROT_INVALID)
2793 			first_free = i - 1;
2794 
2795 		for (j = 0; j < ICE_FD_SRC_DST_PAIR_COUNT; j++)
2796 			if (es[i].prot_id == ice_fd_pairs[j].prot_id &&
2797 			    es[i].off == ice_fd_pairs[j].off) {
2798 				__set_bit(j, pair_list);
2799 				pair_start[j] = i;
2800 			}
2801 	}
2802 
2803 	orig_free = first_free;
2804 
2805 	/* determine missing swap fields that need to be added */
2806 	for (i = 0; i < ICE_FD_SRC_DST_PAIR_COUNT; i += 2) {
2807 		u8 bit1 = test_bit(i + 1, pair_list);
2808 		u8 bit0 = test_bit(i, pair_list);
2809 
2810 		if (bit0 ^ bit1) {
2811 			u8 index;
2812 
2813 			/* add the appropriate 'paired' entry */
2814 			if (!bit0)
2815 				index = i;
2816 			else
2817 				index = i + 1;
2818 
2819 			/* check for room */
2820 			if (first_free + 1 < (s8)ice_fd_pairs[index].count)
2821 				return -ENOSPC;
2822 
2823 			/* place in extraction sequence */
2824 			for (k = 0; k < ice_fd_pairs[index].count; k++) {
2825 				es[first_free - k].prot_id =
2826 					ice_fd_pairs[index].prot_id;
2827 				es[first_free - k].off =
2828 					ice_fd_pairs[index].off + (k * 2);
2829 
2830 				if (k > first_free)
2831 					return -EIO;
2832 
2833 				/* keep track of non-relevant fields */
2834 				mask_sel |= BIT(first_free - k);
2835 			}
2836 
2837 			pair_start[index] = first_free;
2838 			first_free -= ice_fd_pairs[index].count;
2839 		}
2840 	}
2841 
2842 	/* fill in the swap array */
2843 	si = hw->blk[ICE_BLK_FD].es.fvw - 1;
2844 	while (si >= 0) {
2845 		u8 indexes_used = 1;
2846 
2847 		/* assume flat at this index */
2848 #define ICE_SWAP_VALID	0x80
2849 		used[si] = si | ICE_SWAP_VALID;
2850 
2851 		if (orig_free == ICE_FD_FV_NOT_FOUND || si <= orig_free) {
2852 			si -= indexes_used;
2853 			continue;
2854 		}
2855 
2856 		/* check for a swap location */
2857 		for (j = 0; j < ICE_FD_SRC_DST_PAIR_COUNT; j++)
2858 			if (es[si].prot_id == ice_fd_pairs[j].prot_id &&
2859 			    es[si].off == ice_fd_pairs[j].off) {
2860 				u8 idx;
2861 
2862 				/* determine the appropriate matching field */
2863 				idx = j + ((j % 2) ? -1 : 1);
2864 
2865 				indexes_used = ice_fd_pairs[idx].count;
2866 				for (k = 0; k < indexes_used; k++) {
2867 					used[si - k] = (pair_start[idx] - k) |
2868 						ICE_SWAP_VALID;
2869 				}
2870 
2871 				break;
2872 			}
2873 
2874 		si -= indexes_used;
2875 	}
2876 
2877 	/* for each set of 4 swap and 4 inset indexes, write the appropriate
2878 	 * register
2879 	 */
2880 	for (j = 0; j < hw->blk[ICE_BLK_FD].es.fvw / 4; j++) {
2881 		u32 raw_swap = 0;
2882 		u32 raw_in = 0;
2883 
2884 		for (k = 0; k < 4; k++) {
2885 			u8 idx;
2886 
2887 			idx = (j * 4) + k;
2888 			if (used[idx] && !(mask_sel & BIT(idx))) {
2889 				raw_swap |= used[idx] << (k * BITS_PER_BYTE);
2890 #define ICE_INSET_DFLT 0x9f
2891 				raw_in |= ICE_INSET_DFLT << (k * BITS_PER_BYTE);
2892 			}
2893 		}
2894 
2895 		/* write the appropriate swap register set */
2896 		wr32(hw, GLQF_FDSWAP(prof_id, j), raw_swap);
2897 
2898 		ice_debug(hw, ICE_DBG_INIT, "swap wr(%d, %d): %x = %08x\n",
2899 			  prof_id, j, GLQF_FDSWAP(prof_id, j), raw_swap);
2900 
2901 		/* write the appropriate inset register set */
2902 		wr32(hw, GLQF_FDINSET(prof_id, j), raw_in);
2903 
2904 		ice_debug(hw, ICE_DBG_INIT, "inset wr(%d, %d): %x = %08x\n",
2905 			  prof_id, j, GLQF_FDINSET(prof_id, j), raw_in);
2906 	}
2907 
2908 	/* initially clear the mask select for this profile */
2909 	ice_update_fd_mask(hw, prof_id, 0);
2910 
2911 	return 0;
2912 }
2913 
2914 /* The entries here needs to match the order of enum ice_ptype_attrib */
2915 static const struct ice_ptype_attrib_info ice_ptype_attributes[] = {
2916 	{ ICE_GTP_PDU_EH,	ICE_GTP_PDU_FLAG_MASK },
2917 	{ ICE_GTP_SESSION,	ICE_GTP_FLAGS_MASK },
2918 	{ ICE_GTP_DOWNLINK,	ICE_GTP_FLAGS_MASK },
2919 	{ ICE_GTP_UPLINK,	ICE_GTP_FLAGS_MASK },
2920 };
2921 
2922 /**
2923  * ice_get_ptype_attrib_info - get PTYPE attribute information
2924  * @type: attribute type
2925  * @info: pointer to variable to the attribute information
2926  */
2927 static void
2928 ice_get_ptype_attrib_info(enum ice_ptype_attrib_type type,
2929 			  struct ice_ptype_attrib_info *info)
2930 {
2931 	*info = ice_ptype_attributes[type];
2932 }
2933 
2934 /**
2935  * ice_add_prof_attrib - add any PTG with attributes to profile
2936  * @prof: pointer to the profile to which PTG entries will be added
2937  * @ptg: PTG to be added
2938  * @ptype: PTYPE that needs to be looked up
2939  * @attr: array of attributes that will be considered
2940  * @attr_cnt: number of elements in the attribute array
2941  */
2942 static int
2943 ice_add_prof_attrib(struct ice_prof_map *prof, u8 ptg, u16 ptype,
2944 		    const struct ice_ptype_attributes *attr, u16 attr_cnt)
2945 {
2946 	bool found = false;
2947 	u16 i;
2948 
2949 	for (i = 0; i < attr_cnt; i++)
2950 		if (attr[i].ptype == ptype) {
2951 			found = true;
2952 
2953 			prof->ptg[prof->ptg_cnt] = ptg;
2954 			ice_get_ptype_attrib_info(attr[i].attrib,
2955 						  &prof->attr[prof->ptg_cnt]);
2956 
2957 			if (++prof->ptg_cnt >= ICE_MAX_PTG_PER_PROFILE)
2958 				return -ENOSPC;
2959 		}
2960 
2961 	if (!found)
2962 		return -ENOENT;
2963 
2964 	return 0;
2965 }
2966 
2967 /**
2968  * ice_add_prof - add profile
2969  * @hw: pointer to the HW struct
2970  * @blk: hardware block
2971  * @id: profile tracking ID
2972  * @ptypes: array of bitmaps indicating ptypes (ICE_FLOW_PTYPE_MAX bits)
2973  * @attr: array of attributes
2974  * @attr_cnt: number of elements in attr array
2975  * @es: extraction sequence (length of array is determined by the block)
2976  * @masks: mask for extraction sequence
2977  *
2978  * This function registers a profile, which matches a set of PTYPES with a
2979  * particular extraction sequence. While the hardware profile is allocated
2980  * it will not be written until the first call to ice_add_flow that specifies
2981  * the ID value used here.
2982  */
2983 int
2984 ice_add_prof(struct ice_hw *hw, enum ice_block blk, u64 id, u8 ptypes[],
2985 	     const struct ice_ptype_attributes *attr, u16 attr_cnt,
2986 	     struct ice_fv_word *es, u16 *masks)
2987 {
2988 	u32 bytes = DIV_ROUND_UP(ICE_FLOW_PTYPE_MAX, BITS_PER_BYTE);
2989 	DECLARE_BITMAP(ptgs_used, ICE_XLT1_CNT);
2990 	struct ice_prof_map *prof;
2991 	u8 byte = 0;
2992 	u8 prof_id;
2993 	int status;
2994 
2995 	bitmap_zero(ptgs_used, ICE_XLT1_CNT);
2996 
2997 	mutex_lock(&hw->blk[blk].es.prof_map_lock);
2998 
2999 	/* search for existing profile */
3000 	status = ice_find_prof_id_with_mask(hw, blk, es, masks, &prof_id);
3001 	if (status) {
3002 		/* allocate profile ID */
3003 		status = ice_alloc_prof_id(hw, blk, &prof_id);
3004 		if (status)
3005 			goto err_ice_add_prof;
3006 		if (blk == ICE_BLK_FD) {
3007 			/* For Flow Director block, the extraction sequence may
3008 			 * need to be altered in the case where there are paired
3009 			 * fields that have no match. This is necessary because
3010 			 * for Flow Director, src and dest fields need to paired
3011 			 * for filter programming and these values are swapped
3012 			 * during Tx.
3013 			 */
3014 			status = ice_update_fd_swap(hw, prof_id, es);
3015 			if (status)
3016 				goto err_ice_add_prof;
3017 		}
3018 		status = ice_update_prof_masking(hw, blk, prof_id, masks);
3019 		if (status)
3020 			goto err_ice_add_prof;
3021 
3022 		/* and write new es */
3023 		ice_write_es(hw, blk, prof_id, es);
3024 	}
3025 
3026 	ice_prof_inc_ref(hw, blk, prof_id);
3027 
3028 	/* add profile info */
3029 	prof = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*prof), GFP_KERNEL);
3030 	if (!prof) {
3031 		status = -ENOMEM;
3032 		goto err_ice_add_prof;
3033 	}
3034 
3035 	prof->profile_cookie = id;
3036 	prof->prof_id = prof_id;
3037 	prof->ptg_cnt = 0;
3038 	prof->context = 0;
3039 
3040 	/* build list of ptgs */
3041 	while (bytes && prof->ptg_cnt < ICE_MAX_PTG_PER_PROFILE) {
3042 		u8 bit;
3043 
3044 		if (!ptypes[byte]) {
3045 			bytes--;
3046 			byte++;
3047 			continue;
3048 		}
3049 
3050 		/* Examine 8 bits per byte */
3051 		for_each_set_bit(bit, (unsigned long *)&ptypes[byte],
3052 				 BITS_PER_BYTE) {
3053 			u16 ptype;
3054 			u8 ptg;
3055 
3056 			ptype = byte * BITS_PER_BYTE + bit;
3057 
3058 			/* The package should place all ptypes in a non-zero
3059 			 * PTG, so the following call should never fail.
3060 			 */
3061 			if (ice_ptg_find_ptype(hw, blk, ptype, &ptg))
3062 				continue;
3063 
3064 			/* If PTG is already added, skip and continue */
3065 			if (test_bit(ptg, ptgs_used))
3066 				continue;
3067 
3068 			__set_bit(ptg, ptgs_used);
3069 			/* Check to see there are any attributes for
3070 			 * this PTYPE, and add them if found.
3071 			 */
3072 			status = ice_add_prof_attrib(prof, ptg, ptype,
3073 						     attr, attr_cnt);
3074 			if (status == -ENOSPC)
3075 				break;
3076 			if (status) {
3077 				/* This is simple a PTYPE/PTG with no
3078 				 * attribute
3079 				 */
3080 				prof->ptg[prof->ptg_cnt] = ptg;
3081 				prof->attr[prof->ptg_cnt].flags = 0;
3082 				prof->attr[prof->ptg_cnt].mask = 0;
3083 
3084 				if (++prof->ptg_cnt >=
3085 				    ICE_MAX_PTG_PER_PROFILE)
3086 					break;
3087 			}
3088 		}
3089 
3090 		bytes--;
3091 		byte++;
3092 	}
3093 
3094 	list_add(&prof->list, &hw->blk[blk].es.prof_map);
3095 	status = 0;
3096 
3097 err_ice_add_prof:
3098 	mutex_unlock(&hw->blk[blk].es.prof_map_lock);
3099 	return status;
3100 }
3101 
3102 /**
3103  * ice_search_prof_id - Search for a profile tracking ID
3104  * @hw: pointer to the HW struct
3105  * @blk: hardware block
3106  * @id: profile tracking ID
3107  *
3108  * This will search for a profile tracking ID which was previously added.
3109  * The profile map lock should be held before calling this function.
3110  */
3111 static struct ice_prof_map *
3112 ice_search_prof_id(struct ice_hw *hw, enum ice_block blk, u64 id)
3113 {
3114 	struct ice_prof_map *entry = NULL;
3115 	struct ice_prof_map *map;
3116 
3117 	list_for_each_entry(map, &hw->blk[blk].es.prof_map, list)
3118 		if (map->profile_cookie == id) {
3119 			entry = map;
3120 			break;
3121 		}
3122 
3123 	return entry;
3124 }
3125 
3126 /**
3127  * ice_vsig_prof_id_count - count profiles in a VSIG
3128  * @hw: pointer to the HW struct
3129  * @blk: hardware block
3130  * @vsig: VSIG to remove the profile from
3131  */
3132 static u16
3133 ice_vsig_prof_id_count(struct ice_hw *hw, enum ice_block blk, u16 vsig)
3134 {
3135 	u16 idx = vsig & ICE_VSIG_IDX_M, count = 0;
3136 	struct ice_vsig_prof *p;
3137 
3138 	list_for_each_entry(p, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
3139 			    list)
3140 		count++;
3141 
3142 	return count;
3143 }
3144 
3145 /**
3146  * ice_rel_tcam_idx - release a TCAM index
3147  * @hw: pointer to the HW struct
3148  * @blk: hardware block
3149  * @idx: the index to release
3150  */
3151 static int ice_rel_tcam_idx(struct ice_hw *hw, enum ice_block blk, u16 idx)
3152 {
3153 	/* Masks to invoke a never match entry */
3154 	u8 vl_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3155 	u8 dc_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFE, 0xFF, 0xFF, 0xFF, 0xFF };
3156 	u8 nm_msk[ICE_TCAM_KEY_VAL_SZ] = { 0x01, 0x00, 0x00, 0x00, 0x00 };
3157 	int status;
3158 
3159 	/* write the TCAM entry */
3160 	status = ice_tcam_write_entry(hw, blk, idx, 0, 0, 0, 0, 0, vl_msk,
3161 				      dc_msk, nm_msk);
3162 	if (status)
3163 		return status;
3164 
3165 	/* release the TCAM entry */
3166 	status = ice_free_tcam_ent(hw, blk, idx);
3167 
3168 	return status;
3169 }
3170 
3171 /**
3172  * ice_rem_prof_id - remove one profile from a VSIG
3173  * @hw: pointer to the HW struct
3174  * @blk: hardware block
3175  * @prof: pointer to profile structure to remove
3176  */
3177 static int
3178 ice_rem_prof_id(struct ice_hw *hw, enum ice_block blk,
3179 		struct ice_vsig_prof *prof)
3180 {
3181 	int status;
3182 	u16 i;
3183 
3184 	for (i = 0; i < prof->tcam_count; i++)
3185 		if (prof->tcam[i].in_use) {
3186 			prof->tcam[i].in_use = false;
3187 			status = ice_rel_tcam_idx(hw, blk,
3188 						  prof->tcam[i].tcam_idx);
3189 			if (status)
3190 				return -EIO;
3191 		}
3192 
3193 	return 0;
3194 }
3195 
3196 /**
3197  * ice_rem_vsig - remove VSIG
3198  * @hw: pointer to the HW struct
3199  * @blk: hardware block
3200  * @vsig: the VSIG to remove
3201  * @chg: the change list
3202  */
3203 static int
3204 ice_rem_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig,
3205 	     struct list_head *chg)
3206 {
3207 	u16 idx = vsig & ICE_VSIG_IDX_M;
3208 	struct ice_vsig_vsi *vsi_cur;
3209 	struct ice_vsig_prof *d, *t;
3210 
3211 	/* remove TCAM entries */
3212 	list_for_each_entry_safe(d, t,
3213 				 &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
3214 				 list) {
3215 		int status;
3216 
3217 		status = ice_rem_prof_id(hw, blk, d);
3218 		if (status)
3219 			return status;
3220 
3221 		list_del(&d->list);
3222 		devm_kfree(ice_hw_to_dev(hw), d);
3223 	}
3224 
3225 	/* Move all VSIS associated with this VSIG to the default VSIG */
3226 	vsi_cur = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
3227 	/* If the VSIG has at least 1 VSI then iterate through the list
3228 	 * and remove the VSIs before deleting the group.
3229 	 */
3230 	if (vsi_cur)
3231 		do {
3232 			struct ice_vsig_vsi *tmp = vsi_cur->next_vsi;
3233 			struct ice_chs_chg *p;
3234 
3235 			p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p),
3236 					 GFP_KERNEL);
3237 			if (!p)
3238 				return -ENOMEM;
3239 
3240 			p->type = ICE_VSIG_REM;
3241 			p->orig_vsig = vsig;
3242 			p->vsig = ICE_DEFAULT_VSIG;
3243 			p->vsi = vsi_cur - hw->blk[blk].xlt2.vsis;
3244 
3245 			list_add(&p->list_entry, chg);
3246 
3247 			vsi_cur = tmp;
3248 		} while (vsi_cur);
3249 
3250 	return ice_vsig_free(hw, blk, vsig);
3251 }
3252 
3253 /**
3254  * ice_rem_prof_id_vsig - remove a specific profile from a VSIG
3255  * @hw: pointer to the HW struct
3256  * @blk: hardware block
3257  * @vsig: VSIG to remove the profile from
3258  * @hdl: profile handle indicating which profile to remove
3259  * @chg: list to receive a record of changes
3260  */
3261 static int
3262 ice_rem_prof_id_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig, u64 hdl,
3263 		     struct list_head *chg)
3264 {
3265 	u16 idx = vsig & ICE_VSIG_IDX_M;
3266 	struct ice_vsig_prof *p, *t;
3267 
3268 	list_for_each_entry_safe(p, t,
3269 				 &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
3270 				 list)
3271 		if (p->profile_cookie == hdl) {
3272 			int status;
3273 
3274 			if (ice_vsig_prof_id_count(hw, blk, vsig) == 1)
3275 				/* this is the last profile, remove the VSIG */
3276 				return ice_rem_vsig(hw, blk, vsig, chg);
3277 
3278 			status = ice_rem_prof_id(hw, blk, p);
3279 			if (!status) {
3280 				list_del(&p->list);
3281 				devm_kfree(ice_hw_to_dev(hw), p);
3282 			}
3283 			return status;
3284 		}
3285 
3286 	return -ENOENT;
3287 }
3288 
3289 /**
3290  * ice_rem_flow_all - remove all flows with a particular profile
3291  * @hw: pointer to the HW struct
3292  * @blk: hardware block
3293  * @id: profile tracking ID
3294  */
3295 static int ice_rem_flow_all(struct ice_hw *hw, enum ice_block blk, u64 id)
3296 {
3297 	struct ice_chs_chg *del, *tmp;
3298 	struct list_head chg;
3299 	int status;
3300 	u16 i;
3301 
3302 	INIT_LIST_HEAD(&chg);
3303 
3304 	for (i = 1; i < ICE_MAX_VSIGS; i++)
3305 		if (hw->blk[blk].xlt2.vsig_tbl[i].in_use) {
3306 			if (ice_has_prof_vsig(hw, blk, i, id)) {
3307 				status = ice_rem_prof_id_vsig(hw, blk, i, id,
3308 							      &chg);
3309 				if (status)
3310 					goto err_ice_rem_flow_all;
3311 			}
3312 		}
3313 
3314 	status = ice_upd_prof_hw(hw, blk, &chg);
3315 
3316 err_ice_rem_flow_all:
3317 	list_for_each_entry_safe(del, tmp, &chg, list_entry) {
3318 		list_del(&del->list_entry);
3319 		devm_kfree(ice_hw_to_dev(hw), del);
3320 	}
3321 
3322 	return status;
3323 }
3324 
3325 /**
3326  * ice_rem_prof - remove profile
3327  * @hw: pointer to the HW struct
3328  * @blk: hardware block
3329  * @id: profile tracking ID
3330  *
3331  * This will remove the profile specified by the ID parameter, which was
3332  * previously created through ice_add_prof. If any existing entries
3333  * are associated with this profile, they will be removed as well.
3334  */
3335 int ice_rem_prof(struct ice_hw *hw, enum ice_block blk, u64 id)
3336 {
3337 	struct ice_prof_map *pmap;
3338 	int status;
3339 
3340 	mutex_lock(&hw->blk[blk].es.prof_map_lock);
3341 
3342 	pmap = ice_search_prof_id(hw, blk, id);
3343 	if (!pmap) {
3344 		status = -ENOENT;
3345 		goto err_ice_rem_prof;
3346 	}
3347 
3348 	/* remove all flows with this profile */
3349 	status = ice_rem_flow_all(hw, blk, pmap->profile_cookie);
3350 	if (status)
3351 		goto err_ice_rem_prof;
3352 
3353 	/* dereference profile, and possibly remove */
3354 	ice_prof_dec_ref(hw, blk, pmap->prof_id);
3355 
3356 	list_del(&pmap->list);
3357 	devm_kfree(ice_hw_to_dev(hw), pmap);
3358 
3359 err_ice_rem_prof:
3360 	mutex_unlock(&hw->blk[blk].es.prof_map_lock);
3361 	return status;
3362 }
3363 
3364 /**
3365  * ice_get_prof - get profile
3366  * @hw: pointer to the HW struct
3367  * @blk: hardware block
3368  * @hdl: profile handle
3369  * @chg: change list
3370  */
3371 static int
3372 ice_get_prof(struct ice_hw *hw, enum ice_block blk, u64 hdl,
3373 	     struct list_head *chg)
3374 {
3375 	struct ice_prof_map *map;
3376 	struct ice_chs_chg *p;
3377 	int status = 0;
3378 	u16 i;
3379 
3380 	mutex_lock(&hw->blk[blk].es.prof_map_lock);
3381 	/* Get the details on the profile specified by the handle ID */
3382 	map = ice_search_prof_id(hw, blk, hdl);
3383 	if (!map) {
3384 		status = -ENOENT;
3385 		goto err_ice_get_prof;
3386 	}
3387 
3388 	for (i = 0; i < map->ptg_cnt; i++)
3389 		if (!hw->blk[blk].es.written[map->prof_id]) {
3390 			/* add ES to change list */
3391 			p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p),
3392 					 GFP_KERNEL);
3393 			if (!p) {
3394 				status = -ENOMEM;
3395 				goto err_ice_get_prof;
3396 			}
3397 
3398 			p->type = ICE_PTG_ES_ADD;
3399 			p->ptype = 0;
3400 			p->ptg = map->ptg[i];
3401 			p->add_ptg = 0;
3402 
3403 			p->add_prof = 1;
3404 			p->prof_id = map->prof_id;
3405 
3406 			hw->blk[blk].es.written[map->prof_id] = true;
3407 
3408 			list_add(&p->list_entry, chg);
3409 		}
3410 
3411 err_ice_get_prof:
3412 	mutex_unlock(&hw->blk[blk].es.prof_map_lock);
3413 	/* let caller clean up the change list */
3414 	return status;
3415 }
3416 
3417 /**
3418  * ice_get_profs_vsig - get a copy of the list of profiles from a VSIG
3419  * @hw: pointer to the HW struct
3420  * @blk: hardware block
3421  * @vsig: VSIG from which to copy the list
3422  * @lst: output list
3423  *
3424  * This routine makes a copy of the list of profiles in the specified VSIG.
3425  */
3426 static int
3427 ice_get_profs_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig,
3428 		   struct list_head *lst)
3429 {
3430 	struct ice_vsig_prof *ent1, *ent2;
3431 	u16 idx = vsig & ICE_VSIG_IDX_M;
3432 
3433 	list_for_each_entry(ent1, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
3434 			    list) {
3435 		struct ice_vsig_prof *p;
3436 
3437 		/* copy to the input list */
3438 		p = devm_kmemdup(ice_hw_to_dev(hw), ent1, sizeof(*p),
3439 				 GFP_KERNEL);
3440 		if (!p)
3441 			goto err_ice_get_profs_vsig;
3442 
3443 		list_add_tail(&p->list, lst);
3444 	}
3445 
3446 	return 0;
3447 
3448 err_ice_get_profs_vsig:
3449 	list_for_each_entry_safe(ent1, ent2, lst, list) {
3450 		list_del(&ent1->list);
3451 		devm_kfree(ice_hw_to_dev(hw), ent1);
3452 	}
3453 
3454 	return -ENOMEM;
3455 }
3456 
3457 /**
3458  * ice_add_prof_to_lst - add profile entry to a list
3459  * @hw: pointer to the HW struct
3460  * @blk: hardware block
3461  * @lst: the list to be added to
3462  * @hdl: profile handle of entry to add
3463  */
3464 static int
3465 ice_add_prof_to_lst(struct ice_hw *hw, enum ice_block blk,
3466 		    struct list_head *lst, u64 hdl)
3467 {
3468 	struct ice_prof_map *map;
3469 	struct ice_vsig_prof *p;
3470 	int status = 0;
3471 	u16 i;
3472 
3473 	mutex_lock(&hw->blk[blk].es.prof_map_lock);
3474 	map = ice_search_prof_id(hw, blk, hdl);
3475 	if (!map) {
3476 		status = -ENOENT;
3477 		goto err_ice_add_prof_to_lst;
3478 	}
3479 
3480 	p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p), GFP_KERNEL);
3481 	if (!p) {
3482 		status = -ENOMEM;
3483 		goto err_ice_add_prof_to_lst;
3484 	}
3485 
3486 	p->profile_cookie = map->profile_cookie;
3487 	p->prof_id = map->prof_id;
3488 	p->tcam_count = map->ptg_cnt;
3489 
3490 	for (i = 0; i < map->ptg_cnt; i++) {
3491 		p->tcam[i].prof_id = map->prof_id;
3492 		p->tcam[i].tcam_idx = ICE_INVALID_TCAM;
3493 		p->tcam[i].ptg = map->ptg[i];
3494 	}
3495 
3496 	list_add(&p->list, lst);
3497 
3498 err_ice_add_prof_to_lst:
3499 	mutex_unlock(&hw->blk[blk].es.prof_map_lock);
3500 	return status;
3501 }
3502 
3503 /**
3504  * ice_move_vsi - move VSI to another VSIG
3505  * @hw: pointer to the HW struct
3506  * @blk: hardware block
3507  * @vsi: the VSI to move
3508  * @vsig: the VSIG to move the VSI to
3509  * @chg: the change list
3510  */
3511 static int
3512 ice_move_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 vsig,
3513 	     struct list_head *chg)
3514 {
3515 	struct ice_chs_chg *p;
3516 	u16 orig_vsig;
3517 	int status;
3518 
3519 	p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p), GFP_KERNEL);
3520 	if (!p)
3521 		return -ENOMEM;
3522 
3523 	status = ice_vsig_find_vsi(hw, blk, vsi, &orig_vsig);
3524 	if (!status)
3525 		status = ice_vsig_add_mv_vsi(hw, blk, vsi, vsig);
3526 
3527 	if (status) {
3528 		devm_kfree(ice_hw_to_dev(hw), p);
3529 		return status;
3530 	}
3531 
3532 	p->type = ICE_VSI_MOVE;
3533 	p->vsi = vsi;
3534 	p->orig_vsig = orig_vsig;
3535 	p->vsig = vsig;
3536 
3537 	list_add(&p->list_entry, chg);
3538 
3539 	return 0;
3540 }
3541 
3542 /**
3543  * ice_rem_chg_tcam_ent - remove a specific TCAM entry from change list
3544  * @hw: pointer to the HW struct
3545  * @idx: the index of the TCAM entry to remove
3546  * @chg: the list of change structures to search
3547  */
3548 static void
3549 ice_rem_chg_tcam_ent(struct ice_hw *hw, u16 idx, struct list_head *chg)
3550 {
3551 	struct ice_chs_chg *pos, *tmp;
3552 
3553 	list_for_each_entry_safe(tmp, pos, chg, list_entry)
3554 		if (tmp->type == ICE_TCAM_ADD && tmp->tcam_idx == idx) {
3555 			list_del(&tmp->list_entry);
3556 			devm_kfree(ice_hw_to_dev(hw), tmp);
3557 		}
3558 }
3559 
3560 /**
3561  * ice_prof_tcam_ena_dis - add enable or disable TCAM change
3562  * @hw: pointer to the HW struct
3563  * @blk: hardware block
3564  * @enable: true to enable, false to disable
3565  * @vsig: the VSIG of the TCAM entry
3566  * @tcam: pointer the TCAM info structure of the TCAM to disable
3567  * @chg: the change list
3568  *
3569  * This function appends an enable or disable TCAM entry in the change log
3570  */
3571 static int
3572 ice_prof_tcam_ena_dis(struct ice_hw *hw, enum ice_block blk, bool enable,
3573 		      u16 vsig, struct ice_tcam_inf *tcam,
3574 		      struct list_head *chg)
3575 {
3576 	struct ice_chs_chg *p;
3577 	int status;
3578 
3579 	u8 vl_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3580 	u8 dc_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0x00, 0x00, 0x00 };
3581 	u8 nm_msk[ICE_TCAM_KEY_VAL_SZ] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
3582 
3583 	/* if disabling, free the TCAM */
3584 	if (!enable) {
3585 		status = ice_rel_tcam_idx(hw, blk, tcam->tcam_idx);
3586 
3587 		/* if we have already created a change for this TCAM entry, then
3588 		 * we need to remove that entry, in order to prevent writing to
3589 		 * a TCAM entry we no longer will have ownership of.
3590 		 */
3591 		ice_rem_chg_tcam_ent(hw, tcam->tcam_idx, chg);
3592 		tcam->tcam_idx = 0;
3593 		tcam->in_use = 0;
3594 		return status;
3595 	}
3596 
3597 	/* for re-enabling, reallocate a TCAM */
3598 	/* for entries with empty attribute masks, allocate entry from
3599 	 * the bottom of the TCAM table; otherwise, allocate from the
3600 	 * top of the table in order to give it higher priority
3601 	 */
3602 	status = ice_alloc_tcam_ent(hw, blk, tcam->attr.mask == 0,
3603 				    &tcam->tcam_idx);
3604 	if (status)
3605 		return status;
3606 
3607 	/* add TCAM to change list */
3608 	p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p), GFP_KERNEL);
3609 	if (!p)
3610 		return -ENOMEM;
3611 
3612 	status = ice_tcam_write_entry(hw, blk, tcam->tcam_idx, tcam->prof_id,
3613 				      tcam->ptg, vsig, 0, tcam->attr.flags,
3614 				      vl_msk, dc_msk, nm_msk);
3615 	if (status)
3616 		goto err_ice_prof_tcam_ena_dis;
3617 
3618 	tcam->in_use = 1;
3619 
3620 	p->type = ICE_TCAM_ADD;
3621 	p->add_tcam_idx = true;
3622 	p->prof_id = tcam->prof_id;
3623 	p->ptg = tcam->ptg;
3624 	p->vsig = 0;
3625 	p->tcam_idx = tcam->tcam_idx;
3626 
3627 	/* log change */
3628 	list_add(&p->list_entry, chg);
3629 
3630 	return 0;
3631 
3632 err_ice_prof_tcam_ena_dis:
3633 	devm_kfree(ice_hw_to_dev(hw), p);
3634 	return status;
3635 }
3636 
3637 /**
3638  * ice_adj_prof_priorities - adjust profile based on priorities
3639  * @hw: pointer to the HW struct
3640  * @blk: hardware block
3641  * @vsig: the VSIG for which to adjust profile priorities
3642  * @chg: the change list
3643  */
3644 static int
3645 ice_adj_prof_priorities(struct ice_hw *hw, enum ice_block blk, u16 vsig,
3646 			struct list_head *chg)
3647 {
3648 	DECLARE_BITMAP(ptgs_used, ICE_XLT1_CNT);
3649 	struct ice_vsig_prof *t;
3650 	int status;
3651 	u16 idx;
3652 
3653 	bitmap_zero(ptgs_used, ICE_XLT1_CNT);
3654 	idx = vsig & ICE_VSIG_IDX_M;
3655 
3656 	/* Priority is based on the order in which the profiles are added. The
3657 	 * newest added profile has highest priority and the oldest added
3658 	 * profile has the lowest priority. Since the profile property list for
3659 	 * a VSIG is sorted from newest to oldest, this code traverses the list
3660 	 * in order and enables the first of each PTG that it finds (that is not
3661 	 * already enabled); it also disables any duplicate PTGs that it finds
3662 	 * in the older profiles (that are currently enabled).
3663 	 */
3664 
3665 	list_for_each_entry(t, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
3666 			    list) {
3667 		u16 i;
3668 
3669 		for (i = 0; i < t->tcam_count; i++) {
3670 			/* Scan the priorities from newest to oldest.
3671 			 * Make sure that the newest profiles take priority.
3672 			 */
3673 			if (test_bit(t->tcam[i].ptg, ptgs_used) &&
3674 			    t->tcam[i].in_use) {
3675 				/* need to mark this PTG as never match, as it
3676 				 * was already in use and therefore duplicate
3677 				 * (and lower priority)
3678 				 */
3679 				status = ice_prof_tcam_ena_dis(hw, blk, false,
3680 							       vsig,
3681 							       &t->tcam[i],
3682 							       chg);
3683 				if (status)
3684 					return status;
3685 			} else if (!test_bit(t->tcam[i].ptg, ptgs_used) &&
3686 				   !t->tcam[i].in_use) {
3687 				/* need to enable this PTG, as it in not in use
3688 				 * and not enabled (highest priority)
3689 				 */
3690 				status = ice_prof_tcam_ena_dis(hw, blk, true,
3691 							       vsig,
3692 							       &t->tcam[i],
3693 							       chg);
3694 				if (status)
3695 					return status;
3696 			}
3697 
3698 			/* keep track of used ptgs */
3699 			__set_bit(t->tcam[i].ptg, ptgs_used);
3700 		}
3701 	}
3702 
3703 	return 0;
3704 }
3705 
3706 /**
3707  * ice_add_prof_id_vsig - add profile to VSIG
3708  * @hw: pointer to the HW struct
3709  * @blk: hardware block
3710  * @vsig: the VSIG to which this profile is to be added
3711  * @hdl: the profile handle indicating the profile to add
3712  * @rev: true to add entries to the end of the list
3713  * @chg: the change list
3714  */
3715 static int
3716 ice_add_prof_id_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig, u64 hdl,
3717 		     bool rev, struct list_head *chg)
3718 {
3719 	/* Masks that ignore flags */
3720 	u8 vl_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3721 	u8 dc_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0x00, 0x00, 0x00 };
3722 	u8 nm_msk[ICE_TCAM_KEY_VAL_SZ] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
3723 	struct ice_prof_map *map;
3724 	struct ice_vsig_prof *t;
3725 	struct ice_chs_chg *p;
3726 	u16 vsig_idx, i;
3727 	int status = 0;
3728 
3729 	/* Error, if this VSIG already has this profile */
3730 	if (ice_has_prof_vsig(hw, blk, vsig, hdl))
3731 		return -EEXIST;
3732 
3733 	/* new VSIG profile structure */
3734 	t = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*t), GFP_KERNEL);
3735 	if (!t)
3736 		return -ENOMEM;
3737 
3738 	mutex_lock(&hw->blk[blk].es.prof_map_lock);
3739 	/* Get the details on the profile specified by the handle ID */
3740 	map = ice_search_prof_id(hw, blk, hdl);
3741 	if (!map) {
3742 		status = -ENOENT;
3743 		goto err_ice_add_prof_id_vsig;
3744 	}
3745 
3746 	t->profile_cookie = map->profile_cookie;
3747 	t->prof_id = map->prof_id;
3748 	t->tcam_count = map->ptg_cnt;
3749 
3750 	/* create TCAM entries */
3751 	for (i = 0; i < map->ptg_cnt; i++) {
3752 		u16 tcam_idx;
3753 
3754 		/* add TCAM to change list */
3755 		p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p), GFP_KERNEL);
3756 		if (!p) {
3757 			status = -ENOMEM;
3758 			goto err_ice_add_prof_id_vsig;
3759 		}
3760 
3761 		/* allocate the TCAM entry index */
3762 		/* for entries with empty attribute masks, allocate entry from
3763 		 * the bottom of the TCAM table; otherwise, allocate from the
3764 		 * top of the table in order to give it higher priority
3765 		 */
3766 		status = ice_alloc_tcam_ent(hw, blk, map->attr[i].mask == 0,
3767 					    &tcam_idx);
3768 		if (status) {
3769 			devm_kfree(ice_hw_to_dev(hw), p);
3770 			goto err_ice_add_prof_id_vsig;
3771 		}
3772 
3773 		t->tcam[i].ptg = map->ptg[i];
3774 		t->tcam[i].prof_id = map->prof_id;
3775 		t->tcam[i].tcam_idx = tcam_idx;
3776 		t->tcam[i].attr = map->attr[i];
3777 		t->tcam[i].in_use = true;
3778 
3779 		p->type = ICE_TCAM_ADD;
3780 		p->add_tcam_idx = true;
3781 		p->prof_id = t->tcam[i].prof_id;
3782 		p->ptg = t->tcam[i].ptg;
3783 		p->vsig = vsig;
3784 		p->tcam_idx = t->tcam[i].tcam_idx;
3785 
3786 		/* write the TCAM entry */
3787 		status = ice_tcam_write_entry(hw, blk, t->tcam[i].tcam_idx,
3788 					      t->tcam[i].prof_id,
3789 					      t->tcam[i].ptg, vsig, 0, 0,
3790 					      vl_msk, dc_msk, nm_msk);
3791 		if (status) {
3792 			devm_kfree(ice_hw_to_dev(hw), p);
3793 			goto err_ice_add_prof_id_vsig;
3794 		}
3795 
3796 		/* log change */
3797 		list_add(&p->list_entry, chg);
3798 	}
3799 
3800 	/* add profile to VSIG */
3801 	vsig_idx = vsig & ICE_VSIG_IDX_M;
3802 	if (rev)
3803 		list_add_tail(&t->list,
3804 			      &hw->blk[blk].xlt2.vsig_tbl[vsig_idx].prop_lst);
3805 	else
3806 		list_add(&t->list,
3807 			 &hw->blk[blk].xlt2.vsig_tbl[vsig_idx].prop_lst);
3808 
3809 	mutex_unlock(&hw->blk[blk].es.prof_map_lock);
3810 	return status;
3811 
3812 err_ice_add_prof_id_vsig:
3813 	mutex_unlock(&hw->blk[blk].es.prof_map_lock);
3814 	/* let caller clean up the change list */
3815 	devm_kfree(ice_hw_to_dev(hw), t);
3816 	return status;
3817 }
3818 
3819 /**
3820  * ice_create_prof_id_vsig - add a new VSIG with a single profile
3821  * @hw: pointer to the HW struct
3822  * @blk: hardware block
3823  * @vsi: the initial VSI that will be in VSIG
3824  * @hdl: the profile handle of the profile that will be added to the VSIG
3825  * @chg: the change list
3826  */
3827 static int
3828 ice_create_prof_id_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsi, u64 hdl,
3829 			struct list_head *chg)
3830 {
3831 	struct ice_chs_chg *p;
3832 	u16 new_vsig;
3833 	int status;
3834 
3835 	p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p), GFP_KERNEL);
3836 	if (!p)
3837 		return -ENOMEM;
3838 
3839 	new_vsig = ice_vsig_alloc(hw, blk);
3840 	if (!new_vsig) {
3841 		status = -EIO;
3842 		goto err_ice_create_prof_id_vsig;
3843 	}
3844 
3845 	status = ice_move_vsi(hw, blk, vsi, new_vsig, chg);
3846 	if (status)
3847 		goto err_ice_create_prof_id_vsig;
3848 
3849 	status = ice_add_prof_id_vsig(hw, blk, new_vsig, hdl, false, chg);
3850 	if (status)
3851 		goto err_ice_create_prof_id_vsig;
3852 
3853 	p->type = ICE_VSIG_ADD;
3854 	p->vsi = vsi;
3855 	p->orig_vsig = ICE_DEFAULT_VSIG;
3856 	p->vsig = new_vsig;
3857 
3858 	list_add(&p->list_entry, chg);
3859 
3860 	return 0;
3861 
3862 err_ice_create_prof_id_vsig:
3863 	/* let caller clean up the change list */
3864 	devm_kfree(ice_hw_to_dev(hw), p);
3865 	return status;
3866 }
3867 
3868 /**
3869  * ice_create_vsig_from_lst - create a new VSIG with a list of profiles
3870  * @hw: pointer to the HW struct
3871  * @blk: hardware block
3872  * @vsi: the initial VSI that will be in VSIG
3873  * @lst: the list of profile that will be added to the VSIG
3874  * @new_vsig: return of new VSIG
3875  * @chg: the change list
3876  */
3877 static int
3878 ice_create_vsig_from_lst(struct ice_hw *hw, enum ice_block blk, u16 vsi,
3879 			 struct list_head *lst, u16 *new_vsig,
3880 			 struct list_head *chg)
3881 {
3882 	struct ice_vsig_prof *t;
3883 	int status;
3884 	u16 vsig;
3885 
3886 	vsig = ice_vsig_alloc(hw, blk);
3887 	if (!vsig)
3888 		return -EIO;
3889 
3890 	status = ice_move_vsi(hw, blk, vsi, vsig, chg);
3891 	if (status)
3892 		return status;
3893 
3894 	list_for_each_entry(t, lst, list) {
3895 		/* Reverse the order here since we are copying the list */
3896 		status = ice_add_prof_id_vsig(hw, blk, vsig, t->profile_cookie,
3897 					      true, chg);
3898 		if (status)
3899 			return status;
3900 	}
3901 
3902 	*new_vsig = vsig;
3903 
3904 	return 0;
3905 }
3906 
3907 /**
3908  * ice_find_prof_vsig - find a VSIG with a specific profile handle
3909  * @hw: pointer to the HW struct
3910  * @blk: hardware block
3911  * @hdl: the profile handle of the profile to search for
3912  * @vsig: returns the VSIG with the matching profile
3913  */
3914 static bool
3915 ice_find_prof_vsig(struct ice_hw *hw, enum ice_block blk, u64 hdl, u16 *vsig)
3916 {
3917 	struct ice_vsig_prof *t;
3918 	struct list_head lst;
3919 	int status;
3920 
3921 	INIT_LIST_HEAD(&lst);
3922 
3923 	t = kzalloc(sizeof(*t), GFP_KERNEL);
3924 	if (!t)
3925 		return false;
3926 
3927 	t->profile_cookie = hdl;
3928 	list_add(&t->list, &lst);
3929 
3930 	status = ice_find_dup_props_vsig(hw, blk, &lst, vsig);
3931 
3932 	list_del(&t->list);
3933 	kfree(t);
3934 
3935 	return !status;
3936 }
3937 
3938 /**
3939  * ice_add_prof_id_flow - add profile flow
3940  * @hw: pointer to the HW struct
3941  * @blk: hardware block
3942  * @vsi: the VSI to enable with the profile specified by ID
3943  * @hdl: profile handle
3944  *
3945  * Calling this function will update the hardware tables to enable the
3946  * profile indicated by the ID parameter for the VSIs specified in the VSI
3947  * array. Once successfully called, the flow will be enabled.
3948  */
3949 int
3950 ice_add_prof_id_flow(struct ice_hw *hw, enum ice_block blk, u16 vsi, u64 hdl)
3951 {
3952 	struct ice_vsig_prof *tmp1, *del1;
3953 	struct ice_chs_chg *tmp, *del;
3954 	struct list_head union_lst;
3955 	struct list_head chg;
3956 	int status;
3957 	u16 vsig;
3958 
3959 	INIT_LIST_HEAD(&union_lst);
3960 	INIT_LIST_HEAD(&chg);
3961 
3962 	/* Get profile */
3963 	status = ice_get_prof(hw, blk, hdl, &chg);
3964 	if (status)
3965 		return status;
3966 
3967 	/* determine if VSI is already part of a VSIG */
3968 	status = ice_vsig_find_vsi(hw, blk, vsi, &vsig);
3969 	if (!status && vsig) {
3970 		bool only_vsi;
3971 		u16 or_vsig;
3972 		u16 ref;
3973 
3974 		/* found in VSIG */
3975 		or_vsig = vsig;
3976 
3977 		/* make sure that there is no overlap/conflict between the new
3978 		 * characteristics and the existing ones; we don't support that
3979 		 * scenario
3980 		 */
3981 		if (ice_has_prof_vsig(hw, blk, vsig, hdl)) {
3982 			status = -EEXIST;
3983 			goto err_ice_add_prof_id_flow;
3984 		}
3985 
3986 		/* last VSI in the VSIG? */
3987 		status = ice_vsig_get_ref(hw, blk, vsig, &ref);
3988 		if (status)
3989 			goto err_ice_add_prof_id_flow;
3990 		only_vsi = (ref == 1);
3991 
3992 		/* create a union of the current profiles and the one being
3993 		 * added
3994 		 */
3995 		status = ice_get_profs_vsig(hw, blk, vsig, &union_lst);
3996 		if (status)
3997 			goto err_ice_add_prof_id_flow;
3998 
3999 		status = ice_add_prof_to_lst(hw, blk, &union_lst, hdl);
4000 		if (status)
4001 			goto err_ice_add_prof_id_flow;
4002 
4003 		/* search for an existing VSIG with an exact charc match */
4004 		status = ice_find_dup_props_vsig(hw, blk, &union_lst, &vsig);
4005 		if (!status) {
4006 			/* move VSI to the VSIG that matches */
4007 			status = ice_move_vsi(hw, blk, vsi, vsig, &chg);
4008 			if (status)
4009 				goto err_ice_add_prof_id_flow;
4010 
4011 			/* VSI has been moved out of or_vsig. If the or_vsig had
4012 			 * only that VSI it is now empty and can be removed.
4013 			 */
4014 			if (only_vsi) {
4015 				status = ice_rem_vsig(hw, blk, or_vsig, &chg);
4016 				if (status)
4017 					goto err_ice_add_prof_id_flow;
4018 			}
4019 		} else if (only_vsi) {
4020 			/* If the original VSIG only contains one VSI, then it
4021 			 * will be the requesting VSI. In this case the VSI is
4022 			 * not sharing entries and we can simply add the new
4023 			 * profile to the VSIG.
4024 			 */
4025 			status = ice_add_prof_id_vsig(hw, blk, vsig, hdl, false,
4026 						      &chg);
4027 			if (status)
4028 				goto err_ice_add_prof_id_flow;
4029 
4030 			/* Adjust priorities */
4031 			status = ice_adj_prof_priorities(hw, blk, vsig, &chg);
4032 			if (status)
4033 				goto err_ice_add_prof_id_flow;
4034 		} else {
4035 			/* No match, so we need a new VSIG */
4036 			status = ice_create_vsig_from_lst(hw, blk, vsi,
4037 							  &union_lst, &vsig,
4038 							  &chg);
4039 			if (status)
4040 				goto err_ice_add_prof_id_flow;
4041 
4042 			/* Adjust priorities */
4043 			status = ice_adj_prof_priorities(hw, blk, vsig, &chg);
4044 			if (status)
4045 				goto err_ice_add_prof_id_flow;
4046 		}
4047 	} else {
4048 		/* need to find or add a VSIG */
4049 		/* search for an existing VSIG with an exact charc match */
4050 		if (ice_find_prof_vsig(hw, blk, hdl, &vsig)) {
4051 			/* found an exact match */
4052 			/* add or move VSI to the VSIG that matches */
4053 			status = ice_move_vsi(hw, blk, vsi, vsig, &chg);
4054 			if (status)
4055 				goto err_ice_add_prof_id_flow;
4056 		} else {
4057 			/* we did not find an exact match */
4058 			/* we need to add a VSIG */
4059 			status = ice_create_prof_id_vsig(hw, blk, vsi, hdl,
4060 							 &chg);
4061 			if (status)
4062 				goto err_ice_add_prof_id_flow;
4063 		}
4064 	}
4065 
4066 	/* update hardware */
4067 	if (!status)
4068 		status = ice_upd_prof_hw(hw, blk, &chg);
4069 
4070 err_ice_add_prof_id_flow:
4071 	list_for_each_entry_safe(del, tmp, &chg, list_entry) {
4072 		list_del(&del->list_entry);
4073 		devm_kfree(ice_hw_to_dev(hw), del);
4074 	}
4075 
4076 	list_for_each_entry_safe(del1, tmp1, &union_lst, list) {
4077 		list_del(&del1->list);
4078 		devm_kfree(ice_hw_to_dev(hw), del1);
4079 	}
4080 
4081 	return status;
4082 }
4083 
4084 /**
4085  * ice_rem_prof_from_list - remove a profile from list
4086  * @hw: pointer to the HW struct
4087  * @lst: list to remove the profile from
4088  * @hdl: the profile handle indicating the profile to remove
4089  */
4090 static int
4091 ice_rem_prof_from_list(struct ice_hw *hw, struct list_head *lst, u64 hdl)
4092 {
4093 	struct ice_vsig_prof *ent, *tmp;
4094 
4095 	list_for_each_entry_safe(ent, tmp, lst, list)
4096 		if (ent->profile_cookie == hdl) {
4097 			list_del(&ent->list);
4098 			devm_kfree(ice_hw_to_dev(hw), ent);
4099 			return 0;
4100 		}
4101 
4102 	return -ENOENT;
4103 }
4104 
4105 /**
4106  * ice_rem_prof_id_flow - remove flow
4107  * @hw: pointer to the HW struct
4108  * @blk: hardware block
4109  * @vsi: the VSI from which to remove the profile specified by ID
4110  * @hdl: profile tracking handle
4111  *
4112  * Calling this function will update the hardware tables to remove the
4113  * profile indicated by the ID parameter for the VSIs specified in the VSI
4114  * array. Once successfully called, the flow will be disabled.
4115  */
4116 int
4117 ice_rem_prof_id_flow(struct ice_hw *hw, enum ice_block blk, u16 vsi, u64 hdl)
4118 {
4119 	struct ice_vsig_prof *tmp1, *del1;
4120 	struct ice_chs_chg *tmp, *del;
4121 	struct list_head chg, copy;
4122 	int status;
4123 	u16 vsig;
4124 
4125 	INIT_LIST_HEAD(&copy);
4126 	INIT_LIST_HEAD(&chg);
4127 
4128 	/* determine if VSI is already part of a VSIG */
4129 	status = ice_vsig_find_vsi(hw, blk, vsi, &vsig);
4130 	if (!status && vsig) {
4131 		bool last_profile;
4132 		bool only_vsi;
4133 		u16 ref;
4134 
4135 		/* found in VSIG */
4136 		last_profile = ice_vsig_prof_id_count(hw, blk, vsig) == 1;
4137 		status = ice_vsig_get_ref(hw, blk, vsig, &ref);
4138 		if (status)
4139 			goto err_ice_rem_prof_id_flow;
4140 		only_vsi = (ref == 1);
4141 
4142 		if (only_vsi) {
4143 			/* If the original VSIG only contains one reference,
4144 			 * which will be the requesting VSI, then the VSI is not
4145 			 * sharing entries and we can simply remove the specific
4146 			 * characteristics from the VSIG.
4147 			 */
4148 
4149 			if (last_profile) {
4150 				/* If there are no profiles left for this VSIG,
4151 				 * then simply remove the VSIG.
4152 				 */
4153 				status = ice_rem_vsig(hw, blk, vsig, &chg);
4154 				if (status)
4155 					goto err_ice_rem_prof_id_flow;
4156 			} else {
4157 				status = ice_rem_prof_id_vsig(hw, blk, vsig,
4158 							      hdl, &chg);
4159 				if (status)
4160 					goto err_ice_rem_prof_id_flow;
4161 
4162 				/* Adjust priorities */
4163 				status = ice_adj_prof_priorities(hw, blk, vsig,
4164 								 &chg);
4165 				if (status)
4166 					goto err_ice_rem_prof_id_flow;
4167 			}
4168 
4169 		} else {
4170 			/* Make a copy of the VSIG's list of Profiles */
4171 			status = ice_get_profs_vsig(hw, blk, vsig, &copy);
4172 			if (status)
4173 				goto err_ice_rem_prof_id_flow;
4174 
4175 			/* Remove specified profile entry from the list */
4176 			status = ice_rem_prof_from_list(hw, &copy, hdl);
4177 			if (status)
4178 				goto err_ice_rem_prof_id_flow;
4179 
4180 			if (list_empty(&copy)) {
4181 				status = ice_move_vsi(hw, blk, vsi,
4182 						      ICE_DEFAULT_VSIG, &chg);
4183 				if (status)
4184 					goto err_ice_rem_prof_id_flow;
4185 
4186 			} else if (!ice_find_dup_props_vsig(hw, blk, &copy,
4187 							    &vsig)) {
4188 				/* found an exact match */
4189 				/* add or move VSI to the VSIG that matches */
4190 				/* Search for a VSIG with a matching profile
4191 				 * list
4192 				 */
4193 
4194 				/* Found match, move VSI to the matching VSIG */
4195 				status = ice_move_vsi(hw, blk, vsi, vsig, &chg);
4196 				if (status)
4197 					goto err_ice_rem_prof_id_flow;
4198 			} else {
4199 				/* since no existing VSIG supports this
4200 				 * characteristic pattern, we need to create a
4201 				 * new VSIG and TCAM entries
4202 				 */
4203 				status = ice_create_vsig_from_lst(hw, blk, vsi,
4204 								  &copy, &vsig,
4205 								  &chg);
4206 				if (status)
4207 					goto err_ice_rem_prof_id_flow;
4208 
4209 				/* Adjust priorities */
4210 				status = ice_adj_prof_priorities(hw, blk, vsig,
4211 								 &chg);
4212 				if (status)
4213 					goto err_ice_rem_prof_id_flow;
4214 			}
4215 		}
4216 	} else {
4217 		status = -ENOENT;
4218 	}
4219 
4220 	/* update hardware tables */
4221 	if (!status)
4222 		status = ice_upd_prof_hw(hw, blk, &chg);
4223 
4224 err_ice_rem_prof_id_flow:
4225 	list_for_each_entry_safe(del, tmp, &chg, list_entry) {
4226 		list_del(&del->list_entry);
4227 		devm_kfree(ice_hw_to_dev(hw), del);
4228 	}
4229 
4230 	list_for_each_entry_safe(del1, tmp1, &copy, list) {
4231 		list_del(&del1->list);
4232 		devm_kfree(ice_hw_to_dev(hw), del1);
4233 	}
4234 
4235 	return status;
4236 }
4237