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