1171095Ssam/*-
2171095Ssam * Copyright (c) 2002-2007 Neterion, Inc.
3171095Ssam * All rights reserved.
4171095Ssam *
5171095Ssam * Redistribution and use in source and binary forms, with or without
6171095Ssam * modification, are permitted provided that the following conditions
7171095Ssam * are met:
8171095Ssam * 1. Redistributions of source code must retain the above copyright
9171095Ssam *    notice, this list of conditions and the following disclaimer.
10171095Ssam * 2. Redistributions in binary form must reproduce the above copyright
11171095Ssam *    notice, this list of conditions and the following disclaimer in the
12171095Ssam *    documentation and/or other materials provided with the distribution.
13171095Ssam *
14171095Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15171095Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16171095Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17171095Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18171095Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19171095Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20171095Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21171095Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22171095Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23171095Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24171095Ssam * SUCH DAMAGE.
25171095Ssam *
26171095Ssam * $FreeBSD$
27171095Ssam */
28171095Ssam
29171095Ssam#ifndef XGE_HAL_MM_H
30171095Ssam#define XGE_HAL_MM_H
31171095Ssam
32171095Ssam#include <dev/nxge/include/xge-os-pal.h>
33171095Ssam#include <dev/nxge/include/xge-debug.h>
34171095Ssam#include <dev/nxge/include/xgehal-types.h>
35171095Ssam#include <dev/nxge/include/xgehal-driver.h>
36171095Ssam
37171095Ssam__EXTERN_BEGIN_DECLS
38171095Ssam
39171095Ssamtypedef void* xge_hal_mempool_h;
40171095Ssam
41171095Ssam/*
42171095Ssam * struct xge_hal_mempool_dma_t - Represents DMA objects passed to the
43171095Ssam caller.
44171095Ssam */
45171095Ssamtypedef struct xge_hal_mempool_dma_t {
46173139Srwatson	dma_addr_t          addr;
47173139Srwatson	pci_dma_h           handle;
48173139Srwatson	pci_dma_acc_h           acc_handle;
49171095Ssam} xge_hal_mempool_dma_t;
50171095Ssam
51171095Ssam/*
52171095Ssam * xge_hal_mempool_item_f  - Mempool item alloc/free callback
53171095Ssam * @mempoolh: Memory pool handle.
54171095Ssam * @item: Item that gets allocated or freed.
55171095Ssam * @index: Item's index in the memory pool.
56171095Ssam * @is_last: True, if this item is the last one in the pool; false - otherwise.
57171095Ssam * userdat: Per-pool user context.
58171095Ssam *
59171095Ssam * Memory pool allocation/deallocation callback.
60171095Ssam */
61171095Ssamtypedef xge_hal_status_e (*xge_hal_mempool_item_f) (xge_hal_mempool_h mempoolh,
62173139Srwatson	            void *memblock, int memblock_index,
63173139Srwatson	            xge_hal_mempool_dma_t *dma_object, void *item,
64173139Srwatson	            int index, int is_last, void *userdata);
65171095Ssam
66171095Ssam/*
67171095Ssam * struct xge_hal_mempool_t - Memory pool.
68171095Ssam */
69171095Ssamtypedef struct xge_hal_mempool_t {
70173139Srwatson	xge_hal_mempool_item_f      item_func_alloc;
71173139Srwatson	xge_hal_mempool_item_f      item_func_free;
72173139Srwatson	void                *userdata;
73173139Srwatson	void                **memblocks_arr;
74173139Srwatson	void                **memblocks_priv_arr;
75173139Srwatson	xge_hal_mempool_dma_t       *memblocks_dma_arr;
76173139Srwatson	pci_dev_h           pdev;
77173139Srwatson	int             memblock_size;
78173139Srwatson	int             memblocks_max;
79173139Srwatson	int             memblocks_allocated;
80173139Srwatson	int             item_size;
81173139Srwatson	int             items_max;
82173139Srwatson	int             items_initial;
83173139Srwatson	int             items_current;
84173139Srwatson	int             items_per_memblock;
85173139Srwatson	void                **items_arr;
86173139Srwatson	void                **shadow_items_arr;
87173139Srwatson	int             items_priv_size;
88171095Ssam} xge_hal_mempool_t;
89171095Ssam
90171095Ssam/*
91171095Ssam * __hal_mempool_item - Returns pointer to the item in the mempool
92171095Ssam * items array.
93171095Ssam */
94171095Ssamstatic inline void*
95171095Ssam__hal_mempool_item(xge_hal_mempool_t *mempool, int index)
96171095Ssam{
97171095Ssam	return mempool->items_arr[index];
98171095Ssam}
99171095Ssam
100171095Ssam/*
101171095Ssam * __hal_mempool_item_priv - will return pointer on per item private space
102171095Ssam */
103171095Ssamstatic inline void*
104171095Ssam__hal_mempool_item_priv(xge_hal_mempool_t *mempool, int memblock_idx,
105173139Srwatson	        void *item, int *memblock_item_idx)
106171095Ssam{
107171095Ssam	ptrdiff_t offset;
108171095Ssam	void *memblock = mempool->memblocks_arr[memblock_idx];
109171095Ssam
110171095Ssam	xge_assert(memblock);
111171095Ssam
112171095Ssam	offset = (int)((char * )item - (char *)memblock);
113171095Ssam	xge_assert(offset >= 0 && offset < mempool->memblock_size);
114171095Ssam
115171095Ssam	(*memblock_item_idx) = (int) offset / mempool->item_size;
116171095Ssam	xge_assert((*memblock_item_idx) < mempool->items_per_memblock);
117171095Ssam
118171095Ssam	return (char*)mempool->memblocks_priv_arr[memblock_idx] +
119173139Srwatson	            (*memblock_item_idx) * mempool->items_priv_size;
120171095Ssam}
121171095Ssam
122171095Ssam/*
123171095Ssam * __hal_mempool_items_arr - will return pointer to the items array in the
124171095Ssam *  mempool.
125171095Ssam */
126171095Ssamstatic inline void*
127171095Ssam__hal_mempool_items_arr(xge_hal_mempool_t *mempool)
128171095Ssam{
129171095Ssam	return mempool->items_arr;
130171095Ssam}
131171095Ssam
132171095Ssam/*
133171095Ssam * __hal_mempool_memblock - will return pointer to the memblock in the
134171095Ssam *  mempool memblocks array.
135171095Ssam */
136171095Ssamstatic inline void*
137171095Ssam__hal_mempool_memblock(xge_hal_mempool_t *mempool, int memblock_idx)
138171095Ssam{
139171095Ssam	xge_assert(mempool->memblocks_arr[memblock_idx]);
140171095Ssam	return mempool->memblocks_arr[memblock_idx];
141171095Ssam}
142171095Ssam
143171095Ssam/*
144171095Ssam * __hal_mempool_memblock_dma - will return pointer to the dma block
145171095Ssam *  corresponds to the memblock(identified by memblock_idx) in the mempool.
146171095Ssam */
147171095Ssamstatic inline xge_hal_mempool_dma_t*
148171095Ssam__hal_mempool_memblock_dma(xge_hal_mempool_t *mempool, int memblock_idx)
149171095Ssam{
150171095Ssam	return mempool->memblocks_dma_arr + memblock_idx;
151171095Ssam}
152171095Ssam
153171095Ssamxge_hal_status_e __hal_mempool_grow(xge_hal_mempool_t *mempool,
154173139Srwatson	        int num_allocate, int *num_allocated);
155171095Ssam
156171095Ssamxge_hal_mempool_t* __hal_mempool_create(pci_dev_h pdev, int memblock_size,
157173139Srwatson	        int item_size, int private_size, int items_initial,
158173139Srwatson	        int items_max, xge_hal_mempool_item_f item_func_alloc,
159173139Srwatson	        xge_hal_mempool_item_f item_func_free, void *userdata);
160171095Ssam
161171095Ssamvoid __hal_mempool_destroy(xge_hal_mempool_t *mempool);
162171095Ssam
163171095Ssam
164171095Ssam__EXTERN_END_DECLS
165171095Ssam
166171095Ssam#endif /* XGE_HAL_MM_H */
167