]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/nxge/include/xgehal-mm.h
This commit was generated by cvs2svn to compensate for changes in r171366,
[FreeBSD/FreeBSD.git] / sys / dev / nxge / include / xgehal-mm.h
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 /*
30  *  FileName :    xgehal-mm.h
31  *
32  *  Description:  memory pool object
33  *
34  *  Created:      28 May 2004
35  */
36
37 #ifndef XGE_HAL_MM_H
38 #define XGE_HAL_MM_H
39
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>
44
45 __EXTERN_BEGIN_DECLS
46
47 typedef void* xge_hal_mempool_h;
48
49 /*
50  * struct xge_hal_mempool_dma_t - Represents DMA objects passed to the
51  caller.
52  */
53 typedef struct xge_hal_mempool_dma_t {
54         dma_addr_t                      addr;
55         pci_dma_h                       handle;
56         pci_dma_acc_h                   acc_handle;
57 } xge_hal_mempool_dma_t;
58
59 /*
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.
66  *
67  * Memory pool allocation/deallocation callback.
68  */
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);
73
74 /*
75  * struct xge_hal_mempool_t - Memory pool.
76  */
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;
80         void                            *userdata;
81         void                            **memblocks_arr;
82         void                            **memblocks_priv_arr;
83         xge_hal_mempool_dma_t           *memblocks_dma_arr;
84         pci_dev_h                       pdev;
85         int                             memblock_size;
86         int                             memblocks_max;
87         int                             memblocks_allocated;
88         int                             item_size;
89         int                             items_max;
90         int                             items_initial;
91         int                             items_current;
92         int                             items_per_memblock;
93         void                            **items_arr;
94         void                            **shadow_items_arr;
95         int                             items_priv_size;
96 } xge_hal_mempool_t;
97
98 /*
99  * __hal_mempool_item - Returns pointer to the item in the mempool
100  * items array.
101  */
102 static inline void*
103 __hal_mempool_item(xge_hal_mempool_t *mempool, int index)
104 {
105         return mempool->items_arr[index];
106 }
107
108 /*
109  * __hal_mempool_item_priv - will return pointer on per item private space
110  */
111 static inline void*
112 __hal_mempool_item_priv(xge_hal_mempool_t *mempool, int memblock_idx,
113                         void *item, int *memblock_item_idx)
114 {
115         ptrdiff_t offset;
116         void *memblock = mempool->memblocks_arr[memblock_idx];
117
118         xge_assert(memblock);
119
120         offset = (int)((char * )item - (char *)memblock);
121         xge_assert(offset >= 0 && offset < mempool->memblock_size);
122
123         (*memblock_item_idx) = (int) offset / mempool->item_size;
124         xge_assert((*memblock_item_idx) < mempool->items_per_memblock);
125
126         return (char*)mempool->memblocks_priv_arr[memblock_idx] +
127                             (*memblock_item_idx) * mempool->items_priv_size;
128 }
129
130 /*
131  * __hal_mempool_items_arr - will return pointer to the items array in the
132  *  mempool.
133  */
134 static inline void*
135 __hal_mempool_items_arr(xge_hal_mempool_t *mempool)
136 {
137         return mempool->items_arr;
138 }
139
140 /*
141  * __hal_mempool_memblock - will return pointer to the memblock in the
142  *  mempool memblocks array.
143  */
144 static inline void*
145 __hal_mempool_memblock(xge_hal_mempool_t *mempool, int memblock_idx)
146 {
147         xge_assert(mempool->memblocks_arr[memblock_idx]);
148         return mempool->memblocks_arr[memblock_idx];
149 }
150
151 /*
152  * __hal_mempool_memblock_dma - will return pointer to the dma block
153  *  corresponds to the memblock(identified by memblock_idx) in the mempool.
154  */
155 static inline xge_hal_mempool_dma_t*
156 __hal_mempool_memblock_dma(xge_hal_mempool_t *mempool, int memblock_idx)
157 {
158         return mempool->memblocks_dma_arr + memblock_idx;
159 }
160
161 xge_hal_status_e __hal_mempool_grow(xge_hal_mempool_t *mempool,
162                         int num_allocate, int *num_allocated);
163
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);
168
169 void __hal_mempool_destroy(xge_hal_mempool_t *mempool);
170
171
172 __EXTERN_END_DECLS
173
174 #endif /* XGE_HAL_MM_H */