xref: /freebsd/sys/dev/netmap/if_re_netmap.h (revision 6e10c8b8c596d1c03428c74b6e243530c0da5813)
168b8534bSLuigi Rizzo /*
268b8534bSLuigi Rizzo  * Copyright (C) 2011 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$
28506cc70cSLuigi Rizzo  * $Id: if_re_netmap.h 9802 2011-12-02 18:42:37Z luigi $
2968b8534bSLuigi Rizzo  *
3068b8534bSLuigi Rizzo  * netmap support for if_re
3168b8534bSLuigi Rizzo  */
3268b8534bSLuigi Rizzo 
3368b8534bSLuigi Rizzo #include <net/netmap.h>
3468b8534bSLuigi Rizzo #include <sys/selinfo.h>
3568b8534bSLuigi Rizzo #include <vm/vm.h>
3668b8534bSLuigi Rizzo #include <vm/pmap.h>    /* vtophys ? */
3768b8534bSLuigi Rizzo #include <dev/netmap/netmap_kern.h>
3868b8534bSLuigi Rizzo 
3968b8534bSLuigi Rizzo static int re_netmap_reg(struct ifnet *, int onoff);
4068b8534bSLuigi Rizzo static int re_netmap_txsync(void *, u_int, int);
4168b8534bSLuigi Rizzo static int re_netmap_rxsync(void *, u_int, int);
4268b8534bSLuigi Rizzo static void re_netmap_lock_wrapper(void *, int, u_int);
4368b8534bSLuigi Rizzo 
4468b8534bSLuigi Rizzo static void
4568b8534bSLuigi Rizzo re_netmap_attach(struct rl_softc *sc)
4668b8534bSLuigi Rizzo {
4768b8534bSLuigi Rizzo 	struct netmap_adapter na;
4868b8534bSLuigi Rizzo 
4968b8534bSLuigi Rizzo 	bzero(&na, sizeof(na));
5068b8534bSLuigi Rizzo 
5168b8534bSLuigi Rizzo 	na.ifp = sc->rl_ifp;
5268b8534bSLuigi Rizzo 	na.separate_locks = 0;
5368b8534bSLuigi Rizzo 	na.num_tx_desc = sc->rl_ldata.rl_tx_desc_cnt;
5468b8534bSLuigi Rizzo 	na.num_rx_desc = sc->rl_ldata.rl_rx_desc_cnt;
5568b8534bSLuigi Rizzo 	na.nm_txsync = re_netmap_txsync;
5668b8534bSLuigi Rizzo 	na.nm_rxsync = re_netmap_rxsync;
5768b8534bSLuigi Rizzo 	na.nm_lock = re_netmap_lock_wrapper;
5868b8534bSLuigi Rizzo 	na.nm_register = re_netmap_reg;
59506cc70cSLuigi Rizzo 	na.buff_size = NETMAP_BUF_SIZE;
6068b8534bSLuigi Rizzo 	netmap_attach(&na, 1);
6168b8534bSLuigi Rizzo }
6268b8534bSLuigi Rizzo 
6368b8534bSLuigi Rizzo 
6468b8534bSLuigi Rizzo /*
6568b8534bSLuigi Rizzo  * wrapper to export locks to the generic code
6668b8534bSLuigi Rizzo  * We should not use the tx/rx locks
6768b8534bSLuigi Rizzo  */
6868b8534bSLuigi Rizzo static void
6968b8534bSLuigi Rizzo re_netmap_lock_wrapper(void *_a, int what, u_int queueid)
7068b8534bSLuigi Rizzo {
7168b8534bSLuigi Rizzo 	struct rl_softc *adapter = _a;
7268b8534bSLuigi Rizzo 
7368b8534bSLuigi Rizzo 	switch (what) {
7468b8534bSLuigi Rizzo 	case NETMAP_CORE_LOCK:
7568b8534bSLuigi Rizzo 		RL_LOCK(adapter);
7668b8534bSLuigi Rizzo 		break;
7768b8534bSLuigi Rizzo 	case NETMAP_CORE_UNLOCK:
7868b8534bSLuigi Rizzo 		RL_UNLOCK(adapter);
7968b8534bSLuigi Rizzo 		break;
8068b8534bSLuigi Rizzo 
8168b8534bSLuigi Rizzo 	case NETMAP_TX_LOCK:
8268b8534bSLuigi Rizzo 	case NETMAP_RX_LOCK:
8368b8534bSLuigi Rizzo 	case NETMAP_TX_UNLOCK:
8468b8534bSLuigi Rizzo 	case NETMAP_RX_UNLOCK:
8568b8534bSLuigi Rizzo 		D("invalid lock call %d, no tx/rx locks here", what);
8668b8534bSLuigi Rizzo 		break;
8768b8534bSLuigi Rizzo 	}
8868b8534bSLuigi Rizzo }
8968b8534bSLuigi Rizzo 
9068b8534bSLuigi Rizzo 
9168b8534bSLuigi Rizzo /*
9268b8534bSLuigi Rizzo  * support for netmap register/unregisted. We are already under core lock.
9368b8534bSLuigi Rizzo  * only called on the first register or the last unregister.
9468b8534bSLuigi Rizzo  */
9568b8534bSLuigi Rizzo static int
9668b8534bSLuigi Rizzo re_netmap_reg(struct ifnet *ifp, int onoff)
9768b8534bSLuigi Rizzo {
9868b8534bSLuigi Rizzo 	struct rl_softc *adapter = ifp->if_softc;
9968b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
10068b8534bSLuigi Rizzo 	int error = 0;
10168b8534bSLuigi Rizzo 
102506cc70cSLuigi Rizzo 	if (na == NULL)
10368b8534bSLuigi Rizzo 		return EINVAL;
10468b8534bSLuigi Rizzo 	/* Tell the stack that the interface is no longer active */
10568b8534bSLuigi Rizzo 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
10668b8534bSLuigi Rizzo 
10768b8534bSLuigi Rizzo 	re_stop(adapter);
10868b8534bSLuigi Rizzo 
10968b8534bSLuigi Rizzo 	if (onoff) {
11068b8534bSLuigi Rizzo 		ifp->if_capenable |= IFCAP_NETMAP;
11168b8534bSLuigi Rizzo 
112506cc70cSLuigi Rizzo 		/* save if_transmit to restore it later */
11368b8534bSLuigi Rizzo 		na->if_transmit = ifp->if_transmit;
11468b8534bSLuigi Rizzo 		ifp->if_transmit = netmap_start;
11568b8534bSLuigi Rizzo 
11668b8534bSLuigi Rizzo 		re_init_locked(adapter);
11768b8534bSLuigi Rizzo 
11868b8534bSLuigi Rizzo 		if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) == 0) {
11968b8534bSLuigi Rizzo 			error = ENOMEM;
12068b8534bSLuigi Rizzo 			goto fail;
12168b8534bSLuigi Rizzo 		}
12268b8534bSLuigi Rizzo 	} else {
12368b8534bSLuigi Rizzo fail:
12468b8534bSLuigi Rizzo 		/* restore if_transmit */
12568b8534bSLuigi Rizzo 		ifp->if_transmit = na->if_transmit;
12668b8534bSLuigi Rizzo 		ifp->if_capenable &= ~IFCAP_NETMAP;
12768b8534bSLuigi Rizzo 		re_init_locked(adapter);	/* also enables intr */
12868b8534bSLuigi Rizzo 	}
12968b8534bSLuigi Rizzo 	return (error);
13068b8534bSLuigi Rizzo }
13168b8534bSLuigi Rizzo 
13268b8534bSLuigi Rizzo 
13368b8534bSLuigi Rizzo /*
13468b8534bSLuigi Rizzo  * Reconcile kernel and user view of the transmit ring.
13568b8534bSLuigi Rizzo  */
13668b8534bSLuigi Rizzo static int
13768b8534bSLuigi Rizzo re_netmap_txsync(void *a, u_int ring_nr, int do_lock)
13868b8534bSLuigi Rizzo {
13968b8534bSLuigi Rizzo 	struct rl_softc *sc = a;
14068b8534bSLuigi Rizzo 	struct rl_txdesc *txd = sc->rl_ldata.rl_tx_desc;
14168b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(sc->rl_ifp);
14268b8534bSLuigi Rizzo 	struct netmap_kring *kring = &na->tx_rings[ring_nr];
14368b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
144506cc70cSLuigi Rizzo 	int j, k, l, n, lim = kring->nkr_num_slots - 1;
14568b8534bSLuigi Rizzo 
14668b8534bSLuigi Rizzo 	k = ring->cur;
147506cc70cSLuigi Rizzo 	if (k > lim)
14868b8534bSLuigi Rizzo 		return netmap_ring_reinit(kring);
14968b8534bSLuigi Rizzo 
15068b8534bSLuigi Rizzo 	if (do_lock)
15168b8534bSLuigi Rizzo 		RL_LOCK(sc);
15268b8534bSLuigi Rizzo 
15368b8534bSLuigi Rizzo 	/* Sync the TX descriptor list */
15468b8534bSLuigi Rizzo 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
15568b8534bSLuigi Rizzo             sc->rl_ldata.rl_tx_list_map,
15668b8534bSLuigi Rizzo             BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
15768b8534bSLuigi Rizzo 
158506cc70cSLuigi Rizzo 	/* XXX move after the transmissions */
15968b8534bSLuigi Rizzo 	/* record completed transmissions */
160506cc70cSLuigi Rizzo         for (n = 0, l = sc->rl_ldata.rl_tx_considx;
161506cc70cSLuigi Rizzo 	    l != sc->rl_ldata.rl_tx_prodidx;
162506cc70cSLuigi Rizzo 	    n++, l = RL_TX_DESC_NXT(sc, l)) {
16368b8534bSLuigi Rizzo 		uint32_t cmdstat =
164506cc70cSLuigi Rizzo 			le32toh(sc->rl_ldata.rl_tx_list[l].rl_cmdstat);
16568b8534bSLuigi Rizzo 		if (cmdstat & RL_TDESC_STAT_OWN)
16668b8534bSLuigi Rizzo 			break;
16768b8534bSLuigi Rizzo 	}
16868b8534bSLuigi Rizzo 	if (n > 0) {
169506cc70cSLuigi Rizzo 		sc->rl_ldata.rl_tx_considx = l;
17068b8534bSLuigi Rizzo 		sc->rl_ldata.rl_tx_free += n;
17168b8534bSLuigi Rizzo 		kring->nr_hwavail += n;
17268b8534bSLuigi Rizzo 	}
17368b8534bSLuigi Rizzo 
17468b8534bSLuigi Rizzo 	/* update avail to what the hardware knows */
17568b8534bSLuigi Rizzo 	ring->avail = kring->nr_hwavail;
17668b8534bSLuigi Rizzo 
177506cc70cSLuigi Rizzo 	j = kring->nr_hwcur;
17868b8534bSLuigi Rizzo 	if (j != k) {	/* we have new packets to send */
17968b8534bSLuigi Rizzo 		n = 0;
180506cc70cSLuigi Rizzo 		l = sc->rl_ldata.rl_tx_prodidx;
18168b8534bSLuigi Rizzo 		while (j != k) {
18268b8534bSLuigi Rizzo 			struct netmap_slot *slot = &ring->slot[j];
183506cc70cSLuigi Rizzo 			struct rl_desc *desc = &sc->rl_ldata.rl_tx_list[l];
18468b8534bSLuigi Rizzo 			int cmd = slot->len | RL_TDESC_CMD_EOF |
18568b8534bSLuigi Rizzo 				RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF ;
186*6e10c8b8SLuigi Rizzo 			uint64_t paddr;
187*6e10c8b8SLuigi Rizzo 			void *addr = PNMB(slot, &paddr);
18868b8534bSLuigi Rizzo 			int len = slot->len;
18968b8534bSLuigi Rizzo 
19068b8534bSLuigi Rizzo 			if (addr == netmap_buffer_base || len > NETMAP_BUF_SIZE) {
19168b8534bSLuigi Rizzo 				if (do_lock)
19268b8534bSLuigi Rizzo 					RL_UNLOCK(sc);
193506cc70cSLuigi Rizzo 				// XXX what about prodidx ?
19468b8534bSLuigi Rizzo 				return netmap_ring_reinit(kring);
19568b8534bSLuigi Rizzo 			}
19668b8534bSLuigi Rizzo 
197506cc70cSLuigi Rizzo 			if (l == lim)	/* mark end of ring */
19868b8534bSLuigi Rizzo 				cmd |= RL_TDESC_CMD_EOR;
19968b8534bSLuigi Rizzo 
20068b8534bSLuigi Rizzo 			if (slot->flags & NS_BUF_CHANGED) {
20168b8534bSLuigi Rizzo 				desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr));
20268b8534bSLuigi Rizzo 				desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr));
20368b8534bSLuigi Rizzo 				/* buffer has changed, unload and reload map */
20468b8534bSLuigi Rizzo 				netmap_reload_map(sc->rl_ldata.rl_tx_mtag,
205*6e10c8b8SLuigi Rizzo 					txd[l].tx_dmamap, addr);
20668b8534bSLuigi Rizzo 				slot->flags &= ~NS_BUF_CHANGED;
20768b8534bSLuigi Rizzo 			}
20868b8534bSLuigi Rizzo 			slot->flags &= ~NS_REPORT;
20968b8534bSLuigi Rizzo 			desc->rl_cmdstat = htole32(cmd);
21068b8534bSLuigi Rizzo 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
211506cc70cSLuigi Rizzo 				txd[l].tx_dmamap, BUS_DMASYNC_PREWRITE);
21268b8534bSLuigi Rizzo 			j = (j == lim) ? 0 : j + 1;
213506cc70cSLuigi Rizzo 			l = (l == lim) ? 0 : l + 1;
21468b8534bSLuigi Rizzo 			n++;
21568b8534bSLuigi Rizzo 		}
216506cc70cSLuigi Rizzo 		sc->rl_ldata.rl_tx_prodidx = l;
217506cc70cSLuigi Rizzo 		kring->nr_hwcur = k;
21868b8534bSLuigi Rizzo 
21968b8534bSLuigi Rizzo 		/* decrease avail by number of sent packets */
22068b8534bSLuigi Rizzo 		ring->avail -= n;
22168b8534bSLuigi Rizzo 		kring->nr_hwavail = ring->avail;
22268b8534bSLuigi Rizzo 
22368b8534bSLuigi Rizzo 		bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
22468b8534bSLuigi Rizzo 		    sc->rl_ldata.rl_tx_list_map,
22568b8534bSLuigi Rizzo 		    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
22668b8534bSLuigi Rizzo 
22768b8534bSLuigi Rizzo 		/* start ? */
22868b8534bSLuigi Rizzo 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
22968b8534bSLuigi Rizzo 	}
23068b8534bSLuigi Rizzo 	if (do_lock)
23168b8534bSLuigi Rizzo 		RL_UNLOCK(sc);
23268b8534bSLuigi Rizzo 	return 0;
23368b8534bSLuigi Rizzo }
23468b8534bSLuigi Rizzo 
23568b8534bSLuigi Rizzo 
23668b8534bSLuigi Rizzo /*
23768b8534bSLuigi Rizzo  * Reconcile kernel and user view of the receive ring.
23868b8534bSLuigi Rizzo  */
23968b8534bSLuigi Rizzo static int
24068b8534bSLuigi Rizzo re_netmap_rxsync(void *a, u_int ring_nr, int do_lock)
24168b8534bSLuigi Rizzo {
24268b8534bSLuigi Rizzo 	struct rl_softc *sc = a;
24368b8534bSLuigi Rizzo 	struct rl_rxdesc *rxd = sc->rl_ldata.rl_rx_desc;
24468b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(sc->rl_ifp);
24568b8534bSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[ring_nr];
24668b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
247506cc70cSLuigi Rizzo 	int j, k, l, n, lim = kring->nkr_num_slots - 1;
24868b8534bSLuigi Rizzo 
24968b8534bSLuigi Rizzo 	k = ring->cur;
250506cc70cSLuigi Rizzo 	if (k > lim)
25168b8534bSLuigi Rizzo 		return netmap_ring_reinit(kring);
25268b8534bSLuigi Rizzo 
25368b8534bSLuigi Rizzo 	if (do_lock)
25468b8534bSLuigi Rizzo 		RL_LOCK(sc);
25568b8534bSLuigi Rizzo 	/* XXX check sync modes */
25668b8534bSLuigi Rizzo 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
25768b8534bSLuigi Rizzo 	    sc->rl_ldata.rl_rx_list_map,
25868b8534bSLuigi Rizzo 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
25968b8534bSLuigi Rizzo 
26068b8534bSLuigi Rizzo 	/*
26168b8534bSLuigi Rizzo 	 * The device uses all the buffers in the ring, so we need
26268b8534bSLuigi Rizzo 	 * another termination condition in addition to RL_RDESC_STAT_OWN
26368b8534bSLuigi Rizzo 	 * cleared (all buffers could have it cleared. The easiest one
26468b8534bSLuigi Rizzo 	 * is to limit the amount of data reported up to 'lim'
26568b8534bSLuigi Rizzo 	 */
266506cc70cSLuigi Rizzo 	l = sc->rl_ldata.rl_rx_prodidx; /* next pkt to check */
267506cc70cSLuigi Rizzo 	j = l + kring->nkr_hwofs;
26868b8534bSLuigi Rizzo 	for (n = kring->nr_hwavail; n < lim ; n++) {
269506cc70cSLuigi Rizzo 		struct rl_desc *cur_rx = &sc->rl_ldata.rl_rx_list[l];
27068b8534bSLuigi Rizzo 		uint32_t rxstat = le32toh(cur_rx->rl_cmdstat);
27168b8534bSLuigi Rizzo 		uint32_t total_len;
27268b8534bSLuigi Rizzo 
27368b8534bSLuigi Rizzo 		if ((rxstat & RL_RDESC_STAT_OWN) != 0)
27468b8534bSLuigi Rizzo 			break;
27568b8534bSLuigi Rizzo 		total_len = rxstat & sc->rl_rxlenmask;
27668b8534bSLuigi Rizzo 		/* XXX subtract crc */
27768b8534bSLuigi Rizzo 		total_len = (total_len < 4) ? 0 : total_len - 4;
27868b8534bSLuigi Rizzo 		kring->ring->slot[j].len = total_len;
27968b8534bSLuigi Rizzo 		/*  sync was in re_newbuf() */
28068b8534bSLuigi Rizzo 		bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
281506cc70cSLuigi Rizzo 		    rxd[l].rx_dmamap, BUS_DMASYNC_POSTREAD);
282506cc70cSLuigi Rizzo 		j = (j == lim) ? 0 : j + 1;
283506cc70cSLuigi Rizzo 		l = (l == lim) ? 0 : l + 1;
28468b8534bSLuigi Rizzo 	}
28568b8534bSLuigi Rizzo 	if (n != kring->nr_hwavail) {
286506cc70cSLuigi Rizzo 		sc->rl_ldata.rl_rx_prodidx = l;
28768b8534bSLuigi Rizzo 		sc->rl_ifp->if_ipackets += n - kring->nr_hwavail;
28868b8534bSLuigi Rizzo 		kring->nr_hwavail = n;
28968b8534bSLuigi Rizzo 	}
29068b8534bSLuigi Rizzo 
29168b8534bSLuigi Rizzo 	/* skip past packets that userspace has already processed,
29268b8534bSLuigi Rizzo 	 * making them available for reception.
29368b8534bSLuigi Rizzo 	 * advance nr_hwcur and issue a bus_dmamap_sync on the
29468b8534bSLuigi Rizzo 	 * buffers so it is safe to write to them.
29568b8534bSLuigi Rizzo 	 * Also increase nr_hwavail
29668b8534bSLuigi Rizzo 	 */
29768b8534bSLuigi Rizzo 	j = kring->nr_hwcur;
29868b8534bSLuigi Rizzo 	if (j != k) {	/* userspace has read some packets. */
29968b8534bSLuigi Rizzo 		n = 0;
300506cc70cSLuigi Rizzo 		l = kring->nr_hwcur - kring->nkr_hwofs;
301506cc70cSLuigi Rizzo 		if (l < 0)
302506cc70cSLuigi Rizzo 			l += lim + 1;
30368b8534bSLuigi Rizzo 		while (j != k) {
30468b8534bSLuigi Rizzo 			struct netmap_slot *slot = ring->slot + j;
305506cc70cSLuigi Rizzo 			struct rl_desc *desc = &sc->rl_ldata.rl_rx_list[l];
30668b8534bSLuigi Rizzo 			int cmd = na->buff_size | RL_RDESC_CMD_OWN;
307*6e10c8b8SLuigi Rizzo 			uint64_t paddr;
308*6e10c8b8SLuigi Rizzo 			void *addr = PNMB(slot, &paddr);
30968b8534bSLuigi Rizzo 
31068b8534bSLuigi Rizzo 			if (addr == netmap_buffer_base) { /* bad buf */
31168b8534bSLuigi Rizzo 				if (do_lock)
31268b8534bSLuigi Rizzo 					RL_UNLOCK(sc);
31368b8534bSLuigi Rizzo 				return netmap_ring_reinit(kring);
31468b8534bSLuigi Rizzo 			}
31568b8534bSLuigi Rizzo 
316506cc70cSLuigi Rizzo 			if (l == lim)	/* mark end of ring */
31768b8534bSLuigi Rizzo 				cmd |= RL_RDESC_CMD_EOR;
31868b8534bSLuigi Rizzo 
31968b8534bSLuigi Rizzo 			desc->rl_cmdstat = htole32(cmd);
32068b8534bSLuigi Rizzo 			slot->flags &= ~NS_REPORT;
32168b8534bSLuigi Rizzo 			if (slot->flags & NS_BUF_CHANGED) {
32268b8534bSLuigi Rizzo 				desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr));
32368b8534bSLuigi Rizzo 				desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr));
32468b8534bSLuigi Rizzo 				netmap_reload_map(sc->rl_ldata.rl_rx_mtag,
325*6e10c8b8SLuigi Rizzo 					rxd[l].rx_dmamap, addr);
32668b8534bSLuigi Rizzo 				slot->flags &= ~NS_BUF_CHANGED;
32768b8534bSLuigi Rizzo 			}
32868b8534bSLuigi Rizzo 			bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
329506cc70cSLuigi Rizzo 				rxd[l].rx_dmamap, BUS_DMASYNC_PREREAD);
33068b8534bSLuigi Rizzo 			j = (j == lim) ? 0 : j + 1;
331506cc70cSLuigi Rizzo 			l = (l == lim) ? 0 : l + 1;
33268b8534bSLuigi Rizzo 			n++;
33368b8534bSLuigi Rizzo 		}
33468b8534bSLuigi Rizzo 		kring->nr_hwavail -= n;
33568b8534bSLuigi Rizzo 		kring->nr_hwcur = k;
33668b8534bSLuigi Rizzo 		/* Flush the RX DMA ring */
33768b8534bSLuigi Rizzo 
33868b8534bSLuigi Rizzo 		bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
33968b8534bSLuigi Rizzo 		    sc->rl_ldata.rl_rx_list_map,
34068b8534bSLuigi Rizzo 		    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
34168b8534bSLuigi Rizzo 	}
34268b8534bSLuigi Rizzo 	/* tell userspace that there are new packets */
34368b8534bSLuigi Rizzo 	ring->avail = kring->nr_hwavail;
34468b8534bSLuigi Rizzo 	if (do_lock)
34568b8534bSLuigi Rizzo 		RL_UNLOCK(sc);
34668b8534bSLuigi Rizzo 	return 0;
34768b8534bSLuigi Rizzo }
34868b8534bSLuigi Rizzo 
349506cc70cSLuigi Rizzo /*
350506cc70cSLuigi Rizzo  * Additional routines to init the tx and rx rings.
351506cc70cSLuigi Rizzo  * In other drivers we do that inline in the main code.
352506cc70cSLuigi Rizzo  */
35368b8534bSLuigi Rizzo static void
35468b8534bSLuigi Rizzo re_netmap_tx_init(struct rl_softc *sc)
35568b8534bSLuigi Rizzo {
35668b8534bSLuigi Rizzo 	struct rl_txdesc *txd;
35768b8534bSLuigi Rizzo 	struct rl_desc *desc;
358506cc70cSLuigi Rizzo 	int i, n;
35968b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(sc->rl_ifp);
36068b8534bSLuigi Rizzo 	struct netmap_slot *slot = netmap_reset(na, NR_TX, 0, 0);
36168b8534bSLuigi Rizzo 
36268b8534bSLuigi Rizzo 	/* slot is NULL if we are not in netmap mode */
36368b8534bSLuigi Rizzo 	if (!slot)
36468b8534bSLuigi Rizzo 		return;
36568b8534bSLuigi Rizzo 	/* in netmap mode, overwrite addresses and maps */
36668b8534bSLuigi Rizzo 	txd = sc->rl_ldata.rl_tx_desc;
36768b8534bSLuigi Rizzo 	desc = sc->rl_ldata.rl_tx_list;
368506cc70cSLuigi Rizzo 	n = sc->rl_ldata.rl_tx_desc_cnt;
36968b8534bSLuigi Rizzo 
370506cc70cSLuigi Rizzo 	/* l points in the netmap ring, i points in the NIC ring */
371506cc70cSLuigi Rizzo 	for (i = 0; i < n; i++) {
372506cc70cSLuigi Rizzo 		void *addr;
373506cc70cSLuigi Rizzo 		uint64_t paddr;
374506cc70cSLuigi Rizzo 		struct netmap_kring *kring = &na->tx_rings[0];
375506cc70cSLuigi Rizzo 		int l = i + kring->nkr_hwofs;
37668b8534bSLuigi Rizzo 
377506cc70cSLuigi Rizzo 		if (l >= n)
378506cc70cSLuigi Rizzo 			l -= n;
379506cc70cSLuigi Rizzo 
380*6e10c8b8SLuigi Rizzo 		addr = PNMB(slot + l, &paddr);
38168b8534bSLuigi Rizzo 		desc[i].rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr));
38268b8534bSLuigi Rizzo 		desc[i].rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr));
38368b8534bSLuigi Rizzo 		netmap_load_map(sc->rl_ldata.rl_tx_mtag,
384*6e10c8b8SLuigi Rizzo 			txd[i].tx_dmamap, addr);
38568b8534bSLuigi Rizzo 	}
38668b8534bSLuigi Rizzo }
38768b8534bSLuigi Rizzo 
38868b8534bSLuigi Rizzo static void
38968b8534bSLuigi Rizzo re_netmap_rx_init(struct rl_softc *sc)
39068b8534bSLuigi Rizzo {
39168b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(sc->rl_ifp);
39268b8534bSLuigi Rizzo 	struct netmap_slot *slot = netmap_reset(na, NR_RX, 0, 0);
39368b8534bSLuigi Rizzo 	struct rl_desc *desc = sc->rl_ldata.rl_rx_list;
39468b8534bSLuigi Rizzo 	uint32_t cmdstat;
395506cc70cSLuigi Rizzo 	int i, n;
39668b8534bSLuigi Rizzo 
39768b8534bSLuigi Rizzo 	if (!slot)
39868b8534bSLuigi Rizzo 		return;
399506cc70cSLuigi Rizzo 	n = sc->rl_ldata.rl_rx_desc_cnt;
400506cc70cSLuigi Rizzo 	for (i = 0; i < n; i++) {
401506cc70cSLuigi Rizzo 		void *addr;
402506cc70cSLuigi Rizzo 		uint64_t paddr;
403506cc70cSLuigi Rizzo 		struct netmap_kring *kring = &na->rx_rings[0];
404506cc70cSLuigi Rizzo 		int l = i + kring->nkr_hwofs;
40568b8534bSLuigi Rizzo 
406506cc70cSLuigi Rizzo 		if (l >= n)
407506cc70cSLuigi Rizzo 			l -= n;
40868b8534bSLuigi Rizzo 
409*6e10c8b8SLuigi Rizzo 		addr = PNMB(slot + l, &paddr);
410*6e10c8b8SLuigi Rizzo 
411*6e10c8b8SLuigi Rizzo 		netmap_reload_map(sc->rl_ldata.rl_rx_mtag,
412*6e10c8b8SLuigi Rizzo 			sc->rl_ldata.rl_rx_desc[i].rx_dmamap, addr);
413*6e10c8b8SLuigi Rizzo 		bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
414*6e10c8b8SLuigi Rizzo 		    sc->rl_ldata.rl_rx_desc[i].rx_dmamap, BUS_DMASYNC_PREREAD);
41568b8534bSLuigi Rizzo 		desc[i].rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr));
41668b8534bSLuigi Rizzo 		desc[i].rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr));
417506cc70cSLuigi Rizzo 		cmdstat = na->buff_size;
418506cc70cSLuigi Rizzo 		if (i == n - 1)
41968b8534bSLuigi Rizzo 			cmdstat |= RL_RDESC_CMD_EOR;
420506cc70cSLuigi Rizzo 		/*
421506cc70cSLuigi Rizzo 		 * userspace knows that hwavail packets were ready before the
422506cc70cSLuigi Rizzo 		 * reset, so we need to tell the NIC that last hwavail
423506cc70cSLuigi Rizzo 		 * descriptors of the ring are still owned by the driver.
424506cc70cSLuigi Rizzo 		 */
425506cc70cSLuigi Rizzo 		if (i < n - 1 - kring->nr_hwavail) // XXX + 1 ?
426506cc70cSLuigi Rizzo 			cmdstat |= RL_RDESC_CMD_OWN;
427506cc70cSLuigi Rizzo 		desc[i].rl_cmdstat = htole32(cmdstat);
42868b8534bSLuigi Rizzo 	}
42968b8534bSLuigi Rizzo }
430