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