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