xgehal-mm.h revision 330897
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2002-2007 Neterion, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: stable/11/sys/dev/nxge/include/xgehal-mm.h 330897 2018-03-14 03:19:51Z eadler $
29 */
30
31#ifndef XGE_HAL_MM_H
32#define XGE_HAL_MM_H
33
34#include <dev/nxge/include/xge-os-pal.h>
35#include <dev/nxge/include/xge-debug.h>
36#include <dev/nxge/include/xgehal-types.h>
37#include <dev/nxge/include/xgehal-driver.h>
38
39__EXTERN_BEGIN_DECLS
40
41typedef void* xge_hal_mempool_h;
42
43/*
44 * struct xge_hal_mempool_dma_t - Represents DMA objects passed to the
45 caller.
46 */
47typedef struct xge_hal_mempool_dma_t {
48	dma_addr_t          addr;
49	pci_dma_h           handle;
50	pci_dma_acc_h           acc_handle;
51} xge_hal_mempool_dma_t;
52
53/*
54 * xge_hal_mempool_item_f  - Mempool item alloc/free callback
55 * @mempoolh: Memory pool handle.
56 * @item: Item that gets allocated or freed.
57 * @index: Item's index in the memory pool.
58 * @is_last: True, if this item is the last one in the pool; false - otherwise.
59 * userdat: Per-pool user context.
60 *
61 * Memory pool allocation/deallocation callback.
62 */
63typedef xge_hal_status_e (*xge_hal_mempool_item_f) (xge_hal_mempool_h mempoolh,
64	            void *memblock, int memblock_index,
65	            xge_hal_mempool_dma_t *dma_object, void *item,
66	            int index, int is_last, void *userdata);
67
68/*
69 * struct xge_hal_mempool_t - Memory pool.
70 */
71typedef struct xge_hal_mempool_t {
72	xge_hal_mempool_item_f      item_func_alloc;
73	xge_hal_mempool_item_f      item_func_free;
74	void                *userdata;
75	void                **memblocks_arr;
76	void                **memblocks_priv_arr;
77	xge_hal_mempool_dma_t       *memblocks_dma_arr;
78	pci_dev_h           pdev;
79	int             memblock_size;
80	int             memblocks_max;
81	int             memblocks_allocated;
82	int             item_size;
83	int             items_max;
84	int             items_initial;
85	int             items_current;
86	int             items_per_memblock;
87	void                **items_arr;
88	void                **shadow_items_arr;
89	int             items_priv_size;
90} xge_hal_mempool_t;
91
92/*
93 * __hal_mempool_item - Returns pointer to the item in the mempool
94 * items array.
95 */
96static inline void*
97__hal_mempool_item(xge_hal_mempool_t *mempool, int index)
98{
99	return mempool->items_arr[index];
100}
101
102/*
103 * __hal_mempool_item_priv - will return pointer on per item private space
104 */
105static inline void*
106__hal_mempool_item_priv(xge_hal_mempool_t *mempool, int memblock_idx,
107	        void *item, int *memblock_item_idx)
108{
109	ptrdiff_t offset;
110	void *memblock = mempool->memblocks_arr[memblock_idx];
111
112	xge_assert(memblock);
113
114	offset = (int)((char * )item - (char *)memblock);
115	xge_assert(offset >= 0 && offset < mempool->memblock_size);
116
117	(*memblock_item_idx) = (int) offset / mempool->item_size;
118	xge_assert((*memblock_item_idx) < mempool->items_per_memblock);
119
120	return (char*)mempool->memblocks_priv_arr[memblock_idx] +
121	            (*memblock_item_idx) * mempool->items_priv_size;
122}
123
124/*
125 * __hal_mempool_items_arr - will return pointer to the items array in the
126 *  mempool.
127 */
128static inline void*
129__hal_mempool_items_arr(xge_hal_mempool_t *mempool)
130{
131	return mempool->items_arr;
132}
133
134/*
135 * __hal_mempool_memblock - will return pointer to the memblock in the
136 *  mempool memblocks array.
137 */
138static inline void*
139__hal_mempool_memblock(xge_hal_mempool_t *mempool, int memblock_idx)
140{
141	xge_assert(mempool->memblocks_arr[memblock_idx]);
142	return mempool->memblocks_arr[memblock_idx];
143}
144
145/*
146 * __hal_mempool_memblock_dma - will return pointer to the dma block
147 *  corresponds to the memblock(identified by memblock_idx) in the mempool.
148 */
149static inline xge_hal_mempool_dma_t*
150__hal_mempool_memblock_dma(xge_hal_mempool_t *mempool, int memblock_idx)
151{
152	return mempool->memblocks_dma_arr + memblock_idx;
153}
154
155xge_hal_status_e __hal_mempool_grow(xge_hal_mempool_t *mempool,
156	        int num_allocate, int *num_allocated);
157
158xge_hal_mempool_t* __hal_mempool_create(pci_dev_h pdev, int memblock_size,
159	        int item_size, int private_size, int items_initial,
160	        int items_max, xge_hal_mempool_item_f item_func_alloc,
161	        xge_hal_mempool_item_f item_func_free, void *userdata);
162
163void __hal_mempool_destroy(xge_hal_mempool_t *mempool);
164
165
166__EXTERN_END_DECLS
167
168#endif /* XGE_HAL_MM_H */
169