xref: /linux/drivers/net/ethernet/intel/ice/ice_parser.c (revision 9410645520e9b820069761f3450ef6661418e279)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2024 Intel Corporation */
3 
4 #include "ice_common.h"
5 
6 struct ice_pkg_sect_hdr {
7 	__le16 count;
8 	__le16 offset;
9 };
10 
11 /**
12  * ice_parser_sect_item_get - parse an item from a section
13  * @sect_type: section type
14  * @section: section object
15  * @index: index of the item to get
16  * @offset: dummy as prototype of ice_pkg_enum_entry's last parameter
17  *
18  * Return: a pointer to the item or NULL.
19  */
ice_parser_sect_item_get(u32 sect_type,void * section,u32 index,u32 __maybe_unused * offset)20 static void *ice_parser_sect_item_get(u32 sect_type, void *section,
21 				      u32 index, u32 __maybe_unused *offset)
22 {
23 	size_t data_off = ICE_SEC_DATA_OFFSET;
24 	struct ice_pkg_sect_hdr *hdr;
25 	size_t size;
26 
27 	if (!section)
28 		return NULL;
29 
30 	switch (sect_type) {
31 	case ICE_SID_RXPARSER_IMEM:
32 		size = ICE_SID_RXPARSER_IMEM_ENTRY_SIZE;
33 		break;
34 	case ICE_SID_RXPARSER_METADATA_INIT:
35 		size = ICE_SID_RXPARSER_METADATA_INIT_ENTRY_SIZE;
36 		break;
37 	case ICE_SID_RXPARSER_CAM:
38 		size = ICE_SID_RXPARSER_CAM_ENTRY_SIZE;
39 		break;
40 	case ICE_SID_RXPARSER_PG_SPILL:
41 		size = ICE_SID_RXPARSER_PG_SPILL_ENTRY_SIZE;
42 		break;
43 	case ICE_SID_RXPARSER_NOMATCH_CAM:
44 		size = ICE_SID_RXPARSER_NOMATCH_CAM_ENTRY_SIZE;
45 		break;
46 	case ICE_SID_RXPARSER_NOMATCH_SPILL:
47 		size = ICE_SID_RXPARSER_NOMATCH_SPILL_ENTRY_SIZE;
48 		break;
49 	case ICE_SID_RXPARSER_BOOST_TCAM:
50 		size = ICE_SID_RXPARSER_BOOST_TCAM_ENTRY_SIZE;
51 		break;
52 	case ICE_SID_LBL_RXPARSER_TMEM:
53 		data_off = ICE_SEC_LBL_DATA_OFFSET;
54 		size = ICE_SID_LBL_ENTRY_SIZE;
55 		break;
56 	case ICE_SID_RXPARSER_MARKER_PTYPE:
57 		size = ICE_SID_RXPARSER_MARKER_TYPE_ENTRY_SIZE;
58 		break;
59 	case ICE_SID_RXPARSER_MARKER_GRP:
60 		size = ICE_SID_RXPARSER_MARKER_GRP_ENTRY_SIZE;
61 		break;
62 	case ICE_SID_RXPARSER_PROTO_GRP:
63 		size = ICE_SID_RXPARSER_PROTO_GRP_ENTRY_SIZE;
64 		break;
65 	case ICE_SID_RXPARSER_FLAG_REDIR:
66 		size = ICE_SID_RXPARSER_FLAG_REDIR_ENTRY_SIZE;
67 		break;
68 	default:
69 		return NULL;
70 	}
71 
72 	hdr = section;
73 	if (index >= le16_to_cpu(hdr->count))
74 		return NULL;
75 
76 	return section + data_off + index * size;
77 }
78 
79 /**
80  * ice_parser_create_table - create an item table from a section
81  * @hw: pointer to the hardware structure
82  * @sect_type: section type
83  * @item_size: item size in bytes
84  * @length: number of items in the table to create
85  * @parse_item: the function to parse the item
86  * @no_offset: ignore header offset, calculate index from 0
87  *
88  * Return: a pointer to the allocated table or ERR_PTR.
89  */
90 static void *
ice_parser_create_table(struct ice_hw * hw,u32 sect_type,u32 item_size,u32 length,void (* parse_item)(struct ice_hw * hw,u16 idx,void * item,void * data,int size),bool no_offset)91 ice_parser_create_table(struct ice_hw *hw, u32 sect_type,
92 			u32 item_size, u32 length,
93 			void (*parse_item)(struct ice_hw *hw, u16 idx,
94 					   void *item, void *data,
95 					   int size), bool no_offset)
96 {
97 	struct ice_pkg_enum state = {};
98 	struct ice_seg *seg = hw->seg;
99 	void *table, *data, *item;
100 	u16 idx = 0;
101 
102 	if (!seg)
103 		return ERR_PTR(-EINVAL);
104 
105 	table = kzalloc(item_size * length, GFP_KERNEL);
106 	if (!table)
107 		return ERR_PTR(-ENOMEM);
108 
109 	do {
110 		data = ice_pkg_enum_entry(seg, &state, sect_type, NULL,
111 					  ice_parser_sect_item_get);
112 		seg = NULL;
113 		if (data) {
114 			struct ice_pkg_sect_hdr *hdr = state.sect;
115 
116 			if (!no_offset)
117 				idx = le16_to_cpu(hdr->offset) +
118 					state.entry_idx;
119 
120 			item = (void *)((uintptr_t)table + idx * item_size);
121 			parse_item(hw, idx, item, data, item_size);
122 
123 			if (no_offset)
124 				idx++;
125 		}
126 	} while (data);
127 
128 	return table;
129 }
130 
131 /*** ICE_SID_RXPARSER_IMEM section ***/
ice_imem_bst_bm_dump(struct ice_hw * hw,struct ice_bst_main * bm)132 static void ice_imem_bst_bm_dump(struct ice_hw *hw, struct ice_bst_main *bm)
133 {
134 	struct device *dev = ice_hw_to_dev(hw);
135 
136 	dev_info(dev, "boost main:\n");
137 	dev_info(dev, "\talu0 = %d\n", bm->alu0);
138 	dev_info(dev, "\talu1 = %d\n", bm->alu1);
139 	dev_info(dev, "\talu2 = %d\n", bm->alu2);
140 	dev_info(dev, "\tpg = %d\n", bm->pg);
141 }
142 
ice_imem_bst_kb_dump(struct ice_hw * hw,struct ice_bst_keybuilder * kb)143 static void ice_imem_bst_kb_dump(struct ice_hw *hw,
144 				 struct ice_bst_keybuilder *kb)
145 {
146 	struct device *dev = ice_hw_to_dev(hw);
147 
148 	dev_info(dev, "boost key builder:\n");
149 	dev_info(dev, "\tpriority = %d\n", kb->prio);
150 	dev_info(dev, "\ttsr_ctrl = %d\n", kb->tsr_ctrl);
151 }
152 
ice_imem_np_kb_dump(struct ice_hw * hw,struct ice_np_keybuilder * kb)153 static void ice_imem_np_kb_dump(struct ice_hw *hw,
154 				struct ice_np_keybuilder *kb)
155 {
156 	struct device *dev = ice_hw_to_dev(hw);
157 
158 	dev_info(dev, "next proto key builder:\n");
159 	dev_info(dev, "\topc = %d\n", kb->opc);
160 	dev_info(dev, "\tstart_or_reg0 = %d\n", kb->start_reg0);
161 	dev_info(dev, "\tlen_or_reg1 = %d\n", kb->len_reg1);
162 }
163 
ice_imem_pg_kb_dump(struct ice_hw * hw,struct ice_pg_keybuilder * kb)164 static void ice_imem_pg_kb_dump(struct ice_hw *hw,
165 				struct ice_pg_keybuilder *kb)
166 {
167 	struct device *dev = ice_hw_to_dev(hw);
168 
169 	dev_info(dev, "parse graph key builder:\n");
170 	dev_info(dev, "\tflag0_ena = %d\n", kb->flag0_ena);
171 	dev_info(dev, "\tflag1_ena = %d\n", kb->flag1_ena);
172 	dev_info(dev, "\tflag2_ena = %d\n", kb->flag2_ena);
173 	dev_info(dev, "\tflag3_ena = %d\n", kb->flag3_ena);
174 	dev_info(dev, "\tflag0_idx = %d\n", kb->flag0_idx);
175 	dev_info(dev, "\tflag1_idx = %d\n", kb->flag1_idx);
176 	dev_info(dev, "\tflag2_idx = %d\n", kb->flag2_idx);
177 	dev_info(dev, "\tflag3_idx = %d\n", kb->flag3_idx);
178 	dev_info(dev, "\talu_reg_idx = %d\n", kb->alu_reg_idx);
179 }
180 
ice_imem_alu_dump(struct ice_hw * hw,struct ice_alu * alu,int index)181 static void ice_imem_alu_dump(struct ice_hw *hw,
182 			      struct ice_alu *alu, int index)
183 {
184 	struct device *dev = ice_hw_to_dev(hw);
185 
186 	dev_info(dev, "alu%d:\n", index);
187 	dev_info(dev, "\topc = %d\n", alu->opc);
188 	dev_info(dev, "\tsrc_start = %d\n", alu->src_start);
189 	dev_info(dev, "\tsrc_len = %d\n", alu->src_len);
190 	dev_info(dev, "\tshift_xlate_sel = %d\n", alu->shift_xlate_sel);
191 	dev_info(dev, "\tshift_xlate_key = %d\n", alu->shift_xlate_key);
192 	dev_info(dev, "\tsrc_reg_id = %d\n", alu->src_reg_id);
193 	dev_info(dev, "\tdst_reg_id = %d\n", alu->dst_reg_id);
194 	dev_info(dev, "\tinc0 = %d\n", alu->inc0);
195 	dev_info(dev, "\tinc1 = %d\n", alu->inc1);
196 	dev_info(dev, "\tproto_offset_opc = %d\n", alu->proto_offset_opc);
197 	dev_info(dev, "\tproto_offset = %d\n", alu->proto_offset);
198 	dev_info(dev, "\tbranch_addr = %d\n", alu->branch_addr);
199 	dev_info(dev, "\timm = %d\n", alu->imm);
200 	dev_info(dev, "\tdst_start = %d\n", alu->dst_start);
201 	dev_info(dev, "\tdst_len = %d\n", alu->dst_len);
202 	dev_info(dev, "\tflags_extr_imm = %d\n", alu->flags_extr_imm);
203 	dev_info(dev, "\tflags_start_imm= %d\n", alu->flags_start_imm);
204 }
205 
206 /**
207  * ice_imem_dump - dump an imem item info
208  * @hw: pointer to the hardware structure
209  * @item: imem item to dump
210  */
ice_imem_dump(struct ice_hw * hw,struct ice_imem_item * item)211 static void ice_imem_dump(struct ice_hw *hw, struct ice_imem_item *item)
212 {
213 	struct device *dev = ice_hw_to_dev(hw);
214 
215 	dev_info(dev, "index = %d\n", item->idx);
216 	ice_imem_bst_bm_dump(hw, &item->b_m);
217 	ice_imem_bst_kb_dump(hw, &item->b_kb);
218 	dev_info(dev, "pg priority = %d\n", item->pg_prio);
219 	ice_imem_np_kb_dump(hw, &item->np_kb);
220 	ice_imem_pg_kb_dump(hw, &item->pg_kb);
221 	ice_imem_alu_dump(hw, &item->alu0, 0);
222 	ice_imem_alu_dump(hw, &item->alu1, 1);
223 	ice_imem_alu_dump(hw, &item->alu2, 2);
224 }
225 
226 #define ICE_IM_BM_ALU0		BIT(0)
227 #define ICE_IM_BM_ALU1		BIT(1)
228 #define ICE_IM_BM_ALU2		BIT(2)
229 #define ICE_IM_BM_PG		BIT(3)
230 
231 /**
232  * ice_imem_bm_init - parse 4 bits of Boost Main
233  * @bm: pointer to the Boost Main structure
234  * @data: Boost Main data to be parsed
235  */
ice_imem_bm_init(struct ice_bst_main * bm,u8 data)236 static void ice_imem_bm_init(struct ice_bst_main *bm, u8 data)
237 {
238 	bm->alu0	= FIELD_GET(ICE_IM_BM_ALU0, data);
239 	bm->alu1	= FIELD_GET(ICE_IM_BM_ALU1, data);
240 	bm->alu2	= FIELD_GET(ICE_IM_BM_ALU2, data);
241 	bm->pg		= FIELD_GET(ICE_IM_BM_PG, data);
242 }
243 
244 #define ICE_IM_BKB_PRIO		GENMASK(7, 0)
245 #define ICE_IM_BKB_TSR_CTRL	BIT(8)
246 
247 /**
248  * ice_imem_bkb_init - parse 10 bits of Boost Main Build
249  * @bkb: pointer to the Boost Main Build structure
250  * @data: Boost Main Build data to be parsed
251  */
ice_imem_bkb_init(struct ice_bst_keybuilder * bkb,u16 data)252 static void ice_imem_bkb_init(struct ice_bst_keybuilder *bkb, u16 data)
253 {
254 	bkb->prio	= FIELD_GET(ICE_IM_BKB_PRIO, data);
255 	bkb->tsr_ctrl	= FIELD_GET(ICE_IM_BKB_TSR_CTRL, data);
256 }
257 
258 #define ICE_IM_NPKB_OPC		GENMASK(1, 0)
259 #define ICE_IM_NPKB_S_R0	GENMASK(9, 2)
260 #define ICE_IM_NPKB_L_R1	GENMASK(17, 10)
261 
262 /**
263  * ice_imem_npkb_init - parse 18 bits of Next Protocol Key Build
264  * @kb: pointer to the Next Protocol Key Build structure
265  * @data: Next Protocol Key Build data to be parsed
266  */
ice_imem_npkb_init(struct ice_np_keybuilder * kb,u32 data)267 static void ice_imem_npkb_init(struct ice_np_keybuilder *kb, u32 data)
268 {
269 	kb->opc		= FIELD_GET(ICE_IM_NPKB_OPC, data);
270 	kb->start_reg0	= FIELD_GET(ICE_IM_NPKB_S_R0, data);
271 	kb->len_reg1	= FIELD_GET(ICE_IM_NPKB_L_R1, data);
272 }
273 
274 #define ICE_IM_PGKB_F0_ENA	BIT_ULL(0)
275 #define ICE_IM_PGKB_F0_IDX	GENMASK_ULL(6, 1)
276 #define ICE_IM_PGKB_F1_ENA	BIT_ULL(7)
277 #define ICE_IM_PGKB_F1_IDX	GENMASK_ULL(13, 8)
278 #define ICE_IM_PGKB_F2_ENA	BIT_ULL(14)
279 #define ICE_IM_PGKB_F2_IDX	GENMASK_ULL(20, 15)
280 #define ICE_IM_PGKB_F3_ENA	BIT_ULL(21)
281 #define ICE_IM_PGKB_F3_IDX	GENMASK_ULL(27, 22)
282 #define ICE_IM_PGKB_AR_IDX	GENMASK_ULL(34, 28)
283 
284 /**
285  * ice_imem_pgkb_init - parse 35 bits of Parse Graph Key Build
286  * @kb: pointer to the Parse Graph Key Build structure
287  * @data: Parse Graph Key Build data to be parsed
288  */
ice_imem_pgkb_init(struct ice_pg_keybuilder * kb,u64 data)289 static void ice_imem_pgkb_init(struct ice_pg_keybuilder *kb, u64 data)
290 {
291 	kb->flag0_ena	= FIELD_GET(ICE_IM_PGKB_F0_ENA, data);
292 	kb->flag0_idx	= FIELD_GET(ICE_IM_PGKB_F0_IDX, data);
293 	kb->flag1_ena	= FIELD_GET(ICE_IM_PGKB_F1_ENA, data);
294 	kb->flag1_idx	= FIELD_GET(ICE_IM_PGKB_F1_IDX, data);
295 	kb->flag2_ena	= FIELD_GET(ICE_IM_PGKB_F2_ENA, data);
296 	kb->flag2_idx	= FIELD_GET(ICE_IM_PGKB_F2_IDX, data);
297 	kb->flag3_ena	= FIELD_GET(ICE_IM_PGKB_F3_ENA, data);
298 	kb->flag3_idx	= FIELD_GET(ICE_IM_PGKB_F3_IDX, data);
299 	kb->alu_reg_idx	= FIELD_GET(ICE_IM_PGKB_AR_IDX, data);
300 }
301 
302 #define ICE_IM_ALU_OPC		GENMASK_ULL(5, 0)
303 #define ICE_IM_ALU_SS		GENMASK_ULL(13, 6)
304 #define ICE_IM_ALU_SL		GENMASK_ULL(18, 14)
305 #define ICE_IM_ALU_SXS		BIT_ULL(19)
306 #define ICE_IM_ALU_SXK		GENMASK_ULL(23, 20)
307 #define ICE_IM_ALU_SRID		GENMASK_ULL(30, 24)
308 #define ICE_IM_ALU_DRID		GENMASK_ULL(37, 31)
309 #define ICE_IM_ALU_INC0		BIT_ULL(38)
310 #define ICE_IM_ALU_INC1		BIT_ULL(39)
311 #define ICE_IM_ALU_POO		GENMASK_ULL(41, 40)
312 #define ICE_IM_ALU_PO		GENMASK_ULL(49, 42)
313 #define ICE_IM_ALU_BA_S		50	/* offset for the 2nd 64-bits field */
314 #define ICE_IM_ALU_BA		GENMASK_ULL(57 - ICE_IM_ALU_BA_S, \
315 					    50 - ICE_IM_ALU_BA_S)
316 #define ICE_IM_ALU_IMM		GENMASK_ULL(73 - ICE_IM_ALU_BA_S, \
317 					    58 - ICE_IM_ALU_BA_S)
318 #define ICE_IM_ALU_DFE		BIT_ULL(74 - ICE_IM_ALU_BA_S)
319 #define ICE_IM_ALU_DS		GENMASK_ULL(80 - ICE_IM_ALU_BA_S, \
320 					    75 - ICE_IM_ALU_BA_S)
321 #define ICE_IM_ALU_DL		GENMASK_ULL(86 - ICE_IM_ALU_BA_S, \
322 					    81 - ICE_IM_ALU_BA_S)
323 #define ICE_IM_ALU_FEI		BIT_ULL(87 - ICE_IM_ALU_BA_S)
324 #define ICE_IM_ALU_FSI		GENMASK_ULL(95 - ICE_IM_ALU_BA_S, \
325 					    88 - ICE_IM_ALU_BA_S)
326 
327 /**
328  * ice_imem_alu_init - parse 96 bits of ALU entry
329  * @alu: pointer to the ALU entry structure
330  * @data: ALU entry data to be parsed
331  * @off: offset of the ALU entry data
332  */
ice_imem_alu_init(struct ice_alu * alu,u8 * data,u8 off)333 static void ice_imem_alu_init(struct ice_alu *alu, u8 *data, u8 off)
334 {
335 	u64 d64;
336 	u8 idd;
337 
338 	d64 = *((u64 *)data) >> off;
339 
340 	alu->opc		= FIELD_GET(ICE_IM_ALU_OPC, d64);
341 	alu->src_start		= FIELD_GET(ICE_IM_ALU_SS, d64);
342 	alu->src_len		= FIELD_GET(ICE_IM_ALU_SL, d64);
343 	alu->shift_xlate_sel	= FIELD_GET(ICE_IM_ALU_SXS, d64);
344 	alu->shift_xlate_key	= FIELD_GET(ICE_IM_ALU_SXK, d64);
345 	alu->src_reg_id		= FIELD_GET(ICE_IM_ALU_SRID, d64);
346 	alu->dst_reg_id		= FIELD_GET(ICE_IM_ALU_DRID, d64);
347 	alu->inc0		= FIELD_GET(ICE_IM_ALU_INC0, d64);
348 	alu->inc1		= FIELD_GET(ICE_IM_ALU_INC1, d64);
349 	alu->proto_offset_opc	= FIELD_GET(ICE_IM_ALU_POO, d64);
350 	alu->proto_offset	= FIELD_GET(ICE_IM_ALU_PO, d64);
351 
352 	idd = (ICE_IM_ALU_BA_S + off) / BITS_PER_BYTE;
353 	off = (ICE_IM_ALU_BA_S + off) % BITS_PER_BYTE;
354 	d64 = *((u64 *)(&data[idd])) >> off;
355 
356 	alu->branch_addr	= FIELD_GET(ICE_IM_ALU_BA, d64);
357 	alu->imm		= FIELD_GET(ICE_IM_ALU_IMM, d64);
358 	alu->dedicate_flags_ena	= FIELD_GET(ICE_IM_ALU_DFE, d64);
359 	alu->dst_start		= FIELD_GET(ICE_IM_ALU_DS, d64);
360 	alu->dst_len		= FIELD_GET(ICE_IM_ALU_DL, d64);
361 	alu->flags_extr_imm	= FIELD_GET(ICE_IM_ALU_FEI, d64);
362 	alu->flags_start_imm	= FIELD_GET(ICE_IM_ALU_FSI, d64);
363 }
364 
365 #define ICE_IMEM_BM_S		0
366 #define ICE_IMEM_BKB_S		4
367 #define ICE_IMEM_BKB_IDD	(ICE_IMEM_BKB_S / BITS_PER_BYTE)
368 #define ICE_IMEM_BKB_OFF	(ICE_IMEM_BKB_S % BITS_PER_BYTE)
369 #define ICE_IMEM_PGP		GENMASK(15, 14)
370 #define ICE_IMEM_NPKB_S		16
371 #define ICE_IMEM_NPKB_IDD	(ICE_IMEM_NPKB_S / BITS_PER_BYTE)
372 #define ICE_IMEM_NPKB_OFF	(ICE_IMEM_NPKB_S % BITS_PER_BYTE)
373 #define ICE_IMEM_PGKB_S		34
374 #define ICE_IMEM_PGKB_IDD	(ICE_IMEM_PGKB_S / BITS_PER_BYTE)
375 #define ICE_IMEM_PGKB_OFF	(ICE_IMEM_PGKB_S % BITS_PER_BYTE)
376 #define ICE_IMEM_ALU0_S		69
377 #define ICE_IMEM_ALU0_IDD	(ICE_IMEM_ALU0_S / BITS_PER_BYTE)
378 #define ICE_IMEM_ALU0_OFF	(ICE_IMEM_ALU0_S % BITS_PER_BYTE)
379 #define ICE_IMEM_ALU1_S		165
380 #define ICE_IMEM_ALU1_IDD	(ICE_IMEM_ALU1_S / BITS_PER_BYTE)
381 #define ICE_IMEM_ALU1_OFF	(ICE_IMEM_ALU1_S % BITS_PER_BYTE)
382 #define ICE_IMEM_ALU2_S		357
383 #define ICE_IMEM_ALU2_IDD	(ICE_IMEM_ALU2_S / BITS_PER_BYTE)
384 #define ICE_IMEM_ALU2_OFF	(ICE_IMEM_ALU2_S % BITS_PER_BYTE)
385 
386 /**
387  * ice_imem_parse_item - parse 384 bits of IMEM entry
388  * @hw: pointer to the hardware structure
389  * @idx: index of IMEM entry
390  * @item: item of IMEM entry
391  * @data: IMEM entry data to be parsed
392  * @size: size of IMEM entry
393  */
ice_imem_parse_item(struct ice_hw * hw,u16 idx,void * item,void * data,int __maybe_unused size)394 static void ice_imem_parse_item(struct ice_hw *hw, u16 idx, void *item,
395 				void *data, int __maybe_unused size)
396 {
397 	struct ice_imem_item *ii = item;
398 	u8 *buf = data;
399 
400 	ii->idx = idx;
401 
402 	ice_imem_bm_init(&ii->b_m, *(u8 *)buf);
403 	ice_imem_bkb_init(&ii->b_kb,
404 			  *((u16 *)(&buf[ICE_IMEM_BKB_IDD])) >>
405 			   ICE_IMEM_BKB_OFF);
406 
407 	ii->pg_prio = FIELD_GET(ICE_IMEM_PGP, *(u16 *)buf);
408 
409 	ice_imem_npkb_init(&ii->np_kb,
410 			   *((u32 *)(&buf[ICE_IMEM_NPKB_IDD])) >>
411 			    ICE_IMEM_NPKB_OFF);
412 	ice_imem_pgkb_init(&ii->pg_kb,
413 			   *((u64 *)(&buf[ICE_IMEM_PGKB_IDD])) >>
414 			    ICE_IMEM_PGKB_OFF);
415 
416 	ice_imem_alu_init(&ii->alu0,
417 			  &buf[ICE_IMEM_ALU0_IDD],
418 			  ICE_IMEM_ALU0_OFF);
419 	ice_imem_alu_init(&ii->alu1,
420 			  &buf[ICE_IMEM_ALU1_IDD],
421 			  ICE_IMEM_ALU1_OFF);
422 	ice_imem_alu_init(&ii->alu2,
423 			  &buf[ICE_IMEM_ALU2_IDD],
424 			  ICE_IMEM_ALU2_OFF);
425 
426 	if (hw->debug_mask & ICE_DBG_PARSER)
427 		ice_imem_dump(hw, ii);
428 }
429 
430 /**
431  * ice_imem_table_get - create an imem table
432  * @hw: pointer to the hardware structure
433  *
434  * Return: a pointer to the allocated IMEM table.
435  */
ice_imem_table_get(struct ice_hw * hw)436 static struct ice_imem_item *ice_imem_table_get(struct ice_hw *hw)
437 {
438 	return ice_parser_create_table(hw, ICE_SID_RXPARSER_IMEM,
439 				       sizeof(struct ice_imem_item),
440 				       ICE_IMEM_TABLE_SIZE,
441 				       ice_imem_parse_item, false);
442 }
443 
444 /*** ICE_SID_RXPARSER_METADATA_INIT section ***/
445 /**
446  * ice_metainit_dump - dump an metainit item info
447  * @hw: pointer to the hardware structure
448  * @item: metainit item to dump
449  */
ice_metainit_dump(struct ice_hw * hw,struct ice_metainit_item * item)450 static void ice_metainit_dump(struct ice_hw *hw, struct ice_metainit_item *item)
451 {
452 	struct device *dev = ice_hw_to_dev(hw);
453 
454 	dev_info(dev, "index = %d\n", item->idx);
455 
456 	dev_info(dev, "tsr = %d\n", item->tsr);
457 	dev_info(dev, "ho = %d\n", item->ho);
458 	dev_info(dev, "pc = %d\n", item->pc);
459 	dev_info(dev, "pg_rn = %d\n", item->pg_rn);
460 	dev_info(dev, "cd = %d\n", item->cd);
461 
462 	dev_info(dev, "gpr_a_ctrl = %d\n", item->gpr_a_ctrl);
463 	dev_info(dev, "gpr_a_data_mdid = %d\n", item->gpr_a_data_mdid);
464 	dev_info(dev, "gpr_a_data_start = %d\n", item->gpr_a_data_start);
465 	dev_info(dev, "gpr_a_data_len = %d\n", item->gpr_a_data_len);
466 	dev_info(dev, "gpr_a_id = %d\n", item->gpr_a_id);
467 
468 	dev_info(dev, "gpr_b_ctrl = %d\n", item->gpr_b_ctrl);
469 	dev_info(dev, "gpr_b_data_mdid = %d\n", item->gpr_b_data_mdid);
470 	dev_info(dev, "gpr_b_data_start = %d\n", item->gpr_b_data_start);
471 	dev_info(dev, "gpr_b_data_len = %d\n", item->gpr_b_data_len);
472 	dev_info(dev, "gpr_b_id = %d\n", item->gpr_b_id);
473 
474 	dev_info(dev, "gpr_c_ctrl = %d\n", item->gpr_c_ctrl);
475 	dev_info(dev, "gpr_c_data_mdid = %d\n", item->gpr_c_data_mdid);
476 	dev_info(dev, "gpr_c_data_start = %d\n", item->gpr_c_data_start);
477 	dev_info(dev, "gpr_c_data_len = %d\n", item->gpr_c_data_len);
478 	dev_info(dev, "gpr_c_id = %d\n", item->gpr_c_id);
479 
480 	dev_info(dev, "gpr_d_ctrl = %d\n", item->gpr_d_ctrl);
481 	dev_info(dev, "gpr_d_data_mdid = %d\n", item->gpr_d_data_mdid);
482 	dev_info(dev, "gpr_d_data_start = %d\n", item->gpr_d_data_start);
483 	dev_info(dev, "gpr_d_data_len = %d\n", item->gpr_d_data_len);
484 	dev_info(dev, "gpr_d_id = %d\n", item->gpr_d_id);
485 
486 	dev_info(dev, "flags = 0x%llx\n", (unsigned long long)(item->flags));
487 }
488 
489 #define ICE_MI_TSR		GENMASK_ULL(7, 0)
490 #define ICE_MI_HO		GENMASK_ULL(16, 8)
491 #define ICE_MI_PC		GENMASK_ULL(24, 17)
492 #define ICE_MI_PGRN		GENMASK_ULL(35, 25)
493 #define ICE_MI_CD		GENMASK_ULL(38, 36)
494 #define ICE_MI_GAC		BIT_ULL(39)
495 #define ICE_MI_GADM		GENMASK_ULL(44, 40)
496 #define ICE_MI_GADS		GENMASK_ULL(48, 45)
497 #define ICE_MI_GADL		GENMASK_ULL(53, 49)
498 #define ICE_MI_GAI		GENMASK_ULL(59, 56)
499 #define ICE_MI_GBC		BIT_ULL(60)
500 #define ICE_MI_GBDM_S		61	/* offset for the 2nd 64-bits field */
501 #define ICE_MI_GBDM_IDD		(ICE_MI_GBDM_S / BITS_PER_BYTE)
502 #define ICE_MI_GBDM_OFF		(ICE_MI_GBDM_S % BITS_PER_BYTE)
503 
504 #define ICE_MI_GBDM_GENMASK_ULL(high, low) \
505 	GENMASK_ULL((high) - ICE_MI_GBDM_S, (low) - ICE_MI_GBDM_S)
506 #define ICE_MI_GBDM		ICE_MI_GBDM_GENMASK_ULL(65, 61)
507 #define ICE_MI_GBDS		ICE_MI_GBDM_GENMASK_ULL(69, 66)
508 #define ICE_MI_GBDL		ICE_MI_GBDM_GENMASK_ULL(74, 70)
509 #define ICE_MI_GBI		ICE_MI_GBDM_GENMASK_ULL(80, 77)
510 #define ICE_MI_GCC		BIT_ULL(81 - ICE_MI_GBDM_S)
511 #define ICE_MI_GCDM		ICE_MI_GBDM_GENMASK_ULL(86, 82)
512 #define ICE_MI_GCDS		ICE_MI_GBDM_GENMASK_ULL(90, 87)
513 #define ICE_MI_GCDL		ICE_MI_GBDM_GENMASK_ULL(95, 91)
514 #define ICE_MI_GCI		ICE_MI_GBDM_GENMASK_ULL(101, 98)
515 #define ICE_MI_GDC		BIT_ULL(102 - ICE_MI_GBDM_S)
516 #define ICE_MI_GDDM		ICE_MI_GBDM_GENMASK_ULL(107, 103)
517 #define ICE_MI_GDDS		ICE_MI_GBDM_GENMASK_ULL(111, 108)
518 #define ICE_MI_GDDL		ICE_MI_GBDM_GENMASK_ULL(116, 112)
519 #define ICE_MI_GDI		ICE_MI_GBDM_GENMASK_ULL(122, 119)
520 #define ICE_MI_FLAG_S		123	/* offset for the 3rd 64-bits field */
521 #define ICE_MI_FLAG_IDD		(ICE_MI_FLAG_S / BITS_PER_BYTE)
522 #define ICE_MI_FLAG_OFF		(ICE_MI_FLAG_S % BITS_PER_BYTE)
523 #define ICE_MI_FLAG		GENMASK_ULL(186 - ICE_MI_FLAG_S, \
524 					    123 - ICE_MI_FLAG_S)
525 
526 /**
527  * ice_metainit_parse_item - parse 192 bits of Metadata Init entry
528  * @hw: pointer to the hardware structure
529  * @idx: index of Metadata Init entry
530  * @item: item of Metadata Init entry
531  * @data: Metadata Init entry data to be parsed
532  * @size: size of Metadata Init entry
533  */
ice_metainit_parse_item(struct ice_hw * hw,u16 idx,void * item,void * data,int __maybe_unused size)534 static void ice_metainit_parse_item(struct ice_hw *hw, u16 idx, void *item,
535 				    void *data, int __maybe_unused size)
536 {
537 	struct ice_metainit_item *mi = item;
538 	u8 *buf = data;
539 	u64 d64;
540 
541 	mi->idx = idx;
542 
543 	d64 = *(u64 *)buf;
544 
545 	mi->tsr			= FIELD_GET(ICE_MI_TSR, d64);
546 	mi->ho			= FIELD_GET(ICE_MI_HO, d64);
547 	mi->pc			= FIELD_GET(ICE_MI_PC, d64);
548 	mi->pg_rn		= FIELD_GET(ICE_MI_PGRN, d64);
549 	mi->cd			= FIELD_GET(ICE_MI_CD, d64);
550 
551 	mi->gpr_a_ctrl		= FIELD_GET(ICE_MI_GAC, d64);
552 	mi->gpr_a_data_mdid	= FIELD_GET(ICE_MI_GADM, d64);
553 	mi->gpr_a_data_start	= FIELD_GET(ICE_MI_GADS, d64);
554 	mi->gpr_a_data_len	= FIELD_GET(ICE_MI_GADL, d64);
555 	mi->gpr_a_id		= FIELD_GET(ICE_MI_GAI, d64);
556 
557 	mi->gpr_b_ctrl		= FIELD_GET(ICE_MI_GBC, d64);
558 
559 	d64 = *((u64 *)&buf[ICE_MI_GBDM_IDD]) >> ICE_MI_GBDM_OFF;
560 
561 	mi->gpr_b_data_mdid	= FIELD_GET(ICE_MI_GBDM, d64);
562 	mi->gpr_b_data_start	= FIELD_GET(ICE_MI_GBDS, d64);
563 	mi->gpr_b_data_len	= FIELD_GET(ICE_MI_GBDL, d64);
564 	mi->gpr_b_id		= FIELD_GET(ICE_MI_GBI, d64);
565 
566 	mi->gpr_c_ctrl		= FIELD_GET(ICE_MI_GCC, d64);
567 	mi->gpr_c_data_mdid	= FIELD_GET(ICE_MI_GCDM, d64);
568 	mi->gpr_c_data_start	= FIELD_GET(ICE_MI_GCDS, d64);
569 	mi->gpr_c_data_len	= FIELD_GET(ICE_MI_GCDL, d64);
570 	mi->gpr_c_id		= FIELD_GET(ICE_MI_GCI, d64);
571 
572 	mi->gpr_d_ctrl		= FIELD_GET(ICE_MI_GDC, d64);
573 	mi->gpr_d_data_mdid	= FIELD_GET(ICE_MI_GDDM, d64);
574 	mi->gpr_d_data_start	= FIELD_GET(ICE_MI_GDDS, d64);
575 	mi->gpr_d_data_len	= FIELD_GET(ICE_MI_GDDL, d64);
576 	mi->gpr_d_id		= FIELD_GET(ICE_MI_GDI, d64);
577 
578 	d64 = *((u64 *)&buf[ICE_MI_FLAG_IDD]) >> ICE_MI_FLAG_OFF;
579 
580 	mi->flags		= FIELD_GET(ICE_MI_FLAG, d64);
581 
582 	if (hw->debug_mask & ICE_DBG_PARSER)
583 		ice_metainit_dump(hw, mi);
584 }
585 
586 /**
587  * ice_metainit_table_get - create a metainit table
588  * @hw: pointer to the hardware structure
589  *
590  * Return: a pointer to the allocated Metadata initialization table.
591  */
ice_metainit_table_get(struct ice_hw * hw)592 static struct ice_metainit_item *ice_metainit_table_get(struct ice_hw *hw)
593 {
594 	return ice_parser_create_table(hw, ICE_SID_RXPARSER_METADATA_INIT,
595 				       sizeof(struct ice_metainit_item),
596 				       ICE_METAINIT_TABLE_SIZE,
597 				       ice_metainit_parse_item, false);
598 }
599 
600 /**
601  * ice_bst_tcam_search - find a TCAM item with specific type
602  * @tcam_table: the TCAM table
603  * @lbl_table: the lbl table to search
604  * @type: the type we need to match against
605  * @start: start searching from this index
606  *
607  * Return: a pointer to the matching BOOST TCAM item or NULL.
608  */
609 struct ice_bst_tcam_item *
ice_bst_tcam_search(struct ice_bst_tcam_item * tcam_table,struct ice_lbl_item * lbl_table,enum ice_lbl_type type,u16 * start)610 ice_bst_tcam_search(struct ice_bst_tcam_item *tcam_table,
611 		    struct ice_lbl_item *lbl_table,
612 		    enum ice_lbl_type type, u16 *start)
613 {
614 	u16 i = *start;
615 
616 	for (; i < ICE_BST_TCAM_TABLE_SIZE; i++) {
617 		if (lbl_table[i].type == type) {
618 			*start = i;
619 			return &tcam_table[lbl_table[i].idx];
620 		}
621 	}
622 
623 	return NULL;
624 }
625 
626 /*** ICE_SID_RXPARSER_CAM, ICE_SID_RXPARSER_PG_SPILL,
627  *    ICE_SID_RXPARSER_NOMATCH_CAM and ICE_SID_RXPARSER_NOMATCH_CAM
628  *    sections ***/
ice_pg_cam_key_dump(struct ice_hw * hw,struct ice_pg_cam_key * key)629 static void ice_pg_cam_key_dump(struct ice_hw *hw, struct ice_pg_cam_key *key)
630 {
631 	struct device *dev = ice_hw_to_dev(hw);
632 
633 	dev_info(dev, "key:\n");
634 	dev_info(dev, "\tvalid = %d\n", key->valid);
635 	dev_info(dev, "\tnode_id = %d\n", key->node_id);
636 	dev_info(dev, "\tflag0 = %d\n", key->flag0);
637 	dev_info(dev, "\tflag1 = %d\n", key->flag1);
638 	dev_info(dev, "\tflag2 = %d\n", key->flag2);
639 	dev_info(dev, "\tflag3 = %d\n", key->flag3);
640 	dev_info(dev, "\tboost_idx = %d\n", key->boost_idx);
641 	dev_info(dev, "\talu_reg = 0x%04x\n", key->alu_reg);
642 	dev_info(dev, "\tnext_proto = 0x%08x\n", key->next_proto);
643 }
644 
ice_pg_nm_cam_key_dump(struct ice_hw * hw,struct ice_pg_nm_cam_key * key)645 static void ice_pg_nm_cam_key_dump(struct ice_hw *hw,
646 				   struct ice_pg_nm_cam_key *key)
647 {
648 	struct device *dev = ice_hw_to_dev(hw);
649 
650 	dev_info(dev, "key:\n");
651 	dev_info(dev, "\tvalid = %d\n", key->valid);
652 	dev_info(dev, "\tnode_id = %d\n", key->node_id);
653 	dev_info(dev, "\tflag0 = %d\n", key->flag0);
654 	dev_info(dev, "\tflag1 = %d\n", key->flag1);
655 	dev_info(dev, "\tflag2 = %d\n", key->flag2);
656 	dev_info(dev, "\tflag3 = %d\n", key->flag3);
657 	dev_info(dev, "\tboost_idx = %d\n", key->boost_idx);
658 	dev_info(dev, "\talu_reg = 0x%04x\n", key->alu_reg);
659 }
660 
ice_pg_cam_action_dump(struct ice_hw * hw,struct ice_pg_cam_action * action)661 static void ice_pg_cam_action_dump(struct ice_hw *hw,
662 				   struct ice_pg_cam_action *action)
663 {
664 	struct device *dev = ice_hw_to_dev(hw);
665 
666 	dev_info(dev, "action:\n");
667 	dev_info(dev, "\tnext_node = %d\n", action->next_node);
668 	dev_info(dev, "\tnext_pc = %d\n", action->next_pc);
669 	dev_info(dev, "\tis_pg = %d\n", action->is_pg);
670 	dev_info(dev, "\tproto_id = %d\n", action->proto_id);
671 	dev_info(dev, "\tis_mg = %d\n", action->is_mg);
672 	dev_info(dev, "\tmarker_id = %d\n", action->marker_id);
673 	dev_info(dev, "\tis_last_round = %d\n", action->is_last_round);
674 	dev_info(dev, "\tho_polarity = %d\n", action->ho_polarity);
675 	dev_info(dev, "\tho_inc = %d\n", action->ho_inc);
676 }
677 
678 /**
679  * ice_pg_cam_dump - dump an parse graph cam info
680  * @hw: pointer to the hardware structure
681  * @item: parse graph cam to dump
682  */
ice_pg_cam_dump(struct ice_hw * hw,struct ice_pg_cam_item * item)683 static void ice_pg_cam_dump(struct ice_hw *hw, struct ice_pg_cam_item *item)
684 {
685 	dev_info(ice_hw_to_dev(hw), "index = %d\n", item->idx);
686 	ice_pg_cam_key_dump(hw, &item->key);
687 	ice_pg_cam_action_dump(hw, &item->action);
688 }
689 
690 /**
691  * ice_pg_nm_cam_dump - dump an parse graph no match cam info
692  * @hw: pointer to the hardware structure
693  * @item: parse graph no match cam to dump
694  */
ice_pg_nm_cam_dump(struct ice_hw * hw,struct ice_pg_nm_cam_item * item)695 static void ice_pg_nm_cam_dump(struct ice_hw *hw,
696 			       struct ice_pg_nm_cam_item *item)
697 {
698 	dev_info(ice_hw_to_dev(hw), "index = %d\n", item->idx);
699 	ice_pg_nm_cam_key_dump(hw, &item->key);
700 	ice_pg_cam_action_dump(hw, &item->action);
701 }
702 
703 #define ICE_PGCA_NN	GENMASK_ULL(10, 0)
704 #define ICE_PGCA_NPC	GENMASK_ULL(18, 11)
705 #define ICE_PGCA_IPG	BIT_ULL(19)
706 #define ICE_PGCA_PID	GENMASK_ULL(30, 23)
707 #define ICE_PGCA_IMG	BIT_ULL(31)
708 #define ICE_PGCA_MID	GENMASK_ULL(39, 32)
709 #define ICE_PGCA_ILR	BIT_ULL(40)
710 #define ICE_PGCA_HOP	BIT_ULL(41)
711 #define ICE_PGCA_HOI	GENMASK_ULL(50, 42)
712 
713 /**
714  * ice_pg_cam_action_init - parse 55 bits of Parse Graph CAM Action
715  * @action: pointer to the Parse Graph CAM Action structure
716  * @data: Parse Graph CAM Action data to be parsed
717  */
ice_pg_cam_action_init(struct ice_pg_cam_action * action,u64 data)718 static void ice_pg_cam_action_init(struct ice_pg_cam_action *action, u64 data)
719 {
720 	action->next_node	= FIELD_GET(ICE_PGCA_NN, data);
721 	action->next_pc		= FIELD_GET(ICE_PGCA_NPC, data);
722 	action->is_pg		= FIELD_GET(ICE_PGCA_IPG, data);
723 	action->proto_id	= FIELD_GET(ICE_PGCA_PID, data);
724 	action->is_mg		= FIELD_GET(ICE_PGCA_IMG, data);
725 	action->marker_id	= FIELD_GET(ICE_PGCA_MID, data);
726 	action->is_last_round	= FIELD_GET(ICE_PGCA_ILR, data);
727 	action->ho_polarity	= FIELD_GET(ICE_PGCA_HOP, data);
728 	action->ho_inc		= FIELD_GET(ICE_PGCA_HOI, data);
729 }
730 
731 #define ICE_PGNCK_VLD		BIT_ULL(0)
732 #define ICE_PGNCK_NID		GENMASK_ULL(11, 1)
733 #define ICE_PGNCK_F0		BIT_ULL(12)
734 #define ICE_PGNCK_F1		BIT_ULL(13)
735 #define ICE_PGNCK_F2		BIT_ULL(14)
736 #define ICE_PGNCK_F3		BIT_ULL(15)
737 #define ICE_PGNCK_BH		BIT_ULL(16)
738 #define ICE_PGNCK_BI		GENMASK_ULL(24, 17)
739 #define ICE_PGNCK_AR		GENMASK_ULL(40, 25)
740 
741 /**
742  * ice_pg_nm_cam_key_init - parse 41 bits of Parse Graph NoMatch CAM Key
743  * @key: pointer to the Parse Graph NoMatch CAM Key structure
744  * @data: Parse Graph NoMatch CAM Key data to be parsed
745  */
ice_pg_nm_cam_key_init(struct ice_pg_nm_cam_key * key,u64 data)746 static void ice_pg_nm_cam_key_init(struct ice_pg_nm_cam_key *key, u64 data)
747 {
748 	key->valid	= FIELD_GET(ICE_PGNCK_VLD, data);
749 	key->node_id	= FIELD_GET(ICE_PGNCK_NID, data);
750 	key->flag0	= FIELD_GET(ICE_PGNCK_F0, data);
751 	key->flag1	= FIELD_GET(ICE_PGNCK_F1, data);
752 	key->flag2	= FIELD_GET(ICE_PGNCK_F2, data);
753 	key->flag3	= FIELD_GET(ICE_PGNCK_F3, data);
754 
755 	if (FIELD_GET(ICE_PGNCK_BH, data))
756 		key->boost_idx = FIELD_GET(ICE_PGNCK_BI, data);
757 	else
758 		key->boost_idx = 0;
759 
760 	key->alu_reg	= FIELD_GET(ICE_PGNCK_AR, data);
761 }
762 
763 #define ICE_PGCK_VLD		BIT_ULL(0)
764 #define ICE_PGCK_NID		GENMASK_ULL(11, 1)
765 #define ICE_PGCK_F0		BIT_ULL(12)
766 #define ICE_PGCK_F1		BIT_ULL(13)
767 #define ICE_PGCK_F2		BIT_ULL(14)
768 #define ICE_PGCK_F3		BIT_ULL(15)
769 #define ICE_PGCK_BH		BIT_ULL(16)
770 #define ICE_PGCK_BI		GENMASK_ULL(24, 17)
771 #define ICE_PGCK_AR		GENMASK_ULL(40, 25)
772 #define ICE_PGCK_NPK_S		41	/* offset for the 2nd 64-bits field */
773 #define ICE_PGCK_NPK_IDD	(ICE_PGCK_NPK_S / BITS_PER_BYTE)
774 #define ICE_PGCK_NPK_OFF	(ICE_PGCK_NPK_S % BITS_PER_BYTE)
775 #define ICE_PGCK_NPK		GENMASK_ULL(72 - ICE_PGCK_NPK_S, \
776 					    41 - ICE_PGCK_NPK_S)
777 
778 /**
779  * ice_pg_cam_key_init - parse 73 bits of Parse Graph CAM Key
780  * @key: pointer to the Parse Graph CAM Key structure
781  * @data: Parse Graph CAM Key data to be parsed
782  */
ice_pg_cam_key_init(struct ice_pg_cam_key * key,u8 * data)783 static void ice_pg_cam_key_init(struct ice_pg_cam_key *key, u8 *data)
784 {
785 	u64 d64 = *(u64 *)data;
786 
787 	key->valid	= FIELD_GET(ICE_PGCK_VLD, d64);
788 	key->node_id	= FIELD_GET(ICE_PGCK_NID, d64);
789 	key->flag0	= FIELD_GET(ICE_PGCK_F0, d64);
790 	key->flag1	= FIELD_GET(ICE_PGCK_F1, d64);
791 	key->flag2	= FIELD_GET(ICE_PGCK_F2, d64);
792 	key->flag3	= FIELD_GET(ICE_PGCK_F3, d64);
793 
794 	if (FIELD_GET(ICE_PGCK_BH, d64))
795 		key->boost_idx = FIELD_GET(ICE_PGCK_BI, d64);
796 	else
797 		key->boost_idx = 0;
798 
799 	key->alu_reg	= FIELD_GET(ICE_PGCK_AR, d64);
800 
801 	d64 = *((u64 *)&data[ICE_PGCK_NPK_IDD]) >> ICE_PGCK_NPK_OFF;
802 
803 	key->next_proto	= FIELD_GET(ICE_PGCK_NPK, d64);
804 }
805 
806 #define ICE_PG_CAM_ACT_S	73
807 #define ICE_PG_CAM_ACT_IDD	(ICE_PG_CAM_ACT_S / BITS_PER_BYTE)
808 #define ICE_PG_CAM_ACT_OFF	(ICE_PG_CAM_ACT_S % BITS_PER_BYTE)
809 
810 /**
811  * ice_pg_cam_parse_item - parse 128 bits of Parse Graph CAM Entry
812  * @hw: pointer to the hardware structure
813  * @idx: index of Parse Graph CAM Entry
814  * @item: item of Parse Graph CAM Entry
815  * @data: Parse Graph CAM Entry data to be parsed
816  * @size: size of Parse Graph CAM Entry
817  */
ice_pg_cam_parse_item(struct ice_hw * hw,u16 idx,void * item,void * data,int __maybe_unused size)818 static void ice_pg_cam_parse_item(struct ice_hw *hw, u16 idx, void *item,
819 				  void *data, int __maybe_unused size)
820 {
821 	struct ice_pg_cam_item *ci = item;
822 	u8 *buf = data;
823 	u64 d64;
824 
825 	ci->idx = idx;
826 
827 	ice_pg_cam_key_init(&ci->key, buf);
828 
829 	d64 = *((u64 *)&buf[ICE_PG_CAM_ACT_IDD]) >> ICE_PG_CAM_ACT_OFF;
830 	ice_pg_cam_action_init(&ci->action, d64);
831 
832 	if (hw->debug_mask & ICE_DBG_PARSER)
833 		ice_pg_cam_dump(hw, ci);
834 }
835 
836 #define ICE_PG_SP_CAM_KEY_S	56
837 #define ICE_PG_SP_CAM_KEY_IDD	(ICE_PG_SP_CAM_KEY_S / BITS_PER_BYTE)
838 
839 /**
840  * ice_pg_sp_cam_parse_item - parse 136 bits of Parse Graph Spill CAM Entry
841  * @hw: pointer to the hardware structure
842  * @idx: index of Parse Graph Spill CAM Entry
843  * @item: item of Parse Graph Spill CAM Entry
844  * @data: Parse Graph Spill CAM Entry data to be parsed
845  * @size: size of Parse Graph Spill CAM Entry
846  */
ice_pg_sp_cam_parse_item(struct ice_hw * hw,u16 idx,void * item,void * data,int __maybe_unused size)847 static void ice_pg_sp_cam_parse_item(struct ice_hw *hw, u16 idx, void *item,
848 				     void *data, int __maybe_unused size)
849 {
850 	struct ice_pg_cam_item *ci = item;
851 	u8 *buf = data;
852 	u64 d64;
853 
854 	ci->idx = idx;
855 
856 	d64 = *(u64 *)buf;
857 	ice_pg_cam_action_init(&ci->action, d64);
858 
859 	ice_pg_cam_key_init(&ci->key, &buf[ICE_PG_SP_CAM_KEY_IDD]);
860 
861 	if (hw->debug_mask & ICE_DBG_PARSER)
862 		ice_pg_cam_dump(hw, ci);
863 }
864 
865 #define ICE_PG_NM_CAM_ACT_S	41
866 #define ICE_PG_NM_CAM_ACT_IDD	(ICE_PG_NM_CAM_ACT_S / BITS_PER_BYTE)
867 #define ICE_PG_NM_CAM_ACT_OFF   (ICE_PG_NM_CAM_ACT_S % BITS_PER_BYTE)
868 
869 /**
870  * ice_pg_nm_cam_parse_item - parse 96 bits of Parse Graph NoMatch CAM Entry
871  * @hw: pointer to the hardware structure
872  * @idx: index of Parse Graph NoMatch CAM Entry
873  * @item: item of Parse Graph NoMatch CAM Entry
874  * @data: Parse Graph NoMatch CAM Entry data to be parsed
875  * @size: size of Parse Graph NoMatch CAM Entry
876  */
ice_pg_nm_cam_parse_item(struct ice_hw * hw,u16 idx,void * item,void * data,int __maybe_unused size)877 static void ice_pg_nm_cam_parse_item(struct ice_hw *hw, u16 idx, void *item,
878 				     void *data, int __maybe_unused size)
879 {
880 	struct ice_pg_nm_cam_item *ci = item;
881 	u8 *buf = data;
882 	u64 d64;
883 
884 	ci->idx = idx;
885 
886 	d64 = *(u64 *)buf;
887 	ice_pg_nm_cam_key_init(&ci->key, d64);
888 
889 	d64 = *((u64 *)&buf[ICE_PG_NM_CAM_ACT_IDD]) >> ICE_PG_NM_CAM_ACT_OFF;
890 	ice_pg_cam_action_init(&ci->action, d64);
891 
892 	if (hw->debug_mask & ICE_DBG_PARSER)
893 		ice_pg_nm_cam_dump(hw, ci);
894 }
895 
896 #define ICE_PG_NM_SP_CAM_ACT_S		56
897 #define ICE_PG_NM_SP_CAM_ACT_IDD	(ICE_PG_NM_SP_CAM_ACT_S / BITS_PER_BYTE)
898 #define ICE_PG_NM_SP_CAM_ACT_OFF	(ICE_PG_NM_SP_CAM_ACT_S % BITS_PER_BYTE)
899 
900 /**
901  * ice_pg_nm_sp_cam_parse_item - parse 104 bits of Parse Graph NoMatch Spill
902  *  CAM Entry
903  * @hw: pointer to the hardware structure
904  * @idx: index of Parse Graph NoMatch Spill CAM Entry
905  * @item: item of Parse Graph NoMatch Spill CAM Entry
906  * @data: Parse Graph NoMatch Spill CAM Entry data to be parsed
907  * @size: size of Parse Graph NoMatch Spill CAM Entry
908  */
ice_pg_nm_sp_cam_parse_item(struct ice_hw * hw,u16 idx,void * item,void * data,int __maybe_unused size)909 static void ice_pg_nm_sp_cam_parse_item(struct ice_hw *hw, u16 idx,
910 					void *item, void *data,
911 					int __maybe_unused size)
912 {
913 	struct ice_pg_nm_cam_item *ci = item;
914 	u8 *buf = data;
915 	u64 d64;
916 
917 	ci->idx = idx;
918 
919 	d64 = *(u64 *)buf;
920 	ice_pg_cam_action_init(&ci->action, d64);
921 
922 	d64 = *((u64 *)&buf[ICE_PG_NM_SP_CAM_ACT_IDD]) >>
923 		ICE_PG_NM_SP_CAM_ACT_OFF;
924 	ice_pg_nm_cam_key_init(&ci->key, d64);
925 
926 	if (hw->debug_mask & ICE_DBG_PARSER)
927 		ice_pg_nm_cam_dump(hw, ci);
928 }
929 
930 /**
931  * ice_pg_cam_table_get - create a parse graph cam table
932  * @hw: pointer to the hardware structure
933  *
934  * Return: a pointer to the allocated Parse Graph CAM table.
935  */
ice_pg_cam_table_get(struct ice_hw * hw)936 static struct ice_pg_cam_item *ice_pg_cam_table_get(struct ice_hw *hw)
937 {
938 	return ice_parser_create_table(hw, ICE_SID_RXPARSER_CAM,
939 				       sizeof(struct ice_pg_cam_item),
940 				       ICE_PG_CAM_TABLE_SIZE,
941 				       ice_pg_cam_parse_item, false);
942 }
943 
944 /**
945  * ice_pg_sp_cam_table_get - create a parse graph spill cam table
946  * @hw: pointer to the hardware structure
947  *
948  * Return: a pointer to the allocated Parse Graph Spill CAM table.
949  */
ice_pg_sp_cam_table_get(struct ice_hw * hw)950 static struct ice_pg_cam_item *ice_pg_sp_cam_table_get(struct ice_hw *hw)
951 {
952 	return ice_parser_create_table(hw, ICE_SID_RXPARSER_PG_SPILL,
953 				       sizeof(struct ice_pg_cam_item),
954 				       ICE_PG_SP_CAM_TABLE_SIZE,
955 				       ice_pg_sp_cam_parse_item, false);
956 }
957 
958 /**
959  * ice_pg_nm_cam_table_get - create a parse graph no match cam table
960  * @hw: pointer to the hardware structure
961  *
962  * Return: a pointer to the allocated Parse Graph No Match CAM table.
963  */
ice_pg_nm_cam_table_get(struct ice_hw * hw)964 static struct ice_pg_nm_cam_item *ice_pg_nm_cam_table_get(struct ice_hw *hw)
965 {
966 	return ice_parser_create_table(hw, ICE_SID_RXPARSER_NOMATCH_CAM,
967 				       sizeof(struct ice_pg_nm_cam_item),
968 				       ICE_PG_NM_CAM_TABLE_SIZE,
969 				       ice_pg_nm_cam_parse_item, false);
970 }
971 
972 /**
973  * ice_pg_nm_sp_cam_table_get - create a parse graph no match spill cam table
974  * @hw: pointer to the hardware structure
975  *
976  * Return: a pointer to the allocated Parse Graph No Match Spill CAM table.
977  */
ice_pg_nm_sp_cam_table_get(struct ice_hw * hw)978 static struct ice_pg_nm_cam_item *ice_pg_nm_sp_cam_table_get(struct ice_hw *hw)
979 {
980 	return ice_parser_create_table(hw, ICE_SID_RXPARSER_NOMATCH_SPILL,
981 				       sizeof(struct ice_pg_nm_cam_item),
982 				       ICE_PG_NM_SP_CAM_TABLE_SIZE,
983 				       ice_pg_nm_sp_cam_parse_item, false);
984 }
985 
__ice_pg_cam_match(struct ice_pg_cam_item * item,struct ice_pg_cam_key * key)986 static bool __ice_pg_cam_match(struct ice_pg_cam_item *item,
987 			       struct ice_pg_cam_key *key)
988 {
989 	return (item->key.valid &&
990 		!memcmp(&item->key.val, &key->val, sizeof(key->val)));
991 }
992 
__ice_pg_nm_cam_match(struct ice_pg_nm_cam_item * item,struct ice_pg_cam_key * key)993 static bool __ice_pg_nm_cam_match(struct ice_pg_nm_cam_item *item,
994 				  struct ice_pg_cam_key *key)
995 {
996 	return (item->key.valid &&
997 		!memcmp(&item->key.val, &key->val, sizeof(item->key.val)));
998 }
999 
1000 /**
1001  * ice_pg_cam_match - search parse graph cam table by key
1002  * @table: parse graph cam table to search
1003  * @size: cam table size
1004  * @key: search key
1005  *
1006  * Return: a pointer to the matching PG CAM item or NULL.
1007  */
ice_pg_cam_match(struct ice_pg_cam_item * table,int size,struct ice_pg_cam_key * key)1008 struct ice_pg_cam_item *ice_pg_cam_match(struct ice_pg_cam_item *table,
1009 					 int size, struct ice_pg_cam_key *key)
1010 {
1011 	int i;
1012 
1013 	for (i = 0; i < size; i++) {
1014 		struct ice_pg_cam_item *item = &table[i];
1015 
1016 		if (__ice_pg_cam_match(item, key))
1017 			return item;
1018 	}
1019 
1020 	return NULL;
1021 }
1022 
1023 /**
1024  * ice_pg_nm_cam_match - search parse graph no match cam table by key
1025  * @table: parse graph no match cam table to search
1026  * @size: cam table size
1027  * @key: search key
1028  *
1029  * Return: a pointer to the matching PG No Match CAM item or NULL.
1030  */
1031 struct ice_pg_nm_cam_item *
ice_pg_nm_cam_match(struct ice_pg_nm_cam_item * table,int size,struct ice_pg_cam_key * key)1032 ice_pg_nm_cam_match(struct ice_pg_nm_cam_item *table, int size,
1033 		    struct ice_pg_cam_key *key)
1034 {
1035 	int i;
1036 
1037 	for (i = 0; i < size; i++) {
1038 		struct ice_pg_nm_cam_item *item = &table[i];
1039 
1040 		if (__ice_pg_nm_cam_match(item, key))
1041 			return item;
1042 	}
1043 
1044 	return NULL;
1045 }
1046 
1047 /*** Ternary match ***/
1048 /* Perform a ternary match on a 1-byte pattern (@pat) given @key and @key_inv
1049  * Rules (per bit):
1050  *     Key == 0 and Key_inv == 0 : Never match (Don't care)
1051  *     Key == 0 and Key_inv == 1 : Match on bit == 1
1052  *     Key == 1 and Key_inv == 0 : Match on bit == 0
1053  *     Key == 1 and Key_inv == 1 : Always match (Don't care)
1054  *
1055  * Return: true if all bits match, false otherwise.
1056  */
ice_ternary_match_byte(u8 key,u8 key_inv,u8 pat)1057 static bool ice_ternary_match_byte(u8 key, u8 key_inv, u8 pat)
1058 {
1059 	u8 bit_key, bit_key_inv, bit_pat;
1060 	int i;
1061 
1062 	for (i = 0; i < BITS_PER_BYTE; i++) {
1063 		bit_key = key & BIT(i);
1064 		bit_key_inv = key_inv & BIT(i);
1065 		bit_pat = pat & BIT(i);
1066 
1067 		if (bit_key != 0 && bit_key_inv != 0)
1068 			continue;
1069 
1070 		if ((bit_key == 0 && bit_key_inv == 0) || bit_key == bit_pat)
1071 			return false;
1072 	}
1073 
1074 	return true;
1075 }
1076 
ice_ternary_match(const u8 * key,const u8 * key_inv,const u8 * pat,int len)1077 static bool ice_ternary_match(const u8 *key, const u8 *key_inv,
1078 			      const u8 *pat, int len)
1079 {
1080 	int i;
1081 
1082 	for (i = 0; i < len; i++)
1083 		if (!ice_ternary_match_byte(key[i], key_inv[i], pat[i]))
1084 			return false;
1085 
1086 	return true;
1087 }
1088 
1089 /*** ICE_SID_RXPARSER_BOOST_TCAM and ICE_SID_LBL_RXPARSER_TMEM sections ***/
ice_bst_np_kb_dump(struct ice_hw * hw,struct ice_np_keybuilder * kb)1090 static void ice_bst_np_kb_dump(struct ice_hw *hw, struct ice_np_keybuilder *kb)
1091 {
1092 	struct device *dev = ice_hw_to_dev(hw);
1093 
1094 	dev_info(dev, "next proto key builder:\n");
1095 	dev_info(dev, "\topc = %d\n", kb->opc);
1096 	dev_info(dev, "\tstart_reg0 = %d\n", kb->start_reg0);
1097 	dev_info(dev, "\tlen_reg1 = %d\n", kb->len_reg1);
1098 }
1099 
ice_bst_pg_kb_dump(struct ice_hw * hw,struct ice_pg_keybuilder * kb)1100 static void ice_bst_pg_kb_dump(struct ice_hw *hw, struct ice_pg_keybuilder *kb)
1101 {
1102 	struct device *dev = ice_hw_to_dev(hw);
1103 
1104 	dev_info(dev, "parse graph key builder:\n");
1105 	dev_info(dev, "\tflag0_ena = %d\n", kb->flag0_ena);
1106 	dev_info(dev, "\tflag1_ena = %d\n", kb->flag1_ena);
1107 	dev_info(dev, "\tflag2_ena = %d\n", kb->flag2_ena);
1108 	dev_info(dev, "\tflag3_ena = %d\n", kb->flag3_ena);
1109 	dev_info(dev, "\tflag0_idx = %d\n", kb->flag0_idx);
1110 	dev_info(dev, "\tflag1_idx = %d\n", kb->flag1_idx);
1111 	dev_info(dev, "\tflag2_idx = %d\n", kb->flag2_idx);
1112 	dev_info(dev, "\tflag3_idx = %d\n", kb->flag3_idx);
1113 	dev_info(dev, "\talu_reg_idx = %d\n", kb->alu_reg_idx);
1114 }
1115 
ice_bst_alu_dump(struct ice_hw * hw,struct ice_alu * alu,int idx)1116 static void ice_bst_alu_dump(struct ice_hw *hw, struct ice_alu *alu, int idx)
1117 {
1118 	struct device *dev = ice_hw_to_dev(hw);
1119 
1120 	dev_info(dev, "alu%d:\n", idx);
1121 	dev_info(dev, "\topc = %d\n", alu->opc);
1122 	dev_info(dev, "\tsrc_start = %d\n", alu->src_start);
1123 	dev_info(dev, "\tsrc_len = %d\n", alu->src_len);
1124 	dev_info(dev, "\tshift_xlate_sel = %d\n", alu->shift_xlate_sel);
1125 	dev_info(dev, "\tshift_xlate_key = %d\n", alu->shift_xlate_key);
1126 	dev_info(dev, "\tsrc_reg_id = %d\n", alu->src_reg_id);
1127 	dev_info(dev, "\tdst_reg_id = %d\n", alu->dst_reg_id);
1128 	dev_info(dev, "\tinc0 = %d\n", alu->inc0);
1129 	dev_info(dev, "\tinc1 = %d\n", alu->inc1);
1130 	dev_info(dev, "\tproto_offset_opc = %d\n", alu->proto_offset_opc);
1131 	dev_info(dev, "\tproto_offset = %d\n", alu->proto_offset);
1132 	dev_info(dev, "\tbranch_addr = %d\n", alu->branch_addr);
1133 	dev_info(dev, "\timm = %d\n", alu->imm);
1134 	dev_info(dev, "\tdst_start = %d\n", alu->dst_start);
1135 	dev_info(dev, "\tdst_len = %d\n", alu->dst_len);
1136 	dev_info(dev, "\tflags_extr_imm = %d\n", alu->flags_extr_imm);
1137 	dev_info(dev, "\tflags_start_imm= %d\n", alu->flags_start_imm);
1138 }
1139 
1140 /**
1141  * ice_bst_tcam_dump - dump a boost tcam info
1142  * @hw: pointer to the hardware structure
1143  * @item: boost tcam to dump
1144  */
ice_bst_tcam_dump(struct ice_hw * hw,struct ice_bst_tcam_item * item)1145 static void ice_bst_tcam_dump(struct ice_hw *hw, struct ice_bst_tcam_item *item)
1146 {
1147 	struct device *dev = ice_hw_to_dev(hw);
1148 	int i;
1149 
1150 	dev_info(dev, "addr = %d\n", item->addr);
1151 
1152 	dev_info(dev, "key    : ");
1153 	for (i = 0; i < ICE_BST_TCAM_KEY_SIZE; i++)
1154 		dev_info(dev, "%02x ", item->key[i]);
1155 
1156 	dev_info(dev, "\n");
1157 
1158 	dev_info(dev, "key_inv: ");
1159 	for (i = 0; i < ICE_BST_TCAM_KEY_SIZE; i++)
1160 		dev_info(dev, "%02x ", item->key_inv[i]);
1161 
1162 	dev_info(dev, "\n");
1163 
1164 	dev_info(dev, "hit_idx_grp = %d\n", item->hit_idx_grp);
1165 	dev_info(dev, "pg_prio = %d\n", item->pg_prio);
1166 
1167 	ice_bst_np_kb_dump(hw, &item->np_kb);
1168 	ice_bst_pg_kb_dump(hw, &item->pg_kb);
1169 
1170 	ice_bst_alu_dump(hw, &item->alu0, ICE_ALU0_IDX);
1171 	ice_bst_alu_dump(hw, &item->alu1, ICE_ALU1_IDX);
1172 	ice_bst_alu_dump(hw, &item->alu2, ICE_ALU2_IDX);
1173 }
1174 
ice_lbl_dump(struct ice_hw * hw,struct ice_lbl_item * item)1175 static void ice_lbl_dump(struct ice_hw *hw, struct ice_lbl_item *item)
1176 {
1177 	struct device *dev = ice_hw_to_dev(hw);
1178 
1179 	dev_info(dev, "index = %u\n", item->idx);
1180 	dev_info(dev, "type = %u\n", item->type);
1181 	dev_info(dev, "label = %s\n", item->label);
1182 }
1183 
1184 #define ICE_BST_ALU_OPC		GENMASK_ULL(5, 0)
1185 #define ICE_BST_ALU_SS		GENMASK_ULL(13, 6)
1186 #define ICE_BST_ALU_SL		GENMASK_ULL(18, 14)
1187 #define ICE_BST_ALU_SXS		BIT_ULL(19)
1188 #define ICE_BST_ALU_SXK		GENMASK_ULL(23, 20)
1189 #define ICE_BST_ALU_SRID	GENMASK_ULL(30, 24)
1190 #define ICE_BST_ALU_DRID	GENMASK_ULL(37, 31)
1191 #define ICE_BST_ALU_INC0	BIT_ULL(38)
1192 #define ICE_BST_ALU_INC1	BIT_ULL(39)
1193 #define ICE_BST_ALU_POO		GENMASK_ULL(41, 40)
1194 #define ICE_BST_ALU_PO		GENMASK_ULL(49, 42)
1195 #define ICE_BST_ALU_BA_S	50	/* offset for the 2nd 64-bits field */
1196 #define ICE_BST_ALU_BA		GENMASK_ULL(57 - ICE_BST_ALU_BA_S, \
1197 					    50 - ICE_BST_ALU_BA_S)
1198 #define ICE_BST_ALU_IMM		GENMASK_ULL(73 - ICE_BST_ALU_BA_S, \
1199 					    58 - ICE_BST_ALU_BA_S)
1200 #define ICE_BST_ALU_DFE		BIT_ULL(74 - ICE_BST_ALU_BA_S)
1201 #define ICE_BST_ALU_DS		GENMASK_ULL(80 - ICE_BST_ALU_BA_S, \
1202 					    75 - ICE_BST_ALU_BA_S)
1203 #define ICE_BST_ALU_DL		GENMASK_ULL(86 - ICE_BST_ALU_BA_S, \
1204 					    81 - ICE_BST_ALU_BA_S)
1205 #define ICE_BST_ALU_FEI		BIT_ULL(87 - ICE_BST_ALU_BA_S)
1206 #define ICE_BST_ALU_FSI		GENMASK_ULL(95 - ICE_BST_ALU_BA_S, \
1207 					    88 - ICE_BST_ALU_BA_S)
1208 
1209 /**
1210  * ice_bst_alu_init - parse 96 bits of ALU entry
1211  * @alu: pointer to the ALU entry structure
1212  * @data: ALU entry data to be parsed
1213  * @off: offset of the ALU entry data
1214  */
ice_bst_alu_init(struct ice_alu * alu,u8 * data,u8 off)1215 static void ice_bst_alu_init(struct ice_alu *alu, u8 *data, u8 off)
1216 {
1217 	u64 d64;
1218 	u8 idd;
1219 
1220 	d64 = *((u64 *)data) >> off;
1221 
1222 	alu->opc		= FIELD_GET(ICE_BST_ALU_OPC, d64);
1223 	alu->src_start		= FIELD_GET(ICE_BST_ALU_SS, d64);
1224 	alu->src_len		= FIELD_GET(ICE_BST_ALU_SL, d64);
1225 	alu->shift_xlate_sel	= FIELD_GET(ICE_BST_ALU_SXS, d64);
1226 	alu->shift_xlate_key	= FIELD_GET(ICE_BST_ALU_SXK, d64);
1227 	alu->src_reg_id		= FIELD_GET(ICE_BST_ALU_SRID, d64);
1228 	alu->dst_reg_id		= FIELD_GET(ICE_BST_ALU_DRID, d64);
1229 	alu->inc0		= FIELD_GET(ICE_BST_ALU_INC0, d64);
1230 	alu->inc1		= FIELD_GET(ICE_BST_ALU_INC1, d64);
1231 	alu->proto_offset_opc	= FIELD_GET(ICE_BST_ALU_POO, d64);
1232 	alu->proto_offset	= FIELD_GET(ICE_BST_ALU_PO, d64);
1233 
1234 	idd = (ICE_BST_ALU_BA_S + off) / BITS_PER_BYTE;
1235 	off = (ICE_BST_ALU_BA_S + off) % BITS_PER_BYTE;
1236 	d64 = *((u64 *)(&data[idd])) >> off;
1237 
1238 	alu->branch_addr	= FIELD_GET(ICE_BST_ALU_BA, d64);
1239 	alu->imm		= FIELD_GET(ICE_BST_ALU_IMM, d64);
1240 	alu->dedicate_flags_ena	= FIELD_GET(ICE_BST_ALU_DFE, d64);
1241 	alu->dst_start		= FIELD_GET(ICE_BST_ALU_DS, d64);
1242 	alu->dst_len		= FIELD_GET(ICE_BST_ALU_DL, d64);
1243 	alu->flags_extr_imm	= FIELD_GET(ICE_BST_ALU_FEI, d64);
1244 	alu->flags_start_imm	= FIELD_GET(ICE_BST_ALU_FSI, d64);
1245 }
1246 
1247 #define ICE_BST_PGKB_F0_ENA		BIT_ULL(0)
1248 #define ICE_BST_PGKB_F0_IDX		GENMASK_ULL(6, 1)
1249 #define ICE_BST_PGKB_F1_ENA		BIT_ULL(7)
1250 #define ICE_BST_PGKB_F1_IDX		GENMASK_ULL(13, 8)
1251 #define ICE_BST_PGKB_F2_ENA		BIT_ULL(14)
1252 #define ICE_BST_PGKB_F2_IDX		GENMASK_ULL(20, 15)
1253 #define ICE_BST_PGKB_F3_ENA		BIT_ULL(21)
1254 #define ICE_BST_PGKB_F3_IDX		GENMASK_ULL(27, 22)
1255 #define ICE_BST_PGKB_AR_IDX		GENMASK_ULL(34, 28)
1256 
1257 /**
1258  * ice_bst_pgkb_init - parse 35 bits of Parse Graph Key Build
1259  * @kb: pointer to the Parse Graph Key Build structure
1260  * @data: Parse Graph Key Build data to be parsed
1261  */
ice_bst_pgkb_init(struct ice_pg_keybuilder * kb,u64 data)1262 static void ice_bst_pgkb_init(struct ice_pg_keybuilder *kb, u64 data)
1263 {
1264 	kb->flag0_ena	= FIELD_GET(ICE_BST_PGKB_F0_ENA, data);
1265 	kb->flag0_idx	= FIELD_GET(ICE_BST_PGKB_F0_IDX, data);
1266 	kb->flag1_ena	= FIELD_GET(ICE_BST_PGKB_F1_ENA, data);
1267 	kb->flag1_idx	= FIELD_GET(ICE_BST_PGKB_F1_IDX, data);
1268 	kb->flag2_ena	= FIELD_GET(ICE_BST_PGKB_F2_ENA, data);
1269 	kb->flag2_idx	= FIELD_GET(ICE_BST_PGKB_F2_IDX, data);
1270 	kb->flag3_ena	= FIELD_GET(ICE_BST_PGKB_F3_ENA, data);
1271 	kb->flag3_idx	= FIELD_GET(ICE_BST_PGKB_F3_IDX, data);
1272 	kb->alu_reg_idx	= FIELD_GET(ICE_BST_PGKB_AR_IDX, data);
1273 }
1274 
1275 #define ICE_BST_NPKB_OPC	GENMASK(1, 0)
1276 #define ICE_BST_NPKB_S_R0	GENMASK(9, 2)
1277 #define ICE_BST_NPKB_L_R1	GENMASK(17, 10)
1278 
1279 /**
1280  * ice_bst_npkb_init - parse 18 bits of Next Protocol Key Build
1281  * @kb: pointer to the Next Protocol Key Build structure
1282  * @data: Next Protocol Key Build data to be parsed
1283  */
ice_bst_npkb_init(struct ice_np_keybuilder * kb,u32 data)1284 static void ice_bst_npkb_init(struct ice_np_keybuilder *kb, u32 data)
1285 {
1286 	kb->opc		= FIELD_GET(ICE_BST_NPKB_OPC, data);
1287 	kb->start_reg0	= FIELD_GET(ICE_BST_NPKB_S_R0, data);
1288 	kb->len_reg1	= FIELD_GET(ICE_BST_NPKB_L_R1, data);
1289 }
1290 
1291 #define ICE_BT_KEY_S		32
1292 #define ICE_BT_KEY_IDD		(ICE_BT_KEY_S / BITS_PER_BYTE)
1293 #define ICE_BT_KIV_S		192
1294 #define ICE_BT_KIV_IDD		(ICE_BT_KIV_S / BITS_PER_BYTE)
1295 #define ICE_BT_HIG_S		352
1296 #define ICE_BT_HIG_IDD		(ICE_BT_HIG_S / BITS_PER_BYTE)
1297 #define ICE_BT_PGP_S		360
1298 #define ICE_BT_PGP_IDD		(ICE_BT_PGP_S / BITS_PER_BYTE)
1299 #define ICE_BT_PGP_M		GENMASK(361 - ICE_BT_PGP_S, 360 - ICE_BT_PGP_S)
1300 #define ICE_BT_NPKB_S		362
1301 #define ICE_BT_NPKB_IDD		(ICE_BT_NPKB_S / BITS_PER_BYTE)
1302 #define ICE_BT_NPKB_OFF		(ICE_BT_NPKB_S % BITS_PER_BYTE)
1303 #define ICE_BT_PGKB_S		380
1304 #define ICE_BT_PGKB_IDD		(ICE_BT_PGKB_S / BITS_PER_BYTE)
1305 #define ICE_BT_PGKB_OFF		(ICE_BT_PGKB_S % BITS_PER_BYTE)
1306 #define ICE_BT_ALU0_S		415
1307 #define ICE_BT_ALU0_IDD		(ICE_BT_ALU0_S / BITS_PER_BYTE)
1308 #define ICE_BT_ALU0_OFF		(ICE_BT_ALU0_S % BITS_PER_BYTE)
1309 #define ICE_BT_ALU1_S		511
1310 #define ICE_BT_ALU1_IDD		(ICE_BT_ALU1_S / BITS_PER_BYTE)
1311 #define ICE_BT_ALU1_OFF		(ICE_BT_ALU1_S % BITS_PER_BYTE)
1312 #define ICE_BT_ALU2_S		607
1313 #define ICE_BT_ALU2_IDD		(ICE_BT_ALU2_S / BITS_PER_BYTE)
1314 #define ICE_BT_ALU2_OFF		(ICE_BT_ALU2_S % BITS_PER_BYTE)
1315 
1316 /**
1317  * ice_bst_parse_item - parse 704 bits of Boost TCAM entry
1318  * @hw: pointer to the hardware structure
1319  * @idx: index of Boost TCAM entry
1320  * @item: item of Boost TCAM entry
1321  * @data: Boost TCAM entry data to be parsed
1322  * @size: size of Boost TCAM entry
1323  */
ice_bst_parse_item(struct ice_hw * hw,u16 idx,void * item,void * data,int __maybe_unused size)1324 static void ice_bst_parse_item(struct ice_hw *hw, u16 idx, void *item,
1325 			       void *data, int __maybe_unused size)
1326 {
1327 	struct ice_bst_tcam_item *ti = item;
1328 	u8 *buf = (u8 *)data;
1329 	int i;
1330 
1331 	ti->addr = *(u16 *)buf;
1332 
1333 	for (i = 0; i < ICE_BST_TCAM_KEY_SIZE; i++) {
1334 		ti->key[i] = buf[ICE_BT_KEY_IDD + i];
1335 		ti->key_inv[i] = buf[ICE_BT_KIV_IDD + i];
1336 	}
1337 	ti->hit_idx_grp	= buf[ICE_BT_HIG_IDD];
1338 	ti->pg_prio	= buf[ICE_BT_PGP_IDD] & ICE_BT_PGP_M;
1339 
1340 	ice_bst_npkb_init(&ti->np_kb,
1341 			  *((u32 *)(&buf[ICE_BT_NPKB_IDD])) >>
1342 			   ICE_BT_NPKB_OFF);
1343 	ice_bst_pgkb_init(&ti->pg_kb,
1344 			  *((u64 *)(&buf[ICE_BT_PGKB_IDD])) >>
1345 			   ICE_BT_PGKB_OFF);
1346 
1347 	ice_bst_alu_init(&ti->alu0, &buf[ICE_BT_ALU0_IDD], ICE_BT_ALU0_OFF);
1348 	ice_bst_alu_init(&ti->alu1, &buf[ICE_BT_ALU1_IDD], ICE_BT_ALU1_OFF);
1349 	ice_bst_alu_init(&ti->alu2, &buf[ICE_BT_ALU2_IDD], ICE_BT_ALU2_OFF);
1350 
1351 	if (hw->debug_mask & ICE_DBG_PARSER)
1352 		ice_bst_tcam_dump(hw, ti);
1353 }
1354 
1355 /**
1356  * ice_bst_tcam_table_get - create a boost tcam table
1357  * @hw: pointer to the hardware structure
1358  *
1359  * Return: a pointer to the allocated Boost TCAM table.
1360  */
ice_bst_tcam_table_get(struct ice_hw * hw)1361 static struct ice_bst_tcam_item *ice_bst_tcam_table_get(struct ice_hw *hw)
1362 {
1363 	return ice_parser_create_table(hw, ICE_SID_RXPARSER_BOOST_TCAM,
1364 				       sizeof(struct ice_bst_tcam_item),
1365 				       ICE_BST_TCAM_TABLE_SIZE,
1366 				       ice_bst_parse_item, true);
1367 }
1368 
ice_parse_lbl_item(struct ice_hw * hw,u16 idx,void * item,void * data,int __maybe_unused size)1369 static void ice_parse_lbl_item(struct ice_hw *hw, u16 idx, void *item,
1370 			       void *data, int __maybe_unused size)
1371 {
1372 	struct ice_lbl_item *lbl_item = item;
1373 	struct ice_lbl_item *lbl_data = data;
1374 
1375 	lbl_item->idx = lbl_data->idx;
1376 	memcpy(lbl_item->label, lbl_data->label, sizeof(lbl_item->label));
1377 
1378 	if (strstarts(lbl_item->label, ICE_LBL_BST_DVM))
1379 		lbl_item->type = ICE_LBL_BST_TYPE_DVM;
1380 	else if (strstarts(lbl_item->label, ICE_LBL_BST_SVM))
1381 		lbl_item->type = ICE_LBL_BST_TYPE_SVM;
1382 	else if (strstarts(lbl_item->label, ICE_LBL_TNL_VXLAN))
1383 		lbl_item->type = ICE_LBL_BST_TYPE_VXLAN;
1384 	else if (strstarts(lbl_item->label, ICE_LBL_TNL_GENEVE))
1385 		lbl_item->type = ICE_LBL_BST_TYPE_GENEVE;
1386 	else if (strstarts(lbl_item->label, ICE_LBL_TNL_UDP_ECPRI))
1387 		lbl_item->type = ICE_LBL_BST_TYPE_UDP_ECPRI;
1388 
1389 	if (hw->debug_mask & ICE_DBG_PARSER)
1390 		ice_lbl_dump(hw, lbl_item);
1391 }
1392 
1393 /**
1394  * ice_bst_lbl_table_get - create a boost label table
1395  * @hw: pointer to the hardware structure
1396  *
1397  * Return: a pointer to the allocated Boost label table.
1398  */
ice_bst_lbl_table_get(struct ice_hw * hw)1399 static struct ice_lbl_item *ice_bst_lbl_table_get(struct ice_hw *hw)
1400 {
1401 	return ice_parser_create_table(hw, ICE_SID_LBL_RXPARSER_TMEM,
1402 				       sizeof(struct ice_lbl_item),
1403 				       ICE_BST_TCAM_TABLE_SIZE,
1404 				       ice_parse_lbl_item, true);
1405 }
1406 
1407 /**
1408  * ice_bst_tcam_match - match a pattern on the boost tcam table
1409  * @tcam_table: boost tcam table to search
1410  * @pat: pattern to match
1411  *
1412  * Return: a pointer to the matching Boost TCAM item or NULL.
1413  */
1414 struct ice_bst_tcam_item *
ice_bst_tcam_match(struct ice_bst_tcam_item * tcam_table,u8 * pat)1415 ice_bst_tcam_match(struct ice_bst_tcam_item *tcam_table, u8 *pat)
1416 {
1417 	int i;
1418 
1419 	for (i = 0; i < ICE_BST_TCAM_TABLE_SIZE; i++) {
1420 		struct ice_bst_tcam_item *item = &tcam_table[i];
1421 
1422 		if (item->hit_idx_grp == 0)
1423 			continue;
1424 		if (ice_ternary_match(item->key, item->key_inv, pat,
1425 				      ICE_BST_TCAM_KEY_SIZE))
1426 			return item;
1427 	}
1428 
1429 	return NULL;
1430 }
1431 
1432 /*** ICE_SID_RXPARSER_MARKER_PTYPE section ***/
1433 /**
1434  * ice_ptype_mk_tcam_dump - dump an ptype marker tcam info
1435  * @hw: pointer to the hardware structure
1436  * @item: ptype marker tcam to dump
1437  */
ice_ptype_mk_tcam_dump(struct ice_hw * hw,struct ice_ptype_mk_tcam_item * item)1438 static void ice_ptype_mk_tcam_dump(struct ice_hw *hw,
1439 				   struct ice_ptype_mk_tcam_item *item)
1440 {
1441 	struct device *dev = ice_hw_to_dev(hw);
1442 	int i;
1443 
1444 	dev_info(dev, "address = %d\n", item->address);
1445 	dev_info(dev, "ptype = %d\n", item->ptype);
1446 
1447 	dev_info(dev, "key    :");
1448 	for (i = 0; i < ICE_PTYPE_MK_TCAM_KEY_SIZE; i++)
1449 		dev_info(dev, "%02x ", item->key[i]);
1450 
1451 	dev_info(dev, "\n");
1452 
1453 	dev_info(dev, "key_inv:");
1454 	for (i = 0; i < ICE_PTYPE_MK_TCAM_KEY_SIZE; i++)
1455 		dev_info(dev, "%02x ", item->key_inv[i]);
1456 
1457 	dev_info(dev, "\n");
1458 }
1459 
ice_parse_ptype_mk_tcam_item(struct ice_hw * hw,u16 idx,void * item,void * data,int size)1460 static void ice_parse_ptype_mk_tcam_item(struct ice_hw *hw, u16 idx,
1461 					 void *item, void *data, int size)
1462 {
1463 	memcpy(item, data, size);
1464 
1465 	if (hw->debug_mask & ICE_DBG_PARSER)
1466 		ice_ptype_mk_tcam_dump(hw,
1467 				       (struct ice_ptype_mk_tcam_item *)item);
1468 }
1469 
1470 /**
1471  * ice_ptype_mk_tcam_table_get - create a ptype marker tcam table
1472  * @hw: pointer to the hardware structure
1473  *
1474  * Return: a pointer to the allocated Marker PType TCAM table.
1475  */
1476 static
ice_ptype_mk_tcam_table_get(struct ice_hw * hw)1477 struct ice_ptype_mk_tcam_item *ice_ptype_mk_tcam_table_get(struct ice_hw *hw)
1478 {
1479 	return ice_parser_create_table(hw, ICE_SID_RXPARSER_MARKER_PTYPE,
1480 				       sizeof(struct ice_ptype_mk_tcam_item),
1481 				       ICE_PTYPE_MK_TCAM_TABLE_SIZE,
1482 				       ice_parse_ptype_mk_tcam_item, true);
1483 }
1484 
1485 /**
1486  * ice_ptype_mk_tcam_match - match a pattern on a ptype marker tcam table
1487  * @table: ptype marker tcam table to search
1488  * @pat: pattern to match
1489  * @len: length of the pattern
1490  *
1491  * Return: a pointer to the matching Marker PType item or NULL.
1492  */
1493 struct ice_ptype_mk_tcam_item *
ice_ptype_mk_tcam_match(struct ice_ptype_mk_tcam_item * table,u8 * pat,int len)1494 ice_ptype_mk_tcam_match(struct ice_ptype_mk_tcam_item *table,
1495 			u8 *pat, int len)
1496 {
1497 	int i;
1498 
1499 	for (i = 0; i < ICE_PTYPE_MK_TCAM_TABLE_SIZE; i++) {
1500 		struct ice_ptype_mk_tcam_item *item = &table[i];
1501 
1502 		if (ice_ternary_match(item->key, item->key_inv, pat, len))
1503 			return item;
1504 	}
1505 
1506 	return NULL;
1507 }
1508 
1509 /*** ICE_SID_RXPARSER_MARKER_GRP section ***/
1510 /**
1511  * ice_mk_grp_dump - dump an marker group item info
1512  * @hw: pointer to the hardware structure
1513  * @item: marker group item to dump
1514  */
ice_mk_grp_dump(struct ice_hw * hw,struct ice_mk_grp_item * item)1515 static void ice_mk_grp_dump(struct ice_hw *hw, struct ice_mk_grp_item *item)
1516 {
1517 	struct device *dev = ice_hw_to_dev(hw);
1518 	int i;
1519 
1520 	dev_info(dev, "index = %d\n", item->idx);
1521 
1522 	dev_info(dev, "markers: ");
1523 	for (i = 0; i < ICE_MK_COUNT_PER_GRP; i++)
1524 		dev_info(dev, "%d ", item->markers[i]);
1525 
1526 	dev_info(dev, "\n");
1527 }
1528 
ice_mk_grp_parse_item(struct ice_hw * hw,u16 idx,void * item,void * data,int __maybe_unused size)1529 static void ice_mk_grp_parse_item(struct ice_hw *hw, u16 idx, void *item,
1530 				  void *data, int __maybe_unused size)
1531 {
1532 	struct ice_mk_grp_item *grp = item;
1533 	u8 *buf = data;
1534 	int i;
1535 
1536 	grp->idx = idx;
1537 
1538 	for (i = 0; i < ICE_MK_COUNT_PER_GRP; i++)
1539 		grp->markers[i] = buf[i];
1540 
1541 	if (hw->debug_mask & ICE_DBG_PARSER)
1542 		ice_mk_grp_dump(hw, grp);
1543 }
1544 
1545 /**
1546  * ice_mk_grp_table_get - create a marker group table
1547  * @hw: pointer to the hardware structure
1548  *
1549  * Return: a pointer to the allocated Marker Group ID table.
1550  */
ice_mk_grp_table_get(struct ice_hw * hw)1551 static struct ice_mk_grp_item *ice_mk_grp_table_get(struct ice_hw *hw)
1552 {
1553 	return ice_parser_create_table(hw, ICE_SID_RXPARSER_MARKER_GRP,
1554 				       sizeof(struct ice_mk_grp_item),
1555 				       ICE_MK_GRP_TABLE_SIZE,
1556 				       ice_mk_grp_parse_item, false);
1557 }
1558 
1559 /*** ICE_SID_RXPARSER_PROTO_GRP section ***/
ice_proto_off_dump(struct ice_hw * hw,struct ice_proto_off * po,int idx)1560 static void ice_proto_off_dump(struct ice_hw *hw,
1561 			       struct ice_proto_off *po, int idx)
1562 {
1563 	struct device *dev = ice_hw_to_dev(hw);
1564 
1565 	dev_info(dev, "proto %d\n", idx);
1566 	dev_info(dev, "\tpolarity = %d\n", po->polarity);
1567 	dev_info(dev, "\tproto_id = %d\n", po->proto_id);
1568 	dev_info(dev, "\toffset = %d\n", po->offset);
1569 }
1570 
1571 /**
1572  * ice_proto_grp_dump - dump a proto group item info
1573  * @hw: pointer to the hardware structure
1574  * @item: proto group item to dump
1575  */
ice_proto_grp_dump(struct ice_hw * hw,struct ice_proto_grp_item * item)1576 static void ice_proto_grp_dump(struct ice_hw *hw,
1577 			       struct ice_proto_grp_item *item)
1578 {
1579 	int i;
1580 
1581 	dev_info(ice_hw_to_dev(hw), "index = %d\n", item->idx);
1582 
1583 	for (i = 0; i < ICE_PROTO_COUNT_PER_GRP; i++)
1584 		ice_proto_off_dump(hw, &item->po[i], i);
1585 }
1586 
1587 #define ICE_PO_POL	BIT(0)
1588 #define ICE_PO_PID	GENMASK(8, 1)
1589 #define ICE_PO_OFF	GENMASK(21, 12)
1590 
1591 /**
1592  * ice_proto_off_parse - parse 22 bits of Protocol entry
1593  * @po: pointer to the Protocol entry structure
1594  * @data: Protocol entry data to be parsed
1595  */
ice_proto_off_parse(struct ice_proto_off * po,u32 data)1596 static void ice_proto_off_parse(struct ice_proto_off *po, u32 data)
1597 {
1598 	po->polarity = FIELD_GET(ICE_PO_POL, data);
1599 	po->proto_id = FIELD_GET(ICE_PO_PID, data);
1600 	po->offset = FIELD_GET(ICE_PO_OFF, data);
1601 }
1602 
1603 /**
1604  * ice_proto_grp_parse_item - parse 192 bits of Protocol Group Table entry
1605  * @hw: pointer to the hardware structure
1606  * @idx: index of Protocol Group Table entry
1607  * @item: item of Protocol Group Table entry
1608  * @data: Protocol Group Table entry data to be parsed
1609  * @size: size of Protocol Group Table entry
1610  */
ice_proto_grp_parse_item(struct ice_hw * hw,u16 idx,void * item,void * data,int __maybe_unused size)1611 static void ice_proto_grp_parse_item(struct ice_hw *hw, u16 idx, void *item,
1612 				     void *data, int __maybe_unused size)
1613 {
1614 	struct ice_proto_grp_item *grp = item;
1615 	u8 *buf = (u8 *)data;
1616 	u8 idd, off;
1617 	u32 d32;
1618 	int i;
1619 
1620 	grp->idx = idx;
1621 
1622 	for (i = 0; i < ICE_PROTO_COUNT_PER_GRP; i++) {
1623 		idd = (ICE_PROTO_GRP_ITEM_SIZE * i) / BITS_PER_BYTE;
1624 		off = (ICE_PROTO_GRP_ITEM_SIZE * i) % BITS_PER_BYTE;
1625 		d32 = *((u32 *)&buf[idd]) >> off;
1626 		ice_proto_off_parse(&grp->po[i], d32);
1627 	}
1628 
1629 	if (hw->debug_mask & ICE_DBG_PARSER)
1630 		ice_proto_grp_dump(hw, grp);
1631 }
1632 
1633 /**
1634  * ice_proto_grp_table_get - create a proto group table
1635  * @hw: pointer to the hardware structure
1636  *
1637  * Return: a pointer to the allocated Protocol Group table.
1638  */
ice_proto_grp_table_get(struct ice_hw * hw)1639 static struct ice_proto_grp_item *ice_proto_grp_table_get(struct ice_hw *hw)
1640 {
1641 	return ice_parser_create_table(hw, ICE_SID_RXPARSER_PROTO_GRP,
1642 				       sizeof(struct ice_proto_grp_item),
1643 				       ICE_PROTO_GRP_TABLE_SIZE,
1644 				       ice_proto_grp_parse_item, false);
1645 }
1646 
1647 /*** ICE_SID_RXPARSER_FLAG_REDIR section ***/
1648 /**
1649  * ice_flg_rd_dump - dump a flag redirect item info
1650  * @hw: pointer to the hardware structure
1651  * @item: flag redirect item to dump
1652  */
ice_flg_rd_dump(struct ice_hw * hw,struct ice_flg_rd_item * item)1653 static void ice_flg_rd_dump(struct ice_hw *hw, struct ice_flg_rd_item *item)
1654 {
1655 	struct device *dev = ice_hw_to_dev(hw);
1656 
1657 	dev_info(dev, "index = %d\n", item->idx);
1658 	dev_info(dev, "expose = %d\n", item->expose);
1659 	dev_info(dev, "intr_flg_id = %d\n", item->intr_flg_id);
1660 }
1661 
1662 #define ICE_FRT_EXPO	BIT(0)
1663 #define ICE_FRT_IFID	GENMASK(6, 1)
1664 
1665 /**
1666  * ice_flg_rd_parse_item - parse 8 bits of Flag Redirect Table entry
1667  * @hw: pointer to the hardware structure
1668  * @idx: index of Flag Redirect Table entry
1669  * @item: item of Flag Redirect Table entry
1670  * @data: Flag Redirect Table entry data to be parsed
1671  * @size: size of Flag Redirect Table entry
1672  */
ice_flg_rd_parse_item(struct ice_hw * hw,u16 idx,void * item,void * data,int __maybe_unused size)1673 static void ice_flg_rd_parse_item(struct ice_hw *hw, u16 idx, void *item,
1674 				  void *data, int __maybe_unused size)
1675 {
1676 	struct ice_flg_rd_item *rdi = item;
1677 	u8 d8 = *(u8 *)data;
1678 
1679 	rdi->idx = idx;
1680 	rdi->expose = FIELD_GET(ICE_FRT_EXPO, d8);
1681 	rdi->intr_flg_id = FIELD_GET(ICE_FRT_IFID, d8);
1682 
1683 	if (hw->debug_mask & ICE_DBG_PARSER)
1684 		ice_flg_rd_dump(hw, rdi);
1685 }
1686 
1687 /**
1688  * ice_flg_rd_table_get - create a flag redirect table
1689  * @hw: pointer to the hardware structure
1690  *
1691  * Return: a pointer to the allocated Flags Redirection table.
1692  */
ice_flg_rd_table_get(struct ice_hw * hw)1693 static struct ice_flg_rd_item *ice_flg_rd_table_get(struct ice_hw *hw)
1694 {
1695 	return ice_parser_create_table(hw, ICE_SID_RXPARSER_FLAG_REDIR,
1696 				       sizeof(struct ice_flg_rd_item),
1697 				       ICE_FLG_RD_TABLE_SIZE,
1698 				       ice_flg_rd_parse_item, false);
1699 }
1700 
1701 /**
1702  * ice_flg_redirect - redirect a parser flag to packet flag
1703  * @table: flag redirect table
1704  * @psr_flg: parser flag to redirect
1705  *
1706  * Return: flag or 0 if @psr_flag = 0.
1707  */
ice_flg_redirect(struct ice_flg_rd_item * table,u64 psr_flg)1708 u64 ice_flg_redirect(struct ice_flg_rd_item *table, u64 psr_flg)
1709 {
1710 	u64 flg = 0;
1711 	int i;
1712 
1713 	for (i = 0; i < ICE_FLG_RDT_SIZE; i++) {
1714 		struct ice_flg_rd_item *item = &table[i];
1715 
1716 		if (!item->expose)
1717 			continue;
1718 
1719 		if (psr_flg & BIT(item->intr_flg_id))
1720 			flg |= BIT(i);
1721 	}
1722 
1723 	return flg;
1724 }
1725 
1726 /*** ICE_SID_XLT_KEY_BUILDER_SW, ICE_SID_XLT_KEY_BUILDER_ACL,
1727  * ICE_SID_XLT_KEY_BUILDER_FD and ICE_SID_XLT_KEY_BUILDER_RSS
1728  * sections ***/
ice_xlt_kb_entry_dump(struct ice_hw * hw,struct ice_xlt_kb_entry * entry,int idx)1729 static void ice_xlt_kb_entry_dump(struct ice_hw *hw,
1730 				  struct ice_xlt_kb_entry *entry, int idx)
1731 {
1732 	struct device *dev = ice_hw_to_dev(hw);
1733 	int i;
1734 
1735 	dev_info(dev, "key builder entry %d\n", idx);
1736 	dev_info(dev, "\txlt1_ad_sel = %d\n", entry->xlt1_ad_sel);
1737 	dev_info(dev, "\txlt2_ad_sel = %d\n", entry->xlt2_ad_sel);
1738 
1739 	for (i = 0; i < ICE_XLT_KB_FLAG0_14_CNT; i++)
1740 		dev_info(dev, "\tflg%d_sel = %d\n", i, entry->flg0_14_sel[i]);
1741 
1742 	dev_info(dev, "\txlt1_md_sel = %d\n", entry->xlt1_md_sel);
1743 	dev_info(dev, "\txlt2_md_sel = %d\n", entry->xlt2_md_sel);
1744 }
1745 
1746 /**
1747  * ice_xlt_kb_dump - dump a xlt key build info
1748  * @hw: pointer to the hardware structure
1749  * @kb: key build to dump
1750  */
ice_xlt_kb_dump(struct ice_hw * hw,struct ice_xlt_kb * kb)1751 static void ice_xlt_kb_dump(struct ice_hw *hw, struct ice_xlt_kb *kb)
1752 {
1753 	struct device *dev = ice_hw_to_dev(hw);
1754 	int i;
1755 
1756 	dev_info(dev, "xlt1_pm = %d\n", kb->xlt1_pm);
1757 	dev_info(dev, "xlt2_pm = %d\n", kb->xlt2_pm);
1758 	dev_info(dev, "prof_id_pm = %d\n", kb->prof_id_pm);
1759 	dev_info(dev, "flag15 lo = 0x%08x\n", (u32)kb->flag15);
1760 	dev_info(dev, "flag15 hi = 0x%08x\n",
1761 		 (u32)(kb->flag15 >> (sizeof(u32) * BITS_PER_BYTE)));
1762 
1763 	for (i = 0; i < ICE_XLT_KB_TBL_CNT; i++)
1764 		ice_xlt_kb_entry_dump(hw, &kb->entries[i], i);
1765 }
1766 
1767 #define ICE_XLT_KB_X1AS_S	32	/* offset for the 1st 64-bits field */
1768 #define ICE_XLT_KB_X1AS_IDD	(ICE_XLT_KB_X1AS_S / BITS_PER_BYTE)
1769 #define ICE_XLT_KB_X1AS_OFF	(ICE_XLT_KB_X1AS_S % BITS_PER_BYTE)
1770 #define ICE_XLT_KB_X1AS		GENMASK_ULL(34 - ICE_XLT_KB_X1AS_S, \
1771 					    32 - ICE_XLT_KB_X1AS_S)
1772 #define ICE_XLT_KB_X2AS		GENMASK_ULL(37 - ICE_XLT_KB_X1AS_S, \
1773 					    35 - ICE_XLT_KB_X1AS_S)
1774 #define ICE_XLT_KB_FL00		GENMASK_ULL(46 - ICE_XLT_KB_X1AS_S, \
1775 					    38 - ICE_XLT_KB_X1AS_S)
1776 #define ICE_XLT_KB_FL01		GENMASK_ULL(55 - ICE_XLT_KB_X1AS_S, \
1777 					    47 - ICE_XLT_KB_X1AS_S)
1778 #define ICE_XLT_KB_FL02		GENMASK_ULL(64 - ICE_XLT_KB_X1AS_S, \
1779 					    56 - ICE_XLT_KB_X1AS_S)
1780 #define ICE_XLT_KB_FL03		GENMASK_ULL(73 - ICE_XLT_KB_X1AS_S, \
1781 					    65 - ICE_XLT_KB_X1AS_S)
1782 #define ICE_XLT_KB_FL04		GENMASK_ULL(82 - ICE_XLT_KB_X1AS_S, \
1783 					    74 - ICE_XLT_KB_X1AS_S)
1784 #define ICE_XLT_KB_FL05		GENMASK_ULL(91 - ICE_XLT_KB_X1AS_S, \
1785 					    83 - ICE_XLT_KB_X1AS_S)
1786 #define ICE_XLT_KB_FL06_S	92	/* offset for the 2nd 64-bits field */
1787 #define ICE_XLT_KB_FL06_IDD	(ICE_XLT_KB_FL06_S / BITS_PER_BYTE)
1788 #define ICE_XLT_KB_FL06_OFF	(ICE_XLT_KB_FL06_S % BITS_PER_BYTE)
1789 #define ICE_XLT_KB_FL06		GENMASK_ULL(100 - ICE_XLT_KB_FL06_S, \
1790 					    92 - ICE_XLT_KB_FL06_S)
1791 #define ICE_XLT_KB_FL07		GENMASK_ULL(109 - ICE_XLT_KB_FL06_S, \
1792 					    101 - ICE_XLT_KB_FL06_S)
1793 #define ICE_XLT_KB_FL08		GENMASK_ULL(118 - ICE_XLT_KB_FL06_S, \
1794 					    110 - ICE_XLT_KB_FL06_S)
1795 #define ICE_XLT_KB_FL09		GENMASK_ULL(127 - ICE_XLT_KB_FL06_S, \
1796 					    119 - ICE_XLT_KB_FL06_S)
1797 #define ICE_XLT_KB_FL10		GENMASK_ULL(136 - ICE_XLT_KB_FL06_S, \
1798 					    128 - ICE_XLT_KB_FL06_S)
1799 #define ICE_XLT_KB_FL11		GENMASK_ULL(145 - ICE_XLT_KB_FL06_S, \
1800 					    137 - ICE_XLT_KB_FL06_S)
1801 #define ICE_XLT_KB_FL12_S	146	/* offset for the 3rd 64-bits field */
1802 #define ICE_XLT_KB_FL12_IDD	(ICE_XLT_KB_FL12_S / BITS_PER_BYTE)
1803 #define ICE_XLT_KB_FL12_OFF	(ICE_XLT_KB_FL12_S % BITS_PER_BYTE)
1804 #define ICE_XLT_KB_FL12		GENMASK_ULL(154 - ICE_XLT_KB_FL12_S, \
1805 					    146 - ICE_XLT_KB_FL12_S)
1806 #define ICE_XLT_KB_FL13		GENMASK_ULL(163 - ICE_XLT_KB_FL12_S, \
1807 					    155 - ICE_XLT_KB_FL12_S)
1808 #define ICE_XLT_KB_FL14		GENMASK_ULL(181 - ICE_XLT_KB_FL12_S, \
1809 					    164 - ICE_XLT_KB_FL12_S)
1810 #define ICE_XLT_KB_X1MS		GENMASK_ULL(186 - ICE_XLT_KB_FL12_S, \
1811 					    182 - ICE_XLT_KB_FL12_S)
1812 #define ICE_XLT_KB_X2MS		GENMASK_ULL(191 - ICE_XLT_KB_FL12_S, \
1813 					    187 - ICE_XLT_KB_FL12_S)
1814 
1815 /**
1816  * ice_kb_entry_init - parse 192 bits of XLT Key Builder entry
1817  * @entry: pointer to the XLT Key Builder entry structure
1818  * @data: XLT Key Builder entry data to be parsed
1819  */
ice_kb_entry_init(struct ice_xlt_kb_entry * entry,u8 * data)1820 static void ice_kb_entry_init(struct ice_xlt_kb_entry *entry, u8 *data)
1821 {
1822 	u8 i = 0;
1823 	u64 d64;
1824 
1825 	d64 = *((u64 *)&data[ICE_XLT_KB_X1AS_IDD]) >> ICE_XLT_KB_X1AS_OFF;
1826 
1827 	entry->xlt1_ad_sel	= FIELD_GET(ICE_XLT_KB_X1AS, d64);
1828 	entry->xlt2_ad_sel	= FIELD_GET(ICE_XLT_KB_X2AS, d64);
1829 
1830 	entry->flg0_14_sel[i++]	= FIELD_GET(ICE_XLT_KB_FL00, d64);
1831 	entry->flg0_14_sel[i++]	= FIELD_GET(ICE_XLT_KB_FL01, d64);
1832 	entry->flg0_14_sel[i++]	= FIELD_GET(ICE_XLT_KB_FL02, d64);
1833 	entry->flg0_14_sel[i++]	= FIELD_GET(ICE_XLT_KB_FL03, d64);
1834 	entry->flg0_14_sel[i++]	= FIELD_GET(ICE_XLT_KB_FL04, d64);
1835 	entry->flg0_14_sel[i++]	= FIELD_GET(ICE_XLT_KB_FL05, d64);
1836 
1837 	d64 = *((u64 *)&data[ICE_XLT_KB_FL06_IDD]) >> ICE_XLT_KB_FL06_OFF;
1838 
1839 	entry->flg0_14_sel[i++]	= FIELD_GET(ICE_XLT_KB_FL06, d64);
1840 	entry->flg0_14_sel[i++]	= FIELD_GET(ICE_XLT_KB_FL07, d64);
1841 	entry->flg0_14_sel[i++]	= FIELD_GET(ICE_XLT_KB_FL08, d64);
1842 	entry->flg0_14_sel[i++]	= FIELD_GET(ICE_XLT_KB_FL09, d64);
1843 	entry->flg0_14_sel[i++]	= FIELD_GET(ICE_XLT_KB_FL10, d64);
1844 	entry->flg0_14_sel[i++]	= FIELD_GET(ICE_XLT_KB_FL11, d64);
1845 
1846 	d64 = *((u64 *)&data[ICE_XLT_KB_FL12_IDD]) >> ICE_XLT_KB_FL12_OFF;
1847 
1848 	entry->flg0_14_sel[i++]	= FIELD_GET(ICE_XLT_KB_FL12, d64);
1849 	entry->flg0_14_sel[i++]	= FIELD_GET(ICE_XLT_KB_FL13, d64);
1850 	entry->flg0_14_sel[i]	= FIELD_GET(ICE_XLT_KB_FL14, d64);
1851 
1852 	entry->xlt1_md_sel	= FIELD_GET(ICE_XLT_KB_X1MS, d64);
1853 	entry->xlt2_md_sel	= FIELD_GET(ICE_XLT_KB_X2MS, d64);
1854 }
1855 
1856 #define ICE_XLT_KB_X1PM_OFF	0
1857 #define ICE_XLT_KB_X2PM_OFF	1
1858 #define ICE_XLT_KB_PIPM_OFF	2
1859 #define ICE_XLT_KB_FL15_OFF	4
1860 #define ICE_XLT_KB_TBL_OFF	12
1861 
1862 /**
1863  * ice_parse_kb_data - parse 204 bits of XLT Key Build Table
1864  * @hw: pointer to the hardware structure
1865  * @kb: pointer to the XLT Key Build Table structure
1866  * @data: XLT Key Build Table data to be parsed
1867  */
ice_parse_kb_data(struct ice_hw * hw,struct ice_xlt_kb * kb,void * data)1868 static void ice_parse_kb_data(struct ice_hw *hw, struct ice_xlt_kb *kb,
1869 			      void *data)
1870 {
1871 	u8 *buf = data;
1872 	int i;
1873 
1874 	kb->xlt1_pm	= buf[ICE_XLT_KB_X1PM_OFF];
1875 	kb->xlt2_pm	= buf[ICE_XLT_KB_X2PM_OFF];
1876 	kb->prof_id_pm	= buf[ICE_XLT_KB_PIPM_OFF];
1877 
1878 	kb->flag15 = *(u64 *)&buf[ICE_XLT_KB_FL15_OFF];
1879 	for (i = 0; i < ICE_XLT_KB_TBL_CNT; i++)
1880 		ice_kb_entry_init(&kb->entries[i],
1881 				  &buf[ICE_XLT_KB_TBL_OFF +
1882 				       i * ICE_XLT_KB_TBL_ENTRY_SIZE]);
1883 
1884 	if (hw->debug_mask & ICE_DBG_PARSER)
1885 		ice_xlt_kb_dump(hw, kb);
1886 }
1887 
ice_xlt_kb_get(struct ice_hw * hw,u32 sect_type)1888 static struct ice_xlt_kb *ice_xlt_kb_get(struct ice_hw *hw, u32 sect_type)
1889 {
1890 	struct ice_pkg_enum state = {};
1891 	struct ice_seg *seg = hw->seg;
1892 	struct ice_xlt_kb *kb;
1893 	void *data;
1894 
1895 	if (!seg)
1896 		return ERR_PTR(-EINVAL);
1897 
1898 	kb = kzalloc(sizeof(*kb), GFP_KERNEL);
1899 	if (!kb)
1900 		return ERR_PTR(-ENOMEM);
1901 
1902 	data = ice_pkg_enum_section(seg, &state, sect_type);
1903 	if (!data) {
1904 		ice_debug(hw, ICE_DBG_PARSER, "failed to find section type %d.\n",
1905 			  sect_type);
1906 		kfree(kb);
1907 		return ERR_PTR(-EINVAL);
1908 	}
1909 
1910 	ice_parse_kb_data(hw, kb, data);
1911 
1912 	return kb;
1913 }
1914 
1915 /**
1916  * ice_xlt_kb_get_sw - create switch xlt key build
1917  * @hw: pointer to the hardware structure
1918  *
1919  * Return: a pointer to the allocated Key Builder table for Switch.
1920  */
ice_xlt_kb_get_sw(struct ice_hw * hw)1921 static struct ice_xlt_kb *ice_xlt_kb_get_sw(struct ice_hw *hw)
1922 {
1923 	return ice_xlt_kb_get(hw, ICE_SID_XLT_KEY_BUILDER_SW);
1924 }
1925 
1926 /**
1927  * ice_xlt_kb_get_acl - create acl xlt key build
1928  * @hw: pointer to the hardware structure
1929  *
1930  * Return: a pointer to the allocated Key Builder table for ACL.
1931  */
ice_xlt_kb_get_acl(struct ice_hw * hw)1932 static struct ice_xlt_kb *ice_xlt_kb_get_acl(struct ice_hw *hw)
1933 {
1934 	return ice_xlt_kb_get(hw, ICE_SID_XLT_KEY_BUILDER_ACL);
1935 }
1936 
1937 /**
1938  * ice_xlt_kb_get_fd - create fdir xlt key build
1939  * @hw: pointer to the hardware structure
1940  *
1941  * Return: a pointer to the allocated Key Builder table for Flow Director.
1942  */
ice_xlt_kb_get_fd(struct ice_hw * hw)1943 static struct ice_xlt_kb *ice_xlt_kb_get_fd(struct ice_hw *hw)
1944 {
1945 	return ice_xlt_kb_get(hw, ICE_SID_XLT_KEY_BUILDER_FD);
1946 }
1947 
1948 /**
1949  * ice_xlt_kb_get_rss - create rss xlt key build
1950  * @hw: pointer to the hardware structure
1951  *
1952  * Return: a pointer to the allocated Key Builder table for RSS.
1953  */
ice_xlt_kb_get_rss(struct ice_hw * hw)1954 static struct ice_xlt_kb *ice_xlt_kb_get_rss(struct ice_hw *hw)
1955 {
1956 	return ice_xlt_kb_get(hw, ICE_SID_XLT_KEY_BUILDER_RSS);
1957 }
1958 
1959 #define ICE_XLT_KB_MASK		GENMASK_ULL(5, 0)
1960 
1961 /**
1962  * ice_xlt_kb_flag_get - aggregate 64 bits packet flag into 16 bits xlt flag
1963  * @kb: xlt key build
1964  * @pkt_flag: 64 bits packet flag
1965  *
1966  * Return: XLT flag or 0 if @pkt_flag = 0.
1967  */
ice_xlt_kb_flag_get(struct ice_xlt_kb * kb,u64 pkt_flag)1968 u16 ice_xlt_kb_flag_get(struct ice_xlt_kb *kb, u64 pkt_flag)
1969 {
1970 	struct ice_xlt_kb_entry *entry = &kb->entries[0];
1971 	u16 flag = 0;
1972 	int i;
1973 
1974 	/* check flag 15 */
1975 	if (kb->flag15 & pkt_flag)
1976 		flag = BIT(ICE_XLT_KB_FLAG0_14_CNT);
1977 
1978 	/* check flag 0 - 14 */
1979 	for (i = 0; i < ICE_XLT_KB_FLAG0_14_CNT; i++) {
1980 		/* only check first entry */
1981 		u16 idx = entry->flg0_14_sel[i] & ICE_XLT_KB_MASK;
1982 
1983 		if (pkt_flag & BIT(idx))
1984 			flag |= (u16)BIT(i);
1985 	}
1986 
1987 	return flag;
1988 }
1989 
1990 /*** Parser API ***/
1991 /**
1992  * ice_parser_create - create a parser instance
1993  * @hw: pointer to the hardware structure
1994  *
1995  * Return: a pointer to the allocated parser instance or ERR_PTR
1996  * in case of error.
1997  */
ice_parser_create(struct ice_hw * hw)1998 struct ice_parser *ice_parser_create(struct ice_hw *hw)
1999 {
2000 	struct ice_parser *p;
2001 	void *err;
2002 
2003 	p = kzalloc(sizeof(*p), GFP_KERNEL);
2004 	if (!p)
2005 		return ERR_PTR(-ENOMEM);
2006 
2007 	p->hw = hw;
2008 	p->rt.psr = p;
2009 
2010 	p->imem_table = ice_imem_table_get(hw);
2011 	if (IS_ERR(p->imem_table)) {
2012 		err = p->imem_table;
2013 		goto err;
2014 	}
2015 
2016 	p->mi_table = ice_metainit_table_get(hw);
2017 	if (IS_ERR(p->mi_table)) {
2018 		err = p->mi_table;
2019 		goto err;
2020 	}
2021 
2022 	p->pg_cam_table = ice_pg_cam_table_get(hw);
2023 	if (IS_ERR(p->pg_cam_table)) {
2024 		err = p->pg_cam_table;
2025 		goto err;
2026 	}
2027 
2028 	p->pg_sp_cam_table = ice_pg_sp_cam_table_get(hw);
2029 	if (IS_ERR(p->pg_sp_cam_table)) {
2030 		err = p->pg_sp_cam_table;
2031 		goto err;
2032 	}
2033 
2034 	p->pg_nm_cam_table = ice_pg_nm_cam_table_get(hw);
2035 	if (IS_ERR(p->pg_nm_cam_table)) {
2036 		err = p->pg_nm_cam_table;
2037 		goto err;
2038 	}
2039 
2040 	p->pg_nm_sp_cam_table = ice_pg_nm_sp_cam_table_get(hw);
2041 	if (IS_ERR(p->pg_nm_sp_cam_table)) {
2042 		err = p->pg_nm_sp_cam_table;
2043 		goto err;
2044 	}
2045 
2046 	p->bst_tcam_table = ice_bst_tcam_table_get(hw);
2047 	if (IS_ERR(p->bst_tcam_table)) {
2048 		err = p->bst_tcam_table;
2049 		goto err;
2050 	}
2051 
2052 	p->bst_lbl_table = ice_bst_lbl_table_get(hw);
2053 	if (IS_ERR(p->bst_lbl_table)) {
2054 		err = p->bst_lbl_table;
2055 		goto err;
2056 	}
2057 
2058 	p->ptype_mk_tcam_table = ice_ptype_mk_tcam_table_get(hw);
2059 	if (IS_ERR(p->ptype_mk_tcam_table)) {
2060 		err = p->ptype_mk_tcam_table;
2061 		goto err;
2062 	}
2063 
2064 	p->mk_grp_table = ice_mk_grp_table_get(hw);
2065 	if (IS_ERR(p->mk_grp_table)) {
2066 		err = p->mk_grp_table;
2067 		goto err;
2068 	}
2069 
2070 	p->proto_grp_table = ice_proto_grp_table_get(hw);
2071 	if (IS_ERR(p->proto_grp_table)) {
2072 		err = p->proto_grp_table;
2073 		goto err;
2074 	}
2075 
2076 	p->flg_rd_table = ice_flg_rd_table_get(hw);
2077 	if (IS_ERR(p->flg_rd_table)) {
2078 		err = p->flg_rd_table;
2079 		goto err;
2080 	}
2081 
2082 	p->xlt_kb_sw = ice_xlt_kb_get_sw(hw);
2083 	if (IS_ERR(p->xlt_kb_sw)) {
2084 		err = p->xlt_kb_sw;
2085 		goto err;
2086 	}
2087 
2088 	p->xlt_kb_acl = ice_xlt_kb_get_acl(hw);
2089 	if (IS_ERR(p->xlt_kb_acl)) {
2090 		err = p->xlt_kb_acl;
2091 		goto err;
2092 	}
2093 
2094 	p->xlt_kb_fd = ice_xlt_kb_get_fd(hw);
2095 	if (IS_ERR(p->xlt_kb_fd)) {
2096 		err = p->xlt_kb_fd;
2097 		goto err;
2098 	}
2099 
2100 	p->xlt_kb_rss = ice_xlt_kb_get_rss(hw);
2101 	if (IS_ERR(p->xlt_kb_rss)) {
2102 		err = p->xlt_kb_rss;
2103 		goto err;
2104 	}
2105 
2106 	return p;
2107 err:
2108 	ice_parser_destroy(p);
2109 	return err;
2110 }
2111 
2112 /**
2113  * ice_parser_destroy - destroy a parser instance
2114  * @psr: pointer to a parser instance
2115  */
ice_parser_destroy(struct ice_parser * psr)2116 void ice_parser_destroy(struct ice_parser *psr)
2117 {
2118 	kfree(psr->imem_table);
2119 	kfree(psr->mi_table);
2120 	kfree(psr->pg_cam_table);
2121 	kfree(psr->pg_sp_cam_table);
2122 	kfree(psr->pg_nm_cam_table);
2123 	kfree(psr->pg_nm_sp_cam_table);
2124 	kfree(psr->bst_tcam_table);
2125 	kfree(psr->bst_lbl_table);
2126 	kfree(psr->ptype_mk_tcam_table);
2127 	kfree(psr->mk_grp_table);
2128 	kfree(psr->proto_grp_table);
2129 	kfree(psr->flg_rd_table);
2130 	kfree(psr->xlt_kb_sw);
2131 	kfree(psr->xlt_kb_acl);
2132 	kfree(psr->xlt_kb_fd);
2133 	kfree(psr->xlt_kb_rss);
2134 
2135 	kfree(psr);
2136 }
2137 
2138 /**
2139  * ice_parser_run - parse on a packet in binary and return the result
2140  * @psr: pointer to a parser instance
2141  * @pkt_buf: packet data
2142  * @pkt_len: packet length
2143  * @rslt: input/output parameter to save parser result.
2144  *
2145  * Return: 0 on success or errno.
2146  */
ice_parser_run(struct ice_parser * psr,const u8 * pkt_buf,int pkt_len,struct ice_parser_result * rslt)2147 int ice_parser_run(struct ice_parser *psr, const u8 *pkt_buf,
2148 		   int pkt_len, struct ice_parser_result *rslt)
2149 {
2150 	ice_parser_rt_reset(&psr->rt);
2151 	ice_parser_rt_pktbuf_set(&psr->rt, pkt_buf, pkt_len);
2152 
2153 	return ice_parser_rt_execute(&psr->rt, rslt);
2154 }
2155 
2156 /**
2157  * ice_parser_result_dump - dump a parser result info
2158  * @hw: pointer to the hardware structure
2159  * @rslt: parser result info to dump
2160  */
ice_parser_result_dump(struct ice_hw * hw,struct ice_parser_result * rslt)2161 void ice_parser_result_dump(struct ice_hw *hw, struct ice_parser_result *rslt)
2162 {
2163 	struct device *dev = ice_hw_to_dev(hw);
2164 	int i;
2165 
2166 	dev_info(dev, "ptype = %d\n", rslt->ptype);
2167 	for (i = 0; i < rslt->po_num; i++)
2168 		dev_info(dev, "proto = %d, offset = %d\n",
2169 			 rslt->po[i].proto_id, rslt->po[i].offset);
2170 
2171 	dev_info(dev, "flags_psr = 0x%016llx\n", rslt->flags_psr);
2172 	dev_info(dev, "flags_pkt = 0x%016llx\n", rslt->flags_pkt);
2173 	dev_info(dev, "flags_sw = 0x%04x\n", rslt->flags_sw);
2174 	dev_info(dev, "flags_fd = 0x%04x\n", rslt->flags_fd);
2175 	dev_info(dev, "flags_rss = 0x%04x\n", rslt->flags_rss);
2176 }
2177 
2178 #define ICE_BT_VLD_KEY	0xFF
2179 #define ICE_BT_INV_KEY	0xFE
2180 
ice_bst_dvm_set(struct ice_parser * psr,enum ice_lbl_type type,bool on)2181 static void ice_bst_dvm_set(struct ice_parser *psr, enum ice_lbl_type type,
2182 			    bool on)
2183 {
2184 	u16 i = 0;
2185 
2186 	while (true) {
2187 		struct ice_bst_tcam_item *item;
2188 		u8 key;
2189 
2190 		item = ice_bst_tcam_search(psr->bst_tcam_table,
2191 					   psr->bst_lbl_table,
2192 					   type, &i);
2193 		if (!item)
2194 			break;
2195 
2196 		key = on ? ICE_BT_VLD_KEY : ICE_BT_INV_KEY;
2197 		item->key[ICE_BT_VM_OFF] = key;
2198 		item->key_inv[ICE_BT_VM_OFF] = key;
2199 		i++;
2200 	}
2201 }
2202 
2203 /**
2204  * ice_parser_dvm_set - configure double vlan mode for parser
2205  * @psr: pointer to a parser instance
2206  * @on: true to turn on; false to turn off
2207  */
ice_parser_dvm_set(struct ice_parser * psr,bool on)2208 void ice_parser_dvm_set(struct ice_parser *psr, bool on)
2209 {
2210 	ice_bst_dvm_set(psr, ICE_LBL_BST_TYPE_DVM, on);
2211 	ice_bst_dvm_set(psr, ICE_LBL_BST_TYPE_SVM, !on);
2212 }
2213 
ice_tunnel_port_set(struct ice_parser * psr,enum ice_lbl_type type,u16 udp_port,bool on)2214 static int ice_tunnel_port_set(struct ice_parser *psr, enum ice_lbl_type type,
2215 			       u16 udp_port, bool on)
2216 {
2217 	u8 *buf = (u8 *)&udp_port;
2218 	u16 i = 0;
2219 
2220 	while (true) {
2221 		struct ice_bst_tcam_item *item;
2222 
2223 		item = ice_bst_tcam_search(psr->bst_tcam_table,
2224 					   psr->bst_lbl_table,
2225 					   type, &i);
2226 		if (!item)
2227 			break;
2228 
2229 		/* found empty slot to add */
2230 		if (on && item->key[ICE_BT_TUN_PORT_OFF_H] == ICE_BT_INV_KEY &&
2231 		    item->key_inv[ICE_BT_TUN_PORT_OFF_H] == ICE_BT_INV_KEY) {
2232 			item->key_inv[ICE_BT_TUN_PORT_OFF_L] =
2233 						buf[ICE_UDP_PORT_OFF_L];
2234 			item->key_inv[ICE_BT_TUN_PORT_OFF_H] =
2235 						buf[ICE_UDP_PORT_OFF_H];
2236 
2237 			item->key[ICE_BT_TUN_PORT_OFF_L] =
2238 				ICE_BT_VLD_KEY - buf[ICE_UDP_PORT_OFF_L];
2239 			item->key[ICE_BT_TUN_PORT_OFF_H] =
2240 				ICE_BT_VLD_KEY - buf[ICE_UDP_PORT_OFF_H];
2241 
2242 			return 0;
2243 		/* found a matched slot to delete */
2244 		} else if (!on &&
2245 			   (item->key_inv[ICE_BT_TUN_PORT_OFF_L] ==
2246 				buf[ICE_UDP_PORT_OFF_L] ||
2247 			    item->key_inv[ICE_BT_TUN_PORT_OFF_H] ==
2248 				buf[ICE_UDP_PORT_OFF_H])) {
2249 			item->key_inv[ICE_BT_TUN_PORT_OFF_L] = ICE_BT_VLD_KEY;
2250 			item->key_inv[ICE_BT_TUN_PORT_OFF_H] = ICE_BT_INV_KEY;
2251 
2252 			item->key[ICE_BT_TUN_PORT_OFF_L] = ICE_BT_VLD_KEY;
2253 			item->key[ICE_BT_TUN_PORT_OFF_H] = ICE_BT_INV_KEY;
2254 
2255 			return 0;
2256 		}
2257 		i++;
2258 	}
2259 
2260 	return -EINVAL;
2261 }
2262 
2263 /**
2264  * ice_parser_vxlan_tunnel_set - configure vxlan tunnel for parser
2265  * @psr: pointer to a parser instance
2266  * @udp_port: vxlan tunnel port in UDP header
2267  * @on: true to turn on; false to turn off
2268  *
2269  * Return: 0 on success or errno on failure.
2270  */
ice_parser_vxlan_tunnel_set(struct ice_parser * psr,u16 udp_port,bool on)2271 int ice_parser_vxlan_tunnel_set(struct ice_parser *psr,
2272 				u16 udp_port, bool on)
2273 {
2274 	return ice_tunnel_port_set(psr, ICE_LBL_BST_TYPE_VXLAN, udp_port, on);
2275 }
2276 
2277 /**
2278  * ice_parser_geneve_tunnel_set - configure geneve tunnel for parser
2279  * @psr: pointer to a parser instance
2280  * @udp_port: geneve tunnel port in UDP header
2281  * @on: true to turn on; false to turn off
2282  *
2283  * Return: 0 on success or errno on failure.
2284  */
ice_parser_geneve_tunnel_set(struct ice_parser * psr,u16 udp_port,bool on)2285 int ice_parser_geneve_tunnel_set(struct ice_parser *psr,
2286 				 u16 udp_port, bool on)
2287 {
2288 	return ice_tunnel_port_set(psr, ICE_LBL_BST_TYPE_GENEVE, udp_port, on);
2289 }
2290 
2291 /**
2292  * ice_parser_ecpri_tunnel_set - configure ecpri tunnel for parser
2293  * @psr: pointer to a parser instance
2294  * @udp_port: ecpri tunnel port in UDP header
2295  * @on: true to turn on; false to turn off
2296  *
2297  * Return: 0 on success or errno on failure.
2298  */
ice_parser_ecpri_tunnel_set(struct ice_parser * psr,u16 udp_port,bool on)2299 int ice_parser_ecpri_tunnel_set(struct ice_parser *psr,
2300 				u16 udp_port, bool on)
2301 {
2302 	return ice_tunnel_port_set(psr, ICE_LBL_BST_TYPE_UDP_ECPRI,
2303 				   udp_port, on);
2304 }
2305 
2306 /**
2307  * ice_nearest_proto_id - find nearest protocol ID
2308  * @rslt: pointer to a parser result instance
2309  * @offset: a min value for the protocol offset
2310  * @proto_id: the protocol ID (output)
2311  * @proto_off: the protocol offset (output)
2312  *
2313  * From the protocols in @rslt, find the nearest protocol that has offset
2314  * larger than @offset.
2315  *
2316  * Return: if true, the protocol's ID and offset
2317  */
ice_nearest_proto_id(struct ice_parser_result * rslt,u16 offset,u8 * proto_id,u16 * proto_off)2318 static bool ice_nearest_proto_id(struct ice_parser_result *rslt, u16 offset,
2319 				 u8 *proto_id, u16 *proto_off)
2320 {
2321 	u16 dist = U16_MAX;
2322 	u8 proto = 0;
2323 	int i;
2324 
2325 	for (i = 0; i < rslt->po_num; i++) {
2326 		if (offset < rslt->po[i].offset)
2327 			continue;
2328 		if (offset - rslt->po[i].offset < dist) {
2329 			proto = rslt->po[i].proto_id;
2330 			dist = offset - rslt->po[i].offset;
2331 		}
2332 	}
2333 
2334 	if (dist % 2)
2335 		return false;
2336 
2337 	*proto_id = proto;
2338 	*proto_off = dist;
2339 
2340 	return true;
2341 }
2342 
2343 /* default flag mask to cover GTP_EH_PDU, GTP_EH_PDU_LINK and TUN2
2344  * In future, the flag masks should learn from DDP
2345  */
2346 #define ICE_KEYBUILD_FLAG_MASK_DEFAULT_SW	0x4002
2347 #define ICE_KEYBUILD_FLAG_MASK_DEFAULT_ACL	0x0000
2348 #define ICE_KEYBUILD_FLAG_MASK_DEFAULT_FD	0x6080
2349 #define ICE_KEYBUILD_FLAG_MASK_DEFAULT_RSS	0x6010
2350 
2351 /**
2352  * ice_parser_profile_init - initialize a FXP profile based on parser result
2353  * @rslt: a instance of a parser result
2354  * @pkt_buf: packet data buffer
2355  * @msk_buf: packet mask buffer
2356  * @buf_len: packet length
2357  * @blk: FXP pipeline stage
2358  * @prof: input/output parameter to save the profile
2359  *
2360  * Return: 0 on success or errno on failure.
2361  */
ice_parser_profile_init(struct ice_parser_result * rslt,const u8 * pkt_buf,const u8 * msk_buf,int buf_len,enum ice_block blk,struct ice_parser_profile * prof)2362 int ice_parser_profile_init(struct ice_parser_result *rslt,
2363 			    const u8 *pkt_buf, const u8 *msk_buf,
2364 			    int buf_len, enum ice_block blk,
2365 			    struct ice_parser_profile *prof)
2366 {
2367 	u8 proto_id = U8_MAX;
2368 	u16 proto_off = 0;
2369 	u16 off;
2370 
2371 	memset(prof, 0, sizeof(*prof));
2372 	set_bit(rslt->ptype, prof->ptypes);
2373 	if (blk == ICE_BLK_SW) {
2374 		prof->flags	= rslt->flags_sw;
2375 		prof->flags_msk	= ICE_KEYBUILD_FLAG_MASK_DEFAULT_SW;
2376 	} else if (blk == ICE_BLK_ACL) {
2377 		prof->flags	= rslt->flags_acl;
2378 		prof->flags_msk	= ICE_KEYBUILD_FLAG_MASK_DEFAULT_ACL;
2379 	} else if (blk == ICE_BLK_FD) {
2380 		prof->flags	= rslt->flags_fd;
2381 		prof->flags_msk	= ICE_KEYBUILD_FLAG_MASK_DEFAULT_FD;
2382 	} else if (blk == ICE_BLK_RSS) {
2383 		prof->flags	= rslt->flags_rss;
2384 		prof->flags_msk	= ICE_KEYBUILD_FLAG_MASK_DEFAULT_RSS;
2385 	} else {
2386 		return -EINVAL;
2387 	}
2388 
2389 	for (off = 0; off < buf_len - 1; off++) {
2390 		if (msk_buf[off] == 0 && msk_buf[off + 1] == 0)
2391 			continue;
2392 		if (!ice_nearest_proto_id(rslt, off, &proto_id, &proto_off))
2393 			continue;
2394 		if (prof->fv_num >= ICE_PARSER_FV_MAX)
2395 			return -EINVAL;
2396 
2397 		prof->fv[prof->fv_num].proto_id	= proto_id;
2398 		prof->fv[prof->fv_num].offset	= proto_off;
2399 		prof->fv[prof->fv_num].spec	= *(const u16 *)&pkt_buf[off];
2400 		prof->fv[prof->fv_num].msk	= *(const u16 *)&msk_buf[off];
2401 		prof->fv_num++;
2402 	}
2403 
2404 	return 0;
2405 }
2406 
2407 /**
2408  * ice_parser_profile_dump - dump an FXP profile info
2409  * @hw: pointer to the hardware structure
2410  * @prof: profile info to dump
2411  */
ice_parser_profile_dump(struct ice_hw * hw,struct ice_parser_profile * prof)2412 void ice_parser_profile_dump(struct ice_hw *hw,
2413 			     struct ice_parser_profile *prof)
2414 {
2415 	struct device *dev = ice_hw_to_dev(hw);
2416 	u16 i;
2417 
2418 	dev_info(dev, "ptypes:\n");
2419 	for (i = 0; i < ICE_FLOW_PTYPE_MAX; i++)
2420 		if (test_bit(i, prof->ptypes))
2421 			dev_info(dev, "\t%u\n", i);
2422 
2423 	for (i = 0; i < prof->fv_num; i++)
2424 		dev_info(dev, "proto = %u, offset = %2u, spec = 0x%04x, mask = 0x%04x\n",
2425 			 prof->fv[i].proto_id, prof->fv[i].offset,
2426 			 prof->fv[i].spec, prof->fv[i].msk);
2427 
2428 	dev_info(dev, "flags = 0x%04x\n", prof->flags);
2429 	dev_info(dev, "flags_msk = 0x%04x\n", prof->flags_msk);
2430 }
2431