1*718cf2ccSPedro F. Giffuni /*- 2*718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*718cf2ccSPedro F. Giffuni * 437e3a6d3SLuigi Rizzo * Copyright (C) 2012-2014 Matteo Landi 537e3a6d3SLuigi Rizzo * Copyright (C) 2012-2016 Luigi Rizzo 637e3a6d3SLuigi Rizzo * Copyright (C) 2012-2016 Giuseppe Lettieri 737e3a6d3SLuigi Rizzo * All rights reserved. 8ccdc3305SLuigi Rizzo * 9ccdc3305SLuigi Rizzo * Redistribution and use in source and binary forms, with or without 10ccdc3305SLuigi Rizzo * modification, are permitted provided that the following conditions 11ccdc3305SLuigi Rizzo * are met: 12ccdc3305SLuigi Rizzo * 1. Redistributions of source code must retain the above copyright 13ccdc3305SLuigi Rizzo * notice, this list of conditions and the following disclaimer. 14ccdc3305SLuigi Rizzo * 2. Redistributions in binary form must reproduce the above copyright 15ccdc3305SLuigi Rizzo * notice, this list of conditions and the following disclaimer in the 16ccdc3305SLuigi Rizzo * documentation and/or other materials provided with the distribution. 17ccdc3305SLuigi Rizzo * 18ccdc3305SLuigi Rizzo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19ccdc3305SLuigi Rizzo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20ccdc3305SLuigi Rizzo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21ccdc3305SLuigi Rizzo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22ccdc3305SLuigi Rizzo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23ccdc3305SLuigi Rizzo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24ccdc3305SLuigi Rizzo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25ccdc3305SLuigi Rizzo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26ccdc3305SLuigi Rizzo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27ccdc3305SLuigi Rizzo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28ccdc3305SLuigi Rizzo * SUCH DAMAGE. 29ccdc3305SLuigi Rizzo */ 30ccdc3305SLuigi Rizzo 318241616dSLuigi Rizzo #ifdef linux 32ce3ee1e7SLuigi Rizzo #include "bsd_glue.h" 338241616dSLuigi Rizzo #endif /* linux */ 348241616dSLuigi Rizzo 35ce3ee1e7SLuigi Rizzo #ifdef __APPLE__ 36ce3ee1e7SLuigi Rizzo #include "osx_glue.h" 37ce3ee1e7SLuigi Rizzo #endif /* __APPLE__ */ 388241616dSLuigi Rizzo 39ce3ee1e7SLuigi Rizzo #ifdef __FreeBSD__ 40ce3ee1e7SLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */ 41ce3ee1e7SLuigi Rizzo __FBSDID("$FreeBSD$"); 428241616dSLuigi Rizzo 43ce3ee1e7SLuigi Rizzo #include <sys/types.h> 44ce3ee1e7SLuigi Rizzo #include <sys/malloc.h> 4537e3a6d3SLuigi Rizzo #include <sys/kernel.h> /* MALLOC_DEFINE */ 46ce3ee1e7SLuigi Rizzo #include <sys/proc.h> 47ce3ee1e7SLuigi Rizzo #include <vm/vm.h> /* vtophys */ 48ce3ee1e7SLuigi Rizzo #include <vm/pmap.h> /* vtophys */ 49ce3ee1e7SLuigi Rizzo #include <sys/socket.h> /* sockaddrs */ 50ce3ee1e7SLuigi Rizzo #include <sys/selinfo.h> 51ce3ee1e7SLuigi Rizzo #include <sys/sysctl.h> 52ce3ee1e7SLuigi Rizzo #include <net/if.h> 53ce3ee1e7SLuigi Rizzo #include <net/if_var.h> 54ce3ee1e7SLuigi Rizzo #include <net/vnet.h> 55ce3ee1e7SLuigi Rizzo #include <machine/bus.h> /* bus_dmamap_* */ 56ce3ee1e7SLuigi Rizzo 5737e3a6d3SLuigi Rizzo /* M_NETMAP only used in here */ 5837e3a6d3SLuigi Rizzo MALLOC_DECLARE(M_NETMAP); 5937e3a6d3SLuigi Rizzo MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map"); 6037e3a6d3SLuigi Rizzo 61ce3ee1e7SLuigi Rizzo #endif /* __FreeBSD__ */ 62ce3ee1e7SLuigi Rizzo 6337e3a6d3SLuigi Rizzo #ifdef _WIN32 6437e3a6d3SLuigi Rizzo #include <win_glue.h> 6537e3a6d3SLuigi Rizzo #endif 6637e3a6d3SLuigi Rizzo 67ce3ee1e7SLuigi Rizzo #include <net/netmap.h> 68ce3ee1e7SLuigi Rizzo #include <dev/netmap/netmap_kern.h> 6937e3a6d3SLuigi Rizzo #include <net/netmap_virt.h> 70ce3ee1e7SLuigi Rizzo #include "netmap_mem2.h" 71ce3ee1e7SLuigi Rizzo 7237e3a6d3SLuigi Rizzo #ifdef _WIN32_USE_SMALL_GENERIC_DEVICES_MEMORY 7337e3a6d3SLuigi Rizzo #define NETMAP_BUF_MAX_NUM 8*4096 /* if too big takes too much time to allocate */ 7437e3a6d3SLuigi Rizzo #else 754bf50f18SLuigi Rizzo #define NETMAP_BUF_MAX_NUM 20*4096*2 /* large machine */ 7637e3a6d3SLuigi Rizzo #endif 774bf50f18SLuigi Rizzo 784bf50f18SLuigi Rizzo #define NETMAP_POOL_MAX_NAMSZ 32 794bf50f18SLuigi Rizzo 804bf50f18SLuigi Rizzo 814bf50f18SLuigi Rizzo enum { 824bf50f18SLuigi Rizzo NETMAP_IF_POOL = 0, 834bf50f18SLuigi Rizzo NETMAP_RING_POOL, 844bf50f18SLuigi Rizzo NETMAP_BUF_POOL, 854bf50f18SLuigi Rizzo NETMAP_POOLS_NR 864bf50f18SLuigi Rizzo }; 874bf50f18SLuigi Rizzo 884bf50f18SLuigi Rizzo 894bf50f18SLuigi Rizzo struct netmap_obj_params { 904bf50f18SLuigi Rizzo u_int size; 914bf50f18SLuigi Rizzo u_int num; 92c3e9b4dbSLuiz Otavio O Souza 93c3e9b4dbSLuiz Otavio O Souza u_int last_size; 94c3e9b4dbSLuiz Otavio O Souza u_int last_num; 954bf50f18SLuigi Rizzo }; 96847bf383SLuigi Rizzo 974bf50f18SLuigi Rizzo struct netmap_obj_pool { 984bf50f18SLuigi Rizzo char name[NETMAP_POOL_MAX_NAMSZ]; /* name of the allocator */ 994bf50f18SLuigi Rizzo 1004bf50f18SLuigi Rizzo /* ---------------------------------------------------*/ 1014bf50f18SLuigi Rizzo /* these are only meaningful if the pool is finalized */ 1024bf50f18SLuigi Rizzo /* (see 'finalized' field in netmap_mem_d) */ 1034bf50f18SLuigi Rizzo u_int objtotal; /* actual total number of objects. */ 1044bf50f18SLuigi Rizzo u_int memtotal; /* actual total memory space */ 1054bf50f18SLuigi Rizzo u_int numclusters; /* actual number of clusters */ 1064bf50f18SLuigi Rizzo 1074bf50f18SLuigi Rizzo u_int objfree; /* number of free objects. */ 1084bf50f18SLuigi Rizzo 1094bf50f18SLuigi Rizzo struct lut_entry *lut; /* virt,phys addresses, objtotal entries */ 1104bf50f18SLuigi Rizzo uint32_t *bitmap; /* one bit per buffer, 1 means free */ 1114bf50f18SLuigi Rizzo uint32_t bitmap_slots; /* number of uint32 entries in bitmap */ 1124bf50f18SLuigi Rizzo /* ---------------------------------------------------*/ 1134bf50f18SLuigi Rizzo 1144bf50f18SLuigi Rizzo /* limits */ 1154bf50f18SLuigi Rizzo u_int objminsize; /* minimum object size */ 1164bf50f18SLuigi Rizzo u_int objmaxsize; /* maximum object size */ 1174bf50f18SLuigi Rizzo u_int nummin; /* minimum number of objects */ 1184bf50f18SLuigi Rizzo u_int nummax; /* maximum number of objects */ 1194bf50f18SLuigi Rizzo 1204bf50f18SLuigi Rizzo /* these are changed only by config */ 1214bf50f18SLuigi Rizzo u_int _objtotal; /* total number of objects */ 1224bf50f18SLuigi Rizzo u_int _objsize; /* object size */ 1234bf50f18SLuigi Rizzo u_int _clustsize; /* cluster size */ 1244bf50f18SLuigi Rizzo u_int _clustentries; /* objects per cluster */ 1254bf50f18SLuigi Rizzo u_int _numclusters; /* number of clusters */ 1264bf50f18SLuigi Rizzo 1274bf50f18SLuigi Rizzo /* requested values */ 1284bf50f18SLuigi Rizzo u_int r_objtotal; 1294bf50f18SLuigi Rizzo u_int r_objsize; 1304bf50f18SLuigi Rizzo }; 1314bf50f18SLuigi Rizzo 132847bf383SLuigi Rizzo #define NMA_LOCK_T NM_MTX_T 1334bf50f18SLuigi Rizzo 134847bf383SLuigi Rizzo 135847bf383SLuigi Rizzo struct netmap_mem_ops { 13637e3a6d3SLuigi Rizzo int (*nmd_get_lut)(struct netmap_mem_d *, struct netmap_lut*); 137847bf383SLuigi Rizzo int (*nmd_get_info)(struct netmap_mem_d *, u_int *size, 138847bf383SLuigi Rizzo u_int *memflags, uint16_t *id); 139847bf383SLuigi Rizzo 140847bf383SLuigi Rizzo vm_paddr_t (*nmd_ofstophys)(struct netmap_mem_d *, vm_ooffset_t); 141847bf383SLuigi Rizzo int (*nmd_config)(struct netmap_mem_d *); 142847bf383SLuigi Rizzo int (*nmd_finalize)(struct netmap_mem_d *); 143847bf383SLuigi Rizzo void (*nmd_deref)(struct netmap_mem_d *); 144847bf383SLuigi Rizzo ssize_t (*nmd_if_offset)(struct netmap_mem_d *, const void *vaddr); 145847bf383SLuigi Rizzo void (*nmd_delete)(struct netmap_mem_d *); 146847bf383SLuigi Rizzo 147c3e9b4dbSLuiz Otavio O Souza struct netmap_if * (*nmd_if_new)(struct netmap_adapter *, 148c3e9b4dbSLuiz Otavio O Souza struct netmap_priv_d *); 149847bf383SLuigi Rizzo void (*nmd_if_delete)(struct netmap_adapter *, struct netmap_if *); 150847bf383SLuigi Rizzo int (*nmd_rings_create)(struct netmap_adapter *); 151847bf383SLuigi Rizzo void (*nmd_rings_delete)(struct netmap_adapter *); 152847bf383SLuigi Rizzo }; 1534bf50f18SLuigi Rizzo 1544bf50f18SLuigi Rizzo struct netmap_mem_d { 1554bf50f18SLuigi Rizzo NMA_LOCK_T nm_mtx; /* protect the allocator */ 1564bf50f18SLuigi Rizzo u_int nm_totalsize; /* shorthand */ 1574bf50f18SLuigi Rizzo 1584bf50f18SLuigi Rizzo u_int flags; 1594bf50f18SLuigi Rizzo #define NETMAP_MEM_FINALIZED 0x1 /* preallocation done */ 160c3e9b4dbSLuiz Otavio O Souza #define NETMAP_MEM_HIDDEN 0x8 /* beeing prepared */ 1614bf50f18SLuigi Rizzo int lasterr; /* last error for curr config */ 162847bf383SLuigi Rizzo int active; /* active users */ 163847bf383SLuigi Rizzo int refcount; 1644bf50f18SLuigi Rizzo /* the three allocators */ 1654bf50f18SLuigi Rizzo struct netmap_obj_pool pools[NETMAP_POOLS_NR]; 1664bf50f18SLuigi Rizzo 1674bf50f18SLuigi Rizzo nm_memid_t nm_id; /* allocator identifier */ 1684bf50f18SLuigi Rizzo int nm_grp; /* iommu groupd id */ 1694bf50f18SLuigi Rizzo 1704bf50f18SLuigi Rizzo /* list of all existing allocators, sorted by nm_id */ 1714bf50f18SLuigi Rizzo struct netmap_mem_d *prev, *next; 172847bf383SLuigi Rizzo 173847bf383SLuigi Rizzo struct netmap_mem_ops *ops; 174c3e9b4dbSLuiz Otavio O Souza 175c3e9b4dbSLuiz Otavio O Souza struct netmap_obj_params params[NETMAP_POOLS_NR]; 176c3e9b4dbSLuiz Otavio O Souza 177c3e9b4dbSLuiz Otavio O Souza #define NM_MEM_NAMESZ 16 178c3e9b4dbSLuiz Otavio O Souza char name[NM_MEM_NAMESZ]; 1794bf50f18SLuigi Rizzo }; 1804bf50f18SLuigi Rizzo 18137e3a6d3SLuigi Rizzo /* 18237e3a6d3SLuigi Rizzo * XXX need to fix the case of t0 == void 18337e3a6d3SLuigi Rizzo */ 184847bf383SLuigi Rizzo #define NMD_DEFCB(t0, name) \ 185847bf383SLuigi Rizzo t0 \ 186847bf383SLuigi Rizzo netmap_mem_##name(struct netmap_mem_d *nmd) \ 187847bf383SLuigi Rizzo { \ 188847bf383SLuigi Rizzo return nmd->ops->nmd_##name(nmd); \ 189847bf383SLuigi Rizzo } 190847bf383SLuigi Rizzo 191847bf383SLuigi Rizzo #define NMD_DEFCB1(t0, name, t1) \ 192847bf383SLuigi Rizzo t0 \ 193847bf383SLuigi Rizzo netmap_mem_##name(struct netmap_mem_d *nmd, t1 a1) \ 194847bf383SLuigi Rizzo { \ 195847bf383SLuigi Rizzo return nmd->ops->nmd_##name(nmd, a1); \ 196847bf383SLuigi Rizzo } 197847bf383SLuigi Rizzo 198847bf383SLuigi Rizzo #define NMD_DEFCB3(t0, name, t1, t2, t3) \ 199847bf383SLuigi Rizzo t0 \ 200847bf383SLuigi Rizzo netmap_mem_##name(struct netmap_mem_d *nmd, t1 a1, t2 a2, t3 a3) \ 201847bf383SLuigi Rizzo { \ 202847bf383SLuigi Rizzo return nmd->ops->nmd_##name(nmd, a1, a2, a3); \ 203847bf383SLuigi Rizzo } 204847bf383SLuigi Rizzo 205847bf383SLuigi Rizzo #define NMD_DEFNACB(t0, name) \ 206847bf383SLuigi Rizzo t0 \ 207847bf383SLuigi Rizzo netmap_mem_##name(struct netmap_adapter *na) \ 208847bf383SLuigi Rizzo { \ 209847bf383SLuigi Rizzo return na->nm_mem->ops->nmd_##name(na); \ 210847bf383SLuigi Rizzo } 211847bf383SLuigi Rizzo 212847bf383SLuigi Rizzo #define NMD_DEFNACB1(t0, name, t1) \ 213847bf383SLuigi Rizzo t0 \ 214847bf383SLuigi Rizzo netmap_mem_##name(struct netmap_adapter *na, t1 a1) \ 215847bf383SLuigi Rizzo { \ 216847bf383SLuigi Rizzo return na->nm_mem->ops->nmd_##name(na, a1); \ 217847bf383SLuigi Rizzo } 218847bf383SLuigi Rizzo 21937e3a6d3SLuigi Rizzo NMD_DEFCB1(int, get_lut, struct netmap_lut *); 220847bf383SLuigi Rizzo NMD_DEFCB3(int, get_info, u_int *, u_int *, uint16_t *); 221847bf383SLuigi Rizzo NMD_DEFCB1(vm_paddr_t, ofstophys, vm_ooffset_t); 222847bf383SLuigi Rizzo static int netmap_mem_config(struct netmap_mem_d *); 223847bf383SLuigi Rizzo NMD_DEFCB(int, config); 224847bf383SLuigi Rizzo NMD_DEFCB1(ssize_t, if_offset, const void *); 225847bf383SLuigi Rizzo NMD_DEFCB(void, delete); 226847bf383SLuigi Rizzo 227c3e9b4dbSLuiz Otavio O Souza NMD_DEFNACB1(struct netmap_if *, if_new, struct netmap_priv_d *); 228847bf383SLuigi Rizzo NMD_DEFNACB1(void, if_delete, struct netmap_if *); 229847bf383SLuigi Rizzo NMD_DEFNACB(int, rings_create); 230847bf383SLuigi Rizzo NMD_DEFNACB(void, rings_delete); 231847bf383SLuigi Rizzo 232847bf383SLuigi Rizzo static int netmap_mem_map(struct netmap_obj_pool *, struct netmap_adapter *); 233847bf383SLuigi Rizzo static int netmap_mem_unmap(struct netmap_obj_pool *, struct netmap_adapter *); 23437e3a6d3SLuigi Rizzo static int nm_mem_assign_group(struct netmap_mem_d *, struct device *); 235c3e9b4dbSLuiz Otavio O Souza static void nm_mem_release_id(struct netmap_mem_d *); 236c3e9b4dbSLuiz Otavio O Souza 237c3e9b4dbSLuiz Otavio O Souza nm_memid_t 238c3e9b4dbSLuiz Otavio O Souza netmap_mem_get_id(struct netmap_mem_d *nmd) 239c3e9b4dbSLuiz Otavio O Souza { 240c3e9b4dbSLuiz Otavio O Souza return nmd->nm_id; 241c3e9b4dbSLuiz Otavio O Souza } 242847bf383SLuigi Rizzo 243847bf383SLuigi Rizzo #define NMA_LOCK_INIT(n) NM_MTX_INIT((n)->nm_mtx) 244847bf383SLuigi Rizzo #define NMA_LOCK_DESTROY(n) NM_MTX_DESTROY((n)->nm_mtx) 245847bf383SLuigi Rizzo #define NMA_LOCK(n) NM_MTX_LOCK((n)->nm_mtx) 246847bf383SLuigi Rizzo #define NMA_UNLOCK(n) NM_MTX_UNLOCK((n)->nm_mtx) 247847bf383SLuigi Rizzo 248847bf383SLuigi Rizzo #ifdef NM_DEBUG_MEM_PUTGET 249847bf383SLuigi Rizzo #define NM_DBG_REFC(nmd, func, line) \ 250c3e9b4dbSLuiz Otavio O Souza nm_prinf("%s:%d mem[%d] -> %d\n", func, line, (nmd)->nm_id, (nmd)->refcount); 251847bf383SLuigi Rizzo #else 252847bf383SLuigi Rizzo #define NM_DBG_REFC(nmd, func, line) 253847bf383SLuigi Rizzo #endif 254847bf383SLuigi Rizzo 255c3e9b4dbSLuiz Otavio O Souza /* circular list of all existing allocators */ 256c3e9b4dbSLuiz Otavio O Souza static struct netmap_mem_d *netmap_last_mem_d = &nm_mem; 257c3e9b4dbSLuiz Otavio O Souza NM_MTX_T nm_mem_list_lock; 258c3e9b4dbSLuiz Otavio O Souza 259c3e9b4dbSLuiz Otavio O Souza struct netmap_mem_d * 260c3e9b4dbSLuiz Otavio O Souza __netmap_mem_get(struct netmap_mem_d *nmd, const char *func, int line) 261847bf383SLuigi Rizzo { 262c3e9b4dbSLuiz Otavio O Souza NM_MTX_LOCK(nm_mem_list_lock); 263847bf383SLuigi Rizzo nmd->refcount++; 264847bf383SLuigi Rizzo NM_DBG_REFC(nmd, func, line); 265c3e9b4dbSLuiz Otavio O Souza NM_MTX_UNLOCK(nm_mem_list_lock); 266c3e9b4dbSLuiz Otavio O Souza return nmd; 267847bf383SLuigi Rizzo } 268847bf383SLuigi Rizzo 269c3e9b4dbSLuiz Otavio O Souza void 270c3e9b4dbSLuiz Otavio O Souza __netmap_mem_put(struct netmap_mem_d *nmd, const char *func, int line) 271847bf383SLuigi Rizzo { 272847bf383SLuigi Rizzo int last; 273c3e9b4dbSLuiz Otavio O Souza NM_MTX_LOCK(nm_mem_list_lock); 274847bf383SLuigi Rizzo last = (--nmd->refcount == 0); 275c3e9b4dbSLuiz Otavio O Souza if (last) 276c3e9b4dbSLuiz Otavio O Souza nm_mem_release_id(nmd); 277847bf383SLuigi Rizzo NM_DBG_REFC(nmd, func, line); 278c3e9b4dbSLuiz Otavio O Souza NM_MTX_UNLOCK(nm_mem_list_lock); 279847bf383SLuigi Rizzo if (last) 280847bf383SLuigi Rizzo netmap_mem_delete(nmd); 281847bf383SLuigi Rizzo } 282847bf383SLuigi Rizzo 283847bf383SLuigi Rizzo int 284847bf383SLuigi Rizzo netmap_mem_finalize(struct netmap_mem_d *nmd, struct netmap_adapter *na) 285847bf383SLuigi Rizzo { 286847bf383SLuigi Rizzo if (nm_mem_assign_group(nmd, na->pdev) < 0) { 287847bf383SLuigi Rizzo return ENOMEM; 288847bf383SLuigi Rizzo } else { 28937e3a6d3SLuigi Rizzo NMA_LOCK(nmd); 29037e3a6d3SLuigi Rizzo nmd->lasterr = nmd->ops->nmd_finalize(nmd); 29137e3a6d3SLuigi Rizzo NMA_UNLOCK(nmd); 292847bf383SLuigi Rizzo } 293847bf383SLuigi Rizzo 294847bf383SLuigi Rizzo if (!nmd->lasterr && na->pdev) 295847bf383SLuigi Rizzo netmap_mem_map(&nmd->pools[NETMAP_BUF_POOL], na); 296847bf383SLuigi Rizzo 297847bf383SLuigi Rizzo return nmd->lasterr; 298847bf383SLuigi Rizzo } 299847bf383SLuigi Rizzo 300847bf383SLuigi Rizzo void 301847bf383SLuigi Rizzo netmap_mem_deref(struct netmap_mem_d *nmd, struct netmap_adapter *na) 302847bf383SLuigi Rizzo { 303847bf383SLuigi Rizzo NMA_LOCK(nmd); 304847bf383SLuigi Rizzo netmap_mem_unmap(&nmd->pools[NETMAP_BUF_POOL], na); 30537e3a6d3SLuigi Rizzo if (nmd->active == 1) { 30637e3a6d3SLuigi Rizzo u_int i; 30737e3a6d3SLuigi Rizzo 30837e3a6d3SLuigi Rizzo /* 30937e3a6d3SLuigi Rizzo * Reset the allocator when it falls out of use so that any 31037e3a6d3SLuigi Rizzo * pool resources leaked by unclean application exits are 31137e3a6d3SLuigi Rizzo * reclaimed. 31237e3a6d3SLuigi Rizzo */ 31337e3a6d3SLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 31437e3a6d3SLuigi Rizzo struct netmap_obj_pool *p; 31537e3a6d3SLuigi Rizzo u_int j; 31637e3a6d3SLuigi Rizzo 31737e3a6d3SLuigi Rizzo p = &nmd->pools[i]; 31837e3a6d3SLuigi Rizzo p->objfree = p->objtotal; 31937e3a6d3SLuigi Rizzo /* 32037e3a6d3SLuigi Rizzo * Reproduce the net effect of the M_ZERO malloc() 32137e3a6d3SLuigi Rizzo * and marking of free entries in the bitmap that 32237e3a6d3SLuigi Rizzo * occur in finalize_obj_allocator() 32337e3a6d3SLuigi Rizzo */ 32437e3a6d3SLuigi Rizzo memset(p->bitmap, 32537e3a6d3SLuigi Rizzo '\0', 32637e3a6d3SLuigi Rizzo sizeof(uint32_t) * ((p->objtotal + 31) / 32)); 32737e3a6d3SLuigi Rizzo 32837e3a6d3SLuigi Rizzo /* 32937e3a6d3SLuigi Rizzo * Set all the bits in the bitmap that have 33037e3a6d3SLuigi Rizzo * corresponding buffers to 1 to indicate they are 33137e3a6d3SLuigi Rizzo * free. 33237e3a6d3SLuigi Rizzo */ 33337e3a6d3SLuigi Rizzo for (j = 0; j < p->objtotal; j++) { 33437e3a6d3SLuigi Rizzo if (p->lut[j].vaddr != NULL) { 33537e3a6d3SLuigi Rizzo p->bitmap[ (j>>5) ] |= ( 1 << (j & 31) ); 33637e3a6d3SLuigi Rizzo } 33737e3a6d3SLuigi Rizzo } 33837e3a6d3SLuigi Rizzo } 33937e3a6d3SLuigi Rizzo 34037e3a6d3SLuigi Rizzo /* 34137e3a6d3SLuigi Rizzo * Per netmap_mem_finalize_all(), 34237e3a6d3SLuigi Rizzo * buffers 0 and 1 are reserved 34337e3a6d3SLuigi Rizzo */ 34437e3a6d3SLuigi Rizzo nmd->pools[NETMAP_BUF_POOL].objfree -= 2; 34537e3a6d3SLuigi Rizzo if (nmd->pools[NETMAP_BUF_POOL].bitmap) { 34637e3a6d3SLuigi Rizzo /* XXX This check is a workaround that prevents a 34737e3a6d3SLuigi Rizzo * NULL pointer crash which currently happens only 348844a6f0cSLuigi Rizzo * with ptnetmap guests. 349844a6f0cSLuigi Rizzo * Removed shared-info --> is the bug still there? */ 35037e3a6d3SLuigi Rizzo nmd->pools[NETMAP_BUF_POOL].bitmap[0] = ~3; 35137e3a6d3SLuigi Rizzo } 35237e3a6d3SLuigi Rizzo } 35337e3a6d3SLuigi Rizzo nmd->ops->nmd_deref(nmd); 35437e3a6d3SLuigi Rizzo 355847bf383SLuigi Rizzo NMA_UNLOCK(nmd); 356847bf383SLuigi Rizzo } 357847bf383SLuigi Rizzo 358847bf383SLuigi Rizzo 3594bf50f18SLuigi Rizzo /* accessor functions */ 36037e3a6d3SLuigi Rizzo static int 361847bf383SLuigi Rizzo netmap_mem2_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut) 3624bf50f18SLuigi Rizzo { 363847bf383SLuigi Rizzo lut->lut = nmd->pools[NETMAP_BUF_POOL].lut; 364847bf383SLuigi Rizzo lut->objtotal = nmd->pools[NETMAP_BUF_POOL].objtotal; 365847bf383SLuigi Rizzo lut->objsize = nmd->pools[NETMAP_BUF_POOL]._objsize; 36637e3a6d3SLuigi Rizzo 36737e3a6d3SLuigi Rizzo return 0; 3684bf50f18SLuigi Rizzo } 3694bf50f18SLuigi Rizzo 37037e3a6d3SLuigi Rizzo static struct netmap_obj_params netmap_min_priv_params[NETMAP_POOLS_NR] = { 371f0ea3689SLuigi Rizzo [NETMAP_IF_POOL] = { 372f0ea3689SLuigi Rizzo .size = 1024, 37337e3a6d3SLuigi Rizzo .num = 2, 374f0ea3689SLuigi Rizzo }, 375f0ea3689SLuigi Rizzo [NETMAP_RING_POOL] = { 376f0ea3689SLuigi Rizzo .size = 5*PAGE_SIZE, 377f0ea3689SLuigi Rizzo .num = 4, 378f0ea3689SLuigi Rizzo }, 379f0ea3689SLuigi Rizzo [NETMAP_BUF_POOL] = { 380f0ea3689SLuigi Rizzo .size = 2048, 381f0ea3689SLuigi Rizzo .num = 4098, 382f0ea3689SLuigi Rizzo }, 383f0ea3689SLuigi Rizzo }; 384f0ea3689SLuigi Rizzo 385ccdc3305SLuigi Rizzo 3862579e2d7SLuigi Rizzo /* 3872579e2d7SLuigi Rizzo * nm_mem is the memory allocator used for all physical interfaces 3882579e2d7SLuigi Rizzo * running in netmap mode. 3892579e2d7SLuigi Rizzo * Virtual (VALE) ports will have each its own allocator. 3902579e2d7SLuigi Rizzo */ 391847bf383SLuigi Rizzo extern struct netmap_mem_ops netmap_mem_global_ops; /* forward */ 392ce3ee1e7SLuigi Rizzo struct netmap_mem_d nm_mem = { /* Our memory allocator. */ 3938241616dSLuigi Rizzo .pools = { 3948241616dSLuigi Rizzo [NETMAP_IF_POOL] = { 3958241616dSLuigi Rizzo .name = "netmap_if", 3968241616dSLuigi Rizzo .objminsize = sizeof(struct netmap_if), 3978241616dSLuigi Rizzo .objmaxsize = 4096, 3988241616dSLuigi Rizzo .nummin = 10, /* don't be stingy */ 3998241616dSLuigi Rizzo .nummax = 10000, /* XXX very large */ 4008241616dSLuigi Rizzo }, 4018241616dSLuigi Rizzo [NETMAP_RING_POOL] = { 4028241616dSLuigi Rizzo .name = "netmap_ring", 4038241616dSLuigi Rizzo .objminsize = sizeof(struct netmap_ring), 4048241616dSLuigi Rizzo .objmaxsize = 32*PAGE_SIZE, 4058241616dSLuigi Rizzo .nummin = 2, 4068241616dSLuigi Rizzo .nummax = 1024, 4078241616dSLuigi Rizzo }, 4088241616dSLuigi Rizzo [NETMAP_BUF_POOL] = { 4098241616dSLuigi Rizzo .name = "netmap_buf", 4108241616dSLuigi Rizzo .objminsize = 64, 4118241616dSLuigi Rizzo .objmaxsize = 65536, 4128241616dSLuigi Rizzo .nummin = 4, 4138241616dSLuigi Rizzo .nummax = 1000000, /* one million! */ 4148241616dSLuigi Rizzo }, 4158241616dSLuigi Rizzo }, 416f0ea3689SLuigi Rizzo 417c3e9b4dbSLuiz Otavio O Souza .params = { 418c3e9b4dbSLuiz Otavio O Souza [NETMAP_IF_POOL] = { 419c3e9b4dbSLuiz Otavio O Souza .size = 1024, 420c3e9b4dbSLuiz Otavio O Souza .num = 100, 421c3e9b4dbSLuiz Otavio O Souza }, 422c3e9b4dbSLuiz Otavio O Souza [NETMAP_RING_POOL] = { 423c3e9b4dbSLuiz Otavio O Souza .size = 9*PAGE_SIZE, 424c3e9b4dbSLuiz Otavio O Souza .num = 200, 425c3e9b4dbSLuiz Otavio O Souza }, 426c3e9b4dbSLuiz Otavio O Souza [NETMAP_BUF_POOL] = { 427c3e9b4dbSLuiz Otavio O Souza .size = 2048, 428c3e9b4dbSLuiz Otavio O Souza .num = NETMAP_BUF_MAX_NUM, 429c3e9b4dbSLuiz Otavio O Souza }, 430c3e9b4dbSLuiz Otavio O Souza }, 431c3e9b4dbSLuiz Otavio O Souza 432f0ea3689SLuigi Rizzo .nm_id = 1, 4334bf50f18SLuigi Rizzo .nm_grp = -1, 434f0ea3689SLuigi Rizzo 435f0ea3689SLuigi Rizzo .prev = &nm_mem, 436f0ea3689SLuigi Rizzo .next = &nm_mem, 437847bf383SLuigi Rizzo 438c3e9b4dbSLuiz Otavio O Souza .ops = &netmap_mem_global_ops, 439c3e9b4dbSLuiz Otavio O Souza 440c3e9b4dbSLuiz Otavio O Souza .name = "1" 441ccdc3305SLuigi Rizzo }; 442ccdc3305SLuigi Rizzo 443ce3ee1e7SLuigi Rizzo 444ce3ee1e7SLuigi Rizzo /* blueprint for the private memory allocators */ 445847bf383SLuigi Rizzo extern struct netmap_mem_ops netmap_mem_private_ops; /* forward */ 44637e3a6d3SLuigi Rizzo /* XXX clang is not happy about using name as a print format */ 44737e3a6d3SLuigi Rizzo static const struct netmap_mem_d nm_blueprint = { 448ce3ee1e7SLuigi Rizzo .pools = { 449ce3ee1e7SLuigi Rizzo [NETMAP_IF_POOL] = { 450ce3ee1e7SLuigi Rizzo .name = "%s_if", 451ce3ee1e7SLuigi Rizzo .objminsize = sizeof(struct netmap_if), 452ce3ee1e7SLuigi Rizzo .objmaxsize = 4096, 453ce3ee1e7SLuigi Rizzo .nummin = 1, 454f0ea3689SLuigi Rizzo .nummax = 100, 455ce3ee1e7SLuigi Rizzo }, 456ce3ee1e7SLuigi Rizzo [NETMAP_RING_POOL] = { 457ce3ee1e7SLuigi Rizzo .name = "%s_ring", 458ce3ee1e7SLuigi Rizzo .objminsize = sizeof(struct netmap_ring), 459ce3ee1e7SLuigi Rizzo .objmaxsize = 32*PAGE_SIZE, 460ce3ee1e7SLuigi Rizzo .nummin = 2, 461ce3ee1e7SLuigi Rizzo .nummax = 1024, 462ce3ee1e7SLuigi Rizzo }, 463ce3ee1e7SLuigi Rizzo [NETMAP_BUF_POOL] = { 464ce3ee1e7SLuigi Rizzo .name = "%s_buf", 465ce3ee1e7SLuigi Rizzo .objminsize = 64, 466ce3ee1e7SLuigi Rizzo .objmaxsize = 65536, 467ce3ee1e7SLuigi Rizzo .nummin = 4, 468ce3ee1e7SLuigi Rizzo .nummax = 1000000, /* one million! */ 469ce3ee1e7SLuigi Rizzo }, 470ce3ee1e7SLuigi Rizzo }, 471ce3ee1e7SLuigi Rizzo 472c3e9b4dbSLuiz Otavio O Souza .nm_grp = -1, 473c3e9b4dbSLuiz Otavio O Souza 474ce3ee1e7SLuigi Rizzo .flags = NETMAP_MEM_PRIVATE, 475847bf383SLuigi Rizzo 476c3e9b4dbSLuiz Otavio O Souza .ops = &netmap_mem_global_ops, 477ce3ee1e7SLuigi Rizzo }; 478ce3ee1e7SLuigi Rizzo 4798241616dSLuigi Rizzo /* memory allocator related sysctls */ 4808241616dSLuigi Rizzo 4818241616dSLuigi Rizzo #define STRINGIFY(x) #x 4828241616dSLuigi Rizzo 483ce3ee1e7SLuigi Rizzo 4848241616dSLuigi Rizzo #define DECLARE_SYSCTLS(id, name) \ 48537e3a6d3SLuigi Rizzo SYSBEGIN(mem2_ ## name); \ 4868241616dSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, name##_size, \ 487c3e9b4dbSLuiz Otavio O Souza CTLFLAG_RW, &nm_mem.params[id].size, 0, "Requested size of netmap " STRINGIFY(name) "s"); \ 4888241616dSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_size, \ 4898241616dSLuigi Rizzo CTLFLAG_RD, &nm_mem.pools[id]._objsize, 0, "Current size of netmap " STRINGIFY(name) "s"); \ 4908241616dSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, name##_num, \ 491c3e9b4dbSLuiz Otavio O Souza CTLFLAG_RW, &nm_mem.params[id].num, 0, "Requested number of netmap " STRINGIFY(name) "s"); \ 4928241616dSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, name##_curr_num, \ 493f0ea3689SLuigi Rizzo CTLFLAG_RD, &nm_mem.pools[id].objtotal, 0, "Current number of netmap " STRINGIFY(name) "s"); \ 494f0ea3689SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_size, \ 495f0ea3689SLuigi Rizzo CTLFLAG_RW, &netmap_min_priv_params[id].size, 0, \ 496f0ea3689SLuigi Rizzo "Default size of private netmap " STRINGIFY(name) "s"); \ 497f0ea3689SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, priv_##name##_num, \ 498f0ea3689SLuigi Rizzo CTLFLAG_RW, &netmap_min_priv_params[id].num, 0, \ 49937e3a6d3SLuigi Rizzo "Default number of private netmap " STRINGIFY(name) "s"); \ 50037e3a6d3SLuigi Rizzo SYSEND 5018241616dSLuigi Rizzo 502984ff0d9SEd Maste SYSCTL_DECL(_dev_netmap); 5038241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_IF_POOL, if); 5048241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_RING_POOL, ring); 5058241616dSLuigi Rizzo DECLARE_SYSCTLS(NETMAP_BUF_POOL, buf); 506ccdc3305SLuigi Rizzo 507c3e9b4dbSLuiz Otavio O Souza /* call with nm_mem_list_lock held */ 508f0ea3689SLuigi Rizzo static int 50937e3a6d3SLuigi Rizzo nm_mem_assign_id_locked(struct netmap_mem_d *nmd) 510f0ea3689SLuigi Rizzo { 511f0ea3689SLuigi Rizzo nm_memid_t id; 512f0ea3689SLuigi Rizzo struct netmap_mem_d *scan = netmap_last_mem_d; 513f0ea3689SLuigi Rizzo int error = ENOMEM; 514f0ea3689SLuigi Rizzo 515f0ea3689SLuigi Rizzo do { 516f0ea3689SLuigi Rizzo /* we rely on unsigned wrap around */ 517f0ea3689SLuigi Rizzo id = scan->nm_id + 1; 518f0ea3689SLuigi Rizzo if (id == 0) /* reserve 0 as error value */ 519f0ea3689SLuigi Rizzo id = 1; 520f0ea3689SLuigi Rizzo scan = scan->next; 521f0ea3689SLuigi Rizzo if (id != scan->nm_id) { 522f0ea3689SLuigi Rizzo nmd->nm_id = id; 523f0ea3689SLuigi Rizzo nmd->prev = scan->prev; 524f0ea3689SLuigi Rizzo nmd->next = scan; 525f0ea3689SLuigi Rizzo scan->prev->next = nmd; 526f0ea3689SLuigi Rizzo scan->prev = nmd; 527f0ea3689SLuigi Rizzo netmap_last_mem_d = nmd; 528c3e9b4dbSLuiz Otavio O Souza nmd->refcount = 1; 529c3e9b4dbSLuiz Otavio O Souza NM_DBG_REFC(nmd, __FUNCTION__, __LINE__); 530f0ea3689SLuigi Rizzo error = 0; 531f0ea3689SLuigi Rizzo break; 532f0ea3689SLuigi Rizzo } 533f0ea3689SLuigi Rizzo } while (scan != netmap_last_mem_d); 534f0ea3689SLuigi Rizzo 535f0ea3689SLuigi Rizzo return error; 536f0ea3689SLuigi Rizzo } 537f0ea3689SLuigi Rizzo 538c3e9b4dbSLuiz Otavio O Souza /* call with nm_mem_list_lock *not* held */ 53937e3a6d3SLuigi Rizzo static int 54037e3a6d3SLuigi Rizzo nm_mem_assign_id(struct netmap_mem_d *nmd) 54137e3a6d3SLuigi Rizzo { 54237e3a6d3SLuigi Rizzo int ret; 54337e3a6d3SLuigi Rizzo 544c3e9b4dbSLuiz Otavio O Souza NM_MTX_LOCK(nm_mem_list_lock); 54537e3a6d3SLuigi Rizzo ret = nm_mem_assign_id_locked(nmd); 546c3e9b4dbSLuiz Otavio O Souza NM_MTX_UNLOCK(nm_mem_list_lock); 54737e3a6d3SLuigi Rizzo 54837e3a6d3SLuigi Rizzo return ret; 54937e3a6d3SLuigi Rizzo } 55037e3a6d3SLuigi Rizzo 551c3e9b4dbSLuiz Otavio O Souza /* call with nm_mem_list_lock held */ 552f0ea3689SLuigi Rizzo static void 553f0ea3689SLuigi Rizzo nm_mem_release_id(struct netmap_mem_d *nmd) 554f0ea3689SLuigi Rizzo { 555f0ea3689SLuigi Rizzo nmd->prev->next = nmd->next; 556f0ea3689SLuigi Rizzo nmd->next->prev = nmd->prev; 557f0ea3689SLuigi Rizzo 558f0ea3689SLuigi Rizzo if (netmap_last_mem_d == nmd) 559f0ea3689SLuigi Rizzo netmap_last_mem_d = nmd->prev; 560f0ea3689SLuigi Rizzo 561f0ea3689SLuigi Rizzo nmd->prev = nmd->next = NULL; 562c3e9b4dbSLuiz Otavio O Souza } 563f0ea3689SLuigi Rizzo 564c3e9b4dbSLuiz Otavio O Souza struct netmap_mem_d * 565c3e9b4dbSLuiz Otavio O Souza netmap_mem_find(nm_memid_t id) 566c3e9b4dbSLuiz Otavio O Souza { 567c3e9b4dbSLuiz Otavio O Souza struct netmap_mem_d *nmd; 568c3e9b4dbSLuiz Otavio O Souza 569c3e9b4dbSLuiz Otavio O Souza NM_MTX_LOCK(nm_mem_list_lock); 570c3e9b4dbSLuiz Otavio O Souza nmd = netmap_last_mem_d; 571c3e9b4dbSLuiz Otavio O Souza do { 572c3e9b4dbSLuiz Otavio O Souza if (!(nmd->flags & NETMAP_MEM_HIDDEN) && nmd->nm_id == id) { 573c3e9b4dbSLuiz Otavio O Souza nmd->refcount++; 574c3e9b4dbSLuiz Otavio O Souza NM_DBG_REFC(nmd, __FUNCTION__, __LINE__); 575c3e9b4dbSLuiz Otavio O Souza NM_MTX_UNLOCK(nm_mem_list_lock); 576c3e9b4dbSLuiz Otavio O Souza return nmd; 577c3e9b4dbSLuiz Otavio O Souza } 578c3e9b4dbSLuiz Otavio O Souza nmd = nmd->next; 579c3e9b4dbSLuiz Otavio O Souza } while (nmd != netmap_last_mem_d); 580c3e9b4dbSLuiz Otavio O Souza NM_MTX_UNLOCK(nm_mem_list_lock); 581c3e9b4dbSLuiz Otavio O Souza return NULL; 582f0ea3689SLuigi Rizzo } 583f0ea3689SLuigi Rizzo 5844bf50f18SLuigi Rizzo static int 58537e3a6d3SLuigi Rizzo nm_mem_assign_group(struct netmap_mem_d *nmd, struct device *dev) 5864bf50f18SLuigi Rizzo { 5874bf50f18SLuigi Rizzo int err = 0, id; 5884bf50f18SLuigi Rizzo id = nm_iommu_group_id(dev); 5894bf50f18SLuigi Rizzo if (netmap_verbose) 5904bf50f18SLuigi Rizzo D("iommu_group %d", id); 5914bf50f18SLuigi Rizzo 5924bf50f18SLuigi Rizzo NMA_LOCK(nmd); 5934bf50f18SLuigi Rizzo 5944bf50f18SLuigi Rizzo if (nmd->nm_grp < 0) 5954bf50f18SLuigi Rizzo nmd->nm_grp = id; 5964bf50f18SLuigi Rizzo 5974bf50f18SLuigi Rizzo if (nmd->nm_grp != id) 5984bf50f18SLuigi Rizzo nmd->lasterr = err = ENOMEM; 5994bf50f18SLuigi Rizzo 6004bf50f18SLuigi Rizzo NMA_UNLOCK(nmd); 6014bf50f18SLuigi Rizzo return err; 6024bf50f18SLuigi Rizzo } 603f0ea3689SLuigi Rizzo 604ccdc3305SLuigi Rizzo /* 6052579e2d7SLuigi Rizzo * First, find the allocator that contains the requested offset, 6062579e2d7SLuigi Rizzo * then locate the cluster through a lookup table. 607ccdc3305SLuigi Rizzo */ 608847bf383SLuigi Rizzo static vm_paddr_t 609847bf383SLuigi Rizzo netmap_mem2_ofstophys(struct netmap_mem_d* nmd, vm_ooffset_t offset) 610ccdc3305SLuigi Rizzo { 611ccdc3305SLuigi Rizzo int i; 612ce3ee1e7SLuigi Rizzo vm_ooffset_t o = offset; 613ce3ee1e7SLuigi Rizzo vm_paddr_t pa; 614ce3ee1e7SLuigi Rizzo struct netmap_obj_pool *p; 615ccdc3305SLuigi Rizzo 616ce3ee1e7SLuigi Rizzo NMA_LOCK(nmd); 617ce3ee1e7SLuigi Rizzo p = nmd->pools; 618ce3ee1e7SLuigi Rizzo 619ce3ee1e7SLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; offset -= p[i].memtotal, i++) { 620ce3ee1e7SLuigi Rizzo if (offset >= p[i].memtotal) 621ccdc3305SLuigi Rizzo continue; 6222579e2d7SLuigi Rizzo // now lookup the cluster's address 62337e3a6d3SLuigi Rizzo #ifndef _WIN32 6244bf50f18SLuigi Rizzo pa = vtophys(p[i].lut[offset / p[i]._objsize].vaddr) + 6258241616dSLuigi Rizzo offset % p[i]._objsize; 62637e3a6d3SLuigi Rizzo #else 62737e3a6d3SLuigi Rizzo pa = vtophys(p[i].lut[offset / p[i]._objsize].vaddr); 62837e3a6d3SLuigi Rizzo pa.QuadPart += offset % p[i]._objsize; 62937e3a6d3SLuigi Rizzo #endif 630ce3ee1e7SLuigi Rizzo NMA_UNLOCK(nmd); 631ce3ee1e7SLuigi Rizzo return pa; 632ccdc3305SLuigi Rizzo } 6338241616dSLuigi Rizzo /* this is only in case of errors */ 634b1123b01SLuigi Rizzo D("invalid ofs 0x%x out of 0x%x 0x%x 0x%x", (u_int)o, 635ce3ee1e7SLuigi Rizzo p[NETMAP_IF_POOL].memtotal, 636ce3ee1e7SLuigi Rizzo p[NETMAP_IF_POOL].memtotal 637ce3ee1e7SLuigi Rizzo + p[NETMAP_RING_POOL].memtotal, 638ce3ee1e7SLuigi Rizzo p[NETMAP_IF_POOL].memtotal 639ce3ee1e7SLuigi Rizzo + p[NETMAP_RING_POOL].memtotal 640ce3ee1e7SLuigi Rizzo + p[NETMAP_BUF_POOL].memtotal); 641ce3ee1e7SLuigi Rizzo NMA_UNLOCK(nmd); 64237e3a6d3SLuigi Rizzo #ifndef _WIN32 643ccdc3305SLuigi Rizzo return 0; // XXX bad address 64437e3a6d3SLuigi Rizzo #else 64537e3a6d3SLuigi Rizzo vm_paddr_t res; 64637e3a6d3SLuigi Rizzo res.QuadPart = 0; 64737e3a6d3SLuigi Rizzo return res; 64837e3a6d3SLuigi Rizzo #endif 64937e3a6d3SLuigi Rizzo } 65037e3a6d3SLuigi Rizzo 65137e3a6d3SLuigi Rizzo #ifdef _WIN32 65237e3a6d3SLuigi Rizzo 65337e3a6d3SLuigi Rizzo /* 65437e3a6d3SLuigi Rizzo * win32_build_virtual_memory_for_userspace 65537e3a6d3SLuigi Rizzo * 65637e3a6d3SLuigi Rizzo * This function get all the object making part of the pools and maps 65737e3a6d3SLuigi Rizzo * a contiguous virtual memory space for the userspace 65837e3a6d3SLuigi Rizzo * It works this way 65937e3a6d3SLuigi Rizzo * 1 - allocate a Memory Descriptor List wide as the sum 66037e3a6d3SLuigi Rizzo * of the memory needed for the pools 66137e3a6d3SLuigi Rizzo * 2 - cycle all the objects in every pool and for every object do 66237e3a6d3SLuigi Rizzo * 66337e3a6d3SLuigi Rizzo * 2a - cycle all the objects in every pool, get the list 66437e3a6d3SLuigi Rizzo * of the physical address descriptors 66537e3a6d3SLuigi Rizzo * 2b - calculate the offset in the array of pages desciptor in the 66637e3a6d3SLuigi Rizzo * main MDL 66737e3a6d3SLuigi Rizzo * 2c - copy the descriptors of the object in the main MDL 66837e3a6d3SLuigi Rizzo * 66937e3a6d3SLuigi Rizzo * 3 - return the resulting MDL that needs to be mapped in userland 67037e3a6d3SLuigi Rizzo * 67137e3a6d3SLuigi Rizzo * In this way we will have an MDL that describes all the memory for the 67237e3a6d3SLuigi Rizzo * objects in a single object 67337e3a6d3SLuigi Rizzo */ 67437e3a6d3SLuigi Rizzo 67537e3a6d3SLuigi Rizzo PMDL 67637e3a6d3SLuigi Rizzo win32_build_user_vm_map(struct netmap_mem_d* nmd) 67737e3a6d3SLuigi Rizzo { 67837e3a6d3SLuigi Rizzo int i, j; 67937e3a6d3SLuigi Rizzo u_int memsize, memflags, ofs = 0; 68037e3a6d3SLuigi Rizzo PMDL mainMdl, tempMdl; 68137e3a6d3SLuigi Rizzo 68237e3a6d3SLuigi Rizzo if (netmap_mem_get_info(nmd, &memsize, &memflags, NULL)) { 68337e3a6d3SLuigi Rizzo D("memory not finalised yet"); 68437e3a6d3SLuigi Rizzo return NULL; 68537e3a6d3SLuigi Rizzo } 68637e3a6d3SLuigi Rizzo 68737e3a6d3SLuigi Rizzo mainMdl = IoAllocateMdl(NULL, memsize, FALSE, FALSE, NULL); 68837e3a6d3SLuigi Rizzo if (mainMdl == NULL) { 68937e3a6d3SLuigi Rizzo D("failed to allocate mdl"); 69037e3a6d3SLuigi Rizzo return NULL; 69137e3a6d3SLuigi Rizzo } 69237e3a6d3SLuigi Rizzo 69337e3a6d3SLuigi Rizzo NMA_LOCK(nmd); 69437e3a6d3SLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 69537e3a6d3SLuigi Rizzo struct netmap_obj_pool *p = &nmd->pools[i]; 69637e3a6d3SLuigi Rizzo int clsz = p->_clustsize; 69737e3a6d3SLuigi Rizzo int clobjs = p->_clustentries; /* objects per cluster */ 69837e3a6d3SLuigi Rizzo int mdl_len = sizeof(PFN_NUMBER) * BYTES_TO_PAGES(clsz); 69937e3a6d3SLuigi Rizzo PPFN_NUMBER pSrc, pDst; 70037e3a6d3SLuigi Rizzo 70137e3a6d3SLuigi Rizzo /* each pool has a different cluster size so we need to reallocate */ 70237e3a6d3SLuigi Rizzo tempMdl = IoAllocateMdl(p->lut[0].vaddr, clsz, FALSE, FALSE, NULL); 70337e3a6d3SLuigi Rizzo if (tempMdl == NULL) { 70437e3a6d3SLuigi Rizzo NMA_UNLOCK(nmd); 70537e3a6d3SLuigi Rizzo D("fail to allocate tempMdl"); 70637e3a6d3SLuigi Rizzo IoFreeMdl(mainMdl); 70737e3a6d3SLuigi Rizzo return NULL; 70837e3a6d3SLuigi Rizzo } 70937e3a6d3SLuigi Rizzo pSrc = MmGetMdlPfnArray(tempMdl); 71037e3a6d3SLuigi Rizzo /* create one entry per cluster, the lut[] has one entry per object */ 71137e3a6d3SLuigi Rizzo for (j = 0; j < p->numclusters; j++, ofs += clsz) { 71237e3a6d3SLuigi Rizzo pDst = &MmGetMdlPfnArray(mainMdl)[BYTES_TO_PAGES(ofs)]; 71337e3a6d3SLuigi Rizzo MmInitializeMdl(tempMdl, p->lut[j*clobjs].vaddr, clsz); 71437e3a6d3SLuigi Rizzo MmBuildMdlForNonPagedPool(tempMdl); /* compute physical page addresses */ 71537e3a6d3SLuigi Rizzo RtlCopyMemory(pDst, pSrc, mdl_len); /* copy the page descriptors */ 71637e3a6d3SLuigi Rizzo mainMdl->MdlFlags = tempMdl->MdlFlags; /* XXX what is in here ? */ 71737e3a6d3SLuigi Rizzo } 71837e3a6d3SLuigi Rizzo IoFreeMdl(tempMdl); 71937e3a6d3SLuigi Rizzo } 72037e3a6d3SLuigi Rizzo NMA_UNLOCK(nmd); 72137e3a6d3SLuigi Rizzo return mainMdl; 72237e3a6d3SLuigi Rizzo } 72337e3a6d3SLuigi Rizzo 72437e3a6d3SLuigi Rizzo #endif /* _WIN32 */ 72537e3a6d3SLuigi Rizzo 72637e3a6d3SLuigi Rizzo /* 72737e3a6d3SLuigi Rizzo * helper function for OS-specific mmap routines (currently only windows). 72837e3a6d3SLuigi Rizzo * Given an nmd and a pool index, returns the cluster size and number of clusters. 72937e3a6d3SLuigi Rizzo * Returns 0 if memory is finalised and the pool is valid, otherwise 1. 73037e3a6d3SLuigi Rizzo * It should be called under NMA_LOCK(nmd) otherwise the underlying info can change. 73137e3a6d3SLuigi Rizzo */ 73237e3a6d3SLuigi Rizzo 73337e3a6d3SLuigi Rizzo int 73437e3a6d3SLuigi Rizzo netmap_mem2_get_pool_info(struct netmap_mem_d* nmd, u_int pool, u_int *clustsize, u_int *numclusters) 73537e3a6d3SLuigi Rizzo { 73637e3a6d3SLuigi Rizzo if (!nmd || !clustsize || !numclusters || pool >= NETMAP_POOLS_NR) 73737e3a6d3SLuigi Rizzo return 1; /* invalid arguments */ 73837e3a6d3SLuigi Rizzo // NMA_LOCK_ASSERT(nmd); 73937e3a6d3SLuigi Rizzo if (!(nmd->flags & NETMAP_MEM_FINALIZED)) { 74037e3a6d3SLuigi Rizzo *clustsize = *numclusters = 0; 74137e3a6d3SLuigi Rizzo return 1; /* not ready yet */ 74237e3a6d3SLuigi Rizzo } 74337e3a6d3SLuigi Rizzo *clustsize = nmd->pools[pool]._clustsize; 74437e3a6d3SLuigi Rizzo *numclusters = nmd->pools[pool].numclusters; 74537e3a6d3SLuigi Rizzo return 0; /* success */ 746ccdc3305SLuigi Rizzo } 747ccdc3305SLuigi Rizzo 748847bf383SLuigi Rizzo static int 749847bf383SLuigi Rizzo netmap_mem2_get_info(struct netmap_mem_d* nmd, u_int* size, u_int *memflags, 750f0ea3689SLuigi Rizzo nm_memid_t *id) 751ce3ee1e7SLuigi Rizzo { 752ce3ee1e7SLuigi Rizzo int error = 0; 753ce3ee1e7SLuigi Rizzo NMA_LOCK(nmd); 754847bf383SLuigi Rizzo error = netmap_mem_config(nmd); 755ce3ee1e7SLuigi Rizzo if (error) 756ce3ee1e7SLuigi Rizzo goto out; 7574bf50f18SLuigi Rizzo if (size) { 758ce3ee1e7SLuigi Rizzo if (nmd->flags & NETMAP_MEM_FINALIZED) { 759ce3ee1e7SLuigi Rizzo *size = nmd->nm_totalsize; 760ce3ee1e7SLuigi Rizzo } else { 761ce3ee1e7SLuigi Rizzo int i; 762ce3ee1e7SLuigi Rizzo *size = 0; 763ce3ee1e7SLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 764ce3ee1e7SLuigi Rizzo struct netmap_obj_pool *p = nmd->pools + i; 765ce3ee1e7SLuigi Rizzo *size += (p->_numclusters * p->_clustsize); 766ce3ee1e7SLuigi Rizzo } 767ce3ee1e7SLuigi Rizzo } 7684bf50f18SLuigi Rizzo } 7694bf50f18SLuigi Rizzo if (memflags) 770ce3ee1e7SLuigi Rizzo *memflags = nmd->flags; 7714bf50f18SLuigi Rizzo if (id) 772f0ea3689SLuigi Rizzo *id = nmd->nm_id; 773ce3ee1e7SLuigi Rizzo out: 774ce3ee1e7SLuigi Rizzo NMA_UNLOCK(nmd); 775ce3ee1e7SLuigi Rizzo return error; 776ce3ee1e7SLuigi Rizzo } 777ce3ee1e7SLuigi Rizzo 778ccdc3305SLuigi Rizzo /* 779ccdc3305SLuigi Rizzo * we store objects by kernel address, need to find the offset 780ccdc3305SLuigi Rizzo * within the pool to export the value to userspace. 781ccdc3305SLuigi Rizzo * Algorithm: scan until we find the cluster, then add the 782ccdc3305SLuigi Rizzo * actual offset in the cluster 783ccdc3305SLuigi Rizzo */ 784ce2cb792SLuigi Rizzo static ssize_t 785ccdc3305SLuigi Rizzo netmap_obj_offset(struct netmap_obj_pool *p, const void *vaddr) 786ccdc3305SLuigi Rizzo { 787ce3ee1e7SLuigi Rizzo int i, k = p->_clustentries, n = p->objtotal; 788ccdc3305SLuigi Rizzo ssize_t ofs = 0; 789ccdc3305SLuigi Rizzo 790ccdc3305SLuigi Rizzo for (i = 0; i < n; i += k, ofs += p->_clustsize) { 791ccdc3305SLuigi Rizzo const char *base = p->lut[i].vaddr; 792ccdc3305SLuigi Rizzo ssize_t relofs = (const char *) vaddr - base; 793ccdc3305SLuigi Rizzo 794aa76317cSLuigi Rizzo if (relofs < 0 || relofs >= p->_clustsize) 795ccdc3305SLuigi Rizzo continue; 796ccdc3305SLuigi Rizzo 797ccdc3305SLuigi Rizzo ofs = ofs + relofs; 798ccdc3305SLuigi Rizzo ND("%s: return offset %d (cluster %d) for pointer %p", 799ccdc3305SLuigi Rizzo p->name, ofs, i, vaddr); 800ccdc3305SLuigi Rizzo return ofs; 801ccdc3305SLuigi Rizzo } 802ccdc3305SLuigi Rizzo D("address %p is not contained inside any cluster (%s)", 803ccdc3305SLuigi Rizzo vaddr, p->name); 804ccdc3305SLuigi Rizzo return 0; /* An error occurred */ 805ccdc3305SLuigi Rizzo } 806ccdc3305SLuigi Rizzo 807ccdc3305SLuigi Rizzo /* Helper functions which convert virtual addresses to offsets */ 808ce3ee1e7SLuigi Rizzo #define netmap_if_offset(n, v) \ 809ce3ee1e7SLuigi Rizzo netmap_obj_offset(&(n)->pools[NETMAP_IF_POOL], (v)) 810ccdc3305SLuigi Rizzo 811ce3ee1e7SLuigi Rizzo #define netmap_ring_offset(n, v) \ 812ce3ee1e7SLuigi Rizzo ((n)->pools[NETMAP_IF_POOL].memtotal + \ 813ce3ee1e7SLuigi Rizzo netmap_obj_offset(&(n)->pools[NETMAP_RING_POOL], (v))) 814ccdc3305SLuigi Rizzo 815847bf383SLuigi Rizzo static ssize_t 816847bf383SLuigi Rizzo netmap_mem2_if_offset(struct netmap_mem_d *nmd, const void *addr) 817ce3ee1e7SLuigi Rizzo { 818ce3ee1e7SLuigi Rizzo ssize_t v; 819ce3ee1e7SLuigi Rizzo NMA_LOCK(nmd); 820ce3ee1e7SLuigi Rizzo v = netmap_if_offset(nmd, addr); 821ce3ee1e7SLuigi Rizzo NMA_UNLOCK(nmd); 822ce3ee1e7SLuigi Rizzo return v; 823ce3ee1e7SLuigi Rizzo } 824ce3ee1e7SLuigi Rizzo 8258241616dSLuigi Rizzo /* 8268241616dSLuigi Rizzo * report the index, and use start position as a hint, 8278241616dSLuigi Rizzo * otherwise buffer allocation becomes terribly expensive. 8288241616dSLuigi Rizzo */ 829ccdc3305SLuigi Rizzo static void * 830ce3ee1e7SLuigi Rizzo netmap_obj_malloc(struct netmap_obj_pool *p, u_int len, uint32_t *start, uint32_t *index) 831ccdc3305SLuigi Rizzo { 832ccdc3305SLuigi Rizzo uint32_t i = 0; /* index in the bitmap */ 83337e3a6d3SLuigi Rizzo uint32_t mask, j = 0; /* slot counter */ 834ccdc3305SLuigi Rizzo void *vaddr = NULL; 835ccdc3305SLuigi Rizzo 836ccdc3305SLuigi Rizzo if (len > p->_objsize) { 837ccdc3305SLuigi Rizzo D("%s request size %d too large", p->name, len); 838ccdc3305SLuigi Rizzo // XXX cannot reduce the size 839ccdc3305SLuigi Rizzo return NULL; 840ccdc3305SLuigi Rizzo } 841ccdc3305SLuigi Rizzo 842ccdc3305SLuigi Rizzo if (p->objfree == 0) { 843f9790aebSLuigi Rizzo D("no more %s objects", p->name); 844ccdc3305SLuigi Rizzo return NULL; 845ccdc3305SLuigi Rizzo } 8468241616dSLuigi Rizzo if (start) 8478241616dSLuigi Rizzo i = *start; 848ccdc3305SLuigi Rizzo 8498241616dSLuigi Rizzo /* termination is guaranteed by p->free, but better check bounds on i */ 8508241616dSLuigi Rizzo while (vaddr == NULL && i < p->bitmap_slots) { 851ccdc3305SLuigi Rizzo uint32_t cur = p->bitmap[i]; 852ccdc3305SLuigi Rizzo if (cur == 0) { /* bitmask is fully used */ 853ccdc3305SLuigi Rizzo i++; 854ccdc3305SLuigi Rizzo continue; 855ccdc3305SLuigi Rizzo } 856ccdc3305SLuigi Rizzo /* locate a slot */ 857ccdc3305SLuigi Rizzo for (j = 0, mask = 1; (cur & mask) == 0; j++, mask <<= 1) 858ccdc3305SLuigi Rizzo ; 859ccdc3305SLuigi Rizzo 860ccdc3305SLuigi Rizzo p->bitmap[i] &= ~mask; /* mark object as in use */ 861ccdc3305SLuigi Rizzo p->objfree--; 862ccdc3305SLuigi Rizzo 863ccdc3305SLuigi Rizzo vaddr = p->lut[i * 32 + j].vaddr; 8648241616dSLuigi Rizzo if (index) 8658241616dSLuigi Rizzo *index = i * 32 + j; 866ccdc3305SLuigi Rizzo } 86737e3a6d3SLuigi Rizzo ND("%s allocator: allocated object @ [%d][%d]: vaddr %p",p->name, i, j, vaddr); 868ccdc3305SLuigi Rizzo 8698241616dSLuigi Rizzo if (start) 8708241616dSLuigi Rizzo *start = i; 871ccdc3305SLuigi Rizzo return vaddr; 872ccdc3305SLuigi Rizzo } 873ccdc3305SLuigi Rizzo 874ccdc3305SLuigi Rizzo 875ccdc3305SLuigi Rizzo /* 876f0ea3689SLuigi Rizzo * free by index, not by address. 877f0ea3689SLuigi Rizzo * XXX should we also cleanup the content ? 878ccdc3305SLuigi Rizzo */ 879f0ea3689SLuigi Rizzo static int 880ccdc3305SLuigi Rizzo netmap_obj_free(struct netmap_obj_pool *p, uint32_t j) 881ccdc3305SLuigi Rizzo { 882f0ea3689SLuigi Rizzo uint32_t *ptr, mask; 883f0ea3689SLuigi Rizzo 884ccdc3305SLuigi Rizzo if (j >= p->objtotal) { 885ccdc3305SLuigi Rizzo D("invalid index %u, max %u", j, p->objtotal); 886f0ea3689SLuigi Rizzo return 1; 887ccdc3305SLuigi Rizzo } 888f0ea3689SLuigi Rizzo ptr = &p->bitmap[j / 32]; 889f0ea3689SLuigi Rizzo mask = (1 << (j % 32)); 890f0ea3689SLuigi Rizzo if (*ptr & mask) { 891f0ea3689SLuigi Rizzo D("ouch, double free on buffer %d", j); 892f0ea3689SLuigi Rizzo return 1; 893f0ea3689SLuigi Rizzo } else { 894f0ea3689SLuigi Rizzo *ptr |= mask; 895ccdc3305SLuigi Rizzo p->objfree++; 896f0ea3689SLuigi Rizzo return 0; 897f0ea3689SLuigi Rizzo } 898ccdc3305SLuigi Rizzo } 899ccdc3305SLuigi Rizzo 900f0ea3689SLuigi Rizzo /* 901f0ea3689SLuigi Rizzo * free by address. This is slow but is only used for a few 902f0ea3689SLuigi Rizzo * objects (rings, nifp) 903f0ea3689SLuigi Rizzo */ 904ccdc3305SLuigi Rizzo static void 905ccdc3305SLuigi Rizzo netmap_obj_free_va(struct netmap_obj_pool *p, void *vaddr) 906ccdc3305SLuigi Rizzo { 907ce3ee1e7SLuigi Rizzo u_int i, j, n = p->numclusters; 908ccdc3305SLuigi Rizzo 909ce3ee1e7SLuigi Rizzo for (i = 0, j = 0; i < n; i++, j += p->_clustentries) { 910ce3ee1e7SLuigi Rizzo void *base = p->lut[i * p->_clustentries].vaddr; 911ccdc3305SLuigi Rizzo ssize_t relofs = (ssize_t) vaddr - (ssize_t) base; 912ccdc3305SLuigi Rizzo 913ccdc3305SLuigi Rizzo /* Given address, is out of the scope of the current cluster.*/ 914ede69cffSLuigi Rizzo if (vaddr < base || relofs >= p->_clustsize) 915ccdc3305SLuigi Rizzo continue; 916ccdc3305SLuigi Rizzo 917ccdc3305SLuigi Rizzo j = j + relofs / p->_objsize; 918ce3ee1e7SLuigi Rizzo /* KASSERT(j != 0, ("Cannot free object 0")); */ 919ccdc3305SLuigi Rizzo netmap_obj_free(p, j); 920ccdc3305SLuigi Rizzo return; 921ccdc3305SLuigi Rizzo } 922ae10d1afSLuigi Rizzo D("address %p is not contained inside any cluster (%s)", 923ccdc3305SLuigi Rizzo vaddr, p->name); 924ccdc3305SLuigi Rizzo } 925ccdc3305SLuigi Rizzo 9264bf50f18SLuigi Rizzo #define netmap_mem_bufsize(n) \ 9274bf50f18SLuigi Rizzo ((n)->pools[NETMAP_BUF_POOL]._objsize) 9284bf50f18SLuigi Rizzo 929ce3ee1e7SLuigi Rizzo #define netmap_if_malloc(n, len) netmap_obj_malloc(&(n)->pools[NETMAP_IF_POOL], len, NULL, NULL) 930ce3ee1e7SLuigi Rizzo #define netmap_if_free(n, v) netmap_obj_free_va(&(n)->pools[NETMAP_IF_POOL], (v)) 931ce3ee1e7SLuigi Rizzo #define netmap_ring_malloc(n, len) netmap_obj_malloc(&(n)->pools[NETMAP_RING_POOL], len, NULL, NULL) 932ce3ee1e7SLuigi Rizzo #define netmap_ring_free(n, v) netmap_obj_free_va(&(n)->pools[NETMAP_RING_POOL], (v)) 933ce3ee1e7SLuigi Rizzo #define netmap_buf_malloc(n, _pos, _index) \ 9344bf50f18SLuigi Rizzo netmap_obj_malloc(&(n)->pools[NETMAP_BUF_POOL], netmap_mem_bufsize(n), _pos, _index) 935ccdc3305SLuigi Rizzo 936ccdc3305SLuigi Rizzo 937f0ea3689SLuigi Rizzo #if 0 // XXX unused 938ccdc3305SLuigi Rizzo /* Return the index associated to the given packet buffer */ 939ce3ee1e7SLuigi Rizzo #define netmap_buf_index(n, v) \ 940ce3ee1e7SLuigi Rizzo (netmap_obj_offset(&(n)->pools[NETMAP_BUF_POOL], (v)) / NETMAP_BDG_BUF_SIZE(n)) 941f0ea3689SLuigi Rizzo #endif 942f0ea3689SLuigi Rizzo 943f0ea3689SLuigi Rizzo /* 944f0ea3689SLuigi Rizzo * allocate extra buffers in a linked list. 945f0ea3689SLuigi Rizzo * returns the actual number. 946f0ea3689SLuigi Rizzo */ 947f0ea3689SLuigi Rizzo uint32_t 948f0ea3689SLuigi Rizzo netmap_extra_alloc(struct netmap_adapter *na, uint32_t *head, uint32_t n) 949f0ea3689SLuigi Rizzo { 950f0ea3689SLuigi Rizzo struct netmap_mem_d *nmd = na->nm_mem; 951f0ea3689SLuigi Rizzo uint32_t i, pos = 0; /* opaque, scan position in the bitmap */ 952f0ea3689SLuigi Rizzo 953f0ea3689SLuigi Rizzo NMA_LOCK(nmd); 954f0ea3689SLuigi Rizzo 955f0ea3689SLuigi Rizzo *head = 0; /* default, 'null' index ie empty list */ 956f0ea3689SLuigi Rizzo for (i = 0 ; i < n; i++) { 957f0ea3689SLuigi Rizzo uint32_t cur = *head; /* save current head */ 958f0ea3689SLuigi Rizzo uint32_t *p = netmap_buf_malloc(nmd, &pos, head); 959f0ea3689SLuigi Rizzo if (p == NULL) { 960f0ea3689SLuigi Rizzo D("no more buffers after %d of %d", i, n); 961f0ea3689SLuigi Rizzo *head = cur; /* restore */ 962f0ea3689SLuigi Rizzo break; 963f0ea3689SLuigi Rizzo } 96437e3a6d3SLuigi Rizzo ND(5, "allocate buffer %d -> %d", *head, cur); 965f0ea3689SLuigi Rizzo *p = cur; /* link to previous head */ 966f0ea3689SLuigi Rizzo } 967f0ea3689SLuigi Rizzo 968f0ea3689SLuigi Rizzo NMA_UNLOCK(nmd); 969f0ea3689SLuigi Rizzo 970f0ea3689SLuigi Rizzo return i; 971f0ea3689SLuigi Rizzo } 972f0ea3689SLuigi Rizzo 973f0ea3689SLuigi Rizzo static void 974f0ea3689SLuigi Rizzo netmap_extra_free(struct netmap_adapter *na, uint32_t head) 975f0ea3689SLuigi Rizzo { 976847bf383SLuigi Rizzo struct lut_entry *lut = na->na_lut.lut; 977f0ea3689SLuigi Rizzo struct netmap_mem_d *nmd = na->nm_mem; 978f0ea3689SLuigi Rizzo struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL]; 979f0ea3689SLuigi Rizzo uint32_t i, cur, *buf; 980f0ea3689SLuigi Rizzo 98137e3a6d3SLuigi Rizzo ND("freeing the extra list"); 982f0ea3689SLuigi Rizzo for (i = 0; head >=2 && head < p->objtotal; i++) { 983f0ea3689SLuigi Rizzo cur = head; 984f0ea3689SLuigi Rizzo buf = lut[head].vaddr; 985f0ea3689SLuigi Rizzo head = *buf; 986f0ea3689SLuigi Rizzo *buf = 0; 987f0ea3689SLuigi Rizzo if (netmap_obj_free(p, cur)) 988f0ea3689SLuigi Rizzo break; 989f0ea3689SLuigi Rizzo } 990f0ea3689SLuigi Rizzo if (head != 0) 991f0ea3689SLuigi Rizzo D("breaking with head %d", head); 99237e3a6d3SLuigi Rizzo if (netmap_verbose) 993f0ea3689SLuigi Rizzo D("freed %d buffers", i); 994f0ea3689SLuigi Rizzo } 995ccdc3305SLuigi Rizzo 996ccdc3305SLuigi Rizzo 9978241616dSLuigi Rizzo /* Return nonzero on error */ 9988241616dSLuigi Rizzo static int 999f9790aebSLuigi Rizzo netmap_new_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n) 1000ccdc3305SLuigi Rizzo { 1001ce3ee1e7SLuigi Rizzo struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL]; 1002ce3ee1e7SLuigi Rizzo u_int i = 0; /* slot counter */ 10038241616dSLuigi Rizzo uint32_t pos = 0; /* slot in p->bitmap */ 10048241616dSLuigi Rizzo uint32_t index = 0; /* buffer index */ 1005ccdc3305SLuigi Rizzo 1006ccdc3305SLuigi Rizzo for (i = 0; i < n; i++) { 1007ce3ee1e7SLuigi Rizzo void *vaddr = netmap_buf_malloc(nmd, &pos, &index); 1008ccdc3305SLuigi Rizzo if (vaddr == NULL) { 1009f9790aebSLuigi Rizzo D("no more buffers after %d of %d", i, n); 1010ccdc3305SLuigi Rizzo goto cleanup; 1011ccdc3305SLuigi Rizzo } 10128241616dSLuigi Rizzo slot[i].buf_idx = index; 1013ccdc3305SLuigi Rizzo slot[i].len = p->_objsize; 1014f9790aebSLuigi Rizzo slot[i].flags = 0; 1015ccdc3305SLuigi Rizzo } 1016ccdc3305SLuigi Rizzo 10178241616dSLuigi Rizzo ND("allocated %d buffers, %d available, first at %d", n, p->objfree, pos); 10188241616dSLuigi Rizzo return (0); 1019ccdc3305SLuigi Rizzo 1020ccdc3305SLuigi Rizzo cleanup: 10214cf8455fSEd Maste while (i > 0) { 10224cf8455fSEd Maste i--; 10238241616dSLuigi Rizzo netmap_obj_free(p, slot[i].buf_idx); 1024ccdc3305SLuigi Rizzo } 10258241616dSLuigi Rizzo bzero(slot, n * sizeof(slot[0])); 10268241616dSLuigi Rizzo return (ENOMEM); 1027ccdc3305SLuigi Rizzo } 1028ccdc3305SLuigi Rizzo 1029f0ea3689SLuigi Rizzo static void 1030f0ea3689SLuigi Rizzo netmap_mem_set_ring(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n, uint32_t index) 1031f0ea3689SLuigi Rizzo { 1032f0ea3689SLuigi Rizzo struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL]; 1033f0ea3689SLuigi Rizzo u_int i; 1034f0ea3689SLuigi Rizzo 1035f0ea3689SLuigi Rizzo for (i = 0; i < n; i++) { 1036f0ea3689SLuigi Rizzo slot[i].buf_idx = index; 1037f0ea3689SLuigi Rizzo slot[i].len = p->_objsize; 1038f0ea3689SLuigi Rizzo slot[i].flags = 0; 1039f0ea3689SLuigi Rizzo } 1040f0ea3689SLuigi Rizzo } 1041f0ea3689SLuigi Rizzo 1042ccdc3305SLuigi Rizzo 1043ccdc3305SLuigi Rizzo static void 1044f9790aebSLuigi Rizzo netmap_free_buf(struct netmap_mem_d *nmd, uint32_t i) 1045ccdc3305SLuigi Rizzo { 1046ce3ee1e7SLuigi Rizzo struct netmap_obj_pool *p = &nmd->pools[NETMAP_BUF_POOL]; 10478241616dSLuigi Rizzo 1048ccdc3305SLuigi Rizzo if (i < 2 || i >= p->objtotal) { 1049ccdc3305SLuigi Rizzo D("Cannot free buf#%d: should be in [2, %d[", i, p->objtotal); 1050ccdc3305SLuigi Rizzo return; 1051ccdc3305SLuigi Rizzo } 10528241616dSLuigi Rizzo netmap_obj_free(p, i); 1053ccdc3305SLuigi Rizzo } 1054ccdc3305SLuigi Rizzo 1055f0ea3689SLuigi Rizzo 1056f0ea3689SLuigi Rizzo static void 1057f0ea3689SLuigi Rizzo netmap_free_bufs(struct netmap_mem_d *nmd, struct netmap_slot *slot, u_int n) 1058f0ea3689SLuigi Rizzo { 1059f0ea3689SLuigi Rizzo u_int i; 1060f0ea3689SLuigi Rizzo 1061f0ea3689SLuigi Rizzo for (i = 0; i < n; i++) { 1062f0ea3689SLuigi Rizzo if (slot[i].buf_idx > 2) 1063f0ea3689SLuigi Rizzo netmap_free_buf(nmd, slot[i].buf_idx); 1064f0ea3689SLuigi Rizzo } 1065f0ea3689SLuigi Rizzo } 1066f0ea3689SLuigi Rizzo 10678241616dSLuigi Rizzo static void 10688241616dSLuigi Rizzo netmap_reset_obj_allocator(struct netmap_obj_pool *p) 10698241616dSLuigi Rizzo { 1070ce3ee1e7SLuigi Rizzo 10718241616dSLuigi Rizzo if (p == NULL) 10728241616dSLuigi Rizzo return; 10738241616dSLuigi Rizzo if (p->bitmap) 1074c3e9b4dbSLuiz Otavio O Souza nm_os_free(p->bitmap); 10758241616dSLuigi Rizzo p->bitmap = NULL; 10768241616dSLuigi Rizzo if (p->lut) { 1077ce3ee1e7SLuigi Rizzo u_int i; 1078ce3ee1e7SLuigi Rizzo 1079dd4fcbc5SPatrick Kelsey /* 1080dd4fcbc5SPatrick Kelsey * Free each cluster allocated in 1081dd4fcbc5SPatrick Kelsey * netmap_finalize_obj_allocator(). The cluster start 1082dd4fcbc5SPatrick Kelsey * addresses are stored at multiples of p->_clusterentries 1083dd4fcbc5SPatrick Kelsey * in the lut. 1084dd4fcbc5SPatrick Kelsey */ 1085ce3ee1e7SLuigi Rizzo for (i = 0; i < p->objtotal; i += p->_clustentries) { 10868241616dSLuigi Rizzo if (p->lut[i].vaddr) 108737e3a6d3SLuigi Rizzo contigfree(p->lut[i].vaddr, p->_clustsize, M_NETMAP); 10888241616dSLuigi Rizzo } 10898241616dSLuigi Rizzo bzero(p->lut, sizeof(struct lut_entry) * p->objtotal); 10908241616dSLuigi Rizzo #ifdef linux 10918241616dSLuigi Rizzo vfree(p->lut); 10928241616dSLuigi Rizzo #else 1093c3e9b4dbSLuiz Otavio O Souza nm_os_free(p->lut); 10948241616dSLuigi Rizzo #endif 10958241616dSLuigi Rizzo } 10968241616dSLuigi Rizzo p->lut = NULL; 1097ce3ee1e7SLuigi Rizzo p->objtotal = 0; 1098ce3ee1e7SLuigi Rizzo p->memtotal = 0; 1099ce3ee1e7SLuigi Rizzo p->numclusters = 0; 1100ce3ee1e7SLuigi Rizzo p->objfree = 0; 11018241616dSLuigi Rizzo } 1102ccdc3305SLuigi Rizzo 1103ccdc3305SLuigi Rizzo /* 1104ccdc3305SLuigi Rizzo * Free all resources related to an allocator. 1105ccdc3305SLuigi Rizzo */ 1106ccdc3305SLuigi Rizzo static void 1107ccdc3305SLuigi Rizzo netmap_destroy_obj_allocator(struct netmap_obj_pool *p) 1108ccdc3305SLuigi Rizzo { 1109ccdc3305SLuigi Rizzo if (p == NULL) 1110ccdc3305SLuigi Rizzo return; 11118241616dSLuigi Rizzo netmap_reset_obj_allocator(p); 1112ccdc3305SLuigi Rizzo } 1113ccdc3305SLuigi Rizzo 1114ccdc3305SLuigi Rizzo /* 1115ccdc3305SLuigi Rizzo * We receive a request for objtotal objects, of size objsize each. 1116ccdc3305SLuigi Rizzo * Internally we may round up both numbers, as we allocate objects 1117ccdc3305SLuigi Rizzo * in small clusters multiple of the page size. 1118ce3ee1e7SLuigi Rizzo * We need to keep track of objtotal and clustentries, 1119ccdc3305SLuigi Rizzo * as they are needed when freeing memory. 1120ccdc3305SLuigi Rizzo * 1121ccdc3305SLuigi Rizzo * XXX note -- userspace needs the buffers to be contiguous, 1122ccdc3305SLuigi Rizzo * so we cannot afford gaps at the end of a cluster. 1123ccdc3305SLuigi Rizzo */ 11248241616dSLuigi Rizzo 11258241616dSLuigi Rizzo 11268241616dSLuigi Rizzo /* call with NMA_LOCK held */ 11278241616dSLuigi Rizzo static int 11288241616dSLuigi Rizzo netmap_config_obj_allocator(struct netmap_obj_pool *p, u_int objtotal, u_int objsize) 1129ccdc3305SLuigi Rizzo { 1130ce3ee1e7SLuigi Rizzo int i; 1131ccdc3305SLuigi Rizzo u_int clustsize; /* the cluster size, multiple of page size */ 1132ccdc3305SLuigi Rizzo u_int clustentries; /* how many objects per entry */ 1133ccdc3305SLuigi Rizzo 1134ce3ee1e7SLuigi Rizzo /* we store the current request, so we can 1135ce3ee1e7SLuigi Rizzo * detect configuration changes later */ 1136ce3ee1e7SLuigi Rizzo p->r_objtotal = objtotal; 1137ce3ee1e7SLuigi Rizzo p->r_objsize = objsize; 1138ce3ee1e7SLuigi Rizzo 11394bf50f18SLuigi Rizzo #define MAX_CLUSTSIZE (1<<22) // 4 MB 114017885a7bSLuigi Rizzo #define LINE_ROUND NM_CACHE_ALIGN // 64 1141ccdc3305SLuigi Rizzo if (objsize >= MAX_CLUSTSIZE) { 1142ccdc3305SLuigi Rizzo /* we could do it but there is no point */ 1143ccdc3305SLuigi Rizzo D("unsupported allocation for %d bytes", objsize); 1144ce3ee1e7SLuigi Rizzo return EINVAL; 1145ccdc3305SLuigi Rizzo } 1146ccdc3305SLuigi Rizzo /* make sure objsize is a multiple of LINE_ROUND */ 1147ccdc3305SLuigi Rizzo i = (objsize & (LINE_ROUND - 1)); 1148ccdc3305SLuigi Rizzo if (i) { 1149ccdc3305SLuigi Rizzo D("XXX aligning object by %d bytes", LINE_ROUND - i); 1150ccdc3305SLuigi Rizzo objsize += LINE_ROUND - i; 1151ccdc3305SLuigi Rizzo } 11528241616dSLuigi Rizzo if (objsize < p->objminsize || objsize > p->objmaxsize) { 11538241616dSLuigi Rizzo D("requested objsize %d out of range [%d, %d]", 11548241616dSLuigi Rizzo objsize, p->objminsize, p->objmaxsize); 1155ce3ee1e7SLuigi Rizzo return EINVAL; 11568241616dSLuigi Rizzo } 11578241616dSLuigi Rizzo if (objtotal < p->nummin || objtotal > p->nummax) { 11588241616dSLuigi Rizzo D("requested objtotal %d out of range [%d, %d]", 11598241616dSLuigi Rizzo objtotal, p->nummin, p->nummax); 1160ce3ee1e7SLuigi Rizzo return EINVAL; 11618241616dSLuigi Rizzo } 1162ccdc3305SLuigi Rizzo /* 1163ccdc3305SLuigi Rizzo * Compute number of objects using a brute-force approach: 1164ccdc3305SLuigi Rizzo * given a max cluster size, 1165ccdc3305SLuigi Rizzo * we try to fill it with objects keeping track of the 1166ccdc3305SLuigi Rizzo * wasted space to the next page boundary. 1167ccdc3305SLuigi Rizzo */ 1168ccdc3305SLuigi Rizzo for (clustentries = 0, i = 1;; i++) { 1169ccdc3305SLuigi Rizzo u_int delta, used = i * objsize; 1170ccdc3305SLuigi Rizzo if (used > MAX_CLUSTSIZE) 1171ccdc3305SLuigi Rizzo break; 1172ccdc3305SLuigi Rizzo delta = used % PAGE_SIZE; 1173ccdc3305SLuigi Rizzo if (delta == 0) { // exact solution 1174ccdc3305SLuigi Rizzo clustentries = i; 1175ccdc3305SLuigi Rizzo break; 1176ccdc3305SLuigi Rizzo } 1177ccdc3305SLuigi Rizzo } 11784bf50f18SLuigi Rizzo /* exact solution not found */ 11794bf50f18SLuigi Rizzo if (clustentries == 0) { 11804bf50f18SLuigi Rizzo D("unsupported allocation for %d bytes", objsize); 11814bf50f18SLuigi Rizzo return EINVAL; 11824bf50f18SLuigi Rizzo } 11834bf50f18SLuigi Rizzo /* compute clustsize */ 1184ccdc3305SLuigi Rizzo clustsize = clustentries * objsize; 1185ae10d1afSLuigi Rizzo if (netmap_verbose) 1186ccdc3305SLuigi Rizzo D("objsize %d clustsize %d objects %d", 1187ccdc3305SLuigi Rizzo objsize, clustsize, clustentries); 1188ccdc3305SLuigi Rizzo 1189ccdc3305SLuigi Rizzo /* 1190ccdc3305SLuigi Rizzo * The number of clusters is n = ceil(objtotal/clustentries) 1191ccdc3305SLuigi Rizzo * objtotal' = n * clustentries 1192ccdc3305SLuigi Rizzo */ 1193ce3ee1e7SLuigi Rizzo p->_clustentries = clustentries; 1194ccdc3305SLuigi Rizzo p->_clustsize = clustsize; 1195ce3ee1e7SLuigi Rizzo p->_numclusters = (objtotal + clustentries - 1) / clustentries; 1196ce3ee1e7SLuigi Rizzo 1197ce3ee1e7SLuigi Rizzo /* actual values (may be larger than requested) */ 11988241616dSLuigi Rizzo p->_objsize = objsize; 1199ce3ee1e7SLuigi Rizzo p->_objtotal = p->_numclusters * clustentries; 1200ccdc3305SLuigi Rizzo 12018241616dSLuigi Rizzo return 0; 12028241616dSLuigi Rizzo } 12038241616dSLuigi Rizzo 120437e3a6d3SLuigi Rizzo static struct lut_entry * 120537e3a6d3SLuigi Rizzo nm_alloc_lut(u_int nobj) 120637e3a6d3SLuigi Rizzo { 120737e3a6d3SLuigi Rizzo size_t n = sizeof(struct lut_entry) * nobj; 120837e3a6d3SLuigi Rizzo struct lut_entry *lut; 120937e3a6d3SLuigi Rizzo #ifdef linux 121037e3a6d3SLuigi Rizzo lut = vmalloc(n); 121137e3a6d3SLuigi Rizzo #else 1212c3e9b4dbSLuiz Otavio O Souza lut = nm_os_malloc(n); 121337e3a6d3SLuigi Rizzo #endif 121437e3a6d3SLuigi Rizzo return lut; 121537e3a6d3SLuigi Rizzo } 12168241616dSLuigi Rizzo 12178241616dSLuigi Rizzo /* call with NMA_LOCK held */ 12188241616dSLuigi Rizzo static int 12198241616dSLuigi Rizzo netmap_finalize_obj_allocator(struct netmap_obj_pool *p) 12208241616dSLuigi Rizzo { 1221ce3ee1e7SLuigi Rizzo int i; /* must be signed */ 1222ce3ee1e7SLuigi Rizzo size_t n; 1223ce3ee1e7SLuigi Rizzo 1224ce3ee1e7SLuigi Rizzo /* optimistically assume we have enough memory */ 1225ce3ee1e7SLuigi Rizzo p->numclusters = p->_numclusters; 1226ce3ee1e7SLuigi Rizzo p->objtotal = p->_objtotal; 12278241616dSLuigi Rizzo 122837e3a6d3SLuigi Rizzo p->lut = nm_alloc_lut(p->objtotal); 1229ccdc3305SLuigi Rizzo if (p->lut == NULL) { 123037e3a6d3SLuigi Rizzo D("Unable to create lookup table for '%s'", p->name); 1231ccdc3305SLuigi Rizzo goto clean; 1232ccdc3305SLuigi Rizzo } 1233ccdc3305SLuigi Rizzo 1234ccdc3305SLuigi Rizzo /* Allocate the bitmap */ 1235ccdc3305SLuigi Rizzo n = (p->objtotal + 31) / 32; 1236c3e9b4dbSLuiz Otavio O Souza p->bitmap = nm_os_malloc(sizeof(uint32_t) * n); 1237ccdc3305SLuigi Rizzo if (p->bitmap == NULL) { 1238ce3ee1e7SLuigi Rizzo D("Unable to create bitmap (%d entries) for allocator '%s'", (int)n, 12398241616dSLuigi Rizzo p->name); 1240ccdc3305SLuigi Rizzo goto clean; 1241ccdc3305SLuigi Rizzo } 12428241616dSLuigi Rizzo p->bitmap_slots = n; 1243ccdc3305SLuigi Rizzo 1244ccdc3305SLuigi Rizzo /* 1245ccdc3305SLuigi Rizzo * Allocate clusters, init pointers and bitmap 1246ccdc3305SLuigi Rizzo */ 1247ce3ee1e7SLuigi Rizzo 1248ce3ee1e7SLuigi Rizzo n = p->_clustsize; 1249ce3ee1e7SLuigi Rizzo for (i = 0; i < (int)p->objtotal;) { 1250ce3ee1e7SLuigi Rizzo int lim = i + p->_clustentries; 1251ccdc3305SLuigi Rizzo char *clust; 1252ccdc3305SLuigi Rizzo 125337e3a6d3SLuigi Rizzo /* 125437e3a6d3SLuigi Rizzo * XXX Note, we only need contigmalloc() for buffers attached 125537e3a6d3SLuigi Rizzo * to native interfaces. In all other cases (nifp, netmap rings 125637e3a6d3SLuigi Rizzo * and even buffers for VALE ports or emulated interfaces) we 125737e3a6d3SLuigi Rizzo * can live with standard malloc, because the hardware will not 125837e3a6d3SLuigi Rizzo * access the pages directly. 125937e3a6d3SLuigi Rizzo */ 1260ce3ee1e7SLuigi Rizzo clust = contigmalloc(n, M_NETMAP, M_NOWAIT | M_ZERO, 1261ce3ee1e7SLuigi Rizzo (size_t)0, -1UL, PAGE_SIZE, 0); 1262ccdc3305SLuigi Rizzo if (clust == NULL) { 1263ccdc3305SLuigi Rizzo /* 1264ccdc3305SLuigi Rizzo * If we get here, there is a severe memory shortage, 1265ccdc3305SLuigi Rizzo * so halve the allocated memory to reclaim some. 1266ccdc3305SLuigi Rizzo */ 1267ccdc3305SLuigi Rizzo D("Unable to create cluster at %d for '%s' allocator", 12688241616dSLuigi Rizzo i, p->name); 1269ce3ee1e7SLuigi Rizzo if (i < 2) /* nothing to halve */ 1270ce3ee1e7SLuigi Rizzo goto out; 1271ccdc3305SLuigi Rizzo lim = i / 2; 12728241616dSLuigi Rizzo for (i--; i >= lim; i--) { 1273ccdc3305SLuigi Rizzo p->bitmap[ (i>>5) ] &= ~( 1 << (i & 31) ); 1274ce3ee1e7SLuigi Rizzo if (i % p->_clustentries == 0 && p->lut[i].vaddr) 1275ccdc3305SLuigi Rizzo contigfree(p->lut[i].vaddr, 1276ce3ee1e7SLuigi Rizzo n, M_NETMAP); 1277dd4fcbc5SPatrick Kelsey p->lut[i].vaddr = NULL; 1278ccdc3305SLuigi Rizzo } 1279ce3ee1e7SLuigi Rizzo out: 1280ccdc3305SLuigi Rizzo p->objtotal = i; 1281ce3ee1e7SLuigi Rizzo /* we may have stopped in the middle of a cluster */ 1282ce3ee1e7SLuigi Rizzo p->numclusters = (i + p->_clustentries - 1) / p->_clustentries; 1283ccdc3305SLuigi Rizzo break; 1284ccdc3305SLuigi Rizzo } 1285dd4fcbc5SPatrick Kelsey /* 1286dd4fcbc5SPatrick Kelsey * Set bitmap and lut state for all buffers in the current 1287dd4fcbc5SPatrick Kelsey * cluster. 1288dd4fcbc5SPatrick Kelsey * 1289dd4fcbc5SPatrick Kelsey * [i, lim) is the set of buffer indexes that cover the 1290dd4fcbc5SPatrick Kelsey * current cluster. 1291dd4fcbc5SPatrick Kelsey * 1292dd4fcbc5SPatrick Kelsey * 'clust' is really the address of the current buffer in 1293dd4fcbc5SPatrick Kelsey * the current cluster as we index through it with a stride 1294dd4fcbc5SPatrick Kelsey * of p->_objsize. 1295dd4fcbc5SPatrick Kelsey */ 12968241616dSLuigi Rizzo for (; i < lim; i++, clust += p->_objsize) { 1297ccdc3305SLuigi Rizzo p->bitmap[ (i>>5) ] |= ( 1 << (i & 31) ); 1298ccdc3305SLuigi Rizzo p->lut[i].vaddr = clust; 1299ccdc3305SLuigi Rizzo p->lut[i].paddr = vtophys(clust); 1300ccdc3305SLuigi Rizzo } 1301ccdc3305SLuigi Rizzo } 1302ce3ee1e7SLuigi Rizzo p->objfree = p->objtotal; 1303ce3ee1e7SLuigi Rizzo p->memtotal = p->numclusters * p->_clustsize; 1304ce3ee1e7SLuigi Rizzo if (p->objfree == 0) 1305ce3ee1e7SLuigi Rizzo goto clean; 1306ae10d1afSLuigi Rizzo if (netmap_verbose) 1307ccdc3305SLuigi Rizzo D("Pre-allocated %d clusters (%d/%dKB) for '%s'", 1308ce3ee1e7SLuigi Rizzo p->numclusters, p->_clustsize >> 10, 1309ce3ee1e7SLuigi Rizzo p->memtotal >> 10, p->name); 1310ccdc3305SLuigi Rizzo 13118241616dSLuigi Rizzo return 0; 1312ccdc3305SLuigi Rizzo 1313ccdc3305SLuigi Rizzo clean: 13148241616dSLuigi Rizzo netmap_reset_obj_allocator(p); 13158241616dSLuigi Rizzo return ENOMEM; 13168241616dSLuigi Rizzo } 13178241616dSLuigi Rizzo 13188241616dSLuigi Rizzo /* call with lock held */ 13198241616dSLuigi Rizzo static int 1320c3e9b4dbSLuiz Otavio O Souza netmap_mem_params_changed(struct netmap_obj_params* p) 13218241616dSLuigi Rizzo { 1322c3e9b4dbSLuiz Otavio O Souza int i, rv = 0; 13238241616dSLuigi Rizzo 13248241616dSLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 1325c3e9b4dbSLuiz Otavio O Souza if (p[i].last_size != p[i].size || p[i].last_num != p[i].num) { 1326c3e9b4dbSLuiz Otavio O Souza p[i].last_size = p[i].size; 1327c3e9b4dbSLuiz Otavio O Souza p[i].last_num = p[i].num; 1328c3e9b4dbSLuiz Otavio O Souza rv = 1; 13298241616dSLuigi Rizzo } 1330c3e9b4dbSLuiz Otavio O Souza } 1331c3e9b4dbSLuiz Otavio O Souza return rv; 13328241616dSLuigi Rizzo } 13338241616dSLuigi Rizzo 1334ce3ee1e7SLuigi Rizzo static void 1335ce3ee1e7SLuigi Rizzo netmap_mem_reset_all(struct netmap_mem_d *nmd) 1336ce3ee1e7SLuigi Rizzo { 1337ce3ee1e7SLuigi Rizzo int i; 1338f0ea3689SLuigi Rizzo 1339f0ea3689SLuigi Rizzo if (netmap_verbose) 1340ce3ee1e7SLuigi Rizzo D("resetting %p", nmd); 1341ce3ee1e7SLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 1342ce3ee1e7SLuigi Rizzo netmap_reset_obj_allocator(&nmd->pools[i]); 1343ce3ee1e7SLuigi Rizzo } 1344ce3ee1e7SLuigi Rizzo nmd->flags &= ~NETMAP_MEM_FINALIZED; 1345ce3ee1e7SLuigi Rizzo } 1346ce3ee1e7SLuigi Rizzo 1347ce3ee1e7SLuigi Rizzo static int 13484bf50f18SLuigi Rizzo netmap_mem_unmap(struct netmap_obj_pool *p, struct netmap_adapter *na) 13494bf50f18SLuigi Rizzo { 13504bf50f18SLuigi Rizzo int i, lim = p->_objtotal; 13514bf50f18SLuigi Rizzo 1352c3e9b4dbSLuiz Otavio O Souza if (na == NULL || na->pdev == NULL) 13534bf50f18SLuigi Rizzo return 0; 13544bf50f18SLuigi Rizzo 135537e3a6d3SLuigi Rizzo #if defined(__FreeBSD__) 13564bf50f18SLuigi Rizzo (void)i; 13574bf50f18SLuigi Rizzo (void)lim; 13584bf50f18SLuigi Rizzo D("unsupported on FreeBSD"); 135937e3a6d3SLuigi Rizzo 136037e3a6d3SLuigi Rizzo #elif defined(_WIN32) 136137e3a6d3SLuigi Rizzo (void)i; 136237e3a6d3SLuigi Rizzo (void)lim; 136337e3a6d3SLuigi Rizzo D("unsupported on Windows"); //XXX_ale, really? 13644bf50f18SLuigi Rizzo #else /* linux */ 13654bf50f18SLuigi Rizzo for (i = 2; i < lim; i++) { 13664bf50f18SLuigi Rizzo netmap_unload_map(na, (bus_dma_tag_t) na->pdev, &p->lut[i].paddr); 13674bf50f18SLuigi Rizzo } 13684bf50f18SLuigi Rizzo #endif /* linux */ 13694bf50f18SLuigi Rizzo 13704bf50f18SLuigi Rizzo return 0; 13714bf50f18SLuigi Rizzo } 13724bf50f18SLuigi Rizzo 13734bf50f18SLuigi Rizzo static int 13744bf50f18SLuigi Rizzo netmap_mem_map(struct netmap_obj_pool *p, struct netmap_adapter *na) 13754bf50f18SLuigi Rizzo { 137637e3a6d3SLuigi Rizzo #if defined(__FreeBSD__) 13774bf50f18SLuigi Rizzo D("unsupported on FreeBSD"); 137837e3a6d3SLuigi Rizzo #elif defined(_WIN32) 137937e3a6d3SLuigi Rizzo D("unsupported on Windows"); //XXX_ale, really? 13804bf50f18SLuigi Rizzo #else /* linux */ 13814bf50f18SLuigi Rizzo int i, lim = p->_objtotal; 13824bf50f18SLuigi Rizzo 13834bf50f18SLuigi Rizzo if (na->pdev == NULL) 13844bf50f18SLuigi Rizzo return 0; 13854bf50f18SLuigi Rizzo 13864bf50f18SLuigi Rizzo for (i = 2; i < lim; i++) { 13874bf50f18SLuigi Rizzo netmap_load_map(na, (bus_dma_tag_t) na->pdev, &p->lut[i].paddr, 13884bf50f18SLuigi Rizzo p->lut[i].vaddr); 13894bf50f18SLuigi Rizzo } 13904bf50f18SLuigi Rizzo #endif /* linux */ 13914bf50f18SLuigi Rizzo 13924bf50f18SLuigi Rizzo return 0; 13934bf50f18SLuigi Rizzo } 13944bf50f18SLuigi Rizzo 13954bf50f18SLuigi Rizzo static int 1396ce3ee1e7SLuigi Rizzo netmap_mem_finalize_all(struct netmap_mem_d *nmd) 1397ce3ee1e7SLuigi Rizzo { 1398ce3ee1e7SLuigi Rizzo int i; 1399ce3ee1e7SLuigi Rizzo if (nmd->flags & NETMAP_MEM_FINALIZED) 1400ce3ee1e7SLuigi Rizzo return 0; 1401ce3ee1e7SLuigi Rizzo nmd->lasterr = 0; 1402ce3ee1e7SLuigi Rizzo nmd->nm_totalsize = 0; 1403ce3ee1e7SLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 1404ce3ee1e7SLuigi Rizzo nmd->lasterr = netmap_finalize_obj_allocator(&nmd->pools[i]); 1405ce3ee1e7SLuigi Rizzo if (nmd->lasterr) 1406ce3ee1e7SLuigi Rizzo goto error; 1407ce3ee1e7SLuigi Rizzo nmd->nm_totalsize += nmd->pools[i].memtotal; 1408ce3ee1e7SLuigi Rizzo } 1409ce3ee1e7SLuigi Rizzo /* buffers 0 and 1 are reserved */ 1410ce3ee1e7SLuigi Rizzo nmd->pools[NETMAP_BUF_POOL].objfree -= 2; 1411ce3ee1e7SLuigi Rizzo nmd->pools[NETMAP_BUF_POOL].bitmap[0] = ~3; 1412ce3ee1e7SLuigi Rizzo nmd->flags |= NETMAP_MEM_FINALIZED; 1413ce3ee1e7SLuigi Rizzo 1414f0ea3689SLuigi Rizzo if (netmap_verbose) 1415f0ea3689SLuigi Rizzo D("interfaces %d KB, rings %d KB, buffers %d MB", 1416ce3ee1e7SLuigi Rizzo nmd->pools[NETMAP_IF_POOL].memtotal >> 10, 1417ce3ee1e7SLuigi Rizzo nmd->pools[NETMAP_RING_POOL].memtotal >> 10, 1418ce3ee1e7SLuigi Rizzo nmd->pools[NETMAP_BUF_POOL].memtotal >> 20); 1419ce3ee1e7SLuigi Rizzo 1420f0ea3689SLuigi Rizzo if (netmap_verbose) 1421ce3ee1e7SLuigi Rizzo D("Free buffers: %d", nmd->pools[NETMAP_BUF_POOL].objfree); 1422ce3ee1e7SLuigi Rizzo 1423ce3ee1e7SLuigi Rizzo 1424ce3ee1e7SLuigi Rizzo return 0; 1425ce3ee1e7SLuigi Rizzo error: 1426ce3ee1e7SLuigi Rizzo netmap_mem_reset_all(nmd); 1427ce3ee1e7SLuigi Rizzo return nmd->lasterr; 1428ce3ee1e7SLuigi Rizzo } 1429ce3ee1e7SLuigi Rizzo 1430f0ea3689SLuigi Rizzo /* 1431f0ea3689SLuigi Rizzo * allocator for private memory 1432f0ea3689SLuigi Rizzo */ 1433c3e9b4dbSLuiz Otavio O Souza static struct netmap_mem_d * 1434c3e9b4dbSLuiz Otavio O Souza _netmap_mem_private_new(struct netmap_obj_params *p, int *perr) 1435ce3ee1e7SLuigi Rizzo { 1436ce3ee1e7SLuigi Rizzo struct netmap_mem_d *d = NULL; 1437c3e9b4dbSLuiz Otavio O Souza int i, err = 0; 1438ce3ee1e7SLuigi Rizzo 1439c3e9b4dbSLuiz Otavio O Souza d = nm_os_malloc(sizeof(struct netmap_mem_d)); 1440f0ea3689SLuigi Rizzo if (d == NULL) { 1441f0ea3689SLuigi Rizzo err = ENOMEM; 1442f0ea3689SLuigi Rizzo goto error; 1443f0ea3689SLuigi Rizzo } 1444ce3ee1e7SLuigi Rizzo 1445ce3ee1e7SLuigi Rizzo *d = nm_blueprint; 1446ce3ee1e7SLuigi Rizzo 1447f0ea3689SLuigi Rizzo err = nm_mem_assign_id(d); 1448f0ea3689SLuigi Rizzo if (err) 1449f0ea3689SLuigi Rizzo goto error; 1450c3e9b4dbSLuiz Otavio O Souza snprintf(d->name, NM_MEM_NAMESZ, "%d", d->nm_id); 1451f0ea3689SLuigi Rizzo 1452c3e9b4dbSLuiz Otavio O Souza for (i = 0; i < NETMAP_POOLS_NR; i++) { 1453c3e9b4dbSLuiz Otavio O Souza snprintf(d->pools[i].name, NETMAP_POOL_MAX_NAMSZ, 1454c3e9b4dbSLuiz Otavio O Souza nm_blueprint.pools[i].name, 1455c3e9b4dbSLuiz Otavio O Souza d->name); 1456c3e9b4dbSLuiz Otavio O Souza d->params[i].num = p[i].num; 1457c3e9b4dbSLuiz Otavio O Souza d->params[i].size = p[i].size; 1458c3e9b4dbSLuiz Otavio O Souza } 1459c3e9b4dbSLuiz Otavio O Souza 1460c3e9b4dbSLuiz Otavio O Souza NMA_LOCK_INIT(d); 1461c3e9b4dbSLuiz Otavio O Souza 1462c3e9b4dbSLuiz Otavio O Souza err = netmap_mem_config(d); 1463c3e9b4dbSLuiz Otavio O Souza if (err) 1464c3e9b4dbSLuiz Otavio O Souza goto error; 1465c3e9b4dbSLuiz Otavio O Souza 1466c3e9b4dbSLuiz Otavio O Souza d->flags &= ~NETMAP_MEM_FINALIZED; 1467c3e9b4dbSLuiz Otavio O Souza 1468c3e9b4dbSLuiz Otavio O Souza return d; 1469c3e9b4dbSLuiz Otavio O Souza 1470c3e9b4dbSLuiz Otavio O Souza error: 1471c3e9b4dbSLuiz Otavio O Souza netmap_mem_delete(d); 1472c3e9b4dbSLuiz Otavio O Souza if (perr) 1473c3e9b4dbSLuiz Otavio O Souza *perr = err; 1474c3e9b4dbSLuiz Otavio O Souza return NULL; 1475c3e9b4dbSLuiz Otavio O Souza } 1476c3e9b4dbSLuiz Otavio O Souza 1477c3e9b4dbSLuiz Otavio O Souza struct netmap_mem_d * 1478c3e9b4dbSLuiz Otavio O Souza netmap_mem_private_new(u_int txr, u_int txd, u_int rxr, u_int rxd, 1479c3e9b4dbSLuiz Otavio O Souza u_int extra_bufs, u_int npipes, int *perr) 1480c3e9b4dbSLuiz Otavio O Souza { 1481c3e9b4dbSLuiz Otavio O Souza struct netmap_mem_d *d = NULL; 1482c3e9b4dbSLuiz Otavio O Souza struct netmap_obj_params p[NETMAP_POOLS_NR]; 1483c3e9b4dbSLuiz Otavio O Souza int i, err = 0; 1484c3e9b4dbSLuiz Otavio O Souza u_int v, maxd; 1485f0ea3689SLuigi Rizzo /* account for the fake host rings */ 1486ce3ee1e7SLuigi Rizzo txr++; 1487ce3ee1e7SLuigi Rizzo rxr++; 1488ce3ee1e7SLuigi Rizzo 1489f0ea3689SLuigi Rizzo /* copy the min values */ 1490f0ea3689SLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 1491f0ea3689SLuigi Rizzo p[i] = netmap_min_priv_params[i]; 1492f0ea3689SLuigi Rizzo } 1493f0ea3689SLuigi Rizzo 1494f0ea3689SLuigi Rizzo /* possibly increase them to fit user request */ 1495f0ea3689SLuigi Rizzo v = sizeof(struct netmap_if) + sizeof(ssize_t) * (txr + rxr); 1496f0ea3689SLuigi Rizzo if (p[NETMAP_IF_POOL].size < v) 1497f0ea3689SLuigi Rizzo p[NETMAP_IF_POOL].size = v; 1498f0ea3689SLuigi Rizzo v = 2 + 4 * npipes; 1499f0ea3689SLuigi Rizzo if (p[NETMAP_IF_POOL].num < v) 1500f0ea3689SLuigi Rizzo p[NETMAP_IF_POOL].num = v; 1501f0ea3689SLuigi Rizzo maxd = (txd > rxd) ? txd : rxd; 1502f0ea3689SLuigi Rizzo v = sizeof(struct netmap_ring) + sizeof(struct netmap_slot) * maxd; 1503f0ea3689SLuigi Rizzo if (p[NETMAP_RING_POOL].size < v) 1504f0ea3689SLuigi Rizzo p[NETMAP_RING_POOL].size = v; 1505f0ea3689SLuigi Rizzo /* each pipe endpoint needs two tx rings (1 normal + 1 host, fake) 1506f0ea3689SLuigi Rizzo * and two rx rings (again, 1 normal and 1 fake host) 1507f0ea3689SLuigi Rizzo */ 1508f0ea3689SLuigi Rizzo v = txr + rxr + 8 * npipes; 1509f0ea3689SLuigi Rizzo if (p[NETMAP_RING_POOL].num < v) 1510f0ea3689SLuigi Rizzo p[NETMAP_RING_POOL].num = v; 1511f0ea3689SLuigi Rizzo /* for each pipe we only need the buffers for the 4 "real" rings. 1512f0ea3689SLuigi Rizzo * On the other end, the pipe ring dimension may be different from 1513f0ea3689SLuigi Rizzo * the parent port ring dimension. As a compromise, we allocate twice the 1514f0ea3689SLuigi Rizzo * space actually needed if the pipe rings were the same size as the parent rings 1515f0ea3689SLuigi Rizzo */ 1516f0ea3689SLuigi Rizzo v = (4 * npipes + rxr) * rxd + (4 * npipes + txr) * txd + 2 + extra_bufs; 1517f0ea3689SLuigi Rizzo /* the +2 is for the tx and rx fake buffers (indices 0 and 1) */ 1518f0ea3689SLuigi Rizzo if (p[NETMAP_BUF_POOL].num < v) 1519f0ea3689SLuigi Rizzo p[NETMAP_BUF_POOL].num = v; 1520f0ea3689SLuigi Rizzo 1521f0ea3689SLuigi Rizzo if (netmap_verbose) 1522ce3ee1e7SLuigi Rizzo D("req if %d*%d ring %d*%d buf %d*%d", 1523ce3ee1e7SLuigi Rizzo p[NETMAP_IF_POOL].num, 1524ce3ee1e7SLuigi Rizzo p[NETMAP_IF_POOL].size, 1525ce3ee1e7SLuigi Rizzo p[NETMAP_RING_POOL].num, 1526ce3ee1e7SLuigi Rizzo p[NETMAP_RING_POOL].size, 1527ce3ee1e7SLuigi Rizzo p[NETMAP_BUF_POOL].num, 1528ce3ee1e7SLuigi Rizzo p[NETMAP_BUF_POOL].size); 1529ce3ee1e7SLuigi Rizzo 1530c3e9b4dbSLuiz Otavio O Souza d = _netmap_mem_private_new(p, perr); 1531c3e9b4dbSLuiz Otavio O Souza if (d == NULL) 1532ce3ee1e7SLuigi Rizzo goto error; 1533ce3ee1e7SLuigi Rizzo 1534ce3ee1e7SLuigi Rizzo return d; 1535ce3ee1e7SLuigi Rizzo error: 1536c3e9b4dbSLuiz Otavio O Souza netmap_mem_delete(d); 1537f0ea3689SLuigi Rizzo if (perr) 1538f0ea3689SLuigi Rizzo *perr = err; 1539ce3ee1e7SLuigi Rizzo return NULL; 1540ce3ee1e7SLuigi Rizzo } 1541ce3ee1e7SLuigi Rizzo 15428241616dSLuigi Rizzo 15438241616dSLuigi Rizzo /* call with lock held */ 15448241616dSLuigi Rizzo static int 1545c3e9b4dbSLuiz Otavio O Souza netmap_mem2_config(struct netmap_mem_d *nmd) 15468241616dSLuigi Rizzo { 15478241616dSLuigi Rizzo int i; 15488241616dSLuigi Rizzo 1549847bf383SLuigi Rizzo if (nmd->active) 1550ce3ee1e7SLuigi Rizzo /* already in use, we cannot change the configuration */ 1551ce3ee1e7SLuigi Rizzo goto out; 1552ce3ee1e7SLuigi Rizzo 1553c3e9b4dbSLuiz Otavio O Souza if (!netmap_mem_params_changed(nmd->params)) 15548241616dSLuigi Rizzo goto out; 15558241616dSLuigi Rizzo 1556847bf383SLuigi Rizzo ND("reconfiguring"); 15578241616dSLuigi Rizzo 1558ce3ee1e7SLuigi Rizzo if (nmd->flags & NETMAP_MEM_FINALIZED) { 15598241616dSLuigi Rizzo /* reset previous allocation */ 15608241616dSLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 1561ce3ee1e7SLuigi Rizzo netmap_reset_obj_allocator(&nmd->pools[i]); 15628241616dSLuigi Rizzo } 1563ce3ee1e7SLuigi Rizzo nmd->flags &= ~NETMAP_MEM_FINALIZED; 15648241616dSLuigi Rizzo } 15658241616dSLuigi Rizzo 15668241616dSLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 1567ce3ee1e7SLuigi Rizzo nmd->lasterr = netmap_config_obj_allocator(&nmd->pools[i], 1568c3e9b4dbSLuiz Otavio O Souza nmd->params[i].num, nmd->params[i].size); 1569ce3ee1e7SLuigi Rizzo if (nmd->lasterr) 15708241616dSLuigi Rizzo goto out; 15718241616dSLuigi Rizzo } 15728241616dSLuigi Rizzo 15738241616dSLuigi Rizzo out: 15748241616dSLuigi Rizzo 1575ce3ee1e7SLuigi Rizzo return nmd->lasterr; 15768241616dSLuigi Rizzo } 15778241616dSLuigi Rizzo 15788241616dSLuigi Rizzo static int 1579c3e9b4dbSLuiz Otavio O Souza netmap_mem2_finalize(struct netmap_mem_d *nmd) 15808241616dSLuigi Rizzo { 1581ce3ee1e7SLuigi Rizzo int err; 15828241616dSLuigi Rizzo 15838241616dSLuigi Rizzo /* update configuration if changed */ 1584c3e9b4dbSLuiz Otavio O Souza if (netmap_mem2_config(nmd)) 1585c3e9b4dbSLuiz Otavio O Souza goto out1; 15868241616dSLuigi Rizzo 1587847bf383SLuigi Rizzo nmd->active++; 1588ce3ee1e7SLuigi Rizzo 1589ce3ee1e7SLuigi Rizzo if (nmd->flags & NETMAP_MEM_FINALIZED) { 15908241616dSLuigi Rizzo /* may happen if config is not changed */ 15918241616dSLuigi Rizzo ND("nothing to do"); 15928241616dSLuigi Rizzo goto out; 15938241616dSLuigi Rizzo } 15948241616dSLuigi Rizzo 1595ce3ee1e7SLuigi Rizzo if (netmap_mem_finalize_all(nmd)) 1596ce3ee1e7SLuigi Rizzo goto out; 15978241616dSLuigi Rizzo 1598ce3ee1e7SLuigi Rizzo nmd->lasterr = 0; 15998241616dSLuigi Rizzo 16008241616dSLuigi Rizzo out: 1601ce3ee1e7SLuigi Rizzo if (nmd->lasterr) 1602847bf383SLuigi Rizzo nmd->active--; 1603c3e9b4dbSLuiz Otavio O Souza out1: 1604ce3ee1e7SLuigi Rizzo err = nmd->lasterr; 16058241616dSLuigi Rizzo 1606ce3ee1e7SLuigi Rizzo return err; 16078241616dSLuigi Rizzo 1608ccdc3305SLuigi Rizzo } 1609ccdc3305SLuigi Rizzo 1610847bf383SLuigi Rizzo static void 1611c3e9b4dbSLuiz Otavio O Souza netmap_mem2_delete(struct netmap_mem_d *nmd) 1612ccdc3305SLuigi Rizzo { 16138241616dSLuigi Rizzo int i; 16148241616dSLuigi Rizzo 16158241616dSLuigi Rizzo for (i = 0; i < NETMAP_POOLS_NR; i++) { 1616c3e9b4dbSLuiz Otavio O Souza netmap_destroy_obj_allocator(&nmd->pools[i]); 16178241616dSLuigi Rizzo } 1618847bf383SLuigi Rizzo 1619c3e9b4dbSLuiz Otavio O Souza NMA_LOCK_DESTROY(nmd); 1620c3e9b4dbSLuiz Otavio O Souza if (nmd != &nm_mem) 1621c3e9b4dbSLuiz Otavio O Souza nm_os_free(nmd); 16228241616dSLuigi Rizzo } 16238241616dSLuigi Rizzo 1624847bf383SLuigi Rizzo int 1625847bf383SLuigi Rizzo netmap_mem_init(void) 1626847bf383SLuigi Rizzo { 1627c3e9b4dbSLuiz Otavio O Souza NM_MTX_INIT(nm_mem_list_lock); 1628847bf383SLuigi Rizzo NMA_LOCK_INIT(&nm_mem); 1629847bf383SLuigi Rizzo netmap_mem_get(&nm_mem); 1630847bf383SLuigi Rizzo return (0); 1631847bf383SLuigi Rizzo } 1632847bf383SLuigi Rizzo 1633847bf383SLuigi Rizzo void 1634847bf383SLuigi Rizzo netmap_mem_fini(void) 1635847bf383SLuigi Rizzo { 1636847bf383SLuigi Rizzo netmap_mem_put(&nm_mem); 1637847bf383SLuigi Rizzo } 1638847bf383SLuigi Rizzo 16398241616dSLuigi Rizzo static void 16408241616dSLuigi Rizzo netmap_free_rings(struct netmap_adapter *na) 16418241616dSLuigi Rizzo { 1642847bf383SLuigi Rizzo enum txrx t; 1643847bf383SLuigi Rizzo 1644847bf383SLuigi Rizzo for_rx_tx(t) { 1645847bf383SLuigi Rizzo u_int i; 164637e3a6d3SLuigi Rizzo for (i = 0; i < nma_get_nrings(na, t) + 1; i++) { 1647847bf383SLuigi Rizzo struct netmap_kring *kring = &NMR(na, t)[i]; 1648847bf383SLuigi Rizzo struct netmap_ring *ring = kring->ring; 1649847bf383SLuigi Rizzo 165037e3a6d3SLuigi Rizzo if (ring == NULL || kring->users > 0 || (kring->nr_kflags & NKR_NEEDRING)) { 165137e3a6d3SLuigi Rizzo ND("skipping ring %s (ring %p, users %d)", 165237e3a6d3SLuigi Rizzo kring->name, ring, kring->users); 1653f0ea3689SLuigi Rizzo continue; 165437e3a6d3SLuigi Rizzo } 165537e3a6d3SLuigi Rizzo if (i != nma_get_nrings(na, t) || na->na_flags & NAF_HOST_RINGS) 1656f0ea3689SLuigi Rizzo netmap_free_bufs(na->nm_mem, ring->slot, kring->nkr_num_slots); 1657f0ea3689SLuigi Rizzo netmap_ring_free(na->nm_mem, ring); 1658f0ea3689SLuigi Rizzo kring->ring = NULL; 16598241616dSLuigi Rizzo } 1660ce3ee1e7SLuigi Rizzo } 1661ccdc3305SLuigi Rizzo } 1662ccdc3305SLuigi Rizzo 1663f9790aebSLuigi Rizzo /* call with NMA_LOCK held * 1664f9790aebSLuigi Rizzo * 1665f9790aebSLuigi Rizzo * Allocate netmap rings and buffers for this card 1666f9790aebSLuigi Rizzo * The rings are contiguous, but have variable size. 1667f0ea3689SLuigi Rizzo * The kring array must follow the layout described 1668f0ea3689SLuigi Rizzo * in netmap_krings_create(). 1669f9790aebSLuigi Rizzo */ 1670847bf383SLuigi Rizzo static int 1671847bf383SLuigi Rizzo netmap_mem2_rings_create(struct netmap_adapter *na) 1672f9790aebSLuigi Rizzo { 1673847bf383SLuigi Rizzo enum txrx t; 1674f9790aebSLuigi Rizzo 1675f9790aebSLuigi Rizzo NMA_LOCK(na->nm_mem); 1676f9790aebSLuigi Rizzo 1677847bf383SLuigi Rizzo for_rx_tx(t) { 1678847bf383SLuigi Rizzo u_int i; 1679847bf383SLuigi Rizzo 1680847bf383SLuigi Rizzo for (i = 0; i <= nma_get_nrings(na, t); i++) { 1681847bf383SLuigi Rizzo struct netmap_kring *kring = &NMR(na, t)[i]; 1682847bf383SLuigi Rizzo struct netmap_ring *ring = kring->ring; 1683847bf383SLuigi Rizzo u_int len, ndesc; 1684847bf383SLuigi Rizzo 168537e3a6d3SLuigi Rizzo if (ring || (!kring->users && !(kring->nr_kflags & NKR_NEEDRING))) { 168637e3a6d3SLuigi Rizzo /* uneeded, or already created by somebody else */ 168737e3a6d3SLuigi Rizzo ND("skipping ring %s", kring->name); 168837e3a6d3SLuigi Rizzo continue; 1689f0ea3689SLuigi Rizzo } 1690f9790aebSLuigi Rizzo ndesc = kring->nkr_num_slots; 1691f9790aebSLuigi Rizzo len = sizeof(struct netmap_ring) + 1692f9790aebSLuigi Rizzo ndesc * sizeof(struct netmap_slot); 1693f9790aebSLuigi Rizzo ring = netmap_ring_malloc(na->nm_mem, len); 1694f9790aebSLuigi Rizzo if (ring == NULL) { 1695847bf383SLuigi Rizzo D("Cannot allocate %s_ring", nm_txrx2str(t)); 1696f9790aebSLuigi Rizzo goto cleanup; 1697f9790aebSLuigi Rizzo } 1698f6c2a31fSLuigi Rizzo ND("txring at %p", ring); 1699f9790aebSLuigi Rizzo kring->ring = ring; 1700f9790aebSLuigi Rizzo *(uint32_t *)(uintptr_t)&ring->num_slots = ndesc; 170117885a7bSLuigi Rizzo *(int64_t *)(uintptr_t)&ring->buf_ofs = 1702f9790aebSLuigi Rizzo (na->nm_mem->pools[NETMAP_IF_POOL].memtotal + 1703f9790aebSLuigi Rizzo na->nm_mem->pools[NETMAP_RING_POOL].memtotal) - 1704f9790aebSLuigi Rizzo netmap_ring_offset(na->nm_mem, ring); 1705f9790aebSLuigi Rizzo 170617885a7bSLuigi Rizzo /* copy values from kring */ 170717885a7bSLuigi Rizzo ring->head = kring->rhead; 170817885a7bSLuigi Rizzo ring->cur = kring->rcur; 170917885a7bSLuigi Rizzo ring->tail = kring->rtail; 1710f9790aebSLuigi Rizzo *(uint16_t *)(uintptr_t)&ring->nr_buf_size = 17114bf50f18SLuigi Rizzo netmap_mem_bufsize(na->nm_mem); 1712f0ea3689SLuigi Rizzo ND("%s h %d c %d t %d", kring->name, 1713f0ea3689SLuigi Rizzo ring->head, ring->cur, ring->tail); 1714847bf383SLuigi Rizzo ND("initializing slots for %s_ring", nm_txrx2str(txrx)); 1715847bf383SLuigi Rizzo if (i != nma_get_nrings(na, t) || (na->na_flags & NAF_HOST_RINGS)) { 1716f0ea3689SLuigi Rizzo /* this is a real ring */ 1717f9790aebSLuigi Rizzo if (netmap_new_bufs(na->nm_mem, ring->slot, ndesc)) { 1718847bf383SLuigi Rizzo D("Cannot allocate buffers for %s_ring", nm_txrx2str(t)); 1719f9790aebSLuigi Rizzo goto cleanup; 1720f9790aebSLuigi Rizzo } 1721f0ea3689SLuigi Rizzo } else { 1722847bf383SLuigi Rizzo /* this is a fake ring, set all indices to 0 */ 1723f0ea3689SLuigi Rizzo netmap_mem_set_ring(na->nm_mem, ring->slot, ndesc, 0); 1724f0ea3689SLuigi Rizzo } 1725847bf383SLuigi Rizzo /* ring info */ 1726847bf383SLuigi Rizzo *(uint16_t *)(uintptr_t)&ring->ringid = kring->ring_id; 1727847bf383SLuigi Rizzo *(uint16_t *)(uintptr_t)&ring->dir = kring->tx; 1728f0ea3689SLuigi Rizzo } 1729f9790aebSLuigi Rizzo } 1730f9790aebSLuigi Rizzo 1731f9790aebSLuigi Rizzo NMA_UNLOCK(na->nm_mem); 1732f9790aebSLuigi Rizzo 1733f9790aebSLuigi Rizzo return 0; 1734f9790aebSLuigi Rizzo 1735f9790aebSLuigi Rizzo cleanup: 1736f9790aebSLuigi Rizzo netmap_free_rings(na); 1737f9790aebSLuigi Rizzo 1738f9790aebSLuigi Rizzo NMA_UNLOCK(na->nm_mem); 1739f9790aebSLuigi Rizzo 1740f9790aebSLuigi Rizzo return ENOMEM; 1741f9790aebSLuigi Rizzo } 1742f9790aebSLuigi Rizzo 1743847bf383SLuigi Rizzo static void 1744847bf383SLuigi Rizzo netmap_mem2_rings_delete(struct netmap_adapter *na) 1745f9790aebSLuigi Rizzo { 1746f9790aebSLuigi Rizzo /* last instance, release bufs and rings */ 1747f9790aebSLuigi Rizzo NMA_LOCK(na->nm_mem); 1748f9790aebSLuigi Rizzo 1749f9790aebSLuigi Rizzo netmap_free_rings(na); 1750f9790aebSLuigi Rizzo 1751f9790aebSLuigi Rizzo NMA_UNLOCK(na->nm_mem); 1752f9790aebSLuigi Rizzo } 1753ccdc3305SLuigi Rizzo 1754ccdc3305SLuigi Rizzo 17558241616dSLuigi Rizzo /* call with NMA_LOCK held */ 1756ae10d1afSLuigi Rizzo /* 1757ae10d1afSLuigi Rizzo * Allocate the per-fd structure netmap_if. 1758ce3ee1e7SLuigi Rizzo * 1759ce3ee1e7SLuigi Rizzo * We assume that the configuration stored in na 1760ce3ee1e7SLuigi Rizzo * (number of tx/rx rings and descs) does not change while 1761ce3ee1e7SLuigi Rizzo * the interface is in netmap mode. 1762ae10d1afSLuigi Rizzo */ 1763847bf383SLuigi Rizzo static struct netmap_if * 1764c3e9b4dbSLuiz Otavio O Souza netmap_mem2_if_new(struct netmap_adapter *na, struct netmap_priv_d *priv) 1765ccdc3305SLuigi Rizzo { 1766ccdc3305SLuigi Rizzo struct netmap_if *nifp; 1767ccdc3305SLuigi Rizzo ssize_t base; /* handy for relative offsets between rings and nifp */ 1768847bf383SLuigi Rizzo u_int i, len, n[NR_TXRX], ntot; 1769847bf383SLuigi Rizzo enum txrx t; 1770ccdc3305SLuigi Rizzo 1771847bf383SLuigi Rizzo ntot = 0; 1772847bf383SLuigi Rizzo for_rx_tx(t) { 1773f0ea3689SLuigi Rizzo /* account for the (eventually fake) host rings */ 1774847bf383SLuigi Rizzo n[t] = nma_get_nrings(na, t) + 1; 1775847bf383SLuigi Rizzo ntot += n[t]; 1776847bf383SLuigi Rizzo } 1777ccdc3305SLuigi Rizzo /* 1778ccdc3305SLuigi Rizzo * the descriptor is followed inline by an array of offsets 1779ccdc3305SLuigi Rizzo * to the tx and rx rings in the shared memory region. 1780ccdc3305SLuigi Rizzo */ 1781ce3ee1e7SLuigi Rizzo 1782ce3ee1e7SLuigi Rizzo NMA_LOCK(na->nm_mem); 1783ce3ee1e7SLuigi Rizzo 1784847bf383SLuigi Rizzo len = sizeof(struct netmap_if) + (ntot * sizeof(ssize_t)); 1785ce3ee1e7SLuigi Rizzo nifp = netmap_if_malloc(na->nm_mem, len); 1786ccdc3305SLuigi Rizzo if (nifp == NULL) { 1787ce3ee1e7SLuigi Rizzo NMA_UNLOCK(na->nm_mem); 1788ccdc3305SLuigi Rizzo return NULL; 1789ccdc3305SLuigi Rizzo } 1790ccdc3305SLuigi Rizzo 1791ccdc3305SLuigi Rizzo /* initialize base fields -- override const */ 1792ce3ee1e7SLuigi Rizzo *(u_int *)(uintptr_t)&nifp->ni_tx_rings = na->num_tx_rings; 1793ce3ee1e7SLuigi Rizzo *(u_int *)(uintptr_t)&nifp->ni_rx_rings = na->num_rx_rings; 17944bf50f18SLuigi Rizzo strncpy(nifp->ni_name, na->name, (size_t)IFNAMSIZ); 1795ccdc3305SLuigi Rizzo 1796ccdc3305SLuigi Rizzo /* 1797ccdc3305SLuigi Rizzo * fill the slots for the rx and tx rings. They contain the offset 1798ccdc3305SLuigi Rizzo * between the ring and nifp, so the information is usable in 1799ccdc3305SLuigi Rizzo * userspace to reach the ring from the nifp. 1800ccdc3305SLuigi Rizzo */ 1801ce3ee1e7SLuigi Rizzo base = netmap_if_offset(na->nm_mem, nifp); 1802847bf383SLuigi Rizzo for (i = 0; i < n[NR_TX]; i++) { 1803c3e9b4dbSLuiz Otavio O Souza /* XXX instead of ofs == 0 maybe use the offset of an error 1804c3e9b4dbSLuiz Otavio O Souza * ring, like we do for buffers? */ 1805c3e9b4dbSLuiz Otavio O Souza ssize_t ofs = 0; 1806c3e9b4dbSLuiz Otavio O Souza 1807c3e9b4dbSLuiz Otavio O Souza if (na->tx_rings[i].ring != NULL && i >= priv->np_qfirst[NR_TX] 1808c3e9b4dbSLuiz Otavio O Souza && i < priv->np_qlast[NR_TX]) { 1809c3e9b4dbSLuiz Otavio O Souza ofs = netmap_ring_offset(na->nm_mem, 1810c3e9b4dbSLuiz Otavio O Souza na->tx_rings[i].ring) - base; 181137e3a6d3SLuigi Rizzo } 1812c3e9b4dbSLuiz Otavio O Souza *(ssize_t *)(uintptr_t)&nifp->ring_ofs[i] = ofs; 1813ccdc3305SLuigi Rizzo } 1814847bf383SLuigi Rizzo for (i = 0; i < n[NR_RX]; i++) { 1815c3e9b4dbSLuiz Otavio O Souza /* XXX instead of ofs == 0 maybe use the offset of an error 1816c3e9b4dbSLuiz Otavio O Souza * ring, like we do for buffers? */ 1817c3e9b4dbSLuiz Otavio O Souza ssize_t ofs = 0; 1818c3e9b4dbSLuiz Otavio O Souza 1819c3e9b4dbSLuiz Otavio O Souza if (na->rx_rings[i].ring != NULL && i >= priv->np_qfirst[NR_RX] 1820c3e9b4dbSLuiz Otavio O Souza && i < priv->np_qlast[NR_RX]) { 1821c3e9b4dbSLuiz Otavio O Souza ofs = netmap_ring_offset(na->nm_mem, 1822c3e9b4dbSLuiz Otavio O Souza na->rx_rings[i].ring) - base; 182337e3a6d3SLuigi Rizzo } 1824c3e9b4dbSLuiz Otavio O Souza *(ssize_t *)(uintptr_t)&nifp->ring_ofs[i+n[NR_TX]] = ofs; 1825ccdc3305SLuigi Rizzo } 1826ce3ee1e7SLuigi Rizzo 1827ce3ee1e7SLuigi Rizzo NMA_UNLOCK(na->nm_mem); 1828ce3ee1e7SLuigi Rizzo 1829ccdc3305SLuigi Rizzo return (nifp); 1830ccdc3305SLuigi Rizzo } 1831ccdc3305SLuigi Rizzo 1832847bf383SLuigi Rizzo static void 1833847bf383SLuigi Rizzo netmap_mem2_if_delete(struct netmap_adapter *na, struct netmap_if *nifp) 1834ccdc3305SLuigi Rizzo { 1835ce3ee1e7SLuigi Rizzo if (nifp == NULL) 1836ce3ee1e7SLuigi Rizzo /* nothing to do */ 1837ce3ee1e7SLuigi Rizzo return; 1838ce3ee1e7SLuigi Rizzo NMA_LOCK(na->nm_mem); 1839f0ea3689SLuigi Rizzo if (nifp->ni_bufs_head) 1840f0ea3689SLuigi Rizzo netmap_extra_free(na, nifp->ni_bufs_head); 1841ce3ee1e7SLuigi Rizzo netmap_if_free(na->nm_mem, nifp); 1842ce3ee1e7SLuigi Rizzo 1843ce3ee1e7SLuigi Rizzo NMA_UNLOCK(na->nm_mem); 1844ce3ee1e7SLuigi Rizzo } 1845ce3ee1e7SLuigi Rizzo 1846ce3ee1e7SLuigi Rizzo static void 1847c3e9b4dbSLuiz Otavio O Souza netmap_mem2_deref(struct netmap_mem_d *nmd) 1848ce3ee1e7SLuigi Rizzo { 1849ce3ee1e7SLuigi Rizzo 1850847bf383SLuigi Rizzo nmd->active--; 1851847bf383SLuigi Rizzo if (!nmd->active) 18524bf50f18SLuigi Rizzo nmd->nm_grp = -1; 1853ae10d1afSLuigi Rizzo if (netmap_verbose) 1854847bf383SLuigi Rizzo D("active = %d", nmd->active); 1855ce3ee1e7SLuigi Rizzo 1856ce3ee1e7SLuigi Rizzo } 1857ce3ee1e7SLuigi Rizzo 1858847bf383SLuigi Rizzo struct netmap_mem_ops netmap_mem_global_ops = { 1859847bf383SLuigi Rizzo .nmd_get_lut = netmap_mem2_get_lut, 1860847bf383SLuigi Rizzo .nmd_get_info = netmap_mem2_get_info, 1861847bf383SLuigi Rizzo .nmd_ofstophys = netmap_mem2_ofstophys, 1862c3e9b4dbSLuiz Otavio O Souza .nmd_config = netmap_mem2_config, 1863c3e9b4dbSLuiz Otavio O Souza .nmd_finalize = netmap_mem2_finalize, 1864c3e9b4dbSLuiz Otavio O Souza .nmd_deref = netmap_mem2_deref, 1865c3e9b4dbSLuiz Otavio O Souza .nmd_delete = netmap_mem2_delete, 1866847bf383SLuigi Rizzo .nmd_if_offset = netmap_mem2_if_offset, 1867847bf383SLuigi Rizzo .nmd_if_new = netmap_mem2_if_new, 1868847bf383SLuigi Rizzo .nmd_if_delete = netmap_mem2_if_delete, 1869847bf383SLuigi Rizzo .nmd_rings_create = netmap_mem2_rings_create, 1870847bf383SLuigi Rizzo .nmd_rings_delete = netmap_mem2_rings_delete 1871847bf383SLuigi Rizzo }; 187237e3a6d3SLuigi Rizzo 1873844a6f0cSLuigi Rizzo int 1874c3e9b4dbSLuiz Otavio O Souza netmap_mem_pools_info_get(struct nmreq *nmr, struct netmap_mem_d *nmd) 1875844a6f0cSLuigi Rizzo { 1876844a6f0cSLuigi Rizzo uintptr_t *pp = (uintptr_t *)&nmr->nr_arg1; 1877844a6f0cSLuigi Rizzo struct netmap_pools_info *upi = (struct netmap_pools_info *)(*pp); 1878844a6f0cSLuigi Rizzo struct netmap_pools_info pi; 1879844a6f0cSLuigi Rizzo unsigned int memsize; 1880844a6f0cSLuigi Rizzo uint16_t memid; 1881844a6f0cSLuigi Rizzo int ret; 1882844a6f0cSLuigi Rizzo 1883844a6f0cSLuigi Rizzo ret = netmap_mem_get_info(nmd, &memsize, NULL, &memid); 1884844a6f0cSLuigi Rizzo if (ret) { 1885844a6f0cSLuigi Rizzo return ret; 1886844a6f0cSLuigi Rizzo } 1887844a6f0cSLuigi Rizzo 1888844a6f0cSLuigi Rizzo pi.memsize = memsize; 1889844a6f0cSLuigi Rizzo pi.memid = memid; 1890c3e9b4dbSLuiz Otavio O Souza NMA_LOCK(nmd); 1891844a6f0cSLuigi Rizzo pi.if_pool_offset = 0; 1892844a6f0cSLuigi Rizzo pi.if_pool_objtotal = nmd->pools[NETMAP_IF_POOL].objtotal; 1893844a6f0cSLuigi Rizzo pi.if_pool_objsize = nmd->pools[NETMAP_IF_POOL]._objsize; 1894844a6f0cSLuigi Rizzo 1895844a6f0cSLuigi Rizzo pi.ring_pool_offset = nmd->pools[NETMAP_IF_POOL].memtotal; 1896844a6f0cSLuigi Rizzo pi.ring_pool_objtotal = nmd->pools[NETMAP_RING_POOL].objtotal; 1897844a6f0cSLuigi Rizzo pi.ring_pool_objsize = nmd->pools[NETMAP_RING_POOL]._objsize; 1898844a6f0cSLuigi Rizzo 1899844a6f0cSLuigi Rizzo pi.buf_pool_offset = nmd->pools[NETMAP_IF_POOL].memtotal + 1900844a6f0cSLuigi Rizzo nmd->pools[NETMAP_RING_POOL].memtotal; 1901844a6f0cSLuigi Rizzo pi.buf_pool_objtotal = nmd->pools[NETMAP_BUF_POOL].objtotal; 1902844a6f0cSLuigi Rizzo pi.buf_pool_objsize = nmd->pools[NETMAP_BUF_POOL]._objsize; 1903c3e9b4dbSLuiz Otavio O Souza NMA_UNLOCK(nmd); 1904844a6f0cSLuigi Rizzo 1905844a6f0cSLuigi Rizzo ret = copyout(&pi, upi, sizeof(pi)); 1906844a6f0cSLuigi Rizzo if (ret) { 1907844a6f0cSLuigi Rizzo return ret; 1908844a6f0cSLuigi Rizzo } 1909844a6f0cSLuigi Rizzo 1910844a6f0cSLuigi Rizzo return 0; 1911844a6f0cSLuigi Rizzo } 1912844a6f0cSLuigi Rizzo 191337e3a6d3SLuigi Rizzo #ifdef WITH_PTNETMAP_GUEST 191437e3a6d3SLuigi Rizzo struct mem_pt_if { 191537e3a6d3SLuigi Rizzo struct mem_pt_if *next; 191637e3a6d3SLuigi Rizzo struct ifnet *ifp; 191737e3a6d3SLuigi Rizzo unsigned int nifp_offset; 191837e3a6d3SLuigi Rizzo }; 191937e3a6d3SLuigi Rizzo 192037e3a6d3SLuigi Rizzo /* Netmap allocator for ptnetmap guests. */ 192137e3a6d3SLuigi Rizzo struct netmap_mem_ptg { 192237e3a6d3SLuigi Rizzo struct netmap_mem_d up; 192337e3a6d3SLuigi Rizzo 192437e3a6d3SLuigi Rizzo vm_paddr_t nm_paddr; /* physical address in the guest */ 192537e3a6d3SLuigi Rizzo void *nm_addr; /* virtual address in the guest */ 192637e3a6d3SLuigi Rizzo struct netmap_lut buf_lut; /* lookup table for BUF pool in the guest */ 1927844a6f0cSLuigi Rizzo nm_memid_t host_mem_id; /* allocator identifier in the host */ 1928844a6f0cSLuigi Rizzo struct ptnetmap_memdev *ptn_dev;/* ptnetmap memdev */ 192937e3a6d3SLuigi Rizzo struct mem_pt_if *pt_ifs; /* list of interfaces in passthrough */ 193037e3a6d3SLuigi Rizzo }; 193137e3a6d3SLuigi Rizzo 193237e3a6d3SLuigi Rizzo /* Link a passthrough interface to a passthrough netmap allocator. */ 193337e3a6d3SLuigi Rizzo static int 193437e3a6d3SLuigi Rizzo netmap_mem_pt_guest_ifp_add(struct netmap_mem_d *nmd, struct ifnet *ifp, 1935844a6f0cSLuigi Rizzo unsigned int nifp_offset) 193637e3a6d3SLuigi Rizzo { 193737e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd; 1938c3e9b4dbSLuiz Otavio O Souza struct mem_pt_if *ptif = nm_os_malloc(sizeof(*ptif)); 193937e3a6d3SLuigi Rizzo 194037e3a6d3SLuigi Rizzo if (!ptif) { 194137e3a6d3SLuigi Rizzo return ENOMEM; 194237e3a6d3SLuigi Rizzo } 194337e3a6d3SLuigi Rizzo 194437e3a6d3SLuigi Rizzo NMA_LOCK(nmd); 194537e3a6d3SLuigi Rizzo 194637e3a6d3SLuigi Rizzo ptif->ifp = ifp; 194737e3a6d3SLuigi Rizzo ptif->nifp_offset = nifp_offset; 194837e3a6d3SLuigi Rizzo 194937e3a6d3SLuigi Rizzo if (ptnmd->pt_ifs) { 195037e3a6d3SLuigi Rizzo ptif->next = ptnmd->pt_ifs; 195137e3a6d3SLuigi Rizzo } 195237e3a6d3SLuigi Rizzo ptnmd->pt_ifs = ptif; 195337e3a6d3SLuigi Rizzo 195437e3a6d3SLuigi Rizzo NMA_UNLOCK(nmd); 195537e3a6d3SLuigi Rizzo 195637e3a6d3SLuigi Rizzo D("added (ifp=%p,nifp_offset=%u)", ptif->ifp, ptif->nifp_offset); 195737e3a6d3SLuigi Rizzo 195837e3a6d3SLuigi Rizzo return 0; 195937e3a6d3SLuigi Rizzo } 196037e3a6d3SLuigi Rizzo 196137e3a6d3SLuigi Rizzo /* Called with NMA_LOCK(nmd) held. */ 196237e3a6d3SLuigi Rizzo static struct mem_pt_if * 196337e3a6d3SLuigi Rizzo netmap_mem_pt_guest_ifp_lookup(struct netmap_mem_d *nmd, struct ifnet *ifp) 196437e3a6d3SLuigi Rizzo { 196537e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd; 196637e3a6d3SLuigi Rizzo struct mem_pt_if *curr; 196737e3a6d3SLuigi Rizzo 196837e3a6d3SLuigi Rizzo for (curr = ptnmd->pt_ifs; curr; curr = curr->next) { 196937e3a6d3SLuigi Rizzo if (curr->ifp == ifp) { 197037e3a6d3SLuigi Rizzo return curr; 197137e3a6d3SLuigi Rizzo } 197237e3a6d3SLuigi Rizzo } 197337e3a6d3SLuigi Rizzo 197437e3a6d3SLuigi Rizzo return NULL; 197537e3a6d3SLuigi Rizzo } 197637e3a6d3SLuigi Rizzo 197737e3a6d3SLuigi Rizzo /* Unlink a passthrough interface from a passthrough netmap allocator. */ 197837e3a6d3SLuigi Rizzo int 197937e3a6d3SLuigi Rizzo netmap_mem_pt_guest_ifp_del(struct netmap_mem_d *nmd, struct ifnet *ifp) 198037e3a6d3SLuigi Rizzo { 198137e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd; 198237e3a6d3SLuigi Rizzo struct mem_pt_if *prev = NULL; 198337e3a6d3SLuigi Rizzo struct mem_pt_if *curr; 198437e3a6d3SLuigi Rizzo int ret = -1; 198537e3a6d3SLuigi Rizzo 198637e3a6d3SLuigi Rizzo NMA_LOCK(nmd); 198737e3a6d3SLuigi Rizzo 198837e3a6d3SLuigi Rizzo for (curr = ptnmd->pt_ifs; curr; curr = curr->next) { 198937e3a6d3SLuigi Rizzo if (curr->ifp == ifp) { 199037e3a6d3SLuigi Rizzo if (prev) { 199137e3a6d3SLuigi Rizzo prev->next = curr->next; 199237e3a6d3SLuigi Rizzo } else { 199337e3a6d3SLuigi Rizzo ptnmd->pt_ifs = curr->next; 199437e3a6d3SLuigi Rizzo } 199537e3a6d3SLuigi Rizzo D("removed (ifp=%p,nifp_offset=%u)", 199637e3a6d3SLuigi Rizzo curr->ifp, curr->nifp_offset); 1997c3e9b4dbSLuiz Otavio O Souza nm_os_free(curr); 199837e3a6d3SLuigi Rizzo ret = 0; 199937e3a6d3SLuigi Rizzo break; 200037e3a6d3SLuigi Rizzo } 200137e3a6d3SLuigi Rizzo prev = curr; 200237e3a6d3SLuigi Rizzo } 200337e3a6d3SLuigi Rizzo 200437e3a6d3SLuigi Rizzo NMA_UNLOCK(nmd); 200537e3a6d3SLuigi Rizzo 200637e3a6d3SLuigi Rizzo return ret; 200737e3a6d3SLuigi Rizzo } 200837e3a6d3SLuigi Rizzo 200937e3a6d3SLuigi Rizzo static int 201037e3a6d3SLuigi Rizzo netmap_mem_pt_guest_get_lut(struct netmap_mem_d *nmd, struct netmap_lut *lut) 201137e3a6d3SLuigi Rizzo { 201237e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd; 201337e3a6d3SLuigi Rizzo 201437e3a6d3SLuigi Rizzo if (!(nmd->flags & NETMAP_MEM_FINALIZED)) { 201537e3a6d3SLuigi Rizzo return EINVAL; 201637e3a6d3SLuigi Rizzo } 201737e3a6d3SLuigi Rizzo 201837e3a6d3SLuigi Rizzo *lut = ptnmd->buf_lut; 201937e3a6d3SLuigi Rizzo return 0; 202037e3a6d3SLuigi Rizzo } 202137e3a6d3SLuigi Rizzo 202237e3a6d3SLuigi Rizzo static int 202337e3a6d3SLuigi Rizzo netmap_mem_pt_guest_get_info(struct netmap_mem_d *nmd, u_int *size, 202437e3a6d3SLuigi Rizzo u_int *memflags, uint16_t *id) 202537e3a6d3SLuigi Rizzo { 202637e3a6d3SLuigi Rizzo int error = 0; 202737e3a6d3SLuigi Rizzo 202837e3a6d3SLuigi Rizzo NMA_LOCK(nmd); 202937e3a6d3SLuigi Rizzo 203037e3a6d3SLuigi Rizzo error = nmd->ops->nmd_config(nmd); 203137e3a6d3SLuigi Rizzo if (error) 203237e3a6d3SLuigi Rizzo goto out; 203337e3a6d3SLuigi Rizzo 203437e3a6d3SLuigi Rizzo if (size) 203537e3a6d3SLuigi Rizzo *size = nmd->nm_totalsize; 203637e3a6d3SLuigi Rizzo if (memflags) 203737e3a6d3SLuigi Rizzo *memflags = nmd->flags; 203837e3a6d3SLuigi Rizzo if (id) 203937e3a6d3SLuigi Rizzo *id = nmd->nm_id; 204037e3a6d3SLuigi Rizzo 204137e3a6d3SLuigi Rizzo out: 204237e3a6d3SLuigi Rizzo NMA_UNLOCK(nmd); 204337e3a6d3SLuigi Rizzo 204437e3a6d3SLuigi Rizzo return error; 204537e3a6d3SLuigi Rizzo } 204637e3a6d3SLuigi Rizzo 204737e3a6d3SLuigi Rizzo static vm_paddr_t 204837e3a6d3SLuigi Rizzo netmap_mem_pt_guest_ofstophys(struct netmap_mem_d *nmd, vm_ooffset_t off) 204937e3a6d3SLuigi Rizzo { 205037e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd; 205137e3a6d3SLuigi Rizzo vm_paddr_t paddr; 205237e3a6d3SLuigi Rizzo /* if the offset is valid, just return csb->base_addr + off */ 205337e3a6d3SLuigi Rizzo paddr = (vm_paddr_t)(ptnmd->nm_paddr + off); 205437e3a6d3SLuigi Rizzo ND("off %lx padr %lx", off, (unsigned long)paddr); 205537e3a6d3SLuigi Rizzo return paddr; 205637e3a6d3SLuigi Rizzo } 205737e3a6d3SLuigi Rizzo 205837e3a6d3SLuigi Rizzo static int 205937e3a6d3SLuigi Rizzo netmap_mem_pt_guest_config(struct netmap_mem_d *nmd) 206037e3a6d3SLuigi Rizzo { 206137e3a6d3SLuigi Rizzo /* nothing to do, we are configured on creation 206237e3a6d3SLuigi Rizzo * and configuration never changes thereafter 206337e3a6d3SLuigi Rizzo */ 206437e3a6d3SLuigi Rizzo return 0; 206537e3a6d3SLuigi Rizzo } 206637e3a6d3SLuigi Rizzo 206737e3a6d3SLuigi Rizzo static int 206837e3a6d3SLuigi Rizzo netmap_mem_pt_guest_finalize(struct netmap_mem_d *nmd) 206937e3a6d3SLuigi Rizzo { 207037e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd; 2071844a6f0cSLuigi Rizzo uint64_t mem_size; 2072844a6f0cSLuigi Rizzo uint32_t bufsize; 2073844a6f0cSLuigi Rizzo uint32_t nbuffers; 2074844a6f0cSLuigi Rizzo uint32_t poolofs; 2075844a6f0cSLuigi Rizzo vm_paddr_t paddr; 2076844a6f0cSLuigi Rizzo char *vaddr; 2077844a6f0cSLuigi Rizzo int i; 207837e3a6d3SLuigi Rizzo int error = 0; 207937e3a6d3SLuigi Rizzo 208037e3a6d3SLuigi Rizzo nmd->active++; 208137e3a6d3SLuigi Rizzo 208237e3a6d3SLuigi Rizzo if (nmd->flags & NETMAP_MEM_FINALIZED) 208337e3a6d3SLuigi Rizzo goto out; 208437e3a6d3SLuigi Rizzo 208537e3a6d3SLuigi Rizzo if (ptnmd->ptn_dev == NULL) { 208637e3a6d3SLuigi Rizzo D("ptnetmap memdev not attached"); 208737e3a6d3SLuigi Rizzo error = ENOMEM; 208837e3a6d3SLuigi Rizzo goto err; 208937e3a6d3SLuigi Rizzo } 2090844a6f0cSLuigi Rizzo /* Map memory through ptnetmap-memdev BAR. */ 209137e3a6d3SLuigi Rizzo error = nm_os_pt_memdev_iomap(ptnmd->ptn_dev, &ptnmd->nm_paddr, 2092844a6f0cSLuigi Rizzo &ptnmd->nm_addr, &mem_size); 209337e3a6d3SLuigi Rizzo if (error) 209437e3a6d3SLuigi Rizzo goto err; 209537e3a6d3SLuigi Rizzo 2096844a6f0cSLuigi Rizzo /* Initialize the lut using the information contained in the 2097844a6f0cSLuigi Rizzo * ptnetmap memory device. */ 2098844a6f0cSLuigi Rizzo bufsize = nm_os_pt_memdev_ioread(ptnmd->ptn_dev, 2099844a6f0cSLuigi Rizzo PTNET_MDEV_IO_BUF_POOL_OBJSZ); 2100844a6f0cSLuigi Rizzo nbuffers = nm_os_pt_memdev_ioread(ptnmd->ptn_dev, 2101844a6f0cSLuigi Rizzo PTNET_MDEV_IO_BUF_POOL_OBJNUM); 2102844a6f0cSLuigi Rizzo 2103844a6f0cSLuigi Rizzo /* allocate the lut */ 2104844a6f0cSLuigi Rizzo if (ptnmd->buf_lut.lut == NULL) { 2105844a6f0cSLuigi Rizzo D("allocating lut"); 2106844a6f0cSLuigi Rizzo ptnmd->buf_lut.lut = nm_alloc_lut(nbuffers); 2107844a6f0cSLuigi Rizzo if (ptnmd->buf_lut.lut == NULL) { 2108844a6f0cSLuigi Rizzo D("lut allocation failed"); 2109844a6f0cSLuigi Rizzo return ENOMEM; 2110844a6f0cSLuigi Rizzo } 2111844a6f0cSLuigi Rizzo } 2112844a6f0cSLuigi Rizzo 2113844a6f0cSLuigi Rizzo /* we have physically contiguous memory mapped through PCI BAR */ 2114844a6f0cSLuigi Rizzo poolofs = nm_os_pt_memdev_ioread(ptnmd->ptn_dev, 2115844a6f0cSLuigi Rizzo PTNET_MDEV_IO_BUF_POOL_OFS); 2116844a6f0cSLuigi Rizzo vaddr = (char *)(ptnmd->nm_addr) + poolofs; 2117844a6f0cSLuigi Rizzo paddr = ptnmd->nm_paddr + poolofs; 2118844a6f0cSLuigi Rizzo 2119844a6f0cSLuigi Rizzo for (i = 0; i < nbuffers; i++) { 2120844a6f0cSLuigi Rizzo ptnmd->buf_lut.lut[i].vaddr = vaddr; 2121844a6f0cSLuigi Rizzo ptnmd->buf_lut.lut[i].paddr = paddr; 2122844a6f0cSLuigi Rizzo vaddr += bufsize; 2123844a6f0cSLuigi Rizzo paddr += bufsize; 2124844a6f0cSLuigi Rizzo } 2125844a6f0cSLuigi Rizzo 2126844a6f0cSLuigi Rizzo ptnmd->buf_lut.objtotal = nbuffers; 2127844a6f0cSLuigi Rizzo ptnmd->buf_lut.objsize = bufsize; 2128844a6f0cSLuigi Rizzo nmd->nm_totalsize = (unsigned int)mem_size; 212937e3a6d3SLuigi Rizzo 213037e3a6d3SLuigi Rizzo nmd->flags |= NETMAP_MEM_FINALIZED; 213137e3a6d3SLuigi Rizzo out: 213237e3a6d3SLuigi Rizzo return 0; 213337e3a6d3SLuigi Rizzo err: 213437e3a6d3SLuigi Rizzo nmd->active--; 213537e3a6d3SLuigi Rizzo return error; 213637e3a6d3SLuigi Rizzo } 213737e3a6d3SLuigi Rizzo 213837e3a6d3SLuigi Rizzo static void 213937e3a6d3SLuigi Rizzo netmap_mem_pt_guest_deref(struct netmap_mem_d *nmd) 214037e3a6d3SLuigi Rizzo { 214137e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd; 214237e3a6d3SLuigi Rizzo 214337e3a6d3SLuigi Rizzo nmd->active--; 214437e3a6d3SLuigi Rizzo if (nmd->active <= 0 && 214537e3a6d3SLuigi Rizzo (nmd->flags & NETMAP_MEM_FINALIZED)) { 214637e3a6d3SLuigi Rizzo nmd->flags &= ~NETMAP_MEM_FINALIZED; 214737e3a6d3SLuigi Rizzo /* unmap ptnetmap-memdev memory */ 214837e3a6d3SLuigi Rizzo if (ptnmd->ptn_dev) { 214937e3a6d3SLuigi Rizzo nm_os_pt_memdev_iounmap(ptnmd->ptn_dev); 215037e3a6d3SLuigi Rizzo } 21514dd44461SLuiz Otavio O Souza ptnmd->nm_addr = NULL; 215237e3a6d3SLuigi Rizzo ptnmd->nm_paddr = 0; 215337e3a6d3SLuigi Rizzo } 215437e3a6d3SLuigi Rizzo } 215537e3a6d3SLuigi Rizzo 215637e3a6d3SLuigi Rizzo static ssize_t 215737e3a6d3SLuigi Rizzo netmap_mem_pt_guest_if_offset(struct netmap_mem_d *nmd, const void *vaddr) 215837e3a6d3SLuigi Rizzo { 215937e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)nmd; 216037e3a6d3SLuigi Rizzo 216137e3a6d3SLuigi Rizzo return (const char *)(vaddr) - (char *)(ptnmd->nm_addr); 216237e3a6d3SLuigi Rizzo } 216337e3a6d3SLuigi Rizzo 216437e3a6d3SLuigi Rizzo static void 216537e3a6d3SLuigi Rizzo netmap_mem_pt_guest_delete(struct netmap_mem_d *nmd) 216637e3a6d3SLuigi Rizzo { 216737e3a6d3SLuigi Rizzo if (nmd == NULL) 216837e3a6d3SLuigi Rizzo return; 216937e3a6d3SLuigi Rizzo if (netmap_verbose) 217037e3a6d3SLuigi Rizzo D("deleting %p", nmd); 217137e3a6d3SLuigi Rizzo if (nmd->active > 0) 217237e3a6d3SLuigi Rizzo D("bug: deleting mem allocator with active=%d!", nmd->active); 217337e3a6d3SLuigi Rizzo if (netmap_verbose) 217437e3a6d3SLuigi Rizzo D("done deleting %p", nmd); 217537e3a6d3SLuigi Rizzo NMA_LOCK_DESTROY(nmd); 2176c3e9b4dbSLuiz Otavio O Souza nm_os_free(nmd); 217737e3a6d3SLuigi Rizzo } 217837e3a6d3SLuigi Rizzo 217937e3a6d3SLuigi Rizzo static struct netmap_if * 2180c3e9b4dbSLuiz Otavio O Souza netmap_mem_pt_guest_if_new(struct netmap_adapter *na, struct netmap_priv_d *priv) 218137e3a6d3SLuigi Rizzo { 218237e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)na->nm_mem; 218337e3a6d3SLuigi Rizzo struct mem_pt_if *ptif; 218437e3a6d3SLuigi Rizzo struct netmap_if *nifp = NULL; 218537e3a6d3SLuigi Rizzo 218637e3a6d3SLuigi Rizzo NMA_LOCK(na->nm_mem); 218737e3a6d3SLuigi Rizzo 218837e3a6d3SLuigi Rizzo ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, na->ifp); 218937e3a6d3SLuigi Rizzo if (ptif == NULL) { 219037e3a6d3SLuigi Rizzo D("Error: interface %p is not in passthrough", na->ifp); 219137e3a6d3SLuigi Rizzo goto out; 219237e3a6d3SLuigi Rizzo } 219337e3a6d3SLuigi Rizzo 219437e3a6d3SLuigi Rizzo nifp = (struct netmap_if *)((char *)(ptnmd->nm_addr) + 219537e3a6d3SLuigi Rizzo ptif->nifp_offset); 219637e3a6d3SLuigi Rizzo NMA_UNLOCK(na->nm_mem); 219737e3a6d3SLuigi Rizzo out: 219837e3a6d3SLuigi Rizzo return nifp; 219937e3a6d3SLuigi Rizzo } 220037e3a6d3SLuigi Rizzo 220137e3a6d3SLuigi Rizzo static void 220237e3a6d3SLuigi Rizzo netmap_mem_pt_guest_if_delete(struct netmap_adapter *na, struct netmap_if *nifp) 220337e3a6d3SLuigi Rizzo { 220437e3a6d3SLuigi Rizzo struct mem_pt_if *ptif; 220537e3a6d3SLuigi Rizzo 220637e3a6d3SLuigi Rizzo NMA_LOCK(na->nm_mem); 220737e3a6d3SLuigi Rizzo ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, na->ifp); 220837e3a6d3SLuigi Rizzo if (ptif == NULL) { 220937e3a6d3SLuigi Rizzo D("Error: interface %p is not in passthrough", na->ifp); 221037e3a6d3SLuigi Rizzo } 221137e3a6d3SLuigi Rizzo NMA_UNLOCK(na->nm_mem); 221237e3a6d3SLuigi Rizzo } 221337e3a6d3SLuigi Rizzo 221437e3a6d3SLuigi Rizzo static int 221537e3a6d3SLuigi Rizzo netmap_mem_pt_guest_rings_create(struct netmap_adapter *na) 221637e3a6d3SLuigi Rizzo { 221737e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)na->nm_mem; 221837e3a6d3SLuigi Rizzo struct mem_pt_if *ptif; 221937e3a6d3SLuigi Rizzo struct netmap_if *nifp; 222037e3a6d3SLuigi Rizzo int i, error = -1; 222137e3a6d3SLuigi Rizzo 222237e3a6d3SLuigi Rizzo NMA_LOCK(na->nm_mem); 222337e3a6d3SLuigi Rizzo 222437e3a6d3SLuigi Rizzo ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, na->ifp); 222537e3a6d3SLuigi Rizzo if (ptif == NULL) { 222637e3a6d3SLuigi Rizzo D("Error: interface %p is not in passthrough", na->ifp); 222737e3a6d3SLuigi Rizzo goto out; 222837e3a6d3SLuigi Rizzo } 222937e3a6d3SLuigi Rizzo 223037e3a6d3SLuigi Rizzo 223137e3a6d3SLuigi Rizzo /* point each kring to the corresponding backend ring */ 223237e3a6d3SLuigi Rizzo nifp = (struct netmap_if *)((char *)ptnmd->nm_addr + ptif->nifp_offset); 223337e3a6d3SLuigi Rizzo for (i = 0; i <= na->num_tx_rings; i++) { 223437e3a6d3SLuigi Rizzo struct netmap_kring *kring = na->tx_rings + i; 223537e3a6d3SLuigi Rizzo if (kring->ring) 223637e3a6d3SLuigi Rizzo continue; 223737e3a6d3SLuigi Rizzo kring->ring = (struct netmap_ring *) 223837e3a6d3SLuigi Rizzo ((char *)nifp + nifp->ring_ofs[i]); 223937e3a6d3SLuigi Rizzo } 224037e3a6d3SLuigi Rizzo for (i = 0; i <= na->num_rx_rings; i++) { 224137e3a6d3SLuigi Rizzo struct netmap_kring *kring = na->rx_rings + i; 224237e3a6d3SLuigi Rizzo if (kring->ring) 224337e3a6d3SLuigi Rizzo continue; 224437e3a6d3SLuigi Rizzo kring->ring = (struct netmap_ring *) 224537e3a6d3SLuigi Rizzo ((char *)nifp + 224637e3a6d3SLuigi Rizzo nifp->ring_ofs[i + na->num_tx_rings + 1]); 224737e3a6d3SLuigi Rizzo } 224837e3a6d3SLuigi Rizzo 224937e3a6d3SLuigi Rizzo error = 0; 225037e3a6d3SLuigi Rizzo out: 225137e3a6d3SLuigi Rizzo NMA_UNLOCK(na->nm_mem); 225237e3a6d3SLuigi Rizzo 225337e3a6d3SLuigi Rizzo return error; 225437e3a6d3SLuigi Rizzo } 225537e3a6d3SLuigi Rizzo 225637e3a6d3SLuigi Rizzo static void 225737e3a6d3SLuigi Rizzo netmap_mem_pt_guest_rings_delete(struct netmap_adapter *na) 225837e3a6d3SLuigi Rizzo { 225937e3a6d3SLuigi Rizzo /* TODO: remove?? */ 226037e3a6d3SLuigi Rizzo #if 0 226137e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd = (struct netmap_mem_ptg *)na->nm_mem; 226237e3a6d3SLuigi Rizzo struct mem_pt_if *ptif = netmap_mem_pt_guest_ifp_lookup(na->nm_mem, 226337e3a6d3SLuigi Rizzo na->ifp); 226437e3a6d3SLuigi Rizzo #endif 226537e3a6d3SLuigi Rizzo } 226637e3a6d3SLuigi Rizzo 226737e3a6d3SLuigi Rizzo static struct netmap_mem_ops netmap_mem_pt_guest_ops = { 226837e3a6d3SLuigi Rizzo .nmd_get_lut = netmap_mem_pt_guest_get_lut, 226937e3a6d3SLuigi Rizzo .nmd_get_info = netmap_mem_pt_guest_get_info, 227037e3a6d3SLuigi Rizzo .nmd_ofstophys = netmap_mem_pt_guest_ofstophys, 227137e3a6d3SLuigi Rizzo .nmd_config = netmap_mem_pt_guest_config, 227237e3a6d3SLuigi Rizzo .nmd_finalize = netmap_mem_pt_guest_finalize, 227337e3a6d3SLuigi Rizzo .nmd_deref = netmap_mem_pt_guest_deref, 227437e3a6d3SLuigi Rizzo .nmd_if_offset = netmap_mem_pt_guest_if_offset, 227537e3a6d3SLuigi Rizzo .nmd_delete = netmap_mem_pt_guest_delete, 227637e3a6d3SLuigi Rizzo .nmd_if_new = netmap_mem_pt_guest_if_new, 227737e3a6d3SLuigi Rizzo .nmd_if_delete = netmap_mem_pt_guest_if_delete, 227837e3a6d3SLuigi Rizzo .nmd_rings_create = netmap_mem_pt_guest_rings_create, 227937e3a6d3SLuigi Rizzo .nmd_rings_delete = netmap_mem_pt_guest_rings_delete 228037e3a6d3SLuigi Rizzo }; 228137e3a6d3SLuigi Rizzo 2282c3e9b4dbSLuiz Otavio O Souza /* Called with nm_mem_list_lock held. */ 228337e3a6d3SLuigi Rizzo static struct netmap_mem_d * 2284844a6f0cSLuigi Rizzo netmap_mem_pt_guest_find_memid(nm_memid_t mem_id) 228537e3a6d3SLuigi Rizzo { 228637e3a6d3SLuigi Rizzo struct netmap_mem_d *mem = NULL; 228737e3a6d3SLuigi Rizzo struct netmap_mem_d *scan = netmap_last_mem_d; 228837e3a6d3SLuigi Rizzo 228937e3a6d3SLuigi Rizzo do { 229037e3a6d3SLuigi Rizzo /* find ptnetmap allocator through host ID */ 229137e3a6d3SLuigi Rizzo if (scan->ops->nmd_deref == netmap_mem_pt_guest_deref && 2292844a6f0cSLuigi Rizzo ((struct netmap_mem_ptg *)(scan))->host_mem_id == mem_id) { 229337e3a6d3SLuigi Rizzo mem = scan; 2294c3e9b4dbSLuiz Otavio O Souza mem->refcount++; 2295c3e9b4dbSLuiz Otavio O Souza NM_DBG_REFC(mem, __FUNCTION__, __LINE__); 229637e3a6d3SLuigi Rizzo break; 229737e3a6d3SLuigi Rizzo } 229837e3a6d3SLuigi Rizzo scan = scan->next; 229937e3a6d3SLuigi Rizzo } while (scan != netmap_last_mem_d); 230037e3a6d3SLuigi Rizzo 230137e3a6d3SLuigi Rizzo return mem; 230237e3a6d3SLuigi Rizzo } 230337e3a6d3SLuigi Rizzo 2304c3e9b4dbSLuiz Otavio O Souza /* Called with nm_mem_list_lock held. */ 230537e3a6d3SLuigi Rizzo static struct netmap_mem_d * 2306844a6f0cSLuigi Rizzo netmap_mem_pt_guest_create(nm_memid_t mem_id) 230737e3a6d3SLuigi Rizzo { 230837e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd; 230937e3a6d3SLuigi Rizzo int err = 0; 231037e3a6d3SLuigi Rizzo 2311c3e9b4dbSLuiz Otavio O Souza ptnmd = nm_os_malloc(sizeof(struct netmap_mem_ptg)); 231237e3a6d3SLuigi Rizzo if (ptnmd == NULL) { 231337e3a6d3SLuigi Rizzo err = ENOMEM; 231437e3a6d3SLuigi Rizzo goto error; 231537e3a6d3SLuigi Rizzo } 231637e3a6d3SLuigi Rizzo 231737e3a6d3SLuigi Rizzo ptnmd->up.ops = &netmap_mem_pt_guest_ops; 2318844a6f0cSLuigi Rizzo ptnmd->host_mem_id = mem_id; 231937e3a6d3SLuigi Rizzo ptnmd->pt_ifs = NULL; 232037e3a6d3SLuigi Rizzo 232137e3a6d3SLuigi Rizzo /* Assign new id in the guest (We have the lock) */ 232237e3a6d3SLuigi Rizzo err = nm_mem_assign_id_locked(&ptnmd->up); 232337e3a6d3SLuigi Rizzo if (err) 232437e3a6d3SLuigi Rizzo goto error; 232537e3a6d3SLuigi Rizzo 232637e3a6d3SLuigi Rizzo ptnmd->up.flags &= ~NETMAP_MEM_FINALIZED; 232737e3a6d3SLuigi Rizzo ptnmd->up.flags |= NETMAP_MEM_IO; 232837e3a6d3SLuigi Rizzo 232937e3a6d3SLuigi Rizzo NMA_LOCK_INIT(&ptnmd->up); 233037e3a6d3SLuigi Rizzo 2331c3e9b4dbSLuiz Otavio O Souza snprintf(ptnmd->up.name, NM_MEM_NAMESZ, "%d", ptnmd->up.nm_id); 2332c3e9b4dbSLuiz Otavio O Souza 2333c3e9b4dbSLuiz Otavio O Souza 233437e3a6d3SLuigi Rizzo return &ptnmd->up; 233537e3a6d3SLuigi Rizzo error: 233637e3a6d3SLuigi Rizzo netmap_mem_pt_guest_delete(&ptnmd->up); 233737e3a6d3SLuigi Rizzo return NULL; 233837e3a6d3SLuigi Rizzo } 233937e3a6d3SLuigi Rizzo 234037e3a6d3SLuigi Rizzo /* 234137e3a6d3SLuigi Rizzo * find host id in guest allocators and create guest allocator 234237e3a6d3SLuigi Rizzo * if it is not there 234337e3a6d3SLuigi Rizzo */ 234437e3a6d3SLuigi Rizzo static struct netmap_mem_d * 2345844a6f0cSLuigi Rizzo netmap_mem_pt_guest_get(nm_memid_t mem_id) 234637e3a6d3SLuigi Rizzo { 234737e3a6d3SLuigi Rizzo struct netmap_mem_d *nmd; 234837e3a6d3SLuigi Rizzo 2349c3e9b4dbSLuiz Otavio O Souza NM_MTX_LOCK(nm_mem_list_lock); 2350844a6f0cSLuigi Rizzo nmd = netmap_mem_pt_guest_find_memid(mem_id); 235137e3a6d3SLuigi Rizzo if (nmd == NULL) { 2352844a6f0cSLuigi Rizzo nmd = netmap_mem_pt_guest_create(mem_id); 235337e3a6d3SLuigi Rizzo } 2354c3e9b4dbSLuiz Otavio O Souza NM_MTX_UNLOCK(nm_mem_list_lock); 235537e3a6d3SLuigi Rizzo 235637e3a6d3SLuigi Rizzo return nmd; 235737e3a6d3SLuigi Rizzo } 235837e3a6d3SLuigi Rizzo 235937e3a6d3SLuigi Rizzo /* 236037e3a6d3SLuigi Rizzo * The guest allocator can be created by ptnetmap_memdev (during the device 2361844a6f0cSLuigi Rizzo * attach) or by ptnetmap device (ptnet), during the netmap_attach. 236237e3a6d3SLuigi Rizzo * 236337e3a6d3SLuigi Rizzo * The order is not important (we have different order in LINUX and FreeBSD). 236437e3a6d3SLuigi Rizzo * The first one, creates the device, and the second one simply attaches it. 236537e3a6d3SLuigi Rizzo */ 236637e3a6d3SLuigi Rizzo 236737e3a6d3SLuigi Rizzo /* Called when ptnetmap_memdev is attaching, to attach a new allocator in 236837e3a6d3SLuigi Rizzo * the guest */ 236937e3a6d3SLuigi Rizzo struct netmap_mem_d * 2370844a6f0cSLuigi Rizzo netmap_mem_pt_guest_attach(struct ptnetmap_memdev *ptn_dev, nm_memid_t mem_id) 237137e3a6d3SLuigi Rizzo { 237237e3a6d3SLuigi Rizzo struct netmap_mem_d *nmd; 237337e3a6d3SLuigi Rizzo struct netmap_mem_ptg *ptnmd; 237437e3a6d3SLuigi Rizzo 2375844a6f0cSLuigi Rizzo nmd = netmap_mem_pt_guest_get(mem_id); 237637e3a6d3SLuigi Rizzo 237737e3a6d3SLuigi Rizzo /* assign this device to the guest allocator */ 237837e3a6d3SLuigi Rizzo if (nmd) { 237937e3a6d3SLuigi Rizzo ptnmd = (struct netmap_mem_ptg *)nmd; 238037e3a6d3SLuigi Rizzo ptnmd->ptn_dev = ptn_dev; 238137e3a6d3SLuigi Rizzo } 238237e3a6d3SLuigi Rizzo 238337e3a6d3SLuigi Rizzo return nmd; 238437e3a6d3SLuigi Rizzo } 238537e3a6d3SLuigi Rizzo 2386844a6f0cSLuigi Rizzo /* Called when ptnet device is attaching */ 238737e3a6d3SLuigi Rizzo struct netmap_mem_d * 238837e3a6d3SLuigi Rizzo netmap_mem_pt_guest_new(struct ifnet *ifp, 238937e3a6d3SLuigi Rizzo unsigned int nifp_offset, 2390844a6f0cSLuigi Rizzo unsigned int memid) 239137e3a6d3SLuigi Rizzo { 239237e3a6d3SLuigi Rizzo struct netmap_mem_d *nmd; 239337e3a6d3SLuigi Rizzo 2394844a6f0cSLuigi Rizzo if (ifp == NULL) { 239537e3a6d3SLuigi Rizzo return NULL; 239637e3a6d3SLuigi Rizzo } 239737e3a6d3SLuigi Rizzo 2398844a6f0cSLuigi Rizzo nmd = netmap_mem_pt_guest_get((nm_memid_t)memid); 239937e3a6d3SLuigi Rizzo 240037e3a6d3SLuigi Rizzo if (nmd) { 2401844a6f0cSLuigi Rizzo netmap_mem_pt_guest_ifp_add(nmd, ifp, nifp_offset); 240237e3a6d3SLuigi Rizzo } 240337e3a6d3SLuigi Rizzo 240437e3a6d3SLuigi Rizzo return nmd; 240537e3a6d3SLuigi Rizzo } 240637e3a6d3SLuigi Rizzo 240737e3a6d3SLuigi Rizzo #endif /* WITH_PTNETMAP_GUEST */ 2408