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 #include "hcapi_cfa_p4.h"
39 #define CFA_PROF_L2CTXT_TCAM_MAX_FIELD_CNT CFA_P40_PROF_L2_CTXT_TCAM_MAX_FLD
40 #define CFA_PROF_L2CTXT_REMAP_MAX_FIELD_CNT CFA_P40_PROF_L2_CTXT_RMP_DR_MAX_FLD
41 #define CFA_PROF_MAX_KEY_CFG_SZ sizeof(struct cfa_p4_prof_key_cfg)
42 #define CFA_KEY_MAX_FIELD_CNT 41
43 #define CFA_ACT_MAX_TEMPLATE_SZ sizeof(struct cfa_p4_action_template)
44 
45 /**
46  * CFA HW version definition
47  */
48 enum hcapi_cfa_ver {
49 	HCAPI_CFA_P40 = 0, /**< CFA phase 4.0 */
50 	HCAPI_CFA_P45 = 1, /**< CFA phase 4.5 */
51 	HCAPI_CFA_P58 = 2, /**< CFA phase 5.8 */
52 	HCAPI_CFA_P59 = 3, /**< CFA phase 5.9 */
53 	HCAPI_CFA_PMAX = 4
54 };
55 
56 /**
57  * CFA direction definition
58  */
59 enum hcapi_cfa_dir {
60 	HCAPI_CFA_DIR_RX = 0, /**< Receive */
61 	HCAPI_CFA_DIR_TX = 1, /**< Transmit */
62 	HCAPI_CFA_DIR_MAX = 2
63 };
64 
65 /**
66  * CFA HW OPCODE definition
67  */
68 enum hcapi_cfa_hwops {
69 	HCAPI_CFA_HWOPS_PUT, /**< Write to HW operation */
70 	HCAPI_CFA_HWOPS_GET, /**< Read from HW operation */
71 	HCAPI_CFA_HWOPS_ADD, /**< For operations which require more than simple
72 			      * writes to HW, this operation is used. The
73 			      * distinction with this operation when compared
74 			      * to the PUT ops is that this operation is used
75 			      * in conjunction with the HCAPI_CFA_HWOPS_DEL
76 			      * op to remove the operations issued by the
77 			      * ADD OP.
78 			      */
79 	HCAPI_CFA_HWOPS_DEL, /**< This issues operations to clear the hardware.
80 			      * This operation is used in conjunction
81 			      * with the HCAPI_CFA_HWOPS_ADD op and is the
82 			      * way to undo/clear the ADD op.
83 			      */
84 	HCAPI_CFA_HWOPS_MAX
85 };
86 
87 /**
88  * CFA HW KEY CONTROL OPCODE definition
89  */
90 enum hcapi_cfa_key_ctrlops {
91 	HCAPI_CFA_KEY_CTRLOPS_INSERT, /**< insert control bits */
92 	HCAPI_CFA_KEY_CTRLOPS_STRIP, /**< strip control bits */
93 	HCAPI_CFA_KEY_CTRLOPS_MAX
94 };
95 
96 /**
97  * CFA HW field structure definition
98  */
99 struct hcapi_cfa_field {
100 	/** [in] Starting bit position pf the HW field within a HW table
101 	 *  entry.
102 	 */
103 	uint16_t bitpos;
104 	/** [in] Number of bits for the HW field. */
105 	uint8_t bitlen;
106 };
107 
108 /**
109  * CFA HW table entry layout structure definition
110  */
111 struct hcapi_cfa_layout {
112 	/** [out] Bit order of layout */
113 	bool is_msb_order;
114 	/** [out] Size in bits of entry */
115 	uint32_t total_sz_in_bits;
116 	/** [out] data pointer of the HW layout fields array */
117 	const struct hcapi_cfa_field *field_array;
118 	/** [out] number of HW field entries in the HW layout field array */
119 	uint32_t array_sz;
120 	/** [out] layout_id - layout id associated with the layout */
121 	uint16_t layout_id;
122 };
123 
124 /**
125  * CFA HW data object definition
126  */
127 struct hcapi_cfa_data_obj {
128 	/** [in] HW field identifier. Used as an index to a HW table layout */
129 	uint16_t field_id;
130 	/** [in] Value of the HW field */
131 	uint64_t val;
132 };
133 
134 /**
135  * CFA HW definition
136  */
137 struct hcapi_cfa_hw {
138 	/** [in] HW table base address for the operation with optional device
139 	 *  handle. For on-chip HW table operation, this is the either the TX
140 	 *  or RX CFA HW base address. For off-chip table, this field is the
141 	 *  base memory address of the off-chip table.
142 	 */
143 	uint64_t base_addr;
144 	/** [in] Optional opaque device handle. It is generally used to access
145 	 *  an GRC register space through PCIE BAR and passed to the BAR memory
146 	 *  accessor routine.
147 	 */
148 	void *handle;
149 };
150 
151 /**
152  * CFA HW operation definition
153  *
154  */
155 struct hcapi_cfa_hwop {
156 	/** [in] HW opcode */
157 	enum hcapi_cfa_hwops opcode;
158 	/** [in] CFA HW information used by accessor routines.
159 	 */
160 	struct hcapi_cfa_hw hw;
161 };
162 
163 /**
164  * CFA HW data structure definition
165  */
166 struct hcapi_cfa_data {
167 	/** [in] physical offset to the HW table for the data to be
168 	 *  written to.  If this is an array of registers, this is the
169 	 *  index into the array of registers.  For writing keys, this
170 	 *  is the byte offset into the memory where the key should be
171 	 *  written.
172 	 */
173 	union {
174 		uint32_t index;
175 		uint32_t byte_offset;
176 	} u;
177 	/** [in] HW data buffer pointer */
178 	uint8_t *data;
179 	/** [in] HW data mask buffer pointer */
180 	uint8_t *data_mask;
181 	/** [in] size of the HW data buffer in bytes */
182 	uint16_t data_sz;
183 };
184 
185 /*********************** Truflow start ***************************/
186 enum hcapi_cfa_pg_tbl_lvl {
187 	TF_PT_LVL_0,
188 	TF_PT_LVL_1,
189 	TF_PT_LVL_2,
190 	TF_PT_LVL_MAX
191 };
192 
193 enum hcapi_cfa_em_table_type {
194 	TF_KEY0_TABLE,
195 	TF_KEY1_TABLE,
196 	TF_RECORD_TABLE,
197 	TF_EFC_TABLE,
198 	TF_MAX_TABLE
199 };
200 
201 struct hcapi_cfa_em_page_tbl {
202 	uint32_t	pg_count;
203 	uint32_t	pg_size;
204 	void		**pg_va_tbl;
205 	uint64_t	*pg_pa_tbl;
206 };
207 
208 struct hcapi_cfa_em_table {
209 	int				type;
210 	uint32_t			num_entries;
211 	uint16_t			ctx_id;
212 	uint32_t			entry_size;
213 	int				num_lvl;
214 	uint32_t			page_cnt[TF_PT_LVL_MAX];
215 	uint64_t			num_data_pages;
216 	void				*l0_addr;
217 	uint64_t			l0_dma_addr;
218 	struct hcapi_cfa_em_page_tbl    pg_tbl[TF_PT_LVL_MAX];
219 };
220 
221 struct hcapi_cfa_em_ctx_mem_info {
222 	struct hcapi_cfa_em_table		em_tables[TF_MAX_TABLE];
223 };
224 
225 /*********************** Truflow end ****************************/
226 
227 /**
228  * CFA HW key table definition
229  *
230  * Applicable to EEM and off-chip EM table only.
231  */
232 struct hcapi_cfa_key_tbl {
233 	/** [in] For EEM, this is the KEY0 base mem pointer. For off-chip EM,
234 	 *  this is the base mem pointer of the key table.
235 	 */
236 	uint8_t *base0;
237 	/** [in] total size of the key table in bytes. For EEM, this size is
238 	 *  same for both KEY0 and KEY1 table.
239 	 */
240 	uint32_t size;
241 	/** [in] number of key buckets, applicable for newer chips */
242 	uint32_t num_buckets;
243 	/** [in] For EEM, this is KEY1 base mem pointer. Fo off-chip EM,
244 	 *  this is the key record memory base pointer within the key table,
245 	 *  applicable for newer chip
246 	 */
247 	uint8_t *base1;
248 	/** [in] Page size for EEM tables */
249 	uint32_t page_size;
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 and adjusted for page and entry size.
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 page);
672 #endif /* HCAPI_CFA_DEFS_H_ */
673