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