f1c98bed333f126f584fe0618f06eb3dbc95cfeb
[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_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 /**
16  * The Resource Manager (RM) module provides basic DB handling for
17  * internal resources. These resources exists within the actual device
18  * and are controlled by the HCAPI Resource Manager running on the
19  * firmware.
20  *
21  * The RM DBs are all intended to be indexed using TF types there for
22  * a lookup requires no additional conversion. The DB configuration
23  * specifies the TF Type to HCAPI Type mapping and it becomes the
24  * responsibility of the DB initialization to handle this static
25  * mapping.
26  *
27  * Accessor functions are providing access to the DB, thus hiding the
28  * implementation.
29  *
30  * The RM DB will work on its initial allocated sizes so the
31  * capability of dynamically growing a particular resource is not
32  * possible. If this capability later becomes a requirement then the
33  * MAX pool size of the Chip Å“needs to be added to the tf_rm_elem_info
34  * structure and several new APIs would need to be added to allow for
35  * growth of a single TF resource type.
36  *
37  * The access functions does not check for NULL pointers as it's a
38  * support module, not called directly.
39  */
40
41 /**
42  * Resource reservation single entry result. Used when accessing HCAPI
43  * RM on the firmware.
44  */
45 struct tf_rm_new_entry {
46         /** Starting index of the allocated resource */
47         uint16_t start;
48         /** Number of allocated elements */
49         uint16_t stride;
50 };
51
52 /**
53  * RM Element configuration enumeration. Used by the Device to
54  * indicate how the RM elements the DB consists off, are to be
55  * configured at time of DB creation. The TF may present types to the
56  * ULP layer that is not controlled by HCAPI within the Firmware.
57  */
58 enum tf_rm_elem_cfg_type {
59         /**
60          * No configuration
61          */
62         TF_RM_ELEM_CFG_NULL,
63         /** HCAPI 'controlled', no RM storage thus the Device Module
64          *  using the RM can chose to handle storage locally.
65          */
66         TF_RM_ELEM_CFG_HCAPI,
67         /** HCAPI 'controlled', uses a Bit Allocator Pool for internal
68          *  storage in the RM.
69          */
70         TF_RM_ELEM_CFG_HCAPI_BA,
71         /**
72          * Shared element thus it belongs to a shared FW Session and
73          * is not controlled by the Host.
74          */
75         TF_RM_ELEM_CFG_SHARED,
76         TF_RM_TYPE_MAX
77 };
78
79 /**
80  * RM Reservation strategy enumeration. Type of strategy comes from
81  * the HCAPI RM QCAPS handshake.
82  */
83 enum tf_rm_resc_resv_strategy {
84         TF_RM_RESC_RESV_STATIC_PARTITION,
85         TF_RM_RESC_RESV_STRATEGY_1,
86         TF_RM_RESC_RESV_STRATEGY_2,
87         TF_RM_RESC_RESV_STRATEGY_3,
88         TF_RM_RESC_RESV_MAX
89 };
90
91 /**
92  * RM Element configuration structure, used by the Device to configure
93  * how an individual TF type is configured in regard to the HCAPI RM
94  * of same type.
95  */
96 struct tf_rm_element_cfg {
97         /**
98          * RM Element config controls how the DB for that element is
99          * processed.
100          */
101         enum tf_rm_elem_cfg_type cfg_type;
102
103         /* If a HCAPI to TF type conversion is required then TF type
104          * can be added here.
105          */
106
107         /**
108          * HCAPI RM Type for the element. Used for TF to HCAPI type
109          * conversion.
110          */
111         uint16_t hcapi_type;
112 };
113
114 /**
115  * Allocation information for a single element.
116  */
117 struct tf_rm_alloc_info {
118         /**
119          * HCAPI RM allocated range information.
120          *
121          * NOTE:
122          * In case of dynamic allocation support this would have
123          * to be changed to linked list of tf_rm_entry instead.
124          */
125         struct tf_rm_new_entry entry;
126 };
127
128 /**
129  * Create RM DB parameters
130  */
131 struct tf_rm_create_db_parms {
132         /**
133          * [in] Device module type. Used for logging purposes.
134          */
135         enum tf_device_module_type type;
136         /**
137          * [in] Receive or transmit direction.
138          */
139         enum tf_dir dir;
140         /**
141          * [in] Number of elements.
142          */
143         uint16_t num_elements;
144         /**
145          * [in] Parameter structure array. Array size is num_elements.
146          */
147         struct tf_rm_element_cfg *cfg;
148         /**
149          * Resource allocation count array. This array content
150          * originates from the tf_session_resources that is passed in
151          * on session open.
152          * Array size is num_elements.
153          */
154         uint16_t *alloc_cnt;
155         /**
156          * [out] RM DB Handle
157          */
158         void **rm_db;
159 };
160
161 /**
162  * Free RM DB parameters
163  */
164 struct tf_rm_free_db_parms {
165         /**
166          * [in] Receive or transmit direction
167          */
168         enum tf_dir dir;
169         /**
170          * [in] RM DB Handle
171          */
172         void *rm_db;
173 };
174
175 /**
176  * Allocate RM parameters for a single element
177  */
178 struct tf_rm_allocate_parms {
179         /**
180          * [in] RM DB Handle
181          */
182         void *rm_db;
183         /**
184          * [in] DB Index, indicates which DB entry to perform the
185          * action on.
186          */
187         uint16_t db_index;
188         /**
189          * [in] Pointer to the allocated index in normalized
190          * form. Normalized means the index has been adjusted,
191          * i.e. Full Action Record offsets.
192          */
193         uint32_t *index;
194         /**
195          * [in] Priority, indicates the priority of the entry
196          * priority  0: allocate from top of the tcam (from index 0
197          *              or lowest available index)
198          * priority !0: allocate from bottom of the tcam (from highest
199          *              available index)
200          */
201         uint32_t priority;
202 };
203
204 /**
205  * Free RM parameters for a single element
206  */
207 struct tf_rm_free_parms {
208         /**
209          * [in] RM DB Handle
210          */
211         void *rm_db;
212         /**
213          * [in] DB Index, indicates which DB entry to perform the
214          * action on.
215          */
216         uint16_t db_index;
217         /**
218          * [in] Index to free
219          */
220         uint16_t index;
221 };
222
223 /**
224  * Is Allocated parameters for a single element
225  */
226 struct tf_rm_is_allocated_parms {
227         /**
228          * [in] RM DB Handle
229          */
230         void *rm_db;
231         /**
232          * [in] DB Index, indicates which DB entry to perform the
233          * action on.
234          */
235         uint16_t db_index;
236         /**
237          * [in] Index to free
238          */
239         uint32_t index;
240         /**
241          * [in] Pointer to flag that indicates the state of the query
242          */
243         int *allocated;
244 };
245
246 /**
247  * Get Allocation information for a single element
248  */
249 struct tf_rm_get_alloc_info_parms {
250         /**
251          * [in] RM DB Handle
252          */
253         void *rm_db;
254         /**
255          * [in] DB Index, indicates which DB entry to perform the
256          * action on.
257          */
258         uint16_t db_index;
259         /**
260          * [out] Pointer to the requested allocation information for
261          * the specified db_index
262          */
263         struct tf_rm_alloc_info *info;
264 };
265
266 /**
267  * Get HCAPI type parameters for a single element
268  */
269 struct tf_rm_get_hcapi_parms {
270         /**
271          * [in] RM DB Handle
272          */
273         void *rm_db;
274         /**
275          * [in] DB Index, indicates which DB entry to perform the
276          * action on.
277          */
278         uint16_t db_index;
279         /**
280          * [out] Pointer to the hcapi type for the specified db_index
281          */
282         uint16_t *hcapi_type;
283 };
284
285 /**
286  * Get InUse count parameters for single element
287  */
288 struct tf_rm_get_inuse_count_parms {
289         /**
290          * [in] RM DB Handle
291          */
292         void *rm_db;
293         /**
294          * [in] DB Index, indicates which DB entry to perform the
295          * action on.
296          */
297         uint16_t db_index;
298         /**
299          * [out] Pointer to the inuse count for the specified db_index
300          */
301         uint16_t *count;
302 };
303
304 /**
305  * @page rm Resource Manager
306  *
307  * @ref tf_rm_create_db
308  *
309  * @ref tf_rm_free_db
310  *
311  * @ref tf_rm_allocate
312  *
313  * @ref tf_rm_free
314  *
315  * @ref tf_rm_is_allocated
316  *
317  * @ref tf_rm_get_info
318  *
319  * @ref tf_rm_get_hcapi_type
320  *
321  * @ref tf_rm_get_inuse_count
322  */
323
324 /**
325  * Creates and fills a Resource Manager (RM) DB with requested
326  * elements. The DB is indexed per the parms structure.
327  *
328  * [in] tfp
329  *   Pointer to TF handle, used for HCAPI communication
330  *
331  * [in] parms
332  *   Pointer to create parameters
333  *
334  * Returns
335  *   - (0) if successful.
336  *   - (-EINVAL) on failure.
337  */
338 /*
339  * NOTE:
340  * - Fail on parameter check
341  * - Fail on DB creation, i.e. alloc amount is not possible or validation fails
342  * - Fail on DB creation if DB already exist
343  *
344  * - Allocs local DB
345  * - Does hcapi qcaps
346  * - Does hcapi reservation
347  * - Populates the pool with allocated elements
348  * - Returns handle to the created DB
349  */
350 int tf_rm_create_db(struct tf *tfp,
351                     struct tf_rm_create_db_parms *parms);
352
353 /**
354  * Closes the Resource Manager (RM) DB and frees all allocated
355  * resources per the associated database.
356  *
357  * [in] tfp
358  *   Pointer to TF handle, used for HCAPI communication
359  *
360  * [in] parms
361  *   Pointer to free parameters
362  *
363  * Returns
364  *   - (0) if successful.
365  *   - (-EINVAL) on failure.
366  */
367 int tf_rm_free_db(struct tf *tfp,
368                   struct tf_rm_free_db_parms *parms);
369
370 /**
371  * Allocates a single element for the type specified, within the DB.
372  *
373  * [in] parms
374  *   Pointer to allocate parameters
375  *
376  * Returns
377  *   - (0) if successful.
378  *   - (-EINVAL) on failure.
379  *   - (-ENOMEM) if pool is empty
380  */
381 int tf_rm_allocate(struct tf_rm_allocate_parms *parms);
382
383 /**
384  * Free's a single element for the type specified, within the DB.
385  *
386  * [in] parms
387  *   Pointer to free parameters
388  *
389  * Returns
390  *   - (0) if successful.
391  *   - (-EINVAL) on failure.
392  */
393 int tf_rm_free(struct tf_rm_free_parms *parms);
394
395 /**
396  * Performs an allocation verification check on a specified element.
397  *
398  * [in] parms
399  *   Pointer to is allocated parameters
400  *
401  * Returns
402  *   - (0) if successful.
403  *   - (-EINVAL) on failure.
404  */
405 /*
406  * NOTE:
407  *  - If pool is set to Chip MAX, then the query index must be checked
408  *    against the allocated range and query index must be allocated as well.
409  *  - If pool is allocated size only, then check if query index is allocated.
410  */
411 int tf_rm_is_allocated(struct tf_rm_is_allocated_parms *parms);
412
413 /**
414  * Retrieves an elements allocation information from the Resource
415  * Manager (RM) DB.
416  *
417  * [in] parms
418  *   Pointer to get info parameters
419  *
420  * Returns
421  *   - (0) if successful.
422  *   - (-EINVAL) on failure.
423  */
424 int tf_rm_get_info(struct tf_rm_get_alloc_info_parms *parms);
425
426 /**
427  * Performs a lookup in the Resource Manager DB and retrieves the
428  * requested HCAPI RM type.
429  *
430  * [in] parms
431  *   Pointer to get hcapi parameters
432  *
433  * Returns
434  *   - (0) if successful.
435  *   - (-EINVAL) on failure.
436  */
437 int tf_rm_get_hcapi_type(struct tf_rm_get_hcapi_parms *parms);
438
439 /**
440  * Performs a lookup in the Resource Manager DB and retrieves the
441  * requested HCAPI RM type inuse count.
442  *
443  * [in] parms
444  *   Pointer to get inuse parameters
445  *
446  * Returns
447  *   - (0) if successful.
448  *   - (-EINVAL) on failure.
449  */
450 int tf_rm_get_inuse_count(struct tf_rm_get_inuse_count_parms *parms);
451
452 #endif /* TF_RM_NEW_H_ */