xref: /dpdk/drivers/net/bnxt/tf_core/tf_rm.h (revision e6e8f03e5459f25153f1e4cd3e9ac30d3e473a61)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2023 Broadcom
3  * All rights reserved.
4  */
5 
6 #ifndef TF_RM_NEW_H_
7 #define TF_RM_NEW_H_
8 
9 #include "tf_core.h"
10 #include "bitalloc.h"
11 #include "tf_device.h"
12 
13 struct tf;
14 
15 /** RM return codes */
16 #define TF_RM_ALLOCATED_ENTRY_FREE        0
17 #define TF_RM_ALLOCATED_ENTRY_IN_USE      1
18 #define TF_RM_ALLOCATED_NO_ENTRY_FOUND   -1
19 
20 /**
21  * The Resource Manager (RM) module provides basic DB handling for
22  * internal resources. These resources exists within the actual device
23  * and are controlled by the HCAPI Resource Manager running on the
24  * firmware.
25  *
26  * The RM DBs are all intended to be indexed using TF types there for
27  * a lookup requires no additional conversion. The DB configuration
28  * specifies the TF Type to HCAPI Type mapping and it becomes the
29  * responsibility of the DB initialization to handle this static
30  * mapping.
31  *
32  * Accessor functions are providing access to the DB, thus hiding the
33  * implementation.
34  *
35  * The RM DB will work on its initial allocated sizes so the
36  * capability of dynamically growing a particular resource is not
37  * possible. If this capability later becomes a requirement then the
38  * MAX pool size of the chip needs to be added to the tf_rm_elem_info
39  * structure and several new APIs would need to be added to allow for
40  * growth of a single TF resource type.
41  *
42  * The access functions do not check for NULL pointers as they are a
43  * support module, not called directly.
44  */
45 
46 /**
47  * RM Element configuration enumeration. Used by the Device to
48  * indicate how the RM elements the DB consists off, are to be
49  * configured at time of DB creation. The TF may present types to the
50  * ULP layer that is not controlled by HCAPI within the Firmware.
51  */
52 enum tf_rm_elem_cfg_type {
53 	/**
54 	 * No configuration
55 	 */
56 	TF_RM_ELEM_CFG_NULL,
57 	/** HCAPI 'controlled', no RM storage so the module
58 	 *  using the RM can chose to handle storage locally.
59 	 */
60 	TF_RM_ELEM_CFG_HCAPI,
61 	/** HCAPI 'controlled', uses a bit allocator pool for internal
62 	 *  storage in the RM.
63 	 */
64 	TF_RM_ELEM_CFG_HCAPI_BA,
65 	/**
66 	 * HCAPI 'controlled', uses a bit allocator pool for internal
67 	 * storage in the RM but multiple TF types map to a single
68 	 * HCAPI type.  Parent manages the table.
69 	 */
70 	TF_RM_ELEM_CFG_HCAPI_BA_PARENT,
71 	/**
72 	 * HCAPI 'controlled', uses a bit allocator pool for internal
73 	 * storage in the RM but multiple TF types map to a single
74 	 * HCAPI type.  Child accesses the parent db.
75 	 */
76 	TF_RM_ELEM_CFG_HCAPI_BA_CHILD,
77 	TF_RM_TYPE_MAX
78 };
79 
80 /**
81  * RM Reservation strategy enumeration. Type of strategy comes from
82  * the HCAPI RM QCAPS handshake.
83  */
84 enum tf_rm_resc_resv_strategy {
85 	TF_RM_RESC_RESV_STATIC_PARTITION,
86 	TF_RM_RESC_RESV_STRATEGY_1,
87 	TF_RM_RESC_RESV_STRATEGY_2,
88 	TF_RM_RESC_RESV_STRATEGY_3,
89 	TF_RM_RESC_RESV_MAX
90 };
91 
92 /**
93  * RM Element configuration structure, used by the Device to configure
94  * how an individual TF type is configured in regard to the HCAPI RM
95  * of same type.
96  */
97 struct tf_rm_element_cfg {
98 	/**
99 	 * RM Element config controls how the DB for that element is
100 	 * processed.
101 	 */
102 	enum tf_rm_elem_cfg_type cfg_type;
103 
104 	/**
105 	 * HCAPI RM Type for the element. Used for TF to HCAPI type
106 	 * conversion.
107 	 */
108 	uint16_t hcapi_type;
109 
110 	/**
111 	 * if cfg_type == TF_RM_ELEM_CFG_HCAPI_BA_CHILD/PARENT
112 	 *
113 	 * Parent Truflow module subtype associated with this resource type.
114 	 */
115 	uint16_t parent_subtype;
116 
117 	/**
118 	 * if cfg_type == TF_RM_ELEM_CFG_HCAPI_BA_CHILD/PARENT
119 	 *
120 	 * Resource slices.  How many slices will fit in the
121 	 * resource pool chunk size.
122 	 */
123 	uint8_t slices;
124 };
125 
126 /**
127  * Allocation information for a single element.
128  */
129 struct tf_rm_alloc_info {
130 	/**
131 	 * HCAPI RM allocated range information.
132 	 *
133 	 * NOTE:
134 	 * In case of dynamic allocation support this would have
135 	 * to be changed to linked list of tf_rm_entry instead.
136 	 */
137 	struct tf_resource_info entry;
138 };
139 
140 /**
141  * Create RM DB parameters
142  */
143 struct tf_rm_create_db_parms {
144 	/**
145 	 * [in] Module type. Used for logging purposes.
146 	 */
147 	enum tf_module_type module;
148 	/**
149 	 * [in] Receive or transmit direction.
150 	 */
151 	enum tf_dir dir;
152 	/**
153 	 * [in] Number of elements.
154 	 */
155 	uint16_t num_elements;
156 	/**
157 	 * [in] Parameter structure array. Array size is num_elements.
158 	 */
159 	struct tf_rm_element_cfg *cfg;
160 	/**
161 	 * Resource allocation count array. This array content
162 	 * originates from the tf_session_resources that is passed in
163 	 * on session open. Array size is num_elements.
164 	 */
165 	uint16_t *alloc_cnt;
166 	/**
167 	 * [out] RM DB Handle
168 	 */
169 	void **rm_db;
170 };
171 
172 /**
173  * Free RM DB parameters
174  */
175 struct tf_rm_free_db_parms {
176 	/**
177 	 * [in] Receive or transmit direction
178 	 */
179 	enum tf_dir dir;
180 	/**
181 	 * [in] RM DB Handle
182 	 */
183 	void *rm_db;
184 };
185 
186 /**
187  * Allocate RM parameters for a single element
188  */
189 struct tf_rm_allocate_parms {
190 	/**
191 	 * [in] RM DB Handle
192 	 */
193 	void *rm_db;
194 	/**
195 	 * [in] Module subtype indicates which DB entry to perform the
196 	 * action on.  (e.g. TF_TCAM_TBL_TYPE_L2_CTXT subtype of module
197 	 * TF_MODULE_TYPE_TCAM)
198 	 */
199 	uint16_t subtype;
200 	/**
201 	 * [in] Pointer to the allocated index in normalized
202 	 * form. Normalized means the index has been adjusted,
203 	 * i.e. Full Action Record offsets.
204 	 */
205 	uint32_t *index;
206 	/**
207 	 * [in] Priority, indicates the priority of the entry
208 	 * priority  0: allocate from top of the tcam (from index 0
209 	 *              or lowest available index)
210 	 * priority !0: allocate from bottom of the tcam (from highest
211 	 *              available index)
212 	 */
213 	uint32_t priority;
214 	/**
215 	 * [in] Pointer to the allocated index before adjusted.
216 	 */
217 	uint32_t *base_index;
218 };
219 
220 /**
221  * Free RM parameters for a single element
222  */
223 struct tf_rm_free_parms {
224 	/**
225 	 * [in] RM DB Handle
226 	 */
227 	void *rm_db;
228 	/**
229 	 * [in] TF subtype indicates which DB entry to perform the
230 	 * action on. (e.g. TF_TCAM_TBL_TYPE_L2_CTXT subtype of module
231 	 * TF_MODULE_TYPE_TCAM)
232 	 */
233 	uint16_t subtype;
234 	/**
235 	 * [in] Index to free
236 	 */
237 	uint16_t index;
238 };
239 
240 /**
241  * Is Allocated parameters for a single element
242  */
243 struct tf_rm_is_allocated_parms {
244 	/**
245 	 * [in] RM DB Handle
246 	 */
247 	void *rm_db;
248 	/**
249 	 * [in] TF subtype indicates which DB entry to perform the
250 	 * action on. (e.g. TF_TCAM_TBL_TYPE_L2_CTXT subtype of module
251 	 * TF_MODULE_TYPE_TCAM)
252 	 */
253 	uint16_t subtype;
254 	/**
255 	 * [in] Index to free
256 	 */
257 	uint32_t index;
258 	/**
259 	 * [in] Pointer to flag that indicates the state of the query
260 	 */
261 	int *allocated;
262 	/**
263 	 * [in] Pointer to the allocated index before adjusted.
264 	 */
265 	uint32_t *base_index;
266 };
267 
268 /**
269  * Get Allocation information for a single element
270  */
271 struct tf_rm_get_alloc_info_parms {
272 	/**
273 	 * [in] RM DB Handle
274 	 */
275 	void *rm_db;
276 	/**
277 	 * [in] TF subtype indicates which DB entry to perform the
278 	 * action on. (e.g. TF_TCAM_TBL_TYPE_L2_CTXT subtype of module
279 	 * TF_MODULE_TYPE_TCAM)
280 	 */
281 	uint16_t subtype;
282 	/**
283 	 * [out] Pointer to the requested allocation information for
284 	 * the specified subtype
285 	 */
286 	struct tf_rm_alloc_info *info;
287 };
288 
289 /**
290  * Get HCAPI type parameters for a single element
291  */
292 struct tf_rm_get_hcapi_parms {
293 	/**
294 	 * [in] RM DB Handle
295 	 */
296 	void *rm_db;
297 	/**
298 	 * [in] TF subtype indicates which DB entry to perform the
299 	 * action on. (e.g. TF_TCAM_TBL_TYPE_L2_CTXT subtype of module
300 	 * TF_MODULE_TYPE_TCAM)
301 	 */
302 	uint16_t subtype;
303 	/**
304 	 * [out] Pointer to the hcapi type for the specified subtype
305 	 */
306 	uint16_t *hcapi_type;
307 };
308 /**
309  * Get Slices parameters for a single element
310  */
311 struct tf_rm_get_slices_parms {
312 	/**
313 	 * [in] RM DB Handle
314 	 */
315 	void *rm_db;
316 	/**
317 	 * [in] TF subtype indicates which DB entry to perform the
318 	 * action on. (e.g. TF_TBL_TYPE_FULL_ACTION subtype of module
319 	 * TF_MODULE_TYPE_TABLE)
320 	 */
321 	uint16_t subtype;
322 	/**
323 	 * [in/out] Pointer to number of slices for the given type
324 	 */
325 	uint16_t *slices;
326 };
327 
328 /**
329  * Get InUse count parameters for single element
330  */
331 struct tf_rm_get_inuse_count_parms {
332 	/**
333 	 * [in] RM DB Handle
334 	 */
335 	void *rm_db;
336 	/**
337 	 * [in] TF subtype indicates which DB entry to perform the
338 	 * action on. (e.g. TF_TCAM_TBL_TYPE_L2_CTXT subtype of module
339 	 * TF_MODULE_TYPE_TCAM)
340 	 */
341 	uint16_t subtype;
342 	/**
343 	 * [out] Pointer to the inuse count for the specified subtype
344 	 */
345 	uint16_t *count;
346 };
347 
348 /**
349  * Check if the indexes are in the range of reserved resource
350  */
351 struct tf_rm_check_indexes_in_range_parms {
352 	/**
353 	 * [in] RM DB Handle
354 	 */
355 	void *rm_db;
356 	/**
357 	 * [in] TF subtype indicates which DB entry to perform the
358 	 * action on. (e.g. TF_TCAM_TBL_TYPE_L2_CTXT subtype of module
359 	 * TF_MODULE_TYPE_TCAM)
360 	 */
361 	uint16_t subtype;
362 	/**
363 	 * [in] Starting index
364 	 */
365 	uint16_t starting_index;
366 	/**
367 	 * [in] number of entries
368 	 */
369 	uint16_t num_entries;
370 };
371 
372 /**
373  * @page rm Resource Manager
374  *
375  * @ref tf_rm_create_db
376  *
377  * @ref tf_rm_free_db
378  *
379  * @ref tf_rm_allocate
380  *
381  * @ref tf_rm_free
382  *
383  * @ref tf_rm_is_allocated
384  *
385  * @ref tf_rm_get_info
386  *
387  * @ref tf_rm_get_hcapi_type
388  *
389  * @ref tf_rm_get_inuse_count
390  *
391  * @ref tf_rm_get_slice_size
392  */
393 
394 /**
395  * Creates and fills a Resource Manager (RM) DB with requested
396  * elements. The DB is indexed per the parms structure.
397  *
398  * [in] tfp
399  *   Pointer to TF handle, used for HCAPI communication
400  *
401  * [in] parms
402  *   Pointer to create parameters
403  *
404  * Returns
405  *   - (0) if successful.
406  *   - (-EINVAL) on failure.
407  */
408 /*
409  * NOTE:
410  * - Fail on parameter check
411  * - Fail on DB creation, i.e. alloc amount is not possible or validation fails
412  * - Fail on DB creation if DB already exist
413  *
414  * - Allocs local DB
415  * - Does hcapi qcaps
416  * - Does hcapi reservation
417  * - Populates the pool with allocated elements
418  * - Returns handle to the created DB
419  */
420 int tf_rm_create_db(struct tf *tfp,
421 		    struct tf_rm_create_db_parms *parms);
422 
423 /**
424  * Creates and fills a Resource Manager (RM) DB with requested
425  * elements. The DB is indexed per the parms structure. It only retrieve
426  * allocated resource information for a exist session.
427  *
428  * [in] tfp
429  *   Pointer to TF handle, used for HCAPI communication
430  *
431  * [in] parms
432  *   Pointer to create parameters
433  *
434  * Returns
435  *   - (0) if successful.
436  *   - (-EINVAL) on failure.
437  */
438 int tf_rm_create_db_no_reservation(struct tf *tfp,
439 		    struct tf_rm_create_db_parms *parms);
440 
441 /**
442  * Closes the Resource Manager (RM) DB and frees all allocated
443  * resources per the associated database.
444  *
445  * [in] tfp
446  *   Pointer to TF handle, used for HCAPI communication
447  *
448  * [in] parms
449  *   Pointer to free parameters
450  *
451  * Returns
452  *   - (0) if successful.
453  *   - (-EINVAL) on failure.
454  */
455 int tf_rm_free_db(struct tf *tfp,
456 		  struct tf_rm_free_db_parms *parms);
457 
458 /**
459  * Allocates a single element for the type specified, within the DB.
460  *
461  * [in] parms
462  *   Pointer to allocate parameters
463  *
464  * Returns
465  *   - (0) if successful.
466  *   - (-EINVAL) on failure.
467  *   - (-ENOMEM) if pool is empty
468  */
469 int tf_rm_allocate(struct tf_rm_allocate_parms *parms);
470 
471 /**
472  * Free's a single element for the type specified, within the DB.
473  *
474  * [in] parms
475  *   Pointer to free parameters
476  *
477  * Returns
478  *   - (0) if successful.
479  *   - (-EINVAL) on failure.
480  */
481 int tf_rm_free(struct tf_rm_free_parms *parms);
482 
483 /**
484  * Performs an allocation verification check on a specified element.
485  *
486  * [in] parms
487  *   Pointer to is allocated parameters
488  *
489  * Returns
490  *   - (0) if successful.
491  *   - (-EINVAL) on failure.
492  */
493 /*
494  * NOTE:
495  *  - If pool is set to Chip MAX, then the query index must be checked
496  *    against the allocated range and query index must be allocated as well.
497  *  - If pool is allocated size only, then check if query index is allocated.
498  */
499 int tf_rm_is_allocated(struct tf_rm_is_allocated_parms *parms);
500 
501 /**
502  * Retrieves an elements allocation information from the Resource
503  * Manager (RM) DB.
504  *
505  * [in] parms
506  *   Pointer to get info parameters
507  *
508  * Returns
509  *   - (0) if successful.
510  *   - (-EINVAL) on failure.
511  */
512 int tf_rm_get_info(struct tf_rm_get_alloc_info_parms *parms);
513 
514 /**
515  * Retrieves all elements allocation information from the Resource
516  * Manager (RM) DB.
517  *
518  * [in] parms
519  *   Pointer to get info parameters
520  *
521  * [in] size
522  *   number of the elements for the specific module
523  *
524  * Returns
525  *   - (0) if successful.
526  *   - (-EINVAL) on failure.
527  */
528 int tf_rm_get_all_info(struct tf_rm_get_alloc_info_parms *parms, int size);
529 
530 /**
531  * Performs a lookup in the Resource Manager DB and retrieves the
532  * requested HCAPI RM type.
533  *
534  * [in] parms
535  *   Pointer to get hcapi parameters
536  *
537  * Returns
538  *   - (0) if successful.
539  *   - (-EINVAL) on failure.
540  */
541 int tf_rm_get_hcapi_type(struct tf_rm_get_hcapi_parms *parms);
542 
543 /**
544  * Performs a lookup in the Resource Manager DB and retrieves the
545  * requested HCAPI RM type inuse count.
546  *
547  * [in] parms
548  *   Pointer to get inuse parameters
549  *
550  * Returns
551  *   - (0) if successful.
552  *   - (-EINVAL) on failure.
553  */
554 int tf_rm_get_inuse_count(struct tf_rm_get_inuse_count_parms *parms);
555 
556 /**
557  * Check if the requested indexes are in the range of reserved resource.
558  *
559  * [in] parms
560  *   Pointer to get inuse parameters
561  *
562  * Returns
563  *   - (0) if successful.
564  *   - (-EINVAL) on failure.
565  */
566 int
567 tf_rm_check_indexes_in_range(struct tf_rm_check_indexes_in_range_parms *parms);
568 
569 /**
570  * Get the number of slices per resource bit allocator for the resource type
571  *
572  * [in] parms
573  *   Pointer to get inuse parameters
574  *
575  * Returns
576  *   - (0) if successful.
577  *   - (-EINVAL) on failure.
578  */
579 int
580 tf_rm_get_slices(struct tf_rm_get_slices_parms *parms);
581 #endif /* TF_RM_NEW_H_ */
582