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