xref: /freebsd/sys/dev/netmap/netmap_mem2.c (revision 4f52dfbb)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 2012-2014 Matteo Landi
5  * Copyright (C) 2012-2016 Luigi Rizzo
6  * Copyright (C) 2012-2016 Giuseppe Lettieri
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *   1. Redistributions of source code must retain the above copyright
13  *      notice, this list of conditions and the following disclaimer.
14  *   2. Redistributions in binary form must reproduce the above copyright
15  *      notice, this list of conditions and the following disclaimer in the
16  *      documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #ifdef linux
32 #include "bsd_glue.h"
33 #endif /* linux */
34 
35 #ifdef __APPLE__
36 #include "osx_glue.h"
37 #endif /* __APPLE__ */
38 
39 #ifdef __FreeBSD__
40 #include <sys/cdefs.h> /* prerequisite */
41 __FBSDID("$FreeBSD$");
42 
43 #include <sys/types.h>
44 #include <sys/malloc.h>
45 #include <sys/kernel.h>		/* MALLOC_DEFINE */
46 #include <sys/proc.h>
47 #include <vm/vm.h>	/* vtophys */
48 #include <vm/pmap.h>	/* vtophys */
49 #include <sys/socket.h> /* sockaddrs */
50 #include <sys/selinfo.h>
51 #include <sys/sysctl.h>
52 #include <net/if.h>
53 #include <net/if_var.h>
54 #include <net/vnet.h>
55 #include <machine/bus.h>	/* bus_dmamap_* */
56 
57 /* M_NETMAP only used in here */
58 MALLOC_DECLARE(M_NETMAP);
59 MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map");
60 
61 #endif /* __FreeBSD__ */
62 
63 #ifdef _WIN32
64 #include <win_glue.h>
65 #endif
66 
67 #include <net/netmap.h>
68 #include <dev/netmap/netmap_kern.h>
69 #include <net/netmap_virt.h>
70 #include "netmap_mem2.h"
71 
72 #ifdef _WIN32_USE_SMALL_GENERIC_DEVICES_MEMORY
73 #define NETMAP_BUF_MAX_NUM  8*4096      /* if too big takes too much time to allocate */
74 #else
75 #define NETMAP_BUF_MAX_NUM 20*4096*2	/* large machine */
76 #endif
77 
78 #define NETMAP_POOL_MAX_NAMSZ	32
79 
80 
81 enum {
82 	NETMAP_IF_POOL   = 0,
83 	NETMAP_RING_POOL,
84 	NETMAP_BUF_POOL,
85 	NETMAP_POOLS_NR
86 };
87 
88 
89 struct netmap_obj_params {
90 	u_int size;
91 	u_int num;
92 
93 	u_int last_size;
94 	u_int last_num;
95 };
96 
97 struct netmap_obj_pool {
98 	char name[NETMAP_POOL_MAX_NAMSZ];	/* name of the allocator */
99 
100 	/* ---------------------------------------------------*/
101 	/* these are only meaningful if the pool is finalized */
102 	/* (see 'finalized' field in netmap_mem_d)            */
103 	u_int objtotal;         /* actual total number of objects. */
104 	u_int memtotal;		/* actual total memory space */
105 	u_int numclusters;	/* actual number of clusters */
106 
107 	u_int objfree;          /* number of free objects. */
108 
109 	struct lut_entry *lut;  /* virt,phys addresses, objtotal entries */
110 	uint32_t *bitmap;       /* one bit per buffer, 1 means free */
111 	uint32_t *invalid_bitmap;/* one bit per buffer, 1 means invalid */
112 	uint32_t bitmap_slots;	/* number of uint32 entries in bitmap */
113 	int	alloc_done;	/* we have allocated the memory */
114 	/* ---------------------------------------------------*/
115 
116 	/* limits */
117 	u_int objminsize;	/* minimum object size */
118 	u_int objmaxsize;	/* maximum object size */
119 	u_int nummin;		/* minimum number of objects */
120 	u_int nummax;		/* maximum number of objects */
121 
122 	/* these are changed only by config */
123 	u_int _objtotal;	/* total number of objects */
124 	u_int _objsize;		/* object size */
125 	u_int _clustsize;       /* cluster size */
126 	u_int _clustentries;    /* objects per cluster */
127 	u_int _numclusters;	/* number of clusters */
128 
129 	/* requested values */
130 	u_int r_objtotal;
131 	u_int r_objsize;
132 };
133 
134 #define NMA_LOCK_T		NM_MTX_T
135 #define NMA_LOCK_INIT(n)	NM_MTX_INIT((n)->nm_mtx)
136 #define NMA_LOCK_DESTROY(n)	NM_MTX_DESTROY((n)->nm_mtx)
137 #define NMA_LOCK(n)		NM_MTX_LOCK((n)->nm_mtx)
138 #define NMA_SPINLOCK(n)         NM_MTX_SPINLOCK((n)->nm_mtx)
139 #define NMA_UNLOCK(n)		NM_MTX_UNLOCK((n)->nm_mtx)
140 
141 struct netmap_mem_ops {
142 	int (*nmd_get_lut)(struct netmap_mem_d *, struct netmap_lut*);
143 	int  (*nmd_get_info)(struct netmap_mem_d *, uint64_t *size,
144 			u_int *memflags, uint16_t *id);
145 
146 	vm_paddr_t (*nmd_ofstophys)(struct netmap_mem_d *, vm_ooffset_t);
147 	int (*nmd_config)(struct netmap_mem_d *);
148 	int (*nmd_finalize)(struct netmap_mem_d *);
149 	void (*nmd_deref)(struct netmap_mem_d *);
150 	ssize_t  (*nmd_if_offset)(struct netmap_mem_d *, const void *vaddr);
151 	void (*nmd_delete)(struct netmap_mem_d *);
152 
153 	struct netmap_if * (*nmd_if_new)(struct netmap_adapter *,
154 					 struct netmap_priv_d *);
155 	void (*nmd_if_delete)(struct netmap_adapter *, struct netmap_if *);
156 	int  (*nmd_rings_create)(struct netmap_adapter *);
157 	void (*nmd_rings_delete)(struct netmap_adapter *);
158 };
159 
160 struct netmap_mem_d {
161 	NMA_LOCK_T nm_mtx;  /* protect the allocator */
162 	u_int nm_totalsize; /* shorthand */
163 
164 	u_int flags;
165 #define NETMAP_MEM_FINALIZED	0x1	/* preallocation done */
166 #define NETMAP_MEM_HIDDEN	0x8	/* beeing prepared */
167 	int lasterr;		/* last error for curr config */
168 	int active;		/* active users */
169 	int refcount;
170 	/* the three allocators */
171 	struct netmap_obj_pool pools[NETMAP_POOLS_NR];
172 
173 	nm_memid_t nm_id;	/* allocator identifier */
174 	int nm_grp;	/* iommu groupd id */
175 
176 	/* list of all existing allocators, sorted by nm_id */
177 	struct netmap_mem_d *prev, *next;
178 
179 	struct netmap_mem_ops *ops;
180 
181 	struct netmap_obj_params params[NETMAP_POOLS_NR];
182 
183 #define NM_MEM_NAMESZ	16
184 	char name[NM_MEM_NAMESZ];
185 };
186 
187 int
188 netmap_mem_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut)
189 {
190 	int rv;
191 
192 	NMA_LOCK(nmd);
193 	rv = nmd->ops->nmd_get_lut(nmd, lut);
194 	NMA_UNLOCK(nmd);
195 
196 	return rv;
197 }
198 
199 int
200 netmap_mem_get_info(struct netmap_mem_d *nmd, uint64_t *size,
201 		u_int *memflags, nm_memid_t *memid)
202 {
203 	int rv;
204 
205 	NMA_LOCK(nmd);
206 	rv = nmd->ops->nmd_get_info(nmd, size, memflags, memid);
207 	NMA_UNLOCK(nmd);
208 
209 	return rv;
210 }
211 
212 vm_paddr_t
213 netmap_mem_ofstophys(struct netmap_mem_d *nmd, vm_ooffset_t off)
214 {
215 	vm_paddr_t pa;
216 
217 #if defined(__FreeBSD__)
218 	/* This function is called by netmap_dev_pager_fault(), which holds a
219 	 * non-sleepable lock since FreeBSD 12. Since we cannot sleep, we
220 	 * spin on the trylock. */
221 	NMA_SPINLOCK(nmd);
222 #else
223 	NMA_LOCK(nmd);
224 #endif
225 	pa = nmd->ops->nmd_ofstophys(nmd, off);
226 	NMA_UNLOCK(nmd);
227 
228 	return pa;
229 }
230 
231 static int
232 netmap_mem_config(struct netmap_mem_d *nmd)
233 {
234 	if (nmd->active) {
235 		/* already in use. Not fatal, but we
236 		 * cannot change the configuration
237 		 */
238 		return 0;
239 	}
240 
241 	return nmd->ops->nmd_config(nmd);
242 }
243 
244 ssize_t
245 netmap_mem_if_offset(struct netmap_mem_d *nmd, const void *off)
246 {
247 	ssize_t rv;
248 
249 	NMA_LOCK(nmd);
250 	rv = nmd->ops->nmd_if_offset(nmd, off);
251 	NMA_UNLOCK(nmd);
252 
253 	return rv;
254 }
255 
256 static void
257 netmap_mem_delete(struct netmap_mem_d *nmd)
258 {
259 	nmd->ops->nmd_delete(nmd);
260 }
261 
262 struct netmap_if *
263 netmap_mem_if_new(struct netmap_adapter *na, struct netmap_priv_d *priv)
264 {
265 	struct netmap_if *nifp;
266 	struct netmap_mem_d *nmd = na->nm_mem;
267 
268 	NMA_LOCK(nmd);
269 	nifp = nmd->ops->nmd_if_new(na, priv);
270 	NMA_UNLOCK(nmd);
271 
272 	return nifp;
273 }
274 
275 void
276 netmap_mem_if_delete(struct netmap_adapter *na, struct netmap_if *nif)
277 {
278 	struct netmap_mem_d *nmd = na->nm_mem;
279 
280 	NMA_LOCK(nmd);
281 	nmd->ops->nmd_if_delete(na, nif);
282 	NMA_UNLOCK(nmd);
283 }
284 
285 int
286 netmap_mem_rings_create(struct netmap_adapter *na)
287 {
288 	int rv;
289 	struct netmap_mem_d *nmd = na->nm_mem;
290 
291 	NMA_LOCK(nmd);
292 	rv = nmd->ops->nmd_rings_create(na);
293 	NMA_UNLOCK(nmd);
294 
295 	return rv;
296 }
297 
298 void
299 netmap_mem_rings_delete(struct netmap_adapter *na)
300 {
301 	struct netmap_mem_d *nmd = na->nm_mem;
302 
303 	NMA_LOCK(nmd);
304 	nmd->ops->nmd_rings_delete(na);
305 	NMA_UNLOCK(nmd);
306 }
307 
308 static int netmap_mem_map(struct netmap_obj_pool *, struct netmap_adapter *);
309 static int netmap_mem_unmap(struct netmap_obj_pool *, struct netmap_adapter *);
310 static int nm_mem_assign_group(struct netmap_mem_d *, struct device *);
311 static void nm_mem_release_id(struct netmap_mem_d *);
312 
313 nm_memid_t
314 netmap_mem_get_id(struct netmap_mem_d *nmd)
315 {
316 	return nmd->nm_id;
317 }
318 
319 #ifdef NM_DEBUG_MEM_PUTGET
320 #define NM_DBG_REFC(nmd, func, line)	\
321 	nm_prinf("%s:%d mem[%d] -> %d\n", func, line, (nmd)->nm_id, (nmd)->refcount);
322 #else
323 #define NM_DBG_REFC(nmd, func, line)
324 #endif
325 
326 /* circular list of all existing allocators */
327 static struct netmap_mem_d *netmap_last_mem_d = &nm_mem;
328 NM_MTX_T nm_mem_list_lock;
329 
330 struct netmap_mem_d *
331 __netmap_mem_get(struct netmap_mem_d *nmd, const char *func, int line)
332 {
333 	NM_MTX_LOCK(nm_mem_list_lock);
334 	nmd->refcount++;
335 	NM_DBG_REFC(nmd, func, line);
336 	NM_MTX_UNLOCK(nm_mem_list_lock);
337 	return nmd;
338 }
339 
340 void
341 __netmap_mem_put(struct netmap_mem_d *nmd, const char *func, int line)
342 {
343 	int last;
344 	NM_MTX_LOCK(nm_mem_list_lock);
345 	last = (--nmd->refcount == 0);
346 	if (last)
347 		nm_mem_release_id(nmd);
348 	NM_DBG_REFC(nmd, func, line);
349 	NM_MTX_UNLOCK(nm_mem_list_lock);
350 	if (last)
351 		netmap_mem_delete(nmd);
352 }
353 
354 int
355 netmap_mem_finalize(struct netmap_mem_d *nmd, struct netmap_adapter *na)
356 {
357 	int lasterr = 0;
358 	if (nm_mem_assign_group(nmd, na->pdev) < 0) {
359 		return ENOMEM;
360 	}
361 
362 	NMA_LOCK(nmd);
363 
364 	if (netmap_mem_config(nmd))
365 		goto out;
366 
367 	nmd->active++;
368 
369 	nmd->lasterr = nmd->ops->nmd_finalize(nmd);
370 
371 	if (!nmd->lasterr && na->pdev) {
372 		nmd->lasterr = netmap_mem_map(&nmd->pools[NETMAP_BUF_POOL], na);
373 	}
374 
375 out:
376 	lasterr = nmd->lasterr;
377 	NMA_UNLOCK(nmd);
378 
379 	if (lasterr)
380 		netmap_mem_deref(nmd, na);
381 
382 	return lasterr;
383 }
384 
385 static int
386 nm_isset(uint32_t *bitmap, u_int i)
387 {
388 	return bitmap[ (i>>5) ] & ( 1U << (i & 31U) );
389 }
390 
391 
392 static int
393 netmap_init_obj_allocator_bitmap(struct netmap_obj_pool *p)
394 {
395 	u_int n, j;
396 
397 	if (p->bitmap == NULL) {
398 		/* Allocate the bitmap */
399 		n = (p->objtotal + 31) / 32;
400 		p->bitmap = nm_os_malloc(sizeof(uint32_t) * n);
401 		if (p->bitmap == NULL) {
402 			D("Unable to create bitmap (%d entries) for allocator '%s'", (int)n,
403 			    p->name);
404 			return ENOMEM;
405 		}
406 		p->bitmap_slots = n;
407 	} else {
408 		memset(p->bitmap, 0, p->bitmap_slots);
409 	}
410 
411 	p->objfree = 0;
412 	/*
413 	 * Set all the bits in the bitmap that have
414 	 * corresponding buffers to 1 to indicate they are
415 	 * free.
416 	 */
417 	for (j = 0; j < p->objtotal; j++) {
418 		if (p->invalid_bitmap && nm_isset(p->invalid_bitmap, j)) {
419 			D("skipping %s %d", p->name, j);
420 			continue;
421 		}
422 		p->bitmap[ (j>>5) ] |=  ( 1U << (j & 31U) );
423 		p->objfree++;
424 	}
425 
426 	ND("%s free %u", p->name, p->objfree);
427 	if (p->objfree == 0)
428 		return ENOMEM;
429 
430 	return 0;
431 }
432 
433 static int
434 netmap_mem_init_bitmaps(struct netmap_mem_d *nmd)
435 {
436 	int i, error = 0;
437 
438 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
439 		struct netmap_obj_pool *p = &nmd->pools[i];
440 
441 		error = netmap_init_obj_allocator_bitmap(p);
442 		if (error)
443 			return error;
444 	}
445 
446 	/*
447 	 * buffers 0 and 1 are reserved
448 	 */
449 	if (nmd->pools[NETMAP_BUF_POOL].objfree < 2) {
450 		return ENOMEM;
451 	}
452 
453 	nmd->pools[NETMAP_BUF_POOL].objfree -= 2;
454 	if (nmd->pools[NETMAP_BUF_POOL].bitmap) {
455 		/* XXX This check is a workaround that prevents a
456 		 * NULL pointer crash which currently happens only
457 		 * with ptnetmap guests.
458 		 * Removed shared-info --> is the bug still there? */
459 		nmd->pools[NETMAP_BUF_POOL].bitmap[0] = ~3U;
460 	}
461 	return 0;
462 }
463 
464 int
465 netmap_mem_deref(struct netmap_mem_d *nmd, struct netmap_adapter *na)
466 {
467 	int last_user = 0;
468 	NMA_LOCK(nmd);
469 	if (na->active_fds <= 0)
470 		netmap_mem_unmap(&nmd->pools[NETMAP_BUF_POOL], na);
471 	if (nmd->active == 1) {
472 		last_user = 1;
473 		/*
474 		 * Reset the allocator when it falls out of use so that any
475 		 * pool resources leaked by unclean application exits are
476 		 * reclaimed.
477 		 */
478 		netmap_mem_init_bitmaps(nmd);
479 	}
480 	nmd->ops->nmd_deref(nmd);
481 
482 	nmd->active--;
483 	if (!nmd->active)
484 		nmd->nm_grp = -1;
485 
486 	NMA_UNLOCK(nmd);
487 	return last_user;
488 }
489 
490 
491 /* accessor functions */
492 static int
493 netmap_mem2_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut)
494 {
495 	lut->lut = nmd->pools[NETMAP_BUF_POOL].lut;
496 #ifdef __FreeBSD__
497 	lut->plut = lut->lut;
498 #endif
499 	lut->objtotal = nmd->pools[NETMAP_BUF_POOL].objtotal;
500 	lut->objsize = nmd->pools[NETMAP_BUF_POOL]._objsize;
501 
502 	return 0;
503 }
504 
505 static struct netmap_obj_params netmap_min_priv_params[NETMAP_POOLS_NR] = {
506 	[NETMAP_IF_POOL] = {
507 		.size = 1024,
508 		.num  = 2,
509 	},
510 	[NETMAP_RING_POOL] = {
511 		.size = 5*PAGE_SIZE,
512 		.num  = 4,
513 	},
514 	[NETMAP_BUF_POOL] = {
515 		.size = 2048,
516 		.num  = 4098,
517 	},
518 };
519 
520 
521 /*
522  * nm_mem is the memory allocator used for all physical interfaces
523  * running in netmap mode.
524  * Virtual (VALE) ports will have each its own allocator.
525  */
526 extern struct netmap_mem_ops netmap_mem_global_ops; /* forward */
527 struct netmap_mem_d nm_mem = {	/* Our memory allocator. */
528 	.pools = {
529 		[NETMAP_IF_POOL] = {
530 			.name 	= "netmap_if",
531 			.objminsize = sizeof(struct netmap_if),
532 			.objmaxsize = 4096,
533 			.nummin     = 10,	/* don't be stingy */
534 			.nummax	    = 10000,	/* XXX very large */
535 		},
536 		[NETMAP_RING_POOL] = {
537 			.name 	= "netmap_ring",
538 			.objminsize = sizeof(struct netmap_ring),
539 			.objmaxsize = 32*PAGE_SIZE,
540 			.nummin     = 2,
541 			.nummax	    = 1024,
542 		},
543 		[NETMAP_BUF_POOL] = {
544 			.name	= "netmap_buf",
545 			.objminsize = 64,
546 			.objmaxsize = 65536,
547 			.nummin     = 4,
548 			.nummax	    = 1000000, /* one million! */
549 		},
550 	},
551 
552 	.params = {
553 		[NETMAP_IF_POOL] = {
554 			.size = 1024,
555 			.num  = 100,
556 		},
557 		[NETMAP_RING_POOL] = {
558 			.size = 9*PAGE_SIZE,
559 			.num  = 200,
560 		},
561 		[NETMAP_BUF_POOL] = {
562 			.size = 2048,
563 			.num  = NETMAP_BUF_MAX_NUM,
564 		},
565 	},
566 
567 	.nm_id = 1,
568 	.nm_grp = -1,
569 
570 	.prev = &nm_mem,
571 	.next = &nm_mem,
572 
573 	.ops = &netmap_mem_global_ops,
574 
575 	.name = "1"
576 };
577 
578 
579 /* blueprint for the private memory allocators */
580 /* XXX clang is not happy about using name as a print format */
581 static const struct netmap_mem_d nm_blueprint = {
582 	.pools = {
583 		[NETMAP_IF_POOL] = {
584 			.name 	= "%s_if",
585 			.objminsize = sizeof(struct netmap_if),
586 			.objmaxsize = 4096,
587 			.nummin     = 1,
588 			.nummax	    = 100,
589 		},
590 		[NETMAP_RING_POOL] = {
591 			.name 	= "%s_ring",
592 			.objminsize = sizeof(struct netmap_ring),
593 			.objmaxsize = 32*PAGE_SIZE,
594 			.nummin     = 2,
595 			.nummax	    = 1024,
596 		},
597 		[NETMAP_BUF_POOL] = {
598 			.name	= "%s_buf",
599 			.objminsize = 64,
600 			.objmaxsize = 65536,
601 			.nummin     = 4,
602 			.nummax	    = 1000000, /* one million! */
603 		},
604 	},
605 
606 	.nm_grp = -1,
607 
608 	.flags = NETMAP_MEM_PRIVATE,
609 
610 	.ops = &netmap_mem_global_ops,
611 };
612 
613 /* memory allocator related sysctls */
614 
615 #define STRINGIFY(x) #x
616 
617 
618 #define DECLARE_SYSCTLS(id, name) \
619 	SYSBEGIN(mem2_ ## name); \
620 	SYSCTL_INT(_dev_netmap, OID_AUTO, name##_size, \
621 	    CTLFLAG_RW, &nm_mem.params[id].size, 0, "Requested size of netmap " STRINGIFY(name) "s"); \
622 	SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_size, \
623 	    CTLFLAG_RD, &nm_mem.pools[id]._objsize, 0, "Current size of netmap " STRINGIFY(name) "s"); \
624 	SYSCTL_INT(_dev_netmap, OID_AUTO, name##_num, \
625 	    CTLFLAG_RW, &nm_mem.params[id].num, 0, "Requested number of netmap " STRINGIFY(name) "s"); \
626 	SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_num, \
627 	    CTLFLAG_RD, &nm_mem.pools[id].objtotal, 0, "Current number of netmap " STRINGIFY(name) "s"); \
628 	SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_size, \
629 	    CTLFLAG_RW, &netmap_min_priv_params[id].size, 0, \
630 	    "Default size of private netmap " STRINGIFY(name) "s"); \
631 	SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_num, \
632 	    CTLFLAG_RW, &netmap_min_priv_params[id].num, 0, \
633 	    "Default number of private netmap " STRINGIFY(name) "s");	\
634 	SYSEND
635 
636 SYSCTL_DECL(_dev_netmap);
637 DECLARE_SYSCTLS(NETMAP_IF_POOL, if);
638 DECLARE_SYSCTLS(NETMAP_RING_POOL, ring);
639 DECLARE_SYSCTLS(NETMAP_BUF_POOL, buf);
640 
641 /* call with nm_mem_list_lock held */
642 static int
643 nm_mem_assign_id_locked(struct netmap_mem_d *nmd)
644 {
645 	nm_memid_t id;
646 	struct netmap_mem_d *scan = netmap_last_mem_d;
647 	int error = ENOMEM;
648 
649 	do {
650 		/* we rely on unsigned wrap around */
651 		id = scan->nm_id + 1;
652 		if (id == 0) /* reserve 0 as error value */
653 			id = 1;
654 		scan = scan->next;
655 		if (id != scan->nm_id) {
656 			nmd->nm_id = id;
657 			nmd->prev = scan->prev;
658 			nmd->next = scan;
659 			scan->prev->next = nmd;
660 			scan->prev = nmd;
661 			netmap_last_mem_d = nmd;
662 			nmd->refcount = 1;
663 			NM_DBG_REFC(nmd, __FUNCTION__, __LINE__);
664 			error = 0;
665 			break;
666 		}
667 	} while (scan != netmap_last_mem_d);
668 
669 	return error;
670 }
671 
672 /* call with nm_mem_list_lock *not* held */
673 static int
674 nm_mem_assign_id(struct netmap_mem_d *nmd)
675 {
676         int ret;
677 
678 	NM_MTX_LOCK(nm_mem_list_lock);
679         ret = nm_mem_assign_id_locked(nmd);
680 	NM_MTX_UNLOCK(nm_mem_list_lock);
681 
682 	return ret;
683 }
684 
685 /* call with nm_mem_list_lock held */
686 static void
687 nm_mem_release_id(struct netmap_mem_d *nmd)
688 {
689 	nmd->prev->next = nmd->next;
690 	nmd->next->prev = nmd->prev;
691 
692 	if (netmap_last_mem_d == nmd)
693 		netmap_last_mem_d = nmd->prev;
694 
695 	nmd->prev = nmd->next = NULL;
696 }
697 
698 struct netmap_mem_d *
699 netmap_mem_find(nm_memid_t id)
700 {
701 	struct netmap_mem_d *nmd;
702 
703 	NM_MTX_LOCK(nm_mem_list_lock);
704 	nmd = netmap_last_mem_d;
705 	do {
706 		if (!(nmd->flags & NETMAP_MEM_HIDDEN) && nmd->nm_id == id) {
707 			nmd->refcount++;
708 			NM_DBG_REFC(nmd, __FUNCTION__, __LINE__);
709 			NM_MTX_UNLOCK(nm_mem_list_lock);
710 			return nmd;
711 		}
712 		nmd = nmd->next;
713 	} while (nmd != netmap_last_mem_d);
714 	NM_MTX_UNLOCK(nm_mem_list_lock);
715 	return NULL;
716 }
717 
718 static int
719 nm_mem_assign_group(struct netmap_mem_d *nmd, struct device *dev)
720 {
721 	int err = 0, id;
722 	id = nm_iommu_group_id(dev);
723 	if (netmap_verbose)
724 		D("iommu_group %d", id);
725 
726 	NMA_LOCK(nmd);
727 
728 	if (nmd->nm_grp < 0)
729 		nmd->nm_grp = id;
730 
731 	if (nmd->nm_grp != id)
732 		nmd->lasterr = err = ENOMEM;
733 
734 	NMA_UNLOCK(nmd);
735 	return err;
736 }
737 
738 static struct lut_entry *
739 nm_alloc_lut(u_int nobj)
740 {
741 	size_t n = sizeof(struct lut_entry) * nobj;
742 	struct lut_entry *lut;
743 #ifdef linux
744 	lut = vmalloc(n);
745 #else
746 	lut = nm_os_malloc(n);
747 #endif
748 	return lut;
749 }
750 
751 static void
752 nm_free_lut(struct lut_entry *lut, u_int objtotal)
753 {
754 	bzero(lut, sizeof(struct lut_entry) * objtotal);
755 #ifdef linux
756 	vfree(lut);
757 #else
758 	nm_os_free(lut);
759 #endif
760 }
761 
762 #if defined(linux) || defined(_WIN32)
763 static struct plut_entry *
764 nm_alloc_plut(u_int nobj)
765 {
766 	size_t n = sizeof(struct plut_entry) * nobj;
767 	struct plut_entry *lut;
768 	lut = vmalloc(n);
769 	return lut;
770 }
771 
772 static void
773 nm_free_plut(struct plut_entry * lut)
774 {
775 	vfree(lut);
776 }
777 #endif /* linux or _WIN32 */
778 
779 
780 /*
781  * First, find the allocator that contains the requested offset,
782  * then locate the cluster through a lookup table.
783  */
784 static vm_paddr_t
785 netmap_mem2_ofstophys(struct netmap_mem_d* nmd, vm_ooffset_t offset)
786 {
787 	int i;
788 	vm_ooffset_t o = offset;
789 	vm_paddr_t pa;
790 	struct netmap_obj_pool *p;
791 
792 	p = nmd->pools;
793 
794 	for (i = 0; i < NETMAP_POOLS_NR; offset -= p[i].memtotal, i++) {
795 		if (offset >= p[i].memtotal)
796 			continue;
797 		// now lookup the cluster's address
798 #ifndef _WIN32
799 		pa = vtophys(p[i].lut[offset / p[i]._objsize].vaddr) +
800 			offset % p[i]._objsize;
801 #else
802 		pa = vtophys(p[i].lut[offset / p[i]._objsize].vaddr);
803 		pa.QuadPart += offset % p[i]._objsize;
804 #endif
805 		return pa;
806 	}
807 	/* this is only in case of errors */
808 	D("invalid ofs 0x%x out of 0x%x 0x%x 0x%x", (u_int)o,
809 		p[NETMAP_IF_POOL].memtotal,
810 		p[NETMAP_IF_POOL].memtotal
811 			+ p[NETMAP_RING_POOL].memtotal,
812 		p[NETMAP_IF_POOL].memtotal
813 			+ p[NETMAP_RING_POOL].memtotal
814 			+ p[NETMAP_BUF_POOL].memtotal);
815 #ifndef _WIN32
816 	return 0; /* bad address */
817 #else
818 	vm_paddr_t res;
819 	res.QuadPart = 0;
820 	return res;
821 #endif
822 }
823 
824 #ifdef _WIN32
825 
826 /*
827  * win32_build_virtual_memory_for_userspace
828  *
829  * This function get all the object making part of the pools and maps
830  * a contiguous virtual memory space for the userspace
831  * It works this way
832  * 1 - allocate a Memory Descriptor List wide as the sum
833  *		of the memory needed for the pools
834  * 2 - cycle all the objects in every pool and for every object do
835  *
836  *		2a - cycle all the objects in every pool, get the list
837  *				of the physical address descriptors
838  *		2b - calculate the offset in the array of pages desciptor in the
839  *				main MDL
840  *		2c - copy the descriptors of the object in the main MDL
841  *
842  * 3 - return the resulting MDL that needs to be mapped in userland
843  *
844  * In this way we will have an MDL that describes all the memory for the
845  * objects in a single object
846 */
847 
848 PMDL
849 win32_build_user_vm_map(struct netmap_mem_d* nmd)
850 {
851 	u_int memflags, ofs = 0;
852 	PMDL mainMdl, tempMdl;
853 	uint64_t memsize;
854 	int i, j;
855 
856 	if (netmap_mem_get_info(nmd, &memsize, &memflags, NULL)) {
857 		D("memory not finalised yet");
858 		return NULL;
859 	}
860 
861 	mainMdl = IoAllocateMdl(NULL, memsize, FALSE, FALSE, NULL);
862 	if (mainMdl == NULL) {
863 		D("failed to allocate mdl");
864 		return NULL;
865 	}
866 
867 	NMA_LOCK(nmd);
868 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
869 		struct netmap_obj_pool *p = &nmd->pools[i];
870 		int clsz = p->_clustsize;
871 		int clobjs = p->_clustentries; /* objects per cluster */
872 		int mdl_len = sizeof(PFN_NUMBER) * BYTES_TO_PAGES(clsz);
873 		PPFN_NUMBER pSrc, pDst;
874 
875 		/* each pool has a different cluster size so we need to reallocate */
876 		tempMdl = IoAllocateMdl(p->lut[0].vaddr, clsz, FALSE, FALSE, NULL);
877 		if (tempMdl == NULL) {
878 			NMA_UNLOCK(nmd);
879 			D("fail to allocate tempMdl");
880 			IoFreeMdl(mainMdl);
881 			return NULL;
882 		}
883 		pSrc = MmGetMdlPfnArray(tempMdl);
884 		/* create one entry per cluster, the lut[] has one entry per object */
885 		for (j = 0; j < p->numclusters; j++, ofs += clsz) {
886 			pDst = &MmGetMdlPfnArray(mainMdl)[BYTES_TO_PAGES(ofs)];
887 			MmInitializeMdl(tempMdl, p->lut[j*clobjs].vaddr, clsz);
888 			MmBuildMdlForNonPagedPool(tempMdl); /* compute physical page addresses */
889 			RtlCopyMemory(pDst, pSrc, mdl_len); /* copy the page descriptors */
890 			mainMdl->MdlFlags = tempMdl->MdlFlags; /* XXX what is in here ? */
891 		}
892 		IoFreeMdl(tempMdl);
893 	}
894 	NMA_UNLOCK(nmd);
895 	return mainMdl;
896 }
897 
898 #endif /* _WIN32 */
899 
900 /*
901  * helper function for OS-specific mmap routines (currently only windows).
902  * Given an nmd and a pool index, returns the cluster size and number of clusters.
903  * Returns 0 if memory is finalised and the pool is valid, otherwise 1.
904  * It should be called under NMA_LOCK(nmd) otherwise the underlying info can change.
905  */
906 
907 int
908 netmap_mem2_get_pool_info(struct netmap_mem_d* nmd, u_int pool, u_int *clustsize, u_int *numclusters)
909 {
910 	if (!nmd || !clustsize || !numclusters || pool >= NETMAP_POOLS_NR)
911 		return 1; /* invalid arguments */
912 	// NMA_LOCK_ASSERT(nmd);
913 	if (!(nmd->flags & NETMAP_MEM_FINALIZED)) {
914 		*clustsize = *numclusters = 0;
915 		return 1; /* not ready yet */
916 	}
917 	*clustsize = nmd->pools[pool]._clustsize;
918 	*numclusters = nmd->pools[pool].numclusters;
919 	return 0; /* success */
920 }
921 
922 static int
923 netmap_mem2_get_info(struct netmap_mem_d* nmd, uint64_t* size,
924 			u_int *memflags, nm_memid_t *id)
925 {
926 	int error = 0;
927 	error = netmap_mem_config(nmd);
928 	if (error)
929 		goto out;
930 	if (size) {
931 		if (nmd->flags & NETMAP_MEM_FINALIZED) {
932 			*size = nmd->nm_totalsize;
933 		} else {
934 			int i;
935 			*size = 0;
936 			for (i = 0; i < NETMAP_POOLS_NR; i++) {
937 				struct netmap_obj_pool *p = nmd->pools + i;
938 				*size += (p->_numclusters * p->_clustsize);
939 			}
940 		}
941 	}
942 	if (memflags)
943 		*memflags = nmd->flags;
944 	if (id)
945 		*id = nmd->nm_id;
946 out:
947 	return error;
948 }
949 
950 /*
951  * we store objects by kernel address, need to find the offset
952  * within the pool to export the value to userspace.
953  * Algorithm: scan until we find the cluster, then add the
954  * actual offset in the cluster
955  */
956 static ssize_t
957 netmap_obj_offset(struct netmap_obj_pool *p, const void *vaddr)
958 {
959 	int i, k = p->_clustentries, n = p->objtotal;
960 	ssize_t ofs = 0;
961 
962 	for (i = 0; i < n; i += k, ofs += p->_clustsize) {
963 		const char *base = p->lut[i].vaddr;
964 		ssize_t relofs = (const char *) vaddr - base;
965 
966 		if (relofs < 0 || relofs >= p->_clustsize)
967 			continue;
968 
969 		ofs = ofs + relofs;
970 		ND("%s: return offset %d (cluster %d) for pointer %p",
971 		    p->name, ofs, i, vaddr);
972 		return ofs;
973 	}
974 	D("address %p is not contained inside any cluster (%s)",
975 	    vaddr, p->name);
976 	return 0; /* An error occurred */
977 }
978 
979 /* Helper functions which convert virtual addresses to offsets */
980 #define netmap_if_offset(n, v)					\
981 	netmap_obj_offset(&(n)->pools[NETMAP_IF_POOL], (v))
982 
983 #define netmap_ring_offset(n, v)				\
984     ((n)->pools[NETMAP_IF_POOL].memtotal + 			\
985 	netmap_obj_offset(&(n)->pools[NETMAP_RING_POOL], (v)))
986 
987 static ssize_t
988 netmap_mem2_if_offset(struct netmap_mem_d *nmd, const void *addr)
989 {
990 	return netmap_if_offset(nmd, addr);
991 }
992 
993 /*
994  * report the index, and use start position as a hint,
995  * otherwise buffer allocation becomes terribly expensive.
996  */
997 static void *
998 netmap_obj_malloc(struct netmap_obj_pool *p, u_int len, uint32_t *start, uint32_t *index)
999 {
1000 	uint32_t i = 0;			/* index in the bitmap */
1001 	uint32_t mask, j = 0;		/* slot counter */
1002 	void *vaddr = NULL;
1003 
1004 	if (len > p->_objsize) {
1005 		D("%s request size %d too large", p->name, len);
1006 		return NULL;
1007 	}
1008 
1009 	if (p->objfree == 0) {
1010 		D("no more %s objects", p->name);
1011 		return NULL;
1012 	}
1013 	if (start)
1014 		i = *start;
1015 
1016 	/* termination is guaranteed by p->free, but better check bounds on i */
1017 	while (vaddr == NULL && i < p->bitmap_slots)  {
1018 		uint32_t cur = p->bitmap[i];
1019 		if (cur == 0) { /* bitmask is fully used */
1020 			i++;
1021 			continue;
1022 		}
1023 		/* locate a slot */
1024 		for (j = 0, mask = 1; (cur & mask) == 0; j++, mask <<= 1)
1025 			;
1026 
1027 		p->bitmap[i] &= ~mask; /* mark object as in use */
1028 		p->objfree--;
1029 
1030 		vaddr = p->lut[i * 32 + j].vaddr;
1031 		if (index)
1032 			*index = i * 32 + j;
1033 	}
1034 	ND("%s allocator: allocated object @ [%d][%d]: vaddr %p",p->name, i, j, vaddr);
1035 
1036 	if (start)
1037 		*start = i;
1038 	return vaddr;
1039 }
1040 
1041 
1042 /*
1043  * free by index, not by address.
1044  * XXX should we also cleanup the content ?
1045  */
1046 static int
1047 netmap_obj_free(struct netmap_obj_pool *p, uint32_t j)
1048 {
1049 	uint32_t *ptr, mask;
1050 
1051 	if (j >= p->objtotal) {
1052 		D("invalid index %u, max %u", j, p->objtotal);
1053 		return 1;
1054 	}
1055 	ptr = &p->bitmap[j / 32];
1056 	mask = (1 << (j % 32));
1057 	if (*ptr & mask) {
1058 		D("ouch, double free on buffer %d", j);
1059 		return 1;
1060 	} else {
1061 		*ptr |= mask;
1062 		p->objfree++;
1063 		return 0;
1064 	}
1065 }
1066 
1067 /*
1068  * free by address. This is slow but is only used for a few
1069  * objects (rings, nifp)
1070  */
1071 static void
1072 netmap_obj_free_va(struct netmap_obj_pool *p, void *vaddr)
1073 {
1074 	u_int i, j, n = p->numclusters;
1075 
1076 	for (i = 0, j = 0; i < n; i++, j += p->_clustentries) {
1077 		void *base = p->lut[i * p->_clustentries].vaddr;
1078 		ssize_t relofs = (ssize_t) vaddr - (ssize_t) base;
1079 
1080 		/* Given address, is out of the scope of the current cluster.*/
1081 		if (base == NULL || vaddr < base || relofs >= p->_clustsize)
1082 			continue;
1083 
1084 		j = j + relofs / p->_objsize;
1085 		/* KASSERT(j != 0, ("Cannot free object 0")); */
1086 		netmap_obj_free(p, j);
1087 		return;
1088 	}
1089 	D("address %p is not contained inside any cluster (%s)",
1090 	    vaddr, p->name);
1091 }
1092 
1093 unsigned
1094 netmap_mem_bufsize(struct netmap_mem_d *nmd)
1095 {
1096 	return nmd->pools[NETMAP_BUF_POOL]._objsize;
1097 }
1098 
1099 #define netmap_if_malloc(n, len)	netmap_obj_malloc(&(n)->pools[NETMAP_IF_POOL], len, NULL, NULL)
1100 #define netmap_if_free(n, v)		netmap_obj_free_va(&(n)->pools[NETMAP_IF_POOL], (v))
1101 #define netmap_ring_malloc(n, len)	netmap_obj_malloc(&(n)->pools[NETMAP_RING_POOL], len, NULL, NULL)
1102 #define netmap_ring_free(n, v)		netmap_obj_free_va(&(n)->pools[NETMAP_RING_POOL], (v))
1103 #define netmap_buf_malloc(n, _pos, _index)			\
1104 	netmap_obj_malloc(&(n)->pools[NETMAP_BUF_POOL], netmap_mem_bufsize(n), _pos, _index)
1105 
1106 
1107 #if 0 /* currently unused */
1108 /* Return the index associated to the given packet buffer */
1109 #define netmap_buf_index(n, v)						\
1110     (netmap_obj_offset(&(n)->pools[NETMAP_BUF_POOL], (v)) / NETMAP_BDG_BUF_SIZE(n))
1111 #endif
1112 
1113 /*
1114  * allocate extra buffers in a linked list.
1115  * returns the actual number.
1116  */
1117 uint32_t
1118 netmap_extra_alloc(struct netmap_adapter *na, uint32_t *head, uint32_t n)
1119 {
1120 	struct netmap_mem_d *nmd = na->nm_mem;
1121 	uint32_t i, pos = 0; /* opaque, scan position in the bitmap */
1122 
1123 	NMA_LOCK(nmd);
1124 
1125 	*head = 0;	/* default, 'null' index ie empty list */
1126 	for (i = 0 ; i < n; i++) {
1127 		uint32_t cur = *head;	/* save current head */
1128 		uint32_t *p = netmap_buf_malloc(nmd, &pos, head);
1129 		if (p == NULL) {
1130 			D("no more buffers after %d of %d", i, n);
1131 			*head = cur; /* restore */
1132 			break;
1133 		}
1134 		ND(5, "allocate buffer %d -> %d", *head, cur);
1135 		*p = cur; /* link to previous head */
1136 	}
1137 
1138 	NMA_UNLOCK(nmd);
1139 
1140 	return i;
1141 }
1142 
1143 static void
1144 netmap_extra_free(struct netmap_adapter *na, uint32_t head)
1145 {
1146         struct lut_entry *lut = na->na_lut.lut;
1147 	struct netmap_mem_d *nmd = na->nm_mem;
1148 	struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
1149 	uint32_t i, cur, *buf;
1150 
1151 	ND("freeing the extra list");
1152 	for (i = 0; head >=2 && head < p->objtotal; i++) {
1153 		cur = head;
1154 		buf = lut[head].vaddr;
1155 		head = *buf;
1156 		*buf = 0;
1157 		if (netmap_obj_free(p, cur))
1158 			break;
1159 	}
1160 	if (head != 0)
1161 		D("breaking with head %d", head);
1162 	if (netmap_verbose)
1163 		D("freed %d buffers", i);
1164 }
1165 
1166 
1167 /* Return nonzero on error */
1168 static int
1169 netmap_new_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n)
1170 {
1171 	struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
1172 	u_int i = 0;	/* slot counter */
1173 	uint32_t pos = 0;	/* slot in p->bitmap */
1174 	uint32_t index = 0;	/* buffer index */
1175 
1176 	for (i = 0; i < n; i++) {
1177 		void *vaddr = netmap_buf_malloc(nmd, &pos, &index);
1178 		if (vaddr == NULL) {
1179 			D("no more buffers after %d of %d", i, n);
1180 			goto cleanup;
1181 		}
1182 		slot[i].buf_idx = index;
1183 		slot[i].len = p->_objsize;
1184 		slot[i].flags = 0;
1185 		slot[i].ptr = 0;
1186 	}
1187 
1188 	ND("%s: allocated %d buffers, %d available, first at %d", p->name, n, p->objfree, pos);
1189 	return (0);
1190 
1191 cleanup:
1192 	while (i > 0) {
1193 		i--;
1194 		netmap_obj_free(p, slot[i].buf_idx);
1195 	}
1196 	bzero(slot, n * sizeof(slot[0]));
1197 	return (ENOMEM);
1198 }
1199 
1200 static void
1201 netmap_mem_set_ring(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n, uint32_t index)
1202 {
1203 	struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
1204 	u_int i;
1205 
1206 	for (i = 0; i < n; i++) {
1207 		slot[i].buf_idx = index;
1208 		slot[i].len = p->_objsize;
1209 		slot[i].flags = 0;
1210 	}
1211 }
1212 
1213 
1214 static void
1215 netmap_free_buf(struct netmap_mem_d *nmd, uint32_t i)
1216 {
1217 	struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL];
1218 
1219 	if (i < 2 || i >= p->objtotal) {
1220 		D("Cannot free buf#%d: should be in [2, %d[", i, p->objtotal);
1221 		return;
1222 	}
1223 	netmap_obj_free(p, i);
1224 }
1225 
1226 
1227 static void
1228 netmap_free_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n)
1229 {
1230 	u_int i;
1231 
1232 	for (i = 0; i < n; i++) {
1233 		if (slot[i].buf_idx > 1)
1234 			netmap_free_buf(nmd, slot[i].buf_idx);
1235 	}
1236 	ND("%s: released some buffers, available: %u",
1237 			p->name, p->objfree);
1238 }
1239 
1240 static void
1241 netmap_reset_obj_allocator(struct netmap_obj_pool *p)
1242 {
1243 
1244 	if (p == NULL)
1245 		return;
1246 	if (p->bitmap)
1247 		nm_os_free(p->bitmap);
1248 	p->bitmap = NULL;
1249 	if (p->invalid_bitmap)
1250 		nm_os_free(p->invalid_bitmap);
1251 	p->invalid_bitmap = NULL;
1252 	if (!p->alloc_done) {
1253 		/* allocation was done by somebody else.
1254 		 * Let them clean up after themselves.
1255 		 */
1256 		return;
1257 	}
1258 	if (p->lut) {
1259 		u_int i;
1260 
1261 		/*
1262 		 * Free each cluster allocated in
1263 		 * netmap_finalize_obj_allocator().  The cluster start
1264 		 * addresses are stored at multiples of p->_clusterentries
1265 		 * in the lut.
1266 		 */
1267 		for (i = 0; i < p->objtotal; i += p->_clustentries) {
1268 			contigfree(p->lut[i].vaddr, p->_clustsize, M_NETMAP);
1269 		}
1270 		nm_free_lut(p->lut, p->objtotal);
1271 	}
1272 	p->lut = NULL;
1273 	p->objtotal = 0;
1274 	p->memtotal = 0;
1275 	p->numclusters = 0;
1276 	p->objfree = 0;
1277 	p->alloc_done = 0;
1278 }
1279 
1280 /*
1281  * Free all resources related to an allocator.
1282  */
1283 static void
1284 netmap_destroy_obj_allocator(struct netmap_obj_pool *p)
1285 {
1286 	if (p == NULL)
1287 		return;
1288 	netmap_reset_obj_allocator(p);
1289 }
1290 
1291 /*
1292  * We receive a request for objtotal objects, of size objsize each.
1293  * Internally we may round up both numbers, as we allocate objects
1294  * in small clusters multiple of the page size.
1295  * We need to keep track of objtotal and clustentries,
1296  * as they are needed when freeing memory.
1297  *
1298  * XXX note -- userspace needs the buffers to be contiguous,
1299  *	so we cannot afford gaps at the end of a cluster.
1300  */
1301 
1302 
1303 /* call with NMA_LOCK held */
1304 static int
1305 netmap_config_obj_allocator(struct netmap_obj_pool *p, u_int objtotal, u_int objsize)
1306 {
1307 	int i;
1308 	u_int clustsize;	/* the cluster size, multiple of page size */
1309 	u_int clustentries;	/* how many objects per entry */
1310 
1311 	/* we store the current request, so we can
1312 	 * detect configuration changes later */
1313 	p->r_objtotal = objtotal;
1314 	p->r_objsize = objsize;
1315 
1316 #define MAX_CLUSTSIZE	(1<<22)		// 4 MB
1317 #define LINE_ROUND	NM_CACHE_ALIGN	// 64
1318 	if (objsize >= MAX_CLUSTSIZE) {
1319 		/* we could do it but there is no point */
1320 		D("unsupported allocation for %d bytes", objsize);
1321 		return EINVAL;
1322 	}
1323 	/* make sure objsize is a multiple of LINE_ROUND */
1324 	i = (objsize & (LINE_ROUND - 1));
1325 	if (i) {
1326 		D("XXX aligning object by %d bytes", LINE_ROUND - i);
1327 		objsize += LINE_ROUND - i;
1328 	}
1329 	if (objsize < p->objminsize || objsize > p->objmaxsize) {
1330 		D("requested objsize %d out of range [%d, %d]",
1331 			objsize, p->objminsize, p->objmaxsize);
1332 		return EINVAL;
1333 	}
1334 	if (objtotal < p->nummin || objtotal > p->nummax) {
1335 		D("requested objtotal %d out of range [%d, %d]",
1336 			objtotal, p->nummin, p->nummax);
1337 		return EINVAL;
1338 	}
1339 	/*
1340 	 * Compute number of objects using a brute-force approach:
1341 	 * given a max cluster size,
1342 	 * we try to fill it with objects keeping track of the
1343 	 * wasted space to the next page boundary.
1344 	 */
1345 	for (clustentries = 0, i = 1;; i++) {
1346 		u_int delta, used = i * objsize;
1347 		if (used > MAX_CLUSTSIZE)
1348 			break;
1349 		delta = used % PAGE_SIZE;
1350 		if (delta == 0) { // exact solution
1351 			clustentries = i;
1352 			break;
1353 		}
1354 	}
1355 	/* exact solution not found */
1356 	if (clustentries == 0) {
1357 		D("unsupported allocation for %d bytes", objsize);
1358 		return EINVAL;
1359 	}
1360 	/* compute clustsize */
1361 	clustsize = clustentries * objsize;
1362 	if (netmap_verbose)
1363 		D("objsize %d clustsize %d objects %d",
1364 			objsize, clustsize, clustentries);
1365 
1366 	/*
1367 	 * The number of clusters is n = ceil(objtotal/clustentries)
1368 	 * objtotal' = n * clustentries
1369 	 */
1370 	p->_clustentries = clustentries;
1371 	p->_clustsize = clustsize;
1372 	p->_numclusters = (objtotal + clustentries - 1) / clustentries;
1373 
1374 	/* actual values (may be larger than requested) */
1375 	p->_objsize = objsize;
1376 	p->_objtotal = p->_numclusters * clustentries;
1377 
1378 	return 0;
1379 }
1380 
1381 /* call with NMA_LOCK held */
1382 static int
1383 netmap_finalize_obj_allocator(struct netmap_obj_pool *p)
1384 {
1385 	int i; /* must be signed */
1386 	size_t n;
1387 
1388 	if (p->lut) {
1389 		/* if the lut is already there we assume that also all the
1390 		 * clusters have already been allocated, possibily by somebody
1391 		 * else (e.g., extmem). In the latter case, the alloc_done flag
1392 		 * will remain at zero, so that we will not attempt to
1393 		 * deallocate the clusters by ourselves in
1394 		 * netmap_reset_obj_allocator.
1395 		 */
1396 		return 0;
1397 	}
1398 
1399 	/* optimistically assume we have enough memory */
1400 	p->numclusters = p->_numclusters;
1401 	p->objtotal = p->_objtotal;
1402 	p->alloc_done = 1;
1403 
1404 	p->lut = nm_alloc_lut(p->objtotal);
1405 	if (p->lut == NULL) {
1406 		D("Unable to create lookup table for '%s'", p->name);
1407 		goto clean;
1408 	}
1409 
1410 	/*
1411 	 * Allocate clusters, init pointers
1412 	 */
1413 
1414 	n = p->_clustsize;
1415 	for (i = 0; i < (int)p->objtotal;) {
1416 		int lim = i + p->_clustentries;
1417 		char *clust;
1418 
1419 		/*
1420 		 * XXX Note, we only need contigmalloc() for buffers attached
1421 		 * to native interfaces. In all other cases (nifp, netmap rings
1422 		 * and even buffers for VALE ports or emulated interfaces) we
1423 		 * can live with standard malloc, because the hardware will not
1424 		 * access the pages directly.
1425 		 */
1426 		clust = contigmalloc(n, M_NETMAP, M_NOWAIT | M_ZERO,
1427 		    (size_t)0, -1UL, PAGE_SIZE, 0);
1428 		if (clust == NULL) {
1429 			/*
1430 			 * If we get here, there is a severe memory shortage,
1431 			 * so halve the allocated memory to reclaim some.
1432 			 */
1433 			D("Unable to create cluster at %d for '%s' allocator",
1434 			    i, p->name);
1435 			if (i < 2) /* nothing to halve */
1436 				goto out;
1437 			lim = i / 2;
1438 			for (i--; i >= lim; i--) {
1439 				if (i % p->_clustentries == 0 && p->lut[i].vaddr)
1440 					contigfree(p->lut[i].vaddr,
1441 						n, M_NETMAP);
1442 				p->lut[i].vaddr = NULL;
1443 			}
1444 		out:
1445 			p->objtotal = i;
1446 			/* we may have stopped in the middle of a cluster */
1447 			p->numclusters = (i + p->_clustentries - 1) / p->_clustentries;
1448 			break;
1449 		}
1450 		/*
1451 		 * Set lut state for all buffers in the current cluster.
1452 		 *
1453 		 * [i, lim) is the set of buffer indexes that cover the
1454 		 * current cluster.
1455 		 *
1456 		 * 'clust' is really the address of the current buffer in
1457 		 * the current cluster as we index through it with a stride
1458 		 * of p->_objsize.
1459 		 */
1460 		for (; i < lim; i++, clust += p->_objsize) {
1461 			p->lut[i].vaddr = clust;
1462 #if !defined(linux) && !defined(_WIN32)
1463 			p->lut[i].paddr = vtophys(clust);
1464 #endif
1465 		}
1466 	}
1467 	p->memtotal = p->numclusters * p->_clustsize;
1468 	if (netmap_verbose)
1469 		D("Pre-allocated %d clusters (%d/%dKB) for '%s'",
1470 		    p->numclusters, p->_clustsize >> 10,
1471 		    p->memtotal >> 10, p->name);
1472 
1473 	return 0;
1474 
1475 clean:
1476 	netmap_reset_obj_allocator(p);
1477 	return ENOMEM;
1478 }
1479 
1480 /* call with lock held */
1481 static int
1482 netmap_mem_params_changed(struct netmap_obj_params* p)
1483 {
1484 	int i, rv = 0;
1485 
1486 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1487 		if (p[i].last_size != p[i].size || p[i].last_num != p[i].num) {
1488 			p[i].last_size = p[i].size;
1489 			p[i].last_num = p[i].num;
1490 			rv = 1;
1491 		}
1492 	}
1493 	return rv;
1494 }
1495 
1496 static void
1497 netmap_mem_reset_all(struct netmap_mem_d *nmd)
1498 {
1499 	int i;
1500 
1501 	if (netmap_verbose)
1502 		D("resetting %p", nmd);
1503 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1504 		netmap_reset_obj_allocator(&nmd->pools[i]);
1505 	}
1506 	nmd->flags  &= ~NETMAP_MEM_FINALIZED;
1507 }
1508 
1509 static int
1510 netmap_mem_unmap(struct netmap_obj_pool *p, struct netmap_adapter *na)
1511 {
1512 	int i, lim = p->objtotal;
1513 	struct netmap_lut *lut = &na->na_lut;
1514 
1515 	if (na == NULL || na->pdev == NULL)
1516 		return 0;
1517 
1518 #if defined(__FreeBSD__)
1519 	(void)i;
1520 	(void)lim;
1521 	(void)lut;
1522 	D("unsupported on FreeBSD");
1523 #elif defined(_WIN32)
1524 	(void)i;
1525 	(void)lim;
1526 	(void)lut;
1527 	D("unsupported on Windows");
1528 #else /* linux */
1529 	ND("unmapping and freeing plut for %s", na->name);
1530 	if (lut->plut == NULL)
1531 		return 0;
1532 	for (i = 0; i < lim; i += p->_clustentries) {
1533 		if (lut->plut[i].paddr)
1534 			netmap_unload_map(na, (bus_dma_tag_t) na->pdev, &lut->plut[i].paddr, p->_clustsize);
1535 	}
1536 	nm_free_plut(lut->plut);
1537 	lut->plut = NULL;
1538 #endif /* linux */
1539 
1540 	return 0;
1541 }
1542 
1543 static int
1544 netmap_mem_map(struct netmap_obj_pool *p, struct netmap_adapter *na)
1545 {
1546 	int error = 0;
1547 	int i, lim = p->objtotal;
1548 	struct netmap_lut *lut = &na->na_lut;
1549 
1550 	if (na->pdev == NULL)
1551 		return 0;
1552 
1553 #if defined(__FreeBSD__)
1554 	(void)i;
1555 	(void)lim;
1556 	(void)lut;
1557 	D("unsupported on FreeBSD");
1558 #elif defined(_WIN32)
1559 	(void)i;
1560 	(void)lim;
1561 	(void)lut;
1562 	D("unsupported on Windows");
1563 #else /* linux */
1564 
1565 	if (lut->plut != NULL) {
1566 		ND("plut already allocated for %s", na->name);
1567 		return 0;
1568 	}
1569 
1570 	ND("allocating physical lut for %s", na->name);
1571 	lut->plut = nm_alloc_plut(lim);
1572 	if (lut->plut == NULL) {
1573 		D("Failed to allocate physical lut for %s", na->name);
1574 		return ENOMEM;
1575         }
1576 
1577 	for (i = 0; i < lim; i += p->_clustentries) {
1578 		lut->plut[i].paddr = 0;
1579 	}
1580 
1581 	for (i = 0; i < lim; i += p->_clustentries) {
1582 		int j;
1583 
1584 		if (p->lut[i].vaddr == NULL)
1585 			continue;
1586 
1587 		error = netmap_load_map(na, (bus_dma_tag_t) na->pdev, &lut->plut[i].paddr,
1588 				p->lut[i].vaddr, p->_clustsize);
1589 		if (error) {
1590 			D("Failed to map cluster #%d from the %s pool", i, p->name);
1591 			break;
1592 		}
1593 
1594 		for (j = 1; j < p->_clustentries; j++) {
1595 			lut->plut[i + j].paddr = lut->plut[i + j - 1].paddr + p->_objsize;
1596 		}
1597 	}
1598 
1599 	if (error)
1600 		netmap_mem_unmap(p, na);
1601 
1602 #endif /* linux */
1603 
1604 	return error;
1605 }
1606 
1607 static int
1608 netmap_mem_finalize_all(struct netmap_mem_d *nmd)
1609 {
1610 	int i;
1611 	if (nmd->flags & NETMAP_MEM_FINALIZED)
1612 		return 0;
1613 	nmd->lasterr = 0;
1614 	nmd->nm_totalsize = 0;
1615 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1616 		nmd->lasterr = netmap_finalize_obj_allocator(&nmd->pools[i]);
1617 		if (nmd->lasterr)
1618 			goto error;
1619 		nmd->nm_totalsize += nmd->pools[i].memtotal;
1620 	}
1621 	nmd->lasterr = netmap_mem_init_bitmaps(nmd);
1622 	if (nmd->lasterr)
1623 		goto error;
1624 
1625 	nmd->flags |= NETMAP_MEM_FINALIZED;
1626 
1627 	if (netmap_verbose)
1628 		D("interfaces %d KB, rings %d KB, buffers %d MB",
1629 		    nmd->pools[NETMAP_IF_POOL].memtotal >> 10,
1630 		    nmd->pools[NETMAP_RING_POOL].memtotal >> 10,
1631 		    nmd->pools[NETMAP_BUF_POOL].memtotal >> 20);
1632 
1633 	if (netmap_verbose)
1634 		D("Free buffers: %d", nmd->pools[NETMAP_BUF_POOL].objfree);
1635 
1636 
1637 	return 0;
1638 error:
1639 	netmap_mem_reset_all(nmd);
1640 	return nmd->lasterr;
1641 }
1642 
1643 /*
1644  * allocator for private memory
1645  */
1646 static void *
1647 _netmap_mem_private_new(size_t size, struct netmap_obj_params *p,
1648 		struct netmap_mem_ops *ops, int *perr)
1649 {
1650 	struct netmap_mem_d *d = NULL;
1651 	int i, err = 0;
1652 
1653 	d = nm_os_malloc(size);
1654 	if (d == NULL) {
1655 		err = ENOMEM;
1656 		goto error;
1657 	}
1658 
1659 	*d = nm_blueprint;
1660 	d->ops = ops;
1661 
1662 	err = nm_mem_assign_id(d);
1663 	if (err)
1664 		goto error_free;
1665 	snprintf(d->name, NM_MEM_NAMESZ, "%d", d->nm_id);
1666 
1667 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1668 		snprintf(d->pools[i].name, NETMAP_POOL_MAX_NAMSZ,
1669 				nm_blueprint.pools[i].name,
1670 				d->name);
1671 		d->params[i].num = p[i].num;
1672 		d->params[i].size = p[i].size;
1673 	}
1674 
1675 	NMA_LOCK_INIT(d);
1676 
1677 	err = netmap_mem_config(d);
1678 	if (err)
1679 		goto error_rel_id;
1680 
1681 	d->flags &= ~NETMAP_MEM_FINALIZED;
1682 
1683 	return d;
1684 
1685 error_rel_id:
1686 	NMA_LOCK_DESTROY(d);
1687 	nm_mem_release_id(d);
1688 error_free:
1689 	nm_os_free(d);
1690 error:
1691 	if (perr)
1692 		*perr = err;
1693 	return NULL;
1694 }
1695 
1696 struct netmap_mem_d *
1697 netmap_mem_private_new(u_int txr, u_int txd, u_int rxr, u_int rxd,
1698 		u_int extra_bufs, u_int npipes, int *perr)
1699 {
1700 	struct netmap_mem_d *d = NULL;
1701 	struct netmap_obj_params p[NETMAP_POOLS_NR];
1702 	int i;
1703 	u_int v, maxd;
1704 	/* account for the fake host rings */
1705 	txr++;
1706 	rxr++;
1707 
1708 	/* copy the min values */
1709 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1710 		p[i] = netmap_min_priv_params[i];
1711 	}
1712 
1713 	/* possibly increase them to fit user request */
1714 	v = sizeof(struct netmap_if) + sizeof(ssize_t) * (txr + rxr);
1715 	if (p[NETMAP_IF_POOL].size < v)
1716 		p[NETMAP_IF_POOL].size = v;
1717 	v = 2 + 4 * npipes;
1718 	if (p[NETMAP_IF_POOL].num < v)
1719 		p[NETMAP_IF_POOL].num = v;
1720 	maxd = (txd > rxd) ? txd : rxd;
1721 	v = sizeof(struct netmap_ring) + sizeof(struct netmap_slot) * maxd;
1722 	if (p[NETMAP_RING_POOL].size < v)
1723 		p[NETMAP_RING_POOL].size = v;
1724 	/* each pipe endpoint needs two tx rings (1 normal + 1 host, fake)
1725          * and two rx rings (again, 1 normal and 1 fake host)
1726          */
1727 	v = txr + rxr + 8 * npipes;
1728 	if (p[NETMAP_RING_POOL].num < v)
1729 		p[NETMAP_RING_POOL].num = v;
1730 	/* for each pipe we only need the buffers for the 4 "real" rings.
1731          * On the other end, the pipe ring dimension may be different from
1732          * the parent port ring dimension. As a compromise, we allocate twice the
1733          * space actually needed if the pipe rings were the same size as the parent rings
1734          */
1735 	v = (4 * npipes + rxr) * rxd + (4 * npipes + txr) * txd + 2 + extra_bufs;
1736 		/* the +2 is for the tx and rx fake buffers (indices 0 and 1) */
1737 	if (p[NETMAP_BUF_POOL].num < v)
1738 		p[NETMAP_BUF_POOL].num = v;
1739 
1740 	if (netmap_verbose)
1741 		D("req if %d*%d ring %d*%d buf %d*%d",
1742 			p[NETMAP_IF_POOL].num,
1743 			p[NETMAP_IF_POOL].size,
1744 			p[NETMAP_RING_POOL].num,
1745 			p[NETMAP_RING_POOL].size,
1746 			p[NETMAP_BUF_POOL].num,
1747 			p[NETMAP_BUF_POOL].size);
1748 
1749 	d = _netmap_mem_private_new(sizeof(*d), p, &netmap_mem_global_ops, perr);
1750 
1751 	return d;
1752 }
1753 
1754 
1755 /* call with lock held */
1756 static int
1757 netmap_mem2_config(struct netmap_mem_d *nmd)
1758 {
1759 	int i;
1760 
1761 	if (!netmap_mem_params_changed(nmd->params))
1762 		goto out;
1763 
1764 	ND("reconfiguring");
1765 
1766 	if (nmd->flags & NETMAP_MEM_FINALIZED) {
1767 		/* reset previous allocation */
1768 		for (i = 0; i < NETMAP_POOLS_NR; i++) {
1769 			netmap_reset_obj_allocator(&nmd->pools[i]);
1770 		}
1771 		nmd->flags &= ~NETMAP_MEM_FINALIZED;
1772 	}
1773 
1774 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1775 		nmd->lasterr = netmap_config_obj_allocator(&nmd->pools[i],
1776 				nmd->params[i].num, nmd->params[i].size);
1777 		if (nmd->lasterr)
1778 			goto out;
1779 	}
1780 
1781 out:
1782 
1783 	return nmd->lasterr;
1784 }
1785 
1786 static int
1787 netmap_mem2_finalize(struct netmap_mem_d *nmd)
1788 {
1789 	if (nmd->flags & NETMAP_MEM_FINALIZED)
1790 		goto out;
1791 
1792 	if (netmap_mem_finalize_all(nmd))
1793 		goto out;
1794 
1795 	nmd->lasterr = 0;
1796 
1797 out:
1798 	return nmd->lasterr;
1799 }
1800 
1801 static void
1802 netmap_mem2_delete(struct netmap_mem_d *nmd)
1803 {
1804 	int i;
1805 
1806 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
1807 	    netmap_destroy_obj_allocator(&nmd->pools[i]);
1808 	}
1809 
1810 	NMA_LOCK_DESTROY(nmd);
1811 	if (nmd != &nm_mem)
1812 		nm_os_free(nmd);
1813 }
1814 
1815 #ifdef WITH_EXTMEM
1816 /* doubly linekd list of all existing external allocators */
1817 static struct netmap_mem_ext *netmap_mem_ext_list = NULL;
1818 NM_MTX_T nm_mem_ext_list_lock;
1819 #endif /* WITH_EXTMEM */
1820 
1821 int
1822 netmap_mem_init(void)
1823 {
1824 	NM_MTX_INIT(nm_mem_list_lock);
1825 	NMA_LOCK_INIT(&nm_mem);
1826 	netmap_mem_get(&nm_mem);
1827 #ifdef WITH_EXTMEM
1828 	NM_MTX_INIT(nm_mem_ext_list_lock);
1829 #endif /* WITH_EXTMEM */
1830 	return (0);
1831 }
1832 
1833 void
1834 netmap_mem_fini(void)
1835 {
1836 	netmap_mem_put(&nm_mem);
1837 }
1838 
1839 static void
1840 netmap_free_rings(struct netmap_adapter *na)
1841 {
1842 	enum txrx t;
1843 
1844 	for_rx_tx(t) {
1845 		u_int i;
1846 		for (i = 0; i < nma_get_nrings(na, t) + 1; i++) {
1847 			struct netmap_kring *kring = NMR(na, t)[i];
1848 			struct netmap_ring *ring = kring->ring;
1849 
1850 			if (ring == NULL || kring->users > 0 || (kring->nr_kflags & NKR_NEEDRING)) {
1851 				if (netmap_verbose)
1852 					D("NOT deleting ring %s (ring %p, users %d neekring %d)",
1853 						kring->name, ring, kring->users, kring->nr_kflags & NKR_NEEDRING);
1854 				continue;
1855 			}
1856 			if (netmap_verbose)
1857 				D("deleting ring %s", kring->name);
1858 			if (!(kring->nr_kflags & NKR_FAKERING)) {
1859 				ND("freeing bufs for %s", kring->name);
1860 				netmap_free_bufs(na->nm_mem, ring->slot, kring->nkr_num_slots);
1861 			} else {
1862 				ND("NOT freeing bufs for %s", kring->name);
1863 			}
1864 			netmap_ring_free(na->nm_mem, ring);
1865 			kring->ring = NULL;
1866 		}
1867 	}
1868 }
1869 
1870 /* call with NMA_LOCK held *
1871  *
1872  * Allocate netmap rings and buffers for this card
1873  * The rings are contiguous, but have variable size.
1874  * The kring array must follow the layout described
1875  * in netmap_krings_create().
1876  */
1877 static int
1878 netmap_mem2_rings_create(struct netmap_adapter *na)
1879 {
1880 	enum txrx t;
1881 
1882 	for_rx_tx(t) {
1883 		u_int i;
1884 
1885 		for (i = 0; i <= nma_get_nrings(na, t); i++) {
1886 			struct netmap_kring *kring = NMR(na, t)[i];
1887 			struct netmap_ring *ring = kring->ring;
1888 			u_int len, ndesc;
1889 
1890 			if (ring || (!kring->users && !(kring->nr_kflags & NKR_NEEDRING))) {
1891 				/* uneeded, or already created by somebody else */
1892 				if (netmap_verbose)
1893 					D("NOT creating ring %s (ring %p, users %d neekring %d)",
1894 						kring->name, ring, kring->users, kring->nr_kflags & NKR_NEEDRING);
1895 				continue;
1896 			}
1897 			if (netmap_verbose)
1898 				D("creating %s", kring->name);
1899 			ndesc = kring->nkr_num_slots;
1900 			len = sizeof(struct netmap_ring) +
1901 				  ndesc * sizeof(struct netmap_slot);
1902 			ring = netmap_ring_malloc(na->nm_mem, len);
1903 			if (ring == NULL) {
1904 				D("Cannot allocate %s_ring", nm_txrx2str(t));
1905 				goto cleanup;
1906 			}
1907 			ND("txring at %p", ring);
1908 			kring->ring = ring;
1909 			*(uint32_t *)(uintptr_t)&ring->num_slots = ndesc;
1910 			*(int64_t *)(uintptr_t)&ring->buf_ofs =
1911 			    (na->nm_mem->pools[NETMAP_IF_POOL].memtotal +
1912 				na->nm_mem->pools[NETMAP_RING_POOL].memtotal) -
1913 				netmap_ring_offset(na->nm_mem, ring);
1914 
1915 			/* copy values from kring */
1916 			ring->head = kring->rhead;
1917 			ring->cur = kring->rcur;
1918 			ring->tail = kring->rtail;
1919 			*(uint32_t *)(uintptr_t)&ring->nr_buf_size =
1920 				netmap_mem_bufsize(na->nm_mem);
1921 			ND("%s h %d c %d t %d", kring->name,
1922 				ring->head, ring->cur, ring->tail);
1923 			ND("initializing slots for %s_ring", nm_txrx2str(txrx));
1924 			if (!(kring->nr_kflags & NKR_FAKERING)) {
1925 				/* this is a real ring */
1926 				ND("allocating buffers for %s", kring->name);
1927 				if (netmap_new_bufs(na->nm_mem, ring->slot, ndesc)) {
1928 					D("Cannot allocate buffers for %s_ring", nm_txrx2str(t));
1929 					goto cleanup;
1930 				}
1931 			} else {
1932 				/* this is a fake ring, set all indices to 0 */
1933 				ND("NOT allocating buffers for %s", kring->name);
1934 				netmap_mem_set_ring(na->nm_mem, ring->slot, ndesc, 0);
1935 			}
1936 		        /* ring info */
1937 		        *(uint16_t *)(uintptr_t)&ring->ringid = kring->ring_id;
1938 		        *(uint16_t *)(uintptr_t)&ring->dir = kring->tx;
1939 		}
1940 	}
1941 
1942 	return 0;
1943 
1944 cleanup:
1945 	netmap_free_rings(na);
1946 
1947 	return ENOMEM;
1948 }
1949 
1950 static void
1951 netmap_mem2_rings_delete(struct netmap_adapter *na)
1952 {
1953 	/* last instance, release bufs and rings */
1954 	netmap_free_rings(na);
1955 }
1956 
1957 
1958 /* call with NMA_LOCK held */
1959 /*
1960  * Allocate the per-fd structure netmap_if.
1961  *
1962  * We assume that the configuration stored in na
1963  * (number of tx/rx rings and descs) does not change while
1964  * the interface is in netmap mode.
1965  */
1966 static struct netmap_if *
1967 netmap_mem2_if_new(struct netmap_adapter *na, struct netmap_priv_d *priv)
1968 {
1969 	struct netmap_if *nifp;
1970 	ssize_t base; /* handy for relative offsets between rings and nifp */
1971 	u_int i, len, n[NR_TXRX], ntot;
1972 	enum txrx t;
1973 
1974 	ntot = 0;
1975 	for_rx_tx(t) {
1976 		/* account for the (eventually fake) host rings */
1977 		n[t] = nma_get_nrings(na, t) + 1;
1978 		ntot += n[t];
1979 	}
1980 	/*
1981 	 * the descriptor is followed inline by an array of offsets
1982 	 * to the tx and rx rings in the shared memory region.
1983 	 */
1984 
1985 	len = sizeof(struct netmap_if) + (ntot * sizeof(ssize_t));
1986 	nifp = netmap_if_malloc(na->nm_mem, len);
1987 	if (nifp == NULL) {
1988 		NMA_UNLOCK(na->nm_mem);
1989 		return NULL;
1990 	}
1991 
1992 	/* initialize base fields -- override const */
1993 	*(u_int *)(uintptr_t)&nifp->ni_tx_rings = na->num_tx_rings;
1994 	*(u_int *)(uintptr_t)&nifp->ni_rx_rings = na->num_rx_rings;
1995 	strncpy(nifp->ni_name, na->name, (size_t)IFNAMSIZ);
1996 
1997 	/*
1998 	 * fill the slots for the rx and tx rings. They contain the offset
1999 	 * between the ring and nifp, so the information is usable in
2000 	 * userspace to reach the ring from the nifp.
2001 	 */
2002 	base = netmap_if_offset(na->nm_mem, nifp);
2003 	for (i = 0; i < n[NR_TX]; i++) {
2004 		/* XXX instead of ofs == 0 maybe use the offset of an error
2005 		 * ring, like we do for buffers? */
2006 		ssize_t ofs = 0;
2007 
2008 		if (na->tx_rings[i]->ring != NULL && i >= priv->np_qfirst[NR_TX]
2009 				&& i < priv->np_qlast[NR_TX]) {
2010 			ofs = netmap_ring_offset(na->nm_mem,
2011 						 na->tx_rings[i]->ring) - base;
2012 		}
2013 		*(ssize_t *)(uintptr_t)&nifp->ring_ofs[i] = ofs;
2014 	}
2015 	for (i = 0; i < n[NR_RX]; i++) {
2016 		/* XXX instead of ofs == 0 maybe use the offset of an error
2017 		 * ring, like we do for buffers? */
2018 		ssize_t ofs = 0;
2019 
2020 		if (na->rx_rings[i]->ring != NULL && i >= priv->np_qfirst[NR_RX]
2021 				&& i < priv->np_qlast[NR_RX]) {
2022 			ofs = netmap_ring_offset(na->nm_mem,
2023 						 na->rx_rings[i]->ring) - base;
2024 		}
2025 		*(ssize_t *)(uintptr_t)&nifp->ring_ofs[i+n[NR_TX]] = ofs;
2026 	}
2027 
2028 	return (nifp);
2029 }
2030 
2031 static void
2032 netmap_mem2_if_delete(struct netmap_adapter *na, struct netmap_if *nifp)
2033 {
2034 	if (nifp == NULL)
2035 		/* nothing to do */
2036 		return;
2037 	if (nifp->ni_bufs_head)
2038 		netmap_extra_free(na, nifp->ni_bufs_head);
2039 	netmap_if_free(na->nm_mem, nifp);
2040 }
2041 
2042 static void
2043 netmap_mem2_deref(struct netmap_mem_d *nmd)
2044 {
2045 
2046 	if (netmap_verbose)
2047 		D("active = %d", nmd->active);
2048 
2049 }
2050 
2051 struct netmap_mem_ops netmap_mem_global_ops = {
2052 	.nmd_get_lut = netmap_mem2_get_lut,
2053 	.nmd_get_info = netmap_mem2_get_info,
2054 	.nmd_ofstophys = netmap_mem2_ofstophys,
2055 	.nmd_config = netmap_mem2_config,
2056 	.nmd_finalize = netmap_mem2_finalize,
2057 	.nmd_deref = netmap_mem2_deref,
2058 	.nmd_delete = netmap_mem2_delete,
2059 	.nmd_if_offset = netmap_mem2_if_offset,
2060 	.nmd_if_new = netmap_mem2_if_new,
2061 	.nmd_if_delete = netmap_mem2_if_delete,
2062 	.nmd_rings_create = netmap_mem2_rings_create,
2063 	.nmd_rings_delete = netmap_mem2_rings_delete
2064 };
2065 
2066 int
2067 netmap_mem_pools_info_get(struct nmreq_pools_info *req,
2068 				struct netmap_mem_d *nmd)
2069 {
2070 	int ret;
2071 
2072 	ret = netmap_mem_get_info(nmd, &req->nr_memsize, NULL,
2073 					&req->nr_mem_id);
2074 	if (ret) {
2075 		return ret;
2076 	}
2077 
2078 	NMA_LOCK(nmd);
2079 	req->nr_if_pool_offset = 0;
2080 	req->nr_if_pool_objtotal = nmd->pools[NETMAP_IF_POOL].objtotal;
2081 	req->nr_if_pool_objsize = nmd->pools[NETMAP_IF_POOL]._objsize;
2082 
2083 	req->nr_ring_pool_offset = nmd->pools[NETMAP_IF_POOL].memtotal;
2084 	req->nr_ring_pool_objtotal = nmd->pools[NETMAP_RING_POOL].objtotal;
2085 	req->nr_ring_pool_objsize = nmd->pools[NETMAP_RING_POOL]._objsize;
2086 
2087 	req->nr_buf_pool_offset = nmd->pools[NETMAP_IF_POOL].memtotal +
2088 			     nmd->pools[NETMAP_RING_POOL].memtotal;
2089 	req->nr_buf_pool_objtotal = nmd->pools[NETMAP_BUF_POOL].objtotal;
2090 	req->nr_buf_pool_objsize = nmd->pools[NETMAP_BUF_POOL]._objsize;
2091 	NMA_UNLOCK(nmd);
2092 
2093 	return 0;
2094 }
2095 
2096 #ifdef WITH_EXTMEM
2097 struct netmap_mem_ext {
2098 	struct netmap_mem_d up;
2099 
2100 	struct nm_os_extmem *os;
2101 	struct netmap_mem_ext *next, *prev;
2102 };
2103 
2104 /* call with nm_mem_list_lock held */
2105 static void
2106 netmap_mem_ext_register(struct netmap_mem_ext *e)
2107 {
2108 	NM_MTX_LOCK(nm_mem_ext_list_lock);
2109 	if (netmap_mem_ext_list)
2110 		netmap_mem_ext_list->prev = e;
2111 	e->next = netmap_mem_ext_list;
2112 	netmap_mem_ext_list = e;
2113 	e->prev = NULL;
2114 	NM_MTX_UNLOCK(nm_mem_ext_list_lock);
2115 }
2116 
2117 /* call with nm_mem_list_lock held */
2118 static void
2119 netmap_mem_ext_unregister(struct netmap_mem_ext *e)
2120 {
2121 	if (e->prev)
2122 		e->prev->next = e->next;
2123 	else
2124 		netmap_mem_ext_list = e->next;
2125 	if (e->next)
2126 		e->next->prev = e->prev;
2127 	e->prev = e->next = NULL;
2128 }
2129 
2130 static struct netmap_mem_ext *
2131 netmap_mem_ext_search(struct nm_os_extmem *os)
2132 {
2133 	struct netmap_mem_ext *e;
2134 
2135 	NM_MTX_LOCK(nm_mem_ext_list_lock);
2136 	for (e = netmap_mem_ext_list; e; e = e->next) {
2137 		if (nm_os_extmem_isequal(e->os, os)) {
2138 			netmap_mem_get(&e->up);
2139 			break;
2140 		}
2141 	}
2142 	NM_MTX_UNLOCK(nm_mem_ext_list_lock);
2143 	return e;
2144 }
2145 
2146 
2147 static void
2148 netmap_mem_ext_delete(struct netmap_mem_d *d)
2149 {
2150 	int i;
2151 	struct netmap_mem_ext *e =
2152 		(struct netmap_mem_ext *)d;
2153 
2154 	netmap_mem_ext_unregister(e);
2155 
2156 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
2157 		struct netmap_obj_pool *p = &d->pools[i];
2158 
2159 		if (p->lut) {
2160 			nm_free_lut(p->lut, p->objtotal);
2161 			p->lut = NULL;
2162 		}
2163 	}
2164 	if (e->os)
2165 		nm_os_extmem_delete(e->os);
2166 	netmap_mem2_delete(d);
2167 }
2168 
2169 static int
2170 netmap_mem_ext_config(struct netmap_mem_d *nmd)
2171 {
2172 	return 0;
2173 }
2174 
2175 struct netmap_mem_ops netmap_mem_ext_ops = {
2176 	.nmd_get_lut = netmap_mem2_get_lut,
2177 	.nmd_get_info = netmap_mem2_get_info,
2178 	.nmd_ofstophys = netmap_mem2_ofstophys,
2179 	.nmd_config = netmap_mem_ext_config,
2180 	.nmd_finalize = netmap_mem2_finalize,
2181 	.nmd_deref = netmap_mem2_deref,
2182 	.nmd_delete = netmap_mem_ext_delete,
2183 	.nmd_if_offset = netmap_mem2_if_offset,
2184 	.nmd_if_new = netmap_mem2_if_new,
2185 	.nmd_if_delete = netmap_mem2_if_delete,
2186 	.nmd_rings_create = netmap_mem2_rings_create,
2187 	.nmd_rings_delete = netmap_mem2_rings_delete
2188 };
2189 
2190 struct netmap_mem_d *
2191 netmap_mem_ext_create(uint64_t usrptr, struct nmreq_pools_info *pi, int *perror)
2192 {
2193 	int error = 0;
2194 	int i, j;
2195 	struct netmap_mem_ext *nme;
2196 	char *clust;
2197 	size_t off;
2198 	struct nm_os_extmem *os = NULL;
2199 	int nr_pages;
2200 
2201 	// XXX sanity checks
2202 	if (pi->nr_if_pool_objtotal == 0)
2203 		pi->nr_if_pool_objtotal = netmap_min_priv_params[NETMAP_IF_POOL].num;
2204 	if (pi->nr_if_pool_objsize == 0)
2205 		pi->nr_if_pool_objsize = netmap_min_priv_params[NETMAP_IF_POOL].size;
2206 	if (pi->nr_ring_pool_objtotal == 0)
2207 		pi->nr_ring_pool_objtotal = netmap_min_priv_params[NETMAP_RING_POOL].num;
2208 	if (pi->nr_ring_pool_objsize == 0)
2209 		pi->nr_ring_pool_objsize = netmap_min_priv_params[NETMAP_RING_POOL].size;
2210 	if (pi->nr_buf_pool_objtotal == 0)
2211 		pi->nr_buf_pool_objtotal = netmap_min_priv_params[NETMAP_BUF_POOL].num;
2212 	if (pi->nr_buf_pool_objsize == 0)
2213 		pi->nr_buf_pool_objsize = netmap_min_priv_params[NETMAP_BUF_POOL].size;
2214 	D("if %d %d ring %d %d buf %d %d",
2215 			pi->nr_if_pool_objtotal, pi->nr_if_pool_objsize,
2216 			pi->nr_ring_pool_objtotal, pi->nr_ring_pool_objsize,
2217 			pi->nr_buf_pool_objtotal, pi->nr_buf_pool_objsize);
2218 
2219 	os = nm_os_extmem_create(usrptr, pi, &error);
2220 	if (os == NULL) {
2221 		D("os extmem creation failed");
2222 		goto out;
2223 	}
2224 
2225 	nme = netmap_mem_ext_search(os);
2226 	if (nme) {
2227 		nm_os_extmem_delete(os);
2228 		return &nme->up;
2229 	}
2230 	D("not found, creating new");
2231 
2232 	nme = _netmap_mem_private_new(sizeof(*nme),
2233 			(struct netmap_obj_params[]){
2234 				{ pi->nr_if_pool_objsize, pi->nr_if_pool_objtotal },
2235 				{ pi->nr_ring_pool_objsize, pi->nr_ring_pool_objtotal },
2236 				{ pi->nr_buf_pool_objsize, pi->nr_buf_pool_objtotal }},
2237 			&netmap_mem_ext_ops,
2238 			&error);
2239 	if (nme == NULL)
2240 		goto out_unmap;
2241 
2242 	nr_pages = nm_os_extmem_nr_pages(os);
2243 
2244 	/* from now on pages will be released by nme destructor;
2245 	 * we let res = 0 to prevent release in out_unmap below
2246 	 */
2247 	nme->os = os;
2248 	os = NULL; /* pass ownership */
2249 
2250 	clust = nm_os_extmem_nextpage(nme->os);
2251 	off = 0;
2252 	for (i = 0; i < NETMAP_POOLS_NR; i++) {
2253 		struct netmap_obj_pool *p = &nme->up.pools[i];
2254 		struct netmap_obj_params *o = &nme->up.params[i];
2255 
2256 		p->_objsize = o->size;
2257 		p->_clustsize = o->size;
2258 		p->_clustentries = 1;
2259 
2260 		p->lut = nm_alloc_lut(o->num);
2261 		if (p->lut == NULL) {
2262 			error = ENOMEM;
2263 			goto out_delete;
2264 		}
2265 
2266 		p->bitmap_slots = (o->num + sizeof(uint32_t) - 1) / sizeof(uint32_t);
2267 		p->invalid_bitmap = nm_os_malloc(sizeof(uint32_t) * p->bitmap_slots);
2268 		if (p->invalid_bitmap == NULL) {
2269 			error = ENOMEM;
2270 			goto out_delete;
2271 		}
2272 
2273 		if (nr_pages == 0) {
2274 			p->objtotal = 0;
2275 			p->memtotal = 0;
2276 			p->objfree = 0;
2277 			continue;
2278 		}
2279 
2280 		for (j = 0; j < o->num && nr_pages > 0; j++) {
2281 			size_t noff;
2282 
2283 			p->lut[j].vaddr = clust + off;
2284 #if !defined(linux) && !defined(_WIN32)
2285 			p->lut[j].paddr = vtophys(p->lut[j].vaddr);
2286 #endif
2287 			ND("%s %d at %p", p->name, j, p->lut[j].vaddr);
2288 			noff = off + p->_objsize;
2289 			if (noff < PAGE_SIZE) {
2290 				off = noff;
2291 				continue;
2292 			}
2293 			ND("too big, recomputing offset...");
2294 			while (noff >= PAGE_SIZE) {
2295 				char *old_clust = clust;
2296 				noff -= PAGE_SIZE;
2297 				clust = nm_os_extmem_nextpage(nme->os);
2298 				nr_pages--;
2299 				ND("noff %zu page %p nr_pages %d", noff,
2300 						page_to_virt(*pages), nr_pages);
2301 				if (noff > 0 && !nm_isset(p->invalid_bitmap, j) &&
2302 					(nr_pages == 0 ||
2303 					 old_clust + PAGE_SIZE != clust))
2304 				{
2305 					/* out of space or non contiguous,
2306 					 * drop this object
2307 					 * */
2308 					p->invalid_bitmap[ (j>>5) ] |= 1U << (j & 31U);
2309 					ND("non contiguous at off %zu, drop", noff);
2310 				}
2311 				if (nr_pages == 0)
2312 					break;
2313 			}
2314 			off = noff;
2315 		}
2316 		p->objtotal = j;
2317 		p->numclusters = p->objtotal;
2318 		p->memtotal = j * p->_objsize;
2319 		ND("%d memtotal %u", j, p->memtotal);
2320 	}
2321 
2322 	netmap_mem_ext_register(nme);
2323 
2324 	return &nme->up;
2325 
2326 out_delete:
2327 	netmap_mem_put(&nme->up);
2328 out_unmap:
2329 	if (os)
2330 		nm_os_extmem_delete(os);
2331 out:
2332 	if (perror)
2333 		*perror = error;
2334 	return NULL;
2335 
2336 }
2337 #endif /* WITH_EXTMEM */
2338 
2339 
2340 #ifdef WITH_PTNETMAP_GUEST
2341 struct mem_pt_if {
2342 	struct mem_pt_if *next;
2343 	struct ifnet *ifp;
2344 	unsigned int nifp_offset;
2345 };
2346 
2347 /* Netmap allocator for ptnetmap guests. */
2348 struct netmap_mem_ptg {
2349 	struct netmap_mem_d up;
2350 
2351 	vm_paddr_t nm_paddr;            /* physical address in the guest */
2352 	void *nm_addr;                  /* virtual address in the guest */
2353 	struct netmap_lut buf_lut;      /* lookup table for BUF pool in the guest */
2354 	nm_memid_t host_mem_id;         /* allocator identifier in the host */
2355 	struct ptnetmap_memdev *ptn_dev;/* ptnetmap memdev */
2356 	struct mem_pt_if *pt_ifs;	/* list of interfaces in passthrough */
2357 };
2358 
2359 /* Link a passthrough interface to a passthrough netmap allocator. */
2360 static int
2361 netmap_mem_pt_guest_ifp_add(struct netmap_mem_d *nmd, struct ifnet *ifp,
2362 			    unsigned int nifp_offset)
2363 {
2364 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2365 	struct mem_pt_if *ptif = nm_os_malloc(sizeof(*ptif));
2366 
2367 	if (!ptif) {
2368 		return ENOMEM;
2369 	}
2370 
2371 	NMA_LOCK(nmd);
2372 
2373 	ptif->ifp = ifp;
2374 	ptif->nifp_offset = nifp_offset;
2375 
2376 	if (ptnmd->pt_ifs) {
2377 		ptif->next = ptnmd->pt_ifs;
2378 	}
2379 	ptnmd->pt_ifs = ptif;
2380 
2381 	NMA_UNLOCK(nmd);
2382 
2383 	D("added (ifp=%p,nifp_offset=%u)", ptif->ifp, ptif->nifp_offset);
2384 
2385 	return 0;
2386 }
2387 
2388 /* Called with NMA_LOCK(nmd) held. */
2389 static struct mem_pt_if *
2390 netmap_mem_pt_guest_ifp_lookup(struct netmap_mem_d *nmd, struct ifnet *ifp)
2391 {
2392 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2393 	struct mem_pt_if *curr;
2394 
2395 	for (curr = ptnmd->pt_ifs; curr; curr = curr->next) {
2396 		if (curr->ifp == ifp) {
2397 			return curr;
2398 		}
2399 	}
2400 
2401 	return NULL;
2402 }
2403 
2404 /* Unlink a passthrough interface from a passthrough netmap allocator. */
2405 int
2406 netmap_mem_pt_guest_ifp_del(struct netmap_mem_d *nmd, struct ifnet *ifp)
2407 {
2408 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2409 	struct mem_pt_if *prev = NULL;
2410 	struct mem_pt_if *curr;
2411 	int ret = -1;
2412 
2413 	NMA_LOCK(nmd);
2414 
2415 	for (curr = ptnmd->pt_ifs; curr; curr = curr->next) {
2416 		if (curr->ifp == ifp) {
2417 			if (prev) {
2418 				prev->next = curr->next;
2419 			} else {
2420 				ptnmd->pt_ifs = curr->next;
2421 			}
2422 			D("removed (ifp=%p,nifp_offset=%u)",
2423 			  curr->ifp, curr->nifp_offset);
2424 			nm_os_free(curr);
2425 			ret = 0;
2426 			break;
2427 		}
2428 		prev = curr;
2429 	}
2430 
2431 	NMA_UNLOCK(nmd);
2432 
2433 	return ret;
2434 }
2435 
2436 static int
2437 netmap_mem_pt_guest_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut)
2438 {
2439 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2440 
2441 	if (!(nmd->flags & NETMAP_MEM_FINALIZED)) {
2442 		return EINVAL;
2443 	}
2444 
2445 	*lut = ptnmd->buf_lut;
2446 	return 0;
2447 }
2448 
2449 static int
2450 netmap_mem_pt_guest_get_info(struct netmap_mem_d *nmd, uint64_t *size,
2451 			     u_int *memflags, uint16_t *id)
2452 {
2453 	int error = 0;
2454 
2455 	error = nmd->ops->nmd_config(nmd);
2456 	if (error)
2457 		goto out;
2458 
2459 	if (size)
2460 		*size = nmd->nm_totalsize;
2461 	if (memflags)
2462 		*memflags = nmd->flags;
2463 	if (id)
2464 		*id = nmd->nm_id;
2465 
2466 out:
2467 
2468 	return error;
2469 }
2470 
2471 static vm_paddr_t
2472 netmap_mem_pt_guest_ofstophys(struct netmap_mem_d *nmd, vm_ooffset_t off)
2473 {
2474 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2475 	vm_paddr_t paddr;
2476 	/* if the offset is valid, just return csb->base_addr + off */
2477 	paddr = (vm_paddr_t)(ptnmd->nm_paddr + off);
2478 	ND("off %lx padr %lx", off, (unsigned long)paddr);
2479 	return paddr;
2480 }
2481 
2482 static int
2483 netmap_mem_pt_guest_config(struct netmap_mem_d *nmd)
2484 {
2485 	/* nothing to do, we are configured on creation
2486 	 * and configuration never changes thereafter
2487 	 */
2488 	return 0;
2489 }
2490 
2491 static int
2492 netmap_mem_pt_guest_finalize(struct netmap_mem_d *nmd)
2493 {
2494 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2495 	uint64_t mem_size;
2496 	uint32_t bufsize;
2497 	uint32_t nbuffers;
2498 	uint32_t poolofs;
2499 	vm_paddr_t paddr;
2500 	char *vaddr;
2501 	int i;
2502 	int error = 0;
2503 
2504 	if (nmd->flags & NETMAP_MEM_FINALIZED)
2505 		goto out;
2506 
2507 	if (ptnmd->ptn_dev == NULL) {
2508 		D("ptnetmap memdev not attached");
2509 		error = ENOMEM;
2510 		goto out;
2511 	}
2512 	/* Map memory through ptnetmap-memdev BAR. */
2513 	error = nm_os_pt_memdev_iomap(ptnmd->ptn_dev, &ptnmd->nm_paddr,
2514 				      &ptnmd->nm_addr, &mem_size);
2515 	if (error)
2516 		goto out;
2517 
2518         /* Initialize the lut using the information contained in the
2519 	 * ptnetmap memory device. */
2520         bufsize = nm_os_pt_memdev_ioread(ptnmd->ptn_dev,
2521 					 PTNET_MDEV_IO_BUF_POOL_OBJSZ);
2522         nbuffers = nm_os_pt_memdev_ioread(ptnmd->ptn_dev,
2523 					 PTNET_MDEV_IO_BUF_POOL_OBJNUM);
2524 
2525 	/* allocate the lut */
2526 	if (ptnmd->buf_lut.lut == NULL) {
2527 		D("allocating lut");
2528 		ptnmd->buf_lut.lut = nm_alloc_lut(nbuffers);
2529 		if (ptnmd->buf_lut.lut == NULL) {
2530 			D("lut allocation failed");
2531 			return ENOMEM;
2532 		}
2533 	}
2534 
2535 	/* we have physically contiguous memory mapped through PCI BAR */
2536 	poolofs = nm_os_pt_memdev_ioread(ptnmd->ptn_dev,
2537 					 PTNET_MDEV_IO_BUF_POOL_OFS);
2538 	vaddr = (char *)(ptnmd->nm_addr) + poolofs;
2539 	paddr = ptnmd->nm_paddr + poolofs;
2540 
2541 	for (i = 0; i < nbuffers; i++) {
2542 		ptnmd->buf_lut.lut[i].vaddr = vaddr;
2543 		vaddr += bufsize;
2544 		paddr += bufsize;
2545 	}
2546 
2547 	ptnmd->buf_lut.objtotal = nbuffers;
2548 	ptnmd->buf_lut.objsize = bufsize;
2549 	nmd->nm_totalsize = (unsigned int)mem_size;
2550 
2551 	/* Initialize these fields as are needed by
2552 	 * netmap_mem_bufsize().
2553 	 * XXX please improve this, why do we need this
2554 	 * replication? maybe we nmd->pools[] should no be
2555 	 * there for the guest allocator? */
2556 	nmd->pools[NETMAP_BUF_POOL]._objsize = bufsize;
2557 	nmd->pools[NETMAP_BUF_POOL]._objtotal = nbuffers;
2558 
2559 	nmd->flags |= NETMAP_MEM_FINALIZED;
2560 out:
2561 	return error;
2562 }
2563 
2564 static void
2565 netmap_mem_pt_guest_deref(struct netmap_mem_d *nmd)
2566 {
2567 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2568 
2569 	if (nmd->active == 1 &&
2570 		(nmd->flags & NETMAP_MEM_FINALIZED)) {
2571 	    nmd->flags  &= ~NETMAP_MEM_FINALIZED;
2572 	    /* unmap ptnetmap-memdev memory */
2573 	    if (ptnmd->ptn_dev) {
2574 		nm_os_pt_memdev_iounmap(ptnmd->ptn_dev);
2575 	    }
2576 	    ptnmd->nm_addr = NULL;
2577 	    ptnmd->nm_paddr = 0;
2578 	}
2579 }
2580 
2581 static ssize_t
2582 netmap_mem_pt_guest_if_offset(struct netmap_mem_d *nmd, const void *vaddr)
2583 {
2584 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd;
2585 
2586 	return (const char *)(vaddr) - (char *)(ptnmd->nm_addr);
2587 }
2588 
2589 static void
2590 netmap_mem_pt_guest_delete(struct netmap_mem_d *nmd)
2591 {
2592 	if (nmd == NULL)
2593 		return;
2594 	if (netmap_verbose)
2595 		D("deleting %p", nmd);
2596 	if (nmd->active > 0)
2597 		D("bug: deleting mem allocator with active=%d!", nmd->active);
2598 	if (netmap_verbose)
2599 		D("done deleting %p", nmd);
2600 	NMA_LOCK_DESTROY(nmd);
2601 	nm_os_free(nmd);
2602 }
2603 
2604 static struct netmap_if *
2605 netmap_mem_pt_guest_if_new(struct netmap_adapter *na, struct netmap_priv_d *priv)
2606 {
2607 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)na->nm_mem;
2608 	struct mem_pt_if *ptif;
2609 	struct netmap_if *nifp = NULL;
2610 
2611 	ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, na->ifp);
2612 	if (ptif == NULL) {
2613 		D("Error: interface %p is not in passthrough", na->ifp);
2614 		goto out;
2615 	}
2616 
2617 	nifp = (struct netmap_if *)((char *)(ptnmd->nm_addr) +
2618 				    ptif->nifp_offset);
2619 out:
2620 	return nifp;
2621 }
2622 
2623 static void
2624 netmap_mem_pt_guest_if_delete(struct netmap_adapter *na, struct netmap_if *nifp)
2625 {
2626 	struct mem_pt_if *ptif;
2627 
2628 	ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, na->ifp);
2629 	if (ptif == NULL) {
2630 		D("Error: interface %p is not in passthrough", na->ifp);
2631 	}
2632 }
2633 
2634 static int
2635 netmap_mem_pt_guest_rings_create(struct netmap_adapter *na)
2636 {
2637 	struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)na->nm_mem;
2638 	struct mem_pt_if *ptif;
2639 	struct netmap_if *nifp;
2640 	int i, error = -1;
2641 
2642 	ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, na->ifp);
2643 	if (ptif == NULL) {
2644 		D("Error: interface %p is not in passthrough", na->ifp);
2645 		goto out;
2646 	}
2647 
2648 
2649 	/* point each kring to the corresponding backend ring */
2650 	nifp = (struct netmap_if *)((char *)ptnmd->nm_addr + ptif->nifp_offset);
2651 	for (i = 0; i <= na->num_tx_rings; i++) {
2652 		struct netmap_kring *kring = na->tx_rings[i];
2653 		if (kring->ring)
2654 			continue;
2655 		kring->ring = (struct netmap_ring *)
2656 			((char *)nifp + nifp->ring_ofs[i]);
2657 	}
2658 	for (i = 0; i <= na->num_rx_rings; i++) {
2659 		struct netmap_kring *kring = na->rx_rings[i];
2660 		if (kring->ring)
2661 			continue;
2662 		kring->ring = (struct netmap_ring *)
2663 			((char *)nifp +
2664 			 nifp->ring_ofs[i + na->num_tx_rings + 1]);
2665 	}
2666 
2667 	error = 0;
2668 out:
2669 	return error;
2670 }
2671 
2672 static void
2673 netmap_mem_pt_guest_rings_delete(struct netmap_adapter *na)
2674 {
2675 #if 0
2676 	enum txrx t;
2677 
2678 	for_rx_tx(t) {
2679 		u_int i;
2680 		for (i = 0; i < nma_get_nrings(na, t) + 1; i++) {
2681 			struct netmap_kring *kring = &NMR(na, t)[i];
2682 
2683 			kring->ring = NULL;
2684 		}
2685 	}
2686 #endif
2687 }
2688 
2689 static struct netmap_mem_ops netmap_mem_pt_guest_ops = {
2690 	.nmd_get_lut = netmap_mem_pt_guest_get_lut,
2691 	.nmd_get_info = netmap_mem_pt_guest_get_info,
2692 	.nmd_ofstophys = netmap_mem_pt_guest_ofstophys,
2693 	.nmd_config = netmap_mem_pt_guest_config,
2694 	.nmd_finalize = netmap_mem_pt_guest_finalize,
2695 	.nmd_deref = netmap_mem_pt_guest_deref,
2696 	.nmd_if_offset = netmap_mem_pt_guest_if_offset,
2697 	.nmd_delete = netmap_mem_pt_guest_delete,
2698 	.nmd_if_new = netmap_mem_pt_guest_if_new,
2699 	.nmd_if_delete = netmap_mem_pt_guest_if_delete,
2700 	.nmd_rings_create = netmap_mem_pt_guest_rings_create,
2701 	.nmd_rings_delete = netmap_mem_pt_guest_rings_delete
2702 };
2703 
2704 /* Called with nm_mem_list_lock held. */
2705 static struct netmap_mem_d *
2706 netmap_mem_pt_guest_find_memid(nm_memid_t mem_id)
2707 {
2708 	struct netmap_mem_d *mem = NULL;
2709 	struct netmap_mem_d *scan = netmap_last_mem_d;
2710 
2711 	do {
2712 		/* find ptnetmap allocator through host ID */
2713 		if (scan->ops->nmd_deref == netmap_mem_pt_guest_deref &&
2714 			((struct netmap_mem_ptg *)(scan))->host_mem_id == mem_id) {
2715 			mem = scan;
2716 			mem->refcount++;
2717 			NM_DBG_REFC(mem, __FUNCTION__, __LINE__);
2718 			break;
2719 		}
2720 		scan = scan->next;
2721 	} while (scan != netmap_last_mem_d);
2722 
2723 	return mem;
2724 }
2725 
2726 /* Called with nm_mem_list_lock held. */
2727 static struct netmap_mem_d *
2728 netmap_mem_pt_guest_create(nm_memid_t mem_id)
2729 {
2730 	struct netmap_mem_ptg *ptnmd;
2731 	int err = 0;
2732 
2733 	ptnmd = nm_os_malloc(sizeof(struct netmap_mem_ptg));
2734 	if (ptnmd == NULL) {
2735 		err = ENOMEM;
2736 		goto error;
2737 	}
2738 
2739 	ptnmd->up.ops = &netmap_mem_pt_guest_ops;
2740 	ptnmd->host_mem_id = mem_id;
2741 	ptnmd->pt_ifs = NULL;
2742 
2743         /* Assign new id in the guest (We have the lock) */
2744 	err = nm_mem_assign_id_locked(&ptnmd->up);
2745 	if (err)
2746 		goto error;
2747 
2748 	ptnmd->up.flags &= ~NETMAP_MEM_FINALIZED;
2749 	ptnmd->up.flags |= NETMAP_MEM_IO;
2750 
2751 	NMA_LOCK_INIT(&ptnmd->up);
2752 
2753 	snprintf(ptnmd->up.name, NM_MEM_NAMESZ, "%d", ptnmd->up.nm_id);
2754 
2755 
2756 	return &ptnmd->up;
2757 error:
2758 	netmap_mem_pt_guest_delete(&ptnmd->up);
2759 	return NULL;
2760 }
2761 
2762 /*
2763  * find host id in guest allocators and create guest allocator
2764  * if it is not there
2765  */
2766 static struct netmap_mem_d *
2767 netmap_mem_pt_guest_get(nm_memid_t mem_id)
2768 {
2769 	struct netmap_mem_d *nmd;
2770 
2771 	NM_MTX_LOCK(nm_mem_list_lock);
2772 	nmd = netmap_mem_pt_guest_find_memid(mem_id);
2773 	if (nmd == NULL) {
2774 		nmd = netmap_mem_pt_guest_create(mem_id);
2775 	}
2776 	NM_MTX_UNLOCK(nm_mem_list_lock);
2777 
2778 	return nmd;
2779 }
2780 
2781 /*
2782  * The guest allocator can be created by ptnetmap_memdev (during the device
2783  * attach) or by ptnetmap device (ptnet), during the netmap_attach.
2784  *
2785  * The order is not important (we have different order in LINUX and FreeBSD).
2786  * The first one, creates the device, and the second one simply attaches it.
2787  */
2788 
2789 /* Called when ptnetmap_memdev is attaching, to attach a new allocator in
2790  * the guest */
2791 struct netmap_mem_d *
2792 netmap_mem_pt_guest_attach(struct ptnetmap_memdev *ptn_dev, nm_memid_t mem_id)
2793 {
2794 	struct netmap_mem_d *nmd;
2795 	struct netmap_mem_ptg *ptnmd;
2796 
2797 	nmd = netmap_mem_pt_guest_get(mem_id);
2798 
2799 	/* assign this device to the guest allocator */
2800 	if (nmd) {
2801 		ptnmd = (struct netmap_mem_ptg *)nmd;
2802 		ptnmd->ptn_dev = ptn_dev;
2803 	}
2804 
2805 	return nmd;
2806 }
2807 
2808 /* Called when ptnet device is attaching */
2809 struct netmap_mem_d *
2810 netmap_mem_pt_guest_new(struct ifnet *ifp,
2811 			unsigned int nifp_offset,
2812 			unsigned int memid)
2813 {
2814 	struct netmap_mem_d *nmd;
2815 
2816 	if (ifp == NULL) {
2817 		return NULL;
2818 	}
2819 
2820 	nmd = netmap_mem_pt_guest_get((nm_memid_t)memid);
2821 
2822 	if (nmd) {
2823 		netmap_mem_pt_guest_ifp_add(nmd, ifp, nifp_offset);
2824 	}
2825 
2826 	return nmd;
2827 }
2828 
2829 #endif /* WITH_PTNETMAP_GUEST */
2830