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