xref: /freebsd-14.2/sys/dev/netmap/netmap_mem2.c (revision 893caa2a)
1718cf2ccSPedro F. Giffuni /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
437e3a6d3SLuigi Rizzo  * Copyright (C) 2012-2014 Matteo Landi
537e3a6d3SLuigi Rizzo  * Copyright (C) 2012-2016 Luigi Rizzo
637e3a6d3SLuigi Rizzo  * Copyright (C) 2012-2016 Giuseppe Lettieri
737e3a6d3SLuigi Rizzo  * All rights reserved.
8ccdc3305SLuigi Rizzo  *
9ccdc3305SLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
10ccdc3305SLuigi Rizzo  * modification, are permitted provided that the following conditions
11ccdc3305SLuigi Rizzo  * are met:
12ccdc3305SLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
13ccdc3305SLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
14ccdc3305SLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
15ccdc3305SLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
16ccdc3305SLuigi Rizzo  *      documentation and/or other materials provided with the distribution.
17ccdc3305SLuigi Rizzo  *
18ccdc3305SLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19ccdc3305SLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20ccdc3305SLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ccdc3305SLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22ccdc3305SLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23ccdc3305SLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24ccdc3305SLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25ccdc3305SLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26ccdc3305SLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27ccdc3305SLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28ccdc3305SLuigi Rizzo  * SUCH DAMAGE.
29ccdc3305SLuigi Rizzo  */
30ccdc3305SLuigi Rizzo 
318241616dSLuigi Rizzo #ifdef linux
32ce3ee1e7SLuigi Rizzo #include "bsd_glue.h"
338241616dSLuigi Rizzo #endif /* linux */
348241616dSLuigi Rizzo 
35ce3ee1e7SLuigi Rizzo #ifdef __APPLE__
36ce3ee1e7SLuigi Rizzo #include "osx_glue.h"
37ce3ee1e7SLuigi Rizzo #endif /* __APPLE__ */
388241616dSLuigi Rizzo 
39ce3ee1e7SLuigi Rizzo #ifdef __FreeBSD__
40ce3ee1e7SLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */
41ce3ee1e7SLuigi Rizzo #include <sys/types.h>
42ce3ee1e7SLuigi Rizzo #include <sys/malloc.h>
4337e3a6d3SLuigi Rizzo #include <sys/kernel.h>		/* MALLOC_DEFINE */
44ce3ee1e7SLuigi Rizzo #include <sys/proc.h>
45ce3ee1e7SLuigi Rizzo #include <vm/vm.h>	/* vtophys */
46ce3ee1e7SLuigi Rizzo #include <vm/pmap.h>	/* vtophys */
47ce3ee1e7SLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
48ce3ee1e7SLuigi Rizzo #include <sys/selinfo.h>
49ce3ee1e7SLuigi Rizzo #include <sys/sysctl.h>
50ce3ee1e7SLuigi Rizzo #include <net/if.h>
51ce3ee1e7SLuigi Rizzo #include <net/if_var.h>
52ce3ee1e7SLuigi Rizzo #include <net/vnet.h>
53ce3ee1e7SLuigi Rizzo #include <machine/bus.h>	/* bus_dmamap_* */
54ce3ee1e7SLuigi Rizzo 
5537e3a6d3SLuigi Rizzo /* M_NETMAP only used in here */
5637e3a6d3SLuigi Rizzo MALLOC_DECLARE(M_NETMAP);
5737e3a6d3SLuigi Rizzo MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map");
5837e3a6d3SLuigi Rizzo 
59ce3ee1e7SLuigi Rizzo #endif /* __FreeBSD__ */
60ce3ee1e7SLuigi Rizzo 
6137e3a6d3SLuigi Rizzo #ifdef _WIN32
6237e3a6d3SLuigi Rizzo #include <win_glue.h>
6337e3a6d3SLuigi Rizzo #endif
6437e3a6d3SLuigi Rizzo 
65ce3ee1e7SLuigi Rizzo #include <net/netmap.h>
66ce3ee1e7SLuigi Rizzo #include <dev/netmap/netmap_kern.h>
6737e3a6d3SLuigi Rizzo #include <net/netmap_virt.h>
68ce3ee1e7SLuigi Rizzo #include "netmap_mem2.h"
69ce3ee1e7SLuigi Rizzo 
7037e3a6d3SLuigi Rizzo #ifdef _WIN32_USE_SMALL_GENERIC_DEVICES_MEMORY
7137e3a6d3SLuigi Rizzo #define NETMAP_BUF_MAX_NUM  8*4096      /* if too big takes too much time to allocate */
7237e3a6d3SLuigi Rizzo #else
734bf50f18SLuigi Rizzo #define NETMAP_BUF_MAX_NUM 20*4096*2	/* large machine */
7437e3a6d3SLuigi Rizzo #endif
754bf50f18SLuigi Rizzo 
764bf50f18SLuigi Rizzo #define NETMAP_POOL_MAX_NAMSZ	32
774bf50f18SLuigi Rizzo 
784bf50f18SLuigi Rizzo 
794bf50f18SLuigi Rizzo enum {
804bf50f18SLuigi Rizzo 	NETMAP_IF_POOL   = 0,
814bf50f18SLuigi Rizzo 	NETMAP_RING_POOL,
824bf50f18SLuigi Rizzo 	NETMAP_BUF_POOL,
834bf50f18SLuigi Rizzo 	NETMAP_POOLS_NR
844bf50f18SLuigi Rizzo };
854bf50f18SLuigi Rizzo 
864bf50f18SLuigi Rizzo 
874bf50f18SLuigi Rizzo struct netmap_obj_params {
884bf50f18SLuigi Rizzo 	u_int size;
894bf50f18SLuigi Rizzo 	u_int num;
90c3e9b4dbSLuiz Otavio O Souza 
91c3e9b4dbSLuiz Otavio O Souza 	u_int last_size;
92c3e9b4dbSLuiz Otavio O Souza 	u_int last_num;
934bf50f18SLuigi Rizzo };
94847bf383SLuigi Rizzo 
954bf50f18SLuigi Rizzo struct netmap_obj_pool {
964bf50f18SLuigi Rizzo 	char name[NETMAP_POOL_MAX_NAMSZ];	/* name of the allocator */
974bf50f18SLuigi Rizzo 
984bf50f18SLuigi Rizzo 	/* ---------------------------------------------------*/
994bf50f18SLuigi Rizzo 	/* these are only meaningful if the pool is finalized */
1004bf50f18SLuigi Rizzo 	/* (see 'finalized' field in netmap_mem_d)            */
101253b2ec1SVincenzo Maffione 	size_t memtotal;	/* actual total memory space */
1024bf50f18SLuigi Rizzo 
1034bf50f18SLuigi Rizzo 	struct lut_entry *lut;  /* virt,phys addresses, objtotal entries */
1044bf50f18SLuigi Rizzo 	uint32_t *bitmap;       /* one bit per buffer, 1 means free */
1054f80b14cSVincenzo Maffione 	uint32_t *invalid_bitmap;/* one bit per buffer, 1 means invalid */
1064bf50f18SLuigi Rizzo 	uint32_t bitmap_slots;	/* number of uint32 entries in bitmap */
107253b2ec1SVincenzo Maffione 
108253b2ec1SVincenzo Maffione 	u_int objtotal;         /* actual total number of objects. */
109253b2ec1SVincenzo Maffione 	u_int numclusters;	/* actual number of clusters */
110253b2ec1SVincenzo Maffione 	u_int objfree;          /* number of free objects. */
111253b2ec1SVincenzo Maffione 
1122ff91c17SVincenzo Maffione 	int	alloc_done;	/* we have allocated the memory */
1134bf50f18SLuigi Rizzo 	/* ---------------------------------------------------*/
1144bf50f18SLuigi Rizzo 
1154bf50f18SLuigi Rizzo 	/* limits */
1164bf50f18SLuigi Rizzo 	u_int objminsize;	/* minimum object size */
1174bf50f18SLuigi Rizzo 	u_int objmaxsize;	/* maximum object size */
1184bf50f18SLuigi Rizzo 	u_int nummin;		/* minimum number of objects */
1194bf50f18SLuigi Rizzo 	u_int nummax;		/* maximum number of objects */
1204bf50f18SLuigi Rizzo 
1214bf50f18SLuigi Rizzo 	/* these are changed only by config */
1224bf50f18SLuigi Rizzo 	u_int _objtotal;	/* total number of objects */
1234bf50f18SLuigi Rizzo 	u_int _objsize;		/* object size */
1244bf50f18SLuigi Rizzo 	u_int _clustsize;       /* cluster size */
1254bf50f18SLuigi Rizzo 	u_int _clustentries;    /* objects per cluster */
1264bf50f18SLuigi Rizzo 	u_int _numclusters;	/* number of clusters */
1274bf50f18SLuigi Rizzo 
1284bf50f18SLuigi Rizzo 	/* requested values */
1294bf50f18SLuigi Rizzo 	u_int r_objtotal;
1304bf50f18SLuigi Rizzo 	u_int r_objsize;
1314bf50f18SLuigi Rizzo };
1324bf50f18SLuigi Rizzo 
133847bf383SLuigi Rizzo #define NMA_LOCK_T		NM_MTX_T
1342ff91c17SVincenzo Maffione #define NMA_LOCK_INIT(n)	NM_MTX_INIT((n)->nm_mtx)
1352ff91c17SVincenzo Maffione #define NMA_LOCK_DESTROY(n)	NM_MTX_DESTROY((n)->nm_mtx)
1362ff91c17SVincenzo Maffione #define NMA_LOCK(n)		NM_MTX_LOCK((n)->nm_mtx)
1372ff91c17SVincenzo Maffione #define NMA_SPINLOCK(n)         NM_MTX_SPINLOCK((n)->nm_mtx)
1382ff91c17SVincenzo Maffione #define NMA_UNLOCK(n)		NM_MTX_UNLOCK((n)->nm_mtx)
139847bf383SLuigi Rizzo 
140847bf383SLuigi Rizzo struct netmap_mem_ops {
14137e3a6d3SLuigi Rizzo 	int (*nmd_get_lut)(struct netmap_mem_d *, struct netmap_lut*);
1424f80b14cSVincenzo Maffione 	int  (*nmd_get_info)(struct netmap_mem_d *, uint64_t *size,
143847bf383SLuigi Rizzo 			u_int *memflags, uint16_t *id);
144847bf383SLuigi Rizzo 
145847bf383SLuigi Rizzo 	vm_paddr_t (*nmd_ofstophys)(struct netmap_mem_d *, vm_ooffset_t);
146847bf383SLuigi Rizzo 	int (*nmd_config)(struct netmap_mem_d *);
147a6d768d8SVincenzo Maffione 	int (*nmd_finalize)(struct netmap_mem_d *, struct netmap_adapter *);
148a6d768d8SVincenzo Maffione 	void (*nmd_deref)(struct netmap_mem_d *, struct netmap_adapter *);
149847bf383SLuigi Rizzo 	ssize_t  (*nmd_if_offset)(struct netmap_mem_d *, const void *vaddr);
150847bf383SLuigi Rizzo 	void (*nmd_delete)(struct netmap_mem_d *);
151847bf383SLuigi Rizzo 
152a6d768d8SVincenzo Maffione 	struct netmap_if * (*nmd_if_new)(struct netmap_mem_d *,
153a6d768d8SVincenzo Maffione 			struct netmap_adapter *, struct netmap_priv_d *);
154a6d768d8SVincenzo Maffione 	void (*nmd_if_delete)(struct netmap_mem_d *,
155a6d768d8SVincenzo Maffione 			struct netmap_adapter *, struct netmap_if *);
156a6d768d8SVincenzo Maffione 	int  (*nmd_rings_create)(struct netmap_mem_d *,
157a6d768d8SVincenzo Maffione 			struct netmap_adapter *);
158a6d768d8SVincenzo Maffione 	void (*nmd_rings_delete)(struct netmap_mem_d *,
159a6d768d8SVincenzo Maffione 			struct netmap_adapter *);
160847bf383SLuigi Rizzo };
1614bf50f18SLuigi Rizzo 
1624bf50f18SLuigi Rizzo struct netmap_mem_d {
1634bf50f18SLuigi Rizzo 	NMA_LOCK_T nm_mtx;  /* protect the allocator */
164253b2ec1SVincenzo Maffione 	size_t nm_totalsize; /* shorthand */
1654bf50f18SLuigi Rizzo 
1664bf50f18SLuigi Rizzo 	u_int flags;
1674bf50f18SLuigi Rizzo #define NETMAP_MEM_FINALIZED	0x1	/* preallocation done */
16845c67e8fSVincenzo Maffione #define NETMAP_MEM_HIDDEN	0x8	/* being prepared */
169a6d768d8SVincenzo Maffione #define NETMAP_MEM_NOMAP	0x10	/* do not map/unmap pdevs */
1704bf50f18SLuigi Rizzo 	int lasterr;		/* last error for curr config */
171847bf383SLuigi Rizzo 	int active;		/* active users */
172847bf383SLuigi Rizzo 	int refcount;
1734bf50f18SLuigi Rizzo 	/* the three allocators */
1744bf50f18SLuigi Rizzo 	struct netmap_obj_pool pools[NETMAP_POOLS_NR];
1754bf50f18SLuigi Rizzo 
1764bf50f18SLuigi Rizzo 	nm_memid_t nm_id;	/* allocator identifier */
17745c67e8fSVincenzo Maffione 	int nm_grp;	/* iommu group id */
1784bf50f18SLuigi Rizzo 
1794bf50f18SLuigi Rizzo 	/* list of all existing allocators, sorted by nm_id */
1804bf50f18SLuigi Rizzo 	struct netmap_mem_d *prev, *next;
181847bf383SLuigi Rizzo 
182*893caa2aSMark Johnston 	const struct netmap_mem_ops *ops;
183c3e9b4dbSLuiz Otavio O Souza 
184c3e9b4dbSLuiz Otavio O Souza 	struct netmap_obj_params params[NETMAP_POOLS_NR];
185c3e9b4dbSLuiz Otavio O Souza 
186c3e9b4dbSLuiz Otavio O Souza #define NM_MEM_NAMESZ	16
187c3e9b4dbSLuiz Otavio O Souza 	char name[NM_MEM_NAMESZ];
1884bf50f18SLuigi Rizzo };
1894bf50f18SLuigi Rizzo 
1902ff91c17SVincenzo Maffione int
1912ff91c17SVincenzo Maffione netmap_mem_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut)
1922ff91c17SVincenzo Maffione {
1932ff91c17SVincenzo Maffione 	int rv;
1942ff91c17SVincenzo Maffione 
1952ff91c17SVincenzo Maffione 	NMA_LOCK(nmd);
1962ff91c17SVincenzo Maffione 	rv = nmd->ops->nmd_get_lut(nmd, lut);
1972ff91c17SVincenzo Maffione 	NMA_UNLOCK(nmd);
1982ff91c17SVincenzo Maffione 
1992ff91c17SVincenzo Maffione 	return rv;
2002ff91c17SVincenzo Maffione }
2012ff91c17SVincenzo Maffione 
2022ff91c17SVincenzo Maffione int
2032ff91c17SVincenzo Maffione netmap_mem_get_info(struct netmap_mem_d *nmd, uint64_t *size,
2042ff91c17SVincenzo Maffione 		u_int *memflags, nm_memid_t *memid)
2052ff91c17SVincenzo Maffione {
2062ff91c17SVincenzo Maffione 	int rv;
2072ff91c17SVincenzo Maffione 
2082ff91c17SVincenzo Maffione 	NMA_LOCK(nmd);
2092ff91c17SVincenzo Maffione 	rv = nmd->ops->nmd_get_info(nmd, size, memflags, memid);
2102ff91c17SVincenzo Maffione 	NMA_UNLOCK(nmd);
2112ff91c17SVincenzo Maffione 
2122ff91c17SVincenzo Maffione 	return rv;
2132ff91c17SVincenzo Maffione }
2142ff91c17SVincenzo Maffione 
2152ff91c17SVincenzo Maffione vm_paddr_t
2162ff91c17SVincenzo Maffione netmap_mem_ofstophys(struct netmap_mem_d *nmd, vm_ooffset_t off)
2172ff91c17SVincenzo Maffione {
2182ff91c17SVincenzo Maffione 	vm_paddr_t pa;
2192ff91c17SVincenzo Maffione 
2202ff91c17SVincenzo Maffione #if defined(__FreeBSD__)
2212ff91c17SVincenzo Maffione 	/* This function is called by netmap_dev_pager_fault(), which holds a
2222ff91c17SVincenzo Maffione 	 * non-sleepable lock since FreeBSD 12. Since we cannot sleep, we
2232ff91c17SVincenzo Maffione 	 * spin on the trylock. */
2242ff91c17SVincenzo Maffione 	NMA_SPINLOCK(nmd);
2252ff91c17SVincenzo Maffione #else
2262ff91c17SVincenzo Maffione 	NMA_LOCK(nmd);
2272ff91c17SVincenzo Maffione #endif
2282ff91c17SVincenzo Maffione 	pa = nmd->ops->nmd_ofstophys(nmd, off);
2292ff91c17SVincenzo Maffione 	NMA_UNLOCK(nmd);
2302ff91c17SVincenzo Maffione 
2312ff91c17SVincenzo Maffione 	return pa;
2322ff91c17SVincenzo Maffione }
2332ff91c17SVincenzo Maffione 
2342ff91c17SVincenzo Maffione static int
2352ff91c17SVincenzo Maffione netmap_mem_config(struct netmap_mem_d *nmd)
2362ff91c17SVincenzo Maffione {
2372ff91c17SVincenzo Maffione 	if (nmd->active) {
2382ff91c17SVincenzo Maffione 		/* already in use. Not fatal, but we
2392ff91c17SVincenzo Maffione 		 * cannot change the configuration
24037e3a6d3SLuigi Rizzo 		 */
2412ff91c17SVincenzo Maffione 		return 0;
242847bf383SLuigi Rizzo 	}
243847bf383SLuigi Rizzo 
2442ff91c17SVincenzo Maffione 	return nmd->ops->nmd_config(nmd);
245847bf383SLuigi Rizzo }
246847bf383SLuigi Rizzo 
2472ff91c17SVincenzo Maffione ssize_t
2482ff91c17SVincenzo Maffione netmap_mem_if_offset(struct netmap_mem_d *nmd, const void *off)
2492ff91c17SVincenzo Maffione {
2502ff91c17SVincenzo Maffione 	ssize_t rv;
2512ff91c17SVincenzo Maffione 
2522ff91c17SVincenzo Maffione 	NMA_LOCK(nmd);
2532ff91c17SVincenzo Maffione 	rv = nmd->ops->nmd_if_offset(nmd, off);
2542ff91c17SVincenzo Maffione 	NMA_UNLOCK(nmd);
2552ff91c17SVincenzo Maffione 
2562ff91c17SVincenzo Maffione 	return rv;
257847bf383SLuigi Rizzo }
258847bf383SLuigi Rizzo 
2592ff91c17SVincenzo Maffione static void
2602ff91c17SVincenzo Maffione netmap_mem_delete(struct netmap_mem_d *nmd)
2612ff91c17SVincenzo Maffione {
2622ff91c17SVincenzo Maffione 	nmd->ops->nmd_delete(nmd);
263847bf383SLuigi Rizzo }
264847bf383SLuigi Rizzo 
2652ff91c17SVincenzo Maffione struct netmap_if *
2662ff91c17SVincenzo Maffione netmap_mem_if_new(struct netmap_adapter *na, struct netmap_priv_d *priv)
2672ff91c17SVincenzo Maffione {
2682ff91c17SVincenzo Maffione 	struct netmap_if *nifp;
2692ff91c17SVincenzo Maffione 	struct netmap_mem_d *nmd = na->nm_mem;
2702ff91c17SVincenzo Maffione 
2712ff91c17SVincenzo Maffione 	NMA_LOCK(nmd);
272a6d768d8SVincenzo Maffione 	nifp = nmd->ops->nmd_if_new(nmd, na, priv);
2732ff91c17SVincenzo Maffione 	NMA_UNLOCK(nmd);
2742ff91c17SVincenzo Maffione 
2752ff91c17SVincenzo Maffione 	return nifp;
276847bf383SLuigi Rizzo }
277847bf383SLuigi Rizzo 
2782ff91c17SVincenzo Maffione void
2792ff91c17SVincenzo Maffione netmap_mem_if_delete(struct netmap_adapter *na, struct netmap_if *nif)
2802ff91c17SVincenzo Maffione {
2812ff91c17SVincenzo Maffione 	struct netmap_mem_d *nmd = na->nm_mem;
282847bf383SLuigi Rizzo 
2832ff91c17SVincenzo Maffione 	NMA_LOCK(nmd);
284a6d768d8SVincenzo Maffione 	nmd->ops->nmd_if_delete(nmd, na, nif);
2852ff91c17SVincenzo Maffione 	NMA_UNLOCK(nmd);
2862ff91c17SVincenzo Maffione }
2872ff91c17SVincenzo Maffione 
2882ff91c17SVincenzo Maffione int
2892ff91c17SVincenzo Maffione netmap_mem_rings_create(struct netmap_adapter *na)
2902ff91c17SVincenzo Maffione {
2912ff91c17SVincenzo Maffione 	int rv;
2922ff91c17SVincenzo Maffione 	struct netmap_mem_d *nmd = na->nm_mem;
2932ff91c17SVincenzo Maffione 
2942ff91c17SVincenzo Maffione 	NMA_LOCK(nmd);
295a6d768d8SVincenzo Maffione 	rv = nmd->ops->nmd_rings_create(nmd, na);
2962ff91c17SVincenzo Maffione 	NMA_UNLOCK(nmd);
2972ff91c17SVincenzo Maffione 
2982ff91c17SVincenzo Maffione 	return rv;
2992ff91c17SVincenzo Maffione }
3002ff91c17SVincenzo Maffione 
3012ff91c17SVincenzo Maffione void
3022ff91c17SVincenzo Maffione netmap_mem_rings_delete(struct netmap_adapter *na)
3032ff91c17SVincenzo Maffione {
3042ff91c17SVincenzo Maffione 	struct netmap_mem_d *nmd = na->nm_mem;
3052ff91c17SVincenzo Maffione 
3062ff91c17SVincenzo Maffione 	NMA_LOCK(nmd);
307a6d768d8SVincenzo Maffione 	nmd->ops->nmd_rings_delete(nmd, na);
3082ff91c17SVincenzo Maffione 	NMA_UNLOCK(nmd);
3092ff91c17SVincenzo Maffione }
310847bf383SLuigi Rizzo 
311847bf383SLuigi Rizzo static int netmap_mem_map(struct netmap_obj_pool *, struct netmap_adapter *);
312847bf383SLuigi Rizzo static int netmap_mem_unmap(struct netmap_obj_pool *, struct netmap_adapter *);
31370275a67SVincenzo Maffione static int nm_mem_check_group(struct netmap_mem_d *, bus_dma_tag_t);
314c3e9b4dbSLuiz Otavio O Souza static void nm_mem_release_id(struct netmap_mem_d *);
315c3e9b4dbSLuiz Otavio O Souza 
316c3e9b4dbSLuiz Otavio O Souza nm_memid_t
317c3e9b4dbSLuiz Otavio O Souza netmap_mem_get_id(struct netmap_mem_d *nmd)
318c3e9b4dbSLuiz Otavio O Souza {
319c3e9b4dbSLuiz Otavio O Souza 	return nmd->nm_id;
320c3e9b4dbSLuiz Otavio O Souza }
321847bf383SLuigi Rizzo 
322847bf383SLuigi Rizzo #ifdef NM_DEBUG_MEM_PUTGET
323847bf383SLuigi Rizzo #define NM_DBG_REFC(nmd, func, line)	\
32409a18933SVincenzo Maffione 	nm_prinf("%s:%d mem[%d:%d] -> %d", func, line, (nmd)->nm_id, (nmd)->nm_grp, (nmd)->refcount);
325847bf383SLuigi Rizzo #else
326847bf383SLuigi Rizzo #define NM_DBG_REFC(nmd, func, line)
327847bf383SLuigi Rizzo #endif
328847bf383SLuigi Rizzo 
329c3e9b4dbSLuiz Otavio O Souza /* circular list of all existing allocators */
330c3e9b4dbSLuiz Otavio O Souza static struct netmap_mem_d *netmap_last_mem_d = &nm_mem;
331a6d768d8SVincenzo Maffione static NM_MTX_T nm_mem_list_lock;
332c3e9b4dbSLuiz Otavio O Souza 
333c3e9b4dbSLuiz Otavio O Souza struct netmap_mem_d *
334c3e9b4dbSLuiz Otavio O Souza __netmap_mem_get(struct netmap_mem_d *nmd, const char *func, int line)
335847bf383SLuigi Rizzo {
336c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_LOCK(nm_mem_list_lock);
337847bf383SLuigi Rizzo 	nmd->refcount++;
338847bf383SLuigi Rizzo 	NM_DBG_REFC(nmd, func, line);
339c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_UNLOCK(nm_mem_list_lock);
340c3e9b4dbSLuiz Otavio O Souza 	return nmd;
341847bf383SLuigi Rizzo }
342847bf383SLuigi Rizzo 
343c3e9b4dbSLuiz Otavio O Souza void
344c3e9b4dbSLuiz Otavio O Souza __netmap_mem_put(struct netmap_mem_d *nmd, const char *func, int line)
345847bf383SLuigi Rizzo {
346847bf383SLuigi Rizzo 	int last;
347c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_LOCK(nm_mem_list_lock);
348847bf383SLuigi Rizzo 	last = (--nmd->refcount == 0);
349c3e9b4dbSLuiz Otavio O Souza 	if (last)
350c3e9b4dbSLuiz Otavio O Souza 		nm_mem_release_id(nmd);
351847bf383SLuigi Rizzo 	NM_DBG_REFC(nmd, func, line);
352c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_UNLOCK(nm_mem_list_lock);
353847bf383SLuigi Rizzo 	if (last)
354847bf383SLuigi Rizzo 		netmap_mem_delete(nmd);
355847bf383SLuigi Rizzo }
356847bf383SLuigi Rizzo 
357847bf383SLuigi Rizzo int
358847bf383SLuigi Rizzo netmap_mem_finalize(struct netmap_mem_d *nmd, struct netmap_adapter *na)
359847bf383SLuigi Rizzo {
3602ff91c17SVincenzo Maffione 	int lasterr = 0;
361a6d768d8SVincenzo Maffione 	if (nm_mem_check_group(nmd, na->pdev) < 0) {
362847bf383SLuigi Rizzo 		return ENOMEM;
363847bf383SLuigi Rizzo 	}
364847bf383SLuigi Rizzo 
3652ff91c17SVincenzo Maffione 	NMA_LOCK(nmd);
3662ff91c17SVincenzo Maffione 
3672ff91c17SVincenzo Maffione 	if (netmap_mem_config(nmd))
3682ff91c17SVincenzo Maffione 		goto out;
3692ff91c17SVincenzo Maffione 
3702ff91c17SVincenzo Maffione 	nmd->active++;
3712ff91c17SVincenzo Maffione 
372a6d768d8SVincenzo Maffione 	nmd->lasterr = nmd->ops->nmd_finalize(nmd, na);
3732ff91c17SVincenzo Maffione 
374a6d768d8SVincenzo Maffione 	if (!nmd->lasterr && !(nmd->flags & NETMAP_MEM_NOMAP)) {
3754f80b14cSVincenzo Maffione 		nmd->lasterr = netmap_mem_map(&nmd->pools[NETMAP_BUF_POOL], na);
3764f80b14cSVincenzo Maffione 	}
377847bf383SLuigi Rizzo 
3782ff91c17SVincenzo Maffione out:
3792ff91c17SVincenzo Maffione 	lasterr = nmd->lasterr;
3802ff91c17SVincenzo Maffione 	NMA_UNLOCK(nmd);
3812ff91c17SVincenzo Maffione 
3822ff91c17SVincenzo Maffione 	if (lasterr)
3832ff91c17SVincenzo Maffione 		netmap_mem_deref(nmd, na);
3842ff91c17SVincenzo Maffione 
3852ff91c17SVincenzo Maffione 	return lasterr;
386847bf383SLuigi Rizzo }
387847bf383SLuigi Rizzo 
3884f80b14cSVincenzo Maffione static int
3894f80b14cSVincenzo Maffione nm_isset(uint32_t *bitmap, u_int i)
390847bf383SLuigi Rizzo {
3914f80b14cSVincenzo Maffione 	return bitmap[ (i>>5) ] & ( 1U << (i & 31U) );
3924f80b14cSVincenzo Maffione }
39337e3a6d3SLuigi Rizzo 
39437e3a6d3SLuigi Rizzo 
3954f80b14cSVincenzo Maffione static int
3964f80b14cSVincenzo Maffione netmap_init_obj_allocator_bitmap(struct netmap_obj_pool *p)
3974f80b14cSVincenzo Maffione {
3984f80b14cSVincenzo Maffione 	u_int n, j;
39937e3a6d3SLuigi Rizzo 
4004f80b14cSVincenzo Maffione 	if (p->bitmap == NULL) {
4014f80b14cSVincenzo Maffione 		/* Allocate the bitmap */
4024f80b14cSVincenzo Maffione 		n = (p->objtotal + 31) / 32;
403b6e66be2SVincenzo Maffione 		p->bitmap = nm_os_malloc(sizeof(p->bitmap[0]) * n);
4044f80b14cSVincenzo Maffione 		if (p->bitmap == NULL) {
405b6e66be2SVincenzo Maffione 			nm_prerr("Unable to create bitmap (%d entries) for allocator '%s'", (int)n,
4064f80b14cSVincenzo Maffione 			    p->name);
4074f80b14cSVincenzo Maffione 			return ENOMEM;
4084f80b14cSVincenzo Maffione 		}
4094f80b14cSVincenzo Maffione 		p->bitmap_slots = n;
4104f80b14cSVincenzo Maffione 	} else {
411b6e66be2SVincenzo Maffione 		memset(p->bitmap, 0, p->bitmap_slots * sizeof(p->bitmap[0]));
4124f80b14cSVincenzo Maffione 	}
4134f80b14cSVincenzo Maffione 
4144f80b14cSVincenzo Maffione 	p->objfree = 0;
41537e3a6d3SLuigi Rizzo 	/*
41637e3a6d3SLuigi Rizzo 	 * Set all the bits in the bitmap that have
41737e3a6d3SLuigi Rizzo 	 * corresponding buffers to 1 to indicate they are
41837e3a6d3SLuigi Rizzo 	 * free.
41937e3a6d3SLuigi Rizzo 	 */
42037e3a6d3SLuigi Rizzo 	for (j = 0; j < p->objtotal; j++) {
4214f80b14cSVincenzo Maffione 		if (p->invalid_bitmap && nm_isset(p->invalid_bitmap, j)) {
422b6e66be2SVincenzo Maffione 			if (netmap_debug & NM_DEBUG_MEM)
423b6e66be2SVincenzo Maffione 				nm_prinf("skipping %s %d", p->name, j);
4244f80b14cSVincenzo Maffione 			continue;
42537e3a6d3SLuigi Rizzo 		}
4264f80b14cSVincenzo Maffione 		p->bitmap[ (j>>5) ] |=  ( 1U << (j & 31U) );
4274f80b14cSVincenzo Maffione 		p->objfree++;
42837e3a6d3SLuigi Rizzo 	}
4294f80b14cSVincenzo Maffione 
430b6e66be2SVincenzo Maffione 	if (netmap_verbose)
431b6e66be2SVincenzo Maffione 		nm_prinf("%s free %u", p->name, p->objfree);
432b6e66be2SVincenzo Maffione 	if (p->objfree == 0) {
433b6e66be2SVincenzo Maffione 		if (netmap_verbose)
434b6e66be2SVincenzo Maffione 			nm_prerr("%s: no objects available", p->name);
4354f80b14cSVincenzo Maffione 		return ENOMEM;
436b6e66be2SVincenzo Maffione 	}
4374f80b14cSVincenzo Maffione 
4384f80b14cSVincenzo Maffione 	return 0;
4394f80b14cSVincenzo Maffione }
4404f80b14cSVincenzo Maffione 
4414f80b14cSVincenzo Maffione static int
4424f80b14cSVincenzo Maffione netmap_mem_init_bitmaps(struct netmap_mem_d *nmd)
4434f80b14cSVincenzo Maffione {
4444f80b14cSVincenzo Maffione 	int i, error = 0;
4454f80b14cSVincenzo Maffione 
4464f80b14cSVincenzo Maffione 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
4474f80b14cSVincenzo Maffione 		struct netmap_obj_pool *p = &nmd->pools[i];
4484f80b14cSVincenzo Maffione 
4494f80b14cSVincenzo Maffione 		error = netmap_init_obj_allocator_bitmap(p);
4504f80b14cSVincenzo Maffione 		if (error)
4514f80b14cSVincenzo Maffione 			return error;
45237e3a6d3SLuigi Rizzo 	}
45337e3a6d3SLuigi Rizzo 
45437e3a6d3SLuigi Rizzo 	/*
45537e3a6d3SLuigi Rizzo 	 * buffers 0 and 1 are reserved
45637e3a6d3SLuigi Rizzo 	 */
4574f80b14cSVincenzo Maffione 	if (nmd->pools[NETMAP_BUF_POOL].objfree < 2) {
458b6e66be2SVincenzo Maffione 		nm_prerr("%s: not enough buffers", nmd->pools[NETMAP_BUF_POOL].name);
4594f80b14cSVincenzo Maffione 		return ENOMEM;
4604f80b14cSVincenzo Maffione 	}
4614f80b14cSVincenzo Maffione 
46237e3a6d3SLuigi Rizzo 	nmd->pools[NETMAP_BUF_POOL].objfree -= 2;
46337e3a6d3SLuigi Rizzo 	if (nmd->pools[NETMAP_BUF_POOL].bitmap) {
46437e3a6d3SLuigi Rizzo 		/* XXX This check is a workaround that prevents a
46537e3a6d3SLuigi Rizzo 		 * NULL pointer crash which currently happens only
466844a6f0cSLuigi Rizzo 		 * with ptnetmap guests.
467844a6f0cSLuigi Rizzo 		 * Removed shared-info --> is the bug still there? */
4684f80b14cSVincenzo Maffione 		nmd->pools[NETMAP_BUF_POOL].bitmap[0] = ~3U;
46937e3a6d3SLuigi Rizzo 	}
4704f80b14cSVincenzo Maffione 	return 0;
4714f80b14cSVincenzo Maffione }
4724f80b14cSVincenzo Maffione 
4734f80b14cSVincenzo Maffione int
4744f80b14cSVincenzo Maffione netmap_mem_deref(struct netmap_mem_d *nmd, struct netmap_adapter *na)
4754f80b14cSVincenzo Maffione {
4764f80b14cSVincenzo Maffione 	int last_user = 0;
4774f80b14cSVincenzo Maffione 	NMA_LOCK(nmd);
478a6d768d8SVincenzo Maffione 	if (na->active_fds <= 0 && !(nmd->flags & NETMAP_MEM_NOMAP))
4794f80b14cSVincenzo Maffione 		netmap_mem_unmap(&nmd->pools[NETMAP_BUF_POOL], na);
4804f80b14cSVincenzo Maffione 	if (nmd->active == 1) {
4814f80b14cSVincenzo Maffione 		last_user = 1;
4824f80b14cSVincenzo Maffione 		/*
4834f80b14cSVincenzo Maffione 		 * Reset the allocator when it falls out of use so that any
4844f80b14cSVincenzo Maffione 		 * pool resources leaked by unclean application exits are
4854f80b14cSVincenzo Maffione 		 * reclaimed.
4864f80b14cSVincenzo Maffione 		 */
4874f80b14cSVincenzo Maffione 		netmap_mem_init_bitmaps(nmd);
48837e3a6d3SLuigi Rizzo 	}
489a6d768d8SVincenzo Maffione 	nmd->ops->nmd_deref(nmd, na);
49037e3a6d3SLuigi Rizzo 
4912ff91c17SVincenzo Maffione 	nmd->active--;
492b6e66be2SVincenzo Maffione 	if (last_user) {
493b6e66be2SVincenzo Maffione 		nmd->lasterr = 0;
494b6e66be2SVincenzo Maffione 	}
4952ff91c17SVincenzo Maffione 
496847bf383SLuigi Rizzo 	NMA_UNLOCK(nmd);
4974f80b14cSVincenzo Maffione 	return last_user;
498847bf383SLuigi Rizzo }
499847bf383SLuigi Rizzo 
500847bf383SLuigi Rizzo 
5014bf50f18SLuigi Rizzo /* accessor functions */
50237e3a6d3SLuigi Rizzo static int
503847bf383SLuigi Rizzo netmap_mem2_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut)
5044bf50f18SLuigi Rizzo {
505847bf383SLuigi Rizzo 	lut->lut = nmd->pools[NETMAP_BUF_POOL].lut;
5064f80b14cSVincenzo Maffione #ifdef __FreeBSD__
5074f80b14cSVincenzo Maffione 	lut->plut = lut->lut;
5084f80b14cSVincenzo Maffione #endif
509847bf383SLuigi Rizzo 	lut->objtotal = nmd->pools[NETMAP_BUF_POOL].objtotal;
510847bf383SLuigi Rizzo 	lut->objsize = nmd->pools[NETMAP_BUF_POOL]._objsize;
51137e3a6d3SLuigi Rizzo 
51237e3a6d3SLuigi Rizzo 	return 0;
5134bf50f18SLuigi Rizzo }
5144bf50f18SLuigi Rizzo 
51537e3a6d3SLuigi Rizzo static struct netmap_obj_params netmap_min_priv_params[NETMAP_POOLS_NR] = {
516f0ea3689SLuigi Rizzo 	[NETMAP_IF_POOL] = {
517f0ea3689SLuigi Rizzo 		.size = 1024,
51837e3a6d3SLuigi Rizzo 		.num  = 2,
519f0ea3689SLuigi Rizzo 	},
520f0ea3689SLuigi Rizzo 	[NETMAP_RING_POOL] = {
521f0ea3689SLuigi Rizzo 		.size = 5*PAGE_SIZE,
522f0ea3689SLuigi Rizzo 		.num  = 4,
523f0ea3689SLuigi Rizzo 	},
524f0ea3689SLuigi Rizzo 	[NETMAP_BUF_POOL] = {
525f0ea3689SLuigi Rizzo 		.size = 2048,
526f0ea3689SLuigi Rizzo 		.num  = 4098,
527f0ea3689SLuigi Rizzo 	},
528f0ea3689SLuigi Rizzo };
529f0ea3689SLuigi Rizzo 
530ccdc3305SLuigi Rizzo 
5312579e2d7SLuigi Rizzo /*
5322579e2d7SLuigi Rizzo  * nm_mem is the memory allocator used for all physical interfaces
5332579e2d7SLuigi Rizzo  * running in netmap mode.
5342579e2d7SLuigi Rizzo  * Virtual (VALE) ports will have each its own allocator.
5352579e2d7SLuigi Rizzo  */
536*893caa2aSMark Johnston extern const struct netmap_mem_ops netmap_mem_global_ops; /* forward */
537ce3ee1e7SLuigi Rizzo struct netmap_mem_d nm_mem = {	/* Our memory allocator. */
5388241616dSLuigi Rizzo 	.pools = {
5398241616dSLuigi Rizzo 		[NETMAP_IF_POOL] = {
5408241616dSLuigi Rizzo 			.name 	= "netmap_if",
5418241616dSLuigi Rizzo 			.objminsize = sizeof(struct netmap_if),
5428241616dSLuigi Rizzo 			.objmaxsize = 4096,
5438241616dSLuigi Rizzo 			.nummin     = 10,	/* don't be stingy */
5448241616dSLuigi Rizzo 			.nummax	    = 10000,	/* XXX very large */
5458241616dSLuigi Rizzo 		},
5468241616dSLuigi Rizzo 		[NETMAP_RING_POOL] = {
5478241616dSLuigi Rizzo 			.name 	= "netmap_ring",
5488241616dSLuigi Rizzo 			.objminsize = sizeof(struct netmap_ring),
5498241616dSLuigi Rizzo 			.objmaxsize = 32*PAGE_SIZE,
5508241616dSLuigi Rizzo 			.nummin     = 2,
5518241616dSLuigi Rizzo 			.nummax	    = 1024,
5528241616dSLuigi Rizzo 		},
5538241616dSLuigi Rizzo 		[NETMAP_BUF_POOL] = {
5548241616dSLuigi Rizzo 			.name	= "netmap_buf",
5558241616dSLuigi Rizzo 			.objminsize = 64,
5568241616dSLuigi Rizzo 			.objmaxsize = 65536,
5578241616dSLuigi Rizzo 			.nummin     = 4,
5588241616dSLuigi Rizzo 			.nummax	    = 1000000, /* one million! */
5598241616dSLuigi Rizzo 		},
5608241616dSLuigi Rizzo 	},
561f0ea3689SLuigi Rizzo 
562c3e9b4dbSLuiz Otavio O Souza 	.params = {
563c3e9b4dbSLuiz Otavio O Souza 		[NETMAP_IF_POOL] = {
564c3e9b4dbSLuiz Otavio O Souza 			.size = 1024,
565c3e9b4dbSLuiz Otavio O Souza 			.num  = 100,
566c3e9b4dbSLuiz Otavio O Souza 		},
567c3e9b4dbSLuiz Otavio O Souza 		[NETMAP_RING_POOL] = {
568c3e9b4dbSLuiz Otavio O Souza 			.size = 9*PAGE_SIZE,
569c3e9b4dbSLuiz Otavio O Souza 			.num  = 200,
570c3e9b4dbSLuiz Otavio O Souza 		},
571c3e9b4dbSLuiz Otavio O Souza 		[NETMAP_BUF_POOL] = {
572c3e9b4dbSLuiz Otavio O Souza 			.size = 2048,
573c3e9b4dbSLuiz Otavio O Souza 			.num  = NETMAP_BUF_MAX_NUM,
574c3e9b4dbSLuiz Otavio O Souza 		},
575c3e9b4dbSLuiz Otavio O Souza 	},
576c3e9b4dbSLuiz Otavio O Souza 
577f0ea3689SLuigi Rizzo 	.nm_id = 1,
5784bf50f18SLuigi Rizzo 	.nm_grp = -1,
579f0ea3689SLuigi Rizzo 
580f0ea3689SLuigi Rizzo 	.prev = &nm_mem,
581f0ea3689SLuigi Rizzo 	.next = &nm_mem,
582847bf383SLuigi Rizzo 
583c3e9b4dbSLuiz Otavio O Souza 	.ops = &netmap_mem_global_ops,
584c3e9b4dbSLuiz Otavio O Souza 
585c3e9b4dbSLuiz Otavio O Souza 	.name = "1"
586ccdc3305SLuigi Rizzo };
587ccdc3305SLuigi Rizzo 
588a6d768d8SVincenzo Maffione static struct netmap_mem_d nm_mem_blueprint;
589ce3ee1e7SLuigi Rizzo 
590ce3ee1e7SLuigi Rizzo /* blueprint for the private memory allocators */
59137e3a6d3SLuigi Rizzo /* XXX clang is not happy about using name as a print format */
59237e3a6d3SLuigi Rizzo static const struct netmap_mem_d nm_blueprint = {
593ce3ee1e7SLuigi Rizzo 	.pools = {
594ce3ee1e7SLuigi Rizzo 		[NETMAP_IF_POOL] = {
595ce3ee1e7SLuigi Rizzo 			.name 	= "%s_if",
596ce3ee1e7SLuigi Rizzo 			.objminsize = sizeof(struct netmap_if),
597ce3ee1e7SLuigi Rizzo 			.objmaxsize = 4096,
598ce3ee1e7SLuigi Rizzo 			.nummin     = 1,
599f0ea3689SLuigi Rizzo 			.nummax	    = 100,
600ce3ee1e7SLuigi Rizzo 		},
601ce3ee1e7SLuigi Rizzo 		[NETMAP_RING_POOL] = {
602ce3ee1e7SLuigi Rizzo 			.name 	= "%s_ring",
603ce3ee1e7SLuigi Rizzo 			.objminsize = sizeof(struct netmap_ring),
604ce3ee1e7SLuigi Rizzo 			.objmaxsize = 32*PAGE_SIZE,
605ce3ee1e7SLuigi Rizzo 			.nummin     = 2,
606ce3ee1e7SLuigi Rizzo 			.nummax	    = 1024,
607ce3ee1e7SLuigi Rizzo 		},
608ce3ee1e7SLuigi Rizzo 		[NETMAP_BUF_POOL] = {
609ce3ee1e7SLuigi Rizzo 			.name	= "%s_buf",
610ce3ee1e7SLuigi Rizzo 			.objminsize = 64,
611ce3ee1e7SLuigi Rizzo 			.objmaxsize = 65536,
612ce3ee1e7SLuigi Rizzo 			.nummin     = 4,
613ce3ee1e7SLuigi Rizzo 			.nummax	    = 1000000, /* one million! */
614ce3ee1e7SLuigi Rizzo 		},
615ce3ee1e7SLuigi Rizzo 	},
616ce3ee1e7SLuigi Rizzo 
617c3e9b4dbSLuiz Otavio O Souza 	.nm_grp = -1,
618c3e9b4dbSLuiz Otavio O Souza 
619ce3ee1e7SLuigi Rizzo 	.flags = NETMAP_MEM_PRIVATE,
620847bf383SLuigi Rizzo 
621c3e9b4dbSLuiz Otavio O Souza 	.ops = &netmap_mem_global_ops,
622ce3ee1e7SLuigi Rizzo };
623ce3ee1e7SLuigi Rizzo 
6248241616dSLuigi Rizzo /* memory allocator related sysctls */
6258241616dSLuigi Rizzo 
6268241616dSLuigi Rizzo #define STRINGIFY(x) #x
6278241616dSLuigi Rizzo 
628ce3ee1e7SLuigi Rizzo 
6298241616dSLuigi Rizzo #define DECLARE_SYSCTLS(id, name) \
63037e3a6d3SLuigi Rizzo 	SYSBEGIN(mem2_ ## name); \
6318241616dSLuigi Rizzo 	SYSCTL_INT(_dev_netmap, OID_AUTO, name##_size, \
632c3e9b4dbSLuiz Otavio O Souza 	    CTLFLAG_RW, &nm_mem.params[id].size, 0, "Requested size of netmap " STRINGIFY(name) "s"); \
6338241616dSLuigi Rizzo 	SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_size, \
6348241616dSLuigi Rizzo 	    CTLFLAG_RD, &nm_mem.pools[id]._objsize, 0, "Current size of netmap " STRINGIFY(name) "s"); \
6358241616dSLuigi Rizzo 	SYSCTL_INT(_dev_netmap, OID_AUTO, name##_num, \
636c3e9b4dbSLuiz Otavio O Souza 	    CTLFLAG_RW, &nm_mem.params[id].num, 0, "Requested number of netmap " STRINGIFY(name) "s"); \
6378241616dSLuigi Rizzo 	SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_num, \
638f0ea3689SLuigi Rizzo 	    CTLFLAG_RD, &nm_mem.pools[id].objtotal, 0, "Current number of netmap " STRINGIFY(name) "s"); \
639f0ea3689SLuigi Rizzo 	SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_size, \
640f0ea3689SLuigi Rizzo 	    CTLFLAG_RW, &netmap_min_priv_params[id].size, 0, \
641f0ea3689SLuigi Rizzo 	    "Default size of private netmap " STRINGIFY(name) "s"); \
642f0ea3689SLuigi Rizzo 	SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_num, \
643f0ea3689SLuigi Rizzo 	    CTLFLAG_RW, &netmap_min_priv_params[id].num, 0, \
64437e3a6d3SLuigi Rizzo 	    "Default number of private netmap " STRINGIFY(name) "s");	\
64537e3a6d3SLuigi Rizzo 	SYSEND
6468241616dSLuigi Rizzo 
647984ff0d9SEd Maste SYSCTL_DECL(_dev_netmap);
6488241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_IF_POOL, if);
6498241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_RING_POOL, ring);
6508241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_BUF_POOL, buf);
651ccdc3305SLuigi Rizzo 
652c3e9b4dbSLuiz Otavio O Souza /* call with nm_mem_list_lock held */
653f0ea3689SLuigi Rizzo static int
654a6d768d8SVincenzo Maffione nm_mem_assign_id_locked(struct netmap_mem_d *nmd, int grp_id)
655f0ea3689SLuigi Rizzo {
656f0ea3689SLuigi Rizzo 	nm_memid_t id;
657f0ea3689SLuigi Rizzo 	struct netmap_mem_d *scan = netmap_last_mem_d;
658f0ea3689SLuigi Rizzo 	int error = ENOMEM;
659f0ea3689SLuigi Rizzo 
660f0ea3689SLuigi Rizzo 	do {
661f0ea3689SLuigi Rizzo 		/* we rely on unsigned wrap around */
662f0ea3689SLuigi Rizzo 		id = scan->nm_id + 1;
663f0ea3689SLuigi Rizzo 		if (id == 0) /* reserve 0 as error value */
664f0ea3689SLuigi Rizzo 			id = 1;
665f0ea3689SLuigi Rizzo 		scan = scan->next;
666f0ea3689SLuigi Rizzo 		if (id != scan->nm_id) {
667f0ea3689SLuigi Rizzo 			nmd->nm_id = id;
668a6d768d8SVincenzo Maffione 			nmd->nm_grp = grp_id;
669f0ea3689SLuigi Rizzo 			nmd->prev = scan->prev;
670f0ea3689SLuigi Rizzo 			nmd->next = scan;
671f0ea3689SLuigi Rizzo 			scan->prev->next = nmd;
672f0ea3689SLuigi Rizzo 			scan->prev = nmd;
673f0ea3689SLuigi Rizzo 			netmap_last_mem_d = nmd;
674c3e9b4dbSLuiz Otavio O Souza 			nmd->refcount = 1;
675c3e9b4dbSLuiz Otavio O Souza 			NM_DBG_REFC(nmd, __FUNCTION__, __LINE__);
676f0ea3689SLuigi Rizzo 			error = 0;
677f0ea3689SLuigi Rizzo 			break;
678f0ea3689SLuigi Rizzo 		}
679f0ea3689SLuigi Rizzo 	} while (scan != netmap_last_mem_d);
680f0ea3689SLuigi Rizzo 
681f0ea3689SLuigi Rizzo 	return error;
682f0ea3689SLuigi Rizzo }
683f0ea3689SLuigi Rizzo 
684c3e9b4dbSLuiz Otavio O Souza /* call with nm_mem_list_lock *not* held */
68537e3a6d3SLuigi Rizzo static int
686a6d768d8SVincenzo Maffione nm_mem_assign_id(struct netmap_mem_d *nmd, int grp_id)
68737e3a6d3SLuigi Rizzo {
68837e3a6d3SLuigi Rizzo 	int ret;
68937e3a6d3SLuigi Rizzo 
690c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_LOCK(nm_mem_list_lock);
691a6d768d8SVincenzo Maffione 	ret = nm_mem_assign_id_locked(nmd, grp_id);
692c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_UNLOCK(nm_mem_list_lock);
69337e3a6d3SLuigi Rizzo 
69437e3a6d3SLuigi Rizzo 	return ret;
69537e3a6d3SLuigi Rizzo }
69637e3a6d3SLuigi Rizzo 
697c3e9b4dbSLuiz Otavio O Souza /* call with nm_mem_list_lock held */
698f0ea3689SLuigi Rizzo static void
699f0ea3689SLuigi Rizzo nm_mem_release_id(struct netmap_mem_d *nmd)
700f0ea3689SLuigi Rizzo {
701f0ea3689SLuigi Rizzo 	nmd->prev->next = nmd->next;
702f0ea3689SLuigi Rizzo 	nmd->next->prev = nmd->prev;
703f0ea3689SLuigi Rizzo 
704f0ea3689SLuigi Rizzo 	if (netmap_last_mem_d == nmd)
705f0ea3689SLuigi Rizzo 		netmap_last_mem_d = nmd->prev;
706f0ea3689SLuigi Rizzo 
707f0ea3689SLuigi Rizzo 	nmd->prev = nmd->next = NULL;
708c3e9b4dbSLuiz Otavio O Souza }
709f0ea3689SLuigi Rizzo 
710c3e9b4dbSLuiz Otavio O Souza struct netmap_mem_d *
711c3e9b4dbSLuiz Otavio O Souza netmap_mem_find(nm_memid_t id)
712c3e9b4dbSLuiz Otavio O Souza {
713c3e9b4dbSLuiz Otavio O Souza 	struct netmap_mem_d *nmd;
714c3e9b4dbSLuiz Otavio O Souza 
715c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_LOCK(nm_mem_list_lock);
716c3e9b4dbSLuiz Otavio O Souza 	nmd = netmap_last_mem_d;
717c3e9b4dbSLuiz Otavio O Souza 	do {
718c3e9b4dbSLuiz Otavio O Souza 		if (!(nmd->flags & NETMAP_MEM_HIDDEN) && nmd->nm_id == id) {
719c3e9b4dbSLuiz Otavio O Souza 			nmd->refcount++;
720c3e9b4dbSLuiz Otavio O Souza 			NM_DBG_REFC(nmd, __FUNCTION__, __LINE__);
721c3e9b4dbSLuiz Otavio O Souza 			NM_MTX_UNLOCK(nm_mem_list_lock);
722c3e9b4dbSLuiz Otavio O Souza 			return nmd;
723c3e9b4dbSLuiz Otavio O Souza 		}
724c3e9b4dbSLuiz Otavio O Souza 		nmd = nmd->next;
725c3e9b4dbSLuiz Otavio O Souza 	} while (nmd != netmap_last_mem_d);
726c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_UNLOCK(nm_mem_list_lock);
727c3e9b4dbSLuiz Otavio O Souza 	return NULL;
728f0ea3689SLuigi Rizzo }
729f0ea3689SLuigi Rizzo 
7304bf50f18SLuigi Rizzo static int
73170275a67SVincenzo Maffione nm_mem_check_group(struct netmap_mem_d *nmd, bus_dma_tag_t dev)
7324bf50f18SLuigi Rizzo {
7334bf50f18SLuigi Rizzo 	int err = 0, id;
734a6d768d8SVincenzo Maffione 
735a6d768d8SVincenzo Maffione 	/* Skip not hw adapters.
736a6d768d8SVincenzo Maffione 	 * Vale port can use particular allocator through vale-ctl -m option
737a6d768d8SVincenzo Maffione 	 */
738a6d768d8SVincenzo Maffione 	if (!dev)
739a6d768d8SVincenzo Maffione 		return 0;
7404bf50f18SLuigi Rizzo 	id = nm_iommu_group_id(dev);
741b6e66be2SVincenzo Maffione 	if (netmap_debug & NM_DEBUG_MEM)
742b6e66be2SVincenzo Maffione 		nm_prinf("iommu_group %d", id);
7434bf50f18SLuigi Rizzo 
7444bf50f18SLuigi Rizzo 	NMA_LOCK(nmd);
7454bf50f18SLuigi Rizzo 
746b6e66be2SVincenzo Maffione 	if (nmd->nm_grp != id) {
747b6e66be2SVincenzo Maffione 		if (netmap_verbose)
748a6d768d8SVincenzo Maffione 			nm_prerr("iommu group mismatch: %d vs %d",
749b6e66be2SVincenzo Maffione 					nmd->nm_grp, id);
7504bf50f18SLuigi Rizzo 		nmd->lasterr = err = ENOMEM;
751b6e66be2SVincenzo Maffione 	}
7524bf50f18SLuigi Rizzo 
7534bf50f18SLuigi Rizzo 	NMA_UNLOCK(nmd);
7544bf50f18SLuigi Rizzo 	return err;
7554bf50f18SLuigi Rizzo }
756f0ea3689SLuigi Rizzo 
7574f80b14cSVincenzo Maffione static struct lut_entry *
7584f80b14cSVincenzo Maffione nm_alloc_lut(u_int nobj)
7594f80b14cSVincenzo Maffione {
7604f80b14cSVincenzo Maffione 	size_t n = sizeof(struct lut_entry) * nobj;
7614f80b14cSVincenzo Maffione 	struct lut_entry *lut;
7624f80b14cSVincenzo Maffione #ifdef linux
7634f80b14cSVincenzo Maffione 	lut = vmalloc(n);
7644f80b14cSVincenzo Maffione #else
7654f80b14cSVincenzo Maffione 	lut = nm_os_malloc(n);
7664f80b14cSVincenzo Maffione #endif
7674f80b14cSVincenzo Maffione 	return lut;
7684f80b14cSVincenzo Maffione }
7694f80b14cSVincenzo Maffione 
7704f80b14cSVincenzo Maffione static void
7714f80b14cSVincenzo Maffione nm_free_lut(struct lut_entry *lut, u_int objtotal)
7724f80b14cSVincenzo Maffione {
7734f80b14cSVincenzo Maffione 	bzero(lut, sizeof(struct lut_entry) * objtotal);
7744f80b14cSVincenzo Maffione #ifdef linux
7754f80b14cSVincenzo Maffione 	vfree(lut);
7764f80b14cSVincenzo Maffione #else
7774f80b14cSVincenzo Maffione 	nm_os_free(lut);
7784f80b14cSVincenzo Maffione #endif
7794f80b14cSVincenzo Maffione }
7804f80b14cSVincenzo Maffione 
7814f80b14cSVincenzo Maffione #if defined(linux) || defined(_WIN32)
7824f80b14cSVincenzo Maffione static struct plut_entry *
7834f80b14cSVincenzo Maffione nm_alloc_plut(u_int nobj)
7844f80b14cSVincenzo Maffione {
7854f80b14cSVincenzo Maffione 	size_t n = sizeof(struct plut_entry) * nobj;
7864f80b14cSVincenzo Maffione 	struct plut_entry *lut;
7874f80b14cSVincenzo Maffione 	lut = vmalloc(n);
7884f80b14cSVincenzo Maffione 	return lut;
7894f80b14cSVincenzo Maffione }
7904f80b14cSVincenzo Maffione 
7914f80b14cSVincenzo Maffione static void
7924f80b14cSVincenzo Maffione nm_free_plut(struct plut_entry * lut)
7934f80b14cSVincenzo Maffione {
7944f80b14cSVincenzo Maffione 	vfree(lut);
7954f80b14cSVincenzo Maffione }
7964f80b14cSVincenzo Maffione #endif /* linux or _WIN32 */
7974f80b14cSVincenzo Maffione 
7984f80b14cSVincenzo Maffione 
799ccdc3305SLuigi Rizzo /*
8002579e2d7SLuigi Rizzo  * First, find the allocator that contains the requested offset,
8012579e2d7SLuigi Rizzo  * then locate the cluster through a lookup table.
802ccdc3305SLuigi Rizzo  */
803847bf383SLuigi Rizzo static vm_paddr_t
804847bf383SLuigi Rizzo netmap_mem2_ofstophys(struct netmap_mem_d* nmd, vm_ooffset_t offset)
805ccdc3305SLuigi Rizzo {
806ccdc3305SLuigi Rizzo 	int i;
807ce3ee1e7SLuigi Rizzo 	vm_ooffset_t o = offset;
808ce3ee1e7SLuigi Rizzo 	vm_paddr_t pa;
809ce3ee1e7SLuigi Rizzo 	struct netmap_obj_pool *p;
810ccdc3305SLuigi Rizzo 
811ce3ee1e7SLuigi Rizzo 	p = nmd->pools;
812ce3ee1e7SLuigi Rizzo 
813ce3ee1e7SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; offset -= p[i].memtotal, i++) {
814ce3ee1e7SLuigi Rizzo 		if (offset >= p[i].memtotal)
815ccdc3305SLuigi Rizzo 			continue;
8162579e2d7SLuigi Rizzo 		// now lookup the cluster's address
81737e3a6d3SLuigi Rizzo #ifndef _WIN32
8184bf50f18SLuigi Rizzo 		pa = vtophys(p[i].lut[offset / p[i]._objsize].vaddr) +
8198241616dSLuigi Rizzo 			offset % p[i]._objsize;
82037e3a6d3SLuigi Rizzo #else
82137e3a6d3SLuigi Rizzo 		pa = vtophys(p[i].lut[offset / p[i]._objsize].vaddr);
82237e3a6d3SLuigi Rizzo 		pa.QuadPart += offset % p[i]._objsize;
82337e3a6d3SLuigi Rizzo #endif
824ce3ee1e7SLuigi Rizzo 		return pa;
825ccdc3305SLuigi Rizzo 	}
8268241616dSLuigi Rizzo 	/* this is only in case of errors */
827253b2ec1SVincenzo Maffione 	nm_prerr("invalid ofs 0x%x out of 0x%zx 0x%zx 0x%zx", (u_int)o,
828ce3ee1e7SLuigi Rizzo 		p[NETMAP_IF_POOL].memtotal,
829ce3ee1e7SLuigi Rizzo 		p[NETMAP_IF_POOL].memtotal
830ce3ee1e7SLuigi Rizzo 			+ p[NETMAP_RING_POOL].memtotal,
831ce3ee1e7SLuigi Rizzo 		p[NETMAP_IF_POOL].memtotal
832ce3ee1e7SLuigi Rizzo 			+ p[NETMAP_RING_POOL].memtotal
833ce3ee1e7SLuigi Rizzo 			+ p[NETMAP_BUF_POOL].memtotal);
83437e3a6d3SLuigi Rizzo #ifndef _WIN32
8354f80b14cSVincenzo Maffione 	return 0; /* bad address */
83637e3a6d3SLuigi Rizzo #else
83737e3a6d3SLuigi Rizzo 	vm_paddr_t res;
83837e3a6d3SLuigi Rizzo 	res.QuadPart = 0;
83937e3a6d3SLuigi Rizzo 	return res;
84037e3a6d3SLuigi Rizzo #endif
84137e3a6d3SLuigi Rizzo }
84237e3a6d3SLuigi Rizzo 
84337e3a6d3SLuigi Rizzo #ifdef _WIN32
84437e3a6d3SLuigi Rizzo 
84537e3a6d3SLuigi Rizzo /*
84637e3a6d3SLuigi Rizzo  * win32_build_virtual_memory_for_userspace
84737e3a6d3SLuigi Rizzo  *
84837e3a6d3SLuigi Rizzo  * This function get all the object making part of the pools and maps
84937e3a6d3SLuigi Rizzo  * a contiguous virtual memory space for the userspace
85037e3a6d3SLuigi Rizzo  * It works this way
85137e3a6d3SLuigi Rizzo  * 1 - allocate a Memory Descriptor List wide as the sum
85237e3a6d3SLuigi Rizzo  *		of the memory needed for the pools
85337e3a6d3SLuigi Rizzo  * 2 - cycle all the objects in every pool and for every object do
85437e3a6d3SLuigi Rizzo  *
85537e3a6d3SLuigi Rizzo  *		2a - cycle all the objects in every pool, get the list
85637e3a6d3SLuigi Rizzo  *				of the physical address descriptors
85745c67e8fSVincenzo Maffione  *		2b - calculate the offset in the array of pages descriptor in the
85837e3a6d3SLuigi Rizzo  *				main MDL
85937e3a6d3SLuigi Rizzo  *		2c - copy the descriptors of the object in the main MDL
86037e3a6d3SLuigi Rizzo  *
86137e3a6d3SLuigi Rizzo  * 3 - return the resulting MDL that needs to be mapped in userland
86237e3a6d3SLuigi Rizzo  *
86337e3a6d3SLuigi Rizzo  * In this way we will have an MDL that describes all the memory for the
86437e3a6d3SLuigi Rizzo  * objects in a single object
86537e3a6d3SLuigi Rizzo */
86637e3a6d3SLuigi Rizzo 
86737e3a6d3SLuigi Rizzo PMDL
86837e3a6d3SLuigi Rizzo win32_build_user_vm_map(struct netmap_mem_d* nmd)
86937e3a6d3SLuigi Rizzo {
8704f80b14cSVincenzo Maffione 	u_int memflags, ofs = 0;
87137e3a6d3SLuigi Rizzo 	PMDL mainMdl, tempMdl;
8722ff91c17SVincenzo Maffione 	uint64_t memsize;
8732ff91c17SVincenzo Maffione 	int i, j;
87437e3a6d3SLuigi Rizzo 
87537e3a6d3SLuigi Rizzo 	if (netmap_mem_get_info(nmd, &memsize, &memflags, NULL)) {
876b6e66be2SVincenzo Maffione 		nm_prerr("memory not finalised yet");
87737e3a6d3SLuigi Rizzo 		return NULL;
87837e3a6d3SLuigi Rizzo 	}
87937e3a6d3SLuigi Rizzo 
88037e3a6d3SLuigi Rizzo 	mainMdl = IoAllocateMdl(NULL, memsize, FALSE, FALSE, NULL);
88137e3a6d3SLuigi Rizzo 	if (mainMdl == NULL) {
882b6e66be2SVincenzo Maffione 		nm_prerr("failed to allocate mdl");
88337e3a6d3SLuigi Rizzo 		return NULL;
88437e3a6d3SLuigi Rizzo 	}
88537e3a6d3SLuigi Rizzo 
88637e3a6d3SLuigi Rizzo 	NMA_LOCK(nmd);
88737e3a6d3SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
88837e3a6d3SLuigi Rizzo 		struct netmap_obj_pool *p = &nmd->pools[i];
88937e3a6d3SLuigi Rizzo 		int clsz = p->_clustsize;
89037e3a6d3SLuigi Rizzo 		int clobjs = p->_clustentries; /* objects per cluster */
89137e3a6d3SLuigi Rizzo 		int mdl_len = sizeof(PFN_NUMBER) * BYTES_TO_PAGES(clsz);
89237e3a6d3SLuigi Rizzo 		PPFN_NUMBER pSrc, pDst;
89337e3a6d3SLuigi Rizzo 
89437e3a6d3SLuigi Rizzo 		/* each pool has a different cluster size so we need to reallocate */
89537e3a6d3SLuigi Rizzo 		tempMdl = IoAllocateMdl(p->lut[0].vaddr, clsz, FALSE, FALSE, NULL);
89637e3a6d3SLuigi Rizzo 		if (tempMdl == NULL) {
89737e3a6d3SLuigi Rizzo 			NMA_UNLOCK(nmd);
898b6e66be2SVincenzo Maffione 			nm_prerr("fail to allocate tempMdl");
89937e3a6d3SLuigi Rizzo 			IoFreeMdl(mainMdl);
90037e3a6d3SLuigi Rizzo 			return NULL;
90137e3a6d3SLuigi Rizzo 		}
90237e3a6d3SLuigi Rizzo 		pSrc = MmGetMdlPfnArray(tempMdl);
90337e3a6d3SLuigi Rizzo 		/* create one entry per cluster, the lut[] has one entry per object */
90437e3a6d3SLuigi Rizzo 		for (j = 0; j < p->numclusters; j++, ofs += clsz) {
90537e3a6d3SLuigi Rizzo 			pDst = &MmGetMdlPfnArray(mainMdl)[BYTES_TO_PAGES(ofs)];
90637e3a6d3SLuigi Rizzo 			MmInitializeMdl(tempMdl, p->lut[j*clobjs].vaddr, clsz);
90737e3a6d3SLuigi Rizzo 			MmBuildMdlForNonPagedPool(tempMdl); /* compute physical page addresses */
90837e3a6d3SLuigi Rizzo 			RtlCopyMemory(pDst, pSrc, mdl_len); /* copy the page descriptors */
90937e3a6d3SLuigi Rizzo 			mainMdl->MdlFlags = tempMdl->MdlFlags; /* XXX what is in here ? */
91037e3a6d3SLuigi Rizzo 		}
91137e3a6d3SLuigi Rizzo 		IoFreeMdl(tempMdl);
91237e3a6d3SLuigi Rizzo 	}
91337e3a6d3SLuigi Rizzo 	NMA_UNLOCK(nmd);
91437e3a6d3SLuigi Rizzo 	return mainMdl;
91537e3a6d3SLuigi Rizzo }
91637e3a6d3SLuigi Rizzo 
91737e3a6d3SLuigi Rizzo #endif /* _WIN32 */
91837e3a6d3SLuigi Rizzo 
91937e3a6d3SLuigi Rizzo /*
92037e3a6d3SLuigi Rizzo  * helper function for OS-specific mmap routines (currently only windows).
92137e3a6d3SLuigi Rizzo  * Given an nmd and a pool index, returns the cluster size and number of clusters.
92237e3a6d3SLuigi Rizzo  * Returns 0 if memory is finalised and the pool is valid, otherwise 1.
92337e3a6d3SLuigi Rizzo  * It should be called under NMA_LOCK(nmd) otherwise the underlying info can change.
92437e3a6d3SLuigi Rizzo  */
92537e3a6d3SLuigi Rizzo 
92637e3a6d3SLuigi Rizzo int
92737e3a6d3SLuigi Rizzo netmap_mem2_get_pool_info(struct netmap_mem_d* nmd, u_int pool, u_int *clustsize, u_int *numclusters)
92837e3a6d3SLuigi Rizzo {
92937e3a6d3SLuigi Rizzo 	if (!nmd || !clustsize || !numclusters || pool >= NETMAP_POOLS_NR)
93037e3a6d3SLuigi Rizzo 		return 1; /* invalid arguments */
93137e3a6d3SLuigi Rizzo 	// NMA_LOCK_ASSERT(nmd);
93237e3a6d3SLuigi Rizzo 	if (!(nmd->flags & NETMAP_MEM_FINALIZED)) {
93337e3a6d3SLuigi Rizzo 		*clustsize = *numclusters = 0;
93437e3a6d3SLuigi Rizzo 		return 1; /* not ready yet */
93537e3a6d3SLuigi Rizzo 	}
93637e3a6d3SLuigi Rizzo 	*clustsize = nmd->pools[pool]._clustsize;
93737e3a6d3SLuigi Rizzo 	*numclusters = nmd->pools[pool].numclusters;
93837e3a6d3SLuigi Rizzo 	return 0; /* success */
939ccdc3305SLuigi Rizzo }
940ccdc3305SLuigi Rizzo 
941847bf383SLuigi Rizzo static int
9422ff91c17SVincenzo Maffione netmap_mem2_get_info(struct netmap_mem_d* nmd, uint64_t* size,
9432ff91c17SVincenzo Maffione 			u_int *memflags, nm_memid_t *id)
944ce3ee1e7SLuigi Rizzo {
945ce3ee1e7SLuigi Rizzo 	int error = 0;
946847bf383SLuigi Rizzo 	error = netmap_mem_config(nmd);
947ce3ee1e7SLuigi Rizzo 	if (error)
948ce3ee1e7SLuigi Rizzo 		goto out;
9494bf50f18SLuigi Rizzo 	if (size) {
950ce3ee1e7SLuigi Rizzo 		if (nmd->flags & NETMAP_MEM_FINALIZED) {
951ce3ee1e7SLuigi Rizzo 			*size = nmd->nm_totalsize;
952ce3ee1e7SLuigi Rizzo 		} else {
953ce3ee1e7SLuigi Rizzo 			int i;
954ce3ee1e7SLuigi Rizzo 			*size = 0;
955ce3ee1e7SLuigi Rizzo 			for (i = 0; i < NETMAP_POOLS_NR; i++) {
956ce3ee1e7SLuigi Rizzo 				struct netmap_obj_pool *p = nmd->pools + i;
957253b2ec1SVincenzo Maffione 				*size += ((size_t)p->_numclusters * (size_t)p->_clustsize);
958ce3ee1e7SLuigi Rizzo 			}
959ce3ee1e7SLuigi Rizzo 		}
9604bf50f18SLuigi Rizzo 	}
9614bf50f18SLuigi Rizzo 	if (memflags)
962ce3ee1e7SLuigi Rizzo 		*memflags = nmd->flags;
9634bf50f18SLuigi Rizzo 	if (id)
964f0ea3689SLuigi Rizzo 		*id = nmd->nm_id;
965ce3ee1e7SLuigi Rizzo out:
966ce3ee1e7SLuigi Rizzo 	return error;
967ce3ee1e7SLuigi Rizzo }
968ce3ee1e7SLuigi Rizzo 
969ccdc3305SLuigi Rizzo /*
970ccdc3305SLuigi Rizzo  * we store objects by kernel address, need to find the offset
971ccdc3305SLuigi Rizzo  * within the pool to export the value to userspace.
972ccdc3305SLuigi Rizzo  * Algorithm: scan until we find the cluster, then add the
973ccdc3305SLuigi Rizzo  * actual offset in the cluster
974ccdc3305SLuigi Rizzo  */
975ce2cb792SLuigi Rizzo static ssize_t
976ccdc3305SLuigi Rizzo netmap_obj_offset(struct netmap_obj_pool *p, const void *vaddr)
977ccdc3305SLuigi Rizzo {
978ce3ee1e7SLuigi Rizzo 	int i, k = p->_clustentries, n = p->objtotal;
979ccdc3305SLuigi Rizzo 	ssize_t ofs = 0;
980ccdc3305SLuigi Rizzo 
981ccdc3305SLuigi Rizzo 	for (i = 0; i < n; i += k, ofs += p->_clustsize) {
982ccdc3305SLuigi Rizzo 		const char *base = p->lut[i].vaddr;
983ccdc3305SLuigi Rizzo 		ssize_t relofs = (const char *) vaddr - base;
984ccdc3305SLuigi Rizzo 
985aa76317cSLuigi Rizzo 		if (relofs < 0 || relofs >= p->_clustsize)
986ccdc3305SLuigi Rizzo 			continue;
987ccdc3305SLuigi Rizzo 
988ccdc3305SLuigi Rizzo 		ofs = ofs + relofs;
98975f4f3edSVincenzo Maffione 		nm_prdis("%s: return offset %d (cluster %d) for pointer %p",
990ccdc3305SLuigi Rizzo 		    p->name, ofs, i, vaddr);
991ccdc3305SLuigi Rizzo 		return ofs;
992ccdc3305SLuigi Rizzo 	}
993b6e66be2SVincenzo Maffione 	nm_prerr("address %p is not contained inside any cluster (%s)",
994ccdc3305SLuigi Rizzo 	    vaddr, p->name);
995ccdc3305SLuigi Rizzo 	return 0; /* An error occurred */
996ccdc3305SLuigi Rizzo }
997ccdc3305SLuigi Rizzo 
998ccdc3305SLuigi Rizzo /* Helper functions which convert virtual addresses to offsets */
999ce3ee1e7SLuigi Rizzo #define netmap_if_offset(n, v)					\
1000ce3ee1e7SLuigi Rizzo 	netmap_obj_offset(&(n)->pools[NETMAP_IF_POOL], (v))
1001ccdc3305SLuigi Rizzo 
1002ce3ee1e7SLuigi Rizzo #define netmap_ring_offset(n, v)				\
1003ce3ee1e7SLuigi Rizzo     ((n)->pools[NETMAP_IF_POOL].memtotal + 			\
1004ce3ee1e7SLuigi Rizzo 	netmap_obj_offset(&(n)->pools[NETMAP_RING_POOL], (v)))
1005ccdc3305SLuigi Rizzo 
1006847bf383SLuigi Rizzo static ssize_t
1007847bf383SLuigi Rizzo netmap_mem2_if_offset(struct netmap_mem_d *nmd, const void *addr)
1008ce3ee1e7SLuigi Rizzo {
10092ff91c17SVincenzo Maffione 	return netmap_if_offset(nmd, addr);
1010ce3ee1e7SLuigi Rizzo }
1011ce3ee1e7SLuigi Rizzo 
10128241616dSLuigi Rizzo /*
10138241616dSLuigi Rizzo  * report the index, and use start position as a hint,
10148241616dSLuigi Rizzo  * otherwise buffer allocation becomes terribly expensive.
10158241616dSLuigi Rizzo  */
1016ccdc3305SLuigi Rizzo static void *
1017ce3ee1e7SLuigi Rizzo netmap_obj_malloc(struct netmap_obj_pool *p, u_int len, uint32_t *start, uint32_t *index)
1018ccdc3305SLuigi Rizzo {
1019ccdc3305SLuigi Rizzo 	uint32_t i = 0;			/* index in the bitmap */
102037e3a6d3SLuigi Rizzo 	uint32_t mask, j = 0;		/* slot counter */
1021ccdc3305SLuigi Rizzo 	void *vaddr = NULL;
1022ccdc3305SLuigi Rizzo 
1023ccdc3305SLuigi Rizzo 	if (len > p->_objsize) {
1024b6e66be2SVincenzo Maffione 		nm_prerr("%s request size %d too large", p->name, len);
1025ccdc3305SLuigi Rizzo 		return NULL;
1026ccdc3305SLuigi Rizzo 	}
1027ccdc3305SLuigi Rizzo 
1028ccdc3305SLuigi Rizzo 	if (p->objfree == 0) {
1029b6e66be2SVincenzo Maffione 		nm_prerr("no more %s objects", p->name);
1030ccdc3305SLuigi Rizzo 		return NULL;
1031ccdc3305SLuigi Rizzo 	}
10328241616dSLuigi Rizzo 	if (start)
10338241616dSLuigi Rizzo 		i = *start;
1034ccdc3305SLuigi Rizzo 
10358241616dSLuigi Rizzo 	/* termination is guaranteed by p->free, but better check bounds on i */
10368241616dSLuigi Rizzo 	while (vaddr == NULL && i < p->bitmap_slots)  {
1037ccdc3305SLuigi Rizzo 		uint32_t cur = p->bitmap[i];
1038ccdc3305SLuigi Rizzo 		if (cur == 0) { /* bitmask is fully used */
1039ccdc3305SLuigi Rizzo 			i++;
1040ccdc3305SLuigi Rizzo 			continue;
1041ccdc3305SLuigi Rizzo 		}
1042ccdc3305SLuigi Rizzo 		/* locate a slot */
1043ccdc3305SLuigi Rizzo 		for (j = 0, mask = 1; (cur & mask) == 0; j++, mask <<= 1)
1044ccdc3305SLuigi Rizzo 			;
1045ccdc3305SLuigi Rizzo 
1046ccdc3305SLuigi Rizzo 		p->bitmap[i] &= ~mask; /* mark object as in use */
1047ccdc3305SLuigi Rizzo 		p->objfree--;
1048ccdc3305SLuigi Rizzo 
1049ccdc3305SLuigi Rizzo 		vaddr = p->lut[i * 32 + j].vaddr;
10508241616dSLuigi Rizzo 		if (index)
10518241616dSLuigi Rizzo 			*index = i * 32 + j;
1052ccdc3305SLuigi Rizzo 	}
105375f4f3edSVincenzo Maffione 	nm_prdis("%s allocator: allocated object @ [%d][%d]: vaddr %p",p->name, i, j, vaddr);
1054ccdc3305SLuigi Rizzo 
10558241616dSLuigi Rizzo 	if (start)
10568241616dSLuigi Rizzo 		*start = i;
1057ccdc3305SLuigi Rizzo 	return vaddr;
1058ccdc3305SLuigi Rizzo }
1059ccdc3305SLuigi Rizzo 
1060ccdc3305SLuigi Rizzo 
1061ccdc3305SLuigi Rizzo /*
1062f0ea3689SLuigi Rizzo  * free by index, not by address.
1063f0ea3689SLuigi Rizzo  * XXX should we also cleanup the content ?
1064ccdc3305SLuigi Rizzo  */
1065f0ea3689SLuigi Rizzo static int
1066ccdc3305SLuigi Rizzo netmap_obj_free(struct netmap_obj_pool *p, uint32_t j)
1067ccdc3305SLuigi Rizzo {
1068f0ea3689SLuigi Rizzo 	uint32_t *ptr, mask;
1069f0ea3689SLuigi Rizzo 
1070ccdc3305SLuigi Rizzo 	if (j >= p->objtotal) {
1071b6e66be2SVincenzo Maffione 		nm_prerr("invalid index %u, max %u", j, p->objtotal);
1072f0ea3689SLuigi Rizzo 		return 1;
1073ccdc3305SLuigi Rizzo 	}
1074f0ea3689SLuigi Rizzo 	ptr = &p->bitmap[j / 32];
1075f0ea3689SLuigi Rizzo 	mask = (1 << (j % 32));
1076f0ea3689SLuigi Rizzo 	if (*ptr & mask) {
1077b6e66be2SVincenzo Maffione 		nm_prerr("ouch, double free on buffer %d", j);
1078f0ea3689SLuigi Rizzo 		return 1;
1079f0ea3689SLuigi Rizzo 	} else {
1080f0ea3689SLuigi Rizzo 		*ptr |= mask;
1081ccdc3305SLuigi Rizzo 		p->objfree++;
1082f0ea3689SLuigi Rizzo 		return 0;
1083f0ea3689SLuigi Rizzo 	}
1084ccdc3305SLuigi Rizzo }
1085ccdc3305SLuigi Rizzo 
1086f0ea3689SLuigi Rizzo /*
1087f0ea3689SLuigi Rizzo  * free by address. This is slow but is only used for a few
1088f0ea3689SLuigi Rizzo  * objects (rings, nifp)
1089f0ea3689SLuigi Rizzo  */
1090ccdc3305SLuigi Rizzo static void
1091ccdc3305SLuigi Rizzo netmap_obj_free_va(struct netmap_obj_pool *p, void *vaddr)
1092ccdc3305SLuigi Rizzo {
1093ce3ee1e7SLuigi Rizzo 	u_int i, j, n = p->numclusters;
1094ccdc3305SLuigi Rizzo 
1095ce3ee1e7SLuigi Rizzo 	for (i = 0, j = 0; i < n; i++, j += p->_clustentries) {
1096ce3ee1e7SLuigi Rizzo 		void *base = p->lut[i * p->_clustentries].vaddr;
1097ccdc3305SLuigi Rizzo 		ssize_t relofs = (ssize_t) vaddr - (ssize_t) base;
1098ccdc3305SLuigi Rizzo 
1099ccdc3305SLuigi Rizzo 		/* Given address, is out of the scope of the current cluster.*/
11004f80b14cSVincenzo Maffione 		if (base == NULL || vaddr < base || relofs >= p->_clustsize)
1101ccdc3305SLuigi Rizzo 			continue;
1102ccdc3305SLuigi Rizzo 
1103ccdc3305SLuigi Rizzo 		j = j + relofs / p->_objsize;
1104ce3ee1e7SLuigi Rizzo 		/* KASSERT(j != 0, ("Cannot free object 0")); */
1105ccdc3305SLuigi Rizzo 		netmap_obj_free(p, j);
1106ccdc3305SLuigi Rizzo 		return;
1107ccdc3305SLuigi Rizzo 	}
1108b6e66be2SVincenzo Maffione 	nm_prerr("address %p is not contained inside any cluster (%s)",
1109ccdc3305SLuigi Rizzo 	    vaddr, p->name);
1110ccdc3305SLuigi Rizzo }
1111ccdc3305SLuigi Rizzo 
11124f80b14cSVincenzo Maffione unsigned
11134f80b14cSVincenzo Maffione netmap_mem_bufsize(struct netmap_mem_d *nmd)
11144f80b14cSVincenzo Maffione {
11154f80b14cSVincenzo Maffione 	return nmd->pools[NETMAP_BUF_POOL]._objsize;
11164f80b14cSVincenzo Maffione }
11174bf50f18SLuigi Rizzo 
1118ce3ee1e7SLuigi Rizzo #define netmap_if_malloc(n, len)	netmap_obj_malloc(&(n)->pools[NETMAP_IF_POOL], len, NULL, NULL)
1119ce3ee1e7SLuigi Rizzo #define netmap_if_free(n, v)		netmap_obj_free_va(&(n)->pools[NETMAP_IF_POOL], (v))
1120ce3ee1e7SLuigi Rizzo #define netmap_ring_malloc(n, len)	netmap_obj_malloc(&(n)->pools[NETMAP_RING_POOL], len, NULL, NULL)
1121ce3ee1e7SLuigi Rizzo #define netmap_ring_free(n, v)		netmap_obj_free_va(&(n)->pools[NETMAP_RING_POOL], (v))
1122ce3ee1e7SLuigi Rizzo #define netmap_buf_malloc(n, _pos, _index)			\
11234bf50f18SLuigi Rizzo 	netmap_obj_malloc(&(n)->pools[NETMAP_BUF_POOL], netmap_mem_bufsize(n), _pos, _index)
1124ccdc3305SLuigi Rizzo 
1125ccdc3305SLuigi Rizzo 
11264f80b14cSVincenzo Maffione #if 0 /* currently unused */
1127ccdc3305SLuigi Rizzo /* Return the index associated to the given packet buffer */
1128ce3ee1e7SLuigi Rizzo #define netmap_buf_index(n, v)						\
1129ce3ee1e7SLuigi Rizzo     (netmap_obj_offset(&(n)->pools[NETMAP_BUF_POOL], (v)) / NETMAP_BDG_BUF_SIZE(n))
1130f0ea3689SLuigi Rizzo #endif
1131f0ea3689SLuigi Rizzo 
1132f0ea3689SLuigi Rizzo /*
1133f0ea3689SLuigi Rizzo  * allocate extra buffers in a linked list.
1134f0ea3689SLuigi Rizzo  * returns the actual number.
1135f0ea3689SLuigi Rizzo  */
1136f0ea3689SLuigi Rizzo uint32_t
1137f0ea3689SLuigi Rizzo netmap_extra_alloc(struct netmap_adapter *na, uint32_t *head, uint32_t n)
1138f0ea3689SLuigi Rizzo {
1139f0ea3689SLuigi Rizzo 	struct netmap_mem_d *nmd = na->nm_mem;
1140f0ea3689SLuigi Rizzo 	uint32_t i, pos = 0; /* opaque, scan position in the bitmap */
1141f0ea3689SLuigi Rizzo 
1142f0ea3689SLuigi Rizzo 	NMA_LOCK(nmd);
1143f0ea3689SLuigi Rizzo 
1144f0ea3689SLuigi Rizzo 	*head = 0;	/* default, 'null' index ie empty list */
1145f0ea3689SLuigi Rizzo 	for (i = 0 ; i < n; i++) {
1146f0ea3689SLuigi Rizzo 		uint32_t cur = *head;	/* save current head */
1147f0ea3689SLuigi Rizzo 		uint32_t *p = netmap_buf_malloc(nmd, &pos, head);
1148f0ea3689SLuigi Rizzo 		if (p == NULL) {
1149b6e66be2SVincenzo Maffione 			nm_prerr("no more buffers after %d of %d", i, n);
1150f0ea3689SLuigi Rizzo 			*head = cur; /* restore */
1151f0ea3689SLuigi Rizzo 			break;
1152f0ea3689SLuigi Rizzo 		}
115375f4f3edSVincenzo Maffione 		nm_prdis(5, "allocate buffer %d -> %d", *head, cur);
1154f0ea3689SLuigi Rizzo 		*p = cur; /* link to previous head */
1155f0ea3689SLuigi Rizzo 	}
1156f0ea3689SLuigi Rizzo 
1157f0ea3689SLuigi Rizzo 	NMA_UNLOCK(nmd);
1158f0ea3689SLuigi Rizzo 
1159f0ea3689SLuigi Rizzo 	return i;
1160f0ea3689SLuigi Rizzo }
1161f0ea3689SLuigi Rizzo 
1162f0ea3689SLuigi Rizzo static void
1163f0ea3689SLuigi Rizzo netmap_extra_free(struct netmap_adapter *na, uint32_t head)
1164f0ea3689SLuigi Rizzo {
1165847bf383SLuigi Rizzo 	struct lut_entry *lut = na->na_lut.lut;
1166f0ea3689SLuigi Rizzo 	struct netmap_mem_d *nmd = na->nm_mem;
1167f0ea3689SLuigi Rizzo 	struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
1168f0ea3689SLuigi Rizzo 	uint32_t i, cur, *buf;
1169f0ea3689SLuigi Rizzo 
117075f4f3edSVincenzo Maffione 	nm_prdis("freeing the extra list");
1171f0ea3689SLuigi Rizzo 	for (i = 0; head >=2 && head < p->objtotal; i++) {
1172f0ea3689SLuigi Rizzo 		cur = head;
1173f0ea3689SLuigi Rizzo 		buf = lut[head].vaddr;
1174f0ea3689SLuigi Rizzo 		head = *buf;
1175f0ea3689SLuigi Rizzo 		*buf = 0;
1176f0ea3689SLuigi Rizzo 		if (netmap_obj_free(p, cur))
1177f0ea3689SLuigi Rizzo 			break;
1178f0ea3689SLuigi Rizzo 	}
1179f0ea3689SLuigi Rizzo 	if (head != 0)
1180b6e66be2SVincenzo Maffione 		nm_prerr("breaking with head %d", head);
1181b6e66be2SVincenzo Maffione 	if (netmap_debug & NM_DEBUG_MEM)
1182b6e66be2SVincenzo Maffione 		nm_prinf("freed %d buffers", i);
1183f0ea3689SLuigi Rizzo }
1184ccdc3305SLuigi Rizzo 
1185ccdc3305SLuigi Rizzo 
11868241616dSLuigi Rizzo /* Return nonzero on error */
11878241616dSLuigi Rizzo static int
1188f9790aebSLuigi Rizzo netmap_new_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n)
1189ccdc3305SLuigi Rizzo {
1190ce3ee1e7SLuigi Rizzo 	struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
1191ce3ee1e7SLuigi Rizzo 	u_int i = 0;	/* slot counter */
11928241616dSLuigi Rizzo 	uint32_t pos = 0;	/* slot in p->bitmap */
11938241616dSLuigi Rizzo 	uint32_t index = 0;	/* buffer index */
1194ccdc3305SLuigi Rizzo 
1195ccdc3305SLuigi Rizzo 	for (i = 0; i < n; i++) {
1196ce3ee1e7SLuigi Rizzo 		void *vaddr = netmap_buf_malloc(nmd, &pos, &index);
1197ccdc3305SLuigi Rizzo 		if (vaddr == NULL) {
1198b6e66be2SVincenzo Maffione 			nm_prerr("no more buffers after %d of %d", i, n);
1199ccdc3305SLuigi Rizzo 			goto cleanup;
1200ccdc3305SLuigi Rizzo 		}
12018241616dSLuigi Rizzo 		slot[i].buf_idx = index;
1202ccdc3305SLuigi Rizzo 		slot[i].len = p->_objsize;
1203f9790aebSLuigi Rizzo 		slot[i].flags = 0;
12044f80b14cSVincenzo Maffione 		slot[i].ptr = 0;
1205ccdc3305SLuigi Rizzo 	}
1206ccdc3305SLuigi Rizzo 
120775f4f3edSVincenzo Maffione 	nm_prdis("%s: allocated %d buffers, %d available, first at %d", p->name, n, p->objfree, pos);
12088241616dSLuigi Rizzo 	return (0);
1209ccdc3305SLuigi Rizzo 
1210ccdc3305SLuigi Rizzo cleanup:
12114cf8455fSEd Maste 	while (i > 0) {
12124cf8455fSEd Maste 		i--;
12138241616dSLuigi Rizzo 		netmap_obj_free(p, slot[i].buf_idx);
1214ccdc3305SLuigi Rizzo 	}
12158241616dSLuigi Rizzo 	bzero(slot, n * sizeof(slot[0]));
12168241616dSLuigi Rizzo 	return (ENOMEM);
1217ccdc3305SLuigi Rizzo }
1218ccdc3305SLuigi Rizzo 
1219f0ea3689SLuigi Rizzo static void
1220f0ea3689SLuigi Rizzo netmap_mem_set_ring(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n, uint32_t index)
1221f0ea3689SLuigi Rizzo {
1222f0ea3689SLuigi Rizzo 	struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
1223f0ea3689SLuigi Rizzo 	u_int i;
1224f0ea3689SLuigi Rizzo 
1225f0ea3689SLuigi Rizzo 	for (i = 0; i < n; i++) {
1226f0ea3689SLuigi Rizzo 		slot[i].buf_idx = index;
1227f0ea3689SLuigi Rizzo 		slot[i].len = p->_objsize;
1228f0ea3689SLuigi Rizzo 		slot[i].flags = 0;
1229f0ea3689SLuigi Rizzo 	}
1230f0ea3689SLuigi Rizzo }
1231f0ea3689SLuigi Rizzo 
1232ccdc3305SLuigi Rizzo 
1233ccdc3305SLuigi Rizzo static void
1234f9790aebSLuigi Rizzo netmap_free_buf(struct netmap_mem_d *nmd, uint32_t i)
1235ccdc3305SLuigi Rizzo {
1236ce3ee1e7SLuigi Rizzo 	struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
12378241616dSLuigi Rizzo 
1238ccdc3305SLuigi Rizzo 	if (i < 2 || i >= p->objtotal) {
1239b6e66be2SVincenzo Maffione 		nm_prerr("Cannot free buf#%d: should be in [2, %d[", i, p->objtotal);
1240ccdc3305SLuigi Rizzo 		return;
1241ccdc3305SLuigi Rizzo 	}
12428241616dSLuigi Rizzo 	netmap_obj_free(p, i);
1243ccdc3305SLuigi Rizzo }
1244ccdc3305SLuigi Rizzo 
1245f0ea3689SLuigi Rizzo 
1246f0ea3689SLuigi Rizzo static void
1247f0ea3689SLuigi Rizzo netmap_free_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n)
1248f0ea3689SLuigi Rizzo {
1249f0ea3689SLuigi Rizzo 	u_int i;
1250f0ea3689SLuigi Rizzo 
1251f0ea3689SLuigi Rizzo 	for (i = 0; i < n; i++) {
12522ff91c17SVincenzo Maffione 		if (slot[i].buf_idx > 1)
1253f0ea3689SLuigi Rizzo 			netmap_free_buf(nmd, slot[i].buf_idx);
1254f0ea3689SLuigi Rizzo 	}
125575f4f3edSVincenzo Maffione 	nm_prdis("%s: released some buffers, available: %u",
12562ff91c17SVincenzo Maffione 			p->name, p->objfree);
1257f0ea3689SLuigi Rizzo }
1258f0ea3689SLuigi Rizzo 
12598241616dSLuigi Rizzo static void
12608241616dSLuigi Rizzo netmap_reset_obj_allocator(struct netmap_obj_pool *p)
12618241616dSLuigi Rizzo {
1262ce3ee1e7SLuigi Rizzo 
12638241616dSLuigi Rizzo 	if (p == NULL)
12648241616dSLuigi Rizzo 		return;
12658241616dSLuigi Rizzo 	if (p->bitmap)
1266c3e9b4dbSLuiz Otavio O Souza 		nm_os_free(p->bitmap);
12678241616dSLuigi Rizzo 	p->bitmap = NULL;
12684f80b14cSVincenzo Maffione 	if (p->invalid_bitmap)
12694f80b14cSVincenzo Maffione 		nm_os_free(p->invalid_bitmap);
12704f80b14cSVincenzo Maffione 	p->invalid_bitmap = NULL;
12712ff91c17SVincenzo Maffione 	if (!p->alloc_done) {
12722ff91c17SVincenzo Maffione 		/* allocation was done by somebody else.
12732ff91c17SVincenzo Maffione 		 * Let them clean up after themselves.
12742ff91c17SVincenzo Maffione 		 */
12752ff91c17SVincenzo Maffione 		return;
12762ff91c17SVincenzo Maffione 	}
12778241616dSLuigi Rizzo 	if (p->lut) {
1278ce3ee1e7SLuigi Rizzo 		u_int i;
1279ce3ee1e7SLuigi Rizzo 
1280dd4fcbc5SPatrick Kelsey 		/*
1281dd4fcbc5SPatrick Kelsey 		 * Free each cluster allocated in
1282dd4fcbc5SPatrick Kelsey 		 * netmap_finalize_obj_allocator().  The cluster start
1283dd4fcbc5SPatrick Kelsey 		 * addresses are stored at multiples of p->_clusterentries
1284dd4fcbc5SPatrick Kelsey 		 * in the lut.
1285dd4fcbc5SPatrick Kelsey 		 */
1286ce3ee1e7SLuigi Rizzo 		for (i = 0; i < p->objtotal; i += p->_clustentries) {
128737e3a6d3SLuigi Rizzo 			contigfree(p->lut[i].vaddr, p->_clustsize, M_NETMAP);
12888241616dSLuigi Rizzo 		}
12894f80b14cSVincenzo Maffione 		nm_free_lut(p->lut, p->objtotal);
12908241616dSLuigi Rizzo 	}
12918241616dSLuigi Rizzo 	p->lut = NULL;
1292ce3ee1e7SLuigi Rizzo 	p->objtotal = 0;
1293ce3ee1e7SLuigi Rizzo 	p->memtotal = 0;
1294ce3ee1e7SLuigi Rizzo 	p->numclusters = 0;
1295ce3ee1e7SLuigi Rizzo 	p->objfree = 0;
12962ff91c17SVincenzo Maffione 	p->alloc_done = 0;
12978241616dSLuigi Rizzo }
1298ccdc3305SLuigi Rizzo 
1299ccdc3305SLuigi Rizzo /*
1300ccdc3305SLuigi Rizzo  * Free all resources related to an allocator.
1301ccdc3305SLuigi Rizzo  */
1302ccdc3305SLuigi Rizzo static void
1303ccdc3305SLuigi Rizzo netmap_destroy_obj_allocator(struct netmap_obj_pool *p)
1304ccdc3305SLuigi Rizzo {
1305ccdc3305SLuigi Rizzo 	if (p == NULL)
1306ccdc3305SLuigi Rizzo 		return;
13078241616dSLuigi Rizzo 	netmap_reset_obj_allocator(p);
1308ccdc3305SLuigi Rizzo }
1309ccdc3305SLuigi Rizzo 
1310ccdc3305SLuigi Rizzo /*
1311ccdc3305SLuigi Rizzo  * We receive a request for objtotal objects, of size objsize each.
1312ccdc3305SLuigi Rizzo  * Internally we may round up both numbers, as we allocate objects
1313ccdc3305SLuigi Rizzo  * in small clusters multiple of the page size.
1314ce3ee1e7SLuigi Rizzo  * We need to keep track of objtotal and clustentries,
1315ccdc3305SLuigi Rizzo  * as they are needed when freeing memory.
1316ccdc3305SLuigi Rizzo  *
1317ccdc3305SLuigi Rizzo  * XXX note -- userspace needs the buffers to be contiguous,
1318ccdc3305SLuigi Rizzo  *	so we cannot afford gaps at the end of a cluster.
1319ccdc3305SLuigi Rizzo  */
13208241616dSLuigi Rizzo 
13218241616dSLuigi Rizzo 
13228241616dSLuigi Rizzo /* call with NMA_LOCK held */
13238241616dSLuigi Rizzo static int
13248241616dSLuigi Rizzo netmap_config_obj_allocator(struct netmap_obj_pool *p, u_int objtotal, u_int objsize)
1325ccdc3305SLuigi Rizzo {
1326ce3ee1e7SLuigi Rizzo 	int i;
1327ccdc3305SLuigi Rizzo 	u_int clustsize;	/* the cluster size, multiple of page size */
1328ccdc3305SLuigi Rizzo 	u_int clustentries;	/* how many objects per entry */
1329ccdc3305SLuigi Rizzo 
1330ce3ee1e7SLuigi Rizzo 	/* we store the current request, so we can
1331ce3ee1e7SLuigi Rizzo 	 * detect configuration changes later */
1332ce3ee1e7SLuigi Rizzo 	p->r_objtotal = objtotal;
1333ce3ee1e7SLuigi Rizzo 	p->r_objsize = objsize;
1334ce3ee1e7SLuigi Rizzo 
13354bf50f18SLuigi Rizzo #define MAX_CLUSTSIZE	(1<<22)		// 4 MB
1336a6d768d8SVincenzo Maffione #define LINE_ROUND	NM_BUF_ALIGN	// 64
1337ccdc3305SLuigi Rizzo 	if (objsize >= MAX_CLUSTSIZE) {
1338ccdc3305SLuigi Rizzo 		/* we could do it but there is no point */
1339b6e66be2SVincenzo Maffione 		nm_prerr("unsupported allocation for %d bytes", objsize);
1340ce3ee1e7SLuigi Rizzo 		return EINVAL;
1341ccdc3305SLuigi Rizzo 	}
1342ccdc3305SLuigi Rizzo 	/* make sure objsize is a multiple of LINE_ROUND */
1343ccdc3305SLuigi Rizzo 	i = (objsize & (LINE_ROUND - 1));
1344ccdc3305SLuigi Rizzo 	if (i) {
1345b6e66be2SVincenzo Maffione 		nm_prinf("aligning object by %d bytes", LINE_ROUND - i);
1346ccdc3305SLuigi Rizzo 		objsize += LINE_ROUND - i;
1347ccdc3305SLuigi Rizzo 	}
13488241616dSLuigi Rizzo 	if (objsize < p->objminsize || objsize > p->objmaxsize) {
1349b6e66be2SVincenzo Maffione 		nm_prerr("requested objsize %d out of range [%d, %d]",
13508241616dSLuigi Rizzo 			objsize, p->objminsize, p->objmaxsize);
1351ce3ee1e7SLuigi Rizzo 		return EINVAL;
13528241616dSLuigi Rizzo 	}
13538241616dSLuigi Rizzo 	if (objtotal < p->nummin || objtotal > p->nummax) {
1354b6e66be2SVincenzo Maffione 		nm_prerr("requested objtotal %d out of range [%d, %d]",
13558241616dSLuigi Rizzo 			objtotal, p->nummin, p->nummax);
1356ce3ee1e7SLuigi Rizzo 		return EINVAL;
13578241616dSLuigi Rizzo 	}
1358ccdc3305SLuigi Rizzo 	/*
1359ccdc3305SLuigi Rizzo 	 * Compute number of objects using a brute-force approach:
1360ccdc3305SLuigi Rizzo 	 * given a max cluster size,
1361ccdc3305SLuigi Rizzo 	 * we try to fill it with objects keeping track of the
1362ccdc3305SLuigi Rizzo 	 * wasted space to the next page boundary.
1363ccdc3305SLuigi Rizzo 	 */
1364ccdc3305SLuigi Rizzo 	for (clustentries = 0, i = 1;; i++) {
1365ccdc3305SLuigi Rizzo 		u_int delta, used = i * objsize;
1366ccdc3305SLuigi Rizzo 		if (used > MAX_CLUSTSIZE)
1367ccdc3305SLuigi Rizzo 			break;
1368ccdc3305SLuigi Rizzo 		delta = used % PAGE_SIZE;
1369ccdc3305SLuigi Rizzo 		if (delta == 0) { // exact solution
1370ccdc3305SLuigi Rizzo 			clustentries = i;
1371ccdc3305SLuigi Rizzo 			break;
1372ccdc3305SLuigi Rizzo 		}
1373ccdc3305SLuigi Rizzo 	}
13744bf50f18SLuigi Rizzo 	/* exact solution not found */
13754bf50f18SLuigi Rizzo 	if (clustentries == 0) {
1376b6e66be2SVincenzo Maffione 		nm_prerr("unsupported allocation for %d bytes", objsize);
13774bf50f18SLuigi Rizzo 		return EINVAL;
13784bf50f18SLuigi Rizzo 	}
13794bf50f18SLuigi Rizzo 	/* compute clustsize */
1380ccdc3305SLuigi Rizzo 	clustsize = clustentries * objsize;
1381b6e66be2SVincenzo Maffione 	if (netmap_debug & NM_DEBUG_MEM)
1382b6e66be2SVincenzo Maffione 		nm_prinf("objsize %d clustsize %d objects %d",
1383ccdc3305SLuigi Rizzo 			objsize, clustsize, clustentries);
1384ccdc3305SLuigi Rizzo 
1385ccdc3305SLuigi Rizzo 	/*
1386ccdc3305SLuigi Rizzo 	 * The number of clusters is n = ceil(objtotal/clustentries)
1387ccdc3305SLuigi Rizzo 	 * objtotal' = n * clustentries
1388ccdc3305SLuigi Rizzo 	 */
1389ce3ee1e7SLuigi Rizzo 	p->_clustentries = clustentries;
1390ccdc3305SLuigi Rizzo 	p->_clustsize = clustsize;
1391ce3ee1e7SLuigi Rizzo 	p->_numclusters = (objtotal + clustentries - 1) / clustentries;
1392ce3ee1e7SLuigi Rizzo 
1393ce3ee1e7SLuigi Rizzo 	/* actual values (may be larger than requested) */
13948241616dSLuigi Rizzo 	p->_objsize = objsize;
1395ce3ee1e7SLuigi Rizzo 	p->_objtotal = p->_numclusters * clustentries;
1396ccdc3305SLuigi Rizzo 
13978241616dSLuigi Rizzo 	return 0;
13988241616dSLuigi Rizzo }
13998241616dSLuigi Rizzo 
14008241616dSLuigi Rizzo /* call with NMA_LOCK held */
14018241616dSLuigi Rizzo static int
14028241616dSLuigi Rizzo netmap_finalize_obj_allocator(struct netmap_obj_pool *p)
14038241616dSLuigi Rizzo {
1404ce3ee1e7SLuigi Rizzo 	int i; /* must be signed */
1405ce3ee1e7SLuigi Rizzo 	size_t n;
1406ce3ee1e7SLuigi Rizzo 
14074f80b14cSVincenzo Maffione 	if (p->lut) {
14082ff91c17SVincenzo Maffione 		/* if the lut is already there we assume that also all the
140945c67e8fSVincenzo Maffione 		 * clusters have already been allocated, possibly by somebody
14102ff91c17SVincenzo Maffione 		 * else (e.g., extmem). In the latter case, the alloc_done flag
14112ff91c17SVincenzo Maffione 		 * will remain at zero, so that we will not attempt to
14122ff91c17SVincenzo Maffione 		 * deallocate the clusters by ourselves in
14132ff91c17SVincenzo Maffione 		 * netmap_reset_obj_allocator.
14142ff91c17SVincenzo Maffione 		 */
14154f80b14cSVincenzo Maffione 		return 0;
14164f80b14cSVincenzo Maffione 	}
14174f80b14cSVincenzo Maffione 
1418ce3ee1e7SLuigi Rizzo 	/* optimistically assume we have enough memory */
1419ce3ee1e7SLuigi Rizzo 	p->numclusters = p->_numclusters;
1420ce3ee1e7SLuigi Rizzo 	p->objtotal = p->_objtotal;
14212ff91c17SVincenzo Maffione 	p->alloc_done = 1;
14228241616dSLuigi Rizzo 
142337e3a6d3SLuigi Rizzo 	p->lut = nm_alloc_lut(p->objtotal);
1424ccdc3305SLuigi Rizzo 	if (p->lut == NULL) {
1425b6e66be2SVincenzo Maffione 		nm_prerr("Unable to create lookup table for '%s'", p->name);
1426ccdc3305SLuigi Rizzo 		goto clean;
1427ccdc3305SLuigi Rizzo 	}
1428ccdc3305SLuigi Rizzo 
1429ccdc3305SLuigi Rizzo 	/*
14304f80b14cSVincenzo Maffione 	 * Allocate clusters, init pointers
1431ccdc3305SLuigi Rizzo 	 */
1432ce3ee1e7SLuigi Rizzo 
1433ce3ee1e7SLuigi Rizzo 	n = p->_clustsize;
1434ce3ee1e7SLuigi Rizzo 	for (i = 0; i < (int)p->objtotal;) {
1435ce3ee1e7SLuigi Rizzo 		int lim = i + p->_clustentries;
1436ccdc3305SLuigi Rizzo 		char *clust;
1437ccdc3305SLuigi Rizzo 
143837e3a6d3SLuigi Rizzo 		/*
143937e3a6d3SLuigi Rizzo 		 * XXX Note, we only need contigmalloc() for buffers attached
144037e3a6d3SLuigi Rizzo 		 * to native interfaces. In all other cases (nifp, netmap rings
144137e3a6d3SLuigi Rizzo 		 * and even buffers for VALE ports or emulated interfaces) we
144237e3a6d3SLuigi Rizzo 		 * can live with standard malloc, because the hardware will not
144337e3a6d3SLuigi Rizzo 		 * access the pages directly.
144437e3a6d3SLuigi Rizzo 		 */
1445ce3ee1e7SLuigi Rizzo 		clust = contigmalloc(n, M_NETMAP, M_NOWAIT | M_ZERO,
1446ce3ee1e7SLuigi Rizzo 		    (size_t)0, -1UL, PAGE_SIZE, 0);
1447ccdc3305SLuigi Rizzo 		if (clust == NULL) {
1448ccdc3305SLuigi Rizzo 			/*
1449ccdc3305SLuigi Rizzo 			 * If we get here, there is a severe memory shortage,
1450ccdc3305SLuigi Rizzo 			 * so halve the allocated memory to reclaim some.
1451ccdc3305SLuigi Rizzo 			 */
1452b6e66be2SVincenzo Maffione 			nm_prerr("Unable to create cluster at %d for '%s' allocator",
14538241616dSLuigi Rizzo 			    i, p->name);
1454ce3ee1e7SLuigi Rizzo 			if (i < 2) /* nothing to halve */
1455ce3ee1e7SLuigi Rizzo 				goto out;
1456ccdc3305SLuigi Rizzo 			lim = i / 2;
14578241616dSLuigi Rizzo 			for (i--; i >= lim; i--) {
1458ce3ee1e7SLuigi Rizzo 				if (i % p->_clustentries == 0 && p->lut[i].vaddr)
1459ccdc3305SLuigi Rizzo 					contigfree(p->lut[i].vaddr,
1460ce3ee1e7SLuigi Rizzo 						n, M_NETMAP);
1461dd4fcbc5SPatrick Kelsey 				p->lut[i].vaddr = NULL;
1462ccdc3305SLuigi Rizzo 			}
1463ce3ee1e7SLuigi Rizzo 		out:
1464ccdc3305SLuigi Rizzo 			p->objtotal = i;
1465ce3ee1e7SLuigi Rizzo 			/* we may have stopped in the middle of a cluster */
1466ce3ee1e7SLuigi Rizzo 			p->numclusters = (i + p->_clustentries - 1) / p->_clustentries;
1467ccdc3305SLuigi Rizzo 			break;
1468ccdc3305SLuigi Rizzo 		}
1469dd4fcbc5SPatrick Kelsey 		/*
14704f80b14cSVincenzo Maffione 		 * Set lut state for all buffers in the current cluster.
1471dd4fcbc5SPatrick Kelsey 		 *
1472dd4fcbc5SPatrick Kelsey 		 * [i, lim) is the set of buffer indexes that cover the
1473dd4fcbc5SPatrick Kelsey 		 * current cluster.
1474dd4fcbc5SPatrick Kelsey 		 *
1475dd4fcbc5SPatrick Kelsey 		 * 'clust' is really the address of the current buffer in
1476dd4fcbc5SPatrick Kelsey 		 * the current cluster as we index through it with a stride
1477dd4fcbc5SPatrick Kelsey 		 * of p->_objsize.
1478dd4fcbc5SPatrick Kelsey 		 */
14798241616dSLuigi Rizzo 		for (; i < lim; i++, clust += p->_objsize) {
1480ccdc3305SLuigi Rizzo 			p->lut[i].vaddr = clust;
14814f80b14cSVincenzo Maffione #if !defined(linux) && !defined(_WIN32)
1482ccdc3305SLuigi Rizzo 			p->lut[i].paddr = vtophys(clust);
14834f80b14cSVincenzo Maffione #endif
1484ccdc3305SLuigi Rizzo 		}
1485ccdc3305SLuigi Rizzo 	}
1486253b2ec1SVincenzo Maffione 	p->memtotal = (size_t)p->numclusters * (size_t)p->_clustsize;
1487ae10d1afSLuigi Rizzo 	if (netmap_verbose)
1488253b2ec1SVincenzo Maffione 		nm_prinf("Pre-allocated %d clusters (%d/%zuKB) for '%s'",
1489ce3ee1e7SLuigi Rizzo 		    p->numclusters, p->_clustsize >> 10,
1490ce3ee1e7SLuigi Rizzo 		    p->memtotal >> 10, p->name);
1491ccdc3305SLuigi Rizzo 
14928241616dSLuigi Rizzo 	return 0;
1493ccdc3305SLuigi Rizzo 
1494ccdc3305SLuigi Rizzo clean:
14958241616dSLuigi Rizzo 	netmap_reset_obj_allocator(p);
14968241616dSLuigi Rizzo 	return ENOMEM;
14978241616dSLuigi Rizzo }
14988241616dSLuigi Rizzo 
14998241616dSLuigi Rizzo /* call with lock held */
15008241616dSLuigi Rizzo static int
1501c3e9b4dbSLuiz Otavio O Souza netmap_mem_params_changed(struct netmap_obj_params* p)
15028241616dSLuigi Rizzo {
1503c3e9b4dbSLuiz Otavio O Souza 	int i, rv = 0;
15048241616dSLuigi Rizzo 
15058241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1506c3e9b4dbSLuiz Otavio O Souza 		if (p[i].last_size != p[i].size || p[i].last_num != p[i].num) {
1507c3e9b4dbSLuiz Otavio O Souza 			p[i].last_size = p[i].size;
1508c3e9b4dbSLuiz Otavio O Souza 			p[i].last_num = p[i].num;
1509c3e9b4dbSLuiz Otavio O Souza 			rv = 1;
15108241616dSLuigi Rizzo 		}
1511c3e9b4dbSLuiz Otavio O Souza 	}
1512c3e9b4dbSLuiz Otavio O Souza 	return rv;
15138241616dSLuigi Rizzo }
15148241616dSLuigi Rizzo 
1515ce3ee1e7SLuigi Rizzo static void
1516ce3ee1e7SLuigi Rizzo netmap_mem_reset_all(struct netmap_mem_d *nmd)
1517ce3ee1e7SLuigi Rizzo {
1518ce3ee1e7SLuigi Rizzo 	int i;
1519f0ea3689SLuigi Rizzo 
1520b6e66be2SVincenzo Maffione 	if (netmap_debug & NM_DEBUG_MEM)
1521b6e66be2SVincenzo Maffione 		nm_prinf("resetting %p", nmd);
1522ce3ee1e7SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1523ce3ee1e7SLuigi Rizzo 		netmap_reset_obj_allocator(&nmd->pools[i]);
1524ce3ee1e7SLuigi Rizzo 	}
1525ce3ee1e7SLuigi Rizzo 	nmd->flags  &= ~NETMAP_MEM_FINALIZED;
1526ce3ee1e7SLuigi Rizzo }
1527ce3ee1e7SLuigi Rizzo 
1528ce3ee1e7SLuigi Rizzo static int
15294bf50f18SLuigi Rizzo netmap_mem_unmap(struct netmap_obj_pool *p, struct netmap_adapter *na)
15304bf50f18SLuigi Rizzo {
15312ff91c17SVincenzo Maffione 	int i, lim = p->objtotal;
1532de27b303SVincenzo Maffione 	struct netmap_lut *lut;
1533c3e9b4dbSLuiz Otavio O Souza 	if (na == NULL || na->pdev == NULL)
15344bf50f18SLuigi Rizzo 		return 0;
15354bf50f18SLuigi Rizzo 
1536de27b303SVincenzo Maffione 	lut = &na->na_lut;
1537a6d768d8SVincenzo Maffione 
1538a6d768d8SVincenzo Maffione 
1539a6d768d8SVincenzo Maffione 
154037e3a6d3SLuigi Rizzo #if defined(__FreeBSD__)
1541cfa866f6SMatt Macy 	/* On FreeBSD mapping and unmapping is performed by the txsync
1542cfa866f6SMatt Macy 	 * and rxsync routine, packet by packet. */
15434bf50f18SLuigi Rizzo 	(void)i;
15444bf50f18SLuigi Rizzo 	(void)lim;
15454f80b14cSVincenzo Maffione 	(void)lut;
154637e3a6d3SLuigi Rizzo #elif defined(_WIN32)
154737e3a6d3SLuigi Rizzo 	(void)i;
154837e3a6d3SLuigi Rizzo 	(void)lim;
15494f80b14cSVincenzo Maffione 	(void)lut;
1550b6e66be2SVincenzo Maffione 	nm_prerr("unsupported on Windows");
15514bf50f18SLuigi Rizzo #else /* linux */
155275f4f3edSVincenzo Maffione 	nm_prdis("unmapping and freeing plut for %s", na->name);
1553a6d768d8SVincenzo Maffione 	if (lut->plut == NULL || na->pdev == NULL)
15544f80b14cSVincenzo Maffione 		return 0;
15554f80b14cSVincenzo Maffione 	for (i = 0; i < lim; i += p->_clustentries) {
15564f80b14cSVincenzo Maffione 		if (lut->plut[i].paddr)
15574f80b14cSVincenzo Maffione 			netmap_unload_map(na, (bus_dma_tag_t) na->pdev, &lut->plut[i].paddr, p->_clustsize);
15584bf50f18SLuigi Rizzo 	}
15594f80b14cSVincenzo Maffione 	nm_free_plut(lut->plut);
15604f80b14cSVincenzo Maffione 	lut->plut = NULL;
15614bf50f18SLuigi Rizzo #endif /* linux */
15624bf50f18SLuigi Rizzo 
15634bf50f18SLuigi Rizzo 	return 0;
15644bf50f18SLuigi Rizzo }
15654bf50f18SLuigi Rizzo 
15664bf50f18SLuigi Rizzo static int
15674bf50f18SLuigi Rizzo netmap_mem_map(struct netmap_obj_pool *p, struct netmap_adapter *na)
15684bf50f18SLuigi Rizzo {
15694f80b14cSVincenzo Maffione 	int error = 0;
15704f80b14cSVincenzo Maffione 	int i, lim = p->objtotal;
15714f80b14cSVincenzo Maffione 	struct netmap_lut *lut = &na->na_lut;
15724bf50f18SLuigi Rizzo 
15734bf50f18SLuigi Rizzo 	if (na->pdev == NULL)
15744bf50f18SLuigi Rizzo 		return 0;
15754bf50f18SLuigi Rizzo 
15764f80b14cSVincenzo Maffione #if defined(__FreeBSD__)
1577cfa866f6SMatt Macy 	/* On FreeBSD mapping and unmapping is performed by the txsync
1578cfa866f6SMatt Macy 	 * and rxsync routine, packet by packet. */
15794f80b14cSVincenzo Maffione 	(void)i;
15804f80b14cSVincenzo Maffione 	(void)lim;
15814f80b14cSVincenzo Maffione 	(void)lut;
15824f80b14cSVincenzo Maffione #elif defined(_WIN32)
15834f80b14cSVincenzo Maffione 	(void)i;
15844f80b14cSVincenzo Maffione 	(void)lim;
15854f80b14cSVincenzo Maffione 	(void)lut;
1586b6e66be2SVincenzo Maffione 	nm_prerr("unsupported on Windows");
15874f80b14cSVincenzo Maffione #else /* linux */
15884f80b14cSVincenzo Maffione 
15894f80b14cSVincenzo Maffione 	if (lut->plut != NULL) {
159075f4f3edSVincenzo Maffione 		nm_prdis("plut already allocated for %s", na->name);
15914f80b14cSVincenzo Maffione 		return 0;
15924bf50f18SLuigi Rizzo 	}
15934f80b14cSVincenzo Maffione 
159475f4f3edSVincenzo Maffione 	nm_prdis("allocating physical lut for %s", na->name);
15954f80b14cSVincenzo Maffione 	lut->plut = nm_alloc_plut(lim);
15964f80b14cSVincenzo Maffione 	if (lut->plut == NULL) {
1597b6e66be2SVincenzo Maffione 		nm_prerr("Failed to allocate physical lut for %s", na->name);
15984f80b14cSVincenzo Maffione 		return ENOMEM;
15994f80b14cSVincenzo Maffione 	}
16004f80b14cSVincenzo Maffione 
16014f80b14cSVincenzo Maffione 	for (i = 0; i < lim; i += p->_clustentries) {
16024f80b14cSVincenzo Maffione 		lut->plut[i].paddr = 0;
16034f80b14cSVincenzo Maffione 	}
16044f80b14cSVincenzo Maffione 
16054f80b14cSVincenzo Maffione 	for (i = 0; i < lim; i += p->_clustentries) {
16064f80b14cSVincenzo Maffione 		int j;
16074f80b14cSVincenzo Maffione 
16084f80b14cSVincenzo Maffione 		if (p->lut[i].vaddr == NULL)
16094f80b14cSVincenzo Maffione 			continue;
16104f80b14cSVincenzo Maffione 
16114f80b14cSVincenzo Maffione 		error = netmap_load_map(na, (bus_dma_tag_t) na->pdev, &lut->plut[i].paddr,
16124f80b14cSVincenzo Maffione 				p->lut[i].vaddr, p->_clustsize);
16134f80b14cSVincenzo Maffione 		if (error) {
1614b6e66be2SVincenzo Maffione 			nm_prerr("Failed to map cluster #%d from the %s pool", i, p->name);
16154f80b14cSVincenzo Maffione 			break;
16164f80b14cSVincenzo Maffione 		}
16174f80b14cSVincenzo Maffione 
16184f80b14cSVincenzo Maffione 		for (j = 1; j < p->_clustentries; j++) {
16194f80b14cSVincenzo Maffione 			lut->plut[i + j].paddr = lut->plut[i + j - 1].paddr + p->_objsize;
16204f80b14cSVincenzo Maffione 		}
16214f80b14cSVincenzo Maffione 	}
16224f80b14cSVincenzo Maffione 
16234f80b14cSVincenzo Maffione 	if (error)
16244f80b14cSVincenzo Maffione 		netmap_mem_unmap(p, na);
16254f80b14cSVincenzo Maffione 
16264bf50f18SLuigi Rizzo #endif /* linux */
16274bf50f18SLuigi Rizzo 
16284f80b14cSVincenzo Maffione 	return error;
16294bf50f18SLuigi Rizzo }
16304bf50f18SLuigi Rizzo 
16314bf50f18SLuigi Rizzo static int
1632ce3ee1e7SLuigi Rizzo netmap_mem_finalize_all(struct netmap_mem_d *nmd)
1633ce3ee1e7SLuigi Rizzo {
1634ce3ee1e7SLuigi Rizzo 	int i;
1635ce3ee1e7SLuigi Rizzo 	if (nmd->flags & NETMAP_MEM_FINALIZED)
1636ce3ee1e7SLuigi Rizzo 		return 0;
1637ce3ee1e7SLuigi Rizzo 	nmd->lasterr = 0;
1638ce3ee1e7SLuigi Rizzo 	nmd->nm_totalsize = 0;
1639ce3ee1e7SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1640ce3ee1e7SLuigi Rizzo 		nmd->lasterr = netmap_finalize_obj_allocator(&nmd->pools[i]);
1641ce3ee1e7SLuigi Rizzo 		if (nmd->lasterr)
1642ce3ee1e7SLuigi Rizzo 			goto error;
1643ce3ee1e7SLuigi Rizzo 		nmd->nm_totalsize += nmd->pools[i].memtotal;
1644ce3ee1e7SLuigi Rizzo 	}
1645a6d768d8SVincenzo Maffione 	nmd->nm_totalsize = (nmd->nm_totalsize + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
16464f80b14cSVincenzo Maffione 	nmd->lasterr = netmap_mem_init_bitmaps(nmd);
16474f80b14cSVincenzo Maffione 	if (nmd->lasterr)
16484f80b14cSVincenzo Maffione 		goto error;
16494f80b14cSVincenzo Maffione 
1650ce3ee1e7SLuigi Rizzo 	nmd->flags |= NETMAP_MEM_FINALIZED;
1651ce3ee1e7SLuigi Rizzo 
1652f0ea3689SLuigi Rizzo 	if (netmap_verbose)
1653253b2ec1SVincenzo Maffione 		nm_prinf("interfaces %zd KB, rings %zd KB, buffers %zd MB",
1654ce3ee1e7SLuigi Rizzo 		    nmd->pools[NETMAP_IF_POOL].memtotal >> 10,
1655ce3ee1e7SLuigi Rizzo 		    nmd->pools[NETMAP_RING_POOL].memtotal >> 10,
1656ce3ee1e7SLuigi Rizzo 		    nmd->pools[NETMAP_BUF_POOL].memtotal >> 20);
1657ce3ee1e7SLuigi Rizzo 
1658f0ea3689SLuigi Rizzo 	if (netmap_verbose)
1659b6e66be2SVincenzo Maffione 		nm_prinf("Free buffers: %d", nmd->pools[NETMAP_BUF_POOL].objfree);
1660ce3ee1e7SLuigi Rizzo 
1661ce3ee1e7SLuigi Rizzo 
1662ce3ee1e7SLuigi Rizzo 	return 0;
1663ce3ee1e7SLuigi Rizzo error:
1664ce3ee1e7SLuigi Rizzo 	netmap_mem_reset_all(nmd);
1665ce3ee1e7SLuigi Rizzo 	return nmd->lasterr;
1666ce3ee1e7SLuigi Rizzo }
1667ce3ee1e7SLuigi Rizzo 
1668f0ea3689SLuigi Rizzo /*
1669f0ea3689SLuigi Rizzo  * allocator for private memory
1670f0ea3689SLuigi Rizzo  */
16714f80b14cSVincenzo Maffione static void *
1672a6d768d8SVincenzo Maffione _netmap_mem_private_new(size_t size, struct netmap_obj_params *p, int grp_id,
1673*893caa2aSMark Johnston 		const struct netmap_mem_ops *ops, uint64_t memtotal, int *perr)
1674ce3ee1e7SLuigi Rizzo {
1675ce3ee1e7SLuigi Rizzo 	struct netmap_mem_d *d = NULL;
1676c3e9b4dbSLuiz Otavio O Souza 	int i, err = 0;
1677a6d768d8SVincenzo Maffione 	int checksz = 0;
1678a6d768d8SVincenzo Maffione 
1679a6d768d8SVincenzo Maffione 	/* if memtotal is !=0 we check that the request fits the available
1680a6d768d8SVincenzo Maffione 	 * memory. Moreover, any surprlus memory is assigned to buffers.
1681a6d768d8SVincenzo Maffione 	 */
1682a6d768d8SVincenzo Maffione 	checksz = (memtotal > 0);
1683ce3ee1e7SLuigi Rizzo 
16844f80b14cSVincenzo Maffione 	d = nm_os_malloc(size);
1685f0ea3689SLuigi Rizzo 	if (d == NULL) {
1686f0ea3689SLuigi Rizzo 		err = ENOMEM;
1687f0ea3689SLuigi Rizzo 		goto error;
1688f0ea3689SLuigi Rizzo 	}
1689ce3ee1e7SLuigi Rizzo 
1690ce3ee1e7SLuigi Rizzo 	*d = nm_blueprint;
16914f80b14cSVincenzo Maffione 	d->ops = ops;
1692ce3ee1e7SLuigi Rizzo 
1693a6d768d8SVincenzo Maffione 	err = nm_mem_assign_id(d, grp_id);
1694f0ea3689SLuigi Rizzo 	if (err)
16954f80b14cSVincenzo Maffione 		goto error_free;
1696c3e9b4dbSLuiz Otavio O Souza 	snprintf(d->name, NM_MEM_NAMESZ, "%d", d->nm_id);
1697f0ea3689SLuigi Rizzo 
1698c3e9b4dbSLuiz Otavio O Souza 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1699c3e9b4dbSLuiz Otavio O Souza 		snprintf(d->pools[i].name, NETMAP_POOL_MAX_NAMSZ,
1700c3e9b4dbSLuiz Otavio O Souza 				nm_blueprint.pools[i].name,
1701c3e9b4dbSLuiz Otavio O Souza 				d->name);
1702a6d768d8SVincenzo Maffione 		if (checksz) {
17033e3314a8SVincenzo Maffione 			uint64_t poolsz = (uint64_t)p[i].num * p[i].size;
1704a6d768d8SVincenzo Maffione 			if (memtotal < poolsz) {
1705a6d768d8SVincenzo Maffione 				nm_prerr("%s: request too large", d->pools[i].name);
1706a6d768d8SVincenzo Maffione 				err = ENOMEM;
170798399ab0SVincenzo Maffione 				goto error_rel_id;
1708a6d768d8SVincenzo Maffione 			}
1709a6d768d8SVincenzo Maffione 			memtotal -= poolsz;
1710a6d768d8SVincenzo Maffione 		}
1711c3e9b4dbSLuiz Otavio O Souza 		d->params[i].num = p[i].num;
1712c3e9b4dbSLuiz Otavio O Souza 		d->params[i].size = p[i].size;
1713c3e9b4dbSLuiz Otavio O Souza 	}
1714a6d768d8SVincenzo Maffione 	if (checksz && memtotal > 0) {
1715a6d768d8SVincenzo Maffione 		uint64_t sz = d->params[NETMAP_BUF_POOL].size;
1716a6d768d8SVincenzo Maffione 		uint64_t n = (memtotal + sz - 1) / sz;
1717a6d768d8SVincenzo Maffione 
1718a6d768d8SVincenzo Maffione 		if (n) {
1719a6d768d8SVincenzo Maffione 			if (netmap_verbose) {
1720a6d768d8SVincenzo Maffione 				nm_prinf("%s: adding %llu more buffers",
1721a6d768d8SVincenzo Maffione 				    d->pools[NETMAP_BUF_POOL].name,
1722a6d768d8SVincenzo Maffione 				    (unsigned long long)n);
1723a6d768d8SVincenzo Maffione 			}
1724a6d768d8SVincenzo Maffione 			d->params[NETMAP_BUF_POOL].num += n;
1725a6d768d8SVincenzo Maffione 		}
1726a6d768d8SVincenzo Maffione 	}
1727c3e9b4dbSLuiz Otavio O Souza 
1728c3e9b4dbSLuiz Otavio O Souza 	NMA_LOCK_INIT(d);
1729c3e9b4dbSLuiz Otavio O Souza 
1730c3e9b4dbSLuiz Otavio O Souza 	err = netmap_mem_config(d);
1731c3e9b4dbSLuiz Otavio O Souza 	if (err)
173298399ab0SVincenzo Maffione 		goto error_destroy_lock;
1733c3e9b4dbSLuiz Otavio O Souza 
1734c3e9b4dbSLuiz Otavio O Souza 	d->flags &= ~NETMAP_MEM_FINALIZED;
1735c3e9b4dbSLuiz Otavio O Souza 
1736c3e9b4dbSLuiz Otavio O Souza 	return d;
1737c3e9b4dbSLuiz Otavio O Souza 
173898399ab0SVincenzo Maffione error_destroy_lock:
17394f80b14cSVincenzo Maffione 	NMA_LOCK_DESTROY(d);
174098399ab0SVincenzo Maffione error_rel_id:
17414f80b14cSVincenzo Maffione 	nm_mem_release_id(d);
17424f80b14cSVincenzo Maffione error_free:
17434f80b14cSVincenzo Maffione 	nm_os_free(d);
1744c3e9b4dbSLuiz Otavio O Souza error:
1745c3e9b4dbSLuiz Otavio O Souza 	if (perr)
1746c3e9b4dbSLuiz Otavio O Souza 		*perr = err;
1747c3e9b4dbSLuiz Otavio O Souza 	return NULL;
1748c3e9b4dbSLuiz Otavio O Souza }
1749c3e9b4dbSLuiz Otavio O Souza 
1750c3e9b4dbSLuiz Otavio O Souza struct netmap_mem_d *
1751c3e9b4dbSLuiz Otavio O Souza netmap_mem_private_new(u_int txr, u_int txd, u_int rxr, u_int rxd,
1752c3e9b4dbSLuiz Otavio O Souza 		u_int extra_bufs, u_int npipes, int *perr)
1753c3e9b4dbSLuiz Otavio O Souza {
1754c3e9b4dbSLuiz Otavio O Souza 	struct netmap_mem_d *d = NULL;
1755c3e9b4dbSLuiz Otavio O Souza 	struct netmap_obj_params p[NETMAP_POOLS_NR];
17564f80b14cSVincenzo Maffione 	int i;
1757c3e9b4dbSLuiz Otavio O Souza 	u_int v, maxd;
1758f0ea3689SLuigi Rizzo 	/* account for the fake host rings */
1759ce3ee1e7SLuigi Rizzo 	txr++;
1760ce3ee1e7SLuigi Rizzo 	rxr++;
1761ce3ee1e7SLuigi Rizzo 
1762f0ea3689SLuigi Rizzo 	/* copy the min values */
1763f0ea3689SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1764f0ea3689SLuigi Rizzo 		p[i] = netmap_min_priv_params[i];
1765f0ea3689SLuigi Rizzo 	}
1766f0ea3689SLuigi Rizzo 
1767f0ea3689SLuigi Rizzo 	/* possibly increase them to fit user request */
1768f0ea3689SLuigi Rizzo 	v = sizeof(struct netmap_if) + sizeof(ssize_t) * (txr + rxr);
1769f0ea3689SLuigi Rizzo 	if (p[NETMAP_IF_POOL].size < v)
1770f0ea3689SLuigi Rizzo 		p[NETMAP_IF_POOL].size = v;
1771f0ea3689SLuigi Rizzo 	v = 2 + 4 * npipes;
1772f0ea3689SLuigi Rizzo 	if (p[NETMAP_IF_POOL].num < v)
1773f0ea3689SLuigi Rizzo 		p[NETMAP_IF_POOL].num = v;
1774f0ea3689SLuigi Rizzo 	maxd = (txd > rxd) ? txd : rxd;
1775f0ea3689SLuigi Rizzo 	v = sizeof(struct netmap_ring) + sizeof(struct netmap_slot) * maxd;
1776f0ea3689SLuigi Rizzo 	if (p[NETMAP_RING_POOL].size < v)
1777f0ea3689SLuigi Rizzo 		p[NETMAP_RING_POOL].size = v;
1778f0ea3689SLuigi Rizzo 	/* each pipe endpoint needs two tx rings (1 normal + 1 host, fake)
1779f0ea3689SLuigi Rizzo 	 * and two rx rings (again, 1 normal and 1 fake host)
1780f0ea3689SLuigi Rizzo 	 */
1781f0ea3689SLuigi Rizzo 	v = txr + rxr + 8 * npipes;
1782f0ea3689SLuigi Rizzo 	if (p[NETMAP_RING_POOL].num < v)
1783f0ea3689SLuigi Rizzo 		p[NETMAP_RING_POOL].num = v;
1784f0ea3689SLuigi Rizzo 	/* for each pipe we only need the buffers for the 4 "real" rings.
1785f0ea3689SLuigi Rizzo 	 * On the other end, the pipe ring dimension may be different from
1786f0ea3689SLuigi Rizzo 	 * the parent port ring dimension. As a compromise, we allocate twice the
1787f0ea3689SLuigi Rizzo 	 * space actually needed if the pipe rings were the same size as the parent rings
1788f0ea3689SLuigi Rizzo 	 */
1789f0ea3689SLuigi Rizzo 	v = (4 * npipes + rxr) * rxd + (4 * npipes + txr) * txd + 2 + extra_bufs;
1790f0ea3689SLuigi Rizzo 		/* the +2 is for the tx and rx fake buffers (indices 0 and 1) */
1791f0ea3689SLuigi Rizzo 	if (p[NETMAP_BUF_POOL].num < v)
1792f0ea3689SLuigi Rizzo 		p[NETMAP_BUF_POOL].num = v;
1793f0ea3689SLuigi Rizzo 
1794f0ea3689SLuigi Rizzo 	if (netmap_verbose)
1795b6e66be2SVincenzo Maffione 		nm_prinf("req if %d*%d ring %d*%d buf %d*%d",
1796ce3ee1e7SLuigi Rizzo 			p[NETMAP_IF_POOL].num,
1797ce3ee1e7SLuigi Rizzo 			p[NETMAP_IF_POOL].size,
1798ce3ee1e7SLuigi Rizzo 			p[NETMAP_RING_POOL].num,
1799ce3ee1e7SLuigi Rizzo 			p[NETMAP_RING_POOL].size,
1800ce3ee1e7SLuigi Rizzo 			p[NETMAP_BUF_POOL].num,
1801ce3ee1e7SLuigi Rizzo 			p[NETMAP_BUF_POOL].size);
1802ce3ee1e7SLuigi Rizzo 
1803a6d768d8SVincenzo Maffione 	d = _netmap_mem_private_new(sizeof(*d), p, -1, &netmap_mem_global_ops, 0, perr);
1804ce3ee1e7SLuigi Rizzo 
1805ce3ee1e7SLuigi Rizzo 	return d;
1806ce3ee1e7SLuigi Rizzo }
1807ce3ee1e7SLuigi Rizzo 
1808a6d768d8SVincenzo Maffione /* Reference iommu allocator - find existing or create new,
1809a6d768d8SVincenzo Maffione  * for not hw addapeters fallback to global allocator.
1810a6d768d8SVincenzo Maffione  */
1811a6d768d8SVincenzo Maffione struct netmap_mem_d *
1812a6d768d8SVincenzo Maffione netmap_mem_get_iommu(struct netmap_adapter *na)
1813a6d768d8SVincenzo Maffione {
1814a6d768d8SVincenzo Maffione 	int i, err, grp_id;
1815a6d768d8SVincenzo Maffione 	struct netmap_mem_d *nmd;
1816a6d768d8SVincenzo Maffione 
1817a6d768d8SVincenzo Maffione 	if (na == NULL || na->pdev == NULL)
1818a6d768d8SVincenzo Maffione 		return netmap_mem_get(&nm_mem);
1819a6d768d8SVincenzo Maffione 
1820a6d768d8SVincenzo Maffione 	grp_id = nm_iommu_group_id(na->pdev);
1821a6d768d8SVincenzo Maffione 
1822a6d768d8SVincenzo Maffione 	NM_MTX_LOCK(nm_mem_list_lock);
1823a6d768d8SVincenzo Maffione 	nmd = netmap_last_mem_d;
1824a6d768d8SVincenzo Maffione 	do {
1825a6d768d8SVincenzo Maffione 		if (!(nmd->flags & NETMAP_MEM_HIDDEN) && nmd->nm_grp == grp_id) {
1826a6d768d8SVincenzo Maffione 			nmd->refcount++;
1827a6d768d8SVincenzo Maffione 			NM_DBG_REFC(nmd, __FUNCTION__, __LINE__);
1828a6d768d8SVincenzo Maffione 			NM_MTX_UNLOCK(nm_mem_list_lock);
1829a6d768d8SVincenzo Maffione 			return nmd;
1830a6d768d8SVincenzo Maffione 		}
1831a6d768d8SVincenzo Maffione 		nmd = nmd->next;
1832a6d768d8SVincenzo Maffione 	} while (nmd != netmap_last_mem_d);
1833a6d768d8SVincenzo Maffione 
1834a6d768d8SVincenzo Maffione 	nmd = nm_os_malloc(sizeof(*nmd));
1835a6d768d8SVincenzo Maffione 	if (nmd == NULL)
1836a6d768d8SVincenzo Maffione 		goto error;
1837a6d768d8SVincenzo Maffione 
1838a6d768d8SVincenzo Maffione 	*nmd = nm_mem_blueprint;
1839a6d768d8SVincenzo Maffione 
1840a6d768d8SVincenzo Maffione 	err = nm_mem_assign_id_locked(nmd, grp_id);
1841a6d768d8SVincenzo Maffione 	if (err)
1842a6d768d8SVincenzo Maffione 		goto error_free;
1843a6d768d8SVincenzo Maffione 
1844a6d768d8SVincenzo Maffione 	snprintf(nmd->name, sizeof(nmd->name), "%d", nmd->nm_id);
1845a6d768d8SVincenzo Maffione 
1846a6d768d8SVincenzo Maffione 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1847a6d768d8SVincenzo Maffione 		snprintf(nmd->pools[i].name, NETMAP_POOL_MAX_NAMSZ, "%s-%s",
1848a6d768d8SVincenzo Maffione 			nm_mem_blueprint.pools[i].name, nmd->name);
1849a6d768d8SVincenzo Maffione 	}
1850a6d768d8SVincenzo Maffione 
1851a6d768d8SVincenzo Maffione 	NMA_LOCK_INIT(nmd);
1852a6d768d8SVincenzo Maffione 
1853a6d768d8SVincenzo Maffione 	NM_MTX_UNLOCK(nm_mem_list_lock);
1854a6d768d8SVincenzo Maffione 	return nmd;
1855a6d768d8SVincenzo Maffione 
1856a6d768d8SVincenzo Maffione error_free:
1857a6d768d8SVincenzo Maffione 	nm_os_free(nmd);
1858a6d768d8SVincenzo Maffione error:
1859a6d768d8SVincenzo Maffione 	NM_MTX_UNLOCK(nm_mem_list_lock);
1860a6d768d8SVincenzo Maffione 	return NULL;
1861a6d768d8SVincenzo Maffione }
18628241616dSLuigi Rizzo 
18638241616dSLuigi Rizzo /* call with lock held */
18648241616dSLuigi Rizzo static int
1865c3e9b4dbSLuiz Otavio O Souza netmap_mem2_config(struct netmap_mem_d *nmd)
18668241616dSLuigi Rizzo {
18678241616dSLuigi Rizzo 	int i;
18688241616dSLuigi Rizzo 
1869c3e9b4dbSLuiz Otavio O Souza 	if (!netmap_mem_params_changed(nmd->params))
18708241616dSLuigi Rizzo 		goto out;
18718241616dSLuigi Rizzo 
187275f4f3edSVincenzo Maffione 	nm_prdis("reconfiguring");
18738241616dSLuigi Rizzo 
1874ce3ee1e7SLuigi Rizzo 	if (nmd->flags & NETMAP_MEM_FINALIZED) {
18758241616dSLuigi Rizzo 		/* reset previous allocation */
18768241616dSLuigi Rizzo 		for (i = 0; i < NETMAP_POOLS_NR; i++) {
1877ce3ee1e7SLuigi Rizzo 			netmap_reset_obj_allocator(&nmd->pools[i]);
18788241616dSLuigi Rizzo 		}
1879ce3ee1e7SLuigi Rizzo 		nmd->flags &= ~NETMAP_MEM_FINALIZED;
18808241616dSLuigi Rizzo 	}
18818241616dSLuigi Rizzo 
18828241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1883ce3ee1e7SLuigi Rizzo 		nmd->lasterr = netmap_config_obj_allocator(&nmd->pools[i],
1884c3e9b4dbSLuiz Otavio O Souza 				nmd->params[i].num, nmd->params[i].size);
1885ce3ee1e7SLuigi Rizzo 		if (nmd->lasterr)
18868241616dSLuigi Rizzo 			goto out;
18878241616dSLuigi Rizzo 	}
18888241616dSLuigi Rizzo 
18898241616dSLuigi Rizzo out:
18908241616dSLuigi Rizzo 
1891ce3ee1e7SLuigi Rizzo 	return nmd->lasterr;
18928241616dSLuigi Rizzo }
18938241616dSLuigi Rizzo 
18948241616dSLuigi Rizzo static int
1895a6d768d8SVincenzo Maffione netmap_mem2_finalize(struct netmap_mem_d *nmd, struct netmap_adapter *na)
18968241616dSLuigi Rizzo {
18972ff91c17SVincenzo Maffione 	if (nmd->flags & NETMAP_MEM_FINALIZED)
18988241616dSLuigi Rizzo 		goto out;
18998241616dSLuigi Rizzo 
1900ce3ee1e7SLuigi Rizzo 	if (netmap_mem_finalize_all(nmd))
1901ce3ee1e7SLuigi Rizzo 		goto out;
19028241616dSLuigi Rizzo 
1903ce3ee1e7SLuigi Rizzo 	nmd->lasterr = 0;
19048241616dSLuigi Rizzo 
19058241616dSLuigi Rizzo out:
19062ff91c17SVincenzo Maffione 	return nmd->lasterr;
1907ccdc3305SLuigi Rizzo }
1908ccdc3305SLuigi Rizzo 
1909847bf383SLuigi Rizzo static void
1910c3e9b4dbSLuiz Otavio O Souza netmap_mem2_delete(struct netmap_mem_d *nmd)
1911ccdc3305SLuigi Rizzo {
19128241616dSLuigi Rizzo 	int i;
19138241616dSLuigi Rizzo 
19148241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1915c3e9b4dbSLuiz Otavio O Souza 	    netmap_destroy_obj_allocator(&nmd->pools[i]);
19168241616dSLuigi Rizzo 	}
1917847bf383SLuigi Rizzo 
1918c3e9b4dbSLuiz Otavio O Souza 	NMA_LOCK_DESTROY(nmd);
1919c3e9b4dbSLuiz Otavio O Souza 	if (nmd != &nm_mem)
1920c3e9b4dbSLuiz Otavio O Souza 		nm_os_free(nmd);
19218241616dSLuigi Rizzo }
19228241616dSLuigi Rizzo 
19234f80b14cSVincenzo Maffione #ifdef WITH_EXTMEM
19244f80b14cSVincenzo Maffione /* doubly linekd list of all existing external allocators */
19254f80b14cSVincenzo Maffione static struct netmap_mem_ext *netmap_mem_ext_list = NULL;
19264f80b14cSVincenzo Maffione NM_MTX_T nm_mem_ext_list_lock;
19274f80b14cSVincenzo Maffione #endif /* WITH_EXTMEM */
19284f80b14cSVincenzo Maffione 
1929847bf383SLuigi Rizzo int
1930847bf383SLuigi Rizzo netmap_mem_init(void)
1931847bf383SLuigi Rizzo {
1932a6d768d8SVincenzo Maffione 	nm_mem_blueprint = nm_mem;
1933c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_INIT(nm_mem_list_lock);
1934847bf383SLuigi Rizzo 	NMA_LOCK_INIT(&nm_mem);
1935847bf383SLuigi Rizzo 	netmap_mem_get(&nm_mem);
19364f80b14cSVincenzo Maffione #ifdef WITH_EXTMEM
19374f80b14cSVincenzo Maffione 	NM_MTX_INIT(nm_mem_ext_list_lock);
19384f80b14cSVincenzo Maffione #endif /* WITH_EXTMEM */
1939847bf383SLuigi Rizzo 	return (0);
1940847bf383SLuigi Rizzo }
1941847bf383SLuigi Rizzo 
1942847bf383SLuigi Rizzo void
1943847bf383SLuigi Rizzo netmap_mem_fini(void)
1944847bf383SLuigi Rizzo {
1945847bf383SLuigi Rizzo 	netmap_mem_put(&nm_mem);
1946847bf383SLuigi Rizzo }
1947847bf383SLuigi Rizzo 
1948a6d768d8SVincenzo Maffione static int
1949a6d768d8SVincenzo Maffione netmap_mem_ring_needed(struct netmap_kring *kring)
19508241616dSLuigi Rizzo {
1951a6d768d8SVincenzo Maffione 	return kring->ring == NULL &&
1952a6d768d8SVincenzo Maffione 		(kring->users > 0 ||
1953a6d768d8SVincenzo Maffione 		 (kring->nr_kflags & NKR_NEEDRING));
1954a6d768d8SVincenzo Maffione }
1955847bf383SLuigi Rizzo 
1956a6d768d8SVincenzo Maffione static int
1957a6d768d8SVincenzo Maffione netmap_mem_ring_todelete(struct netmap_kring *kring)
1958a6d768d8SVincenzo Maffione {
1959a6d768d8SVincenzo Maffione 	return kring->ring != NULL &&
1960a6d768d8SVincenzo Maffione 		kring->users == 0 &&
1961a6d768d8SVincenzo Maffione 		!(kring->nr_kflags & NKR_NEEDRING);
1962a6d768d8SVincenzo Maffione }
1963847bf383SLuigi Rizzo 
1964ccdc3305SLuigi Rizzo 
1965f9790aebSLuigi Rizzo /* call with NMA_LOCK held *
1966f9790aebSLuigi Rizzo  *
1967f9790aebSLuigi Rizzo  * Allocate netmap rings and buffers for this card
1968f9790aebSLuigi Rizzo  * The rings are contiguous, but have variable size.
1969f0ea3689SLuigi Rizzo  * The kring array must follow the layout described
1970f0ea3689SLuigi Rizzo  * in netmap_krings_create().
1971f9790aebSLuigi Rizzo  */
1972847bf383SLuigi Rizzo static int
1973a6d768d8SVincenzo Maffione netmap_mem2_rings_create(struct netmap_mem_d *nmd, struct netmap_adapter *na)
1974f9790aebSLuigi Rizzo {
1975847bf383SLuigi Rizzo 	enum txrx t;
1976f9790aebSLuigi Rizzo 
1977847bf383SLuigi Rizzo 	for_rx_tx(t) {
1978847bf383SLuigi Rizzo 		u_int i;
1979847bf383SLuigi Rizzo 
19802a7db7a6SVincenzo Maffione 		for (i = 0; i < netmap_all_rings(na, t); i++) {
19812ff91c17SVincenzo Maffione 			struct netmap_kring *kring = NMR(na, t)[i];
1982847bf383SLuigi Rizzo 			struct netmap_ring *ring = kring->ring;
1983847bf383SLuigi Rizzo 			u_int len, ndesc;
1984847bf383SLuigi Rizzo 
1985a6d768d8SVincenzo Maffione 			if (!netmap_mem_ring_needed(kring)) {
198645c67e8fSVincenzo Maffione 				/* unneeded, or already created by somebody else */
1987b6e66be2SVincenzo Maffione 				if (netmap_debug & NM_DEBUG_MEM)
1988b6e66be2SVincenzo Maffione 					nm_prinf("NOT creating ring %s (ring %p, users %d neekring %d)",
19894f80b14cSVincenzo Maffione 						kring->name, ring, kring->users, kring->nr_kflags & NKR_NEEDRING);
199037e3a6d3SLuigi Rizzo 				continue;
1991f0ea3689SLuigi Rizzo 			}
1992b6e66be2SVincenzo Maffione 			if (netmap_debug & NM_DEBUG_MEM)
1993b6e66be2SVincenzo Maffione 				nm_prinf("creating %s", kring->name);
1994f9790aebSLuigi Rizzo 			ndesc = kring->nkr_num_slots;
1995f9790aebSLuigi Rizzo 			len = sizeof(struct netmap_ring) +
1996f9790aebSLuigi Rizzo 				  ndesc * sizeof(struct netmap_slot);
1997a6d768d8SVincenzo Maffione 			ring = netmap_ring_malloc(nmd, len);
1998f9790aebSLuigi Rizzo 			if (ring == NULL) {
1999b6e66be2SVincenzo Maffione 				nm_prerr("Cannot allocate %s_ring", nm_txrx2str(t));
2000f9790aebSLuigi Rizzo 				goto cleanup;
2001f9790aebSLuigi Rizzo 			}
200275f4f3edSVincenzo Maffione 			nm_prdis("txring at %p", ring);
2003f9790aebSLuigi Rizzo 			kring->ring = ring;
2004f9790aebSLuigi Rizzo 			*(uint32_t *)(uintptr_t)&ring->num_slots = ndesc;
200517885a7bSLuigi Rizzo 			*(int64_t *)(uintptr_t)&ring->buf_ofs =
2006a6d768d8SVincenzo Maffione 			    (nmd->pools[NETMAP_IF_POOL].memtotal +
2007a6d768d8SVincenzo Maffione 				nmd->pools[NETMAP_RING_POOL].memtotal) -
2008a6d768d8SVincenzo Maffione 				netmap_ring_offset(nmd, ring);
2009f9790aebSLuigi Rizzo 
201017885a7bSLuigi Rizzo 			/* copy values from kring */
201117885a7bSLuigi Rizzo 			ring->head = kring->rhead;
201217885a7bSLuigi Rizzo 			ring->cur = kring->rcur;
201317885a7bSLuigi Rizzo 			ring->tail = kring->rtail;
20144f80b14cSVincenzo Maffione 			*(uint32_t *)(uintptr_t)&ring->nr_buf_size =
2015a6d768d8SVincenzo Maffione 				netmap_mem_bufsize(nmd);
201675f4f3edSVincenzo Maffione 			nm_prdis("%s h %d c %d t %d", kring->name,
2017f0ea3689SLuigi Rizzo 				ring->head, ring->cur, ring->tail);
201875f4f3edSVincenzo Maffione 			nm_prdis("initializing slots for %s_ring", nm_txrx2str(t));
20192ff91c17SVincenzo Maffione 			if (!(kring->nr_kflags & NKR_FAKERING)) {
2020f0ea3689SLuigi Rizzo 				/* this is a real ring */
2021b6e66be2SVincenzo Maffione 				if (netmap_debug & NM_DEBUG_MEM)
2022b6e66be2SVincenzo Maffione 					nm_prinf("allocating buffers for %s", kring->name);
2023a6d768d8SVincenzo Maffione 				if (netmap_new_bufs(nmd, ring->slot, ndesc)) {
2024b6e66be2SVincenzo Maffione 					nm_prerr("Cannot allocate buffers for %s_ring", nm_txrx2str(t));
2025f9790aebSLuigi Rizzo 					goto cleanup;
2026f9790aebSLuigi Rizzo 				}
2027f0ea3689SLuigi Rizzo 			} else {
2028847bf383SLuigi Rizzo 				/* this is a fake ring, set all indices to 0 */
2029b6e66be2SVincenzo Maffione 				if (netmap_debug & NM_DEBUG_MEM)
2030b6e66be2SVincenzo Maffione 					nm_prinf("NOT allocating buffers for %s", kring->name);
2031a6d768d8SVincenzo Maffione 				netmap_mem_set_ring(nmd, ring->slot, ndesc, 0);
2032f0ea3689SLuigi Rizzo 			}
2033847bf383SLuigi Rizzo 		        /* ring info */
2034847bf383SLuigi Rizzo 		        *(uint16_t *)(uintptr_t)&ring->ringid = kring->ring_id;
2035847bf383SLuigi Rizzo 		        *(uint16_t *)(uintptr_t)&ring->dir = kring->tx;
2036f0ea3689SLuigi Rizzo 		}
2037f9790aebSLuigi Rizzo 	}
2038f9790aebSLuigi Rizzo 
2039f9790aebSLuigi Rizzo 	return 0;
2040f9790aebSLuigi Rizzo 
2041f9790aebSLuigi Rizzo cleanup:
2042cfa866f6SMatt Macy 	/* we cannot actually cleanup here, since we don't own kring->users
2043cfa866f6SMatt Macy 	 * and kring->nr_klags & NKR_NEEDRING. The caller must decrement
2044cfa866f6SMatt Macy 	 * the first or zero-out the second, then call netmap_free_rings()
2045cfa866f6SMatt Macy 	 * to do the cleanup
2046cfa866f6SMatt Macy 	 */
2047f9790aebSLuigi Rizzo 
2048f9790aebSLuigi Rizzo 	return ENOMEM;
2049f9790aebSLuigi Rizzo }
2050f9790aebSLuigi Rizzo 
2051847bf383SLuigi Rizzo static void
2052a6d768d8SVincenzo Maffione netmap_mem2_rings_delete(struct netmap_mem_d *nmd, struct netmap_adapter *na)
2053f9790aebSLuigi Rizzo {
2054a6d768d8SVincenzo Maffione 	enum txrx t;
2055ccdc3305SLuigi Rizzo 
2056a6d768d8SVincenzo Maffione 	for_rx_tx(t) {
2057a6d768d8SVincenzo Maffione 		u_int i;
2058a6d768d8SVincenzo Maffione 		for (i = 0; i < netmap_all_rings(na, t); i++) {
2059a6d768d8SVincenzo Maffione 			struct netmap_kring *kring = NMR(na, t)[i];
2060a6d768d8SVincenzo Maffione 			struct netmap_ring *ring = kring->ring;
2061a6d768d8SVincenzo Maffione 
2062a6d768d8SVincenzo Maffione 			if (!netmap_mem_ring_todelete(kring)) {
2063a6d768d8SVincenzo Maffione 				if (netmap_debug & NM_DEBUG_MEM)
2064a6d768d8SVincenzo Maffione 					nm_prinf("NOT deleting ring %s (ring %p, users %d neekring %d)",
2065a6d768d8SVincenzo Maffione 						kring->name, ring, kring->users, kring->nr_kflags & NKR_NEEDRING);
2066a6d768d8SVincenzo Maffione 				continue;
2067a6d768d8SVincenzo Maffione 			}
2068a6d768d8SVincenzo Maffione 			if (netmap_debug & NM_DEBUG_MEM)
2069a6d768d8SVincenzo Maffione 				nm_prinf("deleting ring %s", kring->name);
2070a6d768d8SVincenzo Maffione 			if (!(kring->nr_kflags & NKR_FAKERING)) {
2071a6d768d8SVincenzo Maffione 				nm_prdis("freeing bufs for %s", kring->name);
2072a6d768d8SVincenzo Maffione 				netmap_free_bufs(nmd, ring->slot, kring->nkr_num_slots);
2073a6d768d8SVincenzo Maffione 			} else {
2074a6d768d8SVincenzo Maffione 				nm_prdis("NOT freeing bufs for %s", kring->name);
2075a6d768d8SVincenzo Maffione 			}
2076a6d768d8SVincenzo Maffione 			netmap_ring_free(nmd, ring);
2077a6d768d8SVincenzo Maffione 			kring->ring = NULL;
2078a6d768d8SVincenzo Maffione 		}
2079a6d768d8SVincenzo Maffione 	}
2080a6d768d8SVincenzo Maffione }
2081ccdc3305SLuigi Rizzo 
20828241616dSLuigi Rizzo /* call with NMA_LOCK held */
2083ae10d1afSLuigi Rizzo /*
2084ae10d1afSLuigi Rizzo  * Allocate the per-fd structure netmap_if.
2085ce3ee1e7SLuigi Rizzo  *
2086ce3ee1e7SLuigi Rizzo  * We assume that the configuration stored in na
2087ce3ee1e7SLuigi Rizzo  * (number of tx/rx rings and descs) does not change while
2088ce3ee1e7SLuigi Rizzo  * the interface is in netmap mode.
2089ae10d1afSLuigi Rizzo  */
2090847bf383SLuigi Rizzo static struct netmap_if *
2091a6d768d8SVincenzo Maffione netmap_mem2_if_new(struct netmap_mem_d *nmd,
2092a6d768d8SVincenzo Maffione 		struct netmap_adapter *na, struct netmap_priv_d *priv)
2093ccdc3305SLuigi Rizzo {
2094ccdc3305SLuigi Rizzo 	struct netmap_if *nifp;
2095ccdc3305SLuigi Rizzo 	ssize_t base; /* handy for relative offsets between rings and nifp */
2096847bf383SLuigi Rizzo 	u_int i, len, n[NR_TXRX], ntot;
2097847bf383SLuigi Rizzo 	enum txrx t;
2098ccdc3305SLuigi Rizzo 
2099847bf383SLuigi Rizzo 	ntot = 0;
2100847bf383SLuigi Rizzo 	for_rx_tx(t) {
2101f0ea3689SLuigi Rizzo 		/* account for the (eventually fake) host rings */
21022a7db7a6SVincenzo Maffione 		n[t] = netmap_all_rings(na, t);
2103847bf383SLuigi Rizzo 		ntot += n[t];
2104847bf383SLuigi Rizzo 	}
2105ccdc3305SLuigi Rizzo 	/*
2106ccdc3305SLuigi Rizzo 	 * the descriptor is followed inline by an array of offsets
2107ccdc3305SLuigi Rizzo 	 * to the tx and rx rings in the shared memory region.
2108ccdc3305SLuigi Rizzo 	 */
2109ce3ee1e7SLuigi Rizzo 
2110847bf383SLuigi Rizzo 	len = sizeof(struct netmap_if) + (ntot * sizeof(ssize_t));
2111a6d768d8SVincenzo Maffione 	nifp = netmap_if_malloc(nmd, len);
2112ccdc3305SLuigi Rizzo 	if (nifp == NULL) {
2113ccdc3305SLuigi Rizzo 		return NULL;
2114ccdc3305SLuigi Rizzo 	}
2115ccdc3305SLuigi Rizzo 
2116ccdc3305SLuigi Rizzo 	/* initialize base fields -- override const */
2117ce3ee1e7SLuigi Rizzo 	*(u_int *)(uintptr_t)&nifp->ni_tx_rings = na->num_tx_rings;
2118ce3ee1e7SLuigi Rizzo 	*(u_int *)(uintptr_t)&nifp->ni_rx_rings = na->num_rx_rings;
2119d12354a5SVincenzo Maffione 	*(u_int *)(uintptr_t)&nifp->ni_host_tx_rings =
2120d12354a5SVincenzo Maffione 		(na->num_host_tx_rings ? na->num_host_tx_rings : 1);
2121d12354a5SVincenzo Maffione 	*(u_int *)(uintptr_t)&nifp->ni_host_rx_rings =
2122d12354a5SVincenzo Maffione 		(na->num_host_rx_rings ? na->num_host_rx_rings : 1);
2123b6e66be2SVincenzo Maffione 	strlcpy(nifp->ni_name, na->name, sizeof(nifp->ni_name));
2124ccdc3305SLuigi Rizzo 
2125ccdc3305SLuigi Rizzo 	/*
2126ccdc3305SLuigi Rizzo 	 * fill the slots for the rx and tx rings. They contain the offset
2127ccdc3305SLuigi Rizzo 	 * between the ring and nifp, so the information is usable in
2128ccdc3305SLuigi Rizzo 	 * userspace to reach the ring from the nifp.
2129ccdc3305SLuigi Rizzo 	 */
2130a6d768d8SVincenzo Maffione 	base = netmap_if_offset(nmd, nifp);
2131847bf383SLuigi Rizzo 	for (i = 0; i < n[NR_TX]; i++) {
2132c3e9b4dbSLuiz Otavio O Souza 		/* XXX instead of ofs == 0 maybe use the offset of an error
2133c3e9b4dbSLuiz Otavio O Souza 		 * ring, like we do for buffers? */
2134c3e9b4dbSLuiz Otavio O Souza 		ssize_t ofs = 0;
2135c3e9b4dbSLuiz Otavio O Souza 
21362ff91c17SVincenzo Maffione 		if (na->tx_rings[i]->ring != NULL && i >= priv->np_qfirst[NR_TX]
2137c3e9b4dbSLuiz Otavio O Souza 				&& i < priv->np_qlast[NR_TX]) {
2138a6d768d8SVincenzo Maffione 			ofs = netmap_ring_offset(nmd,
21392ff91c17SVincenzo Maffione 						 na->tx_rings[i]->ring) - base;
214037e3a6d3SLuigi Rizzo 		}
2141c3e9b4dbSLuiz Otavio O Souza 		*(ssize_t *)(uintptr_t)&nifp->ring_ofs[i] = ofs;
2142ccdc3305SLuigi Rizzo 	}
2143847bf383SLuigi Rizzo 	for (i = 0; i < n[NR_RX]; i++) {
2144c3e9b4dbSLuiz Otavio O Souza 		/* XXX instead of ofs == 0 maybe use the offset of an error
2145c3e9b4dbSLuiz Otavio O Souza 		 * ring, like we do for buffers? */
2146c3e9b4dbSLuiz Otavio O Souza 		ssize_t ofs = 0;
2147c3e9b4dbSLuiz Otavio O Souza 
21482ff91c17SVincenzo Maffione 		if (na->rx_rings[i]->ring != NULL && i >= priv->np_qfirst[NR_RX]
2149c3e9b4dbSLuiz Otavio O Souza 				&& i < priv->np_qlast[NR_RX]) {
2150a6d768d8SVincenzo Maffione 			ofs = netmap_ring_offset(nmd,
21512ff91c17SVincenzo Maffione 						 na->rx_rings[i]->ring) - base;
215237e3a6d3SLuigi Rizzo 		}
2153c3e9b4dbSLuiz Otavio O Souza 		*(ssize_t *)(uintptr_t)&nifp->ring_ofs[i+n[NR_TX]] = ofs;
2154ccdc3305SLuigi Rizzo 	}
2155ce3ee1e7SLuigi Rizzo 
2156ccdc3305SLuigi Rizzo 	return (nifp);
2157ccdc3305SLuigi Rizzo }
2158ccdc3305SLuigi Rizzo 
2159847bf383SLuigi Rizzo static void
2160a6d768d8SVincenzo Maffione netmap_mem2_if_delete(struct netmap_mem_d *nmd,
2161a6d768d8SVincenzo Maffione 		struct netmap_adapter *na, struct netmap_if *nifp)
2162ccdc3305SLuigi Rizzo {
2163ce3ee1e7SLuigi Rizzo 	if (nifp == NULL)
2164ce3ee1e7SLuigi Rizzo 		/* nothing to do */
2165ce3ee1e7SLuigi Rizzo 		return;
2166f0ea3689SLuigi Rizzo 	if (nifp->ni_bufs_head)
2167f0ea3689SLuigi Rizzo 		netmap_extra_free(na, nifp->ni_bufs_head);
2168a6d768d8SVincenzo Maffione 	netmap_if_free(nmd, nifp);
2169ce3ee1e7SLuigi Rizzo }
2170ce3ee1e7SLuigi Rizzo 
2171ce3ee1e7SLuigi Rizzo static void
2172a6d768d8SVincenzo Maffione netmap_mem2_deref(struct netmap_mem_d *nmd, struct netmap_adapter *na)
2173ce3ee1e7SLuigi Rizzo {
2174ce3ee1e7SLuigi Rizzo 
2175b6e66be2SVincenzo Maffione 	if (netmap_debug & NM_DEBUG_MEM)
2176b6e66be2SVincenzo Maffione 		nm_prinf("active = %d", nmd->active);
2177ce3ee1e7SLuigi Rizzo 
2178ce3ee1e7SLuigi Rizzo }
2179ce3ee1e7SLuigi Rizzo 
2180*893caa2aSMark Johnston const struct netmap_mem_ops netmap_mem_global_ops = {
2181847bf383SLuigi Rizzo 	.nmd_get_lut = netmap_mem2_get_lut,
2182847bf383SLuigi Rizzo 	.nmd_get_info = netmap_mem2_get_info,
2183847bf383SLuigi Rizzo 	.nmd_ofstophys = netmap_mem2_ofstophys,
2184c3e9b4dbSLuiz Otavio O Souza 	.nmd_config = netmap_mem2_config,
2185c3e9b4dbSLuiz Otavio O Souza 	.nmd_finalize = netmap_mem2_finalize,
2186c3e9b4dbSLuiz Otavio O Souza 	.nmd_deref = netmap_mem2_deref,
2187c3e9b4dbSLuiz Otavio O Souza 	.nmd_delete = netmap_mem2_delete,
2188847bf383SLuigi Rizzo 	.nmd_if_offset = netmap_mem2_if_offset,
2189847bf383SLuigi Rizzo 	.nmd_if_new = netmap_mem2_if_new,
2190847bf383SLuigi Rizzo 	.nmd_if_delete = netmap_mem2_if_delete,
2191847bf383SLuigi Rizzo 	.nmd_rings_create = netmap_mem2_rings_create,
2192847bf383SLuigi Rizzo 	.nmd_rings_delete = netmap_mem2_rings_delete
2193847bf383SLuigi Rizzo };
219437e3a6d3SLuigi Rizzo 
2195844a6f0cSLuigi Rizzo int
21962ff91c17SVincenzo Maffione netmap_mem_pools_info_get(struct nmreq_pools_info *req,
21972ff91c17SVincenzo Maffione 				struct netmap_mem_d *nmd)
2198844a6f0cSLuigi Rizzo {
2199844a6f0cSLuigi Rizzo 	int ret;
2200844a6f0cSLuigi Rizzo 
22012ff91c17SVincenzo Maffione 	ret = netmap_mem_get_info(nmd, &req->nr_memsize, NULL,
22022ff91c17SVincenzo Maffione 					&req->nr_mem_id);
2203844a6f0cSLuigi Rizzo 	if (ret) {
2204844a6f0cSLuigi Rizzo 		return ret;
2205844a6f0cSLuigi Rizzo 	}
2206844a6f0cSLuigi Rizzo 
2207c3e9b4dbSLuiz Otavio O Souza 	NMA_LOCK(nmd);
22082ff91c17SVincenzo Maffione 	req->nr_if_pool_offset = 0;
22092ff91c17SVincenzo Maffione 	req->nr_if_pool_objtotal = nmd->pools[NETMAP_IF_POOL].objtotal;
22102ff91c17SVincenzo Maffione 	req->nr_if_pool_objsize = nmd->pools[NETMAP_IF_POOL]._objsize;
2211844a6f0cSLuigi Rizzo 
22122ff91c17SVincenzo Maffione 	req->nr_ring_pool_offset = nmd->pools[NETMAP_IF_POOL].memtotal;
22132ff91c17SVincenzo Maffione 	req->nr_ring_pool_objtotal = nmd->pools[NETMAP_RING_POOL].objtotal;
22142ff91c17SVincenzo Maffione 	req->nr_ring_pool_objsize = nmd->pools[NETMAP_RING_POOL]._objsize;
2215844a6f0cSLuigi Rizzo 
22162ff91c17SVincenzo Maffione 	req->nr_buf_pool_offset = nmd->pools[NETMAP_IF_POOL].memtotal +
2217844a6f0cSLuigi Rizzo 			     nmd->pools[NETMAP_RING_POOL].memtotal;
22182ff91c17SVincenzo Maffione 	req->nr_buf_pool_objtotal = nmd->pools[NETMAP_BUF_POOL].objtotal;
22192ff91c17SVincenzo Maffione 	req->nr_buf_pool_objsize = nmd->pools[NETMAP_BUF_POOL]._objsize;
2220c3e9b4dbSLuiz Otavio O Souza 	NMA_UNLOCK(nmd);
2221844a6f0cSLuigi Rizzo 
2222844a6f0cSLuigi Rizzo 	return 0;
2223844a6f0cSLuigi Rizzo }
2224844a6f0cSLuigi Rizzo 
22254f80b14cSVincenzo Maffione #ifdef WITH_EXTMEM
22264f80b14cSVincenzo Maffione struct netmap_mem_ext {
22274f80b14cSVincenzo Maffione 	struct netmap_mem_d up;
22284f80b14cSVincenzo Maffione 
22292ff91c17SVincenzo Maffione 	struct nm_os_extmem *os;
22304f80b14cSVincenzo Maffione 	struct netmap_mem_ext *next, *prev;
22314f80b14cSVincenzo Maffione };
22324f80b14cSVincenzo Maffione 
22334f80b14cSVincenzo Maffione /* call with nm_mem_list_lock held */
22344f80b14cSVincenzo Maffione static void
22354f80b14cSVincenzo Maffione netmap_mem_ext_register(struct netmap_mem_ext *e)
22364f80b14cSVincenzo Maffione {
22374f80b14cSVincenzo Maffione 	NM_MTX_LOCK(nm_mem_ext_list_lock);
22384f80b14cSVincenzo Maffione 	if (netmap_mem_ext_list)
22394f80b14cSVincenzo Maffione 		netmap_mem_ext_list->prev = e;
22404f80b14cSVincenzo Maffione 	e->next = netmap_mem_ext_list;
22414f80b14cSVincenzo Maffione 	netmap_mem_ext_list = e;
22424f80b14cSVincenzo Maffione 	e->prev = NULL;
22434f80b14cSVincenzo Maffione 	NM_MTX_UNLOCK(nm_mem_ext_list_lock);
22444f80b14cSVincenzo Maffione }
22454f80b14cSVincenzo Maffione 
22464f80b14cSVincenzo Maffione /* call with nm_mem_list_lock held */
22474f80b14cSVincenzo Maffione static void
22484f80b14cSVincenzo Maffione netmap_mem_ext_unregister(struct netmap_mem_ext *e)
22494f80b14cSVincenzo Maffione {
22504f80b14cSVincenzo Maffione 	if (e->prev)
22514f80b14cSVincenzo Maffione 		e->prev->next = e->next;
22524f80b14cSVincenzo Maffione 	else
22534f80b14cSVincenzo Maffione 		netmap_mem_ext_list = e->next;
22544f80b14cSVincenzo Maffione 	if (e->next)
22554f80b14cSVincenzo Maffione 		e->next->prev = e->prev;
22564f80b14cSVincenzo Maffione 	e->prev = e->next = NULL;
22574f80b14cSVincenzo Maffione }
22584f80b14cSVincenzo Maffione 
22594f80b14cSVincenzo Maffione static struct netmap_mem_ext *
22602ff91c17SVincenzo Maffione netmap_mem_ext_search(struct nm_os_extmem *os)
22614f80b14cSVincenzo Maffione {
22624f80b14cSVincenzo Maffione 	struct netmap_mem_ext *e;
22634f80b14cSVincenzo Maffione 
22644f80b14cSVincenzo Maffione 	NM_MTX_LOCK(nm_mem_ext_list_lock);
22654f80b14cSVincenzo Maffione 	for (e = netmap_mem_ext_list; e; e = e->next) {
22662ff91c17SVincenzo Maffione 		if (nm_os_extmem_isequal(e->os, os)) {
22674f80b14cSVincenzo Maffione 			netmap_mem_get(&e->up);
22684f80b14cSVincenzo Maffione 			break;
22694f80b14cSVincenzo Maffione 		}
22704f80b14cSVincenzo Maffione 	}
22714f80b14cSVincenzo Maffione 	NM_MTX_UNLOCK(nm_mem_ext_list_lock);
22724f80b14cSVincenzo Maffione 	return e;
22734f80b14cSVincenzo Maffione }
22744f80b14cSVincenzo Maffione 
22754f80b14cSVincenzo Maffione 
22764f80b14cSVincenzo Maffione static void
22774f80b14cSVincenzo Maffione netmap_mem_ext_delete(struct netmap_mem_d *d)
22784f80b14cSVincenzo Maffione {
22794f80b14cSVincenzo Maffione 	int i;
22804f80b14cSVincenzo Maffione 	struct netmap_mem_ext *e =
22814f80b14cSVincenzo Maffione 		(struct netmap_mem_ext *)d;
22824f80b14cSVincenzo Maffione 
22834f80b14cSVincenzo Maffione 	netmap_mem_ext_unregister(e);
22844f80b14cSVincenzo Maffione 
22854f80b14cSVincenzo Maffione 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
22864f80b14cSVincenzo Maffione 		struct netmap_obj_pool *p = &d->pools[i];
22874f80b14cSVincenzo Maffione 
22884f80b14cSVincenzo Maffione 		if (p->lut) {
22894f80b14cSVincenzo Maffione 			nm_free_lut(p->lut, p->objtotal);
22904f80b14cSVincenzo Maffione 			p->lut = NULL;
22914f80b14cSVincenzo Maffione 		}
22924f80b14cSVincenzo Maffione 	}
22932ff91c17SVincenzo Maffione 	if (e->os)
22942ff91c17SVincenzo Maffione 		nm_os_extmem_delete(e->os);
22954f80b14cSVincenzo Maffione 	netmap_mem2_delete(d);
22964f80b14cSVincenzo Maffione }
22974f80b14cSVincenzo Maffione 
22984f80b14cSVincenzo Maffione static int
22994f80b14cSVincenzo Maffione netmap_mem_ext_config(struct netmap_mem_d *nmd)
23004f80b14cSVincenzo Maffione {
23014f80b14cSVincenzo Maffione 	return 0;
23024f80b14cSVincenzo Maffione }
23034f80b14cSVincenzo Maffione 
23044f80b14cSVincenzo Maffione struct netmap_mem_ops netmap_mem_ext_ops = {
23054f80b14cSVincenzo Maffione 	.nmd_get_lut = netmap_mem2_get_lut,
23064f80b14cSVincenzo Maffione 	.nmd_get_info = netmap_mem2_get_info,
23074f80b14cSVincenzo Maffione 	.nmd_ofstophys = netmap_mem2_ofstophys,
23084f80b14cSVincenzo Maffione 	.nmd_config = netmap_mem_ext_config,
23094f80b14cSVincenzo Maffione 	.nmd_finalize = netmap_mem2_finalize,
23104f80b14cSVincenzo Maffione 	.nmd_deref = netmap_mem2_deref,
23114f80b14cSVincenzo Maffione 	.nmd_delete = netmap_mem_ext_delete,
23124f80b14cSVincenzo Maffione 	.nmd_if_offset = netmap_mem2_if_offset,
23134f80b14cSVincenzo Maffione 	.nmd_if_new = netmap_mem2_if_new,
23144f80b14cSVincenzo Maffione 	.nmd_if_delete = netmap_mem2_if_delete,
23154f80b14cSVincenzo Maffione 	.nmd_rings_create = netmap_mem2_rings_create,
23164f80b14cSVincenzo Maffione 	.nmd_rings_delete = netmap_mem2_rings_delete
23174f80b14cSVincenzo Maffione };
23184f80b14cSVincenzo Maffione 
23194f80b14cSVincenzo Maffione struct netmap_mem_d *
23202ff91c17SVincenzo Maffione netmap_mem_ext_create(uint64_t usrptr, struct nmreq_pools_info *pi, int *perror)
23214f80b14cSVincenzo Maffione {
23224f80b14cSVincenzo Maffione 	int error = 0;
23232ff91c17SVincenzo Maffione 	int i, j;
23244f80b14cSVincenzo Maffione 	struct netmap_mem_ext *nme;
23254f80b14cSVincenzo Maffione 	char *clust;
23264f80b14cSVincenzo Maffione 	size_t off;
23272ff91c17SVincenzo Maffione 	struct nm_os_extmem *os = NULL;
23282ff91c17SVincenzo Maffione 	int nr_pages;
23294f80b14cSVincenzo Maffione 
23304f80b14cSVincenzo Maffione 	// XXX sanity checks
23312ff91c17SVincenzo Maffione 	if (pi->nr_if_pool_objtotal == 0)
23322ff91c17SVincenzo Maffione 		pi->nr_if_pool_objtotal = netmap_min_priv_params[NETMAP_IF_POOL].num;
23332ff91c17SVincenzo Maffione 	if (pi->nr_if_pool_objsize == 0)
23342ff91c17SVincenzo Maffione 		pi->nr_if_pool_objsize = netmap_min_priv_params[NETMAP_IF_POOL].size;
23352ff91c17SVincenzo Maffione 	if (pi->nr_ring_pool_objtotal == 0)
23362ff91c17SVincenzo Maffione 		pi->nr_ring_pool_objtotal = netmap_min_priv_params[NETMAP_RING_POOL].num;
23372ff91c17SVincenzo Maffione 	if (pi->nr_ring_pool_objsize == 0)
23382ff91c17SVincenzo Maffione 		pi->nr_ring_pool_objsize = netmap_min_priv_params[NETMAP_RING_POOL].size;
23392ff91c17SVincenzo Maffione 	if (pi->nr_buf_pool_objtotal == 0)
23402ff91c17SVincenzo Maffione 		pi->nr_buf_pool_objtotal = netmap_min_priv_params[NETMAP_BUF_POOL].num;
23412ff91c17SVincenzo Maffione 	if (pi->nr_buf_pool_objsize == 0)
23422ff91c17SVincenzo Maffione 		pi->nr_buf_pool_objsize = netmap_min_priv_params[NETMAP_BUF_POOL].size;
2343b6e66be2SVincenzo Maffione 	if (netmap_verbose & NM_DEBUG_MEM)
2344b6e66be2SVincenzo Maffione 		nm_prinf("if %d %d ring %d %d buf %d %d",
23452ff91c17SVincenzo Maffione 			pi->nr_if_pool_objtotal, pi->nr_if_pool_objsize,
23462ff91c17SVincenzo Maffione 			pi->nr_ring_pool_objtotal, pi->nr_ring_pool_objsize,
23472ff91c17SVincenzo Maffione 			pi->nr_buf_pool_objtotal, pi->nr_buf_pool_objsize);
23484f80b14cSVincenzo Maffione 
23492ff91c17SVincenzo Maffione 	os = nm_os_extmem_create(usrptr, pi, &error);
23502ff91c17SVincenzo Maffione 	if (os == NULL) {
2351b6e66be2SVincenzo Maffione 		nm_prerr("os extmem creation failed");
23524f80b14cSVincenzo Maffione 		goto out;
23534f80b14cSVincenzo Maffione 	}
23544f80b14cSVincenzo Maffione 
23552ff91c17SVincenzo Maffione 	nme = netmap_mem_ext_search(os);
23564f80b14cSVincenzo Maffione 	if (nme) {
23572ff91c17SVincenzo Maffione 		nm_os_extmem_delete(os);
23584f80b14cSVincenzo Maffione 		return &nme->up;
23594f80b14cSVincenzo Maffione 	}
2360b6e66be2SVincenzo Maffione 	if (netmap_verbose & NM_DEBUG_MEM)
2361b6e66be2SVincenzo Maffione 		nm_prinf("not found, creating new");
23624f80b14cSVincenzo Maffione 
23634f80b14cSVincenzo Maffione 	nme = _netmap_mem_private_new(sizeof(*nme),
2364a6d768d8SVincenzo Maffione 
23654f80b14cSVincenzo Maffione 			(struct netmap_obj_params[]){
23662ff91c17SVincenzo Maffione 				{ pi->nr_if_pool_objsize, pi->nr_if_pool_objtotal },
23672ff91c17SVincenzo Maffione 				{ pi->nr_ring_pool_objsize, pi->nr_ring_pool_objtotal },
23682ff91c17SVincenzo Maffione 				{ pi->nr_buf_pool_objsize, pi->nr_buf_pool_objtotal }},
2369a6d768d8SVincenzo Maffione 			-1,
23704f80b14cSVincenzo Maffione 			&netmap_mem_ext_ops,
2371a6d768d8SVincenzo Maffione 			pi->nr_memsize,
23724f80b14cSVincenzo Maffione 			&error);
23734f80b14cSVincenzo Maffione 	if (nme == NULL)
23744f80b14cSVincenzo Maffione 		goto out_unmap;
23754f80b14cSVincenzo Maffione 
23762ff91c17SVincenzo Maffione 	nr_pages = nm_os_extmem_nr_pages(os);
23772ff91c17SVincenzo Maffione 
23784f80b14cSVincenzo Maffione 	/* from now on pages will be released by nme destructor;
23794f80b14cSVincenzo Maffione 	 * we let res = 0 to prevent release in out_unmap below
23804f80b14cSVincenzo Maffione 	 */
23812ff91c17SVincenzo Maffione 	nme->os = os;
23822ff91c17SVincenzo Maffione 	os = NULL; /* pass ownership */
23834f80b14cSVincenzo Maffione 
23842ff91c17SVincenzo Maffione 	clust = nm_os_extmem_nextpage(nme->os);
23854f80b14cSVincenzo Maffione 	off = 0;
23864f80b14cSVincenzo Maffione 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
23874f80b14cSVincenzo Maffione 		struct netmap_obj_pool *p = &nme->up.pools[i];
23884f80b14cSVincenzo Maffione 		struct netmap_obj_params *o = &nme->up.params[i];
23894f80b14cSVincenzo Maffione 
23904f80b14cSVincenzo Maffione 		p->_objsize = o->size;
23914f80b14cSVincenzo Maffione 		p->_clustsize = o->size;
23924f80b14cSVincenzo Maffione 		p->_clustentries = 1;
23934f80b14cSVincenzo Maffione 
23944f80b14cSVincenzo Maffione 		p->lut = nm_alloc_lut(o->num);
23954f80b14cSVincenzo Maffione 		if (p->lut == NULL) {
23964f80b14cSVincenzo Maffione 			error = ENOMEM;
23974f80b14cSVincenzo Maffione 			goto out_delete;
23984f80b14cSVincenzo Maffione 		}
23994f80b14cSVincenzo Maffione 
24004f80b14cSVincenzo Maffione 		p->bitmap_slots = (o->num + sizeof(uint32_t) - 1) / sizeof(uint32_t);
24014f80b14cSVincenzo Maffione 		p->invalid_bitmap = nm_os_malloc(sizeof(uint32_t) * p->bitmap_slots);
24024f80b14cSVincenzo Maffione 		if (p->invalid_bitmap == NULL) {
24034f80b14cSVincenzo Maffione 			error = ENOMEM;
24044f80b14cSVincenzo Maffione 			goto out_delete;
24054f80b14cSVincenzo Maffione 		}
24064f80b14cSVincenzo Maffione 
24074f80b14cSVincenzo Maffione 		if (nr_pages == 0) {
24084f80b14cSVincenzo Maffione 			p->objtotal = 0;
24094f80b14cSVincenzo Maffione 			p->memtotal = 0;
24104f80b14cSVincenzo Maffione 			p->objfree = 0;
24114f80b14cSVincenzo Maffione 			continue;
24124f80b14cSVincenzo Maffione 		}
24134f80b14cSVincenzo Maffione 
24144f80b14cSVincenzo Maffione 		for (j = 0; j < o->num && nr_pages > 0; j++) {
24154f80b14cSVincenzo Maffione 			size_t noff;
24164f80b14cSVincenzo Maffione 
24174f80b14cSVincenzo Maffione 			p->lut[j].vaddr = clust + off;
24182ff91c17SVincenzo Maffione #if !defined(linux) && !defined(_WIN32)
24192ff91c17SVincenzo Maffione 			p->lut[j].paddr = vtophys(p->lut[j].vaddr);
24202ff91c17SVincenzo Maffione #endif
242175f4f3edSVincenzo Maffione 			nm_prdis("%s %d at %p", p->name, j, p->lut[j].vaddr);
24224f80b14cSVincenzo Maffione 			noff = off + p->_objsize;
24234f80b14cSVincenzo Maffione 			if (noff < PAGE_SIZE) {
24244f80b14cSVincenzo Maffione 				off = noff;
24254f80b14cSVincenzo Maffione 				continue;
24264f80b14cSVincenzo Maffione 			}
242775f4f3edSVincenzo Maffione 			nm_prdis("too big, recomputing offset...");
24284f80b14cSVincenzo Maffione 			while (noff >= PAGE_SIZE) {
24292ff91c17SVincenzo Maffione 				char *old_clust = clust;
24302ff91c17SVincenzo Maffione 				noff -= PAGE_SIZE;
24312ff91c17SVincenzo Maffione 				clust = nm_os_extmem_nextpage(nme->os);
24324f80b14cSVincenzo Maffione 				nr_pages--;
243375f4f3edSVincenzo Maffione 				nm_prdis("noff %zu page %p nr_pages %d", noff,
24344f80b14cSVincenzo Maffione 						page_to_virt(*pages), nr_pages);
24354f80b14cSVincenzo Maffione 				if (noff > 0 && !nm_isset(p->invalid_bitmap, j) &&
24362ff91c17SVincenzo Maffione 					(nr_pages == 0 ||
24372ff91c17SVincenzo Maffione 					 old_clust + PAGE_SIZE != clust))
24384f80b14cSVincenzo Maffione 				{
24394f80b14cSVincenzo Maffione 					/* out of space or non contiguous,
24404f80b14cSVincenzo Maffione 					 * drop this object
24414f80b14cSVincenzo Maffione 					 * */
24424f80b14cSVincenzo Maffione 					p->invalid_bitmap[ (j>>5) ] |= 1U << (j & 31U);
244375f4f3edSVincenzo Maffione 					nm_prdis("non contiguous at off %zu, drop", noff);
24444f80b14cSVincenzo Maffione 				}
24454f80b14cSVincenzo Maffione 				if (nr_pages == 0)
24464f80b14cSVincenzo Maffione 					break;
24474f80b14cSVincenzo Maffione 			}
24484f80b14cSVincenzo Maffione 			off = noff;
24494f80b14cSVincenzo Maffione 		}
24504f80b14cSVincenzo Maffione 		p->objtotal = j;
24514f80b14cSVincenzo Maffione 		p->numclusters = p->objtotal;
2452253b2ec1SVincenzo Maffione 		p->memtotal = j * (size_t)p->_objsize;
2453253b2ec1SVincenzo Maffione 		nm_prdis("%d memtotal %zu", j, p->memtotal);
24544f80b14cSVincenzo Maffione 	}
24554f80b14cSVincenzo Maffione 
24564f80b14cSVincenzo Maffione 	netmap_mem_ext_register(nme);
24574f80b14cSVincenzo Maffione 
24584f80b14cSVincenzo Maffione 	return &nme->up;
24594f80b14cSVincenzo Maffione 
24604f80b14cSVincenzo Maffione out_delete:
24614f80b14cSVincenzo Maffione 	netmap_mem_put(&nme->up);
24624f80b14cSVincenzo Maffione out_unmap:
24632ff91c17SVincenzo Maffione 	if (os)
24642ff91c17SVincenzo Maffione 		nm_os_extmem_delete(os);
24654f80b14cSVincenzo Maffione out:
24664f80b14cSVincenzo Maffione 	if (perror)
24674f80b14cSVincenzo Maffione 		*perror = error;
24684f80b14cSVincenzo Maffione 	return NULL;
24694f80b14cSVincenzo Maffione 
24704f80b14cSVincenzo Maffione }
24714f80b14cSVincenzo Maffione #endif /* WITH_EXTMEM */
24724f80b14cSVincenzo Maffione 
24734f80b14cSVincenzo Maffione 
2474b6e66be2SVincenzo Maffione #ifdef WITH_PTNETMAP
247537e3a6d3SLuigi Rizzo struct mem_pt_if {
247637e3a6d3SLuigi Rizzo 	struct mem_pt_if *next;
2477e330262fSJustin Hibbits 	if_t ifp;
247837e3a6d3SLuigi Rizzo 	unsigned int nifp_offset;
247937e3a6d3SLuigi Rizzo };
248037e3a6d3SLuigi Rizzo 
248137e3a6d3SLuigi Rizzo /* Netmap allocator for ptnetmap guests. */
248237e3a6d3SLuigi Rizzo struct netmap_mem_ptg {
248337e3a6d3SLuigi Rizzo 	struct netmap_mem_d up;
248437e3a6d3SLuigi Rizzo 
248537e3a6d3SLuigi Rizzo 	vm_paddr_t nm_paddr;            /* physical address in the guest */
248637e3a6d3SLuigi Rizzo 	void *nm_addr;                  /* virtual address in the guest */
248737e3a6d3SLuigi Rizzo 	struct netmap_lut buf_lut;      /* lookup table for BUF pool in the guest */
2488844a6f0cSLuigi Rizzo 	nm_memid_t host_mem_id;         /* allocator identifier in the host */
2489844a6f0cSLuigi Rizzo 	struct ptnetmap_memdev *ptn_dev;/* ptnetmap memdev */
249037e3a6d3SLuigi Rizzo 	struct mem_pt_if *pt_ifs;	/* list of interfaces in passthrough */
249137e3a6d3SLuigi Rizzo };
249237e3a6d3SLuigi Rizzo 
249337e3a6d3SLuigi Rizzo /* Link a passthrough interface to a passthrough netmap allocator. */
249437e3a6d3SLuigi Rizzo static int
2495e330262fSJustin Hibbits netmap_mem_pt_guest_ifp_add(struct netmap_mem_d *nmd, if_t ifp,
2496844a6f0cSLuigi Rizzo 			    unsigned int nifp_offset)
249737e3a6d3SLuigi Rizzo {
249837e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2499c3e9b4dbSLuiz Otavio O Souza 	struct mem_pt_if *ptif = nm_os_malloc(sizeof(*ptif));
250037e3a6d3SLuigi Rizzo 
250137e3a6d3SLuigi Rizzo 	if (!ptif) {
250237e3a6d3SLuigi Rizzo 		return ENOMEM;
250337e3a6d3SLuigi Rizzo 	}
250437e3a6d3SLuigi Rizzo 
250537e3a6d3SLuigi Rizzo 	NMA_LOCK(nmd);
250637e3a6d3SLuigi Rizzo 
250737e3a6d3SLuigi Rizzo 	ptif->ifp = ifp;
250837e3a6d3SLuigi Rizzo 	ptif->nifp_offset = nifp_offset;
250937e3a6d3SLuigi Rizzo 
251037e3a6d3SLuigi Rizzo 	if (ptnmd->pt_ifs) {
251137e3a6d3SLuigi Rizzo 		ptif->next = ptnmd->pt_ifs;
251237e3a6d3SLuigi Rizzo 	}
251337e3a6d3SLuigi Rizzo 	ptnmd->pt_ifs = ptif;
251437e3a6d3SLuigi Rizzo 
251537e3a6d3SLuigi Rizzo 	NMA_UNLOCK(nmd);
251637e3a6d3SLuigi Rizzo 
2517b6e66be2SVincenzo Maffione 	nm_prinf("ifp=%s,nifp_offset=%u",
2518e330262fSJustin Hibbits 		if_name(ptif->ifp), ptif->nifp_offset);
251937e3a6d3SLuigi Rizzo 
252037e3a6d3SLuigi Rizzo 	return 0;
252137e3a6d3SLuigi Rizzo }
252237e3a6d3SLuigi Rizzo 
252337e3a6d3SLuigi Rizzo /* Called with NMA_LOCK(nmd) held. */
252437e3a6d3SLuigi Rizzo static struct mem_pt_if *
2525e330262fSJustin Hibbits netmap_mem_pt_guest_ifp_lookup(struct netmap_mem_d *nmd, if_t ifp)
252637e3a6d3SLuigi Rizzo {
252737e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
252837e3a6d3SLuigi Rizzo 	struct mem_pt_if *curr;
252937e3a6d3SLuigi Rizzo 
253037e3a6d3SLuigi Rizzo 	for (curr = ptnmd->pt_ifs; curr; curr = curr->next) {
253137e3a6d3SLuigi Rizzo 		if (curr->ifp == ifp) {
253237e3a6d3SLuigi Rizzo 			return curr;
253337e3a6d3SLuigi Rizzo 		}
253437e3a6d3SLuigi Rizzo 	}
253537e3a6d3SLuigi Rizzo 
253637e3a6d3SLuigi Rizzo 	return NULL;
253737e3a6d3SLuigi Rizzo }
253837e3a6d3SLuigi Rizzo 
253937e3a6d3SLuigi Rizzo /* Unlink a passthrough interface from a passthrough netmap allocator. */
254037e3a6d3SLuigi Rizzo int
2541e330262fSJustin Hibbits netmap_mem_pt_guest_ifp_del(struct netmap_mem_d *nmd, if_t ifp)
254237e3a6d3SLuigi Rizzo {
254337e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
254437e3a6d3SLuigi Rizzo 	struct mem_pt_if *prev = NULL;
254537e3a6d3SLuigi Rizzo 	struct mem_pt_if *curr;
254637e3a6d3SLuigi Rizzo 	int ret = -1;
254737e3a6d3SLuigi Rizzo 
254837e3a6d3SLuigi Rizzo 	NMA_LOCK(nmd);
254937e3a6d3SLuigi Rizzo 
255037e3a6d3SLuigi Rizzo 	for (curr = ptnmd->pt_ifs; curr; curr = curr->next) {
255137e3a6d3SLuigi Rizzo 		if (curr->ifp == ifp) {
255237e3a6d3SLuigi Rizzo 			if (prev) {
255337e3a6d3SLuigi Rizzo 				prev->next = curr->next;
255437e3a6d3SLuigi Rizzo 			} else {
255537e3a6d3SLuigi Rizzo 				ptnmd->pt_ifs = curr->next;
255637e3a6d3SLuigi Rizzo 			}
2557760fa2abSVincenzo Maffione 			nm_prinf("removed (ifp=%s,nifp_offset=%u)",
2558e330262fSJustin Hibbits 			  if_name(curr->ifp), curr->nifp_offset);
2559c3e9b4dbSLuiz Otavio O Souza 			nm_os_free(curr);
256037e3a6d3SLuigi Rizzo 			ret = 0;
256137e3a6d3SLuigi Rizzo 			break;
256237e3a6d3SLuigi Rizzo 		}
256337e3a6d3SLuigi Rizzo 		prev = curr;
256437e3a6d3SLuigi Rizzo 	}
256537e3a6d3SLuigi Rizzo 
256637e3a6d3SLuigi Rizzo 	NMA_UNLOCK(nmd);
256737e3a6d3SLuigi Rizzo 
256837e3a6d3SLuigi Rizzo 	return ret;
256937e3a6d3SLuigi Rizzo }
257037e3a6d3SLuigi Rizzo 
257137e3a6d3SLuigi Rizzo static int
257237e3a6d3SLuigi Rizzo netmap_mem_pt_guest_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut)
257337e3a6d3SLuigi Rizzo {
257437e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
257537e3a6d3SLuigi Rizzo 
257637e3a6d3SLuigi Rizzo 	if (!(nmd->flags & NETMAP_MEM_FINALIZED)) {
257737e3a6d3SLuigi Rizzo 		return EINVAL;
257837e3a6d3SLuigi Rizzo 	}
257937e3a6d3SLuigi Rizzo 
258037e3a6d3SLuigi Rizzo 	*lut = ptnmd->buf_lut;
258137e3a6d3SLuigi Rizzo 	return 0;
258237e3a6d3SLuigi Rizzo }
258337e3a6d3SLuigi Rizzo 
258437e3a6d3SLuigi Rizzo static int
25854f80b14cSVincenzo Maffione netmap_mem_pt_guest_get_info(struct netmap_mem_d *nmd, uint64_t *size,
258637e3a6d3SLuigi Rizzo 			     u_int *memflags, uint16_t *id)
258737e3a6d3SLuigi Rizzo {
258837e3a6d3SLuigi Rizzo 	int error = 0;
258937e3a6d3SLuigi Rizzo 
259037e3a6d3SLuigi Rizzo 	error = nmd->ops->nmd_config(nmd);
259137e3a6d3SLuigi Rizzo 	if (error)
259237e3a6d3SLuigi Rizzo 		goto out;
259337e3a6d3SLuigi Rizzo 
259437e3a6d3SLuigi Rizzo 	if (size)
259537e3a6d3SLuigi Rizzo 		*size = nmd->nm_totalsize;
259637e3a6d3SLuigi Rizzo 	if (memflags)
259737e3a6d3SLuigi Rizzo 		*memflags = nmd->flags;
259837e3a6d3SLuigi Rizzo 	if (id)
259937e3a6d3SLuigi Rizzo 		*id = nmd->nm_id;
260037e3a6d3SLuigi Rizzo 
260137e3a6d3SLuigi Rizzo out:
260237e3a6d3SLuigi Rizzo 
260337e3a6d3SLuigi Rizzo 	return error;
260437e3a6d3SLuigi Rizzo }
260537e3a6d3SLuigi Rizzo 
260637e3a6d3SLuigi Rizzo static vm_paddr_t
260737e3a6d3SLuigi Rizzo netmap_mem_pt_guest_ofstophys(struct netmap_mem_d *nmd, vm_ooffset_t off)
260837e3a6d3SLuigi Rizzo {
260937e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
261037e3a6d3SLuigi Rizzo 	vm_paddr_t paddr;
261137e3a6d3SLuigi Rizzo 	/* if the offset is valid, just return csb->base_addr + off */
261237e3a6d3SLuigi Rizzo 	paddr = (vm_paddr_t)(ptnmd->nm_paddr + off);
261375f4f3edSVincenzo Maffione 	nm_prdis("off %lx padr %lx", off, (unsigned long)paddr);
261437e3a6d3SLuigi Rizzo 	return paddr;
261537e3a6d3SLuigi Rizzo }
261637e3a6d3SLuigi Rizzo 
261737e3a6d3SLuigi Rizzo static int
261837e3a6d3SLuigi Rizzo netmap_mem_pt_guest_config(struct netmap_mem_d *nmd)
261937e3a6d3SLuigi Rizzo {
262037e3a6d3SLuigi Rizzo 	/* nothing to do, we are configured on creation
262137e3a6d3SLuigi Rizzo 	 * and configuration never changes thereafter
262237e3a6d3SLuigi Rizzo 	 */
262337e3a6d3SLuigi Rizzo 	return 0;
262437e3a6d3SLuigi Rizzo }
262537e3a6d3SLuigi Rizzo 
262637e3a6d3SLuigi Rizzo static int
2627a6d768d8SVincenzo Maffione netmap_mem_pt_guest_finalize(struct netmap_mem_d *nmd, struct netmap_adapter *na)
262837e3a6d3SLuigi Rizzo {
262937e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2630844a6f0cSLuigi Rizzo 	uint64_t mem_size;
2631844a6f0cSLuigi Rizzo 	uint32_t bufsize;
2632844a6f0cSLuigi Rizzo 	uint32_t nbuffers;
2633844a6f0cSLuigi Rizzo 	uint32_t poolofs;
2634844a6f0cSLuigi Rizzo 	vm_paddr_t paddr;
2635844a6f0cSLuigi Rizzo 	char *vaddr;
2636844a6f0cSLuigi Rizzo 	int i;
263737e3a6d3SLuigi Rizzo 	int error = 0;
263837e3a6d3SLuigi Rizzo 
263937e3a6d3SLuigi Rizzo 	if (nmd->flags & NETMAP_MEM_FINALIZED)
264037e3a6d3SLuigi Rizzo 		goto out;
264137e3a6d3SLuigi Rizzo 
264237e3a6d3SLuigi Rizzo 	if (ptnmd->ptn_dev == NULL) {
264375f4f3edSVincenzo Maffione 		nm_prerr("ptnetmap memdev not attached");
264437e3a6d3SLuigi Rizzo 		error = ENOMEM;
26452ff91c17SVincenzo Maffione 		goto out;
264637e3a6d3SLuigi Rizzo 	}
2647844a6f0cSLuigi Rizzo 	/* Map memory through ptnetmap-memdev BAR. */
264837e3a6d3SLuigi Rizzo 	error = nm_os_pt_memdev_iomap(ptnmd->ptn_dev, &ptnmd->nm_paddr,
2649844a6f0cSLuigi Rizzo 				      &ptnmd->nm_addr, &mem_size);
265037e3a6d3SLuigi Rizzo 	if (error)
26512ff91c17SVincenzo Maffione 		goto out;
265237e3a6d3SLuigi Rizzo 
2653844a6f0cSLuigi Rizzo 	/* Initialize the lut using the information contained in the
2654844a6f0cSLuigi Rizzo 	 * ptnetmap memory device. */
2655844a6f0cSLuigi Rizzo 	bufsize = nm_os_pt_memdev_ioread(ptnmd->ptn_dev,
2656844a6f0cSLuigi Rizzo 					 PTNET_MDEV_IO_BUF_POOL_OBJSZ);
2657844a6f0cSLuigi Rizzo 	nbuffers = nm_os_pt_memdev_ioread(ptnmd->ptn_dev,
2658844a6f0cSLuigi Rizzo 					 PTNET_MDEV_IO_BUF_POOL_OBJNUM);
2659844a6f0cSLuigi Rizzo 
2660844a6f0cSLuigi Rizzo 	/* allocate the lut */
2661844a6f0cSLuigi Rizzo 	if (ptnmd->buf_lut.lut == NULL) {
266275f4f3edSVincenzo Maffione 		nm_prinf("allocating lut");
2663844a6f0cSLuigi Rizzo 		ptnmd->buf_lut.lut = nm_alloc_lut(nbuffers);
2664844a6f0cSLuigi Rizzo 		if (ptnmd->buf_lut.lut == NULL) {
266575f4f3edSVincenzo Maffione 			nm_prerr("lut allocation failed");
2666844a6f0cSLuigi Rizzo 			return ENOMEM;
2667844a6f0cSLuigi Rizzo 		}
2668844a6f0cSLuigi Rizzo 	}
2669844a6f0cSLuigi Rizzo 
2670844a6f0cSLuigi Rizzo 	/* we have physically contiguous memory mapped through PCI BAR */
2671844a6f0cSLuigi Rizzo 	poolofs = nm_os_pt_memdev_ioread(ptnmd->ptn_dev,
2672844a6f0cSLuigi Rizzo 					 PTNET_MDEV_IO_BUF_POOL_OFS);
2673844a6f0cSLuigi Rizzo 	vaddr = (char *)(ptnmd->nm_addr) + poolofs;
2674844a6f0cSLuigi Rizzo 	paddr = ptnmd->nm_paddr + poolofs;
2675844a6f0cSLuigi Rizzo 
2676844a6f0cSLuigi Rizzo 	for (i = 0; i < nbuffers; i++) {
2677844a6f0cSLuigi Rizzo 		ptnmd->buf_lut.lut[i].vaddr = vaddr;
2678844a6f0cSLuigi Rizzo 		vaddr += bufsize;
2679844a6f0cSLuigi Rizzo 		paddr += bufsize;
2680844a6f0cSLuigi Rizzo 	}
2681844a6f0cSLuigi Rizzo 
2682844a6f0cSLuigi Rizzo 	ptnmd->buf_lut.objtotal = nbuffers;
2683844a6f0cSLuigi Rizzo 	ptnmd->buf_lut.objsize = bufsize;
2684253b2ec1SVincenzo Maffione 	nmd->nm_totalsize = mem_size;
268537e3a6d3SLuigi Rizzo 
26862ff91c17SVincenzo Maffione 	/* Initialize these fields as are needed by
26872ff91c17SVincenzo Maffione 	 * netmap_mem_bufsize().
26882ff91c17SVincenzo Maffione 	 * XXX please improve this, why do we need this
26892ff91c17SVincenzo Maffione 	 * replication? maybe we nmd->pools[] should no be
26902ff91c17SVincenzo Maffione 	 * there for the guest allocator? */
26912ff91c17SVincenzo Maffione 	nmd->pools[NETMAP_BUF_POOL]._objsize = bufsize;
26922ff91c17SVincenzo Maffione 	nmd->pools[NETMAP_BUF_POOL]._objtotal = nbuffers;
26932ff91c17SVincenzo Maffione 
269437e3a6d3SLuigi Rizzo 	nmd->flags |= NETMAP_MEM_FINALIZED;
269537e3a6d3SLuigi Rizzo out:
269637e3a6d3SLuigi Rizzo 	return error;
269737e3a6d3SLuigi Rizzo }
269837e3a6d3SLuigi Rizzo 
269937e3a6d3SLuigi Rizzo static void
2700a6d768d8SVincenzo Maffione netmap_mem_pt_guest_deref(struct netmap_mem_d *nmd, struct netmap_adapter *na)
270137e3a6d3SLuigi Rizzo {
270237e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
270337e3a6d3SLuigi Rizzo 
27042ff91c17SVincenzo Maffione 	if (nmd->active == 1 &&
270537e3a6d3SLuigi Rizzo 		(nmd->flags & NETMAP_MEM_FINALIZED)) {
270637e3a6d3SLuigi Rizzo 	    nmd->flags  &= ~NETMAP_MEM_FINALIZED;
270737e3a6d3SLuigi Rizzo 	    /* unmap ptnetmap-memdev memory */
270837e3a6d3SLuigi Rizzo 	    if (ptnmd->ptn_dev) {
270937e3a6d3SLuigi Rizzo 		nm_os_pt_memdev_iounmap(ptnmd->ptn_dev);
271037e3a6d3SLuigi Rizzo 	    }
27114dd44461SLuiz Otavio O Souza 	    ptnmd->nm_addr = NULL;
271237e3a6d3SLuigi Rizzo 	    ptnmd->nm_paddr = 0;
271337e3a6d3SLuigi Rizzo 	}
271437e3a6d3SLuigi Rizzo }
271537e3a6d3SLuigi Rizzo 
271637e3a6d3SLuigi Rizzo static ssize_t
271737e3a6d3SLuigi Rizzo netmap_mem_pt_guest_if_offset(struct netmap_mem_d *nmd, const void *vaddr)
271837e3a6d3SLuigi Rizzo {
271937e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
272037e3a6d3SLuigi Rizzo 
272137e3a6d3SLuigi Rizzo 	return (const char *)(vaddr) - (char *)(ptnmd->nm_addr);
272237e3a6d3SLuigi Rizzo }
272337e3a6d3SLuigi Rizzo 
272437e3a6d3SLuigi Rizzo static void
272537e3a6d3SLuigi Rizzo netmap_mem_pt_guest_delete(struct netmap_mem_d *nmd)
272637e3a6d3SLuigi Rizzo {
272737e3a6d3SLuigi Rizzo 	if (nmd == NULL)
272837e3a6d3SLuigi Rizzo 		return;
272937e3a6d3SLuigi Rizzo 	if (netmap_verbose)
273075f4f3edSVincenzo Maffione 		nm_prinf("deleting %p", nmd);
273137e3a6d3SLuigi Rizzo 	if (nmd->active > 0)
273275f4f3edSVincenzo Maffione 		nm_prerr("bug: deleting mem allocator with active=%d!", nmd->active);
273337e3a6d3SLuigi Rizzo 	if (netmap_verbose)
273475f4f3edSVincenzo Maffione 		nm_prinf("done deleting %p", nmd);
273537e3a6d3SLuigi Rizzo 	NMA_LOCK_DESTROY(nmd);
2736c3e9b4dbSLuiz Otavio O Souza 	nm_os_free(nmd);
273737e3a6d3SLuigi Rizzo }
273837e3a6d3SLuigi Rizzo 
273937e3a6d3SLuigi Rizzo static struct netmap_if *
2740a6d768d8SVincenzo Maffione netmap_mem_pt_guest_if_new(struct netmap_mem_d *nmd,
2741a6d768d8SVincenzo Maffione 		struct netmap_adapter *na, struct netmap_priv_d *priv)
274237e3a6d3SLuigi Rizzo {
2743a6d768d8SVincenzo Maffione 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
274437e3a6d3SLuigi Rizzo 	struct mem_pt_if *ptif;
274537e3a6d3SLuigi Rizzo 	struct netmap_if *nifp = NULL;
274637e3a6d3SLuigi Rizzo 
2747a6d768d8SVincenzo Maffione 	ptif = netmap_mem_pt_guest_ifp_lookup(nmd, na->ifp);
274837e3a6d3SLuigi Rizzo 	if (ptif == NULL) {
274975f4f3edSVincenzo Maffione 		nm_prerr("interface %s is not in passthrough", na->name);
275037e3a6d3SLuigi Rizzo 		goto out;
275137e3a6d3SLuigi Rizzo 	}
275237e3a6d3SLuigi Rizzo 
275337e3a6d3SLuigi Rizzo 	nifp = (struct netmap_if *)((char *)(ptnmd->nm_addr) +
275437e3a6d3SLuigi Rizzo 				    ptif->nifp_offset);
275537e3a6d3SLuigi Rizzo out:
275637e3a6d3SLuigi Rizzo 	return nifp;
275737e3a6d3SLuigi Rizzo }
275837e3a6d3SLuigi Rizzo 
275937e3a6d3SLuigi Rizzo static void
2760a6d768d8SVincenzo Maffione netmap_mem_pt_guest_if_delete(struct netmap_mem_d * nmd,
2761a6d768d8SVincenzo Maffione 		struct netmap_adapter *na, struct netmap_if *nifp)
276237e3a6d3SLuigi Rizzo {
276337e3a6d3SLuigi Rizzo 	struct mem_pt_if *ptif;
276437e3a6d3SLuigi Rizzo 
2765a6d768d8SVincenzo Maffione 	ptif = netmap_mem_pt_guest_ifp_lookup(nmd, na->ifp);
276637e3a6d3SLuigi Rizzo 	if (ptif == NULL) {
276775f4f3edSVincenzo Maffione 		nm_prerr("interface %s is not in passthrough", na->name);
276837e3a6d3SLuigi Rizzo 	}
276937e3a6d3SLuigi Rizzo }
277037e3a6d3SLuigi Rizzo 
277137e3a6d3SLuigi Rizzo static int
2772a6d768d8SVincenzo Maffione netmap_mem_pt_guest_rings_create(struct netmap_mem_d *nmd,
2773a6d768d8SVincenzo Maffione 		struct netmap_adapter *na)
277437e3a6d3SLuigi Rizzo {
2775a6d768d8SVincenzo Maffione 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
277637e3a6d3SLuigi Rizzo 	struct mem_pt_if *ptif;
277737e3a6d3SLuigi Rizzo 	struct netmap_if *nifp;
277837e3a6d3SLuigi Rizzo 	int i, error = -1;
277937e3a6d3SLuigi Rizzo 
2780a6d768d8SVincenzo Maffione 	ptif = netmap_mem_pt_guest_ifp_lookup(nmd, na->ifp);
278137e3a6d3SLuigi Rizzo 	if (ptif == NULL) {
278275f4f3edSVincenzo Maffione 		nm_prerr("interface %s is not in passthrough", na->name);
278337e3a6d3SLuigi Rizzo 		goto out;
278437e3a6d3SLuigi Rizzo 	}
278537e3a6d3SLuigi Rizzo 
278637e3a6d3SLuigi Rizzo 
278737e3a6d3SLuigi Rizzo 	/* point each kring to the corresponding backend ring */
278837e3a6d3SLuigi Rizzo 	nifp = (struct netmap_if *)((char *)ptnmd->nm_addr + ptif->nifp_offset);
27892a7db7a6SVincenzo Maffione 	for (i = 0; i < netmap_all_rings(na, NR_TX); i++) {
27902ff91c17SVincenzo Maffione 		struct netmap_kring *kring = na->tx_rings[i];
279137e3a6d3SLuigi Rizzo 		if (kring->ring)
279237e3a6d3SLuigi Rizzo 			continue;
279337e3a6d3SLuigi Rizzo 		kring->ring = (struct netmap_ring *)
279437e3a6d3SLuigi Rizzo 			((char *)nifp + nifp->ring_ofs[i]);
279537e3a6d3SLuigi Rizzo 	}
27962a7db7a6SVincenzo Maffione 	for (i = 0; i < netmap_all_rings(na, NR_RX); i++) {
27972ff91c17SVincenzo Maffione 		struct netmap_kring *kring = na->rx_rings[i];
279837e3a6d3SLuigi Rizzo 		if (kring->ring)
279937e3a6d3SLuigi Rizzo 			continue;
280037e3a6d3SLuigi Rizzo 		kring->ring = (struct netmap_ring *)
280137e3a6d3SLuigi Rizzo 			((char *)nifp +
2802b6e66be2SVincenzo Maffione 			 nifp->ring_ofs[netmap_all_rings(na, NR_TX) + i]);
280337e3a6d3SLuigi Rizzo 	}
280437e3a6d3SLuigi Rizzo 
280537e3a6d3SLuigi Rizzo 	error = 0;
280637e3a6d3SLuigi Rizzo out:
280737e3a6d3SLuigi Rizzo 	return error;
280837e3a6d3SLuigi Rizzo }
280937e3a6d3SLuigi Rizzo 
281037e3a6d3SLuigi Rizzo static void
2811a6d768d8SVincenzo Maffione netmap_mem_pt_guest_rings_delete(struct netmap_mem_d *nmd, struct netmap_adapter *na)
281237e3a6d3SLuigi Rizzo {
281337e3a6d3SLuigi Rizzo #if 0
28144f80b14cSVincenzo Maffione 	enum txrx t;
28154f80b14cSVincenzo Maffione 
28164f80b14cSVincenzo Maffione 	for_rx_tx(t) {
28174f80b14cSVincenzo Maffione 		u_int i;
28184f80b14cSVincenzo Maffione 		for (i = 0; i < nma_get_nrings(na, t) + 1; i++) {
28194f80b14cSVincenzo Maffione 			struct netmap_kring *kring = &NMR(na, t)[i];
28204f80b14cSVincenzo Maffione 
28214f80b14cSVincenzo Maffione 			kring->ring = NULL;
28224f80b14cSVincenzo Maffione 		}
28234f80b14cSVincenzo Maffione 	}
282437e3a6d3SLuigi Rizzo #endif
2825a6d768d8SVincenzo Maffione 	(void)nmd;
2826a6d768d8SVincenzo Maffione 	(void)na;
282737e3a6d3SLuigi Rizzo }
282837e3a6d3SLuigi Rizzo 
282937e3a6d3SLuigi Rizzo static struct netmap_mem_ops netmap_mem_pt_guest_ops = {
283037e3a6d3SLuigi Rizzo 	.nmd_get_lut = netmap_mem_pt_guest_get_lut,
283137e3a6d3SLuigi Rizzo 	.nmd_get_info = netmap_mem_pt_guest_get_info,
283237e3a6d3SLuigi Rizzo 	.nmd_ofstophys = netmap_mem_pt_guest_ofstophys,
283337e3a6d3SLuigi Rizzo 	.nmd_config = netmap_mem_pt_guest_config,
283437e3a6d3SLuigi Rizzo 	.nmd_finalize = netmap_mem_pt_guest_finalize,
283537e3a6d3SLuigi Rizzo 	.nmd_deref = netmap_mem_pt_guest_deref,
283637e3a6d3SLuigi Rizzo 	.nmd_if_offset = netmap_mem_pt_guest_if_offset,
283737e3a6d3SLuigi Rizzo 	.nmd_delete = netmap_mem_pt_guest_delete,
283837e3a6d3SLuigi Rizzo 	.nmd_if_new = netmap_mem_pt_guest_if_new,
283937e3a6d3SLuigi Rizzo 	.nmd_if_delete = netmap_mem_pt_guest_if_delete,
284037e3a6d3SLuigi Rizzo 	.nmd_rings_create = netmap_mem_pt_guest_rings_create,
284137e3a6d3SLuigi Rizzo 	.nmd_rings_delete = netmap_mem_pt_guest_rings_delete
284237e3a6d3SLuigi Rizzo };
284337e3a6d3SLuigi Rizzo 
2844c3e9b4dbSLuiz Otavio O Souza /* Called with nm_mem_list_lock held. */
284537e3a6d3SLuigi Rizzo static struct netmap_mem_d *
2846844a6f0cSLuigi Rizzo netmap_mem_pt_guest_find_memid(nm_memid_t mem_id)
284737e3a6d3SLuigi Rizzo {
284837e3a6d3SLuigi Rizzo 	struct netmap_mem_d *mem = NULL;
284937e3a6d3SLuigi Rizzo 	struct netmap_mem_d *scan = netmap_last_mem_d;
285037e3a6d3SLuigi Rizzo 
285137e3a6d3SLuigi Rizzo 	do {
285237e3a6d3SLuigi Rizzo 		/* find ptnetmap allocator through host ID */
285337e3a6d3SLuigi Rizzo 		if (scan->ops->nmd_deref == netmap_mem_pt_guest_deref &&
2854844a6f0cSLuigi Rizzo 			((struct netmap_mem_ptg *)(scan))->host_mem_id == mem_id) {
285537e3a6d3SLuigi Rizzo 			mem = scan;
2856c3e9b4dbSLuiz Otavio O Souza 			mem->refcount++;
2857c3e9b4dbSLuiz Otavio O Souza 			NM_DBG_REFC(mem, __FUNCTION__, __LINE__);
285837e3a6d3SLuigi Rizzo 			break;
285937e3a6d3SLuigi Rizzo 		}
286037e3a6d3SLuigi Rizzo 		scan = scan->next;
286137e3a6d3SLuigi Rizzo 	} while (scan != netmap_last_mem_d);
286237e3a6d3SLuigi Rizzo 
286337e3a6d3SLuigi Rizzo 	return mem;
286437e3a6d3SLuigi Rizzo }
286537e3a6d3SLuigi Rizzo 
2866c3e9b4dbSLuiz Otavio O Souza /* Called with nm_mem_list_lock held. */
286737e3a6d3SLuigi Rizzo static struct netmap_mem_d *
2868844a6f0cSLuigi Rizzo netmap_mem_pt_guest_create(nm_memid_t mem_id)
286937e3a6d3SLuigi Rizzo {
287037e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd;
287137e3a6d3SLuigi Rizzo 	int err = 0;
287237e3a6d3SLuigi Rizzo 
2873c3e9b4dbSLuiz Otavio O Souza 	ptnmd = nm_os_malloc(sizeof(struct netmap_mem_ptg));
287437e3a6d3SLuigi Rizzo 	if (ptnmd == NULL) {
287537e3a6d3SLuigi Rizzo 		err = ENOMEM;
287637e3a6d3SLuigi Rizzo 		goto error;
287737e3a6d3SLuigi Rizzo 	}
287837e3a6d3SLuigi Rizzo 
287937e3a6d3SLuigi Rizzo 	ptnmd->up.ops = &netmap_mem_pt_guest_ops;
2880844a6f0cSLuigi Rizzo 	ptnmd->host_mem_id = mem_id;
288137e3a6d3SLuigi Rizzo 	ptnmd->pt_ifs = NULL;
288237e3a6d3SLuigi Rizzo 
288337e3a6d3SLuigi Rizzo 	/* Assign new id in the guest (We have the lock) */
2884a6d768d8SVincenzo Maffione 	err = nm_mem_assign_id_locked(&ptnmd->up, -1);
288537e3a6d3SLuigi Rizzo 	if (err)
288637e3a6d3SLuigi Rizzo 		goto error;
288737e3a6d3SLuigi Rizzo 
288837e3a6d3SLuigi Rizzo 	ptnmd->up.flags &= ~NETMAP_MEM_FINALIZED;
288937e3a6d3SLuigi Rizzo 	ptnmd->up.flags |= NETMAP_MEM_IO;
289037e3a6d3SLuigi Rizzo 
289137e3a6d3SLuigi Rizzo 	NMA_LOCK_INIT(&ptnmd->up);
289237e3a6d3SLuigi Rizzo 
2893c3e9b4dbSLuiz Otavio O Souza 	snprintf(ptnmd->up.name, NM_MEM_NAMESZ, "%d", ptnmd->up.nm_id);
2894c3e9b4dbSLuiz Otavio O Souza 
2895c3e9b4dbSLuiz Otavio O Souza 
289637e3a6d3SLuigi Rizzo 	return &ptnmd->up;
289737e3a6d3SLuigi Rizzo error:
289837e3a6d3SLuigi Rizzo 	netmap_mem_pt_guest_delete(&ptnmd->up);
289937e3a6d3SLuigi Rizzo 	return NULL;
290037e3a6d3SLuigi Rizzo }
290137e3a6d3SLuigi Rizzo 
290237e3a6d3SLuigi Rizzo /*
290337e3a6d3SLuigi Rizzo  * find host id in guest allocators and create guest allocator
290437e3a6d3SLuigi Rizzo  * if it is not there
290537e3a6d3SLuigi Rizzo  */
290637e3a6d3SLuigi Rizzo static struct netmap_mem_d *
2907844a6f0cSLuigi Rizzo netmap_mem_pt_guest_get(nm_memid_t mem_id)
290837e3a6d3SLuigi Rizzo {
290937e3a6d3SLuigi Rizzo 	struct netmap_mem_d *nmd;
291037e3a6d3SLuigi Rizzo 
2911c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_LOCK(nm_mem_list_lock);
2912844a6f0cSLuigi Rizzo 	nmd = netmap_mem_pt_guest_find_memid(mem_id);
291337e3a6d3SLuigi Rizzo 	if (nmd == NULL) {
2914844a6f0cSLuigi Rizzo 		nmd = netmap_mem_pt_guest_create(mem_id);
291537e3a6d3SLuigi Rizzo 	}
2916c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_UNLOCK(nm_mem_list_lock);
291737e3a6d3SLuigi Rizzo 
291837e3a6d3SLuigi Rizzo 	return nmd;
291937e3a6d3SLuigi Rizzo }
292037e3a6d3SLuigi Rizzo 
292137e3a6d3SLuigi Rizzo /*
292237e3a6d3SLuigi Rizzo  * The guest allocator can be created by ptnetmap_memdev (during the device
2923844a6f0cSLuigi Rizzo  * attach) or by ptnetmap device (ptnet), during the netmap_attach.
292437e3a6d3SLuigi Rizzo  *
292537e3a6d3SLuigi Rizzo  * The order is not important (we have different order in LINUX and FreeBSD).
292637e3a6d3SLuigi Rizzo  * The first one, creates the device, and the second one simply attaches it.
292737e3a6d3SLuigi Rizzo  */
292837e3a6d3SLuigi Rizzo 
292937e3a6d3SLuigi Rizzo /* Called when ptnetmap_memdev is attaching, to attach a new allocator in
293037e3a6d3SLuigi Rizzo  * the guest */
293137e3a6d3SLuigi Rizzo struct netmap_mem_d *
2932844a6f0cSLuigi Rizzo netmap_mem_pt_guest_attach(struct ptnetmap_memdev *ptn_dev, nm_memid_t mem_id)
293337e3a6d3SLuigi Rizzo {
293437e3a6d3SLuigi Rizzo 	struct netmap_mem_d *nmd;
293537e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd;
293637e3a6d3SLuigi Rizzo 
2937844a6f0cSLuigi Rizzo 	nmd = netmap_mem_pt_guest_get(mem_id);
293837e3a6d3SLuigi Rizzo 
293937e3a6d3SLuigi Rizzo 	/* assign this device to the guest allocator */
294037e3a6d3SLuigi Rizzo 	if (nmd) {
294137e3a6d3SLuigi Rizzo 		ptnmd = (struct netmap_mem_ptg *)nmd;
294237e3a6d3SLuigi Rizzo 		ptnmd->ptn_dev = ptn_dev;
294337e3a6d3SLuigi Rizzo 	}
294437e3a6d3SLuigi Rizzo 
294537e3a6d3SLuigi Rizzo 	return nmd;
294637e3a6d3SLuigi Rizzo }
294737e3a6d3SLuigi Rizzo 
2948844a6f0cSLuigi Rizzo /* Called when ptnet device is attaching */
294937e3a6d3SLuigi Rizzo struct netmap_mem_d *
2950e330262fSJustin Hibbits netmap_mem_pt_guest_new(if_t ifp,
295137e3a6d3SLuigi Rizzo 			unsigned int nifp_offset,
2952844a6f0cSLuigi Rizzo 			unsigned int memid)
295337e3a6d3SLuigi Rizzo {
295437e3a6d3SLuigi Rizzo 	struct netmap_mem_d *nmd;
295537e3a6d3SLuigi Rizzo 
2956844a6f0cSLuigi Rizzo 	if (ifp == NULL) {
295737e3a6d3SLuigi Rizzo 		return NULL;
295837e3a6d3SLuigi Rizzo 	}
295937e3a6d3SLuigi Rizzo 
2960844a6f0cSLuigi Rizzo 	nmd = netmap_mem_pt_guest_get((nm_memid_t)memid);
296137e3a6d3SLuigi Rizzo 
296237e3a6d3SLuigi Rizzo 	if (nmd) {
2963844a6f0cSLuigi Rizzo 		netmap_mem_pt_guest_ifp_add(nmd, ifp, nifp_offset);
296437e3a6d3SLuigi Rizzo 	}
296537e3a6d3SLuigi Rizzo 
296637e3a6d3SLuigi Rizzo 	return nmd;
296737e3a6d3SLuigi Rizzo }
296837e3a6d3SLuigi Rizzo 
2969b6e66be2SVincenzo Maffione #endif /* WITH_PTNETMAP */
2970