e898f19a013863f49ef7eabd11e3df0b05f2fc9d
[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 #include "hcapi/hcapi_cfa.h"
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 #define TF_KILOBYTE  1024
26 #define TF_MEGABYTE  (1024 * 1024)
27
28 /**
29  * direction
30  */
31 enum tf_dir {
32         TF_DIR_RX,  /**< Receive */
33         TF_DIR_TX,  /**< Transmit */
34         TF_DIR_MAX
35 };
36
37 /**
38  * memory choice
39  */
40 enum tf_mem {
41         TF_MEM_INTERNAL, /**< Internal */
42         TF_MEM_EXTERNAL, /**< External */
43         TF_MEM_MAX
44 };
45
46 /**
47  * EEM record AR helper
48  *
49  * Helper to handle the Action Record Pointer in the EEM Record Entry.
50  *
51  * Convert absolute offset to action record pointer in EEM record entry
52  * Convert action record pointer in EEM record entry to absolute offset
53  */
54 #define TF_ACT_REC_OFFSET_2_PTR(offset) ((offset) >> 4)
55 #define TF_ACT_REC_PTR_2_OFFSET(offset) ((offset) << 4)
56
57
58 /*
59  * Helper Macros
60  */
61 #define TF_BITS_2_BYTES(num_bits) (((num_bits) + 7) / 8)
62
63 /********** BEGIN API FUNCTION PROTOTYPES/PARAMETERS **********/
64
65 /**
66  * @page general General
67  *
68  * @ref tf_open_session
69  *
70  * @ref tf_attach_session
71  *
72  * @ref tf_close_session
73  */
74
75
76 /**
77  * Session Version defines
78  *
79  * The version controls the format of the tf_session and
80  * tf_session_info structure. This is to assure upgrade between
81  * versions can be supported.
82  */
83 #define TF_SESSION_VER_MAJOR  1   /**< Major Version */
84 #define TF_SESSION_VER_MINOR  0   /**< Minor Version */
85 #define TF_SESSION_VER_UPDATE 0   /**< Update Version */
86
87 /**
88  * Session Name
89  *
90  * Name of the TruFlow control channel interface.  Expects
91  * format to be RTE Name specific, i.e. rte_eth_dev_get_name_by_port()
92  */
93 #define TF_SESSION_NAME_MAX       64
94
95 #define TF_FW_SESSION_ID_INVALID  0xFF  /**< Invalid FW Session ID define */
96
97 /**
98  * Session Identifier
99  *
100  * Unique session identifier which includes PCIe bus info to
101  * distinguish the PF and session info to identify the associated
102  * TruFlow session. Session ID is constructed from the passed in
103  * ctrl_chan_name in tf_open_session() together with an allocated
104  * fw_session_id. Done by TruFlow on tf_open_session().
105  */
106 union tf_session_id {
107         uint32_t id;
108         struct {
109                 uint8_t domain;
110                 uint8_t bus;
111                 uint8_t device;
112                 uint8_t fw_session_id;
113         } internal;
114 };
115
116 /**
117  * Session Version
118  *
119  * The version controls the format of the tf_session and
120  * tf_session_info structure. This is to assure upgrade between
121  * versions can be supported.
122  *
123  * Please see the TF_VER_MAJOR/MINOR and UPDATE defines.
124  */
125 struct tf_session_version {
126         uint8_t major;
127         uint8_t minor;
128         uint8_t update;
129 };
130
131 /**
132  * Session supported device types
133  */
134 enum tf_device_type {
135         TF_DEVICE_TYPE_WH = 0, /**< Whitney+  */
136         TF_DEVICE_TYPE_SR,     /**< Stingray  */
137         TF_DEVICE_TYPE_THOR,   /**< Thor      */
138         TF_DEVICE_TYPE_SR2,    /**< Stingray2 */
139         TF_DEVICE_TYPE_MAX     /**< Maximum   */
140 };
141
142 /**
143  * Identifier resource types
144  */
145 enum tf_identifier_type {
146         /**
147          *  The L2 Context is returned from the L2 Ctxt TCAM lookup
148          *  and can be used in WC TCAM or EM keys to virtualize further
149          *  lookups.
150          */
151         TF_IDENT_TYPE_L2_CTXT,
152         /**
153          *  The WC profile func is returned from the L2 Ctxt TCAM lookup
154          *  to enable virtualization of the profile TCAM.
155          */
156         TF_IDENT_TYPE_PROF_FUNC,
157         /**
158          *  The WC profile ID is included in the WC lookup key
159          *  to enable virtualization of the WC TCAM hardware.
160          */
161         TF_IDENT_TYPE_WC_PROF,
162         /**
163          *  The EM profile ID is included in the EM lookup key
164          *  to enable virtualization of the EM hardware. (not required for SR2
165          *  as it has table scope)
166          */
167         TF_IDENT_TYPE_EM_PROF,
168         /**
169          *  The L2 func is included in the ILT result and from recycling to
170          *  enable virtualization of further lookups.
171          */
172         TF_IDENT_TYPE_L2_FUNC,
173         TF_IDENT_TYPE_MAX
174 };
175
176 /**
177  * Enumeration of TruFlow table types. A table type is used to identify a
178  * resource object.
179  *
180  * NOTE: The table type TF_TBL_TYPE_EXT is unique in that it is
181  * the only table type that is connected with a table scope.
182  */
183 enum tf_tbl_type {
184         /* Internal */
185
186         /** Wh+/SR Action Record */
187         TF_TBL_TYPE_FULL_ACT_RECORD,
188         /** Wh+/SR/Th Multicast Groups */
189         TF_TBL_TYPE_MCAST_GROUPS,
190         /** Wh+/SR Action Encap 8 Bytes */
191         TF_TBL_TYPE_ACT_ENCAP_8B,
192         /** Wh+/SR Action Encap 16 Bytes */
193         TF_TBL_TYPE_ACT_ENCAP_16B,
194         /** Action Encap 32 Bytes */
195         TF_TBL_TYPE_ACT_ENCAP_32B,
196         /** Wh+/SR Action Encap 64 Bytes */
197         TF_TBL_TYPE_ACT_ENCAP_64B,
198         /** Action Source Properties SMAC */
199         TF_TBL_TYPE_ACT_SP_SMAC,
200         /** Wh+/SR Action Source Properties SMAC IPv4 */
201         TF_TBL_TYPE_ACT_SP_SMAC_IPV4,
202         /** Action Source Properties SMAC IPv6 */
203         TF_TBL_TYPE_ACT_SP_SMAC_IPV6,
204         /** Wh+/SR Action Statistics 64 Bits */
205         TF_TBL_TYPE_ACT_STATS_64,
206         /** Wh+/SR Action Modify L4 Src Port */
207         TF_TBL_TYPE_ACT_MODIFY_SPORT,
208         /** Wh+/SR Action Modify L4 Dest Port */
209         TF_TBL_TYPE_ACT_MODIFY_DPORT,
210         /** Wh+/SR Action Modify IPv4 Source */
211         TF_TBL_TYPE_ACT_MODIFY_IPV4_SRC,
212         /** Wh+/SR Action _Modify L4 Dest Port */
213         TF_TBL_TYPE_ACT_MODIFY_IPV4_DEST,
214         /** Meter Profiles */
215         TF_TBL_TYPE_METER_PROF,
216         /** Meter Instance */
217         TF_TBL_TYPE_METER_INST,
218         /** Mirror Config */
219         TF_TBL_TYPE_MIRROR_CONFIG,
220         /** UPAR */
221         TF_TBL_TYPE_UPAR,
222         /** SR2 Epoch 0 table */
223         TF_TBL_TYPE_EPOCH0,
224         /** SR2 Epoch 1 table  */
225         TF_TBL_TYPE_EPOCH1,
226         /** SR2 Metadata  */
227         TF_TBL_TYPE_METADATA,
228         /** SR2 CT State  */
229         TF_TBL_TYPE_CT_STATE,
230         /** SR2 Range Profile  */
231         TF_TBL_TYPE_RANGE_PROF,
232         /** SR2 Range Entry  */
233         TF_TBL_TYPE_RANGE_ENTRY,
234         /** SR2 LAG Entry  */
235         TF_TBL_TYPE_LAG,
236         /** SR2 VNIC/SVIF Table */
237         TF_TBL_TYPE_VNIC_SVIF,
238         /** Th/SR2 EM Flexible Key builder */
239         TF_TBL_TYPE_EM_FKB,
240         /** Th/SR2 WC Flexible Key builder */
241         TF_TBL_TYPE_WC_FKB,
242
243         /* External */
244
245         /**
246          * External table type - initially 1 poolsize entries.
247          * All External table types are associated with a table
248          * scope. Internal types are not.
249          */
250         TF_TBL_TYPE_EXT,
251         TF_TBL_TYPE_MAX
252 };
253
254 /**
255  * TCAM table type
256  */
257 enum tf_tcam_tbl_type {
258         /** L2 Context TCAM */
259         TF_TCAM_TBL_TYPE_L2_CTXT_TCAM,
260         /** Profile TCAM */
261         TF_TCAM_TBL_TYPE_PROF_TCAM,
262         /** Wildcard TCAM */
263         TF_TCAM_TBL_TYPE_WC_TCAM,
264         /** Source Properties TCAM */
265         TF_TCAM_TBL_TYPE_SP_TCAM,
266         /** Connection Tracking Rule TCAM */
267         TF_TCAM_TBL_TYPE_CT_RULE_TCAM,
268         /** Virtual Edge Bridge TCAM */
269         TF_TCAM_TBL_TYPE_VEB_TCAM,
270         TF_TCAM_TBL_TYPE_MAX
271 };
272
273 /**
274  * EM Resources
275  * These defines are provisioned during
276  * tf_open_session()
277  */
278 enum tf_em_tbl_type {
279         /** The number of internal EM records for the session */
280         TF_EM_TBL_TYPE_EM_RECORD,
281         /** The number of table scopes reequested */
282         TF_EM_TBL_TYPE_TBL_SCOPE,
283         TF_EM_TBL_TYPE_MAX
284 };
285
286 /**
287  * TruFlow Session Information
288  *
289  * Structure defining a TruFlow Session, also known as a Management
290  * session. This structure is initialized at time of
291  * tf_open_session(). It is passed to all of the TruFlow APIs as way
292  * to prescribe and isolate resources between different TruFlow ULP
293  * Applications.
294  *
295  * Ownership of the elements is split between ULP and TruFlow. Please
296  * see the individual elements.
297  */
298 struct tf_session_info {
299         /**
300          * TrueFlow Version. Used to control the structure layout when
301          * sharing sessions. No guarantee that a secondary process
302          * would come from the same version of an executable.
303          * TruFlow initializes this variable on tf_open_session().
304          *
305          * Owner:  TruFlow
306          * Access: TruFlow
307          */
308         struct tf_session_version ver;
309         /**
310          * will be STAILQ_ENTRY(tf_session_info) next
311          *
312          * Owner:  ULP
313          * Access: ULP
314          */
315         void                 *next;
316         /**
317          * Session ID is a unique identifier for the session. TruFlow
318          * initializes this variable during tf_open_session()
319          * processing.
320          *
321          * Owner:  TruFlow
322          * Access: Truflow & ULP
323          */
324         union tf_session_id   session_id;
325         /**
326          * Protects access to core_data. Lock is initialized and owned
327          * by ULP. TruFlow can access the core_data without checking
328          * the lock.
329          *
330          * Owner:  ULP
331          * Access: ULP
332          */
333         uint8_t               spin_lock;
334         /**
335          * The core_data holds the TruFlow tf_session data
336          * structure. This memory is allocated and owned by TruFlow on
337          * tf_open_session().
338          *
339          * TruFlow uses this memory for session management control
340          * until the session is closed by ULP. Access control is done
341          * by the spin_lock which ULP controls ahead of TruFlow API
342          * calls.
343          *
344          * Please see tf_open_session_parms for specification details
345          * on this variable.
346          *
347          * Owner:  TruFlow
348          * Access: TruFlow
349          */
350         void                 *core_data;
351         /**
352          * The core_data_sz_bytes specifies the size of core_data in
353          * bytes.
354          *
355          * The size is set by TruFlow on tf_open_session().
356          *
357          * Please see tf_open_session_parms for specification details
358          * on this variable.
359          *
360          * Owner:  TruFlow
361          * Access: TruFlow
362          */
363         uint32_t              core_data_sz_bytes;
364 };
365
366 /**
367  * TruFlow handle
368  *
369  * Contains a pointer to the session info. Allocated by ULP and passed
370  * to TruFlow using tf_open_session(). TruFlow will populate the
371  * session info at that time. Additional 'opens' can be done using
372  * same session_info by using tf_attach_session().
373  *
374  * It is expected that ULP allocates this memory as shared memory.
375  *
376  * NOTE: This struct must be within the BNXT PMD struct bnxt
377  *       (bp). This allows use of container_of() to get access to the PMD.
378  */
379 struct tf {
380         struct tf_session_info *session;
381 };
382
383 /**
384  * Identifier resource definition
385  */
386 struct tf_identifier_resources {
387         /**
388          * Array of TF Identifiers where each entry is expected to be
389          * set to the requested resource number of that specific type.
390          * The index used is tf_identifier_type.
391          */
392         uint16_t cnt[TF_IDENT_TYPE_MAX];
393 };
394
395 /**
396  * Table type resource definition
397  */
398 struct tf_tbl_resources {
399         /**
400          * Array of TF Table types where each entry is expected to be
401          * set to the requeste resource number of that specific
402          * type. The index used is tf_tbl_type.
403          */
404         uint16_t cnt[TF_TBL_TYPE_MAX];
405 };
406
407 /**
408  * TCAM type resource definition
409  */
410 struct tf_tcam_resources {
411         /**
412          * Array of TF TCAM types where each entry is expected to be
413          * set to the requested resource number of that specific
414          * type. The index used is tf_tcam_tbl_type.
415          */
416         uint16_t cnt[TF_TCAM_TBL_TYPE_MAX];
417 };
418
419 /**
420  * EM type resource definition
421  */
422 struct tf_em_resources {
423         /**
424          * Array of TF EM table types where each entry is expected to
425          * be set to the requested resource number of that specific
426          * type. The index used is tf_em_tbl_type.
427          */
428         uint16_t cnt[TF_EM_TBL_TYPE_MAX];
429 };
430
431 /**
432  * tf_session_resources parameter definition.
433  */
434 struct tf_session_resources {
435         /**
436          * [in] Requested Identifier Resources
437          *
438          * Number of identifier resources requested for the
439          * session.
440          */
441         struct tf_identifier_resources ident_cnt[TF_DIR_MAX];
442         /**
443          * [in] Requested Index Table resource counts
444          *
445          * The number of index table resources requested for the
446          * session.
447          */
448         struct tf_tbl_resources tbl_cnt[TF_DIR_MAX];
449         /**
450          * [in] Requested TCAM Table resource counts
451          *
452          * The number of TCAM table resources requested for the
453          * session.
454          */
455
456         struct tf_tcam_resources tcam_cnt[TF_DIR_MAX];
457         /**
458          * [in] Requested EM resource counts
459          *
460          * The number of internal EM table resources requested for the
461          * session.
462          */
463         struct tf_em_resources em_cnt[TF_DIR_MAX];
464 };
465
466 /**
467  * tf_open_session parameters definition.
468  */
469 struct tf_open_session_parms {
470         /**
471          * [in] ctrl_chan_name
472          *
473          * String containing name of control channel interface to be
474          * used for this session to communicate with firmware.
475          *
476          * The ctrl_chan_name can be looked up by using
477          * rte_eth_dev_get_name_by_port() within the ULP.
478          *
479          * ctrl_chan_name will be used as part of a name for any
480          * shared memory allocation.
481          */
482         char ctrl_chan_name[TF_SESSION_NAME_MAX];
483         /**
484          * [in] shadow_copy
485          *
486          * Boolean controlling the use and availability of shadow
487          * copy. Shadow copy will allow the TruFlow to keep track of
488          * resource content on the firmware side without having to
489          * query firmware. Additional private session core_data will
490          * be allocated if this boolean is set to 'true', default
491          * 'false'.
492          *
493          * Size of memory depends on the NVM Resource settings for the
494          * control channel.
495          */
496         bool shadow_copy;
497         /**
498          * [in/out] session_id
499          *
500          * Session_id is unique per session.
501          *
502          * Session_id is composed of domain, bus, device and
503          * fw_session_id. The construction is done by parsing the
504          * ctrl_chan_name together with allocation of a fw_session_id.
505          *
506          * The session_id allows a session to be shared between devices.
507          */
508         union tf_session_id session_id;
509         /**
510          * [in] device type
511          *
512          * Device type is passed, one of Wh+, SR, Thor, SR2
513          */
514         enum tf_device_type device_type;
515         /** [in] resources
516          *
517          * Resource allocation
518          */
519         struct tf_session_resources resources;
520 };
521
522 /**
523  * Opens a new TruFlow management session.
524  *
525  * TruFlow will allocate session specific memory, shared memory, to
526  * hold its session data. This data is private to TruFlow.
527  *
528  * Multiple PFs can share the same session. An association, refcount,
529  * between session and PFs is maintained within TruFlow. Thus, a PF
530  * can attach to an existing session, see tf_attach_session().
531  *
532  * No other TruFlow APIs will succeed unless this API is first called and
533  * succeeds.
534  *
535  * tf_open_session() returns a session id that can be used on attach.
536  *
537  * [in] tfp
538  *   Pointer to TF handle
539  * [in] parms
540  *   Pointer to open parameters
541  *
542  * Returns
543  *   - (0) if successful.
544  *   - (-EINVAL) on failure.
545  */
546 int tf_open_session(struct tf *tfp,
547                     struct tf_open_session_parms *parms);
548
549 struct tf_attach_session_parms {
550         /**
551          * [in] ctrl_chan_name
552          *
553          * String containing name of control channel interface to be
554          * used for this session to communicate with firmware.
555          *
556          * The ctrl_chan_name can be looked up by using
557          * rte_eth_dev_get_name_by_port() within the ULP.
558          *
559          * ctrl_chan_name will be used as part of a name for any
560          * shared memory allocation.
561          */
562         char ctrl_chan_name[TF_SESSION_NAME_MAX];
563
564         /**
565          * [in] attach_chan_name
566          *
567          * String containing name of attach channel interface to be
568          * used for this session.
569          *
570          * The attach_chan_name must be given to a 2nd process after
571          * the primary process has been created. This is the
572          * ctrl_chan_name of the primary process and is used to find
573          * the shared memory for the session that the attach is going
574          * to use.
575          */
576         char attach_chan_name[TF_SESSION_NAME_MAX];
577
578         /**
579          * [in] session_id
580          *
581          * Session_id is unique per session. For Attach the session_id
582          * should be the session_id that was returned on the first
583          * open.
584          *
585          * Session_id is composed of domain, bus, device and
586          * fw_session_id. The construction is done by parsing the
587          * ctrl_chan_name together with allocation of a fw_session_id
588          * during tf_open_session().
589          *
590          * A reference count will be incremented on attach. A session
591          * is first fully closed when reference count is zero by
592          * calling tf_close_session().
593          */
594         union tf_session_id session_id;
595 };
596
597 /**
598  * Attaches to an existing session. Used when more than one PF wants
599  * to share a single session. In that case all TruFlow management
600  * traffic will be sent to the TruFlow firmware using the 'PF' that
601  * did the attach not the session ctrl channel.
602  *
603  * Attach will increment a ref count as to manage the shared session data.
604  *
605  * [in] tfp, pointer to TF handle
606  * [in] parms, pointer to attach parameters
607  *
608  * Returns
609  *   - (0) if successful.
610  *   - (-EINVAL) on failure.
611  */
612 int tf_attach_session(struct tf *tfp,
613                       struct tf_attach_session_parms *parms);
614
615 /**
616  * Closes an existing session. Cleans up all hardware and firmware
617  * state associated with the TruFlow application session when the last
618  * PF associated with the session results in refcount to be zero.
619  *
620  * Returns success or failure code.
621  */
622 int tf_close_session(struct tf *tfp);
623
624 /**
625  * @page  ident Identity Management
626  *
627  * @ref tf_alloc_identifier
628  *
629  * @ref tf_free_identifier
630  */
631 /**
632  * tf_alloc_identifier parameter definition
633  */
634 struct tf_alloc_identifier_parms {
635         /**
636          * [in]  receive or transmit direction
637          */
638         enum tf_dir dir;
639         /**
640          * [in] Identifier type
641          */
642         enum tf_identifier_type ident_type;
643         /**
644          * [out] Identifier allocated
645          */
646         uint16_t id;
647 };
648
649 /**
650  * tf_free_identifier parameter definition
651  */
652 struct tf_free_identifier_parms {
653         /**
654          * [in]  receive or transmit direction
655          */
656         enum tf_dir dir;
657         /**
658          * [in] Identifier type
659          */
660         enum tf_identifier_type ident_type;
661         /**
662          * [in] ID to free
663          */
664         uint16_t id;
665 };
666
667 /**
668  * allocate identifier resource
669  *
670  * TruFlow core will allocate a free id from the per identifier resource type
671  * pool reserved for the session during tf_open().  No firmware is involved.
672  *
673  * Returns success or failure code.
674  */
675 int tf_alloc_identifier(struct tf *tfp,
676                         struct tf_alloc_identifier_parms *parms);
677
678 /**
679  * free identifier resource
680  *
681  * TruFlow core will return an id back to the per identifier resource type pool
682  * reserved for the session.  No firmware is involved.  During tf_close, the
683  * complete pool is returned to the firmware.
684  *
685  * Returns success or failure code.
686  */
687 int tf_free_identifier(struct tf *tfp,
688                        struct tf_free_identifier_parms *parms);
689
690 /**
691  * @page dram_table DRAM Table Scope Interface
692  *
693  * @ref tf_alloc_tbl_scope
694  *
695  * @ref tf_free_tbl_scope
696  *
697  * If we allocate the EEM memory from the core, we need to store it in
698  * the shared session data structure to make sure it can be freed later.
699  * (for example if the PF goes away)
700  *
701  * Current thought is that memory is allocated within core.
702  */
703
704
705 /**
706  * tf_alloc_tbl_scope_parms definition
707  */
708 struct tf_alloc_tbl_scope_parms {
709         /**
710          * [in] All Maximum key size required.
711          */
712         uint16_t rx_max_key_sz_in_bits;
713         /**
714          * [in] Maximum Action size required (includes inlined items)
715          */
716         uint16_t rx_max_action_entry_sz_in_bits;
717         /**
718          * [in] Memory size in Megabytes
719          * Total memory size allocated by user to be divided
720          * up for actions, hash, counters.  Only inline external actions.
721          * Use this variable or the number of flows, do not set both.
722          */
723         uint32_t rx_mem_size_in_mb;
724         /**
725          * [in] Number of flows * 1000. If set, rx_mem_size_in_mb must equal 0.
726          */
727         uint32_t rx_num_flows_in_k;
728         /**
729          * [in] SR2 only receive table access interface id
730          */
731         uint32_t rx_tbl_if_id;
732         /**
733          * [in] All Maximum key size required.
734          */
735         uint16_t tx_max_key_sz_in_bits;
736         /**
737          * [in] Maximum Action size required (includes inlined items)
738          */
739         uint16_t tx_max_action_entry_sz_in_bits;
740         /**
741          * [in] Memory size in Megabytes
742          * Total memory size allocated by user to be divided
743          * up for actions, hash, counters.  Only inline external actions.
744          */
745         uint32_t tx_mem_size_in_mb;
746         /**
747          * [in] Number of flows * 1000
748          */
749         uint32_t tx_num_flows_in_k;
750         /**
751          * [in] SR2 only receive table access interface id
752          */
753         uint32_t tx_tbl_if_id;
754         /**
755          * [in] Flush pending HW cached flows every 1/10th of value
756          * set in seconds, both idle and active flows are flushed
757          * from the HW cache. If set to 0, this feature will be disabled.
758          */
759         uint8_t hw_flow_cache_flush_timer;
760         /**
761          * [out] table scope identifier
762          */
763         uint32_t tbl_scope_id;
764 };
765
766 struct tf_free_tbl_scope_parms {
767         /**
768          * [in] table scope identifier
769          */
770         uint32_t tbl_scope_id;
771 };
772
773 /**
774  * allocate a table scope
775  *
776  * On SR2 Firmware will allocate a scope ID.  On other devices, the scope
777  * is a software construct to identify an EEM table.  This function will
778  * divide the hash memory/buckets and records according to the device
779  * device constraints based upon calculations using either the number of flows
780  * requested or the size of memory indicated.  Other parameters passed in
781  * determine the configuration (maximum key size, maximum external action record
782  * size.
783  *
784  * This API will allocate the table region in
785  * DRAM, program the PTU page table entries, and program the number of static
786  * buckets (if SR2) in the RX and TX CFAs.  Buckets are assumed to start at
787  * 0 in the EM memory for the scope.  Upon successful completion of this API,
788  * hash tables are fully initialized and ready for entries to be inserted.
789  *
790  * A single API is used to allocate a common table scope identifier in both
791  * receive and transmit CFA. The scope identifier is common due to nature of
792  * connection tracking sending notifications between RX and TX direction.
793  *
794  * The receive and transmit table access identifiers specify which rings will
795  * be used to initialize table DRAM.  The application must ensure mutual
796  * exclusivity of ring usage for table scope allocation and any table update
797  * operations.
798  *
799  * The hash table buckets, EM keys, and EM lookup results are stored in the
800  * memory allocated based on the rx_em_hash_mb/tx_em_hash_mb parameters.  The
801  * hash table buckets are stored at the beginning of that memory.
802  *
803  * NOTE:  No EM internal setup is done here. On chip EM records are managed
804  * internally by TruFlow core.
805  *
806  * Returns success or failure code.
807  */
808 int tf_alloc_tbl_scope(struct tf *tfp,
809                        struct tf_alloc_tbl_scope_parms *parms);
810
811
812 /**
813  * free a table scope
814  *
815  * Firmware checks that the table scope ID is owned by the TruFlow
816  * session, verifies that no references to this table scope remains
817  * (SR2 ILT) or Profile TCAM entries for either CFA (RX/TX) direction,
818  * then frees the table scope ID.
819  *
820  * Returns success or failure code.
821  */
822 int tf_free_tbl_scope(struct tf *tfp,
823                       struct tf_free_tbl_scope_parms *parms);
824
825 /**
826  * @page tcam TCAM Access
827  *
828  * @ref tf_alloc_tcam_entry
829  *
830  * @ref tf_set_tcam_entry
831  *
832  * @ref tf_get_tcam_entry
833  *
834  * @ref tf_free_tcam_entry
835  */
836
837
838 /**
839  * tf_alloc_tcam_entry parameter definition
840  */
841 struct tf_alloc_tcam_entry_parms {
842         /**
843          * [in] receive or transmit direction
844          */
845         enum tf_dir dir;
846         /**
847          * [in] TCAM table type
848          */
849         enum tf_tcam_tbl_type tcam_tbl_type;
850         /**
851          * [in] Enable search for matching entry
852          */
853         uint8_t search_enable;
854         /**
855          * [in] Key data to match on (if search)
856          */
857         uint8_t *key;
858         /**
859          * [in] key size in bits (if search)
860          */
861         uint16_t key_sz_in_bits;
862         /**
863          * [in] Mask data to match on (if search)
864          */
865         uint8_t *mask;
866         /**
867          * [in] Priority of entry requested (definition TBD)
868          */
869         uint32_t priority;
870         /**
871          * [out] If search, set if matching entry found
872          */
873         uint8_t hit;
874         /**
875          * [out] Current refcnt after allocation
876          */
877         uint16_t ref_cnt;
878         /**
879          * [out] Idx allocated
880          *
881          */
882         uint16_t idx;
883 };
884
885 /**
886  * allocate TCAM entry
887  *
888  * Allocate a TCAM entry - one of these types:
889  *
890  * L2 Context
891  * Profile TCAM
892  * WC TCAM
893  * VEB TCAM
894  *
895  * This function allocates a TCAM table record.  This function
896  * will attempt to allocate a TCAM table entry from the session
897  * owned TCAM entries or search a shadow copy of the TCAM table for a
898  * matching entry if search is enabled.  Key, mask and result must match for
899  * hit to be set.  Only TruFlow core data is accessed.
900  * A hash table to entry mapping is maintained for search purposes.  If
901  * search is not enabled, the first available free entry is returned based
902  * on priority and alloc_cnt is set to 1.  If search is enabled and a matching
903  * entry to entry_data is found, hit is set to TRUE and alloc_cnt is set to 1.
904  * RefCnt is also returned.
905  *
906  * Also returns success or failure code.
907  */
908 int tf_alloc_tcam_entry(struct tf *tfp,
909                         struct tf_alloc_tcam_entry_parms *parms);
910
911 /**
912  * tf_set_tcam_entry parameter definition
913  */
914 struct  tf_set_tcam_entry_parms {
915         /**
916          * [in] receive or transmit direction
917          */
918         enum tf_dir dir;
919         /**
920          * [in] TCAM table type
921          */
922         enum tf_tcam_tbl_type tcam_tbl_type;
923         /**
924          * [in] base index of the entry to program
925          */
926         uint16_t idx;
927         /**
928          * [in] struct containing key
929          */
930         uint8_t *key;
931         /**
932          * [in] struct containing mask fields
933          */
934         uint8_t *mask;
935         /**
936          * [in] key size in bits (if search)
937          */
938         uint16_t key_sz_in_bits;
939         /**
940          * [in] struct containing result
941          */
942         uint8_t *result;
943         /**
944          * [in] struct containing result size in bits
945          */
946         uint16_t result_sz_in_bits;
947 };
948
949 /**
950  * set TCAM entry
951  *
952  * Program a TCAM table entry for a TruFlow session.
953  *
954  * If the entry has not been allocated, an error will be returned.
955  *
956  * Returns success or failure code.
957  */
958 int tf_set_tcam_entry(struct tf *tfp,
959                       struct tf_set_tcam_entry_parms *parms);
960
961 /**
962  * tf_get_tcam_entry parameter definition
963  */
964 struct tf_get_tcam_entry_parms {
965         /**
966          * [in] receive or transmit direction
967          */
968         enum tf_dir dir;
969         /**
970          * [in] TCAM table type
971          */
972         enum tf_tcam_tbl_type  tcam_tbl_type;
973         /**
974          * [in] index of the entry to get
975          */
976         uint16_t idx;
977         /**
978          * [out] struct containing key
979          */
980         uint8_t *key;
981         /**
982          * [out] struct containing mask fields
983          */
984         uint8_t *mask;
985         /**
986          * [out] key size in bits
987          */
988         uint16_t key_sz_in_bits;
989         /**
990          * [out] struct containing result
991          */
992         uint8_t *result;
993         /**
994          * [out] struct containing result size in bits
995          */
996         uint16_t result_sz_in_bits;
997 };
998
999 /**
1000  * get TCAM entry
1001  *
1002  * Program a TCAM table entry for a TruFlow session.
1003  *
1004  * If the entry has not been allocated, an error will be returned.
1005  *
1006  * Returns success or failure code.
1007  */
1008 int tf_get_tcam_entry(struct tf *tfp,
1009                       struct tf_get_tcam_entry_parms *parms);
1010
1011 /**
1012  * tf_free_tcam_entry parameter definition
1013  */
1014 struct tf_free_tcam_entry_parms {
1015         /**
1016          * [in] receive or transmit direction
1017          */
1018         enum tf_dir dir;
1019         /**
1020          * [in] TCAM table type
1021          */
1022         enum tf_tcam_tbl_type tcam_tbl_type;
1023         /**
1024          * [in] Index to free
1025          */
1026         uint16_t idx;
1027         /**
1028          * [out] reference count after free
1029          */
1030         uint16_t ref_cnt;
1031 };
1032
1033 /**
1034  * free TCAM entry
1035  *
1036  * Free TCAM entry.
1037  *
1038  * Firmware checks to ensure the TCAM entries are owned by the TruFlow
1039  * session.  TCAM entry will be invalidated.  All-ones mask.
1040  * writes to hw.
1041  *
1042  * WCTCAM profile id of 0 must be used to invalidate an entry.
1043  *
1044  * Returns success or failure code.
1045  */
1046 int tf_free_tcam_entry(struct tf *tfp,
1047                        struct tf_free_tcam_entry_parms *parms);
1048
1049 /**
1050  * @page table Table Access
1051  *
1052  * @ref tf_alloc_tbl_entry
1053  *
1054  * @ref tf_free_tbl_entry
1055  *
1056  * @ref tf_set_tbl_entry
1057  *
1058  * @ref tf_get_tbl_entry
1059  */
1060
1061
1062 /**
1063  * tf_alloc_tbl_entry parameter definition
1064  */
1065 struct tf_alloc_tbl_entry_parms {
1066         /**
1067          * [in] Receive or transmit direction
1068          */
1069         enum tf_dir dir;
1070         /**
1071          * [in] Type of the allocation
1072          */
1073         enum tf_tbl_type type;
1074         /**
1075          * [in] Table scope identifier (ignored unless TF_TBL_TYPE_EXT)
1076          */
1077         uint32_t tbl_scope_id;
1078         /**
1079          * [in] Enable search for matching entry. If the table type is
1080          * internal the shadow copy will be searched before
1081          * alloc. Session must be configured with shadow copy enabled.
1082          */
1083         uint8_t search_enable;
1084         /**
1085          * [in] Result data to search for (if search_enable)
1086          */
1087         uint8_t *result;
1088         /**
1089          * [in] Result data size in bytes (if search_enable)
1090          */
1091         uint16_t result_sz_in_bytes;
1092         /**
1093          * [out] If search_enable, set if matching entry found
1094          */
1095         uint8_t hit;
1096         /**
1097          * [out] Current ref count after allocation (if search_enable)
1098          */
1099         uint16_t ref_cnt;
1100         /**
1101          * [out] Idx of allocated entry or found entry (if search_enable)
1102          */
1103         uint32_t idx;
1104 };
1105
1106 /**
1107  * allocate index table entries
1108  *
1109  * Internal types:
1110  *
1111  * Allocate an on chip index table entry or search for a matching
1112  * entry of the indicated type for this TruFlow session.
1113  *
1114  * Allocates an index table record. This function will attempt to
1115  * allocate an entry or search an index table for a matching entry if
1116  * search is enabled (only the shadow copy of the table is accessed).
1117  *
1118  * If search is not enabled, the first available free entry is
1119  * returned. If search is enabled and a matching entry to entry_data
1120  * is found hit is set to TRUE and success is returned.
1121  *
1122  * External types:
1123  *
1124  * These are used to allocate inlined action record memory.
1125  *
1126  * Allocates an external index table action record.
1127  *
1128  * NOTE:
1129  * Implementation of the internals of this function will be a stack with push
1130  * and pop.
1131  *
1132  * Returns success or failure code.
1133  */
1134 int tf_alloc_tbl_entry(struct tf *tfp,
1135                        struct tf_alloc_tbl_entry_parms *parms);
1136
1137 /**
1138  * tf_free_tbl_entry parameter definition
1139  */
1140 struct tf_free_tbl_entry_parms {
1141         /**
1142          * [in] Receive or transmit direction
1143          */
1144         enum tf_dir dir;
1145         /**
1146          * [in] Type of the allocation type
1147          */
1148         enum tf_tbl_type type;
1149         /**
1150          * [in] Table scope identifier (ignored unless TF_TBL_TYPE_EXT)
1151          */
1152         uint32_t tbl_scope_id;
1153         /**
1154          * [in] Index to free
1155          */
1156         uint32_t idx;
1157         /**
1158          * [out] Reference count after free, only valid if session has been
1159          * created with shadow_copy.
1160          */
1161         uint16_t ref_cnt;
1162 };
1163
1164 /**
1165  * free index table entry
1166  *
1167  * Used to free a previously allocated table entry.
1168  *
1169  * Internal types:
1170  *
1171  * If session has shadow_copy enabled the shadow DB is searched and if
1172  * found the element ref_cnt is decremented. If ref_cnt goes to
1173  * zero then the element is returned to the session pool.
1174  *
1175  * If the session does not have a shadow DB the element is free'ed and
1176  * given back to the session pool.
1177  *
1178  * External types:
1179  *
1180  * Free's an external index table action record.
1181  *
1182  * NOTE:
1183  * Implementation of the internals of this function will be a stack with push
1184  * and pop.
1185  *
1186  * Returns success or failure code.
1187  */
1188 int tf_free_tbl_entry(struct tf *tfp,
1189                       struct tf_free_tbl_entry_parms *parms);
1190
1191 /**
1192  * tf_set_tbl_entry parameter definition
1193  */
1194 struct tf_set_tbl_entry_parms {
1195         /**
1196          * [in] Table scope identifier
1197          */
1198         uint32_t tbl_scope_id;
1199         /**
1200          * [in] Receive or transmit direction
1201          */
1202         enum tf_dir dir;
1203         /**
1204          * [in] Type of object to set
1205          */
1206         enum tf_tbl_type type;
1207         /**
1208          * [in] Entry data
1209          */
1210         uint8_t *data;
1211         /**
1212          * [in] Entry size
1213          */
1214         uint16_t data_sz_in_bytes;
1215         /**
1216          * [in] Entry index to write to
1217          */
1218         uint32_t idx;
1219 };
1220
1221 /**
1222  * set index table entry
1223  *
1224  * Used to insert an application programmed index table entry into a
1225  * previous allocated table location.  A shadow copy of the table
1226  * is maintained (if enabled) (only for internal objects)
1227  *
1228  * Returns success or failure code.
1229  */
1230 int tf_set_tbl_entry(struct tf *tfp,
1231                      struct tf_set_tbl_entry_parms *parms);
1232
1233 /**
1234  * tf_get_tbl_entry parameter definition
1235  */
1236 struct tf_get_tbl_entry_parms {
1237         /**
1238          * [in] Receive or transmit direction
1239          */
1240         enum tf_dir dir;
1241         /**
1242          * [in] Type of object to get
1243          */
1244         enum tf_tbl_type type;
1245         /**
1246          * [out] Entry data
1247          */
1248         uint8_t *data;
1249         /**
1250          * [in] Entry size
1251          */
1252         uint16_t data_sz_in_bytes;
1253         /**
1254          * [in] Entry index to read
1255          */
1256         uint32_t idx;
1257 };
1258
1259 /**
1260  * get index table entry
1261  *
1262  * Used to retrieve a previous set index table entry.
1263  *
1264  * Reads and compares with the shadow table copy (if enabled) (only
1265  * for internal objects).
1266  *
1267  * Returns success or failure code. Failure will be returned if the
1268  * provided data buffer is too small for the data type requested.
1269  */
1270 int tf_get_tbl_entry(struct tf *tfp,
1271                      struct tf_get_tbl_entry_parms *parms);
1272
1273 /**
1274  * tf_bulk_get_tbl_entry parameter definition
1275  */
1276 struct tf_bulk_get_tbl_entry_parms {
1277         /**
1278          * [in] Receive or transmit direction
1279          */
1280         enum tf_dir dir;
1281         /**
1282          * [in] Type of object to get
1283          */
1284         enum tf_tbl_type type;
1285         /**
1286          * [in] Starting index to read from
1287          */
1288         uint32_t starting_idx;
1289         /**
1290          * [in] Number of sequential entries
1291          */
1292         uint16_t num_entries;
1293         /**
1294          * [in] Size of the single entry
1295          */
1296         uint16_t entry_sz_in_bytes;
1297         /**
1298          * [out] Host physical address, where the data
1299          * will be copied to by the firmware.
1300          * Use tfp_calloc() API and mem_pa
1301          * variable of the tfp_calloc_parms
1302          * structure for the physical address.
1303          */
1304         uint64_t physical_mem_addr;
1305 };
1306
1307 /**
1308  * Bulk get index table entry
1309  *
1310  * Used to retrieve a previous set index table entry.
1311  *
1312  * Reads and compares with the shadow table copy (if enabled) (only
1313  * for internal objects).
1314  *
1315  * Returns success or failure code. Failure will be returned if the
1316  * provided data buffer is too small for the data type requested.
1317  */
1318 int tf_bulk_get_tbl_entry(struct tf *tfp,
1319                           struct tf_bulk_get_tbl_entry_parms *parms);
1320
1321 /**
1322  * @page exact_match Exact Match Table
1323  *
1324  * @ref tf_insert_em_entry
1325  *
1326  * @ref tf_delete_em_entry
1327  *
1328  * @ref tf_search_em_entry
1329  *
1330  */
1331 /**
1332  * tf_insert_em_entry parameter definition
1333  */
1334 struct tf_insert_em_entry_parms {
1335         /**
1336          * [in] receive or transmit direction
1337          */
1338         enum tf_dir dir;
1339         /**
1340          * [in] internal or external
1341          */
1342         enum tf_mem mem;
1343         /**
1344          * [in] ID of table scope to use (external only)
1345          */
1346         uint32_t tbl_scope_id;
1347         /**
1348          * [in] ID of table interface to use (SR2 only)
1349          */
1350         uint32_t tbl_if_id;
1351         /**
1352          * [in] ptr to structure containing key fields
1353          */
1354         uint8_t *key;
1355         /**
1356          * [in] key bit length
1357          */
1358         uint16_t key_sz_in_bits;
1359         /**
1360          * [in] ptr to structure containing result field
1361          */
1362         uint8_t *em_record;
1363         /**
1364          * [out] result size in bits
1365          */
1366         uint16_t em_record_sz_in_bits;
1367         /**
1368          * [in] duplicate check flag
1369          */
1370         uint8_t dup_check;
1371         /**
1372          * [out] Flow handle value for the inserted entry.  This is encoded
1373          * as the entries[4]:bucket[2]:hashId[1]:hash[14]
1374          */
1375         uint64_t flow_handle;
1376         /**
1377          * [out] Flow id is returned as null (internal)
1378          * Flow id is the GFID value for the inserted entry (external)
1379          * This is the value written to the BD and useful information for mark.
1380          */
1381         uint64_t flow_id;
1382 };
1383 /**
1384  * tf_delete_em_entry parameter definition
1385  */
1386 struct tf_delete_em_entry_parms {
1387         /**
1388          * [in] receive or transmit direction
1389          */
1390         enum tf_dir dir;
1391         /**
1392          * [in] internal or external
1393          */
1394         enum tf_mem mem;
1395         /**
1396          * [in] ID of table scope to use (external only)
1397          */
1398         uint32_t tbl_scope_id;
1399         /**
1400          * [in] ID of table interface to use (SR2 only)
1401          */
1402         uint32_t tbl_if_id;
1403         /**
1404          * [in] epoch group IDs of entry to delete
1405          * 2 element array with 2 ids. (SR2 only)
1406          */
1407         uint16_t *epochs;
1408         /**
1409          * [out] The index of the entry
1410          */
1411         uint16_t index;
1412         /**
1413          * [in] structure containing flow delete handle information
1414          */
1415         uint64_t flow_handle;
1416 };
1417 /**
1418  * tf_search_em_entry parameter definition
1419  */
1420 struct tf_search_em_entry_parms {
1421         /**
1422          * [in] receive or transmit direction
1423          */
1424         enum tf_dir dir;
1425         /**
1426          * [in] internal or external
1427          */
1428         enum tf_mem mem;
1429         /**
1430          * [in] ID of table scope to use (external only)
1431          */
1432         uint32_t tbl_scope_id;
1433         /**
1434          * [in] ID of table interface to use (SR2 only)
1435          */
1436         uint32_t tbl_if_id;
1437         /**
1438          * [in] ptr to structure containing key fields
1439          */
1440         uint8_t *key;
1441         /**
1442          * [in] key bit length
1443          */
1444         uint16_t key_sz_in_bits;
1445         /**
1446          * [in/out] ptr to structure containing EM record fields
1447          */
1448         uint8_t *em_record;
1449         /**
1450          * [out] result size in bits
1451          */
1452         uint16_t em_record_sz_in_bits;
1453         /**
1454          * [in] epoch group IDs of entry to lookup
1455          * 2 element array with 2 ids. (SR2 only)
1456          */
1457         uint16_t *epochs;
1458         /**
1459          * [in] ptr to structure containing flow delete handle
1460          */
1461         uint64_t flow_handle;
1462 };
1463
1464 /**
1465  * insert em hash entry in internal table memory
1466  *
1467  * Internal:
1468  *
1469  * This API inserts an exact match entry into internal EM table memory
1470  * of the specified direction.
1471  *
1472  * Note: The EM record is managed within the TruFlow core and not the
1473  * application.
1474  *
1475  * Shadow copy of internal record table an association with hash and 1,2, or 4
1476  * associated buckets
1477  *
1478  * External:
1479  * This API inserts an exact match entry into DRAM EM table memory of the
1480  * specified direction and table scope.
1481  *
1482  * When inserting an entry into an exact match table, the TruFlow library may
1483  * need to allocate a dynamic bucket for the entry (SR2 only).
1484  *
1485  * The insertion of duplicate entries in an EM table is not permitted.  If a
1486  * TruFlow application can guarantee that it will never insert duplicates, it
1487  * can disable duplicate checking by passing a zero value in the  dup_check
1488  * parameter to this API.  This will optimize performance. Otherwise, the
1489  * TruFlow library will enforce protection against inserting duplicate entries.
1490  *
1491  * Flow handle is defined in this document:
1492  *
1493  * https://docs.google.com
1494  * /document/d/1NESu7RpTN3jwxbokaPfYORQyChYRmJgs40wMIRe8_-Q/edit
1495  *
1496  * Returns success or busy code.
1497  *
1498  */
1499 int tf_insert_em_entry(struct tf *tfp,
1500                        struct tf_insert_em_entry_parms *parms);
1501
1502 /**
1503  * delete em hash entry table memory
1504  *
1505  * Internal:
1506  *
1507  * This API deletes an exact match entry from internal EM table memory of the
1508  * specified direction. If a valid flow ptr is passed in then that takes
1509  * precedence over the pointer to the complete key passed in.
1510  *
1511  *
1512  * External:
1513  *
1514  * This API deletes an exact match entry from EM table memory of the specified
1515  * direction and table scope. If a valid flow handle is passed in then that
1516  * takes precedence over the pointer to the complete key passed in.
1517  *
1518  * The TruFlow library may release a dynamic bucket when an entry is deleted.
1519  *
1520  *
1521  * Returns success or not found code
1522  *
1523  *
1524  */
1525 int tf_delete_em_entry(struct tf *tfp,
1526                        struct tf_delete_em_entry_parms *parms);
1527
1528 /**
1529  * search em hash entry table memory
1530  *
1531  * Internal:
1532
1533  * This API looks up an EM entry in table memory with the specified EM
1534  * key or flow (flow takes precedence) and direction.
1535  *
1536  * The status will be one of: success or entry not found.  If the lookup
1537  * succeeds, a pointer to the matching entry and the result record associated
1538  * with the matching entry will be provided.
1539  *
1540  * If flow_handle is set, search shadow copy.
1541  *
1542  * Otherwise, query the fw with key to get result.
1543  *
1544  * External:
1545  *
1546  * This API looks up an EM entry in table memory with the specified EM
1547  * key or flow_handle (flow takes precedence), direction and table scope.
1548  *
1549  * The status will be one of: success or entry not found.  If the lookup
1550  * succeeds, a pointer to the matching entry and the result record associated
1551  * with the matching entry will be provided.
1552  *
1553  * Returns success or not found code
1554  *
1555  */
1556 int tf_search_em_entry(struct tf *tfp,
1557                        struct tf_search_em_entry_parms *parms);
1558
1559 #endif /* _TF_CORE_H_ */