xref: /freebsd/sys/dev/netmap/netmap_kern.h (revision f9790aeb8869bfcedf111517bace712b390e6cc5)
168b8534bSLuigi Rizzo /*
22579e2d7SLuigi Rizzo  * Copyright (C) 2011-2013 Matteo Landi, Luigi Rizzo. All rights reserved.
3*f9790aebSLuigi Rizzo  * Copyright (C) 2013 Universita` di Pisa. All rights reserved.
468b8534bSLuigi Rizzo  *
568b8534bSLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
668b8534bSLuigi Rizzo  * modification, are permitted provided that the following conditions
768b8534bSLuigi Rizzo  * are met:
868b8534bSLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
968b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
1068b8534bSLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
1168b8534bSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
1268b8534bSLuigi Rizzo  *    documentation and/or other materials provided with the distribution.
1368b8534bSLuigi Rizzo  *
1468b8534bSLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1568b8534bSLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1668b8534bSLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1768b8534bSLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1868b8534bSLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1968b8534bSLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2068b8534bSLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2168b8534bSLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2268b8534bSLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2368b8534bSLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2468b8534bSLuigi Rizzo  * SUCH DAMAGE.
2568b8534bSLuigi Rizzo  */
2668b8534bSLuigi Rizzo 
2768b8534bSLuigi Rizzo /*
2868b8534bSLuigi Rizzo  * $FreeBSD$
2968b8534bSLuigi Rizzo  *
3068b8534bSLuigi Rizzo  * The header contains the definitions of constants and function
3168b8534bSLuigi Rizzo  * prototypes used only in kernelspace.
3268b8534bSLuigi Rizzo  */
3368b8534bSLuigi Rizzo 
3468b8534bSLuigi Rizzo #ifndef _NET_NETMAP_KERN_H_
3568b8534bSLuigi Rizzo #define _NET_NETMAP_KERN_H_
3668b8534bSLuigi Rizzo 
37*f9790aebSLuigi Rizzo #define WITH_VALE	// comment out to disable VALE support
38*f9790aebSLuigi Rizzo 
391a26580eSLuigi Rizzo #if defined(__FreeBSD__)
40d4b42e08SLuigi Rizzo 
41ce3ee1e7SLuigi Rizzo #define likely(x)	__builtin_expect((long)!!(x), 1L)
42ce3ee1e7SLuigi Rizzo #define unlikely(x)	__builtin_expect((long)!!(x), 0L)
43f196ce38SLuigi Rizzo 
441a26580eSLuigi Rizzo #define	NM_LOCK_T	struct mtx
45*f9790aebSLuigi Rizzo #define	NMG_LOCK_T	struct mtx
46*f9790aebSLuigi Rizzo #define NMG_LOCK_INIT()	mtx_init(&netmap_global_lock, \
47*f9790aebSLuigi Rizzo 				"netmap global lock", NULL, MTX_DEF)
48*f9790aebSLuigi Rizzo #define NMG_LOCK_DESTROY()	mtx_destroy(&netmap_global_lock)
49*f9790aebSLuigi Rizzo #define NMG_LOCK()	mtx_lock(&netmap_global_lock)
50*f9790aebSLuigi Rizzo #define NMG_UNLOCK()	mtx_unlock(&netmap_global_lock)
51*f9790aebSLuigi Rizzo #define NMG_LOCK_ASSERT()	mtx_assert(&netmap_global_lock, MA_OWNED)
52*f9790aebSLuigi Rizzo 
531a26580eSLuigi Rizzo #define	NM_SELINFO_T	struct selinfo
541a26580eSLuigi Rizzo #define	MBUF_LEN(m)	((m)->m_pkthdr.len)
55*f9790aebSLuigi Rizzo #define	MBUF_IFP(m)	((m)->m_pkthdr.rcvif)
561a26580eSLuigi Rizzo #define	NM_SEND_UP(ifp, m)	((ifp)->if_input)(ifp, m)
57d4b42e08SLuigi Rizzo 
58*f9790aebSLuigi Rizzo #define NM_ATOMIC_T	volatile int	// XXX ?
59*f9790aebSLuigi Rizzo /* atomic operations */
60*f9790aebSLuigi Rizzo #include <machine/atomic.h>
61*f9790aebSLuigi Rizzo #define NM_ATOMIC_TEST_AND_SET(p)       (!atomic_cmpset_acq_int((p), 0, 1))
62*f9790aebSLuigi Rizzo #define NM_ATOMIC_CLEAR(p)              atomic_store_rel_int((p), 0)
63*f9790aebSLuigi Rizzo 
64*f9790aebSLuigi Rizzo #define prefetch(x)     __builtin_prefetch(x)
65*f9790aebSLuigi Rizzo 
66*f9790aebSLuigi Rizzo MALLOC_DECLARE(M_NETMAP);
67*f9790aebSLuigi Rizzo 
68*f9790aebSLuigi Rizzo // XXX linux struct, not used in FreeBSD
69*f9790aebSLuigi Rizzo struct net_device_ops {
70*f9790aebSLuigi Rizzo };
71*f9790aebSLuigi Rizzo struct hrtimer {
72*f9790aebSLuigi Rizzo };
73ce3ee1e7SLuigi Rizzo 
7464ae02c3SLuigi Rizzo #elif defined (linux)
75d4b42e08SLuigi Rizzo 
762579e2d7SLuigi Rizzo #define	NM_LOCK_T	safe_spinlock_t	// see bsd_glue.h
771a26580eSLuigi Rizzo #define	NM_SELINFO_T	wait_queue_head_t
781a26580eSLuigi Rizzo #define	MBUF_LEN(m)	((m)->len)
79*f9790aebSLuigi Rizzo #define	MBUF_IFP(m)	((m)->dev)
801a26580eSLuigi Rizzo #define	NM_SEND_UP(ifp, m)	netif_rx(m)
81f196ce38SLuigi Rizzo 
82ce3ee1e7SLuigi Rizzo #define NM_ATOMIC_T	volatile long unsigned int
83ce3ee1e7SLuigi Rizzo 
84*f9790aebSLuigi Rizzo // XXX a mtx would suffice here too 20130404 gl
85*f9790aebSLuigi Rizzo #define NMG_LOCK_T		struct semaphore
86*f9790aebSLuigi Rizzo #define NMG_LOCK_INIT()		sema_init(&netmap_global_lock, 1)
87*f9790aebSLuigi Rizzo #define NMG_LOCK_DESTROY()
88*f9790aebSLuigi Rizzo #define NMG_LOCK()		down(&netmap_global_lock)
89*f9790aebSLuigi Rizzo #define NMG_UNLOCK()		up(&netmap_global_lock)
90*f9790aebSLuigi Rizzo #define NMG_LOCK_ASSERT()	//	XXX to be completed
91*f9790aebSLuigi Rizzo 
92f196ce38SLuigi Rizzo #ifndef DEV_NETMAP
93f196ce38SLuigi Rizzo #define DEV_NETMAP
94ce3ee1e7SLuigi Rizzo #endif /* DEV_NETMAP */
95f196ce38SLuigi Rizzo 
96f196ce38SLuigi Rizzo /*
978241616dSLuigi Rizzo  * IFCAP_NETMAP goes into net_device's priv_flags (if_capenable).
988241616dSLuigi Rizzo  * This was 16 bits up to linux 2.6.36, so we need a 16 bit value on older
99f196ce38SLuigi Rizzo  * platforms and tolerate the clash with IFF_DYNAMIC and IFF_BRIDGE_PORT.
1008241616dSLuigi Rizzo  * For the 32-bit value, 0x100000 has no clashes until at least 3.5.1
101f196ce38SLuigi Rizzo  */
102f196ce38SLuigi Rizzo #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,37)
103f196ce38SLuigi Rizzo #define IFCAP_NETMAP	0x8000
104f196ce38SLuigi Rizzo #else
105f18be576SLuigi Rizzo #define IFCAP_NETMAP	0x200000
106f196ce38SLuigi Rizzo #endif
107f196ce38SLuigi Rizzo 
108f196ce38SLuigi Rizzo #elif defined (__APPLE__)
109d4b42e08SLuigi Rizzo 
1108241616dSLuigi Rizzo #warning apple support is incomplete.
111f196ce38SLuigi Rizzo #define likely(x)	__builtin_expect(!!(x), 1)
112f196ce38SLuigi Rizzo #define unlikely(x)	__builtin_expect(!!(x), 0)
113f196ce38SLuigi Rizzo #define	NM_LOCK_T	IOLock *
114f196ce38SLuigi Rizzo #define	NM_SELINFO_T	struct selinfo
115f196ce38SLuigi Rizzo #define	MBUF_LEN(m)	((m)->m_pkthdr.len)
116f196ce38SLuigi Rizzo #define	NM_SEND_UP(ifp, m)	((ifp)->if_input)(ifp, m)
117f196ce38SLuigi Rizzo 
1181a26580eSLuigi Rizzo #else
119d4b42e08SLuigi Rizzo 
1201a26580eSLuigi Rizzo #error unsupported platform
121d4b42e08SLuigi Rizzo 
122d4b42e08SLuigi Rizzo #endif /* end - platform-specific code */
1231a26580eSLuigi Rizzo 
12468b8534bSLuigi Rizzo #define ND(format, ...)
12568b8534bSLuigi Rizzo #define D(format, ...)						\
12668b8534bSLuigi Rizzo 	do {							\
12768b8534bSLuigi Rizzo 		struct timeval __xxts;				\
12868b8534bSLuigi Rizzo 		microtime(&__xxts);				\
12968b8534bSLuigi Rizzo 		printf("%03d.%06d %s [%d] " format "\n",	\
13068b8534bSLuigi Rizzo 		(int)__xxts.tv_sec % 1000, (int)__xxts.tv_usec,	\
13168b8534bSLuigi Rizzo 		__FUNCTION__, __LINE__, ##__VA_ARGS__);		\
13268b8534bSLuigi Rizzo 	} while (0)
13368b8534bSLuigi Rizzo 
1348241616dSLuigi Rizzo /* rate limited, lps indicates how many per second */
1358241616dSLuigi Rizzo #define RD(lps, format, ...)					\
1368241616dSLuigi Rizzo 	do {							\
1378241616dSLuigi Rizzo 		static int t0, __cnt;				\
1388241616dSLuigi Rizzo 		if (t0 != time_second) {			\
1398241616dSLuigi Rizzo 			t0 = time_second;			\
1408241616dSLuigi Rizzo 			__cnt = 0;				\
1418241616dSLuigi Rizzo 		}						\
1428241616dSLuigi Rizzo 		if (__cnt++ < lps)				\
1438241616dSLuigi Rizzo 			D(format, ##__VA_ARGS__);		\
1448241616dSLuigi Rizzo 	} while (0)
1458241616dSLuigi Rizzo 
14668b8534bSLuigi Rizzo struct netmap_adapter;
147f18be576SLuigi Rizzo struct nm_bdg_fwd;
148f18be576SLuigi Rizzo struct nm_bridge;
149f18be576SLuigi Rizzo struct netmap_priv_d;
15068b8534bSLuigi Rizzo 
151ce3ee1e7SLuigi Rizzo const char *nm_dump_buf(char *p, int len, int lim, char *dst);
152ce3ee1e7SLuigi Rizzo 
153*f9790aebSLuigi Rizzo #include "netmap_mbq.h"
154*f9790aebSLuigi Rizzo 
155*f9790aebSLuigi Rizzo extern NMG_LOCK_T	netmap_global_lock;
156*f9790aebSLuigi Rizzo 
15768b8534bSLuigi Rizzo /*
15864ae02c3SLuigi Rizzo  * private, kernel view of a ring. Keeps track of the status of
15964ae02c3SLuigi Rizzo  * a ring across system calls.
16064ae02c3SLuigi Rizzo  *
16164ae02c3SLuigi Rizzo  *	nr_hwcur	index of the next buffer to refill.
16264ae02c3SLuigi Rizzo  *			It corresponds to ring->cur - ring->reserved
16364ae02c3SLuigi Rizzo  *
16464ae02c3SLuigi Rizzo  *	nr_hwavail	the number of slots "owned" by userspace.
16564ae02c3SLuigi Rizzo  *			nr_hwavail =:= ring->avail + ring->reserved
16668b8534bSLuigi Rizzo  *
1671a26580eSLuigi Rizzo  * The indexes in the NIC and netmap rings are offset by nkr_hwofs slots.
16868b8534bSLuigi Rizzo  * This is so that, on a reset, buffers owned by userspace are not
16968b8534bSLuigi Rizzo  * modified by the kernel. In particular:
1701a26580eSLuigi Rizzo  * RX rings: the next empty buffer (hwcur + hwavail + hwofs) coincides with
17168b8534bSLuigi Rizzo  * 	the next empty buffer as known by the hardware (next_to_check or so).
17268b8534bSLuigi Rizzo  * TX rings: hwcur + hwofs coincides with next_to_send
1731dce924dSLuigi Rizzo  *
174ce3ee1e7SLuigi Rizzo  * Clients cannot issue concurrent syscall on a ring. The system
175ce3ee1e7SLuigi Rizzo  * detects this and reports an error using two flags,
176ce3ee1e7SLuigi Rizzo  * NKR_WBUSY and NKR_RBUSY
1771dce924dSLuigi Rizzo  * For received packets, slot->flags is set to nkr_slot_flags
1781dce924dSLuigi Rizzo  * so we can provide a proper initial value (e.g. set NS_FORWARD
1791dce924dSLuigi Rizzo  * when operating in 'transparent' mode).
180ce3ee1e7SLuigi Rizzo  *
181ce3ee1e7SLuigi Rizzo  * The following fields are used to implement lock-free copy of packets
182ce3ee1e7SLuigi Rizzo  * from input to output ports in VALE switch:
183ce3ee1e7SLuigi Rizzo  *	nkr_hwlease	buffer after the last one being copied.
184ce3ee1e7SLuigi Rizzo  *			A writer in nm_bdg_flush reserves N buffers
185ce3ee1e7SLuigi Rizzo  *			from nr_hwlease, advances it, then does the
186ce3ee1e7SLuigi Rizzo  *			copy outside the lock.
187ce3ee1e7SLuigi Rizzo  *			In RX rings (used for VALE ports),
188ce3ee1e7SLuigi Rizzo  *			nkr_hwcur + nkr_hwavail <= nkr_hwlease < nkr_hwcur+N-1
189ce3ee1e7SLuigi Rizzo  *			In TX rings (used for NIC or host stack ports)
190ce3ee1e7SLuigi Rizzo  *			nkr_hwcur <= nkr_hwlease < nkr_hwcur+ nkr_hwavail
191ce3ee1e7SLuigi Rizzo  *	nkr_leases	array of nkr_num_slots where writers can report
192ce3ee1e7SLuigi Rizzo  *			completion of their block. NR_NOSLOT (~0) indicates
193ce3ee1e7SLuigi Rizzo  *			that the writer has not finished yet
194ce3ee1e7SLuigi Rizzo  *	nkr_lease_idx	index of next free slot in nr_leases, to be assigned
195ce3ee1e7SLuigi Rizzo  *
196ce3ee1e7SLuigi Rizzo  * The kring is manipulated by txsync/rxsync and generic netmap function.
197ce3ee1e7SLuigi Rizzo  * q_lock is used to arbitrate access to the kring from within the netmap
198ce3ee1e7SLuigi Rizzo  * code, and this and other protections guarantee that there is never
199ce3ee1e7SLuigi Rizzo  * more than 1 concurrent call to txsync or rxsync. So we are free
200ce3ee1e7SLuigi Rizzo  * to manipulate the kring from within txsync/rxsync without any extra
201ce3ee1e7SLuigi Rizzo  * locks.
20268b8534bSLuigi Rizzo  */
20368b8534bSLuigi Rizzo struct netmap_kring {
20468b8534bSLuigi Rizzo 	struct netmap_ring *ring;
205ce3ee1e7SLuigi Rizzo 	uint32_t nr_hwcur;
206ce3ee1e7SLuigi Rizzo 	uint32_t nr_hwavail;
207ce3ee1e7SLuigi Rizzo 	uint32_t nr_kflags;	/* private driver flags */
208*f9790aebSLuigi Rizzo 	int32_t nr_hwreserved;
2092157a17cSLuigi Rizzo #define NKR_PENDINTR	0x1	// Pending interrupt.
210ce3ee1e7SLuigi Rizzo 	uint32_t nkr_num_slots;
211ce3ee1e7SLuigi Rizzo 	int32_t	nkr_hwofs;	/* offset between NIC and netmap ring */
21268b8534bSLuigi Rizzo 
2131dce924dSLuigi Rizzo 	uint16_t	nkr_slot_flags;	/* initial value for flags */
2141a26580eSLuigi Rizzo 	struct netmap_adapter *na;
215f18be576SLuigi Rizzo 	struct nm_bdg_fwd *nkr_ft;
216ce3ee1e7SLuigi Rizzo 	uint32_t *nkr_leases;
217ce3ee1e7SLuigi Rizzo #define NR_NOSLOT	((uint32_t)~0)
218ce3ee1e7SLuigi Rizzo 	uint32_t nkr_hwlease;
219ce3ee1e7SLuigi Rizzo 	uint32_t nkr_lease_idx;
220ce3ee1e7SLuigi Rizzo 
2211a26580eSLuigi Rizzo 	NM_SELINFO_T si;	/* poll/select wait queue */
222ce3ee1e7SLuigi Rizzo 	NM_LOCK_T q_lock;	/* protects kring and ring. */
223ce3ee1e7SLuigi Rizzo 	NM_ATOMIC_T nr_busy;	/* prevent concurrent syscalls */
224ce3ee1e7SLuigi Rizzo 
225ce3ee1e7SLuigi Rizzo 	volatile int nkr_stopped;
226*f9790aebSLuigi Rizzo 
227*f9790aebSLuigi Rizzo 	/* support for adapters without native netmap support.
228*f9790aebSLuigi Rizzo 	 * On tx rings we preallocate an array of tx buffers
229*f9790aebSLuigi Rizzo 	 * (same size as the netmap ring), on rx rings we
230*f9790aebSLuigi Rizzo 	 * store incoming packets in a queue.
231*f9790aebSLuigi Rizzo 	 * XXX who writes to the rx queue ?
232*f9790aebSLuigi Rizzo 	 */
233*f9790aebSLuigi Rizzo 	struct mbuf **tx_pool;
234*f9790aebSLuigi Rizzo 	u_int nr_ntc;                   /* Emulation of a next-to-clean RX ring pointer. */
235*f9790aebSLuigi Rizzo 	struct mbq rx_queue;            /* A queue for intercepted rx mbufs. */
236*f9790aebSLuigi Rizzo 
2372157a17cSLuigi Rizzo } __attribute__((__aligned__(64)));
23868b8534bSLuigi Rizzo 
239ce3ee1e7SLuigi Rizzo 
240ce3ee1e7SLuigi Rizzo /* return the next index, with wraparound */
241ce3ee1e7SLuigi Rizzo static inline uint32_t
242ce3ee1e7SLuigi Rizzo nm_next(uint32_t i, uint32_t lim)
243ce3ee1e7SLuigi Rizzo {
244ce3ee1e7SLuigi Rizzo 	return unlikely (i == lim) ? 0 : i + 1;
245ce3ee1e7SLuigi Rizzo }
246ce3ee1e7SLuigi Rizzo 
247ce3ee1e7SLuigi Rizzo /*
248ce3ee1e7SLuigi Rizzo  *
249ce3ee1e7SLuigi Rizzo  * Here is the layout for the Rx and Tx rings.
250ce3ee1e7SLuigi Rizzo 
251ce3ee1e7SLuigi Rizzo        RxRING                            TxRING
252ce3ee1e7SLuigi Rizzo 
253ce3ee1e7SLuigi Rizzo       +-----------------+            +-----------------+
254ce3ee1e7SLuigi Rizzo       |                 |            |                 |
255ce3ee1e7SLuigi Rizzo       |XXX free slot XXX|            |XXX free slot XXX|
256ce3ee1e7SLuigi Rizzo       +-----------------+            +-----------------+
257ce3ee1e7SLuigi Rizzo       |                 |<-hwcur     |                 |<-hwcur
258ce3ee1e7SLuigi Rizzo       | reserved    h   |            | (ready          |
259ce3ee1e7SLuigi Rizzo       +-----------  w  -+            |  to be          |
260ce3ee1e7SLuigi Rizzo  cur->|             a   |            |  sent)      h   |
261ce3ee1e7SLuigi Rizzo       |             v   |            +----------   w   |
262ce3ee1e7SLuigi Rizzo       |             a   |       cur->| (being      a   |
263ce3ee1e7SLuigi Rizzo       |             i   |            |  prepared)  v   |
264ce3ee1e7SLuigi Rizzo       | avail       l   |            |             a   |
265ce3ee1e7SLuigi Rizzo       +-----------------+            +  a  ------  i   +
266ce3ee1e7SLuigi Rizzo       |                 | ...        |  v          l   |<-hwlease
267ce3ee1e7SLuigi Rizzo       | (being          | ...        |  a              | ...
268ce3ee1e7SLuigi Rizzo       |  prepared)      | ...        |  i              | ...
269ce3ee1e7SLuigi Rizzo       +-----------------+ ...        |  l              | ...
270ce3ee1e7SLuigi Rizzo       |                 |<-hwlease   +-----------------+
271ce3ee1e7SLuigi Rizzo       |                 |            |                 |
272ce3ee1e7SLuigi Rizzo       |                 |            |                 |
273ce3ee1e7SLuigi Rizzo       |                 |            |                 |
274ce3ee1e7SLuigi Rizzo       |                 |            |                 |
275ce3ee1e7SLuigi Rizzo       +-----------------+            +-----------------+
276ce3ee1e7SLuigi Rizzo 
277ce3ee1e7SLuigi Rizzo  * The cur/avail (user view) and hwcur/hwavail (kernel view)
278ce3ee1e7SLuigi Rizzo  * are used in the normal operation of the card.
279ce3ee1e7SLuigi Rizzo  *
280ce3ee1e7SLuigi Rizzo  * When a ring is the output of a switch port (Rx ring for
281ce3ee1e7SLuigi Rizzo  * a VALE port, Tx ring for the host stack or NIC), slots
282ce3ee1e7SLuigi Rizzo  * are reserved in blocks through 'hwlease' which points
283ce3ee1e7SLuigi Rizzo  * to the next unused slot.
284ce3ee1e7SLuigi Rizzo  * On an Rx ring, hwlease is always after hwavail,
285ce3ee1e7SLuigi Rizzo  * and completions cause avail to advance.
286ce3ee1e7SLuigi Rizzo  * On a Tx ring, hwlease is always between cur and hwavail,
287ce3ee1e7SLuigi Rizzo  * and completions cause cur to advance.
288ce3ee1e7SLuigi Rizzo  *
289ce3ee1e7SLuigi Rizzo  * nm_kr_space() returns the maximum number of slots that
290ce3ee1e7SLuigi Rizzo  * can be assigned.
291ce3ee1e7SLuigi Rizzo  * nm_kr_lease() reserves the required number of buffers,
292ce3ee1e7SLuigi Rizzo  *    advances nkr_hwlease and also returns an entry in
293ce3ee1e7SLuigi Rizzo  *    a circular array where completions should be reported.
294ce3ee1e7SLuigi Rizzo  */
295ce3ee1e7SLuigi Rizzo 
296ce3ee1e7SLuigi Rizzo 
297ce3ee1e7SLuigi Rizzo 
298ce3ee1e7SLuigi Rizzo 
299*f9790aebSLuigi Rizzo enum txrx { NR_RX = 0, NR_TX = 1 };
300ce3ee1e7SLuigi Rizzo 
30168b8534bSLuigi Rizzo /*
302*f9790aebSLuigi Rizzo  * The "struct netmap_adapter" extends the "struct adapter"
303*f9790aebSLuigi Rizzo  * (or equivalent) device descriptor.
304*f9790aebSLuigi Rizzo  * It contains all base fields needed to support netmap operation.
305*f9790aebSLuigi Rizzo  * There are in fact different types of netmap adapters
306*f9790aebSLuigi Rizzo  * (native, generic, VALE switch...) so a netmap_adapter is
307*f9790aebSLuigi Rizzo  * just the first field in the derived type.
30868b8534bSLuigi Rizzo  */
30968b8534bSLuigi Rizzo struct netmap_adapter {
3108241616dSLuigi Rizzo 	/*
3118241616dSLuigi Rizzo 	 * On linux we do not have a good way to tell if an interface
312*f9790aebSLuigi Rizzo 	 * is netmap-capable. So we always use the following trick:
3138241616dSLuigi Rizzo 	 * NA(ifp) points here, and the first entry (which hopefully
3148241616dSLuigi Rizzo 	 * always exists and is at least 32 bits) contains a magic
3158241616dSLuigi Rizzo 	 * value which we can use to detect that the interface is good.
3168241616dSLuigi Rizzo 	 */
3178241616dSLuigi Rizzo 	uint32_t magic;
318*f9790aebSLuigi Rizzo 	uint32_t na_flags;	/* enabled, and other flags */
3198241616dSLuigi Rizzo #define NAF_SKIP_INTR	1	/* use the regular interrupt handler.
3208241616dSLuigi Rizzo 				 * useful during initialization
3218241616dSLuigi Rizzo 				 */
322f18be576SLuigi Rizzo #define NAF_SW_ONLY	2	/* forward packets only to sw adapter */
323ce3ee1e7SLuigi Rizzo #define NAF_BDG_MAYSLEEP 4	/* the bridge is allowed to sleep when
324ce3ee1e7SLuigi Rizzo 				 * forwarding packets coming from this
325ce3ee1e7SLuigi Rizzo 				 * interface
326ce3ee1e7SLuigi Rizzo 				 */
327ce3ee1e7SLuigi Rizzo #define NAF_MEM_OWNER	8	/* the adapter is responsible for the
328ce3ee1e7SLuigi Rizzo 				 * deallocation of the memory allocator
329ce3ee1e7SLuigi Rizzo 				 */
330*f9790aebSLuigi Rizzo #define NAF_NATIVE_ON   16      /* the adapter is native and the attached
331*f9790aebSLuigi Rizzo 				 * interface is in netmap mode
332*f9790aebSLuigi Rizzo 				 */
333*f9790aebSLuigi Rizzo #define	NAF_NETMAP_ON	32	/* netmap is active (either native or
334*f9790aebSLuigi Rizzo 				 * emulated. Where possible (e.g. FreeBSD)
335*f9790aebSLuigi Rizzo 				 * IFCAP_NETMAP also mirrors this flag.
336*f9790aebSLuigi Rizzo 				 */
337*f9790aebSLuigi Rizzo 	int active_fds; /* number of user-space descriptors using this
33868b8534bSLuigi Rizzo 			 interface, which is equal to the number of
33968b8534bSLuigi Rizzo 			 struct netmap_if objs in the mapped region. */
34068b8534bSLuigi Rizzo 
34124e57ec9SEd Maste 	u_int num_rx_rings; /* number of adapter receive rings */
34224e57ec9SEd Maste 	u_int num_tx_rings; /* number of adapter transmit rings */
34368b8534bSLuigi Rizzo 
34468b8534bSLuigi Rizzo 	u_int num_tx_desc; /* number of descriptor in each queue */
34568b8534bSLuigi Rizzo 	u_int num_rx_desc;
34668b8534bSLuigi Rizzo 
34768b8534bSLuigi Rizzo 	/* tx_rings and rx_rings are private but allocated
34868b8534bSLuigi Rizzo 	 * as a contiguous chunk of memory. Each array has
34968b8534bSLuigi Rizzo 	 * N+1 entries, for the adapter queues and for the host queue.
35068b8534bSLuigi Rizzo 	 */
35168b8534bSLuigi Rizzo 	struct netmap_kring *tx_rings; /* array of TX rings. */
35268b8534bSLuigi Rizzo 	struct netmap_kring *rx_rings; /* array of RX rings. */
353*f9790aebSLuigi Rizzo 	void *tailroom;		       /* space below the rings array */
354*f9790aebSLuigi Rizzo 				       /* (used for leases) */
355*f9790aebSLuigi Rizzo 
35668b8534bSLuigi Rizzo 
35764ae02c3SLuigi Rizzo 	NM_SELINFO_T tx_si, rx_si;	/* global wait queues */
35864ae02c3SLuigi Rizzo 
35968b8534bSLuigi Rizzo 	/* copy of if_qflush and if_transmit pointers, to intercept
36068b8534bSLuigi Rizzo 	 * packets from the network stack when netmap is active.
36168b8534bSLuigi Rizzo 	 */
36268b8534bSLuigi Rizzo 	int     (*if_transmit)(struct ifnet *, struct mbuf *);
36368b8534bSLuigi Rizzo 
36468b8534bSLuigi Rizzo 	/* references to the ifnet and device routines, used by
36568b8534bSLuigi Rizzo 	 * the generic netmap functions.
36668b8534bSLuigi Rizzo 	 */
36768b8534bSLuigi Rizzo 	struct ifnet *ifp; /* adapter is ifp->if_softc */
36868b8534bSLuigi Rizzo 
369*f9790aebSLuigi Rizzo 	/* private cleanup */
370*f9790aebSLuigi Rizzo 	void (*nm_dtor)(struct netmap_adapter *);
3711a26580eSLuigi Rizzo 
372*f9790aebSLuigi Rizzo 	int (*nm_register)(struct netmap_adapter *, int onoff);
373ce3ee1e7SLuigi Rizzo 
374*f9790aebSLuigi Rizzo 	int (*nm_txsync)(struct netmap_adapter *, u_int ring, int flags);
375*f9790aebSLuigi Rizzo 	int (*nm_rxsync)(struct netmap_adapter *, u_int ring, int flags);
376ce3ee1e7SLuigi Rizzo #define NAF_FORCE_READ    1
377ce3ee1e7SLuigi Rizzo #define NAF_FORCE_RECLAIM 2
378ae10d1afSLuigi Rizzo 	/* return configuration information */
379*f9790aebSLuigi Rizzo 	int (*nm_config)(struct netmap_adapter *,
380*f9790aebSLuigi Rizzo 		u_int *txr, u_int *txd, u_int *rxr, u_int *rxd);
381*f9790aebSLuigi Rizzo 	int (*nm_krings_create)(struct netmap_adapter *);
382*f9790aebSLuigi Rizzo 	void (*nm_krings_delete)(struct netmap_adapter *);
383*f9790aebSLuigi Rizzo 	int (*nm_notify)(struct netmap_adapter *,
384*f9790aebSLuigi Rizzo 		u_int ring, enum txrx, int flags);
385*f9790aebSLuigi Rizzo #define NAF_GLOBAL_NOTIFY 4
386*f9790aebSLuigi Rizzo #define NAF_DISABLE_NOTIFY 8
387*f9790aebSLuigi Rizzo 
388*f9790aebSLuigi Rizzo 	/* standard refcount to control the lifetime of the adapter
389*f9790aebSLuigi Rizzo 	 * (it should be equal to the lifetime of the corresponding ifp)
390*f9790aebSLuigi Rizzo 	 */
391*f9790aebSLuigi Rizzo 	int na_refcount;
392*f9790aebSLuigi Rizzo 
393*f9790aebSLuigi Rizzo 	/* memory allocator (opaque)
394*f9790aebSLuigi Rizzo 	 * We also cache a pointer to the lut_entry for translating
395*f9790aebSLuigi Rizzo 	 * buffer addresses, and the total number of buffers.
396*f9790aebSLuigi Rizzo 	 */
397*f9790aebSLuigi Rizzo  	struct netmap_mem_d *nm_mem;
398*f9790aebSLuigi Rizzo 	struct lut_entry *na_lut;
399*f9790aebSLuigi Rizzo 	uint32_t na_lut_objtotal;	/* max buffer index */
400*f9790aebSLuigi Rizzo 
401*f9790aebSLuigi Rizzo 	/* used internally. If non-null, the interface cannot be bound
402*f9790aebSLuigi Rizzo 	 * from userspace
403*f9790aebSLuigi Rizzo 	 */
404*f9790aebSLuigi Rizzo 	void *na_private;
405*f9790aebSLuigi Rizzo };
406*f9790aebSLuigi Rizzo 
407*f9790aebSLuigi Rizzo /*
408*f9790aebSLuigi Rizzo  * If the NIC is owned by the kernel
409*f9790aebSLuigi Rizzo  * (i.e., bridge), neither another bridge nor user can use it;
410*f9790aebSLuigi Rizzo  * if the NIC is owned by a user, only users can share it.
411*f9790aebSLuigi Rizzo  * Evaluation must be done under NMG_LOCK().
412*f9790aebSLuigi Rizzo  */
413*f9790aebSLuigi Rizzo #define NETMAP_OWNED_BY_KERN(na)	(na->na_private)
414*f9790aebSLuigi Rizzo #define NETMAP_OWNED_BY_ANY(na) \
415*f9790aebSLuigi Rizzo 	(NETMAP_OWNED_BY_KERN(na) || (na->active_fds > 0))
416*f9790aebSLuigi Rizzo 
417*f9790aebSLuigi Rizzo 
418*f9790aebSLuigi Rizzo /*
419*f9790aebSLuigi Rizzo  * derived netmap adapters for various types of ports
420*f9790aebSLuigi Rizzo  */
421*f9790aebSLuigi Rizzo struct netmap_vp_adapter {	/* VALE software port */
422*f9790aebSLuigi Rizzo 	struct netmap_adapter up;
423f196ce38SLuigi Rizzo 
424849bec0eSLuigi Rizzo 	/*
425849bec0eSLuigi Rizzo 	 * Bridge support:
426849bec0eSLuigi Rizzo 	 *
427849bec0eSLuigi Rizzo 	 * bdg_port is the port number used in the bridge;
428f18be576SLuigi Rizzo 	 * na_bdg points to the bridge this NA is attached to.
429849bec0eSLuigi Rizzo 	 */
430f196ce38SLuigi Rizzo 	int bdg_port;
431f18be576SLuigi Rizzo 	struct nm_bridge *na_bdg;
432*f9790aebSLuigi Rizzo 	int retry;
433*f9790aebSLuigi Rizzo 
434*f9790aebSLuigi Rizzo 	u_int offset;   /* Offset of ethernet header for each packet. */
435*f9790aebSLuigi Rizzo };
436*f9790aebSLuigi Rizzo 
437*f9790aebSLuigi Rizzo struct netmap_hw_adapter {	/* physical device */
438*f9790aebSLuigi Rizzo 	struct netmap_adapter up;
439*f9790aebSLuigi Rizzo 
440*f9790aebSLuigi Rizzo 	struct net_device_ops nm_ndo;	// XXX linux only
441*f9790aebSLuigi Rizzo };
442*f9790aebSLuigi Rizzo 
443*f9790aebSLuigi Rizzo struct netmap_generic_adapter {	/* non-native device */
444*f9790aebSLuigi Rizzo 	struct netmap_hw_adapter up;
445*f9790aebSLuigi Rizzo 
446*f9790aebSLuigi Rizzo 	/* Pointer to a previously used netmap adapter. */
447*f9790aebSLuigi Rizzo 	struct netmap_adapter *prev;
448*f9790aebSLuigi Rizzo 
449*f9790aebSLuigi Rizzo 	/* generic netmap adapters support:
450*f9790aebSLuigi Rizzo 	 * a net_device_ops struct overrides ndo_select_queue(),
451*f9790aebSLuigi Rizzo 	 * save_if_input saves the if_input hook (FreeBSD),
452*f9790aebSLuigi Rizzo 	 * mit_timer and mit_pending implement rx interrupt mitigation,
453*f9790aebSLuigi Rizzo 	 */
454*f9790aebSLuigi Rizzo 	struct net_device_ops generic_ndo;
455*f9790aebSLuigi Rizzo 	void (*save_if_input)(struct ifnet *, struct mbuf *);
456*f9790aebSLuigi Rizzo 
457*f9790aebSLuigi Rizzo 	struct hrtimer mit_timer;
458*f9790aebSLuigi Rizzo 	int mit_pending;
459*f9790aebSLuigi Rizzo };
460*f9790aebSLuigi Rizzo 
461*f9790aebSLuigi Rizzo #ifdef WITH_VALE
462*f9790aebSLuigi Rizzo 
463*f9790aebSLuigi Rizzo /* bridge wrapper for non VALE ports. It is used to connect real devices to the bridge.
464*f9790aebSLuigi Rizzo  *
465*f9790aebSLuigi Rizzo  * The real device must already have its own netmap adapter (hwna).  The
466*f9790aebSLuigi Rizzo  * bridge wrapper and the hwna adapter share the same set of netmap rings and
467*f9790aebSLuigi Rizzo  * buffers, but they have two separate sets of krings descriptors, with tx/rx
468*f9790aebSLuigi Rizzo  * meanings swapped:
469*f9790aebSLuigi Rizzo  *
470*f9790aebSLuigi Rizzo  *                                  netmap
471*f9790aebSLuigi Rizzo  *           bwrap     krings       rings      krings      hwna
472*f9790aebSLuigi Rizzo  *         +------+   +------+     +-----+    +------+   +------+
473*f9790aebSLuigi Rizzo  *         |tx_rings->|      |\   /|     |----|      |<-tx_rings|
474*f9790aebSLuigi Rizzo  *         |      |   +------+ \ / +-----+    +------+   |      |
475*f9790aebSLuigi Rizzo  *         |      |             X                        |      |
476*f9790aebSLuigi Rizzo  *         |      |            / \                       |      |
477*f9790aebSLuigi Rizzo  *         |      |   +------+/   \+-----+    +------+   |      |
478*f9790aebSLuigi Rizzo  *         |rx_rings->|      |     |     |----|      |<-rx_rings|
479*f9790aebSLuigi Rizzo  *         |      |   +------+     +-----+    +------+   |      |
480*f9790aebSLuigi Rizzo  *         +------+                                      +------+
481*f9790aebSLuigi Rizzo  *
482*f9790aebSLuigi Rizzo  * - packets coming from the bridge go to the brwap rx rings, which are also the
483*f9790aebSLuigi Rizzo  *   hwna tx rings.  The bwrap notify callback will then complete the hwna tx
484*f9790aebSLuigi Rizzo  *   (see netmap_bwrap_notify).
485*f9790aebSLuigi Rizzo  * - packets coming from the outside go to the hwna rx rings, which are also the
486*f9790aebSLuigi Rizzo  *   bwrap tx rings.  The (overwritten) hwna notify method will then complete
487*f9790aebSLuigi Rizzo  *   the bridge tx (see netmap_bwrap_intr_notify).
488*f9790aebSLuigi Rizzo  *
489*f9790aebSLuigi Rizzo  *   The bridge wrapper may optionally connect the hwna 'host' rings to the
490*f9790aebSLuigi Rizzo  *   bridge. This is done by using a second port in the bridge and connecting it
491*f9790aebSLuigi Rizzo  *   to the 'host' netmap_vp_adapter contained in the netmap_bwrap_adapter.
492*f9790aebSLuigi Rizzo  *   The brwap host adapter cross-links the hwna host rings in the same way as shown above.
493*f9790aebSLuigi Rizzo  *
494*f9790aebSLuigi Rizzo  * - packets coming from the bridge and directed to host stack are handled by the
495*f9790aebSLuigi Rizzo  *   bwrap host notify callback (see netmap_bwrap_host_notify)
496*f9790aebSLuigi Rizzo  * - packets coming from the host stack are still handled by the overwritten
497*f9790aebSLuigi Rizzo  *   hwna notify callback (netmap_bwrap_intr_notify), but are diverted to the
498*f9790aebSLuigi Rizzo  *   host adapter depending on the ring number.
499*f9790aebSLuigi Rizzo  *
500*f9790aebSLuigi Rizzo  */
501*f9790aebSLuigi Rizzo struct netmap_bwrap_adapter {
502*f9790aebSLuigi Rizzo 	struct netmap_vp_adapter up;
503*f9790aebSLuigi Rizzo 	struct netmap_vp_adapter host;  /* for host rings */
504*f9790aebSLuigi Rizzo 	struct netmap_adapter *hwna;	/* the underlying device */
505*f9790aebSLuigi Rizzo 
506*f9790aebSLuigi Rizzo 	/* backup of the hwna notify callback */
507*f9790aebSLuigi Rizzo 	int (*save_notify)(struct netmap_adapter *,
508*f9790aebSLuigi Rizzo 			u_int ring, enum txrx, int flags);
509f18be576SLuigi Rizzo 	/* When we attach a physical interface to the bridge, we
510f18be576SLuigi Rizzo 	 * allow the controlling process to terminate, so we need
511f18be576SLuigi Rizzo 	 * a place to store the netmap_priv_d data structure.
512f18be576SLuigi Rizzo 	 * This is only done when physical interfaces are attached to a bridge.
513f18be576SLuigi Rizzo 	 */
514f18be576SLuigi Rizzo 	struct netmap_priv_d *na_kpriv;
51568b8534bSLuigi Rizzo };
51668b8534bSLuigi Rizzo 
517*f9790aebSLuigi Rizzo 
51868b8534bSLuigi Rizzo /*
519*f9790aebSLuigi Rizzo  * Available space in the ring. Only used in VALE code
520ce3ee1e7SLuigi Rizzo  */
521ce3ee1e7SLuigi Rizzo static inline uint32_t
522ce3ee1e7SLuigi Rizzo nm_kr_space(struct netmap_kring *k, int is_rx)
523ce3ee1e7SLuigi Rizzo {
524ce3ee1e7SLuigi Rizzo 	int space;
525ce3ee1e7SLuigi Rizzo 
526ce3ee1e7SLuigi Rizzo 	if (is_rx) {
527*f9790aebSLuigi Rizzo 		int busy = k->nkr_hwlease - k->nr_hwcur + k->nr_hwreserved;
528ce3ee1e7SLuigi Rizzo 		if (busy < 0)
529ce3ee1e7SLuigi Rizzo 			busy += k->nkr_num_slots;
530ce3ee1e7SLuigi Rizzo 		space = k->nkr_num_slots - 1 - busy;
531ce3ee1e7SLuigi Rizzo 	} else {
532ce3ee1e7SLuigi Rizzo 		space = k->nr_hwcur + k->nr_hwavail - k->nkr_hwlease;
533ce3ee1e7SLuigi Rizzo 		if (space < 0)
534ce3ee1e7SLuigi Rizzo 			space += k->nkr_num_slots;
535ce3ee1e7SLuigi Rizzo 	}
536ce3ee1e7SLuigi Rizzo #if 0
537ce3ee1e7SLuigi Rizzo 	// sanity check
538ce3ee1e7SLuigi Rizzo 	if (k->nkr_hwlease >= k->nkr_num_slots ||
539ce3ee1e7SLuigi Rizzo 		k->nr_hwcur >= k->nkr_num_slots ||
540ce3ee1e7SLuigi Rizzo 		k->nr_hwavail >= k->nkr_num_slots ||
541ce3ee1e7SLuigi Rizzo 		busy < 0 ||
542ce3ee1e7SLuigi Rizzo 		busy >= k->nkr_num_slots) {
543ce3ee1e7SLuigi Rizzo 		D("invalid kring, cur %d avail %d lease %d lease_idx %d lim %d",			k->nr_hwcur, k->nr_hwavail, k->nkr_hwlease,
544ce3ee1e7SLuigi Rizzo 			k->nkr_lease_idx, k->nkr_num_slots);
545ce3ee1e7SLuigi Rizzo 	}
546ce3ee1e7SLuigi Rizzo #endif
547ce3ee1e7SLuigi Rizzo 	return space;
548ce3ee1e7SLuigi Rizzo }
549ce3ee1e7SLuigi Rizzo 
550ce3ee1e7SLuigi Rizzo 
551ce3ee1e7SLuigi Rizzo 
552ce3ee1e7SLuigi Rizzo 
553ce3ee1e7SLuigi Rizzo /* make a lease on the kring for N positions. return the
554ce3ee1e7SLuigi Rizzo  * lease index
555ce3ee1e7SLuigi Rizzo  */
556ce3ee1e7SLuigi Rizzo static inline uint32_t
557ce3ee1e7SLuigi Rizzo nm_kr_lease(struct netmap_kring *k, u_int n, int is_rx)
558ce3ee1e7SLuigi Rizzo {
559ce3ee1e7SLuigi Rizzo 	uint32_t lim = k->nkr_num_slots - 1;
560ce3ee1e7SLuigi Rizzo 	uint32_t lease_idx = k->nkr_lease_idx;
561ce3ee1e7SLuigi Rizzo 
562ce3ee1e7SLuigi Rizzo 	k->nkr_leases[lease_idx] = NR_NOSLOT;
563ce3ee1e7SLuigi Rizzo 	k->nkr_lease_idx = nm_next(lease_idx, lim);
564ce3ee1e7SLuigi Rizzo 
565ce3ee1e7SLuigi Rizzo 	if (n > nm_kr_space(k, is_rx)) {
566ce3ee1e7SLuigi Rizzo 		D("invalid request for %d slots", n);
567ce3ee1e7SLuigi Rizzo 		panic("x");
568ce3ee1e7SLuigi Rizzo 	}
569ce3ee1e7SLuigi Rizzo 	/* XXX verify that there are n slots */
570ce3ee1e7SLuigi Rizzo 	k->nkr_hwlease += n;
571ce3ee1e7SLuigi Rizzo 	if (k->nkr_hwlease > lim)
572ce3ee1e7SLuigi Rizzo 		k->nkr_hwlease -= lim + 1;
573ce3ee1e7SLuigi Rizzo 
574ce3ee1e7SLuigi Rizzo 	if (k->nkr_hwlease >= k->nkr_num_slots ||
575ce3ee1e7SLuigi Rizzo 		k->nr_hwcur >= k->nkr_num_slots ||
576ce3ee1e7SLuigi Rizzo 		k->nr_hwavail >= k->nkr_num_slots ||
577ce3ee1e7SLuigi Rizzo 		k->nkr_lease_idx >= k->nkr_num_slots) {
578ce3ee1e7SLuigi Rizzo 		D("invalid kring %s, cur %d avail %d lease %d lease_idx %d lim %d",
579ce3ee1e7SLuigi Rizzo 			k->na->ifp->if_xname,
580ce3ee1e7SLuigi Rizzo 			k->nr_hwcur, k->nr_hwavail, k->nkr_hwlease,
581ce3ee1e7SLuigi Rizzo 			k->nkr_lease_idx, k->nkr_num_slots);
582ce3ee1e7SLuigi Rizzo 	}
583ce3ee1e7SLuigi Rizzo 	return lease_idx;
584ce3ee1e7SLuigi Rizzo }
585ce3ee1e7SLuigi Rizzo 
586*f9790aebSLuigi Rizzo #endif /* WITH_VALE */
587*f9790aebSLuigi Rizzo 
588*f9790aebSLuigi Rizzo /* return update position */
589*f9790aebSLuigi Rizzo static inline uint32_t
590*f9790aebSLuigi Rizzo nm_kr_rxpos(struct netmap_kring *k)
591*f9790aebSLuigi Rizzo {
592*f9790aebSLuigi Rizzo 	uint32_t pos = k->nr_hwcur + k->nr_hwavail;
593*f9790aebSLuigi Rizzo 	if (pos >= k->nkr_num_slots)
594*f9790aebSLuigi Rizzo 		pos -= k->nkr_num_slots;
595*f9790aebSLuigi Rizzo #if 0
596*f9790aebSLuigi Rizzo 	if (pos >= k->nkr_num_slots ||
597*f9790aebSLuigi Rizzo 		k->nkr_hwlease >= k->nkr_num_slots ||
598*f9790aebSLuigi Rizzo 		k->nr_hwcur >= k->nkr_num_slots ||
599*f9790aebSLuigi Rizzo 		k->nr_hwavail >= k->nkr_num_slots ||
600*f9790aebSLuigi Rizzo 		k->nkr_lease_idx >= k->nkr_num_slots) {
601*f9790aebSLuigi Rizzo 		D("invalid kring, cur %d avail %d lease %d lease_idx %d lim %d",			k->nr_hwcur, k->nr_hwavail, k->nkr_hwlease,
602*f9790aebSLuigi Rizzo 			k->nkr_lease_idx, k->nkr_num_slots);
603*f9790aebSLuigi Rizzo 	}
604*f9790aebSLuigi Rizzo #endif
605*f9790aebSLuigi Rizzo 	return pos;
606*f9790aebSLuigi Rizzo }
607*f9790aebSLuigi Rizzo 
608ce3ee1e7SLuigi Rizzo 
609ce3ee1e7SLuigi Rizzo /*
610*f9790aebSLuigi Rizzo  * protect against multiple threads using the same ring.
611*f9790aebSLuigi Rizzo  * also check that the ring has not been stopped.
612*f9790aebSLuigi Rizzo  * We only care for 0 or !=0 as a return code.
61368b8534bSLuigi Rizzo  */
614*f9790aebSLuigi Rizzo #define NM_KR_BUSY	1
615*f9790aebSLuigi Rizzo #define NM_KR_STOPPED	2
61668b8534bSLuigi Rizzo 
617*f9790aebSLuigi Rizzo static __inline void nm_kr_put(struct netmap_kring *kr)
618*f9790aebSLuigi Rizzo {
619*f9790aebSLuigi Rizzo 	NM_ATOMIC_CLEAR(&kr->nr_busy);
620*f9790aebSLuigi Rizzo }
621*f9790aebSLuigi Rizzo 
622*f9790aebSLuigi Rizzo static __inline int nm_kr_tryget(struct netmap_kring *kr)
623*f9790aebSLuigi Rizzo {
624*f9790aebSLuigi Rizzo 	/* check a first time without taking the lock
625*f9790aebSLuigi Rizzo 	 * to avoid starvation for nm_kr_get()
626*f9790aebSLuigi Rizzo 	 */
627*f9790aebSLuigi Rizzo 	if (unlikely(kr->nkr_stopped)) {
628*f9790aebSLuigi Rizzo 		ND("ring %p stopped (%d)", kr, kr->nkr_stopped);
629*f9790aebSLuigi Rizzo 		return NM_KR_STOPPED;
630*f9790aebSLuigi Rizzo 	}
631*f9790aebSLuigi Rizzo 	if (unlikely(NM_ATOMIC_TEST_AND_SET(&kr->nr_busy)))
632*f9790aebSLuigi Rizzo 		return NM_KR_BUSY;
633*f9790aebSLuigi Rizzo 	/* check a second time with lock held */
634*f9790aebSLuigi Rizzo 	if (unlikely(kr->nkr_stopped)) {
635*f9790aebSLuigi Rizzo 		ND("ring %p stopped (%d)", kr, kr->nkr_stopped);
636*f9790aebSLuigi Rizzo 		nm_kr_put(kr);
637*f9790aebSLuigi Rizzo 		return NM_KR_STOPPED;
638*f9790aebSLuigi Rizzo 	}
639*f9790aebSLuigi Rizzo 	return 0;
640*f9790aebSLuigi Rizzo }
64168b8534bSLuigi Rizzo 
642849bec0eSLuigi Rizzo 
64368b8534bSLuigi Rizzo /*
64468b8534bSLuigi Rizzo  * The following are support routines used by individual drivers to
64568b8534bSLuigi Rizzo  * support netmap operation.
64668b8534bSLuigi Rizzo  *
64768b8534bSLuigi Rizzo  * netmap_attach() initializes a struct netmap_adapter, allocating the
64868b8534bSLuigi Rizzo  * 	struct netmap_ring's and the struct selinfo.
64968b8534bSLuigi Rizzo  *
65068b8534bSLuigi Rizzo  * netmap_detach() frees the memory allocated by netmap_attach().
65168b8534bSLuigi Rizzo  *
652ce3ee1e7SLuigi Rizzo  * netmap_transmit() replaces the if_transmit routine of the interface,
65368b8534bSLuigi Rizzo  *	and is used to intercept packets coming from the stack.
65468b8534bSLuigi Rizzo  *
65568b8534bSLuigi Rizzo  * netmap_load_map/netmap_reload_map are helper routines to set/reset
65668b8534bSLuigi Rizzo  *	the dmamap for a packet buffer
65768b8534bSLuigi Rizzo  *
65868b8534bSLuigi Rizzo  * netmap_reset() is a helper routine to be called in the driver
65968b8534bSLuigi Rizzo  *	when reinitializing a ring.
66068b8534bSLuigi Rizzo  */
661*f9790aebSLuigi Rizzo int netmap_attach(struct netmap_adapter *);
662*f9790aebSLuigi Rizzo int netmap_attach_common(struct netmap_adapter *);
663*f9790aebSLuigi Rizzo void netmap_detach_common(struct netmap_adapter *na);
66468b8534bSLuigi Rizzo void netmap_detach(struct ifnet *);
665ce3ee1e7SLuigi Rizzo int netmap_transmit(struct ifnet *, struct mbuf *);
66668b8534bSLuigi Rizzo struct netmap_slot *netmap_reset(struct netmap_adapter *na,
667ce3ee1e7SLuigi Rizzo 	enum txrx tx, u_int n, u_int new_cur);
66868b8534bSLuigi Rizzo int netmap_ring_reinit(struct netmap_kring *);
66968b8534bSLuigi Rizzo 
670*f9790aebSLuigi Rizzo /* set/clear native flags. XXX maybe also if_transmit ? */
671*f9790aebSLuigi Rizzo static inline void
672*f9790aebSLuigi Rizzo nm_set_native_flags(struct netmap_adapter *na)
673*f9790aebSLuigi Rizzo {
674*f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
675ce3ee1e7SLuigi Rizzo 
676*f9790aebSLuigi Rizzo 	na->na_flags |= (NAF_NATIVE_ON | NAF_NETMAP_ON);
677*f9790aebSLuigi Rizzo #ifdef IFCAP_NETMAP /* or FreeBSD ? */
678*f9790aebSLuigi Rizzo 	ifp->if_capenable |= IFCAP_NETMAP;
679*f9790aebSLuigi Rizzo #endif
680*f9790aebSLuigi Rizzo #ifdef __FreeBSD__
681*f9790aebSLuigi Rizzo 	na->if_transmit = ifp->if_transmit;
682*f9790aebSLuigi Rizzo 	ifp->if_transmit = netmap_transmit;
683*f9790aebSLuigi Rizzo #else
684*f9790aebSLuigi Rizzo 	na->if_transmit = (void *)ifp->netdev_ops;
685*f9790aebSLuigi Rizzo 	ifp->netdev_ops = &((struct netmap_hw_adapter *)na)->nm_ndo;
686*f9790aebSLuigi Rizzo #endif
687*f9790aebSLuigi Rizzo }
688*f9790aebSLuigi Rizzo 
689*f9790aebSLuigi Rizzo static inline void
690*f9790aebSLuigi Rizzo nm_clear_native_flags(struct netmap_adapter *na)
691*f9790aebSLuigi Rizzo {
692*f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
693*f9790aebSLuigi Rizzo 
694*f9790aebSLuigi Rizzo #ifdef __FreeBSD__
695*f9790aebSLuigi Rizzo 	ifp->if_transmit = na->if_transmit;
696*f9790aebSLuigi Rizzo #else
697*f9790aebSLuigi Rizzo 	ifp->netdev_ops = (void *)na->if_transmit;
698*f9790aebSLuigi Rizzo #endif
699*f9790aebSLuigi Rizzo 	na->na_flags &= ~(NAF_NATIVE_ON | NAF_NETMAP_ON);
700*f9790aebSLuigi Rizzo #ifdef IFCAP_NETMAP /* or FreeBSD ? */
701*f9790aebSLuigi Rizzo 	ifp->if_capenable &= ~IFCAP_NETMAP;
702*f9790aebSLuigi Rizzo #endif
703*f9790aebSLuigi Rizzo }
704*f9790aebSLuigi Rizzo 
705*f9790aebSLuigi Rizzo /*
706*f9790aebSLuigi Rizzo  * validates parameters in the ring/kring, returns a value for cur,
707*f9790aebSLuigi Rizzo  * and the 'new_slots' value in the argument.
708*f9790aebSLuigi Rizzo  * If any error, returns cur > lim to force a reinit.
709*f9790aebSLuigi Rizzo  */
710*f9790aebSLuigi Rizzo u_int nm_txsync_prologue(struct netmap_kring *, u_int *);
711*f9790aebSLuigi Rizzo 
712*f9790aebSLuigi Rizzo /*
713*f9790aebSLuigi Rizzo  * validates parameters in the ring/kring, returns a value for cur,
714*f9790aebSLuigi Rizzo  * and the 'reserved' value in the argument.
715*f9790aebSLuigi Rizzo  * If any error, returns cur > lim to force a reinit.
716*f9790aebSLuigi Rizzo  */
717*f9790aebSLuigi Rizzo u_int nm_rxsync_prologue(struct netmap_kring *, u_int *);
718*f9790aebSLuigi Rizzo 
719*f9790aebSLuigi Rizzo /*
720*f9790aebSLuigi Rizzo  * update kring and ring at the end of txsync
721*f9790aebSLuigi Rizzo  */
722*f9790aebSLuigi Rizzo static inline void
723*f9790aebSLuigi Rizzo nm_txsync_finalize(struct netmap_kring *kring, u_int cur)
724*f9790aebSLuigi Rizzo {
725*f9790aebSLuigi Rizzo 	/* recompute hwreserved */
726*f9790aebSLuigi Rizzo 	kring->nr_hwreserved = cur - kring->nr_hwcur;
727*f9790aebSLuigi Rizzo 	if (kring->nr_hwreserved < 0)
728*f9790aebSLuigi Rizzo 		kring->nr_hwreserved += kring->nkr_num_slots;
729*f9790aebSLuigi Rizzo 
730*f9790aebSLuigi Rizzo 	/* update avail and reserved to what the kernel knows */
731*f9790aebSLuigi Rizzo 	kring->ring->avail = kring->nr_hwavail;
732*f9790aebSLuigi Rizzo 	kring->ring->reserved = kring->nr_hwreserved;
733*f9790aebSLuigi Rizzo }
734*f9790aebSLuigi Rizzo 
735*f9790aebSLuigi Rizzo /* check/fix address and len in tx rings */
736*f9790aebSLuigi Rizzo #if 1 /* debug version */
737*f9790aebSLuigi Rizzo #define	NM_CHECK_ADDR_LEN(_a, _l)	do {				\
738*f9790aebSLuigi Rizzo 	if (_a == netmap_buffer_base || _l > NETMAP_BUF_SIZE) {		\
739*f9790aebSLuigi Rizzo 		RD(5, "bad addr/len ring %d slot %d idx %d len %d",	\
740*f9790aebSLuigi Rizzo 			ring_nr, nm_i, slot->buf_idx, len);		\
741*f9790aebSLuigi Rizzo 		if (_l > NETMAP_BUF_SIZE)				\
742*f9790aebSLuigi Rizzo 			_l = NETMAP_BUF_SIZE;				\
743*f9790aebSLuigi Rizzo 	} } while (0)
744*f9790aebSLuigi Rizzo #else /* no debug version */
745*f9790aebSLuigi Rizzo #define	NM_CHECK_ADDR_LEN(_a, _l)	do {				\
746*f9790aebSLuigi Rizzo 		if (_l > NETMAP_BUF_SIZE)				\
747*f9790aebSLuigi Rizzo 			_l = NETMAP_BUF_SIZE;				\
748*f9790aebSLuigi Rizzo 	} while (0)
749*f9790aebSLuigi Rizzo #endif
750*f9790aebSLuigi Rizzo 
751*f9790aebSLuigi Rizzo 
752*f9790aebSLuigi Rizzo /*---------------------------------------------------------------*/
753*f9790aebSLuigi Rizzo /*
754*f9790aebSLuigi Rizzo  * Support routines to be used with the VALE switch
755*f9790aebSLuigi Rizzo  */
756*f9790aebSLuigi Rizzo int netmap_update_config(struct netmap_adapter *na);
757*f9790aebSLuigi Rizzo int netmap_krings_create(struct netmap_adapter *na, u_int ntx, u_int nrx, u_int tailroom);
758*f9790aebSLuigi Rizzo void netmap_krings_delete(struct netmap_adapter *na);
759*f9790aebSLuigi Rizzo 
760*f9790aebSLuigi Rizzo struct netmap_if *
761*f9790aebSLuigi Rizzo netmap_do_regif(struct netmap_priv_d *priv, struct netmap_adapter *na,
762*f9790aebSLuigi Rizzo 	uint16_t ringid, int *err);
763*f9790aebSLuigi Rizzo 
764*f9790aebSLuigi Rizzo 
765*f9790aebSLuigi Rizzo 
766*f9790aebSLuigi Rizzo u_int nm_bound_var(u_int *v, u_int dflt, u_int lo, u_int hi, const char *msg);
767*f9790aebSLuigi Rizzo int netmap_get_na(struct nmreq *nmr, struct netmap_adapter **na, int create);
768*f9790aebSLuigi Rizzo int netmap_get_hw_na(struct ifnet *ifp, struct netmap_adapter **na);
769*f9790aebSLuigi Rizzo 
770*f9790aebSLuigi Rizzo #ifdef WITH_VALE
771f18be576SLuigi Rizzo /*
772f18be576SLuigi Rizzo  * The following bridge-related interfaces are used by other kernel modules
773f18be576SLuigi Rizzo  * In the version that only supports unicast or broadcast, the lookup
774f18be576SLuigi Rizzo  * function can return 0 .. NM_BDG_MAXPORTS-1 for regular ports,
775f18be576SLuigi Rizzo  * NM_BDG_MAXPORTS for broadcast, NM_BDG_MAXPORTS+1 for unknown.
776f18be576SLuigi Rizzo  * XXX in practice "unknown" might be handled same as broadcast.
777f18be576SLuigi Rizzo  */
778*f9790aebSLuigi Rizzo typedef u_int (*bdg_lookup_fn_t)(char *buf, u_int len,
779*f9790aebSLuigi Rizzo 		uint8_t *ring_nr, struct netmap_vp_adapter *);
780*f9790aebSLuigi Rizzo u_int netmap_bdg_learning(char *, u_int, uint8_t *,
781*f9790aebSLuigi Rizzo 		struct netmap_vp_adapter *);
782*f9790aebSLuigi Rizzo 
783*f9790aebSLuigi Rizzo #define	NM_BDG_MAXPORTS		254	/* up to 254 */
784f18be576SLuigi Rizzo #define	NM_BDG_BROADCAST	NM_BDG_MAXPORTS
785f18be576SLuigi Rizzo #define	NM_BDG_NOPORT		(NM_BDG_MAXPORTS+1)
786f18be576SLuigi Rizzo 
787*f9790aebSLuigi Rizzo #define	NM_NAME			"vale"	/* prefix for bridge port name */
788*f9790aebSLuigi Rizzo 
789*f9790aebSLuigi Rizzo 
790*f9790aebSLuigi Rizzo /* these are redefined in case of no VALE support */
791*f9790aebSLuigi Rizzo int netmap_get_bdg_na(struct nmreq *nmr, struct netmap_adapter **na, int create);
792*f9790aebSLuigi Rizzo void netmap_init_bridges(void);
793*f9790aebSLuigi Rizzo int netmap_bdg_ctl(struct nmreq *nmr, bdg_lookup_fn_t func);
794*f9790aebSLuigi Rizzo 
795*f9790aebSLuigi Rizzo #else /* !WITH_VALE */
796*f9790aebSLuigi Rizzo #define	netmap_get_bdg_na(_1, _2, _3)	0
797*f9790aebSLuigi Rizzo #define netmap_init_bridges(_1)
798*f9790aebSLuigi Rizzo #define	netmap_bdg_ctl(_1, _2)	EINVAL
799*f9790aebSLuigi Rizzo #endif /* !WITH_VALE */
800*f9790aebSLuigi Rizzo 
801*f9790aebSLuigi Rizzo /* Various prototypes */
802*f9790aebSLuigi Rizzo int netmap_poll(struct cdev *dev, int events, struct thread *td);
803*f9790aebSLuigi Rizzo 
804*f9790aebSLuigi Rizzo 
805*f9790aebSLuigi Rizzo int netmap_init(void);
806*f9790aebSLuigi Rizzo void netmap_fini(void);
807*f9790aebSLuigi Rizzo int netmap_get_memory(struct netmap_priv_d* p);
808*f9790aebSLuigi Rizzo void netmap_dtor(void *data);
809*f9790aebSLuigi Rizzo int netmap_dtor_locked(struct netmap_priv_d *priv);
810*f9790aebSLuigi Rizzo 
811*f9790aebSLuigi Rizzo int netmap_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td);
812*f9790aebSLuigi Rizzo 
813*f9790aebSLuigi Rizzo /* netmap_adapter creation/destruction */
814*f9790aebSLuigi Rizzo #define NM_IFPNAME(ifp) ((ifp) ? (ifp)->if_xname : "zombie")
815*f9790aebSLuigi Rizzo #define NM_DEBUG_PUTGET 1
816*f9790aebSLuigi Rizzo 
817*f9790aebSLuigi Rizzo #ifdef NM_DEBUG_PUTGET
818*f9790aebSLuigi Rizzo 
819*f9790aebSLuigi Rizzo #define NM_DBG(f) __##f
820*f9790aebSLuigi Rizzo 
821*f9790aebSLuigi Rizzo void __netmap_adapter_get(struct netmap_adapter *na);
822*f9790aebSLuigi Rizzo 
823*f9790aebSLuigi Rizzo #define netmap_adapter_get(na) 				\
824*f9790aebSLuigi Rizzo 	do {						\
825*f9790aebSLuigi Rizzo 		struct netmap_adapter *__na = na;	\
826*f9790aebSLuigi Rizzo 		D("getting %p:%s (%d)", __na, NM_IFPNAME(__na->ifp), __na->na_refcount);	\
827*f9790aebSLuigi Rizzo 		__netmap_adapter_get(__na);		\
828*f9790aebSLuigi Rizzo 	} while (0)
829*f9790aebSLuigi Rizzo 
830*f9790aebSLuigi Rizzo int __netmap_adapter_put(struct netmap_adapter *na);
831*f9790aebSLuigi Rizzo 
832*f9790aebSLuigi Rizzo #define netmap_adapter_put(na)				\
833*f9790aebSLuigi Rizzo 	do {						\
834*f9790aebSLuigi Rizzo 		struct netmap_adapter *__na = na;	\
835*f9790aebSLuigi Rizzo 		D("putting %p:%s (%d)", __na, NM_IFPNAME(__na->ifp), __na->na_refcount);	\
836*f9790aebSLuigi Rizzo 		__netmap_adapter_put(__na);		\
837*f9790aebSLuigi Rizzo 	} while (0)
838*f9790aebSLuigi Rizzo 
839*f9790aebSLuigi Rizzo #else /* !NM_DEBUG_PUTGET */
840*f9790aebSLuigi Rizzo 
841*f9790aebSLuigi Rizzo #define NM_DBG(f) f
842*f9790aebSLuigi Rizzo void netmap_adapter_get(struct netmap_adapter *na);
843*f9790aebSLuigi Rizzo int netmap_adapter_put(struct netmap_adapter *na);
844*f9790aebSLuigi Rizzo 
845*f9790aebSLuigi Rizzo #endif /* !NM_DEBUG_PUTGET */
846*f9790aebSLuigi Rizzo 
847*f9790aebSLuigi Rizzo 
848b3d53016SLuigi Rizzo extern u_int netmap_buf_size;
849849bec0eSLuigi Rizzo #define NETMAP_BUF_SIZE	netmap_buf_size	// XXX remove
8502157a17cSLuigi Rizzo extern int netmap_mitigate;
8515819da83SLuigi Rizzo extern int netmap_no_pendintr;
85268b8534bSLuigi Rizzo extern u_int netmap_total_buffers;
85368b8534bSLuigi Rizzo extern char *netmap_buffer_base;
85468b8534bSLuigi Rizzo extern int netmap_verbose;	// XXX debugging
85568b8534bSLuigi Rizzo enum {                                  /* verbose flags */
85668b8534bSLuigi Rizzo 	NM_VERB_ON = 1,                 /* generic verbose */
85768b8534bSLuigi Rizzo 	NM_VERB_HOST = 0x2,             /* verbose host stack */
85868b8534bSLuigi Rizzo 	NM_VERB_RXSYNC = 0x10,          /* verbose on rxsync/txsync */
85968b8534bSLuigi Rizzo 	NM_VERB_TXSYNC = 0x20,
86068b8534bSLuigi Rizzo 	NM_VERB_RXINTR = 0x100,         /* verbose on rx/tx intr (driver) */
86168b8534bSLuigi Rizzo 	NM_VERB_TXINTR = 0x200,
86268b8534bSLuigi Rizzo 	NM_VERB_NIC_RXSYNC = 0x1000,    /* verbose on rx/tx intr (driver) */
86368b8534bSLuigi Rizzo 	NM_VERB_NIC_TXSYNC = 0x2000,
86468b8534bSLuigi Rizzo };
86568b8534bSLuigi Rizzo 
866*f9790aebSLuigi Rizzo extern int netmap_txsync_retry;
867*f9790aebSLuigi Rizzo extern int netmap_generic_mit;
868*f9790aebSLuigi Rizzo extern int netmap_generic_ringsize;
869*f9790aebSLuigi Rizzo 
87068b8534bSLuigi Rizzo /*
871d0c7b075SLuigi Rizzo  * NA returns a pointer to the struct netmap adapter from the ifp,
872d0c7b075SLuigi Rizzo  * WNA is used to write it.
87368b8534bSLuigi Rizzo  */
874d0c7b075SLuigi Rizzo #ifndef WNA
875d0c7b075SLuigi Rizzo #define	WNA(_ifp)	(_ifp)->if_pspare[0]
876d0c7b075SLuigi Rizzo #endif
877d0c7b075SLuigi Rizzo #define	NA(_ifp)	((struct netmap_adapter *)WNA(_ifp))
87868b8534bSLuigi Rizzo 
8798241616dSLuigi Rizzo /*
8808241616dSLuigi Rizzo  * Macros to determine if an interface is netmap capable or netmap enabled.
8818241616dSLuigi Rizzo  * See the magic field in struct netmap_adapter.
8828241616dSLuigi Rizzo  */
8838241616dSLuigi Rizzo #ifdef __FreeBSD__
8848241616dSLuigi Rizzo /*
8858241616dSLuigi Rizzo  * on FreeBSD just use if_capabilities and if_capenable.
8868241616dSLuigi Rizzo  */
8878241616dSLuigi Rizzo #define NETMAP_CAPABLE(ifp)	(NA(ifp) &&		\
8888241616dSLuigi Rizzo 	(ifp)->if_capabilities & IFCAP_NETMAP )
8898241616dSLuigi Rizzo 
8908241616dSLuigi Rizzo #define	NETMAP_SET_CAPABLE(ifp)				\
8918241616dSLuigi Rizzo 	(ifp)->if_capabilities |= IFCAP_NETMAP
8928241616dSLuigi Rizzo 
8938241616dSLuigi Rizzo #else	/* linux */
8948241616dSLuigi Rizzo 
8958241616dSLuigi Rizzo /*
8968241616dSLuigi Rizzo  * on linux:
8978241616dSLuigi Rizzo  * we check if NA(ifp) is set and its first element has a related
8988241616dSLuigi Rizzo  * magic value. The capenable is within the struct netmap_adapter.
8998241616dSLuigi Rizzo  */
9008241616dSLuigi Rizzo #define	NETMAP_MAGIC	0x52697a7a
9018241616dSLuigi Rizzo 
9028241616dSLuigi Rizzo #define NETMAP_CAPABLE(ifp)	(NA(ifp) &&		\
9038241616dSLuigi Rizzo 	((uint32_t)(uintptr_t)NA(ifp) ^ NA(ifp)->magic) == NETMAP_MAGIC )
9048241616dSLuigi Rizzo 
9058241616dSLuigi Rizzo #define	NETMAP_SET_CAPABLE(ifp)				\
9068241616dSLuigi Rizzo 	NA(ifp)->magic = ((uint32_t)(uintptr_t)NA(ifp)) ^ NETMAP_MAGIC
9078241616dSLuigi Rizzo 
9088241616dSLuigi Rizzo #endif	/* linux */
90968b8534bSLuigi Rizzo 
910f196ce38SLuigi Rizzo #ifdef __FreeBSD__
911*f9790aebSLuigi Rizzo 
9126dba29a2SLuigi Rizzo /* Callback invoked by the dma machinery after a successfull dmamap_load */
9136dba29a2SLuigi Rizzo static void netmap_dmamap_cb(__unused void *arg,
9146dba29a2SLuigi Rizzo     __unused bus_dma_segment_t * segs, __unused int nseg, __unused int error)
9156dba29a2SLuigi Rizzo {
9166dba29a2SLuigi Rizzo }
9176dba29a2SLuigi Rizzo 
9186dba29a2SLuigi Rizzo /* bus_dmamap_load wrapper: call aforementioned function if map != NULL.
9196dba29a2SLuigi Rizzo  * XXX can we do it without a callback ?
9206dba29a2SLuigi Rizzo  */
9216dba29a2SLuigi Rizzo static inline void
9226dba29a2SLuigi Rizzo netmap_load_map(bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
9236dba29a2SLuigi Rizzo {
9246dba29a2SLuigi Rizzo 	if (map)
9256dba29a2SLuigi Rizzo 		bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE,
9266dba29a2SLuigi Rizzo 		    netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT);
9276dba29a2SLuigi Rizzo }
9286dba29a2SLuigi Rizzo 
9296dba29a2SLuigi Rizzo /* update the map when a buffer changes. */
9306dba29a2SLuigi Rizzo static inline void
9316dba29a2SLuigi Rizzo netmap_reload_map(bus_dma_tag_t tag, bus_dmamap_t map, void *buf)
9326dba29a2SLuigi Rizzo {
9336dba29a2SLuigi Rizzo 	if (map) {
9346dba29a2SLuigi Rizzo 		bus_dmamap_unload(tag, map);
9356dba29a2SLuigi Rizzo 		bus_dmamap_load(tag, map, buf, NETMAP_BUF_SIZE,
9366dba29a2SLuigi Rizzo 		    netmap_dmamap_cb, NULL, BUS_DMA_NOWAIT);
9376dba29a2SLuigi Rizzo 	}
9386dba29a2SLuigi Rizzo }
939*f9790aebSLuigi Rizzo 
940f196ce38SLuigi Rizzo #else /* linux */
941f196ce38SLuigi Rizzo 
942f196ce38SLuigi Rizzo /*
943f196ce38SLuigi Rizzo  * XXX How do we redefine these functions:
944f196ce38SLuigi Rizzo  *
945f196ce38SLuigi Rizzo  * on linux we need
946f196ce38SLuigi Rizzo  *	dma_map_single(&pdev->dev, virt_addr, len, direction)
947f196ce38SLuigi Rizzo  *	dma_unmap_single(&adapter->pdev->dev, phys_addr, len, direction
948f196ce38SLuigi Rizzo  * The len can be implicit (on netmap it is NETMAP_BUF_SIZE)
949f196ce38SLuigi Rizzo  * unfortunately the direction is not, so we need to change
950f196ce38SLuigi Rizzo  * something to have a cross API
951f196ce38SLuigi Rizzo  */
952f196ce38SLuigi Rizzo #define netmap_load_map(_t, _m, _b)
953f196ce38SLuigi Rizzo #define netmap_reload_map(_t, _m, _b)
954f196ce38SLuigi Rizzo #if 0
955f196ce38SLuigi Rizzo 	struct e1000_buffer *buffer_info =  &tx_ring->buffer_info[l];
956f196ce38SLuigi Rizzo 	/* set time_stamp *before* dma to help avoid a possible race */
957f196ce38SLuigi Rizzo 	buffer_info->time_stamp = jiffies;
958f196ce38SLuigi Rizzo 	buffer_info->mapped_as_page = false;
959f196ce38SLuigi Rizzo 	buffer_info->length = len;
960f196ce38SLuigi Rizzo 	//buffer_info->next_to_watch = l;
961f196ce38SLuigi Rizzo 	/* reload dma map */
962f196ce38SLuigi Rizzo 	dma_unmap_single(&adapter->pdev->dev, buffer_info->dma,
963f196ce38SLuigi Rizzo 			NETMAP_BUF_SIZE, DMA_TO_DEVICE);
964f196ce38SLuigi Rizzo 	buffer_info->dma = dma_map_single(&adapter->pdev->dev,
965f196ce38SLuigi Rizzo 			addr, NETMAP_BUF_SIZE, DMA_TO_DEVICE);
966f196ce38SLuigi Rizzo 
967f196ce38SLuigi Rizzo 	if (dma_mapping_error(&adapter->pdev->dev, buffer_info->dma)) {
968f196ce38SLuigi Rizzo 		D("dma mapping error");
969f196ce38SLuigi Rizzo 		/* goto dma_error; See e1000_put_txbuf() */
970f196ce38SLuigi Rizzo 		/* XXX reset */
971f196ce38SLuigi Rizzo 	}
972f196ce38SLuigi Rizzo 	tx_desc->buffer_addr = htole64(buffer_info->dma); //XXX
973f196ce38SLuigi Rizzo 
974f196ce38SLuigi Rizzo #endif
975f196ce38SLuigi Rizzo 
976f196ce38SLuigi Rizzo /*
977f196ce38SLuigi Rizzo  * The bus_dmamap_sync() can be one of wmb() or rmb() depending on direction.
978f196ce38SLuigi Rizzo  */
979f196ce38SLuigi Rizzo #define bus_dmamap_sync(_a, _b, _c)
980f196ce38SLuigi Rizzo 
981f196ce38SLuigi Rizzo #endif /* linux */
9826dba29a2SLuigi Rizzo 
983ce3ee1e7SLuigi Rizzo 
9845644ccecSLuigi Rizzo /*
9855644ccecSLuigi Rizzo  * functions to map NIC to KRING indexes (n2k) and vice versa (k2n)
9865644ccecSLuigi Rizzo  */
9875644ccecSLuigi Rizzo static inline int
98864ae02c3SLuigi Rizzo netmap_idx_n2k(struct netmap_kring *kr, int idx)
9895644ccecSLuigi Rizzo {
99064ae02c3SLuigi Rizzo 	int n = kr->nkr_num_slots;
99164ae02c3SLuigi Rizzo 	idx += kr->nkr_hwofs;
99264ae02c3SLuigi Rizzo 	if (idx < 0)
99364ae02c3SLuigi Rizzo 		return idx + n;
99464ae02c3SLuigi Rizzo 	else if (idx < n)
99564ae02c3SLuigi Rizzo 		return idx;
9965644ccecSLuigi Rizzo 	else
99764ae02c3SLuigi Rizzo 		return idx - n;
9985644ccecSLuigi Rizzo }
9995644ccecSLuigi Rizzo 
10005644ccecSLuigi Rizzo 
10015644ccecSLuigi Rizzo static inline int
100264ae02c3SLuigi Rizzo netmap_idx_k2n(struct netmap_kring *kr, int idx)
10035644ccecSLuigi Rizzo {
100464ae02c3SLuigi Rizzo 	int n = kr->nkr_num_slots;
100564ae02c3SLuigi Rizzo 	idx -= kr->nkr_hwofs;
100664ae02c3SLuigi Rizzo 	if (idx < 0)
100764ae02c3SLuigi Rizzo 		return idx + n;
100864ae02c3SLuigi Rizzo 	else if (idx < n)
100964ae02c3SLuigi Rizzo 		return idx;
10105644ccecSLuigi Rizzo 	else
101164ae02c3SLuigi Rizzo 		return idx - n;
10125644ccecSLuigi Rizzo }
10135644ccecSLuigi Rizzo 
10145644ccecSLuigi Rizzo 
1015d76bf4ffSLuigi Rizzo /* Entries of the look-up table. */
1016d76bf4ffSLuigi Rizzo struct lut_entry {
1017d76bf4ffSLuigi Rizzo 	void *vaddr;		/* virtual address. */
1018849bec0eSLuigi Rizzo 	vm_paddr_t paddr;	/* physical address. */
1019d76bf4ffSLuigi Rizzo };
1020d76bf4ffSLuigi Rizzo 
1021d76bf4ffSLuigi Rizzo struct netmap_obj_pool;
1022d76bf4ffSLuigi Rizzo extern struct lut_entry *netmap_buffer_lut;
1023d76bf4ffSLuigi Rizzo #define NMB_VA(i)	(netmap_buffer_lut[i].vaddr)
1024d76bf4ffSLuigi Rizzo #define NMB_PA(i)	(netmap_buffer_lut[i].paddr)
1025d76bf4ffSLuigi Rizzo 
102668b8534bSLuigi Rizzo /*
10276e10c8b8SLuigi Rizzo  * NMB return the virtual address of a buffer (buffer 0 on bad index)
10286e10c8b8SLuigi Rizzo  * PNMB also fills the physical address
102968b8534bSLuigi Rizzo  */
10306e10c8b8SLuigi Rizzo static inline void *
103168b8534bSLuigi Rizzo NMB(struct netmap_slot *slot)
103268b8534bSLuigi Rizzo {
103368b8534bSLuigi Rizzo 	uint32_t i = slot->buf_idx;
1034f196ce38SLuigi Rizzo 	return (unlikely(i >= netmap_total_buffers)) ?  NMB_VA(0) : NMB_VA(i);
103568b8534bSLuigi Rizzo }
103668b8534bSLuigi Rizzo 
10376e10c8b8SLuigi Rizzo static inline void *
10386e10c8b8SLuigi Rizzo PNMB(struct netmap_slot *slot, uint64_t *pp)
10396e10c8b8SLuigi Rizzo {
10406e10c8b8SLuigi Rizzo 	uint32_t i = slot->buf_idx;
1041d76bf4ffSLuigi Rizzo 	void *ret = (i >= netmap_total_buffers) ? NMB_VA(0) : NMB_VA(i);
10422579e2d7SLuigi Rizzo 
1043d76bf4ffSLuigi Rizzo 	*pp = (i >= netmap_total_buffers) ? NMB_PA(0) : NMB_PA(i);
10446e10c8b8SLuigi Rizzo 	return ret;
10456e10c8b8SLuigi Rizzo }
10466e10c8b8SLuigi Rizzo 
1047*f9790aebSLuigi Rizzo /* Generic version of NMB, which uses device-specific memory. */
1048*f9790aebSLuigi Rizzo static inline void *
1049*f9790aebSLuigi Rizzo BDG_NMB(struct netmap_adapter *na, struct netmap_slot *slot)
1050*f9790aebSLuigi Rizzo {
1051*f9790aebSLuigi Rizzo 	struct lut_entry *lut = na->na_lut;
1052*f9790aebSLuigi Rizzo 	uint32_t i = slot->buf_idx;
1053*f9790aebSLuigi Rizzo 	return (unlikely(i >= na->na_lut_objtotal)) ?
1054*f9790aebSLuigi Rizzo 		lut[0].vaddr : lut[i].vaddr;
1055*f9790aebSLuigi Rizzo }
1056*f9790aebSLuigi Rizzo 
10571a26580eSLuigi Rizzo /* default functions to handle rx/tx interrupts */
1058ce3ee1e7SLuigi Rizzo int netmap_rx_irq(struct ifnet *, u_int, u_int *);
10591a26580eSLuigi Rizzo #define netmap_tx_irq(_n, _q) netmap_rx_irq(_n, _q, NULL)
1060*f9790aebSLuigi Rizzo void netmap_common_irq(struct ifnet *, u_int, u_int *work_done);
1061ce3ee1e7SLuigi Rizzo 
1062ce3ee1e7SLuigi Rizzo 
1063*f9790aebSLuigi Rizzo void netmap_txsync_to_host(struct netmap_adapter *na);
1064ce3ee1e7SLuigi Rizzo void netmap_disable_all_rings(struct ifnet *);
1065ce3ee1e7SLuigi Rizzo void netmap_enable_all_rings(struct ifnet *);
1066*f9790aebSLuigi Rizzo void netmap_disable_ring(struct netmap_kring *kr);
1067*f9790aebSLuigi Rizzo 
1068*f9790aebSLuigi Rizzo 
1069*f9790aebSLuigi Rizzo /* Structure associated to each thread which registered an interface.
1070*f9790aebSLuigi Rizzo  *
1071*f9790aebSLuigi Rizzo  * The first 4 fields of this structure are written by NIOCREGIF and
1072*f9790aebSLuigi Rizzo  * read by poll() and NIOC?XSYNC.
1073*f9790aebSLuigi Rizzo  * There is low contention among writers (actually, a correct user program
1074*f9790aebSLuigi Rizzo  * should have no contention among writers) and among writers and readers,
1075*f9790aebSLuigi Rizzo  * so we use a single global lock to protect the structure initialization.
1076*f9790aebSLuigi Rizzo  * Since initialization involves the allocation of memory, we reuse the memory
1077*f9790aebSLuigi Rizzo  * allocator lock.
1078*f9790aebSLuigi Rizzo  * Read access to the structure is lock free. Readers must check that
1079*f9790aebSLuigi Rizzo  * np_nifp is not NULL before using the other fields.
1080*f9790aebSLuigi Rizzo  * If np_nifp is NULL initialization has not been performed, so they should
1081*f9790aebSLuigi Rizzo  * return an error to userlevel.
1082*f9790aebSLuigi Rizzo  *
1083*f9790aebSLuigi Rizzo  * The ref_done field is used to regulate access to the refcount in the
1084*f9790aebSLuigi Rizzo  * memory allocator. The refcount must be incremented at most once for
1085*f9790aebSLuigi Rizzo  * each open("/dev/netmap"). The increment is performed by the first
1086*f9790aebSLuigi Rizzo  * function that calls netmap_get_memory() (currently called by
1087*f9790aebSLuigi Rizzo  * mmap(), NIOCGINFO and NIOCREGIF).
1088*f9790aebSLuigi Rizzo  * If the refcount is incremented, it is then decremented when the
1089*f9790aebSLuigi Rizzo  * private structure is destroyed.
1090*f9790aebSLuigi Rizzo  */
1091*f9790aebSLuigi Rizzo struct netmap_priv_d {
1092*f9790aebSLuigi Rizzo 	struct netmap_if * volatile np_nifp;	/* netmap if descriptor. */
1093*f9790aebSLuigi Rizzo 
1094*f9790aebSLuigi Rizzo 	struct netmap_adapter	*np_na;
1095*f9790aebSLuigi Rizzo 	int		        np_ringid;	/* from the ioctl */
1096*f9790aebSLuigi Rizzo 	u_int		        np_qfirst, np_qlast;	/* range of rings to scan */
1097*f9790aebSLuigi Rizzo 	uint16_t	        np_txpoll;
1098*f9790aebSLuigi Rizzo 
1099*f9790aebSLuigi Rizzo 	struct netmap_mem_d     *np_mref;	/* use with NMG_LOCK held */
1100*f9790aebSLuigi Rizzo 	/* np_refcount is only used on FreeBSD */
1101*f9790aebSLuigi Rizzo 	int		        np_refcount;	/* use with NMG_LOCK held */
1102*f9790aebSLuigi Rizzo };
1103*f9790aebSLuigi Rizzo 
1104*f9790aebSLuigi Rizzo 
1105*f9790aebSLuigi Rizzo /*
1106*f9790aebSLuigi Rizzo  * generic netmap emulation for devices that do not have
1107*f9790aebSLuigi Rizzo  * native netmap support.
1108*f9790aebSLuigi Rizzo  * XXX generic_netmap_register() is only exported to implement
1109*f9790aebSLuigi Rizzo  *	nma_is_generic().
1110*f9790aebSLuigi Rizzo  */
1111*f9790aebSLuigi Rizzo int generic_netmap_register(struct netmap_adapter *na, int enable);
1112*f9790aebSLuigi Rizzo int generic_netmap_attach(struct ifnet *ifp);
1113*f9790aebSLuigi Rizzo 
1114*f9790aebSLuigi Rizzo int netmap_catch_rx(struct netmap_adapter *na, int intercept);
1115*f9790aebSLuigi Rizzo void generic_rx_handler(struct ifnet *ifp, struct mbuf *m);;
1116*f9790aebSLuigi Rizzo void netmap_catch_packet_steering(struct netmap_generic_adapter *na, int enable);
1117*f9790aebSLuigi Rizzo int generic_xmit_frame(struct ifnet *ifp, struct mbuf *m, void *addr, u_int len, u_int ring_nr);
1118*f9790aebSLuigi Rizzo int generic_find_num_desc(struct ifnet *ifp, u_int *tx, u_int *rx);
1119*f9790aebSLuigi Rizzo void generic_find_num_queues(struct ifnet *ifp, u_int *txq, u_int *rxq);
1120*f9790aebSLuigi Rizzo 
1121*f9790aebSLuigi Rizzo static __inline int
1122*f9790aebSLuigi Rizzo nma_is_generic(struct netmap_adapter *na)
1123*f9790aebSLuigi Rizzo {
1124*f9790aebSLuigi Rizzo 	return na->nm_register == generic_netmap_register;
1125*f9790aebSLuigi Rizzo }
1126*f9790aebSLuigi Rizzo 
1127*f9790aebSLuigi Rizzo /*
1128*f9790aebSLuigi Rizzo  * netmap_mitigation API. This is used by the generic adapter
1129*f9790aebSLuigi Rizzo  * to reduce the number of interrupt requests/selwakeup
1130*f9790aebSLuigi Rizzo  * to clients on incoming packets.
1131*f9790aebSLuigi Rizzo  */
1132*f9790aebSLuigi Rizzo void netmap_mitigation_init(struct netmap_generic_adapter *na);
1133*f9790aebSLuigi Rizzo void netmap_mitigation_start(struct netmap_generic_adapter *na);
1134*f9790aebSLuigi Rizzo void netmap_mitigation_restart(struct netmap_generic_adapter *na);
1135*f9790aebSLuigi Rizzo int netmap_mitigation_active(struct netmap_generic_adapter *na);
1136*f9790aebSLuigi Rizzo void netmap_mitigation_cleanup(struct netmap_generic_adapter *na);
1137*f9790aebSLuigi Rizzo 
1138*f9790aebSLuigi Rizzo // int generic_timer_handler(struct hrtimer *t);
1139ce3ee1e7SLuigi Rizzo 
114068b8534bSLuigi Rizzo #endif /* _NET_NETMAP_KERN_H_ */
1141