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