xref: /wlan-dirver/qca-wifi-host-cmn/qdf/inc/qdf_util.h (revision 6ecd284e5a94a1c96e26d571dd47419ac305990d)
1 /*
2  * Copyright (c) 2014-2018 The Linux Foundation. All rights reserved.
3  *
4  * Previously licensed under the ISC license by Qualcomm Atheros, Inc.
5  *
6  *
7  * Permission to use, copy, modify, and/or distribute this software for
8  * any purpose with or without fee is hereby granted, provided that the
9  * above copyright notice and this permission notice appear in all
10  * copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
13  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
14  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
15  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
16  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
17  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
18  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19  * PERFORMANCE OF THIS SOFTWARE.
20  */
21 
22 /*
23  * This file was originally distributed by Qualcomm Atheros, Inc.
24  * under proprietary terms before Copyright ownership was assigned
25  * to the Linux Foundation.
26  */
27 
28 /**
29  * DOC: qdf_util.h
30  * This file defines utility functions.
31  */
32 
33 #ifndef _QDF_UTIL_H
34 #define _QDF_UTIL_H
35 
36 #include <i_qdf_util.h>
37 
38 #ifdef QCA_CONFIG_SMP
39 #define QDF_MAX_AVAILABLE_CPU	8
40 #else
41 #define QDF_MAX_AVAILABLE_CPU	1
42 #endif
43 
44 typedef __qdf_wait_queue_head_t qdf_wait_queue_head_t;
45 
46 /**
47  * qdf_unlikely - Compiler-dependent macro denoting code likely to execute
48  * @_expr: expression to be checked
49  */
50 #define qdf_unlikely(_expr)     __qdf_unlikely(_expr)
51 
52 /**
53  * qdf_likely - Compiler-dependent macro denoting code unlikely to execute
54  * @_expr: expression to be checked
55  */
56 #define qdf_likely(_expr)       __qdf_likely(_expr)
57 
58 /**
59  * qdf_mb - read + write memory barrier.
60  */
61 #define qdf_mb()                 __qdf_mb()
62 
63 /**
64  * qdf_ioread32 - read a register
65  * @offset: register address
66  */
67 #define qdf_ioread32(offset)            __qdf_ioread32(offset)
68 /**
69  * qdf_iowrite32 - write a register
70  * @offset: register address
71  * @value: value to write (32bit value)
72  */
73 #define qdf_iowrite32(offset, value)    __qdf_iowrite32(offset, value)
74 
75 /**
76  * qdf_assert - assert "expr" evaluates to false.
77  */
78 #ifdef QDF_DEBUG
79 #define qdf_assert(expr)         __qdf_assert(expr)
80 #else
81 #define qdf_assert(expr)
82 #endif /* QDF_DEBUG */
83 
84 /**
85  * qdf_assert_always - alway assert "expr" evaluates to false.
86  */
87 #define qdf_assert_always(expr)  __qdf_assert(expr)
88 
89 /**
90  * qdf_target_assert_always - alway target assert "expr" evaluates to false.
91  */
92 #define qdf_target_assert_always(expr)  __qdf_target_assert(expr)
93 
94 /**
95  * QDF_MAX - get maximum of two values
96  * @_x: 1st arguement
97  * @_y: 2nd arguement
98  */
99 #define QDF_MAX(_x, _y) (((_x) > (_y)) ? (_x) : (_y))
100 
101 /**
102  * QDF_MIN - get minimum of two values
103  * @_x: 1st arguement
104  * @_y: 2nd arguement
105  */
106 #define QDF_MIN(_x, _y) (((_x) < (_y)) ? (_x) : (_y))
107 
108 /**
109  * qdf_status_to_os_return - returns the status to OS.
110  * @status: enum QDF_STATUS
111  *
112  * returns: int status success/failure
113  */
114 static inline int qdf_status_to_os_return(QDF_STATUS status)
115 {
116 	return __qdf_status_to_os_return(status);
117 }
118 
119 /**
120  * qdf_set_bit() - set bit in address
121  * @nr: bit number to be set
122  * @addr: address buffer pointer
123  *
124  * Return: none
125  */
126 #define qdf_set_bit(nr, addr)    __qdf_set_bit(nr, addr)
127 
128 /**
129  * qdf_clear_bit() - clear bit in address
130  * @nr: bit number to be clear
131  * @addr: address buffer pointer
132  *
133  * Return: none
134  */
135 #define qdf_clear_bit(nr, addr)    __qdf_clear_bit(nr, addr)
136 
137 /**
138  * qdf_test_bit() - test bit position in address
139  * @nr: bit number to be tested
140  * @addr: address buffer pointer
141  *
142  * Return: none
143  */
144 #define qdf_test_bit(nr, addr)    __qdf_test_bit(nr, addr)
145 
146 /**
147  * qdf_test_and_clear_bit() - test and clear bit position in address
148  * @nr: bit number to be tested
149  * @addr: address buffer pointer
150  *
151  * Return: none
152  */
153 #define qdf_test_and_clear_bit(nr, addr)    __qdf_test_and_clear_bit(nr, addr)
154 
155 /**
156  * qdf_find_first_bit() - find first bit position in address
157  * @addr: address buffer pointer
158  * @nbits: number of bits
159  *
160  * Return: position first set bit in addr
161  */
162 #define qdf_find_first_bit(addr, nbits)    __qdf_find_first_bit(addr, nbits)
163 
164 #define qdf_wait_queue_interruptible(wait_queue, condition) \
165 		__qdf_wait_queue_interruptible(wait_queue, condition)
166 
167 /**
168  * qdf_wait_queue_timeout() - wait for specified time on given condition
169  * @wait_queue: wait queue to wait on
170  * @condition: condition to wait on
171  * @timeout: timeout value in jiffies
172  *
173  * Return: 0 if condition becomes false after timeout
174  *         1 or remaining jiffies, if condition becomes true during timeout
175  */
176 #define qdf_wait_queue_timeout(wait_queue, condition, timeout) \
177 			__qdf_wait_queue_timeout(wait_queue, \
178 						condition, timeout)
179 
180 
181 #define qdf_init_waitqueue_head(_q) __qdf_init_waitqueue_head(_q)
182 
183 #define qdf_wake_up_interruptible(_q) __qdf_wake_up_interruptible(_q)
184 
185 /**
186  * qdf_wake_up() - wakes up sleeping waitqueue
187  * @wait_queue: wait queue, which needs wake up
188  *
189  * Return: none
190  */
191 #define qdf_wake_up(_q) __qdf_wake_up(_q)
192 
193 #define qdf_wake_up_completion(_q) __qdf_wake_up_completion(_q)
194 
195 /**
196  * qdf_container_of - cast a member of a structure out to the containing
197  * structure
198  * @ptr: the pointer to the member.
199  * @type: the type of the container struct this is embedded in.
200  * @member: the name of the member within the struct.
201  */
202 #define qdf_container_of(ptr, type, member) \
203 	 __qdf_container_of(ptr, type, member)
204 
205 /**
206  * qdf_is_pwr2 - test input value is power of 2 integer
207  * @value: input integer
208  */
209 #define QDF_IS_PWR2(value) (((value) ^ ((value)-1)) == ((value) << 1) - 1)
210 
211 /**
212  * qdf_roundup() - roundup the input value
213  * @x: value to roundup
214  * @y: input value rounded to multiple of this
215  *
216  * Return: rounded value
217  */
218 #define qdf_roundup(x, y) __qdf_roundup(x, y)
219 
220 /**
221  * qdf_is_macaddr_equal() - compare two QDF MacAddress
222  * @mac_addr1: Pointer to one qdf MacAddress to compare
223  * @mac_addr2: Pointer to the other qdf MacAddress to compare
224  *
225  * This function returns a bool that tells if a two QDF MacAddress'
226  * are equivalent.
227  *
228  * Return: true if the MacAddress's are equal
229  * not true if the MacAddress's are not equal
230  */
231 static inline bool qdf_is_macaddr_equal(struct qdf_mac_addr *mac_addr1,
232 					struct qdf_mac_addr *mac_addr2)
233 {
234 	return __qdf_is_macaddr_equal(mac_addr1, mac_addr2);
235 }
236 
237 
238 /**
239  * qdf_is_macaddr_zero() - check for a MacAddress of all zeros.
240  * @mac_addr: pointer to the struct qdf_mac_addr to check.
241  *
242  * This function returns a bool that tells if a MacAddress is made up of
243  * all zeros.
244  *
245  * Return: true if the MacAddress is all Zeros
246  * false if the MacAddress is not all Zeros.
247  */
248 static inline bool qdf_is_macaddr_zero(struct qdf_mac_addr *mac_addr)
249 {
250 	struct qdf_mac_addr zero_mac_addr = QDF_MAC_ADDR_ZERO_INIT;
251 
252 	return qdf_is_macaddr_equal(mac_addr, &zero_mac_addr);
253 }
254 
255 /**
256  * qdf_zero_macaddr() - zero out a MacAddress
257  * @mac_addr: pointer to the struct qdf_mac_addr to zero.
258  *
259  * This function zeros out a QDF MacAddress type.
260  *
261  * Return: none
262  */
263 static inline void qdf_zero_macaddr(struct qdf_mac_addr *mac_addr)
264 {
265 	__qdf_zero_macaddr(mac_addr);
266 }
267 
268 
269 /**
270  * qdf_is_macaddr_group() - check for a MacAddress is a 'group' address
271  * @mac_addr1: pointer to the qdf MacAddress to check
272  *
273  * This function returns a bool that tells if a the input QDF MacAddress
274  * is a "group" address. Group addresses have the 'group address bit' turned
275  * on in the MacAddress. Group addresses are made up of Broadcast and
276  * Multicast addresses.
277  *
278  * Return: true if the input MacAddress is a Group address
279  * false if the input MacAddress is not a Group address
280  */
281 static inline bool qdf_is_macaddr_group(struct qdf_mac_addr *mac_addr)
282 {
283 	return mac_addr->bytes[0] & 0x01;
284 }
285 
286 
287 /**
288  * qdf_is_macaddr_broadcast() - check for a MacAddress is a broadcast address
289  * @mac_addr: Pointer to the qdf MacAddress to check
290  *
291  * This function returns a bool that tells if a the input QDF MacAddress
292  * is a "broadcast" address.
293  *
294  * Return: true if the input MacAddress is a broadcast address
295  * flase if the input MacAddress is not a broadcast address
296  */
297 static inline bool qdf_is_macaddr_broadcast(struct qdf_mac_addr *mac_addr)
298 {
299 	struct qdf_mac_addr broadcast_mac_addr = QDF_MAC_ADDR_BCAST_INIT;
300 	return qdf_is_macaddr_equal(mac_addr, &broadcast_mac_addr);
301 }
302 
303 /**
304  * qdf_copy_macaddr() - copy a QDF MacAddress
305  * @dst_addr: pointer to the qdf MacAddress to copy TO (the destination)
306  * @src_addr: pointer to the qdf MacAddress to copy FROM (the source)
307  *
308  * This function copies a QDF MacAddress into another QDF MacAddress.
309  *
310  * Return: none
311  */
312 static inline void qdf_copy_macaddr(struct qdf_mac_addr *dst_addr,
313 				    struct qdf_mac_addr *src_addr)
314 {
315 	*dst_addr = *src_addr;
316 }
317 
318 /**
319  * qdf_set_macaddr_broadcast() - set a QDF MacAddress to the 'broadcast'
320  * @mac_addr: pointer to the qdf MacAddress to set to broadcast
321  *
322  * This function sets a QDF MacAddress to the 'broadcast' MacAddress. Broadcast
323  * MacAddress contains all 0xFF bytes.
324  *
325  * Return: none
326  */
327 static inline void qdf_set_macaddr_broadcast(struct qdf_mac_addr *mac_addr)
328 {
329 	__qdf_set_macaddr_broadcast(mac_addr);
330 }
331 
332 /**
333  * qdf_set_u16() - Assign 16-bit unsigned value to a byte array base on CPU's
334  * endianness.
335  * @ptr: Starting address of a byte array
336  * @value: The value to assign to the byte array
337  *
338  * Caller must validate the byte array has enough space to hold the vlaue
339  *
340  * Return: The address to the byte after the assignment. This may or may not
341  * be valid. Caller to verify.
342  */
343 static inline uint8_t *qdf_set_u16(uint8_t *ptr, uint16_t value)
344 {
345 #if defined(ANI_BIG_BYTE_ENDIAN)
346 	*(ptr) = (uint8_t) (value >> 8);
347 	*(ptr + 1) = (uint8_t) (value);
348 #else
349 	*(ptr + 1) = (uint8_t) (value >> 8);
350 	*(ptr) = (uint8_t) (value);
351 #endif
352 	return ptr + 2;
353 }
354 
355 /**
356  * qdf_get_u16() - Retrieve a 16-bit unsigned value from a byte array base on
357  * CPU's endianness.
358  * @ptr: Starting address of a byte array
359  * @value: Pointer to a caller allocated buffer for 16 bit value. Value is to
360  * assign to this location.
361  *
362  * Caller must validate the byte array has enough space to hold the vlaue
363  *
364  * Return: The address to the byte after the assignment. This may or may not
365  * be valid. Caller to verify.
366  */
367 static inline uint8_t *qdf_get_u16(uint8_t *ptr, uint16_t *value)
368 {
369 #if defined(ANI_BIG_BYTE_ENDIAN)
370 	*value = (((uint16_t) (*ptr << 8)) | ((uint16_t) (*(ptr + 1))));
371 #else
372 	*value = (((uint16_t) (*(ptr + 1) << 8)) | ((uint16_t) (*ptr)));
373 #endif
374 	return ptr + 2;
375 }
376 
377 /**
378  * qdf_get_u32() - retrieve a 32-bit unsigned value from a byte array base on
379  * CPU's endianness.
380  * @ptr: Starting address of a byte array
381  * @value: Pointer to a caller allocated buffer for 32 bit value. Value is to
382  * assign to this location.
383  *
384  * Caller must validate the byte array has enough space to hold the vlaue
385  *
386  * Return: The address to the byte after the assignment. This may or may not
387  * be valid. Caller to verify.
388  */
389 static inline uint8_t *qdf_get_u32(uint8_t *ptr, uint32_t *value)
390 {
391 #if defined(ANI_BIG_BYTE_ENDIAN)
392 	*value = ((uint32_t) (*(ptr) << 24) |
393 		   (uint32_t) (*(ptr + 1) << 16) |
394 		   (uint32_t) (*(ptr + 2) << 8) | (uint32_t) (*(ptr + 3)));
395 #else
396 	*value = ((uint32_t) (*(ptr + 3) << 24) |
397 		   (uint32_t) (*(ptr + 2) << 16) |
398 		   (uint32_t) (*(ptr + 1) << 8) | (uint32_t) (*(ptr)));
399 #endif
400 	return ptr + 4;
401 }
402 
403 /**
404  * qdf_ntohs - Convert a 16-bit value from network byte order to host byte order
405  */
406 #define qdf_ntohs(x)                         __qdf_ntohs(x)
407 
408 /**
409  * qdf_ntohl - Convert a 32-bit value from network byte order to host byte order
410  */
411 #define qdf_ntohl(x)                         __qdf_ntohl(x)
412 
413 /**
414  * qdf_htons - Convert a 16-bit value from host byte order to network byte order
415  */
416 #define qdf_htons(x)                         __qdf_htons(x)
417 
418 /**
419  * qdf_htonl - Convert a 32-bit value from host byte order to network byte order
420  */
421 #define qdf_htonl(x)                         __qdf_htonl(x)
422 
423 /**
424  * qdf_cpu_to_le16 - Convert a 16-bit value from CPU byte order to
425  * little-endian byte order
426  *
427  * @x: value to be converted
428  */
429 #define qdf_cpu_to_le16(x)                   __qdf_cpu_to_le16(x)
430 
431 /**
432  * qdf_cpu_to_le32 - Convert a 32-bit value from CPU byte order to
433  * little-endian byte order
434  *
435  * @x: value to be converted
436  */
437 #define qdf_cpu_to_le32(x)                   __qdf_cpu_to_le32(x)
438 
439 /**
440  * qdf_cpu_to_le64 - Convert a 64-bit value from CPU byte order to
441  * little-endian byte order
442  *
443  * @x: value to be converted
444  */
445 #define qdf_cpu_to_le64(x)                   __qdf_cpu_to_le64(x)
446 
447 /**
448  * qdf_le16_to_cpu - Convert a 16-bit value from little-endian byte order
449  * to CPU byte order
450  *
451  * @x: value to be converted
452  */
453 #define qdf_le16_to_cpu(x)                   __qdf_le16_to_cpu(x)
454 
455 /**
456  * qdf_le32_to_cpu - Convert a 32-bit value from little-endian byte
457  * order to CPU byte order
458  *
459  * @x: value to be converted
460  */
461 #define qdf_le32_to_cpu(x)                   __qdf_le32_to_cpu(x)
462 
463 /**
464  * qdf_le64_to_cpu - Convert a 64-bit value from little-endian byte
465  * order to CPU byte order
466  *
467  * @x: value to be converted
468  */
469 #define qdf_le64_to_cpu(x)                   __qdf_le64_to_cpu(x)
470 
471 /**
472  * qdf_cpu_to_be16 - Convert a 16-bit value from CPU byte order to
473  * big-endian byte order
474  *
475  * @x: value to be converted
476  */
477 #define qdf_cpu_to_be16(x)                   __qdf_cpu_to_be16(x)
478 
479 /**
480  * qdf_cpu_to_be32 - Convert a 32-bit value from CPU byte order to
481  * big-endian byte order
482  *
483  * @x: value to be converted
484  */
485 #define qdf_cpu_to_be32(x)                   __qdf_cpu_to_be32(x)
486 
487 /**
488  * qdf_cpu_to_be64 - Convert a 64-bit value from CPU byte order to
489  * big-endian byte order
490  *
491  * @x: value to be converted
492  */
493 #define qdf_cpu_to_be64(x)                   __qdf_cpu_to_be64(x)
494 
495 
496 /**
497  * qdf_be16_to_cpu - Convert a 16-bit value from big-endian byte order
498  * to CPU byte order
499  *
500  * @x: value to be converted
501  */
502 #define qdf_be16_to_cpu(x)                   __qdf_be16_to_cpu(x)
503 
504 /**
505  * qdf_be32_to_cpu - Convert a 32-bit value from big-endian byte order
506  * to CPU byte order
507  *
508  * @x: value to be converted
509  */
510 #define qdf_be32_to_cpu(x)                   __qdf_be32_to_cpu(x)
511 
512 /**
513  * qdf_be64_to_cpu - Convert a 64-bit value from big-endian byte order
514  * to CPU byte order
515  *
516  * @x: value to be converted
517  */
518 #define qdf_be64_to_cpu(x)                   __qdf_be64_to_cpu(x)
519 
520 /**
521  * qdf_function - replace with the name of the current function
522  */
523 #define qdf_function             __qdf_function
524 
525 /**
526  * qdf_min - minimum of two numbers
527  */
528 #define qdf_min(a, b)   __qdf_min(a, b)
529 
530 /**
531  * qdf_ffz() - find first (least significant) zero bit
532  * @mask: the bitmask to check
533  *
534  * Return: The zero-based index of the first zero bit, or -1 if none are found
535  */
536 #define qdf_ffz(mask) __qdf_ffz(mask)
537 
538 /**
539  * qdf_get_pwr2() - get next power of 2 integer from input value
540  * @value: input value to find next power of 2 integer
541  *
542  * Get next power of 2 integer from input value
543  *
544  * Return: Power of 2 integer
545  */
546 static inline int qdf_get_pwr2(int value)
547 {
548 	int log2;
549 
550 	if (QDF_IS_PWR2(value))
551 		return value;
552 
553 	log2 = 0;
554 	while (value) {
555 		value >>= 1;
556 		log2++;
557 	}
558 	return 1 << log2;
559 }
560 
561 static inline
562 int qdf_get_cpu(void)
563 {
564 	return __qdf_get_cpu();
565 }
566 
567 /**
568  * qdf_device_init_wakeup() - allow a device to wake up the aps system
569  * @qdf_dev: the qdf device context
570  * @enable: enable/disable the device as a wakup source
571  *
572  * Return: 0 or errno
573  */
574 static inline int qdf_device_init_wakeup(qdf_device_t qdf_dev, bool enable)
575 {
576 	return __qdf_device_init_wakeup(qdf_dev, enable);
577 }
578 
579 static inline
580 uint64_t qdf_get_totalramsize(void)
581 {
582 	return __qdf_get_totalramsize();
583 }
584 
585 /**
586  * qdf_get_lower_32_bits() - get lower 32 bits from an address.
587  * @addr: address
588  *
589  * This api returns the lower 32 bits of an address.
590  *
591  * Return: lower 32 bits.
592  */
593 static inline
594 uint32_t qdf_get_lower_32_bits(qdf_dma_addr_t addr)
595 {
596 	return __qdf_get_lower_32_bits(addr);
597 }
598 
599 /**
600  * qdf_get_upper_32_bits() - get upper 32 bits from an address.
601  * @addr: address
602  *
603  * This api returns the upper 32 bits of an address.
604  *
605  * Return: upper 32 bits.
606  */
607 static inline
608 uint32_t qdf_get_upper_32_bits(qdf_dma_addr_t addr)
609 {
610 	return __qdf_get_upper_32_bits(addr);
611 }
612 
613 /**
614  * qdf_rounddown_pow_of_two() - Round down to nearest power of two
615  * @n: number to be tested
616  *
617  * Test if the input number is power of two, and return the nearest power of two
618  *
619  * Return: number rounded down to the nearest power of two
620  */
621 static inline
622 unsigned long qdf_rounddown_pow_of_two(unsigned long n)
623 {
624 	return __qdf_rounddown_pow_of_two(n);
625 }
626 
627 /**
628  * qdf_set_dma_coherent_mask() - set max number of bits allowed in dma addr
629  * @dev: device pointer
630  * @addr_bits: max number of bits allowed in dma address
631  *
632  * This API sets the maximum allowed number of bits in the dma address.
633  *
634  * Return: 0 - success, non zero - failure
635  */
636 static inline
637 int qdf_set_dma_coherent_mask(struct device *dev, uint8_t addr_bits)
638 {
639 	return __qdf_set_dma_coherent_mask(dev, addr_bits);
640 }
641 
642 /**
643  * qdf_do_div() - wrapper function for kernel macro(do_div).
644  * @dividend: Dividend value
645  * @divisor : Divisor value
646  *
647  * Return: Quotient
648  */
649 static inline
650 uint64_t qdf_do_div(uint64_t dividend, uint32_t divisor)
651 {
652 	return __qdf_do_div(dividend, divisor);
653 }
654 
655 /**
656  * qdf_do_div_rem() - wrapper function for kernel macro(do_div)
657  *                    to get remainder.
658  * @dividend: Dividend value
659  * @divisor : Divisor value
660  *
661  * Return: remainder
662  */
663 static inline
664 uint64_t qdf_do_div_rem(uint64_t dividend, uint32_t divisor)
665 {
666 	return __qdf_do_div_rem(dividend, divisor);
667 }
668 
669 /**
670  * qdf_get_random_bytes() - returns nbytes bytes of random
671  * data
672  *
673  * Return: random bytes of data
674  */
675 static inline
676 void qdf_get_random_bytes(void *buf, int nbytes)
677 {
678 	return __qdf_get_random_bytes(buf, nbytes);
679 }
680 #endif /*_QDF_UTIL_H*/
681