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