xref: /freebsd-14.2/sys/dev/netmap/netmap_mem2.c (revision 4bf50f18)
1ccdc3305SLuigi Rizzo /*
217885a7bSLuigi Rizzo  * Copyright (C) 2012-2014 Matteo Landi, Luigi Rizzo, Giuseppe Lettieri. All rights reserved.
3ccdc3305SLuigi Rizzo  *
4ccdc3305SLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
5ccdc3305SLuigi Rizzo  * modification, are permitted provided that the following conditions
6ccdc3305SLuigi Rizzo  * are met:
7ccdc3305SLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
8ccdc3305SLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
9ccdc3305SLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
10ccdc3305SLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
11ccdc3305SLuigi Rizzo  *      documentation and/or other materials provided with the distribution.
12ccdc3305SLuigi Rizzo  *
13ccdc3305SLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14ccdc3305SLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15ccdc3305SLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16ccdc3305SLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17ccdc3305SLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18ccdc3305SLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19ccdc3305SLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20ccdc3305SLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21ccdc3305SLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22ccdc3305SLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23ccdc3305SLuigi Rizzo  * SUCH DAMAGE.
24ccdc3305SLuigi Rizzo  */
25ccdc3305SLuigi Rizzo 
268241616dSLuigi Rizzo #ifdef linux
27ce3ee1e7SLuigi Rizzo #include "bsd_glue.h"
288241616dSLuigi Rizzo #endif /* linux */
298241616dSLuigi Rizzo 
30ce3ee1e7SLuigi Rizzo #ifdef __APPLE__
31ce3ee1e7SLuigi Rizzo #include "osx_glue.h"
32ce3ee1e7SLuigi Rizzo #endif /* __APPLE__ */
338241616dSLuigi Rizzo 
34ce3ee1e7SLuigi Rizzo #ifdef __FreeBSD__
35ce3ee1e7SLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */
36ce3ee1e7SLuigi Rizzo __FBSDID("$FreeBSD$");
378241616dSLuigi Rizzo 
38ce3ee1e7SLuigi Rizzo #include <sys/types.h>
39ce3ee1e7SLuigi Rizzo #include <sys/malloc.h>
40ce3ee1e7SLuigi Rizzo #include <sys/proc.h>
41ce3ee1e7SLuigi Rizzo #include <vm/vm.h>	/* vtophys */
42ce3ee1e7SLuigi Rizzo #include <vm/pmap.h>	/* vtophys */
43ce3ee1e7SLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
44ce3ee1e7SLuigi Rizzo #include <sys/selinfo.h>
45ce3ee1e7SLuigi Rizzo #include <sys/sysctl.h>
46ce3ee1e7SLuigi Rizzo #include <net/if.h>
47ce3ee1e7SLuigi Rizzo #include <net/if_var.h>
48ce3ee1e7SLuigi Rizzo #include <net/vnet.h>
49ce3ee1e7SLuigi Rizzo #include <machine/bus.h>	/* bus_dmamap_* */
50ce3ee1e7SLuigi Rizzo 
51ce3ee1e7SLuigi Rizzo #endif /* __FreeBSD__ */
52ce3ee1e7SLuigi Rizzo 
53ce3ee1e7SLuigi Rizzo #include <net/netmap.h>
54ce3ee1e7SLuigi Rizzo #include <dev/netmap/netmap_kern.h>
55ce3ee1e7SLuigi Rizzo #include "netmap_mem2.h"
56ce3ee1e7SLuigi Rizzo 
57*4bf50f18SLuigi Rizzo #define NETMAP_BUF_MAX_NUM	20*4096*2	/* large machine */
58*4bf50f18SLuigi Rizzo 
59*4bf50f18SLuigi Rizzo #define NETMAP_POOL_MAX_NAMSZ	32
60*4bf50f18SLuigi Rizzo 
61*4bf50f18SLuigi Rizzo 
62*4bf50f18SLuigi Rizzo enum {
63*4bf50f18SLuigi Rizzo 	NETMAP_IF_POOL   = 0,
64*4bf50f18SLuigi Rizzo 	NETMAP_RING_POOL,
65*4bf50f18SLuigi Rizzo 	NETMAP_BUF_POOL,
66*4bf50f18SLuigi Rizzo 	NETMAP_POOLS_NR
67*4bf50f18SLuigi Rizzo };
68*4bf50f18SLuigi Rizzo 
69*4bf50f18SLuigi Rizzo 
70*4bf50f18SLuigi Rizzo struct netmap_obj_params {
71*4bf50f18SLuigi Rizzo 	u_int size;
72*4bf50f18SLuigi Rizzo 	u_int num;
73*4bf50f18SLuigi Rizzo };
74*4bf50f18SLuigi Rizzo struct netmap_obj_pool {
75*4bf50f18SLuigi Rizzo 	char name[NETMAP_POOL_MAX_NAMSZ];	/* name of the allocator */
76*4bf50f18SLuigi Rizzo 
77*4bf50f18SLuigi Rizzo 	/* ---------------------------------------------------*/
78*4bf50f18SLuigi Rizzo 	/* these are only meaningful if the pool is finalized */
79*4bf50f18SLuigi Rizzo 	/* (see 'finalized' field in netmap_mem_d)            */
80*4bf50f18SLuigi Rizzo 	u_int objtotal;         /* actual total number of objects. */
81*4bf50f18SLuigi Rizzo 	u_int memtotal;		/* actual total memory space */
82*4bf50f18SLuigi Rizzo 	u_int numclusters;	/* actual number of clusters */
83*4bf50f18SLuigi Rizzo 
84*4bf50f18SLuigi Rizzo 	u_int objfree;          /* number of free objects. */
85*4bf50f18SLuigi Rizzo 
86*4bf50f18SLuigi Rizzo 	struct lut_entry *lut;  /* virt,phys addresses, objtotal entries */
87*4bf50f18SLuigi Rizzo 	uint32_t *bitmap;       /* one bit per buffer, 1 means free */
88*4bf50f18SLuigi Rizzo 	uint32_t bitmap_slots;	/* number of uint32 entries in bitmap */
89*4bf50f18SLuigi Rizzo 	/* ---------------------------------------------------*/
90*4bf50f18SLuigi Rizzo 
91*4bf50f18SLuigi Rizzo 	/* limits */
92*4bf50f18SLuigi Rizzo 	u_int objminsize;	/* minimum object size */
93*4bf50f18SLuigi Rizzo 	u_int objmaxsize;	/* maximum object size */
94*4bf50f18SLuigi Rizzo 	u_int nummin;		/* minimum number of objects */
95*4bf50f18SLuigi Rizzo 	u_int nummax;		/* maximum number of objects */
96*4bf50f18SLuigi Rizzo 
97*4bf50f18SLuigi Rizzo 	/* these are changed only by config */
98*4bf50f18SLuigi Rizzo 	u_int _objtotal;	/* total number of objects */
99*4bf50f18SLuigi Rizzo 	u_int _objsize;		/* object size */
100*4bf50f18SLuigi Rizzo 	u_int _clustsize;       /* cluster size */
101*4bf50f18SLuigi Rizzo 	u_int _clustentries;    /* objects per cluster */
102*4bf50f18SLuigi Rizzo 	u_int _numclusters;	/* number of clusters */
103*4bf50f18SLuigi Rizzo 
104*4bf50f18SLuigi Rizzo 	/* requested values */
105*4bf50f18SLuigi Rizzo 	u_int r_objtotal;
106*4bf50f18SLuigi Rizzo 	u_int r_objsize;
107*4bf50f18SLuigi Rizzo };
108*4bf50f18SLuigi Rizzo 
109*4bf50f18SLuigi Rizzo #ifdef linux
110*4bf50f18SLuigi Rizzo // XXX a mtx would suffice here 20130415 lr
111*4bf50f18SLuigi Rizzo #define NMA_LOCK_T		struct semaphore
112*4bf50f18SLuigi Rizzo #else /* !linux */
113*4bf50f18SLuigi Rizzo #define NMA_LOCK_T		struct mtx
114*4bf50f18SLuigi Rizzo #endif /* linux */
115*4bf50f18SLuigi Rizzo 
116*4bf50f18SLuigi Rizzo typedef int (*netmap_mem_config_t)(struct netmap_mem_d*);
117*4bf50f18SLuigi Rizzo typedef int (*netmap_mem_finalize_t)(struct netmap_mem_d*);
118*4bf50f18SLuigi Rizzo typedef void (*netmap_mem_deref_t)(struct netmap_mem_d*);
119*4bf50f18SLuigi Rizzo 
120*4bf50f18SLuigi Rizzo typedef uint16_t nm_memid_t;
121*4bf50f18SLuigi Rizzo 
122*4bf50f18SLuigi Rizzo struct netmap_mem_d {
123*4bf50f18SLuigi Rizzo 	NMA_LOCK_T nm_mtx;  /* protect the allocator */
124*4bf50f18SLuigi Rizzo 	u_int nm_totalsize; /* shorthand */
125*4bf50f18SLuigi Rizzo 
126*4bf50f18SLuigi Rizzo 	u_int flags;
127*4bf50f18SLuigi Rizzo #define NETMAP_MEM_FINALIZED	0x1	/* preallocation done */
128*4bf50f18SLuigi Rizzo 	int lasterr;		/* last error for curr config */
129*4bf50f18SLuigi Rizzo 	int refcount;		/* existing priv structures */
130*4bf50f18SLuigi Rizzo 	/* the three allocators */
131*4bf50f18SLuigi Rizzo 	struct netmap_obj_pool pools[NETMAP_POOLS_NR];
132*4bf50f18SLuigi Rizzo 
133*4bf50f18SLuigi Rizzo 	netmap_mem_config_t   config;
134*4bf50f18SLuigi Rizzo 	netmap_mem_finalize_t finalize;
135*4bf50f18SLuigi Rizzo 	netmap_mem_deref_t    deref;
136*4bf50f18SLuigi Rizzo 
137*4bf50f18SLuigi Rizzo 	nm_memid_t nm_id;	/* allocator identifier */
138*4bf50f18SLuigi Rizzo 	int nm_grp;	/* iommu groupd id */
139*4bf50f18SLuigi Rizzo 
140*4bf50f18SLuigi Rizzo 	/* list of all existing allocators, sorted by nm_id */
141*4bf50f18SLuigi Rizzo 	struct netmap_mem_d *prev, *next;
142*4bf50f18SLuigi Rizzo };
143*4bf50f18SLuigi Rizzo 
144*4bf50f18SLuigi Rizzo /* accessor functions */
145*4bf50f18SLuigi Rizzo struct lut_entry*
146*4bf50f18SLuigi Rizzo netmap_mem_get_lut(struct netmap_mem_d *nmd)
147*4bf50f18SLuigi Rizzo {
148*4bf50f18SLuigi Rizzo 	return nmd->pools[NETMAP_BUF_POOL].lut;
149*4bf50f18SLuigi Rizzo }
150*4bf50f18SLuigi Rizzo 
151*4bf50f18SLuigi Rizzo u_int
152*4bf50f18SLuigi Rizzo netmap_mem_get_buftotal(struct netmap_mem_d *nmd)
153*4bf50f18SLuigi Rizzo {
154*4bf50f18SLuigi Rizzo 	return nmd->pools[NETMAP_BUF_POOL].objtotal;
155*4bf50f18SLuigi Rizzo }
156*4bf50f18SLuigi Rizzo 
157*4bf50f18SLuigi Rizzo size_t
158*4bf50f18SLuigi Rizzo netmap_mem_get_bufsize(struct netmap_mem_d *nmd)
159*4bf50f18SLuigi Rizzo {
160*4bf50f18SLuigi Rizzo 	return nmd->pools[NETMAP_BUF_POOL]._objsize;
161*4bf50f18SLuigi Rizzo }
162*4bf50f18SLuigi Rizzo 
163ce3ee1e7SLuigi Rizzo #ifdef linux
164ce3ee1e7SLuigi Rizzo #define NMA_LOCK_INIT(n)	sema_init(&(n)->nm_mtx, 1)
165ce3ee1e7SLuigi Rizzo #define NMA_LOCK_DESTROY(n)
166ce3ee1e7SLuigi Rizzo #define NMA_LOCK(n)		down(&(n)->nm_mtx)
167ce3ee1e7SLuigi Rizzo #define NMA_UNLOCK(n)		up(&(n)->nm_mtx)
168ce3ee1e7SLuigi Rizzo #else /* !linux */
169ce3ee1e7SLuigi Rizzo #define NMA_LOCK_INIT(n)	mtx_init(&(n)->nm_mtx, "netmap memory allocator lock", NULL, MTX_DEF)
170ce3ee1e7SLuigi Rizzo #define NMA_LOCK_DESTROY(n)	mtx_destroy(&(n)->nm_mtx)
171ce3ee1e7SLuigi Rizzo #define NMA_LOCK(n)		mtx_lock(&(n)->nm_mtx)
172ce3ee1e7SLuigi Rizzo #define NMA_UNLOCK(n)		mtx_unlock(&(n)->nm_mtx)
173ce3ee1e7SLuigi Rizzo #endif /* linux */
1748241616dSLuigi Rizzo 
1758241616dSLuigi Rizzo 
1768241616dSLuigi Rizzo struct netmap_obj_params netmap_params[NETMAP_POOLS_NR] = {
1778241616dSLuigi Rizzo 	[NETMAP_IF_POOL] = {
1788241616dSLuigi Rizzo 		.size = 1024,
1798241616dSLuigi Rizzo 		.num  = 100,
1808241616dSLuigi Rizzo 	},
1818241616dSLuigi Rizzo 	[NETMAP_RING_POOL] = {
1828241616dSLuigi Rizzo 		.size = 9*PAGE_SIZE,
1838241616dSLuigi Rizzo 		.num  = 200,
1848241616dSLuigi Rizzo 	},
1858241616dSLuigi Rizzo 	[NETMAP_BUF_POOL] = {
1868241616dSLuigi Rizzo 		.size = 2048,
1878241616dSLuigi Rizzo 		.num  = NETMAP_BUF_MAX_NUM,
1888241616dSLuigi Rizzo 	},
1898241616dSLuigi Rizzo };
1908241616dSLuigi Rizzo 
191f0ea3689SLuigi Rizzo struct netmap_obj_params netmap_min_priv_params[NETMAP_POOLS_NR] = {
192f0ea3689SLuigi Rizzo 	[NETMAP_IF_POOL] = {
193f0ea3689SLuigi Rizzo 		.size = 1024,
194f0ea3689SLuigi Rizzo 		.num  = 1,
195f0ea3689SLuigi Rizzo 	},
196f0ea3689SLuigi Rizzo 	[NETMAP_RING_POOL] = {
197f0ea3689SLuigi Rizzo 		.size = 5*PAGE_SIZE,
198f0ea3689SLuigi Rizzo 		.num  = 4,
199f0ea3689SLuigi Rizzo 	},
200f0ea3689SLuigi Rizzo 	[NETMAP_BUF_POOL] = {
201f0ea3689SLuigi Rizzo 		.size = 2048,
202f0ea3689SLuigi Rizzo 		.num  = 4098,
203f0ea3689SLuigi Rizzo 	},
204f0ea3689SLuigi Rizzo };
205f0ea3689SLuigi Rizzo 
206ccdc3305SLuigi Rizzo 
2072579e2d7SLuigi Rizzo /*
2082579e2d7SLuigi Rizzo  * nm_mem is the memory allocator used for all physical interfaces
2092579e2d7SLuigi Rizzo  * running in netmap mode.
2102579e2d7SLuigi Rizzo  * Virtual (VALE) ports will have each its own allocator.
2112579e2d7SLuigi Rizzo  */
212ce3ee1e7SLuigi Rizzo static int netmap_mem_global_config(struct netmap_mem_d *nmd);
213ce3ee1e7SLuigi Rizzo static int netmap_mem_global_finalize(struct netmap_mem_d *nmd);
214ce3ee1e7SLuigi Rizzo static void netmap_mem_global_deref(struct netmap_mem_d *nmd);
215ce3ee1e7SLuigi Rizzo struct netmap_mem_d nm_mem = {	/* Our memory allocator. */
2168241616dSLuigi Rizzo 	.pools = {
2178241616dSLuigi Rizzo 		[NETMAP_IF_POOL] = {
2188241616dSLuigi Rizzo 			.name 	= "netmap_if",
2198241616dSLuigi Rizzo 			.objminsize = sizeof(struct netmap_if),
2208241616dSLuigi Rizzo 			.objmaxsize = 4096,
2218241616dSLuigi Rizzo 			.nummin     = 10,	/* don't be stingy */
2228241616dSLuigi Rizzo 			.nummax	    = 10000,	/* XXX very large */
2238241616dSLuigi Rizzo 		},
2248241616dSLuigi Rizzo 		[NETMAP_RING_POOL] = {
2258241616dSLuigi Rizzo 			.name 	= "netmap_ring",
2268241616dSLuigi Rizzo 			.objminsize = sizeof(struct netmap_ring),
2278241616dSLuigi Rizzo 			.objmaxsize = 32*PAGE_SIZE,
2288241616dSLuigi Rizzo 			.nummin     = 2,
2298241616dSLuigi Rizzo 			.nummax	    = 1024,
2308241616dSLuigi Rizzo 		},
2318241616dSLuigi Rizzo 		[NETMAP_BUF_POOL] = {
2328241616dSLuigi Rizzo 			.name	= "netmap_buf",
2338241616dSLuigi Rizzo 			.objminsize = 64,
2348241616dSLuigi Rizzo 			.objmaxsize = 65536,
2358241616dSLuigi Rizzo 			.nummin     = 4,
2368241616dSLuigi Rizzo 			.nummax	    = 1000000, /* one million! */
2378241616dSLuigi Rizzo 		},
2388241616dSLuigi Rizzo 	},
239ce3ee1e7SLuigi Rizzo 	.config   = netmap_mem_global_config,
240ce3ee1e7SLuigi Rizzo 	.finalize = netmap_mem_global_finalize,
241ce3ee1e7SLuigi Rizzo 	.deref    = netmap_mem_global_deref,
242f0ea3689SLuigi Rizzo 
243f0ea3689SLuigi Rizzo 	.nm_id = 1,
244*4bf50f18SLuigi Rizzo 	.nm_grp = -1,
245f0ea3689SLuigi Rizzo 
246f0ea3689SLuigi Rizzo 	.prev = &nm_mem,
247f0ea3689SLuigi Rizzo 	.next = &nm_mem,
248ccdc3305SLuigi Rizzo };
249ccdc3305SLuigi Rizzo 
250ce3ee1e7SLuigi Rizzo 
251f0ea3689SLuigi Rizzo struct netmap_mem_d *netmap_last_mem_d = &nm_mem;
252f0ea3689SLuigi Rizzo 
253ce3ee1e7SLuigi Rizzo /* blueprint for the private memory allocators */
254ce3ee1e7SLuigi Rizzo static int netmap_mem_private_config(struct netmap_mem_d *nmd);
255ce3ee1e7SLuigi Rizzo static int netmap_mem_private_finalize(struct netmap_mem_d *nmd);
256ce3ee1e7SLuigi Rizzo static void netmap_mem_private_deref(struct netmap_mem_d *nmd);
257ce3ee1e7SLuigi Rizzo const struct netmap_mem_d nm_blueprint = {
258ce3ee1e7SLuigi Rizzo 	.pools = {
259ce3ee1e7SLuigi Rizzo 		[NETMAP_IF_POOL] = {
260ce3ee1e7SLuigi Rizzo 			.name 	= "%s_if",
261ce3ee1e7SLuigi Rizzo 			.objminsize = sizeof(struct netmap_if),
262ce3ee1e7SLuigi Rizzo 			.objmaxsize = 4096,
263ce3ee1e7SLuigi Rizzo 			.nummin     = 1,
264f0ea3689SLuigi Rizzo 			.nummax	    = 100,
265ce3ee1e7SLuigi Rizzo 		},
266ce3ee1e7SLuigi Rizzo 		[NETMAP_RING_POOL] = {
267ce3ee1e7SLuigi Rizzo 			.name 	= "%s_ring",
268ce3ee1e7SLuigi Rizzo 			.objminsize = sizeof(struct netmap_ring),
269ce3ee1e7SLuigi Rizzo 			.objmaxsize = 32*PAGE_SIZE,
270ce3ee1e7SLuigi Rizzo 			.nummin     = 2,
271ce3ee1e7SLuigi Rizzo 			.nummax	    = 1024,
272ce3ee1e7SLuigi Rizzo 		},
273ce3ee1e7SLuigi Rizzo 		[NETMAP_BUF_POOL] = {
274ce3ee1e7SLuigi Rizzo 			.name	= "%s_buf",
275ce3ee1e7SLuigi Rizzo 			.objminsize = 64,
276ce3ee1e7SLuigi Rizzo 			.objmaxsize = 65536,
277ce3ee1e7SLuigi Rizzo 			.nummin     = 4,
278ce3ee1e7SLuigi Rizzo 			.nummax	    = 1000000, /* one million! */
279ce3ee1e7SLuigi Rizzo 		},
280ce3ee1e7SLuigi Rizzo 	},
281ce3ee1e7SLuigi Rizzo 	.config   = netmap_mem_private_config,
282ce3ee1e7SLuigi Rizzo 	.finalize = netmap_mem_private_finalize,
283ce3ee1e7SLuigi Rizzo 	.deref    = netmap_mem_private_deref,
284ce3ee1e7SLuigi Rizzo 
285ce3ee1e7SLuigi Rizzo 	.flags = NETMAP_MEM_PRIVATE,
286ce3ee1e7SLuigi Rizzo };
287ce3ee1e7SLuigi Rizzo 
2888241616dSLuigi Rizzo /* memory allocator related sysctls */
2898241616dSLuigi Rizzo 
2908241616dSLuigi Rizzo #define STRINGIFY(x) #x
2918241616dSLuigi Rizzo 
292ce3ee1e7SLuigi Rizzo 
2938241616dSLuigi Rizzo #define DECLARE_SYSCTLS(id, name) \
2948241616dSLuigi Rizzo 	SYSCTL_INT(_dev_netmap, OID_AUTO, name##_size, \
2958241616dSLuigi Rizzo 	    CTLFLAG_RW, &netmap_params[id].size, 0, "Requested size of netmap " STRINGIFY(name) "s"); \
2968241616dSLuigi Rizzo 	SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_size, \
2978241616dSLuigi Rizzo 	    CTLFLAG_RD, &nm_mem.pools[id]._objsize, 0, "Current size of netmap " STRINGIFY(name) "s"); \
2988241616dSLuigi Rizzo 	SYSCTL_INT(_dev_netmap, OID_AUTO, name##_num, \
2998241616dSLuigi Rizzo 	    CTLFLAG_RW, &netmap_params[id].num, 0, "Requested number of netmap " STRINGIFY(name) "s"); \
3008241616dSLuigi Rizzo 	SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_num, \
301f0ea3689SLuigi Rizzo 	    CTLFLAG_RD, &nm_mem.pools[id].objtotal, 0, "Current number of netmap " STRINGIFY(name) "s"); \
302f0ea3689SLuigi Rizzo 	SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_size, \
303f0ea3689SLuigi Rizzo 	    CTLFLAG_RW, &netmap_min_priv_params[id].size, 0, \
304f0ea3689SLuigi Rizzo 	    "Default size of private netmap " STRINGIFY(name) "s"); \
305f0ea3689SLuigi Rizzo 	SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_num, \
306f0ea3689SLuigi Rizzo 	    CTLFLAG_RW, &netmap_min_priv_params[id].num, 0, \
307f0ea3689SLuigi Rizzo 	    "Default number of private netmap " STRINGIFY(name) "s")
3088241616dSLuigi Rizzo 
309ce3ee1e7SLuigi Rizzo SYSCTL_DECL(_dev_netmap);
3108241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_IF_POOL, if);
3118241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_RING_POOL, ring);
3128241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_BUF_POOL, buf);
313ccdc3305SLuigi Rizzo 
314f0ea3689SLuigi Rizzo static int
315f0ea3689SLuigi Rizzo nm_mem_assign_id(struct netmap_mem_d *nmd)
316f0ea3689SLuigi Rizzo {
317f0ea3689SLuigi Rizzo 	nm_memid_t id;
318f0ea3689SLuigi Rizzo 	struct netmap_mem_d *scan = netmap_last_mem_d;
319f0ea3689SLuigi Rizzo 	int error = ENOMEM;
320f0ea3689SLuigi Rizzo 
321f0ea3689SLuigi Rizzo 	NMA_LOCK(&nm_mem);
322f0ea3689SLuigi Rizzo 
323f0ea3689SLuigi Rizzo 	do {
324f0ea3689SLuigi Rizzo 		/* we rely on unsigned wrap around */
325f0ea3689SLuigi Rizzo 		id = scan->nm_id + 1;
326f0ea3689SLuigi Rizzo 		if (id == 0) /* reserve 0 as error value */
327f0ea3689SLuigi Rizzo 			id = 1;
328f0ea3689SLuigi Rizzo 		scan = scan->next;
329f0ea3689SLuigi Rizzo 		if (id != scan->nm_id) {
330f0ea3689SLuigi Rizzo 			nmd->nm_id = id;
331f0ea3689SLuigi Rizzo 			nmd->prev = scan->prev;
332f0ea3689SLuigi Rizzo 			nmd->next = scan;
333f0ea3689SLuigi Rizzo 			scan->prev->next = nmd;
334f0ea3689SLuigi Rizzo 			scan->prev = nmd;
335f0ea3689SLuigi Rizzo 			netmap_last_mem_d = nmd;
336f0ea3689SLuigi Rizzo 			error = 0;
337f0ea3689SLuigi Rizzo 			break;
338f0ea3689SLuigi Rizzo 		}
339f0ea3689SLuigi Rizzo 	} while (scan != netmap_last_mem_d);
340f0ea3689SLuigi Rizzo 
341f0ea3689SLuigi Rizzo 	NMA_UNLOCK(&nm_mem);
342f0ea3689SLuigi Rizzo 	return error;
343f0ea3689SLuigi Rizzo }
344f0ea3689SLuigi Rizzo 
345f0ea3689SLuigi Rizzo static void
346f0ea3689SLuigi Rizzo nm_mem_release_id(struct netmap_mem_d *nmd)
347f0ea3689SLuigi Rizzo {
348f0ea3689SLuigi Rizzo 	NMA_LOCK(&nm_mem);
349f0ea3689SLuigi Rizzo 
350f0ea3689SLuigi Rizzo 	nmd->prev->next = nmd->next;
351f0ea3689SLuigi Rizzo 	nmd->next->prev = nmd->prev;
352f0ea3689SLuigi Rizzo 
353f0ea3689SLuigi Rizzo 	if (netmap_last_mem_d == nmd)
354f0ea3689SLuigi Rizzo 		netmap_last_mem_d = nmd->prev;
355f0ea3689SLuigi Rizzo 
356f0ea3689SLuigi Rizzo 	nmd->prev = nmd->next = NULL;
357f0ea3689SLuigi Rizzo 
358f0ea3689SLuigi Rizzo 	NMA_UNLOCK(&nm_mem);
359f0ea3689SLuigi Rizzo }
360f0ea3689SLuigi Rizzo 
361*4bf50f18SLuigi Rizzo static int
362*4bf50f18SLuigi Rizzo nm_mem_assign_group(struct netmap_mem_d *nmd, struct device *dev)
363*4bf50f18SLuigi Rizzo {
364*4bf50f18SLuigi Rizzo 	int err = 0, id;
365*4bf50f18SLuigi Rizzo 	id = nm_iommu_group_id(dev);
366*4bf50f18SLuigi Rizzo 	if (netmap_verbose)
367*4bf50f18SLuigi Rizzo 		D("iommu_group %d", id);
368*4bf50f18SLuigi Rizzo 
369*4bf50f18SLuigi Rizzo 	NMA_LOCK(nmd);
370*4bf50f18SLuigi Rizzo 
371*4bf50f18SLuigi Rizzo 	if (nmd->nm_grp < 0)
372*4bf50f18SLuigi Rizzo 		nmd->nm_grp = id;
373*4bf50f18SLuigi Rizzo 
374*4bf50f18SLuigi Rizzo 	if (nmd->nm_grp != id)
375*4bf50f18SLuigi Rizzo 		nmd->lasterr = err = ENOMEM;
376*4bf50f18SLuigi Rizzo 
377*4bf50f18SLuigi Rizzo 	NMA_UNLOCK(nmd);
378*4bf50f18SLuigi Rizzo 	return err;
379*4bf50f18SLuigi Rizzo }
380f0ea3689SLuigi Rizzo 
381ccdc3305SLuigi Rizzo /*
3822579e2d7SLuigi Rizzo  * First, find the allocator that contains the requested offset,
3832579e2d7SLuigi Rizzo  * then locate the cluster through a lookup table.
384ccdc3305SLuigi Rizzo  */
385ce3ee1e7SLuigi Rizzo vm_paddr_t
386ce3ee1e7SLuigi Rizzo netmap_mem_ofstophys(struct netmap_mem_d* nmd, vm_ooffset_t offset)
387ccdc3305SLuigi Rizzo {
388ccdc3305SLuigi Rizzo 	int i;
389ce3ee1e7SLuigi Rizzo 	vm_ooffset_t o = offset;
390ce3ee1e7SLuigi Rizzo 	vm_paddr_t pa;
391ce3ee1e7SLuigi Rizzo 	struct netmap_obj_pool *p;
392ccdc3305SLuigi Rizzo 
393ce3ee1e7SLuigi Rizzo 	NMA_LOCK(nmd);
394ce3ee1e7SLuigi Rizzo 	p = nmd->pools;
395ce3ee1e7SLuigi Rizzo 
396ce3ee1e7SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; offset -= p[i].memtotal, i++) {
397ce3ee1e7SLuigi Rizzo 		if (offset >= p[i].memtotal)
398ccdc3305SLuigi Rizzo 			continue;
3992579e2d7SLuigi Rizzo 		// now lookup the cluster's address
400*4bf50f18SLuigi Rizzo 		pa = vtophys(p[i].lut[offset / p[i]._objsize].vaddr) +
4018241616dSLuigi Rizzo 			offset % p[i]._objsize;
402ce3ee1e7SLuigi Rizzo 		NMA_UNLOCK(nmd);
403ce3ee1e7SLuigi Rizzo 		return pa;
404ccdc3305SLuigi Rizzo 	}
4058241616dSLuigi Rizzo 	/* this is only in case of errors */
406b1123b01SLuigi Rizzo 	D("invalid ofs 0x%x out of 0x%x 0x%x 0x%x", (u_int)o,
407ce3ee1e7SLuigi Rizzo 		p[NETMAP_IF_POOL].memtotal,
408ce3ee1e7SLuigi Rizzo 		p[NETMAP_IF_POOL].memtotal
409ce3ee1e7SLuigi Rizzo 			+ p[NETMAP_RING_POOL].memtotal,
410ce3ee1e7SLuigi Rizzo 		p[NETMAP_IF_POOL].memtotal
411ce3ee1e7SLuigi Rizzo 			+ p[NETMAP_RING_POOL].memtotal
412ce3ee1e7SLuigi Rizzo 			+ p[NETMAP_BUF_POOL].memtotal);
413ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(nmd);
414ccdc3305SLuigi Rizzo 	return 0;	// XXX bad address
415ccdc3305SLuigi Rizzo }
416ccdc3305SLuigi Rizzo 
417ce3ee1e7SLuigi Rizzo int
418f0ea3689SLuigi Rizzo netmap_mem_get_info(struct netmap_mem_d* nmd, u_int* size, u_int *memflags,
419f0ea3689SLuigi Rizzo 	nm_memid_t *id)
420ce3ee1e7SLuigi Rizzo {
421ce3ee1e7SLuigi Rizzo 	int error = 0;
422ce3ee1e7SLuigi Rizzo 	NMA_LOCK(nmd);
423ce3ee1e7SLuigi Rizzo 	error = nmd->config(nmd);
424ce3ee1e7SLuigi Rizzo 	if (error)
425ce3ee1e7SLuigi Rizzo 		goto out;
426*4bf50f18SLuigi Rizzo 	if (size) {
427ce3ee1e7SLuigi Rizzo 		if (nmd->flags & NETMAP_MEM_FINALIZED) {
428ce3ee1e7SLuigi Rizzo 			*size = nmd->nm_totalsize;
429ce3ee1e7SLuigi Rizzo 		} else {
430ce3ee1e7SLuigi Rizzo 			int i;
431ce3ee1e7SLuigi Rizzo 			*size = 0;
432ce3ee1e7SLuigi Rizzo 			for (i = 0; i < NETMAP_POOLS_NR; i++) {
433ce3ee1e7SLuigi Rizzo 				struct netmap_obj_pool *p = nmd->pools + i;
434ce3ee1e7SLuigi Rizzo 				*size += (p->_numclusters * p->_clustsize);
435ce3ee1e7SLuigi Rizzo 			}
436ce3ee1e7SLuigi Rizzo 		}
437*4bf50f18SLuigi Rizzo 	}
438*4bf50f18SLuigi Rizzo 	if (memflags)
439ce3ee1e7SLuigi Rizzo 		*memflags = nmd->flags;
440*4bf50f18SLuigi Rizzo 	if (id)
441f0ea3689SLuigi Rizzo 		*id = nmd->nm_id;
442ce3ee1e7SLuigi Rizzo out:
443ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(nmd);
444ce3ee1e7SLuigi Rizzo 	return error;
445ce3ee1e7SLuigi Rizzo }
446ce3ee1e7SLuigi Rizzo 
447ccdc3305SLuigi Rizzo /*
448ccdc3305SLuigi Rizzo  * we store objects by kernel address, need to find the offset
449ccdc3305SLuigi Rizzo  * within the pool to export the value to userspace.
450ccdc3305SLuigi Rizzo  * Algorithm: scan until we find the cluster, then add the
451ccdc3305SLuigi Rizzo  * actual offset in the cluster
452ccdc3305SLuigi Rizzo  */
453ce2cb792SLuigi Rizzo static ssize_t
454ccdc3305SLuigi Rizzo netmap_obj_offset(struct netmap_obj_pool *p, const void *vaddr)
455ccdc3305SLuigi Rizzo {
456ce3ee1e7SLuigi Rizzo 	int i, k = p->_clustentries, n = p->objtotal;
457ccdc3305SLuigi Rizzo 	ssize_t ofs = 0;
458ccdc3305SLuigi Rizzo 
459ccdc3305SLuigi Rizzo 	for (i = 0; i < n; i += k, ofs += p->_clustsize) {
460ccdc3305SLuigi Rizzo 		const char *base = p->lut[i].vaddr;
461ccdc3305SLuigi Rizzo 		ssize_t relofs = (const char *) vaddr - base;
462ccdc3305SLuigi Rizzo 
463aa76317cSLuigi Rizzo 		if (relofs < 0 || relofs >= p->_clustsize)
464ccdc3305SLuigi Rizzo 			continue;
465ccdc3305SLuigi Rizzo 
466ccdc3305SLuigi Rizzo 		ofs = ofs + relofs;
467ccdc3305SLuigi Rizzo 		ND("%s: return offset %d (cluster %d) for pointer %p",
468ccdc3305SLuigi Rizzo 		    p->name, ofs, i, vaddr);
469ccdc3305SLuigi Rizzo 		return ofs;
470ccdc3305SLuigi Rizzo 	}
471ccdc3305SLuigi Rizzo 	D("address %p is not contained inside any cluster (%s)",
472ccdc3305SLuigi Rizzo 	    vaddr, p->name);
473ccdc3305SLuigi Rizzo 	return 0; /* An error occurred */
474ccdc3305SLuigi Rizzo }
475ccdc3305SLuigi Rizzo 
476ccdc3305SLuigi Rizzo /* Helper functions which convert virtual addresses to offsets */
477ce3ee1e7SLuigi Rizzo #define netmap_if_offset(n, v)					\
478ce3ee1e7SLuigi Rizzo 	netmap_obj_offset(&(n)->pools[NETMAP_IF_POOL], (v))
479ccdc3305SLuigi Rizzo 
480ce3ee1e7SLuigi Rizzo #define netmap_ring_offset(n, v)				\
481ce3ee1e7SLuigi Rizzo     ((n)->pools[NETMAP_IF_POOL].memtotal + 			\
482ce3ee1e7SLuigi Rizzo 	netmap_obj_offset(&(n)->pools[NETMAP_RING_POOL], (v)))
483ccdc3305SLuigi Rizzo 
484ce3ee1e7SLuigi Rizzo #define netmap_buf_offset(n, v)					\
485ce3ee1e7SLuigi Rizzo     ((n)->pools[NETMAP_IF_POOL].memtotal +			\
486ce3ee1e7SLuigi Rizzo 	(n)->pools[NETMAP_RING_POOL].memtotal +		\
487ce3ee1e7SLuigi Rizzo 	netmap_obj_offset(&(n)->pools[NETMAP_BUF_POOL], (v)))
488ccdc3305SLuigi Rizzo 
489ccdc3305SLuigi Rizzo 
490ce3ee1e7SLuigi Rizzo ssize_t
491ce3ee1e7SLuigi Rizzo netmap_mem_if_offset(struct netmap_mem_d *nmd, const void *addr)
492ce3ee1e7SLuigi Rizzo {
493ce3ee1e7SLuigi Rizzo 	ssize_t v;
494ce3ee1e7SLuigi Rizzo 	NMA_LOCK(nmd);
495ce3ee1e7SLuigi Rizzo 	v = netmap_if_offset(nmd, addr);
496ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(nmd);
497ce3ee1e7SLuigi Rizzo 	return v;
498ce3ee1e7SLuigi Rizzo }
499ce3ee1e7SLuigi Rizzo 
5008241616dSLuigi Rizzo /*
5018241616dSLuigi Rizzo  * report the index, and use start position as a hint,
5028241616dSLuigi Rizzo  * otherwise buffer allocation becomes terribly expensive.
5038241616dSLuigi Rizzo  */
504ccdc3305SLuigi Rizzo static void *
505ce3ee1e7SLuigi Rizzo netmap_obj_malloc(struct netmap_obj_pool *p, u_int len, uint32_t *start, uint32_t *index)
506ccdc3305SLuigi Rizzo {
507ccdc3305SLuigi Rizzo 	uint32_t i = 0;			/* index in the bitmap */
508ccdc3305SLuigi Rizzo 	uint32_t mask, j;		/* slot counter */
509ccdc3305SLuigi Rizzo 	void *vaddr = NULL;
510ccdc3305SLuigi Rizzo 
511ccdc3305SLuigi Rizzo 	if (len > p->_objsize) {
512ccdc3305SLuigi Rizzo 		D("%s request size %d too large", p->name, len);
513ccdc3305SLuigi Rizzo 		// XXX cannot reduce the size
514ccdc3305SLuigi Rizzo 		return NULL;
515ccdc3305SLuigi Rizzo 	}
516ccdc3305SLuigi Rizzo 
517ccdc3305SLuigi Rizzo 	if (p->objfree == 0) {
518f9790aebSLuigi Rizzo 		D("no more %s objects", p->name);
519ccdc3305SLuigi Rizzo 		return NULL;
520ccdc3305SLuigi Rizzo 	}
5218241616dSLuigi Rizzo 	if (start)
5228241616dSLuigi Rizzo 		i = *start;
523ccdc3305SLuigi Rizzo 
5248241616dSLuigi Rizzo 	/* termination is guaranteed by p->free, but better check bounds on i */
5258241616dSLuigi Rizzo 	while (vaddr == NULL && i < p->bitmap_slots)  {
526ccdc3305SLuigi Rizzo 		uint32_t cur = p->bitmap[i];
527ccdc3305SLuigi Rizzo 		if (cur == 0) { /* bitmask is fully used */
528ccdc3305SLuigi Rizzo 			i++;
529ccdc3305SLuigi Rizzo 			continue;
530ccdc3305SLuigi Rizzo 		}
531ccdc3305SLuigi Rizzo 		/* locate a slot */
532ccdc3305SLuigi Rizzo 		for (j = 0, mask = 1; (cur & mask) == 0; j++, mask <<= 1)
533ccdc3305SLuigi Rizzo 			;
534ccdc3305SLuigi Rizzo 
535ccdc3305SLuigi Rizzo 		p->bitmap[i] &= ~mask; /* mark object as in use */
536ccdc3305SLuigi Rizzo 		p->objfree--;
537ccdc3305SLuigi Rizzo 
538ccdc3305SLuigi Rizzo 		vaddr = p->lut[i * 32 + j].vaddr;
5398241616dSLuigi Rizzo 		if (index)
5408241616dSLuigi Rizzo 			*index = i * 32 + j;
541ccdc3305SLuigi Rizzo 	}
542ccdc3305SLuigi Rizzo 	ND("%s allocator: allocated object @ [%d][%d]: vaddr %p", i, j, vaddr);
543ccdc3305SLuigi Rizzo 
5448241616dSLuigi Rizzo 	if (start)
5458241616dSLuigi Rizzo 		*start = i;
546ccdc3305SLuigi Rizzo 	return vaddr;
547ccdc3305SLuigi Rizzo }
548ccdc3305SLuigi Rizzo 
549ccdc3305SLuigi Rizzo 
550ccdc3305SLuigi Rizzo /*
551f0ea3689SLuigi Rizzo  * free by index, not by address.
552f0ea3689SLuigi Rizzo  * XXX should we also cleanup the content ?
553ccdc3305SLuigi Rizzo  */
554f0ea3689SLuigi Rizzo static int
555ccdc3305SLuigi Rizzo netmap_obj_free(struct netmap_obj_pool *p, uint32_t j)
556ccdc3305SLuigi Rizzo {
557f0ea3689SLuigi Rizzo 	uint32_t *ptr, mask;
558f0ea3689SLuigi Rizzo 
559ccdc3305SLuigi Rizzo 	if (j >= p->objtotal) {
560ccdc3305SLuigi Rizzo 		D("invalid index %u, max %u", j, p->objtotal);
561f0ea3689SLuigi Rizzo 		return 1;
562ccdc3305SLuigi Rizzo 	}
563f0ea3689SLuigi Rizzo 	ptr = &p->bitmap[j / 32];
564f0ea3689SLuigi Rizzo 	mask = (1 << (j % 32));
565f0ea3689SLuigi Rizzo 	if (*ptr & mask) {
566f0ea3689SLuigi Rizzo 		D("ouch, double free on buffer %d", j);
567f0ea3689SLuigi Rizzo 		return 1;
568f0ea3689SLuigi Rizzo 	} else {
569f0ea3689SLuigi Rizzo 		*ptr |= mask;
570ccdc3305SLuigi Rizzo 		p->objfree++;
571f0ea3689SLuigi Rizzo 		return 0;
572f0ea3689SLuigi Rizzo 	}
573ccdc3305SLuigi Rizzo }
574ccdc3305SLuigi Rizzo 
575f0ea3689SLuigi Rizzo /*
576f0ea3689SLuigi Rizzo  * free by address. This is slow but is only used for a few
577f0ea3689SLuigi Rizzo  * objects (rings, nifp)
578f0ea3689SLuigi Rizzo  */
579ccdc3305SLuigi Rizzo static void
580ccdc3305SLuigi Rizzo netmap_obj_free_va(struct netmap_obj_pool *p, void *vaddr)
581ccdc3305SLuigi Rizzo {
582ce3ee1e7SLuigi Rizzo 	u_int i, j, n = p->numclusters;
583ccdc3305SLuigi Rizzo 
584ce3ee1e7SLuigi Rizzo 	for (i = 0, j = 0; i < n; i++, j += p->_clustentries) {
585ce3ee1e7SLuigi Rizzo 		void *base = p->lut[i * p->_clustentries].vaddr;
586ccdc3305SLuigi Rizzo 		ssize_t relofs = (ssize_t) vaddr - (ssize_t) base;
587ccdc3305SLuigi Rizzo 
588ccdc3305SLuigi Rizzo 		/* Given address, is out of the scope of the current cluster.*/
589ede69cffSLuigi Rizzo 		if (vaddr < base || relofs >= p->_clustsize)
590ccdc3305SLuigi Rizzo 			continue;
591ccdc3305SLuigi Rizzo 
592ccdc3305SLuigi Rizzo 		j = j + relofs / p->_objsize;
593ce3ee1e7SLuigi Rizzo 		/* KASSERT(j != 0, ("Cannot free object 0")); */
594ccdc3305SLuigi Rizzo 		netmap_obj_free(p, j);
595ccdc3305SLuigi Rizzo 		return;
596ccdc3305SLuigi Rizzo 	}
597ae10d1afSLuigi Rizzo 	D("address %p is not contained inside any cluster (%s)",
598ccdc3305SLuigi Rizzo 	    vaddr, p->name);
599ccdc3305SLuigi Rizzo }
600ccdc3305SLuigi Rizzo 
601*4bf50f18SLuigi Rizzo #define netmap_mem_bufsize(n)	\
602*4bf50f18SLuigi Rizzo 	((n)->pools[NETMAP_BUF_POOL]._objsize)
603*4bf50f18SLuigi Rizzo 
604ce3ee1e7SLuigi Rizzo #define netmap_if_malloc(n, len)	netmap_obj_malloc(&(n)->pools[NETMAP_IF_POOL], len, NULL, NULL)
605ce3ee1e7SLuigi Rizzo #define netmap_if_free(n, v)		netmap_obj_free_va(&(n)->pools[NETMAP_IF_POOL], (v))
606ce3ee1e7SLuigi Rizzo #define netmap_ring_malloc(n, len)	netmap_obj_malloc(&(n)->pools[NETMAP_RING_POOL], len, NULL, NULL)
607ce3ee1e7SLuigi Rizzo #define netmap_ring_free(n, v)		netmap_obj_free_va(&(n)->pools[NETMAP_RING_POOL], (v))
608ce3ee1e7SLuigi Rizzo #define netmap_buf_malloc(n, _pos, _index)			\
609*4bf50f18SLuigi Rizzo 	netmap_obj_malloc(&(n)->pools[NETMAP_BUF_POOL], netmap_mem_bufsize(n), _pos, _index)
610ccdc3305SLuigi Rizzo 
611ccdc3305SLuigi Rizzo 
612f0ea3689SLuigi Rizzo #if 0 // XXX unused
613ccdc3305SLuigi Rizzo /* Return the index associated to the given packet buffer */
614ce3ee1e7SLuigi Rizzo #define netmap_buf_index(n, v)						\
615ce3ee1e7SLuigi Rizzo     (netmap_obj_offset(&(n)->pools[NETMAP_BUF_POOL], (v)) / NETMAP_BDG_BUF_SIZE(n))
616f0ea3689SLuigi Rizzo #endif
617f0ea3689SLuigi Rizzo 
618f0ea3689SLuigi Rizzo /*
619f0ea3689SLuigi Rizzo  * allocate extra buffers in a linked list.
620f0ea3689SLuigi Rizzo  * returns the actual number.
621f0ea3689SLuigi Rizzo  */
622f0ea3689SLuigi Rizzo uint32_t
623f0ea3689SLuigi Rizzo netmap_extra_alloc(struct netmap_adapter *na, uint32_t *head, uint32_t n)
624f0ea3689SLuigi Rizzo {
625f0ea3689SLuigi Rizzo 	struct netmap_mem_d *nmd = na->nm_mem;
626f0ea3689SLuigi Rizzo 	uint32_t i, pos = 0; /* opaque, scan position in the bitmap */
627f0ea3689SLuigi Rizzo 
628f0ea3689SLuigi Rizzo 	NMA_LOCK(nmd);
629f0ea3689SLuigi Rizzo 
630f0ea3689SLuigi Rizzo 	*head = 0;	/* default, 'null' index ie empty list */
631f0ea3689SLuigi Rizzo 	for (i = 0 ; i < n; i++) {
632f0ea3689SLuigi Rizzo 		uint32_t cur = *head;	/* save current head */
633f0ea3689SLuigi Rizzo 		uint32_t *p = netmap_buf_malloc(nmd, &pos, head);
634f0ea3689SLuigi Rizzo 		if (p == NULL) {
635f0ea3689SLuigi Rizzo 			D("no more buffers after %d of %d", i, n);
636f0ea3689SLuigi Rizzo 			*head = cur; /* restore */
637f0ea3689SLuigi Rizzo 			break;
638f0ea3689SLuigi Rizzo 		}
639f0ea3689SLuigi Rizzo 		RD(5, "allocate buffer %d -> %d", *head, cur);
640f0ea3689SLuigi Rizzo 		*p = cur; /* link to previous head */
641f0ea3689SLuigi Rizzo 	}
642f0ea3689SLuigi Rizzo 
643f0ea3689SLuigi Rizzo 	NMA_UNLOCK(nmd);
644f0ea3689SLuigi Rizzo 
645f0ea3689SLuigi Rizzo 	return i;
646f0ea3689SLuigi Rizzo }
647f0ea3689SLuigi Rizzo 
648f0ea3689SLuigi Rizzo static void
649f0ea3689SLuigi Rizzo netmap_extra_free(struct netmap_adapter *na, uint32_t head)
650f0ea3689SLuigi Rizzo {
651f0ea3689SLuigi Rizzo         struct lut_entry *lut = na->na_lut;
652f0ea3689SLuigi Rizzo 	struct netmap_mem_d *nmd = na->nm_mem;
653f0ea3689SLuigi Rizzo 	struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
654f0ea3689SLuigi Rizzo 	uint32_t i, cur, *buf;
655f0ea3689SLuigi Rizzo 
656f0ea3689SLuigi Rizzo 	D("freeing the extra list");
657f0ea3689SLuigi Rizzo 	for (i = 0; head >=2 && head < p->objtotal; i++) {
658f0ea3689SLuigi Rizzo 		cur = head;
659f0ea3689SLuigi Rizzo 		buf = lut[head].vaddr;
660f0ea3689SLuigi Rizzo 		head = *buf;
661f0ea3689SLuigi Rizzo 		*buf = 0;
662f0ea3689SLuigi Rizzo 		if (netmap_obj_free(p, cur))
663f0ea3689SLuigi Rizzo 			break;
664f0ea3689SLuigi Rizzo 	}
665f0ea3689SLuigi Rizzo 	if (head != 0)
666f0ea3689SLuigi Rizzo 		D("breaking with head %d", head);
667f0ea3689SLuigi Rizzo 	D("freed %d buffers", i);
668f0ea3689SLuigi Rizzo }
669ccdc3305SLuigi Rizzo 
670ccdc3305SLuigi Rizzo 
6718241616dSLuigi Rizzo /* Return nonzero on error */
6728241616dSLuigi Rizzo static int
673f9790aebSLuigi Rizzo netmap_new_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n)
674ccdc3305SLuigi Rizzo {
675ce3ee1e7SLuigi Rizzo 	struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
676ce3ee1e7SLuigi Rizzo 	u_int i = 0;	/* slot counter */
6778241616dSLuigi Rizzo 	uint32_t pos = 0;	/* slot in p->bitmap */
6788241616dSLuigi Rizzo 	uint32_t index = 0;	/* buffer index */
679ccdc3305SLuigi Rizzo 
680ccdc3305SLuigi Rizzo 	for (i = 0; i < n; i++) {
681ce3ee1e7SLuigi Rizzo 		void *vaddr = netmap_buf_malloc(nmd, &pos, &index);
682ccdc3305SLuigi Rizzo 		if (vaddr == NULL) {
683f9790aebSLuigi Rizzo 			D("no more buffers after %d of %d", i, n);
684ccdc3305SLuigi Rizzo 			goto cleanup;
685ccdc3305SLuigi Rizzo 		}
6868241616dSLuigi Rizzo 		slot[i].buf_idx = index;
687ccdc3305SLuigi Rizzo 		slot[i].len = p->_objsize;
688f9790aebSLuigi Rizzo 		slot[i].flags = 0;
689ccdc3305SLuigi Rizzo 	}
690ccdc3305SLuigi Rizzo 
6918241616dSLuigi Rizzo 	ND("allocated %d buffers, %d available, first at %d", n, p->objfree, pos);
6928241616dSLuigi Rizzo 	return (0);
693ccdc3305SLuigi Rizzo 
694ccdc3305SLuigi Rizzo cleanup:
6954cf8455fSEd Maste 	while (i > 0) {
6964cf8455fSEd Maste 		i--;
6978241616dSLuigi Rizzo 		netmap_obj_free(p, slot[i].buf_idx);
698ccdc3305SLuigi Rizzo 	}
6998241616dSLuigi Rizzo 	bzero(slot, n * sizeof(slot[0]));
7008241616dSLuigi Rizzo 	return (ENOMEM);
701ccdc3305SLuigi Rizzo }
702ccdc3305SLuigi Rizzo 
703f0ea3689SLuigi Rizzo static void
704f0ea3689SLuigi Rizzo netmap_mem_set_ring(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n, uint32_t index)
705f0ea3689SLuigi Rizzo {
706f0ea3689SLuigi Rizzo 	struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
707f0ea3689SLuigi Rizzo 	u_int i;
708f0ea3689SLuigi Rizzo 
709f0ea3689SLuigi Rizzo 	for (i = 0; i < n; i++) {
710f0ea3689SLuigi Rizzo 		slot[i].buf_idx = index;
711f0ea3689SLuigi Rizzo 		slot[i].len = p->_objsize;
712f0ea3689SLuigi Rizzo 		slot[i].flags = 0;
713f0ea3689SLuigi Rizzo 	}
714f0ea3689SLuigi Rizzo }
715f0ea3689SLuigi Rizzo 
716ccdc3305SLuigi Rizzo 
717ccdc3305SLuigi Rizzo static void
718f9790aebSLuigi Rizzo netmap_free_buf(struct netmap_mem_d *nmd, uint32_t i)
719ccdc3305SLuigi Rizzo {
720ce3ee1e7SLuigi Rizzo 	struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
7218241616dSLuigi Rizzo 
722ccdc3305SLuigi Rizzo 	if (i < 2 || i >= p->objtotal) {
723ccdc3305SLuigi Rizzo 		D("Cannot free buf#%d: should be in [2, %d[", i, p->objtotal);
724ccdc3305SLuigi Rizzo 		return;
725ccdc3305SLuigi Rizzo 	}
7268241616dSLuigi Rizzo 	netmap_obj_free(p, i);
727ccdc3305SLuigi Rizzo }
728ccdc3305SLuigi Rizzo 
729f0ea3689SLuigi Rizzo 
730f0ea3689SLuigi Rizzo static void
731f0ea3689SLuigi Rizzo netmap_free_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n)
732f0ea3689SLuigi Rizzo {
733f0ea3689SLuigi Rizzo 	u_int i;
734f0ea3689SLuigi Rizzo 
735f0ea3689SLuigi Rizzo 	for (i = 0; i < n; i++) {
736f0ea3689SLuigi Rizzo 		if (slot[i].buf_idx > 2)
737f0ea3689SLuigi Rizzo 			netmap_free_buf(nmd, slot[i].buf_idx);
738f0ea3689SLuigi Rizzo 	}
739f0ea3689SLuigi Rizzo }
740f0ea3689SLuigi Rizzo 
7418241616dSLuigi Rizzo static void
7428241616dSLuigi Rizzo netmap_reset_obj_allocator(struct netmap_obj_pool *p)
7438241616dSLuigi Rizzo {
744ce3ee1e7SLuigi Rizzo 
7458241616dSLuigi Rizzo 	if (p == NULL)
7468241616dSLuigi Rizzo 		return;
7478241616dSLuigi Rizzo 	if (p->bitmap)
7488241616dSLuigi Rizzo 		free(p->bitmap, M_NETMAP);
7498241616dSLuigi Rizzo 	p->bitmap = NULL;
7508241616dSLuigi Rizzo 	if (p->lut) {
751ce3ee1e7SLuigi Rizzo 		u_int i;
752ce3ee1e7SLuigi Rizzo 		size_t sz = p->_clustsize;
753ce3ee1e7SLuigi Rizzo 
754ce3ee1e7SLuigi Rizzo 		for (i = 0; i < p->objtotal; i += p->_clustentries) {
7558241616dSLuigi Rizzo 			if (p->lut[i].vaddr)
756ce3ee1e7SLuigi Rizzo 				contigfree(p->lut[i].vaddr, sz, M_NETMAP);
7578241616dSLuigi Rizzo 		}
7588241616dSLuigi Rizzo 		bzero(p->lut, sizeof(struct lut_entry) * p->objtotal);
7598241616dSLuigi Rizzo #ifdef linux
7608241616dSLuigi Rizzo 		vfree(p->lut);
7618241616dSLuigi Rizzo #else
7628241616dSLuigi Rizzo 		free(p->lut, M_NETMAP);
7638241616dSLuigi Rizzo #endif
7648241616dSLuigi Rizzo 	}
7658241616dSLuigi Rizzo 	p->lut = NULL;
766ce3ee1e7SLuigi Rizzo 	p->objtotal = 0;
767ce3ee1e7SLuigi Rizzo 	p->memtotal = 0;
768ce3ee1e7SLuigi Rizzo 	p->numclusters = 0;
769ce3ee1e7SLuigi Rizzo 	p->objfree = 0;
7708241616dSLuigi Rizzo }
771ccdc3305SLuigi Rizzo 
772ccdc3305SLuigi Rizzo /*
773ccdc3305SLuigi Rizzo  * Free all resources related to an allocator.
774ccdc3305SLuigi Rizzo  */
775ccdc3305SLuigi Rizzo static void
776ccdc3305SLuigi Rizzo netmap_destroy_obj_allocator(struct netmap_obj_pool *p)
777ccdc3305SLuigi Rizzo {
778ccdc3305SLuigi Rizzo 	if (p == NULL)
779ccdc3305SLuigi Rizzo 		return;
7808241616dSLuigi Rizzo 	netmap_reset_obj_allocator(p);
781ccdc3305SLuigi Rizzo }
782ccdc3305SLuigi Rizzo 
783ccdc3305SLuigi Rizzo /*
784ccdc3305SLuigi Rizzo  * We receive a request for objtotal objects, of size objsize each.
785ccdc3305SLuigi Rizzo  * Internally we may round up both numbers, as we allocate objects
786ccdc3305SLuigi Rizzo  * in small clusters multiple of the page size.
787ce3ee1e7SLuigi Rizzo  * We need to keep track of objtotal and clustentries,
788ccdc3305SLuigi Rizzo  * as they are needed when freeing memory.
789ccdc3305SLuigi Rizzo  *
790ccdc3305SLuigi Rizzo  * XXX note -- userspace needs the buffers to be contiguous,
791ccdc3305SLuigi Rizzo  *	so we cannot afford gaps at the end of a cluster.
792ccdc3305SLuigi Rizzo  */
7938241616dSLuigi Rizzo 
7948241616dSLuigi Rizzo 
7958241616dSLuigi Rizzo /* call with NMA_LOCK held */
7968241616dSLuigi Rizzo static int
7978241616dSLuigi Rizzo netmap_config_obj_allocator(struct netmap_obj_pool *p, u_int objtotal, u_int objsize)
798ccdc3305SLuigi Rizzo {
799ce3ee1e7SLuigi Rizzo 	int i;
800ccdc3305SLuigi Rizzo 	u_int clustsize;	/* the cluster size, multiple of page size */
801ccdc3305SLuigi Rizzo 	u_int clustentries;	/* how many objects per entry */
802ccdc3305SLuigi Rizzo 
803ce3ee1e7SLuigi Rizzo 	/* we store the current request, so we can
804ce3ee1e7SLuigi Rizzo 	 * detect configuration changes later */
805ce3ee1e7SLuigi Rizzo 	p->r_objtotal = objtotal;
806ce3ee1e7SLuigi Rizzo 	p->r_objsize = objsize;
807ce3ee1e7SLuigi Rizzo 
808*4bf50f18SLuigi Rizzo #define MAX_CLUSTSIZE	(1<<22)		// 4 MB
80917885a7bSLuigi Rizzo #define LINE_ROUND	NM_CACHE_ALIGN	// 64
810ccdc3305SLuigi Rizzo 	if (objsize >= MAX_CLUSTSIZE) {
811ccdc3305SLuigi Rizzo 		/* we could do it but there is no point */
812ccdc3305SLuigi Rizzo 		D("unsupported allocation for %d bytes", objsize);
813ce3ee1e7SLuigi Rizzo 		return EINVAL;
814ccdc3305SLuigi Rizzo 	}
815ccdc3305SLuigi Rizzo 	/* make sure objsize is a multiple of LINE_ROUND */
816ccdc3305SLuigi Rizzo 	i = (objsize & (LINE_ROUND - 1));
817ccdc3305SLuigi Rizzo 	if (i) {
818ccdc3305SLuigi Rizzo 		D("XXX aligning object by %d bytes", LINE_ROUND - i);
819ccdc3305SLuigi Rizzo 		objsize += LINE_ROUND - i;
820ccdc3305SLuigi Rizzo 	}
8218241616dSLuigi Rizzo 	if (objsize < p->objminsize || objsize > p->objmaxsize) {
8228241616dSLuigi Rizzo 		D("requested objsize %d out of range [%d, %d]",
8238241616dSLuigi Rizzo 			objsize, p->objminsize, p->objmaxsize);
824ce3ee1e7SLuigi Rizzo 		return EINVAL;
8258241616dSLuigi Rizzo 	}
8268241616dSLuigi Rizzo 	if (objtotal < p->nummin || objtotal > p->nummax) {
8278241616dSLuigi Rizzo 		D("requested objtotal %d out of range [%d, %d]",
8288241616dSLuigi Rizzo 			objtotal, p->nummin, p->nummax);
829ce3ee1e7SLuigi Rizzo 		return EINVAL;
8308241616dSLuigi Rizzo 	}
831ccdc3305SLuigi Rizzo 	/*
832ccdc3305SLuigi Rizzo 	 * Compute number of objects using a brute-force approach:
833ccdc3305SLuigi Rizzo 	 * given a max cluster size,
834ccdc3305SLuigi Rizzo 	 * we try to fill it with objects keeping track of the
835ccdc3305SLuigi Rizzo 	 * wasted space to the next page boundary.
836ccdc3305SLuigi Rizzo 	 */
837ccdc3305SLuigi Rizzo 	for (clustentries = 0, i = 1;; i++) {
838ccdc3305SLuigi Rizzo 		u_int delta, used = i * objsize;
839ccdc3305SLuigi Rizzo 		if (used > MAX_CLUSTSIZE)
840ccdc3305SLuigi Rizzo 			break;
841ccdc3305SLuigi Rizzo 		delta = used % PAGE_SIZE;
842ccdc3305SLuigi Rizzo 		if (delta == 0) { // exact solution
843ccdc3305SLuigi Rizzo 			clustentries = i;
844ccdc3305SLuigi Rizzo 			break;
845ccdc3305SLuigi Rizzo 		}
846ccdc3305SLuigi Rizzo 	}
847*4bf50f18SLuigi Rizzo 	/* exact solution not found */
848*4bf50f18SLuigi Rizzo 	if (clustentries == 0) {
849*4bf50f18SLuigi Rizzo 		D("unsupported allocation for %d bytes", objsize);
850*4bf50f18SLuigi Rizzo 		return EINVAL;
851*4bf50f18SLuigi Rizzo 	}
852*4bf50f18SLuigi Rizzo 	/* compute clustsize */
853ccdc3305SLuigi Rizzo 	clustsize = clustentries * objsize;
854ae10d1afSLuigi Rizzo 	if (netmap_verbose)
855ccdc3305SLuigi Rizzo 		D("objsize %d clustsize %d objects %d",
856ccdc3305SLuigi Rizzo 			objsize, clustsize, clustentries);
857ccdc3305SLuigi Rizzo 
858ccdc3305SLuigi Rizzo 	/*
859ccdc3305SLuigi Rizzo 	 * The number of clusters is n = ceil(objtotal/clustentries)
860ccdc3305SLuigi Rizzo 	 * objtotal' = n * clustentries
861ccdc3305SLuigi Rizzo 	 */
862ce3ee1e7SLuigi Rizzo 	p->_clustentries = clustentries;
863ccdc3305SLuigi Rizzo 	p->_clustsize = clustsize;
864ce3ee1e7SLuigi Rizzo 	p->_numclusters = (objtotal + clustentries - 1) / clustentries;
865ce3ee1e7SLuigi Rizzo 
866ce3ee1e7SLuigi Rizzo 	/* actual values (may be larger than requested) */
8678241616dSLuigi Rizzo 	p->_objsize = objsize;
868ce3ee1e7SLuigi Rizzo 	p->_objtotal = p->_numclusters * clustentries;
869ccdc3305SLuigi Rizzo 
8708241616dSLuigi Rizzo 	return 0;
8718241616dSLuigi Rizzo }
8728241616dSLuigi Rizzo 
8738241616dSLuigi Rizzo 
8748241616dSLuigi Rizzo /* call with NMA_LOCK held */
8758241616dSLuigi Rizzo static int
8768241616dSLuigi Rizzo netmap_finalize_obj_allocator(struct netmap_obj_pool *p)
8778241616dSLuigi Rizzo {
878ce3ee1e7SLuigi Rizzo 	int i; /* must be signed */
879ce3ee1e7SLuigi Rizzo 	size_t n;
880ce3ee1e7SLuigi Rizzo 
881ce3ee1e7SLuigi Rizzo 	/* optimistically assume we have enough memory */
882ce3ee1e7SLuigi Rizzo 	p->numclusters = p->_numclusters;
883ce3ee1e7SLuigi Rizzo 	p->objtotal = p->_objtotal;
8848241616dSLuigi Rizzo 
8858241616dSLuigi Rizzo 	n = sizeof(struct lut_entry) * p->objtotal;
8868241616dSLuigi Rizzo #ifdef linux
8878241616dSLuigi Rizzo 	p->lut = vmalloc(n);
8888241616dSLuigi Rizzo #else
889d2b91851SEd Maste 	p->lut = malloc(n, M_NETMAP, M_NOWAIT | M_ZERO);
8908241616dSLuigi Rizzo #endif
891ccdc3305SLuigi Rizzo 	if (p->lut == NULL) {
892ce3ee1e7SLuigi Rizzo 		D("Unable to create lookup table (%d bytes) for '%s'", (int)n, p->name);
893ccdc3305SLuigi Rizzo 		goto clean;
894ccdc3305SLuigi Rizzo 	}
895ccdc3305SLuigi Rizzo 
896ccdc3305SLuigi Rizzo 	/* Allocate the bitmap */
897ccdc3305SLuigi Rizzo 	n = (p->objtotal + 31) / 32;
898d2b91851SEd Maste 	p->bitmap = malloc(sizeof(uint32_t) * n, M_NETMAP, M_NOWAIT | M_ZERO);
899ccdc3305SLuigi Rizzo 	if (p->bitmap == NULL) {
900ce3ee1e7SLuigi Rizzo 		D("Unable to create bitmap (%d entries) for allocator '%s'", (int)n,
9018241616dSLuigi Rizzo 		    p->name);
902ccdc3305SLuigi Rizzo 		goto clean;
903ccdc3305SLuigi Rizzo 	}
9048241616dSLuigi Rizzo 	p->bitmap_slots = n;
905ccdc3305SLuigi Rizzo 
906ccdc3305SLuigi Rizzo 	/*
907ccdc3305SLuigi Rizzo 	 * Allocate clusters, init pointers and bitmap
908ccdc3305SLuigi Rizzo 	 */
909ce3ee1e7SLuigi Rizzo 
910ce3ee1e7SLuigi Rizzo 	n = p->_clustsize;
911ce3ee1e7SLuigi Rizzo 	for (i = 0; i < (int)p->objtotal;) {
912ce3ee1e7SLuigi Rizzo 		int lim = i + p->_clustentries;
913ccdc3305SLuigi Rizzo 		char *clust;
914ccdc3305SLuigi Rizzo 
915ce3ee1e7SLuigi Rizzo 		clust = contigmalloc(n, M_NETMAP, M_NOWAIT | M_ZERO,
916ce3ee1e7SLuigi Rizzo 		    (size_t)0, -1UL, PAGE_SIZE, 0);
917ccdc3305SLuigi Rizzo 		if (clust == NULL) {
918ccdc3305SLuigi Rizzo 			/*
919ccdc3305SLuigi Rizzo 			 * If we get here, there is a severe memory shortage,
920ccdc3305SLuigi Rizzo 			 * so halve the allocated memory to reclaim some.
921ccdc3305SLuigi Rizzo 			 */
922ccdc3305SLuigi Rizzo 			D("Unable to create cluster at %d for '%s' allocator",
9238241616dSLuigi Rizzo 			    i, p->name);
924ce3ee1e7SLuigi Rizzo 			if (i < 2) /* nothing to halve */
925ce3ee1e7SLuigi Rizzo 				goto out;
926ccdc3305SLuigi Rizzo 			lim = i / 2;
9278241616dSLuigi Rizzo 			for (i--; i >= lim; i--) {
928ccdc3305SLuigi Rizzo 				p->bitmap[ (i>>5) ] &=  ~( 1 << (i & 31) );
929ce3ee1e7SLuigi Rizzo 				if (i % p->_clustentries == 0 && p->lut[i].vaddr)
930ccdc3305SLuigi Rizzo 					contigfree(p->lut[i].vaddr,
931ce3ee1e7SLuigi Rizzo 						n, M_NETMAP);
932ccdc3305SLuigi Rizzo 			}
933ce3ee1e7SLuigi Rizzo 		out:
934ccdc3305SLuigi Rizzo 			p->objtotal = i;
935ce3ee1e7SLuigi Rizzo 			/* we may have stopped in the middle of a cluster */
936ce3ee1e7SLuigi Rizzo 			p->numclusters = (i + p->_clustentries - 1) / p->_clustentries;
937ccdc3305SLuigi Rizzo 			break;
938ccdc3305SLuigi Rizzo 		}
9398241616dSLuigi Rizzo 		for (; i < lim; i++, clust += p->_objsize) {
940ccdc3305SLuigi Rizzo 			p->bitmap[ (i>>5) ] |=  ( 1 << (i & 31) );
941ccdc3305SLuigi Rizzo 			p->lut[i].vaddr = clust;
942ccdc3305SLuigi Rizzo 			p->lut[i].paddr = vtophys(clust);
943ccdc3305SLuigi Rizzo 		}
944ccdc3305SLuigi Rizzo 	}
945ce3ee1e7SLuigi Rizzo 	p->objfree = p->objtotal;
946ce3ee1e7SLuigi Rizzo 	p->memtotal = p->numclusters * p->_clustsize;
947ce3ee1e7SLuigi Rizzo 	if (p->objfree == 0)
948ce3ee1e7SLuigi Rizzo 		goto clean;
949ae10d1afSLuigi Rizzo 	if (netmap_verbose)
950ccdc3305SLuigi Rizzo 		D("Pre-allocated %d clusters (%d/%dKB) for '%s'",
951ce3ee1e7SLuigi Rizzo 		    p->numclusters, p->_clustsize >> 10,
952ce3ee1e7SLuigi Rizzo 		    p->memtotal >> 10, p->name);
953ccdc3305SLuigi Rizzo 
9548241616dSLuigi Rizzo 	return 0;
955ccdc3305SLuigi Rizzo 
956ccdc3305SLuigi Rizzo clean:
9578241616dSLuigi Rizzo 	netmap_reset_obj_allocator(p);
9588241616dSLuigi Rizzo 	return ENOMEM;
9598241616dSLuigi Rizzo }
9608241616dSLuigi Rizzo 
9618241616dSLuigi Rizzo /* call with lock held */
9628241616dSLuigi Rizzo static int
963ce3ee1e7SLuigi Rizzo netmap_memory_config_changed(struct netmap_mem_d *nmd)
9648241616dSLuigi Rizzo {
9658241616dSLuigi Rizzo 	int i;
9668241616dSLuigi Rizzo 
9678241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
968ce3ee1e7SLuigi Rizzo 		if (nmd->pools[i].r_objsize != netmap_params[i].size ||
969ce3ee1e7SLuigi Rizzo 		    nmd->pools[i].r_objtotal != netmap_params[i].num)
9708241616dSLuigi Rizzo 		    return 1;
9718241616dSLuigi Rizzo 	}
9728241616dSLuigi Rizzo 	return 0;
9738241616dSLuigi Rizzo }
9748241616dSLuigi Rizzo 
975ce3ee1e7SLuigi Rizzo static void
976ce3ee1e7SLuigi Rizzo netmap_mem_reset_all(struct netmap_mem_d *nmd)
977ce3ee1e7SLuigi Rizzo {
978ce3ee1e7SLuigi Rizzo 	int i;
979f0ea3689SLuigi Rizzo 
980f0ea3689SLuigi Rizzo 	if (netmap_verbose)
981ce3ee1e7SLuigi Rizzo 		D("resetting %p", nmd);
982ce3ee1e7SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
983ce3ee1e7SLuigi Rizzo 		netmap_reset_obj_allocator(&nmd->pools[i]);
984ce3ee1e7SLuigi Rizzo 	}
985ce3ee1e7SLuigi Rizzo 	nmd->flags  &= ~NETMAP_MEM_FINALIZED;
986ce3ee1e7SLuigi Rizzo }
987ce3ee1e7SLuigi Rizzo 
988ce3ee1e7SLuigi Rizzo static int
989*4bf50f18SLuigi Rizzo netmap_mem_unmap(struct netmap_obj_pool *p, struct netmap_adapter *na)
990*4bf50f18SLuigi Rizzo {
991*4bf50f18SLuigi Rizzo 	int i, lim = p->_objtotal;
992*4bf50f18SLuigi Rizzo 
993*4bf50f18SLuigi Rizzo 	if (na->pdev == NULL)
994*4bf50f18SLuigi Rizzo 		return 0;
995*4bf50f18SLuigi Rizzo 
996*4bf50f18SLuigi Rizzo #ifdef __FreeBSD__
997*4bf50f18SLuigi Rizzo 	(void)i;
998*4bf50f18SLuigi Rizzo 	(void)lim;
999*4bf50f18SLuigi Rizzo 	D("unsupported on FreeBSD");
1000*4bf50f18SLuigi Rizzo #else /* linux */
1001*4bf50f18SLuigi Rizzo 	for (i = 2; i < lim; i++) {
1002*4bf50f18SLuigi Rizzo 		netmap_unload_map(na, (bus_dma_tag_t) na->pdev, &p->lut[i].paddr);
1003*4bf50f18SLuigi Rizzo 	}
1004*4bf50f18SLuigi Rizzo #endif /* linux */
1005*4bf50f18SLuigi Rizzo 
1006*4bf50f18SLuigi Rizzo 	return 0;
1007*4bf50f18SLuigi Rizzo }
1008*4bf50f18SLuigi Rizzo 
1009*4bf50f18SLuigi Rizzo static int
1010*4bf50f18SLuigi Rizzo netmap_mem_map(struct netmap_obj_pool *p, struct netmap_adapter *na)
1011*4bf50f18SLuigi Rizzo {
1012*4bf50f18SLuigi Rizzo #ifdef __FreeBSD__
1013*4bf50f18SLuigi Rizzo 	D("unsupported on FreeBSD");
1014*4bf50f18SLuigi Rizzo #else /* linux */
1015*4bf50f18SLuigi Rizzo 	int i, lim = p->_objtotal;
1016*4bf50f18SLuigi Rizzo 
1017*4bf50f18SLuigi Rizzo 	if (na->pdev == NULL)
1018*4bf50f18SLuigi Rizzo 		return 0;
1019*4bf50f18SLuigi Rizzo 
1020*4bf50f18SLuigi Rizzo 	for (i = 2; i < lim; i++) {
1021*4bf50f18SLuigi Rizzo 		netmap_load_map(na, (bus_dma_tag_t) na->pdev, &p->lut[i].paddr,
1022*4bf50f18SLuigi Rizzo 				p->lut[i].vaddr);
1023*4bf50f18SLuigi Rizzo 	}
1024*4bf50f18SLuigi Rizzo #endif /* linux */
1025*4bf50f18SLuigi Rizzo 
1026*4bf50f18SLuigi Rizzo 	return 0;
1027*4bf50f18SLuigi Rizzo }
1028*4bf50f18SLuigi Rizzo 
1029*4bf50f18SLuigi Rizzo static int
1030ce3ee1e7SLuigi Rizzo netmap_mem_finalize_all(struct netmap_mem_d *nmd)
1031ce3ee1e7SLuigi Rizzo {
1032ce3ee1e7SLuigi Rizzo 	int i;
1033ce3ee1e7SLuigi Rizzo 	if (nmd->flags & NETMAP_MEM_FINALIZED)
1034ce3ee1e7SLuigi Rizzo 		return 0;
1035ce3ee1e7SLuigi Rizzo 	nmd->lasterr = 0;
1036ce3ee1e7SLuigi Rizzo 	nmd->nm_totalsize = 0;
1037ce3ee1e7SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1038ce3ee1e7SLuigi Rizzo 		nmd->lasterr = netmap_finalize_obj_allocator(&nmd->pools[i]);
1039ce3ee1e7SLuigi Rizzo 		if (nmd->lasterr)
1040ce3ee1e7SLuigi Rizzo 			goto error;
1041ce3ee1e7SLuigi Rizzo 		nmd->nm_totalsize += nmd->pools[i].memtotal;
1042ce3ee1e7SLuigi Rizzo 	}
1043ce3ee1e7SLuigi Rizzo 	/* buffers 0 and 1 are reserved */
1044ce3ee1e7SLuigi Rizzo 	nmd->pools[NETMAP_BUF_POOL].objfree -= 2;
1045ce3ee1e7SLuigi Rizzo 	nmd->pools[NETMAP_BUF_POOL].bitmap[0] = ~3;
1046ce3ee1e7SLuigi Rizzo 	nmd->flags |= NETMAP_MEM_FINALIZED;
1047ce3ee1e7SLuigi Rizzo 
1048f0ea3689SLuigi Rizzo 	if (netmap_verbose)
1049f0ea3689SLuigi Rizzo 		D("interfaces %d KB, rings %d KB, buffers %d MB",
1050ce3ee1e7SLuigi Rizzo 		    nmd->pools[NETMAP_IF_POOL].memtotal >> 10,
1051ce3ee1e7SLuigi Rizzo 		    nmd->pools[NETMAP_RING_POOL].memtotal >> 10,
1052ce3ee1e7SLuigi Rizzo 		    nmd->pools[NETMAP_BUF_POOL].memtotal >> 20);
1053ce3ee1e7SLuigi Rizzo 
1054f0ea3689SLuigi Rizzo 	if (netmap_verbose)
1055ce3ee1e7SLuigi Rizzo 		D("Free buffers: %d", nmd->pools[NETMAP_BUF_POOL].objfree);
1056ce3ee1e7SLuigi Rizzo 
1057ce3ee1e7SLuigi Rizzo 
1058ce3ee1e7SLuigi Rizzo 	return 0;
1059ce3ee1e7SLuigi Rizzo error:
1060ce3ee1e7SLuigi Rizzo 	netmap_mem_reset_all(nmd);
1061ce3ee1e7SLuigi Rizzo 	return nmd->lasterr;
1062ce3ee1e7SLuigi Rizzo }
1063ce3ee1e7SLuigi Rizzo 
1064ce3ee1e7SLuigi Rizzo 
1065ce3ee1e7SLuigi Rizzo 
1066ce3ee1e7SLuigi Rizzo void
1067ce3ee1e7SLuigi Rizzo netmap_mem_private_delete(struct netmap_mem_d *nmd)
1068ce3ee1e7SLuigi Rizzo {
1069ce3ee1e7SLuigi Rizzo 	if (nmd == NULL)
1070ce3ee1e7SLuigi Rizzo 		return;
1071f0ea3689SLuigi Rizzo 	if (netmap_verbose)
1072ce3ee1e7SLuigi Rizzo 		D("deleting %p", nmd);
1073ce3ee1e7SLuigi Rizzo 	if (nmd->refcount > 0)
1074ce3ee1e7SLuigi Rizzo 		D("bug: deleting mem allocator with refcount=%d!", nmd->refcount);
1075f0ea3689SLuigi Rizzo 	nm_mem_release_id(nmd);
1076f0ea3689SLuigi Rizzo 	if (netmap_verbose)
1077ce3ee1e7SLuigi Rizzo 		D("done deleting %p", nmd);
1078ce3ee1e7SLuigi Rizzo 	NMA_LOCK_DESTROY(nmd);
1079ce3ee1e7SLuigi Rizzo 	free(nmd, M_DEVBUF);
1080ce3ee1e7SLuigi Rizzo }
1081ce3ee1e7SLuigi Rizzo 
1082ce3ee1e7SLuigi Rizzo static int
1083ce3ee1e7SLuigi Rizzo netmap_mem_private_config(struct netmap_mem_d *nmd)
1084ce3ee1e7SLuigi Rizzo {
1085ce3ee1e7SLuigi Rizzo 	/* nothing to do, we are configured on creation
1086ce3ee1e7SLuigi Rizzo  	 * and configuration never changes thereafter
1087ce3ee1e7SLuigi Rizzo  	 */
1088ce3ee1e7SLuigi Rizzo 	return 0;
1089ce3ee1e7SLuigi Rizzo }
1090ce3ee1e7SLuigi Rizzo 
1091ce3ee1e7SLuigi Rizzo static int
1092ce3ee1e7SLuigi Rizzo netmap_mem_private_finalize(struct netmap_mem_d *nmd)
1093ce3ee1e7SLuigi Rizzo {
1094ce3ee1e7SLuigi Rizzo 	int err;
1095ce3ee1e7SLuigi Rizzo 	NMA_LOCK(nmd);
1096ce3ee1e7SLuigi Rizzo 	nmd->refcount++;
1097ce3ee1e7SLuigi Rizzo 	err = netmap_mem_finalize_all(nmd);
1098ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(nmd);
1099ce3ee1e7SLuigi Rizzo 	return err;
1100ce3ee1e7SLuigi Rizzo 
1101ce3ee1e7SLuigi Rizzo }
1102ce3ee1e7SLuigi Rizzo 
1103f9790aebSLuigi Rizzo static void
1104f9790aebSLuigi Rizzo netmap_mem_private_deref(struct netmap_mem_d *nmd)
1105ce3ee1e7SLuigi Rizzo {
1106ce3ee1e7SLuigi Rizzo 	NMA_LOCK(nmd);
1107ce3ee1e7SLuigi Rizzo 	if (--nmd->refcount <= 0)
1108ce3ee1e7SLuigi Rizzo 		netmap_mem_reset_all(nmd);
1109ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(nmd);
1110ce3ee1e7SLuigi Rizzo }
1111ce3ee1e7SLuigi Rizzo 
1112f0ea3689SLuigi Rizzo 
1113f0ea3689SLuigi Rizzo /*
1114f0ea3689SLuigi Rizzo  * allocator for private memory
1115f0ea3689SLuigi Rizzo  */
1116ce3ee1e7SLuigi Rizzo struct netmap_mem_d *
1117f0ea3689SLuigi Rizzo netmap_mem_private_new(const char *name, u_int txr, u_int txd,
1118f0ea3689SLuigi Rizzo 	u_int rxr, u_int rxd, u_int extra_bufs, u_int npipes, int *perr)
1119ce3ee1e7SLuigi Rizzo {
1120ce3ee1e7SLuigi Rizzo 	struct netmap_mem_d *d = NULL;
1121ce3ee1e7SLuigi Rizzo 	struct netmap_obj_params p[NETMAP_POOLS_NR];
1122f0ea3689SLuigi Rizzo 	int i, err;
1123f0ea3689SLuigi Rizzo 	u_int v, maxd;
1124ce3ee1e7SLuigi Rizzo 
1125ce3ee1e7SLuigi Rizzo 	d = malloc(sizeof(struct netmap_mem_d),
1126ce3ee1e7SLuigi Rizzo 			M_DEVBUF, M_NOWAIT | M_ZERO);
1127f0ea3689SLuigi Rizzo 	if (d == NULL) {
1128f0ea3689SLuigi Rizzo 		err = ENOMEM;
1129f0ea3689SLuigi Rizzo 		goto error;
1130f0ea3689SLuigi Rizzo 	}
1131ce3ee1e7SLuigi Rizzo 
1132ce3ee1e7SLuigi Rizzo 	*d = nm_blueprint;
1133ce3ee1e7SLuigi Rizzo 
1134f0ea3689SLuigi Rizzo 	err = nm_mem_assign_id(d);
1135f0ea3689SLuigi Rizzo 	if (err)
1136f0ea3689SLuigi Rizzo 		goto error;
1137f0ea3689SLuigi Rizzo 
1138f0ea3689SLuigi Rizzo 	/* account for the fake host rings */
1139ce3ee1e7SLuigi Rizzo 	txr++;
1140ce3ee1e7SLuigi Rizzo 	rxr++;
1141ce3ee1e7SLuigi Rizzo 
1142f0ea3689SLuigi Rizzo 	/* copy the min values */
1143f0ea3689SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1144f0ea3689SLuigi Rizzo 		p[i] = netmap_min_priv_params[i];
1145f0ea3689SLuigi Rizzo 	}
1146f0ea3689SLuigi Rizzo 
1147f0ea3689SLuigi Rizzo 	/* possibly increase them to fit user request */
1148f0ea3689SLuigi Rizzo 	v = sizeof(struct netmap_if) + sizeof(ssize_t) * (txr + rxr);
1149f0ea3689SLuigi Rizzo 	if (p[NETMAP_IF_POOL].size < v)
1150f0ea3689SLuigi Rizzo 		p[NETMAP_IF_POOL].size = v;
1151f0ea3689SLuigi Rizzo 	v = 2 + 4 * npipes;
1152f0ea3689SLuigi Rizzo 	if (p[NETMAP_IF_POOL].num < v)
1153f0ea3689SLuigi Rizzo 		p[NETMAP_IF_POOL].num = v;
1154f0ea3689SLuigi Rizzo 	maxd = (txd > rxd) ? txd : rxd;
1155f0ea3689SLuigi Rizzo 	v = sizeof(struct netmap_ring) + sizeof(struct netmap_slot) * maxd;
1156f0ea3689SLuigi Rizzo 	if (p[NETMAP_RING_POOL].size < v)
1157f0ea3689SLuigi Rizzo 		p[NETMAP_RING_POOL].size = v;
1158f0ea3689SLuigi Rizzo 	/* each pipe endpoint needs two tx rings (1 normal + 1 host, fake)
1159f0ea3689SLuigi Rizzo          * and two rx rings (again, 1 normal and 1 fake host)
1160f0ea3689SLuigi Rizzo          */
1161f0ea3689SLuigi Rizzo 	v = txr + rxr + 8 * npipes;
1162f0ea3689SLuigi Rizzo 	if (p[NETMAP_RING_POOL].num < v)
1163f0ea3689SLuigi Rizzo 		p[NETMAP_RING_POOL].num = v;
1164f0ea3689SLuigi Rizzo 	/* for each pipe we only need the buffers for the 4 "real" rings.
1165f0ea3689SLuigi Rizzo          * On the other end, the pipe ring dimension may be different from
1166f0ea3689SLuigi Rizzo          * the parent port ring dimension. As a compromise, we allocate twice the
1167f0ea3689SLuigi Rizzo          * space actually needed if the pipe rings were the same size as the parent rings
1168f0ea3689SLuigi Rizzo          */
1169f0ea3689SLuigi Rizzo 	v = (4 * npipes + rxr) * rxd + (4 * npipes + txr) * txd + 2 + extra_bufs;
1170f0ea3689SLuigi Rizzo 		/* the +2 is for the tx and rx fake buffers (indices 0 and 1) */
1171f0ea3689SLuigi Rizzo 	if (p[NETMAP_BUF_POOL].num < v)
1172f0ea3689SLuigi Rizzo 		p[NETMAP_BUF_POOL].num = v;
1173f0ea3689SLuigi Rizzo 
1174f0ea3689SLuigi Rizzo 	if (netmap_verbose)
1175ce3ee1e7SLuigi Rizzo 		D("req if %d*%d ring %d*%d buf %d*%d",
1176ce3ee1e7SLuigi Rizzo 			p[NETMAP_IF_POOL].num,
1177ce3ee1e7SLuigi Rizzo 			p[NETMAP_IF_POOL].size,
1178ce3ee1e7SLuigi Rizzo 			p[NETMAP_RING_POOL].num,
1179ce3ee1e7SLuigi Rizzo 			p[NETMAP_RING_POOL].size,
1180ce3ee1e7SLuigi Rizzo 			p[NETMAP_BUF_POOL].num,
1181ce3ee1e7SLuigi Rizzo 			p[NETMAP_BUF_POOL].size);
1182ce3ee1e7SLuigi Rizzo 
1183ce3ee1e7SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1184ce3ee1e7SLuigi Rizzo 		snprintf(d->pools[i].name, NETMAP_POOL_MAX_NAMSZ,
1185ce3ee1e7SLuigi Rizzo 				nm_blueprint.pools[i].name,
1186ce3ee1e7SLuigi Rizzo 				name);
1187f0ea3689SLuigi Rizzo 		err = netmap_config_obj_allocator(&d->pools[i],
1188f0ea3689SLuigi Rizzo 				p[i].num, p[i].size);
1189f0ea3689SLuigi Rizzo 		if (err)
1190ce3ee1e7SLuigi Rizzo 			goto error;
1191ce3ee1e7SLuigi Rizzo 	}
1192ce3ee1e7SLuigi Rizzo 
1193ce3ee1e7SLuigi Rizzo 	d->flags &= ~NETMAP_MEM_FINALIZED;
1194ce3ee1e7SLuigi Rizzo 
1195ce3ee1e7SLuigi Rizzo 	NMA_LOCK_INIT(d);
1196ce3ee1e7SLuigi Rizzo 
1197ce3ee1e7SLuigi Rizzo 	return d;
1198ce3ee1e7SLuigi Rizzo error:
1199ce3ee1e7SLuigi Rizzo 	netmap_mem_private_delete(d);
1200f0ea3689SLuigi Rizzo 	if (perr)
1201f0ea3689SLuigi Rizzo 		*perr = err;
1202ce3ee1e7SLuigi Rizzo 	return NULL;
1203ce3ee1e7SLuigi Rizzo }
1204ce3ee1e7SLuigi Rizzo 
12058241616dSLuigi Rizzo 
12068241616dSLuigi Rizzo /* call with lock held */
12078241616dSLuigi Rizzo static int
1208ce3ee1e7SLuigi Rizzo netmap_mem_global_config(struct netmap_mem_d *nmd)
12098241616dSLuigi Rizzo {
12108241616dSLuigi Rizzo 	int i;
12118241616dSLuigi Rizzo 
1212ce3ee1e7SLuigi Rizzo 	if (nmd->refcount)
1213ce3ee1e7SLuigi Rizzo 		/* already in use, we cannot change the configuration */
1214ce3ee1e7SLuigi Rizzo 		goto out;
1215ce3ee1e7SLuigi Rizzo 
1216ce3ee1e7SLuigi Rizzo 	if (!netmap_memory_config_changed(nmd))
12178241616dSLuigi Rizzo 		goto out;
12188241616dSLuigi Rizzo 
12198241616dSLuigi Rizzo 	D("reconfiguring");
12208241616dSLuigi Rizzo 
1221ce3ee1e7SLuigi Rizzo 	if (nmd->flags & NETMAP_MEM_FINALIZED) {
12228241616dSLuigi Rizzo 		/* reset previous allocation */
12238241616dSLuigi Rizzo 		for (i = 0; i < NETMAP_POOLS_NR; i++) {
1224ce3ee1e7SLuigi Rizzo 			netmap_reset_obj_allocator(&nmd->pools[i]);
12258241616dSLuigi Rizzo 		}
1226ce3ee1e7SLuigi Rizzo 		nmd->flags &= ~NETMAP_MEM_FINALIZED;
12278241616dSLuigi Rizzo 	}
12288241616dSLuigi Rizzo 
12298241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1230ce3ee1e7SLuigi Rizzo 		nmd->lasterr = netmap_config_obj_allocator(&nmd->pools[i],
12318241616dSLuigi Rizzo 				netmap_params[i].num, netmap_params[i].size);
1232ce3ee1e7SLuigi Rizzo 		if (nmd->lasterr)
12338241616dSLuigi Rizzo 			goto out;
12348241616dSLuigi Rizzo 	}
12358241616dSLuigi Rizzo 
12368241616dSLuigi Rizzo out:
12378241616dSLuigi Rizzo 
1238ce3ee1e7SLuigi Rizzo 	return nmd->lasterr;
12398241616dSLuigi Rizzo }
12408241616dSLuigi Rizzo 
12418241616dSLuigi Rizzo static int
1242ce3ee1e7SLuigi Rizzo netmap_mem_global_finalize(struct netmap_mem_d *nmd)
12438241616dSLuigi Rizzo {
1244ce3ee1e7SLuigi Rizzo 	int err;
12458241616dSLuigi Rizzo 
1246ce3ee1e7SLuigi Rizzo 	NMA_LOCK(nmd);
1247ce3ee1e7SLuigi Rizzo 
12488241616dSLuigi Rizzo 
12498241616dSLuigi Rizzo 	/* update configuration if changed */
1250ce3ee1e7SLuigi Rizzo 	if (netmap_mem_global_config(nmd))
12518241616dSLuigi Rizzo 		goto out;
12528241616dSLuigi Rizzo 
1253ce3ee1e7SLuigi Rizzo 	nmd->refcount++;
1254ce3ee1e7SLuigi Rizzo 
1255ce3ee1e7SLuigi Rizzo 	if (nmd->flags & NETMAP_MEM_FINALIZED) {
12568241616dSLuigi Rizzo 		/* may happen if config is not changed */
12578241616dSLuigi Rizzo 		ND("nothing to do");
12588241616dSLuigi Rizzo 		goto out;
12598241616dSLuigi Rizzo 	}
12608241616dSLuigi Rizzo 
1261ce3ee1e7SLuigi Rizzo 	if (netmap_mem_finalize_all(nmd))
1262ce3ee1e7SLuigi Rizzo 		goto out;
12638241616dSLuigi Rizzo 
1264ce3ee1e7SLuigi Rizzo 	nmd->lasterr = 0;
12658241616dSLuigi Rizzo 
12668241616dSLuigi Rizzo out:
1267ce3ee1e7SLuigi Rizzo 	if (nmd->lasterr)
1268ce3ee1e7SLuigi Rizzo 		nmd->refcount--;
1269ce3ee1e7SLuigi Rizzo 	err = nmd->lasterr;
12708241616dSLuigi Rizzo 
1271ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(nmd);
12728241616dSLuigi Rizzo 
1273ce3ee1e7SLuigi Rizzo 	return err;
12748241616dSLuigi Rizzo 
1275ccdc3305SLuigi Rizzo }
1276ccdc3305SLuigi Rizzo 
1277ce3ee1e7SLuigi Rizzo int
1278ce3ee1e7SLuigi Rizzo netmap_mem_init(void)
1279ccdc3305SLuigi Rizzo {
1280ce3ee1e7SLuigi Rizzo 	NMA_LOCK_INIT(&nm_mem);
12818241616dSLuigi Rizzo 	return (0);
1282ccdc3305SLuigi Rizzo }
1283ccdc3305SLuigi Rizzo 
1284ce3ee1e7SLuigi Rizzo void
1285ce3ee1e7SLuigi Rizzo netmap_mem_fini(void)
1286ccdc3305SLuigi Rizzo {
12878241616dSLuigi Rizzo 	int i;
12888241616dSLuigi Rizzo 
12898241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
12908241616dSLuigi Rizzo 	    netmap_destroy_obj_allocator(&nm_mem.pools[i]);
12918241616dSLuigi Rizzo 	}
1292ce3ee1e7SLuigi Rizzo 	NMA_LOCK_DESTROY(&nm_mem);
12938241616dSLuigi Rizzo }
12948241616dSLuigi Rizzo 
12958241616dSLuigi Rizzo static void
12968241616dSLuigi Rizzo netmap_free_rings(struct netmap_adapter *na)
12978241616dSLuigi Rizzo {
1298f0ea3689SLuigi Rizzo 	struct netmap_kring *kring;
1299f0ea3689SLuigi Rizzo 	struct netmap_ring *ring;
1300ae10d1afSLuigi Rizzo 	if (!na->tx_rings)
1301ae10d1afSLuigi Rizzo 		return;
1302f0ea3689SLuigi Rizzo 	for (kring = na->tx_rings; kring != na->rx_rings; kring++) {
1303f0ea3689SLuigi Rizzo 		ring = kring->ring;
1304f0ea3689SLuigi Rizzo 		if (ring == NULL)
1305f0ea3689SLuigi Rizzo 			continue;
1306f0ea3689SLuigi Rizzo 		netmap_free_bufs(na->nm_mem, ring->slot, kring->nkr_num_slots);
1307f0ea3689SLuigi Rizzo 		netmap_ring_free(na->nm_mem, ring);
1308f0ea3689SLuigi Rizzo 		kring->ring = NULL;
13098241616dSLuigi Rizzo 	}
1310f0ea3689SLuigi Rizzo 	for (/* cont'd from above */; kring != na->tailroom; kring++) {
1311f0ea3689SLuigi Rizzo 		ring = kring->ring;
1312f0ea3689SLuigi Rizzo 		if (ring == NULL)
1313f0ea3689SLuigi Rizzo 			continue;
1314f0ea3689SLuigi Rizzo 		netmap_free_bufs(na->nm_mem, ring->slot, kring->nkr_num_slots);
1315f0ea3689SLuigi Rizzo 		netmap_ring_free(na->nm_mem, ring);
1316f0ea3689SLuigi Rizzo 		kring->ring = NULL;
1317ce3ee1e7SLuigi Rizzo 	}
1318ccdc3305SLuigi Rizzo }
1319ccdc3305SLuigi Rizzo 
1320f9790aebSLuigi Rizzo /* call with NMA_LOCK held *
1321f9790aebSLuigi Rizzo  *
1322f9790aebSLuigi Rizzo  * Allocate netmap rings and buffers for this card
1323f9790aebSLuigi Rizzo  * The rings are contiguous, but have variable size.
1324f0ea3689SLuigi Rizzo  * The kring array must follow the layout described
1325f0ea3689SLuigi Rizzo  * in netmap_krings_create().
1326f9790aebSLuigi Rizzo  */
1327f9790aebSLuigi Rizzo int
1328f9790aebSLuigi Rizzo netmap_mem_rings_create(struct netmap_adapter *na)
1329f9790aebSLuigi Rizzo {
1330f9790aebSLuigi Rizzo 	struct netmap_ring *ring;
1331f9790aebSLuigi Rizzo 	u_int len, ndesc;
1332f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
1333f0ea3689SLuigi Rizzo 	u_int i;
1334f9790aebSLuigi Rizzo 
1335f9790aebSLuigi Rizzo 	NMA_LOCK(na->nm_mem);
1336f9790aebSLuigi Rizzo 
1337f0ea3689SLuigi Rizzo         /* transmit rings */
1338f0ea3689SLuigi Rizzo 	for (i =0, kring = na->tx_rings; kring != na->rx_rings; kring++, i++) {
1339f0ea3689SLuigi Rizzo 		if (kring->ring) {
1340f0ea3689SLuigi Rizzo 			ND("%s %ld already created", kring->name, kring - na->tx_rings);
1341f0ea3689SLuigi Rizzo 			continue; /* already created by somebody else */
1342f0ea3689SLuigi Rizzo 		}
1343f9790aebSLuigi Rizzo 		ndesc = kring->nkr_num_slots;
1344f9790aebSLuigi Rizzo 		len = sizeof(struct netmap_ring) +
1345f9790aebSLuigi Rizzo 			  ndesc * sizeof(struct netmap_slot);
1346f9790aebSLuigi Rizzo 		ring = netmap_ring_malloc(na->nm_mem, len);
1347f9790aebSLuigi Rizzo 		if (ring == NULL) {
1348f9790aebSLuigi Rizzo 			D("Cannot allocate tx_ring");
1349f9790aebSLuigi Rizzo 			goto cleanup;
1350f9790aebSLuigi Rizzo 		}
1351f6c2a31fSLuigi Rizzo 		ND("txring at %p", ring);
1352f9790aebSLuigi Rizzo 		kring->ring = ring;
1353f9790aebSLuigi Rizzo 		*(uint32_t *)(uintptr_t)&ring->num_slots = ndesc;
135417885a7bSLuigi Rizzo 		*(int64_t *)(uintptr_t)&ring->buf_ofs =
1355f9790aebSLuigi Rizzo 		    (na->nm_mem->pools[NETMAP_IF_POOL].memtotal +
1356f9790aebSLuigi Rizzo 			na->nm_mem->pools[NETMAP_RING_POOL].memtotal) -
1357f9790aebSLuigi Rizzo 			netmap_ring_offset(na->nm_mem, ring);
1358f9790aebSLuigi Rizzo 
135917885a7bSLuigi Rizzo 		/* copy values from kring */
136017885a7bSLuigi Rizzo 		ring->head = kring->rhead;
136117885a7bSLuigi Rizzo 		ring->cur = kring->rcur;
136217885a7bSLuigi Rizzo 		ring->tail = kring->rtail;
1363f9790aebSLuigi Rizzo 		*(uint16_t *)(uintptr_t)&ring->nr_buf_size =
1364*4bf50f18SLuigi Rizzo 			netmap_mem_bufsize(na->nm_mem);
1365f0ea3689SLuigi Rizzo 		ND("%s h %d c %d t %d", kring->name,
1366f0ea3689SLuigi Rizzo 			ring->head, ring->cur, ring->tail);
1367f9790aebSLuigi Rizzo 		ND("initializing slots for txring");
1368f0ea3689SLuigi Rizzo 		if (i != na->num_tx_rings || (na->na_flags & NAF_HOST_RINGS)) {
1369f0ea3689SLuigi Rizzo 			/* this is a real ring */
1370f9790aebSLuigi Rizzo 			if (netmap_new_bufs(na->nm_mem, ring->slot, ndesc)) {
1371f9790aebSLuigi Rizzo 				D("Cannot allocate buffers for tx_ring");
1372f9790aebSLuigi Rizzo 				goto cleanup;
1373f9790aebSLuigi Rizzo 			}
1374f0ea3689SLuigi Rizzo 		} else {
1375f0ea3689SLuigi Rizzo 			/* this is a fake tx ring, set all indices to 0 */
1376f0ea3689SLuigi Rizzo 			netmap_mem_set_ring(na->nm_mem, ring->slot, ndesc, 0);
1377f0ea3689SLuigi Rizzo 		}
1378f9790aebSLuigi Rizzo 	}
1379f9790aebSLuigi Rizzo 
1380f0ea3689SLuigi Rizzo 	/* receive rings */
1381f0ea3689SLuigi Rizzo 	for ( i = 0 /* kring cont'd from above */ ; kring != na->tailroom; kring++, i++) {
1382f0ea3689SLuigi Rizzo 		if (kring->ring) {
1383f0ea3689SLuigi Rizzo 			ND("%s %ld already created", kring->name, kring - na->rx_rings);
1384f0ea3689SLuigi Rizzo 			continue; /* already created by somebody else */
1385f0ea3689SLuigi Rizzo 		}
1386f9790aebSLuigi Rizzo 		ndesc = kring->nkr_num_slots;
1387f9790aebSLuigi Rizzo 		len = sizeof(struct netmap_ring) +
1388f9790aebSLuigi Rizzo 			  ndesc * sizeof(struct netmap_slot);
1389f9790aebSLuigi Rizzo 		ring = netmap_ring_malloc(na->nm_mem, len);
1390f9790aebSLuigi Rizzo 		if (ring == NULL) {
1391f9790aebSLuigi Rizzo 			D("Cannot allocate rx_ring");
1392f9790aebSLuigi Rizzo 			goto cleanup;
1393f9790aebSLuigi Rizzo 		}
1394f6c2a31fSLuigi Rizzo 		ND("rxring at %p", ring);
1395f9790aebSLuigi Rizzo 		kring->ring = ring;
1396f9790aebSLuigi Rizzo 		*(uint32_t *)(uintptr_t)&ring->num_slots = ndesc;
139717885a7bSLuigi Rizzo 		*(int64_t *)(uintptr_t)&ring->buf_ofs =
1398f9790aebSLuigi Rizzo 		    (na->nm_mem->pools[NETMAP_IF_POOL].memtotal +
1399f9790aebSLuigi Rizzo 		        na->nm_mem->pools[NETMAP_RING_POOL].memtotal) -
1400f9790aebSLuigi Rizzo 			netmap_ring_offset(na->nm_mem, ring);
1401f9790aebSLuigi Rizzo 
140217885a7bSLuigi Rizzo 		/* copy values from kring */
140317885a7bSLuigi Rizzo 		ring->head = kring->rhead;
140417885a7bSLuigi Rizzo 		ring->cur = kring->rcur;
140517885a7bSLuigi Rizzo 		ring->tail = kring->rtail;
1406f9790aebSLuigi Rizzo 		*(int *)(uintptr_t)&ring->nr_buf_size =
1407*4bf50f18SLuigi Rizzo 			netmap_mem_bufsize(na->nm_mem);
1408f0ea3689SLuigi Rizzo 		ND("%s h %d c %d t %d", kring->name,
1409f0ea3689SLuigi Rizzo 			ring->head, ring->cur, ring->tail);
1410f6c2a31fSLuigi Rizzo 		ND("initializing slots for rxring %p", ring);
1411f0ea3689SLuigi Rizzo 		if (i != na->num_rx_rings || (na->na_flags & NAF_HOST_RINGS)) {
1412f0ea3689SLuigi Rizzo 			/* this is a real ring */
1413f9790aebSLuigi Rizzo 			if (netmap_new_bufs(na->nm_mem, ring->slot, ndesc)) {
1414f9790aebSLuigi Rizzo 				D("Cannot allocate buffers for rx_ring");
1415f9790aebSLuigi Rizzo 				goto cleanup;
1416f9790aebSLuigi Rizzo 			}
1417f0ea3689SLuigi Rizzo 		} else {
1418f0ea3689SLuigi Rizzo 			/* this is a fake rx ring, set all indices to 1 */
1419f0ea3689SLuigi Rizzo 			netmap_mem_set_ring(na->nm_mem, ring->slot, ndesc, 1);
1420f0ea3689SLuigi Rizzo 		}
1421f9790aebSLuigi Rizzo 	}
1422f9790aebSLuigi Rizzo 
1423f9790aebSLuigi Rizzo 	NMA_UNLOCK(na->nm_mem);
1424f9790aebSLuigi Rizzo 
1425f9790aebSLuigi Rizzo 	return 0;
1426f9790aebSLuigi Rizzo 
1427f9790aebSLuigi Rizzo cleanup:
1428f9790aebSLuigi Rizzo 	netmap_free_rings(na);
1429f9790aebSLuigi Rizzo 
1430f9790aebSLuigi Rizzo 	NMA_UNLOCK(na->nm_mem);
1431f9790aebSLuigi Rizzo 
1432f9790aebSLuigi Rizzo 	return ENOMEM;
1433f9790aebSLuigi Rizzo }
1434f9790aebSLuigi Rizzo 
1435f9790aebSLuigi Rizzo void
1436f9790aebSLuigi Rizzo netmap_mem_rings_delete(struct netmap_adapter *na)
1437f9790aebSLuigi Rizzo {
1438f9790aebSLuigi Rizzo 	/* last instance, release bufs and rings */
1439f9790aebSLuigi Rizzo 	NMA_LOCK(na->nm_mem);
1440f9790aebSLuigi Rizzo 
1441f9790aebSLuigi Rizzo 	netmap_free_rings(na);
1442f9790aebSLuigi Rizzo 
1443f9790aebSLuigi Rizzo 	NMA_UNLOCK(na->nm_mem);
1444f9790aebSLuigi Rizzo }
1445ccdc3305SLuigi Rizzo 
1446ccdc3305SLuigi Rizzo 
14478241616dSLuigi Rizzo /* call with NMA_LOCK held */
1448ae10d1afSLuigi Rizzo /*
1449ae10d1afSLuigi Rizzo  * Allocate the per-fd structure netmap_if.
1450ce3ee1e7SLuigi Rizzo  *
1451ce3ee1e7SLuigi Rizzo  * We assume that the configuration stored in na
1452ce3ee1e7SLuigi Rizzo  * (number of tx/rx rings and descs) does not change while
1453ce3ee1e7SLuigi Rizzo  * the interface is in netmap mode.
1454ae10d1afSLuigi Rizzo  */
1455ce3ee1e7SLuigi Rizzo struct netmap_if *
1456*4bf50f18SLuigi Rizzo netmap_mem_if_new(struct netmap_adapter *na)
1457ccdc3305SLuigi Rizzo {
1458ccdc3305SLuigi Rizzo 	struct netmap_if *nifp;
1459ccdc3305SLuigi Rizzo 	ssize_t base; /* handy for relative offsets between rings and nifp */
1460f9790aebSLuigi Rizzo 	u_int i, len, ntx, nrx;
1461ccdc3305SLuigi Rizzo 
1462f0ea3689SLuigi Rizzo 	/* account for the (eventually fake) host rings */
1463f0ea3689SLuigi Rizzo 	ntx = na->num_tx_rings + 1;
1464f0ea3689SLuigi Rizzo 	nrx = na->num_rx_rings + 1;
1465ccdc3305SLuigi Rizzo 	/*
1466ccdc3305SLuigi Rizzo 	 * the descriptor is followed inline by an array of offsets
1467ccdc3305SLuigi Rizzo 	 * to the tx and rx rings in the shared memory region.
1468ccdc3305SLuigi Rizzo 	 */
1469ce3ee1e7SLuigi Rizzo 
1470ce3ee1e7SLuigi Rizzo 	NMA_LOCK(na->nm_mem);
1471ce3ee1e7SLuigi Rizzo 
1472ccdc3305SLuigi Rizzo 	len = sizeof(struct netmap_if) + (nrx + ntx) * sizeof(ssize_t);
1473ce3ee1e7SLuigi Rizzo 	nifp = netmap_if_malloc(na->nm_mem, len);
1474ccdc3305SLuigi Rizzo 	if (nifp == NULL) {
1475ce3ee1e7SLuigi Rizzo 		NMA_UNLOCK(na->nm_mem);
1476ccdc3305SLuigi Rizzo 		return NULL;
1477ccdc3305SLuigi Rizzo 	}
1478ccdc3305SLuigi Rizzo 
1479ccdc3305SLuigi Rizzo 	/* initialize base fields -- override const */
1480ce3ee1e7SLuigi Rizzo 	*(u_int *)(uintptr_t)&nifp->ni_tx_rings = na->num_tx_rings;
1481ce3ee1e7SLuigi Rizzo 	*(u_int *)(uintptr_t)&nifp->ni_rx_rings = na->num_rx_rings;
1482*4bf50f18SLuigi Rizzo 	strncpy(nifp->ni_name, na->name, (size_t)IFNAMSIZ);
1483ccdc3305SLuigi Rizzo 
1484ccdc3305SLuigi Rizzo 	/*
1485ccdc3305SLuigi Rizzo 	 * fill the slots for the rx and tx rings. They contain the offset
1486ccdc3305SLuigi Rizzo 	 * between the ring and nifp, so the information is usable in
1487ccdc3305SLuigi Rizzo 	 * userspace to reach the ring from the nifp.
1488ccdc3305SLuigi Rizzo 	 */
1489ce3ee1e7SLuigi Rizzo 	base = netmap_if_offset(na->nm_mem, nifp);
1490ccdc3305SLuigi Rizzo 	for (i = 0; i < ntx; i++) {
1491ccdc3305SLuigi Rizzo 		*(ssize_t *)(uintptr_t)&nifp->ring_ofs[i] =
1492ce3ee1e7SLuigi Rizzo 			netmap_ring_offset(na->nm_mem, na->tx_rings[i].ring) - base;
1493ccdc3305SLuigi Rizzo 	}
1494ccdc3305SLuigi Rizzo 	for (i = 0; i < nrx; i++) {
1495ccdc3305SLuigi Rizzo 		*(ssize_t *)(uintptr_t)&nifp->ring_ofs[i+ntx] =
1496ce3ee1e7SLuigi Rizzo 			netmap_ring_offset(na->nm_mem, na->rx_rings[i].ring) - base;
1497ccdc3305SLuigi Rizzo 	}
1498ce3ee1e7SLuigi Rizzo 
1499ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(na->nm_mem);
1500ce3ee1e7SLuigi Rizzo 
1501ccdc3305SLuigi Rizzo 	return (nifp);
1502ccdc3305SLuigi Rizzo }
1503ccdc3305SLuigi Rizzo 
1504ce3ee1e7SLuigi Rizzo void
1505ce3ee1e7SLuigi Rizzo netmap_mem_if_delete(struct netmap_adapter *na, struct netmap_if *nifp)
1506ccdc3305SLuigi Rizzo {
1507ce3ee1e7SLuigi Rizzo 	if (nifp == NULL)
1508ce3ee1e7SLuigi Rizzo 		/* nothing to do */
1509ce3ee1e7SLuigi Rizzo 		return;
1510ce3ee1e7SLuigi Rizzo 	NMA_LOCK(na->nm_mem);
1511f0ea3689SLuigi Rizzo 	if (nifp->ni_bufs_head)
1512f0ea3689SLuigi Rizzo 		netmap_extra_free(na, nifp->ni_bufs_head);
1513ce3ee1e7SLuigi Rizzo 	netmap_if_free(na->nm_mem, nifp);
1514ce3ee1e7SLuigi Rizzo 
1515ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(na->nm_mem);
1516ce3ee1e7SLuigi Rizzo }
1517ce3ee1e7SLuigi Rizzo 
1518ce3ee1e7SLuigi Rizzo static void
1519ce3ee1e7SLuigi Rizzo netmap_mem_global_deref(struct netmap_mem_d *nmd)
1520ce3ee1e7SLuigi Rizzo {
1521ce3ee1e7SLuigi Rizzo 	NMA_LOCK(nmd);
1522ce3ee1e7SLuigi Rizzo 
1523ce3ee1e7SLuigi Rizzo 	nmd->refcount--;
1524*4bf50f18SLuigi Rizzo 	if (!nmd->refcount)
1525*4bf50f18SLuigi Rizzo 		nmd->nm_grp = -1;
1526ae10d1afSLuigi Rizzo 	if (netmap_verbose)
1527ce3ee1e7SLuigi Rizzo 		D("refcount = %d", nmd->refcount);
1528ce3ee1e7SLuigi Rizzo 
1529ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(nmd);
1530ce3ee1e7SLuigi Rizzo }
1531ce3ee1e7SLuigi Rizzo 
1532f9790aebSLuigi Rizzo int
1533*4bf50f18SLuigi Rizzo netmap_mem_finalize(struct netmap_mem_d *nmd, struct netmap_adapter *na)
1534ce3ee1e7SLuigi Rizzo {
1535*4bf50f18SLuigi Rizzo 	if (nm_mem_assign_group(nmd, na->pdev) < 0) {
1536*4bf50f18SLuigi Rizzo 		return ENOMEM;
1537*4bf50f18SLuigi Rizzo 	} else {
1538*4bf50f18SLuigi Rizzo 		nmd->finalize(nmd);
1539*4bf50f18SLuigi Rizzo 	}
1540*4bf50f18SLuigi Rizzo 
1541*4bf50f18SLuigi Rizzo 	if (!nmd->lasterr && na->pdev)
1542*4bf50f18SLuigi Rizzo 		netmap_mem_map(&nmd->pools[NETMAP_BUF_POOL], na);
1543*4bf50f18SLuigi Rizzo 
1544*4bf50f18SLuigi Rizzo 	return nmd->lasterr;
1545ce3ee1e7SLuigi Rizzo }
1546ce3ee1e7SLuigi Rizzo 
1547f9790aebSLuigi Rizzo void
1548*4bf50f18SLuigi Rizzo netmap_mem_deref(struct netmap_mem_d *nmd, struct netmap_adapter *na)
1549ce3ee1e7SLuigi Rizzo {
1550*4bf50f18SLuigi Rizzo 	NMA_LOCK(nmd);
1551*4bf50f18SLuigi Rizzo 	netmap_mem_unmap(&nmd->pools[NETMAP_BUF_POOL], na);
1552*4bf50f18SLuigi Rizzo 	NMA_UNLOCK(nmd);
1553ce3ee1e7SLuigi Rizzo 	return nmd->deref(nmd);
1554ccdc3305SLuigi Rizzo }
1555