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