xref: /dpdk/drivers/net/bnxt/tf_core/tf_session.h (revision 5873bd31dc8393b17fb7b45bd1f714c227dad3f5)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2024 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 #include "bitalloc.h"
12 #include "tf_core.h"
13 #include "tf_device.h"
14 #include "tf_rm.h"
15 #include "tf_resources.h"
16 #include "stack.h"
17 #include "ll.h"
18 
19 /**
20  * The Session module provides session control support. A session is
21  * to the ULP layer known as a session_info instance. The session
22  * private data is the actual session.
23  *
24  * Session manages:
25  *   - The device and all the resources related to the device.
26  *   - Any session sharing between ULP applications
27  */
28 
29 /** Session defines
30  */
31 #define TF_SESSION_ID_INVALID     0xFFFFFFFF /** Invalid Session ID define */
32 
33 /**
34  * At this stage we are using fixed size entries so that each
35  * stack entry represents either 2 or 4 RT (f/n)blocks. So we
36  * take the total block allocation for truflow and divide that
37  * by either 2 or 4.
38  */
39 #ifdef TF_EM_ENTRY_IPV4_ONLY
40 #define TF_SESSION_EM_ENTRY_SIZE 2 /* 2 blocks per entry */
41 #else
42 #define TF_SESSION_EM_ENTRY_SIZE 4 /* 4 blocks per entry */
43 #endif
44 
45 /**
46  * Session
47  *
48  * Shared memory containing private TruFlow session information.
49  * Through this structure the session can keep track of resource
50  * allocations. It also holds info about Session Clients.
51  *
52  * Memory is assigned to the Truflow instance by way of
53  * tf_open_session. Memory is allocated and owned by i.e. ULP.
54  *
55  * Access control to this shared memory is handled by the spin_lock in
56  * tf_session_info.
57  */
58 struct tf_session {
59 	/** TruFlow Version. Used to control the structure layout
60 	 * when sharing sessions. No guarantee that a secondary
61 	 * process would come from the same version of an executable.
62 	 */
63 	struct tf_session_version ver;
64 
65 	/**
66 	 * Session ID, allocated by FW on tf_open_session()
67 	 */
68 	union tf_session_id session_id;
69 
70 	/**
71 	 * Boolean controlling the use and availability of shared session.
72 	 * Shared session will allow the application to share resources
73 	 * on the firmware side without having to allocate them on firmware.
74 	 * Additional private session core_data will be allocated if this
75 	 * boolean is set to 'true', default 'false'.
76 	 *
77 	 */
78 	bool shared_session;
79 
80 	/**
81 	 * Boolean controlling the split of hardware resources for hotupgrade.
82 	 */
83 	bool shared_session_hotup;
84 
85 	/**
86 	 * This flag indicates the shared session on firmware side is created
87 	 * by this session. Some privileges may be assigned to this session.
88 	 *
89 	 */
90 	bool shared_session_creator;
91 
92 	/**
93 	 * Session Reference Count. To keep track of functions per
94 	 * session the ref_count is updated. There is also a
95 	 * parallel TruFlow Firmware ref_count in case the TruFlow
96 	 * Core goes away without informing the Firmware.
97 	 */
98 	uint8_t ref_count;
99 
100 	/**
101 	 * Session Reference Count for attached sessions. To keep
102 	 * track of application sharing of a session the
103 	 * ref_count_attach is updated.
104 	 */
105 	uint8_t ref_count_attach;
106 
107 	/**
108 	 * Device handle
109 	 */
110 	struct tf_dev_info dev;
111 	/**
112 	 * Device init flag. False if Device is not fully initialized,
113 	 * else true.
114 	 */
115 	bool dev_init;
116 
117 	/**
118 	 * Linked list of clients registered for this session
119 	 */
120 	struct ll client_ll;
121 
122 	/**
123 	 * em ext db reference for the session
124 	 */
125 	void *em_ext_db_handle;
126 
127 	/**
128 	 * tcam db reference for the session
129 	 */
130 	void *tcam_db_handle;
131 
132 	/**
133 	 * table db reference for the session
134 	 */
135 	void *tbl_db_handle;
136 
137 	/**
138 	 * identifier db reference for the session
139 	 */
140 	void *id_db_handle;
141 
142 	/**
143 	 * em db reference for the session
144 	 */
145 	void *em_db_handle;
146 
147 	/**
148 	 * EM allocator for session
149 	 */
150 	void *em_pool[TF_DIR_MAX];
151 
152 	/**
153 	 * tcam db reference for the session
154 	 */
155 	void *tcam_shared_db_handle;
156 
157 	/**
158 	 * SRAM db reference for the session
159 	 */
160 	void *sram_handle;
161 
162 	/**
163 	 * if table db reference for the session
164 	 */
165 	void *if_tbl_db_handle;
166 
167 	/**
168 	 * global db reference for the session
169 	 */
170 	void *global_db_handle;
171 
172 	/**
173 	 * Number of slices per row for WC TCAM
174 	 */
175 	uint16_t wc_num_slices_per_row;
176 
177 	/**
178 	 * TCAM Manager handle pointing to session based tcam memory
179 	 */
180 	void *tcam_mgr_handle;
181 };
182 
183 /**
184  * Session Client
185  *
186  * Shared memory for each of the Session Clients. A session can have
187  * one or more clients.
188  */
189 struct tf_session_client {
190 	/**
191 	 * Linked list of clients
192 	 */
193 	struct ll_entry ll_entry; /* For inserting in link list, must be
194 				   * first field of struct.
195 				   */
196 
197 	/**
198 	 * String containing name of control channel interface to be
199 	 * used for this session to communicate with firmware.
200 	 *
201 	 * ctrl_chan_name will be used as part of a name for any
202 	 * shared memory allocation.
203 	 */
204 	char ctrl_chan_name[TF_SESSION_NAME_MAX];
205 
206 	/**
207 	 * Firmware FID, learned at time of Session Client create.
208 	 */
209 	uint16_t fw_fid;
210 
211 	/**
212 	 * Session Client ID, allocated by FW on tf_register_session()
213 	 */
214 	union tf_session_client_id session_client_id;
215 };
216 
217 /**
218  * Session open parameter definition
219  */
220 struct tf_session_open_session_parms {
221 	/**
222 	 * [in] Pointer to the TF open session configuration
223 	 */
224 	struct tf_open_session_parms *open_cfg;
225 };
226 
227 /**
228  * Session attach parameter definition
229  */
230 struct tf_session_attach_session_parms {
231 	/**
232 	 * [in] Pointer to the TF attach session configuration
233 	 */
234 	struct tf_attach_session_parms *attach_cfg;
235 };
236 
237 /**
238  * Session close parameter definition
239  */
240 struct tf_session_close_session_parms {
241 	/**
242 	 * []
243 	 */
244 	uint8_t *ref_count;
245 	/**
246 	 * []
247 	 */
248 	union tf_session_id *session_id;
249 };
250 
251 /**
252  * @page session Session Management
253  *
254  * @ref tf_session_open_session
255  *
256  * @ref tf_session_attach_session
257  *
258  * @ref tf_session_close_session
259  *
260  * @ref tf_session_is_fid_supported
261  *
262  * @ref tf_session_get_session_internal
263  *
264  * @ref tf_session_get_session
265  *
266  * @ref tf_session_get_session_client
267  *
268  * @ref tf_session_find_session_client_by_name
269  *
270  * @ref tf_session_find_session_client_by_fid
271  *
272  * @ref tf_session_get_device
273  *
274  * @ref tf_session_get_fw_session_id
275  *
276  * @ref tf_session_get_session_id
277  *
278  * @ref tf_session_is_shared_session_creator
279  *
280  * @ref tf_session_get_db
281  *
282  * @ref tf_session_set_db
283  *
284  * @ref tf_session_get_bp
285  *
286  * @ref tf_session_is_shared_session
287  *
288  * @ref tf_session_get_tcam_shared_db
289  *
290  * @ref tf_session_set_tcam_shared_db
291  *
292  * @ref tf_session_get_sram_db
293  *
294  * @ref tf_session_set_sram_db
295  */
296 
297 /**
298  * Creates a host session with a corresponding firmware session.
299  *
300  * [in] tfp
301  *   Pointer to TF handle
302  *
303  * [in] parms
304  *   Pointer to the session open parameters
305  *
306  * Returns
307  *   - (0) if successful.
308  *   - (-EINVAL) on failure.
309  */
310 int tf_session_open_session(struct tf *tfp,
311 			    struct tf_session_open_session_parms *parms);
312 
313 /**
314  * Attaches a previous created session.
315  *
316  * [in] tfp
317  *   Pointer to TF handle
318  *
319  * [in] parms
320  *   Pointer to the session attach parameters
321  *
322  * Returns
323  *   - (0) if successful.
324  *   - (-EINVAL) on failure.
325  */
326 int tf_session_attach_session(struct tf *tfp,
327 			      struct tf_session_attach_session_parms *parms);
328 
329 /**
330  * Closes a previous created session. Only possible if previous
331  * registered Clients had been unregistered first.
332  *
333  * [in] tfp
334  *   Pointer to TF handle
335  *
336  * [in/out] parms
337  *   Pointer to the session close parameters.
338  *
339  * Returns
340  *   - (0) if successful.
341  *   - (-EUSERS) if clients are still registered with the session.
342  *   - (-EINVAL) on failure.
343  */
344 int tf_session_close_session(struct tf *tfp,
345 			     struct tf_session_close_session_parms *parms);
346 
347 /**
348  * Verifies that the fid is supported by the session. Used to assure
349  * that a function i.e. client/control channel is registered with the
350  * session.
351  *
352  * [in] tfs
353  *   Pointer to TF Session handle
354  *
355  * [in] fid
356  *   FID value to check
357  *
358  * Returns
359  *   - (true) if successful, else false
360  *   - (-EINVAL) on failure.
361  */
362 bool
363 tf_session_is_fid_supported(struct tf_session *tfs,
364 			    uint16_t fid);
365 
366 /**
367  * Looks up the private session information from the TF session
368  * info. Does not perform a fid check against the registered
369  * clients. Should be used if tf_session_get_session() was used
370  * previously i.e. at the TF API boundary.
371  *
372  * [in] tfp
373  *   Pointer to TF handle
374  *
375  * [out] tfs
376  *   Pointer pointer to the session
377  *
378  * Returns
379  *   - (0) if successful.
380  *   - (-EINVAL) on failure.
381  */
382 int tf_session_get_session_internal(struct tf *tfp,
383 				    struct tf_session **tfs);
384 
385 /**
386  * Looks up the private session information from the TF session
387  * info. Performs a fid check against the clients on the session.
388  *
389  * [in] tfp
390  *   Pointer to TF handle
391  *
392  * [out] tfs
393  *   Pointer pointer to the session
394  *
395  * Returns
396  *   - (0) if successful.
397  *   - (-EINVAL) on failure.
398  */
399 int tf_session_get_session(struct tf *tfp,
400 			   struct tf_session **tfs);
401 
402 /**
403  * Looks up client within the session.
404  *
405  * [in] tfs
406  *   Pointer pointer to the session
407  *
408  * [in] session_client_id
409  *   Client id to look for within the session
410  *
411  * Returns
412  *   client if successful.
413  *   - (NULL) on failure, client not found.
414  */
415 struct tf_session_client *
416 tf_session_get_session_client(struct tf_session *tfs,
417 			      union tf_session_client_id session_client_id);
418 
419 /**
420  * Looks up client using name within the session.
421  *
422  * [in] session, pointer to the session
423  *
424  * [in] session_client_name, name of the client to lookup in the session
425  *
426  * Returns:
427  *   - Pointer to the session, if found.
428  *   - (NULL) on failure, client not found.
429  */
430 struct tf_session_client *
431 tf_session_find_session_client_by_name(struct tf_session *tfs,
432 				       const char *ctrl_chan_name);
433 
434 /**
435  * Looks up client using the fid.
436  *
437  * [in] session, pointer to the session
438  *
439  * [in] fid, fid of the client to find
440  *
441  * Returns:
442  *   - Pointer to the session, if found.
443  *   - (NULL) on failure, client not found.
444  */
445 struct tf_session_client *
446 tf_session_find_session_client_by_fid(struct tf_session *tfs,
447 				      uint16_t fid);
448 
449 /**
450  * Looks up the device information from the TF Session.
451  *
452  * [in] tfs
453  *   Pointer to session handle
454  *
455  * [out] tfd
456  *   Pointer to the device
457  *
458  * Returns
459  *   - (0) if successful.
460  *   - (-EINVAL) on failure.
461  */
462 int tf_session_get_device(struct tf_session *tfs,
463 			  struct tf_dev_info **tfd);
464 
465 /**
466  * Returns the session and the device from the tfp.
467  *
468  * [in] tfp
469  *   Pointer to TF handle
470  *
471  * [out] tfs
472  *   Pointer to the session
473  *
474  * [out] tfd
475  *   Pointer to the device
476 
477  * Returns
478  *   - (0) if successful.
479  *   - (-EINVAL) on failure.
480  */
481 int tf_session_get(struct tf *tfp,
482 		   struct tf_session **tfs,
483 		   struct tf_dev_info **tfd);
484 
485 /**
486  * Looks up the FW Session id the requested TF handle.
487  *
488  * [in] tfp
489  *   Pointer to TF handle
490  *
491  * [out] session_id
492  *   Pointer to the session_id
493  *
494  * Returns
495  *   - (0) if successful.
496  *   - (-EINVAL) on failure.
497  */
498 int tf_session_get_fw_session_id(struct tf *tfp,
499 				 uint8_t *fw_session_id);
500 
501 /**
502  * Looks up the Session id the requested TF handle.
503  *
504  * [in] tfp
505  *   Pointer to TF handle
506  *
507  * [out] session_id
508  *   Pointer to the session_id
509  *
510  * Returns
511  *   - (0) if successful.
512  *   - (-EINVAL) on failure.
513  */
514 int tf_session_get_session_id(struct tf *tfp,
515 			      union tf_session_id *session_id);
516 
517 /**
518  * API to get the em_ext_db from tf_session.
519  *
520  * [in] tfp
521  *   Pointer to TF handle
522  *
523  * [out] em_ext_db_handle, pointer to eem handle
524  *
525  * Returns:
526  *   - (0) if successful.
527  *   - (-EINVAL) on failure.
528  */
529 int
530 tf_session_get_em_ext_db(struct tf *tfp,
531 			void **em_ext_db_handle);
532 
533 /**
534  * API to set the em_ext_db in tf_session.
535  *
536  * [in] tfp
537  *   Pointer to TF handle
538  *
539  * [in] em_ext_db_handle, pointer to eem handle
540  *
541  * Returns:
542  *   - (0) if successful.
543  *   - (-EINVAL) on failure.
544  */
545 int
546 tf_session_set_em_ext_db(struct tf *tfp,
547 			void *em_ext_db_handle);
548 
549 /**
550  * API to get the db from tf_session.
551  *
552  * [in] tfp
553  *   Pointer to TF handle
554  *
555  * [out] db_handle, pointer to db handle
556  *
557  * Returns:
558  *   - (0) if successful.
559  *   - (-EINVAL) on failure.
560  */
561 int
562 tf_session_get_db(struct tf *tfp,
563 		   enum tf_module_type type,
564 		  void **db_handle);
565 
566 /**
567  * API to set the db in tf_session.
568  *
569  * [in] tfp
570  *   Pointer to TF handle
571  *
572  * [in] db_handle, pointer to db handle
573  *
574  * Returns:
575  *   - (0) if successful.
576  *   - (-EINVAL) on failure.
577  */
578 int
579 tf_session_set_db(struct tf *tfp,
580 		   enum tf_module_type type,
581 		  void *db_handle);
582 
583 /**
584  * Check if the session is shared session.
585  *
586  * [in] session, pointer to the session
587  *
588  * Returns:
589  *   - true if it is shared session
590  *   - false if it is not shared session
591  */
592 static inline bool
593 tf_session_is_shared_session(struct tf_session *tfs)
594 {
595 	return tfs->shared_session;
596 }
597 
598 /**
599  * Check if the session is shared session for hot upgrade.
600  *
601  * [in] session, pointer to the session
602  *
603  * Returns:
604  *   - true if it is shared session for hot upgrade
605  *   - false if it is not shared session for hot upgrade
606  */
607 static inline bool
608 tf_session_is_shared_hotup_session(struct tf_session *tfs)
609 {
610 	return tfs->shared_session_hotup;
611 }
612 
613 /**
614  * Check if the session is the shared session creator
615  *
616  * [in] session, pointer to the session
617  *
618  * Returns:
619  *   - true if it is the shared session creator
620  *   - false if it is not the shared session creator
621  */
622 static inline bool
623 tf_session_is_shared_session_creator(struct tf_session *tfs)
624 {
625 	return tfs->shared_session_creator;
626 }
627 
628 /**
629  * Get the pointer to the parent bnxt struct
630  *
631  * [in] session, pointer to the session
632  *
633  * Returns:
634  *   - the pointer to the parent bnxt struct
635  */
636 static inline struct bnxt*
637 tf_session_get_bp(struct tf *tfp)
638 {
639 	return tfp->bp;
640 }
641 
642 /**
643  * Set the pointer to the tcam shared database
644  *
645  * [in] session, pointer to the session
646  *
647  * Returns:
648  *   - the pointer to the parent bnxt struct
649  */
650 int
651 tf_session_set_tcam_shared_db(struct tf *tfp,
652 			      void *tcam_shared_db_handle);
653 
654 /**
655  * Get the pointer to the tcam shared database
656  *
657  * [in] session, pointer to the session
658  *
659  * Returns:
660  *   - the pointer to the parent bnxt struct
661  */
662 int
663 tf_session_get_tcam_shared_db(struct tf *tfp,
664 			      void **tcam_shared_db_handle);
665 
666 /**
667  * Set the pointer to the SRAM database
668  *
669  * [in] session, pointer to the session
670  *
671  * Returns:
672  *   - the pointer to the parent bnxt struct
673  */
674 int
675 tf_session_set_sram_db(struct tf *tfp,
676 		       void *sram_handle);
677 
678 /**
679  * Get the pointer to the SRAM database
680  *
681  * [in] session, pointer to the session
682  *
683  * Returns:
684  *   - the pointer to the parent bnxt struct
685  */
686 int
687 tf_session_get_sram_db(struct tf *tfp,
688 		       void **sram_handle);
689 
690 /**
691  * Set the pointer to the global cfg database
692  *
693  * [in] session, pointer to the session
694  *
695  * Returns:
696  *   - (0) if successful.
697  *   - (-EINVAL) on failure.
698  */
699 int
700 tf_session_set_global_db(struct tf *tfp,
701 			 void *global_handle);
702 
703 /**
704  * Get the pointer to the global cfg database
705  *
706  * [in] session, pointer to the session
707  *
708  * Returns:
709  *   - (0) if successful.
710  *   - (-EINVAL) on failure.
711  */
712 int
713 tf_session_get_global_db(struct tf *tfp,
714 			 void **global_handle);
715 
716 /**
717  * Set the pointer to the if table cfg database
718  *
719  * [in] session, pointer to the session
720  *
721  * Returns:
722  *   - (0) if successful.
723  *   - (-EINVAL) on failure.
724  */
725 int
726 tf_session_set_if_tbl_db(struct tf *tfp,
727 			 void *if_tbl_handle);
728 
729 /**
730  * Get the pointer to the if table cfg database
731  *
732  * [in] session, pointer to the session
733  *
734  * Returns:
735  *   - (0) if successful.
736  *   - (-EINVAL) on failure.
737  */
738 int
739 tf_session_get_if_tbl_db(struct tf *tfp,
740 			 void **if_tbl_handle);
741 
742 /**
743  * Set hot upgrade session state.
744  *
745  * [in] tfp
746  *   Pointer to session handle
747  *
748  * [in] parms
749  *   Hot upgrade session state parms
750  *
751  * Returns:
752  *  0 on Success else internal Truflow error
753  */
754 int
755 tf_session_set_hotup_state(struct tf *tfp,
756 			   struct tf_set_session_hotup_state_parms *parms);
757 
758 /**
759  * Get hot upgrade session state.
760  *
761  * [in] tfp
762  *   Pointer to session handle
763  *
764  * [out] parms
765  *   Pointer to hot upgrade session state parms
766  *
767  * Returns:
768  *  0 on Success else internal Truflow error
769  */
770 int
771 tf_session_get_hotup_state(struct tf *tfp,
772 			   struct tf_get_session_hotup_state_parms *parms);
773 #endif /* _TF_SESSION_H_ */
774