xref: /freebsd-14.2/sys/dev/netmap/netmap_mem2.c (revision dd4fcbc5)
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 
574bf50f18SLuigi Rizzo #define NETMAP_BUF_MAX_NUM	20*4096*2	/* large machine */
584bf50f18SLuigi Rizzo 
594bf50f18SLuigi Rizzo #define NETMAP_POOL_MAX_NAMSZ	32
604bf50f18SLuigi Rizzo 
614bf50f18SLuigi Rizzo 
624bf50f18SLuigi Rizzo enum {
634bf50f18SLuigi Rizzo 	NETMAP_IF_POOL   = 0,
644bf50f18SLuigi Rizzo 	NETMAP_RING_POOL,
654bf50f18SLuigi Rizzo 	NETMAP_BUF_POOL,
664bf50f18SLuigi Rizzo 	NETMAP_POOLS_NR
674bf50f18SLuigi Rizzo };
684bf50f18SLuigi Rizzo 
694bf50f18SLuigi Rizzo 
704bf50f18SLuigi Rizzo struct netmap_obj_params {
714bf50f18SLuigi Rizzo 	u_int size;
724bf50f18SLuigi Rizzo 	u_int num;
734bf50f18SLuigi Rizzo };
744bf50f18SLuigi Rizzo struct netmap_obj_pool {
754bf50f18SLuigi Rizzo 	char name[NETMAP_POOL_MAX_NAMSZ];	/* name of the allocator */
764bf50f18SLuigi Rizzo 
774bf50f18SLuigi Rizzo 	/* ---------------------------------------------------*/
784bf50f18SLuigi Rizzo 	/* these are only meaningful if the pool is finalized */
794bf50f18SLuigi Rizzo 	/* (see 'finalized' field in netmap_mem_d)            */
804bf50f18SLuigi Rizzo 	u_int objtotal;         /* actual total number of objects. */
814bf50f18SLuigi Rizzo 	u_int memtotal;		/* actual total memory space */
824bf50f18SLuigi Rizzo 	u_int numclusters;	/* actual number of clusters */
834bf50f18SLuigi Rizzo 
844bf50f18SLuigi Rizzo 	u_int objfree;          /* number of free objects. */
854bf50f18SLuigi Rizzo 
864bf50f18SLuigi Rizzo 	struct lut_entry *lut;  /* virt,phys addresses, objtotal entries */
874bf50f18SLuigi Rizzo 	uint32_t *bitmap;       /* one bit per buffer, 1 means free */
884bf50f18SLuigi Rizzo 	uint32_t bitmap_slots;	/* number of uint32 entries in bitmap */
894bf50f18SLuigi Rizzo 	/* ---------------------------------------------------*/
904bf50f18SLuigi Rizzo 
914bf50f18SLuigi Rizzo 	/* limits */
924bf50f18SLuigi Rizzo 	u_int objminsize;	/* minimum object size */
934bf50f18SLuigi Rizzo 	u_int objmaxsize;	/* maximum object size */
944bf50f18SLuigi Rizzo 	u_int nummin;		/* minimum number of objects */
954bf50f18SLuigi Rizzo 	u_int nummax;		/* maximum number of objects */
964bf50f18SLuigi Rizzo 
974bf50f18SLuigi Rizzo 	/* these are changed only by config */
984bf50f18SLuigi Rizzo 	u_int _objtotal;	/* total number of objects */
994bf50f18SLuigi Rizzo 	u_int _objsize;		/* object size */
1004bf50f18SLuigi Rizzo 	u_int _clustsize;       /* cluster size */
1014bf50f18SLuigi Rizzo 	u_int _clustentries;    /* objects per cluster */
1024bf50f18SLuigi Rizzo 	u_int _numclusters;	/* number of clusters */
1034bf50f18SLuigi Rizzo 
1044bf50f18SLuigi Rizzo 	/* requested values */
1054bf50f18SLuigi Rizzo 	u_int r_objtotal;
1064bf50f18SLuigi Rizzo 	u_int r_objsize;
1074bf50f18SLuigi Rizzo };
1084bf50f18SLuigi Rizzo 
1094bf50f18SLuigi Rizzo #ifdef linux
1104bf50f18SLuigi Rizzo // XXX a mtx would suffice here 20130415 lr
1114bf50f18SLuigi Rizzo #define NMA_LOCK_T		struct semaphore
1124bf50f18SLuigi Rizzo #else /* !linux */
1134bf50f18SLuigi Rizzo #define NMA_LOCK_T		struct mtx
1144bf50f18SLuigi Rizzo #endif /* linux */
1154bf50f18SLuigi Rizzo 
1164bf50f18SLuigi Rizzo typedef int (*netmap_mem_config_t)(struct netmap_mem_d*);
1174bf50f18SLuigi Rizzo typedef int (*netmap_mem_finalize_t)(struct netmap_mem_d*);
1184bf50f18SLuigi Rizzo typedef void (*netmap_mem_deref_t)(struct netmap_mem_d*);
1194bf50f18SLuigi Rizzo 
1204bf50f18SLuigi Rizzo typedef uint16_t nm_memid_t;
1214bf50f18SLuigi Rizzo 
1224bf50f18SLuigi Rizzo struct netmap_mem_d {
1234bf50f18SLuigi Rizzo 	NMA_LOCK_T nm_mtx;  /* protect the allocator */
1244bf50f18SLuigi Rizzo 	u_int nm_totalsize; /* shorthand */
1254bf50f18SLuigi Rizzo 
1264bf50f18SLuigi Rizzo 	u_int flags;
1274bf50f18SLuigi Rizzo #define NETMAP_MEM_FINALIZED	0x1	/* preallocation done */
1284bf50f18SLuigi Rizzo 	int lasterr;		/* last error for curr config */
1294bf50f18SLuigi Rizzo 	int refcount;		/* existing priv structures */
1304bf50f18SLuigi Rizzo 	/* the three allocators */
1314bf50f18SLuigi Rizzo 	struct netmap_obj_pool pools[NETMAP_POOLS_NR];
1324bf50f18SLuigi Rizzo 
133*dd4fcbc5SPatrick Kelsey 	netmap_mem_config_t   config;	/* called with NMA_LOCK held */
134*dd4fcbc5SPatrick Kelsey 	netmap_mem_finalize_t finalize;	/* called with NMA_LOCK held */
135*dd4fcbc5SPatrick Kelsey 	netmap_mem_deref_t    deref;	/* called with NMA_LOCK held */
1364bf50f18SLuigi Rizzo 
1374bf50f18SLuigi Rizzo 	nm_memid_t nm_id;	/* allocator identifier */
1384bf50f18SLuigi Rizzo 	int nm_grp;	/* iommu groupd id */
1394bf50f18SLuigi Rizzo 
1404bf50f18SLuigi Rizzo 	/* list of all existing allocators, sorted by nm_id */
1414bf50f18SLuigi Rizzo 	struct netmap_mem_d *prev, *next;
1424bf50f18SLuigi Rizzo };
1434bf50f18SLuigi Rizzo 
1444bf50f18SLuigi Rizzo /* accessor functions */
1454bf50f18SLuigi Rizzo struct lut_entry*
1464bf50f18SLuigi Rizzo netmap_mem_get_lut(struct netmap_mem_d *nmd)
1474bf50f18SLuigi Rizzo {
1484bf50f18SLuigi Rizzo 	return nmd->pools[NETMAP_BUF_POOL].lut;
1494bf50f18SLuigi Rizzo }
1504bf50f18SLuigi Rizzo 
1514bf50f18SLuigi Rizzo u_int
1524bf50f18SLuigi Rizzo netmap_mem_get_buftotal(struct netmap_mem_d *nmd)
1534bf50f18SLuigi Rizzo {
1544bf50f18SLuigi Rizzo 	return nmd->pools[NETMAP_BUF_POOL].objtotal;
1554bf50f18SLuigi Rizzo }
1564bf50f18SLuigi Rizzo 
1574bf50f18SLuigi Rizzo size_t
1584bf50f18SLuigi Rizzo netmap_mem_get_bufsize(struct netmap_mem_d *nmd)
1594bf50f18SLuigi Rizzo {
1604bf50f18SLuigi Rizzo 	return nmd->pools[NETMAP_BUF_POOL]._objsize;
1614bf50f18SLuigi Rizzo }
1624bf50f18SLuigi 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,
2444bf50f18SLuigi 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 
3614bf50f18SLuigi Rizzo static int
3624bf50f18SLuigi Rizzo nm_mem_assign_group(struct netmap_mem_d *nmd, struct device *dev)
3634bf50f18SLuigi Rizzo {
3644bf50f18SLuigi Rizzo 	int err = 0, id;
3654bf50f18SLuigi Rizzo 	id = nm_iommu_group_id(dev);
3664bf50f18SLuigi Rizzo 	if (netmap_verbose)
3674bf50f18SLuigi Rizzo 		D("iommu_group %d", id);
3684bf50f18SLuigi Rizzo 
3694bf50f18SLuigi Rizzo 	NMA_LOCK(nmd);
3704bf50f18SLuigi Rizzo 
3714bf50f18SLuigi Rizzo 	if (nmd->nm_grp < 0)
3724bf50f18SLuigi Rizzo 		nmd->nm_grp = id;
3734bf50f18SLuigi Rizzo 
3744bf50f18SLuigi Rizzo 	if (nmd->nm_grp != id)
3754bf50f18SLuigi Rizzo 		nmd->lasterr = err = ENOMEM;
3764bf50f18SLuigi Rizzo 
3774bf50f18SLuigi Rizzo 	NMA_UNLOCK(nmd);
3784bf50f18SLuigi Rizzo 	return err;
3794bf50f18SLuigi 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
4004bf50f18SLuigi 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;
4264bf50f18SLuigi 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 		}
4374bf50f18SLuigi Rizzo 	}
4384bf50f18SLuigi Rizzo 	if (memflags)
439ce3ee1e7SLuigi Rizzo 		*memflags = nmd->flags;
4404bf50f18SLuigi 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 
6014bf50f18SLuigi Rizzo #define netmap_mem_bufsize(n)	\
6024bf50f18SLuigi Rizzo 	((n)->pools[NETMAP_BUF_POOL]._objsize)
6034bf50f18SLuigi 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)			\
6094bf50f18SLuigi 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 
754*dd4fcbc5SPatrick Kelsey 		/*
755*dd4fcbc5SPatrick Kelsey 		 * Free each cluster allocated in
756*dd4fcbc5SPatrick Kelsey 		 * netmap_finalize_obj_allocator().  The cluster start
757*dd4fcbc5SPatrick Kelsey 		 * addresses are stored at multiples of p->_clusterentries
758*dd4fcbc5SPatrick Kelsey 		 * in the lut.
759*dd4fcbc5SPatrick Kelsey 		 */
760ce3ee1e7SLuigi Rizzo 		for (i = 0; i < p->objtotal; i += p->_clustentries) {
7618241616dSLuigi Rizzo 			if (p->lut[i].vaddr)
762ce3ee1e7SLuigi Rizzo 				contigfree(p->lut[i].vaddr, sz, M_NETMAP);
7638241616dSLuigi Rizzo 		}
7648241616dSLuigi Rizzo 		bzero(p->lut, sizeof(struct lut_entry) * p->objtotal);
7658241616dSLuigi Rizzo #ifdef linux
7668241616dSLuigi Rizzo 		vfree(p->lut);
7678241616dSLuigi Rizzo #else
7688241616dSLuigi Rizzo 		free(p->lut, M_NETMAP);
7698241616dSLuigi Rizzo #endif
7708241616dSLuigi Rizzo 	}
7718241616dSLuigi Rizzo 	p->lut = NULL;
772ce3ee1e7SLuigi Rizzo 	p->objtotal = 0;
773ce3ee1e7SLuigi Rizzo 	p->memtotal = 0;
774ce3ee1e7SLuigi Rizzo 	p->numclusters = 0;
775ce3ee1e7SLuigi Rizzo 	p->objfree = 0;
7768241616dSLuigi Rizzo }
777ccdc3305SLuigi Rizzo 
778ccdc3305SLuigi Rizzo /*
779ccdc3305SLuigi Rizzo  * Free all resources related to an allocator.
780ccdc3305SLuigi Rizzo  */
781ccdc3305SLuigi Rizzo static void
782ccdc3305SLuigi Rizzo netmap_destroy_obj_allocator(struct netmap_obj_pool *p)
783ccdc3305SLuigi Rizzo {
784ccdc3305SLuigi Rizzo 	if (p == NULL)
785ccdc3305SLuigi Rizzo 		return;
7868241616dSLuigi Rizzo 	netmap_reset_obj_allocator(p);
787ccdc3305SLuigi Rizzo }
788ccdc3305SLuigi Rizzo 
789ccdc3305SLuigi Rizzo /*
790ccdc3305SLuigi Rizzo  * We receive a request for objtotal objects, of size objsize each.
791ccdc3305SLuigi Rizzo  * Internally we may round up both numbers, as we allocate objects
792ccdc3305SLuigi Rizzo  * in small clusters multiple of the page size.
793ce3ee1e7SLuigi Rizzo  * We need to keep track of objtotal and clustentries,
794ccdc3305SLuigi Rizzo  * as they are needed when freeing memory.
795ccdc3305SLuigi Rizzo  *
796ccdc3305SLuigi Rizzo  * XXX note -- userspace needs the buffers to be contiguous,
797ccdc3305SLuigi Rizzo  *	so we cannot afford gaps at the end of a cluster.
798ccdc3305SLuigi Rizzo  */
7998241616dSLuigi Rizzo 
8008241616dSLuigi Rizzo 
8018241616dSLuigi Rizzo /* call with NMA_LOCK held */
8028241616dSLuigi Rizzo static int
8038241616dSLuigi Rizzo netmap_config_obj_allocator(struct netmap_obj_pool *p, u_int objtotal, u_int objsize)
804ccdc3305SLuigi Rizzo {
805ce3ee1e7SLuigi Rizzo 	int i;
806ccdc3305SLuigi Rizzo 	u_int clustsize;	/* the cluster size, multiple of page size */
807ccdc3305SLuigi Rizzo 	u_int clustentries;	/* how many objects per entry */
808ccdc3305SLuigi Rizzo 
809ce3ee1e7SLuigi Rizzo 	/* we store the current request, so we can
810ce3ee1e7SLuigi Rizzo 	 * detect configuration changes later */
811ce3ee1e7SLuigi Rizzo 	p->r_objtotal = objtotal;
812ce3ee1e7SLuigi Rizzo 	p->r_objsize = objsize;
813ce3ee1e7SLuigi Rizzo 
8144bf50f18SLuigi Rizzo #define MAX_CLUSTSIZE	(1<<22)		// 4 MB
81517885a7bSLuigi Rizzo #define LINE_ROUND	NM_CACHE_ALIGN	// 64
816ccdc3305SLuigi Rizzo 	if (objsize >= MAX_CLUSTSIZE) {
817ccdc3305SLuigi Rizzo 		/* we could do it but there is no point */
818ccdc3305SLuigi Rizzo 		D("unsupported allocation for %d bytes", objsize);
819ce3ee1e7SLuigi Rizzo 		return EINVAL;
820ccdc3305SLuigi Rizzo 	}
821ccdc3305SLuigi Rizzo 	/* make sure objsize is a multiple of LINE_ROUND */
822ccdc3305SLuigi Rizzo 	i = (objsize & (LINE_ROUND - 1));
823ccdc3305SLuigi Rizzo 	if (i) {
824ccdc3305SLuigi Rizzo 		D("XXX aligning object by %d bytes", LINE_ROUND - i);
825ccdc3305SLuigi Rizzo 		objsize += LINE_ROUND - i;
826ccdc3305SLuigi Rizzo 	}
8278241616dSLuigi Rizzo 	if (objsize < p->objminsize || objsize > p->objmaxsize) {
8288241616dSLuigi Rizzo 		D("requested objsize %d out of range [%d, %d]",
8298241616dSLuigi Rizzo 			objsize, p->objminsize, p->objmaxsize);
830ce3ee1e7SLuigi Rizzo 		return EINVAL;
8318241616dSLuigi Rizzo 	}
8328241616dSLuigi Rizzo 	if (objtotal < p->nummin || objtotal > p->nummax) {
8338241616dSLuigi Rizzo 		D("requested objtotal %d out of range [%d, %d]",
8348241616dSLuigi Rizzo 			objtotal, p->nummin, p->nummax);
835ce3ee1e7SLuigi Rizzo 		return EINVAL;
8368241616dSLuigi Rizzo 	}
837ccdc3305SLuigi Rizzo 	/*
838ccdc3305SLuigi Rizzo 	 * Compute number of objects using a brute-force approach:
839ccdc3305SLuigi Rizzo 	 * given a max cluster size,
840ccdc3305SLuigi Rizzo 	 * we try to fill it with objects keeping track of the
841ccdc3305SLuigi Rizzo 	 * wasted space to the next page boundary.
842ccdc3305SLuigi Rizzo 	 */
843ccdc3305SLuigi Rizzo 	for (clustentries = 0, i = 1;; i++) {
844ccdc3305SLuigi Rizzo 		u_int delta, used = i * objsize;
845ccdc3305SLuigi Rizzo 		if (used > MAX_CLUSTSIZE)
846ccdc3305SLuigi Rizzo 			break;
847ccdc3305SLuigi Rizzo 		delta = used % PAGE_SIZE;
848ccdc3305SLuigi Rizzo 		if (delta == 0) { // exact solution
849ccdc3305SLuigi Rizzo 			clustentries = i;
850ccdc3305SLuigi Rizzo 			break;
851ccdc3305SLuigi Rizzo 		}
852ccdc3305SLuigi Rizzo 	}
8534bf50f18SLuigi Rizzo 	/* exact solution not found */
8544bf50f18SLuigi Rizzo 	if (clustentries == 0) {
8554bf50f18SLuigi Rizzo 		D("unsupported allocation for %d bytes", objsize);
8564bf50f18SLuigi Rizzo 		return EINVAL;
8574bf50f18SLuigi Rizzo 	}
8584bf50f18SLuigi Rizzo 	/* compute clustsize */
859ccdc3305SLuigi Rizzo 	clustsize = clustentries * objsize;
860ae10d1afSLuigi Rizzo 	if (netmap_verbose)
861ccdc3305SLuigi Rizzo 		D("objsize %d clustsize %d objects %d",
862ccdc3305SLuigi Rizzo 			objsize, clustsize, clustentries);
863ccdc3305SLuigi Rizzo 
864ccdc3305SLuigi Rizzo 	/*
865ccdc3305SLuigi Rizzo 	 * The number of clusters is n = ceil(objtotal/clustentries)
866ccdc3305SLuigi Rizzo 	 * objtotal' = n * clustentries
867ccdc3305SLuigi Rizzo 	 */
868ce3ee1e7SLuigi Rizzo 	p->_clustentries = clustentries;
869ccdc3305SLuigi Rizzo 	p->_clustsize = clustsize;
870ce3ee1e7SLuigi Rizzo 	p->_numclusters = (objtotal + clustentries - 1) / clustentries;
871ce3ee1e7SLuigi Rizzo 
872ce3ee1e7SLuigi Rizzo 	/* actual values (may be larger than requested) */
8738241616dSLuigi Rizzo 	p->_objsize = objsize;
874ce3ee1e7SLuigi Rizzo 	p->_objtotal = p->_numclusters * clustentries;
875ccdc3305SLuigi Rizzo 
8768241616dSLuigi Rizzo 	return 0;
8778241616dSLuigi Rizzo }
8788241616dSLuigi Rizzo 
8798241616dSLuigi Rizzo 
8808241616dSLuigi Rizzo /* call with NMA_LOCK held */
8818241616dSLuigi Rizzo static int
8828241616dSLuigi Rizzo netmap_finalize_obj_allocator(struct netmap_obj_pool *p)
8838241616dSLuigi Rizzo {
884ce3ee1e7SLuigi Rizzo 	int i; /* must be signed */
885ce3ee1e7SLuigi Rizzo 	size_t n;
886ce3ee1e7SLuigi Rizzo 
887ce3ee1e7SLuigi Rizzo 	/* optimistically assume we have enough memory */
888ce3ee1e7SLuigi Rizzo 	p->numclusters = p->_numclusters;
889ce3ee1e7SLuigi Rizzo 	p->objtotal = p->_objtotal;
8908241616dSLuigi Rizzo 
8918241616dSLuigi Rizzo 	n = sizeof(struct lut_entry) * p->objtotal;
8928241616dSLuigi Rizzo #ifdef linux
8938241616dSLuigi Rizzo 	p->lut = vmalloc(n);
8948241616dSLuigi Rizzo #else
895d2b91851SEd Maste 	p->lut = malloc(n, M_NETMAP, M_NOWAIT | M_ZERO);
8968241616dSLuigi Rizzo #endif
897ccdc3305SLuigi Rizzo 	if (p->lut == NULL) {
898ce3ee1e7SLuigi Rizzo 		D("Unable to create lookup table (%d bytes) for '%s'", (int)n, p->name);
899ccdc3305SLuigi Rizzo 		goto clean;
900ccdc3305SLuigi Rizzo 	}
901ccdc3305SLuigi Rizzo 
902ccdc3305SLuigi Rizzo 	/* Allocate the bitmap */
903ccdc3305SLuigi Rizzo 	n = (p->objtotal + 31) / 32;
904d2b91851SEd Maste 	p->bitmap = malloc(sizeof(uint32_t) * n, M_NETMAP, M_NOWAIT | M_ZERO);
905ccdc3305SLuigi Rizzo 	if (p->bitmap == NULL) {
906ce3ee1e7SLuigi Rizzo 		D("Unable to create bitmap (%d entries) for allocator '%s'", (int)n,
9078241616dSLuigi Rizzo 		    p->name);
908ccdc3305SLuigi Rizzo 		goto clean;
909ccdc3305SLuigi Rizzo 	}
9108241616dSLuigi Rizzo 	p->bitmap_slots = n;
911ccdc3305SLuigi Rizzo 
912ccdc3305SLuigi Rizzo 	/*
913ccdc3305SLuigi Rizzo 	 * Allocate clusters, init pointers and bitmap
914ccdc3305SLuigi Rizzo 	 */
915ce3ee1e7SLuigi Rizzo 
916ce3ee1e7SLuigi Rizzo 	n = p->_clustsize;
917ce3ee1e7SLuigi Rizzo 	for (i = 0; i < (int)p->objtotal;) {
918ce3ee1e7SLuigi Rizzo 		int lim = i + p->_clustentries;
919ccdc3305SLuigi Rizzo 		char *clust;
920ccdc3305SLuigi Rizzo 
921ce3ee1e7SLuigi Rizzo 		clust = contigmalloc(n, M_NETMAP, M_NOWAIT | M_ZERO,
922ce3ee1e7SLuigi Rizzo 		    (size_t)0, -1UL, PAGE_SIZE, 0);
923ccdc3305SLuigi Rizzo 		if (clust == NULL) {
924ccdc3305SLuigi Rizzo 			/*
925ccdc3305SLuigi Rizzo 			 * If we get here, there is a severe memory shortage,
926ccdc3305SLuigi Rizzo 			 * so halve the allocated memory to reclaim some.
927ccdc3305SLuigi Rizzo 			 */
928ccdc3305SLuigi Rizzo 			D("Unable to create cluster at %d for '%s' allocator",
9298241616dSLuigi Rizzo 			    i, p->name);
930ce3ee1e7SLuigi Rizzo 			if (i < 2) /* nothing to halve */
931ce3ee1e7SLuigi Rizzo 				goto out;
932ccdc3305SLuigi Rizzo 			lim = i / 2;
9338241616dSLuigi Rizzo 			for (i--; i >= lim; i--) {
934ccdc3305SLuigi Rizzo 				p->bitmap[ (i>>5) ] &=  ~( 1 << (i & 31) );
935ce3ee1e7SLuigi Rizzo 				if (i % p->_clustentries == 0 && p->lut[i].vaddr)
936ccdc3305SLuigi Rizzo 					contigfree(p->lut[i].vaddr,
937ce3ee1e7SLuigi Rizzo 						n, M_NETMAP);
938*dd4fcbc5SPatrick Kelsey 				p->lut[i].vaddr = NULL;
939ccdc3305SLuigi Rizzo 			}
940ce3ee1e7SLuigi Rizzo 		out:
941ccdc3305SLuigi Rizzo 			p->objtotal = i;
942ce3ee1e7SLuigi Rizzo 			/* we may have stopped in the middle of a cluster */
943ce3ee1e7SLuigi Rizzo 			p->numclusters = (i + p->_clustentries - 1) / p->_clustentries;
944ccdc3305SLuigi Rizzo 			break;
945ccdc3305SLuigi Rizzo 		}
946*dd4fcbc5SPatrick Kelsey 		/*
947*dd4fcbc5SPatrick Kelsey 		 * Set bitmap and lut state for all buffers in the current
948*dd4fcbc5SPatrick Kelsey 		 * cluster.
949*dd4fcbc5SPatrick Kelsey 		 *
950*dd4fcbc5SPatrick Kelsey 		 * [i, lim) is the set of buffer indexes that cover the
951*dd4fcbc5SPatrick Kelsey 		 * current cluster.
952*dd4fcbc5SPatrick Kelsey 		 *
953*dd4fcbc5SPatrick Kelsey 		 * 'clust' is really the address of the current buffer in
954*dd4fcbc5SPatrick Kelsey 		 * the current cluster as we index through it with a stride
955*dd4fcbc5SPatrick Kelsey 		 * of p->_objsize.
956*dd4fcbc5SPatrick Kelsey 		 */
9578241616dSLuigi Rizzo 		for (; i < lim; i++, clust += p->_objsize) {
958ccdc3305SLuigi Rizzo 			p->bitmap[ (i>>5) ] |=  ( 1 << (i & 31) );
959ccdc3305SLuigi Rizzo 			p->lut[i].vaddr = clust;
960ccdc3305SLuigi Rizzo 			p->lut[i].paddr = vtophys(clust);
961ccdc3305SLuigi Rizzo 		}
962ccdc3305SLuigi Rizzo 	}
963ce3ee1e7SLuigi Rizzo 	p->objfree = p->objtotal;
964ce3ee1e7SLuigi Rizzo 	p->memtotal = p->numclusters * p->_clustsize;
965ce3ee1e7SLuigi Rizzo 	if (p->objfree == 0)
966ce3ee1e7SLuigi Rizzo 		goto clean;
967ae10d1afSLuigi Rizzo 	if (netmap_verbose)
968ccdc3305SLuigi Rizzo 		D("Pre-allocated %d clusters (%d/%dKB) for '%s'",
969ce3ee1e7SLuigi Rizzo 		    p->numclusters, p->_clustsize >> 10,
970ce3ee1e7SLuigi Rizzo 		    p->memtotal >> 10, p->name);
971ccdc3305SLuigi Rizzo 
9728241616dSLuigi Rizzo 	return 0;
973ccdc3305SLuigi Rizzo 
974ccdc3305SLuigi Rizzo clean:
9758241616dSLuigi Rizzo 	netmap_reset_obj_allocator(p);
9768241616dSLuigi Rizzo 	return ENOMEM;
9778241616dSLuigi Rizzo }
9788241616dSLuigi Rizzo 
9798241616dSLuigi Rizzo /* call with lock held */
9808241616dSLuigi Rizzo static int
981ce3ee1e7SLuigi Rizzo netmap_memory_config_changed(struct netmap_mem_d *nmd)
9828241616dSLuigi Rizzo {
9838241616dSLuigi Rizzo 	int i;
9848241616dSLuigi Rizzo 
9858241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
986ce3ee1e7SLuigi Rizzo 		if (nmd->pools[i].r_objsize != netmap_params[i].size ||
987ce3ee1e7SLuigi Rizzo 		    nmd->pools[i].r_objtotal != netmap_params[i].num)
9888241616dSLuigi Rizzo 		    return 1;
9898241616dSLuigi Rizzo 	}
9908241616dSLuigi Rizzo 	return 0;
9918241616dSLuigi Rizzo }
9928241616dSLuigi Rizzo 
993ce3ee1e7SLuigi Rizzo static void
994ce3ee1e7SLuigi Rizzo netmap_mem_reset_all(struct netmap_mem_d *nmd)
995ce3ee1e7SLuigi Rizzo {
996ce3ee1e7SLuigi Rizzo 	int i;
997f0ea3689SLuigi Rizzo 
998f0ea3689SLuigi Rizzo 	if (netmap_verbose)
999ce3ee1e7SLuigi Rizzo 		D("resetting %p", nmd);
1000ce3ee1e7SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1001ce3ee1e7SLuigi Rizzo 		netmap_reset_obj_allocator(&nmd->pools[i]);
1002ce3ee1e7SLuigi Rizzo 	}
1003ce3ee1e7SLuigi Rizzo 	nmd->flags  &= ~NETMAP_MEM_FINALIZED;
1004ce3ee1e7SLuigi Rizzo }
1005ce3ee1e7SLuigi Rizzo 
1006ce3ee1e7SLuigi Rizzo static int
10074bf50f18SLuigi Rizzo netmap_mem_unmap(struct netmap_obj_pool *p, struct netmap_adapter *na)
10084bf50f18SLuigi Rizzo {
10094bf50f18SLuigi Rizzo 	int i, lim = p->_objtotal;
10104bf50f18SLuigi Rizzo 
10114bf50f18SLuigi Rizzo 	if (na->pdev == NULL)
10124bf50f18SLuigi Rizzo 		return 0;
10134bf50f18SLuigi Rizzo 
10144bf50f18SLuigi Rizzo #ifdef __FreeBSD__
10154bf50f18SLuigi Rizzo 	(void)i;
10164bf50f18SLuigi Rizzo 	(void)lim;
10174bf50f18SLuigi Rizzo 	D("unsupported on FreeBSD");
10184bf50f18SLuigi Rizzo #else /* linux */
10194bf50f18SLuigi Rizzo 	for (i = 2; i < lim; i++) {
10204bf50f18SLuigi Rizzo 		netmap_unload_map(na, (bus_dma_tag_t) na->pdev, &p->lut[i].paddr);
10214bf50f18SLuigi Rizzo 	}
10224bf50f18SLuigi Rizzo #endif /* linux */
10234bf50f18SLuigi Rizzo 
10244bf50f18SLuigi Rizzo 	return 0;
10254bf50f18SLuigi Rizzo }
10264bf50f18SLuigi Rizzo 
10274bf50f18SLuigi Rizzo static int
10284bf50f18SLuigi Rizzo netmap_mem_map(struct netmap_obj_pool *p, struct netmap_adapter *na)
10294bf50f18SLuigi Rizzo {
10304bf50f18SLuigi Rizzo #ifdef __FreeBSD__
10314bf50f18SLuigi Rizzo 	D("unsupported on FreeBSD");
10324bf50f18SLuigi Rizzo #else /* linux */
10334bf50f18SLuigi Rizzo 	int i, lim = p->_objtotal;
10344bf50f18SLuigi Rizzo 
10354bf50f18SLuigi Rizzo 	if (na->pdev == NULL)
10364bf50f18SLuigi Rizzo 		return 0;
10374bf50f18SLuigi Rizzo 
10384bf50f18SLuigi Rizzo 	for (i = 2; i < lim; i++) {
10394bf50f18SLuigi Rizzo 		netmap_load_map(na, (bus_dma_tag_t) na->pdev, &p->lut[i].paddr,
10404bf50f18SLuigi Rizzo 				p->lut[i].vaddr);
10414bf50f18SLuigi Rizzo 	}
10424bf50f18SLuigi Rizzo #endif /* linux */
10434bf50f18SLuigi Rizzo 
10444bf50f18SLuigi Rizzo 	return 0;
10454bf50f18SLuigi Rizzo }
10464bf50f18SLuigi Rizzo 
10474bf50f18SLuigi Rizzo static int
1048ce3ee1e7SLuigi Rizzo netmap_mem_finalize_all(struct netmap_mem_d *nmd)
1049ce3ee1e7SLuigi Rizzo {
1050ce3ee1e7SLuigi Rizzo 	int i;
1051ce3ee1e7SLuigi Rizzo 	if (nmd->flags & NETMAP_MEM_FINALIZED)
1052ce3ee1e7SLuigi Rizzo 		return 0;
1053ce3ee1e7SLuigi Rizzo 	nmd->lasterr = 0;
1054ce3ee1e7SLuigi Rizzo 	nmd->nm_totalsize = 0;
1055ce3ee1e7SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1056ce3ee1e7SLuigi Rizzo 		nmd->lasterr = netmap_finalize_obj_allocator(&nmd->pools[i]);
1057ce3ee1e7SLuigi Rizzo 		if (nmd->lasterr)
1058ce3ee1e7SLuigi Rizzo 			goto error;
1059ce3ee1e7SLuigi Rizzo 		nmd->nm_totalsize += nmd->pools[i].memtotal;
1060ce3ee1e7SLuigi Rizzo 	}
1061ce3ee1e7SLuigi Rizzo 	/* buffers 0 and 1 are reserved */
1062ce3ee1e7SLuigi Rizzo 	nmd->pools[NETMAP_BUF_POOL].objfree -= 2;
1063ce3ee1e7SLuigi Rizzo 	nmd->pools[NETMAP_BUF_POOL].bitmap[0] = ~3;
1064ce3ee1e7SLuigi Rizzo 	nmd->flags |= NETMAP_MEM_FINALIZED;
1065ce3ee1e7SLuigi Rizzo 
1066f0ea3689SLuigi Rizzo 	if (netmap_verbose)
1067f0ea3689SLuigi Rizzo 		D("interfaces %d KB, rings %d KB, buffers %d MB",
1068ce3ee1e7SLuigi Rizzo 		    nmd->pools[NETMAP_IF_POOL].memtotal >> 10,
1069ce3ee1e7SLuigi Rizzo 		    nmd->pools[NETMAP_RING_POOL].memtotal >> 10,
1070ce3ee1e7SLuigi Rizzo 		    nmd->pools[NETMAP_BUF_POOL].memtotal >> 20);
1071ce3ee1e7SLuigi Rizzo 
1072f0ea3689SLuigi Rizzo 	if (netmap_verbose)
1073ce3ee1e7SLuigi Rizzo 		D("Free buffers: %d", nmd->pools[NETMAP_BUF_POOL].objfree);
1074ce3ee1e7SLuigi Rizzo 
1075ce3ee1e7SLuigi Rizzo 
1076ce3ee1e7SLuigi Rizzo 	return 0;
1077ce3ee1e7SLuigi Rizzo error:
1078ce3ee1e7SLuigi Rizzo 	netmap_mem_reset_all(nmd);
1079ce3ee1e7SLuigi Rizzo 	return nmd->lasterr;
1080ce3ee1e7SLuigi Rizzo }
1081ce3ee1e7SLuigi Rizzo 
1082ce3ee1e7SLuigi Rizzo 
1083ce3ee1e7SLuigi Rizzo 
1084ce3ee1e7SLuigi Rizzo void
1085ce3ee1e7SLuigi Rizzo netmap_mem_private_delete(struct netmap_mem_d *nmd)
1086ce3ee1e7SLuigi Rizzo {
1087ce3ee1e7SLuigi Rizzo 	if (nmd == NULL)
1088ce3ee1e7SLuigi Rizzo 		return;
1089f0ea3689SLuigi Rizzo 	if (netmap_verbose)
1090ce3ee1e7SLuigi Rizzo 		D("deleting %p", nmd);
1091ce3ee1e7SLuigi Rizzo 	if (nmd->refcount > 0)
1092ce3ee1e7SLuigi Rizzo 		D("bug: deleting mem allocator with refcount=%d!", nmd->refcount);
1093f0ea3689SLuigi Rizzo 	nm_mem_release_id(nmd);
1094f0ea3689SLuigi Rizzo 	if (netmap_verbose)
1095ce3ee1e7SLuigi Rizzo 		D("done deleting %p", nmd);
1096ce3ee1e7SLuigi Rizzo 	NMA_LOCK_DESTROY(nmd);
1097ce3ee1e7SLuigi Rizzo 	free(nmd, M_DEVBUF);
1098ce3ee1e7SLuigi Rizzo }
1099ce3ee1e7SLuigi Rizzo 
1100ce3ee1e7SLuigi Rizzo static int
1101ce3ee1e7SLuigi Rizzo netmap_mem_private_config(struct netmap_mem_d *nmd)
1102ce3ee1e7SLuigi Rizzo {
1103ce3ee1e7SLuigi Rizzo 	/* nothing to do, we are configured on creation
1104ce3ee1e7SLuigi Rizzo  	 * and configuration never changes thereafter
1105ce3ee1e7SLuigi Rizzo  	 */
1106ce3ee1e7SLuigi Rizzo 	return 0;
1107ce3ee1e7SLuigi Rizzo }
1108ce3ee1e7SLuigi Rizzo 
1109ce3ee1e7SLuigi Rizzo static int
1110ce3ee1e7SLuigi Rizzo netmap_mem_private_finalize(struct netmap_mem_d *nmd)
1111ce3ee1e7SLuigi Rizzo {
1112ce3ee1e7SLuigi Rizzo 	int err;
1113ce3ee1e7SLuigi Rizzo 	nmd->refcount++;
1114ce3ee1e7SLuigi Rizzo 	err = netmap_mem_finalize_all(nmd);
1115ce3ee1e7SLuigi Rizzo 	return err;
1116ce3ee1e7SLuigi Rizzo 
1117ce3ee1e7SLuigi Rizzo }
1118ce3ee1e7SLuigi Rizzo 
1119f9790aebSLuigi Rizzo static void
1120f9790aebSLuigi Rizzo netmap_mem_private_deref(struct netmap_mem_d *nmd)
1121ce3ee1e7SLuigi Rizzo {
1122ce3ee1e7SLuigi Rizzo 	if (--nmd->refcount <= 0)
1123ce3ee1e7SLuigi Rizzo 		netmap_mem_reset_all(nmd);
1124ce3ee1e7SLuigi Rizzo }
1125ce3ee1e7SLuigi Rizzo 
1126f0ea3689SLuigi Rizzo 
1127f0ea3689SLuigi Rizzo /*
1128f0ea3689SLuigi Rizzo  * allocator for private memory
1129f0ea3689SLuigi Rizzo  */
1130ce3ee1e7SLuigi Rizzo struct netmap_mem_d *
1131f0ea3689SLuigi Rizzo netmap_mem_private_new(const char *name, u_int txr, u_int txd,
1132f0ea3689SLuigi Rizzo 	u_int rxr, u_int rxd, u_int extra_bufs, u_int npipes, int *perr)
1133ce3ee1e7SLuigi Rizzo {
1134ce3ee1e7SLuigi Rizzo 	struct netmap_mem_d *d = NULL;
1135ce3ee1e7SLuigi Rizzo 	struct netmap_obj_params p[NETMAP_POOLS_NR];
1136f0ea3689SLuigi Rizzo 	int i, err;
1137f0ea3689SLuigi Rizzo 	u_int v, maxd;
1138ce3ee1e7SLuigi Rizzo 
1139ce3ee1e7SLuigi Rizzo 	d = malloc(sizeof(struct netmap_mem_d),
1140ce3ee1e7SLuigi Rizzo 			M_DEVBUF, M_NOWAIT | M_ZERO);
1141f0ea3689SLuigi Rizzo 	if (d == NULL) {
1142f0ea3689SLuigi Rizzo 		err = ENOMEM;
1143f0ea3689SLuigi Rizzo 		goto error;
1144f0ea3689SLuigi Rizzo 	}
1145ce3ee1e7SLuigi Rizzo 
1146ce3ee1e7SLuigi Rizzo 	*d = nm_blueprint;
1147ce3ee1e7SLuigi Rizzo 
1148f0ea3689SLuigi Rizzo 	err = nm_mem_assign_id(d);
1149f0ea3689SLuigi Rizzo 	if (err)
1150f0ea3689SLuigi Rizzo 		goto error;
1151f0ea3689SLuigi Rizzo 
1152f0ea3689SLuigi Rizzo 	/* account for the fake host rings */
1153ce3ee1e7SLuigi Rizzo 	txr++;
1154ce3ee1e7SLuigi Rizzo 	rxr++;
1155ce3ee1e7SLuigi Rizzo 
1156f0ea3689SLuigi Rizzo 	/* copy the min values */
1157f0ea3689SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1158f0ea3689SLuigi Rizzo 		p[i] = netmap_min_priv_params[i];
1159f0ea3689SLuigi Rizzo 	}
1160f0ea3689SLuigi Rizzo 
1161f0ea3689SLuigi Rizzo 	/* possibly increase them to fit user request */
1162f0ea3689SLuigi Rizzo 	v = sizeof(struct netmap_if) + sizeof(ssize_t) * (txr + rxr);
1163f0ea3689SLuigi Rizzo 	if (p[NETMAP_IF_POOL].size < v)
1164f0ea3689SLuigi Rizzo 		p[NETMAP_IF_POOL].size = v;
1165f0ea3689SLuigi Rizzo 	v = 2 + 4 * npipes;
1166f0ea3689SLuigi Rizzo 	if (p[NETMAP_IF_POOL].num < v)
1167f0ea3689SLuigi Rizzo 		p[NETMAP_IF_POOL].num = v;
1168f0ea3689SLuigi Rizzo 	maxd = (txd > rxd) ? txd : rxd;
1169f0ea3689SLuigi Rizzo 	v = sizeof(struct netmap_ring) + sizeof(struct netmap_slot) * maxd;
1170f0ea3689SLuigi Rizzo 	if (p[NETMAP_RING_POOL].size < v)
1171f0ea3689SLuigi Rizzo 		p[NETMAP_RING_POOL].size = v;
1172f0ea3689SLuigi Rizzo 	/* each pipe endpoint needs two tx rings (1 normal + 1 host, fake)
1173f0ea3689SLuigi Rizzo          * and two rx rings (again, 1 normal and 1 fake host)
1174f0ea3689SLuigi Rizzo          */
1175f0ea3689SLuigi Rizzo 	v = txr + rxr + 8 * npipes;
1176f0ea3689SLuigi Rizzo 	if (p[NETMAP_RING_POOL].num < v)
1177f0ea3689SLuigi Rizzo 		p[NETMAP_RING_POOL].num = v;
1178f0ea3689SLuigi Rizzo 	/* for each pipe we only need the buffers for the 4 "real" rings.
1179f0ea3689SLuigi Rizzo          * On the other end, the pipe ring dimension may be different from
1180f0ea3689SLuigi Rizzo          * the parent port ring dimension. As a compromise, we allocate twice the
1181f0ea3689SLuigi Rizzo          * space actually needed if the pipe rings were the same size as the parent rings
1182f0ea3689SLuigi Rizzo          */
1183f0ea3689SLuigi Rizzo 	v = (4 * npipes + rxr) * rxd + (4 * npipes + txr) * txd + 2 + extra_bufs;
1184f0ea3689SLuigi Rizzo 		/* the +2 is for the tx and rx fake buffers (indices 0 and 1) */
1185f0ea3689SLuigi Rizzo 	if (p[NETMAP_BUF_POOL].num < v)
1186f0ea3689SLuigi Rizzo 		p[NETMAP_BUF_POOL].num = v;
1187f0ea3689SLuigi Rizzo 
1188f0ea3689SLuigi Rizzo 	if (netmap_verbose)
1189ce3ee1e7SLuigi Rizzo 		D("req if %d*%d ring %d*%d buf %d*%d",
1190ce3ee1e7SLuigi Rizzo 			p[NETMAP_IF_POOL].num,
1191ce3ee1e7SLuigi Rizzo 			p[NETMAP_IF_POOL].size,
1192ce3ee1e7SLuigi Rizzo 			p[NETMAP_RING_POOL].num,
1193ce3ee1e7SLuigi Rizzo 			p[NETMAP_RING_POOL].size,
1194ce3ee1e7SLuigi Rizzo 			p[NETMAP_BUF_POOL].num,
1195ce3ee1e7SLuigi Rizzo 			p[NETMAP_BUF_POOL].size);
1196ce3ee1e7SLuigi Rizzo 
1197ce3ee1e7SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1198ce3ee1e7SLuigi Rizzo 		snprintf(d->pools[i].name, NETMAP_POOL_MAX_NAMSZ,
1199ce3ee1e7SLuigi Rizzo 				nm_blueprint.pools[i].name,
1200ce3ee1e7SLuigi Rizzo 				name);
1201f0ea3689SLuigi Rizzo 		err = netmap_config_obj_allocator(&d->pools[i],
1202f0ea3689SLuigi Rizzo 				p[i].num, p[i].size);
1203f0ea3689SLuigi Rizzo 		if (err)
1204ce3ee1e7SLuigi Rizzo 			goto error;
1205ce3ee1e7SLuigi Rizzo 	}
1206ce3ee1e7SLuigi Rizzo 
1207ce3ee1e7SLuigi Rizzo 	d->flags &= ~NETMAP_MEM_FINALIZED;
1208ce3ee1e7SLuigi Rizzo 
1209ce3ee1e7SLuigi Rizzo 	NMA_LOCK_INIT(d);
1210ce3ee1e7SLuigi Rizzo 
1211ce3ee1e7SLuigi Rizzo 	return d;
1212ce3ee1e7SLuigi Rizzo error:
1213ce3ee1e7SLuigi Rizzo 	netmap_mem_private_delete(d);
1214f0ea3689SLuigi Rizzo 	if (perr)
1215f0ea3689SLuigi Rizzo 		*perr = err;
1216ce3ee1e7SLuigi Rizzo 	return NULL;
1217ce3ee1e7SLuigi Rizzo }
1218ce3ee1e7SLuigi Rizzo 
12198241616dSLuigi Rizzo 
12208241616dSLuigi Rizzo /* call with lock held */
12218241616dSLuigi Rizzo static int
1222ce3ee1e7SLuigi Rizzo netmap_mem_global_config(struct netmap_mem_d *nmd)
12238241616dSLuigi Rizzo {
12248241616dSLuigi Rizzo 	int i;
12258241616dSLuigi Rizzo 
1226ce3ee1e7SLuigi Rizzo 	if (nmd->refcount)
1227ce3ee1e7SLuigi Rizzo 		/* already in use, we cannot change the configuration */
1228ce3ee1e7SLuigi Rizzo 		goto out;
1229ce3ee1e7SLuigi Rizzo 
1230ce3ee1e7SLuigi Rizzo 	if (!netmap_memory_config_changed(nmd))
12318241616dSLuigi Rizzo 		goto out;
12328241616dSLuigi Rizzo 
12338241616dSLuigi Rizzo 	D("reconfiguring");
12348241616dSLuigi Rizzo 
1235ce3ee1e7SLuigi Rizzo 	if (nmd->flags & NETMAP_MEM_FINALIZED) {
12368241616dSLuigi Rizzo 		/* reset previous allocation */
12378241616dSLuigi Rizzo 		for (i = 0; i < NETMAP_POOLS_NR; i++) {
1238ce3ee1e7SLuigi Rizzo 			netmap_reset_obj_allocator(&nmd->pools[i]);
12398241616dSLuigi Rizzo 		}
1240ce3ee1e7SLuigi Rizzo 		nmd->flags &= ~NETMAP_MEM_FINALIZED;
12418241616dSLuigi Rizzo 	}
12428241616dSLuigi Rizzo 
12438241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1244ce3ee1e7SLuigi Rizzo 		nmd->lasterr = netmap_config_obj_allocator(&nmd->pools[i],
12458241616dSLuigi Rizzo 				netmap_params[i].num, netmap_params[i].size);
1246ce3ee1e7SLuigi Rizzo 		if (nmd->lasterr)
12478241616dSLuigi Rizzo 			goto out;
12488241616dSLuigi Rizzo 	}
12498241616dSLuigi Rizzo 
12508241616dSLuigi Rizzo out:
12518241616dSLuigi Rizzo 
1252ce3ee1e7SLuigi Rizzo 	return nmd->lasterr;
12538241616dSLuigi Rizzo }
12548241616dSLuigi Rizzo 
12558241616dSLuigi Rizzo static int
1256ce3ee1e7SLuigi Rizzo netmap_mem_global_finalize(struct netmap_mem_d *nmd)
12578241616dSLuigi Rizzo {
1258ce3ee1e7SLuigi Rizzo 	int err;
12598241616dSLuigi Rizzo 
12608241616dSLuigi Rizzo 	/* update configuration if changed */
1261ce3ee1e7SLuigi Rizzo 	if (netmap_mem_global_config(nmd))
12628241616dSLuigi Rizzo 		goto out;
12638241616dSLuigi Rizzo 
1264ce3ee1e7SLuigi Rizzo 	nmd->refcount++;
1265ce3ee1e7SLuigi Rizzo 
1266ce3ee1e7SLuigi Rizzo 	if (nmd->flags & NETMAP_MEM_FINALIZED) {
12678241616dSLuigi Rizzo 		/* may happen if config is not changed */
12688241616dSLuigi Rizzo 		ND("nothing to do");
12698241616dSLuigi Rizzo 		goto out;
12708241616dSLuigi Rizzo 	}
12718241616dSLuigi Rizzo 
1272ce3ee1e7SLuigi Rizzo 	if (netmap_mem_finalize_all(nmd))
1273ce3ee1e7SLuigi Rizzo 		goto out;
12748241616dSLuigi Rizzo 
1275ce3ee1e7SLuigi Rizzo 	nmd->lasterr = 0;
12768241616dSLuigi Rizzo 
12778241616dSLuigi Rizzo out:
1278ce3ee1e7SLuigi Rizzo 	if (nmd->lasterr)
1279ce3ee1e7SLuigi Rizzo 		nmd->refcount--;
1280ce3ee1e7SLuigi Rizzo 	err = nmd->lasterr;
12818241616dSLuigi Rizzo 
1282ce3ee1e7SLuigi Rizzo 	return err;
12838241616dSLuigi Rizzo 
1284ccdc3305SLuigi Rizzo }
1285ccdc3305SLuigi Rizzo 
1286ce3ee1e7SLuigi Rizzo int
1287ce3ee1e7SLuigi Rizzo netmap_mem_init(void)
1288ccdc3305SLuigi Rizzo {
1289ce3ee1e7SLuigi Rizzo 	NMA_LOCK_INIT(&nm_mem);
12908241616dSLuigi Rizzo 	return (0);
1291ccdc3305SLuigi Rizzo }
1292ccdc3305SLuigi Rizzo 
1293ce3ee1e7SLuigi Rizzo void
1294ce3ee1e7SLuigi Rizzo netmap_mem_fini(void)
1295ccdc3305SLuigi Rizzo {
12968241616dSLuigi Rizzo 	int i;
12978241616dSLuigi Rizzo 
12988241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
12998241616dSLuigi Rizzo 	    netmap_destroy_obj_allocator(&nm_mem.pools[i]);
13008241616dSLuigi Rizzo 	}
1301ce3ee1e7SLuigi Rizzo 	NMA_LOCK_DESTROY(&nm_mem);
13028241616dSLuigi Rizzo }
13038241616dSLuigi Rizzo 
13048241616dSLuigi Rizzo static void
13058241616dSLuigi Rizzo netmap_free_rings(struct netmap_adapter *na)
13068241616dSLuigi Rizzo {
1307f0ea3689SLuigi Rizzo 	struct netmap_kring *kring;
1308f0ea3689SLuigi Rizzo 	struct netmap_ring *ring;
1309ae10d1afSLuigi Rizzo 	if (!na->tx_rings)
1310ae10d1afSLuigi Rizzo 		return;
1311f0ea3689SLuigi Rizzo 	for (kring = na->tx_rings; kring != na->rx_rings; kring++) {
1312f0ea3689SLuigi Rizzo 		ring = kring->ring;
1313f0ea3689SLuigi Rizzo 		if (ring == NULL)
1314f0ea3689SLuigi Rizzo 			continue;
1315f0ea3689SLuigi Rizzo 		netmap_free_bufs(na->nm_mem, ring->slot, kring->nkr_num_slots);
1316f0ea3689SLuigi Rizzo 		netmap_ring_free(na->nm_mem, ring);
1317f0ea3689SLuigi Rizzo 		kring->ring = NULL;
13188241616dSLuigi Rizzo 	}
1319f0ea3689SLuigi Rizzo 	for (/* cont'd from above */; kring != na->tailroom; kring++) {
1320f0ea3689SLuigi Rizzo 		ring = kring->ring;
1321f0ea3689SLuigi Rizzo 		if (ring == NULL)
1322f0ea3689SLuigi Rizzo 			continue;
1323f0ea3689SLuigi Rizzo 		netmap_free_bufs(na->nm_mem, ring->slot, kring->nkr_num_slots);
1324f0ea3689SLuigi Rizzo 		netmap_ring_free(na->nm_mem, ring);
1325f0ea3689SLuigi Rizzo 		kring->ring = NULL;
1326ce3ee1e7SLuigi Rizzo 	}
1327ccdc3305SLuigi Rizzo }
1328ccdc3305SLuigi Rizzo 
1329f9790aebSLuigi Rizzo /* call with NMA_LOCK held *
1330f9790aebSLuigi Rizzo  *
1331f9790aebSLuigi Rizzo  * Allocate netmap rings and buffers for this card
1332f9790aebSLuigi Rizzo  * The rings are contiguous, but have variable size.
1333f0ea3689SLuigi Rizzo  * The kring array must follow the layout described
1334f0ea3689SLuigi Rizzo  * in netmap_krings_create().
1335f9790aebSLuigi Rizzo  */
1336f9790aebSLuigi Rizzo int
1337f9790aebSLuigi Rizzo netmap_mem_rings_create(struct netmap_adapter *na)
1338f9790aebSLuigi Rizzo {
1339f9790aebSLuigi Rizzo 	struct netmap_ring *ring;
1340f9790aebSLuigi Rizzo 	u_int len, ndesc;
1341f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
1342f0ea3689SLuigi Rizzo 	u_int i;
1343f9790aebSLuigi Rizzo 
1344f9790aebSLuigi Rizzo 	NMA_LOCK(na->nm_mem);
1345f9790aebSLuigi Rizzo 
1346f0ea3689SLuigi Rizzo         /* transmit rings */
1347f0ea3689SLuigi Rizzo 	for (i =0, kring = na->tx_rings; kring != na->rx_rings; kring++, i++) {
1348f0ea3689SLuigi Rizzo 		if (kring->ring) {
1349f0ea3689SLuigi Rizzo 			ND("%s %ld already created", kring->name, kring - na->tx_rings);
1350f0ea3689SLuigi Rizzo 			continue; /* already created by somebody else */
1351f0ea3689SLuigi Rizzo 		}
1352f9790aebSLuigi Rizzo 		ndesc = kring->nkr_num_slots;
1353f9790aebSLuigi Rizzo 		len = sizeof(struct netmap_ring) +
1354f9790aebSLuigi Rizzo 			  ndesc * sizeof(struct netmap_slot);
1355f9790aebSLuigi Rizzo 		ring = netmap_ring_malloc(na->nm_mem, len);
1356f9790aebSLuigi Rizzo 		if (ring == NULL) {
1357f9790aebSLuigi Rizzo 			D("Cannot allocate tx_ring");
1358f9790aebSLuigi Rizzo 			goto cleanup;
1359f9790aebSLuigi Rizzo 		}
1360f6c2a31fSLuigi Rizzo 		ND("txring at %p", ring);
1361f9790aebSLuigi Rizzo 		kring->ring = ring;
1362f9790aebSLuigi Rizzo 		*(uint32_t *)(uintptr_t)&ring->num_slots = ndesc;
136317885a7bSLuigi Rizzo 		*(int64_t *)(uintptr_t)&ring->buf_ofs =
1364f9790aebSLuigi Rizzo 		    (na->nm_mem->pools[NETMAP_IF_POOL].memtotal +
1365f9790aebSLuigi Rizzo 			na->nm_mem->pools[NETMAP_RING_POOL].memtotal) -
1366f9790aebSLuigi Rizzo 			netmap_ring_offset(na->nm_mem, ring);
1367f9790aebSLuigi Rizzo 
136817885a7bSLuigi Rizzo 		/* copy values from kring */
136917885a7bSLuigi Rizzo 		ring->head = kring->rhead;
137017885a7bSLuigi Rizzo 		ring->cur = kring->rcur;
137117885a7bSLuigi Rizzo 		ring->tail = kring->rtail;
1372f9790aebSLuigi Rizzo 		*(uint16_t *)(uintptr_t)&ring->nr_buf_size =
13734bf50f18SLuigi Rizzo 			netmap_mem_bufsize(na->nm_mem);
1374f0ea3689SLuigi Rizzo 		ND("%s h %d c %d t %d", kring->name,
1375f0ea3689SLuigi Rizzo 			ring->head, ring->cur, ring->tail);
1376f9790aebSLuigi Rizzo 		ND("initializing slots for txring");
1377f0ea3689SLuigi Rizzo 		if (i != na->num_tx_rings || (na->na_flags & NAF_HOST_RINGS)) {
1378f0ea3689SLuigi Rizzo 			/* this is a real ring */
1379f9790aebSLuigi Rizzo 			if (netmap_new_bufs(na->nm_mem, ring->slot, ndesc)) {
1380f9790aebSLuigi Rizzo 				D("Cannot allocate buffers for tx_ring");
1381f9790aebSLuigi Rizzo 				goto cleanup;
1382f9790aebSLuigi Rizzo 			}
1383f0ea3689SLuigi Rizzo 		} else {
1384f0ea3689SLuigi Rizzo 			/* this is a fake tx ring, set all indices to 0 */
1385f0ea3689SLuigi Rizzo 			netmap_mem_set_ring(na->nm_mem, ring->slot, ndesc, 0);
1386f0ea3689SLuigi Rizzo 		}
1387f9790aebSLuigi Rizzo 	}
1388f9790aebSLuigi Rizzo 
1389f0ea3689SLuigi Rizzo 	/* receive rings */
1390f0ea3689SLuigi Rizzo 	for ( i = 0 /* kring cont'd from above */ ; kring != na->tailroom; kring++, i++) {
1391f0ea3689SLuigi Rizzo 		if (kring->ring) {
1392f0ea3689SLuigi Rizzo 			ND("%s %ld already created", kring->name, kring - na->rx_rings);
1393f0ea3689SLuigi Rizzo 			continue; /* already created by somebody else */
1394f0ea3689SLuigi Rizzo 		}
1395f9790aebSLuigi Rizzo 		ndesc = kring->nkr_num_slots;
1396f9790aebSLuigi Rizzo 		len = sizeof(struct netmap_ring) +
1397f9790aebSLuigi Rizzo 			  ndesc * sizeof(struct netmap_slot);
1398f9790aebSLuigi Rizzo 		ring = netmap_ring_malloc(na->nm_mem, len);
1399f9790aebSLuigi Rizzo 		if (ring == NULL) {
1400f9790aebSLuigi Rizzo 			D("Cannot allocate rx_ring");
1401f9790aebSLuigi Rizzo 			goto cleanup;
1402f9790aebSLuigi Rizzo 		}
1403f6c2a31fSLuigi Rizzo 		ND("rxring at %p", ring);
1404f9790aebSLuigi Rizzo 		kring->ring = ring;
1405f9790aebSLuigi Rizzo 		*(uint32_t *)(uintptr_t)&ring->num_slots = ndesc;
140617885a7bSLuigi Rizzo 		*(int64_t *)(uintptr_t)&ring->buf_ofs =
1407f9790aebSLuigi Rizzo 		    (na->nm_mem->pools[NETMAP_IF_POOL].memtotal +
1408f9790aebSLuigi Rizzo 		        na->nm_mem->pools[NETMAP_RING_POOL].memtotal) -
1409f9790aebSLuigi Rizzo 			netmap_ring_offset(na->nm_mem, ring);
1410f9790aebSLuigi Rizzo 
141117885a7bSLuigi Rizzo 		/* copy values from kring */
141217885a7bSLuigi Rizzo 		ring->head = kring->rhead;
141317885a7bSLuigi Rizzo 		ring->cur = kring->rcur;
141417885a7bSLuigi Rizzo 		ring->tail = kring->rtail;
1415f9790aebSLuigi Rizzo 		*(int *)(uintptr_t)&ring->nr_buf_size =
14164bf50f18SLuigi Rizzo 			netmap_mem_bufsize(na->nm_mem);
1417f0ea3689SLuigi Rizzo 		ND("%s h %d c %d t %d", kring->name,
1418f0ea3689SLuigi Rizzo 			ring->head, ring->cur, ring->tail);
1419f6c2a31fSLuigi Rizzo 		ND("initializing slots for rxring %p", ring);
1420f0ea3689SLuigi Rizzo 		if (i != na->num_rx_rings || (na->na_flags & NAF_HOST_RINGS)) {
1421f0ea3689SLuigi Rizzo 			/* this is a real ring */
1422f9790aebSLuigi Rizzo 			if (netmap_new_bufs(na->nm_mem, ring->slot, ndesc)) {
1423f9790aebSLuigi Rizzo 				D("Cannot allocate buffers for rx_ring");
1424f9790aebSLuigi Rizzo 				goto cleanup;
1425f9790aebSLuigi Rizzo 			}
1426f0ea3689SLuigi Rizzo 		} else {
1427f0ea3689SLuigi Rizzo 			/* this is a fake rx ring, set all indices to 1 */
1428f0ea3689SLuigi Rizzo 			netmap_mem_set_ring(na->nm_mem, ring->slot, ndesc, 1);
1429f0ea3689SLuigi Rizzo 		}
1430f9790aebSLuigi Rizzo 	}
1431f9790aebSLuigi Rizzo 
1432f9790aebSLuigi Rizzo 	NMA_UNLOCK(na->nm_mem);
1433f9790aebSLuigi Rizzo 
1434f9790aebSLuigi Rizzo 	return 0;
1435f9790aebSLuigi Rizzo 
1436f9790aebSLuigi Rizzo cleanup:
1437f9790aebSLuigi Rizzo 	netmap_free_rings(na);
1438f9790aebSLuigi Rizzo 
1439f9790aebSLuigi Rizzo 	NMA_UNLOCK(na->nm_mem);
1440f9790aebSLuigi Rizzo 
1441f9790aebSLuigi Rizzo 	return ENOMEM;
1442f9790aebSLuigi Rizzo }
1443f9790aebSLuigi Rizzo 
1444f9790aebSLuigi Rizzo void
1445f9790aebSLuigi Rizzo netmap_mem_rings_delete(struct netmap_adapter *na)
1446f9790aebSLuigi Rizzo {
1447f9790aebSLuigi Rizzo 	/* last instance, release bufs and rings */
1448f9790aebSLuigi Rizzo 	NMA_LOCK(na->nm_mem);
1449f9790aebSLuigi Rizzo 
1450f9790aebSLuigi Rizzo 	netmap_free_rings(na);
1451f9790aebSLuigi Rizzo 
1452f9790aebSLuigi Rizzo 	NMA_UNLOCK(na->nm_mem);
1453f9790aebSLuigi Rizzo }
1454ccdc3305SLuigi Rizzo 
1455ccdc3305SLuigi Rizzo 
14568241616dSLuigi Rizzo /* call with NMA_LOCK held */
1457ae10d1afSLuigi Rizzo /*
1458ae10d1afSLuigi Rizzo  * Allocate the per-fd structure netmap_if.
1459ce3ee1e7SLuigi Rizzo  *
1460ce3ee1e7SLuigi Rizzo  * We assume that the configuration stored in na
1461ce3ee1e7SLuigi Rizzo  * (number of tx/rx rings and descs) does not change while
1462ce3ee1e7SLuigi Rizzo  * the interface is in netmap mode.
1463ae10d1afSLuigi Rizzo  */
1464ce3ee1e7SLuigi Rizzo struct netmap_if *
14654bf50f18SLuigi Rizzo netmap_mem_if_new(struct netmap_adapter *na)
1466ccdc3305SLuigi Rizzo {
1467ccdc3305SLuigi Rizzo 	struct netmap_if *nifp;
1468ccdc3305SLuigi Rizzo 	ssize_t base; /* handy for relative offsets between rings and nifp */
1469f9790aebSLuigi Rizzo 	u_int i, len, ntx, nrx;
1470ccdc3305SLuigi Rizzo 
1471f0ea3689SLuigi Rizzo 	/* account for the (eventually fake) host rings */
1472f0ea3689SLuigi Rizzo 	ntx = na->num_tx_rings + 1;
1473f0ea3689SLuigi Rizzo 	nrx = na->num_rx_rings + 1;
1474ccdc3305SLuigi Rizzo 	/*
1475ccdc3305SLuigi Rizzo 	 * the descriptor is followed inline by an array of offsets
1476ccdc3305SLuigi Rizzo 	 * to the tx and rx rings in the shared memory region.
1477ccdc3305SLuigi Rizzo 	 */
1478ce3ee1e7SLuigi Rizzo 
1479ce3ee1e7SLuigi Rizzo 	NMA_LOCK(na->nm_mem);
1480ce3ee1e7SLuigi Rizzo 
1481ccdc3305SLuigi Rizzo 	len = sizeof(struct netmap_if) + (nrx + ntx) * sizeof(ssize_t);
1482ce3ee1e7SLuigi Rizzo 	nifp = netmap_if_malloc(na->nm_mem, len);
1483ccdc3305SLuigi Rizzo 	if (nifp == NULL) {
1484ce3ee1e7SLuigi Rizzo 		NMA_UNLOCK(na->nm_mem);
1485ccdc3305SLuigi Rizzo 		return NULL;
1486ccdc3305SLuigi Rizzo 	}
1487ccdc3305SLuigi Rizzo 
1488ccdc3305SLuigi Rizzo 	/* initialize base fields -- override const */
1489ce3ee1e7SLuigi Rizzo 	*(u_int *)(uintptr_t)&nifp->ni_tx_rings = na->num_tx_rings;
1490ce3ee1e7SLuigi Rizzo 	*(u_int *)(uintptr_t)&nifp->ni_rx_rings = na->num_rx_rings;
14914bf50f18SLuigi Rizzo 	strncpy(nifp->ni_name, na->name, (size_t)IFNAMSIZ);
1492ccdc3305SLuigi Rizzo 
1493ccdc3305SLuigi Rizzo 	/*
1494ccdc3305SLuigi Rizzo 	 * fill the slots for the rx and tx rings. They contain the offset
1495ccdc3305SLuigi Rizzo 	 * between the ring and nifp, so the information is usable in
1496ccdc3305SLuigi Rizzo 	 * userspace to reach the ring from the nifp.
1497ccdc3305SLuigi Rizzo 	 */
1498ce3ee1e7SLuigi Rizzo 	base = netmap_if_offset(na->nm_mem, nifp);
1499ccdc3305SLuigi Rizzo 	for (i = 0; i < ntx; i++) {
1500ccdc3305SLuigi Rizzo 		*(ssize_t *)(uintptr_t)&nifp->ring_ofs[i] =
1501ce3ee1e7SLuigi Rizzo 			netmap_ring_offset(na->nm_mem, na->tx_rings[i].ring) - base;
1502ccdc3305SLuigi Rizzo 	}
1503ccdc3305SLuigi Rizzo 	for (i = 0; i < nrx; i++) {
1504ccdc3305SLuigi Rizzo 		*(ssize_t *)(uintptr_t)&nifp->ring_ofs[i+ntx] =
1505ce3ee1e7SLuigi Rizzo 			netmap_ring_offset(na->nm_mem, na->rx_rings[i].ring) - base;
1506ccdc3305SLuigi Rizzo 	}
1507ce3ee1e7SLuigi Rizzo 
1508ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(na->nm_mem);
1509ce3ee1e7SLuigi Rizzo 
1510ccdc3305SLuigi Rizzo 	return (nifp);
1511ccdc3305SLuigi Rizzo }
1512ccdc3305SLuigi Rizzo 
1513ce3ee1e7SLuigi Rizzo void
1514ce3ee1e7SLuigi Rizzo netmap_mem_if_delete(struct netmap_adapter *na, struct netmap_if *nifp)
1515ccdc3305SLuigi Rizzo {
1516ce3ee1e7SLuigi Rizzo 	if (nifp == NULL)
1517ce3ee1e7SLuigi Rizzo 		/* nothing to do */
1518ce3ee1e7SLuigi Rizzo 		return;
1519ce3ee1e7SLuigi Rizzo 	NMA_LOCK(na->nm_mem);
1520f0ea3689SLuigi Rizzo 	if (nifp->ni_bufs_head)
1521f0ea3689SLuigi Rizzo 		netmap_extra_free(na, nifp->ni_bufs_head);
1522ce3ee1e7SLuigi Rizzo 	netmap_if_free(na->nm_mem, nifp);
1523ce3ee1e7SLuigi Rizzo 
1524ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(na->nm_mem);
1525ce3ee1e7SLuigi Rizzo }
1526ce3ee1e7SLuigi Rizzo 
1527ce3ee1e7SLuigi Rizzo static void
1528ce3ee1e7SLuigi Rizzo netmap_mem_global_deref(struct netmap_mem_d *nmd)
1529ce3ee1e7SLuigi Rizzo {
1530ce3ee1e7SLuigi Rizzo 
1531ce3ee1e7SLuigi Rizzo 	nmd->refcount--;
15324bf50f18SLuigi Rizzo 	if (!nmd->refcount)
15334bf50f18SLuigi Rizzo 		nmd->nm_grp = -1;
1534ae10d1afSLuigi Rizzo 	if (netmap_verbose)
1535ce3ee1e7SLuigi Rizzo 		D("refcount = %d", nmd->refcount);
1536ce3ee1e7SLuigi Rizzo 
1537ce3ee1e7SLuigi Rizzo }
1538ce3ee1e7SLuigi Rizzo 
1539f9790aebSLuigi Rizzo int
15404bf50f18SLuigi Rizzo netmap_mem_finalize(struct netmap_mem_d *nmd, struct netmap_adapter *na)
1541ce3ee1e7SLuigi Rizzo {
15424bf50f18SLuigi Rizzo 	if (nm_mem_assign_group(nmd, na->pdev) < 0) {
15434bf50f18SLuigi Rizzo 		return ENOMEM;
15444bf50f18SLuigi Rizzo 	} else {
1545*dd4fcbc5SPatrick Kelsey 		NMA_LOCK(nmd);
15464bf50f18SLuigi Rizzo 		nmd->finalize(nmd);
1547*dd4fcbc5SPatrick Kelsey 		NMA_UNLOCK(nmd);
15484bf50f18SLuigi Rizzo 	}
15494bf50f18SLuigi Rizzo 
15504bf50f18SLuigi Rizzo 	if (!nmd->lasterr && na->pdev)
15514bf50f18SLuigi Rizzo 		netmap_mem_map(&nmd->pools[NETMAP_BUF_POOL], na);
15524bf50f18SLuigi Rizzo 
15534bf50f18SLuigi Rizzo 	return nmd->lasterr;
1554ce3ee1e7SLuigi Rizzo }
1555ce3ee1e7SLuigi Rizzo 
1556f9790aebSLuigi Rizzo void
15574bf50f18SLuigi Rizzo netmap_mem_deref(struct netmap_mem_d *nmd, struct netmap_adapter *na)
1558ce3ee1e7SLuigi Rizzo {
15594bf50f18SLuigi Rizzo 	NMA_LOCK(nmd);
15604bf50f18SLuigi Rizzo 	netmap_mem_unmap(&nmd->pools[NETMAP_BUF_POOL], na);
1561*dd4fcbc5SPatrick Kelsey 	if (nmd->refcount == 1) {
1562*dd4fcbc5SPatrick Kelsey 		u_int i;
1563*dd4fcbc5SPatrick Kelsey 
1564*dd4fcbc5SPatrick Kelsey 		/*
1565*dd4fcbc5SPatrick Kelsey 		 * Reset the allocator when it falls out of use so that any
1566*dd4fcbc5SPatrick Kelsey 		 * pool resources leaked by unclean application exits are
1567*dd4fcbc5SPatrick Kelsey 		 * reclaimed.
1568*dd4fcbc5SPatrick Kelsey 		 */
1569*dd4fcbc5SPatrick Kelsey 		for (i = 0; i < NETMAP_POOLS_NR; i++) {
1570*dd4fcbc5SPatrick Kelsey 			struct netmap_obj_pool *p;
1571*dd4fcbc5SPatrick Kelsey 			u_int j;
1572*dd4fcbc5SPatrick Kelsey 
1573*dd4fcbc5SPatrick Kelsey 			p = &nmd->pools[i];
1574*dd4fcbc5SPatrick Kelsey 			p->objfree = p->objtotal;
1575*dd4fcbc5SPatrick Kelsey 			/*
1576*dd4fcbc5SPatrick Kelsey 			 * Reproduce the net effect of the M_ZERO malloc()
1577*dd4fcbc5SPatrick Kelsey 			 * and marking of free entries in the bitmap that
1578*dd4fcbc5SPatrick Kelsey 			 * occur in finalize_obj_allocator()
1579*dd4fcbc5SPatrick Kelsey 			 */
1580*dd4fcbc5SPatrick Kelsey 			memset(p->bitmap,
1581*dd4fcbc5SPatrick Kelsey 			    '\0',
1582*dd4fcbc5SPatrick Kelsey 			    sizeof(uint32_t) * ((p->objtotal + 31) / 32));
1583*dd4fcbc5SPatrick Kelsey 
1584*dd4fcbc5SPatrick Kelsey 			/*
1585*dd4fcbc5SPatrick Kelsey 			 * Set all the bits in the bitmap that have
1586*dd4fcbc5SPatrick Kelsey 			 * corresponding buffers to 1 to indicate they are
1587*dd4fcbc5SPatrick Kelsey 			 * free.
1588*dd4fcbc5SPatrick Kelsey 			 */
1589*dd4fcbc5SPatrick Kelsey 			for (j = 0; j < p->objtotal; j++) {
1590*dd4fcbc5SPatrick Kelsey 				if (p->lut[j].vaddr != NULL) {
1591*dd4fcbc5SPatrick Kelsey 					p->bitmap[ (j>>5) ] |=  ( 1 << (j & 31) );
1592*dd4fcbc5SPatrick Kelsey 				}
1593*dd4fcbc5SPatrick Kelsey 			}
1594*dd4fcbc5SPatrick Kelsey 		}
1595*dd4fcbc5SPatrick Kelsey 
1596*dd4fcbc5SPatrick Kelsey 		/*
1597*dd4fcbc5SPatrick Kelsey 		 * Per netmap_mem_finalize_all(),
1598*dd4fcbc5SPatrick Kelsey 		 * buffers 0 and 1 are reserved
1599*dd4fcbc5SPatrick Kelsey 		 */
1600*dd4fcbc5SPatrick Kelsey 		nmd->pools[NETMAP_BUF_POOL].objfree -= 2;
1601*dd4fcbc5SPatrick Kelsey 		nmd->pools[NETMAP_BUF_POOL].bitmap[0] = ~3;
1602*dd4fcbc5SPatrick Kelsey 	}
1603*dd4fcbc5SPatrick Kelsey 	nmd->deref(nmd);
16044bf50f18SLuigi Rizzo 	NMA_UNLOCK(nmd);
1605ccdc3305SLuigi Rizzo }
1606