xref: /dpdk/drivers/net/bnxt/tf_core/cfa_tcam_mgr.h (revision fd51012de5369679e807be1d6a81d63ef15015ce)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2021-2024 Broadcom
3  * All rights reserved.
4  */
5 
6 #ifndef _CFA_TCAM_MGR_H_
7 #define _CFA_TCAM_MGR_H_
8 
9 #include <errno.h>
10 #include "rte_common.h"
11 #include "hsi_struct_def_dpdk.h"
12 #include "tf_core.h"
13 
14 /**
15  * The TCAM module provides processing of Internal TCAM types.
16  */
17 
18 #define ENTRY_ID_INVALID UINT16_MAX
19 
20 #define TF_TCAM_PRIORITY_MIN 0
21 #define TF_TCAM_PRIORITY_MAX UINT16_MAX
22 
23 #ifndef ARRAY_SIZE
24 #define ARRAY_SIZE(_array) (sizeof(_array) / sizeof(_array[0]))
25 #endif
26 
27 /* Use TFP_DRV_LOG definition in tfp.h */
28 #define CFA_TCAM_MGR_LOG(level, fmt, ...)	\
29 	TFP_DRV_LOG(level, fmt, ## __VA_ARGS__)
30 #define CFA_TCAM_MGR_LOG_DIR(level, dir, fmt, ...)			\
31 	TFP_DRV_LOG(level, "%s: " fmt, tf_dir_2_str(dir), ## __VA_ARGS__)
32 #define CFA_TCAM_MGR_LOG_DIR_TYPE(level, dir, type, fmt, ...)	\
33 	TFP_DRV_LOG(level, "%s: %s " fmt, tf_dir_2_str(dir),		\
34 		    cfa_tcam_mgr_tbl_2_str(type), ## __VA_ARGS__)
35 
36 #define CFA_TCAM_MGR_LOG_0(level, fmt)		\
37 	TFP_DRV_LOG(level, fmt)
38 #define CFA_TCAM_MGR_LOG_DIR_0(level, dir, fmt)			\
39 	TFP_DRV_LOG(level, "%s: " fmt, tf_dir_2_str(dir))
40 #define CFA_TCAM_MGR_LOG_DIR_TYPE_0(level, dir, type, fmt)	\
41 	TFP_DRV_LOG(level, "%s: %s " fmt, tf_dir_2_str(dir),	\
42 		    cfa_tcam_mgr_tbl_2_str(type))
43 
44 #define CFA_TCAM_MGR_ERR_CODE(type) E ## type
45 
46 /**
47  * Checks 1 parameter against NULL.
48  */
49 #define CFA_TCAM_MGR_CHECK_PARMS1(parms) do {				\
50 		if ((parms) == NULL) {					\
51 			CFA_TCAM_MGR_LOG_0(ERR, "Invalid Argument(s)\n"); \
52 			return -CFA_TCAM_MGR_ERR_CODE(INVAL);		\
53 		}							\
54 	} while (0)
55 
56 /**
57  * Checks 2 parameters against NULL.
58  */
59 #define CFA_TCAM_MGR_CHECK_PARMS2(parms1, parms2) do {			\
60 		if ((parms1) == NULL || (parms2) == NULL) {		\
61 			CFA_TCAM_MGR_LOG_0(ERR, "Invalid Argument(s)\n"); \
62 			return -CFA_TCAM_MGR_ERR_CODE(INVAL);		\
63 		}							\
64 	} while (0)
65 
66 /**
67  * Checks 3 parameters against NULL.
68  */
69 #define CFA_TCAM_MGR_CHECK_PARMS3(parms1, parms2, parms3) do {		\
70 		if ((parms1) == NULL ||					\
71 		    (parms2) == NULL ||					\
72 		    (parms3) == NULL) {					\
73 			CFA_TCAM_MGR_LOG_0(ERR, "Invalid Argument(s)\n"); \
74 			return -CFA_TCAM_MGR_ERR_CODE(INVAL);		\
75 		}							\
76 	} while (0)
77 
78 #define CFA_TCAM_MGR_TBL_TYPE_START 0
79 
80 /* Logical TCAM tables */
81 enum cfa_tcam_mgr_tbl_type {
82 	CFA_TCAM_MGR_TBL_TYPE_L2_CTXT_TCAM_HIGH =
83 		CFA_TCAM_MGR_TBL_TYPE_START,
84 	CFA_TCAM_MGR_TBL_TYPE_L2_CTXT_TCAM_LOW,
85 	CFA_TCAM_MGR_TBL_TYPE_PROF_TCAM,
86 	CFA_TCAM_MGR_TBL_TYPE_WC_TCAM,
87 	CFA_TCAM_MGR_TBL_TYPE_SP_TCAM,
88 	CFA_TCAM_MGR_TBL_TYPE_CT_RULE_TCAM,
89 	CFA_TCAM_MGR_TBL_TYPE_VEB_TCAM,
90 	CFA_TCAM_MGR_TBL_TYPE_WC_TCAM_HIGH,
91 	CFA_TCAM_MGR_TBL_TYPE_WC_TCAM_LOW,
92 	CFA_TCAM_MGR_TBL_TYPE_MAX
93 };
94 
95 enum cfa_tcam_mgr_device_type {
96 	CFA_TCAM_MGR_DEVICE_TYPE_P4 = 0,
97 	CFA_TCAM_MGR_DEVICE_TYPE_P5,
98 	CFA_TCAM_MGR_DEVICE_TYPE_MAX
99 };
100 
101 /**
102  * TCAM Manager initialization parameters
103  */
104 struct cfa_tcam_mgr_init_parms {
105 	/**
106 	 * [in] TCAM resources reserved
107 	 *      type element is not used.
108 	 */
109 	struct tf_rm_resc_entry resc[TF_DIR_MAX][CFA_TCAM_MGR_TBL_TYPE_MAX];
110 	/**
111 	 * [out] maximum number of entries available.
112 	 */
113 	uint32_t max_entries;
114 };
115 
116 /**
117  * TCAM Manager configuration parameters
118  */
119 struct cfa_tcam_mgr_cfg_parms {
120 	/**
121 	 * [in] Number of tcam types in each of the configuration arrays
122 	 */
123 	uint16_t num_elements;
124 	/**
125 	 * [in] Session resource allocations
126 	 */
127 	uint16_t tcam_cnt[TF_DIR_MAX][CFA_TCAM_MGR_TBL_TYPE_MAX];
128 
129 	/**
130 	 * [in] TCAM Locations reserved
131 	 */
132 	struct tf_rm_resc_entry (*resv_res)[CFA_TCAM_MGR_TBL_TYPE_MAX];
133 };
134 
135 /**
136  * TCAM Manager allocation parameters
137  */
138 struct cfa_tcam_mgr_alloc_parms {
139 	/**
140 	 * [in] Receive or transmit direction
141 	 */
142 	enum tf_dir dir;
143 	/**
144 	 * [in] Type of the allocation
145 	 */
146 	enum cfa_tcam_mgr_tbl_type type;
147 	/**
148 	 * [in] Type of HCAPI
149 	 */
150 	uint16_t hcapi_type;
151 	/**
152 	 * [in] key size (bytes)
153 	 */
154 	uint16_t key_size;
155 	/**
156 	 * [in] Priority of entry requested (definition TBD)
157 	 */
158 	uint16_t priority;
159 	/**
160 	 * [out] Id of allocated entry or found entry (if search_enable)
161 	 */
162 	uint16_t id;
163 };
164 
165 /**
166  * TCAM Manager free parameters
167  */
168 struct cfa_tcam_mgr_free_parms {
169 	/**
170 	 * [in] Receive or transmit direction
171 	 */
172 	enum tf_dir dir;
173 	/**
174 	 * [in] Type of the allocation
175 	 * If the type is not known, set the type to CFA_TCAM_MGR_TBL_TYPE_MAX.
176 	 */
177 	enum cfa_tcam_mgr_tbl_type type;
178 	/**
179 	 * [in] Type of HCAPI
180 	 */
181 	uint16_t hcapi_type;
182 	/**
183 	 * [in] Entry ID to free
184 	 */
185 	uint16_t id;
186 };
187 
188 /**
189  * TCAM Manager set parameters
190  */
191 struct cfa_tcam_mgr_set_parms {
192 	/**
193 	 * [in] Receive or transmit direction
194 	 */
195 	enum tf_dir dir;
196 	/**
197 	 * [in] Type of object to set
198 	 */
199 	enum cfa_tcam_mgr_tbl_type type;
200 	/**
201 	 * [in] Type of HCAPI
202 	 */
203 	uint16_t hcapi_type;
204 	/**
205 	 * [in] Entry ID to write to
206 	 */
207 	uint16_t id;
208 	/**
209 	 * [in] array containing key
210 	 */
211 	uint8_t *key;
212 	/**
213 	 * [in] array containing mask fields
214 	 */
215 	uint8_t *mask;
216 	/**
217 	 * [in] key size (bytes)
218 	 */
219 	uint16_t key_size;
220 	/**
221 	 * [in] array containing result
222 	 */
223 	uint8_t *result;
224 	/**
225 	 * [in] result size (bytes)
226 	 */
227 	uint16_t result_size;
228 };
229 
230 /**
231  * TCAM Manager get parameters
232  */
233 struct cfa_tcam_mgr_get_parms {
234 	/**
235 	 * [in] Receive or transmit direction
236 	 */
237 	enum tf_dir dir;
238 	/**
239 	 * [in] Type of object to get
240 	 */
241 	enum cfa_tcam_mgr_tbl_type type;
242 	/**
243 	 * [in] Type of HCAPI
244 	 */
245 	uint16_t hcapi_type;
246 	/**
247 	 * [in] Entry ID to read
248 	 */
249 	uint16_t id;
250 	/**
251 	 * [out] array containing key
252 	 */
253 	uint8_t *key;
254 	/**
255 	 * [out] array containing mask fields
256 	 */
257 	uint8_t *mask;
258 	/**
259 	 * [out] key size (bytes)
260 	 */
261 	uint16_t key_size;
262 	/**
263 	 * [out] array containing result
264 	 */
265 	uint8_t *result;
266 	/**
267 	 * [out] result size (bytes)
268 	 */
269 	uint16_t result_size;
270 };
271 
272 /**
273  * cfa_tcam_mgr_shared_clear_parms parameter definition
274  */
275 struct cfa_tcam_mgr_shared_clear_parms {
276 	/**
277 	 * [in] receive or transmit direction
278 	 */
279 	enum tf_dir dir;
280 	/**
281 	 * [in] TCAM table type
282 	 */
283 	enum cfa_tcam_mgr_tbl_type type;
284 };
285 
286 /**
287  * cfa_tcam_mgr_shared_move_parms parameter definition
288  */
289 struct cfa_tcam_mgr_shared_move_parms {
290 	/**
291 	 * [in] receive or transmit direction
292 	 */
293 	enum tf_dir dir;
294 	/**
295 	 * [in] TCAM table type
296 	 */
297 	enum cfa_tcam_mgr_tbl_type type;
298 };
299 
300 /**
301  * @page tcam TCAM Manager
302  *
303  * @ref cfa_tcam_mgr_init
304  *
305  * @ref cfa_tcam_mgr_get_phys_table_type
306  *
307  * @ref cfa_tcam_mgr_bind
308  *
309  * @ref cfa_tcam_mgr_unbind
310  *
311  * @ref cfa_tcam_mgr_alloc
312  *
313  * @ref cfa_tcam_mgr_free
314  *
315  * @ref cfa_tcam_mgr_set
316  *
317  * @ref cfa_tcam_mgr_get
318  *
319  */
320 
321 const char *
322 cfa_tcam_mgr_tbl_2_str(enum cfa_tcam_mgr_tbl_type tcam_type);
323 
324 /**
325  * Initializes the TCAM Manager
326  *
327  * [in] type
328  *   Device type
329  *
330  * Returns
331  *   - (0) if successful.
332  *   - (<0) on failure.
333  */
334 int
335 cfa_tcam_mgr_init(struct tf *tfp, enum cfa_tcam_mgr_device_type type,
336 		  struct cfa_tcam_mgr_init_parms *parms);
337 
338 /**
339  * Returns the physical TCAM table that a logical TCAM table uses.
340  *
341  * [in] type
342  *   Logical table type
343  *
344  * Returns
345  *   - (tf_tcam_tbl_type) if successful.
346  *   - (<0) on failure.
347  */
348 int
349 cfa_tcam_mgr_get_phys_table_type(enum cfa_tcam_mgr_tbl_type type);
350 
351 /**
352  * Initializes the TCAM module with the requested DBs. Must be
353  * invoked as the first thing before any of the access functions.
354  *
355  * [in] context
356  *   Pointer to context information
357  *
358  * [in] parms
359  *   Pointer to parameters
360  *
361  * Returns
362  *   - (0) if successful.
363  *   - (-EINVAL) on failure.
364  */
365 int cfa_tcam_mgr_bind(struct tf *tfp,
366 		      struct cfa_tcam_mgr_cfg_parms *parms);
367 
368 /**
369  * Cleans up the private DBs and releases all the data.
370  *
371  * [in] context
372  *   Pointer to context information
373  *
374  * [in] parms
375  *   Pointer to parameters
376  *
377  * Returns
378  *   - (0) if successful.
379  *   - (-EINVAL) on failure.
380  */
381 int cfa_tcam_mgr_unbind(struct tf *tfp);
382 
383 /**
384  * Allocates the requested tcam type from the internal RM DB.
385  *
386  * [in] context
387  *   Pointer to context information
388  *
389  * [in] parms
390  *   Pointer to parameters
391  *
392  * Returns
393  *   - (0) if successful.
394  *   - (-EINVAL) on failure.
395  */
396 int cfa_tcam_mgr_alloc(struct tf *tfp,
397 		       struct cfa_tcam_mgr_alloc_parms *parms);
398 
399 /**
400  * Free's the requested table type and returns it to the DB.
401  * If refcount goes to 0 then it is returned to the table type DB.
402  *
403  * [in] context
404  *   Pointer to context information
405  *
406  * [in] parms
407  *   Pointer to parameters
408  *
409  * Returns
410  *   - (0) if successful.
411  *   - (-EINVAL) on failure.
412  */
413 int cfa_tcam_mgr_free(struct tf *tfp,
414 		      struct cfa_tcam_mgr_free_parms *parms);
415 
416 /**
417  * Configures the requested element by sending a firmware request which
418  * then installs it into the device internal structures.
419  *
420  * [in] context
421  *   Pointer to context information
422  *
423  * [in] parms
424  *   Pointer to parameters
425  *
426  * Returns
427  *   - (0) if successful.
428  *   - (-EINVAL) on failure.
429  */
430 int cfa_tcam_mgr_set(struct tf *tfp,
431 		     struct cfa_tcam_mgr_set_parms *parms);
432 
433 /**
434  * Retrieves the requested element by sending a firmware request to get
435  * the element.
436  *
437  * [in] context
438  *   Pointer to context information
439  *
440  * [in] parms
441  *   Pointer to parameters
442  *
443  * Returns
444  *   - (0) if successful.
445  *   - (-EINVAL) on failure.
446  */
447 int cfa_tcam_mgr_get(struct tf *tfp,
448 		     struct cfa_tcam_mgr_get_parms *parms);
449 
450 int
451 cfa_tcam_mgr_tables_get(struct tf *tfp, enum tf_dir dir,
452 			enum cfa_tcam_mgr_tbl_type type,
453 			uint16_t *start_row,
454 			uint16_t *end_row,
455 			uint16_t *max_entries,
456 			uint16_t *slices);
457 int
458 cfa_tcam_mgr_tables_set(struct tf *tfp, enum tf_dir dir,
459 			enum cfa_tcam_mgr_tbl_type type,
460 			uint16_t start_row,
461 			uint16_t end_row,
462 			uint16_t max_entries);
463 
464 int cfa_tcam_mgr_shared_clear(struct tf *tfp,
465 		     struct cfa_tcam_mgr_shared_clear_parms *parms);
466 
467 int cfa_tcam_mgr_shared_move(struct tf *tfp,
468 		     struct cfa_tcam_mgr_shared_move_parms *parms);
469 
470 #endif /* _CFA_TCAM_MGR_H */
471