1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2014-2020 Broadcom
6 #include <rte_malloc.h>
9 #include "bnxt_tf_common.h"
10 #include "ulp_port_db.h"
13 ulp_port_db_allocate_ifindex(struct bnxt_ulp_port_db *port_db)
17 while (idx < port_db->ulp_intf_list_size &&
18 port_db->ulp_intf_list[idx].type != BNXT_ULP_INTF_TYPE_INVALID)
21 if (idx >= port_db->ulp_intf_list_size) {
22 BNXT_TF_DBG(ERR, "Port DB interface list is full\n");
29 * Initialize the port database. Memory is allocated in this
30 * call and assigned to the port database.
32 * ulp_ctxt [in] Ptr to ulp context
34 * Returns 0 on success or negative number on failure.
36 int32_t ulp_port_db_init(struct bnxt_ulp_context *ulp_ctxt)
38 struct bnxt_ulp_port_db *port_db;
40 port_db = rte_zmalloc("bnxt_ulp_port_db",
41 sizeof(struct bnxt_ulp_port_db), 0);
44 "Failed to allocate memory for port db\n");
48 /* Attach the port database to the ulp context. */
49 bnxt_ulp_cntxt_ptr2_port_db_set(ulp_ctxt, port_db);
51 /* index 0 is not being used hence add 1 to size */
52 port_db->ulp_intf_list_size = BNXT_PORT_DB_MAX_INTF_LIST + 1;
53 /* Allocate the port tables */
54 port_db->ulp_intf_list = rte_zmalloc("bnxt_ulp_port_db_intf_list",
55 port_db->ulp_intf_list_size *
56 sizeof(struct ulp_interface_info),
58 if (!port_db->ulp_intf_list) {
60 "Failed to allocate mem for port interface list\n");
66 ulp_port_db_deinit(ulp_ctxt);
71 * Deinitialize the port database. Memory is deallocated in
74 * ulp_ctxt [in] Ptr to ulp context
76 * Returns 0 on success.
78 int32_t ulp_port_db_deinit(struct bnxt_ulp_context *ulp_ctxt)
80 struct bnxt_ulp_port_db *port_db;
82 port_db = bnxt_ulp_cntxt_ptr2_port_db_get(ulp_ctxt);
84 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
88 /* Detach the flow database from the ulp context. */
89 bnxt_ulp_cntxt_ptr2_port_db_set(ulp_ctxt, NULL);
91 /* Free up all the memory. */
92 rte_free(port_db->ulp_intf_list);
98 * Update the port database.This api is called when the port
99 * details are available during the startup.
101 * ulp_ctxt [in] Ptr to ulp context
102 * bp [in]. ptr to the device function.
104 * Returns 0 on success or negative number on failure.
106 int32_t ulp_port_db_dev_port_intf_update(struct bnxt_ulp_context *ulp_ctxt,
107 struct rte_eth_dev *eth_dev)
109 uint32_t port_id = eth_dev->data->port_id;
110 struct ulp_phy_port_info *port_data;
111 struct bnxt_ulp_port_db *port_db;
112 struct ulp_interface_info *intf;
116 port_db = bnxt_ulp_cntxt_ptr2_port_db_get(ulp_ctxt);
118 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
122 rc = ulp_port_db_dev_port_to_ulp_index(ulp_ctxt, port_id, &ifindex);
124 /* port not found, allocate one */
125 ifindex = ulp_port_db_allocate_ifindex(port_db);
128 port_db->dev_port_list[port_id] = ifindex;
129 } else if (rc == -EINVAL) {
133 /* update the interface details */
134 intf = &port_db->ulp_intf_list[ifindex];
136 intf->type = bnxt_get_interface_type(port_id);
138 intf->func_id = bnxt_get_fw_func_id(port_id);
139 intf->func_svif = bnxt_get_svif(port_id, 1);
140 intf->func_spif = bnxt_get_phy_port_id(port_id);
141 intf->func_parif = bnxt_get_parif(port_id);
142 intf->default_vnic = bnxt_get_vnic_id(port_id);
143 intf->phy_port_id = bnxt_get_phy_port_id(port_id);
145 if (intf->type == BNXT_ULP_INTF_TYPE_PF) {
146 port_data = &port_db->phy_port_list[intf->phy_port_id];
147 port_data->port_svif = bnxt_get_svif(port_id, 0);
148 port_data->port_spif = bnxt_get_phy_port_id(port_id);
149 port_data->port_parif = bnxt_get_parif(port_id);
150 port_data->port_vport = bnxt_get_vport(port_id);
157 * Api to get the ulp ifindex for a given device port.
159 * ulp_ctxt [in] Ptr to ulp context
160 * port_id [in].device port id
161 * ifindex [out] ulp ifindex
163 * Returns 0 on success or negative number on failure.
166 ulp_port_db_dev_port_to_ulp_index(struct bnxt_ulp_context *ulp_ctxt,
170 struct bnxt_ulp_port_db *port_db;
173 port_db = bnxt_ulp_cntxt_ptr2_port_db_get(ulp_ctxt);
174 if (!port_db || port_id >= RTE_MAX_ETHPORTS) {
175 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
178 if (!port_db->dev_port_list[port_id])
181 *ifindex = port_db->dev_port_list[port_id];
186 * Api to get the function id for a given ulp ifindex.
188 * ulp_ctxt [in] Ptr to ulp context
189 * ifindex [in] ulp ifindex
190 * func_id [out] the function id of the given ifindex.
192 * Returns 0 on success or negative number on failure.
195 ulp_port_db_function_id_get(struct bnxt_ulp_context *ulp_ctxt,
199 struct bnxt_ulp_port_db *port_db;
201 port_db = bnxt_ulp_cntxt_ptr2_port_db_get(ulp_ctxt);
202 if (!port_db || ifindex >= port_db->ulp_intf_list_size || !ifindex) {
203 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
206 *func_id = port_db->ulp_intf_list[ifindex].func_id;
211 * Api to get the svif for a given ulp ifindex.
213 * ulp_ctxt [in] Ptr to ulp context
214 * ifindex [in] ulp ifindex
215 * dir [in] the direction for the flow.
216 * svif [out] the svif of the given ifindex.
218 * Returns 0 on success or negative number on failure.
221 ulp_port_db_svif_get(struct bnxt_ulp_context *ulp_ctxt,
226 struct bnxt_ulp_port_db *port_db;
227 uint16_t phy_port_id;
229 port_db = bnxt_ulp_cntxt_ptr2_port_db_get(ulp_ctxt);
230 if (!port_db || ifindex >= port_db->ulp_intf_list_size || !ifindex) {
231 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
234 if (dir == ULP_DIR_EGRESS) {
235 *svif = port_db->ulp_intf_list[ifindex].func_svif;
237 phy_port_id = port_db->ulp_intf_list[ifindex].phy_port_id;
238 *svif = port_db->phy_port_list[phy_port_id].port_svif;
245 * Api to get the spif for a given ulp ifindex.
247 * ulp_ctxt [in] Ptr to ulp context
248 * ifindex [in] ulp ifindex
249 * dir [in] the direction for the flow.
250 * spif [out] the spif of the given ifindex.
252 * Returns 0 on success or negative number on failure.
255 ulp_port_db_spif_get(struct bnxt_ulp_context *ulp_ctxt,
260 struct bnxt_ulp_port_db *port_db;
261 uint16_t phy_port_id;
263 port_db = bnxt_ulp_cntxt_ptr2_port_db_get(ulp_ctxt);
264 if (!port_db || ifindex >= port_db->ulp_intf_list_size || !ifindex) {
265 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
268 if (dir == ULP_DIR_EGRESS) {
269 *spif = port_db->ulp_intf_list[ifindex].func_spif;
271 phy_port_id = port_db->ulp_intf_list[ifindex].phy_port_id;
272 *spif = port_db->phy_port_list[phy_port_id].port_spif;
279 * Api to get the parif for a given ulp ifindex.
281 * ulp_ctxt [in] Ptr to ulp context
282 * ifindex [in] ulp ifindex
283 * dir [in] the direction for the flow.
284 * parif [out] the parif of the given ifindex.
286 * Returns 0 on success or negative number on failure.
289 ulp_port_db_parif_get(struct bnxt_ulp_context *ulp_ctxt,
294 struct bnxt_ulp_port_db *port_db;
295 uint16_t phy_port_id;
297 port_db = bnxt_ulp_cntxt_ptr2_port_db_get(ulp_ctxt);
298 if (!port_db || ifindex >= port_db->ulp_intf_list_size || !ifindex) {
299 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
302 if (dir == ULP_DIR_EGRESS) {
303 *parif = port_db->ulp_intf_list[ifindex].func_parif;
305 phy_port_id = port_db->ulp_intf_list[ifindex].phy_port_id;
306 *parif = port_db->phy_port_list[phy_port_id].port_parif;
313 * Api to get the vnic id for a given ulp ifindex.
315 * ulp_ctxt [in] Ptr to ulp context
316 * ifindex [in] ulp ifindex
317 * vnic [out] the vnic of the given ifindex.
319 * Returns 0 on success or negative number on failure.
322 ulp_port_db_default_vnic_get(struct bnxt_ulp_context *ulp_ctxt,
326 struct bnxt_ulp_port_db *port_db;
328 port_db = bnxt_ulp_cntxt_ptr2_port_db_get(ulp_ctxt);
329 if (!port_db || ifindex >= port_db->ulp_intf_list_size || !ifindex) {
330 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
333 *vnic = port_db->ulp_intf_list[ifindex].default_vnic;
338 * Api to get the vport id for a given ulp ifindex.
340 * ulp_ctxt [in] Ptr to ulp context
341 * ifindex [in] ulp ifindex
342 * vport [out] the port of the given ifindex.
344 * Returns 0 on success or negative number on failure.
347 ulp_port_db_vport_get(struct bnxt_ulp_context *ulp_ctxt,
348 uint32_t ifindex, uint16_t *vport)
350 struct bnxt_ulp_port_db *port_db;
351 uint16_t phy_port_id;
353 port_db = bnxt_ulp_cntxt_ptr2_port_db_get(ulp_ctxt);
354 if (!port_db || ifindex >= port_db->ulp_intf_list_size || !ifindex) {
355 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
358 phy_port_id = port_db->ulp_intf_list[ifindex].phy_port_id;
359 *vport = port_db->phy_port_list[phy_port_id].port_vport;