qdf_util.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. /*
  2. * Copyright (c) 2014-2021 The Linux Foundation. All rights reserved.
  3. * Copyright (c) 2021-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for
  6. * any purpose with or without fee is hereby granted, provided that the
  7. * above copyright notice and this permission notice appear in all
  8. * copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  11. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  12. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  13. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  14. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  15. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  17. * PERFORMANCE OF THIS SOFTWARE.
  18. */
  19. /**
  20. * DOC: qdf_util.h
  21. * This file defines utility functions.
  22. */
  23. #ifndef _QDF_UTIL_H
  24. #define _QDF_UTIL_H
  25. #include <i_qdf_util.h>
  26. #ifdef QCA_CONFIG_SMP
  27. #define QDF_MAX_AVAILABLE_CPU 8
  28. #else
  29. #define QDF_MAX_AVAILABLE_CPU 1
  30. #endif
  31. typedef __qdf_wait_queue_head_t qdf_wait_queue_head_t;
  32. /**
  33. * qdf_unlikely - Compiler-dependent macro denoting code unlikely to execute
  34. * @_expr: expression to be checked
  35. */
  36. #define qdf_unlikely(_expr) __qdf_unlikely(_expr)
  37. /**
  38. * qdf_likely - Compiler-dependent macro denoting code likely to execute
  39. * @_expr: expression to be checked
  40. */
  41. #define qdf_likely(_expr) __qdf_likely(_expr)
  42. /**
  43. * qdf_wmb - write memory barrier.
  44. */
  45. #define qdf_wmb() __qdf_wmb()
  46. /**
  47. * qdf_rmb - read memory barrier.
  48. */
  49. #define qdf_rmb() __qdf_rmb()
  50. /**
  51. * qdf_mb - read + write memory barrier.
  52. */
  53. #define qdf_mb() __qdf_mb()
  54. /**
  55. * qdf_ioread32 - read a register
  56. * @offset: register address
  57. */
  58. #define qdf_ioread32(offset) __qdf_ioread32(offset)
  59. /**
  60. * qdf_iowrite32 - write a register
  61. * @offset: register address
  62. * @value: value to write (32bit value)
  63. */
  64. #define qdf_iowrite32(offset, value) __qdf_iowrite32(offset, value)
  65. /**
  66. * qdf_assert - assert "expr" evaluates to false.
  67. * @expr: expression to test
  68. */
  69. #ifdef QDF_DEBUG
  70. #define qdf_assert(expr) __qdf_assert(expr)
  71. #else
  72. #define qdf_assert(expr)
  73. #endif /* QDF_DEBUG */
  74. /**
  75. * qdf_assert_always - always assert "expr" evaluates to false.
  76. * @expr: expression to test
  77. */
  78. #define qdf_assert_always(expr) __qdf_assert(expr)
  79. /**
  80. * qdf_assert_always_func - invoke function to dump needed info before assert
  81. * @expr: expression to test
  82. * @debug_fp: function pointer to be invoked for debugging
  83. */
  84. #define qdf_assert_always_func(expr, debug_fp, ...) \
  85. do { \
  86. typeof(debug_fp) _debug_fp = debug_fp; \
  87. if (unlikely(!(expr))) { \
  88. pr_err("Assertion failed! %s:%s %s:%d\n", \
  89. # expr, __func__, __FILE__, __LINE__); \
  90. if (_debug_fp) \
  91. _debug_fp(__VA_ARGS__); \
  92. QDF_BUG_ON_ASSERT(0); \
  93. } \
  94. } while (0)
  95. /**
  96. * qdf_target_assert_always - always target assert "expr" evaluates to false.
  97. * @expr: expression to test
  98. */
  99. #define qdf_target_assert_always(expr) __qdf_target_assert(expr)
  100. #define QDF_SET_PARAM(__param, __val) ((__param) |= (1 << (__val)))
  101. #define QDF_HAS_PARAM(__param, __val) ((__param) & (1 << (__val)))
  102. #define QDF_CLEAR_PARAM(__param, __val) ((__param) &= (~(1 << (__val))))
  103. /**
  104. * QDF_MAX - get maximum of two values
  105. * @_x: 1st argument
  106. * @_y: 2nd argument
  107. */
  108. #define QDF_MAX(_x, _y) (((_x) > (_y)) ? (_x) : (_y))
  109. /**
  110. * QDF_MIN - get minimum of two values
  111. * @_x: 1st argument
  112. * @_y: 2nd argument
  113. */
  114. #define QDF_MIN(_x, _y) (((_x) < (_y)) ? (_x) : (_y))
  115. /**
  116. * QDF_IS_ADDR_BROADCAST - is mac address broadcast mac address
  117. * @_a: pointer to mac address
  118. */
  119. #define QDF_IS_ADDR_BROADCAST(_a) \
  120. ((_a)[0] == 0xff && \
  121. (_a)[1] == 0xff && \
  122. (_a)[2] == 0xff && \
  123. (_a)[3] == 0xff && \
  124. (_a)[4] == 0xff && \
  125. (_a)[5] == 0xff)
  126. /**
  127. * QDF_IS_LAST_3_BYTES_OF_MAC_SAME - check the last 3 bytes
  128. * same or not for two mac addresses
  129. * @mac1: mac address 1
  130. * @mac2: mac address 2
  131. */
  132. #define QDF_IS_LAST_3_BYTES_OF_MAC_SAME(mac1, mac2) \
  133. ((mac1)->bytes[3] == (mac2)->bytes[3] && \
  134. (mac1)->bytes[4] == (mac2)->bytes[4] && \
  135. (mac1)->bytes[5] == (mac2)->bytes[5])
  136. /* Get number of bits from the index bit */
  137. #define QDF_GET_BITS(_val, _index, _num_bits) \
  138. (((_val) >> (_index)) & ((1 << (_num_bits)) - 1))
  139. /* Set val to number of bits from the index bit */
  140. #define QDF_SET_BITS(_var, _index, _num_bits, _val) do { \
  141. (_var) &= ~(((1 << (_num_bits)) - 1) << (_index)); \
  142. (_var) |= (((_val) & ((1 << (_num_bits)) - 1)) << (_index)); \
  143. } while (0)
  144. #define QDF_SET_BITS64(_var, _tmp, _index, _num_bits, _val) do { \
  145. (_var) = (((_var) & 0xffffffff00000000) >> 32); \
  146. (_var) &= ~(((1 << (_num_bits)) - 1) << ((_index) - 32)); \
  147. (_var) |= (((_val) & ((1 << (_num_bits)) - 1)) << ((_index) - 32)); \
  148. (_var) = (((_var) & 0x00000000ffffffff) << 32); \
  149. (_var) |= ((_tmp) & 0x00000000ffffffff); \
  150. } while (0)
  151. /* Get number of bits from the index bit supporting 64 bits */
  152. #define QDF_GET_BITS64(_val, _index, _num_bits) \
  153. (((_val) >> (_index)) & ((1LLU << (_num_bits)) - 1))
  154. #define QDF_DECLARE_EWMA(name, factor, weight) \
  155. __QDF_DECLARE_EWMA(name, factor, weight)
  156. #define qdf_ewma_tx_lag __qdf_ewma_tx_lag
  157. #define qdf_ewma_tx_lag_init(tx_lag) \
  158. __qdf_ewma_tx_lag_init(tx_lag)
  159. #define qdf_ewma_tx_lag_add(tx_lag, value) \
  160. __qdf_ewma_tx_lag_add(tx_lag, value)
  161. #define qdf_ewma_tx_lag_read(tx_lag) \
  162. __qdf_ewma_tx_lag_read(tx_lag)
  163. #define qdf_ewma_rx_rssi __qdf_ewma_rx_rssi
  164. #define qdf_ewma_rx_rssi_init(rx_rssi) \
  165. __qdf_ewma_rx_rssi_init(rx_rssi)
  166. #define qdf_ewma_rx_rssi_add(rx_rssi, value) \
  167. __qdf_ewma_rx_rssi_add(rx_rssi, value)
  168. #define qdf_ewma_rx_rssi_read(rx_rssi) \
  169. __qdf_ewma_rx_rssi_read(rx_rssi)
  170. #define QDF_CHAR_BIT 8
  171. /**
  172. * qdf_bitmap - Define a bitmap
  173. * @name: name of the bitmap
  174. * @bits: num of bits in the bitmap
  175. *
  176. * Return: none
  177. */
  178. #define qdf_bitmap(name, bits) __qdf_bitmap(name, bits)
  179. /**
  180. * qdf_set_bit() - set bit in address
  181. * @nr: bit number to be set
  182. * @addr: address buffer pointer
  183. *
  184. * Return: none
  185. */
  186. #define qdf_set_bit(nr, addr) __qdf_set_bit(nr, addr)
  187. /**
  188. * qdf_clear_bit() - clear bit in address
  189. * @nr: bit number to be clear
  190. * @addr: address buffer pointer
  191. *
  192. * Return: none
  193. */
  194. #define qdf_clear_bit(nr, addr) __qdf_clear_bit(nr, addr)
  195. /**
  196. * qdf_test_bit() - test bit position in address
  197. * @nr: bit number to be tested
  198. * @addr: address buffer pointer
  199. *
  200. * Return: none
  201. */
  202. #define qdf_test_bit(nr, addr) __qdf_test_bit(nr, addr)
  203. /**
  204. * qdf_test_and_clear_bit() - test and clear bit position in address
  205. * @nr: bit number to be tested
  206. * @addr: address buffer pointer
  207. *
  208. * Return: none
  209. */
  210. #define qdf_test_and_clear_bit(nr, addr) __qdf_test_and_clear_bit(nr, addr)
  211. /**
  212. * qdf_find_first_bit() - find first bit position in address
  213. * @addr: address buffer pointer
  214. * @nbits: number of bits
  215. *
  216. * Return: position first set bit in addr
  217. */
  218. #define qdf_find_first_bit(addr, nbits) __qdf_find_first_bit(addr, nbits)
  219. /**
  220. * qdf_bitmap_empty() - Check if bitmap is empty
  221. * @addr: Address buffer pointer
  222. * @nbits: Number of bits
  223. *
  224. * Return: True if no bit set, else false
  225. */
  226. #define qdf_bitmap_empty(addr, nbits) __qdf_bitmap_empty(addr, nbits)
  227. /**
  228. * qdf_bitmap_and() - AND operation on the bitmap
  229. * @dst: Destination buffer pointer
  230. * @src1: First source buffer pointer
  231. * @src2: Second source buffer pointer
  232. * @nbits: Number of bits
  233. *
  234. * Return: Bitwise and of src1 and src2 in dst
  235. */
  236. #define qdf_bitmap_and(dst, src1, src2, nbits) \
  237. __qdf_bitmap_and(dst, src1, src2, nbits)
  238. #define qdf_wait_queue_interruptible(wait_queue, condition) \
  239. __qdf_wait_queue_interruptible(wait_queue, condition)
  240. /**
  241. * qdf_wait_queue_timeout() - wait for specified time on given condition
  242. * @wait_queue: wait queue to wait on
  243. * @condition: condition to wait on
  244. * @timeout: timeout value in jiffies
  245. *
  246. * Return: 0 if condition becomes false after timeout
  247. * 1 or remaining jiffies, if condition becomes true during timeout
  248. */
  249. #define qdf_wait_queue_timeout(wait_queue, condition, timeout) \
  250. __qdf_wait_queue_timeout(wait_queue, \
  251. condition, timeout)
  252. #define qdf_init_waitqueue_head(_q) __qdf_init_waitqueue_head(_q)
  253. #define qdf_wake_up_interruptible(_q) __qdf_wake_up_interruptible(_q)
  254. /**
  255. * qdf_wake_up() - wakes up sleeping waitqueue
  256. * @_q: wait queue, which needs wake up
  257. *
  258. * Return: none
  259. */
  260. #define qdf_wake_up(_q) __qdf_wake_up(_q)
  261. #define qdf_wake_up_completion(_q) __qdf_wake_up_completion(_q)
  262. /**
  263. * qdf_container_of() - cast a member of a structure out to the containing
  264. * structure
  265. * @ptr: the pointer to the member.
  266. * @type: the type of the container struct this is embedded in.
  267. * @member: the name of the member within the struct.
  268. */
  269. #define qdf_container_of(ptr, type, member) \
  270. __qdf_container_of(ptr, type, member)
  271. /**
  272. * QDF_IS_PWR2() - test input value is power of 2 integer
  273. * @value: input integer
  274. */
  275. #define QDF_IS_PWR2(value) (((value) ^ ((value)-1)) == ((value) << 1) - 1)
  276. /**
  277. * qdf_roundup() - roundup the input value
  278. * @x: value to roundup
  279. * @y: input value rounded to multiple of this
  280. *
  281. * Return: rounded value
  282. */
  283. #define qdf_roundup(x, y) __qdf_roundup(x, y)
  284. /**
  285. * qdf_ceil() - roundup of x/y
  286. * @x: dividend
  287. * @y: divisor
  288. *
  289. * Return: rounded value
  290. */
  291. #define qdf_ceil(x, y) __qdf_ceil(x, y)
  292. /**
  293. * qdf_in_interrupt - returns true if in interrupt context
  294. */
  295. #define qdf_in_interrupt __qdf_in_interrupt
  296. /**
  297. * qdf_is_macaddr_equal() - compare two QDF MacAddress
  298. * @mac_addr1: Pointer to one qdf MacAddress to compare
  299. * @mac_addr2: Pointer to the other qdf MacAddress to compare
  300. *
  301. * This function returns a bool that tells if a two QDF MacAddress'
  302. * are equivalent.
  303. *
  304. * Return: true if the MacAddress's are equal
  305. * not true if the MacAddress's are not equal
  306. */
  307. static inline bool qdf_is_macaddr_equal(const struct qdf_mac_addr *mac_addr1,
  308. const struct qdf_mac_addr *mac_addr2)
  309. {
  310. return __qdf_is_macaddr_equal(mac_addr1, mac_addr2);
  311. }
  312. /**
  313. * qdf_is_macaddr_zero() - check for a MacAddress of all zeros.
  314. * @mac_addr: pointer to the struct qdf_mac_addr to check.
  315. *
  316. * This function returns a bool that tells if a MacAddress is made up of
  317. * all zeros.
  318. *
  319. * Return: true if the MacAddress is all Zeros
  320. * false if the MacAddress is not all Zeros.
  321. */
  322. static inline bool qdf_is_macaddr_zero(const struct qdf_mac_addr *mac_addr)
  323. {
  324. struct qdf_mac_addr zero_mac_addr = QDF_MAC_ADDR_ZERO_INIT;
  325. return qdf_is_macaddr_equal(mac_addr, &zero_mac_addr);
  326. }
  327. /**
  328. * qdf_zero_macaddr() - zero out a MacAddress
  329. * @mac_addr: pointer to the struct qdf_mac_addr to zero.
  330. *
  331. * This function zeros out a QDF MacAddress type.
  332. *
  333. * Return: none
  334. */
  335. static inline void qdf_zero_macaddr(struct qdf_mac_addr *mac_addr)
  336. {
  337. __qdf_zero_macaddr(mac_addr);
  338. }
  339. /**
  340. * qdf_is_macaddr_group() - check for a MacAddress is a 'group' address
  341. * @mac_addr: pointer to the qdf MacAddress to check
  342. *
  343. * This function returns a bool that tells if a the input QDF MacAddress
  344. * is a "group" address. Group addresses have the 'group address bit' turned
  345. * on in the MacAddress. Group addresses are made up of Broadcast and
  346. * Multicast addresses.
  347. *
  348. * Return: true if the input MacAddress is a Group address
  349. * false if the input MacAddress is not a Group address
  350. */
  351. static inline bool qdf_is_macaddr_group(struct qdf_mac_addr *mac_addr)
  352. {
  353. return mac_addr->bytes[0] & 0x01;
  354. }
  355. /**
  356. * qdf_is_macaddr_broadcast() - check for a MacAddress is a broadcast address
  357. * @mac_addr: Pointer to the qdf MacAddress to check
  358. *
  359. * This function returns a bool that tells if a the input QDF MacAddress
  360. * is a "broadcast" address.
  361. *
  362. * Return: true if the input MacAddress is a broadcast address
  363. * flase if the input MacAddress is not a broadcast address
  364. */
  365. static inline bool qdf_is_macaddr_broadcast(const struct qdf_mac_addr *mac_addr)
  366. {
  367. struct qdf_mac_addr broadcast_mac_addr = QDF_MAC_ADDR_BCAST_INIT;
  368. return qdf_is_macaddr_equal(mac_addr, &broadcast_mac_addr);
  369. }
  370. /**
  371. * qdf_copy_macaddr() - copy a QDF MacAddress
  372. * @dst_addr: pointer to the qdf MacAddress to copy TO (the destination)
  373. * @src_addr: pointer to the qdf MacAddress to copy FROM (the source)
  374. *
  375. * This function copies a QDF MacAddress into another QDF MacAddress.
  376. *
  377. * Return: none
  378. */
  379. static inline void qdf_copy_macaddr(struct qdf_mac_addr *dst_addr,
  380. const struct qdf_mac_addr *src_addr)
  381. {
  382. *dst_addr = *src_addr;
  383. }
  384. /**
  385. * qdf_set_macaddr_broadcast() - set a QDF MacAddress to the 'broadcast'
  386. * @mac_addr: pointer to the qdf MacAddress to set to broadcast
  387. *
  388. * This function sets a QDF MacAddress to the 'broadcast' MacAddress. Broadcast
  389. * MacAddress contains all 0xFF bytes.
  390. *
  391. * Return: none
  392. */
  393. static inline void qdf_set_macaddr_broadcast(struct qdf_mac_addr *mac_addr)
  394. {
  395. __qdf_set_macaddr_broadcast(mac_addr);
  396. }
  397. /**
  398. * qdf_set_u16() - Assign 16-bit unsigned value to a byte array base on CPU's
  399. * endianness.
  400. * @ptr: Starting address of a byte array
  401. * @value: The value to assign to the byte array
  402. *
  403. * Caller must validate the byte array has enough space to hold the value
  404. *
  405. * Return: The address to the byte after the assignment. This may or may not
  406. * be valid. Caller to verify.
  407. */
  408. static inline uint8_t *qdf_set_u16(uint8_t *ptr, uint16_t value)
  409. {
  410. #if defined(ANI_BIG_BYTE_ENDIAN)
  411. *(ptr) = (uint8_t) (value >> 8);
  412. *(ptr + 1) = (uint8_t) (value);
  413. #else
  414. *(ptr + 1) = (uint8_t) (value >> 8);
  415. *(ptr) = (uint8_t) (value);
  416. #endif
  417. return ptr + 2;
  418. }
  419. /**
  420. * qdf_get_u16() - Retrieve a 16-bit unsigned value from a byte array base on
  421. * CPU's endianness.
  422. * @ptr: Starting address of a byte array
  423. * @value: Pointer to a caller allocated buffer for 16 bit value. Value is to
  424. * assign to this location.
  425. *
  426. * Caller must validate the byte array has enough space to hold the value
  427. *
  428. * Return: The address to the byte after the assignment. This may or may not
  429. * be valid. Caller to verify.
  430. */
  431. static inline uint8_t *qdf_get_u16(uint8_t *ptr, uint16_t *value)
  432. {
  433. #if defined(ANI_BIG_BYTE_ENDIAN)
  434. *value = (((uint16_t) (*ptr << 8)) | ((uint16_t) (*(ptr + 1))));
  435. #else
  436. *value = (((uint16_t) (*(ptr + 1) << 8)) | ((uint16_t) (*ptr)));
  437. #endif
  438. return ptr + 2;
  439. }
  440. /**
  441. * qdf_get_u32() - retrieve a 32-bit unsigned value from a byte array base on
  442. * CPU's endianness.
  443. * @ptr: Starting address of a byte array
  444. * @value: Pointer to a caller allocated buffer for 32 bit value. Value is to
  445. * assign to this location.
  446. *
  447. * Caller must validate the byte array has enough space to hold the value
  448. *
  449. * Return: The address to the byte after the assignment. This may or may not
  450. * be valid. Caller to verify.
  451. */
  452. static inline uint8_t *qdf_get_u32(uint8_t *ptr, uint32_t *value)
  453. {
  454. #if defined(ANI_BIG_BYTE_ENDIAN)
  455. *value = ((uint32_t) (*(ptr) << 24) |
  456. (uint32_t) (*(ptr + 1) << 16) |
  457. (uint32_t) (*(ptr + 2) << 8) | (uint32_t) (*(ptr + 3)));
  458. #else
  459. *value = ((uint32_t) (*(ptr + 3) << 24) |
  460. (uint32_t) (*(ptr + 2) << 16) |
  461. (uint32_t) (*(ptr + 1) << 8) | (uint32_t) (*(ptr)));
  462. #endif
  463. return ptr + 4;
  464. }
  465. /**
  466. * qdf_abs() - Get absolute value
  467. * @x: value to be converted
  468. */
  469. #define qdf_abs(x) __qdf_abs(x)
  470. /**
  471. * qdf_ntohs() - Convert a 16-bit value from network byte order to host byte order
  472. * @x: value to be converted
  473. */
  474. #define qdf_ntohs(x) __qdf_ntohs(x)
  475. /**
  476. * qdf_ntohl() - Convert a 32-bit value from network byte order to host byte order
  477. * @x: value to be converted
  478. */
  479. #define qdf_ntohl(x) __qdf_ntohl(x)
  480. /**
  481. * qdf_htons() - Convert a 16-bit value from host byte order to network byte order
  482. * @x: value to be converted
  483. */
  484. #define qdf_htons(x) __qdf_htons(x)
  485. /**
  486. * qdf_htonl() - Convert a 32-bit value from host byte order to network byte order
  487. * @x: value to be converted
  488. */
  489. #define qdf_htonl(x) __qdf_htonl(x)
  490. /**
  491. * qdf_cpu_to_le16() - Convert a 16-bit value from CPU byte order to
  492. * little-endian byte order
  493. *
  494. * @x: value to be converted
  495. */
  496. #define qdf_cpu_to_le16(x) __qdf_cpu_to_le16(x)
  497. /**
  498. * qdf_cpu_to_le32() - Convert a 32-bit value from CPU byte order to
  499. * little-endian byte order
  500. *
  501. * @x: value to be converted
  502. */
  503. #define qdf_cpu_to_le32(x) __qdf_cpu_to_le32(x)
  504. /**
  505. * qdf_cpu_to_le64() - Convert a 64-bit value from CPU byte order to
  506. * little-endian byte order
  507. *
  508. * @x: value to be converted
  509. */
  510. #define qdf_cpu_to_le64(x) __qdf_cpu_to_le64(x)
  511. /**
  512. * qdf_le16_to_cpu() - Convert a 16-bit value from little-endian byte order
  513. * to CPU byte order
  514. *
  515. * @x: value to be converted
  516. */
  517. #define qdf_le16_to_cpu(x) __qdf_le16_to_cpu(x)
  518. /**
  519. * qdf_le32_to_cpu() - Convert a 32-bit value from little-endian byte
  520. * order to CPU byte order
  521. *
  522. * @x: value to be converted
  523. */
  524. #define qdf_le32_to_cpu(x) __qdf_le32_to_cpu(x)
  525. /**
  526. * qdf_le64_to_cpu() - Convert a 64-bit value from little-endian byte
  527. * order to CPU byte order
  528. *
  529. * @x: value to be converted
  530. */
  531. #define qdf_le64_to_cpu(x) __qdf_le64_to_cpu(x)
  532. /**
  533. * qdf_cpu_to_be16() - Convert a 16-bit value from CPU byte order to
  534. * big-endian byte order
  535. *
  536. * @x: value to be converted
  537. */
  538. #define qdf_cpu_to_be16(x) __qdf_cpu_to_be16(x)
  539. /**
  540. * qdf_cpu_to_be32() - Convert a 32-bit value from CPU byte order to
  541. * big-endian byte order
  542. *
  543. * @x: value to be converted
  544. */
  545. #define qdf_cpu_to_be32(x) __qdf_cpu_to_be32(x)
  546. /**
  547. * qdf_cpu_to_be64() - Convert a 64-bit value from CPU byte order to
  548. * big-endian byte order
  549. *
  550. * @x: value to be converted
  551. */
  552. #define qdf_cpu_to_be64(x) __qdf_cpu_to_be64(x)
  553. /**
  554. * qdf_be16_to_cpu() - Convert a 16-bit value from big-endian byte order
  555. * to CPU byte order
  556. *
  557. * @x: value to be converted
  558. */
  559. #define qdf_be16_to_cpu(x) __qdf_be16_to_cpu(x)
  560. /**
  561. * qdf_be32_to_cpu() - Convert a 32-bit value from big-endian byte order
  562. * to CPU byte order
  563. *
  564. * @x: value to be converted
  565. */
  566. #define qdf_be32_to_cpu(x) __qdf_be32_to_cpu(x)
  567. /**
  568. * qdf_be64_to_cpu() - Convert a 64-bit value from big-endian byte order
  569. * to CPU byte order
  570. *
  571. * @x: value to be converted
  572. */
  573. #define qdf_be64_to_cpu(x) __qdf_be64_to_cpu(x)
  574. /**
  575. * qdf_function - replace with the name of the current function
  576. */
  577. #define qdf_function __qdf_function
  578. /**
  579. * qdf_min() - minimum of two numbers
  580. * @a: first number
  581. * @b: second number
  582. */
  583. #define qdf_min(a, b) __qdf_min(a, b)
  584. /**
  585. * qdf_ffz() - find first (least significant) zero bit
  586. * @mask: the bitmask to check
  587. *
  588. * Return: The zero-based index of the first zero bit, or -1 if none are found
  589. */
  590. #define qdf_ffz(mask) __qdf_ffz(mask)
  591. /**
  592. * qdf_prefetch - prefetches the cacheline for read
  593. *
  594. * @x: address to be prefetched
  595. */
  596. #define qdf_prefetch(x) __qdf_prefetch(x)
  597. /**
  598. * qdf_get_pwr2() - get next power of 2 integer from input value
  599. * @value: input value to find next power of 2 integer
  600. *
  601. * Get next power of 2 integer from input value
  602. *
  603. * Return: Power of 2 integer
  604. */
  605. static inline int qdf_get_pwr2(int value)
  606. {
  607. int log2;
  608. if (QDF_IS_PWR2(value))
  609. return value;
  610. log2 = 0;
  611. while (value) {
  612. value >>= 1;
  613. log2++;
  614. }
  615. return 1 << log2;
  616. }
  617. static inline
  618. int qdf_get_cpu(void)
  619. {
  620. return __qdf_get_cpu();
  621. }
  622. /**
  623. * qdf_get_hweight8() - count num of 1's in 8-bit bitmap
  624. * @w: input bitmap
  625. *
  626. * Count num of 1's set in the 8-bit bitmap
  627. *
  628. * Return: num of 1's
  629. */
  630. static inline
  631. unsigned int qdf_get_hweight8(unsigned int w)
  632. {
  633. unsigned int res = w - ((w >> 1) & 0x55);
  634. res = (res & 0x33) + ((res >> 2) & 0x33);
  635. return (res + (res >> 4)) & 0x0F;
  636. }
  637. /**
  638. * qdf_get_hweight16() - count num of 1's in 16-bit bitmap
  639. * @w: input bitmap
  640. *
  641. * Count num of 1's set in the 16-bit bitmap
  642. *
  643. * Return: num of 1's
  644. */
  645. static inline
  646. unsigned int qdf_get_hweight16(unsigned int w)
  647. {
  648. unsigned int res = (w & 0x5555) + ((w >> 1) & 0x5555);
  649. res = (res & 0x3333) + ((res >> 2) & 0x3333);
  650. res = (res & 0x0F0F) + ((res >> 4) & 0x0F0F);
  651. return (res & 0x00FF) + ((res >> 8) & 0x00FF);
  652. }
  653. /**
  654. * qdf_get_hweight32() - count num of 1's in 32-bit bitmap
  655. * @w: input bitmap
  656. *
  657. * Count num of 1's set in the 32-bit bitmap
  658. *
  659. * Return: num of 1's
  660. */
  661. static inline
  662. unsigned int qdf_get_hweight32(unsigned int w)
  663. {
  664. unsigned int res = (w & 0x55555555) + ((w >> 1) & 0x55555555);
  665. res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
  666. res = (res & 0x0F0F0F0F) + ((res >> 4) & 0x0F0F0F0F);
  667. res = (res & 0x00FF00FF) + ((res >> 8) & 0x00FF00FF);
  668. return (res & 0x0000FFFF) + ((res >> 16) & 0x0000FFFF);
  669. }
  670. /**
  671. * qdf_device_init_wakeup() - allow a device to wake up the aps system
  672. * @qdf_dev: the qdf device context
  673. * @enable: enable/disable the device as a wakeup source
  674. *
  675. * Return: 0 or errno
  676. */
  677. static inline int qdf_device_init_wakeup(qdf_device_t qdf_dev, bool enable)
  678. {
  679. return __qdf_device_init_wakeup(qdf_dev, enable);
  680. }
  681. static inline
  682. uint64_t qdf_get_totalramsize(void)
  683. {
  684. return __qdf_get_totalramsize();
  685. }
  686. /**
  687. * qdf_get_lower_32_bits() - get lower 32 bits from an address.
  688. * @addr: address
  689. *
  690. * This api returns the lower 32 bits of an address.
  691. *
  692. * Return: lower 32 bits.
  693. */
  694. static inline
  695. uint32_t qdf_get_lower_32_bits(qdf_dma_addr_t addr)
  696. {
  697. return __qdf_get_lower_32_bits(addr);
  698. }
  699. /**
  700. * qdf_get_upper_32_bits() - get upper 32 bits from an address.
  701. * @addr: address
  702. *
  703. * This api returns the upper 32 bits of an address.
  704. *
  705. * Return: upper 32 bits.
  706. */
  707. static inline
  708. uint32_t qdf_get_upper_32_bits(qdf_dma_addr_t addr)
  709. {
  710. return __qdf_get_upper_32_bits(addr);
  711. }
  712. /**
  713. * qdf_rounddown_pow_of_two() - Round down to nearest power of two
  714. * @n: number to be tested
  715. *
  716. * Test if the input number is power of two, and return the nearest power of two
  717. *
  718. * Return: number rounded down to the nearest power of two
  719. */
  720. static inline
  721. unsigned long qdf_rounddown_pow_of_two(unsigned long n)
  722. {
  723. return __qdf_rounddown_pow_of_two(n);
  724. }
  725. /**
  726. * qdf_set_dma_coherent_mask() - set max number of bits allowed in dma addr
  727. * @dev: device pointer
  728. * @addr_bits: max number of bits allowed in dma address
  729. *
  730. * This API sets the maximum allowed number of bits in the dma address.
  731. *
  732. * Return: 0 - success, non zero - failure
  733. */
  734. static inline
  735. int qdf_set_dma_coherent_mask(struct device *dev, uint8_t addr_bits)
  736. {
  737. return __qdf_set_dma_coherent_mask(dev, addr_bits);
  738. }
  739. /**
  740. * qdf_do_div() - wrapper function for kernel macro(do_div).
  741. * @dividend: Dividend value
  742. * @divisor : Divisor value
  743. *
  744. * Return: Quotient
  745. */
  746. static inline
  747. uint64_t qdf_do_div(uint64_t dividend, uint32_t divisor)
  748. {
  749. return __qdf_do_div(dividend, divisor);
  750. }
  751. /**
  752. * qdf_do_div_rem() - wrapper function for kernel macro(do_div)
  753. * to get remainder.
  754. * @dividend: Dividend value
  755. * @divisor : Divisor value
  756. *
  757. * Return: remainder
  758. */
  759. static inline
  760. uint64_t qdf_do_div_rem(uint64_t dividend, uint32_t divisor)
  761. {
  762. return __qdf_do_div_rem(dividend, divisor);
  763. }
  764. /**
  765. * qdf_get_random_bytes() - returns nbytes bytes of random data
  766. * @buf: buffer to fill
  767. * @nbytes: number of bytes to fill
  768. *
  769. * Return: random bytes of data
  770. */
  771. static inline
  772. void qdf_get_random_bytes(void *buf, int nbytes)
  773. {
  774. return __qdf_get_random_bytes(buf, nbytes);
  775. }
  776. /**
  777. * qdf_hex_to_bin() - QDF API to Convert hexa decimal ASCII character to
  778. * unsigned integer value.
  779. * @ch: hexa decimal ASCII character
  780. *
  781. * Return: For hexa decimal ASCII char return actual decimal value
  782. * else -1 for bad input.
  783. */
  784. static inline
  785. int qdf_hex_to_bin(char ch)
  786. {
  787. return __qdf_hex_to_bin(ch);
  788. }
  789. /**
  790. * qdf_hex_str_to_binary() - QDF API to Convert string of hexa decimal
  791. * ASCII characters to array of unsigned integers.
  792. * @dst: output array to hold converted values
  793. * @src: input string of hexa decimal ASCII characters
  794. * @count: size of dst string
  795. *
  796. * This function is used to convert string of hexa decimal characters to
  797. * array of unsigned integers and caller should ensure:
  798. * a) @dst, @src are not NULL,
  799. * b) size of @dst should be (size of src / 2)
  800. *
  801. * Example 1:
  802. * src = 11aa, means, src[0] = '1', src[1] = '2', src[2] = 'a', src[3] = 'a'
  803. * count = (size of src / 2) = 2
  804. * after conversion, dst[0] = 0x11, dst[1] = oxAA and return (0).
  805. *
  806. * Example 2:
  807. * src = 11az, means, src[0] = '1', src[1] = '2', src[2] = 'a', src[3] = 'z'
  808. * src[3] is not ASCII hexa decimal character, return negative value (-1).
  809. *
  810. * Return: For a string of hexa decimal ASCII characters return 0
  811. * else -1 for bad input.
  812. */
  813. static inline
  814. int qdf_hex_str_to_binary(u8 *dst, const char *src, size_t count)
  815. {
  816. return __qdf_hex_str_to_binary(dst, src, count);
  817. }
  818. /**
  819. * qdf_fls() - find last set bit in a given 32 bit input
  820. * @x: 32 bit mask
  821. *
  822. * Return: zero if the input is zero, otherwise returns the bit
  823. * position of the last set bit, where the LSB is 1 and MSB is 32.
  824. */
  825. static inline
  826. int qdf_fls(uint32_t x)
  827. {
  828. return __qdf_fls(x);
  829. }
  830. /**
  831. * qdf_get_smp_processor_id() - Get the current CPU id
  832. *
  833. * Return: current CPU id
  834. */
  835. static inline int qdf_get_smp_processor_id(void)
  836. {
  837. return __qdf_get_smp_processor_id();
  838. }
  839. /**
  840. * qdf_in_atomic: Check whether current thread running in atomic context
  841. *
  842. * Return: true if current thread is running in the atomic context
  843. * else it will be return false.
  844. */
  845. static inline bool qdf_in_atomic(void)
  846. {
  847. return __qdf_in_atomic();
  848. }
  849. #endif /*_QDF_UTIL_H*/