xref: /wlan-dirver/qca-wifi-host-cmn/qdf/linux/src/i_qdf_util.h (revision 6ecd284e5a94a1c96e26d571dd47419ac305990d)
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 #define __qdf_min(_a, _b) min(_a, _b)
237 #define __qdf_max(_a, _b) max(_a, _b)
238 
239 #define __qdf_ffz(mask) (~(mask) == 0 ? -1 : ffz(mask))
240 
241 #define MEMINFO_KB(x)  ((x) << (PAGE_SHIFT - 10))   /* In kilobytes */
242 
243 /**
244  * @brief Assert
245  */
246 #define __qdf_assert(expr)  do { \
247 		if (unlikely(!(expr))) { \
248 			pr_err("Assertion failed! %s:%s %s:%d\n", \
249 			       # expr, __func__, __FILE__, __LINE__); \
250 			dump_stack(); \
251 			QDF_BUG(0); \
252 		} \
253 } while (0)
254 
255 /**
256  * @brief Assert
257  */
258 #define __qdf_target_assert(expr)  do {    \
259 	if (unlikely(!(expr))) {                                 \
260 		qdf_print("Assertion failed! %s:%s %s:%d\n",   \
261 		#expr, __FUNCTION__, __FILE__, __LINE__);      \
262 		dump_stack();                                      \
263 		panic("Take care of the TARGET ASSERT first\n");          \
264 	}     \
265 } while (0)
266 
267 /**
268  * @brief Compile time Assert
269  */
270 #define QDF_COMPILE_TIME_ASSERT(assertion_name, predicate) \
271     typedef char assertion_name[(predicate) ? 1 : -1]
272 
273 #define __qdf_container_of(ptr, type, member) container_of(ptr, type, member)
274 
275 #define __qdf_ntohs                      ntohs
276 #define __qdf_ntohl                      ntohl
277 
278 #define __qdf_htons                      htons
279 #define __qdf_htonl                      htonl
280 
281 #define __qdf_cpu_to_le16 cpu_to_le16
282 #define __qdf_cpu_to_le32 cpu_to_le32
283 #define __qdf_cpu_to_le64 cpu_to_le64
284 
285 #define __qdf_le16_to_cpu le16_to_cpu
286 #define __qdf_le32_to_cpu le32_to_cpu
287 #define __qdf_le64_to_cpu le64_to_cpu
288 
289 #define __qdf_cpu_to_be16 cpu_to_be16
290 #define __qdf_cpu_to_be32 cpu_to_be32
291 #define __qdf_cpu_to_be64 cpu_to_be64
292 
293 #define __qdf_be16_to_cpu be16_to_cpu
294 #define __qdf_be32_to_cpu be32_to_cpu
295 #define __qdf_be64_to_cpu be64_to_cpu
296 
297 /**
298  * @brief memory barriers.
299  */
300 #define __qdf_wmb()                wmb()
301 #define __qdf_rmb()                rmb()
302 #define __qdf_mb()                 mb()
303 #define __qdf_ioread32(offset)             ioread32(offset)
304 #define __qdf_iowrite32(offset, value)     iowrite32(value, offset)
305 
306 #define __qdf_roundup(x, y) roundup(x, y)
307 
308 #ifdef QCA_CONFIG_SMP
309 /**
310  * __qdf_get_cpu() - get cpu_index
311  *
312  * Return: cpu_index
313  */
314 static inline
315 int __qdf_get_cpu(void)
316 {
317 	int cpu_index = get_cpu();
318 
319 	put_cpu();
320 	return cpu_index;
321 }
322 #else
323 static inline
324 int __qdf_get_cpu(void)
325 {
326 	return 0;
327 }
328 #endif
329 
330 static inline int __qdf_device_init_wakeup(__qdf_device_t qdf_dev, bool enable)
331 {
332 	return device_init_wakeup(qdf_dev->dev, enable);
333 }
334 
335 /**
336  * __qdf_get_totalramsize() -  Get total ram size in Kb
337  *
338  * Return: Total ram size in Kb
339  */
340 static inline uint64_t
341 __qdf_get_totalramsize(void)
342 {
343 	struct sysinfo meminfo;
344 
345 	si_meminfo(&meminfo);
346 	return MEMINFO_KB(meminfo.totalram);
347 }
348 
349 /**
350  * __qdf_get_lower_32_bits() - get lower 32 bits from an address.
351  * @addr: address
352  *
353  * This api returns the lower 32 bits of an address.
354  *
355  * Return: lower 32 bits.
356  */
357 static inline
358 uint32_t __qdf_get_lower_32_bits(__qdf_dma_addr_t addr)
359 {
360 	return lower_32_bits(addr);
361 }
362 
363 /**
364  * __qdf_get_upper_32_bits() - get upper 32 bits from an address.
365  * @addr: address
366  *
367  * This api returns the upper 32 bits of an address.
368  *
369  * Return: upper 32 bits.
370  */
371 static inline
372 uint32_t __qdf_get_upper_32_bits(__qdf_dma_addr_t addr)
373 {
374 	return upper_32_bits(addr);
375 }
376 
377 /**
378  * __qdf_rounddown_pow_of_two() - Round down to nearest power of two
379  * @n: number to be tested
380  *
381  * Test if the input number is power of two, and return the nearest power of two
382  *
383  * Return: number rounded down to the nearest power of two
384  */
385 static inline
386 unsigned long __qdf_rounddown_pow_of_two(unsigned long n)
387 {
388 	if (is_power_of_2(n))
389 		return n; /* already a power of 2 */
390 
391 	return __rounddown_pow_of_two(n);
392 }
393 
394 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0)
395 
396 /**
397  * __qdf_set_dma_coherent_mask() - set max number of bits allowed in dma addr
398  * @dev: device pointer
399  * @addr_bits: max number of bits allowed in dma address
400  *
401  * This API sets the maximum allowed number of bits in the dma address.
402  *
403  * Return: 0 - success, non zero - failure
404  */
405 static inline
406 int __qdf_set_dma_coherent_mask(struct device *dev, uint8_t addr_bits)
407 {
408 	return dma_set_mask_and_coherent(dev, DMA_BIT_MASK(addr_bits));
409 }
410 
411 #else
412 
413 /**
414  * __qdf_set_dma_coherent_mask() - set max number of bits allowed in dma addr
415  * @dev: device pointer
416  * @addr_bits: max number of bits allowed in dma address
417  *
418  * This API sets the maximum allowed number of bits in the dma address.
419  *
420  * Return: 0 - success, non zero - failure
421  */
422 static inline
423 int __qdf_set_dma_coherent_mask(struct device *dev, uint8_t addr_bits)
424 {
425 	return dma_set_coherent_mask(dev, DMA_BIT_MASK(addr_bits));
426 }
427 #endif
428 /**
429  * qdf_get_random_bytes() - returns nbytes bytes of random
430  * data
431  *
432  * Return: random bytes of data
433  */
434 static inline
435 void __qdf_get_random_bytes(void *buf, int nbytes)
436 {
437 	return get_random_bytes(buf, nbytes);
438 }
439 
440 /**
441  * __qdf_do_div() - wrapper function for kernel macro(do_div).
442  * @dividend: Dividend value
443  * @divisor : Divisor value
444  *
445  * Return: Quotient
446  */
447 static inline
448 uint64_t __qdf_do_div(uint64_t dividend, uint32_t divisor)
449 {
450 	do_div(dividend, divisor);
451 	/*do_div macro updates dividend with Quotient of dividend/divisor */
452 	return dividend;
453 }
454 
455 /**
456  * __qdf_do_div_rem() - wrapper function for kernel macro(do_div)
457  *                      to get remainder.
458  * @dividend: Dividend value
459  * @divisor : Divisor value
460  *
461  * Return: remainder
462  */
463 static inline
464 uint64_t __qdf_do_div_rem(uint64_t dividend, uint32_t divisor)
465 {
466 	return do_div(dividend, divisor);
467 }
468 #endif /*_I_QDF_UTIL_H*/
469