xref: /illumos-gate/usr/src/uts/common/io/xge/hal/include/xgehal-mm.h (revision 9dd828891378a0a6a509ab601b4c5c20ca5562ec)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  *  Copyright (c) 2002-2005 Neterion, Inc.
24  *  All right Reserved.
25  *
26  *  FileName :    xgehal-mm.h
27  *
28  *  Description:  memory pool object
29  *
30  *  Created:      28 May 2004
31  */
32 
33 #ifndef XGE_HAL_MM_H
34 #define XGE_HAL_MM_H
35 
36 #include "xge-os-pal.h"
37 #include "xge-debug.h"
38 #include "xgehal-types.h"
39 #include "xgehal-driver.h"
40 
41 typedef void* xge_hal_mempool_h;
42 
43 /*
44  * struct xge_hal_mempool_dma_t - Represents DMA objects passed to the
45  caller.
46  */
47 typedef struct xge_hal_mempool_dma_t {
48 	dma_addr_t			addr;
49 	pci_dma_h			handle;
50 	pci_dma_acc_h			acc_handle;
51 } xge_hal_mempool_dma_t;
52 
53 /*
54  * xge_hal_mempool_item_f  - Mempool item alloc/free callback
55  * @mempoolh: Memory pool handle.
56  * @item: Item that gets allocated or freed.
57  * @index: Item's index in the memory pool.
58  * @is_last: True, if this item is the last one in the pool; false - otherwise.
59  * userdat: Per-pool user context.
60  *
61  * Memory pool allocation/deallocation callback.
62  */
63 typedef xge_hal_status_e (*xge_hal_mempool_item_f) (xge_hal_mempool_h mempoolh,
64 				void *memblock, int memblock_index,
65 				xge_hal_mempool_dma_t *dma_object, void	*item,
66 				int index, int is_last, void *userdata);
67 
68 /*
69  * struct xge_hal_mempool_t - Memory pool.
70  */
71 typedef struct xge_hal_mempool_t {
72 	xge_hal_mempool_item_f		item_func_alloc;
73 	xge_hal_mempool_item_f		item_func_free;
74 	void				*userdata;
75 	void				**memblocks_arr;
76 	void				**memblocks_priv_arr;
77 	xge_hal_mempool_dma_t		*memblocks_dma_arr;
78 	pci_dev_h			pdev;
79 	int				memblock_size;
80 	int				memblocks_max;
81 	int				memblocks_allocated;
82 	int				item_size;
83 	int				items_max;
84 	int				items_initial;
85 	int				items_current;
86 	int				items_per_memblock;
87 	void				**items_arr;
88 	void				**shadow_items_arr;
89 	int				items_priv_size;
90 } xge_hal_mempool_t;
91 
92 /*
93  * __hal_mempool_item - Returns pointer to the item in the mempool
94  * items array.
95  */
96 static inline void*
97 __hal_mempool_item(xge_hal_mempool_t *mempool, int index)
98 {
99 	return mempool->items_arr[index];
100 }
101 
102 /*
103  * __hal_mempool_item_priv - will return pointer on per item private space
104  */
105 static inline void*
106 __hal_mempool_item_priv(xge_hal_mempool_t *mempool, int memblock_idx,
107 			void *item, int *memblock_item_idx)
108 {
109 	ptrdiff_t offset;
110 	void *memblock = mempool->memblocks_arr[memblock_idx];
111 
112 	xge_assert(memblock);
113 
114 	offset = (int)((char * )item - (char *)memblock);
115 	xge_assert(offset >= 0 && offset < mempool->memblock_size);
116 
117 	(*memblock_item_idx) = (int) offset / mempool->item_size;
118 	xge_assert((*memblock_item_idx) < mempool->items_per_memblock);
119 
120 	return (char*)mempool->memblocks_priv_arr[memblock_idx] +
121 			    (*memblock_item_idx) * mempool->items_priv_size;
122 }
123 
124 /*
125  * __hal_mempool_items_arr - will return pointer to the items array in the
126  *  mempool.
127  */
128 static inline void*
129 __hal_mempool_items_arr(xge_hal_mempool_t *mempool)
130 {
131 	return mempool->items_arr;
132 }
133 
134 /*
135  * __hal_mempool_memblock - will return pointer to the memblock in the
136  *  mempool memblocks array.
137  */
138 static inline void*
139 __hal_mempool_memblock(xge_hal_mempool_t *mempool, int memblock_idx)
140 {
141 	xge_assert(mempool->memblocks_arr[memblock_idx]);
142 	return mempool->memblocks_arr[memblock_idx];
143 }
144 
145 /*
146  * __hal_mempool_memblock_dma - will return pointer to the dma block
147  *  corresponds to the memblock(identified by memblock_idx) in the mempool.
148  */
149 static inline xge_hal_mempool_dma_t*
150 __hal_mempool_memblock_dma(xge_hal_mempool_t *mempool, int memblock_idx)
151 {
152 	return mempool->memblocks_dma_arr + memblock_idx;
153 }
154 
155 xge_hal_status_e __hal_mempool_grow(xge_hal_mempool_t *mempool,
156 			int num_allocate, int *num_allocated);
157 
158 xge_hal_mempool_t* __hal_mempool_create(pci_dev_h pdev, int memblock_size,
159 			int item_size, int private_size, int items_initial,
160 			int items_max, xge_hal_mempool_item_f item_func_alloc,
161 			xge_hal_mempool_item_f item_func_free, void *userdata);
162 
163 void __hal_mempool_destroy(xge_hal_mempool_t *mempool);
164 
165 #endif /* XGE_HAL_MM_H */
166