1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019-2023 Broadcom
3 * All rights reserved.
4 */
5
6 #include <rte_common.h>
7
8 #include "tf_global_cfg.h"
9 #include "tf_common.h"
10 #include "tf_util.h"
11 #include "tf_msg.h"
12 #include "tfp.h"
13
14 struct tf;
15
16 /**
17 * Global cfg database
18 */
19 struct tf_global_cfg_db {
20 struct tf_global_cfg_cfg *global_cfg_db[TF_DIR_MAX];
21 };
22
23 /**
24 * Get HCAPI type parameters for a single element
25 */
26 struct tf_global_cfg_get_hcapi_parms {
27 /**
28 * [in] Global Cfg DB Handle
29 */
30 void *global_cfg_db;
31 /**
32 * [in] DB Index, indicates which DB entry to perform the
33 * action on.
34 */
35 uint16_t db_index;
36 /**
37 * [out] Pointer to the hcapi type for the specified db_index
38 */
39 uint16_t *hcapi_type;
40 };
41
42 /**
43 * Check global_cfg_type and return hwrm type.
44 *
45 * [in] global_cfg_type
46 * Global Cfg type
47 *
48 * [out] hwrm_type
49 * HWRM device data type
50 *
51 * Returns:
52 * 0 - Success
53 * -EOPNOTSUPP - Type not supported
54 */
55 static int
tf_global_cfg_get_hcapi_type(struct tf_global_cfg_get_hcapi_parms * parms)56 tf_global_cfg_get_hcapi_type(struct tf_global_cfg_get_hcapi_parms *parms)
57 {
58 struct tf_global_cfg_cfg *global_cfg;
59 enum tf_global_cfg_cfg_type cfg_type;
60
61 global_cfg = (struct tf_global_cfg_cfg *)parms->global_cfg_db;
62 cfg_type = global_cfg[parms->db_index].cfg_type;
63
64 if (cfg_type != TF_GLOBAL_CFG_CFG_HCAPI)
65 return -ENOTSUP;
66
67 *parms->hcapi_type = global_cfg[parms->db_index].hcapi_type;
68
69 return 0;
70 }
71
72 int
tf_global_cfg_bind(struct tf * tfp,struct tf_global_cfg_cfg_parms * parms)73 tf_global_cfg_bind(struct tf *tfp,
74 struct tf_global_cfg_cfg_parms *parms)
75 {
76 struct tfp_calloc_parms cparms;
77 struct tf_global_cfg_db *global_cfg_db;
78
79 TF_CHECK_PARMS2(tfp, parms);
80
81 cparms.nitems = 1;
82 cparms.size = sizeof(struct tf_global_cfg_db);
83 cparms.alignment = 0;
84 if (tfp_calloc(&cparms) != 0) {
85 TFP_DRV_LOG(ERR, "global_rm_db alloc error %s\n",
86 strerror(ENOMEM));
87 return -ENOMEM;
88 }
89
90 global_cfg_db = cparms.mem_va;
91 global_cfg_db->global_cfg_db[TF_DIR_RX] = parms->cfg;
92 global_cfg_db->global_cfg_db[TF_DIR_TX] = parms->cfg;
93 tf_session_set_global_db(tfp, (void *)global_cfg_db);
94
95 TFP_DRV_LOG(INFO, "Global Cfg - initialized\n");
96 return 0;
97 }
98
99 int
tf_global_cfg_unbind(struct tf * tfp)100 tf_global_cfg_unbind(struct tf *tfp)
101 {
102 int rc;
103 struct tf_global_cfg_db *global_cfg_db_ptr;
104
105 TF_CHECK_PARMS1(tfp);
106
107 rc = tf_session_get_global_db(tfp, (void **)&global_cfg_db_ptr);
108 if (rc) {
109 TFP_DRV_LOG(INFO, "global_cfg_db is not initialized\n");
110 return 0;
111 }
112
113 tfp_free((void *)global_cfg_db_ptr);
114 return 0;
115 }
116
117 int
tf_global_cfg_set(struct tf * tfp,struct tf_global_cfg_parms * parms)118 tf_global_cfg_set(struct tf *tfp,
119 struct tf_global_cfg_parms *parms)
120 {
121 int rc;
122 struct tf_global_cfg_get_hcapi_parms hparms;
123 struct tf_global_cfg_db *global_cfg_db_ptr;
124 uint16_t hcapi_type;
125
126 TF_CHECK_PARMS3(tfp, parms, parms->config);
127
128 rc = tf_session_get_global_db(tfp, (void **)&global_cfg_db_ptr);
129 if (rc) {
130 TFP_DRV_LOG(INFO, "No global cfg DBs initialized\n");
131 return 0;
132 }
133
134 /* Convert TF type to HCAPI type */
135 hparms.global_cfg_db = global_cfg_db_ptr->global_cfg_db[parms->dir];
136 hparms.db_index = parms->type;
137 hparms.hcapi_type = &hcapi_type;
138 rc = tf_global_cfg_get_hcapi_type(&hparms);
139 if (rc) {
140 TFP_DRV_LOG(ERR,
141 "%s, Failed type lookup, type:%d, rc:%s\n",
142 tf_dir_2_str(parms->dir),
143 parms->type,
144 strerror(-rc));
145 return rc;
146 }
147
148 rc = tf_msg_set_global_cfg(tfp, parms);
149 if (rc) {
150 TFP_DRV_LOG(ERR,
151 "%s, Set failed, type:%d, rc:%s\n",
152 tf_dir_2_str(parms->dir),
153 parms->type,
154 strerror(-rc));
155 }
156
157 return 0;
158 }
159
160 int
tf_global_cfg_get(struct tf * tfp,struct tf_global_cfg_parms * parms)161 tf_global_cfg_get(struct tf *tfp,
162 struct tf_global_cfg_parms *parms)
163
164 {
165 int rc;
166 struct tf_global_cfg_get_hcapi_parms hparms;
167 struct tf_global_cfg_db *global_cfg_db_ptr;
168 uint16_t hcapi_type;
169
170 TF_CHECK_PARMS3(tfp, parms, parms->config);
171
172 rc = tf_session_get_global_db(tfp, (void **)&global_cfg_db_ptr);
173 if (rc) {
174 TFP_DRV_LOG(INFO, "No Global cfg DBs initialized\n");
175 return 0;
176 }
177
178 /* Convert TF type to HCAPI type */
179 hparms.global_cfg_db = global_cfg_db_ptr->global_cfg_db[parms->dir];
180 hparms.db_index = parms->type;
181 hparms.hcapi_type = &hcapi_type;
182 rc = tf_global_cfg_get_hcapi_type(&hparms);
183 if (rc) {
184 TFP_DRV_LOG(ERR,
185 "%s, Failed type lookup, type:%d, rc:%s\n",
186 tf_dir_2_str(parms->dir),
187 parms->type,
188 strerror(-rc));
189 return rc;
190 }
191
192 /* Get the entry */
193 rc = tf_msg_get_global_cfg(tfp, parms);
194 if (rc) {
195 TFP_DRV_LOG(ERR,
196 "%s, Get failed, type:%d, rc:%s\n",
197 tf_dir_2_str(parms->dir),
198 parms->type,
199 strerror(-rc));
200 }
201
202 return 0;
203 }
204