net/bnxt: cleanup ULP parser and mapper
[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         /** TruFlow 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          * EM allocator for session
160          */
161         void *em_pool[TF_DIR_MAX];
162
163 #ifdef TF_TCAM_SHARED
164         /**
165          * tcam db reference for the session
166          */
167         void *tcam_shared_db_handle;
168 #endif /* TF_TCAM_SHARED */
169 };
170
171 /**
172  * Session Client
173  *
174  * Shared memory for each of the Session Clients. A session can have
175  * one or more clients.
176  */
177 struct tf_session_client {
178         /**
179          * Linked list of clients
180          */
181         struct ll_entry ll_entry; /* For inserting in link list, must be
182                                    * first field of struct.
183                                    */
184
185         /**
186          * String containing name of control channel interface to be
187          * used for this session to communicate with firmware.
188          *
189          * ctrl_chan_name will be used as part of a name for any
190          * shared memory allocation.
191          */
192         char ctrl_chan_name[TF_SESSION_NAME_MAX];
193
194         /**
195          * Firmware FID, learned at time of Session Client create.
196          */
197         uint16_t fw_fid;
198
199         /**
200          * Session Client ID, allocated by FW on tf_register_session()
201          */
202         union tf_session_client_id session_client_id;
203 };
204
205 /**
206  * Session open parameter definition
207  */
208 struct tf_session_open_session_parms {
209         /**
210          * [in] Pointer to the TF open session configuration
211          */
212         struct tf_open_session_parms *open_cfg;
213 };
214
215 /**
216  * Session attach parameter definition
217  */
218 struct tf_session_attach_session_parms {
219         /**
220          * [in] Pointer to the TF attach session configuration
221          */
222         struct tf_attach_session_parms *attach_cfg;
223 };
224
225 /**
226  * Session close parameter definition
227  */
228 struct tf_session_close_session_parms {
229         /**
230          * []
231          */
232         uint8_t *ref_count;
233         /**
234          * []
235          */
236         union tf_session_id *session_id;
237 };
238
239 /**
240  * @page session Session Management
241  *
242  * @ref tf_session_open_session
243  *
244  * @ref tf_session_attach_session
245  *
246  * @ref tf_session_close_session
247  *
248  * @ref tf_session_is_fid_supported
249  *
250  * @ref tf_session_get_session_internal
251  *
252  * @ref tf_session_get_session
253  *
254  * @ref tf_session_get_session_client
255  *
256  * @ref tf_session_find_session_client_by_name
257  *
258  * @ref tf_session_find_session_client_by_fid
259  *
260  * @ref tf_session_get_device
261  *
262  * @ref tf_session_get_fw_session_id
263  *
264  * @ref tf_session_get_session_id
265  *
266  * @ref tf_session_is_shared_session_creator
267  *
268  * @ref tf_session_get_db
269  *
270  * @ref tf_session_set_db
271  *
272  * @ref tf_session_get_bp
273  *
274  * @ref tf_session_is_shared_session
275  *
276  * #define TF_SHARED
277  * @ref tf_session_get_tcam_shared_db
278  *
279  * @ref tf_session_set_tcam_shared_db
280  * #endif
281  */
282
283 /**
284  * Creates a host session with a corresponding firmware session.
285  *
286  * [in] tfp
287  *   Pointer to TF handle
288  *
289  * [in] parms
290  *   Pointer to the session open parameters
291  *
292  * Returns
293  *   - (0) if successful.
294  *   - (-EINVAL) on failure.
295  */
296 int tf_session_open_session(struct tf *tfp,
297                             struct tf_session_open_session_parms *parms);
298
299 /**
300  * Attaches a previous created session.
301  *
302  * [in] tfp
303  *   Pointer to TF handle
304  *
305  * [in] parms
306  *   Pointer to the session attach parameters
307  *
308  * Returns
309  *   - (0) if successful.
310  *   - (-EINVAL) on failure.
311  */
312 int tf_session_attach_session(struct tf *tfp,
313                               struct tf_session_attach_session_parms *parms);
314
315 /**
316  * Closes a previous created session. Only possible if previous
317  * registered Clients had been unregistered first.
318  *
319  * [in] tfp
320  *   Pointer to TF handle
321  *
322  * [in/out] parms
323  *   Pointer to the session close parameters.
324  *
325  * Returns
326  *   - (0) if successful.
327  *   - (-EUSERS) if clients are still registered with the session.
328  *   - (-EINVAL) on failure.
329  */
330 int tf_session_close_session(struct tf *tfp,
331                              struct tf_session_close_session_parms *parms);
332
333 /**
334  * Verifies that the fid is supported by the session. Used to assure
335  * that a function i.e. client/control channel is registered with the
336  * session.
337  *
338  * [in] tfs
339  *   Pointer to TF Session handle
340  *
341  * [in] fid
342  *   FID value to check
343  *
344  * Returns
345  *   - (true) if successful, else false
346  *   - (-EINVAL) on failure.
347  */
348 bool
349 tf_session_is_fid_supported(struct tf_session *tfs,
350                             uint16_t fid);
351
352 /**
353  * Looks up the private session information from the TF session
354  * info. Does not perform a fid check against the registered
355  * clients. Should be used if tf_session_get_session() was used
356  * previously i.e. at the TF API boundary.
357  *
358  * [in] tfp
359  *   Pointer to TF handle
360  *
361  * [out] tfs
362  *   Pointer pointer to the session
363  *
364  * Returns
365  *   - (0) if successful.
366  *   - (-EINVAL) on failure.
367  */
368 int tf_session_get_session_internal(struct tf *tfp,
369                                     struct tf_session **tfs);
370
371 /**
372  * Looks up the private session information from the TF session
373  * info. Performs a fid check against the clients on the session.
374  *
375  * [in] tfp
376  *   Pointer to TF handle
377  *
378  * [out] tfs
379  *   Pointer pointer to the session
380  *
381  * Returns
382  *   - (0) if successful.
383  *   - (-EINVAL) on failure.
384  */
385 int tf_session_get_session(struct tf *tfp,
386                            struct tf_session **tfs);
387
388 /**
389  * Looks up client within the session.
390  *
391  * [in] tfs
392  *   Pointer pointer to the session
393  *
394  * [in] session_client_id
395  *   Client id to look for within the session
396  *
397  * Returns
398  *   client if successful.
399  *   - (NULL) on failure, client not found.
400  */
401 struct tf_session_client *
402 tf_session_get_session_client(struct tf_session *tfs,
403                               union tf_session_client_id session_client_id);
404
405 /**
406  * Looks up client using name within the session.
407  *
408  * [in] session, pointer to the session
409  *
410  * [in] session_client_name, name of the client to lookup in the session
411  *
412  * Returns:
413  *   - Pointer to the session, if found.
414  *   - (NULL) on failure, client not found.
415  */
416 struct tf_session_client *
417 tf_session_find_session_client_by_name(struct tf_session *tfs,
418                                        const char *ctrl_chan_name);
419
420 /**
421  * Looks up client using the fid.
422  *
423  * [in] session, pointer to the session
424  *
425  * [in] fid, fid of the client to find
426  *
427  * Returns:
428  *   - Pointer to the session, if found.
429  *   - (NULL) on failure, client not found.
430  */
431 struct tf_session_client *
432 tf_session_find_session_client_by_fid(struct tf_session *tfs,
433                                       uint16_t fid);
434
435 /**
436  * Looks up the device information from the TF Session.
437  *
438  * [in] tfp
439  *   Pointer to TF handle
440  *
441  * [out] tfd
442  *   Pointer pointer to the device
443  *
444  * Returns
445  *   - (0) if successful.
446  *   - (-EINVAL) on failure.
447  */
448 int tf_session_get_device(struct tf_session *tfs,
449                           struct tf_dev_info **tfd);
450
451 /**
452  * Looks up the FW Session id the requested TF handle.
453  *
454  * [in] tfp
455  *   Pointer to TF handle
456  *
457  * [out] session_id
458  *   Pointer to the session_id
459  *
460  * Returns
461  *   - (0) if successful.
462  *   - (-EINVAL) on failure.
463  */
464 int tf_session_get_fw_session_id(struct tf *tfp,
465                                  uint8_t *fw_session_id);
466
467 /**
468  * Looks up the Session id the requested TF handle.
469  *
470  * [in] tfp
471  *   Pointer to TF handle
472  *
473  * [out] session_id
474  *   Pointer to the session_id
475  *
476  * Returns
477  *   - (0) if successful.
478  *   - (-EINVAL) on failure.
479  */
480 int tf_session_get_session_id(struct tf *tfp,
481                               union tf_session_id *session_id);
482
483 /**
484  * API to get the em_ext_db from tf_session.
485  *
486  * [in] tfp
487  *   Pointer to TF handle
488  *
489  * [out] em_ext_db_handle, pointer to eem handle
490  *
491  * Returns:
492  *   - (0) if successful.
493  *   - (-EINVAL) on failure.
494  */
495 int
496 tf_session_get_em_ext_db(struct tf *tfp,
497                         void **em_ext_db_handle);
498
499 /**
500  * API to set the em_ext_db in tf_session.
501  *
502  * [in] tfp
503  *   Pointer to TF handle
504  *
505  * [in] em_ext_db_handle, pointer to eem handle
506  *
507  * Returns:
508  *   - (0) if successful.
509  *   - (-EINVAL) on failure.
510  */
511 int
512 tf_session_set_em_ext_db(struct tf *tfp,
513                         void *em_ext_db_handle);
514
515 /**
516  * API to get the db from tf_session.
517  *
518  * [in] tfp
519  *   Pointer to TF handle
520  *
521  * [out] db_handle, pointer to db handle
522  *
523  * Returns:
524  *   - (0) if successful.
525  *   - (-EINVAL) on failure.
526  */
527 int
528 tf_session_get_db(struct tf *tfp,
529                    enum tf_module_type type,
530                   void **db_handle);
531
532 /**
533  * API to set the db in tf_session.
534  *
535  * [in] tfp
536  *   Pointer to TF handle
537  *
538  * [in] db_handle, pointer to db handle
539  *
540  * Returns:
541  *   - (0) if successful.
542  *   - (-EINVAL) on failure.
543  */
544 int
545 tf_session_set_db(struct tf *tfp,
546                    enum tf_module_type type,
547                   void *db_handle);
548
549 /**
550  * Check if the session is shared session.
551  *
552  * [in] session, pointer to the session
553  *
554  * Returns:
555  *   - true if it is shared session
556  *   - false if it is not shared session
557  */
558 static inline bool
559 tf_session_is_shared_session(struct tf_session *tfs)
560 {
561         return tfs->shared_session;
562 }
563
564 /**
565  * Check if the session is the shared session creator
566  *
567  * [in] session, pointer to the session
568  *
569  * Returns:
570  *   - true if it is the shared session creator
571  *   - false if it is not the shared session creator
572  */
573 static inline bool
574 tf_session_is_shared_session_creator(struct tf_session *tfs)
575 {
576         return tfs->shared_session_creator;
577 }
578
579 /**
580  * Get the pointer to the parent bnxt struct
581  *
582  * [in] session, pointer to the session
583  *
584  * Returns:
585  *   - the pointer to the parent bnxt struct
586  */
587 static inline struct bnxt*
588 tf_session_get_bp(struct tf *tfp)
589 {
590         return tfp->bp;
591 }
592
593 /**
594  * Set the pointer to the tcam shared database
595  *
596  * [in] session, pointer to the session
597  *
598  * Returns:
599  *   - the pointer to the parent bnxt struct
600  */
601 int
602 tf_session_set_tcam_shared_db(struct tf *tfp,
603                               void *tcam_shared_db_handle);
604
605 /**
606  * Get the pointer to the tcam shared database
607  *
608  * [in] session, pointer to the session
609  *
610  * Returns:
611  *   - the pointer to the parent bnxt struct
612  */
613 int
614 tf_session_get_tcam_shared_db(struct tf *tfp,
615                               void **tcam_shared_db_handle);
616
617 #endif /* _TF_SESSION_H_ */