net/ice/base: init marker group table 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
19 #define ICE_SEC_LBL_DATA_OFFSET                         2
20 #define ICE_SID_LBL_ENTRY_SIZE                          66
21
22 void ice_lbl_dump(struct ice_hw *hw, struct ice_lbl_item *item)
23 {
24         ice_info(hw, "index = %d\n", item->idx);
25         ice_info(hw, "label = %s\n", item->label);
26 }
27
28 void ice_parse_item_dflt(struct ice_hw *hw, u16 idx, void *item,
29                          void *data, int size)
30 {
31         ice_memcpy(item, data, size, ICE_DMA_TO_NONDMA);
32 }
33
34 /**
35  * ice_parser_sect_item_get - parse a item from a section
36  * @sect_type: section type
37  * @section: section object
38  * @index: index of the item to get
39  * @offset: dummy as prototype of ice_pkg_enum_entry's last parameter
40  */
41 void *ice_parser_sect_item_get(u32 sect_type, void *section,
42                                u32 index, u32 *offset)
43 {
44         struct ice_pkg_sect_hdr *hdr;
45         int data_off = ICE_SEC_DATA_OFFSET;
46         int size;
47
48         if (!section)
49                 return NULL;
50
51         switch (sect_type) {
52         case ICE_SID_RXPARSER_IMEM:
53                 size = ICE_SID_RXPARSER_IMEM_ENTRY_SIZE;
54                 break;
55         case ICE_SID_RXPARSER_METADATA_INIT:
56                 size = ICE_SID_RXPARSER_METADATA_INIT_ENTRY_SIZE;
57                 break;
58         case ICE_SID_RXPARSER_CAM:
59                 size = ICE_SID_RXPARSER_CAM_ENTRY_SIZE;
60                 break;
61         case ICE_SID_RXPARSER_PG_SPILL:
62                 size = ICE_SID_RXPARSER_PG_SPILL_ENTRY_SIZE;
63                 break;
64         case ICE_SID_RXPARSER_NOMATCH_CAM:
65                 size = ICE_SID_RXPARSER_NOMATCH_CAM_ENTRY_SIZE;
66                 break;
67         case ICE_SID_RXPARSER_NOMATCH_SPILL:
68                 size = ICE_SID_RXPARSER_NOMATCH_SPILL_ENTRY_SIZE;
69                 break;
70         case ICE_SID_RXPARSER_BOOST_TCAM:
71                 size = ICE_SID_RXPARSER_BOOST_TCAM_ENTRY_SIZE;
72                 break;
73         case ICE_SID_LBL_RXPARSER_TMEM:
74                 data_off = ICE_SEC_LBL_DATA_OFFSET;
75                 size = ICE_SID_LBL_ENTRY_SIZE;
76                 break;
77         case ICE_SID_RXPARSER_MARKER_PTYPE:
78                 size = ICE_SID_RXPARSER_MARKER_TYPE_ENTRY_SIZE;
79                 break;
80         case ICE_SID_RXPARSER_MARKER_GRP:
81                 size = ICE_SID_RXPARSER_MARKER_GRP_ENTRY_SIZE;
82                 break;
83         default:
84                 return NULL;
85         }
86
87         hdr = (struct ice_pkg_sect_hdr *)section;
88         if (index >= LE16_TO_CPU(hdr->count))
89                 return NULL;
90
91         return (void *)((uintptr_t)section + data_off + index * size);
92 }
93
94 /**
95  * ice_parser_create_table - create a item table from a section
96  * @hw: pointer to the hardware structure
97  * @sect_type: section type
98  * @item_size: item size in byte
99  * @length: number of items in the table to create
100  * @item_get: the function will be parsed to ice_pkg_enum_entry
101  * @parser_item: the function to parse the item
102  * @no_offset: ignore header offset, calculate index from 0
103  */
104 void *ice_parser_create_table(struct ice_hw *hw, u32 sect_type,
105                               u32 item_size, u32 length,
106                               void *(*item_get)(u32 sect_type, void *section,
107                                                 u32 index, u32 *offset),
108                               void (*parse_item)(struct ice_hw *hw, u16 idx,
109                                                  void *item, void *data,
110                                                  int size),
111                               bool no_offset)
112 {
113         struct ice_seg *seg = hw->seg;
114         struct ice_pkg_enum state;
115         u16 idx = 0xffff;
116         void *table;
117         void *data;
118
119         if (!seg)
120                 return NULL;
121
122         table = ice_malloc(hw, item_size * length);
123         if (!table) {
124                 ice_debug(hw, ICE_DBG_PARSER, "failed to allocate memory for table type %d.\n",
125                           sect_type);
126                 return NULL;
127         }
128
129         ice_memset(&state, 0, sizeof(state), ICE_NONDMA_MEM);
130         do {
131                 data = ice_pkg_enum_entry(seg, &state, sect_type, NULL,
132                                           item_get);
133                 seg = NULL;
134                 if (data) {
135                         struct ice_pkg_sect_hdr *hdr =
136                                 (struct ice_pkg_sect_hdr *)state.sect;
137
138                         if (no_offset)
139                                 idx++;
140                         else
141                                 idx = hdr->offset + state.entry_idx;
142                         parse_item(hw, idx,
143                                    (void *)((uintptr_t)table + idx * item_size),
144                                    data, item_size);
145                 }
146         } while (data);
147
148         return table;
149 }
150
151 /**
152  * ice_parser_create - create a parser instance
153  * @hw: pointer to the hardware structure
154  * @psr: output parameter for a new parser instance be created
155  */
156 enum ice_status ice_parser_create(struct ice_hw *hw, struct ice_parser **psr)
157 {
158         enum ice_status status;
159         struct ice_parser *p;
160
161         p = (struct ice_parser *)ice_malloc(hw, sizeof(struct ice_parser));
162
163         if (!p)
164                 return ICE_ERR_NO_MEMORY;
165
166         p->hw = hw;
167
168         p->imem_table = ice_imem_table_get(hw);
169         if (!p->imem_table) {
170                 status = ICE_ERR_PARAM;
171                 goto err;
172         }
173
174         p->mi_table = ice_metainit_table_get(hw);
175         if (!p->mi_table) {
176                 status = ICE_ERR_PARAM;
177                 goto err;
178         }
179
180         p->pg_cam_table = ice_pg_cam_table_get(hw);
181         if (!p->pg_cam_table) {
182                 status = ICE_ERR_PARAM;
183                 goto err;
184         }
185
186         p->pg_sp_cam_table = ice_pg_sp_cam_table_get(hw);
187         if (!p->pg_sp_cam_table) {
188                 status = ICE_ERR_PARAM;
189                 goto err;
190         }
191
192         p->pg_nm_cam_table = ice_pg_nm_cam_table_get(hw);
193         if (!p->pg_nm_cam_table) {
194                 status = ICE_ERR_PARAM;
195                 goto err;
196         }
197
198         p->pg_nm_sp_cam_table = ice_pg_nm_sp_cam_table_get(hw);
199         if (!p->pg_nm_sp_cam_table) {
200                 status = ICE_ERR_PARAM;
201                 goto err;
202         }
203
204         p->bst_tcam_table = ice_bst_tcam_table_get(hw);
205         if (!p->bst_tcam_table) {
206                 status = ICE_ERR_PARAM;
207                 goto err;
208         }
209
210         p->bst_lbl_table = ice_bst_lbl_table_get(hw);
211         if (!p->bst_lbl_table) {
212                 status = ICE_ERR_PARAM;
213                 goto err;
214         }
215
216         p->ptype_mk_tcam_table = ice_ptype_mk_tcam_table_get(hw);
217         if (!p->ptype_mk_tcam_table) {
218                 status = ICE_ERR_PARAM;
219                 goto err;
220         }
221
222         p->mk_grp_table = ice_mk_grp_table_get(hw);
223         if (!p->mk_grp_table) {
224                 status = ICE_ERR_PARAM;
225                 goto err;
226         }
227
228         *psr = p;
229         return ICE_SUCCESS;
230 err:
231         ice_parser_destroy(p);
232         return status;
233 }
234
235 /**
236  * ice_parser_destroy - destroy a parser instance
237  * @psr: pointer to a parser instance
238  */
239 void ice_parser_destroy(struct ice_parser *psr)
240 {
241         ice_free(psr->hw, psr->imem_table);
242         ice_free(psr->hw, psr->mi_table);
243         ice_free(psr->hw, psr->pg_cam_table);
244         ice_free(psr->hw, psr->pg_sp_cam_table);
245         ice_free(psr->hw, psr->pg_nm_cam_table);
246         ice_free(psr->hw, psr->pg_nm_sp_cam_table);
247         ice_free(psr->hw, psr->bst_tcam_table);
248         ice_free(psr->hw, psr->bst_lbl_table);
249         ice_free(psr->hw, psr->ptype_mk_tcam_table);
250         ice_free(psr->hw, psr->mk_grp_table);
251
252         ice_free(psr->hw, psr);
253 }