eal: remove sys/queue.h from public headers
[dpdk.git] / drivers / net / bnxt / tf_core / tfp.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * see the individual elements.
3  * Copyright(c) 2019-2021 Broadcom
4  * All rights reserved.
5  */
6
7 #include <rte_memcpy.h>
8 #include <rte_byteorder.h>
9 #include <rte_config.h>
10 #include <rte_mbuf.h>
11 #include <rte_ethdev.h>
12 #include <rte_lcore.h>
13 #include <rte_log.h>
14 #include <rte_errno.h>
15 #include <rte_malloc.h>
16 #include <rte_spinlock.h>
17
18 #include "tf_core.h"
19 #include "tfp.h"
20 #include "bnxt.h"
21 #include "bnxt_hwrm.h"
22 #include "tf_msg_common.h"
23
24 /**
25  * Sends TruFlow msg to the TruFlow Firmware using
26  * a message specific HWRM message type.
27  *
28  * Returns success or failure code.
29  */
30 int
31 tfp_send_msg_direct(struct bnxt *bp,
32                     struct tfp_send_msg_parms *parms)
33 {
34         int      rc = 0;
35         uint8_t  use_kong_mb = 1;
36
37         if (parms == NULL)
38                 return -EINVAL;
39
40         if (parms->mailbox == TF_CHIMP_MB)
41                 use_kong_mb = 0;
42
43         rc = bnxt_hwrm_tf_message_direct(bp,
44                                          use_kong_mb,
45                                          parms->tf_type,
46                                          parms->req_data,
47                                          parms->req_size,
48                                          parms->resp_data,
49                                          parms->resp_size);
50
51         return rc;
52 }
53
54 /**
55  * Allocates zero'ed memory from the heap.
56  *
57  * Returns success or failure code.
58  */
59 int
60 tfp_calloc(struct tfp_calloc_parms *parms)
61 {
62         if (parms == NULL)
63                 return -EINVAL;
64
65         parms->mem_va = rte_zmalloc("tf",
66                                     (parms->nitems * parms->size),
67                                     parms->alignment);
68         if (parms->mem_va == NULL) {
69                 TFP_DRV_LOG(ERR, "Allocate failed mem_va\n");
70                 return -ENOMEM;
71         }
72
73         parms->mem_pa = (void *)((uintptr_t)rte_mem_virt2iova(parms->mem_va));
74         if (parms->mem_pa == (void *)((uintptr_t)RTE_BAD_IOVA)) {
75                 TFP_DRV_LOG(ERR, "Allocate failed mem_pa\n");
76                 return -ENOMEM;
77         }
78
79         return 0;
80 }
81
82 /**
83  * Frees the memory space pointed to by the provided pointer. The
84  * pointer must have been returned from the tfp_calloc().
85  */
86 void
87 tfp_free(void *addr)
88 {
89         rte_free(addr);
90 }
91
92 /**
93  * Copies n bytes from src memory to dest memory. The memory areas
94  * must not overlap.
95  */
96 void
97 tfp_memcpy(void *dest, void *src, size_t n)
98 {
99         rte_memcpy(dest, src, n);
100 }
101
102 /**
103  * Used to initialize portable spin lock
104  */
105 void
106 tfp_spinlock_init(struct tfp_spinlock_parms *parms)
107 {
108         rte_spinlock_init(&parms->slock);
109 }
110
111 /**
112  * Used to lock portable spin lock
113  */
114 void
115 tfp_spinlock_lock(struct tfp_spinlock_parms *parms)
116 {
117         rte_spinlock_lock(&parms->slock);
118 }
119
120 /**
121  * Used to unlock portable spin lock
122  */
123 void
124 tfp_spinlock_unlock(struct tfp_spinlock_parms *parms)
125 {
126         rte_spinlock_unlock(&parms->slock);
127 }
128
129 int
130 tfp_get_fid(struct tf *tfp, uint16_t *fw_fid)
131 {
132         struct bnxt *bp = NULL;
133
134         if (tfp == NULL || fw_fid == NULL)
135                 return -EINVAL;
136
137         bp = (struct bnxt *)tfp->bp;
138         if (bp == NULL)
139                 return -EINVAL;
140
141         *fw_fid = bp->fw_fid;
142
143         return 0;
144 }
145
146 int
147 tfp_get_pf(struct tf *tfp, uint16_t *pf)
148 {
149         struct bnxt *bp = NULL;
150
151         if (tfp == NULL || pf == NULL)
152                 return -EINVAL;
153
154         bp = (struct bnxt *)tfp->bp;
155         if (BNXT_VF(bp) && bp->parent) {
156                 *pf = bp->parent->fid - 1;
157                 return 0;
158         } else if (BNXT_PF(bp)) {
159                 *pf = bp->fw_fid - 1;
160                 return 0;
161         }
162         return -EINVAL;
163 }