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