xref: /freebsd/sys/dev/netmap/netmap.c (revision 997b054cf10b391fd4796b5e92c286ebd1fb1405)
168b8534bSLuigi Rizzo /*
217885a7bSLuigi Rizzo  * Copyright (C) 2011-2014 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 /*
28f9790aebSLuigi Rizzo  * $FreeBSD$
29f9790aebSLuigi 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 */
139f9790aebSLuigi Rizzo #include <sys/conf.h>	/* cdevsw struct, UID, GID */
14089e3fd52SLuigi Rizzo #include <sys/filio.h>	/* FIONBIO */
14168b8534bSLuigi Rizzo #include <sys/sockio.h>
14268b8534bSLuigi Rizzo #include <sys/socketvar.h>	/* struct socket */
14368b8534bSLuigi Rizzo #include <sys/malloc.h>
14468b8534bSLuigi Rizzo #include <sys/poll.h>
14589f6b863SAttilio Rao #include <sys/rwlock.h>
14668b8534bSLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
14768b8534bSLuigi Rizzo #include <sys/selinfo.h>
14868b8534bSLuigi Rizzo #include <sys/sysctl.h>
149339f59c0SGleb Smirnoff #include <sys/jail.h>
150339f59c0SGleb Smirnoff #include <net/vnet.h>
15168b8534bSLuigi Rizzo #include <net/if.h>
15276039bc8SGleb Smirnoff #include <net/if_var.h>
15368b8534bSLuigi Rizzo #include <net/bpf.h>		/* BIOCIMMEDIATE */
15468b8534bSLuigi Rizzo #include <machine/bus.h>	/* bus_dmamap_* */
155ce3ee1e7SLuigi Rizzo #include <sys/endian.h>
156ce3ee1e7SLuigi Rizzo #include <sys/refcount.h>
15768b8534bSLuigi Rizzo 
15868b8534bSLuigi Rizzo 
159f9790aebSLuigi Rizzo /* reduce conditional code */
160f0ea3689SLuigi Rizzo // linux API, use for the knlist in FreeBSD
161f0ea3689SLuigi Rizzo #define init_waitqueue_head(x)	knlist_init_mtx(&(x)->si_note, NULL)
162ce3ee1e7SLuigi Rizzo 
163f0ea3689SLuigi Rizzo void freebsd_selwakeup(struct selinfo *si, int pri);
164f0ea3689SLuigi Rizzo #define OS_selwakeup(a, b)	freebsd_selwakeup(a, b)
165ce3ee1e7SLuigi Rizzo 
166ce3ee1e7SLuigi Rizzo #elif defined(linux)
167ce3ee1e7SLuigi Rizzo 
168ce3ee1e7SLuigi Rizzo #include "bsd_glue.h"
169ce3ee1e7SLuigi Rizzo 
170ce3ee1e7SLuigi Rizzo 
171ce3ee1e7SLuigi Rizzo 
172ce3ee1e7SLuigi Rizzo #elif defined(__APPLE__)
173ce3ee1e7SLuigi Rizzo 
174ce3ee1e7SLuigi Rizzo #warning OSX support is only partial
175ce3ee1e7SLuigi Rizzo #include "osx_glue.h"
176ce3ee1e7SLuigi Rizzo 
177ce3ee1e7SLuigi Rizzo #else
178ce3ee1e7SLuigi Rizzo 
179ce3ee1e7SLuigi Rizzo #error	Unsupported platform
180ce3ee1e7SLuigi Rizzo 
181ce3ee1e7SLuigi Rizzo #endif /* unsupported */
182ce3ee1e7SLuigi Rizzo 
183ce3ee1e7SLuigi Rizzo /*
184ce3ee1e7SLuigi Rizzo  * common headers
185ce3ee1e7SLuigi Rizzo  */
1860b8ed8e0SLuigi Rizzo #include <net/netmap.h>
1870b8ed8e0SLuigi Rizzo #include <dev/netmap/netmap_kern.h>
188ce3ee1e7SLuigi Rizzo #include <dev/netmap/netmap_mem2.h>
1890b8ed8e0SLuigi Rizzo 
190ce3ee1e7SLuigi Rizzo 
191ce3ee1e7SLuigi Rizzo MALLOC_DEFINE(M_NETMAP, "netmap", "Network memory map");
192ce3ee1e7SLuigi Rizzo 
193ce3ee1e7SLuigi Rizzo /*
194ce3ee1e7SLuigi Rizzo  * The following variables are used by the drivers and replicate
195ce3ee1e7SLuigi Rizzo  * fields in the global memory pool. They only refer to buffers
196ce3ee1e7SLuigi Rizzo  * used by physical interfaces.
197ce3ee1e7SLuigi Rizzo  */
1985819da83SLuigi Rizzo u_int netmap_total_buffers;
1998241616dSLuigi Rizzo u_int netmap_buf_size;
200ce3ee1e7SLuigi Rizzo char *netmap_buffer_base;	/* also address of an invalid buffer */
2015819da83SLuigi Rizzo 
2025819da83SLuigi Rizzo /* user-controlled variables */
2035819da83SLuigi Rizzo int netmap_verbose;
2045819da83SLuigi Rizzo 
2055819da83SLuigi Rizzo static int netmap_no_timestamp; /* don't timestamp on rxsync */
2065819da83SLuigi Rizzo 
2075819da83SLuigi Rizzo SYSCTL_NODE(_dev, OID_AUTO, netmap, CTLFLAG_RW, 0, "Netmap args");
2085819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, verbose,
2095819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_verbose, 0, "Verbose mode");
2105819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_timestamp,
2115819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_timestamp, 0, "no_timestamp");
2125819da83SLuigi Rizzo int netmap_mitigate = 1;
2135819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mitigate, CTLFLAG_RW, &netmap_mitigate, 0, "");
214c85cb1a0SLuigi Rizzo int netmap_no_pendintr = 1;
2155819da83SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, no_pendintr,
2165819da83SLuigi Rizzo     CTLFLAG_RW, &netmap_no_pendintr, 0, "Always look for new received packets.");
217f18be576SLuigi Rizzo int netmap_txsync_retry = 2;
218f18be576SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, txsync_retry, CTLFLAG_RW,
219f18be576SLuigi Rizzo     &netmap_txsync_retry, 0 , "Number of txsync loops in bridge's flush.");
2205819da83SLuigi Rizzo 
221f196ce38SLuigi Rizzo int netmap_flags = 0;	/* debug flags */
222091fd0abSLuigi Rizzo int netmap_fwd = 0;	/* force transparent mode */
223ce3ee1e7SLuigi Rizzo int netmap_mmap_unreg = 0; /* allow mmap of unregistered fds */
224f196ce38SLuigi Rizzo 
225f9790aebSLuigi Rizzo /*
226f9790aebSLuigi Rizzo  * netmap_admode selects the netmap mode to use.
227f9790aebSLuigi Rizzo  * Invalid values are reset to NETMAP_ADMODE_BEST
228f9790aebSLuigi Rizzo  */
229f9790aebSLuigi Rizzo enum { NETMAP_ADMODE_BEST = 0,	/* use native, fallback to generic */
230f9790aebSLuigi Rizzo 	NETMAP_ADMODE_NATIVE,	/* either native or none */
231f9790aebSLuigi Rizzo 	NETMAP_ADMODE_GENERIC,	/* force generic */
232f9790aebSLuigi Rizzo 	NETMAP_ADMODE_LAST };
233f9790aebSLuigi Rizzo static int netmap_admode = NETMAP_ADMODE_BEST;
234f9790aebSLuigi Rizzo 
235f9790aebSLuigi Rizzo int netmap_generic_mit = 100*1000;   /* Generic mitigation interval in nanoseconds. */
236f9790aebSLuigi Rizzo int netmap_generic_ringsize = 1024;   /* Generic ringsize. */
237f0ea3689SLuigi Rizzo int netmap_generic_rings = 1;   /* number of queues in generic. */
238f9790aebSLuigi Rizzo 
239f196ce38SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, flags, CTLFLAG_RW, &netmap_flags, 0 , "");
240091fd0abSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, fwd, CTLFLAG_RW, &netmap_fwd, 0 , "");
241ce3ee1e7SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, mmap_unreg, CTLFLAG_RW, &netmap_mmap_unreg, 0, "");
242f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, admode, CTLFLAG_RW, &netmap_admode, 0 , "");
243f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, generic_mit, CTLFLAG_RW, &netmap_generic_mit, 0 , "");
244f9790aebSLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, generic_ringsize, CTLFLAG_RW, &netmap_generic_ringsize, 0 , "");
245f0ea3689SLuigi Rizzo SYSCTL_INT(_dev_netmap, OID_AUTO, generic_rings, CTLFLAG_RW, &netmap_generic_rings, 0 , "");
246f196ce38SLuigi Rizzo 
247ce3ee1e7SLuigi Rizzo NMG_LOCK_T	netmap_global_lock;
248ce3ee1e7SLuigi Rizzo 
249ce3ee1e7SLuigi Rizzo 
250f9790aebSLuigi Rizzo static void
251f9790aebSLuigi Rizzo nm_kr_get(struct netmap_kring *kr)
252ce3ee1e7SLuigi Rizzo {
253ce3ee1e7SLuigi Rizzo 	while (NM_ATOMIC_TEST_AND_SET(&kr->nr_busy))
254ce3ee1e7SLuigi Rizzo 		tsleep(kr, 0, "NM_KR_GET", 4);
255ce3ee1e7SLuigi Rizzo }
256ce3ee1e7SLuigi Rizzo 
257f9790aebSLuigi Rizzo 
25817885a7bSLuigi Rizzo /*
25917885a7bSLuigi Rizzo  * mark the ring as stopped, and run through the locks
26017885a7bSLuigi Rizzo  * to make sure other users get to see it.
26117885a7bSLuigi Rizzo  */
262f9790aebSLuigi Rizzo void
263f9790aebSLuigi Rizzo netmap_disable_ring(struct netmap_kring *kr)
264ce3ee1e7SLuigi Rizzo {
265ce3ee1e7SLuigi Rizzo 	kr->nkr_stopped = 1;
266ce3ee1e7SLuigi Rizzo 	nm_kr_get(kr);
267ce3ee1e7SLuigi Rizzo 	mtx_lock(&kr->q_lock);
268ce3ee1e7SLuigi Rizzo 	mtx_unlock(&kr->q_lock);
269ce3ee1e7SLuigi Rizzo 	nm_kr_put(kr);
270ce3ee1e7SLuigi Rizzo }
271ce3ee1e7SLuigi Rizzo 
272f9790aebSLuigi Rizzo 
27389cc2556SLuigi Rizzo /* stop or enable all the rings of na */
274f9790aebSLuigi Rizzo static void
275f9790aebSLuigi Rizzo netmap_set_all_rings(struct ifnet *ifp, int stopped)
276ce3ee1e7SLuigi Rizzo {
277ce3ee1e7SLuigi Rizzo 	struct netmap_adapter *na;
278ce3ee1e7SLuigi Rizzo 	int i;
279f0ea3689SLuigi Rizzo 	u_int ntx, nrx;
280ce3ee1e7SLuigi Rizzo 
281ce3ee1e7SLuigi Rizzo 	if (!(ifp->if_capenable & IFCAP_NETMAP))
282ce3ee1e7SLuigi Rizzo 		return;
283ce3ee1e7SLuigi Rizzo 
284ce3ee1e7SLuigi Rizzo 	na = NA(ifp);
285ce3ee1e7SLuigi Rizzo 
286f0ea3689SLuigi Rizzo 	ntx = netmap_real_tx_rings(na);
287f0ea3689SLuigi Rizzo 	nrx = netmap_real_rx_rings(na);
288f0ea3689SLuigi Rizzo 
289f0ea3689SLuigi Rizzo 	for (i = 0; i < ntx; i++) {
290f9790aebSLuigi Rizzo 		if (stopped)
291f9790aebSLuigi Rizzo 			netmap_disable_ring(na->tx_rings + i);
292f9790aebSLuigi Rizzo 		else
293ce3ee1e7SLuigi Rizzo 			na->tx_rings[i].nkr_stopped = 0;
294f0ea3689SLuigi Rizzo 		na->nm_notify(na, i, NR_TX, NAF_DISABLE_NOTIFY);
295ce3ee1e7SLuigi Rizzo 	}
296f9790aebSLuigi Rizzo 
297f0ea3689SLuigi Rizzo 	for (i = 0; i < nrx; i++) {
298f9790aebSLuigi Rizzo 		if (stopped)
299f9790aebSLuigi Rizzo 			netmap_disable_ring(na->rx_rings + i);
300f9790aebSLuigi Rizzo 		else
301ce3ee1e7SLuigi Rizzo 			na->rx_rings[i].nkr_stopped = 0;
302f0ea3689SLuigi Rizzo 		na->nm_notify(na, i, NR_RX, NAF_DISABLE_NOTIFY);
303ce3ee1e7SLuigi Rizzo 	}
304ce3ee1e7SLuigi Rizzo }
305ce3ee1e7SLuigi Rizzo 
306ce3ee1e7SLuigi Rizzo 
30789cc2556SLuigi Rizzo /*
30889cc2556SLuigi Rizzo  * Convenience function used in drivers.  Waits for current txsync()s/rxsync()s
30989cc2556SLuigi Rizzo  * to finish and prevents any new one from starting.  Call this before turning
31089cc2556SLuigi Rizzo  * netmap mode off, or before removing the harware rings (e.g., on module
31189cc2556SLuigi Rizzo  * onload).  As a rule of thumb for linux drivers, this should be placed near
31289cc2556SLuigi Rizzo  * each napi_disable().
31389cc2556SLuigi Rizzo  */
314f9790aebSLuigi Rizzo void
315f9790aebSLuigi Rizzo netmap_disable_all_rings(struct ifnet *ifp)
316f9790aebSLuigi Rizzo {
317f9790aebSLuigi Rizzo 	netmap_set_all_rings(ifp, 1 /* stopped */);
318f9790aebSLuigi Rizzo }
319f9790aebSLuigi Rizzo 
320f9790aebSLuigi Rizzo 
32189cc2556SLuigi Rizzo /*
32289cc2556SLuigi Rizzo  * Convenience function used in drivers.  Re-enables rxsync and txsync on the
32389cc2556SLuigi Rizzo  * adapter's rings In linux drivers, this should be placed near each
32489cc2556SLuigi Rizzo  * napi_enable().
32589cc2556SLuigi Rizzo  */
326f9790aebSLuigi Rizzo void
327f9790aebSLuigi Rizzo netmap_enable_all_rings(struct ifnet *ifp)
328f9790aebSLuigi Rizzo {
329f9790aebSLuigi Rizzo 	netmap_set_all_rings(ifp, 0 /* enabled */);
330f9790aebSLuigi Rizzo }
331f9790aebSLuigi Rizzo 
332f9790aebSLuigi Rizzo 
333ce3ee1e7SLuigi Rizzo /*
334ce3ee1e7SLuigi Rizzo  * generic bound_checking function
335ce3ee1e7SLuigi Rizzo  */
336ce3ee1e7SLuigi Rizzo u_int
337ce3ee1e7SLuigi Rizzo nm_bound_var(u_int *v, u_int dflt, u_int lo, u_int hi, const char *msg)
338ce3ee1e7SLuigi Rizzo {
339ce3ee1e7SLuigi Rizzo 	u_int oldv = *v;
340ce3ee1e7SLuigi Rizzo 	const char *op = NULL;
341ce3ee1e7SLuigi Rizzo 
342ce3ee1e7SLuigi Rizzo 	if (dflt < lo)
343ce3ee1e7SLuigi Rizzo 		dflt = lo;
344ce3ee1e7SLuigi Rizzo 	if (dflt > hi)
345ce3ee1e7SLuigi Rizzo 		dflt = hi;
346ce3ee1e7SLuigi Rizzo 	if (oldv < lo) {
347ce3ee1e7SLuigi Rizzo 		*v = dflt;
348ce3ee1e7SLuigi Rizzo 		op = "Bump";
349ce3ee1e7SLuigi Rizzo 	} else if (oldv > hi) {
350ce3ee1e7SLuigi Rizzo 		*v = hi;
351ce3ee1e7SLuigi Rizzo 		op = "Clamp";
352ce3ee1e7SLuigi Rizzo 	}
353ce3ee1e7SLuigi Rizzo 	if (op && msg)
354ce3ee1e7SLuigi Rizzo 		printf("%s %s to %d (was %d)\n", op, msg, *v, oldv);
355ce3ee1e7SLuigi Rizzo 	return *v;
356ce3ee1e7SLuigi Rizzo }
357ce3ee1e7SLuigi Rizzo 
358f9790aebSLuigi Rizzo 
359ce3ee1e7SLuigi Rizzo /*
360ce3ee1e7SLuigi Rizzo  * packet-dump function, user-supplied or static buffer.
361ce3ee1e7SLuigi Rizzo  * The destination buffer must be at least 30+4*len
362ce3ee1e7SLuigi Rizzo  */
363ce3ee1e7SLuigi Rizzo const char *
364ce3ee1e7SLuigi Rizzo nm_dump_buf(char *p, int len, int lim, char *dst)
365ce3ee1e7SLuigi Rizzo {
366ce3ee1e7SLuigi Rizzo 	static char _dst[8192];
367ce3ee1e7SLuigi Rizzo 	int i, j, i0;
368ce3ee1e7SLuigi Rizzo 	static char hex[] ="0123456789abcdef";
369ce3ee1e7SLuigi Rizzo 	char *o;	/* output position */
370ce3ee1e7SLuigi Rizzo 
371ce3ee1e7SLuigi Rizzo #define P_HI(x)	hex[((x) & 0xf0)>>4]
372ce3ee1e7SLuigi Rizzo #define P_LO(x)	hex[((x) & 0xf)]
373ce3ee1e7SLuigi Rizzo #define P_C(x)	((x) >= 0x20 && (x) <= 0x7e ? (x) : '.')
374ce3ee1e7SLuigi Rizzo 	if (!dst)
375ce3ee1e7SLuigi Rizzo 		dst = _dst;
376ce3ee1e7SLuigi Rizzo 	if (lim <= 0 || lim > len)
377ce3ee1e7SLuigi Rizzo 		lim = len;
378ce3ee1e7SLuigi Rizzo 	o = dst;
379ce3ee1e7SLuigi Rizzo 	sprintf(o, "buf 0x%p len %d lim %d\n", p, len, lim);
380ce3ee1e7SLuigi Rizzo 	o += strlen(o);
381ce3ee1e7SLuigi Rizzo 	/* hexdump routine */
382ce3ee1e7SLuigi Rizzo 	for (i = 0; i < lim; ) {
383ce3ee1e7SLuigi Rizzo 		sprintf(o, "%5d: ", i);
384ce3ee1e7SLuigi Rizzo 		o += strlen(o);
385ce3ee1e7SLuigi Rizzo 		memset(o, ' ', 48);
386ce3ee1e7SLuigi Rizzo 		i0 = i;
387ce3ee1e7SLuigi Rizzo 		for (j=0; j < 16 && i < lim; i++, j++) {
388ce3ee1e7SLuigi Rizzo 			o[j*3] = P_HI(p[i]);
389ce3ee1e7SLuigi Rizzo 			o[j*3+1] = P_LO(p[i]);
390ce3ee1e7SLuigi Rizzo 		}
391ce3ee1e7SLuigi Rizzo 		i = i0;
392ce3ee1e7SLuigi Rizzo 		for (j=0; j < 16 && i < lim; i++, j++)
393ce3ee1e7SLuigi Rizzo 			o[j + 48] = P_C(p[i]);
394ce3ee1e7SLuigi Rizzo 		o[j+48] = '\n';
395ce3ee1e7SLuigi Rizzo 		o += j+49;
396ce3ee1e7SLuigi Rizzo 	}
397ce3ee1e7SLuigi Rizzo 	*o = '\0';
398ce3ee1e7SLuigi Rizzo #undef P_HI
399ce3ee1e7SLuigi Rizzo #undef P_LO
400ce3ee1e7SLuigi Rizzo #undef P_C
401ce3ee1e7SLuigi Rizzo 	return dst;
402ce3ee1e7SLuigi Rizzo }
403f196ce38SLuigi Rizzo 
404f18be576SLuigi Rizzo 
405ae10d1afSLuigi Rizzo /*
406ae10d1afSLuigi Rizzo  * Fetch configuration from the device, to cope with dynamic
407ae10d1afSLuigi Rizzo  * reconfigurations after loading the module.
408ae10d1afSLuigi Rizzo  */
40989cc2556SLuigi Rizzo /* call with NMG_LOCK held */
410f9790aebSLuigi Rizzo int
411ae10d1afSLuigi Rizzo netmap_update_config(struct netmap_adapter *na)
412ae10d1afSLuigi Rizzo {
413ae10d1afSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
414ae10d1afSLuigi Rizzo 	u_int txr, txd, rxr, rxd;
415ae10d1afSLuigi Rizzo 
416ae10d1afSLuigi Rizzo 	txr = txd = rxr = rxd = 0;
417ae10d1afSLuigi Rizzo 	if (na->nm_config) {
418f9790aebSLuigi Rizzo 		na->nm_config(na, &txr, &txd, &rxr, &rxd);
419ae10d1afSLuigi Rizzo 	} else {
420ae10d1afSLuigi Rizzo 		/* take whatever we had at init time */
421ae10d1afSLuigi Rizzo 		txr = na->num_tx_rings;
422ae10d1afSLuigi Rizzo 		txd = na->num_tx_desc;
423ae10d1afSLuigi Rizzo 		rxr = na->num_rx_rings;
424ae10d1afSLuigi Rizzo 		rxd = na->num_rx_desc;
425ae10d1afSLuigi Rizzo 	}
426ae10d1afSLuigi Rizzo 
427ae10d1afSLuigi Rizzo 	if (na->num_tx_rings == txr && na->num_tx_desc == txd &&
428ae10d1afSLuigi Rizzo 	    na->num_rx_rings == rxr && na->num_rx_desc == rxd)
429ae10d1afSLuigi Rizzo 		return 0; /* nothing changed */
430f9790aebSLuigi Rizzo 	if (netmap_verbose || na->active_fds > 0) {
431ae10d1afSLuigi Rizzo 		D("stored config %s: txring %d x %d, rxring %d x %d",
432f9790aebSLuigi Rizzo 			NM_IFPNAME(ifp),
433ae10d1afSLuigi Rizzo 			na->num_tx_rings, na->num_tx_desc,
434ae10d1afSLuigi Rizzo 			na->num_rx_rings, na->num_rx_desc);
435ae10d1afSLuigi Rizzo 		D("new config %s: txring %d x %d, rxring %d x %d",
436f9790aebSLuigi Rizzo 			NM_IFPNAME(ifp), txr, txd, rxr, rxd);
437ae10d1afSLuigi Rizzo 	}
438f9790aebSLuigi Rizzo 	if (na->active_fds == 0) {
439ae10d1afSLuigi Rizzo 		D("configuration changed (but fine)");
440ae10d1afSLuigi Rizzo 		na->num_tx_rings = txr;
441ae10d1afSLuigi Rizzo 		na->num_tx_desc = txd;
442ae10d1afSLuigi Rizzo 		na->num_rx_rings = rxr;
443ae10d1afSLuigi Rizzo 		na->num_rx_desc = rxd;
444ae10d1afSLuigi Rizzo 		return 0;
445ae10d1afSLuigi Rizzo 	}
446ae10d1afSLuigi Rizzo 	D("configuration changed while active, this is bad...");
447ae10d1afSLuigi Rizzo 	return 1;
448ae10d1afSLuigi Rizzo }
449ae10d1afSLuigi Rizzo 
450f0ea3689SLuigi Rizzo static int
451f0ea3689SLuigi Rizzo netmap_txsync_compat(struct netmap_kring *kring, int flags)
452f0ea3689SLuigi Rizzo {
453f0ea3689SLuigi Rizzo 	struct netmap_adapter *na = kring->na;
454f0ea3689SLuigi Rizzo 	return na->nm_txsync(na, kring->ring_id, flags);
455f0ea3689SLuigi Rizzo }
456f9790aebSLuigi Rizzo 
457f0ea3689SLuigi Rizzo static int
458f0ea3689SLuigi Rizzo netmap_rxsync_compat(struct netmap_kring *kring, int flags)
459f0ea3689SLuigi Rizzo {
460f0ea3689SLuigi Rizzo 	struct netmap_adapter *na = kring->na;
461f0ea3689SLuigi Rizzo 	return na->nm_rxsync(na, kring->ring_id, flags);
462f0ea3689SLuigi Rizzo }
463f0ea3689SLuigi Rizzo 
46489cc2556SLuigi Rizzo /* kring->nm_sync callback for the host tx ring */
465f0ea3689SLuigi Rizzo static int
466f0ea3689SLuigi Rizzo netmap_txsync_to_host_compat(struct netmap_kring *kring, int flags)
467f0ea3689SLuigi Rizzo {
46889cc2556SLuigi Rizzo 	(void)flags; /* unused */
469f0ea3689SLuigi Rizzo 	netmap_txsync_to_host(kring->na);
470f0ea3689SLuigi Rizzo 	return 0;
471f0ea3689SLuigi Rizzo }
472f0ea3689SLuigi Rizzo 
47389cc2556SLuigi Rizzo /* kring->nm_sync callback for the host rx ring */
474f0ea3689SLuigi Rizzo static int
475f0ea3689SLuigi Rizzo netmap_rxsync_from_host_compat(struct netmap_kring *kring, int flags)
476f0ea3689SLuigi Rizzo {
47789cc2556SLuigi Rizzo 	(void)flags; /* unused */
478f0ea3689SLuigi Rizzo 	netmap_rxsync_from_host(kring->na, NULL, NULL);
479f0ea3689SLuigi Rizzo 	return 0;
480f0ea3689SLuigi Rizzo }
481f0ea3689SLuigi Rizzo 
482f0ea3689SLuigi Rizzo 
483f0ea3689SLuigi Rizzo 
484f0ea3689SLuigi Rizzo /* create the krings array and initialize the fields common to all adapters.
485f0ea3689SLuigi Rizzo  * The array layout is this:
486f0ea3689SLuigi Rizzo  *
487f0ea3689SLuigi Rizzo  *                    +----------+
488f0ea3689SLuigi Rizzo  * na->tx_rings ----->|          | \
489f0ea3689SLuigi Rizzo  *                    |          |  } na->num_tx_ring
490f0ea3689SLuigi Rizzo  *                    |          | /
491f0ea3689SLuigi Rizzo  *                    +----------+
492f0ea3689SLuigi Rizzo  *                    |          |    host tx kring
493f0ea3689SLuigi Rizzo  * na->rx_rings ----> +----------+
494f0ea3689SLuigi Rizzo  *                    |          | \
495f0ea3689SLuigi Rizzo  *                    |          |  } na->num_rx_rings
496f0ea3689SLuigi Rizzo  *                    |          | /
497f0ea3689SLuigi Rizzo  *                    +----------+
498f0ea3689SLuigi Rizzo  *                    |          |    host rx kring
499f0ea3689SLuigi Rizzo  *                    +----------+
500f0ea3689SLuigi Rizzo  * na->tailroom ----->|          | \
501f0ea3689SLuigi Rizzo  *                    |          |  } tailroom bytes
502f0ea3689SLuigi Rizzo  *                    |          | /
503f0ea3689SLuigi Rizzo  *                    +----------+
504f0ea3689SLuigi Rizzo  *
505f0ea3689SLuigi Rizzo  * Note: for compatibility, host krings are created even when not needed.
506f0ea3689SLuigi Rizzo  * The tailroom space is currently used by vale ports for allocating leases.
507f0ea3689SLuigi Rizzo  */
50889cc2556SLuigi Rizzo /* call with NMG_LOCK held */
509f9790aebSLuigi Rizzo int
510f0ea3689SLuigi Rizzo netmap_krings_create(struct netmap_adapter *na, u_int tailroom)
511f9790aebSLuigi Rizzo {
512f9790aebSLuigi Rizzo 	u_int i, len, ndesc;
513f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
514f0ea3689SLuigi Rizzo 	u_int ntx, nrx;
515f9790aebSLuigi Rizzo 
516f0ea3689SLuigi Rizzo 	/* account for the (possibly fake) host rings */
517f0ea3689SLuigi Rizzo 	ntx = na->num_tx_rings + 1;
518f0ea3689SLuigi Rizzo 	nrx = na->num_rx_rings + 1;
519f0ea3689SLuigi Rizzo 
520f9790aebSLuigi Rizzo 	len = (ntx + nrx) * sizeof(struct netmap_kring) + tailroom;
521f9790aebSLuigi Rizzo 
522f9790aebSLuigi Rizzo 	na->tx_rings = malloc((size_t)len, M_DEVBUF, M_NOWAIT | M_ZERO);
523f9790aebSLuigi Rizzo 	if (na->tx_rings == NULL) {
524f9790aebSLuigi Rizzo 		D("Cannot allocate krings");
525f9790aebSLuigi Rizzo 		return ENOMEM;
526f9790aebSLuigi Rizzo 	}
527f9790aebSLuigi Rizzo 	na->rx_rings = na->tx_rings + ntx;
528f9790aebSLuigi Rizzo 
52917885a7bSLuigi Rizzo 	/*
53017885a7bSLuigi Rizzo 	 * All fields in krings are 0 except the one initialized below.
53117885a7bSLuigi Rizzo 	 * but better be explicit on important kring fields.
53217885a7bSLuigi Rizzo 	 */
533f9790aebSLuigi Rizzo 	ndesc = na->num_tx_desc;
534f9790aebSLuigi Rizzo 	for (i = 0; i < ntx; i++) { /* Transmit rings */
535f9790aebSLuigi Rizzo 		kring = &na->tx_rings[i];
536f9790aebSLuigi Rizzo 		bzero(kring, sizeof(*kring));
537f9790aebSLuigi Rizzo 		kring->na = na;
53817885a7bSLuigi Rizzo 		kring->ring_id = i;
539f9790aebSLuigi Rizzo 		kring->nkr_num_slots = ndesc;
540f0ea3689SLuigi Rizzo 		if (i < na->num_tx_rings) {
541f0ea3689SLuigi Rizzo 			kring->nm_sync = netmap_txsync_compat; // XXX
542f0ea3689SLuigi Rizzo 		} else if (i == na->num_tx_rings) {
543f0ea3689SLuigi Rizzo 			kring->nm_sync = netmap_txsync_to_host_compat;
544f0ea3689SLuigi Rizzo 		}
545f9790aebSLuigi Rizzo 		/*
54617885a7bSLuigi Rizzo 		 * IMPORTANT: Always keep one slot empty.
547f9790aebSLuigi Rizzo 		 */
54817885a7bSLuigi Rizzo 		kring->rhead = kring->rcur = kring->nr_hwcur = 0;
54917885a7bSLuigi Rizzo 		kring->rtail = kring->nr_hwtail = ndesc - 1;
55017885a7bSLuigi Rizzo 		snprintf(kring->name, sizeof(kring->name) - 1, "%s TX%d", NM_IFPNAME(na->ifp), i);
551f0ea3689SLuigi Rizzo 		ND("ktx %s h %d c %d t %d",
552f0ea3689SLuigi Rizzo 			kring->name, kring->rhead, kring->rcur, kring->rtail);
553f9790aebSLuigi Rizzo 		mtx_init(&kring->q_lock, "nm_txq_lock", NULL, MTX_DEF);
554f9790aebSLuigi Rizzo 		init_waitqueue_head(&kring->si);
555f9790aebSLuigi Rizzo 	}
556f9790aebSLuigi Rizzo 
557f9790aebSLuigi Rizzo 	ndesc = na->num_rx_desc;
558f9790aebSLuigi Rizzo 	for (i = 0; i < nrx; i++) { /* Receive rings */
559f9790aebSLuigi Rizzo 		kring = &na->rx_rings[i];
560f9790aebSLuigi Rizzo 		bzero(kring, sizeof(*kring));
561f9790aebSLuigi Rizzo 		kring->na = na;
56217885a7bSLuigi Rizzo 		kring->ring_id = i;
563f9790aebSLuigi Rizzo 		kring->nkr_num_slots = ndesc;
564f0ea3689SLuigi Rizzo 		if (i < na->num_rx_rings) {
565f0ea3689SLuigi Rizzo 			kring->nm_sync = netmap_rxsync_compat; // XXX
566f0ea3689SLuigi Rizzo 		} else if (i == na->num_rx_rings) {
567f0ea3689SLuigi Rizzo 			kring->nm_sync = netmap_rxsync_from_host_compat;
568f0ea3689SLuigi Rizzo 		}
56917885a7bSLuigi Rizzo 		kring->rhead = kring->rcur = kring->nr_hwcur = 0;
57017885a7bSLuigi Rizzo 		kring->rtail = kring->nr_hwtail = 0;
57117885a7bSLuigi Rizzo 		snprintf(kring->name, sizeof(kring->name) - 1, "%s RX%d", NM_IFPNAME(na->ifp), i);
572f0ea3689SLuigi Rizzo 		ND("krx %s h %d c %d t %d",
573f0ea3689SLuigi Rizzo 			kring->name, kring->rhead, kring->rcur, kring->rtail);
574f9790aebSLuigi Rizzo 		mtx_init(&kring->q_lock, "nm_rxq_lock", NULL, MTX_DEF);
575f9790aebSLuigi Rizzo 		init_waitqueue_head(&kring->si);
576f9790aebSLuigi Rizzo 	}
577f9790aebSLuigi Rizzo 	init_waitqueue_head(&na->tx_si);
578f9790aebSLuigi Rizzo 	init_waitqueue_head(&na->rx_si);
579f9790aebSLuigi Rizzo 
580f9790aebSLuigi Rizzo 	na->tailroom = na->rx_rings + nrx;
581f9790aebSLuigi Rizzo 
582f9790aebSLuigi Rizzo 	return 0;
583f9790aebSLuigi Rizzo }
584f9790aebSLuigi Rizzo 
585f9790aebSLuigi Rizzo 
586f0ea3689SLuigi Rizzo /* undo the actions performed by netmap_krings_create */
58789cc2556SLuigi Rizzo /* call with NMG_LOCK held */
588f9790aebSLuigi Rizzo void
589f9790aebSLuigi Rizzo netmap_krings_delete(struct netmap_adapter *na)
590f9790aebSLuigi Rizzo {
591f0ea3689SLuigi Rizzo 	struct netmap_kring *kring = na->tx_rings;
592f9790aebSLuigi Rizzo 
593f0ea3689SLuigi Rizzo 	/* we rely on the krings layout described above */
594f0ea3689SLuigi Rizzo 	for ( ; kring != na->tailroom; kring++) {
595f0ea3689SLuigi Rizzo 		mtx_destroy(&kring->q_lock);
596f9790aebSLuigi Rizzo 	}
597f9790aebSLuigi Rizzo 	free(na->tx_rings, M_DEVBUF);
598f9790aebSLuigi Rizzo 	na->tx_rings = na->rx_rings = na->tailroom = NULL;
599f9790aebSLuigi Rizzo }
600f9790aebSLuigi Rizzo 
601f9790aebSLuigi Rizzo 
60217885a7bSLuigi Rizzo /*
60317885a7bSLuigi Rizzo  * Destructor for NIC ports. They also have an mbuf queue
60417885a7bSLuigi Rizzo  * on the rings connected to the host so we need to purge
60517885a7bSLuigi Rizzo  * them first.
60617885a7bSLuigi Rizzo  */
60789cc2556SLuigi Rizzo /* call with NMG_LOCK held */
60817885a7bSLuigi Rizzo static void
60917885a7bSLuigi Rizzo netmap_hw_krings_delete(struct netmap_adapter *na)
61017885a7bSLuigi Rizzo {
61117885a7bSLuigi Rizzo 	struct mbq *q = &na->rx_rings[na->num_rx_rings].rx_queue;
61217885a7bSLuigi Rizzo 
61317885a7bSLuigi Rizzo 	ND("destroy sw mbq with len %d", mbq_len(q));
61417885a7bSLuigi Rizzo 	mbq_purge(q);
61517885a7bSLuigi Rizzo 	mbq_safe_destroy(q);
61617885a7bSLuigi Rizzo 	netmap_krings_delete(na);
61717885a7bSLuigi Rizzo }
61817885a7bSLuigi Rizzo 
61917885a7bSLuigi Rizzo 
62089cc2556SLuigi Rizzo /* create a new netmap_if for a newly registered fd.
62189cc2556SLuigi Rizzo  * If this is the first registration of the adapter,
62289cc2556SLuigi Rizzo  * also create the netmap rings and their in-kernel view,
62389cc2556SLuigi Rizzo  * the netmap krings.
62489cc2556SLuigi Rizzo  */
62589cc2556SLuigi Rizzo /* call with NMG_LOCK held */
626ce3ee1e7SLuigi Rizzo static struct netmap_if*
627ce3ee1e7SLuigi Rizzo netmap_if_new(const char *ifname, struct netmap_adapter *na)
628ce3ee1e7SLuigi Rizzo {
629f9790aebSLuigi Rizzo 	struct netmap_if *nifp;
630f9790aebSLuigi Rizzo 
631ce3ee1e7SLuigi Rizzo 	if (netmap_update_config(na)) {
632ce3ee1e7SLuigi Rizzo 		/* configuration mismatch, report and fail */
633ce3ee1e7SLuigi Rizzo 		return NULL;
634ce3ee1e7SLuigi Rizzo 	}
635f9790aebSLuigi Rizzo 
63689cc2556SLuigi Rizzo 	if (na->active_fds)	/* already registered */
637f9790aebSLuigi Rizzo 		goto final;
638f9790aebSLuigi Rizzo 
63989cc2556SLuigi Rizzo 	/* create and init the krings arrays.
64089cc2556SLuigi Rizzo 	 * Depending on the adapter, this may also create
64189cc2556SLuigi Rizzo 	 * the netmap rings themselves
64289cc2556SLuigi Rizzo 	 */
643f9790aebSLuigi Rizzo 	if (na->nm_krings_create(na))
644f9790aebSLuigi Rizzo 		goto cleanup;
645f9790aebSLuigi Rizzo 
64689cc2556SLuigi Rizzo 	/* create all missing netmap rings */
647f9790aebSLuigi Rizzo 	if (netmap_mem_rings_create(na))
648f9790aebSLuigi Rizzo 		goto cleanup;
649f9790aebSLuigi Rizzo 
650f9790aebSLuigi Rizzo final:
651f9790aebSLuigi Rizzo 
65289cc2556SLuigi Rizzo 	/* in all cases, create a new netmap if */
653f9790aebSLuigi Rizzo 	nifp = netmap_mem_if_new(ifname, na);
654f9790aebSLuigi Rizzo 	if (nifp == NULL)
655f9790aebSLuigi Rizzo 		goto cleanup;
656f9790aebSLuigi Rizzo 
657f9790aebSLuigi Rizzo 	return (nifp);
658f9790aebSLuigi Rizzo 
659f9790aebSLuigi Rizzo cleanup:
660f9790aebSLuigi Rizzo 
661f9790aebSLuigi Rizzo 	if (na->active_fds == 0) {
662f9790aebSLuigi Rizzo 		netmap_mem_rings_delete(na);
663f9790aebSLuigi Rizzo 		na->nm_krings_delete(na);
664ce3ee1e7SLuigi Rizzo 	}
66568b8534bSLuigi Rizzo 
666f9790aebSLuigi Rizzo 	return NULL;
667f9790aebSLuigi Rizzo }
6688241616dSLuigi Rizzo 
66968b8534bSLuigi Rizzo 
670ce3ee1e7SLuigi Rizzo /* grab a reference to the memory allocator, if we don't have one already.  The
671ce3ee1e7SLuigi Rizzo  * reference is taken from the netmap_adapter registered with the priv.
672ce3ee1e7SLuigi Rizzo  */
67389cc2556SLuigi Rizzo /* call with NMG_LOCK held */
674ce3ee1e7SLuigi Rizzo static int
675ce3ee1e7SLuigi Rizzo netmap_get_memory_locked(struct netmap_priv_d* p)
676ce3ee1e7SLuigi Rizzo {
677ce3ee1e7SLuigi Rizzo 	struct netmap_mem_d *nmd;
678ce3ee1e7SLuigi Rizzo 	int error = 0;
679ce3ee1e7SLuigi Rizzo 
680f9790aebSLuigi Rizzo 	if (p->np_na == NULL) {
681ce3ee1e7SLuigi Rizzo 		if (!netmap_mmap_unreg)
682ce3ee1e7SLuigi Rizzo 			return ENODEV;
683ce3ee1e7SLuigi Rizzo 		/* for compatibility with older versions of the API
684ce3ee1e7SLuigi Rizzo  		 * we use the global allocator when no interface has been
685ce3ee1e7SLuigi Rizzo  		 * registered
686ce3ee1e7SLuigi Rizzo  		 */
687ce3ee1e7SLuigi Rizzo 		nmd = &nm_mem;
688ce3ee1e7SLuigi Rizzo 	} else {
689f9790aebSLuigi Rizzo 		nmd = p->np_na->nm_mem;
690ce3ee1e7SLuigi Rizzo 	}
691ce3ee1e7SLuigi Rizzo 	if (p->np_mref == NULL) {
692ce3ee1e7SLuigi Rizzo 		error = netmap_mem_finalize(nmd);
693ce3ee1e7SLuigi Rizzo 		if (!error)
694ce3ee1e7SLuigi Rizzo 			p->np_mref = nmd;
695ce3ee1e7SLuigi Rizzo 	} else if (p->np_mref != nmd) {
696ce3ee1e7SLuigi Rizzo 		/* a virtual port has been registered, but previous
697ce3ee1e7SLuigi Rizzo  		 * syscalls already used the global allocator.
698ce3ee1e7SLuigi Rizzo  		 * We cannot continue
699ce3ee1e7SLuigi Rizzo  		 */
700ce3ee1e7SLuigi Rizzo 		error = ENODEV;
701ce3ee1e7SLuigi Rizzo 	}
702ce3ee1e7SLuigi Rizzo 	return error;
703ce3ee1e7SLuigi Rizzo }
70468b8534bSLuigi Rizzo 
705f9790aebSLuigi Rizzo 
70689cc2556SLuigi Rizzo /* call with NMG_LOCK *not* held */
707f9790aebSLuigi Rizzo int
7088241616dSLuigi Rizzo netmap_get_memory(struct netmap_priv_d* p)
7098241616dSLuigi Rizzo {
710ce3ee1e7SLuigi Rizzo 	int error;
711ce3ee1e7SLuigi Rizzo 	NMG_LOCK();
712ce3ee1e7SLuigi Rizzo 	error = netmap_get_memory_locked(p);
713ce3ee1e7SLuigi Rizzo 	NMG_UNLOCK();
7148241616dSLuigi Rizzo 	return error;
7158241616dSLuigi Rizzo }
7168241616dSLuigi Rizzo 
717f9790aebSLuigi Rizzo 
71889cc2556SLuigi Rizzo /* call with NMG_LOCK held */
719ce3ee1e7SLuigi Rizzo static int
720ce3ee1e7SLuigi Rizzo netmap_have_memory_locked(struct netmap_priv_d* p)
721ce3ee1e7SLuigi Rizzo {
722ce3ee1e7SLuigi Rizzo 	return p->np_mref != NULL;
723ce3ee1e7SLuigi Rizzo }
724ce3ee1e7SLuigi Rizzo 
725f9790aebSLuigi Rizzo 
72689cc2556SLuigi Rizzo /* call with NMG_LOCK held */
727ce3ee1e7SLuigi Rizzo static void
728ce3ee1e7SLuigi Rizzo netmap_drop_memory_locked(struct netmap_priv_d* p)
729ce3ee1e7SLuigi Rizzo {
730ce3ee1e7SLuigi Rizzo 	if (p->np_mref) {
731ce3ee1e7SLuigi Rizzo 		netmap_mem_deref(p->np_mref);
732ce3ee1e7SLuigi Rizzo 		p->np_mref = NULL;
733ce3ee1e7SLuigi Rizzo 	}
734ce3ee1e7SLuigi Rizzo }
735ce3ee1e7SLuigi Rizzo 
736f9790aebSLuigi Rizzo 
73768b8534bSLuigi Rizzo /*
73868b8534bSLuigi Rizzo  * File descriptor's private data destructor.
73968b8534bSLuigi Rizzo  *
74068b8534bSLuigi Rizzo  * Call nm_register(ifp,0) to stop netmap mode on the interface and
741f9790aebSLuigi Rizzo  * revert to normal operation. We expect that np_na->ifp has not gone.
742ce3ee1e7SLuigi Rizzo  * The second argument is the nifp to work on. In some cases it is
743ce3ee1e7SLuigi Rizzo  * not attached yet to the netmap_priv_d so we need to pass it as
744ce3ee1e7SLuigi Rizzo  * a separate argument.
74568b8534bSLuigi Rizzo  */
746ce3ee1e7SLuigi Rizzo /* call with NMG_LOCK held */
74768b8534bSLuigi Rizzo static void
748ce3ee1e7SLuigi Rizzo netmap_do_unregif(struct netmap_priv_d *priv, struct netmap_if *nifp)
74968b8534bSLuigi Rizzo {
750f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
751f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
75268b8534bSLuigi Rizzo 
753ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
754f9790aebSLuigi Rizzo 	na->active_fds--;
755f9790aebSLuigi Rizzo 	if (na->active_fds <= 0) {	/* last instance */
75668b8534bSLuigi Rizzo 
757ae10d1afSLuigi Rizzo 		if (netmap_verbose)
758f9790aebSLuigi Rizzo 			D("deleting last instance for %s", NM_IFPNAME(ifp));
75968b8534bSLuigi Rizzo 		/*
760f18be576SLuigi Rizzo 		 * (TO CHECK) This function is only called
761f18be576SLuigi Rizzo 		 * when the last reference to this file descriptor goes
762f18be576SLuigi Rizzo 		 * away. This means we cannot have any pending poll()
763f18be576SLuigi Rizzo 		 * or interrupt routine operating on the structure.
764ce3ee1e7SLuigi Rizzo 		 * XXX The file may be closed in a thread while
765ce3ee1e7SLuigi Rizzo 		 * another thread is using it.
766ce3ee1e7SLuigi Rizzo 		 * Linux keeps the file opened until the last reference
767ce3ee1e7SLuigi Rizzo 		 * by any outstanding ioctl/poll or mmap is gone.
768ce3ee1e7SLuigi Rizzo 		 * FreeBSD does not track mmap()s (but we do) and
769ce3ee1e7SLuigi Rizzo 		 * wakes up any sleeping poll(). Need to check what
770ce3ee1e7SLuigi Rizzo 		 * happens if the close() occurs while a concurrent
771ce3ee1e7SLuigi Rizzo 		 * syscall is running.
77268b8534bSLuigi Rizzo 		 */
773f9790aebSLuigi Rizzo 		if (ifp)
774f9790aebSLuigi Rizzo 			na->nm_register(na, 0); /* off, clear flags */
77568b8534bSLuigi Rizzo 		/* Wake up any sleeping threads. netmap_poll will
77668b8534bSLuigi Rizzo 		 * then return POLLERR
777ce3ee1e7SLuigi Rizzo 		 * XXX The wake up now must happen during *_down(), when
778ce3ee1e7SLuigi Rizzo 		 * we order all activities to stop. -gl
77968b8534bSLuigi Rizzo 		 */
7802f70fca5SEd Maste 		/* XXX kqueue(9) needed; these will mirror knlist_init. */
7812f70fca5SEd Maste 		/* knlist_destroy(&na->tx_si.si_note); */
7822f70fca5SEd Maste 		/* knlist_destroy(&na->rx_si.si_note); */
783f9790aebSLuigi Rizzo 
784f9790aebSLuigi Rizzo 		/* delete rings and buffers */
785f9790aebSLuigi Rizzo 		netmap_mem_rings_delete(na);
786f9790aebSLuigi Rizzo 		na->nm_krings_delete(na);
78768b8534bSLuigi Rizzo 	}
788f9790aebSLuigi Rizzo 	/* delete the nifp */
789ce3ee1e7SLuigi Rizzo 	netmap_mem_if_delete(na, nifp);
7905819da83SLuigi Rizzo }
79168b8534bSLuigi Rizzo 
79289cc2556SLuigi Rizzo /* call with NMG_LOCK held */
793f0ea3689SLuigi Rizzo static __inline int
794f0ea3689SLuigi Rizzo nm_tx_si_user(struct netmap_priv_d *priv)
795f0ea3689SLuigi Rizzo {
796f0ea3689SLuigi Rizzo 	return (priv->np_na != NULL &&
797f0ea3689SLuigi Rizzo 		(priv->np_txqlast - priv->np_txqfirst > 1));
798f0ea3689SLuigi Rizzo }
799f0ea3689SLuigi Rizzo 
80089cc2556SLuigi Rizzo /* call with NMG_LOCK held */
801f0ea3689SLuigi Rizzo static __inline int
802f0ea3689SLuigi Rizzo nm_rx_si_user(struct netmap_priv_d *priv)
803f0ea3689SLuigi Rizzo {
804f0ea3689SLuigi Rizzo 	return (priv->np_na != NULL &&
805f0ea3689SLuigi Rizzo 		(priv->np_rxqlast - priv->np_rxqfirst > 1));
806f0ea3689SLuigi Rizzo }
807f0ea3689SLuigi Rizzo 
808f18be576SLuigi Rizzo 
809ce3ee1e7SLuigi Rizzo /*
81089cc2556SLuigi Rizzo  * Destructor of the netmap_priv_d, called when the fd has
81189cc2556SLuigi Rizzo  * no active open() and mmap(). Also called in error paths.
81289cc2556SLuigi Rizzo  *
813ce3ee1e7SLuigi Rizzo  * returns 1 if this is the last instance and we can free priv
814ce3ee1e7SLuigi Rizzo  */
81589cc2556SLuigi Rizzo /* call with NMG_LOCK held */
816f9790aebSLuigi Rizzo int
817ce3ee1e7SLuigi Rizzo netmap_dtor_locked(struct netmap_priv_d *priv)
818ce3ee1e7SLuigi Rizzo {
819f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
820ce3ee1e7SLuigi Rizzo 
821ce3ee1e7SLuigi Rizzo #ifdef __FreeBSD__
822ce3ee1e7SLuigi Rizzo 	/*
823ce3ee1e7SLuigi Rizzo 	 * np_refcount is the number of active mmaps on
824ce3ee1e7SLuigi Rizzo 	 * this file descriptor
825ce3ee1e7SLuigi Rizzo 	 */
826ce3ee1e7SLuigi Rizzo 	if (--priv->np_refcount > 0) {
827ce3ee1e7SLuigi Rizzo 		return 0;
828ce3ee1e7SLuigi Rizzo 	}
829ce3ee1e7SLuigi Rizzo #endif /* __FreeBSD__ */
830f9790aebSLuigi Rizzo 	if (!na) {
831f9790aebSLuigi Rizzo 	    return 1; //XXX is it correct?
832ce3ee1e7SLuigi Rizzo 	}
833f9790aebSLuigi Rizzo 	netmap_do_unregif(priv, priv->np_nifp);
834f9790aebSLuigi Rizzo 	priv->np_nifp = NULL;
835ce3ee1e7SLuigi Rizzo 	netmap_drop_memory_locked(priv);
836f9790aebSLuigi Rizzo 	if (priv->np_na) {
837f0ea3689SLuigi Rizzo 		if (nm_tx_si_user(priv))
838f0ea3689SLuigi Rizzo 			na->tx_si_users--;
839f0ea3689SLuigi Rizzo 		if (nm_rx_si_user(priv))
840f0ea3689SLuigi Rizzo 			na->rx_si_users--;
841f9790aebSLuigi Rizzo 		netmap_adapter_put(na);
842f9790aebSLuigi Rizzo 		priv->np_na = NULL;
843ce3ee1e7SLuigi Rizzo 	}
844ce3ee1e7SLuigi Rizzo 	return 1;
845f196ce38SLuigi Rizzo }
8465819da83SLuigi Rizzo 
847f9790aebSLuigi Rizzo 
84889cc2556SLuigi Rizzo /* call with NMG_LOCK *not* held */
849f9790aebSLuigi Rizzo void
8505819da83SLuigi Rizzo netmap_dtor(void *data)
8515819da83SLuigi Rizzo {
8525819da83SLuigi Rizzo 	struct netmap_priv_d *priv = data;
853ce3ee1e7SLuigi Rizzo 	int last_instance;
8545819da83SLuigi Rizzo 
855ce3ee1e7SLuigi Rizzo 	NMG_LOCK();
856ce3ee1e7SLuigi Rizzo 	last_instance = netmap_dtor_locked(priv);
857ce3ee1e7SLuigi Rizzo 	NMG_UNLOCK();
858ce3ee1e7SLuigi Rizzo 	if (last_instance) {
859ce3ee1e7SLuigi Rizzo 		bzero(priv, sizeof(*priv));	/* for safety */
86068b8534bSLuigi Rizzo 		free(priv, M_DEVBUF);
86168b8534bSLuigi Rizzo 	}
862ce3ee1e7SLuigi Rizzo }
86368b8534bSLuigi Rizzo 
864f18be576SLuigi Rizzo 
86568b8534bSLuigi Rizzo 
86668b8534bSLuigi Rizzo 
86768b8534bSLuigi Rizzo /*
86802ad4083SLuigi Rizzo  * Handlers for synchronization of the queues from/to the host.
869091fd0abSLuigi Rizzo  * Netmap has two operating modes:
870091fd0abSLuigi Rizzo  * - in the default mode, the rings connected to the host stack are
871091fd0abSLuigi Rizzo  *   just another ring pair managed by userspace;
872091fd0abSLuigi Rizzo  * - in transparent mode (XXX to be defined) incoming packets
873091fd0abSLuigi Rizzo  *   (from the host or the NIC) are marked as NS_FORWARD upon
874091fd0abSLuigi Rizzo  *   arrival, and the user application has a chance to reset the
875091fd0abSLuigi Rizzo  *   flag for packets that should be dropped.
876091fd0abSLuigi Rizzo  *   On the RXSYNC or poll(), packets in RX rings between
877091fd0abSLuigi Rizzo  *   kring->nr_kcur and ring->cur with NS_FORWARD still set are moved
878091fd0abSLuigi Rizzo  *   to the other side.
879091fd0abSLuigi Rizzo  * The transfer NIC --> host is relatively easy, just encapsulate
880091fd0abSLuigi Rizzo  * into mbufs and we are done. The host --> NIC side is slightly
881091fd0abSLuigi Rizzo  * harder because there might not be room in the tx ring so it
882091fd0abSLuigi Rizzo  * might take a while before releasing the buffer.
883091fd0abSLuigi Rizzo  */
884091fd0abSLuigi Rizzo 
885f18be576SLuigi Rizzo 
886091fd0abSLuigi Rizzo /*
887091fd0abSLuigi Rizzo  * pass a chain of buffers to the host stack as coming from 'dst'
88817885a7bSLuigi Rizzo  * We do not need to lock because the queue is private.
889091fd0abSLuigi Rizzo  */
890091fd0abSLuigi Rizzo static void
891f9790aebSLuigi Rizzo netmap_send_up(struct ifnet *dst, struct mbq *q)
892091fd0abSLuigi Rizzo {
893091fd0abSLuigi Rizzo 	struct mbuf *m;
894091fd0abSLuigi Rizzo 
895091fd0abSLuigi Rizzo 	/* send packets up, outside the lock */
896f9790aebSLuigi Rizzo 	while ((m = mbq_dequeue(q)) != NULL) {
897091fd0abSLuigi Rizzo 		if (netmap_verbose & NM_VERB_HOST)
898091fd0abSLuigi Rizzo 			D("sending up pkt %p size %d", m, MBUF_LEN(m));
899091fd0abSLuigi Rizzo 		NM_SEND_UP(dst, m);
900091fd0abSLuigi Rizzo 	}
901f9790aebSLuigi Rizzo 	mbq_destroy(q);
902091fd0abSLuigi Rizzo }
903091fd0abSLuigi Rizzo 
904f18be576SLuigi Rizzo 
905091fd0abSLuigi Rizzo /*
906091fd0abSLuigi Rizzo  * put a copy of the buffers marked NS_FORWARD into an mbuf chain.
90717885a7bSLuigi Rizzo  * Take packets from hwcur to ring->head marked NS_FORWARD (or forced)
90817885a7bSLuigi Rizzo  * and pass them up. Drop remaining packets in the unlikely event
90917885a7bSLuigi Rizzo  * of an mbuf shortage.
910091fd0abSLuigi Rizzo  */
911091fd0abSLuigi Rizzo static void
912091fd0abSLuigi Rizzo netmap_grab_packets(struct netmap_kring *kring, struct mbq *q, int force)
913091fd0abSLuigi Rizzo {
91417885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
91517885a7bSLuigi Rizzo 	u_int const head = kring->ring->head;
91617885a7bSLuigi Rizzo 	u_int n;
917f9790aebSLuigi Rizzo 	struct netmap_adapter *na = kring->na;
918091fd0abSLuigi Rizzo 
91917885a7bSLuigi Rizzo 	for (n = kring->nr_hwcur; n != head; n = nm_next(n, lim)) {
92017885a7bSLuigi Rizzo 		struct mbuf *m;
921091fd0abSLuigi Rizzo 		struct netmap_slot *slot = &kring->ring->slot[n];
922091fd0abSLuigi Rizzo 
923091fd0abSLuigi Rizzo 		if ((slot->flags & NS_FORWARD) == 0 && !force)
924091fd0abSLuigi Rizzo 			continue;
925f9790aebSLuigi Rizzo 		if (slot->len < 14 || slot->len > NETMAP_BDG_BUF_SIZE(na->nm_mem)) {
92617885a7bSLuigi Rizzo 			RD(5, "bad pkt at %d len %d", n, slot->len);
927091fd0abSLuigi Rizzo 			continue;
928091fd0abSLuigi Rizzo 		}
929091fd0abSLuigi Rizzo 		slot->flags &= ~NS_FORWARD; // XXX needed ?
93017885a7bSLuigi Rizzo 		/* XXX TODO: adapt to the case of a multisegment packet */
931f9790aebSLuigi Rizzo 		m = m_devget(BDG_NMB(na, slot), slot->len, 0, na->ifp, NULL);
932091fd0abSLuigi Rizzo 
933091fd0abSLuigi Rizzo 		if (m == NULL)
934091fd0abSLuigi Rizzo 			break;
935f9790aebSLuigi Rizzo 		mbq_enqueue(q, m);
936091fd0abSLuigi Rizzo 	}
937091fd0abSLuigi Rizzo }
938091fd0abSLuigi Rizzo 
939f18be576SLuigi Rizzo 
940091fd0abSLuigi Rizzo /*
94117885a7bSLuigi Rizzo  * Send to the NIC rings packets marked NS_FORWARD between
94217885a7bSLuigi Rizzo  * kring->nr_hwcur and kring->rhead
94317885a7bSLuigi Rizzo  * Called under kring->rx_queue.lock on the sw rx ring,
944091fd0abSLuigi Rizzo  */
94517885a7bSLuigi Rizzo static u_int
946091fd0abSLuigi Rizzo netmap_sw_to_nic(struct netmap_adapter *na)
947091fd0abSLuigi Rizzo {
948091fd0abSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
94917885a7bSLuigi Rizzo 	struct netmap_slot *rxslot = kring->ring->slot;
95017885a7bSLuigi Rizzo 	u_int i, rxcur = kring->nr_hwcur;
95117885a7bSLuigi Rizzo 	u_int const head = kring->rhead;
95217885a7bSLuigi Rizzo 	u_int const src_lim = kring->nkr_num_slots - 1;
95317885a7bSLuigi Rizzo 	u_int sent = 0;
954ce3ee1e7SLuigi Rizzo 
95517885a7bSLuigi Rizzo 	/* scan rings to find space, then fill as much as possible */
95617885a7bSLuigi Rizzo 	for (i = 0; i < na->num_tx_rings; i++) {
95717885a7bSLuigi Rizzo 		struct netmap_kring *kdst = &na->tx_rings[i];
95817885a7bSLuigi Rizzo 		struct netmap_ring *rdst = kdst->ring;
95917885a7bSLuigi Rizzo 		u_int const dst_lim = kdst->nkr_num_slots - 1;
960ce3ee1e7SLuigi Rizzo 
96117885a7bSLuigi Rizzo 		/* XXX do we trust ring or kring->rcur,rtail ? */
96217885a7bSLuigi Rizzo 		for (; rxcur != head && !nm_ring_empty(rdst);
96317885a7bSLuigi Rizzo 		     rxcur = nm_next(rxcur, src_lim) ) {
964091fd0abSLuigi Rizzo 			struct netmap_slot *src, *dst, tmp;
96517885a7bSLuigi Rizzo 			u_int dst_cur = rdst->cur;
96617885a7bSLuigi Rizzo 
96717885a7bSLuigi Rizzo 			src = &rxslot[rxcur];
96817885a7bSLuigi Rizzo 			if ((src->flags & NS_FORWARD) == 0 && !netmap_fwd)
96917885a7bSLuigi Rizzo 				continue;
97017885a7bSLuigi Rizzo 
97117885a7bSLuigi Rizzo 			sent++;
97217885a7bSLuigi Rizzo 
97317885a7bSLuigi Rizzo 			dst = &rdst->slot[dst_cur];
97417885a7bSLuigi Rizzo 
975091fd0abSLuigi Rizzo 			tmp = *src;
97617885a7bSLuigi Rizzo 
977091fd0abSLuigi Rizzo 			src->buf_idx = dst->buf_idx;
978091fd0abSLuigi Rizzo 			src->flags = NS_BUF_CHANGED;
979091fd0abSLuigi Rizzo 
980091fd0abSLuigi Rizzo 			dst->buf_idx = tmp.buf_idx;
981091fd0abSLuigi Rizzo 			dst->len = tmp.len;
982091fd0abSLuigi Rizzo 			dst->flags = NS_BUF_CHANGED;
983091fd0abSLuigi Rizzo 
98417885a7bSLuigi Rizzo 			rdst->cur = nm_next(dst_cur, dst_lim);
985091fd0abSLuigi Rizzo 		}
98617885a7bSLuigi Rizzo 		/* if (sent) XXX txsync ? */
987091fd0abSLuigi Rizzo 	}
98817885a7bSLuigi Rizzo 	return sent;
989091fd0abSLuigi Rizzo }
990091fd0abSLuigi Rizzo 
991f18be576SLuigi Rizzo 
992091fd0abSLuigi Rizzo /*
993ce3ee1e7SLuigi Rizzo  * netmap_txsync_to_host() passes packets up. We are called from a
99402ad4083SLuigi Rizzo  * system call in user process context, and the only contention
99502ad4083SLuigi Rizzo  * can be among multiple user threads erroneously calling
996091fd0abSLuigi Rizzo  * this routine concurrently.
99768b8534bSLuigi Rizzo  */
998f9790aebSLuigi Rizzo void
999ce3ee1e7SLuigi Rizzo netmap_txsync_to_host(struct netmap_adapter *na)
100068b8534bSLuigi Rizzo {
1001d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->tx_rings[na->num_tx_rings];
100268b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
100317885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
1004f0ea3689SLuigi Rizzo 	u_int const head = kring->rhead;
1005f9790aebSLuigi Rizzo 	struct mbq q;
100668b8534bSLuigi Rizzo 
100717885a7bSLuigi Rizzo 	/* Take packets from hwcur to head and pass them up.
100817885a7bSLuigi Rizzo 	 * force head = cur since netmap_grab_packets() stops at head
100968b8534bSLuigi Rizzo 	 * In case of no buffers we give up. At the end of the loop,
101068b8534bSLuigi Rizzo 	 * the queue is drained in all cases.
101168b8534bSLuigi Rizzo 	 */
1012f9790aebSLuigi Rizzo 	mbq_init(&q);
101317885a7bSLuigi Rizzo 	ring->cur = head;
101417885a7bSLuigi Rizzo 	netmap_grab_packets(kring, &q, 1 /* force */);
101517885a7bSLuigi Rizzo 	ND("have %d pkts in queue", mbq_len(&q));
101617885a7bSLuigi Rizzo 	kring->nr_hwcur = head;
101717885a7bSLuigi Rizzo 	kring->nr_hwtail = head + lim;
101817885a7bSLuigi Rizzo 	if (kring->nr_hwtail > lim)
101917885a7bSLuigi Rizzo 		kring->nr_hwtail -= lim + 1;
102017885a7bSLuigi Rizzo 	nm_txsync_finalize(kring);
102168b8534bSLuigi Rizzo 
1022f9790aebSLuigi Rizzo 	netmap_send_up(na->ifp, &q);
1023f18be576SLuigi Rizzo }
1024f18be576SLuigi Rizzo 
1025f18be576SLuigi Rizzo 
102668b8534bSLuigi Rizzo /*
102702ad4083SLuigi Rizzo  * rxsync backend for packets coming from the host stack.
102817885a7bSLuigi Rizzo  * They have been put in kring->rx_queue by netmap_transmit().
102917885a7bSLuigi Rizzo  * We protect access to the kring using kring->rx_queue.lock
103002ad4083SLuigi Rizzo  *
103168b8534bSLuigi Rizzo  * This routine also does the selrecord if called from the poll handler
103268b8534bSLuigi Rizzo  * (we know because td != NULL).
103301c7d25fSLuigi Rizzo  *
103401c7d25fSLuigi Rizzo  * NOTE: on linux, selrecord() is defined as a macro and uses pwait
103501c7d25fSLuigi Rizzo  *     as an additional hidden argument.
103617885a7bSLuigi Rizzo  * returns the number of packets delivered to tx queues in
103717885a7bSLuigi Rizzo  * transparent mode, or a negative value if error
103868b8534bSLuigi Rizzo  */
103917885a7bSLuigi Rizzo int
1040ce3ee1e7SLuigi Rizzo netmap_rxsync_from_host(struct netmap_adapter *na, struct thread *td, void *pwait)
104168b8534bSLuigi Rizzo {
1042d76bf4ffSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[na->num_rx_rings];
104368b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
104417885a7bSLuigi Rizzo 	u_int nm_i, n;
104517885a7bSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
1046f0ea3689SLuigi Rizzo 	u_int const head = kring->rhead;
104717885a7bSLuigi Rizzo 	int ret = 0;
104817885a7bSLuigi Rizzo 	struct mbq *q = &kring->rx_queue;
104968b8534bSLuigi Rizzo 
105001c7d25fSLuigi Rizzo 	(void)pwait;	/* disable unused warnings */
1051f0ea3689SLuigi Rizzo 	(void)td;
105217885a7bSLuigi Rizzo 
1053*997b054cSLuigi Rizzo 	mbq_lock(q);
105417885a7bSLuigi Rizzo 
105517885a7bSLuigi Rizzo 	/* First part: import newly received packets */
105617885a7bSLuigi Rizzo 	n = mbq_len(q);
105717885a7bSLuigi Rizzo 	if (n) { /* grab packets from the queue */
105817885a7bSLuigi Rizzo 		struct mbuf *m;
105917885a7bSLuigi Rizzo 		uint32_t stop_i;
106017885a7bSLuigi Rizzo 
106117885a7bSLuigi Rizzo 		nm_i = kring->nr_hwtail;
106217885a7bSLuigi Rizzo 		stop_i = nm_prev(nm_i, lim);
106317885a7bSLuigi Rizzo 		while ( nm_i != stop_i && (m = mbq_dequeue(q)) != NULL ) {
106417885a7bSLuigi Rizzo 			int len = MBUF_LEN(m);
106517885a7bSLuigi Rizzo 			struct netmap_slot *slot = &ring->slot[nm_i];
106617885a7bSLuigi Rizzo 
106717885a7bSLuigi Rizzo 			m_copydata(m, 0, len, BDG_NMB(na, slot));
106817885a7bSLuigi Rizzo 			ND("nm %d len %d", nm_i, len);
106917885a7bSLuigi Rizzo 			if (netmap_verbose)
107017885a7bSLuigi Rizzo                                 D("%s", nm_dump_buf(BDG_NMB(na, slot),len, 128, NULL));
107117885a7bSLuigi Rizzo 
107217885a7bSLuigi Rizzo 			slot->len = len;
107317885a7bSLuigi Rizzo 			slot->flags = kring->nkr_slot_flags;
107417885a7bSLuigi Rizzo 			nm_i = nm_next(nm_i, lim);
107564ae02c3SLuigi Rizzo 		}
107617885a7bSLuigi Rizzo 		kring->nr_hwtail = nm_i;
107764ae02c3SLuigi Rizzo 	}
107817885a7bSLuigi Rizzo 
107917885a7bSLuigi Rizzo 	/*
108017885a7bSLuigi Rizzo 	 * Second part: skip past packets that userspace has released.
108117885a7bSLuigi Rizzo 	 */
108217885a7bSLuigi Rizzo 	nm_i = kring->nr_hwcur;
108317885a7bSLuigi Rizzo 	if (nm_i != head) { /* something was released */
108417885a7bSLuigi Rizzo 		if (netmap_fwd || kring->ring->flags & NR_FORWARD)
108517885a7bSLuigi Rizzo 			ret = netmap_sw_to_nic(na);
108617885a7bSLuigi Rizzo 		kring->nr_hwcur = head;
108764ae02c3SLuigi Rizzo 	}
108817885a7bSLuigi Rizzo 
108917885a7bSLuigi Rizzo 	nm_rxsync_finalize(kring);
109017885a7bSLuigi Rizzo 
109117885a7bSLuigi Rizzo 	/* access copies of cur,tail in the kring */
109217885a7bSLuigi Rizzo 	if (kring->rcur == kring->rtail && td) /* no bufs available */
109368b8534bSLuigi Rizzo 		selrecord(td, &kring->si);
109417885a7bSLuigi Rizzo 
1095*997b054cSLuigi Rizzo 	mbq_unlock(q);
109617885a7bSLuigi Rizzo 	return ret;
109768b8534bSLuigi Rizzo }
109868b8534bSLuigi Rizzo 
109968b8534bSLuigi Rizzo 
1100f9790aebSLuigi Rizzo /* Get a netmap adapter for the port.
1101f9790aebSLuigi Rizzo  *
1102f9790aebSLuigi Rizzo  * If it is possible to satisfy the request, return 0
1103f9790aebSLuigi Rizzo  * with *na containing the netmap adapter found.
1104f9790aebSLuigi Rizzo  * Otherwise return an error code, with *na containing NULL.
1105f9790aebSLuigi Rizzo  *
1106f9790aebSLuigi Rizzo  * When the port is attached to a bridge, we always return
1107f9790aebSLuigi Rizzo  * EBUSY.
1108f9790aebSLuigi Rizzo  * Otherwise, if the port is already bound to a file descriptor,
1109f9790aebSLuigi Rizzo  * then we unconditionally return the existing adapter into *na.
1110f9790aebSLuigi Rizzo  * In all the other cases, we return (into *na) either native,
1111f9790aebSLuigi Rizzo  * generic or NULL, according to the following table:
1112f9790aebSLuigi Rizzo  *
1113f9790aebSLuigi Rizzo  *					native_support
1114f9790aebSLuigi Rizzo  * active_fds   dev.netmap.admode         YES     NO
1115f9790aebSLuigi Rizzo  * -------------------------------------------------------
1116f9790aebSLuigi Rizzo  *    >0              *                 NA(ifp) NA(ifp)
1117f9790aebSLuigi Rizzo  *
1118f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_BEST      NATIVE  GENERIC
1119f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_NATIVE    NATIVE   NULL
1120f9790aebSLuigi Rizzo  *     0        NETMAP_ADMODE_GENERIC   GENERIC GENERIC
1121f9790aebSLuigi Rizzo  *
1122f9790aebSLuigi Rizzo  */
1123f9790aebSLuigi Rizzo 
1124f9790aebSLuigi Rizzo int
1125f9790aebSLuigi Rizzo netmap_get_hw_na(struct ifnet *ifp, struct netmap_adapter **na)
1126f9790aebSLuigi Rizzo {
1127f9790aebSLuigi Rizzo 	/* generic support */
1128f9790aebSLuigi Rizzo 	int i = netmap_admode;	/* Take a snapshot. */
1129f9790aebSLuigi Rizzo 	int error = 0;
1130f9790aebSLuigi Rizzo 	struct netmap_adapter *prev_na;
1131f9790aebSLuigi Rizzo 	struct netmap_generic_adapter *gna;
1132f9790aebSLuigi Rizzo 
1133f9790aebSLuigi Rizzo 	*na = NULL; /* default */
1134f9790aebSLuigi Rizzo 
1135f9790aebSLuigi Rizzo 	/* reset in case of invalid value */
1136f9790aebSLuigi Rizzo 	if (i < NETMAP_ADMODE_BEST || i >= NETMAP_ADMODE_LAST)
1137f9790aebSLuigi Rizzo 		i = netmap_admode = NETMAP_ADMODE_BEST;
1138f9790aebSLuigi Rizzo 
1139f9790aebSLuigi Rizzo 	if (NETMAP_CAPABLE(ifp)) {
1140f9790aebSLuigi Rizzo 		/* If an adapter already exists, but is
1141f9790aebSLuigi Rizzo 		 * attached to a vale port, we report that the
1142f9790aebSLuigi Rizzo 		 * port is busy.
1143f9790aebSLuigi Rizzo 		 */
1144f9790aebSLuigi Rizzo 		if (NETMAP_OWNED_BY_KERN(NA(ifp)))
1145f9790aebSLuigi Rizzo 			return EBUSY;
1146f9790aebSLuigi Rizzo 
1147f9790aebSLuigi Rizzo 		/* If an adapter already exists, return it if
1148f9790aebSLuigi Rizzo 		 * there are active file descriptors or if
1149f9790aebSLuigi Rizzo 		 * netmap is not forced to use generic
1150f9790aebSLuigi Rizzo 		 * adapters.
1151f9790aebSLuigi Rizzo 		 */
1152f9790aebSLuigi Rizzo 		if (NA(ifp)->active_fds > 0 ||
1153f9790aebSLuigi Rizzo 				i != NETMAP_ADMODE_GENERIC) {
1154f9790aebSLuigi Rizzo 			*na = NA(ifp);
1155f9790aebSLuigi Rizzo 			return 0;
1156f9790aebSLuigi Rizzo 		}
1157f9790aebSLuigi Rizzo 	}
1158f9790aebSLuigi Rizzo 
1159f9790aebSLuigi Rizzo 	/* If there isn't native support and netmap is not allowed
1160f9790aebSLuigi Rizzo 	 * to use generic adapters, we cannot satisfy the request.
1161f9790aebSLuigi Rizzo 	 */
1162f9790aebSLuigi Rizzo 	if (!NETMAP_CAPABLE(ifp) && i == NETMAP_ADMODE_NATIVE)
1163f2637526SLuigi Rizzo 		return EOPNOTSUPP;
1164f9790aebSLuigi Rizzo 
1165f9790aebSLuigi Rizzo 	/* Otherwise, create a generic adapter and return it,
1166f9790aebSLuigi Rizzo 	 * saving the previously used netmap adapter, if any.
1167f9790aebSLuigi Rizzo 	 *
1168f9790aebSLuigi Rizzo 	 * Note that here 'prev_na', if not NULL, MUST be a
1169f9790aebSLuigi Rizzo 	 * native adapter, and CANNOT be a generic one. This is
1170f9790aebSLuigi Rizzo 	 * true because generic adapters are created on demand, and
1171f9790aebSLuigi Rizzo 	 * destroyed when not used anymore. Therefore, if the adapter
1172f9790aebSLuigi Rizzo 	 * currently attached to an interface 'ifp' is generic, it
1173f9790aebSLuigi Rizzo 	 * must be that
1174f9790aebSLuigi Rizzo 	 * (NA(ifp)->active_fds > 0 || NETMAP_OWNED_BY_KERN(NA(ifp))).
1175f9790aebSLuigi Rizzo 	 * Consequently, if NA(ifp) is generic, we will enter one of
1176f9790aebSLuigi Rizzo 	 * the branches above. This ensures that we never override
1177f9790aebSLuigi Rizzo 	 * a generic adapter with another generic adapter.
1178f9790aebSLuigi Rizzo 	 */
1179f9790aebSLuigi Rizzo 	prev_na = NA(ifp);
1180f9790aebSLuigi Rizzo 	error = generic_netmap_attach(ifp);
1181f9790aebSLuigi Rizzo 	if (error)
1182f9790aebSLuigi Rizzo 		return error;
1183f9790aebSLuigi Rizzo 
1184f9790aebSLuigi Rizzo 	*na = NA(ifp);
1185f9790aebSLuigi Rizzo 	gna = (struct netmap_generic_adapter*)NA(ifp);
1186f9790aebSLuigi Rizzo 	gna->prev = prev_na; /* save old na */
1187f9790aebSLuigi Rizzo 	if (prev_na != NULL) {
1188f9790aebSLuigi Rizzo 		ifunit_ref(ifp->if_xname);
1189f9790aebSLuigi Rizzo 		// XXX add a refcount ?
1190f9790aebSLuigi Rizzo 		netmap_adapter_get(prev_na);
1191f9790aebSLuigi Rizzo 	}
119217885a7bSLuigi Rizzo 	ND("Created generic NA %p (prev %p)", gna, gna->prev);
1193f9790aebSLuigi Rizzo 
1194f9790aebSLuigi Rizzo 	return 0;
1195f9790aebSLuigi Rizzo }
1196f9790aebSLuigi Rizzo 
1197f9790aebSLuigi Rizzo 
119868b8534bSLuigi Rizzo /*
1199ce3ee1e7SLuigi Rizzo  * MUST BE CALLED UNDER NMG_LOCK()
1200ce3ee1e7SLuigi Rizzo  *
1201f2637526SLuigi Rizzo  * Get a refcounted reference to a netmap adapter attached
1202f2637526SLuigi Rizzo  * to the interface specified by nmr.
1203ce3ee1e7SLuigi Rizzo  * This is always called in the execution of an ioctl().
1204ce3ee1e7SLuigi Rizzo  *
1205f2637526SLuigi Rizzo  * Return ENXIO if the interface specified by the request does
1206f2637526SLuigi Rizzo  * not exist, ENOTSUP if netmap is not supported by the interface,
1207f2637526SLuigi Rizzo  * EBUSY if the interface is already attached to a bridge,
1208f2637526SLuigi Rizzo  * EINVAL if parameters are invalid, ENOMEM if needed resources
1209f2637526SLuigi Rizzo  * could not be allocated.
1210f2637526SLuigi Rizzo  * If successful, hold a reference to the netmap adapter.
1211f18be576SLuigi Rizzo  *
1212f2637526SLuigi Rizzo  * No reference is kept on the real interface, which may then
1213f2637526SLuigi Rizzo  * disappear at any time.
121468b8534bSLuigi Rizzo  */
1215f9790aebSLuigi Rizzo int
1216f9790aebSLuigi Rizzo netmap_get_na(struct nmreq *nmr, struct netmap_adapter **na, int create)
121768b8534bSLuigi Rizzo {
1218f0ea3689SLuigi Rizzo 	struct ifnet *ifp = NULL;
1219f9790aebSLuigi Rizzo 	int error = 0;
1220f0ea3689SLuigi Rizzo 	struct netmap_adapter *ret = NULL;
1221f9790aebSLuigi Rizzo 
1222f9790aebSLuigi Rizzo 	*na = NULL;     /* default return value */
1223f196ce38SLuigi Rizzo 
1224ce3ee1e7SLuigi Rizzo 	/* first try to see if this is a bridge port. */
1225ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
1226ce3ee1e7SLuigi Rizzo 
1227f0ea3689SLuigi Rizzo 	error = netmap_get_pipe_na(nmr, na, create);
1228f0ea3689SLuigi Rizzo 	if (error || *na != NULL)
1229f9790aebSLuigi Rizzo 		return error;
1230ce3ee1e7SLuigi Rizzo 
1231f0ea3689SLuigi Rizzo 	error = netmap_get_bdg_na(nmr, na, create);
1232f0ea3689SLuigi Rizzo 	if (error)
1233f0ea3689SLuigi Rizzo 		return error;
1234f0ea3689SLuigi Rizzo 
1235f0ea3689SLuigi Rizzo 	if (*na != NULL) /* valid match in netmap_get_bdg_na() */
1236f0ea3689SLuigi Rizzo 		goto pipes;
1237f0ea3689SLuigi Rizzo 
123889cc2556SLuigi Rizzo 	/*
123989cc2556SLuigi Rizzo 	 * This must be a hardware na, lookup the name in the system.
124089cc2556SLuigi Rizzo 	 * Note that by hardware we actually mean "it shows up in ifconfig".
124189cc2556SLuigi Rizzo 	 * This may still be a tap, a veth/epair, or even a
124289cc2556SLuigi Rizzo 	 * persistent VALE port.
124389cc2556SLuigi Rizzo 	 */
1244f9790aebSLuigi Rizzo 	ifp = ifunit_ref(nmr->nr_name);
1245f9790aebSLuigi Rizzo 	if (ifp == NULL) {
1246ce3ee1e7SLuigi Rizzo 	        return ENXIO;
1247f196ce38SLuigi Rizzo 	}
1248ce3ee1e7SLuigi Rizzo 
1249f9790aebSLuigi Rizzo 	error = netmap_get_hw_na(ifp, &ret);
1250f9790aebSLuigi Rizzo 	if (error)
1251f9790aebSLuigi Rizzo 		goto out;
1252f18be576SLuigi Rizzo 
1253f9790aebSLuigi Rizzo 	/* Users cannot use the NIC attached to a bridge directly */
1254f9790aebSLuigi Rizzo 	if (NETMAP_OWNED_BY_KERN(ret)) {
1255f2637526SLuigi Rizzo 		error = EBUSY;
1256f9790aebSLuigi Rizzo 		goto out;
1257ce3ee1e7SLuigi Rizzo 	}
1258f9790aebSLuigi Rizzo 	*na = ret;
1259f9790aebSLuigi Rizzo 	netmap_adapter_get(ret);
1260f0ea3689SLuigi Rizzo 
1261f0ea3689SLuigi Rizzo pipes:
126289cc2556SLuigi Rizzo 	/*
126389cc2556SLuigi Rizzo 	 * If we are opening a pipe whose parent was not in netmap mode,
126489cc2556SLuigi Rizzo 	 * we have to allocate the pipe array now.
126589cc2556SLuigi Rizzo 	 * XXX get rid of this clumsiness (2014-03-15)
126689cc2556SLuigi Rizzo 	 */
1267f0ea3689SLuigi Rizzo 	error = netmap_pipe_alloc(*na, nmr);
1268f0ea3689SLuigi Rizzo 
1269f9790aebSLuigi Rizzo out:
1270f0ea3689SLuigi Rizzo 	if (error && ret != NULL)
1271f0ea3689SLuigi Rizzo 		netmap_adapter_put(ret);
1272f0ea3689SLuigi Rizzo 
1273f0ea3689SLuigi Rizzo 	if (ifp)
127489cc2556SLuigi Rizzo 		if_rele(ifp); /* allow live unloading of drivers modules */
1275f18be576SLuigi Rizzo 
12765ab0d24dSLuigi Rizzo 	return error;
12775ab0d24dSLuigi Rizzo }
1278ce3ee1e7SLuigi Rizzo 
1279ce3ee1e7SLuigi Rizzo 
1280f9790aebSLuigi Rizzo /*
1281f9790aebSLuigi Rizzo  * validate parameters on entry for *_txsync()
1282f9790aebSLuigi Rizzo  * Returns ring->cur if ok, or something >= kring->nkr_num_slots
128317885a7bSLuigi Rizzo  * in case of error.
1284f9790aebSLuigi Rizzo  *
128517885a7bSLuigi Rizzo  * rhead, rcur and rtail=hwtail are stored from previous round.
128617885a7bSLuigi Rizzo  * hwcur is the next packet to send to the ring.
1287f9790aebSLuigi Rizzo  *
128817885a7bSLuigi Rizzo  * We want
128917885a7bSLuigi Rizzo  *    hwcur <= *rhead <= head <= cur <= tail = *rtail <= hwtail
1290f9790aebSLuigi Rizzo  *
129117885a7bSLuigi Rizzo  * hwcur, rhead, rtail and hwtail are reliable
1292f9790aebSLuigi Rizzo  */
1293f9790aebSLuigi Rizzo u_int
129417885a7bSLuigi Rizzo nm_txsync_prologue(struct netmap_kring *kring)
1295f9790aebSLuigi Rizzo {
1296f9790aebSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
129717885a7bSLuigi Rizzo 	u_int head = ring->head; /* read only once */
1298f9790aebSLuigi Rizzo 	u_int cur = ring->cur; /* read only once */
1299f9790aebSLuigi Rizzo 	u_int n = kring->nkr_num_slots;
1300ce3ee1e7SLuigi Rizzo 
130117885a7bSLuigi Rizzo 	ND(5, "%s kcur %d ktail %d head %d cur %d tail %d",
130217885a7bSLuigi Rizzo 		kring->name,
130317885a7bSLuigi Rizzo 		kring->nr_hwcur, kring->nr_hwtail,
130417885a7bSLuigi Rizzo 		ring->head, ring->cur, ring->tail);
130517885a7bSLuigi Rizzo #if 1 /* kernel sanity checks; but we can trust the kring. */
130617885a7bSLuigi Rizzo 	if (kring->nr_hwcur >= n || kring->rhead >= n ||
130717885a7bSLuigi Rizzo 	    kring->rtail >= n ||  kring->nr_hwtail >= n)
1308f9790aebSLuigi Rizzo 		goto error;
1309f9790aebSLuigi Rizzo #endif /* kernel sanity checks */
131017885a7bSLuigi Rizzo 	/*
131117885a7bSLuigi Rizzo 	 * user sanity checks. We only use 'cur',
131217885a7bSLuigi Rizzo 	 * A, B, ... are possible positions for cur:
131317885a7bSLuigi Rizzo 	 *
131417885a7bSLuigi Rizzo 	 *  0    A  cur   B  tail  C  n-1
131517885a7bSLuigi Rizzo 	 *  0    D  tail  E  cur   F  n-1
131617885a7bSLuigi Rizzo 	 *
131717885a7bSLuigi Rizzo 	 * B, F, D are valid. A, C, E are wrong
131817885a7bSLuigi Rizzo 	 */
131917885a7bSLuigi Rizzo 	if (kring->rtail >= kring->rhead) {
132017885a7bSLuigi Rizzo 		/* want rhead <= head <= rtail */
132117885a7bSLuigi Rizzo 		if (head < kring->rhead || head > kring->rtail)
1322f9790aebSLuigi Rizzo 			goto error;
132317885a7bSLuigi Rizzo 		/* and also head <= cur <= rtail */
132417885a7bSLuigi Rizzo 		if (cur < head || cur > kring->rtail)
1325f9790aebSLuigi Rizzo 			goto error;
132617885a7bSLuigi Rizzo 	} else { /* here rtail < rhead */
132717885a7bSLuigi Rizzo 		/* we need head outside rtail .. rhead */
132817885a7bSLuigi Rizzo 		if (head > kring->rtail && head < kring->rhead)
132917885a7bSLuigi Rizzo 			goto error;
133017885a7bSLuigi Rizzo 
133117885a7bSLuigi Rizzo 		/* two cases now: head <= rtail or head >= rhead  */
133217885a7bSLuigi Rizzo 		if (head <= kring->rtail) {
133317885a7bSLuigi Rizzo 			/* want head <= cur <= rtail */
133417885a7bSLuigi Rizzo 			if (cur < head || cur > kring->rtail)
133517885a7bSLuigi Rizzo 				goto error;
133617885a7bSLuigi Rizzo 		} else { /* head >= rhead */
133717885a7bSLuigi Rizzo 			/* cur must be outside rtail..head */
133817885a7bSLuigi Rizzo 			if (cur > kring->rtail && cur < head)
133917885a7bSLuigi Rizzo 				goto error;
1340f18be576SLuigi Rizzo 		}
1341f9790aebSLuigi Rizzo 	}
134217885a7bSLuigi Rizzo 	if (ring->tail != kring->rtail) {
134317885a7bSLuigi Rizzo 		RD(5, "tail overwritten was %d need %d",
134417885a7bSLuigi Rizzo 			ring->tail, kring->rtail);
134517885a7bSLuigi Rizzo 		ring->tail = kring->rtail;
134617885a7bSLuigi Rizzo 	}
134717885a7bSLuigi Rizzo 	kring->rhead = head;
134817885a7bSLuigi Rizzo 	kring->rcur = cur;
134917885a7bSLuigi Rizzo 	return head;
1350f9790aebSLuigi Rizzo 
1351f9790aebSLuigi Rizzo error:
135217885a7bSLuigi Rizzo 	RD(5, "%s kring error: hwcur %d rcur %d hwtail %d cur %d tail %d",
135317885a7bSLuigi Rizzo 		kring->name,
1354f9790aebSLuigi Rizzo 		kring->nr_hwcur,
135517885a7bSLuigi Rizzo 		kring->rcur, kring->nr_hwtail,
135617885a7bSLuigi Rizzo 		cur, ring->tail);
1357f9790aebSLuigi Rizzo 	return n;
135868b8534bSLuigi Rizzo }
135968b8534bSLuigi Rizzo 
136068b8534bSLuigi Rizzo 
136168b8534bSLuigi Rizzo /*
1362f9790aebSLuigi Rizzo  * validate parameters on entry for *_rxsync()
136317885a7bSLuigi Rizzo  * Returns ring->head if ok, kring->nkr_num_slots on error.
1364f9790aebSLuigi Rizzo  *
136517885a7bSLuigi Rizzo  * For a valid configuration,
136617885a7bSLuigi Rizzo  * hwcur <= head <= cur <= tail <= hwtail
1367f9790aebSLuigi Rizzo  *
136817885a7bSLuigi Rizzo  * We only consider head and cur.
136917885a7bSLuigi Rizzo  * hwcur and hwtail are reliable.
1370f9790aebSLuigi Rizzo  *
1371f9790aebSLuigi Rizzo  */
1372f9790aebSLuigi Rizzo u_int
137317885a7bSLuigi Rizzo nm_rxsync_prologue(struct netmap_kring *kring)
1374f9790aebSLuigi Rizzo {
1375f9790aebSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
137617885a7bSLuigi Rizzo 	uint32_t const n = kring->nkr_num_slots;
137717885a7bSLuigi Rizzo 	uint32_t head, cur;
1378f9790aebSLuigi Rizzo 
137917885a7bSLuigi Rizzo 	ND("%s kc %d kt %d h %d c %d t %d",
138017885a7bSLuigi Rizzo 		kring->name,
138117885a7bSLuigi Rizzo 		kring->nr_hwcur, kring->nr_hwtail,
138217885a7bSLuigi Rizzo 		ring->head, ring->cur, ring->tail);
138317885a7bSLuigi Rizzo 	/*
138417885a7bSLuigi Rizzo 	 * Before storing the new values, we should check they do not
138517885a7bSLuigi Rizzo 	 * move backwards. However:
138617885a7bSLuigi Rizzo 	 * - head is not an issue because the previous value is hwcur;
138717885a7bSLuigi Rizzo 	 * - cur could in principle go back, however it does not matter
138817885a7bSLuigi Rizzo 	 *   because we are processing a brand new rxsync()
138917885a7bSLuigi Rizzo 	 */
139017885a7bSLuigi Rizzo 	cur = kring->rcur = ring->cur;	/* read only once */
139117885a7bSLuigi Rizzo 	head = kring->rhead = ring->head;	/* read only once */
1392f9790aebSLuigi Rizzo #if 1 /* kernel sanity checks */
139317885a7bSLuigi Rizzo 	if (kring->nr_hwcur >= n || kring->nr_hwtail >= n)
1394f9790aebSLuigi Rizzo 		goto error;
1395f9790aebSLuigi Rizzo #endif /* kernel sanity checks */
1396f9790aebSLuigi Rizzo 	/* user sanity checks */
139717885a7bSLuigi Rizzo 	if (kring->nr_hwtail >= kring->nr_hwcur) {
139817885a7bSLuigi Rizzo 		/* want hwcur <= rhead <= hwtail */
139917885a7bSLuigi Rizzo 		if (head < kring->nr_hwcur || head > kring->nr_hwtail)
1400f9790aebSLuigi Rizzo 			goto error;
140117885a7bSLuigi Rizzo 		/* and also rhead <= rcur <= hwtail */
140217885a7bSLuigi Rizzo 		if (cur < head || cur > kring->nr_hwtail)
1403f9790aebSLuigi Rizzo 			goto error;
1404f9790aebSLuigi Rizzo 	} else {
140517885a7bSLuigi Rizzo 		/* we need rhead outside hwtail..hwcur */
140617885a7bSLuigi Rizzo 		if (head < kring->nr_hwcur && head > kring->nr_hwtail)
1407f9790aebSLuigi Rizzo 			goto error;
140817885a7bSLuigi Rizzo 		/* two cases now: head <= hwtail or head >= hwcur  */
140917885a7bSLuigi Rizzo 		if (head <= kring->nr_hwtail) {
141017885a7bSLuigi Rizzo 			/* want head <= cur <= hwtail */
141117885a7bSLuigi Rizzo 			if (cur < head || cur > kring->nr_hwtail)
1412f9790aebSLuigi Rizzo 				goto error;
141317885a7bSLuigi Rizzo 		} else {
141417885a7bSLuigi Rizzo 			/* cur must be outside hwtail..head */
141517885a7bSLuigi Rizzo 			if (cur < head && cur > kring->nr_hwtail)
1416f9790aebSLuigi Rizzo 				goto error;
1417f9790aebSLuigi Rizzo 		}
1418f9790aebSLuigi Rizzo 	}
141917885a7bSLuigi Rizzo 	if (ring->tail != kring->rtail) {
142017885a7bSLuigi Rizzo 		RD(5, "%s tail overwritten was %d need %d",
142117885a7bSLuigi Rizzo 			kring->name,
142217885a7bSLuigi Rizzo 			ring->tail, kring->rtail);
142317885a7bSLuigi Rizzo 		ring->tail = kring->rtail;
142417885a7bSLuigi Rizzo 	}
142517885a7bSLuigi Rizzo 	return head;
1426f9790aebSLuigi Rizzo 
1427f9790aebSLuigi Rizzo error:
142817885a7bSLuigi Rizzo 	RD(5, "kring error: hwcur %d rcur %d hwtail %d head %d cur %d tail %d",
1429f9790aebSLuigi Rizzo 		kring->nr_hwcur,
143017885a7bSLuigi Rizzo 		kring->rcur, kring->nr_hwtail,
143117885a7bSLuigi Rizzo 		kring->rhead, kring->rcur, ring->tail);
1432f9790aebSLuigi Rizzo 	return n;
1433f9790aebSLuigi Rizzo }
1434f9790aebSLuigi Rizzo 
143517885a7bSLuigi Rizzo 
1436f9790aebSLuigi Rizzo /*
143768b8534bSLuigi Rizzo  * Error routine called when txsync/rxsync detects an error.
143817885a7bSLuigi Rizzo  * Can't do much more than resetting head =cur = hwcur, tail = hwtail
143968b8534bSLuigi Rizzo  * Return 1 on reinit.
1440506cc70cSLuigi Rizzo  *
1441506cc70cSLuigi Rizzo  * This routine is only called by the upper half of the kernel.
1442506cc70cSLuigi Rizzo  * It only reads hwcur (which is changed only by the upper half, too)
144317885a7bSLuigi Rizzo  * and hwtail (which may be changed by the lower half, but only on
1444506cc70cSLuigi Rizzo  * a tx ring and only to increase it, so any error will be recovered
1445506cc70cSLuigi Rizzo  * on the next call). For the above, we don't strictly need to call
1446506cc70cSLuigi Rizzo  * it under lock.
144768b8534bSLuigi Rizzo  */
144868b8534bSLuigi Rizzo int
144968b8534bSLuigi Rizzo netmap_ring_reinit(struct netmap_kring *kring)
145068b8534bSLuigi Rizzo {
145168b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
145268b8534bSLuigi Rizzo 	u_int i, lim = kring->nkr_num_slots - 1;
145368b8534bSLuigi Rizzo 	int errors = 0;
145468b8534bSLuigi Rizzo 
1455ce3ee1e7SLuigi Rizzo 	// XXX KASSERT nm_kr_tryget
1456f9790aebSLuigi Rizzo 	RD(10, "called for %s", NM_IFPNAME(kring->na->ifp));
145717885a7bSLuigi Rizzo 	// XXX probably wrong to trust userspace
145817885a7bSLuigi Rizzo 	kring->rhead = ring->head;
145917885a7bSLuigi Rizzo 	kring->rcur  = ring->cur;
146017885a7bSLuigi Rizzo 	kring->rtail = ring->tail;
146117885a7bSLuigi Rizzo 
146268b8534bSLuigi Rizzo 	if (ring->cur > lim)
146368b8534bSLuigi Rizzo 		errors++;
146417885a7bSLuigi Rizzo 	if (ring->head > lim)
146517885a7bSLuigi Rizzo 		errors++;
146617885a7bSLuigi Rizzo 	if (ring->tail > lim)
146717885a7bSLuigi Rizzo 		errors++;
146868b8534bSLuigi Rizzo 	for (i = 0; i <= lim; i++) {
146968b8534bSLuigi Rizzo 		u_int idx = ring->slot[i].buf_idx;
147068b8534bSLuigi Rizzo 		u_int len = ring->slot[i].len;
147168b8534bSLuigi Rizzo 		if (idx < 2 || idx >= netmap_total_buffers) {
147217885a7bSLuigi Rizzo 			RD(5, "bad index at slot %d idx %d len %d ", i, idx, len);
147368b8534bSLuigi Rizzo 			ring->slot[i].buf_idx = 0;
147468b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
1475ce3ee1e7SLuigi Rizzo 		} else if (len > NETMAP_BDG_BUF_SIZE(kring->na->nm_mem)) {
147668b8534bSLuigi Rizzo 			ring->slot[i].len = 0;
147717885a7bSLuigi Rizzo 			RD(5, "bad len at slot %d idx %d len %d", i, idx, len);
147868b8534bSLuigi Rizzo 		}
147968b8534bSLuigi Rizzo 	}
148068b8534bSLuigi Rizzo 	if (errors) {
14818241616dSLuigi Rizzo 		RD(10, "total %d errors", errors);
148217885a7bSLuigi Rizzo 		RD(10, "%s reinit, cur %d -> %d tail %d -> %d",
148317885a7bSLuigi Rizzo 			kring->name,
148468b8534bSLuigi Rizzo 			ring->cur, kring->nr_hwcur,
148517885a7bSLuigi Rizzo 			ring->tail, kring->nr_hwtail);
148617885a7bSLuigi Rizzo 		ring->head = kring->rhead = kring->nr_hwcur;
148717885a7bSLuigi Rizzo 		ring->cur  = kring->rcur  = kring->nr_hwcur;
148817885a7bSLuigi Rizzo 		ring->tail = kring->rtail = kring->nr_hwtail;
148968b8534bSLuigi Rizzo 	}
149068b8534bSLuigi Rizzo 	return (errors ? 1 : 0);
149168b8534bSLuigi Rizzo }
149268b8534bSLuigi Rizzo 
149368b8534bSLuigi Rizzo 
149468b8534bSLuigi Rizzo /*
149568b8534bSLuigi Rizzo  * Set the ring ID. For devices with a single queue, a request
149668b8534bSLuigi Rizzo  * for all rings is the same as a single ring.
149768b8534bSLuigi Rizzo  */
149868b8534bSLuigi Rizzo static int
1499f0ea3689SLuigi Rizzo netmap_set_ringid(struct netmap_priv_d *priv, uint16_t ringid, uint32_t flags)
150068b8534bSLuigi Rizzo {
1501f9790aebSLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1502f0ea3689SLuigi Rizzo 	u_int j, i = ringid & NETMAP_RING_MASK;
1503f0ea3689SLuigi Rizzo 	u_int reg = flags & NR_REG_MASK;
150468b8534bSLuigi Rizzo 
1505f0ea3689SLuigi Rizzo 	if (reg == NR_REG_DEFAULT) {
1506f0ea3689SLuigi Rizzo 		/* convert from old ringid to flags */
150768b8534bSLuigi Rizzo 		if (ringid & NETMAP_SW_RING) {
1508f0ea3689SLuigi Rizzo 			reg = NR_REG_SW;
150968b8534bSLuigi Rizzo 		} else if (ringid & NETMAP_HW_RING) {
1510f0ea3689SLuigi Rizzo 			reg = NR_REG_ONE_NIC;
151168b8534bSLuigi Rizzo 		} else {
1512f0ea3689SLuigi Rizzo 			reg = NR_REG_ALL_NIC;
1513f0ea3689SLuigi Rizzo 		}
1514f0ea3689SLuigi Rizzo 		D("deprecated API, old ringid 0x%x -> ringid %x reg %d", ringid, i, reg);
1515f0ea3689SLuigi Rizzo 	}
1516f0ea3689SLuigi Rizzo 	switch (reg) {
1517f0ea3689SLuigi Rizzo 	case NR_REG_ALL_NIC:
1518f0ea3689SLuigi Rizzo 	case NR_REG_PIPE_MASTER:
1519f0ea3689SLuigi Rizzo 	case NR_REG_PIPE_SLAVE:
1520f0ea3689SLuigi Rizzo 		priv->np_txqfirst = 0;
1521f0ea3689SLuigi Rizzo 		priv->np_txqlast = na->num_tx_rings;
1522f0ea3689SLuigi Rizzo 		priv->np_rxqfirst = 0;
1523f0ea3689SLuigi Rizzo 		priv->np_rxqlast = na->num_rx_rings;
1524f0ea3689SLuigi Rizzo 		ND("%s %d %d", "ALL/PIPE",
1525f0ea3689SLuigi Rizzo 			priv->np_rxqfirst, priv->np_rxqlast);
1526f0ea3689SLuigi Rizzo 		break;
1527f0ea3689SLuigi Rizzo 	case NR_REG_SW:
1528f0ea3689SLuigi Rizzo 	case NR_REG_NIC_SW:
1529f0ea3689SLuigi Rizzo 		if (!(na->na_flags & NAF_HOST_RINGS)) {
1530f0ea3689SLuigi Rizzo 			D("host rings not supported");
1531f0ea3689SLuigi Rizzo 			return EINVAL;
1532f0ea3689SLuigi Rizzo 		}
1533f0ea3689SLuigi Rizzo 		priv->np_txqfirst = (reg == NR_REG_SW ?
1534f0ea3689SLuigi Rizzo 			na->num_tx_rings : 0);
1535f0ea3689SLuigi Rizzo 		priv->np_txqlast = na->num_tx_rings + 1;
1536f0ea3689SLuigi Rizzo 		priv->np_rxqfirst = (reg == NR_REG_SW ?
1537f0ea3689SLuigi Rizzo 			na->num_rx_rings : 0);
1538f0ea3689SLuigi Rizzo 		priv->np_rxqlast = na->num_rx_rings + 1;
1539f0ea3689SLuigi Rizzo 		ND("%s %d %d", reg == NR_REG_SW ? "SW" : "NIC+SW",
1540f0ea3689SLuigi Rizzo 			priv->np_rxqfirst, priv->np_rxqlast);
1541f0ea3689SLuigi Rizzo 		break;
1542f0ea3689SLuigi Rizzo 	case NR_REG_ONE_NIC:
1543f0ea3689SLuigi Rizzo 		if (i >= na->num_tx_rings && i >= na->num_rx_rings) {
1544f0ea3689SLuigi Rizzo 			D("invalid ring id %d", i);
1545f0ea3689SLuigi Rizzo 			return EINVAL;
1546f0ea3689SLuigi Rizzo 		}
1547f0ea3689SLuigi Rizzo 		/* if not enough rings, use the first one */
1548f0ea3689SLuigi Rizzo 		j = i;
1549f0ea3689SLuigi Rizzo 		if (j >= na->num_tx_rings)
1550f0ea3689SLuigi Rizzo 			j = 0;
1551f0ea3689SLuigi Rizzo 		priv->np_txqfirst = j;
1552f0ea3689SLuigi Rizzo 		priv->np_txqlast = j + 1;
1553f0ea3689SLuigi Rizzo 		j = i;
1554f0ea3689SLuigi Rizzo 		if (j >= na->num_rx_rings)
1555f0ea3689SLuigi Rizzo 			j = 0;
1556f0ea3689SLuigi Rizzo 		priv->np_rxqfirst = j;
1557f0ea3689SLuigi Rizzo 		priv->np_rxqlast = j + 1;
1558f0ea3689SLuigi Rizzo 		break;
1559f0ea3689SLuigi Rizzo 	default:
1560f0ea3689SLuigi Rizzo 		D("invalid regif type %d", reg);
1561f0ea3689SLuigi Rizzo 		return EINVAL;
156268b8534bSLuigi Rizzo 	}
156368b8534bSLuigi Rizzo 	priv->np_txpoll = (ringid & NETMAP_NO_TX_POLL) ? 0 : 1;
1564f0ea3689SLuigi Rizzo 	priv->np_flags = (flags & ~NR_REG_MASK) | reg;
1565f0ea3689SLuigi Rizzo 	if (nm_tx_si_user(priv))
1566f0ea3689SLuigi Rizzo 		na->tx_si_users++;
1567f0ea3689SLuigi Rizzo 	if (nm_rx_si_user(priv))
1568f0ea3689SLuigi Rizzo 		na->rx_si_users++;
1569ae10d1afSLuigi Rizzo 	if (netmap_verbose) {
1570f0ea3689SLuigi Rizzo 		D("%s: tx [%d,%d) rx [%d,%d) id %d",
1571f0ea3689SLuigi Rizzo 			NM_IFPNAME(na->ifp),
1572f0ea3689SLuigi Rizzo 			priv->np_txqfirst,
1573f0ea3689SLuigi Rizzo 			priv->np_txqlast,
1574f0ea3689SLuigi Rizzo 			priv->np_rxqfirst,
1575f0ea3689SLuigi Rizzo 			priv->np_rxqlast,
1576f0ea3689SLuigi Rizzo 			i);
1577ae10d1afSLuigi Rizzo 	}
157868b8534bSLuigi Rizzo 	return 0;
157968b8534bSLuigi Rizzo }
158068b8534bSLuigi Rizzo 
1581f18be576SLuigi Rizzo /*
1582f18be576SLuigi Rizzo  * possibly move the interface to netmap-mode.
1583f18be576SLuigi Rizzo  * If success it returns a pointer to netmap_if, otherwise NULL.
1584ce3ee1e7SLuigi Rizzo  * This must be called with NMG_LOCK held.
1585f18be576SLuigi Rizzo  */
1586f9790aebSLuigi Rizzo struct netmap_if *
1587f9790aebSLuigi Rizzo netmap_do_regif(struct netmap_priv_d *priv, struct netmap_adapter *na,
1588f0ea3689SLuigi Rizzo 	uint16_t ringid, uint32_t flags, int *err)
1589f18be576SLuigi Rizzo {
1590f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
1591f18be576SLuigi Rizzo 	struct netmap_if *nifp = NULL;
1592f9790aebSLuigi Rizzo 	int error, need_mem = 0;
1593f18be576SLuigi Rizzo 
1594ce3ee1e7SLuigi Rizzo 	NMG_LOCK_ASSERT();
1595f18be576SLuigi Rizzo 	/* ring configuration may have changed, fetch from the card */
1596f18be576SLuigi Rizzo 	netmap_update_config(na);
1597f9790aebSLuigi Rizzo 	priv->np_na = na;     /* store the reference */
1598f0ea3689SLuigi Rizzo 	error = netmap_set_ringid(priv, ringid, flags);
1599f18be576SLuigi Rizzo 	if (error)
1600f18be576SLuigi Rizzo 		goto out;
1601ce3ee1e7SLuigi Rizzo 	/* ensure allocators are ready */
1602ce3ee1e7SLuigi Rizzo 	need_mem = !netmap_have_memory_locked(priv);
1603ce3ee1e7SLuigi Rizzo 	if (need_mem) {
1604ce3ee1e7SLuigi Rizzo 		error = netmap_get_memory_locked(priv);
1605ce3ee1e7SLuigi Rizzo 		ND("get_memory returned %d", error);
1606ce3ee1e7SLuigi Rizzo 		if (error)
1607ce3ee1e7SLuigi Rizzo 			goto out;
1608ce3ee1e7SLuigi Rizzo 	}
1609f9790aebSLuigi Rizzo 	nifp = netmap_if_new(NM_IFPNAME(ifp), na);
161089cc2556SLuigi Rizzo 
161189cc2556SLuigi Rizzo 	/* Allocate a netmap_if and, if necessary, all the netmap_ring's */
1612f18be576SLuigi Rizzo 	if (nifp == NULL) { /* allocation failed */
1613f18be576SLuigi Rizzo 		error = ENOMEM;
1614ce3ee1e7SLuigi Rizzo 		goto out;
1615ce3ee1e7SLuigi Rizzo 	}
1616f9790aebSLuigi Rizzo 	na->active_fds++;
1617ce3ee1e7SLuigi Rizzo 	if (ifp->if_capenable & IFCAP_NETMAP) {
1618f18be576SLuigi Rizzo 		/* was already set */
1619f18be576SLuigi Rizzo 	} else {
1620f18be576SLuigi Rizzo 		/* Otherwise set the card in netmap mode
1621f18be576SLuigi Rizzo 		 * and make it use the shared buffers.
1622ce3ee1e7SLuigi Rizzo 		 */
162389cc2556SLuigi Rizzo 		/* cache the allocator info in the na */
1624f9790aebSLuigi Rizzo 		na->na_lut = na->nm_mem->pools[NETMAP_BUF_POOL].lut;
1625f9790aebSLuigi Rizzo 		ND("%p->na_lut == %p", na, na->na_lut);
1626f9790aebSLuigi Rizzo 		na->na_lut_objtotal = na->nm_mem->pools[NETMAP_BUF_POOL].objtotal;
1627f9790aebSLuigi Rizzo 		error = na->nm_register(na, 1); /* mode on */
1628f18be576SLuigi Rizzo 		if (error) {
1629ce3ee1e7SLuigi Rizzo 			netmap_do_unregif(priv, nifp);
1630f18be576SLuigi Rizzo 			nifp = NULL;
1631f18be576SLuigi Rizzo 		}
1632f18be576SLuigi Rizzo 	}
1633f18be576SLuigi Rizzo out:
1634f18be576SLuigi Rizzo 	*err = error;
1635f9790aebSLuigi Rizzo 	if (error) {
1636f9790aebSLuigi Rizzo 		priv->np_na = NULL;
163789cc2556SLuigi Rizzo 		/* we should drop the allocator, but only
163889cc2556SLuigi Rizzo 		 * if we were the ones who grabbed it
163989cc2556SLuigi Rizzo 		 */
1640f9790aebSLuigi Rizzo 		if (need_mem)
1641f9790aebSLuigi Rizzo 			netmap_drop_memory_locked(priv);
1642f9790aebSLuigi Rizzo 	}
1643ce3ee1e7SLuigi Rizzo 	if (nifp != NULL) {
1644ce3ee1e7SLuigi Rizzo 		/*
1645ce3ee1e7SLuigi Rizzo 		 * advertise that the interface is ready bt setting ni_nifp.
1646ce3ee1e7SLuigi Rizzo 		 * The barrier is needed because readers (poll and *SYNC)
1647ce3ee1e7SLuigi Rizzo 		 * check for priv->np_nifp != NULL without locking
1648ce3ee1e7SLuigi Rizzo 		 */
1649ce3ee1e7SLuigi Rizzo 		wmb(); /* make sure previous writes are visible to all CPUs */
1650ce3ee1e7SLuigi Rizzo 		priv->np_nifp = nifp;
1651ce3ee1e7SLuigi Rizzo 	}
1652f18be576SLuigi Rizzo 	return nifp;
1653f18be576SLuigi Rizzo }
1654f18be576SLuigi Rizzo 
1655f18be576SLuigi Rizzo 
1656f18be576SLuigi Rizzo 
165768b8534bSLuigi Rizzo /*
165868b8534bSLuigi Rizzo  * ioctl(2) support for the "netmap" device.
165968b8534bSLuigi Rizzo  *
166068b8534bSLuigi Rizzo  * Following a list of accepted commands:
166168b8534bSLuigi Rizzo  * - NIOCGINFO
166268b8534bSLuigi Rizzo  * - SIOCGIFADDR	just for convenience
166368b8534bSLuigi Rizzo  * - NIOCREGIF
166468b8534bSLuigi Rizzo  * - NIOCTXSYNC
166568b8534bSLuigi Rizzo  * - NIOCRXSYNC
166668b8534bSLuigi Rizzo  *
166768b8534bSLuigi Rizzo  * Return 0 on success, errno otherwise.
166868b8534bSLuigi Rizzo  */
1669f9790aebSLuigi Rizzo int
16700b8ed8e0SLuigi Rizzo netmap_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
16710b8ed8e0SLuigi Rizzo 	int fflag, struct thread *td)
167268b8534bSLuigi Rizzo {
167368b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
1674ce3ee1e7SLuigi Rizzo 	struct ifnet *ifp = NULL;
167568b8534bSLuigi Rizzo 	struct nmreq *nmr = (struct nmreq *) data;
1676ce3ee1e7SLuigi Rizzo 	struct netmap_adapter *na = NULL;
167768b8534bSLuigi Rizzo 	int error;
1678f0ea3689SLuigi Rizzo 	u_int i, qfirst, qlast;
167968b8534bSLuigi Rizzo 	struct netmap_if *nifp;
1680ce3ee1e7SLuigi Rizzo 	struct netmap_kring *krings;
168168b8534bSLuigi Rizzo 
16820b8ed8e0SLuigi Rizzo 	(void)dev;	/* UNUSED */
16830b8ed8e0SLuigi Rizzo 	(void)fflag;	/* UNUSED */
1684f196ce38SLuigi Rizzo 
168517885a7bSLuigi Rizzo 	if (cmd == NIOCGINFO || cmd == NIOCREGIF) {
168617885a7bSLuigi Rizzo 		/* truncate name */
168717885a7bSLuigi Rizzo 		nmr->nr_name[sizeof(nmr->nr_name) - 1] = '\0';
168817885a7bSLuigi Rizzo 		if (nmr->nr_version != NETMAP_API) {
168917885a7bSLuigi Rizzo 			D("API mismatch for %s got %d need %d",
169017885a7bSLuigi Rizzo 				nmr->nr_name,
169117885a7bSLuigi Rizzo 				nmr->nr_version, NETMAP_API);
169217885a7bSLuigi Rizzo 			nmr->nr_version = NETMAP_API;
1693f0ea3689SLuigi Rizzo 		}
1694f0ea3689SLuigi Rizzo 		if (nmr->nr_version < NETMAP_MIN_API ||
1695f0ea3689SLuigi Rizzo 		    nmr->nr_version > NETMAP_MAX_API) {
169617885a7bSLuigi Rizzo 			return EINVAL;
169717885a7bSLuigi Rizzo 		}
169817885a7bSLuigi Rizzo 	}
1699506cc70cSLuigi Rizzo 	CURVNET_SET(TD_TO_VNET(td));
1700506cc70cSLuigi Rizzo 
170168b8534bSLuigi Rizzo 	error = devfs_get_cdevpriv((void **)&priv);
17028241616dSLuigi Rizzo 	if (error) {
1703506cc70cSLuigi Rizzo 		CURVNET_RESTORE();
17048241616dSLuigi Rizzo 		/* XXX ENOENT should be impossible, since the priv
17058241616dSLuigi Rizzo 		 * is now created in the open */
17068241616dSLuigi Rizzo 		return (error == ENOENT ? ENXIO : error);
1707506cc70cSLuigi Rizzo 	}
170868b8534bSLuigi Rizzo 
170968b8534bSLuigi Rizzo 	switch (cmd) {
171068b8534bSLuigi Rizzo 	case NIOCGINFO:		/* return capabilities etc */
1711f18be576SLuigi Rizzo 		if (nmr->nr_cmd == NETMAP_BDG_LIST) {
1712f18be576SLuigi Rizzo 			error = netmap_bdg_ctl(nmr, NULL);
1713f18be576SLuigi Rizzo 			break;
1714f18be576SLuigi Rizzo 		}
1715ce3ee1e7SLuigi Rizzo 
1716ce3ee1e7SLuigi Rizzo 		NMG_LOCK();
1717ce3ee1e7SLuigi Rizzo 		do {
1718ce3ee1e7SLuigi Rizzo 			/* memsize is always valid */
1719ce3ee1e7SLuigi Rizzo 			struct netmap_mem_d *nmd = &nm_mem;
1720ce3ee1e7SLuigi Rizzo 			u_int memflags;
1721ce3ee1e7SLuigi Rizzo 
1722ce3ee1e7SLuigi Rizzo 			if (nmr->nr_name[0] != '\0') {
1723ce3ee1e7SLuigi Rizzo 				/* get a refcount */
1724f9790aebSLuigi Rizzo 				error = netmap_get_na(nmr, &na, 1 /* create */);
17258241616dSLuigi Rizzo 				if (error)
17268241616dSLuigi Rizzo 					break;
1727f9790aebSLuigi Rizzo 				nmd = na->nm_mem; /* get memory allocator */
1728ce3ee1e7SLuigi Rizzo 			}
1729ce3ee1e7SLuigi Rizzo 
1730f0ea3689SLuigi Rizzo 			error = netmap_mem_get_info(nmd, &nmr->nr_memsize, &memflags,
1731f0ea3689SLuigi Rizzo 				&nmr->nr_arg2);
1732ce3ee1e7SLuigi Rizzo 			if (error)
1733ce3ee1e7SLuigi Rizzo 				break;
1734ce3ee1e7SLuigi Rizzo 			if (na == NULL) /* only memory info */
1735ce3ee1e7SLuigi Rizzo 				break;
17368241616dSLuigi Rizzo 			nmr->nr_offset = 0;
17378241616dSLuigi Rizzo 			nmr->nr_rx_slots = nmr->nr_tx_slots = 0;
1738ae10d1afSLuigi Rizzo 			netmap_update_config(na);
1739d76bf4ffSLuigi Rizzo 			nmr->nr_rx_rings = na->num_rx_rings;
1740d76bf4ffSLuigi Rizzo 			nmr->nr_tx_rings = na->num_tx_rings;
174164ae02c3SLuigi Rizzo 			nmr->nr_rx_slots = na->num_rx_desc;
174264ae02c3SLuigi Rizzo 			nmr->nr_tx_slots = na->num_tx_desc;
1743f9790aebSLuigi Rizzo 			netmap_adapter_put(na);
1744ce3ee1e7SLuigi Rizzo 		} while (0);
1745ce3ee1e7SLuigi Rizzo 		NMG_UNLOCK();
174668b8534bSLuigi Rizzo 		break;
174768b8534bSLuigi Rizzo 
174868b8534bSLuigi Rizzo 	case NIOCREGIF:
1749f18be576SLuigi Rizzo 		/* possibly attach/detach NIC and VALE switch */
1750f18be576SLuigi Rizzo 		i = nmr->nr_cmd;
1751f9790aebSLuigi Rizzo 		if (i == NETMAP_BDG_ATTACH || i == NETMAP_BDG_DETACH
1752f0ea3689SLuigi Rizzo 				|| i == NETMAP_BDG_VNET_HDR) {
1753f18be576SLuigi Rizzo 			error = netmap_bdg_ctl(nmr, NULL);
1754f18be576SLuigi Rizzo 			break;
1755f18be576SLuigi Rizzo 		} else if (i != 0) {
1756f18be576SLuigi Rizzo 			D("nr_cmd must be 0 not %d", i);
1757f18be576SLuigi Rizzo 			error = EINVAL;
1758f18be576SLuigi Rizzo 			break;
1759f18be576SLuigi Rizzo 		}
1760f18be576SLuigi Rizzo 
17618241616dSLuigi Rizzo 		/* protect access to priv from concurrent NIOCREGIF */
1762ce3ee1e7SLuigi Rizzo 		NMG_LOCK();
1763ce3ee1e7SLuigi Rizzo 		do {
1764ce3ee1e7SLuigi Rizzo 			u_int memflags;
1765ce3ee1e7SLuigi Rizzo 
1766f9790aebSLuigi Rizzo 			if (priv->np_na != NULL) {	/* thread already registered */
1767f0ea3689SLuigi Rizzo 				error = EBUSY;
1768506cc70cSLuigi Rizzo 				break;
1769506cc70cSLuigi Rizzo 			}
177068b8534bSLuigi Rizzo 			/* find the interface and a reference */
1771f9790aebSLuigi Rizzo 			error = netmap_get_na(nmr, &na, 1 /* create */); /* keep reference */
177268b8534bSLuigi Rizzo 			if (error)
1773ce3ee1e7SLuigi Rizzo 				break;
1774f9790aebSLuigi Rizzo 			ifp = na->ifp;
1775f9790aebSLuigi Rizzo 			if (NETMAP_OWNED_BY_KERN(na)) {
1776f9790aebSLuigi Rizzo 				netmap_adapter_put(na);
1777ce3ee1e7SLuigi Rizzo 				error = EBUSY;
1778ce3ee1e7SLuigi Rizzo 				break;
1779f196ce38SLuigi Rizzo 			}
1780f0ea3689SLuigi Rizzo 			nifp = netmap_do_regif(priv, na, nmr->nr_ringid, nmr->nr_flags, &error);
1781f18be576SLuigi Rizzo 			if (!nifp) {    /* reg. failed, release priv and ref */
1782f9790aebSLuigi Rizzo 				netmap_adapter_put(na);
17838241616dSLuigi Rizzo 				priv->np_nifp = NULL;
1784ce3ee1e7SLuigi Rizzo 				break;
178568b8534bSLuigi Rizzo 			}
1786f0ea3689SLuigi Rizzo 			priv->np_td = td; // XXX kqueue, debugging only
178768b8534bSLuigi Rizzo 
178868b8534bSLuigi Rizzo 			/* return the offset of the netmap_if object */
1789d76bf4ffSLuigi Rizzo 			nmr->nr_rx_rings = na->num_rx_rings;
1790d76bf4ffSLuigi Rizzo 			nmr->nr_tx_rings = na->num_tx_rings;
179164ae02c3SLuigi Rizzo 			nmr->nr_rx_slots = na->num_rx_desc;
179264ae02c3SLuigi Rizzo 			nmr->nr_tx_slots = na->num_tx_desc;
1793f0ea3689SLuigi Rizzo 			error = netmap_mem_get_info(na->nm_mem, &nmr->nr_memsize, &memflags,
1794f0ea3689SLuigi Rizzo 				&nmr->nr_arg2);
1795ce3ee1e7SLuigi Rizzo 			if (error) {
1796f9790aebSLuigi Rizzo 				netmap_adapter_put(na);
1797ce3ee1e7SLuigi Rizzo 				break;
1798ce3ee1e7SLuigi Rizzo 			}
1799ce3ee1e7SLuigi Rizzo 			if (memflags & NETMAP_MEM_PRIVATE) {
18003d819cb6SLuigi Rizzo 				*(uint32_t *)(uintptr_t)&nifp->ni_flags |= NI_PRIV_MEM;
1801ce3ee1e7SLuigi Rizzo 			}
1802f0ea3689SLuigi Rizzo 			priv->np_txsi = (priv->np_txqlast - priv->np_txqfirst > 1) ?
1803f0ea3689SLuigi Rizzo 				&na->tx_si : &na->tx_rings[priv->np_txqfirst].si;
1804f0ea3689SLuigi Rizzo 			priv->np_rxsi = (priv->np_rxqlast - priv->np_rxqfirst > 1) ?
1805f0ea3689SLuigi Rizzo 				&na->rx_si : &na->rx_rings[priv->np_rxqfirst].si;
1806f0ea3689SLuigi Rizzo 
1807f0ea3689SLuigi Rizzo 			if (nmr->nr_arg3) {
1808f0ea3689SLuigi Rizzo 				D("requested %d extra buffers", nmr->nr_arg3);
1809f0ea3689SLuigi Rizzo 				nmr->nr_arg3 = netmap_extra_alloc(na,
1810f0ea3689SLuigi Rizzo 					&nifp->ni_bufs_head, nmr->nr_arg3);
1811f0ea3689SLuigi Rizzo 				D("got %d extra buffers", nmr->nr_arg3);
1812f0ea3689SLuigi Rizzo 			}
1813ce3ee1e7SLuigi Rizzo 			nmr->nr_offset = netmap_mem_if_offset(na->nm_mem, nifp);
1814ce3ee1e7SLuigi Rizzo 		} while (0);
1815ce3ee1e7SLuigi Rizzo 		NMG_UNLOCK();
181668b8534bSLuigi Rizzo 		break;
181768b8534bSLuigi Rizzo 
181868b8534bSLuigi Rizzo 	case NIOCTXSYNC:
181968b8534bSLuigi Rizzo 	case NIOCRXSYNC:
18208241616dSLuigi Rizzo 		nifp = priv->np_nifp;
18218241616dSLuigi Rizzo 
18228241616dSLuigi Rizzo 		if (nifp == NULL) {
1823506cc70cSLuigi Rizzo 			error = ENXIO;
1824506cc70cSLuigi Rizzo 			break;
1825506cc70cSLuigi Rizzo 		}
18268241616dSLuigi Rizzo 		rmb(); /* make sure following reads are not from cache */
18278241616dSLuigi Rizzo 
1828f9790aebSLuigi Rizzo 		na = priv->np_na;      /* we have a reference */
18298241616dSLuigi Rizzo 
1830f9790aebSLuigi Rizzo 		if (na == NULL) {
1831f9790aebSLuigi Rizzo 			D("Internal error: nifp != NULL && na == NULL");
18328241616dSLuigi Rizzo 			error = ENXIO;
18338241616dSLuigi Rizzo 			break;
18348241616dSLuigi Rizzo 		}
18358241616dSLuigi Rizzo 
1836f9790aebSLuigi Rizzo 		ifp = na->ifp;
1837f9790aebSLuigi Rizzo 		if (ifp == NULL) {
1838f9790aebSLuigi Rizzo 			RD(1, "the ifp is gone");
1839f9790aebSLuigi Rizzo 			error = ENXIO;
1840f9790aebSLuigi Rizzo 			break;
1841f9790aebSLuigi Rizzo 		}
1842f9790aebSLuigi Rizzo 
1843f0ea3689SLuigi Rizzo 		if (cmd == NIOCTXSYNC) {
1844f0ea3689SLuigi Rizzo 			krings = na->tx_rings;
1845f0ea3689SLuigi Rizzo 			qfirst = priv->np_txqfirst;
1846f0ea3689SLuigi Rizzo 			qlast = priv->np_txqlast;
1847f0ea3689SLuigi Rizzo 		} else {
1848f0ea3689SLuigi Rizzo 			krings = na->rx_rings;
1849f0ea3689SLuigi Rizzo 			qfirst = priv->np_rxqfirst;
1850f0ea3689SLuigi Rizzo 			qlast = priv->np_rxqlast;
185168b8534bSLuigi Rizzo 		}
185268b8534bSLuigi Rizzo 
1853f0ea3689SLuigi Rizzo 		for (i = qfirst; i < qlast; i++) {
1854ce3ee1e7SLuigi Rizzo 			struct netmap_kring *kring = krings + i;
1855ce3ee1e7SLuigi Rizzo 			if (nm_kr_tryget(kring)) {
1856ce3ee1e7SLuigi Rizzo 				error = EBUSY;
1857ce3ee1e7SLuigi Rizzo 				goto out;
1858ce3ee1e7SLuigi Rizzo 			}
185968b8534bSLuigi Rizzo 			if (cmd == NIOCTXSYNC) {
186068b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
18613c0caf6cSLuigi Rizzo 					D("pre txsync ring %d cur %d hwcur %d",
186268b8534bSLuigi Rizzo 					    i, kring->ring->cur,
186368b8534bSLuigi Rizzo 					    kring->nr_hwcur);
186417885a7bSLuigi Rizzo 				if (nm_txsync_prologue(kring) >= kring->nkr_num_slots) {
186517885a7bSLuigi Rizzo 					netmap_ring_reinit(kring);
186617885a7bSLuigi Rizzo 				} else {
1867f0ea3689SLuigi Rizzo 					kring->nm_sync(kring, NAF_FORCE_RECLAIM);
186817885a7bSLuigi Rizzo 				}
186968b8534bSLuigi Rizzo 				if (netmap_verbose & NM_VERB_TXSYNC)
18703c0caf6cSLuigi Rizzo 					D("post txsync ring %d cur %d hwcur %d",
187168b8534bSLuigi Rizzo 					    i, kring->ring->cur,
187268b8534bSLuigi Rizzo 					    kring->nr_hwcur);
187368b8534bSLuigi Rizzo 			} else {
1874f0ea3689SLuigi Rizzo 				kring->nm_sync(kring, NAF_FORCE_READ);
187568b8534bSLuigi Rizzo 				microtime(&na->rx_rings[i].ring->ts);
187668b8534bSLuigi Rizzo 			}
1877ce3ee1e7SLuigi Rizzo 			nm_kr_put(kring);
187868b8534bSLuigi Rizzo 		}
187968b8534bSLuigi Rizzo 
188068b8534bSLuigi Rizzo 		break;
188168b8534bSLuigi Rizzo 
1882f196ce38SLuigi Rizzo #ifdef __FreeBSD__
188389e3fd52SLuigi Rizzo 	case FIONBIO:
188489e3fd52SLuigi Rizzo 	case FIOASYNC:
188589e3fd52SLuigi Rizzo 		ND("FIONBIO/FIOASYNC are no-ops");
188689e3fd52SLuigi Rizzo 		break;
188789e3fd52SLuigi Rizzo 
188868b8534bSLuigi Rizzo 	case BIOCIMMEDIATE:
188968b8534bSLuigi Rizzo 	case BIOCGHDRCMPLT:
189068b8534bSLuigi Rizzo 	case BIOCSHDRCMPLT:
189168b8534bSLuigi Rizzo 	case BIOCSSEESENT:
189268b8534bSLuigi Rizzo 		D("ignore BIOCIMMEDIATE/BIOCSHDRCMPLT/BIOCSHDRCMPLT/BIOCSSEESENT");
189368b8534bSLuigi Rizzo 		break;
189468b8534bSLuigi Rizzo 
1895babc7c12SLuigi Rizzo 	default:	/* allow device-specific ioctls */
189668b8534bSLuigi Rizzo 	    {
189768b8534bSLuigi Rizzo 		struct socket so;
1898ce3ee1e7SLuigi Rizzo 
189968b8534bSLuigi Rizzo 		bzero(&so, sizeof(so));
1900ce3ee1e7SLuigi Rizzo 		NMG_LOCK();
1901f9790aebSLuigi Rizzo 		error = netmap_get_na(nmr, &na, 0 /* don't create */); /* keep reference */
1902ce3ee1e7SLuigi Rizzo 		if (error) {
1903f9790aebSLuigi Rizzo 			netmap_adapter_put(na);
1904ce3ee1e7SLuigi Rizzo 			NMG_UNLOCK();
190568b8534bSLuigi Rizzo 			break;
1906ce3ee1e7SLuigi Rizzo 		}
1907f9790aebSLuigi Rizzo 		ifp = na->ifp;
190868b8534bSLuigi Rizzo 		so.so_vnet = ifp->if_vnet;
190968b8534bSLuigi Rizzo 		// so->so_proto not null.
191068b8534bSLuigi Rizzo 		error = ifioctl(&so, cmd, data, td);
1911f9790aebSLuigi Rizzo 		netmap_adapter_put(na);
1912ce3ee1e7SLuigi Rizzo 		NMG_UNLOCK();
1913babc7c12SLuigi Rizzo 		break;
191468b8534bSLuigi Rizzo 	    }
1915f196ce38SLuigi Rizzo 
1916f196ce38SLuigi Rizzo #else /* linux */
1917f196ce38SLuigi Rizzo 	default:
1918f196ce38SLuigi Rizzo 		error = EOPNOTSUPP;
1919f196ce38SLuigi Rizzo #endif /* linux */
192068b8534bSLuigi Rizzo 	}
1921ce3ee1e7SLuigi Rizzo out:
192268b8534bSLuigi Rizzo 
1923506cc70cSLuigi Rizzo 	CURVNET_RESTORE();
192468b8534bSLuigi Rizzo 	return (error);
192568b8534bSLuigi Rizzo }
192668b8534bSLuigi Rizzo 
192768b8534bSLuigi Rizzo 
192868b8534bSLuigi Rizzo /*
192968b8534bSLuigi Rizzo  * select(2) and poll(2) handlers for the "netmap" device.
193068b8534bSLuigi Rizzo  *
193168b8534bSLuigi Rizzo  * Can be called for one or more queues.
193268b8534bSLuigi Rizzo  * Return true the event mask corresponding to ready events.
193368b8534bSLuigi Rizzo  * If there are no ready events, do a selrecord on either individual
1934ce3ee1e7SLuigi Rizzo  * selinfo or on the global one.
193568b8534bSLuigi Rizzo  * Device-dependent parts (locking and sync of tx/rx rings)
193668b8534bSLuigi Rizzo  * are done through callbacks.
1937f196ce38SLuigi Rizzo  *
193801c7d25fSLuigi Rizzo  * On linux, arguments are really pwait, the poll table, and 'td' is struct file *
193901c7d25fSLuigi Rizzo  * The first one is remapped to pwait as selrecord() uses the name as an
194001c7d25fSLuigi Rizzo  * hidden argument.
194168b8534bSLuigi Rizzo  */
1942f9790aebSLuigi Rizzo int
194301c7d25fSLuigi Rizzo netmap_poll(struct cdev *dev, int events, struct thread *td)
194468b8534bSLuigi Rizzo {
194568b8534bSLuigi Rizzo 	struct netmap_priv_d *priv = NULL;
194668b8534bSLuigi Rizzo 	struct netmap_adapter *na;
194768b8534bSLuigi Rizzo 	struct ifnet *ifp;
194868b8534bSLuigi Rizzo 	struct netmap_kring *kring;
1949954dca4cSLuigi Rizzo 	u_int i, check_all_tx, check_all_rx, want_tx, want_rx, revents = 0;
195017885a7bSLuigi Rizzo 	struct mbq q;		/* packets from hw queues to host stack */
195101c7d25fSLuigi Rizzo 	void *pwait = dev;	/* linux compatibility */
1952f0ea3689SLuigi Rizzo 	int is_kevent = 0;
195301c7d25fSLuigi Rizzo 
1954f9790aebSLuigi Rizzo 	/*
1955f9790aebSLuigi Rizzo 	 * In order to avoid nested locks, we need to "double check"
1956f9790aebSLuigi Rizzo 	 * txsync and rxsync if we decide to do a selrecord().
1957f9790aebSLuigi Rizzo 	 * retry_tx (and retry_rx, later) prevent looping forever.
1958f9790aebSLuigi Rizzo 	 */
195917885a7bSLuigi Rizzo 	int retry_tx = 1, retry_rx = 1;
1960ce3ee1e7SLuigi Rizzo 
196101c7d25fSLuigi Rizzo 	(void)pwait;
1962f9790aebSLuigi Rizzo 	mbq_init(&q);
196368b8534bSLuigi Rizzo 
1964f0ea3689SLuigi Rizzo 	/*
1965f0ea3689SLuigi Rizzo 	 * XXX kevent has curthread->tp_fop == NULL,
1966f0ea3689SLuigi Rizzo 	 * so devfs_get_cdevpriv() fails. We circumvent this by passing
1967f0ea3689SLuigi Rizzo 	 * priv as the first argument, which is also useful to avoid
1968f0ea3689SLuigi Rizzo 	 * the selrecord() which are not necessary in that case.
1969f0ea3689SLuigi Rizzo 	 */
1970f0ea3689SLuigi Rizzo 	if (devfs_get_cdevpriv((void **)&priv) != 0) {
1971f0ea3689SLuigi Rizzo 		is_kevent = 1;
1972f0ea3689SLuigi Rizzo 		if (netmap_verbose)
1973f0ea3689SLuigi Rizzo 			D("called from kevent");
1974f0ea3689SLuigi Rizzo 		priv = (struct netmap_priv_d *)dev;
1975f0ea3689SLuigi Rizzo 	}
1976f0ea3689SLuigi Rizzo 	if (priv == NULL)
197768b8534bSLuigi Rizzo 		return POLLERR;
197868b8534bSLuigi Rizzo 
19798241616dSLuigi Rizzo 	if (priv->np_nifp == NULL) {
19808241616dSLuigi Rizzo 		D("No if registered");
19818241616dSLuigi Rizzo 		return POLLERR;
19828241616dSLuigi Rizzo 	}
19838241616dSLuigi Rizzo 	rmb(); /* make sure following reads are not from cache */
19848241616dSLuigi Rizzo 
1985f9790aebSLuigi Rizzo 	na = priv->np_na;
1986f9790aebSLuigi Rizzo 	ifp = na->ifp;
1987f9790aebSLuigi Rizzo 	// check for deleted
1988f9790aebSLuigi Rizzo 	if (ifp == NULL) {
1989f9790aebSLuigi Rizzo 		RD(1, "the ifp is gone");
1990f9790aebSLuigi Rizzo 		return POLLERR;
1991f9790aebSLuigi Rizzo 	}
1992f9790aebSLuigi Rizzo 
199368b8534bSLuigi Rizzo 	if ( (ifp->if_capenable & IFCAP_NETMAP) == 0)
199468b8534bSLuigi Rizzo 		return POLLERR;
199568b8534bSLuigi Rizzo 
199668b8534bSLuigi Rizzo 	if (netmap_verbose & 0x8000)
1997f9790aebSLuigi Rizzo 		D("device %s events 0x%x", NM_IFPNAME(ifp), events);
199868b8534bSLuigi Rizzo 	want_tx = events & (POLLOUT | POLLWRNORM);
199968b8534bSLuigi Rizzo 	want_rx = events & (POLLIN | POLLRDNORM);
200068b8534bSLuigi Rizzo 
2001091fd0abSLuigi Rizzo 
200268b8534bSLuigi Rizzo 	/*
2003f9790aebSLuigi Rizzo 	 * check_all_{tx|rx} are set if the card has more than one queue AND
2004f9790aebSLuigi Rizzo 	 * the file descriptor is bound to all of them. If so, we sleep on
2005ce3ee1e7SLuigi Rizzo 	 * the "global" selinfo, otherwise we sleep on individual selinfo
2006ce3ee1e7SLuigi Rizzo 	 * (FreeBSD only allows two selinfo's per file descriptor).
2007ce3ee1e7SLuigi Rizzo 	 * The interrupt routine in the driver wake one or the other
2008ce3ee1e7SLuigi Rizzo 	 * (or both) depending on which clients are active.
200968b8534bSLuigi Rizzo 	 *
201068b8534bSLuigi Rizzo 	 * rxsync() is only called if we run out of buffers on a POLLIN.
201168b8534bSLuigi Rizzo 	 * txsync() is called if we run out of buffers on POLLOUT, or
201268b8534bSLuigi Rizzo 	 * there are pending packets to send. The latter can be disabled
201368b8534bSLuigi Rizzo 	 * passing NETMAP_NO_TX_POLL in the NIOCREG call.
201468b8534bSLuigi Rizzo 	 */
2015f0ea3689SLuigi Rizzo 	check_all_tx = nm_tx_si_user(priv);
2016f0ea3689SLuigi Rizzo 	check_all_rx = nm_rx_si_user(priv);
201764ae02c3SLuigi Rizzo 
201868b8534bSLuigi Rizzo 	/*
2019f9790aebSLuigi Rizzo 	 * We start with a lock free round which is cheap if we have
2020f9790aebSLuigi Rizzo 	 * slots available. If this fails, then lock and call the sync
202168b8534bSLuigi Rizzo 	 * routines.
202268b8534bSLuigi Rizzo 	 */
2023f0ea3689SLuigi Rizzo 	for (i = priv->np_rxqfirst; want_rx && i < priv->np_rxqlast; i++) {
202468b8534bSLuigi Rizzo 		kring = &na->rx_rings[i];
202517885a7bSLuigi Rizzo 		/* XXX compare ring->cur and kring->tail */
202617885a7bSLuigi Rizzo 		if (!nm_ring_empty(kring->ring)) {
202768b8534bSLuigi Rizzo 			revents |= want_rx;
202868b8534bSLuigi Rizzo 			want_rx = 0;	/* also breaks the loop */
202968b8534bSLuigi Rizzo 		}
203068b8534bSLuigi Rizzo 	}
2031f0ea3689SLuigi Rizzo 	for (i = priv->np_txqfirst; want_tx && i < priv->np_txqlast; i++) {
203268b8534bSLuigi Rizzo 		kring = &na->tx_rings[i];
203317885a7bSLuigi Rizzo 		/* XXX compare ring->cur and kring->tail */
203417885a7bSLuigi Rizzo 		if (!nm_ring_empty(kring->ring)) {
203568b8534bSLuigi Rizzo 			revents |= want_tx;
203668b8534bSLuigi Rizzo 			want_tx = 0;	/* also breaks the loop */
203768b8534bSLuigi Rizzo 		}
203868b8534bSLuigi Rizzo 	}
203968b8534bSLuigi Rizzo 
204068b8534bSLuigi Rizzo 	/*
204117885a7bSLuigi Rizzo 	 * If we want to push packets out (priv->np_txpoll) or
204217885a7bSLuigi Rizzo 	 * want_tx is still set, we must issue txsync calls
204317885a7bSLuigi Rizzo 	 * (on all rings, to avoid that the tx rings stall).
2044f9790aebSLuigi Rizzo 	 * XXX should also check cur != hwcur on the tx rings.
2045f9790aebSLuigi Rizzo 	 * Fortunately, normal tx mode has np_txpoll set.
204668b8534bSLuigi Rizzo 	 */
204768b8534bSLuigi Rizzo 	if (priv->np_txpoll || want_tx) {
204817885a7bSLuigi Rizzo 		/*
204917885a7bSLuigi Rizzo 		 * The first round checks if anyone is ready, if not
205017885a7bSLuigi Rizzo 		 * do a selrecord and another round to handle races.
205117885a7bSLuigi Rizzo 		 * want_tx goes to 0 if any space is found, and is
205217885a7bSLuigi Rizzo 		 * used to skip rings with no pending transmissions.
2053ce3ee1e7SLuigi Rizzo 		 */
2054091fd0abSLuigi Rizzo flush_tx:
2055f0ea3689SLuigi Rizzo 		for (i = priv->np_txqfirst; i < priv->np_txqlast; i++) {
205617885a7bSLuigi Rizzo 			int found = 0;
205717885a7bSLuigi Rizzo 
205868b8534bSLuigi Rizzo 			kring = &na->tx_rings[i];
205968b8534bSLuigi Rizzo 			if (!want_tx && kring->ring->cur == kring->nr_hwcur)
206068b8534bSLuigi Rizzo 				continue;
206117885a7bSLuigi Rizzo 			/* only one thread does txsync */
2062ce3ee1e7SLuigi Rizzo 			if (nm_kr_tryget(kring)) {
206389cc2556SLuigi Rizzo 				/* either busy or stopped
206489cc2556SLuigi Rizzo 				 * XXX if the ring is stopped, sleeping would
206589cc2556SLuigi Rizzo 				 * be better. In current code, however, we only
206689cc2556SLuigi Rizzo 				 * stop the rings for brief intervals (2014-03-14)
206789cc2556SLuigi Rizzo 				 */
206889cc2556SLuigi Rizzo 
206989e3fd52SLuigi Rizzo 				if (netmap_verbose)
207089e3fd52SLuigi Rizzo 					RD(2, "%p lost race on txring %d, ok",
207189e3fd52SLuigi Rizzo 					    priv, i);
207217885a7bSLuigi Rizzo 				continue;
207368b8534bSLuigi Rizzo 			}
207417885a7bSLuigi Rizzo 			if (nm_txsync_prologue(kring) >= kring->nkr_num_slots) {
207517885a7bSLuigi Rizzo 				netmap_ring_reinit(kring);
207617885a7bSLuigi Rizzo 				revents |= POLLERR;
207717885a7bSLuigi Rizzo 			} else {
2078f0ea3689SLuigi Rizzo 				if (kring->nm_sync(kring, 0))
207968b8534bSLuigi Rizzo 					revents |= POLLERR;
208017885a7bSLuigi Rizzo 			}
208168b8534bSLuigi Rizzo 
208217885a7bSLuigi Rizzo 			/*
208317885a7bSLuigi Rizzo 			 * If we found new slots, notify potential
208417885a7bSLuigi Rizzo 			 * listeners on the same ring.
208517885a7bSLuigi Rizzo 			 * Since we just did a txsync, look at the copies
208617885a7bSLuigi Rizzo 			 * of cur,tail in the kring.
2087f9790aebSLuigi Rizzo 			 */
208817885a7bSLuigi Rizzo 			found = kring->rcur != kring->rtail;
208917885a7bSLuigi Rizzo 			nm_kr_put(kring);
209017885a7bSLuigi Rizzo 			if (found) { /* notify other listeners */
209168b8534bSLuigi Rizzo 				revents |= want_tx;
209268b8534bSLuigi Rizzo 				want_tx = 0;
2093f0ea3689SLuigi Rizzo 				na->nm_notify(na, i, NR_TX, 0);
209468b8534bSLuigi Rizzo 			}
2095ce3ee1e7SLuigi Rizzo 		}
2096f0ea3689SLuigi Rizzo 		if (want_tx && retry_tx && !is_kevent) {
2097954dca4cSLuigi Rizzo 			selrecord(td, check_all_tx ?
2098f0ea3689SLuigi Rizzo 			    &na->tx_si : &na->tx_rings[priv->np_txqfirst].si);
2099ce3ee1e7SLuigi Rizzo 			retry_tx = 0;
2100ce3ee1e7SLuigi Rizzo 			goto flush_tx;
210168b8534bSLuigi Rizzo 		}
210268b8534bSLuigi Rizzo 	}
210368b8534bSLuigi Rizzo 
210468b8534bSLuigi Rizzo 	/*
210517885a7bSLuigi Rizzo 	 * If want_rx is still set scan receive rings.
210668b8534bSLuigi Rizzo 	 * Do it on all rings because otherwise we starve.
210768b8534bSLuigi Rizzo 	 */
210868b8534bSLuigi Rizzo 	if (want_rx) {
210917885a7bSLuigi Rizzo 		int send_down = 0; /* transparent mode */
211089cc2556SLuigi Rizzo 		/* two rounds here for race avoidance */
2111ce3ee1e7SLuigi Rizzo do_retry_rx:
2112f0ea3689SLuigi Rizzo 		for (i = priv->np_rxqfirst; i < priv->np_rxqlast; i++) {
211317885a7bSLuigi Rizzo 			int found = 0;
211417885a7bSLuigi Rizzo 
211568b8534bSLuigi Rizzo 			kring = &na->rx_rings[i];
2116ce3ee1e7SLuigi Rizzo 
2117ce3ee1e7SLuigi Rizzo 			if (nm_kr_tryget(kring)) {
211889e3fd52SLuigi Rizzo 				if (netmap_verbose)
211989e3fd52SLuigi Rizzo 					RD(2, "%p lost race on rxring %d, ok",
212089e3fd52SLuigi Rizzo 					    priv, i);
212117885a7bSLuigi Rizzo 				continue;
212268b8534bSLuigi Rizzo 			}
2123ce3ee1e7SLuigi Rizzo 
212417885a7bSLuigi Rizzo 			/*
212517885a7bSLuigi Rizzo 			 * transparent mode support: collect packets
212617885a7bSLuigi Rizzo 			 * from the rxring(s).
212717885a7bSLuigi Rizzo 			 * XXX NR_FORWARD should only be read on
2128ce3ee1e7SLuigi Rizzo 			 * physical or NIC ports
2129ce3ee1e7SLuigi Rizzo 			 */
2130091fd0abSLuigi Rizzo 			if (netmap_fwd ||kring->ring->flags & NR_FORWARD) {
2131091fd0abSLuigi Rizzo 				ND(10, "forwarding some buffers up %d to %d",
2132091fd0abSLuigi Rizzo 				    kring->nr_hwcur, kring->ring->cur);
2133091fd0abSLuigi Rizzo 				netmap_grab_packets(kring, &q, netmap_fwd);
2134091fd0abSLuigi Rizzo 			}
213568b8534bSLuigi Rizzo 
2136f0ea3689SLuigi Rizzo 			if (kring->nm_sync(kring, 0))
213768b8534bSLuigi Rizzo 				revents |= POLLERR;
21385819da83SLuigi Rizzo 			if (netmap_no_timestamp == 0 ||
21395819da83SLuigi Rizzo 					kring->ring->flags & NR_TIMESTAMP) {
214068b8534bSLuigi Rizzo 				microtime(&kring->ring->ts);
21415819da83SLuigi Rizzo 			}
214217885a7bSLuigi Rizzo 			/* after an rxsync we can use kring->rcur, rtail */
214317885a7bSLuigi Rizzo 			found = kring->rcur != kring->rtail;
214417885a7bSLuigi Rizzo 			nm_kr_put(kring);
214517885a7bSLuigi Rizzo 			if (found) {
214668b8534bSLuigi Rizzo 				revents |= want_rx;
2147ce3ee1e7SLuigi Rizzo 				retry_rx = 0;
2148f0ea3689SLuigi Rizzo 				na->nm_notify(na, i, NR_RX, 0);
214968b8534bSLuigi Rizzo 			}
215068b8534bSLuigi Rizzo 		}
215117885a7bSLuigi Rizzo 
215217885a7bSLuigi Rizzo 		/* transparent mode XXX only during first pass ? */
2153f0ea3689SLuigi Rizzo 		if (na->na_flags & NAF_HOST_RINGS) {
2154f0ea3689SLuigi Rizzo 			kring = &na->rx_rings[na->num_rx_rings];
215517885a7bSLuigi Rizzo 			if (check_all_rx
215617885a7bSLuigi Rizzo 			    && (netmap_fwd || kring->ring->flags & NR_FORWARD)) {
215717885a7bSLuigi Rizzo 				/* XXX fix to use kring fields */
215817885a7bSLuigi Rizzo 				if (nm_ring_empty(kring->ring))
215917885a7bSLuigi Rizzo 					send_down = netmap_rxsync_from_host(na, td, dev);
216017885a7bSLuigi Rizzo 				if (!nm_ring_empty(kring->ring))
216117885a7bSLuigi Rizzo 					revents |= want_rx;
216217885a7bSLuigi Rizzo 			}
2163f0ea3689SLuigi Rizzo 		}
216417885a7bSLuigi Rizzo 
2165f0ea3689SLuigi Rizzo 		if (retry_rx && !is_kevent)
2166954dca4cSLuigi Rizzo 			selrecord(td, check_all_rx ?
2167f0ea3689SLuigi Rizzo 			    &na->rx_si : &na->rx_rings[priv->np_rxqfirst].si);
216817885a7bSLuigi Rizzo 		if (send_down > 0 || retry_rx) {
216917885a7bSLuigi Rizzo 			retry_rx = 0;
217017885a7bSLuigi Rizzo 			if (send_down)
217117885a7bSLuigi Rizzo 				goto flush_tx; /* and retry_rx */
217217885a7bSLuigi Rizzo 			else
2173ce3ee1e7SLuigi Rizzo 				goto do_retry_rx;
2174ce3ee1e7SLuigi Rizzo 		}
217568b8534bSLuigi Rizzo 	}
2176091fd0abSLuigi Rizzo 
217717885a7bSLuigi Rizzo 	/*
217817885a7bSLuigi Rizzo 	 * Transparent mode: marked bufs on rx rings between
217917885a7bSLuigi Rizzo 	 * kring->nr_hwcur and ring->head
218017885a7bSLuigi Rizzo 	 * are passed to the other endpoint.
218117885a7bSLuigi Rizzo 	 *
218217885a7bSLuigi Rizzo 	 * In this mode we also scan the sw rxring, which in
218317885a7bSLuigi Rizzo 	 * turn passes packets up.
218417885a7bSLuigi Rizzo 	 *
218517885a7bSLuigi Rizzo 	 * XXX Transparent mode at the moment requires to bind all
218617885a7bSLuigi Rizzo  	 * rings to a single file descriptor.
2187ce3ee1e7SLuigi Rizzo 	 */
2188091fd0abSLuigi Rizzo 
2189091fd0abSLuigi Rizzo 	if (q.head)
2190f9790aebSLuigi Rizzo 		netmap_send_up(na->ifp, &q);
219168b8534bSLuigi Rizzo 
219268b8534bSLuigi Rizzo 	return (revents);
219368b8534bSLuigi Rizzo }
219468b8534bSLuigi Rizzo 
219517885a7bSLuigi Rizzo 
219617885a7bSLuigi Rizzo /*-------------------- driver support routines -------------------*/
219768b8534bSLuigi Rizzo 
2198f9790aebSLuigi Rizzo static int netmap_hw_krings_create(struct netmap_adapter *);
2199f9790aebSLuigi Rizzo 
220089cc2556SLuigi Rizzo /* default notify callback */
2201f9790aebSLuigi Rizzo static int
220217885a7bSLuigi Rizzo netmap_notify(struct netmap_adapter *na, u_int n_ring,
220317885a7bSLuigi Rizzo 	enum txrx tx, int flags)
2204f9790aebSLuigi Rizzo {
2205f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
2206f9790aebSLuigi Rizzo 
2207f9790aebSLuigi Rizzo 	if (tx == NR_TX) {
2208f9790aebSLuigi Rizzo 		kring = na->tx_rings + n_ring;
2209f0ea3689SLuigi Rizzo 		OS_selwakeup(&kring->si, PI_NET);
221089cc2556SLuigi Rizzo 		/* optimization: avoid a wake up on the global
221189cc2556SLuigi Rizzo 		 * queue if nobody has registered for more
221289cc2556SLuigi Rizzo 		 * than one ring
221389cc2556SLuigi Rizzo 		 */
2214f0ea3689SLuigi Rizzo 		if (na->tx_si_users > 0)
2215f0ea3689SLuigi Rizzo 			OS_selwakeup(&na->tx_si, PI_NET);
2216f9790aebSLuigi Rizzo 	} else {
2217f9790aebSLuigi Rizzo 		kring = na->rx_rings + n_ring;
2218f0ea3689SLuigi Rizzo 		OS_selwakeup(&kring->si, PI_NET);
221989cc2556SLuigi Rizzo 		/* optimization: same as above */
2220f0ea3689SLuigi Rizzo 		if (na->rx_si_users > 0)
2221f0ea3689SLuigi Rizzo 			OS_selwakeup(&na->rx_si, PI_NET);
2222f9790aebSLuigi Rizzo 	}
2223f9790aebSLuigi Rizzo 	return 0;
2224f9790aebSLuigi Rizzo }
2225f9790aebSLuigi Rizzo 
2226f9790aebSLuigi Rizzo 
222789cc2556SLuigi Rizzo /* called by all routines that create netmap_adapters.
222889cc2556SLuigi Rizzo  * Attach na to the ifp (if any) and provide defaults
222989cc2556SLuigi Rizzo  * for optional callbacks. Defaults assume that we
223089cc2556SLuigi Rizzo  * are creating an hardware netmap_adapter.
223189cc2556SLuigi Rizzo  */
2232f9790aebSLuigi Rizzo int
2233f9790aebSLuigi Rizzo netmap_attach_common(struct netmap_adapter *na)
2234f9790aebSLuigi Rizzo {
2235f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
2236f9790aebSLuigi Rizzo 
2237f9790aebSLuigi Rizzo 	if (na->num_tx_rings == 0 || na->num_rx_rings == 0) {
2238f9790aebSLuigi Rizzo 		D("%s: invalid rings tx %d rx %d",
2239f9790aebSLuigi Rizzo 			ifp->if_xname, na->num_tx_rings, na->num_rx_rings);
2240f9790aebSLuigi Rizzo 		return EINVAL;
2241f9790aebSLuigi Rizzo 	}
2242f9790aebSLuigi Rizzo 	WNA(ifp) = na;
224317885a7bSLuigi Rizzo 
224417885a7bSLuigi Rizzo 	/* the following is only needed for na that use the host port.
224517885a7bSLuigi Rizzo 	 * XXX do we have something similar for linux ?
224617885a7bSLuigi Rizzo 	 */
224717885a7bSLuigi Rizzo #ifdef __FreeBSD__
224817885a7bSLuigi Rizzo 	na->if_input = ifp->if_input; /* for netmap_send_up */
224917885a7bSLuigi Rizzo #endif /* __FreeBSD__ */
225017885a7bSLuigi Rizzo 
2251f9790aebSLuigi Rizzo 	NETMAP_SET_CAPABLE(ifp);
2252f9790aebSLuigi Rizzo 	if (na->nm_krings_create == NULL) {
225389cc2556SLuigi Rizzo 		/* we assume that we have been called by a driver,
225489cc2556SLuigi Rizzo 		 * since other port types all provide their own
225589cc2556SLuigi Rizzo 		 * nm_krings_create
225689cc2556SLuigi Rizzo 		 */
2257f9790aebSLuigi Rizzo 		na->nm_krings_create = netmap_hw_krings_create;
225817885a7bSLuigi Rizzo 		na->nm_krings_delete = netmap_hw_krings_delete;
2259f9790aebSLuigi Rizzo 	}
2260f9790aebSLuigi Rizzo 	if (na->nm_notify == NULL)
2261f9790aebSLuigi Rizzo 		na->nm_notify = netmap_notify;
2262f9790aebSLuigi Rizzo 	na->active_fds = 0;
2263f9790aebSLuigi Rizzo 
2264f9790aebSLuigi Rizzo 	if (na->nm_mem == NULL)
2265f9790aebSLuigi Rizzo 		na->nm_mem = &nm_mem;
2266f9790aebSLuigi Rizzo 	return 0;
2267f9790aebSLuigi Rizzo }
2268f9790aebSLuigi Rizzo 
2269f9790aebSLuigi Rizzo 
227089cc2556SLuigi Rizzo /* standard cleanup, called by all destructors */
2271f9790aebSLuigi Rizzo void
2272f9790aebSLuigi Rizzo netmap_detach_common(struct netmap_adapter *na)
2273f9790aebSLuigi Rizzo {
227489cc2556SLuigi Rizzo 	if (na->ifp != NULL)
2275f9790aebSLuigi Rizzo 		WNA(na->ifp) = NULL; /* XXX do we need this? */
2276f9790aebSLuigi Rizzo 
2277f9790aebSLuigi Rizzo 	if (na->tx_rings) { /* XXX should not happen */
2278f9790aebSLuigi Rizzo 		D("freeing leftover tx_rings");
2279f9790aebSLuigi Rizzo 		na->nm_krings_delete(na);
2280f9790aebSLuigi Rizzo 	}
2281f0ea3689SLuigi Rizzo 	netmap_pipe_dealloc(na);
2282f9790aebSLuigi Rizzo 	if (na->na_flags & NAF_MEM_OWNER)
2283f9790aebSLuigi Rizzo 		netmap_mem_private_delete(na->nm_mem);
2284f9790aebSLuigi Rizzo 	bzero(na, sizeof(*na));
2285f9790aebSLuigi Rizzo 	free(na, M_DEVBUF);
2286f9790aebSLuigi Rizzo }
2287f9790aebSLuigi Rizzo 
2288f18be576SLuigi Rizzo 
228968b8534bSLuigi Rizzo /*
229068b8534bSLuigi Rizzo  * Initialize a ``netmap_adapter`` object created by driver on attach.
229168b8534bSLuigi Rizzo  * We allocate a block of memory with room for a struct netmap_adapter
229268b8534bSLuigi Rizzo  * plus two sets of N+2 struct netmap_kring (where N is the number
229368b8534bSLuigi Rizzo  * of hardware rings):
229468b8534bSLuigi Rizzo  * krings	0..N-1	are for the hardware queues.
229568b8534bSLuigi Rizzo  * kring	N	is for the host stack queue
229617885a7bSLuigi Rizzo  * kring	N+1	is only used for the selinfo for all queues. // XXX still true ?
229768b8534bSLuigi Rizzo  * Return 0 on success, ENOMEM otherwise.
229868b8534bSLuigi Rizzo  */
229968b8534bSLuigi Rizzo int
2300f9790aebSLuigi Rizzo netmap_attach(struct netmap_adapter *arg)
230168b8534bSLuigi Rizzo {
2302f9790aebSLuigi Rizzo 	struct netmap_hw_adapter *hwna = NULL;
2303f9790aebSLuigi Rizzo 	// XXX when is arg == NULL ?
2304ae10d1afSLuigi Rizzo 	struct ifnet *ifp = arg ? arg->ifp : NULL;
230568b8534bSLuigi Rizzo 
2306ae10d1afSLuigi Rizzo 	if (arg == NULL || ifp == NULL)
2307ae10d1afSLuigi Rizzo 		goto fail;
2308f9790aebSLuigi Rizzo 	hwna = malloc(sizeof(*hwna), M_DEVBUF, M_NOWAIT | M_ZERO);
2309f9790aebSLuigi Rizzo 	if (hwna == NULL)
2310ae10d1afSLuigi Rizzo 		goto fail;
2311f9790aebSLuigi Rizzo 	hwna->up = *arg;
2312f0ea3689SLuigi Rizzo 	hwna->up.na_flags |= NAF_HOST_RINGS;
2313f9790aebSLuigi Rizzo 	if (netmap_attach_common(&hwna->up)) {
2314f9790aebSLuigi Rizzo 		free(hwna, M_DEVBUF);
2315f9790aebSLuigi Rizzo 		goto fail;
2316f9790aebSLuigi Rizzo 	}
2317f9790aebSLuigi Rizzo 	netmap_adapter_get(&hwna->up);
2318f9790aebSLuigi Rizzo 
231964ae02c3SLuigi Rizzo #ifdef linux
2320f18be576SLuigi Rizzo 	if (ifp->netdev_ops) {
2321f18be576SLuigi Rizzo 		/* prepare a clone of the netdev ops */
2322f18be576SLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28)
2323f9790aebSLuigi Rizzo 		hwna->nm_ndo.ndo_start_xmit = ifp->netdev_ops;
2324f18be576SLuigi Rizzo #else
2325f9790aebSLuigi Rizzo 		hwna->nm_ndo = *ifp->netdev_ops;
2326f18be576SLuigi Rizzo #endif
2327f18be576SLuigi Rizzo 	}
2328f9790aebSLuigi Rizzo 	hwna->nm_ndo.ndo_start_xmit = linux_netmap_start_xmit;
2329ce3ee1e7SLuigi Rizzo #endif /* linux */
2330f9790aebSLuigi Rizzo 
233189cc2556SLuigi Rizzo 	D("success for %s tx %d/%d rx %d/%d queues/slots",
233289cc2556SLuigi Rizzo 		NM_IFPNAME(ifp),
233389cc2556SLuigi Rizzo 		hwna->up.num_tx_rings, hwna->up.num_tx_desc,
233489cc2556SLuigi Rizzo 		hwna->up.num_rx_rings, hwna->up.num_rx_desc
233589cc2556SLuigi Rizzo 		);
2336ae10d1afSLuigi Rizzo 	return 0;
233768b8534bSLuigi Rizzo 
2338ae10d1afSLuigi Rizzo fail:
2339f9790aebSLuigi Rizzo 	D("fail, arg %p ifp %p na %p", arg, ifp, hwna);
2340441ab64fSLuigi Rizzo 	if (ifp)
2341849bec0eSLuigi Rizzo 		netmap_detach(ifp);
2342f9790aebSLuigi Rizzo 	return (hwna ? EINVAL : ENOMEM);
234368b8534bSLuigi Rizzo }
234468b8534bSLuigi Rizzo 
234568b8534bSLuigi Rizzo 
2346f9790aebSLuigi Rizzo void
2347f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_get)(struct netmap_adapter *na)
2348f9790aebSLuigi Rizzo {
2349f9790aebSLuigi Rizzo 	if (!na) {
2350f9790aebSLuigi Rizzo 		return;
2351f9790aebSLuigi Rizzo 	}
2352f9790aebSLuigi Rizzo 
2353f9790aebSLuigi Rizzo 	refcount_acquire(&na->na_refcount);
2354f9790aebSLuigi Rizzo }
2355f9790aebSLuigi Rizzo 
2356f9790aebSLuigi Rizzo 
2357f9790aebSLuigi Rizzo /* returns 1 iff the netmap_adapter is destroyed */
2358f9790aebSLuigi Rizzo int
2359f9790aebSLuigi Rizzo NM_DBG(netmap_adapter_put)(struct netmap_adapter *na)
2360f9790aebSLuigi Rizzo {
2361f9790aebSLuigi Rizzo 	if (!na)
2362f9790aebSLuigi Rizzo 		return 1;
2363f9790aebSLuigi Rizzo 
2364f9790aebSLuigi Rizzo 	if (!refcount_release(&na->na_refcount))
2365f9790aebSLuigi Rizzo 		return 0;
2366f9790aebSLuigi Rizzo 
2367f9790aebSLuigi Rizzo 	if (na->nm_dtor)
2368f9790aebSLuigi Rizzo 		na->nm_dtor(na);
2369f9790aebSLuigi Rizzo 
2370f9790aebSLuigi Rizzo 	netmap_detach_common(na);
2371f9790aebSLuigi Rizzo 
2372f9790aebSLuigi Rizzo 	return 1;
2373f9790aebSLuigi Rizzo }
2374f9790aebSLuigi Rizzo 
237589cc2556SLuigi Rizzo /* nm_krings_create callback for all hardware native adapters */
2376f9790aebSLuigi Rizzo int
2377f9790aebSLuigi Rizzo netmap_hw_krings_create(struct netmap_adapter *na)
2378f9790aebSLuigi Rizzo {
2379f0ea3689SLuigi Rizzo 	int ret = netmap_krings_create(na, 0);
238017885a7bSLuigi Rizzo 	if (ret == 0) {
238117885a7bSLuigi Rizzo 		/* initialize the mbq for the sw rx ring */
238217885a7bSLuigi Rizzo 		mbq_safe_init(&na->rx_rings[na->num_rx_rings].rx_queue);
238317885a7bSLuigi Rizzo 		ND("initialized sw rx queue %d", na->num_rx_rings);
238417885a7bSLuigi Rizzo 	}
238517885a7bSLuigi Rizzo 	return ret;
2386f9790aebSLuigi Rizzo }
2387f9790aebSLuigi Rizzo 
2388f9790aebSLuigi Rizzo 
2389f9790aebSLuigi Rizzo 
239068b8534bSLuigi Rizzo /*
239189cc2556SLuigi Rizzo  * Called on module unload by the netmap-enabled drivers
239268b8534bSLuigi Rizzo  */
239368b8534bSLuigi Rizzo void
239468b8534bSLuigi Rizzo netmap_detach(struct ifnet *ifp)
239568b8534bSLuigi Rizzo {
239668b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
239768b8534bSLuigi Rizzo 
239868b8534bSLuigi Rizzo 	if (!na)
239968b8534bSLuigi Rizzo 		return;
240068b8534bSLuigi Rizzo 
2401f9790aebSLuigi Rizzo 	NMG_LOCK();
2402f9790aebSLuigi Rizzo 	netmap_disable_all_rings(ifp);
2403fb25194fSLuigi Rizzo 	if (!netmap_adapter_put(na)) {
2404fb25194fSLuigi Rizzo 		/* someone is still using the adapter,
2405fb25194fSLuigi Rizzo 		 * tell them that the interface is gone
2406fb25194fSLuigi Rizzo 		 */
2407f9790aebSLuigi Rizzo 		na->ifp = NULL;
2408fb25194fSLuigi Rizzo 		/* give them a chance to notice */
2409f9790aebSLuigi Rizzo 		netmap_enable_all_rings(ifp);
2410fb25194fSLuigi Rizzo 	}
2411f9790aebSLuigi Rizzo 	NMG_UNLOCK();
2412ae10d1afSLuigi Rizzo }
2413f18be576SLuigi Rizzo 
2414f18be576SLuigi Rizzo 
241568b8534bSLuigi Rizzo /*
241602ad4083SLuigi Rizzo  * Intercept packets from the network stack and pass them
241702ad4083SLuigi Rizzo  * to netmap as incoming packets on the 'software' ring.
241817885a7bSLuigi Rizzo  *
241917885a7bSLuigi Rizzo  * We only store packets in a bounded mbq and then copy them
242017885a7bSLuigi Rizzo  * in the relevant rxsync routine.
242117885a7bSLuigi Rizzo  *
2422ce3ee1e7SLuigi Rizzo  * We rely on the OS to make sure that the ifp and na do not go
2423ce3ee1e7SLuigi Rizzo  * away (typically the caller checks for IFF_DRV_RUNNING or the like).
2424ce3ee1e7SLuigi Rizzo  * In nm_register() or whenever there is a reinitialization,
2425f9790aebSLuigi Rizzo  * we make sure to make the mode change visible here.
242668b8534bSLuigi Rizzo  */
242768b8534bSLuigi Rizzo int
2428ce3ee1e7SLuigi Rizzo netmap_transmit(struct ifnet *ifp, struct mbuf *m)
242968b8534bSLuigi Rizzo {
243068b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
2431ce3ee1e7SLuigi Rizzo 	struct netmap_kring *kring;
243217885a7bSLuigi Rizzo 	u_int len = MBUF_LEN(m);
243317885a7bSLuigi Rizzo 	u_int error = ENOBUFS;
243417885a7bSLuigi Rizzo 	struct mbq *q;
243517885a7bSLuigi Rizzo 	int space;
243668b8534bSLuigi Rizzo 
2437ce3ee1e7SLuigi Rizzo 	// XXX [Linux] we do not need this lock
2438ce3ee1e7SLuigi Rizzo 	// if we follow the down/configure/up protocol -gl
2439ce3ee1e7SLuigi Rizzo 	// mtx_lock(&na->core_lock);
244017885a7bSLuigi Rizzo 
2441ce3ee1e7SLuigi Rizzo 	if ( (ifp->if_capenable & IFCAP_NETMAP) == 0) {
244217885a7bSLuigi Rizzo 		D("%s not in netmap mode anymore", NM_IFPNAME(ifp));
2443ce3ee1e7SLuigi Rizzo 		error = ENXIO;
2444ce3ee1e7SLuigi Rizzo 		goto done;
2445ce3ee1e7SLuigi Rizzo 	}
2446ce3ee1e7SLuigi Rizzo 
2447ce3ee1e7SLuigi Rizzo 	kring = &na->rx_rings[na->num_rx_rings];
244817885a7bSLuigi Rizzo 	q = &kring->rx_queue;
244917885a7bSLuigi Rizzo 
2450ce3ee1e7SLuigi Rizzo 	// XXX reconsider long packets if we handle fragments
2451ce3ee1e7SLuigi Rizzo 	if (len > NETMAP_BDG_BUF_SIZE(na->nm_mem)) { /* too long for us */
2452f9790aebSLuigi Rizzo 		D("%s from_host, drop packet size %d > %d", NM_IFPNAME(ifp),
2453ce3ee1e7SLuigi Rizzo 			len, NETMAP_BDG_BUF_SIZE(na->nm_mem));
2454ce3ee1e7SLuigi Rizzo 		goto done;
2455849bec0eSLuigi Rizzo 	}
245617885a7bSLuigi Rizzo 
245717885a7bSLuigi Rizzo 	/* protect against rxsync_from_host(), netmap_sw_to_nic()
245817885a7bSLuigi Rizzo 	 * and maybe other instances of netmap_transmit (the latter
245917885a7bSLuigi Rizzo 	 * not possible on Linux).
246017885a7bSLuigi Rizzo 	 * Also avoid overflowing the queue.
2461ce3ee1e7SLuigi Rizzo 	 */
2462*997b054cSLuigi Rizzo 	mbq_lock(q);
246317885a7bSLuigi Rizzo 
246417885a7bSLuigi Rizzo         space = kring->nr_hwtail - kring->nr_hwcur;
246517885a7bSLuigi Rizzo         if (space < 0)
246617885a7bSLuigi Rizzo                 space += kring->nkr_num_slots;
246717885a7bSLuigi Rizzo 	if (space + mbq_len(q) >= kring->nkr_num_slots - 1) { // XXX
246817885a7bSLuigi Rizzo 		RD(10, "%s full hwcur %d hwtail %d qlen %d len %d m %p",
246917885a7bSLuigi Rizzo 			 NM_IFPNAME(ifp), kring->nr_hwcur, kring->nr_hwtail, mbq_len(q),
247017885a7bSLuigi Rizzo 			len, m);
2471ce3ee1e7SLuigi Rizzo 	} else {
247217885a7bSLuigi Rizzo 		mbq_enqueue(q, m);
247317885a7bSLuigi Rizzo 		ND(10, "%s %d bufs in queue len %d m %p",
247417885a7bSLuigi Rizzo 			NM_IFPNAME(ifp), mbq_len(q), len, m);
247517885a7bSLuigi Rizzo 		/* notify outside the lock */
247617885a7bSLuigi Rizzo 		m = NULL;
247768b8534bSLuigi Rizzo 		error = 0;
2478ce3ee1e7SLuigi Rizzo 	}
2479*997b054cSLuigi Rizzo 	mbq_unlock(q);
2480ce3ee1e7SLuigi Rizzo 
248168b8534bSLuigi Rizzo done:
248217885a7bSLuigi Rizzo 	if (m)
248368b8534bSLuigi Rizzo 		m_freem(m);
248417885a7bSLuigi Rizzo 	/* unconditionally wake up listeners */
248517885a7bSLuigi Rizzo 	na->nm_notify(na, na->num_rx_rings, NR_RX, 0);
248689cc2556SLuigi Rizzo 	/* this is normally netmap_notify(), but for nics
248789cc2556SLuigi Rizzo 	 * connected to a bridge it is netmap_bwrap_intr_notify(),
248889cc2556SLuigi Rizzo 	 * that possibly forwards the frames through the switch
248989cc2556SLuigi Rizzo 	 */
249068b8534bSLuigi Rizzo 
249168b8534bSLuigi Rizzo 	return (error);
249268b8534bSLuigi Rizzo }
249368b8534bSLuigi Rizzo 
249468b8534bSLuigi Rizzo 
249568b8534bSLuigi Rizzo /*
249668b8534bSLuigi Rizzo  * netmap_reset() is called by the driver routines when reinitializing
249768b8534bSLuigi Rizzo  * a ring. The driver is in charge of locking to protect the kring.
2498f9790aebSLuigi Rizzo  * If native netmap mode is not set just return NULL.
249968b8534bSLuigi Rizzo  */
250068b8534bSLuigi Rizzo struct netmap_slot *
2501ce3ee1e7SLuigi Rizzo netmap_reset(struct netmap_adapter *na, enum txrx tx, u_int n,
250268b8534bSLuigi Rizzo 	u_int new_cur)
250368b8534bSLuigi Rizzo {
250468b8534bSLuigi Rizzo 	struct netmap_kring *kring;
2505506cc70cSLuigi Rizzo 	int new_hwofs, lim;
250668b8534bSLuigi Rizzo 
2507ce3ee1e7SLuigi Rizzo 	if (na == NULL) {
2508ce3ee1e7SLuigi Rizzo 		D("NULL na, should not happen");
250968b8534bSLuigi Rizzo 		return NULL;	/* no netmap support here */
2510ce3ee1e7SLuigi Rizzo 	}
2511ce3ee1e7SLuigi Rizzo 	if (!(na->ifp->if_capenable & IFCAP_NETMAP)) {
25125864b3a5SLuigi Rizzo 		ND("interface not in netmap mode");
251368b8534bSLuigi Rizzo 		return NULL;	/* nothing to reinitialize */
2514ce3ee1e7SLuigi Rizzo 	}
251568b8534bSLuigi Rizzo 
2516ce3ee1e7SLuigi Rizzo 	/* XXX note- in the new scheme, we are not guaranteed to be
2517ce3ee1e7SLuigi Rizzo 	 * under lock (e.g. when called on a device reset).
2518ce3ee1e7SLuigi Rizzo 	 * In this case, we should set a flag and do not trust too
2519ce3ee1e7SLuigi Rizzo 	 * much the values. In practice: TODO
2520ce3ee1e7SLuigi Rizzo 	 * - set a RESET flag somewhere in the kring
2521ce3ee1e7SLuigi Rizzo 	 * - do the processing in a conservative way
2522ce3ee1e7SLuigi Rizzo 	 * - let the *sync() fixup at the end.
2523ce3ee1e7SLuigi Rizzo 	 */
252464ae02c3SLuigi Rizzo 	if (tx == NR_TX) {
25258241616dSLuigi Rizzo 		if (n >= na->num_tx_rings)
25268241616dSLuigi Rizzo 			return NULL;
252764ae02c3SLuigi Rizzo 		kring = na->tx_rings + n;
252817885a7bSLuigi Rizzo 		// XXX check whether we should use hwcur or rcur
2529506cc70cSLuigi Rizzo 		new_hwofs = kring->nr_hwcur - new_cur;
253064ae02c3SLuigi Rizzo 	} else {
25318241616dSLuigi Rizzo 		if (n >= na->num_rx_rings)
25328241616dSLuigi Rizzo 			return NULL;
253364ae02c3SLuigi Rizzo 		kring = na->rx_rings + n;
253417885a7bSLuigi Rizzo 		new_hwofs = kring->nr_hwtail - new_cur;
253564ae02c3SLuigi Rizzo 	}
253664ae02c3SLuigi Rizzo 	lim = kring->nkr_num_slots - 1;
2537506cc70cSLuigi Rizzo 	if (new_hwofs > lim)
2538506cc70cSLuigi Rizzo 		new_hwofs -= lim + 1;
2539506cc70cSLuigi Rizzo 
2540ce3ee1e7SLuigi Rizzo 	/* Always set the new offset value and realign the ring. */
254117885a7bSLuigi Rizzo 	if (netmap_verbose)
254217885a7bSLuigi Rizzo 	    D("%s %s%d hwofs %d -> %d, hwtail %d -> %d",
254317885a7bSLuigi Rizzo 		NM_IFPNAME(na->ifp),
254417885a7bSLuigi Rizzo 		tx == NR_TX ? "TX" : "RX", n,
2545ce3ee1e7SLuigi Rizzo 		kring->nkr_hwofs, new_hwofs,
254617885a7bSLuigi Rizzo 		kring->nr_hwtail,
254717885a7bSLuigi Rizzo 		tx == NR_TX ? lim : kring->nr_hwtail);
2548506cc70cSLuigi Rizzo 	kring->nkr_hwofs = new_hwofs;
254917885a7bSLuigi Rizzo 	if (tx == NR_TX) {
255017885a7bSLuigi Rizzo 		kring->nr_hwtail = kring->nr_hwcur + lim;
255117885a7bSLuigi Rizzo 		if (kring->nr_hwtail > lim)
255217885a7bSLuigi Rizzo 			kring->nr_hwtail -= lim + 1;
255317885a7bSLuigi Rizzo 	}
2554506cc70cSLuigi Rizzo 
2555f196ce38SLuigi Rizzo #if 0 // def linux
2556f196ce38SLuigi Rizzo 	/* XXX check that the mappings are correct */
2557f196ce38SLuigi Rizzo 	/* need ring_nr, adapter->pdev, direction */
2558f196ce38SLuigi Rizzo 	buffer_info->dma = dma_map_single(&pdev->dev, addr, adapter->rx_buffer_len, DMA_FROM_DEVICE);
2559f196ce38SLuigi Rizzo 	if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) {
2560f196ce38SLuigi Rizzo 		D("error mapping rx netmap buffer %d", i);
2561f196ce38SLuigi Rizzo 		// XXX fix error handling
2562f196ce38SLuigi Rizzo 	}
2563f196ce38SLuigi Rizzo 
2564f196ce38SLuigi Rizzo #endif /* linux */
256568b8534bSLuigi Rizzo 	/*
2566ce3ee1e7SLuigi Rizzo 	 * Wakeup on the individual and global selwait
2567506cc70cSLuigi Rizzo 	 * We do the wakeup here, but the ring is not yet reconfigured.
2568506cc70cSLuigi Rizzo 	 * However, we are under lock so there are no races.
256968b8534bSLuigi Rizzo 	 */
2570f0ea3689SLuigi Rizzo 	na->nm_notify(na, n, tx, 0);
257168b8534bSLuigi Rizzo 	return kring->ring->slot;
257268b8534bSLuigi Rizzo }
257368b8534bSLuigi Rizzo 
257468b8534bSLuigi Rizzo 
2575ce3ee1e7SLuigi Rizzo /*
2576f9790aebSLuigi Rizzo  * Dispatch rx/tx interrupts to the netmap rings.
2577ce3ee1e7SLuigi Rizzo  *
2578ce3ee1e7SLuigi Rizzo  * "work_done" is non-null on the RX path, NULL for the TX path.
2579ce3ee1e7SLuigi Rizzo  * We rely on the OS to make sure that there is only one active
2580ce3ee1e7SLuigi Rizzo  * instance per queue, and that there is appropriate locking.
2581849bec0eSLuigi Rizzo  *
2582f9790aebSLuigi Rizzo  * The 'notify' routine depends on what the ring is attached to.
2583f9790aebSLuigi Rizzo  * - for a netmap file descriptor, do a selwakeup on the individual
2584f9790aebSLuigi Rizzo  *   waitqueue, plus one on the global one if needed
2585f9790aebSLuigi Rizzo  * - for a switch, call the proper forwarding routine
2586f9790aebSLuigi Rizzo  * - XXX more ?
2587f9790aebSLuigi Rizzo  */
2588f9790aebSLuigi Rizzo void
2589f9790aebSLuigi Rizzo netmap_common_irq(struct ifnet *ifp, u_int q, u_int *work_done)
2590f9790aebSLuigi Rizzo {
2591f9790aebSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
2592f9790aebSLuigi Rizzo 	struct netmap_kring *kring;
2593f9790aebSLuigi Rizzo 
2594f9790aebSLuigi Rizzo 	q &= NETMAP_RING_MASK;
2595f9790aebSLuigi Rizzo 
2596f9790aebSLuigi Rizzo 	if (netmap_verbose) {
2597f9790aebSLuigi Rizzo 	        RD(5, "received %s queue %d", work_done ? "RX" : "TX" , q);
2598f9790aebSLuigi Rizzo 	}
2599f9790aebSLuigi Rizzo 
2600f9790aebSLuigi Rizzo 	if (work_done) { /* RX path */
2601f9790aebSLuigi Rizzo 		if (q >= na->num_rx_rings)
2602f9790aebSLuigi Rizzo 			return;	// not a physical queue
2603f9790aebSLuigi Rizzo 		kring = na->rx_rings + q;
2604f9790aebSLuigi Rizzo 		kring->nr_kflags |= NKR_PENDINTR;	// XXX atomic ?
2605f0ea3689SLuigi Rizzo 		na->nm_notify(na, q, NR_RX, 0);
2606f9790aebSLuigi Rizzo 		*work_done = 1; /* do not fire napi again */
2607f9790aebSLuigi Rizzo 	} else { /* TX path */
2608f9790aebSLuigi Rizzo 		if (q >= na->num_tx_rings)
2609f9790aebSLuigi Rizzo 			return;	// not a physical queue
2610f9790aebSLuigi Rizzo 		kring = na->tx_rings + q;
2611f0ea3689SLuigi Rizzo 		na->nm_notify(na, q, NR_TX, 0);
2612f9790aebSLuigi Rizzo 	}
2613f9790aebSLuigi Rizzo }
2614f9790aebSLuigi Rizzo 
261517885a7bSLuigi Rizzo 
2616f9790aebSLuigi Rizzo /*
2617f9790aebSLuigi Rizzo  * Default functions to handle rx/tx interrupts from a physical device.
2618f9790aebSLuigi Rizzo  * "work_done" is non-null on the RX path, NULL for the TX path.
2619f9790aebSLuigi Rizzo  *
2620ce3ee1e7SLuigi Rizzo  * If the card is not in netmap mode, simply return 0,
2621ce3ee1e7SLuigi Rizzo  * so that the caller proceeds with regular processing.
2622f9790aebSLuigi Rizzo  * Otherwise call netmap_common_irq() and return 1.
2623ce3ee1e7SLuigi Rizzo  *
2624ce3ee1e7SLuigi Rizzo  * If the card is connected to a netmap file descriptor,
2625ce3ee1e7SLuigi Rizzo  * do a selwakeup on the individual queue, plus one on the global one
2626ce3ee1e7SLuigi Rizzo  * if needed (multiqueue card _and_ there are multiqueue listeners),
2627ce3ee1e7SLuigi Rizzo  * and return 1.
2628ce3ee1e7SLuigi Rizzo  *
2629ce3ee1e7SLuigi Rizzo  * Finally, if called on rx from an interface connected to a switch,
2630ce3ee1e7SLuigi Rizzo  * calls the proper forwarding routine, and return 1.
26311a26580eSLuigi Rizzo  */
2632babc7c12SLuigi Rizzo int
2633ce3ee1e7SLuigi Rizzo netmap_rx_irq(struct ifnet *ifp, u_int q, u_int *work_done)
26341a26580eSLuigi Rizzo {
2635f9790aebSLuigi Rizzo 	// XXX could we check NAF_NATIVE_ON ?
26361a26580eSLuigi Rizzo 	if (!(ifp->if_capenable & IFCAP_NETMAP))
26371a26580eSLuigi Rizzo 		return 0;
2638849bec0eSLuigi Rizzo 
2639f9790aebSLuigi Rizzo 	if (NA(ifp)->na_flags & NAF_SKIP_INTR) {
26408241616dSLuigi Rizzo 		ND("use regular interrupt");
26418241616dSLuigi Rizzo 		return 0;
26428241616dSLuigi Rizzo 	}
26438241616dSLuigi Rizzo 
2644f9790aebSLuigi Rizzo 	netmap_common_irq(ifp, q, work_done);
26451a26580eSLuigi Rizzo 	return 1;
26461a26580eSLuigi Rizzo }
26471a26580eSLuigi Rizzo 
264864ae02c3SLuigi Rizzo 
264901c7d25fSLuigi Rizzo /*
2650f9790aebSLuigi Rizzo  * Module loader and unloader
2651f196ce38SLuigi Rizzo  *
2652f9790aebSLuigi Rizzo  * netmap_init() creates the /dev/netmap device and initializes
2653f9790aebSLuigi Rizzo  * all global variables. Returns 0 on success, errno on failure
2654f9790aebSLuigi Rizzo  * (but there is no chance)
2655f9790aebSLuigi Rizzo  *
2656f9790aebSLuigi Rizzo  * netmap_fini() destroys everything.
2657f196ce38SLuigi Rizzo  */
2658babc7c12SLuigi Rizzo 
2659babc7c12SLuigi Rizzo static struct cdev *netmap_dev; /* /dev/netmap character device. */
2660f9790aebSLuigi Rizzo extern struct cdevsw netmap_cdevsw;
2661babc7c12SLuigi Rizzo 
266217885a7bSLuigi Rizzo 
2663f9790aebSLuigi Rizzo void
266468b8534bSLuigi Rizzo netmap_fini(void)
266568b8534bSLuigi Rizzo {
2666f9790aebSLuigi Rizzo 	// XXX destroy_bridges() ?
2667f9790aebSLuigi Rizzo 	if (netmap_dev)
266868b8534bSLuigi Rizzo 		destroy_dev(netmap_dev);
2669ce3ee1e7SLuigi Rizzo 	netmap_mem_fini();
2670ce3ee1e7SLuigi Rizzo 	NMG_LOCK_DESTROY();
267168b8534bSLuigi Rizzo 	printf("netmap: unloaded module.\n");
267268b8534bSLuigi Rizzo }
267368b8534bSLuigi Rizzo 
267417885a7bSLuigi Rizzo 
2675f9790aebSLuigi Rizzo int
2676f9790aebSLuigi Rizzo netmap_init(void)
267768b8534bSLuigi Rizzo {
2678f9790aebSLuigi Rizzo 	int error;
267968b8534bSLuigi Rizzo 
2680f9790aebSLuigi Rizzo 	NMG_LOCK_INIT();
268168b8534bSLuigi Rizzo 
2682f9790aebSLuigi Rizzo 	error = netmap_mem_init();
2683f9790aebSLuigi Rizzo 	if (error != 0)
2684f9790aebSLuigi Rizzo 		goto fail;
2685f9790aebSLuigi Rizzo 	/* XXX could use make_dev_credv() to get error number */
2686f9790aebSLuigi Rizzo 	netmap_dev = make_dev(&netmap_cdevsw, 0, UID_ROOT, GID_WHEEL, 0660,
2687f9790aebSLuigi Rizzo 			      "netmap");
2688f9790aebSLuigi Rizzo 	if (!netmap_dev)
2689f9790aebSLuigi Rizzo 		goto fail;
2690f9790aebSLuigi Rizzo 
2691f9790aebSLuigi Rizzo 	netmap_init_bridges();
2692f9790aebSLuigi Rizzo 	printf("netmap: loaded module\n");
2693f9790aebSLuigi Rizzo 	return (0);
2694f9790aebSLuigi Rizzo fail:
269568b8534bSLuigi Rizzo 	netmap_fini();
2696f9790aebSLuigi Rizzo 	return (EINVAL); /* may be incorrect */
269768b8534bSLuigi Rizzo }
2698