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