2 * Copyright (c) 2002-2007 Neterion, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * FileName : xgehal-mm.h
32 * Description: memory pool object
34 * Created: 28 May 2004
40 #include <dev/nxge/include/xge-os-pal.h>
41 #include <dev/nxge/include/xge-debug.h>
42 #include <dev/nxge/include/xgehal-types.h>
43 #include <dev/nxge/include/xgehal-driver.h>
47 typedef void* xge_hal_mempool_h;
50 * struct xge_hal_mempool_dma_t - Represents DMA objects passed to the
53 typedef struct xge_hal_mempool_dma_t {
56 pci_dma_acc_h acc_handle;
57 } xge_hal_mempool_dma_t;
60 * xge_hal_mempool_item_f - Mempool item alloc/free callback
61 * @mempoolh: Memory pool handle.
62 * @item: Item that gets allocated or freed.
63 * @index: Item's index in the memory pool.
64 * @is_last: True, if this item is the last one in the pool; false - otherwise.
65 * userdat: Per-pool user context.
67 * Memory pool allocation/deallocation callback.
69 typedef xge_hal_status_e (*xge_hal_mempool_item_f) (xge_hal_mempool_h mempoolh,
70 void *memblock, int memblock_index,
71 xge_hal_mempool_dma_t *dma_object, void *item,
72 int index, int is_last, void *userdata);
75 * struct xge_hal_mempool_t - Memory pool.
77 typedef struct xge_hal_mempool_t {
78 xge_hal_mempool_item_f item_func_alloc;
79 xge_hal_mempool_item_f item_func_free;
82 void **memblocks_priv_arr;
83 xge_hal_mempool_dma_t *memblocks_dma_arr;
87 int memblocks_allocated;
92 int items_per_memblock;
94 void **shadow_items_arr;
99 * __hal_mempool_item - Returns pointer to the item in the mempool
103 __hal_mempool_item(xge_hal_mempool_t *mempool, int index)
105 return mempool->items_arr[index];
109 * __hal_mempool_item_priv - will return pointer on per item private space
112 __hal_mempool_item_priv(xge_hal_mempool_t *mempool, int memblock_idx,
113 void *item, int *memblock_item_idx)
116 void *memblock = mempool->memblocks_arr[memblock_idx];
118 xge_assert(memblock);
120 offset = (int)((char * )item - (char *)memblock);
121 xge_assert(offset >= 0 && offset < mempool->memblock_size);
123 (*memblock_item_idx) = (int) offset / mempool->item_size;
124 xge_assert((*memblock_item_idx) < mempool->items_per_memblock);
126 return (char*)mempool->memblocks_priv_arr[memblock_idx] +
127 (*memblock_item_idx) * mempool->items_priv_size;
131 * __hal_mempool_items_arr - will return pointer to the items array in the
135 __hal_mempool_items_arr(xge_hal_mempool_t *mempool)
137 return mempool->items_arr;
141 * __hal_mempool_memblock - will return pointer to the memblock in the
142 * mempool memblocks array.
145 __hal_mempool_memblock(xge_hal_mempool_t *mempool, int memblock_idx)
147 xge_assert(mempool->memblocks_arr[memblock_idx]);
148 return mempool->memblocks_arr[memblock_idx];
152 * __hal_mempool_memblock_dma - will return pointer to the dma block
153 * corresponds to the memblock(identified by memblock_idx) in the mempool.
155 static inline xge_hal_mempool_dma_t*
156 __hal_mempool_memblock_dma(xge_hal_mempool_t *mempool, int memblock_idx)
158 return mempool->memblocks_dma_arr + memblock_idx;
161 xge_hal_status_e __hal_mempool_grow(xge_hal_mempool_t *mempool,
162 int num_allocate, int *num_allocated);
164 xge_hal_mempool_t* __hal_mempool_create(pci_dev_h pdev, int memblock_size,
165 int item_size, int private_size, int items_initial,
166 int items_max, xge_hal_mempool_item_f item_func_alloc,
167 xge_hal_mempool_item_f item_func_free, void *userdata);
169 void __hal_mempool_destroy(xge_hal_mempool_t *mempool);
174 #endif /* XGE_HAL_MM_H */