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 * Copyright (c) 2002-2006 Neterion, Inc.
22 */
23
24#ifndef XGE_HAL_MM_H
25#define XGE_HAL_MM_H
26
27#include "xge-os-pal.h"
28#include "xge-debug.h"
29#include "xgehal-types.h"
30#include "xgehal-driver.h"
31
32__EXTERN_BEGIN_DECLS
33
34typedef void* xge_hal_mempool_h;
35
36/*
37 * struct xge_hal_mempool_dma_t - Represents DMA objects passed to the
38 caller.
39 */
40typedef struct xge_hal_mempool_dma_t {
41	dma_addr_t			addr;
42	pci_dma_h			handle;
43	pci_dma_acc_h			acc_handle;
44} xge_hal_mempool_dma_t;
45
46/*
47 * xge_hal_mempool_item_f  - Mempool item alloc/free callback
48 * @mempoolh: Memory pool handle.
49 * @item: Item that gets allocated or freed.
50 * @index: Item's index in the memory pool.
51 * @is_last: True, if this item is the last one in the pool; false - otherwise.
52 * userdat: Per-pool user context.
53 *
54 * Memory pool allocation/deallocation callback.
55 */
56typedef xge_hal_status_e (*xge_hal_mempool_item_f) (xge_hal_mempool_h mempoolh,
57				void *memblock, int memblock_index,
58				xge_hal_mempool_dma_t *dma_object, void	*item,
59				int index, int is_last, void *userdata);
60
61/*
62 * struct xge_hal_mempool_t - Memory pool.
63 */
64typedef struct xge_hal_mempool_t {
65	xge_hal_mempool_item_f		item_func_alloc;
66	xge_hal_mempool_item_f		item_func_free;
67	void				*userdata;
68	void				**memblocks_arr;
69	void				**memblocks_priv_arr;
70	xge_hal_mempool_dma_t		*memblocks_dma_arr;
71	pci_dev_h			pdev;
72	int				memblock_size;
73	int				memblocks_max;
74	int				memblocks_allocated;
75	int				item_size;
76	int				items_max;
77	int				items_initial;
78	int				items_current;
79	int				items_per_memblock;
80	void				**items_arr;
81	void				**shadow_items_arr;
82	int				items_priv_size;
83} xge_hal_mempool_t;
84
85/*
86 * __hal_mempool_item - Returns pointer to the item in the mempool
87 * items array.
88 */
89static inline void*
90__hal_mempool_item(xge_hal_mempool_t *mempool, int index)
91{
92	return mempool->items_arr[index];
93}
94
95/*
96 * __hal_mempool_item_priv - will return pointer on per item private space
97 */
98static inline void*
99__hal_mempool_item_priv(xge_hal_mempool_t *mempool, int memblock_idx,
100			void *item, int *memblock_item_idx)
101{
102	ptrdiff_t offset;
103	void *memblock = mempool->memblocks_arr[memblock_idx];
104
105	xge_assert(memblock);
106
107	offset = (int)((char * )item - (char *)memblock);
108	xge_assert(offset >= 0 && offset < mempool->memblock_size);
109
110	(*memblock_item_idx) = (int) offset / mempool->item_size;
111	xge_assert((*memblock_item_idx) < mempool->items_per_memblock);
112
113	return (char*)mempool->memblocks_priv_arr[memblock_idx] +
114			    (*memblock_item_idx) * mempool->items_priv_size;
115}
116
117/*
118 * __hal_mempool_items_arr - will return pointer to the items array in the
119 *  mempool.
120 */
121static inline void*
122__hal_mempool_items_arr(xge_hal_mempool_t *mempool)
123{
124	return mempool->items_arr;
125}
126
127/*
128 * __hal_mempool_memblock - will return pointer to the memblock in the
129 *  mempool memblocks array.
130 */
131static inline void*
132__hal_mempool_memblock(xge_hal_mempool_t *mempool, int memblock_idx)
133{
134	xge_assert(mempool->memblocks_arr[memblock_idx]);
135	return mempool->memblocks_arr[memblock_idx];
136}
137
138/*
139 * __hal_mempool_memblock_dma - will return pointer to the dma block
140 *  corresponds to the memblock(identified by memblock_idx) in the mempool.
141 */
142static inline xge_hal_mempool_dma_t*
143__hal_mempool_memblock_dma(xge_hal_mempool_t *mempool, int memblock_idx)
144{
145	return mempool->memblocks_dma_arr + memblock_idx;
146}
147
148xge_hal_status_e __hal_mempool_grow(xge_hal_mempool_t *mempool,
149			int num_allocate, int *num_allocated);
150
151xge_hal_mempool_t* __hal_mempool_create(pci_dev_h pdev, int memblock_size,
152			int item_size, int private_size, int items_initial,
153			int items_max, xge_hal_mempool_item_f item_func_alloc,
154			xge_hal_mempool_item_f item_func_free, void *userdata);
155
156void __hal_mempool_destroy(xge_hal_mempool_t *mempool);
157
158
159__EXTERN_END_DECLS
160
161#endif /* XGE_HAL_MM_H */
162