qdf_util.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * Copyright (c) 2014-2015 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: cdf_util.h
  28. *
  29. * This file defines utility functions.
  30. */
  31. #ifndef _CDF_UTIL_H
  32. #define _CDF_UTIL_H
  33. #include <i_cdf_util.h>
  34. /**
  35. * cdf_unlikely - Compiler-dependent macro denoting code likely to execute
  36. * @_expr: expression to be checked
  37. */
  38. #define cdf_unlikely(_expr) __cdf_unlikely(_expr)
  39. /**
  40. * cdf_likely - Compiler-dependent macro denoting code unlikely to execute
  41. * @_expr: expression to be checked
  42. */
  43. #define cdf_likely(_expr) __cdf_likely(_expr)
  44. CDF_INLINE_FN int cdf_status_to_os_return(CDF_STATUS status)
  45. {
  46. return __cdf_status_to_os_return(status);
  47. }
  48. /**
  49. * cdf_assert - assert "expr" evaluates to false
  50. * @expr: assert expression
  51. */
  52. #ifdef CDF_OS_DEBUG
  53. #define cdf_assert(expr) __cdf_assert(expr)
  54. #else
  55. #define cdf_assert(expr)
  56. #endif /* CDF_OS_DEBUG */
  57. /**
  58. * @cdf_assert_always- alway assert "expr" evaluates to false
  59. * @expr: assert expression
  60. */
  61. #define cdf_assert_always(expr) __cdf_assert(expr)
  62. /**
  63. * cdf_os_cpu_to_le64 - Convert a 64-bit value from CPU byte order to
  64. * little-endian byte order
  65. * @x: value to be converted
  66. */
  67. #define cdf_os_cpu_to_le64(x) __cdf_os_cpu_to_le64(x)
  68. /**
  69. * cdf_le16_to_cpu - Convert a 16-bit value from little-endian byte order
  70. * to CPU byte order
  71. * @x: value to be converted
  72. */
  73. #define cdf_le16_to_cpu(x) __cdf_le16_to_cpu(x)
  74. /**
  75. * cdf_le32_to_cpu - Convert a 32-bit value from little-endian byte order to
  76. * CPU byte order
  77. * @x: value to be converted
  78. */
  79. #define cdf_le32_to_cpu(x) __cdf_le32_to_cpu(x)
  80. /**
  81. * cdf_in_interrupt - returns true if in interrupt context
  82. */
  83. #define cdf_in_interrupt in_interrupt
  84. /**
  85. * cdf_container_of - cast a member of a structure out to the containing
  86. * structure
  87. * @ptr: the pointer to the member.
  88. * @type: the type of the container struct this is embedded in.
  89. * @member: the name of the member within the struct.
  90. *
  91. */
  92. #define cdf_container_of(ptr, type, member) \
  93. __cdf_container_of(ptr, type, member)
  94. /**
  95. * cdf_is_pwr2 - test input value is power of 2 integer
  96. *
  97. * @value: input integer
  98. *
  99. */
  100. #define CDF_IS_PWR2(value) (((value) ^ ((value)-1)) == ((value) << 1) - 1)
  101. /**
  102. * cdf_is_macaddr_equal() - compare two CDF MacAddress
  103. * @pMacAddr1: Pointer to one cdf MacAddress to compare
  104. * @pMacAddr2: Pointer to the other cdf MacAddress to compare
  105. *
  106. * This function returns a bool that tells if a two CDF MacAddress'
  107. * are equivalent.
  108. *
  109. * Return: true if the MacAddress's are equal
  110. * not true if the MacAddress's are not equal
  111. */
  112. CDF_INLINE_FN bool cdf_is_macaddr_equal(struct cdf_mac_addr *pMacAddr1,
  113. struct cdf_mac_addr *pMacAddr2)
  114. {
  115. return 0 == memcmp(pMacAddr1, pMacAddr2, CDF_MAC_ADDR_SIZE);
  116. }
  117. /**
  118. * cdf_is_macaddr_zero() - check for a MacAddress of all zeros.
  119. * @pMacAddr - pointer to the struct cdf_mac_addr to check.
  120. *
  121. * This function returns a bool that tells if a MacAddress is made up of
  122. * all zeros.
  123. *
  124. *
  125. * Return: true if the MacAddress is all Zeros
  126. * false if the MacAddress is not all Zeros.
  127. *
  128. */
  129. CDF_INLINE_FN bool cdf_is_macaddr_zero(struct cdf_mac_addr *pMacAddr)
  130. {
  131. struct cdf_mac_addr zeroMacAddr = CDF_MAC_ADDR_ZERO_INITIALIZER;
  132. return cdf_is_macaddr_equal(pMacAddr, &zeroMacAddr);
  133. }
  134. /**
  135. * cdf_zero_macaddr() - zero out a MacAddress
  136. * @pMacAddr: pointer to the struct cdf_mac_addr to zero.
  137. *
  138. * This function zeros out a CDF MacAddress type.
  139. *
  140. * Return: nothing
  141. */
  142. CDF_INLINE_FN void cdf_zero_macaddr(struct cdf_mac_addr *pMacAddr)
  143. {
  144. memset(pMacAddr, 0, CDF_MAC_ADDR_SIZE);
  145. }
  146. /**
  147. * cdf_is_macaddr_group() - check for a MacAddress is a 'group' address
  148. * @pMacAddr1: pointer to the cdf MacAddress to check
  149. *
  150. * This function returns a bool that tells if a the input CDF MacAddress
  151. * is a "group" address. Group addresses have the 'group address bit' turned
  152. * on in the MacAddress. Group addresses are made up of Broadcast and
  153. * Multicast addresses.
  154. *
  155. * Return: true if the input MacAddress is a Group address
  156. * false if the input MacAddress is not a Group address
  157. */
  158. CDF_INLINE_FN bool cdf_is_macaddr_group(struct cdf_mac_addr *pMacAddr)
  159. {
  160. return pMacAddr->bytes[0] & 0x01;
  161. }
  162. /**
  163. * cdf_is_macaddr_broadcast() - check for a MacAddress is a broadcast address
  164. *
  165. * This function returns a bool that tells if a the input CDF MacAddress
  166. * is a "broadcast" address.
  167. *
  168. * @pMacAddr: Pointer to the cdf MacAddress to check
  169. *
  170. * Return: true if the input MacAddress is a broadcast address
  171. * false if the input MacAddress is not a broadcast address
  172. */
  173. CDF_INLINE_FN bool cdf_is_macaddr_broadcast(struct cdf_mac_addr *pMacAddr)
  174. {
  175. struct cdf_mac_addr broadcastMacAddr =
  176. CDF_MAC_ADDR_BROADCAST_INITIALIZER;
  177. return cdf_is_macaddr_equal(pMacAddr, &broadcastMacAddr);
  178. }
  179. /**
  180. * cdf_copy_macaddr() - copy a CDF MacAddress
  181. * @pDst - pointer to the cdf MacAddress to copy TO (the destination)
  182. * @pSrc - pointer to the cdf MacAddress to copy FROM (the source)
  183. *
  184. * This function copies a CDF MacAddress into another CDF MacAddress.
  185. *
  186. *
  187. * Return: nothing
  188. */
  189. CDF_INLINE_FN void cdf_copy_macaddr(struct cdf_mac_addr *pDst,
  190. struct cdf_mac_addr *pSrc)
  191. {
  192. *pDst = *pSrc;
  193. }
  194. /**
  195. * cdf_set_macaddr_broadcast() - set a CDF MacAddress to the 'broadcast'
  196. * @pMacAddr: pointer to the cdf MacAddress to set to broadcast
  197. *
  198. * This function sets a CDF MacAddress to the 'broadcast' MacAddress. Broadcast
  199. * MacAddress contains all 0xFF bytes.
  200. *
  201. * Return: nothing
  202. */
  203. CDF_INLINE_FN void cdf_set_macaddr_broadcast(struct cdf_mac_addr *pMacAddr)
  204. {
  205. memset(pMacAddr, 0xff, CDF_MAC_ADDR_SIZE);
  206. }
  207. #if defined(ANI_LITTLE_BYTE_ENDIAN)
  208. /**
  209. * i_cdf_htonl() - convert from host byte order to network byte order
  210. * @ul: input to be converted
  211. *
  212. * Return: converted network byte order
  213. */
  214. CDF_INLINE_FN unsigned long i_cdf_htonl(unsigned long ul)
  215. {
  216. return ((ul & 0x000000ff) << 24) |
  217. ((ul & 0x0000ff00) << 8) |
  218. ((ul & 0x00ff0000) >> 8) | ((ul & 0xff000000) >> 24);
  219. }
  220. /**
  221. * i_cdf_ntohl() - convert network byte order to host byte order
  222. * @ul: input to be converted
  223. *
  224. * Return: converted host byte order
  225. */
  226. CDF_INLINE_FN unsigned long i_cdf_ntohl(unsigned long ul)
  227. {
  228. return i_cdf_htonl(ul);
  229. }
  230. #endif
  231. /**
  232. * cdf_set_u16() - Assign 16-bit unsigned value to a byte array base on CPU's
  233. * endianness.
  234. * @ptr: Starting address of a byte array
  235. * @value: The value to assign to the byte array
  236. *
  237. * Caller must validate the byte array has enough space to hold the vlaue
  238. *
  239. * Return: The address to the byte after the assignment. This may or may not
  240. * be valid. Caller to verify.
  241. */
  242. CDF_INLINE_FN uint8_t *cdf_set_u16(uint8_t *ptr, uint16_t value)
  243. {
  244. #if defined(ANI_BIG_BYTE_ENDIAN)
  245. *(ptr) = (uint8_t) (value >> 8);
  246. *(ptr + 1) = (uint8_t) (value);
  247. #else
  248. *(ptr + 1) = (uint8_t) (value >> 8);
  249. *(ptr) = (uint8_t) (value);
  250. #endif
  251. return ptr + 2;
  252. }
  253. /**
  254. * cdf_get_u16() - Retrieve a 16-bit unsigned value from a byte array base on
  255. * CPU's endianness.
  256. * @ptr: Starting address of a byte array
  257. * @pValue: Pointer to a caller allocated buffer for 16 bit value. Value is to
  258. * assign to this location.
  259. *
  260. * Caller must validate the byte array has enough space to hold the vlaue
  261. *
  262. * Return: The address to the byte after the assignment. This may or may not
  263. * be valid. Caller to verify.
  264. */
  265. CDF_INLINE_FN uint8_t *cdf_get_u16(uint8_t *ptr, uint16_t *pValue)
  266. {
  267. #if defined(ANI_BIG_BYTE_ENDIAN)
  268. *pValue = (((uint16_t) (*ptr << 8)) | ((uint16_t) (*(ptr + 1))));
  269. #else
  270. *pValue = (((uint16_t) (*(ptr + 1) << 8)) | ((uint16_t) (*ptr)));
  271. #endif
  272. return ptr + 2;
  273. }
  274. /**
  275. * cdf_get_u32() - retrieve a 32-bit unsigned value from a byte array base on
  276. * CPU's endianness.
  277. * @ptr: Starting address of a byte array
  278. * @pValue: Pointer to a caller allocated buffer for 32 bit value. Value is to
  279. * assign to this location.
  280. *
  281. * Caller must validate the byte array has enough space to hold the vlaue
  282. *
  283. * Return: The address to the byte after the assignment. This may or may not
  284. * be valid. Caller to verify.
  285. */
  286. CDF_INLINE_FN uint8_t *cdf_get_u32(uint8_t *ptr, uint32_t *pValue)
  287. {
  288. #if defined(ANI_BIG_BYTE_ENDIAN)
  289. *pValue = ((uint32_t) (*(ptr) << 24) |
  290. (uint32_t) (*(ptr + 1) << 16) |
  291. (uint32_t) (*(ptr + 2) << 8) | (uint32_t) (*(ptr + 3)));
  292. #else
  293. *pValue = ((uint32_t) (*(ptr + 3) << 24) |
  294. (uint32_t) (*(ptr + 2) << 16) |
  295. (uint32_t) (*(ptr + 1) << 8) | (uint32_t) (*(ptr)));
  296. #endif
  297. return ptr + 4;
  298. }
  299. /**
  300. * cdf_get_pwr2() - get next power of 2 integer from input value
  301. * @value: input value to find next power of 2 integer
  302. *
  303. * Get next power of 2 integer from input value
  304. *
  305. * Return: Power of 2 integer
  306. */
  307. CDF_INLINE_FN int cdf_get_pwr2(int value)
  308. {
  309. int log2;
  310. if (CDF_IS_PWR2(value))
  311. return value;
  312. log2 = 0;
  313. while (value) {
  314. value >>= 1;
  315. log2++;
  316. }
  317. return 1 << log2;
  318. }
  319. #endif /*_CDF_UTIL_H*/