net/bnxt: modify table processing
[dpdk.git] / drivers / net / bnxt / tf_core / tf_device_p4.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2021 Broadcom
3  * All rights reserved.
4  */
5
6 #include <rte_common.h>
7
8 #include "cfa_resource_types.h"
9 #include "tf_device.h"
10 #include "tf_identifier.h"
11 #include "tf_tbl.h"
12 #include "tf_tcam.h"
13 #ifdef TF_TCAM_SHARED
14 #include "tf_tcam_shared.h"
15 #endif /* TF_TCAM_SHARED */
16 #include "tf_em.h"
17 #include "tf_if_tbl.h"
18 #include "tfp.h"
19 #include "tf_msg_common.h"
20
21 #define TF_DEV_P4_PARIF_MAX 16
22 #define TF_DEV_P4_PF_MASK 0xfUL
23
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",
60 };
61
62 /**
63  * Device specific function that retrieves the MAX number of HCAPI
64  * types the device supports.
65  *
66  * [in] tfp
67  *   Pointer to TF handle
68  *
69  * [out] max_types
70  *   Pointer to the MAX number of CFA resource types supported
71  *
72  * Returns
73  *   - (0) if successful.
74  *   - (-EINVAL) on failure.
75  */
76 static int
77 tf_dev_p4_get_max_types(struct tf *tfp,
78                         uint16_t *max_types)
79 {
80         if (max_types == NULL || tfp == NULL)
81                 return -EINVAL;
82
83         *max_types = CFA_RESOURCE_TYPE_P4_LAST + 1;
84
85         return 0;
86 }
87 /**
88  * Device specific function that retrieves a human readable
89  * string to identify a CFA resource type.
90  *
91  * [in] tfp
92  *   Pointer to TF handle
93  *
94  * [in] resource_id
95  *   HCAPI CFA resource id
96  *
97  * [out] resource_str
98  *   Resource string
99  *
100  * Returns
101  *   - (0) if successful.
102  *   - (-EINVAL) on failure.
103  */
104 static int
105 tf_dev_p4_get_resource_str(struct tf *tfp __rte_unused,
106                            uint16_t resource_id,
107                            const char **resource_str)
108 {
109         if (resource_str == NULL)
110                 return -EINVAL;
111
112         if (resource_id > CFA_RESOURCE_TYPE_P4_LAST)
113                 return -EINVAL;
114
115         *resource_str = tf_resource_str_p4[resource_id];
116
117         return 0;
118 }
119
120 /**
121  * Device specific function that retrieves the WC TCAM slices the
122  * device supports.
123  *
124  * [in] tfp
125  *   Pointer to TF handle
126  *
127  * [out] slice_size
128  *   Pointer to the WC TCAM slice size
129  *
130  * [out] num_slices_per_row
131  *   Pointer to the WC TCAM row slice configuration
132  *
133  * Returns
134  *   - (0) if successful.
135  *   - (-EINVAL) on failure.
136  */
137 static int
138 tf_dev_p4_get_tcam_slice_info(struct tf *tfp __rte_unused,
139                               enum tf_tcam_tbl_type type,
140                               uint16_t key_sz,
141                               uint16_t *num_slices_per_row)
142 {
143 /* Single slice support */
144 #define CFA_P4_WC_TCAM_SLICES_PER_ROW 1
145 #define CFA_P4_WC_TCAM_SLICE_SIZE     12
146
147         if (type == TF_TCAM_TBL_TYPE_WC_TCAM) {
148                 *num_slices_per_row = CFA_P4_WC_TCAM_SLICES_PER_ROW;
149                 if (key_sz > *num_slices_per_row * CFA_P4_WC_TCAM_SLICE_SIZE)
150                         return -ENOTSUP;
151         } else { /* for other type of tcam */
152                 *num_slices_per_row = 1;
153         }
154
155         return 0;
156 }
157
158 static int
159 tf_dev_p4_map_parif(struct tf *tfp __rte_unused,
160                     uint16_t parif_bitmask,
161                     uint16_t pf,
162                     uint8_t *data,
163                     uint8_t *mask,
164                     uint16_t sz_in_bytes)
165 {
166         uint32_t parif_pf[2] = { 0 };
167         uint32_t parif_pf_mask[2] = { 0 };
168         uint32_t parif;
169         uint32_t shift;
170
171         if (sz_in_bytes != sizeof(uint64_t))
172                 return -ENOTSUP;
173
174         for (parif = 0; parif < TF_DEV_P4_PARIF_MAX; parif++) {
175                 if (parif_bitmask & (1UL << parif)) {
176                         if (parif < 8) {
177                                 shift = 4 * parif;
178                                 parif_pf_mask[0] |= TF_DEV_P4_PF_MASK << shift;
179                                 parif_pf[0] |= pf << shift;
180                         } else {
181                                 shift = 4 * (parif - 8);
182                                 parif_pf_mask[1] |= TF_DEV_P4_PF_MASK << shift;
183                                 parif_pf[1] |= pf << shift;
184                         }
185                 }
186         }
187         tfp_memcpy(data, parif_pf, sz_in_bytes);
188         tfp_memcpy(mask, parif_pf_mask, sz_in_bytes);
189
190         return 0;
191 }
192
193 /**
194  * Device specific function that retrieves the increment
195  * required for certain table types in a shared session
196  *
197  * [in] tfp
198  *  tf handle
199  *
200  * [in/out] parms
201  *   pointer to parms structure
202  *
203  * Returns
204  *   - (0) if successful.
205  *   - (-EINVAL) on failure.
206  */
207 static int tf_dev_p4_get_shared_tbl_increment(struct tf *tfp __rte_unused,
208                                 struct tf_get_shared_tbl_increment_parms *parms)
209 {
210         parms->increment_cnt = 1;
211         return 0;
212 }
213 static int tf_dev_p4_get_mailbox(void)
214 {
215         return TF_KONG_MB;
216 }
217
218 static int tf_dev_p4_word_align(uint16_t size)
219 {
220         return ((((size) + 31) >> 5) * 4);
221 }
222
223 /**
224  * Truflow P4 device specific functions
225  */
226 const struct tf_dev_ops tf_dev_ops_p4_init = {
227         .tf_dev_get_max_types = tf_dev_p4_get_max_types,
228         .tf_dev_get_resource_str = tf_dev_p4_get_resource_str,
229         .tf_dev_get_tcam_slice_info = tf_dev_p4_get_tcam_slice_info,
230         .tf_dev_alloc_ident = NULL,
231         .tf_dev_free_ident = NULL,
232         .tf_dev_search_ident = NULL,
233         .tf_dev_get_ident_resc_info = NULL,
234         .tf_dev_get_tbl_info = NULL,
235         .tf_dev_alloc_ext_tbl = NULL,
236         .tf_dev_alloc_tbl = NULL,
237         .tf_dev_free_ext_tbl = NULL,
238         .tf_dev_free_tbl = NULL,
239         .tf_dev_alloc_search_tbl = NULL,
240         .tf_dev_set_tbl = NULL,
241         .tf_dev_set_ext_tbl = NULL,
242         .tf_dev_get_tbl = NULL,
243         .tf_dev_get_bulk_tbl = NULL,
244         .tf_dev_get_shared_tbl_increment = tf_dev_p4_get_shared_tbl_increment,
245         .tf_dev_get_tbl_resc_info = NULL,
246         .tf_dev_alloc_tcam = NULL,
247         .tf_dev_free_tcam = NULL,
248         .tf_dev_alloc_search_tcam = NULL,
249         .tf_dev_set_tcam = NULL,
250         .tf_dev_get_tcam = NULL,
251         .tf_dev_get_tcam_resc_info = NULL,
252         .tf_dev_insert_int_em_entry = NULL,
253         .tf_dev_delete_int_em_entry = NULL,
254         .tf_dev_insert_ext_em_entry = NULL,
255         .tf_dev_delete_ext_em_entry = NULL,
256         .tf_dev_get_em_resc_info = NULL,
257         .tf_dev_alloc_tbl_scope = NULL,
258         .tf_dev_map_tbl_scope = NULL,
259         .tf_dev_map_parif = NULL,
260         .tf_dev_free_tbl_scope = NULL,
261         .tf_dev_set_if_tbl = NULL,
262         .tf_dev_get_if_tbl = NULL,
263         .tf_dev_set_global_cfg = NULL,
264         .tf_dev_get_global_cfg = NULL,
265         .tf_dev_get_mailbox = tf_dev_p4_get_mailbox,
266         .tf_dev_word_align = NULL,
267 };
268
269 /**
270  * Truflow P4 device specific functions
271  */
272 const struct tf_dev_ops tf_dev_ops_p4 = {
273         .tf_dev_get_max_types = tf_dev_p4_get_max_types,
274         .tf_dev_get_resource_str = tf_dev_p4_get_resource_str,
275         .tf_dev_get_tcam_slice_info = tf_dev_p4_get_tcam_slice_info,
276         .tf_dev_alloc_ident = tf_ident_alloc,
277         .tf_dev_free_ident = tf_ident_free,
278         .tf_dev_search_ident = tf_ident_search,
279         .tf_dev_get_ident_resc_info = tf_ident_get_resc_info,
280         .tf_dev_get_tbl_info = NULL,
281         .tf_dev_alloc_tbl = tf_tbl_alloc,
282         .tf_dev_alloc_ext_tbl = tf_tbl_ext_alloc,
283         .tf_dev_free_tbl = tf_tbl_free,
284         .tf_dev_free_ext_tbl = tf_tbl_ext_free,
285         .tf_dev_alloc_search_tbl = tf_tbl_alloc_search,
286         .tf_dev_set_tbl = tf_tbl_set,
287         .tf_dev_set_ext_tbl = tf_tbl_ext_common_set,
288         .tf_dev_get_tbl = tf_tbl_get,
289         .tf_dev_get_bulk_tbl = tf_tbl_bulk_get,
290         .tf_dev_get_shared_tbl_increment = tf_dev_p4_get_shared_tbl_increment,
291         .tf_dev_get_tbl_resc_info = tf_tbl_get_resc_info,
292 #ifdef TF_TCAM_SHARED
293         .tf_dev_alloc_tcam = tf_tcam_shared_alloc,
294         .tf_dev_free_tcam = tf_tcam_shared_free,
295         .tf_dev_set_tcam = tf_tcam_shared_set,
296         .tf_dev_get_tcam = tf_tcam_shared_get,
297         .tf_dev_move_tcam = tf_tcam_shared_move_p4,
298         .tf_dev_clear_tcam = tf_tcam_shared_clear,
299 #else /* !TF_TCAM_SHARED */
300         .tf_dev_alloc_tcam = tf_tcam_alloc,
301         .tf_dev_free_tcam = tf_tcam_free,
302         .tf_dev_set_tcam = tf_tcam_set,
303         .tf_dev_get_tcam = tf_tcam_get,
304 #endif
305         .tf_dev_alloc_search_tcam = tf_tcam_alloc_search,
306         .tf_dev_get_tcam_resc_info = tf_tcam_get_resc_info,
307         .tf_dev_insert_int_em_entry = tf_em_insert_int_entry,
308         .tf_dev_delete_int_em_entry = tf_em_delete_int_entry,
309         .tf_dev_insert_ext_em_entry = tf_em_insert_ext_entry,
310         .tf_dev_delete_ext_em_entry = tf_em_delete_ext_entry,
311         .tf_dev_get_em_resc_info = tf_em_get_resc_info,
312         .tf_dev_alloc_tbl_scope = tf_em_ext_common_alloc,
313         .tf_dev_map_tbl_scope = tf_em_ext_map_tbl_scope,
314         .tf_dev_map_parif = tf_dev_p4_map_parif,
315         .tf_dev_free_tbl_scope = tf_em_ext_common_free,
316         .tf_dev_set_if_tbl = tf_if_tbl_set,
317         .tf_dev_get_if_tbl = tf_if_tbl_get,
318         .tf_dev_set_global_cfg = tf_global_cfg_set,
319         .tf_dev_get_global_cfg = tf_global_cfg_get,
320         .tf_dev_get_mailbox = tf_dev_p4_get_mailbox,
321         .tf_dev_word_align = tf_dev_p4_word_align,
322         .tf_dev_cfa_key_hash = hcapi_cfa_p4_key_hash
323 };