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