18430a8b8SMichael Wildt /* SPDX-License-Identifier: BSD-3-Clause
28430a8b8SMichael Wildt * see the individual elements.
3*e6e8f03eSRandy Schacher * Copyright(c) 2019-2023 Broadcom
48430a8b8SMichael Wildt * All rights reserved.
58430a8b8SMichael Wildt */
68430a8b8SMichael Wildt
78430a8b8SMichael Wildt #include <rte_memcpy.h>
88430a8b8SMichael Wildt #include <rte_byteorder.h>
98430a8b8SMichael Wildt #include <rte_config.h>
108430a8b8SMichael Wildt #include <rte_mbuf.h>
118430a8b8SMichael Wildt #include <rte_ethdev.h>
128430a8b8SMichael Wildt #include <rte_lcore.h>
138430a8b8SMichael Wildt #include <rte_log.h>
148430a8b8SMichael Wildt #include <rte_errno.h>
158430a8b8SMichael Wildt #include <rte_malloc.h>
168430a8b8SMichael Wildt #include <rte_spinlock.h>
178430a8b8SMichael Wildt
188430a8b8SMichael Wildt #include "tf_core.h"
198430a8b8SMichael Wildt #include "tfp.h"
208430a8b8SMichael Wildt #include "bnxt.h"
218430a8b8SMichael Wildt #include "bnxt_hwrm.h"
228430a8b8SMichael Wildt #include "tf_msg_common.h"
238430a8b8SMichael Wildt
248430a8b8SMichael Wildt /**
258430a8b8SMichael Wildt * Sends TruFlow msg to the TruFlow Firmware using
268430a8b8SMichael Wildt * a message specific HWRM message type.
278430a8b8SMichael Wildt *
288430a8b8SMichael Wildt * Returns success or failure code.
298430a8b8SMichael Wildt */
308430a8b8SMichael Wildt int
tfp_send_msg_direct(struct bnxt * bp,struct tfp_send_msg_parms * parms)31873661aaSJay Ding tfp_send_msg_direct(struct bnxt *bp,
328430a8b8SMichael Wildt struct tfp_send_msg_parms *parms)
338430a8b8SMichael Wildt {
348430a8b8SMichael Wildt int rc = 0;
358430a8b8SMichael Wildt uint8_t use_kong_mb = 1;
368430a8b8SMichael Wildt
378430a8b8SMichael Wildt if (parms == NULL)
388430a8b8SMichael Wildt return -EINVAL;
398430a8b8SMichael Wildt
408430a8b8SMichael Wildt if (parms->mailbox == TF_CHIMP_MB)
418430a8b8SMichael Wildt use_kong_mb = 0;
428430a8b8SMichael Wildt
43873661aaSJay Ding rc = bnxt_hwrm_tf_message_direct(bp,
448430a8b8SMichael Wildt use_kong_mb,
458430a8b8SMichael Wildt parms->tf_type,
468430a8b8SMichael Wildt parms->req_data,
478430a8b8SMichael Wildt parms->req_size,
488430a8b8SMichael Wildt parms->resp_data,
498430a8b8SMichael Wildt parms->resp_size);
508430a8b8SMichael Wildt
518430a8b8SMichael Wildt return rc;
528430a8b8SMichael Wildt }
538430a8b8SMichael Wildt
548430a8b8SMichael Wildt /**
557be78d02SJosh Soref * Allocates zeroed memory from the heap.
568430a8b8SMichael Wildt *
578430a8b8SMichael Wildt * Returns success or failure code.
588430a8b8SMichael Wildt */
598430a8b8SMichael Wildt int
tfp_calloc(struct tfp_calloc_parms * parms)608430a8b8SMichael Wildt tfp_calloc(struct tfp_calloc_parms *parms)
618430a8b8SMichael Wildt {
628430a8b8SMichael Wildt if (parms == NULL)
638430a8b8SMichael Wildt return -EINVAL;
648430a8b8SMichael Wildt
658430a8b8SMichael Wildt parms->mem_va = rte_zmalloc("tf",
668430a8b8SMichael Wildt (parms->nitems * parms->size),
678430a8b8SMichael Wildt parms->alignment);
688430a8b8SMichael Wildt if (parms->mem_va == NULL) {
6948d3dff2SMichael Wildt TFP_DRV_LOG(ERR, "Allocate failed mem_va\n");
708430a8b8SMichael Wildt return -ENOMEM;
718430a8b8SMichael Wildt }
728430a8b8SMichael Wildt
738430a8b8SMichael Wildt parms->mem_pa = (void *)((uintptr_t)rte_mem_virt2iova(parms->mem_va));
748430a8b8SMichael Wildt if (parms->mem_pa == (void *)((uintptr_t)RTE_BAD_IOVA)) {
7548d3dff2SMichael Wildt TFP_DRV_LOG(ERR, "Allocate failed mem_pa\n");
768430a8b8SMichael Wildt return -ENOMEM;
778430a8b8SMichael Wildt }
788430a8b8SMichael Wildt
798430a8b8SMichael Wildt return 0;
808430a8b8SMichael Wildt }
818430a8b8SMichael Wildt
828430a8b8SMichael Wildt /**
838430a8b8SMichael Wildt * Frees the memory space pointed to by the provided pointer. The
848430a8b8SMichael Wildt * pointer must have been returned from the tfp_calloc().
858430a8b8SMichael Wildt */
868430a8b8SMichael Wildt void
tfp_free(void * addr)878430a8b8SMichael Wildt tfp_free(void *addr)
888430a8b8SMichael Wildt {
898430a8b8SMichael Wildt rte_free(addr);
908430a8b8SMichael Wildt }
918430a8b8SMichael Wildt
928430a8b8SMichael Wildt /**
938430a8b8SMichael Wildt * Copies n bytes from src memory to dest memory. The memory areas
948430a8b8SMichael Wildt * must not overlap.
958430a8b8SMichael Wildt */
968430a8b8SMichael Wildt void
tfp_memcpy(void * dest,void * src,size_t n)978430a8b8SMichael Wildt tfp_memcpy(void *dest, void *src, size_t n)
988430a8b8SMichael Wildt {
998430a8b8SMichael Wildt rte_memcpy(dest, src, n);
1008430a8b8SMichael Wildt }
1018430a8b8SMichael Wildt
1028430a8b8SMichael Wildt /**
1038430a8b8SMichael Wildt * Used to initialize portable spin lock
1048430a8b8SMichael Wildt */
1058430a8b8SMichael Wildt void
tfp_spinlock_init(struct tfp_spinlock_parms * parms)1068430a8b8SMichael Wildt tfp_spinlock_init(struct tfp_spinlock_parms *parms)
1078430a8b8SMichael Wildt {
1088430a8b8SMichael Wildt rte_spinlock_init(&parms->slock);
1098430a8b8SMichael Wildt }
1108430a8b8SMichael Wildt
1118430a8b8SMichael Wildt /**
1128430a8b8SMichael Wildt * Used to lock portable spin lock
1138430a8b8SMichael Wildt */
1148430a8b8SMichael Wildt void
tfp_spinlock_lock(struct tfp_spinlock_parms * parms)1158430a8b8SMichael Wildt tfp_spinlock_lock(struct tfp_spinlock_parms *parms)
1168430a8b8SMichael Wildt {
1178430a8b8SMichael Wildt rte_spinlock_lock(&parms->slock);
1188430a8b8SMichael Wildt }
1198430a8b8SMichael Wildt
1208430a8b8SMichael Wildt /**
1218430a8b8SMichael Wildt * Used to unlock portable spin lock
1228430a8b8SMichael Wildt */
1238430a8b8SMichael Wildt void
tfp_spinlock_unlock(struct tfp_spinlock_parms * parms)1248430a8b8SMichael Wildt tfp_spinlock_unlock(struct tfp_spinlock_parms *parms)
1258430a8b8SMichael Wildt {
1268430a8b8SMichael Wildt rte_spinlock_unlock(&parms->slock);
1278430a8b8SMichael Wildt }
128aa2be509SMichael Wildt
129aa2be509SMichael Wildt int
tfp_get_fid(struct tf * tfp,uint16_t * fw_fid)130aa2be509SMichael Wildt tfp_get_fid(struct tf *tfp, uint16_t *fw_fid)
131aa2be509SMichael Wildt {
132aa2be509SMichael Wildt struct bnxt *bp = NULL;
133aa2be509SMichael Wildt
134aa2be509SMichael Wildt if (tfp == NULL || fw_fid == NULL)
135aa2be509SMichael Wildt return -EINVAL;
136aa2be509SMichael Wildt
137b97763fcSJay Ding bp = (struct bnxt *)tfp->bp;
138aa2be509SMichael Wildt if (bp == NULL)
139aa2be509SMichael Wildt return -EINVAL;
140aa2be509SMichael Wildt
141aa2be509SMichael Wildt *fw_fid = bp->fw_fid;
142aa2be509SMichael Wildt
143aa2be509SMichael Wildt return 0;
144aa2be509SMichael Wildt }
1450ea250f8SFarah Smith
1460ea250f8SFarah Smith int
tfp_get_pf(struct tf * tfp,uint16_t * pf)1470ea250f8SFarah Smith tfp_get_pf(struct tf *tfp, uint16_t *pf)
1480ea250f8SFarah Smith {
1490ea250f8SFarah Smith struct bnxt *bp = NULL;
1500ea250f8SFarah Smith
1510ea250f8SFarah Smith if (tfp == NULL || pf == NULL)
1520ea250f8SFarah Smith return -EINVAL;
1530ea250f8SFarah Smith
154b97763fcSJay Ding bp = (struct bnxt *)tfp->bp;
1550ea250f8SFarah Smith if (BNXT_VF(bp) && bp->parent) {
1560ea250f8SFarah Smith *pf = bp->parent->fid - 1;
1570ea250f8SFarah Smith return 0;
1580ea250f8SFarah Smith } else if (BNXT_PF(bp)) {
1590ea250f8SFarah Smith *pf = bp->fw_fid - 1;
1600ea250f8SFarah Smith return 0;
1610ea250f8SFarah Smith }
1620ea250f8SFarah Smith return -EINVAL;
1630ea250f8SFarah Smith }
164