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