net/bnxt: modify TRUFLOW HWRM messages
[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 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  * Allocates zero'ed memory from the heap.
58  *
59  * Returns success or failure code.
60  */
61 int
62 tfp_calloc(struct tfp_calloc_parms *parms)
63 {
64         if (parms == NULL)
65                 return -EINVAL;
66
67         parms->mem_va = rte_zmalloc("tf",
68                                     (parms->nitems * parms->size),
69                                     parms->alignment);
70         if (parms->mem_va == NULL) {
71                 TFP_DRV_LOG(ERR, "Allocate failed mem_va\n");
72                 return -ENOMEM;
73         }
74
75         parms->mem_pa = (void *)((uintptr_t)rte_mem_virt2iova(parms->mem_va));
76         if (parms->mem_pa == (void *)((uintptr_t)RTE_BAD_IOVA)) {
77                 TFP_DRV_LOG(ERR, "Allocate failed mem_pa\n");
78                 return -ENOMEM;
79         }
80
81         return 0;
82 }
83
84 /**
85  * Frees the memory space pointed to by the provided pointer. The
86  * pointer must have been returned from the tfp_calloc().
87  */
88 void
89 tfp_free(void *addr)
90 {
91         rte_free(addr);
92 }
93
94 /**
95  * Copies n bytes from src memory to dest memory. The memory areas
96  * must not overlap.
97  */
98 void
99 tfp_memcpy(void *dest, void *src, size_t n)
100 {
101         rte_memcpy(dest, src, n);
102 }
103
104 /**
105  * Used to initialize portable spin lock
106  */
107 void
108 tfp_spinlock_init(struct tfp_spinlock_parms *parms)
109 {
110         rte_spinlock_init(&parms->slock);
111 }
112
113 /**
114  * Used to lock portable spin lock
115  */
116 void
117 tfp_spinlock_lock(struct tfp_spinlock_parms *parms)
118 {
119         rte_spinlock_lock(&parms->slock);
120 }
121
122 /**
123  * Used to unlock portable spin lock
124  */
125 void
126 tfp_spinlock_unlock(struct tfp_spinlock_parms *parms)
127 {
128         rte_spinlock_unlock(&parms->slock);
129 }
130
131 int
132 tfp_get_fid(struct tf *tfp, uint16_t *fw_fid)
133 {
134         struct bnxt *bp = NULL;
135
136         if (tfp == NULL || fw_fid == NULL)
137                 return -EINVAL;
138
139         bp = container_of(tfp, struct bnxt, tfp);
140         if (bp == NULL)
141                 return -EINVAL;
142
143         *fw_fid = bp->fw_fid;
144
145         return 0;
146 }
147
148 int
149 tfp_get_pf(struct tf *tfp, uint16_t *pf)
150 {
151         struct bnxt *bp = NULL;
152
153         if (tfp == NULL || pf == NULL)
154                 return -EINVAL;
155
156         bp = container_of(tfp, struct bnxt, tfp);
157         if (BNXT_VF(bp) && bp->parent) {
158                 *pf = bp->parent->fid - 1;
159                 return 0;
160         } else if (BNXT_PF(bp)) {
161                 *pf = bp->fw_fid - 1;
162                 return 0;
163         }
164         return -EINVAL;
165 }