xref: /freebsd-14.2/sys/dev/netmap/netmap_mem2.c (revision c3e9b4db)
1ccdc3305SLuigi Rizzo /*
237e3a6d3SLuigi Rizzo  * Copyright (C) 2012-2014 Matteo Landi
337e3a6d3SLuigi Rizzo  * Copyright (C) 2012-2016 Luigi Rizzo
437e3a6d3SLuigi Rizzo  * Copyright (C) 2012-2016 Giuseppe Lettieri
537e3a6d3SLuigi Rizzo  * All rights reserved.
6ccdc3305SLuigi Rizzo  *
7ccdc3305SLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
8ccdc3305SLuigi Rizzo  * modification, are permitted provided that the following conditions
9ccdc3305SLuigi Rizzo  * are met:
10ccdc3305SLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
11ccdc3305SLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
12ccdc3305SLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
13ccdc3305SLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
14ccdc3305SLuigi Rizzo  *      documentation and/or other materials provided with the distribution.
15ccdc3305SLuigi Rizzo  *
16ccdc3305SLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17ccdc3305SLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18ccdc3305SLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19ccdc3305SLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20ccdc3305SLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21ccdc3305SLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22ccdc3305SLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23ccdc3305SLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24ccdc3305SLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25ccdc3305SLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26ccdc3305SLuigi Rizzo  * SUCH DAMAGE.
27ccdc3305SLuigi Rizzo  */
28ccdc3305SLuigi Rizzo 
298241616dSLuigi Rizzo #ifdef linux
30ce3ee1e7SLuigi Rizzo #include "bsd_glue.h"
318241616dSLuigi Rizzo #endif /* linux */
328241616dSLuigi Rizzo 
33ce3ee1e7SLuigi Rizzo #ifdef __APPLE__
34ce3ee1e7SLuigi Rizzo #include "osx_glue.h"
35ce3ee1e7SLuigi Rizzo #endif /* __APPLE__ */
368241616dSLuigi Rizzo 
37ce3ee1e7SLuigi Rizzo #ifdef __FreeBSD__
38ce3ee1e7SLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */
39ce3ee1e7SLuigi Rizzo __FBSDID("$FreeBSD$");
408241616dSLuigi Rizzo 
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;
90*c3e9b4dbSLuiz Otavio O Souza 
91*c3e9b4dbSLuiz Otavio O Souza 	u_int last_size;
92*c3e9b4dbSLuiz 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)            */
1014bf50f18SLuigi Rizzo 	u_int objtotal;         /* actual total number of objects. */
1024bf50f18SLuigi Rizzo 	u_int memtotal;		/* actual total memory space */
1034bf50f18SLuigi Rizzo 	u_int numclusters;	/* actual number of clusters */
1044bf50f18SLuigi Rizzo 
1054bf50f18SLuigi Rizzo 	u_int objfree;          /* number of free objects. */
1064bf50f18SLuigi Rizzo 
1074bf50f18SLuigi Rizzo 	struct lut_entry *lut;  /* virt,phys addresses, objtotal entries */
1084bf50f18SLuigi Rizzo 	uint32_t *bitmap;       /* one bit per buffer, 1 means free */
1094bf50f18SLuigi Rizzo 	uint32_t bitmap_slots;	/* number of uint32 entries in bitmap */
1104bf50f18SLuigi Rizzo 	/* ---------------------------------------------------*/
1114bf50f18SLuigi Rizzo 
1124bf50f18SLuigi Rizzo 	/* limits */
1134bf50f18SLuigi Rizzo 	u_int objminsize;	/* minimum object size */
1144bf50f18SLuigi Rizzo 	u_int objmaxsize;	/* maximum object size */
1154bf50f18SLuigi Rizzo 	u_int nummin;		/* minimum number of objects */
1164bf50f18SLuigi Rizzo 	u_int nummax;		/* maximum number of objects */
1174bf50f18SLuigi Rizzo 
1184bf50f18SLuigi Rizzo 	/* these are changed only by config */
1194bf50f18SLuigi Rizzo 	u_int _objtotal;	/* total number of objects */
1204bf50f18SLuigi Rizzo 	u_int _objsize;		/* object size */
1214bf50f18SLuigi Rizzo 	u_int _clustsize;       /* cluster size */
1224bf50f18SLuigi Rizzo 	u_int _clustentries;    /* objects per cluster */
1234bf50f18SLuigi Rizzo 	u_int _numclusters;	/* number of clusters */
1244bf50f18SLuigi Rizzo 
1254bf50f18SLuigi Rizzo 	/* requested values */
1264bf50f18SLuigi Rizzo 	u_int r_objtotal;
1274bf50f18SLuigi Rizzo 	u_int r_objsize;
1284bf50f18SLuigi Rizzo };
1294bf50f18SLuigi Rizzo 
130847bf383SLuigi Rizzo #define NMA_LOCK_T		NM_MTX_T
1314bf50f18SLuigi Rizzo 
132847bf383SLuigi Rizzo 
133847bf383SLuigi Rizzo struct netmap_mem_ops {
13437e3a6d3SLuigi Rizzo 	int (*nmd_get_lut)(struct netmap_mem_d *, struct netmap_lut*);
135847bf383SLuigi Rizzo 	int  (*nmd_get_info)(struct netmap_mem_d *, u_int *size,
136847bf383SLuigi Rizzo 			u_int *memflags, uint16_t *id);
137847bf383SLuigi Rizzo 
138847bf383SLuigi Rizzo 	vm_paddr_t (*nmd_ofstophys)(struct netmap_mem_d *, vm_ooffset_t);
139847bf383SLuigi Rizzo 	int (*nmd_config)(struct netmap_mem_d *);
140847bf383SLuigi Rizzo 	int (*nmd_finalize)(struct netmap_mem_d *);
141847bf383SLuigi Rizzo 	void (*nmd_deref)(struct netmap_mem_d *);
142847bf383SLuigi Rizzo 	ssize_t  (*nmd_if_offset)(struct netmap_mem_d *, const void *vaddr);
143847bf383SLuigi Rizzo 	void (*nmd_delete)(struct netmap_mem_d *);
144847bf383SLuigi Rizzo 
145*c3e9b4dbSLuiz Otavio O Souza 	struct netmap_if * (*nmd_if_new)(struct netmap_adapter *,
146*c3e9b4dbSLuiz Otavio O Souza 					 struct netmap_priv_d *);
147847bf383SLuigi Rizzo 	void (*nmd_if_delete)(struct netmap_adapter *, struct netmap_if *);
148847bf383SLuigi Rizzo 	int  (*nmd_rings_create)(struct netmap_adapter *);
149847bf383SLuigi Rizzo 	void (*nmd_rings_delete)(struct netmap_adapter *);
150847bf383SLuigi Rizzo };
1514bf50f18SLuigi Rizzo 
1524bf50f18SLuigi Rizzo struct netmap_mem_d {
1534bf50f18SLuigi Rizzo 	NMA_LOCK_T nm_mtx;  /* protect the allocator */
1544bf50f18SLuigi Rizzo 	u_int nm_totalsize; /* shorthand */
1554bf50f18SLuigi Rizzo 
1564bf50f18SLuigi Rizzo 	u_int flags;
1574bf50f18SLuigi Rizzo #define NETMAP_MEM_FINALIZED	0x1	/* preallocation done */
158*c3e9b4dbSLuiz Otavio O Souza #define NETMAP_MEM_HIDDEN	0x8	/* beeing prepared */
1594bf50f18SLuigi Rizzo 	int lasterr;		/* last error for curr config */
160847bf383SLuigi Rizzo 	int active;		/* active users */
161847bf383SLuigi Rizzo 	int refcount;
1624bf50f18SLuigi Rizzo 	/* the three allocators */
1634bf50f18SLuigi Rizzo 	struct netmap_obj_pool pools[NETMAP_POOLS_NR];
1644bf50f18SLuigi Rizzo 
1654bf50f18SLuigi Rizzo 	nm_memid_t nm_id;	/* allocator identifier */
1664bf50f18SLuigi Rizzo 	int nm_grp;	/* iommu groupd id */
1674bf50f18SLuigi Rizzo 
1684bf50f18SLuigi Rizzo 	/* list of all existing allocators, sorted by nm_id */
1694bf50f18SLuigi Rizzo 	struct netmap_mem_d *prev, *next;
170847bf383SLuigi Rizzo 
171847bf383SLuigi Rizzo 	struct netmap_mem_ops *ops;
172*c3e9b4dbSLuiz Otavio O Souza 
173*c3e9b4dbSLuiz Otavio O Souza 	struct netmap_obj_params params[NETMAP_POOLS_NR];
174*c3e9b4dbSLuiz Otavio O Souza 
175*c3e9b4dbSLuiz Otavio O Souza #define NM_MEM_NAMESZ	16
176*c3e9b4dbSLuiz Otavio O Souza 	char name[NM_MEM_NAMESZ];
1774bf50f18SLuigi Rizzo };
1784bf50f18SLuigi Rizzo 
17937e3a6d3SLuigi Rizzo /*
18037e3a6d3SLuigi Rizzo  * XXX need to fix the case of t0 == void
18137e3a6d3SLuigi Rizzo  */
182847bf383SLuigi Rizzo #define NMD_DEFCB(t0, name) \
183847bf383SLuigi Rizzo t0 \
184847bf383SLuigi Rizzo netmap_mem_##name(struct netmap_mem_d *nmd) \
185847bf383SLuigi Rizzo { \
186847bf383SLuigi Rizzo 	return nmd->ops->nmd_##name(nmd); \
187847bf383SLuigi Rizzo }
188847bf383SLuigi Rizzo 
189847bf383SLuigi Rizzo #define NMD_DEFCB1(t0, name, t1) \
190847bf383SLuigi Rizzo t0 \
191847bf383SLuigi Rizzo netmap_mem_##name(struct netmap_mem_d *nmd, t1 a1) \
192847bf383SLuigi Rizzo { \
193847bf383SLuigi Rizzo 	return nmd->ops->nmd_##name(nmd, a1); \
194847bf383SLuigi Rizzo }
195847bf383SLuigi Rizzo 
196847bf383SLuigi Rizzo #define NMD_DEFCB3(t0, name, t1, t2, t3) \
197847bf383SLuigi Rizzo t0 \
198847bf383SLuigi Rizzo netmap_mem_##name(struct netmap_mem_d *nmd, t1 a1, t2 a2, t3 a3) \
199847bf383SLuigi Rizzo { \
200847bf383SLuigi Rizzo 	return nmd->ops->nmd_##name(nmd, a1, a2, a3); \
201847bf383SLuigi Rizzo }
202847bf383SLuigi Rizzo 
203847bf383SLuigi Rizzo #define NMD_DEFNACB(t0, name) \
204847bf383SLuigi Rizzo t0 \
205847bf383SLuigi Rizzo netmap_mem_##name(struct netmap_adapter *na) \
206847bf383SLuigi Rizzo { \
207847bf383SLuigi Rizzo 	return na->nm_mem->ops->nmd_##name(na); \
208847bf383SLuigi Rizzo }
209847bf383SLuigi Rizzo 
210847bf383SLuigi Rizzo #define NMD_DEFNACB1(t0, name, t1) \
211847bf383SLuigi Rizzo t0 \
212847bf383SLuigi Rizzo netmap_mem_##name(struct netmap_adapter *na, t1 a1) \
213847bf383SLuigi Rizzo { \
214847bf383SLuigi Rizzo 	return na->nm_mem->ops->nmd_##name(na, a1); \
215847bf383SLuigi Rizzo }
216847bf383SLuigi Rizzo 
21737e3a6d3SLuigi Rizzo NMD_DEFCB1(int, get_lut, struct netmap_lut *);
218847bf383SLuigi Rizzo NMD_DEFCB3(int, get_info, u_int *, u_int *, uint16_t *);
219847bf383SLuigi Rizzo NMD_DEFCB1(vm_paddr_t, ofstophys, vm_ooffset_t);
220847bf383SLuigi Rizzo static int netmap_mem_config(struct netmap_mem_d *);
221847bf383SLuigi Rizzo NMD_DEFCB(int, config);
222847bf383SLuigi Rizzo NMD_DEFCB1(ssize_t, if_offset, const void *);
223847bf383SLuigi Rizzo NMD_DEFCB(void, delete);
224847bf383SLuigi Rizzo 
225*c3e9b4dbSLuiz Otavio O Souza NMD_DEFNACB1(struct netmap_if *, if_new, struct netmap_priv_d *);
226847bf383SLuigi Rizzo NMD_DEFNACB1(void, if_delete, struct netmap_if *);
227847bf383SLuigi Rizzo NMD_DEFNACB(int, rings_create);
228847bf383SLuigi Rizzo NMD_DEFNACB(void, rings_delete);
229847bf383SLuigi Rizzo 
230847bf383SLuigi Rizzo static int netmap_mem_map(struct netmap_obj_pool *, struct netmap_adapter *);
231847bf383SLuigi Rizzo static int netmap_mem_unmap(struct netmap_obj_pool *, struct netmap_adapter *);
23237e3a6d3SLuigi Rizzo static int nm_mem_assign_group(struct netmap_mem_d *, struct device *);
233*c3e9b4dbSLuiz Otavio O Souza static void nm_mem_release_id(struct netmap_mem_d *);
234*c3e9b4dbSLuiz Otavio O Souza 
235*c3e9b4dbSLuiz Otavio O Souza nm_memid_t
236*c3e9b4dbSLuiz Otavio O Souza netmap_mem_get_id(struct netmap_mem_d *nmd)
237*c3e9b4dbSLuiz Otavio O Souza {
238*c3e9b4dbSLuiz Otavio O Souza 	return nmd->nm_id;
239*c3e9b4dbSLuiz Otavio O Souza }
240847bf383SLuigi Rizzo 
241847bf383SLuigi Rizzo #define NMA_LOCK_INIT(n)	NM_MTX_INIT((n)->nm_mtx)
242847bf383SLuigi Rizzo #define NMA_LOCK_DESTROY(n)	NM_MTX_DESTROY((n)->nm_mtx)
243847bf383SLuigi Rizzo #define NMA_LOCK(n)		NM_MTX_LOCK((n)->nm_mtx)
244847bf383SLuigi Rizzo #define NMA_UNLOCK(n)		NM_MTX_UNLOCK((n)->nm_mtx)
245847bf383SLuigi Rizzo 
246847bf383SLuigi Rizzo #ifdef NM_DEBUG_MEM_PUTGET
247847bf383SLuigi Rizzo #define NM_DBG_REFC(nmd, func, line)	\
248*c3e9b4dbSLuiz Otavio O Souza 	nm_prinf("%s:%d mem[%d] -> %d\n", func, line, (nmd)->nm_id, (nmd)->refcount);
249847bf383SLuigi Rizzo #else
250847bf383SLuigi Rizzo #define NM_DBG_REFC(nmd, func, line)
251847bf383SLuigi Rizzo #endif
252847bf383SLuigi Rizzo 
253*c3e9b4dbSLuiz Otavio O Souza /* circular list of all existing allocators */
254*c3e9b4dbSLuiz Otavio O Souza static struct netmap_mem_d *netmap_last_mem_d = &nm_mem;
255*c3e9b4dbSLuiz Otavio O Souza NM_MTX_T nm_mem_list_lock;
256*c3e9b4dbSLuiz Otavio O Souza 
257*c3e9b4dbSLuiz Otavio O Souza struct netmap_mem_d *
258*c3e9b4dbSLuiz Otavio O Souza __netmap_mem_get(struct netmap_mem_d *nmd, const char *func, int line)
259847bf383SLuigi Rizzo {
260*c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_LOCK(nm_mem_list_lock);
261847bf383SLuigi Rizzo 	nmd->refcount++;
262847bf383SLuigi Rizzo 	NM_DBG_REFC(nmd, func, line);
263*c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_UNLOCK(nm_mem_list_lock);
264*c3e9b4dbSLuiz Otavio O Souza 	return nmd;
265847bf383SLuigi Rizzo }
266847bf383SLuigi Rizzo 
267*c3e9b4dbSLuiz Otavio O Souza void
268*c3e9b4dbSLuiz Otavio O Souza __netmap_mem_put(struct netmap_mem_d *nmd, const char *func, int line)
269847bf383SLuigi Rizzo {
270847bf383SLuigi Rizzo 	int last;
271*c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_LOCK(nm_mem_list_lock);
272847bf383SLuigi Rizzo 	last = (--nmd->refcount == 0);
273*c3e9b4dbSLuiz Otavio O Souza 	if (last)
274*c3e9b4dbSLuiz Otavio O Souza 		nm_mem_release_id(nmd);
275847bf383SLuigi Rizzo 	NM_DBG_REFC(nmd, func, line);
276*c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_UNLOCK(nm_mem_list_lock);
277847bf383SLuigi Rizzo 	if (last)
278847bf383SLuigi Rizzo 		netmap_mem_delete(nmd);
279847bf383SLuigi Rizzo }
280847bf383SLuigi Rizzo 
281847bf383SLuigi Rizzo int
282847bf383SLuigi Rizzo netmap_mem_finalize(struct netmap_mem_d *nmd, struct netmap_adapter *na)
283847bf383SLuigi Rizzo {
284847bf383SLuigi Rizzo 	if (nm_mem_assign_group(nmd, na->pdev) < 0) {
285847bf383SLuigi Rizzo 		return ENOMEM;
286847bf383SLuigi Rizzo 	} else {
28737e3a6d3SLuigi Rizzo 		NMA_LOCK(nmd);
28837e3a6d3SLuigi Rizzo 		nmd->lasterr = nmd->ops->nmd_finalize(nmd);
28937e3a6d3SLuigi Rizzo 		NMA_UNLOCK(nmd);
290847bf383SLuigi Rizzo 	}
291847bf383SLuigi Rizzo 
292847bf383SLuigi Rizzo 	if (!nmd->lasterr && na->pdev)
293847bf383SLuigi Rizzo 		netmap_mem_map(&nmd->pools[NETMAP_BUF_POOL], na);
294847bf383SLuigi Rizzo 
295847bf383SLuigi Rizzo 	return nmd->lasterr;
296847bf383SLuigi Rizzo }
297847bf383SLuigi Rizzo 
298847bf383SLuigi Rizzo void
299847bf383SLuigi Rizzo netmap_mem_deref(struct netmap_mem_d *nmd, struct netmap_adapter *na)
300847bf383SLuigi Rizzo {
301847bf383SLuigi Rizzo 	NMA_LOCK(nmd);
302847bf383SLuigi Rizzo 	netmap_mem_unmap(&nmd->pools[NETMAP_BUF_POOL], na);
30337e3a6d3SLuigi Rizzo 	if (nmd->active == 1) {
30437e3a6d3SLuigi Rizzo 		u_int i;
30537e3a6d3SLuigi Rizzo 
30637e3a6d3SLuigi Rizzo 		/*
30737e3a6d3SLuigi Rizzo 		 * Reset the allocator when it falls out of use so that any
30837e3a6d3SLuigi Rizzo 		 * pool resources leaked by unclean application exits are
30937e3a6d3SLuigi Rizzo 		 * reclaimed.
31037e3a6d3SLuigi Rizzo 		 */
31137e3a6d3SLuigi Rizzo 		for (i = 0; i < NETMAP_POOLS_NR; i++) {
31237e3a6d3SLuigi Rizzo 			struct netmap_obj_pool *p;
31337e3a6d3SLuigi Rizzo 			u_int j;
31437e3a6d3SLuigi Rizzo 
31537e3a6d3SLuigi Rizzo 			p = &nmd->pools[i];
31637e3a6d3SLuigi Rizzo 			p->objfree = p->objtotal;
31737e3a6d3SLuigi Rizzo 			/*
31837e3a6d3SLuigi Rizzo 			 * Reproduce the net effect of the M_ZERO malloc()
31937e3a6d3SLuigi Rizzo 			 * and marking of free entries in the bitmap that
32037e3a6d3SLuigi Rizzo 			 * occur in finalize_obj_allocator()
32137e3a6d3SLuigi Rizzo 			 */
32237e3a6d3SLuigi Rizzo 			memset(p->bitmap,
32337e3a6d3SLuigi Rizzo 			    '\0',
32437e3a6d3SLuigi Rizzo 			    sizeof(uint32_t) * ((p->objtotal + 31) / 32));
32537e3a6d3SLuigi Rizzo 
32637e3a6d3SLuigi Rizzo 			/*
32737e3a6d3SLuigi Rizzo 			 * Set all the bits in the bitmap that have
32837e3a6d3SLuigi Rizzo 			 * corresponding buffers to 1 to indicate they are
32937e3a6d3SLuigi Rizzo 			 * free.
33037e3a6d3SLuigi Rizzo 			 */
33137e3a6d3SLuigi Rizzo 			for (j = 0; j < p->objtotal; j++) {
33237e3a6d3SLuigi Rizzo 				if (p->lut[j].vaddr != NULL) {
33337e3a6d3SLuigi Rizzo 					p->bitmap[ (j>>5) ] |=  ( 1 << (j & 31) );
33437e3a6d3SLuigi Rizzo 				}
33537e3a6d3SLuigi Rizzo 			}
33637e3a6d3SLuigi Rizzo 		}
33737e3a6d3SLuigi Rizzo 
33837e3a6d3SLuigi Rizzo 		/*
33937e3a6d3SLuigi Rizzo 		 * Per netmap_mem_finalize_all(),
34037e3a6d3SLuigi Rizzo 		 * buffers 0 and 1 are reserved
34137e3a6d3SLuigi Rizzo 		 */
34237e3a6d3SLuigi Rizzo 		nmd->pools[NETMAP_BUF_POOL].objfree -= 2;
34337e3a6d3SLuigi Rizzo 		if (nmd->pools[NETMAP_BUF_POOL].bitmap) {
34437e3a6d3SLuigi Rizzo 			/* XXX This check is a workaround that prevents a
34537e3a6d3SLuigi Rizzo 			 * NULL pointer crash which currently happens only
346844a6f0cSLuigi Rizzo 			 * with ptnetmap guests.
347844a6f0cSLuigi Rizzo 			 * Removed shared-info --> is the bug still there? */
34837e3a6d3SLuigi Rizzo 			nmd->pools[NETMAP_BUF_POOL].bitmap[0] = ~3;
34937e3a6d3SLuigi Rizzo 		}
35037e3a6d3SLuigi Rizzo 	}
35137e3a6d3SLuigi Rizzo 	nmd->ops->nmd_deref(nmd);
35237e3a6d3SLuigi Rizzo 
353847bf383SLuigi Rizzo 	NMA_UNLOCK(nmd);
354847bf383SLuigi Rizzo }
355847bf383SLuigi Rizzo 
356847bf383SLuigi Rizzo 
3574bf50f18SLuigi Rizzo /* accessor functions */
35837e3a6d3SLuigi Rizzo static int
359847bf383SLuigi Rizzo netmap_mem2_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut)
3604bf50f18SLuigi Rizzo {
361847bf383SLuigi Rizzo 	lut->lut = nmd->pools[NETMAP_BUF_POOL].lut;
362847bf383SLuigi Rizzo 	lut->objtotal = nmd->pools[NETMAP_BUF_POOL].objtotal;
363847bf383SLuigi Rizzo 	lut->objsize = nmd->pools[NETMAP_BUF_POOL]._objsize;
36437e3a6d3SLuigi Rizzo 
36537e3a6d3SLuigi Rizzo 	return 0;
3664bf50f18SLuigi Rizzo }
3674bf50f18SLuigi Rizzo 
36837e3a6d3SLuigi Rizzo static struct netmap_obj_params netmap_min_priv_params[NETMAP_POOLS_NR] = {
369f0ea3689SLuigi Rizzo 	[NETMAP_IF_POOL] = {
370f0ea3689SLuigi Rizzo 		.size = 1024,
37137e3a6d3SLuigi Rizzo 		.num  = 2,
372f0ea3689SLuigi Rizzo 	},
373f0ea3689SLuigi Rizzo 	[NETMAP_RING_POOL] = {
374f0ea3689SLuigi Rizzo 		.size = 5*PAGE_SIZE,
375f0ea3689SLuigi Rizzo 		.num  = 4,
376f0ea3689SLuigi Rizzo 	},
377f0ea3689SLuigi Rizzo 	[NETMAP_BUF_POOL] = {
378f0ea3689SLuigi Rizzo 		.size = 2048,
379f0ea3689SLuigi Rizzo 		.num  = 4098,
380f0ea3689SLuigi Rizzo 	},
381f0ea3689SLuigi Rizzo };
382f0ea3689SLuigi Rizzo 
383ccdc3305SLuigi Rizzo 
3842579e2d7SLuigi Rizzo /*
3852579e2d7SLuigi Rizzo  * nm_mem is the memory allocator used for all physical interfaces
3862579e2d7SLuigi Rizzo  * running in netmap mode.
3872579e2d7SLuigi Rizzo  * Virtual (VALE) ports will have each its own allocator.
3882579e2d7SLuigi Rizzo  */
389847bf383SLuigi Rizzo extern struct netmap_mem_ops netmap_mem_global_ops; /* forward */
390ce3ee1e7SLuigi Rizzo struct netmap_mem_d nm_mem = {	/* Our memory allocator. */
3918241616dSLuigi Rizzo 	.pools = {
3928241616dSLuigi Rizzo 		[NETMAP_IF_POOL] = {
3938241616dSLuigi Rizzo 			.name 	= "netmap_if",
3948241616dSLuigi Rizzo 			.objminsize = sizeof(struct netmap_if),
3958241616dSLuigi Rizzo 			.objmaxsize = 4096,
3968241616dSLuigi Rizzo 			.nummin     = 10,	/* don't be stingy */
3978241616dSLuigi Rizzo 			.nummax	    = 10000,	/* XXX very large */
3988241616dSLuigi Rizzo 		},
3998241616dSLuigi Rizzo 		[NETMAP_RING_POOL] = {
4008241616dSLuigi Rizzo 			.name 	= "netmap_ring",
4018241616dSLuigi Rizzo 			.objminsize = sizeof(struct netmap_ring),
4028241616dSLuigi Rizzo 			.objmaxsize = 32*PAGE_SIZE,
4038241616dSLuigi Rizzo 			.nummin     = 2,
4048241616dSLuigi Rizzo 			.nummax	    = 1024,
4058241616dSLuigi Rizzo 		},
4068241616dSLuigi Rizzo 		[NETMAP_BUF_POOL] = {
4078241616dSLuigi Rizzo 			.name	= "netmap_buf",
4088241616dSLuigi Rizzo 			.objminsize = 64,
4098241616dSLuigi Rizzo 			.objmaxsize = 65536,
4108241616dSLuigi Rizzo 			.nummin     = 4,
4118241616dSLuigi Rizzo 			.nummax	    = 1000000, /* one million! */
4128241616dSLuigi Rizzo 		},
4138241616dSLuigi Rizzo 	},
414f0ea3689SLuigi Rizzo 
415*c3e9b4dbSLuiz Otavio O Souza 	.params = {
416*c3e9b4dbSLuiz Otavio O Souza 		[NETMAP_IF_POOL] = {
417*c3e9b4dbSLuiz Otavio O Souza 			.size = 1024,
418*c3e9b4dbSLuiz Otavio O Souza 			.num  = 100,
419*c3e9b4dbSLuiz Otavio O Souza 		},
420*c3e9b4dbSLuiz Otavio O Souza 		[NETMAP_RING_POOL] = {
421*c3e9b4dbSLuiz Otavio O Souza 			.size = 9*PAGE_SIZE,
422*c3e9b4dbSLuiz Otavio O Souza 			.num  = 200,
423*c3e9b4dbSLuiz Otavio O Souza 		},
424*c3e9b4dbSLuiz Otavio O Souza 		[NETMAP_BUF_POOL] = {
425*c3e9b4dbSLuiz Otavio O Souza 			.size = 2048,
426*c3e9b4dbSLuiz Otavio O Souza 			.num  = NETMAP_BUF_MAX_NUM,
427*c3e9b4dbSLuiz Otavio O Souza 		},
428*c3e9b4dbSLuiz Otavio O Souza 	},
429*c3e9b4dbSLuiz Otavio O Souza 
430f0ea3689SLuigi Rizzo 	.nm_id = 1,
4314bf50f18SLuigi Rizzo 	.nm_grp = -1,
432f0ea3689SLuigi Rizzo 
433f0ea3689SLuigi Rizzo 	.prev = &nm_mem,
434f0ea3689SLuigi Rizzo 	.next = &nm_mem,
435847bf383SLuigi Rizzo 
436*c3e9b4dbSLuiz Otavio O Souza 	.ops = &netmap_mem_global_ops,
437*c3e9b4dbSLuiz Otavio O Souza 
438*c3e9b4dbSLuiz Otavio O Souza 	.name = "1"
439ccdc3305SLuigi Rizzo };
440ccdc3305SLuigi Rizzo 
441ce3ee1e7SLuigi Rizzo 
442ce3ee1e7SLuigi Rizzo /* blueprint for the private memory allocators */
443847bf383SLuigi Rizzo extern struct netmap_mem_ops netmap_mem_private_ops; /* forward */
44437e3a6d3SLuigi Rizzo /* XXX clang is not happy about using name as a print format */
44537e3a6d3SLuigi Rizzo static const struct netmap_mem_d nm_blueprint = {
446ce3ee1e7SLuigi Rizzo 	.pools = {
447ce3ee1e7SLuigi Rizzo 		[NETMAP_IF_POOL] = {
448ce3ee1e7SLuigi Rizzo 			.name 	= "%s_if",
449ce3ee1e7SLuigi Rizzo 			.objminsize = sizeof(struct netmap_if),
450ce3ee1e7SLuigi Rizzo 			.objmaxsize = 4096,
451ce3ee1e7SLuigi Rizzo 			.nummin     = 1,
452f0ea3689SLuigi Rizzo 			.nummax	    = 100,
453ce3ee1e7SLuigi Rizzo 		},
454ce3ee1e7SLuigi Rizzo 		[NETMAP_RING_POOL] = {
455ce3ee1e7SLuigi Rizzo 			.name 	= "%s_ring",
456ce3ee1e7SLuigi Rizzo 			.objminsize = sizeof(struct netmap_ring),
457ce3ee1e7SLuigi Rizzo 			.objmaxsize = 32*PAGE_SIZE,
458ce3ee1e7SLuigi Rizzo 			.nummin     = 2,
459ce3ee1e7SLuigi Rizzo 			.nummax	    = 1024,
460ce3ee1e7SLuigi Rizzo 		},
461ce3ee1e7SLuigi Rizzo 		[NETMAP_BUF_POOL] = {
462ce3ee1e7SLuigi Rizzo 			.name	= "%s_buf",
463ce3ee1e7SLuigi Rizzo 			.objminsize = 64,
464ce3ee1e7SLuigi Rizzo 			.objmaxsize = 65536,
465ce3ee1e7SLuigi Rizzo 			.nummin     = 4,
466ce3ee1e7SLuigi Rizzo 			.nummax	    = 1000000, /* one million! */
467ce3ee1e7SLuigi Rizzo 		},
468ce3ee1e7SLuigi Rizzo 	},
469ce3ee1e7SLuigi Rizzo 
470*c3e9b4dbSLuiz Otavio O Souza 	.nm_grp = -1,
471*c3e9b4dbSLuiz Otavio O Souza 
472ce3ee1e7SLuigi Rizzo 	.flags = NETMAP_MEM_PRIVATE,
473847bf383SLuigi Rizzo 
474*c3e9b4dbSLuiz Otavio O Souza 	.ops = &netmap_mem_global_ops,
475ce3ee1e7SLuigi Rizzo };
476ce3ee1e7SLuigi Rizzo 
4778241616dSLuigi Rizzo /* memory allocator related sysctls */
4788241616dSLuigi Rizzo 
4798241616dSLuigi Rizzo #define STRINGIFY(x) #x
4808241616dSLuigi Rizzo 
481ce3ee1e7SLuigi Rizzo 
4828241616dSLuigi Rizzo #define DECLARE_SYSCTLS(id, name) \
48337e3a6d3SLuigi Rizzo 	SYSBEGIN(mem2_ ## name); \
4848241616dSLuigi Rizzo 	SYSCTL_INT(_dev_netmap, OID_AUTO, name##_size, \
485*c3e9b4dbSLuiz Otavio O Souza 	    CTLFLAG_RW, &nm_mem.params[id].size, 0, "Requested size of netmap " STRINGIFY(name) "s"); \
4868241616dSLuigi Rizzo 	SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_size, \
4878241616dSLuigi Rizzo 	    CTLFLAG_RD, &nm_mem.pools[id]._objsize, 0, "Current size of netmap " STRINGIFY(name) "s"); \
4888241616dSLuigi Rizzo 	SYSCTL_INT(_dev_netmap, OID_AUTO, name##_num, \
489*c3e9b4dbSLuiz Otavio O Souza 	    CTLFLAG_RW, &nm_mem.params[id].num, 0, "Requested number of netmap " STRINGIFY(name) "s"); \
4908241616dSLuigi Rizzo 	SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_num, \
491f0ea3689SLuigi Rizzo 	    CTLFLAG_RD, &nm_mem.pools[id].objtotal, 0, "Current number of netmap " STRINGIFY(name) "s"); \
492f0ea3689SLuigi Rizzo 	SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_size, \
493f0ea3689SLuigi Rizzo 	    CTLFLAG_RW, &netmap_min_priv_params[id].size, 0, \
494f0ea3689SLuigi Rizzo 	    "Default size of private netmap " STRINGIFY(name) "s"); \
495f0ea3689SLuigi Rizzo 	SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_num, \
496f0ea3689SLuigi Rizzo 	    CTLFLAG_RW, &netmap_min_priv_params[id].num, 0, \
49737e3a6d3SLuigi Rizzo 	    "Default number of private netmap " STRINGIFY(name) "s");	\
49837e3a6d3SLuigi Rizzo 	SYSEND
4998241616dSLuigi Rizzo 
500984ff0d9SEd Maste SYSCTL_DECL(_dev_netmap);
5018241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_IF_POOL, if);
5028241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_RING_POOL, ring);
5038241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_BUF_POOL, buf);
504ccdc3305SLuigi Rizzo 
505*c3e9b4dbSLuiz Otavio O Souza /* call with nm_mem_list_lock held */
506f0ea3689SLuigi Rizzo static int
50737e3a6d3SLuigi Rizzo nm_mem_assign_id_locked(struct netmap_mem_d *nmd)
508f0ea3689SLuigi Rizzo {
509f0ea3689SLuigi Rizzo 	nm_memid_t id;
510f0ea3689SLuigi Rizzo 	struct netmap_mem_d *scan = netmap_last_mem_d;
511f0ea3689SLuigi Rizzo 	int error = ENOMEM;
512f0ea3689SLuigi Rizzo 
513f0ea3689SLuigi Rizzo 	do {
514f0ea3689SLuigi Rizzo 		/* we rely on unsigned wrap around */
515f0ea3689SLuigi Rizzo 		id = scan->nm_id + 1;
516f0ea3689SLuigi Rizzo 		if (id == 0) /* reserve 0 as error value */
517f0ea3689SLuigi Rizzo 			id = 1;
518f0ea3689SLuigi Rizzo 		scan = scan->next;
519f0ea3689SLuigi Rizzo 		if (id != scan->nm_id) {
520f0ea3689SLuigi Rizzo 			nmd->nm_id = id;
521f0ea3689SLuigi Rizzo 			nmd->prev = scan->prev;
522f0ea3689SLuigi Rizzo 			nmd->next = scan;
523f0ea3689SLuigi Rizzo 			scan->prev->next = nmd;
524f0ea3689SLuigi Rizzo 			scan->prev = nmd;
525f0ea3689SLuigi Rizzo 			netmap_last_mem_d = nmd;
526*c3e9b4dbSLuiz Otavio O Souza 			nmd->refcount = 1;
527*c3e9b4dbSLuiz Otavio O Souza 			NM_DBG_REFC(nmd, __FUNCTION__, __LINE__);
528f0ea3689SLuigi Rizzo 			error = 0;
529f0ea3689SLuigi Rizzo 			break;
530f0ea3689SLuigi Rizzo 		}
531f0ea3689SLuigi Rizzo 	} while (scan != netmap_last_mem_d);
532f0ea3689SLuigi Rizzo 
533f0ea3689SLuigi Rizzo 	return error;
534f0ea3689SLuigi Rizzo }
535f0ea3689SLuigi Rizzo 
536*c3e9b4dbSLuiz Otavio O Souza /* call with nm_mem_list_lock *not* held */
53737e3a6d3SLuigi Rizzo static int
53837e3a6d3SLuigi Rizzo nm_mem_assign_id(struct netmap_mem_d *nmd)
53937e3a6d3SLuigi Rizzo {
54037e3a6d3SLuigi Rizzo         int ret;
54137e3a6d3SLuigi Rizzo 
542*c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_LOCK(nm_mem_list_lock);
54337e3a6d3SLuigi Rizzo         ret = nm_mem_assign_id_locked(nmd);
544*c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_UNLOCK(nm_mem_list_lock);
54537e3a6d3SLuigi Rizzo 
54637e3a6d3SLuigi Rizzo 	return ret;
54737e3a6d3SLuigi Rizzo }
54837e3a6d3SLuigi Rizzo 
549*c3e9b4dbSLuiz Otavio O Souza /* call with nm_mem_list_lock held */
550f0ea3689SLuigi Rizzo static void
551f0ea3689SLuigi Rizzo nm_mem_release_id(struct netmap_mem_d *nmd)
552f0ea3689SLuigi Rizzo {
553f0ea3689SLuigi Rizzo 	nmd->prev->next = nmd->next;
554f0ea3689SLuigi Rizzo 	nmd->next->prev = nmd->prev;
555f0ea3689SLuigi Rizzo 
556f0ea3689SLuigi Rizzo 	if (netmap_last_mem_d == nmd)
557f0ea3689SLuigi Rizzo 		netmap_last_mem_d = nmd->prev;
558f0ea3689SLuigi Rizzo 
559f0ea3689SLuigi Rizzo 	nmd->prev = nmd->next = NULL;
560*c3e9b4dbSLuiz Otavio O Souza }
561f0ea3689SLuigi Rizzo 
562*c3e9b4dbSLuiz Otavio O Souza struct netmap_mem_d *
563*c3e9b4dbSLuiz Otavio O Souza netmap_mem_find(nm_memid_t id)
564*c3e9b4dbSLuiz Otavio O Souza {
565*c3e9b4dbSLuiz Otavio O Souza 	struct netmap_mem_d *nmd;
566*c3e9b4dbSLuiz Otavio O Souza 
567*c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_LOCK(nm_mem_list_lock);
568*c3e9b4dbSLuiz Otavio O Souza 	nmd = netmap_last_mem_d;
569*c3e9b4dbSLuiz Otavio O Souza 	do {
570*c3e9b4dbSLuiz Otavio O Souza 		if (!(nmd->flags & NETMAP_MEM_HIDDEN) && nmd->nm_id == id) {
571*c3e9b4dbSLuiz Otavio O Souza 			nmd->refcount++;
572*c3e9b4dbSLuiz Otavio O Souza 			NM_DBG_REFC(nmd, __FUNCTION__, __LINE__);
573*c3e9b4dbSLuiz Otavio O Souza 			NM_MTX_UNLOCK(nm_mem_list_lock);
574*c3e9b4dbSLuiz Otavio O Souza 			return nmd;
575*c3e9b4dbSLuiz Otavio O Souza 		}
576*c3e9b4dbSLuiz Otavio O Souza 		nmd = nmd->next;
577*c3e9b4dbSLuiz Otavio O Souza 	} while (nmd != netmap_last_mem_d);
578*c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_UNLOCK(nm_mem_list_lock);
579*c3e9b4dbSLuiz Otavio O Souza 	return NULL;
580f0ea3689SLuigi Rizzo }
581f0ea3689SLuigi Rizzo 
5824bf50f18SLuigi Rizzo static int
58337e3a6d3SLuigi Rizzo nm_mem_assign_group(struct netmap_mem_d *nmd, struct device *dev)
5844bf50f18SLuigi Rizzo {
5854bf50f18SLuigi Rizzo 	int err = 0, id;
5864bf50f18SLuigi Rizzo 	id = nm_iommu_group_id(dev);
5874bf50f18SLuigi Rizzo 	if (netmap_verbose)
5884bf50f18SLuigi Rizzo 		D("iommu_group %d", id);
5894bf50f18SLuigi Rizzo 
5904bf50f18SLuigi Rizzo 	NMA_LOCK(nmd);
5914bf50f18SLuigi Rizzo 
5924bf50f18SLuigi Rizzo 	if (nmd->nm_grp < 0)
5934bf50f18SLuigi Rizzo 		nmd->nm_grp = id;
5944bf50f18SLuigi Rizzo 
5954bf50f18SLuigi Rizzo 	if (nmd->nm_grp != id)
5964bf50f18SLuigi Rizzo 		nmd->lasterr = err = ENOMEM;
5974bf50f18SLuigi Rizzo 
5984bf50f18SLuigi Rizzo 	NMA_UNLOCK(nmd);
5994bf50f18SLuigi Rizzo 	return err;
6004bf50f18SLuigi Rizzo }
601f0ea3689SLuigi Rizzo 
602ccdc3305SLuigi Rizzo /*
6032579e2d7SLuigi Rizzo  * First, find the allocator that contains the requested offset,
6042579e2d7SLuigi Rizzo  * then locate the cluster through a lookup table.
605ccdc3305SLuigi Rizzo  */
606847bf383SLuigi Rizzo static vm_paddr_t
607847bf383SLuigi Rizzo netmap_mem2_ofstophys(struct netmap_mem_d* nmd, vm_ooffset_t offset)
608ccdc3305SLuigi Rizzo {
609ccdc3305SLuigi Rizzo 	int i;
610ce3ee1e7SLuigi Rizzo 	vm_ooffset_t o = offset;
611ce3ee1e7SLuigi Rizzo 	vm_paddr_t pa;
612ce3ee1e7SLuigi Rizzo 	struct netmap_obj_pool *p;
613ccdc3305SLuigi Rizzo 
614ce3ee1e7SLuigi Rizzo 	NMA_LOCK(nmd);
615ce3ee1e7SLuigi Rizzo 	p = nmd->pools;
616ce3ee1e7SLuigi Rizzo 
617ce3ee1e7SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; offset -= p[i].memtotal, i++) {
618ce3ee1e7SLuigi Rizzo 		if (offset >= p[i].memtotal)
619ccdc3305SLuigi Rizzo 			continue;
6202579e2d7SLuigi Rizzo 		// now lookup the cluster's address
62137e3a6d3SLuigi Rizzo #ifndef _WIN32
6224bf50f18SLuigi Rizzo 		pa = vtophys(p[i].lut[offset / p[i]._objsize].vaddr) +
6238241616dSLuigi Rizzo 			offset % p[i]._objsize;
62437e3a6d3SLuigi Rizzo #else
62537e3a6d3SLuigi Rizzo 		pa = vtophys(p[i].lut[offset / p[i]._objsize].vaddr);
62637e3a6d3SLuigi Rizzo 		pa.QuadPart += offset % p[i]._objsize;
62737e3a6d3SLuigi Rizzo #endif
628ce3ee1e7SLuigi Rizzo 		NMA_UNLOCK(nmd);
629ce3ee1e7SLuigi Rizzo 		return pa;
630ccdc3305SLuigi Rizzo 	}
6318241616dSLuigi Rizzo 	/* this is only in case of errors */
632b1123b01SLuigi Rizzo 	D("invalid ofs 0x%x out of 0x%x 0x%x 0x%x", (u_int)o,
633ce3ee1e7SLuigi Rizzo 		p[NETMAP_IF_POOL].memtotal,
634ce3ee1e7SLuigi Rizzo 		p[NETMAP_IF_POOL].memtotal
635ce3ee1e7SLuigi Rizzo 			+ p[NETMAP_RING_POOL].memtotal,
636ce3ee1e7SLuigi Rizzo 		p[NETMAP_IF_POOL].memtotal
637ce3ee1e7SLuigi Rizzo 			+ p[NETMAP_RING_POOL].memtotal
638ce3ee1e7SLuigi Rizzo 			+ p[NETMAP_BUF_POOL].memtotal);
639ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(nmd);
64037e3a6d3SLuigi Rizzo #ifndef _WIN32
641ccdc3305SLuigi Rizzo 	return 0;	// XXX bad address
64237e3a6d3SLuigi Rizzo #else
64337e3a6d3SLuigi Rizzo 	vm_paddr_t res;
64437e3a6d3SLuigi Rizzo 	res.QuadPart = 0;
64537e3a6d3SLuigi Rizzo 	return res;
64637e3a6d3SLuigi Rizzo #endif
64737e3a6d3SLuigi Rizzo }
64837e3a6d3SLuigi Rizzo 
64937e3a6d3SLuigi Rizzo #ifdef _WIN32
65037e3a6d3SLuigi Rizzo 
65137e3a6d3SLuigi Rizzo /*
65237e3a6d3SLuigi Rizzo  * win32_build_virtual_memory_for_userspace
65337e3a6d3SLuigi Rizzo  *
65437e3a6d3SLuigi Rizzo  * This function get all the object making part of the pools and maps
65537e3a6d3SLuigi Rizzo  * a contiguous virtual memory space for the userspace
65637e3a6d3SLuigi Rizzo  * It works this way
65737e3a6d3SLuigi Rizzo  * 1 - allocate a Memory Descriptor List wide as the sum
65837e3a6d3SLuigi Rizzo  *		of the memory needed for the pools
65937e3a6d3SLuigi Rizzo  * 2 - cycle all the objects in every pool and for every object do
66037e3a6d3SLuigi Rizzo  *
66137e3a6d3SLuigi Rizzo  *		2a - cycle all the objects in every pool, get the list
66237e3a6d3SLuigi Rizzo  *				of the physical address descriptors
66337e3a6d3SLuigi Rizzo  *		2b - calculate the offset in the array of pages desciptor in the
66437e3a6d3SLuigi Rizzo  *				main MDL
66537e3a6d3SLuigi Rizzo  *		2c - copy the descriptors of the object in the main MDL
66637e3a6d3SLuigi Rizzo  *
66737e3a6d3SLuigi Rizzo  * 3 - return the resulting MDL that needs to be mapped in userland
66837e3a6d3SLuigi Rizzo  *
66937e3a6d3SLuigi Rizzo  * In this way we will have an MDL that describes all the memory for the
67037e3a6d3SLuigi Rizzo  * objects in a single object
67137e3a6d3SLuigi Rizzo */
67237e3a6d3SLuigi Rizzo 
67337e3a6d3SLuigi Rizzo PMDL
67437e3a6d3SLuigi Rizzo win32_build_user_vm_map(struct netmap_mem_d* nmd)
67537e3a6d3SLuigi Rizzo {
67637e3a6d3SLuigi Rizzo 	int i, j;
67737e3a6d3SLuigi Rizzo 	u_int memsize, memflags, ofs = 0;
67837e3a6d3SLuigi Rizzo 	PMDL mainMdl, tempMdl;
67937e3a6d3SLuigi Rizzo 
68037e3a6d3SLuigi Rizzo 	if (netmap_mem_get_info(nmd, &memsize, &memflags, NULL)) {
68137e3a6d3SLuigi Rizzo 		D("memory not finalised yet");
68237e3a6d3SLuigi Rizzo 		return NULL;
68337e3a6d3SLuigi Rizzo 	}
68437e3a6d3SLuigi Rizzo 
68537e3a6d3SLuigi Rizzo 	mainMdl = IoAllocateMdl(NULL, memsize, FALSE, FALSE, NULL);
68637e3a6d3SLuigi Rizzo 	if (mainMdl == NULL) {
68737e3a6d3SLuigi Rizzo 		D("failed to allocate mdl");
68837e3a6d3SLuigi Rizzo 		return NULL;
68937e3a6d3SLuigi Rizzo 	}
69037e3a6d3SLuigi Rizzo 
69137e3a6d3SLuigi Rizzo 	NMA_LOCK(nmd);
69237e3a6d3SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
69337e3a6d3SLuigi Rizzo 		struct netmap_obj_pool *p = &nmd->pools[i];
69437e3a6d3SLuigi Rizzo 		int clsz = p->_clustsize;
69537e3a6d3SLuigi Rizzo 		int clobjs = p->_clustentries; /* objects per cluster */
69637e3a6d3SLuigi Rizzo 		int mdl_len = sizeof(PFN_NUMBER) * BYTES_TO_PAGES(clsz);
69737e3a6d3SLuigi Rizzo 		PPFN_NUMBER pSrc, pDst;
69837e3a6d3SLuigi Rizzo 
69937e3a6d3SLuigi Rizzo 		/* each pool has a different cluster size so we need to reallocate */
70037e3a6d3SLuigi Rizzo 		tempMdl = IoAllocateMdl(p->lut[0].vaddr, clsz, FALSE, FALSE, NULL);
70137e3a6d3SLuigi Rizzo 		if (tempMdl == NULL) {
70237e3a6d3SLuigi Rizzo 			NMA_UNLOCK(nmd);
70337e3a6d3SLuigi Rizzo 			D("fail to allocate tempMdl");
70437e3a6d3SLuigi Rizzo 			IoFreeMdl(mainMdl);
70537e3a6d3SLuigi Rizzo 			return NULL;
70637e3a6d3SLuigi Rizzo 		}
70737e3a6d3SLuigi Rizzo 		pSrc = MmGetMdlPfnArray(tempMdl);
70837e3a6d3SLuigi Rizzo 		/* create one entry per cluster, the lut[] has one entry per object */
70937e3a6d3SLuigi Rizzo 		for (j = 0; j < p->numclusters; j++, ofs += clsz) {
71037e3a6d3SLuigi Rizzo 			pDst = &MmGetMdlPfnArray(mainMdl)[BYTES_TO_PAGES(ofs)];
71137e3a6d3SLuigi Rizzo 			MmInitializeMdl(tempMdl, p->lut[j*clobjs].vaddr, clsz);
71237e3a6d3SLuigi Rizzo 			MmBuildMdlForNonPagedPool(tempMdl); /* compute physical page addresses */
71337e3a6d3SLuigi Rizzo 			RtlCopyMemory(pDst, pSrc, mdl_len); /* copy the page descriptors */
71437e3a6d3SLuigi Rizzo 			mainMdl->MdlFlags = tempMdl->MdlFlags; /* XXX what is in here ? */
71537e3a6d3SLuigi Rizzo 		}
71637e3a6d3SLuigi Rizzo 		IoFreeMdl(tempMdl);
71737e3a6d3SLuigi Rizzo 	}
71837e3a6d3SLuigi Rizzo 	NMA_UNLOCK(nmd);
71937e3a6d3SLuigi Rizzo 	return mainMdl;
72037e3a6d3SLuigi Rizzo }
72137e3a6d3SLuigi Rizzo 
72237e3a6d3SLuigi Rizzo #endif /* _WIN32 */
72337e3a6d3SLuigi Rizzo 
72437e3a6d3SLuigi Rizzo /*
72537e3a6d3SLuigi Rizzo  * helper function for OS-specific mmap routines (currently only windows).
72637e3a6d3SLuigi Rizzo  * Given an nmd and a pool index, returns the cluster size and number of clusters.
72737e3a6d3SLuigi Rizzo  * Returns 0 if memory is finalised and the pool is valid, otherwise 1.
72837e3a6d3SLuigi Rizzo  * It should be called under NMA_LOCK(nmd) otherwise the underlying info can change.
72937e3a6d3SLuigi Rizzo  */
73037e3a6d3SLuigi Rizzo 
73137e3a6d3SLuigi Rizzo int
73237e3a6d3SLuigi Rizzo netmap_mem2_get_pool_info(struct netmap_mem_d* nmd, u_int pool, u_int *clustsize, u_int *numclusters)
73337e3a6d3SLuigi Rizzo {
73437e3a6d3SLuigi Rizzo 	if (!nmd || !clustsize || !numclusters || pool >= NETMAP_POOLS_NR)
73537e3a6d3SLuigi Rizzo 		return 1; /* invalid arguments */
73637e3a6d3SLuigi Rizzo 	// NMA_LOCK_ASSERT(nmd);
73737e3a6d3SLuigi Rizzo 	if (!(nmd->flags & NETMAP_MEM_FINALIZED)) {
73837e3a6d3SLuigi Rizzo 		*clustsize = *numclusters = 0;
73937e3a6d3SLuigi Rizzo 		return 1; /* not ready yet */
74037e3a6d3SLuigi Rizzo 	}
74137e3a6d3SLuigi Rizzo 	*clustsize = nmd->pools[pool]._clustsize;
74237e3a6d3SLuigi Rizzo 	*numclusters = nmd->pools[pool].numclusters;
74337e3a6d3SLuigi Rizzo 	return 0; /* success */
744ccdc3305SLuigi Rizzo }
745ccdc3305SLuigi Rizzo 
746847bf383SLuigi Rizzo static int
747847bf383SLuigi Rizzo netmap_mem2_get_info(struct netmap_mem_d* nmd, u_int* size, u_int *memflags,
748f0ea3689SLuigi Rizzo 	nm_memid_t *id)
749ce3ee1e7SLuigi Rizzo {
750ce3ee1e7SLuigi Rizzo 	int error = 0;
751ce3ee1e7SLuigi Rizzo 	NMA_LOCK(nmd);
752847bf383SLuigi Rizzo 	error = netmap_mem_config(nmd);
753ce3ee1e7SLuigi Rizzo 	if (error)
754ce3ee1e7SLuigi Rizzo 		goto out;
7554bf50f18SLuigi Rizzo 	if (size) {
756ce3ee1e7SLuigi Rizzo 		if (nmd->flags & NETMAP_MEM_FINALIZED) {
757ce3ee1e7SLuigi Rizzo 			*size = nmd->nm_totalsize;
758ce3ee1e7SLuigi Rizzo 		} else {
759ce3ee1e7SLuigi Rizzo 			int i;
760ce3ee1e7SLuigi Rizzo 			*size = 0;
761ce3ee1e7SLuigi Rizzo 			for (i = 0; i < NETMAP_POOLS_NR; i++) {
762ce3ee1e7SLuigi Rizzo 				struct netmap_obj_pool *p = nmd->pools + i;
763ce3ee1e7SLuigi Rizzo 				*size += (p->_numclusters * p->_clustsize);
764ce3ee1e7SLuigi Rizzo 			}
765ce3ee1e7SLuigi Rizzo 		}
7664bf50f18SLuigi Rizzo 	}
7674bf50f18SLuigi Rizzo 	if (memflags)
768ce3ee1e7SLuigi Rizzo 		*memflags = nmd->flags;
7694bf50f18SLuigi Rizzo 	if (id)
770f0ea3689SLuigi Rizzo 		*id = nmd->nm_id;
771ce3ee1e7SLuigi Rizzo out:
772ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(nmd);
773ce3ee1e7SLuigi Rizzo 	return error;
774ce3ee1e7SLuigi Rizzo }
775ce3ee1e7SLuigi Rizzo 
776ccdc3305SLuigi Rizzo /*
777ccdc3305SLuigi Rizzo  * we store objects by kernel address, need to find the offset
778ccdc3305SLuigi Rizzo  * within the pool to export the value to userspace.
779ccdc3305SLuigi Rizzo  * Algorithm: scan until we find the cluster, then add the
780ccdc3305SLuigi Rizzo  * actual offset in the cluster
781ccdc3305SLuigi Rizzo  */
782ce2cb792SLuigi Rizzo static ssize_t
783ccdc3305SLuigi Rizzo netmap_obj_offset(struct netmap_obj_pool *p, const void *vaddr)
784ccdc3305SLuigi Rizzo {
785ce3ee1e7SLuigi Rizzo 	int i, k = p->_clustentries, n = p->objtotal;
786ccdc3305SLuigi Rizzo 	ssize_t ofs = 0;
787ccdc3305SLuigi Rizzo 
788ccdc3305SLuigi Rizzo 	for (i = 0; i < n; i += k, ofs += p->_clustsize) {
789ccdc3305SLuigi Rizzo 		const char *base = p->lut[i].vaddr;
790ccdc3305SLuigi Rizzo 		ssize_t relofs = (const char *) vaddr - base;
791ccdc3305SLuigi Rizzo 
792aa76317cSLuigi Rizzo 		if (relofs < 0 || relofs >= p->_clustsize)
793ccdc3305SLuigi Rizzo 			continue;
794ccdc3305SLuigi Rizzo 
795ccdc3305SLuigi Rizzo 		ofs = ofs + relofs;
796ccdc3305SLuigi Rizzo 		ND("%s: return offset %d (cluster %d) for pointer %p",
797ccdc3305SLuigi Rizzo 		    p->name, ofs, i, vaddr);
798ccdc3305SLuigi Rizzo 		return ofs;
799ccdc3305SLuigi Rizzo 	}
800ccdc3305SLuigi Rizzo 	D("address %p is not contained inside any cluster (%s)",
801ccdc3305SLuigi Rizzo 	    vaddr, p->name);
802ccdc3305SLuigi Rizzo 	return 0; /* An error occurred */
803ccdc3305SLuigi Rizzo }
804ccdc3305SLuigi Rizzo 
805ccdc3305SLuigi Rizzo /* Helper functions which convert virtual addresses to offsets */
806ce3ee1e7SLuigi Rizzo #define netmap_if_offset(n, v)					\
807ce3ee1e7SLuigi Rizzo 	netmap_obj_offset(&(n)->pools[NETMAP_IF_POOL], (v))
808ccdc3305SLuigi Rizzo 
809ce3ee1e7SLuigi Rizzo #define netmap_ring_offset(n, v)				\
810ce3ee1e7SLuigi Rizzo     ((n)->pools[NETMAP_IF_POOL].memtotal + 			\
811ce3ee1e7SLuigi Rizzo 	netmap_obj_offset(&(n)->pools[NETMAP_RING_POOL], (v)))
812ccdc3305SLuigi Rizzo 
813847bf383SLuigi Rizzo static ssize_t
814847bf383SLuigi Rizzo netmap_mem2_if_offset(struct netmap_mem_d *nmd, const void *addr)
815ce3ee1e7SLuigi Rizzo {
816ce3ee1e7SLuigi Rizzo 	ssize_t v;
817ce3ee1e7SLuigi Rizzo 	NMA_LOCK(nmd);
818ce3ee1e7SLuigi Rizzo 	v = netmap_if_offset(nmd, addr);
819ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(nmd);
820ce3ee1e7SLuigi Rizzo 	return v;
821ce3ee1e7SLuigi Rizzo }
822ce3ee1e7SLuigi Rizzo 
8238241616dSLuigi Rizzo /*
8248241616dSLuigi Rizzo  * report the index, and use start position as a hint,
8258241616dSLuigi Rizzo  * otherwise buffer allocation becomes terribly expensive.
8268241616dSLuigi Rizzo  */
827ccdc3305SLuigi Rizzo static void *
828ce3ee1e7SLuigi Rizzo netmap_obj_malloc(struct netmap_obj_pool *p, u_int len, uint32_t *start, uint32_t *index)
829ccdc3305SLuigi Rizzo {
830ccdc3305SLuigi Rizzo 	uint32_t i = 0;			/* index in the bitmap */
83137e3a6d3SLuigi Rizzo 	uint32_t mask, j = 0;		/* slot counter */
832ccdc3305SLuigi Rizzo 	void *vaddr = NULL;
833ccdc3305SLuigi Rizzo 
834ccdc3305SLuigi Rizzo 	if (len > p->_objsize) {
835ccdc3305SLuigi Rizzo 		D("%s request size %d too large", p->name, len);
836ccdc3305SLuigi Rizzo 		// XXX cannot reduce the size
837ccdc3305SLuigi Rizzo 		return NULL;
838ccdc3305SLuigi Rizzo 	}
839ccdc3305SLuigi Rizzo 
840ccdc3305SLuigi Rizzo 	if (p->objfree == 0) {
841f9790aebSLuigi Rizzo 		D("no more %s objects", p->name);
842ccdc3305SLuigi Rizzo 		return NULL;
843ccdc3305SLuigi Rizzo 	}
8448241616dSLuigi Rizzo 	if (start)
8458241616dSLuigi Rizzo 		i = *start;
846ccdc3305SLuigi Rizzo 
8478241616dSLuigi Rizzo 	/* termination is guaranteed by p->free, but better check bounds on i */
8488241616dSLuigi Rizzo 	while (vaddr == NULL && i < p->bitmap_slots)  {
849ccdc3305SLuigi Rizzo 		uint32_t cur = p->bitmap[i];
850ccdc3305SLuigi Rizzo 		if (cur == 0) { /* bitmask is fully used */
851ccdc3305SLuigi Rizzo 			i++;
852ccdc3305SLuigi Rizzo 			continue;
853ccdc3305SLuigi Rizzo 		}
854ccdc3305SLuigi Rizzo 		/* locate a slot */
855ccdc3305SLuigi Rizzo 		for (j = 0, mask = 1; (cur & mask) == 0; j++, mask <<= 1)
856ccdc3305SLuigi Rizzo 			;
857ccdc3305SLuigi Rizzo 
858ccdc3305SLuigi Rizzo 		p->bitmap[i] &= ~mask; /* mark object as in use */
859ccdc3305SLuigi Rizzo 		p->objfree--;
860ccdc3305SLuigi Rizzo 
861ccdc3305SLuigi Rizzo 		vaddr = p->lut[i * 32 + j].vaddr;
8628241616dSLuigi Rizzo 		if (index)
8638241616dSLuigi Rizzo 			*index = i * 32 + j;
864ccdc3305SLuigi Rizzo 	}
86537e3a6d3SLuigi Rizzo 	ND("%s allocator: allocated object @ [%d][%d]: vaddr %p",p->name, i, j, vaddr);
866ccdc3305SLuigi Rizzo 
8678241616dSLuigi Rizzo 	if (start)
8688241616dSLuigi Rizzo 		*start = i;
869ccdc3305SLuigi Rizzo 	return vaddr;
870ccdc3305SLuigi Rizzo }
871ccdc3305SLuigi Rizzo 
872ccdc3305SLuigi Rizzo 
873ccdc3305SLuigi Rizzo /*
874f0ea3689SLuigi Rizzo  * free by index, not by address.
875f0ea3689SLuigi Rizzo  * XXX should we also cleanup the content ?
876ccdc3305SLuigi Rizzo  */
877f0ea3689SLuigi Rizzo static int
878ccdc3305SLuigi Rizzo netmap_obj_free(struct netmap_obj_pool *p, uint32_t j)
879ccdc3305SLuigi Rizzo {
880f0ea3689SLuigi Rizzo 	uint32_t *ptr, mask;
881f0ea3689SLuigi Rizzo 
882ccdc3305SLuigi Rizzo 	if (j >= p->objtotal) {
883ccdc3305SLuigi Rizzo 		D("invalid index %u, max %u", j, p->objtotal);
884f0ea3689SLuigi Rizzo 		return 1;
885ccdc3305SLuigi Rizzo 	}
886f0ea3689SLuigi Rizzo 	ptr = &p->bitmap[j / 32];
887f0ea3689SLuigi Rizzo 	mask = (1 << (j % 32));
888f0ea3689SLuigi Rizzo 	if (*ptr & mask) {
889f0ea3689SLuigi Rizzo 		D("ouch, double free on buffer %d", j);
890f0ea3689SLuigi Rizzo 		return 1;
891f0ea3689SLuigi Rizzo 	} else {
892f0ea3689SLuigi Rizzo 		*ptr |= mask;
893ccdc3305SLuigi Rizzo 		p->objfree++;
894f0ea3689SLuigi Rizzo 		return 0;
895f0ea3689SLuigi Rizzo 	}
896ccdc3305SLuigi Rizzo }
897ccdc3305SLuigi Rizzo 
898f0ea3689SLuigi Rizzo /*
899f0ea3689SLuigi Rizzo  * free by address. This is slow but is only used for a few
900f0ea3689SLuigi Rizzo  * objects (rings, nifp)
901f0ea3689SLuigi Rizzo  */
902ccdc3305SLuigi Rizzo static void
903ccdc3305SLuigi Rizzo netmap_obj_free_va(struct netmap_obj_pool *p, void *vaddr)
904ccdc3305SLuigi Rizzo {
905ce3ee1e7SLuigi Rizzo 	u_int i, j, n = p->numclusters;
906ccdc3305SLuigi Rizzo 
907ce3ee1e7SLuigi Rizzo 	for (i = 0, j = 0; i < n; i++, j += p->_clustentries) {
908ce3ee1e7SLuigi Rizzo 		void *base = p->lut[i * p->_clustentries].vaddr;
909ccdc3305SLuigi Rizzo 		ssize_t relofs = (ssize_t) vaddr - (ssize_t) base;
910ccdc3305SLuigi Rizzo 
911ccdc3305SLuigi Rizzo 		/* Given address, is out of the scope of the current cluster.*/
912ede69cffSLuigi Rizzo 		if (vaddr < base || relofs >= p->_clustsize)
913ccdc3305SLuigi Rizzo 			continue;
914ccdc3305SLuigi Rizzo 
915ccdc3305SLuigi Rizzo 		j = j + relofs / p->_objsize;
916ce3ee1e7SLuigi Rizzo 		/* KASSERT(j != 0, ("Cannot free object 0")); */
917ccdc3305SLuigi Rizzo 		netmap_obj_free(p, j);
918ccdc3305SLuigi Rizzo 		return;
919ccdc3305SLuigi Rizzo 	}
920ae10d1afSLuigi Rizzo 	D("address %p is not contained inside any cluster (%s)",
921ccdc3305SLuigi Rizzo 	    vaddr, p->name);
922ccdc3305SLuigi Rizzo }
923ccdc3305SLuigi Rizzo 
9244bf50f18SLuigi Rizzo #define netmap_mem_bufsize(n)	\
9254bf50f18SLuigi Rizzo 	((n)->pools[NETMAP_BUF_POOL]._objsize)
9264bf50f18SLuigi Rizzo 
927ce3ee1e7SLuigi Rizzo #define netmap_if_malloc(n, len)	netmap_obj_malloc(&(n)->pools[NETMAP_IF_POOL], len, NULL, NULL)
928ce3ee1e7SLuigi Rizzo #define netmap_if_free(n, v)		netmap_obj_free_va(&(n)->pools[NETMAP_IF_POOL], (v))
929ce3ee1e7SLuigi Rizzo #define netmap_ring_malloc(n, len)	netmap_obj_malloc(&(n)->pools[NETMAP_RING_POOL], len, NULL, NULL)
930ce3ee1e7SLuigi Rizzo #define netmap_ring_free(n, v)		netmap_obj_free_va(&(n)->pools[NETMAP_RING_POOL], (v))
931ce3ee1e7SLuigi Rizzo #define netmap_buf_malloc(n, _pos, _index)			\
9324bf50f18SLuigi Rizzo 	netmap_obj_malloc(&(n)->pools[NETMAP_BUF_POOL], netmap_mem_bufsize(n), _pos, _index)
933ccdc3305SLuigi Rizzo 
934ccdc3305SLuigi Rizzo 
935f0ea3689SLuigi Rizzo #if 0 // XXX unused
936ccdc3305SLuigi Rizzo /* Return the index associated to the given packet buffer */
937ce3ee1e7SLuigi Rizzo #define netmap_buf_index(n, v)						\
938ce3ee1e7SLuigi Rizzo     (netmap_obj_offset(&(n)->pools[NETMAP_BUF_POOL], (v)) / NETMAP_BDG_BUF_SIZE(n))
939f0ea3689SLuigi Rizzo #endif
940f0ea3689SLuigi Rizzo 
941f0ea3689SLuigi Rizzo /*
942f0ea3689SLuigi Rizzo  * allocate extra buffers in a linked list.
943f0ea3689SLuigi Rizzo  * returns the actual number.
944f0ea3689SLuigi Rizzo  */
945f0ea3689SLuigi Rizzo uint32_t
946f0ea3689SLuigi Rizzo netmap_extra_alloc(struct netmap_adapter *na, uint32_t *head, uint32_t n)
947f0ea3689SLuigi Rizzo {
948f0ea3689SLuigi Rizzo 	struct netmap_mem_d *nmd = na->nm_mem;
949f0ea3689SLuigi Rizzo 	uint32_t i, pos = 0; /* opaque, scan position in the bitmap */
950f0ea3689SLuigi Rizzo 
951f0ea3689SLuigi Rizzo 	NMA_LOCK(nmd);
952f0ea3689SLuigi Rizzo 
953f0ea3689SLuigi Rizzo 	*head = 0;	/* default, 'null' index ie empty list */
954f0ea3689SLuigi Rizzo 	for (i = 0 ; i < n; i++) {
955f0ea3689SLuigi Rizzo 		uint32_t cur = *head;	/* save current head */
956f0ea3689SLuigi Rizzo 		uint32_t *p = netmap_buf_malloc(nmd, &pos, head);
957f0ea3689SLuigi Rizzo 		if (p == NULL) {
958f0ea3689SLuigi Rizzo 			D("no more buffers after %d of %d", i, n);
959f0ea3689SLuigi Rizzo 			*head = cur; /* restore */
960f0ea3689SLuigi Rizzo 			break;
961f0ea3689SLuigi Rizzo 		}
96237e3a6d3SLuigi Rizzo 		ND(5, "allocate buffer %d -> %d", *head, cur);
963f0ea3689SLuigi Rizzo 		*p = cur; /* link to previous head */
964f0ea3689SLuigi Rizzo 	}
965f0ea3689SLuigi Rizzo 
966f0ea3689SLuigi Rizzo 	NMA_UNLOCK(nmd);
967f0ea3689SLuigi Rizzo 
968f0ea3689SLuigi Rizzo 	return i;
969f0ea3689SLuigi Rizzo }
970f0ea3689SLuigi Rizzo 
971f0ea3689SLuigi Rizzo static void
972f0ea3689SLuigi Rizzo netmap_extra_free(struct netmap_adapter *na, uint32_t head)
973f0ea3689SLuigi Rizzo {
974847bf383SLuigi Rizzo         struct lut_entry *lut = na->na_lut.lut;
975f0ea3689SLuigi Rizzo 	struct netmap_mem_d *nmd = na->nm_mem;
976f0ea3689SLuigi Rizzo 	struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
977f0ea3689SLuigi Rizzo 	uint32_t i, cur, *buf;
978f0ea3689SLuigi Rizzo 
97937e3a6d3SLuigi Rizzo 	ND("freeing the extra list");
980f0ea3689SLuigi Rizzo 	for (i = 0; head >=2 && head < p->objtotal; i++) {
981f0ea3689SLuigi Rizzo 		cur = head;
982f0ea3689SLuigi Rizzo 		buf = lut[head].vaddr;
983f0ea3689SLuigi Rizzo 		head = *buf;
984f0ea3689SLuigi Rizzo 		*buf = 0;
985f0ea3689SLuigi Rizzo 		if (netmap_obj_free(p, cur))
986f0ea3689SLuigi Rizzo 			break;
987f0ea3689SLuigi Rizzo 	}
988f0ea3689SLuigi Rizzo 	if (head != 0)
989f0ea3689SLuigi Rizzo 		D("breaking with head %d", head);
99037e3a6d3SLuigi Rizzo 	if (netmap_verbose)
991f0ea3689SLuigi Rizzo 		D("freed %d buffers", i);
992f0ea3689SLuigi Rizzo }
993ccdc3305SLuigi Rizzo 
994ccdc3305SLuigi Rizzo 
9958241616dSLuigi Rizzo /* Return nonzero on error */
9968241616dSLuigi Rizzo static int
997f9790aebSLuigi Rizzo netmap_new_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n)
998ccdc3305SLuigi Rizzo {
999ce3ee1e7SLuigi Rizzo 	struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
1000ce3ee1e7SLuigi Rizzo 	u_int i = 0;	/* slot counter */
10018241616dSLuigi Rizzo 	uint32_t pos = 0;	/* slot in p->bitmap */
10028241616dSLuigi Rizzo 	uint32_t index = 0;	/* buffer index */
1003ccdc3305SLuigi Rizzo 
1004ccdc3305SLuigi Rizzo 	for (i = 0; i < n; i++) {
1005ce3ee1e7SLuigi Rizzo 		void *vaddr = netmap_buf_malloc(nmd, &pos, &index);
1006ccdc3305SLuigi Rizzo 		if (vaddr == NULL) {
1007f9790aebSLuigi Rizzo 			D("no more buffers after %d of %d", i, n);
1008ccdc3305SLuigi Rizzo 			goto cleanup;
1009ccdc3305SLuigi Rizzo 		}
10108241616dSLuigi Rizzo 		slot[i].buf_idx = index;
1011ccdc3305SLuigi Rizzo 		slot[i].len = p->_objsize;
1012f9790aebSLuigi Rizzo 		slot[i].flags = 0;
1013ccdc3305SLuigi Rizzo 	}
1014ccdc3305SLuigi Rizzo 
10158241616dSLuigi Rizzo 	ND("allocated %d buffers, %d available, first at %d", n, p->objfree, pos);
10168241616dSLuigi Rizzo 	return (0);
1017ccdc3305SLuigi Rizzo 
1018ccdc3305SLuigi Rizzo cleanup:
10194cf8455fSEd Maste 	while (i > 0) {
10204cf8455fSEd Maste 		i--;
10218241616dSLuigi Rizzo 		netmap_obj_free(p, slot[i].buf_idx);
1022ccdc3305SLuigi Rizzo 	}
10238241616dSLuigi Rizzo 	bzero(slot, n * sizeof(slot[0]));
10248241616dSLuigi Rizzo 	return (ENOMEM);
1025ccdc3305SLuigi Rizzo }
1026ccdc3305SLuigi Rizzo 
1027f0ea3689SLuigi Rizzo static void
1028f0ea3689SLuigi Rizzo netmap_mem_set_ring(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n, uint32_t index)
1029f0ea3689SLuigi Rizzo {
1030f0ea3689SLuigi Rizzo 	struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
1031f0ea3689SLuigi Rizzo 	u_int i;
1032f0ea3689SLuigi Rizzo 
1033f0ea3689SLuigi Rizzo 	for (i = 0; i < n; i++) {
1034f0ea3689SLuigi Rizzo 		slot[i].buf_idx = index;
1035f0ea3689SLuigi Rizzo 		slot[i].len = p->_objsize;
1036f0ea3689SLuigi Rizzo 		slot[i].flags = 0;
1037f0ea3689SLuigi Rizzo 	}
1038f0ea3689SLuigi Rizzo }
1039f0ea3689SLuigi Rizzo 
1040ccdc3305SLuigi Rizzo 
1041ccdc3305SLuigi Rizzo static void
1042f9790aebSLuigi Rizzo netmap_free_buf(struct netmap_mem_d *nmd, uint32_t i)
1043ccdc3305SLuigi Rizzo {
1044ce3ee1e7SLuigi Rizzo 	struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
10458241616dSLuigi Rizzo 
1046ccdc3305SLuigi Rizzo 	if (i < 2 || i >= p->objtotal) {
1047ccdc3305SLuigi Rizzo 		D("Cannot free buf#%d: should be in [2, %d[", i, p->objtotal);
1048ccdc3305SLuigi Rizzo 		return;
1049ccdc3305SLuigi Rizzo 	}
10508241616dSLuigi Rizzo 	netmap_obj_free(p, i);
1051ccdc3305SLuigi Rizzo }
1052ccdc3305SLuigi Rizzo 
1053f0ea3689SLuigi Rizzo 
1054f0ea3689SLuigi Rizzo static void
1055f0ea3689SLuigi Rizzo netmap_free_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n)
1056f0ea3689SLuigi Rizzo {
1057f0ea3689SLuigi Rizzo 	u_int i;
1058f0ea3689SLuigi Rizzo 
1059f0ea3689SLuigi Rizzo 	for (i = 0; i < n; i++) {
1060f0ea3689SLuigi Rizzo 		if (slot[i].buf_idx > 2)
1061f0ea3689SLuigi Rizzo 			netmap_free_buf(nmd, slot[i].buf_idx);
1062f0ea3689SLuigi Rizzo 	}
1063f0ea3689SLuigi Rizzo }
1064f0ea3689SLuigi Rizzo 
10658241616dSLuigi Rizzo static void
10668241616dSLuigi Rizzo netmap_reset_obj_allocator(struct netmap_obj_pool *p)
10678241616dSLuigi Rizzo {
1068ce3ee1e7SLuigi Rizzo 
10698241616dSLuigi Rizzo 	if (p == NULL)
10708241616dSLuigi Rizzo 		return;
10718241616dSLuigi Rizzo 	if (p->bitmap)
1072*c3e9b4dbSLuiz Otavio O Souza 		nm_os_free(p->bitmap);
10738241616dSLuigi Rizzo 	p->bitmap = NULL;
10748241616dSLuigi Rizzo 	if (p->lut) {
1075ce3ee1e7SLuigi Rizzo 		u_int i;
1076ce3ee1e7SLuigi Rizzo 
1077dd4fcbc5SPatrick Kelsey 		/*
1078dd4fcbc5SPatrick Kelsey 		 * Free each cluster allocated in
1079dd4fcbc5SPatrick Kelsey 		 * netmap_finalize_obj_allocator().  The cluster start
1080dd4fcbc5SPatrick Kelsey 		 * addresses are stored at multiples of p->_clusterentries
1081dd4fcbc5SPatrick Kelsey 		 * in the lut.
1082dd4fcbc5SPatrick Kelsey 		 */
1083ce3ee1e7SLuigi Rizzo 		for (i = 0; i < p->objtotal; i += p->_clustentries) {
10848241616dSLuigi Rizzo 			if (p->lut[i].vaddr)
108537e3a6d3SLuigi Rizzo 				contigfree(p->lut[i].vaddr, p->_clustsize, M_NETMAP);
10868241616dSLuigi Rizzo 		}
10878241616dSLuigi Rizzo 		bzero(p->lut, sizeof(struct lut_entry) * p->objtotal);
10888241616dSLuigi Rizzo #ifdef linux
10898241616dSLuigi Rizzo 		vfree(p->lut);
10908241616dSLuigi Rizzo #else
1091*c3e9b4dbSLuiz Otavio O Souza 		nm_os_free(p->lut);
10928241616dSLuigi Rizzo #endif
10938241616dSLuigi Rizzo 	}
10948241616dSLuigi Rizzo 	p->lut = NULL;
1095ce3ee1e7SLuigi Rizzo 	p->objtotal = 0;
1096ce3ee1e7SLuigi Rizzo 	p->memtotal = 0;
1097ce3ee1e7SLuigi Rizzo 	p->numclusters = 0;
1098ce3ee1e7SLuigi Rizzo 	p->objfree = 0;
10998241616dSLuigi Rizzo }
1100ccdc3305SLuigi Rizzo 
1101ccdc3305SLuigi Rizzo /*
1102ccdc3305SLuigi Rizzo  * Free all resources related to an allocator.
1103ccdc3305SLuigi Rizzo  */
1104ccdc3305SLuigi Rizzo static void
1105ccdc3305SLuigi Rizzo netmap_destroy_obj_allocator(struct netmap_obj_pool *p)
1106ccdc3305SLuigi Rizzo {
1107ccdc3305SLuigi Rizzo 	if (p == NULL)
1108ccdc3305SLuigi Rizzo 		return;
11098241616dSLuigi Rizzo 	netmap_reset_obj_allocator(p);
1110ccdc3305SLuigi Rizzo }
1111ccdc3305SLuigi Rizzo 
1112ccdc3305SLuigi Rizzo /*
1113ccdc3305SLuigi Rizzo  * We receive a request for objtotal objects, of size objsize each.
1114ccdc3305SLuigi Rizzo  * Internally we may round up both numbers, as we allocate objects
1115ccdc3305SLuigi Rizzo  * in small clusters multiple of the page size.
1116ce3ee1e7SLuigi Rizzo  * We need to keep track of objtotal and clustentries,
1117ccdc3305SLuigi Rizzo  * as they are needed when freeing memory.
1118ccdc3305SLuigi Rizzo  *
1119ccdc3305SLuigi Rizzo  * XXX note -- userspace needs the buffers to be contiguous,
1120ccdc3305SLuigi Rizzo  *	so we cannot afford gaps at the end of a cluster.
1121ccdc3305SLuigi Rizzo  */
11228241616dSLuigi Rizzo 
11238241616dSLuigi Rizzo 
11248241616dSLuigi Rizzo /* call with NMA_LOCK held */
11258241616dSLuigi Rizzo static int
11268241616dSLuigi Rizzo netmap_config_obj_allocator(struct netmap_obj_pool *p, u_int objtotal, u_int objsize)
1127ccdc3305SLuigi Rizzo {
1128ce3ee1e7SLuigi Rizzo 	int i;
1129ccdc3305SLuigi Rizzo 	u_int clustsize;	/* the cluster size, multiple of page size */
1130ccdc3305SLuigi Rizzo 	u_int clustentries;	/* how many objects per entry */
1131ccdc3305SLuigi Rizzo 
1132ce3ee1e7SLuigi Rizzo 	/* we store the current request, so we can
1133ce3ee1e7SLuigi Rizzo 	 * detect configuration changes later */
1134ce3ee1e7SLuigi Rizzo 	p->r_objtotal = objtotal;
1135ce3ee1e7SLuigi Rizzo 	p->r_objsize = objsize;
1136ce3ee1e7SLuigi Rizzo 
11374bf50f18SLuigi Rizzo #define MAX_CLUSTSIZE	(1<<22)		// 4 MB
113817885a7bSLuigi Rizzo #define LINE_ROUND	NM_CACHE_ALIGN	// 64
1139ccdc3305SLuigi Rizzo 	if (objsize >= MAX_CLUSTSIZE) {
1140ccdc3305SLuigi Rizzo 		/* we could do it but there is no point */
1141ccdc3305SLuigi Rizzo 		D("unsupported allocation for %d bytes", objsize);
1142ce3ee1e7SLuigi Rizzo 		return EINVAL;
1143ccdc3305SLuigi Rizzo 	}
1144ccdc3305SLuigi Rizzo 	/* make sure objsize is a multiple of LINE_ROUND */
1145ccdc3305SLuigi Rizzo 	i = (objsize & (LINE_ROUND - 1));
1146ccdc3305SLuigi Rizzo 	if (i) {
1147ccdc3305SLuigi Rizzo 		D("XXX aligning object by %d bytes", LINE_ROUND - i);
1148ccdc3305SLuigi Rizzo 		objsize += LINE_ROUND - i;
1149ccdc3305SLuigi Rizzo 	}
11508241616dSLuigi Rizzo 	if (objsize < p->objminsize || objsize > p->objmaxsize) {
11518241616dSLuigi Rizzo 		D("requested objsize %d out of range [%d, %d]",
11528241616dSLuigi Rizzo 			objsize, p->objminsize, p->objmaxsize);
1153ce3ee1e7SLuigi Rizzo 		return EINVAL;
11548241616dSLuigi Rizzo 	}
11558241616dSLuigi Rizzo 	if (objtotal < p->nummin || objtotal > p->nummax) {
11568241616dSLuigi Rizzo 		D("requested objtotal %d out of range [%d, %d]",
11578241616dSLuigi Rizzo 			objtotal, p->nummin, p->nummax);
1158ce3ee1e7SLuigi Rizzo 		return EINVAL;
11598241616dSLuigi Rizzo 	}
1160ccdc3305SLuigi Rizzo 	/*
1161ccdc3305SLuigi Rizzo 	 * Compute number of objects using a brute-force approach:
1162ccdc3305SLuigi Rizzo 	 * given a max cluster size,
1163ccdc3305SLuigi Rizzo 	 * we try to fill it with objects keeping track of the
1164ccdc3305SLuigi Rizzo 	 * wasted space to the next page boundary.
1165ccdc3305SLuigi Rizzo 	 */
1166ccdc3305SLuigi Rizzo 	for (clustentries = 0, i = 1;; i++) {
1167ccdc3305SLuigi Rizzo 		u_int delta, used = i * objsize;
1168ccdc3305SLuigi Rizzo 		if (used > MAX_CLUSTSIZE)
1169ccdc3305SLuigi Rizzo 			break;
1170ccdc3305SLuigi Rizzo 		delta = used % PAGE_SIZE;
1171ccdc3305SLuigi Rizzo 		if (delta == 0) { // exact solution
1172ccdc3305SLuigi Rizzo 			clustentries = i;
1173ccdc3305SLuigi Rizzo 			break;
1174ccdc3305SLuigi Rizzo 		}
1175ccdc3305SLuigi Rizzo 	}
11764bf50f18SLuigi Rizzo 	/* exact solution not found */
11774bf50f18SLuigi Rizzo 	if (clustentries == 0) {
11784bf50f18SLuigi Rizzo 		D("unsupported allocation for %d bytes", objsize);
11794bf50f18SLuigi Rizzo 		return EINVAL;
11804bf50f18SLuigi Rizzo 	}
11814bf50f18SLuigi Rizzo 	/* compute clustsize */
1182ccdc3305SLuigi Rizzo 	clustsize = clustentries * objsize;
1183ae10d1afSLuigi Rizzo 	if (netmap_verbose)
1184ccdc3305SLuigi Rizzo 		D("objsize %d clustsize %d objects %d",
1185ccdc3305SLuigi Rizzo 			objsize, clustsize, clustentries);
1186ccdc3305SLuigi Rizzo 
1187ccdc3305SLuigi Rizzo 	/*
1188ccdc3305SLuigi Rizzo 	 * The number of clusters is n = ceil(objtotal/clustentries)
1189ccdc3305SLuigi Rizzo 	 * objtotal' = n * clustentries
1190ccdc3305SLuigi Rizzo 	 */
1191ce3ee1e7SLuigi Rizzo 	p->_clustentries = clustentries;
1192ccdc3305SLuigi Rizzo 	p->_clustsize = clustsize;
1193ce3ee1e7SLuigi Rizzo 	p->_numclusters = (objtotal + clustentries - 1) / clustentries;
1194ce3ee1e7SLuigi Rizzo 
1195ce3ee1e7SLuigi Rizzo 	/* actual values (may be larger than requested) */
11968241616dSLuigi Rizzo 	p->_objsize = objsize;
1197ce3ee1e7SLuigi Rizzo 	p->_objtotal = p->_numclusters * clustentries;
1198ccdc3305SLuigi Rizzo 
11998241616dSLuigi Rizzo 	return 0;
12008241616dSLuigi Rizzo }
12018241616dSLuigi Rizzo 
120237e3a6d3SLuigi Rizzo static struct lut_entry *
120337e3a6d3SLuigi Rizzo nm_alloc_lut(u_int nobj)
120437e3a6d3SLuigi Rizzo {
120537e3a6d3SLuigi Rizzo 	size_t n = sizeof(struct lut_entry) * nobj;
120637e3a6d3SLuigi Rizzo 	struct lut_entry *lut;
120737e3a6d3SLuigi Rizzo #ifdef linux
120837e3a6d3SLuigi Rizzo 	lut = vmalloc(n);
120937e3a6d3SLuigi Rizzo #else
1210*c3e9b4dbSLuiz Otavio O Souza 	lut = nm_os_malloc(n);
121137e3a6d3SLuigi Rizzo #endif
121237e3a6d3SLuigi Rizzo 	return lut;
121337e3a6d3SLuigi Rizzo }
12148241616dSLuigi Rizzo 
12158241616dSLuigi Rizzo /* call with NMA_LOCK held */
12168241616dSLuigi Rizzo static int
12178241616dSLuigi Rizzo netmap_finalize_obj_allocator(struct netmap_obj_pool *p)
12188241616dSLuigi Rizzo {
1219ce3ee1e7SLuigi Rizzo 	int i; /* must be signed */
1220ce3ee1e7SLuigi Rizzo 	size_t n;
1221ce3ee1e7SLuigi Rizzo 
1222ce3ee1e7SLuigi Rizzo 	/* optimistically assume we have enough memory */
1223ce3ee1e7SLuigi Rizzo 	p->numclusters = p->_numclusters;
1224ce3ee1e7SLuigi Rizzo 	p->objtotal = p->_objtotal;
12258241616dSLuigi Rizzo 
122637e3a6d3SLuigi Rizzo 	p->lut = nm_alloc_lut(p->objtotal);
1227ccdc3305SLuigi Rizzo 	if (p->lut == NULL) {
122837e3a6d3SLuigi Rizzo 		D("Unable to create lookup table for '%s'", p->name);
1229ccdc3305SLuigi Rizzo 		goto clean;
1230ccdc3305SLuigi Rizzo 	}
1231ccdc3305SLuigi Rizzo 
1232ccdc3305SLuigi Rizzo 	/* Allocate the bitmap */
1233ccdc3305SLuigi Rizzo 	n = (p->objtotal + 31) / 32;
1234*c3e9b4dbSLuiz Otavio O Souza 	p->bitmap = nm_os_malloc(sizeof(uint32_t) * n);
1235ccdc3305SLuigi Rizzo 	if (p->bitmap == NULL) {
1236ce3ee1e7SLuigi Rizzo 		D("Unable to create bitmap (%d entries) for allocator '%s'", (int)n,
12378241616dSLuigi Rizzo 		    p->name);
1238ccdc3305SLuigi Rizzo 		goto clean;
1239ccdc3305SLuigi Rizzo 	}
12408241616dSLuigi Rizzo 	p->bitmap_slots = n;
1241ccdc3305SLuigi Rizzo 
1242ccdc3305SLuigi Rizzo 	/*
1243ccdc3305SLuigi Rizzo 	 * Allocate clusters, init pointers and bitmap
1244ccdc3305SLuigi Rizzo 	 */
1245ce3ee1e7SLuigi Rizzo 
1246ce3ee1e7SLuigi Rizzo 	n = p->_clustsize;
1247ce3ee1e7SLuigi Rizzo 	for (i = 0; i < (int)p->objtotal;) {
1248ce3ee1e7SLuigi Rizzo 		int lim = i + p->_clustentries;
1249ccdc3305SLuigi Rizzo 		char *clust;
1250ccdc3305SLuigi Rizzo 
125137e3a6d3SLuigi Rizzo 		/*
125237e3a6d3SLuigi Rizzo 		 * XXX Note, we only need contigmalloc() for buffers attached
125337e3a6d3SLuigi Rizzo 		 * to native interfaces. In all other cases (nifp, netmap rings
125437e3a6d3SLuigi Rizzo 		 * and even buffers for VALE ports or emulated interfaces) we
125537e3a6d3SLuigi Rizzo 		 * can live with standard malloc, because the hardware will not
125637e3a6d3SLuigi Rizzo 		 * access the pages directly.
125737e3a6d3SLuigi Rizzo 		 */
1258ce3ee1e7SLuigi Rizzo 		clust = contigmalloc(n, M_NETMAP, M_NOWAIT | M_ZERO,
1259ce3ee1e7SLuigi Rizzo 		    (size_t)0, -1UL, PAGE_SIZE, 0);
1260ccdc3305SLuigi Rizzo 		if (clust == NULL) {
1261ccdc3305SLuigi Rizzo 			/*
1262ccdc3305SLuigi Rizzo 			 * If we get here, there is a severe memory shortage,
1263ccdc3305SLuigi Rizzo 			 * so halve the allocated memory to reclaim some.
1264ccdc3305SLuigi Rizzo 			 */
1265ccdc3305SLuigi Rizzo 			D("Unable to create cluster at %d for '%s' allocator",
12668241616dSLuigi Rizzo 			    i, p->name);
1267ce3ee1e7SLuigi Rizzo 			if (i < 2) /* nothing to halve */
1268ce3ee1e7SLuigi Rizzo 				goto out;
1269ccdc3305SLuigi Rizzo 			lim = i / 2;
12708241616dSLuigi Rizzo 			for (i--; i >= lim; i--) {
1271ccdc3305SLuigi Rizzo 				p->bitmap[ (i>>5) ] &=  ~( 1 << (i & 31) );
1272ce3ee1e7SLuigi Rizzo 				if (i % p->_clustentries == 0 && p->lut[i].vaddr)
1273ccdc3305SLuigi Rizzo 					contigfree(p->lut[i].vaddr,
1274ce3ee1e7SLuigi Rizzo 						n, M_NETMAP);
1275dd4fcbc5SPatrick Kelsey 				p->lut[i].vaddr = NULL;
1276ccdc3305SLuigi Rizzo 			}
1277ce3ee1e7SLuigi Rizzo 		out:
1278ccdc3305SLuigi Rizzo 			p->objtotal = i;
1279ce3ee1e7SLuigi Rizzo 			/* we may have stopped in the middle of a cluster */
1280ce3ee1e7SLuigi Rizzo 			p->numclusters = (i + p->_clustentries - 1) / p->_clustentries;
1281ccdc3305SLuigi Rizzo 			break;
1282ccdc3305SLuigi Rizzo 		}
1283dd4fcbc5SPatrick Kelsey 		/*
1284dd4fcbc5SPatrick Kelsey 		 * Set bitmap and lut state for all buffers in the current
1285dd4fcbc5SPatrick Kelsey 		 * cluster.
1286dd4fcbc5SPatrick Kelsey 		 *
1287dd4fcbc5SPatrick Kelsey 		 * [i, lim) is the set of buffer indexes that cover the
1288dd4fcbc5SPatrick Kelsey 		 * current cluster.
1289dd4fcbc5SPatrick Kelsey 		 *
1290dd4fcbc5SPatrick Kelsey 		 * 'clust' is really the address of the current buffer in
1291dd4fcbc5SPatrick Kelsey 		 * the current cluster as we index through it with a stride
1292dd4fcbc5SPatrick Kelsey 		 * of p->_objsize.
1293dd4fcbc5SPatrick Kelsey 		 */
12948241616dSLuigi Rizzo 		for (; i < lim; i++, clust += p->_objsize) {
1295ccdc3305SLuigi Rizzo 			p->bitmap[ (i>>5) ] |=  ( 1 << (i & 31) );
1296ccdc3305SLuigi Rizzo 			p->lut[i].vaddr = clust;
1297ccdc3305SLuigi Rizzo 			p->lut[i].paddr = vtophys(clust);
1298ccdc3305SLuigi Rizzo 		}
1299ccdc3305SLuigi Rizzo 	}
1300ce3ee1e7SLuigi Rizzo 	p->objfree = p->objtotal;
1301ce3ee1e7SLuigi Rizzo 	p->memtotal = p->numclusters * p->_clustsize;
1302ce3ee1e7SLuigi Rizzo 	if (p->objfree == 0)
1303ce3ee1e7SLuigi Rizzo 		goto clean;
1304ae10d1afSLuigi Rizzo 	if (netmap_verbose)
1305ccdc3305SLuigi Rizzo 		D("Pre-allocated %d clusters (%d/%dKB) for '%s'",
1306ce3ee1e7SLuigi Rizzo 		    p->numclusters, p->_clustsize >> 10,
1307ce3ee1e7SLuigi Rizzo 		    p->memtotal >> 10, p->name);
1308ccdc3305SLuigi Rizzo 
13098241616dSLuigi Rizzo 	return 0;
1310ccdc3305SLuigi Rizzo 
1311ccdc3305SLuigi Rizzo clean:
13128241616dSLuigi Rizzo 	netmap_reset_obj_allocator(p);
13138241616dSLuigi Rizzo 	return ENOMEM;
13148241616dSLuigi Rizzo }
13158241616dSLuigi Rizzo 
13168241616dSLuigi Rizzo /* call with lock held */
13178241616dSLuigi Rizzo static int
1318*c3e9b4dbSLuiz Otavio O Souza netmap_mem_params_changed(struct netmap_obj_params* p)
13198241616dSLuigi Rizzo {
1320*c3e9b4dbSLuiz Otavio O Souza 	int i, rv = 0;
13218241616dSLuigi Rizzo 
13228241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1323*c3e9b4dbSLuiz Otavio O Souza 		if (p[i].last_size != p[i].size || p[i].last_num != p[i].num) {
1324*c3e9b4dbSLuiz Otavio O Souza 			p[i].last_size = p[i].size;
1325*c3e9b4dbSLuiz Otavio O Souza 			p[i].last_num = p[i].num;
1326*c3e9b4dbSLuiz Otavio O Souza 			rv = 1;
13278241616dSLuigi Rizzo 		}
1328*c3e9b4dbSLuiz Otavio O Souza 	}
1329*c3e9b4dbSLuiz Otavio O Souza 	return rv;
13308241616dSLuigi Rizzo }
13318241616dSLuigi Rizzo 
1332ce3ee1e7SLuigi Rizzo static void
1333ce3ee1e7SLuigi Rizzo netmap_mem_reset_all(struct netmap_mem_d *nmd)
1334ce3ee1e7SLuigi Rizzo {
1335ce3ee1e7SLuigi Rizzo 	int i;
1336f0ea3689SLuigi Rizzo 
1337f0ea3689SLuigi Rizzo 	if (netmap_verbose)
1338ce3ee1e7SLuigi Rizzo 		D("resetting %p", nmd);
1339ce3ee1e7SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1340ce3ee1e7SLuigi Rizzo 		netmap_reset_obj_allocator(&nmd->pools[i]);
1341ce3ee1e7SLuigi Rizzo 	}
1342ce3ee1e7SLuigi Rizzo 	nmd->flags  &= ~NETMAP_MEM_FINALIZED;
1343ce3ee1e7SLuigi Rizzo }
1344ce3ee1e7SLuigi Rizzo 
1345ce3ee1e7SLuigi Rizzo static int
13464bf50f18SLuigi Rizzo netmap_mem_unmap(struct netmap_obj_pool *p, struct netmap_adapter *na)
13474bf50f18SLuigi Rizzo {
13484bf50f18SLuigi Rizzo 	int i, lim = p->_objtotal;
13494bf50f18SLuigi Rizzo 
1350*c3e9b4dbSLuiz Otavio O Souza 	if (na == NULL || na->pdev == NULL)
13514bf50f18SLuigi Rizzo 		return 0;
13524bf50f18SLuigi Rizzo 
135337e3a6d3SLuigi Rizzo #if defined(__FreeBSD__)
13544bf50f18SLuigi Rizzo 	(void)i;
13554bf50f18SLuigi Rizzo 	(void)lim;
13564bf50f18SLuigi Rizzo 	D("unsupported on FreeBSD");
135737e3a6d3SLuigi Rizzo 
135837e3a6d3SLuigi Rizzo #elif defined(_WIN32)
135937e3a6d3SLuigi Rizzo 	(void)i;
136037e3a6d3SLuigi Rizzo 	(void)lim;
136137e3a6d3SLuigi Rizzo 	D("unsupported on Windows");	//XXX_ale, really?
13624bf50f18SLuigi Rizzo #else /* linux */
13634bf50f18SLuigi Rizzo 	for (i = 2; i < lim; i++) {
13644bf50f18SLuigi Rizzo 		netmap_unload_map(na, (bus_dma_tag_t) na->pdev, &p->lut[i].paddr);
13654bf50f18SLuigi Rizzo 	}
13664bf50f18SLuigi Rizzo #endif /* linux */
13674bf50f18SLuigi Rizzo 
13684bf50f18SLuigi Rizzo 	return 0;
13694bf50f18SLuigi Rizzo }
13704bf50f18SLuigi Rizzo 
13714bf50f18SLuigi Rizzo static int
13724bf50f18SLuigi Rizzo netmap_mem_map(struct netmap_obj_pool *p, struct netmap_adapter *na)
13734bf50f18SLuigi Rizzo {
137437e3a6d3SLuigi Rizzo #if defined(__FreeBSD__)
13754bf50f18SLuigi Rizzo 	D("unsupported on FreeBSD");
137637e3a6d3SLuigi Rizzo #elif defined(_WIN32)
137737e3a6d3SLuigi Rizzo 	D("unsupported on Windows");	//XXX_ale, really?
13784bf50f18SLuigi Rizzo #else /* linux */
13794bf50f18SLuigi Rizzo 	int i, lim = p->_objtotal;
13804bf50f18SLuigi Rizzo 
13814bf50f18SLuigi Rizzo 	if (na->pdev == NULL)
13824bf50f18SLuigi Rizzo 		return 0;
13834bf50f18SLuigi Rizzo 
13844bf50f18SLuigi Rizzo 	for (i = 2; i < lim; i++) {
13854bf50f18SLuigi Rizzo 		netmap_load_map(na, (bus_dma_tag_t) na->pdev, &p->lut[i].paddr,
13864bf50f18SLuigi Rizzo 				p->lut[i].vaddr);
13874bf50f18SLuigi Rizzo 	}
13884bf50f18SLuigi Rizzo #endif /* linux */
13894bf50f18SLuigi Rizzo 
13904bf50f18SLuigi Rizzo 	return 0;
13914bf50f18SLuigi Rizzo }
13924bf50f18SLuigi Rizzo 
13934bf50f18SLuigi Rizzo static int
1394ce3ee1e7SLuigi Rizzo netmap_mem_finalize_all(struct netmap_mem_d *nmd)
1395ce3ee1e7SLuigi Rizzo {
1396ce3ee1e7SLuigi Rizzo 	int i;
1397ce3ee1e7SLuigi Rizzo 	if (nmd->flags & NETMAP_MEM_FINALIZED)
1398ce3ee1e7SLuigi Rizzo 		return 0;
1399ce3ee1e7SLuigi Rizzo 	nmd->lasterr = 0;
1400ce3ee1e7SLuigi Rizzo 	nmd->nm_totalsize = 0;
1401ce3ee1e7SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1402ce3ee1e7SLuigi Rizzo 		nmd->lasterr = netmap_finalize_obj_allocator(&nmd->pools[i]);
1403ce3ee1e7SLuigi Rizzo 		if (nmd->lasterr)
1404ce3ee1e7SLuigi Rizzo 			goto error;
1405ce3ee1e7SLuigi Rizzo 		nmd->nm_totalsize += nmd->pools[i].memtotal;
1406ce3ee1e7SLuigi Rizzo 	}
1407ce3ee1e7SLuigi Rizzo 	/* buffers 0 and 1 are reserved */
1408ce3ee1e7SLuigi Rizzo 	nmd->pools[NETMAP_BUF_POOL].objfree -= 2;
1409ce3ee1e7SLuigi Rizzo 	nmd->pools[NETMAP_BUF_POOL].bitmap[0] = ~3;
1410ce3ee1e7SLuigi Rizzo 	nmd->flags |= NETMAP_MEM_FINALIZED;
1411ce3ee1e7SLuigi Rizzo 
1412f0ea3689SLuigi Rizzo 	if (netmap_verbose)
1413f0ea3689SLuigi Rizzo 		D("interfaces %d KB, rings %d KB, buffers %d MB",
1414ce3ee1e7SLuigi Rizzo 		    nmd->pools[NETMAP_IF_POOL].memtotal >> 10,
1415ce3ee1e7SLuigi Rizzo 		    nmd->pools[NETMAP_RING_POOL].memtotal >> 10,
1416ce3ee1e7SLuigi Rizzo 		    nmd->pools[NETMAP_BUF_POOL].memtotal >> 20);
1417ce3ee1e7SLuigi Rizzo 
1418f0ea3689SLuigi Rizzo 	if (netmap_verbose)
1419ce3ee1e7SLuigi Rizzo 		D("Free buffers: %d", nmd->pools[NETMAP_BUF_POOL].objfree);
1420ce3ee1e7SLuigi Rizzo 
1421ce3ee1e7SLuigi Rizzo 
1422ce3ee1e7SLuigi Rizzo 	return 0;
1423ce3ee1e7SLuigi Rizzo error:
1424ce3ee1e7SLuigi Rizzo 	netmap_mem_reset_all(nmd);
1425ce3ee1e7SLuigi Rizzo 	return nmd->lasterr;
1426ce3ee1e7SLuigi Rizzo }
1427ce3ee1e7SLuigi Rizzo 
1428f0ea3689SLuigi Rizzo /*
1429f0ea3689SLuigi Rizzo  * allocator for private memory
1430f0ea3689SLuigi Rizzo  */
1431*c3e9b4dbSLuiz Otavio O Souza static struct netmap_mem_d *
1432*c3e9b4dbSLuiz Otavio O Souza _netmap_mem_private_new(struct netmap_obj_params *p, int *perr)
1433ce3ee1e7SLuigi Rizzo {
1434ce3ee1e7SLuigi Rizzo 	struct netmap_mem_d *d = NULL;
1435*c3e9b4dbSLuiz Otavio O Souza 	int i, err = 0;
1436ce3ee1e7SLuigi Rizzo 
1437*c3e9b4dbSLuiz Otavio O Souza 	d = nm_os_malloc(sizeof(struct netmap_mem_d));
1438f0ea3689SLuigi Rizzo 	if (d == NULL) {
1439f0ea3689SLuigi Rizzo 		err = ENOMEM;
1440f0ea3689SLuigi Rizzo 		goto error;
1441f0ea3689SLuigi Rizzo 	}
1442ce3ee1e7SLuigi Rizzo 
1443ce3ee1e7SLuigi Rizzo 	*d = nm_blueprint;
1444ce3ee1e7SLuigi Rizzo 
1445f0ea3689SLuigi Rizzo 	err = nm_mem_assign_id(d);
1446f0ea3689SLuigi Rizzo 	if (err)
1447f0ea3689SLuigi Rizzo 		goto error;
1448*c3e9b4dbSLuiz Otavio O Souza 	snprintf(d->name, NM_MEM_NAMESZ, "%d", d->nm_id);
1449f0ea3689SLuigi Rizzo 
1450*c3e9b4dbSLuiz Otavio O Souza 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1451*c3e9b4dbSLuiz Otavio O Souza 		snprintf(d->pools[i].name, NETMAP_POOL_MAX_NAMSZ,
1452*c3e9b4dbSLuiz Otavio O Souza 				nm_blueprint.pools[i].name,
1453*c3e9b4dbSLuiz Otavio O Souza 				d->name);
1454*c3e9b4dbSLuiz Otavio O Souza 		d->params[i].num = p[i].num;
1455*c3e9b4dbSLuiz Otavio O Souza 		d->params[i].size = p[i].size;
1456*c3e9b4dbSLuiz Otavio O Souza 	}
1457*c3e9b4dbSLuiz Otavio O Souza 
1458*c3e9b4dbSLuiz Otavio O Souza 	NMA_LOCK_INIT(d);
1459*c3e9b4dbSLuiz Otavio O Souza 
1460*c3e9b4dbSLuiz Otavio O Souza 	err = netmap_mem_config(d);
1461*c3e9b4dbSLuiz Otavio O Souza 	if (err)
1462*c3e9b4dbSLuiz Otavio O Souza 		goto error;
1463*c3e9b4dbSLuiz Otavio O Souza 
1464*c3e9b4dbSLuiz Otavio O Souza 	d->flags &= ~NETMAP_MEM_FINALIZED;
1465*c3e9b4dbSLuiz Otavio O Souza 
1466*c3e9b4dbSLuiz Otavio O Souza 	return d;
1467*c3e9b4dbSLuiz Otavio O Souza 
1468*c3e9b4dbSLuiz Otavio O Souza error:
1469*c3e9b4dbSLuiz Otavio O Souza 	netmap_mem_delete(d);
1470*c3e9b4dbSLuiz Otavio O Souza 	if (perr)
1471*c3e9b4dbSLuiz Otavio O Souza 		*perr = err;
1472*c3e9b4dbSLuiz Otavio O Souza 	return NULL;
1473*c3e9b4dbSLuiz Otavio O Souza }
1474*c3e9b4dbSLuiz Otavio O Souza 
1475*c3e9b4dbSLuiz Otavio O Souza struct netmap_mem_d *
1476*c3e9b4dbSLuiz Otavio O Souza netmap_mem_private_new(u_int txr, u_int txd, u_int rxr, u_int rxd,
1477*c3e9b4dbSLuiz Otavio O Souza 		u_int extra_bufs, u_int npipes, int *perr)
1478*c3e9b4dbSLuiz Otavio O Souza {
1479*c3e9b4dbSLuiz Otavio O Souza 	struct netmap_mem_d *d = NULL;
1480*c3e9b4dbSLuiz Otavio O Souza 	struct netmap_obj_params p[NETMAP_POOLS_NR];
1481*c3e9b4dbSLuiz Otavio O Souza 	int i, err = 0;
1482*c3e9b4dbSLuiz Otavio O Souza 	u_int v, maxd;
1483f0ea3689SLuigi Rizzo 	/* account for the fake host rings */
1484ce3ee1e7SLuigi Rizzo 	txr++;
1485ce3ee1e7SLuigi Rizzo 	rxr++;
1486ce3ee1e7SLuigi Rizzo 
1487f0ea3689SLuigi Rizzo 	/* copy the min values */
1488f0ea3689SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1489f0ea3689SLuigi Rizzo 		p[i] = netmap_min_priv_params[i];
1490f0ea3689SLuigi Rizzo 	}
1491f0ea3689SLuigi Rizzo 
1492f0ea3689SLuigi Rizzo 	/* possibly increase them to fit user request */
1493f0ea3689SLuigi Rizzo 	v = sizeof(struct netmap_if) + sizeof(ssize_t) * (txr + rxr);
1494f0ea3689SLuigi Rizzo 	if (p[NETMAP_IF_POOL].size < v)
1495f0ea3689SLuigi Rizzo 		p[NETMAP_IF_POOL].size = v;
1496f0ea3689SLuigi Rizzo 	v = 2 + 4 * npipes;
1497f0ea3689SLuigi Rizzo 	if (p[NETMAP_IF_POOL].num < v)
1498f0ea3689SLuigi Rizzo 		p[NETMAP_IF_POOL].num = v;
1499f0ea3689SLuigi Rizzo 	maxd = (txd > rxd) ? txd : rxd;
1500f0ea3689SLuigi Rizzo 	v = sizeof(struct netmap_ring) + sizeof(struct netmap_slot) * maxd;
1501f0ea3689SLuigi Rizzo 	if (p[NETMAP_RING_POOL].size < v)
1502f0ea3689SLuigi Rizzo 		p[NETMAP_RING_POOL].size = v;
1503f0ea3689SLuigi Rizzo 	/* each pipe endpoint needs two tx rings (1 normal + 1 host, fake)
1504f0ea3689SLuigi Rizzo          * and two rx rings (again, 1 normal and 1 fake host)
1505f0ea3689SLuigi Rizzo          */
1506f0ea3689SLuigi Rizzo 	v = txr + rxr + 8 * npipes;
1507f0ea3689SLuigi Rizzo 	if (p[NETMAP_RING_POOL].num < v)
1508f0ea3689SLuigi Rizzo 		p[NETMAP_RING_POOL].num = v;
1509f0ea3689SLuigi Rizzo 	/* for each pipe we only need the buffers for the 4 "real" rings.
1510f0ea3689SLuigi Rizzo          * On the other end, the pipe ring dimension may be different from
1511f0ea3689SLuigi Rizzo          * the parent port ring dimension. As a compromise, we allocate twice the
1512f0ea3689SLuigi Rizzo          * space actually needed if the pipe rings were the same size as the parent rings
1513f0ea3689SLuigi Rizzo          */
1514f0ea3689SLuigi Rizzo 	v = (4 * npipes + rxr) * rxd + (4 * npipes + txr) * txd + 2 + extra_bufs;
1515f0ea3689SLuigi Rizzo 		/* the +2 is for the tx and rx fake buffers (indices 0 and 1) */
1516f0ea3689SLuigi Rizzo 	if (p[NETMAP_BUF_POOL].num < v)
1517f0ea3689SLuigi Rizzo 		p[NETMAP_BUF_POOL].num = v;
1518f0ea3689SLuigi Rizzo 
1519f0ea3689SLuigi Rizzo 	if (netmap_verbose)
1520ce3ee1e7SLuigi Rizzo 		D("req if %d*%d ring %d*%d buf %d*%d",
1521ce3ee1e7SLuigi Rizzo 			p[NETMAP_IF_POOL].num,
1522ce3ee1e7SLuigi Rizzo 			p[NETMAP_IF_POOL].size,
1523ce3ee1e7SLuigi Rizzo 			p[NETMAP_RING_POOL].num,
1524ce3ee1e7SLuigi Rizzo 			p[NETMAP_RING_POOL].size,
1525ce3ee1e7SLuigi Rizzo 			p[NETMAP_BUF_POOL].num,
1526ce3ee1e7SLuigi Rizzo 			p[NETMAP_BUF_POOL].size);
1527ce3ee1e7SLuigi Rizzo 
1528*c3e9b4dbSLuiz Otavio O Souza 	d = _netmap_mem_private_new(p, perr);
1529*c3e9b4dbSLuiz Otavio O Souza 	if (d == NULL)
1530ce3ee1e7SLuigi Rizzo 		goto error;
1531ce3ee1e7SLuigi Rizzo 
1532ce3ee1e7SLuigi Rizzo 	return d;
1533ce3ee1e7SLuigi Rizzo error:
1534*c3e9b4dbSLuiz Otavio O Souza 	netmap_mem_delete(d);
1535f0ea3689SLuigi Rizzo 	if (perr)
1536f0ea3689SLuigi Rizzo 		*perr = err;
1537ce3ee1e7SLuigi Rizzo 	return NULL;
1538ce3ee1e7SLuigi Rizzo }
1539ce3ee1e7SLuigi Rizzo 
15408241616dSLuigi Rizzo 
15418241616dSLuigi Rizzo /* call with lock held */
15428241616dSLuigi Rizzo static int
1543*c3e9b4dbSLuiz Otavio O Souza netmap_mem2_config(struct netmap_mem_d *nmd)
15448241616dSLuigi Rizzo {
15458241616dSLuigi Rizzo 	int i;
15468241616dSLuigi Rizzo 
1547847bf383SLuigi Rizzo 	if (nmd->active)
1548ce3ee1e7SLuigi Rizzo 		/* already in use, we cannot change the configuration */
1549ce3ee1e7SLuigi Rizzo 		goto out;
1550ce3ee1e7SLuigi Rizzo 
1551*c3e9b4dbSLuiz Otavio O Souza 	if (!netmap_mem_params_changed(nmd->params))
15528241616dSLuigi Rizzo 		goto out;
15538241616dSLuigi Rizzo 
1554847bf383SLuigi Rizzo 	ND("reconfiguring");
15558241616dSLuigi Rizzo 
1556ce3ee1e7SLuigi Rizzo 	if (nmd->flags & NETMAP_MEM_FINALIZED) {
15578241616dSLuigi Rizzo 		/* reset previous allocation */
15588241616dSLuigi Rizzo 		for (i = 0; i < NETMAP_POOLS_NR; i++) {
1559ce3ee1e7SLuigi Rizzo 			netmap_reset_obj_allocator(&nmd->pools[i]);
15608241616dSLuigi Rizzo 		}
1561ce3ee1e7SLuigi Rizzo 		nmd->flags &= ~NETMAP_MEM_FINALIZED;
15628241616dSLuigi Rizzo 	}
15638241616dSLuigi Rizzo 
15648241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1565ce3ee1e7SLuigi Rizzo 		nmd->lasterr = netmap_config_obj_allocator(&nmd->pools[i],
1566*c3e9b4dbSLuiz Otavio O Souza 				nmd->params[i].num, nmd->params[i].size);
1567ce3ee1e7SLuigi Rizzo 		if (nmd->lasterr)
15688241616dSLuigi Rizzo 			goto out;
15698241616dSLuigi Rizzo 	}
15708241616dSLuigi Rizzo 
15718241616dSLuigi Rizzo out:
15728241616dSLuigi Rizzo 
1573ce3ee1e7SLuigi Rizzo 	return nmd->lasterr;
15748241616dSLuigi Rizzo }
15758241616dSLuigi Rizzo 
15768241616dSLuigi Rizzo static int
1577*c3e9b4dbSLuiz Otavio O Souza netmap_mem2_finalize(struct netmap_mem_d *nmd)
15788241616dSLuigi Rizzo {
1579ce3ee1e7SLuigi Rizzo 	int err;
15808241616dSLuigi Rizzo 
15818241616dSLuigi Rizzo 	/* update configuration if changed */
1582*c3e9b4dbSLuiz Otavio O Souza 	if (netmap_mem2_config(nmd))
1583*c3e9b4dbSLuiz Otavio O Souza 		goto out1;
15848241616dSLuigi Rizzo 
1585847bf383SLuigi Rizzo 	nmd->active++;
1586ce3ee1e7SLuigi Rizzo 
1587ce3ee1e7SLuigi Rizzo 	if (nmd->flags & NETMAP_MEM_FINALIZED) {
15888241616dSLuigi Rizzo 		/* may happen if config is not changed */
15898241616dSLuigi Rizzo 		ND("nothing to do");
15908241616dSLuigi Rizzo 		goto out;
15918241616dSLuigi Rizzo 	}
15928241616dSLuigi Rizzo 
1593ce3ee1e7SLuigi Rizzo 	if (netmap_mem_finalize_all(nmd))
1594ce3ee1e7SLuigi Rizzo 		goto out;
15958241616dSLuigi Rizzo 
1596ce3ee1e7SLuigi Rizzo 	nmd->lasterr = 0;
15978241616dSLuigi Rizzo 
15988241616dSLuigi Rizzo out:
1599ce3ee1e7SLuigi Rizzo 	if (nmd->lasterr)
1600847bf383SLuigi Rizzo 		nmd->active--;
1601*c3e9b4dbSLuiz Otavio O Souza out1:
1602ce3ee1e7SLuigi Rizzo 	err = nmd->lasterr;
16038241616dSLuigi Rizzo 
1604ce3ee1e7SLuigi Rizzo 	return err;
16058241616dSLuigi Rizzo 
1606ccdc3305SLuigi Rizzo }
1607ccdc3305SLuigi Rizzo 
1608847bf383SLuigi Rizzo static void
1609*c3e9b4dbSLuiz Otavio O Souza netmap_mem2_delete(struct netmap_mem_d *nmd)
1610ccdc3305SLuigi Rizzo {
16118241616dSLuigi Rizzo 	int i;
16128241616dSLuigi Rizzo 
16138241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1614*c3e9b4dbSLuiz Otavio O Souza 	    netmap_destroy_obj_allocator(&nmd->pools[i]);
16158241616dSLuigi Rizzo 	}
1616847bf383SLuigi Rizzo 
1617*c3e9b4dbSLuiz Otavio O Souza 	NMA_LOCK_DESTROY(nmd);
1618*c3e9b4dbSLuiz Otavio O Souza 	if (nmd != &nm_mem)
1619*c3e9b4dbSLuiz Otavio O Souza 		nm_os_free(nmd);
16208241616dSLuigi Rizzo }
16218241616dSLuigi Rizzo 
1622847bf383SLuigi Rizzo int
1623847bf383SLuigi Rizzo netmap_mem_init(void)
1624847bf383SLuigi Rizzo {
1625*c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_INIT(nm_mem_list_lock);
1626847bf383SLuigi Rizzo 	NMA_LOCK_INIT(&nm_mem);
1627847bf383SLuigi Rizzo 	netmap_mem_get(&nm_mem);
1628847bf383SLuigi Rizzo 	return (0);
1629847bf383SLuigi Rizzo }
1630847bf383SLuigi Rizzo 
1631847bf383SLuigi Rizzo void
1632847bf383SLuigi Rizzo netmap_mem_fini(void)
1633847bf383SLuigi Rizzo {
1634847bf383SLuigi Rizzo 	netmap_mem_put(&nm_mem);
1635847bf383SLuigi Rizzo }
1636847bf383SLuigi Rizzo 
16378241616dSLuigi Rizzo static void
16388241616dSLuigi Rizzo netmap_free_rings(struct netmap_adapter *na)
16398241616dSLuigi Rizzo {
1640847bf383SLuigi Rizzo 	enum txrx t;
1641847bf383SLuigi Rizzo 
1642847bf383SLuigi Rizzo 	for_rx_tx(t) {
1643847bf383SLuigi Rizzo 		u_int i;
164437e3a6d3SLuigi Rizzo 		for (i = 0; i < nma_get_nrings(na, t) + 1; i++) {
1645847bf383SLuigi Rizzo 			struct netmap_kring *kring = &NMR(na, t)[i];
1646847bf383SLuigi Rizzo 			struct netmap_ring *ring = kring->ring;
1647847bf383SLuigi Rizzo 
164837e3a6d3SLuigi Rizzo 			if (ring == NULL || kring->users > 0 || (kring->nr_kflags & NKR_NEEDRING)) {
164937e3a6d3SLuigi Rizzo 				ND("skipping ring %s (ring %p, users %d)",
165037e3a6d3SLuigi Rizzo 						kring->name, ring, kring->users);
1651f0ea3689SLuigi Rizzo 				continue;
165237e3a6d3SLuigi Rizzo 			}
165337e3a6d3SLuigi Rizzo 			if (i != nma_get_nrings(na, t) || na->na_flags & NAF_HOST_RINGS)
1654f0ea3689SLuigi Rizzo 				netmap_free_bufs(na->nm_mem, ring->slot, kring->nkr_num_slots);
1655f0ea3689SLuigi Rizzo 			netmap_ring_free(na->nm_mem, ring);
1656f0ea3689SLuigi Rizzo 			kring->ring = NULL;
16578241616dSLuigi Rizzo 		}
1658ce3ee1e7SLuigi Rizzo 	}
1659ccdc3305SLuigi Rizzo }
1660ccdc3305SLuigi Rizzo 
1661f9790aebSLuigi Rizzo /* call with NMA_LOCK held *
1662f9790aebSLuigi Rizzo  *
1663f9790aebSLuigi Rizzo  * Allocate netmap rings and buffers for this card
1664f9790aebSLuigi Rizzo  * The rings are contiguous, but have variable size.
1665f0ea3689SLuigi Rizzo  * The kring array must follow the layout described
1666f0ea3689SLuigi Rizzo  * in netmap_krings_create().
1667f9790aebSLuigi Rizzo  */
1668847bf383SLuigi Rizzo static int
1669847bf383SLuigi Rizzo netmap_mem2_rings_create(struct netmap_adapter *na)
1670f9790aebSLuigi Rizzo {
1671847bf383SLuigi Rizzo 	enum txrx t;
1672f9790aebSLuigi Rizzo 
1673f9790aebSLuigi Rizzo 	NMA_LOCK(na->nm_mem);
1674f9790aebSLuigi Rizzo 
1675847bf383SLuigi Rizzo 	for_rx_tx(t) {
1676847bf383SLuigi Rizzo 		u_int i;
1677847bf383SLuigi Rizzo 
1678847bf383SLuigi Rizzo 		for (i = 0; i <= nma_get_nrings(na, t); i++) {
1679847bf383SLuigi Rizzo 			struct netmap_kring *kring = &NMR(na, t)[i];
1680847bf383SLuigi Rizzo 			struct netmap_ring *ring = kring->ring;
1681847bf383SLuigi Rizzo 			u_int len, ndesc;
1682847bf383SLuigi Rizzo 
168337e3a6d3SLuigi Rizzo 			if (ring || (!kring->users && !(kring->nr_kflags & NKR_NEEDRING))) {
168437e3a6d3SLuigi Rizzo 				/* uneeded, or already created by somebody else */
168537e3a6d3SLuigi Rizzo 				ND("skipping ring %s", kring->name);
168637e3a6d3SLuigi Rizzo 				continue;
1687f0ea3689SLuigi Rizzo 			}
1688f9790aebSLuigi Rizzo 			ndesc = kring->nkr_num_slots;
1689f9790aebSLuigi Rizzo 			len = sizeof(struct netmap_ring) +
1690f9790aebSLuigi Rizzo 				  ndesc * sizeof(struct netmap_slot);
1691f9790aebSLuigi Rizzo 			ring = netmap_ring_malloc(na->nm_mem, len);
1692f9790aebSLuigi Rizzo 			if (ring == NULL) {
1693847bf383SLuigi Rizzo 				D("Cannot allocate %s_ring", nm_txrx2str(t));
1694f9790aebSLuigi Rizzo 				goto cleanup;
1695f9790aebSLuigi Rizzo 			}
1696f6c2a31fSLuigi Rizzo 			ND("txring at %p", ring);
1697f9790aebSLuigi Rizzo 			kring->ring = ring;
1698f9790aebSLuigi Rizzo 			*(uint32_t *)(uintptr_t)&ring->num_slots = ndesc;
169917885a7bSLuigi Rizzo 			*(int64_t *)(uintptr_t)&ring->buf_ofs =
1700f9790aebSLuigi Rizzo 			    (na->nm_mem->pools[NETMAP_IF_POOL].memtotal +
1701f9790aebSLuigi Rizzo 				na->nm_mem->pools[NETMAP_RING_POOL].memtotal) -
1702f9790aebSLuigi Rizzo 				netmap_ring_offset(na->nm_mem, ring);
1703f9790aebSLuigi Rizzo 
170417885a7bSLuigi Rizzo 			/* copy values from kring */
170517885a7bSLuigi Rizzo 			ring->head = kring->rhead;
170617885a7bSLuigi Rizzo 			ring->cur = kring->rcur;
170717885a7bSLuigi Rizzo 			ring->tail = kring->rtail;
1708f9790aebSLuigi Rizzo 			*(uint16_t *)(uintptr_t)&ring->nr_buf_size =
17094bf50f18SLuigi Rizzo 				netmap_mem_bufsize(na->nm_mem);
1710f0ea3689SLuigi Rizzo 			ND("%s h %d c %d t %d", kring->name,
1711f0ea3689SLuigi Rizzo 				ring->head, ring->cur, ring->tail);
1712847bf383SLuigi Rizzo 			ND("initializing slots for %s_ring", nm_txrx2str(txrx));
1713847bf383SLuigi Rizzo 			if (i != nma_get_nrings(na, t) || (na->na_flags & NAF_HOST_RINGS)) {
1714f0ea3689SLuigi Rizzo 				/* this is a real ring */
1715f9790aebSLuigi Rizzo 				if (netmap_new_bufs(na->nm_mem, ring->slot, ndesc)) {
1716847bf383SLuigi Rizzo 					D("Cannot allocate buffers for %s_ring", nm_txrx2str(t));
1717f9790aebSLuigi Rizzo 					goto cleanup;
1718f9790aebSLuigi Rizzo 				}
1719f0ea3689SLuigi Rizzo 			} else {
1720847bf383SLuigi Rizzo 				/* this is a fake ring, set all indices to 0 */
1721f0ea3689SLuigi Rizzo 				netmap_mem_set_ring(na->nm_mem, ring->slot, ndesc, 0);
1722f0ea3689SLuigi Rizzo 			}
1723847bf383SLuigi Rizzo 		        /* ring info */
1724847bf383SLuigi Rizzo 		        *(uint16_t *)(uintptr_t)&ring->ringid = kring->ring_id;
1725847bf383SLuigi Rizzo 		        *(uint16_t *)(uintptr_t)&ring->dir = kring->tx;
1726f0ea3689SLuigi Rizzo 		}
1727f9790aebSLuigi Rizzo 	}
1728f9790aebSLuigi Rizzo 
1729f9790aebSLuigi Rizzo 	NMA_UNLOCK(na->nm_mem);
1730f9790aebSLuigi Rizzo 
1731f9790aebSLuigi Rizzo 	return 0;
1732f9790aebSLuigi Rizzo 
1733f9790aebSLuigi Rizzo cleanup:
1734f9790aebSLuigi Rizzo 	netmap_free_rings(na);
1735f9790aebSLuigi Rizzo 
1736f9790aebSLuigi Rizzo 	NMA_UNLOCK(na->nm_mem);
1737f9790aebSLuigi Rizzo 
1738f9790aebSLuigi Rizzo 	return ENOMEM;
1739f9790aebSLuigi Rizzo }
1740f9790aebSLuigi Rizzo 
1741847bf383SLuigi Rizzo static void
1742847bf383SLuigi Rizzo netmap_mem2_rings_delete(struct netmap_adapter *na)
1743f9790aebSLuigi Rizzo {
1744f9790aebSLuigi Rizzo 	/* last instance, release bufs and rings */
1745f9790aebSLuigi Rizzo 	NMA_LOCK(na->nm_mem);
1746f9790aebSLuigi Rizzo 
1747f9790aebSLuigi Rizzo 	netmap_free_rings(na);
1748f9790aebSLuigi Rizzo 
1749f9790aebSLuigi Rizzo 	NMA_UNLOCK(na->nm_mem);
1750f9790aebSLuigi Rizzo }
1751ccdc3305SLuigi Rizzo 
1752ccdc3305SLuigi Rizzo 
17538241616dSLuigi Rizzo /* call with NMA_LOCK held */
1754ae10d1afSLuigi Rizzo /*
1755ae10d1afSLuigi Rizzo  * Allocate the per-fd structure netmap_if.
1756ce3ee1e7SLuigi Rizzo  *
1757ce3ee1e7SLuigi Rizzo  * We assume that the configuration stored in na
1758ce3ee1e7SLuigi Rizzo  * (number of tx/rx rings and descs) does not change while
1759ce3ee1e7SLuigi Rizzo  * the interface is in netmap mode.
1760ae10d1afSLuigi Rizzo  */
1761847bf383SLuigi Rizzo static struct netmap_if *
1762*c3e9b4dbSLuiz Otavio O Souza netmap_mem2_if_new(struct netmap_adapter *na, struct netmap_priv_d *priv)
1763ccdc3305SLuigi Rizzo {
1764ccdc3305SLuigi Rizzo 	struct netmap_if *nifp;
1765ccdc3305SLuigi Rizzo 	ssize_t base; /* handy for relative offsets between rings and nifp */
1766847bf383SLuigi Rizzo 	u_int i, len, n[NR_TXRX], ntot;
1767847bf383SLuigi Rizzo 	enum txrx t;
1768ccdc3305SLuigi Rizzo 
1769847bf383SLuigi Rizzo 	ntot = 0;
1770847bf383SLuigi Rizzo 	for_rx_tx(t) {
1771f0ea3689SLuigi Rizzo 		/* account for the (eventually fake) host rings */
1772847bf383SLuigi Rizzo 		n[t] = nma_get_nrings(na, t) + 1;
1773847bf383SLuigi Rizzo 		ntot += n[t];
1774847bf383SLuigi Rizzo 	}
1775ccdc3305SLuigi Rizzo 	/*
1776ccdc3305SLuigi Rizzo 	 * the descriptor is followed inline by an array of offsets
1777ccdc3305SLuigi Rizzo 	 * to the tx and rx rings in the shared memory region.
1778ccdc3305SLuigi Rizzo 	 */
1779ce3ee1e7SLuigi Rizzo 
1780ce3ee1e7SLuigi Rizzo 	NMA_LOCK(na->nm_mem);
1781ce3ee1e7SLuigi Rizzo 
1782847bf383SLuigi Rizzo 	len = sizeof(struct netmap_if) + (ntot * sizeof(ssize_t));
1783ce3ee1e7SLuigi Rizzo 	nifp = netmap_if_malloc(na->nm_mem, len);
1784ccdc3305SLuigi Rizzo 	if (nifp == NULL) {
1785ce3ee1e7SLuigi Rizzo 		NMA_UNLOCK(na->nm_mem);
1786ccdc3305SLuigi Rizzo 		return NULL;
1787ccdc3305SLuigi Rizzo 	}
1788ccdc3305SLuigi Rizzo 
1789ccdc3305SLuigi Rizzo 	/* initialize base fields -- override const */
1790ce3ee1e7SLuigi Rizzo 	*(u_int *)(uintptr_t)&nifp->ni_tx_rings = na->num_tx_rings;
1791ce3ee1e7SLuigi Rizzo 	*(u_int *)(uintptr_t)&nifp->ni_rx_rings = na->num_rx_rings;
17924bf50f18SLuigi Rizzo 	strncpy(nifp->ni_name, na->name, (size_t)IFNAMSIZ);
1793ccdc3305SLuigi Rizzo 
1794ccdc3305SLuigi Rizzo 	/*
1795ccdc3305SLuigi Rizzo 	 * fill the slots for the rx and tx rings. They contain the offset
1796ccdc3305SLuigi Rizzo 	 * between the ring and nifp, so the information is usable in
1797ccdc3305SLuigi Rizzo 	 * userspace to reach the ring from the nifp.
1798ccdc3305SLuigi Rizzo 	 */
1799ce3ee1e7SLuigi Rizzo 	base = netmap_if_offset(na->nm_mem, nifp);
1800847bf383SLuigi Rizzo 	for (i = 0; i < n[NR_TX]; i++) {
1801*c3e9b4dbSLuiz Otavio O Souza 		/* XXX instead of ofs == 0 maybe use the offset of an error
1802*c3e9b4dbSLuiz Otavio O Souza 		 * ring, like we do for buffers? */
1803*c3e9b4dbSLuiz Otavio O Souza 		ssize_t ofs = 0;
1804*c3e9b4dbSLuiz Otavio O Souza 
1805*c3e9b4dbSLuiz Otavio O Souza 		if (na->tx_rings[i].ring != NULL && i >= priv->np_qfirst[NR_TX]
1806*c3e9b4dbSLuiz Otavio O Souza 				&& i < priv->np_qlast[NR_TX]) {
1807*c3e9b4dbSLuiz Otavio O Souza 			ofs = netmap_ring_offset(na->nm_mem,
1808*c3e9b4dbSLuiz Otavio O Souza 						 na->tx_rings[i].ring) - base;
180937e3a6d3SLuigi Rizzo 		}
1810*c3e9b4dbSLuiz Otavio O Souza 		*(ssize_t *)(uintptr_t)&nifp->ring_ofs[i] = ofs;
1811ccdc3305SLuigi Rizzo 	}
1812847bf383SLuigi Rizzo 	for (i = 0; i < n[NR_RX]; i++) {
1813*c3e9b4dbSLuiz Otavio O Souza 		/* XXX instead of ofs == 0 maybe use the offset of an error
1814*c3e9b4dbSLuiz Otavio O Souza 		 * ring, like we do for buffers? */
1815*c3e9b4dbSLuiz Otavio O Souza 		ssize_t ofs = 0;
1816*c3e9b4dbSLuiz Otavio O Souza 
1817*c3e9b4dbSLuiz Otavio O Souza 		if (na->rx_rings[i].ring != NULL && i >= priv->np_qfirst[NR_RX]
1818*c3e9b4dbSLuiz Otavio O Souza 				&& i < priv->np_qlast[NR_RX]) {
1819*c3e9b4dbSLuiz Otavio O Souza 			ofs = netmap_ring_offset(na->nm_mem,
1820*c3e9b4dbSLuiz Otavio O Souza 						 na->rx_rings[i].ring) - base;
182137e3a6d3SLuigi Rizzo 		}
1822*c3e9b4dbSLuiz Otavio O Souza 		*(ssize_t *)(uintptr_t)&nifp->ring_ofs[i+n[NR_TX]] = ofs;
1823ccdc3305SLuigi Rizzo 	}
1824ce3ee1e7SLuigi Rizzo 
1825ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(na->nm_mem);
1826ce3ee1e7SLuigi Rizzo 
1827ccdc3305SLuigi Rizzo 	return (nifp);
1828ccdc3305SLuigi Rizzo }
1829ccdc3305SLuigi Rizzo 
1830847bf383SLuigi Rizzo static void
1831847bf383SLuigi Rizzo netmap_mem2_if_delete(struct netmap_adapter *na, struct netmap_if *nifp)
1832ccdc3305SLuigi Rizzo {
1833ce3ee1e7SLuigi Rizzo 	if (nifp == NULL)
1834ce3ee1e7SLuigi Rizzo 		/* nothing to do */
1835ce3ee1e7SLuigi Rizzo 		return;
1836ce3ee1e7SLuigi Rizzo 	NMA_LOCK(na->nm_mem);
1837f0ea3689SLuigi Rizzo 	if (nifp->ni_bufs_head)
1838f0ea3689SLuigi Rizzo 		netmap_extra_free(na, nifp->ni_bufs_head);
1839ce3ee1e7SLuigi Rizzo 	netmap_if_free(na->nm_mem, nifp);
1840ce3ee1e7SLuigi Rizzo 
1841ce3ee1e7SLuigi Rizzo 	NMA_UNLOCK(na->nm_mem);
1842ce3ee1e7SLuigi Rizzo }
1843ce3ee1e7SLuigi Rizzo 
1844ce3ee1e7SLuigi Rizzo static void
1845*c3e9b4dbSLuiz Otavio O Souza netmap_mem2_deref(struct netmap_mem_d *nmd)
1846ce3ee1e7SLuigi Rizzo {
1847ce3ee1e7SLuigi Rizzo 
1848847bf383SLuigi Rizzo 	nmd->active--;
1849847bf383SLuigi Rizzo 	if (!nmd->active)
18504bf50f18SLuigi Rizzo 		nmd->nm_grp = -1;
1851ae10d1afSLuigi Rizzo 	if (netmap_verbose)
1852847bf383SLuigi Rizzo 		D("active = %d", nmd->active);
1853ce3ee1e7SLuigi Rizzo 
1854ce3ee1e7SLuigi Rizzo }
1855ce3ee1e7SLuigi Rizzo 
1856847bf383SLuigi Rizzo struct netmap_mem_ops netmap_mem_global_ops = {
1857847bf383SLuigi Rizzo 	.nmd_get_lut = netmap_mem2_get_lut,
1858847bf383SLuigi Rizzo 	.nmd_get_info = netmap_mem2_get_info,
1859847bf383SLuigi Rizzo 	.nmd_ofstophys = netmap_mem2_ofstophys,
1860*c3e9b4dbSLuiz Otavio O Souza 	.nmd_config = netmap_mem2_config,
1861*c3e9b4dbSLuiz Otavio O Souza 	.nmd_finalize = netmap_mem2_finalize,
1862*c3e9b4dbSLuiz Otavio O Souza 	.nmd_deref = netmap_mem2_deref,
1863*c3e9b4dbSLuiz Otavio O Souza 	.nmd_delete = netmap_mem2_delete,
1864847bf383SLuigi Rizzo 	.nmd_if_offset = netmap_mem2_if_offset,
1865847bf383SLuigi Rizzo 	.nmd_if_new = netmap_mem2_if_new,
1866847bf383SLuigi Rizzo 	.nmd_if_delete = netmap_mem2_if_delete,
1867847bf383SLuigi Rizzo 	.nmd_rings_create = netmap_mem2_rings_create,
1868847bf383SLuigi Rizzo 	.nmd_rings_delete = netmap_mem2_rings_delete
1869847bf383SLuigi Rizzo };
187037e3a6d3SLuigi Rizzo 
1871844a6f0cSLuigi Rizzo int
1872*c3e9b4dbSLuiz Otavio O Souza netmap_mem_pools_info_get(struct nmreq *nmr, struct netmap_mem_d *nmd)
1873844a6f0cSLuigi Rizzo {
1874844a6f0cSLuigi Rizzo 	uintptr_t *pp = (uintptr_t *)&nmr->nr_arg1;
1875844a6f0cSLuigi Rizzo 	struct netmap_pools_info *upi = (struct netmap_pools_info *)(*pp);
1876844a6f0cSLuigi Rizzo 	struct netmap_pools_info pi;
1877844a6f0cSLuigi Rizzo 	unsigned int memsize;
1878844a6f0cSLuigi Rizzo 	uint16_t memid;
1879844a6f0cSLuigi Rizzo 	int ret;
1880844a6f0cSLuigi Rizzo 
1881844a6f0cSLuigi Rizzo 	ret = netmap_mem_get_info(nmd, &memsize, NULL, &memid);
1882844a6f0cSLuigi Rizzo 	if (ret) {
1883844a6f0cSLuigi Rizzo 		return ret;
1884844a6f0cSLuigi Rizzo 	}
1885844a6f0cSLuigi Rizzo 
1886844a6f0cSLuigi Rizzo 	pi.memsize = memsize;
1887844a6f0cSLuigi Rizzo 	pi.memid = memid;
1888*c3e9b4dbSLuiz Otavio O Souza 	NMA_LOCK(nmd);
1889844a6f0cSLuigi Rizzo 	pi.if_pool_offset = 0;
1890844a6f0cSLuigi Rizzo 	pi.if_pool_objtotal = nmd->pools[NETMAP_IF_POOL].objtotal;
1891844a6f0cSLuigi Rizzo 	pi.if_pool_objsize = nmd->pools[NETMAP_IF_POOL]._objsize;
1892844a6f0cSLuigi Rizzo 
1893844a6f0cSLuigi Rizzo 	pi.ring_pool_offset = nmd->pools[NETMAP_IF_POOL].memtotal;
1894844a6f0cSLuigi Rizzo 	pi.ring_pool_objtotal = nmd->pools[NETMAP_RING_POOL].objtotal;
1895844a6f0cSLuigi Rizzo 	pi.ring_pool_objsize = nmd->pools[NETMAP_RING_POOL]._objsize;
1896844a6f0cSLuigi Rizzo 
1897844a6f0cSLuigi Rizzo 	pi.buf_pool_offset = nmd->pools[NETMAP_IF_POOL].memtotal +
1898844a6f0cSLuigi Rizzo 			     nmd->pools[NETMAP_RING_POOL].memtotal;
1899844a6f0cSLuigi Rizzo 	pi.buf_pool_objtotal = nmd->pools[NETMAP_BUF_POOL].objtotal;
1900844a6f0cSLuigi Rizzo 	pi.buf_pool_objsize = nmd->pools[NETMAP_BUF_POOL]._objsize;
1901*c3e9b4dbSLuiz Otavio O Souza 	NMA_UNLOCK(nmd);
1902844a6f0cSLuigi Rizzo 
1903844a6f0cSLuigi Rizzo 	ret = copyout(&pi, upi, sizeof(pi));
1904844a6f0cSLuigi Rizzo 	if (ret) {
1905844a6f0cSLuigi Rizzo 		return ret;
1906844a6f0cSLuigi Rizzo 	}
1907844a6f0cSLuigi Rizzo 
1908844a6f0cSLuigi Rizzo 	return 0;
1909844a6f0cSLuigi Rizzo }
1910844a6f0cSLuigi Rizzo 
191137e3a6d3SLuigi Rizzo #ifdef WITH_PTNETMAP_GUEST
191237e3a6d3SLuigi Rizzo struct mem_pt_if {
191337e3a6d3SLuigi Rizzo 	struct mem_pt_if *next;
191437e3a6d3SLuigi Rizzo 	struct ifnet *ifp;
191537e3a6d3SLuigi Rizzo 	unsigned int nifp_offset;
191637e3a6d3SLuigi Rizzo };
191737e3a6d3SLuigi Rizzo 
191837e3a6d3SLuigi Rizzo /* Netmap allocator for ptnetmap guests. */
191937e3a6d3SLuigi Rizzo struct netmap_mem_ptg {
192037e3a6d3SLuigi Rizzo 	struct netmap_mem_d up;
192137e3a6d3SLuigi Rizzo 
192237e3a6d3SLuigi Rizzo 	vm_paddr_t nm_paddr;            /* physical address in the guest */
192337e3a6d3SLuigi Rizzo 	void *nm_addr;                  /* virtual address in the guest */
192437e3a6d3SLuigi Rizzo 	struct netmap_lut buf_lut;      /* lookup table for BUF pool in the guest */
1925844a6f0cSLuigi Rizzo 	nm_memid_t host_mem_id;         /* allocator identifier in the host */
1926844a6f0cSLuigi Rizzo 	struct ptnetmap_memdev *ptn_dev;/* ptnetmap memdev */
192737e3a6d3SLuigi Rizzo 	struct mem_pt_if *pt_ifs;	/* list of interfaces in passthrough */
192837e3a6d3SLuigi Rizzo };
192937e3a6d3SLuigi Rizzo 
193037e3a6d3SLuigi Rizzo /* Link a passthrough interface to a passthrough netmap allocator. */
193137e3a6d3SLuigi Rizzo static int
193237e3a6d3SLuigi Rizzo netmap_mem_pt_guest_ifp_add(struct netmap_mem_d *nmd, struct ifnet *ifp,
1933844a6f0cSLuigi Rizzo 			    unsigned int nifp_offset)
193437e3a6d3SLuigi Rizzo {
193537e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
1936*c3e9b4dbSLuiz Otavio O Souza 	struct mem_pt_if *ptif = nm_os_malloc(sizeof(*ptif));
193737e3a6d3SLuigi Rizzo 
193837e3a6d3SLuigi Rizzo 	if (!ptif) {
193937e3a6d3SLuigi Rizzo 		return ENOMEM;
194037e3a6d3SLuigi Rizzo 	}
194137e3a6d3SLuigi Rizzo 
194237e3a6d3SLuigi Rizzo 	NMA_LOCK(nmd);
194337e3a6d3SLuigi Rizzo 
194437e3a6d3SLuigi Rizzo 	ptif->ifp = ifp;
194537e3a6d3SLuigi Rizzo 	ptif->nifp_offset = nifp_offset;
194637e3a6d3SLuigi Rizzo 
194737e3a6d3SLuigi Rizzo 	if (ptnmd->pt_ifs) {
194837e3a6d3SLuigi Rizzo 		ptif->next = ptnmd->pt_ifs;
194937e3a6d3SLuigi Rizzo 	}
195037e3a6d3SLuigi Rizzo 	ptnmd->pt_ifs = ptif;
195137e3a6d3SLuigi Rizzo 
195237e3a6d3SLuigi Rizzo 	NMA_UNLOCK(nmd);
195337e3a6d3SLuigi Rizzo 
195437e3a6d3SLuigi Rizzo 	D("added (ifp=%p,nifp_offset=%u)", ptif->ifp, ptif->nifp_offset);
195537e3a6d3SLuigi Rizzo 
195637e3a6d3SLuigi Rizzo 	return 0;
195737e3a6d3SLuigi Rizzo }
195837e3a6d3SLuigi Rizzo 
195937e3a6d3SLuigi Rizzo /* Called with NMA_LOCK(nmd) held. */
196037e3a6d3SLuigi Rizzo static struct mem_pt_if *
196137e3a6d3SLuigi Rizzo netmap_mem_pt_guest_ifp_lookup(struct netmap_mem_d *nmd, struct ifnet *ifp)
196237e3a6d3SLuigi Rizzo {
196337e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
196437e3a6d3SLuigi Rizzo 	struct mem_pt_if *curr;
196537e3a6d3SLuigi Rizzo 
196637e3a6d3SLuigi Rizzo 	for (curr = ptnmd->pt_ifs; curr; curr = curr->next) {
196737e3a6d3SLuigi Rizzo 		if (curr->ifp == ifp) {
196837e3a6d3SLuigi Rizzo 			return curr;
196937e3a6d3SLuigi Rizzo 		}
197037e3a6d3SLuigi Rizzo 	}
197137e3a6d3SLuigi Rizzo 
197237e3a6d3SLuigi Rizzo 	return NULL;
197337e3a6d3SLuigi Rizzo }
197437e3a6d3SLuigi Rizzo 
197537e3a6d3SLuigi Rizzo /* Unlink a passthrough interface from a passthrough netmap allocator. */
197637e3a6d3SLuigi Rizzo int
197737e3a6d3SLuigi Rizzo netmap_mem_pt_guest_ifp_del(struct netmap_mem_d *nmd, struct ifnet *ifp)
197837e3a6d3SLuigi Rizzo {
197937e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
198037e3a6d3SLuigi Rizzo 	struct mem_pt_if *prev = NULL;
198137e3a6d3SLuigi Rizzo 	struct mem_pt_if *curr;
198237e3a6d3SLuigi Rizzo 	int ret = -1;
198337e3a6d3SLuigi Rizzo 
198437e3a6d3SLuigi Rizzo 	NMA_LOCK(nmd);
198537e3a6d3SLuigi Rizzo 
198637e3a6d3SLuigi Rizzo 	for (curr = ptnmd->pt_ifs; curr; curr = curr->next) {
198737e3a6d3SLuigi Rizzo 		if (curr->ifp == ifp) {
198837e3a6d3SLuigi Rizzo 			if (prev) {
198937e3a6d3SLuigi Rizzo 				prev->next = curr->next;
199037e3a6d3SLuigi Rizzo 			} else {
199137e3a6d3SLuigi Rizzo 				ptnmd->pt_ifs = curr->next;
199237e3a6d3SLuigi Rizzo 			}
199337e3a6d3SLuigi Rizzo 			D("removed (ifp=%p,nifp_offset=%u)",
199437e3a6d3SLuigi Rizzo 			  curr->ifp, curr->nifp_offset);
1995*c3e9b4dbSLuiz Otavio O Souza 			nm_os_free(curr);
199637e3a6d3SLuigi Rizzo 			ret = 0;
199737e3a6d3SLuigi Rizzo 			break;
199837e3a6d3SLuigi Rizzo 		}
199937e3a6d3SLuigi Rizzo 		prev = curr;
200037e3a6d3SLuigi Rizzo 	}
200137e3a6d3SLuigi Rizzo 
200237e3a6d3SLuigi Rizzo 	NMA_UNLOCK(nmd);
200337e3a6d3SLuigi Rizzo 
200437e3a6d3SLuigi Rizzo 	return ret;
200537e3a6d3SLuigi Rizzo }
200637e3a6d3SLuigi Rizzo 
200737e3a6d3SLuigi Rizzo static int
200837e3a6d3SLuigi Rizzo netmap_mem_pt_guest_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut)
200937e3a6d3SLuigi Rizzo {
201037e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
201137e3a6d3SLuigi Rizzo 
201237e3a6d3SLuigi Rizzo 	if (!(nmd->flags & NETMAP_MEM_FINALIZED)) {
201337e3a6d3SLuigi Rizzo 		return EINVAL;
201437e3a6d3SLuigi Rizzo 	}
201537e3a6d3SLuigi Rizzo 
201637e3a6d3SLuigi Rizzo 	*lut = ptnmd->buf_lut;
201737e3a6d3SLuigi Rizzo 	return 0;
201837e3a6d3SLuigi Rizzo }
201937e3a6d3SLuigi Rizzo 
202037e3a6d3SLuigi Rizzo static int
202137e3a6d3SLuigi Rizzo netmap_mem_pt_guest_get_info(struct netmap_mem_d *nmd, u_int *size,
202237e3a6d3SLuigi Rizzo 			     u_int *memflags, uint16_t *id)
202337e3a6d3SLuigi Rizzo {
202437e3a6d3SLuigi Rizzo 	int error = 0;
202537e3a6d3SLuigi Rizzo 
202637e3a6d3SLuigi Rizzo 	NMA_LOCK(nmd);
202737e3a6d3SLuigi Rizzo 
202837e3a6d3SLuigi Rizzo 	error = nmd->ops->nmd_config(nmd);
202937e3a6d3SLuigi Rizzo 	if (error)
203037e3a6d3SLuigi Rizzo 		goto out;
203137e3a6d3SLuigi Rizzo 
203237e3a6d3SLuigi Rizzo 	if (size)
203337e3a6d3SLuigi Rizzo 		*size = nmd->nm_totalsize;
203437e3a6d3SLuigi Rizzo 	if (memflags)
203537e3a6d3SLuigi Rizzo 		*memflags = nmd->flags;
203637e3a6d3SLuigi Rizzo 	if (id)
203737e3a6d3SLuigi Rizzo 		*id = nmd->nm_id;
203837e3a6d3SLuigi Rizzo 
203937e3a6d3SLuigi Rizzo out:
204037e3a6d3SLuigi Rizzo 	NMA_UNLOCK(nmd);
204137e3a6d3SLuigi Rizzo 
204237e3a6d3SLuigi Rizzo 	return error;
204337e3a6d3SLuigi Rizzo }
204437e3a6d3SLuigi Rizzo 
204537e3a6d3SLuigi Rizzo static vm_paddr_t
204637e3a6d3SLuigi Rizzo netmap_mem_pt_guest_ofstophys(struct netmap_mem_d *nmd, vm_ooffset_t off)
204737e3a6d3SLuigi Rizzo {
204837e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
204937e3a6d3SLuigi Rizzo 	vm_paddr_t paddr;
205037e3a6d3SLuigi Rizzo 	/* if the offset is valid, just return csb->base_addr + off */
205137e3a6d3SLuigi Rizzo 	paddr = (vm_paddr_t)(ptnmd->nm_paddr + off);
205237e3a6d3SLuigi Rizzo 	ND("off %lx padr %lx", off, (unsigned long)paddr);
205337e3a6d3SLuigi Rizzo 	return paddr;
205437e3a6d3SLuigi Rizzo }
205537e3a6d3SLuigi Rizzo 
205637e3a6d3SLuigi Rizzo static int
205737e3a6d3SLuigi Rizzo netmap_mem_pt_guest_config(struct netmap_mem_d *nmd)
205837e3a6d3SLuigi Rizzo {
205937e3a6d3SLuigi Rizzo 	/* nothing to do, we are configured on creation
206037e3a6d3SLuigi Rizzo 	 * and configuration never changes thereafter
206137e3a6d3SLuigi Rizzo 	 */
206237e3a6d3SLuigi Rizzo 	return 0;
206337e3a6d3SLuigi Rizzo }
206437e3a6d3SLuigi Rizzo 
206537e3a6d3SLuigi Rizzo static int
206637e3a6d3SLuigi Rizzo netmap_mem_pt_guest_finalize(struct netmap_mem_d *nmd)
206737e3a6d3SLuigi Rizzo {
206837e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2069844a6f0cSLuigi Rizzo 	uint64_t mem_size;
2070844a6f0cSLuigi Rizzo 	uint32_t bufsize;
2071844a6f0cSLuigi Rizzo 	uint32_t nbuffers;
2072844a6f0cSLuigi Rizzo 	uint32_t poolofs;
2073844a6f0cSLuigi Rizzo 	vm_paddr_t paddr;
2074844a6f0cSLuigi Rizzo 	char *vaddr;
2075844a6f0cSLuigi Rizzo 	int i;
207637e3a6d3SLuigi Rizzo 	int error = 0;
207737e3a6d3SLuigi Rizzo 
207837e3a6d3SLuigi Rizzo 	nmd->active++;
207937e3a6d3SLuigi Rizzo 
208037e3a6d3SLuigi Rizzo 	if (nmd->flags & NETMAP_MEM_FINALIZED)
208137e3a6d3SLuigi Rizzo 		goto out;
208237e3a6d3SLuigi Rizzo 
208337e3a6d3SLuigi Rizzo 	if (ptnmd->ptn_dev == NULL) {
208437e3a6d3SLuigi Rizzo 		D("ptnetmap memdev not attached");
208537e3a6d3SLuigi Rizzo 		error = ENOMEM;
208637e3a6d3SLuigi Rizzo 		goto err;
208737e3a6d3SLuigi Rizzo 	}
2088844a6f0cSLuigi Rizzo 	/* Map memory through ptnetmap-memdev BAR. */
208937e3a6d3SLuigi Rizzo 	error = nm_os_pt_memdev_iomap(ptnmd->ptn_dev, &ptnmd->nm_paddr,
2090844a6f0cSLuigi Rizzo 				      &ptnmd->nm_addr, &mem_size);
209137e3a6d3SLuigi Rizzo 	if (error)
209237e3a6d3SLuigi Rizzo 		goto err;
209337e3a6d3SLuigi Rizzo 
2094844a6f0cSLuigi Rizzo         /* Initialize the lut using the information contained in the
2095844a6f0cSLuigi Rizzo 	 * ptnetmap memory device. */
2096844a6f0cSLuigi Rizzo         bufsize = nm_os_pt_memdev_ioread(ptnmd->ptn_dev,
2097844a6f0cSLuigi Rizzo 					 PTNET_MDEV_IO_BUF_POOL_OBJSZ);
2098844a6f0cSLuigi Rizzo         nbuffers = nm_os_pt_memdev_ioread(ptnmd->ptn_dev,
2099844a6f0cSLuigi Rizzo 					 PTNET_MDEV_IO_BUF_POOL_OBJNUM);
2100844a6f0cSLuigi Rizzo 
2101844a6f0cSLuigi Rizzo 	/* allocate the lut */
2102844a6f0cSLuigi Rizzo 	if (ptnmd->buf_lut.lut == NULL) {
2103844a6f0cSLuigi Rizzo 		D("allocating lut");
2104844a6f0cSLuigi Rizzo 		ptnmd->buf_lut.lut = nm_alloc_lut(nbuffers);
2105844a6f0cSLuigi Rizzo 		if (ptnmd->buf_lut.lut == NULL) {
2106844a6f0cSLuigi Rizzo 			D("lut allocation failed");
2107844a6f0cSLuigi Rizzo 			return ENOMEM;
2108844a6f0cSLuigi Rizzo 		}
2109844a6f0cSLuigi Rizzo 	}
2110844a6f0cSLuigi Rizzo 
2111844a6f0cSLuigi Rizzo 	/* we have physically contiguous memory mapped through PCI BAR */
2112844a6f0cSLuigi Rizzo 	poolofs = nm_os_pt_memdev_ioread(ptnmd->ptn_dev,
2113844a6f0cSLuigi Rizzo 					 PTNET_MDEV_IO_BUF_POOL_OFS);
2114844a6f0cSLuigi Rizzo 	vaddr = (char *)(ptnmd->nm_addr) + poolofs;
2115844a6f0cSLuigi Rizzo 	paddr = ptnmd->nm_paddr + poolofs;
2116844a6f0cSLuigi Rizzo 
2117844a6f0cSLuigi Rizzo 	for (i = 0; i < nbuffers; i++) {
2118844a6f0cSLuigi Rizzo 		ptnmd->buf_lut.lut[i].vaddr = vaddr;
2119844a6f0cSLuigi Rizzo 		ptnmd->buf_lut.lut[i].paddr = paddr;
2120844a6f0cSLuigi Rizzo 		vaddr += bufsize;
2121844a6f0cSLuigi Rizzo 		paddr += bufsize;
2122844a6f0cSLuigi Rizzo 	}
2123844a6f0cSLuigi Rizzo 
2124844a6f0cSLuigi Rizzo 	ptnmd->buf_lut.objtotal = nbuffers;
2125844a6f0cSLuigi Rizzo 	ptnmd->buf_lut.objsize = bufsize;
2126844a6f0cSLuigi Rizzo 	nmd->nm_totalsize = (unsigned int)mem_size;
212737e3a6d3SLuigi Rizzo 
212837e3a6d3SLuigi Rizzo 	nmd->flags |= NETMAP_MEM_FINALIZED;
212937e3a6d3SLuigi Rizzo out:
213037e3a6d3SLuigi Rizzo 	return 0;
213137e3a6d3SLuigi Rizzo err:
213237e3a6d3SLuigi Rizzo 	nmd->active--;
213337e3a6d3SLuigi Rizzo 	return error;
213437e3a6d3SLuigi Rizzo }
213537e3a6d3SLuigi Rizzo 
213637e3a6d3SLuigi Rizzo static void
213737e3a6d3SLuigi Rizzo netmap_mem_pt_guest_deref(struct netmap_mem_d *nmd)
213837e3a6d3SLuigi Rizzo {
213937e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
214037e3a6d3SLuigi Rizzo 
214137e3a6d3SLuigi Rizzo 	nmd->active--;
214237e3a6d3SLuigi Rizzo 	if (nmd->active <= 0 &&
214337e3a6d3SLuigi Rizzo 		(nmd->flags & NETMAP_MEM_FINALIZED)) {
214437e3a6d3SLuigi Rizzo 	    nmd->flags  &= ~NETMAP_MEM_FINALIZED;
214537e3a6d3SLuigi Rizzo 	    /* unmap ptnetmap-memdev memory */
214637e3a6d3SLuigi Rizzo 	    if (ptnmd->ptn_dev) {
214737e3a6d3SLuigi Rizzo 		nm_os_pt_memdev_iounmap(ptnmd->ptn_dev);
214837e3a6d3SLuigi Rizzo 	    }
2149*c3e9b4dbSLuiz Otavio O Souza 	    ptnmd->nm_addr = 0;
215037e3a6d3SLuigi Rizzo 	    ptnmd->nm_paddr = 0;
215137e3a6d3SLuigi Rizzo 	}
215237e3a6d3SLuigi Rizzo }
215337e3a6d3SLuigi Rizzo 
215437e3a6d3SLuigi Rizzo static ssize_t
215537e3a6d3SLuigi Rizzo netmap_mem_pt_guest_if_offset(struct netmap_mem_d *nmd, const void *vaddr)
215637e3a6d3SLuigi Rizzo {
215737e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
215837e3a6d3SLuigi Rizzo 
215937e3a6d3SLuigi Rizzo 	return (const char *)(vaddr) - (char *)(ptnmd->nm_addr);
216037e3a6d3SLuigi Rizzo }
216137e3a6d3SLuigi Rizzo 
216237e3a6d3SLuigi Rizzo static void
216337e3a6d3SLuigi Rizzo netmap_mem_pt_guest_delete(struct netmap_mem_d *nmd)
216437e3a6d3SLuigi Rizzo {
216537e3a6d3SLuigi Rizzo 	if (nmd == NULL)
216637e3a6d3SLuigi Rizzo 		return;
216737e3a6d3SLuigi Rizzo 	if (netmap_verbose)
216837e3a6d3SLuigi Rizzo 		D("deleting %p", nmd);
216937e3a6d3SLuigi Rizzo 	if (nmd->active > 0)
217037e3a6d3SLuigi Rizzo 		D("bug: deleting mem allocator with active=%d!", nmd->active);
217137e3a6d3SLuigi Rizzo 	if (netmap_verbose)
217237e3a6d3SLuigi Rizzo 		D("done deleting %p", nmd);
217337e3a6d3SLuigi Rizzo 	NMA_LOCK_DESTROY(nmd);
2174*c3e9b4dbSLuiz Otavio O Souza 	nm_os_free(nmd);
217537e3a6d3SLuigi Rizzo }
217637e3a6d3SLuigi Rizzo 
217737e3a6d3SLuigi Rizzo static struct netmap_if *
2178*c3e9b4dbSLuiz Otavio O Souza netmap_mem_pt_guest_if_new(struct netmap_adapter *na, struct netmap_priv_d *priv)
217937e3a6d3SLuigi Rizzo {
218037e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)na->nm_mem;
218137e3a6d3SLuigi Rizzo 	struct mem_pt_if *ptif;
218237e3a6d3SLuigi Rizzo 	struct netmap_if *nifp = NULL;
218337e3a6d3SLuigi Rizzo 
218437e3a6d3SLuigi Rizzo 	NMA_LOCK(na->nm_mem);
218537e3a6d3SLuigi Rizzo 
218637e3a6d3SLuigi Rizzo 	ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, na->ifp);
218737e3a6d3SLuigi Rizzo 	if (ptif == NULL) {
218837e3a6d3SLuigi Rizzo 		D("Error: interface %p is not in passthrough", na->ifp);
218937e3a6d3SLuigi Rizzo 		goto out;
219037e3a6d3SLuigi Rizzo 	}
219137e3a6d3SLuigi Rizzo 
219237e3a6d3SLuigi Rizzo 	nifp = (struct netmap_if *)((char *)(ptnmd->nm_addr) +
219337e3a6d3SLuigi Rizzo 				    ptif->nifp_offset);
219437e3a6d3SLuigi Rizzo 	NMA_UNLOCK(na->nm_mem);
219537e3a6d3SLuigi Rizzo out:
219637e3a6d3SLuigi Rizzo 	return nifp;
219737e3a6d3SLuigi Rizzo }
219837e3a6d3SLuigi Rizzo 
219937e3a6d3SLuigi Rizzo static void
220037e3a6d3SLuigi Rizzo netmap_mem_pt_guest_if_delete(struct netmap_adapter *na, struct netmap_if *nifp)
220137e3a6d3SLuigi Rizzo {
220237e3a6d3SLuigi Rizzo 	struct mem_pt_if *ptif;
220337e3a6d3SLuigi Rizzo 
220437e3a6d3SLuigi Rizzo 	NMA_LOCK(na->nm_mem);
220537e3a6d3SLuigi Rizzo 	ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, na->ifp);
220637e3a6d3SLuigi Rizzo 	if (ptif == NULL) {
220737e3a6d3SLuigi Rizzo 		D("Error: interface %p is not in passthrough", na->ifp);
220837e3a6d3SLuigi Rizzo 	}
220937e3a6d3SLuigi Rizzo 	NMA_UNLOCK(na->nm_mem);
221037e3a6d3SLuigi Rizzo }
221137e3a6d3SLuigi Rizzo 
221237e3a6d3SLuigi Rizzo static int
221337e3a6d3SLuigi Rizzo netmap_mem_pt_guest_rings_create(struct netmap_adapter *na)
221437e3a6d3SLuigi Rizzo {
221537e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)na->nm_mem;
221637e3a6d3SLuigi Rizzo 	struct mem_pt_if *ptif;
221737e3a6d3SLuigi Rizzo 	struct netmap_if *nifp;
221837e3a6d3SLuigi Rizzo 	int i, error = -1;
221937e3a6d3SLuigi Rizzo 
222037e3a6d3SLuigi Rizzo 	NMA_LOCK(na->nm_mem);
222137e3a6d3SLuigi Rizzo 
222237e3a6d3SLuigi Rizzo 	ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, na->ifp);
222337e3a6d3SLuigi Rizzo 	if (ptif == NULL) {
222437e3a6d3SLuigi Rizzo 		D("Error: interface %p is not in passthrough", na->ifp);
222537e3a6d3SLuigi Rizzo 		goto out;
222637e3a6d3SLuigi Rizzo 	}
222737e3a6d3SLuigi Rizzo 
222837e3a6d3SLuigi Rizzo 
222937e3a6d3SLuigi Rizzo 	/* point each kring to the corresponding backend ring */
223037e3a6d3SLuigi Rizzo 	nifp = (struct netmap_if *)((char *)ptnmd->nm_addr + ptif->nifp_offset);
223137e3a6d3SLuigi Rizzo 	for (i = 0; i <= na->num_tx_rings; i++) {
223237e3a6d3SLuigi Rizzo 		struct netmap_kring *kring = na->tx_rings + i;
223337e3a6d3SLuigi Rizzo 		if (kring->ring)
223437e3a6d3SLuigi Rizzo 			continue;
223537e3a6d3SLuigi Rizzo 		kring->ring = (struct netmap_ring *)
223637e3a6d3SLuigi Rizzo 			((char *)nifp + nifp->ring_ofs[i]);
223737e3a6d3SLuigi Rizzo 	}
223837e3a6d3SLuigi Rizzo 	for (i = 0; i <= na->num_rx_rings; i++) {
223937e3a6d3SLuigi Rizzo 		struct netmap_kring *kring = na->rx_rings + i;
224037e3a6d3SLuigi Rizzo 		if (kring->ring)
224137e3a6d3SLuigi Rizzo 			continue;
224237e3a6d3SLuigi Rizzo 		kring->ring = (struct netmap_ring *)
224337e3a6d3SLuigi Rizzo 			((char *)nifp +
224437e3a6d3SLuigi Rizzo 			 nifp->ring_ofs[i + na->num_tx_rings + 1]);
224537e3a6d3SLuigi Rizzo 	}
224637e3a6d3SLuigi Rizzo 
224737e3a6d3SLuigi Rizzo 	error = 0;
224837e3a6d3SLuigi Rizzo out:
224937e3a6d3SLuigi Rizzo 	NMA_UNLOCK(na->nm_mem);
225037e3a6d3SLuigi Rizzo 
225137e3a6d3SLuigi Rizzo 	return error;
225237e3a6d3SLuigi Rizzo }
225337e3a6d3SLuigi Rizzo 
225437e3a6d3SLuigi Rizzo static void
225537e3a6d3SLuigi Rizzo netmap_mem_pt_guest_rings_delete(struct netmap_adapter *na)
225637e3a6d3SLuigi Rizzo {
225737e3a6d3SLuigi Rizzo 	/* TODO: remove?? */
225837e3a6d3SLuigi Rizzo #if 0
225937e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)na->nm_mem;
226037e3a6d3SLuigi Rizzo 	struct mem_pt_if *ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem,
226137e3a6d3SLuigi Rizzo 								na->ifp);
226237e3a6d3SLuigi Rizzo #endif
226337e3a6d3SLuigi Rizzo }
226437e3a6d3SLuigi Rizzo 
226537e3a6d3SLuigi Rizzo static struct netmap_mem_ops netmap_mem_pt_guest_ops = {
226637e3a6d3SLuigi Rizzo 	.nmd_get_lut = netmap_mem_pt_guest_get_lut,
226737e3a6d3SLuigi Rizzo 	.nmd_get_info = netmap_mem_pt_guest_get_info,
226837e3a6d3SLuigi Rizzo 	.nmd_ofstophys = netmap_mem_pt_guest_ofstophys,
226937e3a6d3SLuigi Rizzo 	.nmd_config = netmap_mem_pt_guest_config,
227037e3a6d3SLuigi Rizzo 	.nmd_finalize = netmap_mem_pt_guest_finalize,
227137e3a6d3SLuigi Rizzo 	.nmd_deref = netmap_mem_pt_guest_deref,
227237e3a6d3SLuigi Rizzo 	.nmd_if_offset = netmap_mem_pt_guest_if_offset,
227337e3a6d3SLuigi Rizzo 	.nmd_delete = netmap_mem_pt_guest_delete,
227437e3a6d3SLuigi Rizzo 	.nmd_if_new = netmap_mem_pt_guest_if_new,
227537e3a6d3SLuigi Rizzo 	.nmd_if_delete = netmap_mem_pt_guest_if_delete,
227637e3a6d3SLuigi Rizzo 	.nmd_rings_create = netmap_mem_pt_guest_rings_create,
227737e3a6d3SLuigi Rizzo 	.nmd_rings_delete = netmap_mem_pt_guest_rings_delete
227837e3a6d3SLuigi Rizzo };
227937e3a6d3SLuigi Rizzo 
2280*c3e9b4dbSLuiz Otavio O Souza /* Called with nm_mem_list_lock held. */
228137e3a6d3SLuigi Rizzo static struct netmap_mem_d *
2282844a6f0cSLuigi Rizzo netmap_mem_pt_guest_find_memid(nm_memid_t mem_id)
228337e3a6d3SLuigi Rizzo {
228437e3a6d3SLuigi Rizzo 	struct netmap_mem_d *mem = NULL;
228537e3a6d3SLuigi Rizzo 	struct netmap_mem_d *scan = netmap_last_mem_d;
228637e3a6d3SLuigi Rizzo 
228737e3a6d3SLuigi Rizzo 	do {
228837e3a6d3SLuigi Rizzo 		/* find ptnetmap allocator through host ID */
228937e3a6d3SLuigi Rizzo 		if (scan->ops->nmd_deref == netmap_mem_pt_guest_deref &&
2290844a6f0cSLuigi Rizzo 			((struct netmap_mem_ptg *)(scan))->host_mem_id == mem_id) {
229137e3a6d3SLuigi Rizzo 			mem = scan;
2292*c3e9b4dbSLuiz Otavio O Souza 			mem->refcount++;
2293*c3e9b4dbSLuiz Otavio O Souza 			NM_DBG_REFC(mem, __FUNCTION__, __LINE__);
229437e3a6d3SLuigi Rizzo 			break;
229537e3a6d3SLuigi Rizzo 		}
229637e3a6d3SLuigi Rizzo 		scan = scan->next;
229737e3a6d3SLuigi Rizzo 	} while (scan != netmap_last_mem_d);
229837e3a6d3SLuigi Rizzo 
229937e3a6d3SLuigi Rizzo 	return mem;
230037e3a6d3SLuigi Rizzo }
230137e3a6d3SLuigi Rizzo 
2302*c3e9b4dbSLuiz Otavio O Souza /* Called with nm_mem_list_lock held. */
230337e3a6d3SLuigi Rizzo static struct netmap_mem_d *
2304844a6f0cSLuigi Rizzo netmap_mem_pt_guest_create(nm_memid_t mem_id)
230537e3a6d3SLuigi Rizzo {
230637e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd;
230737e3a6d3SLuigi Rizzo 	int err = 0;
230837e3a6d3SLuigi Rizzo 
2309*c3e9b4dbSLuiz Otavio O Souza 	ptnmd = nm_os_malloc(sizeof(struct netmap_mem_ptg));
231037e3a6d3SLuigi Rizzo 	if (ptnmd == NULL) {
231137e3a6d3SLuigi Rizzo 		err = ENOMEM;
231237e3a6d3SLuigi Rizzo 		goto error;
231337e3a6d3SLuigi Rizzo 	}
231437e3a6d3SLuigi Rizzo 
231537e3a6d3SLuigi Rizzo 	ptnmd->up.ops = &netmap_mem_pt_guest_ops;
2316844a6f0cSLuigi Rizzo 	ptnmd->host_mem_id = mem_id;
231737e3a6d3SLuigi Rizzo 	ptnmd->pt_ifs = NULL;
231837e3a6d3SLuigi Rizzo 
231937e3a6d3SLuigi Rizzo         /* Assign new id in the guest (We have the lock) */
232037e3a6d3SLuigi Rizzo 	err = nm_mem_assign_id_locked(&ptnmd->up);
232137e3a6d3SLuigi Rizzo 	if (err)
232237e3a6d3SLuigi Rizzo 		goto error;
232337e3a6d3SLuigi Rizzo 
232437e3a6d3SLuigi Rizzo 	ptnmd->up.flags &= ~NETMAP_MEM_FINALIZED;
232537e3a6d3SLuigi Rizzo 	ptnmd->up.flags |= NETMAP_MEM_IO;
232637e3a6d3SLuigi Rizzo 
232737e3a6d3SLuigi Rizzo 	NMA_LOCK_INIT(&ptnmd->up);
232837e3a6d3SLuigi Rizzo 
2329*c3e9b4dbSLuiz Otavio O Souza 	snprintf(ptnmd->up.name, NM_MEM_NAMESZ, "%d", ptnmd->up.nm_id);
2330*c3e9b4dbSLuiz Otavio O Souza 
2331*c3e9b4dbSLuiz Otavio O Souza 
233237e3a6d3SLuigi Rizzo 	return &ptnmd->up;
233337e3a6d3SLuigi Rizzo error:
233437e3a6d3SLuigi Rizzo 	netmap_mem_pt_guest_delete(&ptnmd->up);
233537e3a6d3SLuigi Rizzo 	return NULL;
233637e3a6d3SLuigi Rizzo }
233737e3a6d3SLuigi Rizzo 
233837e3a6d3SLuigi Rizzo /*
233937e3a6d3SLuigi Rizzo  * find host id in guest allocators and create guest allocator
234037e3a6d3SLuigi Rizzo  * if it is not there
234137e3a6d3SLuigi Rizzo  */
234237e3a6d3SLuigi Rizzo static struct netmap_mem_d *
2343844a6f0cSLuigi Rizzo netmap_mem_pt_guest_get(nm_memid_t mem_id)
234437e3a6d3SLuigi Rizzo {
234537e3a6d3SLuigi Rizzo 	struct netmap_mem_d *nmd;
234637e3a6d3SLuigi Rizzo 
2347*c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_LOCK(nm_mem_list_lock);
2348844a6f0cSLuigi Rizzo 	nmd = netmap_mem_pt_guest_find_memid(mem_id);
234937e3a6d3SLuigi Rizzo 	if (nmd == NULL) {
2350844a6f0cSLuigi Rizzo 		nmd = netmap_mem_pt_guest_create(mem_id);
235137e3a6d3SLuigi Rizzo 	}
2352*c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_UNLOCK(nm_mem_list_lock);
235337e3a6d3SLuigi Rizzo 
235437e3a6d3SLuigi Rizzo 	return nmd;
235537e3a6d3SLuigi Rizzo }
235637e3a6d3SLuigi Rizzo 
235737e3a6d3SLuigi Rizzo /*
235837e3a6d3SLuigi Rizzo  * The guest allocator can be created by ptnetmap_memdev (during the device
2359844a6f0cSLuigi Rizzo  * attach) or by ptnetmap device (ptnet), during the netmap_attach.
236037e3a6d3SLuigi Rizzo  *
236137e3a6d3SLuigi Rizzo  * The order is not important (we have different order in LINUX and FreeBSD).
236237e3a6d3SLuigi Rizzo  * The first one, creates the device, and the second one simply attaches it.
236337e3a6d3SLuigi Rizzo  */
236437e3a6d3SLuigi Rizzo 
236537e3a6d3SLuigi Rizzo /* Called when ptnetmap_memdev is attaching, to attach a new allocator in
236637e3a6d3SLuigi Rizzo  * the guest */
236737e3a6d3SLuigi Rizzo struct netmap_mem_d *
2368844a6f0cSLuigi Rizzo netmap_mem_pt_guest_attach(struct ptnetmap_memdev *ptn_dev, nm_memid_t mem_id)
236937e3a6d3SLuigi Rizzo {
237037e3a6d3SLuigi Rizzo 	struct netmap_mem_d *nmd;
237137e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd;
237237e3a6d3SLuigi Rizzo 
2373844a6f0cSLuigi Rizzo 	nmd = netmap_mem_pt_guest_get(mem_id);
237437e3a6d3SLuigi Rizzo 
237537e3a6d3SLuigi Rizzo 	/* assign this device to the guest allocator */
237637e3a6d3SLuigi Rizzo 	if (nmd) {
237737e3a6d3SLuigi Rizzo 		ptnmd = (struct netmap_mem_ptg *)nmd;
237837e3a6d3SLuigi Rizzo 		ptnmd->ptn_dev = ptn_dev;
237937e3a6d3SLuigi Rizzo 	}
238037e3a6d3SLuigi Rizzo 
238137e3a6d3SLuigi Rizzo 	return nmd;
238237e3a6d3SLuigi Rizzo }
238337e3a6d3SLuigi Rizzo 
2384844a6f0cSLuigi Rizzo /* Called when ptnet device is attaching */
238537e3a6d3SLuigi Rizzo struct netmap_mem_d *
238637e3a6d3SLuigi Rizzo netmap_mem_pt_guest_new(struct ifnet *ifp,
238737e3a6d3SLuigi Rizzo 			unsigned int nifp_offset,
2388844a6f0cSLuigi Rizzo 			unsigned int memid)
238937e3a6d3SLuigi Rizzo {
239037e3a6d3SLuigi Rizzo 	struct netmap_mem_d *nmd;
239137e3a6d3SLuigi Rizzo 
2392844a6f0cSLuigi Rizzo 	if (ifp == NULL) {
239337e3a6d3SLuigi Rizzo 		return NULL;
239437e3a6d3SLuigi Rizzo 	}
239537e3a6d3SLuigi Rizzo 
2396844a6f0cSLuigi Rizzo 	nmd = netmap_mem_pt_guest_get((nm_memid_t)memid);
239737e3a6d3SLuigi Rizzo 
239837e3a6d3SLuigi Rizzo 	if (nmd) {
2399844a6f0cSLuigi Rizzo 		netmap_mem_pt_guest_ifp_add(nmd, ifp, nifp_offset);
240037e3a6d3SLuigi Rizzo 	}
240137e3a6d3SLuigi Rizzo 
240237e3a6d3SLuigi Rizzo 	return nmd;
240337e3a6d3SLuigi Rizzo }
240437e3a6d3SLuigi Rizzo 
240537e3a6d3SLuigi Rizzo #endif /* WITH_PTNETMAP_GUEST */
2406