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