1c7648810STony Nguyen // SPDX-License-Identifier: GPL-2.0
2c7648810STony Nguyen /* Copyright (c) 2019, Intel Corporation. */
3c7648810STony Nguyen 
4c7648810STony Nguyen #include "ice_common.h"
5c7648810STony Nguyen #include "ice_flex_pipe.h"
6c90ed40cSTony Nguyen #include "ice_flow.h"
7649c87c6SJacob Keller #include "ice.h"
8c7648810STony Nguyen 
943dbfc7bSTony Nguyen static const u32 ice_sect_lkup[ICE_BLK_COUNT][ICE_SECT_COUNT] = {
1043dbfc7bSTony Nguyen 	/* SWITCH */
1143dbfc7bSTony Nguyen 	{
1243dbfc7bSTony Nguyen 		ICE_SID_XLT0_SW,
1343dbfc7bSTony Nguyen 		ICE_SID_XLT_KEY_BUILDER_SW,
1443dbfc7bSTony Nguyen 		ICE_SID_XLT1_SW,
1543dbfc7bSTony Nguyen 		ICE_SID_XLT2_SW,
1643dbfc7bSTony Nguyen 		ICE_SID_PROFID_TCAM_SW,
1743dbfc7bSTony Nguyen 		ICE_SID_PROFID_REDIR_SW,
1843dbfc7bSTony Nguyen 		ICE_SID_FLD_VEC_SW,
1943dbfc7bSTony Nguyen 		ICE_SID_CDID_KEY_BUILDER_SW,
2043dbfc7bSTony Nguyen 		ICE_SID_CDID_REDIR_SW
2143dbfc7bSTony Nguyen 	},
2243dbfc7bSTony Nguyen 
2343dbfc7bSTony Nguyen 	/* ACL */
2443dbfc7bSTony Nguyen 	{
2543dbfc7bSTony Nguyen 		ICE_SID_XLT0_ACL,
2643dbfc7bSTony Nguyen 		ICE_SID_XLT_KEY_BUILDER_ACL,
2743dbfc7bSTony Nguyen 		ICE_SID_XLT1_ACL,
2843dbfc7bSTony Nguyen 		ICE_SID_XLT2_ACL,
2943dbfc7bSTony Nguyen 		ICE_SID_PROFID_TCAM_ACL,
3043dbfc7bSTony Nguyen 		ICE_SID_PROFID_REDIR_ACL,
3143dbfc7bSTony Nguyen 		ICE_SID_FLD_VEC_ACL,
3243dbfc7bSTony Nguyen 		ICE_SID_CDID_KEY_BUILDER_ACL,
3343dbfc7bSTony Nguyen 		ICE_SID_CDID_REDIR_ACL
3443dbfc7bSTony Nguyen 	},
3543dbfc7bSTony Nguyen 
3643dbfc7bSTony Nguyen 	/* FD */
3743dbfc7bSTony Nguyen 	{
3843dbfc7bSTony Nguyen 		ICE_SID_XLT0_FD,
3943dbfc7bSTony Nguyen 		ICE_SID_XLT_KEY_BUILDER_FD,
4043dbfc7bSTony Nguyen 		ICE_SID_XLT1_FD,
4143dbfc7bSTony Nguyen 		ICE_SID_XLT2_FD,
4243dbfc7bSTony Nguyen 		ICE_SID_PROFID_TCAM_FD,
4343dbfc7bSTony Nguyen 		ICE_SID_PROFID_REDIR_FD,
4443dbfc7bSTony Nguyen 		ICE_SID_FLD_VEC_FD,
4543dbfc7bSTony Nguyen 		ICE_SID_CDID_KEY_BUILDER_FD,
4643dbfc7bSTony Nguyen 		ICE_SID_CDID_REDIR_FD
4743dbfc7bSTony Nguyen 	},
4843dbfc7bSTony Nguyen 
4943dbfc7bSTony Nguyen 	/* RSS */
5043dbfc7bSTony Nguyen 	{
5143dbfc7bSTony Nguyen 		ICE_SID_XLT0_RSS,
5243dbfc7bSTony Nguyen 		ICE_SID_XLT_KEY_BUILDER_RSS,
5343dbfc7bSTony Nguyen 		ICE_SID_XLT1_RSS,
5443dbfc7bSTony Nguyen 		ICE_SID_XLT2_RSS,
5543dbfc7bSTony Nguyen 		ICE_SID_PROFID_TCAM_RSS,
5643dbfc7bSTony Nguyen 		ICE_SID_PROFID_REDIR_RSS,
5743dbfc7bSTony Nguyen 		ICE_SID_FLD_VEC_RSS,
5843dbfc7bSTony Nguyen 		ICE_SID_CDID_KEY_BUILDER_RSS,
5943dbfc7bSTony Nguyen 		ICE_SID_CDID_REDIR_RSS
6043dbfc7bSTony Nguyen 	},
6143dbfc7bSTony Nguyen 
6243dbfc7bSTony Nguyen 	/* PE */
6343dbfc7bSTony Nguyen 	{
6443dbfc7bSTony Nguyen 		ICE_SID_XLT0_PE,
6543dbfc7bSTony Nguyen 		ICE_SID_XLT_KEY_BUILDER_PE,
6643dbfc7bSTony Nguyen 		ICE_SID_XLT1_PE,
6743dbfc7bSTony Nguyen 		ICE_SID_XLT2_PE,
6843dbfc7bSTony Nguyen 		ICE_SID_PROFID_TCAM_PE,
6943dbfc7bSTony Nguyen 		ICE_SID_PROFID_REDIR_PE,
7043dbfc7bSTony Nguyen 		ICE_SID_FLD_VEC_PE,
7143dbfc7bSTony Nguyen 		ICE_SID_CDID_KEY_BUILDER_PE,
7243dbfc7bSTony Nguyen 		ICE_SID_CDID_REDIR_PE
7343dbfc7bSTony Nguyen 	}
7443dbfc7bSTony Nguyen };
7543dbfc7bSTony Nguyen 
7643dbfc7bSTony Nguyen /**
7743dbfc7bSTony Nguyen  * ice_sect_id - returns section ID
7843dbfc7bSTony Nguyen  * @blk: block type
7943dbfc7bSTony Nguyen  * @sect: section type
8043dbfc7bSTony Nguyen  *
8143dbfc7bSTony Nguyen  * This helper function returns the proper section ID given a block type and a
8243dbfc7bSTony Nguyen  * section type.
8343dbfc7bSTony Nguyen  */
ice_sect_id(enum ice_block blk,enum ice_sect sect)8443dbfc7bSTony Nguyen static u32 ice_sect_id(enum ice_block blk, enum ice_sect sect)
8543dbfc7bSTony Nguyen {
8643dbfc7bSTony Nguyen 	return ice_sect_lkup[blk][sect];
8743dbfc7bSTony Nguyen }
8843dbfc7bSTony Nguyen 
8932d63fa1STony Nguyen /**
908818b954SHaiyue Wang  * ice_hw_ptype_ena - check if the PTYPE is enabled or not
918818b954SHaiyue Wang  * @hw: pointer to the HW structure
928818b954SHaiyue Wang  * @ptype: the hardware PTYPE
938818b954SHaiyue Wang  */
ice_hw_ptype_ena(struct ice_hw * hw,u16 ptype)948818b954SHaiyue Wang bool ice_hw_ptype_ena(struct ice_hw *hw, u16 ptype)
958818b954SHaiyue Wang {
968818b954SHaiyue Wang 	return ptype < ICE_FLOW_PTYPE_MAX &&
978818b954SHaiyue Wang 	       test_bit(ptype, hw->hw_ptype);
988818b954SHaiyue Wang }
998818b954SHaiyue Wang 
100451f2c44STony Nguyen /* Key creation */
101451f2c44STony Nguyen 
102451f2c44STony Nguyen #define ICE_DC_KEY	0x1	/* don't care */
103451f2c44STony Nguyen #define ICE_DC_KEYINV	0x1
104451f2c44STony Nguyen #define ICE_NM_KEY	0x0	/* never match */
105451f2c44STony Nguyen #define ICE_NM_KEYINV	0x0
106451f2c44STony Nguyen #define ICE_0_KEY	0x1	/* match 0 */
107451f2c44STony Nguyen #define ICE_0_KEYINV	0x0
108451f2c44STony Nguyen #define ICE_1_KEY	0x0	/* match 1 */
109451f2c44STony Nguyen #define ICE_1_KEYINV	0x1
110451f2c44STony Nguyen 
111451f2c44STony Nguyen /**
112451f2c44STony Nguyen  * ice_gen_key_word - generate 16-bits of a key/mask word
113451f2c44STony Nguyen  * @val: the value
114451f2c44STony Nguyen  * @valid: valid bits mask (change only the valid bits)
115451f2c44STony Nguyen  * @dont_care: don't care mask
116451f2c44STony Nguyen  * @nvr_mtch: never match mask
117451f2c44STony Nguyen  * @key: pointer to an array of where the resulting key portion
118451f2c44STony Nguyen  * @key_inv: pointer to an array of where the resulting key invert portion
119451f2c44STony Nguyen  *
120451f2c44STony Nguyen  * This function generates 16-bits from a 8-bit value, an 8-bit don't care mask
121451f2c44STony Nguyen  * and an 8-bit never match mask. The 16-bits of output are divided into 8 bits
122451f2c44STony Nguyen  * of key and 8 bits of key invert.
123451f2c44STony Nguyen  *
124451f2c44STony Nguyen  *     '0' =    b01, always match a 0 bit
125451f2c44STony Nguyen  *     '1' =    b10, always match a 1 bit
126451f2c44STony Nguyen  *     '?' =    b11, don't care bit (always matches)
127451f2c44STony Nguyen  *     '~' =    b00, never match bit
128451f2c44STony Nguyen  *
129451f2c44STony Nguyen  * Input:
130451f2c44STony Nguyen  *          val:         b0  1  0  1  0  1
131451f2c44STony Nguyen  *          dont_care:   b0  0  1  1  0  0
132451f2c44STony Nguyen  *          never_mtch:  b0  0  0  0  1  1
133451f2c44STony Nguyen  *          ------------------------------
134451f2c44STony Nguyen  * Result:  key:        b01 10 11 11 00 00
135451f2c44STony Nguyen  */
1365e24d598STony Nguyen static int
ice_gen_key_word(u8 val,u8 valid,u8 dont_care,u8 nvr_mtch,u8 * key,u8 * key_inv)137451f2c44STony Nguyen ice_gen_key_word(u8 val, u8 valid, u8 dont_care, u8 nvr_mtch, u8 *key,
138451f2c44STony Nguyen 		 u8 *key_inv)
139451f2c44STony Nguyen {
140451f2c44STony Nguyen 	u8 in_key = *key, in_key_inv = *key_inv;
141451f2c44STony Nguyen 	u8 i;
142451f2c44STony Nguyen 
143451f2c44STony Nguyen 	/* 'dont_care' and 'nvr_mtch' masks cannot overlap */
144451f2c44STony Nguyen 	if ((dont_care ^ nvr_mtch) != (dont_care | nvr_mtch))
145d54699e2STony Nguyen 		return -EIO;
146451f2c44STony Nguyen 
147451f2c44STony Nguyen 	*key = 0;
148451f2c44STony Nguyen 	*key_inv = 0;
149451f2c44STony Nguyen 
150451f2c44STony Nguyen 	/* encode the 8 bits into 8-bit key and 8-bit key invert */
151451f2c44STony Nguyen 	for (i = 0; i < 8; i++) {
152451f2c44STony Nguyen 		*key >>= 1;
153451f2c44STony Nguyen 		*key_inv >>= 1;
154451f2c44STony Nguyen 
155451f2c44STony Nguyen 		if (!(valid & 0x1)) { /* change only valid bits */
156451f2c44STony Nguyen 			*key |= (in_key & 0x1) << 7;
157451f2c44STony Nguyen 			*key_inv |= (in_key_inv & 0x1) << 7;
158451f2c44STony Nguyen 		} else if (dont_care & 0x1) { /* don't care bit */
159451f2c44STony Nguyen 			*key |= ICE_DC_KEY << 7;
160451f2c44STony Nguyen 			*key_inv |= ICE_DC_KEYINV << 7;
161451f2c44STony Nguyen 		} else if (nvr_mtch & 0x1) { /* never match bit */
162451f2c44STony Nguyen 			*key |= ICE_NM_KEY << 7;
163451f2c44STony Nguyen 			*key_inv |= ICE_NM_KEYINV << 7;
164451f2c44STony Nguyen 		} else if (val & 0x01) { /* exact 1 match */
165451f2c44STony Nguyen 			*key |= ICE_1_KEY << 7;
166451f2c44STony Nguyen 			*key_inv |= ICE_1_KEYINV << 7;
167451f2c44STony Nguyen 		} else { /* exact 0 match */
168451f2c44STony Nguyen 			*key |= ICE_0_KEY << 7;
169451f2c44STony Nguyen 			*key_inv |= ICE_0_KEYINV << 7;
170451f2c44STony Nguyen 		}
171451f2c44STony Nguyen 
172451f2c44STony Nguyen 		dont_care >>= 1;
173451f2c44STony Nguyen 		nvr_mtch >>= 1;
174451f2c44STony Nguyen 		valid >>= 1;
175451f2c44STony Nguyen 		val >>= 1;
176451f2c44STony Nguyen 		in_key >>= 1;
177451f2c44STony Nguyen 		in_key_inv >>= 1;
178451f2c44STony Nguyen 	}
179451f2c44STony Nguyen 
180451f2c44STony Nguyen 	return 0;
181451f2c44STony Nguyen }
182451f2c44STony Nguyen 
183451f2c44STony Nguyen /**
184451f2c44STony Nguyen  * ice_bits_max_set - determine if the number of bits set is within a maximum
185451f2c44STony Nguyen  * @mask: pointer to the byte array which is the mask
186451f2c44STony Nguyen  * @size: the number of bytes in the mask
187451f2c44STony Nguyen  * @max: the max number of set bits
188451f2c44STony Nguyen  *
189451f2c44STony Nguyen  * This function determines if there are at most 'max' number of bits set in an
190451f2c44STony Nguyen  * array. Returns true if the number for bits set is <= max or will return false
191451f2c44STony Nguyen  * otherwise.
192451f2c44STony Nguyen  */
ice_bits_max_set(const u8 * mask,u16 size,u16 max)193451f2c44STony Nguyen static bool ice_bits_max_set(const u8 *mask, u16 size, u16 max)
194451f2c44STony Nguyen {
195451f2c44STony Nguyen 	u16 count = 0;
196451f2c44STony Nguyen 	u16 i;
197451f2c44STony Nguyen 
198451f2c44STony Nguyen 	/* check each byte */
199451f2c44STony Nguyen 	for (i = 0; i < size; i++) {
200451f2c44STony Nguyen 		/* if 0, go to next byte */
201451f2c44STony Nguyen 		if (!mask[i])
202451f2c44STony Nguyen 			continue;
203451f2c44STony Nguyen 
204451f2c44STony Nguyen 		/* We know there is at least one set bit in this byte because of
205451f2c44STony Nguyen 		 * the above check; if we already have found 'max' number of
206451f2c44STony Nguyen 		 * bits set, then we can return failure now.
207451f2c44STony Nguyen 		 */
208451f2c44STony Nguyen 		if (count == max)
209451f2c44STony Nguyen 			return false;
210451f2c44STony Nguyen 
211451f2c44STony Nguyen 		/* count the bits in this byte, checking threshold */
212451f2c44STony Nguyen 		count += hweight8(mask[i]);
213451f2c44STony Nguyen 		if (count > max)
214451f2c44STony Nguyen 			return false;
215451f2c44STony Nguyen 	}
216451f2c44STony Nguyen 
217451f2c44STony Nguyen 	return true;
218451f2c44STony Nguyen }
219451f2c44STony Nguyen 
220451f2c44STony Nguyen /**
221451f2c44STony Nguyen  * ice_set_key - generate a variable sized key with multiples of 16-bits
222451f2c44STony Nguyen  * @key: pointer to where the key will be stored
223451f2c44STony Nguyen  * @size: the size of the complete key in bytes (must be even)
224451f2c44STony Nguyen  * @val: array of 8-bit values that makes up the value portion of the key
225451f2c44STony Nguyen  * @upd: array of 8-bit masks that determine what key portion to update
226451f2c44STony Nguyen  * @dc: array of 8-bit masks that make up the don't care mask
227451f2c44STony Nguyen  * @nm: array of 8-bit masks that make up the never match mask
228451f2c44STony Nguyen  * @off: the offset of the first byte in the key to update
229451f2c44STony Nguyen  * @len: the number of bytes in the key update
230451f2c44STony Nguyen  *
231451f2c44STony Nguyen  * This function generates a key from a value, a don't care mask and a never
232451f2c44STony Nguyen  * match mask.
233451f2c44STony Nguyen  * upd, dc, and nm are optional parameters, and can be NULL:
2347dbc63f0STony Nguyen  *	upd == NULL --> upd mask is all 1's (update all bits)
235451f2c44STony Nguyen  *	dc == NULL --> dc mask is all 0's (no don't care bits)
236451f2c44STony Nguyen  *	nm == NULL --> nm mask is all 0's (no never match bits)
237451f2c44STony Nguyen  */
2385e24d598STony Nguyen static int
ice_set_key(u8 * key,u16 size,u8 * val,u8 * upd,u8 * dc,u8 * nm,u16 off,u16 len)239451f2c44STony Nguyen ice_set_key(u8 *key, u16 size, u8 *val, u8 *upd, u8 *dc, u8 *nm, u16 off,
240451f2c44STony Nguyen 	    u16 len)
241451f2c44STony Nguyen {
242451f2c44STony Nguyen 	u16 half_size;
243451f2c44STony Nguyen 	u16 i;
244451f2c44STony Nguyen 
245451f2c44STony Nguyen 	/* size must be a multiple of 2 bytes. */
246451f2c44STony Nguyen 	if (size % 2)
247d54699e2STony Nguyen 		return -EIO;
248451f2c44STony Nguyen 
249451f2c44STony Nguyen 	half_size = size / 2;
250451f2c44STony Nguyen 	if (off + len > half_size)
251d54699e2STony Nguyen 		return -EIO;
252451f2c44STony Nguyen 
253451f2c44STony Nguyen 	/* Make sure at most one bit is set in the never match mask. Having more
254451f2c44STony Nguyen 	 * than one never match mask bit set will cause HW to consume excessive
255451f2c44STony Nguyen 	 * power otherwise; this is a power management efficiency check.
256451f2c44STony Nguyen 	 */
257451f2c44STony Nguyen #define ICE_NVR_MTCH_BITS_MAX	1
258451f2c44STony Nguyen 	if (nm && !ice_bits_max_set(nm, len, ICE_NVR_MTCH_BITS_MAX))
259d54699e2STony Nguyen 		return -EIO;
260451f2c44STony Nguyen 
261451f2c44STony Nguyen 	for (i = 0; i < len; i++)
262451f2c44STony Nguyen 		if (ice_gen_key_word(val[i], upd ? upd[i] : 0xff,
263451f2c44STony Nguyen 				     dc ? dc[i] : 0, nm ? nm[i] : 0,
264451f2c44STony Nguyen 				     key + off + i, key + half_size + off + i))
265d54699e2STony Nguyen 			return -EIO;
266451f2c44STony Nguyen 
267451f2c44STony Nguyen 	return 0;
268451f2c44STony Nguyen }
269451f2c44STony Nguyen 
27032d63fa1STony Nguyen /**
27143dbfc7bSTony Nguyen  * ice_acquire_change_lock
27243dbfc7bSTony Nguyen  * @hw: pointer to the HW structure
27343dbfc7bSTony Nguyen  * @access: access type (read or write)
27443dbfc7bSTony Nguyen  *
27543dbfc7bSTony Nguyen  * This function will request ownership of the change lock.
27643dbfc7bSTony Nguyen  */
2775e24d598STony Nguyen int
ice_acquire_change_lock(struct ice_hw * hw,enum ice_aq_res_access_type access)27843dbfc7bSTony Nguyen ice_acquire_change_lock(struct ice_hw *hw, enum ice_aq_res_access_type access)
27943dbfc7bSTony Nguyen {
28043dbfc7bSTony Nguyen 	return ice_acquire_res(hw, ICE_CHANGE_LOCK_RES_ID, access,
28143dbfc7bSTony Nguyen 			       ICE_CHANGE_LOCK_TIMEOUT);
28243dbfc7bSTony Nguyen }
28343dbfc7bSTony Nguyen 
28443dbfc7bSTony Nguyen /**
28543dbfc7bSTony Nguyen  * ice_release_change_lock
28643dbfc7bSTony Nguyen  * @hw: pointer to the HW structure
28743dbfc7bSTony Nguyen  *
28843dbfc7bSTony Nguyen  * This function will release the change lock using the proper Admin Command.
28943dbfc7bSTony Nguyen  */
ice_release_change_lock(struct ice_hw * hw)290fd2a6b71SDan Nowlin void ice_release_change_lock(struct ice_hw *hw)
29143dbfc7bSTony Nguyen {
29243dbfc7bSTony Nguyen 	ice_release_res(hw, ICE_CHANGE_LOCK_RES_ID);
29343dbfc7bSTony Nguyen }
29443dbfc7bSTony Nguyen 
29543dbfc7bSTony Nguyen /**
296cac2a27cSHenry Tieman  * ice_get_open_tunnel_port - retrieve an open tunnel port
297cac2a27cSHenry Tieman  * @hw: pointer to the HW structure
298cac2a27cSHenry Tieman  * @port: returns open port
299de6acd1cSMichal Swiatkowski  * @type: type of tunnel, can be TNL_LAST if it doesn't matter
300cac2a27cSHenry Tieman  */
301cac2a27cSHenry Tieman bool
ice_get_open_tunnel_port(struct ice_hw * hw,u16 * port,enum ice_tunnel_type type)302de6acd1cSMichal Swiatkowski ice_get_open_tunnel_port(struct ice_hw *hw, u16 *port,
303de6acd1cSMichal Swiatkowski 			 enum ice_tunnel_type type)
304cac2a27cSHenry Tieman {
305cac2a27cSHenry Tieman 	bool res = false;
306cac2a27cSHenry Tieman 	u16 i;
307cac2a27cSHenry Tieman 
308cac2a27cSHenry Tieman 	mutex_lock(&hw->tnl_lock);
309cac2a27cSHenry Tieman 
310cac2a27cSHenry Tieman 	for (i = 0; i < hw->tnl.count && i < ICE_TUNNEL_MAX_ENTRIES; i++)
311de6acd1cSMichal Swiatkowski 		if (hw->tnl.tbl[i].valid && hw->tnl.tbl[i].port &&
312de6acd1cSMichal Swiatkowski 		    (type == TNL_LAST || type == hw->tnl.tbl[i].type)) {
313cac2a27cSHenry Tieman 			*port = hw->tnl.tbl[i].port;
314cac2a27cSHenry Tieman 			res = true;
315cac2a27cSHenry Tieman 			break;
316cac2a27cSHenry Tieman 		}
317cac2a27cSHenry Tieman 
318cac2a27cSHenry Tieman 	mutex_unlock(&hw->tnl_lock);
319cac2a27cSHenry Tieman 
320cac2a27cSHenry Tieman 	return res;
321cac2a27cSHenry Tieman }
322cac2a27cSHenry Tieman 
323cac2a27cSHenry Tieman /**
324a1ffafb0SBrett Creeley  * ice_upd_dvm_boost_entry
325a1ffafb0SBrett Creeley  * @hw: pointer to the HW structure
326a1ffafb0SBrett Creeley  * @entry: pointer to double vlan boost entry info
327a1ffafb0SBrett Creeley  */
328a1ffafb0SBrett Creeley static int
ice_upd_dvm_boost_entry(struct ice_hw * hw,struct ice_dvm_entry * entry)329a1ffafb0SBrett Creeley ice_upd_dvm_boost_entry(struct ice_hw *hw, struct ice_dvm_entry *entry)
330a1ffafb0SBrett Creeley {
331a1ffafb0SBrett Creeley 	struct ice_boost_tcam_section *sect_rx, *sect_tx;
332a1ffafb0SBrett Creeley 	int status = -ENOSPC;
333a1ffafb0SBrett Creeley 	struct ice_buf_build *bld;
334a1ffafb0SBrett Creeley 	u8 val, dc, nm;
335a1ffafb0SBrett Creeley 
336a1ffafb0SBrett Creeley 	bld = ice_pkg_buf_alloc(hw);
337a1ffafb0SBrett Creeley 	if (!bld)
338a1ffafb0SBrett Creeley 		return -ENOMEM;
339a1ffafb0SBrett Creeley 
340a1ffafb0SBrett Creeley 	/* allocate 2 sections, one for Rx parser, one for Tx parser */
341a1ffafb0SBrett Creeley 	if (ice_pkg_buf_reserve_section(bld, 2))
342a1ffafb0SBrett Creeley 		goto ice_upd_dvm_boost_entry_err;
343a1ffafb0SBrett Creeley 
344a1ffafb0SBrett Creeley 	sect_rx = ice_pkg_buf_alloc_section(bld, ICE_SID_RXPARSER_BOOST_TCAM,
345a1ffafb0SBrett Creeley 					    struct_size(sect_rx, tcam, 1));
346a1ffafb0SBrett Creeley 	if (!sect_rx)
347a1ffafb0SBrett Creeley 		goto ice_upd_dvm_boost_entry_err;
348a1ffafb0SBrett Creeley 	sect_rx->count = cpu_to_le16(1);
349a1ffafb0SBrett Creeley 
350a1ffafb0SBrett Creeley 	sect_tx = ice_pkg_buf_alloc_section(bld, ICE_SID_TXPARSER_BOOST_TCAM,
351a1ffafb0SBrett Creeley 					    struct_size(sect_tx, tcam, 1));
352a1ffafb0SBrett Creeley 	if (!sect_tx)
353a1ffafb0SBrett Creeley 		goto ice_upd_dvm_boost_entry_err;
354a1ffafb0SBrett Creeley 	sect_tx->count = cpu_to_le16(1);
355a1ffafb0SBrett Creeley 
356a1ffafb0SBrett Creeley 	/* copy original boost entry to update package buffer */
357a1ffafb0SBrett Creeley 	memcpy(sect_rx->tcam, entry->boost_entry, sizeof(*sect_rx->tcam));
358a1ffafb0SBrett Creeley 
359a1ffafb0SBrett Creeley 	/* re-write the don't care and never match bits accordingly */
360a1ffafb0SBrett Creeley 	if (entry->enable) {
361a1ffafb0SBrett Creeley 		/* all bits are don't care */
362a1ffafb0SBrett Creeley 		val = 0x00;
363a1ffafb0SBrett Creeley 		dc = 0xFF;
364a1ffafb0SBrett Creeley 		nm = 0x00;
365a1ffafb0SBrett Creeley 	} else {
366a1ffafb0SBrett Creeley 		/* disable, one never match bit, the rest are don't care */
367a1ffafb0SBrett Creeley 		val = 0x00;
368a1ffafb0SBrett Creeley 		dc = 0xF7;
369a1ffafb0SBrett Creeley 		nm = 0x08;
370a1ffafb0SBrett Creeley 	}
371a1ffafb0SBrett Creeley 
372a1ffafb0SBrett Creeley 	ice_set_key((u8 *)&sect_rx->tcam[0].key, sizeof(sect_rx->tcam[0].key),
373a1ffafb0SBrett Creeley 		    &val, NULL, &dc, &nm, 0, sizeof(u8));
374a1ffafb0SBrett Creeley 
375a1ffafb0SBrett Creeley 	/* exact copy of entry to Tx section entry */
376a1ffafb0SBrett Creeley 	memcpy(sect_tx->tcam, sect_rx->tcam, sizeof(*sect_tx->tcam));
377a1ffafb0SBrett Creeley 
378a1ffafb0SBrett Creeley 	status = ice_update_pkg_no_lock(hw, ice_pkg_buf(bld), 1);
379a1ffafb0SBrett Creeley 
380a1ffafb0SBrett Creeley ice_upd_dvm_boost_entry_err:
381a1ffafb0SBrett Creeley 	ice_pkg_buf_free(hw, bld);
382a1ffafb0SBrett Creeley 
383a1ffafb0SBrett Creeley 	return status;
384a1ffafb0SBrett Creeley }
385a1ffafb0SBrett Creeley 
386a1ffafb0SBrett Creeley /**
387a1ffafb0SBrett Creeley  * ice_set_dvm_boost_entries
388a1ffafb0SBrett Creeley  * @hw: pointer to the HW structure
389a1ffafb0SBrett Creeley  *
390a1ffafb0SBrett Creeley  * Enable double vlan by updating the appropriate boost tcam entries.
391a1ffafb0SBrett Creeley  */
ice_set_dvm_boost_entries(struct ice_hw * hw)392a1ffafb0SBrett Creeley int ice_set_dvm_boost_entries(struct ice_hw *hw)
393a1ffafb0SBrett Creeley {
394a1ffafb0SBrett Creeley 	u16 i;
395a1ffafb0SBrett Creeley 
396a1ffafb0SBrett Creeley 	for (i = 0; i < hw->dvm_upd.count; i++) {
397*bd557d97STony Nguyen 		int status;
398*bd557d97STony Nguyen 
399a1ffafb0SBrett Creeley 		status = ice_upd_dvm_boost_entry(hw, &hw->dvm_upd.tbl[i]);
400a1ffafb0SBrett Creeley 		if (status)
401a1ffafb0SBrett Creeley 			return status;
402a1ffafb0SBrett Creeley 	}
403a1ffafb0SBrett Creeley 
404a1ffafb0SBrett Creeley 	return 0;
405a1ffafb0SBrett Creeley }
406a1ffafb0SBrett Creeley 
407a1ffafb0SBrett Creeley /**
408b20e6c17SJakub Kicinski  * ice_tunnel_idx_to_entry - convert linear index to the sparse one
409b20e6c17SJakub Kicinski  * @hw: pointer to the HW structure
410b20e6c17SJakub Kicinski  * @type: type of tunnel
411b20e6c17SJakub Kicinski  * @idx: linear index
412b20e6c17SJakub Kicinski  *
413b20e6c17SJakub Kicinski  * Stack assumes we have 2 linear tables with indexes [0, count_valid),
414b20e6c17SJakub Kicinski  * but really the port table may be sprase, and types are mixed, so convert
415b20e6c17SJakub Kicinski  * the stack index into the device index.
416b20e6c17SJakub Kicinski  */
ice_tunnel_idx_to_entry(struct ice_hw * hw,enum ice_tunnel_type type,u16 idx)417b20e6c17SJakub Kicinski static u16 ice_tunnel_idx_to_entry(struct ice_hw *hw, enum ice_tunnel_type type,
418b20e6c17SJakub Kicinski 				   u16 idx)
419b20e6c17SJakub Kicinski {
420b20e6c17SJakub Kicinski 	u16 i;
421b20e6c17SJakub Kicinski 
422b20e6c17SJakub Kicinski 	for (i = 0; i < hw->tnl.count && i < ICE_TUNNEL_MAX_ENTRIES; i++)
423b20e6c17SJakub Kicinski 		if (hw->tnl.tbl[i].valid &&
424b20e6c17SJakub Kicinski 		    hw->tnl.tbl[i].type == type &&
425e4c2efa1SMichal Swiatkowski 		    idx-- == 0)
426b20e6c17SJakub Kicinski 			return i;
427b20e6c17SJakub Kicinski 
428b20e6c17SJakub Kicinski 	WARN_ON_ONCE(1);
429b20e6c17SJakub Kicinski 	return 0;
430b20e6c17SJakub Kicinski }
431b20e6c17SJakub Kicinski 
432b20e6c17SJakub Kicinski /**
433a4e82a81STony Nguyen  * ice_create_tunnel
434a4e82a81STony Nguyen  * @hw: pointer to the HW structure
435b20e6c17SJakub Kicinski  * @index: device table entry
436a4e82a81STony Nguyen  * @type: type of tunnel
437a4e82a81STony Nguyen  * @port: port of tunnel to create
438a4e82a81STony Nguyen  *
439a4e82a81STony Nguyen  * Create a tunnel by updating the parse graph in the parser. We do that by
440a4e82a81STony Nguyen  * creating a package buffer with the tunnel info and issuing an update package
441a4e82a81STony Nguyen  * command.
442a4e82a81STony Nguyen  */
4435e24d598STony Nguyen static int
ice_create_tunnel(struct ice_hw * hw,u16 index,enum ice_tunnel_type type,u16 port)444b20e6c17SJakub Kicinski ice_create_tunnel(struct ice_hw *hw, u16 index,
445b20e6c17SJakub Kicinski 		  enum ice_tunnel_type type, u16 port)
446a4e82a81STony Nguyen {
447a4e82a81STony Nguyen 	struct ice_boost_tcam_section *sect_rx, *sect_tx;
448a4e82a81STony Nguyen 	struct ice_buf_build *bld;
4495518ac2aSTony Nguyen 	int status = -ENOSPC;
450a4e82a81STony Nguyen 
451a4e82a81STony Nguyen 	mutex_lock(&hw->tnl_lock);
452a4e82a81STony Nguyen 
453a4e82a81STony Nguyen 	bld = ice_pkg_buf_alloc(hw);
454a4e82a81STony Nguyen 	if (!bld) {
455d54699e2STony Nguyen 		status = -ENOMEM;
456a4e82a81STony Nguyen 		goto ice_create_tunnel_end;
457a4e82a81STony Nguyen 	}
458a4e82a81STony Nguyen 
459a4e82a81STony Nguyen 	/* allocate 2 sections, one for Rx parser, one for Tx parser */
460a4e82a81STony Nguyen 	if (ice_pkg_buf_reserve_section(bld, 2))
461a4e82a81STony Nguyen 		goto ice_create_tunnel_err;
462a4e82a81STony Nguyen 
463a4e82a81STony Nguyen 	sect_rx = ice_pkg_buf_alloc_section(bld, ICE_SID_RXPARSER_BOOST_TCAM,
46466486d89SBruce Allan 					    struct_size(sect_rx, tcam, 1));
465a4e82a81STony Nguyen 	if (!sect_rx)
466a4e82a81STony Nguyen 		goto ice_create_tunnel_err;
467a4e82a81STony Nguyen 	sect_rx->count = cpu_to_le16(1);
468a4e82a81STony Nguyen 
469a4e82a81STony Nguyen 	sect_tx = ice_pkg_buf_alloc_section(bld, ICE_SID_TXPARSER_BOOST_TCAM,
47066486d89SBruce Allan 					    struct_size(sect_tx, tcam, 1));
471a4e82a81STony Nguyen 	if (!sect_tx)
472a4e82a81STony Nguyen 		goto ice_create_tunnel_err;
473a4e82a81STony Nguyen 	sect_tx->count = cpu_to_le16(1);
474a4e82a81STony Nguyen 
475a4e82a81STony Nguyen 	/* copy original boost entry to update package buffer */
476a4e82a81STony Nguyen 	memcpy(sect_rx->tcam, hw->tnl.tbl[index].boost_entry,
477a4e82a81STony Nguyen 	       sizeof(*sect_rx->tcam));
478a4e82a81STony Nguyen 
479a4e82a81STony Nguyen 	/* over-write the never-match dest port key bits with the encoded port
480a4e82a81STony Nguyen 	 * bits
481a4e82a81STony Nguyen 	 */
482a4e82a81STony Nguyen 	ice_set_key((u8 *)&sect_rx->tcam[0].key, sizeof(sect_rx->tcam[0].key),
483a4e82a81STony Nguyen 		    (u8 *)&port, NULL, NULL, NULL,
48488865fc4SKarol Kolacinski 		    (u16)offsetof(struct ice_boost_key_value, hv_dst_port_key),
485a4e82a81STony Nguyen 		    sizeof(sect_rx->tcam[0].key.key.hv_dst_port_key));
486a4e82a81STony Nguyen 
487a4e82a81STony Nguyen 	/* exact copy of entry to Tx section entry */
488a4e82a81STony Nguyen 	memcpy(sect_tx->tcam, sect_rx->tcam, sizeof(*sect_tx->tcam));
489a4e82a81STony Nguyen 
490a4e82a81STony Nguyen 	status = ice_update_pkg(hw, ice_pkg_buf(bld), 1);
491b20e6c17SJakub Kicinski 	if (!status)
492a4e82a81STony Nguyen 		hw->tnl.tbl[index].port = port;
493a4e82a81STony Nguyen 
494a4e82a81STony Nguyen ice_create_tunnel_err:
495a4e82a81STony Nguyen 	ice_pkg_buf_free(hw, bld);
496a4e82a81STony Nguyen 
497a4e82a81STony Nguyen ice_create_tunnel_end:
498a4e82a81STony Nguyen 	mutex_unlock(&hw->tnl_lock);
499a4e82a81STony Nguyen 
500a4e82a81STony Nguyen 	return status;
501a4e82a81STony Nguyen }
502a4e82a81STony Nguyen 
503a4e82a81STony Nguyen /**
504a4e82a81STony Nguyen  * ice_destroy_tunnel
505a4e82a81STony Nguyen  * @hw: pointer to the HW structure
506b20e6c17SJakub Kicinski  * @index: device table entry
507b20e6c17SJakub Kicinski  * @type: type of tunnel
508a4e82a81STony Nguyen  * @port: port of tunnel to destroy (ignored if the all parameter is true)
509a4e82a81STony Nguyen  *
510a4e82a81STony Nguyen  * Destroys a tunnel or all tunnels by creating an update package buffer
511a4e82a81STony Nguyen  * targeting the specific updates requested and then performing an update
512a4e82a81STony Nguyen  * package.
513a4e82a81STony Nguyen  */
5145e24d598STony Nguyen static int
ice_destroy_tunnel(struct ice_hw * hw,u16 index,enum ice_tunnel_type type,u16 port)515b20e6c17SJakub Kicinski ice_destroy_tunnel(struct ice_hw *hw, u16 index, enum ice_tunnel_type type,
516b20e6c17SJakub Kicinski 		   u16 port)
517a4e82a81STony Nguyen {
518a4e82a81STony Nguyen 	struct ice_boost_tcam_section *sect_rx, *sect_tx;
519a4e82a81STony Nguyen 	struct ice_buf_build *bld;
5205518ac2aSTony Nguyen 	int status = -ENOSPC;
521a4e82a81STony Nguyen 
522a4e82a81STony Nguyen 	mutex_lock(&hw->tnl_lock);
523a4e82a81STony Nguyen 
524b20e6c17SJakub Kicinski 	if (WARN_ON(!hw->tnl.tbl[index].valid ||
525b20e6c17SJakub Kicinski 		    hw->tnl.tbl[index].type != type ||
526b20e6c17SJakub Kicinski 		    hw->tnl.tbl[index].port != port)) {
527d54699e2STony Nguyen 		status = -EIO;
528a4e82a81STony Nguyen 		goto ice_destroy_tunnel_end;
529a4e82a81STony Nguyen 	}
530a4e82a81STony Nguyen 
531a4e82a81STony Nguyen 	bld = ice_pkg_buf_alloc(hw);
532a4e82a81STony Nguyen 	if (!bld) {
533d54699e2STony Nguyen 		status = -ENOMEM;
534a4e82a81STony Nguyen 		goto ice_destroy_tunnel_end;
535a4e82a81STony Nguyen 	}
536a4e82a81STony Nguyen 
537a4e82a81STony Nguyen 	/* allocate 2 sections, one for Rx parser, one for Tx parser */
538a4e82a81STony Nguyen 	if (ice_pkg_buf_reserve_section(bld, 2))
539a4e82a81STony Nguyen 		goto ice_destroy_tunnel_err;
540a4e82a81STony Nguyen 
541a4e82a81STony Nguyen 	sect_rx = ice_pkg_buf_alloc_section(bld, ICE_SID_RXPARSER_BOOST_TCAM,
542b20e6c17SJakub Kicinski 					    struct_size(sect_rx, tcam, 1));
543a4e82a81STony Nguyen 	if (!sect_rx)
544a4e82a81STony Nguyen 		goto ice_destroy_tunnel_err;
545a4e82a81STony Nguyen 	sect_rx->count = cpu_to_le16(1);
546a4e82a81STony Nguyen 
547a4e82a81STony Nguyen 	sect_tx = ice_pkg_buf_alloc_section(bld, ICE_SID_TXPARSER_BOOST_TCAM,
548b20e6c17SJakub Kicinski 					    struct_size(sect_tx, tcam, 1));
549a4e82a81STony Nguyen 	if (!sect_tx)
550a4e82a81STony Nguyen 		goto ice_destroy_tunnel_err;
551a4e82a81STony Nguyen 	sect_tx->count = cpu_to_le16(1);
552a4e82a81STony Nguyen 
553a4e82a81STony Nguyen 	/* copy original boost entry to update package buffer, one copy to Rx
554a4e82a81STony Nguyen 	 * section, another copy to the Tx section
555a4e82a81STony Nguyen 	 */
556b20e6c17SJakub Kicinski 	memcpy(sect_rx->tcam, hw->tnl.tbl[index].boost_entry,
557a4e82a81STony Nguyen 	       sizeof(*sect_rx->tcam));
558b20e6c17SJakub Kicinski 	memcpy(sect_tx->tcam, hw->tnl.tbl[index].boost_entry,
559a4e82a81STony Nguyen 	       sizeof(*sect_tx->tcam));
560a4e82a81STony Nguyen 
561a4e82a81STony Nguyen 	status = ice_update_pkg(hw, ice_pkg_buf(bld), 1);
562a4e82a81STony Nguyen 	if (!status)
563b20e6c17SJakub Kicinski 		hw->tnl.tbl[index].port = 0;
564a4e82a81STony Nguyen 
565a4e82a81STony Nguyen ice_destroy_tunnel_err:
566a4e82a81STony Nguyen 	ice_pkg_buf_free(hw, bld);
567a4e82a81STony Nguyen 
568a4e82a81STony Nguyen ice_destroy_tunnel_end:
569a4e82a81STony Nguyen 	mutex_unlock(&hw->tnl_lock);
570a4e82a81STony Nguyen 
571a4e82a81STony Nguyen 	return status;
572a4e82a81STony Nguyen }
573a4e82a81STony Nguyen 
ice_udp_tunnel_set_port(struct net_device * netdev,unsigned int table,unsigned int idx,struct udp_tunnel_info * ti)574b20e6c17SJakub Kicinski int ice_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
575b20e6c17SJakub Kicinski 			    unsigned int idx, struct udp_tunnel_info *ti)
576b20e6c17SJakub Kicinski {
577b20e6c17SJakub Kicinski 	struct ice_netdev_priv *np = netdev_priv(netdev);
578b20e6c17SJakub Kicinski 	struct ice_vsi *vsi = np->vsi;
579b20e6c17SJakub Kicinski 	struct ice_pf *pf = vsi->back;
580b20e6c17SJakub Kicinski 	enum ice_tunnel_type tnl_type;
5815e24d598STony Nguyen 	int status;
582b20e6c17SJakub Kicinski 	u16 index;
583b20e6c17SJakub Kicinski 
584b20e6c17SJakub Kicinski 	tnl_type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? TNL_VXLAN : TNL_GENEVE;
585e4c2efa1SMichal Swiatkowski 	index = ice_tunnel_idx_to_entry(&pf->hw, tnl_type, idx);
586b20e6c17SJakub Kicinski 
587b20e6c17SJakub Kicinski 	status = ice_create_tunnel(&pf->hw, index, tnl_type, ntohs(ti->port));
588b20e6c17SJakub Kicinski 	if (status) {
5895f87ec48STony Nguyen 		netdev_err(netdev, "Error adding UDP tunnel - %d\n",
5905f87ec48STony Nguyen 			   status);
591b20e6c17SJakub Kicinski 		return -EIO;
592b20e6c17SJakub Kicinski 	}
593b20e6c17SJakub Kicinski 
594b20e6c17SJakub Kicinski 	udp_tunnel_nic_set_port_priv(netdev, table, idx, index);
595b20e6c17SJakub Kicinski 	return 0;
596b20e6c17SJakub Kicinski }
597b20e6c17SJakub Kicinski 
ice_udp_tunnel_unset_port(struct net_device * netdev,unsigned int table,unsigned int idx,struct udp_tunnel_info * ti)598b20e6c17SJakub Kicinski int ice_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
599b20e6c17SJakub Kicinski 			      unsigned int idx, struct udp_tunnel_info *ti)
600b20e6c17SJakub Kicinski {
601b20e6c17SJakub Kicinski 	struct ice_netdev_priv *np = netdev_priv(netdev);
602b20e6c17SJakub Kicinski 	struct ice_vsi *vsi = np->vsi;
603b20e6c17SJakub Kicinski 	struct ice_pf *pf = vsi->back;
604b20e6c17SJakub Kicinski 	enum ice_tunnel_type tnl_type;
6055e24d598STony Nguyen 	int status;
606b20e6c17SJakub Kicinski 
607b20e6c17SJakub Kicinski 	tnl_type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? TNL_VXLAN : TNL_GENEVE;
608b20e6c17SJakub Kicinski 
609b20e6c17SJakub Kicinski 	status = ice_destroy_tunnel(&pf->hw, ti->hw_priv, tnl_type,
610b20e6c17SJakub Kicinski 				    ntohs(ti->port));
611b20e6c17SJakub Kicinski 	if (status) {
6125f87ec48STony Nguyen 		netdev_err(netdev, "Error removing UDP tunnel - %d\n",
6135f87ec48STony Nguyen 			   status);
614b20e6c17SJakub Kicinski 		return -EIO;
615b20e6c17SJakub Kicinski 	}
616b20e6c17SJakub Kicinski 
617b20e6c17SJakub Kicinski 	return 0;
618b20e6c17SJakub Kicinski }
619b20e6c17SJakub Kicinski 
620fd2a6b71SDan Nowlin /**
621fd2a6b71SDan Nowlin  * ice_find_prot_off - find prot ID and offset pair, based on prof and FV index
622fd2a6b71SDan Nowlin  * @hw: pointer to the hardware structure
623fd2a6b71SDan Nowlin  * @blk: hardware block
624fd2a6b71SDan Nowlin  * @prof: profile ID
625fd2a6b71SDan Nowlin  * @fv_idx: field vector word index
626fd2a6b71SDan Nowlin  * @prot: variable to receive the protocol ID
627fd2a6b71SDan Nowlin  * @off: variable to receive the protocol offset
628fd2a6b71SDan Nowlin  */
6295e24d598STony Nguyen int
ice_find_prot_off(struct ice_hw * hw,enum ice_block blk,u8 prof,u16 fv_idx,u8 * prot,u16 * off)630fd2a6b71SDan Nowlin ice_find_prot_off(struct ice_hw *hw, enum ice_block blk, u8 prof, u16 fv_idx,
631fd2a6b71SDan Nowlin 		  u8 *prot, u16 *off)
632fd2a6b71SDan Nowlin {
633fd2a6b71SDan Nowlin 	struct ice_fv_word *fv_ext;
634fd2a6b71SDan Nowlin 
635fd2a6b71SDan Nowlin 	if (prof >= hw->blk[blk].es.count)
636d54699e2STony Nguyen 		return -EINVAL;
637fd2a6b71SDan Nowlin 
638fd2a6b71SDan Nowlin 	if (fv_idx >= hw->blk[blk].es.fvw)
639d54699e2STony Nguyen 		return -EINVAL;
640fd2a6b71SDan Nowlin 
641fd2a6b71SDan Nowlin 	fv_ext = hw->blk[blk].es.t + (prof * hw->blk[blk].es.fvw);
642fd2a6b71SDan Nowlin 
643fd2a6b71SDan Nowlin 	*prot = fv_ext[fv_idx].prot_id;
644fd2a6b71SDan Nowlin 	*off = fv_ext[fv_idx].off;
645fd2a6b71SDan Nowlin 
646fd2a6b71SDan Nowlin 	return 0;
647fd2a6b71SDan Nowlin }
648fd2a6b71SDan Nowlin 
64932d63fa1STony Nguyen /* PTG Management */
65032d63fa1STony Nguyen 
65132d63fa1STony Nguyen /**
65232d63fa1STony Nguyen  * ice_ptg_find_ptype - Search for packet type group using packet type (ptype)
65332d63fa1STony Nguyen  * @hw: pointer to the hardware structure
65432d63fa1STony Nguyen  * @blk: HW block
65532d63fa1STony Nguyen  * @ptype: the ptype to search for
65632d63fa1STony Nguyen  * @ptg: pointer to variable that receives the PTG
65732d63fa1STony Nguyen  *
65832d63fa1STony Nguyen  * This function will search the PTGs for a particular ptype, returning the
65932d63fa1STony Nguyen  * PTG ID that contains it through the PTG parameter, with the value of
66032d63fa1STony Nguyen  * ICE_DEFAULT_PTG (0) meaning it is part the default PTG.
66132d63fa1STony Nguyen  */
6625e24d598STony Nguyen static int
ice_ptg_find_ptype(struct ice_hw * hw,enum ice_block blk,u16 ptype,u8 * ptg)66332d63fa1STony Nguyen ice_ptg_find_ptype(struct ice_hw *hw, enum ice_block blk, u16 ptype, u8 *ptg)
66432d63fa1STony Nguyen {
66532d63fa1STony Nguyen 	if (ptype >= ICE_XLT1_CNT || !ptg)
666d54699e2STony Nguyen 		return -EINVAL;
66732d63fa1STony Nguyen 
66832d63fa1STony Nguyen 	*ptg = hw->blk[blk].xlt1.ptypes[ptype].ptg;
66932d63fa1STony Nguyen 	return 0;
67032d63fa1STony Nguyen }
67132d63fa1STony Nguyen 
67232d63fa1STony Nguyen /**
67332d63fa1STony Nguyen  * ice_ptg_alloc_val - Allocates a new packet type group ID by value
67432d63fa1STony Nguyen  * @hw: pointer to the hardware structure
67532d63fa1STony Nguyen  * @blk: HW block
67632d63fa1STony Nguyen  * @ptg: the PTG to allocate
67732d63fa1STony Nguyen  *
67832d63fa1STony Nguyen  * This function allocates a given packet type group ID specified by the PTG
67932d63fa1STony Nguyen  * parameter.
68032d63fa1STony Nguyen  */
ice_ptg_alloc_val(struct ice_hw * hw,enum ice_block blk,u8 ptg)68132d63fa1STony Nguyen static void ice_ptg_alloc_val(struct ice_hw *hw, enum ice_block blk, u8 ptg)
68232d63fa1STony Nguyen {
68332d63fa1STony Nguyen 	hw->blk[blk].xlt1.ptg_tbl[ptg].in_use = true;
68432d63fa1STony Nguyen }
68532d63fa1STony Nguyen 
68632d63fa1STony Nguyen /**
68732d63fa1STony Nguyen  * ice_ptg_remove_ptype - Removes ptype from a particular packet type group
68832d63fa1STony Nguyen  * @hw: pointer to the hardware structure
68932d63fa1STony Nguyen  * @blk: HW block
69032d63fa1STony Nguyen  * @ptype: the ptype to remove
69132d63fa1STony Nguyen  * @ptg: the PTG to remove the ptype from
69232d63fa1STony Nguyen  *
69332d63fa1STony Nguyen  * This function will remove the ptype from the specific PTG, and move it to
69432d63fa1STony Nguyen  * the default PTG (ICE_DEFAULT_PTG).
69532d63fa1STony Nguyen  */
6965e24d598STony Nguyen static int
ice_ptg_remove_ptype(struct ice_hw * hw,enum ice_block blk,u16 ptype,u8 ptg)69732d63fa1STony Nguyen ice_ptg_remove_ptype(struct ice_hw *hw, enum ice_block blk, u16 ptype, u8 ptg)
69832d63fa1STony Nguyen {
69932d63fa1STony Nguyen 	struct ice_ptg_ptype **ch;
70032d63fa1STony Nguyen 	struct ice_ptg_ptype *p;
70132d63fa1STony Nguyen 
70232d63fa1STony Nguyen 	if (ptype > ICE_XLT1_CNT - 1)
703d54699e2STony Nguyen 		return -EINVAL;
70432d63fa1STony Nguyen 
70532d63fa1STony Nguyen 	if (!hw->blk[blk].xlt1.ptg_tbl[ptg].in_use)
706d54699e2STony Nguyen 		return -ENOENT;
70732d63fa1STony Nguyen 
70832d63fa1STony Nguyen 	/* Should not happen if .in_use is set, bad config */
70932d63fa1STony Nguyen 	if (!hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype)
710d54699e2STony Nguyen 		return -EIO;
71132d63fa1STony Nguyen 
71232d63fa1STony Nguyen 	/* find the ptype within this PTG, and bypass the link over it */
71332d63fa1STony Nguyen 	p = hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype;
71432d63fa1STony Nguyen 	ch = &hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype;
71532d63fa1STony Nguyen 	while (p) {
71632d63fa1STony Nguyen 		if (ptype == (p - hw->blk[blk].xlt1.ptypes)) {
71732d63fa1STony Nguyen 			*ch = p->next_ptype;
71832d63fa1STony Nguyen 			break;
71932d63fa1STony Nguyen 		}
72032d63fa1STony Nguyen 
72132d63fa1STony Nguyen 		ch = &p->next_ptype;
72232d63fa1STony Nguyen 		p = p->next_ptype;
72332d63fa1STony Nguyen 	}
72432d63fa1STony Nguyen 
72532d63fa1STony Nguyen 	hw->blk[blk].xlt1.ptypes[ptype].ptg = ICE_DEFAULT_PTG;
72632d63fa1STony Nguyen 	hw->blk[blk].xlt1.ptypes[ptype].next_ptype = NULL;
72732d63fa1STony Nguyen 
72832d63fa1STony Nguyen 	return 0;
72932d63fa1STony Nguyen }
73032d63fa1STony Nguyen 
73132d63fa1STony Nguyen /**
73232d63fa1STony Nguyen  * ice_ptg_add_mv_ptype - Adds/moves ptype to a particular packet type group
73332d63fa1STony Nguyen  * @hw: pointer to the hardware structure
73432d63fa1STony Nguyen  * @blk: HW block
73532d63fa1STony Nguyen  * @ptype: the ptype to add or move
73632d63fa1STony Nguyen  * @ptg: the PTG to add or move the ptype to
73732d63fa1STony Nguyen  *
73832d63fa1STony Nguyen  * This function will either add or move a ptype to a particular PTG depending
73932d63fa1STony Nguyen  * on if the ptype is already part of another group. Note that using a
740173e468cSJilin Yuan  * destination PTG ID of ICE_DEFAULT_PTG (0) will move the ptype to the
74132d63fa1STony Nguyen  * default PTG.
74232d63fa1STony Nguyen  */
7435e24d598STony Nguyen static int
ice_ptg_add_mv_ptype(struct ice_hw * hw,enum ice_block blk,u16 ptype,u8 ptg)74432d63fa1STony Nguyen ice_ptg_add_mv_ptype(struct ice_hw *hw, enum ice_block blk, u16 ptype, u8 ptg)
74532d63fa1STony Nguyen {
74632d63fa1STony Nguyen 	u8 original_ptg;
7475518ac2aSTony Nguyen 	int status;
74832d63fa1STony Nguyen 
74932d63fa1STony Nguyen 	if (ptype > ICE_XLT1_CNT - 1)
750d54699e2STony Nguyen 		return -EINVAL;
75132d63fa1STony Nguyen 
75232d63fa1STony Nguyen 	if (!hw->blk[blk].xlt1.ptg_tbl[ptg].in_use && ptg != ICE_DEFAULT_PTG)
753d54699e2STony Nguyen 		return -ENOENT;
75432d63fa1STony Nguyen 
75532d63fa1STony Nguyen 	status = ice_ptg_find_ptype(hw, blk, ptype, &original_ptg);
75632d63fa1STony Nguyen 	if (status)
75732d63fa1STony Nguyen 		return status;
75832d63fa1STony Nguyen 
75932d63fa1STony Nguyen 	/* Is ptype already in the correct PTG? */
76032d63fa1STony Nguyen 	if (original_ptg == ptg)
76132d63fa1STony Nguyen 		return 0;
76232d63fa1STony Nguyen 
76332d63fa1STony Nguyen 	/* Remove from original PTG and move back to the default PTG */
76432d63fa1STony Nguyen 	if (original_ptg != ICE_DEFAULT_PTG)
76532d63fa1STony Nguyen 		ice_ptg_remove_ptype(hw, blk, ptype, original_ptg);
76632d63fa1STony Nguyen 
76732d63fa1STony Nguyen 	/* Moving to default PTG? Then we're done with this request */
76832d63fa1STony Nguyen 	if (ptg == ICE_DEFAULT_PTG)
76932d63fa1STony Nguyen 		return 0;
77032d63fa1STony Nguyen 
77132d63fa1STony Nguyen 	/* Add ptype to PTG at beginning of list */
77232d63fa1STony Nguyen 	hw->blk[blk].xlt1.ptypes[ptype].next_ptype =
77332d63fa1STony Nguyen 		hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype;
77432d63fa1STony Nguyen 	hw->blk[blk].xlt1.ptg_tbl[ptg].first_ptype =
77532d63fa1STony Nguyen 		&hw->blk[blk].xlt1.ptypes[ptype];
77632d63fa1STony Nguyen 
77732d63fa1STony Nguyen 	hw->blk[blk].xlt1.ptypes[ptype].ptg = ptg;
77832d63fa1STony Nguyen 	hw->blk[blk].xlt1.t[ptype] = ptg;
77932d63fa1STony Nguyen 
78032d63fa1STony Nguyen 	return 0;
78132d63fa1STony Nguyen }
78232d63fa1STony Nguyen 
78332d63fa1STony Nguyen /* Block / table size info */
78432d63fa1STony Nguyen struct ice_blk_size_details {
78532d63fa1STony Nguyen 	u16 xlt1;			/* # XLT1 entries */
78632d63fa1STony Nguyen 	u16 xlt2;			/* # XLT2 entries */
78732d63fa1STony Nguyen 	u16 prof_tcam;			/* # profile ID TCAM entries */
78832d63fa1STony Nguyen 	u16 prof_id;			/* # profile IDs */
78932d63fa1STony Nguyen 	u8 prof_cdid_bits;		/* # CDID one-hot bits used in key */
79032d63fa1STony Nguyen 	u16 prof_redir;			/* # profile redirection entries */
79132d63fa1STony Nguyen 	u16 es;				/* # extraction sequence entries */
79232d63fa1STony Nguyen 	u16 fvw;			/* # field vector words */
79332d63fa1STony Nguyen 	u8 overwrite;			/* overwrite existing entries allowed */
79432d63fa1STony Nguyen 	u8 reverse;			/* reverse FV order */
79532d63fa1STony Nguyen };
79632d63fa1STony Nguyen 
79732d63fa1STony Nguyen static const struct ice_blk_size_details blk_sizes[ICE_BLK_COUNT] = {
79832d63fa1STony Nguyen 	/**
79932d63fa1STony Nguyen 	 * Table Definitions
80032d63fa1STony Nguyen 	 * XLT1 - Number of entries in XLT1 table
80132d63fa1STony Nguyen 	 * XLT2 - Number of entries in XLT2 table
80232d63fa1STony Nguyen 	 * TCAM - Number of entries Profile ID TCAM table
80332d63fa1STony Nguyen 	 * CDID - Control Domain ID of the hardware block
80432d63fa1STony Nguyen 	 * PRED - Number of entries in the Profile Redirection Table
80532d63fa1STony Nguyen 	 * FV   - Number of entries in the Field Vector
80632d63fa1STony Nguyen 	 * FVW  - Width (in WORDs) of the Field Vector
80732d63fa1STony Nguyen 	 * OVR  - Overwrite existing table entries
80832d63fa1STony Nguyen 	 * REV  - Reverse FV
80932d63fa1STony Nguyen 	 */
81032d63fa1STony Nguyen 	/*          XLT1        , XLT2        ,TCAM, PID,CDID,PRED,   FV, FVW */
81132d63fa1STony Nguyen 	/*          Overwrite   , Reverse FV */
81232d63fa1STony Nguyen 	/* SW  */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 256,   0,  256, 256,  48,
81332d63fa1STony Nguyen 		    false, false },
81432d63fa1STony Nguyen 	/* ACL */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 128,   0,  128, 128,  32,
81532d63fa1STony Nguyen 		    false, false },
81632d63fa1STony Nguyen 	/* FD  */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 128,   0,  128, 128,  24,
81732d63fa1STony Nguyen 		    false, true  },
81832d63fa1STony Nguyen 	/* RSS */ { ICE_XLT1_CNT, ICE_XLT2_CNT, 512, 128,   0,  128, 128,  24,
81932d63fa1STony Nguyen 		    true,  true  },
82032d63fa1STony Nguyen 	/* PE  */ { ICE_XLT1_CNT, ICE_XLT2_CNT,  64,  32,   0,   32,  32,  24,
82132d63fa1STony Nguyen 		    false, false },
82232d63fa1STony Nguyen };
82332d63fa1STony Nguyen 
82432d63fa1STony Nguyen enum ice_sid_all {
82532d63fa1STony Nguyen 	ICE_SID_XLT1_OFF = 0,
82632d63fa1STony Nguyen 	ICE_SID_XLT2_OFF,
82732d63fa1STony Nguyen 	ICE_SID_PR_OFF,
82832d63fa1STony Nguyen 	ICE_SID_PR_REDIR_OFF,
82932d63fa1STony Nguyen 	ICE_SID_ES_OFF,
83032d63fa1STony Nguyen 	ICE_SID_OFF_COUNT,
83132d63fa1STony Nguyen };
83232d63fa1STony Nguyen 
833451f2c44STony Nguyen /* Characteristic handling */
834451f2c44STony Nguyen 
835451f2c44STony Nguyen /**
836451f2c44STony Nguyen  * ice_match_prop_lst - determine if properties of two lists match
837451f2c44STony Nguyen  * @list1: first properties list
838451f2c44STony Nguyen  * @list2: second properties list
839451f2c44STony Nguyen  *
840451f2c44STony Nguyen  * Count, cookies and the order must match in order to be considered equivalent.
841451f2c44STony Nguyen  */
842451f2c44STony Nguyen static bool
ice_match_prop_lst(struct list_head * list1,struct list_head * list2)843451f2c44STony Nguyen ice_match_prop_lst(struct list_head *list1, struct list_head *list2)
844451f2c44STony Nguyen {
845451f2c44STony Nguyen 	struct ice_vsig_prof *tmp1;
846451f2c44STony Nguyen 	struct ice_vsig_prof *tmp2;
847451f2c44STony Nguyen 	u16 chk_count = 0;
848451f2c44STony Nguyen 	u16 count = 0;
849451f2c44STony Nguyen 
850451f2c44STony Nguyen 	/* compare counts */
851451f2c44STony Nguyen 	list_for_each_entry(tmp1, list1, list)
852451f2c44STony Nguyen 		count++;
853451f2c44STony Nguyen 	list_for_each_entry(tmp2, list2, list)
854451f2c44STony Nguyen 		chk_count++;
855451f2c44STony Nguyen 	if (!count || count != chk_count)
856451f2c44STony Nguyen 		return false;
857451f2c44STony Nguyen 
858451f2c44STony Nguyen 	tmp1 = list_first_entry(list1, struct ice_vsig_prof, list);
859451f2c44STony Nguyen 	tmp2 = list_first_entry(list2, struct ice_vsig_prof, list);
860451f2c44STony Nguyen 
861451f2c44STony Nguyen 	/* profile cookies must compare, and in the exact same order to take
862451f2c44STony Nguyen 	 * into account priority
863451f2c44STony Nguyen 	 */
864451f2c44STony Nguyen 	while (count--) {
865451f2c44STony Nguyen 		if (tmp2->profile_cookie != tmp1->profile_cookie)
866451f2c44STony Nguyen 			return false;
867451f2c44STony Nguyen 
868451f2c44STony Nguyen 		tmp1 = list_next_entry(tmp1, list);
869451f2c44STony Nguyen 		tmp2 = list_next_entry(tmp2, list);
870451f2c44STony Nguyen 	}
871451f2c44STony Nguyen 
872451f2c44STony Nguyen 	return true;
873451f2c44STony Nguyen }
874451f2c44STony Nguyen 
87532d63fa1STony Nguyen /* VSIG Management */
87632d63fa1STony Nguyen 
87732d63fa1STony Nguyen /**
87832d63fa1STony Nguyen  * ice_vsig_find_vsi - find a VSIG that contains a specified VSI
87932d63fa1STony Nguyen  * @hw: pointer to the hardware structure
88032d63fa1STony Nguyen  * @blk: HW block
88132d63fa1STony Nguyen  * @vsi: VSI of interest
88232d63fa1STony Nguyen  * @vsig: pointer to receive the VSI group
88332d63fa1STony Nguyen  *
88432d63fa1STony Nguyen  * This function will lookup the VSI entry in the XLT2 list and return
88532d63fa1STony Nguyen  * the VSI group its associated with.
88632d63fa1STony Nguyen  */
8875e24d598STony Nguyen static int
ice_vsig_find_vsi(struct ice_hw * hw,enum ice_block blk,u16 vsi,u16 * vsig)88832d63fa1STony Nguyen ice_vsig_find_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 *vsig)
88932d63fa1STony Nguyen {
89032d63fa1STony Nguyen 	if (!vsig || vsi >= ICE_MAX_VSI)
891d54699e2STony Nguyen 		return -EINVAL;
89232d63fa1STony Nguyen 
89332d63fa1STony Nguyen 	/* As long as there's a default or valid VSIG associated with the input
89432d63fa1STony Nguyen 	 * VSI, the functions returns a success. Any handling of VSIG will be
89532d63fa1STony Nguyen 	 * done by the following add, update or remove functions.
89632d63fa1STony Nguyen 	 */
89732d63fa1STony Nguyen 	*vsig = hw->blk[blk].xlt2.vsis[vsi].vsig;
89832d63fa1STony Nguyen 
89932d63fa1STony Nguyen 	return 0;
90032d63fa1STony Nguyen }
90132d63fa1STony Nguyen 
90232d63fa1STony Nguyen /**
90332d63fa1STony Nguyen  * ice_vsig_alloc_val - allocate a new VSIG by value
90432d63fa1STony Nguyen  * @hw: pointer to the hardware structure
90532d63fa1STony Nguyen  * @blk: HW block
90632d63fa1STony Nguyen  * @vsig: the VSIG to allocate
90732d63fa1STony Nguyen  *
90832d63fa1STony Nguyen  * This function will allocate a given VSIG specified by the VSIG parameter.
90932d63fa1STony Nguyen  */
ice_vsig_alloc_val(struct ice_hw * hw,enum ice_block blk,u16 vsig)91032d63fa1STony Nguyen static u16 ice_vsig_alloc_val(struct ice_hw *hw, enum ice_block blk, u16 vsig)
91132d63fa1STony Nguyen {
91232d63fa1STony Nguyen 	u16 idx = vsig & ICE_VSIG_IDX_M;
91332d63fa1STony Nguyen 
91432d63fa1STony Nguyen 	if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use) {
91532d63fa1STony Nguyen 		INIT_LIST_HEAD(&hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst);
91632d63fa1STony Nguyen 		hw->blk[blk].xlt2.vsig_tbl[idx].in_use = true;
91732d63fa1STony Nguyen 	}
91832d63fa1STony Nguyen 
91932d63fa1STony Nguyen 	return ICE_VSIG_VALUE(idx, hw->pf_id);
92032d63fa1STony Nguyen }
92132d63fa1STony Nguyen 
92232d63fa1STony Nguyen /**
923451f2c44STony Nguyen  * ice_vsig_alloc - Finds a free entry and allocates a new VSIG
924451f2c44STony Nguyen  * @hw: pointer to the hardware structure
925451f2c44STony Nguyen  * @blk: HW block
926451f2c44STony Nguyen  *
927451f2c44STony Nguyen  * This function will iterate through the VSIG list and mark the first
928451f2c44STony Nguyen  * unused entry for the new VSIG entry as used and return that value.
929451f2c44STony Nguyen  */
ice_vsig_alloc(struct ice_hw * hw,enum ice_block blk)930451f2c44STony Nguyen static u16 ice_vsig_alloc(struct ice_hw *hw, enum ice_block blk)
931451f2c44STony Nguyen {
932451f2c44STony Nguyen 	u16 i;
933451f2c44STony Nguyen 
934451f2c44STony Nguyen 	for (i = 1; i < ICE_MAX_VSIGS; i++)
935451f2c44STony Nguyen 		if (!hw->blk[blk].xlt2.vsig_tbl[i].in_use)
936451f2c44STony Nguyen 			return ice_vsig_alloc_val(hw, blk, i);
937451f2c44STony Nguyen 
938451f2c44STony Nguyen 	return ICE_DEFAULT_VSIG;
939451f2c44STony Nguyen }
940451f2c44STony Nguyen 
941451f2c44STony Nguyen /**
942451f2c44STony Nguyen  * ice_find_dup_props_vsig - find VSI group with a specified set of properties
943451f2c44STony Nguyen  * @hw: pointer to the hardware structure
944451f2c44STony Nguyen  * @blk: HW block
945451f2c44STony Nguyen  * @chs: characteristic list
946451f2c44STony Nguyen  * @vsig: returns the VSIG with the matching profiles, if found
947451f2c44STony Nguyen  *
948451f2c44STony Nguyen  * Each VSIG is associated with a characteristic set; i.e. all VSIs under
949451f2c44STony Nguyen  * a group have the same characteristic set. To check if there exists a VSIG
950451f2c44STony Nguyen  * which has the same characteristics as the input characteristics; this
951451f2c44STony Nguyen  * function will iterate through the XLT2 list and return the VSIG that has a
952451f2c44STony Nguyen  * matching configuration. In order to make sure that priorities are accounted
953451f2c44STony Nguyen  * for, the list must match exactly, including the order in which the
954451f2c44STony Nguyen  * characteristics are listed.
955451f2c44STony Nguyen  */
9565e24d598STony Nguyen static int
ice_find_dup_props_vsig(struct ice_hw * hw,enum ice_block blk,struct list_head * chs,u16 * vsig)957451f2c44STony Nguyen ice_find_dup_props_vsig(struct ice_hw *hw, enum ice_block blk,
958451f2c44STony Nguyen 			struct list_head *chs, u16 *vsig)
959451f2c44STony Nguyen {
960451f2c44STony Nguyen 	struct ice_xlt2 *xlt2 = &hw->blk[blk].xlt2;
961451f2c44STony Nguyen 	u16 i;
962451f2c44STony Nguyen 
963451f2c44STony Nguyen 	for (i = 0; i < xlt2->count; i++)
964451f2c44STony Nguyen 		if (xlt2->vsig_tbl[i].in_use &&
965451f2c44STony Nguyen 		    ice_match_prop_lst(chs, &xlt2->vsig_tbl[i].prop_lst)) {
966451f2c44STony Nguyen 			*vsig = ICE_VSIG_VALUE(i, hw->pf_id);
967451f2c44STony Nguyen 			return 0;
968451f2c44STony Nguyen 		}
969451f2c44STony Nguyen 
970d54699e2STony Nguyen 	return -ENOENT;
971451f2c44STony Nguyen }
972451f2c44STony Nguyen 
973451f2c44STony Nguyen /**
974451f2c44STony Nguyen  * ice_vsig_free - free VSI group
975451f2c44STony Nguyen  * @hw: pointer to the hardware structure
976451f2c44STony Nguyen  * @blk: HW block
977451f2c44STony Nguyen  * @vsig: VSIG to remove
978451f2c44STony Nguyen  *
979451f2c44STony Nguyen  * The function will remove all VSIs associated with the input VSIG and move
980451f2c44STony Nguyen  * them to the DEFAULT_VSIG and mark the VSIG available.
981451f2c44STony Nguyen  */
ice_vsig_free(struct ice_hw * hw,enum ice_block blk,u16 vsig)9825518ac2aSTony Nguyen static int ice_vsig_free(struct ice_hw *hw, enum ice_block blk, u16 vsig)
983451f2c44STony Nguyen {
984451f2c44STony Nguyen 	struct ice_vsig_prof *dtmp, *del;
985451f2c44STony Nguyen 	struct ice_vsig_vsi *vsi_cur;
986451f2c44STony Nguyen 	u16 idx;
987451f2c44STony Nguyen 
988451f2c44STony Nguyen 	idx = vsig & ICE_VSIG_IDX_M;
989451f2c44STony Nguyen 	if (idx >= ICE_MAX_VSIGS)
990d54699e2STony Nguyen 		return -EINVAL;
991451f2c44STony Nguyen 
992451f2c44STony Nguyen 	if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use)
993d54699e2STony Nguyen 		return -ENOENT;
994451f2c44STony Nguyen 
995451f2c44STony Nguyen 	hw->blk[blk].xlt2.vsig_tbl[idx].in_use = false;
996451f2c44STony Nguyen 
997451f2c44STony Nguyen 	vsi_cur = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
998451f2c44STony Nguyen 	/* If the VSIG has at least 1 VSI then iterate through the
999451f2c44STony Nguyen 	 * list and remove the VSIs before deleting the group.
1000451f2c44STony Nguyen 	 */
1001451f2c44STony Nguyen 	if (vsi_cur) {
1002451f2c44STony Nguyen 		/* remove all vsis associated with this VSIG XLT2 entry */
1003451f2c44STony Nguyen 		do {
1004451f2c44STony Nguyen 			struct ice_vsig_vsi *tmp = vsi_cur->next_vsi;
1005451f2c44STony Nguyen 
1006451f2c44STony Nguyen 			vsi_cur->vsig = ICE_DEFAULT_VSIG;
1007451f2c44STony Nguyen 			vsi_cur->changed = 1;
1008451f2c44STony Nguyen 			vsi_cur->next_vsi = NULL;
1009451f2c44STony Nguyen 			vsi_cur = tmp;
1010451f2c44STony Nguyen 		} while (vsi_cur);
1011451f2c44STony Nguyen 
1012451f2c44STony Nguyen 		/* NULL terminate head of VSI list */
1013451f2c44STony Nguyen 		hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi = NULL;
1014451f2c44STony Nguyen 	}
1015451f2c44STony Nguyen 
1016451f2c44STony Nguyen 	/* free characteristic list */
1017451f2c44STony Nguyen 	list_for_each_entry_safe(del, dtmp,
1018451f2c44STony Nguyen 				 &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
1019451f2c44STony Nguyen 				 list) {
1020451f2c44STony Nguyen 		list_del(&del->list);
1021451f2c44STony Nguyen 		devm_kfree(ice_hw_to_dev(hw), del);
1022451f2c44STony Nguyen 	}
1023451f2c44STony Nguyen 
1024451f2c44STony Nguyen 	/* if VSIG characteristic list was cleared for reset
1025451f2c44STony Nguyen 	 * re-initialize the list head
1026451f2c44STony Nguyen 	 */
1027451f2c44STony Nguyen 	INIT_LIST_HEAD(&hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst);
1028451f2c44STony Nguyen 
1029451f2c44STony Nguyen 	return 0;
1030451f2c44STony Nguyen }
1031451f2c44STony Nguyen 
1032451f2c44STony Nguyen /**
103332d63fa1STony Nguyen  * ice_vsig_remove_vsi - remove VSI from VSIG
103432d63fa1STony Nguyen  * @hw: pointer to the hardware structure
103532d63fa1STony Nguyen  * @blk: HW block
103632d63fa1STony Nguyen  * @vsi: VSI to remove
103732d63fa1STony Nguyen  * @vsig: VSI group to remove from
103832d63fa1STony Nguyen  *
103932d63fa1STony Nguyen  * The function will remove the input VSI from its VSI group and move it
104032d63fa1STony Nguyen  * to the DEFAULT_VSIG.
104132d63fa1STony Nguyen  */
10425e24d598STony Nguyen static int
ice_vsig_remove_vsi(struct ice_hw * hw,enum ice_block blk,u16 vsi,u16 vsig)104332d63fa1STony Nguyen ice_vsig_remove_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 vsig)
104432d63fa1STony Nguyen {
104532d63fa1STony Nguyen 	struct ice_vsig_vsi **vsi_head, *vsi_cur, *vsi_tgt;
104632d63fa1STony Nguyen 	u16 idx;
104732d63fa1STony Nguyen 
104832d63fa1STony Nguyen 	idx = vsig & ICE_VSIG_IDX_M;
104932d63fa1STony Nguyen 
105032d63fa1STony Nguyen 	if (vsi >= ICE_MAX_VSI || idx >= ICE_MAX_VSIGS)
1051d54699e2STony Nguyen 		return -EINVAL;
105232d63fa1STony Nguyen 
105332d63fa1STony Nguyen 	if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use)
1054d54699e2STony Nguyen 		return -ENOENT;
105532d63fa1STony Nguyen 
105632d63fa1STony Nguyen 	/* entry already in default VSIG, don't have to remove */
105732d63fa1STony Nguyen 	if (idx == ICE_DEFAULT_VSIG)
105832d63fa1STony Nguyen 		return 0;
105932d63fa1STony Nguyen 
106032d63fa1STony Nguyen 	vsi_head = &hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
106132d63fa1STony Nguyen 	if (!(*vsi_head))
1062d54699e2STony Nguyen 		return -EIO;
106332d63fa1STony Nguyen 
106432d63fa1STony Nguyen 	vsi_tgt = &hw->blk[blk].xlt2.vsis[vsi];
106532d63fa1STony Nguyen 	vsi_cur = (*vsi_head);
106632d63fa1STony Nguyen 
106732d63fa1STony Nguyen 	/* iterate the VSI list, skip over the entry to be removed */
106832d63fa1STony Nguyen 	while (vsi_cur) {
106932d63fa1STony Nguyen 		if (vsi_tgt == vsi_cur) {
107032d63fa1STony Nguyen 			(*vsi_head) = vsi_cur->next_vsi;
107132d63fa1STony Nguyen 			break;
107232d63fa1STony Nguyen 		}
107332d63fa1STony Nguyen 		vsi_head = &vsi_cur->next_vsi;
107432d63fa1STony Nguyen 		vsi_cur = vsi_cur->next_vsi;
107532d63fa1STony Nguyen 	}
107632d63fa1STony Nguyen 
107732d63fa1STony Nguyen 	/* verify if VSI was removed from group list */
107832d63fa1STony Nguyen 	if (!vsi_cur)
1079d54699e2STony Nguyen 		return -ENOENT;
108032d63fa1STony Nguyen 
108132d63fa1STony Nguyen 	vsi_cur->vsig = ICE_DEFAULT_VSIG;
108232d63fa1STony Nguyen 	vsi_cur->changed = 1;
108332d63fa1STony Nguyen 	vsi_cur->next_vsi = NULL;
108432d63fa1STony Nguyen 
108532d63fa1STony Nguyen 	return 0;
108632d63fa1STony Nguyen }
108732d63fa1STony Nguyen 
108832d63fa1STony Nguyen /**
108932d63fa1STony Nguyen  * ice_vsig_add_mv_vsi - add or move a VSI to a VSI group
109032d63fa1STony Nguyen  * @hw: pointer to the hardware structure
109132d63fa1STony Nguyen  * @blk: HW block
109232d63fa1STony Nguyen  * @vsi: VSI to move
109332d63fa1STony Nguyen  * @vsig: destination VSI group
109432d63fa1STony Nguyen  *
109532d63fa1STony Nguyen  * This function will move or add the input VSI to the target VSIG.
109632d63fa1STony Nguyen  * The function will find the original VSIG the VSI belongs to and
109732d63fa1STony Nguyen  * move the entry to the DEFAULT_VSIG, update the original VSIG and
109832d63fa1STony Nguyen  * then move entry to the new VSIG.
109932d63fa1STony Nguyen  */
11005e24d598STony Nguyen static int
ice_vsig_add_mv_vsi(struct ice_hw * hw,enum ice_block blk,u16 vsi,u16 vsig)110132d63fa1STony Nguyen ice_vsig_add_mv_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 vsig)
110232d63fa1STony Nguyen {
110332d63fa1STony Nguyen 	struct ice_vsig_vsi *tmp;
110432d63fa1STony Nguyen 	u16 orig_vsig, idx;
11055518ac2aSTony Nguyen 	int status;
110632d63fa1STony Nguyen 
110732d63fa1STony Nguyen 	idx = vsig & ICE_VSIG_IDX_M;
110832d63fa1STony Nguyen 
110932d63fa1STony Nguyen 	if (vsi >= ICE_MAX_VSI || idx >= ICE_MAX_VSIGS)
1110d54699e2STony Nguyen 		return -EINVAL;
111132d63fa1STony Nguyen 
111232d63fa1STony Nguyen 	/* if VSIG not in use and VSIG is not default type this VSIG
111332d63fa1STony Nguyen 	 * doesn't exist.
111432d63fa1STony Nguyen 	 */
111532d63fa1STony Nguyen 	if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use &&
111632d63fa1STony Nguyen 	    vsig != ICE_DEFAULT_VSIG)
1117d54699e2STony Nguyen 		return -ENOENT;
111832d63fa1STony Nguyen 
111932d63fa1STony Nguyen 	status = ice_vsig_find_vsi(hw, blk, vsi, &orig_vsig);
112032d63fa1STony Nguyen 	if (status)
112132d63fa1STony Nguyen 		return status;
112232d63fa1STony Nguyen 
112332d63fa1STony Nguyen 	/* no update required if vsigs match */
112432d63fa1STony Nguyen 	if (orig_vsig == vsig)
112532d63fa1STony Nguyen 		return 0;
112632d63fa1STony Nguyen 
112732d63fa1STony Nguyen 	if (orig_vsig != ICE_DEFAULT_VSIG) {
112832d63fa1STony Nguyen 		/* remove entry from orig_vsig and add to default VSIG */
112932d63fa1STony Nguyen 		status = ice_vsig_remove_vsi(hw, blk, vsi, orig_vsig);
113032d63fa1STony Nguyen 		if (status)
113132d63fa1STony Nguyen 			return status;
113232d63fa1STony Nguyen 	}
113332d63fa1STony Nguyen 
113432d63fa1STony Nguyen 	if (idx == ICE_DEFAULT_VSIG)
113532d63fa1STony Nguyen 		return 0;
113632d63fa1STony Nguyen 
113732d63fa1STony Nguyen 	/* Create VSI entry and add VSIG and prop_mask values */
113832d63fa1STony Nguyen 	hw->blk[blk].xlt2.vsis[vsi].vsig = vsig;
113932d63fa1STony Nguyen 	hw->blk[blk].xlt2.vsis[vsi].changed = 1;
114032d63fa1STony Nguyen 
114132d63fa1STony Nguyen 	/* Add new entry to the head of the VSIG list */
114232d63fa1STony Nguyen 	tmp = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
114332d63fa1STony Nguyen 	hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi =
114432d63fa1STony Nguyen 		&hw->blk[blk].xlt2.vsis[vsi];
114532d63fa1STony Nguyen 	hw->blk[blk].xlt2.vsis[vsi].next_vsi = tmp;
114632d63fa1STony Nguyen 	hw->blk[blk].xlt2.t[vsi] = vsig;
114732d63fa1STony Nguyen 
114832d63fa1STony Nguyen 	return 0;
114932d63fa1STony Nguyen }
115032d63fa1STony Nguyen 
115131ad4e4eSTony Nguyen /**
1152b199dddbSQi Zhang  * ice_prof_has_mask_idx - determine if profile index masking is identical
1153b199dddbSQi Zhang  * @hw: pointer to the hardware structure
1154b199dddbSQi Zhang  * @blk: HW block
1155b199dddbSQi Zhang  * @prof: profile to check
1156b199dddbSQi Zhang  * @idx: profile index to check
1157b199dddbSQi Zhang  * @mask: mask to match
1158b199dddbSQi Zhang  */
1159b199dddbSQi Zhang static bool
ice_prof_has_mask_idx(struct ice_hw * hw,enum ice_block blk,u8 prof,u16 idx,u16 mask)1160b199dddbSQi Zhang ice_prof_has_mask_idx(struct ice_hw *hw, enum ice_block blk, u8 prof, u16 idx,
1161b199dddbSQi Zhang 		      u16 mask)
1162b199dddbSQi Zhang {
1163b199dddbSQi Zhang 	bool expect_no_mask = false;
1164b199dddbSQi Zhang 	bool found = false;
1165b199dddbSQi Zhang 	bool match = false;
1166b199dddbSQi Zhang 	u16 i;
1167b199dddbSQi Zhang 
1168b199dddbSQi Zhang 	/* If mask is 0x0000 or 0xffff, then there is no masking */
1169b199dddbSQi Zhang 	if (mask == 0 || mask == 0xffff)
1170b199dddbSQi Zhang 		expect_no_mask = true;
1171b199dddbSQi Zhang 
1172b199dddbSQi Zhang 	/* Scan the enabled masks on this profile, for the specified idx */
1173b199dddbSQi Zhang 	for (i = hw->blk[blk].masks.first; i < hw->blk[blk].masks.first +
1174b199dddbSQi Zhang 	     hw->blk[blk].masks.count; i++)
1175b199dddbSQi Zhang 		if (hw->blk[blk].es.mask_ena[prof] & BIT(i))
1176b199dddbSQi Zhang 			if (hw->blk[blk].masks.masks[i].in_use &&
1177b199dddbSQi Zhang 			    hw->blk[blk].masks.masks[i].idx == idx) {
1178b199dddbSQi Zhang 				found = true;
1179b199dddbSQi Zhang 				if (hw->blk[blk].masks.masks[i].mask == mask)
1180b199dddbSQi Zhang 					match = true;
1181b199dddbSQi Zhang 				break;
1182b199dddbSQi Zhang 			}
1183b199dddbSQi Zhang 
1184b199dddbSQi Zhang 	if (expect_no_mask) {
1185b199dddbSQi Zhang 		if (found)
1186b199dddbSQi Zhang 			return false;
1187b199dddbSQi Zhang 	} else {
1188b199dddbSQi Zhang 		if (!match)
1189b199dddbSQi Zhang 			return false;
1190b199dddbSQi Zhang 	}
1191b199dddbSQi Zhang 
1192b199dddbSQi Zhang 	return true;
1193b199dddbSQi Zhang }
1194b199dddbSQi Zhang 
1195b199dddbSQi Zhang /**
1196b199dddbSQi Zhang  * ice_prof_has_mask - determine if profile masking is identical
1197b199dddbSQi Zhang  * @hw: pointer to the hardware structure
1198b199dddbSQi Zhang  * @blk: HW block
1199b199dddbSQi Zhang  * @prof: profile to check
1200b199dddbSQi Zhang  * @masks: masks to match
1201b199dddbSQi Zhang  */
1202b199dddbSQi Zhang static bool
ice_prof_has_mask(struct ice_hw * hw,enum ice_block blk,u8 prof,u16 * masks)1203b199dddbSQi Zhang ice_prof_has_mask(struct ice_hw *hw, enum ice_block blk, u8 prof, u16 *masks)
1204b199dddbSQi Zhang {
1205b199dddbSQi Zhang 	u16 i;
1206b199dddbSQi Zhang 
1207b199dddbSQi Zhang 	/* es->mask_ena[prof] will have the mask */
1208b199dddbSQi Zhang 	for (i = 0; i < hw->blk[blk].es.fvw; i++)
1209b199dddbSQi Zhang 		if (!ice_prof_has_mask_idx(hw, blk, prof, i, masks[i]))
1210b199dddbSQi Zhang 			return false;
1211b199dddbSQi Zhang 
1212b199dddbSQi Zhang 	return true;
1213b199dddbSQi Zhang }
1214b199dddbSQi Zhang 
1215b199dddbSQi Zhang /**
1216b199dddbSQi Zhang  * ice_find_prof_id_with_mask - find profile ID for a given field vector
121731ad4e4eSTony Nguyen  * @hw: pointer to the hardware structure
121831ad4e4eSTony Nguyen  * @blk: HW block
121931ad4e4eSTony Nguyen  * @fv: field vector to search for
1220b199dddbSQi Zhang  * @masks: masks for FV
122131ad4e4eSTony Nguyen  * @prof_id: receives the profile ID
122231ad4e4eSTony Nguyen  */
12235e24d598STony Nguyen static int
ice_find_prof_id_with_mask(struct ice_hw * hw,enum ice_block blk,struct ice_fv_word * fv,u16 * masks,u8 * prof_id)1224b199dddbSQi Zhang ice_find_prof_id_with_mask(struct ice_hw *hw, enum ice_block blk,
1225b199dddbSQi Zhang 			   struct ice_fv_word *fv, u16 *masks, u8 *prof_id)
122631ad4e4eSTony Nguyen {
122731ad4e4eSTony Nguyen 	struct ice_es *es = &hw->blk[blk].es;
122888865fc4SKarol Kolacinski 	u8 i;
122931ad4e4eSTony Nguyen 
1230cac2a27cSHenry Tieman 	/* For FD, we don't want to re-use a existed profile with the same
1231cac2a27cSHenry Tieman 	 * field vector and mask. This will cause rule interference.
1232cac2a27cSHenry Tieman 	 */
1233cac2a27cSHenry Tieman 	if (blk == ICE_BLK_FD)
1234d54699e2STony Nguyen 		return -ENOENT;
1235cac2a27cSHenry Tieman 
123688865fc4SKarol Kolacinski 	for (i = 0; i < (u8)es->count; i++) {
1237b199dddbSQi Zhang 		u16 off = i * es->fvw;
123831ad4e4eSTony Nguyen 
123931ad4e4eSTony Nguyen 		if (memcmp(&es->t[off], fv, es->fvw * sizeof(*fv)))
124031ad4e4eSTony Nguyen 			continue;
124131ad4e4eSTony Nguyen 
1242b199dddbSQi Zhang 		/* check if masks settings are the same for this profile */
1243b199dddbSQi Zhang 		if (masks && !ice_prof_has_mask(hw, blk, i, masks))
1244b199dddbSQi Zhang 			continue;
1245b199dddbSQi Zhang 
124631ad4e4eSTony Nguyen 		*prof_id = i;
124731ad4e4eSTony Nguyen 		return 0;
124831ad4e4eSTony Nguyen 	}
124931ad4e4eSTony Nguyen 
1250d54699e2STony Nguyen 	return -ENOENT;
125131ad4e4eSTony Nguyen }
125231ad4e4eSTony Nguyen 
125331ad4e4eSTony Nguyen /**
125431ad4e4eSTony Nguyen  * ice_prof_id_rsrc_type - get profile ID resource type for a block type
125531ad4e4eSTony Nguyen  * @blk: the block type
125631ad4e4eSTony Nguyen  * @rsrc_type: pointer to variable to receive the resource type
125731ad4e4eSTony Nguyen  */
ice_prof_id_rsrc_type(enum ice_block blk,u16 * rsrc_type)125831ad4e4eSTony Nguyen static bool ice_prof_id_rsrc_type(enum ice_block blk, u16 *rsrc_type)
125931ad4e4eSTony Nguyen {
126031ad4e4eSTony Nguyen 	switch (blk) {
1261148beb61SHenry Tieman 	case ICE_BLK_FD:
1262148beb61SHenry Tieman 		*rsrc_type = ICE_AQC_RES_TYPE_FD_PROF_BLDR_PROFID;
1263148beb61SHenry Tieman 		break;
126431ad4e4eSTony Nguyen 	case ICE_BLK_RSS:
126531ad4e4eSTony Nguyen 		*rsrc_type = ICE_AQC_RES_TYPE_HASH_PROF_BLDR_PROFID;
126631ad4e4eSTony Nguyen 		break;
126731ad4e4eSTony Nguyen 	default:
126831ad4e4eSTony Nguyen 		return false;
126931ad4e4eSTony Nguyen 	}
127031ad4e4eSTony Nguyen 	return true;
127131ad4e4eSTony Nguyen }
127231ad4e4eSTony Nguyen 
127331ad4e4eSTony Nguyen /**
1274451f2c44STony Nguyen  * ice_tcam_ent_rsrc_type - get TCAM entry resource type for a block type
1275451f2c44STony Nguyen  * @blk: the block type
1276451f2c44STony Nguyen  * @rsrc_type: pointer to variable to receive the resource type
1277451f2c44STony Nguyen  */
ice_tcam_ent_rsrc_type(enum ice_block blk,u16 * rsrc_type)1278451f2c44STony Nguyen static bool ice_tcam_ent_rsrc_type(enum ice_block blk, u16 *rsrc_type)
1279451f2c44STony Nguyen {
1280451f2c44STony Nguyen 	switch (blk) {
1281148beb61SHenry Tieman 	case ICE_BLK_FD:
1282148beb61SHenry Tieman 		*rsrc_type = ICE_AQC_RES_TYPE_FD_PROF_BLDR_TCAM;
1283148beb61SHenry Tieman 		break;
1284451f2c44STony Nguyen 	case ICE_BLK_RSS:
1285451f2c44STony Nguyen 		*rsrc_type = ICE_AQC_RES_TYPE_HASH_PROF_BLDR_TCAM;
1286451f2c44STony Nguyen 		break;
1287451f2c44STony Nguyen 	default:
1288451f2c44STony Nguyen 		return false;
1289451f2c44STony Nguyen 	}
1290451f2c44STony Nguyen 	return true;
1291451f2c44STony Nguyen }
1292451f2c44STony Nguyen 
1293451f2c44STony Nguyen /**
1294451f2c44STony Nguyen  * ice_alloc_tcam_ent - allocate hardware TCAM entry
1295451f2c44STony Nguyen  * @hw: pointer to the HW struct
1296451f2c44STony Nguyen  * @blk: the block to allocate the TCAM for
1297cbad5db8SQi Zhang  * @btm: true to allocate from bottom of table, false to allocate from top
1298451f2c44STony Nguyen  * @tcam_idx: pointer to variable to receive the TCAM entry
1299451f2c44STony Nguyen  *
1300451f2c44STony Nguyen  * This function allocates a new entry in a Profile ID TCAM for a specific
1301451f2c44STony Nguyen  * block.
1302451f2c44STony Nguyen  */
13035e24d598STony Nguyen static int
ice_alloc_tcam_ent(struct ice_hw * hw,enum ice_block blk,bool btm,u16 * tcam_idx)1304cbad5db8SQi Zhang ice_alloc_tcam_ent(struct ice_hw *hw, enum ice_block blk, bool btm,
1305cbad5db8SQi Zhang 		   u16 *tcam_idx)
1306451f2c44STony Nguyen {
1307451f2c44STony Nguyen 	u16 res_type;
1308451f2c44STony Nguyen 
1309451f2c44STony Nguyen 	if (!ice_tcam_ent_rsrc_type(blk, &res_type))
1310d54699e2STony Nguyen 		return -EINVAL;
1311451f2c44STony Nguyen 
1312cbad5db8SQi Zhang 	return ice_alloc_hw_res(hw, res_type, 1, btm, tcam_idx);
1313451f2c44STony Nguyen }
1314451f2c44STony Nguyen 
1315451f2c44STony Nguyen /**
1316451f2c44STony Nguyen  * ice_free_tcam_ent - free hardware TCAM entry
1317451f2c44STony Nguyen  * @hw: pointer to the HW struct
1318451f2c44STony Nguyen  * @blk: the block from which to free the TCAM entry
1319451f2c44STony Nguyen  * @tcam_idx: the TCAM entry to free
1320451f2c44STony Nguyen  *
1321451f2c44STony Nguyen  * This function frees an entry in a Profile ID TCAM for a specific block.
1322451f2c44STony Nguyen  */
13235e24d598STony Nguyen static int
ice_free_tcam_ent(struct ice_hw * hw,enum ice_block blk,u16 tcam_idx)1324451f2c44STony Nguyen ice_free_tcam_ent(struct ice_hw *hw, enum ice_block blk, u16 tcam_idx)
1325451f2c44STony Nguyen {
1326451f2c44STony Nguyen 	u16 res_type;
1327451f2c44STony Nguyen 
1328451f2c44STony Nguyen 	if (!ice_tcam_ent_rsrc_type(blk, &res_type))
1329d54699e2STony Nguyen 		return -EINVAL;
1330451f2c44STony Nguyen 
1331451f2c44STony Nguyen 	return ice_free_hw_res(hw, res_type, 1, &tcam_idx);
1332451f2c44STony Nguyen }
1333451f2c44STony Nguyen 
1334451f2c44STony Nguyen /**
133531ad4e4eSTony Nguyen  * ice_alloc_prof_id - allocate profile ID
133631ad4e4eSTony Nguyen  * @hw: pointer to the HW struct
133731ad4e4eSTony Nguyen  * @blk: the block to allocate the profile ID for
133831ad4e4eSTony Nguyen  * @prof_id: pointer to variable to receive the profile ID
133931ad4e4eSTony Nguyen  *
134031ad4e4eSTony Nguyen  * This function allocates a new profile ID, which also corresponds to a Field
134131ad4e4eSTony Nguyen  * Vector (Extraction Sequence) entry.
134231ad4e4eSTony Nguyen  */
ice_alloc_prof_id(struct ice_hw * hw,enum ice_block blk,u8 * prof_id)13435518ac2aSTony Nguyen static int ice_alloc_prof_id(struct ice_hw *hw, enum ice_block blk, u8 *prof_id)
134431ad4e4eSTony Nguyen {
134531ad4e4eSTony Nguyen 	u16 res_type;
134631ad4e4eSTony Nguyen 	u16 get_prof;
13475518ac2aSTony Nguyen 	int status;
134831ad4e4eSTony Nguyen 
134931ad4e4eSTony Nguyen 	if (!ice_prof_id_rsrc_type(blk, &res_type))
1350d54699e2STony Nguyen 		return -EINVAL;
135131ad4e4eSTony Nguyen 
135231ad4e4eSTony Nguyen 	status = ice_alloc_hw_res(hw, res_type, 1, false, &get_prof);
135331ad4e4eSTony Nguyen 	if (!status)
135431ad4e4eSTony Nguyen 		*prof_id = (u8)get_prof;
135531ad4e4eSTony Nguyen 
135631ad4e4eSTony Nguyen 	return status;
135731ad4e4eSTony Nguyen }
135831ad4e4eSTony Nguyen 
135931ad4e4eSTony Nguyen /**
13602c61054cSTony Nguyen  * ice_free_prof_id - free profile ID
13612c61054cSTony Nguyen  * @hw: pointer to the HW struct
13622c61054cSTony Nguyen  * @blk: the block from which to free the profile ID
13632c61054cSTony Nguyen  * @prof_id: the profile ID to free
13642c61054cSTony Nguyen  *
13652c61054cSTony Nguyen  * This function frees a profile ID, which also corresponds to a Field Vector.
13662c61054cSTony Nguyen  */
ice_free_prof_id(struct ice_hw * hw,enum ice_block blk,u8 prof_id)13675518ac2aSTony Nguyen static int ice_free_prof_id(struct ice_hw *hw, enum ice_block blk, u8 prof_id)
13682c61054cSTony Nguyen {
13692c61054cSTony Nguyen 	u16 tmp_prof_id = (u16)prof_id;
13702c61054cSTony Nguyen 	u16 res_type;
13712c61054cSTony Nguyen 
13722c61054cSTony Nguyen 	if (!ice_prof_id_rsrc_type(blk, &res_type))
1373d54699e2STony Nguyen 		return -EINVAL;
13742c61054cSTony Nguyen 
13752c61054cSTony Nguyen 	return ice_free_hw_res(hw, res_type, 1, &tmp_prof_id);
13762c61054cSTony Nguyen }
13772c61054cSTony Nguyen 
13782c61054cSTony Nguyen /**
137931ad4e4eSTony Nguyen  * ice_prof_inc_ref - increment reference count for profile
138031ad4e4eSTony Nguyen  * @hw: pointer to the HW struct
138131ad4e4eSTony Nguyen  * @blk: the block from which to free the profile ID
138231ad4e4eSTony Nguyen  * @prof_id: the profile ID for which to increment the reference count
138331ad4e4eSTony Nguyen  */
ice_prof_inc_ref(struct ice_hw * hw,enum ice_block blk,u8 prof_id)13845518ac2aSTony Nguyen static int ice_prof_inc_ref(struct ice_hw *hw, enum ice_block blk, u8 prof_id)
138531ad4e4eSTony Nguyen {
138631ad4e4eSTony Nguyen 	if (prof_id > hw->blk[blk].es.count)
1387d54699e2STony Nguyen 		return -EINVAL;
138831ad4e4eSTony Nguyen 
138931ad4e4eSTony Nguyen 	hw->blk[blk].es.ref_count[prof_id]++;
139031ad4e4eSTony Nguyen 
139131ad4e4eSTony Nguyen 	return 0;
139231ad4e4eSTony Nguyen }
139331ad4e4eSTony Nguyen 
139431ad4e4eSTony Nguyen /**
1395b199dddbSQi Zhang  * ice_write_prof_mask_reg - write profile mask register
1396b199dddbSQi Zhang  * @hw: pointer to the HW struct
1397b199dddbSQi Zhang  * @blk: hardware block
1398b199dddbSQi Zhang  * @mask_idx: mask index
1399b199dddbSQi Zhang  * @idx: index of the FV which will use the mask
1400b199dddbSQi Zhang  * @mask: the 16-bit mask
1401b199dddbSQi Zhang  */
1402b199dddbSQi Zhang static void
ice_write_prof_mask_reg(struct ice_hw * hw,enum ice_block blk,u16 mask_idx,u16 idx,u16 mask)1403b199dddbSQi Zhang ice_write_prof_mask_reg(struct ice_hw *hw, enum ice_block blk, u16 mask_idx,
1404b199dddbSQi Zhang 			u16 idx, u16 mask)
1405b199dddbSQi Zhang {
1406b199dddbSQi Zhang 	u32 offset;
1407b199dddbSQi Zhang 	u32 val;
1408b199dddbSQi Zhang 
1409b199dddbSQi Zhang 	switch (blk) {
1410b199dddbSQi Zhang 	case ICE_BLK_RSS:
1411b199dddbSQi Zhang 		offset = GLQF_HMASK(mask_idx);
1412b199dddbSQi Zhang 		val = (idx << GLQF_HMASK_MSK_INDEX_S) & GLQF_HMASK_MSK_INDEX_M;
1413b199dddbSQi Zhang 		val |= (mask << GLQF_HMASK_MASK_S) & GLQF_HMASK_MASK_M;
1414b199dddbSQi Zhang 		break;
1415b199dddbSQi Zhang 	case ICE_BLK_FD:
1416b199dddbSQi Zhang 		offset = GLQF_FDMASK(mask_idx);
1417b199dddbSQi Zhang 		val = (idx << GLQF_FDMASK_MSK_INDEX_S) & GLQF_FDMASK_MSK_INDEX_M;
1418b199dddbSQi Zhang 		val |= (mask << GLQF_FDMASK_MASK_S) & GLQF_FDMASK_MASK_M;
1419b199dddbSQi Zhang 		break;
1420b199dddbSQi Zhang 	default:
1421b199dddbSQi Zhang 		ice_debug(hw, ICE_DBG_PKG, "No profile masks for block %d\n",
1422b199dddbSQi Zhang 			  blk);
1423b199dddbSQi Zhang 		return;
1424b199dddbSQi Zhang 	}
1425b199dddbSQi Zhang 
1426b199dddbSQi Zhang 	wr32(hw, offset, val);
1427b199dddbSQi Zhang 	ice_debug(hw, ICE_DBG_PKG, "write mask, blk %d (%d): %x = %x\n",
1428b199dddbSQi Zhang 		  blk, idx, offset, val);
1429b199dddbSQi Zhang }
1430b199dddbSQi Zhang 
1431b199dddbSQi Zhang /**
1432b199dddbSQi Zhang  * ice_write_prof_mask_enable_res - write profile mask enable register
1433b199dddbSQi Zhang  * @hw: pointer to the HW struct
1434b199dddbSQi Zhang  * @blk: hardware block
1435b199dddbSQi Zhang  * @prof_id: profile ID
1436b199dddbSQi Zhang  * @enable_mask: enable mask
1437b199dddbSQi Zhang  */
1438b199dddbSQi Zhang static void
ice_write_prof_mask_enable_res(struct ice_hw * hw,enum ice_block blk,u16 prof_id,u32 enable_mask)1439b199dddbSQi Zhang ice_write_prof_mask_enable_res(struct ice_hw *hw, enum ice_block blk,
1440b199dddbSQi Zhang 			       u16 prof_id, u32 enable_mask)
1441b199dddbSQi Zhang {
1442b199dddbSQi Zhang 	u32 offset;
1443b199dddbSQi Zhang 
1444b199dddbSQi Zhang 	switch (blk) {
1445b199dddbSQi Zhang 	case ICE_BLK_RSS:
1446b199dddbSQi Zhang 		offset = GLQF_HMASK_SEL(prof_id);
1447b199dddbSQi Zhang 		break;
1448b199dddbSQi Zhang 	case ICE_BLK_FD:
1449b199dddbSQi Zhang 		offset = GLQF_FDMASK_SEL(prof_id);
1450b199dddbSQi Zhang 		break;
1451b199dddbSQi Zhang 	default:
1452b199dddbSQi Zhang 		ice_debug(hw, ICE_DBG_PKG, "No profile masks for block %d\n",
1453b199dddbSQi Zhang 			  blk);
1454b199dddbSQi Zhang 		return;
1455b199dddbSQi Zhang 	}
1456b199dddbSQi Zhang 
1457b199dddbSQi Zhang 	wr32(hw, offset, enable_mask);
1458b199dddbSQi Zhang 	ice_debug(hw, ICE_DBG_PKG, "write mask enable, blk %d (%d): %x = %x\n",
1459b199dddbSQi Zhang 		  blk, prof_id, offset, enable_mask);
1460b199dddbSQi Zhang }
1461b199dddbSQi Zhang 
1462b199dddbSQi Zhang /**
1463b199dddbSQi Zhang  * ice_init_prof_masks - initial prof masks
1464b199dddbSQi Zhang  * @hw: pointer to the HW struct
1465b199dddbSQi Zhang  * @blk: hardware block
1466b199dddbSQi Zhang  */
ice_init_prof_masks(struct ice_hw * hw,enum ice_block blk)1467b199dddbSQi Zhang static void ice_init_prof_masks(struct ice_hw *hw, enum ice_block blk)
1468b199dddbSQi Zhang {
1469b199dddbSQi Zhang 	u16 per_pf;
1470b199dddbSQi Zhang 	u16 i;
1471b199dddbSQi Zhang 
1472b199dddbSQi Zhang 	mutex_init(&hw->blk[blk].masks.lock);
1473b199dddbSQi Zhang 
1474b199dddbSQi Zhang 	per_pf = ICE_PROF_MASK_COUNT / hw->dev_caps.num_funcs;
1475b199dddbSQi Zhang 
1476b199dddbSQi Zhang 	hw->blk[blk].masks.count = per_pf;
1477b199dddbSQi Zhang 	hw->blk[blk].masks.first = hw->pf_id * per_pf;
1478b199dddbSQi Zhang 
1479b199dddbSQi Zhang 	memset(hw->blk[blk].masks.masks, 0, sizeof(hw->blk[blk].masks.masks));
1480b199dddbSQi Zhang 
1481b199dddbSQi Zhang 	for (i = hw->blk[blk].masks.first;
1482b199dddbSQi Zhang 	     i < hw->blk[blk].masks.first + hw->blk[blk].masks.count; i++)
1483b199dddbSQi Zhang 		ice_write_prof_mask_reg(hw, blk, i, 0, 0);
1484b199dddbSQi Zhang }
1485b199dddbSQi Zhang 
1486b199dddbSQi Zhang /**
1487b199dddbSQi Zhang  * ice_init_all_prof_masks - initialize all prof masks
1488b199dddbSQi Zhang  * @hw: pointer to the HW struct
1489b199dddbSQi Zhang  */
ice_init_all_prof_masks(struct ice_hw * hw)1490b199dddbSQi Zhang static void ice_init_all_prof_masks(struct ice_hw *hw)
1491b199dddbSQi Zhang {
1492b199dddbSQi Zhang 	ice_init_prof_masks(hw, ICE_BLK_RSS);
1493b199dddbSQi Zhang 	ice_init_prof_masks(hw, ICE_BLK_FD);
1494b199dddbSQi Zhang }
1495b199dddbSQi Zhang 
1496b199dddbSQi Zhang /**
1497b199dddbSQi Zhang  * ice_alloc_prof_mask - allocate profile mask
1498b199dddbSQi Zhang  * @hw: pointer to the HW struct
1499b199dddbSQi Zhang  * @blk: hardware block
1500b199dddbSQi Zhang  * @idx: index of FV which will use the mask
1501b199dddbSQi Zhang  * @mask: the 16-bit mask
1502b199dddbSQi Zhang  * @mask_idx: variable to receive the mask index
1503b199dddbSQi Zhang  */
15045e24d598STony Nguyen static int
ice_alloc_prof_mask(struct ice_hw * hw,enum ice_block blk,u16 idx,u16 mask,u16 * mask_idx)1505b199dddbSQi Zhang ice_alloc_prof_mask(struct ice_hw *hw, enum ice_block blk, u16 idx, u16 mask,
1506b199dddbSQi Zhang 		    u16 *mask_idx)
1507b199dddbSQi Zhang {
1508b199dddbSQi Zhang 	bool found_unused = false, found_copy = false;
1509b199dddbSQi Zhang 	u16 unused_idx = 0, copy_idx = 0;
15105518ac2aSTony Nguyen 	int status = -ENOSPC;
1511b199dddbSQi Zhang 	u16 i;
1512b199dddbSQi Zhang 
1513b199dddbSQi Zhang 	if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD)
1514d54699e2STony Nguyen 		return -EINVAL;
1515b199dddbSQi Zhang 
1516b199dddbSQi Zhang 	mutex_lock(&hw->blk[blk].masks.lock);
1517b199dddbSQi Zhang 
1518b199dddbSQi Zhang 	for (i = hw->blk[blk].masks.first;
1519b199dddbSQi Zhang 	     i < hw->blk[blk].masks.first + hw->blk[blk].masks.count; i++)
1520b199dddbSQi Zhang 		if (hw->blk[blk].masks.masks[i].in_use) {
1521b199dddbSQi Zhang 			/* if mask is in use and it exactly duplicates the
1522b199dddbSQi Zhang 			 * desired mask and index, then in can be reused
1523b199dddbSQi Zhang 			 */
1524b199dddbSQi Zhang 			if (hw->blk[blk].masks.masks[i].mask == mask &&
1525b199dddbSQi Zhang 			    hw->blk[blk].masks.masks[i].idx == idx) {
1526b199dddbSQi Zhang 				found_copy = true;
1527b199dddbSQi Zhang 				copy_idx = i;
1528b199dddbSQi Zhang 				break;
1529b199dddbSQi Zhang 			}
1530b199dddbSQi Zhang 		} else {
1531b199dddbSQi Zhang 			/* save off unused index, but keep searching in case
1532b199dddbSQi Zhang 			 * there is an exact match later on
1533b199dddbSQi Zhang 			 */
1534b199dddbSQi Zhang 			if (!found_unused) {
1535b199dddbSQi Zhang 				found_unused = true;
1536b199dddbSQi Zhang 				unused_idx = i;
1537b199dddbSQi Zhang 			}
1538b199dddbSQi Zhang 		}
1539b199dddbSQi Zhang 
1540b199dddbSQi Zhang 	if (found_copy)
1541b199dddbSQi Zhang 		i = copy_idx;
1542b199dddbSQi Zhang 	else if (found_unused)
1543b199dddbSQi Zhang 		i = unused_idx;
1544b199dddbSQi Zhang 	else
1545b199dddbSQi Zhang 		goto err_ice_alloc_prof_mask;
1546b199dddbSQi Zhang 
1547b199dddbSQi Zhang 	/* update mask for a new entry */
1548b199dddbSQi Zhang 	if (found_unused) {
1549b199dddbSQi Zhang 		hw->blk[blk].masks.masks[i].in_use = true;
1550b199dddbSQi Zhang 		hw->blk[blk].masks.masks[i].mask = mask;
1551b199dddbSQi Zhang 		hw->blk[blk].masks.masks[i].idx = idx;
1552b199dddbSQi Zhang 		hw->blk[blk].masks.masks[i].ref = 0;
1553b199dddbSQi Zhang 		ice_write_prof_mask_reg(hw, blk, i, idx, mask);
1554b199dddbSQi Zhang 	}
1555b199dddbSQi Zhang 
1556b199dddbSQi Zhang 	hw->blk[blk].masks.masks[i].ref++;
1557b199dddbSQi Zhang 	*mask_idx = i;
1558b199dddbSQi Zhang 	status = 0;
1559b199dddbSQi Zhang 
1560b199dddbSQi Zhang err_ice_alloc_prof_mask:
1561b199dddbSQi Zhang 	mutex_unlock(&hw->blk[blk].masks.lock);
1562b199dddbSQi Zhang 
1563b199dddbSQi Zhang 	return status;
1564b199dddbSQi Zhang }
1565b199dddbSQi Zhang 
1566b199dddbSQi Zhang /**
1567b199dddbSQi Zhang  * ice_free_prof_mask - free profile mask
1568b199dddbSQi Zhang  * @hw: pointer to the HW struct
1569b199dddbSQi Zhang  * @blk: hardware block
1570b199dddbSQi Zhang  * @mask_idx: index of mask
1571b199dddbSQi Zhang  */
15725e24d598STony Nguyen static int
ice_free_prof_mask(struct ice_hw * hw,enum ice_block blk,u16 mask_idx)1573b199dddbSQi Zhang ice_free_prof_mask(struct ice_hw *hw, enum ice_block blk, u16 mask_idx)
1574b199dddbSQi Zhang {
1575b199dddbSQi Zhang 	if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD)
1576d54699e2STony Nguyen 		return -EINVAL;
1577b199dddbSQi Zhang 
1578b199dddbSQi Zhang 	if (!(mask_idx >= hw->blk[blk].masks.first &&
1579b199dddbSQi Zhang 	      mask_idx < hw->blk[blk].masks.first + hw->blk[blk].masks.count))
1580d54699e2STony Nguyen 		return -ENOENT;
1581b199dddbSQi Zhang 
1582b199dddbSQi Zhang 	mutex_lock(&hw->blk[blk].masks.lock);
1583b199dddbSQi Zhang 
1584b199dddbSQi Zhang 	if (!hw->blk[blk].masks.masks[mask_idx].in_use)
1585b199dddbSQi Zhang 		goto exit_ice_free_prof_mask;
1586b199dddbSQi Zhang 
1587b199dddbSQi Zhang 	if (hw->blk[blk].masks.masks[mask_idx].ref > 1) {
1588b199dddbSQi Zhang 		hw->blk[blk].masks.masks[mask_idx].ref--;
1589b199dddbSQi Zhang 		goto exit_ice_free_prof_mask;
1590b199dddbSQi Zhang 	}
1591b199dddbSQi Zhang 
1592b199dddbSQi Zhang 	/* remove mask */
1593b199dddbSQi Zhang 	hw->blk[blk].masks.masks[mask_idx].in_use = false;
1594b199dddbSQi Zhang 	hw->blk[blk].masks.masks[mask_idx].mask = 0;
1595b199dddbSQi Zhang 	hw->blk[blk].masks.masks[mask_idx].idx = 0;
1596b199dddbSQi Zhang 
1597b199dddbSQi Zhang 	/* update mask as unused entry */
1598b199dddbSQi Zhang 	ice_debug(hw, ICE_DBG_PKG, "Free mask, blk %d, mask %d\n", blk,
1599b199dddbSQi Zhang 		  mask_idx);
1600b199dddbSQi Zhang 	ice_write_prof_mask_reg(hw, blk, mask_idx, 0, 0);
1601b199dddbSQi Zhang 
1602b199dddbSQi Zhang exit_ice_free_prof_mask:
1603b199dddbSQi Zhang 	mutex_unlock(&hw->blk[blk].masks.lock);
1604b199dddbSQi Zhang 
1605b199dddbSQi Zhang 	return 0;
1606b199dddbSQi Zhang }
1607b199dddbSQi Zhang 
1608b199dddbSQi Zhang /**
1609b199dddbSQi Zhang  * ice_free_prof_masks - free all profile masks for a profile
1610b199dddbSQi Zhang  * @hw: pointer to the HW struct
1611b199dddbSQi Zhang  * @blk: hardware block
1612b199dddbSQi Zhang  * @prof_id: profile ID
1613b199dddbSQi Zhang  */
16145e24d598STony Nguyen static int
ice_free_prof_masks(struct ice_hw * hw,enum ice_block blk,u16 prof_id)1615b199dddbSQi Zhang ice_free_prof_masks(struct ice_hw *hw, enum ice_block blk, u16 prof_id)
1616b199dddbSQi Zhang {
1617b199dddbSQi Zhang 	u32 mask_bm;
1618b199dddbSQi Zhang 	u16 i;
1619b199dddbSQi Zhang 
1620b199dddbSQi Zhang 	if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD)
1621d54699e2STony Nguyen 		return -EINVAL;
1622b199dddbSQi Zhang 
1623b199dddbSQi Zhang 	mask_bm = hw->blk[blk].es.mask_ena[prof_id];
1624b199dddbSQi Zhang 	for (i = 0; i < BITS_PER_BYTE * sizeof(mask_bm); i++)
1625b199dddbSQi Zhang 		if (mask_bm & BIT(i))
1626b199dddbSQi Zhang 			ice_free_prof_mask(hw, blk, i);
1627b199dddbSQi Zhang 
1628b199dddbSQi Zhang 	return 0;
1629b199dddbSQi Zhang }
1630b199dddbSQi Zhang 
1631b199dddbSQi Zhang /**
1632b199dddbSQi Zhang  * ice_shutdown_prof_masks - releases lock for masking
1633b199dddbSQi Zhang  * @hw: pointer to the HW struct
1634b199dddbSQi Zhang  * @blk: hardware block
1635b199dddbSQi Zhang  *
1636b199dddbSQi Zhang  * This should be called before unloading the driver
1637b199dddbSQi Zhang  */
ice_shutdown_prof_masks(struct ice_hw * hw,enum ice_block blk)1638b199dddbSQi Zhang static void ice_shutdown_prof_masks(struct ice_hw *hw, enum ice_block blk)
1639b199dddbSQi Zhang {
1640b199dddbSQi Zhang 	u16 i;
1641b199dddbSQi Zhang 
1642b199dddbSQi Zhang 	mutex_lock(&hw->blk[blk].masks.lock);
1643b199dddbSQi Zhang 
1644b199dddbSQi Zhang 	for (i = hw->blk[blk].masks.first;
1645b199dddbSQi Zhang 	     i < hw->blk[blk].masks.first + hw->blk[blk].masks.count; i++) {
1646b199dddbSQi Zhang 		ice_write_prof_mask_reg(hw, blk, i, 0, 0);
1647b199dddbSQi Zhang 
1648b199dddbSQi Zhang 		hw->blk[blk].masks.masks[i].in_use = false;
1649b199dddbSQi Zhang 		hw->blk[blk].masks.masks[i].idx = 0;
1650b199dddbSQi Zhang 		hw->blk[blk].masks.masks[i].mask = 0;
1651b199dddbSQi Zhang 	}
1652b199dddbSQi Zhang 
1653b199dddbSQi Zhang 	mutex_unlock(&hw->blk[blk].masks.lock);
1654b199dddbSQi Zhang 	mutex_destroy(&hw->blk[blk].masks.lock);
1655b199dddbSQi Zhang }
1656b199dddbSQi Zhang 
1657b199dddbSQi Zhang /**
1658b199dddbSQi Zhang  * ice_shutdown_all_prof_masks - releases all locks for masking
1659b199dddbSQi Zhang  * @hw: pointer to the HW struct
1660b199dddbSQi Zhang  *
1661b199dddbSQi Zhang  * This should be called before unloading the driver
1662b199dddbSQi Zhang  */
ice_shutdown_all_prof_masks(struct ice_hw * hw)1663b199dddbSQi Zhang static void ice_shutdown_all_prof_masks(struct ice_hw *hw)
1664b199dddbSQi Zhang {
1665b199dddbSQi Zhang 	ice_shutdown_prof_masks(hw, ICE_BLK_RSS);
1666b199dddbSQi Zhang 	ice_shutdown_prof_masks(hw, ICE_BLK_FD);
1667b199dddbSQi Zhang }
1668b199dddbSQi Zhang 
1669b199dddbSQi Zhang /**
1670b199dddbSQi Zhang  * ice_update_prof_masking - set registers according to masking
1671b199dddbSQi Zhang  * @hw: pointer to the HW struct
1672b199dddbSQi Zhang  * @blk: hardware block
1673b199dddbSQi Zhang  * @prof_id: profile ID
1674b199dddbSQi Zhang  * @masks: masks
1675b199dddbSQi Zhang  */
16765e24d598STony Nguyen static int
ice_update_prof_masking(struct ice_hw * hw,enum ice_block blk,u16 prof_id,u16 * masks)1677b199dddbSQi Zhang ice_update_prof_masking(struct ice_hw *hw, enum ice_block blk, u16 prof_id,
1678b199dddbSQi Zhang 			u16 *masks)
1679b199dddbSQi Zhang {
1680b199dddbSQi Zhang 	bool err = false;
1681b199dddbSQi Zhang 	u32 ena_mask = 0;
1682b199dddbSQi Zhang 	u16 idx;
1683b199dddbSQi Zhang 	u16 i;
1684b199dddbSQi Zhang 
1685b199dddbSQi Zhang 	/* Only support FD and RSS masking, otherwise nothing to be done */
1686b199dddbSQi Zhang 	if (blk != ICE_BLK_RSS && blk != ICE_BLK_FD)
1687b199dddbSQi Zhang 		return 0;
1688b199dddbSQi Zhang 
1689b199dddbSQi Zhang 	for (i = 0; i < hw->blk[blk].es.fvw; i++)
1690b199dddbSQi Zhang 		if (masks[i] && masks[i] != 0xFFFF) {
1691b199dddbSQi Zhang 			if (!ice_alloc_prof_mask(hw, blk, i, masks[i], &idx)) {
1692b199dddbSQi Zhang 				ena_mask |= BIT(idx);
1693b199dddbSQi Zhang 			} else {
1694b199dddbSQi Zhang 				/* not enough bitmaps */
1695b199dddbSQi Zhang 				err = true;
1696b199dddbSQi Zhang 				break;
1697b199dddbSQi Zhang 			}
1698b199dddbSQi Zhang 		}
1699b199dddbSQi Zhang 
1700b199dddbSQi Zhang 	if (err) {
1701b199dddbSQi Zhang 		/* free any bitmaps we have allocated */
1702b199dddbSQi Zhang 		for (i = 0; i < BITS_PER_BYTE * sizeof(ena_mask); i++)
1703b199dddbSQi Zhang 			if (ena_mask & BIT(i))
1704b199dddbSQi Zhang 				ice_free_prof_mask(hw, blk, i);
1705b199dddbSQi Zhang 
1706d54699e2STony Nguyen 		return -EIO;
1707b199dddbSQi Zhang 	}
1708b199dddbSQi Zhang 
1709b199dddbSQi Zhang 	/* enable the masks for this profile */
1710b199dddbSQi Zhang 	ice_write_prof_mask_enable_res(hw, blk, prof_id, ena_mask);
1711b199dddbSQi Zhang 
1712b199dddbSQi Zhang 	/* store enabled masks with profile so that they can be freed later */
1713b199dddbSQi Zhang 	hw->blk[blk].es.mask_ena[prof_id] = ena_mask;
1714b199dddbSQi Zhang 
1715b199dddbSQi Zhang 	return 0;
1716b199dddbSQi Zhang }
1717b199dddbSQi Zhang 
1718b199dddbSQi Zhang /**
171931ad4e4eSTony Nguyen  * ice_write_es - write an extraction sequence to hardware
172031ad4e4eSTony Nguyen  * @hw: pointer to the HW struct
172131ad4e4eSTony Nguyen  * @blk: the block in which to write the extraction sequence
172231ad4e4eSTony Nguyen  * @prof_id: the profile ID to write
172331ad4e4eSTony Nguyen  * @fv: pointer to the extraction sequence to write - NULL to clear extraction
172431ad4e4eSTony Nguyen  */
172531ad4e4eSTony Nguyen static void
ice_write_es(struct ice_hw * hw,enum ice_block blk,u8 prof_id,struct ice_fv_word * fv)172631ad4e4eSTony Nguyen ice_write_es(struct ice_hw *hw, enum ice_block blk, u8 prof_id,
172731ad4e4eSTony Nguyen 	     struct ice_fv_word *fv)
172831ad4e4eSTony Nguyen {
172931ad4e4eSTony Nguyen 	u16 off;
173031ad4e4eSTony Nguyen 
173131ad4e4eSTony Nguyen 	off = prof_id * hw->blk[blk].es.fvw;
173231ad4e4eSTony Nguyen 	if (!fv) {
173331ad4e4eSTony Nguyen 		memset(&hw->blk[blk].es.t[off], 0,
173431ad4e4eSTony Nguyen 		       hw->blk[blk].es.fvw * sizeof(*fv));
173531ad4e4eSTony Nguyen 		hw->blk[blk].es.written[prof_id] = false;
173631ad4e4eSTony Nguyen 	} else {
173731ad4e4eSTony Nguyen 		memcpy(&hw->blk[blk].es.t[off], fv,
173831ad4e4eSTony Nguyen 		       hw->blk[blk].es.fvw * sizeof(*fv));
173931ad4e4eSTony Nguyen 	}
174031ad4e4eSTony Nguyen }
174131ad4e4eSTony Nguyen 
17422c61054cSTony Nguyen /**
17432c61054cSTony Nguyen  * ice_prof_dec_ref - decrement reference count for profile
17442c61054cSTony Nguyen  * @hw: pointer to the HW struct
17452c61054cSTony Nguyen  * @blk: the block from which to free the profile ID
17462c61054cSTony Nguyen  * @prof_id: the profile ID for which to decrement the reference count
17472c61054cSTony Nguyen  */
17485e24d598STony Nguyen static int
ice_prof_dec_ref(struct ice_hw * hw,enum ice_block blk,u8 prof_id)17492c61054cSTony Nguyen ice_prof_dec_ref(struct ice_hw *hw, enum ice_block blk, u8 prof_id)
17502c61054cSTony Nguyen {
17512c61054cSTony Nguyen 	if (prof_id > hw->blk[blk].es.count)
1752d54699e2STony Nguyen 		return -EINVAL;
17532c61054cSTony Nguyen 
17542c61054cSTony Nguyen 	if (hw->blk[blk].es.ref_count[prof_id] > 0) {
17552c61054cSTony Nguyen 		if (!--hw->blk[blk].es.ref_count[prof_id]) {
17562c61054cSTony Nguyen 			ice_write_es(hw, blk, prof_id, NULL);
1757b199dddbSQi Zhang 			ice_free_prof_masks(hw, blk, prof_id);
17582c61054cSTony Nguyen 			return ice_free_prof_id(hw, blk, prof_id);
17592c61054cSTony Nguyen 		}
17602c61054cSTony Nguyen 	}
17612c61054cSTony Nguyen 
17622c61054cSTony Nguyen 	return 0;
17632c61054cSTony Nguyen }
17642c61054cSTony Nguyen 
176532d63fa1STony Nguyen /* Block / table section IDs */
176632d63fa1STony Nguyen static const u32 ice_blk_sids[ICE_BLK_COUNT][ICE_SID_OFF_COUNT] = {
176732d63fa1STony Nguyen 	/* SWITCH */
176832d63fa1STony Nguyen 	{	ICE_SID_XLT1_SW,
176932d63fa1STony Nguyen 		ICE_SID_XLT2_SW,
177032d63fa1STony Nguyen 		ICE_SID_PROFID_TCAM_SW,
177132d63fa1STony Nguyen 		ICE_SID_PROFID_REDIR_SW,
177232d63fa1STony Nguyen 		ICE_SID_FLD_VEC_SW
177332d63fa1STony Nguyen 	},
177432d63fa1STony Nguyen 
177532d63fa1STony Nguyen 	/* ACL */
177632d63fa1STony Nguyen 	{	ICE_SID_XLT1_ACL,
177732d63fa1STony Nguyen 		ICE_SID_XLT2_ACL,
177832d63fa1STony Nguyen 		ICE_SID_PROFID_TCAM_ACL,
177932d63fa1STony Nguyen 		ICE_SID_PROFID_REDIR_ACL,
178032d63fa1STony Nguyen 		ICE_SID_FLD_VEC_ACL
178132d63fa1STony Nguyen 	},
178232d63fa1STony Nguyen 
178332d63fa1STony Nguyen 	/* FD */
178432d63fa1STony Nguyen 	{	ICE_SID_XLT1_FD,
178532d63fa1STony Nguyen 		ICE_SID_XLT2_FD,
178632d63fa1STony Nguyen 		ICE_SID_PROFID_TCAM_FD,
178732d63fa1STony Nguyen 		ICE_SID_PROFID_REDIR_FD,
178832d63fa1STony Nguyen 		ICE_SID_FLD_VEC_FD
178932d63fa1STony Nguyen 	},
179032d63fa1STony Nguyen 
179132d63fa1STony Nguyen 	/* RSS */
179232d63fa1STony Nguyen 	{	ICE_SID_XLT1_RSS,
179332d63fa1STony Nguyen 		ICE_SID_XLT2_RSS,
179432d63fa1STony Nguyen 		ICE_SID_PROFID_TCAM_RSS,
179532d63fa1STony Nguyen 		ICE_SID_PROFID_REDIR_RSS,
179632d63fa1STony Nguyen 		ICE_SID_FLD_VEC_RSS
179732d63fa1STony Nguyen 	},
179832d63fa1STony Nguyen 
179932d63fa1STony Nguyen 	/* PE */
180032d63fa1STony Nguyen 	{	ICE_SID_XLT1_PE,
180132d63fa1STony Nguyen 		ICE_SID_XLT2_PE,
180232d63fa1STony Nguyen 		ICE_SID_PROFID_TCAM_PE,
180332d63fa1STony Nguyen 		ICE_SID_PROFID_REDIR_PE,
180432d63fa1STony Nguyen 		ICE_SID_FLD_VEC_PE
180532d63fa1STony Nguyen 	}
180632d63fa1STony Nguyen };
180732d63fa1STony Nguyen 
180832d63fa1STony Nguyen /**
180932d63fa1STony Nguyen  * ice_init_sw_xlt1_db - init software XLT1 database from HW tables
181032d63fa1STony Nguyen  * @hw: pointer to the hardware structure
181132d63fa1STony Nguyen  * @blk: the HW block to initialize
181232d63fa1STony Nguyen  */
ice_init_sw_xlt1_db(struct ice_hw * hw,enum ice_block blk)181332d63fa1STony Nguyen static void ice_init_sw_xlt1_db(struct ice_hw *hw, enum ice_block blk)
181432d63fa1STony Nguyen {
181532d63fa1STony Nguyen 	u16 pt;
181632d63fa1STony Nguyen 
181732d63fa1STony Nguyen 	for (pt = 0; pt < hw->blk[blk].xlt1.count; pt++) {
181832d63fa1STony Nguyen 		u8 ptg;
181932d63fa1STony Nguyen 
182032d63fa1STony Nguyen 		ptg = hw->blk[blk].xlt1.t[pt];
182132d63fa1STony Nguyen 		if (ptg != ICE_DEFAULT_PTG) {
182232d63fa1STony Nguyen 			ice_ptg_alloc_val(hw, blk, ptg);
182332d63fa1STony Nguyen 			ice_ptg_add_mv_ptype(hw, blk, pt, ptg);
182432d63fa1STony Nguyen 		}
182532d63fa1STony Nguyen 	}
182632d63fa1STony Nguyen }
182732d63fa1STony Nguyen 
182832d63fa1STony Nguyen /**
182932d63fa1STony Nguyen  * ice_init_sw_xlt2_db - init software XLT2 database from HW tables
183032d63fa1STony Nguyen  * @hw: pointer to the hardware structure
183132d63fa1STony Nguyen  * @blk: the HW block to initialize
183232d63fa1STony Nguyen  */
ice_init_sw_xlt2_db(struct ice_hw * hw,enum ice_block blk)183332d63fa1STony Nguyen static void ice_init_sw_xlt2_db(struct ice_hw *hw, enum ice_block blk)
183432d63fa1STony Nguyen {
183532d63fa1STony Nguyen 	u16 vsi;
183632d63fa1STony Nguyen 
183732d63fa1STony Nguyen 	for (vsi = 0; vsi < hw->blk[blk].xlt2.count; vsi++) {
183832d63fa1STony Nguyen 		u16 vsig;
183932d63fa1STony Nguyen 
184032d63fa1STony Nguyen 		vsig = hw->blk[blk].xlt2.t[vsi];
184132d63fa1STony Nguyen 		if (vsig) {
184232d63fa1STony Nguyen 			ice_vsig_alloc_val(hw, blk, vsig);
184332d63fa1STony Nguyen 			ice_vsig_add_mv_vsi(hw, blk, vsi, vsig);
184432d63fa1STony Nguyen 			/* no changes at this time, since this has been
184532d63fa1STony Nguyen 			 * initialized from the original package
184632d63fa1STony Nguyen 			 */
184732d63fa1STony Nguyen 			hw->blk[blk].xlt2.vsis[vsi].changed = 0;
184832d63fa1STony Nguyen 		}
184932d63fa1STony Nguyen 	}
185032d63fa1STony Nguyen }
185132d63fa1STony Nguyen 
185232d63fa1STony Nguyen /**
185332d63fa1STony Nguyen  * ice_init_sw_db - init software database from HW tables
185432d63fa1STony Nguyen  * @hw: pointer to the hardware structure
185532d63fa1STony Nguyen  */
ice_init_sw_db(struct ice_hw * hw)185632d63fa1STony Nguyen static void ice_init_sw_db(struct ice_hw *hw)
185732d63fa1STony Nguyen {
185832d63fa1STony Nguyen 	u16 i;
185932d63fa1STony Nguyen 
186032d63fa1STony Nguyen 	for (i = 0; i < ICE_BLK_COUNT; i++) {
186132d63fa1STony Nguyen 		ice_init_sw_xlt1_db(hw, (enum ice_block)i);
186232d63fa1STony Nguyen 		ice_init_sw_xlt2_db(hw, (enum ice_block)i);
186332d63fa1STony Nguyen 	}
186432d63fa1STony Nguyen }
186532d63fa1STony Nguyen 
186632d63fa1STony Nguyen /**
186732d63fa1STony Nguyen  * ice_fill_tbl - Reads content of a single table type into database
186832d63fa1STony Nguyen  * @hw: pointer to the hardware structure
186932d63fa1STony Nguyen  * @block_id: Block ID of the table to copy
187032d63fa1STony Nguyen  * @sid: Section ID of the table to copy
187132d63fa1STony Nguyen  *
187232d63fa1STony Nguyen  * Will attempt to read the entire content of a given table of a single block
187332d63fa1STony Nguyen  * into the driver database. We assume that the buffer will always
187432d63fa1STony Nguyen  * be as large or larger than the data contained in the package. If
187532d63fa1STony Nguyen  * this condition is not met, there is most likely an error in the package
187632d63fa1STony Nguyen  * contents.
187732d63fa1STony Nguyen  */
ice_fill_tbl(struct ice_hw * hw,enum ice_block block_id,u32 sid)187832d63fa1STony Nguyen static void ice_fill_tbl(struct ice_hw *hw, enum ice_block block_id, u32 sid)
187932d63fa1STony Nguyen {
188032d63fa1STony Nguyen 	u32 dst_len, sect_len, offset = 0;
188132d63fa1STony Nguyen 	struct ice_prof_redir_section *pr;
188232d63fa1STony Nguyen 	struct ice_prof_id_section *pid;
188332d63fa1STony Nguyen 	struct ice_xlt1_section *xlt1;
188432d63fa1STony Nguyen 	struct ice_xlt2_section *xlt2;
188532d63fa1STony Nguyen 	struct ice_sw_fv_section *es;
188632d63fa1STony Nguyen 	struct ice_pkg_enum state;
188732d63fa1STony Nguyen 	u8 *src, *dst;
188832d63fa1STony Nguyen 	void *sect;
188932d63fa1STony Nguyen 
189032d63fa1STony Nguyen 	/* if the HW segment pointer is null then the first iteration of
189132d63fa1STony Nguyen 	 * ice_pkg_enum_section() will fail. In this case the HW tables will
189232d63fa1STony Nguyen 	 * not be filled and return success.
189332d63fa1STony Nguyen 	 */
189432d63fa1STony Nguyen 	if (!hw->seg) {
189532d63fa1STony Nguyen 		ice_debug(hw, ICE_DBG_PKG, "hw->seg is NULL, tables are not filled\n");
189632d63fa1STony Nguyen 		return;
189732d63fa1STony Nguyen 	}
189832d63fa1STony Nguyen 
189932d63fa1STony Nguyen 	memset(&state, 0, sizeof(state));
190032d63fa1STony Nguyen 
190132d63fa1STony Nguyen 	sect = ice_pkg_enum_section(hw->seg, &state, sid);
190232d63fa1STony Nguyen 
190332d63fa1STony Nguyen 	while (sect) {
190432d63fa1STony Nguyen 		switch (sid) {
190532d63fa1STony Nguyen 		case ICE_SID_XLT1_SW:
190632d63fa1STony Nguyen 		case ICE_SID_XLT1_FD:
190732d63fa1STony Nguyen 		case ICE_SID_XLT1_RSS:
190832d63fa1STony Nguyen 		case ICE_SID_XLT1_ACL:
190932d63fa1STony Nguyen 		case ICE_SID_XLT1_PE:
19107a63dae0SBruce Allan 			xlt1 = sect;
191132d63fa1STony Nguyen 			src = xlt1->value;
191232d63fa1STony Nguyen 			sect_len = le16_to_cpu(xlt1->count) *
191332d63fa1STony Nguyen 				sizeof(*hw->blk[block_id].xlt1.t);
191432d63fa1STony Nguyen 			dst = hw->blk[block_id].xlt1.t;
191532d63fa1STony Nguyen 			dst_len = hw->blk[block_id].xlt1.count *
191632d63fa1STony Nguyen 				sizeof(*hw->blk[block_id].xlt1.t);
191732d63fa1STony Nguyen 			break;
191832d63fa1STony Nguyen 		case ICE_SID_XLT2_SW:
191932d63fa1STony Nguyen 		case ICE_SID_XLT2_FD:
192032d63fa1STony Nguyen 		case ICE_SID_XLT2_RSS:
192132d63fa1STony Nguyen 		case ICE_SID_XLT2_ACL:
192232d63fa1STony Nguyen 		case ICE_SID_XLT2_PE:
19237a63dae0SBruce Allan 			xlt2 = sect;
192432d63fa1STony Nguyen 			src = (__force u8 *)xlt2->value;
192532d63fa1STony Nguyen 			sect_len = le16_to_cpu(xlt2->count) *
192632d63fa1STony Nguyen 				sizeof(*hw->blk[block_id].xlt2.t);
192732d63fa1STony Nguyen 			dst = (u8 *)hw->blk[block_id].xlt2.t;
192832d63fa1STony Nguyen 			dst_len = hw->blk[block_id].xlt2.count *
192932d63fa1STony Nguyen 				sizeof(*hw->blk[block_id].xlt2.t);
193032d63fa1STony Nguyen 			break;
193132d63fa1STony Nguyen 		case ICE_SID_PROFID_TCAM_SW:
193232d63fa1STony Nguyen 		case ICE_SID_PROFID_TCAM_FD:
193332d63fa1STony Nguyen 		case ICE_SID_PROFID_TCAM_RSS:
193432d63fa1STony Nguyen 		case ICE_SID_PROFID_TCAM_ACL:
193532d63fa1STony Nguyen 		case ICE_SID_PROFID_TCAM_PE:
19367a63dae0SBruce Allan 			pid = sect;
193732d63fa1STony Nguyen 			src = (u8 *)pid->entry;
193832d63fa1STony Nguyen 			sect_len = le16_to_cpu(pid->count) *
193932d63fa1STony Nguyen 				sizeof(*hw->blk[block_id].prof.t);
194032d63fa1STony Nguyen 			dst = (u8 *)hw->blk[block_id].prof.t;
194132d63fa1STony Nguyen 			dst_len = hw->blk[block_id].prof.count *
194232d63fa1STony Nguyen 				sizeof(*hw->blk[block_id].prof.t);
194332d63fa1STony Nguyen 			break;
194432d63fa1STony Nguyen 		case ICE_SID_PROFID_REDIR_SW:
194532d63fa1STony Nguyen 		case ICE_SID_PROFID_REDIR_FD:
194632d63fa1STony Nguyen 		case ICE_SID_PROFID_REDIR_RSS:
194732d63fa1STony Nguyen 		case ICE_SID_PROFID_REDIR_ACL:
194832d63fa1STony Nguyen 		case ICE_SID_PROFID_REDIR_PE:
19497a63dae0SBruce Allan 			pr = sect;
195032d63fa1STony Nguyen 			src = pr->redir_value;
195132d63fa1STony Nguyen 			sect_len = le16_to_cpu(pr->count) *
195232d63fa1STony Nguyen 				sizeof(*hw->blk[block_id].prof_redir.t);
195332d63fa1STony Nguyen 			dst = hw->blk[block_id].prof_redir.t;
195432d63fa1STony Nguyen 			dst_len = hw->blk[block_id].prof_redir.count *
195532d63fa1STony Nguyen 				sizeof(*hw->blk[block_id].prof_redir.t);
195632d63fa1STony Nguyen 			break;
195732d63fa1STony Nguyen 		case ICE_SID_FLD_VEC_SW:
195832d63fa1STony Nguyen 		case ICE_SID_FLD_VEC_FD:
195932d63fa1STony Nguyen 		case ICE_SID_FLD_VEC_RSS:
196032d63fa1STony Nguyen 		case ICE_SID_FLD_VEC_ACL:
196132d63fa1STony Nguyen 		case ICE_SID_FLD_VEC_PE:
19627a63dae0SBruce Allan 			es = sect;
196332d63fa1STony Nguyen 			src = (u8 *)es->fv;
196432d63fa1STony Nguyen 			sect_len = (u32)(le16_to_cpu(es->count) *
196532d63fa1STony Nguyen 					 hw->blk[block_id].es.fvw) *
196632d63fa1STony Nguyen 				sizeof(*hw->blk[block_id].es.t);
196732d63fa1STony Nguyen 			dst = (u8 *)hw->blk[block_id].es.t;
196832d63fa1STony Nguyen 			dst_len = (u32)(hw->blk[block_id].es.count *
196932d63fa1STony Nguyen 					hw->blk[block_id].es.fvw) *
197032d63fa1STony Nguyen 				sizeof(*hw->blk[block_id].es.t);
197132d63fa1STony Nguyen 			break;
197232d63fa1STony Nguyen 		default:
197332d63fa1STony Nguyen 			return;
197432d63fa1STony Nguyen 		}
197532d63fa1STony Nguyen 
197632d63fa1STony Nguyen 		/* if the section offset exceeds destination length, terminate
197732d63fa1STony Nguyen 		 * table fill.
197832d63fa1STony Nguyen 		 */
197932d63fa1STony Nguyen 		if (offset > dst_len)
198032d63fa1STony Nguyen 			return;
198132d63fa1STony Nguyen 
198232d63fa1STony Nguyen 		/* if the sum of section size and offset exceed destination size
198332d63fa1STony Nguyen 		 * then we are out of bounds of the HW table size for that PF.
198432d63fa1STony Nguyen 		 * Changing section length to fill the remaining table space
198532d63fa1STony Nguyen 		 * of that PF.
198632d63fa1STony Nguyen 		 */
198732d63fa1STony Nguyen 		if ((offset + sect_len) > dst_len)
198832d63fa1STony Nguyen 			sect_len = dst_len - offset;
198932d63fa1STony Nguyen 
199032d63fa1STony Nguyen 		memcpy(dst + offset, src, sect_len);
199132d63fa1STony Nguyen 		offset += sect_len;
199232d63fa1STony Nguyen 		sect = ice_pkg_enum_section(NULL, &state, sid);
199332d63fa1STony Nguyen 	}
199432d63fa1STony Nguyen }
199532d63fa1STony Nguyen 
199632d63fa1STony Nguyen /**
199732d63fa1STony Nguyen  * ice_fill_blk_tbls - Read package context for tables
199832d63fa1STony Nguyen  * @hw: pointer to the hardware structure
199932d63fa1STony Nguyen  *
200032d63fa1STony Nguyen  * Reads the current package contents and populates the driver
200132d63fa1STony Nguyen  * database with the data iteratively for all advanced feature
200232d63fa1STony Nguyen  * blocks. Assume that the HW tables have been allocated.
200332d63fa1STony Nguyen  */
ice_fill_blk_tbls(struct ice_hw * hw)2004462acf6aSTony Nguyen void ice_fill_blk_tbls(struct ice_hw *hw)
200532d63fa1STony Nguyen {
200632d63fa1STony Nguyen 	u8 i;
200732d63fa1STony Nguyen 
200832d63fa1STony Nguyen 	for (i = 0; i < ICE_BLK_COUNT; i++) {
200932d63fa1STony Nguyen 		enum ice_block blk_id = (enum ice_block)i;
201032d63fa1STony Nguyen 
201132d63fa1STony Nguyen 		ice_fill_tbl(hw, blk_id, hw->blk[blk_id].xlt1.sid);
201232d63fa1STony Nguyen 		ice_fill_tbl(hw, blk_id, hw->blk[blk_id].xlt2.sid);
201332d63fa1STony Nguyen 		ice_fill_tbl(hw, blk_id, hw->blk[blk_id].prof.sid);
201432d63fa1STony Nguyen 		ice_fill_tbl(hw, blk_id, hw->blk[blk_id].prof_redir.sid);
201532d63fa1STony Nguyen 		ice_fill_tbl(hw, blk_id, hw->blk[blk_id].es.sid);
201632d63fa1STony Nguyen 	}
201732d63fa1STony Nguyen 
201832d63fa1STony Nguyen 	ice_init_sw_db(hw);
201932d63fa1STony Nguyen }
202032d63fa1STony Nguyen 
202132d63fa1STony Nguyen /**
20222c61054cSTony Nguyen  * ice_free_prof_map - free profile map
20232c61054cSTony Nguyen  * @hw: pointer to the hardware structure
20242c61054cSTony Nguyen  * @blk_idx: HW block index
20252c61054cSTony Nguyen  */
ice_free_prof_map(struct ice_hw * hw,u8 blk_idx)20262c61054cSTony Nguyen static void ice_free_prof_map(struct ice_hw *hw, u8 blk_idx)
20272c61054cSTony Nguyen {
20282c61054cSTony Nguyen 	struct ice_es *es = &hw->blk[blk_idx].es;
20292c61054cSTony Nguyen 	struct ice_prof_map *del, *tmp;
20302c61054cSTony Nguyen 
20312c61054cSTony Nguyen 	mutex_lock(&es->prof_map_lock);
20322c61054cSTony Nguyen 	list_for_each_entry_safe(del, tmp, &es->prof_map, list) {
20332c61054cSTony Nguyen 		list_del(&del->list);
20342c61054cSTony Nguyen 		devm_kfree(ice_hw_to_dev(hw), del);
20352c61054cSTony Nguyen 	}
20362c61054cSTony Nguyen 	INIT_LIST_HEAD(&es->prof_map);
20372c61054cSTony Nguyen 	mutex_unlock(&es->prof_map_lock);
20382c61054cSTony Nguyen }
20392c61054cSTony Nguyen 
20402c61054cSTony Nguyen /**
20412c61054cSTony Nguyen  * ice_free_flow_profs - free flow profile entries
20422c61054cSTony Nguyen  * @hw: pointer to the hardware structure
20432c61054cSTony Nguyen  * @blk_idx: HW block index
20442c61054cSTony Nguyen  */
ice_free_flow_profs(struct ice_hw * hw,u8 blk_idx)20452c61054cSTony Nguyen static void ice_free_flow_profs(struct ice_hw *hw, u8 blk_idx)
20462c61054cSTony Nguyen {
20472c61054cSTony Nguyen 	struct ice_flow_prof *p, *tmp;
20482c61054cSTony Nguyen 
20492c61054cSTony Nguyen 	mutex_lock(&hw->fl_profs_locks[blk_idx]);
20502c61054cSTony Nguyen 	list_for_each_entry_safe(p, tmp, &hw->fl_profs[blk_idx], l_entry) {
2051148beb61SHenry Tieman 		struct ice_flow_entry *e, *t;
2052148beb61SHenry Tieman 
2053148beb61SHenry Tieman 		list_for_each_entry_safe(e, t, &p->entries, l_entry)
2054148beb61SHenry Tieman 			ice_flow_rem_entry(hw, (enum ice_block)blk_idx,
2055148beb61SHenry Tieman 					   ICE_FLOW_ENTRY_HNDL(e));
2056148beb61SHenry Tieman 
20572c61054cSTony Nguyen 		list_del(&p->l_entry);
2058ec1d1d23SVignesh Sridhar 
2059ec1d1d23SVignesh Sridhar 		mutex_destroy(&p->entries_lock);
20602c61054cSTony Nguyen 		devm_kfree(ice_hw_to_dev(hw), p);
20612c61054cSTony Nguyen 	}
20622c61054cSTony Nguyen 	mutex_unlock(&hw->fl_profs_locks[blk_idx]);
20632c61054cSTony Nguyen 
20642c61054cSTony Nguyen 	/* if driver is in reset and tables are being cleared
20652c61054cSTony Nguyen 	 * re-initialize the flow profile list heads
20662c61054cSTony Nguyen 	 */
20672c61054cSTony Nguyen 	INIT_LIST_HEAD(&hw->fl_profs[blk_idx]);
20682c61054cSTony Nguyen }
20692c61054cSTony Nguyen 
20702c61054cSTony Nguyen /**
20712c61054cSTony Nguyen  * ice_free_vsig_tbl - free complete VSIG table entries
20722c61054cSTony Nguyen  * @hw: pointer to the hardware structure
20732c61054cSTony Nguyen  * @blk: the HW block on which to free the VSIG table entries
20742c61054cSTony Nguyen  */
ice_free_vsig_tbl(struct ice_hw * hw,enum ice_block blk)20752c61054cSTony Nguyen static void ice_free_vsig_tbl(struct ice_hw *hw, enum ice_block blk)
20762c61054cSTony Nguyen {
20772c61054cSTony Nguyen 	u16 i;
20782c61054cSTony Nguyen 
20792c61054cSTony Nguyen 	if (!hw->blk[blk].xlt2.vsig_tbl)
20802c61054cSTony Nguyen 		return;
20812c61054cSTony Nguyen 
20822c61054cSTony Nguyen 	for (i = 1; i < ICE_MAX_VSIGS; i++)
20832c61054cSTony Nguyen 		if (hw->blk[blk].xlt2.vsig_tbl[i].in_use)
20842c61054cSTony Nguyen 			ice_vsig_free(hw, blk, i);
20852c61054cSTony Nguyen }
20862c61054cSTony Nguyen 
20872c61054cSTony Nguyen /**
208832d63fa1STony Nguyen  * ice_free_hw_tbls - free hardware table memory
208932d63fa1STony Nguyen  * @hw: pointer to the hardware structure
209032d63fa1STony Nguyen  */
ice_free_hw_tbls(struct ice_hw * hw)209132d63fa1STony Nguyen void ice_free_hw_tbls(struct ice_hw *hw)
209232d63fa1STony Nguyen {
2093c90ed40cSTony Nguyen 	struct ice_rss_cfg *r, *rt;
209432d63fa1STony Nguyen 	u8 i;
209532d63fa1STony Nguyen 
209632d63fa1STony Nguyen 	for (i = 0; i < ICE_BLK_COUNT; i++) {
2097c90ed40cSTony Nguyen 		if (hw->blk[i].is_list_init) {
2098c90ed40cSTony Nguyen 			struct ice_es *es = &hw->blk[i].es;
209932d63fa1STony Nguyen 
21002c61054cSTony Nguyen 			ice_free_prof_map(hw, i);
2101c90ed40cSTony Nguyen 			mutex_destroy(&es->prof_map_lock);
21022c61054cSTony Nguyen 
21032c61054cSTony Nguyen 			ice_free_flow_profs(hw, i);
2104c90ed40cSTony Nguyen 			mutex_destroy(&hw->fl_profs_locks[i]);
2105c90ed40cSTony Nguyen 
2106c90ed40cSTony Nguyen 			hw->blk[i].is_list_init = false;
2107c90ed40cSTony Nguyen 		}
21082c61054cSTony Nguyen 		ice_free_vsig_tbl(hw, (enum ice_block)i);
210932d63fa1STony Nguyen 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt1.ptypes);
211032d63fa1STony Nguyen 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt1.ptg_tbl);
211132d63fa1STony Nguyen 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt1.t);
211232d63fa1STony Nguyen 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt2.t);
211332d63fa1STony Nguyen 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt2.vsig_tbl);
211432d63fa1STony Nguyen 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].xlt2.vsis);
211532d63fa1STony Nguyen 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].prof.t);
211632d63fa1STony Nguyen 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].prof_redir.t);
211732d63fa1STony Nguyen 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].es.t);
211832d63fa1STony Nguyen 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].es.ref_count);
211932d63fa1STony Nguyen 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].es.written);
2120b199dddbSQi Zhang 		devm_kfree(ice_hw_to_dev(hw), hw->blk[i].es.mask_ena);
212132d63fa1STony Nguyen 	}
212232d63fa1STony Nguyen 
2123c90ed40cSTony Nguyen 	list_for_each_entry_safe(r, rt, &hw->rss_list_head, l_entry) {
2124c90ed40cSTony Nguyen 		list_del(&r->l_entry);
2125c90ed40cSTony Nguyen 		devm_kfree(ice_hw_to_dev(hw), r);
2126c90ed40cSTony Nguyen 	}
2127c90ed40cSTony Nguyen 	mutex_destroy(&hw->rss_locks);
2128b199dddbSQi Zhang 	ice_shutdown_all_prof_masks(hw);
212932d63fa1STony Nguyen 	memset(hw->blk, 0, sizeof(hw->blk));
213032d63fa1STony Nguyen }
213132d63fa1STony Nguyen 
213232d63fa1STony Nguyen /**
2133c90ed40cSTony Nguyen  * ice_init_flow_profs - init flow profile locks and list heads
2134c90ed40cSTony Nguyen  * @hw: pointer to the hardware structure
2135c90ed40cSTony Nguyen  * @blk_idx: HW block index
2136c90ed40cSTony Nguyen  */
ice_init_flow_profs(struct ice_hw * hw,u8 blk_idx)2137c90ed40cSTony Nguyen static void ice_init_flow_profs(struct ice_hw *hw, u8 blk_idx)
2138c90ed40cSTony Nguyen {
2139c90ed40cSTony Nguyen 	mutex_init(&hw->fl_profs_locks[blk_idx]);
2140c90ed40cSTony Nguyen 	INIT_LIST_HEAD(&hw->fl_profs[blk_idx]);
2141c90ed40cSTony Nguyen }
2142c90ed40cSTony Nguyen 
2143c90ed40cSTony Nguyen /**
214432d63fa1STony Nguyen  * ice_clear_hw_tbls - clear HW tables and flow profiles
214532d63fa1STony Nguyen  * @hw: pointer to the hardware structure
214632d63fa1STony Nguyen  */
ice_clear_hw_tbls(struct ice_hw * hw)214732d63fa1STony Nguyen void ice_clear_hw_tbls(struct ice_hw *hw)
214832d63fa1STony Nguyen {
214932d63fa1STony Nguyen 	u8 i;
215032d63fa1STony Nguyen 
215132d63fa1STony Nguyen 	for (i = 0; i < ICE_BLK_COUNT; i++) {
215232d63fa1STony Nguyen 		struct ice_prof_redir *prof_redir = &hw->blk[i].prof_redir;
215332d63fa1STony Nguyen 		struct ice_prof_tcam *prof = &hw->blk[i].prof;
215432d63fa1STony Nguyen 		struct ice_xlt1 *xlt1 = &hw->blk[i].xlt1;
215532d63fa1STony Nguyen 		struct ice_xlt2 *xlt2 = &hw->blk[i].xlt2;
215632d63fa1STony Nguyen 		struct ice_es *es = &hw->blk[i].es;
215732d63fa1STony Nguyen 
21582c61054cSTony Nguyen 		if (hw->blk[i].is_list_init) {
21592c61054cSTony Nguyen 			ice_free_prof_map(hw, i);
21602c61054cSTony Nguyen 			ice_free_flow_profs(hw, i);
21612c61054cSTony Nguyen 		}
21622c61054cSTony Nguyen 
21632c61054cSTony Nguyen 		ice_free_vsig_tbl(hw, (enum ice_block)i);
21642c61054cSTony Nguyen 
216532d63fa1STony Nguyen 		memset(xlt1->ptypes, 0, xlt1->count * sizeof(*xlt1->ptypes));
216632d63fa1STony Nguyen 		memset(xlt1->ptg_tbl, 0,
216732d63fa1STony Nguyen 		       ICE_MAX_PTGS * sizeof(*xlt1->ptg_tbl));
216832d63fa1STony Nguyen 		memset(xlt1->t, 0, xlt1->count * sizeof(*xlt1->t));
216932d63fa1STony Nguyen 
217032d63fa1STony Nguyen 		memset(xlt2->vsis, 0, xlt2->count * sizeof(*xlt2->vsis));
217132d63fa1STony Nguyen 		memset(xlt2->vsig_tbl, 0,
217232d63fa1STony Nguyen 		       xlt2->count * sizeof(*xlt2->vsig_tbl));
217332d63fa1STony Nguyen 		memset(xlt2->t, 0, xlt2->count * sizeof(*xlt2->t));
217432d63fa1STony Nguyen 
217532d63fa1STony Nguyen 		memset(prof->t, 0, prof->count * sizeof(*prof->t));
217632d63fa1STony Nguyen 		memset(prof_redir->t, 0,
217732d63fa1STony Nguyen 		       prof_redir->count * sizeof(*prof_redir->t));
217832d63fa1STony Nguyen 
2179ec1d1d23SVignesh Sridhar 		memset(es->t, 0, es->count * sizeof(*es->t) * es->fvw);
218032d63fa1STony Nguyen 		memset(es->ref_count, 0, es->count * sizeof(*es->ref_count));
218132d63fa1STony Nguyen 		memset(es->written, 0, es->count * sizeof(*es->written));
2182b199dddbSQi Zhang 		memset(es->mask_ena, 0, es->count * sizeof(*es->mask_ena));
218332d63fa1STony Nguyen 	}
218432d63fa1STony Nguyen }
218532d63fa1STony Nguyen 
218632d63fa1STony Nguyen /**
218732d63fa1STony Nguyen  * ice_init_hw_tbls - init hardware table memory
218832d63fa1STony Nguyen  * @hw: pointer to the hardware structure
218932d63fa1STony Nguyen  */
ice_init_hw_tbls(struct ice_hw * hw)21905e24d598STony Nguyen int ice_init_hw_tbls(struct ice_hw *hw)
219132d63fa1STony Nguyen {
219232d63fa1STony Nguyen 	u8 i;
219332d63fa1STony Nguyen 
2194c90ed40cSTony Nguyen 	mutex_init(&hw->rss_locks);
2195c90ed40cSTony Nguyen 	INIT_LIST_HEAD(&hw->rss_list_head);
2196b199dddbSQi Zhang 	ice_init_all_prof_masks(hw);
219732d63fa1STony Nguyen 	for (i = 0; i < ICE_BLK_COUNT; i++) {
219832d63fa1STony Nguyen 		struct ice_prof_redir *prof_redir = &hw->blk[i].prof_redir;
219932d63fa1STony Nguyen 		struct ice_prof_tcam *prof = &hw->blk[i].prof;
220032d63fa1STony Nguyen 		struct ice_xlt1 *xlt1 = &hw->blk[i].xlt1;
220132d63fa1STony Nguyen 		struct ice_xlt2 *xlt2 = &hw->blk[i].xlt2;
220232d63fa1STony Nguyen 		struct ice_es *es = &hw->blk[i].es;
220332d63fa1STony Nguyen 		u16 j;
220432d63fa1STony Nguyen 
220532d63fa1STony Nguyen 		if (hw->blk[i].is_list_init)
220632d63fa1STony Nguyen 			continue;
220732d63fa1STony Nguyen 
2208c90ed40cSTony Nguyen 		ice_init_flow_profs(hw, i);
2209c90ed40cSTony Nguyen 		mutex_init(&es->prof_map_lock);
2210c90ed40cSTony Nguyen 		INIT_LIST_HEAD(&es->prof_map);
221132d63fa1STony Nguyen 		hw->blk[i].is_list_init = true;
221232d63fa1STony Nguyen 
221332d63fa1STony Nguyen 		hw->blk[i].overwrite = blk_sizes[i].overwrite;
221432d63fa1STony Nguyen 		es->reverse = blk_sizes[i].reverse;
221532d63fa1STony Nguyen 
221632d63fa1STony Nguyen 		xlt1->sid = ice_blk_sids[i][ICE_SID_XLT1_OFF];
221732d63fa1STony Nguyen 		xlt1->count = blk_sizes[i].xlt1;
221832d63fa1STony Nguyen 
221932d63fa1STony Nguyen 		xlt1->ptypes = devm_kcalloc(ice_hw_to_dev(hw), xlt1->count,
222032d63fa1STony Nguyen 					    sizeof(*xlt1->ptypes), GFP_KERNEL);
222132d63fa1STony Nguyen 
222232d63fa1STony Nguyen 		if (!xlt1->ptypes)
222332d63fa1STony Nguyen 			goto err;
222432d63fa1STony Nguyen 
222532d63fa1STony Nguyen 		xlt1->ptg_tbl = devm_kcalloc(ice_hw_to_dev(hw), ICE_MAX_PTGS,
222632d63fa1STony Nguyen 					     sizeof(*xlt1->ptg_tbl),
222732d63fa1STony Nguyen 					     GFP_KERNEL);
222832d63fa1STony Nguyen 
222932d63fa1STony Nguyen 		if (!xlt1->ptg_tbl)
223032d63fa1STony Nguyen 			goto err;
223132d63fa1STony Nguyen 
223232d63fa1STony Nguyen 		xlt1->t = devm_kcalloc(ice_hw_to_dev(hw), xlt1->count,
223332d63fa1STony Nguyen 				       sizeof(*xlt1->t), GFP_KERNEL);
223432d63fa1STony Nguyen 		if (!xlt1->t)
223532d63fa1STony Nguyen 			goto err;
223632d63fa1STony Nguyen 
223732d63fa1STony Nguyen 		xlt2->sid = ice_blk_sids[i][ICE_SID_XLT2_OFF];
223832d63fa1STony Nguyen 		xlt2->count = blk_sizes[i].xlt2;
223932d63fa1STony Nguyen 
224032d63fa1STony Nguyen 		xlt2->vsis = devm_kcalloc(ice_hw_to_dev(hw), xlt2->count,
224132d63fa1STony Nguyen 					  sizeof(*xlt2->vsis), GFP_KERNEL);
224232d63fa1STony Nguyen 
224332d63fa1STony Nguyen 		if (!xlt2->vsis)
224432d63fa1STony Nguyen 			goto err;
224532d63fa1STony Nguyen 
224632d63fa1STony Nguyen 		xlt2->vsig_tbl = devm_kcalloc(ice_hw_to_dev(hw), xlt2->count,
224732d63fa1STony Nguyen 					      sizeof(*xlt2->vsig_tbl),
224832d63fa1STony Nguyen 					      GFP_KERNEL);
224932d63fa1STony Nguyen 		if (!xlt2->vsig_tbl)
225032d63fa1STony Nguyen 			goto err;
225132d63fa1STony Nguyen 
225232d63fa1STony Nguyen 		for (j = 0; j < xlt2->count; j++)
225332d63fa1STony Nguyen 			INIT_LIST_HEAD(&xlt2->vsig_tbl[j].prop_lst);
225432d63fa1STony Nguyen 
225532d63fa1STony Nguyen 		xlt2->t = devm_kcalloc(ice_hw_to_dev(hw), xlt2->count,
225632d63fa1STony Nguyen 				       sizeof(*xlt2->t), GFP_KERNEL);
225732d63fa1STony Nguyen 		if (!xlt2->t)
225832d63fa1STony Nguyen 			goto err;
225932d63fa1STony Nguyen 
226032d63fa1STony Nguyen 		prof->sid = ice_blk_sids[i][ICE_SID_PR_OFF];
226132d63fa1STony Nguyen 		prof->count = blk_sizes[i].prof_tcam;
226232d63fa1STony Nguyen 		prof->max_prof_id = blk_sizes[i].prof_id;
226332d63fa1STony Nguyen 		prof->cdid_bits = blk_sizes[i].prof_cdid_bits;
226432d63fa1STony Nguyen 		prof->t = devm_kcalloc(ice_hw_to_dev(hw), prof->count,
226532d63fa1STony Nguyen 				       sizeof(*prof->t), GFP_KERNEL);
226632d63fa1STony Nguyen 
226732d63fa1STony Nguyen 		if (!prof->t)
226832d63fa1STony Nguyen 			goto err;
226932d63fa1STony Nguyen 
227032d63fa1STony Nguyen 		prof_redir->sid = ice_blk_sids[i][ICE_SID_PR_REDIR_OFF];
227132d63fa1STony Nguyen 		prof_redir->count = blk_sizes[i].prof_redir;
227232d63fa1STony Nguyen 		prof_redir->t = devm_kcalloc(ice_hw_to_dev(hw),
227332d63fa1STony Nguyen 					     prof_redir->count,
227432d63fa1STony Nguyen 					     sizeof(*prof_redir->t),
227532d63fa1STony Nguyen 					     GFP_KERNEL);
227632d63fa1STony Nguyen 
227732d63fa1STony Nguyen 		if (!prof_redir->t)
227832d63fa1STony Nguyen 			goto err;
227932d63fa1STony Nguyen 
228032d63fa1STony Nguyen 		es->sid = ice_blk_sids[i][ICE_SID_ES_OFF];
228132d63fa1STony Nguyen 		es->count = blk_sizes[i].es;
228232d63fa1STony Nguyen 		es->fvw = blk_sizes[i].fvw;
228332d63fa1STony Nguyen 		es->t = devm_kcalloc(ice_hw_to_dev(hw),
228432d63fa1STony Nguyen 				     (u32)(es->count * es->fvw),
228532d63fa1STony Nguyen 				     sizeof(*es->t), GFP_KERNEL);
228632d63fa1STony Nguyen 		if (!es->t)
228732d63fa1STony Nguyen 			goto err;
228832d63fa1STony Nguyen 
228932d63fa1STony Nguyen 		es->ref_count = devm_kcalloc(ice_hw_to_dev(hw), es->count,
229032d63fa1STony Nguyen 					     sizeof(*es->ref_count),
229132d63fa1STony Nguyen 					     GFP_KERNEL);
2292bcc46cb8SSurabhi Boob 		if (!es->ref_count)
2293bcc46cb8SSurabhi Boob 			goto err;
229432d63fa1STony Nguyen 
229532d63fa1STony Nguyen 		es->written = devm_kcalloc(ice_hw_to_dev(hw), es->count,
229632d63fa1STony Nguyen 					   sizeof(*es->written), GFP_KERNEL);
2297bcc46cb8SSurabhi Boob 		if (!es->written)
229832d63fa1STony Nguyen 			goto err;
2299b199dddbSQi Zhang 
2300b199dddbSQi Zhang 		es->mask_ena = devm_kcalloc(ice_hw_to_dev(hw), es->count,
2301b199dddbSQi Zhang 					    sizeof(*es->mask_ena), GFP_KERNEL);
2302b199dddbSQi Zhang 		if (!es->mask_ena)
2303b199dddbSQi Zhang 			goto err;
230432d63fa1STony Nguyen 	}
230532d63fa1STony Nguyen 	return 0;
230632d63fa1STony Nguyen 
230732d63fa1STony Nguyen err:
230832d63fa1STony Nguyen 	ice_free_hw_tbls(hw);
2309d54699e2STony Nguyen 	return -ENOMEM;
231032d63fa1STony Nguyen }
231131ad4e4eSTony Nguyen 
231231ad4e4eSTony Nguyen /**
2313451f2c44STony Nguyen  * ice_prof_gen_key - generate profile ID key
2314451f2c44STony Nguyen  * @hw: pointer to the HW struct
2315451f2c44STony Nguyen  * @blk: the block in which to write profile ID to
2316451f2c44STony Nguyen  * @ptg: packet type group (PTG) portion of key
2317451f2c44STony Nguyen  * @vsig: VSIG portion of key
2318451f2c44STony Nguyen  * @cdid: CDID portion of key
2319451f2c44STony Nguyen  * @flags: flag portion of key
2320451f2c44STony Nguyen  * @vl_msk: valid mask
2321451f2c44STony Nguyen  * @dc_msk: don't care mask
2322451f2c44STony Nguyen  * @nm_msk: never match mask
2323451f2c44STony Nguyen  * @key: output of profile ID key
2324451f2c44STony Nguyen  */
23255e24d598STony Nguyen static int
ice_prof_gen_key(struct ice_hw * hw,enum ice_block blk,u8 ptg,u16 vsig,u8 cdid,u16 flags,u8 vl_msk[ICE_TCAM_KEY_VAL_SZ],u8 dc_msk[ICE_TCAM_KEY_VAL_SZ],u8 nm_msk[ICE_TCAM_KEY_VAL_SZ],u8 key[ICE_TCAM_KEY_SZ])2326451f2c44STony Nguyen ice_prof_gen_key(struct ice_hw *hw, enum ice_block blk, u8 ptg, u16 vsig,
2327451f2c44STony Nguyen 		 u8 cdid, u16 flags, u8 vl_msk[ICE_TCAM_KEY_VAL_SZ],
2328451f2c44STony Nguyen 		 u8 dc_msk[ICE_TCAM_KEY_VAL_SZ], u8 nm_msk[ICE_TCAM_KEY_VAL_SZ],
2329451f2c44STony Nguyen 		 u8 key[ICE_TCAM_KEY_SZ])
2330451f2c44STony Nguyen {
2331451f2c44STony Nguyen 	struct ice_prof_id_key inkey;
2332451f2c44STony Nguyen 
2333451f2c44STony Nguyen 	inkey.xlt1 = ptg;
2334451f2c44STony Nguyen 	inkey.xlt2_cdid = cpu_to_le16(vsig);
2335451f2c44STony Nguyen 	inkey.flags = cpu_to_le16(flags);
2336451f2c44STony Nguyen 
2337451f2c44STony Nguyen 	switch (hw->blk[blk].prof.cdid_bits) {
2338451f2c44STony Nguyen 	case 0:
2339451f2c44STony Nguyen 		break;
2340451f2c44STony Nguyen 	case 2:
2341451f2c44STony Nguyen #define ICE_CD_2_M 0xC000U
2342451f2c44STony Nguyen #define ICE_CD_2_S 14
2343451f2c44STony Nguyen 		inkey.xlt2_cdid &= ~cpu_to_le16(ICE_CD_2_M);
2344451f2c44STony Nguyen 		inkey.xlt2_cdid |= cpu_to_le16(BIT(cdid) << ICE_CD_2_S);
2345451f2c44STony Nguyen 		break;
2346451f2c44STony Nguyen 	case 4:
2347451f2c44STony Nguyen #define ICE_CD_4_M 0xF000U
2348451f2c44STony Nguyen #define ICE_CD_4_S 12
2349451f2c44STony Nguyen 		inkey.xlt2_cdid &= ~cpu_to_le16(ICE_CD_4_M);
2350451f2c44STony Nguyen 		inkey.xlt2_cdid |= cpu_to_le16(BIT(cdid) << ICE_CD_4_S);
2351451f2c44STony Nguyen 		break;
2352451f2c44STony Nguyen 	case 8:
2353451f2c44STony Nguyen #define ICE_CD_8_M 0xFF00U
2354451f2c44STony Nguyen #define ICE_CD_8_S 16
2355451f2c44STony Nguyen 		inkey.xlt2_cdid &= ~cpu_to_le16(ICE_CD_8_M);
2356451f2c44STony Nguyen 		inkey.xlt2_cdid |= cpu_to_le16(BIT(cdid) << ICE_CD_8_S);
2357451f2c44STony Nguyen 		break;
2358451f2c44STony Nguyen 	default:
2359451f2c44STony Nguyen 		ice_debug(hw, ICE_DBG_PKG, "Error in profile config\n");
2360451f2c44STony Nguyen 		break;
2361451f2c44STony Nguyen 	}
2362451f2c44STony Nguyen 
2363451f2c44STony Nguyen 	return ice_set_key(key, ICE_TCAM_KEY_SZ, (u8 *)&inkey, vl_msk, dc_msk,
2364451f2c44STony Nguyen 			   nm_msk, 0, ICE_TCAM_KEY_SZ / 2);
2365451f2c44STony Nguyen }
2366451f2c44STony Nguyen 
2367451f2c44STony Nguyen /**
2368451f2c44STony Nguyen  * ice_tcam_write_entry - write TCAM entry
2369451f2c44STony Nguyen  * @hw: pointer to the HW struct
2370451f2c44STony Nguyen  * @blk: the block in which to write profile ID to
2371451f2c44STony Nguyen  * @idx: the entry index to write to
2372451f2c44STony Nguyen  * @prof_id: profile ID
2373451f2c44STony Nguyen  * @ptg: packet type group (PTG) portion of key
2374451f2c44STony Nguyen  * @vsig: VSIG portion of key
2375451f2c44STony Nguyen  * @cdid: CDID portion of key
2376451f2c44STony Nguyen  * @flags: flag portion of key
2377451f2c44STony Nguyen  * @vl_msk: valid mask
2378451f2c44STony Nguyen  * @dc_msk: don't care mask
2379451f2c44STony Nguyen  * @nm_msk: never match mask
2380451f2c44STony Nguyen  */
23815e24d598STony Nguyen static int
ice_tcam_write_entry(struct ice_hw * hw,enum ice_block blk,u16 idx,u8 prof_id,u8 ptg,u16 vsig,u8 cdid,u16 flags,u8 vl_msk[ICE_TCAM_KEY_VAL_SZ],u8 dc_msk[ICE_TCAM_KEY_VAL_SZ],u8 nm_msk[ICE_TCAM_KEY_VAL_SZ])2382451f2c44STony Nguyen ice_tcam_write_entry(struct ice_hw *hw, enum ice_block blk, u16 idx,
2383451f2c44STony Nguyen 		     u8 prof_id, u8 ptg, u16 vsig, u8 cdid, u16 flags,
2384451f2c44STony Nguyen 		     u8 vl_msk[ICE_TCAM_KEY_VAL_SZ],
2385451f2c44STony Nguyen 		     u8 dc_msk[ICE_TCAM_KEY_VAL_SZ],
2386451f2c44STony Nguyen 		     u8 nm_msk[ICE_TCAM_KEY_VAL_SZ])
2387451f2c44STony Nguyen {
2388451f2c44STony Nguyen 	struct ice_prof_tcam_entry;
23895e24d598STony Nguyen 	int status;
2390451f2c44STony Nguyen 
2391451f2c44STony Nguyen 	status = ice_prof_gen_key(hw, blk, ptg, vsig, cdid, flags, vl_msk,
2392451f2c44STony Nguyen 				  dc_msk, nm_msk, hw->blk[blk].prof.t[idx].key);
2393451f2c44STony Nguyen 	if (!status) {
2394451f2c44STony Nguyen 		hw->blk[blk].prof.t[idx].addr = cpu_to_le16(idx);
2395451f2c44STony Nguyen 		hw->blk[blk].prof.t[idx].prof_id = prof_id;
2396451f2c44STony Nguyen 	}
2397451f2c44STony Nguyen 
2398451f2c44STony Nguyen 	return status;
2399451f2c44STony Nguyen }
2400451f2c44STony Nguyen 
2401451f2c44STony Nguyen /**
2402451f2c44STony Nguyen  * ice_vsig_get_ref - returns number of VSIs belong to a VSIG
2403451f2c44STony Nguyen  * @hw: pointer to the hardware structure
2404451f2c44STony Nguyen  * @blk: HW block
2405451f2c44STony Nguyen  * @vsig: VSIG to query
2406451f2c44STony Nguyen  * @refs: pointer to variable to receive the reference count
2407451f2c44STony Nguyen  */
24085e24d598STony Nguyen static int
ice_vsig_get_ref(struct ice_hw * hw,enum ice_block blk,u16 vsig,u16 * refs)2409451f2c44STony Nguyen ice_vsig_get_ref(struct ice_hw *hw, enum ice_block blk, u16 vsig, u16 *refs)
2410451f2c44STony Nguyen {
2411451f2c44STony Nguyen 	u16 idx = vsig & ICE_VSIG_IDX_M;
2412451f2c44STony Nguyen 	struct ice_vsig_vsi *ptr;
2413451f2c44STony Nguyen 
2414451f2c44STony Nguyen 	*refs = 0;
2415451f2c44STony Nguyen 
2416451f2c44STony Nguyen 	if (!hw->blk[blk].xlt2.vsig_tbl[idx].in_use)
2417d54699e2STony Nguyen 		return -ENOENT;
2418451f2c44STony Nguyen 
2419451f2c44STony Nguyen 	ptr = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
2420451f2c44STony Nguyen 	while (ptr) {
2421451f2c44STony Nguyen 		(*refs)++;
2422451f2c44STony Nguyen 		ptr = ptr->next_vsi;
2423451f2c44STony Nguyen 	}
2424451f2c44STony Nguyen 
2425451f2c44STony Nguyen 	return 0;
2426451f2c44STony Nguyen }
2427451f2c44STony Nguyen 
2428451f2c44STony Nguyen /**
2429451f2c44STony Nguyen  * ice_has_prof_vsig - check to see if VSIG has a specific profile
2430451f2c44STony Nguyen  * @hw: pointer to the hardware structure
2431451f2c44STony Nguyen  * @blk: HW block
2432451f2c44STony Nguyen  * @vsig: VSIG to check against
2433451f2c44STony Nguyen  * @hdl: profile handle
2434451f2c44STony Nguyen  */
2435451f2c44STony Nguyen static bool
ice_has_prof_vsig(struct ice_hw * hw,enum ice_block blk,u16 vsig,u64 hdl)2436451f2c44STony Nguyen ice_has_prof_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig, u64 hdl)
2437451f2c44STony Nguyen {
2438451f2c44STony Nguyen 	u16 idx = vsig & ICE_VSIG_IDX_M;
2439451f2c44STony Nguyen 	struct ice_vsig_prof *ent;
2440451f2c44STony Nguyen 
2441451f2c44STony Nguyen 	list_for_each_entry(ent, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
2442451f2c44STony Nguyen 			    list)
2443451f2c44STony Nguyen 		if (ent->profile_cookie == hdl)
2444451f2c44STony Nguyen 			return true;
2445451f2c44STony Nguyen 
24469228d8b2SJacob Keller 	ice_debug(hw, ICE_DBG_INIT, "Characteristic list for VSI group %d not found.\n",
2447451f2c44STony Nguyen 		  vsig);
2448451f2c44STony Nguyen 	return false;
2449451f2c44STony Nguyen }
2450451f2c44STony Nguyen 
2451451f2c44STony Nguyen /**
245243dbfc7bSTony Nguyen  * ice_prof_bld_es - build profile ID extraction sequence changes
245343dbfc7bSTony Nguyen  * @hw: pointer to the HW struct
245443dbfc7bSTony Nguyen  * @blk: hardware block
245543dbfc7bSTony Nguyen  * @bld: the update package buffer build to add to
245643dbfc7bSTony Nguyen  * @chgs: the list of changes to make in hardware
245743dbfc7bSTony Nguyen  */
24585e24d598STony Nguyen static int
ice_prof_bld_es(struct ice_hw * hw,enum ice_block blk,struct ice_buf_build * bld,struct list_head * chgs)245943dbfc7bSTony Nguyen ice_prof_bld_es(struct ice_hw *hw, enum ice_block blk,
246043dbfc7bSTony Nguyen 		struct ice_buf_build *bld, struct list_head *chgs)
246143dbfc7bSTony Nguyen {
246243dbfc7bSTony Nguyen 	u16 vec_size = hw->blk[blk].es.fvw * sizeof(struct ice_fv_word);
246343dbfc7bSTony Nguyen 	struct ice_chs_chg *tmp;
246443dbfc7bSTony Nguyen 
246543dbfc7bSTony Nguyen 	list_for_each_entry(tmp, chgs, list_entry)
246643dbfc7bSTony Nguyen 		if (tmp->type == ICE_PTG_ES_ADD && tmp->add_prof) {
246743dbfc7bSTony Nguyen 			u16 off = tmp->prof_id * hw->blk[blk].es.fvw;
246843dbfc7bSTony Nguyen 			struct ice_pkg_es *p;
246943dbfc7bSTony Nguyen 			u32 id;
247043dbfc7bSTony Nguyen 
247143dbfc7bSTony Nguyen 			id = ice_sect_id(blk, ICE_VEC_TBL);
247266486d89SBruce Allan 			p = ice_pkg_buf_alloc_section(bld, id,
247366486d89SBruce Allan 						      struct_size(p, es, 1) +
247443dbfc7bSTony Nguyen 						      vec_size -
247543dbfc7bSTony Nguyen 						      sizeof(p->es[0]));
247643dbfc7bSTony Nguyen 
247743dbfc7bSTony Nguyen 			if (!p)
2478d54699e2STony Nguyen 				return -ENOSPC;
247943dbfc7bSTony Nguyen 
248043dbfc7bSTony Nguyen 			p->count = cpu_to_le16(1);
248143dbfc7bSTony Nguyen 			p->offset = cpu_to_le16(tmp->prof_id);
248243dbfc7bSTony Nguyen 
248343dbfc7bSTony Nguyen 			memcpy(p->es, &hw->blk[blk].es.t[off], vec_size);
248443dbfc7bSTony Nguyen 		}
248543dbfc7bSTony Nguyen 
248643dbfc7bSTony Nguyen 	return 0;
248743dbfc7bSTony Nguyen }
248843dbfc7bSTony Nguyen 
248943dbfc7bSTony Nguyen /**
249043dbfc7bSTony Nguyen  * ice_prof_bld_tcam - build profile ID TCAM changes
249143dbfc7bSTony Nguyen  * @hw: pointer to the HW struct
249243dbfc7bSTony Nguyen  * @blk: hardware block
249343dbfc7bSTony Nguyen  * @bld: the update package buffer build to add to
249443dbfc7bSTony Nguyen  * @chgs: the list of changes to make in hardware
249543dbfc7bSTony Nguyen  */
24965e24d598STony Nguyen static int
ice_prof_bld_tcam(struct ice_hw * hw,enum ice_block blk,struct ice_buf_build * bld,struct list_head * chgs)249743dbfc7bSTony Nguyen ice_prof_bld_tcam(struct ice_hw *hw, enum ice_block blk,
249843dbfc7bSTony Nguyen 		  struct ice_buf_build *bld, struct list_head *chgs)
249943dbfc7bSTony Nguyen {
250043dbfc7bSTony Nguyen 	struct ice_chs_chg *tmp;
250143dbfc7bSTony Nguyen 
250243dbfc7bSTony Nguyen 	list_for_each_entry(tmp, chgs, list_entry)
250343dbfc7bSTony Nguyen 		if (tmp->type == ICE_TCAM_ADD && tmp->add_tcam_idx) {
250443dbfc7bSTony Nguyen 			struct ice_prof_id_section *p;
250543dbfc7bSTony Nguyen 			u32 id;
250643dbfc7bSTony Nguyen 
250743dbfc7bSTony Nguyen 			id = ice_sect_id(blk, ICE_PROF_TCAM);
250866486d89SBruce Allan 			p = ice_pkg_buf_alloc_section(bld, id,
250966486d89SBruce Allan 						      struct_size(p, entry, 1));
251043dbfc7bSTony Nguyen 
251143dbfc7bSTony Nguyen 			if (!p)
2512d54699e2STony Nguyen 				return -ENOSPC;
251343dbfc7bSTony Nguyen 
251443dbfc7bSTony Nguyen 			p->count = cpu_to_le16(1);
251543dbfc7bSTony Nguyen 			p->entry[0].addr = cpu_to_le16(tmp->tcam_idx);
251643dbfc7bSTony Nguyen 			p->entry[0].prof_id = tmp->prof_id;
251743dbfc7bSTony Nguyen 
251843dbfc7bSTony Nguyen 			memcpy(p->entry[0].key,
251943dbfc7bSTony Nguyen 			       &hw->blk[blk].prof.t[tmp->tcam_idx].key,
252043dbfc7bSTony Nguyen 			       sizeof(hw->blk[blk].prof.t->key));
252143dbfc7bSTony Nguyen 		}
252243dbfc7bSTony Nguyen 
252343dbfc7bSTony Nguyen 	return 0;
252443dbfc7bSTony Nguyen }
252543dbfc7bSTony Nguyen 
252643dbfc7bSTony Nguyen /**
252743dbfc7bSTony Nguyen  * ice_prof_bld_xlt1 - build XLT1 changes
252843dbfc7bSTony Nguyen  * @blk: hardware block
252943dbfc7bSTony Nguyen  * @bld: the update package buffer build to add to
253043dbfc7bSTony Nguyen  * @chgs: the list of changes to make in hardware
253143dbfc7bSTony Nguyen  */
25325e24d598STony Nguyen static int
ice_prof_bld_xlt1(enum ice_block blk,struct ice_buf_build * bld,struct list_head * chgs)253343dbfc7bSTony Nguyen ice_prof_bld_xlt1(enum ice_block blk, struct ice_buf_build *bld,
253443dbfc7bSTony Nguyen 		  struct list_head *chgs)
253543dbfc7bSTony Nguyen {
253643dbfc7bSTony Nguyen 	struct ice_chs_chg *tmp;
253743dbfc7bSTony Nguyen 
253843dbfc7bSTony Nguyen 	list_for_each_entry(tmp, chgs, list_entry)
253943dbfc7bSTony Nguyen 		if (tmp->type == ICE_PTG_ES_ADD && tmp->add_ptg) {
254043dbfc7bSTony Nguyen 			struct ice_xlt1_section *p;
254143dbfc7bSTony Nguyen 			u32 id;
254243dbfc7bSTony Nguyen 
254343dbfc7bSTony Nguyen 			id = ice_sect_id(blk, ICE_XLT1);
254466486d89SBruce Allan 			p = ice_pkg_buf_alloc_section(bld, id,
254566486d89SBruce Allan 						      struct_size(p, value, 1));
254643dbfc7bSTony Nguyen 
254743dbfc7bSTony Nguyen 			if (!p)
2548d54699e2STony Nguyen 				return -ENOSPC;
254943dbfc7bSTony Nguyen 
255043dbfc7bSTony Nguyen 			p->count = cpu_to_le16(1);
255143dbfc7bSTony Nguyen 			p->offset = cpu_to_le16(tmp->ptype);
255243dbfc7bSTony Nguyen 			p->value[0] = tmp->ptg;
255343dbfc7bSTony Nguyen 		}
255443dbfc7bSTony Nguyen 
255543dbfc7bSTony Nguyen 	return 0;
255643dbfc7bSTony Nguyen }
255743dbfc7bSTony Nguyen 
255843dbfc7bSTony Nguyen /**
255943dbfc7bSTony Nguyen  * ice_prof_bld_xlt2 - build XLT2 changes
256043dbfc7bSTony Nguyen  * @blk: hardware block
256143dbfc7bSTony Nguyen  * @bld: the update package buffer build to add to
256243dbfc7bSTony Nguyen  * @chgs: the list of changes to make in hardware
256343dbfc7bSTony Nguyen  */
25645e24d598STony Nguyen static int
ice_prof_bld_xlt2(enum ice_block blk,struct ice_buf_build * bld,struct list_head * chgs)256543dbfc7bSTony Nguyen ice_prof_bld_xlt2(enum ice_block blk, struct ice_buf_build *bld,
256643dbfc7bSTony Nguyen 		  struct list_head *chgs)
256743dbfc7bSTony Nguyen {
256843dbfc7bSTony Nguyen 	struct ice_chs_chg *tmp;
256943dbfc7bSTony Nguyen 
257043dbfc7bSTony Nguyen 	list_for_each_entry(tmp, chgs, list_entry) {
257143dbfc7bSTony Nguyen 		struct ice_xlt2_section *p;
257243dbfc7bSTony Nguyen 		u32 id;
257343dbfc7bSTony Nguyen 
257443dbfc7bSTony Nguyen 		switch (tmp->type) {
257543dbfc7bSTony Nguyen 		case ICE_VSIG_ADD:
257643dbfc7bSTony Nguyen 		case ICE_VSI_MOVE:
257743dbfc7bSTony Nguyen 		case ICE_VSIG_REM:
257843dbfc7bSTony Nguyen 			id = ice_sect_id(blk, ICE_XLT2);
257966486d89SBruce Allan 			p = ice_pkg_buf_alloc_section(bld, id,
258066486d89SBruce Allan 						      struct_size(p, value, 1));
258143dbfc7bSTony Nguyen 
258243dbfc7bSTony Nguyen 			if (!p)
2583d54699e2STony Nguyen 				return -ENOSPC;
258443dbfc7bSTony Nguyen 
258543dbfc7bSTony Nguyen 			p->count = cpu_to_le16(1);
258643dbfc7bSTony Nguyen 			p->offset = cpu_to_le16(tmp->vsi);
258743dbfc7bSTony Nguyen 			p->value[0] = cpu_to_le16(tmp->vsig);
258843dbfc7bSTony Nguyen 			break;
258943dbfc7bSTony Nguyen 		default:
259043dbfc7bSTony Nguyen 			break;
259143dbfc7bSTony Nguyen 		}
259243dbfc7bSTony Nguyen 	}
259343dbfc7bSTony Nguyen 
259443dbfc7bSTony Nguyen 	return 0;
259543dbfc7bSTony Nguyen }
259643dbfc7bSTony Nguyen 
259743dbfc7bSTony Nguyen /**
259843dbfc7bSTony Nguyen  * ice_upd_prof_hw - update hardware using the change list
259943dbfc7bSTony Nguyen  * @hw: pointer to the HW struct
260043dbfc7bSTony Nguyen  * @blk: hardware block
260143dbfc7bSTony Nguyen  * @chgs: the list of changes to make in hardware
260243dbfc7bSTony Nguyen  */
26035e24d598STony Nguyen static int
ice_upd_prof_hw(struct ice_hw * hw,enum ice_block blk,struct list_head * chgs)260443dbfc7bSTony Nguyen ice_upd_prof_hw(struct ice_hw *hw, enum ice_block blk,
260543dbfc7bSTony Nguyen 		struct list_head *chgs)
260643dbfc7bSTony Nguyen {
260743dbfc7bSTony Nguyen 	struct ice_buf_build *b;
260843dbfc7bSTony Nguyen 	struct ice_chs_chg *tmp;
260943dbfc7bSTony Nguyen 	u16 pkg_sects;
261043dbfc7bSTony Nguyen 	u16 xlt1 = 0;
261143dbfc7bSTony Nguyen 	u16 xlt2 = 0;
261243dbfc7bSTony Nguyen 	u16 tcam = 0;
261343dbfc7bSTony Nguyen 	u16 es = 0;
26145518ac2aSTony Nguyen 	int status;
261543dbfc7bSTony Nguyen 	u16 sects;
261643dbfc7bSTony Nguyen 
261743dbfc7bSTony Nguyen 	/* count number of sections we need */
261843dbfc7bSTony Nguyen 	list_for_each_entry(tmp, chgs, list_entry) {
261943dbfc7bSTony Nguyen 		switch (tmp->type) {
262043dbfc7bSTony Nguyen 		case ICE_PTG_ES_ADD:
262143dbfc7bSTony Nguyen 			if (tmp->add_ptg)
262243dbfc7bSTony Nguyen 				xlt1++;
262343dbfc7bSTony Nguyen 			if (tmp->add_prof)
262443dbfc7bSTony Nguyen 				es++;
262543dbfc7bSTony Nguyen 			break;
262643dbfc7bSTony Nguyen 		case ICE_TCAM_ADD:
262743dbfc7bSTony Nguyen 			tcam++;
262843dbfc7bSTony Nguyen 			break;
262943dbfc7bSTony Nguyen 		case ICE_VSIG_ADD:
263043dbfc7bSTony Nguyen 		case ICE_VSI_MOVE:
263143dbfc7bSTony Nguyen 		case ICE_VSIG_REM:
263243dbfc7bSTony Nguyen 			xlt2++;
263343dbfc7bSTony Nguyen 			break;
263443dbfc7bSTony Nguyen 		default:
263543dbfc7bSTony Nguyen 			break;
263643dbfc7bSTony Nguyen 		}
263743dbfc7bSTony Nguyen 	}
263843dbfc7bSTony Nguyen 	sects = xlt1 + xlt2 + tcam + es;
263943dbfc7bSTony Nguyen 
264043dbfc7bSTony Nguyen 	if (!sects)
264143dbfc7bSTony Nguyen 		return 0;
264243dbfc7bSTony Nguyen 
264343dbfc7bSTony Nguyen 	/* Build update package buffer */
264443dbfc7bSTony Nguyen 	b = ice_pkg_buf_alloc(hw);
264543dbfc7bSTony Nguyen 	if (!b)
2646d54699e2STony Nguyen 		return -ENOMEM;
264743dbfc7bSTony Nguyen 
264843dbfc7bSTony Nguyen 	status = ice_pkg_buf_reserve_section(b, sects);
264943dbfc7bSTony Nguyen 	if (status)
265043dbfc7bSTony Nguyen 		goto error_tmp;
265143dbfc7bSTony Nguyen 
265243dbfc7bSTony Nguyen 	/* Preserve order of table update: ES, TCAM, PTG, VSIG */
265343dbfc7bSTony Nguyen 	if (es) {
265443dbfc7bSTony Nguyen 		status = ice_prof_bld_es(hw, blk, b, chgs);
265543dbfc7bSTony Nguyen 		if (status)
265643dbfc7bSTony Nguyen 			goto error_tmp;
265743dbfc7bSTony Nguyen 	}
265843dbfc7bSTony Nguyen 
265943dbfc7bSTony Nguyen 	if (tcam) {
266043dbfc7bSTony Nguyen 		status = ice_prof_bld_tcam(hw, blk, b, chgs);
266143dbfc7bSTony Nguyen 		if (status)
266243dbfc7bSTony Nguyen 			goto error_tmp;
266343dbfc7bSTony Nguyen 	}
266443dbfc7bSTony Nguyen 
266543dbfc7bSTony Nguyen 	if (xlt1) {
266643dbfc7bSTony Nguyen 		status = ice_prof_bld_xlt1(blk, b, chgs);
266743dbfc7bSTony Nguyen 		if (status)
266843dbfc7bSTony Nguyen 			goto error_tmp;
266943dbfc7bSTony Nguyen 	}
267043dbfc7bSTony Nguyen 
267143dbfc7bSTony Nguyen 	if (xlt2) {
267243dbfc7bSTony Nguyen 		status = ice_prof_bld_xlt2(blk, b, chgs);
267343dbfc7bSTony Nguyen 		if (status)
267443dbfc7bSTony Nguyen 			goto error_tmp;
267543dbfc7bSTony Nguyen 	}
267643dbfc7bSTony Nguyen 
267743dbfc7bSTony Nguyen 	/* After package buffer build check if the section count in buffer is
267843dbfc7bSTony Nguyen 	 * non-zero and matches the number of sections detected for package
267943dbfc7bSTony Nguyen 	 * update.
268043dbfc7bSTony Nguyen 	 */
268143dbfc7bSTony Nguyen 	pkg_sects = ice_pkg_buf_get_active_sections(b);
268243dbfc7bSTony Nguyen 	if (!pkg_sects || pkg_sects != sects) {
2683d54699e2STony Nguyen 		status = -EINVAL;
268443dbfc7bSTony Nguyen 		goto error_tmp;
268543dbfc7bSTony Nguyen 	}
268643dbfc7bSTony Nguyen 
268743dbfc7bSTony Nguyen 	/* update package */
268843dbfc7bSTony Nguyen 	status = ice_update_pkg(hw, ice_pkg_buf(b), 1);
2689d54699e2STony Nguyen 	if (status == -EIO)
269043dbfc7bSTony Nguyen 		ice_debug(hw, ICE_DBG_INIT, "Unable to update HW profile\n");
269143dbfc7bSTony Nguyen 
269243dbfc7bSTony Nguyen error_tmp:
269343dbfc7bSTony Nguyen 	ice_pkg_buf_free(hw, b);
269443dbfc7bSTony Nguyen 	return status;
269543dbfc7bSTony Nguyen }
269643dbfc7bSTony Nguyen 
269743dbfc7bSTony Nguyen /**
2698148beb61SHenry Tieman  * ice_update_fd_mask - set Flow Director Field Vector mask for a profile
2699148beb61SHenry Tieman  * @hw: pointer to the HW struct
2700148beb61SHenry Tieman  * @prof_id: profile ID
2701148beb61SHenry Tieman  * @mask_sel: mask select
2702148beb61SHenry Tieman  *
2703148beb61SHenry Tieman  * This function enable any of the masks selected by the mask select parameter
2704148beb61SHenry Tieman  * for the profile specified.
2705148beb61SHenry Tieman  */
ice_update_fd_mask(struct ice_hw * hw,u16 prof_id,u32 mask_sel)2706148beb61SHenry Tieman static void ice_update_fd_mask(struct ice_hw *hw, u16 prof_id, u32 mask_sel)
2707148beb61SHenry Tieman {
2708148beb61SHenry Tieman 	wr32(hw, GLQF_FDMASK_SEL(prof_id), mask_sel);
2709148beb61SHenry Tieman 
2710148beb61SHenry Tieman 	ice_debug(hw, ICE_DBG_INIT, "fd mask(%d): %x = %x\n", prof_id,
2711148beb61SHenry Tieman 		  GLQF_FDMASK_SEL(prof_id), mask_sel);
2712148beb61SHenry Tieman }
2713148beb61SHenry Tieman 
2714148beb61SHenry Tieman struct ice_fd_src_dst_pair {
2715148beb61SHenry Tieman 	u8 prot_id;
2716148beb61SHenry Tieman 	u8 count;
2717148beb61SHenry Tieman 	u16 off;
2718148beb61SHenry Tieman };
2719148beb61SHenry Tieman 
2720148beb61SHenry Tieman static const struct ice_fd_src_dst_pair ice_fd_pairs[] = {
2721148beb61SHenry Tieman 	/* These are defined in pairs */
2722148beb61SHenry Tieman 	{ ICE_PROT_IPV4_OF_OR_S, 2, 12 },
2723148beb61SHenry Tieman 	{ ICE_PROT_IPV4_OF_OR_S, 2, 16 },
2724148beb61SHenry Tieman 
2725148beb61SHenry Tieman 	{ ICE_PROT_IPV4_IL, 2, 12 },
2726148beb61SHenry Tieman 	{ ICE_PROT_IPV4_IL, 2, 16 },
2727148beb61SHenry Tieman 
2728165d80d6SHenry Tieman 	{ ICE_PROT_IPV6_OF_OR_S, 8, 8 },
2729165d80d6SHenry Tieman 	{ ICE_PROT_IPV6_OF_OR_S, 8, 24 },
2730165d80d6SHenry Tieman 
2731165d80d6SHenry Tieman 	{ ICE_PROT_IPV6_IL, 8, 8 },
2732165d80d6SHenry Tieman 	{ ICE_PROT_IPV6_IL, 8, 24 },
2733165d80d6SHenry Tieman 
2734148beb61SHenry Tieman 	{ ICE_PROT_TCP_IL, 1, 0 },
2735148beb61SHenry Tieman 	{ ICE_PROT_TCP_IL, 1, 2 },
2736148beb61SHenry Tieman 
2737148beb61SHenry Tieman 	{ ICE_PROT_UDP_OF, 1, 0 },
2738148beb61SHenry Tieman 	{ ICE_PROT_UDP_OF, 1, 2 },
2739148beb61SHenry Tieman 
2740148beb61SHenry Tieman 	{ ICE_PROT_UDP_IL_OR_S, 1, 0 },
2741148beb61SHenry Tieman 	{ ICE_PROT_UDP_IL_OR_S, 1, 2 },
2742148beb61SHenry Tieman 
2743148beb61SHenry Tieman 	{ ICE_PROT_SCTP_IL, 1, 0 },
2744148beb61SHenry Tieman 	{ ICE_PROT_SCTP_IL, 1, 2 }
2745148beb61SHenry Tieman };
2746148beb61SHenry Tieman 
2747148beb61SHenry Tieman #define ICE_FD_SRC_DST_PAIR_COUNT	ARRAY_SIZE(ice_fd_pairs)
2748148beb61SHenry Tieman 
2749148beb61SHenry Tieman /**
2750148beb61SHenry Tieman  * ice_update_fd_swap - set register appropriately for a FD FV extraction
2751148beb61SHenry Tieman  * @hw: pointer to the HW struct
2752148beb61SHenry Tieman  * @prof_id: profile ID
2753148beb61SHenry Tieman  * @es: extraction sequence (length of array is determined by the block)
2754148beb61SHenry Tieman  */
27555e24d598STony Nguyen static int
ice_update_fd_swap(struct ice_hw * hw,u16 prof_id,struct ice_fv_word * es)2756148beb61SHenry Tieman ice_update_fd_swap(struct ice_hw *hw, u16 prof_id, struct ice_fv_word *es)
2757148beb61SHenry Tieman {
2758148beb61SHenry Tieman 	DECLARE_BITMAP(pair_list, ICE_FD_SRC_DST_PAIR_COUNT);
2759148beb61SHenry Tieman 	u8 pair_start[ICE_FD_SRC_DST_PAIR_COUNT] = { 0 };
2760148beb61SHenry Tieman #define ICE_FD_FV_NOT_FOUND (-2)
2761148beb61SHenry Tieman 	s8 first_free = ICE_FD_FV_NOT_FOUND;
2762148beb61SHenry Tieman 	u8 used[ICE_MAX_FV_WORDS] = { 0 };
2763148beb61SHenry Tieman 	s8 orig_free, si;
2764148beb61SHenry Tieman 	u32 mask_sel = 0;
2765148beb61SHenry Tieman 	u8 i, j, k;
2766148beb61SHenry Tieman 
2767148beb61SHenry Tieman 	bitmap_zero(pair_list, ICE_FD_SRC_DST_PAIR_COUNT);
2768148beb61SHenry Tieman 
2769148beb61SHenry Tieman 	/* This code assumes that the Flow Director field vectors are assigned
2770148beb61SHenry Tieman 	 * from the end of the FV indexes working towards the zero index, that
2771148beb61SHenry Tieman 	 * only complete fields will be included and will be consecutive, and
2772148beb61SHenry Tieman 	 * that there are no gaps between valid indexes.
2773148beb61SHenry Tieman 	 */
2774148beb61SHenry Tieman 
2775148beb61SHenry Tieman 	/* Determine swap fields present */
2776148beb61SHenry Tieman 	for (i = 0; i < hw->blk[ICE_BLK_FD].es.fvw; i++) {
2777148beb61SHenry Tieman 		/* Find the first free entry, assuming right to left population.
2778148beb61SHenry Tieman 		 * This is where we can start adding additional pairs if needed.
2779148beb61SHenry Tieman 		 */
2780148beb61SHenry Tieman 		if (first_free == ICE_FD_FV_NOT_FOUND && es[i].prot_id !=
2781148beb61SHenry Tieman 		    ICE_PROT_INVALID)
2782148beb61SHenry Tieman 			first_free = i - 1;
2783148beb61SHenry Tieman 
2784148beb61SHenry Tieman 		for (j = 0; j < ICE_FD_SRC_DST_PAIR_COUNT; j++)
2785148beb61SHenry Tieman 			if (es[i].prot_id == ice_fd_pairs[j].prot_id &&
2786148beb61SHenry Tieman 			    es[i].off == ice_fd_pairs[j].off) {
2787e75ed29dSChristophe JAILLET 				__set_bit(j, pair_list);
2788148beb61SHenry Tieman 				pair_start[j] = i;
2789148beb61SHenry Tieman 			}
2790148beb61SHenry Tieman 	}
2791148beb61SHenry Tieman 
2792148beb61SHenry Tieman 	orig_free = first_free;
2793148beb61SHenry Tieman 
2794148beb61SHenry Tieman 	/* determine missing swap fields that need to be added */
2795148beb61SHenry Tieman 	for (i = 0; i < ICE_FD_SRC_DST_PAIR_COUNT; i += 2) {
2796148beb61SHenry Tieman 		u8 bit1 = test_bit(i + 1, pair_list);
2797148beb61SHenry Tieman 		u8 bit0 = test_bit(i, pair_list);
2798148beb61SHenry Tieman 
2799148beb61SHenry Tieman 		if (bit0 ^ bit1) {
2800148beb61SHenry Tieman 			u8 index;
2801148beb61SHenry Tieman 
2802148beb61SHenry Tieman 			/* add the appropriate 'paired' entry */
2803148beb61SHenry Tieman 			if (!bit0)
2804148beb61SHenry Tieman 				index = i;
2805148beb61SHenry Tieman 			else
2806148beb61SHenry Tieman 				index = i + 1;
2807148beb61SHenry Tieman 
2808148beb61SHenry Tieman 			/* check for room */
2809148beb61SHenry Tieman 			if (first_free + 1 < (s8)ice_fd_pairs[index].count)
2810d54699e2STony Nguyen 				return -ENOSPC;
2811148beb61SHenry Tieman 
2812148beb61SHenry Tieman 			/* place in extraction sequence */
2813148beb61SHenry Tieman 			for (k = 0; k < ice_fd_pairs[index].count; k++) {
2814148beb61SHenry Tieman 				es[first_free - k].prot_id =
2815148beb61SHenry Tieman 					ice_fd_pairs[index].prot_id;
2816148beb61SHenry Tieman 				es[first_free - k].off =
2817148beb61SHenry Tieman 					ice_fd_pairs[index].off + (k * 2);
2818148beb61SHenry Tieman 
2819148beb61SHenry Tieman 				if (k > first_free)
2820d54699e2STony Nguyen 					return -EIO;
2821148beb61SHenry Tieman 
2822148beb61SHenry Tieman 				/* keep track of non-relevant fields */
2823148beb61SHenry Tieman 				mask_sel |= BIT(first_free - k);
2824148beb61SHenry Tieman 			}
2825148beb61SHenry Tieman 
2826148beb61SHenry Tieman 			pair_start[index] = first_free;
2827148beb61SHenry Tieman 			first_free -= ice_fd_pairs[index].count;
2828148beb61SHenry Tieman 		}
2829148beb61SHenry Tieman 	}
2830148beb61SHenry Tieman 
2831148beb61SHenry Tieman 	/* fill in the swap array */
2832148beb61SHenry Tieman 	si = hw->blk[ICE_BLK_FD].es.fvw - 1;
2833148beb61SHenry Tieman 	while (si >= 0) {
2834148beb61SHenry Tieman 		u8 indexes_used = 1;
2835148beb61SHenry Tieman 
2836148beb61SHenry Tieman 		/* assume flat at this index */
2837148beb61SHenry Tieman #define ICE_SWAP_VALID	0x80
2838148beb61SHenry Tieman 		used[si] = si | ICE_SWAP_VALID;
2839148beb61SHenry Tieman 
2840148beb61SHenry Tieman 		if (orig_free == ICE_FD_FV_NOT_FOUND || si <= orig_free) {
2841148beb61SHenry Tieman 			si -= indexes_used;
2842148beb61SHenry Tieman 			continue;
2843148beb61SHenry Tieman 		}
2844148beb61SHenry Tieman 
2845148beb61SHenry Tieman 		/* check for a swap location */
2846148beb61SHenry Tieman 		for (j = 0; j < ICE_FD_SRC_DST_PAIR_COUNT; j++)
2847148beb61SHenry Tieman 			if (es[si].prot_id == ice_fd_pairs[j].prot_id &&
2848148beb61SHenry Tieman 			    es[si].off == ice_fd_pairs[j].off) {
2849148beb61SHenry Tieman 				u8 idx;
2850148beb61SHenry Tieman 
2851148beb61SHenry Tieman 				/* determine the appropriate matching field */
2852148beb61SHenry Tieman 				idx = j + ((j % 2) ? -1 : 1);
2853148beb61SHenry Tieman 
2854148beb61SHenry Tieman 				indexes_used = ice_fd_pairs[idx].count;
2855148beb61SHenry Tieman 				for (k = 0; k < indexes_used; k++) {
2856148beb61SHenry Tieman 					used[si - k] = (pair_start[idx] - k) |
2857148beb61SHenry Tieman 						ICE_SWAP_VALID;
2858148beb61SHenry Tieman 				}
2859148beb61SHenry Tieman 
2860148beb61SHenry Tieman 				break;
2861148beb61SHenry Tieman 			}
2862148beb61SHenry Tieman 
2863148beb61SHenry Tieman 		si -= indexes_used;
2864148beb61SHenry Tieman 	}
2865148beb61SHenry Tieman 
2866148beb61SHenry Tieman 	/* for each set of 4 swap and 4 inset indexes, write the appropriate
2867148beb61SHenry Tieman 	 * register
2868148beb61SHenry Tieman 	 */
2869148beb61SHenry Tieman 	for (j = 0; j < hw->blk[ICE_BLK_FD].es.fvw / 4; j++) {
2870148beb61SHenry Tieman 		u32 raw_swap = 0;
2871148beb61SHenry Tieman 		u32 raw_in = 0;
2872148beb61SHenry Tieman 
2873148beb61SHenry Tieman 		for (k = 0; k < 4; k++) {
2874148beb61SHenry Tieman 			u8 idx;
2875148beb61SHenry Tieman 
2876148beb61SHenry Tieman 			idx = (j * 4) + k;
2877148beb61SHenry Tieman 			if (used[idx] && !(mask_sel & BIT(idx))) {
2878148beb61SHenry Tieman 				raw_swap |= used[idx] << (k * BITS_PER_BYTE);
2879148beb61SHenry Tieman #define ICE_INSET_DFLT 0x9f
2880148beb61SHenry Tieman 				raw_in |= ICE_INSET_DFLT << (k * BITS_PER_BYTE);
2881148beb61SHenry Tieman 			}
2882148beb61SHenry Tieman 		}
2883148beb61SHenry Tieman 
2884148beb61SHenry Tieman 		/* write the appropriate swap register set */
2885148beb61SHenry Tieman 		wr32(hw, GLQF_FDSWAP(prof_id, j), raw_swap);
2886148beb61SHenry Tieman 
2887148beb61SHenry Tieman 		ice_debug(hw, ICE_DBG_INIT, "swap wr(%d, %d): %x = %08x\n",
2888148beb61SHenry Tieman 			  prof_id, j, GLQF_FDSWAP(prof_id, j), raw_swap);
2889148beb61SHenry Tieman 
2890148beb61SHenry Tieman 		/* write the appropriate inset register set */
2891148beb61SHenry Tieman 		wr32(hw, GLQF_FDINSET(prof_id, j), raw_in);
2892148beb61SHenry Tieman 
2893148beb61SHenry Tieman 		ice_debug(hw, ICE_DBG_INIT, "inset wr(%d, %d): %x = %08x\n",
2894148beb61SHenry Tieman 			  prof_id, j, GLQF_FDINSET(prof_id, j), raw_in);
2895148beb61SHenry Tieman 	}
2896148beb61SHenry Tieman 
2897148beb61SHenry Tieman 	/* initially clear the mask select for this profile */
2898148beb61SHenry Tieman 	ice_update_fd_mask(hw, prof_id, 0);
2899148beb61SHenry Tieman 
2900148beb61SHenry Tieman 	return 0;
2901148beb61SHenry Tieman }
2902148beb61SHenry Tieman 
2903cbad5db8SQi Zhang /* The entries here needs to match the order of enum ice_ptype_attrib */
2904cbad5db8SQi Zhang static const struct ice_ptype_attrib_info ice_ptype_attributes[] = {
2905cbad5db8SQi Zhang 	{ ICE_GTP_PDU_EH,	ICE_GTP_PDU_FLAG_MASK },
2906cbad5db8SQi Zhang 	{ ICE_GTP_SESSION,	ICE_GTP_FLAGS_MASK },
2907cbad5db8SQi Zhang 	{ ICE_GTP_DOWNLINK,	ICE_GTP_FLAGS_MASK },
2908cbad5db8SQi Zhang 	{ ICE_GTP_UPLINK,	ICE_GTP_FLAGS_MASK },
2909cbad5db8SQi Zhang };
2910cbad5db8SQi Zhang 
2911cbad5db8SQi Zhang /**
2912cbad5db8SQi Zhang  * ice_get_ptype_attrib_info - get PTYPE attribute information
2913cbad5db8SQi Zhang  * @type: attribute type
2914cbad5db8SQi Zhang  * @info: pointer to variable to the attribute information
2915cbad5db8SQi Zhang  */
2916cbad5db8SQi Zhang static void
ice_get_ptype_attrib_info(enum ice_ptype_attrib_type type,struct ice_ptype_attrib_info * info)2917cbad5db8SQi Zhang ice_get_ptype_attrib_info(enum ice_ptype_attrib_type type,
2918cbad5db8SQi Zhang 			  struct ice_ptype_attrib_info *info)
2919cbad5db8SQi Zhang {
2920cbad5db8SQi Zhang 	*info = ice_ptype_attributes[type];
2921cbad5db8SQi Zhang }
2922cbad5db8SQi Zhang 
2923cbad5db8SQi Zhang /**
2924cbad5db8SQi Zhang  * ice_add_prof_attrib - add any PTG with attributes to profile
2925cbad5db8SQi Zhang  * @prof: pointer to the profile to which PTG entries will be added
2926cbad5db8SQi Zhang  * @ptg: PTG to be added
2927cbad5db8SQi Zhang  * @ptype: PTYPE that needs to be looked up
2928cbad5db8SQi Zhang  * @attr: array of attributes that will be considered
2929cbad5db8SQi Zhang  * @attr_cnt: number of elements in the attribute array
2930cbad5db8SQi Zhang  */
29315e24d598STony Nguyen static int
ice_add_prof_attrib(struct ice_prof_map * prof,u8 ptg,u16 ptype,const struct ice_ptype_attributes * attr,u16 attr_cnt)2932cbad5db8SQi Zhang ice_add_prof_attrib(struct ice_prof_map *prof, u8 ptg, u16 ptype,
2933cbad5db8SQi Zhang 		    const struct ice_ptype_attributes *attr, u16 attr_cnt)
2934cbad5db8SQi Zhang {
2935cbad5db8SQi Zhang 	bool found = false;
2936cbad5db8SQi Zhang 	u16 i;
2937cbad5db8SQi Zhang 
2938cbad5db8SQi Zhang 	for (i = 0; i < attr_cnt; i++)
2939cbad5db8SQi Zhang 		if (attr[i].ptype == ptype) {
2940cbad5db8SQi Zhang 			found = true;
2941cbad5db8SQi Zhang 
2942cbad5db8SQi Zhang 			prof->ptg[prof->ptg_cnt] = ptg;
2943cbad5db8SQi Zhang 			ice_get_ptype_attrib_info(attr[i].attrib,
2944cbad5db8SQi Zhang 						  &prof->attr[prof->ptg_cnt]);
2945cbad5db8SQi Zhang 
2946cbad5db8SQi Zhang 			if (++prof->ptg_cnt >= ICE_MAX_PTG_PER_PROFILE)
2947d54699e2STony Nguyen 				return -ENOSPC;
2948cbad5db8SQi Zhang 		}
2949cbad5db8SQi Zhang 
2950cbad5db8SQi Zhang 	if (!found)
2951d54699e2STony Nguyen 		return -ENOENT;
2952cbad5db8SQi Zhang 
2953cbad5db8SQi Zhang 	return 0;
2954cbad5db8SQi Zhang }
2955cbad5db8SQi Zhang 
2956148beb61SHenry Tieman /**
295731ad4e4eSTony Nguyen  * ice_add_prof - add profile
295831ad4e4eSTony Nguyen  * @hw: pointer to the HW struct
295931ad4e4eSTony Nguyen  * @blk: hardware block
296031ad4e4eSTony Nguyen  * @id: profile tracking ID
296131ad4e4eSTony Nguyen  * @ptypes: array of bitmaps indicating ptypes (ICE_FLOW_PTYPE_MAX bits)
2962cbad5db8SQi Zhang  * @attr: array of attributes
2963cbad5db8SQi Zhang  * @attr_cnt: number of elements in attr array
296431ad4e4eSTony Nguyen  * @es: extraction sequence (length of array is determined by the block)
2965b199dddbSQi Zhang  * @masks: mask for extraction sequence
296631ad4e4eSTony Nguyen  *
2967b199dddbSQi Zhang  * This function registers a profile, which matches a set of PTYPES with a
296831ad4e4eSTony Nguyen  * particular extraction sequence. While the hardware profile is allocated
296931ad4e4eSTony Nguyen  * it will not be written until the first call to ice_add_flow that specifies
297031ad4e4eSTony Nguyen  * the ID value used here.
297131ad4e4eSTony Nguyen  */
29725e24d598STony Nguyen int
ice_add_prof(struct ice_hw * hw,enum ice_block blk,u64 id,u8 ptypes[],const struct ice_ptype_attributes * attr,u16 attr_cnt,struct ice_fv_word * es,u16 * masks)297331ad4e4eSTony Nguyen ice_add_prof(struct ice_hw *hw, enum ice_block blk, u64 id, u8 ptypes[],
2974cbad5db8SQi Zhang 	     const struct ice_ptype_attributes *attr, u16 attr_cnt,
2975b199dddbSQi Zhang 	     struct ice_fv_word *es, u16 *masks)
297631ad4e4eSTony Nguyen {
297731ad4e4eSTony Nguyen 	u32 bytes = DIV_ROUND_UP(ICE_FLOW_PTYPE_MAX, BITS_PER_BYTE);
297831ad4e4eSTony Nguyen 	DECLARE_BITMAP(ptgs_used, ICE_XLT1_CNT);
297931ad4e4eSTony Nguyen 	struct ice_prof_map *prof;
298088865fc4SKarol Kolacinski 	u8 byte = 0;
298131ad4e4eSTony Nguyen 	u8 prof_id;
29825518ac2aSTony Nguyen 	int status;
298331ad4e4eSTony Nguyen 
298431ad4e4eSTony Nguyen 	bitmap_zero(ptgs_used, ICE_XLT1_CNT);
298531ad4e4eSTony Nguyen 
298631ad4e4eSTony Nguyen 	mutex_lock(&hw->blk[blk].es.prof_map_lock);
298731ad4e4eSTony Nguyen 
298831ad4e4eSTony Nguyen 	/* search for existing profile */
2989b199dddbSQi Zhang 	status = ice_find_prof_id_with_mask(hw, blk, es, masks, &prof_id);
299031ad4e4eSTony Nguyen 	if (status) {
299131ad4e4eSTony Nguyen 		/* allocate profile ID */
299231ad4e4eSTony Nguyen 		status = ice_alloc_prof_id(hw, blk, &prof_id);
299331ad4e4eSTony Nguyen 		if (status)
299431ad4e4eSTony Nguyen 			goto err_ice_add_prof;
2995148beb61SHenry Tieman 		if (blk == ICE_BLK_FD) {
2996148beb61SHenry Tieman 			/* For Flow Director block, the extraction sequence may
2997148beb61SHenry Tieman 			 * need to be altered in the case where there are paired
2998148beb61SHenry Tieman 			 * fields that have no match. This is necessary because
2999148beb61SHenry Tieman 			 * for Flow Director, src and dest fields need to paired
3000148beb61SHenry Tieman 			 * for filter programming and these values are swapped
3001148beb61SHenry Tieman 			 * during Tx.
3002148beb61SHenry Tieman 			 */
3003148beb61SHenry Tieman 			status = ice_update_fd_swap(hw, prof_id, es);
3004148beb61SHenry Tieman 			if (status)
3005148beb61SHenry Tieman 				goto err_ice_add_prof;
3006148beb61SHenry Tieman 		}
3007b199dddbSQi Zhang 		status = ice_update_prof_masking(hw, blk, prof_id, masks);
3008b199dddbSQi Zhang 		if (status)
3009b199dddbSQi Zhang 			goto err_ice_add_prof;
301031ad4e4eSTony Nguyen 
301131ad4e4eSTony Nguyen 		/* and write new es */
301231ad4e4eSTony Nguyen 		ice_write_es(hw, blk, prof_id, es);
301331ad4e4eSTony Nguyen 	}
301431ad4e4eSTony Nguyen 
301531ad4e4eSTony Nguyen 	ice_prof_inc_ref(hw, blk, prof_id);
301631ad4e4eSTony Nguyen 
301731ad4e4eSTony Nguyen 	/* add profile info */
301831ad4e4eSTony Nguyen 	prof = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*prof), GFP_KERNEL);
3019f8d530acSWei Yongjun 	if (!prof) {
3020d54699e2STony Nguyen 		status = -ENOMEM;
302131ad4e4eSTony Nguyen 		goto err_ice_add_prof;
3022f8d530acSWei Yongjun 	}
302331ad4e4eSTony Nguyen 
302431ad4e4eSTony Nguyen 	prof->profile_cookie = id;
302531ad4e4eSTony Nguyen 	prof->prof_id = prof_id;
302631ad4e4eSTony Nguyen 	prof->ptg_cnt = 0;
302731ad4e4eSTony Nguyen 	prof->context = 0;
302831ad4e4eSTony Nguyen 
302931ad4e4eSTony Nguyen 	/* build list of ptgs */
303031ad4e4eSTony Nguyen 	while (bytes && prof->ptg_cnt < ICE_MAX_PTG_PER_PROFILE) {
303188865fc4SKarol Kolacinski 		u8 bit;
303231ad4e4eSTony Nguyen 
303331ad4e4eSTony Nguyen 		if (!ptypes[byte]) {
303431ad4e4eSTony Nguyen 			bytes--;
303531ad4e4eSTony Nguyen 			byte++;
303631ad4e4eSTony Nguyen 			continue;
303731ad4e4eSTony Nguyen 		}
303831ad4e4eSTony Nguyen 
303931ad4e4eSTony Nguyen 		/* Examine 8 bits per byte */
304031ad4e4eSTony Nguyen 		for_each_set_bit(bit, (unsigned long *)&ptypes[byte],
304131ad4e4eSTony Nguyen 				 BITS_PER_BYTE) {
304231ad4e4eSTony Nguyen 			u16 ptype;
304331ad4e4eSTony Nguyen 			u8 ptg;
304431ad4e4eSTony Nguyen 
304531ad4e4eSTony Nguyen 			ptype = byte * BITS_PER_BYTE + bit;
304631ad4e4eSTony Nguyen 
304731ad4e4eSTony Nguyen 			/* The package should place all ptypes in a non-zero
304831ad4e4eSTony Nguyen 			 * PTG, so the following call should never fail.
304931ad4e4eSTony Nguyen 			 */
305031ad4e4eSTony Nguyen 			if (ice_ptg_find_ptype(hw, blk, ptype, &ptg))
305131ad4e4eSTony Nguyen 				continue;
305231ad4e4eSTony Nguyen 
305331ad4e4eSTony Nguyen 			/* If PTG is already added, skip and continue */
305431ad4e4eSTony Nguyen 			if (test_bit(ptg, ptgs_used))
305531ad4e4eSTony Nguyen 				continue;
305631ad4e4eSTony Nguyen 
3057e75ed29dSChristophe JAILLET 			__set_bit(ptg, ptgs_used);
3058cbad5db8SQi Zhang 			/* Check to see there are any attributes for
3059cbad5db8SQi Zhang 			 * this PTYPE, and add them if found.
3060cbad5db8SQi Zhang 			 */
3061cbad5db8SQi Zhang 			status = ice_add_prof_attrib(prof, ptg, ptype,
3062cbad5db8SQi Zhang 						     attr, attr_cnt);
3063d54699e2STony Nguyen 			if (status == -ENOSPC)
3064cbad5db8SQi Zhang 				break;
3065cbad5db8SQi Zhang 			if (status) {
3066cbad5db8SQi Zhang 				/* This is simple a PTYPE/PTG with no
3067cbad5db8SQi Zhang 				 * attribute
3068cbad5db8SQi Zhang 				 */
306931ad4e4eSTony Nguyen 				prof->ptg[prof->ptg_cnt] = ptg;
3070cbad5db8SQi Zhang 				prof->attr[prof->ptg_cnt].flags = 0;
3071cbad5db8SQi Zhang 				prof->attr[prof->ptg_cnt].mask = 0;
307231ad4e4eSTony Nguyen 
3073cbad5db8SQi Zhang 				if (++prof->ptg_cnt >=
3074cbad5db8SQi Zhang 				    ICE_MAX_PTG_PER_PROFILE)
307531ad4e4eSTony Nguyen 					break;
3076cbad5db8SQi Zhang 			}
307731ad4e4eSTony Nguyen 		}
307831ad4e4eSTony Nguyen 
307931ad4e4eSTony Nguyen 		bytes--;
308031ad4e4eSTony Nguyen 		byte++;
308131ad4e4eSTony Nguyen 	}
308231ad4e4eSTony Nguyen 
308331ad4e4eSTony Nguyen 	list_add(&prof->list, &hw->blk[blk].es.prof_map);
308431ad4e4eSTony Nguyen 	status = 0;
308531ad4e4eSTony Nguyen 
308631ad4e4eSTony Nguyen err_ice_add_prof:
308731ad4e4eSTony Nguyen 	mutex_unlock(&hw->blk[blk].es.prof_map_lock);
308831ad4e4eSTony Nguyen 	return status;
308931ad4e4eSTony Nguyen }
3090451f2c44STony Nguyen 
3091451f2c44STony Nguyen /**
30926a2c2b2cSVictor Raj  * ice_search_prof_id - Search for a profile tracking ID
3093451f2c44STony Nguyen  * @hw: pointer to the HW struct
3094451f2c44STony Nguyen  * @blk: hardware block
3095451f2c44STony Nguyen  * @id: profile tracking ID
3096451f2c44STony Nguyen  *
30976a2c2b2cSVictor Raj  * This will search for a profile tracking ID which was previously added.
30986a2c2b2cSVictor Raj  * The profile map lock should be held before calling this function.
3099451f2c44STony Nguyen  */
3100451f2c44STony Nguyen static struct ice_prof_map *
ice_search_prof_id(struct ice_hw * hw,enum ice_block blk,u64 id)31016a2c2b2cSVictor Raj ice_search_prof_id(struct ice_hw *hw, enum ice_block blk, u64 id)
3102451f2c44STony Nguyen {
3103451f2c44STony Nguyen 	struct ice_prof_map *entry = NULL;
3104451f2c44STony Nguyen 	struct ice_prof_map *map;
3105451f2c44STony Nguyen 
3106451f2c44STony Nguyen 	list_for_each_entry(map, &hw->blk[blk].es.prof_map, list)
3107451f2c44STony Nguyen 		if (map->profile_cookie == id) {
3108451f2c44STony Nguyen 			entry = map;
3109451f2c44STony Nguyen 			break;
3110451f2c44STony Nguyen 		}
3111451f2c44STony Nguyen 
3112451f2c44STony Nguyen 	return entry;
3113451f2c44STony Nguyen }
3114451f2c44STony Nguyen 
3115451f2c44STony Nguyen /**
31162c61054cSTony Nguyen  * ice_vsig_prof_id_count - count profiles in a VSIG
31172c61054cSTony Nguyen  * @hw: pointer to the HW struct
31182c61054cSTony Nguyen  * @blk: hardware block
31192c61054cSTony Nguyen  * @vsig: VSIG to remove the profile from
31202c61054cSTony Nguyen  */
31212c61054cSTony Nguyen static u16
ice_vsig_prof_id_count(struct ice_hw * hw,enum ice_block blk,u16 vsig)31222c61054cSTony Nguyen ice_vsig_prof_id_count(struct ice_hw *hw, enum ice_block blk, u16 vsig)
31232c61054cSTony Nguyen {
31242c61054cSTony Nguyen 	u16 idx = vsig & ICE_VSIG_IDX_M, count = 0;
31252c61054cSTony Nguyen 	struct ice_vsig_prof *p;
31262c61054cSTony Nguyen 
31272c61054cSTony Nguyen 	list_for_each_entry(p, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
31282c61054cSTony Nguyen 			    list)
31292c61054cSTony Nguyen 		count++;
31302c61054cSTony Nguyen 
31312c61054cSTony Nguyen 	return count;
31322c61054cSTony Nguyen }
31332c61054cSTony Nguyen 
31342c61054cSTony Nguyen /**
3135451f2c44STony Nguyen  * ice_rel_tcam_idx - release a TCAM index
3136451f2c44STony Nguyen  * @hw: pointer to the HW struct
3137451f2c44STony Nguyen  * @blk: hardware block
3138451f2c44STony Nguyen  * @idx: the index to release
3139451f2c44STony Nguyen  */
ice_rel_tcam_idx(struct ice_hw * hw,enum ice_block blk,u16 idx)31405518ac2aSTony Nguyen static int ice_rel_tcam_idx(struct ice_hw *hw, enum ice_block blk, u16 idx)
3141451f2c44STony Nguyen {
3142451f2c44STony Nguyen 	/* Masks to invoke a never match entry */
3143451f2c44STony Nguyen 	u8 vl_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3144451f2c44STony Nguyen 	u8 dc_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFE, 0xFF, 0xFF, 0xFF, 0xFF };
3145451f2c44STony Nguyen 	u8 nm_msk[ICE_TCAM_KEY_VAL_SZ] = { 0x01, 0x00, 0x00, 0x00, 0x00 };
31465e24d598STony Nguyen 	int status;
3147451f2c44STony Nguyen 
3148451f2c44STony Nguyen 	/* write the TCAM entry */
3149451f2c44STony Nguyen 	status = ice_tcam_write_entry(hw, blk, idx, 0, 0, 0, 0, 0, vl_msk,
3150451f2c44STony Nguyen 				      dc_msk, nm_msk);
3151451f2c44STony Nguyen 	if (status)
3152451f2c44STony Nguyen 		return status;
3153451f2c44STony Nguyen 
3154451f2c44STony Nguyen 	/* release the TCAM entry */
3155451f2c44STony Nguyen 	status = ice_free_tcam_ent(hw, blk, idx);
3156451f2c44STony Nguyen 
3157451f2c44STony Nguyen 	return status;
3158451f2c44STony Nguyen }
3159451f2c44STony Nguyen 
3160451f2c44STony Nguyen /**
3161451f2c44STony Nguyen  * ice_rem_prof_id - remove one profile from a VSIG
3162451f2c44STony Nguyen  * @hw: pointer to the HW struct
3163451f2c44STony Nguyen  * @blk: hardware block
3164451f2c44STony Nguyen  * @prof: pointer to profile structure to remove
3165451f2c44STony Nguyen  */
31665e24d598STony Nguyen static int
ice_rem_prof_id(struct ice_hw * hw,enum ice_block blk,struct ice_vsig_prof * prof)3167451f2c44STony Nguyen ice_rem_prof_id(struct ice_hw *hw, enum ice_block blk,
3168451f2c44STony Nguyen 		struct ice_vsig_prof *prof)
3169451f2c44STony Nguyen {
31705e24d598STony Nguyen 	int status;
3171451f2c44STony Nguyen 	u16 i;
3172451f2c44STony Nguyen 
3173451f2c44STony Nguyen 	for (i = 0; i < prof->tcam_count; i++)
3174451f2c44STony Nguyen 		if (prof->tcam[i].in_use) {
3175451f2c44STony Nguyen 			prof->tcam[i].in_use = false;
3176451f2c44STony Nguyen 			status = ice_rel_tcam_idx(hw, blk,
3177451f2c44STony Nguyen 						  prof->tcam[i].tcam_idx);
3178451f2c44STony Nguyen 			if (status)
3179d54699e2STony Nguyen 				return -EIO;
3180451f2c44STony Nguyen 		}
3181451f2c44STony Nguyen 
3182451f2c44STony Nguyen 	return 0;
3183451f2c44STony Nguyen }
3184451f2c44STony Nguyen 
3185451f2c44STony Nguyen /**
3186451f2c44STony Nguyen  * ice_rem_vsig - remove VSIG
3187451f2c44STony Nguyen  * @hw: pointer to the HW struct
3188451f2c44STony Nguyen  * @blk: hardware block
3189451f2c44STony Nguyen  * @vsig: the VSIG to remove
3190451f2c44STony Nguyen  * @chg: the change list
3191451f2c44STony Nguyen  */
31925e24d598STony Nguyen static int
ice_rem_vsig(struct ice_hw * hw,enum ice_block blk,u16 vsig,struct list_head * chg)3193451f2c44STony Nguyen ice_rem_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig,
3194451f2c44STony Nguyen 	     struct list_head *chg)
3195451f2c44STony Nguyen {
3196451f2c44STony Nguyen 	u16 idx = vsig & ICE_VSIG_IDX_M;
3197451f2c44STony Nguyen 	struct ice_vsig_vsi *vsi_cur;
3198451f2c44STony Nguyen 	struct ice_vsig_prof *d, *t;
3199451f2c44STony Nguyen 
3200451f2c44STony Nguyen 	/* remove TCAM entries */
3201451f2c44STony Nguyen 	list_for_each_entry_safe(d, t,
3202451f2c44STony Nguyen 				 &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
3203451f2c44STony Nguyen 				 list) {
3204*bd557d97STony Nguyen 		int status;
3205*bd557d97STony Nguyen 
3206451f2c44STony Nguyen 		status = ice_rem_prof_id(hw, blk, d);
3207451f2c44STony Nguyen 		if (status)
3208451f2c44STony Nguyen 			return status;
3209451f2c44STony Nguyen 
3210451f2c44STony Nguyen 		list_del(&d->list);
3211451f2c44STony Nguyen 		devm_kfree(ice_hw_to_dev(hw), d);
3212451f2c44STony Nguyen 	}
3213451f2c44STony Nguyen 
3214451f2c44STony Nguyen 	/* Move all VSIS associated with this VSIG to the default VSIG */
3215451f2c44STony Nguyen 	vsi_cur = hw->blk[blk].xlt2.vsig_tbl[idx].first_vsi;
3216451f2c44STony Nguyen 	/* If the VSIG has at least 1 VSI then iterate through the list
3217451f2c44STony Nguyen 	 * and remove the VSIs before deleting the group.
3218451f2c44STony Nguyen 	 */
3219451f2c44STony Nguyen 	if (vsi_cur)
3220451f2c44STony Nguyen 		do {
3221451f2c44STony Nguyen 			struct ice_vsig_vsi *tmp = vsi_cur->next_vsi;
3222451f2c44STony Nguyen 			struct ice_chs_chg *p;
3223451f2c44STony Nguyen 
3224451f2c44STony Nguyen 			p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p),
3225451f2c44STony Nguyen 					 GFP_KERNEL);
3226451f2c44STony Nguyen 			if (!p)
3227d54699e2STony Nguyen 				return -ENOMEM;
3228451f2c44STony Nguyen 
3229451f2c44STony Nguyen 			p->type = ICE_VSIG_REM;
3230451f2c44STony Nguyen 			p->orig_vsig = vsig;
3231451f2c44STony Nguyen 			p->vsig = ICE_DEFAULT_VSIG;
3232451f2c44STony Nguyen 			p->vsi = vsi_cur - hw->blk[blk].xlt2.vsis;
3233451f2c44STony Nguyen 
3234451f2c44STony Nguyen 			list_add(&p->list_entry, chg);
3235451f2c44STony Nguyen 
3236451f2c44STony Nguyen 			vsi_cur = tmp;
3237451f2c44STony Nguyen 		} while (vsi_cur);
3238451f2c44STony Nguyen 
3239451f2c44STony Nguyen 	return ice_vsig_free(hw, blk, vsig);
3240451f2c44STony Nguyen }
3241451f2c44STony Nguyen 
3242451f2c44STony Nguyen /**
32432c61054cSTony Nguyen  * ice_rem_prof_id_vsig - remove a specific profile from a VSIG
32442c61054cSTony Nguyen  * @hw: pointer to the HW struct
32452c61054cSTony Nguyen  * @blk: hardware block
32462c61054cSTony Nguyen  * @vsig: VSIG to remove the profile from
32472c61054cSTony Nguyen  * @hdl: profile handle indicating which profile to remove
32482c61054cSTony Nguyen  * @chg: list to receive a record of changes
32492c61054cSTony Nguyen  */
32505e24d598STony Nguyen static int
ice_rem_prof_id_vsig(struct ice_hw * hw,enum ice_block blk,u16 vsig,u64 hdl,struct list_head * chg)32512c61054cSTony Nguyen ice_rem_prof_id_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig, u64 hdl,
32522c61054cSTony Nguyen 		     struct list_head *chg)
32532c61054cSTony Nguyen {
32542c61054cSTony Nguyen 	u16 idx = vsig & ICE_VSIG_IDX_M;
32552c61054cSTony Nguyen 	struct ice_vsig_prof *p, *t;
32562c61054cSTony Nguyen 
32572c61054cSTony Nguyen 	list_for_each_entry_safe(p, t,
32582c61054cSTony Nguyen 				 &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
32592c61054cSTony Nguyen 				 list)
32602c61054cSTony Nguyen 		if (p->profile_cookie == hdl) {
3261*bd557d97STony Nguyen 			int status;
3262*bd557d97STony Nguyen 
32632c61054cSTony Nguyen 			if (ice_vsig_prof_id_count(hw, blk, vsig) == 1)
32642c61054cSTony Nguyen 				/* this is the last profile, remove the VSIG */
32652c61054cSTony Nguyen 				return ice_rem_vsig(hw, blk, vsig, chg);
32662c61054cSTony Nguyen 
32672c61054cSTony Nguyen 			status = ice_rem_prof_id(hw, blk, p);
32682c61054cSTony Nguyen 			if (!status) {
32692c61054cSTony Nguyen 				list_del(&p->list);
32702c61054cSTony Nguyen 				devm_kfree(ice_hw_to_dev(hw), p);
32712c61054cSTony Nguyen 			}
32722c61054cSTony Nguyen 			return status;
32732c61054cSTony Nguyen 		}
32742c61054cSTony Nguyen 
3275d54699e2STony Nguyen 	return -ENOENT;
32762c61054cSTony Nguyen }
32772c61054cSTony Nguyen 
32782c61054cSTony Nguyen /**
32792c61054cSTony Nguyen  * ice_rem_flow_all - remove all flows with a particular profile
32802c61054cSTony Nguyen  * @hw: pointer to the HW struct
32812c61054cSTony Nguyen  * @blk: hardware block
32822c61054cSTony Nguyen  * @id: profile tracking ID
32832c61054cSTony Nguyen  */
ice_rem_flow_all(struct ice_hw * hw,enum ice_block blk,u64 id)32845518ac2aSTony Nguyen static int ice_rem_flow_all(struct ice_hw *hw, enum ice_block blk, u64 id)
32852c61054cSTony Nguyen {
32862c61054cSTony Nguyen 	struct ice_chs_chg *del, *tmp;
32872c61054cSTony Nguyen 	struct list_head chg;
32885518ac2aSTony Nguyen 	int status;
32892c61054cSTony Nguyen 	u16 i;
32902c61054cSTony Nguyen 
32912c61054cSTony Nguyen 	INIT_LIST_HEAD(&chg);
32922c61054cSTony Nguyen 
32932c61054cSTony Nguyen 	for (i = 1; i < ICE_MAX_VSIGS; i++)
32942c61054cSTony Nguyen 		if (hw->blk[blk].xlt2.vsig_tbl[i].in_use) {
32952c61054cSTony Nguyen 			if (ice_has_prof_vsig(hw, blk, i, id)) {
32962c61054cSTony Nguyen 				status = ice_rem_prof_id_vsig(hw, blk, i, id,
32972c61054cSTony Nguyen 							      &chg);
32982c61054cSTony Nguyen 				if (status)
32992c61054cSTony Nguyen 					goto err_ice_rem_flow_all;
33002c61054cSTony Nguyen 			}
33012c61054cSTony Nguyen 		}
33022c61054cSTony Nguyen 
33032c61054cSTony Nguyen 	status = ice_upd_prof_hw(hw, blk, &chg);
33042c61054cSTony Nguyen 
33052c61054cSTony Nguyen err_ice_rem_flow_all:
33062c61054cSTony Nguyen 	list_for_each_entry_safe(del, tmp, &chg, list_entry) {
33072c61054cSTony Nguyen 		list_del(&del->list_entry);
33082c61054cSTony Nguyen 		devm_kfree(ice_hw_to_dev(hw), del);
33092c61054cSTony Nguyen 	}
33102c61054cSTony Nguyen 
33112c61054cSTony Nguyen 	return status;
33122c61054cSTony Nguyen }
33132c61054cSTony Nguyen 
33142c61054cSTony Nguyen /**
33152c61054cSTony Nguyen  * ice_rem_prof - remove profile
33162c61054cSTony Nguyen  * @hw: pointer to the HW struct
33172c61054cSTony Nguyen  * @blk: hardware block
33182c61054cSTony Nguyen  * @id: profile tracking ID
33192c61054cSTony Nguyen  *
33202c61054cSTony Nguyen  * This will remove the profile specified by the ID parameter, which was
33212c61054cSTony Nguyen  * previously created through ice_add_prof. If any existing entries
33222c61054cSTony Nguyen  * are associated with this profile, they will be removed as well.
33232c61054cSTony Nguyen  */
ice_rem_prof(struct ice_hw * hw,enum ice_block blk,u64 id)33245e24d598STony Nguyen int ice_rem_prof(struct ice_hw *hw, enum ice_block blk, u64 id)
33252c61054cSTony Nguyen {
33262c61054cSTony Nguyen 	struct ice_prof_map *pmap;
33275e24d598STony Nguyen 	int status;
33282c61054cSTony Nguyen 
33292c61054cSTony Nguyen 	mutex_lock(&hw->blk[blk].es.prof_map_lock);
33302c61054cSTony Nguyen 
33316a2c2b2cSVictor Raj 	pmap = ice_search_prof_id(hw, blk, id);
33322c61054cSTony Nguyen 	if (!pmap) {
3333d54699e2STony Nguyen 		status = -ENOENT;
33342c61054cSTony Nguyen 		goto err_ice_rem_prof;
33352c61054cSTony Nguyen 	}
33362c61054cSTony Nguyen 
33372c61054cSTony Nguyen 	/* remove all flows with this profile */
33382c61054cSTony Nguyen 	status = ice_rem_flow_all(hw, blk, pmap->profile_cookie);
33392c61054cSTony Nguyen 	if (status)
33402c61054cSTony Nguyen 		goto err_ice_rem_prof;
33412c61054cSTony Nguyen 
33422c61054cSTony Nguyen 	/* dereference profile, and possibly remove */
33432c61054cSTony Nguyen 	ice_prof_dec_ref(hw, blk, pmap->prof_id);
33442c61054cSTony Nguyen 
33452c61054cSTony Nguyen 	list_del(&pmap->list);
33462c61054cSTony Nguyen 	devm_kfree(ice_hw_to_dev(hw), pmap);
33472c61054cSTony Nguyen 
33482c61054cSTony Nguyen err_ice_rem_prof:
33492c61054cSTony Nguyen 	mutex_unlock(&hw->blk[blk].es.prof_map_lock);
33502c61054cSTony Nguyen 	return status;
33512c61054cSTony Nguyen }
33522c61054cSTony Nguyen 
33532c61054cSTony Nguyen /**
3354451f2c44STony Nguyen  * ice_get_prof - get profile
3355451f2c44STony Nguyen  * @hw: pointer to the HW struct
3356451f2c44STony Nguyen  * @blk: hardware block
3357451f2c44STony Nguyen  * @hdl: profile handle
3358451f2c44STony Nguyen  * @chg: change list
3359451f2c44STony Nguyen  */
33605e24d598STony Nguyen static int
ice_get_prof(struct ice_hw * hw,enum ice_block blk,u64 hdl,struct list_head * chg)3361451f2c44STony Nguyen ice_get_prof(struct ice_hw *hw, enum ice_block blk, u64 hdl,
3362451f2c44STony Nguyen 	     struct list_head *chg)
3363451f2c44STony Nguyen {
3364451f2c44STony Nguyen 	struct ice_prof_map *map;
3365451f2c44STony Nguyen 	struct ice_chs_chg *p;
33665518ac2aSTony Nguyen 	int status = 0;
3367451f2c44STony Nguyen 	u16 i;
3368451f2c44STony Nguyen 
33696a2c2b2cSVictor Raj 	mutex_lock(&hw->blk[blk].es.prof_map_lock);
3370451f2c44STony Nguyen 	/* Get the details on the profile specified by the handle ID */
3371451f2c44STony Nguyen 	map = ice_search_prof_id(hw, blk, hdl);
33726a2c2b2cSVictor Raj 	if (!map) {
3373d54699e2STony Nguyen 		status = -ENOENT;
33746a2c2b2cSVictor Raj 		goto err_ice_get_prof;
33756a2c2b2cSVictor Raj 	}
3376451f2c44STony Nguyen 
3377451f2c44STony Nguyen 	for (i = 0; i < map->ptg_cnt; i++)
3378451f2c44STony Nguyen 		if (!hw->blk[blk].es.written[map->prof_id]) {
3379451f2c44STony Nguyen 			/* add ES to change list */
3380451f2c44STony Nguyen 			p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p),
3381451f2c44STony Nguyen 					 GFP_KERNEL);
33826a2c2b2cSVictor Raj 			if (!p) {
3383d54699e2STony Nguyen 				status = -ENOMEM;
3384451f2c44STony Nguyen 				goto err_ice_get_prof;
33856a2c2b2cSVictor Raj 			}
3386451f2c44STony Nguyen 
3387451f2c44STony Nguyen 			p->type = ICE_PTG_ES_ADD;
3388451f2c44STony Nguyen 			p->ptype = 0;
3389451f2c44STony Nguyen 			p->ptg = map->ptg[i];
3390451f2c44STony Nguyen 			p->add_ptg = 0;
3391451f2c44STony Nguyen 
3392451f2c44STony Nguyen 			p->add_prof = 1;
3393451f2c44STony Nguyen 			p->prof_id = map->prof_id;
3394451f2c44STony Nguyen 
3395451f2c44STony Nguyen 			hw->blk[blk].es.written[map->prof_id] = true;
3396451f2c44STony Nguyen 
3397451f2c44STony Nguyen 			list_add(&p->list_entry, chg);
3398451f2c44STony Nguyen 		}
3399451f2c44STony Nguyen 
3400451f2c44STony Nguyen err_ice_get_prof:
34016a2c2b2cSVictor Raj 	mutex_unlock(&hw->blk[blk].es.prof_map_lock);
3402451f2c44STony Nguyen 	/* let caller clean up the change list */
34036a2c2b2cSVictor Raj 	return status;
3404451f2c44STony Nguyen }
3405451f2c44STony Nguyen 
3406451f2c44STony Nguyen /**
3407451f2c44STony Nguyen  * ice_get_profs_vsig - get a copy of the list of profiles from a VSIG
3408451f2c44STony Nguyen  * @hw: pointer to the HW struct
3409451f2c44STony Nguyen  * @blk: hardware block
3410451f2c44STony Nguyen  * @vsig: VSIG from which to copy the list
3411451f2c44STony Nguyen  * @lst: output list
3412451f2c44STony Nguyen  *
3413451f2c44STony Nguyen  * This routine makes a copy of the list of profiles in the specified VSIG.
3414451f2c44STony Nguyen  */
34155e24d598STony Nguyen static int
ice_get_profs_vsig(struct ice_hw * hw,enum ice_block blk,u16 vsig,struct list_head * lst)3416451f2c44STony Nguyen ice_get_profs_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig,
3417451f2c44STony Nguyen 		   struct list_head *lst)
3418451f2c44STony Nguyen {
3419451f2c44STony Nguyen 	struct ice_vsig_prof *ent1, *ent2;
3420451f2c44STony Nguyen 	u16 idx = vsig & ICE_VSIG_IDX_M;
3421451f2c44STony Nguyen 
3422451f2c44STony Nguyen 	list_for_each_entry(ent1, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
3423451f2c44STony Nguyen 			    list) {
3424451f2c44STony Nguyen 		struct ice_vsig_prof *p;
3425451f2c44STony Nguyen 
3426451f2c44STony Nguyen 		/* copy to the input list */
3427451f2c44STony Nguyen 		p = devm_kmemdup(ice_hw_to_dev(hw), ent1, sizeof(*p),
3428451f2c44STony Nguyen 				 GFP_KERNEL);
3429451f2c44STony Nguyen 		if (!p)
3430451f2c44STony Nguyen 			goto err_ice_get_profs_vsig;
3431451f2c44STony Nguyen 
3432451f2c44STony Nguyen 		list_add_tail(&p->list, lst);
3433451f2c44STony Nguyen 	}
3434451f2c44STony Nguyen 
3435451f2c44STony Nguyen 	return 0;
3436451f2c44STony Nguyen 
3437451f2c44STony Nguyen err_ice_get_profs_vsig:
3438451f2c44STony Nguyen 	list_for_each_entry_safe(ent1, ent2, lst, list) {
3439451f2c44STony Nguyen 		list_del(&ent1->list);
3440451f2c44STony Nguyen 		devm_kfree(ice_hw_to_dev(hw), ent1);
3441451f2c44STony Nguyen 	}
3442451f2c44STony Nguyen 
3443d54699e2STony Nguyen 	return -ENOMEM;
3444451f2c44STony Nguyen }
3445451f2c44STony Nguyen 
3446451f2c44STony Nguyen /**
3447451f2c44STony Nguyen  * ice_add_prof_to_lst - add profile entry to a list
3448451f2c44STony Nguyen  * @hw: pointer to the HW struct
3449451f2c44STony Nguyen  * @blk: hardware block
3450451f2c44STony Nguyen  * @lst: the list to be added to
3451451f2c44STony Nguyen  * @hdl: profile handle of entry to add
3452451f2c44STony Nguyen  */
34535e24d598STony Nguyen static int
ice_add_prof_to_lst(struct ice_hw * hw,enum ice_block blk,struct list_head * lst,u64 hdl)3454451f2c44STony Nguyen ice_add_prof_to_lst(struct ice_hw *hw, enum ice_block blk,
3455451f2c44STony Nguyen 		    struct list_head *lst, u64 hdl)
3456451f2c44STony Nguyen {
3457451f2c44STony Nguyen 	struct ice_prof_map *map;
3458451f2c44STony Nguyen 	struct ice_vsig_prof *p;
34595518ac2aSTony Nguyen 	int status = 0;
3460451f2c44STony Nguyen 	u16 i;
3461451f2c44STony Nguyen 
34626a2c2b2cSVictor Raj 	mutex_lock(&hw->blk[blk].es.prof_map_lock);
3463451f2c44STony Nguyen 	map = ice_search_prof_id(hw, blk, hdl);
34646a2c2b2cSVictor Raj 	if (!map) {
3465d54699e2STony Nguyen 		status = -ENOENT;
34666a2c2b2cSVictor Raj 		goto err_ice_add_prof_to_lst;
34676a2c2b2cSVictor Raj 	}
3468451f2c44STony Nguyen 
3469451f2c44STony Nguyen 	p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p), GFP_KERNEL);
34706a2c2b2cSVictor Raj 	if (!p) {
3471d54699e2STony Nguyen 		status = -ENOMEM;
34726a2c2b2cSVictor Raj 		goto err_ice_add_prof_to_lst;
34736a2c2b2cSVictor Raj 	}
3474451f2c44STony Nguyen 
3475451f2c44STony Nguyen 	p->profile_cookie = map->profile_cookie;
3476451f2c44STony Nguyen 	p->prof_id = map->prof_id;
3477451f2c44STony Nguyen 	p->tcam_count = map->ptg_cnt;
3478451f2c44STony Nguyen 
3479451f2c44STony Nguyen 	for (i = 0; i < map->ptg_cnt; i++) {
3480451f2c44STony Nguyen 		p->tcam[i].prof_id = map->prof_id;
3481451f2c44STony Nguyen 		p->tcam[i].tcam_idx = ICE_INVALID_TCAM;
3482451f2c44STony Nguyen 		p->tcam[i].ptg = map->ptg[i];
3483451f2c44STony Nguyen 	}
3484451f2c44STony Nguyen 
3485451f2c44STony Nguyen 	list_add(&p->list, lst);
3486451f2c44STony Nguyen 
34876a2c2b2cSVictor Raj err_ice_add_prof_to_lst:
34886a2c2b2cSVictor Raj 	mutex_unlock(&hw->blk[blk].es.prof_map_lock);
34896a2c2b2cSVictor Raj 	return status;
3490451f2c44STony Nguyen }
3491451f2c44STony Nguyen 
3492451f2c44STony Nguyen /**
3493451f2c44STony Nguyen  * ice_move_vsi - move VSI to another VSIG
3494451f2c44STony Nguyen  * @hw: pointer to the HW struct
3495451f2c44STony Nguyen  * @blk: hardware block
3496451f2c44STony Nguyen  * @vsi: the VSI to move
3497451f2c44STony Nguyen  * @vsig: the VSIG to move the VSI to
3498451f2c44STony Nguyen  * @chg: the change list
3499451f2c44STony Nguyen  */
35005e24d598STony Nguyen static int
ice_move_vsi(struct ice_hw * hw,enum ice_block blk,u16 vsi,u16 vsig,struct list_head * chg)3501451f2c44STony Nguyen ice_move_vsi(struct ice_hw *hw, enum ice_block blk, u16 vsi, u16 vsig,
3502451f2c44STony Nguyen 	     struct list_head *chg)
3503451f2c44STony Nguyen {
3504451f2c44STony Nguyen 	struct ice_chs_chg *p;
3505451f2c44STony Nguyen 	u16 orig_vsig;
35065518ac2aSTony Nguyen 	int status;
3507451f2c44STony Nguyen 
3508451f2c44STony Nguyen 	p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p), GFP_KERNEL);
3509451f2c44STony Nguyen 	if (!p)
3510d54699e2STony Nguyen 		return -ENOMEM;
3511451f2c44STony Nguyen 
3512451f2c44STony Nguyen 	status = ice_vsig_find_vsi(hw, blk, vsi, &orig_vsig);
3513451f2c44STony Nguyen 	if (!status)
3514451f2c44STony Nguyen 		status = ice_vsig_add_mv_vsi(hw, blk, vsi, vsig);
3515451f2c44STony Nguyen 
3516451f2c44STony Nguyen 	if (status) {
3517451f2c44STony Nguyen 		devm_kfree(ice_hw_to_dev(hw), p);
3518451f2c44STony Nguyen 		return status;
3519451f2c44STony Nguyen 	}
3520451f2c44STony Nguyen 
3521451f2c44STony Nguyen 	p->type = ICE_VSI_MOVE;
3522451f2c44STony Nguyen 	p->vsi = vsi;
3523451f2c44STony Nguyen 	p->orig_vsig = orig_vsig;
3524451f2c44STony Nguyen 	p->vsig = vsig;
3525451f2c44STony Nguyen 
3526451f2c44STony Nguyen 	list_add(&p->list_entry, chg);
3527451f2c44STony Nguyen 
3528451f2c44STony Nguyen 	return 0;
3529451f2c44STony Nguyen }
3530451f2c44STony Nguyen 
3531451f2c44STony Nguyen /**
3532a6892c96SDan Nowlin  * ice_rem_chg_tcam_ent - remove a specific TCAM entry from change list
3533a6892c96SDan Nowlin  * @hw: pointer to the HW struct
3534a6892c96SDan Nowlin  * @idx: the index of the TCAM entry to remove
3535a6892c96SDan Nowlin  * @chg: the list of change structures to search
3536a6892c96SDan Nowlin  */
3537a6892c96SDan Nowlin static void
ice_rem_chg_tcam_ent(struct ice_hw * hw,u16 idx,struct list_head * chg)3538a6892c96SDan Nowlin ice_rem_chg_tcam_ent(struct ice_hw *hw, u16 idx, struct list_head *chg)
3539a6892c96SDan Nowlin {
3540a6892c96SDan Nowlin 	struct ice_chs_chg *pos, *tmp;
3541a6892c96SDan Nowlin 
3542a6892c96SDan Nowlin 	list_for_each_entry_safe(tmp, pos, chg, list_entry)
3543a6892c96SDan Nowlin 		if (tmp->type == ICE_TCAM_ADD && tmp->tcam_idx == idx) {
3544a6892c96SDan Nowlin 			list_del(&tmp->list_entry);
3545a6892c96SDan Nowlin 			devm_kfree(ice_hw_to_dev(hw), tmp);
3546a6892c96SDan Nowlin 		}
3547a6892c96SDan Nowlin }
3548a6892c96SDan Nowlin 
3549a6892c96SDan Nowlin /**
3550451f2c44STony Nguyen  * ice_prof_tcam_ena_dis - add enable or disable TCAM change
3551451f2c44STony Nguyen  * @hw: pointer to the HW struct
3552451f2c44STony Nguyen  * @blk: hardware block
3553451f2c44STony Nguyen  * @enable: true to enable, false to disable
3554451f2c44STony Nguyen  * @vsig: the VSIG of the TCAM entry
3555451f2c44STony Nguyen  * @tcam: pointer the TCAM info structure of the TCAM to disable
3556451f2c44STony Nguyen  * @chg: the change list
3557451f2c44STony Nguyen  *
3558451f2c44STony Nguyen  * This function appends an enable or disable TCAM entry in the change log
3559451f2c44STony Nguyen  */
35605e24d598STony Nguyen static int
ice_prof_tcam_ena_dis(struct ice_hw * hw,enum ice_block blk,bool enable,u16 vsig,struct ice_tcam_inf * tcam,struct list_head * chg)3561451f2c44STony Nguyen ice_prof_tcam_ena_dis(struct ice_hw *hw, enum ice_block blk, bool enable,
3562451f2c44STony Nguyen 		      u16 vsig, struct ice_tcam_inf *tcam,
3563451f2c44STony Nguyen 		      struct list_head *chg)
3564451f2c44STony Nguyen {
3565451f2c44STony Nguyen 	struct ice_chs_chg *p;
35665518ac2aSTony Nguyen 	int status;
3567451f2c44STony Nguyen 
3568a6892c96SDan Nowlin 	u8 vl_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3569a6892c96SDan Nowlin 	u8 dc_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0x00, 0x00, 0x00 };
3570451f2c44STony Nguyen 	u8 nm_msk[ICE_TCAM_KEY_VAL_SZ] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
3571451f2c44STony Nguyen 
3572451f2c44STony Nguyen 	/* if disabling, free the TCAM */
3573451f2c44STony Nguyen 	if (!enable) {
3574a6892c96SDan Nowlin 		status = ice_rel_tcam_idx(hw, blk, tcam->tcam_idx);
3575a6892c96SDan Nowlin 
3576a6892c96SDan Nowlin 		/* if we have already created a change for this TCAM entry, then
3577a6892c96SDan Nowlin 		 * we need to remove that entry, in order to prevent writing to
3578a6892c96SDan Nowlin 		 * a TCAM entry we no longer will have ownership of.
3579a6892c96SDan Nowlin 		 */
3580a6892c96SDan Nowlin 		ice_rem_chg_tcam_ent(hw, tcam->tcam_idx, chg);
3581451f2c44STony Nguyen 		tcam->tcam_idx = 0;
3582451f2c44STony Nguyen 		tcam->in_use = 0;
3583451f2c44STony Nguyen 		return status;
3584451f2c44STony Nguyen 	}
3585451f2c44STony Nguyen 
3586451f2c44STony Nguyen 	/* for re-enabling, reallocate a TCAM */
3587cbad5db8SQi Zhang 	/* for entries with empty attribute masks, allocate entry from
3588cbad5db8SQi Zhang 	 * the bottom of the TCAM table; otherwise, allocate from the
3589cbad5db8SQi Zhang 	 * top of the table in order to give it higher priority
3590cbad5db8SQi Zhang 	 */
3591cbad5db8SQi Zhang 	status = ice_alloc_tcam_ent(hw, blk, tcam->attr.mask == 0,
3592cbad5db8SQi Zhang 				    &tcam->tcam_idx);
3593451f2c44STony Nguyen 	if (status)
3594451f2c44STony Nguyen 		return status;
3595451f2c44STony Nguyen 
3596451f2c44STony Nguyen 	/* add TCAM to change list */
3597451f2c44STony Nguyen 	p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p), GFP_KERNEL);
3598451f2c44STony Nguyen 	if (!p)
3599d54699e2STony Nguyen 		return -ENOMEM;
3600451f2c44STony Nguyen 
3601451f2c44STony Nguyen 	status = ice_tcam_write_entry(hw, blk, tcam->tcam_idx, tcam->prof_id,
3602cbad5db8SQi Zhang 				      tcam->ptg, vsig, 0, tcam->attr.flags,
3603cbad5db8SQi Zhang 				      vl_msk, dc_msk, nm_msk);
3604451f2c44STony Nguyen 	if (status)
3605451f2c44STony Nguyen 		goto err_ice_prof_tcam_ena_dis;
3606451f2c44STony Nguyen 
3607451f2c44STony Nguyen 	tcam->in_use = 1;
3608451f2c44STony Nguyen 
3609451f2c44STony Nguyen 	p->type = ICE_TCAM_ADD;
3610451f2c44STony Nguyen 	p->add_tcam_idx = true;
3611451f2c44STony Nguyen 	p->prof_id = tcam->prof_id;
3612451f2c44STony Nguyen 	p->ptg = tcam->ptg;
3613451f2c44STony Nguyen 	p->vsig = 0;
3614451f2c44STony Nguyen 	p->tcam_idx = tcam->tcam_idx;
3615451f2c44STony Nguyen 
3616451f2c44STony Nguyen 	/* log change */
3617451f2c44STony Nguyen 	list_add(&p->list_entry, chg);
3618451f2c44STony Nguyen 
3619451f2c44STony Nguyen 	return 0;
3620451f2c44STony Nguyen 
3621451f2c44STony Nguyen err_ice_prof_tcam_ena_dis:
3622451f2c44STony Nguyen 	devm_kfree(ice_hw_to_dev(hw), p);
3623451f2c44STony Nguyen 	return status;
3624451f2c44STony Nguyen }
3625451f2c44STony Nguyen 
3626451f2c44STony Nguyen /**
3627451f2c44STony Nguyen  * ice_adj_prof_priorities - adjust profile based on priorities
3628451f2c44STony Nguyen  * @hw: pointer to the HW struct
3629451f2c44STony Nguyen  * @blk: hardware block
3630451f2c44STony Nguyen  * @vsig: the VSIG for which to adjust profile priorities
3631451f2c44STony Nguyen  * @chg: the change list
3632451f2c44STony Nguyen  */
36335e24d598STony Nguyen static int
ice_adj_prof_priorities(struct ice_hw * hw,enum ice_block blk,u16 vsig,struct list_head * chg)3634451f2c44STony Nguyen ice_adj_prof_priorities(struct ice_hw *hw, enum ice_block blk, u16 vsig,
3635451f2c44STony Nguyen 			struct list_head *chg)
3636451f2c44STony Nguyen {
3637451f2c44STony Nguyen 	DECLARE_BITMAP(ptgs_used, ICE_XLT1_CNT);
3638451f2c44STony Nguyen 	struct ice_vsig_prof *t;
36395e24d598STony Nguyen 	int status;
3640451f2c44STony Nguyen 	u16 idx;
3641451f2c44STony Nguyen 
3642451f2c44STony Nguyen 	bitmap_zero(ptgs_used, ICE_XLT1_CNT);
3643451f2c44STony Nguyen 	idx = vsig & ICE_VSIG_IDX_M;
3644451f2c44STony Nguyen 
3645451f2c44STony Nguyen 	/* Priority is based on the order in which the profiles are added. The
3646451f2c44STony Nguyen 	 * newest added profile has highest priority and the oldest added
3647451f2c44STony Nguyen 	 * profile has the lowest priority. Since the profile property list for
3648451f2c44STony Nguyen 	 * a VSIG is sorted from newest to oldest, this code traverses the list
3649451f2c44STony Nguyen 	 * in order and enables the first of each PTG that it finds (that is not
3650451f2c44STony Nguyen 	 * already enabled); it also disables any duplicate PTGs that it finds
3651451f2c44STony Nguyen 	 * in the older profiles (that are currently enabled).
3652451f2c44STony Nguyen 	 */
3653451f2c44STony Nguyen 
3654451f2c44STony Nguyen 	list_for_each_entry(t, &hw->blk[blk].xlt2.vsig_tbl[idx].prop_lst,
3655451f2c44STony Nguyen 			    list) {
3656451f2c44STony Nguyen 		u16 i;
3657451f2c44STony Nguyen 
3658451f2c44STony Nguyen 		for (i = 0; i < t->tcam_count; i++) {
3659451f2c44STony Nguyen 			/* Scan the priorities from newest to oldest.
3660451f2c44STony Nguyen 			 * Make sure that the newest profiles take priority.
3661451f2c44STony Nguyen 			 */
3662451f2c44STony Nguyen 			if (test_bit(t->tcam[i].ptg, ptgs_used) &&
3663451f2c44STony Nguyen 			    t->tcam[i].in_use) {
3664451f2c44STony Nguyen 				/* need to mark this PTG as never match, as it
3665451f2c44STony Nguyen 				 * was already in use and therefore duplicate
3666451f2c44STony Nguyen 				 * (and lower priority)
3667451f2c44STony Nguyen 				 */
3668451f2c44STony Nguyen 				status = ice_prof_tcam_ena_dis(hw, blk, false,
3669451f2c44STony Nguyen 							       vsig,
3670451f2c44STony Nguyen 							       &t->tcam[i],
3671451f2c44STony Nguyen 							       chg);
3672451f2c44STony Nguyen 				if (status)
3673451f2c44STony Nguyen 					return status;
3674451f2c44STony Nguyen 			} else if (!test_bit(t->tcam[i].ptg, ptgs_used) &&
3675451f2c44STony Nguyen 				   !t->tcam[i].in_use) {
3676451f2c44STony Nguyen 				/* need to enable this PTG, as it in not in use
3677451f2c44STony Nguyen 				 * and not enabled (highest priority)
3678451f2c44STony Nguyen 				 */
3679451f2c44STony Nguyen 				status = ice_prof_tcam_ena_dis(hw, blk, true,
3680451f2c44STony Nguyen 							       vsig,
3681451f2c44STony Nguyen 							       &t->tcam[i],
3682451f2c44STony Nguyen 							       chg);
3683451f2c44STony Nguyen 				if (status)
3684451f2c44STony Nguyen 					return status;
3685451f2c44STony Nguyen 			}
3686451f2c44STony Nguyen 
3687451f2c44STony Nguyen 			/* keep track of used ptgs */
3688e75ed29dSChristophe JAILLET 			__set_bit(t->tcam[i].ptg, ptgs_used);
3689451f2c44STony Nguyen 		}
3690451f2c44STony Nguyen 	}
3691451f2c44STony Nguyen 
3692451f2c44STony Nguyen 	return 0;
3693451f2c44STony Nguyen }
3694451f2c44STony Nguyen 
3695451f2c44STony Nguyen /**
3696451f2c44STony Nguyen  * ice_add_prof_id_vsig - add profile to VSIG
3697451f2c44STony Nguyen  * @hw: pointer to the HW struct
3698451f2c44STony Nguyen  * @blk: hardware block
3699451f2c44STony Nguyen  * @vsig: the VSIG to which this profile is to be added
3700451f2c44STony Nguyen  * @hdl: the profile handle indicating the profile to add
3701a6892c96SDan Nowlin  * @rev: true to add entries to the end of the list
3702451f2c44STony Nguyen  * @chg: the change list
3703451f2c44STony Nguyen  */
37045e24d598STony Nguyen static int
ice_add_prof_id_vsig(struct ice_hw * hw,enum ice_block blk,u16 vsig,u64 hdl,bool rev,struct list_head * chg)3705451f2c44STony Nguyen ice_add_prof_id_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsig, u64 hdl,
3706a6892c96SDan Nowlin 		     bool rev, struct list_head *chg)
3707451f2c44STony Nguyen {
3708451f2c44STony Nguyen 	/* Masks that ignore flags */
3709451f2c44STony Nguyen 	u8 vl_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
3710451f2c44STony Nguyen 	u8 dc_msk[ICE_TCAM_KEY_VAL_SZ] = { 0xFF, 0xFF, 0x00, 0x00, 0x00 };
3711451f2c44STony Nguyen 	u8 nm_msk[ICE_TCAM_KEY_VAL_SZ] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
3712451f2c44STony Nguyen 	struct ice_prof_map *map;
3713451f2c44STony Nguyen 	struct ice_vsig_prof *t;
3714451f2c44STony Nguyen 	struct ice_chs_chg *p;
3715a6892c96SDan Nowlin 	u16 vsig_idx, i;
37165518ac2aSTony Nguyen 	int status = 0;
3717451f2c44STony Nguyen 
3718451f2c44STony Nguyen 	/* Error, if this VSIG already has this profile */
3719451f2c44STony Nguyen 	if (ice_has_prof_vsig(hw, blk, vsig, hdl))
3720d54699e2STony Nguyen 		return -EEXIST;
3721451f2c44STony Nguyen 
3722451f2c44STony Nguyen 	/* new VSIG profile structure */
3723451f2c44STony Nguyen 	t = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*t), GFP_KERNEL);
3724451f2c44STony Nguyen 	if (!t)
3725d54699e2STony Nguyen 		return -ENOMEM;
3726451f2c44STony Nguyen 
37276a2c2b2cSVictor Raj 	mutex_lock(&hw->blk[blk].es.prof_map_lock);
37286a2c2b2cSVictor Raj 	/* Get the details on the profile specified by the handle ID */
37296a2c2b2cSVictor Raj 	map = ice_search_prof_id(hw, blk, hdl);
37306a2c2b2cSVictor Raj 	if (!map) {
3731d54699e2STony Nguyen 		status = -ENOENT;
37326a2c2b2cSVictor Raj 		goto err_ice_add_prof_id_vsig;
37336a2c2b2cSVictor Raj 	}
37346a2c2b2cSVictor Raj 
3735451f2c44STony Nguyen 	t->profile_cookie = map->profile_cookie;
3736451f2c44STony Nguyen 	t->prof_id = map->prof_id;
3737451f2c44STony Nguyen 	t->tcam_count = map->ptg_cnt;
3738451f2c44STony Nguyen 
3739451f2c44STony Nguyen 	/* create TCAM entries */
3740451f2c44STony Nguyen 	for (i = 0; i < map->ptg_cnt; i++) {
3741451f2c44STony Nguyen 		u16 tcam_idx;
3742451f2c44STony Nguyen 
3743451f2c44STony Nguyen 		/* add TCAM to change list */
3744451f2c44STony Nguyen 		p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p), GFP_KERNEL);
37456a2c2b2cSVictor Raj 		if (!p) {
3746d54699e2STony Nguyen 			status = -ENOMEM;
3747451f2c44STony Nguyen 			goto err_ice_add_prof_id_vsig;
37486a2c2b2cSVictor Raj 		}
3749451f2c44STony Nguyen 
3750451f2c44STony Nguyen 		/* allocate the TCAM entry index */
3751cbad5db8SQi Zhang 		/* for entries with empty attribute masks, allocate entry from
3752cbad5db8SQi Zhang 		 * the bottom of the TCAM table; otherwise, allocate from the
3753cbad5db8SQi Zhang 		 * top of the table in order to give it higher priority
3754cbad5db8SQi Zhang 		 */
3755cbad5db8SQi Zhang 		status = ice_alloc_tcam_ent(hw, blk, map->attr[i].mask == 0,
3756cbad5db8SQi Zhang 					    &tcam_idx);
3757451f2c44STony Nguyen 		if (status) {
3758451f2c44STony Nguyen 			devm_kfree(ice_hw_to_dev(hw), p);
3759451f2c44STony Nguyen 			goto err_ice_add_prof_id_vsig;
3760451f2c44STony Nguyen 		}
3761451f2c44STony Nguyen 
3762451f2c44STony Nguyen 		t->tcam[i].ptg = map->ptg[i];
3763451f2c44STony Nguyen 		t->tcam[i].prof_id = map->prof_id;
3764451f2c44STony Nguyen 		t->tcam[i].tcam_idx = tcam_idx;
3765cbad5db8SQi Zhang 		t->tcam[i].attr = map->attr[i];
3766451f2c44STony Nguyen 		t->tcam[i].in_use = true;
3767451f2c44STony Nguyen 
3768451f2c44STony Nguyen 		p->type = ICE_TCAM_ADD;
3769451f2c44STony Nguyen 		p->add_tcam_idx = true;
3770451f2c44STony Nguyen 		p->prof_id = t->tcam[i].prof_id;
3771451f2c44STony Nguyen 		p->ptg = t->tcam[i].ptg;
3772451f2c44STony Nguyen 		p->vsig = vsig;
3773451f2c44STony Nguyen 		p->tcam_idx = t->tcam[i].tcam_idx;
3774451f2c44STony Nguyen 
3775451f2c44STony Nguyen 		/* write the TCAM entry */
3776451f2c44STony Nguyen 		status = ice_tcam_write_entry(hw, blk, t->tcam[i].tcam_idx,
3777451f2c44STony Nguyen 					      t->tcam[i].prof_id,
3778451f2c44STony Nguyen 					      t->tcam[i].ptg, vsig, 0, 0,
3779451f2c44STony Nguyen 					      vl_msk, dc_msk, nm_msk);
3780857a4f0eSEric Joyner 		if (status) {
3781857a4f0eSEric Joyner 			devm_kfree(ice_hw_to_dev(hw), p);
3782451f2c44STony Nguyen 			goto err_ice_add_prof_id_vsig;
3783857a4f0eSEric Joyner 		}
3784451f2c44STony Nguyen 
3785451f2c44STony Nguyen 		/* log change */
3786451f2c44STony Nguyen 		list_add(&p->list_entry, chg);
3787451f2c44STony Nguyen 	}
3788451f2c44STony Nguyen 
3789451f2c44STony Nguyen 	/* add profile to VSIG */
3790a6892c96SDan Nowlin 	vsig_idx = vsig & ICE_VSIG_IDX_M;
3791a6892c96SDan Nowlin 	if (rev)
3792a6892c96SDan Nowlin 		list_add_tail(&t->list,
3793a6892c96SDan Nowlin 			      &hw->blk[blk].xlt2.vsig_tbl[vsig_idx].prop_lst);
3794a6892c96SDan Nowlin 	else
3795451f2c44STony Nguyen 		list_add(&t->list,
3796a6892c96SDan Nowlin 			 &hw->blk[blk].xlt2.vsig_tbl[vsig_idx].prop_lst);
3797451f2c44STony Nguyen 
37986a2c2b2cSVictor Raj 	mutex_unlock(&hw->blk[blk].es.prof_map_lock);
37996a2c2b2cSVictor Raj 	return status;
3800451f2c44STony Nguyen 
3801451f2c44STony Nguyen err_ice_add_prof_id_vsig:
38026a2c2b2cSVictor Raj 	mutex_unlock(&hw->blk[blk].es.prof_map_lock);
3803451f2c44STony Nguyen 	/* let caller clean up the change list */
3804451f2c44STony Nguyen 	devm_kfree(ice_hw_to_dev(hw), t);
38056a2c2b2cSVictor Raj 	return status;
3806451f2c44STony Nguyen }
3807451f2c44STony Nguyen 
3808451f2c44STony Nguyen /**
3809451f2c44STony Nguyen  * ice_create_prof_id_vsig - add a new VSIG with a single profile
3810451f2c44STony Nguyen  * @hw: pointer to the HW struct
3811451f2c44STony Nguyen  * @blk: hardware block
3812451f2c44STony Nguyen  * @vsi: the initial VSI that will be in VSIG
3813451f2c44STony Nguyen  * @hdl: the profile handle of the profile that will be added to the VSIG
3814451f2c44STony Nguyen  * @chg: the change list
3815451f2c44STony Nguyen  */
38165e24d598STony Nguyen static int
ice_create_prof_id_vsig(struct ice_hw * hw,enum ice_block blk,u16 vsi,u64 hdl,struct list_head * chg)3817451f2c44STony Nguyen ice_create_prof_id_vsig(struct ice_hw *hw, enum ice_block blk, u16 vsi, u64 hdl,
3818451f2c44STony Nguyen 			struct list_head *chg)
3819451f2c44STony Nguyen {
3820451f2c44STony Nguyen 	struct ice_chs_chg *p;
3821451f2c44STony Nguyen 	u16 new_vsig;
38225518ac2aSTony Nguyen 	int status;
3823451f2c44STony Nguyen 
3824451f2c44STony Nguyen 	p = devm_kzalloc(ice_hw_to_dev(hw), sizeof(*p), GFP_KERNEL);
3825451f2c44STony Nguyen 	if (!p)
3826d54699e2STony Nguyen 		return -ENOMEM;
3827451f2c44STony Nguyen 
3828451f2c44STony Nguyen 	new_vsig = ice_vsig_alloc(hw, blk);
3829451f2c44STony Nguyen 	if (!new_vsig) {
3830d54699e2STony Nguyen 		status = -EIO;
3831451f2c44STony Nguyen 		goto err_ice_create_prof_id_vsig;
3832451f2c44STony Nguyen 	}
3833451f2c44STony Nguyen 
3834451f2c44STony Nguyen 	status = ice_move_vsi(hw, blk, vsi, new_vsig, chg);
3835451f2c44STony Nguyen 	if (status)
3836451f2c44STony Nguyen 		goto err_ice_create_prof_id_vsig;
3837451f2c44STony Nguyen 
3838a6892c96SDan Nowlin 	status = ice_add_prof_id_vsig(hw, blk, new_vsig, hdl, false, chg);
3839451f2c44STony Nguyen 	if (status)
3840451f2c44STony Nguyen 		goto err_ice_create_prof_id_vsig;
3841451f2c44STony Nguyen 
3842451f2c44STony Nguyen 	p->type = ICE_VSIG_ADD;
3843451f2c44STony Nguyen 	p->vsi = vsi;
3844451f2c44STony Nguyen 	p->orig_vsig = ICE_DEFAULT_VSIG;
3845451f2c44STony Nguyen 	p->vsig = new_vsig;
3846451f2c44STony Nguyen 
3847451f2c44STony Nguyen 	list_add(&p->list_entry, chg);
3848451f2c44STony Nguyen 
3849451f2c44STony Nguyen 	return 0;
3850451f2c44STony Nguyen 
3851451f2c44STony Nguyen err_ice_create_prof_id_vsig:
3852451f2c44STony Nguyen 	/* let caller clean up the change list */
3853451f2c44STony Nguyen 	devm_kfree(ice_hw_to_dev(hw), p);
3854451f2c44STony Nguyen 	return status;
3855451f2c44STony Nguyen }
3856451f2c44STony Nguyen 
3857451f2c44STony Nguyen /**
3858451f2c44STony Nguyen  * ice_create_vsig_from_lst - create a new VSIG with a list of profiles
3859451f2c44STony Nguyen  * @hw: pointer to the HW struct
3860451f2c44STony Nguyen  * @blk: hardware block
3861451f2c44STony Nguyen  * @vsi: the initial VSI that will be in VSIG
3862451f2c44STony Nguyen  * @lst: the list of profile that will be added to the VSIG
3863a6892c96SDan Nowlin  * @new_vsig: return of new VSIG
3864451f2c44STony Nguyen  * @chg: the change list
3865451f2c44STony Nguyen  */
38665e24d598STony Nguyen static int
ice_create_vsig_from_lst(struct ice_hw * hw,enum ice_block blk,u16 vsi,struct list_head * lst,u16 * new_vsig,struct list_head * chg)3867451f2c44STony Nguyen ice_create_vsig_from_lst(struct ice_hw *hw, enum ice_block blk, u16 vsi,
3868a6892c96SDan Nowlin 			 struct list_head *lst, u16 *new_vsig,
3869a6892c96SDan Nowlin 			 struct list_head *chg)
3870451f2c44STony Nguyen {
3871451f2c44STony Nguyen 	struct ice_vsig_prof *t;
38725e24d598STony Nguyen 	int status;
3873451f2c44STony Nguyen 	u16 vsig;
3874451f2c44STony Nguyen 
3875451f2c44STony Nguyen 	vsig = ice_vsig_alloc(hw, blk);
3876451f2c44STony Nguyen 	if (!vsig)
3877d54699e2STony Nguyen 		return -EIO;
3878451f2c44STony Nguyen 
3879451f2c44STony Nguyen 	status = ice_move_vsi(hw, blk, vsi, vsig, chg);
3880451f2c44STony Nguyen 	if (status)
3881451f2c44STony Nguyen 		return status;
3882451f2c44STony Nguyen 
3883451f2c44STony Nguyen 	list_for_each_entry(t, lst, list) {
3884a6892c96SDan Nowlin 		/* Reverse the order here since we are copying the list */
3885451f2c44STony Nguyen 		status = ice_add_prof_id_vsig(hw, blk, vsig, t->profile_cookie,
3886a6892c96SDan Nowlin 					      true, chg);
3887451f2c44STony Nguyen 		if (status)
3888451f2c44STony Nguyen 			return status;
3889451f2c44STony Nguyen 	}
3890451f2c44STony Nguyen 
3891a6892c96SDan Nowlin 	*new_vsig = vsig;
3892a6892c96SDan Nowlin 
3893451f2c44STony Nguyen 	return 0;
3894451f2c44STony Nguyen }
3895451f2c44STony Nguyen 
3896451f2c44STony Nguyen /**
3897451f2c44STony Nguyen  * ice_find_prof_vsig - find a VSIG with a specific profile handle
3898451f2c44STony Nguyen  * @hw: pointer to the HW struct
3899451f2c44STony Nguyen  * @blk: hardware block
3900451f2c44STony Nguyen  * @hdl: the profile handle of the profile to search for
3901451f2c44STony Nguyen  * @vsig: returns the VSIG with the matching profile
3902451f2c44STony Nguyen  */
3903451f2c44STony Nguyen static bool
ice_find_prof_vsig(struct ice_hw * hw,enum ice_block blk,u64 hdl,u16 * vsig)3904451f2c44STony Nguyen ice_find_prof_vsig(struct ice_hw *hw, enum ice_block blk, u64 hdl, u16 *vsig)
3905451f2c44STony Nguyen {
3906451f2c44STony Nguyen 	struct ice_vsig_prof *t;
3907451f2c44STony Nguyen 	struct list_head lst;
39085518ac2aSTony Nguyen 	int status;
3909451f2c44STony Nguyen 
3910451f2c44STony Nguyen 	INIT_LIST_HEAD(&lst);
3911451f2c44STony Nguyen 
3912451f2c44STony Nguyen 	t = kzalloc(sizeof(*t), GFP_KERNEL);
3913451f2c44STony Nguyen 	if (!t)
3914451f2c44STony Nguyen 		return false;
3915451f2c44STony Nguyen 
3916451f2c44STony Nguyen 	t->profile_cookie = hdl;
3917451f2c44STony Nguyen 	list_add(&t->list, &lst);
3918451f2c44STony Nguyen 
3919451f2c44STony Nguyen 	status = ice_find_dup_props_vsig(hw, blk, &lst, vsig);
3920451f2c44STony Nguyen 
3921451f2c44STony Nguyen 	list_del(&t->list);
3922451f2c44STony Nguyen 	kfree(t);
3923451f2c44STony Nguyen 
3924451f2c44STony Nguyen 	return !status;
3925451f2c44STony Nguyen }
3926451f2c44STony Nguyen 
3927451f2c44STony Nguyen /**
3928451f2c44STony Nguyen  * ice_add_prof_id_flow - add profile flow
3929451f2c44STony Nguyen  * @hw: pointer to the HW struct
3930451f2c44STony Nguyen  * @blk: hardware block
3931451f2c44STony Nguyen  * @vsi: the VSI to enable with the profile specified by ID
3932451f2c44STony Nguyen  * @hdl: profile handle
3933451f2c44STony Nguyen  *
3934451f2c44STony Nguyen  * Calling this function will update the hardware tables to enable the
3935451f2c44STony Nguyen  * profile indicated by the ID parameter for the VSIs specified in the VSI
3936451f2c44STony Nguyen  * array. Once successfully called, the flow will be enabled.
3937451f2c44STony Nguyen  */
39385e24d598STony Nguyen int
ice_add_prof_id_flow(struct ice_hw * hw,enum ice_block blk,u16 vsi,u64 hdl)3939451f2c44STony Nguyen ice_add_prof_id_flow(struct ice_hw *hw, enum ice_block blk, u16 vsi, u64 hdl)
3940451f2c44STony Nguyen {
3941451f2c44STony Nguyen 	struct ice_vsig_prof *tmp1, *del1;
3942451f2c44STony Nguyen 	struct ice_chs_chg *tmp, *del;
3943451f2c44STony Nguyen 	struct list_head union_lst;
3944451f2c44STony Nguyen 	struct list_head chg;
39455518ac2aSTony Nguyen 	int status;
3946451f2c44STony Nguyen 	u16 vsig;
3947451f2c44STony Nguyen 
3948451f2c44STony Nguyen 	INIT_LIST_HEAD(&union_lst);
3949451f2c44STony Nguyen 	INIT_LIST_HEAD(&chg);
3950451f2c44STony Nguyen 
3951451f2c44STony Nguyen 	/* Get profile */
3952451f2c44STony Nguyen 	status = ice_get_prof(hw, blk, hdl, &chg);
3953451f2c44STony Nguyen 	if (status)
3954451f2c44STony Nguyen 		return status;
3955451f2c44STony Nguyen 
3956451f2c44STony Nguyen 	/* determine if VSI is already part of a VSIG */
3957451f2c44STony Nguyen 	status = ice_vsig_find_vsi(hw, blk, vsi, &vsig);
3958451f2c44STony Nguyen 	if (!status && vsig) {
3959451f2c44STony Nguyen 		bool only_vsi;
3960451f2c44STony Nguyen 		u16 or_vsig;
3961451f2c44STony Nguyen 		u16 ref;
3962451f2c44STony Nguyen 
3963451f2c44STony Nguyen 		/* found in VSIG */
3964451f2c44STony Nguyen 		or_vsig = vsig;
3965451f2c44STony Nguyen 
3966451f2c44STony Nguyen 		/* make sure that there is no overlap/conflict between the new
3967451f2c44STony Nguyen 		 * characteristics and the existing ones; we don't support that
3968451f2c44STony Nguyen 		 * scenario
3969451f2c44STony Nguyen 		 */
3970451f2c44STony Nguyen 		if (ice_has_prof_vsig(hw, blk, vsig, hdl)) {
3971d54699e2STony Nguyen 			status = -EEXIST;
3972451f2c44STony Nguyen 			goto err_ice_add_prof_id_flow;
3973451f2c44STony Nguyen 		}
3974451f2c44STony Nguyen 
3975451f2c44STony Nguyen 		/* last VSI in the VSIG? */
3976451f2c44STony Nguyen 		status = ice_vsig_get_ref(hw, blk, vsig, &ref);
3977451f2c44STony Nguyen 		if (status)
3978451f2c44STony Nguyen 			goto err_ice_add_prof_id_flow;
3979451f2c44STony Nguyen 		only_vsi = (ref == 1);
3980451f2c44STony Nguyen 
3981451f2c44STony Nguyen 		/* create a union of the current profiles and the one being
3982451f2c44STony Nguyen 		 * added
3983451f2c44STony Nguyen 		 */
3984451f2c44STony Nguyen 		status = ice_get_profs_vsig(hw, blk, vsig, &union_lst);
3985451f2c44STony Nguyen 		if (status)
3986451f2c44STony Nguyen 			goto err_ice_add_prof_id_flow;
3987451f2c44STony Nguyen 
3988451f2c44STony Nguyen 		status = ice_add_prof_to_lst(hw, blk, &union_lst, hdl);
3989451f2c44STony Nguyen 		if (status)
3990451f2c44STony Nguyen 			goto err_ice_add_prof_id_flow;
3991451f2c44STony Nguyen 
3992451f2c44STony Nguyen 		/* search for an existing VSIG with an exact charc match */
3993451f2c44STony Nguyen 		status = ice_find_dup_props_vsig(hw, blk, &union_lst, &vsig);
3994451f2c44STony Nguyen 		if (!status) {
3995451f2c44STony Nguyen 			/* move VSI to the VSIG that matches */
3996451f2c44STony Nguyen 			status = ice_move_vsi(hw, blk, vsi, vsig, &chg);
3997451f2c44STony Nguyen 			if (status)
3998451f2c44STony Nguyen 				goto err_ice_add_prof_id_flow;
3999451f2c44STony Nguyen 
4000451f2c44STony Nguyen 			/* VSI has been moved out of or_vsig. If the or_vsig had
4001451f2c44STony Nguyen 			 * only that VSI it is now empty and can be removed.
4002451f2c44STony Nguyen 			 */
4003451f2c44STony Nguyen 			if (only_vsi) {
4004451f2c44STony Nguyen 				status = ice_rem_vsig(hw, blk, or_vsig, &chg);
4005451f2c44STony Nguyen 				if (status)
4006451f2c44STony Nguyen 					goto err_ice_add_prof_id_flow;
4007451f2c44STony Nguyen 			}
4008451f2c44STony Nguyen 		} else if (only_vsi) {
4009451f2c44STony Nguyen 			/* If the original VSIG only contains one VSI, then it
4010451f2c44STony Nguyen 			 * will be the requesting VSI. In this case the VSI is
4011451f2c44STony Nguyen 			 * not sharing entries and we can simply add the new
4012451f2c44STony Nguyen 			 * profile to the VSIG.
4013451f2c44STony Nguyen 			 */
4014a6892c96SDan Nowlin 			status = ice_add_prof_id_vsig(hw, blk, vsig, hdl, false,
4015a6892c96SDan Nowlin 						      &chg);
4016451f2c44STony Nguyen 			if (status)
4017451f2c44STony Nguyen 				goto err_ice_add_prof_id_flow;
4018451f2c44STony Nguyen 
4019451f2c44STony Nguyen 			/* Adjust priorities */
4020451f2c44STony Nguyen 			status = ice_adj_prof_priorities(hw, blk, vsig, &chg);
4021451f2c44STony Nguyen 			if (status)
4022451f2c44STony Nguyen 				goto err_ice_add_prof_id_flow;
4023451f2c44STony Nguyen 		} else {
4024451f2c44STony Nguyen 			/* No match, so we need a new VSIG */
4025451f2c44STony Nguyen 			status = ice_create_vsig_from_lst(hw, blk, vsi,
4026a6892c96SDan Nowlin 							  &union_lst, &vsig,
4027a6892c96SDan Nowlin 							  &chg);
4028451f2c44STony Nguyen 			if (status)
4029451f2c44STony Nguyen 				goto err_ice_add_prof_id_flow;
4030451f2c44STony Nguyen 
4031451f2c44STony Nguyen 			/* Adjust priorities */
4032451f2c44STony Nguyen 			status = ice_adj_prof_priorities(hw, blk, vsig, &chg);
4033451f2c44STony Nguyen 			if (status)
4034451f2c44STony Nguyen 				goto err_ice_add_prof_id_flow;
4035451f2c44STony Nguyen 		}
4036451f2c44STony Nguyen 	} else {
4037451f2c44STony Nguyen 		/* need to find or add a VSIG */
4038451f2c44STony Nguyen 		/* search for an existing VSIG with an exact charc match */
4039451f2c44STony Nguyen 		if (ice_find_prof_vsig(hw, blk, hdl, &vsig)) {
4040451f2c44STony Nguyen 			/* found an exact match */
4041451f2c44STony Nguyen 			/* add or move VSI to the VSIG that matches */
4042451f2c44STony Nguyen 			status = ice_move_vsi(hw, blk, vsi, vsig, &chg);
4043451f2c44STony Nguyen 			if (status)
4044451f2c44STony Nguyen 				goto err_ice_add_prof_id_flow;
4045451f2c44STony Nguyen 		} else {
4046451f2c44STony Nguyen 			/* we did not find an exact match */
4047451f2c44STony Nguyen 			/* we need to add a VSIG */
4048451f2c44STony Nguyen 			status = ice_create_prof_id_vsig(hw, blk, vsi, hdl,
4049451f2c44STony Nguyen 							 &chg);
4050451f2c44STony Nguyen 			if (status)
4051451f2c44STony Nguyen 				goto err_ice_add_prof_id_flow;
4052451f2c44STony Nguyen 		}
4053451f2c44STony Nguyen 	}
4054451f2c44STony Nguyen 
405543dbfc7bSTony Nguyen 	/* update hardware */
405643dbfc7bSTony Nguyen 	if (!status)
405743dbfc7bSTony Nguyen 		status = ice_upd_prof_hw(hw, blk, &chg);
405843dbfc7bSTony Nguyen 
4059451f2c44STony Nguyen err_ice_add_prof_id_flow:
4060451f2c44STony Nguyen 	list_for_each_entry_safe(del, tmp, &chg, list_entry) {
4061451f2c44STony Nguyen 		list_del(&del->list_entry);
4062451f2c44STony Nguyen 		devm_kfree(ice_hw_to_dev(hw), del);
4063451f2c44STony Nguyen 	}
4064451f2c44STony Nguyen 
4065451f2c44STony Nguyen 	list_for_each_entry_safe(del1, tmp1, &union_lst, list) {
4066451f2c44STony Nguyen 		list_del(&del1->list);
4067451f2c44STony Nguyen 		devm_kfree(ice_hw_to_dev(hw), del1);
4068451f2c44STony Nguyen 	}
4069451f2c44STony Nguyen 
4070451f2c44STony Nguyen 	return status;
4071451f2c44STony Nguyen }
40722c61054cSTony Nguyen 
40732c61054cSTony Nguyen /**
40742c61054cSTony Nguyen  * ice_rem_prof_from_list - remove a profile from list
40752c61054cSTony Nguyen  * @hw: pointer to the HW struct
40762c61054cSTony Nguyen  * @lst: list to remove the profile from
40772c61054cSTony Nguyen  * @hdl: the profile handle indicating the profile to remove
40782c61054cSTony Nguyen  */
40795e24d598STony Nguyen static int
ice_rem_prof_from_list(struct ice_hw * hw,struct list_head * lst,u64 hdl)40802c61054cSTony Nguyen ice_rem_prof_from_list(struct ice_hw *hw, struct list_head *lst, u64 hdl)
40812c61054cSTony Nguyen {
40822c61054cSTony Nguyen 	struct ice_vsig_prof *ent, *tmp;
40832c61054cSTony Nguyen 
40842c61054cSTony Nguyen 	list_for_each_entry_safe(ent, tmp, lst, list)
40852c61054cSTony Nguyen 		if (ent->profile_cookie == hdl) {
40862c61054cSTony Nguyen 			list_del(&ent->list);
40872c61054cSTony Nguyen 			devm_kfree(ice_hw_to_dev(hw), ent);
40882c61054cSTony Nguyen 			return 0;
40892c61054cSTony Nguyen 		}
40902c61054cSTony Nguyen 
4091d54699e2STony Nguyen 	return -ENOENT;
40922c61054cSTony Nguyen }
40932c61054cSTony Nguyen 
40942c61054cSTony Nguyen /**
40952c61054cSTony Nguyen  * ice_rem_prof_id_flow - remove flow
40962c61054cSTony Nguyen  * @hw: pointer to the HW struct
40972c61054cSTony Nguyen  * @blk: hardware block
40982c61054cSTony Nguyen  * @vsi: the VSI from which to remove the profile specified by ID
40992c61054cSTony Nguyen  * @hdl: profile tracking handle
41002c61054cSTony Nguyen  *
41012c61054cSTony Nguyen  * Calling this function will update the hardware tables to remove the
41022c61054cSTony Nguyen  * profile indicated by the ID parameter for the VSIs specified in the VSI
41032c61054cSTony Nguyen  * array. Once successfully called, the flow will be disabled.
41042c61054cSTony Nguyen  */
41055e24d598STony Nguyen int
ice_rem_prof_id_flow(struct ice_hw * hw,enum ice_block blk,u16 vsi,u64 hdl)41062c61054cSTony Nguyen ice_rem_prof_id_flow(struct ice_hw *hw, enum ice_block blk, u16 vsi, u64 hdl)
41072c61054cSTony Nguyen {
41082c61054cSTony Nguyen 	struct ice_vsig_prof *tmp1, *del1;
41092c61054cSTony Nguyen 	struct ice_chs_chg *tmp, *del;
41102c61054cSTony Nguyen 	struct list_head chg, copy;
41115e24d598STony Nguyen 	int status;
41122c61054cSTony Nguyen 	u16 vsig;
41132c61054cSTony Nguyen 
41142c61054cSTony Nguyen 	INIT_LIST_HEAD(&copy);
41152c61054cSTony Nguyen 	INIT_LIST_HEAD(&chg);
41162c61054cSTony Nguyen 
41172c61054cSTony Nguyen 	/* determine if VSI is already part of a VSIG */
41182c61054cSTony Nguyen 	status = ice_vsig_find_vsi(hw, blk, vsi, &vsig);
41192c61054cSTony Nguyen 	if (!status && vsig) {
41202c61054cSTony Nguyen 		bool last_profile;
41212c61054cSTony Nguyen 		bool only_vsi;
41222c61054cSTony Nguyen 		u16 ref;
41232c61054cSTony Nguyen 
41242c61054cSTony Nguyen 		/* found in VSIG */
41252c61054cSTony Nguyen 		last_profile = ice_vsig_prof_id_count(hw, blk, vsig) == 1;
41262c61054cSTony Nguyen 		status = ice_vsig_get_ref(hw, blk, vsig, &ref);
41272c61054cSTony Nguyen 		if (status)
41282c61054cSTony Nguyen 			goto err_ice_rem_prof_id_flow;
41292c61054cSTony Nguyen 		only_vsi = (ref == 1);
41302c61054cSTony Nguyen 
41312c61054cSTony Nguyen 		if (only_vsi) {
41322c61054cSTony Nguyen 			/* If the original VSIG only contains one reference,
41332c61054cSTony Nguyen 			 * which will be the requesting VSI, then the VSI is not
41342c61054cSTony Nguyen 			 * sharing entries and we can simply remove the specific
41352c61054cSTony Nguyen 			 * characteristics from the VSIG.
41362c61054cSTony Nguyen 			 */
41372c61054cSTony Nguyen 
41382c61054cSTony Nguyen 			if (last_profile) {
41392c61054cSTony Nguyen 				/* If there are no profiles left for this VSIG,
4140ac382a09SBruce Allan 				 * then simply remove the VSIG.
41412c61054cSTony Nguyen 				 */
41422c61054cSTony Nguyen 				status = ice_rem_vsig(hw, blk, vsig, &chg);
41432c61054cSTony Nguyen 				if (status)
41442c61054cSTony Nguyen 					goto err_ice_rem_prof_id_flow;
41452c61054cSTony Nguyen 			} else {
41462c61054cSTony Nguyen 				status = ice_rem_prof_id_vsig(hw, blk, vsig,
41472c61054cSTony Nguyen 							      hdl, &chg);
41482c61054cSTony Nguyen 				if (status)
41492c61054cSTony Nguyen 					goto err_ice_rem_prof_id_flow;
41502c61054cSTony Nguyen 
41512c61054cSTony Nguyen 				/* Adjust priorities */
41522c61054cSTony Nguyen 				status = ice_adj_prof_priorities(hw, blk, vsig,
41532c61054cSTony Nguyen 								 &chg);
41542c61054cSTony Nguyen 				if (status)
41552c61054cSTony Nguyen 					goto err_ice_rem_prof_id_flow;
41562c61054cSTony Nguyen 			}
41572c61054cSTony Nguyen 
41582c61054cSTony Nguyen 		} else {
41592c61054cSTony Nguyen 			/* Make a copy of the VSIG's list of Profiles */
41602c61054cSTony Nguyen 			status = ice_get_profs_vsig(hw, blk, vsig, &copy);
41612c61054cSTony Nguyen 			if (status)
41622c61054cSTony Nguyen 				goto err_ice_rem_prof_id_flow;
41632c61054cSTony Nguyen 
41642c61054cSTony Nguyen 			/* Remove specified profile entry from the list */
41652c61054cSTony Nguyen 			status = ice_rem_prof_from_list(hw, &copy, hdl);
41662c61054cSTony Nguyen 			if (status)
41672c61054cSTony Nguyen 				goto err_ice_rem_prof_id_flow;
41682c61054cSTony Nguyen 
41692c61054cSTony Nguyen 			if (list_empty(&copy)) {
41702c61054cSTony Nguyen 				status = ice_move_vsi(hw, blk, vsi,
41712c61054cSTony Nguyen 						      ICE_DEFAULT_VSIG, &chg);
41722c61054cSTony Nguyen 				if (status)
41732c61054cSTony Nguyen 					goto err_ice_rem_prof_id_flow;
41742c61054cSTony Nguyen 
41752c61054cSTony Nguyen 			} else if (!ice_find_dup_props_vsig(hw, blk, &copy,
41762c61054cSTony Nguyen 							    &vsig)) {
41772c61054cSTony Nguyen 				/* found an exact match */
41782c61054cSTony Nguyen 				/* add or move VSI to the VSIG that matches */
41792c61054cSTony Nguyen 				/* Search for a VSIG with a matching profile
41802c61054cSTony Nguyen 				 * list
41812c61054cSTony Nguyen 				 */
41822c61054cSTony Nguyen 
41832c61054cSTony Nguyen 				/* Found match, move VSI to the matching VSIG */
41842c61054cSTony Nguyen 				status = ice_move_vsi(hw, blk, vsi, vsig, &chg);
41852c61054cSTony Nguyen 				if (status)
41862c61054cSTony Nguyen 					goto err_ice_rem_prof_id_flow;
41872c61054cSTony Nguyen 			} else {
41882c61054cSTony Nguyen 				/* since no existing VSIG supports this
41892c61054cSTony Nguyen 				 * characteristic pattern, we need to create a
41902c61054cSTony Nguyen 				 * new VSIG and TCAM entries
41912c61054cSTony Nguyen 				 */
41922c61054cSTony Nguyen 				status = ice_create_vsig_from_lst(hw, blk, vsi,
4193a6892c96SDan Nowlin 								  &copy, &vsig,
4194a6892c96SDan Nowlin 								  &chg);
41952c61054cSTony Nguyen 				if (status)
41962c61054cSTony Nguyen 					goto err_ice_rem_prof_id_flow;
41972c61054cSTony Nguyen 
41982c61054cSTony Nguyen 				/* Adjust priorities */
41992c61054cSTony Nguyen 				status = ice_adj_prof_priorities(hw, blk, vsig,
42002c61054cSTony Nguyen 								 &chg);
42012c61054cSTony Nguyen 				if (status)
42022c61054cSTony Nguyen 					goto err_ice_rem_prof_id_flow;
42032c61054cSTony Nguyen 			}
42042c61054cSTony Nguyen 		}
42052c61054cSTony Nguyen 	} else {
4206d54699e2STony Nguyen 		status = -ENOENT;
42072c61054cSTony Nguyen 	}
42082c61054cSTony Nguyen 
42092c61054cSTony Nguyen 	/* update hardware tables */
42102c61054cSTony Nguyen 	if (!status)
42112c61054cSTony Nguyen 		status = ice_upd_prof_hw(hw, blk, &chg);
42122c61054cSTony Nguyen 
42132c61054cSTony Nguyen err_ice_rem_prof_id_flow:
42142c61054cSTony Nguyen 	list_for_each_entry_safe(del, tmp, &chg, list_entry) {
42152c61054cSTony Nguyen 		list_del(&del->list_entry);
42162c61054cSTony Nguyen 		devm_kfree(ice_hw_to_dev(hw), del);
42172c61054cSTony Nguyen 	}
42182c61054cSTony Nguyen 
42192c61054cSTony Nguyen 	list_for_each_entry_safe(del1, tmp1, &copy, list) {
42202c61054cSTony Nguyen 		list_del(&del1->list);
42212c61054cSTony Nguyen 		devm_kfree(ice_hw_to_dev(hw), del1);
42222c61054cSTony Nguyen 	}
42232c61054cSTony Nguyen 
42242c61054cSTony Nguyen 	return status;
42252c61054cSTony Nguyen }
4226