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