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