1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2001-2021 Intel Corporation
5 #include "ice_common.h"
6 #include "ice_parser_util.h"
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
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
19 void *ice_parser_sect_item_get(u32 sect_type, void *section,
20 u32 index, u32 *offset)
22 struct ice_pkg_sect_hdr *hdr;
23 int data_off = ICE_SEC_DATA_OFFSET;
30 case ICE_SID_RXPARSER_IMEM:
31 size = ICE_SID_RXPARSER_IMEM_ENTRY_SIZE;
33 case ICE_SID_RXPARSER_METADATA_INIT:
34 size = ICE_SID_RXPARSER_METADATA_INIT_ENTRY_SIZE;
40 hdr = (struct ice_pkg_sect_hdr *)section;
41 if (index >= LE16_TO_CPU(hdr->count))
44 return (void *)((uintptr_t)section + data_off + index * size);
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
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,
64 struct ice_seg *seg = hw->seg;
65 struct ice_pkg_enum state;
73 table = ice_malloc(hw, item_size * length);
75 ice_debug(hw, ICE_DBG_PARSER, "failed to allocate memory for table type %d.\n",
80 ice_memset(&state, 0, sizeof(state), ICE_NONDMA_MEM);
82 data = ice_pkg_enum_entry(seg, &state, sect_type, NULL,
86 struct ice_pkg_sect_hdr *hdr =
87 (struct ice_pkg_sect_hdr *)state.sect;
89 idx = hdr->offset + state.entry_idx;
91 (void *)((uintptr_t)table + idx * item_size),
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
104 enum ice_status ice_parser_create(struct ice_hw *hw, struct ice_parser **psr)
106 enum ice_status status;
107 struct ice_parser *p;
109 p = (struct ice_parser *)ice_malloc(hw, sizeof(struct ice_parser));
112 return ICE_ERR_NO_MEMORY;
116 p->imem_table = ice_imem_table_get(hw);
117 if (!p->imem_table) {
118 status = ICE_ERR_PARAM;
122 p->mi_table = ice_metainit_table_get(hw);
124 status = ICE_ERR_PARAM;
131 ice_parser_destroy(p);
136 * ice_parser_destroy - destroy a parser instance
137 * @psr: pointer to a parser instance
139 void ice_parser_destroy(struct ice_parser *psr)
141 ice_free(psr->hw, psr->imem_table);
142 ice_free(psr->hw, psr->mi_table);
144 ice_free(psr->hw, psr);