xref: /dpdk/drivers/net/bnxt/tf_core/tfp.c (revision e11bdd37745229bf26b557305c07d118c3dbaad7)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * see the individual elements.
3  * Copyright(c) 2019-2020 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 tf *tfp,
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(container_of(tfp,
44 					       struct bnxt,
45 					       tfp),
46 					 use_kong_mb,
47 					 parms->tf_type,
48 					 parms->req_data,
49 					 parms->req_size,
50 					 parms->resp_data,
51 					 parms->resp_size);
52 
53 	return rc;
54 }
55 
56 /**
57  * Sends preformatted TruFlow msg to the TruFlow Firmware using
58  * the Truflow tunnel HWRM message type.
59  *
60  * Returns success or failure code.
61  */
62 int
63 tfp_send_msg_tunneled(struct tf *tfp,
64 		      struct tfp_send_msg_parms *parms)
65 {
66 	int      rc = 0;
67 	uint8_t  use_kong_mb = 1;
68 
69 	if (parms == NULL)
70 		return -EINVAL;
71 
72 	if (parms->mailbox == TF_CHIMP_MB)
73 		use_kong_mb = 0;
74 
75 	rc = bnxt_hwrm_tf_message_tunneled(container_of(tfp,
76 						  struct bnxt,
77 						  tfp),
78 					   use_kong_mb,
79 					   parms->tf_type,
80 					   parms->tf_subtype,
81 					   &parms->tf_resp_code,
82 					   parms->req_data,
83 					   parms->req_size,
84 					   parms->resp_data,
85 					   parms->resp_size);
86 
87 	return rc;
88 }
89 
90 /**
91  * Allocates zero'ed memory from the heap.
92  *
93  * Returns success or failure code.
94  */
95 int
96 tfp_calloc(struct tfp_calloc_parms *parms)
97 {
98 	if (parms == NULL)
99 		return -EINVAL;
100 
101 	parms->mem_va = rte_zmalloc("tf",
102 				    (parms->nitems * parms->size),
103 				    parms->alignment);
104 	if (parms->mem_va == NULL) {
105 		PMD_DRV_LOG(ERR, "Allocate failed mem_va\n");
106 		return -ENOMEM;
107 	}
108 
109 	parms->mem_pa = (void *)((uintptr_t)rte_mem_virt2iova(parms->mem_va));
110 	if (parms->mem_pa == (void *)((uintptr_t)RTE_BAD_IOVA)) {
111 		PMD_DRV_LOG(ERR, "Allocate failed mem_pa\n");
112 		return -ENOMEM;
113 	}
114 
115 	return 0;
116 }
117 
118 /**
119  * Frees the memory space pointed to by the provided pointer. The
120  * pointer must have been returned from the tfp_calloc().
121  */
122 void
123 tfp_free(void *addr)
124 {
125 	rte_free(addr);
126 }
127 
128 /**
129  * Copies n bytes from src memory to dest memory. The memory areas
130  * must not overlap.
131  */
132 void
133 tfp_memcpy(void *dest, void *src, size_t n)
134 {
135 	rte_memcpy(dest, src, n);
136 }
137 
138 /**
139  * Used to initialize portable spin lock
140  */
141 void
142 tfp_spinlock_init(struct tfp_spinlock_parms *parms)
143 {
144 	rte_spinlock_init(&parms->slock);
145 }
146 
147 /**
148  * Used to lock portable spin lock
149  */
150 void
151 tfp_spinlock_lock(struct tfp_spinlock_parms *parms)
152 {
153 	rte_spinlock_lock(&parms->slock);
154 }
155 
156 /**
157  * Used to unlock portable spin lock
158  */
159 void
160 tfp_spinlock_unlock(struct tfp_spinlock_parms *parms)
161 {
162 	rte_spinlock_unlock(&parms->slock);
163 }
164