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