net/bnxt: identify duplicate flows
[dpdk.git] / drivers / net / bnxt / tf_ulp / ulp_utils.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2021 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_enum.h"
11
12 #define ULP_BUFFER_ALIGN_8_BYTE         8
13 #define ULP_BUFFER_ALIGN_16_BYTE        16
14 #define ULP_BUFFER_ALIGN_64_BYTE        64
15 #define ULP_64B_IN_BYTES                8
16 /*
17  * Macros for bitmap sets and gets
18  * These macros can be used if the val are power of 2.
19  */
20 #define ULP_BITMAP_SET(bitmap, val)     ((bitmap) |= (val))
21 #define ULP_BITMAP_RESET(bitmap, val)   ((bitmap) &= ~(val))
22 #define ULP_BITMAP_ISSET(bitmap, val)   ((bitmap) & (val))
23 #define ULP_BITMAP_CMP(b1, b2)  memcmp(&(b1)->bits, \
24                                 &(b2)->bits, sizeof((b1)->bits))
25 /*
26  * Macros for bitmap sets and gets
27  * These macros can be used if the val are not power of 2 and
28  * are simple index values.
29  */
30 #define ULP_INDEX_BITMAP_SIZE   (sizeof(uint64_t) * 8)
31 #define ULP_INDEX_BITMAP_CSET(i)        (1UL << \
32                         ((ULP_INDEX_BITMAP_SIZE - 1) - \
33                         ((i) % ULP_INDEX_BITMAP_SIZE)))
34
35 #define ULP_INDEX_BITMAP_SET(b, i)      ((b) |= \
36                         (1UL << ((ULP_INDEX_BITMAP_SIZE - 1) - \
37                         ((i) % ULP_INDEX_BITMAP_SIZE))))
38
39 #define ULP_INDEX_BITMAP_RESET(b, i)    ((b) &= \
40                         (~(1UL << ((ULP_INDEX_BITMAP_SIZE - 1) - \
41                         ((i) % ULP_INDEX_BITMAP_SIZE)))))
42
43 #define ULP_INDEX_BITMAP_GET(b, i)              (((b) >> \
44                         ((ULP_INDEX_BITMAP_SIZE - 1) - \
45                         ((i) % ULP_INDEX_BITMAP_SIZE))) & 1)
46
47 #define ULP_DEVICE_PARAMS_INDEX(tid, dev_id)    \
48         (((tid) << BNXT_ULP_LOG2_MAX_NUM_DEV) | (dev_id))
49
50 /* Macro to convert bytes to bits */
51 #define ULP_BYTE_2_BITS(byte_x)         ((byte_x) * 8)
52 /* Macro to convert bits to bytes */
53 #define ULP_BITS_2_BYTE(bits_x)         (((bits_x) + 7) / 8)
54 /* Macro to convert bits to bytes with no round off*/
55 #define ULP_BITS_2_BYTE_NR(bits_x)      ((bits_x) / 8)
56
57 /* Macro to round off to next multiple of 8*/
58 #define ULP_BYTE_ROUND_OFF_8(x) (((x) + 7) & ~7)
59
60 /* Macro to check bits are byte aligned */
61 #define ULP_BITS_IS_BYTE_NOT_ALIGNED(x) ((x) % 8)
62
63 /* Macros to read the computed fields */
64 #define ULP_COMP_FLD_IDX_RD(params, idx) \
65         rte_be_to_cpu_32((params)->comp_fld[(idx)])
66
67 #define ULP_COMP_FLD_IDX_WR(params, idx, val)   \
68         ((params)->comp_fld[(idx)] = rte_cpu_to_be_32((val)))
69 /*
70  * Making the blob statically sized to 128 bytes for now.
71  * The blob must be initialized with ulp_blob_init prior to using.
72  */
73 #define BNXT_ULP_FLMP_BLOB_SIZE (128)
74 #define BNXT_ULP_FLMP_BLOB_SIZE_IN_BITS ULP_BYTE_2_BITS(BNXT_ULP_FLMP_BLOB_SIZE)
75 struct ulp_blob {
76         enum bnxt_ulp_byte_order        byte_order;
77         uint16_t                        write_idx;
78         uint16_t                        bitlen;
79         uint8_t                         data[BNXT_ULP_FLMP_BLOB_SIZE];
80         uint16_t                        encap_swap_idx;
81 };
82
83 /*
84  * The data can likely be only 32 bits for now.  Just size check
85  * the data when being written.
86  */
87 #define ULP_REGFILE_ENTRY_SIZE  (sizeof(uint32_t))
88 struct ulp_regfile_entry {
89         uint64_t        data;
90         uint32_t        size;
91 };
92
93 struct ulp_regfile {
94         struct ulp_regfile_entry entry[BNXT_ULP_RF_IDX_LAST];
95 };
96
97 /*
98  * Initialize the regfile structure for writing
99  *
100  * regfile [in] Ptr to a regfile instance
101  *
102  * returns 0 on error or 1 on success
103  */
104 uint32_t
105 ulp_regfile_init(struct ulp_regfile *regfile);
106
107 /*
108  * Read a value from the regfile
109  *
110  * regfile [in] The regfile instance.  Must be initialized prior to being used
111  *
112  * field [in] The field to be read within the regfile.
113  *
114  * returns the byte array
115  */
116 uint32_t
117 ulp_regfile_read(struct ulp_regfile *regfile,
118                  enum bnxt_ulp_rf_idx field,
119                  uint64_t *data);
120
121 /*
122  * Write a value to the regfile
123  *
124  * regfile [in] The regfile instance.  Must be initialized prior to being used
125  *
126  * field [in] The field to be written within the regfile.
127  *
128  * data [in] The value is written into this variable.  It is going to be in the
129  * same byte order as it was written.
130  *
131  * returns zero on success
132  */
133 int32_t
134 ulp_regfile_write(struct ulp_regfile *regfile,
135                   enum bnxt_ulp_rf_idx field,
136                   uint64_t data);
137
138 /*
139  * Add data to the byte array in Little endian format.
140  *
141  * bs [in] The byte array where data is pushed
142  *
143  * pos [in] The offset where data is pushed
144  *
145  * len [in] The number of bits to be added to the data array.
146  *
147  * val [in] The data to be added to the data array.
148  *
149  * returns the number of bits pushed.
150  */
151 uint32_t
152 ulp_bs_push_lsb(uint8_t *bs, uint16_t pos, uint8_t len, uint8_t *val);
153
154 /*
155  * Add data to the byte array in Big endian format.
156  *
157  * bs [in] The byte array where data is pushed
158  *
159  * pos [in] The offset where data is pushed
160  *
161  * len [in] The number of bits to be added to the data array.
162  *
163  * val [in] The data to be added to the data array.
164  *
165  * returns the number of bits pushed.
166  */
167 uint32_t
168 ulp_bs_push_msb(uint8_t *bs, uint16_t pos, uint8_t len, uint8_t *val);
169
170 /*
171  * Initializes the blob structure for creating binary blob
172  *
173  * blob [in] The blob to be initialized
174  *
175  * bitlen [in] The bit length of the blob
176  *
177  * order [in] The byte order for the blob.  Currently only supporting
178  * big endian.  All fields are packed with this order.
179  *
180  * returns 0 on error or 1 on success
181  */
182 uint32_t
183 ulp_blob_init(struct ulp_blob *blob,
184               uint16_t bitlen,
185               enum bnxt_ulp_byte_order order);
186
187 /*
188  * Add data to the binary blob at the current offset.
189  *
190  * blob [in] The blob that data is added to.  The blob must
191  * be initialized prior to pushing data.
192  *
193  * data [in] A pointer to bytes to be added to the blob.
194  *
195  * datalen [in] The number of bits to be added to the blob.
196  *
197  * The offset of the data is updated after each push of data.
198  * NULL returned on error.
199  */
200 uint32_t
201 ulp_blob_push(struct ulp_blob *blob,
202               uint8_t *data,
203               uint32_t datalen);
204
205 /*
206  * Insert data into the binary blob at the given offset.
207  *
208  * blob [in] The blob that data is added to.  The blob must
209  * be initialized prior to pushing data.
210  *
211  * offset [in] The offset where the data needs to be inserted.
212  *
213  * data [in/out] A pointer to bytes to be added to the blob.
214  *
215  * datalen [in] The number of bits to be added to the blob.
216  *
217  * The offset of the data is updated after each push of data.
218  * NULL returned on error.
219  */
220 uint32_t
221 ulp_blob_insert(struct ulp_blob *blob, uint32_t offset,
222                 uint8_t *data, uint32_t datalen);
223
224 /*
225  * Add data to the binary blob at the current offset.
226  *
227  * blob [in] The blob that data is added to.  The blob must
228  * be initialized prior to pushing data.
229  *
230  * data [in] 64-bit value to be added to the blob.
231  *
232  * datalen [in] The number of bits to be added to the blob.
233  *
234  * The offset of the data is updated after each push of data.
235  * NULL returned on error, ptr to pushed data otherwise
236  */
237 uint8_t *
238 ulp_blob_push_64(struct ulp_blob *blob,
239                  uint64_t *data,
240                  uint32_t datalen);
241
242 /*
243  * Add data to the binary blob at the current offset.
244  *
245  * blob [in] The blob that data is added to.  The blob must
246  * be initialized prior to pushing data.
247  *
248  * data [in] 32-bit value to be added to the blob.
249  *
250  * datalen [in] The number of bits to be added ot the blob.
251  *
252  * The offset of the data is updated after each push of data.
253  * NULL returned on error, pointer pushed value otherwise.
254  */
255 uint8_t *
256 ulp_blob_push_32(struct ulp_blob *blob,
257                  uint32_t *data,
258                  uint32_t datalen);
259
260 /*
261  * Add encap data to the binary blob at the current offset.
262  *
263  * blob [in] The blob that data is added to.  The blob must
264  * be initialized prior to pushing data.
265  *
266  * data [in] value to be added to the blob.
267  *
268  * datalen [in] The number of bits to be added to the blob.
269  *
270  * The offset of the data is updated after each push of data.
271  * NULL returned on error, pointer pushed value otherwise.
272  */
273 uint32_t
274 ulp_blob_push_encap(struct ulp_blob *blob,
275                     uint8_t *data,
276                     uint32_t datalen);
277
278 /*
279  * Get the data portion of the binary blob.
280  *
281  * blob [in] The blob's data to be retrieved. The blob must be
282  * initialized prior to pushing data.
283  *
284  * datalen [out] The number of bits to that are filled.
285  *
286  * returns a byte array of the blob data.  Returns NULL on error.
287  */
288 uint8_t *
289 ulp_blob_data_get(struct ulp_blob *blob,
290                   uint16_t *datalen);
291
292 /*
293  * Get data from the byte array in Little endian format.
294  *
295  * src [in] The byte array where data is extracted from
296  *
297  * dst [out] The byte array where data is pulled into
298  *
299  * size [in] The size of dst array in bytes
300  *
301  * offset [in] The offset where data is pulled
302  *
303  * len [in] The number of bits to be extracted from the data array
304  *
305  * returns None.
306  */
307 void
308 ulp_bs_pull_lsb(uint8_t *src, uint8_t *dst, uint32_t size,
309                 uint32_t offset, uint32_t len);
310
311 /*
312  * Get data from the byte array in Big endian format.
313  *
314  * src [in] The byte array where data is extracted from
315  *
316  * dst [out] The byte array where data is pulled into
317  *
318  * offset [in] The offset where data is pulled
319  *
320  * len [in] The number of bits to be extracted from the data array
321  *
322  * returns None.
323  */
324 void
325 ulp_bs_pull_msb(uint8_t *src, uint8_t *dst,
326                 uint32_t offset, uint32_t len);
327
328 /*
329  * Extract data from the binary blob using given offset.
330  *
331  * blob [in] The blob that data is extracted from. The blob must
332  * be initialized prior to pulling data.
333  *
334  * data [in] A pointer to put the data.
335  * data_size [in] size of the data buffer in bytes.
336  *offset [in] - Offset in the blob to extract the data in bits format.
337  * len [in] The number of bits to be pulled from the blob.
338  *
339  * Output: zero on success, -1 on failure
340  */
341 int32_t
342 ulp_blob_pull(struct ulp_blob *blob, uint8_t *data, uint32_t data_size,
343               uint16_t offset, uint16_t len);
344
345 /*
346  * Adds pad to an initialized blob at the current offset
347  *
348  * blob [in] The blob that data is added to.  The blob must
349  * be initialized prior to pushing data.
350  *
351  * datalen [in] The number of bits of pad to add
352  *
353  * returns the number of pad bits added, -1 on failure
354  */
355 int32_t
356 ulp_blob_pad_push(struct ulp_blob *blob,
357                   uint32_t datalen);
358
359 /*
360  * Set the 64 bit swap start index of the binary blob.
361  *
362  * blob [in] The blob's data to be retrieved. The blob must be
363  * initialized prior to pushing data.
364  *
365  * returns void.
366  */
367 void
368 ulp_blob_encap_swap_idx_set(struct ulp_blob *blob);
369
370 /*
371  * Perform the encap buffer swap to 64 bit reversal.
372  *
373  * blob [in] The blob's data to be used for swap.
374  *
375  * returns void.
376  */
377 void
378 ulp_blob_perform_encap_swap(struct ulp_blob *blob);
379
380 /*
381  * Perform the blob buffer reversal byte wise.
382  * This api makes the first byte the last and
383  * vice-versa.
384  *
385  * blob [in] The blob's data to be used for swap.
386  *
387  * returns void.
388  */
389 void
390 ulp_blob_perform_byte_reverse(struct ulp_blob *blob);
391
392 /*
393  * Perform the blob buffer 64 bit word swap.
394  * This api makes the first 4 bytes the last in
395  * a given 64 bit value and vice-versa.
396  *
397  * blob [in] The blob's data to be used for swap.
398  *
399  * returns void.
400  */
401 void
402 ulp_blob_perform_64B_word_swap(struct ulp_blob *blob);
403
404 /*
405  * Perform the blob buffer 64 bit byte swap.
406  * This api makes the first byte the last in
407  * a given 64 bit value and vice-versa.
408  *
409  * blob [in] The blob's data to be used for swap.
410  *
411  * returns void.
412  */
413 void
414 ulp_blob_perform_64B_byte_swap(struct ulp_blob *blob);
415
416 /*
417  * Read data from the operand
418  *
419  * operand [in] A pointer to a 16 Byte operand
420  *
421  * val [in/out] The variable to copy the operand to
422  *
423  * bitlen [in] The number of bits to read into val
424  *
425  * returns number of bits read, zero on error
426  */
427 uint16_t
428 ulp_operand_read(uint8_t *operand,
429                  uint8_t *val,
430                  uint16_t bitlen);
431
432 /*
433  * copy the buffer in the encap format which is 2 bytes.
434  * The MSB of the src is placed at the LSB of dst.
435  *
436  * dst [out] The destination buffer
437  * src [in] The source buffer dst
438  * size[in] size of the buffer.
439  * align[in] The alignment is either 8 or 16.
440  */
441 void
442 ulp_encap_buffer_copy(uint8_t *dst,
443                       const uint8_t *src,
444                       uint16_t size,
445                       uint16_t align);
446
447 /*
448  * Check the buffer is empty
449  *
450  * buf [in] The buffer
451  * size [in] The size of the buffer
452  */
453 int32_t ulp_buffer_is_empty(const uint8_t *buf, uint32_t size);
454
455 /* Function to check if bitmap is zero.Return 1 on success */
456 uint32_t ulp_bitmap_is_zero(uint8_t *bitmap, int32_t size);
457
458 /* Function to check if bitmap is ones. Return 1 on success */
459 uint32_t ulp_bitmap_is_ones(uint8_t *bitmap, int32_t size);
460
461 /* Function to check if bitmap is not zero. Return 1 on success */
462 uint32_t ulp_bitmap_notzero(uint8_t *bitmap, int32_t size);
463
464 #endif /* _ULP_UTILS_H_ */