xref: /freebsd/sys/dev/netmap/if_re_netmap.h (revision 17885a7bfde9d164e45a9833bb172215c55739f9)
168b8534bSLuigi Rizzo /*
2*17885a7bSLuigi Rizzo  * Copyright (C) 2011-2014 Luigi Rizzo. All rights reserved.
368b8534bSLuigi Rizzo  *
468b8534bSLuigi Rizzo  * Redistribution and use in source and binary forms, with or without
568b8534bSLuigi Rizzo  * modification, are permitted provided that the following conditions
668b8534bSLuigi Rizzo  * are met:
768b8534bSLuigi Rizzo  * 1. Redistributions of source code must retain the above copyright
868b8534bSLuigi Rizzo  *    notice, this list of conditions and the following disclaimer.
968b8534bSLuigi Rizzo  * 2. Redistributions in binary form must reproduce the above copyright
1068b8534bSLuigi Rizzo  *    notice, this list of conditions and the following disclaimer in the
1168b8534bSLuigi Rizzo  *    documentation and/or other materials provided with the distribution.
1268b8534bSLuigi Rizzo  *
1368b8534bSLuigi Rizzo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1468b8534bSLuigi Rizzo  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1568b8534bSLuigi Rizzo  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1668b8534bSLuigi Rizzo  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1768b8534bSLuigi Rizzo  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1868b8534bSLuigi Rizzo  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1968b8534bSLuigi Rizzo  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2068b8534bSLuigi Rizzo  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2168b8534bSLuigi Rizzo  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2268b8534bSLuigi Rizzo  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2368b8534bSLuigi Rizzo  * SUCH DAMAGE.
2468b8534bSLuigi Rizzo  */
2568b8534bSLuigi Rizzo 
2668b8534bSLuigi Rizzo /*
2768b8534bSLuigi Rizzo  * $FreeBSD$
2868b8534bSLuigi Rizzo  *
29f9790aebSLuigi Rizzo  * netmap support for: re
30f9790aebSLuigi Rizzo  *
31f9790aebSLuigi Rizzo  * For more details on netmap support please see ixgbe_netmap.h
3268b8534bSLuigi Rizzo  */
3368b8534bSLuigi Rizzo 
3464ae02c3SLuigi Rizzo 
3568b8534bSLuigi Rizzo #include <net/netmap.h>
3668b8534bSLuigi Rizzo #include <sys/selinfo.h>
3768b8534bSLuigi Rizzo #include <vm/vm.h>
3868b8534bSLuigi Rizzo #include <vm/pmap.h>    /* vtophys ? */
3968b8534bSLuigi Rizzo #include <dev/netmap/netmap_kern.h>
4068b8534bSLuigi Rizzo 
4168b8534bSLuigi Rizzo 
4268b8534bSLuigi Rizzo /*
43f9790aebSLuigi Rizzo  * Register/unregister. We are already under netmap lock.
4468b8534bSLuigi Rizzo  */
4568b8534bSLuigi Rizzo static int
46f9790aebSLuigi Rizzo re_netmap_reg(struct netmap_adapter *na, int onoff)
4768b8534bSLuigi Rizzo {
48f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
4968b8534bSLuigi Rizzo 	struct rl_softc *adapter = ifp->if_softc;
5068b8534bSLuigi Rizzo 
51f9790aebSLuigi Rizzo 	RL_LOCK(adapter);
52f9790aebSLuigi Rizzo 	re_stop(adapter); /* also clears IFF_DRV_RUNNING */
5368b8534bSLuigi Rizzo 	if (onoff) {
54f9790aebSLuigi Rizzo 		nm_set_native_flags(na);
5568b8534bSLuigi Rizzo 	} else {
56f9790aebSLuigi Rizzo 		nm_clear_native_flags(na);
5768b8534bSLuigi Rizzo 	}
58f9790aebSLuigi Rizzo 	re_init_locked(adapter);	/* also enables intr */
59f9790aebSLuigi Rizzo 	RL_UNLOCK(adapter);
60f9790aebSLuigi Rizzo 	return (ifp->if_drv_flags & IFF_DRV_RUNNING ? 0 : 1);
6168b8534bSLuigi Rizzo }
6268b8534bSLuigi Rizzo 
6368b8534bSLuigi Rizzo 
6468b8534bSLuigi Rizzo /*
6568b8534bSLuigi Rizzo  * Reconcile kernel and user view of the transmit ring.
6668b8534bSLuigi Rizzo  */
6768b8534bSLuigi Rizzo static int
68f9790aebSLuigi Rizzo re_netmap_txsync(struct netmap_adapter *na, u_int ring_nr, int flags)
6968b8534bSLuigi Rizzo {
70f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
7168b8534bSLuigi Rizzo 	struct netmap_kring *kring = &na->tx_rings[ring_nr];
7268b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
73f9790aebSLuigi Rizzo 	u_int nm_i;	/* index into the netmap ring */
74f9790aebSLuigi Rizzo 	u_int nic_i;	/* index into the NIC ring */
75*17885a7bSLuigi Rizzo 	u_int n;
76f9790aebSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
77*17885a7bSLuigi Rizzo 	u_int const head = kring->rhead;
7868b8534bSLuigi Rizzo 
79f9790aebSLuigi Rizzo 	/* device-specific */
80f9790aebSLuigi Rizzo 	struct rl_softc *sc = ifp->if_softc;
81f9790aebSLuigi Rizzo 	struct rl_txdesc *txd = sc->rl_ldata.rl_tx_desc;
82f9790aebSLuigi Rizzo 
8368b8534bSLuigi Rizzo 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
8468b8534bSLuigi Rizzo 	    sc->rl_ldata.rl_tx_list_map,
85f9790aebSLuigi Rizzo 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); // XXX extra postwrite ?
8668b8534bSLuigi Rizzo 
87f9790aebSLuigi Rizzo 	/*
88f9790aebSLuigi Rizzo 	 * First part: process new packets to send.
89f9790aebSLuigi Rizzo 	 */
90f9790aebSLuigi Rizzo 	nm_i = kring->nr_hwcur;
91*17885a7bSLuigi Rizzo 	if (nm_i != head) {	/* we have new packets to send */
92f9790aebSLuigi Rizzo 		nic_i = sc->rl_ldata.rl_tx_prodidx;
93f9790aebSLuigi Rizzo 		// XXX or netmap_idx_k2n(kring, nm_i);
9468b8534bSLuigi Rizzo 
95*17885a7bSLuigi Rizzo 		for (n = 0; nm_i != head; n++) {
96f9790aebSLuigi Rizzo 			struct netmap_slot *slot = &ring->slot[nm_i];
97f9790aebSLuigi Rizzo 			u_int len = slot->len;
986e10c8b8SLuigi Rizzo 			uint64_t paddr;
996e10c8b8SLuigi Rizzo 			void *addr = PNMB(slot, &paddr);
10068b8534bSLuigi Rizzo 
101f9790aebSLuigi Rizzo 			/* device-specific */
102f9790aebSLuigi Rizzo 			struct rl_desc *desc = &sc->rl_ldata.rl_tx_list[nic_i];
103f9790aebSLuigi Rizzo 			int cmd = slot->len | RL_TDESC_CMD_EOF |
104f9790aebSLuigi Rizzo 				RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF ;
10568b8534bSLuigi Rizzo 
106f9790aebSLuigi Rizzo 			NM_CHECK_ADDR_LEN(addr, len);
107f9790aebSLuigi Rizzo 
108f9790aebSLuigi Rizzo 			if (nic_i == lim)	/* mark end of ring */
10968b8534bSLuigi Rizzo 				cmd |= RL_TDESC_CMD_EOR;
11068b8534bSLuigi Rizzo 
11168b8534bSLuigi Rizzo 			if (slot->flags & NS_BUF_CHANGED) {
112f9790aebSLuigi Rizzo 				/* buffer has changed, reload map */
11368b8534bSLuigi Rizzo 				desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr));
11468b8534bSLuigi Rizzo 				desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr));
11568b8534bSLuigi Rizzo 				netmap_reload_map(sc->rl_ldata.rl_tx_mtag,
116f9790aebSLuigi Rizzo 					txd[nic_i].tx_dmamap, addr);
11768b8534bSLuigi Rizzo 			}
118f9790aebSLuigi Rizzo 			slot->flags &= ~(NS_REPORT | NS_BUF_CHANGED);
11968b8534bSLuigi Rizzo 
120f9790aebSLuigi Rizzo 			/* Fill the slot in the NIC ring. */
121f9790aebSLuigi Rizzo 			desc->rl_cmdstat = htole32(cmd);
122f9790aebSLuigi Rizzo 
123f9790aebSLuigi Rizzo 			/* make sure changes to the buffer are synced */
124f9790aebSLuigi Rizzo 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
125f9790aebSLuigi Rizzo 				txd[nic_i].tx_dmamap,
126f9790aebSLuigi Rizzo 				BUS_DMASYNC_PREWRITE);
127f9790aebSLuigi Rizzo 
128f9790aebSLuigi Rizzo 			nm_i = nm_next(nm_i, lim);
129f9790aebSLuigi Rizzo 			nic_i = nm_next(nic_i, lim);
130f9790aebSLuigi Rizzo 		}
131f9790aebSLuigi Rizzo 		sc->rl_ldata.rl_tx_prodidx = nic_i;
132*17885a7bSLuigi Rizzo 		kring->nr_hwcur = head;
133f9790aebSLuigi Rizzo 
134f9790aebSLuigi Rizzo 		/* synchronize the NIC ring */
13568b8534bSLuigi Rizzo 		bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
13668b8534bSLuigi Rizzo 			sc->rl_ldata.rl_tx_list_map,
137f9790aebSLuigi Rizzo 			BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
13868b8534bSLuigi Rizzo 
13968b8534bSLuigi Rizzo 		/* start ? */
14068b8534bSLuigi Rizzo 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
14168b8534bSLuigi Rizzo 	}
142f9790aebSLuigi Rizzo 
143f9790aebSLuigi Rizzo 	/*
144f9790aebSLuigi Rizzo 	 * Second part: reclaim buffers for completed transmissions.
145f9790aebSLuigi Rizzo 	 */
146*17885a7bSLuigi Rizzo 	if (flags & NAF_FORCE_RECLAIM || nm_kr_txempty(kring)) {
147f9790aebSLuigi Rizzo 		nic_i = sc->rl_ldata.rl_tx_considx;
148f9790aebSLuigi Rizzo 		for (n = 0; nic_i != sc->rl_ldata.rl_tx_prodidx;
149f9790aebSLuigi Rizzo 		    n++, nic_i = RL_TX_DESC_NXT(sc, nic_i)) {
150f9790aebSLuigi Rizzo 			uint32_t cmdstat =
151f9790aebSLuigi Rizzo 				le32toh(sc->rl_ldata.rl_tx_list[nic_i].rl_cmdstat);
152f9790aebSLuigi Rizzo 			if (cmdstat & RL_TDESC_STAT_OWN)
153f9790aebSLuigi Rizzo 				break;
154f9790aebSLuigi Rizzo 		}
155f9790aebSLuigi Rizzo 		if (n > 0) {
156f9790aebSLuigi Rizzo 			sc->rl_ldata.rl_tx_considx = nic_i;
157f9790aebSLuigi Rizzo 			sc->rl_ldata.rl_tx_free += n;
158*17885a7bSLuigi Rizzo 			kring->nr_hwtail = nm_prev(netmap_idx_n2k(kring, nic_i), lim);
159f9790aebSLuigi Rizzo 		}
160f9790aebSLuigi Rizzo 	}
161f9790aebSLuigi Rizzo 
162*17885a7bSLuigi Rizzo 	nm_txsync_finalize(kring);
163f9790aebSLuigi Rizzo 
16468b8534bSLuigi Rizzo 	return 0;
16568b8534bSLuigi Rizzo }
16668b8534bSLuigi Rizzo 
16768b8534bSLuigi Rizzo 
16868b8534bSLuigi Rizzo /*
16968b8534bSLuigi Rizzo  * Reconcile kernel and user view of the receive ring.
17068b8534bSLuigi Rizzo  */
17168b8534bSLuigi Rizzo static int
172f9790aebSLuigi Rizzo re_netmap_rxsync(struct netmap_adapter *na, u_int ring_nr, int flags)
17368b8534bSLuigi Rizzo {
174f9790aebSLuigi Rizzo 	struct ifnet *ifp = na->ifp;
17568b8534bSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[ring_nr];
17668b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
177f9790aebSLuigi Rizzo 	u_int nm_i;	/* index into the netmap ring */
178f9790aebSLuigi Rizzo 	u_int nic_i;	/* index into the NIC ring */
179*17885a7bSLuigi Rizzo 	u_int n;
180f9790aebSLuigi Rizzo 	u_int const lim = kring->nkr_num_slots - 1;
181*17885a7bSLuigi Rizzo 	u_int const head = nm_rxsync_prologue(kring);
182ce3ee1e7SLuigi Rizzo 	int force_update = (flags & NAF_FORCE_READ) || kring->nr_kflags & NKR_PENDINTR;
18368b8534bSLuigi Rizzo 
184f9790aebSLuigi Rizzo 	/* device-specific */
185f9790aebSLuigi Rizzo 	struct rl_softc *sc = ifp->if_softc;
186f9790aebSLuigi Rizzo 	struct rl_rxdesc *rxd = sc->rl_ldata.rl_rx_desc;
187f9790aebSLuigi Rizzo 
188*17885a7bSLuigi Rizzo 	if (head > lim)
18968b8534bSLuigi Rizzo 		return netmap_ring_reinit(kring);
19068b8534bSLuigi Rizzo 
19168b8534bSLuigi Rizzo 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
19268b8534bSLuigi Rizzo 			sc->rl_ldata.rl_rx_list_map,
19368b8534bSLuigi Rizzo 			BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
19468b8534bSLuigi Rizzo 
19568b8534bSLuigi Rizzo 	/*
196f9790aebSLuigi Rizzo 	 * First part: import newly received packets.
19764ae02c3SLuigi Rizzo 	 *
198f9790aebSLuigi Rizzo 	 * This device uses all the buffers in the ring, so we need
19968b8534bSLuigi Rizzo 	 * another termination condition in addition to RL_RDESC_STAT_OWN
200*17885a7bSLuigi Rizzo 	 * cleared (all buffers could have it cleared). The easiest one
201*17885a7bSLuigi Rizzo 	 * is to stop right before nm_hwcur.
20268b8534bSLuigi Rizzo 	 */
20364ae02c3SLuigi Rizzo 	if (netmap_no_pendintr || force_update) {
2041dce924dSLuigi Rizzo 		uint16_t slot_flags = kring->nkr_slot_flags;
205*17885a7bSLuigi Rizzo 		uint32_t stop_i = nm_prev(kring->nr_hwcur, lim);
2061dce924dSLuigi Rizzo 
207f9790aebSLuigi Rizzo 		nic_i = sc->rl_ldata.rl_rx_prodidx; /* next pkt to check */
208f9790aebSLuigi Rizzo 		nm_i = netmap_idx_n2k(kring, nic_i);
209f9790aebSLuigi Rizzo 
210*17885a7bSLuigi Rizzo 		while (nm_i != stop_i) {
211f9790aebSLuigi Rizzo 			struct rl_desc *cur_rx = &sc->rl_ldata.rl_rx_list[nic_i];
21268b8534bSLuigi Rizzo 			uint32_t rxstat = le32toh(cur_rx->rl_cmdstat);
21368b8534bSLuigi Rizzo 			uint32_t total_len;
21468b8534bSLuigi Rizzo 
21568b8534bSLuigi Rizzo 			if ((rxstat & RL_RDESC_STAT_OWN) != 0)
21668b8534bSLuigi Rizzo 				break;
21768b8534bSLuigi Rizzo 			total_len = rxstat & sc->rl_rxlenmask;
21868b8534bSLuigi Rizzo 			/* XXX subtract crc */
21968b8534bSLuigi Rizzo 			total_len = (total_len < 4) ? 0 : total_len - 4;
220f9790aebSLuigi Rizzo 			ring->slot[nm_i].len = total_len;
221f9790aebSLuigi Rizzo 			ring->slot[nm_i].flags = slot_flags;
22268b8534bSLuigi Rizzo 			/*  sync was in re_newbuf() */
22368b8534bSLuigi Rizzo 			bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
224f9790aebSLuigi Rizzo 			    rxd[nic_i].rx_dmamap, BUS_DMASYNC_POSTREAD);
225*17885a7bSLuigi Rizzo 			// sc->rl_ifp->if_ipackets++;
226f9790aebSLuigi Rizzo 			nm_i = nm_next(nm_i, lim);
227f9790aebSLuigi Rizzo 			nic_i = nm_next(nic_i, lim);
22868b8534bSLuigi Rizzo 		}
229f9790aebSLuigi Rizzo 		sc->rl_ldata.rl_rx_prodidx = nic_i;
230*17885a7bSLuigi Rizzo 		kring->nr_hwtail = nm_i;
23164ae02c3SLuigi Rizzo 		kring->nr_kflags &= ~NKR_PENDINTR;
23264ae02c3SLuigi Rizzo 	}
23368b8534bSLuigi Rizzo 
234f9790aebSLuigi Rizzo 	/*
235f9790aebSLuigi Rizzo 	 * Second part: skip past packets that userspace has released.
236f9790aebSLuigi Rizzo 	 */
237f9790aebSLuigi Rizzo 	nm_i = kring->nr_hwcur;
238*17885a7bSLuigi Rizzo 	if (nm_i != head) {
239f9790aebSLuigi Rizzo 		nic_i = netmap_idx_k2n(kring, nm_i);
240*17885a7bSLuigi Rizzo 		for (n = 0; nm_i != head; n++) {
241f9790aebSLuigi Rizzo 			struct netmap_slot *slot = &ring->slot[nm_i];
2426e10c8b8SLuigi Rizzo 			uint64_t paddr;
2436e10c8b8SLuigi Rizzo 			void *addr = PNMB(slot, &paddr);
24468b8534bSLuigi Rizzo 
245f9790aebSLuigi Rizzo 			struct rl_desc *desc = &sc->rl_ldata.rl_rx_list[nic_i];
246f9790aebSLuigi Rizzo 			int cmd = NETMAP_BUF_SIZE | RL_RDESC_CMD_OWN;
24768b8534bSLuigi Rizzo 
248f9790aebSLuigi Rizzo 			if (addr == netmap_buffer_base) /* bad buf */
249f9790aebSLuigi Rizzo 				goto ring_reset;
250f9790aebSLuigi Rizzo 
251f9790aebSLuigi Rizzo 			if (nic_i == lim)	/* mark end of ring */
25268b8534bSLuigi Rizzo 				cmd |= RL_RDESC_CMD_EOR;
25368b8534bSLuigi Rizzo 
25468b8534bSLuigi Rizzo 			if (slot->flags & NS_BUF_CHANGED) {
255f9790aebSLuigi Rizzo 				/* buffer has changed, reload map */
25664ae02c3SLuigi Rizzo 				desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr));
25764ae02c3SLuigi Rizzo 				desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr));
258f9790aebSLuigi Rizzo 				netmap_reload_map(sc->rl_ldata.rl_rx_mtag,
259f9790aebSLuigi Rizzo 					rxd[nic_i].rx_dmamap, addr);
26068b8534bSLuigi Rizzo 				slot->flags &= ~NS_BUF_CHANGED;
26168b8534bSLuigi Rizzo 			}
26264ae02c3SLuigi Rizzo 			desc->rl_cmdstat = htole32(cmd);
26368b8534bSLuigi Rizzo 			bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
264f9790aebSLuigi Rizzo 			    rxd[nic_i].rx_dmamap,
265f9790aebSLuigi Rizzo 			    BUS_DMASYNC_PREREAD);
266f9790aebSLuigi Rizzo 			nm_i = nm_next(nm_i, lim);
267f9790aebSLuigi Rizzo 			nic_i = nm_next(nic_i, lim);
26868b8534bSLuigi Rizzo 		}
269*17885a7bSLuigi Rizzo 		kring->nr_hwcur = head;
27068b8534bSLuigi Rizzo 
27168b8534bSLuigi Rizzo 		bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
27268b8534bSLuigi Rizzo 		    sc->rl_ldata.rl_rx_list_map,
273f9790aebSLuigi Rizzo 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
27468b8534bSLuigi Rizzo 	}
275f9790aebSLuigi Rizzo 
276f9790aebSLuigi Rizzo 	/* tell userspace that there might be new packets */
277*17885a7bSLuigi Rizzo 	nm_rxsync_finalize(kring);
278f9790aebSLuigi Rizzo 
27968b8534bSLuigi Rizzo 	return 0;
280f9790aebSLuigi Rizzo 
281f9790aebSLuigi Rizzo ring_reset:
282f9790aebSLuigi Rizzo 	return netmap_ring_reinit(kring);
28368b8534bSLuigi Rizzo }
28468b8534bSLuigi Rizzo 
285f9790aebSLuigi Rizzo 
286506cc70cSLuigi Rizzo /*
287506cc70cSLuigi Rizzo  * Additional routines to init the tx and rx rings.
288506cc70cSLuigi Rizzo  * In other drivers we do that inline in the main code.
289506cc70cSLuigi Rizzo  */
29068b8534bSLuigi Rizzo static void
29168b8534bSLuigi Rizzo re_netmap_tx_init(struct rl_softc *sc)
29268b8534bSLuigi Rizzo {
29368b8534bSLuigi Rizzo 	struct rl_txdesc *txd;
29468b8534bSLuigi Rizzo 	struct rl_desc *desc;
295506cc70cSLuigi Rizzo 	int i, n;
29668b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(sc->rl_ifp);
297f9790aebSLuigi Rizzo 	struct netmap_slot *slot;
29868b8534bSLuigi Rizzo 
299f9790aebSLuigi Rizzo 	if (!na || !(na->na_flags & NAF_NATIVE_ON)) {
300f9790aebSLuigi Rizzo 		return;
301f9790aebSLuigi Rizzo 	}
302f9790aebSLuigi Rizzo 
303f9790aebSLuigi Rizzo 	slot = netmap_reset(na, NR_TX, 0, 0);
30468b8534bSLuigi Rizzo 	/* slot is NULL if we are not in netmap mode */
30568b8534bSLuigi Rizzo 	if (!slot)
306f9790aebSLuigi Rizzo 		return;  // XXX cannot happen
30768b8534bSLuigi Rizzo 	/* in netmap mode, overwrite addresses and maps */
30868b8534bSLuigi Rizzo 	txd = sc->rl_ldata.rl_tx_desc;
30968b8534bSLuigi Rizzo 	desc = sc->rl_ldata.rl_tx_list;
310506cc70cSLuigi Rizzo 	n = sc->rl_ldata.rl_tx_desc_cnt;
31168b8534bSLuigi Rizzo 
312506cc70cSLuigi Rizzo 	/* l points in the netmap ring, i points in the NIC ring */
313506cc70cSLuigi Rizzo 	for (i = 0; i < n; i++) {
314506cc70cSLuigi Rizzo 		uint64_t paddr;
31564ae02c3SLuigi Rizzo 		int l = netmap_idx_n2k(&na->tx_rings[0], i);
316babc7c12SLuigi Rizzo 		void *addr = PNMB(slot + l, &paddr);
317506cc70cSLuigi Rizzo 
31868b8534bSLuigi Rizzo 		desc[i].rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr));
31968b8534bSLuigi Rizzo 		desc[i].rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr));
32068b8534bSLuigi Rizzo 		netmap_load_map(sc->rl_ldata.rl_tx_mtag,
3216e10c8b8SLuigi Rizzo 			txd[i].tx_dmamap, addr);
32268b8534bSLuigi Rizzo 	}
32368b8534bSLuigi Rizzo }
32468b8534bSLuigi Rizzo 
32568b8534bSLuigi Rizzo static void
32668b8534bSLuigi Rizzo re_netmap_rx_init(struct rl_softc *sc)
32768b8534bSLuigi Rizzo {
32868b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(sc->rl_ifp);
32968b8534bSLuigi Rizzo 	struct netmap_slot *slot = netmap_reset(na, NR_RX, 0, 0);
33068b8534bSLuigi Rizzo 	struct rl_desc *desc = sc->rl_ldata.rl_rx_list;
33168b8534bSLuigi Rizzo 	uint32_t cmdstat;
332*17885a7bSLuigi Rizzo 	uint32_t nic_i, max_avail;
333*17885a7bSLuigi Rizzo 	uint32_t const n = sc->rl_ldata.rl_rx_desc_cnt;
33468b8534bSLuigi Rizzo 
33568b8534bSLuigi Rizzo 	if (!slot)
33668b8534bSLuigi Rizzo 		return;
3375644ccecSLuigi Rizzo 	/*
338*17885a7bSLuigi Rizzo 	 * Do not release the slots owned by userspace,
339*17885a7bSLuigi Rizzo 	 * and also keep one empty.
3405644ccecSLuigi Rizzo 	 */
341*17885a7bSLuigi Rizzo 	max_avail = n - 1 - nm_kr_rxspace(&na->rx_rings[0]);
342*17885a7bSLuigi Rizzo 	for (nic_i = 0; nic_i < n; nic_i++) {
343506cc70cSLuigi Rizzo 		void *addr;
344506cc70cSLuigi Rizzo 		uint64_t paddr;
345*17885a7bSLuigi Rizzo 		uint32_t nm_i = netmap_idx_n2k(&na->rx_rings[0], nic_i);
34668b8534bSLuigi Rizzo 
347*17885a7bSLuigi Rizzo 		addr = PNMB(slot + nm_i, &paddr);
3486e10c8b8SLuigi Rizzo 
3496e10c8b8SLuigi Rizzo 		netmap_reload_map(sc->rl_ldata.rl_rx_mtag,
350*17885a7bSLuigi Rizzo 		    sc->rl_ldata.rl_rx_desc[nic_i].rx_dmamap, addr);
3516e10c8b8SLuigi Rizzo 		bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
352*17885a7bSLuigi Rizzo 		    sc->rl_ldata.rl_rx_desc[nic_i].rx_dmamap, BUS_DMASYNC_PREREAD);
353*17885a7bSLuigi Rizzo 		desc[nic_i].rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr));
354*17885a7bSLuigi Rizzo 		desc[nic_i].rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr));
35582d2fe10SLuigi Rizzo 		cmdstat = NETMAP_BUF_SIZE;
356*17885a7bSLuigi Rizzo 		if (nic_i == n - 1) /* mark the end of ring */
35768b8534bSLuigi Rizzo 			cmdstat |= RL_RDESC_CMD_EOR;
358*17885a7bSLuigi Rizzo 		if (nic_i < max_avail)
359506cc70cSLuigi Rizzo 			cmdstat |= RL_RDESC_CMD_OWN;
360*17885a7bSLuigi Rizzo 		desc[nic_i].rl_cmdstat = htole32(cmdstat);
36168b8534bSLuigi Rizzo 	}
36268b8534bSLuigi Rizzo }
36364ae02c3SLuigi Rizzo 
36464ae02c3SLuigi Rizzo 
36564ae02c3SLuigi Rizzo static void
36664ae02c3SLuigi Rizzo re_netmap_attach(struct rl_softc *sc)
36764ae02c3SLuigi Rizzo {
36864ae02c3SLuigi Rizzo 	struct netmap_adapter na;
36964ae02c3SLuigi Rizzo 
37064ae02c3SLuigi Rizzo 	bzero(&na, sizeof(na));
37164ae02c3SLuigi Rizzo 
37264ae02c3SLuigi Rizzo 	na.ifp = sc->rl_ifp;
373ce3ee1e7SLuigi Rizzo 	na.na_flags = NAF_BDG_MAYSLEEP;
37464ae02c3SLuigi Rizzo 	na.num_tx_desc = sc->rl_ldata.rl_tx_desc_cnt;
37564ae02c3SLuigi Rizzo 	na.num_rx_desc = sc->rl_ldata.rl_rx_desc_cnt;
37664ae02c3SLuigi Rizzo 	na.nm_txsync = re_netmap_txsync;
37764ae02c3SLuigi Rizzo 	na.nm_rxsync = re_netmap_rxsync;
37864ae02c3SLuigi Rizzo 	na.nm_register = re_netmap_reg;
379f9790aebSLuigi Rizzo 	na.num_tx_rings = na.num_rx_rings = 1;
380f9790aebSLuigi Rizzo 	netmap_attach(&na);
38164ae02c3SLuigi Rizzo }
382f9790aebSLuigi Rizzo 
38364ae02c3SLuigi Rizzo /* end of file */
384