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