net/bnxt: support TruFlow core TCAM
[dpdk.git] / drivers / net / bnxt / tf_core / tf_core.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2020 Broadcom
3  * All rights reserved.
4  */
5
6 #ifndef _TF_CORE_H_
7 #define _TF_CORE_H_
8
9 #include <stdint.h>
10 #include <stdlib.h>
11 #include <stdbool.h>
12 #include <stdio.h>
13
14 #include "tf_project.h"
15
16 /**
17  * @file
18  *
19  * Truflow Core API Header File
20  */
21
22 /********** BEGIN Truflow Core DEFINITIONS **********/
23
24 /**
25  * direction
26  */
27 enum tf_dir {
28         TF_DIR_RX,  /**< Receive */
29         TF_DIR_TX,  /**< Transmit */
30         TF_DIR_MAX
31 };
32
33 /**
34  * External pool size
35  *
36  * Defines a single pool of external action records of
37  * fixed size.  Currently, this is an index.
38  */
39 #define TF_EXT_POOL_ENTRY_SZ_BYTES 1
40
41 /**
42  *  External pool entry count
43  *
44  *  Defines the number of entries in the external action pool
45  */
46 #define TF_EXT_POOL_ENTRY_CNT (1 * 1024)
47
48 /**
49  * Number of external pools
50  */
51 #define TF_EXT_POOL_CNT_MAX 1
52
53 /**
54  * External pool Id
55  */
56 #define TF_EXT_POOL_0      0 /**< matches TF_TBL_TYPE_EXT   */
57 #define TF_EXT_POOL_1      1 /**< matches TF_TBL_TYPE_EXT_0 */
58
59 /********** BEGIN API FUNCTION PROTOTYPES/PARAMETERS **********/
60
61 /**
62  * @page general General
63  *
64  * @ref tf_open_session
65  *
66  * @ref tf_attach_session
67  *
68  * @ref tf_close_session
69  */
70
71
72 /** Session Version defines
73  *
74  * The version controls the format of the tf_session and
75  * tf_session_info structure. This is to assure upgrade between
76  * versions can be supported.
77  */
78 #define TF_SESSION_VER_MAJOR  1   /**< Major Version */
79 #define TF_SESSION_VER_MINOR  0   /**< Minor Version */
80 #define TF_SESSION_VER_UPDATE 0   /**< Update Version */
81
82 /** Session Name
83  *
84  * Name of the TruFlow control channel interface.  Expects
85  * format to be RTE Name specific, i.e. rte_eth_dev_get_name_by_port()
86  */
87 #define TF_SESSION_NAME_MAX       64
88
89 #define TF_FW_SESSION_ID_INVALID  0xFF  /**< Invalid FW Session ID define */
90
91 /** Session Identifier
92  *
93  * Unique session identifier which includes PCIe bus info to
94  * distinguish the PF and session info to identify the associated
95  * TruFlow session. Session ID is constructed from the passed in
96  * ctrl_chan_name in tf_open_session() together with an allocated
97  * fw_session_id. Done by TruFlow on tf_open_session().
98  */
99 union tf_session_id {
100         uint32_t id;
101         struct {
102                 uint8_t domain;
103                 uint8_t bus;
104                 uint8_t device;
105                 uint8_t fw_session_id;
106         } internal;
107 };
108
109 /** Session Version
110  *
111  * The version controls the format of the tf_session and
112  * tf_session_info structure. This is to assure upgrade between
113  * versions can be supported.
114  *
115  * Please see the TF_VER_MAJOR/MINOR and UPDATE defines.
116  */
117 struct tf_session_version {
118         uint8_t major;
119         uint8_t minor;
120         uint8_t update;
121 };
122
123 /** Session supported device types
124  *
125  */
126 enum tf_device_type {
127         TF_DEVICE_TYPE_WH = 0, /**< Whitney+  */
128         TF_DEVICE_TYPE_BRD2,   /**< TBD       */
129         TF_DEVICE_TYPE_BRD3,   /**< TBD       */
130         TF_DEVICE_TYPE_BRD4,   /**< TBD       */
131         TF_DEVICE_TYPE_MAX     /**< Maximum   */
132 };
133
134 /** TruFlow Session Information
135  *
136  * Structure defining a TruFlow Session, also known as a Management
137  * session. This structure is initialized at time of
138  * tf_open_session(). It is passed to all of the TruFlow APIs as way
139  * to prescribe and isolate resources between different TruFlow ULP
140  * Applications.
141  */
142 struct tf_session_info {
143         /**
144          * TrueFlow Version. Used to control the structure layout when
145          * sharing sessions. No guarantee that a secondary process
146          * would come from the same version of an executable.
147          * TruFlow initializes this variable on tf_open_session().
148          *
149          * Owner:  TruFlow
150          * Access: TruFlow
151          */
152         struct tf_session_version ver;
153         /**
154          * will be STAILQ_ENTRY(tf_session_info) next
155          *
156          * Owner:  ULP
157          * Access: ULP
158          */
159         void                 *next;
160         /**
161          * Session ID is a unique identifier for the session. TruFlow
162          * initializes this variable during tf_open_session()
163          * processing.
164          *
165          * Owner:  TruFlow
166          * Access: Truflow & ULP
167          */
168         union tf_session_id   session_id;
169         /**
170          * Protects access to core_data. Lock is initialized and owned
171          * by ULP. TruFlow can access the core_data without checking
172          * the lock.
173          *
174          * Owner:  ULP
175          * Access: ULP
176          */
177         uint8_t               spin_lock;
178         /**
179          * The core_data holds the TruFlow tf_session data
180          * structure. This memory is allocated and owned by TruFlow on
181          * tf_open_session().
182          *
183          * TruFlow uses this memory for session management control
184          * until the session is closed by ULP. Access control is done
185          * by the spin_lock which ULP controls ahead of TruFlow API
186          * calls.
187          *
188          * Please see tf_open_session_parms for specification details
189          * on this variable.
190          *
191          * Owner:  TruFlow
192          * Access: TruFlow
193          */
194         void                 *core_data;
195         /**
196          * The core_data_sz_bytes specifies the size of core_data in
197          * bytes.
198          *
199          * The size is set by TruFlow on tf_open_session().
200          *
201          * Please see tf_open_session_parms for specification details
202          * on this variable.
203          *
204          * Owner:  TruFlow
205          * Access: TruFlow
206          */
207         uint32_t              core_data_sz_bytes;
208 };
209
210 /** TruFlow handle
211  *
212  * Contains a pointer to the session info. Allocated by ULP and passed
213  * to TruFlow using tf_open_session(). TruFlow will populate the
214  * session info at that time. Additional 'opens' can be done using
215  * same session_info by using tf_attach_session().
216  *
217  * It is expected that ULP allocates this memory as shared memory.
218  *
219  * NOTE: This struct must be within the BNXT PMD struct bnxt
220  *       (bp). This allows use of container_of() to get access to the PMD.
221  */
222 struct tf {
223         struct tf_session_info *session;
224 };
225
226
227 /**
228  * tf_open_session parameters definition.
229  */
230 struct tf_open_session_parms {
231         /** [in] ctrl_chan_name
232          *
233          * String containing name of control channel interface to be
234          * used for this session to communicate with firmware.
235          *
236          * The ctrl_chan_name can be looked up by using
237          * rte_eth_dev_get_name_by_port() within the ULP.
238          *
239          * ctrl_chan_name will be used as part of a name for any
240          * shared memory allocation.
241          */
242         char ctrl_chan_name[TF_SESSION_NAME_MAX];
243         /** [in] shadow_copy
244          *
245          * Boolean controlling the use and availability of shadow
246          * copy. Shadow copy will allow the TruFlow to keep track of
247          * resource content on the firmware side without having to
248          * query firmware. Additional private session core_data will
249          * be allocated if this boolean is set to 'true', default
250          * 'false'.
251          *
252          * Size of memory depends on the NVM Resource settings for the
253          * control channel.
254          */
255         bool shadow_copy;
256         /** [in/out] session_id
257          *
258          * Session_id is unique per session.
259          *
260          * Session_id is composed of domain, bus, device and
261          * fw_session_id. The construction is done by parsing the
262          * ctrl_chan_name together with allocation of a fw_session_id.
263          *
264          * The session_id allows a session to be shared between devices.
265          */
266         union tf_session_id session_id;
267         /** [in] device type
268          *
269          * Device type is passed, one of Wh+, Brd2, Brd3, Brd4
270          */
271         enum tf_device_type device_type;
272 };
273
274 /**
275  * Opens a new TruFlow management session.
276  *
277  * TruFlow will allocate session specific memory, shared memory, to
278  * hold its session data. This data is private to TruFlow.
279  *
280  * Multiple PFs can share the same session. An association, refcount,
281  * between session and PFs is maintained within TruFlow. Thus, a PF
282  * can attach to an existing session, see tf_attach_session().
283  *
284  * No other TruFlow APIs will succeed unless this API is first called and
285  * succeeds.
286  *
287  * tf_open_session() returns a session id that can be used on attach.
288  *
289  * [in] tfp
290  *   Pointer to TF handle
291  * [in] parms
292  *   Pointer to open parameters
293  *
294  * Returns
295  *   - (0) if successful.
296  *   - (-EINVAL) on failure.
297  */
298 int tf_open_session(struct tf *tfp,
299                     struct tf_open_session_parms *parms);
300
301 struct tf_attach_session_parms {
302         /** [in] ctrl_chan_name
303          *
304          * String containing name of control channel interface to be
305          * used for this session to communicate with firmware.
306          *
307          * The ctrl_chan_name can be looked up by using
308          * rte_eth_dev_get_name_by_port() within the ULP.
309          *
310          * ctrl_chan_name will be used as part of a name for any
311          * shared memory allocation.
312          */
313         char ctrl_chan_name[TF_SESSION_NAME_MAX];
314
315         /** [in] attach_chan_name
316          *
317          * String containing name of attach channel interface to be
318          * used for this session.
319          *
320          * The attach_chan_name must be given to a 2nd process after
321          * the primary process has been created. This is the
322          * ctrl_chan_name of the primary process and is used to find
323          * the shared memory for the session that the attach is going
324          * to use.
325          */
326         char attach_chan_name[TF_SESSION_NAME_MAX];
327
328         /** [in] session_id
329          *
330          * Session_id is unique per session. For Attach the session_id
331          * should be the session_id that was returned on the first
332          * open.
333          *
334          * Session_id is composed of domain, bus, device and
335          * fw_session_id. The construction is done by parsing the
336          * ctrl_chan_name together with allocation of a fw_session_id
337          * during tf_open_session().
338          *
339          * A reference count will be incremented on attach. A session
340          * is first fully closed when reference count is zero by
341          * calling tf_close_session().
342          */
343         union tf_session_id session_id;
344 };
345
346 /**
347  * Attaches to an existing session. Used when more than one PF wants
348  * to share a single session. In that case all TruFlow management
349  * traffic will be sent to the TruFlow firmware using the 'PF' that
350  * did the attach not the session ctrl channel.
351  *
352  * Attach will increment a ref count as to manage the shared session data.
353  *
354  * [in] tfp, pointer to TF handle
355  * [in] parms, pointer to attach parameters
356  *
357  * Returns
358  *   - (0) if successful.
359  *   - (-EINVAL) on failure.
360  */
361 int tf_attach_session(struct tf *tfp,
362                       struct tf_attach_session_parms *parms);
363
364 /**
365  * Closes an existing session. Cleans up all hardware and firmware
366  * state associated with the TruFlow application session when the last
367  * PF associated with the session results in refcount to be zero.
368  *
369  * Returns success or failure code.
370  */
371 int tf_close_session(struct tf *tfp);
372
373 /**
374  * @page  ident Identity Management
375  *
376  * @ref tf_alloc_identifier
377  *
378  * @ref tf_free_identifier
379  */
380 enum tf_identifier_type {
381         /** The L2 Context is returned from the L2 Ctxt TCAM lookup
382          *  and can be used in WC TCAM or EM keys to virtualize further
383          *  lookups.
384          */
385         TF_IDENT_TYPE_L2_CTXT,
386         /** The WC profile func is returned from the L2 Ctxt TCAM lookup
387          *  to enable virtualization of the profile TCAM.
388          */
389         TF_IDENT_TYPE_PROF_FUNC,
390         /** The WC profile ID is included in the WC lookup key
391          *  to enable virtualization of the WC TCAM hardware.
392          */
393         TF_IDENT_TYPE_WC_PROF,
394         /** The EM profile ID is included in the EM lookup key
395          *  to enable virtualization of the EM hardware. (not required for Brd4
396          *  as it has table scope)
397          */
398         TF_IDENT_TYPE_EM_PROF,
399         /** The L2 func is included in the ILT result and from recycling to
400          *  enable virtualization of further lookups.
401          */
402         TF_IDENT_TYPE_L2_FUNC
403 };
404
405 /** tf_alloc_identifier parameter definition
406  */
407 struct tf_alloc_identifier_parms {
408         /**
409          * [in]  receive or transmit direction
410          */
411         enum tf_dir dir;
412         /**
413          * [in] Identifier type
414          */
415         enum tf_identifier_type ident_type;
416         /**
417          * [out] Identifier allocated
418          */
419         uint16_t id;
420 };
421
422 /** tf_free_identifier parameter definition
423  */
424 struct tf_free_identifier_parms {
425         /**
426          * [in]  receive or transmit direction
427          */
428         enum tf_dir dir;
429         /**
430          * [in] Identifier type
431          */
432         enum tf_identifier_type ident_type;
433         /**
434          * [in] ID to free
435          */
436         uint16_t id;
437 };
438
439 /** allocate identifier resource
440  *
441  * TruFlow core will allocate a free id from the per identifier resource type
442  * pool reserved for the session during tf_open().  No firmware is involved.
443  *
444  * Returns success or failure code.
445  */
446 int tf_alloc_identifier(struct tf *tfp,
447                         struct tf_alloc_identifier_parms *parms);
448
449 /** free identifier resource
450  *
451  * TruFlow core will return an id back to the per identifier resource type pool
452  * reserved for the session.  No firmware is involved.  During tf_close, the
453  * complete pool is returned to the firmware.
454  *
455  * Returns success or failure code.
456  */
457 int tf_free_identifier(struct tf *tfp,
458                        struct tf_free_identifier_parms *parms);
459
460 /**
461  * TCAM table type
462  */
463 enum tf_tcam_tbl_type {
464         TF_TCAM_TBL_TYPE_L2_CTXT_TCAM,
465         TF_TCAM_TBL_TYPE_PROF_TCAM,
466         TF_TCAM_TBL_TYPE_WC_TCAM,
467         TF_TCAM_TBL_TYPE_SP_TCAM,
468         TF_TCAM_TBL_TYPE_CT_RULE_TCAM,
469         TF_TCAM_TBL_TYPE_VEB_TCAM,
470         TF_TCAM_TBL_TYPE_MAX
471
472 };
473
474 /**
475  * @page tcam TCAM Access
476  *
477  * @ref tf_alloc_tcam_entry
478  *
479  * @ref tf_set_tcam_entry
480  *
481  * @ref tf_get_tcam_entry
482  *
483  * @ref tf_free_tcam_entry
484  */
485
486 /** tf_alloc_tcam_entry parameter definition
487  */
488 struct tf_alloc_tcam_entry_parms {
489         /**
490          * [in] receive or transmit direction
491          */
492         enum tf_dir dir;
493         /**
494          * [in] TCAM table type
495          */
496         enum tf_tcam_tbl_type tcam_tbl_type;
497         /**
498          * [in] Enable search for matching entry
499          */
500         uint8_t search_enable;
501         /**
502          * [in] Key data to match on (if search)
503          */
504         uint8_t *key;
505         /**
506          * [in] key size in bits (if search)
507          */
508         uint16_t key_sz_in_bits;
509         /**
510          * [in] Mask data to match on (if search)
511          */
512         uint8_t *mask;
513         /**
514          * [in] Priority of entry requested (definition TBD)
515          */
516         uint32_t priority;
517         /**
518          * [out] If search, set if matching entry found
519          */
520         uint8_t hit;
521         /**
522          * [out] Current refcnt after allocation
523          */
524         uint16_t ref_cnt;
525         /**
526          * [out] Idx allocated
527          *
528          */
529         uint16_t idx;
530 };
531
532 /** allocate TCAM entry
533  *
534  * Allocate a TCAM entry - one of these types:
535  *
536  * L2 Context
537  * Profile TCAM
538  * WC TCAM
539  * VEB TCAM
540  *
541  * This function allocates a TCAM table record.  This function
542  * will attempt to allocate a TCAM table entry from the session
543  * owned TCAM entries or search a shadow copy of the TCAM table for a
544  * matching entry if search is enabled.  Key, mask and result must match for
545  * hit to be set.  Only TruFlow core data is accessed.
546  * A hash table to entry mapping is maintained for search purposes.  If
547  * search is not enabled, the first available free entry is returned based
548  * on priority and alloc_cnt is set to 1.  If search is enabled and a matching
549  * entry to entry_data is found, hit is set to TRUE and alloc_cnt is set to 1.
550  * RefCnt is also returned.
551  *
552  * Also returns success or failure code.
553  */
554 int tf_alloc_tcam_entry(struct tf *tfp,
555                         struct tf_alloc_tcam_entry_parms *parms);
556
557 /** tf_set_tcam_entry parameter definition
558  */
559 struct  tf_set_tcam_entry_parms {
560         /**
561          * [in] receive or transmit direction
562          */
563         enum tf_dir dir;
564         /**
565          * [in] TCAM table type
566          */
567         enum tf_tcam_tbl_type tcam_tbl_type;
568         /**
569          * [in] base index of the entry to program
570          */
571         uint16_t idx;
572         /**
573          * [in] struct containing key
574          */
575         uint8_t *key;
576         /**
577          * [in] struct containing mask fields
578          */
579         uint8_t *mask;
580         /**
581          * [in] key size in bits (if search)
582          */
583         uint16_t key_sz_in_bits;
584         /**
585          * [in] struct containing result
586          */
587         uint8_t *result;
588         /**
589          * [in] struct containing result size in bits
590          */
591         uint16_t result_sz_in_bits;
592 };
593
594 /** set TCAM entry
595  *
596  * Program a TCAM table entry for a TruFlow session.
597  *
598  * If the entry has not been allocated, an error will be returned.
599  *
600  * Returns success or failure code.
601  */
602 int tf_set_tcam_entry(struct tf *tfp,
603                       struct tf_set_tcam_entry_parms *parms);
604
605 /** tf_get_tcam_entry parameter definition
606  */
607 struct tf_get_tcam_entry_parms {
608         /**
609          * [in] receive or transmit direction
610          */
611         enum tf_dir dir;
612         /**
613          * [in] TCAM table type
614          */
615         enum tf_tcam_tbl_type  tcam_tbl_type;
616         /**
617          * [in] index of the entry to get
618          */
619         uint16_t idx;
620         /**
621          * [out] struct containing key
622          */
623         uint8_t *key;
624         /**
625          * [out] struct containing mask fields
626          */
627         uint8_t *mask;
628         /**
629          * [out] key size in bits
630          */
631         uint16_t key_sz_in_bits;
632         /**
633          * [out] struct containing result
634          */
635         uint8_t *result;
636         /**
637          * [out] struct containing result size in bits
638          */
639         uint16_t result_sz_in_bits;
640 };
641
642 /** get TCAM entry
643  *
644  * Program a TCAM table entry for a TruFlow session.
645  *
646  * If the entry has not been allocated, an error will be returned.
647  *
648  * Returns success or failure code.
649  */
650 int tf_get_tcam_entry(struct tf *tfp,
651                       struct tf_get_tcam_entry_parms *parms);
652
653 /** tf_free_tcam_entry parameter definition
654  */
655 struct tf_free_tcam_entry_parms {
656         /**
657          * [in] receive or transmit direction
658          */
659         enum tf_dir dir;
660         /**
661          * [in] TCAM table type
662          */
663         enum tf_tcam_tbl_type tcam_tbl_type;
664         /**
665          * [in] Index to free
666          */
667         uint16_t idx;
668         /**
669          * [out] reference count after free
670          */
671         uint16_t ref_cnt;
672 };
673
674 /** free TCAM entry
675  *
676  * Free TCAM entry.
677  *
678  * Firmware checks to ensure the TCAM entries are owned by the TruFlow
679  * session.  TCAM entry will be invalidated.  All-ones mask.
680  * writes to hw.
681  *
682  * WCTCAM profile id of 0 must be used to invalidate an entry.
683  *
684  * Returns success or failure code.
685  */
686 int tf_free_tcam_entry(struct tf *tfp,
687                        struct tf_free_tcam_entry_parms *parms);
688
689 /**
690  * @page table Table Access
691  *
692  * @ref tf_alloc_tbl_entry
693  *
694  * @ref tf_free_tbl_entry
695  *
696  * @ref tf_set_tbl_entry
697  *
698  * @ref tf_get_tbl_entry
699  */
700
701 /**
702  * Enumeration of TruFlow table types. A table type is used to identify a
703  * resource object.
704  *
705  * NOTE: The table type TF_TBL_TYPE_EXT is unique in that it is
706  * the only table type that is connected with a table scope.
707  */
708 enum tf_tbl_type {
709         /** Wh+/Brd2 Action Record */
710         TF_TBL_TYPE_FULL_ACT_RECORD,
711         /** Multicast Groups */
712         TF_TBL_TYPE_MCAST_GROUPS,
713         /** Action Encap 8 Bytes */
714         TF_TBL_TYPE_ACT_ENCAP_8B,
715         /** Action Encap 16 Bytes */
716         TF_TBL_TYPE_ACT_ENCAP_16B,
717         /** Action Encap 64 Bytes */
718         TF_TBL_TYPE_ACT_ENCAP_32B,
719         /** Action Encap 64 Bytes */
720         TF_TBL_TYPE_ACT_ENCAP_64B,
721         /** Action Source Properties SMAC */
722         TF_TBL_TYPE_ACT_SP_SMAC,
723         /** Action Source Properties SMAC IPv4 */
724         TF_TBL_TYPE_ACT_SP_SMAC_IPV4,
725         /** Action Source Properties SMAC IPv6 */
726         TF_TBL_TYPE_ACT_SP_SMAC_IPV6,
727         /** Action Statistics 64 Bits */
728         TF_TBL_TYPE_ACT_STATS_64,
729         /** Action Modify L4 Src Port */
730         TF_TBL_TYPE_ACT_MODIFY_SPORT,
731         /** Action Modify L4 Dest Port */
732         TF_TBL_TYPE_ACT_MODIFY_DPORT,
733         /** Action Modify IPv4 Source */
734         TF_TBL_TYPE_ACT_MODIFY_IPV4_SRC,
735         /** Action _Modify L4 Dest Port */
736         TF_TBL_TYPE_ACT_MODIFY_IPV4_DEST,
737         /** Action Modify IPv6 Source */
738         TF_TBL_TYPE_ACT_MODIFY_IPV6_SRC,
739         /** Action Modify IPv6 Destination */
740         TF_TBL_TYPE_ACT_MODIFY_IPV6_DEST,
741
742         /* HW */
743
744         /** Meter Profiles */
745         TF_TBL_TYPE_METER_PROF,
746         /** Meter Instance */
747         TF_TBL_TYPE_METER_INST,
748         /** Mirror Config */
749         TF_TBL_TYPE_MIRROR_CONFIG,
750         /** UPAR */
751         TF_TBL_TYPE_UPAR,
752         /** Brd4 Epoch 0 table */
753         TF_TBL_TYPE_EPOCH0,
754         /** Brd4 Epoch 1 table  */
755         TF_TBL_TYPE_EPOCH1,
756         /** Brd4 Metadata  */
757         TF_TBL_TYPE_METADATA,
758         /** Brd4 CT State  */
759         TF_TBL_TYPE_CT_STATE,
760         /** Brd4 Range Profile  */
761         TF_TBL_TYPE_RANGE_PROF,
762         /** Brd4 Range Entry  */
763         TF_TBL_TYPE_RANGE_ENTRY,
764         /** Brd4 LAG Entry  */
765         TF_TBL_TYPE_LAG,
766         /** Brd4 only VNIC/SVIF Table */
767         TF_TBL_TYPE_VNIC_SVIF,
768
769         /* External */
770
771         /** External table type - initially 1 poolsize entries.
772          * All External table types are associated with a table
773          * scope. Internal types are not.
774          */
775         TF_TBL_TYPE_EXT,
776         /** Future - external pool of size0 entries */
777         TF_TBL_TYPE_EXT_0,
778         TF_TBL_TYPE_MAX
779 };
780 #endif /* _TF_CORE_H_ */