xref: /freebsd/sys/dev/netmap/if_re_netmap.h (revision ce3ee1e7c4cac5b86bbc15daac68f2129aa42187)
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$
2868b8534bSLuigi Rizzo  *
2964ae02c3SLuigi Rizzo  * netmap support for "re"
3064ae02c3SLuigi Rizzo  * For details on netmap support please see ixgbe_netmap.h
3168b8534bSLuigi Rizzo  */
3268b8534bSLuigi Rizzo 
3364ae02c3SLuigi Rizzo 
3468b8534bSLuigi Rizzo #include <net/netmap.h>
3568b8534bSLuigi Rizzo #include <sys/selinfo.h>
3668b8534bSLuigi Rizzo #include <vm/vm.h>
3768b8534bSLuigi Rizzo #include <vm/pmap.h>    /* vtophys ? */
3868b8534bSLuigi Rizzo #include <dev/netmap/netmap_kern.h>
3968b8534bSLuigi Rizzo 
4068b8534bSLuigi Rizzo 
4168b8534bSLuigi Rizzo /*
4268b8534bSLuigi Rizzo  * support for netmap register/unregisted. We are already under core lock.
4368b8534bSLuigi Rizzo  * only called on the first register or the last unregister.
4468b8534bSLuigi Rizzo  */
4568b8534bSLuigi Rizzo static int
4668b8534bSLuigi Rizzo re_netmap_reg(struct ifnet *ifp, int onoff)
4768b8534bSLuigi Rizzo {
4868b8534bSLuigi Rizzo 	struct rl_softc *adapter = ifp->if_softc;
4968b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(ifp);
5068b8534bSLuigi Rizzo 	int error = 0;
5168b8534bSLuigi Rizzo 
52506cc70cSLuigi Rizzo 	if (na == NULL)
5368b8534bSLuigi Rizzo 		return EINVAL;
5468b8534bSLuigi Rizzo 	/* Tell the stack that the interface is no longer active */
5568b8534bSLuigi Rizzo 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
5668b8534bSLuigi Rizzo 
5768b8534bSLuigi Rizzo 	re_stop(adapter);
5868b8534bSLuigi Rizzo 
5968b8534bSLuigi Rizzo 	if (onoff) {
6068b8534bSLuigi Rizzo 		ifp->if_capenable |= IFCAP_NETMAP;
6168b8534bSLuigi Rizzo 
62506cc70cSLuigi Rizzo 		/* save if_transmit to restore it later */
6368b8534bSLuigi Rizzo 		na->if_transmit = ifp->if_transmit;
64*ce3ee1e7SLuigi Rizzo 		ifp->if_transmit = netmap_transmit;
6568b8534bSLuigi Rizzo 
6668b8534bSLuigi Rizzo 		re_init_locked(adapter);
6768b8534bSLuigi Rizzo 
6868b8534bSLuigi Rizzo 		if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) == 0) {
6968b8534bSLuigi Rizzo 			error = ENOMEM;
7068b8534bSLuigi Rizzo 			goto fail;
7168b8534bSLuigi Rizzo 		}
7268b8534bSLuigi Rizzo 	} else {
7368b8534bSLuigi Rizzo fail:
7468b8534bSLuigi Rizzo 		/* restore if_transmit */
7568b8534bSLuigi Rizzo 		ifp->if_transmit = na->if_transmit;
7668b8534bSLuigi Rizzo 		ifp->if_capenable &= ~IFCAP_NETMAP;
7768b8534bSLuigi Rizzo 		re_init_locked(adapter);	/* also enables intr */
7868b8534bSLuigi Rizzo 	}
7968b8534bSLuigi Rizzo 	return (error);
8068b8534bSLuigi Rizzo }
8168b8534bSLuigi Rizzo 
8268b8534bSLuigi Rizzo 
8368b8534bSLuigi Rizzo /*
8468b8534bSLuigi Rizzo  * Reconcile kernel and user view of the transmit ring.
8568b8534bSLuigi Rizzo  */
8668b8534bSLuigi Rizzo static int
87*ce3ee1e7SLuigi Rizzo re_netmap_txsync(struct ifnet *ifp, u_int ring_nr, int flags)
8868b8534bSLuigi Rizzo {
891a26580eSLuigi Rizzo 	struct rl_softc *sc = ifp->if_softc;
9068b8534bSLuigi Rizzo 	struct rl_txdesc *txd = sc->rl_ldata.rl_tx_desc;
9168b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(sc->rl_ifp);
9268b8534bSLuigi Rizzo 	struct netmap_kring *kring = &na->tx_rings[ring_nr];
9368b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
94506cc70cSLuigi Rizzo 	int j, k, l, n, lim = kring->nkr_num_slots - 1;
9568b8534bSLuigi Rizzo 
9668b8534bSLuigi Rizzo 	k = ring->cur;
97506cc70cSLuigi Rizzo 	if (k > lim)
9868b8534bSLuigi Rizzo 		return netmap_ring_reinit(kring);
9968b8534bSLuigi Rizzo 
10068b8534bSLuigi Rizzo 	/* Sync the TX descriptor list */
10168b8534bSLuigi Rizzo 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
10268b8534bSLuigi Rizzo             sc->rl_ldata.rl_tx_list_map,
10368b8534bSLuigi Rizzo             BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
10468b8534bSLuigi Rizzo 
105506cc70cSLuigi Rizzo 	/* XXX move after the transmissions */
10668b8534bSLuigi Rizzo 	/* record completed transmissions */
107506cc70cSLuigi Rizzo         for (n = 0, l = sc->rl_ldata.rl_tx_considx;
108506cc70cSLuigi Rizzo 	    l != sc->rl_ldata.rl_tx_prodidx;
109506cc70cSLuigi Rizzo 	    n++, l = RL_TX_DESC_NXT(sc, l)) {
11068b8534bSLuigi Rizzo 		uint32_t cmdstat =
111506cc70cSLuigi Rizzo 			le32toh(sc->rl_ldata.rl_tx_list[l].rl_cmdstat);
11268b8534bSLuigi Rizzo 		if (cmdstat & RL_TDESC_STAT_OWN)
11368b8534bSLuigi Rizzo 			break;
11468b8534bSLuigi Rizzo 	}
11568b8534bSLuigi Rizzo 	if (n > 0) {
116506cc70cSLuigi Rizzo 		sc->rl_ldata.rl_tx_considx = l;
11768b8534bSLuigi Rizzo 		sc->rl_ldata.rl_tx_free += n;
11868b8534bSLuigi Rizzo 		kring->nr_hwavail += n;
11968b8534bSLuigi Rizzo 	}
12068b8534bSLuigi Rizzo 
12164ae02c3SLuigi Rizzo 	/* update avail to what the kernel knows */
12268b8534bSLuigi Rizzo 	ring->avail = kring->nr_hwavail;
12368b8534bSLuigi Rizzo 
124506cc70cSLuigi Rizzo 	j = kring->nr_hwcur;
12568b8534bSLuigi Rizzo 	if (j != k) {	/* we have new packets to send */
126506cc70cSLuigi Rizzo 		l = sc->rl_ldata.rl_tx_prodidx;
127babc7c12SLuigi Rizzo 		for (n = 0; j != k; n++) {
12868b8534bSLuigi Rizzo 			struct netmap_slot *slot = &ring->slot[j];
129506cc70cSLuigi Rizzo 			struct rl_desc *desc = &sc->rl_ldata.rl_tx_list[l];
13068b8534bSLuigi Rizzo 			int cmd = slot->len | RL_TDESC_CMD_EOF |
13168b8534bSLuigi Rizzo 				RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF ;
1326e10c8b8SLuigi Rizzo 			uint64_t paddr;
1336e10c8b8SLuigi Rizzo 			void *addr = PNMB(slot, &paddr);
13468b8534bSLuigi Rizzo 			int len = slot->len;
13568b8534bSLuigi Rizzo 
13668b8534bSLuigi Rizzo 			if (addr == netmap_buffer_base || len > NETMAP_BUF_SIZE) {
137506cc70cSLuigi Rizzo 				// XXX what about prodidx ?
13868b8534bSLuigi Rizzo 				return netmap_ring_reinit(kring);
13968b8534bSLuigi Rizzo 			}
14068b8534bSLuigi Rizzo 
141506cc70cSLuigi Rizzo 			if (l == lim)	/* mark end of ring */
14268b8534bSLuigi Rizzo 				cmd |= RL_TDESC_CMD_EOR;
14368b8534bSLuigi Rizzo 
14468b8534bSLuigi Rizzo 			if (slot->flags & NS_BUF_CHANGED) {
14568b8534bSLuigi Rizzo 				desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr));
14668b8534bSLuigi Rizzo 				desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr));
14768b8534bSLuigi Rizzo 				/* buffer has changed, unload and reload map */
14868b8534bSLuigi Rizzo 				netmap_reload_map(sc->rl_ldata.rl_tx_mtag,
1496e10c8b8SLuigi Rizzo 					txd[l].tx_dmamap, addr);
15068b8534bSLuigi Rizzo 				slot->flags &= ~NS_BUF_CHANGED;
15168b8534bSLuigi Rizzo 			}
15268b8534bSLuigi Rizzo 			slot->flags &= ~NS_REPORT;
15368b8534bSLuigi Rizzo 			desc->rl_cmdstat = htole32(cmd);
15468b8534bSLuigi Rizzo 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
155506cc70cSLuigi Rizzo 				txd[l].tx_dmamap, BUS_DMASYNC_PREWRITE);
15668b8534bSLuigi Rizzo 			j = (j == lim) ? 0 : j + 1;
157506cc70cSLuigi Rizzo 			l = (l == lim) ? 0 : l + 1;
15868b8534bSLuigi Rizzo 		}
159506cc70cSLuigi Rizzo 		sc->rl_ldata.rl_tx_prodidx = l;
16064ae02c3SLuigi Rizzo 		kring->nr_hwcur = k; /* the saved ring->cur */
16164ae02c3SLuigi Rizzo 		ring->avail -= n; // XXX see others
16268b8534bSLuigi Rizzo 		kring->nr_hwavail = ring->avail;
16368b8534bSLuigi Rizzo 
16468b8534bSLuigi Rizzo 		bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
16568b8534bSLuigi Rizzo 		    sc->rl_ldata.rl_tx_list_map,
16668b8534bSLuigi Rizzo 		    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
16768b8534bSLuigi Rizzo 
16868b8534bSLuigi Rizzo 		/* start ? */
16968b8534bSLuigi Rizzo 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
17068b8534bSLuigi Rizzo 	}
17168b8534bSLuigi Rizzo 	return 0;
17268b8534bSLuigi Rizzo }
17368b8534bSLuigi Rizzo 
17468b8534bSLuigi Rizzo 
17568b8534bSLuigi Rizzo /*
17668b8534bSLuigi Rizzo  * Reconcile kernel and user view of the receive ring.
17768b8534bSLuigi Rizzo  */
17868b8534bSLuigi Rizzo static int
179*ce3ee1e7SLuigi Rizzo re_netmap_rxsync(struct ifnet *ifp, u_int ring_nr, int flags)
18068b8534bSLuigi Rizzo {
1811a26580eSLuigi Rizzo 	struct rl_softc *sc = ifp->if_softc;
18268b8534bSLuigi Rizzo 	struct rl_rxdesc *rxd = sc->rl_ldata.rl_rx_desc;
18368b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(sc->rl_ifp);
18468b8534bSLuigi Rizzo 	struct netmap_kring *kring = &na->rx_rings[ring_nr];
18568b8534bSLuigi Rizzo 	struct netmap_ring *ring = kring->ring;
18664ae02c3SLuigi Rizzo 	int j, l, n, lim = kring->nkr_num_slots - 1;
187*ce3ee1e7SLuigi Rizzo 	int force_update = (flags & NAF_FORCE_READ) || kring->nr_kflags & NKR_PENDINTR;
18864ae02c3SLuigi Rizzo 	u_int k = ring->cur, resvd = ring->reserved;
18968b8534bSLuigi Rizzo 
19068b8534bSLuigi Rizzo 	k = ring->cur;
191506cc70cSLuigi Rizzo 	if (k > lim)
19268b8534bSLuigi Rizzo 		return netmap_ring_reinit(kring);
19368b8534bSLuigi Rizzo 
19468b8534bSLuigi Rizzo 	/* XXX check sync modes */
19568b8534bSLuigi Rizzo 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
19668b8534bSLuigi Rizzo 	    sc->rl_ldata.rl_rx_list_map,
19768b8534bSLuigi Rizzo 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
19868b8534bSLuigi Rizzo 
19968b8534bSLuigi Rizzo 	/*
20064ae02c3SLuigi Rizzo 	 * Import newly received packets into the netmap ring.
20164ae02c3SLuigi Rizzo 	 * j is an index in the netmap ring, l in the NIC ring.
20264ae02c3SLuigi Rizzo 	 *
20368b8534bSLuigi Rizzo 	 * The device uses all the buffers in the ring, so we need
20468b8534bSLuigi Rizzo 	 * another termination condition in addition to RL_RDESC_STAT_OWN
20568b8534bSLuigi Rizzo 	 * cleared (all buffers could have it cleared. The easiest one
20668b8534bSLuigi Rizzo 	 * is to limit the amount of data reported up to 'lim'
20768b8534bSLuigi Rizzo 	 */
208506cc70cSLuigi Rizzo 	l = sc->rl_ldata.rl_rx_prodidx; /* next pkt to check */
20964ae02c3SLuigi Rizzo 	j = netmap_idx_n2k(kring, l); /* the kring index */
21064ae02c3SLuigi Rizzo 	if (netmap_no_pendintr || force_update) {
2111dce924dSLuigi Rizzo 		uint16_t slot_flags = kring->nkr_slot_flags;
2121dce924dSLuigi Rizzo 
21368b8534bSLuigi Rizzo 		for (n = kring->nr_hwavail; n < lim ; n++) {
214506cc70cSLuigi Rizzo 			struct rl_desc *cur_rx = &sc->rl_ldata.rl_rx_list[l];
21568b8534bSLuigi Rizzo 			uint32_t rxstat = le32toh(cur_rx->rl_cmdstat);
21668b8534bSLuigi Rizzo 			uint32_t total_len;
21768b8534bSLuigi Rizzo 
21868b8534bSLuigi Rizzo 			if ((rxstat & RL_RDESC_STAT_OWN) != 0)
21968b8534bSLuigi Rizzo 				break;
22068b8534bSLuigi Rizzo 			total_len = rxstat & sc->rl_rxlenmask;
22168b8534bSLuigi Rizzo 			/* XXX subtract crc */
22268b8534bSLuigi Rizzo 			total_len = (total_len < 4) ? 0 : total_len - 4;
22368b8534bSLuigi Rizzo 			kring->ring->slot[j].len = total_len;
2241dce924dSLuigi Rizzo 			kring->ring->slot[j].flags = slot_flags;
22568b8534bSLuigi Rizzo 			/*  sync was in re_newbuf() */
22668b8534bSLuigi Rizzo 			bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
227506cc70cSLuigi Rizzo 			    rxd[l].rx_dmamap, BUS_DMASYNC_POSTREAD);
228506cc70cSLuigi Rizzo 			j = (j == lim) ? 0 : j + 1;
229506cc70cSLuigi Rizzo 			l = (l == lim) ? 0 : l + 1;
23068b8534bSLuigi Rizzo 		}
23168b8534bSLuigi Rizzo 		if (n != kring->nr_hwavail) {
232506cc70cSLuigi Rizzo 			sc->rl_ldata.rl_rx_prodidx = l;
23368b8534bSLuigi Rizzo 			sc->rl_ifp->if_ipackets += n - kring->nr_hwavail;
23468b8534bSLuigi Rizzo 			kring->nr_hwavail = n;
23568b8534bSLuigi Rizzo 		}
23664ae02c3SLuigi Rizzo 		kring->nr_kflags &= ~NKR_PENDINTR;
23764ae02c3SLuigi Rizzo 	}
23868b8534bSLuigi Rizzo 
23964ae02c3SLuigi Rizzo 	/* skip past packets that userspace has released */
24068b8534bSLuigi Rizzo 	j = kring->nr_hwcur;
24164ae02c3SLuigi Rizzo 	if (resvd > 0) {
24264ae02c3SLuigi Rizzo 		if (resvd + ring->avail >= lim + 1) {
24364ae02c3SLuigi Rizzo 			D("XXX invalid reserve/avail %d %d", resvd, ring->avail);
24464ae02c3SLuigi Rizzo 			ring->reserved = resvd = 0; // XXX panic...
24564ae02c3SLuigi Rizzo 		}
24664ae02c3SLuigi Rizzo 		k = (k >= resvd) ? k - resvd : k + lim + 1 - resvd;
24764ae02c3SLuigi Rizzo 	}
24864ae02c3SLuigi Rizzo 	if (j != k) { /* userspace has released some packets. */
24964ae02c3SLuigi Rizzo 		l = netmap_idx_k2n(kring, j); /* the NIC index */
250babc7c12SLuigi Rizzo 		for (n = 0; j != k; n++) {
25168b8534bSLuigi Rizzo 			struct netmap_slot *slot = ring->slot + j;
252506cc70cSLuigi Rizzo 			struct rl_desc *desc = &sc->rl_ldata.rl_rx_list[l];
25382d2fe10SLuigi Rizzo 			int cmd = NETMAP_BUF_SIZE | RL_RDESC_CMD_OWN;
2546e10c8b8SLuigi Rizzo 			uint64_t paddr;
2556e10c8b8SLuigi Rizzo 			void *addr = PNMB(slot, &paddr);
25668b8534bSLuigi Rizzo 
25768b8534bSLuigi Rizzo 			if (addr == netmap_buffer_base) { /* bad buf */
25868b8534bSLuigi Rizzo 				return netmap_ring_reinit(kring);
25968b8534bSLuigi Rizzo 			}
26068b8534bSLuigi Rizzo 
261506cc70cSLuigi Rizzo 			if (l == lim)	/* mark end of ring */
26268b8534bSLuigi Rizzo 				cmd |= RL_RDESC_CMD_EOR;
26368b8534bSLuigi Rizzo 
26468b8534bSLuigi Rizzo 			slot->flags &= ~NS_REPORT;
26568b8534bSLuigi Rizzo 			if (slot->flags & NS_BUF_CHANGED) {
26668b8534bSLuigi Rizzo 				netmap_reload_map(sc->rl_ldata.rl_rx_mtag,
2676e10c8b8SLuigi Rizzo 					rxd[l].rx_dmamap, addr);
26864ae02c3SLuigi Rizzo 				desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr));
26964ae02c3SLuigi Rizzo 				desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr));
27068b8534bSLuigi Rizzo 				slot->flags &= ~NS_BUF_CHANGED;
27168b8534bSLuigi Rizzo 			}
27264ae02c3SLuigi Rizzo 			desc->rl_cmdstat = htole32(cmd);
27368b8534bSLuigi Rizzo 			bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
274506cc70cSLuigi Rizzo 				rxd[l].rx_dmamap, BUS_DMASYNC_PREREAD);
27568b8534bSLuigi Rizzo 			j = (j == lim) ? 0 : j + 1;
276506cc70cSLuigi Rizzo 			l = (l == lim) ? 0 : l + 1;
27768b8534bSLuigi Rizzo 		}
27868b8534bSLuigi Rizzo 		kring->nr_hwavail -= n;
27968b8534bSLuigi Rizzo 		kring->nr_hwcur = k;
28068b8534bSLuigi Rizzo 		/* Flush the RX DMA ring */
28168b8534bSLuigi Rizzo 
28268b8534bSLuigi Rizzo 		bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
28368b8534bSLuigi Rizzo 		    sc->rl_ldata.rl_rx_list_map,
28468b8534bSLuigi Rizzo 		    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
28568b8534bSLuigi Rizzo 	}
28668b8534bSLuigi Rizzo 	/* tell userspace that there are new packets */
28764ae02c3SLuigi Rizzo 	ring->avail = kring->nr_hwavail - resvd;
28868b8534bSLuigi Rizzo 	return 0;
28968b8534bSLuigi Rizzo }
29068b8534bSLuigi Rizzo 
291506cc70cSLuigi Rizzo /*
292506cc70cSLuigi Rizzo  * Additional routines to init the tx and rx rings.
293506cc70cSLuigi Rizzo  * In other drivers we do that inline in the main code.
294506cc70cSLuigi Rizzo  */
29568b8534bSLuigi Rizzo static void
29668b8534bSLuigi Rizzo re_netmap_tx_init(struct rl_softc *sc)
29768b8534bSLuigi Rizzo {
29868b8534bSLuigi Rizzo 	struct rl_txdesc *txd;
29968b8534bSLuigi Rizzo 	struct rl_desc *desc;
300506cc70cSLuigi Rizzo 	int i, n;
30168b8534bSLuigi Rizzo 	struct netmap_adapter *na = NA(sc->rl_ifp);
30268b8534bSLuigi Rizzo 	struct netmap_slot *slot = netmap_reset(na, NR_TX, 0, 0);
30368b8534bSLuigi Rizzo 
30468b8534bSLuigi Rizzo 	/* slot is NULL if we are not in netmap mode */
30568b8534bSLuigi Rizzo 	if (!slot)
30668b8534bSLuigi Rizzo 		return;
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;
3325644ccecSLuigi Rizzo 	int i, n, max_avail;
33368b8534bSLuigi Rizzo 
33468b8534bSLuigi Rizzo 	if (!slot)
33568b8534bSLuigi Rizzo 		return;
336506cc70cSLuigi Rizzo 	n = sc->rl_ldata.rl_rx_desc_cnt;
3375644ccecSLuigi Rizzo 	/*
3385644ccecSLuigi Rizzo 	 * Userspace owned hwavail packets before the reset,
3395644ccecSLuigi Rizzo 	 * so the NIC that last hwavail descriptors of the ring
3405644ccecSLuigi Rizzo 	 * are still owned by the driver (and keep one empty).
3415644ccecSLuigi Rizzo 	 */
3425644ccecSLuigi Rizzo 	max_avail = n - 1 - na->rx_rings[0].nr_hwavail;
343506cc70cSLuigi Rizzo 	for (i = 0; i < n; i++) {
344506cc70cSLuigi Rizzo 		void *addr;
345506cc70cSLuigi Rizzo 		uint64_t paddr;
34664ae02c3SLuigi Rizzo 		int l = netmap_idx_n2k(&na->rx_rings[0], i);
34768b8534bSLuigi Rizzo 
3486e10c8b8SLuigi Rizzo 		addr = PNMB(slot + l, &paddr);
3496e10c8b8SLuigi Rizzo 
3506e10c8b8SLuigi Rizzo 		netmap_reload_map(sc->rl_ldata.rl_rx_mtag,
3516e10c8b8SLuigi Rizzo 		    sc->rl_ldata.rl_rx_desc[i].rx_dmamap, addr);
3526e10c8b8SLuigi Rizzo 		bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
3536e10c8b8SLuigi Rizzo 		    sc->rl_ldata.rl_rx_desc[i].rx_dmamap, BUS_DMASYNC_PREREAD);
35468b8534bSLuigi Rizzo 		desc[i].rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr));
35568b8534bSLuigi Rizzo 		desc[i].rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr));
35682d2fe10SLuigi Rizzo 		cmdstat = NETMAP_BUF_SIZE;
3575644ccecSLuigi Rizzo 		if (i == n - 1) /* mark the end of ring */
35868b8534bSLuigi Rizzo 			cmdstat |= RL_RDESC_CMD_EOR;
3595644ccecSLuigi Rizzo 		if (i < max_avail)
360506cc70cSLuigi Rizzo 			cmdstat |= RL_RDESC_CMD_OWN;
361506cc70cSLuigi Rizzo 		desc[i].rl_cmdstat = htole32(cmdstat);
36268b8534bSLuigi Rizzo 	}
36368b8534bSLuigi Rizzo }
36464ae02c3SLuigi Rizzo 
36564ae02c3SLuigi Rizzo 
36664ae02c3SLuigi Rizzo static void
36764ae02c3SLuigi Rizzo re_netmap_attach(struct rl_softc *sc)
36864ae02c3SLuigi Rizzo {
36964ae02c3SLuigi Rizzo 	struct netmap_adapter na;
37064ae02c3SLuigi Rizzo 
37164ae02c3SLuigi Rizzo 	bzero(&na, sizeof(na));
37264ae02c3SLuigi Rizzo 
37364ae02c3SLuigi Rizzo 	na.ifp = sc->rl_ifp;
374*ce3ee1e7SLuigi Rizzo 	na.na_flags = NAF_BDG_MAYSLEEP;
37564ae02c3SLuigi Rizzo 	na.num_tx_desc = sc->rl_ldata.rl_tx_desc_cnt;
37664ae02c3SLuigi Rizzo 	na.num_rx_desc = sc->rl_ldata.rl_rx_desc_cnt;
37764ae02c3SLuigi Rizzo 	na.nm_txsync = re_netmap_txsync;
37864ae02c3SLuigi Rizzo 	na.nm_rxsync = re_netmap_rxsync;
37964ae02c3SLuigi Rizzo 	na.nm_register = re_netmap_reg;
38064ae02c3SLuigi Rizzo 	netmap_attach(&na, 1);
38164ae02c3SLuigi Rizzo }
38264ae02c3SLuigi Rizzo /* end of file */
383