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