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