xref: /dpdk/drivers/net/ice/base/ice_bst_tcam.c (revision cc3f51e4)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2001-2021 Intel Corporation
3  */
4 
5 #include "ice_common.h"
6 #include "ice_parser_util.h"
7 
8 #define ICE_BST_TCAM_TABLE_SIZE 256
9 
_bst_np_kb_dump(struct ice_hw * hw,struct ice_np_keybuilder * kb)10 static void _bst_np_kb_dump(struct ice_hw *hw, struct ice_np_keybuilder *kb)
11 {
12 	ice_info(hw, "next proto key builder:\n");
13 	ice_info(hw, "\tops = %d\n", kb->ops);
14 	ice_info(hw, "\tstart_or_reg0 = %d\n", kb->start_or_reg0);
15 	ice_info(hw, "\tlen_or_reg1 = %d\n", kb->len_or_reg1);
16 }
17 
_bst_pg_kb_dump(struct ice_hw * hw,struct ice_pg_keybuilder * kb)18 static void _bst_pg_kb_dump(struct ice_hw *hw, struct ice_pg_keybuilder *kb)
19 {
20 	ice_info(hw, "parse graph key builder:\n");
21 	ice_info(hw, "\tflag0_ena = %d\n", kb->flag0_ena);
22 	ice_info(hw, "\tflag1_ena = %d\n", kb->flag1_ena);
23 	ice_info(hw, "\tflag2_ena = %d\n", kb->flag2_ena);
24 	ice_info(hw, "\tflag3_ena = %d\n", kb->flag3_ena);
25 	ice_info(hw, "\tflag0_idx = %d\n", kb->flag0_idx);
26 	ice_info(hw, "\tflag1_idx = %d\n", kb->flag1_idx);
27 	ice_info(hw, "\tflag2_idx = %d\n", kb->flag2_idx);
28 	ice_info(hw, "\tflag3_idx = %d\n", kb->flag3_idx);
29 	ice_info(hw, "\talu_reg_idx = %d\n", kb->alu_reg_idx);
30 }
31 
_bst_alu_dump(struct ice_hw * hw,struct ice_alu * alu,int index)32 static void _bst_alu_dump(struct ice_hw *hw, struct ice_alu *alu, int index)
33 {
34 	ice_info(hw, "alu%d:\n", index);
35 	ice_info(hw, "\topc = %d\n", alu->opc);
36 	ice_info(hw, "\tsrc_start = %d\n", alu->src_start);
37 	ice_info(hw, "\tsrc_len = %d\n", alu->src_len);
38 	ice_info(hw, "\tshift_xlate_select = %d\n", alu->shift_xlate_select);
39 	ice_info(hw, "\tshift_xlate_key = %d\n", alu->shift_xlate_key);
40 	ice_info(hw, "\tsrc_reg_id = %d\n", alu->src_reg_id);
41 	ice_info(hw, "\tdst_reg_id = %d\n", alu->dst_reg_id);
42 	ice_info(hw, "\tinc0 = %d\n", alu->inc0);
43 	ice_info(hw, "\tinc1 = %d\n", alu->inc1);
44 	ice_info(hw, "\tproto_offset_opc = %d\n", alu->proto_offset_opc);
45 	ice_info(hw, "\tproto_offset = %d\n", alu->proto_offset);
46 	ice_info(hw, "\tbranch_addr = %d\n", alu->branch_addr);
47 	ice_info(hw, "\timm = %d\n", alu->imm);
48 	ice_info(hw, "\tdst_start = %d\n", alu->dst_start);
49 	ice_info(hw, "\tdst_len = %d\n", alu->dst_len);
50 	ice_info(hw, "\tflags_extr_imm = %d\n", alu->flags_extr_imm);
51 	ice_info(hw, "\tflags_start_imm= %d\n", alu->flags_start_imm);
52 }
53 
54 /**
55  * ice_bst_tcam_dump - dump a boost tcam info
56  * @ice_hw: pointer to the hardware structure
57  * @item: boost tcam to dump
58  */
ice_bst_tcam_dump(struct ice_hw * hw,struct ice_bst_tcam_item * item)59 void ice_bst_tcam_dump(struct ice_hw *hw, struct ice_bst_tcam_item *item)
60 {
61 	int i;
62 
63 	ice_info(hw, "address = %d\n", item->address);
64 	ice_info(hw, "key    :");
65 	for (i = 0; i < 20; i++)
66 		ice_info(hw, "%02x ", item->key[i]);
67 	ice_info(hw, "\n");
68 	ice_info(hw, "key_inv:");
69 	for (i = 0; i < 20; i++)
70 		ice_info(hw, "%02x ", item->key_inv[i]);
71 	ice_info(hw, "\n");
72 	ice_info(hw, "hit_idx_grp = %d\n", item->hit_idx_grp);
73 	ice_info(hw, "pg_pri = %d\n", item->pg_pri);
74 	_bst_np_kb_dump(hw, &item->np_kb);
75 	_bst_pg_kb_dump(hw, &item->pg_kb);
76 	_bst_alu_dump(hw, &item->alu0, 0);
77 	_bst_alu_dump(hw, &item->alu1, 1);
78 	_bst_alu_dump(hw, &item->alu2, 2);
79 }
80 
81 /** The function parses a 96 bits ALU entry with below format:
82  *  BIT 0-5:	Opcode (alu->opc)
83  *  BIT 6-13:	Source Start (alu->src_start)
84  *  BIT 14-18:	Source Length (alu->src_len)
85  *  BIT 19:	Shift/Xlate Select (alu->shift_xlate_select)
86  *  BIT 20-23:	Shift/Xlate Key (alu->shift_xlate_key)
87  *  BIT 24-30:	Source Register ID (alu->src_reg_id)
88  *  BIT 31-37:	Dest. Register ID (alu->dst_reg_id)
89  *  BIT 38:	Inc0 (alu->inc0)
90  *  BIT 39:	Inc1:(alu->inc1)
91  *  BIT 40:41	Protocol Offset Opcode (alu->proto_offset_opc)
92  *  BIT 42:49	Protocol Offset (alu->proto_offset)
93  *  BIT 50:57	Branch Address (alu->branch_addr)
94  *  BIT 58:73	Immediate (alu->imm)
95  *  BIT 74	Dedicated Flags Enable (alu->dedicate_flags_ena)
96  *  BIT 75:80	Dest. Start (alu->dst_start)
97  *  BIT 81:86	Dest. Length (alu->dst_len)
98  *  BIT 87	Flags Extract Imm. (alu->flags_extr_imm)
99  *  BIT 88:95	Flags Start/Immediate (alu->flags_start_imm)
100  *
101  *  NOTE: the first 7 bits are skipped as the start bit is not
102  *  byte aligned.
103  */
_bst_alu_init(struct ice_alu * alu,u8 * data)104 static void _bst_alu_init(struct ice_alu *alu, u8 *data)
105 {
106 	u64 d64 = *(u64 *)data >> 7;
107 
108 	alu->opc = (enum ice_alu_opcode)(d64 & 0x3f);
109 	alu->src_start = (u8)((d64 >> 6) & 0xff);
110 	alu->src_len = (u8)((d64 >> 14) & 0x1f);
111 	alu->shift_xlate_select = ((d64 >> 19) & 0x1) != 0;
112 	alu->shift_xlate_key = (u8)((d64 >> 20) & 0xf);
113 	alu->src_reg_id = (u8)((d64 >> 24) & 0x7f);
114 	alu->dst_reg_id = (u8)((d64 >> 31) & 0x7f);
115 	alu->inc0 = ((d64 >> 38) & 0x1) != 0;
116 	alu->inc1 = ((d64 >> 39) & 0x1) != 0;
117 	alu->proto_offset_opc = (u8)((d64 >> 40) & 0x3);
118 	alu->proto_offset = (u8)((d64 >> 42) & 0xff);
119 
120 	d64 = *(u64 *)(&data[6]) >> 9;
121 
122 	alu->branch_addr = (u8)(d64 & 0xff);
123 	alu->imm = (u16)((d64 >> 8) & 0xffff);
124 	alu->dedicate_flags_ena = ((d64 >> 24) & 0x1) != 0;
125 	alu->dst_start = (u8)((d64 >> 25) & 0x3f);
126 	alu->dst_len = (u8)((d64 >> 31) & 0x3f);
127 	alu->flags_extr_imm = ((d64 >> 37) & 0x1) != 0;
128 	alu->flags_start_imm = (u8)((d64 >> 38) & 0xff);
129 }
130 
131 /** The function parses a 35 bits Parse Graph Key Build with below format:
132  *  BIT 0:	Flag 0 Enable (kb->flag0_ena)
133  *  BIT 1-6:	Flag 0 Index (kb->flag0_idx)
134  *  BIT 7:	Flag 1 Enable (kb->flag1_ena)
135  *  BIT 8-13:	Flag 1 Index (kb->flag1_idx)
136  *  BIT 14:	Flag 2 Enable (kb->flag2_ena)
137  *  BIT 15-20:	Flag 2 Index (kb->flag2_idx)
138  *  BIT 21:	Flag 3 Enable (kb->flag3_ena)
139  *  BIT 22-27:	Flag 3 Index (kb->flag3_idx)
140  *  BIT 28-34:	ALU Register Index (kb->alu_reg_idx)
141  */
_bst_pgkb_init(struct ice_pg_keybuilder * kb,u64 data)142 static void _bst_pgkb_init(struct ice_pg_keybuilder *kb, u64 data)
143 {
144 	kb->flag0_ena = (data & 0x1) != 0;
145 	kb->flag0_idx = (u8)((data >> 1) & 0x3f);
146 	kb->flag1_ena = ((data >> 7) & 0x1) != 0;
147 	kb->flag1_idx = (u8)((data >> 8) & 0x3f);
148 	kb->flag2_ena = ((data >> 14) & 0x1) != 0;
149 	kb->flag2_idx = (u8)((data >> 15) & 0x3f);
150 	kb->flag3_ena = ((data >> 21) & 0x1) != 0;
151 	kb->flag3_idx = (u8)((data >> 22) & 0x3f);
152 	kb->alu_reg_idx = (u8)((data >> 28) & 0x7f);
153 }
154 
155 /** The function parses a 18 bits Next Protocol Key Build with below format:
156  *  BIT 0-1:	Opcode kb->ops
157  *  BIT 2-9:	Start / Reg 0 (kb->start_or_reg0)
158  *  BIT 10-17:	Length / Reg 1 (kb->len_or_reg1)
159  */
_bst_npkb_init(struct ice_np_keybuilder * kb,u32 data)160 static void _bst_npkb_init(struct ice_np_keybuilder *kb, u32 data)
161 {
162 	kb->ops = (u8)(data & 0x3);
163 	kb->start_or_reg0 = (u8)((data >> 2) & 0xff);
164 	kb->len_or_reg1 = (u8)((data >> 10) & 0xff);
165 }
166 
167 /** The function parses a 704 bits Boost TCAM entry with below format:
168  *  BIT 0-15:	Address (ti->address)
169  *  BIT 16-31:	reserved
170  *  BIT 32-191: Key (ti->key)
171  *  BIT 192-351:Key Invert (ti->key_inv)
172  *  BIT 352-359:Boost Hit Index Group (ti->hit_idx_grp)
173  *  BIT 360-361:PG Priority (ti->pg_pri)
174  *  BIT 362-379:Next Proto Key Build (ti->np_kb)
175  *  BIT 380-414:PG Key Build (ti->pg_kb)
176  *  BIT 415-510:ALU 0 (ti->alu0)
177  *  BIT 511-606:ALU 1 (ti->alu1)
178  *  BIT 607-702:ALU 2 (ti->alu2)
179  *  BIT 703:	reserved
180  */
_bst_parse_item(struct ice_hw * hw,u16 idx,void * item,void * data,int size)181 static void _bst_parse_item(struct ice_hw *hw, u16 idx, void *item,
182 			    void *data, int size)
183 {
184 	struct ice_bst_tcam_item *ti = (struct ice_bst_tcam_item *)item;
185 	u8 *buf = (u8 *)data;
186 	int i;
187 
188 	ti->address = *(u16 *)buf;
189 
190 	for (i = 0; i < 20; i++)
191 		ti->key[i] = buf[4 + i];
192 	for (i = 0; i < 20; i++)
193 		ti->key_inv[i] = buf[24 + i];
194 	ti->hit_idx_grp = buf[44];
195 	ti->pg_pri = buf[45] & 0x3;
196 	_bst_npkb_init(&ti->np_kb, *(u32 *)&buf[45] >> 2);
197 	_bst_pgkb_init(&ti->pg_kb, *(u64 *)&buf[47] >> 4);
198 	_bst_alu_init(&ti->alu0, &buf[51]);
199 	_bst_alu_init(&ti->alu1, &buf[63]);
200 	_bst_alu_init(&ti->alu2, &buf[75]);
201 
202 	if (hw->debug_mask & ICE_DBG_PARSER)
203 		ice_bst_tcam_dump(hw, ti);
204 }
205 
206 /**
207  * ice_bst_tcam_table_get - create a boost tcam table
208  * @ice_hw: pointer to the hardware structure
209  */
ice_bst_tcam_table_get(struct ice_hw * hw)210 struct ice_bst_tcam_item *ice_bst_tcam_table_get(struct ice_hw *hw)
211 {
212 	return (struct ice_bst_tcam_item *)
213 		ice_parser_create_table(hw, ICE_SID_RXPARSER_BOOST_TCAM,
214 					sizeof(struct ice_bst_tcam_item),
215 					ICE_BST_TCAM_TABLE_SIZE,
216 					ice_parser_sect_item_get,
217 					_bst_parse_item, true);
218 }
219 
_parse_lbl_item(struct ice_hw * hw,u16 idx,void * item,void * data,int size)220 static void _parse_lbl_item(struct ice_hw *hw, u16 idx, void *item,
221 			    void *data, int size)
222 {
223 	ice_parse_item_dflt(hw, idx, item, data, size);
224 
225 	if (hw->debug_mask & ICE_DBG_PARSER)
226 		ice_lbl_dump(hw, (struct ice_lbl_item *)item);
227 }
228 
229 /**
230  * ice_bst_lbl_table_get - create a boost label table
231  * @ice_hw: pointer to the hardware structure
232  */
ice_bst_lbl_table_get(struct ice_hw * hw)233 struct ice_lbl_item *ice_bst_lbl_table_get(struct ice_hw *hw)
234 {
235 	return (struct ice_lbl_item *)
236 		ice_parser_create_table(hw, ICE_SID_LBL_RXPARSER_TMEM,
237 					sizeof(struct ice_lbl_item),
238 					ICE_BST_TCAM_TABLE_SIZE,
239 					ice_parser_sect_item_get,
240 					_parse_lbl_item, true);
241 }
242 
243 /**
244  * ice_bst_tcam_match - match a pattern on the boost tcam table
245  * @tcam_table: boost tcam table to search
246  * @pat: pattern to match
247  */
248 struct ice_bst_tcam_item *
ice_bst_tcam_match(struct ice_bst_tcam_item * tcam_table,u8 * pat)249 ice_bst_tcam_match(struct ice_bst_tcam_item *tcam_table, u8 *pat)
250 {
251 	int i;
252 
253 	for (i = 0; i < ICE_BST_TCAM_TABLE_SIZE; i++) {
254 		struct ice_bst_tcam_item *item = &tcam_table[i];
255 
256 		if (item->hit_idx_grp == 0)
257 			continue;
258 		if (ice_ternary_match(item->key, item->key_inv, pat, 20))
259 			return item;
260 	}
261 
262 	return NULL;
263 }
264 
_start_with(const char * prefix,const char * string)265 static bool _start_with(const char *prefix, const char *string)
266 {
267 	int len1 = strlen(prefix);
268 	int len2 = strlen(string);
269 
270 	if (len2 < len1)
271 		return false;
272 
273 	return !memcmp(prefix, string, len1);
274 }
275 
276 struct ice_bst_tcam_item *
ice_bst_tcam_search(struct ice_bst_tcam_item * tcam_table,struct ice_lbl_item * lbl_table,const char * prefix,u16 * start)277 ice_bst_tcam_search(struct ice_bst_tcam_item *tcam_table,
278 		    struct ice_lbl_item *lbl_table,
279 		    const char *prefix, u16 *start)
280 {
281 	u16 i = *start;
282 
283 	for (; i < ICE_BST_TCAM_TABLE_SIZE; i++) {
284 		if (_start_with(prefix, lbl_table[i].label)) {
285 			*start = i;
286 			return &tcam_table[lbl_table[i].idx];
287 		}
288 	}
289 
290 	return NULL;
291 }
292