qdf_util.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. /**
  34. * qdf_unlikely - Compiler-dependent macro denoting code likely to execute
  35. * @_expr: expression to be checked
  36. */
  37. #define qdf_unlikely(_expr) __qdf_unlikely(_expr)
  38. /**
  39. * qdf_likely - Compiler-dependent macro denoting code unlikely to execute
  40. * @_expr: expression to be checked
  41. */
  42. #define qdf_likely(_expr) __qdf_likely(_expr)
  43. /**
  44. * qdf_mb - read + write memory barrier.
  45. */
  46. #define qdf_mb() __qdf_mb()
  47. /**
  48. * qdf_assert - assert "expr" evaluates to false.
  49. */
  50. #ifdef QDF_DEBUG
  51. #define qdf_assert(expr) __qdf_assert(expr)
  52. #else
  53. #define qdf_assert(expr)
  54. #endif /* QDF_DEBUG */
  55. /**
  56. * qdf_assert_always - alway assert "expr" evaluates to false.
  57. */
  58. #define qdf_assert_always(expr) __qdf_assert(expr)
  59. /**
  60. * qdf_target_assert_always - alway target assert "expr" evaluates to false.
  61. */
  62. #define qdf_target_assert_always(expr) __qdf_target_assert(expr)
  63. /**
  64. * QDF_MAX - get maximum of two values
  65. * @_x: 1st arguement
  66. * @_y: 2nd arguement
  67. */
  68. #define QDF_MAX(_x, _y) (((_x) > (_y)) ? (_x) : (_y))
  69. /**
  70. * QDF_MIN - get minimum of two values
  71. * @_x: 1st arguement
  72. * @_y: 2nd arguement
  73. */
  74. #define QDF_MIN(_x, _y) (((_x) < (_y)) ? (_x) : (_y))
  75. /**
  76. * qdf_status_to_os_return - returns the status to OS.
  77. * @status: enum QDF_STATUS
  78. *
  79. * returns: int status success/failure
  80. */
  81. static inline int qdf_status_to_os_return(QDF_STATUS status)
  82. {
  83. return __qdf_status_to_os_return(status);
  84. }
  85. /**
  86. * qdf_set_bit() - set bit in address
  87. * @nr: bit number to be set
  88. * @addr: address buffer pointer
  89. *
  90. * Return: none
  91. */
  92. #define qdf_set_bit(nr, addr) __qdf_set_bit(nr, addr)
  93. /**
  94. * qdf_container_of - cast a member of a structure out to the containing
  95. * structure
  96. * @ptr: the pointer to the member.
  97. * @type: the type of the container struct this is embedded in.
  98. * @member: the name of the member within the struct.
  99. */
  100. #define qdf_container_of(ptr, type, member) \
  101. __qdf_container_of(ptr, type, member)
  102. /**
  103. * qdf_is_pwr2 - test input value is power of 2 integer
  104. * @value: input integer
  105. */
  106. #define QDF_IS_PWR2(value) (((value) ^ ((value)-1)) == ((value) << 1) - 1)
  107. /**
  108. * qdf_roundup() - roundup the input value
  109. * @x: value to roundup
  110. * @y: input value rounded to multiple of this
  111. *
  112. * Return: rounded value
  113. */
  114. #define qdf_roundup(x, y) __qdf_roundup(x, y)
  115. /**
  116. * qdf_is_macaddr_equal() - compare two QDF MacAddress
  117. * @mac_addr1: Pointer to one qdf MacAddress to compare
  118. * @mac_addr2: Pointer to the other qdf MacAddress to compare
  119. *
  120. * This function returns a bool that tells if a two QDF MacAddress'
  121. * are equivalent.
  122. *
  123. * Return: true if the MacAddress's are equal
  124. * not true if the MacAddress's are not equal
  125. */
  126. static inline bool qdf_is_macaddr_equal(struct qdf_mac_addr *mac_addr1,
  127. struct qdf_mac_addr *mac_addr2)
  128. {
  129. return __qdf_is_macaddr_equal(mac_addr1, mac_addr2);
  130. }
  131. /**
  132. * qdf_is_macaddr_zero() - check for a MacAddress of all zeros.
  133. * @mac_addr: pointer to the struct qdf_mac_addr to check.
  134. *
  135. * This function returns a bool that tells if a MacAddress is made up of
  136. * all zeros.
  137. *
  138. * Return: true if the MacAddress is all Zeros
  139. * false if the MacAddress is not all Zeros.
  140. */
  141. static inline bool qdf_is_macaddr_zero(struct qdf_mac_addr *mac_addr)
  142. {
  143. struct qdf_mac_addr zero_mac_addr = QDF_MAC_ADDR_ZERO_INITIALIZER;
  144. return qdf_is_macaddr_equal(mac_addr, &zero_mac_addr);
  145. }
  146. /**
  147. * qdf_zero_macaddr() - zero out a MacAddress
  148. * @mac_addr: pointer to the struct qdf_mac_addr to zero.
  149. *
  150. * This function zeros out a QDF MacAddress type.
  151. *
  152. * Return: none
  153. */
  154. static inline void qdf_zero_macaddr(struct qdf_mac_addr *mac_addr)
  155. {
  156. __qdf_zero_macaddr(mac_addr);
  157. }
  158. /**
  159. * qdf_is_macaddr_group() - check for a MacAddress is a 'group' address
  160. * @mac_addr1: pointer to the qdf MacAddress to check
  161. *
  162. * This function returns a bool that tells if a the input QDF MacAddress
  163. * is a "group" address. Group addresses have the 'group address bit' turned
  164. * on in the MacAddress. Group addresses are made up of Broadcast and
  165. * Multicast addresses.
  166. *
  167. * Return: true if the input MacAddress is a Group address
  168. * false if the input MacAddress is not a Group address
  169. */
  170. static inline bool qdf_is_macaddr_group(struct qdf_mac_addr *mac_addr)
  171. {
  172. return mac_addr->bytes[0] & 0x01;
  173. }
  174. /**
  175. * qdf_is_macaddr_broadcast() - check for a MacAddress is a broadcast address
  176. * @mac_addr: Pointer to the qdf MacAddress to check
  177. *
  178. * This function returns a bool that tells if a the input QDF MacAddress
  179. * is a "broadcast" address.
  180. *
  181. * Return: true if the input MacAddress is a broadcast address
  182. * flase if the input MacAddress is not a broadcast address
  183. */
  184. static inline bool qdf_is_macaddr_broadcast(struct qdf_mac_addr *mac_addr)
  185. {
  186. struct qdf_mac_addr broadcast_mac_addr =
  187. QDF_MAC_ADDR_BROADCAST_INITIALIZER;
  188. return qdf_is_macaddr_equal(mac_addr, &broadcast_mac_addr);
  189. }
  190. /**
  191. * qdf_copy_macaddr() - copy a QDF MacAddress
  192. * @dst_addr: pointer to the qdf MacAddress to copy TO (the destination)
  193. * @src_addr: pointer to the qdf MacAddress to copy FROM (the source)
  194. *
  195. * This function copies a QDF MacAddress into another QDF MacAddress.
  196. *
  197. * Return: none
  198. */
  199. static inline void qdf_copy_macaddr(struct qdf_mac_addr *dst_addr,
  200. struct qdf_mac_addr *src_addr)
  201. {
  202. *dst_addr = *src_addr;
  203. }
  204. /**
  205. * qdf_set_macaddr_broadcast() - set a QDF MacAddress to the 'broadcast'
  206. * @mac_addr: pointer to the qdf MacAddress to set to broadcast
  207. *
  208. * This function sets a QDF MacAddress to the 'broadcast' MacAddress. Broadcast
  209. * MacAddress contains all 0xFF bytes.
  210. *
  211. * Return: none
  212. */
  213. static inline void qdf_set_macaddr_broadcast(struct qdf_mac_addr *mac_addr)
  214. {
  215. __qdf_set_macaddr_broadcast(mac_addr);
  216. }
  217. /**
  218. * qdf_set_u16() - Assign 16-bit unsigned value to a byte array base on CPU's
  219. * endianness.
  220. * @ptr: Starting address of a byte array
  221. * @value: The value to assign to the byte array
  222. *
  223. * Caller must validate the byte array has enough space to hold the vlaue
  224. *
  225. * Return: The address to the byte after the assignment. This may or may not
  226. * be valid. Caller to verify.
  227. */
  228. static inline uint8_t *qdf_set_u16(uint8_t *ptr, uint16_t value)
  229. {
  230. #if defined(ANI_BIG_BYTE_ENDIAN)
  231. *(ptr) = (uint8_t) (value >> 8);
  232. *(ptr + 1) = (uint8_t) (value);
  233. #else
  234. *(ptr + 1) = (uint8_t) (value >> 8);
  235. *(ptr) = (uint8_t) (value);
  236. #endif
  237. return ptr + 2;
  238. }
  239. /**
  240. * qdf_get_u16() - Retrieve a 16-bit unsigned value from a byte array base on
  241. * CPU's endianness.
  242. * @ptr: Starting address of a byte array
  243. * @value: Pointer to a caller allocated buffer for 16 bit value. Value is to
  244. * assign to this location.
  245. *
  246. * Caller must validate the byte array has enough space to hold the vlaue
  247. *
  248. * Return: The address to the byte after the assignment. This may or may not
  249. * be valid. Caller to verify.
  250. */
  251. static inline uint8_t *qdf_get_u16(uint8_t *ptr, uint16_t *value)
  252. {
  253. #if defined(ANI_BIG_BYTE_ENDIAN)
  254. *value = (((uint16_t) (*ptr << 8)) | ((uint16_t) (*(ptr + 1))));
  255. #else
  256. *value = (((uint16_t) (*(ptr + 1) << 8)) | ((uint16_t) (*ptr)));
  257. #endif
  258. return ptr + 2;
  259. }
  260. /**
  261. * qdf_get_u32() - retrieve a 32-bit unsigned value from a byte array base on
  262. * CPU's endianness.
  263. * @ptr: Starting address of a byte array
  264. * @value: Pointer to a caller allocated buffer for 32 bit value. Value is to
  265. * assign to this location.
  266. *
  267. * Caller must validate the byte array has enough space to hold the vlaue
  268. *
  269. * Return: The address to the byte after the assignment. This may or may not
  270. * be valid. Caller to verify.
  271. */
  272. static inline uint8_t *qdf_get_u32(uint8_t *ptr, uint32_t *value)
  273. {
  274. #if defined(ANI_BIG_BYTE_ENDIAN)
  275. *value = ((uint32_t) (*(ptr) << 24) |
  276. (uint32_t) (*(ptr + 1) << 16) |
  277. (uint32_t) (*(ptr + 2) << 8) | (uint32_t) (*(ptr + 3)));
  278. #else
  279. *value = ((uint32_t) (*(ptr + 3) << 24) |
  280. (uint32_t) (*(ptr + 2) << 16) |
  281. (uint32_t) (*(ptr + 1) << 8) | (uint32_t) (*(ptr)));
  282. #endif
  283. return ptr + 4;
  284. }
  285. /**
  286. * qdf_ntohs - Convert a 16-bit value from network byte order to host byte order
  287. */
  288. #define qdf_ntohs(x) __qdf_ntohs(x)
  289. /**
  290. * qdf_ntohl - Convert a 32-bit value from network byte order to host byte order
  291. */
  292. #define qdf_ntohl(x) __qdf_ntohl(x)
  293. /**
  294. * qdf_htons - Convert a 16-bit value from host byte order to network byte order
  295. */
  296. #define qdf_htons(x) __qdf_htons(x)
  297. /**
  298. * qdf_htonl - Convert a 32-bit value from host byte order to network byte order
  299. */
  300. #define qdf_htonl(x) __qdf_htonl(x)
  301. /**
  302. * qdf_cpu_to_le16 - Convert a 16-bit value from CPU byte order to
  303. * little-endian byte order
  304. */
  305. #define qdf_cpu_to_le16(x) __qdf_cpu_to_le16(x)
  306. /**
  307. * qdf_cpu_to_le32 - Convert a 32-bit value from CPU byte order to
  308. * little-endian byte order
  309. */
  310. #define qdf_cpu_to_le32(x) __qdf_cpu_to_le32(x)
  311. /**
  312. * qdf_cpu_to_le64 - Convert a 64-bit value from CPU byte order to
  313. * little-endian byte order
  314. */
  315. #define qdf_cpu_to_le64(x) __qdf_cpu_to_le64(x)
  316. /**
  317. * qdf_be32_to_cpu - Convert a 32-bit value from big-endian byte order
  318. * to CPU byte order
  319. */
  320. #define qdf_be32_to_cpu(x) __qdf_be32_to_cpu(x)
  321. /**
  322. * qdf_be64_to_cpu - Convert a 64-bit value from big-endian byte order
  323. * to CPU byte order
  324. */
  325. #define qdf_be64_to_cpu(x) __qdf_be64_to_cpu(x)
  326. /**
  327. * qdf_le32_to_cpu - Convert a 32-bit value from little-endian byte
  328. * order to CPU byte order
  329. */
  330. #define qdf_le32_to_cpu(x) __qdf_le32_to_cpu(x)
  331. /**
  332. * qdf_le64_to_cpu - Convert a 64-bit value from little-endian byte
  333. * order to CPU byte order
  334. */
  335. #define qdf_le64_to_cpu(x) __qdf_le64_to_cpu(x)
  336. /**
  337. * qdf_le16_to_cpu - Convert a 16-bit value from little-endian byte order
  338. * to CPU byte order
  339. * @x: value to be converted
  340. */
  341. #define qdf_le16_to_cpu(x) __qdf_le16_to_cpu(x)
  342. /**
  343. * qdf_function - replace with the name of the current function
  344. */
  345. #define qdf_function __qdf_function
  346. /**
  347. * qdf_get_pwr2() - get next power of 2 integer from input value
  348. * @value: input value to find next power of 2 integer
  349. *
  350. * Get next power of 2 integer from input value
  351. *
  352. * Return: Power of 2 integer
  353. */
  354. static inline int qdf_get_pwr2(int value)
  355. {
  356. int log2;
  357. if (QDF_IS_PWR2(value))
  358. return value;
  359. log2 = 0;
  360. while (value) {
  361. value >>= 1;
  362. log2++;
  363. }
  364. return 1 << log2;
  365. }
  366. #endif /*_QDF_UTIL_H*/