xref: /wlan-dirver/qca-wifi-host-cmn/qdf/linux/src/i_qdf_mem.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_mem.h
30  * Linux-specific definitions for QDF memory API's
31  */
32 
33 #ifndef __I_QDF_MEM_H
34 #define __I_QDF_MEM_H
35 
36 #ifdef __KERNEL__
37 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 17)
38 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33)
39 #include <linux/autoconf.h>
40 #else
41 #include <generated/autoconf.h>
42 #endif
43 #endif
44 #include <linux/slab.h>
45 #include <linux/hardirq.h>
46 #include <linux/vmalloc.h>
47 #include <linux/pci.h> /* pci_alloc_consistent */
48 #include <linux/cache.h> /* L1_CACHE_BYTES */
49 
50 #define __qdf_cache_line_sz L1_CACHE_BYTES
51 #if CONFIG_MCL
52 #include <cds_queue.h>
53 #else
54 #include <sys/queue.h>
55 #endif
56 #else
57 /*
58  * Provide dummy defs for kernel data types, functions, and enums
59  * used in this header file.
60  */
61 #define GFP_KERNEL 0
62 #define GFP_ATOMIC 0
63 #define kzalloc(size, flags) NULL
64 #define vmalloc(size)        NULL
65 #define kfree(buf)
66 #define vfree(buf)
67 #define pci_alloc_consistent(dev, size, paddr) NULL
68 #define __qdf_mempool_t void*
69 #define QDF_RET_IP NULL
70 #endif /* __KERNEL__ */
71 #include <qdf_status.h>
72 
73 #ifdef __KERNEL__
74 typedef struct mempool_elem {
75 	STAILQ_ENTRY(mempool_elem) mempool_entry;
76 } mempool_elem_t;
77 
78 /**
79  * typedef __qdf_mempool_ctxt_t - Memory pool context
80  * @pool_id: pool identifier
81  * @flags: flags
82  * @elem_size: size of each pool element in bytes
83  * @pool_mem: pool_addr address of the pool created
84  * @mem_size: Total size of the pool in bytes
85  * @free_list: free pool list
86  * @lock: spinlock object
87  * @max_elem: Maximum number of elements in tha pool
88  * @free_cnt: Number of free elements available
89  */
90 typedef struct __qdf_mempool_ctxt {
91 	int pool_id;
92 	u_int32_t flags;
93 	size_t elem_size;
94 	void *pool_mem;
95 	u_int32_t mem_size;
96 
97 	STAILQ_HEAD(, mempool_elem) free_list;
98 	spinlock_t lock;
99 	u_int32_t max_elem;
100 	u_int32_t free_cnt;
101 } __qdf_mempool_ctxt_t;
102 
103 #endif /* __KERNEL__ */
104 
105 #define __qdf_align(a, mask) ALIGN(a, mask)
106 
107 /* typedef for dma_data_direction */
108 typedef enum dma_data_direction __dma_data_direction;
109 
110 /**
111  * __qdf_dma_dir_to_os() - Convert DMA data direction to OS specific enum
112  * @dir: QDF DMA data direction
113  *
114  * Return:
115  * enum dma_data_direction
116  */
117 static inline
118 enum dma_data_direction __qdf_dma_dir_to_os(qdf_dma_dir_t qdf_dir)
119 {
120 	switch (qdf_dir) {
121 	case QDF_DMA_BIDIRECTIONAL:
122 		return DMA_BIDIRECTIONAL;
123 	case QDF_DMA_TO_DEVICE:
124 		return DMA_TO_DEVICE;
125 	case QDF_DMA_FROM_DEVICE:
126 		return DMA_FROM_DEVICE;
127 	default:
128 		return DMA_NONE;
129 	}
130 }
131 
132 
133 /**
134  * __qdf_mem_map_nbytes_single - Map memory for DMA
135  * @osdev: pomter OS device context
136  * @buf: pointer to memory to be dma mapped
137  * @dir: DMA map direction
138  * @nbytes: number of bytes to be mapped.
139  * @phy_addr: ponter to recive physical address.
140  *
141  * Return: success/failure
142  */
143 static inline uint32_t __qdf_mem_map_nbytes_single(qdf_device_t osdev,
144 						  void *buf, qdf_dma_dir_t dir,
145 						  int nbytes,
146 						  qdf_dma_addr_t *phy_addr)
147 {
148 	/* assume that the OS only provides a single fragment */
149 	*phy_addr = dma_map_single(osdev->dev, buf, nbytes,
150 					__qdf_dma_dir_to_os(dir));
151 	return dma_mapping_error(osdev->dev, *phy_addr) ?
152 	QDF_STATUS_E_FAILURE : QDF_STATUS_SUCCESS;
153 }
154 
155 /**
156  * __qdf_mem_unmap_nbytes_single() - un_map memory for DMA
157  *
158  * @osdev: pomter OS device context
159  * @phy_addr: physical address of memory to be dma unmapped
160  * @dir: DMA unmap direction
161  * @nbytes: number of bytes to be unmapped.
162  *
163  * @return - none
164  */
165 static inline void __qdf_mem_unmap_nbytes_single(qdf_device_t osdev,
166 						 qdf_dma_addr_t phy_addr,
167 						 qdf_dma_dir_t dir, int nbytes)
168 {
169 	dma_unmap_single(osdev->dev, phy_addr, nbytes,
170 				__qdf_dma_dir_to_os(dir));
171 }
172 #ifdef __KERNEL__
173 
174 typedef __qdf_mempool_ctxt_t *__qdf_mempool_t;
175 
176 int __qdf_mempool_init(qdf_device_t osdev, __qdf_mempool_t *pool, int pool_cnt,
177 		       size_t pool_entry_size, u_int32_t flags);
178 void __qdf_mempool_destroy(qdf_device_t osdev, __qdf_mempool_t pool);
179 void *__qdf_mempool_alloc(qdf_device_t osdev, __qdf_mempool_t pool);
180 void __qdf_mempool_free(qdf_device_t osdev, __qdf_mempool_t pool, void *buf);
181 #define QDF_RET_IP ((void *)_RET_IP_)
182 
183 #define __qdf_mempool_elem_size(_pool) ((_pool)->elem_size)
184 #endif
185 
186 /**
187  * __qdf_mem_cmp() - memory compare
188  * @memory1: pointer to one location in memory to compare.
189  * @memory2: pointer to second location in memory to compare.
190  * @num_bytes: the number of bytes to compare.
191  *
192  * Function to compare two pieces of memory, similar to memcmp function
193  * in standard C.
194  * Return:
195  * int32_t - returns an int value that tells if the memory
196  * locations are equal or not equal.
197  * 0 -- equal
198  * < 0 -- *memory1 is less than *memory2
199  * > 0 -- *memory1 is bigger than *memory2
200  */
201 static inline int32_t __qdf_mem_cmp(const void *memory1, const void *memory2,
202 				    uint32_t num_bytes)
203 {
204 	return (int32_t) memcmp(memory1, memory2, num_bytes);
205 }
206 
207 #endif /* __I_QDF_MEM_H */
208