1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2019-2021 Broadcom
6 #include <rte_common.h>
8 #include "cfa_resource_types.h"
10 #include "tf_identifier.h"
14 #include "tf_tcam_shared.h"
15 #endif /* TF_TCAM_SHARED */
17 #include "tf_if_tbl.h"
19 #include "tf_msg_common.h"
21 #define TF_DEV_P4_PARIF_MAX 16
22 #define TF_DEV_P4_PF_MASK 0xfUL
24 const char *tf_resource_str_p4[CFA_RESOURCE_TYPE_P4_LAST + 1] = {
25 [CFA_RESOURCE_TYPE_P4_MCG] = "mc_group",
26 [CFA_RESOURCE_TYPE_P4_ENCAP_8B] = "encap_8 ",
27 [CFA_RESOURCE_TYPE_P4_ENCAP_16B] = "encap_16",
28 [CFA_RESOURCE_TYPE_P4_ENCAP_64B] = "encap_64",
29 [CFA_RESOURCE_TYPE_P4_SP_MAC] = "sp_mac ",
30 [CFA_RESOURCE_TYPE_P4_SP_MAC_IPV4] = "sp_macv4",
31 [CFA_RESOURCE_TYPE_P4_SP_MAC_IPV6] = "sp_macv6",
32 [CFA_RESOURCE_TYPE_P4_COUNTER_64B] = "ctr_64b ",
33 [CFA_RESOURCE_TYPE_P4_NAT_PORT] = "nat_port",
34 [CFA_RESOURCE_TYPE_P4_NAT_IPV4] = "nat_ipv4",
35 [CFA_RESOURCE_TYPE_P4_METER] = "meter ",
36 [CFA_RESOURCE_TYPE_P4_FLOW_STATE] = "flow_st ",
37 [CFA_RESOURCE_TYPE_P4_FULL_ACTION] = "full_act",
38 [CFA_RESOURCE_TYPE_P4_FORMAT_0_ACTION] = "fmt0_act",
39 [CFA_RESOURCE_TYPE_P4_EXT_FORMAT_0_ACTION] = "ext0_act",
40 [CFA_RESOURCE_TYPE_P4_FORMAT_1_ACTION] = "fmt1_act",
41 [CFA_RESOURCE_TYPE_P4_FORMAT_2_ACTION] = "fmt2_act",
42 [CFA_RESOURCE_TYPE_P4_FORMAT_3_ACTION] = "fmt3_act",
43 [CFA_RESOURCE_TYPE_P4_FORMAT_4_ACTION] = "fmt4_act",
44 [CFA_RESOURCE_TYPE_P4_FORMAT_5_ACTION] = "fmt5_act",
45 [CFA_RESOURCE_TYPE_P4_FORMAT_6_ACTION] = "fmt6_act",
46 [CFA_RESOURCE_TYPE_P4_L2_CTXT_TCAM_HIGH] = "l2ctx_hi",
47 [CFA_RESOURCE_TYPE_P4_L2_CTXT_TCAM_LOW] = "l2ctx_lo",
48 [CFA_RESOURCE_TYPE_P4_L2_CTXT_REMAP_HIGH] = "l2ctr_hi",
49 [CFA_RESOURCE_TYPE_P4_L2_CTXT_REMAP_LOW] = "l2ctr_lo",
50 [CFA_RESOURCE_TYPE_P4_PROF_FUNC] = "prf_func",
51 [CFA_RESOURCE_TYPE_P4_PROF_TCAM] = "prf_tcam",
52 [CFA_RESOURCE_TYPE_P4_EM_PROF_ID] = "em_prof ",
53 [CFA_RESOURCE_TYPE_P4_EM_REC] = "em_rec ",
54 [CFA_RESOURCE_TYPE_P4_WC_TCAM_PROF_ID] = "wc_prof ",
55 [CFA_RESOURCE_TYPE_P4_WC_TCAM] = "wc_tcam ",
56 [CFA_RESOURCE_TYPE_P4_METER_PROF] = "mtr_prof",
57 [CFA_RESOURCE_TYPE_P4_MIRROR] = "mirror ",
58 [CFA_RESOURCE_TYPE_P4_SP_TCAM] = "sp_tcam ",
59 [CFA_RESOURCE_TYPE_P4_TBL_SCOPE] = "tb_scope",
62 struct tf_rm_element_cfg tf_tbl_p4[TF_DIR_MAX][TF_TBL_TYPE_MAX] = {
63 [TF_DIR_RX][TF_TBL_TYPE_FULL_ACT_RECORD] = {
64 TF_RM_ELEM_CFG_HCAPI_BA, CFA_RESOURCE_TYPE_P4_FULL_ACTION,
67 [TF_DIR_RX][TF_TBL_TYPE_MCAST_GROUPS] = {
68 TF_RM_ELEM_CFG_HCAPI_BA, CFA_RESOURCE_TYPE_P4_MCG,
71 [TF_DIR_RX][TF_TBL_TYPE_ACT_ENCAP_8B] = {
72 TF_RM_ELEM_CFG_HCAPI_BA, CFA_RESOURCE_TYPE_P4_ENCAP_8B,
75 [TF_DIR_RX][TF_TBL_TYPE_ACT_ENCAP_16B] = {
76 TF_RM_ELEM_CFG_HCAPI_BA, CFA_RESOURCE_TYPE_P4_ENCAP_16B,
79 [TF_DIR_RX][TF_TBL_TYPE_ACT_ENCAP_64B] = {
80 TF_RM_ELEM_CFG_HCAPI_BA, CFA_RESOURCE_TYPE_P4_ENCAP_64B,
83 [TF_DIR_RX][TF_TBL_TYPE_ACT_SP_SMAC] = {
84 TF_RM_ELEM_CFG_HCAPI_BA, CFA_RESOURCE_TYPE_P4_SP_MAC,
87 [TF_DIR_RX][TF_TBL_TYPE_ACT_SP_SMAC_IPV4] = {
88 TF_RM_ELEM_CFG_HCAPI_BA, CFA_RESOURCE_TYPE_P4_SP_MAC_IPV4,
91 [TF_DIR_RX][TF_TBL_TYPE_ACT_SP_SMAC_IPV6] = {
92 TF_RM_ELEM_CFG_HCAPI_BA, CFA_RESOURCE_TYPE_P4_SP_MAC_IPV6,
95 [TF_DIR_RX][TF_TBL_TYPE_ACT_STATS_64] = {
96 TF_RM_ELEM_CFG_HCAPI_BA, CFA_RESOURCE_TYPE_P4_COUNTER_64B,
99 [TF_DIR_RX][TF_TBL_TYPE_ACT_MODIFY_IPV4] = {
100 TF_RM_ELEM_CFG_HCAPI_BA, CFA_RESOURCE_TYPE_P4_NAT_IPV4,
103 [TF_DIR_RX][TF_TBL_TYPE_METER_PROF] = {
104 TF_RM_ELEM_CFG_HCAPI_BA, CFA_RESOURCE_TYPE_P4_METER_PROF,
107 [TF_DIR_RX][TF_TBL_TYPE_METER_INST] = {
108 TF_RM_ELEM_CFG_HCAPI_BA, CFA_RESOURCE_TYPE_P4_METER,
111 [TF_DIR_RX][TF_TBL_TYPE_MIRROR_CONFIG] = {
112 TF_RM_ELEM_CFG_HCAPI_BA, CFA_RESOURCE_TYPE_P4_MIRROR,
115 [TF_DIR_TX][TF_TBL_TYPE_FULL_ACT_RECORD] = {
116 TF_RM_ELEM_CFG_HCAPI_BA, CFA_RESOURCE_TYPE_P4_FULL_ACTION,
119 [TF_DIR_TX][TF_TBL_TYPE_MCAST_GROUPS] = {
120 TF_RM_ELEM_CFG_HCAPI_BA, CFA_RESOURCE_TYPE_P4_MCG,
123 [TF_DIR_TX][TF_TBL_TYPE_ACT_ENCAP_8B] = {
124 TF_RM_ELEM_CFG_HCAPI_BA, CFA_RESOURCE_TYPE_P4_ENCAP_8B,
127 [TF_DIR_TX][TF_TBL_TYPE_ACT_ENCAP_16B] = {
128 TF_RM_ELEM_CFG_HCAPI_BA, CFA_RESOURCE_TYPE_P4_ENCAP_16B,
131 [TF_DIR_TX][TF_TBL_TYPE_ACT_ENCAP_64B] = {
132 TF_RM_ELEM_CFG_HCAPI_BA, CFA_RESOURCE_TYPE_P4_ENCAP_64B,
135 [TF_DIR_TX][TF_TBL_TYPE_ACT_SP_SMAC] = {
136 TF_RM_ELEM_CFG_HCAPI_BA, CFA_RESOURCE_TYPE_P4_SP_MAC,
139 [TF_DIR_TX][TF_TBL_TYPE_ACT_SP_SMAC_IPV4] = {
140 TF_RM_ELEM_CFG_HCAPI_BA, CFA_RESOURCE_TYPE_P4_SP_MAC_IPV4,
143 [TF_DIR_TX][TF_TBL_TYPE_ACT_SP_SMAC_IPV6] = {
144 TF_RM_ELEM_CFG_HCAPI_BA, CFA_RESOURCE_TYPE_P4_SP_MAC_IPV6,
147 [TF_DIR_TX][TF_TBL_TYPE_ACT_STATS_64] = {
148 TF_RM_ELEM_CFG_HCAPI_BA, CFA_RESOURCE_TYPE_P4_COUNTER_64B,
151 [TF_DIR_TX][TF_TBL_TYPE_ACT_MODIFY_IPV4] = {
152 TF_RM_ELEM_CFG_HCAPI_BA, CFA_RESOURCE_TYPE_P4_NAT_IPV4,
155 [TF_DIR_TX][TF_TBL_TYPE_METER_PROF] = {
156 TF_RM_ELEM_CFG_HCAPI_BA, CFA_RESOURCE_TYPE_P4_METER_PROF,
159 [TF_DIR_TX][TF_TBL_TYPE_METER_INST] = {
160 TF_RM_ELEM_CFG_HCAPI_BA, CFA_RESOURCE_TYPE_P4_METER,
163 [TF_DIR_TX][TF_TBL_TYPE_MIRROR_CONFIG] = {
164 TF_RM_ELEM_CFG_HCAPI_BA, CFA_RESOURCE_TYPE_P4_MIRROR,
170 * Device specific function that retrieves the MAX number of HCAPI
171 * types the device supports.
174 * Pointer to TF handle
177 * Pointer to the MAX number of CFA resource types supported
180 * - (0) if successful.
181 * - (-EINVAL) on failure.
184 tf_dev_p4_get_max_types(struct tf *tfp,
187 if (max_types == NULL || tfp == NULL)
190 *max_types = CFA_RESOURCE_TYPE_P4_LAST + 1;
195 * Device specific function that retrieves a human readable
196 * string to identify a CFA resource type.
199 * Pointer to TF handle
202 * HCAPI CFA resource id
208 * - (0) if successful.
209 * - (-EINVAL) on failure.
212 tf_dev_p4_get_resource_str(struct tf *tfp __rte_unused,
213 uint16_t resource_id,
214 const char **resource_str)
216 if (resource_str == NULL)
219 if (resource_id > CFA_RESOURCE_TYPE_P4_LAST)
222 *resource_str = tf_resource_str_p4[resource_id];
228 * Device specific function that set the WC TCAM slices the
232 * Pointer to TF handle
234 * [in] num_slices_per_row
235 * The WC TCAM row slice configuration
238 * - (0) if successful.
239 * - (-EINVAL) on failure.
242 tf_dev_p4_set_tcam_slice_info(struct tf *tfp __rte_unused,
243 enum tf_wc_num_slice num_slices_per_row)
245 switch (num_slices_per_row) {
246 case TF_WC_TCAM_1_SLICE_PER_ROW:
247 case TF_WC_TCAM_2_SLICE_PER_ROW:
248 case TF_WC_TCAM_4_SLICE_PER_ROW:
249 g_wc_num_slices_per_row = num_slices_per_row;
259 * Device specific function that retrieves the TCAM slices the
263 * Pointer to TF handle
271 * [out] num_slices_per_row
272 * Pointer to the WC TCAM row slice configuration
275 * - (0) if successful.
276 * - (-EINVAL) on failure.
279 tf_dev_p4_get_tcam_slice_info(struct tf *tfp __rte_unused,
280 enum tf_tcam_tbl_type type,
282 uint16_t *num_slices_per_row)
284 /* Single slice support */
285 #define CFA_P4_WC_TCAM_SLICE_SIZE 12
287 if (type == TF_TCAM_TBL_TYPE_WC_TCAM) {
288 *num_slices_per_row = g_wc_num_slices_per_row;
289 if (key_sz > *num_slices_per_row * CFA_P4_WC_TCAM_SLICE_SIZE)
291 } else { /* for other type of tcam */
292 *num_slices_per_row = 1;
299 tf_dev_p4_map_parif(struct tf *tfp __rte_unused,
300 uint16_t parif_bitmask,
304 uint16_t sz_in_bytes)
306 uint32_t parif_pf[2] = { 0 };
307 uint32_t parif_pf_mask[2] = { 0 };
311 if (sz_in_bytes != sizeof(uint64_t))
314 for (parif = 0; parif < TF_DEV_P4_PARIF_MAX; parif++) {
315 if (parif_bitmask & (1UL << parif)) {
318 parif_pf_mask[0] |= TF_DEV_P4_PF_MASK << shift;
319 parif_pf[0] |= pf << shift;
321 shift = 4 * (parif - 8);
322 parif_pf_mask[1] |= TF_DEV_P4_PF_MASK << shift;
323 parif_pf[1] |= pf << shift;
327 tfp_memcpy(data, parif_pf, sz_in_bytes);
328 tfp_memcpy(mask, parif_pf_mask, sz_in_bytes);
334 * Device specific function that retrieves the increment
335 * required for certain table types in a shared session
341 * pointer to parms structure
344 * - (0) if successful.
345 * - (-EINVAL) on failure.
347 static int tf_dev_p4_get_shared_tbl_increment(struct tf *tfp __rte_unused,
348 struct tf_get_shared_tbl_increment_parms *parms)
350 parms->increment_cnt = 1;
353 static int tf_dev_p4_get_mailbox(void)
358 static int tf_dev_p4_word_align(uint16_t size)
360 return ((((size) + 31) >> 5) * 4);
364 * Indicates whether the index table type is SRAM managed
367 * Pointer to TF handle
370 * Truflow index table type, e.g. TF_TYPE_FULL_ACT_RECORD
373 * - (0) if the table is not managed by the SRAM manager
374 * - (1) if the table is managed by the SRAM manager
376 static bool tf_dev_p4_is_sram_managed(struct tf *tfp __rte_unused,
377 enum tf_tbl_type type __rte_unused)
383 * Device specific function that maps the hcapi resource types
387 * CFA resource type bitmap
390 * Pointer to identifier type bitmap
393 * Pointer to tcam type bitmap
396 * Pointer to table type bitmap
399 * Pointer to em type bitmap
402 * - (0) if successful.
403 * - (-EINVAL) on failure.
405 static int tf_dev_p4_map_hcapi_caps(uint64_t hcapi_caps,
406 uint32_t *ident_caps,
418 for (i = 0; i <= CFA_RESOURCE_TYPE_P4_LAST; i++) {
419 if (hcapi_caps & 1ULL << i) {
420 switch (tf_hcapi_res_map_p4[i].module_type) {
421 case TF_MODULE_TYPE_IDENTIFIER:
422 *ident_caps |= tf_hcapi_res_map_p4[i].type_caps;
424 case TF_MODULE_TYPE_TABLE:
425 *tbl_caps |= tf_hcapi_res_map_p4[i].type_caps;
427 case TF_MODULE_TYPE_TCAM:
428 *tcam_caps |= tf_hcapi_res_map_p4[i].type_caps;
430 case TF_MODULE_TYPE_EM:
431 *em_caps |= tf_hcapi_res_map_p4[i].type_caps;
443 * Truflow P4 device specific functions
445 const struct tf_dev_ops tf_dev_ops_p4_init = {
446 .tf_dev_get_max_types = tf_dev_p4_get_max_types,
447 .tf_dev_get_resource_str = tf_dev_p4_get_resource_str,
448 .tf_dev_set_tcam_slice_info = tf_dev_p4_set_tcam_slice_info,
449 .tf_dev_get_tcam_slice_info = tf_dev_p4_get_tcam_slice_info,
450 .tf_dev_alloc_ident = NULL,
451 .tf_dev_free_ident = NULL,
452 .tf_dev_search_ident = NULL,
453 .tf_dev_get_ident_resc_info = NULL,
454 .tf_dev_get_tbl_info = NULL,
455 .tf_dev_is_sram_managed = tf_dev_p4_is_sram_managed,
456 .tf_dev_alloc_ext_tbl = NULL,
457 .tf_dev_alloc_tbl = NULL,
458 .tf_dev_alloc_sram_tbl = NULL,
459 .tf_dev_free_ext_tbl = NULL,
460 .tf_dev_free_tbl = NULL,
461 .tf_dev_free_sram_tbl = NULL,
462 .tf_dev_set_tbl = NULL,
463 .tf_dev_set_ext_tbl = NULL,
464 .tf_dev_set_sram_tbl = NULL,
465 .tf_dev_get_tbl = NULL,
466 .tf_dev_get_sram_tbl = NULL,
467 .tf_dev_get_bulk_tbl = NULL,
468 .tf_dev_get_bulk_sram_tbl = NULL,
469 .tf_dev_get_shared_tbl_increment = tf_dev_p4_get_shared_tbl_increment,
470 .tf_dev_get_tbl_resc_info = NULL,
471 .tf_dev_alloc_tcam = NULL,
472 .tf_dev_free_tcam = NULL,
473 .tf_dev_alloc_search_tcam = NULL,
474 .tf_dev_set_tcam = NULL,
475 .tf_dev_get_tcam = NULL,
476 .tf_dev_get_tcam_resc_info = NULL,
477 .tf_dev_insert_int_em_entry = NULL,
478 .tf_dev_delete_int_em_entry = NULL,
479 .tf_dev_insert_ext_em_entry = NULL,
480 .tf_dev_delete_ext_em_entry = NULL,
481 .tf_dev_get_em_resc_info = NULL,
482 .tf_dev_alloc_tbl_scope = NULL,
483 .tf_dev_map_tbl_scope = NULL,
484 .tf_dev_map_parif = NULL,
485 .tf_dev_free_tbl_scope = NULL,
486 .tf_dev_set_if_tbl = NULL,
487 .tf_dev_get_if_tbl = NULL,
488 .tf_dev_set_global_cfg = NULL,
489 .tf_dev_get_global_cfg = NULL,
490 .tf_dev_get_mailbox = tf_dev_p4_get_mailbox,
491 .tf_dev_word_align = NULL,
492 .tf_dev_map_hcapi_caps = tf_dev_p4_map_hcapi_caps,
493 .tf_dev_get_sram_resources = NULL,
494 .tf_dev_set_sram_policy = NULL,
495 .tf_dev_get_sram_policy = NULL,
499 * Truflow P4 device specific functions
501 const struct tf_dev_ops tf_dev_ops_p4 = {
502 .tf_dev_get_max_types = tf_dev_p4_get_max_types,
503 .tf_dev_get_resource_str = tf_dev_p4_get_resource_str,
504 .tf_dev_set_tcam_slice_info = tf_dev_p4_set_tcam_slice_info,
505 .tf_dev_get_tcam_slice_info = tf_dev_p4_get_tcam_slice_info,
506 .tf_dev_alloc_ident = tf_ident_alloc,
507 .tf_dev_free_ident = tf_ident_free,
508 .tf_dev_search_ident = tf_ident_search,
509 .tf_dev_get_ident_resc_info = tf_ident_get_resc_info,
510 .tf_dev_get_tbl_info = NULL,
511 .tf_dev_is_sram_managed = tf_dev_p4_is_sram_managed,
512 .tf_dev_alloc_tbl = tf_tbl_alloc,
513 .tf_dev_alloc_ext_tbl = tf_tbl_ext_alloc,
514 .tf_dev_alloc_sram_tbl = tf_tbl_alloc,
515 .tf_dev_free_tbl = tf_tbl_free,
516 .tf_dev_free_ext_tbl = tf_tbl_ext_free,
517 .tf_dev_free_sram_tbl = tf_tbl_free,
518 .tf_dev_set_tbl = tf_tbl_set,
519 .tf_dev_set_ext_tbl = tf_tbl_ext_common_set,
520 .tf_dev_set_sram_tbl = NULL,
521 .tf_dev_get_tbl = tf_tbl_get,
522 .tf_dev_get_sram_tbl = NULL,
523 .tf_dev_get_bulk_tbl = tf_tbl_bulk_get,
524 .tf_dev_get_bulk_sram_tbl = NULL,
525 .tf_dev_get_shared_tbl_increment = tf_dev_p4_get_shared_tbl_increment,
526 .tf_dev_get_tbl_resc_info = tf_tbl_get_resc_info,
527 #ifdef TF_TCAM_SHARED
528 .tf_dev_alloc_tcam = tf_tcam_shared_alloc,
529 .tf_dev_free_tcam = tf_tcam_shared_free,
530 .tf_dev_set_tcam = tf_tcam_shared_set,
531 .tf_dev_get_tcam = tf_tcam_shared_get,
532 .tf_dev_move_tcam = tf_tcam_shared_move_p4,
533 .tf_dev_clear_tcam = tf_tcam_shared_clear,
534 #else /* !TF_TCAM_SHARED */
535 .tf_dev_alloc_tcam = tf_tcam_alloc,
536 .tf_dev_free_tcam = tf_tcam_free,
537 .tf_dev_set_tcam = tf_tcam_set,
538 .tf_dev_get_tcam = tf_tcam_get,
540 .tf_dev_alloc_search_tcam = tf_tcam_alloc_search,
541 .tf_dev_get_tcam_resc_info = tf_tcam_get_resc_info,
542 .tf_dev_insert_int_em_entry = tf_em_insert_int_entry,
543 .tf_dev_delete_int_em_entry = tf_em_delete_int_entry,
544 .tf_dev_insert_ext_em_entry = tf_em_insert_ext_entry,
545 .tf_dev_delete_ext_em_entry = tf_em_delete_ext_entry,
546 .tf_dev_get_em_resc_info = tf_em_get_resc_info,
547 .tf_dev_alloc_tbl_scope = tf_em_ext_common_alloc,
548 .tf_dev_map_tbl_scope = tf_em_ext_map_tbl_scope,
549 .tf_dev_map_parif = tf_dev_p4_map_parif,
550 .tf_dev_free_tbl_scope = tf_em_ext_common_free,
551 .tf_dev_set_if_tbl = tf_if_tbl_set,
552 .tf_dev_get_if_tbl = tf_if_tbl_get,
553 .tf_dev_set_global_cfg = tf_global_cfg_set,
554 .tf_dev_get_global_cfg = tf_global_cfg_get,
555 .tf_dev_get_mailbox = tf_dev_p4_get_mailbox,
556 .tf_dev_word_align = tf_dev_p4_word_align,
557 .tf_dev_cfa_key_hash = hcapi_cfa_p4_key_hash,
558 .tf_dev_map_hcapi_caps = tf_dev_p4_map_hcapi_caps,
559 .tf_dev_get_sram_resources = NULL,
560 .tf_dev_set_sram_policy = NULL,
561 .tf_dev_get_sram_policy = NULL,