xref: /freebsd/sys/dev/netmap/if_re_netmap.h (revision 64ae02c36579bad7d5e682589a0bc1023e359f9d)
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$
28*64ae02c3SLuigi Rizzo  * $Id: if_re_netmap.h 10609 2012-02-22 19:44:58Z luigi $
2968b8534bSLuigi Rizzo  *
30*64ae02c3SLuigi Rizzo  * netmap support for "re"
31*64ae02c3SLuigi Rizzo  * For details on netmap support please see ixgbe_netmap.h
3268b8534bSLuigi Rizzo  */
3368b8534bSLuigi Rizzo 
34*64ae02c3SLuigi 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 /*
4368b8534bSLuigi Rizzo  * wrapper to export locks to the generic code
4468b8534bSLuigi Rizzo  * We should not use the tx/rx locks
4568b8534bSLuigi Rizzo  */
4668b8534bSLuigi Rizzo static void
471a26580eSLuigi Rizzo re_netmap_lock_wrapper(struct ifnet *ifp, int what, u_int queueid)
4868b8534bSLuigi Rizzo {
491a26580eSLuigi Rizzo 	struct rl_softc *adapter = ifp->if_softc;
5068b8534bSLuigi Rizzo 
5168b8534bSLuigi Rizzo 	switch (what) {
5268b8534bSLuigi Rizzo 	case NETMAP_CORE_LOCK:
5368b8534bSLuigi Rizzo 		RL_LOCK(adapter);
5468b8534bSLuigi Rizzo 		break;
5568b8534bSLuigi Rizzo 	case NETMAP_CORE_UNLOCK:
5668b8534bSLuigi Rizzo 		RL_UNLOCK(adapter);
5768b8534bSLuigi Rizzo 		break;
5868b8534bSLuigi Rizzo 
5968b8534bSLuigi Rizzo 	case NETMAP_TX_LOCK:
6068b8534bSLuigi Rizzo 	case NETMAP_RX_LOCK:
6168b8534bSLuigi Rizzo 	case NETMAP_TX_UNLOCK:
6268b8534bSLuigi Rizzo 	case NETMAP_RX_UNLOCK:
6368b8534bSLuigi Rizzo 		D("invalid lock call %d, no tx/rx locks here", what);
6468b8534bSLuigi Rizzo 		break;
6568b8534bSLuigi Rizzo 	}
6668b8534bSLuigi Rizzo }
6768b8534bSLuigi Rizzo 
6868b8534bSLuigi Rizzo 
6968b8534bSLuigi Rizzo /*
7068b8534bSLuigi Rizzo  * support for netmap register/unregisted. We are already under core lock.
7168b8534bSLuigi Rizzo  * only called on the first register or the last unregister.
7268b8534bSLuigi Rizzo  */
7368b8534bSLuigi Rizzo static int
7468b8534bSLuigi Rizzo re_netmap_reg(struct ifnet *ifp, int onoff)
7568b8534bSLuigi Rizzo {
7668b8534bSLuigi Rizzo 	struct rl_softc *adapter = ifp->if_softc;
7768b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
7868b8534bSLuigi Rizzo 	int error = 0;
7968b8534bSLuigi Rizzo 
80506cc70cSLuigi Rizzo 	if (na == NULL)
8168b8534bSLuigi Rizzo 		return EINVAL;
8268b8534bSLuigi Rizzo 	/* Tell the stack that the interface is no longer active */
8368b8534bSLuigi Rizzo 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
8468b8534bSLuigi Rizzo 
8568b8534bSLuigi Rizzo 	re_stop(adapter);
8668b8534bSLuigi Rizzo 
8768b8534bSLuigi Rizzo 	if (onoff) {
8868b8534bSLuigi Rizzo 		ifp->if_capenable |= IFCAP_NETMAP;
8968b8534bSLuigi Rizzo 
90506cc70cSLuigi Rizzo 		/* save if_transmit to restore it later */
9168b8534bSLuigi Rizzo 		na->if_transmit = ifp->if_transmit;
9268b8534bSLuigi Rizzo 		ifp->if_transmit = netmap_start;
9368b8534bSLuigi Rizzo 
9468b8534bSLuigi Rizzo 		re_init_locked(adapter);
9568b8534bSLuigi Rizzo 
9668b8534bSLuigi Rizzo 		if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) == 0) {
9768b8534bSLuigi Rizzo 			error = ENOMEM;
9868b8534bSLuigi Rizzo 			goto fail;
9968b8534bSLuigi Rizzo 		}
10068b8534bSLuigi Rizzo 	} else {
10168b8534bSLuigi Rizzo fail:
10268b8534bSLuigi Rizzo 		/* restore if_transmit */
10368b8534bSLuigi Rizzo 		ifp->if_transmit = na->if_transmit;
10468b8534bSLuigi Rizzo 		ifp->if_capenable &= ~IFCAP_NETMAP;
10568b8534bSLuigi Rizzo 		re_init_locked(adapter);	/* also enables intr */
10668b8534bSLuigi Rizzo 	}
10768b8534bSLuigi Rizzo 	return (error);
10868b8534bSLuigi Rizzo }
10968b8534bSLuigi Rizzo 
11068b8534bSLuigi Rizzo 
11168b8534bSLuigi Rizzo /*
11268b8534bSLuigi Rizzo  * Reconcile kernel and user view of the transmit ring.
11368b8534bSLuigi Rizzo  */
11468b8534bSLuigi Rizzo static int
1151a26580eSLuigi Rizzo re_netmap_txsync(struct ifnet *ifp, u_int ring_nr, int do_lock)
11668b8534bSLuigi Rizzo {
1171a26580eSLuigi Rizzo 	struct rl_softc *sc = ifp->if_softc;
11868b8534bSLuigi Rizzo 	struct rl_txdesc *txd = sc->rl_ldata.rl_tx_desc;
11968b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(sc->rl_ifp);
12068b8534bSLuigi Rizzo 	struct netmap_kring *kring = &na->tx_rings[ring_nr];
12168b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
122506cc70cSLuigi Rizzo 	int j, k, l, n, lim = kring->nkr_num_slots - 1;
12368b8534bSLuigi Rizzo 
12468b8534bSLuigi Rizzo 	k = ring->cur;
125506cc70cSLuigi Rizzo 	if (k > lim)
12668b8534bSLuigi Rizzo 		return netmap_ring_reinit(kring);
12768b8534bSLuigi Rizzo 
12868b8534bSLuigi Rizzo 	if (do_lock)
12968b8534bSLuigi Rizzo 		RL_LOCK(sc);
13068b8534bSLuigi Rizzo 
13168b8534bSLuigi Rizzo 	/* Sync the TX descriptor list */
13268b8534bSLuigi Rizzo 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
13368b8534bSLuigi Rizzo             sc->rl_ldata.rl_tx_list_map,
13468b8534bSLuigi Rizzo             BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
13568b8534bSLuigi Rizzo 
136506cc70cSLuigi Rizzo 	/* XXX move after the transmissions */
13768b8534bSLuigi Rizzo 	/* record completed transmissions */
138506cc70cSLuigi Rizzo         for (n = 0, l = sc->rl_ldata.rl_tx_considx;
139506cc70cSLuigi Rizzo 	    l != sc->rl_ldata.rl_tx_prodidx;
140506cc70cSLuigi Rizzo 	    n++, l = RL_TX_DESC_NXT(sc, l)) {
14168b8534bSLuigi Rizzo 		uint32_t cmdstat =
142506cc70cSLuigi Rizzo 			le32toh(sc->rl_ldata.rl_tx_list[l].rl_cmdstat);
14368b8534bSLuigi Rizzo 		if (cmdstat & RL_TDESC_STAT_OWN)
14468b8534bSLuigi Rizzo 			break;
14568b8534bSLuigi Rizzo 	}
14668b8534bSLuigi Rizzo 	if (n > 0) {
147506cc70cSLuigi Rizzo 		sc->rl_ldata.rl_tx_considx = l;
14868b8534bSLuigi Rizzo 		sc->rl_ldata.rl_tx_free += n;
14968b8534bSLuigi Rizzo 		kring->nr_hwavail += n;
15068b8534bSLuigi Rizzo 	}
15168b8534bSLuigi Rizzo 
152*64ae02c3SLuigi Rizzo 	/* update avail to what the kernel knows */
15368b8534bSLuigi Rizzo 	ring->avail = kring->nr_hwavail;
15468b8534bSLuigi Rizzo 
155506cc70cSLuigi Rizzo 	j = kring->nr_hwcur;
15668b8534bSLuigi Rizzo 	if (j != k) {	/* we have new packets to send */
157506cc70cSLuigi Rizzo 		l = sc->rl_ldata.rl_tx_prodidx;
158babc7c12SLuigi Rizzo 		for (n = 0; j != k; n++) {
15968b8534bSLuigi Rizzo 			struct netmap_slot *slot = &ring->slot[j];
160506cc70cSLuigi Rizzo 			struct rl_desc *desc = &sc->rl_ldata.rl_tx_list[l];
16168b8534bSLuigi Rizzo 			int cmd = slot->len | RL_TDESC_CMD_EOF |
16268b8534bSLuigi Rizzo 				RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF ;
1636e10c8b8SLuigi Rizzo 			uint64_t paddr;
1646e10c8b8SLuigi Rizzo 			void *addr = PNMB(slot, &paddr);
16568b8534bSLuigi Rizzo 			int len = slot->len;
16668b8534bSLuigi Rizzo 
16768b8534bSLuigi Rizzo 			if (addr == netmap_buffer_base || len > NETMAP_BUF_SIZE) {
16868b8534bSLuigi Rizzo 				if (do_lock)
16968b8534bSLuigi Rizzo 					RL_UNLOCK(sc);
170506cc70cSLuigi Rizzo 				// XXX what about prodidx ?
17168b8534bSLuigi Rizzo 				return netmap_ring_reinit(kring);
17268b8534bSLuigi Rizzo 			}
17368b8534bSLuigi Rizzo 
174506cc70cSLuigi Rizzo 			if (l == lim)	/* mark end of ring */
17568b8534bSLuigi Rizzo 				cmd |= RL_TDESC_CMD_EOR;
17668b8534bSLuigi Rizzo 
17768b8534bSLuigi Rizzo 			if (slot->flags & NS_BUF_CHANGED) {
17868b8534bSLuigi Rizzo 				desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr));
17968b8534bSLuigi Rizzo 				desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr));
18068b8534bSLuigi Rizzo 				/* buffer has changed, unload and reload map */
18168b8534bSLuigi Rizzo 				netmap_reload_map(sc->rl_ldata.rl_tx_mtag,
1826e10c8b8SLuigi Rizzo 					txd[l].tx_dmamap, addr);
18368b8534bSLuigi Rizzo 				slot->flags &= ~NS_BUF_CHANGED;
18468b8534bSLuigi Rizzo 			}
18568b8534bSLuigi Rizzo 			slot->flags &= ~NS_REPORT;
18668b8534bSLuigi Rizzo 			desc->rl_cmdstat = htole32(cmd);
18768b8534bSLuigi Rizzo 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
188506cc70cSLuigi Rizzo 				txd[l].tx_dmamap, BUS_DMASYNC_PREWRITE);
18968b8534bSLuigi Rizzo 			j = (j == lim) ? 0 : j + 1;
190506cc70cSLuigi Rizzo 			l = (l == lim) ? 0 : l + 1;
19168b8534bSLuigi Rizzo 		}
192506cc70cSLuigi Rizzo 		sc->rl_ldata.rl_tx_prodidx = l;
193*64ae02c3SLuigi Rizzo 		kring->nr_hwcur = k; /* the saved ring->cur */
194*64ae02c3SLuigi Rizzo 		ring->avail -= n; // XXX see others
19568b8534bSLuigi Rizzo 		kring->nr_hwavail = ring->avail;
19668b8534bSLuigi Rizzo 
19768b8534bSLuigi Rizzo 		bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
19868b8534bSLuigi Rizzo 		    sc->rl_ldata.rl_tx_list_map,
19968b8534bSLuigi Rizzo 		    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
20068b8534bSLuigi Rizzo 
20168b8534bSLuigi Rizzo 		/* start ? */
20268b8534bSLuigi Rizzo 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
20368b8534bSLuigi Rizzo 	}
20468b8534bSLuigi Rizzo 	if (do_lock)
20568b8534bSLuigi Rizzo 		RL_UNLOCK(sc);
20668b8534bSLuigi Rizzo 	return 0;
20768b8534bSLuigi Rizzo }
20868b8534bSLuigi Rizzo 
20968b8534bSLuigi Rizzo 
21068b8534bSLuigi Rizzo /*
21168b8534bSLuigi Rizzo  * Reconcile kernel and user view of the receive ring.
21268b8534bSLuigi Rizzo  */
21368b8534bSLuigi Rizzo static int
2141a26580eSLuigi Rizzo re_netmap_rxsync(struct ifnet *ifp, u_int ring_nr, int do_lock)
21568b8534bSLuigi Rizzo {
2161a26580eSLuigi Rizzo 	struct rl_softc *sc = ifp->if_softc;
21768b8534bSLuigi Rizzo 	struct rl_rxdesc *rxd = sc->rl_ldata.rl_rx_desc;
21868b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(sc->rl_ifp);
21968b8534bSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[ring_nr];
22068b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
221*64ae02c3SLuigi Rizzo 	int j, l, n, lim = kring->nkr_num_slots - 1;
222*64ae02c3SLuigi Rizzo 	int force_update = do_lock || kring->nr_kflags & NKR_PENDINTR;
223*64ae02c3SLuigi Rizzo 	u_int k = ring->cur, resvd = ring->reserved;
22468b8534bSLuigi Rizzo 
22568b8534bSLuigi Rizzo 	k = ring->cur;
226506cc70cSLuigi Rizzo 	if (k > lim)
22768b8534bSLuigi Rizzo 		return netmap_ring_reinit(kring);
22868b8534bSLuigi Rizzo 
22968b8534bSLuigi Rizzo 	if (do_lock)
23068b8534bSLuigi Rizzo 		RL_LOCK(sc);
23168b8534bSLuigi Rizzo 	/* XXX check sync modes */
23268b8534bSLuigi Rizzo 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
23368b8534bSLuigi Rizzo 	    sc->rl_ldata.rl_rx_list_map,
23468b8534bSLuigi Rizzo 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
23568b8534bSLuigi Rizzo 
23668b8534bSLuigi Rizzo 	/*
237*64ae02c3SLuigi Rizzo 	 * Import newly received packets into the netmap ring.
238*64ae02c3SLuigi Rizzo 	 * j is an index in the netmap ring, l in the NIC ring.
239*64ae02c3SLuigi Rizzo 	 *
24068b8534bSLuigi Rizzo 	 * The device uses all the buffers in the ring, so we need
24168b8534bSLuigi Rizzo 	 * another termination condition in addition to RL_RDESC_STAT_OWN
24268b8534bSLuigi Rizzo 	 * cleared (all buffers could have it cleared. The easiest one
24368b8534bSLuigi Rizzo 	 * is to limit the amount of data reported up to 'lim'
24468b8534bSLuigi Rizzo 	 */
245506cc70cSLuigi Rizzo 	l = sc->rl_ldata.rl_rx_prodidx; /* next pkt to check */
246*64ae02c3SLuigi Rizzo 	j = netmap_idx_n2k(kring, l); /* the kring index */
247*64ae02c3SLuigi Rizzo 	if (netmap_no_pendintr || force_update) {
24868b8534bSLuigi Rizzo 		for (n = kring->nr_hwavail; n < lim ; n++) {
249506cc70cSLuigi Rizzo 			struct rl_desc *cur_rx = &sc->rl_ldata.rl_rx_list[l];
25068b8534bSLuigi Rizzo 			uint32_t rxstat = le32toh(cur_rx->rl_cmdstat);
25168b8534bSLuigi Rizzo 			uint32_t total_len;
25268b8534bSLuigi Rizzo 
25368b8534bSLuigi Rizzo 			if ((rxstat & RL_RDESC_STAT_OWN) != 0)
25468b8534bSLuigi Rizzo 				break;
25568b8534bSLuigi Rizzo 			total_len = rxstat & sc->rl_rxlenmask;
25668b8534bSLuigi Rizzo 			/* XXX subtract crc */
25768b8534bSLuigi Rizzo 			total_len = (total_len < 4) ? 0 : total_len - 4;
25868b8534bSLuigi Rizzo 			kring->ring->slot[j].len = total_len;
25968b8534bSLuigi Rizzo 			/*  sync was in re_newbuf() */
26068b8534bSLuigi Rizzo 			bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
261506cc70cSLuigi Rizzo 			    rxd[l].rx_dmamap, BUS_DMASYNC_POSTREAD);
262506cc70cSLuigi Rizzo 			j = (j == lim) ? 0 : j + 1;
263506cc70cSLuigi Rizzo 			l = (l == lim) ? 0 : l + 1;
26468b8534bSLuigi Rizzo 		}
26568b8534bSLuigi Rizzo 		if (n != kring->nr_hwavail) {
266506cc70cSLuigi Rizzo 			sc->rl_ldata.rl_rx_prodidx = l;
26768b8534bSLuigi Rizzo 			sc->rl_ifp->if_ipackets += n - kring->nr_hwavail;
26868b8534bSLuigi Rizzo 			kring->nr_hwavail = n;
26968b8534bSLuigi Rizzo 		}
270*64ae02c3SLuigi Rizzo 		kring->nr_kflags &= ~NKR_PENDINTR;
271*64ae02c3SLuigi Rizzo 	}
27268b8534bSLuigi Rizzo 
273*64ae02c3SLuigi Rizzo 	/* skip past packets that userspace has released */
27468b8534bSLuigi Rizzo 	j = kring->nr_hwcur;
275*64ae02c3SLuigi Rizzo 	if (resvd > 0) {
276*64ae02c3SLuigi Rizzo 		if (resvd + ring->avail >= lim + 1) {
277*64ae02c3SLuigi Rizzo 			D("XXX invalid reserve/avail %d %d", resvd, ring->avail);
278*64ae02c3SLuigi Rizzo 			ring->reserved = resvd = 0; // XXX panic...
279*64ae02c3SLuigi Rizzo 		}
280*64ae02c3SLuigi Rizzo 		k = (k >= resvd) ? k - resvd : k + lim + 1 - resvd;
281*64ae02c3SLuigi Rizzo 	}
282*64ae02c3SLuigi Rizzo 	if (j != k) { /* userspace has released some packets. */
283*64ae02c3SLuigi Rizzo 		l = netmap_idx_k2n(kring, j); /* the NIC index */
284babc7c12SLuigi Rizzo 		for (n = 0; j != k; n++) {
28568b8534bSLuigi Rizzo 			struct netmap_slot *slot = ring->slot + j;
286506cc70cSLuigi Rizzo 			struct rl_desc *desc = &sc->rl_ldata.rl_rx_list[l];
28768b8534bSLuigi Rizzo 			int cmd = na->buff_size | RL_RDESC_CMD_OWN;
2886e10c8b8SLuigi Rizzo 			uint64_t paddr;
2896e10c8b8SLuigi Rizzo 			void *addr = PNMB(slot, &paddr);
29068b8534bSLuigi Rizzo 
29168b8534bSLuigi Rizzo 			if (addr == netmap_buffer_base) { /* bad buf */
29268b8534bSLuigi Rizzo 				if (do_lock)
29368b8534bSLuigi Rizzo 					RL_UNLOCK(sc);
29468b8534bSLuigi Rizzo 				return netmap_ring_reinit(kring);
29568b8534bSLuigi Rizzo 			}
29668b8534bSLuigi Rizzo 
297506cc70cSLuigi Rizzo 			if (l == lim)	/* mark end of ring */
29868b8534bSLuigi Rizzo 				cmd |= RL_RDESC_CMD_EOR;
29968b8534bSLuigi Rizzo 
30068b8534bSLuigi Rizzo 			slot->flags &= ~NS_REPORT;
30168b8534bSLuigi Rizzo 			if (slot->flags & NS_BUF_CHANGED) {
30268b8534bSLuigi Rizzo 				netmap_reload_map(sc->rl_ldata.rl_rx_mtag,
3036e10c8b8SLuigi Rizzo 					rxd[l].rx_dmamap, addr);
304*64ae02c3SLuigi Rizzo 				desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr));
305*64ae02c3SLuigi Rizzo 				desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr));
30668b8534bSLuigi Rizzo 				slot->flags &= ~NS_BUF_CHANGED;
30768b8534bSLuigi Rizzo 			}
308*64ae02c3SLuigi Rizzo 			desc->rl_cmdstat = htole32(cmd);
30968b8534bSLuigi Rizzo 			bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
310506cc70cSLuigi Rizzo 				rxd[l].rx_dmamap, BUS_DMASYNC_PREREAD);
31168b8534bSLuigi Rizzo 			j = (j == lim) ? 0 : j + 1;
312506cc70cSLuigi Rizzo 			l = (l == lim) ? 0 : l + 1;
31368b8534bSLuigi Rizzo 		}
31468b8534bSLuigi Rizzo 		kring->nr_hwavail -= n;
31568b8534bSLuigi Rizzo 		kring->nr_hwcur = k;
31668b8534bSLuigi Rizzo 		/* Flush the RX DMA ring */
31768b8534bSLuigi Rizzo 
31868b8534bSLuigi Rizzo 		bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
31968b8534bSLuigi Rizzo 		    sc->rl_ldata.rl_rx_list_map,
32068b8534bSLuigi Rizzo 		    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
32168b8534bSLuigi Rizzo 	}
32268b8534bSLuigi Rizzo 	/* tell userspace that there are new packets */
323*64ae02c3SLuigi Rizzo 	ring->avail = kring->nr_hwavail - resvd;
32468b8534bSLuigi Rizzo 	if (do_lock)
32568b8534bSLuigi Rizzo 		RL_UNLOCK(sc);
32668b8534bSLuigi Rizzo 	return 0;
32768b8534bSLuigi Rizzo }
32868b8534bSLuigi Rizzo 
329506cc70cSLuigi Rizzo /*
330506cc70cSLuigi Rizzo  * Additional routines to init the tx and rx rings.
331506cc70cSLuigi Rizzo  * In other drivers we do that inline in the main code.
332506cc70cSLuigi Rizzo  */
33368b8534bSLuigi Rizzo static void
33468b8534bSLuigi Rizzo re_netmap_tx_init(struct rl_softc *sc)
33568b8534bSLuigi Rizzo {
33668b8534bSLuigi Rizzo 	struct rl_txdesc *txd;
33768b8534bSLuigi Rizzo 	struct rl_desc *desc;
338506cc70cSLuigi Rizzo 	int i, n;
33968b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(sc->rl_ifp);
34068b8534bSLuigi Rizzo 	struct netmap_slot *slot = netmap_reset(na, NR_TX, 0, 0);
34168b8534bSLuigi Rizzo 
34268b8534bSLuigi Rizzo 	/* slot is NULL if we are not in netmap mode */
34368b8534bSLuigi Rizzo 	if (!slot)
34468b8534bSLuigi Rizzo 		return;
34568b8534bSLuigi Rizzo 	/* in netmap mode, overwrite addresses and maps */
34668b8534bSLuigi Rizzo 	txd = sc->rl_ldata.rl_tx_desc;
34768b8534bSLuigi Rizzo 	desc = sc->rl_ldata.rl_tx_list;
348506cc70cSLuigi Rizzo 	n = sc->rl_ldata.rl_tx_desc_cnt;
34968b8534bSLuigi Rizzo 
350506cc70cSLuigi Rizzo 	/* l points in the netmap ring, i points in the NIC ring */
351506cc70cSLuigi Rizzo 	for (i = 0; i < n; i++) {
352506cc70cSLuigi Rizzo 		uint64_t paddr;
353*64ae02c3SLuigi Rizzo 		int l = netmap_idx_n2k(&na->tx_rings[0], i);
354babc7c12SLuigi Rizzo 		void *addr = PNMB(slot + l, &paddr);
355506cc70cSLuigi Rizzo 
35668b8534bSLuigi Rizzo 		desc[i].rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr));
35768b8534bSLuigi Rizzo 		desc[i].rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr));
35868b8534bSLuigi Rizzo 		netmap_load_map(sc->rl_ldata.rl_tx_mtag,
3596e10c8b8SLuigi Rizzo 			txd[i].tx_dmamap, addr);
36068b8534bSLuigi Rizzo 	}
36168b8534bSLuigi Rizzo }
36268b8534bSLuigi Rizzo 
36368b8534bSLuigi Rizzo static void
36468b8534bSLuigi Rizzo re_netmap_rx_init(struct rl_softc *sc)
36568b8534bSLuigi Rizzo {
36668b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(sc->rl_ifp);
36768b8534bSLuigi Rizzo 	struct netmap_slot *slot = netmap_reset(na, NR_RX, 0, 0);
36868b8534bSLuigi Rizzo 	struct rl_desc *desc = sc->rl_ldata.rl_rx_list;
36968b8534bSLuigi Rizzo 	uint32_t cmdstat;
3705644ccecSLuigi Rizzo 	int i, n, max_avail;
37168b8534bSLuigi Rizzo 
37268b8534bSLuigi Rizzo 	if (!slot)
37368b8534bSLuigi Rizzo 		return;
374506cc70cSLuigi Rizzo 	n = sc->rl_ldata.rl_rx_desc_cnt;
3755644ccecSLuigi Rizzo 	/*
3765644ccecSLuigi Rizzo 	 * Userspace owned hwavail packets before the reset,
3775644ccecSLuigi Rizzo 	 * so the NIC that last hwavail descriptors of the ring
3785644ccecSLuigi Rizzo 	 * are still owned by the driver (and keep one empty).
3795644ccecSLuigi Rizzo 	 */
3805644ccecSLuigi Rizzo 	max_avail = n - 1 - na->rx_rings[0].nr_hwavail;
381506cc70cSLuigi Rizzo 	for (i = 0; i < n; i++) {
382506cc70cSLuigi Rizzo 		void *addr;
383506cc70cSLuigi Rizzo 		uint64_t paddr;
384*64ae02c3SLuigi Rizzo 		int l = netmap_idx_n2k(&na->rx_rings[0], i);
38568b8534bSLuigi Rizzo 
3866e10c8b8SLuigi Rizzo 		addr = PNMB(slot + l, &paddr);
3876e10c8b8SLuigi Rizzo 
3886e10c8b8SLuigi Rizzo 		netmap_reload_map(sc->rl_ldata.rl_rx_mtag,
3896e10c8b8SLuigi Rizzo 		    sc->rl_ldata.rl_rx_desc[i].rx_dmamap, addr);
3906e10c8b8SLuigi Rizzo 		bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
3916e10c8b8SLuigi Rizzo 		    sc->rl_ldata.rl_rx_desc[i].rx_dmamap, BUS_DMASYNC_PREREAD);
39268b8534bSLuigi Rizzo 		desc[i].rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr));
39368b8534bSLuigi Rizzo 		desc[i].rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr));
394506cc70cSLuigi Rizzo 		cmdstat = na->buff_size;
3955644ccecSLuigi Rizzo 		if (i == n - 1) /* mark the end of ring */
39668b8534bSLuigi Rizzo 			cmdstat |= RL_RDESC_CMD_EOR;
3975644ccecSLuigi Rizzo 		if (i < max_avail)
398506cc70cSLuigi Rizzo 			cmdstat |= RL_RDESC_CMD_OWN;
399506cc70cSLuigi Rizzo 		desc[i].rl_cmdstat = htole32(cmdstat);
40068b8534bSLuigi Rizzo 	}
40168b8534bSLuigi Rizzo }
402*64ae02c3SLuigi Rizzo 
403*64ae02c3SLuigi Rizzo 
404*64ae02c3SLuigi Rizzo static void
405*64ae02c3SLuigi Rizzo re_netmap_attach(struct rl_softc *sc)
406*64ae02c3SLuigi Rizzo {
407*64ae02c3SLuigi Rizzo 	struct netmap_adapter na;
408*64ae02c3SLuigi Rizzo 
409*64ae02c3SLuigi Rizzo 	bzero(&na, sizeof(na));
410*64ae02c3SLuigi Rizzo 
411*64ae02c3SLuigi Rizzo 	na.ifp = sc->rl_ifp;
412*64ae02c3SLuigi Rizzo 	na.separate_locks = 0;
413*64ae02c3SLuigi Rizzo 	na.num_tx_desc = sc->rl_ldata.rl_tx_desc_cnt;
414*64ae02c3SLuigi Rizzo 	na.num_rx_desc = sc->rl_ldata.rl_rx_desc_cnt;
415*64ae02c3SLuigi Rizzo 	na.nm_txsync = re_netmap_txsync;
416*64ae02c3SLuigi Rizzo 	na.nm_rxsync = re_netmap_rxsync;
417*64ae02c3SLuigi Rizzo 	na.nm_lock = re_netmap_lock_wrapper;
418*64ae02c3SLuigi Rizzo 	na.nm_register = re_netmap_reg;
419*64ae02c3SLuigi Rizzo 	netmap_attach(&na, 1);
420*64ae02c3SLuigi Rizzo }
421*64ae02c3SLuigi Rizzo /* end of file */
422