e667d6fa6df0e75d7b72a0b0a4aa6850695417de
[dpdk.git] / drivers / net / bnxt / tf_core / tf_if_tbl.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2021 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 database
19  */
20 struct tf_if_tbl_db {
21         struct tf_if_tbl_cfg *if_tbl_cfg_db[TF_DIR_MAX];
22 };
23
24 /**
25  * Init flag, set on bind and cleared on unbind
26  * TODO: Store this data in session db
27  */
28 static uint8_t init;
29
30 /**
31  * Convert if_tbl_type to hwrm type.
32  *
33  * [in] if_tbl_type
34  *   Interface table type
35  *
36  * [out] hwrm_type
37  *   HWRM device data type
38  *
39  * Returns:
40  *    0          - Success
41  *   -EOPNOTSUPP - Type not supported
42  */
43 static int
44 tf_if_tbl_get_hcapi_type(struct tf_if_tbl_get_hcapi_parms *parms)
45 {
46         struct tf_if_tbl_cfg *tbl_cfg;
47         enum tf_if_tbl_cfg_type cfg_type;
48
49         tbl_cfg = (struct tf_if_tbl_cfg *)parms->tbl_db;
50         cfg_type = tbl_cfg[parms->db_index].cfg_type;
51
52         if (cfg_type != TF_IF_TBL_CFG)
53                 return -ENOTSUP;
54
55         *parms->hcapi_type = tbl_cfg[parms->db_index].hcapi_type;
56
57         return 0;
58 }
59
60 int
61 tf_if_tbl_bind(struct tf *tfp,
62                struct tf_if_tbl_cfg_parms *parms)
63 {
64         struct tfp_calloc_parms cparms;
65         struct tf_if_tbl_db *if_tbl_db;
66
67         TF_CHECK_PARMS2(tfp, parms);
68
69         cparms.nitems = 1;
70         cparms.size = sizeof(struct tf_if_tbl_db);
71         cparms.alignment = 0;
72         if (tfp_calloc(&cparms) != 0) {
73                 TFP_DRV_LOG(ERR, "if_tbl_rm_db alloc error %s\n",
74                             strerror(ENOMEM));
75                 return -ENOMEM;
76         }
77
78         if_tbl_db = cparms.mem_va;
79         if_tbl_db->if_tbl_cfg_db[TF_DIR_RX] = parms->cfg;
80         if_tbl_db->if_tbl_cfg_db[TF_DIR_TX] = parms->cfg;
81         tf_session_set_if_tbl_db(tfp, (void *)if_tbl_db);
82
83         init = 1;
84
85         TFP_DRV_LOG(INFO,
86                     "Table Type - initialized\n");
87
88         return 0;
89 }
90
91 int
92 tf_if_tbl_unbind(struct tf *tfp)
93 {
94         int rc;
95         struct tf_if_tbl_db *if_tbl_db_ptr;
96
97         /* Bail if nothing has been initialized */
98         if (!init) {
99                 TFP_DRV_LOG(INFO,
100                             "No Table DBs created\n");
101                 return 0;
102         }
103
104         TF_CHECK_PARMS1(tfp);
105
106         rc = tf_session_get_if_tbl_db(tfp, (void **)&if_tbl_db_ptr);
107         if (rc) {
108                 TFP_DRV_LOG(INFO, "No IF Table DBs initialized\n");
109                 return 0;
110         }
111
112         tfp_free((void *)if_tbl_db_ptr);
113         init = 0;
114
115         return 0;
116 }
117
118 int
119 tf_if_tbl_set(struct tf *tfp,
120               struct tf_if_tbl_set_parms *parms)
121 {
122         int rc;
123         struct tf_if_tbl_db *if_tbl_db_ptr;
124         struct tf_if_tbl_get_hcapi_parms hparms;
125
126         TF_CHECK_PARMS3(tfp, parms, parms->data);
127
128         if (!init) {
129                 TFP_DRV_LOG(ERR,
130                             "%s: No Table DBs created\n",
131                             tf_dir_2_str(parms->dir));
132                 return -EINVAL;
133         }
134
135         rc = tf_session_get_if_tbl_db(tfp, (void **)&if_tbl_db_ptr);
136         if (rc) {
137                 TFP_DRV_LOG(INFO, "No IF Table DBs initialized\n");
138                 return 0;
139         }
140
141         /* Convert TF type to HCAPI type */
142         hparms.tbl_db = if_tbl_db_ptr->if_tbl_cfg_db[parms->dir];
143         hparms.db_index = parms->type;
144         hparms.hcapi_type = &parms->hcapi_type;
145         rc = tf_if_tbl_get_hcapi_type(&hparms);
146         if (rc)
147                 return rc;
148
149         rc = tf_msg_set_if_tbl_entry(tfp, parms);
150         if (rc) {
151                 TFP_DRV_LOG(ERR,
152                             "%s, If Tbl set failed, type:%d, rc:%s\n",
153                             tf_dir_2_str(parms->dir),
154                             parms->type,
155                             strerror(-rc));
156         }
157
158         return 0;
159 }
160
161 int
162 tf_if_tbl_get(struct tf *tfp,
163               struct tf_if_tbl_get_parms *parms)
164 {
165         int rc = 0;
166         struct tf_if_tbl_db *if_tbl_db_ptr;
167         struct tf_if_tbl_get_hcapi_parms hparms;
168
169         TF_CHECK_PARMS3(tfp, parms, parms->data);
170
171         if (!init) {
172                 TFP_DRV_LOG(ERR,
173                             "%s: No Table DBs created\n",
174                             tf_dir_2_str(parms->dir));
175                 return -EINVAL;
176         }
177
178         rc = tf_session_get_if_tbl_db(tfp, (void **)&if_tbl_db_ptr);
179         if (rc) {
180                 TFP_DRV_LOG(INFO, "No IF Table DBs initialized\n");
181                 return 0;
182         }
183
184         /* Convert TF type to HCAPI type */
185         hparms.tbl_db = if_tbl_db_ptr->if_tbl_cfg_db[parms->dir];
186         hparms.db_index = parms->type;
187         hparms.hcapi_type = &parms->hcapi_type;
188         rc = tf_if_tbl_get_hcapi_type(&hparms);
189         if (rc)
190                 return rc;
191
192         /* Get the entry */
193         rc = tf_msg_get_if_tbl_entry(tfp, parms);
194         if (rc) {
195                 TFP_DRV_LOG(ERR,
196                             "%s, If Tbl 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 }