qdf_util.h 25 KB

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