1*718cf2ccSPedro F. Giffuni /*-
2*718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*718cf2ccSPedro 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 */
61f9790aebSLuigi Rizzo #include <net/if.h>
62f9790aebSLuigi Rizzo #include <net/if_var.h>
634bf50f18SLuigi Rizzo #include <net/if_types.h> /* IFT_ETHER */
644bf50f18SLuigi Rizzo #include <net/ethernet.h> /* ether_ifdetach */
654bf50f18SLuigi Rizzo #include <net/if_dl.h> /* LLADDR */
66f9790aebSLuigi Rizzo #include <machine/bus.h>        /* bus_dmamap_* */
67f0ea3689SLuigi Rizzo #include <netinet/in.h>		/* in6_cksum_pseudo() */
68f0ea3689SLuigi Rizzo #include <machine/in_cksum.h>  /* in_pseudo(), in_cksum_hdr() */
69f9790aebSLuigi Rizzo 
70f9790aebSLuigi Rizzo #include <net/netmap.h>
71f9790aebSLuigi Rizzo #include <dev/netmap/netmap_kern.h>
7237e3a6d3SLuigi Rizzo #include <net/netmap_virt.h>
73f9790aebSLuigi Rizzo #include <dev/netmap/netmap_mem2.h>
74f9790aebSLuigi Rizzo 
75f9790aebSLuigi Rizzo 
76f9790aebSLuigi Rizzo /* ======================== FREEBSD-SPECIFIC ROUTINES ================== */
77f9790aebSLuigi Rizzo 
7837e3a6d3SLuigi Rizzo void nm_os_selinfo_init(NM_SELINFO_T *si) {
7937e3a6d3SLuigi Rizzo 	struct mtx *m = &si->m;
8037e3a6d3SLuigi Rizzo 	mtx_init(m, "nm_kn_lock", NULL, MTX_DEF);
8137e3a6d3SLuigi Rizzo 	knlist_init_mtx(&si->si.si_note, m);
8237e3a6d3SLuigi Rizzo }
8337e3a6d3SLuigi Rizzo 
8437e3a6d3SLuigi Rizzo void
8537e3a6d3SLuigi Rizzo nm_os_selinfo_uninit(NM_SELINFO_T *si)
8637e3a6d3SLuigi Rizzo {
8737e3a6d3SLuigi Rizzo 	/* XXX kqueue(9) needed; these will mirror knlist_init. */
8837e3a6d3SLuigi Rizzo 	knlist_delete(&si->si.si_note, curthread, 0 /* not locked */ );
8937e3a6d3SLuigi Rizzo 	knlist_destroy(&si->si.si_note);
9037e3a6d3SLuigi Rizzo 	/* now we don't need the mutex anymore */
9137e3a6d3SLuigi Rizzo 	mtx_destroy(&si->m);
9237e3a6d3SLuigi Rizzo }
9337e3a6d3SLuigi Rizzo 
94c3e9b4dbSLuiz Otavio O Souza void *
95c3e9b4dbSLuiz Otavio O Souza nm_os_malloc(size_t size)
96c3e9b4dbSLuiz Otavio O Souza {
97c3e9b4dbSLuiz Otavio O Souza 	return malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
98c3e9b4dbSLuiz Otavio O Souza }
99c3e9b4dbSLuiz Otavio O Souza 
100c3e9b4dbSLuiz Otavio O Souza void *
101c3e9b4dbSLuiz Otavio O Souza nm_os_realloc(void *addr, size_t new_size, size_t old_size __unused)
102c3e9b4dbSLuiz Otavio O Souza {
103c3e9b4dbSLuiz Otavio O Souza 	return realloc(addr, new_size, M_DEVBUF, M_NOWAIT | M_ZERO);
104c3e9b4dbSLuiz Otavio O Souza }
105c3e9b4dbSLuiz Otavio O Souza 
106c3e9b4dbSLuiz Otavio O Souza void
107c3e9b4dbSLuiz Otavio O Souza nm_os_free(void *addr)
108c3e9b4dbSLuiz Otavio O Souza {
109c3e9b4dbSLuiz Otavio O Souza 	free(addr, M_DEVBUF);
110c3e9b4dbSLuiz Otavio O Souza }
111c3e9b4dbSLuiz Otavio O Souza 
11237e3a6d3SLuigi Rizzo void
11337e3a6d3SLuigi Rizzo nm_os_ifnet_lock(void)
11437e3a6d3SLuigi Rizzo {
115869d8878SAdrian Chadd 	IFNET_RLOCK();
11637e3a6d3SLuigi Rizzo }
11737e3a6d3SLuigi Rizzo 
11837e3a6d3SLuigi Rizzo void
11937e3a6d3SLuigi Rizzo nm_os_ifnet_unlock(void)
12037e3a6d3SLuigi Rizzo {
12167ca1051SAdrian Chadd 	IFNET_RUNLOCK();
12237e3a6d3SLuigi Rizzo }
12337e3a6d3SLuigi Rizzo 
12437e3a6d3SLuigi Rizzo static int netmap_use_count = 0;
12537e3a6d3SLuigi Rizzo 
12637e3a6d3SLuigi Rizzo void
12737e3a6d3SLuigi Rizzo nm_os_get_module(void)
12837e3a6d3SLuigi Rizzo {
12937e3a6d3SLuigi Rizzo 	netmap_use_count++;
13037e3a6d3SLuigi Rizzo }
13137e3a6d3SLuigi Rizzo 
13237e3a6d3SLuigi Rizzo void
13337e3a6d3SLuigi Rizzo nm_os_put_module(void)
13437e3a6d3SLuigi Rizzo {
13537e3a6d3SLuigi Rizzo 	netmap_use_count--;
13637e3a6d3SLuigi Rizzo }
13737e3a6d3SLuigi Rizzo 
13837e3a6d3SLuigi Rizzo static void
13937e3a6d3SLuigi Rizzo netmap_ifnet_arrival_handler(void *arg __unused, struct ifnet *ifp)
14037e3a6d3SLuigi Rizzo {
14137e3a6d3SLuigi Rizzo         netmap_undo_zombie(ifp);
14237e3a6d3SLuigi Rizzo }
14337e3a6d3SLuigi Rizzo 
14437e3a6d3SLuigi Rizzo static void
14537e3a6d3SLuigi Rizzo netmap_ifnet_departure_handler(void *arg __unused, struct ifnet *ifp)
14637e3a6d3SLuigi Rizzo {
14737e3a6d3SLuigi Rizzo         netmap_make_zombie(ifp);
14837e3a6d3SLuigi Rizzo }
14937e3a6d3SLuigi Rizzo 
15037e3a6d3SLuigi Rizzo static eventhandler_tag nm_ifnet_ah_tag;
15137e3a6d3SLuigi Rizzo static eventhandler_tag nm_ifnet_dh_tag;
15237e3a6d3SLuigi Rizzo 
15337e3a6d3SLuigi Rizzo int
15437e3a6d3SLuigi Rizzo nm_os_ifnet_init(void)
15537e3a6d3SLuigi Rizzo {
15637e3a6d3SLuigi Rizzo         nm_ifnet_ah_tag =
15737e3a6d3SLuigi Rizzo                 EVENTHANDLER_REGISTER(ifnet_arrival_event,
15837e3a6d3SLuigi Rizzo                         netmap_ifnet_arrival_handler,
15937e3a6d3SLuigi Rizzo                         NULL, EVENTHANDLER_PRI_ANY);
16037e3a6d3SLuigi Rizzo         nm_ifnet_dh_tag =
16137e3a6d3SLuigi Rizzo                 EVENTHANDLER_REGISTER(ifnet_departure_event,
16237e3a6d3SLuigi Rizzo                         netmap_ifnet_departure_handler,
16337e3a6d3SLuigi Rizzo                         NULL, EVENTHANDLER_PRI_ANY);
16437e3a6d3SLuigi Rizzo         return 0;
16537e3a6d3SLuigi Rizzo }
16637e3a6d3SLuigi Rizzo 
16737e3a6d3SLuigi Rizzo void
16837e3a6d3SLuigi Rizzo nm_os_ifnet_fini(void)
16937e3a6d3SLuigi Rizzo {
17037e3a6d3SLuigi Rizzo         EVENTHANDLER_DEREGISTER(ifnet_arrival_event,
17137e3a6d3SLuigi Rizzo                 nm_ifnet_ah_tag);
17237e3a6d3SLuigi Rizzo         EVENTHANDLER_DEREGISTER(ifnet_departure_event,
17337e3a6d3SLuigi Rizzo                 nm_ifnet_dh_tag);
17437e3a6d3SLuigi Rizzo }
17537e3a6d3SLuigi Rizzo 
176e4166283SLuigi Rizzo rawsum_t
17737e3a6d3SLuigi Rizzo nm_os_csum_raw(uint8_t *data, size_t len, rawsum_t cur_sum)
178f0ea3689SLuigi Rizzo {
179f0ea3689SLuigi Rizzo 	/* TODO XXX please use the FreeBSD implementation for this. */
180f0ea3689SLuigi Rizzo 	uint16_t *words = (uint16_t *)data;
181f0ea3689SLuigi Rizzo 	int nw = len / 2;
182f0ea3689SLuigi Rizzo 	int i;
183f0ea3689SLuigi Rizzo 
184f0ea3689SLuigi Rizzo 	for (i = 0; i < nw; i++)
185f0ea3689SLuigi Rizzo 		cur_sum += be16toh(words[i]);
186f0ea3689SLuigi Rizzo 
187f0ea3689SLuigi Rizzo 	if (len & 1)
188f0ea3689SLuigi Rizzo 		cur_sum += (data[len-1] << 8);
189f0ea3689SLuigi Rizzo 
190f0ea3689SLuigi Rizzo 	return cur_sum;
191f0ea3689SLuigi Rizzo }
192f0ea3689SLuigi Rizzo 
193f0ea3689SLuigi Rizzo /* Fold a raw checksum: 'cur_sum' is in host byte order, while the
194f0ea3689SLuigi Rizzo  * return value is in network byte order.
195f0ea3689SLuigi Rizzo  */
196e4166283SLuigi Rizzo uint16_t
19737e3a6d3SLuigi Rizzo nm_os_csum_fold(rawsum_t cur_sum)
198f0ea3689SLuigi Rizzo {
199f0ea3689SLuigi Rizzo 	/* TODO XXX please use the FreeBSD implementation for this. */
200f0ea3689SLuigi Rizzo 	while (cur_sum >> 16)
201f0ea3689SLuigi Rizzo 		cur_sum = (cur_sum & 0xFFFF) + (cur_sum >> 16);
202f0ea3689SLuigi Rizzo 
203f0ea3689SLuigi Rizzo 	return htobe16((~cur_sum) & 0xFFFF);
204f0ea3689SLuigi Rizzo }
205f0ea3689SLuigi Rizzo 
20637e3a6d3SLuigi Rizzo uint16_t nm_os_csum_ipv4(struct nm_iphdr *iph)
207f0ea3689SLuigi Rizzo {
208f0ea3689SLuigi Rizzo #if 0
209f0ea3689SLuigi Rizzo 	return in_cksum_hdr((void *)iph);
210f0ea3689SLuigi Rizzo #else
21137e3a6d3SLuigi Rizzo 	return nm_os_csum_fold(nm_os_csum_raw((uint8_t*)iph, sizeof(struct nm_iphdr), 0));
212f0ea3689SLuigi Rizzo #endif
213f0ea3689SLuigi Rizzo }
214f0ea3689SLuigi Rizzo 
215e4166283SLuigi Rizzo void
21637e3a6d3SLuigi Rizzo nm_os_csum_tcpudp_ipv4(struct nm_iphdr *iph, void *data,
217f0ea3689SLuigi Rizzo 					size_t datalen, uint16_t *check)
218f0ea3689SLuigi Rizzo {
2195a067ae1SLuigi Rizzo #ifdef INET
220f0ea3689SLuigi Rizzo 	uint16_t pseudolen = datalen + iph->protocol;
221f0ea3689SLuigi Rizzo 
222f0ea3689SLuigi Rizzo 	/* Compute and insert the pseudo-header cheksum. */
223f0ea3689SLuigi Rizzo 	*check = in_pseudo(iph->saddr, iph->daddr,
224f0ea3689SLuigi Rizzo 				 htobe16(pseudolen));
225f0ea3689SLuigi Rizzo 	/* Compute the checksum on TCP/UDP header + payload
226f0ea3689SLuigi Rizzo 	 * (includes the pseudo-header).
227f0ea3689SLuigi Rizzo 	 */
22837e3a6d3SLuigi Rizzo 	*check = nm_os_csum_fold(nm_os_csum_raw(data, datalen, 0));
2295a067ae1SLuigi Rizzo #else
2305a067ae1SLuigi Rizzo 	static int notsupported = 0;
2315a067ae1SLuigi Rizzo 	if (!notsupported) {
2325a067ae1SLuigi Rizzo 		notsupported = 1;
2335a067ae1SLuigi Rizzo 		D("inet4 segmentation not supported");
2345a067ae1SLuigi Rizzo 	}
2355a067ae1SLuigi Rizzo #endif
236f0ea3689SLuigi Rizzo }
237f0ea3689SLuigi Rizzo 
238e4166283SLuigi Rizzo void
23937e3a6d3SLuigi Rizzo nm_os_csum_tcpudp_ipv6(struct nm_ipv6hdr *ip6h, void *data,
240f0ea3689SLuigi Rizzo 					size_t datalen, uint16_t *check)
241f0ea3689SLuigi Rizzo {
242f0ea3689SLuigi Rizzo #ifdef INET6
243f0ea3689SLuigi Rizzo 	*check = in6_cksum_pseudo((void*)ip6h, datalen, ip6h->nexthdr, 0);
24437e3a6d3SLuigi Rizzo 	*check = nm_os_csum_fold(nm_os_csum_raw(data, datalen, 0));
245f0ea3689SLuigi Rizzo #else
246f0ea3689SLuigi Rizzo 	static int notsupported = 0;
247f0ea3689SLuigi Rizzo 	if (!notsupported) {
248f0ea3689SLuigi Rizzo 		notsupported = 1;
249f0ea3689SLuigi Rizzo 		D("inet6 segmentation not supported");
250f0ea3689SLuigi Rizzo 	}
251f0ea3689SLuigi Rizzo #endif
252f0ea3689SLuigi Rizzo }
253f0ea3689SLuigi Rizzo 
25437e3a6d3SLuigi Rizzo /* on FreeBSD we send up one packet at a time */
25537e3a6d3SLuigi Rizzo void *
25637e3a6d3SLuigi Rizzo nm_os_send_up(struct ifnet *ifp, struct mbuf *m, struct mbuf *prev)
25737e3a6d3SLuigi Rizzo {
25837e3a6d3SLuigi Rizzo 	NA(ifp)->if_input(ifp, m);
25937e3a6d3SLuigi Rizzo 	return NULL;
26037e3a6d3SLuigi Rizzo }
26137e3a6d3SLuigi Rizzo 
26237e3a6d3SLuigi Rizzo int
26337e3a6d3SLuigi Rizzo nm_os_mbuf_has_offld(struct mbuf *m)
26437e3a6d3SLuigi Rizzo {
26537e3a6d3SLuigi Rizzo 	return m->m_pkthdr.csum_flags & (CSUM_TCP | CSUM_UDP | CSUM_SCTP |
26637e3a6d3SLuigi Rizzo 					 CSUM_TCP_IPV6 | CSUM_UDP_IPV6 |
26737e3a6d3SLuigi Rizzo 					 CSUM_SCTP_IPV6 | CSUM_TSO);
26837e3a6d3SLuigi Rizzo }
26937e3a6d3SLuigi Rizzo 
27037e3a6d3SLuigi Rizzo static void
27137e3a6d3SLuigi Rizzo freebsd_generic_rx_handler(struct ifnet *ifp, struct mbuf *m)
27237e3a6d3SLuigi Rizzo {
273c3e9b4dbSLuiz Otavio O Souza 	int stolen;
274c3e9b4dbSLuiz Otavio O Souza 
275c3e9b4dbSLuiz Otavio O Souza 	if (!NM_NA_VALID(ifp)) {
276c3e9b4dbSLuiz Otavio O Souza 		RD(1, "Warning: got RX packet for invalid emulated adapter");
277c3e9b4dbSLuiz Otavio O Souza 		return;
278c3e9b4dbSLuiz Otavio O Souza 	}
279c3e9b4dbSLuiz Otavio O Souza 
280c3e9b4dbSLuiz Otavio O Souza 	stolen = generic_rx_handler(ifp, m);
281c3e9b4dbSLuiz Otavio O Souza 	if (!stolen) {
28237e3a6d3SLuigi Rizzo 		struct netmap_generic_adapter *gna =
28337e3a6d3SLuigi Rizzo 				(struct netmap_generic_adapter *)NA(ifp);
28437e3a6d3SLuigi Rizzo 		gna->save_if_input(ifp, m);
28537e3a6d3SLuigi Rizzo 	}
28637e3a6d3SLuigi Rizzo }
287f0ea3689SLuigi Rizzo 
288f9790aebSLuigi Rizzo /*
289f9790aebSLuigi Rizzo  * Intercept the rx routine in the standard device driver.
290f9790aebSLuigi Rizzo  * Second argument is non-zero to intercept, 0 to restore
291f9790aebSLuigi Rizzo  */
292f9790aebSLuigi Rizzo int
29337e3a6d3SLuigi Rizzo nm_os_catch_rx(struct netmap_generic_adapter *gna, int intercept)
294f9790aebSLuigi Rizzo {
295847bf383SLuigi Rizzo 	struct netmap_adapter *na = &gna->up.up;
296f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
297f9790aebSLuigi Rizzo 
298f9790aebSLuigi Rizzo 	if (intercept) {
299f9790aebSLuigi Rizzo 		if (gna->save_if_input) {
300f9790aebSLuigi Rizzo 			D("cannot intercept again");
301f9790aebSLuigi Rizzo 			return EINVAL; /* already set */
302f9790aebSLuigi Rizzo 		}
303f9790aebSLuigi Rizzo 		gna->save_if_input = ifp->if_input;
30437e3a6d3SLuigi Rizzo 		ifp->if_input = freebsd_generic_rx_handler;
305f9790aebSLuigi Rizzo 	} else {
306f9790aebSLuigi Rizzo 		if (!gna->save_if_input){
307f9790aebSLuigi Rizzo 			D("cannot restore");
308f9790aebSLuigi Rizzo 			return EINVAL;  /* not saved */
309f9790aebSLuigi Rizzo 		}
310f9790aebSLuigi Rizzo 		ifp->if_input = gna->save_if_input;
311f9790aebSLuigi Rizzo 		gna->save_if_input = NULL;
312f9790aebSLuigi Rizzo 	}
313f9790aebSLuigi Rizzo 
314f9790aebSLuigi Rizzo 	return 0;
315f9790aebSLuigi Rizzo }
316f9790aebSLuigi Rizzo 
31717885a7bSLuigi Rizzo 
318f9790aebSLuigi Rizzo /*
319f9790aebSLuigi Rizzo  * Intercept the packet steering routine in the tx path,
320f9790aebSLuigi Rizzo  * so that we can decide which queue is used for an mbuf.
321f9790aebSLuigi Rizzo  * Second argument is non-zero to intercept, 0 to restore.
322f0ea3689SLuigi Rizzo  * On freebsd we just intercept if_transmit.
323f9790aebSLuigi Rizzo  */
32437e3a6d3SLuigi Rizzo int
32537e3a6d3SLuigi Rizzo nm_os_catch_tx(struct netmap_generic_adapter *gna, int intercept)
326f9790aebSLuigi Rizzo {
32717885a7bSLuigi Rizzo 	struct netmap_adapter *na = &gna->up.up;
328847bf383SLuigi Rizzo 	struct ifnet *ifp = netmap_generic_getifp(gna);
32917885a7bSLuigi Rizzo 
33037e3a6d3SLuigi Rizzo 	if (intercept) {
33117885a7bSLuigi Rizzo 		na->if_transmit = ifp->if_transmit;
33217885a7bSLuigi Rizzo 		ifp->if_transmit = netmap_transmit;
333f9790aebSLuigi Rizzo 	} else {
33417885a7bSLuigi Rizzo 		ifp->if_transmit = na->if_transmit;
335f9790aebSLuigi Rizzo 	}
33637e3a6d3SLuigi Rizzo 
33737e3a6d3SLuigi Rizzo 	return 0;
338f9790aebSLuigi Rizzo }
339f9790aebSLuigi Rizzo 
34017885a7bSLuigi Rizzo 
341f0ea3689SLuigi Rizzo /*
342f0ea3689SLuigi Rizzo  * Transmit routine used by generic_netmap_txsync(). Returns 0 on success
343f9790aebSLuigi Rizzo  * and non-zero on error (which may be packet drops or other errors).
344f9790aebSLuigi Rizzo  * addr and len identify the netmap buffer, m is the (preallocated)
345f9790aebSLuigi Rizzo  * mbuf to use for transmissions.
346f9790aebSLuigi Rizzo  *
347f9790aebSLuigi Rizzo  * We should add a reference to the mbuf so the m_freem() at the end
348f9790aebSLuigi Rizzo  * of the transmission does not consume resources.
349f9790aebSLuigi Rizzo  *
350f9790aebSLuigi Rizzo  * On FreeBSD, and on multiqueue cards, we can force the queue using
351c2529042SHans Petter Selasky  *      if (M_HASHTYPE_GET(m) != M_HASHTYPE_NONE)
352f9790aebSLuigi Rizzo  *              i = m->m_pkthdr.flowid % adapter->num_queues;
353f9790aebSLuigi Rizzo  *      else
354f9790aebSLuigi Rizzo  *              i = curcpu % adapter->num_queues;
355f9790aebSLuigi Rizzo  *
356f9790aebSLuigi Rizzo  */
357f9790aebSLuigi Rizzo int
35837e3a6d3SLuigi Rizzo nm_os_generic_xmit_frame(struct nm_os_gen_arg *a)
359f9790aebSLuigi Rizzo {
360f9790aebSLuigi Rizzo 	int ret;
36137e3a6d3SLuigi Rizzo 	u_int len = a->len;
36237e3a6d3SLuigi Rizzo 	struct ifnet *ifp = a->ifp;
36337e3a6d3SLuigi Rizzo 	struct mbuf *m = a->m;
364f9790aebSLuigi Rizzo 
36537e3a6d3SLuigi Rizzo #if __FreeBSD_version < 1100000
366e4166283SLuigi Rizzo 	/*
36737e3a6d3SLuigi Rizzo 	 * Old FreeBSD versions. The mbuf has a cluster attached,
36837e3a6d3SLuigi Rizzo 	 * we need to copy from the cluster to the netmap buffer.
369e4166283SLuigi Rizzo 	 */
37037e3a6d3SLuigi Rizzo 	if (MBUF_REFCNT(m) != 1) {
37137e3a6d3SLuigi Rizzo 		D("invalid refcnt %d for %p", MBUF_REFCNT(m), m);
372e4166283SLuigi Rizzo 		panic("in generic_xmit_frame");
373e4166283SLuigi Rizzo 	}
374e4166283SLuigi Rizzo 	if (m->m_ext.ext_size < len) {
375e4166283SLuigi Rizzo 		RD(5, "size %d < len %d", m->m_ext.ext_size, len);
376e4166283SLuigi Rizzo 		len = m->m_ext.ext_size;
377e4166283SLuigi Rizzo 	}
37837e3a6d3SLuigi Rizzo 	bcopy(a->addr, m->m_data, len);
37937e3a6d3SLuigi Rizzo #else  /* __FreeBSD_version >= 1100000 */
38037e3a6d3SLuigi Rizzo 	/* New FreeBSD versions. Link the external storage to
38137e3a6d3SLuigi Rizzo 	 * the netmap buffer, so that no copy is necessary. */
38237e3a6d3SLuigi Rizzo 	m->m_ext.ext_buf = m->m_data = a->addr;
38337e3a6d3SLuigi Rizzo 	m->m_ext.ext_size = len;
38437e3a6d3SLuigi Rizzo #endif /* __FreeBSD_version >= 1100000 */
38537e3a6d3SLuigi Rizzo 
386e4166283SLuigi Rizzo 	m->m_len = m->m_pkthdr.len = len;
38737e3a6d3SLuigi Rizzo 
38837e3a6d3SLuigi Rizzo 	/* mbuf refcnt is not contended, no need to use atomic
38937e3a6d3SLuigi Rizzo 	 * (a memory barrier is enough). */
39037e3a6d3SLuigi Rizzo 	SET_MBUF_REFCNT(m, 2);
391c2529042SHans Petter Selasky 	M_HASHTYPE_SET(m, M_HASHTYPE_OPAQUE);
39237e3a6d3SLuigi Rizzo 	m->m_pkthdr.flowid = a->ring_nr;
393f9790aebSLuigi Rizzo 	m->m_pkthdr.rcvif = ifp; /* used for tx notification */
39417885a7bSLuigi Rizzo 	ret = NA(ifp)->if_transmit(ifp, m);
39537e3a6d3SLuigi Rizzo 	return ret ? -1 : 0;
396f9790aebSLuigi Rizzo }
397f9790aebSLuigi Rizzo 
39817885a7bSLuigi Rizzo 
3990dc809c0SLuigi Rizzo #if __FreeBSD_version >= 1100005
4000dc809c0SLuigi Rizzo struct netmap_adapter *
4010dc809c0SLuigi Rizzo netmap_getna(if_t ifp)
4020dc809c0SLuigi Rizzo {
4030dc809c0SLuigi Rizzo 	return (NA((struct ifnet *)ifp));
4040dc809c0SLuigi Rizzo }
4050dc809c0SLuigi Rizzo #endif /* __FreeBSD_version >= 1100005 */
4060dc809c0SLuigi Rizzo 
407f9790aebSLuigi Rizzo /*
408f9790aebSLuigi Rizzo  * The following two functions are empty until we have a generic
409f9790aebSLuigi Rizzo  * way to extract the info from the ifp
410f9790aebSLuigi Rizzo  */
411f9790aebSLuigi Rizzo int
41237e3a6d3SLuigi Rizzo nm_os_generic_find_num_desc(struct ifnet *ifp, unsigned int *tx, unsigned int *rx)
413f9790aebSLuigi Rizzo {
414f9790aebSLuigi Rizzo 	return 0;
415f9790aebSLuigi Rizzo }
416f9790aebSLuigi Rizzo 
41717885a7bSLuigi Rizzo 
418f9790aebSLuigi Rizzo void
41937e3a6d3SLuigi Rizzo nm_os_generic_find_num_queues(struct ifnet *ifp, u_int *txq, u_int *rxq)
420f9790aebSLuigi Rizzo {
421c3e9b4dbSLuiz Otavio O Souza 	unsigned num_rings = netmap_generic_rings ? netmap_generic_rings : 1;
422c3e9b4dbSLuiz Otavio O Souza 
423c3e9b4dbSLuiz Otavio O Souza 	*txq = num_rings;
424c3e9b4dbSLuiz Otavio O Souza 	*rxq = num_rings;
425f9790aebSLuigi Rizzo }
426f9790aebSLuigi Rizzo 
42737e3a6d3SLuigi Rizzo void
42837e3a6d3SLuigi Rizzo nm_os_generic_set_features(struct netmap_generic_adapter *gna)
42937e3a6d3SLuigi Rizzo {
43037e3a6d3SLuigi Rizzo 
43137e3a6d3SLuigi Rizzo 	gna->rxsg = 1; /* Supported through m_copydata. */
43237e3a6d3SLuigi Rizzo 	gna->txqdisc = 0; /* Not supported. */
43337e3a6d3SLuigi Rizzo }
43417885a7bSLuigi Rizzo 
435e4166283SLuigi Rizzo void
43637e3a6d3SLuigi Rizzo nm_os_mitigation_init(struct nm_generic_mit *mit, int idx, struct netmap_adapter *na)
437f9790aebSLuigi Rizzo {
438f9790aebSLuigi Rizzo 	ND("called");
439f0ea3689SLuigi Rizzo 	mit->mit_pending = 0;
4404bf50f18SLuigi Rizzo 	mit->mit_ring_idx = idx;
441f0ea3689SLuigi Rizzo 	mit->mit_na = na;
442f9790aebSLuigi Rizzo }
443f9790aebSLuigi Rizzo 
444f9790aebSLuigi Rizzo 
445e4166283SLuigi Rizzo void
44637e3a6d3SLuigi Rizzo nm_os_mitigation_start(struct nm_generic_mit *mit)
447f9790aebSLuigi Rizzo {
448f9790aebSLuigi Rizzo 	ND("called");
449f9790aebSLuigi Rizzo }
450f9790aebSLuigi Rizzo 
45117885a7bSLuigi Rizzo 
452e4166283SLuigi Rizzo void
45337e3a6d3SLuigi Rizzo nm_os_mitigation_restart(struct nm_generic_mit *mit)
454f9790aebSLuigi Rizzo {
455f9790aebSLuigi Rizzo 	ND("called");
456f9790aebSLuigi Rizzo }
457f9790aebSLuigi Rizzo 
45817885a7bSLuigi Rizzo 
459e4166283SLuigi Rizzo int
46037e3a6d3SLuigi Rizzo nm_os_mitigation_active(struct nm_generic_mit *mit)
461f9790aebSLuigi Rizzo {
462f9790aebSLuigi Rizzo 	ND("called");
463f9790aebSLuigi Rizzo 	return 0;
464f9790aebSLuigi Rizzo }
465f9790aebSLuigi Rizzo 
46617885a7bSLuigi Rizzo 
467e4166283SLuigi Rizzo void
46837e3a6d3SLuigi Rizzo nm_os_mitigation_cleanup(struct nm_generic_mit *mit)
469f9790aebSLuigi Rizzo {
470f9790aebSLuigi Rizzo 	ND("called");
471f9790aebSLuigi Rizzo }
472f9790aebSLuigi Rizzo 
4734bf50f18SLuigi Rizzo static int
4744bf50f18SLuigi Rizzo nm_vi_dummy(struct ifnet *ifp, u_long cmd, caddr_t addr)
4754bf50f18SLuigi Rizzo {
4764bf50f18SLuigi Rizzo 	return EINVAL;
4774bf50f18SLuigi Rizzo }
4784bf50f18SLuigi Rizzo 
4794bf50f18SLuigi Rizzo static void
4804bf50f18SLuigi Rizzo nm_vi_start(struct ifnet *ifp)
4814bf50f18SLuigi Rizzo {
4824bf50f18SLuigi Rizzo 	panic("nm_vi_start() must not be called");
4834bf50f18SLuigi Rizzo }
4844bf50f18SLuigi Rizzo 
4854bf50f18SLuigi Rizzo /*
4864bf50f18SLuigi Rizzo  * Index manager of persistent virtual interfaces.
4874bf50f18SLuigi Rizzo  * It is used to decide the lowest byte of the MAC address.
4884bf50f18SLuigi Rizzo  * We use the same algorithm with management of bridge port index.
4894bf50f18SLuigi Rizzo  */
4904bf50f18SLuigi Rizzo #define NM_VI_MAX	255
4914bf50f18SLuigi Rizzo static struct {
4924bf50f18SLuigi Rizzo 	uint8_t index[NM_VI_MAX]; /* XXX just for a reasonable number */
4934bf50f18SLuigi Rizzo 	uint8_t active;
4944bf50f18SLuigi Rizzo 	struct mtx lock;
4954bf50f18SLuigi Rizzo } nm_vi_indices;
4964bf50f18SLuigi Rizzo 
4974bf50f18SLuigi Rizzo void
49837e3a6d3SLuigi Rizzo nm_os_vi_init_index(void)
4994bf50f18SLuigi Rizzo {
5004bf50f18SLuigi Rizzo 	int i;
5014bf50f18SLuigi Rizzo 	for (i = 0; i < NM_VI_MAX; i++)
5024bf50f18SLuigi Rizzo 		nm_vi_indices.index[i] = i;
5034bf50f18SLuigi Rizzo 	nm_vi_indices.active = 0;
5044bf50f18SLuigi Rizzo 	mtx_init(&nm_vi_indices.lock, "nm_vi_indices_lock", NULL, MTX_DEF);
5054bf50f18SLuigi Rizzo }
5064bf50f18SLuigi Rizzo 
5074bf50f18SLuigi Rizzo /* return -1 if no index available */
5084bf50f18SLuigi Rizzo static int
5094bf50f18SLuigi Rizzo nm_vi_get_index(void)
5104bf50f18SLuigi Rizzo {
5114bf50f18SLuigi Rizzo 	int ret;
5124bf50f18SLuigi Rizzo 
5134bf50f18SLuigi Rizzo 	mtx_lock(&nm_vi_indices.lock);
5144bf50f18SLuigi Rizzo 	ret = nm_vi_indices.active == NM_VI_MAX ? -1 :
5154bf50f18SLuigi Rizzo 		nm_vi_indices.index[nm_vi_indices.active++];
5164bf50f18SLuigi Rizzo 	mtx_unlock(&nm_vi_indices.lock);
5174bf50f18SLuigi Rizzo 	return ret;
5184bf50f18SLuigi Rizzo }
5194bf50f18SLuigi Rizzo 
5204bf50f18SLuigi Rizzo static void
5214bf50f18SLuigi Rizzo nm_vi_free_index(uint8_t val)
5224bf50f18SLuigi Rizzo {
5234bf50f18SLuigi Rizzo 	int i, lim;
5244bf50f18SLuigi Rizzo 
5254bf50f18SLuigi Rizzo 	mtx_lock(&nm_vi_indices.lock);
5264bf50f18SLuigi Rizzo 	lim = nm_vi_indices.active;
5274bf50f18SLuigi Rizzo 	for (i = 0; i < lim; i++) {
5284bf50f18SLuigi Rizzo 		if (nm_vi_indices.index[i] == val) {
5294bf50f18SLuigi Rizzo 			/* swap index[lim-1] and j */
5304bf50f18SLuigi Rizzo 			int tmp = nm_vi_indices.index[lim-1];
5314bf50f18SLuigi Rizzo 			nm_vi_indices.index[lim-1] = val;
5324bf50f18SLuigi Rizzo 			nm_vi_indices.index[i] = tmp;
5334bf50f18SLuigi Rizzo 			nm_vi_indices.active--;
5344bf50f18SLuigi Rizzo 			break;
5354bf50f18SLuigi Rizzo 		}
5364bf50f18SLuigi Rizzo 	}
5374bf50f18SLuigi Rizzo 	if (lim == nm_vi_indices.active)
5384bf50f18SLuigi Rizzo 		D("funny, index %u didn't found", val);
5394bf50f18SLuigi Rizzo 	mtx_unlock(&nm_vi_indices.lock);
5404bf50f18SLuigi Rizzo }
5414bf50f18SLuigi Rizzo #undef NM_VI_MAX
5424bf50f18SLuigi Rizzo 
5434bf50f18SLuigi Rizzo /*
5444bf50f18SLuigi Rizzo  * Implementation of a netmap-capable virtual interface that
5454bf50f18SLuigi Rizzo  * registered to the system.
5464bf50f18SLuigi Rizzo  * It is based on if_tap.c and ip_fw_log.c in FreeBSD 9.
5474bf50f18SLuigi Rizzo  *
5484bf50f18SLuigi Rizzo  * Note: Linux sets refcount to 0 on allocation of net_device,
5494bf50f18SLuigi Rizzo  * then increments it on registration to the system.
5504bf50f18SLuigi Rizzo  * FreeBSD sets refcount to 1 on if_alloc(), and does not
5514bf50f18SLuigi Rizzo  * increment this refcount on if_attach().
5524bf50f18SLuigi Rizzo  */
5534bf50f18SLuigi Rizzo int
55437e3a6d3SLuigi Rizzo nm_os_vi_persist(const char *name, struct ifnet **ret)
5554bf50f18SLuigi Rizzo {
5564bf50f18SLuigi Rizzo 	struct ifnet *ifp;
5574bf50f18SLuigi Rizzo 	u_short macaddr_hi;
5584bf50f18SLuigi Rizzo 	uint32_t macaddr_mid;
5594bf50f18SLuigi Rizzo 	u_char eaddr[6];
5604bf50f18SLuigi Rizzo 	int unit = nm_vi_get_index(); /* just to decide MAC address */
5614bf50f18SLuigi Rizzo 
5624bf50f18SLuigi Rizzo 	if (unit < 0)
5634bf50f18SLuigi Rizzo 		return EBUSY;
5644bf50f18SLuigi Rizzo 	/*
5654bf50f18SLuigi Rizzo 	 * We use the same MAC address generation method with tap
5664bf50f18SLuigi Rizzo 	 * except for the highest octet is 00:be instead of 00:bd
5674bf50f18SLuigi Rizzo 	 */
5684bf50f18SLuigi Rizzo 	macaddr_hi = htons(0x00be); /* XXX tap + 1 */
5694bf50f18SLuigi Rizzo 	macaddr_mid = (uint32_t) ticks;
5704bf50f18SLuigi Rizzo 	bcopy(&macaddr_hi, eaddr, sizeof(short));
5714bf50f18SLuigi Rizzo 	bcopy(&macaddr_mid, &eaddr[2], sizeof(uint32_t));
5724bf50f18SLuigi Rizzo 	eaddr[5] = (uint8_t)unit;
5734bf50f18SLuigi Rizzo 
5744bf50f18SLuigi Rizzo 	ifp = if_alloc(IFT_ETHER);
5754bf50f18SLuigi Rizzo 	if (ifp == NULL) {
5764bf50f18SLuigi Rizzo 		D("if_alloc failed");
5774bf50f18SLuigi Rizzo 		return ENOMEM;
5784bf50f18SLuigi Rizzo 	}
5794bf50f18SLuigi Rizzo 	if_initname(ifp, name, IF_DUNIT_NONE);
5804bf50f18SLuigi Rizzo 	ifp->if_mtu = 65536;
5814bf50f18SLuigi Rizzo 	ifp->if_flags = IFF_UP | IFF_SIMPLEX | IFF_MULTICAST;
5824bf50f18SLuigi Rizzo 	ifp->if_init = (void *)nm_vi_dummy;
5834bf50f18SLuigi Rizzo 	ifp->if_ioctl = nm_vi_dummy;
5844bf50f18SLuigi Rizzo 	ifp->if_start = nm_vi_start;
5854bf50f18SLuigi Rizzo 	ifp->if_mtu = ETHERMTU;
5864bf50f18SLuigi Rizzo 	IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
5874bf50f18SLuigi Rizzo 	ifp->if_capabilities |= IFCAP_LINKSTATE;
5884bf50f18SLuigi Rizzo 	ifp->if_capenable |= IFCAP_LINKSTATE;
5894bf50f18SLuigi Rizzo 
5904bf50f18SLuigi Rizzo 	ether_ifattach(ifp, eaddr);
5914bf50f18SLuigi Rizzo 	*ret = ifp;
5924bf50f18SLuigi Rizzo 	return 0;
5934bf50f18SLuigi Rizzo }
59437e3a6d3SLuigi Rizzo 
5954bf50f18SLuigi Rizzo /* unregister from the system and drop the final refcount */
5964bf50f18SLuigi Rizzo void
59737e3a6d3SLuigi Rizzo nm_os_vi_detach(struct ifnet *ifp)
5984bf50f18SLuigi Rizzo {
5994bf50f18SLuigi Rizzo 	nm_vi_free_index(((char *)IF_LLADDR(ifp))[5]);
6004bf50f18SLuigi Rizzo 	ether_ifdetach(ifp);
6014bf50f18SLuigi Rizzo 	if_free(ifp);
6024bf50f18SLuigi Rizzo }
60317885a7bSLuigi Rizzo 
60437e3a6d3SLuigi Rizzo /* ======================== PTNETMAP SUPPORT ========================== */
60537e3a6d3SLuigi Rizzo 
60637e3a6d3SLuigi Rizzo #ifdef WITH_PTNETMAP_GUEST
60737e3a6d3SLuigi Rizzo #include <sys/bus.h>
60837e3a6d3SLuigi Rizzo #include <sys/rman.h>
60937e3a6d3SLuigi Rizzo #include <machine/bus.h>        /* bus_dmamap_* */
61037e3a6d3SLuigi Rizzo #include <machine/resource.h>
61137e3a6d3SLuigi Rizzo #include <dev/pci/pcivar.h>
61237e3a6d3SLuigi Rizzo #include <dev/pci/pcireg.h>
61337e3a6d3SLuigi Rizzo /*
61437e3a6d3SLuigi Rizzo  * ptnetmap memory device (memdev) for freebsd guest,
61537e3a6d3SLuigi Rizzo  * ssed to expose host netmap memory to the guest through a PCI BAR.
61637e3a6d3SLuigi Rizzo  */
61737e3a6d3SLuigi Rizzo 
61837e3a6d3SLuigi Rizzo /*
61937e3a6d3SLuigi Rizzo  * ptnetmap memdev private data structure
62037e3a6d3SLuigi Rizzo  */
62137e3a6d3SLuigi Rizzo struct ptnetmap_memdev {
62237e3a6d3SLuigi Rizzo 	device_t dev;
62337e3a6d3SLuigi Rizzo 	struct resource *pci_io;
62437e3a6d3SLuigi Rizzo 	struct resource *pci_mem;
62537e3a6d3SLuigi Rizzo 	struct netmap_mem_d *nm_mem;
62637e3a6d3SLuigi Rizzo };
62737e3a6d3SLuigi Rizzo 
62837e3a6d3SLuigi Rizzo static int	ptn_memdev_probe(device_t);
62937e3a6d3SLuigi Rizzo static int	ptn_memdev_attach(device_t);
63037e3a6d3SLuigi Rizzo static int	ptn_memdev_detach(device_t);
63137e3a6d3SLuigi Rizzo static int	ptn_memdev_shutdown(device_t);
63237e3a6d3SLuigi Rizzo 
63337e3a6d3SLuigi Rizzo static device_method_t ptn_memdev_methods[] = {
63437e3a6d3SLuigi Rizzo 	DEVMETHOD(device_probe, ptn_memdev_probe),
63537e3a6d3SLuigi Rizzo 	DEVMETHOD(device_attach, ptn_memdev_attach),
63637e3a6d3SLuigi Rizzo 	DEVMETHOD(device_detach, ptn_memdev_detach),
63737e3a6d3SLuigi Rizzo 	DEVMETHOD(device_shutdown, ptn_memdev_shutdown),
63837e3a6d3SLuigi Rizzo 	DEVMETHOD_END
63937e3a6d3SLuigi Rizzo };
64037e3a6d3SLuigi Rizzo 
64137e3a6d3SLuigi Rizzo static driver_t ptn_memdev_driver = {
64237e3a6d3SLuigi Rizzo 	PTNETMAP_MEMDEV_NAME,
64337e3a6d3SLuigi Rizzo 	ptn_memdev_methods,
64437e3a6d3SLuigi Rizzo 	sizeof(struct ptnetmap_memdev),
64537e3a6d3SLuigi Rizzo };
64637e3a6d3SLuigi Rizzo 
64737e3a6d3SLuigi Rizzo /* We use (SI_ORDER_MIDDLE+1) here, see DEV_MODULE_ORDERED() invocation
64837e3a6d3SLuigi Rizzo  * below. */
64937e3a6d3SLuigi Rizzo static devclass_t ptnetmap_devclass;
65037e3a6d3SLuigi Rizzo DRIVER_MODULE_ORDERED(ptn_memdev, pci, ptn_memdev_driver, ptnetmap_devclass,
65137e3a6d3SLuigi Rizzo 		      NULL, NULL, SI_ORDER_MIDDLE + 1);
65237e3a6d3SLuigi Rizzo 
65337e3a6d3SLuigi Rizzo /*
65437e3a6d3SLuigi Rizzo  * Map host netmap memory through PCI-BAR in the guest OS,
65537e3a6d3SLuigi Rizzo  * returning physical (nm_paddr) and virtual (nm_addr) addresses
65637e3a6d3SLuigi Rizzo  * of the netmap memory mapped in the guest.
65737e3a6d3SLuigi Rizzo  */
65837e3a6d3SLuigi Rizzo int
659a2a74091SLuigi Rizzo nm_os_pt_memdev_iomap(struct ptnetmap_memdev *ptn_dev, vm_paddr_t *nm_paddr,
660844a6f0cSLuigi Rizzo 		      void **nm_addr, uint64_t *mem_size)
66137e3a6d3SLuigi Rizzo {
66237e3a6d3SLuigi Rizzo 	int rid;
66337e3a6d3SLuigi Rizzo 
66437e3a6d3SLuigi Rizzo 	D("ptn_memdev_driver iomap");
66537e3a6d3SLuigi Rizzo 
66637e3a6d3SLuigi Rizzo 	rid = PCIR_BAR(PTNETMAP_MEM_PCI_BAR);
667844a6f0cSLuigi Rizzo 	*mem_size = bus_read_4(ptn_dev->pci_io, PTNET_MDEV_IO_MEMSIZE_HI);
668844a6f0cSLuigi Rizzo 	*mem_size = bus_read_4(ptn_dev->pci_io, PTNET_MDEV_IO_MEMSIZE_LO) |
669844a6f0cSLuigi Rizzo 			(*mem_size << 32);
67037e3a6d3SLuigi Rizzo 
67137e3a6d3SLuigi Rizzo 	/* map memory allocator */
67237e3a6d3SLuigi Rizzo 	ptn_dev->pci_mem = bus_alloc_resource(ptn_dev->dev, SYS_RES_MEMORY,
673844a6f0cSLuigi Rizzo 			&rid, 0, ~0, *mem_size, RF_ACTIVE);
67437e3a6d3SLuigi Rizzo 	if (ptn_dev->pci_mem == NULL) {
67537e3a6d3SLuigi Rizzo 		*nm_paddr = 0;
6764dd44461SLuiz Otavio O Souza 		*nm_addr = NULL;
67737e3a6d3SLuigi Rizzo 		return ENOMEM;
67837e3a6d3SLuigi Rizzo 	}
67937e3a6d3SLuigi Rizzo 
68037e3a6d3SLuigi Rizzo 	*nm_paddr = rman_get_start(ptn_dev->pci_mem);
68137e3a6d3SLuigi Rizzo 	*nm_addr = rman_get_virtual(ptn_dev->pci_mem);
68237e3a6d3SLuigi Rizzo 
683844a6f0cSLuigi Rizzo 	D("=== BAR %d start %lx len %lx mem_size %lx ===",
68437e3a6d3SLuigi Rizzo 			PTNETMAP_MEM_PCI_BAR,
685a2a74091SLuigi Rizzo 			(unsigned long)(*nm_paddr),
686a2a74091SLuigi Rizzo 			(unsigned long)rman_get_size(ptn_dev->pci_mem),
687844a6f0cSLuigi Rizzo 			(unsigned long)*mem_size);
68837e3a6d3SLuigi Rizzo 	return (0);
68937e3a6d3SLuigi Rizzo }
69037e3a6d3SLuigi Rizzo 
691844a6f0cSLuigi Rizzo uint32_t
692844a6f0cSLuigi Rizzo nm_os_pt_memdev_ioread(struct ptnetmap_memdev *ptn_dev, unsigned int reg)
693844a6f0cSLuigi Rizzo {
694844a6f0cSLuigi Rizzo 	return bus_read_4(ptn_dev->pci_io, reg);
695844a6f0cSLuigi Rizzo }
696844a6f0cSLuigi Rizzo 
69737e3a6d3SLuigi Rizzo /* Unmap host netmap memory. */
69837e3a6d3SLuigi Rizzo void
69937e3a6d3SLuigi Rizzo nm_os_pt_memdev_iounmap(struct ptnetmap_memdev *ptn_dev)
70037e3a6d3SLuigi Rizzo {
70137e3a6d3SLuigi Rizzo 	D("ptn_memdev_driver iounmap");
70237e3a6d3SLuigi Rizzo 
70337e3a6d3SLuigi Rizzo 	if (ptn_dev->pci_mem) {
70437e3a6d3SLuigi Rizzo 		bus_release_resource(ptn_dev->dev, SYS_RES_MEMORY,
70537e3a6d3SLuigi Rizzo 			PCIR_BAR(PTNETMAP_MEM_PCI_BAR), ptn_dev->pci_mem);
70637e3a6d3SLuigi Rizzo 		ptn_dev->pci_mem = NULL;
70737e3a6d3SLuigi Rizzo 	}
70837e3a6d3SLuigi Rizzo }
70937e3a6d3SLuigi Rizzo 
71037e3a6d3SLuigi Rizzo /* Device identification routine, return BUS_PROBE_DEFAULT on success,
71137e3a6d3SLuigi Rizzo  * positive on failure */
71237e3a6d3SLuigi Rizzo static int
71337e3a6d3SLuigi Rizzo ptn_memdev_probe(device_t dev)
71437e3a6d3SLuigi Rizzo {
71537e3a6d3SLuigi Rizzo 	char desc[256];
71637e3a6d3SLuigi Rizzo 
71737e3a6d3SLuigi Rizzo 	if (pci_get_vendor(dev) != PTNETMAP_PCI_VENDOR_ID)
71837e3a6d3SLuigi Rizzo 		return (ENXIO);
71937e3a6d3SLuigi Rizzo 	if (pci_get_device(dev) != PTNETMAP_PCI_DEVICE_ID)
72037e3a6d3SLuigi Rizzo 		return (ENXIO);
72137e3a6d3SLuigi Rizzo 
72237e3a6d3SLuigi Rizzo 	snprintf(desc, sizeof(desc), "%s PCI adapter",
72337e3a6d3SLuigi Rizzo 			PTNETMAP_MEMDEV_NAME);
72437e3a6d3SLuigi Rizzo 	device_set_desc_copy(dev, desc);
72537e3a6d3SLuigi Rizzo 
72637e3a6d3SLuigi Rizzo 	return (BUS_PROBE_DEFAULT);
72737e3a6d3SLuigi Rizzo }
72837e3a6d3SLuigi Rizzo 
72937e3a6d3SLuigi Rizzo /* Device initialization routine. */
73037e3a6d3SLuigi Rizzo static int
73137e3a6d3SLuigi Rizzo ptn_memdev_attach(device_t dev)
73237e3a6d3SLuigi Rizzo {
73337e3a6d3SLuigi Rizzo 	struct ptnetmap_memdev *ptn_dev;
73437e3a6d3SLuigi Rizzo 	int rid;
73537e3a6d3SLuigi Rizzo 	uint16_t mem_id;
73637e3a6d3SLuigi Rizzo 
73737e3a6d3SLuigi Rizzo 	D("ptn_memdev_driver attach");
73837e3a6d3SLuigi Rizzo 
73937e3a6d3SLuigi Rizzo 	ptn_dev = device_get_softc(dev);
74037e3a6d3SLuigi Rizzo 	ptn_dev->dev = dev;
74137e3a6d3SLuigi Rizzo 
74237e3a6d3SLuigi Rizzo 	pci_enable_busmaster(dev);
74337e3a6d3SLuigi Rizzo 
74437e3a6d3SLuigi Rizzo 	rid = PCIR_BAR(PTNETMAP_IO_PCI_BAR);
74537e3a6d3SLuigi Rizzo 	ptn_dev->pci_io = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
74637e3a6d3SLuigi Rizzo 						 RF_ACTIVE);
74737e3a6d3SLuigi Rizzo 	if (ptn_dev->pci_io == NULL) {
74837e3a6d3SLuigi Rizzo 	        device_printf(dev, "cannot map I/O space\n");
74937e3a6d3SLuigi Rizzo 	        return (ENXIO);
75037e3a6d3SLuigi Rizzo 	}
75137e3a6d3SLuigi Rizzo 
752844a6f0cSLuigi Rizzo 	mem_id = bus_read_4(ptn_dev->pci_io, PTNET_MDEV_IO_MEMID);
75337e3a6d3SLuigi Rizzo 
75437e3a6d3SLuigi Rizzo 	/* create guest allocator */
75537e3a6d3SLuigi Rizzo 	ptn_dev->nm_mem = netmap_mem_pt_guest_attach(ptn_dev, mem_id);
75637e3a6d3SLuigi Rizzo 	if (ptn_dev->nm_mem == NULL) {
75737e3a6d3SLuigi Rizzo 		ptn_memdev_detach(dev);
75837e3a6d3SLuigi Rizzo 	        return (ENOMEM);
75937e3a6d3SLuigi Rizzo 	}
76037e3a6d3SLuigi Rizzo 	netmap_mem_get(ptn_dev->nm_mem);
76137e3a6d3SLuigi Rizzo 
762844a6f0cSLuigi Rizzo 	D("ptn_memdev_driver probe OK - host_mem_id: %d", mem_id);
76337e3a6d3SLuigi Rizzo 
76437e3a6d3SLuigi Rizzo 	return (0);
76537e3a6d3SLuigi Rizzo }
76637e3a6d3SLuigi Rizzo 
76737e3a6d3SLuigi Rizzo /* Device removal routine. */
76837e3a6d3SLuigi Rizzo static int
76937e3a6d3SLuigi Rizzo ptn_memdev_detach(device_t dev)
77037e3a6d3SLuigi Rizzo {
77137e3a6d3SLuigi Rizzo 	struct ptnetmap_memdev *ptn_dev;
77237e3a6d3SLuigi Rizzo 
77337e3a6d3SLuigi Rizzo 	D("ptn_memdev_driver detach");
77437e3a6d3SLuigi Rizzo 	ptn_dev = device_get_softc(dev);
77537e3a6d3SLuigi Rizzo 
77637e3a6d3SLuigi Rizzo 	if (ptn_dev->nm_mem) {
77737e3a6d3SLuigi Rizzo 		netmap_mem_put(ptn_dev->nm_mem);
77837e3a6d3SLuigi Rizzo 		ptn_dev->nm_mem = NULL;
77937e3a6d3SLuigi Rizzo 	}
78037e3a6d3SLuigi Rizzo 	if (ptn_dev->pci_mem) {
78137e3a6d3SLuigi Rizzo 		bus_release_resource(dev, SYS_RES_MEMORY,
78237e3a6d3SLuigi Rizzo 			PCIR_BAR(PTNETMAP_MEM_PCI_BAR), ptn_dev->pci_mem);
78337e3a6d3SLuigi Rizzo 		ptn_dev->pci_mem = NULL;
78437e3a6d3SLuigi Rizzo 	}
78537e3a6d3SLuigi Rizzo 	if (ptn_dev->pci_io) {
78637e3a6d3SLuigi Rizzo 		bus_release_resource(dev, SYS_RES_IOPORT,
78737e3a6d3SLuigi Rizzo 			PCIR_BAR(PTNETMAP_IO_PCI_BAR), ptn_dev->pci_io);
78837e3a6d3SLuigi Rizzo 		ptn_dev->pci_io = NULL;
78937e3a6d3SLuigi Rizzo 	}
79037e3a6d3SLuigi Rizzo 
79137e3a6d3SLuigi Rizzo 	return (0);
79237e3a6d3SLuigi Rizzo }
79337e3a6d3SLuigi Rizzo 
79437e3a6d3SLuigi Rizzo static int
79537e3a6d3SLuigi Rizzo ptn_memdev_shutdown(device_t dev)
79637e3a6d3SLuigi Rizzo {
79737e3a6d3SLuigi Rizzo 	D("ptn_memdev_driver shutdown");
79837e3a6d3SLuigi Rizzo 	return bus_generic_shutdown(dev);
79937e3a6d3SLuigi Rizzo }
80037e3a6d3SLuigi Rizzo 
80137e3a6d3SLuigi Rizzo #endif /* WITH_PTNETMAP_GUEST */
80237e3a6d3SLuigi Rizzo 
803f9790aebSLuigi Rizzo /*
804f9790aebSLuigi Rizzo  * In order to track whether pages are still mapped, we hook into
805f9790aebSLuigi Rizzo  * the standard cdev_pager and intercept the constructor and
806f9790aebSLuigi Rizzo  * destructor.
807f9790aebSLuigi Rizzo  */
808f9790aebSLuigi Rizzo 
809f9790aebSLuigi Rizzo struct netmap_vm_handle_t {
810f9790aebSLuigi Rizzo 	struct cdev 		*dev;
811f9790aebSLuigi Rizzo 	struct netmap_priv_d	*priv;
812f9790aebSLuigi Rizzo };
813f9790aebSLuigi Rizzo 
81417885a7bSLuigi Rizzo 
815f9790aebSLuigi Rizzo static int
816f9790aebSLuigi Rizzo netmap_dev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
817f9790aebSLuigi Rizzo     vm_ooffset_t foff, struct ucred *cred, u_short *color)
818f9790aebSLuigi Rizzo {
819f9790aebSLuigi Rizzo 	struct netmap_vm_handle_t *vmh = handle;
820f0ea3689SLuigi Rizzo 
821f0ea3689SLuigi Rizzo 	if (netmap_verbose)
822f9790aebSLuigi Rizzo 		D("handle %p size %jd prot %d foff %jd",
823f9790aebSLuigi Rizzo 			handle, (intmax_t)size, prot, (intmax_t)foff);
8244e93beffSLuigi Rizzo 	if (color)
8254e93beffSLuigi Rizzo 		*color = 0;
826f9790aebSLuigi Rizzo 	dev_ref(vmh->dev);
827f9790aebSLuigi Rizzo 	return 0;
828f9790aebSLuigi Rizzo }
829f9790aebSLuigi Rizzo 
830f9790aebSLuigi Rizzo 
831f9790aebSLuigi Rizzo static void
832f9790aebSLuigi Rizzo netmap_dev_pager_dtor(void *handle)
833f9790aebSLuigi Rizzo {
834f9790aebSLuigi Rizzo 	struct netmap_vm_handle_t *vmh = handle;
835f9790aebSLuigi Rizzo 	struct cdev *dev = vmh->dev;
836f9790aebSLuigi Rizzo 	struct netmap_priv_d *priv = vmh->priv;
837f0ea3689SLuigi Rizzo 
838f0ea3689SLuigi Rizzo 	if (netmap_verbose)
839f9790aebSLuigi Rizzo 		D("handle %p", handle);
840f9790aebSLuigi Rizzo 	netmap_dtor(priv);
841f9790aebSLuigi Rizzo 	free(vmh, M_DEVBUF);
842f9790aebSLuigi Rizzo 	dev_rel(dev);
843f9790aebSLuigi Rizzo }
844f9790aebSLuigi Rizzo 
84517885a7bSLuigi Rizzo 
846f9790aebSLuigi Rizzo static int
847f9790aebSLuigi Rizzo netmap_dev_pager_fault(vm_object_t object, vm_ooffset_t offset,
848f9790aebSLuigi Rizzo 	int prot, vm_page_t *mres)
849f9790aebSLuigi Rizzo {
850f9790aebSLuigi Rizzo 	struct netmap_vm_handle_t *vmh = object->handle;
851f9790aebSLuigi Rizzo 	struct netmap_priv_d *priv = vmh->priv;
852847bf383SLuigi Rizzo 	struct netmap_adapter *na = priv->np_na;
853f9790aebSLuigi Rizzo 	vm_paddr_t paddr;
854f9790aebSLuigi Rizzo 	vm_page_t page;
855f9790aebSLuigi Rizzo 	vm_memattr_t memattr;
856f9790aebSLuigi Rizzo 	vm_pindex_t pidx;
857f9790aebSLuigi Rizzo 
858f9790aebSLuigi Rizzo 	ND("object %p offset %jd prot %d mres %p",
859f9790aebSLuigi Rizzo 			object, (intmax_t)offset, prot, mres);
860f9790aebSLuigi Rizzo 	memattr = object->memattr;
861f9790aebSLuigi Rizzo 	pidx = OFF_TO_IDX(offset);
862847bf383SLuigi Rizzo 	paddr = netmap_mem_ofstophys(na->nm_mem, offset);
863f9790aebSLuigi Rizzo 	if (paddr == 0)
864f9790aebSLuigi Rizzo 		return VM_PAGER_FAIL;
865f9790aebSLuigi Rizzo 
866f9790aebSLuigi Rizzo 	if (((*mres)->flags & PG_FICTITIOUS) != 0) {
867f9790aebSLuigi Rizzo 		/*
868f9790aebSLuigi Rizzo 		 * If the passed in result page is a fake page, update it with
869f9790aebSLuigi Rizzo 		 * the new physical address.
870f9790aebSLuigi Rizzo 		 */
871f9790aebSLuigi Rizzo 		page = *mres;
872f9790aebSLuigi Rizzo 		vm_page_updatefake(page, paddr, memattr);
873f9790aebSLuigi Rizzo 	} else {
874f9790aebSLuigi Rizzo 		/*
875f9790aebSLuigi Rizzo 		 * Replace the passed in reqpage page with our own fake page and
876f9790aebSLuigi Rizzo 		 * free up the all of the original pages.
877f9790aebSLuigi Rizzo 		 */
878f9790aebSLuigi Rizzo #ifndef VM_OBJECT_WUNLOCK	/* FreeBSD < 10.x */
879f9790aebSLuigi Rizzo #define VM_OBJECT_WUNLOCK VM_OBJECT_UNLOCK
880f9790aebSLuigi Rizzo #define VM_OBJECT_WLOCK	VM_OBJECT_LOCK
881f9790aebSLuigi Rizzo #endif /* VM_OBJECT_WUNLOCK */
882f9790aebSLuigi Rizzo 
883f9790aebSLuigi Rizzo 		VM_OBJECT_WUNLOCK(object);
884f9790aebSLuigi Rizzo 		page = vm_page_getfake(paddr, memattr);
885f9790aebSLuigi Rizzo 		VM_OBJECT_WLOCK(object);
886f9790aebSLuigi Rizzo 		vm_page_lock(*mres);
887f9790aebSLuigi Rizzo 		vm_page_free(*mres);
888f9790aebSLuigi Rizzo 		vm_page_unlock(*mres);
889f9790aebSLuigi Rizzo 		*mres = page;
890f9790aebSLuigi Rizzo 		vm_page_insert(page, object, pidx);
891f9790aebSLuigi Rizzo 	}
892f9790aebSLuigi Rizzo 	page->valid = VM_PAGE_BITS_ALL;
893f9790aebSLuigi Rizzo 	return (VM_PAGER_OK);
894f9790aebSLuigi Rizzo }
895f9790aebSLuigi Rizzo 
896f9790aebSLuigi Rizzo 
897f9790aebSLuigi Rizzo static struct cdev_pager_ops netmap_cdev_pager_ops = {
898f9790aebSLuigi Rizzo 	.cdev_pg_ctor = netmap_dev_pager_ctor,
899f9790aebSLuigi Rizzo 	.cdev_pg_dtor = netmap_dev_pager_dtor,
900f9790aebSLuigi Rizzo 	.cdev_pg_fault = netmap_dev_pager_fault,
901f9790aebSLuigi Rizzo };
902f9790aebSLuigi Rizzo 
903f9790aebSLuigi Rizzo 
904f9790aebSLuigi Rizzo static int
905f9790aebSLuigi Rizzo netmap_mmap_single(struct cdev *cdev, vm_ooffset_t *foff,
906f9790aebSLuigi Rizzo 	vm_size_t objsize,  vm_object_t *objp, int prot)
907f9790aebSLuigi Rizzo {
908f9790aebSLuigi Rizzo 	int error;
909f9790aebSLuigi Rizzo 	struct netmap_vm_handle_t *vmh;
910f9790aebSLuigi Rizzo 	struct netmap_priv_d *priv;
911f9790aebSLuigi Rizzo 	vm_object_t obj;
912f9790aebSLuigi Rizzo 
913f0ea3689SLuigi Rizzo 	if (netmap_verbose)
914f9790aebSLuigi Rizzo 		D("cdev %p foff %jd size %jd objp %p prot %d", cdev,
915f9790aebSLuigi Rizzo 		    (intmax_t )*foff, (intmax_t )objsize, objp, prot);
916f9790aebSLuigi Rizzo 
917f9790aebSLuigi Rizzo 	vmh = malloc(sizeof(struct netmap_vm_handle_t), M_DEVBUF,
918f9790aebSLuigi Rizzo 			      M_NOWAIT | M_ZERO);
919f9790aebSLuigi Rizzo 	if (vmh == NULL)
920f9790aebSLuigi Rizzo 		return ENOMEM;
921f9790aebSLuigi Rizzo 	vmh->dev = cdev;
922f9790aebSLuigi Rizzo 
923f9790aebSLuigi Rizzo 	NMG_LOCK();
924f9790aebSLuigi Rizzo 	error = devfs_get_cdevpriv((void**)&priv);
925f9790aebSLuigi Rizzo 	if (error)
926f9790aebSLuigi Rizzo 		goto err_unlock;
927847bf383SLuigi Rizzo 	if (priv->np_nifp == NULL) {
928847bf383SLuigi Rizzo 		error = EINVAL;
929847bf383SLuigi Rizzo 		goto err_unlock;
930847bf383SLuigi Rizzo 	}
931f9790aebSLuigi Rizzo 	vmh->priv = priv;
9328fd44c93SLuigi Rizzo 	priv->np_refs++;
933f9790aebSLuigi Rizzo 	NMG_UNLOCK();
934f9790aebSLuigi Rizzo 
935f9790aebSLuigi Rizzo 	obj = cdev_pager_allocate(vmh, OBJT_DEVICE,
936f9790aebSLuigi Rizzo 		&netmap_cdev_pager_ops, objsize, prot,
937f9790aebSLuigi Rizzo 		*foff, NULL);
938f9790aebSLuigi Rizzo 	if (obj == NULL) {
939f9790aebSLuigi Rizzo 		D("cdev_pager_allocate failed");
940f9790aebSLuigi Rizzo 		error = EINVAL;
941f9790aebSLuigi Rizzo 		goto err_deref;
942f9790aebSLuigi Rizzo 	}
943f9790aebSLuigi Rizzo 
944f9790aebSLuigi Rizzo 	*objp = obj;
945f9790aebSLuigi Rizzo 	return 0;
946f9790aebSLuigi Rizzo 
947f9790aebSLuigi Rizzo err_deref:
948f9790aebSLuigi Rizzo 	NMG_LOCK();
9498fd44c93SLuigi Rizzo 	priv->np_refs--;
950f9790aebSLuigi Rizzo err_unlock:
951f9790aebSLuigi Rizzo 	NMG_UNLOCK();
952f9790aebSLuigi Rizzo // err:
953f9790aebSLuigi Rizzo 	free(vmh, M_DEVBUF);
954f9790aebSLuigi Rizzo 	return error;
955f9790aebSLuigi Rizzo }
956f9790aebSLuigi Rizzo 
957847bf383SLuigi Rizzo /*
9588fd44c93SLuigi Rizzo  * On FreeBSD the close routine is only called on the last close on
9598fd44c93SLuigi Rizzo  * the device (/dev/netmap) so we cannot do anything useful.
9608fd44c93SLuigi Rizzo  * To track close() on individual file descriptors we pass netmap_dtor() to
961847bf383SLuigi Rizzo  * devfs_set_cdevpriv() on open(). The FreeBSD kernel will call the destructor
962847bf383SLuigi Rizzo  * when the last fd pointing to the device is closed.
963847bf383SLuigi Rizzo  *
9648fd44c93SLuigi Rizzo  * Note that FreeBSD does not even munmap() on close() so we also have
9658fd44c93SLuigi Rizzo  * to track mmap() ourselves, and postpone the call to
966847bf383SLuigi Rizzo  * netmap_dtor() is called when the process has no open fds and no active
967847bf383SLuigi Rizzo  * memory maps on /dev/netmap, as in linux.
968847bf383SLuigi Rizzo  */
969f9790aebSLuigi Rizzo static int
970f9790aebSLuigi Rizzo netmap_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
971f9790aebSLuigi Rizzo {
972f9790aebSLuigi Rizzo 	if (netmap_verbose)
973f9790aebSLuigi Rizzo 		D("dev %p fflag 0x%x devtype %d td %p",
974f9790aebSLuigi Rizzo 			dev, fflag, devtype, td);
975f9790aebSLuigi Rizzo 	return 0;
976f9790aebSLuigi Rizzo }
977f9790aebSLuigi Rizzo 
978f9790aebSLuigi Rizzo 
979f9790aebSLuigi Rizzo static int
980f9790aebSLuigi Rizzo netmap_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
981f9790aebSLuigi Rizzo {
982f9790aebSLuigi Rizzo 	struct netmap_priv_d *priv;
983f9790aebSLuigi Rizzo 	int error;
984f9790aebSLuigi Rizzo 
985f9790aebSLuigi Rizzo 	(void)dev;
986f9790aebSLuigi Rizzo 	(void)oflags;
987f9790aebSLuigi Rizzo 	(void)devtype;
988f9790aebSLuigi Rizzo 	(void)td;
989f9790aebSLuigi Rizzo 
99037e3a6d3SLuigi Rizzo 	NMG_LOCK();
99137e3a6d3SLuigi Rizzo 	priv = netmap_priv_new();
99237e3a6d3SLuigi Rizzo 	if (priv == NULL) {
99337e3a6d3SLuigi Rizzo 		error = ENOMEM;
99437e3a6d3SLuigi Rizzo 		goto out;
99537e3a6d3SLuigi Rizzo 	}
996f9790aebSLuigi Rizzo 	error = devfs_set_cdevpriv(priv, netmap_dtor);
9978fd44c93SLuigi Rizzo 	if (error) {
99837e3a6d3SLuigi Rizzo 		netmap_priv_delete(priv);
9998fd44c93SLuigi Rizzo 	}
100037e3a6d3SLuigi Rizzo out:
100137e3a6d3SLuigi Rizzo 	NMG_UNLOCK();
1002f9790aebSLuigi Rizzo 	return error;
1003f9790aebSLuigi Rizzo }
1004f9790aebSLuigi Rizzo 
100537e3a6d3SLuigi Rizzo /******************** kthread wrapper ****************/
100637e3a6d3SLuigi Rizzo #include <sys/sysproto.h>
100737e3a6d3SLuigi Rizzo u_int
100837e3a6d3SLuigi Rizzo nm_os_ncpus(void)
100937e3a6d3SLuigi Rizzo {
101037e3a6d3SLuigi Rizzo 	return mp_maxid + 1;
101137e3a6d3SLuigi Rizzo }
101237e3a6d3SLuigi Rizzo 
1013c3e9b4dbSLuiz Otavio O Souza struct nm_kctx_ctx {
101437e3a6d3SLuigi Rizzo 	struct thread *user_td;		/* thread user-space (kthread creator) to send ioctl */
1015844a6f0cSLuigi Rizzo 	struct ptnetmap_cfgentry_bhyve	cfg;
101637e3a6d3SLuigi Rizzo 
101737e3a6d3SLuigi Rizzo 	/* worker function and parameter */
1018c3e9b4dbSLuiz Otavio O Souza 	nm_kctx_worker_fn_t worker_fn;
101937e3a6d3SLuigi Rizzo 	void *worker_private;
102037e3a6d3SLuigi Rizzo 
1021c3e9b4dbSLuiz Otavio O Souza 	struct nm_kctx *nmk;
102237e3a6d3SLuigi Rizzo 
102337e3a6d3SLuigi Rizzo 	/* integer to manage multiple worker contexts (e.g., RX or TX on ptnetmap) */
102437e3a6d3SLuigi Rizzo 	long type;
102537e3a6d3SLuigi Rizzo };
102637e3a6d3SLuigi Rizzo 
1027c3e9b4dbSLuiz Otavio O Souza struct nm_kctx {
102837e3a6d3SLuigi Rizzo 	struct thread *worker;
102937e3a6d3SLuigi Rizzo 	struct mtx worker_lock;
103037e3a6d3SLuigi Rizzo 	uint64_t scheduled; 		/* pending wake_up request */
1031c3e9b4dbSLuiz Otavio O Souza 	struct nm_kctx_ctx worker_ctx;
103237e3a6d3SLuigi Rizzo 	int run;			/* used to stop kthread */
103337e3a6d3SLuigi Rizzo 	int attach_user;		/* kthread attached to user_process */
103437e3a6d3SLuigi Rizzo 	int affinity;
103537e3a6d3SLuigi Rizzo };
103637e3a6d3SLuigi Rizzo 
103737e3a6d3SLuigi Rizzo void inline
1038c3e9b4dbSLuiz Otavio O Souza nm_os_kctx_worker_wakeup(struct nm_kctx *nmk)
103937e3a6d3SLuigi Rizzo {
104037e3a6d3SLuigi Rizzo 	/*
104137e3a6d3SLuigi Rizzo 	 * There may be a race between FE and BE,
104237e3a6d3SLuigi Rizzo 	 * which call both this function, and worker kthread,
104337e3a6d3SLuigi Rizzo 	 * that reads nmk->scheduled.
104437e3a6d3SLuigi Rizzo 	 *
104537e3a6d3SLuigi Rizzo 	 * For us it is not important the counter value,
104637e3a6d3SLuigi Rizzo 	 * but simply that it has changed since the last
104737e3a6d3SLuigi Rizzo 	 * time the kthread saw it.
104837e3a6d3SLuigi Rizzo 	 */
104937e3a6d3SLuigi Rizzo 	mtx_lock(&nmk->worker_lock);
105037e3a6d3SLuigi Rizzo 	nmk->scheduled++;
1051844a6f0cSLuigi Rizzo 	if (nmk->worker_ctx.cfg.wchan) {
105254c7693fSEd Maste 		wakeup((void *)(uintptr_t)nmk->worker_ctx.cfg.wchan);
105337e3a6d3SLuigi Rizzo 	}
105437e3a6d3SLuigi Rizzo 	mtx_unlock(&nmk->worker_lock);
105537e3a6d3SLuigi Rizzo }
105637e3a6d3SLuigi Rizzo 
105737e3a6d3SLuigi Rizzo void inline
1058c3e9b4dbSLuiz Otavio O Souza nm_os_kctx_send_irq(struct nm_kctx *nmk)
105937e3a6d3SLuigi Rizzo {
1060c3e9b4dbSLuiz Otavio O Souza 	struct nm_kctx_ctx *ctx = &nmk->worker_ctx;
106137e3a6d3SLuigi Rizzo 	int err;
106237e3a6d3SLuigi Rizzo 
1063844a6f0cSLuigi Rizzo 	if (ctx->user_td && ctx->cfg.ioctl_fd > 0) {
1064844a6f0cSLuigi Rizzo 		err = kern_ioctl(ctx->user_td, ctx->cfg.ioctl_fd, ctx->cfg.ioctl_cmd,
1065844a6f0cSLuigi Rizzo 				 (caddr_t)&ctx->cfg.ioctl_data);
106637e3a6d3SLuigi Rizzo 		if (err) {
1067844a6f0cSLuigi Rizzo 			D("kern_ioctl error: %d ioctl parameters: fd %d com %lu data %p",
1068844a6f0cSLuigi Rizzo 				err, ctx->cfg.ioctl_fd, (unsigned long)ctx->cfg.ioctl_cmd,
1069844a6f0cSLuigi Rizzo 				&ctx->cfg.ioctl_data);
107037e3a6d3SLuigi Rizzo 		}
107137e3a6d3SLuigi Rizzo 	}
107237e3a6d3SLuigi Rizzo }
107337e3a6d3SLuigi Rizzo 
107437e3a6d3SLuigi Rizzo static void
1075c3e9b4dbSLuiz Otavio O Souza nm_kctx_worker(void *data)
107637e3a6d3SLuigi Rizzo {
1077c3e9b4dbSLuiz Otavio O Souza 	struct nm_kctx *nmk = data;
1078c3e9b4dbSLuiz Otavio O Souza 	struct nm_kctx_ctx *ctx = &nmk->worker_ctx;
107937e3a6d3SLuigi Rizzo 	uint64_t old_scheduled = nmk->scheduled;
108037e3a6d3SLuigi Rizzo 
108137e3a6d3SLuigi Rizzo 	if (nmk->affinity >= 0) {
108237e3a6d3SLuigi Rizzo 		thread_lock(curthread);
108337e3a6d3SLuigi Rizzo 		sched_bind(curthread, nmk->affinity);
108437e3a6d3SLuigi Rizzo 		thread_unlock(curthread);
108537e3a6d3SLuigi Rizzo 	}
108637e3a6d3SLuigi Rizzo 
108737e3a6d3SLuigi Rizzo 	while (nmk->run) {
108837e3a6d3SLuigi Rizzo 		/*
108937e3a6d3SLuigi Rizzo 		 * check if the parent process dies
109037e3a6d3SLuigi Rizzo 		 * (when kthread is attached to user process)
109137e3a6d3SLuigi Rizzo 		 */
109237e3a6d3SLuigi Rizzo 		if (ctx->user_td) {
109337e3a6d3SLuigi Rizzo 			PROC_LOCK(curproc);
109437e3a6d3SLuigi Rizzo 			thread_suspend_check(0);
109537e3a6d3SLuigi Rizzo 			PROC_UNLOCK(curproc);
109637e3a6d3SLuigi Rizzo 		} else {
109737e3a6d3SLuigi Rizzo 			kthread_suspend_check();
109837e3a6d3SLuigi Rizzo 		}
109937e3a6d3SLuigi Rizzo 
110037e3a6d3SLuigi Rizzo 		/*
1101844a6f0cSLuigi Rizzo 		 * if wchan is not defined, we don't have notification
110237e3a6d3SLuigi Rizzo 		 * mechanism and we continually execute worker_fn()
110337e3a6d3SLuigi Rizzo 		 */
1104844a6f0cSLuigi Rizzo 		if (!ctx->cfg.wchan) {
1105c3e9b4dbSLuiz Otavio O Souza 			ctx->worker_fn(ctx->worker_private, 1); /* worker body */
110637e3a6d3SLuigi Rizzo 		} else {
110737e3a6d3SLuigi Rizzo 			/* checks if there is a pending notification */
110837e3a6d3SLuigi Rizzo 			mtx_lock(&nmk->worker_lock);
110937e3a6d3SLuigi Rizzo 			if (likely(nmk->scheduled != old_scheduled)) {
111037e3a6d3SLuigi Rizzo 				old_scheduled = nmk->scheduled;
111137e3a6d3SLuigi Rizzo 				mtx_unlock(&nmk->worker_lock);
111237e3a6d3SLuigi Rizzo 
1113c3e9b4dbSLuiz Otavio O Souza 				ctx->worker_fn(ctx->worker_private, 1); /* worker body */
111437e3a6d3SLuigi Rizzo 
111537e3a6d3SLuigi Rizzo 				continue;
111637e3a6d3SLuigi Rizzo 			} else if (nmk->run) {
111737e3a6d3SLuigi Rizzo 				/* wait on event with one second timeout */
1118c3e9b4dbSLuiz Otavio O Souza 				msleep((void *)(uintptr_t)ctx->cfg.wchan, &nmk->worker_lock,
1119c3e9b4dbSLuiz Otavio O Souza 					0, "nmk_ev", hz);
112037e3a6d3SLuigi Rizzo 				nmk->scheduled++;
112137e3a6d3SLuigi Rizzo 			}
112237e3a6d3SLuigi Rizzo 			mtx_unlock(&nmk->worker_lock);
112337e3a6d3SLuigi Rizzo 		}
112437e3a6d3SLuigi Rizzo 	}
112537e3a6d3SLuigi Rizzo 
112637e3a6d3SLuigi Rizzo 	kthread_exit();
112737e3a6d3SLuigi Rizzo }
112837e3a6d3SLuigi Rizzo 
112937e3a6d3SLuigi Rizzo void
1130c3e9b4dbSLuiz Otavio O Souza nm_os_kctx_worker_setaff(struct nm_kctx *nmk, int affinity)
113137e3a6d3SLuigi Rizzo {
113237e3a6d3SLuigi Rizzo 	nmk->affinity = affinity;
113337e3a6d3SLuigi Rizzo }
113437e3a6d3SLuigi Rizzo 
1135c3e9b4dbSLuiz Otavio O Souza struct nm_kctx *
1136c3e9b4dbSLuiz Otavio O Souza nm_os_kctx_create(struct nm_kctx_cfg *cfg, unsigned int cfgtype,
1137844a6f0cSLuigi Rizzo 		     void *opaque)
113837e3a6d3SLuigi Rizzo {
1139c3e9b4dbSLuiz Otavio O Souza 	struct nm_kctx *nmk = NULL;
1140844a6f0cSLuigi Rizzo 
1141844a6f0cSLuigi Rizzo 	if (cfgtype != PTNETMAP_CFGTYPE_BHYVE) {
1142844a6f0cSLuigi Rizzo 		D("Unsupported cfgtype %u", cfgtype);
1143844a6f0cSLuigi Rizzo 		return NULL;
1144844a6f0cSLuigi Rizzo 	}
114537e3a6d3SLuigi Rizzo 
114637e3a6d3SLuigi Rizzo 	nmk = malloc(sizeof(*nmk),  M_DEVBUF, M_NOWAIT | M_ZERO);
114737e3a6d3SLuigi Rizzo 	if (!nmk)
114837e3a6d3SLuigi Rizzo 		return NULL;
114937e3a6d3SLuigi Rizzo 
1150869d8878SAdrian Chadd 	mtx_init(&nmk->worker_lock, "nm_kthread lock", NULL, MTX_DEF);
115137e3a6d3SLuigi Rizzo 	nmk->worker_ctx.worker_fn = cfg->worker_fn;
115237e3a6d3SLuigi Rizzo 	nmk->worker_ctx.worker_private = cfg->worker_private;
115337e3a6d3SLuigi Rizzo 	nmk->worker_ctx.type = cfg->type;
115437e3a6d3SLuigi Rizzo 	nmk->affinity = -1;
115537e3a6d3SLuigi Rizzo 
115637e3a6d3SLuigi Rizzo 	/* attach kthread to user process (ptnetmap) */
115737e3a6d3SLuigi Rizzo 	nmk->attach_user = cfg->attach_user;
115837e3a6d3SLuigi Rizzo 
1159844a6f0cSLuigi Rizzo 	/* store kick/interrupt configuration */
1160844a6f0cSLuigi Rizzo 	if (opaque) {
1161844a6f0cSLuigi Rizzo 		nmk->worker_ctx.cfg = *((struct ptnetmap_cfgentry_bhyve *)opaque);
1162844a6f0cSLuigi Rizzo 	}
116337e3a6d3SLuigi Rizzo 
116437e3a6d3SLuigi Rizzo 	return nmk;
116537e3a6d3SLuigi Rizzo }
116637e3a6d3SLuigi Rizzo 
116737e3a6d3SLuigi Rizzo int
1168c3e9b4dbSLuiz Otavio O Souza nm_os_kctx_worker_start(struct nm_kctx *nmk)
116937e3a6d3SLuigi Rizzo {
117037e3a6d3SLuigi Rizzo 	struct proc *p = NULL;
117137e3a6d3SLuigi Rizzo 	int error = 0;
117237e3a6d3SLuigi Rizzo 
117337e3a6d3SLuigi Rizzo 	if (nmk->worker) {
117437e3a6d3SLuigi Rizzo 		return EBUSY;
117537e3a6d3SLuigi Rizzo 	}
117637e3a6d3SLuigi Rizzo 
117737e3a6d3SLuigi Rizzo 	/* check if we want to attach kthread to user process */
117837e3a6d3SLuigi Rizzo 	if (nmk->attach_user) {
117937e3a6d3SLuigi Rizzo 		nmk->worker_ctx.user_td = curthread;
118037e3a6d3SLuigi Rizzo 		p = curthread->td_proc;
118137e3a6d3SLuigi Rizzo 	}
118237e3a6d3SLuigi Rizzo 
118337e3a6d3SLuigi Rizzo 	/* enable kthread main loop */
118437e3a6d3SLuigi Rizzo 	nmk->run = 1;
118537e3a6d3SLuigi Rizzo 	/* create kthread */
1186c3e9b4dbSLuiz Otavio O Souza 	if((error = kthread_add(nm_kctx_worker, nmk, p,
118737e3a6d3SLuigi Rizzo 			&nmk->worker, RFNOWAIT /* to be checked */, 0, "nm-kthread-%ld",
118837e3a6d3SLuigi Rizzo 			nmk->worker_ctx.type))) {
118937e3a6d3SLuigi Rizzo 		goto err;
119037e3a6d3SLuigi Rizzo 	}
119137e3a6d3SLuigi Rizzo 
1192844a6f0cSLuigi Rizzo 	D("nm_kthread started td %p", nmk->worker);
119337e3a6d3SLuigi Rizzo 
119437e3a6d3SLuigi Rizzo 	return 0;
119537e3a6d3SLuigi Rizzo err:
119637e3a6d3SLuigi Rizzo 	D("nm_kthread start failed err %d", error);
119737e3a6d3SLuigi Rizzo 	nmk->worker = NULL;
119837e3a6d3SLuigi Rizzo 	return error;
119937e3a6d3SLuigi Rizzo }
120037e3a6d3SLuigi Rizzo 
120137e3a6d3SLuigi Rizzo void
1202c3e9b4dbSLuiz Otavio O Souza nm_os_kctx_worker_stop(struct nm_kctx *nmk)
120337e3a6d3SLuigi Rizzo {
120437e3a6d3SLuigi Rizzo 	if (!nmk->worker) {
120537e3a6d3SLuigi Rizzo 		return;
120637e3a6d3SLuigi Rizzo 	}
120737e3a6d3SLuigi Rizzo 	/* tell to kthread to exit from main loop */
120837e3a6d3SLuigi Rizzo 	nmk->run = 0;
120937e3a6d3SLuigi Rizzo 
121037e3a6d3SLuigi Rizzo 	/* wake up kthread if it sleeps */
121137e3a6d3SLuigi Rizzo 	kthread_resume(nmk->worker);
1212c3e9b4dbSLuiz Otavio O Souza 	nm_os_kctx_worker_wakeup(nmk);
121337e3a6d3SLuigi Rizzo 
121437e3a6d3SLuigi Rizzo 	nmk->worker = NULL;
121537e3a6d3SLuigi Rizzo }
121637e3a6d3SLuigi Rizzo 
121737e3a6d3SLuigi Rizzo void
1218c3e9b4dbSLuiz Otavio O Souza nm_os_kctx_destroy(struct nm_kctx *nmk)
121937e3a6d3SLuigi Rizzo {
122037e3a6d3SLuigi Rizzo 	if (!nmk)
122137e3a6d3SLuigi Rizzo 		return;
122237e3a6d3SLuigi Rizzo 	if (nmk->worker) {
1223c3e9b4dbSLuiz Otavio O Souza 		nm_os_kctx_worker_stop(nmk);
122437e3a6d3SLuigi Rizzo 	}
122537e3a6d3SLuigi Rizzo 
1226844a6f0cSLuigi Rizzo 	memset(&nmk->worker_ctx.cfg, 0, sizeof(nmk->worker_ctx.cfg));
122737e3a6d3SLuigi Rizzo 
122837e3a6d3SLuigi Rizzo 	free(nmk, M_DEVBUF);
122937e3a6d3SLuigi Rizzo }
123037e3a6d3SLuigi Rizzo 
1231f0ea3689SLuigi Rizzo /******************** kqueue support ****************/
1232f0ea3689SLuigi Rizzo 
1233f0ea3689SLuigi Rizzo /*
123437e3a6d3SLuigi Rizzo  * nm_os_selwakeup also needs to issue a KNOTE_UNLOCKED.
1235f0ea3689SLuigi Rizzo  * We use a non-zero argument to distinguish the call from the one
1236f0ea3689SLuigi Rizzo  * in kevent_scan() which instead also needs to run netmap_poll().
1237f0ea3689SLuigi Rizzo  * The knote uses a global mutex for the time being. We might
1238f0ea3689SLuigi Rizzo  * try to reuse the one in the si, but it is not allocated
1239f0ea3689SLuigi Rizzo  * permanently so it might be a bit tricky.
1240f0ea3689SLuigi Rizzo  *
1241f0ea3689SLuigi Rizzo  * The *kqfilter function registers one or another f_event
1242f0ea3689SLuigi Rizzo  * depending on read or write mode.
1243f0ea3689SLuigi Rizzo  * In the call to f_event() td_fpop is NULL so any child function
1244f0ea3689SLuigi Rizzo  * calling devfs_get_cdevpriv() would fail - and we need it in
1245f0ea3689SLuigi Rizzo  * netmap_poll(). As a workaround we store priv into kn->kn_hook
1246f0ea3689SLuigi Rizzo  * and pass it as first argument to netmap_poll(), which then
1247f0ea3689SLuigi Rizzo  * uses the failure to tell that we are called from f_event()
1248f0ea3689SLuigi Rizzo  * and do not need the selrecord().
1249f0ea3689SLuigi Rizzo  */
1250f0ea3689SLuigi Rizzo 
1251f0ea3689SLuigi Rizzo 
1252f0ea3689SLuigi Rizzo void
125337e3a6d3SLuigi Rizzo nm_os_selwakeup(struct nm_selinfo *si)
1254f0ea3689SLuigi Rizzo {
1255f0ea3689SLuigi Rizzo 	if (netmap_verbose)
12560e73f29aSLuigi Rizzo 		D("on knote %p", &si->si.si_note);
125737e3a6d3SLuigi Rizzo 	selwakeuppri(&si->si, PI_NET);
1258f0ea3689SLuigi Rizzo 	/* use a non-zero hint to tell the notification from the
1259f0ea3689SLuigi Rizzo 	 * call done in kqueue_scan() which uses 0
1260f0ea3689SLuigi Rizzo 	 */
12610e73f29aSLuigi Rizzo 	KNOTE_UNLOCKED(&si->si.si_note, 0x100 /* notification */);
1262f0ea3689SLuigi Rizzo }
1263f0ea3689SLuigi Rizzo 
126437e3a6d3SLuigi Rizzo void
126537e3a6d3SLuigi Rizzo nm_os_selrecord(struct thread *td, struct nm_selinfo *si)
126637e3a6d3SLuigi Rizzo {
126737e3a6d3SLuigi Rizzo 	selrecord(td, &si->si);
126837e3a6d3SLuigi Rizzo }
126937e3a6d3SLuigi Rizzo 
1270f0ea3689SLuigi Rizzo static void
1271f0ea3689SLuigi Rizzo netmap_knrdetach(struct knote *kn)
1272f0ea3689SLuigi Rizzo {
1273f0ea3689SLuigi Rizzo 	struct netmap_priv_d *priv = (struct netmap_priv_d *)kn->kn_hook;
1274847bf383SLuigi Rizzo 	struct selinfo *si = &priv->np_si[NR_RX]->si;
1275f0ea3689SLuigi Rizzo 
1276f0ea3689SLuigi Rizzo 	D("remove selinfo %p", si);
1277f0ea3689SLuigi Rizzo 	knlist_remove(&si->si_note, kn, 0);
1278f0ea3689SLuigi Rizzo }
1279f0ea3689SLuigi Rizzo 
1280f0ea3689SLuigi Rizzo static void
1281f0ea3689SLuigi Rizzo netmap_knwdetach(struct knote *kn)
1282f0ea3689SLuigi Rizzo {
1283f0ea3689SLuigi Rizzo 	struct netmap_priv_d *priv = (struct netmap_priv_d *)kn->kn_hook;
1284847bf383SLuigi Rizzo 	struct selinfo *si = &priv->np_si[NR_TX]->si;
1285f0ea3689SLuigi Rizzo 
1286f0ea3689SLuigi Rizzo 	D("remove selinfo %p", si);
1287f0ea3689SLuigi Rizzo 	knlist_remove(&si->si_note, kn, 0);
1288f0ea3689SLuigi Rizzo }
1289f0ea3689SLuigi Rizzo 
1290f0ea3689SLuigi Rizzo /*
1291f0ea3689SLuigi Rizzo  * callback from notifies (generated externally) and our
1292f0ea3689SLuigi Rizzo  * calls to kevent(). The former we just return 1 (ready)
1293f0ea3689SLuigi Rizzo  * since we do not know better.
1294f0ea3689SLuigi Rizzo  * In the latter we call netmap_poll and return 0/1 accordingly.
1295f0ea3689SLuigi Rizzo  */
1296f0ea3689SLuigi Rizzo static int
1297f0ea3689SLuigi Rizzo netmap_knrw(struct knote *kn, long hint, int events)
1298f0ea3689SLuigi Rizzo {
1299f0ea3689SLuigi Rizzo 	struct netmap_priv_d *priv;
1300f0ea3689SLuigi Rizzo 	int revents;
1301f0ea3689SLuigi Rizzo 
1302f0ea3689SLuigi Rizzo 	if (hint != 0) {
1303f0ea3689SLuigi Rizzo 		ND(5, "call from notify");
1304f0ea3689SLuigi Rizzo 		return 1; /* assume we are ready */
1305f0ea3689SLuigi Rizzo 	}
1306f0ea3689SLuigi Rizzo 	priv = kn->kn_hook;
1307f0ea3689SLuigi Rizzo 	/* the notification may come from an external thread,
1308f0ea3689SLuigi Rizzo 	 * in which case we do not want to run the netmap_poll
1309f0ea3689SLuigi Rizzo 	 * This should be filtered above, but check just in case.
1310f0ea3689SLuigi Rizzo 	 */
1311f0ea3689SLuigi Rizzo 	if (curthread != priv->np_td) { /* should not happen */
1312f0ea3689SLuigi Rizzo 		RD(5, "curthread changed %p %p", curthread, priv->np_td);
1313f0ea3689SLuigi Rizzo 		return 1;
1314f0ea3689SLuigi Rizzo 	} else {
131537e3a6d3SLuigi Rizzo 		revents = netmap_poll(priv, events, NULL);
1316f0ea3689SLuigi Rizzo 		return (events & revents) ? 1 : 0;
1317f0ea3689SLuigi Rizzo 	}
1318f0ea3689SLuigi Rizzo }
1319f0ea3689SLuigi Rizzo 
1320f0ea3689SLuigi Rizzo static int
1321f0ea3689SLuigi Rizzo netmap_knread(struct knote *kn, long hint)
1322f0ea3689SLuigi Rizzo {
1323f0ea3689SLuigi Rizzo 	return netmap_knrw(kn, hint, POLLIN);
1324f0ea3689SLuigi Rizzo }
1325f0ea3689SLuigi Rizzo 
1326f0ea3689SLuigi Rizzo static int
1327f0ea3689SLuigi Rizzo netmap_knwrite(struct knote *kn, long hint)
1328f0ea3689SLuigi Rizzo {
1329f0ea3689SLuigi Rizzo 	return netmap_knrw(kn, hint, POLLOUT);
1330f0ea3689SLuigi Rizzo }
1331f0ea3689SLuigi Rizzo 
1332f0ea3689SLuigi Rizzo static struct filterops netmap_rfiltops = {
1333f0ea3689SLuigi Rizzo 	.f_isfd = 1,
1334f0ea3689SLuigi Rizzo 	.f_detach = netmap_knrdetach,
1335f0ea3689SLuigi Rizzo 	.f_event = netmap_knread,
1336f0ea3689SLuigi Rizzo };
1337f0ea3689SLuigi Rizzo 
1338f0ea3689SLuigi Rizzo static struct filterops netmap_wfiltops = {
1339f0ea3689SLuigi Rizzo 	.f_isfd = 1,
1340f0ea3689SLuigi Rizzo 	.f_detach = netmap_knwdetach,
1341f0ea3689SLuigi Rizzo 	.f_event = netmap_knwrite,
1342f0ea3689SLuigi Rizzo };
1343f0ea3689SLuigi Rizzo 
1344f0ea3689SLuigi Rizzo 
1345f0ea3689SLuigi Rizzo /*
1346f0ea3689SLuigi Rizzo  * This is called when a thread invokes kevent() to record
1347f0ea3689SLuigi Rizzo  * a change in the configuration of the kqueue().
1348f0ea3689SLuigi Rizzo  * The 'priv' should be the same as in the netmap device.
1349f0ea3689SLuigi Rizzo  */
1350f0ea3689SLuigi Rizzo static int
1351f0ea3689SLuigi Rizzo netmap_kqfilter(struct cdev *dev, struct knote *kn)
1352f0ea3689SLuigi Rizzo {
1353f0ea3689SLuigi Rizzo 	struct netmap_priv_d *priv;
1354f0ea3689SLuigi Rizzo 	int error;
1355f0ea3689SLuigi Rizzo 	struct netmap_adapter *na;
13560e73f29aSLuigi Rizzo 	struct nm_selinfo *si;
1357f0ea3689SLuigi Rizzo 	int ev = kn->kn_filter;
1358f0ea3689SLuigi Rizzo 
1359f0ea3689SLuigi Rizzo 	if (ev != EVFILT_READ && ev != EVFILT_WRITE) {
1360f0ea3689SLuigi Rizzo 		D("bad filter request %d", ev);
1361f0ea3689SLuigi Rizzo 		return 1;
1362f0ea3689SLuigi Rizzo 	}
1363f0ea3689SLuigi Rizzo 	error = devfs_get_cdevpriv((void**)&priv);
1364f0ea3689SLuigi Rizzo 	if (error) {
1365f0ea3689SLuigi Rizzo 		D("device not yet setup");
1366f0ea3689SLuigi Rizzo 		return 1;
1367f0ea3689SLuigi Rizzo 	}
1368f0ea3689SLuigi Rizzo 	na = priv->np_na;
1369f0ea3689SLuigi Rizzo 	if (na == NULL) {
1370f0ea3689SLuigi Rizzo 		D("no netmap adapter for this file descriptor");
1371f0ea3689SLuigi Rizzo 		return 1;
1372f0ea3689SLuigi Rizzo 	}
1373f0ea3689SLuigi Rizzo 	/* the si is indicated in the priv */
1374847bf383SLuigi Rizzo 	si = priv->np_si[(ev == EVFILT_WRITE) ? NR_TX : NR_RX];
1375f0ea3689SLuigi Rizzo 	// XXX lock(priv) ?
1376f0ea3689SLuigi Rizzo 	kn->kn_fop = (ev == EVFILT_WRITE) ?
1377f0ea3689SLuigi Rizzo 		&netmap_wfiltops : &netmap_rfiltops;
1378f0ea3689SLuigi Rizzo 	kn->kn_hook = priv;
13790e73f29aSLuigi Rizzo 	knlist_add(&si->si.si_note, kn, 1);
1380f0ea3689SLuigi Rizzo 	// XXX unlock(priv)
1381f0ea3689SLuigi Rizzo 	ND("register %p %s td %p priv %p kn %p np_nifp %p kn_fp/fpop %s",
1382f0ea3689SLuigi Rizzo 		na, na->ifp->if_xname, curthread, priv, kn,
1383f0ea3689SLuigi Rizzo 		priv->np_nifp,
1384f0ea3689SLuigi Rizzo 		kn->kn_fp == curthread->td_fpop ? "match" : "MISMATCH");
1385f0ea3689SLuigi Rizzo 	return 0;
1386f0ea3689SLuigi Rizzo }
1387f9790aebSLuigi Rizzo 
138837e3a6d3SLuigi Rizzo static int
138937e3a6d3SLuigi Rizzo freebsd_netmap_poll(struct cdev *cdevi __unused, int events, struct thread *td)
139037e3a6d3SLuigi Rizzo {
139137e3a6d3SLuigi Rizzo 	struct netmap_priv_d *priv;
139237e3a6d3SLuigi Rizzo 	if (devfs_get_cdevpriv((void **)&priv)) {
139337e3a6d3SLuigi Rizzo 		return POLLERR;
139437e3a6d3SLuigi Rizzo 	}
139537e3a6d3SLuigi Rizzo 	return netmap_poll(priv, events, td);
139637e3a6d3SLuigi Rizzo }
139737e3a6d3SLuigi Rizzo 
139837e3a6d3SLuigi Rizzo static int
139937e3a6d3SLuigi Rizzo freebsd_netmap_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data,
140037e3a6d3SLuigi Rizzo         int ffla __unused, struct thread *td)
140137e3a6d3SLuigi Rizzo {
140237e3a6d3SLuigi Rizzo 	int error;
140337e3a6d3SLuigi Rizzo 	struct netmap_priv_d *priv;
140437e3a6d3SLuigi Rizzo 
1405ffaa5debSSepherosa Ziehau 	CURVNET_SET(TD_TO_VNET(td));
140637e3a6d3SLuigi Rizzo 	error = devfs_get_cdevpriv((void **)&priv);
140737e3a6d3SLuigi Rizzo 	if (error) {
140837e3a6d3SLuigi Rizzo 		/* XXX ENOENT should be impossible, since the priv
140937e3a6d3SLuigi Rizzo 		 * is now created in the open */
141037e3a6d3SLuigi Rizzo 		if (error == ENOENT)
141137e3a6d3SLuigi Rizzo 			error = ENXIO;
141237e3a6d3SLuigi Rizzo 		goto out;
141337e3a6d3SLuigi Rizzo 	}
141437e3a6d3SLuigi Rizzo 	error = netmap_ioctl(priv, cmd, data, td);
141537e3a6d3SLuigi Rizzo out:
141637e3a6d3SLuigi Rizzo 	CURVNET_RESTORE();
141737e3a6d3SLuigi Rizzo 
141837e3a6d3SLuigi Rizzo 	return error;
141937e3a6d3SLuigi Rizzo }
142037e3a6d3SLuigi Rizzo 
142137e3a6d3SLuigi Rizzo extern struct cdevsw netmap_cdevsw; /* XXX used in netmap.c, should go elsewhere */
1422f9790aebSLuigi Rizzo struct cdevsw netmap_cdevsw = {
1423f9790aebSLuigi Rizzo 	.d_version = D_VERSION,
1424f9790aebSLuigi Rizzo 	.d_name = "netmap",
1425f9790aebSLuigi Rizzo 	.d_open = netmap_open,
1426f9790aebSLuigi Rizzo 	.d_mmap_single = netmap_mmap_single,
142737e3a6d3SLuigi Rizzo 	.d_ioctl = freebsd_netmap_ioctl,
142837e3a6d3SLuigi Rizzo 	.d_poll = freebsd_netmap_poll,
1429f0ea3689SLuigi Rizzo 	.d_kqfilter = netmap_kqfilter,
1430f9790aebSLuigi Rizzo 	.d_close = netmap_close,
1431f9790aebSLuigi Rizzo };
1432f0ea3689SLuigi Rizzo /*--- end of kqueue support ----*/
1433f9790aebSLuigi Rizzo 
1434f9790aebSLuigi Rizzo /*
1435f9790aebSLuigi Rizzo  * Kernel entry point.
1436f9790aebSLuigi Rizzo  *
1437f9790aebSLuigi Rizzo  * Initialize/finalize the module and return.
1438f9790aebSLuigi Rizzo  *
1439f9790aebSLuigi Rizzo  * Return 0 on success, errno on failure.
1440f9790aebSLuigi Rizzo  */
1441f9790aebSLuigi Rizzo static int
1442f9790aebSLuigi Rizzo netmap_loader(__unused struct module *module, int event, __unused void *arg)
1443f9790aebSLuigi Rizzo {
1444f9790aebSLuigi Rizzo 	int error = 0;
1445f9790aebSLuigi Rizzo 
1446f9790aebSLuigi Rizzo 	switch (event) {
1447f9790aebSLuigi Rizzo 	case MOD_LOAD:
1448f9790aebSLuigi Rizzo 		error = netmap_init();
1449f9790aebSLuigi Rizzo 		break;
1450f9790aebSLuigi Rizzo 
1451f9790aebSLuigi Rizzo 	case MOD_UNLOAD:
1452847adfb7SLuigi Rizzo 		/*
1453847adfb7SLuigi Rizzo 		 * if some one is still using netmap,
1454847adfb7SLuigi Rizzo 		 * then the module can not be unloaded.
1455847adfb7SLuigi Rizzo 		 */
1456847adfb7SLuigi Rizzo 		if (netmap_use_count) {
1457847adfb7SLuigi Rizzo 			D("netmap module can not be unloaded - netmap_use_count: %d",
1458847adfb7SLuigi Rizzo 					netmap_use_count);
1459847adfb7SLuigi Rizzo 			error = EBUSY;
1460847adfb7SLuigi Rizzo 			break;
1461847adfb7SLuigi Rizzo 		}
1462f9790aebSLuigi Rizzo 		netmap_fini();
1463f9790aebSLuigi Rizzo 		break;
1464f9790aebSLuigi Rizzo 
1465f9790aebSLuigi Rizzo 	default:
1466f9790aebSLuigi Rizzo 		error = EOPNOTSUPP;
1467f9790aebSLuigi Rizzo 		break;
1468f9790aebSLuigi Rizzo 	}
1469f9790aebSLuigi Rizzo 
1470f9790aebSLuigi Rizzo 	return (error);
1471f9790aebSLuigi Rizzo }
1472f9790aebSLuigi Rizzo 
147337e3a6d3SLuigi Rizzo #ifdef DEV_MODULE_ORDERED
147437e3a6d3SLuigi Rizzo /*
147537e3a6d3SLuigi Rizzo  * The netmap module contains three drivers: (i) the netmap character device
147637e3a6d3SLuigi Rizzo  * driver; (ii) the ptnetmap memdev PCI device driver, (iii) the ptnet PCI
147737e3a6d3SLuigi Rizzo  * device driver. The attach() routines of both (ii) and (iii) need the
147837e3a6d3SLuigi Rizzo  * lock of the global allocator, and such lock is initialized in netmap_init(),
147937e3a6d3SLuigi Rizzo  * which is part of (i).
148037e3a6d3SLuigi Rizzo  * Therefore, we make sure that (i) is loaded before (ii) and (iii), using
148137e3a6d3SLuigi Rizzo  * the 'order' parameter of driver declaration macros. For (i), we specify
148237e3a6d3SLuigi Rizzo  * SI_ORDER_MIDDLE, while higher orders are used with the DRIVER_MODULE_ORDERED
148337e3a6d3SLuigi Rizzo  * macros for (ii) and (iii).
148437e3a6d3SLuigi Rizzo  */
148537e3a6d3SLuigi Rizzo DEV_MODULE_ORDERED(netmap, netmap_loader, NULL, SI_ORDER_MIDDLE);
148637e3a6d3SLuigi Rizzo #else /* !DEV_MODULE_ORDERED */
1487f9790aebSLuigi Rizzo DEV_MODULE(netmap, netmap_loader, NULL);
148837e3a6d3SLuigi Rizzo #endif /* DEV_MODULE_ORDERED  */
148937e3a6d3SLuigi Rizzo MODULE_DEPEND(netmap, pci, 1, 1, 1);
1490735c8d95SLuigi Rizzo MODULE_VERSION(netmap, 1);
149137e3a6d3SLuigi Rizzo /* reduce conditional code */
149237e3a6d3SLuigi Rizzo // linux API, use for the knlist in FreeBSD
149337e3a6d3SLuigi Rizzo /* use a private mutex for the knlist */
1494