1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2014-2021 Broadcom
10 #include "ulp_template_db_enum.h"
12 #define ULP_BUFFER_ALIGN_8_BITS 8
13 #define ULP_BUFFER_ALIGN_8_BYTE 8
14 #define ULP_BUFFER_ALIGN_16_BYTE 16
15 #define ULP_BUFFER_ALIGN_64_BYTE 64
16 #define ULP_64B_IN_BYTES 8
19 * Macros for bitmap sets and gets
20 * These macros can be used if the val are power of 2.
22 #define ULP_BITMAP_SET(bitmap, val) ((bitmap) |= (val))
23 #define ULP_BITMAP_RESET(bitmap, val) ((bitmap) &= ~(val))
24 #define ULP_BITMAP_ISSET(bitmap, val) ((bitmap) & (val))
25 #define ULP_BITMAP_CMP(b1, b2) memcmp(&(b1)->bits, \
26 &(b2)->bits, sizeof((b1)->bits))
28 * Macros for bitmap sets and gets
29 * These macros can be used if the val are not power of 2 and
30 * are simple index values.
32 #define ULP_INDEX_BITMAP_SIZE (sizeof(uint64_t) * 8)
33 #define ULP_INDEX_BITMAP_CSET(i) (1UL << \
34 ((ULP_INDEX_BITMAP_SIZE - 1) - \
35 ((i) % ULP_INDEX_BITMAP_SIZE)))
37 #define ULP_INDEX_BITMAP_SET(b, i) ((b) |= \
38 (1UL << ((ULP_INDEX_BITMAP_SIZE - 1) - \
39 ((i) % ULP_INDEX_BITMAP_SIZE))))
41 #define ULP_INDEX_BITMAP_RESET(b, i) ((b) &= \
42 (~(1UL << ((ULP_INDEX_BITMAP_SIZE - 1) - \
43 ((i) % ULP_INDEX_BITMAP_SIZE)))))
45 #define ULP_INDEX_BITMAP_GET(b, i) (((b) >> \
46 ((ULP_INDEX_BITMAP_SIZE - 1) - \
47 ((i) % ULP_INDEX_BITMAP_SIZE))) & 1)
49 #define ULP_DEVICE_PARAMS_INDEX(tid, dev_id) \
50 (((tid) << BNXT_ULP_LOG2_MAX_NUM_DEV) | (dev_id))
52 /* Macro to convert bytes to bits */
53 #define ULP_BYTE_2_BITS(byte_x) ((byte_x) * 8)
54 /* Macro to convert bits to bytes */
55 #define ULP_BITS_2_BYTE(bits_x) (((bits_x) + 7) / 8)
56 /* Macro to convert bits to bytes with no round off*/
57 #define ULP_BITS_2_BYTE_NR(bits_x) ((bits_x) / 8)
59 /* Macro to round off to next multiple of 8*/
60 #define ULP_BYTE_ROUND_OFF_8(x) (((x) + 7) & ~7)
62 /* Macro to check bits are byte aligned */
63 #define ULP_BITS_IS_BYTE_NOT_ALIGNED(x) ((x) % 8)
65 /* Macros to read the computed fields */
66 #define ULP_COMP_FLD_IDX_RD(params, idx) \
67 rte_be_to_cpu_64((params)->comp_fld[(idx)])
69 #define ULP_COMP_FLD_IDX_WR(params, idx, val) \
70 ((params)->comp_fld[(idx)] = rte_cpu_to_be_64((uint64_t)(val)))
72 * Making the blob statically sized to 128 bytes for now.
73 * The blob must be initialized with ulp_blob_init prior to using.
75 #define BNXT_ULP_FLMP_BLOB_SIZE (128)
76 #define BNXT_ULP_FLMP_BLOB_SIZE_IN_BITS ULP_BYTE_2_BITS(BNXT_ULP_FLMP_BLOB_SIZE)
78 enum bnxt_ulp_byte_order byte_order;
81 uint8_t data[BNXT_ULP_FLMP_BLOB_SIZE];
82 uint16_t encap_swap_idx;
86 * The data can likely be only 32 bits for now. Just size check
87 * the data when being written.
89 #define ULP_REGFILE_ENTRY_SIZE (sizeof(uint32_t))
90 struct ulp_regfile_entry {
96 struct ulp_regfile_entry entry[BNXT_ULP_RF_IDX_LAST];
100 * Initialize the regfile structure for writing
102 * regfile [in] Ptr to a regfile instance
104 * returns 0 on error or 1 on success
107 ulp_regfile_init(struct ulp_regfile *regfile);
110 * Read a value from the regfile
112 * regfile [in] The regfile instance. Must be initialized prior to being used
114 * field [in] The field to be read within the regfile.
116 * returns the byte array
119 ulp_regfile_read(struct ulp_regfile *regfile,
120 enum bnxt_ulp_rf_idx field,
124 * Write a value to the regfile
126 * regfile [in] The regfile instance. Must be initialized prior to being used
128 * field [in] The field to be written within the regfile.
130 * data [in] The value is written into this variable. It is going to be in the
131 * same byte order as it was written.
133 * returns zero on success
136 ulp_regfile_write(struct ulp_regfile *regfile,
137 enum bnxt_ulp_rf_idx field,
141 * Add data to the byte array in Little endian format.
143 * bs [in] The byte array where data is pushed
145 * pos [in] The offset where data is pushed
147 * len [in] The number of bits to be added to the data array.
149 * val [in] The data to be added to the data array.
151 * returns the number of bits pushed.
154 ulp_bs_push_lsb(uint8_t *bs, uint16_t pos, uint8_t len, uint8_t *val);
157 * Add data to the byte array in Big endian format.
159 * bs [in] The byte array where data is pushed
161 * pos [in] The offset where data is pushed
163 * len [in] The number of bits to be added to the data array.
165 * val [in] The data to be added to the data array.
167 * returns the number of bits pushed.
170 ulp_bs_push_msb(uint8_t *bs, uint16_t pos, uint8_t len, uint8_t *val);
173 * Initializes the blob structure for creating binary blob
175 * blob [in] The blob to be initialized
177 * bitlen [in] The bit length of the blob
179 * order [in] The byte order for the blob. Currently only supporting
180 * big endian. All fields are packed with this order.
182 * returns 0 on error or 1 on success
185 ulp_blob_init(struct ulp_blob *blob,
187 enum bnxt_ulp_byte_order order);
190 * Add data to the binary blob at the current offset.
192 * blob [in] The blob that data is added to. The blob must
193 * be initialized prior to pushing data.
195 * data [in] A pointer to bytes to be added to the blob.
197 * datalen [in] The number of bits to be added to the blob.
199 * The offset of the data is updated after each push of data.
200 * NULL returned on error.
203 ulp_blob_push(struct ulp_blob *blob,
208 * Insert data into the binary blob at the given offset.
210 * blob [in] The blob that data is added to. The blob must
211 * be initialized prior to pushing data.
213 * offset [in] The offset where the data needs to be inserted.
215 * data [in/out] A pointer to bytes to be added to the blob.
217 * datalen [in] The number of bits to be added to the blob.
219 * The offset of the data is updated after each push of data.
220 * NULL returned on error.
223 ulp_blob_insert(struct ulp_blob *blob, uint32_t offset,
224 uint8_t *data, uint32_t datalen);
227 * Add data to the binary blob at the current offset.
229 * blob [in] The blob that data is added to. The blob must
230 * be initialized prior to pushing data.
232 * data [in] 64-bit value to be added to the blob.
234 * datalen [in] The number of bits to be added to the blob.
236 * The offset of the data is updated after each push of data.
237 * NULL returned on error, ptr to pushed data otherwise
240 ulp_blob_push_64(struct ulp_blob *blob,
245 * Add data to the binary blob at the current offset.
247 * blob [in] The blob that data is added to. The blob must
248 * be initialized prior to pushing data.
250 * data [in] 32-bit value to be added to the blob.
252 * datalen [in] The number of bits to be added ot the blob.
254 * The offset of the data is updated after each push of data.
255 * NULL returned on error, pointer pushed value otherwise.
258 ulp_blob_push_32(struct ulp_blob *blob,
263 * Add encap data to the binary blob at the current offset.
265 * blob [in] The blob that data is added to. The blob must
266 * be initialized prior to pushing data.
268 * data [in] value to be added to the blob.
270 * datalen [in] The number of bits to be added to the blob.
272 * The offset of the data is updated after each push of data.
273 * NULL returned on error, pointer pushed value otherwise.
276 ulp_blob_push_encap(struct ulp_blob *blob,
281 * Get the data portion of the binary blob.
283 * blob [in] The blob's data to be retrieved. The blob must be
284 * initialized prior to pushing data.
286 * datalen [out] The number of bits to that are filled.
288 * returns a byte array of the blob data. Returns NULL on error.
291 ulp_blob_data_get(struct ulp_blob *blob,
295 * Get the data length of the binary blob.
297 * blob [in] The blob's data len to be retrieved.
299 * returns length of the binary blob
302 ulp_blob_data_len_get(struct ulp_blob *blob);
305 * Get data from the byte array in Little endian format.
307 * src [in] The byte array where data is extracted from
309 * dst [out] The byte array where data is pulled into
311 * size [in] The size of dst array in bytes
313 * offset [in] The offset where data is pulled
315 * len [in] The number of bits to be extracted from the data array
320 ulp_bs_pull_lsb(uint8_t *src, uint8_t *dst, uint32_t size,
321 uint32_t offset, uint32_t len);
324 * Get data from the byte array in Big endian format.
326 * src [in] The byte array where data is extracted from
328 * dst [out] The byte array where data is pulled into
330 * offset [in] The offset where data is pulled
332 * len [in] The number of bits to be extracted from the data array
337 ulp_bs_pull_msb(uint8_t *src, uint8_t *dst,
338 uint32_t offset, uint32_t len);
341 * Extract data from the binary blob using given offset.
343 * blob [in] The blob that data is extracted from. The blob must
344 * be initialized prior to pulling data.
346 * data [in] A pointer to put the data.
347 * data_size [in] size of the data buffer in bytes.
348 *offset [in] - Offset in the blob to extract the data in bits format.
349 * len [in] The number of bits to be pulled from the blob.
351 * Output: zero on success, -1 on failure
354 ulp_blob_pull(struct ulp_blob *blob, uint8_t *data, uint32_t data_size,
355 uint16_t offset, uint16_t len);
358 * Adds pad to an initialized blob at the current offset
360 * blob [in] The blob that data is added to. The blob must
361 * be initialized prior to pushing data.
363 * datalen [in] The number of bits of pad to add
365 * returns the number of pad bits added, -1 on failure
368 ulp_blob_pad_push(struct ulp_blob *blob,
372 * Adds pad to an initialized blob at the current offset based on
375 * blob [in] The blob that needs to be aligned
377 * align [in] Alignment in bits.
379 * returns the number of pad bits added, -1 on failure
382 ulp_blob_pad_align(struct ulp_blob *blob,
386 * Set the 64 bit swap start index of the binary blob.
388 * blob [in] The blob's data to be retrieved. The blob must be
389 * initialized prior to pushing data.
394 ulp_blob_encap_swap_idx_set(struct ulp_blob *blob);
397 * Perform the encap buffer swap to 64 bit reversal.
399 * blob [in] The blob's data to be used for swap.
404 ulp_blob_perform_encap_swap(struct ulp_blob *blob);
407 * Perform the blob buffer reversal byte wise.
408 * This api makes the first byte the last and
411 * blob [in] The blob's data to be used for swap.
412 * chunk_size[in] the swap is done within the chunk in bytes
417 ulp_blob_perform_byte_reverse(struct ulp_blob *blob,
418 uint32_t chunk_size);
421 * Perform the blob buffer 64 bit word swap.
422 * This api makes the first 4 bytes the last in
423 * a given 64 bit value and vice-versa.
425 * blob [in] The blob's data to be used for swap.
430 ulp_blob_perform_64B_word_swap(struct ulp_blob *blob);
433 * Perform the blob buffer 64 bit byte swap.
434 * This api makes the first byte the last in
435 * a given 64 bit value and vice-versa.
437 * blob [in] The blob's data to be used for swap.
442 ulp_blob_perform_64B_byte_swap(struct ulp_blob *blob);
445 * Perform the blob buffer merge.
446 * This api makes the src blob merged to the dst blob.
447 * The block size and pad size help in padding the dst blob
449 * dst [in] The destination blob, the blob to be merged.
450 * src [in] The src blob.
451 * block_size [in] The size of the block after which padding gets applied.
452 * pad [in] The size of the pad to be applied.
454 * returns 0 on success.
457 ulp_blob_block_merge(struct ulp_blob *dst, struct ulp_blob *src,
458 uint32_t block_size, uint32_t pad);
461 * Append bits from src blob to dst blob.
462 * Only works on BE blobs
464 * dst [in/out] The destination blob to append to
466 * src [in] The src blob to append from
468 * src_offset [in] The bit offset from src to start at
470 * src_len [in] The number of bits to append to dst
472 * returns 0 on success, non-zero on error
475 ulp_blob_append(struct ulp_blob *dst, struct ulp_blob *src,
476 uint16_t src_offset, uint16_t src_len);
479 * Read data from the operand
481 * operand [in] A pointer to a 16 Byte operand
483 * val [in/out] The variable to copy the operand to
485 * bitlen [in] The number of bits to read into val
487 * returns number of bits read, zero on error
490 ulp_operand_read(uint8_t *operand,
495 * copy the buffer in the encap format which is 2 bytes.
496 * The MSB of the src is placed at the LSB of dst.
498 * dst [out] The destination buffer
499 * src [in] The source buffer dst
500 * size[in] size of the buffer.
501 * align[in] The alignment is either 8 or 16.
504 ulp_encap_buffer_copy(uint8_t *dst,
510 * Check the buffer is empty
512 * buf [in] The buffer
513 * size [in] The size of the buffer
515 int32_t ulp_buffer_is_empty(const uint8_t *buf, uint32_t size);
517 /* Function to check if bitmap is zero.Return 1 on success */
518 uint32_t ulp_bitmap_is_zero(uint8_t *bitmap, int32_t size);
520 /* Function to check if bitmap is ones. Return 1 on success */
521 uint32_t ulp_bitmap_is_ones(uint8_t *bitmap, int32_t size);
523 /* Function to check if bitmap is not zero. Return 1 on success */
524 uint32_t ulp_bitmap_notzero(const uint8_t *bitmap, int32_t size);
526 /* returns 0 if input is power of 2 */
527 int32_t ulp_util_is_power_of_2(uint64_t x);
529 #endif /* _ULP_UTILS_H_ */