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_if_tbl.h"
9 #include "tf_common.h"
10 #include "tf_rm.h"
11 #include "tf_util.h"
12 #include "tf_msg.h"
13 #include "tfp.h"
14 
15 struct tf;
16 
17 /**
18  * IF Table DBs.
19  */
20 static void *if_tbl_db[TF_DIR_MAX];
21 
22 /**
23  * IF Table Shadow DBs
24  */
25 /* static void *shadow_if_tbl_db[TF_DIR_MAX]; */
26 
27 /**
28  * Init flag, set on bind and cleared on unbind
29  */
30 static uint8_t init;
31 
32 /**
33  * Shadow init flag, set on bind and cleared on unbind
34  */
35 /* static uint8_t shadow_init; */
36 
37 /**
38  * Convert if_tbl_type to hwrm type.
39  *
40  * [in] if_tbl_type
41  *   Interface table type
42  *
43  * [out] hwrm_type
44  *   HWRM device data type
45  *
46  * Returns:
47  *    0          - Success
48  *   -EOPNOTSUPP - Type not supported
49  */
50 static int
tf_if_tbl_get_hcapi_type(struct tf_if_tbl_get_hcapi_parms * parms)51 tf_if_tbl_get_hcapi_type(struct tf_if_tbl_get_hcapi_parms *parms)
52 {
53 	struct tf_if_tbl_cfg *tbl_cfg;
54 	enum tf_if_tbl_cfg_type cfg_type;
55 
56 	tbl_cfg = (struct tf_if_tbl_cfg *)parms->tbl_db;
57 	cfg_type = tbl_cfg[parms->db_index].cfg_type;
58 
59 	if (cfg_type != TF_IF_TBL_CFG)
60 		return -ENOTSUP;
61 
62 	*parms->hcapi_type = tbl_cfg[parms->db_index].hcapi_type;
63 
64 	return 0;
65 }
66 
67 int
tf_if_tbl_bind(struct tf * tfp __rte_unused,struct tf_if_tbl_cfg_parms * parms)68 tf_if_tbl_bind(struct tf *tfp __rte_unused,
69 	       struct tf_if_tbl_cfg_parms *parms)
70 {
71 	TF_CHECK_PARMS2(tfp, parms);
72 
73 	if (init) {
74 		TFP_DRV_LOG(ERR,
75 			    "IF TBL DB already initialized\n");
76 		return -EINVAL;
77 	}
78 
79 	if_tbl_db[TF_DIR_RX] = parms->cfg;
80 	if_tbl_db[TF_DIR_TX] = parms->cfg;
81 
82 	init = 1;
83 
84 	TFP_DRV_LOG(INFO,
85 		    "Table Type - initialized\n");
86 
87 	return 0;
88 }
89 
90 int
tf_if_tbl_unbind(struct tf * tfp __rte_unused)91 tf_if_tbl_unbind(struct tf *tfp __rte_unused)
92 {
93 	/* Bail if nothing has been initialized */
94 	if (!init) {
95 		TFP_DRV_LOG(INFO,
96 			    "No Table DBs created\n");
97 		return 0;
98 	}
99 
100 	if_tbl_db[TF_DIR_RX] = NULL;
101 	if_tbl_db[TF_DIR_TX] = NULL;
102 	init = 0;
103 
104 	return 0;
105 }
106 
107 int
tf_if_tbl_set(struct tf * tfp,struct tf_if_tbl_set_parms * parms)108 tf_if_tbl_set(struct tf *tfp,
109 	      struct tf_if_tbl_set_parms *parms)
110 {
111 	int rc;
112 	struct tf_if_tbl_get_hcapi_parms hparms;
113 
114 	TF_CHECK_PARMS3(tfp, parms, parms->data);
115 
116 	if (!init) {
117 		TFP_DRV_LOG(ERR,
118 			    "%s: No Table DBs created\n",
119 			    tf_dir_2_str(parms->dir));
120 		return -EINVAL;
121 	}
122 
123 	/* Convert TF type to HCAPI type */
124 	hparms.tbl_db = if_tbl_db[parms->dir];
125 	hparms.db_index = parms->type;
126 	hparms.hcapi_type = &parms->hcapi_type;
127 	rc = tf_if_tbl_get_hcapi_type(&hparms);
128 	if (rc)
129 		return rc;
130 
131 	rc = tf_msg_set_if_tbl_entry(tfp, parms);
132 	if (rc) {
133 		TFP_DRV_LOG(ERR,
134 			    "%s, If Tbl set failed, type:%d, rc:%s\n",
135 			    tf_dir_2_str(parms->dir),
136 			    parms->type,
137 			    strerror(-rc));
138 	}
139 
140 	return 0;
141 }
142 
143 int
tf_if_tbl_get(struct tf * tfp,struct tf_if_tbl_get_parms * parms)144 tf_if_tbl_get(struct tf *tfp,
145 	      struct tf_if_tbl_get_parms *parms)
146 {
147 	int rc;
148 	struct tf_if_tbl_get_hcapi_parms hparms;
149 
150 	TF_CHECK_PARMS3(tfp, parms, parms->data);
151 
152 	if (!init) {
153 		TFP_DRV_LOG(ERR,
154 			    "%s: No Table DBs created\n",
155 			    tf_dir_2_str(parms->dir));
156 		return -EINVAL;
157 	}
158 
159 	/* Convert TF type to HCAPI type */
160 	hparms.tbl_db = if_tbl_db[parms->dir];
161 	hparms.db_index = parms->type;
162 	hparms.hcapi_type = &parms->hcapi_type;
163 	rc = tf_if_tbl_get_hcapi_type(&hparms);
164 	if (rc)
165 		return rc;
166 
167 	/* Get the entry */
168 	rc = tf_msg_get_if_tbl_entry(tfp, parms);
169 	if (rc) {
170 		TFP_DRV_LOG(ERR,
171 			    "%s, If Tbl get failed, type:%d, rc:%s\n",
172 			    tf_dir_2_str(parms->dir),
173 			    parms->type,
174 			    strerror(-rc));
175 	}
176 
177 	return 0;
178 }
179