1 /* 2 * Copyright (c) 2014-2018 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_mb - read + write memory barrier. 51 */ 52 #define qdf_mb() __qdf_mb() 53 54 /** 55 * qdf_ioread32 - read a register 56 * @offset: register address 57 */ 58 #define qdf_ioread32(offset) __qdf_ioread32(offset) 59 /** 60 * qdf_iowrite32 - write a register 61 * @offset: register address 62 * @value: value to write (32bit value) 63 */ 64 #define qdf_iowrite32(offset, value) __qdf_iowrite32(offset, value) 65 66 /** 67 * qdf_assert - assert "expr" evaluates to false. 68 */ 69 #ifdef QDF_DEBUG 70 #define qdf_assert(expr) __qdf_assert(expr) 71 #else 72 #define qdf_assert(expr) 73 #endif /* QDF_DEBUG */ 74 75 /** 76 * qdf_assert_always - alway assert "expr" evaluates to false. 77 */ 78 #define qdf_assert_always(expr) __qdf_assert(expr) 79 80 /** 81 * qdf_target_assert_always - alway target assert "expr" evaluates to false. 82 */ 83 #define qdf_target_assert_always(expr) __qdf_target_assert(expr) 84 85 /** 86 * QDF_MAX - get maximum of two values 87 * @_x: 1st argument 88 * @_y: 2nd argument 89 */ 90 #define QDF_MAX(_x, _y) (((_x) > (_y)) ? (_x) : (_y)) 91 92 /** 93 * QDF_MIN - get minimum of two values 94 * @_x: 1st argument 95 * @_y: 2nd argument 96 */ 97 #define QDF_MIN(_x, _y) (((_x) < (_y)) ? (_x) : (_y)) 98 99 /** 100 * QDF_IS_ADDR_BROADCAST - is mac address broadcast mac address 101 * @_a: pointer to mac address 102 */ 103 #define QDF_IS_ADDR_BROADCAST(_a) \ 104 ((_a)[0] == 0xff && \ 105 (_a)[1] == 0xff && \ 106 (_a)[2] == 0xff && \ 107 (_a)[3] == 0xff && \ 108 (_a)[4] == 0xff && \ 109 (_a)[5] == 0xff) 110 111 /** 112 * qdf_status_to_os_return - returns the status to OS. 113 * @status: enum QDF_STATUS 114 * 115 * returns: int status success/failure 116 */ 117 static inline int qdf_status_to_os_return(QDF_STATUS status) 118 { 119 return __qdf_status_to_os_return(status); 120 } 121 122 /** 123 * qdf_status_from_os_return() - map OS specific return code to a QDF_STATUS 124 * @rc: the input return code to map 125 * 126 * Return: QDF_STATUS 127 */ 128 static inline QDF_STATUS qdf_status_from_os_return(int rc) 129 { 130 return __qdf_status_from_os_return(rc); 131 } 132 133 /** 134 * qdf_set_bit() - set bit in address 135 * @nr: bit number to be set 136 * @addr: address buffer pointer 137 * 138 * Return: none 139 */ 140 #define qdf_set_bit(nr, addr) __qdf_set_bit(nr, addr) 141 142 /** 143 * qdf_clear_bit() - clear bit in address 144 * @nr: bit number to be clear 145 * @addr: address buffer pointer 146 * 147 * Return: none 148 */ 149 #define qdf_clear_bit(nr, addr) __qdf_clear_bit(nr, addr) 150 151 /** 152 * qdf_test_bit() - test bit position in address 153 * @nr: bit number to be tested 154 * @addr: address buffer pointer 155 * 156 * Return: none 157 */ 158 #define qdf_test_bit(nr, addr) __qdf_test_bit(nr, addr) 159 160 /** 161 * qdf_test_and_clear_bit() - test and clear bit position in address 162 * @nr: bit number to be tested 163 * @addr: address buffer pointer 164 * 165 * Return: none 166 */ 167 #define qdf_test_and_clear_bit(nr, addr) __qdf_test_and_clear_bit(nr, addr) 168 169 /** 170 * qdf_find_first_bit() - find first bit position in address 171 * @addr: address buffer pointer 172 * @nbits: number of bits 173 * 174 * Return: position first set bit in addr 175 */ 176 #define qdf_find_first_bit(addr, nbits) __qdf_find_first_bit(addr, nbits) 177 178 #define qdf_wait_queue_interruptible(wait_queue, condition) \ 179 __qdf_wait_queue_interruptible(wait_queue, condition) 180 181 /** 182 * qdf_wait_queue_timeout() - wait for specified time on given condition 183 * @wait_queue: wait queue to wait on 184 * @condition: condition to wait on 185 * @timeout: timeout value in jiffies 186 * 187 * Return: 0 if condition becomes false after timeout 188 * 1 or remaining jiffies, if condition becomes true during timeout 189 */ 190 #define qdf_wait_queue_timeout(wait_queue, condition, timeout) \ 191 __qdf_wait_queue_timeout(wait_queue, \ 192 condition, timeout) 193 194 195 #define qdf_init_waitqueue_head(_q) __qdf_init_waitqueue_head(_q) 196 197 #define qdf_wake_up_interruptible(_q) __qdf_wake_up_interruptible(_q) 198 199 /** 200 * qdf_wake_up() - wakes up sleeping waitqueue 201 * @wait_queue: wait queue, which needs wake up 202 * 203 * Return: none 204 */ 205 #define qdf_wake_up(_q) __qdf_wake_up(_q) 206 207 #define qdf_wake_up_completion(_q) __qdf_wake_up_completion(_q) 208 209 /** 210 * qdf_container_of - cast a member of a structure out to the containing 211 * structure 212 * @ptr: the pointer to the member. 213 * @type: the type of the container struct this is embedded in. 214 * @member: the name of the member within the struct. 215 */ 216 #define qdf_container_of(ptr, type, member) \ 217 __qdf_container_of(ptr, type, member) 218 219 /** 220 * qdf_is_pwr2 - test input value is power of 2 integer 221 * @value: input integer 222 */ 223 #define QDF_IS_PWR2(value) (((value) ^ ((value)-1)) == ((value) << 1) - 1) 224 225 /** 226 * qdf_roundup() - roundup the input value 227 * @x: value to roundup 228 * @y: input value rounded to multiple of this 229 * 230 * Return: rounded value 231 */ 232 #define qdf_roundup(x, y) __qdf_roundup(x, y) 233 234 /** 235 * qdf_is_macaddr_equal() - compare two QDF MacAddress 236 * @mac_addr1: Pointer to one qdf MacAddress to compare 237 * @mac_addr2: Pointer to the other qdf MacAddress to compare 238 * 239 * This function returns a bool that tells if a two QDF MacAddress' 240 * are equivalent. 241 * 242 * Return: true if the MacAddress's are equal 243 * not true if the MacAddress's are not equal 244 */ 245 static inline bool qdf_is_macaddr_equal(struct qdf_mac_addr *mac_addr1, 246 struct qdf_mac_addr *mac_addr2) 247 { 248 return __qdf_is_macaddr_equal(mac_addr1, mac_addr2); 249 } 250 251 252 /** 253 * qdf_is_macaddr_zero() - check for a MacAddress of all zeros. 254 * @mac_addr: pointer to the struct qdf_mac_addr to check. 255 * 256 * This function returns a bool that tells if a MacAddress is made up of 257 * all zeros. 258 * 259 * Return: true if the MacAddress is all Zeros 260 * false if the MacAddress is not all Zeros. 261 */ 262 static inline bool qdf_is_macaddr_zero(struct qdf_mac_addr *mac_addr) 263 { 264 struct qdf_mac_addr zero_mac_addr = QDF_MAC_ADDR_ZERO_INIT; 265 266 return qdf_is_macaddr_equal(mac_addr, &zero_mac_addr); 267 } 268 269 /** 270 * qdf_zero_macaddr() - zero out a MacAddress 271 * @mac_addr: pointer to the struct qdf_mac_addr to zero. 272 * 273 * This function zeros out a QDF MacAddress type. 274 * 275 * Return: none 276 */ 277 static inline void qdf_zero_macaddr(struct qdf_mac_addr *mac_addr) 278 { 279 __qdf_zero_macaddr(mac_addr); 280 } 281 282 283 /** 284 * qdf_is_macaddr_group() - check for a MacAddress is a 'group' address 285 * @mac_addr1: pointer to the qdf MacAddress to check 286 * 287 * This function returns a bool that tells if a the input QDF MacAddress 288 * is a "group" address. Group addresses have the 'group address bit' turned 289 * on in the MacAddress. Group addresses are made up of Broadcast and 290 * Multicast addresses. 291 * 292 * Return: true if the input MacAddress is a Group address 293 * false if the input MacAddress is not a Group address 294 */ 295 static inline bool qdf_is_macaddr_group(struct qdf_mac_addr *mac_addr) 296 { 297 return mac_addr->bytes[0] & 0x01; 298 } 299 300 301 /** 302 * qdf_is_macaddr_broadcast() - check for a MacAddress is a broadcast address 303 * @mac_addr: Pointer to the qdf MacAddress to check 304 * 305 * This function returns a bool that tells if a the input QDF MacAddress 306 * is a "broadcast" address. 307 * 308 * Return: true if the input MacAddress is a broadcast address 309 * flase if the input MacAddress is not a broadcast address 310 */ 311 static inline bool qdf_is_macaddr_broadcast(struct qdf_mac_addr *mac_addr) 312 { 313 struct qdf_mac_addr broadcast_mac_addr = QDF_MAC_ADDR_BCAST_INIT; 314 return qdf_is_macaddr_equal(mac_addr, &broadcast_mac_addr); 315 } 316 317 /** 318 * qdf_copy_macaddr() - copy a QDF MacAddress 319 * @dst_addr: pointer to the qdf MacAddress to copy TO (the destination) 320 * @src_addr: pointer to the qdf MacAddress to copy FROM (the source) 321 * 322 * This function copies a QDF MacAddress into another QDF MacAddress. 323 * 324 * Return: none 325 */ 326 static inline void qdf_copy_macaddr(struct qdf_mac_addr *dst_addr, 327 struct qdf_mac_addr *src_addr) 328 { 329 *dst_addr = *src_addr; 330 } 331 332 /** 333 * qdf_set_macaddr_broadcast() - set a QDF MacAddress to the 'broadcast' 334 * @mac_addr: pointer to the qdf MacAddress to set to broadcast 335 * 336 * This function sets a QDF MacAddress to the 'broadcast' MacAddress. Broadcast 337 * MacAddress contains all 0xFF bytes. 338 * 339 * Return: none 340 */ 341 static inline void qdf_set_macaddr_broadcast(struct qdf_mac_addr *mac_addr) 342 { 343 __qdf_set_macaddr_broadcast(mac_addr); 344 } 345 346 /** 347 * qdf_set_u16() - Assign 16-bit unsigned value to a byte array base on CPU's 348 * endianness. 349 * @ptr: Starting address of a byte array 350 * @value: The value to assign to the byte array 351 * 352 * Caller must validate the byte array has enough space to hold the vlaue 353 * 354 * Return: The address to the byte after the assignment. This may or may not 355 * be valid. Caller to verify. 356 */ 357 static inline uint8_t *qdf_set_u16(uint8_t *ptr, uint16_t value) 358 { 359 #if defined(ANI_BIG_BYTE_ENDIAN) 360 *(ptr) = (uint8_t) (value >> 8); 361 *(ptr + 1) = (uint8_t) (value); 362 #else 363 *(ptr + 1) = (uint8_t) (value >> 8); 364 *(ptr) = (uint8_t) (value); 365 #endif 366 return ptr + 2; 367 } 368 369 /** 370 * qdf_get_u16() - Retrieve a 16-bit unsigned value from a byte array base on 371 * CPU's endianness. 372 * @ptr: Starting address of a byte array 373 * @value: Pointer to a caller allocated buffer for 16 bit value. Value is to 374 * assign to this location. 375 * 376 * Caller must validate the byte array has enough space to hold the vlaue 377 * 378 * Return: The address to the byte after the assignment. This may or may not 379 * be valid. Caller to verify. 380 */ 381 static inline uint8_t *qdf_get_u16(uint8_t *ptr, uint16_t *value) 382 { 383 #if defined(ANI_BIG_BYTE_ENDIAN) 384 *value = (((uint16_t) (*ptr << 8)) | ((uint16_t) (*(ptr + 1)))); 385 #else 386 *value = (((uint16_t) (*(ptr + 1) << 8)) | ((uint16_t) (*ptr))); 387 #endif 388 return ptr + 2; 389 } 390 391 /** 392 * qdf_get_u32() - retrieve a 32-bit unsigned value from a byte array base on 393 * CPU's endianness. 394 * @ptr: Starting address of a byte array 395 * @value: Pointer to a caller allocated buffer for 32 bit value. Value is to 396 * assign to this location. 397 * 398 * Caller must validate the byte array has enough space to hold the vlaue 399 * 400 * Return: The address to the byte after the assignment. This may or may not 401 * be valid. Caller to verify. 402 */ 403 static inline uint8_t *qdf_get_u32(uint8_t *ptr, uint32_t *value) 404 { 405 #if defined(ANI_BIG_BYTE_ENDIAN) 406 *value = ((uint32_t) (*(ptr) << 24) | 407 (uint32_t) (*(ptr + 1) << 16) | 408 (uint32_t) (*(ptr + 2) << 8) | (uint32_t) (*(ptr + 3))); 409 #else 410 *value = ((uint32_t) (*(ptr + 3) << 24) | 411 (uint32_t) (*(ptr + 2) << 16) | 412 (uint32_t) (*(ptr + 1) << 8) | (uint32_t) (*(ptr))); 413 #endif 414 return ptr + 4; 415 } 416 417 /** 418 * qdf_ntohs - Convert a 16-bit value from network byte order to host byte order 419 */ 420 #define qdf_ntohs(x) __qdf_ntohs(x) 421 422 /** 423 * qdf_ntohl - Convert a 32-bit value from network byte order to host byte order 424 */ 425 #define qdf_ntohl(x) __qdf_ntohl(x) 426 427 /** 428 * qdf_htons - Convert a 16-bit value from host byte order to network byte order 429 */ 430 #define qdf_htons(x) __qdf_htons(x) 431 432 /** 433 * qdf_htonl - Convert a 32-bit value from host byte order to network byte order 434 */ 435 #define qdf_htonl(x) __qdf_htonl(x) 436 437 /** 438 * qdf_cpu_to_le16 - Convert a 16-bit value from CPU byte order to 439 * little-endian byte order 440 * 441 * @x: value to be converted 442 */ 443 #define qdf_cpu_to_le16(x) __qdf_cpu_to_le16(x) 444 445 /** 446 * qdf_cpu_to_le32 - Convert a 32-bit value from CPU byte order to 447 * little-endian byte order 448 * 449 * @x: value to be converted 450 */ 451 #define qdf_cpu_to_le32(x) __qdf_cpu_to_le32(x) 452 453 /** 454 * qdf_cpu_to_le64 - Convert a 64-bit value from CPU byte order to 455 * little-endian byte order 456 * 457 * @x: value to be converted 458 */ 459 #define qdf_cpu_to_le64(x) __qdf_cpu_to_le64(x) 460 461 /** 462 * qdf_le16_to_cpu - Convert a 16-bit value from little-endian byte order 463 * to CPU byte order 464 * 465 * @x: value to be converted 466 */ 467 #define qdf_le16_to_cpu(x) __qdf_le16_to_cpu(x) 468 469 /** 470 * qdf_le32_to_cpu - Convert a 32-bit value from little-endian byte 471 * order to CPU byte order 472 * 473 * @x: value to be converted 474 */ 475 #define qdf_le32_to_cpu(x) __qdf_le32_to_cpu(x) 476 477 /** 478 * qdf_le64_to_cpu - Convert a 64-bit value from little-endian byte 479 * order to CPU byte order 480 * 481 * @x: value to be converted 482 */ 483 #define qdf_le64_to_cpu(x) __qdf_le64_to_cpu(x) 484 485 /** 486 * qdf_cpu_to_be16 - Convert a 16-bit value from CPU byte order to 487 * big-endian byte order 488 * 489 * @x: value to be converted 490 */ 491 #define qdf_cpu_to_be16(x) __qdf_cpu_to_be16(x) 492 493 /** 494 * qdf_cpu_to_be32 - Convert a 32-bit value from CPU byte order to 495 * big-endian byte order 496 * 497 * @x: value to be converted 498 */ 499 #define qdf_cpu_to_be32(x) __qdf_cpu_to_be32(x) 500 501 /** 502 * qdf_cpu_to_be64 - Convert a 64-bit value from CPU byte order to 503 * big-endian byte order 504 * 505 * @x: value to be converted 506 */ 507 #define qdf_cpu_to_be64(x) __qdf_cpu_to_be64(x) 508 509 510 /** 511 * qdf_be16_to_cpu - Convert a 16-bit value from big-endian byte order 512 * to CPU byte order 513 * 514 * @x: value to be converted 515 */ 516 #define qdf_be16_to_cpu(x) __qdf_be16_to_cpu(x) 517 518 /** 519 * qdf_be32_to_cpu - Convert a 32-bit value from big-endian byte order 520 * to CPU byte order 521 * 522 * @x: value to be converted 523 */ 524 #define qdf_be32_to_cpu(x) __qdf_be32_to_cpu(x) 525 526 /** 527 * qdf_be64_to_cpu - Convert a 64-bit value from big-endian byte order 528 * to CPU byte order 529 * 530 * @x: value to be converted 531 */ 532 #define qdf_be64_to_cpu(x) __qdf_be64_to_cpu(x) 533 534 /** 535 * qdf_function - replace with the name of the current function 536 */ 537 #define qdf_function __qdf_function 538 539 /** 540 * qdf_min - minimum of two numbers 541 */ 542 #define qdf_min(a, b) __qdf_min(a, b) 543 544 /** 545 * qdf_ffz() - find first (least significant) zero bit 546 * @mask: the bitmask to check 547 * 548 * Return: The zero-based index of the first zero bit, or -1 if none are found 549 */ 550 #define qdf_ffz(mask) __qdf_ffz(mask) 551 552 /** 553 * qdf_get_pwr2() - get next power of 2 integer from input value 554 * @value: input value to find next power of 2 integer 555 * 556 * Get next power of 2 integer from input value 557 * 558 * Return: Power of 2 integer 559 */ 560 static inline int qdf_get_pwr2(int value) 561 { 562 int log2; 563 564 if (QDF_IS_PWR2(value)) 565 return value; 566 567 log2 = 0; 568 while (value) { 569 value >>= 1; 570 log2++; 571 } 572 return 1 << log2; 573 } 574 575 static inline 576 int qdf_get_cpu(void) 577 { 578 return __qdf_get_cpu(); 579 } 580 581 /** 582 * qdf_get_hweight8() - count num of 1's in bitmap 583 * @value: input bitmap 584 * 585 * Count num of 1's set in the bitmap 586 * 587 * Return: num of 1's 588 */ 589 static inline 590 unsigned int qdf_get_hweight8(unsigned int w) 591 { 592 unsigned int res = w - ((w >> 1) & 0x55); 593 res = (res & 0x33) + ((res >> 2) & 0x33); 594 return (res + (res >> 4)) & 0x0F; 595 } 596 597 /** 598 * qdf_device_init_wakeup() - allow a device to wake up the aps system 599 * @qdf_dev: the qdf device context 600 * @enable: enable/disable the device as a wakup source 601 * 602 * Return: 0 or errno 603 */ 604 static inline int qdf_device_init_wakeup(qdf_device_t qdf_dev, bool enable) 605 { 606 return __qdf_device_init_wakeup(qdf_dev, enable); 607 } 608 609 static inline 610 uint64_t qdf_get_totalramsize(void) 611 { 612 return __qdf_get_totalramsize(); 613 } 614 615 /** 616 * qdf_get_lower_32_bits() - get lower 32 bits from an address. 617 * @addr: address 618 * 619 * This api returns the lower 32 bits of an address. 620 * 621 * Return: lower 32 bits. 622 */ 623 static inline 624 uint32_t qdf_get_lower_32_bits(qdf_dma_addr_t addr) 625 { 626 return __qdf_get_lower_32_bits(addr); 627 } 628 629 /** 630 * qdf_get_upper_32_bits() - get upper 32 bits from an address. 631 * @addr: address 632 * 633 * This api returns the upper 32 bits of an address. 634 * 635 * Return: upper 32 bits. 636 */ 637 static inline 638 uint32_t qdf_get_upper_32_bits(qdf_dma_addr_t addr) 639 { 640 return __qdf_get_upper_32_bits(addr); 641 } 642 643 /** 644 * qdf_rounddown_pow_of_two() - Round down to nearest power of two 645 * @n: number to be tested 646 * 647 * Test if the input number is power of two, and return the nearest power of two 648 * 649 * Return: number rounded down to the nearest power of two 650 */ 651 static inline 652 unsigned long qdf_rounddown_pow_of_two(unsigned long n) 653 { 654 return __qdf_rounddown_pow_of_two(n); 655 } 656 657 /** 658 * qdf_set_dma_coherent_mask() - set max number of bits allowed in dma addr 659 * @dev: device pointer 660 * @addr_bits: max number of bits allowed in dma address 661 * 662 * This API sets the maximum allowed number of bits in the dma address. 663 * 664 * Return: 0 - success, non zero - failure 665 */ 666 static inline 667 int qdf_set_dma_coherent_mask(struct device *dev, uint8_t addr_bits) 668 { 669 return __qdf_set_dma_coherent_mask(dev, addr_bits); 670 } 671 672 /** 673 * qdf_do_div() - wrapper function for kernel macro(do_div). 674 * @dividend: Dividend value 675 * @divisor : Divisor value 676 * 677 * Return: Quotient 678 */ 679 static inline 680 uint64_t qdf_do_div(uint64_t dividend, uint32_t divisor) 681 { 682 return __qdf_do_div(dividend, divisor); 683 } 684 685 /** 686 * qdf_do_div_rem() - wrapper function for kernel macro(do_div) 687 * to get remainder. 688 * @dividend: Dividend value 689 * @divisor : Divisor value 690 * 691 * Return: remainder 692 */ 693 static inline 694 uint64_t qdf_do_div_rem(uint64_t dividend, uint32_t divisor) 695 { 696 return __qdf_do_div_rem(dividend, divisor); 697 } 698 699 /** 700 * qdf_get_random_bytes() - returns nbytes bytes of random 701 * data 702 * 703 * Return: random bytes of data 704 */ 705 static inline 706 void qdf_get_random_bytes(void *buf, int nbytes) 707 { 708 return __qdf_get_random_bytes(buf, nbytes); 709 } 710 #endif /*_QDF_UTIL_H*/ 711