xref: /freebsd-14.2/sys/dev/netmap/netmap_mem2.c (revision 2ff91c17)
1718cf2ccSPedro F. Giffuni /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3718cf2ccSPedro F. Giffuni  *
437e3a6d3SLuigi Rizzo  * Copyright (C) 2012-2014 Matteo Landi
537e3a6d3SLuigi Rizzo  * Copyright (C) 2012-2016 Luigi Rizzo
637e3a6d3SLuigi Rizzo  * Copyright (C) 2012-2016 Giuseppe Lettieri
737e3a6d3SLuigi Rizzo  * All rights reserved.
8ccdc3305SLuigi Rizzo  *
9ccdc3305SLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
10ccdc3305SLuigi Rizzo  * modification, are permitted provided that the following conditions
11ccdc3305SLuigi Rizzo  * are met:
12ccdc3305SLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
13ccdc3305SLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
14ccdc3305SLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
15ccdc3305SLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
16ccdc3305SLuigi Rizzo  *      documentation and/or other materials provided with the distribution.
17ccdc3305SLuigi Rizzo  *
18ccdc3305SLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19ccdc3305SLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20ccdc3305SLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ccdc3305SLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22ccdc3305SLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23ccdc3305SLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24ccdc3305SLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25ccdc3305SLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26ccdc3305SLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27ccdc3305SLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28ccdc3305SLuigi Rizzo  * SUCH DAMAGE.
29ccdc3305SLuigi Rizzo  */
30ccdc3305SLuigi Rizzo 
318241616dSLuigi Rizzo #ifdef linux
32ce3ee1e7SLuigi Rizzo #include "bsd_glue.h"
338241616dSLuigi Rizzo #endif /* linux */
348241616dSLuigi Rizzo 
35ce3ee1e7SLuigi Rizzo #ifdef __APPLE__
36ce3ee1e7SLuigi Rizzo #include "osx_glue.h"
37ce3ee1e7SLuigi Rizzo #endif /* __APPLE__ */
388241616dSLuigi Rizzo 
39ce3ee1e7SLuigi Rizzo #ifdef __FreeBSD__
40ce3ee1e7SLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */
41ce3ee1e7SLuigi Rizzo __FBSDID("$FreeBSD$");
428241616dSLuigi Rizzo 
43ce3ee1e7SLuigi Rizzo #include <sys/types.h>
44ce3ee1e7SLuigi Rizzo #include <sys/malloc.h>
4537e3a6d3SLuigi Rizzo #include <sys/kernel.h>		/* MALLOC_DEFINE */
46ce3ee1e7SLuigi Rizzo #include <sys/proc.h>
47ce3ee1e7SLuigi Rizzo #include <vm/vm.h>	/* vtophys */
48ce3ee1e7SLuigi Rizzo #include <vm/pmap.h>	/* vtophys */
49ce3ee1e7SLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
50ce3ee1e7SLuigi Rizzo #include <sys/selinfo.h>
51ce3ee1e7SLuigi Rizzo #include <sys/sysctl.h>
52ce3ee1e7SLuigi Rizzo #include <net/if.h>
53ce3ee1e7SLuigi Rizzo #include <net/if_var.h>
54ce3ee1e7SLuigi Rizzo #include <net/vnet.h>
55ce3ee1e7SLuigi Rizzo #include <machine/bus.h>	/* bus_dmamap_* */
56ce3ee1e7SLuigi Rizzo 
5737e3a6d3SLuigi Rizzo /* M_NETMAP only used in here */
5837e3a6d3SLuigi Rizzo MALLOC_DECLARE(M_NETMAP);
5937e3a6d3SLuigi Rizzo MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map");
6037e3a6d3SLuigi Rizzo 
61ce3ee1e7SLuigi Rizzo #endif /* __FreeBSD__ */
62ce3ee1e7SLuigi Rizzo 
6337e3a6d3SLuigi Rizzo #ifdef _WIN32
6437e3a6d3SLuigi Rizzo #include <win_glue.h>
6537e3a6d3SLuigi Rizzo #endif
6637e3a6d3SLuigi Rizzo 
67ce3ee1e7SLuigi Rizzo #include <net/netmap.h>
68ce3ee1e7SLuigi Rizzo #include <dev/netmap/netmap_kern.h>
6937e3a6d3SLuigi Rizzo #include <net/netmap_virt.h>
70ce3ee1e7SLuigi Rizzo #include "netmap_mem2.h"
71ce3ee1e7SLuigi Rizzo 
7237e3a6d3SLuigi Rizzo #ifdef _WIN32_USE_SMALL_GENERIC_DEVICES_MEMORY
7337e3a6d3SLuigi Rizzo #define NETMAP_BUF_MAX_NUM  8*4096      /* if too big takes too much time to allocate */
7437e3a6d3SLuigi Rizzo #else
754bf50f18SLuigi Rizzo #define NETMAP_BUF_MAX_NUM 20*4096*2	/* large machine */
7637e3a6d3SLuigi Rizzo #endif
774bf50f18SLuigi Rizzo 
784bf50f18SLuigi Rizzo #define NETMAP_POOL_MAX_NAMSZ	32
794bf50f18SLuigi Rizzo 
804bf50f18SLuigi Rizzo 
814bf50f18SLuigi Rizzo enum {
824bf50f18SLuigi Rizzo 	NETMAP_IF_POOL   = 0,
834bf50f18SLuigi Rizzo 	NETMAP_RING_POOL,
844bf50f18SLuigi Rizzo 	NETMAP_BUF_POOL,
854bf50f18SLuigi Rizzo 	NETMAP_POOLS_NR
864bf50f18SLuigi Rizzo };
874bf50f18SLuigi Rizzo 
884bf50f18SLuigi Rizzo 
894bf50f18SLuigi Rizzo struct netmap_obj_params {
904bf50f18SLuigi Rizzo 	u_int size;
914bf50f18SLuigi Rizzo 	u_int num;
92c3e9b4dbSLuiz Otavio O Souza 
93c3e9b4dbSLuiz Otavio O Souza 	u_int last_size;
94c3e9b4dbSLuiz Otavio O Souza 	u_int last_num;
954bf50f18SLuigi Rizzo };
96847bf383SLuigi Rizzo 
974bf50f18SLuigi Rizzo struct netmap_obj_pool {
984bf50f18SLuigi Rizzo 	char name[NETMAP_POOL_MAX_NAMSZ];	/* name of the allocator */
994bf50f18SLuigi Rizzo 
1004bf50f18SLuigi Rizzo 	/* ---------------------------------------------------*/
1014bf50f18SLuigi Rizzo 	/* these are only meaningful if the pool is finalized */
1024bf50f18SLuigi Rizzo 	/* (see 'finalized' field in netmap_mem_d)            */
1034bf50f18SLuigi Rizzo 	u_int objtotal;         /* actual total number of objects. */
1044bf50f18SLuigi Rizzo 	u_int memtotal;		/* actual total memory space */
1054bf50f18SLuigi Rizzo 	u_int numclusters;	/* actual number of clusters */
1064bf50f18SLuigi Rizzo 
1074bf50f18SLuigi Rizzo 	u_int objfree;          /* number of free objects. */
1084bf50f18SLuigi Rizzo 
1094bf50f18SLuigi Rizzo 	struct lut_entry *lut;  /* virt,phys addresses, objtotal entries */
1104bf50f18SLuigi Rizzo 	uint32_t *bitmap;       /* one bit per buffer, 1 means free */
1114f80b14cSVincenzo Maffione 	uint32_t *invalid_bitmap;/* one bit per buffer, 1 means invalid */
1124bf50f18SLuigi Rizzo 	uint32_t bitmap_slots;	/* number of uint32 entries in bitmap */
113*2ff91c17SVincenzo Maffione 	int	alloc_done;	/* we have allocated the memory */
1144bf50f18SLuigi Rizzo 	/* ---------------------------------------------------*/
1154bf50f18SLuigi Rizzo 
1164bf50f18SLuigi Rizzo 	/* limits */
1174bf50f18SLuigi Rizzo 	u_int objminsize;	/* minimum object size */
1184bf50f18SLuigi Rizzo 	u_int objmaxsize;	/* maximum object size */
1194bf50f18SLuigi Rizzo 	u_int nummin;		/* minimum number of objects */
1204bf50f18SLuigi Rizzo 	u_int nummax;		/* maximum number of objects */
1214bf50f18SLuigi Rizzo 
1224bf50f18SLuigi Rizzo 	/* these are changed only by config */
1234bf50f18SLuigi Rizzo 	u_int _objtotal;	/* total number of objects */
1244bf50f18SLuigi Rizzo 	u_int _objsize;		/* object size */
1254bf50f18SLuigi Rizzo 	u_int _clustsize;       /* cluster size */
1264bf50f18SLuigi Rizzo 	u_int _clustentries;    /* objects per cluster */
1274bf50f18SLuigi Rizzo 	u_int _numclusters;	/* number of clusters */
1284bf50f18SLuigi Rizzo 
1294bf50f18SLuigi Rizzo 	/* requested values */
1304bf50f18SLuigi Rizzo 	u_int r_objtotal;
1314bf50f18SLuigi Rizzo 	u_int r_objsize;
1324bf50f18SLuigi Rizzo };
1334bf50f18SLuigi Rizzo 
134847bf383SLuigi Rizzo #define NMA_LOCK_T		NM_MTX_T
135*2ff91c17SVincenzo Maffione #define NMA_LOCK_INIT(n)	NM_MTX_INIT((n)->nm_mtx)
136*2ff91c17SVincenzo Maffione #define NMA_LOCK_DESTROY(n)	NM_MTX_DESTROY((n)->nm_mtx)
137*2ff91c17SVincenzo Maffione #define NMA_LOCK(n)		NM_MTX_LOCK((n)->nm_mtx)
138*2ff91c17SVincenzo Maffione #define NMA_SPINLOCK(n)         NM_MTX_SPINLOCK((n)->nm_mtx)
139*2ff91c17SVincenzo Maffione #define NMA_UNLOCK(n)		NM_MTX_UNLOCK((n)->nm_mtx)
140847bf383SLuigi Rizzo 
141847bf383SLuigi Rizzo struct netmap_mem_ops {
14237e3a6d3SLuigi Rizzo 	int (*nmd_get_lut)(struct netmap_mem_d *, struct netmap_lut*);
1434f80b14cSVincenzo Maffione 	int  (*nmd_get_info)(struct netmap_mem_d *, uint64_t *size,
144847bf383SLuigi Rizzo 			u_int *memflags, uint16_t *id);
145847bf383SLuigi Rizzo 
146847bf383SLuigi Rizzo 	vm_paddr_t (*nmd_ofstophys)(struct netmap_mem_d *, vm_ooffset_t);
147847bf383SLuigi Rizzo 	int (*nmd_config)(struct netmap_mem_d *);
148847bf383SLuigi Rizzo 	int (*nmd_finalize)(struct netmap_mem_d *);
149847bf383SLuigi Rizzo 	void (*nmd_deref)(struct netmap_mem_d *);
150847bf383SLuigi Rizzo 	ssize_t  (*nmd_if_offset)(struct netmap_mem_d *, const void *vaddr);
151847bf383SLuigi Rizzo 	void (*nmd_delete)(struct netmap_mem_d *);
152847bf383SLuigi Rizzo 
153c3e9b4dbSLuiz Otavio O Souza 	struct netmap_if * (*nmd_if_new)(struct netmap_adapter *,
154c3e9b4dbSLuiz Otavio O Souza 					 struct netmap_priv_d *);
155847bf383SLuigi Rizzo 	void (*nmd_if_delete)(struct netmap_adapter *, struct netmap_if *);
156847bf383SLuigi Rizzo 	int  (*nmd_rings_create)(struct netmap_adapter *);
157847bf383SLuigi Rizzo 	void (*nmd_rings_delete)(struct netmap_adapter *);
158847bf383SLuigi Rizzo };
1594bf50f18SLuigi Rizzo 
1604bf50f18SLuigi Rizzo struct netmap_mem_d {
1614bf50f18SLuigi Rizzo 	NMA_LOCK_T nm_mtx;  /* protect the allocator */
1624bf50f18SLuigi Rizzo 	u_int nm_totalsize; /* shorthand */
1634bf50f18SLuigi Rizzo 
1644bf50f18SLuigi Rizzo 	u_int flags;
1654bf50f18SLuigi Rizzo #define NETMAP_MEM_FINALIZED	0x1	/* preallocation done */
166c3e9b4dbSLuiz Otavio O Souza #define NETMAP_MEM_HIDDEN	0x8	/* beeing prepared */
1674bf50f18SLuigi Rizzo 	int lasterr;		/* last error for curr config */
168847bf383SLuigi Rizzo 	int active;		/* active users */
169847bf383SLuigi Rizzo 	int refcount;
1704bf50f18SLuigi Rizzo 	/* the three allocators */
1714bf50f18SLuigi Rizzo 	struct netmap_obj_pool pools[NETMAP_POOLS_NR];
1724bf50f18SLuigi Rizzo 
1734bf50f18SLuigi Rizzo 	nm_memid_t nm_id;	/* allocator identifier */
1744bf50f18SLuigi Rizzo 	int nm_grp;	/* iommu groupd id */
1754bf50f18SLuigi Rizzo 
1764bf50f18SLuigi Rizzo 	/* list of all existing allocators, sorted by nm_id */
1774bf50f18SLuigi Rizzo 	struct netmap_mem_d *prev, *next;
178847bf383SLuigi Rizzo 
179847bf383SLuigi Rizzo 	struct netmap_mem_ops *ops;
180c3e9b4dbSLuiz Otavio O Souza 
181c3e9b4dbSLuiz Otavio O Souza 	struct netmap_obj_params params[NETMAP_POOLS_NR];
182c3e9b4dbSLuiz Otavio O Souza 
183c3e9b4dbSLuiz Otavio O Souza #define NM_MEM_NAMESZ	16
184c3e9b4dbSLuiz Otavio O Souza 	char name[NM_MEM_NAMESZ];
1854bf50f18SLuigi Rizzo };
1864bf50f18SLuigi Rizzo 
187*2ff91c17SVincenzo Maffione int
188*2ff91c17SVincenzo Maffione netmap_mem_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut)
189*2ff91c17SVincenzo Maffione {
190*2ff91c17SVincenzo Maffione 	int rv;
191*2ff91c17SVincenzo Maffione 
192*2ff91c17SVincenzo Maffione 	NMA_LOCK(nmd);
193*2ff91c17SVincenzo Maffione 	rv = nmd->ops->nmd_get_lut(nmd, lut);
194*2ff91c17SVincenzo Maffione 	NMA_UNLOCK(nmd);
195*2ff91c17SVincenzo Maffione 
196*2ff91c17SVincenzo Maffione 	return rv;
197*2ff91c17SVincenzo Maffione }
198*2ff91c17SVincenzo Maffione 
199*2ff91c17SVincenzo Maffione int
200*2ff91c17SVincenzo Maffione netmap_mem_get_info(struct netmap_mem_d *nmd, uint64_t *size,
201*2ff91c17SVincenzo Maffione 		u_int *memflags, nm_memid_t *memid)
202*2ff91c17SVincenzo Maffione {
203*2ff91c17SVincenzo Maffione 	int rv;
204*2ff91c17SVincenzo Maffione 
205*2ff91c17SVincenzo Maffione 	NMA_LOCK(nmd);
206*2ff91c17SVincenzo Maffione 	rv = nmd->ops->nmd_get_info(nmd, size, memflags, memid);
207*2ff91c17SVincenzo Maffione 	NMA_UNLOCK(nmd);
208*2ff91c17SVincenzo Maffione 
209*2ff91c17SVincenzo Maffione 	return rv;
210*2ff91c17SVincenzo Maffione }
211*2ff91c17SVincenzo Maffione 
212*2ff91c17SVincenzo Maffione vm_paddr_t
213*2ff91c17SVincenzo Maffione netmap_mem_ofstophys(struct netmap_mem_d *nmd, vm_ooffset_t off)
214*2ff91c17SVincenzo Maffione {
215*2ff91c17SVincenzo Maffione 	vm_paddr_t pa;
216*2ff91c17SVincenzo Maffione 
217*2ff91c17SVincenzo Maffione #if defined(__FreeBSD__)
218*2ff91c17SVincenzo Maffione 	/* This function is called by netmap_dev_pager_fault(), which holds a
219*2ff91c17SVincenzo Maffione 	 * non-sleepable lock since FreeBSD 12. Since we cannot sleep, we
220*2ff91c17SVincenzo Maffione 	 * spin on the trylock. */
221*2ff91c17SVincenzo Maffione 	NMA_SPINLOCK(nmd);
222*2ff91c17SVincenzo Maffione #else
223*2ff91c17SVincenzo Maffione 	NMA_LOCK(nmd);
224*2ff91c17SVincenzo Maffione #endif
225*2ff91c17SVincenzo Maffione 	pa = nmd->ops->nmd_ofstophys(nmd, off);
226*2ff91c17SVincenzo Maffione 	NMA_UNLOCK(nmd);
227*2ff91c17SVincenzo Maffione 
228*2ff91c17SVincenzo Maffione 	return pa;
229*2ff91c17SVincenzo Maffione }
230*2ff91c17SVincenzo Maffione 
231*2ff91c17SVincenzo Maffione static int
232*2ff91c17SVincenzo Maffione netmap_mem_config(struct netmap_mem_d *nmd)
233*2ff91c17SVincenzo Maffione {
234*2ff91c17SVincenzo Maffione 	if (nmd->active) {
235*2ff91c17SVincenzo Maffione 		/* already in use. Not fatal, but we
236*2ff91c17SVincenzo Maffione 		 * cannot change the configuration
23737e3a6d3SLuigi Rizzo 		 */
238*2ff91c17SVincenzo Maffione 		return 0;
239847bf383SLuigi Rizzo 	}
240847bf383SLuigi Rizzo 
241*2ff91c17SVincenzo Maffione 	return nmd->ops->nmd_config(nmd);
242847bf383SLuigi Rizzo }
243847bf383SLuigi Rizzo 
244*2ff91c17SVincenzo Maffione ssize_t
245*2ff91c17SVincenzo Maffione netmap_mem_if_offset(struct netmap_mem_d *nmd, const void *off)
246*2ff91c17SVincenzo Maffione {
247*2ff91c17SVincenzo Maffione 	ssize_t rv;
248*2ff91c17SVincenzo Maffione 
249*2ff91c17SVincenzo Maffione 	NMA_LOCK(nmd);
250*2ff91c17SVincenzo Maffione 	rv = nmd->ops->nmd_if_offset(nmd, off);
251*2ff91c17SVincenzo Maffione 	NMA_UNLOCK(nmd);
252*2ff91c17SVincenzo Maffione 
253*2ff91c17SVincenzo Maffione 	return rv;
254847bf383SLuigi Rizzo }
255847bf383SLuigi Rizzo 
256*2ff91c17SVincenzo Maffione static void
257*2ff91c17SVincenzo Maffione netmap_mem_delete(struct netmap_mem_d *nmd)
258*2ff91c17SVincenzo Maffione {
259*2ff91c17SVincenzo Maffione 	nmd->ops->nmd_delete(nmd);
260847bf383SLuigi Rizzo }
261847bf383SLuigi Rizzo 
262*2ff91c17SVincenzo Maffione struct netmap_if *
263*2ff91c17SVincenzo Maffione netmap_mem_if_new(struct netmap_adapter *na, struct netmap_priv_d *priv)
264*2ff91c17SVincenzo Maffione {
265*2ff91c17SVincenzo Maffione 	struct netmap_if *nifp;
266*2ff91c17SVincenzo Maffione 	struct netmap_mem_d *nmd = na->nm_mem;
267*2ff91c17SVincenzo Maffione 
268*2ff91c17SVincenzo Maffione 	NMA_LOCK(nmd);
269*2ff91c17SVincenzo Maffione 	nifp = nmd->ops->nmd_if_new(na, priv);
270*2ff91c17SVincenzo Maffione 	NMA_UNLOCK(nmd);
271*2ff91c17SVincenzo Maffione 
272*2ff91c17SVincenzo Maffione 	return nifp;
273847bf383SLuigi Rizzo }
274847bf383SLuigi Rizzo 
275*2ff91c17SVincenzo Maffione void
276*2ff91c17SVincenzo Maffione netmap_mem_if_delete(struct netmap_adapter *na, struct netmap_if *nif)
277*2ff91c17SVincenzo Maffione {
278*2ff91c17SVincenzo Maffione 	struct netmap_mem_d *nmd = na->nm_mem;
279847bf383SLuigi Rizzo 
280*2ff91c17SVincenzo Maffione 	NMA_LOCK(nmd);
281*2ff91c17SVincenzo Maffione 	nmd->ops->nmd_if_delete(na, nif);
282*2ff91c17SVincenzo Maffione 	NMA_UNLOCK(nmd);
283*2ff91c17SVincenzo Maffione }
284*2ff91c17SVincenzo Maffione 
285*2ff91c17SVincenzo Maffione int
286*2ff91c17SVincenzo Maffione netmap_mem_rings_create(struct netmap_adapter *na)
287*2ff91c17SVincenzo Maffione {
288*2ff91c17SVincenzo Maffione 	int rv;
289*2ff91c17SVincenzo Maffione 	struct netmap_mem_d *nmd = na->nm_mem;
290*2ff91c17SVincenzo Maffione 
291*2ff91c17SVincenzo Maffione 	NMA_LOCK(nmd);
292*2ff91c17SVincenzo Maffione 	rv = nmd->ops->nmd_rings_create(na);
293*2ff91c17SVincenzo Maffione 	NMA_UNLOCK(nmd);
294*2ff91c17SVincenzo Maffione 
295*2ff91c17SVincenzo Maffione 	return rv;
296*2ff91c17SVincenzo Maffione }
297*2ff91c17SVincenzo Maffione 
298*2ff91c17SVincenzo Maffione void
299*2ff91c17SVincenzo Maffione netmap_mem_rings_delete(struct netmap_adapter *na)
300*2ff91c17SVincenzo Maffione {
301*2ff91c17SVincenzo Maffione 	struct netmap_mem_d *nmd = na->nm_mem;
302*2ff91c17SVincenzo Maffione 
303*2ff91c17SVincenzo Maffione 	NMA_LOCK(nmd);
304*2ff91c17SVincenzo Maffione 	nmd->ops->nmd_rings_delete(na);
305*2ff91c17SVincenzo Maffione 	NMA_UNLOCK(nmd);
306*2ff91c17SVincenzo Maffione }
307847bf383SLuigi Rizzo 
308847bf383SLuigi Rizzo static int netmap_mem_map(struct netmap_obj_pool *, struct netmap_adapter *);
309847bf383SLuigi Rizzo static int netmap_mem_unmap(struct netmap_obj_pool *, struct netmap_adapter *);
31037e3a6d3SLuigi Rizzo static int nm_mem_assign_group(struct netmap_mem_d *, struct device *);
311c3e9b4dbSLuiz Otavio O Souza static void nm_mem_release_id(struct netmap_mem_d *);
312c3e9b4dbSLuiz Otavio O Souza 
313c3e9b4dbSLuiz Otavio O Souza nm_memid_t
314c3e9b4dbSLuiz Otavio O Souza netmap_mem_get_id(struct netmap_mem_d *nmd)
315c3e9b4dbSLuiz Otavio O Souza {
316c3e9b4dbSLuiz Otavio O Souza 	return nmd->nm_id;
317c3e9b4dbSLuiz Otavio O Souza }
318847bf383SLuigi Rizzo 
319847bf383SLuigi Rizzo #ifdef NM_DEBUG_MEM_PUTGET
320847bf383SLuigi Rizzo #define NM_DBG_REFC(nmd, func, line)	\
321c3e9b4dbSLuiz Otavio O Souza 	nm_prinf("%s:%d mem[%d] -> %d\n", func, line, (nmd)->nm_id, (nmd)->refcount);
322847bf383SLuigi Rizzo #else
323847bf383SLuigi Rizzo #define NM_DBG_REFC(nmd, func, line)
324847bf383SLuigi Rizzo #endif
325847bf383SLuigi Rizzo 
326c3e9b4dbSLuiz Otavio O Souza /* circular list of all existing allocators */
327c3e9b4dbSLuiz Otavio O Souza static struct netmap_mem_d *netmap_last_mem_d = &nm_mem;
328c3e9b4dbSLuiz Otavio O Souza NM_MTX_T nm_mem_list_lock;
329c3e9b4dbSLuiz Otavio O Souza 
330c3e9b4dbSLuiz Otavio O Souza struct netmap_mem_d *
331c3e9b4dbSLuiz Otavio O Souza __netmap_mem_get(struct netmap_mem_d *nmd, const char *func, int line)
332847bf383SLuigi Rizzo {
333c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_LOCK(nm_mem_list_lock);
334847bf383SLuigi Rizzo 	nmd->refcount++;
335847bf383SLuigi Rizzo 	NM_DBG_REFC(nmd, func, line);
336c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_UNLOCK(nm_mem_list_lock);
337c3e9b4dbSLuiz Otavio O Souza 	return nmd;
338847bf383SLuigi Rizzo }
339847bf383SLuigi Rizzo 
340c3e9b4dbSLuiz Otavio O Souza void
341c3e9b4dbSLuiz Otavio O Souza __netmap_mem_put(struct netmap_mem_d *nmd, const char *func, int line)
342847bf383SLuigi Rizzo {
343847bf383SLuigi Rizzo 	int last;
344c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_LOCK(nm_mem_list_lock);
345847bf383SLuigi Rizzo 	last = (--nmd->refcount == 0);
346c3e9b4dbSLuiz Otavio O Souza 	if (last)
347c3e9b4dbSLuiz Otavio O Souza 		nm_mem_release_id(nmd);
348847bf383SLuigi Rizzo 	NM_DBG_REFC(nmd, func, line);
349c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_UNLOCK(nm_mem_list_lock);
350847bf383SLuigi Rizzo 	if (last)
351847bf383SLuigi Rizzo 		netmap_mem_delete(nmd);
352847bf383SLuigi Rizzo }
353847bf383SLuigi Rizzo 
354847bf383SLuigi Rizzo int
355847bf383SLuigi Rizzo netmap_mem_finalize(struct netmap_mem_d *nmd, struct netmap_adapter *na)
356847bf383SLuigi Rizzo {
357*2ff91c17SVincenzo Maffione 	int lasterr = 0;
358847bf383SLuigi Rizzo 	if (nm_mem_assign_group(nmd, na->pdev) < 0) {
359847bf383SLuigi Rizzo 		return ENOMEM;
360847bf383SLuigi Rizzo 	}
361847bf383SLuigi Rizzo 
362*2ff91c17SVincenzo Maffione 	NMA_LOCK(nmd);
363*2ff91c17SVincenzo Maffione 
364*2ff91c17SVincenzo Maffione 	if (netmap_mem_config(nmd))
365*2ff91c17SVincenzo Maffione 		goto out;
366*2ff91c17SVincenzo Maffione 
367*2ff91c17SVincenzo Maffione 	nmd->active++;
368*2ff91c17SVincenzo Maffione 
369*2ff91c17SVincenzo Maffione 	nmd->lasterr = nmd->ops->nmd_finalize(nmd);
370*2ff91c17SVincenzo Maffione 
3714f80b14cSVincenzo Maffione 	if (!nmd->lasterr && na->pdev) {
3724f80b14cSVincenzo Maffione 		nmd->lasterr = netmap_mem_map(&nmd->pools[NETMAP_BUF_POOL], na);
3734f80b14cSVincenzo Maffione 	}
374847bf383SLuigi Rizzo 
375*2ff91c17SVincenzo Maffione out:
376*2ff91c17SVincenzo Maffione 	lasterr = nmd->lasterr;
377*2ff91c17SVincenzo Maffione 	NMA_UNLOCK(nmd);
378*2ff91c17SVincenzo Maffione 
379*2ff91c17SVincenzo Maffione 	if (lasterr)
380*2ff91c17SVincenzo Maffione 		netmap_mem_deref(nmd, na);
381*2ff91c17SVincenzo Maffione 
382*2ff91c17SVincenzo Maffione 	return lasterr;
383847bf383SLuigi Rizzo }
384847bf383SLuigi Rizzo 
3854f80b14cSVincenzo Maffione static int
3864f80b14cSVincenzo Maffione nm_isset(uint32_t *bitmap, u_int i)
387847bf383SLuigi Rizzo {
3884f80b14cSVincenzo Maffione 	return bitmap[ (i>>5) ] & ( 1U << (i & 31U) );
3894f80b14cSVincenzo Maffione }
39037e3a6d3SLuigi Rizzo 
39137e3a6d3SLuigi Rizzo 
3924f80b14cSVincenzo Maffione static int
3934f80b14cSVincenzo Maffione netmap_init_obj_allocator_bitmap(struct netmap_obj_pool *p)
3944f80b14cSVincenzo Maffione {
3954f80b14cSVincenzo Maffione 	u_int n, j;
39637e3a6d3SLuigi Rizzo 
3974f80b14cSVincenzo Maffione 	if (p->bitmap == NULL) {
3984f80b14cSVincenzo Maffione 		/* Allocate the bitmap */
3994f80b14cSVincenzo Maffione 		n = (p->objtotal + 31) / 32;
4004f80b14cSVincenzo Maffione 		p->bitmap = nm_os_malloc(sizeof(uint32_t) * n);
4014f80b14cSVincenzo Maffione 		if (p->bitmap == NULL) {
4024f80b14cSVincenzo Maffione 			D("Unable to create bitmap (%d entries) for allocator '%s'", (int)n,
4034f80b14cSVincenzo Maffione 			    p->name);
4044f80b14cSVincenzo Maffione 			return ENOMEM;
4054f80b14cSVincenzo Maffione 		}
4064f80b14cSVincenzo Maffione 		p->bitmap_slots = n;
4074f80b14cSVincenzo Maffione 	} else {
4084f80b14cSVincenzo Maffione 		memset(p->bitmap, 0, p->bitmap_slots);
4094f80b14cSVincenzo Maffione 	}
4104f80b14cSVincenzo Maffione 
4114f80b14cSVincenzo Maffione 	p->objfree = 0;
41237e3a6d3SLuigi Rizzo 	/*
41337e3a6d3SLuigi Rizzo 	 * Set all the bits in the bitmap that have
41437e3a6d3SLuigi Rizzo 	 * corresponding buffers to 1 to indicate they are
41537e3a6d3SLuigi Rizzo 	 * free.
41637e3a6d3SLuigi Rizzo 	 */
41737e3a6d3SLuigi Rizzo 	for (j = 0; j < p->objtotal; j++) {
4184f80b14cSVincenzo Maffione 		if (p->invalid_bitmap && nm_isset(p->invalid_bitmap, j)) {
4194f80b14cSVincenzo Maffione 			D("skipping %s %d", p->name, j);
4204f80b14cSVincenzo Maffione 			continue;
42137e3a6d3SLuigi Rizzo 		}
4224f80b14cSVincenzo Maffione 		p->bitmap[ (j>>5) ] |=  ( 1U << (j & 31U) );
4234f80b14cSVincenzo Maffione 		p->objfree++;
42437e3a6d3SLuigi Rizzo 	}
4254f80b14cSVincenzo Maffione 
4264f80b14cSVincenzo Maffione 	ND("%s free %u", p->name, p->objfree);
4274f80b14cSVincenzo Maffione 	if (p->objfree == 0)
4284f80b14cSVincenzo Maffione 		return ENOMEM;
4294f80b14cSVincenzo Maffione 
4304f80b14cSVincenzo Maffione 	return 0;
4314f80b14cSVincenzo Maffione }
4324f80b14cSVincenzo Maffione 
4334f80b14cSVincenzo Maffione static int
4344f80b14cSVincenzo Maffione netmap_mem_init_bitmaps(struct netmap_mem_d *nmd)
4354f80b14cSVincenzo Maffione {
4364f80b14cSVincenzo Maffione 	int i, error = 0;
4374f80b14cSVincenzo Maffione 
4384f80b14cSVincenzo Maffione 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
4394f80b14cSVincenzo Maffione 		struct netmap_obj_pool *p = &nmd->pools[i];
4404f80b14cSVincenzo Maffione 
4414f80b14cSVincenzo Maffione 		error = netmap_init_obj_allocator_bitmap(p);
4424f80b14cSVincenzo Maffione 		if (error)
4434f80b14cSVincenzo Maffione 			return error;
44437e3a6d3SLuigi Rizzo 	}
44537e3a6d3SLuigi Rizzo 
44637e3a6d3SLuigi Rizzo 	/*
44737e3a6d3SLuigi Rizzo 	 * buffers 0 and 1 are reserved
44837e3a6d3SLuigi Rizzo 	 */
4494f80b14cSVincenzo Maffione 	if (nmd->pools[NETMAP_BUF_POOL].objfree < 2) {
4504f80b14cSVincenzo Maffione 		return ENOMEM;
4514f80b14cSVincenzo Maffione 	}
4524f80b14cSVincenzo Maffione 
45337e3a6d3SLuigi Rizzo 	nmd->pools[NETMAP_BUF_POOL].objfree -= 2;
45437e3a6d3SLuigi Rizzo 	if (nmd->pools[NETMAP_BUF_POOL].bitmap) {
45537e3a6d3SLuigi Rizzo 		/* XXX This check is a workaround that prevents a
45637e3a6d3SLuigi Rizzo 		 * NULL pointer crash which currently happens only
457844a6f0cSLuigi Rizzo 		 * with ptnetmap guests.
458844a6f0cSLuigi Rizzo 		 * Removed shared-info --> is the bug still there? */
4594f80b14cSVincenzo Maffione 		nmd->pools[NETMAP_BUF_POOL].bitmap[0] = ~3U;
46037e3a6d3SLuigi Rizzo 	}
4614f80b14cSVincenzo Maffione 	return 0;
4624f80b14cSVincenzo Maffione }
4634f80b14cSVincenzo Maffione 
4644f80b14cSVincenzo Maffione int
4654f80b14cSVincenzo Maffione netmap_mem_deref(struct netmap_mem_d *nmd, struct netmap_adapter *na)
4664f80b14cSVincenzo Maffione {
4674f80b14cSVincenzo Maffione 	int last_user = 0;
4684f80b14cSVincenzo Maffione 	NMA_LOCK(nmd);
4694f80b14cSVincenzo Maffione 	if (na->active_fds <= 0)
4704f80b14cSVincenzo Maffione 		netmap_mem_unmap(&nmd->pools[NETMAP_BUF_POOL], na);
4714f80b14cSVincenzo Maffione 	if (nmd->active == 1) {
4724f80b14cSVincenzo Maffione 		last_user = 1;
4734f80b14cSVincenzo Maffione 		/*
4744f80b14cSVincenzo Maffione 		 * Reset the allocator when it falls out of use so that any
4754f80b14cSVincenzo Maffione 		 * pool resources leaked by unclean application exits are
4764f80b14cSVincenzo Maffione 		 * reclaimed.
4774f80b14cSVincenzo Maffione 		 */
4784f80b14cSVincenzo Maffione 		netmap_mem_init_bitmaps(nmd);
47937e3a6d3SLuigi Rizzo 	}
48037e3a6d3SLuigi Rizzo 	nmd->ops->nmd_deref(nmd);
48137e3a6d3SLuigi Rizzo 
482*2ff91c17SVincenzo Maffione 	nmd->active--;
483*2ff91c17SVincenzo Maffione 	if (!nmd->active)
484*2ff91c17SVincenzo Maffione 		nmd->nm_grp = -1;
485*2ff91c17SVincenzo Maffione 
486847bf383SLuigi Rizzo 	NMA_UNLOCK(nmd);
4874f80b14cSVincenzo Maffione 	return last_user;
488847bf383SLuigi Rizzo }
489847bf383SLuigi Rizzo 
490847bf383SLuigi Rizzo 
4914bf50f18SLuigi Rizzo /* accessor functions */
49237e3a6d3SLuigi Rizzo static int
493847bf383SLuigi Rizzo netmap_mem2_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut)
4944bf50f18SLuigi Rizzo {
495847bf383SLuigi Rizzo 	lut->lut = nmd->pools[NETMAP_BUF_POOL].lut;
4964f80b14cSVincenzo Maffione #ifdef __FreeBSD__
4974f80b14cSVincenzo Maffione 	lut->plut = lut->lut;
4984f80b14cSVincenzo Maffione #endif
499847bf383SLuigi Rizzo 	lut->objtotal = nmd->pools[NETMAP_BUF_POOL].objtotal;
500847bf383SLuigi Rizzo 	lut->objsize = nmd->pools[NETMAP_BUF_POOL]._objsize;
50137e3a6d3SLuigi Rizzo 
50237e3a6d3SLuigi Rizzo 	return 0;
5034bf50f18SLuigi Rizzo }
5044bf50f18SLuigi Rizzo 
50537e3a6d3SLuigi Rizzo static struct netmap_obj_params netmap_min_priv_params[NETMAP_POOLS_NR] = {
506f0ea3689SLuigi Rizzo 	[NETMAP_IF_POOL] = {
507f0ea3689SLuigi Rizzo 		.size = 1024,
50837e3a6d3SLuigi Rizzo 		.num  = 2,
509f0ea3689SLuigi Rizzo 	},
510f0ea3689SLuigi Rizzo 	[NETMAP_RING_POOL] = {
511f0ea3689SLuigi Rizzo 		.size = 5*PAGE_SIZE,
512f0ea3689SLuigi Rizzo 		.num  = 4,
513f0ea3689SLuigi Rizzo 	},
514f0ea3689SLuigi Rizzo 	[NETMAP_BUF_POOL] = {
515f0ea3689SLuigi Rizzo 		.size = 2048,
516f0ea3689SLuigi Rizzo 		.num  = 4098,
517f0ea3689SLuigi Rizzo 	},
518f0ea3689SLuigi Rizzo };
519f0ea3689SLuigi Rizzo 
520ccdc3305SLuigi Rizzo 
5212579e2d7SLuigi Rizzo /*
5222579e2d7SLuigi Rizzo  * nm_mem is the memory allocator used for all physical interfaces
5232579e2d7SLuigi Rizzo  * running in netmap mode.
5242579e2d7SLuigi Rizzo  * Virtual (VALE) ports will have each its own allocator.
5252579e2d7SLuigi Rizzo  */
526847bf383SLuigi Rizzo extern struct netmap_mem_ops netmap_mem_global_ops; /* forward */
527ce3ee1e7SLuigi Rizzo struct netmap_mem_d nm_mem = {	/* Our memory allocator. */
5288241616dSLuigi Rizzo 	.pools = {
5298241616dSLuigi Rizzo 		[NETMAP_IF_POOL] = {
5308241616dSLuigi Rizzo 			.name 	= "netmap_if",
5318241616dSLuigi Rizzo 			.objminsize = sizeof(struct netmap_if),
5328241616dSLuigi Rizzo 			.objmaxsize = 4096,
5338241616dSLuigi Rizzo 			.nummin     = 10,	/* don't be stingy */
5348241616dSLuigi Rizzo 			.nummax	    = 10000,	/* XXX very large */
5358241616dSLuigi Rizzo 		},
5368241616dSLuigi Rizzo 		[NETMAP_RING_POOL] = {
5378241616dSLuigi Rizzo 			.name 	= "netmap_ring",
5388241616dSLuigi Rizzo 			.objminsize = sizeof(struct netmap_ring),
5398241616dSLuigi Rizzo 			.objmaxsize = 32*PAGE_SIZE,
5408241616dSLuigi Rizzo 			.nummin     = 2,
5418241616dSLuigi Rizzo 			.nummax	    = 1024,
5428241616dSLuigi Rizzo 		},
5438241616dSLuigi Rizzo 		[NETMAP_BUF_POOL] = {
5448241616dSLuigi Rizzo 			.name	= "netmap_buf",
5458241616dSLuigi Rizzo 			.objminsize = 64,
5468241616dSLuigi Rizzo 			.objmaxsize = 65536,
5478241616dSLuigi Rizzo 			.nummin     = 4,
5488241616dSLuigi Rizzo 			.nummax	    = 1000000, /* one million! */
5498241616dSLuigi Rizzo 		},
5508241616dSLuigi Rizzo 	},
551f0ea3689SLuigi Rizzo 
552c3e9b4dbSLuiz Otavio O Souza 	.params = {
553c3e9b4dbSLuiz Otavio O Souza 		[NETMAP_IF_POOL] = {
554c3e9b4dbSLuiz Otavio O Souza 			.size = 1024,
555c3e9b4dbSLuiz Otavio O Souza 			.num  = 100,
556c3e9b4dbSLuiz Otavio O Souza 		},
557c3e9b4dbSLuiz Otavio O Souza 		[NETMAP_RING_POOL] = {
558c3e9b4dbSLuiz Otavio O Souza 			.size = 9*PAGE_SIZE,
559c3e9b4dbSLuiz Otavio O Souza 			.num  = 200,
560c3e9b4dbSLuiz Otavio O Souza 		},
561c3e9b4dbSLuiz Otavio O Souza 		[NETMAP_BUF_POOL] = {
562c3e9b4dbSLuiz Otavio O Souza 			.size = 2048,
563c3e9b4dbSLuiz Otavio O Souza 			.num  = NETMAP_BUF_MAX_NUM,
564c3e9b4dbSLuiz Otavio O Souza 		},
565c3e9b4dbSLuiz Otavio O Souza 	},
566c3e9b4dbSLuiz Otavio O Souza 
567f0ea3689SLuigi Rizzo 	.nm_id = 1,
5684bf50f18SLuigi Rizzo 	.nm_grp = -1,
569f0ea3689SLuigi Rizzo 
570f0ea3689SLuigi Rizzo 	.prev = &nm_mem,
571f0ea3689SLuigi Rizzo 	.next = &nm_mem,
572847bf383SLuigi Rizzo 
573c3e9b4dbSLuiz Otavio O Souza 	.ops = &netmap_mem_global_ops,
574c3e9b4dbSLuiz Otavio O Souza 
575c3e9b4dbSLuiz Otavio O Souza 	.name = "1"
576ccdc3305SLuigi Rizzo };
577ccdc3305SLuigi Rizzo 
578ce3ee1e7SLuigi Rizzo 
579ce3ee1e7SLuigi Rizzo /* blueprint for the private memory allocators */
58037e3a6d3SLuigi Rizzo /* XXX clang is not happy about using name as a print format */
58137e3a6d3SLuigi Rizzo static const struct netmap_mem_d nm_blueprint = {
582ce3ee1e7SLuigi Rizzo 	.pools = {
583ce3ee1e7SLuigi Rizzo 		[NETMAP_IF_POOL] = {
584ce3ee1e7SLuigi Rizzo 			.name 	= "%s_if",
585ce3ee1e7SLuigi Rizzo 			.objminsize = sizeof(struct netmap_if),
586ce3ee1e7SLuigi Rizzo 			.objmaxsize = 4096,
587ce3ee1e7SLuigi Rizzo 			.nummin     = 1,
588f0ea3689SLuigi Rizzo 			.nummax	    = 100,
589ce3ee1e7SLuigi Rizzo 		},
590ce3ee1e7SLuigi Rizzo 		[NETMAP_RING_POOL] = {
591ce3ee1e7SLuigi Rizzo 			.name 	= "%s_ring",
592ce3ee1e7SLuigi Rizzo 			.objminsize = sizeof(struct netmap_ring),
593ce3ee1e7SLuigi Rizzo 			.objmaxsize = 32*PAGE_SIZE,
594ce3ee1e7SLuigi Rizzo 			.nummin     = 2,
595ce3ee1e7SLuigi Rizzo 			.nummax	    = 1024,
596ce3ee1e7SLuigi Rizzo 		},
597ce3ee1e7SLuigi Rizzo 		[NETMAP_BUF_POOL] = {
598ce3ee1e7SLuigi Rizzo 			.name	= "%s_buf",
599ce3ee1e7SLuigi Rizzo 			.objminsize = 64,
600ce3ee1e7SLuigi Rizzo 			.objmaxsize = 65536,
601ce3ee1e7SLuigi Rizzo 			.nummin     = 4,
602ce3ee1e7SLuigi Rizzo 			.nummax	    = 1000000, /* one million! */
603ce3ee1e7SLuigi Rizzo 		},
604ce3ee1e7SLuigi Rizzo 	},
605ce3ee1e7SLuigi Rizzo 
606c3e9b4dbSLuiz Otavio O Souza 	.nm_grp = -1,
607c3e9b4dbSLuiz Otavio O Souza 
608ce3ee1e7SLuigi Rizzo 	.flags = NETMAP_MEM_PRIVATE,
609847bf383SLuigi Rizzo 
610c3e9b4dbSLuiz Otavio O Souza 	.ops = &netmap_mem_global_ops,
611ce3ee1e7SLuigi Rizzo };
612ce3ee1e7SLuigi Rizzo 
6138241616dSLuigi Rizzo /* memory allocator related sysctls */
6148241616dSLuigi Rizzo 
6158241616dSLuigi Rizzo #define STRINGIFY(x) #x
6168241616dSLuigi Rizzo 
617ce3ee1e7SLuigi Rizzo 
6188241616dSLuigi Rizzo #define DECLARE_SYSCTLS(id, name) \
61937e3a6d3SLuigi Rizzo 	SYSBEGIN(mem2_ ## name); \
6208241616dSLuigi Rizzo 	SYSCTL_INT(_dev_netmap, OID_AUTO, name##_size, \
621c3e9b4dbSLuiz Otavio O Souza 	    CTLFLAG_RW, &nm_mem.params[id].size, 0, "Requested size of netmap " STRINGIFY(name) "s"); \
6228241616dSLuigi Rizzo 	SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_size, \
6238241616dSLuigi Rizzo 	    CTLFLAG_RD, &nm_mem.pools[id]._objsize, 0, "Current size of netmap " STRINGIFY(name) "s"); \
6248241616dSLuigi Rizzo 	SYSCTL_INT(_dev_netmap, OID_AUTO, name##_num, \
625c3e9b4dbSLuiz Otavio O Souza 	    CTLFLAG_RW, &nm_mem.params[id].num, 0, "Requested number of netmap " STRINGIFY(name) "s"); \
6268241616dSLuigi Rizzo 	SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_num, \
627f0ea3689SLuigi Rizzo 	    CTLFLAG_RD, &nm_mem.pools[id].objtotal, 0, "Current number of netmap " STRINGIFY(name) "s"); \
628f0ea3689SLuigi Rizzo 	SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_size, \
629f0ea3689SLuigi Rizzo 	    CTLFLAG_RW, &netmap_min_priv_params[id].size, 0, \
630f0ea3689SLuigi Rizzo 	    "Default size of private netmap " STRINGIFY(name) "s"); \
631f0ea3689SLuigi Rizzo 	SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_num, \
632f0ea3689SLuigi Rizzo 	    CTLFLAG_RW, &netmap_min_priv_params[id].num, 0, \
63337e3a6d3SLuigi Rizzo 	    "Default number of private netmap " STRINGIFY(name) "s");	\
63437e3a6d3SLuigi Rizzo 	SYSEND
6358241616dSLuigi Rizzo 
636984ff0d9SEd Maste SYSCTL_DECL(_dev_netmap);
6378241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_IF_POOL, if);
6388241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_RING_POOL, ring);
6398241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_BUF_POOL, buf);
640ccdc3305SLuigi Rizzo 
641c3e9b4dbSLuiz Otavio O Souza /* call with nm_mem_list_lock held */
642f0ea3689SLuigi Rizzo static int
64337e3a6d3SLuigi Rizzo nm_mem_assign_id_locked(struct netmap_mem_d *nmd)
644f0ea3689SLuigi Rizzo {
645f0ea3689SLuigi Rizzo 	nm_memid_t id;
646f0ea3689SLuigi Rizzo 	struct netmap_mem_d *scan = netmap_last_mem_d;
647f0ea3689SLuigi Rizzo 	int error = ENOMEM;
648f0ea3689SLuigi Rizzo 
649f0ea3689SLuigi Rizzo 	do {
650f0ea3689SLuigi Rizzo 		/* we rely on unsigned wrap around */
651f0ea3689SLuigi Rizzo 		id = scan->nm_id + 1;
652f0ea3689SLuigi Rizzo 		if (id == 0) /* reserve 0 as error value */
653f0ea3689SLuigi Rizzo 			id = 1;
654f0ea3689SLuigi Rizzo 		scan = scan->next;
655f0ea3689SLuigi Rizzo 		if (id != scan->nm_id) {
656f0ea3689SLuigi Rizzo 			nmd->nm_id = id;
657f0ea3689SLuigi Rizzo 			nmd->prev = scan->prev;
658f0ea3689SLuigi Rizzo 			nmd->next = scan;
659f0ea3689SLuigi Rizzo 			scan->prev->next = nmd;
660f0ea3689SLuigi Rizzo 			scan->prev = nmd;
661f0ea3689SLuigi Rizzo 			netmap_last_mem_d = nmd;
662c3e9b4dbSLuiz Otavio O Souza 			nmd->refcount = 1;
663c3e9b4dbSLuiz Otavio O Souza 			NM_DBG_REFC(nmd, __FUNCTION__, __LINE__);
664f0ea3689SLuigi Rizzo 			error = 0;
665f0ea3689SLuigi Rizzo 			break;
666f0ea3689SLuigi Rizzo 		}
667f0ea3689SLuigi Rizzo 	} while (scan != netmap_last_mem_d);
668f0ea3689SLuigi Rizzo 
669f0ea3689SLuigi Rizzo 	return error;
670f0ea3689SLuigi Rizzo }
671f0ea3689SLuigi Rizzo 
672c3e9b4dbSLuiz Otavio O Souza /* call with nm_mem_list_lock *not* held */
67337e3a6d3SLuigi Rizzo static int
67437e3a6d3SLuigi Rizzo nm_mem_assign_id(struct netmap_mem_d *nmd)
67537e3a6d3SLuigi Rizzo {
67637e3a6d3SLuigi Rizzo         int ret;
67737e3a6d3SLuigi Rizzo 
678c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_LOCK(nm_mem_list_lock);
67937e3a6d3SLuigi Rizzo         ret = nm_mem_assign_id_locked(nmd);
680c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_UNLOCK(nm_mem_list_lock);
68137e3a6d3SLuigi Rizzo 
68237e3a6d3SLuigi Rizzo 	return ret;
68337e3a6d3SLuigi Rizzo }
68437e3a6d3SLuigi Rizzo 
685c3e9b4dbSLuiz Otavio O Souza /* call with nm_mem_list_lock held */
686f0ea3689SLuigi Rizzo static void
687f0ea3689SLuigi Rizzo nm_mem_release_id(struct netmap_mem_d *nmd)
688f0ea3689SLuigi Rizzo {
689f0ea3689SLuigi Rizzo 	nmd->prev->next = nmd->next;
690f0ea3689SLuigi Rizzo 	nmd->next->prev = nmd->prev;
691f0ea3689SLuigi Rizzo 
692f0ea3689SLuigi Rizzo 	if (netmap_last_mem_d == nmd)
693f0ea3689SLuigi Rizzo 		netmap_last_mem_d = nmd->prev;
694f0ea3689SLuigi Rizzo 
695f0ea3689SLuigi Rizzo 	nmd->prev = nmd->next = NULL;
696c3e9b4dbSLuiz Otavio O Souza }
697f0ea3689SLuigi Rizzo 
698c3e9b4dbSLuiz Otavio O Souza struct netmap_mem_d *
699c3e9b4dbSLuiz Otavio O Souza netmap_mem_find(nm_memid_t id)
700c3e9b4dbSLuiz Otavio O Souza {
701c3e9b4dbSLuiz Otavio O Souza 	struct netmap_mem_d *nmd;
702c3e9b4dbSLuiz Otavio O Souza 
703c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_LOCK(nm_mem_list_lock);
704c3e9b4dbSLuiz Otavio O Souza 	nmd = netmap_last_mem_d;
705c3e9b4dbSLuiz Otavio O Souza 	do {
706c3e9b4dbSLuiz Otavio O Souza 		if (!(nmd->flags & NETMAP_MEM_HIDDEN) && nmd->nm_id == id) {
707c3e9b4dbSLuiz Otavio O Souza 			nmd->refcount++;
708c3e9b4dbSLuiz Otavio O Souza 			NM_DBG_REFC(nmd, __FUNCTION__, __LINE__);
709c3e9b4dbSLuiz Otavio O Souza 			NM_MTX_UNLOCK(nm_mem_list_lock);
710c3e9b4dbSLuiz Otavio O Souza 			return nmd;
711c3e9b4dbSLuiz Otavio O Souza 		}
712c3e9b4dbSLuiz Otavio O Souza 		nmd = nmd->next;
713c3e9b4dbSLuiz Otavio O Souza 	} while (nmd != netmap_last_mem_d);
714c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_UNLOCK(nm_mem_list_lock);
715c3e9b4dbSLuiz Otavio O Souza 	return NULL;
716f0ea3689SLuigi Rizzo }
717f0ea3689SLuigi Rizzo 
7184bf50f18SLuigi Rizzo static int
71937e3a6d3SLuigi Rizzo nm_mem_assign_group(struct netmap_mem_d *nmd, struct device *dev)
7204bf50f18SLuigi Rizzo {
7214bf50f18SLuigi Rizzo 	int err = 0, id;
7224bf50f18SLuigi Rizzo 	id = nm_iommu_group_id(dev);
7234bf50f18SLuigi Rizzo 	if (netmap_verbose)
7244bf50f18SLuigi Rizzo 		D("iommu_group %d", id);
7254bf50f18SLuigi Rizzo 
7264bf50f18SLuigi Rizzo 	NMA_LOCK(nmd);
7274bf50f18SLuigi Rizzo 
7284bf50f18SLuigi Rizzo 	if (nmd->nm_grp < 0)
7294bf50f18SLuigi Rizzo 		nmd->nm_grp = id;
7304bf50f18SLuigi Rizzo 
7314bf50f18SLuigi Rizzo 	if (nmd->nm_grp != id)
7324bf50f18SLuigi Rizzo 		nmd->lasterr = err = ENOMEM;
7334bf50f18SLuigi Rizzo 
7344bf50f18SLuigi Rizzo 	NMA_UNLOCK(nmd);
7354bf50f18SLuigi Rizzo 	return err;
7364bf50f18SLuigi Rizzo }
737f0ea3689SLuigi Rizzo 
7384f80b14cSVincenzo Maffione static struct lut_entry *
7394f80b14cSVincenzo Maffione nm_alloc_lut(u_int nobj)
7404f80b14cSVincenzo Maffione {
7414f80b14cSVincenzo Maffione 	size_t n = sizeof(struct lut_entry) * nobj;
7424f80b14cSVincenzo Maffione 	struct lut_entry *lut;
7434f80b14cSVincenzo Maffione #ifdef linux
7444f80b14cSVincenzo Maffione 	lut = vmalloc(n);
7454f80b14cSVincenzo Maffione #else
7464f80b14cSVincenzo Maffione 	lut = nm_os_malloc(n);
7474f80b14cSVincenzo Maffione #endif
7484f80b14cSVincenzo Maffione 	return lut;
7494f80b14cSVincenzo Maffione }
7504f80b14cSVincenzo Maffione 
7514f80b14cSVincenzo Maffione static void
7524f80b14cSVincenzo Maffione nm_free_lut(struct lut_entry *lut, u_int objtotal)
7534f80b14cSVincenzo Maffione {
7544f80b14cSVincenzo Maffione 	bzero(lut, sizeof(struct lut_entry) * objtotal);
7554f80b14cSVincenzo Maffione #ifdef linux
7564f80b14cSVincenzo Maffione 	vfree(lut);
7574f80b14cSVincenzo Maffione #else
7584f80b14cSVincenzo Maffione 	nm_os_free(lut);
7594f80b14cSVincenzo Maffione #endif
7604f80b14cSVincenzo Maffione }
7614f80b14cSVincenzo Maffione 
7624f80b14cSVincenzo Maffione #if defined(linux) || defined(_WIN32)
7634f80b14cSVincenzo Maffione static struct plut_entry *
7644f80b14cSVincenzo Maffione nm_alloc_plut(u_int nobj)
7654f80b14cSVincenzo Maffione {
7664f80b14cSVincenzo Maffione 	size_t n = sizeof(struct plut_entry) * nobj;
7674f80b14cSVincenzo Maffione 	struct plut_entry *lut;
7684f80b14cSVincenzo Maffione 	lut = vmalloc(n);
7694f80b14cSVincenzo Maffione 	return lut;
7704f80b14cSVincenzo Maffione }
7714f80b14cSVincenzo Maffione 
7724f80b14cSVincenzo Maffione static void
7734f80b14cSVincenzo Maffione nm_free_plut(struct plut_entry * lut)
7744f80b14cSVincenzo Maffione {
7754f80b14cSVincenzo Maffione 	vfree(lut);
7764f80b14cSVincenzo Maffione }
7774f80b14cSVincenzo Maffione #endif /* linux or _WIN32 */
7784f80b14cSVincenzo Maffione 
7794f80b14cSVincenzo Maffione 
780ccdc3305SLuigi Rizzo /*
7812579e2d7SLuigi Rizzo  * First, find the allocator that contains the requested offset,
7822579e2d7SLuigi Rizzo  * then locate the cluster through a lookup table.
783ccdc3305SLuigi Rizzo  */
784847bf383SLuigi Rizzo static vm_paddr_t
785847bf383SLuigi Rizzo netmap_mem2_ofstophys(struct netmap_mem_d* nmd, vm_ooffset_t offset)
786ccdc3305SLuigi Rizzo {
787ccdc3305SLuigi Rizzo 	int i;
788ce3ee1e7SLuigi Rizzo 	vm_ooffset_t o = offset;
789ce3ee1e7SLuigi Rizzo 	vm_paddr_t pa;
790ce3ee1e7SLuigi Rizzo 	struct netmap_obj_pool *p;
791ccdc3305SLuigi Rizzo 
792ce3ee1e7SLuigi Rizzo 	p = nmd->pools;
793ce3ee1e7SLuigi Rizzo 
794ce3ee1e7SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; offset -= p[i].memtotal, i++) {
795ce3ee1e7SLuigi Rizzo 		if (offset >= p[i].memtotal)
796ccdc3305SLuigi Rizzo 			continue;
7972579e2d7SLuigi Rizzo 		// now lookup the cluster's address
79837e3a6d3SLuigi Rizzo #ifndef _WIN32
7994bf50f18SLuigi Rizzo 		pa = vtophys(p[i].lut[offset / p[i]._objsize].vaddr) +
8008241616dSLuigi Rizzo 			offset % p[i]._objsize;
80137e3a6d3SLuigi Rizzo #else
80237e3a6d3SLuigi Rizzo 		pa = vtophys(p[i].lut[offset / p[i]._objsize].vaddr);
80337e3a6d3SLuigi Rizzo 		pa.QuadPart += offset % p[i]._objsize;
80437e3a6d3SLuigi Rizzo #endif
805ce3ee1e7SLuigi Rizzo 		return pa;
806ccdc3305SLuigi Rizzo 	}
8078241616dSLuigi Rizzo 	/* this is only in case of errors */
808b1123b01SLuigi Rizzo 	D("invalid ofs 0x%x out of 0x%x 0x%x 0x%x", (u_int)o,
809ce3ee1e7SLuigi Rizzo 		p[NETMAP_IF_POOL].memtotal,
810ce3ee1e7SLuigi Rizzo 		p[NETMAP_IF_POOL].memtotal
811ce3ee1e7SLuigi Rizzo 			+ p[NETMAP_RING_POOL].memtotal,
812ce3ee1e7SLuigi Rizzo 		p[NETMAP_IF_POOL].memtotal
813ce3ee1e7SLuigi Rizzo 			+ p[NETMAP_RING_POOL].memtotal
814ce3ee1e7SLuigi Rizzo 			+ p[NETMAP_BUF_POOL].memtotal);
81537e3a6d3SLuigi Rizzo #ifndef _WIN32
8164f80b14cSVincenzo Maffione 	return 0; /* bad address */
81737e3a6d3SLuigi Rizzo #else
81837e3a6d3SLuigi Rizzo 	vm_paddr_t res;
81937e3a6d3SLuigi Rizzo 	res.QuadPart = 0;
82037e3a6d3SLuigi Rizzo 	return res;
82137e3a6d3SLuigi Rizzo #endif
82237e3a6d3SLuigi Rizzo }
82337e3a6d3SLuigi Rizzo 
82437e3a6d3SLuigi Rizzo #ifdef _WIN32
82537e3a6d3SLuigi Rizzo 
82637e3a6d3SLuigi Rizzo /*
82737e3a6d3SLuigi Rizzo  * win32_build_virtual_memory_for_userspace
82837e3a6d3SLuigi Rizzo  *
82937e3a6d3SLuigi Rizzo  * This function get all the object making part of the pools and maps
83037e3a6d3SLuigi Rizzo  * a contiguous virtual memory space for the userspace
83137e3a6d3SLuigi Rizzo  * It works this way
83237e3a6d3SLuigi Rizzo  * 1 - allocate a Memory Descriptor List wide as the sum
83337e3a6d3SLuigi Rizzo  *		of the memory needed for the pools
83437e3a6d3SLuigi Rizzo  * 2 - cycle all the objects in every pool and for every object do
83537e3a6d3SLuigi Rizzo  *
83637e3a6d3SLuigi Rizzo  *		2a - cycle all the objects in every pool, get the list
83737e3a6d3SLuigi Rizzo  *				of the physical address descriptors
83837e3a6d3SLuigi Rizzo  *		2b - calculate the offset in the array of pages desciptor in the
83937e3a6d3SLuigi Rizzo  *				main MDL
84037e3a6d3SLuigi Rizzo  *		2c - copy the descriptors of the object in the main MDL
84137e3a6d3SLuigi Rizzo  *
84237e3a6d3SLuigi Rizzo  * 3 - return the resulting MDL that needs to be mapped in userland
84337e3a6d3SLuigi Rizzo  *
84437e3a6d3SLuigi Rizzo  * In this way we will have an MDL that describes all the memory for the
84537e3a6d3SLuigi Rizzo  * objects in a single object
84637e3a6d3SLuigi Rizzo */
84737e3a6d3SLuigi Rizzo 
84837e3a6d3SLuigi Rizzo PMDL
84937e3a6d3SLuigi Rizzo win32_build_user_vm_map(struct netmap_mem_d* nmd)
85037e3a6d3SLuigi Rizzo {
8514f80b14cSVincenzo Maffione 	u_int memflags, ofs = 0;
85237e3a6d3SLuigi Rizzo 	PMDL mainMdl, tempMdl;
853*2ff91c17SVincenzo Maffione 	uint64_t memsize;
854*2ff91c17SVincenzo Maffione 	int i, j;
85537e3a6d3SLuigi Rizzo 
85637e3a6d3SLuigi Rizzo 	if (netmap_mem_get_info(nmd, &memsize, &memflags, NULL)) {
85737e3a6d3SLuigi Rizzo 		D("memory not finalised yet");
85837e3a6d3SLuigi Rizzo 		return NULL;
85937e3a6d3SLuigi Rizzo 	}
86037e3a6d3SLuigi Rizzo 
86137e3a6d3SLuigi Rizzo 	mainMdl = IoAllocateMdl(NULL, memsize, FALSE, FALSE, NULL);
86237e3a6d3SLuigi Rizzo 	if (mainMdl == NULL) {
86337e3a6d3SLuigi Rizzo 		D("failed to allocate mdl");
86437e3a6d3SLuigi Rizzo 		return NULL;
86537e3a6d3SLuigi Rizzo 	}
86637e3a6d3SLuigi Rizzo 
86737e3a6d3SLuigi Rizzo 	NMA_LOCK(nmd);
86837e3a6d3SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
86937e3a6d3SLuigi Rizzo 		struct netmap_obj_pool *p = &nmd->pools[i];
87037e3a6d3SLuigi Rizzo 		int clsz = p->_clustsize;
87137e3a6d3SLuigi Rizzo 		int clobjs = p->_clustentries; /* objects per cluster */
87237e3a6d3SLuigi Rizzo 		int mdl_len = sizeof(PFN_NUMBER) * BYTES_TO_PAGES(clsz);
87337e3a6d3SLuigi Rizzo 		PPFN_NUMBER pSrc, pDst;
87437e3a6d3SLuigi Rizzo 
87537e3a6d3SLuigi Rizzo 		/* each pool has a different cluster size so we need to reallocate */
87637e3a6d3SLuigi Rizzo 		tempMdl = IoAllocateMdl(p->lut[0].vaddr, clsz, FALSE, FALSE, NULL);
87737e3a6d3SLuigi Rizzo 		if (tempMdl == NULL) {
87837e3a6d3SLuigi Rizzo 			NMA_UNLOCK(nmd);
87937e3a6d3SLuigi Rizzo 			D("fail to allocate tempMdl");
88037e3a6d3SLuigi Rizzo 			IoFreeMdl(mainMdl);
88137e3a6d3SLuigi Rizzo 			return NULL;
88237e3a6d3SLuigi Rizzo 		}
88337e3a6d3SLuigi Rizzo 		pSrc = MmGetMdlPfnArray(tempMdl);
88437e3a6d3SLuigi Rizzo 		/* create one entry per cluster, the lut[] has one entry per object */
88537e3a6d3SLuigi Rizzo 		for (j = 0; j < p->numclusters; j++, ofs += clsz) {
88637e3a6d3SLuigi Rizzo 			pDst = &MmGetMdlPfnArray(mainMdl)[BYTES_TO_PAGES(ofs)];
88737e3a6d3SLuigi Rizzo 			MmInitializeMdl(tempMdl, p->lut[j*clobjs].vaddr, clsz);
88837e3a6d3SLuigi Rizzo 			MmBuildMdlForNonPagedPool(tempMdl); /* compute physical page addresses */
88937e3a6d3SLuigi Rizzo 			RtlCopyMemory(pDst, pSrc, mdl_len); /* copy the page descriptors */
89037e3a6d3SLuigi Rizzo 			mainMdl->MdlFlags = tempMdl->MdlFlags; /* XXX what is in here ? */
89137e3a6d3SLuigi Rizzo 		}
89237e3a6d3SLuigi Rizzo 		IoFreeMdl(tempMdl);
89337e3a6d3SLuigi Rizzo 	}
89437e3a6d3SLuigi Rizzo 	NMA_UNLOCK(nmd);
89537e3a6d3SLuigi Rizzo 	return mainMdl;
89637e3a6d3SLuigi Rizzo }
89737e3a6d3SLuigi Rizzo 
89837e3a6d3SLuigi Rizzo #endif /* _WIN32 */
89937e3a6d3SLuigi Rizzo 
90037e3a6d3SLuigi Rizzo /*
90137e3a6d3SLuigi Rizzo  * helper function for OS-specific mmap routines (currently only windows).
90237e3a6d3SLuigi Rizzo  * Given an nmd and a pool index, returns the cluster size and number of clusters.
90337e3a6d3SLuigi Rizzo  * Returns 0 if memory is finalised and the pool is valid, otherwise 1.
90437e3a6d3SLuigi Rizzo  * It should be called under NMA_LOCK(nmd) otherwise the underlying info can change.
90537e3a6d3SLuigi Rizzo  */
90637e3a6d3SLuigi Rizzo 
90737e3a6d3SLuigi Rizzo int
90837e3a6d3SLuigi Rizzo netmap_mem2_get_pool_info(struct netmap_mem_d* nmd, u_int pool, u_int *clustsize, u_int *numclusters)
90937e3a6d3SLuigi Rizzo {
91037e3a6d3SLuigi Rizzo 	if (!nmd || !clustsize || !numclusters || pool >= NETMAP_POOLS_NR)
91137e3a6d3SLuigi Rizzo 		return 1; /* invalid arguments */
91237e3a6d3SLuigi Rizzo 	// NMA_LOCK_ASSERT(nmd);
91337e3a6d3SLuigi Rizzo 	if (!(nmd->flags & NETMAP_MEM_FINALIZED)) {
91437e3a6d3SLuigi Rizzo 		*clustsize = *numclusters = 0;
91537e3a6d3SLuigi Rizzo 		return 1; /* not ready yet */
91637e3a6d3SLuigi Rizzo 	}
91737e3a6d3SLuigi Rizzo 	*clustsize = nmd->pools[pool]._clustsize;
91837e3a6d3SLuigi Rizzo 	*numclusters = nmd->pools[pool].numclusters;
91937e3a6d3SLuigi Rizzo 	return 0; /* success */
920ccdc3305SLuigi Rizzo }
921ccdc3305SLuigi Rizzo 
922847bf383SLuigi Rizzo static int
923*2ff91c17SVincenzo Maffione netmap_mem2_get_info(struct netmap_mem_d* nmd, uint64_t* size,
924*2ff91c17SVincenzo Maffione 			u_int *memflags, nm_memid_t *id)
925ce3ee1e7SLuigi Rizzo {
926ce3ee1e7SLuigi Rizzo 	int error = 0;
927847bf383SLuigi Rizzo 	error = netmap_mem_config(nmd);
928ce3ee1e7SLuigi Rizzo 	if (error)
929ce3ee1e7SLuigi Rizzo 		goto out;
9304bf50f18SLuigi Rizzo 	if (size) {
931ce3ee1e7SLuigi Rizzo 		if (nmd->flags & NETMAP_MEM_FINALIZED) {
932ce3ee1e7SLuigi Rizzo 			*size = nmd->nm_totalsize;
933ce3ee1e7SLuigi Rizzo 		} else {
934ce3ee1e7SLuigi Rizzo 			int i;
935ce3ee1e7SLuigi Rizzo 			*size = 0;
936ce3ee1e7SLuigi Rizzo 			for (i = 0; i < NETMAP_POOLS_NR; i++) {
937ce3ee1e7SLuigi Rizzo 				struct netmap_obj_pool *p = nmd->pools + i;
938ce3ee1e7SLuigi Rizzo 				*size += (p->_numclusters * p->_clustsize);
939ce3ee1e7SLuigi Rizzo 			}
940ce3ee1e7SLuigi Rizzo 		}
9414bf50f18SLuigi Rizzo 	}
9424bf50f18SLuigi Rizzo 	if (memflags)
943ce3ee1e7SLuigi Rizzo 		*memflags = nmd->flags;
9444bf50f18SLuigi Rizzo 	if (id)
945f0ea3689SLuigi Rizzo 		*id = nmd->nm_id;
946ce3ee1e7SLuigi Rizzo out:
947ce3ee1e7SLuigi Rizzo 	return error;
948ce3ee1e7SLuigi Rizzo }
949ce3ee1e7SLuigi Rizzo 
950ccdc3305SLuigi Rizzo /*
951ccdc3305SLuigi Rizzo  * we store objects by kernel address, need to find the offset
952ccdc3305SLuigi Rizzo  * within the pool to export the value to userspace.
953ccdc3305SLuigi Rizzo  * Algorithm: scan until we find the cluster, then add the
954ccdc3305SLuigi Rizzo  * actual offset in the cluster
955ccdc3305SLuigi Rizzo  */
956ce2cb792SLuigi Rizzo static ssize_t
957ccdc3305SLuigi Rizzo netmap_obj_offset(struct netmap_obj_pool *p, const void *vaddr)
958ccdc3305SLuigi Rizzo {
959ce3ee1e7SLuigi Rizzo 	int i, k = p->_clustentries, n = p->objtotal;
960ccdc3305SLuigi Rizzo 	ssize_t ofs = 0;
961ccdc3305SLuigi Rizzo 
962ccdc3305SLuigi Rizzo 	for (i = 0; i < n; i += k, ofs += p->_clustsize) {
963ccdc3305SLuigi Rizzo 		const char *base = p->lut[i].vaddr;
964ccdc3305SLuigi Rizzo 		ssize_t relofs = (const char *) vaddr - base;
965ccdc3305SLuigi Rizzo 
966aa76317cSLuigi Rizzo 		if (relofs < 0 || relofs >= p->_clustsize)
967ccdc3305SLuigi Rizzo 			continue;
968ccdc3305SLuigi Rizzo 
969ccdc3305SLuigi Rizzo 		ofs = ofs + relofs;
970ccdc3305SLuigi Rizzo 		ND("%s: return offset %d (cluster %d) for pointer %p",
971ccdc3305SLuigi Rizzo 		    p->name, ofs, i, vaddr);
972ccdc3305SLuigi Rizzo 		return ofs;
973ccdc3305SLuigi Rizzo 	}
974ccdc3305SLuigi Rizzo 	D("address %p is not contained inside any cluster (%s)",
975ccdc3305SLuigi Rizzo 	    vaddr, p->name);
976ccdc3305SLuigi Rizzo 	return 0; /* An error occurred */
977ccdc3305SLuigi Rizzo }
978ccdc3305SLuigi Rizzo 
979ccdc3305SLuigi Rizzo /* Helper functions which convert virtual addresses to offsets */
980ce3ee1e7SLuigi Rizzo #define netmap_if_offset(n, v)					\
981ce3ee1e7SLuigi Rizzo 	netmap_obj_offset(&(n)->pools[NETMAP_IF_POOL], (v))
982ccdc3305SLuigi Rizzo 
983ce3ee1e7SLuigi Rizzo #define netmap_ring_offset(n, v)				\
984ce3ee1e7SLuigi Rizzo     ((n)->pools[NETMAP_IF_POOL].memtotal + 			\
985ce3ee1e7SLuigi Rizzo 	netmap_obj_offset(&(n)->pools[NETMAP_RING_POOL], (v)))
986ccdc3305SLuigi Rizzo 
987847bf383SLuigi Rizzo static ssize_t
988847bf383SLuigi Rizzo netmap_mem2_if_offset(struct netmap_mem_d *nmd, const void *addr)
989ce3ee1e7SLuigi Rizzo {
990*2ff91c17SVincenzo Maffione 	return netmap_if_offset(nmd, addr);
991ce3ee1e7SLuigi Rizzo }
992ce3ee1e7SLuigi Rizzo 
9938241616dSLuigi Rizzo /*
9948241616dSLuigi Rizzo  * report the index, and use start position as a hint,
9958241616dSLuigi Rizzo  * otherwise buffer allocation becomes terribly expensive.
9968241616dSLuigi Rizzo  */
997ccdc3305SLuigi Rizzo static void *
998ce3ee1e7SLuigi Rizzo netmap_obj_malloc(struct netmap_obj_pool *p, u_int len, uint32_t *start, uint32_t *index)
999ccdc3305SLuigi Rizzo {
1000ccdc3305SLuigi Rizzo 	uint32_t i = 0;			/* index in the bitmap */
100137e3a6d3SLuigi Rizzo 	uint32_t mask, j = 0;		/* slot counter */
1002ccdc3305SLuigi Rizzo 	void *vaddr = NULL;
1003ccdc3305SLuigi Rizzo 
1004ccdc3305SLuigi Rizzo 	if (len > p->_objsize) {
1005ccdc3305SLuigi Rizzo 		D("%s request size %d too large", p->name, len);
1006ccdc3305SLuigi Rizzo 		return NULL;
1007ccdc3305SLuigi Rizzo 	}
1008ccdc3305SLuigi Rizzo 
1009ccdc3305SLuigi Rizzo 	if (p->objfree == 0) {
1010f9790aebSLuigi Rizzo 		D("no more %s objects", p->name);
1011ccdc3305SLuigi Rizzo 		return NULL;
1012ccdc3305SLuigi Rizzo 	}
10138241616dSLuigi Rizzo 	if (start)
10148241616dSLuigi Rizzo 		i = *start;
1015ccdc3305SLuigi Rizzo 
10168241616dSLuigi Rizzo 	/* termination is guaranteed by p->free, but better check bounds on i */
10178241616dSLuigi Rizzo 	while (vaddr == NULL && i < p->bitmap_slots)  {
1018ccdc3305SLuigi Rizzo 		uint32_t cur = p->bitmap[i];
1019ccdc3305SLuigi Rizzo 		if (cur == 0) { /* bitmask is fully used */
1020ccdc3305SLuigi Rizzo 			i++;
1021ccdc3305SLuigi Rizzo 			continue;
1022ccdc3305SLuigi Rizzo 		}
1023ccdc3305SLuigi Rizzo 		/* locate a slot */
1024ccdc3305SLuigi Rizzo 		for (j = 0, mask = 1; (cur & mask) == 0; j++, mask <<= 1)
1025ccdc3305SLuigi Rizzo 			;
1026ccdc3305SLuigi Rizzo 
1027ccdc3305SLuigi Rizzo 		p->bitmap[i] &= ~mask; /* mark object as in use */
1028ccdc3305SLuigi Rizzo 		p->objfree--;
1029ccdc3305SLuigi Rizzo 
1030ccdc3305SLuigi Rizzo 		vaddr = p->lut[i * 32 + j].vaddr;
10318241616dSLuigi Rizzo 		if (index)
10328241616dSLuigi Rizzo 			*index = i * 32 + j;
1033ccdc3305SLuigi Rizzo 	}
103437e3a6d3SLuigi Rizzo 	ND("%s allocator: allocated object @ [%d][%d]: vaddr %p",p->name, i, j, vaddr);
1035ccdc3305SLuigi Rizzo 
10368241616dSLuigi Rizzo 	if (start)
10378241616dSLuigi Rizzo 		*start = i;
1038ccdc3305SLuigi Rizzo 	return vaddr;
1039ccdc3305SLuigi Rizzo }
1040ccdc3305SLuigi Rizzo 
1041ccdc3305SLuigi Rizzo 
1042ccdc3305SLuigi Rizzo /*
1043f0ea3689SLuigi Rizzo  * free by index, not by address.
1044f0ea3689SLuigi Rizzo  * XXX should we also cleanup the content ?
1045ccdc3305SLuigi Rizzo  */
1046f0ea3689SLuigi Rizzo static int
1047ccdc3305SLuigi Rizzo netmap_obj_free(struct netmap_obj_pool *p, uint32_t j)
1048ccdc3305SLuigi Rizzo {
1049f0ea3689SLuigi Rizzo 	uint32_t *ptr, mask;
1050f0ea3689SLuigi Rizzo 
1051ccdc3305SLuigi Rizzo 	if (j >= p->objtotal) {
1052ccdc3305SLuigi Rizzo 		D("invalid index %u, max %u", j, p->objtotal);
1053f0ea3689SLuigi Rizzo 		return 1;
1054ccdc3305SLuigi Rizzo 	}
1055f0ea3689SLuigi Rizzo 	ptr = &p->bitmap[j / 32];
1056f0ea3689SLuigi Rizzo 	mask = (1 << (j % 32));
1057f0ea3689SLuigi Rizzo 	if (*ptr & mask) {
1058f0ea3689SLuigi Rizzo 		D("ouch, double free on buffer %d", j);
1059f0ea3689SLuigi Rizzo 		return 1;
1060f0ea3689SLuigi Rizzo 	} else {
1061f0ea3689SLuigi Rizzo 		*ptr |= mask;
1062ccdc3305SLuigi Rizzo 		p->objfree++;
1063f0ea3689SLuigi Rizzo 		return 0;
1064f0ea3689SLuigi Rizzo 	}
1065ccdc3305SLuigi Rizzo }
1066ccdc3305SLuigi Rizzo 
1067f0ea3689SLuigi Rizzo /*
1068f0ea3689SLuigi Rizzo  * free by address. This is slow but is only used for a few
1069f0ea3689SLuigi Rizzo  * objects (rings, nifp)
1070f0ea3689SLuigi Rizzo  */
1071ccdc3305SLuigi Rizzo static void
1072ccdc3305SLuigi Rizzo netmap_obj_free_va(struct netmap_obj_pool *p, void *vaddr)
1073ccdc3305SLuigi Rizzo {
1074ce3ee1e7SLuigi Rizzo 	u_int i, j, n = p->numclusters;
1075ccdc3305SLuigi Rizzo 
1076ce3ee1e7SLuigi Rizzo 	for (i = 0, j = 0; i < n; i++, j += p->_clustentries) {
1077ce3ee1e7SLuigi Rizzo 		void *base = p->lut[i * p->_clustentries].vaddr;
1078ccdc3305SLuigi Rizzo 		ssize_t relofs = (ssize_t) vaddr - (ssize_t) base;
1079ccdc3305SLuigi Rizzo 
1080ccdc3305SLuigi Rizzo 		/* Given address, is out of the scope of the current cluster.*/
10814f80b14cSVincenzo Maffione 		if (base == NULL || vaddr < base || relofs >= p->_clustsize)
1082ccdc3305SLuigi Rizzo 			continue;
1083ccdc3305SLuigi Rizzo 
1084ccdc3305SLuigi Rizzo 		j = j + relofs / p->_objsize;
1085ce3ee1e7SLuigi Rizzo 		/* KASSERT(j != 0, ("Cannot free object 0")); */
1086ccdc3305SLuigi Rizzo 		netmap_obj_free(p, j);
1087ccdc3305SLuigi Rizzo 		return;
1088ccdc3305SLuigi Rizzo 	}
1089ae10d1afSLuigi Rizzo 	D("address %p is not contained inside any cluster (%s)",
1090ccdc3305SLuigi Rizzo 	    vaddr, p->name);
1091ccdc3305SLuigi Rizzo }
1092ccdc3305SLuigi Rizzo 
10934f80b14cSVincenzo Maffione unsigned
10944f80b14cSVincenzo Maffione netmap_mem_bufsize(struct netmap_mem_d *nmd)
10954f80b14cSVincenzo Maffione {
10964f80b14cSVincenzo Maffione 	return nmd->pools[NETMAP_BUF_POOL]._objsize;
10974f80b14cSVincenzo Maffione }
10984bf50f18SLuigi Rizzo 
1099ce3ee1e7SLuigi Rizzo #define netmap_if_malloc(n, len)	netmap_obj_malloc(&(n)->pools[NETMAP_IF_POOL], len, NULL, NULL)
1100ce3ee1e7SLuigi Rizzo #define netmap_if_free(n, v)		netmap_obj_free_va(&(n)->pools[NETMAP_IF_POOL], (v))
1101ce3ee1e7SLuigi Rizzo #define netmap_ring_malloc(n, len)	netmap_obj_malloc(&(n)->pools[NETMAP_RING_POOL], len, NULL, NULL)
1102ce3ee1e7SLuigi Rizzo #define netmap_ring_free(n, v)		netmap_obj_free_va(&(n)->pools[NETMAP_RING_POOL], (v))
1103ce3ee1e7SLuigi Rizzo #define netmap_buf_malloc(n, _pos, _index)			\
11044bf50f18SLuigi Rizzo 	netmap_obj_malloc(&(n)->pools[NETMAP_BUF_POOL], netmap_mem_bufsize(n), _pos, _index)
1105ccdc3305SLuigi Rizzo 
1106ccdc3305SLuigi Rizzo 
11074f80b14cSVincenzo Maffione #if 0 /* currently unused */
1108ccdc3305SLuigi Rizzo /* Return the index associated to the given packet buffer */
1109ce3ee1e7SLuigi Rizzo #define netmap_buf_index(n, v)						\
1110ce3ee1e7SLuigi Rizzo     (netmap_obj_offset(&(n)->pools[NETMAP_BUF_POOL], (v)) / NETMAP_BDG_BUF_SIZE(n))
1111f0ea3689SLuigi Rizzo #endif
1112f0ea3689SLuigi Rizzo 
1113f0ea3689SLuigi Rizzo /*
1114f0ea3689SLuigi Rizzo  * allocate extra buffers in a linked list.
1115f0ea3689SLuigi Rizzo  * returns the actual number.
1116f0ea3689SLuigi Rizzo  */
1117f0ea3689SLuigi Rizzo uint32_t
1118f0ea3689SLuigi Rizzo netmap_extra_alloc(struct netmap_adapter *na, uint32_t *head, uint32_t n)
1119f0ea3689SLuigi Rizzo {
1120f0ea3689SLuigi Rizzo 	struct netmap_mem_d *nmd = na->nm_mem;
1121f0ea3689SLuigi Rizzo 	uint32_t i, pos = 0; /* opaque, scan position in the bitmap */
1122f0ea3689SLuigi Rizzo 
1123f0ea3689SLuigi Rizzo 	NMA_LOCK(nmd);
1124f0ea3689SLuigi Rizzo 
1125f0ea3689SLuigi Rizzo 	*head = 0;	/* default, 'null' index ie empty list */
1126f0ea3689SLuigi Rizzo 	for (i = 0 ; i < n; i++) {
1127f0ea3689SLuigi Rizzo 		uint32_t cur = *head;	/* save current head */
1128f0ea3689SLuigi Rizzo 		uint32_t *p = netmap_buf_malloc(nmd, &pos, head);
1129f0ea3689SLuigi Rizzo 		if (p == NULL) {
1130f0ea3689SLuigi Rizzo 			D("no more buffers after %d of %d", i, n);
1131f0ea3689SLuigi Rizzo 			*head = cur; /* restore */
1132f0ea3689SLuigi Rizzo 			break;
1133f0ea3689SLuigi Rizzo 		}
113437e3a6d3SLuigi Rizzo 		ND(5, "allocate buffer %d -> %d", *head, cur);
1135f0ea3689SLuigi Rizzo 		*p = cur; /* link to previous head */
1136f0ea3689SLuigi Rizzo 	}
1137f0ea3689SLuigi Rizzo 
1138f0ea3689SLuigi Rizzo 	NMA_UNLOCK(nmd);
1139f0ea3689SLuigi Rizzo 
1140f0ea3689SLuigi Rizzo 	return i;
1141f0ea3689SLuigi Rizzo }
1142f0ea3689SLuigi Rizzo 
1143f0ea3689SLuigi Rizzo static void
1144f0ea3689SLuigi Rizzo netmap_extra_free(struct netmap_adapter *na, uint32_t head)
1145f0ea3689SLuigi Rizzo {
1146847bf383SLuigi Rizzo         struct lut_entry *lut = na->na_lut.lut;
1147f0ea3689SLuigi Rizzo 	struct netmap_mem_d *nmd = na->nm_mem;
1148f0ea3689SLuigi Rizzo 	struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
1149f0ea3689SLuigi Rizzo 	uint32_t i, cur, *buf;
1150f0ea3689SLuigi Rizzo 
115137e3a6d3SLuigi Rizzo 	ND("freeing the extra list");
1152f0ea3689SLuigi Rizzo 	for (i = 0; head >=2 && head < p->objtotal; i++) {
1153f0ea3689SLuigi Rizzo 		cur = head;
1154f0ea3689SLuigi Rizzo 		buf = lut[head].vaddr;
1155f0ea3689SLuigi Rizzo 		head = *buf;
1156f0ea3689SLuigi Rizzo 		*buf = 0;
1157f0ea3689SLuigi Rizzo 		if (netmap_obj_free(p, cur))
1158f0ea3689SLuigi Rizzo 			break;
1159f0ea3689SLuigi Rizzo 	}
1160f0ea3689SLuigi Rizzo 	if (head != 0)
1161f0ea3689SLuigi Rizzo 		D("breaking with head %d", head);
116237e3a6d3SLuigi Rizzo 	if (netmap_verbose)
1163f0ea3689SLuigi Rizzo 		D("freed %d buffers", i);
1164f0ea3689SLuigi Rizzo }
1165ccdc3305SLuigi Rizzo 
1166ccdc3305SLuigi Rizzo 
11678241616dSLuigi Rizzo /* Return nonzero on error */
11688241616dSLuigi Rizzo static int
1169f9790aebSLuigi Rizzo netmap_new_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n)
1170ccdc3305SLuigi Rizzo {
1171ce3ee1e7SLuigi Rizzo 	struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
1172ce3ee1e7SLuigi Rizzo 	u_int i = 0;	/* slot counter */
11738241616dSLuigi Rizzo 	uint32_t pos = 0;	/* slot in p->bitmap */
11748241616dSLuigi Rizzo 	uint32_t index = 0;	/* buffer index */
1175ccdc3305SLuigi Rizzo 
1176ccdc3305SLuigi Rizzo 	for (i = 0; i < n; i++) {
1177ce3ee1e7SLuigi Rizzo 		void *vaddr = netmap_buf_malloc(nmd, &pos, &index);
1178ccdc3305SLuigi Rizzo 		if (vaddr == NULL) {
1179f9790aebSLuigi Rizzo 			D("no more buffers after %d of %d", i, n);
1180ccdc3305SLuigi Rizzo 			goto cleanup;
1181ccdc3305SLuigi Rizzo 		}
11828241616dSLuigi Rizzo 		slot[i].buf_idx = index;
1183ccdc3305SLuigi Rizzo 		slot[i].len = p->_objsize;
1184f9790aebSLuigi Rizzo 		slot[i].flags = 0;
11854f80b14cSVincenzo Maffione 		slot[i].ptr = 0;
1186ccdc3305SLuigi Rizzo 	}
1187ccdc3305SLuigi Rizzo 
1188*2ff91c17SVincenzo Maffione 	ND("%s: allocated %d buffers, %d available, first at %d", p->name, n, p->objfree, pos);
11898241616dSLuigi Rizzo 	return (0);
1190ccdc3305SLuigi Rizzo 
1191ccdc3305SLuigi Rizzo cleanup:
11924cf8455fSEd Maste 	while (i > 0) {
11934cf8455fSEd Maste 		i--;
11948241616dSLuigi Rizzo 		netmap_obj_free(p, slot[i].buf_idx);
1195ccdc3305SLuigi Rizzo 	}
11968241616dSLuigi Rizzo 	bzero(slot, n * sizeof(slot[0]));
11978241616dSLuigi Rizzo 	return (ENOMEM);
1198ccdc3305SLuigi Rizzo }
1199ccdc3305SLuigi Rizzo 
1200f0ea3689SLuigi Rizzo static void
1201f0ea3689SLuigi Rizzo netmap_mem_set_ring(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n, uint32_t index)
1202f0ea3689SLuigi Rizzo {
1203f0ea3689SLuigi Rizzo 	struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
1204f0ea3689SLuigi Rizzo 	u_int i;
1205f0ea3689SLuigi Rizzo 
1206f0ea3689SLuigi Rizzo 	for (i = 0; i < n; i++) {
1207f0ea3689SLuigi Rizzo 		slot[i].buf_idx = index;
1208f0ea3689SLuigi Rizzo 		slot[i].len = p->_objsize;
1209f0ea3689SLuigi Rizzo 		slot[i].flags = 0;
1210f0ea3689SLuigi Rizzo 	}
1211f0ea3689SLuigi Rizzo }
1212f0ea3689SLuigi Rizzo 
1213ccdc3305SLuigi Rizzo 
1214ccdc3305SLuigi Rizzo static void
1215f9790aebSLuigi Rizzo netmap_free_buf(struct netmap_mem_d *nmd, uint32_t i)
1216ccdc3305SLuigi Rizzo {
1217ce3ee1e7SLuigi Rizzo 	struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
12188241616dSLuigi Rizzo 
1219ccdc3305SLuigi Rizzo 	if (i < 2 || i >= p->objtotal) {
1220ccdc3305SLuigi Rizzo 		D("Cannot free buf#%d: should be in [2, %d[", i, p->objtotal);
1221ccdc3305SLuigi Rizzo 		return;
1222ccdc3305SLuigi Rizzo 	}
12238241616dSLuigi Rizzo 	netmap_obj_free(p, i);
1224ccdc3305SLuigi Rizzo }
1225ccdc3305SLuigi Rizzo 
1226f0ea3689SLuigi Rizzo 
1227f0ea3689SLuigi Rizzo static void
1228f0ea3689SLuigi Rizzo netmap_free_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n)
1229f0ea3689SLuigi Rizzo {
1230f0ea3689SLuigi Rizzo 	u_int i;
1231f0ea3689SLuigi Rizzo 
1232f0ea3689SLuigi Rizzo 	for (i = 0; i < n; i++) {
1233*2ff91c17SVincenzo Maffione 		if (slot[i].buf_idx > 1)
1234f0ea3689SLuigi Rizzo 			netmap_free_buf(nmd, slot[i].buf_idx);
1235f0ea3689SLuigi Rizzo 	}
1236*2ff91c17SVincenzo Maffione 	ND("%s: released some buffers, available: %u",
1237*2ff91c17SVincenzo Maffione 			p->name, p->objfree);
1238f0ea3689SLuigi Rizzo }
1239f0ea3689SLuigi Rizzo 
12408241616dSLuigi Rizzo static void
12418241616dSLuigi Rizzo netmap_reset_obj_allocator(struct netmap_obj_pool *p)
12428241616dSLuigi Rizzo {
1243ce3ee1e7SLuigi Rizzo 
12448241616dSLuigi Rizzo 	if (p == NULL)
12458241616dSLuigi Rizzo 		return;
12468241616dSLuigi Rizzo 	if (p->bitmap)
1247c3e9b4dbSLuiz Otavio O Souza 		nm_os_free(p->bitmap);
12488241616dSLuigi Rizzo 	p->bitmap = NULL;
12494f80b14cSVincenzo Maffione 	if (p->invalid_bitmap)
12504f80b14cSVincenzo Maffione 		nm_os_free(p->invalid_bitmap);
12514f80b14cSVincenzo Maffione 	p->invalid_bitmap = NULL;
1252*2ff91c17SVincenzo Maffione 	if (!p->alloc_done) {
1253*2ff91c17SVincenzo Maffione 		/* allocation was done by somebody else.
1254*2ff91c17SVincenzo Maffione 		 * Let them clean up after themselves.
1255*2ff91c17SVincenzo Maffione 		 */
1256*2ff91c17SVincenzo Maffione 		return;
1257*2ff91c17SVincenzo Maffione 	}
12588241616dSLuigi Rizzo 	if (p->lut) {
1259ce3ee1e7SLuigi Rizzo 		u_int i;
1260ce3ee1e7SLuigi Rizzo 
1261dd4fcbc5SPatrick Kelsey 		/*
1262dd4fcbc5SPatrick Kelsey 		 * Free each cluster allocated in
1263dd4fcbc5SPatrick Kelsey 		 * netmap_finalize_obj_allocator().  The cluster start
1264dd4fcbc5SPatrick Kelsey 		 * addresses are stored at multiples of p->_clusterentries
1265dd4fcbc5SPatrick Kelsey 		 * in the lut.
1266dd4fcbc5SPatrick Kelsey 		 */
1267ce3ee1e7SLuigi Rizzo 		for (i = 0; i < p->objtotal; i += p->_clustentries) {
126837e3a6d3SLuigi Rizzo 			contigfree(p->lut[i].vaddr, p->_clustsize, M_NETMAP);
12698241616dSLuigi Rizzo 		}
12704f80b14cSVincenzo Maffione 		nm_free_lut(p->lut, p->objtotal);
12718241616dSLuigi Rizzo 	}
12728241616dSLuigi Rizzo 	p->lut = NULL;
1273ce3ee1e7SLuigi Rizzo 	p->objtotal = 0;
1274ce3ee1e7SLuigi Rizzo 	p->memtotal = 0;
1275ce3ee1e7SLuigi Rizzo 	p->numclusters = 0;
1276ce3ee1e7SLuigi Rizzo 	p->objfree = 0;
1277*2ff91c17SVincenzo Maffione 	p->alloc_done = 0;
12788241616dSLuigi Rizzo }
1279ccdc3305SLuigi Rizzo 
1280ccdc3305SLuigi Rizzo /*
1281ccdc3305SLuigi Rizzo  * Free all resources related to an allocator.
1282ccdc3305SLuigi Rizzo  */
1283ccdc3305SLuigi Rizzo static void
1284ccdc3305SLuigi Rizzo netmap_destroy_obj_allocator(struct netmap_obj_pool *p)
1285ccdc3305SLuigi Rizzo {
1286ccdc3305SLuigi Rizzo 	if (p == NULL)
1287ccdc3305SLuigi Rizzo 		return;
12888241616dSLuigi Rizzo 	netmap_reset_obj_allocator(p);
1289ccdc3305SLuigi Rizzo }
1290ccdc3305SLuigi Rizzo 
1291ccdc3305SLuigi Rizzo /*
1292ccdc3305SLuigi Rizzo  * We receive a request for objtotal objects, of size objsize each.
1293ccdc3305SLuigi Rizzo  * Internally we may round up both numbers, as we allocate objects
1294ccdc3305SLuigi Rizzo  * in small clusters multiple of the page size.
1295ce3ee1e7SLuigi Rizzo  * We need to keep track of objtotal and clustentries,
1296ccdc3305SLuigi Rizzo  * as they are needed when freeing memory.
1297ccdc3305SLuigi Rizzo  *
1298ccdc3305SLuigi Rizzo  * XXX note -- userspace needs the buffers to be contiguous,
1299ccdc3305SLuigi Rizzo  *	so we cannot afford gaps at the end of a cluster.
1300ccdc3305SLuigi Rizzo  */
13018241616dSLuigi Rizzo 
13028241616dSLuigi Rizzo 
13038241616dSLuigi Rizzo /* call with NMA_LOCK held */
13048241616dSLuigi Rizzo static int
13058241616dSLuigi Rizzo netmap_config_obj_allocator(struct netmap_obj_pool *p, u_int objtotal, u_int objsize)
1306ccdc3305SLuigi Rizzo {
1307ce3ee1e7SLuigi Rizzo 	int i;
1308ccdc3305SLuigi Rizzo 	u_int clustsize;	/* the cluster size, multiple of page size */
1309ccdc3305SLuigi Rizzo 	u_int clustentries;	/* how many objects per entry */
1310ccdc3305SLuigi Rizzo 
1311ce3ee1e7SLuigi Rizzo 	/* we store the current request, so we can
1312ce3ee1e7SLuigi Rizzo 	 * detect configuration changes later */
1313ce3ee1e7SLuigi Rizzo 	p->r_objtotal = objtotal;
1314ce3ee1e7SLuigi Rizzo 	p->r_objsize = objsize;
1315ce3ee1e7SLuigi Rizzo 
13164bf50f18SLuigi Rizzo #define MAX_CLUSTSIZE	(1<<22)		// 4 MB
131717885a7bSLuigi Rizzo #define LINE_ROUND	NM_CACHE_ALIGN	// 64
1318ccdc3305SLuigi Rizzo 	if (objsize >= MAX_CLUSTSIZE) {
1319ccdc3305SLuigi Rizzo 		/* we could do it but there is no point */
1320ccdc3305SLuigi Rizzo 		D("unsupported allocation for %d bytes", objsize);
1321ce3ee1e7SLuigi Rizzo 		return EINVAL;
1322ccdc3305SLuigi Rizzo 	}
1323ccdc3305SLuigi Rizzo 	/* make sure objsize is a multiple of LINE_ROUND */
1324ccdc3305SLuigi Rizzo 	i = (objsize & (LINE_ROUND - 1));
1325ccdc3305SLuigi Rizzo 	if (i) {
1326ccdc3305SLuigi Rizzo 		D("XXX aligning object by %d bytes", LINE_ROUND - i);
1327ccdc3305SLuigi Rizzo 		objsize += LINE_ROUND - i;
1328ccdc3305SLuigi Rizzo 	}
13298241616dSLuigi Rizzo 	if (objsize < p->objminsize || objsize > p->objmaxsize) {
13308241616dSLuigi Rizzo 		D("requested objsize %d out of range [%d, %d]",
13318241616dSLuigi Rizzo 			objsize, p->objminsize, p->objmaxsize);
1332ce3ee1e7SLuigi Rizzo 		return EINVAL;
13338241616dSLuigi Rizzo 	}
13348241616dSLuigi Rizzo 	if (objtotal < p->nummin || objtotal > p->nummax) {
13358241616dSLuigi Rizzo 		D("requested objtotal %d out of range [%d, %d]",
13368241616dSLuigi Rizzo 			objtotal, p->nummin, p->nummax);
1337ce3ee1e7SLuigi Rizzo 		return EINVAL;
13388241616dSLuigi Rizzo 	}
1339ccdc3305SLuigi Rizzo 	/*
1340ccdc3305SLuigi Rizzo 	 * Compute number of objects using a brute-force approach:
1341ccdc3305SLuigi Rizzo 	 * given a max cluster size,
1342ccdc3305SLuigi Rizzo 	 * we try to fill it with objects keeping track of the
1343ccdc3305SLuigi Rizzo 	 * wasted space to the next page boundary.
1344ccdc3305SLuigi Rizzo 	 */
1345ccdc3305SLuigi Rizzo 	for (clustentries = 0, i = 1;; i++) {
1346ccdc3305SLuigi Rizzo 		u_int delta, used = i * objsize;
1347ccdc3305SLuigi Rizzo 		if (used > MAX_CLUSTSIZE)
1348ccdc3305SLuigi Rizzo 			break;
1349ccdc3305SLuigi Rizzo 		delta = used % PAGE_SIZE;
1350ccdc3305SLuigi Rizzo 		if (delta == 0) { // exact solution
1351ccdc3305SLuigi Rizzo 			clustentries = i;
1352ccdc3305SLuigi Rizzo 			break;
1353ccdc3305SLuigi Rizzo 		}
1354ccdc3305SLuigi Rizzo 	}
13554bf50f18SLuigi Rizzo 	/* exact solution not found */
13564bf50f18SLuigi Rizzo 	if (clustentries == 0) {
13574bf50f18SLuigi Rizzo 		D("unsupported allocation for %d bytes", objsize);
13584bf50f18SLuigi Rizzo 		return EINVAL;
13594bf50f18SLuigi Rizzo 	}
13604bf50f18SLuigi Rizzo 	/* compute clustsize */
1361ccdc3305SLuigi Rizzo 	clustsize = clustentries * objsize;
1362ae10d1afSLuigi Rizzo 	if (netmap_verbose)
1363ccdc3305SLuigi Rizzo 		D("objsize %d clustsize %d objects %d",
1364ccdc3305SLuigi Rizzo 			objsize, clustsize, clustentries);
1365ccdc3305SLuigi Rizzo 
1366ccdc3305SLuigi Rizzo 	/*
1367ccdc3305SLuigi Rizzo 	 * The number of clusters is n = ceil(objtotal/clustentries)
1368ccdc3305SLuigi Rizzo 	 * objtotal' = n * clustentries
1369ccdc3305SLuigi Rizzo 	 */
1370ce3ee1e7SLuigi Rizzo 	p->_clustentries = clustentries;
1371ccdc3305SLuigi Rizzo 	p->_clustsize = clustsize;
1372ce3ee1e7SLuigi Rizzo 	p->_numclusters = (objtotal + clustentries - 1) / clustentries;
1373ce3ee1e7SLuigi Rizzo 
1374ce3ee1e7SLuigi Rizzo 	/* actual values (may be larger than requested) */
13758241616dSLuigi Rizzo 	p->_objsize = objsize;
1376ce3ee1e7SLuigi Rizzo 	p->_objtotal = p->_numclusters * clustentries;
1377ccdc3305SLuigi Rizzo 
13788241616dSLuigi Rizzo 	return 0;
13798241616dSLuigi Rizzo }
13808241616dSLuigi Rizzo 
13818241616dSLuigi Rizzo /* call with NMA_LOCK held */
13828241616dSLuigi Rizzo static int
13838241616dSLuigi Rizzo netmap_finalize_obj_allocator(struct netmap_obj_pool *p)
13848241616dSLuigi Rizzo {
1385ce3ee1e7SLuigi Rizzo 	int i; /* must be signed */
1386ce3ee1e7SLuigi Rizzo 	size_t n;
1387ce3ee1e7SLuigi Rizzo 
13884f80b14cSVincenzo Maffione 	if (p->lut) {
1389*2ff91c17SVincenzo Maffione 		/* if the lut is already there we assume that also all the
1390*2ff91c17SVincenzo Maffione 		 * clusters have already been allocated, possibily by somebody
1391*2ff91c17SVincenzo Maffione 		 * else (e.g., extmem). In the latter case, the alloc_done flag
1392*2ff91c17SVincenzo Maffione 		 * will remain at zero, so that we will not attempt to
1393*2ff91c17SVincenzo Maffione 		 * deallocate the clusters by ourselves in
1394*2ff91c17SVincenzo Maffione 		 * netmap_reset_obj_allocator.
1395*2ff91c17SVincenzo Maffione 		 */
13964f80b14cSVincenzo Maffione 		return 0;
13974f80b14cSVincenzo Maffione 	}
13984f80b14cSVincenzo Maffione 
1399ce3ee1e7SLuigi Rizzo 	/* optimistically assume we have enough memory */
1400ce3ee1e7SLuigi Rizzo 	p->numclusters = p->_numclusters;
1401ce3ee1e7SLuigi Rizzo 	p->objtotal = p->_objtotal;
1402*2ff91c17SVincenzo Maffione 	p->alloc_done = 1;
14038241616dSLuigi Rizzo 
140437e3a6d3SLuigi Rizzo 	p->lut = nm_alloc_lut(p->objtotal);
1405ccdc3305SLuigi Rizzo 	if (p->lut == NULL) {
140637e3a6d3SLuigi Rizzo 		D("Unable to create lookup table for '%s'", p->name);
1407ccdc3305SLuigi Rizzo 		goto clean;
1408ccdc3305SLuigi Rizzo 	}
1409ccdc3305SLuigi Rizzo 
1410ccdc3305SLuigi Rizzo 	/*
14114f80b14cSVincenzo Maffione 	 * Allocate clusters, init pointers
1412ccdc3305SLuigi Rizzo 	 */
1413ce3ee1e7SLuigi Rizzo 
1414ce3ee1e7SLuigi Rizzo 	n = p->_clustsize;
1415ce3ee1e7SLuigi Rizzo 	for (i = 0; i < (int)p->objtotal;) {
1416ce3ee1e7SLuigi Rizzo 		int lim = i + p->_clustentries;
1417ccdc3305SLuigi Rizzo 		char *clust;
1418ccdc3305SLuigi Rizzo 
141937e3a6d3SLuigi Rizzo 		/*
142037e3a6d3SLuigi Rizzo 		 * XXX Note, we only need contigmalloc() for buffers attached
142137e3a6d3SLuigi Rizzo 		 * to native interfaces. In all other cases (nifp, netmap rings
142237e3a6d3SLuigi Rizzo 		 * and even buffers for VALE ports or emulated interfaces) we
142337e3a6d3SLuigi Rizzo 		 * can live with standard malloc, because the hardware will not
142437e3a6d3SLuigi Rizzo 		 * access the pages directly.
142537e3a6d3SLuigi Rizzo 		 */
1426ce3ee1e7SLuigi Rizzo 		clust = contigmalloc(n, M_NETMAP, M_NOWAIT | M_ZERO,
1427ce3ee1e7SLuigi Rizzo 		    (size_t)0, -1UL, PAGE_SIZE, 0);
1428ccdc3305SLuigi Rizzo 		if (clust == NULL) {
1429ccdc3305SLuigi Rizzo 			/*
1430ccdc3305SLuigi Rizzo 			 * If we get here, there is a severe memory shortage,
1431ccdc3305SLuigi Rizzo 			 * so halve the allocated memory to reclaim some.
1432ccdc3305SLuigi Rizzo 			 */
1433ccdc3305SLuigi Rizzo 			D("Unable to create cluster at %d for '%s' allocator",
14348241616dSLuigi Rizzo 			    i, p->name);
1435ce3ee1e7SLuigi Rizzo 			if (i < 2) /* nothing to halve */
1436ce3ee1e7SLuigi Rizzo 				goto out;
1437ccdc3305SLuigi Rizzo 			lim = i / 2;
14388241616dSLuigi Rizzo 			for (i--; i >= lim; i--) {
1439ce3ee1e7SLuigi Rizzo 				if (i % p->_clustentries == 0 && p->lut[i].vaddr)
1440ccdc3305SLuigi Rizzo 					contigfree(p->lut[i].vaddr,
1441ce3ee1e7SLuigi Rizzo 						n, M_NETMAP);
1442dd4fcbc5SPatrick Kelsey 				p->lut[i].vaddr = NULL;
1443ccdc3305SLuigi Rizzo 			}
1444ce3ee1e7SLuigi Rizzo 		out:
1445ccdc3305SLuigi Rizzo 			p->objtotal = i;
1446ce3ee1e7SLuigi Rizzo 			/* we may have stopped in the middle of a cluster */
1447ce3ee1e7SLuigi Rizzo 			p->numclusters = (i + p->_clustentries - 1) / p->_clustentries;
1448ccdc3305SLuigi Rizzo 			break;
1449ccdc3305SLuigi Rizzo 		}
1450dd4fcbc5SPatrick Kelsey 		/*
14514f80b14cSVincenzo Maffione 		 * Set lut state for all buffers in the current cluster.
1452dd4fcbc5SPatrick Kelsey 		 *
1453dd4fcbc5SPatrick Kelsey 		 * [i, lim) is the set of buffer indexes that cover the
1454dd4fcbc5SPatrick Kelsey 		 * current cluster.
1455dd4fcbc5SPatrick Kelsey 		 *
1456dd4fcbc5SPatrick Kelsey 		 * 'clust' is really the address of the current buffer in
1457dd4fcbc5SPatrick Kelsey 		 * the current cluster as we index through it with a stride
1458dd4fcbc5SPatrick Kelsey 		 * of p->_objsize.
1459dd4fcbc5SPatrick Kelsey 		 */
14608241616dSLuigi Rizzo 		for (; i < lim; i++, clust += p->_objsize) {
1461ccdc3305SLuigi Rizzo 			p->lut[i].vaddr = clust;
14624f80b14cSVincenzo Maffione #if !defined(linux) && !defined(_WIN32)
1463ccdc3305SLuigi Rizzo 			p->lut[i].paddr = vtophys(clust);
14644f80b14cSVincenzo Maffione #endif
1465ccdc3305SLuigi Rizzo 		}
1466ccdc3305SLuigi Rizzo 	}
1467ce3ee1e7SLuigi Rizzo 	p->memtotal = p->numclusters * p->_clustsize;
1468ae10d1afSLuigi Rizzo 	if (netmap_verbose)
1469ccdc3305SLuigi Rizzo 		D("Pre-allocated %d clusters (%d/%dKB) for '%s'",
1470ce3ee1e7SLuigi Rizzo 		    p->numclusters, p->_clustsize >> 10,
1471ce3ee1e7SLuigi Rizzo 		    p->memtotal >> 10, p->name);
1472ccdc3305SLuigi Rizzo 
14738241616dSLuigi Rizzo 	return 0;
1474ccdc3305SLuigi Rizzo 
1475ccdc3305SLuigi Rizzo clean:
14768241616dSLuigi Rizzo 	netmap_reset_obj_allocator(p);
14778241616dSLuigi Rizzo 	return ENOMEM;
14788241616dSLuigi Rizzo }
14798241616dSLuigi Rizzo 
14808241616dSLuigi Rizzo /* call with lock held */
14818241616dSLuigi Rizzo static int
1482c3e9b4dbSLuiz Otavio O Souza netmap_mem_params_changed(struct netmap_obj_params* p)
14838241616dSLuigi Rizzo {
1484c3e9b4dbSLuiz Otavio O Souza 	int i, rv = 0;
14858241616dSLuigi Rizzo 
14868241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1487c3e9b4dbSLuiz Otavio O Souza 		if (p[i].last_size != p[i].size || p[i].last_num != p[i].num) {
1488c3e9b4dbSLuiz Otavio O Souza 			p[i].last_size = p[i].size;
1489c3e9b4dbSLuiz Otavio O Souza 			p[i].last_num = p[i].num;
1490c3e9b4dbSLuiz Otavio O Souza 			rv = 1;
14918241616dSLuigi Rizzo 		}
1492c3e9b4dbSLuiz Otavio O Souza 	}
1493c3e9b4dbSLuiz Otavio O Souza 	return rv;
14948241616dSLuigi Rizzo }
14958241616dSLuigi Rizzo 
1496ce3ee1e7SLuigi Rizzo static void
1497ce3ee1e7SLuigi Rizzo netmap_mem_reset_all(struct netmap_mem_d *nmd)
1498ce3ee1e7SLuigi Rizzo {
1499ce3ee1e7SLuigi Rizzo 	int i;
1500f0ea3689SLuigi Rizzo 
1501f0ea3689SLuigi Rizzo 	if (netmap_verbose)
1502ce3ee1e7SLuigi Rizzo 		D("resetting %p", nmd);
1503ce3ee1e7SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1504ce3ee1e7SLuigi Rizzo 		netmap_reset_obj_allocator(&nmd->pools[i]);
1505ce3ee1e7SLuigi Rizzo 	}
1506ce3ee1e7SLuigi Rizzo 	nmd->flags  &= ~NETMAP_MEM_FINALIZED;
1507ce3ee1e7SLuigi Rizzo }
1508ce3ee1e7SLuigi Rizzo 
1509ce3ee1e7SLuigi Rizzo static int
15104bf50f18SLuigi Rizzo netmap_mem_unmap(struct netmap_obj_pool *p, struct netmap_adapter *na)
15114bf50f18SLuigi Rizzo {
1512*2ff91c17SVincenzo Maffione 	int i, lim = p->objtotal;
15134f80b14cSVincenzo Maffione 	struct netmap_lut *lut = &na->na_lut;
15144bf50f18SLuigi Rizzo 
1515c3e9b4dbSLuiz Otavio O Souza 	if (na == NULL || na->pdev == NULL)
15164bf50f18SLuigi Rizzo 		return 0;
15174bf50f18SLuigi Rizzo 
151837e3a6d3SLuigi Rizzo #if defined(__FreeBSD__)
15194bf50f18SLuigi Rizzo 	(void)i;
15204bf50f18SLuigi Rizzo 	(void)lim;
15214f80b14cSVincenzo Maffione 	(void)lut;
15224bf50f18SLuigi Rizzo 	D("unsupported on FreeBSD");
152337e3a6d3SLuigi Rizzo #elif defined(_WIN32)
152437e3a6d3SLuigi Rizzo 	(void)i;
152537e3a6d3SLuigi Rizzo 	(void)lim;
15264f80b14cSVincenzo Maffione 	(void)lut;
15274f80b14cSVincenzo Maffione 	D("unsupported on Windows");
15284bf50f18SLuigi Rizzo #else /* linux */
15294f80b14cSVincenzo Maffione 	ND("unmapping and freeing plut for %s", na->name);
15304f80b14cSVincenzo Maffione 	if (lut->plut == NULL)
15314f80b14cSVincenzo Maffione 		return 0;
15324f80b14cSVincenzo Maffione 	for (i = 0; i < lim; i += p->_clustentries) {
15334f80b14cSVincenzo Maffione 		if (lut->plut[i].paddr)
15344f80b14cSVincenzo Maffione 			netmap_unload_map(na, (bus_dma_tag_t) na->pdev, &lut->plut[i].paddr, p->_clustsize);
15354bf50f18SLuigi Rizzo 	}
15364f80b14cSVincenzo Maffione 	nm_free_plut(lut->plut);
15374f80b14cSVincenzo Maffione 	lut->plut = NULL;
15384bf50f18SLuigi Rizzo #endif /* linux */
15394bf50f18SLuigi Rizzo 
15404bf50f18SLuigi Rizzo 	return 0;
15414bf50f18SLuigi Rizzo }
15424bf50f18SLuigi Rizzo 
15434bf50f18SLuigi Rizzo static int
15444bf50f18SLuigi Rizzo netmap_mem_map(struct netmap_obj_pool *p, struct netmap_adapter *na)
15454bf50f18SLuigi Rizzo {
15464f80b14cSVincenzo Maffione 	int error = 0;
15474f80b14cSVincenzo Maffione 	int i, lim = p->objtotal;
15484f80b14cSVincenzo Maffione 	struct netmap_lut *lut = &na->na_lut;
15494bf50f18SLuigi Rizzo 
15504bf50f18SLuigi Rizzo 	if (na->pdev == NULL)
15514bf50f18SLuigi Rizzo 		return 0;
15524bf50f18SLuigi Rizzo 
15534f80b14cSVincenzo Maffione #if defined(__FreeBSD__)
15544f80b14cSVincenzo Maffione 	(void)i;
15554f80b14cSVincenzo Maffione 	(void)lim;
15564f80b14cSVincenzo Maffione 	(void)lut;
15574f80b14cSVincenzo Maffione 	D("unsupported on FreeBSD");
15584f80b14cSVincenzo Maffione #elif defined(_WIN32)
15594f80b14cSVincenzo Maffione 	(void)i;
15604f80b14cSVincenzo Maffione 	(void)lim;
15614f80b14cSVincenzo Maffione 	(void)lut;
15624f80b14cSVincenzo Maffione 	D("unsupported on Windows");
15634f80b14cSVincenzo Maffione #else /* linux */
15644f80b14cSVincenzo Maffione 
15654f80b14cSVincenzo Maffione 	if (lut->plut != NULL) {
15664f80b14cSVincenzo Maffione 		ND("plut already allocated for %s", na->name);
15674f80b14cSVincenzo Maffione 		return 0;
15684bf50f18SLuigi Rizzo 	}
15694f80b14cSVincenzo Maffione 
15704f80b14cSVincenzo Maffione 	ND("allocating physical lut for %s", na->name);
15714f80b14cSVincenzo Maffione 	lut->plut = nm_alloc_plut(lim);
15724f80b14cSVincenzo Maffione 	if (lut->plut == NULL) {
15734f80b14cSVincenzo Maffione 		D("Failed to allocate physical lut for %s", na->name);
15744f80b14cSVincenzo Maffione 		return ENOMEM;
15754f80b14cSVincenzo Maffione         }
15764f80b14cSVincenzo Maffione 
15774f80b14cSVincenzo Maffione 	for (i = 0; i < lim; i += p->_clustentries) {
15784f80b14cSVincenzo Maffione 		lut->plut[i].paddr = 0;
15794f80b14cSVincenzo Maffione 	}
15804f80b14cSVincenzo Maffione 
15814f80b14cSVincenzo Maffione 	for (i = 0; i < lim; i += p->_clustentries) {
15824f80b14cSVincenzo Maffione 		int j;
15834f80b14cSVincenzo Maffione 
15844f80b14cSVincenzo Maffione 		if (p->lut[i].vaddr == NULL)
15854f80b14cSVincenzo Maffione 			continue;
15864f80b14cSVincenzo Maffione 
15874f80b14cSVincenzo Maffione 		error = netmap_load_map(na, (bus_dma_tag_t) na->pdev, &lut->plut[i].paddr,
15884f80b14cSVincenzo Maffione 				p->lut[i].vaddr, p->_clustsize);
15894f80b14cSVincenzo Maffione 		if (error) {
15904f80b14cSVincenzo Maffione 			D("Failed to map cluster #%d from the %s pool", i, p->name);
15914f80b14cSVincenzo Maffione 			break;
15924f80b14cSVincenzo Maffione 		}
15934f80b14cSVincenzo Maffione 
15944f80b14cSVincenzo Maffione 		for (j = 1; j < p->_clustentries; j++) {
15954f80b14cSVincenzo Maffione 			lut->plut[i + j].paddr = lut->plut[i + j - 1].paddr + p->_objsize;
15964f80b14cSVincenzo Maffione 		}
15974f80b14cSVincenzo Maffione 	}
15984f80b14cSVincenzo Maffione 
15994f80b14cSVincenzo Maffione 	if (error)
16004f80b14cSVincenzo Maffione 		netmap_mem_unmap(p, na);
16014f80b14cSVincenzo Maffione 
16024bf50f18SLuigi Rizzo #endif /* linux */
16034bf50f18SLuigi Rizzo 
16044f80b14cSVincenzo Maffione 	return error;
16054bf50f18SLuigi Rizzo }
16064bf50f18SLuigi Rizzo 
16074bf50f18SLuigi Rizzo static int
1608ce3ee1e7SLuigi Rizzo netmap_mem_finalize_all(struct netmap_mem_d *nmd)
1609ce3ee1e7SLuigi Rizzo {
1610ce3ee1e7SLuigi Rizzo 	int i;
1611ce3ee1e7SLuigi Rizzo 	if (nmd->flags & NETMAP_MEM_FINALIZED)
1612ce3ee1e7SLuigi Rizzo 		return 0;
1613ce3ee1e7SLuigi Rizzo 	nmd->lasterr = 0;
1614ce3ee1e7SLuigi Rizzo 	nmd->nm_totalsize = 0;
1615ce3ee1e7SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1616ce3ee1e7SLuigi Rizzo 		nmd->lasterr = netmap_finalize_obj_allocator(&nmd->pools[i]);
1617ce3ee1e7SLuigi Rizzo 		if (nmd->lasterr)
1618ce3ee1e7SLuigi Rizzo 			goto error;
1619ce3ee1e7SLuigi Rizzo 		nmd->nm_totalsize += nmd->pools[i].memtotal;
1620ce3ee1e7SLuigi Rizzo 	}
16214f80b14cSVincenzo Maffione 	nmd->lasterr = netmap_mem_init_bitmaps(nmd);
16224f80b14cSVincenzo Maffione 	if (nmd->lasterr)
16234f80b14cSVincenzo Maffione 		goto error;
16244f80b14cSVincenzo Maffione 
1625ce3ee1e7SLuigi Rizzo 	nmd->flags |= NETMAP_MEM_FINALIZED;
1626ce3ee1e7SLuigi Rizzo 
1627f0ea3689SLuigi Rizzo 	if (netmap_verbose)
1628f0ea3689SLuigi Rizzo 		D("interfaces %d KB, rings %d KB, buffers %d MB",
1629ce3ee1e7SLuigi Rizzo 		    nmd->pools[NETMAP_IF_POOL].memtotal >> 10,
1630ce3ee1e7SLuigi Rizzo 		    nmd->pools[NETMAP_RING_POOL].memtotal >> 10,
1631ce3ee1e7SLuigi Rizzo 		    nmd->pools[NETMAP_BUF_POOL].memtotal >> 20);
1632ce3ee1e7SLuigi Rizzo 
1633f0ea3689SLuigi Rizzo 	if (netmap_verbose)
1634ce3ee1e7SLuigi Rizzo 		D("Free buffers: %d", nmd->pools[NETMAP_BUF_POOL].objfree);
1635ce3ee1e7SLuigi Rizzo 
1636ce3ee1e7SLuigi Rizzo 
1637ce3ee1e7SLuigi Rizzo 	return 0;
1638ce3ee1e7SLuigi Rizzo error:
1639ce3ee1e7SLuigi Rizzo 	netmap_mem_reset_all(nmd);
1640ce3ee1e7SLuigi Rizzo 	return nmd->lasterr;
1641ce3ee1e7SLuigi Rizzo }
1642ce3ee1e7SLuigi Rizzo 
1643f0ea3689SLuigi Rizzo /*
1644f0ea3689SLuigi Rizzo  * allocator for private memory
1645f0ea3689SLuigi Rizzo  */
16464f80b14cSVincenzo Maffione static void *
16474f80b14cSVincenzo Maffione _netmap_mem_private_new(size_t size, struct netmap_obj_params *p,
16484f80b14cSVincenzo Maffione 		struct netmap_mem_ops *ops, int *perr)
1649ce3ee1e7SLuigi Rizzo {
1650ce3ee1e7SLuigi Rizzo 	struct netmap_mem_d *d = NULL;
1651c3e9b4dbSLuiz Otavio O Souza 	int i, err = 0;
1652ce3ee1e7SLuigi Rizzo 
16534f80b14cSVincenzo Maffione 	d = nm_os_malloc(size);
1654f0ea3689SLuigi Rizzo 	if (d == NULL) {
1655f0ea3689SLuigi Rizzo 		err = ENOMEM;
1656f0ea3689SLuigi Rizzo 		goto error;
1657f0ea3689SLuigi Rizzo 	}
1658ce3ee1e7SLuigi Rizzo 
1659ce3ee1e7SLuigi Rizzo 	*d = nm_blueprint;
16604f80b14cSVincenzo Maffione 	d->ops = ops;
1661ce3ee1e7SLuigi Rizzo 
1662f0ea3689SLuigi Rizzo 	err = nm_mem_assign_id(d);
1663f0ea3689SLuigi Rizzo 	if (err)
16644f80b14cSVincenzo Maffione 		goto error_free;
1665c3e9b4dbSLuiz Otavio O Souza 	snprintf(d->name, NM_MEM_NAMESZ, "%d", d->nm_id);
1666f0ea3689SLuigi Rizzo 
1667c3e9b4dbSLuiz Otavio O Souza 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1668c3e9b4dbSLuiz Otavio O Souza 		snprintf(d->pools[i].name, NETMAP_POOL_MAX_NAMSZ,
1669c3e9b4dbSLuiz Otavio O Souza 				nm_blueprint.pools[i].name,
1670c3e9b4dbSLuiz Otavio O Souza 				d->name);
1671c3e9b4dbSLuiz Otavio O Souza 		d->params[i].num = p[i].num;
1672c3e9b4dbSLuiz Otavio O Souza 		d->params[i].size = p[i].size;
1673c3e9b4dbSLuiz Otavio O Souza 	}
1674c3e9b4dbSLuiz Otavio O Souza 
1675c3e9b4dbSLuiz Otavio O Souza 	NMA_LOCK_INIT(d);
1676c3e9b4dbSLuiz Otavio O Souza 
1677c3e9b4dbSLuiz Otavio O Souza 	err = netmap_mem_config(d);
1678c3e9b4dbSLuiz Otavio O Souza 	if (err)
16794f80b14cSVincenzo Maffione 		goto error_rel_id;
1680c3e9b4dbSLuiz Otavio O Souza 
1681c3e9b4dbSLuiz Otavio O Souza 	d->flags &= ~NETMAP_MEM_FINALIZED;
1682c3e9b4dbSLuiz Otavio O Souza 
1683c3e9b4dbSLuiz Otavio O Souza 	return d;
1684c3e9b4dbSLuiz Otavio O Souza 
16854f80b14cSVincenzo Maffione error_rel_id:
16864f80b14cSVincenzo Maffione 	NMA_LOCK_DESTROY(d);
16874f80b14cSVincenzo Maffione 	nm_mem_release_id(d);
16884f80b14cSVincenzo Maffione error_free:
16894f80b14cSVincenzo Maffione 	nm_os_free(d);
1690c3e9b4dbSLuiz Otavio O Souza error:
1691c3e9b4dbSLuiz Otavio O Souza 	if (perr)
1692c3e9b4dbSLuiz Otavio O Souza 		*perr = err;
1693c3e9b4dbSLuiz Otavio O Souza 	return NULL;
1694c3e9b4dbSLuiz Otavio O Souza }
1695c3e9b4dbSLuiz Otavio O Souza 
1696c3e9b4dbSLuiz Otavio O Souza struct netmap_mem_d *
1697c3e9b4dbSLuiz Otavio O Souza netmap_mem_private_new(u_int txr, u_int txd, u_int rxr, u_int rxd,
1698c3e9b4dbSLuiz Otavio O Souza 		u_int extra_bufs, u_int npipes, int *perr)
1699c3e9b4dbSLuiz Otavio O Souza {
1700c3e9b4dbSLuiz Otavio O Souza 	struct netmap_mem_d *d = NULL;
1701c3e9b4dbSLuiz Otavio O Souza 	struct netmap_obj_params p[NETMAP_POOLS_NR];
17024f80b14cSVincenzo Maffione 	int i;
1703c3e9b4dbSLuiz Otavio O Souza 	u_int v, maxd;
1704f0ea3689SLuigi Rizzo 	/* account for the fake host rings */
1705ce3ee1e7SLuigi Rizzo 	txr++;
1706ce3ee1e7SLuigi Rizzo 	rxr++;
1707ce3ee1e7SLuigi Rizzo 
1708f0ea3689SLuigi Rizzo 	/* copy the min values */
1709f0ea3689SLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1710f0ea3689SLuigi Rizzo 		p[i] = netmap_min_priv_params[i];
1711f0ea3689SLuigi Rizzo 	}
1712f0ea3689SLuigi Rizzo 
1713f0ea3689SLuigi Rizzo 	/* possibly increase them to fit user request */
1714f0ea3689SLuigi Rizzo 	v = sizeof(struct netmap_if) + sizeof(ssize_t) * (txr + rxr);
1715f0ea3689SLuigi Rizzo 	if (p[NETMAP_IF_POOL].size < v)
1716f0ea3689SLuigi Rizzo 		p[NETMAP_IF_POOL].size = v;
1717f0ea3689SLuigi Rizzo 	v = 2 + 4 * npipes;
1718f0ea3689SLuigi Rizzo 	if (p[NETMAP_IF_POOL].num < v)
1719f0ea3689SLuigi Rizzo 		p[NETMAP_IF_POOL].num = v;
1720f0ea3689SLuigi Rizzo 	maxd = (txd > rxd) ? txd : rxd;
1721f0ea3689SLuigi Rizzo 	v = sizeof(struct netmap_ring) + sizeof(struct netmap_slot) * maxd;
1722f0ea3689SLuigi Rizzo 	if (p[NETMAP_RING_POOL].size < v)
1723f0ea3689SLuigi Rizzo 		p[NETMAP_RING_POOL].size = v;
1724f0ea3689SLuigi Rizzo 	/* each pipe endpoint needs two tx rings (1 normal + 1 host, fake)
1725f0ea3689SLuigi Rizzo          * and two rx rings (again, 1 normal and 1 fake host)
1726f0ea3689SLuigi Rizzo          */
1727f0ea3689SLuigi Rizzo 	v = txr + rxr + 8 * npipes;
1728f0ea3689SLuigi Rizzo 	if (p[NETMAP_RING_POOL].num < v)
1729f0ea3689SLuigi Rizzo 		p[NETMAP_RING_POOL].num = v;
1730f0ea3689SLuigi Rizzo 	/* for each pipe we only need the buffers for the 4 "real" rings.
1731f0ea3689SLuigi Rizzo          * On the other end, the pipe ring dimension may be different from
1732f0ea3689SLuigi Rizzo          * the parent port ring dimension. As a compromise, we allocate twice the
1733f0ea3689SLuigi Rizzo          * space actually needed if the pipe rings were the same size as the parent rings
1734f0ea3689SLuigi Rizzo          */
1735f0ea3689SLuigi Rizzo 	v = (4 * npipes + rxr) * rxd + (4 * npipes + txr) * txd + 2 + extra_bufs;
1736f0ea3689SLuigi Rizzo 		/* the +2 is for the tx and rx fake buffers (indices 0 and 1) */
1737f0ea3689SLuigi Rizzo 	if (p[NETMAP_BUF_POOL].num < v)
1738f0ea3689SLuigi Rizzo 		p[NETMAP_BUF_POOL].num = v;
1739f0ea3689SLuigi Rizzo 
1740f0ea3689SLuigi Rizzo 	if (netmap_verbose)
1741ce3ee1e7SLuigi Rizzo 		D("req if %d*%d ring %d*%d buf %d*%d",
1742ce3ee1e7SLuigi Rizzo 			p[NETMAP_IF_POOL].num,
1743ce3ee1e7SLuigi Rizzo 			p[NETMAP_IF_POOL].size,
1744ce3ee1e7SLuigi Rizzo 			p[NETMAP_RING_POOL].num,
1745ce3ee1e7SLuigi Rizzo 			p[NETMAP_RING_POOL].size,
1746ce3ee1e7SLuigi Rizzo 			p[NETMAP_BUF_POOL].num,
1747ce3ee1e7SLuigi Rizzo 			p[NETMAP_BUF_POOL].size);
1748ce3ee1e7SLuigi Rizzo 
17494f80b14cSVincenzo Maffione 	d = _netmap_mem_private_new(sizeof(*d), p, &netmap_mem_global_ops, perr);
1750ce3ee1e7SLuigi Rizzo 
1751ce3ee1e7SLuigi Rizzo 	return d;
1752ce3ee1e7SLuigi Rizzo }
1753ce3ee1e7SLuigi Rizzo 
17548241616dSLuigi Rizzo 
17558241616dSLuigi Rizzo /* call with lock held */
17568241616dSLuigi Rizzo static int
1757c3e9b4dbSLuiz Otavio O Souza netmap_mem2_config(struct netmap_mem_d *nmd)
17588241616dSLuigi Rizzo {
17598241616dSLuigi Rizzo 	int i;
17608241616dSLuigi Rizzo 
1761c3e9b4dbSLuiz Otavio O Souza 	if (!netmap_mem_params_changed(nmd->params))
17628241616dSLuigi Rizzo 		goto out;
17638241616dSLuigi Rizzo 
1764847bf383SLuigi Rizzo 	ND("reconfiguring");
17658241616dSLuigi Rizzo 
1766ce3ee1e7SLuigi Rizzo 	if (nmd->flags & NETMAP_MEM_FINALIZED) {
17678241616dSLuigi Rizzo 		/* reset previous allocation */
17688241616dSLuigi Rizzo 		for (i = 0; i < NETMAP_POOLS_NR; i++) {
1769ce3ee1e7SLuigi Rizzo 			netmap_reset_obj_allocator(&nmd->pools[i]);
17708241616dSLuigi Rizzo 		}
1771ce3ee1e7SLuigi Rizzo 		nmd->flags &= ~NETMAP_MEM_FINALIZED;
17728241616dSLuigi Rizzo 	}
17738241616dSLuigi Rizzo 
17748241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1775ce3ee1e7SLuigi Rizzo 		nmd->lasterr = netmap_config_obj_allocator(&nmd->pools[i],
1776c3e9b4dbSLuiz Otavio O Souza 				nmd->params[i].num, nmd->params[i].size);
1777ce3ee1e7SLuigi Rizzo 		if (nmd->lasterr)
17788241616dSLuigi Rizzo 			goto out;
17798241616dSLuigi Rizzo 	}
17808241616dSLuigi Rizzo 
17818241616dSLuigi Rizzo out:
17828241616dSLuigi Rizzo 
1783ce3ee1e7SLuigi Rizzo 	return nmd->lasterr;
17848241616dSLuigi Rizzo }
17858241616dSLuigi Rizzo 
17868241616dSLuigi Rizzo static int
1787c3e9b4dbSLuiz Otavio O Souza netmap_mem2_finalize(struct netmap_mem_d *nmd)
17888241616dSLuigi Rizzo {
1789*2ff91c17SVincenzo Maffione 	if (nmd->flags & NETMAP_MEM_FINALIZED)
17908241616dSLuigi Rizzo 		goto out;
17918241616dSLuigi Rizzo 
1792ce3ee1e7SLuigi Rizzo 	if (netmap_mem_finalize_all(nmd))
1793ce3ee1e7SLuigi Rizzo 		goto out;
17948241616dSLuigi Rizzo 
1795ce3ee1e7SLuigi Rizzo 	nmd->lasterr = 0;
17968241616dSLuigi Rizzo 
17978241616dSLuigi Rizzo out:
1798*2ff91c17SVincenzo Maffione 	return nmd->lasterr;
1799ccdc3305SLuigi Rizzo }
1800ccdc3305SLuigi Rizzo 
1801847bf383SLuigi Rizzo static void
1802c3e9b4dbSLuiz Otavio O Souza netmap_mem2_delete(struct netmap_mem_d *nmd)
1803ccdc3305SLuigi Rizzo {
18048241616dSLuigi Rizzo 	int i;
18058241616dSLuigi Rizzo 
18068241616dSLuigi Rizzo 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1807c3e9b4dbSLuiz Otavio O Souza 	    netmap_destroy_obj_allocator(&nmd->pools[i]);
18088241616dSLuigi Rizzo 	}
1809847bf383SLuigi Rizzo 
1810c3e9b4dbSLuiz Otavio O Souza 	NMA_LOCK_DESTROY(nmd);
1811c3e9b4dbSLuiz Otavio O Souza 	if (nmd != &nm_mem)
1812c3e9b4dbSLuiz Otavio O Souza 		nm_os_free(nmd);
18138241616dSLuigi Rizzo }
18148241616dSLuigi Rizzo 
18154f80b14cSVincenzo Maffione #ifdef WITH_EXTMEM
18164f80b14cSVincenzo Maffione /* doubly linekd list of all existing external allocators */
18174f80b14cSVincenzo Maffione static struct netmap_mem_ext *netmap_mem_ext_list = NULL;
18184f80b14cSVincenzo Maffione NM_MTX_T nm_mem_ext_list_lock;
18194f80b14cSVincenzo Maffione #endif /* WITH_EXTMEM */
18204f80b14cSVincenzo Maffione 
1821847bf383SLuigi Rizzo int
1822847bf383SLuigi Rizzo netmap_mem_init(void)
1823847bf383SLuigi Rizzo {
1824c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_INIT(nm_mem_list_lock);
1825847bf383SLuigi Rizzo 	NMA_LOCK_INIT(&nm_mem);
1826847bf383SLuigi Rizzo 	netmap_mem_get(&nm_mem);
18274f80b14cSVincenzo Maffione #ifdef WITH_EXTMEM
18284f80b14cSVincenzo Maffione 	NM_MTX_INIT(nm_mem_ext_list_lock);
18294f80b14cSVincenzo Maffione #endif /* WITH_EXTMEM */
1830847bf383SLuigi Rizzo 	return (0);
1831847bf383SLuigi Rizzo }
1832847bf383SLuigi Rizzo 
1833847bf383SLuigi Rizzo void
1834847bf383SLuigi Rizzo netmap_mem_fini(void)
1835847bf383SLuigi Rizzo {
1836847bf383SLuigi Rizzo 	netmap_mem_put(&nm_mem);
1837847bf383SLuigi Rizzo }
1838847bf383SLuigi Rizzo 
18398241616dSLuigi Rizzo static void
18408241616dSLuigi Rizzo netmap_free_rings(struct netmap_adapter *na)
18418241616dSLuigi Rizzo {
1842847bf383SLuigi Rizzo 	enum txrx t;
1843847bf383SLuigi Rizzo 
1844847bf383SLuigi Rizzo 	for_rx_tx(t) {
1845847bf383SLuigi Rizzo 		u_int i;
184637e3a6d3SLuigi Rizzo 		for (i = 0; i < nma_get_nrings(na, t) + 1; i++) {
1847*2ff91c17SVincenzo Maffione 			struct netmap_kring *kring = NMR(na, t)[i];
1848847bf383SLuigi Rizzo 			struct netmap_ring *ring = kring->ring;
1849847bf383SLuigi Rizzo 
185037e3a6d3SLuigi Rizzo 			if (ring == NULL || kring->users > 0 || (kring->nr_kflags & NKR_NEEDRING)) {
18514f80b14cSVincenzo Maffione 				if (netmap_verbose)
18524f80b14cSVincenzo Maffione 					D("NOT deleting ring %s (ring %p, users %d neekring %d)",
18534f80b14cSVincenzo Maffione 						kring->name, ring, kring->users, kring->nr_kflags & NKR_NEEDRING);
1854f0ea3689SLuigi Rizzo 				continue;
185537e3a6d3SLuigi Rizzo 			}
18564f80b14cSVincenzo Maffione 			if (netmap_verbose)
18574f80b14cSVincenzo Maffione 				D("deleting ring %s", kring->name);
1858*2ff91c17SVincenzo Maffione 			if (!(kring->nr_kflags & NKR_FAKERING)) {
1859*2ff91c17SVincenzo Maffione 				ND("freeing bufs for %s", kring->name);
1860f0ea3689SLuigi Rizzo 				netmap_free_bufs(na->nm_mem, ring->slot, kring->nkr_num_slots);
1861*2ff91c17SVincenzo Maffione 			} else {
1862*2ff91c17SVincenzo Maffione 				ND("NOT freeing bufs for %s", kring->name);
1863*2ff91c17SVincenzo Maffione 			}
1864f0ea3689SLuigi Rizzo 			netmap_ring_free(na->nm_mem, ring);
1865f0ea3689SLuigi Rizzo 			kring->ring = NULL;
18668241616dSLuigi Rizzo 		}
1867ce3ee1e7SLuigi Rizzo 	}
1868ccdc3305SLuigi Rizzo }
1869ccdc3305SLuigi Rizzo 
1870f9790aebSLuigi Rizzo /* call with NMA_LOCK held *
1871f9790aebSLuigi Rizzo  *
1872f9790aebSLuigi Rizzo  * Allocate netmap rings and buffers for this card
1873f9790aebSLuigi Rizzo  * The rings are contiguous, but have variable size.
1874f0ea3689SLuigi Rizzo  * The kring array must follow the layout described
1875f0ea3689SLuigi Rizzo  * in netmap_krings_create().
1876f9790aebSLuigi Rizzo  */
1877847bf383SLuigi Rizzo static int
1878847bf383SLuigi Rizzo netmap_mem2_rings_create(struct netmap_adapter *na)
1879f9790aebSLuigi Rizzo {
1880847bf383SLuigi Rizzo 	enum txrx t;
1881f9790aebSLuigi Rizzo 
1882847bf383SLuigi Rizzo 	for_rx_tx(t) {
1883847bf383SLuigi Rizzo 		u_int i;
1884847bf383SLuigi Rizzo 
1885847bf383SLuigi Rizzo 		for (i = 0; i <= nma_get_nrings(na, t); i++) {
1886*2ff91c17SVincenzo Maffione 			struct netmap_kring *kring = NMR(na, t)[i];
1887847bf383SLuigi Rizzo 			struct netmap_ring *ring = kring->ring;
1888847bf383SLuigi Rizzo 			u_int len, ndesc;
1889847bf383SLuigi Rizzo 
189037e3a6d3SLuigi Rizzo 			if (ring || (!kring->users && !(kring->nr_kflags & NKR_NEEDRING))) {
189137e3a6d3SLuigi Rizzo 				/* uneeded, or already created by somebody else */
18924f80b14cSVincenzo Maffione 				if (netmap_verbose)
18934f80b14cSVincenzo Maffione 					D("NOT creating ring %s (ring %p, users %d neekring %d)",
18944f80b14cSVincenzo Maffione 						kring->name, ring, kring->users, kring->nr_kflags & NKR_NEEDRING);
189537e3a6d3SLuigi Rizzo 				continue;
1896f0ea3689SLuigi Rizzo 			}
18974f80b14cSVincenzo Maffione 			if (netmap_verbose)
18984f80b14cSVincenzo Maffione 				D("creating %s", kring->name);
1899f9790aebSLuigi Rizzo 			ndesc = kring->nkr_num_slots;
1900f9790aebSLuigi Rizzo 			len = sizeof(struct netmap_ring) +
1901f9790aebSLuigi Rizzo 				  ndesc * sizeof(struct netmap_slot);
1902f9790aebSLuigi Rizzo 			ring = netmap_ring_malloc(na->nm_mem, len);
1903f9790aebSLuigi Rizzo 			if (ring == NULL) {
1904847bf383SLuigi Rizzo 				D("Cannot allocate %s_ring", nm_txrx2str(t));
1905f9790aebSLuigi Rizzo 				goto cleanup;
1906f9790aebSLuigi Rizzo 			}
1907f6c2a31fSLuigi Rizzo 			ND("txring at %p", ring);
1908f9790aebSLuigi Rizzo 			kring->ring = ring;
1909f9790aebSLuigi Rizzo 			*(uint32_t *)(uintptr_t)&ring->num_slots = ndesc;
191017885a7bSLuigi Rizzo 			*(int64_t *)(uintptr_t)&ring->buf_ofs =
1911f9790aebSLuigi Rizzo 			    (na->nm_mem->pools[NETMAP_IF_POOL].memtotal +
1912f9790aebSLuigi Rizzo 				na->nm_mem->pools[NETMAP_RING_POOL].memtotal) -
1913f9790aebSLuigi Rizzo 				netmap_ring_offset(na->nm_mem, ring);
1914f9790aebSLuigi Rizzo 
191517885a7bSLuigi Rizzo 			/* copy values from kring */
191617885a7bSLuigi Rizzo 			ring->head = kring->rhead;
191717885a7bSLuigi Rizzo 			ring->cur = kring->rcur;
191817885a7bSLuigi Rizzo 			ring->tail = kring->rtail;
19194f80b14cSVincenzo Maffione 			*(uint32_t *)(uintptr_t)&ring->nr_buf_size =
19204bf50f18SLuigi Rizzo 				netmap_mem_bufsize(na->nm_mem);
1921f0ea3689SLuigi Rizzo 			ND("%s h %d c %d t %d", kring->name,
1922f0ea3689SLuigi Rizzo 				ring->head, ring->cur, ring->tail);
1923847bf383SLuigi Rizzo 			ND("initializing slots for %s_ring", nm_txrx2str(txrx));
1924*2ff91c17SVincenzo Maffione 			if (!(kring->nr_kflags & NKR_FAKERING)) {
1925f0ea3689SLuigi Rizzo 				/* this is a real ring */
1926*2ff91c17SVincenzo Maffione 				ND("allocating buffers for %s", kring->name);
1927f9790aebSLuigi Rizzo 				if (netmap_new_bufs(na->nm_mem, ring->slot, ndesc)) {
1928847bf383SLuigi Rizzo 					D("Cannot allocate buffers for %s_ring", nm_txrx2str(t));
1929f9790aebSLuigi Rizzo 					goto cleanup;
1930f9790aebSLuigi Rizzo 				}
1931f0ea3689SLuigi Rizzo 			} else {
1932847bf383SLuigi Rizzo 				/* this is a fake ring, set all indices to 0 */
1933*2ff91c17SVincenzo Maffione 				ND("NOT allocating buffers for %s", kring->name);
1934f0ea3689SLuigi Rizzo 				netmap_mem_set_ring(na->nm_mem, ring->slot, ndesc, 0);
1935f0ea3689SLuigi Rizzo 			}
1936847bf383SLuigi Rizzo 		        /* ring info */
1937847bf383SLuigi Rizzo 		        *(uint16_t *)(uintptr_t)&ring->ringid = kring->ring_id;
1938847bf383SLuigi Rizzo 		        *(uint16_t *)(uintptr_t)&ring->dir = kring->tx;
1939f0ea3689SLuigi Rizzo 		}
1940f9790aebSLuigi Rizzo 	}
1941f9790aebSLuigi Rizzo 
1942f9790aebSLuigi Rizzo 	return 0;
1943f9790aebSLuigi Rizzo 
1944f9790aebSLuigi Rizzo cleanup:
1945f9790aebSLuigi Rizzo 	netmap_free_rings(na);
1946f9790aebSLuigi Rizzo 
1947f9790aebSLuigi Rizzo 	return ENOMEM;
1948f9790aebSLuigi Rizzo }
1949f9790aebSLuigi Rizzo 
1950847bf383SLuigi Rizzo static void
1951847bf383SLuigi Rizzo netmap_mem2_rings_delete(struct netmap_adapter *na)
1952f9790aebSLuigi Rizzo {
1953f9790aebSLuigi Rizzo 	/* last instance, release bufs and rings */
1954f9790aebSLuigi Rizzo 	netmap_free_rings(na);
1955f9790aebSLuigi Rizzo }
1956ccdc3305SLuigi Rizzo 
1957ccdc3305SLuigi Rizzo 
19588241616dSLuigi Rizzo /* call with NMA_LOCK held */
1959ae10d1afSLuigi Rizzo /*
1960ae10d1afSLuigi Rizzo  * Allocate the per-fd structure netmap_if.
1961ce3ee1e7SLuigi Rizzo  *
1962ce3ee1e7SLuigi Rizzo  * We assume that the configuration stored in na
1963ce3ee1e7SLuigi Rizzo  * (number of tx/rx rings and descs) does not change while
1964ce3ee1e7SLuigi Rizzo  * the interface is in netmap mode.
1965ae10d1afSLuigi Rizzo  */
1966847bf383SLuigi Rizzo static struct netmap_if *
1967c3e9b4dbSLuiz Otavio O Souza netmap_mem2_if_new(struct netmap_adapter *na, struct netmap_priv_d *priv)
1968ccdc3305SLuigi Rizzo {
1969ccdc3305SLuigi Rizzo 	struct netmap_if *nifp;
1970ccdc3305SLuigi Rizzo 	ssize_t base; /* handy for relative offsets between rings and nifp */
1971847bf383SLuigi Rizzo 	u_int i, len, n[NR_TXRX], ntot;
1972847bf383SLuigi Rizzo 	enum txrx t;
1973ccdc3305SLuigi Rizzo 
1974847bf383SLuigi Rizzo 	ntot = 0;
1975847bf383SLuigi Rizzo 	for_rx_tx(t) {
1976f0ea3689SLuigi Rizzo 		/* account for the (eventually fake) host rings */
1977847bf383SLuigi Rizzo 		n[t] = nma_get_nrings(na, t) + 1;
1978847bf383SLuigi Rizzo 		ntot += n[t];
1979847bf383SLuigi Rizzo 	}
1980ccdc3305SLuigi Rizzo 	/*
1981ccdc3305SLuigi Rizzo 	 * the descriptor is followed inline by an array of offsets
1982ccdc3305SLuigi Rizzo 	 * to the tx and rx rings in the shared memory region.
1983ccdc3305SLuigi Rizzo 	 */
1984ce3ee1e7SLuigi Rizzo 
1985847bf383SLuigi Rizzo 	len = sizeof(struct netmap_if) + (ntot * sizeof(ssize_t));
1986ce3ee1e7SLuigi Rizzo 	nifp = netmap_if_malloc(na->nm_mem, len);
1987ccdc3305SLuigi Rizzo 	if (nifp == NULL) {
1988ce3ee1e7SLuigi Rizzo 		NMA_UNLOCK(na->nm_mem);
1989ccdc3305SLuigi Rizzo 		return NULL;
1990ccdc3305SLuigi Rizzo 	}
1991ccdc3305SLuigi Rizzo 
1992ccdc3305SLuigi Rizzo 	/* initialize base fields -- override const */
1993ce3ee1e7SLuigi Rizzo 	*(u_int *)(uintptr_t)&nifp->ni_tx_rings = na->num_tx_rings;
1994ce3ee1e7SLuigi Rizzo 	*(u_int *)(uintptr_t)&nifp->ni_rx_rings = na->num_rx_rings;
19954bf50f18SLuigi Rizzo 	strncpy(nifp->ni_name, na->name, (size_t)IFNAMSIZ);
1996ccdc3305SLuigi Rizzo 
1997ccdc3305SLuigi Rizzo 	/*
1998ccdc3305SLuigi Rizzo 	 * fill the slots for the rx and tx rings. They contain the offset
1999ccdc3305SLuigi Rizzo 	 * between the ring and nifp, so the information is usable in
2000ccdc3305SLuigi Rizzo 	 * userspace to reach the ring from the nifp.
2001ccdc3305SLuigi Rizzo 	 */
2002ce3ee1e7SLuigi Rizzo 	base = netmap_if_offset(na->nm_mem, nifp);
2003847bf383SLuigi Rizzo 	for (i = 0; i < n[NR_TX]; i++) {
2004c3e9b4dbSLuiz Otavio O Souza 		/* XXX instead of ofs == 0 maybe use the offset of an error
2005c3e9b4dbSLuiz Otavio O Souza 		 * ring, like we do for buffers? */
2006c3e9b4dbSLuiz Otavio O Souza 		ssize_t ofs = 0;
2007c3e9b4dbSLuiz Otavio O Souza 
2008*2ff91c17SVincenzo Maffione 		if (na->tx_rings[i]->ring != NULL && i >= priv->np_qfirst[NR_TX]
2009c3e9b4dbSLuiz Otavio O Souza 				&& i < priv->np_qlast[NR_TX]) {
2010c3e9b4dbSLuiz Otavio O Souza 			ofs = netmap_ring_offset(na->nm_mem,
2011*2ff91c17SVincenzo Maffione 						 na->tx_rings[i]->ring) - base;
201237e3a6d3SLuigi Rizzo 		}
2013c3e9b4dbSLuiz Otavio O Souza 		*(ssize_t *)(uintptr_t)&nifp->ring_ofs[i] = ofs;
2014ccdc3305SLuigi Rizzo 	}
2015847bf383SLuigi Rizzo 	for (i = 0; i < n[NR_RX]; i++) {
2016c3e9b4dbSLuiz Otavio O Souza 		/* XXX instead of ofs == 0 maybe use the offset of an error
2017c3e9b4dbSLuiz Otavio O Souza 		 * ring, like we do for buffers? */
2018c3e9b4dbSLuiz Otavio O Souza 		ssize_t ofs = 0;
2019c3e9b4dbSLuiz Otavio O Souza 
2020*2ff91c17SVincenzo Maffione 		if (na->rx_rings[i]->ring != NULL && i >= priv->np_qfirst[NR_RX]
2021c3e9b4dbSLuiz Otavio O Souza 				&& i < priv->np_qlast[NR_RX]) {
2022c3e9b4dbSLuiz Otavio O Souza 			ofs = netmap_ring_offset(na->nm_mem,
2023*2ff91c17SVincenzo Maffione 						 na->rx_rings[i]->ring) - base;
202437e3a6d3SLuigi Rizzo 		}
2025c3e9b4dbSLuiz Otavio O Souza 		*(ssize_t *)(uintptr_t)&nifp->ring_ofs[i+n[NR_TX]] = ofs;
2026ccdc3305SLuigi Rizzo 	}
2027ce3ee1e7SLuigi Rizzo 
2028ccdc3305SLuigi Rizzo 	return (nifp);
2029ccdc3305SLuigi Rizzo }
2030ccdc3305SLuigi Rizzo 
2031847bf383SLuigi Rizzo static void
2032847bf383SLuigi Rizzo netmap_mem2_if_delete(struct netmap_adapter *na, struct netmap_if *nifp)
2033ccdc3305SLuigi Rizzo {
2034ce3ee1e7SLuigi Rizzo 	if (nifp == NULL)
2035ce3ee1e7SLuigi Rizzo 		/* nothing to do */
2036ce3ee1e7SLuigi Rizzo 		return;
2037f0ea3689SLuigi Rizzo 	if (nifp->ni_bufs_head)
2038f0ea3689SLuigi Rizzo 		netmap_extra_free(na, nifp->ni_bufs_head);
2039ce3ee1e7SLuigi Rizzo 	netmap_if_free(na->nm_mem, nifp);
2040ce3ee1e7SLuigi Rizzo }
2041ce3ee1e7SLuigi Rizzo 
2042ce3ee1e7SLuigi Rizzo static void
2043c3e9b4dbSLuiz Otavio O Souza netmap_mem2_deref(struct netmap_mem_d *nmd)
2044ce3ee1e7SLuigi Rizzo {
2045ce3ee1e7SLuigi Rizzo 
2046ae10d1afSLuigi Rizzo 	if (netmap_verbose)
2047847bf383SLuigi Rizzo 		D("active = %d", nmd->active);
2048ce3ee1e7SLuigi Rizzo 
2049ce3ee1e7SLuigi Rizzo }
2050ce3ee1e7SLuigi Rizzo 
2051847bf383SLuigi Rizzo struct netmap_mem_ops netmap_mem_global_ops = {
2052847bf383SLuigi Rizzo 	.nmd_get_lut = netmap_mem2_get_lut,
2053847bf383SLuigi Rizzo 	.nmd_get_info = netmap_mem2_get_info,
2054847bf383SLuigi Rizzo 	.nmd_ofstophys = netmap_mem2_ofstophys,
2055c3e9b4dbSLuiz Otavio O Souza 	.nmd_config = netmap_mem2_config,
2056c3e9b4dbSLuiz Otavio O Souza 	.nmd_finalize = netmap_mem2_finalize,
2057c3e9b4dbSLuiz Otavio O Souza 	.nmd_deref = netmap_mem2_deref,
2058c3e9b4dbSLuiz Otavio O Souza 	.nmd_delete = netmap_mem2_delete,
2059847bf383SLuigi Rizzo 	.nmd_if_offset = netmap_mem2_if_offset,
2060847bf383SLuigi Rizzo 	.nmd_if_new = netmap_mem2_if_new,
2061847bf383SLuigi Rizzo 	.nmd_if_delete = netmap_mem2_if_delete,
2062847bf383SLuigi Rizzo 	.nmd_rings_create = netmap_mem2_rings_create,
2063847bf383SLuigi Rizzo 	.nmd_rings_delete = netmap_mem2_rings_delete
2064847bf383SLuigi Rizzo };
206537e3a6d3SLuigi Rizzo 
2066844a6f0cSLuigi Rizzo int
2067*2ff91c17SVincenzo Maffione netmap_mem_pools_info_get(struct nmreq_pools_info *req,
2068*2ff91c17SVincenzo Maffione 				struct netmap_mem_d *nmd)
2069844a6f0cSLuigi Rizzo {
2070844a6f0cSLuigi Rizzo 	int ret;
2071844a6f0cSLuigi Rizzo 
2072*2ff91c17SVincenzo Maffione 	ret = netmap_mem_get_info(nmd, &req->nr_memsize, NULL,
2073*2ff91c17SVincenzo Maffione 					&req->nr_mem_id);
2074844a6f0cSLuigi Rizzo 	if (ret) {
2075844a6f0cSLuigi Rizzo 		return ret;
2076844a6f0cSLuigi Rizzo 	}
2077844a6f0cSLuigi Rizzo 
2078c3e9b4dbSLuiz Otavio O Souza 	NMA_LOCK(nmd);
2079*2ff91c17SVincenzo Maffione 	req->nr_if_pool_offset = 0;
2080*2ff91c17SVincenzo Maffione 	req->nr_if_pool_objtotal = nmd->pools[NETMAP_IF_POOL].objtotal;
2081*2ff91c17SVincenzo Maffione 	req->nr_if_pool_objsize = nmd->pools[NETMAP_IF_POOL]._objsize;
2082844a6f0cSLuigi Rizzo 
2083*2ff91c17SVincenzo Maffione 	req->nr_ring_pool_offset = nmd->pools[NETMAP_IF_POOL].memtotal;
2084*2ff91c17SVincenzo Maffione 	req->nr_ring_pool_objtotal = nmd->pools[NETMAP_RING_POOL].objtotal;
2085*2ff91c17SVincenzo Maffione 	req->nr_ring_pool_objsize = nmd->pools[NETMAP_RING_POOL]._objsize;
2086844a6f0cSLuigi Rizzo 
2087*2ff91c17SVincenzo Maffione 	req->nr_buf_pool_offset = nmd->pools[NETMAP_IF_POOL].memtotal +
2088844a6f0cSLuigi Rizzo 			     nmd->pools[NETMAP_RING_POOL].memtotal;
2089*2ff91c17SVincenzo Maffione 	req->nr_buf_pool_objtotal = nmd->pools[NETMAP_BUF_POOL].objtotal;
2090*2ff91c17SVincenzo Maffione 	req->nr_buf_pool_objsize = nmd->pools[NETMAP_BUF_POOL]._objsize;
2091c3e9b4dbSLuiz Otavio O Souza 	NMA_UNLOCK(nmd);
2092844a6f0cSLuigi Rizzo 
2093844a6f0cSLuigi Rizzo 	return 0;
2094844a6f0cSLuigi Rizzo }
2095844a6f0cSLuigi Rizzo 
20964f80b14cSVincenzo Maffione #ifdef WITH_EXTMEM
20974f80b14cSVincenzo Maffione struct netmap_mem_ext {
20984f80b14cSVincenzo Maffione 	struct netmap_mem_d up;
20994f80b14cSVincenzo Maffione 
2100*2ff91c17SVincenzo Maffione 	struct nm_os_extmem *os;
21014f80b14cSVincenzo Maffione 	struct netmap_mem_ext *next, *prev;
21024f80b14cSVincenzo Maffione };
21034f80b14cSVincenzo Maffione 
21044f80b14cSVincenzo Maffione /* call with nm_mem_list_lock held */
21054f80b14cSVincenzo Maffione static void
21064f80b14cSVincenzo Maffione netmap_mem_ext_register(struct netmap_mem_ext *e)
21074f80b14cSVincenzo Maffione {
21084f80b14cSVincenzo Maffione 	NM_MTX_LOCK(nm_mem_ext_list_lock);
21094f80b14cSVincenzo Maffione 	if (netmap_mem_ext_list)
21104f80b14cSVincenzo Maffione 		netmap_mem_ext_list->prev = e;
21114f80b14cSVincenzo Maffione 	e->next = netmap_mem_ext_list;
21124f80b14cSVincenzo Maffione 	netmap_mem_ext_list = e;
21134f80b14cSVincenzo Maffione 	e->prev = NULL;
21144f80b14cSVincenzo Maffione 	NM_MTX_UNLOCK(nm_mem_ext_list_lock);
21154f80b14cSVincenzo Maffione }
21164f80b14cSVincenzo Maffione 
21174f80b14cSVincenzo Maffione /* call with nm_mem_list_lock held */
21184f80b14cSVincenzo Maffione static void
21194f80b14cSVincenzo Maffione netmap_mem_ext_unregister(struct netmap_mem_ext *e)
21204f80b14cSVincenzo Maffione {
21214f80b14cSVincenzo Maffione 	if (e->prev)
21224f80b14cSVincenzo Maffione 		e->prev->next = e->next;
21234f80b14cSVincenzo Maffione 	else
21244f80b14cSVincenzo Maffione 		netmap_mem_ext_list = e->next;
21254f80b14cSVincenzo Maffione 	if (e->next)
21264f80b14cSVincenzo Maffione 		e->next->prev = e->prev;
21274f80b14cSVincenzo Maffione 	e->prev = e->next = NULL;
21284f80b14cSVincenzo Maffione }
21294f80b14cSVincenzo Maffione 
21304f80b14cSVincenzo Maffione static struct netmap_mem_ext *
2131*2ff91c17SVincenzo Maffione netmap_mem_ext_search(struct nm_os_extmem *os)
21324f80b14cSVincenzo Maffione {
21334f80b14cSVincenzo Maffione 	struct netmap_mem_ext *e;
21344f80b14cSVincenzo Maffione 
21354f80b14cSVincenzo Maffione 	NM_MTX_LOCK(nm_mem_ext_list_lock);
21364f80b14cSVincenzo Maffione 	for (e = netmap_mem_ext_list; e; e = e->next) {
2137*2ff91c17SVincenzo Maffione 		if (nm_os_extmem_isequal(e->os, os)) {
21384f80b14cSVincenzo Maffione 			netmap_mem_get(&e->up);
21394f80b14cSVincenzo Maffione 			break;
21404f80b14cSVincenzo Maffione 		}
21414f80b14cSVincenzo Maffione 	}
21424f80b14cSVincenzo Maffione 	NM_MTX_UNLOCK(nm_mem_ext_list_lock);
21434f80b14cSVincenzo Maffione 	return e;
21444f80b14cSVincenzo Maffione }
21454f80b14cSVincenzo Maffione 
21464f80b14cSVincenzo Maffione 
21474f80b14cSVincenzo Maffione static void
21484f80b14cSVincenzo Maffione netmap_mem_ext_delete(struct netmap_mem_d *d)
21494f80b14cSVincenzo Maffione {
21504f80b14cSVincenzo Maffione 	int i;
21514f80b14cSVincenzo Maffione 	struct netmap_mem_ext *e =
21524f80b14cSVincenzo Maffione 		(struct netmap_mem_ext *)d;
21534f80b14cSVincenzo Maffione 
21544f80b14cSVincenzo Maffione 	netmap_mem_ext_unregister(e);
21554f80b14cSVincenzo Maffione 
21564f80b14cSVincenzo Maffione 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
21574f80b14cSVincenzo Maffione 		struct netmap_obj_pool *p = &d->pools[i];
21584f80b14cSVincenzo Maffione 
21594f80b14cSVincenzo Maffione 		if (p->lut) {
21604f80b14cSVincenzo Maffione 			nm_free_lut(p->lut, p->objtotal);
21614f80b14cSVincenzo Maffione 			p->lut = NULL;
21624f80b14cSVincenzo Maffione 		}
21634f80b14cSVincenzo Maffione 	}
2164*2ff91c17SVincenzo Maffione 	if (e->os)
2165*2ff91c17SVincenzo Maffione 		nm_os_extmem_delete(e->os);
21664f80b14cSVincenzo Maffione 	netmap_mem2_delete(d);
21674f80b14cSVincenzo Maffione }
21684f80b14cSVincenzo Maffione 
21694f80b14cSVincenzo Maffione static int
21704f80b14cSVincenzo Maffione netmap_mem_ext_config(struct netmap_mem_d *nmd)
21714f80b14cSVincenzo Maffione {
21724f80b14cSVincenzo Maffione 	return 0;
21734f80b14cSVincenzo Maffione }
21744f80b14cSVincenzo Maffione 
21754f80b14cSVincenzo Maffione struct netmap_mem_ops netmap_mem_ext_ops = {
21764f80b14cSVincenzo Maffione 	.nmd_get_lut = netmap_mem2_get_lut,
21774f80b14cSVincenzo Maffione 	.nmd_get_info = netmap_mem2_get_info,
21784f80b14cSVincenzo Maffione 	.nmd_ofstophys = netmap_mem2_ofstophys,
21794f80b14cSVincenzo Maffione 	.nmd_config = netmap_mem_ext_config,
21804f80b14cSVincenzo Maffione 	.nmd_finalize = netmap_mem2_finalize,
21814f80b14cSVincenzo Maffione 	.nmd_deref = netmap_mem2_deref,
21824f80b14cSVincenzo Maffione 	.nmd_delete = netmap_mem_ext_delete,
21834f80b14cSVincenzo Maffione 	.nmd_if_offset = netmap_mem2_if_offset,
21844f80b14cSVincenzo Maffione 	.nmd_if_new = netmap_mem2_if_new,
21854f80b14cSVincenzo Maffione 	.nmd_if_delete = netmap_mem2_if_delete,
21864f80b14cSVincenzo Maffione 	.nmd_rings_create = netmap_mem2_rings_create,
21874f80b14cSVincenzo Maffione 	.nmd_rings_delete = netmap_mem2_rings_delete
21884f80b14cSVincenzo Maffione };
21894f80b14cSVincenzo Maffione 
21904f80b14cSVincenzo Maffione struct netmap_mem_d *
2191*2ff91c17SVincenzo Maffione netmap_mem_ext_create(uint64_t usrptr, struct nmreq_pools_info *pi, int *perror)
21924f80b14cSVincenzo Maffione {
21934f80b14cSVincenzo Maffione 	int error = 0;
2194*2ff91c17SVincenzo Maffione 	int i, j;
21954f80b14cSVincenzo Maffione 	struct netmap_mem_ext *nme;
21964f80b14cSVincenzo Maffione 	char *clust;
21974f80b14cSVincenzo Maffione 	size_t off;
2198*2ff91c17SVincenzo Maffione 	struct nm_os_extmem *os = NULL;
2199*2ff91c17SVincenzo Maffione 	int nr_pages;
22004f80b14cSVincenzo Maffione 
22014f80b14cSVincenzo Maffione 	// XXX sanity checks
2202*2ff91c17SVincenzo Maffione 	if (pi->nr_if_pool_objtotal == 0)
2203*2ff91c17SVincenzo Maffione 		pi->nr_if_pool_objtotal = netmap_min_priv_params[NETMAP_IF_POOL].num;
2204*2ff91c17SVincenzo Maffione 	if (pi->nr_if_pool_objsize == 0)
2205*2ff91c17SVincenzo Maffione 		pi->nr_if_pool_objsize = netmap_min_priv_params[NETMAP_IF_POOL].size;
2206*2ff91c17SVincenzo Maffione 	if (pi->nr_ring_pool_objtotal == 0)
2207*2ff91c17SVincenzo Maffione 		pi->nr_ring_pool_objtotal = netmap_min_priv_params[NETMAP_RING_POOL].num;
2208*2ff91c17SVincenzo Maffione 	if (pi->nr_ring_pool_objsize == 0)
2209*2ff91c17SVincenzo Maffione 		pi->nr_ring_pool_objsize = netmap_min_priv_params[NETMAP_RING_POOL].size;
2210*2ff91c17SVincenzo Maffione 	if (pi->nr_buf_pool_objtotal == 0)
2211*2ff91c17SVincenzo Maffione 		pi->nr_buf_pool_objtotal = netmap_min_priv_params[NETMAP_BUF_POOL].num;
2212*2ff91c17SVincenzo Maffione 	if (pi->nr_buf_pool_objsize == 0)
2213*2ff91c17SVincenzo Maffione 		pi->nr_buf_pool_objsize = netmap_min_priv_params[NETMAP_BUF_POOL].size;
22144f80b14cSVincenzo Maffione 	D("if %d %d ring %d %d buf %d %d",
2215*2ff91c17SVincenzo Maffione 			pi->nr_if_pool_objtotal, pi->nr_if_pool_objsize,
2216*2ff91c17SVincenzo Maffione 			pi->nr_ring_pool_objtotal, pi->nr_ring_pool_objsize,
2217*2ff91c17SVincenzo Maffione 			pi->nr_buf_pool_objtotal, pi->nr_buf_pool_objsize);
22184f80b14cSVincenzo Maffione 
2219*2ff91c17SVincenzo Maffione 	os = nm_os_extmem_create(usrptr, pi, &error);
2220*2ff91c17SVincenzo Maffione 	if (os == NULL) {
2221*2ff91c17SVincenzo Maffione 		D("os extmem creation failed");
22224f80b14cSVincenzo Maffione 		goto out;
22234f80b14cSVincenzo Maffione 	}
22244f80b14cSVincenzo Maffione 
2225*2ff91c17SVincenzo Maffione 	nme = netmap_mem_ext_search(os);
22264f80b14cSVincenzo Maffione 	if (nme) {
2227*2ff91c17SVincenzo Maffione 		nm_os_extmem_delete(os);
22284f80b14cSVincenzo Maffione 		return &nme->up;
22294f80b14cSVincenzo Maffione 	}
22304f80b14cSVincenzo Maffione 	D("not found, creating new");
22314f80b14cSVincenzo Maffione 
22324f80b14cSVincenzo Maffione 	nme = _netmap_mem_private_new(sizeof(*nme),
22334f80b14cSVincenzo Maffione 			(struct netmap_obj_params[]){
2234*2ff91c17SVincenzo Maffione 				{ pi->nr_if_pool_objsize, pi->nr_if_pool_objtotal },
2235*2ff91c17SVincenzo Maffione 				{ pi->nr_ring_pool_objsize, pi->nr_ring_pool_objtotal },
2236*2ff91c17SVincenzo Maffione 				{ pi->nr_buf_pool_objsize, pi->nr_buf_pool_objtotal }},
22374f80b14cSVincenzo Maffione 			&netmap_mem_ext_ops,
22384f80b14cSVincenzo Maffione 			&error);
22394f80b14cSVincenzo Maffione 	if (nme == NULL)
22404f80b14cSVincenzo Maffione 		goto out_unmap;
22414f80b14cSVincenzo Maffione 
2242*2ff91c17SVincenzo Maffione 	nr_pages = nm_os_extmem_nr_pages(os);
2243*2ff91c17SVincenzo Maffione 
22444f80b14cSVincenzo Maffione 	/* from now on pages will be released by nme destructor;
22454f80b14cSVincenzo Maffione 	 * we let res = 0 to prevent release in out_unmap below
22464f80b14cSVincenzo Maffione 	 */
2247*2ff91c17SVincenzo Maffione 	nme->os = os;
2248*2ff91c17SVincenzo Maffione 	os = NULL; /* pass ownership */
22494f80b14cSVincenzo Maffione 
2250*2ff91c17SVincenzo Maffione 	clust = nm_os_extmem_nextpage(nme->os);
22514f80b14cSVincenzo Maffione 	off = 0;
22524f80b14cSVincenzo Maffione 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
22534f80b14cSVincenzo Maffione 		struct netmap_obj_pool *p = &nme->up.pools[i];
22544f80b14cSVincenzo Maffione 		struct netmap_obj_params *o = &nme->up.params[i];
22554f80b14cSVincenzo Maffione 
22564f80b14cSVincenzo Maffione 		p->_objsize = o->size;
22574f80b14cSVincenzo Maffione 		p->_clustsize = o->size;
22584f80b14cSVincenzo Maffione 		p->_clustentries = 1;
22594f80b14cSVincenzo Maffione 
22604f80b14cSVincenzo Maffione 		p->lut = nm_alloc_lut(o->num);
22614f80b14cSVincenzo Maffione 		if (p->lut == NULL) {
22624f80b14cSVincenzo Maffione 			error = ENOMEM;
22634f80b14cSVincenzo Maffione 			goto out_delete;
22644f80b14cSVincenzo Maffione 		}
22654f80b14cSVincenzo Maffione 
22664f80b14cSVincenzo Maffione 		p->bitmap_slots = (o->num + sizeof(uint32_t) - 1) / sizeof(uint32_t);
22674f80b14cSVincenzo Maffione 		p->invalid_bitmap = nm_os_malloc(sizeof(uint32_t) * p->bitmap_slots);
22684f80b14cSVincenzo Maffione 		if (p->invalid_bitmap == NULL) {
22694f80b14cSVincenzo Maffione 			error = ENOMEM;
22704f80b14cSVincenzo Maffione 			goto out_delete;
22714f80b14cSVincenzo Maffione 		}
22724f80b14cSVincenzo Maffione 
22734f80b14cSVincenzo Maffione 		if (nr_pages == 0) {
22744f80b14cSVincenzo Maffione 			p->objtotal = 0;
22754f80b14cSVincenzo Maffione 			p->memtotal = 0;
22764f80b14cSVincenzo Maffione 			p->objfree = 0;
22774f80b14cSVincenzo Maffione 			continue;
22784f80b14cSVincenzo Maffione 		}
22794f80b14cSVincenzo Maffione 
22804f80b14cSVincenzo Maffione 		for (j = 0; j < o->num && nr_pages > 0; j++) {
22814f80b14cSVincenzo Maffione 			size_t noff;
22824f80b14cSVincenzo Maffione 
22834f80b14cSVincenzo Maffione 			p->lut[j].vaddr = clust + off;
2284*2ff91c17SVincenzo Maffione #if !defined(linux) && !defined(_WIN32)
2285*2ff91c17SVincenzo Maffione 			p->lut[j].paddr = vtophys(p->lut[j].vaddr);
2286*2ff91c17SVincenzo Maffione #endif
22874f80b14cSVincenzo Maffione 			ND("%s %d at %p", p->name, j, p->lut[j].vaddr);
22884f80b14cSVincenzo Maffione 			noff = off + p->_objsize;
22894f80b14cSVincenzo Maffione 			if (noff < PAGE_SIZE) {
22904f80b14cSVincenzo Maffione 				off = noff;
22914f80b14cSVincenzo Maffione 				continue;
22924f80b14cSVincenzo Maffione 			}
22934f80b14cSVincenzo Maffione 			ND("too big, recomputing offset...");
22944f80b14cSVincenzo Maffione 			while (noff >= PAGE_SIZE) {
2295*2ff91c17SVincenzo Maffione 				char *old_clust = clust;
2296*2ff91c17SVincenzo Maffione 				noff -= PAGE_SIZE;
2297*2ff91c17SVincenzo Maffione 				clust = nm_os_extmem_nextpage(nme->os);
22984f80b14cSVincenzo Maffione 				nr_pages--;
22994f80b14cSVincenzo Maffione 				ND("noff %zu page %p nr_pages %d", noff,
23004f80b14cSVincenzo Maffione 						page_to_virt(*pages), nr_pages);
23014f80b14cSVincenzo Maffione 				if (noff > 0 && !nm_isset(p->invalid_bitmap, j) &&
2302*2ff91c17SVincenzo Maffione 					(nr_pages == 0 ||
2303*2ff91c17SVincenzo Maffione 					 old_clust + PAGE_SIZE != clust))
23044f80b14cSVincenzo Maffione 				{
23054f80b14cSVincenzo Maffione 					/* out of space or non contiguous,
23064f80b14cSVincenzo Maffione 					 * drop this object
23074f80b14cSVincenzo Maffione 					 * */
23084f80b14cSVincenzo Maffione 					p->invalid_bitmap[ (j>>5) ] |= 1U << (j & 31U);
23094f80b14cSVincenzo Maffione 					ND("non contiguous at off %zu, drop", noff);
23104f80b14cSVincenzo Maffione 				}
23114f80b14cSVincenzo Maffione 				if (nr_pages == 0)
23124f80b14cSVincenzo Maffione 					break;
23134f80b14cSVincenzo Maffione 			}
23144f80b14cSVincenzo Maffione 			off = noff;
23154f80b14cSVincenzo Maffione 		}
23164f80b14cSVincenzo Maffione 		p->objtotal = j;
23174f80b14cSVincenzo Maffione 		p->numclusters = p->objtotal;
23184f80b14cSVincenzo Maffione 		p->memtotal = j * p->_objsize;
23194f80b14cSVincenzo Maffione 		ND("%d memtotal %u", j, p->memtotal);
23204f80b14cSVincenzo Maffione 	}
23214f80b14cSVincenzo Maffione 
23224f80b14cSVincenzo Maffione 	netmap_mem_ext_register(nme);
23234f80b14cSVincenzo Maffione 
23244f80b14cSVincenzo Maffione 	return &nme->up;
23254f80b14cSVincenzo Maffione 
23264f80b14cSVincenzo Maffione out_delete:
23274f80b14cSVincenzo Maffione 	netmap_mem_put(&nme->up);
23284f80b14cSVincenzo Maffione out_unmap:
2329*2ff91c17SVincenzo Maffione 	if (os)
2330*2ff91c17SVincenzo Maffione 		nm_os_extmem_delete(os);
23314f80b14cSVincenzo Maffione out:
23324f80b14cSVincenzo Maffione 	if (perror)
23334f80b14cSVincenzo Maffione 		*perror = error;
23344f80b14cSVincenzo Maffione 	return NULL;
23354f80b14cSVincenzo Maffione 
23364f80b14cSVincenzo Maffione }
23374f80b14cSVincenzo Maffione #endif /* WITH_EXTMEM */
23384f80b14cSVincenzo Maffione 
23394f80b14cSVincenzo Maffione 
234037e3a6d3SLuigi Rizzo #ifdef WITH_PTNETMAP_GUEST
234137e3a6d3SLuigi Rizzo struct mem_pt_if {
234237e3a6d3SLuigi Rizzo 	struct mem_pt_if *next;
234337e3a6d3SLuigi Rizzo 	struct ifnet *ifp;
234437e3a6d3SLuigi Rizzo 	unsigned int nifp_offset;
234537e3a6d3SLuigi Rizzo };
234637e3a6d3SLuigi Rizzo 
234737e3a6d3SLuigi Rizzo /* Netmap allocator for ptnetmap guests. */
234837e3a6d3SLuigi Rizzo struct netmap_mem_ptg {
234937e3a6d3SLuigi Rizzo 	struct netmap_mem_d up;
235037e3a6d3SLuigi Rizzo 
235137e3a6d3SLuigi Rizzo 	vm_paddr_t nm_paddr;            /* physical address in the guest */
235237e3a6d3SLuigi Rizzo 	void *nm_addr;                  /* virtual address in the guest */
235337e3a6d3SLuigi Rizzo 	struct netmap_lut buf_lut;      /* lookup table for BUF pool in the guest */
2354844a6f0cSLuigi Rizzo 	nm_memid_t host_mem_id;         /* allocator identifier in the host */
2355844a6f0cSLuigi Rizzo 	struct ptnetmap_memdev *ptn_dev;/* ptnetmap memdev */
235637e3a6d3SLuigi Rizzo 	struct mem_pt_if *pt_ifs;	/* list of interfaces in passthrough */
235737e3a6d3SLuigi Rizzo };
235837e3a6d3SLuigi Rizzo 
235937e3a6d3SLuigi Rizzo /* Link a passthrough interface to a passthrough netmap allocator. */
236037e3a6d3SLuigi Rizzo static int
236137e3a6d3SLuigi Rizzo netmap_mem_pt_guest_ifp_add(struct netmap_mem_d *nmd, struct ifnet *ifp,
2362844a6f0cSLuigi Rizzo 			    unsigned int nifp_offset)
236337e3a6d3SLuigi Rizzo {
236437e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2365c3e9b4dbSLuiz Otavio O Souza 	struct mem_pt_if *ptif = nm_os_malloc(sizeof(*ptif));
236637e3a6d3SLuigi Rizzo 
236737e3a6d3SLuigi Rizzo 	if (!ptif) {
236837e3a6d3SLuigi Rizzo 		return ENOMEM;
236937e3a6d3SLuigi Rizzo 	}
237037e3a6d3SLuigi Rizzo 
237137e3a6d3SLuigi Rizzo 	NMA_LOCK(nmd);
237237e3a6d3SLuigi Rizzo 
237337e3a6d3SLuigi Rizzo 	ptif->ifp = ifp;
237437e3a6d3SLuigi Rizzo 	ptif->nifp_offset = nifp_offset;
237537e3a6d3SLuigi Rizzo 
237637e3a6d3SLuigi Rizzo 	if (ptnmd->pt_ifs) {
237737e3a6d3SLuigi Rizzo 		ptif->next = ptnmd->pt_ifs;
237837e3a6d3SLuigi Rizzo 	}
237937e3a6d3SLuigi Rizzo 	ptnmd->pt_ifs = ptif;
238037e3a6d3SLuigi Rizzo 
238137e3a6d3SLuigi Rizzo 	NMA_UNLOCK(nmd);
238237e3a6d3SLuigi Rizzo 
238337e3a6d3SLuigi Rizzo 	D("added (ifp=%p,nifp_offset=%u)", ptif->ifp, ptif->nifp_offset);
238437e3a6d3SLuigi Rizzo 
238537e3a6d3SLuigi Rizzo 	return 0;
238637e3a6d3SLuigi Rizzo }
238737e3a6d3SLuigi Rizzo 
238837e3a6d3SLuigi Rizzo /* Called with NMA_LOCK(nmd) held. */
238937e3a6d3SLuigi Rizzo static struct mem_pt_if *
239037e3a6d3SLuigi Rizzo netmap_mem_pt_guest_ifp_lookup(struct netmap_mem_d *nmd, struct ifnet *ifp)
239137e3a6d3SLuigi Rizzo {
239237e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
239337e3a6d3SLuigi Rizzo 	struct mem_pt_if *curr;
239437e3a6d3SLuigi Rizzo 
239537e3a6d3SLuigi Rizzo 	for (curr = ptnmd->pt_ifs; curr; curr = curr->next) {
239637e3a6d3SLuigi Rizzo 		if (curr->ifp == ifp) {
239737e3a6d3SLuigi Rizzo 			return curr;
239837e3a6d3SLuigi Rizzo 		}
239937e3a6d3SLuigi Rizzo 	}
240037e3a6d3SLuigi Rizzo 
240137e3a6d3SLuigi Rizzo 	return NULL;
240237e3a6d3SLuigi Rizzo }
240337e3a6d3SLuigi Rizzo 
240437e3a6d3SLuigi Rizzo /* Unlink a passthrough interface from a passthrough netmap allocator. */
240537e3a6d3SLuigi Rizzo int
240637e3a6d3SLuigi Rizzo netmap_mem_pt_guest_ifp_del(struct netmap_mem_d *nmd, struct ifnet *ifp)
240737e3a6d3SLuigi Rizzo {
240837e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
240937e3a6d3SLuigi Rizzo 	struct mem_pt_if *prev = NULL;
241037e3a6d3SLuigi Rizzo 	struct mem_pt_if *curr;
241137e3a6d3SLuigi Rizzo 	int ret = -1;
241237e3a6d3SLuigi Rizzo 
241337e3a6d3SLuigi Rizzo 	NMA_LOCK(nmd);
241437e3a6d3SLuigi Rizzo 
241537e3a6d3SLuigi Rizzo 	for (curr = ptnmd->pt_ifs; curr; curr = curr->next) {
241637e3a6d3SLuigi Rizzo 		if (curr->ifp == ifp) {
241737e3a6d3SLuigi Rizzo 			if (prev) {
241837e3a6d3SLuigi Rizzo 				prev->next = curr->next;
241937e3a6d3SLuigi Rizzo 			} else {
242037e3a6d3SLuigi Rizzo 				ptnmd->pt_ifs = curr->next;
242137e3a6d3SLuigi Rizzo 			}
242237e3a6d3SLuigi Rizzo 			D("removed (ifp=%p,nifp_offset=%u)",
242337e3a6d3SLuigi Rizzo 			  curr->ifp, curr->nifp_offset);
2424c3e9b4dbSLuiz Otavio O Souza 			nm_os_free(curr);
242537e3a6d3SLuigi Rizzo 			ret = 0;
242637e3a6d3SLuigi Rizzo 			break;
242737e3a6d3SLuigi Rizzo 		}
242837e3a6d3SLuigi Rizzo 		prev = curr;
242937e3a6d3SLuigi Rizzo 	}
243037e3a6d3SLuigi Rizzo 
243137e3a6d3SLuigi Rizzo 	NMA_UNLOCK(nmd);
243237e3a6d3SLuigi Rizzo 
243337e3a6d3SLuigi Rizzo 	return ret;
243437e3a6d3SLuigi Rizzo }
243537e3a6d3SLuigi Rizzo 
243637e3a6d3SLuigi Rizzo static int
243737e3a6d3SLuigi Rizzo netmap_mem_pt_guest_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut)
243837e3a6d3SLuigi Rizzo {
243937e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
244037e3a6d3SLuigi Rizzo 
244137e3a6d3SLuigi Rizzo 	if (!(nmd->flags & NETMAP_MEM_FINALIZED)) {
244237e3a6d3SLuigi Rizzo 		return EINVAL;
244337e3a6d3SLuigi Rizzo 	}
244437e3a6d3SLuigi Rizzo 
244537e3a6d3SLuigi Rizzo 	*lut = ptnmd->buf_lut;
244637e3a6d3SLuigi Rizzo 	return 0;
244737e3a6d3SLuigi Rizzo }
244837e3a6d3SLuigi Rizzo 
244937e3a6d3SLuigi Rizzo static int
24504f80b14cSVincenzo Maffione netmap_mem_pt_guest_get_info(struct netmap_mem_d *nmd, uint64_t *size,
245137e3a6d3SLuigi Rizzo 			     u_int *memflags, uint16_t *id)
245237e3a6d3SLuigi Rizzo {
245337e3a6d3SLuigi Rizzo 	int error = 0;
245437e3a6d3SLuigi Rizzo 
245537e3a6d3SLuigi Rizzo 	error = nmd->ops->nmd_config(nmd);
245637e3a6d3SLuigi Rizzo 	if (error)
245737e3a6d3SLuigi Rizzo 		goto out;
245837e3a6d3SLuigi Rizzo 
245937e3a6d3SLuigi Rizzo 	if (size)
246037e3a6d3SLuigi Rizzo 		*size = nmd->nm_totalsize;
246137e3a6d3SLuigi Rizzo 	if (memflags)
246237e3a6d3SLuigi Rizzo 		*memflags = nmd->flags;
246337e3a6d3SLuigi Rizzo 	if (id)
246437e3a6d3SLuigi Rizzo 		*id = nmd->nm_id;
246537e3a6d3SLuigi Rizzo 
246637e3a6d3SLuigi Rizzo out:
246737e3a6d3SLuigi Rizzo 
246837e3a6d3SLuigi Rizzo 	return error;
246937e3a6d3SLuigi Rizzo }
247037e3a6d3SLuigi Rizzo 
247137e3a6d3SLuigi Rizzo static vm_paddr_t
247237e3a6d3SLuigi Rizzo netmap_mem_pt_guest_ofstophys(struct netmap_mem_d *nmd, vm_ooffset_t off)
247337e3a6d3SLuigi Rizzo {
247437e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
247537e3a6d3SLuigi Rizzo 	vm_paddr_t paddr;
247637e3a6d3SLuigi Rizzo 	/* if the offset is valid, just return csb->base_addr + off */
247737e3a6d3SLuigi Rizzo 	paddr = (vm_paddr_t)(ptnmd->nm_paddr + off);
247837e3a6d3SLuigi Rizzo 	ND("off %lx padr %lx", off, (unsigned long)paddr);
247937e3a6d3SLuigi Rizzo 	return paddr;
248037e3a6d3SLuigi Rizzo }
248137e3a6d3SLuigi Rizzo 
248237e3a6d3SLuigi Rizzo static int
248337e3a6d3SLuigi Rizzo netmap_mem_pt_guest_config(struct netmap_mem_d *nmd)
248437e3a6d3SLuigi Rizzo {
248537e3a6d3SLuigi Rizzo 	/* nothing to do, we are configured on creation
248637e3a6d3SLuigi Rizzo 	 * and configuration never changes thereafter
248737e3a6d3SLuigi Rizzo 	 */
248837e3a6d3SLuigi Rizzo 	return 0;
248937e3a6d3SLuigi Rizzo }
249037e3a6d3SLuigi Rizzo 
249137e3a6d3SLuigi Rizzo static int
249237e3a6d3SLuigi Rizzo netmap_mem_pt_guest_finalize(struct netmap_mem_d *nmd)
249337e3a6d3SLuigi Rizzo {
249437e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2495844a6f0cSLuigi Rizzo 	uint64_t mem_size;
2496844a6f0cSLuigi Rizzo 	uint32_t bufsize;
2497844a6f0cSLuigi Rizzo 	uint32_t nbuffers;
2498844a6f0cSLuigi Rizzo 	uint32_t poolofs;
2499844a6f0cSLuigi Rizzo 	vm_paddr_t paddr;
2500844a6f0cSLuigi Rizzo 	char *vaddr;
2501844a6f0cSLuigi Rizzo 	int i;
250237e3a6d3SLuigi Rizzo 	int error = 0;
250337e3a6d3SLuigi Rizzo 
250437e3a6d3SLuigi Rizzo 	if (nmd->flags & NETMAP_MEM_FINALIZED)
250537e3a6d3SLuigi Rizzo 		goto out;
250637e3a6d3SLuigi Rizzo 
250737e3a6d3SLuigi Rizzo 	if (ptnmd->ptn_dev == NULL) {
250837e3a6d3SLuigi Rizzo 		D("ptnetmap memdev not attached");
250937e3a6d3SLuigi Rizzo 		error = ENOMEM;
2510*2ff91c17SVincenzo Maffione 		goto out;
251137e3a6d3SLuigi Rizzo 	}
2512844a6f0cSLuigi Rizzo 	/* Map memory through ptnetmap-memdev BAR. */
251337e3a6d3SLuigi Rizzo 	error = nm_os_pt_memdev_iomap(ptnmd->ptn_dev, &ptnmd->nm_paddr,
2514844a6f0cSLuigi Rizzo 				      &ptnmd->nm_addr, &mem_size);
251537e3a6d3SLuigi Rizzo 	if (error)
2516*2ff91c17SVincenzo Maffione 		goto out;
251737e3a6d3SLuigi Rizzo 
2518844a6f0cSLuigi Rizzo         /* Initialize the lut using the information contained in the
2519844a6f0cSLuigi Rizzo 	 * ptnetmap memory device. */
2520844a6f0cSLuigi Rizzo         bufsize = nm_os_pt_memdev_ioread(ptnmd->ptn_dev,
2521844a6f0cSLuigi Rizzo 					 PTNET_MDEV_IO_BUF_POOL_OBJSZ);
2522844a6f0cSLuigi Rizzo         nbuffers = nm_os_pt_memdev_ioread(ptnmd->ptn_dev,
2523844a6f0cSLuigi Rizzo 					 PTNET_MDEV_IO_BUF_POOL_OBJNUM);
2524844a6f0cSLuigi Rizzo 
2525844a6f0cSLuigi Rizzo 	/* allocate the lut */
2526844a6f0cSLuigi Rizzo 	if (ptnmd->buf_lut.lut == NULL) {
2527844a6f0cSLuigi Rizzo 		D("allocating lut");
2528844a6f0cSLuigi Rizzo 		ptnmd->buf_lut.lut = nm_alloc_lut(nbuffers);
2529844a6f0cSLuigi Rizzo 		if (ptnmd->buf_lut.lut == NULL) {
2530844a6f0cSLuigi Rizzo 			D("lut allocation failed");
2531844a6f0cSLuigi Rizzo 			return ENOMEM;
2532844a6f0cSLuigi Rizzo 		}
2533844a6f0cSLuigi Rizzo 	}
2534844a6f0cSLuigi Rizzo 
2535844a6f0cSLuigi Rizzo 	/* we have physically contiguous memory mapped through PCI BAR */
2536844a6f0cSLuigi Rizzo 	poolofs = nm_os_pt_memdev_ioread(ptnmd->ptn_dev,
2537844a6f0cSLuigi Rizzo 					 PTNET_MDEV_IO_BUF_POOL_OFS);
2538844a6f0cSLuigi Rizzo 	vaddr = (char *)(ptnmd->nm_addr) + poolofs;
2539844a6f0cSLuigi Rizzo 	paddr = ptnmd->nm_paddr + poolofs;
2540844a6f0cSLuigi Rizzo 
2541844a6f0cSLuigi Rizzo 	for (i = 0; i < nbuffers; i++) {
2542844a6f0cSLuigi Rizzo 		ptnmd->buf_lut.lut[i].vaddr = vaddr;
2543844a6f0cSLuigi Rizzo 		vaddr += bufsize;
2544844a6f0cSLuigi Rizzo 		paddr += bufsize;
2545844a6f0cSLuigi Rizzo 	}
2546844a6f0cSLuigi Rizzo 
2547844a6f0cSLuigi Rizzo 	ptnmd->buf_lut.objtotal = nbuffers;
2548844a6f0cSLuigi Rizzo 	ptnmd->buf_lut.objsize = bufsize;
2549844a6f0cSLuigi Rizzo 	nmd->nm_totalsize = (unsigned int)mem_size;
255037e3a6d3SLuigi Rizzo 
2551*2ff91c17SVincenzo Maffione 	/* Initialize these fields as are needed by
2552*2ff91c17SVincenzo Maffione 	 * netmap_mem_bufsize().
2553*2ff91c17SVincenzo Maffione 	 * XXX please improve this, why do we need this
2554*2ff91c17SVincenzo Maffione 	 * replication? maybe we nmd->pools[] should no be
2555*2ff91c17SVincenzo Maffione 	 * there for the guest allocator? */
2556*2ff91c17SVincenzo Maffione 	nmd->pools[NETMAP_BUF_POOL]._objsize = bufsize;
2557*2ff91c17SVincenzo Maffione 	nmd->pools[NETMAP_BUF_POOL]._objtotal = nbuffers;
2558*2ff91c17SVincenzo Maffione 
255937e3a6d3SLuigi Rizzo 	nmd->flags |= NETMAP_MEM_FINALIZED;
256037e3a6d3SLuigi Rizzo out:
256137e3a6d3SLuigi Rizzo 	return error;
256237e3a6d3SLuigi Rizzo }
256337e3a6d3SLuigi Rizzo 
256437e3a6d3SLuigi Rizzo static void
256537e3a6d3SLuigi Rizzo netmap_mem_pt_guest_deref(struct netmap_mem_d *nmd)
256637e3a6d3SLuigi Rizzo {
256737e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
256837e3a6d3SLuigi Rizzo 
2569*2ff91c17SVincenzo Maffione 	if (nmd->active == 1 &&
257037e3a6d3SLuigi Rizzo 		(nmd->flags & NETMAP_MEM_FINALIZED)) {
257137e3a6d3SLuigi Rizzo 	    nmd->flags  &= ~NETMAP_MEM_FINALIZED;
257237e3a6d3SLuigi Rizzo 	    /* unmap ptnetmap-memdev memory */
257337e3a6d3SLuigi Rizzo 	    if (ptnmd->ptn_dev) {
257437e3a6d3SLuigi Rizzo 		nm_os_pt_memdev_iounmap(ptnmd->ptn_dev);
257537e3a6d3SLuigi Rizzo 	    }
25764dd44461SLuiz Otavio O Souza 	    ptnmd->nm_addr = NULL;
257737e3a6d3SLuigi Rizzo 	    ptnmd->nm_paddr = 0;
257837e3a6d3SLuigi Rizzo 	}
257937e3a6d3SLuigi Rizzo }
258037e3a6d3SLuigi Rizzo 
258137e3a6d3SLuigi Rizzo static ssize_t
258237e3a6d3SLuigi Rizzo netmap_mem_pt_guest_if_offset(struct netmap_mem_d *nmd, const void *vaddr)
258337e3a6d3SLuigi Rizzo {
258437e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
258537e3a6d3SLuigi Rizzo 
258637e3a6d3SLuigi Rizzo 	return (const char *)(vaddr) - (char *)(ptnmd->nm_addr);
258737e3a6d3SLuigi Rizzo }
258837e3a6d3SLuigi Rizzo 
258937e3a6d3SLuigi Rizzo static void
259037e3a6d3SLuigi Rizzo netmap_mem_pt_guest_delete(struct netmap_mem_d *nmd)
259137e3a6d3SLuigi Rizzo {
259237e3a6d3SLuigi Rizzo 	if (nmd == NULL)
259337e3a6d3SLuigi Rizzo 		return;
259437e3a6d3SLuigi Rizzo 	if (netmap_verbose)
259537e3a6d3SLuigi Rizzo 		D("deleting %p", nmd);
259637e3a6d3SLuigi Rizzo 	if (nmd->active > 0)
259737e3a6d3SLuigi Rizzo 		D("bug: deleting mem allocator with active=%d!", nmd->active);
259837e3a6d3SLuigi Rizzo 	if (netmap_verbose)
259937e3a6d3SLuigi Rizzo 		D("done deleting %p", nmd);
260037e3a6d3SLuigi Rizzo 	NMA_LOCK_DESTROY(nmd);
2601c3e9b4dbSLuiz Otavio O Souza 	nm_os_free(nmd);
260237e3a6d3SLuigi Rizzo }
260337e3a6d3SLuigi Rizzo 
260437e3a6d3SLuigi Rizzo static struct netmap_if *
2605c3e9b4dbSLuiz Otavio O Souza netmap_mem_pt_guest_if_new(struct netmap_adapter *na, struct netmap_priv_d *priv)
260637e3a6d3SLuigi Rizzo {
260737e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)na->nm_mem;
260837e3a6d3SLuigi Rizzo 	struct mem_pt_if *ptif;
260937e3a6d3SLuigi Rizzo 	struct netmap_if *nifp = NULL;
261037e3a6d3SLuigi Rizzo 
261137e3a6d3SLuigi Rizzo 	ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, na->ifp);
261237e3a6d3SLuigi Rizzo 	if (ptif == NULL) {
261337e3a6d3SLuigi Rizzo 		D("Error: interface %p is not in passthrough", na->ifp);
261437e3a6d3SLuigi Rizzo 		goto out;
261537e3a6d3SLuigi Rizzo 	}
261637e3a6d3SLuigi Rizzo 
261737e3a6d3SLuigi Rizzo 	nifp = (struct netmap_if *)((char *)(ptnmd->nm_addr) +
261837e3a6d3SLuigi Rizzo 				    ptif->nifp_offset);
261937e3a6d3SLuigi Rizzo out:
262037e3a6d3SLuigi Rizzo 	return nifp;
262137e3a6d3SLuigi Rizzo }
262237e3a6d3SLuigi Rizzo 
262337e3a6d3SLuigi Rizzo static void
262437e3a6d3SLuigi Rizzo netmap_mem_pt_guest_if_delete(struct netmap_adapter *na, struct netmap_if *nifp)
262537e3a6d3SLuigi Rizzo {
262637e3a6d3SLuigi Rizzo 	struct mem_pt_if *ptif;
262737e3a6d3SLuigi Rizzo 
262837e3a6d3SLuigi Rizzo 	ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, na->ifp);
262937e3a6d3SLuigi Rizzo 	if (ptif == NULL) {
263037e3a6d3SLuigi Rizzo 		D("Error: interface %p is not in passthrough", na->ifp);
263137e3a6d3SLuigi Rizzo 	}
263237e3a6d3SLuigi Rizzo }
263337e3a6d3SLuigi Rizzo 
263437e3a6d3SLuigi Rizzo static int
263537e3a6d3SLuigi Rizzo netmap_mem_pt_guest_rings_create(struct netmap_adapter *na)
263637e3a6d3SLuigi Rizzo {
263737e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)na->nm_mem;
263837e3a6d3SLuigi Rizzo 	struct mem_pt_if *ptif;
263937e3a6d3SLuigi Rizzo 	struct netmap_if *nifp;
264037e3a6d3SLuigi Rizzo 	int i, error = -1;
264137e3a6d3SLuigi Rizzo 
264237e3a6d3SLuigi Rizzo 	ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, na->ifp);
264337e3a6d3SLuigi Rizzo 	if (ptif == NULL) {
264437e3a6d3SLuigi Rizzo 		D("Error: interface %p is not in passthrough", na->ifp);
264537e3a6d3SLuigi Rizzo 		goto out;
264637e3a6d3SLuigi Rizzo 	}
264737e3a6d3SLuigi Rizzo 
264837e3a6d3SLuigi Rizzo 
264937e3a6d3SLuigi Rizzo 	/* point each kring to the corresponding backend ring */
265037e3a6d3SLuigi Rizzo 	nifp = (struct netmap_if *)((char *)ptnmd->nm_addr + ptif->nifp_offset);
265137e3a6d3SLuigi Rizzo 	for (i = 0; i <= na->num_tx_rings; i++) {
2652*2ff91c17SVincenzo Maffione 		struct netmap_kring *kring = na->tx_rings[i];
265337e3a6d3SLuigi Rizzo 		if (kring->ring)
265437e3a6d3SLuigi Rizzo 			continue;
265537e3a6d3SLuigi Rizzo 		kring->ring = (struct netmap_ring *)
265637e3a6d3SLuigi Rizzo 			((char *)nifp + nifp->ring_ofs[i]);
265737e3a6d3SLuigi Rizzo 	}
265837e3a6d3SLuigi Rizzo 	for (i = 0; i <= na->num_rx_rings; i++) {
2659*2ff91c17SVincenzo Maffione 		struct netmap_kring *kring = na->rx_rings[i];
266037e3a6d3SLuigi Rizzo 		if (kring->ring)
266137e3a6d3SLuigi Rizzo 			continue;
266237e3a6d3SLuigi Rizzo 		kring->ring = (struct netmap_ring *)
266337e3a6d3SLuigi Rizzo 			((char *)nifp +
266437e3a6d3SLuigi Rizzo 			 nifp->ring_ofs[i + na->num_tx_rings + 1]);
266537e3a6d3SLuigi Rizzo 	}
266637e3a6d3SLuigi Rizzo 
266737e3a6d3SLuigi Rizzo 	error = 0;
266837e3a6d3SLuigi Rizzo out:
266937e3a6d3SLuigi Rizzo 	return error;
267037e3a6d3SLuigi Rizzo }
267137e3a6d3SLuigi Rizzo 
267237e3a6d3SLuigi Rizzo static void
267337e3a6d3SLuigi Rizzo netmap_mem_pt_guest_rings_delete(struct netmap_adapter *na)
267437e3a6d3SLuigi Rizzo {
267537e3a6d3SLuigi Rizzo #if 0
26764f80b14cSVincenzo Maffione 	enum txrx t;
26774f80b14cSVincenzo Maffione 
26784f80b14cSVincenzo Maffione 	for_rx_tx(t) {
26794f80b14cSVincenzo Maffione 		u_int i;
26804f80b14cSVincenzo Maffione 		for (i = 0; i < nma_get_nrings(na, t) + 1; i++) {
26814f80b14cSVincenzo Maffione 			struct netmap_kring *kring = &NMR(na, t)[i];
26824f80b14cSVincenzo Maffione 
26834f80b14cSVincenzo Maffione 			kring->ring = NULL;
26844f80b14cSVincenzo Maffione 		}
26854f80b14cSVincenzo Maffione 	}
268637e3a6d3SLuigi Rizzo #endif
268737e3a6d3SLuigi Rizzo }
268837e3a6d3SLuigi Rizzo 
268937e3a6d3SLuigi Rizzo static struct netmap_mem_ops netmap_mem_pt_guest_ops = {
269037e3a6d3SLuigi Rizzo 	.nmd_get_lut = netmap_mem_pt_guest_get_lut,
269137e3a6d3SLuigi Rizzo 	.nmd_get_info = netmap_mem_pt_guest_get_info,
269237e3a6d3SLuigi Rizzo 	.nmd_ofstophys = netmap_mem_pt_guest_ofstophys,
269337e3a6d3SLuigi Rizzo 	.nmd_config = netmap_mem_pt_guest_config,
269437e3a6d3SLuigi Rizzo 	.nmd_finalize = netmap_mem_pt_guest_finalize,
269537e3a6d3SLuigi Rizzo 	.nmd_deref = netmap_mem_pt_guest_deref,
269637e3a6d3SLuigi Rizzo 	.nmd_if_offset = netmap_mem_pt_guest_if_offset,
269737e3a6d3SLuigi Rizzo 	.nmd_delete = netmap_mem_pt_guest_delete,
269837e3a6d3SLuigi Rizzo 	.nmd_if_new = netmap_mem_pt_guest_if_new,
269937e3a6d3SLuigi Rizzo 	.nmd_if_delete = netmap_mem_pt_guest_if_delete,
270037e3a6d3SLuigi Rizzo 	.nmd_rings_create = netmap_mem_pt_guest_rings_create,
270137e3a6d3SLuigi Rizzo 	.nmd_rings_delete = netmap_mem_pt_guest_rings_delete
270237e3a6d3SLuigi Rizzo };
270337e3a6d3SLuigi Rizzo 
2704c3e9b4dbSLuiz Otavio O Souza /* Called with nm_mem_list_lock held. */
270537e3a6d3SLuigi Rizzo static struct netmap_mem_d *
2706844a6f0cSLuigi Rizzo netmap_mem_pt_guest_find_memid(nm_memid_t mem_id)
270737e3a6d3SLuigi Rizzo {
270837e3a6d3SLuigi Rizzo 	struct netmap_mem_d *mem = NULL;
270937e3a6d3SLuigi Rizzo 	struct netmap_mem_d *scan = netmap_last_mem_d;
271037e3a6d3SLuigi Rizzo 
271137e3a6d3SLuigi Rizzo 	do {
271237e3a6d3SLuigi Rizzo 		/* find ptnetmap allocator through host ID */
271337e3a6d3SLuigi Rizzo 		if (scan->ops->nmd_deref == netmap_mem_pt_guest_deref &&
2714844a6f0cSLuigi Rizzo 			((struct netmap_mem_ptg *)(scan))->host_mem_id == mem_id) {
271537e3a6d3SLuigi Rizzo 			mem = scan;
2716c3e9b4dbSLuiz Otavio O Souza 			mem->refcount++;
2717c3e9b4dbSLuiz Otavio O Souza 			NM_DBG_REFC(mem, __FUNCTION__, __LINE__);
271837e3a6d3SLuigi Rizzo 			break;
271937e3a6d3SLuigi Rizzo 		}
272037e3a6d3SLuigi Rizzo 		scan = scan->next;
272137e3a6d3SLuigi Rizzo 	} while (scan != netmap_last_mem_d);
272237e3a6d3SLuigi Rizzo 
272337e3a6d3SLuigi Rizzo 	return mem;
272437e3a6d3SLuigi Rizzo }
272537e3a6d3SLuigi Rizzo 
2726c3e9b4dbSLuiz Otavio O Souza /* Called with nm_mem_list_lock held. */
272737e3a6d3SLuigi Rizzo static struct netmap_mem_d *
2728844a6f0cSLuigi Rizzo netmap_mem_pt_guest_create(nm_memid_t mem_id)
272937e3a6d3SLuigi Rizzo {
273037e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd;
273137e3a6d3SLuigi Rizzo 	int err = 0;
273237e3a6d3SLuigi Rizzo 
2733c3e9b4dbSLuiz Otavio O Souza 	ptnmd = nm_os_malloc(sizeof(struct netmap_mem_ptg));
273437e3a6d3SLuigi Rizzo 	if (ptnmd == NULL) {
273537e3a6d3SLuigi Rizzo 		err = ENOMEM;
273637e3a6d3SLuigi Rizzo 		goto error;
273737e3a6d3SLuigi Rizzo 	}
273837e3a6d3SLuigi Rizzo 
273937e3a6d3SLuigi Rizzo 	ptnmd->up.ops = &netmap_mem_pt_guest_ops;
2740844a6f0cSLuigi Rizzo 	ptnmd->host_mem_id = mem_id;
274137e3a6d3SLuigi Rizzo 	ptnmd->pt_ifs = NULL;
274237e3a6d3SLuigi Rizzo 
274337e3a6d3SLuigi Rizzo         /* Assign new id in the guest (We have the lock) */
274437e3a6d3SLuigi Rizzo 	err = nm_mem_assign_id_locked(&ptnmd->up);
274537e3a6d3SLuigi Rizzo 	if (err)
274637e3a6d3SLuigi Rizzo 		goto error;
274737e3a6d3SLuigi Rizzo 
274837e3a6d3SLuigi Rizzo 	ptnmd->up.flags &= ~NETMAP_MEM_FINALIZED;
274937e3a6d3SLuigi Rizzo 	ptnmd->up.flags |= NETMAP_MEM_IO;
275037e3a6d3SLuigi Rizzo 
275137e3a6d3SLuigi Rizzo 	NMA_LOCK_INIT(&ptnmd->up);
275237e3a6d3SLuigi Rizzo 
2753c3e9b4dbSLuiz Otavio O Souza 	snprintf(ptnmd->up.name, NM_MEM_NAMESZ, "%d", ptnmd->up.nm_id);
2754c3e9b4dbSLuiz Otavio O Souza 
2755c3e9b4dbSLuiz Otavio O Souza 
275637e3a6d3SLuigi Rizzo 	return &ptnmd->up;
275737e3a6d3SLuigi Rizzo error:
275837e3a6d3SLuigi Rizzo 	netmap_mem_pt_guest_delete(&ptnmd->up);
275937e3a6d3SLuigi Rizzo 	return NULL;
276037e3a6d3SLuigi Rizzo }
276137e3a6d3SLuigi Rizzo 
276237e3a6d3SLuigi Rizzo /*
276337e3a6d3SLuigi Rizzo  * find host id in guest allocators and create guest allocator
276437e3a6d3SLuigi Rizzo  * if it is not there
276537e3a6d3SLuigi Rizzo  */
276637e3a6d3SLuigi Rizzo static struct netmap_mem_d *
2767844a6f0cSLuigi Rizzo netmap_mem_pt_guest_get(nm_memid_t mem_id)
276837e3a6d3SLuigi Rizzo {
276937e3a6d3SLuigi Rizzo 	struct netmap_mem_d *nmd;
277037e3a6d3SLuigi Rizzo 
2771c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_LOCK(nm_mem_list_lock);
2772844a6f0cSLuigi Rizzo 	nmd = netmap_mem_pt_guest_find_memid(mem_id);
277337e3a6d3SLuigi Rizzo 	if (nmd == NULL) {
2774844a6f0cSLuigi Rizzo 		nmd = netmap_mem_pt_guest_create(mem_id);
277537e3a6d3SLuigi Rizzo 	}
2776c3e9b4dbSLuiz Otavio O Souza 	NM_MTX_UNLOCK(nm_mem_list_lock);
277737e3a6d3SLuigi Rizzo 
277837e3a6d3SLuigi Rizzo 	return nmd;
277937e3a6d3SLuigi Rizzo }
278037e3a6d3SLuigi Rizzo 
278137e3a6d3SLuigi Rizzo /*
278237e3a6d3SLuigi Rizzo  * The guest allocator can be created by ptnetmap_memdev (during the device
2783844a6f0cSLuigi Rizzo  * attach) or by ptnetmap device (ptnet), during the netmap_attach.
278437e3a6d3SLuigi Rizzo  *
278537e3a6d3SLuigi Rizzo  * The order is not important (we have different order in LINUX and FreeBSD).
278637e3a6d3SLuigi Rizzo  * The first one, creates the device, and the second one simply attaches it.
278737e3a6d3SLuigi Rizzo  */
278837e3a6d3SLuigi Rizzo 
278937e3a6d3SLuigi Rizzo /* Called when ptnetmap_memdev is attaching, to attach a new allocator in
279037e3a6d3SLuigi Rizzo  * the guest */
279137e3a6d3SLuigi Rizzo struct netmap_mem_d *
2792844a6f0cSLuigi Rizzo netmap_mem_pt_guest_attach(struct ptnetmap_memdev *ptn_dev, nm_memid_t mem_id)
279337e3a6d3SLuigi Rizzo {
279437e3a6d3SLuigi Rizzo 	struct netmap_mem_d *nmd;
279537e3a6d3SLuigi Rizzo 	struct netmap_mem_ptg *ptnmd;
279637e3a6d3SLuigi Rizzo 
2797844a6f0cSLuigi Rizzo 	nmd = netmap_mem_pt_guest_get(mem_id);
279837e3a6d3SLuigi Rizzo 
279937e3a6d3SLuigi Rizzo 	/* assign this device to the guest allocator */
280037e3a6d3SLuigi Rizzo 	if (nmd) {
280137e3a6d3SLuigi Rizzo 		ptnmd = (struct netmap_mem_ptg *)nmd;
280237e3a6d3SLuigi Rizzo 		ptnmd->ptn_dev = ptn_dev;
280337e3a6d3SLuigi Rizzo 	}
280437e3a6d3SLuigi Rizzo 
280537e3a6d3SLuigi Rizzo 	return nmd;
280637e3a6d3SLuigi Rizzo }
280737e3a6d3SLuigi Rizzo 
2808844a6f0cSLuigi Rizzo /* Called when ptnet device is attaching */
280937e3a6d3SLuigi Rizzo struct netmap_mem_d *
281037e3a6d3SLuigi Rizzo netmap_mem_pt_guest_new(struct ifnet *ifp,
281137e3a6d3SLuigi Rizzo 			unsigned int nifp_offset,
2812844a6f0cSLuigi Rizzo 			unsigned int memid)
281337e3a6d3SLuigi Rizzo {
281437e3a6d3SLuigi Rizzo 	struct netmap_mem_d *nmd;
281537e3a6d3SLuigi Rizzo 
2816844a6f0cSLuigi Rizzo 	if (ifp == NULL) {
281737e3a6d3SLuigi Rizzo 		return NULL;
281837e3a6d3SLuigi Rizzo 	}
281937e3a6d3SLuigi Rizzo 
2820844a6f0cSLuigi Rizzo 	nmd = netmap_mem_pt_guest_get((nm_memid_t)memid);
282137e3a6d3SLuigi Rizzo 
282237e3a6d3SLuigi Rizzo 	if (nmd) {
2823844a6f0cSLuigi Rizzo 		netmap_mem_pt_guest_ifp_add(nmd, ifp, nifp_offset);
282437e3a6d3SLuigi Rizzo 	}
282537e3a6d3SLuigi Rizzo 
282637e3a6d3SLuigi Rizzo 	return nmd;
282737e3a6d3SLuigi Rizzo }
282837e3a6d3SLuigi Rizzo 
282937e3a6d3SLuigi Rizzo #endif /* WITH_PTNETMAP_GUEST */
2830