xref: /wlan-dirver/qca-wifi-host-cmn/qdf/linux/src/i_qdf_util.h (revision 97f44cd39e4ff816eaa1710279d28cf6b9e65ad9)
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: i_qdf_util.h
21  * This file provides OS dependent API's.
22  */
23 
24 #ifndef _I_QDF_UTIL_H
25 #define _I_QDF_UTIL_H
26 
27 #include <linux/compiler.h>
28 #include <linux/kernel.h>
29 #include <linux/types.h>
30 #include <linux/mm.h>
31 #include <linux/errno.h>
32 #include <linux/average.h>
33 
34 #include <linux/random.h>
35 #include <linux/io.h>
36 
37 #include <qdf_types.h>
38 #include <asm/byteorder.h>
39 
40 #if LINUX_VERSION_CODE  <= KERNEL_VERSION(3, 3, 8)
41 #include <asm/system.h>
42 #else
43 #if defined(__LINUX_MIPS32_ARCH__) || defined(__LINUX_MIPS64_ARCH__)
44 #include <asm/dec/system.h>
45 #else
46 #endif
47 #endif
48 
49 #include <qdf_types.h>
50 #include <linux/io.h>
51 #include <asm/byteorder.h>
52 
53 #ifdef QCA_PARTNER_PLATFORM
54 #include "ath_carr_pltfrm.h"
55 #else
56 #include <linux/byteorder/generic.h>
57 #endif
58 
59 typedef wait_queue_head_t __qdf_wait_queue_head_t;
60 
61 /* Generic compiler-dependent macros if defined by the OS */
62 #define __qdf_wait_queue_interruptible(wait_queue, condition) \
63 	wait_event_interruptible(wait_queue, condition)
64 
65 #define __qdf_wait_queue_timeout(wait_queue, condition, timeout) \
66 	wait_event_timeout(wait_queue, condition, timeout)
67 
68 
69 #define __qdf_init_waitqueue_head(_q) init_waitqueue_head(_q)
70 
71 #define __qdf_wake_up_interruptible(_q) wake_up_interruptible(_q)
72 
73 #define __qdf_wake_up(_q) wake_up(_q)
74 
75 #define __qdf_wake_up_completion(_q) wake_up_completion(_q)
76 
77 #define __qdf_unlikely(_expr)   unlikely(_expr)
78 #define __qdf_likely(_expr)     likely(_expr)
79 
80 #define __qdf_bitmap(name, bits) DECLARE_BITMAP(name, bits)
81 
82 /**
83  * __qdf_set_bit() - set bit in address
84  * @nr: bit number to be set
85  * @addr: address buffer pointer
86  *
87  * Return: none
88  */
89 static inline void __qdf_set_bit(unsigned int nr, unsigned long *addr)
90 {
91 	__set_bit(nr, addr);
92 }
93 
94 static inline void __qdf_clear_bit(unsigned int nr, unsigned long *addr)
95 {
96 	__clear_bit(nr, addr);
97 }
98 
99 static inline bool __qdf_test_bit(unsigned int nr, unsigned long *addr)
100 {
101 	return test_bit(nr, addr);
102 }
103 
104 static inline bool __qdf_test_and_clear_bit(unsigned int nr,
105 					unsigned long *addr)
106 {
107 	return __test_and_clear_bit(nr, addr);
108 }
109 
110 static inline unsigned long __qdf_find_first_bit(unsigned long *addr,
111 					unsigned long nbits)
112 {
113 	return find_first_bit(addr, nbits);
114 }
115 
116 static inline bool __qdf_bitmap_empty(unsigned long *addr,
117 				      unsigned long nbits)
118 {
119 	return bitmap_empty(addr, nbits);
120 }
121 
122 static inline int __qdf_bitmap_and(unsigned long *dst, unsigned long *src1,
123 				   unsigned long *src2, unsigned long nbits)
124 {
125 	return bitmap_and(dst, src1, src2, nbits);
126 }
127 
128 /**
129  * __qdf_set_macaddr_broadcast() - set a QDF MacAddress to the 'broadcast'
130  * @mac_addr: pointer to the qdf MacAddress to set to broadcast
131  *
132  * This function sets a QDF MacAddress to the 'broadcast' MacAddress. Broadcast
133  * MacAddress contains all 0xFF bytes.
134  *
135  * Return: none
136  */
137 static inline void __qdf_set_macaddr_broadcast(struct qdf_mac_addr *mac_addr)
138 {
139 	memset(mac_addr, 0xff, QDF_MAC_ADDR_SIZE);
140 }
141 
142 /**
143  * __qdf_zero_macaddr() - zero out a MacAddress
144  * @mac_addr: pointer to the struct qdf_mac_addr to zero.
145  *
146  * This function zeros out a QDF MacAddress type.
147  *
148  * Return: none
149  */
150 static inline void __qdf_zero_macaddr(struct qdf_mac_addr *mac_addr)
151 {
152 	memset(mac_addr, 0, QDF_MAC_ADDR_SIZE);
153 }
154 
155 /**
156  * __qdf_is_macaddr_equal() - compare two QDF MacAddress
157  * @mac_addr1: Pointer to one qdf MacAddress to compare
158  * @mac_addr2: Pointer to the other qdf MacAddress to compare
159  *
160  * This function returns a bool that tells if a two QDF MacAddress'
161  * are equivalent.
162  *
163  * Return: true if the MacAddress's are equal
164  *      not true if the MacAddress's are not equal
165  */
166 static inline bool __qdf_is_macaddr_equal(struct qdf_mac_addr *mac_addr1,
167 					  struct qdf_mac_addr *mac_addr2)
168 {
169 	return 0 == memcmp(mac_addr1, mac_addr2, QDF_MAC_ADDR_SIZE);
170 }
171 
172 /**
173  * qdf_in_interrupt - returns true if in interrupt context
174  */
175 #define qdf_in_interrupt          in_interrupt
176 
177 #define __qdf_min(_a, _b) min(_a, _b)
178 #define __qdf_max(_a, _b) max(_a, _b)
179 
180 /**
181  * Setting it to blank as feature is not intended to be supported
182  * on linux version less than 4.3
183  */
184 #if LINUX_VERSION_CODE  < KERNEL_VERSION(4, 3, 0)
185 #define __QDF_DECLARE_EWMA(name, _factor, _weight)
186 
187 #define __qdf_ewma_tx_lag int
188 #define __qdf_ewma_rx_rssi int
189 #else
190 #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)
191 #define __QDF_DECLARE_EWMA(name, _factor, _weight) \
192 	DECLARE_EWMA(name, ilog2(_factor), _weight)
193 #else
194 #define __QDF_DECLARE_EWMA(name, _factor, _weight) \
195 	DECLARE_EWMA(name, _factor, _weight)
196 #endif
197 
198 #define __qdf_ewma_tx_lag struct ewma_tx_lag
199 #define __qdf_ewma_rx_rssi struct ewma_rx_rssi
200 #endif
201 
202 #define __qdf_ffz(mask) (~(mask) == 0 ? -1 : ffz(mask))
203 
204 #define MEMINFO_KB(x)  ((x) << (PAGE_SHIFT - 10))   /* In kilobytes */
205 
206 /**
207  * @brief Assert
208  */
209 #define __qdf_assert(expr)  do { \
210 		if (unlikely(!(expr))) { \
211 			pr_err("Assertion failed! %s:%s %s:%d\n", \
212 			       # expr, __func__, __FILE__, __LINE__); \
213 			dump_stack(); \
214 			QDF_BUG_ON_ASSERT(0); \
215 		} \
216 } while (0)
217 
218 /**
219  * @brief Assert
220  */
221 #define __qdf_target_assert(expr)  do {    \
222 	if (unlikely(!(expr))) {                                 \
223 		qdf_err("Assertion failed! %s:%s %s:%d",   \
224 		#expr, __FUNCTION__, __FILE__, __LINE__);      \
225 		dump_stack();                                      \
226 		QDF_DEBUG_PANIC("Take care of the TARGET ASSERT first\n");  \
227 	}     \
228 } while (0)
229 
230 /**
231  * @brief Compile time Assert
232  */
233 #define QDF_COMPILE_TIME_ASSERT(assertion_name, predicate) \
234     typedef char assertion_name[(predicate) ? 1 : -1]
235 
236 #define __qdf_container_of(ptr, type, member) container_of(ptr, type, member)
237 
238 #define __qdf_ntohs                      ntohs
239 #define __qdf_ntohl                      ntohl
240 
241 #define __qdf_htons                      htons
242 #define __qdf_htonl                      htonl
243 
244 #define __qdf_cpu_to_le16 cpu_to_le16
245 #define __qdf_cpu_to_le32 cpu_to_le32
246 #define __qdf_cpu_to_le64 cpu_to_le64
247 
248 #define __qdf_le16_to_cpu le16_to_cpu
249 #define __qdf_le32_to_cpu le32_to_cpu
250 #define __qdf_le64_to_cpu le64_to_cpu
251 
252 #define __qdf_cpu_to_be16 cpu_to_be16
253 #define __qdf_cpu_to_be32 cpu_to_be32
254 #define __qdf_cpu_to_be64 cpu_to_be64
255 
256 #define __qdf_be16_to_cpu be16_to_cpu
257 #define __qdf_be32_to_cpu be32_to_cpu
258 #define __qdf_be64_to_cpu be64_to_cpu
259 
260 /**
261  * @brief memory barriers.
262  */
263 #define __qdf_wmb()                wmb()
264 #define __qdf_rmb()                rmb()
265 #define __qdf_mb()                 mb()
266 #define __qdf_ioread32(offset)             ioread32(offset)
267 #define __qdf_iowrite32(offset, value)     iowrite32(value, offset)
268 
269 #define __qdf_roundup(x, y) roundup(x, y)
270 #define __qdf_ceil(x, y) DIV_ROUND_UP(x, y)
271 
272 #if LINUX_VERSION_CODE  < KERNEL_VERSION(4, 3, 0)
273 #define  __qdf_ewma_tx_lag_init(tx_lag)
274 #define  __qdf_ewma_tx_lag_add(tx_lag, value)
275 #define  __qdf_ewma_tx_lag_read(tx_lag)
276 
277 #define  __qdf_ewma_rx_rssi_init(rx_rssi)
278 #define  __qdf_ewma_rx_rssi_add(rx_rssi, value)
279 #define  __qdf_ewma_rx_rssi_read(rx_rssi)
280 #else
281 #define  __qdf_ewma_tx_lag_init(tx_lag) \
282 	ewma_tx_lag_init(tx_lag)
283 
284 #define  __qdf_ewma_tx_lag_add(tx_lag, value) \
285 	ewma_tx_lag_add(tx_lag, value)
286 
287 #define  __qdf_ewma_tx_lag_read(tx_lag) \
288 	ewma_tx_lag_read(tx_lag)
289 
290 #define  __qdf_ewma_rx_rssi_init(rx_rssi) \
291 	ewma_rx_rssi_init(rx_rssi)
292 
293 #define  __qdf_ewma_rx_rssi_add(rx_rssi, value) \
294 	ewma_rx_rssi_add(rx_rssi, value)
295 
296 #define  __qdf_ewma_rx_rssi_read(rx_rssi) \
297 	ewma_rx_rssi_read(rx_rssi)
298 #endif
299 
300 #define __qdf_prefetch(x)     prefetch(x)
301 
302 #ifdef QCA_CONFIG_SMP
303 /**
304  * __qdf_get_cpu() - get cpu_index
305  *
306  * Return: cpu_index
307  */
308 static inline
309 int __qdf_get_cpu(void)
310 {
311 	int cpu_index = get_cpu();
312 
313 	put_cpu();
314 	return cpu_index;
315 }
316 #else
317 static inline
318 int __qdf_get_cpu(void)
319 {
320 	return 0;
321 }
322 #endif
323 
324 static inline int __qdf_device_init_wakeup(__qdf_device_t qdf_dev, bool enable)
325 {
326 	return device_init_wakeup(qdf_dev->dev, enable);
327 }
328 
329 /**
330  * __qdf_get_totalramsize() -  Get total ram size in Kb
331  *
332  * Return: Total ram size in Kb
333  */
334 static inline uint64_t
335 __qdf_get_totalramsize(void)
336 {
337 	struct sysinfo meminfo;
338 
339 	si_meminfo(&meminfo);
340 	return MEMINFO_KB(meminfo.totalram);
341 }
342 
343 /**
344  * __qdf_get_lower_32_bits() - get lower 32 bits from an address.
345  * @addr: address
346  *
347  * This api returns the lower 32 bits of an address.
348  *
349  * Return: lower 32 bits.
350  */
351 static inline
352 uint32_t __qdf_get_lower_32_bits(__qdf_dma_addr_t addr)
353 {
354 	return lower_32_bits(addr);
355 }
356 
357 /**
358  * __qdf_get_upper_32_bits() - get upper 32 bits from an address.
359  * @addr: address
360  *
361  * This api returns the upper 32 bits of an address.
362  *
363  * Return: upper 32 bits.
364  */
365 static inline
366 uint32_t __qdf_get_upper_32_bits(__qdf_dma_addr_t addr)
367 {
368 	return upper_32_bits(addr);
369 }
370 
371 /**
372  * __qdf_rounddown_pow_of_two() - Round down to nearest power of two
373  * @n: number to be tested
374  *
375  * Test if the input number is power of two, and return the nearest power of two
376  *
377  * Return: number rounded down to the nearest power of two
378  */
379 static inline
380 unsigned long __qdf_rounddown_pow_of_two(unsigned long n)
381 {
382 	if (is_power_of_2(n))
383 		return n; /* already a power of 2 */
384 
385 	return __rounddown_pow_of_two(n);
386 }
387 
388 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0)
389 
390 /**
391  * __qdf_set_dma_coherent_mask() - set max number of bits allowed in dma addr
392  * @dev: device pointer
393  * @addr_bits: max number of bits allowed in dma address
394  *
395  * This API sets the maximum allowed number of bits in the dma address.
396  *
397  * Return: 0 - success, non zero - failure
398  */
399 static inline
400 int __qdf_set_dma_coherent_mask(struct device *dev, uint8_t addr_bits)
401 {
402 	return dma_set_mask_and_coherent(dev, DMA_BIT_MASK(addr_bits));
403 }
404 
405 #else
406 
407 /**
408  * __qdf_set_dma_coherent_mask() - set max number of bits allowed in dma addr
409  * @dev: device pointer
410  * @addr_bits: max number of bits allowed in dma address
411  *
412  * This API sets the maximum allowed number of bits in the dma address.
413  *
414  * Return: 0 - success, non zero - failure
415  */
416 static inline
417 int __qdf_set_dma_coherent_mask(struct device *dev, uint8_t addr_bits)
418 {
419 	return dma_set_coherent_mask(dev, DMA_BIT_MASK(addr_bits));
420 }
421 #endif
422 /**
423  * qdf_get_random_bytes() - returns nbytes bytes of random
424  * data
425  *
426  * Return: random bytes of data
427  */
428 static inline
429 void __qdf_get_random_bytes(void *buf, int nbytes)
430 {
431 	return get_random_bytes(buf, nbytes);
432 }
433 
434 /**
435  * __qdf_do_div() - wrapper function for kernel macro(do_div).
436  * @dividend: Dividend value
437  * @divisor : Divisor value
438  *
439  * Return: Quotient
440  */
441 static inline
442 uint64_t __qdf_do_div(uint64_t dividend, uint32_t divisor)
443 {
444 	do_div(dividend, divisor);
445 	/*do_div macro updates dividend with Quotient of dividend/divisor */
446 	return dividend;
447 }
448 
449 /**
450  * __qdf_do_div_rem() - wrapper function for kernel macro(do_div)
451  *                      to get remainder.
452  * @dividend: Dividend value
453  * @divisor : Divisor value
454  *
455  * Return: remainder
456  */
457 static inline
458 uint64_t __qdf_do_div_rem(uint64_t dividend, uint32_t divisor)
459 {
460 	return do_div(dividend, divisor);
461 }
462 
463 /**
464  * __qdf_hex_to_bin() - Wrapper function to kernel API to get unsigned
465  * integer from hexa decimal ASCII character.
466  * @ch: hexa decimal ASCII character
467  *
468  * Return: For hexa decimal ASCII char return actual decimal value
469  *	   else -1 for bad input.
470  */
471 static inline
472 int __qdf_hex_to_bin(char ch)
473 {
474 	return hex_to_bin(ch);
475 }
476 
477 /**
478  * __qdf_hex_str_to_binary() - Wrapper function to get array of unsigned
479  * integers from string of hexa decimal ASCII characters.
480  * @dst: output array to hold converted values
481  * @src: input string of hexa decimal ASCII characters
482  * @count: size of dst string
483  *
484  * Return: For a string of hexa decimal ASCII characters return 0
485  *	   else -1 for bad input.
486  */
487 static inline
488 int __qdf_hex_str_to_binary(u8 *dst, const char *src, size_t count)
489 {
490 	return hex2bin(dst, src, count);
491 }
492 
493 /**
494  * __qdf_fls() - find last set bit in a given 32 bit input
495  * @x: 32 bit mask
496  *
497  * Return: zero if the input is zero, otherwise returns the bit
498  * position of the last set bit, where the LSB is 1 and MSB is 32.
499  */
500 static inline
501 int __qdf_fls(uint32_t x)
502 {
503 	return fls(x);
504 }
505 
506 #endif /*_I_QDF_UTIL_H*/
507