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