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