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