xref: /freebsd-14.2/sys/dev/netmap/netmap.c (revision f9790aeb)
168b8534bSLuigi Rizzo /*
2849bec0eSLuigi Rizzo  * Copyright (C) 2011-2013 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 
26ce3ee1e7SLuigi Rizzo 
2768b8534bSLuigi Rizzo /*
28*f9790aebSLuigi Rizzo  * $FreeBSD$
29*f9790aebSLuigi Rizzo  *
3068b8534bSLuigi Rizzo  * This module supports memory mapped access to network devices,
3168b8534bSLuigi Rizzo  * see netmap(4).
3268b8534bSLuigi Rizzo  *
3368b8534bSLuigi Rizzo  * The module uses a large, memory pool allocated by the kernel
3468b8534bSLuigi Rizzo  * and accessible as mmapped memory by multiple userspace threads/processes.
3568b8534bSLuigi Rizzo  * The memory pool contains packet buffers and "netmap rings",
3668b8534bSLuigi Rizzo  * i.e. user-accessible copies of the interface's queues.
3768b8534bSLuigi Rizzo  *
3868b8534bSLuigi Rizzo  * Access to the network card works like this:
3968b8534bSLuigi Rizzo  * 1. a process/thread issues one or more open() on /dev/netmap, to create
4068b8534bSLuigi Rizzo  *    select()able file descriptor on which events are reported.
4168b8534bSLuigi Rizzo  * 2. on each descriptor, the process issues an ioctl() to identify
4268b8534bSLuigi Rizzo  *    the interface that should report events to the file descriptor.
4368b8534bSLuigi Rizzo  * 3. on each descriptor, the process issues an mmap() request to
4468b8534bSLuigi Rizzo  *    map the shared memory region within the process' address space.
4568b8534bSLuigi Rizzo  *    The list of interesting queues is indicated by a location in
4668b8534bSLuigi Rizzo  *    the shared memory region.
4768b8534bSLuigi Rizzo  * 4. using the functions in the netmap(4) userspace API, a process
4868b8534bSLuigi Rizzo  *    can look up the occupation state of a queue, access memory buffers,
4968b8534bSLuigi Rizzo  *    and retrieve received packets or enqueue packets to transmit.
5068b8534bSLuigi Rizzo  * 5. using some ioctl()s the process can synchronize the userspace view
5168b8534bSLuigi Rizzo  *    of the queue with the actual status in the kernel. This includes both
5268b8534bSLuigi Rizzo  *    receiving the notification of new packets, and transmitting new
5368b8534bSLuigi Rizzo  *    packets on the output interface.
5468b8534bSLuigi Rizzo  * 6. select() or poll() can be used to wait for events on individual
5568b8534bSLuigi Rizzo  *    transmit or receive queues (or all queues for a given interface).
56ce3ee1e7SLuigi Rizzo  *
57ce3ee1e7SLuigi Rizzo 
58ce3ee1e7SLuigi Rizzo 		SYNCHRONIZATION (USER)
59ce3ee1e7SLuigi Rizzo 
60ce3ee1e7SLuigi Rizzo The netmap rings and data structures may be shared among multiple
61ce3ee1e7SLuigi Rizzo user threads or even independent processes.
62ce3ee1e7SLuigi Rizzo Any synchronization among those threads/processes is delegated
63ce3ee1e7SLuigi Rizzo to the threads themselves. Only one thread at a time can be in
64ce3ee1e7SLuigi Rizzo a system call on the same netmap ring. The OS does not enforce
65ce3ee1e7SLuigi Rizzo this and only guarantees against system crashes in case of
66ce3ee1e7SLuigi Rizzo invalid usage.
67ce3ee1e7SLuigi Rizzo 
68ce3ee1e7SLuigi Rizzo 		LOCKING (INTERNAL)
69ce3ee1e7SLuigi Rizzo 
70ce3ee1e7SLuigi Rizzo Within the kernel, access to the netmap rings is protected as follows:
71ce3ee1e7SLuigi Rizzo 
72ce3ee1e7SLuigi Rizzo - a spinlock on each ring, to handle producer/consumer races on
73ce3ee1e7SLuigi Rizzo   RX rings attached to the host stack (against multiple host
74ce3ee1e7SLuigi Rizzo   threads writing from the host stack to the same ring),
75ce3ee1e7SLuigi Rizzo   and on 'destination' rings attached to a VALE switch
76ce3ee1e7SLuigi Rizzo   (i.e. RX rings in VALE ports, and TX rings in NIC/host ports)
77ce3ee1e7SLuigi Rizzo   protecting multiple active senders for the same destination)
78ce3ee1e7SLuigi Rizzo 
79ce3ee1e7SLuigi Rizzo - an atomic variable to guarantee that there is at most one
80ce3ee1e7SLuigi Rizzo   instance of *_*xsync() on the ring at any time.
81ce3ee1e7SLuigi Rizzo   For rings connected to user file
82ce3ee1e7SLuigi Rizzo   descriptors, an atomic_test_and_set() protects this, and the
83ce3ee1e7SLuigi Rizzo   lock on the ring is not actually used.
84ce3ee1e7SLuigi Rizzo   For NIC RX rings connected to a VALE switch, an atomic_test_and_set()
85ce3ee1e7SLuigi Rizzo   is also used to prevent multiple executions (the driver might indeed
86ce3ee1e7SLuigi Rizzo   already guarantee this).
87ce3ee1e7SLuigi Rizzo   For NIC TX rings connected to a VALE switch, the lock arbitrates
88ce3ee1e7SLuigi Rizzo   access to the queue (both when allocating buffers and when pushing
89ce3ee1e7SLuigi Rizzo   them out).
90ce3ee1e7SLuigi Rizzo 
91ce3ee1e7SLuigi Rizzo - *xsync() should be protected against initializations of the card.
92ce3ee1e7SLuigi Rizzo   On FreeBSD most devices have the reset routine protected by
93ce3ee1e7SLuigi Rizzo   a RING lock (ixgbe, igb, em) or core lock (re). lem is missing
94ce3ee1e7SLuigi Rizzo   the RING protection on rx_reset(), this should be added.
95ce3ee1e7SLuigi Rizzo 
96ce3ee1e7SLuigi Rizzo   On linux there is an external lock on the tx path, which probably
97ce3ee1e7SLuigi Rizzo   also arbitrates access to the reset routine. XXX to be revised
98ce3ee1e7SLuigi Rizzo 
99ce3ee1e7SLuigi Rizzo - a per-interface core_lock protecting access from the host stack
100ce3ee1e7SLuigi Rizzo   while interfaces may be detached from netmap mode.
101ce3ee1e7SLuigi Rizzo   XXX there should be no need for this lock if we detach the interfaces
102ce3ee1e7SLuigi Rizzo   only while they are down.
103ce3ee1e7SLuigi Rizzo 
104ce3ee1e7SLuigi Rizzo 
105ce3ee1e7SLuigi Rizzo --- VALE SWITCH ---
106ce3ee1e7SLuigi Rizzo 
107ce3ee1e7SLuigi Rizzo NMG_LOCK() serializes all modifications to switches and ports.
108ce3ee1e7SLuigi Rizzo A switch cannot be deleted until all ports are gone.
109ce3ee1e7SLuigi Rizzo 
110ce3ee1e7SLuigi Rizzo For each switch, an SX lock (RWlock on linux) protects
111ce3ee1e7SLuigi Rizzo deletion of ports. When configuring or deleting a new port, the
112ce3ee1e7SLuigi Rizzo lock is acquired in exclusive mode (after holding NMG_LOCK).
113ce3ee1e7SLuigi Rizzo When forwarding, the lock is acquired in shared mode (without NMG_LOCK).
114ce3ee1e7SLuigi Rizzo The lock is held throughout the entire forwarding cycle,
115ce3ee1e7SLuigi Rizzo during which the thread may incur in a page fault.
116ce3ee1e7SLuigi Rizzo Hence it is important that sleepable shared locks are used.
117ce3ee1e7SLuigi Rizzo 
118ce3ee1e7SLuigi Rizzo On the rx ring, the per-port lock is grabbed initially to reserve
119ce3ee1e7SLuigi Rizzo a number of slot in the ring, then the lock is released,
120ce3ee1e7SLuigi Rizzo packets are copied from source to destination, and then
121ce3ee1e7SLuigi Rizzo the lock is acquired again and the receive ring is updated.
122ce3ee1e7SLuigi Rizzo (A similar thing is done on the tx ring for NIC and host stack
123ce3ee1e7SLuigi Rizzo ports attached to the switch)
124ce3ee1e7SLuigi Rizzo 
12568b8534bSLuigi Rizzo  */
12668b8534bSLuigi Rizzo 
127ce3ee1e7SLuigi Rizzo /*
128ce3ee1e7SLuigi Rizzo  * OS-specific code that is used only within this file.
129ce3ee1e7SLuigi Rizzo  * Other OS-specific code that must be accessed by drivers
130ce3ee1e7SLuigi Rizzo  * is present in netmap_kern.h
131ce3ee1e7SLuigi Rizzo  */
13201c7d25fSLuigi Rizzo 
133ce3ee1e7SLuigi Rizzo #if defined(__FreeBSD__)
13468b8534bSLuigi Rizzo #include <sys/cdefs.h> /* prerequisite */
13568b8534bSLuigi Rizzo #include <sys/types.h>
13668b8534bSLuigi Rizzo #include <sys/errno.h>
13768b8534bSLuigi Rizzo #include <sys/param.h>	/* defines used in kernel.h */
13868b8534bSLuigi Rizzo #include <sys/kernel.h>	/* types used in module initialization */
139*f9790aebSLuigi Rizzo #include <sys/conf.h>	/* cdevsw struct, UID, GID */
14068b8534bSLuigi Rizzo #include <sys/sockio.h>
14168b8534bSLuigi Rizzo #include <sys/socketvar.h>	/* struct socket */
14268b8534bSLuigi Rizzo #include <sys/malloc.h>
14368b8534bSLuigi Rizzo #include <sys/poll.h>
14489f6b863SAttilio Rao #include <sys/rwlock.h>
14568b8534bSLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
14668b8534bSLuigi Rizzo #include <sys/selinfo.h>
14768b8534bSLuigi Rizzo #include <sys/sysctl.h>
14868b8534bSLuigi Rizzo #include <net/if.h>
14976039bc8SGleb Smirnoff #include <net/if_var.h>
15068b8534bSLuigi Rizzo #include <net/bpf.h>		/* BIOCIMMEDIATE */
15168b8534bSLuigi Rizzo #include <machine/bus.h>	/* bus_dmamap_* */
152ce3ee1e7SLuigi Rizzo #include <sys/endian.h>
153ce3ee1e7SLuigi Rizzo #include <sys/refcount.h>
15468b8534bSLuigi Rizzo 
15568b8534bSLuigi Rizzo 
156*f9790aebSLuigi Rizzo /* reduce conditional code */
157*f9790aebSLuigi Rizzo #define init_waitqueue_head(x)	// only needed in linux
158ce3ee1e7SLuigi Rizzo 
159ce3ee1e7SLuigi Rizzo 
160ce3ee1e7SLuigi Rizzo 
161ce3ee1e7SLuigi Rizzo #elif defined(linux)
162ce3ee1e7SLuigi Rizzo 
163ce3ee1e7SLuigi Rizzo #include "bsd_glue.h"
164ce3ee1e7SLuigi Rizzo 
165ce3ee1e7SLuigi Rizzo 
166ce3ee1e7SLuigi Rizzo 
167ce3ee1e7SLuigi Rizzo #elif defined(__APPLE__)
168ce3ee1e7SLuigi Rizzo 
169ce3ee1e7SLuigi Rizzo #warning OSX support is only partial
170ce3ee1e7SLuigi Rizzo #include "osx_glue.h"
171ce3ee1e7SLuigi Rizzo 
172ce3ee1e7SLuigi Rizzo #else
173ce3ee1e7SLuigi Rizzo 
174ce3ee1e7SLuigi Rizzo #error	Unsupported platform
175ce3ee1e7SLuigi Rizzo 
176ce3ee1e7SLuigi Rizzo #endif /* unsupported */
177ce3ee1e7SLuigi Rizzo 
178ce3ee1e7SLuigi Rizzo /*
179ce3ee1e7SLuigi Rizzo  * common headers
180ce3ee1e7SLuigi Rizzo  */
1810b8ed8e0SLuigi Rizzo #include <net/netmap.h>
1820b8ed8e0SLuigi Rizzo #include <dev/netmap/netmap_kern.h>
183ce3ee1e7SLuigi Rizzo #include <dev/netmap/netmap_mem2.h>
1840b8ed8e0SLuigi Rizzo 
185ce3ee1e7SLuigi Rizzo 
186ce3ee1e7SLuigi Rizzo MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map");
187ce3ee1e7SLuigi Rizzo 
188ce3ee1e7SLuigi Rizzo /*
189ce3ee1e7SLuigi Rizzo  * The following variables are used by the drivers and replicate
190ce3ee1e7SLuigi Rizzo  * fields in the global memory pool. They only refer to buffers
191ce3ee1e7SLuigi Rizzo  * used by physical interfaces.
192ce3ee1e7SLuigi Rizzo  */
1935819da83SLuigi Rizzo u_int netmap_total_buffers;
1948241616dSLuigi Rizzo u_int netmap_buf_size;
195ce3ee1e7SLuigi Rizzo char *netmap_buffer_base;	/* also address of an invalid buffer */
1965819da83SLuigi Rizzo 
1975819da83SLuigi Rizzo /* user-controlled variables */
1985819da83SLuigi Rizzo int netmap_verbose;
1995819da83SLuigi Rizzo 
2005819da83SLuigi Rizzo static int netmap_no_timestamp; /* don't timestamp on rxsync */
2015819da83SLuigi Rizzo 
2025819da83SLuigi Rizzo SYSCTL_NODE(_dev, OID_AUTO, netmap, CTLFLAG_RW, 0, "Netmap args");
2035819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, verbose,
2045819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_verbose, 0, "Verbose mode");
2055819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_timestamp,
2065819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_timestamp, 0, "no_timestamp");
2075819da83SLuigi Rizzo int netmap_mitigate = 1;
2085819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mitigate, CTLFLAG_RW, &netmap_mitigate, 0, "");
209c85cb1a0SLuigi Rizzo int netmap_no_pendintr = 1;
2105819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_pendintr,
2115819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_pendintr, 0, "Always look for new received packets.");
212f18be576SLuigi Rizzo int netmap_txsync_retry = 2;
213f18be576SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, txsync_retry, CTLFLAG_RW,
214f18be576SLuigi Rizzo     &netmap_txsync_retry, 0 , "Number of txsync loops in bridge's flush.");
2155819da83SLuigi Rizzo 
216f196ce38SLuigi Rizzo int netmap_flags = 0;	/* debug flags */
217091fd0abSLuigi Rizzo int netmap_fwd = 0;	/* force transparent mode */
218ce3ee1e7SLuigi Rizzo int netmap_mmap_unreg = 0; /* allow mmap of unregistered fds */
219f196ce38SLuigi Rizzo 
220*f9790aebSLuigi Rizzo /*
221*f9790aebSLuigi Rizzo  * netmap_admode selects the netmap mode to use.
222*f9790aebSLuigi Rizzo  * Invalid values are reset to NETMAP_ADMODE_BEST
223*f9790aebSLuigi Rizzo  */
224*f9790aebSLuigi Rizzo enum { NETMAP_ADMODE_BEST = 0,	/* use native, fallback to generic */
225*f9790aebSLuigi Rizzo 	NETMAP_ADMODE_NATIVE,	/* either native or none */
226*f9790aebSLuigi Rizzo 	NETMAP_ADMODE_GENERIC,	/* force generic */
227*f9790aebSLuigi Rizzo 	NETMAP_ADMODE_LAST };
228*f9790aebSLuigi Rizzo #define NETMAP_ADMODE_NATIVE        1  /* Force native netmap adapter. */
229*f9790aebSLuigi Rizzo #define NETMAP_ADMODE_GENERIC       2  /* Force generic netmap adapter. */
230*f9790aebSLuigi Rizzo #define NETMAP_ADMODE_BEST          0  /* Priority to native netmap adapter. */
231*f9790aebSLuigi Rizzo static int netmap_admode = NETMAP_ADMODE_BEST;
232*f9790aebSLuigi Rizzo 
233*f9790aebSLuigi Rizzo int netmap_generic_mit = 100*1000;   /* Generic mitigation interval in nanoseconds. */
234*f9790aebSLuigi Rizzo int netmap_generic_ringsize = 1024;   /* Generic ringsize. */
235*f9790aebSLuigi Rizzo 
236f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, flags, CTLFLAG_RW, &netmap_flags, 0 , "");
237091fd0abSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, fwd, CTLFLAG_RW, &netmap_fwd, 0 , "");
238ce3ee1e7SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mmap_unreg, CTLFLAG_RW, &netmap_mmap_unreg, 0, "");
239*f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, admode, CTLFLAG_RW, &netmap_admode, 0 , "");
240*f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, generic_mit, CTLFLAG_RW, &netmap_generic_mit, 0 , "");
241*f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, generic_ringsize, CTLFLAG_RW, &netmap_generic_ringsize, 0 , "");
242f196ce38SLuigi Rizzo 
243ce3ee1e7SLuigi Rizzo NMG_LOCK_T	netmap_global_lock;
244ce3ee1e7SLuigi Rizzo 
245ce3ee1e7SLuigi Rizzo 
246*f9790aebSLuigi Rizzo static void
247*f9790aebSLuigi Rizzo nm_kr_get(struct netmap_kring *kr)
248ce3ee1e7SLuigi Rizzo {
249ce3ee1e7SLuigi Rizzo 	while (NM_ATOMIC_TEST_AND_SET(&kr->nr_busy))
250ce3ee1e7SLuigi Rizzo 		tsleep(kr, 0, "NM_KR_GET", 4);
251ce3ee1e7SLuigi Rizzo }
252ce3ee1e7SLuigi Rizzo 
253*f9790aebSLuigi Rizzo 
254*f9790aebSLuigi Rizzo void
255*f9790aebSLuigi Rizzo netmap_disable_ring(struct netmap_kring *kr)
256ce3ee1e7SLuigi Rizzo {
257ce3ee1e7SLuigi Rizzo 	kr->nkr_stopped = 1;
258ce3ee1e7SLuigi Rizzo 	nm_kr_get(kr);
259ce3ee1e7SLuigi Rizzo 	mtx_lock(&kr->q_lock);
260ce3ee1e7SLuigi Rizzo 	mtx_unlock(&kr->q_lock);
261ce3ee1e7SLuigi Rizzo 	nm_kr_put(kr);
262ce3ee1e7SLuigi Rizzo }
263ce3ee1e7SLuigi Rizzo 
264*f9790aebSLuigi Rizzo 
265*f9790aebSLuigi Rizzo static void
266*f9790aebSLuigi Rizzo netmap_set_all_rings(struct ifnet *ifp, int stopped)
267ce3ee1e7SLuigi Rizzo {
268ce3ee1e7SLuigi Rizzo 	struct netmap_adapter *na;
269ce3ee1e7SLuigi Rizzo 	int i;
270ce3ee1e7SLuigi Rizzo 
271ce3ee1e7SLuigi Rizzo 	if (!(ifp->if_capenable & IFCAP_NETMAP))
272ce3ee1e7SLuigi Rizzo 		return;
273ce3ee1e7SLuigi Rizzo 
274ce3ee1e7SLuigi Rizzo 	na = NA(ifp);
275ce3ee1e7SLuigi Rizzo 
276*f9790aebSLuigi Rizzo 	for (i = 0; i <= na->num_tx_rings; i++) {
277*f9790aebSLuigi Rizzo 		if (stopped)
278*f9790aebSLuigi Rizzo 			netmap_disable_ring(na->tx_rings + i);
279*f9790aebSLuigi Rizzo 		else
280ce3ee1e7SLuigi Rizzo 			na->tx_rings[i].nkr_stopped = 0;
281*f9790aebSLuigi Rizzo 		na->nm_notify(na, i, NR_TX, NAF_DISABLE_NOTIFY |
282*f9790aebSLuigi Rizzo 			(i == na->num_tx_rings ? NAF_GLOBAL_NOTIFY: 0));
283ce3ee1e7SLuigi Rizzo 	}
284*f9790aebSLuigi Rizzo 
285*f9790aebSLuigi Rizzo 	for (i = 0; i <= na->num_rx_rings; i++) {
286*f9790aebSLuigi Rizzo 		if (stopped)
287*f9790aebSLuigi Rizzo 			netmap_disable_ring(na->rx_rings + i);
288*f9790aebSLuigi Rizzo 		else
289ce3ee1e7SLuigi Rizzo 			na->rx_rings[i].nkr_stopped = 0;
290*f9790aebSLuigi Rizzo 		na->nm_notify(na, i, NR_RX, NAF_DISABLE_NOTIFY |
291*f9790aebSLuigi Rizzo 			(i == na->num_rx_rings ? NAF_GLOBAL_NOTIFY: 0));
292ce3ee1e7SLuigi Rizzo 	}
293ce3ee1e7SLuigi Rizzo }
294ce3ee1e7SLuigi Rizzo 
295ce3ee1e7SLuigi Rizzo 
296*f9790aebSLuigi Rizzo void
297*f9790aebSLuigi Rizzo netmap_disable_all_rings(struct ifnet *ifp)
298*f9790aebSLuigi Rizzo {
299*f9790aebSLuigi Rizzo 	netmap_set_all_rings(ifp, 1 /* stopped */);
300*f9790aebSLuigi Rizzo }
301*f9790aebSLuigi Rizzo 
302*f9790aebSLuigi Rizzo 
303*f9790aebSLuigi Rizzo void
304*f9790aebSLuigi Rizzo netmap_enable_all_rings(struct ifnet *ifp)
305*f9790aebSLuigi Rizzo {
306*f9790aebSLuigi Rizzo 	netmap_set_all_rings(ifp, 0 /* enabled */);
307*f9790aebSLuigi Rizzo }
308*f9790aebSLuigi Rizzo 
309*f9790aebSLuigi Rizzo 
310ce3ee1e7SLuigi Rizzo /*
311ce3ee1e7SLuigi Rizzo  * generic bound_checking function
312ce3ee1e7SLuigi Rizzo  */
313ce3ee1e7SLuigi Rizzo u_int
314ce3ee1e7SLuigi Rizzo nm_bound_var(u_int *v, u_int dflt, u_int lo, u_int hi, const char *msg)
315ce3ee1e7SLuigi Rizzo {
316ce3ee1e7SLuigi Rizzo 	u_int oldv = *v;
317ce3ee1e7SLuigi Rizzo 	const char *op = NULL;
318ce3ee1e7SLuigi Rizzo 
319ce3ee1e7SLuigi Rizzo 	if (dflt < lo)
320ce3ee1e7SLuigi Rizzo 		dflt = lo;
321ce3ee1e7SLuigi Rizzo 	if (dflt > hi)
322ce3ee1e7SLuigi Rizzo 		dflt = hi;
323ce3ee1e7SLuigi Rizzo 	if (oldv < lo) {
324ce3ee1e7SLuigi Rizzo 		*v = dflt;
325ce3ee1e7SLuigi Rizzo 		op = "Bump";
326ce3ee1e7SLuigi Rizzo 	} else if (oldv > hi) {
327ce3ee1e7SLuigi Rizzo 		*v = hi;
328ce3ee1e7SLuigi Rizzo 		op = "Clamp";
329ce3ee1e7SLuigi Rizzo 	}
330ce3ee1e7SLuigi Rizzo 	if (op && msg)
331ce3ee1e7SLuigi Rizzo 		printf("%s %s to %d (was %d)\n", op, msg, *v, oldv);
332ce3ee1e7SLuigi Rizzo 	return *v;
333ce3ee1e7SLuigi Rizzo }
334ce3ee1e7SLuigi Rizzo 
335*f9790aebSLuigi Rizzo 
336ce3ee1e7SLuigi Rizzo /*
337ce3ee1e7SLuigi Rizzo  * packet-dump function, user-supplied or static buffer.
338ce3ee1e7SLuigi Rizzo  * The destination buffer must be at least 30+4*len
339ce3ee1e7SLuigi Rizzo  */
340ce3ee1e7SLuigi Rizzo const char *
341ce3ee1e7SLuigi Rizzo nm_dump_buf(char *p, int len, int lim, char *dst)
342ce3ee1e7SLuigi Rizzo {
343ce3ee1e7SLuigi Rizzo 	static char _dst[8192];
344ce3ee1e7SLuigi Rizzo 	int i, j, i0;
345ce3ee1e7SLuigi Rizzo 	static char hex[] ="0123456789abcdef";
346ce3ee1e7SLuigi Rizzo 	char *o;	/* output position */
347ce3ee1e7SLuigi Rizzo 
348ce3ee1e7SLuigi Rizzo #define P_HI(x)	hex[((x) & 0xf0)>>4]
349ce3ee1e7SLuigi Rizzo #define P_LO(x)	hex[((x) & 0xf)]
350ce3ee1e7SLuigi Rizzo #define P_C(x)	((x) >= 0x20 && (x) <= 0x7e ? (x) : '.')
351ce3ee1e7SLuigi Rizzo 	if (!dst)
352ce3ee1e7SLuigi Rizzo 		dst = _dst;
353ce3ee1e7SLuigi Rizzo 	if (lim <= 0 || lim > len)
354ce3ee1e7SLuigi Rizzo 		lim = len;
355ce3ee1e7SLuigi Rizzo 	o = dst;
356ce3ee1e7SLuigi Rizzo 	sprintf(o, "buf 0x%p len %d lim %d\n", p, len, lim);
357ce3ee1e7SLuigi Rizzo 	o += strlen(o);
358ce3ee1e7SLuigi Rizzo 	/* hexdump routine */
359ce3ee1e7SLuigi Rizzo 	for (i = 0; i < lim; ) {
360ce3ee1e7SLuigi Rizzo 		sprintf(o, "%5d: ", i);
361ce3ee1e7SLuigi Rizzo 		o += strlen(o);
362ce3ee1e7SLuigi Rizzo 		memset(o, ' ', 48);
363ce3ee1e7SLuigi Rizzo 		i0 = i;
364ce3ee1e7SLuigi Rizzo 		for (j=0; j < 16 && i < lim; i++, j++) {
365ce3ee1e7SLuigi Rizzo 			o[j*3] = P_HI(p[i]);
366ce3ee1e7SLuigi Rizzo 			o[j*3+1] = P_LO(p[i]);
367ce3ee1e7SLuigi Rizzo 		}
368ce3ee1e7SLuigi Rizzo 		i = i0;
369ce3ee1e7SLuigi Rizzo 		for (j=0; j < 16 && i < lim; i++, j++)
370ce3ee1e7SLuigi Rizzo 			o[j + 48] = P_C(p[i]);
371ce3ee1e7SLuigi Rizzo 		o[j+48] = '\n';
372ce3ee1e7SLuigi Rizzo 		o += j+49;
373ce3ee1e7SLuigi Rizzo 	}
374ce3ee1e7SLuigi Rizzo 	*o = '\0';
375ce3ee1e7SLuigi Rizzo #undef P_HI
376ce3ee1e7SLuigi Rizzo #undef P_LO
377ce3ee1e7SLuigi Rizzo #undef P_C
378ce3ee1e7SLuigi Rizzo 	return dst;
379ce3ee1e7SLuigi Rizzo }
380f196ce38SLuigi Rizzo 
381f18be576SLuigi Rizzo 
382ae10d1afSLuigi Rizzo 
383ae10d1afSLuigi Rizzo /*
384ae10d1afSLuigi Rizzo  * Fetch configuration from the device, to cope with dynamic
385ae10d1afSLuigi Rizzo  * reconfigurations after loading the module.
386ae10d1afSLuigi Rizzo  */
387*f9790aebSLuigi Rizzo int
388ae10d1afSLuigi Rizzo netmap_update_config(struct netmap_adapter *na)
389ae10d1afSLuigi Rizzo {
390ae10d1afSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
391ae10d1afSLuigi Rizzo 	u_int txr, txd, rxr, rxd;
392ae10d1afSLuigi Rizzo 
393ae10d1afSLuigi Rizzo 	txr = txd = rxr = rxd = 0;
394ae10d1afSLuigi Rizzo 	if (na->nm_config) {
395*f9790aebSLuigi Rizzo 		na->nm_config(na, &txr, &txd, &rxr, &rxd);
396ae10d1afSLuigi Rizzo 	} else {
397ae10d1afSLuigi Rizzo 		/* take whatever we had at init time */
398ae10d1afSLuigi Rizzo 		txr = na->num_tx_rings;
399ae10d1afSLuigi Rizzo 		txd = na->num_tx_desc;
400ae10d1afSLuigi Rizzo 		rxr = na->num_rx_rings;
401ae10d1afSLuigi Rizzo 		rxd = na->num_rx_desc;
402ae10d1afSLuigi Rizzo 	}
403ae10d1afSLuigi Rizzo 
404ae10d1afSLuigi Rizzo 	if (na->num_tx_rings == txr && na->num_tx_desc == txd &&
405ae10d1afSLuigi Rizzo 	    na->num_rx_rings == rxr && na->num_rx_desc == rxd)
406ae10d1afSLuigi Rizzo 		return 0; /* nothing changed */
407*f9790aebSLuigi Rizzo 	if (netmap_verbose || na->active_fds > 0) {
408ae10d1afSLuigi Rizzo 		D("stored config %s: txring %d x %d, rxring %d x %d",
409*f9790aebSLuigi Rizzo 			NM_IFPNAME(ifp),
410ae10d1afSLuigi Rizzo 			na->num_tx_rings, na->num_tx_desc,
411ae10d1afSLuigi Rizzo 			na->num_rx_rings, na->num_rx_desc);
412ae10d1afSLuigi Rizzo 		D("new config %s: txring %d x %d, rxring %d x %d",
413*f9790aebSLuigi Rizzo 			NM_IFPNAME(ifp), txr, txd, rxr, rxd);
414ae10d1afSLuigi Rizzo 	}
415*f9790aebSLuigi Rizzo 	if (na->active_fds == 0) {
416ae10d1afSLuigi Rizzo 		D("configuration changed (but fine)");
417ae10d1afSLuigi Rizzo 		na->num_tx_rings = txr;
418ae10d1afSLuigi Rizzo 		na->num_tx_desc = txd;
419ae10d1afSLuigi Rizzo 		na->num_rx_rings = rxr;
420ae10d1afSLuigi Rizzo 		na->num_rx_desc = rxd;
421ae10d1afSLuigi Rizzo 		return 0;
422ae10d1afSLuigi Rizzo 	}
423ae10d1afSLuigi Rizzo 	D("configuration changed while active, this is bad...");
424ae10d1afSLuigi Rizzo 	return 1;
425ae10d1afSLuigi Rizzo }
426ae10d1afSLuigi Rizzo 
427*f9790aebSLuigi Rizzo 
428*f9790aebSLuigi Rizzo int
429*f9790aebSLuigi Rizzo netmap_krings_create(struct netmap_adapter *na, u_int ntx, u_int nrx, u_int tailroom)
430*f9790aebSLuigi Rizzo {
431*f9790aebSLuigi Rizzo 	u_int i, len, ndesc;
432*f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
433*f9790aebSLuigi Rizzo 
434*f9790aebSLuigi Rizzo 	len = (ntx + nrx) * sizeof(struct netmap_kring) + tailroom;
435*f9790aebSLuigi Rizzo 
436*f9790aebSLuigi Rizzo 	na->tx_rings = malloc((size_t)len, M_DEVBUF, M_NOWAIT | M_ZERO);
437*f9790aebSLuigi Rizzo 	if (na->tx_rings == NULL) {
438*f9790aebSLuigi Rizzo 		D("Cannot allocate krings");
439*f9790aebSLuigi Rizzo 		return ENOMEM;
440*f9790aebSLuigi Rizzo 	}
441*f9790aebSLuigi Rizzo 	na->rx_rings = na->tx_rings + ntx;
442*f9790aebSLuigi Rizzo 
443*f9790aebSLuigi Rizzo 	ndesc = na->num_tx_desc;
444*f9790aebSLuigi Rizzo 	for (i = 0; i < ntx; i++) { /* Transmit rings */
445*f9790aebSLuigi Rizzo 		kring = &na->tx_rings[i];
446*f9790aebSLuigi Rizzo 		bzero(kring, sizeof(*kring));
447*f9790aebSLuigi Rizzo 		kring->na = na;
448*f9790aebSLuigi Rizzo 		kring->nkr_num_slots = ndesc;
449*f9790aebSLuigi Rizzo 		/*
450*f9790aebSLuigi Rizzo 		 * IMPORTANT:
451*f9790aebSLuigi Rizzo 		 * Always keep one slot empty, so we can detect new
452*f9790aebSLuigi Rizzo 		 * transmissions comparing cur and nr_hwcur (they are
453*f9790aebSLuigi Rizzo 		 * the same only if there are no new transmissions).
454*f9790aebSLuigi Rizzo 		 */
455*f9790aebSLuigi Rizzo 		kring->nr_hwavail = ndesc - 1;
456*f9790aebSLuigi Rizzo 		mtx_init(&kring->q_lock, "nm_txq_lock", NULL, MTX_DEF);
457*f9790aebSLuigi Rizzo 		init_waitqueue_head(&kring->si);
458*f9790aebSLuigi Rizzo 	}
459*f9790aebSLuigi Rizzo 
460*f9790aebSLuigi Rizzo 	ndesc = na->num_rx_desc;
461*f9790aebSLuigi Rizzo 	for (i = 0; i < nrx; i++) { /* Receive rings */
462*f9790aebSLuigi Rizzo 		kring = &na->rx_rings[i];
463*f9790aebSLuigi Rizzo 		bzero(kring, sizeof(*kring));
464*f9790aebSLuigi Rizzo 		kring->na = na;
465*f9790aebSLuigi Rizzo 		kring->nkr_num_slots = ndesc;
466*f9790aebSLuigi Rizzo 		mtx_init(&kring->q_lock, "nm_rxq_lock", NULL, MTX_DEF);
467*f9790aebSLuigi Rizzo 		init_waitqueue_head(&kring->si);
468*f9790aebSLuigi Rizzo 	}
469*f9790aebSLuigi Rizzo 	init_waitqueue_head(&na->tx_si);
470*f9790aebSLuigi Rizzo 	init_waitqueue_head(&na->rx_si);
471*f9790aebSLuigi Rizzo 
472*f9790aebSLuigi Rizzo 	na->tailroom = na->rx_rings + nrx;
473*f9790aebSLuigi Rizzo 
474*f9790aebSLuigi Rizzo 	return 0;
475*f9790aebSLuigi Rizzo 
476*f9790aebSLuigi Rizzo }
477*f9790aebSLuigi Rizzo 
478*f9790aebSLuigi Rizzo 
479*f9790aebSLuigi Rizzo void
480*f9790aebSLuigi Rizzo netmap_krings_delete(struct netmap_adapter *na)
481*f9790aebSLuigi Rizzo {
482*f9790aebSLuigi Rizzo 	int i;
483*f9790aebSLuigi Rizzo 
484*f9790aebSLuigi Rizzo 	for (i = 0; i < na->num_tx_rings + 1; i++) {
485*f9790aebSLuigi Rizzo 		mtx_destroy(&na->tx_rings[i].q_lock);
486*f9790aebSLuigi Rizzo 	}
487*f9790aebSLuigi Rizzo 	for (i = 0; i < na->num_rx_rings + 1; i++) {
488*f9790aebSLuigi Rizzo 		mtx_destroy(&na->rx_rings[i].q_lock);
489*f9790aebSLuigi Rizzo 	}
490*f9790aebSLuigi Rizzo 	free(na->tx_rings, M_DEVBUF);
491*f9790aebSLuigi Rizzo 	na->tx_rings = na->rx_rings = na->tailroom = NULL;
492*f9790aebSLuigi Rizzo }
493*f9790aebSLuigi Rizzo 
494*f9790aebSLuigi Rizzo 
495ce3ee1e7SLuigi Rizzo static struct netmap_if*
496ce3ee1e7SLuigi Rizzo netmap_if_new(const char *ifname, struct netmap_adapter *na)
497ce3ee1e7SLuigi Rizzo {
498*f9790aebSLuigi Rizzo 	struct netmap_if *nifp;
499*f9790aebSLuigi Rizzo 
500ce3ee1e7SLuigi Rizzo 	if (netmap_update_config(na)) {
501ce3ee1e7SLuigi Rizzo 		/* configuration mismatch, report and fail */
502ce3ee1e7SLuigi Rizzo 		return NULL;
503ce3ee1e7SLuigi Rizzo 	}
504*f9790aebSLuigi Rizzo 
505*f9790aebSLuigi Rizzo 	if (na->active_fds)
506*f9790aebSLuigi Rizzo 		goto final;
507*f9790aebSLuigi Rizzo 
508*f9790aebSLuigi Rizzo 	if (na->nm_krings_create(na))
509*f9790aebSLuigi Rizzo 		goto cleanup;
510*f9790aebSLuigi Rizzo 
511*f9790aebSLuigi Rizzo 	if (netmap_mem_rings_create(na))
512*f9790aebSLuigi Rizzo 		goto cleanup;
513*f9790aebSLuigi Rizzo 
514*f9790aebSLuigi Rizzo final:
515*f9790aebSLuigi Rizzo 
516*f9790aebSLuigi Rizzo 	nifp = netmap_mem_if_new(ifname, na);
517*f9790aebSLuigi Rizzo 	if (nifp == NULL)
518*f9790aebSLuigi Rizzo 		goto cleanup;
519*f9790aebSLuigi Rizzo 
520*f9790aebSLuigi Rizzo 	return (nifp);
521*f9790aebSLuigi Rizzo 
522*f9790aebSLuigi Rizzo cleanup:
523*f9790aebSLuigi Rizzo 
524*f9790aebSLuigi Rizzo 	if (na->active_fds == 0) {
525*f9790aebSLuigi Rizzo 		netmap_mem_rings_delete(na);
526*f9790aebSLuigi Rizzo 		na->nm_krings_delete(na);
527ce3ee1e7SLuigi Rizzo 	}
52868b8534bSLuigi Rizzo 
529*f9790aebSLuigi Rizzo 	return NULL;
530*f9790aebSLuigi Rizzo }
5318241616dSLuigi Rizzo 
53268b8534bSLuigi Rizzo 
533ce3ee1e7SLuigi Rizzo /* grab a reference to the memory allocator, if we don't have one already.  The
534ce3ee1e7SLuigi Rizzo  * reference is taken from the netmap_adapter registered with the priv.
535ce3ee1e7SLuigi Rizzo  *
536ce3ee1e7SLuigi Rizzo  */
537ce3ee1e7SLuigi Rizzo static int
538ce3ee1e7SLuigi Rizzo netmap_get_memory_locked(struct netmap_priv_d* p)
539ce3ee1e7SLuigi Rizzo {
540ce3ee1e7SLuigi Rizzo 	struct netmap_mem_d *nmd;
541ce3ee1e7SLuigi Rizzo 	int error = 0;
542ce3ee1e7SLuigi Rizzo 
543*f9790aebSLuigi Rizzo 	if (p->np_na == NULL) {
544ce3ee1e7SLuigi Rizzo 		if (!netmap_mmap_unreg)
545ce3ee1e7SLuigi Rizzo 			return ENODEV;
546ce3ee1e7SLuigi Rizzo 		/* for compatibility with older versions of the API
547ce3ee1e7SLuigi Rizzo  		 * we use the global allocator when no interface has been
548ce3ee1e7SLuigi Rizzo  		 * registered
549ce3ee1e7SLuigi Rizzo  		 */
550ce3ee1e7SLuigi Rizzo 		nmd = &nm_mem;
551ce3ee1e7SLuigi Rizzo 	} else {
552*f9790aebSLuigi Rizzo 		nmd = p->np_na->nm_mem;
553ce3ee1e7SLuigi Rizzo 	}
554ce3ee1e7SLuigi Rizzo 	if (p->np_mref == NULL) {
555ce3ee1e7SLuigi Rizzo 		error = netmap_mem_finalize(nmd);
556ce3ee1e7SLuigi Rizzo 		if (!error)
557ce3ee1e7SLuigi Rizzo 			p->np_mref = nmd;
558ce3ee1e7SLuigi Rizzo 	} else if (p->np_mref != nmd) {
559ce3ee1e7SLuigi Rizzo 		/* a virtual port has been registered, but previous
560ce3ee1e7SLuigi Rizzo  		 * syscalls already used the global allocator.
561ce3ee1e7SLuigi Rizzo  		 * We cannot continue
562ce3ee1e7SLuigi Rizzo  		 */
563ce3ee1e7SLuigi Rizzo 		error = ENODEV;
564ce3ee1e7SLuigi Rizzo 	}
565ce3ee1e7SLuigi Rizzo 	return error;
566ce3ee1e7SLuigi Rizzo }
56768b8534bSLuigi Rizzo 
568*f9790aebSLuigi Rizzo 
569*f9790aebSLuigi Rizzo int
5708241616dSLuigi Rizzo netmap_get_memory(struct netmap_priv_d* p)
5718241616dSLuigi Rizzo {
572ce3ee1e7SLuigi Rizzo 	int error;
573ce3ee1e7SLuigi Rizzo 	NMG_LOCK();
574ce3ee1e7SLuigi Rizzo 	error = netmap_get_memory_locked(p);
575ce3ee1e7SLuigi Rizzo 	NMG_UNLOCK();
5768241616dSLuigi Rizzo 	return error;
5778241616dSLuigi Rizzo }
5788241616dSLuigi Rizzo 
579*f9790aebSLuigi Rizzo 
580ce3ee1e7SLuigi Rizzo static int
581ce3ee1e7SLuigi Rizzo netmap_have_memory_locked(struct netmap_priv_d* p)
582ce3ee1e7SLuigi Rizzo {
583ce3ee1e7SLuigi Rizzo 	return p->np_mref != NULL;
584ce3ee1e7SLuigi Rizzo }
585ce3ee1e7SLuigi Rizzo 
586*f9790aebSLuigi Rizzo 
587ce3ee1e7SLuigi Rizzo static void
588ce3ee1e7SLuigi Rizzo netmap_drop_memory_locked(struct netmap_priv_d* p)
589ce3ee1e7SLuigi Rizzo {
590ce3ee1e7SLuigi Rizzo 	if (p->np_mref) {
591ce3ee1e7SLuigi Rizzo 		netmap_mem_deref(p->np_mref);
592ce3ee1e7SLuigi Rizzo 		p->np_mref = NULL;
593ce3ee1e7SLuigi Rizzo 	}
594ce3ee1e7SLuigi Rizzo }
595ce3ee1e7SLuigi Rizzo 
596*f9790aebSLuigi Rizzo 
59768b8534bSLuigi Rizzo /*
59868b8534bSLuigi Rizzo  * File descriptor's private data destructor.
59968b8534bSLuigi Rizzo  *
60068b8534bSLuigi Rizzo  * Call nm_register(ifp,0) to stop netmap mode on the interface and
601*f9790aebSLuigi Rizzo  * revert to normal operation. We expect that np_na->ifp has not gone.
602ce3ee1e7SLuigi Rizzo  * The second argument is the nifp to work on. In some cases it is
603ce3ee1e7SLuigi Rizzo  * not attached yet to the netmap_priv_d so we need to pass it as
604ce3ee1e7SLuigi Rizzo  * a separate argument.
60568b8534bSLuigi Rizzo  */
606ce3ee1e7SLuigi Rizzo /* call with NMG_LOCK held */
60768b8534bSLuigi Rizzo static void
608ce3ee1e7SLuigi Rizzo netmap_do_unregif(struct netmap_priv_d *priv, struct netmap_if *nifp)
60968b8534bSLuigi Rizzo {
610*f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
611*f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
61268b8534bSLuigi Rizzo 
613ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
614*f9790aebSLuigi Rizzo 	na->active_fds--;
615*f9790aebSLuigi Rizzo 	if (na->active_fds <= 0) {	/* last instance */
61668b8534bSLuigi Rizzo 
617ae10d1afSLuigi Rizzo 		if (netmap_verbose)
618*f9790aebSLuigi Rizzo 			D("deleting last instance for %s", NM_IFPNAME(ifp));
61968b8534bSLuigi Rizzo 		/*
620f18be576SLuigi Rizzo 		 * (TO CHECK) This function is only called
621f18be576SLuigi Rizzo 		 * when the last reference to this file descriptor goes
622f18be576SLuigi Rizzo 		 * away. This means we cannot have any pending poll()
623f18be576SLuigi Rizzo 		 * or interrupt routine operating on the structure.
624ce3ee1e7SLuigi Rizzo 		 * XXX The file may be closed in a thread while
625ce3ee1e7SLuigi Rizzo 		 * another thread is using it.
626ce3ee1e7SLuigi Rizzo 		 * Linux keeps the file opened until the last reference
627ce3ee1e7SLuigi Rizzo 		 * by any outstanding ioctl/poll or mmap is gone.
628ce3ee1e7SLuigi Rizzo 		 * FreeBSD does not track mmap()s (but we do) and
629ce3ee1e7SLuigi Rizzo 		 * wakes up any sleeping poll(). Need to check what
630ce3ee1e7SLuigi Rizzo 		 * happens if the close() occurs while a concurrent
631ce3ee1e7SLuigi Rizzo 		 * syscall is running.
63268b8534bSLuigi Rizzo 		 */
633*f9790aebSLuigi Rizzo 		if (ifp)
634*f9790aebSLuigi Rizzo 			na->nm_register(na, 0); /* off, clear flags */
63568b8534bSLuigi Rizzo 		/* Wake up any sleeping threads. netmap_poll will
63668b8534bSLuigi Rizzo 		 * then return POLLERR
637ce3ee1e7SLuigi Rizzo 		 * XXX The wake up now must happen during *_down(), when
638ce3ee1e7SLuigi Rizzo 		 * we order all activities to stop. -gl
63968b8534bSLuigi Rizzo 		 */
6402f70fca5SEd Maste 		/* XXX kqueue(9) needed; these will mirror knlist_init. */
6412f70fca5SEd Maste 		/* knlist_destroy(&na->tx_si.si_note); */
6422f70fca5SEd Maste 		/* knlist_destroy(&na->rx_si.si_note); */
643*f9790aebSLuigi Rizzo 
644*f9790aebSLuigi Rizzo 		/* delete rings and buffers */
645*f9790aebSLuigi Rizzo 		netmap_mem_rings_delete(na);
646*f9790aebSLuigi Rizzo 		na->nm_krings_delete(na);
64768b8534bSLuigi Rizzo 	}
648*f9790aebSLuigi Rizzo 	/* delete the nifp */
649ce3ee1e7SLuigi Rizzo 	netmap_mem_if_delete(na, nifp);
6505819da83SLuigi Rizzo }
65168b8534bSLuigi Rizzo 
652f18be576SLuigi Rizzo 
653ce3ee1e7SLuigi Rizzo /*
654ce3ee1e7SLuigi Rizzo  * returns 1 if this is the last instance and we can free priv
655ce3ee1e7SLuigi Rizzo  */
656*f9790aebSLuigi Rizzo int
657ce3ee1e7SLuigi Rizzo netmap_dtor_locked(struct netmap_priv_d *priv)
658ce3ee1e7SLuigi Rizzo {
659*f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
660ce3ee1e7SLuigi Rizzo 
661ce3ee1e7SLuigi Rizzo #ifdef __FreeBSD__
662ce3ee1e7SLuigi Rizzo 	/*
663ce3ee1e7SLuigi Rizzo 	 * np_refcount is the number of active mmaps on
664ce3ee1e7SLuigi Rizzo 	 * this file descriptor
665ce3ee1e7SLuigi Rizzo 	 */
666ce3ee1e7SLuigi Rizzo 	if (--priv->np_refcount > 0) {
667ce3ee1e7SLuigi Rizzo 		return 0;
668ce3ee1e7SLuigi Rizzo 	}
669ce3ee1e7SLuigi Rizzo #endif /* __FreeBSD__ */
670*f9790aebSLuigi Rizzo 	if (!na) {
671*f9790aebSLuigi Rizzo 	    return 1; //XXX is it correct?
672ce3ee1e7SLuigi Rizzo 	}
673*f9790aebSLuigi Rizzo 	netmap_do_unregif(priv, priv->np_nifp);
674*f9790aebSLuigi Rizzo 	priv->np_nifp = NULL;
675ce3ee1e7SLuigi Rizzo 	netmap_drop_memory_locked(priv);
676*f9790aebSLuigi Rizzo 	if (priv->np_na) {
677*f9790aebSLuigi Rizzo 		netmap_adapter_put(na);
678*f9790aebSLuigi Rizzo 		priv->np_na = NULL;
679ce3ee1e7SLuigi Rizzo 	}
680ce3ee1e7SLuigi Rizzo 	return 1;
681f196ce38SLuigi Rizzo }
6825819da83SLuigi Rizzo 
683*f9790aebSLuigi Rizzo 
684*f9790aebSLuigi Rizzo void
6855819da83SLuigi Rizzo netmap_dtor(void *data)
6865819da83SLuigi Rizzo {
6875819da83SLuigi Rizzo 	struct netmap_priv_d *priv = data;
688ce3ee1e7SLuigi Rizzo 	int last_instance;
6895819da83SLuigi Rizzo 
690ce3ee1e7SLuigi Rizzo 	NMG_LOCK();
691ce3ee1e7SLuigi Rizzo 	last_instance = netmap_dtor_locked(priv);
692ce3ee1e7SLuigi Rizzo 	NMG_UNLOCK();
693ce3ee1e7SLuigi Rizzo 	if (last_instance) {
694ce3ee1e7SLuigi Rizzo 		bzero(priv, sizeof(*priv));	/* for safety */
69568b8534bSLuigi Rizzo 		free(priv, M_DEVBUF);
69668b8534bSLuigi Rizzo 	}
697ce3ee1e7SLuigi Rizzo }
69868b8534bSLuigi Rizzo 
699f18be576SLuigi Rizzo 
70068b8534bSLuigi Rizzo 
70168b8534bSLuigi Rizzo 
70268b8534bSLuigi Rizzo /*
70302ad4083SLuigi Rizzo  * Handlers for synchronization of the queues from/to the host.
704091fd0abSLuigi Rizzo  * Netmap has two operating modes:
705091fd0abSLuigi Rizzo  * - in the default mode, the rings connected to the host stack are
706091fd0abSLuigi Rizzo  *   just another ring pair managed by userspace;
707091fd0abSLuigi Rizzo  * - in transparent mode (XXX to be defined) incoming packets
708091fd0abSLuigi Rizzo  *   (from the host or the NIC) are marked as NS_FORWARD upon
709091fd0abSLuigi Rizzo  *   arrival, and the user application has a chance to reset the
710091fd0abSLuigi Rizzo  *   flag for packets that should be dropped.
711091fd0abSLuigi Rizzo  *   On the RXSYNC or poll(), packets in RX rings between
712091fd0abSLuigi Rizzo  *   kring->nr_kcur and ring->cur with NS_FORWARD still set are moved
713091fd0abSLuigi Rizzo  *   to the other side.
714091fd0abSLuigi Rizzo  * The transfer NIC --> host is relatively easy, just encapsulate
715091fd0abSLuigi Rizzo  * into mbufs and we are done. The host --> NIC side is slightly
716091fd0abSLuigi Rizzo  * harder because there might not be room in the tx ring so it
717091fd0abSLuigi Rizzo  * might take a while before releasing the buffer.
718091fd0abSLuigi Rizzo  */
719091fd0abSLuigi Rizzo 
720f18be576SLuigi Rizzo 
721091fd0abSLuigi Rizzo /*
722091fd0abSLuigi Rizzo  * pass a chain of buffers to the host stack as coming from 'dst'
723091fd0abSLuigi Rizzo  */
724091fd0abSLuigi Rizzo static void
725*f9790aebSLuigi Rizzo netmap_send_up(struct ifnet *dst, struct mbq *q)
726091fd0abSLuigi Rizzo {
727091fd0abSLuigi Rizzo 	struct mbuf *m;
728091fd0abSLuigi Rizzo 
729091fd0abSLuigi Rizzo 	/* send packets up, outside the lock */
730*f9790aebSLuigi Rizzo 	while ((m = mbq_dequeue(q)) != NULL) {
731091fd0abSLuigi Rizzo 		if (netmap_verbose & NM_VERB_HOST)
732091fd0abSLuigi Rizzo 			D("sending up pkt %p size %d", m, MBUF_LEN(m));
733091fd0abSLuigi Rizzo 		NM_SEND_UP(dst, m);
734091fd0abSLuigi Rizzo 	}
735*f9790aebSLuigi Rizzo 	mbq_destroy(q);
736091fd0abSLuigi Rizzo }
737091fd0abSLuigi Rizzo 
738f18be576SLuigi Rizzo 
739091fd0abSLuigi Rizzo /*
740091fd0abSLuigi Rizzo  * put a copy of the buffers marked NS_FORWARD into an mbuf chain.
741091fd0abSLuigi Rizzo  * Run from hwcur to cur - reserved
742091fd0abSLuigi Rizzo  */
743091fd0abSLuigi Rizzo static void
744091fd0abSLuigi Rizzo netmap_grab_packets(struct netmap_kring *kring, struct mbq *q, int force)
745091fd0abSLuigi Rizzo {
746091fd0abSLuigi Rizzo 	/* Take packets from hwcur to cur-reserved and pass them up.
747091fd0abSLuigi Rizzo 	 * In case of no buffers we give up. At the end of the loop,
748091fd0abSLuigi Rizzo 	 * the queue is drained in all cases.
749091fd0abSLuigi Rizzo 	 * XXX handle reserved
750091fd0abSLuigi Rizzo 	 */
751ce3ee1e7SLuigi Rizzo 	u_int lim = kring->nkr_num_slots - 1;
752*f9790aebSLuigi Rizzo 	struct mbuf *m;
753ce3ee1e7SLuigi Rizzo 	u_int k = kring->ring->cur, n = kring->ring->reserved;
754*f9790aebSLuigi Rizzo 	struct netmap_adapter *na = kring->na;
755091fd0abSLuigi Rizzo 
756ce3ee1e7SLuigi Rizzo 	/* compute the final position, ring->cur - ring->reserved */
757ce3ee1e7SLuigi Rizzo 	if (n > 0) {
758ce3ee1e7SLuigi Rizzo 		if (k < n)
759ce3ee1e7SLuigi Rizzo 			k += kring->nkr_num_slots;
760ce3ee1e7SLuigi Rizzo 		k += n;
761ce3ee1e7SLuigi Rizzo 	}
762091fd0abSLuigi Rizzo 	for (n = kring->nr_hwcur; n != k;) {
763091fd0abSLuigi Rizzo 		struct netmap_slot *slot = &kring->ring->slot[n];
764091fd0abSLuigi Rizzo 
765ce3ee1e7SLuigi Rizzo 		n = nm_next(n, lim);
766091fd0abSLuigi Rizzo 		if ((slot->flags & NS_FORWARD) == 0 && !force)
767091fd0abSLuigi Rizzo 			continue;
768*f9790aebSLuigi Rizzo 		if (slot->len < 14 || slot->len > NETMAP_BDG_BUF_SIZE(na->nm_mem)) {
769091fd0abSLuigi Rizzo 			D("bad pkt at %d len %d", n, slot->len);
770091fd0abSLuigi Rizzo 			continue;
771091fd0abSLuigi Rizzo 		}
772091fd0abSLuigi Rizzo 		slot->flags &= ~NS_FORWARD; // XXX needed ?
773ce3ee1e7SLuigi Rizzo 		/* XXX adapt to the case of a multisegment packet */
774*f9790aebSLuigi Rizzo 		m = m_devget(BDG_NMB(na, slot), slot->len, 0, na->ifp, NULL);
775091fd0abSLuigi Rizzo 
776091fd0abSLuigi Rizzo 		if (m == NULL)
777091fd0abSLuigi Rizzo 			break;
778*f9790aebSLuigi Rizzo 		mbq_enqueue(q, m);
779091fd0abSLuigi Rizzo 	}
780091fd0abSLuigi Rizzo }
781091fd0abSLuigi Rizzo 
782f18be576SLuigi Rizzo 
783091fd0abSLuigi Rizzo /*
784091fd0abSLuigi Rizzo  * The host ring has packets from nr_hwcur to (cur - reserved)
785ce3ee1e7SLuigi Rizzo  * to be sent down to the NIC.
786ce3ee1e7SLuigi Rizzo  * We need to use the queue lock on the source (host RX ring)
787ce3ee1e7SLuigi Rizzo  * to protect against netmap_transmit.
788ce3ee1e7SLuigi Rizzo  * If the user is well behaved we do not need to acquire locks
789ce3ee1e7SLuigi Rizzo  * on the destination(s),
790ce3ee1e7SLuigi Rizzo  * so we only need to make sure that there are no panics because
791ce3ee1e7SLuigi Rizzo  * of user errors.
792ce3ee1e7SLuigi Rizzo  * XXX verify
793ce3ee1e7SLuigi Rizzo  *
794ce3ee1e7SLuigi Rizzo  * We scan the tx rings, which have just been
795091fd0abSLuigi Rizzo  * flushed so nr_hwcur == cur. Pushing packets down means
796091fd0abSLuigi Rizzo  * increment cur and decrement avail.
797091fd0abSLuigi Rizzo  * XXX to be verified
798091fd0abSLuigi Rizzo  */
799091fd0abSLuigi Rizzo static void
800091fd0abSLuigi Rizzo netmap_sw_to_nic(struct netmap_adapter *na)
801091fd0abSLuigi Rizzo {
802091fd0abSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
803091fd0abSLuigi Rizzo 	struct netmap_kring *k1 = &na->tx_rings[0];
804ce3ee1e7SLuigi Rizzo 	u_int i, howmany, src_lim, dst_lim;
805ce3ee1e7SLuigi Rizzo 
806ce3ee1e7SLuigi Rizzo 	/* XXX we should also check that the carrier is on */
807ce3ee1e7SLuigi Rizzo 	if (kring->nkr_stopped)
808ce3ee1e7SLuigi Rizzo 		return;
809ce3ee1e7SLuigi Rizzo 
810ce3ee1e7SLuigi Rizzo 	mtx_lock(&kring->q_lock);
811ce3ee1e7SLuigi Rizzo 
812ce3ee1e7SLuigi Rizzo 	if (kring->nkr_stopped)
813ce3ee1e7SLuigi Rizzo 		goto out;
814091fd0abSLuigi Rizzo 
815091fd0abSLuigi Rizzo 	howmany = kring->nr_hwavail;	/* XXX otherwise cur - reserved - nr_hwcur */
816091fd0abSLuigi Rizzo 
817ce3ee1e7SLuigi Rizzo 	src_lim = kring->nkr_num_slots - 1;
818091fd0abSLuigi Rizzo 	for (i = 0; howmany > 0 && i < na->num_tx_rings; i++, k1++) {
819091fd0abSLuigi Rizzo 		ND("%d packets left to ring %d (space %d)", howmany, i, k1->nr_hwavail);
820ce3ee1e7SLuigi Rizzo 		dst_lim = k1->nkr_num_slots - 1;
821091fd0abSLuigi Rizzo 		while (howmany > 0 && k1->ring->avail > 0) {
822091fd0abSLuigi Rizzo 			struct netmap_slot *src, *dst, tmp;
823091fd0abSLuigi Rizzo 			src = &kring->ring->slot[kring->nr_hwcur];
824091fd0abSLuigi Rizzo 			dst = &k1->ring->slot[k1->ring->cur];
825091fd0abSLuigi Rizzo 			tmp = *src;
826091fd0abSLuigi Rizzo 			src->buf_idx = dst->buf_idx;
827091fd0abSLuigi Rizzo 			src->flags = NS_BUF_CHANGED;
828091fd0abSLuigi Rizzo 
829091fd0abSLuigi Rizzo 			dst->buf_idx = tmp.buf_idx;
830091fd0abSLuigi Rizzo 			dst->len = tmp.len;
831091fd0abSLuigi Rizzo 			dst->flags = NS_BUF_CHANGED;
832091fd0abSLuigi Rizzo 			ND("out len %d buf %d from %d to %d",
833091fd0abSLuigi Rizzo 				dst->len, dst->buf_idx,
834091fd0abSLuigi Rizzo 				kring->nr_hwcur, k1->ring->cur);
835091fd0abSLuigi Rizzo 
836ce3ee1e7SLuigi Rizzo 			kring->nr_hwcur = nm_next(kring->nr_hwcur, src_lim);
837091fd0abSLuigi Rizzo 			howmany--;
838091fd0abSLuigi Rizzo 			kring->nr_hwavail--;
839ce3ee1e7SLuigi Rizzo 			k1->ring->cur = nm_next(k1->ring->cur, dst_lim);
840091fd0abSLuigi Rizzo 			k1->ring->avail--;
841091fd0abSLuigi Rizzo 		}
842091fd0abSLuigi Rizzo 		kring->ring->cur = kring->nr_hwcur; // XXX
843ce3ee1e7SLuigi Rizzo 		k1++; // XXX why?
844091fd0abSLuigi Rizzo 	}
845ce3ee1e7SLuigi Rizzo out:
846ce3ee1e7SLuigi Rizzo 	mtx_unlock(&kring->q_lock);
847091fd0abSLuigi Rizzo }
848091fd0abSLuigi Rizzo 
849f18be576SLuigi Rizzo 
850091fd0abSLuigi Rizzo /*
851ce3ee1e7SLuigi Rizzo  * netmap_txsync_to_host() passes packets up. We are called from a
85202ad4083SLuigi Rizzo  * system call in user process context, and the only contention
85302ad4083SLuigi Rizzo  * can be among multiple user threads erroneously calling
854091fd0abSLuigi Rizzo  * this routine concurrently.
85568b8534bSLuigi Rizzo  */
856*f9790aebSLuigi Rizzo void
857ce3ee1e7SLuigi Rizzo netmap_txsync_to_host(struct netmap_adapter *na)
85868b8534bSLuigi Rizzo {
859d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->tx_rings[na->num_tx_rings];
86068b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
861091fd0abSLuigi Rizzo 	u_int k, lim = kring->nkr_num_slots - 1;
862*f9790aebSLuigi Rizzo 	struct mbq q;
863*f9790aebSLuigi Rizzo 	int error;
86468b8534bSLuigi Rizzo 
865*f9790aebSLuigi Rizzo 	error = nm_kr_tryget(kring);
866*f9790aebSLuigi Rizzo 	if (error) {
867*f9790aebSLuigi Rizzo 		if (error == NM_KR_BUSY)
868ce3ee1e7SLuigi Rizzo 			D("ring %p busy (user error)", kring);
86902ad4083SLuigi Rizzo 		return;
87002ad4083SLuigi Rizzo 	}
871ce3ee1e7SLuigi Rizzo 	k = ring->cur;
872ce3ee1e7SLuigi Rizzo 	if (k > lim) {
873ce3ee1e7SLuigi Rizzo 		D("invalid ring index in stack TX kring %p", kring);
874ce3ee1e7SLuigi Rizzo 		netmap_ring_reinit(kring);
875ce3ee1e7SLuigi Rizzo 		nm_kr_put(kring);
876ce3ee1e7SLuigi Rizzo 		return;
877ce3ee1e7SLuigi Rizzo 	}
87868b8534bSLuigi Rizzo 
87968b8534bSLuigi Rizzo 	/* Take packets from hwcur to cur and pass them up.
88068b8534bSLuigi Rizzo 	 * In case of no buffers we give up. At the end of the loop,
88168b8534bSLuigi Rizzo 	 * the queue is drained in all cases.
88268b8534bSLuigi Rizzo 	 */
883*f9790aebSLuigi Rizzo 	mbq_init(&q);
884091fd0abSLuigi Rizzo 	netmap_grab_packets(kring, &q, 1);
88502ad4083SLuigi Rizzo 	kring->nr_hwcur = k;
88668b8534bSLuigi Rizzo 	kring->nr_hwavail = ring->avail = lim;
88768b8534bSLuigi Rizzo 
888ce3ee1e7SLuigi Rizzo 	nm_kr_put(kring);
889*f9790aebSLuigi Rizzo 	netmap_send_up(na->ifp, &q);
890f18be576SLuigi Rizzo }
891f18be576SLuigi Rizzo 
892f18be576SLuigi Rizzo 
89368b8534bSLuigi Rizzo /*
89402ad4083SLuigi Rizzo  * rxsync backend for packets coming from the host stack.
895ce3ee1e7SLuigi Rizzo  * They have been put in the queue by netmap_transmit() so we
89602ad4083SLuigi Rizzo  * need to protect access to the kring using a lock.
89702ad4083SLuigi Rizzo  *
89868b8534bSLuigi Rizzo  * This routine also does the selrecord if called from the poll handler
89968b8534bSLuigi Rizzo  * (we know because td != NULL).
90001c7d25fSLuigi Rizzo  *
90101c7d25fSLuigi Rizzo  * NOTE: on linux, selrecord() is defined as a macro and uses pwait
90201c7d25fSLuigi Rizzo  *     as an additional hidden argument.
90368b8534bSLuigi Rizzo  */
90468b8534bSLuigi Rizzo static void
905ce3ee1e7SLuigi Rizzo netmap_rxsync_from_host(struct netmap_adapter *na, struct thread *td, void *pwait)
90668b8534bSLuigi Rizzo {
907d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
90868b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
90964ae02c3SLuigi Rizzo 	u_int j, n, lim = kring->nkr_num_slots;
91064ae02c3SLuigi Rizzo 	u_int k = ring->cur, resvd = ring->reserved;
91168b8534bSLuigi Rizzo 
91201c7d25fSLuigi Rizzo 	(void)pwait;	/* disable unused warnings */
913ce3ee1e7SLuigi Rizzo 
914ce3ee1e7SLuigi Rizzo 	if (kring->nkr_stopped) /* check a first time without lock */
915ce3ee1e7SLuigi Rizzo 		return;
916ce3ee1e7SLuigi Rizzo 
917ce3ee1e7SLuigi Rizzo 	mtx_lock(&kring->q_lock);
918ce3ee1e7SLuigi Rizzo 
919ce3ee1e7SLuigi Rizzo 	if (kring->nkr_stopped)  /* check again with lock held */
920ce3ee1e7SLuigi Rizzo 		goto unlock_out;
921ce3ee1e7SLuigi Rizzo 
92264ae02c3SLuigi Rizzo 	if (k >= lim) {
92364ae02c3SLuigi Rizzo 		netmap_ring_reinit(kring);
924ce3ee1e7SLuigi Rizzo 		goto unlock_out;
92564ae02c3SLuigi Rizzo 	}
92664ae02c3SLuigi Rizzo 	/* new packets are already set in nr_hwavail */
92764ae02c3SLuigi Rizzo 	/* skip past packets that userspace has released */
92864ae02c3SLuigi Rizzo 	j = kring->nr_hwcur;
92964ae02c3SLuigi Rizzo 	if (resvd > 0) {
93064ae02c3SLuigi Rizzo 		if (resvd + ring->avail >= lim + 1) {
93164ae02c3SLuigi Rizzo 			D("XXX invalid reserve/avail %d %d", resvd, ring->avail);
93264ae02c3SLuigi Rizzo 			ring->reserved = resvd = 0; // XXX panic...
93364ae02c3SLuigi Rizzo 		}
93464ae02c3SLuigi Rizzo 		k = (k >= resvd) ? k - resvd : k + lim - resvd;
93564ae02c3SLuigi Rizzo 	}
93664ae02c3SLuigi Rizzo 	if (j != k) {
93764ae02c3SLuigi Rizzo 		n = k >= j ? k - j : k + lim - j;
93864ae02c3SLuigi Rizzo 		kring->nr_hwavail -= n;
93902ad4083SLuigi Rizzo 		kring->nr_hwcur = k;
94064ae02c3SLuigi Rizzo 	}
94164ae02c3SLuigi Rizzo 	k = ring->avail = kring->nr_hwavail - resvd;
94202ad4083SLuigi Rizzo 	if (k == 0 && td)
94368b8534bSLuigi Rizzo 		selrecord(td, &kring->si);
94402ad4083SLuigi Rizzo 	if (k && (netmap_verbose & NM_VERB_HOST))
94502ad4083SLuigi Rizzo 		D("%d pkts from stack", k);
946ce3ee1e7SLuigi Rizzo unlock_out:
947ce3ee1e7SLuigi Rizzo 
948ce3ee1e7SLuigi Rizzo 	mtx_unlock(&kring->q_lock);
94968b8534bSLuigi Rizzo }
95068b8534bSLuigi Rizzo 
95168b8534bSLuigi Rizzo 
952*f9790aebSLuigi Rizzo /* Get a netmap adapter for the port.
953*f9790aebSLuigi Rizzo  *
954*f9790aebSLuigi Rizzo  * If it is possible to satisfy the request, return 0
955*f9790aebSLuigi Rizzo  * with *na containing the netmap adapter found.
956*f9790aebSLuigi Rizzo  * Otherwise return an error code, with *na containing NULL.
957*f9790aebSLuigi Rizzo  *
958*f9790aebSLuigi Rizzo  * When the port is attached to a bridge, we always return
959*f9790aebSLuigi Rizzo  * EBUSY.
960*f9790aebSLuigi Rizzo  * Otherwise, if the port is already bound to a file descriptor,
961*f9790aebSLuigi Rizzo  * then we unconditionally return the existing adapter into *na.
962*f9790aebSLuigi Rizzo  * In all the other cases, we return (into *na) either native,
963*f9790aebSLuigi Rizzo  * generic or NULL, according to the following table:
964*f9790aebSLuigi Rizzo  *
965*f9790aebSLuigi Rizzo  *					native_support
966*f9790aebSLuigi Rizzo  * active_fds   dev.netmap.admode         YES     NO
967*f9790aebSLuigi Rizzo  * -------------------------------------------------------
968*f9790aebSLuigi Rizzo  *    >0              *                 NA(ifp) NA(ifp)
969*f9790aebSLuigi Rizzo  *
970*f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_BEST      NATIVE  GENERIC
971*f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_NATIVE    NATIVE   NULL
972*f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_GENERIC   GENERIC GENERIC
973*f9790aebSLuigi Rizzo  *
974*f9790aebSLuigi Rizzo  */
975*f9790aebSLuigi Rizzo 
976*f9790aebSLuigi Rizzo int
977*f9790aebSLuigi Rizzo netmap_get_hw_na(struct ifnet *ifp, struct netmap_adapter **na)
978*f9790aebSLuigi Rizzo {
979*f9790aebSLuigi Rizzo 	/* generic support */
980*f9790aebSLuigi Rizzo 	int i = netmap_admode;	/* Take a snapshot. */
981*f9790aebSLuigi Rizzo 	int error = 0;
982*f9790aebSLuigi Rizzo 	struct netmap_adapter *prev_na;
983*f9790aebSLuigi Rizzo 	struct netmap_generic_adapter *gna;
984*f9790aebSLuigi Rizzo 
985*f9790aebSLuigi Rizzo 	*na = NULL; /* default */
986*f9790aebSLuigi Rizzo 
987*f9790aebSLuigi Rizzo 	/* reset in case of invalid value */
988*f9790aebSLuigi Rizzo 	if (i < NETMAP_ADMODE_BEST || i >= NETMAP_ADMODE_LAST)
989*f9790aebSLuigi Rizzo 		i = netmap_admode = NETMAP_ADMODE_BEST;
990*f9790aebSLuigi Rizzo 
991*f9790aebSLuigi Rizzo 	if (NETMAP_CAPABLE(ifp)) {
992*f9790aebSLuigi Rizzo 		/* If an adapter already exists, but is
993*f9790aebSLuigi Rizzo 		 * attached to a vale port, we report that the
994*f9790aebSLuigi Rizzo 		 * port is busy.
995*f9790aebSLuigi Rizzo 		 */
996*f9790aebSLuigi Rizzo 		if (NETMAP_OWNED_BY_KERN(NA(ifp)))
997*f9790aebSLuigi Rizzo 			return EBUSY;
998*f9790aebSLuigi Rizzo 
999*f9790aebSLuigi Rizzo 		/* If an adapter already exists, return it if
1000*f9790aebSLuigi Rizzo 		 * there are active file descriptors or if
1001*f9790aebSLuigi Rizzo 		 * netmap is not forced to use generic
1002*f9790aebSLuigi Rizzo 		 * adapters.
1003*f9790aebSLuigi Rizzo 		 */
1004*f9790aebSLuigi Rizzo 		if (NA(ifp)->active_fds > 0 ||
1005*f9790aebSLuigi Rizzo 				i != NETMAP_ADMODE_GENERIC) {
1006*f9790aebSLuigi Rizzo 			*na = NA(ifp);
1007*f9790aebSLuigi Rizzo 			return 0;
1008*f9790aebSLuigi Rizzo 		}
1009*f9790aebSLuigi Rizzo 	}
1010*f9790aebSLuigi Rizzo 
1011*f9790aebSLuigi Rizzo 	/* If there isn't native support and netmap is not allowed
1012*f9790aebSLuigi Rizzo 	 * to use generic adapters, we cannot satisfy the request.
1013*f9790aebSLuigi Rizzo 	 */
1014*f9790aebSLuigi Rizzo 	if (!NETMAP_CAPABLE(ifp) && i == NETMAP_ADMODE_NATIVE)
1015*f9790aebSLuigi Rizzo 		return EINVAL;
1016*f9790aebSLuigi Rizzo 
1017*f9790aebSLuigi Rizzo 	/* Otherwise, create a generic adapter and return it,
1018*f9790aebSLuigi Rizzo 	 * saving the previously used netmap adapter, if any.
1019*f9790aebSLuigi Rizzo 	 *
1020*f9790aebSLuigi Rizzo 	 * Note that here 'prev_na', if not NULL, MUST be a
1021*f9790aebSLuigi Rizzo 	 * native adapter, and CANNOT be a generic one. This is
1022*f9790aebSLuigi Rizzo 	 * true because generic adapters are created on demand, and
1023*f9790aebSLuigi Rizzo 	 * destroyed when not used anymore. Therefore, if the adapter
1024*f9790aebSLuigi Rizzo 	 * currently attached to an interface 'ifp' is generic, it
1025*f9790aebSLuigi Rizzo 	 * must be that
1026*f9790aebSLuigi Rizzo 	 * (NA(ifp)->active_fds > 0 || NETMAP_OWNED_BY_KERN(NA(ifp))).
1027*f9790aebSLuigi Rizzo 	 * Consequently, if NA(ifp) is generic, we will enter one of
1028*f9790aebSLuigi Rizzo 	 * the branches above. This ensures that we never override
1029*f9790aebSLuigi Rizzo 	 * a generic adapter with another generic adapter.
1030*f9790aebSLuigi Rizzo 	 */
1031*f9790aebSLuigi Rizzo 	prev_na = NA(ifp);
1032*f9790aebSLuigi Rizzo 	error = generic_netmap_attach(ifp);
1033*f9790aebSLuigi Rizzo 	if (error)
1034*f9790aebSLuigi Rizzo 		return error;
1035*f9790aebSLuigi Rizzo 
1036*f9790aebSLuigi Rizzo 	*na = NA(ifp);
1037*f9790aebSLuigi Rizzo 	gna = (struct netmap_generic_adapter*)NA(ifp);
1038*f9790aebSLuigi Rizzo 	gna->prev = prev_na; /* save old na */
1039*f9790aebSLuigi Rizzo 	if (prev_na != NULL) {
1040*f9790aebSLuigi Rizzo 		ifunit_ref(ifp->if_xname);
1041*f9790aebSLuigi Rizzo 		// XXX add a refcount ?
1042*f9790aebSLuigi Rizzo 		netmap_adapter_get(prev_na);
1043*f9790aebSLuigi Rizzo 	}
1044*f9790aebSLuigi Rizzo 	D("Created generic NA %p (prev %p)", gna, gna->prev);
1045*f9790aebSLuigi Rizzo 
1046*f9790aebSLuigi Rizzo 	return 0;
1047*f9790aebSLuigi Rizzo }
1048*f9790aebSLuigi Rizzo 
1049*f9790aebSLuigi Rizzo 
105068b8534bSLuigi Rizzo /*
1051ce3ee1e7SLuigi Rizzo  * MUST BE CALLED UNDER NMG_LOCK()
1052ce3ee1e7SLuigi Rizzo  *
105368b8534bSLuigi Rizzo  * get a refcounted reference to an interface.
1054ce3ee1e7SLuigi Rizzo  * This is always called in the execution of an ioctl().
1055ce3ee1e7SLuigi Rizzo  *
105668b8534bSLuigi Rizzo  * Return ENXIO if the interface does not exist, EINVAL if netmap
105768b8534bSLuigi Rizzo  * is not supported by the interface.
105868b8534bSLuigi Rizzo  * If successful, hold a reference.
1059f18be576SLuigi Rizzo  *
1060ce3ee1e7SLuigi Rizzo  * When the NIC is attached to a bridge, reference is managed
1061f18be576SLuigi Rizzo  * at na->na_bdg_refcount using ADD/DROP_BDG_REF() as well as
1062f18be576SLuigi Rizzo  * virtual ports.  Hence, on the final DROP_BDG_REF(), the NIC
1063f18be576SLuigi Rizzo  * is detached from the bridge, then ifp's refcount is dropped (this
1064f18be576SLuigi Rizzo  * is equivalent to that ifp is destroyed in case of virtual ports.
1065f18be576SLuigi Rizzo  *
1066f18be576SLuigi Rizzo  * This function uses if_rele() when we want to prevent the NIC from
1067f18be576SLuigi Rizzo  * being detached from the bridge in error handling.  But once refcount
1068f18be576SLuigi Rizzo  * is acquired by this function, it must be released using nm_if_rele().
106968b8534bSLuigi Rizzo  */
1070*f9790aebSLuigi Rizzo int
1071*f9790aebSLuigi Rizzo netmap_get_na(struct nmreq *nmr, struct netmap_adapter **na, int create)
107268b8534bSLuigi Rizzo {
1073*f9790aebSLuigi Rizzo 	struct ifnet *ifp;
1074*f9790aebSLuigi Rizzo 	int error = 0;
1075*f9790aebSLuigi Rizzo 	struct netmap_adapter *ret;
1076*f9790aebSLuigi Rizzo 
1077*f9790aebSLuigi Rizzo 	*na = NULL;     /* default return value */
1078f196ce38SLuigi Rizzo 
1079ce3ee1e7SLuigi Rizzo 	/* first try to see if this is a bridge port. */
1080ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
1081ce3ee1e7SLuigi Rizzo 
1082*f9790aebSLuigi Rizzo 	error = netmap_get_bdg_na(nmr, na, create);
1083*f9790aebSLuigi Rizzo 	if (error || *na != NULL) /* valid match in netmap_get_bdg_na() */
1084*f9790aebSLuigi Rizzo 		return error;
1085ce3ee1e7SLuigi Rizzo 
1086*f9790aebSLuigi Rizzo 	ifp = ifunit_ref(nmr->nr_name);
1087*f9790aebSLuigi Rizzo 	if (ifp == NULL) {
1088ce3ee1e7SLuigi Rizzo 	        return ENXIO;
1089f196ce38SLuigi Rizzo 	}
1090ce3ee1e7SLuigi Rizzo 
1091*f9790aebSLuigi Rizzo 	error = netmap_get_hw_na(ifp, &ret);
1092*f9790aebSLuigi Rizzo 	if (error)
1093*f9790aebSLuigi Rizzo 		goto out;
1094f18be576SLuigi Rizzo 
1095*f9790aebSLuigi Rizzo 	if (ret != NULL) {
1096*f9790aebSLuigi Rizzo 		/* Users cannot use the NIC attached to a bridge directly */
1097*f9790aebSLuigi Rizzo 		if (NETMAP_OWNED_BY_KERN(ret)) {
1098*f9790aebSLuigi Rizzo 			error = EINVAL;
1099*f9790aebSLuigi Rizzo 			goto out;
1100ce3ee1e7SLuigi Rizzo 		}
1101*f9790aebSLuigi Rizzo 		error = 0;
1102*f9790aebSLuigi Rizzo 		*na = ret;
1103*f9790aebSLuigi Rizzo 		netmap_adapter_get(ret);
1104*f9790aebSLuigi Rizzo 	}
1105*f9790aebSLuigi Rizzo out:
1106*f9790aebSLuigi Rizzo 	if_rele(ifp);
1107f18be576SLuigi Rizzo 
11085ab0d24dSLuigi Rizzo 	return error;
11095ab0d24dSLuigi Rizzo }
1110ce3ee1e7SLuigi Rizzo 
1111ce3ee1e7SLuigi Rizzo 
1112*f9790aebSLuigi Rizzo /*
1113*f9790aebSLuigi Rizzo  * validate parameters on entry for *_txsync()
1114*f9790aebSLuigi Rizzo  * Returns ring->cur if ok, or something >= kring->nkr_num_slots
1115*f9790aebSLuigi Rizzo  * in case of error. The extra argument is a pointer to
1116*f9790aebSLuigi Rizzo  * 'new_bufs'. XXX this may be deprecated at some point.
1117*f9790aebSLuigi Rizzo  *
1118*f9790aebSLuigi Rizzo  * Below is a correct configuration on input. ring->cur
1119*f9790aebSLuigi Rizzo  * must be in the region covered by kring->hwavail,
1120*f9790aebSLuigi Rizzo  * and ring->avail and kring->avail should end at the same slot.
1121*f9790aebSLuigi Rizzo  *
1122*f9790aebSLuigi Rizzo  *         +-hwcur
1123*f9790aebSLuigi Rizzo  *         |
1124*f9790aebSLuigi Rizzo  *         v<--hwres-->|<-----hwavail---->
1125*f9790aebSLuigi Rizzo  *   ------+------------------------------+-------- ring
1126*f9790aebSLuigi Rizzo  *                          |
1127*f9790aebSLuigi Rizzo  *                          |<---avail--->
1128*f9790aebSLuigi Rizzo  *                          +--cur
1129*f9790aebSLuigi Rizzo  *
1130*f9790aebSLuigi Rizzo  */
1131*f9790aebSLuigi Rizzo u_int
1132*f9790aebSLuigi Rizzo nm_txsync_prologue(struct netmap_kring *kring, u_int *new_slots)
1133*f9790aebSLuigi Rizzo {
1134*f9790aebSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
1135*f9790aebSLuigi Rizzo 	u_int cur = ring->cur; /* read only once */
1136*f9790aebSLuigi Rizzo 	u_int avail = ring->avail; /* read only once */
1137*f9790aebSLuigi Rizzo 	u_int n = kring->nkr_num_slots;
1138*f9790aebSLuigi Rizzo 	u_int kstart, kend, a;
1139ce3ee1e7SLuigi Rizzo 
1140*f9790aebSLuigi Rizzo #if 1 /* kernel sanity checks */
1141*f9790aebSLuigi Rizzo 	if (kring->nr_hwcur >= n ||
1142*f9790aebSLuigi Rizzo 	    kring->nr_hwreserved >= n || kring->nr_hwavail >= n ||
1143*f9790aebSLuigi Rizzo 	    kring->nr_hwreserved + kring->nr_hwavail >= n)
1144*f9790aebSLuigi Rizzo 		goto error;
1145*f9790aebSLuigi Rizzo #endif /* kernel sanity checks */
1146*f9790aebSLuigi Rizzo 	kstart = kring->nr_hwcur + kring->nr_hwreserved;
1147*f9790aebSLuigi Rizzo 	if (kstart >= n)
1148*f9790aebSLuigi Rizzo 		kstart -= n;
1149*f9790aebSLuigi Rizzo 	kend = kstart + kring->nr_hwavail;
1150*f9790aebSLuigi Rizzo 	/* user sanity checks. a is the expected avail */
1151*f9790aebSLuigi Rizzo 	if (cur < kstart) {
1152*f9790aebSLuigi Rizzo 		/* too low, but maybe wraparound */
1153*f9790aebSLuigi Rizzo 		if (cur + n > kend)
1154*f9790aebSLuigi Rizzo 			goto error;
1155*f9790aebSLuigi Rizzo 		*new_slots = cur + n - kstart;
1156*f9790aebSLuigi Rizzo 		a = kend - cur - n;
1157*f9790aebSLuigi Rizzo 	} else {
1158*f9790aebSLuigi Rizzo 		if (cur > kend)
1159*f9790aebSLuigi Rizzo 			goto error;
1160*f9790aebSLuigi Rizzo 		*new_slots = cur - kstart;
1161*f9790aebSLuigi Rizzo 		a = kend - cur;
1162f18be576SLuigi Rizzo 	}
1163*f9790aebSLuigi Rizzo 	if (a != avail) {
1164*f9790aebSLuigi Rizzo 		RD(5, "wrong but fixable avail have %d need %d",
1165*f9790aebSLuigi Rizzo 			avail, a);
1166*f9790aebSLuigi Rizzo 		ring->avail = avail = a;
1167*f9790aebSLuigi Rizzo 	}
1168*f9790aebSLuigi Rizzo 	return cur;
1169*f9790aebSLuigi Rizzo 
1170*f9790aebSLuigi Rizzo error:
1171*f9790aebSLuigi Rizzo 	RD(5, "kring error: hwcur %d hwres %d hwavail %d cur %d av %d",
1172*f9790aebSLuigi Rizzo 		kring->nr_hwcur,
1173*f9790aebSLuigi Rizzo 		kring->nr_hwreserved, kring->nr_hwavail,
1174*f9790aebSLuigi Rizzo 		cur, avail);
1175*f9790aebSLuigi Rizzo 	return n;
117668b8534bSLuigi Rizzo }
117768b8534bSLuigi Rizzo 
117868b8534bSLuigi Rizzo 
117968b8534bSLuigi Rizzo /*
1180*f9790aebSLuigi Rizzo  * validate parameters on entry for *_rxsync()
1181*f9790aebSLuigi Rizzo  * Returns ring->cur - ring->reserved if ok,
1182*f9790aebSLuigi Rizzo  * or something >= kring->nkr_num_slots
1183*f9790aebSLuigi Rizzo  * in case of error. The extra argument is a pointer to
1184*f9790aebSLuigi Rizzo  * 'resvd'. XXX this may be deprecated at some point.
1185*f9790aebSLuigi Rizzo  *
1186*f9790aebSLuigi Rizzo  * Below is a correct configuration on input. ring->cur and
1187*f9790aebSLuigi Rizzo  * ring->reserved must be in the region covered by kring->hwavail,
1188*f9790aebSLuigi Rizzo  * and ring->avail and kring->avail should end at the same slot.
1189*f9790aebSLuigi Rizzo  *
1190*f9790aebSLuigi Rizzo  *            +-hwcur
1191*f9790aebSLuigi Rizzo  *            |
1192*f9790aebSLuigi Rizzo  *            v<-------hwavail---------->
1193*f9790aebSLuigi Rizzo  *   ---------+--------------------------+-------- ring
1194*f9790aebSLuigi Rizzo  *               |<--res-->|
1195*f9790aebSLuigi Rizzo  *                         |<---avail--->
1196*f9790aebSLuigi Rizzo  *                         +--cur
1197*f9790aebSLuigi Rizzo  *
1198*f9790aebSLuigi Rizzo  */
1199*f9790aebSLuigi Rizzo u_int
1200*f9790aebSLuigi Rizzo nm_rxsync_prologue(struct netmap_kring *kring, u_int *resvd)
1201*f9790aebSLuigi Rizzo {
1202*f9790aebSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
1203*f9790aebSLuigi Rizzo 	u_int cur = ring->cur; /* read only once */
1204*f9790aebSLuigi Rizzo 	u_int avail = ring->avail; /* read only once */
1205*f9790aebSLuigi Rizzo 	u_int res = ring->reserved; /* read only once */
1206*f9790aebSLuigi Rizzo 	u_int n = kring->nkr_num_slots;
1207*f9790aebSLuigi Rizzo 	u_int kend = kring->nr_hwcur + kring->nr_hwavail;
1208*f9790aebSLuigi Rizzo 	u_int a;
1209*f9790aebSLuigi Rizzo 
1210*f9790aebSLuigi Rizzo #if 1 /* kernel sanity checks */
1211*f9790aebSLuigi Rizzo 	if (kring->nr_hwcur >= n || kring->nr_hwavail >= n)
1212*f9790aebSLuigi Rizzo 		goto error;
1213*f9790aebSLuigi Rizzo #endif /* kernel sanity checks */
1214*f9790aebSLuigi Rizzo 	/* user sanity checks */
1215*f9790aebSLuigi Rizzo 	if (res >= n)
1216*f9790aebSLuigi Rizzo 		goto error;
1217*f9790aebSLuigi Rizzo 	/* check that cur is valid, a is the expected value of avail */
1218*f9790aebSLuigi Rizzo 	if (cur < kring->nr_hwcur) {
1219*f9790aebSLuigi Rizzo 		/* too low, but maybe wraparound */
1220*f9790aebSLuigi Rizzo 		if (cur + n > kend)
1221*f9790aebSLuigi Rizzo 			goto error;
1222*f9790aebSLuigi Rizzo 		a = kend - (cur + n);
1223*f9790aebSLuigi Rizzo 	} else  {
1224*f9790aebSLuigi Rizzo 		if (cur > kend)
1225*f9790aebSLuigi Rizzo 			goto error;
1226*f9790aebSLuigi Rizzo 		a = kend - cur;
1227*f9790aebSLuigi Rizzo 	}
1228*f9790aebSLuigi Rizzo 	if (a != avail) {
1229*f9790aebSLuigi Rizzo 		RD(5, "wrong but fixable avail have %d need %d",
1230*f9790aebSLuigi Rizzo 			avail, a);
1231*f9790aebSLuigi Rizzo 		ring->avail = avail = a;
1232*f9790aebSLuigi Rizzo 	}
1233*f9790aebSLuigi Rizzo 	if (res != 0) {
1234*f9790aebSLuigi Rizzo 		/* then repeat the check for cur + res */
1235*f9790aebSLuigi Rizzo 		cur = (cur >= res) ? cur - res : n + cur - res;
1236*f9790aebSLuigi Rizzo 		if (cur < kring->nr_hwcur) {
1237*f9790aebSLuigi Rizzo 			/* too low, but maybe wraparound */
1238*f9790aebSLuigi Rizzo 			if (cur + n > kend)
1239*f9790aebSLuigi Rizzo 				goto error;
1240*f9790aebSLuigi Rizzo 		} else if (cur > kend) {
1241*f9790aebSLuigi Rizzo 			goto error;
1242*f9790aebSLuigi Rizzo 		}
1243*f9790aebSLuigi Rizzo 	}
1244*f9790aebSLuigi Rizzo 	*resvd = res;
1245*f9790aebSLuigi Rizzo 	return cur;
1246*f9790aebSLuigi Rizzo 
1247*f9790aebSLuigi Rizzo error:
1248*f9790aebSLuigi Rizzo 	RD(5, "kring error: hwcur %d hwres %d hwavail %d cur %d av %d res %d",
1249*f9790aebSLuigi Rizzo 		kring->nr_hwcur,
1250*f9790aebSLuigi Rizzo 		kring->nr_hwreserved, kring->nr_hwavail,
1251*f9790aebSLuigi Rizzo 		ring->cur, avail, res);
1252*f9790aebSLuigi Rizzo 	return n;
1253*f9790aebSLuigi Rizzo }
1254*f9790aebSLuigi Rizzo 
1255*f9790aebSLuigi Rizzo /*
125668b8534bSLuigi Rizzo  * Error routine called when txsync/rxsync detects an error.
125768b8534bSLuigi Rizzo  * Can't do much more than resetting cur = hwcur, avail = hwavail.
125868b8534bSLuigi Rizzo  * Return 1 on reinit.
1259506cc70cSLuigi Rizzo  *
1260506cc70cSLuigi Rizzo  * This routine is only called by the upper half of the kernel.
1261506cc70cSLuigi Rizzo  * It only reads hwcur (which is changed only by the upper half, too)
1262506cc70cSLuigi Rizzo  * and hwavail (which may be changed by the lower half, but only on
1263506cc70cSLuigi Rizzo  * a tx ring and only to increase it, so any error will be recovered
1264506cc70cSLuigi Rizzo  * on the next call). For the above, we don't strictly need to call
1265506cc70cSLuigi Rizzo  * it under lock.
126668b8534bSLuigi Rizzo  */
126768b8534bSLuigi Rizzo int
126868b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring)
126968b8534bSLuigi Rizzo {
127068b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
127168b8534bSLuigi Rizzo 	u_int i, lim = kring->nkr_num_slots - 1;
127268b8534bSLuigi Rizzo 	int errors = 0;
127368b8534bSLuigi Rizzo 
1274ce3ee1e7SLuigi Rizzo 	// XXX KASSERT nm_kr_tryget
1275*f9790aebSLuigi Rizzo 	RD(10, "called for %s", NM_IFPNAME(kring->na->ifp));
127668b8534bSLuigi Rizzo 	if (ring->cur > lim)
127768b8534bSLuigi Rizzo 		errors++;
127868b8534bSLuigi Rizzo 	for (i = 0; i <= lim; i++) {
127968b8534bSLuigi Rizzo 		u_int idx = ring->slot[i].buf_idx;
128068b8534bSLuigi Rizzo 		u_int len = ring->slot[i].len;
128168b8534bSLuigi Rizzo 		if (idx < 2 || idx >= netmap_total_buffers) {
128268b8534bSLuigi Rizzo 			if (!errors++)
128368b8534bSLuigi Rizzo 				D("bad buffer at slot %d idx %d len %d ", i, idx, len);
128468b8534bSLuigi Rizzo 			ring->slot[i].buf_idx = 0;
128568b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
1286ce3ee1e7SLuigi Rizzo 		} else if (len > NETMAP_BDG_BUF_SIZE(kring->na->nm_mem)) {
128768b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
128868b8534bSLuigi Rizzo 			if (!errors++)
128968b8534bSLuigi Rizzo 				D("bad len %d at slot %d idx %d",
129068b8534bSLuigi Rizzo 					len, i, idx);
129168b8534bSLuigi Rizzo 		}
129268b8534bSLuigi Rizzo 	}
129368b8534bSLuigi Rizzo 	if (errors) {
129468b8534bSLuigi Rizzo 		int pos = kring - kring->na->tx_rings;
1295d76bf4ffSLuigi Rizzo 		int n = kring->na->num_tx_rings + 1;
129668b8534bSLuigi Rizzo 
12978241616dSLuigi Rizzo 		RD(10, "total %d errors", errors);
129868b8534bSLuigi Rizzo 		errors++;
12998241616dSLuigi Rizzo 		RD(10, "%s %s[%d] reinit, cur %d -> %d avail %d -> %d",
1300*f9790aebSLuigi Rizzo 			NM_IFPNAME(kring->na->ifp),
130168b8534bSLuigi Rizzo 			pos < n ?  "TX" : "RX", pos < n ? pos : pos - n,
130268b8534bSLuigi Rizzo 			ring->cur, kring->nr_hwcur,
130368b8534bSLuigi Rizzo 			ring->avail, kring->nr_hwavail);
130468b8534bSLuigi Rizzo 		ring->cur = kring->nr_hwcur;
130568b8534bSLuigi Rizzo 		ring->avail = kring->nr_hwavail;
130668b8534bSLuigi Rizzo 	}
130768b8534bSLuigi Rizzo 	return (errors ? 1 : 0);
130868b8534bSLuigi Rizzo }
130968b8534bSLuigi Rizzo 
131068b8534bSLuigi Rizzo 
131168b8534bSLuigi Rizzo /*
131268b8534bSLuigi Rizzo  * Set the ring ID. For devices with a single queue, a request
131368b8534bSLuigi Rizzo  * for all rings is the same as a single ring.
131468b8534bSLuigi Rizzo  */
131568b8534bSLuigi Rizzo static int
131668b8534bSLuigi Rizzo netmap_set_ringid(struct netmap_priv_d *priv, u_int ringid)
131768b8534bSLuigi Rizzo {
1318*f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1319*f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
132068b8534bSLuigi Rizzo 	u_int i = ringid & NETMAP_RING_MASK;
132164ae02c3SLuigi Rizzo 	/* initially (np_qfirst == np_qlast) we don't want to lock */
1322ce3ee1e7SLuigi Rizzo 	u_int lim = na->num_rx_rings;
132368b8534bSLuigi Rizzo 
1324d76bf4ffSLuigi Rizzo 	if (na->num_tx_rings > lim)
1325d76bf4ffSLuigi Rizzo 		lim = na->num_tx_rings;
132664ae02c3SLuigi Rizzo 	if ( (ringid & NETMAP_HW_RING) && i >= lim) {
132768b8534bSLuigi Rizzo 		D("invalid ring id %d", i);
132868b8534bSLuigi Rizzo 		return (EINVAL);
132968b8534bSLuigi Rizzo 	}
133068b8534bSLuigi Rizzo 	priv->np_ringid = ringid;
133168b8534bSLuigi Rizzo 	if (ringid & NETMAP_SW_RING) {
133264ae02c3SLuigi Rizzo 		priv->np_qfirst = NETMAP_SW_RING;
133364ae02c3SLuigi Rizzo 		priv->np_qlast = 0;
133468b8534bSLuigi Rizzo 	} else if (ringid & NETMAP_HW_RING) {
133568b8534bSLuigi Rizzo 		priv->np_qfirst = i;
133668b8534bSLuigi Rizzo 		priv->np_qlast = i + 1;
133768b8534bSLuigi Rizzo 	} else {
133868b8534bSLuigi Rizzo 		priv->np_qfirst = 0;
133964ae02c3SLuigi Rizzo 		priv->np_qlast = NETMAP_HW_RING ;
134068b8534bSLuigi Rizzo 	}
134168b8534bSLuigi Rizzo 	priv->np_txpoll = (ringid & NETMAP_NO_TX_POLL) ? 0 : 1;
1342ae10d1afSLuigi Rizzo     if (netmap_verbose) {
134368b8534bSLuigi Rizzo 	if (ringid & NETMAP_SW_RING)
1344*f9790aebSLuigi Rizzo 		D("ringid %s set to SW RING", NM_IFPNAME(ifp));
134568b8534bSLuigi Rizzo 	else if (ringid & NETMAP_HW_RING)
1346*f9790aebSLuigi Rizzo 		D("ringid %s set to HW RING %d", NM_IFPNAME(ifp),
134768b8534bSLuigi Rizzo 			priv->np_qfirst);
134868b8534bSLuigi Rizzo 	else
1349*f9790aebSLuigi Rizzo 		D("ringid %s set to all %d HW RINGS", NM_IFPNAME(ifp), lim);
1350ae10d1afSLuigi Rizzo     }
135168b8534bSLuigi Rizzo 	return 0;
135268b8534bSLuigi Rizzo }
135368b8534bSLuigi Rizzo 
1354f18be576SLuigi Rizzo 
1355f18be576SLuigi Rizzo /*
1356f18be576SLuigi Rizzo  * possibly move the interface to netmap-mode.
1357f18be576SLuigi Rizzo  * If success it returns a pointer to netmap_if, otherwise NULL.
1358ce3ee1e7SLuigi Rizzo  * This must be called with NMG_LOCK held.
1359f18be576SLuigi Rizzo  */
1360*f9790aebSLuigi Rizzo struct netmap_if *
1361*f9790aebSLuigi Rizzo netmap_do_regif(struct netmap_priv_d *priv, struct netmap_adapter *na,
1362f18be576SLuigi Rizzo 	uint16_t ringid, int *err)
1363f18be576SLuigi Rizzo {
1364*f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
1365f18be576SLuigi Rizzo 	struct netmap_if *nifp = NULL;
1366*f9790aebSLuigi Rizzo 	int error, need_mem = 0;
1367f18be576SLuigi Rizzo 
1368ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
1369f18be576SLuigi Rizzo 	/* ring configuration may have changed, fetch from the card */
1370f18be576SLuigi Rizzo 	netmap_update_config(na);
1371*f9790aebSLuigi Rizzo 	priv->np_na = na;     /* store the reference */
1372f18be576SLuigi Rizzo 	error = netmap_set_ringid(priv, ringid);
1373f18be576SLuigi Rizzo 	if (error)
1374f18be576SLuigi Rizzo 		goto out;
1375ce3ee1e7SLuigi Rizzo 	/* ensure allocators are ready */
1376ce3ee1e7SLuigi Rizzo 	need_mem = !netmap_have_memory_locked(priv);
1377ce3ee1e7SLuigi Rizzo 	if (need_mem) {
1378ce3ee1e7SLuigi Rizzo 		error = netmap_get_memory_locked(priv);
1379ce3ee1e7SLuigi Rizzo 		ND("get_memory returned %d", error);
1380ce3ee1e7SLuigi Rizzo 		if (error)
1381ce3ee1e7SLuigi Rizzo 			goto out;
1382ce3ee1e7SLuigi Rizzo 	}
1383*f9790aebSLuigi Rizzo 	nifp = netmap_if_new(NM_IFPNAME(ifp), na);
1384f18be576SLuigi Rizzo 	if (nifp == NULL) { /* allocation failed */
1385ce3ee1e7SLuigi Rizzo 		/* we should drop the allocator, but only
1386ce3ee1e7SLuigi Rizzo 		 * if we were the ones who grabbed it
1387ce3ee1e7SLuigi Rizzo 		 */
1388f18be576SLuigi Rizzo 		error = ENOMEM;
1389ce3ee1e7SLuigi Rizzo 		goto out;
1390ce3ee1e7SLuigi Rizzo 	}
1391*f9790aebSLuigi Rizzo 	na->active_fds++;
1392ce3ee1e7SLuigi Rizzo 	if (ifp->if_capenable & IFCAP_NETMAP) {
1393f18be576SLuigi Rizzo 		/* was already set */
1394f18be576SLuigi Rizzo 	} else {
1395f18be576SLuigi Rizzo 		/* Otherwise set the card in netmap mode
1396f18be576SLuigi Rizzo 		 * and make it use the shared buffers.
1397ce3ee1e7SLuigi Rizzo 		 *
1398ce3ee1e7SLuigi Rizzo 		 * do not core lock because the race is harmless here,
1399ce3ee1e7SLuigi Rizzo 		 * there cannot be any traffic to netmap_transmit()
1400ce3ee1e7SLuigi Rizzo 		 */
1401*f9790aebSLuigi Rizzo 		na->na_lut = na->nm_mem->pools[NETMAP_BUF_POOL].lut;
1402*f9790aebSLuigi Rizzo 		ND("%p->na_lut == %p", na, na->na_lut);
1403*f9790aebSLuigi Rizzo 		na->na_lut_objtotal = na->nm_mem->pools[NETMAP_BUF_POOL].objtotal;
1404*f9790aebSLuigi Rizzo 		error = na->nm_register(na, 1); /* mode on */
1405f18be576SLuigi Rizzo 		if (error) {
1406ce3ee1e7SLuigi Rizzo 			netmap_do_unregif(priv, nifp);
1407f18be576SLuigi Rizzo 			nifp = NULL;
1408f18be576SLuigi Rizzo 		}
1409f18be576SLuigi Rizzo 	}
1410f18be576SLuigi Rizzo out:
1411f18be576SLuigi Rizzo 	*err = error;
1412*f9790aebSLuigi Rizzo 	if (error) {
1413*f9790aebSLuigi Rizzo 		priv->np_na = NULL;
1414*f9790aebSLuigi Rizzo 		if (need_mem)
1415*f9790aebSLuigi Rizzo 			netmap_drop_memory_locked(priv);
1416*f9790aebSLuigi Rizzo 	}
1417ce3ee1e7SLuigi Rizzo 	if (nifp != NULL) {
1418ce3ee1e7SLuigi Rizzo 		/*
1419ce3ee1e7SLuigi Rizzo 		 * advertise that the interface is ready bt setting ni_nifp.
1420ce3ee1e7SLuigi Rizzo 		 * The barrier is needed because readers (poll and *SYNC)
1421ce3ee1e7SLuigi Rizzo 		 * check for priv->np_nifp != NULL without locking
1422ce3ee1e7SLuigi Rizzo 		 */
1423ce3ee1e7SLuigi Rizzo 		wmb(); /* make sure previous writes are visible to all CPUs */
1424ce3ee1e7SLuigi Rizzo 		priv->np_nifp = nifp;
1425ce3ee1e7SLuigi Rizzo 	}
1426f18be576SLuigi Rizzo 	return nifp;
1427f18be576SLuigi Rizzo }
1428f18be576SLuigi Rizzo 
1429f18be576SLuigi Rizzo 
1430f18be576SLuigi Rizzo 
143168b8534bSLuigi Rizzo /*
143268b8534bSLuigi Rizzo  * ioctl(2) support for the "netmap" device.
143368b8534bSLuigi Rizzo  *
143468b8534bSLuigi Rizzo  * Following a list of accepted commands:
143568b8534bSLuigi Rizzo  * - NIOCGINFO
143668b8534bSLuigi Rizzo  * - SIOCGIFADDR	just for convenience
143768b8534bSLuigi Rizzo  * - NIOCREGIF
143868b8534bSLuigi Rizzo  * - NIOCUNREGIF
143968b8534bSLuigi Rizzo  * - NIOCTXSYNC
144068b8534bSLuigi Rizzo  * - NIOCRXSYNC
144168b8534bSLuigi Rizzo  *
144268b8534bSLuigi Rizzo  * Return 0 on success, errno otherwise.
144368b8534bSLuigi Rizzo  */
1444*f9790aebSLuigi Rizzo int
14450b8ed8e0SLuigi Rizzo netmap_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
14460b8ed8e0SLuigi Rizzo 	int fflag, struct thread *td)
144768b8534bSLuigi Rizzo {
144868b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
1449ce3ee1e7SLuigi Rizzo 	struct ifnet *ifp = NULL;
145068b8534bSLuigi Rizzo 	struct nmreq *nmr = (struct nmreq *) data;
1451ce3ee1e7SLuigi Rizzo 	struct netmap_adapter *na = NULL;
145268b8534bSLuigi Rizzo 	int error;
145364ae02c3SLuigi Rizzo 	u_int i, lim;
145468b8534bSLuigi Rizzo 	struct netmap_if *nifp;
1455ce3ee1e7SLuigi Rizzo 	struct netmap_kring *krings;
145668b8534bSLuigi Rizzo 
14570b8ed8e0SLuigi Rizzo 	(void)dev;	/* UNUSED */
14580b8ed8e0SLuigi Rizzo 	(void)fflag;	/* UNUSED */
1459f196ce38SLuigi Rizzo #ifdef linux
1460f196ce38SLuigi Rizzo #define devfs_get_cdevpriv(pp)				\
1461f196ce38SLuigi Rizzo 	({ *(struct netmap_priv_d **)pp = ((struct file *)td)->private_data; 	\
1462f196ce38SLuigi Rizzo 		(*pp ? 0 : ENOENT); })
1463f196ce38SLuigi Rizzo 
1464f196ce38SLuigi Rizzo /* devfs_set_cdevpriv cannot fail on linux */
1465f196ce38SLuigi Rizzo #define devfs_set_cdevpriv(p, fn)				\
1466f196ce38SLuigi Rizzo 	({ ((struct file *)td)->private_data = p; (p ? 0 : EINVAL); })
1467f196ce38SLuigi Rizzo 
1468f196ce38SLuigi Rizzo 
1469f196ce38SLuigi Rizzo #define devfs_clear_cdevpriv()	do {				\
1470f196ce38SLuigi Rizzo 		netmap_dtor(priv); ((struct file *)td)->private_data = 0;	\
1471f196ce38SLuigi Rizzo 	} while (0)
1472f196ce38SLuigi Rizzo #endif /* linux */
1473f196ce38SLuigi Rizzo 
1474506cc70cSLuigi Rizzo 	CURVNET_SET(TD_TO_VNET(td));
1475506cc70cSLuigi Rizzo 
147668b8534bSLuigi Rizzo 	error = devfs_get_cdevpriv((void **)&priv);
14778241616dSLuigi Rizzo 	if (error) {
1478506cc70cSLuigi Rizzo 		CURVNET_RESTORE();
14798241616dSLuigi Rizzo 		/* XXX ENOENT should be impossible, since the priv
14808241616dSLuigi Rizzo 		 * is now created in the open */
14818241616dSLuigi Rizzo 		return (error == ENOENT ? ENXIO : error);
1482506cc70cSLuigi Rizzo 	}
148368b8534bSLuigi Rizzo 
1484f196ce38SLuigi Rizzo 	nmr->nr_name[sizeof(nmr->nr_name) - 1] = '\0';	/* truncate name */
148568b8534bSLuigi Rizzo 	switch (cmd) {
148668b8534bSLuigi Rizzo 	case NIOCGINFO:		/* return capabilities etc */
148764ae02c3SLuigi Rizzo 		if (nmr->nr_version != NETMAP_API) {
148864ae02c3SLuigi Rizzo 			D("API mismatch got %d have %d",
148964ae02c3SLuigi Rizzo 				nmr->nr_version, NETMAP_API);
149064ae02c3SLuigi Rizzo 			nmr->nr_version = NETMAP_API;
149164ae02c3SLuigi Rizzo 			error = EINVAL;
149264ae02c3SLuigi Rizzo 			break;
149364ae02c3SLuigi Rizzo 		}
1494f18be576SLuigi Rizzo 		if (nmr->nr_cmd == NETMAP_BDG_LIST) {
1495f18be576SLuigi Rizzo 			error = netmap_bdg_ctl(nmr, NULL);
1496f18be576SLuigi Rizzo 			break;
1497f18be576SLuigi Rizzo 		}
1498ce3ee1e7SLuigi Rizzo 
1499ce3ee1e7SLuigi Rizzo 		NMG_LOCK();
1500ce3ee1e7SLuigi Rizzo 		do {
1501ce3ee1e7SLuigi Rizzo 			/* memsize is always valid */
1502ce3ee1e7SLuigi Rizzo 			struct netmap_mem_d *nmd = &nm_mem;
1503ce3ee1e7SLuigi Rizzo 			u_int memflags;
1504ce3ee1e7SLuigi Rizzo 
1505ce3ee1e7SLuigi Rizzo 			if (nmr->nr_name[0] != '\0') {
1506ce3ee1e7SLuigi Rizzo 				/* get a refcount */
1507*f9790aebSLuigi Rizzo 				error = netmap_get_na(nmr, &na, 1 /* create */);
15088241616dSLuigi Rizzo 				if (error)
15098241616dSLuigi Rizzo 					break;
1510*f9790aebSLuigi Rizzo 				nmd = na->nm_mem; /* get memory allocator */
1511ce3ee1e7SLuigi Rizzo 			}
1512ce3ee1e7SLuigi Rizzo 
1513ce3ee1e7SLuigi Rizzo 			error = netmap_mem_get_info(nmd, &nmr->nr_memsize, &memflags);
1514ce3ee1e7SLuigi Rizzo 			if (error)
1515ce3ee1e7SLuigi Rizzo 				break;
1516ce3ee1e7SLuigi Rizzo 			if (na == NULL) /* only memory info */
1517ce3ee1e7SLuigi Rizzo 				break;
15188241616dSLuigi Rizzo 			nmr->nr_offset = 0;
15198241616dSLuigi Rizzo 			nmr->nr_rx_slots = nmr->nr_tx_slots = 0;
1520ae10d1afSLuigi Rizzo 			netmap_update_config(na);
1521d76bf4ffSLuigi Rizzo 			nmr->nr_rx_rings = na->num_rx_rings;
1522d76bf4ffSLuigi Rizzo 			nmr->nr_tx_rings = na->num_tx_rings;
152364ae02c3SLuigi Rizzo 			nmr->nr_rx_slots = na->num_rx_desc;
152464ae02c3SLuigi Rizzo 			nmr->nr_tx_slots = na->num_tx_desc;
1525ce3ee1e7SLuigi Rizzo 			if (memflags & NETMAP_MEM_PRIVATE)
1526ce3ee1e7SLuigi Rizzo 				nmr->nr_ringid |= NETMAP_PRIV_MEM;
1527*f9790aebSLuigi Rizzo 			netmap_adapter_put(na);
1528ce3ee1e7SLuigi Rizzo 		} while (0);
1529ce3ee1e7SLuigi Rizzo 		NMG_UNLOCK();
153068b8534bSLuigi Rizzo 		break;
153168b8534bSLuigi Rizzo 
153268b8534bSLuigi Rizzo 	case NIOCREGIF:
153364ae02c3SLuigi Rizzo 		if (nmr->nr_version != NETMAP_API) {
153464ae02c3SLuigi Rizzo 			nmr->nr_version = NETMAP_API;
153564ae02c3SLuigi Rizzo 			error = EINVAL;
153664ae02c3SLuigi Rizzo 			break;
153764ae02c3SLuigi Rizzo 		}
1538f18be576SLuigi Rizzo 		/* possibly attach/detach NIC and VALE switch */
1539f18be576SLuigi Rizzo 		i = nmr->nr_cmd;
1540*f9790aebSLuigi Rizzo 		if (i == NETMAP_BDG_ATTACH || i == NETMAP_BDG_DETACH
1541*f9790aebSLuigi Rizzo 				|| i == NETMAP_BDG_OFFSET) {
1542f18be576SLuigi Rizzo 			error = netmap_bdg_ctl(nmr, NULL);
1543f18be576SLuigi Rizzo 			break;
1544f18be576SLuigi Rizzo 		} else if (i != 0) {
1545f18be576SLuigi Rizzo 			D("nr_cmd must be 0 not %d", i);
1546f18be576SLuigi Rizzo 			error = EINVAL;
1547f18be576SLuigi Rizzo 			break;
1548f18be576SLuigi Rizzo 		}
1549f18be576SLuigi Rizzo 
15508241616dSLuigi Rizzo 		/* protect access to priv from concurrent NIOCREGIF */
1551ce3ee1e7SLuigi Rizzo 		NMG_LOCK();
1552ce3ee1e7SLuigi Rizzo 		do {
1553ce3ee1e7SLuigi Rizzo 			u_int memflags;
1554ce3ee1e7SLuigi Rizzo 
1555*f9790aebSLuigi Rizzo 			if (priv->np_na != NULL) {	/* thread already registered */
1556506cc70cSLuigi Rizzo 				error = netmap_set_ringid(priv, nmr->nr_ringid);
1557506cc70cSLuigi Rizzo 				break;
1558506cc70cSLuigi Rizzo 			}
155968b8534bSLuigi Rizzo 			/* find the interface and a reference */
1560*f9790aebSLuigi Rizzo 			error = netmap_get_na(nmr, &na, 1 /* create */); /* keep reference */
156168b8534bSLuigi Rizzo 			if (error)
1562ce3ee1e7SLuigi Rizzo 				break;
1563*f9790aebSLuigi Rizzo 			ifp = na->ifp;
1564*f9790aebSLuigi Rizzo 			if (NETMAP_OWNED_BY_KERN(na)) {
1565*f9790aebSLuigi Rizzo 				netmap_adapter_put(na);
1566ce3ee1e7SLuigi Rizzo 				error = EBUSY;
1567ce3ee1e7SLuigi Rizzo 				break;
1568f196ce38SLuigi Rizzo 			}
1569*f9790aebSLuigi Rizzo 			nifp = netmap_do_regif(priv, na, nmr->nr_ringid, &error);
1570f18be576SLuigi Rizzo 			if (!nifp) {    /* reg. failed, release priv and ref */
1571*f9790aebSLuigi Rizzo 				netmap_adapter_put(na);
15728241616dSLuigi Rizzo 				priv->np_nifp = NULL;
1573ce3ee1e7SLuigi Rizzo 				break;
157468b8534bSLuigi Rizzo 			}
157568b8534bSLuigi Rizzo 
157668b8534bSLuigi Rizzo 			/* return the offset of the netmap_if object */
1577d76bf4ffSLuigi Rizzo 			nmr->nr_rx_rings = na->num_rx_rings;
1578d76bf4ffSLuigi Rizzo 			nmr->nr_tx_rings = na->num_tx_rings;
157964ae02c3SLuigi Rizzo 			nmr->nr_rx_slots = na->num_rx_desc;
158064ae02c3SLuigi Rizzo 			nmr->nr_tx_slots = na->num_tx_desc;
1581ce3ee1e7SLuigi Rizzo 			error = netmap_mem_get_info(na->nm_mem, &nmr->nr_memsize, &memflags);
1582ce3ee1e7SLuigi Rizzo 			if (error) {
1583*f9790aebSLuigi Rizzo 				netmap_adapter_put(na);
1584ce3ee1e7SLuigi Rizzo 				break;
1585ce3ee1e7SLuigi Rizzo 			}
1586ce3ee1e7SLuigi Rizzo 			if (memflags & NETMAP_MEM_PRIVATE) {
1587ce3ee1e7SLuigi Rizzo 				nmr->nr_ringid |= NETMAP_PRIV_MEM;
15883d819cb6SLuigi Rizzo 				*(uint32_t *)(uintptr_t)&nifp->ni_flags |= NI_PRIV_MEM;
1589ce3ee1e7SLuigi Rizzo 			}
1590ce3ee1e7SLuigi Rizzo 			nmr->nr_offset = netmap_mem_if_offset(na->nm_mem, nifp);
1591ce3ee1e7SLuigi Rizzo 		} while (0);
1592ce3ee1e7SLuigi Rizzo 		NMG_UNLOCK();
159368b8534bSLuigi Rizzo 		break;
159468b8534bSLuigi Rizzo 
159568b8534bSLuigi Rizzo 	case NIOCUNREGIF:
15968241616dSLuigi Rizzo 		// XXX we have no data here ?
15978241616dSLuigi Rizzo 		D("deprecated, data is %p", nmr);
15988241616dSLuigi Rizzo 		error = EINVAL;
159968b8534bSLuigi Rizzo 		break;
160068b8534bSLuigi Rizzo 
160168b8534bSLuigi Rizzo 	case NIOCTXSYNC:
160268b8534bSLuigi Rizzo 	case NIOCRXSYNC:
16038241616dSLuigi Rizzo 		nifp = priv->np_nifp;
16048241616dSLuigi Rizzo 
16058241616dSLuigi Rizzo 		if (nifp == NULL) {
1606506cc70cSLuigi Rizzo 			error = ENXIO;
1607506cc70cSLuigi Rizzo 			break;
1608506cc70cSLuigi Rizzo 		}
16098241616dSLuigi Rizzo 		rmb(); /* make sure following reads are not from cache */
16108241616dSLuigi Rizzo 
1611*f9790aebSLuigi Rizzo 		na = priv->np_na;      /* we have a reference */
16128241616dSLuigi Rizzo 
1613*f9790aebSLuigi Rizzo 		if (na == NULL) {
1614*f9790aebSLuigi Rizzo 			D("Internal error: nifp != NULL && na == NULL");
16158241616dSLuigi Rizzo 			error = ENXIO;
16168241616dSLuigi Rizzo 			break;
16178241616dSLuigi Rizzo 		}
16188241616dSLuigi Rizzo 
1619*f9790aebSLuigi Rizzo 		ifp = na->ifp;
1620*f9790aebSLuigi Rizzo 		if (ifp == NULL) {
1621*f9790aebSLuigi Rizzo 			RD(1, "the ifp is gone");
1622*f9790aebSLuigi Rizzo 			error = ENXIO;
1623*f9790aebSLuigi Rizzo 			break;
1624*f9790aebSLuigi Rizzo 		}
1625*f9790aebSLuigi Rizzo 
162664ae02c3SLuigi Rizzo 		if (priv->np_qfirst == NETMAP_SW_RING) { /* host rings */
162768b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC)
1628ce3ee1e7SLuigi Rizzo 				netmap_txsync_to_host(na);
162968b8534bSLuigi Rizzo 			else
1630ce3ee1e7SLuigi Rizzo 				netmap_rxsync_from_host(na, NULL, NULL);
1631506cc70cSLuigi Rizzo 			break;
163268b8534bSLuigi Rizzo 		}
163364ae02c3SLuigi Rizzo 		/* find the last ring to scan */
163464ae02c3SLuigi Rizzo 		lim = priv->np_qlast;
163564ae02c3SLuigi Rizzo 		if (lim == NETMAP_HW_RING)
16363c0caf6cSLuigi Rizzo 			lim = (cmd == NIOCTXSYNC) ?
1637d76bf4ffSLuigi Rizzo 			    na->num_tx_rings : na->num_rx_rings;
163868b8534bSLuigi Rizzo 
1639ce3ee1e7SLuigi Rizzo 		krings = (cmd == NIOCTXSYNC) ? na->tx_rings : na->rx_rings;
164064ae02c3SLuigi Rizzo 		for (i = priv->np_qfirst; i < lim; i++) {
1641ce3ee1e7SLuigi Rizzo 			struct netmap_kring *kring = krings + i;
1642ce3ee1e7SLuigi Rizzo 			if (nm_kr_tryget(kring)) {
1643ce3ee1e7SLuigi Rizzo 				error = EBUSY;
1644ce3ee1e7SLuigi Rizzo 				goto out;
1645ce3ee1e7SLuigi Rizzo 			}
164668b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC) {
164768b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
16483c0caf6cSLuigi Rizzo 					D("pre txsync ring %d cur %d hwcur %d",
164968b8534bSLuigi Rizzo 					    i, kring->ring->cur,
165068b8534bSLuigi Rizzo 					    kring->nr_hwcur);
1651*f9790aebSLuigi Rizzo 				na->nm_txsync(na, i, NAF_FORCE_RECLAIM);
165268b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
16533c0caf6cSLuigi Rizzo 					D("post txsync ring %d cur %d hwcur %d",
165468b8534bSLuigi Rizzo 					    i, kring->ring->cur,
165568b8534bSLuigi Rizzo 					    kring->nr_hwcur);
165668b8534bSLuigi Rizzo 			} else {
1657*f9790aebSLuigi Rizzo 				na->nm_rxsync(na, i, NAF_FORCE_READ);
165868b8534bSLuigi Rizzo 				microtime(&na->rx_rings[i].ring->ts);
165968b8534bSLuigi Rizzo 			}
1660ce3ee1e7SLuigi Rizzo 			nm_kr_put(kring);
166168b8534bSLuigi Rizzo 		}
166268b8534bSLuigi Rizzo 
166368b8534bSLuigi Rizzo 		break;
166468b8534bSLuigi Rizzo 
1665f196ce38SLuigi Rizzo #ifdef __FreeBSD__
166668b8534bSLuigi Rizzo 	case BIOCIMMEDIATE:
166768b8534bSLuigi Rizzo 	case BIOCGHDRCMPLT:
166868b8534bSLuigi Rizzo 	case BIOCSHDRCMPLT:
166968b8534bSLuigi Rizzo 	case BIOCSSEESENT:
167068b8534bSLuigi Rizzo 		D("ignore BIOCIMMEDIATE/BIOCSHDRCMPLT/BIOCSHDRCMPLT/BIOCSSEESENT");
167168b8534bSLuigi Rizzo 		break;
167268b8534bSLuigi Rizzo 
1673babc7c12SLuigi Rizzo 	default:	/* allow device-specific ioctls */
167468b8534bSLuigi Rizzo 	    {
167568b8534bSLuigi Rizzo 		struct socket so;
1676ce3ee1e7SLuigi Rizzo 
167768b8534bSLuigi Rizzo 		bzero(&so, sizeof(so));
1678ce3ee1e7SLuigi Rizzo 		NMG_LOCK();
1679*f9790aebSLuigi Rizzo 		error = netmap_get_na(nmr, &na, 0 /* don't create */); /* keep reference */
1680ce3ee1e7SLuigi Rizzo 		if (error) {
1681*f9790aebSLuigi Rizzo 			netmap_adapter_put(na);
1682ce3ee1e7SLuigi Rizzo 			NMG_UNLOCK();
168368b8534bSLuigi Rizzo 			break;
1684ce3ee1e7SLuigi Rizzo 		}
1685*f9790aebSLuigi Rizzo 		ifp = na->ifp;
168668b8534bSLuigi Rizzo 		so.so_vnet = ifp->if_vnet;
168768b8534bSLuigi Rizzo 		// so->so_proto not null.
168868b8534bSLuigi Rizzo 		error = ifioctl(&so, cmd, data, td);
1689*f9790aebSLuigi Rizzo 		netmap_adapter_put(na);
1690ce3ee1e7SLuigi Rizzo 		NMG_UNLOCK();
1691babc7c12SLuigi Rizzo 		break;
169268b8534bSLuigi Rizzo 	    }
1693f196ce38SLuigi Rizzo 
1694f196ce38SLuigi Rizzo #else /* linux */
1695f196ce38SLuigi Rizzo 	default:
1696f196ce38SLuigi Rizzo 		error = EOPNOTSUPP;
1697f196ce38SLuigi Rizzo #endif /* linux */
169868b8534bSLuigi Rizzo 	}
1699ce3ee1e7SLuigi Rizzo out:
170068b8534bSLuigi Rizzo 
1701506cc70cSLuigi Rizzo 	CURVNET_RESTORE();
170268b8534bSLuigi Rizzo 	return (error);
170368b8534bSLuigi Rizzo }
170468b8534bSLuigi Rizzo 
170568b8534bSLuigi Rizzo 
170668b8534bSLuigi Rizzo /*
170768b8534bSLuigi Rizzo  * select(2) and poll(2) handlers for the "netmap" device.
170868b8534bSLuigi Rizzo  *
170968b8534bSLuigi Rizzo  * Can be called for one or more queues.
171068b8534bSLuigi Rizzo  * Return true the event mask corresponding to ready events.
171168b8534bSLuigi Rizzo  * If there are no ready events, do a selrecord on either individual
1712ce3ee1e7SLuigi Rizzo  * selinfo or on the global one.
171368b8534bSLuigi Rizzo  * Device-dependent parts (locking and sync of tx/rx rings)
171468b8534bSLuigi Rizzo  * are done through callbacks.
1715f196ce38SLuigi Rizzo  *
171601c7d25fSLuigi Rizzo  * On linux, arguments are really pwait, the poll table, and 'td' is struct file *
171701c7d25fSLuigi Rizzo  * The first one is remapped to pwait as selrecord() uses the name as an
171801c7d25fSLuigi Rizzo  * hidden argument.
171968b8534bSLuigi Rizzo  */
1720*f9790aebSLuigi Rizzo int
172101c7d25fSLuigi Rizzo netmap_poll(struct cdev *dev, int events, struct thread *td)
172268b8534bSLuigi Rizzo {
172368b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
172468b8534bSLuigi Rizzo 	struct netmap_adapter *na;
172568b8534bSLuigi Rizzo 	struct ifnet *ifp;
172668b8534bSLuigi Rizzo 	struct netmap_kring *kring;
1727954dca4cSLuigi Rizzo 	u_int i, check_all_tx, check_all_rx, want_tx, want_rx, revents = 0;
1728091fd0abSLuigi Rizzo 	u_int lim_tx, lim_rx, host_forwarded = 0;
1729*f9790aebSLuigi Rizzo 	struct mbq q;
173001c7d25fSLuigi Rizzo 	void *pwait = dev;	/* linux compatibility */
173101c7d25fSLuigi Rizzo 
1732*f9790aebSLuigi Rizzo 	/*
1733*f9790aebSLuigi Rizzo 	 * In order to avoid nested locks, we need to "double check"
1734*f9790aebSLuigi Rizzo 	 * txsync and rxsync if we decide to do a selrecord().
1735*f9790aebSLuigi Rizzo 	 * retry_tx (and retry_rx, later) prevent looping forever.
1736*f9790aebSLuigi Rizzo 	 */
1737ce3ee1e7SLuigi Rizzo 	int retry_tx = 1;
1738ce3ee1e7SLuigi Rizzo 
173901c7d25fSLuigi Rizzo 	(void)pwait;
1740*f9790aebSLuigi Rizzo 	mbq_init(&q);
174168b8534bSLuigi Rizzo 
174268b8534bSLuigi Rizzo 	if (devfs_get_cdevpriv((void **)&priv) != 0 || priv == NULL)
174368b8534bSLuigi Rizzo 		return POLLERR;
174468b8534bSLuigi Rizzo 
17458241616dSLuigi Rizzo 	if (priv->np_nifp == NULL) {
17468241616dSLuigi Rizzo 		D("No if registered");
17478241616dSLuigi Rizzo 		return POLLERR;
17488241616dSLuigi Rizzo 	}
17498241616dSLuigi Rizzo 	rmb(); /* make sure following reads are not from cache */
17508241616dSLuigi Rizzo 
1751*f9790aebSLuigi Rizzo 	na = priv->np_na;
1752*f9790aebSLuigi Rizzo 	ifp = na->ifp;
1753*f9790aebSLuigi Rizzo 	// check for deleted
1754*f9790aebSLuigi Rizzo 	if (ifp == NULL) {
1755*f9790aebSLuigi Rizzo 		RD(1, "the ifp is gone");
1756*f9790aebSLuigi Rizzo 		return POLLERR;
1757*f9790aebSLuigi Rizzo 	}
1758*f9790aebSLuigi Rizzo 
175968b8534bSLuigi Rizzo 	if ( (ifp->if_capenable & IFCAP_NETMAP) == 0)
176068b8534bSLuigi Rizzo 		return POLLERR;
176168b8534bSLuigi Rizzo 
176268b8534bSLuigi Rizzo 	if (netmap_verbose & 0x8000)
1763*f9790aebSLuigi Rizzo 		D("device %s events 0x%x", NM_IFPNAME(ifp), events);
176468b8534bSLuigi Rizzo 	want_tx = events & (POLLOUT | POLLWRNORM);
176568b8534bSLuigi Rizzo 	want_rx = events & (POLLIN | POLLRDNORM);
176668b8534bSLuigi Rizzo 
1767d76bf4ffSLuigi Rizzo 	lim_tx = na->num_tx_rings;
1768d76bf4ffSLuigi Rizzo 	lim_rx = na->num_rx_rings;
1769ce3ee1e7SLuigi Rizzo 
177064ae02c3SLuigi Rizzo 	if (priv->np_qfirst == NETMAP_SW_RING) {
1771ce3ee1e7SLuigi Rizzo 		/* handle the host stack ring */
177268b8534bSLuigi Rizzo 		if (priv->np_txpoll || want_tx) {
177368b8534bSLuigi Rizzo 			/* push any packets up, then we are always ready */
1774ce3ee1e7SLuigi Rizzo 			netmap_txsync_to_host(na);
177568b8534bSLuigi Rizzo 			revents |= want_tx;
177668b8534bSLuigi Rizzo 		}
177768b8534bSLuigi Rizzo 		if (want_rx) {
177864ae02c3SLuigi Rizzo 			kring = &na->rx_rings[lim_rx];
177968b8534bSLuigi Rizzo 			if (kring->ring->avail == 0)
1780ce3ee1e7SLuigi Rizzo 				netmap_rxsync_from_host(na, td, dev);
178168b8534bSLuigi Rizzo 			if (kring->ring->avail > 0) {
178268b8534bSLuigi Rizzo 				revents |= want_rx;
178368b8534bSLuigi Rizzo 			}
178468b8534bSLuigi Rizzo 		}
178568b8534bSLuigi Rizzo 		return (revents);
178668b8534bSLuigi Rizzo 	}
178768b8534bSLuigi Rizzo 
1788*f9790aebSLuigi Rizzo 	/*
1789*f9790aebSLuigi Rizzo 	 * If we are in transparent mode, check also the host rx ring
1790*f9790aebSLuigi Rizzo 	 * XXX Transparent mode at the moment requires to bind all
1791*f9790aebSLuigi Rizzo  	 * rings to a single file descriptor.
1792*f9790aebSLuigi Rizzo 	 */
1793091fd0abSLuigi Rizzo 	kring = &na->rx_rings[lim_rx];
1794091fd0abSLuigi Rizzo 	if ( (priv->np_qlast == NETMAP_HW_RING) // XXX check_all
1795091fd0abSLuigi Rizzo 			&& want_rx
1796091fd0abSLuigi Rizzo 			&& (netmap_fwd || kring->ring->flags & NR_FORWARD) ) {
1797091fd0abSLuigi Rizzo 		if (kring->ring->avail == 0)
1798ce3ee1e7SLuigi Rizzo 			netmap_rxsync_from_host(na, td, dev);
1799091fd0abSLuigi Rizzo 		if (kring->ring->avail > 0)
1800091fd0abSLuigi Rizzo 			revents |= want_rx;
1801091fd0abSLuigi Rizzo 	}
1802091fd0abSLuigi Rizzo 
180368b8534bSLuigi Rizzo 	/*
1804*f9790aebSLuigi Rizzo 	 * check_all_{tx|rx} are set if the card has more than one queue AND
1805*f9790aebSLuigi Rizzo 	 * the file descriptor is bound to all of them. If so, we sleep on
1806ce3ee1e7SLuigi Rizzo 	 * the "global" selinfo, otherwise we sleep on individual selinfo
1807ce3ee1e7SLuigi Rizzo 	 * (FreeBSD only allows two selinfo's per file descriptor).
1808ce3ee1e7SLuigi Rizzo 	 * The interrupt routine in the driver wake one or the other
1809ce3ee1e7SLuigi Rizzo 	 * (or both) depending on which clients are active.
181068b8534bSLuigi Rizzo 	 *
181168b8534bSLuigi Rizzo 	 * rxsync() is only called if we run out of buffers on a POLLIN.
181268b8534bSLuigi Rizzo 	 * txsync() is called if we run out of buffers on POLLOUT, or
181368b8534bSLuigi Rizzo 	 * there are pending packets to send. The latter can be disabled
181468b8534bSLuigi Rizzo 	 * passing NETMAP_NO_TX_POLL in the NIOCREG call.
181568b8534bSLuigi Rizzo 	 */
1816954dca4cSLuigi Rizzo 	check_all_tx = (priv->np_qlast == NETMAP_HW_RING) && (lim_tx > 1);
1817954dca4cSLuigi Rizzo 	check_all_rx = (priv->np_qlast == NETMAP_HW_RING) && (lim_rx > 1);
181868b8534bSLuigi Rizzo 
181964ae02c3SLuigi Rizzo 	if (priv->np_qlast != NETMAP_HW_RING) {
182064ae02c3SLuigi Rizzo 		lim_tx = lim_rx = priv->np_qlast;
182164ae02c3SLuigi Rizzo 	}
182264ae02c3SLuigi Rizzo 
182368b8534bSLuigi Rizzo 	/*
1824*f9790aebSLuigi Rizzo 	 * We start with a lock free round which is cheap if we have
1825*f9790aebSLuigi Rizzo 	 * slots available. If this fails, then lock and call the sync
182668b8534bSLuigi Rizzo 	 * routines.
1827*f9790aebSLuigi Rizzo 	 * XXX rather than ring->avail >0 should check that
1828*f9790aebSLuigi Rizzo 	 * ring->cur has not reached hwcur+hwavail
182968b8534bSLuigi Rizzo 	 */
183064ae02c3SLuigi Rizzo 	for (i = priv->np_qfirst; want_rx && i < lim_rx; i++) {
183168b8534bSLuigi Rizzo 		kring = &na->rx_rings[i];
183268b8534bSLuigi Rizzo 		if (kring->ring->avail > 0) {
183368b8534bSLuigi Rizzo 			revents |= want_rx;
183468b8534bSLuigi Rizzo 			want_rx = 0;	/* also breaks the loop */
183568b8534bSLuigi Rizzo 		}
183668b8534bSLuigi Rizzo 	}
183764ae02c3SLuigi Rizzo 	for (i = priv->np_qfirst; want_tx && i < lim_tx; i++) {
183868b8534bSLuigi Rizzo 		kring = &na->tx_rings[i];
183968b8534bSLuigi Rizzo 		if (kring->ring->avail > 0) {
184068b8534bSLuigi Rizzo 			revents |= want_tx;
184168b8534bSLuigi Rizzo 			want_tx = 0;	/* also breaks the loop */
184268b8534bSLuigi Rizzo 		}
184368b8534bSLuigi Rizzo 	}
184468b8534bSLuigi Rizzo 
184568b8534bSLuigi Rizzo 	/*
184668b8534bSLuigi Rizzo 	 * If we to push packets out (priv->np_txpoll) or want_tx is
184768b8534bSLuigi Rizzo 	 * still set, we do need to run the txsync calls (on all rings,
184868b8534bSLuigi Rizzo 	 * to avoid that the tx rings stall).
1849*f9790aebSLuigi Rizzo 	 * XXX should also check cur != hwcur on the tx rings.
1850*f9790aebSLuigi Rizzo 	 * Fortunately, normal tx mode has np_txpoll set.
185168b8534bSLuigi Rizzo 	 */
185268b8534bSLuigi Rizzo 	if (priv->np_txpoll || want_tx) {
1853ce3ee1e7SLuigi Rizzo 		/* If we really want to be woken up (want_tx),
1854ce3ee1e7SLuigi Rizzo 		 * do a selrecord, either on the global or on
1855ce3ee1e7SLuigi Rizzo 		 * the private structure.  Then issue the txsync
1856ce3ee1e7SLuigi Rizzo 		 * so there is no race in the selrecord/selwait
1857ce3ee1e7SLuigi Rizzo 		 */
1858091fd0abSLuigi Rizzo flush_tx:
185964ae02c3SLuigi Rizzo 		for (i = priv->np_qfirst; i < lim_tx; i++) {
186068b8534bSLuigi Rizzo 			kring = &na->tx_rings[i];
18615819da83SLuigi Rizzo 			/*
1862ce3ee1e7SLuigi Rizzo 			 * Skip this ring if want_tx == 0
18635819da83SLuigi Rizzo 			 * (we have already done a successful sync on
18645819da83SLuigi Rizzo 			 * a previous ring) AND kring->cur == kring->hwcur
18655819da83SLuigi Rizzo 			 * (there are no pending transmissions for this ring).
18665819da83SLuigi Rizzo 			 */
186768b8534bSLuigi Rizzo 			if (!want_tx && kring->ring->cur == kring->nr_hwcur)
186868b8534bSLuigi Rizzo 				continue;
1869ce3ee1e7SLuigi Rizzo 			/* make sure only one user thread is doing this */
1870ce3ee1e7SLuigi Rizzo 			if (nm_kr_tryget(kring)) {
1871*f9790aebSLuigi Rizzo 				ND("ring %p busy is %d",
1872*f9790aebSLuigi Rizzo 				    kring, (int)kring->nr_busy);
1873ce3ee1e7SLuigi Rizzo 				revents |= POLLERR;
1874ce3ee1e7SLuigi Rizzo 				goto out;
187568b8534bSLuigi Rizzo 			}
1876ce3ee1e7SLuigi Rizzo 
187768b8534bSLuigi Rizzo 			if (netmap_verbose & NM_VERB_TXSYNC)
187868b8534bSLuigi Rizzo 				D("send %d on %s %d",
1879*f9790aebSLuigi Rizzo 					kring->ring->cur, NM_IFPNAME(ifp), i);
1880*f9790aebSLuigi Rizzo 			if (na->nm_txsync(na, i, 0))
188168b8534bSLuigi Rizzo 				revents |= POLLERR;
188268b8534bSLuigi Rizzo 
1883*f9790aebSLuigi Rizzo 			/* Check avail and call selrecord only if
1884*f9790aebSLuigi Rizzo 			 * called with POLLOUT and run out of bufs.
1885*f9790aebSLuigi Rizzo 			 * XXX Note, we cannot trust much ring->avail
1886*f9790aebSLuigi Rizzo 			 * as it is exposed to userspace (even though
1887*f9790aebSLuigi Rizzo 			 * just updated by txsync). We should really
1888*f9790aebSLuigi Rizzo 			 * check kring->nr_hwavail or better have
1889*f9790aebSLuigi Rizzo 			 * txsync set a flag telling if we need
1890*f9790aebSLuigi Rizzo 			 * to do a selrecord().
1891*f9790aebSLuigi Rizzo 			 */
189268b8534bSLuigi Rizzo 			if (want_tx) {
189368b8534bSLuigi Rizzo 				if (kring->ring->avail > 0) {
189468b8534bSLuigi Rizzo 					/* stop at the first ring. We don't risk
189568b8534bSLuigi Rizzo 					 * starvation.
189668b8534bSLuigi Rizzo 					 */
189768b8534bSLuigi Rizzo 					revents |= want_tx;
189868b8534bSLuigi Rizzo 					want_tx = 0;
189968b8534bSLuigi Rizzo 				}
1900ce3ee1e7SLuigi Rizzo 			}
1901ce3ee1e7SLuigi Rizzo 			nm_kr_put(kring);
1902ce3ee1e7SLuigi Rizzo 		}
1903ce3ee1e7SLuigi Rizzo 		if (want_tx && retry_tx) {
1904954dca4cSLuigi Rizzo 			selrecord(td, check_all_tx ?
1905ce3ee1e7SLuigi Rizzo 			    &na->tx_si : &na->tx_rings[priv->np_qfirst].si);
1906ce3ee1e7SLuigi Rizzo 			retry_tx = 0;
1907ce3ee1e7SLuigi Rizzo 			goto flush_tx;
190868b8534bSLuigi Rizzo 		}
190968b8534bSLuigi Rizzo 	}
191068b8534bSLuigi Rizzo 
191168b8534bSLuigi Rizzo 	/*
191268b8534bSLuigi Rizzo 	 * now if want_rx is still set we need to lock and rxsync.
191368b8534bSLuigi Rizzo 	 * Do it on all rings because otherwise we starve.
191468b8534bSLuigi Rizzo 	 */
191568b8534bSLuigi Rizzo 	if (want_rx) {
1916ce3ee1e7SLuigi Rizzo 		int retry_rx = 1;
1917ce3ee1e7SLuigi Rizzo do_retry_rx:
191864ae02c3SLuigi Rizzo 		for (i = priv->np_qfirst; i < lim_rx; i++) {
191968b8534bSLuigi Rizzo 			kring = &na->rx_rings[i];
1920ce3ee1e7SLuigi Rizzo 
1921ce3ee1e7SLuigi Rizzo 			if (nm_kr_tryget(kring)) {
1922ce3ee1e7SLuigi Rizzo 				revents |= POLLERR;
1923ce3ee1e7SLuigi Rizzo 				goto out;
192468b8534bSLuigi Rizzo 			}
1925ce3ee1e7SLuigi Rizzo 
1926ce3ee1e7SLuigi Rizzo 			/* XXX NR_FORWARD should only be read on
1927ce3ee1e7SLuigi Rizzo 			 * physical or NIC ports
1928ce3ee1e7SLuigi Rizzo 			 */
1929091fd0abSLuigi Rizzo 			if (netmap_fwd ||kring->ring->flags & NR_FORWARD) {
1930091fd0abSLuigi Rizzo 				ND(10, "forwarding some buffers up %d to %d",
1931091fd0abSLuigi Rizzo 				    kring->nr_hwcur, kring->ring->cur);
1932091fd0abSLuigi Rizzo 				netmap_grab_packets(kring, &q, netmap_fwd);
1933091fd0abSLuigi Rizzo 			}
193468b8534bSLuigi Rizzo 
1935*f9790aebSLuigi Rizzo 			if (na->nm_rxsync(na, i, 0))
193668b8534bSLuigi Rizzo 				revents |= POLLERR;
19375819da83SLuigi Rizzo 			if (netmap_no_timestamp == 0 ||
19385819da83SLuigi Rizzo 					kring->ring->flags & NR_TIMESTAMP) {
193968b8534bSLuigi Rizzo 				microtime(&kring->ring->ts);
19405819da83SLuigi Rizzo 			}
194168b8534bSLuigi Rizzo 
1942ce3ee1e7SLuigi Rizzo 			if (kring->ring->avail > 0) {
194368b8534bSLuigi Rizzo 				revents |= want_rx;
1944ce3ee1e7SLuigi Rizzo 				retry_rx = 0;
194568b8534bSLuigi Rizzo 			}
1946ce3ee1e7SLuigi Rizzo 			nm_kr_put(kring);
194768b8534bSLuigi Rizzo 		}
1948ce3ee1e7SLuigi Rizzo 		if (retry_rx) {
1949ce3ee1e7SLuigi Rizzo 			retry_rx = 0;
1950954dca4cSLuigi Rizzo 			selrecord(td, check_all_rx ?
1951ce3ee1e7SLuigi Rizzo 			    &na->rx_si : &na->rx_rings[priv->np_qfirst].si);
1952ce3ee1e7SLuigi Rizzo 			goto do_retry_rx;
1953ce3ee1e7SLuigi Rizzo 		}
195468b8534bSLuigi Rizzo 	}
1955091fd0abSLuigi Rizzo 
1956ce3ee1e7SLuigi Rizzo 	/* forward host to the netmap ring.
1957ce3ee1e7SLuigi Rizzo 	 * I am accessing nr_hwavail without lock, but netmap_transmit
1958ce3ee1e7SLuigi Rizzo 	 * can only increment it, so the operation is safe.
1959ce3ee1e7SLuigi Rizzo 	 */
1960091fd0abSLuigi Rizzo 	kring = &na->rx_rings[lim_rx];
1961091fd0abSLuigi Rizzo 	if ( (priv->np_qlast == NETMAP_HW_RING) // XXX check_all
1962091fd0abSLuigi Rizzo 			&& (netmap_fwd || kring->ring->flags & NR_FORWARD)
1963091fd0abSLuigi Rizzo 			 && kring->nr_hwavail > 0 && !host_forwarded) {
1964091fd0abSLuigi Rizzo 		netmap_sw_to_nic(na);
1965091fd0abSLuigi Rizzo 		host_forwarded = 1; /* prevent another pass */
1966091fd0abSLuigi Rizzo 		want_rx = 0;
1967091fd0abSLuigi Rizzo 		goto flush_tx;
1968091fd0abSLuigi Rizzo 	}
1969091fd0abSLuigi Rizzo 
1970091fd0abSLuigi Rizzo 	if (q.head)
1971*f9790aebSLuigi Rizzo 		netmap_send_up(na->ifp, &q);
197268b8534bSLuigi Rizzo 
1973ce3ee1e7SLuigi Rizzo out:
1974ce3ee1e7SLuigi Rizzo 
197568b8534bSLuigi Rizzo 	return (revents);
197668b8534bSLuigi Rizzo }
197768b8534bSLuigi Rizzo 
197868b8534bSLuigi Rizzo /*------- driver support routines ------*/
197968b8534bSLuigi Rizzo 
1980*f9790aebSLuigi Rizzo static int netmap_hw_krings_create(struct netmap_adapter *);
1981*f9790aebSLuigi Rizzo 
1982*f9790aebSLuigi Rizzo static int
1983*f9790aebSLuigi Rizzo netmap_notify(struct netmap_adapter *na, u_int n_ring, enum txrx tx, int flags)
1984*f9790aebSLuigi Rizzo {
1985*f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
1986*f9790aebSLuigi Rizzo 
1987*f9790aebSLuigi Rizzo 	if (tx == NR_TX) {
1988*f9790aebSLuigi Rizzo 		kring = na->tx_rings + n_ring;
1989*f9790aebSLuigi Rizzo 		selwakeuppri(&kring->si, PI_NET);
1990*f9790aebSLuigi Rizzo 		if (flags & NAF_GLOBAL_NOTIFY)
1991*f9790aebSLuigi Rizzo 			selwakeuppri(&na->tx_si, PI_NET);
1992*f9790aebSLuigi Rizzo 	} else {
1993*f9790aebSLuigi Rizzo 		kring = na->rx_rings + n_ring;
1994*f9790aebSLuigi Rizzo 		selwakeuppri(&kring->si, PI_NET);
1995*f9790aebSLuigi Rizzo 		if (flags & NAF_GLOBAL_NOTIFY)
1996*f9790aebSLuigi Rizzo 			selwakeuppri(&na->rx_si, PI_NET);
1997*f9790aebSLuigi Rizzo 	}
1998*f9790aebSLuigi Rizzo 	return 0;
1999*f9790aebSLuigi Rizzo }
2000*f9790aebSLuigi Rizzo 
2001*f9790aebSLuigi Rizzo 
2002*f9790aebSLuigi Rizzo // XXX check handling of failures
2003*f9790aebSLuigi Rizzo int
2004*f9790aebSLuigi Rizzo netmap_attach_common(struct netmap_adapter *na)
2005*f9790aebSLuigi Rizzo {
2006*f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
2007*f9790aebSLuigi Rizzo 
2008*f9790aebSLuigi Rizzo 	if (na->num_tx_rings == 0 || na->num_rx_rings == 0) {
2009*f9790aebSLuigi Rizzo 		D("%s: invalid rings tx %d rx %d",
2010*f9790aebSLuigi Rizzo 			ifp->if_xname, na->num_tx_rings, na->num_rx_rings);
2011*f9790aebSLuigi Rizzo 		return EINVAL;
2012*f9790aebSLuigi Rizzo 	}
2013*f9790aebSLuigi Rizzo 	WNA(ifp) = na;
2014*f9790aebSLuigi Rizzo 	NETMAP_SET_CAPABLE(ifp);
2015*f9790aebSLuigi Rizzo 	if (na->nm_krings_create == NULL) {
2016*f9790aebSLuigi Rizzo 		na->nm_krings_create = netmap_hw_krings_create;
2017*f9790aebSLuigi Rizzo 		na->nm_krings_delete = netmap_krings_delete;
2018*f9790aebSLuigi Rizzo 	}
2019*f9790aebSLuigi Rizzo 	if (na->nm_notify == NULL)
2020*f9790aebSLuigi Rizzo 		na->nm_notify = netmap_notify;
2021*f9790aebSLuigi Rizzo 	na->active_fds = 0;
2022*f9790aebSLuigi Rizzo 
2023*f9790aebSLuigi Rizzo 	if (na->nm_mem == NULL)
2024*f9790aebSLuigi Rizzo 		na->nm_mem = &nm_mem;
2025*f9790aebSLuigi Rizzo 	return 0;
2026*f9790aebSLuigi Rizzo }
2027*f9790aebSLuigi Rizzo 
2028*f9790aebSLuigi Rizzo 
2029*f9790aebSLuigi Rizzo void
2030*f9790aebSLuigi Rizzo netmap_detach_common(struct netmap_adapter *na)
2031*f9790aebSLuigi Rizzo {
2032*f9790aebSLuigi Rizzo 	if (na->ifp)
2033*f9790aebSLuigi Rizzo 		WNA(na->ifp) = NULL; /* XXX do we need this? */
2034*f9790aebSLuigi Rizzo 
2035*f9790aebSLuigi Rizzo 	if (na->tx_rings) { /* XXX should not happen */
2036*f9790aebSLuigi Rizzo 		D("freeing leftover tx_rings");
2037*f9790aebSLuigi Rizzo 		na->nm_krings_delete(na);
2038*f9790aebSLuigi Rizzo 	}
2039*f9790aebSLuigi Rizzo 	if (na->na_flags & NAF_MEM_OWNER)
2040*f9790aebSLuigi Rizzo 		netmap_mem_private_delete(na->nm_mem);
2041*f9790aebSLuigi Rizzo 	bzero(na, sizeof(*na));
2042*f9790aebSLuigi Rizzo 	free(na, M_DEVBUF);
2043*f9790aebSLuigi Rizzo }
2044*f9790aebSLuigi Rizzo 
2045f18be576SLuigi Rizzo 
204668b8534bSLuigi Rizzo /*
204768b8534bSLuigi Rizzo  * Initialize a ``netmap_adapter`` object created by driver on attach.
204868b8534bSLuigi Rizzo  * We allocate a block of memory with room for a struct netmap_adapter
204968b8534bSLuigi Rizzo  * plus two sets of N+2 struct netmap_kring (where N is the number
205068b8534bSLuigi Rizzo  * of hardware rings):
205168b8534bSLuigi Rizzo  * krings	0..N-1	are for the hardware queues.
205268b8534bSLuigi Rizzo  * kring	N	is for the host stack queue
205368b8534bSLuigi Rizzo  * kring	N+1	is only used for the selinfo for all queues.
205468b8534bSLuigi Rizzo  * Return 0 on success, ENOMEM otherwise.
205564ae02c3SLuigi Rizzo  *
20560bf88954SEd Maste  * By default the receive and transmit adapter ring counts are both initialized
20570bf88954SEd Maste  * to num_queues.  na->num_tx_rings can be set for cards with different tx/rx
205824e57ec9SEd Maste  * setups.
205968b8534bSLuigi Rizzo  */
206068b8534bSLuigi Rizzo int
2061*f9790aebSLuigi Rizzo netmap_attach(struct netmap_adapter *arg)
206268b8534bSLuigi Rizzo {
2063*f9790aebSLuigi Rizzo 	struct netmap_hw_adapter *hwna = NULL;
2064*f9790aebSLuigi Rizzo 	// XXX when is arg == NULL ?
2065ae10d1afSLuigi Rizzo 	struct ifnet *ifp = arg ? arg->ifp : NULL;
206668b8534bSLuigi Rizzo 
2067ae10d1afSLuigi Rizzo 	if (arg == NULL || ifp == NULL)
2068ae10d1afSLuigi Rizzo 		goto fail;
2069*f9790aebSLuigi Rizzo 	hwna = malloc(sizeof(*hwna), M_DEVBUF, M_NOWAIT | M_ZERO);
2070*f9790aebSLuigi Rizzo 	if (hwna == NULL)
2071ae10d1afSLuigi Rizzo 		goto fail;
2072*f9790aebSLuigi Rizzo 	hwna->up = *arg;
2073*f9790aebSLuigi Rizzo 	if (netmap_attach_common(&hwna->up)) {
2074*f9790aebSLuigi Rizzo 		free(hwna, M_DEVBUF);
2075*f9790aebSLuigi Rizzo 		goto fail;
2076*f9790aebSLuigi Rizzo 	}
2077*f9790aebSLuigi Rizzo 	netmap_adapter_get(&hwna->up);
2078*f9790aebSLuigi Rizzo 
207964ae02c3SLuigi Rizzo #ifdef linux
2080f18be576SLuigi Rizzo 	if (ifp->netdev_ops) {
2081f18be576SLuigi Rizzo 		/* prepare a clone of the netdev ops */
2082f18be576SLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28)
2083*f9790aebSLuigi Rizzo 		hwna->nm_ndo.ndo_start_xmit = ifp->netdev_ops;
2084f18be576SLuigi Rizzo #else
2085*f9790aebSLuigi Rizzo 		hwna->nm_ndo = *ifp->netdev_ops;
2086f18be576SLuigi Rizzo #endif
2087f18be576SLuigi Rizzo 	}
2088*f9790aebSLuigi Rizzo 	hwna->nm_ndo.ndo_start_xmit = linux_netmap_start_xmit;
2089ce3ee1e7SLuigi Rizzo #endif /* linux */
2090*f9790aebSLuigi Rizzo 
2091*f9790aebSLuigi Rizzo 	D("success for %s", NM_IFPNAME(ifp));
2092ae10d1afSLuigi Rizzo 	return 0;
209368b8534bSLuigi Rizzo 
2094ae10d1afSLuigi Rizzo fail:
2095*f9790aebSLuigi Rizzo 	D("fail, arg %p ifp %p na %p", arg, ifp, hwna);
2096849bec0eSLuigi Rizzo 	netmap_detach(ifp);
2097*f9790aebSLuigi Rizzo 	return (hwna ? EINVAL : ENOMEM);
209868b8534bSLuigi Rizzo }
209968b8534bSLuigi Rizzo 
210068b8534bSLuigi Rizzo 
2101*f9790aebSLuigi Rizzo void
2102*f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_get)(struct netmap_adapter *na)
2103*f9790aebSLuigi Rizzo {
2104*f9790aebSLuigi Rizzo 	if (!na) {
2105*f9790aebSLuigi Rizzo 		return;
2106*f9790aebSLuigi Rizzo 	}
2107*f9790aebSLuigi Rizzo 
2108*f9790aebSLuigi Rizzo 	refcount_acquire(&na->na_refcount);
2109*f9790aebSLuigi Rizzo }
2110*f9790aebSLuigi Rizzo 
2111*f9790aebSLuigi Rizzo 
2112*f9790aebSLuigi Rizzo /* returns 1 iff the netmap_adapter is destroyed */
2113*f9790aebSLuigi Rizzo int
2114*f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_put)(struct netmap_adapter *na)
2115*f9790aebSLuigi Rizzo {
2116*f9790aebSLuigi Rizzo 	if (!na)
2117*f9790aebSLuigi Rizzo 		return 1;
2118*f9790aebSLuigi Rizzo 
2119*f9790aebSLuigi Rizzo 	if (!refcount_release(&na->na_refcount))
2120*f9790aebSLuigi Rizzo 		return 0;
2121*f9790aebSLuigi Rizzo 
2122*f9790aebSLuigi Rizzo 	if (na->nm_dtor)
2123*f9790aebSLuigi Rizzo 		na->nm_dtor(na);
2124*f9790aebSLuigi Rizzo 
2125*f9790aebSLuigi Rizzo 	netmap_detach_common(na);
2126*f9790aebSLuigi Rizzo 
2127*f9790aebSLuigi Rizzo 	return 1;
2128*f9790aebSLuigi Rizzo }
2129*f9790aebSLuigi Rizzo 
2130*f9790aebSLuigi Rizzo 
2131*f9790aebSLuigi Rizzo int
2132*f9790aebSLuigi Rizzo netmap_hw_krings_create(struct netmap_adapter *na)
2133*f9790aebSLuigi Rizzo {
2134*f9790aebSLuigi Rizzo 	return netmap_krings_create(na,
2135*f9790aebSLuigi Rizzo 		na->num_tx_rings + 1, na->num_rx_rings + 1, 0);
2136*f9790aebSLuigi Rizzo }
2137*f9790aebSLuigi Rizzo 
2138*f9790aebSLuigi Rizzo 
2139*f9790aebSLuigi Rizzo 
214068b8534bSLuigi Rizzo /*
214168b8534bSLuigi Rizzo  * Free the allocated memory linked to the given ``netmap_adapter``
214268b8534bSLuigi Rizzo  * object.
214368b8534bSLuigi Rizzo  */
214468b8534bSLuigi Rizzo void
214568b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp)
214668b8534bSLuigi Rizzo {
214768b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
214868b8534bSLuigi Rizzo 
214968b8534bSLuigi Rizzo 	if (!na)
215068b8534bSLuigi Rizzo 		return;
215168b8534bSLuigi Rizzo 
2152*f9790aebSLuigi Rizzo 	NMG_LOCK();
2153*f9790aebSLuigi Rizzo 	netmap_disable_all_rings(ifp);
2154*f9790aebSLuigi Rizzo 	netmap_adapter_put(na);
2155*f9790aebSLuigi Rizzo 	na->ifp = NULL;
2156*f9790aebSLuigi Rizzo 	netmap_enable_all_rings(ifp);
2157*f9790aebSLuigi Rizzo 	NMG_UNLOCK();
2158ae10d1afSLuigi Rizzo }
2159f18be576SLuigi Rizzo 
2160f18be576SLuigi Rizzo 
216168b8534bSLuigi Rizzo /*
216202ad4083SLuigi Rizzo  * Intercept packets from the network stack and pass them
216302ad4083SLuigi Rizzo  * to netmap as incoming packets on the 'software' ring.
2164ce3ee1e7SLuigi Rizzo  * We rely on the OS to make sure that the ifp and na do not go
2165ce3ee1e7SLuigi Rizzo  * away (typically the caller checks for IFF_DRV_RUNNING or the like).
2166ce3ee1e7SLuigi Rizzo  * In nm_register() or whenever there is a reinitialization,
2167*f9790aebSLuigi Rizzo  * we make sure to make the mode change visible here.
216868b8534bSLuigi Rizzo  */
216968b8534bSLuigi Rizzo int
2170ce3ee1e7SLuigi Rizzo netmap_transmit(struct ifnet *ifp, struct mbuf *m)
217168b8534bSLuigi Rizzo {
217268b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
2173ce3ee1e7SLuigi Rizzo 	struct netmap_kring *kring;
21741a26580eSLuigi Rizzo 	u_int i, len = MBUF_LEN(m);
2175ce3ee1e7SLuigi Rizzo 	u_int error = EBUSY, lim;
217668b8534bSLuigi Rizzo 	struct netmap_slot *slot;
217768b8534bSLuigi Rizzo 
2178ce3ee1e7SLuigi Rizzo 	// XXX [Linux] we do not need this lock
2179ce3ee1e7SLuigi Rizzo 	// if we follow the down/configure/up protocol -gl
2180ce3ee1e7SLuigi Rizzo 	// mtx_lock(&na->core_lock);
2181ce3ee1e7SLuigi Rizzo 	if ( (ifp->if_capenable & IFCAP_NETMAP) == 0) {
2182ce3ee1e7SLuigi Rizzo 		/* interface not in netmap mode anymore */
2183ce3ee1e7SLuigi Rizzo 		error = ENXIO;
2184ce3ee1e7SLuigi Rizzo 		goto done;
2185ce3ee1e7SLuigi Rizzo 	}
2186ce3ee1e7SLuigi Rizzo 
2187ce3ee1e7SLuigi Rizzo 	kring = &na->rx_rings[na->num_rx_rings];
2188ce3ee1e7SLuigi Rizzo 	lim = kring->nkr_num_slots - 1;
218968b8534bSLuigi Rizzo 	if (netmap_verbose & NM_VERB_HOST)
2190*f9790aebSLuigi Rizzo 		D("%s packet %d len %d from the stack", NM_IFPNAME(ifp),
219168b8534bSLuigi Rizzo 			kring->nr_hwcur + kring->nr_hwavail, len);
2192ce3ee1e7SLuigi Rizzo 	// XXX reconsider long packets if we handle fragments
2193ce3ee1e7SLuigi Rizzo 	if (len > NETMAP_BDG_BUF_SIZE(na->nm_mem)) { /* too long for us */
2194*f9790aebSLuigi Rizzo 		D("%s from_host, drop packet size %d > %d", NM_IFPNAME(ifp),
2195ce3ee1e7SLuigi Rizzo 			len, NETMAP_BDG_BUF_SIZE(na->nm_mem));
2196ce3ee1e7SLuigi Rizzo 		goto done;
2197849bec0eSLuigi Rizzo 	}
2198ce3ee1e7SLuigi Rizzo 	/* protect against other instances of netmap_transmit,
2199ce3ee1e7SLuigi Rizzo 	 * and userspace invocations of rxsync().
2200ce3ee1e7SLuigi Rizzo 	 */
2201ce3ee1e7SLuigi Rizzo 	// XXX [Linux] there can be no other instances of netmap_transmit
2202ce3ee1e7SLuigi Rizzo 	// on this same ring, but we still need this lock to protect
2203ce3ee1e7SLuigi Rizzo 	// concurrent access from netmap_sw_to_nic() -gl
2204ce3ee1e7SLuigi Rizzo 	mtx_lock(&kring->q_lock);
220502ad4083SLuigi Rizzo 	if (kring->nr_hwavail >= lim) {
22065b248374SLuigi Rizzo 		if (netmap_verbose)
2207*f9790aebSLuigi Rizzo 			D("stack ring %s full\n", NM_IFPNAME(ifp));
2208ce3ee1e7SLuigi Rizzo 	} else {
220968b8534bSLuigi Rizzo 		/* compute the insert position */
2210ce3ee1e7SLuigi Rizzo 		i = nm_kr_rxpos(kring);
221168b8534bSLuigi Rizzo 		slot = &kring->ring->slot[i];
2212*f9790aebSLuigi Rizzo 		m_copydata(m, 0, (int)len, BDG_NMB(na, slot));
221368b8534bSLuigi Rizzo 		slot->len = len;
2214091fd0abSLuigi Rizzo 		slot->flags = kring->nkr_slot_flags;
221568b8534bSLuigi Rizzo 		kring->nr_hwavail++;
221668b8534bSLuigi Rizzo 		if (netmap_verbose  & NM_VERB_HOST)
2217*f9790aebSLuigi Rizzo 			D("wake up host ring %s %d", NM_IFPNAME(na->ifp), na->num_rx_rings);
2218*f9790aebSLuigi Rizzo 		na->nm_notify(na, na->num_rx_rings, NR_RX, 0);
221968b8534bSLuigi Rizzo 		error = 0;
2220ce3ee1e7SLuigi Rizzo 	}
2221ce3ee1e7SLuigi Rizzo 	mtx_unlock(&kring->q_lock);
2222ce3ee1e7SLuigi Rizzo 
222368b8534bSLuigi Rizzo done:
2224ce3ee1e7SLuigi Rizzo 	// mtx_unlock(&na->core_lock);
222568b8534bSLuigi Rizzo 
222668b8534bSLuigi Rizzo 	/* release the mbuf in either cases of success or failure. As an
222768b8534bSLuigi Rizzo 	 * alternative, put the mbuf in a free list and free the list
222868b8534bSLuigi Rizzo 	 * only when really necessary.
222968b8534bSLuigi Rizzo 	 */
223068b8534bSLuigi Rizzo 	m_freem(m);
223168b8534bSLuigi Rizzo 
223268b8534bSLuigi Rizzo 	return (error);
223368b8534bSLuigi Rizzo }
223468b8534bSLuigi Rizzo 
223568b8534bSLuigi Rizzo 
223668b8534bSLuigi Rizzo /*
223768b8534bSLuigi Rizzo  * netmap_reset() is called by the driver routines when reinitializing
223868b8534bSLuigi Rizzo  * a ring. The driver is in charge of locking to protect the kring.
2239*f9790aebSLuigi Rizzo  * If native netmap mode is not set just return NULL.
224068b8534bSLuigi Rizzo  */
224168b8534bSLuigi Rizzo struct netmap_slot *
2242ce3ee1e7SLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, u_int n,
224368b8534bSLuigi Rizzo 	u_int new_cur)
224468b8534bSLuigi Rizzo {
224568b8534bSLuigi Rizzo 	struct netmap_kring *kring;
2246506cc70cSLuigi Rizzo 	int new_hwofs, lim;
224768b8534bSLuigi Rizzo 
2248ce3ee1e7SLuigi Rizzo 	if (na == NULL) {
2249ce3ee1e7SLuigi Rizzo 		D("NULL na, should not happen");
225068b8534bSLuigi Rizzo 		return NULL;	/* no netmap support here */
2251ce3ee1e7SLuigi Rizzo 	}
2252ce3ee1e7SLuigi Rizzo 	if (!(na->ifp->if_capenable & IFCAP_NETMAP)) {
22535864b3a5SLuigi Rizzo 		ND("interface not in netmap mode");
225468b8534bSLuigi Rizzo 		return NULL;	/* nothing to reinitialize */
2255ce3ee1e7SLuigi Rizzo 	}
225668b8534bSLuigi Rizzo 
2257ce3ee1e7SLuigi Rizzo 	/* XXX note- in the new scheme, we are not guaranteed to be
2258ce3ee1e7SLuigi Rizzo 	 * under lock (e.g. when called on a device reset).
2259ce3ee1e7SLuigi Rizzo 	 * In this case, we should set a flag and do not trust too
2260ce3ee1e7SLuigi Rizzo 	 * much the values. In practice: TODO
2261ce3ee1e7SLuigi Rizzo 	 * - set a RESET flag somewhere in the kring
2262ce3ee1e7SLuigi Rizzo 	 * - do the processing in a conservative way
2263ce3ee1e7SLuigi Rizzo 	 * - let the *sync() fixup at the end.
2264ce3ee1e7SLuigi Rizzo 	 */
226564ae02c3SLuigi Rizzo 	if (tx == NR_TX) {
22668241616dSLuigi Rizzo 		if (n >= na->num_tx_rings)
22678241616dSLuigi Rizzo 			return NULL;
226864ae02c3SLuigi Rizzo 		kring = na->tx_rings + n;
2269506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur - new_cur;
227064ae02c3SLuigi Rizzo 	} else {
22718241616dSLuigi Rizzo 		if (n >= na->num_rx_rings)
22728241616dSLuigi Rizzo 			return NULL;
227364ae02c3SLuigi Rizzo 		kring = na->rx_rings + n;
2274506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur + kring->nr_hwavail - new_cur;
227564ae02c3SLuigi Rizzo 	}
227664ae02c3SLuigi Rizzo 	lim = kring->nkr_num_slots - 1;
2277506cc70cSLuigi Rizzo 	if (new_hwofs > lim)
2278506cc70cSLuigi Rizzo 		new_hwofs -= lim + 1;
2279506cc70cSLuigi Rizzo 
2280ce3ee1e7SLuigi Rizzo 	/* Always set the new offset value and realign the ring. */
2281ce3ee1e7SLuigi Rizzo 	D("%s hwofs %d -> %d, hwavail %d -> %d",
2282ce3ee1e7SLuigi Rizzo 		tx == NR_TX ? "TX" : "RX",
2283ce3ee1e7SLuigi Rizzo 		kring->nkr_hwofs, new_hwofs,
2284ce3ee1e7SLuigi Rizzo 		kring->nr_hwavail,
2285ce3ee1e7SLuigi Rizzo 		tx == NR_TX ? lim : kring->nr_hwavail);
2286506cc70cSLuigi Rizzo 	kring->nkr_hwofs = new_hwofs;
2287506cc70cSLuigi Rizzo 	if (tx == NR_TX)
2288ce3ee1e7SLuigi Rizzo 		kring->nr_hwavail = lim;
2289*f9790aebSLuigi Rizzo 	kring->nr_hwreserved = 0;
2290506cc70cSLuigi Rizzo 
2291f196ce38SLuigi Rizzo #if 0 // def linux
2292f196ce38SLuigi Rizzo 	/* XXX check that the mappings are correct */
2293f196ce38SLuigi Rizzo 	/* need ring_nr, adapter->pdev, direction */
2294f196ce38SLuigi Rizzo 	buffer_info->dma = dma_map_single(&pdev->dev, addr, adapter->rx_buffer_len, DMA_FROM_DEVICE);
2295f196ce38SLuigi Rizzo 	if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) {
2296f196ce38SLuigi Rizzo 		D("error mapping rx netmap buffer %d", i);
2297f196ce38SLuigi Rizzo 		// XXX fix error handling
2298f196ce38SLuigi Rizzo 	}
2299f196ce38SLuigi Rizzo 
2300f196ce38SLuigi Rizzo #endif /* linux */
230168b8534bSLuigi Rizzo 	/*
2302ce3ee1e7SLuigi Rizzo 	 * Wakeup on the individual and global selwait
2303506cc70cSLuigi Rizzo 	 * We do the wakeup here, but the ring is not yet reconfigured.
2304506cc70cSLuigi Rizzo 	 * However, we are under lock so there are no races.
230568b8534bSLuigi Rizzo 	 */
2306*f9790aebSLuigi Rizzo 	na->nm_notify(na, n, tx, NAF_GLOBAL_NOTIFY);
230768b8534bSLuigi Rizzo 	return kring->ring->slot;
230868b8534bSLuigi Rizzo }
230968b8534bSLuigi Rizzo 
231068b8534bSLuigi Rizzo 
2311ce3ee1e7SLuigi Rizzo /*
2312*f9790aebSLuigi Rizzo  * Dispatch rx/tx interrupts to the netmap rings.
2313ce3ee1e7SLuigi Rizzo  *
2314ce3ee1e7SLuigi Rizzo  * "work_done" is non-null on the RX path, NULL for the TX path.
2315ce3ee1e7SLuigi Rizzo  * We rely on the OS to make sure that there is only one active
2316ce3ee1e7SLuigi Rizzo  * instance per queue, and that there is appropriate locking.
2317849bec0eSLuigi Rizzo  *
2318*f9790aebSLuigi Rizzo  * The 'notify' routine depends on what the ring is attached to.
2319*f9790aebSLuigi Rizzo  * - for a netmap file descriptor, do a selwakeup on the individual
2320*f9790aebSLuigi Rizzo  *   waitqueue, plus one on the global one if needed
2321*f9790aebSLuigi Rizzo  * - for a switch, call the proper forwarding routine
2322*f9790aebSLuigi Rizzo  * - XXX more ?
2323*f9790aebSLuigi Rizzo  */
2324*f9790aebSLuigi Rizzo void
2325*f9790aebSLuigi Rizzo netmap_common_irq(struct ifnet *ifp, u_int q, u_int *work_done)
2326*f9790aebSLuigi Rizzo {
2327*f9790aebSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
2328*f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
2329*f9790aebSLuigi Rizzo 
2330*f9790aebSLuigi Rizzo 	q &= NETMAP_RING_MASK;
2331*f9790aebSLuigi Rizzo 
2332*f9790aebSLuigi Rizzo 	if (netmap_verbose) {
2333*f9790aebSLuigi Rizzo 	        RD(5, "received %s queue %d", work_done ? "RX" : "TX" , q);
2334*f9790aebSLuigi Rizzo 	}
2335*f9790aebSLuigi Rizzo 
2336*f9790aebSLuigi Rizzo 	if (work_done) { /* RX path */
2337*f9790aebSLuigi Rizzo 		if (q >= na->num_rx_rings)
2338*f9790aebSLuigi Rizzo 			return;	// not a physical queue
2339*f9790aebSLuigi Rizzo 		kring = na->rx_rings + q;
2340*f9790aebSLuigi Rizzo 		kring->nr_kflags |= NKR_PENDINTR;	// XXX atomic ?
2341*f9790aebSLuigi Rizzo 		na->nm_notify(na, q, NR_RX,
2342*f9790aebSLuigi Rizzo 			(na->num_rx_rings > 1 ? NAF_GLOBAL_NOTIFY : 0));
2343*f9790aebSLuigi Rizzo 		*work_done = 1; /* do not fire napi again */
2344*f9790aebSLuigi Rizzo 	} else { /* TX path */
2345*f9790aebSLuigi Rizzo 		if (q >= na->num_tx_rings)
2346*f9790aebSLuigi Rizzo 			return;	// not a physical queue
2347*f9790aebSLuigi Rizzo 		kring = na->tx_rings + q;
2348*f9790aebSLuigi Rizzo 		na->nm_notify(na, q, NR_TX,
2349*f9790aebSLuigi Rizzo 			(na->num_tx_rings > 1 ? NAF_GLOBAL_NOTIFY : 0));
2350*f9790aebSLuigi Rizzo 	}
2351*f9790aebSLuigi Rizzo }
2352*f9790aebSLuigi Rizzo 
2353*f9790aebSLuigi Rizzo /*
2354*f9790aebSLuigi Rizzo  * Default functions to handle rx/tx interrupts from a physical device.
2355*f9790aebSLuigi Rizzo  * "work_done" is non-null on the RX path, NULL for the TX path.
2356*f9790aebSLuigi Rizzo  *
2357ce3ee1e7SLuigi Rizzo  * If the card is not in netmap mode, simply return 0,
2358ce3ee1e7SLuigi Rizzo  * so that the caller proceeds with regular processing.
2359*f9790aebSLuigi Rizzo  * Otherwise call netmap_common_irq() and return 1.
2360ce3ee1e7SLuigi Rizzo  *
2361ce3ee1e7SLuigi Rizzo  * If the card is connected to a netmap file descriptor,
2362ce3ee1e7SLuigi Rizzo  * do a selwakeup on the individual queue, plus one on the global one
2363ce3ee1e7SLuigi Rizzo  * if needed (multiqueue card _and_ there are multiqueue listeners),
2364ce3ee1e7SLuigi Rizzo  * and return 1.
2365ce3ee1e7SLuigi Rizzo  *
2366ce3ee1e7SLuigi Rizzo  * Finally, if called on rx from an interface connected to a switch,
2367ce3ee1e7SLuigi Rizzo  * calls the proper forwarding routine, and return 1.
23681a26580eSLuigi Rizzo  */
2369babc7c12SLuigi Rizzo int
2370ce3ee1e7SLuigi Rizzo netmap_rx_irq(struct ifnet *ifp, u_int q, u_int *work_done)
23711a26580eSLuigi Rizzo {
2372*f9790aebSLuigi Rizzo 	// XXX could we check NAF_NATIVE_ON ?
23731a26580eSLuigi Rizzo 	if (!(ifp->if_capenable & IFCAP_NETMAP))
23741a26580eSLuigi Rizzo 		return 0;
2375849bec0eSLuigi Rizzo 
2376*f9790aebSLuigi Rizzo 	if (NA(ifp)->na_flags & NAF_SKIP_INTR) {
23778241616dSLuigi Rizzo 		ND("use regular interrupt");
23788241616dSLuigi Rizzo 		return 0;
23798241616dSLuigi Rizzo 	}
23808241616dSLuigi Rizzo 
2381*f9790aebSLuigi Rizzo 	netmap_common_irq(ifp, q, work_done);
23821a26580eSLuigi Rizzo 	return 1;
23831a26580eSLuigi Rizzo }
23841a26580eSLuigi Rizzo 
238564ae02c3SLuigi Rizzo 
238601c7d25fSLuigi Rizzo /*
2387*f9790aebSLuigi Rizzo  * Module loader and unloader
2388f196ce38SLuigi Rizzo  *
2389*f9790aebSLuigi Rizzo  * netmap_init() creates the /dev/netmap device and initializes
2390*f9790aebSLuigi Rizzo  * all global variables. Returns 0 on success, errno on failure
2391*f9790aebSLuigi Rizzo  * (but there is no chance)
2392*f9790aebSLuigi Rizzo  *
2393*f9790aebSLuigi Rizzo  * netmap_fini() destroys everything.
2394f196ce38SLuigi Rizzo  */
2395babc7c12SLuigi Rizzo 
2396babc7c12SLuigi Rizzo static struct cdev *netmap_dev; /* /dev/netmap character device. */
2397*f9790aebSLuigi Rizzo extern struct cdevsw netmap_cdevsw;
2398babc7c12SLuigi Rizzo 
2399*f9790aebSLuigi Rizzo void
240068b8534bSLuigi Rizzo netmap_fini(void)
240168b8534bSLuigi Rizzo {
2402*f9790aebSLuigi Rizzo 	// XXX destroy_bridges() ?
2403*f9790aebSLuigi Rizzo 	if (netmap_dev)
240468b8534bSLuigi Rizzo 		destroy_dev(netmap_dev);
2405ce3ee1e7SLuigi Rizzo 	netmap_mem_fini();
2406ce3ee1e7SLuigi Rizzo 	NMG_LOCK_DESTROY();
240768b8534bSLuigi Rizzo 	printf("netmap: unloaded module.\n");
240868b8534bSLuigi Rizzo }
240968b8534bSLuigi Rizzo 
2410*f9790aebSLuigi Rizzo int
2411*f9790aebSLuigi Rizzo netmap_init(void)
241268b8534bSLuigi Rizzo {
2413*f9790aebSLuigi Rizzo 	int error;
241468b8534bSLuigi Rizzo 
2415*f9790aebSLuigi Rizzo 	NMG_LOCK_INIT();
241668b8534bSLuigi Rizzo 
2417*f9790aebSLuigi Rizzo 	error = netmap_mem_init();
2418*f9790aebSLuigi Rizzo 	if (error != 0)
2419*f9790aebSLuigi Rizzo 		goto fail;
2420*f9790aebSLuigi Rizzo 	/* XXX could use make_dev_credv() to get error number */
2421*f9790aebSLuigi Rizzo 	netmap_dev = make_dev(&netmap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0660,
2422*f9790aebSLuigi Rizzo 			      "netmap");
2423*f9790aebSLuigi Rizzo 	if (!netmap_dev)
2424*f9790aebSLuigi Rizzo 		goto fail;
2425*f9790aebSLuigi Rizzo 
2426*f9790aebSLuigi Rizzo 	netmap_init_bridges();
2427*f9790aebSLuigi Rizzo 	printf("netmap: loaded module\n");
2428*f9790aebSLuigi Rizzo 	return (0);
2429*f9790aebSLuigi Rizzo fail:
243068b8534bSLuigi Rizzo 	netmap_fini();
2431*f9790aebSLuigi Rizzo 	return (EINVAL); /* may be incorrect */
243268b8534bSLuigi Rizzo }
2433