net/bnxt: fix build
[dpdk.git] / drivers / net / bnxt / tf_core / tf_em_hash_internal.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019-2021 Broadcom
3  * All rights reserved.
4  */
5
6 #include <string.h>
7 #include <rte_common.h>
8 #include <rte_errno.h>
9 #include <rte_log.h>
10
11 #include "tf_core.h"
12 #include "tf_util.h"
13 #include "tf_common.h"
14 #include "tf_em.h"
15 #include "tf_msg.h"
16 #include "tfp.h"
17 #include "tf_ext_flow_handle.h"
18 #include "tf_device.h"
19
20 #include "bnxt.h"
21
22 /**
23  * EM Pool
24  */
25 #if (TF_EM_ALLOC == 1)
26 #include "dpool.h"
27 #endif
28
29 /**
30  * Insert EM internal entry API
31  *
32  *  returns:
33  *     0 - Success
34  */
35 int
36 tf_em_hash_insert_int_entry(struct tf *tfp,
37                             struct tf_insert_em_entry_parms *parms)
38 {
39         int rc;
40         uint32_t gfid;
41         uint16_t rptr_index = 0;
42         uint8_t rptr_entry = 0;
43         uint8_t num_of_entries = 0;
44 #if (TF_EM_ALLOC == 1)
45         struct dpool *pool;
46 #else
47         struct stack *pool;
48 #endif
49         uint32_t index;
50         uint32_t key0_hash;
51         uint32_t key1_hash;
52         uint64_t big_hash;
53         struct tf_dev_info *dev;
54         struct tf_session *tfs;
55
56         /* Retrieve the session information */
57         rc = tf_session_get_session_internal(tfp, &tfs);
58         if (rc)
59                 return rc;
60
61         /* Retrieve the device information */
62         rc = tf_session_get_device(tfs, &dev);
63         if (rc)
64                 return rc;
65 #if (TF_EM_ALLOC == 1)
66         pool = (struct dpool *)tfs->em_pool[parms->dir];
67         index = dpool_alloc(pool,
68                             parms->em_record_sz_in_bits / 128,
69                             DP_DEFRAG_TO_FIT);
70
71         if (index == DP_INVALID_INDEX) {
72                 PMD_DRV_LOG(ERR,
73                             "%s, EM entry index allocation failed\n",
74                             tf_dir_2_str(parms->dir));
75                 return -1;
76         }
77 #else
78         pool = (struct stack *)tfs->em_pool[parms->dir];
79         rc = stack_pop(pool, &index);
80         if (rc) {
81                 PMD_DRV_LOG(ERR,
82                             "%s, EM entry index allocation failed\n",
83                             tf_dir_2_str(parms->dir));
84                 return rc;
85         }
86 #endif
87
88         if (dev->ops->tf_dev_cfa_key_hash == NULL)
89                 return -EINVAL;
90
91         big_hash = dev->ops->tf_dev_cfa_key_hash((uint64_t *)parms->key,
92                                         TF_P58_HW_EM_KEY_MAX_SIZE * 8);
93         key0_hash = (uint32_t)(big_hash >> 32);
94         key1_hash = (uint32_t)(big_hash & 0xFFFFFFFF);
95
96         rptr_index = index;
97         rc = tf_msg_hash_insert_em_internal_entry(tfp,
98                                                   parms,
99                                                   key0_hash,
100                                                   key1_hash,
101                                                   &rptr_index,
102                                                   &rptr_entry,
103                                                   &num_of_entries);
104         if (rc) {
105                 /* Free the allocated index before returning */
106 #if (TF_EM_ALLOC == 1)
107                 dpool_free(pool, index);
108 #else
109                 stack_push(pool, index);
110 #endif
111                 return -1;
112         }
113
114         TF_SET_GFID(gfid,
115                     ((rptr_index << TF_EM_INTERNAL_INDEX_SHIFT) |
116                      rptr_entry),
117                     0); /* N/A for internal table */
118
119         TF_SET_FLOW_ID(parms->flow_id,
120                        gfid,
121                        TF_GFID_TABLE_INTERNAL,
122                        parms->dir);
123
124         TF_SET_FIELDS_IN_FLOW_HANDLE(parms->flow_handle,
125                                      (uint32_t)num_of_entries,
126                                      0,
127                                      TF_FLAGS_FLOW_HANDLE_INTERNAL,
128                                      rptr_index,
129                                      rptr_entry,
130                                      0);
131 #if (TF_EM_ALLOC == 1)
132         dpool_set_entry_data(pool, index, parms->flow_handle);
133 #endif
134         return 0;
135 }
136
137 /** Delete EM internal entry API
138  *
139  * returns:
140  * 0
141  * -EINVAL
142  */
143 int
144 tf_em_hash_delete_int_entry(struct tf *tfp,
145                             struct tf_delete_em_entry_parms *parms)
146 {
147         int rc = 0;
148         struct tf_session *tfs;
149 #if (TF_EM_ALLOC == 1)
150         struct dpool *pool;
151 #else
152         struct stack *pool;
153 #endif
154         /* Retrieve the session information */
155         rc = tf_session_get_session(tfp, &tfs);
156         if (rc) {
157                 TFP_DRV_LOG(ERR,
158                             "%s: Failed to lookup session, rc:%s\n",
159                             tf_dir_2_str(parms->dir),
160                             strerror(-rc));
161                 return rc;
162         }
163
164         rc = tf_msg_delete_em_entry(tfp, parms);
165
166         /* Return resource to pool */
167         if (rc == 0) {
168 #if (TF_EM_ALLOC == 1)
169                 pool = (struct dpool *)tfs->em_pool[parms->dir];
170                 dpool_free(pool, parms->index);
171 #else
172                 pool = (struct stack *)tfs->em_pool[parms->dir];
173                 stack_push(pool, parms->index);
174 #endif
175         }
176
177         return rc;
178 }
179
180 #if (TF_EM_ALLOC == 1)
181 /** Move EM internal entry API
182  *
183  * returns:
184  * 0
185  * -EINVAL
186  */
187 int
188 tf_em_move_int_entry(struct tf *tfp,
189                      struct tf_move_em_entry_parms *parms)
190 {
191         int rc = 0;
192         struct dpool *pool;
193         struct tf_session *tfs;
194
195         /* Retrieve the session information */
196         rc = tf_session_get_session(tfp, &tfs);
197         if (rc) {
198                 TFP_DRV_LOG(ERR,
199                             "%s: Failed to lookup session, rc:%s\n",
200                             tf_dir_2_str(parms->dir),
201                             strerror(-rc));
202                 return rc;
203         }
204
205         rc = tf_msg_move_em_entry(tfp, parms);
206
207         /* Return resource to pool */
208         if (rc == 0) {
209                 pool = (struct dpool *)tfs->em_pool[parms->dir];
210                 dpool_free(pool, parms->index);
211         }
212
213         return rc;
214 }
215 #endif