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