1 /*-
2  * Copyright(c) 2002-2011 Exar Corp.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification are permitted provided the following conditions are met:
7  *
8  *    1. Redistributions of source code must retain the above copyright notice,
9  *       this list of conditions and the following disclaimer.
10  *
11  *    2. Redistributions in binary form must reproduce the above copyright
12  *       notice, this list of conditions and the following disclaimer in the
13  *       documentation and/or other materials provided with the distribution.
14  *
15  *    3. Neither the name of the Exar Corporation nor the names of its
16  *       contributors may be used to endorse or promote products derived from
17  *       this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 /*$FreeBSD: stable/9/sys/dev/vxge/vxgehal/vxgehal-mm.h 221167 2011-04-28 14:33:15Z gnn $*/
32 
33 #ifndef	VXGE_HAL_MM_H
34 #define	VXGE_HAL_MM_H
35 
36 __EXTERN_BEGIN_DECLS
37 
38 typedef void *vxge_hal_mempool_h;
39 
40 /*
41  * struct vxge_hal_mempool_dma_t - Represents DMA objects passed to the
42  * caller.
43  */
44 typedef struct vxge_hal_mempool_dma_t {
45 	dma_addr_t			addr;
46 	pci_dma_h			handle;
47 	pci_dma_acc_h			acc_handle;
48 } vxge_hal_mempool_dma_t;
49 
50 /*
51  * vxge_hal_mempool_item_f  - Mempool item alloc/free callback
52  * @mempoolh: Memory pool handle.
53  * @memblock: Address of memory block
54  * @memblock_index: Index of memory block
55  * @item: Item that gets allocated or freed.
56  * @index: Item's index in the memory pool.
57  * @is_last: True, if this item is the last one in the pool; false - otherwise.
58  * userdata: Per-pool user context.
59  *
60  * Memory pool allocation/deallocation callback.
61  */
62 typedef vxge_hal_status_e (*vxge_hal_mempool_item_f) (
63 	vxge_hal_mempool_h	mempoolh,
64 	void			*memblock,
65 	u32			memblock_index,
66 	vxge_hal_mempool_dma_t	*dma_object,
67 	void			*item,
68 	u32			index,
69 	u32			is_last,
70 	void			*userdata);
71 
72 /*
73  * struct vxge_hal_mempool_t - Memory pool.
74  */
75 typedef struct vxge_hal_mempool_t {
76 	vxge_hal_mempool_item_f	item_func_alloc;
77 	vxge_hal_mempool_item_f	item_func_free;
78 	void			*userdata;
79 	void			**memblocks_arr;
80 	void			**memblocks_priv_arr;
81 	vxge_hal_mempool_dma_t	*memblocks_dma_arr;
82 	vxge_hal_device_h	devh;
83 	u32			memblock_size;
84 	u32			memblocks_max;
85 	u32			memblocks_allocated;
86 	u32			item_size;
87 	u32			items_max;
88 	u32			items_initial;
89 	u32			items_current;
90 	u32			items_per_memblock;
91 	u32			dma_flags;
92 	void			**items_arr;
93 	void			**shadow_items_arr;
94 	u32			items_priv_size;
95 } vxge_hal_mempool_t;
96 
97 /*
98  * __hal_mempool_item_count - Returns number of items in the mempool
99  */
100 static inline u32
101 /* LINTED */
__hal_mempool_item_count(vxge_hal_mempool_t * mempool)102 __hal_mempool_item_count(
103     vxge_hal_mempool_t *mempool)
104 {
105 	return (mempool->items_current);
106 }
107 
108 /*
109  * __hal_mempool_item - Returns pointer to the item in the mempool
110  * items array.
111  */
112 static inline void *
113 /* LINTED */
__hal_mempool_item(vxge_hal_mempool_t * mempool,u32 items_index)114 __hal_mempool_item(
115     vxge_hal_mempool_t *mempool,
116     u32 items_index)
117 {
118 	return (mempool->items_arr[items_index]);
119 }
120 
121 /*
122  * __hal_mempool_item_priv - will return pointer on per item private space
123  */
124 static inline void*
125 /* LINTED */
__hal_mempool_item_priv(vxge_hal_mempool_t * mempool,u32 memblock_idx,void * item,u32 * memblock_item_idx)126 __hal_mempool_item_priv(
127     vxge_hal_mempool_t *mempool,
128     u32 memblock_idx,
129     void *item,
130     u32 *memblock_item_idx)
131 {
132 	ptrdiff_t offset;
133 	void *memblock = mempool->memblocks_arr[memblock_idx];
134 
135 	vxge_assert(memblock);
136 
137 	/* LINTED */
138 	offset = (u32) ((u8 *) item - (u8 *) memblock);
139 	vxge_assert(offset >= 0 && (u32) offset < mempool->memblock_size);
140 
141 	(*memblock_item_idx) = (u32) offset / mempool->item_size;
142 	vxge_assert((*memblock_item_idx) < mempool->items_per_memblock);
143 
144 	return ((u8 *) mempool->memblocks_priv_arr[memblock_idx] +
145 	    (*memblock_item_idx) * mempool->items_priv_size);
146 }
147 
148 /*
149  * __hal_mempool_items_arr - will return pointer to the items array in the
150  * mempool.
151  */
152 static inline void *
153 /* LINTED */
__hal_mempool_items_arr(vxge_hal_mempool_t * mempool)154 __hal_mempool_items_arr(
155     vxge_hal_mempool_t *mempool)
156 {
157 	return (mempool->items_arr);
158 }
159 
160 /*
161  * __hal_mempool_memblock - will return pointer to the memblock in the
162  * mempool memblocks array.
163  */
164 static inline void *
165 /* LINTED */
__hal_mempool_memblock(vxge_hal_mempool_t * mempool,u32 memblock_idx)166 __hal_mempool_memblock(
167     vxge_hal_mempool_t *mempool,
168     u32 memblock_idx)
169 {
170 	vxge_assert(mempool->memblocks_arr[memblock_idx]);
171 	return (mempool->memblocks_arr[memblock_idx]);
172 }
173 
174 /*
175  * __hal_mempool_memblock_dma - will return pointer to the dma block
176  * corresponds to the memblock(identified by memblock_idx) in the mempool.
177  */
178 static inline vxge_hal_mempool_dma_t *
179 /* LINTED */
__hal_mempool_memblock_dma(vxge_hal_mempool_t * mempool,u32 memblock_idx)180 __hal_mempool_memblock_dma(
181     vxge_hal_mempool_t *mempool,
182     u32 memblock_idx)
183 {
184 	return (mempool->memblocks_dma_arr + memblock_idx);
185 }
186 
187 vxge_hal_mempool_t *
188 vxge_hal_mempool_create(
189     vxge_hal_device_h devh,
190     u32 memblock_size,
191     u32 item_size,
192     u32 private_size,
193     u32 items_initial,
194     u32 items_max,
195     vxge_hal_mempool_item_f item_func_alloc,
196     vxge_hal_mempool_item_f item_func_free,
197     void *userdata);
198 
199 void
200 vxge_hal_mempool_destroy(
201     vxge_hal_mempool_t *mempool);
202 
203 
204 __EXTERN_END_DECLS
205 
206 #endif	/* VXGE_HAL_MM_H */
207