1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2014-2021 Broadcom
6 #include <rte_malloc.h>
9 #include "bnxt_tf_common.h"
10 #include "bnxt_tf_pmd_shim.h"
11 #include "ulp_port_db.h"
15 ulp_port_db_allocate_ifindex(struct bnxt_ulp_port_db *port_db)
19 while (idx < port_db->ulp_intf_list_size &&
20 port_db->ulp_intf_list[idx].type != BNXT_ULP_INTF_TYPE_INVALID)
23 if (idx >= port_db->ulp_intf_list_size) {
24 BNXT_TF_DBG(ERR, "Port DB interface list is full\n");
31 * Initialize the port database. Memory is allocated in this
32 * call and assigned to the port database.
34 * ulp_ctxt [in] Ptr to ulp context
36 * Returns 0 on success or negative number on failure.
38 int32_t ulp_port_db_init(struct bnxt_ulp_context *ulp_ctxt, uint8_t port_cnt)
40 struct bnxt_ulp_port_db *port_db;
42 port_db = rte_zmalloc("bnxt_ulp_port_db",
43 sizeof(struct bnxt_ulp_port_db), 0);
46 "Failed to allocate memory for port db\n");
50 /* Attach the port database to the ulp context. */
51 bnxt_ulp_cntxt_ptr2_port_db_set(ulp_ctxt, port_db);
53 /* index 0 is not being used hence add 1 to size */
54 port_db->ulp_intf_list_size = BNXT_PORT_DB_MAX_INTF_LIST + 1;
55 /* Allocate the port tables */
56 port_db->ulp_intf_list = rte_zmalloc("bnxt_ulp_port_db_intf_list",
57 port_db->ulp_intf_list_size *
58 sizeof(struct ulp_interface_info),
60 if (!port_db->ulp_intf_list) {
62 "Failed to allocate mem for port interface list\n");
66 /* Allocate the phy port list */
67 port_db->phy_port_list = rte_zmalloc("bnxt_ulp_phy_port_list",
69 sizeof(struct ulp_phy_port_info),
71 if (!port_db->phy_port_list) {
73 "Failed to allocate mem for phy port list\n");
76 port_db->phy_port_cnt = port_cnt;
80 ulp_port_db_deinit(ulp_ctxt);
85 * Deinitialize the port database. Memory is deallocated in
88 * ulp_ctxt [in] Ptr to ulp context
90 * Returns 0 on success.
92 int32_t ulp_port_db_deinit(struct bnxt_ulp_context *ulp_ctxt)
94 struct bnxt_ulp_port_db *port_db;
96 port_db = bnxt_ulp_cntxt_ptr2_port_db_get(ulp_ctxt);
98 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
102 /* Detach the flow database from the ulp context. */
103 bnxt_ulp_cntxt_ptr2_port_db_set(ulp_ctxt, NULL);
105 /* Free up all the memory. */
106 rte_free(port_db->phy_port_list);
107 rte_free(port_db->ulp_intf_list);
113 * Update the port database.This api is called when the port
114 * details are available during the startup.
116 * ulp_ctxt [in] Ptr to ulp context
117 * bp [in]. ptr to the device function.
119 * Returns 0 on success or negative number on failure.
121 int32_t ulp_port_db_dev_port_intf_update(struct bnxt_ulp_context *ulp_ctxt,
122 struct rte_eth_dev *eth_dev)
124 uint32_t port_id = eth_dev->data->port_id;
125 struct ulp_phy_port_info *port_data;
126 struct bnxt_ulp_port_db *port_db;
127 struct ulp_interface_info *intf;
128 struct ulp_func_if_info *func;
132 port_db = bnxt_ulp_cntxt_ptr2_port_db_get(ulp_ctxt);
134 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
138 rc = ulp_port_db_dev_port_to_ulp_index(ulp_ctxt, port_id, &ifindex);
140 /* port not found, allocate one */
141 ifindex = ulp_port_db_allocate_ifindex(port_db);
144 port_db->dev_port_list[port_id] = ifindex;
145 } else if (rc == -EINVAL) {
149 /* update the interface details */
150 intf = &port_db->ulp_intf_list[ifindex];
152 intf->type = bnxt_pmd_get_interface_type(port_id);
153 intf->drv_func_id = bnxt_pmd_get_fw_func_id(port_id,
154 BNXT_ULP_INTF_TYPE_INVALID);
156 func = &port_db->ulp_func_id_tbl[intf->drv_func_id];
157 if (!func->func_valid) {
158 func->func_svif = bnxt_pmd_get_svif(port_id, true,
159 BNXT_ULP_INTF_TYPE_INVALID);
160 func->func_spif = bnxt_pmd_get_phy_port_id(port_id);
162 bnxt_pmd_get_parif(port_id, BNXT_ULP_INTF_TYPE_INVALID);
164 bnxt_pmd_get_vnic_id(port_id, BNXT_ULP_INTF_TYPE_INVALID);
165 func->phy_port_id = bnxt_pmd_get_phy_port_id(port_id);
166 func->func_valid = true;
167 func->ifindex = ifindex;
170 if (intf->type == BNXT_ULP_INTF_TYPE_VF_REP) {
172 bnxt_pmd_get_fw_func_id(port_id, BNXT_ULP_INTF_TYPE_VF_REP);
174 func = &port_db->ulp_func_id_tbl[intf->vf_func_id];
176 bnxt_pmd_get_svif(port_id, true, BNXT_ULP_INTF_TYPE_VF_REP);
178 bnxt_pmd_get_phy_port_id(port_id);
180 bnxt_pmd_get_parif(port_id, BNXT_ULP_INTF_TYPE_INVALID);
182 bnxt_pmd_get_vnic_id(port_id, BNXT_ULP_INTF_TYPE_VF_REP);
183 func->phy_port_id = bnxt_pmd_get_phy_port_id(port_id);
184 func->ifindex = ifindex;
187 /* When there is no match, the default action is to send the packet to
188 * the kernel. And to send it to the kernel, we need the PF's vnic id.
190 func->func_parent_vnic = bnxt_pmd_get_parent_vnic_id(port_id, intf->type);
191 func->func_parent_vnic = tfp_cpu_to_be_16(func->func_parent_vnic);
192 bnxt_pmd_get_iface_mac(port_id, intf->type, func->func_mac,
193 func->func_parent_mac);
195 port_data = &port_db->phy_port_list[func->phy_port_id];
196 if (!port_data->port_valid) {
197 port_data->port_svif =
198 bnxt_pmd_get_svif(port_id, false, BNXT_ULP_INTF_TYPE_INVALID);
199 port_data->port_spif = bnxt_pmd_get_phy_port_id(port_id);
200 port_data->port_parif =
201 bnxt_pmd_get_parif(port_id, BNXT_ULP_INTF_TYPE_INVALID);
202 port_data->port_vport = bnxt_pmd_get_vport(port_id);
203 port_data->port_valid = true;
209 * Api to get the ulp ifindex for a given device port.
211 * ulp_ctxt [in] Ptr to ulp context
212 * port_id [in].device port id
213 * ifindex [out] ulp ifindex
215 * Returns 0 on success or negative number on failure.
218 ulp_port_db_dev_port_to_ulp_index(struct bnxt_ulp_context *ulp_ctxt,
222 struct bnxt_ulp_port_db *port_db;
225 port_db = bnxt_ulp_cntxt_ptr2_port_db_get(ulp_ctxt);
226 if (!port_db || port_id >= RTE_MAX_ETHPORTS) {
227 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
230 if (!port_db->dev_port_list[port_id])
233 *ifindex = port_db->dev_port_list[port_id];
238 * Api to get the function id for a given ulp ifindex.
240 * ulp_ctxt [in] Ptr to ulp context
241 * ifindex [in] ulp ifindex
242 * func_id [out] the function id of the given ifindex.
244 * Returns 0 on success or negative number on failure.
247 ulp_port_db_function_id_get(struct bnxt_ulp_context *ulp_ctxt,
252 struct bnxt_ulp_port_db *port_db;
254 port_db = bnxt_ulp_cntxt_ptr2_port_db_get(ulp_ctxt);
255 if (!port_db || ifindex >= port_db->ulp_intf_list_size || !ifindex) {
256 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
260 if (fid_type == BNXT_ULP_DRV_FUNC_FID)
261 *func_id = port_db->ulp_intf_list[ifindex].drv_func_id;
263 *func_id = port_db->ulp_intf_list[ifindex].vf_func_id;
269 * Api to get the svif for a given ulp ifindex.
271 * ulp_ctxt [in] Ptr to ulp context
272 * ifindex [in] ulp ifindex
273 * svif_type [in] the svif type of the given ifindex.
274 * svif [out] the svif of the given ifindex.
276 * Returns 0 on success or negative number on failure.
279 ulp_port_db_svif_get(struct bnxt_ulp_context *ulp_ctxt,
284 struct bnxt_ulp_port_db *port_db;
285 uint16_t phy_port_id, func_id;
287 port_db = bnxt_ulp_cntxt_ptr2_port_db_get(ulp_ctxt);
288 if (!port_db || ifindex >= port_db->ulp_intf_list_size || !ifindex) {
289 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
293 if (svif_type == BNXT_ULP_DRV_FUNC_SVIF) {
294 func_id = port_db->ulp_intf_list[ifindex].drv_func_id;
295 *svif = port_db->ulp_func_id_tbl[func_id].func_svif;
296 } else if (svif_type == BNXT_ULP_VF_FUNC_SVIF) {
297 func_id = port_db->ulp_intf_list[ifindex].vf_func_id;
298 *svif = port_db->ulp_func_id_tbl[func_id].func_svif;
300 func_id = port_db->ulp_intf_list[ifindex].drv_func_id;
301 phy_port_id = port_db->ulp_func_id_tbl[func_id].phy_port_id;
302 *svif = port_db->phy_port_list[phy_port_id].port_svif;
309 * Api to get the spif for a given ulp ifindex.
311 * ulp_ctxt [in] Ptr to ulp context
312 * ifindex [in] ulp ifindex
313 * spif_type [in] the spif type of the given ifindex.
314 * spif [out] the spif of the given ifindex.
316 * Returns 0 on success or negative number on failure.
319 ulp_port_db_spif_get(struct bnxt_ulp_context *ulp_ctxt,
324 struct bnxt_ulp_port_db *port_db;
325 uint16_t phy_port_id, func_id;
327 port_db = bnxt_ulp_cntxt_ptr2_port_db_get(ulp_ctxt);
328 if (!port_db || ifindex >= port_db->ulp_intf_list_size || !ifindex) {
329 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
333 if (spif_type == BNXT_ULP_DRV_FUNC_SPIF) {
334 func_id = port_db->ulp_intf_list[ifindex].drv_func_id;
335 *spif = port_db->ulp_func_id_tbl[func_id].func_spif;
336 } else if (spif_type == BNXT_ULP_VF_FUNC_SPIF) {
337 func_id = port_db->ulp_intf_list[ifindex].vf_func_id;
338 *spif = port_db->ulp_func_id_tbl[func_id].func_spif;
340 func_id = port_db->ulp_intf_list[ifindex].drv_func_id;
341 phy_port_id = port_db->ulp_func_id_tbl[func_id].phy_port_id;
342 *spif = port_db->phy_port_list[phy_port_id].port_spif;
349 * Api to get the parif for a given ulp ifindex.
351 * ulp_ctxt [in] Ptr to ulp context
352 * ifindex [in] ulp ifindex
353 * parif_type [in] the parif type of the given ifindex.
354 * parif [out] the parif of the given ifindex.
356 * Returns 0 on success or negative number on failure.
359 ulp_port_db_parif_get(struct bnxt_ulp_context *ulp_ctxt,
364 struct bnxt_ulp_port_db *port_db;
365 uint16_t phy_port_id, func_id;
367 port_db = bnxt_ulp_cntxt_ptr2_port_db_get(ulp_ctxt);
368 if (!port_db || ifindex >= port_db->ulp_intf_list_size || !ifindex) {
369 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
372 if (parif_type == BNXT_ULP_DRV_FUNC_PARIF) {
373 func_id = port_db->ulp_intf_list[ifindex].drv_func_id;
374 *parif = port_db->ulp_func_id_tbl[func_id].func_parif;
375 } else if (parif_type == BNXT_ULP_VF_FUNC_PARIF) {
376 func_id = port_db->ulp_intf_list[ifindex].vf_func_id;
377 *parif = port_db->ulp_func_id_tbl[func_id].func_parif;
379 func_id = port_db->ulp_intf_list[ifindex].drv_func_id;
380 phy_port_id = port_db->ulp_func_id_tbl[func_id].phy_port_id;
381 *parif = port_db->phy_port_list[phy_port_id].port_parif;
383 /* Parif needs to be reset to a free partition */
384 *parif += BNXT_ULP_FREE_PARIF_BASE;
390 * Api to get the vnic id for a given ulp ifindex.
392 * ulp_ctxt [in] Ptr to ulp context
393 * ifindex [in] ulp ifindex
394 * vnic [out] the vnic of the given ifindex.
396 * Returns 0 on success or negative number on failure.
399 ulp_port_db_default_vnic_get(struct bnxt_ulp_context *ulp_ctxt,
404 struct bnxt_ulp_port_db *port_db;
407 port_db = bnxt_ulp_cntxt_ptr2_port_db_get(ulp_ctxt);
408 if (!port_db || ifindex >= port_db->ulp_intf_list_size || !ifindex) {
409 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
413 if (vnic_type == BNXT_ULP_DRV_FUNC_VNIC) {
414 func_id = port_db->ulp_intf_list[ifindex].drv_func_id;
415 *vnic = port_db->ulp_func_id_tbl[func_id].func_vnic;
417 func_id = port_db->ulp_intf_list[ifindex].vf_func_id;
418 *vnic = port_db->ulp_func_id_tbl[func_id].func_vnic;
425 * Api to get the vport id for a given ulp ifindex.
427 * ulp_ctxt [in] Ptr to ulp context
428 * ifindex [in] ulp ifindex
429 * vport [out] the port of the given ifindex.
431 * Returns 0 on success or negative number on failure.
434 ulp_port_db_vport_get(struct bnxt_ulp_context *ulp_ctxt,
435 uint32_t ifindex, uint16_t *vport)
437 struct bnxt_ulp_port_db *port_db;
438 uint16_t phy_port_id, func_id;
440 port_db = bnxt_ulp_cntxt_ptr2_port_db_get(ulp_ctxt);
441 if (!port_db || ifindex >= port_db->ulp_intf_list_size || !ifindex) {
442 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
446 func_id = port_db->ulp_intf_list[ifindex].drv_func_id;
447 phy_port_id = port_db->ulp_func_id_tbl[func_id].phy_port_id;
448 *vport = port_db->phy_port_list[phy_port_id].port_vport;
453 * Api to get the vport for a given physical port.
455 * ulp_ctxt [in] Ptr to ulp context
456 * phy_port [in] physical port index
457 * out_port [out] the port of the given physical index
459 * Returns 0 on success or negative number on failure.
462 ulp_port_db_phy_port_vport_get(struct bnxt_ulp_context *ulp_ctxt,
466 struct bnxt_ulp_port_db *port_db;
468 port_db = bnxt_ulp_cntxt_ptr2_port_db_get(ulp_ctxt);
469 if (!port_db || phy_port >= port_db->phy_port_cnt) {
470 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
473 *out_port = port_db->phy_port_list[phy_port].port_vport;
478 * Api to get the svif for a given physical port.
480 * ulp_ctxt [in] Ptr to ulp context
481 * phy_port [in] physical port index
482 * svif [out] the svif of the given physical index
484 * Returns 0 on success or negative number on failure.
487 ulp_port_db_phy_port_svif_get(struct bnxt_ulp_context *ulp_ctxt,
491 struct bnxt_ulp_port_db *port_db;
493 port_db = bnxt_ulp_cntxt_ptr2_port_db_get(ulp_ctxt);
494 if (!port_db || phy_port >= port_db->phy_port_cnt) {
495 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
498 *svif = port_db->phy_port_list[phy_port].port_svif;
503 * Api to get the port type for a given ulp ifindex.
505 * ulp_ctxt [in] Ptr to ulp context
506 * ifindex [in] ulp ifindex
510 enum bnxt_ulp_intf_type
511 ulp_port_db_port_type_get(struct bnxt_ulp_context *ulp_ctxt,
514 struct bnxt_ulp_port_db *port_db;
516 port_db = bnxt_ulp_cntxt_ptr2_port_db_get(ulp_ctxt);
517 if (!port_db || ifindex >= port_db->ulp_intf_list_size || !ifindex) {
518 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
519 return BNXT_ULP_INTF_TYPE_INVALID;
521 return port_db->ulp_intf_list[ifindex].type;
525 * Api to get the ulp ifindex for a given function id.
527 * ulp_ctxt [in] Ptr to ulp context
528 * func_id [in].device func id
529 * ifindex [out] ulp ifindex
531 * Returns 0 on success or negative number on failure.
534 ulp_port_db_dev_func_id_to_ulp_index(struct bnxt_ulp_context *ulp_ctxt,
535 uint32_t func_id, uint32_t *ifindex)
537 struct bnxt_ulp_port_db *port_db;
540 port_db = bnxt_ulp_cntxt_ptr2_port_db_get(ulp_ctxt);
541 if (!port_db || func_id >= BNXT_PORT_DB_MAX_FUNC) {
542 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
545 if (!port_db->ulp_func_id_tbl[func_id].func_valid)
548 *ifindex = port_db->ulp_func_id_tbl[func_id].ifindex;
553 * Api to get the function id for a given port id.
555 * ulp_ctxt [in] Ptr to ulp context
556 * port_id [in] dpdk port id
557 * func_id [out] the function id of the given ifindex.
559 * Returns 0 on success or negative number on failure.
562 ulp_port_db_port_func_id_get(struct bnxt_ulp_context *ulp_ctxt,
563 uint16_t port_id, uint16_t *func_id)
565 struct bnxt_ulp_port_db *port_db;
568 port_db = bnxt_ulp_cntxt_ptr2_port_db_get(ulp_ctxt);
569 if (!port_db || port_id >= RTE_MAX_ETHPORTS) {
570 BNXT_TF_DBG(ERR, "Invalid Arguments\n");
573 ifindex = port_db->dev_port_list[port_id];
577 switch (port_db->ulp_intf_list[ifindex].type) {
578 case BNXT_ULP_INTF_TYPE_TRUSTED_VF:
579 case BNXT_ULP_INTF_TYPE_PF:
580 *func_id = port_db->ulp_intf_list[ifindex].drv_func_id;
582 case BNXT_ULP_INTF_TYPE_VF:
583 case BNXT_ULP_INTF_TYPE_VF_REP:
584 *func_id = port_db->ulp_intf_list[ifindex].vf_func_id;
593 /* internal function to get the */
594 static struct ulp_func_if_info*
595 ulp_port_db_func_if_info_get(struct bnxt_ulp_context *ulp_ctxt,
598 struct bnxt_ulp_port_db *port_db;
601 port_db = bnxt_ulp_cntxt_ptr2_port_db_get(ulp_ctxt);
602 if (ulp_port_db_port_func_id_get(ulp_ctxt, port_id, &func_id)) {
603 BNXT_TF_DBG(ERR, "Invalid port_id %x\n", port_id);
607 if (!port_db->ulp_func_id_tbl[func_id].func_valid) {
608 BNXT_TF_DBG(ERR, "Invalid func_id %x\n", func_id);
611 return &port_db->ulp_func_id_tbl[func_id];
615 * Api to get the parent mac address for a given port id.
617 * ulp_ctxt [in] Ptr to ulp context
618 * port_id [in] device port id
619 * mac_addr [out] mac address
621 * Returns 0 on success or negative number on failure.
624 ulp_port_db_parent_mac_addr_get(struct bnxt_ulp_context *ulp_ctxt,
625 uint32_t port_id, uint8_t **mac_addr)
627 struct ulp_func_if_info *info;
629 info = ulp_port_db_func_if_info_get(ulp_ctxt, port_id);
631 *mac_addr = info->func_parent_mac;
638 * Api to get the mac address for a given port id.
640 * ulp_ctxt [in] Ptr to ulp context
641 * port_id [in] device port id
642 * mac_addr [out] mac address
644 * Returns 0 on success or negative number on failure.
647 ulp_port_db_drv_mac_addr_get(struct bnxt_ulp_context *ulp_ctxt,
648 uint32_t port_id, uint8_t **mac_addr)
650 struct ulp_func_if_info *info;
652 info = ulp_port_db_func_if_info_get(ulp_ctxt, port_id);
654 *mac_addr = info->func_mac;
661 * Api to get the parent vnic for a given port id.
663 * ulp_ctxt [in] Ptr to ulp context
664 * port_id [in] device port id
665 * vnic [out] parent vnic
667 * Returns 0 on success or negative number on failure.
670 ulp_port_db_parent_vnic_get(struct bnxt_ulp_context *ulp_ctxt,
671 uint32_t port_id, uint8_t **vnic)
673 struct ulp_func_if_info *info;
675 info = ulp_port_db_func_if_info_get(ulp_ctxt, port_id);
677 *vnic = (uint8_t *)&info->func_parent_vnic;