net/bnxt: support multi device
[dpdk.git] / drivers / net / bnxt / tf_core / tf_rm.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2020 Broadcom
3  * All rights reserved.
4  */
5
6 #ifndef TF_RM_H_
7 #define TF_RM_H_
8
9 #include "tf_resources.h"
10 #include "tf_core.h"
11 #include "bitalloc.h"
12
13 struct tf;
14 struct tf_session;
15
16 /* Internal macro to determine appropriate allocation pools based on
17  * DIRECTION parm, also performs error checking for DIRECTION parm. The
18  * SESSION_POOL and SESSION pointers are set appropriately upon
19  * successful return (the GLOBAL_POOL is used to globally manage
20  * resource allocation and the SESSION_POOL is used to track the
21  * resources that have been allocated to the session)
22  *
23  * parameters:
24  *   struct tfp        *tfp
25  *   enum tf_dir        direction
26  *   struct bitalloc  **session_pool
27  *   string             base_pool_name - used to form pointers to the
28  *                                       appropriate bit allocation
29  *                                       pools, both directions of the
30  *                                       session pools must have same
31  *                                       base name, for example if
32  *                                       POOL_NAME is feat_pool: - the
33  *                                       ptr's to the session pools
34  *                                       are feat_pool_rx feat_pool_tx
35  *
36  *  int                  rc - return code
37  *                            0 - Success
38  *                           -1 - invalid DIRECTION parm
39  */
40 #define TF_RM_GET_POOLS(tfs, direction, session_pool, pool_name, rc) do { \
41                 (rc) = 0;                                               \
42                 if ((direction) == TF_DIR_RX) {                         \
43                         *(session_pool) = (tfs)->pool_name ## _RX;      \
44                 } else if ((direction) == TF_DIR_TX) {                  \
45                         *(session_pool) = (tfs)->pool_name ## _TX;      \
46                 } else {                                                \
47                         rc = -1;                                        \
48                 }                                                       \
49         } while (0)
50
51 #define TF_RM_GET_POOLS_RX(tfs, session_pool, pool_name)        \
52         (*(session_pool) = (tfs)->pool_name ## _RX)
53
54 #define TF_RM_GET_POOLS_TX(tfs, session_pool, pool_name)        \
55         (*(session_pool) = (tfs)->pool_name ## _TX)
56
57 /**
58  * Resource query single entry
59  */
60 struct tf_rm_query_entry {
61         /** Minimum guaranteed number of elements */
62         uint16_t min;
63         /** Maximum non-guaranteed number of elements */
64         uint16_t max;
65 };
66
67 /**
68  * Resource single entry
69  */
70 struct tf_rm_entry {
71         /** Starting index of the allocated resource */
72         uint16_t start;
73         /** Number of allocated elements */
74         uint16_t stride;
75 };
76
77 /**
78  * Resource query array of HW entities
79  */
80 struct tf_rm_hw_query {
81         /** array of HW resource entries */
82         struct tf_rm_query_entry hw_query[TF_RESC_TYPE_HW_MAX];
83 };
84
85 /**
86  * Resource allocation array of HW entities
87  */
88 struct tf_rm_hw_alloc {
89         /** array of HW resource entries */
90         uint16_t hw_num[TF_RESC_TYPE_HW_MAX];
91 };
92
93 /**
94  * Resource query array of SRAM entities
95  */
96 struct tf_rm_sram_query {
97         /** array of SRAM resource entries */
98         struct tf_rm_query_entry sram_query[TF_RESC_TYPE_SRAM_MAX];
99 };
100
101 /**
102  * Resource allocation array of SRAM entities
103  */
104 struct tf_rm_sram_alloc {
105         /** array of SRAM resource entries */
106         uint16_t sram_num[TF_RESC_TYPE_SRAM_MAX];
107 };
108
109 /**
110  * Resource Manager arrays for a single direction
111  */
112 struct tf_rm_resc {
113         /** array of HW resource entries */
114         struct tf_rm_entry hw_entry[TF_RESC_TYPE_HW_MAX];
115         /** array of SRAM resource entries */
116         struct tf_rm_entry sram_entry[TF_RESC_TYPE_SRAM_MAX];
117 };
118
119 /**
120  * Resource Manager Database
121  */
122 struct tf_rm_db {
123         struct tf_rm_resc rx;
124         struct tf_rm_resc tx;
125 };
126
127 /**
128  * Helper function used to convert HW HCAPI resource type to a string.
129  */
130 const char
131 *tf_hcapi_hw_2_str(enum tf_resource_type_hw hw_type);
132
133 /**
134  * Helper function used to convert SRAM HCAPI resource type to a string.
135  */
136 const char
137 *tf_hcapi_sram_2_str(enum tf_resource_type_sram sram_type);
138
139 /**
140  * Initializes the Resource Manager and the associated database
141  * entries for HW and SRAM resources. Must be called before any other
142  * Resource Manager functions.
143  *
144  * [in] tfp
145  *   Pointer to TF handle
146  */
147 void tf_rm_init(struct tf *tfp);
148
149 /**
150  * Allocates and validates both HW and SRAM resources per the NVM
151  * configuration. If any allocation fails all resources for the
152  * session is deallocated.
153  *
154  * [in] tfp
155  *   Pointer to TF handle
156  *
157  * Returns
158  *   - (0) if successful.
159  *   - (-EINVAL) on failure.
160  */
161 int tf_rm_allocate_validate(struct tf *tfp);
162
163 /**
164  * Closes the Resource Manager and frees all allocated resources per
165  * the associated database.
166  *
167  * [in] tfp
168  *   Pointer to TF handle
169  *
170  * Returns
171  *   - (0) if successful.
172  *   - (-EINVAL) on failure.
173  *   - (-ENOTEMPTY) if resources are not cleaned up before close
174  */
175 int tf_rm_close(struct tf *tfp);
176
177 #if (TF_SHADOW == 1)
178 /**
179  * Initializes Shadow DB of configuration elements
180  *
181  * [in] tfs
182  *   Pointer to TF Session
183  *
184  * Returns:
185  *  0  - Success
186  */
187 int tf_rm_shadow_db_init(struct tf_session *tfs);
188 #endif /* TF_SHADOW */
189
190 /**
191  * Perform a Session Pool lookup using the Tcam table type.
192  *
193  * Function will print error msg if tcam type is unsupported or lookup
194  * failed.
195  *
196  * [in] tfs
197  *   Pointer to TF Session
198  *
199  * [in] type
200  *   Type of the object
201  *
202  * [in] dir
203  *    Receive or transmit direction
204  *
205  * [in/out]  session_pool
206  *   Session pool
207  *
208  * Returns:
209  *  0           - Success will set the **pool
210  *  -EOPNOTSUPP - Type is not supported
211  */
212 int
213 tf_rm_lookup_tcam_type_pool(struct tf_session *tfs,
214                             enum tf_dir dir,
215                             enum tf_tcam_tbl_type type,
216                             struct bitalloc **pool);
217
218 /**
219  * Perform a Session Pool lookup using the Table type.
220  *
221  * Function will print error msg if table type is unsupported or
222  * lookup failed.
223  *
224  * [in] tfs
225  *   Pointer to TF Session
226  *
227  * [in] type
228  *   Type of the object
229  *
230  * [in] dir
231  *    Receive or transmit direction
232  *
233  * [in/out]  session_pool
234  *   Session pool
235  *
236  * Returns:
237  *  0           - Success will set the **pool
238  *  -EOPNOTSUPP - Type is not supported
239  */
240 int
241 tf_rm_lookup_tbl_type_pool(struct tf_session *tfs,
242                            enum tf_dir dir,
243                            enum tf_tbl_type type,
244                            struct bitalloc **pool);
245
246 /**
247  * Converts the TF Table Type to internal HCAPI_TYPE
248  *
249  * [in] type
250  *   Type to be converted
251  *
252  * [in/out] hcapi_type
253  *   Converted type
254  *
255  * Returns:
256  *  0           - Success will set the *hcapi_type
257  *  -EOPNOTSUPP - Type is not supported
258  */
259 int
260 tf_rm_convert_tbl_type(enum tf_tbl_type type,
261                        uint32_t *hcapi_type);
262
263 /**
264  * TF RM Convert of index methods.
265  */
266 enum tf_rm_convert_type {
267         /** Adds the base of the Session Pool to the index */
268         TF_RM_CONVERT_ADD_BASE,
269         /** Removes the Session Pool base from the index */
270         TF_RM_CONVERT_RM_BASE
271 };
272
273 /**
274  * Provides conversion of the Table Type index in relation to the
275  * Session Pool base.
276  *
277  * [in] tfs
278  *   Pointer to TF Session
279  *
280  * [in] dir
281  *    Receive or transmit direction
282  *
283  * [in] type
284  *   Type of the object
285  *
286  * [in] c_type
287  *   Type of conversion to perform
288  *
289  * [in] index
290  *   Index to be converted
291  *
292  * [in/out]  convert_index
293  *   Pointer to the converted index
294  */
295 int
296 tf_rm_convert_index(struct tf_session *tfs,
297                     enum tf_dir dir,
298                     enum tf_tbl_type type,
299                     enum tf_rm_convert_type c_type,
300                     uint32_t index,
301                     uint32_t *convert_index);
302
303 #endif /* TF_RM_H_ */