net/ice/base: support double VLAN mode configure for parser
[dpdk.git] / drivers / net / ice / base / ice_parser.c
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_SEC_DATA_OFFSET                             4
9 #define ICE_SID_RXPARSER_IMEM_ENTRY_SIZE                48
10 #define ICE_SID_RXPARSER_METADATA_INIT_ENTRY_SIZE       24
11 #define ICE_SID_RXPARSER_CAM_ENTRY_SIZE                 16
12 #define ICE_SID_RXPARSER_PG_SPILL_ENTRY_SIZE            17
13 #define ICE_SID_RXPARSER_NOMATCH_CAM_ENTRY_SIZE         12
14 #define ICE_SID_RXPARSER_NOMATCH_SPILL_ENTRY_SIZE       13
15 #define ICE_SID_RXPARSER_BOOST_TCAM_ENTRY_SIZE          88
16 #define ICE_SID_RXPARSER_MARKER_TYPE_ENTRY_SIZE         24
17 #define ICE_SID_RXPARSER_MARKER_GRP_ENTRY_SIZE          8
18 #define ICE_SID_RXPARSER_PROTO_GRP_ENTRY_SIZE           24
19 #define ICE_SID_RXPARSER_FLAG_REDIR_ENTRY_SIZE          1
20
21 #define ICE_SEC_LBL_DATA_OFFSET                         2
22 #define ICE_SID_LBL_ENTRY_SIZE                          66
23
24 void ice_lbl_dump(struct ice_hw *hw, struct ice_lbl_item *item)
25 {
26         ice_info(hw, "index = %d\n", item->idx);
27         ice_info(hw, "label = %s\n", item->label);
28 }
29
30 void ice_parse_item_dflt(struct ice_hw *hw, u16 idx, void *item,
31                          void *data, int size)
32 {
33         ice_memcpy(item, data, size, ICE_DMA_TO_NONDMA);
34 }
35
36 /**
37  * ice_parser_sect_item_get - parse a item from a section
38  * @sect_type: section type
39  * @section: section object
40  * @index: index of the item to get
41  * @offset: dummy as prototype of ice_pkg_enum_entry's last parameter
42  */
43 void *ice_parser_sect_item_get(u32 sect_type, void *section,
44                                u32 index, u32 *offset)
45 {
46         struct ice_pkg_sect_hdr *hdr;
47         int data_off = ICE_SEC_DATA_OFFSET;
48         int size;
49
50         if (!section)
51                 return NULL;
52
53         switch (sect_type) {
54         case ICE_SID_RXPARSER_IMEM:
55                 size = ICE_SID_RXPARSER_IMEM_ENTRY_SIZE;
56                 break;
57         case ICE_SID_RXPARSER_METADATA_INIT:
58                 size = ICE_SID_RXPARSER_METADATA_INIT_ENTRY_SIZE;
59                 break;
60         case ICE_SID_RXPARSER_CAM:
61                 size = ICE_SID_RXPARSER_CAM_ENTRY_SIZE;
62                 break;
63         case ICE_SID_RXPARSER_PG_SPILL:
64                 size = ICE_SID_RXPARSER_PG_SPILL_ENTRY_SIZE;
65                 break;
66         case ICE_SID_RXPARSER_NOMATCH_CAM:
67                 size = ICE_SID_RXPARSER_NOMATCH_CAM_ENTRY_SIZE;
68                 break;
69         case ICE_SID_RXPARSER_NOMATCH_SPILL:
70                 size = ICE_SID_RXPARSER_NOMATCH_SPILL_ENTRY_SIZE;
71                 break;
72         case ICE_SID_RXPARSER_BOOST_TCAM:
73                 size = ICE_SID_RXPARSER_BOOST_TCAM_ENTRY_SIZE;
74                 break;
75         case ICE_SID_LBL_RXPARSER_TMEM:
76                 data_off = ICE_SEC_LBL_DATA_OFFSET;
77                 size = ICE_SID_LBL_ENTRY_SIZE;
78                 break;
79         case ICE_SID_RXPARSER_MARKER_PTYPE:
80                 size = ICE_SID_RXPARSER_MARKER_TYPE_ENTRY_SIZE;
81                 break;
82         case ICE_SID_RXPARSER_MARKER_GRP:
83                 size = ICE_SID_RXPARSER_MARKER_GRP_ENTRY_SIZE;
84                 break;
85         case ICE_SID_RXPARSER_PROTO_GRP:
86                 size = ICE_SID_RXPARSER_PROTO_GRP_ENTRY_SIZE;
87                 break;
88         case ICE_SID_RXPARSER_FLAG_REDIR:
89                 size = ICE_SID_RXPARSER_FLAG_REDIR_ENTRY_SIZE;
90                 break;
91         default:
92                 return NULL;
93         }
94
95         hdr = (struct ice_pkg_sect_hdr *)section;
96         if (index >= LE16_TO_CPU(hdr->count))
97                 return NULL;
98
99         return (void *)((uintptr_t)section + data_off + index * size);
100 }
101
102 /**
103  * ice_parser_create_table - create a item table from a section
104  * @hw: pointer to the hardware structure
105  * @sect_type: section type
106  * @item_size: item size in byte
107  * @length: number of items in the table to create
108  * @item_get: the function will be parsed to ice_pkg_enum_entry
109  * @parser_item: the function to parse the item
110  * @no_offset: ignore header offset, calculate index from 0
111  */
112 void *ice_parser_create_table(struct ice_hw *hw, u32 sect_type,
113                               u32 item_size, u32 length,
114                               void *(*item_get)(u32 sect_type, void *section,
115                                                 u32 index, u32 *offset),
116                               void (*parse_item)(struct ice_hw *hw, u16 idx,
117                                                  void *item, void *data,
118                                                  int size),
119                               bool no_offset)
120 {
121         struct ice_seg *seg = hw->seg;
122         struct ice_pkg_enum state;
123         u16 idx = 0xffff;
124         void *table;
125         void *data;
126
127         if (!seg)
128                 return NULL;
129
130         table = ice_malloc(hw, item_size * length);
131         if (!table) {
132                 ice_debug(hw, ICE_DBG_PARSER, "failed to allocate memory for table type %d.\n",
133                           sect_type);
134                 return NULL;
135         }
136
137         ice_memset(&state, 0, sizeof(state), ICE_NONDMA_MEM);
138         do {
139                 data = ice_pkg_enum_entry(seg, &state, sect_type, NULL,
140                                           item_get);
141                 seg = NULL;
142                 if (data) {
143                         struct ice_pkg_sect_hdr *hdr =
144                                 (struct ice_pkg_sect_hdr *)state.sect;
145
146                         if (no_offset)
147                                 idx++;
148                         else
149                                 idx = hdr->offset + state.entry_idx;
150                         parse_item(hw, idx,
151                                    (void *)((uintptr_t)table + idx * item_size),
152                                    data, item_size);
153                 }
154         } while (data);
155
156         return table;
157 }
158
159 /**
160  * ice_parser_create - create a parser instance
161  * @hw: pointer to the hardware structure
162  * @psr: output parameter for a new parser instance be created
163  */
164 enum ice_status ice_parser_create(struct ice_hw *hw, struct ice_parser **psr)
165 {
166         enum ice_status status;
167         struct ice_parser *p;
168
169         p = (struct ice_parser *)ice_malloc(hw, sizeof(struct ice_parser));
170         p->hw = hw;
171         p->rt.psr = p;
172
173         if (!p)
174                 return ICE_ERR_NO_MEMORY;
175
176         p->hw = hw;
177
178         p->imem_table = ice_imem_table_get(hw);
179         if (!p->imem_table) {
180                 status = ICE_ERR_PARAM;
181                 goto err;
182         }
183
184         p->mi_table = ice_metainit_table_get(hw);
185         if (!p->mi_table) {
186                 status = ICE_ERR_PARAM;
187                 goto err;
188         }
189
190         p->pg_cam_table = ice_pg_cam_table_get(hw);
191         if (!p->pg_cam_table) {
192                 status = ICE_ERR_PARAM;
193                 goto err;
194         }
195
196         p->pg_sp_cam_table = ice_pg_sp_cam_table_get(hw);
197         if (!p->pg_sp_cam_table) {
198                 status = ICE_ERR_PARAM;
199                 goto err;
200         }
201
202         p->pg_nm_cam_table = ice_pg_nm_cam_table_get(hw);
203         if (!p->pg_nm_cam_table) {
204                 status = ICE_ERR_PARAM;
205                 goto err;
206         }
207
208         p->pg_nm_sp_cam_table = ice_pg_nm_sp_cam_table_get(hw);
209         if (!p->pg_nm_sp_cam_table) {
210                 status = ICE_ERR_PARAM;
211                 goto err;
212         }
213
214         p->bst_tcam_table = ice_bst_tcam_table_get(hw);
215         if (!p->bst_tcam_table) {
216                 status = ICE_ERR_PARAM;
217                 goto err;
218         }
219
220         p->bst_lbl_table = ice_bst_lbl_table_get(hw);
221         if (!p->bst_lbl_table) {
222                 status = ICE_ERR_PARAM;
223                 goto err;
224         }
225
226         p->ptype_mk_tcam_table = ice_ptype_mk_tcam_table_get(hw);
227         if (!p->ptype_mk_tcam_table) {
228                 status = ICE_ERR_PARAM;
229                 goto err;
230         }
231
232         p->mk_grp_table = ice_mk_grp_table_get(hw);
233         if (!p->mk_grp_table) {
234                 status = ICE_ERR_PARAM;
235                 goto err;
236         }
237
238         p->proto_grp_table = ice_proto_grp_table_get(hw);
239         if (!p->proto_grp_table) {
240                 status = ICE_ERR_PARAM;
241                 goto err;
242         }
243
244         p->flg_rd_table = ice_flg_rd_table_get(hw);
245         if (!p->flg_rd_table) {
246                 status = ICE_ERR_PARAM;
247                 goto err;
248         }
249
250         p->xlt_kb_sw = ice_xlt_kb_get_sw(hw);
251         if (!p->xlt_kb_sw) {
252                 status = ICE_ERR_PARAM;
253                 goto err;
254         }
255
256         p->xlt_kb_acl = ice_xlt_kb_get_acl(hw);
257         if (!p->xlt_kb_acl) {
258                 status = ICE_ERR_PARAM;
259                 goto err;
260         }
261
262         p->xlt_kb_fd = ice_xlt_kb_get_fd(hw);
263         if (!p->xlt_kb_fd) {
264                 status = ICE_ERR_PARAM;
265                 goto err;
266         }
267
268         p->xlt_kb_rss = ice_xlt_kb_get_rss(hw);
269         if (!p->xlt_kb_rss) {
270                 status = ICE_ERR_PARAM;
271                 goto err;
272         }
273
274         *psr = p;
275         return ICE_SUCCESS;
276 err:
277         ice_parser_destroy(p);
278         return status;
279 }
280
281 /**
282  * ice_parser_destroy - destroy a parser instance
283  * @psr: pointer to a parser instance
284  */
285 void ice_parser_destroy(struct ice_parser *psr)
286 {
287         ice_free(psr->hw, psr->imem_table);
288         ice_free(psr->hw, psr->mi_table);
289         ice_free(psr->hw, psr->pg_cam_table);
290         ice_free(psr->hw, psr->pg_sp_cam_table);
291         ice_free(psr->hw, psr->pg_nm_cam_table);
292         ice_free(psr->hw, psr->pg_nm_sp_cam_table);
293         ice_free(psr->hw, psr->bst_tcam_table);
294         ice_free(psr->hw, psr->bst_lbl_table);
295         ice_free(psr->hw, psr->ptype_mk_tcam_table);
296         ice_free(psr->hw, psr->mk_grp_table);
297         ice_free(psr->hw, psr->proto_grp_table);
298         ice_free(psr->hw, psr->flg_rd_table);
299         ice_free(psr->hw, psr->xlt_kb_sw);
300         ice_free(psr->hw, psr->xlt_kb_acl);
301         ice_free(psr->hw, psr->xlt_kb_fd);
302         ice_free(psr->hw, psr->xlt_kb_rss);
303
304         ice_free(psr->hw, psr);
305 }
306
307 /**
308  * ice_parser_run - parse on a packet in binary and return the result
309  * @psr: pointer to a parser instance
310  * @pkt_buf: packet data
311  * @pkt_len: packet length
312  * @rslt: input/output parameter to save parser result.
313  */
314 enum ice_status ice_parser_run(struct ice_parser *psr, const u8 *pkt_buf,
315                                int pkt_len, struct ice_parser_result *rslt)
316 {
317         ice_parser_rt_reset(&psr->rt);
318         ice_parser_rt_pktbuf_set(&psr->rt, pkt_buf, pkt_len);
319
320         return ice_parser_rt_execute(&psr->rt, rslt);
321 }
322
323 /**
324  * ice_parser_result_dump - dump a parser result info
325  * @hw: pointer to the hardware structure
326  * @rslt: parser result info to dump
327  */
328 void ice_parser_result_dump(struct ice_hw *hw, struct ice_parser_result *rslt)
329 {
330         int i;
331
332         ice_info(hw, "ptype = %d\n", rslt->ptype);
333         for (i = 0; i < rslt->po_num; i++)
334                 ice_info(hw, "proto = %d, offset = %d\n",
335                          rslt->po[i].proto_id, rslt->po[i].offset);
336
337         ice_info(hw, "flags_psr = 0x%016" PRIx64 "\n", rslt->flags_psr);
338         ice_info(hw, "flags_pkt = 0x%016" PRIx64 "\n", rslt->flags_pkt);
339         ice_info(hw, "flags_sw = 0x%04x\n", rslt->flags_sw);
340         ice_info(hw, "flags_fd = 0x%04x\n", rslt->flags_fd);
341         ice_info(hw, "flags_rss = 0x%04x\n", rslt->flags_rss);
342 }
343
344 static void _bst_vm_set(struct ice_parser *psr, const char *prefix, bool on)
345 {
346         struct ice_bst_tcam_item *item;
347         u16 i = 0;
348
349         while (true) {
350                 item = ice_bst_tcam_search(psr->bst_tcam_table,
351                                            psr->bst_lbl_table,
352                                            prefix, &i);
353                 if (!item)
354                         break;
355                 item->key[0] = (u8)(on ? 0xff : 0xfe);
356                 item->key_inv[0] = (u8)(on ? 0xff : 0xfe);
357                 i++;
358         }
359 }
360
361 /**
362  * ice_parser_dvm_set - configure double vlan mode for parser
363  * @psr: pointer to a parser instance
364  */
365 void ice_parser_dvm_set(struct ice_parser *psr, bool on)
366 {
367         _bst_vm_set(psr, "BOOST_MAC_VLAN_DVM", on);
368         _bst_vm_set(psr, "BOOST_MAC_VLAN_SVM", !on);
369 }