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_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 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 /* Macros to read the computed fields */ 61 #define ULP_COMP_FLD_IDX_RD(params, idx) \ 62 rte_be_to_cpu_32((params)->comp_fld[(idx)]) 63 64 #define ULP_COMP_FLD_IDX_WR(params, idx, val) \ 65 ((params)->comp_fld[(idx)] = rte_cpu_to_be_32((val))) 66 /* 67 * Making the blob statically sized to 128 bytes for now. 68 * The blob must be initialized with ulp_blob_init prior to using. 69 */ 70 #define BNXT_ULP_FLMP_BLOB_SIZE (128) 71 #define BNXT_ULP_FLMP_BLOB_SIZE_IN_BITS ULP_BYTE_2_BITS(BNXT_ULP_FLMP_BLOB_SIZE) 72 struct ulp_blob { 73 enum bnxt_ulp_byte_order byte_order; 74 uint16_t write_idx; 75 uint16_t bitlen; 76 uint8_t data[BNXT_ULP_FLMP_BLOB_SIZE]; 77 uint16_t encap_swap_idx; 78 }; 79 80 /* 81 * The data can likely be only 32 bits for now. Just size check 82 * the data when being written. 83 */ 84 #define ULP_REGFILE_ENTRY_SIZE (sizeof(uint32_t)) 85 struct ulp_regfile_entry { 86 uint64_t data; 87 uint32_t size; 88 }; 89 90 struct ulp_regfile { 91 struct ulp_regfile_entry entry[BNXT_ULP_REGFILE_INDEX_LAST]; 92 }; 93 94 /* 95 * Initialize the regfile structure for writing 96 * 97 * regfile [in] Ptr to a regfile instance 98 * 99 * returns 0 on error or 1 on success 100 */ 101 uint32_t 102 ulp_regfile_init(struct ulp_regfile *regfile); 103 104 /* 105 * Read a value from the regfile 106 * 107 * regfile [in] The regfile instance. Must be initialized prior to being used 108 * 109 * field [in] The field to be read within the regfile. 110 * 111 * returns the byte array 112 */ 113 uint32_t 114 ulp_regfile_read(struct ulp_regfile *regfile, 115 enum bnxt_ulp_regfile_index field, 116 uint64_t *data); 117 118 /* 119 * Write a value to the regfile 120 * 121 * regfile [in] The regfile instance. Must be initialized prior to being used 122 * 123 * field [in] The field to be written within the regfile. 124 * 125 * data [in] The value is written into this variable. It is going to be in the 126 * same byte order as it was written. 127 * 128 * returns zero on error 129 */ 130 uint32_t 131 ulp_regfile_write(struct ulp_regfile *regfile, 132 enum bnxt_ulp_regfile_index field, 133 uint64_t data); 134 135 /* 136 * Initializes the blob structure for creating binary blob 137 * 138 * blob [in] The blob to be initialized 139 * 140 * bitlen [in] The bit length of the blob 141 * 142 * order [in] The byte order for the blob. Currently only supporting 143 * big endian. All fields are packed with this order. 144 * 145 * returns 0 on error or 1 on success 146 */ 147 uint32_t 148 ulp_blob_init(struct ulp_blob *blob, 149 uint16_t bitlen, 150 enum bnxt_ulp_byte_order order); 151 152 /* 153 * Add data to the binary blob at the current offset. 154 * 155 * blob [in] The blob that data is added to. The blob must 156 * be initialized prior to pushing data. 157 * 158 * data [in] A pointer to bytes to be added to the blob. 159 * 160 * datalen [in] The number of bits to be added to the blob. 161 * 162 * The offset of the data is updated after each push of data. 163 * NULL returned on error. 164 */ 165 uint32_t 166 ulp_blob_push(struct ulp_blob *blob, 167 uint8_t *data, 168 uint32_t datalen); 169 170 /* 171 * Add data to the binary blob at the current offset. 172 * 173 * blob [in] The blob that data is added to. The blob must 174 * be initialized prior to pushing data. 175 * 176 * data [in] 64-bit value to be added to the blob. 177 * 178 * datalen [in] The number of bits to be added to the blob. 179 * 180 * The offset of the data is updated after each push of data. 181 * NULL returned on error, ptr to pushed data otherwise 182 */ 183 uint8_t * 184 ulp_blob_push_64(struct ulp_blob *blob, 185 uint64_t *data, 186 uint32_t datalen); 187 188 /* 189 * Add data to the binary blob at the current offset. 190 * 191 * blob [in] The blob that data is added to. The blob must 192 * be initialized prior to pushing data. 193 * 194 * data [in] 32-bit value to be added to the blob. 195 * 196 * datalen [in] The number of bits to be added ot the blob. 197 * 198 * The offset of the data is updated after each push of data. 199 * NULL returned on error, pointer pushed value otherwise. 200 */ 201 uint8_t * 202 ulp_blob_push_32(struct ulp_blob *blob, 203 uint32_t *data, 204 uint32_t datalen); 205 206 /* 207 * Add encap data to the binary blob at the current offset. 208 * 209 * blob [in] The blob that data is added to. The blob must 210 * be initialized prior to pushing data. 211 * 212 * data [in] value to be added to the blob. 213 * 214 * datalen [in] The number of bits to be added to the blob. 215 * 216 * The offset of the data is updated after each push of data. 217 * NULL returned on error, pointer pushed value otherwise. 218 */ 219 uint32_t 220 ulp_blob_push_encap(struct ulp_blob *blob, 221 uint8_t *data, 222 uint32_t datalen); 223 224 /* 225 * Get the data portion of the binary blob. 226 * 227 * blob [in] The blob's data to be retrieved. The blob must be 228 * initialized prior to pushing data. 229 * 230 * datalen [out] The number of bits to that are filled. 231 * 232 * returns a byte array of the blob data. Returns NULL on error. 233 */ 234 uint8_t * 235 ulp_blob_data_get(struct ulp_blob *blob, 236 uint16_t *datalen); 237 238 /* 239 * Extract data from the binary blob using given offset. 240 * 241 * blob [in] The blob that data is extracted from. The blob must 242 * be initialized prior to pulling data. 243 * 244 * data [in] A pointer to put the data. 245 * data_size [in] size of the data buffer in bytes. 246 *offset [in] - Offset in the blob to extract the data in bits format. 247 * len [in] The number of bits to be pulled from the blob. 248 * 249 * Output: zero on success, -1 on failure 250 */ 251 int32_t 252 ulp_blob_pull(struct ulp_blob *blob, uint8_t *data, uint32_t data_size, 253 uint16_t offset, uint16_t len); 254 255 /* 256 * Adds pad to an initialized blob at the current offset 257 * 258 * blob [in] The blob that data is added to. The blob must 259 * be initialized prior to pushing data. 260 * 261 * datalen [in] The number of bits of pad to add 262 * 263 * returns the number of pad bits added, -1 on failure 264 */ 265 int32_t 266 ulp_blob_pad_push(struct ulp_blob *blob, 267 uint32_t datalen); 268 269 /* 270 * Set the 64 bit swap start index of the binary blob. 271 * 272 * blob [in] The blob's data to be retrieved. The blob must be 273 * initialized prior to pushing data. 274 * 275 * returns void. 276 */ 277 void 278 ulp_blob_encap_swap_idx_set(struct ulp_blob *blob); 279 280 /* 281 * Perform the encap buffer swap to 64 bit reversal. 282 * 283 * blob [in] The blob's data to be used for swap. 284 * 285 * returns void. 286 */ 287 void 288 ulp_blob_perform_encap_swap(struct ulp_blob *blob); 289 290 /* 291 * Perform the blob buffer reversal byte wise. 292 * This api makes the first byte the last and 293 * vice-versa. 294 * 295 * blob [in] The blob's data to be used for swap. 296 * 297 * returns void. 298 */ 299 void 300 ulp_blob_perform_byte_reverse(struct ulp_blob *blob); 301 302 /* 303 * Read data from the operand 304 * 305 * operand [in] A pointer to a 16 Byte operand 306 * 307 * val [in/out] The variable to copy the operand to 308 * 309 * bitlen [in] The number of bits to read into val 310 * 311 * returns number of bits read, zero on error 312 */ 313 uint16_t 314 ulp_operand_read(uint8_t *operand, 315 uint8_t *val, 316 uint16_t bitlen); 317 318 /* 319 * copy the buffer in the encap format which is 2 bytes. 320 * The MSB of the src is placed at the LSB of dst. 321 * 322 * dst [out] The destination buffer 323 * src [in] The source buffer dst 324 * size[in] size of the buffer. 325 * align[in] The alignment is either 8 or 16. 326 */ 327 void 328 ulp_encap_buffer_copy(uint8_t *dst, 329 const uint8_t *src, 330 uint16_t size, 331 uint16_t align); 332 333 /* 334 * Check the buffer is empty 335 * 336 * buf [in] The buffer 337 * size [in] The size of the buffer 338 */ 339 int32_t ulp_buffer_is_empty(const uint8_t *buf, uint32_t size); 340 341 /* Function to check if bitmap is zero.Return 1 on success */ 342 uint32_t ulp_bitmap_is_zero(uint8_t *bitmap, int32_t size); 343 344 /* Function to check if bitmap is ones. Return 1 on success */ 345 uint32_t ulp_bitmap_is_ones(uint8_t *bitmap, int32_t size); 346 347 /* Function to check if bitmap is not zero. Return 1 on success */ 348 uint32_t ulp_bitmap_notzero(uint8_t *bitmap, int32_t size); 349 350 #endif /* _ULP_UTILS_H_ */ 351