net/bnxt: support HCAPI interface
[dpdk.git] / drivers / net / bnxt / hcapi / hcapi_cfa_defs.h
1
2 /* SPDX-License-Identifier: BSD-3-Clause
3  * Copyright(c) 2014-2020 Broadcom
4  * All rights reserved.
5  */
6
7 /*!
8  *   \file
9  *   \brief Exported functions for CFA HW programming
10  */
11 #ifndef _HCAPI_CFA_DEFS_H_
12 #define _HCAPI_CFA_DEFS_H_
13
14 #include <stdio.h>
15 #include <string.h>
16 #include <stdbool.h>
17 #include <stdint.h>
18 #include <stddef.h>
19
20 #define SUPPORT_CFA_HW_ALL 0
21 #define SUPPORT_CFA_HW_P4  1
22 #define SUPPORT_CFA_HW_P58 0
23 #define SUPPORT_CFA_HW_P59 0
24
25 #define CFA_BITS_PER_BYTE (8)
26 #define __CFA_ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
27 #define CFA_ALIGN(x, a) __CFA_ALIGN_MASK(x, (a) - 1)
28 #define CFA_ALIGN_128(x) CFA_ALIGN(x, 128)
29 #define CFA_ALIGN_32(x) CFA_ALIGN(x, 32)
30
31 #define NUM_WORDS_ALIGN_32BIT(x)                                               \
32         (CFA_ALIGN_32(x) / (sizeof(uint32_t) * CFA_BITS_PER_BYTE))
33 #define NUM_WORDS_ALIGN_128BIT(x)                                              \
34         (CFA_ALIGN_128(x) / (sizeof(uint32_t) * CFA_BITS_PER_BYTE))
35
36 #define CFA_GLOBAL_CFG_DATA_SZ (100)
37
38 #if SUPPORT_CFA_HW_P4 && SUPPORT_CFA_HW_P58 && SUPPORT_CFA_HW_P59
39 #define SUPPORT_CFA_HW_ALL (1)
40 #endif
41
42 #include "hcapi_cfa_p4.h"
43 #define CFA_PROF_L2CTXT_TCAM_MAX_FIELD_CNT CFA_P40_PROF_L2_CTXT_TCAM_MAX_FLD
44 #define CFA_PROF_L2CTXT_REMAP_MAX_FIELD_CNT CFA_P40_PROF_L2_CTXT_RMP_DR_MAX_FLD
45 #define CFA_PROF_MAX_KEY_CFG_SZ sizeof(struct cfa_p4_prof_key_cfg)
46 #define CFA_KEY_MAX_FIELD_CNT 41
47 #define CFA_ACT_MAX_TEMPLATE_SZ sizeof(struct cfa_p4_action_template)
48
49 /**
50  * CFA HW version definition
51  */
52 enum hcapi_cfa_ver {
53         HCAPI_CFA_P40 = 0, /**< CFA phase 4.0 */
54         HCAPI_CFA_P45 = 1, /**< CFA phase 4.5 */
55         HCAPI_CFA_P58 = 2, /**< CFA phase 5.8 */
56         HCAPI_CFA_P59 = 3, /**< CFA phase 5.9 */
57         HCAPI_CFA_PMAX = 4
58 };
59
60 /**
61  * CFA direction definition
62  */
63 enum hcapi_cfa_dir {
64         HCAPI_CFA_DIR_RX = 0, /**< Receive */
65         HCAPI_CFA_DIR_TX = 1, /**< Transmit */
66         HCAPI_CFA_DIR_MAX = 2
67 };
68
69 /**
70  * CFA HW OPCODE definition
71  */
72 enum hcapi_cfa_hwops {
73         HCAPI_CFA_HWOPS_PUT, /**< Write to HW operation */
74         HCAPI_CFA_HWOPS_GET, /**< Read from HW operation */
75         HCAPI_CFA_HWOPS_ADD, /**< For operations which require more than simple
76                               * writes to HW, this operation is used. The
77                               * distinction with this operation when compared
78                               * to the PUT ops is that this operation is used
79                               * in conjunction with the HCAPI_CFA_HWOPS_DEL
80                               * op to remove the operations issued by the
81                               * ADD OP.
82                               */
83         HCAPI_CFA_HWOPS_DEL, /**< This issues operations to clear the hardware.
84                               * This operation is used in conjunction
85                               * with the HCAPI_CFA_HWOPS_ADD op and is the
86                               * way to undo/clear the ADD op.
87                               */
88         HCAPI_CFA_HWOPS_MAX
89 };
90
91 /**
92  * CFA HW KEY CONTROL OPCODE definition
93  */
94 enum hcapi_cfa_key_ctrlops {
95         HCAPI_CFA_KEY_CTRLOPS_INSERT, /**< insert control bits */
96         HCAPI_CFA_KEY_CTRLOPS_STRIP, /**< strip control bits */
97         HCAPI_CFA_KEY_CTRLOPS_MAX
98 };
99
100 /**
101  * CFA HW field structure definition
102  */
103 struct hcapi_cfa_field {
104         /** [in] Starting bit position pf the HW field within a HW table
105          *  entry.
106          */
107         uint16_t bitpos;
108         /** [in] Number of bits for the HW field. */
109         uint8_t bitlen;
110 };
111
112 /**
113  * CFA HW table entry layout structure definition
114  */
115 struct hcapi_cfa_layout {
116         /** [out] Bit order of layout */
117         bool is_msb_order;
118         /** [out] Size in bits of entry */
119         uint32_t total_sz_in_bits;
120         /** [out] data pointer of the HW layout fields array */
121         const struct hcapi_cfa_field *field_array;
122         /** [out] number of HW field entries in the HW layout field array */
123         uint32_t array_sz;
124 };
125
126 /**
127  * CFA HW data object definition
128  */
129 struct hcapi_cfa_data_obj {
130         /** [in] HW field identifier. Used as an index to a HW table layout */
131         uint16_t field_id;
132         /** [in] Value of the HW field */
133         uint64_t val;
134 };
135
136 /**
137  * CFA HW definition
138  */
139 struct hcapi_cfa_hw {
140         /** [in] HW table base address for the operation with optional device
141          *  handle. For on-chip HW table operation, this is the either the TX
142          *  or RX CFA HW base address. For off-chip table, this field is the
143          *  base memory address of the off-chip table.
144          */
145         uint64_t base_addr;
146         /** [in] Optional opaque device handle. It is generally used to access
147          *  an GRC register space through PCIE BAR and passed to the BAR memory
148          *  accessor routine.
149          */
150         void *handle;
151 };
152
153 /**
154  * CFA HW operation definition
155  *
156  */
157 struct hcapi_cfa_hwop {
158         /** [in] HW opcode */
159         enum hcapi_cfa_hwops opcode;
160         /** [in] CFA HW information used by accessor routines.
161          */
162         struct hcapi_cfa_hw hw;
163 };
164
165 /**
166  * CFA HW data structure definition
167  */
168 struct hcapi_cfa_data {
169         /** [in] physical offset to the HW table for the data to be
170          *  written to.  If this is an array of registers, this is the
171          *  index into the array of registers.  For writing keys, this
172          *  is the byte offset into the memory where the key should be
173          *  written.
174          */
175         union {
176                 uint32_t index;
177                 uint32_t byte_offset;
178         } u;
179         /** [in] HW data buffer pointer */
180         uint8_t *data;
181         /** [in] HW data mask buffer pointer */
182         uint8_t *data_mask;
183         /** [in] size of the HW data buffer in bytes */
184         uint16_t data_sz;
185 };
186
187 /*********************** Truflow start ***************************/
188 enum hcapi_cfa_pg_tbl_lvl {
189         TF_PT_LVL_0,
190         TF_PT_LVL_1,
191         TF_PT_LVL_2,
192         TF_PT_LVL_MAX
193 };
194
195 enum hcapi_cfa_em_table_type {
196         TF_KEY0_TABLE,
197         TF_KEY1_TABLE,
198         TF_RECORD_TABLE,
199         TF_EFC_TABLE,
200         TF_MAX_TABLE
201 };
202
203 struct hcapi_cfa_em_page_tbl {
204         uint32_t        pg_count;
205         uint32_t        pg_size;
206         void            **pg_va_tbl;
207         uint64_t        *pg_pa_tbl;
208 };
209
210 struct hcapi_cfa_em_table {
211         int                             type;
212         uint32_t                        num_entries;
213         uint16_t                        ctx_id;
214         uint32_t                        entry_size;
215         int                             num_lvl;
216         uint32_t                        page_cnt[TF_PT_LVL_MAX];
217         uint64_t                        num_data_pages;
218         void                            *l0_addr;
219         uint64_t                        l0_dma_addr;
220         struct hcapi_cfa_em_page_tbl    pg_tbl[TF_PT_LVL_MAX];
221 };
222
223 struct hcapi_cfa_em_ctx_mem_info {
224         struct hcapi_cfa_em_table               em_tables[TF_MAX_TABLE];
225 };
226
227 /*********************** Truflow end ****************************/
228
229 /**
230  * CFA HW key table definition
231  *
232  * Applicable to EEM and off-chip EM table only.
233  */
234 struct hcapi_cfa_key_tbl {
235         /** [in] For EEM, this is the KEY0 base mem pointer. For off-chip EM,
236          *  this is the base mem pointer of the key table.
237          */
238         uint8_t *base0;
239         /** [in] total size of the key table in bytes. For EEM, this size is
240          *  same for both KEY0 and KEY1 table.
241          */
242         uint32_t size;
243         /** [in] number of key buckets, applicable for newer chips */
244         uint32_t num_buckets;
245         /** [in] For EEM, this is KEY1 base mem pointer. Fo off-chip EM,
246          *  this is the key record memory base pointer within the key table,
247          *  applicable for newer chip
248          */
249         uint8_t *base1;
250 };
251
252 /**
253  * CFA HW key buffer definition
254  */
255 struct hcapi_cfa_key_obj {
256         /** [in] pointer to the key data buffer */
257         uint32_t *data;
258         /** [in] buffer len in bits */
259         uint32_t len;
260         /** [in] Pointer to the key layout */
261         struct hcapi_cfa_key_layout *layout;
262 };
263
264 /**
265  * CFA HW key data definition
266  */
267 struct hcapi_cfa_key_data {
268         /** [in] For on-chip key table, it is the offset in unit of smallest
269          *  key. For off-chip key table, it is the byte offset relative
270          *  to the key record memory base.
271          */
272         uint32_t offset;
273         /** [in] HW key data buffer pointer */
274         uint8_t *data;
275         /** [in] size of the key in bytes */
276         uint16_t size;
277 };
278
279 /**
280  * CFA HW key location definition
281  */
282 struct hcapi_cfa_key_loc {
283         /** [out] on-chip EM bucket offset or off-chip EM bucket mem pointer */
284         uint64_t bucket_mem_ptr;
285         /** [out] index within the EM bucket */
286         uint8_t bucket_idx;
287 };
288
289 /**
290  * CFA HW layout table definition
291  */
292 struct hcapi_cfa_layout_tbl {
293         /** [out] data pointer to an array of fix formatted layouts supported.
294          *  The index to the array is the CFA HW table ID
295          */
296         const struct hcapi_cfa_layout *tbl;
297         /** [out] number of fix formatted layouts in the layout array */
298         uint16_t num_layouts;
299 };
300
301 /**
302  * Key template consists of key fields that can be enabled/disabled
303  * individually.
304  */
305 struct hcapi_cfa_key_template {
306         /** [in] key field enable field array, set 1 to the correspeonding
307          *  field enable to make a field valid
308          */
309         uint8_t field_en[CFA_KEY_MAX_FIELD_CNT];
310         /** [in] Identified if the key template is for TCAM. If false, the
311          *  the key template is for EM. This field is mandantory for device that
312          *  only support fix key formats.
313          */
314         bool is_wc_tcam_key;
315 };
316
317 /**
318  * key layout consist of field array, key bitlen, key ID, and other meta data
319  * pertain to a key
320  */
321 struct hcapi_cfa_key_layout {
322         /** [out] key layout data */
323         struct hcapi_cfa_layout *layout;
324         /** [out] actual key size in number of bits */
325         uint16_t bitlen;
326         /** [out] key identifier and this field is only valid for device
327          *  that supports fix key formats
328          */
329         uint16_t id;
330         /** [out] Identified the key layout is WC TCAM key */
331         bool is_wc_tcam_key;
332         /** [out] total slices size, valid for WC TCAM key only. It can be
333          *  used by the user to determine the total size of WC TCAM key slices
334          *  in bytes.
335          */
336         uint16_t slices_size;
337 };
338
339 /**
340  * key layout memory contents
341  */
342 struct hcapi_cfa_key_layout_contents {
343         /** key layouts */
344         struct hcapi_cfa_key_layout key_layout;
345
346         /** layout */
347         struct hcapi_cfa_layout layout;
348
349         /** fields */
350         struct hcapi_cfa_field field_array[CFA_KEY_MAX_FIELD_CNT];
351 };
352
353 /**
354  * Action template consists of action fields that can be enabled/disabled
355  * individually.
356  */
357 struct hcapi_cfa_action_template {
358         /** [in] CFA version for the action template */
359         enum hcapi_cfa_ver hw_ver;
360         /** [in] action field enable field array, set 1 to the correspeonding
361          *  field enable to make a field valid
362          */
363         uint8_t data[CFA_ACT_MAX_TEMPLATE_SZ];
364 };
365
366 /**
367  * action layout consist of field array, action wordlen and action format ID
368  */
369 struct hcapi_cfa_action_layout {
370         /** [in] action identifier */
371         uint16_t id;
372         /** [out] action layout data */
373         struct hcapi_cfa_layout *layout;
374         /** [out] actual action record size in number of bits */
375         uint16_t wordlen;
376 };
377
378 /**
379  *  \defgroup CFA_HCAPI_PUT_API
380  *  HCAPI used for writing to the hardware
381  *  @{
382  */
383
384 /**
385  * This API provides the functionality to program a specified value to a
386  * HW field based on the provided programming layout.
387  *
388  * @param[in,out] obj_data
389  *   A data pointer to a CFA HW key/mask data
390  *
391  * @param[in] layout
392  *   A pointer to CFA HW programming layout
393  *
394  * @param[in] field_id
395  *   ID of the HW field to be programmed
396  *
397  * @param[in] val
398  *   Value of the HW field to be programmed
399  *
400  * @return
401  *   0 for SUCCESS, negative value for FAILURE
402  */
403 int hcapi_cfa_put_field(uint64_t *data_buf,
404                         const struct hcapi_cfa_layout *layout,
405                         uint16_t field_id, uint64_t val);
406
407 /**
408  * This API provides the functionality to program an array of field values
409  * with corresponding field IDs to a number of profiler sub-block fields
410  * based on the fixed profiler sub-block hardware programming layout.
411  *
412  * @param[in, out] obj_data
413  *   A pointer to a CFA profiler key/mask object data
414  *
415  * @param[in] layout
416  *   A pointer to CFA HW programming layout
417  *
418  * @param[in] field_tbl
419  *   A pointer to an array that consists of the object field
420  *   ID/value pairs
421  *
422  * @param[in] field_tbl_sz
423  *   Number of entries in the table
424  *
425  * @return
426  *   0 for SUCCESS, negative value for FAILURE
427  */
428 int hcapi_cfa_put_fields(uint64_t *obj_data,
429                          const struct hcapi_cfa_layout *layout,
430                          struct hcapi_cfa_data_obj *field_tbl,
431                          uint16_t field_tbl_sz);
432
433 /**
434  * This API provides the functionality to write a value to a
435  * field within the bit position and bit length of a HW data
436  * object based on a provided programming layout.
437  *
438  * @param[in, out] act_obj
439  *   A pointer of the action object to be initialized
440  *
441  * @param[in] layout
442  *   A pointer of the programming layout
443  *
444  * @param field_id
445  *   [in] Identifier of the HW field
446  *
447  * @param[in] bitpos_adj
448  *   Bit position adjustment value
449  *
450  * @param[in] bitlen_adj
451  *   Bit length adjustment value
452  *
453  * @param[in] val
454  *   HW field value to be programmed
455  *
456  * @return
457  *   0 for SUCCESS, negative value for FAILURE
458  */
459 int hcapi_cfa_put_field_rel(uint64_t *obj_data,
460                             const struct hcapi_cfa_layout *layout,
461                             uint16_t field_id, int16_t bitpos_adj,
462                             int16_t bitlen_adj, uint64_t val);
463
464 /*@}*/
465
466 /**
467  *  \defgroup CFA_HCAPI_GET_API
468  *  HCAPI used for writing to the hardware
469  *  @{
470  */
471
472 /**
473  * This API provides the functionality to get the word length of
474  * a layout object.
475  *
476  * @param[in] layout
477  *   A pointer of the HW layout
478  *
479  * @return
480  *   Word length of the layout object
481  */
482 uint16_t hcapi_cfa_get_wordlen(const struct hcapi_cfa_layout *layout);
483
484 /**
485  * The API provides the functionality to get bit offset and bit
486  * length information of a field from a programming layout.
487  *
488  * @param[in] layout
489  *   A pointer of the action layout
490  *
491  * @param[out] slice
492  *   A pointer to the action offset info data structure
493  *
494  * @return
495  *   0 for SUCCESS, negative value for FAILURE
496  */
497 int hcapi_cfa_get_slice(const struct hcapi_cfa_layout *layout,
498                         uint16_t field_id, struct hcapi_cfa_field *slice);
499
500 /**
501  * This API provides the functionality to read the value of a
502  * CFA HW field from CFA HW data object based on the hardware
503  * programming layout.
504  *
505  * @param[in] obj_data
506  *   A pointer to a CFA HW key/mask object data
507  *
508  * @param[in] layout
509  *   A pointer to CFA HW programming layout
510  *
511  * @param[in] field_id
512  *   ID of the HW field to be programmed
513  *
514  * @param[out] val
515  *   Value of the HW field
516  *
517  * @return
518  *   0 for SUCCESS, negative value for FAILURE
519  */
520 int hcapi_cfa_get_field(uint64_t *obj_data,
521                         const struct hcapi_cfa_layout *layout,
522                         uint16_t field_id, uint64_t *val);
523
524 /**
525  * This API provides the functionality to read a number of
526  * HW fields from a CFA HW data object based on the hardware
527  * programming layout.
528  *
529  * @param[in] obj_data
530  *   A pointer to a CFA profiler key/mask object data
531  *
532  * @param[in] layout
533  *   A pointer to CFA HW programming layout
534  *
535  * @param[in, out] field_tbl
536  *   A pointer to an array that consists of the object field
537  *   ID/value pairs
538  *
539  * @param[in] field_tbl_sz
540  *   Number of entries in the table
541  *
542  * @return
543  *   0 for SUCCESS, negative value for FAILURE
544  */
545 int hcapi_cfa_get_fields(uint64_t *obj_data,
546                          const struct hcapi_cfa_layout *layout,
547                          struct hcapi_cfa_data_obj *field_tbl,
548                          uint16_t field_tbl_sz);
549
550 /**
551  * Get a value to a specific location relative to a HW field
552  *
553  * This API provides the functionality to read HW field from
554  * a section of a HW data object identified by the bit position
555  * and bit length from a given programming layout in order to avoid
556  * reading the entire HW data object.
557  *
558  * @param[in] obj_data
559  *   A pointer of the data object to read from
560  *
561  * @param[in] layout
562  *   A pointer of the programming layout
563  *
564  * @param[in] field_id
565  *   Identifier of the HW field
566  *
567  * @param[in] bitpos_adj
568  *   Bit position adjustment value
569  *
570  * @param[in] bitlen_adj
571  *   Bit length adjustment value
572  *
573  * @param[out] val
574  *   Value of the HW field
575  *
576  * @return
577  *   0 for SUCCESS, negative value for FAILURE
578  */
579 int hcapi_cfa_get_field_rel(uint64_t *obj_data,
580                             const struct hcapi_cfa_layout *layout,
581                             uint16_t field_id, int16_t bitpos_adj,
582                             int16_t bitlen_adj, uint64_t *val);
583
584 /**
585  * This function is used to initialize a layout_contents structure
586  *
587  * The struct hcapi_cfa_key_layout is complex as there are three
588  * layers of abstraction.  Each of those layer need to be properly
589  * initialized.
590  *
591  * @param[in] layout_contents
592  *  A pointer of the layout contents to initialize
593  *
594  * @return
595  *   0 for SUCCESS, negative value for FAILURE
596  */
597 int
598 hcapi_cfa_init_key_layout_contents(struct hcapi_cfa_key_layout_contents *cont);
599
600 /**
601  * This function is used to validate a key template
602  *
603  * The struct hcapi_cfa_key_template is complex as there are three
604  * layers of abstraction.  Each of those layer need to be properly
605  * validated.
606  *
607  * @param[in] key_template
608  *  A pointer of the key template contents to validate
609  *
610  * @return
611  *   0 for SUCCESS, negative value for FAILURE
612  */
613 int
614 hcapi_cfa_is_valid_key_template(struct hcapi_cfa_key_template *key_template);
615
616 /**
617  * This function is used to validate a key layout
618  *
619  * The struct hcapi_cfa_key_layout is complex as there are three
620  * layers of abstraction.  Each of those layer need to be properly
621  * validated.
622  *
623  * @param[in] key_layout
624  *  A pointer of the key layout contents to validate
625  *
626  * @return
627  *   0 for SUCCESS, negative value for FAILURE
628  */
629 int hcapi_cfa_is_valid_key_layout(struct hcapi_cfa_key_layout *key_layout);
630
631 /**
632  * This function is used to hash E/EM keys
633  *
634  *
635  * @param[in] key_data
636  *  A pointer of the key
637  *
638  * @param[in] bitlen
639  *  Number of bits in the key
640  *
641  * @return
642  *   CRC32 and Lookup3 hashes of the input key
643  */
644 uint64_t hcapi_cfa_key_hash(uint64_t *key_data,
645                             uint16_t bitlen);
646
647 /**
648  * This function is used to execute an operation
649  *
650  *
651  * @param[in] op
652  *  Operation
653  *
654  * @param[in] key_tbl
655  *  Table
656  *
657  * @param[in] key_obj
658  *  Key data
659  *
660  * @param[in] key_key_loc
661  *
662  * @return
663  *   0 for SUCCESS, negative value for FAILURE
664  */
665 int hcapi_cfa_key_hw_op(struct hcapi_cfa_hwop *op,
666                         struct hcapi_cfa_key_tbl *key_tbl,
667                         struct hcapi_cfa_key_data *key_obj,
668                         struct hcapi_cfa_key_loc *key_loc);
669
670 uint64_t hcapi_get_table_page(struct hcapi_cfa_em_table *mem,
671                               uint32_t offset);
672 #endif /* HCAPI_CFA_DEFS_H_ */