xref: /freebsd/sys/dev/netmap/netmap.c (revision c85cb1a0a2428eecb95aa24cca1827b566ac5c91)
168b8534bSLuigi Rizzo /*
268b8534bSLuigi Rizzo  * Copyright (C) 2011 Matteo Landi, Luigi Rizzo. All rights reserved.
368b8534bSLuigi Rizzo  *
468b8534bSLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
568b8534bSLuigi Rizzo  * modification, are permitted provided that the following conditions
668b8534bSLuigi Rizzo  * are met:
768b8534bSLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
868b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
968b8534bSLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
1068b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
1168b8534bSLuigi Rizzo  *    documentation and/or other materials provided with the distribution.
1268b8534bSLuigi Rizzo  *
1368b8534bSLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1468b8534bSLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1568b8534bSLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1668b8534bSLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1768b8534bSLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1868b8534bSLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1968b8534bSLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2068b8534bSLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2168b8534bSLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2268b8534bSLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2368b8534bSLuigi Rizzo  * SUCH DAMAGE.
2468b8534bSLuigi Rizzo  */
2568b8534bSLuigi Rizzo 
2668b8534bSLuigi Rizzo /*
2768b8534bSLuigi Rizzo  * This module supports memory mapped access to network devices,
2868b8534bSLuigi Rizzo  * see netmap(4).
2968b8534bSLuigi Rizzo  *
3068b8534bSLuigi Rizzo  * The module uses a large, memory pool allocated by the kernel
3168b8534bSLuigi Rizzo  * and accessible as mmapped memory by multiple userspace threads/processes.
3268b8534bSLuigi Rizzo  * The memory pool contains packet buffers and "netmap rings",
3368b8534bSLuigi Rizzo  * i.e. user-accessible copies of the interface's queues.
3468b8534bSLuigi Rizzo  *
3568b8534bSLuigi Rizzo  * Access to the network card works like this:
3668b8534bSLuigi Rizzo  * 1. a process/thread issues one or more open() on /dev/netmap, to create
3768b8534bSLuigi Rizzo  *    select()able file descriptor on which events are reported.
3868b8534bSLuigi Rizzo  * 2. on each descriptor, the process issues an ioctl() to identify
3968b8534bSLuigi Rizzo  *    the interface that should report events to the file descriptor.
4068b8534bSLuigi Rizzo  * 3. on each descriptor, the process issues an mmap() request to
4168b8534bSLuigi Rizzo  *    map the shared memory region within the process' address space.
4268b8534bSLuigi Rizzo  *    The list of interesting queues is indicated by a location in
4368b8534bSLuigi Rizzo  *    the shared memory region.
4468b8534bSLuigi Rizzo  * 4. using the functions in the netmap(4) userspace API, a process
4568b8534bSLuigi Rizzo  *    can look up the occupation state of a queue, access memory buffers,
4668b8534bSLuigi Rizzo  *    and retrieve received packets or enqueue packets to transmit.
4768b8534bSLuigi Rizzo  * 5. using some ioctl()s the process can synchronize the userspace view
4868b8534bSLuigi Rizzo  *    of the queue with the actual status in the kernel. This includes both
4968b8534bSLuigi Rizzo  *    receiving the notification of new packets, and transmitting new
5068b8534bSLuigi Rizzo  *    packets on the output interface.
5168b8534bSLuigi Rizzo  * 6. select() or poll() can be used to wait for events on individual
5268b8534bSLuigi Rizzo  *    transmit or receive queues (or all queues for a given interface).
5368b8534bSLuigi Rizzo  */
5468b8534bSLuigi Rizzo 
5568b8534bSLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */
5668b8534bSLuigi Rizzo __FBSDID("$FreeBSD$");
5768b8534bSLuigi Rizzo 
5868b8534bSLuigi Rizzo #include <sys/types.h>
5968b8534bSLuigi Rizzo #include <sys/module.h>
6068b8534bSLuigi Rizzo #include <sys/errno.h>
6168b8534bSLuigi Rizzo #include <sys/param.h>	/* defines used in kernel.h */
62506cc70cSLuigi Rizzo #include <sys/jail.h>
6368b8534bSLuigi Rizzo #include <sys/kernel.h>	/* types used in module initialization */
6468b8534bSLuigi Rizzo #include <sys/conf.h>	/* cdevsw struct */
6568b8534bSLuigi Rizzo #include <sys/uio.h>	/* uio struct */
6668b8534bSLuigi Rizzo #include <sys/sockio.h>
6768b8534bSLuigi Rizzo #include <sys/socketvar.h>	/* struct socket */
6868b8534bSLuigi Rizzo #include <sys/malloc.h>
6968b8534bSLuigi Rizzo #include <sys/mman.h>	/* PROT_EXEC */
7068b8534bSLuigi Rizzo #include <sys/poll.h>
71506cc70cSLuigi Rizzo #include <sys/proc.h>
7268b8534bSLuigi Rizzo #include <vm/vm.h>	/* vtophys */
7368b8534bSLuigi Rizzo #include <vm/pmap.h>	/* vtophys */
7468b8534bSLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
7568b8534bSLuigi Rizzo #include <machine/bus.h>
7668b8534bSLuigi Rizzo #include <sys/selinfo.h>
7768b8534bSLuigi Rizzo #include <sys/sysctl.h>
7868b8534bSLuigi Rizzo #include <net/if.h>
7968b8534bSLuigi Rizzo #include <net/bpf.h>		/* BIOCIMMEDIATE */
80506cc70cSLuigi Rizzo #include <net/vnet.h>
8168b8534bSLuigi Rizzo #include <net/netmap.h>
8268b8534bSLuigi Rizzo #include <dev/netmap/netmap_kern.h>
8368b8534bSLuigi Rizzo #include <machine/bus.h>	/* bus_dmamap_* */
8468b8534bSLuigi Rizzo 
8568b8534bSLuigi Rizzo MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map");
8668b8534bSLuigi Rizzo 
8768b8534bSLuigi Rizzo /*
8868b8534bSLuigi Rizzo  * lock and unlock for the netmap memory allocator
8968b8534bSLuigi Rizzo  */
9064ae02c3SLuigi Rizzo #define NMA_LOCK()	mtx_lock(&nm_mem->nm_mtx);
9164ae02c3SLuigi Rizzo #define NMA_UNLOCK()	mtx_unlock(&nm_mem->nm_mtx);
925819da83SLuigi Rizzo struct netmap_mem_d;
9364ae02c3SLuigi Rizzo static struct netmap_mem_d *nm_mem;	/* Our memory allocator. */
945819da83SLuigi Rizzo 
955819da83SLuigi Rizzo u_int netmap_total_buffers;
965819da83SLuigi Rizzo char *netmap_buffer_base;	/* address of an invalid buffer */
975819da83SLuigi Rizzo 
985819da83SLuigi Rizzo /* user-controlled variables */
995819da83SLuigi Rizzo int netmap_verbose;
1005819da83SLuigi Rizzo 
1015819da83SLuigi Rizzo static int netmap_no_timestamp; /* don't timestamp on rxsync */
1025819da83SLuigi Rizzo 
1035819da83SLuigi Rizzo SYSCTL_NODE(_dev, OID_AUTO, netmap, CTLFLAG_RW, 0, "Netmap args");
1045819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, verbose,
1055819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_verbose, 0, "Verbose mode");
1065819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_timestamp,
1075819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_timestamp, 0, "no_timestamp");
1085819da83SLuigi Rizzo int netmap_buf_size = 2048;
1095819da83SLuigi Rizzo TUNABLE_INT("hw.netmap.buf_size", &netmap_buf_size);
1105819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, buf_size,
1115819da83SLuigi Rizzo     CTLFLAG_RD, &netmap_buf_size, 0, "Size of packet buffers");
1125819da83SLuigi Rizzo int netmap_mitigate = 1;
1135819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mitigate, CTLFLAG_RW, &netmap_mitigate, 0, "");
114*c85cb1a0SLuigi Rizzo int netmap_no_pendintr = 1;
1155819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_pendintr,
1165819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_pendintr, 0, "Always look for new received packets.");
1175819da83SLuigi Rizzo 
1185819da83SLuigi Rizzo 
1195819da83SLuigi Rizzo 
1205819da83SLuigi Rizzo /*----- memory allocator -----------------*/
1215819da83SLuigi Rizzo /*
1225819da83SLuigi Rizzo  * Here we have the low level routines for memory allocator
1235819da83SLuigi Rizzo  * and its primary users.
1245819da83SLuigi Rizzo  */
12568b8534bSLuigi Rizzo 
12668b8534bSLuigi Rizzo /*
12768b8534bSLuigi Rizzo  * Default amount of memory pre-allocated by the module.
12868b8534bSLuigi Rizzo  * We start with a large size and then shrink our demand
12968b8534bSLuigi Rizzo  * according to what is avalable when the module is loaded.
13068b8534bSLuigi Rizzo  * At the moment the block is contiguous, but we can easily
13168b8534bSLuigi Rizzo  * restrict our demand to smaller units (16..64k)
13268b8534bSLuigi Rizzo  */
13368b8534bSLuigi Rizzo #define NETMAP_MEMORY_SIZE (64 * 1024 * PAGE_SIZE)
13468b8534bSLuigi Rizzo static void * netmap_malloc(size_t size, const char *msg);
13568b8534bSLuigi Rizzo static void netmap_free(void *addr, const char *msg);
13668b8534bSLuigi Rizzo 
1376e10c8b8SLuigi Rizzo #define netmap_if_malloc(len)   netmap_malloc(len, "nifp")
1386e10c8b8SLuigi Rizzo #define netmap_if_free(v)	netmap_free((v), "nifp")
1396e10c8b8SLuigi Rizzo 
1406e10c8b8SLuigi Rizzo #define netmap_ring_malloc(len) netmap_malloc(len, "ring")
1416e10c8b8SLuigi Rizzo #define netmap_free_rings(na)		\
1426e10c8b8SLuigi Rizzo 	netmap_free((na)->tx_rings[0].ring, "shadow rings");
1436e10c8b8SLuigi Rizzo 
14468b8534bSLuigi Rizzo /*
14568b8534bSLuigi Rizzo  * Allocator for a pool of packet buffers. For each buffer we have
14668b8534bSLuigi Rizzo  * one entry in the bitmap to signal the state. Allocation scans
14768b8534bSLuigi Rizzo  * the bitmap, but since this is done only on attach, we are not
14868b8534bSLuigi Rizzo  * too worried about performance
14968b8534bSLuigi Rizzo  * XXX if we need to allocate small blocks, a translation
15068b8534bSLuigi Rizzo  * table is used both for kernel virtual address and physical
15168b8534bSLuigi Rizzo  * addresses.
15268b8534bSLuigi Rizzo  */
15368b8534bSLuigi Rizzo struct netmap_buf_pool {
15468b8534bSLuigi Rizzo 	u_int total_buffers;	/* total buffers. */
15568b8534bSLuigi Rizzo 	u_int free;
15668b8534bSLuigi Rizzo 	u_int bufsize;
15768b8534bSLuigi Rizzo 	char *base;		/* buffer base address */
15868b8534bSLuigi Rizzo 	uint32_t *bitmap;	/* one bit per buffer, 1 means free */
15968b8534bSLuigi Rizzo };
16068b8534bSLuigi Rizzo struct netmap_buf_pool nm_buf_pool;
16168b8534bSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, total_buffers,
16268b8534bSLuigi Rizzo     CTLFLAG_RD, &nm_buf_pool.total_buffers, 0, "total_buffers");
16368b8534bSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, free_buffers,
16468b8534bSLuigi Rizzo     CTLFLAG_RD, &nm_buf_pool.free, 0, "free_buffers");
1655819da83SLuigi Rizzo 
1665819da83SLuigi Rizzo 
1675819da83SLuigi Rizzo 
16868b8534bSLuigi Rizzo 
16968b8534bSLuigi Rizzo /*
17068b8534bSLuigi Rizzo  * Allocate n buffers from the ring, and fill the slot.
17168b8534bSLuigi Rizzo  * Buffer 0 is the 'junk' buffer.
17268b8534bSLuigi Rizzo  */
17368b8534bSLuigi Rizzo static void
174446ee301SLuigi Rizzo netmap_new_bufs(struct netmap_if *nifp __unused,
175446ee301SLuigi Rizzo 		struct netmap_slot *slot, u_int n)
17668b8534bSLuigi Rizzo {
177446ee301SLuigi Rizzo 	struct netmap_buf_pool *p = &nm_buf_pool;
17868b8534bSLuigi Rizzo 	uint32_t bi = 0;		/* index in the bitmap */
17968b8534bSLuigi Rizzo 	uint32_t mask, j, i = 0;	/* slot counter */
18068b8534bSLuigi Rizzo 
18168b8534bSLuigi Rizzo 	if (n > p->free) {
18268b8534bSLuigi Rizzo 		D("only %d out of %d buffers available", i, n);
18368b8534bSLuigi Rizzo 		return;
18468b8534bSLuigi Rizzo 	}
18568b8534bSLuigi Rizzo 	/* termination is guaranteed by p->free */
18668b8534bSLuigi Rizzo 	while (i < n && p->free > 0) {
18768b8534bSLuigi Rizzo 		uint32_t cur = p->bitmap[bi];
18868b8534bSLuigi Rizzo 		if (cur == 0) { /* bitmask is fully used */
18968b8534bSLuigi Rizzo 			bi++;
19068b8534bSLuigi Rizzo 			continue;
19168b8534bSLuigi Rizzo 		}
19268b8534bSLuigi Rizzo 		/* locate a slot */
19368b8534bSLuigi Rizzo 		for (j = 0, mask = 1; (cur & mask) == 0; j++, mask <<= 1) ;
19468b8534bSLuigi Rizzo 		p->bitmap[bi] &= ~mask;		/* slot in use */
19568b8534bSLuigi Rizzo 		p->free--;
19668b8534bSLuigi Rizzo 		slot[i].buf_idx = bi*32+j;
19768b8534bSLuigi Rizzo 		slot[i].len = p->bufsize;
19868b8534bSLuigi Rizzo 		slot[i].flags = NS_BUF_CHANGED;
19968b8534bSLuigi Rizzo 		i++;
20068b8534bSLuigi Rizzo 	}
20168b8534bSLuigi Rizzo 	ND("allocated %d buffers, %d available", n, p->free);
20268b8534bSLuigi Rizzo }
20368b8534bSLuigi Rizzo 
20468b8534bSLuigi Rizzo 
20568b8534bSLuigi Rizzo static void
206446ee301SLuigi Rizzo netmap_free_buf(struct netmap_if *nifp __unused, uint32_t i)
20768b8534bSLuigi Rizzo {
208446ee301SLuigi Rizzo 	struct netmap_buf_pool *p = &nm_buf_pool;
209446ee301SLuigi Rizzo 
21068b8534bSLuigi Rizzo 	uint32_t pos, mask;
21168b8534bSLuigi Rizzo 	if (i >= p->total_buffers) {
21268b8534bSLuigi Rizzo 		D("invalid free index %d", i);
21368b8534bSLuigi Rizzo 		return;
21468b8534bSLuigi Rizzo 	}
21568b8534bSLuigi Rizzo 	pos = i / 32;
21668b8534bSLuigi Rizzo 	mask = 1 << (i % 32);
21768b8534bSLuigi Rizzo 	if (p->bitmap[pos] & mask) {
21868b8534bSLuigi Rizzo 		D("slot %d already free", i);
21968b8534bSLuigi Rizzo 		return;
22068b8534bSLuigi Rizzo 	}
22168b8534bSLuigi Rizzo 	p->bitmap[pos] |= mask;
22268b8534bSLuigi Rizzo 	p->free++;
22368b8534bSLuigi Rizzo }
22468b8534bSLuigi Rizzo 
22568b8534bSLuigi Rizzo 
22668b8534bSLuigi Rizzo /* Descriptor of the memory objects handled by our memory allocator. */
22768b8534bSLuigi Rizzo struct netmap_mem_obj {
22868b8534bSLuigi Rizzo 	TAILQ_ENTRY(netmap_mem_obj) nmo_next; /* next object in the
22968b8534bSLuigi Rizzo 						 chain. */
23068b8534bSLuigi Rizzo 	int nmo_used; /* flag set on used memory objects. */
23168b8534bSLuigi Rizzo 	size_t nmo_size; /* size of the memory area reserved for the
23268b8534bSLuigi Rizzo 			    object. */
23368b8534bSLuigi Rizzo 	void *nmo_data; /* pointer to the memory area. */
23468b8534bSLuigi Rizzo };
23568b8534bSLuigi Rizzo 
23668b8534bSLuigi Rizzo /* Wrap our memory objects to make them ``chainable``. */
23768b8534bSLuigi Rizzo TAILQ_HEAD(netmap_mem_obj_h, netmap_mem_obj);
23868b8534bSLuigi Rizzo 
23968b8534bSLuigi Rizzo 
24068b8534bSLuigi Rizzo /* Descriptor of our custom memory allocator. */
24168b8534bSLuigi Rizzo struct netmap_mem_d {
24268b8534bSLuigi Rizzo 	struct mtx nm_mtx; /* lock used to handle the chain of memory
24368b8534bSLuigi Rizzo 			      objects. */
24468b8534bSLuigi Rizzo 	struct netmap_mem_obj_h nm_molist; /* list of memory objects */
24568b8534bSLuigi Rizzo 	size_t nm_size; /* total amount of memory used for rings etc. */
24668b8534bSLuigi Rizzo 	size_t nm_totalsize; /* total amount of allocated memory
24768b8534bSLuigi Rizzo 		(the difference is used for buffers) */
24868b8534bSLuigi Rizzo 	size_t nm_buf_start; /* offset of packet buffers.
24968b8534bSLuigi Rizzo 			This is page-aligned. */
25068b8534bSLuigi Rizzo 	size_t nm_buf_len; /* total memory for buffers */
25168b8534bSLuigi Rizzo 	void *nm_buffer; /* pointer to the whole pre-allocated memory
25268b8534bSLuigi Rizzo 			    area. */
25368b8534bSLuigi Rizzo };
25468b8534bSLuigi Rizzo 
2555819da83SLuigi Rizzo /* Shorthand to compute a netmap interface offset. */
2565819da83SLuigi Rizzo #define netmap_if_offset(v)                                     \
25764ae02c3SLuigi Rizzo     ((char *) (v) - (char *) nm_mem->nm_buffer)
2585819da83SLuigi Rizzo /* .. and get a physical address given a memory offset */
2595819da83SLuigi Rizzo #define netmap_ofstophys(o)                                     \
26064ae02c3SLuigi Rizzo     (vtophys(nm_mem->nm_buffer) + (o))
2615819da83SLuigi Rizzo 
2625819da83SLuigi Rizzo 
2635819da83SLuigi Rizzo /*------ netmap memory allocator -------*/
2645819da83SLuigi Rizzo /*
2655819da83SLuigi Rizzo  * Request for a chunk of memory.
2665819da83SLuigi Rizzo  *
2675819da83SLuigi Rizzo  * Memory objects are arranged into a list, hence we need to walk this
2685819da83SLuigi Rizzo  * list until we find an object with the needed amount of data free.
2695819da83SLuigi Rizzo  * This sounds like a completely inefficient implementation, but given
2705819da83SLuigi Rizzo  * the fact that data allocation is done once, we can handle it
2715819da83SLuigi Rizzo  * flawlessly.
2725819da83SLuigi Rizzo  *
2735819da83SLuigi Rizzo  * Return NULL on failure.
2745819da83SLuigi Rizzo  */
2755819da83SLuigi Rizzo static void *
2765819da83SLuigi Rizzo netmap_malloc(size_t size, __unused const char *msg)
2775819da83SLuigi Rizzo {
2785819da83SLuigi Rizzo 	struct netmap_mem_obj *mem_obj, *new_mem_obj;
2795819da83SLuigi Rizzo 	void *ret = NULL;
2805819da83SLuigi Rizzo 
2815819da83SLuigi Rizzo 	NMA_LOCK();
28264ae02c3SLuigi Rizzo 	TAILQ_FOREACH(mem_obj, &nm_mem->nm_molist, nmo_next) {
2835819da83SLuigi Rizzo 		if (mem_obj->nmo_used != 0 || mem_obj->nmo_size < size)
2845819da83SLuigi Rizzo 			continue;
2855819da83SLuigi Rizzo 
2865819da83SLuigi Rizzo 		new_mem_obj = malloc(sizeof(struct netmap_mem_obj), M_NETMAP,
2875819da83SLuigi Rizzo 				     M_WAITOK | M_ZERO);
2885819da83SLuigi Rizzo 		TAILQ_INSERT_BEFORE(mem_obj, new_mem_obj, nmo_next);
2895819da83SLuigi Rizzo 
2905819da83SLuigi Rizzo 		new_mem_obj->nmo_used = 1;
2915819da83SLuigi Rizzo 		new_mem_obj->nmo_size = size;
2925819da83SLuigi Rizzo 		new_mem_obj->nmo_data = mem_obj->nmo_data;
2935819da83SLuigi Rizzo 		memset(new_mem_obj->nmo_data, 0, new_mem_obj->nmo_size);
2945819da83SLuigi Rizzo 
2955819da83SLuigi Rizzo 		mem_obj->nmo_size -= size;
2965819da83SLuigi Rizzo 		mem_obj->nmo_data = (char *) mem_obj->nmo_data + size;
2975819da83SLuigi Rizzo 		if (mem_obj->nmo_size == 0) {
29864ae02c3SLuigi Rizzo 			TAILQ_REMOVE(&nm_mem->nm_molist, mem_obj,
2995819da83SLuigi Rizzo 				     nmo_next);
3005819da83SLuigi Rizzo 			free(mem_obj, M_NETMAP);
3015819da83SLuigi Rizzo 		}
3025819da83SLuigi Rizzo 
3035819da83SLuigi Rizzo 		ret = new_mem_obj->nmo_data;
3045819da83SLuigi Rizzo 
3055819da83SLuigi Rizzo 		break;
3065819da83SLuigi Rizzo 	}
3075819da83SLuigi Rizzo 	NMA_UNLOCK();
3085819da83SLuigi Rizzo 	ND("%s: %d bytes at %p", msg, size, ret);
3095819da83SLuigi Rizzo 
3105819da83SLuigi Rizzo 	return (ret);
3115819da83SLuigi Rizzo }
3125819da83SLuigi Rizzo 
3135819da83SLuigi Rizzo /*
3145819da83SLuigi Rizzo  * Return the memory to the allocator.
3155819da83SLuigi Rizzo  *
3165819da83SLuigi Rizzo  * While freeing a memory object, we try to merge adjacent chunks in
3175819da83SLuigi Rizzo  * order to reduce memory fragmentation.
3185819da83SLuigi Rizzo  */
3195819da83SLuigi Rizzo static void
3205819da83SLuigi Rizzo netmap_free(void *addr, const char *msg)
3215819da83SLuigi Rizzo {
3225819da83SLuigi Rizzo 	size_t size;
3235819da83SLuigi Rizzo 	struct netmap_mem_obj *cur, *prev, *next;
3245819da83SLuigi Rizzo 
3255819da83SLuigi Rizzo 	if (addr == NULL) {
3265819da83SLuigi Rizzo 		D("NULL addr for %s", msg);
3275819da83SLuigi Rizzo 		return;
3285819da83SLuigi Rizzo 	}
3295819da83SLuigi Rizzo 
3305819da83SLuigi Rizzo 	NMA_LOCK();
33164ae02c3SLuigi Rizzo 	TAILQ_FOREACH(cur, &nm_mem->nm_molist, nmo_next) {
3325819da83SLuigi Rizzo 		if (cur->nmo_data == addr && cur->nmo_used)
3335819da83SLuigi Rizzo 			break;
3345819da83SLuigi Rizzo 	}
3355819da83SLuigi Rizzo 	if (cur == NULL) {
3365819da83SLuigi Rizzo 		NMA_UNLOCK();
3375819da83SLuigi Rizzo 		D("invalid addr %s %p", msg, addr);
3385819da83SLuigi Rizzo 		return;
3395819da83SLuigi Rizzo 	}
3405819da83SLuigi Rizzo 
3415819da83SLuigi Rizzo 	size = cur->nmo_size;
3425819da83SLuigi Rizzo 	cur->nmo_used = 0;
3435819da83SLuigi Rizzo 
3445819da83SLuigi Rizzo 	/* merge current chunk of memory with the previous one,
3455819da83SLuigi Rizzo 	   if present. */
3465819da83SLuigi Rizzo 	prev = TAILQ_PREV(cur, netmap_mem_obj_h, nmo_next);
3475819da83SLuigi Rizzo 	if (prev && prev->nmo_used == 0) {
34864ae02c3SLuigi Rizzo 		TAILQ_REMOVE(&nm_mem->nm_molist, cur, nmo_next);
3495819da83SLuigi Rizzo 		prev->nmo_size += cur->nmo_size;
3505819da83SLuigi Rizzo 		free(cur, M_NETMAP);
3515819da83SLuigi Rizzo 		cur = prev;
3525819da83SLuigi Rizzo 	}
3535819da83SLuigi Rizzo 
3545819da83SLuigi Rizzo 	/* merge with the next one */
3555819da83SLuigi Rizzo 	next = TAILQ_NEXT(cur, nmo_next);
3565819da83SLuigi Rizzo 	if (next && next->nmo_used == 0) {
35764ae02c3SLuigi Rizzo 		TAILQ_REMOVE(&nm_mem->nm_molist, next, nmo_next);
3585819da83SLuigi Rizzo 		cur->nmo_size += next->nmo_size;
3595819da83SLuigi Rizzo 		free(next, M_NETMAP);
3605819da83SLuigi Rizzo 	}
3615819da83SLuigi Rizzo 	NMA_UNLOCK();
3625819da83SLuigi Rizzo 	ND("freed %s %d bytes at %p", msg, size, addr);
3635819da83SLuigi Rizzo }
3645819da83SLuigi Rizzo 
3655819da83SLuigi Rizzo 
3665819da83SLuigi Rizzo /*
3675819da83SLuigi Rizzo  * Create and return a new ``netmap_if`` object, and possibly also
3685819da83SLuigi Rizzo  * rings and packet buffors.
3695819da83SLuigi Rizzo  *
3705819da83SLuigi Rizzo  * Return NULL on failure.
3715819da83SLuigi Rizzo  */
3725819da83SLuigi Rizzo static void *
3735819da83SLuigi Rizzo netmap_if_new(const char *ifname, struct netmap_adapter *na)
3745819da83SLuigi Rizzo {
3755819da83SLuigi Rizzo 	struct netmap_if *nifp;
3765819da83SLuigi Rizzo 	struct netmap_ring *ring;
37764ae02c3SLuigi Rizzo 	struct netmap_kring *kring;
3785819da83SLuigi Rizzo 	char *buff;
37964ae02c3SLuigi Rizzo 	u_int i, len, ofs, numdesc;
38064ae02c3SLuigi Rizzo 	u_int nrx = na->num_rx_queues + 1; /* shorthand, include stack queue */
38164ae02c3SLuigi Rizzo 	u_int ntx = na->num_tx_queues + 1; /* shorthand, include stack queue */
3825819da83SLuigi Rizzo 
3835819da83SLuigi Rizzo 	/*
3845819da83SLuigi Rizzo 	 * the descriptor is followed inline by an array of offsets
3855819da83SLuigi Rizzo 	 * to the tx and rx rings in the shared memory region.
3865819da83SLuigi Rizzo 	 */
38764ae02c3SLuigi Rizzo 	len = sizeof(struct netmap_if) + (nrx + ntx) * sizeof(ssize_t);
3885819da83SLuigi Rizzo 	nifp = netmap_if_malloc(len);
3895819da83SLuigi Rizzo 	if (nifp == NULL)
3905819da83SLuigi Rizzo 		return (NULL);
3915819da83SLuigi Rizzo 
3925819da83SLuigi Rizzo 	/* initialize base fields */
39364ae02c3SLuigi Rizzo 	*(int *)(uintptr_t)&nifp->ni_rx_queues = na->num_rx_queues;
39464ae02c3SLuigi Rizzo 	*(int *)(uintptr_t)&nifp->ni_tx_queues = na->num_tx_queues;
3955819da83SLuigi Rizzo 	strncpy(nifp->ni_name, ifname, IFNAMSIZ);
3965819da83SLuigi Rizzo 
3975819da83SLuigi Rizzo 	(na->refcount)++;	/* XXX atomic ? we are under lock */
3985819da83SLuigi Rizzo 	if (na->refcount > 1)
3995819da83SLuigi Rizzo 		goto final;
4005819da83SLuigi Rizzo 
4015819da83SLuigi Rizzo 	/*
40264ae02c3SLuigi Rizzo 	 * First instance. Allocate the netmap rings
40364ae02c3SLuigi Rizzo 	 * (one for each hw queue, one pair for the host).
4045819da83SLuigi Rizzo 	 * The rings are contiguous, but have variable size.
4055819da83SLuigi Rizzo 	 * The entire block is reachable at
40664ae02c3SLuigi Rizzo 	 *	na->tx_rings[0]
4075819da83SLuigi Rizzo 	 */
40864ae02c3SLuigi Rizzo 	len = (ntx + nrx) * sizeof(struct netmap_ring) +
40964ae02c3SLuigi Rizzo 	      (ntx * na->num_tx_desc + nrx * na->num_rx_desc) *
41064ae02c3SLuigi Rizzo 		   sizeof(struct netmap_slot);
4115819da83SLuigi Rizzo 	buff = netmap_ring_malloc(len);
4125819da83SLuigi Rizzo 	if (buff == NULL) {
4135819da83SLuigi Rizzo 		D("failed to allocate %d bytes for %s shadow ring",
4145819da83SLuigi Rizzo 			len, ifname);
4155819da83SLuigi Rizzo error:
4165819da83SLuigi Rizzo 		(na->refcount)--;
4175819da83SLuigi Rizzo 		netmap_if_free(nifp);
4185819da83SLuigi Rizzo 		return (NULL);
4195819da83SLuigi Rizzo 	}
42064ae02c3SLuigi Rizzo 	/* Check whether we have enough buffers */
42164ae02c3SLuigi Rizzo 	len = ntx * na->num_tx_desc + nrx * na->num_rx_desc;
4225819da83SLuigi Rizzo 	NMA_LOCK();
4235819da83SLuigi Rizzo 	if (nm_buf_pool.free < len) {
4245819da83SLuigi Rizzo 		NMA_UNLOCK();
4255819da83SLuigi Rizzo 		netmap_free(buff, "not enough bufs");
4265819da83SLuigi Rizzo 		goto error;
4275819da83SLuigi Rizzo 	}
4285819da83SLuigi Rizzo 	/*
4295819da83SLuigi Rizzo 	 * in the kring, store the pointers to the shared rings
4305819da83SLuigi Rizzo 	 * and initialize the rings. We are under NMA_LOCK().
4315819da83SLuigi Rizzo 	 */
4325819da83SLuigi Rizzo 	ofs = 0;
43364ae02c3SLuigi Rizzo 	for (i = 0; i < ntx; i++) { /* Transmit rings */
4345819da83SLuigi Rizzo 		kring = &na->tx_rings[i];
4355819da83SLuigi Rizzo 		numdesc = na->num_tx_desc;
4365819da83SLuigi Rizzo 		bzero(kring, sizeof(*kring));
4375819da83SLuigi Rizzo 		kring->na = na;
4385819da83SLuigi Rizzo 
4395819da83SLuigi Rizzo 		ring = kring->ring = (struct netmap_ring *)(buff + ofs);
4405819da83SLuigi Rizzo 		*(ssize_t *)(uintptr_t)&ring->buf_ofs =
4415819da83SLuigi Rizzo 			nm_buf_pool.base - (char *)ring;
4425819da83SLuigi Rizzo 		ND("txring[%d] at %p ofs %d", i, ring, ring->buf_ofs);
4435819da83SLuigi Rizzo 		*(uint32_t *)(uintptr_t)&ring->num_slots =
4445819da83SLuigi Rizzo 			kring->nkr_num_slots = numdesc;
4455819da83SLuigi Rizzo 
4465819da83SLuigi Rizzo 		/*
4475819da83SLuigi Rizzo 		 * IMPORTANT:
4485819da83SLuigi Rizzo 		 * Always keep one slot empty, so we can detect new
4495819da83SLuigi Rizzo 		 * transmissions comparing cur and nr_hwcur (they are
4505819da83SLuigi Rizzo 		 * the same only if there are no new transmissions).
4515819da83SLuigi Rizzo 		 */
4525819da83SLuigi Rizzo 		ring->avail = kring->nr_hwavail = numdesc - 1;
4535819da83SLuigi Rizzo 		ring->cur = kring->nr_hwcur = 0;
4545819da83SLuigi Rizzo 		*(uint16_t *)(uintptr_t)&ring->nr_buf_size = NETMAP_BUF_SIZE;
4555819da83SLuigi Rizzo 		netmap_new_bufs(nifp, ring->slot, numdesc);
4565819da83SLuigi Rizzo 
4575819da83SLuigi Rizzo 		ofs += sizeof(struct netmap_ring) +
4585819da83SLuigi Rizzo 			numdesc * sizeof(struct netmap_slot);
45964ae02c3SLuigi Rizzo 	}
4605819da83SLuigi Rizzo 
46164ae02c3SLuigi Rizzo 	for (i = 0; i < nrx; i++) { /* Receive rings */
4625819da83SLuigi Rizzo 		kring = &na->rx_rings[i];
4635819da83SLuigi Rizzo 		numdesc = na->num_rx_desc;
4645819da83SLuigi Rizzo 		bzero(kring, sizeof(*kring));
4655819da83SLuigi Rizzo 		kring->na = na;
4665819da83SLuigi Rizzo 
4675819da83SLuigi Rizzo 		ring = kring->ring = (struct netmap_ring *)(buff + ofs);
4685819da83SLuigi Rizzo 		*(ssize_t *)(uintptr_t)&ring->buf_ofs =
4695819da83SLuigi Rizzo 			nm_buf_pool.base - (char *)ring;
4705819da83SLuigi Rizzo 		ND("rxring[%d] at %p offset %d", i, ring, ring->buf_ofs);
4715819da83SLuigi Rizzo 		*(uint32_t *)(uintptr_t)&ring->num_slots =
4725819da83SLuigi Rizzo 			kring->nkr_num_slots = numdesc;
4735819da83SLuigi Rizzo 		ring->cur = kring->nr_hwcur = 0;
4745819da83SLuigi Rizzo 		ring->avail = kring->nr_hwavail = 0; /* empty */
4755819da83SLuigi Rizzo 		*(uint16_t *)(uintptr_t)&ring->nr_buf_size = NETMAP_BUF_SIZE;
4765819da83SLuigi Rizzo 		netmap_new_bufs(nifp, ring->slot, numdesc);
4775819da83SLuigi Rizzo 		ofs += sizeof(struct netmap_ring) +
4785819da83SLuigi Rizzo 			numdesc * sizeof(struct netmap_slot);
4795819da83SLuigi Rizzo 	}
4805819da83SLuigi Rizzo 	NMA_UNLOCK();
4815819da83SLuigi Rizzo 	// XXX initialize the selrecord structs.
48264ae02c3SLuigi Rizzo 
4835819da83SLuigi Rizzo final:
4845819da83SLuigi Rizzo 	/*
4855819da83SLuigi Rizzo 	 * fill the slots for the rx and tx queues. They contain the offset
4865819da83SLuigi Rizzo 	 * between the ring and nifp, so the information is usable in
4875819da83SLuigi Rizzo 	 * userspace to reach the ring from the nifp.
4885819da83SLuigi Rizzo 	 */
48964ae02c3SLuigi Rizzo 	for (i = 0; i < ntx; i++) {
4905819da83SLuigi Rizzo 		*(ssize_t *)(uintptr_t)&nifp->ring_ofs[i] =
49164ae02c3SLuigi Rizzo 			(char *)na->tx_rings[i].ring - (char *)nifp;
49264ae02c3SLuigi Rizzo 	}
49364ae02c3SLuigi Rizzo 	for (i = 0; i < nrx; i++) {
49464ae02c3SLuigi Rizzo 		*(ssize_t *)(uintptr_t)&nifp->ring_ofs[i+ntx] =
49564ae02c3SLuigi Rizzo 			(char *)na->rx_rings[i].ring - (char *)nifp;
4965819da83SLuigi Rizzo 	}
4975819da83SLuigi Rizzo 	return (nifp);
4985819da83SLuigi Rizzo }
4995819da83SLuigi Rizzo 
5005819da83SLuigi Rizzo /*
5015819da83SLuigi Rizzo  * Initialize the memory allocator.
5025819da83SLuigi Rizzo  *
5035819da83SLuigi Rizzo  * Create the descriptor for the memory , allocate the pool of memory
5045819da83SLuigi Rizzo  * and initialize the list of memory objects with a single chunk
5055819da83SLuigi Rizzo  * containing the whole pre-allocated memory marked as free.
5065819da83SLuigi Rizzo  *
5075819da83SLuigi Rizzo  * Start with a large size, then halve as needed if we fail to
5085819da83SLuigi Rizzo  * allocate the block. While halving, always add one extra page
5095819da83SLuigi Rizzo  * because buffers 0 and 1 are used for special purposes.
5105819da83SLuigi Rizzo  * Return 0 on success, errno otherwise.
5115819da83SLuigi Rizzo  */
5125819da83SLuigi Rizzo static int
5135819da83SLuigi Rizzo netmap_memory_init(void)
5145819da83SLuigi Rizzo {
5155819da83SLuigi Rizzo 	struct netmap_mem_obj *mem_obj;
5165819da83SLuigi Rizzo 	void *buf = NULL;
5175819da83SLuigi Rizzo 	int i, n, sz = NETMAP_MEMORY_SIZE;
5185819da83SLuigi Rizzo 	int extra_sz = 0; // space for rings and two spare buffers
5195819da83SLuigi Rizzo 
5205819da83SLuigi Rizzo 	for (; sz >= 1<<20; sz >>=1) {
5215819da83SLuigi Rizzo 		extra_sz = sz/200;
5225819da83SLuigi Rizzo 		extra_sz = (extra_sz + 2*PAGE_SIZE - 1) & ~(PAGE_SIZE-1);
5235819da83SLuigi Rizzo 	        buf = contigmalloc(sz + extra_sz,
5245819da83SLuigi Rizzo 			     M_NETMAP,
5255819da83SLuigi Rizzo 			     M_WAITOK | M_ZERO,
5265819da83SLuigi Rizzo 			     0, /* low address */
5275819da83SLuigi Rizzo 			     -1UL, /* high address */
5285819da83SLuigi Rizzo 			     PAGE_SIZE, /* alignment */
5295819da83SLuigi Rizzo 			     0 /* boundary */
5305819da83SLuigi Rizzo 			    );
5315819da83SLuigi Rizzo 		if (buf)
5325819da83SLuigi Rizzo 			break;
5335819da83SLuigi Rizzo 	}
5345819da83SLuigi Rizzo 	if (buf == NULL)
5355819da83SLuigi Rizzo 		return (ENOMEM);
5365819da83SLuigi Rizzo 	sz += extra_sz;
53764ae02c3SLuigi Rizzo 	nm_mem = malloc(sizeof(struct netmap_mem_d), M_NETMAP,
5385819da83SLuigi Rizzo 			      M_WAITOK | M_ZERO);
53964ae02c3SLuigi Rizzo 	mtx_init(&nm_mem->nm_mtx, "netmap memory allocator lock", NULL,
5405819da83SLuigi Rizzo 		 MTX_DEF);
54164ae02c3SLuigi Rizzo 	TAILQ_INIT(&nm_mem->nm_molist);
54264ae02c3SLuigi Rizzo 	nm_mem->nm_buffer = buf;
54364ae02c3SLuigi Rizzo 	nm_mem->nm_totalsize = sz;
5445819da83SLuigi Rizzo 
5455819da83SLuigi Rizzo 	/*
5465819da83SLuigi Rizzo 	 * A buffer takes 2k, a slot takes 8 bytes + ring overhead,
5475819da83SLuigi Rizzo 	 * so the ratio is 200:1. In other words, we can use 1/200 of
5485819da83SLuigi Rizzo 	 * the memory for the rings, and the rest for the buffers,
5495819da83SLuigi Rizzo 	 * and be sure we never run out.
5505819da83SLuigi Rizzo 	 */
55164ae02c3SLuigi Rizzo 	nm_mem->nm_size = sz/200;
55264ae02c3SLuigi Rizzo 	nm_mem->nm_buf_start =
55364ae02c3SLuigi Rizzo 		(nm_mem->nm_size + PAGE_SIZE - 1) & ~(PAGE_SIZE-1);
55464ae02c3SLuigi Rizzo 	nm_mem->nm_buf_len = sz - nm_mem->nm_buf_start;
5555819da83SLuigi Rizzo 
55664ae02c3SLuigi Rizzo 	nm_buf_pool.base = nm_mem->nm_buffer;
55764ae02c3SLuigi Rizzo 	nm_buf_pool.base += nm_mem->nm_buf_start;
5585819da83SLuigi Rizzo 	netmap_buffer_base = nm_buf_pool.base;
5595819da83SLuigi Rizzo 	D("netmap_buffer_base %p (offset %d)",
56064ae02c3SLuigi Rizzo 		netmap_buffer_base, (int)nm_mem->nm_buf_start);
5615819da83SLuigi Rizzo 	/* number of buffers, they all start as free */
5625819da83SLuigi Rizzo 
5635819da83SLuigi Rizzo 	netmap_total_buffers = nm_buf_pool.total_buffers =
56464ae02c3SLuigi Rizzo 		nm_mem->nm_buf_len / NETMAP_BUF_SIZE;
5655819da83SLuigi Rizzo 	nm_buf_pool.bufsize = NETMAP_BUF_SIZE;
5665819da83SLuigi Rizzo 
5675819da83SLuigi Rizzo 	D("Have %d MB, use %dKB for rings, %d buffers at %p",
56864ae02c3SLuigi Rizzo 		(sz >> 20), (int)(nm_mem->nm_size >> 10),
5695819da83SLuigi Rizzo 		nm_buf_pool.total_buffers, nm_buf_pool.base);
5705819da83SLuigi Rizzo 
5715819da83SLuigi Rizzo 	/* allocate and initialize the bitmap. Entry 0 is considered
5725819da83SLuigi Rizzo 	 * always busy (used as default when there are no buffers left).
5735819da83SLuigi Rizzo 	 */
5745819da83SLuigi Rizzo 	n = (nm_buf_pool.total_buffers + 31) / 32;
5755819da83SLuigi Rizzo 	nm_buf_pool.bitmap = malloc(sizeof(uint32_t) * n, M_NETMAP,
5765819da83SLuigi Rizzo 			 M_WAITOK | M_ZERO);
5775819da83SLuigi Rizzo 	nm_buf_pool.bitmap[0] = ~3; /* slot 0 and 1 always busy */
5785819da83SLuigi Rizzo 	for (i = 1; i < n; i++)
5795819da83SLuigi Rizzo 		nm_buf_pool.bitmap[i] = ~0;
5805819da83SLuigi Rizzo 	nm_buf_pool.free = nm_buf_pool.total_buffers - 2;
5815819da83SLuigi Rizzo 
5825819da83SLuigi Rizzo 	mem_obj = malloc(sizeof(struct netmap_mem_obj), M_NETMAP,
5835819da83SLuigi Rizzo 			 M_WAITOK | M_ZERO);
58464ae02c3SLuigi Rizzo 	TAILQ_INSERT_HEAD(&nm_mem->nm_molist, mem_obj, nmo_next);
5855819da83SLuigi Rizzo 	mem_obj->nmo_used = 0;
58664ae02c3SLuigi Rizzo 	mem_obj->nmo_size = nm_mem->nm_size;
58764ae02c3SLuigi Rizzo 	mem_obj->nmo_data = nm_mem->nm_buffer;
5885819da83SLuigi Rizzo 
5895819da83SLuigi Rizzo 	return (0);
5905819da83SLuigi Rizzo }
5915819da83SLuigi Rizzo 
5925819da83SLuigi Rizzo 
5935819da83SLuigi Rizzo /*
5945819da83SLuigi Rizzo  * Finalize the memory allocator.
5955819da83SLuigi Rizzo  *
5965819da83SLuigi Rizzo  * Free all the memory objects contained inside the list, and deallocate
5975819da83SLuigi Rizzo  * the pool of memory; finally free the memory allocator descriptor.
5985819da83SLuigi Rizzo  */
5995819da83SLuigi Rizzo static void
6005819da83SLuigi Rizzo netmap_memory_fini(void)
6015819da83SLuigi Rizzo {
6025819da83SLuigi Rizzo 	struct netmap_mem_obj *mem_obj;
6035819da83SLuigi Rizzo 
60464ae02c3SLuigi Rizzo 	while (!TAILQ_EMPTY(&nm_mem->nm_molist)) {
60564ae02c3SLuigi Rizzo 		mem_obj = TAILQ_FIRST(&nm_mem->nm_molist);
60664ae02c3SLuigi Rizzo 		TAILQ_REMOVE(&nm_mem->nm_molist, mem_obj, nmo_next);
6075819da83SLuigi Rizzo 		if (mem_obj->nmo_used == 1) {
6085819da83SLuigi Rizzo 			printf("netmap: leaked %d bytes at %p\n",
6095819da83SLuigi Rizzo 			       (int)mem_obj->nmo_size,
6105819da83SLuigi Rizzo 			       mem_obj->nmo_data);
6115819da83SLuigi Rizzo 		}
6125819da83SLuigi Rizzo 		free(mem_obj, M_NETMAP);
6135819da83SLuigi Rizzo 	}
61464ae02c3SLuigi Rizzo 	contigfree(nm_mem->nm_buffer, nm_mem->nm_totalsize, M_NETMAP);
6155819da83SLuigi Rizzo 	// XXX mutex_destroy(nm_mtx);
61664ae02c3SLuigi Rizzo 	free(nm_mem, M_NETMAP);
6175819da83SLuigi Rizzo }
6185819da83SLuigi Rizzo /*------------- end of memory allocator -----------------*/
6195819da83SLuigi Rizzo 
62068b8534bSLuigi Rizzo 
62168b8534bSLuigi Rizzo /* Structure associated to each thread which registered an interface. */
62268b8534bSLuigi Rizzo struct netmap_priv_d {
62368b8534bSLuigi Rizzo 	struct netmap_if *np_nifp;	/* netmap interface descriptor. */
62468b8534bSLuigi Rizzo 
62568b8534bSLuigi Rizzo 	struct ifnet	*np_ifp;	/* device for which we hold a reference */
62668b8534bSLuigi Rizzo 	int		np_ringid;	/* from the ioctl */
62768b8534bSLuigi Rizzo 	u_int		np_qfirst, np_qlast;	/* range of rings to scan */
62868b8534bSLuigi Rizzo 	uint16_t	np_txpoll;
62968b8534bSLuigi Rizzo };
63068b8534bSLuigi Rizzo 
63168b8534bSLuigi Rizzo 
63268b8534bSLuigi Rizzo /*
63368b8534bSLuigi Rizzo  * File descriptor's private data destructor.
63468b8534bSLuigi Rizzo  *
63568b8534bSLuigi Rizzo  * Call nm_register(ifp,0) to stop netmap mode on the interface and
63668b8534bSLuigi Rizzo  * revert to normal operation. We expect that np_ifp has not gone.
63768b8534bSLuigi Rizzo  */
63868b8534bSLuigi Rizzo static void
6395819da83SLuigi Rizzo netmap_dtor_locked(void *data)
64068b8534bSLuigi Rizzo {
64168b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = data;
64268b8534bSLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
64368b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
64468b8534bSLuigi Rizzo 	struct netmap_if *nifp = priv->np_nifp;
64568b8534bSLuigi Rizzo 
64668b8534bSLuigi Rizzo 	na->refcount--;
64768b8534bSLuigi Rizzo 	if (na->refcount <= 0) {	/* last instance */
64864ae02c3SLuigi Rizzo 		u_int i, j, lim;
64968b8534bSLuigi Rizzo 
65068b8534bSLuigi Rizzo 		D("deleting last netmap instance for %s", ifp->if_xname);
65168b8534bSLuigi Rizzo 		/*
65268b8534bSLuigi Rizzo 		 * there is a race here with *_netmap_task() and
6531a26580eSLuigi Rizzo 		 * netmap_poll(), which don't run under NETMAP_REG_LOCK.
65468b8534bSLuigi Rizzo 		 * na->refcount == 0 && na->ifp->if_capenable & IFCAP_NETMAP
65568b8534bSLuigi Rizzo 		 * (aka NETMAP_DELETING(na)) are a unique marker that the
65668b8534bSLuigi Rizzo 		 * device is dying.
65768b8534bSLuigi Rizzo 		 * Before destroying stuff we sleep a bit, and then complete
65868b8534bSLuigi Rizzo 		 * the job. NIOCREG should realize the condition and
65968b8534bSLuigi Rizzo 		 * loop until they can continue; the other routines
66068b8534bSLuigi Rizzo 		 * should check the condition at entry and quit if
66168b8534bSLuigi Rizzo 		 * they cannot run.
66268b8534bSLuigi Rizzo 		 */
6631a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
66468b8534bSLuigi Rizzo 		tsleep(na, 0, "NIOCUNREG", 4);
6651a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_LOCK, 0);
66668b8534bSLuigi Rizzo 		na->nm_register(ifp, 0); /* off, clear IFCAP_NETMAP */
66768b8534bSLuigi Rizzo 		/* Wake up any sleeping threads. netmap_poll will
66868b8534bSLuigi Rizzo 		 * then return POLLERR
66968b8534bSLuigi Rizzo 		 */
67064ae02c3SLuigi Rizzo 		for (i = 0; i < na->num_tx_queues + 1; i++)
67168b8534bSLuigi Rizzo 			selwakeuppri(&na->tx_rings[i].si, PI_NET);
67264ae02c3SLuigi Rizzo 		for (i = 0; i < na->num_rx_queues + 1; i++)
67368b8534bSLuigi Rizzo 			selwakeuppri(&na->rx_rings[i].si, PI_NET);
67464ae02c3SLuigi Rizzo 		selwakeuppri(&na->tx_si, PI_NET);
67564ae02c3SLuigi Rizzo 		selwakeuppri(&na->rx_si, PI_NET);
67668b8534bSLuigi Rizzo 		/* release all buffers */
67768b8534bSLuigi Rizzo 		NMA_LOCK();
67864ae02c3SLuigi Rizzo 		for (i = 0; i < na->num_tx_queues + 1; i++) {
67964ae02c3SLuigi Rizzo 			struct netmap_ring *ring = na->tx_rings[i].ring;
68068b8534bSLuigi Rizzo 			lim = na->tx_rings[i].nkr_num_slots;
68168b8534bSLuigi Rizzo 			for (j = 0; j < lim; j++)
682446ee301SLuigi Rizzo 				netmap_free_buf(nifp, ring->slot[j].buf_idx);
68364ae02c3SLuigi Rizzo 		}
68464ae02c3SLuigi Rizzo 		for (i = 0; i < na->num_rx_queues + 1; i++) {
68564ae02c3SLuigi Rizzo 			struct netmap_ring *ring = na->rx_rings[i].ring;
68668b8534bSLuigi Rizzo 			lim = na->rx_rings[i].nkr_num_slots;
68768b8534bSLuigi Rizzo 			for (j = 0; j < lim; j++)
688446ee301SLuigi Rizzo 				netmap_free_buf(nifp, ring->slot[j].buf_idx);
68968b8534bSLuigi Rizzo 		}
69068b8534bSLuigi Rizzo 		NMA_UNLOCK();
6916e10c8b8SLuigi Rizzo 		netmap_free_rings(na);
69268b8534bSLuigi Rizzo 		wakeup(na);
69368b8534bSLuigi Rizzo 	}
6946e10c8b8SLuigi Rizzo 	netmap_if_free(nifp);
6955819da83SLuigi Rizzo }
69668b8534bSLuigi Rizzo 
6975819da83SLuigi Rizzo 
6985819da83SLuigi Rizzo static void
6995819da83SLuigi Rizzo netmap_dtor(void *data)
7005819da83SLuigi Rizzo {
7015819da83SLuigi Rizzo 	struct netmap_priv_d *priv = data;
7025819da83SLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
7035819da83SLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
7045819da83SLuigi Rizzo 
7051a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_REG_LOCK, 0);
7065819da83SLuigi Rizzo 	netmap_dtor_locked(data);
7071a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
70868b8534bSLuigi Rizzo 
70968b8534bSLuigi Rizzo 	if_rele(ifp);
71068b8534bSLuigi Rizzo 	bzero(priv, sizeof(*priv));	/* XXX for safety */
71168b8534bSLuigi Rizzo 	free(priv, M_DEVBUF);
71268b8534bSLuigi Rizzo }
71368b8534bSLuigi Rizzo 
71468b8534bSLuigi Rizzo 
71568b8534bSLuigi Rizzo /*
71668b8534bSLuigi Rizzo  * mmap(2) support for the "netmap" device.
71768b8534bSLuigi Rizzo  *
71868b8534bSLuigi Rizzo  * Expose all the memory previously allocated by our custom memory
71968b8534bSLuigi Rizzo  * allocator: this way the user has only to issue a single mmap(2), and
72068b8534bSLuigi Rizzo  * can work on all the data structures flawlessly.
72168b8534bSLuigi Rizzo  *
72268b8534bSLuigi Rizzo  * Return 0 on success, -1 otherwise.
72368b8534bSLuigi Rizzo  */
724babc7c12SLuigi Rizzo 
72568b8534bSLuigi Rizzo static int
726babc7c12SLuigi Rizzo netmap_mmap(__unused struct cdev *dev,
72768b8534bSLuigi Rizzo #if __FreeBSD_version < 900000
728babc7c12SLuigi Rizzo 		vm_offset_t offset, vm_paddr_t *paddr, int nprot
72968b8534bSLuigi Rizzo #else
730babc7c12SLuigi Rizzo 		vm_ooffset_t offset, vm_paddr_t *paddr, int nprot,
731babc7c12SLuigi Rizzo 		__unused vm_memattr_t *memattr
73268b8534bSLuigi Rizzo #endif
733babc7c12SLuigi Rizzo 	)
73468b8534bSLuigi Rizzo {
73568b8534bSLuigi Rizzo 	if (nprot & PROT_EXEC)
73668b8534bSLuigi Rizzo 		return (-1);	// XXX -1 or EINVAL ?
737446ee301SLuigi Rizzo 
73868b8534bSLuigi Rizzo 	ND("request for offset 0x%x", (uint32_t)offset);
739446ee301SLuigi Rizzo 	*paddr = netmap_ofstophys(offset);
74068b8534bSLuigi Rizzo 
74168b8534bSLuigi Rizzo 	return (0);
74268b8534bSLuigi Rizzo }
74368b8534bSLuigi Rizzo 
74468b8534bSLuigi Rizzo 
74568b8534bSLuigi Rizzo /*
74602ad4083SLuigi Rizzo  * Handlers for synchronization of the queues from/to the host.
74702ad4083SLuigi Rizzo  *
74802ad4083SLuigi Rizzo  * netmap_sync_to_host() passes packets up. We are called from a
74902ad4083SLuigi Rizzo  * system call in user process context, and the only contention
75002ad4083SLuigi Rizzo  * can be among multiple user threads erroneously calling
75102ad4083SLuigi Rizzo  * this routine concurrently. In principle we should not even
75202ad4083SLuigi Rizzo  * need to lock.
75368b8534bSLuigi Rizzo  */
75468b8534bSLuigi Rizzo static void
75568b8534bSLuigi Rizzo netmap_sync_to_host(struct netmap_adapter *na)
75668b8534bSLuigi Rizzo {
75764ae02c3SLuigi Rizzo 	struct netmap_kring *kring = &na->tx_rings[na->num_tx_queues];
75868b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
75968b8534bSLuigi Rizzo 	struct mbuf *head = NULL, *tail = NULL, *m;
76002ad4083SLuigi Rizzo 	u_int k, n, lim = kring->nkr_num_slots - 1;
76168b8534bSLuigi Rizzo 
76202ad4083SLuigi Rizzo 	k = ring->cur;
76302ad4083SLuigi Rizzo 	if (k > lim) {
76402ad4083SLuigi Rizzo 		netmap_ring_reinit(kring);
76502ad4083SLuigi Rizzo 		return;
76602ad4083SLuigi Rizzo 	}
7671a26580eSLuigi Rizzo 	// na->nm_lock(na->ifp, NETMAP_CORE_LOCK, 0);
76868b8534bSLuigi Rizzo 
76968b8534bSLuigi Rizzo 	/* Take packets from hwcur to cur and pass them up.
77068b8534bSLuigi Rizzo 	 * In case of no buffers we give up. At the end of the loop,
77168b8534bSLuigi Rizzo 	 * the queue is drained in all cases.
77268b8534bSLuigi Rizzo 	 */
77302ad4083SLuigi Rizzo 	for (n = kring->nr_hwcur; n != k;) {
77468b8534bSLuigi Rizzo 		struct netmap_slot *slot = &ring->slot[n];
77568b8534bSLuigi Rizzo 
77668b8534bSLuigi Rizzo 		n = (n == lim) ? 0 : n + 1;
77768b8534bSLuigi Rizzo 		if (slot->len < 14 || slot->len > NETMAP_BUF_SIZE) {
77868b8534bSLuigi Rizzo 			D("bad pkt at %d len %d", n, slot->len);
77968b8534bSLuigi Rizzo 			continue;
78068b8534bSLuigi Rizzo 		}
78168b8534bSLuigi Rizzo 		m = m_devget(NMB(slot), slot->len, 0, na->ifp, NULL);
78268b8534bSLuigi Rizzo 
78368b8534bSLuigi Rizzo 		if (m == NULL)
78468b8534bSLuigi Rizzo 			break;
78568b8534bSLuigi Rizzo 		if (tail)
78668b8534bSLuigi Rizzo 			tail->m_nextpkt = m;
78768b8534bSLuigi Rizzo 		else
78868b8534bSLuigi Rizzo 			head = m;
78968b8534bSLuigi Rizzo 		tail = m;
79068b8534bSLuigi Rizzo 		m->m_nextpkt = NULL;
79168b8534bSLuigi Rizzo 	}
79202ad4083SLuigi Rizzo 	kring->nr_hwcur = k;
79368b8534bSLuigi Rizzo 	kring->nr_hwavail = ring->avail = lim;
7941a26580eSLuigi Rizzo 	// na->nm_lock(na->ifp, NETMAP_CORE_UNLOCK, 0);
79568b8534bSLuigi Rizzo 
79668b8534bSLuigi Rizzo 	/* send packets up, outside the lock */
79768b8534bSLuigi Rizzo 	while ((m = head) != NULL) {
79868b8534bSLuigi Rizzo 		head = head->m_nextpkt;
79968b8534bSLuigi Rizzo 		m->m_nextpkt = NULL;
80068b8534bSLuigi Rizzo 		if (netmap_verbose & NM_VERB_HOST)
8011a26580eSLuigi Rizzo 			D("sending up pkt %p size %d", m, MBUF_LEN(m));
8021a26580eSLuigi Rizzo 		NM_SEND_UP(na->ifp, m);
80368b8534bSLuigi Rizzo 	}
80468b8534bSLuigi Rizzo }
80568b8534bSLuigi Rizzo 
80668b8534bSLuigi Rizzo /*
80702ad4083SLuigi Rizzo  * rxsync backend for packets coming from the host stack.
80802ad4083SLuigi Rizzo  * They have been put in the queue by netmap_start() so we
80902ad4083SLuigi Rizzo  * need to protect access to the kring using a lock.
81002ad4083SLuigi Rizzo  *
81168b8534bSLuigi Rizzo  * This routine also does the selrecord if called from the poll handler
81268b8534bSLuigi Rizzo  * (we know because td != NULL).
81368b8534bSLuigi Rizzo  */
81468b8534bSLuigi Rizzo static void
81568b8534bSLuigi Rizzo netmap_sync_from_host(struct netmap_adapter *na, struct thread *td)
81668b8534bSLuigi Rizzo {
81764ae02c3SLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_queues];
81868b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
81964ae02c3SLuigi Rizzo 	u_int j, n, lim = kring->nkr_num_slots;
82064ae02c3SLuigi Rizzo 	u_int k = ring->cur, resvd = ring->reserved;
82168b8534bSLuigi Rizzo 
8221a26580eSLuigi Rizzo 	na->nm_lock(na->ifp, NETMAP_CORE_LOCK, 0);
82364ae02c3SLuigi Rizzo 	if (k >= lim) {
82464ae02c3SLuigi Rizzo 		netmap_ring_reinit(kring);
82564ae02c3SLuigi Rizzo 		return;
82664ae02c3SLuigi Rizzo 	}
82764ae02c3SLuigi Rizzo 	/* new packets are already set in nr_hwavail */
82864ae02c3SLuigi Rizzo 	/* skip past packets that userspace has released */
82964ae02c3SLuigi Rizzo 	j = kring->nr_hwcur;
83064ae02c3SLuigi Rizzo 	if (resvd > 0) {
83164ae02c3SLuigi Rizzo 		if (resvd + ring->avail >= lim + 1) {
83264ae02c3SLuigi Rizzo 			D("XXX invalid reserve/avail %d %d", resvd, ring->avail);
83364ae02c3SLuigi Rizzo 			ring->reserved = resvd = 0; // XXX panic...
83464ae02c3SLuigi Rizzo 		}
83564ae02c3SLuigi Rizzo 		k = (k >= resvd) ? k - resvd : k + lim - resvd;
83664ae02c3SLuigi Rizzo         }
83764ae02c3SLuigi Rizzo 	if (j != k) {
83864ae02c3SLuigi Rizzo 		n = k >= j ? k - j : k + lim - j;
83964ae02c3SLuigi Rizzo 		kring->nr_hwavail -= n;
84002ad4083SLuigi Rizzo 	kring->nr_hwcur = k;
84164ae02c3SLuigi Rizzo 	}
84264ae02c3SLuigi Rizzo 	k = ring->avail = kring->nr_hwavail - resvd;
84302ad4083SLuigi Rizzo 	if (k == 0 && td)
84468b8534bSLuigi Rizzo 		selrecord(td, &kring->si);
84502ad4083SLuigi Rizzo 	if (k && (netmap_verbose & NM_VERB_HOST))
84602ad4083SLuigi Rizzo 		D("%d pkts from stack", k);
8471a26580eSLuigi Rizzo 	na->nm_lock(na->ifp, NETMAP_CORE_UNLOCK, 0);
84868b8534bSLuigi Rizzo }
84968b8534bSLuigi Rizzo 
85068b8534bSLuigi Rizzo 
85168b8534bSLuigi Rizzo /*
85268b8534bSLuigi Rizzo  * get a refcounted reference to an interface.
85368b8534bSLuigi Rizzo  * Return ENXIO if the interface does not exist, EINVAL if netmap
85468b8534bSLuigi Rizzo  * is not supported by the interface.
85568b8534bSLuigi Rizzo  * If successful, hold a reference.
85668b8534bSLuigi Rizzo  */
85768b8534bSLuigi Rizzo static int
85868b8534bSLuigi Rizzo get_ifp(const char *name, struct ifnet **ifp)
85968b8534bSLuigi Rizzo {
86068b8534bSLuigi Rizzo 	*ifp = ifunit_ref(name);
86168b8534bSLuigi Rizzo 	if (*ifp == NULL)
86268b8534bSLuigi Rizzo 		return (ENXIO);
86368b8534bSLuigi Rizzo 	/* can do this if the capability exists and if_pspare[0]
86468b8534bSLuigi Rizzo 	 * points to the netmap descriptor.
86568b8534bSLuigi Rizzo 	 */
86668b8534bSLuigi Rizzo 	if ((*ifp)->if_capabilities & IFCAP_NETMAP && NA(*ifp))
86768b8534bSLuigi Rizzo 		return 0;	/* valid pointer, we hold the refcount */
86868b8534bSLuigi Rizzo 	if_rele(*ifp);
86968b8534bSLuigi Rizzo 	return EINVAL;	// not NETMAP capable
87068b8534bSLuigi Rizzo }
87168b8534bSLuigi Rizzo 
87268b8534bSLuigi Rizzo 
87368b8534bSLuigi Rizzo /*
87468b8534bSLuigi Rizzo  * Error routine called when txsync/rxsync detects an error.
87568b8534bSLuigi Rizzo  * Can't do much more than resetting cur = hwcur, avail = hwavail.
87668b8534bSLuigi Rizzo  * Return 1 on reinit.
877506cc70cSLuigi Rizzo  *
878506cc70cSLuigi Rizzo  * This routine is only called by the upper half of the kernel.
879506cc70cSLuigi Rizzo  * It only reads hwcur (which is changed only by the upper half, too)
880506cc70cSLuigi Rizzo  * and hwavail (which may be changed by the lower half, but only on
881506cc70cSLuigi Rizzo  * a tx ring and only to increase it, so any error will be recovered
882506cc70cSLuigi Rizzo  * on the next call). For the above, we don't strictly need to call
883506cc70cSLuigi Rizzo  * it under lock.
88468b8534bSLuigi Rizzo  */
88568b8534bSLuigi Rizzo int
88668b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring)
88768b8534bSLuigi Rizzo {
88868b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
88968b8534bSLuigi Rizzo 	u_int i, lim = kring->nkr_num_slots - 1;
89068b8534bSLuigi Rizzo 	int errors = 0;
89168b8534bSLuigi Rizzo 
89268b8534bSLuigi Rizzo 	D("called for %s", kring->na->ifp->if_xname);
89368b8534bSLuigi Rizzo 	if (ring->cur > lim)
89468b8534bSLuigi Rizzo 		errors++;
89568b8534bSLuigi Rizzo 	for (i = 0; i <= lim; i++) {
89668b8534bSLuigi Rizzo 		u_int idx = ring->slot[i].buf_idx;
89768b8534bSLuigi Rizzo 		u_int len = ring->slot[i].len;
89868b8534bSLuigi Rizzo 		if (idx < 2 || idx >= netmap_total_buffers) {
89968b8534bSLuigi Rizzo 			if (!errors++)
90068b8534bSLuigi Rizzo 				D("bad buffer at slot %d idx %d len %d ", i, idx, len);
90168b8534bSLuigi Rizzo 			ring->slot[i].buf_idx = 0;
90268b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
90368b8534bSLuigi Rizzo 		} else if (len > NETMAP_BUF_SIZE) {
90468b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
90568b8534bSLuigi Rizzo 			if (!errors++)
90668b8534bSLuigi Rizzo 				D("bad len %d at slot %d idx %d",
90768b8534bSLuigi Rizzo 					len, i, idx);
90868b8534bSLuigi Rizzo 		}
90968b8534bSLuigi Rizzo 	}
91068b8534bSLuigi Rizzo 	if (errors) {
91168b8534bSLuigi Rizzo 		int pos = kring - kring->na->tx_rings;
91264ae02c3SLuigi Rizzo 		int n = kring->na->num_tx_queues + 1;
91368b8534bSLuigi Rizzo 
91468b8534bSLuigi Rizzo 		D("total %d errors", errors);
91568b8534bSLuigi Rizzo 		errors++;
91668b8534bSLuigi Rizzo 		D("%s %s[%d] reinit, cur %d -> %d avail %d -> %d",
91768b8534bSLuigi Rizzo 			kring->na->ifp->if_xname,
91868b8534bSLuigi Rizzo 			pos < n ?  "TX" : "RX", pos < n ? pos : pos - n,
91968b8534bSLuigi Rizzo 			ring->cur, kring->nr_hwcur,
92068b8534bSLuigi Rizzo 			ring->avail, kring->nr_hwavail);
92168b8534bSLuigi Rizzo 		ring->cur = kring->nr_hwcur;
92268b8534bSLuigi Rizzo 		ring->avail = kring->nr_hwavail;
92368b8534bSLuigi Rizzo 	}
92468b8534bSLuigi Rizzo 	return (errors ? 1 : 0);
92568b8534bSLuigi Rizzo }
92668b8534bSLuigi Rizzo 
92768b8534bSLuigi Rizzo 
92868b8534bSLuigi Rizzo /*
92968b8534bSLuigi Rizzo  * Set the ring ID. For devices with a single queue, a request
93068b8534bSLuigi Rizzo  * for all rings is the same as a single ring.
93168b8534bSLuigi Rizzo  */
93268b8534bSLuigi Rizzo static int
93368b8534bSLuigi Rizzo netmap_set_ringid(struct netmap_priv_d *priv, u_int ringid)
93468b8534bSLuigi Rizzo {
93568b8534bSLuigi Rizzo 	struct ifnet *ifp = priv->np_ifp;
93668b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
93768b8534bSLuigi Rizzo 	u_int i = ringid & NETMAP_RING_MASK;
93864ae02c3SLuigi Rizzo 	/* initially (np_qfirst == np_qlast) we don't want to lock */
93968b8534bSLuigi Rizzo 	int need_lock = (priv->np_qfirst != priv->np_qlast);
94064ae02c3SLuigi Rizzo 	int lim = na->num_rx_queues;
94168b8534bSLuigi Rizzo 
94264ae02c3SLuigi Rizzo 	if (na->num_tx_queues > lim)
94364ae02c3SLuigi Rizzo 		lim = na->num_tx_queues;
94464ae02c3SLuigi Rizzo 	if ( (ringid & NETMAP_HW_RING) && i >= lim) {
94568b8534bSLuigi Rizzo 		D("invalid ring id %d", i);
94668b8534bSLuigi Rizzo 		return (EINVAL);
94768b8534bSLuigi Rizzo 	}
94868b8534bSLuigi Rizzo 	if (need_lock)
9491a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
95068b8534bSLuigi Rizzo 	priv->np_ringid = ringid;
95168b8534bSLuigi Rizzo 	if (ringid & NETMAP_SW_RING) {
95264ae02c3SLuigi Rizzo 		priv->np_qfirst = NETMAP_SW_RING;
95364ae02c3SLuigi Rizzo 		priv->np_qlast = 0;
95468b8534bSLuigi Rizzo 	} else if (ringid & NETMAP_HW_RING) {
95568b8534bSLuigi Rizzo 		priv->np_qfirst = i;
95668b8534bSLuigi Rizzo 		priv->np_qlast = i + 1;
95768b8534bSLuigi Rizzo 	} else {
95868b8534bSLuigi Rizzo 		priv->np_qfirst = 0;
95964ae02c3SLuigi Rizzo 		priv->np_qlast = NETMAP_HW_RING ;
96068b8534bSLuigi Rizzo 	}
96168b8534bSLuigi Rizzo 	priv->np_txpoll = (ringid & NETMAP_NO_TX_POLL) ? 0 : 1;
96268b8534bSLuigi Rizzo 	if (need_lock)
9631a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
96468b8534bSLuigi Rizzo 	if (ringid & NETMAP_SW_RING)
96568b8534bSLuigi Rizzo 		D("ringid %s set to SW RING", ifp->if_xname);
96668b8534bSLuigi Rizzo 	else if (ringid & NETMAP_HW_RING)
96768b8534bSLuigi Rizzo 		D("ringid %s set to HW RING %d", ifp->if_xname,
96868b8534bSLuigi Rizzo 			priv->np_qfirst);
96968b8534bSLuigi Rizzo 	else
97064ae02c3SLuigi Rizzo 		D("ringid %s set to all %d HW RINGS", ifp->if_xname, lim);
97168b8534bSLuigi Rizzo 	return 0;
97268b8534bSLuigi Rizzo }
97368b8534bSLuigi Rizzo 
97468b8534bSLuigi Rizzo /*
97568b8534bSLuigi Rizzo  * ioctl(2) support for the "netmap" device.
97668b8534bSLuigi Rizzo  *
97768b8534bSLuigi Rizzo  * Following a list of accepted commands:
97868b8534bSLuigi Rizzo  * - NIOCGINFO
97968b8534bSLuigi Rizzo  * - SIOCGIFADDR	just for convenience
98068b8534bSLuigi Rizzo  * - NIOCREGIF
98168b8534bSLuigi Rizzo  * - NIOCUNREGIF
98268b8534bSLuigi Rizzo  * - NIOCTXSYNC
98368b8534bSLuigi Rizzo  * - NIOCRXSYNC
98468b8534bSLuigi Rizzo  *
98568b8534bSLuigi Rizzo  * Return 0 on success, errno otherwise.
98668b8534bSLuigi Rizzo  */
98768b8534bSLuigi Rizzo static int
98868b8534bSLuigi Rizzo netmap_ioctl(__unused struct cdev *dev, u_long cmd, caddr_t data,
989506cc70cSLuigi Rizzo 	__unused int fflag, struct thread *td)
99068b8534bSLuigi Rizzo {
99168b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
99268b8534bSLuigi Rizzo 	struct ifnet *ifp;
99368b8534bSLuigi Rizzo 	struct nmreq *nmr = (struct nmreq *) data;
99468b8534bSLuigi Rizzo 	struct netmap_adapter *na;
99568b8534bSLuigi Rizzo 	int error;
99664ae02c3SLuigi Rizzo 	u_int i, lim;
99768b8534bSLuigi Rizzo 	struct netmap_if *nifp;
99868b8534bSLuigi Rizzo 
999506cc70cSLuigi Rizzo 	CURVNET_SET(TD_TO_VNET(td));
1000506cc70cSLuigi Rizzo 
100168b8534bSLuigi Rizzo 	error = devfs_get_cdevpriv((void **)&priv);
1002506cc70cSLuigi Rizzo 	if (error != ENOENT && error != 0) {
1003506cc70cSLuigi Rizzo 		CURVNET_RESTORE();
100468b8534bSLuigi Rizzo 		return (error);
1005506cc70cSLuigi Rizzo 	}
100668b8534bSLuigi Rizzo 
100768b8534bSLuigi Rizzo 	error = 0;	/* Could be ENOENT */
100868b8534bSLuigi Rizzo 	switch (cmd) {
100968b8534bSLuigi Rizzo 	case NIOCGINFO:		/* return capabilities etc */
101068b8534bSLuigi Rizzo 		/* memsize is always valid */
101164ae02c3SLuigi Rizzo 		nmr->nr_memsize = nm_mem->nm_totalsize;
101268b8534bSLuigi Rizzo 		nmr->nr_offset = 0;
101364ae02c3SLuigi Rizzo 		nmr->nr_rx_rings = nmr->nr_tx_rings = 0;
101464ae02c3SLuigi Rizzo 		nmr->nr_rx_slots = nmr->nr_tx_slots = 0;
101564ae02c3SLuigi Rizzo 		if (nmr->nr_version != NETMAP_API) {
101664ae02c3SLuigi Rizzo 			D("API mismatch got %d have %d",
101764ae02c3SLuigi Rizzo 				nmr->nr_version, NETMAP_API);
101864ae02c3SLuigi Rizzo 			nmr->nr_version = NETMAP_API;
101964ae02c3SLuigi Rizzo 			error = EINVAL;
102064ae02c3SLuigi Rizzo 			break;
102164ae02c3SLuigi Rizzo 		}
102268b8534bSLuigi Rizzo 		if (nmr->nr_name[0] == '\0')	/* just get memory info */
102368b8534bSLuigi Rizzo 			break;
102468b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* get a refcount */
102568b8534bSLuigi Rizzo 		if (error)
102668b8534bSLuigi Rizzo 			break;
102768b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap_adapter */
102864ae02c3SLuigi Rizzo 		nmr->nr_rx_rings = na->num_rx_queues;
102964ae02c3SLuigi Rizzo 		nmr->nr_tx_rings = na->num_tx_queues;
103064ae02c3SLuigi Rizzo 		nmr->nr_rx_slots = na->num_rx_desc;
103164ae02c3SLuigi Rizzo 		nmr->nr_tx_slots = na->num_tx_desc;
103268b8534bSLuigi Rizzo 		if_rele(ifp);	/* return the refcount */
103368b8534bSLuigi Rizzo 		break;
103468b8534bSLuigi Rizzo 
103568b8534bSLuigi Rizzo 	case NIOCREGIF:
103664ae02c3SLuigi Rizzo 		if (nmr->nr_version != NETMAP_API) {
103764ae02c3SLuigi Rizzo 			nmr->nr_version = NETMAP_API;
103864ae02c3SLuigi Rizzo 			error = EINVAL;
103964ae02c3SLuigi Rizzo 			break;
104064ae02c3SLuigi Rizzo 		}
1041506cc70cSLuigi Rizzo 		if (priv != NULL) {	/* thread already registered */
1042506cc70cSLuigi Rizzo 			error = netmap_set_ringid(priv, nmr->nr_ringid);
1043506cc70cSLuigi Rizzo 			break;
1044506cc70cSLuigi Rizzo 		}
104568b8534bSLuigi Rizzo 		/* find the interface and a reference */
104668b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* keep reference */
104768b8534bSLuigi Rizzo 		if (error)
104868b8534bSLuigi Rizzo 			break;
104968b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap adapter */
105068b8534bSLuigi Rizzo 		/*
105168b8534bSLuigi Rizzo 		 * Allocate the private per-thread structure.
105268b8534bSLuigi Rizzo 		 * XXX perhaps we can use a blocking malloc ?
105368b8534bSLuigi Rizzo 		 */
105468b8534bSLuigi Rizzo 		priv = malloc(sizeof(struct netmap_priv_d), M_DEVBUF,
105568b8534bSLuigi Rizzo 			      M_NOWAIT | M_ZERO);
105668b8534bSLuigi Rizzo 		if (priv == NULL) {
105768b8534bSLuigi Rizzo 			error = ENOMEM;
105868b8534bSLuigi Rizzo 			if_rele(ifp);   /* return the refcount */
105968b8534bSLuigi Rizzo 			break;
106068b8534bSLuigi Rizzo 		}
106168b8534bSLuigi Rizzo 
106268b8534bSLuigi Rizzo 		for (i = 10; i > 0; i--) {
10631a26580eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_REG_LOCK, 0);
106468b8534bSLuigi Rizzo 			if (!NETMAP_DELETING(na))
106568b8534bSLuigi Rizzo 				break;
10661a26580eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
106768b8534bSLuigi Rizzo 			tsleep(na, 0, "NIOCREGIF", hz/10);
106868b8534bSLuigi Rizzo 		}
106968b8534bSLuigi Rizzo 		if (i == 0) {
107068b8534bSLuigi Rizzo 			D("too many NIOCREGIF attempts, give up");
107168b8534bSLuigi Rizzo 			error = EINVAL;
107268b8534bSLuigi Rizzo 			free(priv, M_DEVBUF);
107368b8534bSLuigi Rizzo 			if_rele(ifp);	/* return the refcount */
107468b8534bSLuigi Rizzo 			break;
107568b8534bSLuigi Rizzo 		}
107668b8534bSLuigi Rizzo 
107768b8534bSLuigi Rizzo 		priv->np_ifp = ifp;	/* store the reference */
107868b8534bSLuigi Rizzo 		error = netmap_set_ringid(priv, nmr->nr_ringid);
107968b8534bSLuigi Rizzo 		if (error)
108068b8534bSLuigi Rizzo 			goto error;
108168b8534bSLuigi Rizzo 		priv->np_nifp = nifp = netmap_if_new(nmr->nr_name, na);
108268b8534bSLuigi Rizzo 		if (nifp == NULL) { /* allocation failed */
108368b8534bSLuigi Rizzo 			error = ENOMEM;
108468b8534bSLuigi Rizzo 		} else if (ifp->if_capenable & IFCAP_NETMAP) {
108568b8534bSLuigi Rizzo 			/* was already set */
108668b8534bSLuigi Rizzo 		} else {
108768b8534bSLuigi Rizzo 			/* Otherwise set the card in netmap mode
108868b8534bSLuigi Rizzo 			 * and make it use the shared buffers.
108968b8534bSLuigi Rizzo 			 */
109068b8534bSLuigi Rizzo 			error = na->nm_register(ifp, 1); /* mode on */
10915819da83SLuigi Rizzo 			if (error)
10925819da83SLuigi Rizzo 				netmap_dtor_locked(priv);
109368b8534bSLuigi Rizzo 		}
109468b8534bSLuigi Rizzo 
109568b8534bSLuigi Rizzo 		if (error) {	/* reg. failed, release priv and ref */
109668b8534bSLuigi Rizzo error:
10971a26580eSLuigi Rizzo 			na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
109868b8534bSLuigi Rizzo 			if_rele(ifp);	/* return the refcount */
10995819da83SLuigi Rizzo 			bzero(priv, sizeof(*priv));
11005819da83SLuigi Rizzo 			free(priv, M_DEVBUF);
110168b8534bSLuigi Rizzo 			break;
110268b8534bSLuigi Rizzo 		}
110368b8534bSLuigi Rizzo 
11041a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_REG_UNLOCK, 0);
110568b8534bSLuigi Rizzo 		error = devfs_set_cdevpriv(priv, netmap_dtor);
110668b8534bSLuigi Rizzo 
110768b8534bSLuigi Rizzo 		if (error != 0) {
110868b8534bSLuigi Rizzo 			/* could not assign the private storage for the
110968b8534bSLuigi Rizzo 			 * thread, call the destructor explicitly.
111068b8534bSLuigi Rizzo 			 */
111168b8534bSLuigi Rizzo 			netmap_dtor(priv);
111268b8534bSLuigi Rizzo 			break;
111368b8534bSLuigi Rizzo 		}
111468b8534bSLuigi Rizzo 
111568b8534bSLuigi Rizzo 		/* return the offset of the netmap_if object */
111664ae02c3SLuigi Rizzo 		nmr->nr_rx_rings = na->num_rx_queues;
111764ae02c3SLuigi Rizzo 		nmr->nr_tx_rings = na->num_tx_queues;
111864ae02c3SLuigi Rizzo 		nmr->nr_rx_slots = na->num_rx_desc;
111964ae02c3SLuigi Rizzo 		nmr->nr_tx_slots = na->num_tx_desc;
112064ae02c3SLuigi Rizzo 		nmr->nr_memsize = nm_mem->nm_totalsize;
1121446ee301SLuigi Rizzo 		nmr->nr_offset = netmap_if_offset(nifp);
112268b8534bSLuigi Rizzo 		break;
112368b8534bSLuigi Rizzo 
112468b8534bSLuigi Rizzo 	case NIOCUNREGIF:
1125506cc70cSLuigi Rizzo 		if (priv == NULL) {
1126506cc70cSLuigi Rizzo 			error = ENXIO;
1127506cc70cSLuigi Rizzo 			break;
1128506cc70cSLuigi Rizzo 		}
112968b8534bSLuigi Rizzo 
113068b8534bSLuigi Rizzo 		/* the interface is unregistered inside the
113168b8534bSLuigi Rizzo 		   destructor of the private data. */
113268b8534bSLuigi Rizzo 		devfs_clear_cdevpriv();
113368b8534bSLuigi Rizzo 		break;
113468b8534bSLuigi Rizzo 
113568b8534bSLuigi Rizzo 	case NIOCTXSYNC:
113668b8534bSLuigi Rizzo         case NIOCRXSYNC:
1137506cc70cSLuigi Rizzo 		if (priv == NULL) {
1138506cc70cSLuigi Rizzo 			error = ENXIO;
1139506cc70cSLuigi Rizzo 			break;
1140506cc70cSLuigi Rizzo 		}
114168b8534bSLuigi Rizzo 		ifp = priv->np_ifp;	/* we have a reference */
114268b8534bSLuigi Rizzo 		na = NA(ifp); /* retrieve netmap adapter */
114364ae02c3SLuigi Rizzo 		if (priv->np_qfirst == NETMAP_SW_RING) { /* host rings */
114468b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC)
114568b8534bSLuigi Rizzo 				netmap_sync_to_host(na);
114668b8534bSLuigi Rizzo 			else
114768b8534bSLuigi Rizzo 				netmap_sync_from_host(na, NULL);
1148506cc70cSLuigi Rizzo 			break;
114968b8534bSLuigi Rizzo 		}
115064ae02c3SLuigi Rizzo 		/* find the last ring to scan */
115164ae02c3SLuigi Rizzo 		lim = priv->np_qlast;
115264ae02c3SLuigi Rizzo 		if (lim == NETMAP_HW_RING)
115364ae02c3SLuigi Rizzo 		    lim = (cmd == NIOCTXSYNC) ? na->num_tx_queues : na->num_rx_queues;
115468b8534bSLuigi Rizzo 
115564ae02c3SLuigi Rizzo 		for (i = priv->np_qfirst; i < lim; i++) {
115668b8534bSLuigi Rizzo 		    if (cmd == NIOCTXSYNC) {
115768b8534bSLuigi Rizzo 			struct netmap_kring *kring = &na->tx_rings[i];
115868b8534bSLuigi Rizzo 			if (netmap_verbose & NM_VERB_TXSYNC)
115968b8534bSLuigi Rizzo 				D("sync tx ring %d cur %d hwcur %d",
116068b8534bSLuigi Rizzo 					i, kring->ring->cur,
116168b8534bSLuigi Rizzo 					kring->nr_hwcur);
11621a26580eSLuigi Rizzo                         na->nm_txsync(ifp, i, 1 /* do lock */);
116368b8534bSLuigi Rizzo 			if (netmap_verbose & NM_VERB_TXSYNC)
116468b8534bSLuigi Rizzo 				D("after sync tx ring %d cur %d hwcur %d",
116568b8534bSLuigi Rizzo 					i, kring->ring->cur,
116668b8534bSLuigi Rizzo 					kring->nr_hwcur);
116768b8534bSLuigi Rizzo 		    } else {
11681a26580eSLuigi Rizzo 			na->nm_rxsync(ifp, i, 1 /* do lock */);
116968b8534bSLuigi Rizzo 			microtime(&na->rx_rings[i].ring->ts);
117068b8534bSLuigi Rizzo 		    }
117168b8534bSLuigi Rizzo 		}
117268b8534bSLuigi Rizzo 
117368b8534bSLuigi Rizzo                 break;
117468b8534bSLuigi Rizzo 
117568b8534bSLuigi Rizzo 	case BIOCIMMEDIATE:
117668b8534bSLuigi Rizzo 	case BIOCGHDRCMPLT:
117768b8534bSLuigi Rizzo 	case BIOCSHDRCMPLT:
117868b8534bSLuigi Rizzo 	case BIOCSSEESENT:
117968b8534bSLuigi Rizzo 		D("ignore BIOCIMMEDIATE/BIOCSHDRCMPLT/BIOCSHDRCMPLT/BIOCSSEESENT");
118068b8534bSLuigi Rizzo 		break;
118168b8534bSLuigi Rizzo 
1182babc7c12SLuigi Rizzo 	default:	/* allow device-specific ioctls */
118368b8534bSLuigi Rizzo 	    {
118468b8534bSLuigi Rizzo 		struct socket so;
118568b8534bSLuigi Rizzo 		bzero(&so, sizeof(so));
118668b8534bSLuigi Rizzo 		error = get_ifp(nmr->nr_name, &ifp); /* keep reference */
118768b8534bSLuigi Rizzo 		if (error)
118868b8534bSLuigi Rizzo 			break;
118968b8534bSLuigi Rizzo 		so.so_vnet = ifp->if_vnet;
119068b8534bSLuigi Rizzo 		// so->so_proto not null.
119168b8534bSLuigi Rizzo 		error = ifioctl(&so, cmd, data, td);
119268b8534bSLuigi Rizzo 		if_rele(ifp);
1193babc7c12SLuigi Rizzo 		break;
119468b8534bSLuigi Rizzo 	    }
119568b8534bSLuigi Rizzo 	}
119668b8534bSLuigi Rizzo 
1197506cc70cSLuigi Rizzo 	CURVNET_RESTORE();
119868b8534bSLuigi Rizzo 	return (error);
119968b8534bSLuigi Rizzo }
120068b8534bSLuigi Rizzo 
120168b8534bSLuigi Rizzo 
120268b8534bSLuigi Rizzo /*
120368b8534bSLuigi Rizzo  * select(2) and poll(2) handlers for the "netmap" device.
120468b8534bSLuigi Rizzo  *
120568b8534bSLuigi Rizzo  * Can be called for one or more queues.
120668b8534bSLuigi Rizzo  * Return true the event mask corresponding to ready events.
120768b8534bSLuigi Rizzo  * If there are no ready events, do a selrecord on either individual
120868b8534bSLuigi Rizzo  * selfd or on the global one.
120968b8534bSLuigi Rizzo  * Device-dependent parts (locking and sync of tx/rx rings)
121068b8534bSLuigi Rizzo  * are done through callbacks.
121168b8534bSLuigi Rizzo  */
121268b8534bSLuigi Rizzo static int
121368b8534bSLuigi Rizzo netmap_poll(__unused struct cdev *dev, int events, struct thread *td)
121468b8534bSLuigi Rizzo {
121568b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
121668b8534bSLuigi Rizzo 	struct netmap_adapter *na;
121768b8534bSLuigi Rizzo 	struct ifnet *ifp;
121868b8534bSLuigi Rizzo 	struct netmap_kring *kring;
1219d0c7b075SLuigi Rizzo 	u_int core_lock, i, check_all, want_tx, want_rx, revents = 0;
122064ae02c3SLuigi Rizzo 	u_int lim_tx, lim_rx;
1221bcda432eSLuigi Rizzo 	enum {NO_CL, NEED_CL, LOCKED_CL }; /* see below */
122268b8534bSLuigi Rizzo 
122368b8534bSLuigi Rizzo 	if (devfs_get_cdevpriv((void **)&priv) != 0 || priv == NULL)
122468b8534bSLuigi Rizzo 		return POLLERR;
122568b8534bSLuigi Rizzo 
122668b8534bSLuigi Rizzo 	ifp = priv->np_ifp;
122768b8534bSLuigi Rizzo 	// XXX check for deleting() ?
122868b8534bSLuigi Rizzo 	if ( (ifp->if_capenable & IFCAP_NETMAP) == 0)
122968b8534bSLuigi Rizzo 		return POLLERR;
123068b8534bSLuigi Rizzo 
123168b8534bSLuigi Rizzo 	if (netmap_verbose & 0x8000)
123268b8534bSLuigi Rizzo 		D("device %s events 0x%x", ifp->if_xname, events);
123368b8534bSLuigi Rizzo 	want_tx = events & (POLLOUT | POLLWRNORM);
123468b8534bSLuigi Rizzo 	want_rx = events & (POLLIN | POLLRDNORM);
123568b8534bSLuigi Rizzo 
123668b8534bSLuigi Rizzo 	na = NA(ifp); /* retrieve netmap adapter */
123768b8534bSLuigi Rizzo 
123864ae02c3SLuigi Rizzo 	lim_tx = na->num_tx_queues;
123964ae02c3SLuigi Rizzo 	lim_rx = na->num_rx_queues;
124068b8534bSLuigi Rizzo 	/* how many queues we are scanning */
124164ae02c3SLuigi Rizzo 	if (priv->np_qfirst == NETMAP_SW_RING) {
124268b8534bSLuigi Rizzo 		if (priv->np_txpoll || want_tx) {
124368b8534bSLuigi Rizzo 			/* push any packets up, then we are always ready */
124464ae02c3SLuigi Rizzo 			kring = &na->tx_rings[lim_tx];
124568b8534bSLuigi Rizzo 			netmap_sync_to_host(na);
124668b8534bSLuigi Rizzo 			revents |= want_tx;
124768b8534bSLuigi Rizzo 		}
124868b8534bSLuigi Rizzo 		if (want_rx) {
124964ae02c3SLuigi Rizzo 			kring = &na->rx_rings[lim_rx];
125068b8534bSLuigi Rizzo 			if (kring->ring->avail == 0)
125168b8534bSLuigi Rizzo 				netmap_sync_from_host(na, td);
125268b8534bSLuigi Rizzo 			if (kring->ring->avail > 0) {
125368b8534bSLuigi Rizzo 				revents |= want_rx;
125468b8534bSLuigi Rizzo 			}
125568b8534bSLuigi Rizzo 		}
125668b8534bSLuigi Rizzo 		return (revents);
125768b8534bSLuigi Rizzo 	}
125868b8534bSLuigi Rizzo 
125968b8534bSLuigi Rizzo 	/*
126068b8534bSLuigi Rizzo 	 * check_all is set if the card has more than one queue and
126168b8534bSLuigi Rizzo 	 * the client is polling all of them. If true, we sleep on
126268b8534bSLuigi Rizzo 	 * the "global" selfd, otherwise we sleep on individual selfd
126368b8534bSLuigi Rizzo 	 * (we can only sleep on one of them per direction).
126468b8534bSLuigi Rizzo 	 * The interrupt routine in the driver should always wake on
126568b8534bSLuigi Rizzo 	 * the individual selfd, and also on the global one if the card
126668b8534bSLuigi Rizzo 	 * has more than one ring.
126768b8534bSLuigi Rizzo 	 *
126868b8534bSLuigi Rizzo 	 * If the card has only one lock, we just use that.
126968b8534bSLuigi Rizzo 	 * If the card has separate ring locks, we just use those
127068b8534bSLuigi Rizzo 	 * unless we are doing check_all, in which case the whole
127168b8534bSLuigi Rizzo 	 * loop is wrapped by the global lock.
127268b8534bSLuigi Rizzo 	 * We acquire locks only when necessary: if poll is called
127368b8534bSLuigi Rizzo 	 * when buffers are available, we can just return without locks.
127468b8534bSLuigi Rizzo 	 *
127568b8534bSLuigi Rizzo 	 * rxsync() is only called if we run out of buffers on a POLLIN.
127668b8534bSLuigi Rizzo 	 * txsync() is called if we run out of buffers on POLLOUT, or
127768b8534bSLuigi Rizzo 	 * there are pending packets to send. The latter can be disabled
127868b8534bSLuigi Rizzo 	 * passing NETMAP_NO_TX_POLL in the NIOCREG call.
127968b8534bSLuigi Rizzo 	 */
128064ae02c3SLuigi Rizzo 	check_all = (priv->np_qlast == NETMAP_HW_RING) && (lim_tx > 1 || lim_rx > 1);
128168b8534bSLuigi Rizzo 
128268b8534bSLuigi Rizzo 	/*
128368b8534bSLuigi Rizzo 	 * core_lock indicates what to do with the core lock.
128468b8534bSLuigi Rizzo 	 * The core lock is used when either the card has no individual
128568b8534bSLuigi Rizzo 	 * locks, or it has individual locks but we are cheking all
128668b8534bSLuigi Rizzo 	 * rings so we need the core lock to avoid missing wakeup events.
128768b8534bSLuigi Rizzo 	 *
128868b8534bSLuigi Rizzo 	 * It has three possible states:
128968b8534bSLuigi Rizzo 	 * NO_CL	we don't need to use the core lock, e.g.
129068b8534bSLuigi Rizzo 	 *		because we are protected by individual locks.
129168b8534bSLuigi Rizzo 	 * NEED_CL	we need the core lock. In this case, when we
129268b8534bSLuigi Rizzo 	 *		call the lock routine, move to LOCKED_CL
129368b8534bSLuigi Rizzo 	 *		to remember to release the lock once done.
129468b8534bSLuigi Rizzo 	 * LOCKED_CL	core lock is set, so we need to release it.
129568b8534bSLuigi Rizzo 	 */
1296d0c7b075SLuigi Rizzo 	core_lock = (check_all || !na->separate_locks) ? NEED_CL : NO_CL;
129764ae02c3SLuigi Rizzo 	if (priv->np_qlast != NETMAP_HW_RING) {
129864ae02c3SLuigi Rizzo 		lim_tx = lim_rx = priv->np_qlast;
129964ae02c3SLuigi Rizzo 	}
130064ae02c3SLuigi Rizzo 
130168b8534bSLuigi Rizzo 	/*
130268b8534bSLuigi Rizzo 	 * We start with a lock free round which is good if we have
130368b8534bSLuigi Rizzo 	 * data available. If this fails, then lock and call the sync
130468b8534bSLuigi Rizzo 	 * routines.
130568b8534bSLuigi Rizzo 	 */
130664ae02c3SLuigi Rizzo 	for (i = priv->np_qfirst; want_rx && i < lim_rx; i++) {
130768b8534bSLuigi Rizzo 		kring = &na->rx_rings[i];
130868b8534bSLuigi Rizzo 		if (kring->ring->avail > 0) {
130968b8534bSLuigi Rizzo 			revents |= want_rx;
131068b8534bSLuigi Rizzo 			want_rx = 0;	/* also breaks the loop */
131168b8534bSLuigi Rizzo 		}
131268b8534bSLuigi Rizzo 	}
131364ae02c3SLuigi Rizzo 	for (i = priv->np_qfirst; want_tx && i < lim_tx; i++) {
131468b8534bSLuigi Rizzo 		kring = &na->tx_rings[i];
131568b8534bSLuigi Rizzo 		if (kring->ring->avail > 0) {
131668b8534bSLuigi Rizzo 			revents |= want_tx;
131768b8534bSLuigi Rizzo 			want_tx = 0;	/* also breaks the loop */
131868b8534bSLuigi Rizzo 		}
131968b8534bSLuigi Rizzo 	}
132068b8534bSLuigi Rizzo 
132168b8534bSLuigi Rizzo 	/*
132268b8534bSLuigi Rizzo 	 * If we to push packets out (priv->np_txpoll) or want_tx is
132368b8534bSLuigi Rizzo 	 * still set, we do need to run the txsync calls (on all rings,
132468b8534bSLuigi Rizzo 	 * to avoid that the tx rings stall).
132568b8534bSLuigi Rizzo 	 */
132668b8534bSLuigi Rizzo 	if (priv->np_txpoll || want_tx) {
132764ae02c3SLuigi Rizzo 		for (i = priv->np_qfirst; i < lim_tx; i++) {
132868b8534bSLuigi Rizzo 			kring = &na->tx_rings[i];
13295819da83SLuigi Rizzo 			/*
13305819da83SLuigi Rizzo 			 * Skip the current ring if want_tx == 0
13315819da83SLuigi Rizzo 			 * (we have already done a successful sync on
13325819da83SLuigi Rizzo 			 * a previous ring) AND kring->cur == kring->hwcur
13335819da83SLuigi Rizzo 			 * (there are no pending transmissions for this ring).
13345819da83SLuigi Rizzo 			 */
133568b8534bSLuigi Rizzo 			if (!want_tx && kring->ring->cur == kring->nr_hwcur)
133668b8534bSLuigi Rizzo 				continue;
133768b8534bSLuigi Rizzo 			if (core_lock == NEED_CL) {
13381a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
133968b8534bSLuigi Rizzo 				core_lock = LOCKED_CL;
134068b8534bSLuigi Rizzo 			}
134168b8534bSLuigi Rizzo 			if (na->separate_locks)
13421a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_TX_LOCK, i);
134368b8534bSLuigi Rizzo 			if (netmap_verbose & NM_VERB_TXSYNC)
134468b8534bSLuigi Rizzo 				D("send %d on %s %d",
134568b8534bSLuigi Rizzo 					kring->ring->cur,
134668b8534bSLuigi Rizzo 					ifp->if_xname, i);
13471a26580eSLuigi Rizzo 			if (na->nm_txsync(ifp, i, 0 /* no lock */))
134868b8534bSLuigi Rizzo 				revents |= POLLERR;
134968b8534bSLuigi Rizzo 
13505819da83SLuigi Rizzo 			/* Check avail/call selrecord only if called with POLLOUT */
135168b8534bSLuigi Rizzo 			if (want_tx) {
135268b8534bSLuigi Rizzo 				if (kring->ring->avail > 0) {
135368b8534bSLuigi Rizzo 					/* stop at the first ring. We don't risk
135468b8534bSLuigi Rizzo 					 * starvation.
135568b8534bSLuigi Rizzo 					 */
135668b8534bSLuigi Rizzo 					revents |= want_tx;
135768b8534bSLuigi Rizzo 					want_tx = 0;
135868b8534bSLuigi Rizzo 				} else if (!check_all)
135968b8534bSLuigi Rizzo 					selrecord(td, &kring->si);
136068b8534bSLuigi Rizzo 			}
136168b8534bSLuigi Rizzo 			if (na->separate_locks)
13621a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_TX_UNLOCK, i);
136368b8534bSLuigi Rizzo 		}
136468b8534bSLuigi Rizzo 	}
136568b8534bSLuigi Rizzo 
136668b8534bSLuigi Rizzo 	/*
136768b8534bSLuigi Rizzo 	 * now if want_rx is still set we need to lock and rxsync.
136868b8534bSLuigi Rizzo 	 * Do it on all rings because otherwise we starve.
136968b8534bSLuigi Rizzo 	 */
137068b8534bSLuigi Rizzo 	if (want_rx) {
137164ae02c3SLuigi Rizzo 		for (i = priv->np_qfirst; i < lim_rx; i++) {
137268b8534bSLuigi Rizzo 			kring = &na->rx_rings[i];
137368b8534bSLuigi Rizzo 			if (core_lock == NEED_CL) {
13741a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
137568b8534bSLuigi Rizzo 				core_lock = LOCKED_CL;
137668b8534bSLuigi Rizzo 			}
137768b8534bSLuigi Rizzo 			if (na->separate_locks)
13781a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_RX_LOCK, i);
137968b8534bSLuigi Rizzo 
13801a26580eSLuigi Rizzo 			if (na->nm_rxsync(ifp, i, 0 /* no lock */))
138168b8534bSLuigi Rizzo 				revents |= POLLERR;
13825819da83SLuigi Rizzo 			if (netmap_no_timestamp == 0 ||
13835819da83SLuigi Rizzo 					kring->ring->flags & NR_TIMESTAMP) {
138468b8534bSLuigi Rizzo 				microtime(&kring->ring->ts);
13855819da83SLuigi Rizzo 			}
138668b8534bSLuigi Rizzo 
138768b8534bSLuigi Rizzo 			if (kring->ring->avail > 0)
138868b8534bSLuigi Rizzo 				revents |= want_rx;
138968b8534bSLuigi Rizzo 			else if (!check_all)
139068b8534bSLuigi Rizzo 				selrecord(td, &kring->si);
139168b8534bSLuigi Rizzo 			if (na->separate_locks)
13921a26580eSLuigi Rizzo 				na->nm_lock(ifp, NETMAP_RX_UNLOCK, i);
139368b8534bSLuigi Rizzo 		}
139468b8534bSLuigi Rizzo 	}
139564ae02c3SLuigi Rizzo 	if (check_all && revents == 0) { /* signal on the global queue */
139668b8534bSLuigi Rizzo 		if (want_tx)
139764ae02c3SLuigi Rizzo 			selrecord(td, &na->tx_si);
139868b8534bSLuigi Rizzo 		if (want_rx)
139964ae02c3SLuigi Rizzo 			selrecord(td, &na->rx_si);
140068b8534bSLuigi Rizzo 	}
140168b8534bSLuigi Rizzo 	if (core_lock == LOCKED_CL)
14021a26580eSLuigi Rizzo 		na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
140368b8534bSLuigi Rizzo 
140468b8534bSLuigi Rizzo 	return (revents);
140568b8534bSLuigi Rizzo }
140668b8534bSLuigi Rizzo 
140768b8534bSLuigi Rizzo /*------- driver support routines ------*/
140868b8534bSLuigi Rizzo 
140968b8534bSLuigi Rizzo /*
1410babc7c12SLuigi Rizzo  * default lock wrapper.
14111a26580eSLuigi Rizzo  */
14121a26580eSLuigi Rizzo static void
1413babc7c12SLuigi Rizzo netmap_lock_wrapper(struct ifnet *dev, int what, u_int queueid)
14141a26580eSLuigi Rizzo {
1415babc7c12SLuigi Rizzo 	struct netmap_adapter *na = NA(dev);
14161a26580eSLuigi Rizzo 
14171a26580eSLuigi Rizzo 	switch (what) {
1418babc7c12SLuigi Rizzo #ifdef linux	/* some system do not need lock on register */
14191a26580eSLuigi Rizzo 	case NETMAP_REG_LOCK:
14201a26580eSLuigi Rizzo 	case NETMAP_REG_UNLOCK:
14211a26580eSLuigi Rizzo 		break;
1422babc7c12SLuigi Rizzo #endif /* linux */
14231a26580eSLuigi Rizzo 
14241a26580eSLuigi Rizzo 	case NETMAP_CORE_LOCK:
14251a26580eSLuigi Rizzo 		mtx_lock(&na->core_lock);
14261a26580eSLuigi Rizzo 		break;
14271a26580eSLuigi Rizzo 
14281a26580eSLuigi Rizzo 	case NETMAP_CORE_UNLOCK:
14291a26580eSLuigi Rizzo 		mtx_unlock(&na->core_lock);
14301a26580eSLuigi Rizzo 		break;
14311a26580eSLuigi Rizzo 
14321a26580eSLuigi Rizzo 	case NETMAP_TX_LOCK:
14331a26580eSLuigi Rizzo 		mtx_lock(&na->tx_rings[queueid].q_lock);
14341a26580eSLuigi Rizzo 		break;
14351a26580eSLuigi Rizzo 
14361a26580eSLuigi Rizzo 	case NETMAP_TX_UNLOCK:
14371a26580eSLuigi Rizzo 		mtx_unlock(&na->tx_rings[queueid].q_lock);
14381a26580eSLuigi Rizzo 		break;
14391a26580eSLuigi Rizzo 
14401a26580eSLuigi Rizzo 	case NETMAP_RX_LOCK:
14411a26580eSLuigi Rizzo 		mtx_lock(&na->rx_rings[queueid].q_lock);
14421a26580eSLuigi Rizzo 		break;
14431a26580eSLuigi Rizzo 
14441a26580eSLuigi Rizzo 	case NETMAP_RX_UNLOCK:
14451a26580eSLuigi Rizzo 		mtx_unlock(&na->rx_rings[queueid].q_lock);
14461a26580eSLuigi Rizzo 		break;
14471a26580eSLuigi Rizzo 	}
14481a26580eSLuigi Rizzo }
14491a26580eSLuigi Rizzo 
14501a26580eSLuigi Rizzo 
14511a26580eSLuigi Rizzo /*
145268b8534bSLuigi Rizzo  * Initialize a ``netmap_adapter`` object created by driver on attach.
145368b8534bSLuigi Rizzo  * We allocate a block of memory with room for a struct netmap_adapter
145468b8534bSLuigi Rizzo  * plus two sets of N+2 struct netmap_kring (where N is the number
145568b8534bSLuigi Rizzo  * of hardware rings):
145668b8534bSLuigi Rizzo  * krings	0..N-1	are for the hardware queues.
145768b8534bSLuigi Rizzo  * kring	N	is for the host stack queue
145868b8534bSLuigi Rizzo  * kring	N+1	is only used for the selinfo for all queues.
145968b8534bSLuigi Rizzo  * Return 0 on success, ENOMEM otherwise.
146064ae02c3SLuigi Rizzo  *
146164ae02c3SLuigi Rizzo  * na->num_tx_queues can be set for cards with different tx/rx setups
146268b8534bSLuigi Rizzo  */
146368b8534bSLuigi Rizzo int
146468b8534bSLuigi Rizzo netmap_attach(struct netmap_adapter *na, int num_queues)
146568b8534bSLuigi Rizzo {
146664ae02c3SLuigi Rizzo 	int i, n, size;
146768b8534bSLuigi Rizzo 	void *buf;
146868b8534bSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
146968b8534bSLuigi Rizzo 
147068b8534bSLuigi Rizzo 	if (ifp == NULL) {
147168b8534bSLuigi Rizzo 		D("ifp not set, giving up");
147268b8534bSLuigi Rizzo 		return EINVAL;
147368b8534bSLuigi Rizzo 	}
147464ae02c3SLuigi Rizzo 	/* clear other fields ? */
147568b8534bSLuigi Rizzo 	na->refcount = 0;
147664ae02c3SLuigi Rizzo 	if (na->num_tx_queues == 0)
147764ae02c3SLuigi Rizzo 		na->num_tx_queues = num_queues;
147864ae02c3SLuigi Rizzo 	na->num_rx_queues = num_queues;
147964ae02c3SLuigi Rizzo 	/* on each direction we have N+1 resources
148064ae02c3SLuigi Rizzo 	 * 0..n-1	are the hardware rings
148164ae02c3SLuigi Rizzo 	 * n		is the ring attached to the stack.
148264ae02c3SLuigi Rizzo 	 */
148364ae02c3SLuigi Rizzo 	n = na->num_rx_queues + na->num_tx_queues + 2;
148464ae02c3SLuigi Rizzo 	size = sizeof(*na) + n * sizeof(struct netmap_kring);
148568b8534bSLuigi Rizzo 
148668b8534bSLuigi Rizzo 	buf = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
148768b8534bSLuigi Rizzo 	if (buf) {
1488d0c7b075SLuigi Rizzo 		WNA(ifp) = buf;
148968b8534bSLuigi Rizzo 		na->tx_rings = (void *)((char *)buf + sizeof(*na));
149064ae02c3SLuigi Rizzo 		na->rx_rings = na->tx_rings + na->num_tx_queues + 1;
14915819da83SLuigi Rizzo 		na->buff_size = NETMAP_BUF_SIZE;
149268b8534bSLuigi Rizzo 		bcopy(na, buf, sizeof(*na));
149368b8534bSLuigi Rizzo 		ifp->if_capabilities |= IFCAP_NETMAP;
14941a26580eSLuigi Rizzo 
14951a26580eSLuigi Rizzo 		na = buf;
14961a26580eSLuigi Rizzo 		if (na->nm_lock == NULL)
14971a26580eSLuigi Rizzo 			na->nm_lock = netmap_lock_wrapper;
14981a26580eSLuigi Rizzo 		mtx_init(&na->core_lock, "netmap core lock", NULL, MTX_DEF);
149964ae02c3SLuigi Rizzo 		for (i = 0 ; i < na->num_tx_queues + 1; i++)
15001a26580eSLuigi Rizzo 			mtx_init(&na->tx_rings[i].q_lock, "netmap txq lock", NULL, MTX_DEF);
150164ae02c3SLuigi Rizzo 		for (i = 0 ; i < na->num_rx_queues + 1; i++)
15021a26580eSLuigi Rizzo 			mtx_init(&na->rx_rings[i].q_lock, "netmap rxq lock", NULL, MTX_DEF);
150368b8534bSLuigi Rizzo 	}
150464ae02c3SLuigi Rizzo #ifdef linux
150564ae02c3SLuigi Rizzo 	D("netdev_ops %p", ifp->netdev_ops);
150664ae02c3SLuigi Rizzo 	/* prepare a clone of the netdev ops */
150764ae02c3SLuigi Rizzo 	na->nm_ndo = *ifp->netdev_ops;
150864ae02c3SLuigi Rizzo 	na->nm_ndo.ndo_start_xmit = netmap_start_linux;
150964ae02c3SLuigi Rizzo #endif
151068b8534bSLuigi Rizzo 	D("%s for %s", buf ? "ok" : "failed", ifp->if_xname);
151168b8534bSLuigi Rizzo 
151268b8534bSLuigi Rizzo 	return (buf ? 0 : ENOMEM);
151368b8534bSLuigi Rizzo }
151468b8534bSLuigi Rizzo 
151568b8534bSLuigi Rizzo 
151668b8534bSLuigi Rizzo /*
151768b8534bSLuigi Rizzo  * Free the allocated memory linked to the given ``netmap_adapter``
151868b8534bSLuigi Rizzo  * object.
151968b8534bSLuigi Rizzo  */
152068b8534bSLuigi Rizzo void
152168b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp)
152268b8534bSLuigi Rizzo {
152368b8534bSLuigi Rizzo 	u_int i;
152468b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
152568b8534bSLuigi Rizzo 
152668b8534bSLuigi Rizzo 	if (!na)
152768b8534bSLuigi Rizzo 		return;
152868b8534bSLuigi Rizzo 
152964ae02c3SLuigi Rizzo 	for (i = 0; i < na->num_tx_queues + 1; i++) {
153068b8534bSLuigi Rizzo 		knlist_destroy(&na->tx_rings[i].si.si_note);
153164ae02c3SLuigi Rizzo 		mtx_destroy(&na->tx_rings[i].q_lock);
153268b8534bSLuigi Rizzo 	}
153364ae02c3SLuigi Rizzo 	for (i = 0; i < na->num_rx_queues + 1; i++) {
153464ae02c3SLuigi Rizzo 		knlist_destroy(&na->rx_rings[i].si.si_note);
153564ae02c3SLuigi Rizzo 		mtx_destroy(&na->rx_rings[i].q_lock);
153664ae02c3SLuigi Rizzo 	}
153764ae02c3SLuigi Rizzo 	knlist_destroy(&na->tx_si.si_note);
153864ae02c3SLuigi Rizzo 	knlist_destroy(&na->rx_si.si_note);
153968b8534bSLuigi Rizzo 	bzero(na, sizeof(*na));
1540d0c7b075SLuigi Rizzo 	WNA(ifp) = NULL;
154168b8534bSLuigi Rizzo 	free(na, M_DEVBUF);
154268b8534bSLuigi Rizzo }
154368b8534bSLuigi Rizzo 
154468b8534bSLuigi Rizzo 
154568b8534bSLuigi Rizzo /*
154602ad4083SLuigi Rizzo  * Intercept packets from the network stack and pass them
154702ad4083SLuigi Rizzo  * to netmap as incoming packets on the 'software' ring.
154868b8534bSLuigi Rizzo  * We are not locked when called.
154968b8534bSLuigi Rizzo  */
155068b8534bSLuigi Rizzo int
155168b8534bSLuigi Rizzo netmap_start(struct ifnet *ifp, struct mbuf *m)
155268b8534bSLuigi Rizzo {
155368b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
155464ae02c3SLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_queues];
15551a26580eSLuigi Rizzo 	u_int i, len = MBUF_LEN(m);
155602ad4083SLuigi Rizzo 	int error = EBUSY, lim = kring->nkr_num_slots - 1;
155768b8534bSLuigi Rizzo 	struct netmap_slot *slot;
155868b8534bSLuigi Rizzo 
155968b8534bSLuigi Rizzo 	if (netmap_verbose & NM_VERB_HOST)
156068b8534bSLuigi Rizzo 		D("%s packet %d len %d from the stack", ifp->if_xname,
156168b8534bSLuigi Rizzo 			kring->nr_hwcur + kring->nr_hwavail, len);
15621a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_CORE_LOCK, 0);
156302ad4083SLuigi Rizzo 	if (kring->nr_hwavail >= lim) {
156468b8534bSLuigi Rizzo 		D("stack ring %s full\n", ifp->if_xname);
156568b8534bSLuigi Rizzo 		goto done;	/* no space */
156668b8534bSLuigi Rizzo 	}
156764ae02c3SLuigi Rizzo 	if (len > NETMAP_BUF_SIZE) {
156864ae02c3SLuigi Rizzo 		D("drop packet size %d > %d", len, NETMAP_BUF_SIZE);
156968b8534bSLuigi Rizzo 		goto done;	/* too long for us */
157068b8534bSLuigi Rizzo 	}
157168b8534bSLuigi Rizzo 
157268b8534bSLuigi Rizzo 	/* compute the insert position */
157368b8534bSLuigi Rizzo 	i = kring->nr_hwcur + kring->nr_hwavail;
157402ad4083SLuigi Rizzo 	if (i > lim)
157502ad4083SLuigi Rizzo 		i -= lim + 1;
157668b8534bSLuigi Rizzo 	slot = &kring->ring->slot[i];
157768b8534bSLuigi Rizzo 	m_copydata(m, 0, len, NMB(slot));
157868b8534bSLuigi Rizzo 	slot->len = len;
157968b8534bSLuigi Rizzo 	kring->nr_hwavail++;
158068b8534bSLuigi Rizzo 	if (netmap_verbose  & NM_VERB_HOST)
158164ae02c3SLuigi Rizzo 		D("wake up host ring %s %d", na->ifp->if_xname, na->num_rx_queues);
158268b8534bSLuigi Rizzo 	selwakeuppri(&kring->si, PI_NET);
158368b8534bSLuigi Rizzo 	error = 0;
158468b8534bSLuigi Rizzo done:
15851a26580eSLuigi Rizzo 	na->nm_lock(ifp, NETMAP_CORE_UNLOCK, 0);
158668b8534bSLuigi Rizzo 
158768b8534bSLuigi Rizzo 	/* release the mbuf in either cases of success or failure. As an
158868b8534bSLuigi Rizzo 	 * alternative, put the mbuf in a free list and free the list
158968b8534bSLuigi Rizzo 	 * only when really necessary.
159068b8534bSLuigi Rizzo 	 */
159168b8534bSLuigi Rizzo 	m_freem(m);
159268b8534bSLuigi Rizzo 
159368b8534bSLuigi Rizzo 	return (error);
159468b8534bSLuigi Rizzo }
159568b8534bSLuigi Rizzo 
159668b8534bSLuigi Rizzo 
159768b8534bSLuigi Rizzo /*
159868b8534bSLuigi Rizzo  * netmap_reset() is called by the driver routines when reinitializing
159968b8534bSLuigi Rizzo  * a ring. The driver is in charge of locking to protect the kring.
160068b8534bSLuigi Rizzo  * If netmap mode is not set just return NULL.
160168b8534bSLuigi Rizzo  */
160268b8534bSLuigi Rizzo struct netmap_slot *
160368b8534bSLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, int n,
160468b8534bSLuigi Rizzo 	u_int new_cur)
160568b8534bSLuigi Rizzo {
160668b8534bSLuigi Rizzo 	struct netmap_kring *kring;
1607506cc70cSLuigi Rizzo 	int new_hwofs, lim;
160868b8534bSLuigi Rizzo 
160968b8534bSLuigi Rizzo 	if (na == NULL)
161068b8534bSLuigi Rizzo 		return NULL;	/* no netmap support here */
161168b8534bSLuigi Rizzo 	if (!(na->ifp->if_capenable & IFCAP_NETMAP))
161268b8534bSLuigi Rizzo 		return NULL;	/* nothing to reinitialize */
161368b8534bSLuigi Rizzo 
161464ae02c3SLuigi Rizzo 	if (tx == NR_TX) {
161564ae02c3SLuigi Rizzo 		kring = na->tx_rings + n;
1616506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur - new_cur;
161764ae02c3SLuigi Rizzo 	} else {
161864ae02c3SLuigi Rizzo 		kring = na->rx_rings + n;
1619506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur + kring->nr_hwavail - new_cur;
162064ae02c3SLuigi Rizzo 	}
162164ae02c3SLuigi Rizzo 	lim = kring->nkr_num_slots - 1;
1622506cc70cSLuigi Rizzo 	if (new_hwofs > lim)
1623506cc70cSLuigi Rizzo 		new_hwofs -= lim + 1;
1624506cc70cSLuigi Rizzo 
1625506cc70cSLuigi Rizzo 	/* Alwayws set the new offset value and realign the ring. */
1626506cc70cSLuigi Rizzo 	kring->nkr_hwofs = new_hwofs;
1627506cc70cSLuigi Rizzo 	if (tx == NR_TX)
1628506cc70cSLuigi Rizzo 		kring->nr_hwavail = kring->nkr_num_slots - 1;
1629506cc70cSLuigi Rizzo 	D("new hwofs %d on %s %s[%d]",
1630506cc70cSLuigi Rizzo 			kring->nkr_hwofs, na->ifp->if_xname,
1631506cc70cSLuigi Rizzo 			tx == NR_TX ? "TX" : "RX", n);
1632506cc70cSLuigi Rizzo 
163368b8534bSLuigi Rizzo 	/*
163464ae02c3SLuigi Rizzo 	 * Wakeup on the individual and global lock
1635506cc70cSLuigi Rizzo 	 * We do the wakeup here, but the ring is not yet reconfigured.
1636506cc70cSLuigi Rizzo 	 * However, we are under lock so there are no races.
163768b8534bSLuigi Rizzo 	 */
163868b8534bSLuigi Rizzo 	selwakeuppri(&kring->si, PI_NET);
163964ae02c3SLuigi Rizzo 	selwakeuppri(tx == NR_TX ? &na->tx_si : &na->rx_si, PI_NET);
164068b8534bSLuigi Rizzo 	return kring->ring->slot;
164168b8534bSLuigi Rizzo }
164268b8534bSLuigi Rizzo 
164368b8534bSLuigi Rizzo 
164468b8534bSLuigi Rizzo /*
16451a26580eSLuigi Rizzo  * Default functions to handle rx/tx interrupts
16461a26580eSLuigi Rizzo  * we have 4 cases:
16471a26580eSLuigi Rizzo  * 1 ring, single lock:
16481a26580eSLuigi Rizzo  *     lock(core); wake(i=0); unlock(core)
16491a26580eSLuigi Rizzo  * N rings, single lock:
16501a26580eSLuigi Rizzo  *     lock(core); wake(i); wake(N+1) unlock(core)
16511a26580eSLuigi Rizzo  * 1 ring, separate locks: (i=0)
16521a26580eSLuigi Rizzo  *     lock(i); wake(i); unlock(i)
16531a26580eSLuigi Rizzo  * N rings, separate locks:
16541a26580eSLuigi Rizzo  *     lock(i); wake(i); unlock(i); lock(core) wake(N+1) unlock(core)
165564ae02c3SLuigi Rizzo  * work_done is non-null on the RX path.
16561a26580eSLuigi Rizzo  */
1657babc7c12SLuigi Rizzo int
1658babc7c12SLuigi Rizzo netmap_rx_irq(struct ifnet *ifp, int q, int *work_done)
16591a26580eSLuigi Rizzo {
16601a26580eSLuigi Rizzo 	struct netmap_adapter *na;
16611a26580eSLuigi Rizzo 	struct netmap_kring *r;
166264ae02c3SLuigi Rizzo 	NM_SELINFO_T *main_wq;
16631a26580eSLuigi Rizzo 
16641a26580eSLuigi Rizzo 	if (!(ifp->if_capenable & IFCAP_NETMAP))
16651a26580eSLuigi Rizzo 		return 0;
16661a26580eSLuigi Rizzo 	na = NA(ifp);
166764ae02c3SLuigi Rizzo 	if (work_done) { /* RX path */
166864ae02c3SLuigi Rizzo 		r = na->rx_rings + q;
166964ae02c3SLuigi Rizzo 		r->nr_kflags |= NKR_PENDINTR;
167064ae02c3SLuigi Rizzo 		main_wq = (na->num_rx_queues > 1) ? &na->tx_si : NULL;
167164ae02c3SLuigi Rizzo 	} else { /* tx path */
167264ae02c3SLuigi Rizzo 		r = na->tx_rings + q;
167364ae02c3SLuigi Rizzo 		main_wq = (na->num_tx_queues > 1) ? &na->rx_si : NULL;
167464ae02c3SLuigi Rizzo 		work_done = &q; /* dummy */
167564ae02c3SLuigi Rizzo 	}
16761a26580eSLuigi Rizzo 	if (na->separate_locks) {
167764ae02c3SLuigi Rizzo 		mtx_lock(&r->q_lock);
167864ae02c3SLuigi Rizzo 		selwakeuppri(&r->si, PI_NET);
167964ae02c3SLuigi Rizzo 		mtx_unlock(&r->q_lock);
168064ae02c3SLuigi Rizzo 		if (main_wq) {
16811a26580eSLuigi Rizzo 			mtx_lock(&na->core_lock);
168264ae02c3SLuigi Rizzo 			selwakeuppri(main_wq, PI_NET);
16831a26580eSLuigi Rizzo 			mtx_unlock(&na->core_lock);
16841a26580eSLuigi Rizzo 		}
16851a26580eSLuigi Rizzo 	} else {
16861a26580eSLuigi Rizzo 		mtx_lock(&na->core_lock);
168764ae02c3SLuigi Rizzo 		selwakeuppri(&r->si, PI_NET);
168864ae02c3SLuigi Rizzo 		if (main_wq)
168964ae02c3SLuigi Rizzo 			selwakeuppri(main_wq, PI_NET);
16901a26580eSLuigi Rizzo 		mtx_unlock(&na->core_lock);
16911a26580eSLuigi Rizzo 	}
16921a26580eSLuigi Rizzo 		*work_done = 1; /* do not fire napi again */
16931a26580eSLuigi Rizzo 	return 1;
16941a26580eSLuigi Rizzo }
16951a26580eSLuigi Rizzo 
169664ae02c3SLuigi Rizzo 
1697babc7c12SLuigi Rizzo static struct cdevsw netmap_cdevsw = {
1698babc7c12SLuigi Rizzo 	.d_version = D_VERSION,
1699babc7c12SLuigi Rizzo 	.d_name = "netmap",
1700babc7c12SLuigi Rizzo 	.d_mmap = netmap_mmap,
1701babc7c12SLuigi Rizzo 	.d_ioctl = netmap_ioctl,
1702babc7c12SLuigi Rizzo 	.d_poll = netmap_poll,
1703babc7c12SLuigi Rizzo };
1704babc7c12SLuigi Rizzo 
1705babc7c12SLuigi Rizzo 
1706babc7c12SLuigi Rizzo static struct cdev *netmap_dev; /* /dev/netmap character device. */
1707babc7c12SLuigi Rizzo 
1708babc7c12SLuigi Rizzo 
17091a26580eSLuigi Rizzo /*
171068b8534bSLuigi Rizzo  * Module loader.
171168b8534bSLuigi Rizzo  *
171268b8534bSLuigi Rizzo  * Create the /dev/netmap device and initialize all global
171368b8534bSLuigi Rizzo  * variables.
171468b8534bSLuigi Rizzo  *
171568b8534bSLuigi Rizzo  * Return 0 on success, errno on failure.
171668b8534bSLuigi Rizzo  */
171768b8534bSLuigi Rizzo static int
171868b8534bSLuigi Rizzo netmap_init(void)
171968b8534bSLuigi Rizzo {
172068b8534bSLuigi Rizzo 	int error;
172168b8534bSLuigi Rizzo 
172268b8534bSLuigi Rizzo 	error = netmap_memory_init();
172368b8534bSLuigi Rizzo 	if (error != 0) {
172468b8534bSLuigi Rizzo 		printf("netmap: unable to initialize the memory allocator.");
172568b8534bSLuigi Rizzo 		return (error);
172668b8534bSLuigi Rizzo 	}
172768b8534bSLuigi Rizzo 	printf("netmap: loaded module with %d Mbytes\n",
172864ae02c3SLuigi Rizzo 		(int)(nm_mem->nm_totalsize >> 20));
172968b8534bSLuigi Rizzo 	netmap_dev = make_dev(&netmap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0660,
173068b8534bSLuigi Rizzo 			      "netmap");
1731babc7c12SLuigi Rizzo 	return (error);
173268b8534bSLuigi Rizzo }
173368b8534bSLuigi Rizzo 
173468b8534bSLuigi Rizzo 
173568b8534bSLuigi Rizzo /*
173668b8534bSLuigi Rizzo  * Module unloader.
173768b8534bSLuigi Rizzo  *
173868b8534bSLuigi Rizzo  * Free all the memory, and destroy the ``/dev/netmap`` device.
173968b8534bSLuigi Rizzo  */
174068b8534bSLuigi Rizzo static void
174168b8534bSLuigi Rizzo netmap_fini(void)
174268b8534bSLuigi Rizzo {
174368b8534bSLuigi Rizzo 	destroy_dev(netmap_dev);
174468b8534bSLuigi Rizzo 	netmap_memory_fini();
174568b8534bSLuigi Rizzo 	printf("netmap: unloaded module.\n");
174668b8534bSLuigi Rizzo }
174768b8534bSLuigi Rizzo 
174868b8534bSLuigi Rizzo 
174968b8534bSLuigi Rizzo /*
175068b8534bSLuigi Rizzo  * Kernel entry point.
175168b8534bSLuigi Rizzo  *
175268b8534bSLuigi Rizzo  * Initialize/finalize the module and return.
175368b8534bSLuigi Rizzo  *
175468b8534bSLuigi Rizzo  * Return 0 on success, errno on failure.
175568b8534bSLuigi Rizzo  */
175668b8534bSLuigi Rizzo static int
175768b8534bSLuigi Rizzo netmap_loader(__unused struct module *module, int event, __unused void *arg)
175868b8534bSLuigi Rizzo {
175968b8534bSLuigi Rizzo 	int error = 0;
176068b8534bSLuigi Rizzo 
176168b8534bSLuigi Rizzo 	switch (event) {
176268b8534bSLuigi Rizzo 	case MOD_LOAD:
176368b8534bSLuigi Rizzo 		error = netmap_init();
176468b8534bSLuigi Rizzo 		break;
176568b8534bSLuigi Rizzo 
176668b8534bSLuigi Rizzo 	case MOD_UNLOAD:
176768b8534bSLuigi Rizzo 		netmap_fini();
176868b8534bSLuigi Rizzo 		break;
176968b8534bSLuigi Rizzo 
177068b8534bSLuigi Rizzo 	default:
177168b8534bSLuigi Rizzo 		error = EOPNOTSUPP;
177268b8534bSLuigi Rizzo 		break;
177368b8534bSLuigi Rizzo 	}
177468b8534bSLuigi Rizzo 
177568b8534bSLuigi Rizzo 	return (error);
177668b8534bSLuigi Rizzo }
177768b8534bSLuigi Rizzo 
177868b8534bSLuigi Rizzo 
177968b8534bSLuigi Rizzo DEV_MODULE(netmap, netmap_loader, NULL);
1780