d7f5de4c403f84a4cb55cbebc0a55d9d3a286cda
[dpdk.git] / drivers / net / bnxt / tf_core / tf_tbl.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2020 Broadcom
3  * All rights reserved.
4  */
5
6 /* Truflow Table APIs and supporting code */
7
8 #include <stdio.h>
9 #include <string.h>
10 #include <stdbool.h>
11 #include <math.h>
12 #include <sys/param.h>
13 #include <rte_common.h>
14 #include <rte_errno.h>
15 #include "hsi_struct_def_dpdk.h"
16
17 #include "tf_core.h"
18 #include "tf_util.h"
19 #include "tf_em.h"
20 #include "tf_msg.h"
21 #include "tfp.h"
22 #include "hwrm_tf.h"
23 #include "bnxt.h"
24 #include "tf_resources.h"
25 #include "tf_rm.h"
26 #include "stack.h"
27 #include "tf_common.h"
28
29 /**
30  * Internal function to get a Table Entry. Supports all Table Types
31  * except the TF_TBL_TYPE_EXT as that is handled as a table scope.
32  *
33  * [in] tfp
34  *   Pointer to TruFlow handle
35  *
36  * [in] parms
37  *   Pointer to input parameters
38  *
39  * Returns:
40  *   0       - Success
41  *   -EINVAL - Parameter error
42  */
43 static int
44 tf_bulk_get_tbl_entry_internal(struct tf *tfp,
45                           struct tf_bulk_get_tbl_entry_parms *parms)
46 {
47         int rc;
48         int id;
49         uint32_t index;
50         struct bitalloc *session_pool;
51         struct tf_session *tfs = (struct tf_session *)(tfp->session->core_data);
52
53         /* Lookup the pool using the table type of the element */
54         rc = tf_rm_lookup_tbl_type_pool(tfs,
55                                         parms->dir,
56                                         parms->type,
57                                         &session_pool);
58         /* Error logging handled by tf_rm_lookup_tbl_type_pool */
59         if (rc)
60                 return rc;
61
62         index = parms->starting_idx;
63
64         /*
65          * Adjust the returned index/offset as there is no guarantee
66          * that the start is 0 at time of RM allocation
67          */
68         tf_rm_convert_index(tfs,
69                             parms->dir,
70                             parms->type,
71                             TF_RM_CONVERT_RM_BASE,
72                             parms->starting_idx,
73                             &index);
74
75         /* Verify that the entry has been previously allocated */
76         id = ba_inuse(session_pool, index);
77         if (id != 1) {
78                 TFP_DRV_LOG(ERR,
79                    "%s, Invalid or not allocated index, type:%d, starting_idx:%d\n",
80                    tf_dir_2_str(parms->dir),
81                    parms->type,
82                    index);
83                 return -EINVAL;
84         }
85
86         /* Get the entry */
87         rc = tf_msg_bulk_get_tbl_entry(tfp, parms);
88         if (rc) {
89                 TFP_DRV_LOG(ERR,
90                             "%s, Bulk get failed, type:%d, rc:%s\n",
91                             tf_dir_2_str(parms->dir),
92                             parms->type,
93                             strerror(-rc));
94         }
95
96         return rc;
97 }
98
99 #if (TF_SHADOW == 1)
100 /**
101  * Allocate Tbl entry from the Shadow DB. Shadow DB is searched for
102  * the requested entry. If found the ref count is incremente and
103  * returned.
104  *
105  * [in] tfs
106  *   Pointer to session
107  * [in] parms
108  *   Allocation parameters
109  *
110  * Return:
111  *  0       - Success, entry found and ref count incremented
112  *  -ENOENT - Failure, entry not found
113  */
114 static int
115 tf_alloc_tbl_entry_shadow(struct tf_session *tfs __rte_unused,
116                           struct tf_alloc_tbl_entry_parms *parms __rte_unused)
117 {
118         TFP_DRV_LOG(ERR,
119                     "%s, Entry Alloc with search not supported\n",
120                     tf_dir_2_str(parms->dir));
121
122         return -EOPNOTSUPP;
123 }
124
125 /**
126  * Free Tbl entry from the Shadow DB. Shadow DB is searched for
127  * the requested entry. If found the ref count is decremente and
128  * new ref_count returned.
129  *
130  * [in] tfs
131  *   Pointer to session
132  * [in] parms
133  *   Allocation parameters
134  *
135  * Return:
136  *  0       - Success, entry found and ref count decremented
137  *  -ENOENT - Failure, entry not found
138  */
139 static int
140 tf_free_tbl_entry_shadow(struct tf_session *tfs,
141                          struct tf_free_tbl_entry_parms *parms)
142 {
143         TFP_DRV_LOG(ERR,
144                     "%s, Entry Free with search not supported\n",
145                     tf_dir_2_str(parms->dir));
146
147         return -EOPNOTSUPP;
148 }
149 #endif /* TF_SHADOW */
150
151
152  /* API defined in tf_core.h */
153 int
154 tf_bulk_get_tbl_entry(struct tf *tfp,
155                  struct tf_bulk_get_tbl_entry_parms *parms)
156 {
157         int rc = 0;
158
159         TF_CHECK_PARMS_SESSION(tfp, parms);
160
161         if (parms->type == TF_TBL_TYPE_EXT) {
162                 /* Not supported, yet */
163                 TFP_DRV_LOG(ERR,
164                             "%s, External table type not supported\n",
165                             tf_dir_2_str(parms->dir));
166
167                 rc = -EOPNOTSUPP;
168         } else {
169                 /* Internal table type processing */
170                 rc = tf_bulk_get_tbl_entry_internal(tfp, parms);
171                 if (rc)
172                         TFP_DRV_LOG(ERR,
173                                     "%s, Bulk get failed, type:%d, rc:%s\n",
174                                     tf_dir_2_str(parms->dir),
175                                     parms->type,
176                                     strerror(-rc));
177         }
178
179         return rc;
180 }