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