1 /* 2 * Copyright (c) 2014-2021 The Linux Foundation. All rights reserved. 3 * Copyright (c) 2021-2023 Qualcomm Innovation Center, Inc. All rights reserved. 4 * 5 * Permission to use, copy, modify, and/or distribute this software for 6 * any purpose with or without fee is hereby granted, provided that the 7 * above copyright notice and this permission notice appear in all 8 * copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL 11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED 12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE 13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL 14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 * PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 /** 21 * DOC: qdf_util.h 22 * This file defines utility functions. 23 */ 24 25 #ifndef _QDF_UTIL_H 26 #define _QDF_UTIL_H 27 28 #include <i_qdf_util.h> 29 30 #ifdef QCA_CONFIG_SMP 31 #define QDF_MAX_AVAILABLE_CPU 8 32 #else 33 #define QDF_MAX_AVAILABLE_CPU 1 34 #endif 35 36 typedef __qdf_wait_queue_head_t qdf_wait_queue_head_t; 37 38 /** 39 * qdf_unlikely - Compiler-dependent macro denoting code unlikely to execute 40 * @_expr: expression to be checked 41 */ 42 #define qdf_unlikely(_expr) __qdf_unlikely(_expr) 43 44 /** 45 * qdf_likely - Compiler-dependent macro denoting code likely to execute 46 * @_expr: expression to be checked 47 */ 48 #define qdf_likely(_expr) __qdf_likely(_expr) 49 50 /** 51 * qdf_wmb - write memory barrier. 52 */ 53 #define qdf_wmb() __qdf_wmb() 54 55 /** 56 * qdf_rmb - read memory barrier. 57 */ 58 #define qdf_rmb() __qdf_rmb() 59 60 /** 61 * qdf_mb - read + write memory barrier. 62 */ 63 #define qdf_mb() __qdf_mb() 64 65 /** 66 * qdf_ioread32 - read a register 67 * @offset: register address 68 */ 69 #define qdf_ioread32(offset) __qdf_ioread32(offset) 70 71 /** 72 * qdf_iowrite32 - write a register 73 * @offset: register address 74 * @value: value to write (32bit value) 75 */ 76 #define qdf_iowrite32(offset, value) __qdf_iowrite32(offset, value) 77 78 /** 79 * qdf_assert - assert "expr" evaluates to false. 80 * @expr: expression to test 81 */ 82 #ifdef QDF_DEBUG 83 #define qdf_assert(expr) __qdf_assert(expr) 84 #else 85 #define qdf_assert(expr) 86 #endif /* QDF_DEBUG */ 87 88 /** 89 * qdf_assert_always - always assert "expr" evaluates to false. 90 * @expr: expression to test 91 */ 92 #define qdf_assert_always(expr) __qdf_assert(expr) 93 94 /** 95 * qdf_target_assert_always - always target assert "expr" evaluates to false. 96 * @expr: expression to test 97 */ 98 #define qdf_target_assert_always(expr) __qdf_target_assert(expr) 99 100 #define QDF_SET_PARAM(__param, __val) ((__param) |= (1 << (__val))) 101 #define QDF_HAS_PARAM(__param, __val) ((__param) & (1 << (__val))) 102 #define QDF_CLEAR_PARAM(__param, __val) ((__param) &= (~(1 << (__val)))) 103 104 /** 105 * QDF_MAX - get maximum of two values 106 * @_x: 1st argument 107 * @_y: 2nd argument 108 */ 109 #define QDF_MAX(_x, _y) (((_x) > (_y)) ? (_x) : (_y)) 110 111 /** 112 * QDF_MIN - get minimum of two values 113 * @_x: 1st argument 114 * @_y: 2nd argument 115 */ 116 #define QDF_MIN(_x, _y) (((_x) < (_y)) ? (_x) : (_y)) 117 118 /** 119 * QDF_IS_ADDR_BROADCAST - is mac address broadcast mac address 120 * @_a: pointer to mac address 121 */ 122 #define QDF_IS_ADDR_BROADCAST(_a) \ 123 ((_a)[0] == 0xff && \ 124 (_a)[1] == 0xff && \ 125 (_a)[2] == 0xff && \ 126 (_a)[3] == 0xff && \ 127 (_a)[4] == 0xff && \ 128 (_a)[5] == 0xff) 129 130 /** 131 * QDF_IS_LAST_3_BYTES_OF_MAC_SAME - check the last 3 bytes 132 * same or not for two mac addresses 133 * @mac1: mac address 1 134 * @mac2: mac address 2 135 */ 136 #define QDF_IS_LAST_3_BYTES_OF_MAC_SAME(mac1, mac2) \ 137 ((mac1)->bytes[3] == (mac2)->bytes[3] && \ 138 (mac1)->bytes[4] == (mac2)->bytes[4] && \ 139 (mac1)->bytes[5] == (mac2)->bytes[5]) 140 141 /* Get number of bits from the index bit */ 142 #define QDF_GET_BITS(_val, _index, _num_bits) \ 143 (((_val) >> (_index)) & ((1 << (_num_bits)) - 1)) 144 145 /* Set val to number of bits from the index bit */ 146 #define QDF_SET_BITS(_var, _index, _num_bits, _val) do { \ 147 (_var) &= ~(((1 << (_num_bits)) - 1) << (_index)); \ 148 (_var) |= (((_val) & ((1 << (_num_bits)) - 1)) << (_index)); \ 149 } while (0) 150 151 #define QDF_SET_BITS64(_var, _tmp, _index, _num_bits, _val) do { \ 152 (_var) = (((_var) & 0xffffffff00000000) >> 32); \ 153 (_var) &= ~(((1 << (_num_bits)) - 1) << ((_index) - 32)); \ 154 (_var) |= (((_val) & ((1 << (_num_bits)) - 1)) << ((_index) - 32)); \ 155 (_var) = (((_var) & 0x00000000ffffffff) << 32); \ 156 (_var) |= ((_tmp) & 0x00000000ffffffff); \ 157 } while (0) 158 159 /* Get number of bits from the index bit supporting 64 bits */ 160 #define QDF_GET_BITS64(_val, _index, _num_bits) \ 161 (((_val) >> (_index)) & ((1LLU << (_num_bits)) - 1)) 162 163 #define QDF_DECLARE_EWMA(name, factor, weight) \ 164 __QDF_DECLARE_EWMA(name, factor, weight) 165 166 #define qdf_ewma_tx_lag __qdf_ewma_tx_lag 167 168 #define qdf_ewma_tx_lag_init(tx_lag) \ 169 __qdf_ewma_tx_lag_init(tx_lag) 170 171 #define qdf_ewma_tx_lag_add(tx_lag, value) \ 172 __qdf_ewma_tx_lag_add(tx_lag, value) 173 174 #define qdf_ewma_tx_lag_read(tx_lag) \ 175 __qdf_ewma_tx_lag_read(tx_lag) 176 177 #define qdf_ewma_rx_rssi __qdf_ewma_rx_rssi 178 179 #define qdf_ewma_rx_rssi_init(rx_rssi) \ 180 __qdf_ewma_rx_rssi_init(rx_rssi) 181 182 #define qdf_ewma_rx_rssi_add(rx_rssi, value) \ 183 __qdf_ewma_rx_rssi_add(rx_rssi, value) 184 185 #define qdf_ewma_rx_rssi_read(rx_rssi) \ 186 __qdf_ewma_rx_rssi_read(rx_rssi) 187 188 #define QDF_CHAR_BIT 8 189 190 /** 191 * qdf_bitmap - Define a bitmap 192 * @name: name of the bitmap 193 * @bits: num of bits in the bitmap 194 * 195 * Return: none 196 */ 197 #define qdf_bitmap(name, bits) __qdf_bitmap(name, bits) 198 199 /** 200 * qdf_set_bit() - set bit in address 201 * @nr: bit number to be set 202 * @addr: address buffer pointer 203 * 204 * Return: none 205 */ 206 #define qdf_set_bit(nr, addr) __qdf_set_bit(nr, addr) 207 208 /** 209 * qdf_clear_bit() - clear bit in address 210 * @nr: bit number to be clear 211 * @addr: address buffer pointer 212 * 213 * Return: none 214 */ 215 #define qdf_clear_bit(nr, addr) __qdf_clear_bit(nr, addr) 216 217 /** 218 * qdf_test_bit() - test bit position in address 219 * @nr: bit number to be tested 220 * @addr: address buffer pointer 221 * 222 * Return: none 223 */ 224 #define qdf_test_bit(nr, addr) __qdf_test_bit(nr, addr) 225 226 /** 227 * qdf_test_and_clear_bit() - test and clear bit position in address 228 * @nr: bit number to be tested 229 * @addr: address buffer pointer 230 * 231 * Return: none 232 */ 233 #define qdf_test_and_clear_bit(nr, addr) __qdf_test_and_clear_bit(nr, addr) 234 235 /** 236 * qdf_find_first_bit() - find first bit position in address 237 * @addr: address buffer pointer 238 * @nbits: number of bits 239 * 240 * Return: position first set bit in addr 241 */ 242 #define qdf_find_first_bit(addr, nbits) __qdf_find_first_bit(addr, nbits) 243 244 /** 245 * qdf_bitmap_empty() - Check if bitmap is empty 246 * @addr: Address buffer pointer 247 * @nbits: Number of bits 248 * 249 * Return: True if no bit set, else false 250 */ 251 #define qdf_bitmap_empty(addr, nbits) __qdf_bitmap_empty(addr, nbits) 252 253 /** 254 * qdf_bitmap_and() - AND operation on the bitmap 255 * @dst: Destination buffer pointer 256 * @src1: First source buffer pointer 257 * @src2: Second source buffer pointer 258 * @nbits: Number of bits 259 * 260 * Return: Bitwise and of src1 and src2 in dst 261 */ 262 #define qdf_bitmap_and(dst, src1, src2, nbits) \ 263 __qdf_bitmap_and(dst, src1, src2, nbits) 264 265 #define qdf_wait_queue_interruptible(wait_queue, condition) \ 266 __qdf_wait_queue_interruptible(wait_queue, condition) 267 268 /** 269 * qdf_wait_queue_timeout() - wait for specified time on given condition 270 * @wait_queue: wait queue to wait on 271 * @condition: condition to wait on 272 * @timeout: timeout value in jiffies 273 * 274 * Return: 0 if condition becomes false after timeout 275 * 1 or remaining jiffies, if condition becomes true during timeout 276 */ 277 #define qdf_wait_queue_timeout(wait_queue, condition, timeout) \ 278 __qdf_wait_queue_timeout(wait_queue, \ 279 condition, timeout) 280 281 282 #define qdf_init_waitqueue_head(_q) __qdf_init_waitqueue_head(_q) 283 284 #define qdf_wake_up_interruptible(_q) __qdf_wake_up_interruptible(_q) 285 286 /** 287 * qdf_wake_up() - wakes up sleeping waitqueue 288 * @_q: wait queue, which needs wake up 289 * 290 * Return: none 291 */ 292 #define qdf_wake_up(_q) __qdf_wake_up(_q) 293 294 #define qdf_wake_up_completion(_q) __qdf_wake_up_completion(_q) 295 296 /** 297 * qdf_container_of() - cast a member of a structure out to the containing 298 * structure 299 * @ptr: the pointer to the member. 300 * @type: the type of the container struct this is embedded in. 301 * @member: the name of the member within the struct. 302 */ 303 #define qdf_container_of(ptr, type, member) \ 304 __qdf_container_of(ptr, type, member) 305 306 /** 307 * QDF_IS_PWR2() - test input value is power of 2 integer 308 * @value: input integer 309 */ 310 #define QDF_IS_PWR2(value) (((value) ^ ((value)-1)) == ((value) << 1) - 1) 311 312 /** 313 * qdf_roundup() - roundup the input value 314 * @x: value to roundup 315 * @y: input value rounded to multiple of this 316 * 317 * Return: rounded value 318 */ 319 #define qdf_roundup(x, y) __qdf_roundup(x, y) 320 321 /** 322 * qdf_ceil() - roundup of x/y 323 * @x: dividend 324 * @y: divisor 325 * 326 * Return: rounded value 327 */ 328 #define qdf_ceil(x, y) __qdf_ceil(x, y) 329 330 /** 331 * qdf_in_interrupt - returns true if in interrupt context 332 */ 333 #define qdf_in_interrupt __qdf_in_interrupt 334 335 /** 336 * qdf_is_macaddr_equal() - compare two QDF MacAddress 337 * @mac_addr1: Pointer to one qdf MacAddress to compare 338 * @mac_addr2: Pointer to the other qdf MacAddress to compare 339 * 340 * This function returns a bool that tells if a two QDF MacAddress' 341 * are equivalent. 342 * 343 * Return: true if the MacAddress's are equal 344 * not true if the MacAddress's are not equal 345 */ 346 static inline bool qdf_is_macaddr_equal(const struct qdf_mac_addr *mac_addr1, 347 const struct qdf_mac_addr *mac_addr2) 348 { 349 return __qdf_is_macaddr_equal(mac_addr1, mac_addr2); 350 } 351 352 353 /** 354 * qdf_is_macaddr_zero() - check for a MacAddress of all zeros. 355 * @mac_addr: pointer to the struct qdf_mac_addr to check. 356 * 357 * This function returns a bool that tells if a MacAddress is made up of 358 * all zeros. 359 * 360 * Return: true if the MacAddress is all Zeros 361 * false if the MacAddress is not all Zeros. 362 */ 363 static inline bool qdf_is_macaddr_zero(const struct qdf_mac_addr *mac_addr) 364 { 365 struct qdf_mac_addr zero_mac_addr = QDF_MAC_ADDR_ZERO_INIT; 366 367 return qdf_is_macaddr_equal(mac_addr, &zero_mac_addr); 368 } 369 370 /** 371 * qdf_zero_macaddr() - zero out a MacAddress 372 * @mac_addr: pointer to the struct qdf_mac_addr to zero. 373 * 374 * This function zeros out a QDF MacAddress type. 375 * 376 * Return: none 377 */ 378 static inline void qdf_zero_macaddr(struct qdf_mac_addr *mac_addr) 379 { 380 __qdf_zero_macaddr(mac_addr); 381 } 382 383 384 /** 385 * qdf_is_macaddr_group() - check for a MacAddress is a 'group' address 386 * @mac_addr: pointer to the qdf MacAddress to check 387 * 388 * This function returns a bool that tells if a the input QDF MacAddress 389 * is a "group" address. Group addresses have the 'group address bit' turned 390 * on in the MacAddress. Group addresses are made up of Broadcast and 391 * Multicast addresses. 392 * 393 * Return: true if the input MacAddress is a Group address 394 * false if the input MacAddress is not a Group address 395 */ 396 static inline bool qdf_is_macaddr_group(struct qdf_mac_addr *mac_addr) 397 { 398 return mac_addr->bytes[0] & 0x01; 399 } 400 401 402 /** 403 * qdf_is_macaddr_broadcast() - check for a MacAddress is a broadcast address 404 * @mac_addr: Pointer to the qdf MacAddress to check 405 * 406 * This function returns a bool that tells if a the input QDF MacAddress 407 * is a "broadcast" address. 408 * 409 * Return: true if the input MacAddress is a broadcast address 410 * flase if the input MacAddress is not a broadcast address 411 */ 412 static inline bool qdf_is_macaddr_broadcast(const struct qdf_mac_addr *mac_addr) 413 { 414 struct qdf_mac_addr broadcast_mac_addr = QDF_MAC_ADDR_BCAST_INIT; 415 return qdf_is_macaddr_equal(mac_addr, &broadcast_mac_addr); 416 } 417 418 /** 419 * qdf_copy_macaddr() - copy a QDF MacAddress 420 * @dst_addr: pointer to the qdf MacAddress to copy TO (the destination) 421 * @src_addr: pointer to the qdf MacAddress to copy FROM (the source) 422 * 423 * This function copies a QDF MacAddress into another QDF MacAddress. 424 * 425 * Return: none 426 */ 427 static inline void qdf_copy_macaddr(struct qdf_mac_addr *dst_addr, 428 const struct qdf_mac_addr *src_addr) 429 { 430 *dst_addr = *src_addr; 431 } 432 433 /** 434 * qdf_set_macaddr_broadcast() - set a QDF MacAddress to the 'broadcast' 435 * @mac_addr: pointer to the qdf MacAddress to set to broadcast 436 * 437 * This function sets a QDF MacAddress to the 'broadcast' MacAddress. Broadcast 438 * MacAddress contains all 0xFF bytes. 439 * 440 * Return: none 441 */ 442 static inline void qdf_set_macaddr_broadcast(struct qdf_mac_addr *mac_addr) 443 { 444 __qdf_set_macaddr_broadcast(mac_addr); 445 } 446 447 /** 448 * qdf_set_u16() - Assign 16-bit unsigned value to a byte array base on CPU's 449 * endianness. 450 * @ptr: Starting address of a byte array 451 * @value: The value to assign to the byte array 452 * 453 * Caller must validate the byte array has enough space to hold the value 454 * 455 * Return: The address to the byte after the assignment. This may or may not 456 * be valid. Caller to verify. 457 */ 458 static inline uint8_t *qdf_set_u16(uint8_t *ptr, uint16_t value) 459 { 460 #if defined(ANI_BIG_BYTE_ENDIAN) 461 *(ptr) = (uint8_t) (value >> 8); 462 *(ptr + 1) = (uint8_t) (value); 463 #else 464 *(ptr + 1) = (uint8_t) (value >> 8); 465 *(ptr) = (uint8_t) (value); 466 #endif 467 return ptr + 2; 468 } 469 470 /** 471 * qdf_get_u16() - Retrieve a 16-bit unsigned value from a byte array base on 472 * CPU's endianness. 473 * @ptr: Starting address of a byte array 474 * @value: Pointer to a caller allocated buffer for 16 bit value. Value is to 475 * assign to this location. 476 * 477 * Caller must validate the byte array has enough space to hold the value 478 * 479 * Return: The address to the byte after the assignment. This may or may not 480 * be valid. Caller to verify. 481 */ 482 static inline uint8_t *qdf_get_u16(uint8_t *ptr, uint16_t *value) 483 { 484 #if defined(ANI_BIG_BYTE_ENDIAN) 485 *value = (((uint16_t) (*ptr << 8)) | ((uint16_t) (*(ptr + 1)))); 486 #else 487 *value = (((uint16_t) (*(ptr + 1) << 8)) | ((uint16_t) (*ptr))); 488 #endif 489 return ptr + 2; 490 } 491 492 /** 493 * qdf_get_u32() - retrieve a 32-bit unsigned value from a byte array base on 494 * CPU's endianness. 495 * @ptr: Starting address of a byte array 496 * @value: Pointer to a caller allocated buffer for 32 bit value. Value is to 497 * assign to this location. 498 * 499 * Caller must validate the byte array has enough space to hold the value 500 * 501 * Return: The address to the byte after the assignment. This may or may not 502 * be valid. Caller to verify. 503 */ 504 static inline uint8_t *qdf_get_u32(uint8_t *ptr, uint32_t *value) 505 { 506 #if defined(ANI_BIG_BYTE_ENDIAN) 507 *value = ((uint32_t) (*(ptr) << 24) | 508 (uint32_t) (*(ptr + 1) << 16) | 509 (uint32_t) (*(ptr + 2) << 8) | (uint32_t) (*(ptr + 3))); 510 #else 511 *value = ((uint32_t) (*(ptr + 3) << 24) | 512 (uint32_t) (*(ptr + 2) << 16) | 513 (uint32_t) (*(ptr + 1) << 8) | (uint32_t) (*(ptr))); 514 #endif 515 return ptr + 4; 516 } 517 518 /** 519 * qdf_abs() - Get absolute value 520 * @x: value to be converted 521 */ 522 #define qdf_abs(x) __qdf_abs(x) 523 524 /** 525 * qdf_ntohs() - Convert a 16-bit value from network byte order to host byte order 526 * @x: value to be converted 527 */ 528 #define qdf_ntohs(x) __qdf_ntohs(x) 529 530 /** 531 * qdf_ntohl() - Convert a 32-bit value from network byte order to host byte order 532 * @x: value to be converted 533 */ 534 #define qdf_ntohl(x) __qdf_ntohl(x) 535 536 /** 537 * qdf_htons() - Convert a 16-bit value from host byte order to network byte order 538 * @x: value to be converted 539 */ 540 #define qdf_htons(x) __qdf_htons(x) 541 542 /** 543 * qdf_htonl() - Convert a 32-bit value from host byte order to network byte order 544 * @x: value to be converted 545 */ 546 #define qdf_htonl(x) __qdf_htonl(x) 547 548 /** 549 * qdf_cpu_to_le16() - Convert a 16-bit value from CPU byte order to 550 * little-endian byte order 551 * 552 * @x: value to be converted 553 */ 554 #define qdf_cpu_to_le16(x) __qdf_cpu_to_le16(x) 555 556 /** 557 * qdf_cpu_to_le32() - Convert a 32-bit value from CPU byte order to 558 * little-endian byte order 559 * 560 * @x: value to be converted 561 */ 562 #define qdf_cpu_to_le32(x) __qdf_cpu_to_le32(x) 563 564 /** 565 * qdf_cpu_to_le64() - Convert a 64-bit value from CPU byte order to 566 * little-endian byte order 567 * 568 * @x: value to be converted 569 */ 570 #define qdf_cpu_to_le64(x) __qdf_cpu_to_le64(x) 571 572 /** 573 * qdf_le16_to_cpu() - Convert a 16-bit value from little-endian byte order 574 * to CPU byte order 575 * 576 * @x: value to be converted 577 */ 578 #define qdf_le16_to_cpu(x) __qdf_le16_to_cpu(x) 579 580 /** 581 * qdf_le32_to_cpu() - Convert a 32-bit value from little-endian byte 582 * order to CPU byte order 583 * 584 * @x: value to be converted 585 */ 586 #define qdf_le32_to_cpu(x) __qdf_le32_to_cpu(x) 587 588 /** 589 * qdf_le64_to_cpu() - Convert a 64-bit value from little-endian byte 590 * order to CPU byte order 591 * 592 * @x: value to be converted 593 */ 594 #define qdf_le64_to_cpu(x) __qdf_le64_to_cpu(x) 595 596 /** 597 * qdf_cpu_to_be16() - Convert a 16-bit value from CPU byte order to 598 * big-endian byte order 599 * 600 * @x: value to be converted 601 */ 602 #define qdf_cpu_to_be16(x) __qdf_cpu_to_be16(x) 603 604 /** 605 * qdf_cpu_to_be32() - Convert a 32-bit value from CPU byte order to 606 * big-endian byte order 607 * 608 * @x: value to be converted 609 */ 610 #define qdf_cpu_to_be32(x) __qdf_cpu_to_be32(x) 611 612 /** 613 * qdf_cpu_to_be64() - Convert a 64-bit value from CPU byte order to 614 * big-endian byte order 615 * 616 * @x: value to be converted 617 */ 618 #define qdf_cpu_to_be64(x) __qdf_cpu_to_be64(x) 619 620 621 /** 622 * qdf_be16_to_cpu() - Convert a 16-bit value from big-endian byte order 623 * to CPU byte order 624 * 625 * @x: value to be converted 626 */ 627 #define qdf_be16_to_cpu(x) __qdf_be16_to_cpu(x) 628 629 /** 630 * qdf_be32_to_cpu() - Convert a 32-bit value from big-endian byte order 631 * to CPU byte order 632 * 633 * @x: value to be converted 634 */ 635 #define qdf_be32_to_cpu(x) __qdf_be32_to_cpu(x) 636 637 /** 638 * qdf_be64_to_cpu() - Convert a 64-bit value from big-endian byte order 639 * to CPU byte order 640 * 641 * @x: value to be converted 642 */ 643 #define qdf_be64_to_cpu(x) __qdf_be64_to_cpu(x) 644 645 /** 646 * qdf_function - replace with the name of the current function 647 */ 648 #define qdf_function __qdf_function 649 650 /** 651 * qdf_min() - minimum of two numbers 652 * @a: first number 653 * @b: second number 654 */ 655 #define qdf_min(a, b) __qdf_min(a, b) 656 657 /** 658 * qdf_ffz() - find first (least significant) zero bit 659 * @mask: the bitmask to check 660 * 661 * Return: The zero-based index of the first zero bit, or -1 if none are found 662 */ 663 #define qdf_ffz(mask) __qdf_ffz(mask) 664 665 /** 666 * qdf_prefetch - prefetches the cacheline for read 667 * 668 * @x: address to be prefetched 669 */ 670 #define qdf_prefetch(x) __qdf_prefetch(x) 671 672 /** 673 * qdf_get_pwr2() - get next power of 2 integer from input value 674 * @value: input value to find next power of 2 integer 675 * 676 * Get next power of 2 integer from input value 677 * 678 * Return: Power of 2 integer 679 */ 680 static inline int qdf_get_pwr2(int value) 681 { 682 int log2; 683 684 if (QDF_IS_PWR2(value)) 685 return value; 686 687 log2 = 0; 688 while (value) { 689 value >>= 1; 690 log2++; 691 } 692 return 1 << log2; 693 } 694 695 static inline 696 int qdf_get_cpu(void) 697 { 698 return __qdf_get_cpu(); 699 } 700 701 /** 702 * qdf_get_hweight8() - count num of 1's in 8-bit bitmap 703 * @w: input bitmap 704 * 705 * Count num of 1's set in the 8-bit bitmap 706 * 707 * Return: num of 1's 708 */ 709 static inline 710 unsigned int qdf_get_hweight8(unsigned int w) 711 { 712 unsigned int res = w - ((w >> 1) & 0x55); 713 res = (res & 0x33) + ((res >> 2) & 0x33); 714 return (res + (res >> 4)) & 0x0F; 715 } 716 717 /** 718 * qdf_get_hweight16() - count num of 1's in 16-bit bitmap 719 * @w: input bitmap 720 * 721 * Count num of 1's set in the 16-bit bitmap 722 * 723 * Return: num of 1's 724 */ 725 static inline 726 unsigned int qdf_get_hweight16(unsigned int w) 727 { 728 unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555); 729 730 res = (res & 0x3333) + ((res >> 2) & 0x3333); 731 res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F); 732 return (res & 0x00FF) + ((res >> 8) & 0x00FF); 733 } 734 735 /** 736 * qdf_get_hweight32() - count num of 1's in 32-bit bitmap 737 * @w: input bitmap 738 * 739 * Count num of 1's set in the 32-bit bitmap 740 * 741 * Return: num of 1's 742 */ 743 static inline 744 unsigned int qdf_get_hweight32(unsigned int w) 745 { 746 unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555); 747 748 res = (res & 0x33333333) + ((res >> 2) & 0x33333333); 749 res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F); 750 res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF); 751 return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF); 752 } 753 754 /** 755 * qdf_device_init_wakeup() - allow a device to wake up the aps system 756 * @qdf_dev: the qdf device context 757 * @enable: enable/disable the device as a wakeup source 758 * 759 * Return: 0 or errno 760 */ 761 static inline int qdf_device_init_wakeup(qdf_device_t qdf_dev, bool enable) 762 { 763 return __qdf_device_init_wakeup(qdf_dev, enable); 764 } 765 766 static inline 767 uint64_t qdf_get_totalramsize(void) 768 { 769 return __qdf_get_totalramsize(); 770 } 771 772 /** 773 * qdf_get_lower_32_bits() - get lower 32 bits from an address. 774 * @addr: address 775 * 776 * This api returns the lower 32 bits of an address. 777 * 778 * Return: lower 32 bits. 779 */ 780 static inline 781 uint32_t qdf_get_lower_32_bits(qdf_dma_addr_t addr) 782 { 783 return __qdf_get_lower_32_bits(addr); 784 } 785 786 /** 787 * qdf_get_upper_32_bits() - get upper 32 bits from an address. 788 * @addr: address 789 * 790 * This api returns the upper 32 bits of an address. 791 * 792 * Return: upper 32 bits. 793 */ 794 static inline 795 uint32_t qdf_get_upper_32_bits(qdf_dma_addr_t addr) 796 { 797 return __qdf_get_upper_32_bits(addr); 798 } 799 800 /** 801 * qdf_rounddown_pow_of_two() - Round down to nearest power of two 802 * @n: number to be tested 803 * 804 * Test if the input number is power of two, and return the nearest power of two 805 * 806 * Return: number rounded down to the nearest power of two 807 */ 808 static inline 809 unsigned long qdf_rounddown_pow_of_two(unsigned long n) 810 { 811 return __qdf_rounddown_pow_of_two(n); 812 } 813 814 /** 815 * qdf_set_dma_coherent_mask() - set max number of bits allowed in dma addr 816 * @dev: device pointer 817 * @addr_bits: max number of bits allowed in dma address 818 * 819 * This API sets the maximum allowed number of bits in the dma address. 820 * 821 * Return: 0 - success, non zero - failure 822 */ 823 static inline 824 int qdf_set_dma_coherent_mask(struct device *dev, uint8_t addr_bits) 825 { 826 return __qdf_set_dma_coherent_mask(dev, addr_bits); 827 } 828 829 /** 830 * qdf_do_div() - wrapper function for kernel macro(do_div). 831 * @dividend: Dividend value 832 * @divisor : Divisor value 833 * 834 * Return: Quotient 835 */ 836 static inline 837 uint64_t qdf_do_div(uint64_t dividend, uint32_t divisor) 838 { 839 return __qdf_do_div(dividend, divisor); 840 } 841 842 /** 843 * qdf_do_div_rem() - wrapper function for kernel macro(do_div) 844 * to get remainder. 845 * @dividend: Dividend value 846 * @divisor : Divisor value 847 * 848 * Return: remainder 849 */ 850 static inline 851 uint64_t qdf_do_div_rem(uint64_t dividend, uint32_t divisor) 852 { 853 return __qdf_do_div_rem(dividend, divisor); 854 } 855 856 /** 857 * qdf_get_random_bytes() - returns nbytes bytes of random data 858 * @buf: buffer to fill 859 * @nbytes: number of bytes to fill 860 * 861 * Return: random bytes of data 862 */ 863 static inline 864 void qdf_get_random_bytes(void *buf, int nbytes) 865 { 866 return __qdf_get_random_bytes(buf, nbytes); 867 } 868 869 /** 870 * qdf_hex_to_bin() - QDF API to Convert hexa decimal ASCII character to 871 * unsigned integer value. 872 * @ch: hexa decimal ASCII character 873 * 874 * Return: For hexa decimal ASCII char return actual decimal value 875 * else -1 for bad input. 876 */ 877 static inline 878 int qdf_hex_to_bin(char ch) 879 { 880 return __qdf_hex_to_bin(ch); 881 } 882 883 /** 884 * qdf_hex_str_to_binary() - QDF API to Convert string of hexa decimal 885 * ASCII characters to array of unsigned integers. 886 * @dst: output array to hold converted values 887 * @src: input string of hexa decimal ASCII characters 888 * @count: size of dst string 889 * 890 * This function is used to convert string of hexa decimal characters to 891 * array of unsigned integers and caller should ensure: 892 * a) @dst, @src are not NULL, 893 * b) size of @dst should be (size of src / 2) 894 * 895 * Example 1: 896 * src = 11aa, means, src[0] = '1', src[1] = '2', src[2] = 'a', src[3] = 'a' 897 * count = (size of src / 2) = 2 898 * after conversion, dst[0] = 0x11, dst[1] = oxAA and return (0). 899 * 900 * Example 2: 901 * src = 11az, means, src[0] = '1', src[1] = '2', src[2] = 'a', src[3] = 'z' 902 * src[3] is not ASCII hexa decimal character, return negative value (-1). 903 * 904 * Return: For a string of hexa decimal ASCII characters return 0 905 * else -1 for bad input. 906 */ 907 static inline 908 int qdf_hex_str_to_binary(u8 *dst, const char *src, size_t count) 909 { 910 return __qdf_hex_str_to_binary(dst, src, count); 911 } 912 913 /** 914 * qdf_fls() - find last set bit in a given 32 bit input 915 * @x: 32 bit mask 916 * 917 * Return: zero if the input is zero, otherwise returns the bit 918 * position of the last set bit, where the LSB is 1 and MSB is 32. 919 */ 920 static inline 921 int qdf_fls(uint32_t x) 922 { 923 return __qdf_fls(x); 924 } 925 926 /** 927 * qdf_get_smp_processor_id() - Get the current CPU id 928 * 929 * Return: current CPU id 930 */ 931 static inline int qdf_get_smp_processor_id(void) 932 { 933 return __qdf_get_smp_processor_id(); 934 } 935 936 /** 937 * qdf_in_atomic: Check whether current thread running in atomic context 938 * 939 * Return: true if current thread is running in the atomic context 940 * else it will be return false. 941 */ 942 static inline bool qdf_in_atomic(void) 943 { 944 return __qdf_in_atomic(); 945 } 946 #endif /*_QDF_UTIL_H*/ 947