net/bnxt: add hierarchical flow counters
[dpdk.git] / drivers / net / bnxt / tf_ulp / ulp_flow_db.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2019 Broadcom
3  * All rights reserved.
4  */
5
6 #ifndef _ULP_FLOW_DB_H_
7 #define _ULP_FLOW_DB_H_
8
9 #include "bnxt_ulp.h"
10 #include "ulp_template_db_enum.h"
11 #include "ulp_mapper.h"
12
13 #define BNXT_FLOW_DB_DEFAULT_NUM_FLOWS          512
14 #define BNXT_FLOW_DB_DEFAULT_NUM_RESOURCES      8
15
16 /*
17  * Structure for the flow database resource information
18  * The below structure is based on the below paritions
19  * nxt_resource_idx = dir[31],resource_func_upper[30:28],nxt_resource_idx[27:0]
20  * If resource_func is EM_TBL then use resource_em_handle.
21  * Else the other part of the union is used and
22  * resource_func is resource_func_upper[30:28] << 5 | resource_func_lower
23  */
24 struct ulp_fdb_resource_info {
25         /* Points to next resource in the chained list. */
26         uint32_t                        nxt_resource_idx;
27         union {
28                 uint64_t                resource_em_handle;
29                 struct {
30                         uint8_t         resource_func_lower;
31                         uint8_t         resource_type;
32                         uint8_t         resource_sub_type;
33                         uint8_t         reserved;
34                         uint32_t        resource_hndl;
35                 };
36         };
37 };
38
39 /* Structure for the flow database resource information. */
40 struct bnxt_ulp_flow_tbl {
41         /* Flow tbl is the resource object list for each flow id. */
42         struct ulp_fdb_resource_info    *flow_resources;
43
44         /* Flow table stack to track free list of resources. */
45         uint32_t        *flow_tbl_stack;
46         uint32_t        head_index;
47         uint32_t        tail_index;
48
49         /* Table to track the active flows. */
50         uint64_t        *active_reg_flows;
51         uint64_t        *active_dflt_flows;
52         uint32_t        num_flows;
53         uint32_t        num_resources;
54 };
55
56 /* Structure to maintain parent-child flow relationships */
57 struct ulp_fdb_parent_info {
58         uint32_t        parent_fid;
59         uint32_t        counter_acc;
60         uint64_t        pkt_count;
61         uint64_t        byte_count;
62         uint64_t        *child_fid_bitset;
63 };
64
65 /* Structure to maintain parent-child flow relationships */
66 struct ulp_fdb_parent_child_db {
67         struct ulp_fdb_parent_info      *parent_flow_tbl;
68         uint32_t                        child_bitset_size;
69         uint32_t                        entries_count;
70         uint8_t                         *parent_flow_tbl_mem;
71 };
72
73 /* Structure for the flow database resource information. */
74 struct bnxt_ulp_flow_db {
75         struct bnxt_ulp_flow_tbl        flow_tbl;
76         uint16_t                        *func_id_tbl;
77         uint32_t                        func_id_tbl_size;
78         struct ulp_fdb_parent_child_db  parent_child_db;
79 };
80
81 /* flow db resource params to add resources */
82 struct ulp_flow_db_res_params {
83         enum tf_dir                     direction;
84         enum bnxt_ulp_resource_func     resource_func;
85         uint8_t                         resource_type;
86         uint8_t                         resource_sub_type;
87         uint8_t                         reserved;
88         uint8_t                         critical_resource;
89         uint64_t                        resource_hndl;
90 };
91
92 /*
93  * Initialize the flow database. Memory is allocated in this
94  * call and assigned to the flow database.
95  *
96  * ulp_ctxt [in] Ptr to ulp context
97  *
98  * Returns 0 on success or negative number on failure.
99  */
100 int32_t ulp_flow_db_init(struct bnxt_ulp_context *ulp_ctxt);
101
102 /*
103  * Deinitialize the flow database. Memory is deallocated in
104  * this call and all flows should have been purged before this
105  * call.
106  *
107  * ulp_ctxt [in] Ptr to ulp context
108  *
109  * Returns 0 on success.
110  */
111 int32_t ulp_flow_db_deinit(struct bnxt_ulp_context *ulp_ctxt);
112
113 /*
114  * Allocate the flow database entry
115  *
116  * ulp_ctxt [in] Ptr to ulp_context
117  * tbl_idx [in] Specify it is regular or default flow
118  * func_id [in] The function id of the device.Valid only for regular flows.
119  * fid [out] The index to the flow entry
120  *
121  * returns 0 on success and negative on failure.
122  */
123 int32_t
124 ulp_flow_db_fid_alloc(struct bnxt_ulp_context *ulp_ctxt,
125                       enum bnxt_ulp_fdb_type flow_type,
126                       uint16_t func_id,
127                       uint32_t *fid);
128
129 /*
130  * Allocate the flow database entry.
131  * The params->critical_resource has to be set to 0 to allocate a new resource.
132  *
133  * ulp_ctxt [in] Ptr to ulp_context
134  * tbl_idx [in] Specify it is regular or default flow
135  * fid [in] The index to the flow entry
136  * params [in] The contents to be copied into resource
137  *
138  * returns 0 on success and negative on failure.
139  */
140 int32_t
141 ulp_flow_db_resource_add(struct bnxt_ulp_context *ulp_ctxt,
142                          enum bnxt_ulp_fdb_type flow_type,
143                          uint32_t fid,
144                          struct ulp_flow_db_res_params *params);
145
146 /*
147  * Free the flow database entry.
148  * The params->critical_resource has to be set to 1 to free the first resource.
149  *
150  * ulp_ctxt [in] Ptr to ulp_context
151  * tbl_idx [in] Specify it is regular or default flow
152  * fid [in] The index to the flow entry
153  * params [in/out] The contents to be copied into params.
154  * Only the critical_resource needs to be set by the caller.
155  *
156  * Returns 0 on success and negative on failure.
157  */
158 int32_t
159 ulp_flow_db_resource_del(struct bnxt_ulp_context *ulp_ctxt,
160                          enum bnxt_ulp_fdb_type flow_type,
161                          uint32_t fid,
162                          struct ulp_flow_db_res_params *params);
163
164 /*
165  * Free the flow database entry
166  *
167  * ulp_ctxt [in] Ptr to ulp_context
168  * tbl_idx [in] Specify it is regular or default flow
169  * fid [in] The index to the flow entry
170  *
171  * returns 0 on success and negative on failure.
172  */
173 int32_t
174 ulp_flow_db_fid_free(struct bnxt_ulp_context *ulp_ctxt,
175                      enum bnxt_ulp_fdb_type tbl_idx,
176                      uint32_t fid);
177
178 /*
179  *Get the flow database entry details
180  *
181  * ulp_ctxt [in] Ptr to ulp_context
182  * tbl_idx [in] Specify it is regular or default flow
183  * fid [in] The index to the flow entry
184  * nxt_idx [in/out] the index to the next entry
185  * params [out] The contents to be copied into params.
186  *
187  * returns 0 on success and negative on failure.
188  */
189 int32_t
190 ulp_flow_db_resource_get(struct bnxt_ulp_context *ulp_ctxt,
191                          enum bnxt_ulp_fdb_type flow_type,
192                          uint32_t fid,
193                          uint32_t *nxt_idx,
194                          struct ulp_flow_db_res_params *params);
195
196 /*
197  * Flush all flows in the flow database.
198  *
199  * ulp_ctxt [in] Ptr to ulp context
200  * tbl_idx [in] The index to table
201  *
202  * returns 0 on success or negative number on failure
203  */
204 int32_t
205 ulp_flow_db_flush_flows(struct bnxt_ulp_context *ulp_ctx,
206                         uint32_t idx);
207
208 /*
209  * Flush all flows in the flow database that belong to a device function.
210  *
211  * ulp_ctxt [in] Ptr to ulp context
212  * tbl_idx [in] The index to table
213  *
214  * returns 0 on success or negative number on failure
215  */
216 int32_t
217 ulp_flow_db_function_flow_flush(struct bnxt_ulp_context *ulp_ctx,
218                                 uint16_t func_id);
219
220 /*
221  * Flush all flows in the flow database that are associated with the session.
222  *
223  * ulp_ctxt [in] Ptr to ulp context
224  *
225  * returns 0 on success or negative number on failure
226  */
227 int32_t
228 ulp_flow_db_session_flow_flush(struct bnxt_ulp_context *ulp_ctx);
229
230 /*
231  * Check that flow id matches the function id or not
232  *
233  * ulp_ctxt [in] Ptr to ulp context
234  * flow_id [in] flow id of the flow.
235  * func_id [in] The func_id to be set, for reset pass zero.
236  *
237  * returns true on success or false on failure
238  */
239 bool
240 ulp_flow_db_validate_flow_func(struct bnxt_ulp_context *ulp_ctx,
241                                uint32_t flow_id,
242                                uint32_t func_id);
243
244 /*
245  * Api to get the cfa action pointer from a flow.
246  *
247  * ulp_ctxt [in] Ptr to ulp context
248  * flow_id [in] flow id
249  * cfa_action [out] The resource handle stored in the flow database
250  *
251  * returns 0 on success
252  */
253 int32_t
254 ulp_default_flow_db_cfa_action_get(struct bnxt_ulp_context *ulp_ctx,
255                                    uint32_t flow_id,
256                                    uint16_t *cfa_action);
257 /*
258  * Allocate the entry in the parent-child database
259  *
260  * ulp_ctxt [in] Ptr to ulp_context
261  * fid [in] The flow id to the flow entry
262  *
263  * returns index on success and negative on failure.
264  */
265 int32_t
266 ulp_flow_db_parent_flow_alloc(struct bnxt_ulp_context *ulp_ctxt,
267                               uint32_t fid);
268
269 /*
270  * Free the entry in the parent-child database
271  *
272  * ulp_ctxt [in] Ptr to ulp_context
273  * fid [in] The flow id to the flow entry
274  *
275  * returns 0 on success and negative on failure.
276  */
277 int32_t
278 ulp_flow_db_parent_flow_free(struct bnxt_ulp_context *ulp_ctxt,
279                              uint32_t fid);
280
281 /*
282  * Set or reset the child flow in the parent-child database
283  *
284  * ulp_ctxt [in] Ptr to ulp_context
285  * parent_fid [in] The flow id of the parent flow entry
286  * child_fid [in] The flow id of the child flow entry
287  * set_flag [in] Use 1 for setting child, 0 to reset
288  *
289  * returns zero on success and negative on failure.
290  */
291 int32_t
292 ulp_flow_db_parent_child_flow_set(struct bnxt_ulp_context *ulp_ctxt,
293                                   uint32_t parent_fid,
294                                   uint32_t child_fid,
295                                   uint32_t set_flag);
296
297 /*
298  * Get the parent index from the parent-child database
299  *
300  * ulp_ctxt [in] Ptr to ulp_context
301  * parent_fid [in] The flow id of the parent flow entry
302  * parent_idx [out] The parent index of parent flow entry
303  *
304  * returns zero on success and negative on failure.
305  */
306 int32_t
307 ulp_flow_db_parent_flow_idx_get(struct bnxt_ulp_context *ulp_ctxt,
308                                 uint32_t parent_fid,
309                                 uint32_t *parent_idx);
310
311 /*
312  * Get the next child flow in the parent-child database
313  *
314  * ulp_ctxt [in] Ptr to ulp_context
315  * parent_fid [in] The flow id of the parent flow entry
316  * child_fid [in/out] The flow id of the child flow entry
317  *
318  * returns zero on success and negative on failure.
319  * Pass child_fid as zero for first entry.
320  */
321 int32_t
322 ulp_flow_db_parent_child_flow_next_entry_get(struct bnxt_ulp_flow_db *flow_db,
323                                              uint32_t parent_idx,
324                                              uint32_t *child_fid);
325
326 /*
327  * Orphan the child flow entry
328  * This is called only for child flows that have
329  * BNXT_ULP_RESOURCE_FUNC_CHILD_FLOW resource
330  *
331  * ulp_ctxt [in] Ptr to ulp_context
332  * flow_type [in] Specify it is regular or default flow
333  * fid [in] The index to the flow entry
334  *
335  * Returns 0 on success and negative on failure.
336  */
337 int32_t
338 ulp_flow_db_child_flow_reset(struct bnxt_ulp_context *ulp_ctxt,
339                              enum bnxt_ulp_fdb_type flow_type,
340                              uint32_t fid);
341
342 /*
343  * Create parent flow in the parent flow tbl
344  *
345  * parms [in] Ptr to mapper params
346  *
347  * Returns 0 on success and negative on failure.
348  */
349 int32_t
350 ulp_flow_db_parent_flow_create(struct bnxt_ulp_mapper_parms *parms);
351
352 /*
353  * Create child flow in the parent flow tbl
354  *
355  * parms [in] Ptr to mapper params
356  *
357  * Returns 0 on success and negative on failure.
358  */
359 int32_t
360 ulp_flow_db_child_flow_create(struct bnxt_ulp_mapper_parms *parms);
361
362 /*
363  * Update the parent counters
364  *
365  * ulp_ctxt [in] Ptr to ulp_context
366  * parent_fid [in] The flow id of the parent flow entry
367  * packet_count [in] - packet count
368  * byte_count [in] - byte count
369  *
370  * returns 0 on success
371  */
372 int32_t
373 ulp_flow_db_parent_flow_count_update(struct bnxt_ulp_context *ulp_ctxt,
374                                      uint32_t parent_fid,
375                                      uint64_t packet_count,
376                                      uint64_t byte_count);
377 /*
378  * Get the parent accumulation counters
379  *
380  * ulp_ctxt [in] Ptr to ulp_context
381  * parent_fid [in] The flow id of the parent flow entry
382  * packet_count [out] - packet count
383  * byte_count [out] - byte count
384  *
385  * returns 0 on success
386  */
387 int32_t
388 ulp_flow_db_parent_flow_count_get(struct bnxt_ulp_context *ulp_ctxt,
389                                   uint32_t parent_fid,
390                                   uint64_t *packet_count,
391                                   uint64_t *byte_count);
392
393 /*
394  * reset the parent accumulation counters
395  *
396  * ulp_ctxt [in] Ptr to ulp_context
397  *
398  * returns none
399  */
400 void
401 ulp_flow_db_parent_flow_count_reset(struct bnxt_ulp_context *ulp_ctxt);
402
403 #endif /* _ULP_FLOW_DB_H_ */