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