net/bnxt: support freeing key and action tables
[dpdk.git] / drivers / net / bnxt / tf_ulp / ulp_utils.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2019 Broadcom
3  * All rights reserved.
4  */
5
6 #ifndef _ULP_UTILS_H_
7 #define _ULP_UTILS_H_
8
9 #include "bnxt.h"
10 #include "ulp_template_db.h"
11
12 /*
13  * Macros for bitmap sets and gets
14  * These macros can be used if the val are power of 2.
15  */
16 #define ULP_BITMAP_SET(bitmap, val)     ((bitmap) |= (val))
17 #define ULP_BITMAP_RESET(bitmap, val)   ((bitmap) &= ~(val))
18 #define ULP_BITMAP_ISSET(bitmap, val)   ((bitmap) & (val))
19 #define ULP_BITSET_CMP(b1, b2)  memcmp(&(b1)->bits, \
20                                 &(b2)->bits, sizeof((b1)->bits))
21 /*
22  * Macros for bitmap sets and gets
23  * These macros can be used if the val are not power of 2 and
24  * are simple index values.
25  */
26 #define ULP_INDEX_BITMAP_SIZE   (sizeof(uint64_t) * 8)
27 #define ULP_INDEX_BITMAP_CSET(i)        (1UL << \
28                         ((ULP_INDEX_BITMAP_SIZE - 1) - \
29                         ((i) % ULP_INDEX_BITMAP_SIZE)))
30
31 #define ULP_INDEX_BITMAP_SET(b, i)      ((b) |= \
32                         (1UL << ((ULP_INDEX_BITMAP_SIZE - 1) - \
33                         ((i) % ULP_INDEX_BITMAP_SIZE))))
34
35 #define ULP_INDEX_BITMAP_RESET(b, i)    ((b) &= \
36                         (~(1UL << ((ULP_INDEX_BITMAP_SIZE - 1) - \
37                         ((i) % ULP_INDEX_BITMAP_SIZE)))))
38
39 #define ULP_INDEX_BITMAP_GET(b, i)              (((b) >> \
40                         ((ULP_INDEX_BITMAP_SIZE - 1) - \
41                         ((i) % ULP_INDEX_BITMAP_SIZE))) & 1)
42
43 #define ULP_DEVICE_PARAMS_INDEX(tid, dev_id)    \
44         (((tid) << BNXT_ULP_LOG2_MAX_NUM_DEV) | (dev_id))
45
46 /* Macro to convert bytes to bits */
47 #define ULP_BYTE_2_BITS(byte_x)         ((byte_x) * 8)
48 /* Macro to convert bits to bytes */
49 #define ULP_BITS_2_BYTE(bits_x)         (((bits_x) + 7) / 8)
50 /* Macro to convert bits to bytes with no round off*/
51 #define ULP_BITS_2_BYTE_NR(bits_x)      ((bits_x) / 8)
52
53 /*
54  * Making the blob statically sized to 128 bytes for now.
55  * The blob must be initialized with ulp_blob_init prior to using.
56  */
57 #define BNXT_ULP_FLMP_BLOB_SIZE (128)
58 #define BNXT_ULP_FLMP_BLOB_SIZE_IN_BITS ULP_BYTE_2_BITS(BNXT_ULP_FLMP_BLOB_SIZE)
59 struct ulp_blob {
60         enum bnxt_ulp_byte_order        byte_order;
61         uint16_t                        write_idx;
62         uint16_t                        bitlen;
63         uint8_t                         data[BNXT_ULP_FLMP_BLOB_SIZE];
64         uint16_t                        encap_swap_idx;
65 };
66
67 /*
68  * The data can likely be only 32 bits for now.  Just size check
69  * the data when being written.
70  */
71 #define ULP_REGFILE_ENTRY_SIZE  (sizeof(uint32_t))
72 struct ulp_regfile_entry {
73         uint64_t        data;
74         uint32_t        size;
75 };
76
77 struct ulp_regfile {
78         struct ulp_regfile_entry entry[BNXT_ULP_REGFILE_INDEX_LAST];
79 };
80
81 /*
82  * Initialize the regfile structure for writing
83  *
84  * regfile [in] Ptr to a regfile instance
85  *
86  * returns 0 on error or 1 on success
87  */
88 uint32_t
89 ulp_regfile_init(struct ulp_regfile *regfile);
90
91 /*
92  * Read a value from the regfile
93  *
94  * regfile [in] The regfile instance.  Must be initialized prior to being used
95  *
96  * field [in] The field to be read within the regfile.
97  *
98  * returns the byte array
99  */
100 uint32_t
101 ulp_regfile_read(struct ulp_regfile *regfile,
102                  enum bnxt_ulp_regfile_index field,
103                  uint64_t *data);
104
105 /*
106  * Write a value to the regfile
107  *
108  * regfile [in] The regfile instance.  Must be initialized prior to being used
109  *
110  * field [in] The field to be written within the regfile.
111  *
112  * data [in] The value is written into this variable.  It is going to be in the
113  * same byte order as it was written.
114  *
115  * returns zero on error
116  */
117 uint32_t
118 ulp_regfile_write(struct ulp_regfile *regfile,
119                   enum bnxt_ulp_regfile_index field,
120                   uint64_t data);
121
122 /*
123  * Initializes the blob structure for creating binary blob
124  *
125  * blob [in] The blob to be initialized
126  *
127  * bitlen [in] The bit length of the blob
128  *
129  * order [in] The byte order for the blob.  Currently only supporting
130  * big endian.  All fields are packed with this order.
131  *
132  * returns 0 on error or 1 on success
133  */
134 uint32_t
135 ulp_blob_init(struct ulp_blob *blob,
136               uint16_t bitlen,
137               enum bnxt_ulp_byte_order order);
138
139 /*
140  * Add data to the binary blob at the current offset.
141  *
142  * blob [in] The blob that data is added to.  The blob must
143  * be initialized prior to pushing data.
144  *
145  * data [in] A pointer to bytes to be added to the blob.
146  *
147  * datalen [in] The number of bits to be added to the blob.
148  *
149  * The offset of the data is updated after each push of data.
150  * NULL returned on error.
151  */
152 uint32_t
153 ulp_blob_push(struct ulp_blob *blob,
154               uint8_t *data,
155               uint32_t datalen);
156
157 /*
158  * Add data to the binary blob at the current offset.
159  *
160  * blob [in] The blob that data is added to.  The blob must
161  * be initialized prior to pushing data.
162  *
163  * data [in] 64-bit value to be added to the blob.
164  *
165  * datalen [in] The number of bits to be added to the blob.
166  *
167  * The offset of the data is updated after each push of data.
168  * NULL returned on error, ptr to pushed data otherwise
169  */
170 uint8_t *
171 ulp_blob_push_64(struct ulp_blob *blob,
172                  uint64_t *data,
173                  uint32_t datalen);
174
175 /*
176  * Add encap data to the binary blob at the current offset.
177  *
178  * blob [in] The blob that data is added to.  The blob must
179  * be initialized prior to pushing data.
180  *
181  * data [in] value to be added to the blob.
182  *
183  * datalen [in] The number of bits to be added to the blob.
184  *
185  * The offset of the data is updated after each push of data.
186  * NULL returned on error, pointer pushed value otherwise.
187  */
188 uint32_t
189 ulp_blob_push_encap(struct ulp_blob *blob,
190                     uint8_t *data,
191                     uint32_t datalen);
192
193 /*
194  * Get the data portion of the binary blob.
195  *
196  * blob [in] The blob's data to be retrieved. The blob must be
197  * initialized prior to pushing data.
198  *
199  * datalen [out] The number of bits to that are filled.
200  *
201  * returns a byte array of the blob data.  Returns NULL on error.
202  */
203 uint8_t *
204 ulp_blob_data_get(struct ulp_blob *blob,
205                   uint16_t *datalen);
206
207 /*
208  * Adds pad to an initialized blob at the current offset
209  *
210  * blob [in] The blob that data is added to.  The blob must
211  * be initialized prior to pushing data.
212  *
213  * datalen [in] The number of bits of pad to add
214  *
215  * returns the number of pad bits added, zero on failure
216  */
217 uint32_t
218 ulp_blob_pad_push(struct ulp_blob *blob,
219                   uint32_t datalen);
220
221 /*
222  * Set the 64 bit swap start index of the binary blob.
223  *
224  * blob [in] The blob's data to be retrieved. The blob must be
225  * initialized prior to pushing data.
226  *
227  * returns void.
228  */
229 void
230 ulp_blob_encap_swap_idx_set(struct ulp_blob *blob);
231
232 /*
233  * Perform the encap buffer swap to 64 bit reversal.
234  *
235  * blob [in] The blob's data to be used for swap.
236  *
237  * returns void.
238  */
239 void
240 ulp_blob_perform_encap_swap(struct ulp_blob *blob);
241
242 /*
243  * Read data from the operand
244  *
245  * operand [in] A pointer to a 16 Byte operand
246  *
247  * val [in/out] The variable to copy the operand to
248  *
249  * bitlen [in] The number of bits to read into val
250  *
251  * returns number of bits read, zero on error
252  */
253 uint16_t
254 ulp_operand_read(uint8_t *operand,
255                  uint8_t *val,
256                  uint16_t bitlen);
257
258 /*
259  * copy the buffer in the encap format which is 2 bytes.
260  * The MSB of the src is placed at the LSB of dst.
261  *
262  * dst [out] The destination buffer
263  * src [in] The source buffer dst
264  * size[in] size of the buffer.
265  */
266 void
267 ulp_encap_buffer_copy(uint8_t *dst,
268                       const uint8_t *src,
269                       uint16_t size);
270
271 /*
272  * Check the buffer is empty
273  *
274  * buf [in] The buffer
275  * size [in] The size of the buffer
276  */
277 int32_t ulp_buffer_is_empty(const uint8_t *buf, uint32_t size);
278
279 #endif /* _ULP_UTILS_H_ */