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