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