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