qdf_util.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. * Copyright (c) 2014-2016 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. * This file was originally distributed by Qualcomm Atheros, Inc.
  23. * under proprietary terms before Copyright ownership was assigned
  24. * to the Linux Foundation.
  25. */
  26. /**
  27. * DOC: qdf_util.h
  28. * This file defines utility functions.
  29. */
  30. #ifndef _QDF_UTIL_H
  31. #define _QDF_UTIL_H
  32. #include <i_qdf_util.h>
  33. #ifdef QCA_CONFIG_SMP
  34. #define QDF_MAX_AVAILABLE_CPU 8
  35. #else
  36. #define QDF_MAX_AVAILABLE_CPU 1
  37. #endif
  38. typedef __qdf_thread_t qdf_thread_t;
  39. typedef __qdf_wait_queue_head_t qdf_wait_queue_head_t;
  40. /**
  41. * qdf_unlikely - Compiler-dependent macro denoting code likely to execute
  42. * @_expr: expression to be checked
  43. */
  44. #define qdf_unlikely(_expr) __qdf_unlikely(_expr)
  45. /**
  46. * qdf_likely - Compiler-dependent macro denoting code unlikely to execute
  47. * @_expr: expression to be checked
  48. */
  49. #define qdf_likely(_expr) __qdf_likely(_expr)
  50. /**
  51. * qdf_mb - read + write memory barrier.
  52. */
  53. #define qdf_mb() __qdf_mb()
  54. /**
  55. * qdf_assert - assert "expr" evaluates to false.
  56. */
  57. #ifdef QDF_DEBUG
  58. #define qdf_assert(expr) __qdf_assert(expr)
  59. #else
  60. #define qdf_assert(expr)
  61. #endif /* QDF_DEBUG */
  62. /**
  63. * qdf_assert_always - alway assert "expr" evaluates to false.
  64. */
  65. #define qdf_assert_always(expr) __qdf_assert(expr)
  66. /**
  67. * qdf_target_assert_always - alway target assert "expr" evaluates to false.
  68. */
  69. #define qdf_target_assert_always(expr) __qdf_target_assert(expr)
  70. /**
  71. * QDF_MAX - get maximum of two values
  72. * @_x: 1st arguement
  73. * @_y: 2nd arguement
  74. */
  75. #define QDF_MAX(_x, _y) (((_x) > (_y)) ? (_x) : (_y))
  76. /**
  77. * QDF_MIN - get minimum of two values
  78. * @_x: 1st arguement
  79. * @_y: 2nd arguement
  80. */
  81. #define QDF_MIN(_x, _y) (((_x) < (_y)) ? (_x) : (_y))
  82. /**
  83. * qdf_status_to_os_return - returns the status to OS.
  84. * @status: enum QDF_STATUS
  85. *
  86. * returns: int status success/failure
  87. */
  88. static inline int qdf_status_to_os_return(QDF_STATUS status)
  89. {
  90. return __qdf_status_to_os_return(status);
  91. }
  92. /**
  93. * qdf_set_bit() - set bit in address
  94. * @nr: bit number to be set
  95. * @addr: address buffer pointer
  96. *
  97. * Return: none
  98. */
  99. #define qdf_set_bit(nr, addr) __qdf_set_bit(nr, addr)
  100. /**
  101. * qdf_clear_bit() - clear bit in address
  102. * @nr: bit number to be clear
  103. * @addr: address buffer pointer
  104. *
  105. * Return: none
  106. */
  107. #define qdf_clear_bit(nr, addr) __qdf_clear_bit(nr, addr)
  108. /**
  109. * qdf_test_bit() - test bit position in address
  110. * @nr: bit number to be tested
  111. * @addr: address buffer pointer
  112. *
  113. * Return: none
  114. */
  115. #define qdf_test_bit(nr, addr) __qdf_test_bit(nr, addr)
  116. /**
  117. * qdf_test_and_clear_bit() - test and clear bit position in address
  118. * @nr: bit number to be tested
  119. * @addr: address buffer pointer
  120. *
  121. * Return: none
  122. */
  123. #define qdf_test_and_clear_bit(nr, addr) __qdf_test_and_clear_bit(nr, addr)
  124. #define qdf_wait_queue_interruptible(wait_queue, condition) \
  125. __qdf_wait_queue_interruptible(wait_queue, condition)
  126. #define qdf_init_waitqueue_head(_q) __qdf_init_waitqueue_head(_q)
  127. #define qdf_wake_up_interruptible(_q) __qdf_wake_up_interruptible(_q)
  128. #define qdf_wake_up_completion(_q) __qdf_wake_up_completion(_q)
  129. /**
  130. * qdf_container_of - cast a member of a structure out to the containing
  131. * structure
  132. * @ptr: the pointer to the member.
  133. * @type: the type of the container struct this is embedded in.
  134. * @member: the name of the member within the struct.
  135. */
  136. #define qdf_container_of(ptr, type, member) \
  137. __qdf_container_of(ptr, type, member)
  138. /**
  139. * qdf_is_pwr2 - test input value is power of 2 integer
  140. * @value: input integer
  141. */
  142. #define QDF_IS_PWR2(value) (((value) ^ ((value)-1)) == ((value) << 1) - 1)
  143. /**
  144. * qdf_roundup() - roundup the input value
  145. * @x: value to roundup
  146. * @y: input value rounded to multiple of this
  147. *
  148. * Return: rounded value
  149. */
  150. #define qdf_roundup(x, y) __qdf_roundup(x, y)
  151. /**
  152. * qdf_is_macaddr_equal() - compare two QDF MacAddress
  153. * @mac_addr1: Pointer to one qdf MacAddress to compare
  154. * @mac_addr2: Pointer to the other qdf MacAddress to compare
  155. *
  156. * This function returns a bool that tells if a two QDF MacAddress'
  157. * are equivalent.
  158. *
  159. * Return: true if the MacAddress's are equal
  160. * not true if the MacAddress's are not equal
  161. */
  162. static inline bool qdf_is_macaddr_equal(struct qdf_mac_addr *mac_addr1,
  163. struct qdf_mac_addr *mac_addr2)
  164. {
  165. return __qdf_is_macaddr_equal(mac_addr1, mac_addr2);
  166. }
  167. /**
  168. * qdf_is_macaddr_zero() - check for a MacAddress of all zeros.
  169. * @mac_addr: pointer to the struct qdf_mac_addr to check.
  170. *
  171. * This function returns a bool that tells if a MacAddress is made up of
  172. * all zeros.
  173. *
  174. * Return: true if the MacAddress is all Zeros
  175. * false if the MacAddress is not all Zeros.
  176. */
  177. static inline bool qdf_is_macaddr_zero(struct qdf_mac_addr *mac_addr)
  178. {
  179. struct qdf_mac_addr zero_mac_addr = QDF_MAC_ADDR_ZERO_INITIALIZER;
  180. return qdf_is_macaddr_equal(mac_addr, &zero_mac_addr);
  181. }
  182. /**
  183. * qdf_zero_macaddr() - zero out a MacAddress
  184. * @mac_addr: pointer to the struct qdf_mac_addr to zero.
  185. *
  186. * This function zeros out a QDF MacAddress type.
  187. *
  188. * Return: none
  189. */
  190. static inline void qdf_zero_macaddr(struct qdf_mac_addr *mac_addr)
  191. {
  192. __qdf_zero_macaddr(mac_addr);
  193. }
  194. /**
  195. * qdf_is_macaddr_group() - check for a MacAddress is a 'group' address
  196. * @mac_addr1: pointer to the qdf MacAddress to check
  197. *
  198. * This function returns a bool that tells if a the input QDF MacAddress
  199. * is a "group" address. Group addresses have the 'group address bit' turned
  200. * on in the MacAddress. Group addresses are made up of Broadcast and
  201. * Multicast addresses.
  202. *
  203. * Return: true if the input MacAddress is a Group address
  204. * false if the input MacAddress is not a Group address
  205. */
  206. static inline bool qdf_is_macaddr_group(struct qdf_mac_addr *mac_addr)
  207. {
  208. return mac_addr->bytes[0] & 0x01;
  209. }
  210. /**
  211. * qdf_is_macaddr_broadcast() - check for a MacAddress is a broadcast address
  212. * @mac_addr: Pointer to the qdf MacAddress to check
  213. *
  214. * This function returns a bool that tells if a the input QDF MacAddress
  215. * is a "broadcast" address.
  216. *
  217. * Return: true if the input MacAddress is a broadcast address
  218. * flase if the input MacAddress is not a broadcast address
  219. */
  220. static inline bool qdf_is_macaddr_broadcast(struct qdf_mac_addr *mac_addr)
  221. {
  222. struct qdf_mac_addr broadcast_mac_addr =
  223. QDF_MAC_ADDR_BROADCAST_INITIALIZER;
  224. return qdf_is_macaddr_equal(mac_addr, &broadcast_mac_addr);
  225. }
  226. /**
  227. * qdf_copy_macaddr() - copy a QDF MacAddress
  228. * @dst_addr: pointer to the qdf MacAddress to copy TO (the destination)
  229. * @src_addr: pointer to the qdf MacAddress to copy FROM (the source)
  230. *
  231. * This function copies a QDF MacAddress into another QDF MacAddress.
  232. *
  233. * Return: none
  234. */
  235. static inline void qdf_copy_macaddr(struct qdf_mac_addr *dst_addr,
  236. struct qdf_mac_addr *src_addr)
  237. {
  238. *dst_addr = *src_addr;
  239. }
  240. /**
  241. * qdf_set_macaddr_broadcast() - set a QDF MacAddress to the 'broadcast'
  242. * @mac_addr: pointer to the qdf MacAddress to set to broadcast
  243. *
  244. * This function sets a QDF MacAddress to the 'broadcast' MacAddress. Broadcast
  245. * MacAddress contains all 0xFF bytes.
  246. *
  247. * Return: none
  248. */
  249. static inline void qdf_set_macaddr_broadcast(struct qdf_mac_addr *mac_addr)
  250. {
  251. __qdf_set_macaddr_broadcast(mac_addr);
  252. }
  253. /**
  254. * qdf_set_u16() - Assign 16-bit unsigned value to a byte array base on CPU's
  255. * endianness.
  256. * @ptr: Starting address of a byte array
  257. * @value: The value to assign to the byte array
  258. *
  259. * Caller must validate the byte array has enough space to hold the vlaue
  260. *
  261. * Return: The address to the byte after the assignment. This may or may not
  262. * be valid. Caller to verify.
  263. */
  264. static inline uint8_t *qdf_set_u16(uint8_t *ptr, uint16_t value)
  265. {
  266. #if defined(ANI_BIG_BYTE_ENDIAN)
  267. *(ptr) = (uint8_t) (value >> 8);
  268. *(ptr + 1) = (uint8_t) (value);
  269. #else
  270. *(ptr + 1) = (uint8_t) (value >> 8);
  271. *(ptr) = (uint8_t) (value);
  272. #endif
  273. return ptr + 2;
  274. }
  275. /**
  276. * qdf_get_u16() - Retrieve a 16-bit unsigned value from a byte array base on
  277. * CPU's endianness.
  278. * @ptr: Starting address of a byte array
  279. * @value: Pointer to a caller allocated buffer for 16 bit value. Value is to
  280. * assign to this location.
  281. *
  282. * Caller must validate the byte array has enough space to hold the vlaue
  283. *
  284. * Return: The address to the byte after the assignment. This may or may not
  285. * be valid. Caller to verify.
  286. */
  287. static inline uint8_t *qdf_get_u16(uint8_t *ptr, uint16_t *value)
  288. {
  289. #if defined(ANI_BIG_BYTE_ENDIAN)
  290. *value = (((uint16_t) (*ptr << 8)) | ((uint16_t) (*(ptr + 1))));
  291. #else
  292. *value = (((uint16_t) (*(ptr + 1) << 8)) | ((uint16_t) (*ptr)));
  293. #endif
  294. return ptr + 2;
  295. }
  296. /**
  297. * qdf_get_u32() - retrieve a 32-bit unsigned value from a byte array base on
  298. * CPU's endianness.
  299. * @ptr: Starting address of a byte array
  300. * @value: Pointer to a caller allocated buffer for 32 bit value. Value is to
  301. * assign to this location.
  302. *
  303. * Caller must validate the byte array has enough space to hold the vlaue
  304. *
  305. * Return: The address to the byte after the assignment. This may or may not
  306. * be valid. Caller to verify.
  307. */
  308. static inline uint8_t *qdf_get_u32(uint8_t *ptr, uint32_t *value)
  309. {
  310. #if defined(ANI_BIG_BYTE_ENDIAN)
  311. *value = ((uint32_t) (*(ptr) << 24) |
  312. (uint32_t) (*(ptr + 1) << 16) |
  313. (uint32_t) (*(ptr + 2) << 8) | (uint32_t) (*(ptr + 3)));
  314. #else
  315. *value = ((uint32_t) (*(ptr + 3) << 24) |
  316. (uint32_t) (*(ptr + 2) << 16) |
  317. (uint32_t) (*(ptr + 1) << 8) | (uint32_t) (*(ptr)));
  318. #endif
  319. return ptr + 4;
  320. }
  321. /**
  322. * qdf_ntohs - Convert a 16-bit value from network byte order to host byte order
  323. */
  324. #define qdf_ntohs(x) __qdf_ntohs(x)
  325. /**
  326. * qdf_ntohl - Convert a 32-bit value from network byte order to host byte order
  327. */
  328. #define qdf_ntohl(x) __qdf_ntohl(x)
  329. /**
  330. * qdf_htons - Convert a 16-bit value from host byte order to network byte order
  331. */
  332. #define qdf_htons(x) __qdf_htons(x)
  333. /**
  334. * qdf_htonl - Convert a 32-bit value from host byte order to network byte order
  335. */
  336. #define qdf_htonl(x) __qdf_htonl(x)
  337. /**
  338. * qdf_cpu_to_le16 - Convert a 16-bit value from CPU byte order to
  339. * little-endian byte order
  340. */
  341. #define qdf_cpu_to_le16(x) __qdf_cpu_to_le16(x)
  342. /**
  343. * qdf_cpu_to_le32 - Convert a 32-bit value from CPU byte order to
  344. * little-endian byte order
  345. */
  346. #define qdf_cpu_to_le32(x) __qdf_cpu_to_le32(x)
  347. /**
  348. * qdf_cpu_to_le64 - Convert a 64-bit value from CPU byte order to
  349. * little-endian byte order
  350. */
  351. #define qdf_cpu_to_le64(x) __qdf_cpu_to_le64(x)
  352. /**
  353. * qdf_be32_to_cpu - Convert a 32-bit value from big-endian byte order
  354. * to CPU byte order
  355. */
  356. #define qdf_be32_to_cpu(x) __qdf_be32_to_cpu(x)
  357. /**
  358. * qdf_be64_to_cpu - Convert a 64-bit value from big-endian byte order
  359. * to CPU byte order
  360. */
  361. #define qdf_be64_to_cpu(x) __qdf_be64_to_cpu(x)
  362. /**
  363. * qdf_le32_to_cpu - Convert a 32-bit value from little-endian byte
  364. * order to CPU byte order
  365. */
  366. #define qdf_le32_to_cpu(x) __qdf_le32_to_cpu(x)
  367. /**
  368. * qdf_le64_to_cpu - Convert a 64-bit value from little-endian byte
  369. * order to CPU byte order
  370. */
  371. #define qdf_le64_to_cpu(x) __qdf_le64_to_cpu(x)
  372. /**
  373. * qdf_le16_to_cpu - Convert a 16-bit value from little-endian byte order
  374. * to CPU byte order
  375. * @x: value to be converted
  376. */
  377. #define qdf_le16_to_cpu(x) __qdf_le16_to_cpu(x)
  378. /**
  379. * qdf_function - replace with the name of the current function
  380. */
  381. #define qdf_function __qdf_function
  382. /**
  383. * qdf_get_pwr2() - get next power of 2 integer from input value
  384. * @value: input value to find next power of 2 integer
  385. *
  386. * Get next power of 2 integer from input value
  387. *
  388. * Return: Power of 2 integer
  389. */
  390. static inline int qdf_get_pwr2(int value)
  391. {
  392. int log2;
  393. if (QDF_IS_PWR2(value))
  394. return value;
  395. log2 = 0;
  396. while (value) {
  397. value >>= 1;
  398. log2++;
  399. }
  400. return 1 << log2;
  401. }
  402. static inline
  403. int qdf_get_cpu(void)
  404. {
  405. return __qdf_get_cpu();
  406. }
  407. /**
  408. * qdf_device_init_wakeup() - allow a device to wake up the aps system
  409. * @qdf_dev: the qdf device context
  410. * @enable: enable/disable the device as a wakup source
  411. *
  412. * Return: 0 or errno
  413. */
  414. static inline int qdf_device_init_wakeup(qdf_device_t qdf_dev, bool enable)
  415. {
  416. return __qdf_device_init_wakeup(qdf_dev, enable);
  417. }
  418. static inline
  419. uint64_t qdf_get_totalramsize(void)
  420. {
  421. return __qdf_get_totalramsize();
  422. }
  423. /**
  424. * qdf_get_lower_32_bits() - get lower 32 bits from an address.
  425. * @addr: address
  426. *
  427. * This api returns the lower 32 bits of an address.
  428. *
  429. * Return: lower 32 bits.
  430. */
  431. static inline
  432. uint32_t qdf_get_lower_32_bits(qdf_dma_addr_t addr)
  433. {
  434. return __qdf_get_lower_32_bits(addr);
  435. }
  436. /**
  437. * qdf_get_upper_32_bits() - get upper 32 bits from an address.
  438. * @addr: address
  439. *
  440. * This api returns the upper 32 bits of an address.
  441. *
  442. * Return: upper 32 bits.
  443. */
  444. static inline
  445. uint32_t qdf_get_upper_32_bits(qdf_dma_addr_t addr)
  446. {
  447. return __qdf_get_upper_32_bits(addr);
  448. }
  449. #endif /*_QDF_UTIL_H*/