net/bnxt: add dpool allocator for EM allocation
[dpdk.git] / drivers / net / bnxt / tf_core / tf_session.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2021 Broadcom
3  * All rights reserved.
4  */
5
6 #ifndef _TF_SESSION_H_
7 #define _TF_SESSION_H_
8
9 #include <stdint.h>
10 #include <stdlib.h>
11
12 #include "bitalloc.h"
13 #include "tf_core.h"
14 #include "tf_device.h"
15 #include "tf_rm.h"
16 #include "tf_tbl.h"
17 #include "tf_resources.h"
18 #include "stack.h"
19 #include "ll.h"
20
21 /**
22  * The Session module provides session control support. A session is
23  * to the ULP layer known as a session_info instance. The session
24  * private data is the actual session.
25  *
26  * Session manages:
27  *   - The device and all the resources related to the device.
28  *   - Any session sharing between ULP applications
29  */
30
31 /** Session defines
32  */
33 #define TF_SESSION_ID_INVALID     0xFFFFFFFF /** Invalid Session ID define */
34
35 /**
36  * At this stage we are using fixed size entries so that each
37  * stack entry represents either 2 or 4 RT (f/n)blocks. So we
38  * take the total block allocation for truflow and divide that
39  * by either 2 or 4.
40  */
41 #ifdef TF_EM_ENTRY_IPV4_ONLY
42 #define TF_SESSION_EM_ENTRY_SIZE 2 /* 2 blocks per entry */
43 #else
44 #define TF_SESSION_EM_ENTRY_SIZE 4 /* 4 blocks per entry */
45 #endif
46
47 /**
48  * Session
49  *
50  * Shared memory containing private TruFlow session information.
51  * Through this structure the session can keep track of resource
52  * allocations and (if so configured) any shadow copy of flow
53  * information. It also holds info about Session Clients.
54  *
55  * Memory is assigned to the Truflow instance by way of
56  * tf_open_session. Memory is allocated and owned by i.e. ULP.
57  *
58  * Access control to this shared memory is handled by the spin_lock in
59  * tf_session_info.
60  */
61 struct tf_session {
62         /** TrueFlow Version. Used to control the structure layout
63          * when sharing sessions. No guarantee that a secondary
64          * process would come from the same version of an executable.
65          */
66         struct tf_session_version ver;
67
68         /**
69          * Session ID, allocated by FW on tf_open_session()
70          */
71         union tf_session_id session_id;
72
73         /**
74          * Boolean controlling the use and availability of shared session.
75          * Shared session will allow the application to share resources
76          * on the firmware side without having to allocate them on firmware.
77          * Additional private session core_data will be allocated if this
78          * boolean is set to 'true', default 'false'.
79          *
80          */
81         bool shared_session;
82
83         /**
84          * This flag indicates the shared session on firmware side is created
85          * by this session. Some privileges may be assigned to this session.
86          *
87          */
88         bool shared_session_creator;
89
90         /**
91          * Boolean controlling the use and availability of shadow
92          * copy. Shadow copy will allow the TruFlow Core to keep track
93          * of resource content on the firmware side without having to
94          * query firmware. Additional private session core_data will
95          * be allocated if this boolean is set to 'true', default
96          * 'false'.
97          *
98          * Size of memory depends on the NVM Resource settings for the
99          * control channel.
100          */
101         bool shadow_copy;
102
103         /**
104          * Session Reference Count. To keep track of functions per
105          * session the ref_count is updated. There is also a
106          * parallel TruFlow Firmware ref_count in case the TruFlow
107          * Core goes away without informing the Firmware.
108          */
109         uint8_t ref_count;
110
111         /**
112          * Session Reference Count for attached sessions. To keep
113          * track of application sharing of a session the
114          * ref_count_attach is updated.
115          */
116         uint8_t ref_count_attach;
117
118         /**
119          * Device handle
120          */
121         struct tf_dev_info dev;
122         /**
123          * Device init flag. False if Device is not fully initialized,
124          * else true.
125          */
126         bool dev_init;
127
128         /**
129          * Linked list of clients registered for this session
130          */
131         struct ll client_ll;
132
133         /**
134          * em ext db reference for the session
135          */
136         void *em_ext_db_handle;
137
138         /**
139          * tcam db reference for the session
140          */
141         void *tcam_db_handle;
142
143         /**
144          * table db reference for the session
145          */
146         void *tbl_db_handle;
147
148         /**
149          * identifier db reference for the session
150          */
151         void *id_db_handle;
152
153         /**
154          * em db reference for the session
155          */
156         void *em_db_handle;
157
158         /**
159          * the pointer to the parent bp struct
160          */
161         void *bp;
162
163         /**
164          * EM allocator for session
165          */
166         void *em_pool[TF_DIR_MAX];
167 };
168
169 /**
170  * Session Client
171  *
172  * Shared memory for each of the Session Clients. A session can have
173  * one or more clients.
174  */
175 struct tf_session_client {
176         /**
177          * Linked list of clients
178          */
179         struct ll_entry ll_entry; /* For inserting in link list, must be
180                                    * first field of struct.
181                                    */
182
183         /**
184          * String containing name of control channel interface to be
185          * used for this session to communicate with firmware.
186          *
187          * ctrl_chan_name will be used as part of a name for any
188          * shared memory allocation.
189          */
190         char ctrl_chan_name[TF_SESSION_NAME_MAX];
191
192         /**
193          * Firmware FID, learned at time of Session Client create.
194          */
195         uint16_t fw_fid;
196
197         /**
198          * Session Client ID, allocated by FW on tf_register_session()
199          */
200         union tf_session_client_id session_client_id;
201 };
202
203 /**
204  * Session open parameter definition
205  */
206 struct tf_session_open_session_parms {
207         /**
208          * [in] Pointer to the TF open session configuration
209          */
210         struct tf_open_session_parms *open_cfg;
211 };
212
213 /**
214  * Session attach parameter definition
215  */
216 struct tf_session_attach_session_parms {
217         /**
218          * [in] Pointer to the TF attach session configuration
219          */
220         struct tf_attach_session_parms *attach_cfg;
221 };
222
223 /**
224  * Session close parameter definition
225  */
226 struct tf_session_close_session_parms {
227         /**
228          * []
229          */
230         uint8_t *ref_count;
231         /**
232          * []
233          */
234         union tf_session_id *session_id;
235 };
236
237 /**
238  * @page session Session Management
239  *
240  * @ref tf_session_open_session
241  *
242  * @ref tf_session_attach_session
243  *
244  * @ref tf_session_close_session
245  *
246  * @ref tf_session_is_fid_supported
247  *
248  * @ref tf_session_get_session_internal
249  *
250  * @ref tf_session_get_session
251  *
252  * @ref tf_session_get_session_client
253  *
254  * @ref tf_session_find_session_client_by_name
255  *
256  * @ref tf_session_find_session_client_by_fid
257  *
258  * @ref tf_session_get_device
259  *
260  * @ref tf_session_get_fw_session_id
261  *
262  * @ref tf_session_get_session_id
263  */
264
265 /**
266  * Creates a host session with a corresponding firmware session.
267  *
268  * [in] tfp
269  *   Pointer to TF handle
270  *
271  * [in] parms
272  *   Pointer to the session open parameters
273  *
274  * Returns
275  *   - (0) if successful.
276  *   - (-EINVAL) on failure.
277  */
278 int tf_session_open_session(struct tf *tfp,
279                             struct tf_session_open_session_parms *parms);
280
281 /**
282  * Attaches a previous created session.
283  *
284  * [in] tfp
285  *   Pointer to TF handle
286  *
287  * [in] parms
288  *   Pointer to the session attach parameters
289  *
290  * Returns
291  *   - (0) if successful.
292  *   - (-EINVAL) on failure.
293  */
294 int tf_session_attach_session(struct tf *tfp,
295                               struct tf_session_attach_session_parms *parms);
296
297 /**
298  * Closes a previous created session. Only possible if previous
299  * registered Clients had been unregistered first.
300  *
301  * [in] tfp
302  *   Pointer to TF handle
303  *
304  * [in/out] parms
305  *   Pointer to the session close parameters.
306  *
307  * Returns
308  *   - (0) if successful.
309  *   - (-EUSERS) if clients are still registered with the session.
310  *   - (-EINVAL) on failure.
311  */
312 int tf_session_close_session(struct tf *tfp,
313                              struct tf_session_close_session_parms *parms);
314
315 /**
316  * Verifies that the fid is supported by the session. Used to assure
317  * that a function i.e. client/control channel is registered with the
318  * session.
319  *
320  * [in] tfs
321  *   Pointer to TF Session handle
322  *
323  * [in] fid
324  *   FID value to check
325  *
326  * Returns
327  *   - (true) if successful, else false
328  *   - (-EINVAL) on failure.
329  */
330 bool
331 tf_session_is_fid_supported(struct tf_session *tfs,
332                             uint16_t fid);
333
334 /**
335  * Looks up the private session information from the TF session
336  * info. Does not perform a fid check against the registered
337  * clients. Should be used if tf_session_get_session() was used
338  * previously i.e. at the TF API boundary.
339  *
340  * [in] tfp
341  *   Pointer to TF handle
342  *
343  * [out] tfs
344  *   Pointer pointer to the session
345  *
346  * Returns
347  *   - (0) if successful.
348  *   - (-EINVAL) on failure.
349  */
350 int tf_session_get_session_internal(struct tf *tfp,
351                                     struct tf_session **tfs);
352
353 /**
354  * Looks up the private session information from the TF session
355  * info. Performs a fid check against the clients on the session.
356  *
357  * [in] tfp
358  *   Pointer to TF handle
359  *
360  * [out] tfs
361  *   Pointer pointer to the session
362  *
363  * Returns
364  *   - (0) if successful.
365  *   - (-EINVAL) on failure.
366  */
367 int tf_session_get_session(struct tf *tfp,
368                            struct tf_session **tfs);
369
370 /**
371  * Looks up client within the session.
372  *
373  * [in] tfs
374  *   Pointer pointer to the session
375  *
376  * [in] session_client_id
377  *   Client id to look for within the session
378  *
379  * Returns
380  *   client if successful.
381  *   - (NULL) on failure, client not found.
382  */
383 struct tf_session_client *
384 tf_session_get_session_client(struct tf_session *tfs,
385                               union tf_session_client_id session_client_id);
386
387 /**
388  * Looks up client using name within the session.
389  *
390  * [in] session, pointer to the session
391  *
392  * [in] session_client_name, name of the client to lookup in the session
393  *
394  * Returns:
395  *   - Pointer to the session, if found.
396  *   - (NULL) on failure, client not found.
397  */
398 struct tf_session_client *
399 tf_session_find_session_client_by_name(struct tf_session *tfs,
400                                        const char *ctrl_chan_name);
401
402 /**
403  * Looks up client using the fid.
404  *
405  * [in] session, pointer to the session
406  *
407  * [in] fid, fid of the client to find
408  *
409  * Returns:
410  *   - Pointer to the session, if found.
411  *   - (NULL) on failure, client not found.
412  */
413 struct tf_session_client *
414 tf_session_find_session_client_by_fid(struct tf_session *tfs,
415                                       uint16_t fid);
416
417 /**
418  * Looks up the device information from the TF Session.
419  *
420  * [in] tfp
421  *   Pointer to TF handle
422  *
423  * [out] tfd
424  *   Pointer pointer to the device
425  *
426  * Returns
427  *   - (0) if successful.
428  *   - (-EINVAL) on failure.
429  */
430 int tf_session_get_device(struct tf_session *tfs,
431                           struct tf_dev_info **tfd);
432
433 /**
434  * Looks up the FW Session id the requested TF handle.
435  *
436  * [in] tfp
437  *   Pointer to TF handle
438  *
439  * [out] session_id
440  *   Pointer to the session_id
441  *
442  * Returns
443  *   - (0) if successful.
444  *   - (-EINVAL) on failure.
445  */
446 int tf_session_get_fw_session_id(struct tf *tfp,
447                                  uint8_t *fw_session_id);
448
449 /**
450  * Looks up the Session id the requested TF handle.
451  *
452  * [in] tfp
453  *   Pointer to TF handle
454  *
455  * [out] session_id
456  *   Pointer to the session_id
457  *
458  * Returns
459  *   - (0) if successful.
460  *   - (-EINVAL) on failure.
461  */
462 int tf_session_get_session_id(struct tf *tfp,
463                               union tf_session_id *session_id);
464
465 /**
466  * API to get the em_ext_db from tf_session.
467  *
468  * [in] tfp
469  *   Pointer to TF handle
470  *
471  * [out] em_ext_db_handle, pointer to eem handle
472  *
473  * Returns:
474  *   - (0) if successful.
475  *   - (-EINVAL) on failure.
476  */
477 int
478 tf_session_get_em_ext_db(struct tf *tfp,
479                         void **em_ext_db_handle);
480
481 /**
482  * API to set the em_ext_db in tf_session.
483  *
484  * [in] tfp
485  *   Pointer to TF handle
486  *
487  * [in] em_ext_db_handle, pointer to eem handle
488  *
489  * Returns:
490  *   - (0) if successful.
491  *   - (-EINVAL) on failure.
492  */
493 int
494 tf_session_set_em_ext_db(struct tf *tfp,
495                         void *em_ext_db_handle);
496
497 /**
498  * API to get the db from tf_session.
499  *
500  * [in] tfp
501  *   Pointer to TF handle
502  *
503  * [out] db_handle, pointer to db handle
504  *
505  * Returns:
506  *   - (0) if successful.
507  *   - (-EINVAL) on failure.
508  */
509 int
510 tf_session_get_db(struct tf *tfp,
511                    enum tf_module_type type,
512                   void **db_handle);
513
514 /**
515  * API to set the db in tf_session.
516  *
517  * [in] tfp
518  *   Pointer to TF handle
519  *
520  * [in] db_handle, pointer to db handle
521  *
522  * Returns:
523  *   - (0) if successful.
524  *   - (-EINVAL) on failure.
525  */
526 int
527 tf_session_set_db(struct tf *tfp,
528                    enum tf_module_type type,
529                   void *db_handle);
530
531 /**
532  * Check if the session is shared session.
533  *
534  * [in] session, pointer to the session
535  *
536  * Returns:
537  *   - true if it is shared session
538  *   - false if it is not shared session
539  */
540 static inline bool
541 tf_session_is_shared_session(struct tf_session *tfs)
542 {
543         return tfs->shared_session;
544 }
545
546 /**
547  * Check if the session is the shared session creator
548  *
549  * [in] session, pointer to the session
550  *
551  * Returns:
552  *   - true if it is the shared session creator
553  *   - false if it is not the shared session creator
554  */
555 static inline bool
556 tf_session_is_shared_session_creator(struct tf_session *tfs)
557 {
558         return tfs->shared_session_creator;
559 }
560
561 /**
562  * Get the pointer to the parent bnxt struct
563  *
564  * [in] session, pointer to the session
565  *
566  * Returns:
567  *   - the pointer to the parent bnxt struct
568  */
569 static inline struct bnxt*
570 tf_session_get_bp(struct tf_session *tfs)
571 {
572         return tfs->bp;
573 }
574 #endif /* _TF_SESSION_H_ */