1718cf2ccSPedro F. Giffuni /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3718cf2ccSPedro F. Giffuni  *
417885a7bSLuigi Rizzo  * Copyright (C) 2013-2014 Universita` di Pisa. All rights reserved.
5f9790aebSLuigi Rizzo  *
6f9790aebSLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
7f9790aebSLuigi Rizzo  * modification, are permitted provided that the following conditions
8f9790aebSLuigi Rizzo  * are met:
9f9790aebSLuigi Rizzo  *   1. Redistributions of source code must retain the above copyright
10f9790aebSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer.
11f9790aebSLuigi Rizzo  *   2. Redistributions in binary form must reproduce the above copyright
12f9790aebSLuigi Rizzo  *      notice, this list of conditions and the following disclaimer in the
13f9790aebSLuigi Rizzo  *      documentation and/or other materials provided with the distribution.
14f9790aebSLuigi Rizzo  *
15f9790aebSLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16f9790aebSLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17f9790aebSLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18f9790aebSLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19f9790aebSLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20f9790aebSLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21f9790aebSLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22f9790aebSLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23f9790aebSLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24f9790aebSLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25f9790aebSLuigi Rizzo  * SUCH DAMAGE.
26f9790aebSLuigi Rizzo  */
27f9790aebSLuigi Rizzo 
28f9790aebSLuigi Rizzo /* $FreeBSD$ */
29847bf383SLuigi Rizzo #include "opt_inet.h"
30847bf383SLuigi Rizzo #include "opt_inet6.h"
31f9790aebSLuigi Rizzo 
32ffaa5debSSepherosa Ziehau #include <sys/param.h>
33f9790aebSLuigi Rizzo #include <sys/module.h>
34f9790aebSLuigi Rizzo #include <sys/errno.h>
35ffaa5debSSepherosa Ziehau #include <sys/jail.h>
36f0ea3689SLuigi Rizzo #include <sys/poll.h>  /* POLLIN, POLLOUT */
37f9790aebSLuigi Rizzo #include <sys/kernel.h> /* types used in module initialization */
3837e3a6d3SLuigi Rizzo #include <sys/conf.h>	/* DEV_MODULE_ORDERED */
39f0ea3689SLuigi Rizzo #include <sys/endian.h>
4037e3a6d3SLuigi Rizzo #include <sys/syscallsubr.h> /* kern_ioctl() */
41f9790aebSLuigi Rizzo 
42f9790aebSLuigi Rizzo #include <sys/rwlock.h>
43f9790aebSLuigi Rizzo 
44f9790aebSLuigi Rizzo #include <vm/vm.h>      /* vtophys */
45f9790aebSLuigi Rizzo #include <vm/pmap.h>    /* vtophys */
46f9790aebSLuigi Rizzo #include <vm/vm_param.h>
47f9790aebSLuigi Rizzo #include <vm/vm_object.h>
48f9790aebSLuigi Rizzo #include <vm/vm_page.h>
49f9790aebSLuigi Rizzo #include <vm/vm_pager.h>
50f9790aebSLuigi Rizzo #include <vm/uma.h>
51f9790aebSLuigi Rizzo 
52f9790aebSLuigi Rizzo 
53f9790aebSLuigi Rizzo #include <sys/malloc.h>
54f9790aebSLuigi Rizzo #include <sys/socket.h> /* sockaddrs */
55f9790aebSLuigi Rizzo #include <sys/selinfo.h>
5637e3a6d3SLuigi Rizzo #include <sys/kthread.h> /* kthread_add() */
5737e3a6d3SLuigi Rizzo #include <sys/proc.h> /* PROC_LOCK() */
5837e3a6d3SLuigi Rizzo #include <sys/unistd.h> /* RFNOWAIT */
5937e3a6d3SLuigi Rizzo #include <sys/sched.h> /* sched_bind() */
6037e3a6d3SLuigi Rizzo #include <sys/smp.h> /* mp_maxid */
61e8cc65cdSVincenzo Maffione #include <sys/taskqueue.h> /* taskqueue_enqueue(), taskqueue_create(), ... */
62f9790aebSLuigi Rizzo #include <net/if.h>
63f9790aebSLuigi Rizzo #include <net/if_var.h>
644bf50f18SLuigi Rizzo #include <net/if_types.h> /* IFT_ETHER */
654bf50f18SLuigi Rizzo #include <net/ethernet.h> /* ether_ifdetach */
664bf50f18SLuigi Rizzo #include <net/if_dl.h> /* LLADDR */
67f9790aebSLuigi Rizzo #include <machine/bus.h>        /* bus_dmamap_* */
68f0ea3689SLuigi Rizzo #include <netinet/in.h>		/* in6_cksum_pseudo() */
69f0ea3689SLuigi Rizzo #include <machine/in_cksum.h>  /* in_pseudo(), in_cksum_hdr() */
70f9790aebSLuigi Rizzo 
71f9790aebSLuigi Rizzo #include <net/netmap.h>
72f9790aebSLuigi Rizzo #include <dev/netmap/netmap_kern.h>
7337e3a6d3SLuigi Rizzo #include <net/netmap_virt.h>
74f9790aebSLuigi Rizzo #include <dev/netmap/netmap_mem2.h>
75f9790aebSLuigi Rizzo 
76f9790aebSLuigi Rizzo 
77f9790aebSLuigi Rizzo /* ======================== FREEBSD-SPECIFIC ROUTINES ================== */
78f9790aebSLuigi Rizzo 
79e8cc65cdSVincenzo Maffione static void
nm_kqueue_notify(void * opaque,int pending)80e8cc65cdSVincenzo Maffione nm_kqueue_notify(void *opaque, int pending)
81e8cc65cdSVincenzo Maffione {
82e8cc65cdSVincenzo Maffione 	struct nm_selinfo *si = opaque;
83e8cc65cdSVincenzo Maffione 
84e8cc65cdSVincenzo Maffione 	/* We use a non-zero hint to distinguish this notification call
85e8cc65cdSVincenzo Maffione 	 * from the call done in kqueue_scan(), which uses hint=0.
86e8cc65cdSVincenzo Maffione 	 */
87e8cc65cdSVincenzo Maffione 	KNOTE_UNLOCKED(&si->si.si_note, /*hint=*/0x100);
88e8cc65cdSVincenzo Maffione }
89e8cc65cdSVincenzo Maffione 
nm_os_selinfo_init(NM_SELINFO_T * si,const char * name)90e8cc65cdSVincenzo Maffione int nm_os_selinfo_init(NM_SELINFO_T *si, const char *name) {
91e8cc65cdSVincenzo Maffione 	int err;
92e8cc65cdSVincenzo Maffione 
93e8cc65cdSVincenzo Maffione 	TASK_INIT(&si->ntfytask, 0, nm_kqueue_notify, si);
94e8cc65cdSVincenzo Maffione 	si->ntfytq = taskqueue_create(name, M_NOWAIT,
95e8cc65cdSVincenzo Maffione 	    taskqueue_thread_enqueue, &si->ntfytq);
96e8cc65cdSVincenzo Maffione 	if (si->ntfytq == NULL)
97e8cc65cdSVincenzo Maffione 		return -ENOMEM;
98e8cc65cdSVincenzo Maffione 	err = taskqueue_start_threads(&si->ntfytq, 1, PI_NET, "tq %s", name);
99e8cc65cdSVincenzo Maffione 	if (err) {
100e8cc65cdSVincenzo Maffione 		taskqueue_free(si->ntfytq);
101e8cc65cdSVincenzo Maffione 		si->ntfytq = NULL;
102e8cc65cdSVincenzo Maffione 		return err;
103e8cc65cdSVincenzo Maffione 	}
104e8cc65cdSVincenzo Maffione 
105e8cc65cdSVincenzo Maffione 	snprintf(si->mtxname, sizeof(si->mtxname), "nmkl%s", name);
106e8cc65cdSVincenzo Maffione 	mtx_init(&si->m, si->mtxname, NULL, MTX_DEF);
107e8cc65cdSVincenzo Maffione 	knlist_init_mtx(&si->si.si_note, &si->m);
108c328e25cSVincenzo Maffione 	si->kqueue_users = 0;
109e8cc65cdSVincenzo Maffione 
110e8cc65cdSVincenzo Maffione 	return (0);
11137e3a6d3SLuigi Rizzo }
11237e3a6d3SLuigi Rizzo 
11337e3a6d3SLuigi Rizzo void
nm_os_selinfo_uninit(NM_SELINFO_T * si)11437e3a6d3SLuigi Rizzo nm_os_selinfo_uninit(NM_SELINFO_T *si)
11537e3a6d3SLuigi Rizzo {
116e8cc65cdSVincenzo Maffione 	if (si->ntfytq == NULL) {
117e8cc65cdSVincenzo Maffione 		return;	/* si was not initialized */
118e8cc65cdSVincenzo Maffione 	}
119e8cc65cdSVincenzo Maffione 	taskqueue_drain(si->ntfytq, &si->ntfytask);
120e8cc65cdSVincenzo Maffione 	taskqueue_free(si->ntfytq);
121e8cc65cdSVincenzo Maffione 	si->ntfytq = NULL;
122a6c48544SVincenzo Maffione 	knlist_delete(&si->si.si_note, curthread, /*islocked=*/0);
12337e3a6d3SLuigi Rizzo 	knlist_destroy(&si->si.si_note);
12437e3a6d3SLuigi Rizzo 	/* now we don't need the mutex anymore */
12537e3a6d3SLuigi Rizzo 	mtx_destroy(&si->m);
12637e3a6d3SLuigi Rizzo }
12737e3a6d3SLuigi Rizzo 
128c3e9b4dbSLuiz Otavio O Souza void *
nm_os_malloc(size_t size)129c3e9b4dbSLuiz Otavio O Souza nm_os_malloc(size_t size)
130c3e9b4dbSLuiz Otavio O Souza {
131c3e9b4dbSLuiz Otavio O Souza 	return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
132c3e9b4dbSLuiz Otavio O Souza }
133c3e9b4dbSLuiz Otavio O Souza 
134c3e9b4dbSLuiz Otavio O Souza void *
nm_os_realloc(void * addr,size_t new_size,size_t old_size __unused)135c3e9b4dbSLuiz Otavio O Souza nm_os_realloc(void *addr, size_t new_size, size_t old_size __unused)
136c3e9b4dbSLuiz Otavio O Souza {
137c3e9b4dbSLuiz Otavio O Souza 	return realloc(addr, new_size, M_DEVBUF, M_NOWAIT | M_ZERO);
138c3e9b4dbSLuiz Otavio O Souza }
139c3e9b4dbSLuiz Otavio O Souza 
140c3e9b4dbSLuiz Otavio O Souza void
nm_os_free(void * addr)141c3e9b4dbSLuiz Otavio O Souza nm_os_free(void *addr)
142c3e9b4dbSLuiz Otavio O Souza {
143c3e9b4dbSLuiz Otavio O Souza 	free(addr, M_DEVBUF);
144c3e9b4dbSLuiz Otavio O Souza }
145c3e9b4dbSLuiz Otavio O Souza 
14637e3a6d3SLuigi Rizzo void
nm_os_ifnet_lock(void)14737e3a6d3SLuigi Rizzo nm_os_ifnet_lock(void)
14837e3a6d3SLuigi Rizzo {
149869d8878SAdrian Chadd 	IFNET_RLOCK();
15037e3a6d3SLuigi Rizzo }
15137e3a6d3SLuigi Rizzo 
15237e3a6d3SLuigi Rizzo void
nm_os_ifnet_unlock(void)15337e3a6d3SLuigi Rizzo nm_os_ifnet_unlock(void)
15437e3a6d3SLuigi Rizzo {
15567ca1051SAdrian Chadd 	IFNET_RUNLOCK();
15637e3a6d3SLuigi Rizzo }
15737e3a6d3SLuigi Rizzo 
15837e3a6d3SLuigi Rizzo static int netmap_use_count = 0;
15937e3a6d3SLuigi Rizzo 
16037e3a6d3SLuigi Rizzo void
nm_os_get_module(void)16137e3a6d3SLuigi Rizzo nm_os_get_module(void)
16237e3a6d3SLuigi Rizzo {
16337e3a6d3SLuigi Rizzo 	netmap_use_count++;
16437e3a6d3SLuigi Rizzo }
16537e3a6d3SLuigi Rizzo 
16637e3a6d3SLuigi Rizzo void
nm_os_put_module(void)16737e3a6d3SLuigi Rizzo nm_os_put_module(void)
16837e3a6d3SLuigi Rizzo {
16937e3a6d3SLuigi Rizzo 	netmap_use_count--;
17037e3a6d3SLuigi Rizzo }
17137e3a6d3SLuigi Rizzo 
17237e3a6d3SLuigi Rizzo static void
netmap_ifnet_arrival_handler(void * arg __unused,struct ifnet * ifp)17337e3a6d3SLuigi Rizzo netmap_ifnet_arrival_handler(void *arg __unused, struct ifnet *ifp)
17437e3a6d3SLuigi Rizzo {
17537e3a6d3SLuigi Rizzo 	netmap_undo_zombie(ifp);
17637e3a6d3SLuigi Rizzo }
17737e3a6d3SLuigi Rizzo 
17837e3a6d3SLuigi Rizzo static void
netmap_ifnet_departure_handler(void * arg __unused,struct ifnet * ifp)17937e3a6d3SLuigi Rizzo netmap_ifnet_departure_handler(void *arg __unused, struct ifnet *ifp)
18037e3a6d3SLuigi Rizzo {
18137e3a6d3SLuigi Rizzo 	netmap_make_zombie(ifp);
18237e3a6d3SLuigi Rizzo }
18337e3a6d3SLuigi Rizzo 
18437e3a6d3SLuigi Rizzo static eventhandler_tag nm_ifnet_ah_tag;
18537e3a6d3SLuigi Rizzo static eventhandler_tag nm_ifnet_dh_tag;
18637e3a6d3SLuigi Rizzo 
18737e3a6d3SLuigi Rizzo int
nm_os_ifnet_init(void)18837e3a6d3SLuigi Rizzo nm_os_ifnet_init(void)
18937e3a6d3SLuigi Rizzo {
19037e3a6d3SLuigi Rizzo 	nm_ifnet_ah_tag =
19137e3a6d3SLuigi Rizzo 		EVENTHANDLER_REGISTER(ifnet_arrival_event,
19237e3a6d3SLuigi Rizzo 				netmap_ifnet_arrival_handler,
19337e3a6d3SLuigi Rizzo 				NULL, EVENTHANDLER_PRI_ANY);
19437e3a6d3SLuigi Rizzo 	nm_ifnet_dh_tag =
19537e3a6d3SLuigi Rizzo 		EVENTHANDLER_REGISTER(ifnet_departure_event,
19637e3a6d3SLuigi Rizzo 				netmap_ifnet_departure_handler,
19737e3a6d3SLuigi Rizzo 				NULL, EVENTHANDLER_PRI_ANY);
19837e3a6d3SLuigi Rizzo 	return 0;
19937e3a6d3SLuigi Rizzo }
20037e3a6d3SLuigi Rizzo 
20137e3a6d3SLuigi Rizzo void
nm_os_ifnet_fini(void)20237e3a6d3SLuigi Rizzo nm_os_ifnet_fini(void)
20337e3a6d3SLuigi Rizzo {
20437e3a6d3SLuigi Rizzo 	EVENTHANDLER_DEREGISTER(ifnet_arrival_event,
20537e3a6d3SLuigi Rizzo 			nm_ifnet_ah_tag);
20637e3a6d3SLuigi Rizzo 	EVENTHANDLER_DEREGISTER(ifnet_departure_event,
20737e3a6d3SLuigi Rizzo 			nm_ifnet_dh_tag);
20837e3a6d3SLuigi Rizzo }
20937e3a6d3SLuigi Rizzo 
2104f80b14cSVincenzo Maffione unsigned
nm_os_ifnet_mtu(struct ifnet * ifp)2114f80b14cSVincenzo Maffione nm_os_ifnet_mtu(struct ifnet *ifp)
2124f80b14cSVincenzo Maffione {
2134f80b14cSVincenzo Maffione #if __FreeBSD_version < 1100030
2144f80b14cSVincenzo Maffione 	return ifp->if_data.ifi_mtu;
2154f80b14cSVincenzo Maffione #else /* __FreeBSD_version >= 1100030 */
2164f80b14cSVincenzo Maffione 	return ifp->if_mtu;
2174f80b14cSVincenzo Maffione #endif
2184f80b14cSVincenzo Maffione }
2194f80b14cSVincenzo Maffione 
220e4166283SLuigi Rizzo rawsum_t
nm_os_csum_raw(uint8_t * data,size_t len,rawsum_t cur_sum)22137e3a6d3SLuigi Rizzo nm_os_csum_raw(uint8_t *data, size_t len, rawsum_t cur_sum)
222f0ea3689SLuigi Rizzo {
223f0ea3689SLuigi Rizzo 	/* TODO XXX please use the FreeBSD implementation for this. */
224f0ea3689SLuigi Rizzo 	uint16_t *words = (uint16_t *)data;
225f0ea3689SLuigi Rizzo 	int nw = len / 2;
226f0ea3689SLuigi Rizzo 	int i;
227f0ea3689SLuigi Rizzo 
228f0ea3689SLuigi Rizzo 	for (i = 0; i < nw; i++)
229f0ea3689SLuigi Rizzo 		cur_sum += be16toh(words[i]);
230f0ea3689SLuigi Rizzo 
231f0ea3689SLuigi Rizzo 	if (len & 1)
232f0ea3689SLuigi Rizzo 		cur_sum += (data[len-1] << 8);
233f0ea3689SLuigi Rizzo 
234f0ea3689SLuigi Rizzo 	return cur_sum;
235f0ea3689SLuigi Rizzo }
236f0ea3689SLuigi Rizzo 
237f0ea3689SLuigi Rizzo /* Fold a raw checksum: 'cur_sum' is in host byte order, while the
238f0ea3689SLuigi Rizzo  * return value is in network byte order.
239f0ea3689SLuigi Rizzo  */
240e4166283SLuigi Rizzo uint16_t
nm_os_csum_fold(rawsum_t cur_sum)24137e3a6d3SLuigi Rizzo nm_os_csum_fold(rawsum_t cur_sum)
242f0ea3689SLuigi Rizzo {
243f0ea3689SLuigi Rizzo 	/* TODO XXX please use the FreeBSD implementation for this. */
244f0ea3689SLuigi Rizzo 	while (cur_sum >> 16)
245f0ea3689SLuigi Rizzo 		cur_sum = (cur_sum & 0xFFFF) + (cur_sum >> 16);
246f0ea3689SLuigi Rizzo 
247f0ea3689SLuigi Rizzo 	return htobe16((~cur_sum) & 0xFFFF);
248f0ea3689SLuigi Rizzo }
249f0ea3689SLuigi Rizzo 
nm_os_csum_ipv4(struct nm_iphdr * iph)25037e3a6d3SLuigi Rizzo uint16_t nm_os_csum_ipv4(struct nm_iphdr *iph)
251f0ea3689SLuigi Rizzo {
252f0ea3689SLuigi Rizzo #if 0
253f0ea3689SLuigi Rizzo 	return in_cksum_hdr((void *)iph);
254f0ea3689SLuigi Rizzo #else
25537e3a6d3SLuigi Rizzo 	return nm_os_csum_fold(nm_os_csum_raw((uint8_t*)iph, sizeof(struct nm_iphdr), 0));
256f0ea3689SLuigi Rizzo #endif
257f0ea3689SLuigi Rizzo }
258f0ea3689SLuigi Rizzo 
259e4166283SLuigi Rizzo void
nm_os_csum_tcpudp_ipv4(struct nm_iphdr * iph,void * data,size_t datalen,uint16_t * check)26037e3a6d3SLuigi Rizzo nm_os_csum_tcpudp_ipv4(struct nm_iphdr *iph, void *data,
261f0ea3689SLuigi Rizzo 					size_t datalen, uint16_t *check)
262f0ea3689SLuigi Rizzo {
2635a067ae1SLuigi Rizzo #ifdef INET
264f0ea3689SLuigi Rizzo 	uint16_t pseudolen = datalen + iph->protocol;
265f0ea3689SLuigi Rizzo 
266f0ea3689SLuigi Rizzo 	/* Compute and insert the pseudo-header cheksum. */
267f0ea3689SLuigi Rizzo 	*check = in_pseudo(iph->saddr, iph->daddr,
268f0ea3689SLuigi Rizzo 				 htobe16(pseudolen));
269f0ea3689SLuigi Rizzo 	/* Compute the checksum on TCP/UDP header + payload
270f0ea3689SLuigi Rizzo 	 * (includes the pseudo-header).
271f0ea3689SLuigi Rizzo 	 */
27237e3a6d3SLuigi Rizzo 	*check = nm_os_csum_fold(nm_os_csum_raw(data, datalen, 0));
2735a067ae1SLuigi Rizzo #else
2745a067ae1SLuigi Rizzo 	static int notsupported = 0;
2755a067ae1SLuigi Rizzo 	if (!notsupported) {
2765a067ae1SLuigi Rizzo 		notsupported = 1;
277e2e0ef76SVincenzo Maffione 		nm_prerr("inet4 segmentation not supported");
2785a067ae1SLuigi Rizzo 	}
2795a067ae1SLuigi Rizzo #endif
280f0ea3689SLuigi Rizzo }
281f0ea3689SLuigi Rizzo 
282e4166283SLuigi Rizzo void
nm_os_csum_tcpudp_ipv6(struct nm_ipv6hdr * ip6h,void * data,size_t datalen,uint16_t * check)28337e3a6d3SLuigi Rizzo nm_os_csum_tcpudp_ipv6(struct nm_ipv6hdr *ip6h, void *data,
284f0ea3689SLuigi Rizzo 					size_t datalen, uint16_t *check)
285f0ea3689SLuigi Rizzo {
286f0ea3689SLuigi Rizzo #ifdef INET6
287f0ea3689SLuigi Rizzo 	*check = in6_cksum_pseudo((void*)ip6h, datalen, ip6h->nexthdr, 0);
28837e3a6d3SLuigi Rizzo 	*check = nm_os_csum_fold(nm_os_csum_raw(data, datalen, 0));
289f0ea3689SLuigi Rizzo #else
290f0ea3689SLuigi Rizzo 	static int notsupported = 0;
291f0ea3689SLuigi Rizzo 	if (!notsupported) {
292f0ea3689SLuigi Rizzo 		notsupported = 1;
293e2e0ef76SVincenzo Maffione 		nm_prerr("inet6 segmentation not supported");
294f0ea3689SLuigi Rizzo 	}
295f0ea3689SLuigi Rizzo #endif
296f0ea3689SLuigi Rizzo }
297f0ea3689SLuigi Rizzo 
29837e3a6d3SLuigi Rizzo /* on FreeBSD we send up one packet at a time */
29937e3a6d3SLuigi Rizzo void *
nm_os_send_up(struct ifnet * ifp,struct mbuf * m,struct mbuf * prev)30037e3a6d3SLuigi Rizzo nm_os_send_up(struct ifnet *ifp, struct mbuf *m, struct mbuf *prev)
30137e3a6d3SLuigi Rizzo {
30237e3a6d3SLuigi Rizzo 	NA(ifp)->if_input(ifp, m);
30337e3a6d3SLuigi Rizzo 	return NULL;
30437e3a6d3SLuigi Rizzo }
30537e3a6d3SLuigi Rizzo 
30637e3a6d3SLuigi Rizzo int
nm_os_mbuf_has_csum_offld(struct mbuf * m)307d740f837SVincenzo Maffione nm_os_mbuf_has_csum_offld(struct mbuf *m)
30837e3a6d3SLuigi Rizzo {
30937e3a6d3SLuigi Rizzo 	return m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP | CSUM_SCTP |
31037e3a6d3SLuigi Rizzo 					 CSUM_TCP_IPV6 | CSUM_UDP_IPV6 |
311d740f837SVincenzo Maffione 					 CSUM_SCTP_IPV6);
312d740f837SVincenzo Maffione }
313d740f837SVincenzo Maffione 
314d740f837SVincenzo Maffione int
nm_os_mbuf_has_seg_offld(struct mbuf * m)315d740f837SVincenzo Maffione nm_os_mbuf_has_seg_offld(struct mbuf *m)
316d740f837SVincenzo Maffione {
317d740f837SVincenzo Maffione 	return m->m_pkthdr.csum_flags & CSUM_TSO;
31837e3a6d3SLuigi Rizzo }
31937e3a6d3SLuigi Rizzo 
32037e3a6d3SLuigi Rizzo static void
freebsd_generic_rx_handler(struct ifnet * ifp,struct mbuf * m)32137e3a6d3SLuigi Rizzo freebsd_generic_rx_handler(struct ifnet *ifp, struct mbuf *m)
32237e3a6d3SLuigi Rizzo {
323c3e9b4dbSLuiz Otavio O Souza 	int stolen;
324c3e9b4dbSLuiz Otavio O Souza 
325e2e0ef76SVincenzo Maffione 	if (unlikely(!NM_NA_VALID(ifp))) {
326e2e0ef76SVincenzo Maffione 		nm_prlim(1, "Warning: RX packet intercepted, but no"
327e2e0ef76SVincenzo Maffione 				" emulated adapter");
328c3e9b4dbSLuiz Otavio O Souza 		return;
329c3e9b4dbSLuiz Otavio O Souza 	}
330c3e9b4dbSLuiz Otavio O Souza 
331c3e9b4dbSLuiz Otavio O Souza 	stolen = generic_rx_handler(ifp, m);
332c3e9b4dbSLuiz Otavio O Souza 	if (!stolen) {
33337e3a6d3SLuigi Rizzo 		struct netmap_generic_adapter *gna =
33437e3a6d3SLuigi Rizzo 				(struct netmap_generic_adapter *)NA(ifp);
33537e3a6d3SLuigi Rizzo 		gna->save_if_input(ifp, m);
33637e3a6d3SLuigi Rizzo 	}
33737e3a6d3SLuigi Rizzo }
338f0ea3689SLuigi Rizzo 
339f9790aebSLuigi Rizzo /*
340f9790aebSLuigi Rizzo  * Intercept the rx routine in the standard device driver.
341f9790aebSLuigi Rizzo  * Second argument is non-zero to intercept, 0 to restore
342f9790aebSLuigi Rizzo  */
343f9790aebSLuigi Rizzo int
nm_os_catch_rx(struct netmap_generic_adapter * gna,int intercept)34437e3a6d3SLuigi Rizzo nm_os_catch_rx(struct netmap_generic_adapter *gna, int intercept)
345f9790aebSLuigi Rizzo {
346847bf383SLuigi Rizzo 	struct netmap_adapter *na = &gna->up.up;
347f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
3484f80b14cSVincenzo Maffione 	int ret = 0;
349f9790aebSLuigi Rizzo 
3504f80b14cSVincenzo Maffione 	nm_os_ifnet_lock();
351f9790aebSLuigi Rizzo 	if (intercept) {
352f9790aebSLuigi Rizzo 		if (gna->save_if_input) {
353e2e0ef76SVincenzo Maffione 			nm_prerr("RX on %s already intercepted", na->name);
354e2e0ef76SVincenzo Maffione 			ret = EBUSY; /* already set */
3554f80b14cSVincenzo Maffione 			goto out;
356f9790aebSLuigi Rizzo 		}
357f9790aebSLuigi Rizzo 		gna->save_if_input = ifp->if_input;
35837e3a6d3SLuigi Rizzo 		ifp->if_input = freebsd_generic_rx_handler;
359f9790aebSLuigi Rizzo 	} else {
360f9790aebSLuigi Rizzo 		if (!gna->save_if_input) {
361e2e0ef76SVincenzo Maffione 			nm_prerr("Failed to undo RX intercept on %s",
362e2e0ef76SVincenzo Maffione 				na->name);
3634f80b14cSVincenzo Maffione 			ret = EINVAL;  /* not saved */
3644f80b14cSVincenzo Maffione 			goto out;
365f9790aebSLuigi Rizzo 		}
366f9790aebSLuigi Rizzo 		ifp->if_input = gna->save_if_input;
367f9790aebSLuigi Rizzo 		gna->save_if_input = NULL;
368f9790aebSLuigi Rizzo 	}
3694f80b14cSVincenzo Maffione out:
3704f80b14cSVincenzo Maffione 	nm_os_ifnet_unlock();
371f9790aebSLuigi Rizzo 
3724f80b14cSVincenzo Maffione 	return ret;
373f9790aebSLuigi Rizzo }
374f9790aebSLuigi Rizzo 
37517885a7bSLuigi Rizzo 
376f9790aebSLuigi Rizzo /*
377f9790aebSLuigi Rizzo  * Intercept the packet steering routine in the tx path,
378f9790aebSLuigi Rizzo  * so that we can decide which queue is used for an mbuf.
379f9790aebSLuigi Rizzo  * Second argument is non-zero to intercept, 0 to restore.
380f0ea3689SLuigi Rizzo  * On freebsd we just intercept if_transmit.
381f9790aebSLuigi Rizzo  */
38237e3a6d3SLuigi Rizzo int
nm_os_catch_tx(struct netmap_generic_adapter * gna,int intercept)38337e3a6d3SLuigi Rizzo nm_os_catch_tx(struct netmap_generic_adapter *gna, int intercept)
384f9790aebSLuigi Rizzo {
38517885a7bSLuigi Rizzo 	struct netmap_adapter *na = &gna->up.up;
386847bf383SLuigi Rizzo 	struct ifnet *ifp = netmap_generic_getifp(gna);
38717885a7bSLuigi Rizzo 
3884f80b14cSVincenzo Maffione 	nm_os_ifnet_lock();
38937e3a6d3SLuigi Rizzo 	if (intercept) {
39017885a7bSLuigi Rizzo 		na->if_transmit = ifp->if_transmit;
39117885a7bSLuigi Rizzo 		ifp->if_transmit = netmap_transmit;
392f9790aebSLuigi Rizzo 	} else {
39317885a7bSLuigi Rizzo 		ifp->if_transmit = na->if_transmit;
394f9790aebSLuigi Rizzo 	}
3954f80b14cSVincenzo Maffione 	nm_os_ifnet_unlock();
39637e3a6d3SLuigi Rizzo 
39737e3a6d3SLuigi Rizzo 	return 0;
398f9790aebSLuigi Rizzo }
399f9790aebSLuigi Rizzo 
40017885a7bSLuigi Rizzo 
401f0ea3689SLuigi Rizzo /*
402f0ea3689SLuigi Rizzo  * Transmit routine used by generic_netmap_txsync(). Returns 0 on success
403f9790aebSLuigi Rizzo  * and non-zero on error (which may be packet drops or other errors).
404f9790aebSLuigi Rizzo  * addr and len identify the netmap buffer, m is the (preallocated)
405f9790aebSLuigi Rizzo  * mbuf to use for transmissions.
406f9790aebSLuigi Rizzo  *
407f9790aebSLuigi Rizzo  * We should add a reference to the mbuf so the m_freem() at the end
408f9790aebSLuigi Rizzo  * of the transmission does not consume resources.
409f9790aebSLuigi Rizzo  *
410f9790aebSLuigi Rizzo  * On FreeBSD, and on multiqueue cards, we can force the queue using
411c2529042SHans Petter Selasky  *      if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
412f9790aebSLuigi Rizzo  *              i = m->m_pkthdr.flowid % adapter->num_queues;
413f9790aebSLuigi Rizzo  *      else
414f9790aebSLuigi Rizzo  *              i = curcpu % adapter->num_queues;
415f9790aebSLuigi Rizzo  *
416f9790aebSLuigi Rizzo  */
417f9790aebSLuigi Rizzo int
nm_os_generic_xmit_frame(struct nm_os_gen_arg * a)41837e3a6d3SLuigi Rizzo nm_os_generic_xmit_frame(struct nm_os_gen_arg *a)
419f9790aebSLuigi Rizzo {
420f9790aebSLuigi Rizzo 	int ret;
42137e3a6d3SLuigi Rizzo 	u_int len = a->len;
42237e3a6d3SLuigi Rizzo 	struct ifnet *ifp = a->ifp;
42337e3a6d3SLuigi Rizzo 	struct mbuf *m = a->m;
424f9790aebSLuigi Rizzo 
42537e3a6d3SLuigi Rizzo #if __FreeBSD_version < 1100000
426e4166283SLuigi Rizzo 	/*
42737e3a6d3SLuigi Rizzo 	 * Old FreeBSD versions. The mbuf has a cluster attached,
42837e3a6d3SLuigi Rizzo 	 * we need to copy from the cluster to the netmap buffer.
429e4166283SLuigi Rizzo 	 */
43037e3a6d3SLuigi Rizzo 	if (MBUF_REFCNT(m) != 1) {
431e2e0ef76SVincenzo Maffione 		nm_prerr("invalid refcnt %d for %p", MBUF_REFCNT(m), m);
432e4166283SLuigi Rizzo 		panic("in generic_xmit_frame");
433e4166283SLuigi Rizzo 	}
434e4166283SLuigi Rizzo 	if (m->m_ext.ext_size < len) {
435e2e0ef76SVincenzo Maffione 		nm_prlim(2, "size %d < len %d", m->m_ext.ext_size, len);
436e4166283SLuigi Rizzo 		len = m->m_ext.ext_size;
437e4166283SLuigi Rizzo 	}
43837e3a6d3SLuigi Rizzo 	bcopy(a->addr, m->m_data, len);
43937e3a6d3SLuigi Rizzo #else  /* __FreeBSD_version >= 1100000 */
44037e3a6d3SLuigi Rizzo 	/* New FreeBSD versions. Link the external storage to
44137e3a6d3SLuigi Rizzo 	 * the netmap buffer, so that no copy is necessary. */
44237e3a6d3SLuigi Rizzo 	m->m_ext.ext_buf = m->m_data = a->addr;
44337e3a6d3SLuigi Rizzo 	m->m_ext.ext_size = len;
44437e3a6d3SLuigi Rizzo #endif /* __FreeBSD_version >= 1100000 */
44537e3a6d3SLuigi Rizzo 
446*a44270bdSVincenzo Maffione 	m->m_flags |= M_PKTHDR;
447e4166283SLuigi Rizzo 	m->m_len = m->m_pkthdr.len = len;
44837e3a6d3SLuigi Rizzo 
44937e3a6d3SLuigi Rizzo 	/* mbuf refcnt is not contended, no need to use atomic
45037e3a6d3SLuigi Rizzo 	 * (a memory barrier is enough). */
45137e3a6d3SLuigi Rizzo 	SET_MBUF_REFCNT(m, 2);
452c2529042SHans Petter Selasky 	M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE);
45337e3a6d3SLuigi Rizzo 	m->m_pkthdr.flowid = a->ring_nr;
454f9790aebSLuigi Rizzo 	m->m_pkthdr.rcvif = ifp; /* used for tx notification */
455*a44270bdSVincenzo Maffione 	CURVNET_SET(ifp->if_vnet);
45617885a7bSLuigi Rizzo 	ret = NA(ifp)->if_transmit(ifp, m);
457*a44270bdSVincenzo Maffione 	CURVNET_RESTORE();
45837e3a6d3SLuigi Rizzo 	return ret ? -1 : 0;
459f9790aebSLuigi Rizzo }
460f9790aebSLuigi Rizzo 
46117885a7bSLuigi Rizzo 
4620dc809c0SLuigi Rizzo #if __FreeBSD_version >= 1100005
4630dc809c0SLuigi Rizzo struct netmap_adapter *
netmap_getna(if_t ifp)4640dc809c0SLuigi Rizzo netmap_getna(if_t ifp)
4650dc809c0SLuigi Rizzo {
4660dc809c0SLuigi Rizzo 	return (NA((struct ifnet *)ifp));
4670dc809c0SLuigi Rizzo }
4680dc809c0SLuigi Rizzo #endif /* __FreeBSD_version >= 1100005 */
4690dc809c0SLuigi Rizzo 
470f9790aebSLuigi Rizzo /*
471f9790aebSLuigi Rizzo  * The following two functions are empty until we have a generic
472f9790aebSLuigi Rizzo  * way to extract the info from the ifp
473f9790aebSLuigi Rizzo  */
474f9790aebSLuigi Rizzo int
nm_os_generic_find_num_desc(struct ifnet * ifp,unsigned int * tx,unsigned int * rx)47537e3a6d3SLuigi Rizzo nm_os_generic_find_num_desc(struct ifnet *ifp, unsigned int *tx, unsigned int *rx)
476f9790aebSLuigi Rizzo {
477f9790aebSLuigi Rizzo 	return 0;
478f9790aebSLuigi Rizzo }
479f9790aebSLuigi Rizzo 
48017885a7bSLuigi Rizzo 
481f9790aebSLuigi Rizzo void
nm_os_generic_find_num_queues(struct ifnet * ifp,u_int * txq,u_int * rxq)48237e3a6d3SLuigi Rizzo nm_os_generic_find_num_queues(struct ifnet *ifp, u_int *txq, u_int *rxq)
483f9790aebSLuigi Rizzo {
484c3e9b4dbSLuiz Otavio O Souza 	unsigned num_rings = netmap_generic_rings ? netmap_generic_rings : 1;
485c3e9b4dbSLuiz Otavio O Souza 
486c3e9b4dbSLuiz Otavio O Souza 	*txq = num_rings;
487c3e9b4dbSLuiz Otavio O Souza 	*rxq = num_rings;
488f9790aebSLuigi Rizzo }
489f9790aebSLuigi Rizzo 
49037e3a6d3SLuigi Rizzo void
nm_os_generic_set_features(struct netmap_generic_adapter * gna)49137e3a6d3SLuigi Rizzo nm_os_generic_set_features(struct netmap_generic_adapter *gna)
49237e3a6d3SLuigi Rizzo {
49337e3a6d3SLuigi Rizzo 
49437e3a6d3SLuigi Rizzo 	gna->rxsg = 1; /* Supported through m_copydata. */
49537e3a6d3SLuigi Rizzo 	gna->txqdisc = 0; /* Not supported. */
49637e3a6d3SLuigi Rizzo }
49717885a7bSLuigi Rizzo 
498e4166283SLuigi Rizzo void
nm_os_mitigation_init(struct nm_generic_mit * mit,int idx,struct netmap_adapter * na)49937e3a6d3SLuigi Rizzo nm_os_mitigation_init(struct nm_generic_mit *mit, int idx, struct netmap_adapter *na)
500f9790aebSLuigi Rizzo {
501f0ea3689SLuigi Rizzo 	mit->mit_pending = 0;
5024bf50f18SLuigi Rizzo 	mit->mit_ring_idx = idx;
503f0ea3689SLuigi Rizzo 	mit->mit_na = na;
504f9790aebSLuigi Rizzo }
505f9790aebSLuigi Rizzo 
506f9790aebSLuigi Rizzo 
507e4166283SLuigi Rizzo void
nm_os_mitigation_start(struct nm_generic_mit * mit)50837e3a6d3SLuigi Rizzo nm_os_mitigation_start(struct nm_generic_mit *mit)
509f9790aebSLuigi Rizzo {
510f9790aebSLuigi Rizzo }
511f9790aebSLuigi Rizzo 
51217885a7bSLuigi Rizzo 
513e4166283SLuigi Rizzo void
nm_os_mitigation_restart(struct nm_generic_mit * mit)51437e3a6d3SLuigi Rizzo nm_os_mitigation_restart(struct nm_generic_mit *mit)
515f9790aebSLuigi Rizzo {
516f9790aebSLuigi Rizzo }
517f9790aebSLuigi Rizzo 
51817885a7bSLuigi Rizzo 
519e4166283SLuigi Rizzo int
nm_os_mitigation_active(struct nm_generic_mit * mit)52037e3a6d3SLuigi Rizzo nm_os_mitigation_active(struct nm_generic_mit *mit)
521f9790aebSLuigi Rizzo {
522e2e0ef76SVincenzo Maffione 
523f9790aebSLuigi Rizzo 	return 0;
524f9790aebSLuigi Rizzo }
525f9790aebSLuigi Rizzo 
52617885a7bSLuigi Rizzo 
527e4166283SLuigi Rizzo void
nm_os_mitigation_cleanup(struct nm_generic_mit * mit)52837e3a6d3SLuigi Rizzo nm_os_mitigation_cleanup(struct nm_generic_mit *mit)
529f9790aebSLuigi Rizzo {
530f9790aebSLuigi Rizzo }
531f9790aebSLuigi Rizzo 
5324bf50f18SLuigi Rizzo static int
nm_vi_dummy(struct ifnet * ifp,u_long cmd,caddr_t addr)5334bf50f18SLuigi Rizzo nm_vi_dummy(struct ifnet *ifp, u_long cmd, caddr_t addr)
5344bf50f18SLuigi Rizzo {
535e2e0ef76SVincenzo Maffione 
5364bf50f18SLuigi Rizzo 	return EINVAL;
5374bf50f18SLuigi Rizzo }
5384bf50f18SLuigi Rizzo 
5394bf50f18SLuigi Rizzo static void
nm_vi_start(struct ifnet * ifp)5404bf50f18SLuigi Rizzo nm_vi_start(struct ifnet *ifp)
5414bf50f18SLuigi Rizzo {
5424bf50f18SLuigi Rizzo 	panic("nm_vi_start() must not be called");
5434bf50f18SLuigi Rizzo }
5444bf50f18SLuigi Rizzo 
5454bf50f18SLuigi Rizzo /*
5464bf50f18SLuigi Rizzo  * Index manager of persistent virtual interfaces.
5474bf50f18SLuigi Rizzo  * It is used to decide the lowest byte of the MAC address.
5484bf50f18SLuigi Rizzo  * We use the same algorithm with management of bridge port index.
5494bf50f18SLuigi Rizzo  */
5504bf50f18SLuigi Rizzo #define NM_VI_MAX	255
5514bf50f18SLuigi Rizzo static struct {
5524bf50f18SLuigi Rizzo 	uint8_t index[NM_VI_MAX]; /* XXX just for a reasonable number */
5534bf50f18SLuigi Rizzo 	uint8_t active;
5544bf50f18SLuigi Rizzo 	struct mtx lock;
5554bf50f18SLuigi Rizzo } nm_vi_indices;
5564bf50f18SLuigi Rizzo 
5574bf50f18SLuigi Rizzo void
nm_os_vi_init_index(void)55837e3a6d3SLuigi Rizzo nm_os_vi_init_index(void)
5594bf50f18SLuigi Rizzo {
5604bf50f18SLuigi Rizzo 	int i;
5614bf50f18SLuigi Rizzo 	for (i = 0; i < NM_VI_MAX; i++)
5624bf50f18SLuigi Rizzo 		nm_vi_indices.index[i] = i;
5634bf50f18SLuigi Rizzo 	nm_vi_indices.active = 0;
5644bf50f18SLuigi Rizzo 	mtx_init(&nm_vi_indices.lock, "nm_vi_indices_lock", NULL, MTX_DEF);
5654bf50f18SLuigi Rizzo }
5664bf50f18SLuigi Rizzo 
5674bf50f18SLuigi Rizzo /* return -1 if no index available */
5684bf50f18SLuigi Rizzo static int
nm_vi_get_index(void)5694bf50f18SLuigi Rizzo nm_vi_get_index(void)
5704bf50f18SLuigi Rizzo {
5714bf50f18SLuigi Rizzo 	int ret;
5724bf50f18SLuigi Rizzo 
5734bf50f18SLuigi Rizzo 	mtx_lock(&nm_vi_indices.lock);
5744bf50f18SLuigi Rizzo 	ret = nm_vi_indices.active == NM_VI_MAX ? -1 :
5754bf50f18SLuigi Rizzo 		nm_vi_indices.index[nm_vi_indices.active++];
5764bf50f18SLuigi Rizzo 	mtx_unlock(&nm_vi_indices.lock);
5774bf50f18SLuigi Rizzo 	return ret;
5784bf50f18SLuigi Rizzo }
5794bf50f18SLuigi Rizzo 
5804bf50f18SLuigi Rizzo static void
nm_vi_free_index(uint8_t val)5814bf50f18SLuigi Rizzo nm_vi_free_index(uint8_t val)
5824bf50f18SLuigi Rizzo {
5834bf50f18SLuigi Rizzo 	int i, lim;
5844bf50f18SLuigi Rizzo 
5854bf50f18SLuigi Rizzo 	mtx_lock(&nm_vi_indices.lock);
5864bf50f18SLuigi Rizzo 	lim = nm_vi_indices.active;
5874bf50f18SLuigi Rizzo 	for (i = 0; i < lim; i++) {
5884bf50f18SLuigi Rizzo 		if (nm_vi_indices.index[i] == val) {
5894bf50f18SLuigi Rizzo 			/* swap index[lim-1] and j */
5904bf50f18SLuigi Rizzo 			int tmp = nm_vi_indices.index[lim-1];
5914bf50f18SLuigi Rizzo 			nm_vi_indices.index[lim-1] = val;
5924bf50f18SLuigi Rizzo 			nm_vi_indices.index[i] = tmp;
5934bf50f18SLuigi Rizzo 			nm_vi_indices.active--;
5944bf50f18SLuigi Rizzo 			break;
5954bf50f18SLuigi Rizzo 		}
5964bf50f18SLuigi Rizzo 	}
5974bf50f18SLuigi Rizzo 	if (lim == nm_vi_indices.active)
598e2e0ef76SVincenzo Maffione 		nm_prerr("Index %u not found", val);
5994bf50f18SLuigi Rizzo 	mtx_unlock(&nm_vi_indices.lock);
6004bf50f18SLuigi Rizzo }
6014bf50f18SLuigi Rizzo #undef NM_VI_MAX
6024bf50f18SLuigi Rizzo 
6034bf50f18SLuigi Rizzo /*
6044bf50f18SLuigi Rizzo  * Implementation of a netmap-capable virtual interface that
6054bf50f18SLuigi Rizzo  * registered to the system.
6064bf50f18SLuigi Rizzo  * It is based on if_tap.c and ip_fw_log.c in FreeBSD 9.
6074bf50f18SLuigi Rizzo  *
6084bf50f18SLuigi Rizzo  * Note: Linux sets refcount to 0 on allocation of net_device,
6094bf50f18SLuigi Rizzo  * then increments it on registration to the system.
6104bf50f18SLuigi Rizzo  * FreeBSD sets refcount to 1 on if_alloc(), and does not
6114bf50f18SLuigi Rizzo  * increment this refcount on if_attach().
6124bf50f18SLuigi Rizzo  */
6134bf50f18SLuigi Rizzo int
nm_os_vi_persist(const char * name,struct ifnet ** ret)61437e3a6d3SLuigi Rizzo nm_os_vi_persist(const char *name, struct ifnet **ret)
6154bf50f18SLuigi Rizzo {
6164bf50f18SLuigi Rizzo 	struct ifnet *ifp;
6174bf50f18SLuigi Rizzo 	u_short macaddr_hi;
6184bf50f18SLuigi Rizzo 	uint32_t macaddr_mid;
6194bf50f18SLuigi Rizzo 	u_char eaddr[6];
6204bf50f18SLuigi Rizzo 	int unit = nm_vi_get_index(); /* just to decide MAC address */
6214bf50f18SLuigi Rizzo 
6224bf50f18SLuigi Rizzo 	if (unit < 0)
6234bf50f18SLuigi Rizzo 		return EBUSY;
6244bf50f18SLuigi Rizzo 	/*
6254bf50f18SLuigi Rizzo 	 * We use the same MAC address generation method with tap
6264bf50f18SLuigi Rizzo 	 * except for the highest octet is 00:be instead of 00:bd
6274bf50f18SLuigi Rizzo 	 */
6284bf50f18SLuigi Rizzo 	macaddr_hi = htons(0x00be); /* XXX tap + 1 */
6294bf50f18SLuigi Rizzo 	macaddr_mid = (uint32_t) ticks;
6304bf50f18SLuigi Rizzo 	bcopy(&macaddr_hi, eaddr, sizeof(short));
6314bf50f18SLuigi Rizzo 	bcopy(&macaddr_mid, &eaddr[2], sizeof(uint32_t));
6324bf50f18SLuigi Rizzo 	eaddr[5] = (uint8_t)unit;
6334bf50f18SLuigi Rizzo 
6344bf50f18SLuigi Rizzo 	ifp = if_alloc(IFT_ETHER);
6354bf50f18SLuigi Rizzo 	if (ifp == NULL) {
636e2e0ef76SVincenzo Maffione 		nm_prerr("if_alloc failed");
6374bf50f18SLuigi Rizzo 		return ENOMEM;
6384bf50f18SLuigi Rizzo 	}
6394bf50f18SLuigi Rizzo 	if_initname(ifp, name, IF_DUNIT_NONE);
6404bf50f18SLuigi Rizzo 	ifp->if_mtu = 65536;
6414bf50f18SLuigi Rizzo 	ifp->if_flags = IFF_UP | IFF_SIMPLEX | IFF_MULTICAST;
6424bf50f18SLuigi Rizzo 	ifp->if_init = (void *)nm_vi_dummy;
6434bf50f18SLuigi Rizzo 	ifp->if_ioctl = nm_vi_dummy;
6444bf50f18SLuigi Rizzo 	ifp->if_start = nm_vi_start;
6454bf50f18SLuigi Rizzo 	ifp->if_mtu = ETHERMTU;
6464bf50f18SLuigi Rizzo 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
6474bf50f18SLuigi Rizzo 	ifp->if_capabilities |= IFCAP_LINKSTATE;
6484bf50f18SLuigi Rizzo 	ifp->if_capenable |= IFCAP_LINKSTATE;
6494bf50f18SLuigi Rizzo 
6504bf50f18SLuigi Rizzo 	ether_ifattach(ifp, eaddr);
6514bf50f18SLuigi Rizzo 	*ret = ifp;
6524bf50f18SLuigi Rizzo 	return 0;
6534bf50f18SLuigi Rizzo }
65437e3a6d3SLuigi Rizzo 
6554bf50f18SLuigi Rizzo /* unregister from the system and drop the final refcount */
6564bf50f18SLuigi Rizzo void
nm_os_vi_detach(struct ifnet * ifp)65737e3a6d3SLuigi Rizzo nm_os_vi_detach(struct ifnet *ifp)
6584bf50f18SLuigi Rizzo {
6594bf50f18SLuigi Rizzo 	nm_vi_free_index(((char *)IF_LLADDR(ifp))[5]);
6604bf50f18SLuigi Rizzo 	ether_ifdetach(ifp);
6614bf50f18SLuigi Rizzo 	if_free(ifp);
6624bf50f18SLuigi Rizzo }
66317885a7bSLuigi Rizzo 
6642ff91c17SVincenzo Maffione #ifdef WITH_EXTMEM
6652ff91c17SVincenzo Maffione #include <vm/vm_map.h>
6662ff91c17SVincenzo Maffione #include <vm/vm_kern.h>
6672ff91c17SVincenzo Maffione struct nm_os_extmem {
6682ff91c17SVincenzo Maffione 	vm_object_t obj;
6692ff91c17SVincenzo Maffione 	vm_offset_t kva;
6702ff91c17SVincenzo Maffione 	vm_offset_t size;
671cfa866f6SMatt Macy 	uintptr_t scan;
6722ff91c17SVincenzo Maffione };
6732ff91c17SVincenzo Maffione 
6742ff91c17SVincenzo Maffione void
nm_os_extmem_delete(struct nm_os_extmem * e)6752ff91c17SVincenzo Maffione nm_os_extmem_delete(struct nm_os_extmem *e)
6762ff91c17SVincenzo Maffione {
677e2e0ef76SVincenzo Maffione 	nm_prinf("freeing %zx bytes", (size_t)e->size);
6782ff91c17SVincenzo Maffione 	vm_map_remove(kernel_map, e->kva, e->kva + e->size);
6792ff91c17SVincenzo Maffione 	nm_os_free(e);
6802ff91c17SVincenzo Maffione }
6812ff91c17SVincenzo Maffione 
6822ff91c17SVincenzo Maffione char *
nm_os_extmem_nextpage(struct nm_os_extmem * e)6832ff91c17SVincenzo Maffione nm_os_extmem_nextpage(struct nm_os_extmem *e)
6842ff91c17SVincenzo Maffione {
6852ff91c17SVincenzo Maffione 	char *rv = NULL;
6862ff91c17SVincenzo Maffione 	if (e->scan < e->kva + e->size) {
6872ff91c17SVincenzo Maffione 		rv = (char *)e->scan;
6882ff91c17SVincenzo Maffione 		e->scan += PAGE_SIZE;
6892ff91c17SVincenzo Maffione 	}
6902ff91c17SVincenzo Maffione 	return rv;
6912ff91c17SVincenzo Maffione }
6922ff91c17SVincenzo Maffione 
6932ff91c17SVincenzo Maffione int
nm_os_extmem_isequal(struct nm_os_extmem * e1,struct nm_os_extmem * e2)6942ff91c17SVincenzo Maffione nm_os_extmem_isequal(struct nm_os_extmem *e1, struct nm_os_extmem *e2)
6952ff91c17SVincenzo Maffione {
6963535fae8SMatt Macy 	return (e1->obj == e2->obj);
6972ff91c17SVincenzo Maffione }
6982ff91c17SVincenzo Maffione 
6992ff91c17SVincenzo Maffione int
nm_os_extmem_nr_pages(struct nm_os_extmem * e)7002ff91c17SVincenzo Maffione nm_os_extmem_nr_pages(struct nm_os_extmem *e)
7012ff91c17SVincenzo Maffione {
7022ff91c17SVincenzo Maffione 	return e->size >> PAGE_SHIFT;
7032ff91c17SVincenzo Maffione }
7042ff91c17SVincenzo Maffione 
7052ff91c17SVincenzo Maffione struct nm_os_extmem *
nm_os_extmem_create(unsigned long p,struct nmreq_pools_info * pi,int * perror)7062ff91c17SVincenzo Maffione nm_os_extmem_create(unsigned long p, struct nmreq_pools_info *pi, int *perror)
7072ff91c17SVincenzo Maffione {
7082ff91c17SVincenzo Maffione 	vm_map_t map;
7092ff91c17SVincenzo Maffione 	vm_map_entry_t entry;
7102ff91c17SVincenzo Maffione 	vm_object_t obj;
7112ff91c17SVincenzo Maffione 	vm_prot_t prot;
7122ff91c17SVincenzo Maffione 	vm_pindex_t index;
7132ff91c17SVincenzo Maffione 	boolean_t wired;
7142ff91c17SVincenzo Maffione 	struct nm_os_extmem *e = NULL;
7152ff91c17SVincenzo Maffione 	int rv, error = 0;
7162ff91c17SVincenzo Maffione 
7172ff91c17SVincenzo Maffione 	e = nm_os_malloc(sizeof(*e));
7182ff91c17SVincenzo Maffione 	if (e == NULL) {
7192ff91c17SVincenzo Maffione 		error = ENOMEM;
7202ff91c17SVincenzo Maffione 		goto out;
7212ff91c17SVincenzo Maffione 	}
7222ff91c17SVincenzo Maffione 
7232ff91c17SVincenzo Maffione 	map = &curthread->td_proc->p_vmspace->vm_map;
7242ff91c17SVincenzo Maffione 	rv = vm_map_lookup(&map, p, VM_PROT_RW, &entry,
7252ff91c17SVincenzo Maffione 			&obj, &index, &prot, &wired);
7262ff91c17SVincenzo Maffione 	if (rv != KERN_SUCCESS) {
727e2e0ef76SVincenzo Maffione 		nm_prerr("address %lx not found", p);
7282ff91c17SVincenzo Maffione 		goto out_free;
7292ff91c17SVincenzo Maffione 	}
7302ff91c17SVincenzo Maffione 	/* check that we are given the whole vm_object ? */
7312ff91c17SVincenzo Maffione 	vm_map_lookup_done(map, entry);
7322ff91c17SVincenzo Maffione 
7332ff91c17SVincenzo Maffione 	// XXX can we really use obj after releasing the map lock?
7342ff91c17SVincenzo Maffione 	e->obj = obj;
7352ff91c17SVincenzo Maffione 	vm_object_reference(obj);
7362ff91c17SVincenzo Maffione 	/* wire the memory and add the vm_object to the kernel map,
7372ff91c17SVincenzo Maffione 	 * to make sure that it is not fred even if the processes that
7382ff91c17SVincenzo Maffione 	 * are mmap()ing it all exit
7392ff91c17SVincenzo Maffione 	 */
7402ff91c17SVincenzo Maffione 	e->kva = vm_map_min(kernel_map);
7412ff91c17SVincenzo Maffione 	e->size = obj->size << PAGE_SHIFT;
7422ff91c17SVincenzo Maffione 	rv = vm_map_find(kernel_map, obj, 0, &e->kva, e->size, 0,
7432ff91c17SVincenzo Maffione 			VMFS_OPTIMAL_SPACE, VM_PROT_READ | VM_PROT_WRITE,
7442ff91c17SVincenzo Maffione 			VM_PROT_READ | VM_PROT_WRITE, 0);
7452ff91c17SVincenzo Maffione 	if (rv != KERN_SUCCESS) {
746e2e0ef76SVincenzo Maffione 		nm_prerr("vm_map_find(%zx) failed", (size_t)e->size);
7472ff91c17SVincenzo Maffione 		goto out_rel;
7482ff91c17SVincenzo Maffione 	}
7492ff91c17SVincenzo Maffione 	rv = vm_map_wire(kernel_map, e->kva, e->kva + e->size,
7502ff91c17SVincenzo Maffione 			VM_MAP_WIRE_SYSTEM | VM_MAP_WIRE_NOHOLES);
7512ff91c17SVincenzo Maffione 	if (rv != KERN_SUCCESS) {
752e2e0ef76SVincenzo Maffione 		nm_prerr("vm_map_wire failed");
7532ff91c17SVincenzo Maffione 		goto out_rem;
7542ff91c17SVincenzo Maffione 	}
7552ff91c17SVincenzo Maffione 
7562ff91c17SVincenzo Maffione 	e->scan = e->kva;
7572ff91c17SVincenzo Maffione 
7582ff91c17SVincenzo Maffione 	return e;
7592ff91c17SVincenzo Maffione 
7602ff91c17SVincenzo Maffione out_rem:
7612ff91c17SVincenzo Maffione 	vm_map_remove(kernel_map, e->kva, e->kva + e->size);
7622ff91c17SVincenzo Maffione 	e->obj = NULL;
7632ff91c17SVincenzo Maffione out_rel:
7642ff91c17SVincenzo Maffione 	vm_object_deallocate(e->obj);
7652ff91c17SVincenzo Maffione out_free:
7662ff91c17SVincenzo Maffione 	nm_os_free(e);
7672ff91c17SVincenzo Maffione out:
7682ff91c17SVincenzo Maffione 	if (perror)
7692ff91c17SVincenzo Maffione 		*perror = error;
7702ff91c17SVincenzo Maffione 	return NULL;
7712ff91c17SVincenzo Maffione }
7722ff91c17SVincenzo Maffione #endif /* WITH_EXTMEM */
7732ff91c17SVincenzo Maffione 
774b321acabSVincenzo Maffione /* ================== PTNETMAP GUEST SUPPORT ==================== */
77537e3a6d3SLuigi Rizzo 
776b321acabSVincenzo Maffione #ifdef WITH_PTNETMAP
77737e3a6d3SLuigi Rizzo #include <sys/bus.h>
77837e3a6d3SLuigi Rizzo #include <sys/rman.h>
77937e3a6d3SLuigi Rizzo #include <machine/bus.h>        /* bus_dmamap_* */
78037e3a6d3SLuigi Rizzo #include <machine/resource.h>
78137e3a6d3SLuigi Rizzo #include <dev/pci/pcivar.h>
78237e3a6d3SLuigi Rizzo #include <dev/pci/pcireg.h>
78337e3a6d3SLuigi Rizzo /*
78437e3a6d3SLuigi Rizzo  * ptnetmap memory device (memdev) for freebsd guest,
78537e3a6d3SLuigi Rizzo  * ssed to expose host netmap memory to the guest through a PCI BAR.
78637e3a6d3SLuigi Rizzo  */
78737e3a6d3SLuigi Rizzo 
78837e3a6d3SLuigi Rizzo /*
78937e3a6d3SLuigi Rizzo  * ptnetmap memdev private data structure
79037e3a6d3SLuigi Rizzo  */
79137e3a6d3SLuigi Rizzo struct ptnetmap_memdev {
79237e3a6d3SLuigi Rizzo 	device_t dev;
79337e3a6d3SLuigi Rizzo 	struct resource *pci_io;
79437e3a6d3SLuigi Rizzo 	struct resource *pci_mem;
79537e3a6d3SLuigi Rizzo 	struct netmap_mem_d *nm_mem;
79637e3a6d3SLuigi Rizzo };
79737e3a6d3SLuigi Rizzo 
79837e3a6d3SLuigi Rizzo static int	ptn_memdev_probe(device_t);
79937e3a6d3SLuigi Rizzo static int	ptn_memdev_attach(device_t);
80037e3a6d3SLuigi Rizzo static int	ptn_memdev_detach(device_t);
80137e3a6d3SLuigi Rizzo static int	ptn_memdev_shutdown(device_t);
80237e3a6d3SLuigi Rizzo 
80337e3a6d3SLuigi Rizzo static device_method_t ptn_memdev_methods[] = {
80437e3a6d3SLuigi Rizzo 	DEVMETHOD(device_probe, ptn_memdev_probe),
80537e3a6d3SLuigi Rizzo 	DEVMETHOD(device_attach, ptn_memdev_attach),
80637e3a6d3SLuigi Rizzo 	DEVMETHOD(device_detach, ptn_memdev_detach),
80737e3a6d3SLuigi Rizzo 	DEVMETHOD(device_shutdown, ptn_memdev_shutdown),
80837e3a6d3SLuigi Rizzo 	DEVMETHOD_END
80937e3a6d3SLuigi Rizzo };
81037e3a6d3SLuigi Rizzo 
81137e3a6d3SLuigi Rizzo static driver_t ptn_memdev_driver = {
81237e3a6d3SLuigi Rizzo 	PTNETMAP_MEMDEV_NAME,
81337e3a6d3SLuigi Rizzo 	ptn_memdev_methods,
81437e3a6d3SLuigi Rizzo 	sizeof(struct ptnetmap_memdev),
81537e3a6d3SLuigi Rizzo };
81637e3a6d3SLuigi Rizzo 
81737e3a6d3SLuigi Rizzo /* We use (SI_ORDER_MIDDLE+1) here, see DEV_MODULE_ORDERED() invocation
81837e3a6d3SLuigi Rizzo  * below. */
81937e3a6d3SLuigi Rizzo static devclass_t ptnetmap_devclass;
82037e3a6d3SLuigi Rizzo DRIVER_MODULE_ORDERED(ptn_memdev, pci, ptn_memdev_driver, ptnetmap_devclass,
82137e3a6d3SLuigi Rizzo 		      NULL, NULL, SI_ORDER_MIDDLE + 1);
82237e3a6d3SLuigi Rizzo 
82337e3a6d3SLuigi Rizzo /*
82437e3a6d3SLuigi Rizzo  * Map host netmap memory through PCI-BAR in the guest OS,
82537e3a6d3SLuigi Rizzo  * returning physical (nm_paddr) and virtual (nm_addr) addresses
82637e3a6d3SLuigi Rizzo  * of the netmap memory mapped in the guest.
82737e3a6d3SLuigi Rizzo  */
82837e3a6d3SLuigi Rizzo int
nm_os_pt_memdev_iomap(struct ptnetmap_memdev * ptn_dev,vm_paddr_t * nm_paddr,void ** nm_addr,uint64_t * mem_size)829a2a74091SLuigi Rizzo nm_os_pt_memdev_iomap(struct ptnetmap_memdev *ptn_dev, vm_paddr_t *nm_paddr,
830844a6f0cSLuigi Rizzo 		      void **nm_addr, uint64_t *mem_size)
83137e3a6d3SLuigi Rizzo {
83237e3a6d3SLuigi Rizzo 	int rid;
83337e3a6d3SLuigi Rizzo 
834e2e0ef76SVincenzo Maffione 	nm_prinf("ptn_memdev_driver iomap");
83537e3a6d3SLuigi Rizzo 
83637e3a6d3SLuigi Rizzo 	rid = PCIR_BAR(PTNETMAP_MEM_PCI_BAR);
837844a6f0cSLuigi Rizzo 	*mem_size = bus_read_4(ptn_dev->pci_io, PTNET_MDEV_IO_MEMSIZE_HI);
838844a6f0cSLuigi Rizzo 	*mem_size = bus_read_4(ptn_dev->pci_io, PTNET_MDEV_IO_MEMSIZE_LO) |
839844a6f0cSLuigi Rizzo 			(*mem_size << 32);
84037e3a6d3SLuigi Rizzo 
84137e3a6d3SLuigi Rizzo 	/* map memory allocator */
84237e3a6d3SLuigi Rizzo 	ptn_dev->pci_mem = bus_alloc_resource(ptn_dev->dev, SYS_RES_MEMORY,
843844a6f0cSLuigi Rizzo 			&rid, 0, ~0, *mem_size, RF_ACTIVE);
84437e3a6d3SLuigi Rizzo 	if (ptn_dev->pci_mem == NULL) {
84537e3a6d3SLuigi Rizzo 		*nm_paddr = 0;
8464dd44461SLuiz Otavio O Souza 		*nm_addr = NULL;
84737e3a6d3SLuigi Rizzo 		return ENOMEM;
84837e3a6d3SLuigi Rizzo 	}
84937e3a6d3SLuigi Rizzo 
85037e3a6d3SLuigi Rizzo 	*nm_paddr = rman_get_start(ptn_dev->pci_mem);
85137e3a6d3SLuigi Rizzo 	*nm_addr = rman_get_virtual(ptn_dev->pci_mem);
85237e3a6d3SLuigi Rizzo 
853e2e0ef76SVincenzo Maffione 	nm_prinf("=== BAR %d start %lx len %lx mem_size %lx ===",
85437e3a6d3SLuigi Rizzo 			PTNETMAP_MEM_PCI_BAR,
855a2a74091SLuigi Rizzo 			(unsigned long)(*nm_paddr),
856a2a74091SLuigi Rizzo 			(unsigned long)rman_get_size(ptn_dev->pci_mem),
857844a6f0cSLuigi Rizzo 			(unsigned long)*mem_size);
85837e3a6d3SLuigi Rizzo 	return (0);
85937e3a6d3SLuigi Rizzo }
86037e3a6d3SLuigi Rizzo 
861844a6f0cSLuigi Rizzo uint32_t
nm_os_pt_memdev_ioread(struct ptnetmap_memdev * ptn_dev,unsigned int reg)862844a6f0cSLuigi Rizzo nm_os_pt_memdev_ioread(struct ptnetmap_memdev *ptn_dev, unsigned int reg)
863844a6f0cSLuigi Rizzo {
864844a6f0cSLuigi Rizzo 	return bus_read_4(ptn_dev->pci_io, reg);
865844a6f0cSLuigi Rizzo }
866844a6f0cSLuigi Rizzo 
86737e3a6d3SLuigi Rizzo /* Unmap host netmap memory. */
86837e3a6d3SLuigi Rizzo void
nm_os_pt_memdev_iounmap(struct ptnetmap_memdev * ptn_dev)86937e3a6d3SLuigi Rizzo nm_os_pt_memdev_iounmap(struct ptnetmap_memdev *ptn_dev)
87037e3a6d3SLuigi Rizzo {
871e2e0ef76SVincenzo Maffione 	nm_prinf("ptn_memdev_driver iounmap");
87237e3a6d3SLuigi Rizzo 
87337e3a6d3SLuigi Rizzo 	if (ptn_dev->pci_mem) {
87437e3a6d3SLuigi Rizzo 		bus_release_resource(ptn_dev->dev, SYS_RES_MEMORY,
87537e3a6d3SLuigi Rizzo 			PCIR_BAR(PTNETMAP_MEM_PCI_BAR), ptn_dev->pci_mem);
87637e3a6d3SLuigi Rizzo 		ptn_dev->pci_mem = NULL;
87737e3a6d3SLuigi Rizzo 	}
87837e3a6d3SLuigi Rizzo }
87937e3a6d3SLuigi Rizzo 
88037e3a6d3SLuigi Rizzo /* Device identification routine, return BUS_PROBE_DEFAULT on success,
88137e3a6d3SLuigi Rizzo  * positive on failure */
88237e3a6d3SLuigi Rizzo static int
ptn_memdev_probe(device_t dev)88337e3a6d3SLuigi Rizzo ptn_memdev_probe(device_t dev)
88437e3a6d3SLuigi Rizzo {
88537e3a6d3SLuigi Rizzo 	char desc[256];
88637e3a6d3SLuigi Rizzo 
88737e3a6d3SLuigi Rizzo 	if (pci_get_vendor(dev) != PTNETMAP_PCI_VENDOR_ID)
88837e3a6d3SLuigi Rizzo 		return (ENXIO);
88937e3a6d3SLuigi Rizzo 	if (pci_get_device(dev) != PTNETMAP_PCI_DEVICE_ID)
89037e3a6d3SLuigi Rizzo 		return (ENXIO);
89137e3a6d3SLuigi Rizzo 
89237e3a6d3SLuigi Rizzo 	snprintf(desc, sizeof(desc), "%s PCI adapter",
89337e3a6d3SLuigi Rizzo 			PTNETMAP_MEMDEV_NAME);
89437e3a6d3SLuigi Rizzo 	device_set_desc_copy(dev, desc);
89537e3a6d3SLuigi Rizzo 
89637e3a6d3SLuigi Rizzo 	return (BUS_PROBE_DEFAULT);
89737e3a6d3SLuigi Rizzo }
89837e3a6d3SLuigi Rizzo 
89937e3a6d3SLuigi Rizzo /* Device initialization routine. */
90037e3a6d3SLuigi Rizzo static int
ptn_memdev_attach(device_t dev)90137e3a6d3SLuigi Rizzo ptn_memdev_attach(device_t dev)
90237e3a6d3SLuigi Rizzo {
90337e3a6d3SLuigi Rizzo 	struct ptnetmap_memdev *ptn_dev;
90437e3a6d3SLuigi Rizzo 	int rid;
90537e3a6d3SLuigi Rizzo 	uint16_t mem_id;
90637e3a6d3SLuigi Rizzo 
90737e3a6d3SLuigi Rizzo 	ptn_dev = device_get_softc(dev);
90837e3a6d3SLuigi Rizzo 	ptn_dev->dev = dev;
90937e3a6d3SLuigi Rizzo 
91037e3a6d3SLuigi Rizzo 	pci_enable_busmaster(dev);
91137e3a6d3SLuigi Rizzo 
91237e3a6d3SLuigi Rizzo 	rid = PCIR_BAR(PTNETMAP_IO_PCI_BAR);
91337e3a6d3SLuigi Rizzo 	ptn_dev->pci_io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
91437e3a6d3SLuigi Rizzo 						 RF_ACTIVE);
91537e3a6d3SLuigi Rizzo 	if (ptn_dev->pci_io == NULL) {
91637e3a6d3SLuigi Rizzo 	        device_printf(dev, "cannot map I/O space\n");
91737e3a6d3SLuigi Rizzo 	        return (ENXIO);
91837e3a6d3SLuigi Rizzo 	}
91937e3a6d3SLuigi Rizzo 
920844a6f0cSLuigi Rizzo 	mem_id = bus_read_4(ptn_dev->pci_io, PTNET_MDEV_IO_MEMID);
92137e3a6d3SLuigi Rizzo 
92237e3a6d3SLuigi Rizzo 	/* create guest allocator */
92337e3a6d3SLuigi Rizzo 	ptn_dev->nm_mem = netmap_mem_pt_guest_attach(ptn_dev, mem_id);
92437e3a6d3SLuigi Rizzo 	if (ptn_dev->nm_mem == NULL) {
92537e3a6d3SLuigi Rizzo 		ptn_memdev_detach(dev);
92637e3a6d3SLuigi Rizzo 	        return (ENOMEM);
92737e3a6d3SLuigi Rizzo 	}
92837e3a6d3SLuigi Rizzo 	netmap_mem_get(ptn_dev->nm_mem);
92937e3a6d3SLuigi Rizzo 
930e2e0ef76SVincenzo Maffione 	nm_prinf("ptnetmap memdev attached, host memid: %u", mem_id);
93137e3a6d3SLuigi Rizzo 
93237e3a6d3SLuigi Rizzo 	return (0);
93337e3a6d3SLuigi Rizzo }
93437e3a6d3SLuigi Rizzo 
93537e3a6d3SLuigi Rizzo /* Device removal routine. */
93637e3a6d3SLuigi Rizzo static int
ptn_memdev_detach(device_t dev)93737e3a6d3SLuigi Rizzo ptn_memdev_detach(device_t dev)
93837e3a6d3SLuigi Rizzo {
93937e3a6d3SLuigi Rizzo 	struct ptnetmap_memdev *ptn_dev;
94037e3a6d3SLuigi Rizzo 
94137e3a6d3SLuigi Rizzo 	ptn_dev = device_get_softc(dev);
94237e3a6d3SLuigi Rizzo 
94337e3a6d3SLuigi Rizzo 	if (ptn_dev->nm_mem) {
944e2e0ef76SVincenzo Maffione 		nm_prinf("ptnetmap memdev detached, host memid %u",
945e2e0ef76SVincenzo Maffione 			netmap_mem_get_id(ptn_dev->nm_mem));
94637e3a6d3SLuigi Rizzo 		netmap_mem_put(ptn_dev->nm_mem);
94737e3a6d3SLuigi Rizzo 		ptn_dev->nm_mem = NULL;
94837e3a6d3SLuigi Rizzo 	}
94937e3a6d3SLuigi Rizzo 	if (ptn_dev->pci_mem) {
95037e3a6d3SLuigi Rizzo 		bus_release_resource(dev, SYS_RES_MEMORY,
95137e3a6d3SLuigi Rizzo 			PCIR_BAR(PTNETMAP_MEM_PCI_BAR), ptn_dev->pci_mem);
95237e3a6d3SLuigi Rizzo 		ptn_dev->pci_mem = NULL;
95337e3a6d3SLuigi Rizzo 	}
95437e3a6d3SLuigi Rizzo 	if (ptn_dev->pci_io) {
95537e3a6d3SLuigi Rizzo 		bus_release_resource(dev, SYS_RES_IOPORT,
95637e3a6d3SLuigi Rizzo 			PCIR_BAR(PTNETMAP_IO_PCI_BAR), ptn_dev->pci_io);
95737e3a6d3SLuigi Rizzo 		ptn_dev->pci_io = NULL;
95837e3a6d3SLuigi Rizzo 	}
95937e3a6d3SLuigi Rizzo 
96037e3a6d3SLuigi Rizzo 	return (0);
96137e3a6d3SLuigi Rizzo }
96237e3a6d3SLuigi Rizzo 
96337e3a6d3SLuigi Rizzo static int
ptn_memdev_shutdown(device_t dev)96437e3a6d3SLuigi Rizzo ptn_memdev_shutdown(device_t dev)
96537e3a6d3SLuigi Rizzo {
96637e3a6d3SLuigi Rizzo 	return bus_generic_shutdown(dev);
96737e3a6d3SLuigi Rizzo }
96837e3a6d3SLuigi Rizzo 
969b321acabSVincenzo Maffione #endif /* WITH_PTNETMAP */
97037e3a6d3SLuigi Rizzo 
971f9790aebSLuigi Rizzo /*
972f9790aebSLuigi Rizzo  * In order to track whether pages are still mapped, we hook into
973f9790aebSLuigi Rizzo  * the standard cdev_pager and intercept the constructor and
974f9790aebSLuigi Rizzo  * destructor.
975f9790aebSLuigi Rizzo  */
976f9790aebSLuigi Rizzo 
977f9790aebSLuigi Rizzo struct netmap_vm_handle_t {
978f9790aebSLuigi Rizzo 	struct cdev 		*dev;
979f9790aebSLuigi Rizzo 	struct netmap_priv_d	*priv;
980f9790aebSLuigi Rizzo };
981f9790aebSLuigi Rizzo 
98217885a7bSLuigi Rizzo 
983f9790aebSLuigi Rizzo static int
netmap_dev_pager_ctor(void * handle,vm_ooffset_t size,vm_prot_t prot,vm_ooffset_t foff,struct ucred * cred,u_short * color)984f9790aebSLuigi Rizzo netmap_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
985f9790aebSLuigi Rizzo 		vm_ooffset_t foff, struct ucred *cred, u_short *color)
986f9790aebSLuigi Rizzo {
987f9790aebSLuigi Rizzo 	struct netmap_vm_handle_t *vmh = handle;
988f0ea3689SLuigi Rizzo 
989f0ea3689SLuigi Rizzo 	if (netmap_verbose)
990e2e0ef76SVincenzo Maffione 		nm_prinf("handle %p size %jd prot %d foff %jd",
991f9790aebSLuigi Rizzo 			handle, (intmax_t)size, prot, (intmax_t)foff);
9924e93beffSLuigi Rizzo 	if (color)
9934e93beffSLuigi Rizzo 		*color = 0;
994f9790aebSLuigi Rizzo 	dev_ref(vmh->dev);
995f9790aebSLuigi Rizzo 	return 0;
996f9790aebSLuigi Rizzo }
997f9790aebSLuigi Rizzo 
998f9790aebSLuigi Rizzo 
999f9790aebSLuigi Rizzo static void
netmap_dev_pager_dtor(void * handle)1000f9790aebSLuigi Rizzo netmap_dev_pager_dtor(void *handle)
1001f9790aebSLuigi Rizzo {
1002f9790aebSLuigi Rizzo 	struct netmap_vm_handle_t *vmh = handle;
1003f9790aebSLuigi Rizzo 	struct cdev *dev = vmh->dev;
1004f9790aebSLuigi Rizzo 	struct netmap_priv_d *priv = vmh->priv;
1005f0ea3689SLuigi Rizzo 
1006f0ea3689SLuigi Rizzo 	if (netmap_verbose)
1007e2e0ef76SVincenzo Maffione 		nm_prinf("handle %p", handle);
1008f9790aebSLuigi Rizzo 	netmap_dtor(priv);
1009f9790aebSLuigi Rizzo 	free(vmh, M_DEVBUF);
1010f9790aebSLuigi Rizzo 	dev_rel(dev);
1011f9790aebSLuigi Rizzo }
1012f9790aebSLuigi Rizzo 
101317885a7bSLuigi Rizzo 
1014f9790aebSLuigi Rizzo static int
netmap_dev_pager_fault(vm_object_t object,vm_ooffset_t offset,int prot,vm_page_t * mres)1015f9790aebSLuigi Rizzo netmap_dev_pager_fault(vm_object_t object, vm_ooffset_t offset,
1016f9790aebSLuigi Rizzo 	int prot, vm_page_t *mres)
1017f9790aebSLuigi Rizzo {
1018f9790aebSLuigi Rizzo 	struct netmap_vm_handle_t *vmh = object->handle;
1019f9790aebSLuigi Rizzo 	struct netmap_priv_d *priv = vmh->priv;
1020847bf383SLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
1021f9790aebSLuigi Rizzo 	vm_paddr_t paddr;
1022f9790aebSLuigi Rizzo 	vm_page_t page;
1023f9790aebSLuigi Rizzo 	vm_memattr_t memattr;
1024f9790aebSLuigi Rizzo 	vm_pindex_t pidx;
1025f9790aebSLuigi Rizzo 
1026e2e0ef76SVincenzo Maffione 	nm_prdis("object %p offset %jd prot %d mres %p",
1027f9790aebSLuigi Rizzo 			object, (intmax_t)offset, prot, mres);
1028f9790aebSLuigi Rizzo 	memattr = object->memattr;
1029f9790aebSLuigi Rizzo 	pidx = OFF_TO_IDX(offset);
1030847bf383SLuigi Rizzo 	paddr = netmap_mem_ofstophys(na->nm_mem, offset);
1031f9790aebSLuigi Rizzo 	if (paddr == 0)
1032f9790aebSLuigi Rizzo 		return VM_PAGER_FAIL;
1033f9790aebSLuigi Rizzo 
1034f9790aebSLuigi Rizzo 	if (((*mres)->flags & PG_FICTITIOUS) != 0) {
1035f9790aebSLuigi Rizzo 		/*
1036f9790aebSLuigi Rizzo 		 * If the passed in result page is a fake page, update it with
1037f9790aebSLuigi Rizzo 		 * the new physical address.
1038f9790aebSLuigi Rizzo 		 */
1039f9790aebSLuigi Rizzo 		page = *mres;
1040f9790aebSLuigi Rizzo 		vm_page_updatefake(page, paddr, memattr);
1041f9790aebSLuigi Rizzo 	} else {
1042f9790aebSLuigi Rizzo 		/*
1043f9790aebSLuigi Rizzo 		 * Replace the passed in reqpage page with our own fake page and
1044f9790aebSLuigi Rizzo 		 * free up the all of the original pages.
1045f9790aebSLuigi Rizzo 		 */
1046f9790aebSLuigi Rizzo #ifndef VM_OBJECT_WUNLOCK	/* FreeBSD < 10.x */
1047f9790aebSLuigi Rizzo #define VM_OBJECT_WUNLOCK VM_OBJECT_UNLOCK
1048f9790aebSLuigi Rizzo #define VM_OBJECT_WLOCK	VM_OBJECT_LOCK
1049f9790aebSLuigi Rizzo #endif /* VM_OBJECT_WUNLOCK */
1050f9790aebSLuigi Rizzo 
1051f9790aebSLuigi Rizzo 		VM_OBJECT_WUNLOCK(object);
1052f9790aebSLuigi Rizzo 		page = vm_page_getfake(paddr, memattr);
1053f9790aebSLuigi Rizzo 		VM_OBJECT_WLOCK(object);
1054f9790aebSLuigi Rizzo 		vm_page_lock(*mres);
1055f9790aebSLuigi Rizzo 		vm_page_free(*mres);
1056f9790aebSLuigi Rizzo 		vm_page_unlock(*mres);
1057f9790aebSLuigi Rizzo 		*mres = page;
1058f9790aebSLuigi Rizzo 		vm_page_insert(page, object, pidx);
1059f9790aebSLuigi Rizzo 	}
1060f9790aebSLuigi Rizzo 	page->valid = VM_PAGE_BITS_ALL;
1061f9790aebSLuigi Rizzo 	return (VM_PAGER_OK);
1062f9790aebSLuigi Rizzo }
1063f9790aebSLuigi Rizzo 
1064f9790aebSLuigi Rizzo 
1065f9790aebSLuigi Rizzo static struct cdev_pager_ops netmap_cdev_pager_ops = {
1066f9790aebSLuigi Rizzo 	.cdev_pg_ctor = netmap_dev_pager_ctor,
1067f9790aebSLuigi Rizzo 	.cdev_pg_dtor = netmap_dev_pager_dtor,
1068f9790aebSLuigi Rizzo 	.cdev_pg_fault = netmap_dev_pager_fault,
1069f9790aebSLuigi Rizzo };
1070f9790aebSLuigi Rizzo 
1071f9790aebSLuigi Rizzo 
1072f9790aebSLuigi Rizzo static int
netmap_mmap_single(struct cdev * cdev,vm_ooffset_t * foff,vm_size_t objsize,vm_object_t * objp,int prot)1073f9790aebSLuigi Rizzo netmap_mmap_single(struct cdev *cdev, vm_ooffset_t *foff,
1074f9790aebSLuigi Rizzo 	vm_size_t objsize,  vm_object_t *objp, int prot)
1075f9790aebSLuigi Rizzo {
1076f9790aebSLuigi Rizzo 	int error;
1077f9790aebSLuigi Rizzo 	struct netmap_vm_handle_t *vmh;
1078f9790aebSLuigi Rizzo 	struct netmap_priv_d *priv;
1079f9790aebSLuigi Rizzo 	vm_object_t obj;
1080f9790aebSLuigi Rizzo 
1081f0ea3689SLuigi Rizzo 	if (netmap_verbose)
1082e2e0ef76SVincenzo Maffione 		nm_prinf("cdev %p foff %jd size %jd objp %p prot %d", cdev,
1083f9790aebSLuigi Rizzo 		    (intmax_t )*foff, (intmax_t )objsize, objp, prot);
1084f9790aebSLuigi Rizzo 
1085f9790aebSLuigi Rizzo 	vmh = malloc(sizeof(struct netmap_vm_handle_t), M_DEVBUF,
1086f9790aebSLuigi Rizzo 			      M_NOWAIT | M_ZERO);
1087f9790aebSLuigi Rizzo 	if (vmh == NULL)
1088f9790aebSLuigi Rizzo 		return ENOMEM;
1089f9790aebSLuigi Rizzo 	vmh->dev = cdev;
1090f9790aebSLuigi Rizzo 
1091f9790aebSLuigi Rizzo 	NMG_LOCK();
1092f9790aebSLuigi Rizzo 	error = devfs_get_cdevpriv((void**)&priv);
1093f9790aebSLuigi Rizzo 	if (error)
1094f9790aebSLuigi Rizzo 		goto err_unlock;
1095847bf383SLuigi Rizzo 	if (priv->np_nifp == NULL) {
1096847bf383SLuigi Rizzo 		error = EINVAL;
1097847bf383SLuigi Rizzo 		goto err_unlock;
1098847bf383SLuigi Rizzo 	}
1099f9790aebSLuigi Rizzo 	vmh->priv = priv;
11008fd44c93SLuigi Rizzo 	priv->np_refs++;
1101f9790aebSLuigi Rizzo 	NMG_UNLOCK();
1102f9790aebSLuigi Rizzo 
1103f9790aebSLuigi Rizzo 	obj = cdev_pager_allocate(vmh, OBJT_DEVICE,
1104f9790aebSLuigi Rizzo 		&netmap_cdev_pager_ops, objsize, prot,
1105f9790aebSLuigi Rizzo 		*foff, NULL);
1106f9790aebSLuigi Rizzo 	if (obj == NULL) {
1107e2e0ef76SVincenzo Maffione 		nm_prerr("cdev_pager_allocate failed");
1108f9790aebSLuigi Rizzo 		error = EINVAL;
1109f9790aebSLuigi Rizzo 		goto err_deref;
1110f9790aebSLuigi Rizzo 	}
1111f9790aebSLuigi Rizzo 
1112f9790aebSLuigi Rizzo 	*objp = obj;
1113f9790aebSLuigi Rizzo 	return 0;
1114f9790aebSLuigi Rizzo 
1115f9790aebSLuigi Rizzo err_deref:
1116f9790aebSLuigi Rizzo 	NMG_LOCK();
11178fd44c93SLuigi Rizzo 	priv->np_refs--;
1118f9790aebSLuigi Rizzo err_unlock:
1119f9790aebSLuigi Rizzo 	NMG_UNLOCK();
1120f9790aebSLuigi Rizzo // err:
1121f9790aebSLuigi Rizzo 	free(vmh, M_DEVBUF);
1122f9790aebSLuigi Rizzo 	return error;
1123f9790aebSLuigi Rizzo }
1124f9790aebSLuigi Rizzo 
1125847bf383SLuigi Rizzo /*
11268fd44c93SLuigi Rizzo  * On FreeBSD the close routine is only called on the last close on
11278fd44c93SLuigi Rizzo  * the device (/dev/netmap) so we cannot do anything useful.
11288fd44c93SLuigi Rizzo  * To track close() on individual file descriptors we pass netmap_dtor() to
1129847bf383SLuigi Rizzo  * devfs_set_cdevpriv() on open(). The FreeBSD kernel will call the destructor
1130847bf383SLuigi Rizzo  * when the last fd pointing to the device is closed.
1131847bf383SLuigi Rizzo  *
11328fd44c93SLuigi Rizzo  * Note that FreeBSD does not even munmap() on close() so we also have
11338fd44c93SLuigi Rizzo  * to track mmap() ourselves, and postpone the call to
1134847bf383SLuigi Rizzo  * netmap_dtor() is called when the process has no open fds and no active
1135847bf383SLuigi Rizzo  * memory maps on /dev/netmap, as in linux.
1136847bf383SLuigi Rizzo  */
1137f9790aebSLuigi Rizzo static int
netmap_close(struct cdev * dev,int fflag,int devtype,struct thread * td)1138f9790aebSLuigi Rizzo netmap_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
1139f9790aebSLuigi Rizzo {
1140f9790aebSLuigi Rizzo 	if (netmap_verbose)
1141e2e0ef76SVincenzo Maffione 		nm_prinf("dev %p fflag 0x%x devtype %d td %p",
1142f9790aebSLuigi Rizzo 			dev, fflag, devtype, td);
1143f9790aebSLuigi Rizzo 	return 0;
1144f9790aebSLuigi Rizzo }
1145f9790aebSLuigi Rizzo 
1146f9790aebSLuigi Rizzo 
1147f9790aebSLuigi Rizzo static int
netmap_open(struct cdev * dev,int oflags,int devtype,struct thread * td)1148f9790aebSLuigi Rizzo netmap_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
1149f9790aebSLuigi Rizzo {
1150f9790aebSLuigi Rizzo 	struct netmap_priv_d *priv;
1151f9790aebSLuigi Rizzo 	int error;
1152f9790aebSLuigi Rizzo 
1153f9790aebSLuigi Rizzo 	(void)dev;
1154f9790aebSLuigi Rizzo 	(void)oflags;
1155f9790aebSLuigi Rizzo 	(void)devtype;
1156f9790aebSLuigi Rizzo 	(void)td;
1157f9790aebSLuigi Rizzo 
115837e3a6d3SLuigi Rizzo 	NMG_LOCK();
115937e3a6d3SLuigi Rizzo 	priv = netmap_priv_new();
116037e3a6d3SLuigi Rizzo 	if (priv == NULL) {
116137e3a6d3SLuigi Rizzo 		error = ENOMEM;
116237e3a6d3SLuigi Rizzo 		goto out;
116337e3a6d3SLuigi Rizzo 	}
1164f9790aebSLuigi Rizzo 	error = devfs_set_cdevpriv(priv, netmap_dtor);
11658fd44c93SLuigi Rizzo 	if (error) {
116637e3a6d3SLuigi Rizzo 		netmap_priv_delete(priv);
11678fd44c93SLuigi Rizzo 	}
116837e3a6d3SLuigi Rizzo out:
116937e3a6d3SLuigi Rizzo 	NMG_UNLOCK();
1170f9790aebSLuigi Rizzo 	return error;
1171f9790aebSLuigi Rizzo }
1172f9790aebSLuigi Rizzo 
117337e3a6d3SLuigi Rizzo /******************** kthread wrapper ****************/
117437e3a6d3SLuigi Rizzo #include <sys/sysproto.h>
117537e3a6d3SLuigi Rizzo u_int
nm_os_ncpus(void)117637e3a6d3SLuigi Rizzo nm_os_ncpus(void)
117737e3a6d3SLuigi Rizzo {
117837e3a6d3SLuigi Rizzo 	return mp_maxid + 1;
117937e3a6d3SLuigi Rizzo }
118037e3a6d3SLuigi Rizzo 
1181c3e9b4dbSLuiz Otavio O Souza struct nm_kctx_ctx {
1182b321acabSVincenzo Maffione 	/* Userspace thread (kthread creator). */
1183b321acabSVincenzo Maffione 	struct thread *user_td;
118437e3a6d3SLuigi Rizzo 
118537e3a6d3SLuigi Rizzo 	/* worker function and parameter */
1186c3e9b4dbSLuiz Otavio O Souza 	nm_kctx_worker_fn_t worker_fn;
118737e3a6d3SLuigi Rizzo 	void *worker_private;
118837e3a6d3SLuigi Rizzo 
1189c3e9b4dbSLuiz Otavio O Souza 	struct nm_kctx *nmk;
119037e3a6d3SLuigi Rizzo 
119137e3a6d3SLuigi Rizzo 	/* integer to manage multiple worker contexts (e.g., RX or TX on ptnetmap) */
119237e3a6d3SLuigi Rizzo 	long type;
119337e3a6d3SLuigi Rizzo };
119437e3a6d3SLuigi Rizzo 
1195c3e9b4dbSLuiz Otavio O Souza struct nm_kctx {
119637e3a6d3SLuigi Rizzo 	struct thread *worker;
119737e3a6d3SLuigi Rizzo 	struct mtx worker_lock;
1198c3e9b4dbSLuiz Otavio O Souza 	struct nm_kctx_ctx worker_ctx;
119937e3a6d3SLuigi Rizzo 	int run;			/* used to stop kthread */
120037e3a6d3SLuigi Rizzo 	int attach_user;		/* kthread attached to user_process */
120137e3a6d3SLuigi Rizzo 	int affinity;
120237e3a6d3SLuigi Rizzo };
120337e3a6d3SLuigi Rizzo 
120437e3a6d3SLuigi Rizzo static void
nm_kctx_worker(void * data)1205c3e9b4dbSLuiz Otavio O Souza nm_kctx_worker(void *data)
120637e3a6d3SLuigi Rizzo {
1207c3e9b4dbSLuiz Otavio O Souza 	struct nm_kctx *nmk = data;
1208c3e9b4dbSLuiz Otavio O Souza 	struct nm_kctx_ctx *ctx = &nmk->worker_ctx;
120937e3a6d3SLuigi Rizzo 
121037e3a6d3SLuigi Rizzo 	if (nmk->affinity >= 0) {
121137e3a6d3SLuigi Rizzo 		thread_lock(curthread);
121237e3a6d3SLuigi Rizzo 		sched_bind(curthread, nmk->affinity);
121337e3a6d3SLuigi Rizzo 		thread_unlock(curthread);
121437e3a6d3SLuigi Rizzo 	}
121537e3a6d3SLuigi Rizzo 
121637e3a6d3SLuigi Rizzo 	while (nmk->run) {
121737e3a6d3SLuigi Rizzo 		/*
121837e3a6d3SLuigi Rizzo 		 * check if the parent process dies
121937e3a6d3SLuigi Rizzo 		 * (when kthread is attached to user process)
122037e3a6d3SLuigi Rizzo 		 */
122137e3a6d3SLuigi Rizzo 		if (ctx->user_td) {
122237e3a6d3SLuigi Rizzo 			PROC_LOCK(curproc);
122337e3a6d3SLuigi Rizzo 			thread_suspend_check(0);
122437e3a6d3SLuigi Rizzo 			PROC_UNLOCK(curproc);
122537e3a6d3SLuigi Rizzo 		} else {
122637e3a6d3SLuigi Rizzo 			kthread_suspend_check();
122737e3a6d3SLuigi Rizzo 		}
122837e3a6d3SLuigi Rizzo 
1229b321acabSVincenzo Maffione 		/* Continuously execute worker process. */
1230b321acabSVincenzo Maffione 		ctx->worker_fn(ctx->worker_private); /* worker body */
123137e3a6d3SLuigi Rizzo 	}
123237e3a6d3SLuigi Rizzo 
123337e3a6d3SLuigi Rizzo 	kthread_exit();
123437e3a6d3SLuigi Rizzo }
123537e3a6d3SLuigi Rizzo 
123637e3a6d3SLuigi Rizzo void
nm_os_kctx_worker_setaff(struct nm_kctx * nmk,int affinity)1237c3e9b4dbSLuiz Otavio O Souza nm_os_kctx_worker_setaff(struct nm_kctx *nmk, int affinity)
123837e3a6d3SLuigi Rizzo {
123937e3a6d3SLuigi Rizzo 	nmk->affinity = affinity;
124037e3a6d3SLuigi Rizzo }
124137e3a6d3SLuigi Rizzo 
1242c3e9b4dbSLuiz Otavio O Souza struct nm_kctx *
nm_os_kctx_create(struct nm_kctx_cfg * cfg,void * opaque)12432ff91c17SVincenzo Maffione nm_os_kctx_create(struct nm_kctx_cfg *cfg, void *opaque)
124437e3a6d3SLuigi Rizzo {
1245c3e9b4dbSLuiz Otavio O Souza 	struct nm_kctx *nmk = NULL;
1246844a6f0cSLuigi Rizzo 
124737e3a6d3SLuigi Rizzo 	nmk = malloc(sizeof(*nmk),  M_DEVBUF, M_NOWAIT | M_ZERO);
124837e3a6d3SLuigi Rizzo 	if (!nmk)
124937e3a6d3SLuigi Rizzo 		return NULL;
125037e3a6d3SLuigi Rizzo 
1251869d8878SAdrian Chadd 	mtx_init(&nmk->worker_lock, "nm_kthread lock", NULL, MTX_DEF);
125237e3a6d3SLuigi Rizzo 	nmk->worker_ctx.worker_fn = cfg->worker_fn;
125337e3a6d3SLuigi Rizzo 	nmk->worker_ctx.worker_private = cfg->worker_private;
125437e3a6d3SLuigi Rizzo 	nmk->worker_ctx.type = cfg->type;
125537e3a6d3SLuigi Rizzo 	nmk->affinity = -1;
125637e3a6d3SLuigi Rizzo 
125737e3a6d3SLuigi Rizzo 	/* attach kthread to user process (ptnetmap) */
125837e3a6d3SLuigi Rizzo 	nmk->attach_user = cfg->attach_user;
125937e3a6d3SLuigi Rizzo 
126037e3a6d3SLuigi Rizzo 	return nmk;
126137e3a6d3SLuigi Rizzo }
126237e3a6d3SLuigi Rizzo 
126337e3a6d3SLuigi Rizzo int
nm_os_kctx_worker_start(struct nm_kctx * nmk)1264c3e9b4dbSLuiz Otavio O Souza nm_os_kctx_worker_start(struct nm_kctx *nmk)
126537e3a6d3SLuigi Rizzo {
126637e3a6d3SLuigi Rizzo 	struct proc *p = NULL;
126737e3a6d3SLuigi Rizzo 	int error = 0;
126837e3a6d3SLuigi Rizzo 
1269b321acabSVincenzo Maffione 	/* Temporarily disable this function as it is currently broken
1270b321acabSVincenzo Maffione 	 * and causes kernel crashes. The failure can be triggered by
1271b321acabSVincenzo Maffione 	 * the "vale_polling_enable_disable" test in ctrl-api-test.c. */
1272b321acabSVincenzo Maffione 	return EOPNOTSUPP;
1273b321acabSVincenzo Maffione 
1274b321acabSVincenzo Maffione 	if (nmk->worker)
127537e3a6d3SLuigi Rizzo 		return EBUSY;
127637e3a6d3SLuigi Rizzo 
127737e3a6d3SLuigi Rizzo 	/* check if we want to attach kthread to user process */
127837e3a6d3SLuigi Rizzo 	if (nmk->attach_user) {
127937e3a6d3SLuigi Rizzo 		nmk->worker_ctx.user_td = curthread;
128037e3a6d3SLuigi Rizzo 		p = curthread->td_proc;
128137e3a6d3SLuigi Rizzo 	}
128237e3a6d3SLuigi Rizzo 
128337e3a6d3SLuigi Rizzo 	/* enable kthread main loop */
128437e3a6d3SLuigi Rizzo 	nmk->run = 1;
128537e3a6d3SLuigi Rizzo 	/* create kthread */
1286c3e9b4dbSLuiz Otavio O Souza 	if((error = kthread_add(nm_kctx_worker, nmk, p,
128737e3a6d3SLuigi Rizzo 			&nmk->worker, RFNOWAIT /* to be checked */, 0, "nm-kthread-%ld",
128837e3a6d3SLuigi Rizzo 			nmk->worker_ctx.type))) {
128937e3a6d3SLuigi Rizzo 		goto err;
129037e3a6d3SLuigi Rizzo 	}
129137e3a6d3SLuigi Rizzo 
1292e2e0ef76SVincenzo Maffione 	nm_prinf("nm_kthread started td %p", nmk->worker);
129337e3a6d3SLuigi Rizzo 
129437e3a6d3SLuigi Rizzo 	return 0;
129537e3a6d3SLuigi Rizzo err:
1296e2e0ef76SVincenzo Maffione 	nm_prerr("nm_kthread start failed err %d", error);
129737e3a6d3SLuigi Rizzo 	nmk->worker = NULL;
129837e3a6d3SLuigi Rizzo 	return error;
129937e3a6d3SLuigi Rizzo }
130037e3a6d3SLuigi Rizzo 
130137e3a6d3SLuigi Rizzo void
nm_os_kctx_worker_stop(struct nm_kctx * nmk)1302c3e9b4dbSLuiz Otavio O Souza nm_os_kctx_worker_stop(struct nm_kctx *nmk)
130337e3a6d3SLuigi Rizzo {
1304b321acabSVincenzo Maffione 	if (!nmk->worker)
130537e3a6d3SLuigi Rizzo 		return;
1306b321acabSVincenzo Maffione 
130737e3a6d3SLuigi Rizzo 	/* tell to kthread to exit from main loop */
130837e3a6d3SLuigi Rizzo 	nmk->run = 0;
130937e3a6d3SLuigi Rizzo 
131037e3a6d3SLuigi Rizzo 	/* wake up kthread if it sleeps */
131137e3a6d3SLuigi Rizzo 	kthread_resume(nmk->worker);
131237e3a6d3SLuigi Rizzo 
131337e3a6d3SLuigi Rizzo 	nmk->worker = NULL;
131437e3a6d3SLuigi Rizzo }
131537e3a6d3SLuigi Rizzo 
131637e3a6d3SLuigi Rizzo void
nm_os_kctx_destroy(struct nm_kctx * nmk)1317c3e9b4dbSLuiz Otavio O Souza nm_os_kctx_destroy(struct nm_kctx *nmk)
131837e3a6d3SLuigi Rizzo {
131937e3a6d3SLuigi Rizzo 	if (!nmk)
132037e3a6d3SLuigi Rizzo 		return;
132137e3a6d3SLuigi Rizzo 
1322b321acabSVincenzo Maffione 	if (nmk->worker)
1323b321acabSVincenzo Maffione 		nm_os_kctx_worker_stop(nmk);
132437e3a6d3SLuigi Rizzo 
132537e3a6d3SLuigi Rizzo 	free(nmk, M_DEVBUF);
132637e3a6d3SLuigi Rizzo }
132737e3a6d3SLuigi Rizzo 
1328f0ea3689SLuigi Rizzo /******************** kqueue support ****************/
1329f0ea3689SLuigi Rizzo 
1330f0ea3689SLuigi Rizzo /*
1331a6c48544SVincenzo Maffione  * In addition to calling selwakeuppri(), nm_os_selwakeup() also
1332e8cc65cdSVincenzo Maffione  * needs to call knote() to wake up kqueue listeners.
1333e8cc65cdSVincenzo Maffione  * This operation is deferred to a taskqueue in order to avoid possible
1334e8cc65cdSVincenzo Maffione  * lock order reversals; these may happen because knote() grabs a
1335e8cc65cdSVincenzo Maffione  * private lock associated to the 'si' (see struct selinfo,
1336e8cc65cdSVincenzo Maffione  * struct nm_selinfo, and nm_os_selinfo_init), and nm_os_selwakeup()
1337e8cc65cdSVincenzo Maffione  * can be called while holding the lock associated to a different
1338e8cc65cdSVincenzo Maffione  * 'si'.
1339e8cc65cdSVincenzo Maffione  * When calling knote() we use a non-zero 'hint' argument to inform
1340e8cc65cdSVincenzo Maffione  * the netmap_knrw() function that it is being called from
1341e8cc65cdSVincenzo Maffione  * 'nm_os_selwakeup'; this is necessary because when netmap_knrw() is
1342e8cc65cdSVincenzo Maffione  * called by the kevent subsystem (i.e. kevent_scan()) we also need to
1343e8cc65cdSVincenzo Maffione  * call netmap_poll().
1344f0ea3689SLuigi Rizzo  *
1345a6c48544SVincenzo Maffione  * The netmap_kqfilter() function registers one or another f_event
1346a6c48544SVincenzo Maffione  * depending on read or write mode. A pointer to the struct
1347a6c48544SVincenzo Maffione  * 'netmap_priv_d' is stored into kn->kn_hook, so that it can later
1348a6c48544SVincenzo Maffione  * be passed to netmap_poll(). We pass NULL as a third argument to
1349a6c48544SVincenzo Maffione  * netmap_poll(), so that the latter only runs the txsync/rxsync
1350a6c48544SVincenzo Maffione  * (if necessary), and skips the nm_os_selrecord() calls.
1351f0ea3689SLuigi Rizzo  */
1352f0ea3689SLuigi Rizzo 
1353f0ea3689SLuigi Rizzo 
1354f0ea3689SLuigi Rizzo void
nm_os_selwakeup(struct nm_selinfo * si)135537e3a6d3SLuigi Rizzo nm_os_selwakeup(struct nm_selinfo *si)
1356f0ea3689SLuigi Rizzo {
135737e3a6d3SLuigi Rizzo 	selwakeuppri(&si->si, PI_NET);
1358c328e25cSVincenzo Maffione 	if (si->kqueue_users > 0) {
1359e8cc65cdSVincenzo Maffione 		taskqueue_enqueue(si->ntfytq, &si->ntfytask);
1360f0ea3689SLuigi Rizzo 	}
1361c328e25cSVincenzo Maffione }
1362f0ea3689SLuigi Rizzo 
136337e3a6d3SLuigi Rizzo void
nm_os_selrecord(struct thread * td,struct nm_selinfo * si)136437e3a6d3SLuigi Rizzo nm_os_selrecord(struct thread *td, struct nm_selinfo *si)
136537e3a6d3SLuigi Rizzo {
136637e3a6d3SLuigi Rizzo 	selrecord(td, &si->si);
136737e3a6d3SLuigi Rizzo }
136837e3a6d3SLuigi Rizzo 
1369f0ea3689SLuigi Rizzo static void
netmap_knrdetach(struct knote * kn)1370f0ea3689SLuigi Rizzo netmap_knrdetach(struct knote *kn)
1371f0ea3689SLuigi Rizzo {
1372f0ea3689SLuigi Rizzo 	struct netmap_priv_d *priv = (struct netmap_priv_d *)kn->kn_hook;
1373c328e25cSVincenzo Maffione 	struct nm_selinfo *si = priv->np_si[NR_RX];
1374f0ea3689SLuigi Rizzo 
1375c328e25cSVincenzo Maffione 	knlist_remove(&si->si.si_note, kn, /*islocked=*/0);
1376c328e25cSVincenzo Maffione 	NMG_LOCK();
1377c328e25cSVincenzo Maffione 	KASSERT(si->kqueue_users > 0, ("kqueue_user underflow on %s",
1378c328e25cSVincenzo Maffione 	    si->mtxname));
1379c328e25cSVincenzo Maffione 	si->kqueue_users--;
1380c328e25cSVincenzo Maffione 	nm_prinf("kqueue users for %s: %d", si->mtxname, si->kqueue_users);
1381c328e25cSVincenzo Maffione 	NMG_UNLOCK();
1382f0ea3689SLuigi Rizzo }
1383f0ea3689SLuigi Rizzo 
1384f0ea3689SLuigi Rizzo static void
netmap_knwdetach(struct knote * kn)1385f0ea3689SLuigi Rizzo netmap_knwdetach(struct knote *kn)
1386f0ea3689SLuigi Rizzo {
1387f0ea3689SLuigi Rizzo 	struct netmap_priv_d *priv = (struct netmap_priv_d *)kn->kn_hook;
1388c328e25cSVincenzo Maffione 	struct nm_selinfo *si = priv->np_si[NR_TX];
1389f0ea3689SLuigi Rizzo 
1390c328e25cSVincenzo Maffione 	knlist_remove(&si->si.si_note, kn, /*islocked=*/0);
1391c328e25cSVincenzo Maffione 	NMG_LOCK();
1392c328e25cSVincenzo Maffione 	si->kqueue_users--;
1393c328e25cSVincenzo Maffione 	nm_prinf("kqueue users for %s: %d", si->mtxname, si->kqueue_users);
1394c328e25cSVincenzo Maffione 	NMG_UNLOCK();
1395f0ea3689SLuigi Rizzo }
1396f0ea3689SLuigi Rizzo 
1397f0ea3689SLuigi Rizzo /*
1398a6c48544SVincenzo Maffione  * Callback triggered by netmap notifications (see netmap_notify()),
1399a6c48544SVincenzo Maffione  * and by the application calling kevent(). In the former case we
1400a6c48544SVincenzo Maffione  * just return 1 (events ready), since we are not able to do better.
1401a6c48544SVincenzo Maffione  * In the latter case we use netmap_poll() to see which events are
1402a6c48544SVincenzo Maffione  * ready.
1403f0ea3689SLuigi Rizzo  */
1404f0ea3689SLuigi Rizzo static int
netmap_knrw(struct knote * kn,long hint,int events)1405f0ea3689SLuigi Rizzo netmap_knrw(struct knote *kn, long hint, int events)
1406f0ea3689SLuigi Rizzo {
1407f0ea3689SLuigi Rizzo 	struct netmap_priv_d *priv;
1408f0ea3689SLuigi Rizzo 	int revents;
1409f0ea3689SLuigi Rizzo 
1410f0ea3689SLuigi Rizzo 	if (hint != 0) {
1411a6c48544SVincenzo Maffione 		/* Called from netmap_notify(), typically from a
1412a6c48544SVincenzo Maffione 		 * thread different from the one issuing kevent().
1413a6c48544SVincenzo Maffione 		 * Assume we are ready. */
1414f0ea3689SLuigi Rizzo 		return 1;
1415f0ea3689SLuigi Rizzo 	}
1416a6c48544SVincenzo Maffione 
1417a6c48544SVincenzo Maffione 	/* Called from kevent(). */
1418a6c48544SVincenzo Maffione 	priv = kn->kn_hook;
1419a6c48544SVincenzo Maffione 	revents = netmap_poll(priv, events, /*thread=*/NULL);
1420a6c48544SVincenzo Maffione 
1421a6c48544SVincenzo Maffione 	return (events & revents) ? 1 : 0;
1422f0ea3689SLuigi Rizzo }
1423f0ea3689SLuigi Rizzo 
1424f0ea3689SLuigi Rizzo static int
netmap_knread(struct knote * kn,long hint)1425f0ea3689SLuigi Rizzo netmap_knread(struct knote *kn, long hint)
1426f0ea3689SLuigi Rizzo {
1427f0ea3689SLuigi Rizzo 	return netmap_knrw(kn, hint, POLLIN);
1428f0ea3689SLuigi Rizzo }
1429f0ea3689SLuigi Rizzo 
1430f0ea3689SLuigi Rizzo static int
netmap_knwrite(struct knote * kn,long hint)1431f0ea3689SLuigi Rizzo netmap_knwrite(struct knote *kn, long hint)
1432f0ea3689SLuigi Rizzo {
1433f0ea3689SLuigi Rizzo 	return netmap_knrw(kn, hint, POLLOUT);
1434f0ea3689SLuigi Rizzo }
1435f0ea3689SLuigi Rizzo 
1436f0ea3689SLuigi Rizzo static struct filterops netmap_rfiltops = {
1437f0ea3689SLuigi Rizzo 	.f_isfd = 1,
1438f0ea3689SLuigi Rizzo 	.f_detach = netmap_knrdetach,
1439f0ea3689SLuigi Rizzo 	.f_event = netmap_knread,
1440f0ea3689SLuigi Rizzo };
1441f0ea3689SLuigi Rizzo 
1442f0ea3689SLuigi Rizzo static struct filterops netmap_wfiltops = {
1443f0ea3689SLuigi Rizzo 	.f_isfd = 1,
1444f0ea3689SLuigi Rizzo 	.f_detach = netmap_knwdetach,
1445f0ea3689SLuigi Rizzo 	.f_event = netmap_knwrite,
1446f0ea3689SLuigi Rizzo };
1447f0ea3689SLuigi Rizzo 
1448f0ea3689SLuigi Rizzo 
1449f0ea3689SLuigi Rizzo /*
1450f0ea3689SLuigi Rizzo  * This is called when a thread invokes kevent() to record
1451f0ea3689SLuigi Rizzo  * a change in the configuration of the kqueue().
1452a6c48544SVincenzo Maffione  * The 'priv' is the one associated to the open netmap device.
1453f0ea3689SLuigi Rizzo  */
1454f0ea3689SLuigi Rizzo static int
netmap_kqfilter(struct cdev * dev,struct knote * kn)1455f0ea3689SLuigi Rizzo netmap_kqfilter(struct cdev *dev, struct knote *kn)
1456f0ea3689SLuigi Rizzo {
1457f0ea3689SLuigi Rizzo 	struct netmap_priv_d *priv;
1458f0ea3689SLuigi Rizzo 	int error;
1459f0ea3689SLuigi Rizzo 	struct netmap_adapter *na;
14600e73f29aSLuigi Rizzo 	struct nm_selinfo *si;
1461f0ea3689SLuigi Rizzo 	int ev = kn->kn_filter;
1462f0ea3689SLuigi Rizzo 
1463f0ea3689SLuigi Rizzo 	if (ev != EVFILT_READ && ev != EVFILT_WRITE) {
1464e2e0ef76SVincenzo Maffione 		nm_prerr("bad filter request %d", ev);
1465f0ea3689SLuigi Rizzo 		return 1;
1466f0ea3689SLuigi Rizzo 	}
1467f0ea3689SLuigi Rizzo 	error = devfs_get_cdevpriv((void**)&priv);
1468f0ea3689SLuigi Rizzo 	if (error) {
1469e2e0ef76SVincenzo Maffione 		nm_prerr("device not yet setup");
1470f0ea3689SLuigi Rizzo 		return 1;
1471f0ea3689SLuigi Rizzo 	}
1472f0ea3689SLuigi Rizzo 	na = priv->np_na;
1473f0ea3689SLuigi Rizzo 	if (na == NULL) {
1474e2e0ef76SVincenzo Maffione 		nm_prerr("no netmap adapter for this file descriptor");
1475f0ea3689SLuigi Rizzo 		return 1;
1476f0ea3689SLuigi Rizzo 	}
1477f0ea3689SLuigi Rizzo 	/* the si is indicated in the priv */
1478847bf383SLuigi Rizzo 	si = priv->np_si[(ev == EVFILT_WRITE) ? NR_TX : NR_RX];
1479f0ea3689SLuigi Rizzo 	kn->kn_fop = (ev == EVFILT_WRITE) ?
1480f0ea3689SLuigi Rizzo 		&netmap_wfiltops : &netmap_rfiltops;
1481f0ea3689SLuigi Rizzo 	kn->kn_hook = priv;
1482c328e25cSVincenzo Maffione 	NMG_LOCK();
1483c328e25cSVincenzo Maffione 	si->kqueue_users++;
1484c328e25cSVincenzo Maffione 	nm_prinf("kqueue users for %s: %d", si->mtxname, si->kqueue_users);
1485c328e25cSVincenzo Maffione 	NMG_UNLOCK();
1486a6c48544SVincenzo Maffione 	knlist_add(&si->si.si_note, kn, /*islocked=*/0);
1487a6c48544SVincenzo Maffione 
1488f0ea3689SLuigi Rizzo 	return 0;
1489f0ea3689SLuigi Rizzo }
1490f9790aebSLuigi Rizzo 
149137e3a6d3SLuigi Rizzo static int
freebsd_netmap_poll(struct cdev * cdevi __unused,int events,struct thread * td)149237e3a6d3SLuigi Rizzo freebsd_netmap_poll(struct cdev *cdevi __unused, int events, struct thread *td)
149337e3a6d3SLuigi Rizzo {
149437e3a6d3SLuigi Rizzo 	struct netmap_priv_d *priv;
149537e3a6d3SLuigi Rizzo 	if (devfs_get_cdevpriv((void **)&priv)) {
149637e3a6d3SLuigi Rizzo 		return POLLERR;
149737e3a6d3SLuigi Rizzo 	}
149837e3a6d3SLuigi Rizzo 	return netmap_poll(priv, events, td);
149937e3a6d3SLuigi Rizzo }
150037e3a6d3SLuigi Rizzo 
150137e3a6d3SLuigi Rizzo static int
freebsd_netmap_ioctl(struct cdev * dev __unused,u_long cmd,caddr_t data,int ffla __unused,struct thread * td)150237e3a6d3SLuigi Rizzo freebsd_netmap_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data,
150337e3a6d3SLuigi Rizzo 		int ffla __unused, struct thread *td)
150437e3a6d3SLuigi Rizzo {
150537e3a6d3SLuigi Rizzo 	int error;
150637e3a6d3SLuigi Rizzo 	struct netmap_priv_d *priv;
150737e3a6d3SLuigi Rizzo 
1508ffaa5debSSepherosa Ziehau 	CURVNET_SET(TD_TO_VNET(td));
150937e3a6d3SLuigi Rizzo 	error = devfs_get_cdevpriv((void **)&priv);
151037e3a6d3SLuigi Rizzo 	if (error) {
151137e3a6d3SLuigi Rizzo 		/* XXX ENOENT should be impossible, since the priv
151237e3a6d3SLuigi Rizzo 		 * is now created in the open */
151337e3a6d3SLuigi Rizzo 		if (error == ENOENT)
151437e3a6d3SLuigi Rizzo 			error = ENXIO;
151537e3a6d3SLuigi Rizzo 		goto out;
151637e3a6d3SLuigi Rizzo 	}
15172ff91c17SVincenzo Maffione 	error = netmap_ioctl(priv, cmd, data, td, /*nr_body_is_user=*/1);
151837e3a6d3SLuigi Rizzo out:
151937e3a6d3SLuigi Rizzo 	CURVNET_RESTORE();
152037e3a6d3SLuigi Rizzo 
152137e3a6d3SLuigi Rizzo 	return error;
152237e3a6d3SLuigi Rizzo }
152337e3a6d3SLuigi Rizzo 
1524d740f837SVincenzo Maffione void
nm_os_onattach(struct ifnet * ifp)1525d740f837SVincenzo Maffione nm_os_onattach(struct ifnet *ifp)
1526d740f837SVincenzo Maffione {
1527e95ce845SVincenzo Maffione 	ifp->if_capabilities |= IFCAP_NETMAP;
1528d740f837SVincenzo Maffione }
1529d740f837SVincenzo Maffione 
1530d740f837SVincenzo Maffione void
nm_os_onenter(struct ifnet * ifp)1531d740f837SVincenzo Maffione nm_os_onenter(struct ifnet *ifp)
1532d740f837SVincenzo Maffione {
1533d740f837SVincenzo Maffione 	struct netmap_adapter *na = NA(ifp);
1534d740f837SVincenzo Maffione 
1535d740f837SVincenzo Maffione 	na->if_transmit = ifp->if_transmit;
1536d740f837SVincenzo Maffione 	ifp->if_transmit = netmap_transmit;
1537d740f837SVincenzo Maffione 	ifp->if_capenable |= IFCAP_NETMAP;
1538d740f837SVincenzo Maffione }
1539d740f837SVincenzo Maffione 
1540d740f837SVincenzo Maffione void
nm_os_onexit(struct ifnet * ifp)1541d740f837SVincenzo Maffione nm_os_onexit(struct ifnet *ifp)
1542d740f837SVincenzo Maffione {
1543d740f837SVincenzo Maffione 	struct netmap_adapter *na = NA(ifp);
1544d740f837SVincenzo Maffione 
1545d740f837SVincenzo Maffione 	ifp->if_transmit = na->if_transmit;
1546d740f837SVincenzo Maffione 	ifp->if_capenable &= ~IFCAP_NETMAP;
1547d740f837SVincenzo Maffione }
1548d740f837SVincenzo Maffione 
154937e3a6d3SLuigi Rizzo extern struct cdevsw netmap_cdevsw; /* XXX used in netmap.c, should go elsewhere */
1550f9790aebSLuigi Rizzo struct cdevsw netmap_cdevsw = {
1551f9790aebSLuigi Rizzo 	.d_version = D_VERSION,
1552f9790aebSLuigi Rizzo 	.d_name = "netmap",
1553f9790aebSLuigi Rizzo 	.d_open = netmap_open,
1554f9790aebSLuigi Rizzo 	.d_mmap_single = netmap_mmap_single,
155537e3a6d3SLuigi Rizzo 	.d_ioctl = freebsd_netmap_ioctl,
155637e3a6d3SLuigi Rizzo 	.d_poll = freebsd_netmap_poll,
1557f0ea3689SLuigi Rizzo 	.d_kqfilter = netmap_kqfilter,
1558f9790aebSLuigi Rizzo 	.d_close = netmap_close,
1559f9790aebSLuigi Rizzo };
1560f0ea3689SLuigi Rizzo /*--- end of kqueue support ----*/
1561f9790aebSLuigi Rizzo 
1562f9790aebSLuigi Rizzo /*
1563f9790aebSLuigi Rizzo  * Kernel entry point.
1564f9790aebSLuigi Rizzo  *
1565f9790aebSLuigi Rizzo  * Initialize/finalize the module and return.
1566f9790aebSLuigi Rizzo  *
1567f9790aebSLuigi Rizzo  * Return 0 on success, errno on failure.
1568f9790aebSLuigi Rizzo  */
1569f9790aebSLuigi Rizzo static int
netmap_loader(__unused struct module * module,int event,__unused void * arg)1570f9790aebSLuigi Rizzo netmap_loader(__unused struct module *module, int event, __unused void *arg)
1571f9790aebSLuigi Rizzo {
1572f9790aebSLuigi Rizzo 	int error = 0;
1573f9790aebSLuigi Rizzo 
1574f9790aebSLuigi Rizzo 	switch (event) {
1575f9790aebSLuigi Rizzo 	case MOD_LOAD:
1576f9790aebSLuigi Rizzo 		error = netmap_init();
1577f9790aebSLuigi Rizzo 		break;
1578f9790aebSLuigi Rizzo 
1579f9790aebSLuigi Rizzo 	case MOD_UNLOAD:
1580847adfb7SLuigi Rizzo 		/*
1581847adfb7SLuigi Rizzo 		 * if some one is still using netmap,
1582847adfb7SLuigi Rizzo 		 * then the module can not be unloaded.
1583847adfb7SLuigi Rizzo 		 */
1584847adfb7SLuigi Rizzo 		if (netmap_use_count) {
1585e2e0ef76SVincenzo Maffione 			nm_prerr("netmap module can not be unloaded - netmap_use_count: %d",
1586847adfb7SLuigi Rizzo 					netmap_use_count);
1587847adfb7SLuigi Rizzo 			error = EBUSY;
1588847adfb7SLuigi Rizzo 			break;
1589847adfb7SLuigi Rizzo 		}
1590f9790aebSLuigi Rizzo 		netmap_fini();
1591f9790aebSLuigi Rizzo 		break;
1592f9790aebSLuigi Rizzo 
1593f9790aebSLuigi Rizzo 	default:
1594f9790aebSLuigi Rizzo 		error = EOPNOTSUPP;
1595f9790aebSLuigi Rizzo 		break;
1596f9790aebSLuigi Rizzo 	}
1597f9790aebSLuigi Rizzo 
1598f9790aebSLuigi Rizzo 	return (error);
1599f9790aebSLuigi Rizzo }
1600f9790aebSLuigi Rizzo 
160137e3a6d3SLuigi Rizzo #ifdef DEV_MODULE_ORDERED
160237e3a6d3SLuigi Rizzo /*
160337e3a6d3SLuigi Rizzo  * The netmap module contains three drivers: (i) the netmap character device
160437e3a6d3SLuigi Rizzo  * driver; (ii) the ptnetmap memdev PCI device driver, (iii) the ptnet PCI
160537e3a6d3SLuigi Rizzo  * device driver. The attach() routines of both (ii) and (iii) need the
160637e3a6d3SLuigi Rizzo  * lock of the global allocator, and such lock is initialized in netmap_init(),
160737e3a6d3SLuigi Rizzo  * which is part of (i).
160837e3a6d3SLuigi Rizzo  * Therefore, we make sure that (i) is loaded before (ii) and (iii), using
160937e3a6d3SLuigi Rizzo  * the 'order' parameter of driver declaration macros. For (i), we specify
161037e3a6d3SLuigi Rizzo  * SI_ORDER_MIDDLE, while higher orders are used with the DRIVER_MODULE_ORDERED
161137e3a6d3SLuigi Rizzo  * macros for (ii) and (iii).
161237e3a6d3SLuigi Rizzo  */
161337e3a6d3SLuigi Rizzo DEV_MODULE_ORDERED(netmap, netmap_loader, NULL, SI_ORDER_MIDDLE);
161437e3a6d3SLuigi Rizzo #else /* !DEV_MODULE_ORDERED */
1615f9790aebSLuigi Rizzo DEV_MODULE(netmap, netmap_loader, NULL);
161637e3a6d3SLuigi Rizzo #endif /* DEV_MODULE_ORDERED  */
161737e3a6d3SLuigi Rizzo MODULE_DEPEND(netmap, pci, 1, 1, 1);
1618735c8d95SLuigi Rizzo MODULE_VERSION(netmap, 1);
161937e3a6d3SLuigi Rizzo /* reduce conditional code */
162037e3a6d3SLuigi Rizzo // linux API, use for the knlist in FreeBSD
162137e3a6d3SLuigi Rizzo /* use a private mutex for the knlist */
1622