1 /* 2 * Copyright (c) 2014-2021 The Linux Foundation. All rights reserved. 3 * Copyright (c) 2021 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: i_qdf_util.h 22 * This file provides OS dependent API's. 23 */ 24 25 #ifndef _I_QDF_UTIL_H 26 #define _I_QDF_UTIL_H 27 28 #include <linux/compiler.h> 29 #include <linux/kernel.h> 30 #include <linux/types.h> 31 #include <linux/mm.h> 32 #include <linux/errno.h> 33 #include <linux/average.h> 34 35 #include <linux/random.h> 36 #include <linux/io.h> 37 38 #include <qdf_types.h> 39 #include <asm/byteorder.h> 40 41 #if LINUX_VERSION_CODE <= KERNEL_VERSION(3, 3, 8) 42 #include <asm/system.h> 43 #else 44 #if defined(__LINUX_MIPS32_ARCH__) || defined(__LINUX_MIPS64_ARCH__) 45 #include <asm/dec/system.h> 46 #else 47 #endif 48 #endif 49 50 #include <qdf_types.h> 51 #include <linux/io.h> 52 #include <asm/byteorder.h> 53 54 #ifdef QCA_PARTNER_PLATFORM 55 #include "ath_carr_pltfrm.h" 56 #else 57 #include <linux/byteorder/generic.h> 58 #endif 59 60 typedef wait_queue_head_t __qdf_wait_queue_head_t; 61 62 /* Generic compiler-dependent macros if defined by the OS */ 63 #define __qdf_wait_queue_interruptible(wait_queue, condition) \ 64 wait_event_interruptible(wait_queue, condition) 65 66 #define __qdf_wait_queue_timeout(wait_queue, condition, timeout) \ 67 wait_event_timeout(wait_queue, condition, timeout) 68 69 70 #define __qdf_init_waitqueue_head(_q) init_waitqueue_head(_q) 71 72 #define __qdf_wake_up_interruptible(_q) wake_up_interruptible(_q) 73 74 #define __qdf_wake_up(_q) wake_up(_q) 75 76 #define __qdf_wake_up_completion(_q) wake_up_completion(_q) 77 78 #define __qdf_unlikely(_expr) unlikely(_expr) 79 #define __qdf_likely(_expr) likely(_expr) 80 81 #define __qdf_bitmap(name, bits) DECLARE_BITMAP(name, bits) 82 83 /** 84 * __qdf_set_bit() - set bit in address 85 * @nr: bit number to be set 86 * @addr: address buffer pointer 87 * 88 * Return: none 89 */ 90 static inline void __qdf_set_bit(unsigned int nr, unsigned long *addr) 91 { 92 __set_bit(nr, addr); 93 } 94 95 static inline void __qdf_clear_bit(unsigned int nr, unsigned long *addr) 96 { 97 __clear_bit(nr, addr); 98 } 99 100 static inline bool __qdf_test_bit(unsigned int nr, unsigned long *addr) 101 { 102 return test_bit(nr, addr); 103 } 104 105 static inline bool __qdf_test_and_clear_bit(unsigned int nr, 106 unsigned long *addr) 107 { 108 return __test_and_clear_bit(nr, addr); 109 } 110 111 static inline unsigned long __qdf_find_first_bit(unsigned long *addr, 112 unsigned long nbits) 113 { 114 return find_first_bit(addr, nbits); 115 } 116 117 static inline bool __qdf_bitmap_empty(unsigned long *addr, 118 unsigned long nbits) 119 { 120 return bitmap_empty(addr, nbits); 121 } 122 123 static inline int __qdf_bitmap_and(unsigned long *dst, unsigned long *src1, 124 unsigned long *src2, unsigned long nbits) 125 { 126 return bitmap_and(dst, src1, src2, nbits); 127 } 128 129 /** 130 * __qdf_set_macaddr_broadcast() - set a QDF MacAddress to the 'broadcast' 131 * @mac_addr: pointer to the qdf MacAddress to set to broadcast 132 * 133 * This function sets a QDF MacAddress to the 'broadcast' MacAddress. Broadcast 134 * MacAddress contains all 0xFF bytes. 135 * 136 * Return: none 137 */ 138 static inline void __qdf_set_macaddr_broadcast(struct qdf_mac_addr *mac_addr) 139 { 140 memset(mac_addr, 0xff, QDF_MAC_ADDR_SIZE); 141 } 142 143 /** 144 * __qdf_zero_macaddr() - zero out a MacAddress 145 * @mac_addr: pointer to the struct qdf_mac_addr to zero. 146 * 147 * This function zeros out a QDF MacAddress type. 148 * 149 * Return: none 150 */ 151 static inline void __qdf_zero_macaddr(struct qdf_mac_addr *mac_addr) 152 { 153 memset(mac_addr, 0, QDF_MAC_ADDR_SIZE); 154 } 155 156 /** 157 * __qdf_is_macaddr_equal() - compare two QDF MacAddress 158 * @mac_addr1: Pointer to one qdf MacAddress to compare 159 * @mac_addr2: Pointer to the other qdf MacAddress to compare 160 * 161 * This function returns a bool that tells if a two QDF MacAddress' 162 * are equivalent. 163 * 164 * Return: true if the MacAddress's are equal 165 * not true if the MacAddress's are not equal 166 */ 167 static inline bool __qdf_is_macaddr_equal(struct qdf_mac_addr *mac_addr1, 168 struct qdf_mac_addr *mac_addr2) 169 { 170 return 0 == memcmp(mac_addr1, mac_addr2, QDF_MAC_ADDR_SIZE); 171 } 172 173 #define __qdf_in_interrupt in_interrupt 174 175 #define __qdf_min(_a, _b) min(_a, _b) 176 #define __qdf_max(_a, _b) max(_a, _b) 177 178 /** 179 * Setting it to blank as feature is not intended to be supported 180 * on linux version less than 4.3 181 */ 182 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 3, 0) 183 #define __QDF_DECLARE_EWMA(name, _factor, _weight) 184 185 #define __qdf_ewma_tx_lag int 186 #define __qdf_ewma_rx_rssi int 187 #else 188 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0) 189 #define __QDF_DECLARE_EWMA(name, _factor, _weight) \ 190 DECLARE_EWMA(name, ilog2(_factor), _weight) 191 #else 192 #define __QDF_DECLARE_EWMA(name, _factor, _weight) \ 193 DECLARE_EWMA(name, _factor, _weight) 194 #endif 195 196 #define __qdf_ewma_tx_lag struct ewma_tx_lag 197 #define __qdf_ewma_rx_rssi struct ewma_rx_rssi 198 #endif 199 200 #define __qdf_ffz(mask) (~(mask) == 0 ? -1 : ffz(mask)) 201 202 #define MEMINFO_KB(x) ((x) << (PAGE_SHIFT - 10)) /* In kilobytes */ 203 204 /** 205 * @brief Assert 206 */ 207 #define __qdf_assert(expr) do { \ 208 if (unlikely(!(expr))) { \ 209 pr_err("Assertion failed! %s:%s %s:%d\n", \ 210 # expr, __func__, __FILE__, __LINE__); \ 211 dump_stack(); \ 212 QDF_BUG_ON_ASSERT(0); \ 213 } \ 214 } while (0) 215 216 /** 217 * @brief Assert 218 */ 219 #define __qdf_target_assert(expr) do { \ 220 if (unlikely(!(expr))) { \ 221 qdf_err("Assertion failed! %s:%s %s:%d", \ 222 #expr, __FUNCTION__, __FILE__, __LINE__); \ 223 dump_stack(); \ 224 QDF_DEBUG_PANIC("Take care of the TARGET ASSERT first\n"); \ 225 } \ 226 } while (0) 227 228 /** 229 * @brief Compile time Assert 230 */ 231 #define QDF_COMPILE_TIME_ASSERT(assertion_name, predicate) \ 232 typedef char assertion_name[(predicate) ? 1 : -1] 233 234 #define __qdf_container_of(ptr, type, member) container_of(ptr, type, member) 235 236 #define __qdf_ntohs ntohs 237 #define __qdf_ntohl ntohl 238 239 #define __qdf_htons htons 240 #define __qdf_htonl htonl 241 242 #define __qdf_cpu_to_le16 cpu_to_le16 243 #define __qdf_cpu_to_le32 cpu_to_le32 244 #define __qdf_cpu_to_le64 cpu_to_le64 245 246 #define __qdf_le16_to_cpu le16_to_cpu 247 #define __qdf_le32_to_cpu le32_to_cpu 248 #define __qdf_le64_to_cpu le64_to_cpu 249 250 #define __qdf_cpu_to_be16 cpu_to_be16 251 #define __qdf_cpu_to_be32 cpu_to_be32 252 #define __qdf_cpu_to_be64 cpu_to_be64 253 254 #define __qdf_be16_to_cpu be16_to_cpu 255 #define __qdf_be32_to_cpu be32_to_cpu 256 #define __qdf_be64_to_cpu be64_to_cpu 257 258 /** 259 * @brief memory barriers. 260 */ 261 #define __qdf_wmb() wmb() 262 #define __qdf_rmb() rmb() 263 #define __qdf_mb() mb() 264 #define __qdf_ioread32(offset) ioread32(offset) 265 #define __qdf_iowrite32(offset, value) iowrite32(value, offset) 266 267 #define __qdf_roundup(x, y) roundup(x, y) 268 #define __qdf_ceil(x, y) DIV_ROUND_UP(x, y) 269 270 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 3, 0) 271 #define __qdf_ewma_tx_lag_init(tx_lag) 272 #define __qdf_ewma_tx_lag_add(tx_lag, value) 273 #define __qdf_ewma_tx_lag_read(tx_lag) 274 275 #define __qdf_ewma_rx_rssi_init(rx_rssi) 276 #define __qdf_ewma_rx_rssi_add(rx_rssi, value) 277 #define __qdf_ewma_rx_rssi_read(rx_rssi) 278 #else 279 #define __qdf_ewma_tx_lag_init(tx_lag) \ 280 ewma_tx_lag_init(tx_lag) 281 282 #define __qdf_ewma_tx_lag_add(tx_lag, value) \ 283 ewma_tx_lag_add(tx_lag, value) 284 285 #define __qdf_ewma_tx_lag_read(tx_lag) \ 286 ewma_tx_lag_read(tx_lag) 287 288 #define __qdf_ewma_rx_rssi_init(rx_rssi) \ 289 ewma_rx_rssi_init(rx_rssi) 290 291 #define __qdf_ewma_rx_rssi_add(rx_rssi, value) \ 292 ewma_rx_rssi_add(rx_rssi, value) 293 294 #define __qdf_ewma_rx_rssi_read(rx_rssi) \ 295 ewma_rx_rssi_read(rx_rssi) 296 #endif 297 298 #define __qdf_prefetch(x) prefetch(x) 299 300 #ifdef QCA_CONFIG_SMP 301 /** 302 * __qdf_get_cpu() - get cpu_index 303 * 304 * Return: cpu_index 305 */ 306 static inline 307 int __qdf_get_cpu(void) 308 { 309 int cpu_index = get_cpu(); 310 311 put_cpu(); 312 return cpu_index; 313 } 314 #else 315 static inline 316 int __qdf_get_cpu(void) 317 { 318 return 0; 319 } 320 #endif 321 322 static inline int __qdf_device_init_wakeup(__qdf_device_t qdf_dev, bool enable) 323 { 324 return device_init_wakeup(qdf_dev->dev, enable); 325 } 326 327 /** 328 * __qdf_get_totalramsize() - Get total ram size in Kb 329 * 330 * Return: Total ram size in Kb 331 */ 332 static inline uint64_t 333 __qdf_get_totalramsize(void) 334 { 335 struct sysinfo meminfo; 336 337 si_meminfo(&meminfo); 338 return MEMINFO_KB(meminfo.totalram); 339 } 340 341 /** 342 * __qdf_get_lower_32_bits() - get lower 32 bits from an address. 343 * @addr: address 344 * 345 * This api returns the lower 32 bits of an address. 346 * 347 * Return: lower 32 bits. 348 */ 349 static inline 350 uint32_t __qdf_get_lower_32_bits(__qdf_dma_addr_t addr) 351 { 352 return lower_32_bits(addr); 353 } 354 355 /** 356 * __qdf_get_upper_32_bits() - get upper 32 bits from an address. 357 * @addr: address 358 * 359 * This api returns the upper 32 bits of an address. 360 * 361 * Return: upper 32 bits. 362 */ 363 static inline 364 uint32_t __qdf_get_upper_32_bits(__qdf_dma_addr_t addr) 365 { 366 return upper_32_bits(addr); 367 } 368 369 /** 370 * __qdf_rounddown_pow_of_two() - Round down to nearest power of two 371 * @n: number to be tested 372 * 373 * Test if the input number is power of two, and return the nearest power of two 374 * 375 * Return: number rounded down to the nearest power of two 376 */ 377 static inline 378 unsigned long __qdf_rounddown_pow_of_two(unsigned long n) 379 { 380 if (is_power_of_2(n)) 381 return n; /* already a power of 2 */ 382 383 return __rounddown_pow_of_two(n); 384 } 385 386 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0) 387 388 /** 389 * __qdf_set_dma_coherent_mask() - set max number of bits allowed in dma addr 390 * @dev: device pointer 391 * @addr_bits: max number of bits allowed in dma address 392 * 393 * This API sets the maximum allowed number of bits in the dma address. 394 * 395 * Return: 0 - success, non zero - failure 396 */ 397 static inline 398 int __qdf_set_dma_coherent_mask(struct device *dev, uint8_t addr_bits) 399 { 400 return dma_set_mask_and_coherent(dev, DMA_BIT_MASK(addr_bits)); 401 } 402 403 #else 404 405 /** 406 * __qdf_set_dma_coherent_mask() - set max number of bits allowed in dma addr 407 * @dev: device pointer 408 * @addr_bits: max number of bits allowed in dma address 409 * 410 * This API sets the maximum allowed number of bits in the dma address. 411 * 412 * Return: 0 - success, non zero - failure 413 */ 414 static inline 415 int __qdf_set_dma_coherent_mask(struct device *dev, uint8_t addr_bits) 416 { 417 return dma_set_coherent_mask(dev, DMA_BIT_MASK(addr_bits)); 418 } 419 #endif 420 /** 421 * qdf_get_random_bytes() - returns nbytes bytes of random 422 * data 423 * 424 * Return: random bytes of data 425 */ 426 static inline 427 void __qdf_get_random_bytes(void *buf, int nbytes) 428 { 429 return get_random_bytes(buf, nbytes); 430 } 431 432 /** 433 * __qdf_do_div() - wrapper function for kernel macro(do_div). 434 * @dividend: Dividend value 435 * @divisor : Divisor value 436 * 437 * Return: Quotient 438 */ 439 static inline 440 uint64_t __qdf_do_div(uint64_t dividend, uint32_t divisor) 441 { 442 do_div(dividend, divisor); 443 /*do_div macro updates dividend with Quotient of dividend/divisor */ 444 return dividend; 445 } 446 447 /** 448 * __qdf_do_div_rem() - wrapper function for kernel macro(do_div) 449 * to get remainder. 450 * @dividend: Dividend value 451 * @divisor : Divisor value 452 * 453 * Return: remainder 454 */ 455 static inline 456 uint64_t __qdf_do_div_rem(uint64_t dividend, uint32_t divisor) 457 { 458 return do_div(dividend, divisor); 459 } 460 461 /** 462 * __qdf_hex_to_bin() - Wrapper function to kernel API to get unsigned 463 * integer from hexa decimal ASCII character. 464 * @ch: hexa decimal ASCII character 465 * 466 * Return: For hexa decimal ASCII char return actual decimal value 467 * else -1 for bad input. 468 */ 469 static inline 470 int __qdf_hex_to_bin(char ch) 471 { 472 return hex_to_bin(ch); 473 } 474 475 /** 476 * __qdf_hex_str_to_binary() - Wrapper function to get array of unsigned 477 * integers from string of hexa decimal ASCII characters. 478 * @dst: output array to hold converted values 479 * @src: input string of hexa decimal ASCII characters 480 * @count: size of dst string 481 * 482 * Return: For a string of hexa decimal ASCII characters return 0 483 * else -1 for bad input. 484 */ 485 static inline 486 int __qdf_hex_str_to_binary(u8 *dst, const char *src, size_t count) 487 { 488 return hex2bin(dst, src, count); 489 } 490 491 /** 492 * __qdf_fls() - find last set bit in a given 32 bit input 493 * @x: 32 bit mask 494 * 495 * Return: zero if the input is zero, otherwise returns the bit 496 * position of the last set bit, where the LSB is 1 and MSB is 32. 497 */ 498 static inline 499 int __qdf_fls(uint32_t x) 500 { 501 return fls(x); 502 } 503 504 /** 505 * __qdf_get_smp_processor_id() - Get the current CPU id 506 * 507 * Return: current CPU id 508 */ 509 static inline int __qdf_get_smp_processor_id(void) 510 { 511 return smp_processor_id(); 512 } 513 #endif /*_I_QDF_UTIL_H*/ 514