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