net/bnxt: refactor flow parser in ULP
[dpdk.git] / drivers / net / bnxt / tf_ulp / ulp_matcher.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2021 Broadcom
3  * All rights reserved.
4  */
5
6 #include "ulp_matcher.h"
7 #include "ulp_utils.h"
8
9 /* Utility function to calculate the class matcher hash */
10 static uint32_t
11 ulp_matcher_class_hash_calculate(uint64_t hi_sig, uint64_t lo_sig)
12 {
13         uint64_t hash;
14
15         hi_sig |= ((hi_sig % BNXT_ULP_CLASS_HID_HIGH_PRIME) <<
16                    BNXT_ULP_CLASS_HID_SHFTL);
17         lo_sig |= ((lo_sig % BNXT_ULP_CLASS_HID_LOW_PRIME) <<
18                    (BNXT_ULP_CLASS_HID_SHFTL + 2));
19         hash = hi_sig ^ lo_sig;
20         hash = (hash >> BNXT_ULP_CLASS_HID_SHFTR) & BNXT_ULP_CLASS_HID_MASK;
21         return (uint32_t)hash;
22 }
23
24 /* Utility function to calculate the action matcher hash */
25 static uint32_t
26 ulp_matcher_action_hash_calculate(uint64_t hi_sig)
27 {
28         uint64_t hash;
29
30         hi_sig |= ((hi_sig % BNXT_ULP_ACT_HID_HIGH_PRIME) <<
31                    BNXT_ULP_ACT_HID_SHFTL);
32         hash = hi_sig;
33         hash = (hash >> BNXT_ULP_ACT_HID_SHFTR) & BNXT_ULP_ACT_HID_MASK;
34         return (uint32_t)hash;
35 }
36
37 /*
38  * Function to handle the matching of RTE Flows and validating
39  * the pattern masks against the flow templates.
40  */
41 int32_t
42 ulp_matcher_pattern_match(struct ulp_rte_parser_params *params,
43                           uint32_t *class_id)
44 {
45         struct bnxt_ulp_class_match_info *class_match;
46         uint32_t class_hid;
47         uint8_t vf_to_vf;
48         uint16_t tmpl_id;
49
50         /* Get vf to vf flow */
51         vf_to_vf = ULP_COMP_FLD_IDX_RD(params, BNXT_ULP_CF_IDX_VF_TO_VF);
52
53         /* calculate the hash of the given flow */
54         class_hid = ulp_matcher_class_hash_calculate(params->hdr_bitmap.bits,
55                                                      params->fld_s_bitmap.bits);
56
57         /* validate the calculate hash values */
58         if (class_hid >= BNXT_ULP_CLASS_SIG_TBL_MAX_SZ)
59                 goto error;
60         tmpl_id = ulp_class_sig_tbl[class_hid];
61         if (!tmpl_id)
62                 goto error;
63
64         class_match = &ulp_class_match_list[tmpl_id];
65         if (ULP_BITMAP_CMP(&params->hdr_bitmap, &class_match->hdr_sig)) {
66                 BNXT_TF_DBG(DEBUG, "Proto Header does not match\n");
67                 goto error;
68         }
69         if (ULP_BITMAP_CMP(&params->fld_s_bitmap, &class_match->field_sig)) {
70                 BNXT_TF_DBG(DEBUG, "Field signature does not match\n");
71                 goto error;
72         }
73         if (vf_to_vf != class_match->act_vnic) {
74                 BNXT_TF_DBG(DEBUG, "Vnic Match failed\n");
75                 goto error;
76         }
77         BNXT_TF_DBG(DEBUG, "Found matching pattern template %d\n",
78                     class_match->class_tid);
79         *class_id = class_match->class_tid;
80         params->hdr_sig_id = class_match->hdr_sig_id;
81         params->flow_sig_id = class_match->flow_sig_id;
82         params->flow_pattern_id = class_match->flow_pattern_id;
83         return BNXT_TF_RC_SUCCESS;
84
85 error:
86         BNXT_TF_DBG(DEBUG, "Did not find any matching template\n");
87         *class_id = 0;
88         return BNXT_TF_RC_ERROR;
89 }
90
91 /*
92  * Function to handle the matching of RTE Flows and validating
93  * the action against the flow templates.
94  */
95 int32_t
96 ulp_matcher_action_match(struct ulp_rte_parser_params *params,
97                          uint32_t *act_id)
98 {
99         uint32_t act_hid;
100         uint16_t tmpl_id;
101         struct bnxt_ulp_act_match_info *act_match;
102
103         /* calculate the hash of the given flow action */
104         act_hid = ulp_matcher_action_hash_calculate(params->act_bitmap.bits);
105
106         /* validate the calculate hash values */
107         if (act_hid >= BNXT_ULP_ACT_SIG_TBL_MAX_SZ)
108                 goto error;
109         tmpl_id = ulp_act_sig_tbl[act_hid];
110         if (!tmpl_id)
111                 goto error;
112
113         act_match = &ulp_act_match_list[tmpl_id];
114         if (ULP_BITMAP_CMP(&params->act_bitmap, &act_match->act_sig)) {
115                 BNXT_TF_DBG(DEBUG, "Action Header does not match\n");
116                 goto error;
117         }
118         *act_id = act_match->act_tid;
119         params->act_pattern_id = act_match->act_pattern_id;
120         BNXT_TF_DBG(DEBUG, "Found matching action template %u\n", *act_id);
121         return BNXT_TF_RC_SUCCESS;
122
123 error:
124         BNXT_TF_DBG(DEBUG, "Did not find any matching action template\n");
125         *act_id = 0;
126         return BNXT_TF_RC_ERROR;
127 }