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