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$ 2838b4948bSLuigi Rizzo * $Id: if_re_netmap.h 10075 2011-12-25 22:55:48Z 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); 401a26580eSLuigi Rizzo static int re_netmap_txsync(struct ifnet *, u_int, int); 411a26580eSLuigi Rizzo static int re_netmap_rxsync(struct ifnet *, u_int, int); 421a26580eSLuigi Rizzo static void re_netmap_lock_wrapper(struct ifnet *, 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; 5968b8534bSLuigi Rizzo netmap_attach(&na, 1); 6068b8534bSLuigi Rizzo } 6168b8534bSLuigi Rizzo 6268b8534bSLuigi Rizzo 6368b8534bSLuigi Rizzo /* 6468b8534bSLuigi Rizzo * wrapper to export locks to the generic code 6568b8534bSLuigi Rizzo * We should not use the tx/rx locks 6668b8534bSLuigi Rizzo */ 6768b8534bSLuigi Rizzo static void 681a26580eSLuigi Rizzo re_netmap_lock_wrapper(struct ifnet *ifp, int what, u_int queueid) 6968b8534bSLuigi Rizzo { 701a26580eSLuigi Rizzo struct rl_softc *adapter = ifp->if_softc; 7168b8534bSLuigi Rizzo 7268b8534bSLuigi Rizzo switch (what) { 7368b8534bSLuigi Rizzo case NETMAP_CORE_LOCK: 7468b8534bSLuigi Rizzo RL_LOCK(adapter); 7568b8534bSLuigi Rizzo break; 7668b8534bSLuigi Rizzo case NETMAP_CORE_UNLOCK: 7768b8534bSLuigi Rizzo RL_UNLOCK(adapter); 7868b8534bSLuigi Rizzo break; 7968b8534bSLuigi Rizzo 8068b8534bSLuigi Rizzo case NETMAP_TX_LOCK: 8168b8534bSLuigi Rizzo case NETMAP_RX_LOCK: 8268b8534bSLuigi Rizzo case NETMAP_TX_UNLOCK: 8368b8534bSLuigi Rizzo case NETMAP_RX_UNLOCK: 8468b8534bSLuigi Rizzo D("invalid lock call %d, no tx/rx locks here", what); 8568b8534bSLuigi Rizzo break; 8668b8534bSLuigi Rizzo } 8768b8534bSLuigi Rizzo } 8868b8534bSLuigi Rizzo 8968b8534bSLuigi Rizzo 9068b8534bSLuigi Rizzo /* 9168b8534bSLuigi Rizzo * support for netmap register/unregisted. We are already under core lock. 9268b8534bSLuigi Rizzo * only called on the first register or the last unregister. 9368b8534bSLuigi Rizzo */ 9468b8534bSLuigi Rizzo static int 9568b8534bSLuigi Rizzo re_netmap_reg(struct ifnet *ifp, int onoff) 9668b8534bSLuigi Rizzo { 9768b8534bSLuigi Rizzo struct rl_softc *adapter = ifp->if_softc; 9868b8534bSLuigi Rizzo struct netmap_adapter *na = NA(ifp); 9968b8534bSLuigi Rizzo int error = 0; 10068b8534bSLuigi Rizzo 101506cc70cSLuigi Rizzo if (na == NULL) 10268b8534bSLuigi Rizzo return EINVAL; 10368b8534bSLuigi Rizzo /* Tell the stack that the interface is no longer active */ 10468b8534bSLuigi Rizzo ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 10568b8534bSLuigi Rizzo 10668b8534bSLuigi Rizzo re_stop(adapter); 10768b8534bSLuigi Rizzo 10868b8534bSLuigi Rizzo if (onoff) { 10968b8534bSLuigi Rizzo ifp->if_capenable |= IFCAP_NETMAP; 11068b8534bSLuigi Rizzo 111506cc70cSLuigi Rizzo /* save if_transmit to restore it later */ 11268b8534bSLuigi Rizzo na->if_transmit = ifp->if_transmit; 11368b8534bSLuigi Rizzo ifp->if_transmit = netmap_start; 11468b8534bSLuigi Rizzo 11568b8534bSLuigi Rizzo re_init_locked(adapter); 11668b8534bSLuigi Rizzo 11768b8534bSLuigi Rizzo if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) == 0) { 11868b8534bSLuigi Rizzo error = ENOMEM; 11968b8534bSLuigi Rizzo goto fail; 12068b8534bSLuigi Rizzo } 12168b8534bSLuigi Rizzo } else { 12268b8534bSLuigi Rizzo fail: 12368b8534bSLuigi Rizzo /* restore if_transmit */ 12468b8534bSLuigi Rizzo ifp->if_transmit = na->if_transmit; 12568b8534bSLuigi Rizzo ifp->if_capenable &= ~IFCAP_NETMAP; 12668b8534bSLuigi Rizzo re_init_locked(adapter); /* also enables intr */ 12768b8534bSLuigi Rizzo } 12868b8534bSLuigi Rizzo return (error); 12968b8534bSLuigi Rizzo } 13068b8534bSLuigi Rizzo 13168b8534bSLuigi Rizzo 13268b8534bSLuigi Rizzo /* 13368b8534bSLuigi Rizzo * Reconcile kernel and user view of the transmit ring. 13468b8534bSLuigi Rizzo */ 13568b8534bSLuigi Rizzo static int 1361a26580eSLuigi Rizzo re_netmap_txsync(struct ifnet *ifp, u_int ring_nr, int do_lock) 13768b8534bSLuigi Rizzo { 1381a26580eSLuigi Rizzo struct rl_softc *sc = ifp->if_softc; 13968b8534bSLuigi Rizzo struct rl_txdesc *txd = sc->rl_ldata.rl_tx_desc; 14068b8534bSLuigi Rizzo struct netmap_adapter *na = NA(sc->rl_ifp); 14168b8534bSLuigi Rizzo struct netmap_kring *kring = &na->tx_rings[ring_nr]; 14268b8534bSLuigi Rizzo struct netmap_ring *ring = kring->ring; 143506cc70cSLuigi Rizzo int j, k, l, n, lim = kring->nkr_num_slots - 1; 14468b8534bSLuigi Rizzo 14568b8534bSLuigi Rizzo k = ring->cur; 146506cc70cSLuigi Rizzo if (k > lim) 14768b8534bSLuigi Rizzo return netmap_ring_reinit(kring); 14868b8534bSLuigi Rizzo 14968b8534bSLuigi Rizzo if (do_lock) 15068b8534bSLuigi Rizzo RL_LOCK(sc); 15168b8534bSLuigi Rizzo 15268b8534bSLuigi Rizzo /* Sync the TX descriptor list */ 15368b8534bSLuigi Rizzo bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag, 15468b8534bSLuigi Rizzo sc->rl_ldata.rl_tx_list_map, 15568b8534bSLuigi Rizzo BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 15668b8534bSLuigi Rizzo 157506cc70cSLuigi Rizzo /* XXX move after the transmissions */ 15868b8534bSLuigi Rizzo /* record completed transmissions */ 159506cc70cSLuigi Rizzo for (n = 0, l = sc->rl_ldata.rl_tx_considx; 160506cc70cSLuigi Rizzo l != sc->rl_ldata.rl_tx_prodidx; 161506cc70cSLuigi Rizzo n++, l = RL_TX_DESC_NXT(sc, l)) { 16268b8534bSLuigi Rizzo uint32_t cmdstat = 163506cc70cSLuigi Rizzo le32toh(sc->rl_ldata.rl_tx_list[l].rl_cmdstat); 16468b8534bSLuigi Rizzo if (cmdstat & RL_TDESC_STAT_OWN) 16568b8534bSLuigi Rizzo break; 16668b8534bSLuigi Rizzo } 16768b8534bSLuigi Rizzo if (n > 0) { 168506cc70cSLuigi Rizzo sc->rl_ldata.rl_tx_considx = l; 16968b8534bSLuigi Rizzo sc->rl_ldata.rl_tx_free += n; 17068b8534bSLuigi Rizzo kring->nr_hwavail += n; 17168b8534bSLuigi Rizzo } 17268b8534bSLuigi Rizzo 17368b8534bSLuigi Rizzo /* update avail to what the hardware knows */ 17468b8534bSLuigi Rizzo ring->avail = kring->nr_hwavail; 17568b8534bSLuigi Rizzo 176506cc70cSLuigi Rizzo j = kring->nr_hwcur; 17768b8534bSLuigi Rizzo if (j != k) { /* we have new packets to send */ 17868b8534bSLuigi Rizzo n = 0; 179506cc70cSLuigi Rizzo l = sc->rl_ldata.rl_tx_prodidx; 18068b8534bSLuigi Rizzo while (j != k) { 18168b8534bSLuigi Rizzo struct netmap_slot *slot = &ring->slot[j]; 182506cc70cSLuigi Rizzo struct rl_desc *desc = &sc->rl_ldata.rl_tx_list[l]; 18368b8534bSLuigi Rizzo int cmd = slot->len | RL_TDESC_CMD_EOF | 18468b8534bSLuigi Rizzo RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF ; 1856e10c8b8SLuigi Rizzo uint64_t paddr; 1866e10c8b8SLuigi Rizzo void *addr = PNMB(slot, &paddr); 18768b8534bSLuigi Rizzo int len = slot->len; 18868b8534bSLuigi Rizzo 18968b8534bSLuigi Rizzo if (addr == netmap_buffer_base || len > NETMAP_BUF_SIZE) { 19068b8534bSLuigi Rizzo if (do_lock) 19168b8534bSLuigi Rizzo RL_UNLOCK(sc); 192506cc70cSLuigi Rizzo // XXX what about prodidx ? 19368b8534bSLuigi Rizzo return netmap_ring_reinit(kring); 19468b8534bSLuigi Rizzo } 19568b8534bSLuigi Rizzo 196506cc70cSLuigi Rizzo if (l == lim) /* mark end of ring */ 19768b8534bSLuigi Rizzo cmd |= RL_TDESC_CMD_EOR; 19868b8534bSLuigi Rizzo 19968b8534bSLuigi Rizzo if (slot->flags & NS_BUF_CHANGED) { 20068b8534bSLuigi Rizzo desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr)); 20168b8534bSLuigi Rizzo desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr)); 20268b8534bSLuigi Rizzo /* buffer has changed, unload and reload map */ 20368b8534bSLuigi Rizzo netmap_reload_map(sc->rl_ldata.rl_tx_mtag, 2046e10c8b8SLuigi Rizzo txd[l].tx_dmamap, addr); 20568b8534bSLuigi Rizzo slot->flags &= ~NS_BUF_CHANGED; 20668b8534bSLuigi Rizzo } 20768b8534bSLuigi Rizzo slot->flags &= ~NS_REPORT; 20868b8534bSLuigi Rizzo desc->rl_cmdstat = htole32(cmd); 20968b8534bSLuigi Rizzo bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, 210506cc70cSLuigi Rizzo txd[l].tx_dmamap, BUS_DMASYNC_PREWRITE); 21168b8534bSLuigi Rizzo j = (j == lim) ? 0 : j + 1; 212506cc70cSLuigi Rizzo l = (l == lim) ? 0 : l + 1; 21368b8534bSLuigi Rizzo n++; 21468b8534bSLuigi Rizzo } 215506cc70cSLuigi Rizzo sc->rl_ldata.rl_tx_prodidx = l; 216506cc70cSLuigi Rizzo kring->nr_hwcur = k; 21768b8534bSLuigi Rizzo 21868b8534bSLuigi Rizzo /* decrease avail by number of sent packets */ 21968b8534bSLuigi Rizzo ring->avail -= n; 22068b8534bSLuigi Rizzo kring->nr_hwavail = ring->avail; 22168b8534bSLuigi Rizzo 22268b8534bSLuigi Rizzo bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag, 22368b8534bSLuigi Rizzo sc->rl_ldata.rl_tx_list_map, 22468b8534bSLuigi Rizzo BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 22568b8534bSLuigi Rizzo 22668b8534bSLuigi Rizzo /* start ? */ 22768b8534bSLuigi Rizzo CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START); 22868b8534bSLuigi Rizzo } 22968b8534bSLuigi Rizzo if (do_lock) 23068b8534bSLuigi Rizzo RL_UNLOCK(sc); 23168b8534bSLuigi Rizzo return 0; 23268b8534bSLuigi Rizzo } 23368b8534bSLuigi Rizzo 23468b8534bSLuigi Rizzo 23568b8534bSLuigi Rizzo /* 23668b8534bSLuigi Rizzo * Reconcile kernel and user view of the receive ring. 23768b8534bSLuigi Rizzo */ 23868b8534bSLuigi Rizzo static int 2391a26580eSLuigi Rizzo re_netmap_rxsync(struct ifnet *ifp, u_int ring_nr, int do_lock) 24068b8534bSLuigi Rizzo { 2411a26580eSLuigi Rizzo struct rl_softc *sc = ifp->if_softc; 24268b8534bSLuigi Rizzo struct rl_rxdesc *rxd = sc->rl_ldata.rl_rx_desc; 24368b8534bSLuigi Rizzo struct netmap_adapter *na = NA(sc->rl_ifp); 24468b8534bSLuigi Rizzo struct netmap_kring *kring = &na->rx_rings[ring_nr]; 24568b8534bSLuigi Rizzo struct netmap_ring *ring = kring->ring; 246506cc70cSLuigi Rizzo int j, k, l, n, lim = kring->nkr_num_slots - 1; 24768b8534bSLuigi Rizzo 24868b8534bSLuigi Rizzo k = ring->cur; 249506cc70cSLuigi Rizzo if (k > lim) 25068b8534bSLuigi Rizzo return netmap_ring_reinit(kring); 25168b8534bSLuigi Rizzo 25268b8534bSLuigi Rizzo if (do_lock) 25368b8534bSLuigi Rizzo RL_LOCK(sc); 25468b8534bSLuigi Rizzo /* XXX check sync modes */ 25568b8534bSLuigi Rizzo bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 25668b8534bSLuigi Rizzo sc->rl_ldata.rl_rx_list_map, 25768b8534bSLuigi Rizzo BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 25868b8534bSLuigi Rizzo 25968b8534bSLuigi Rizzo /* 26068b8534bSLuigi Rizzo * The device uses all the buffers in the ring, so we need 26168b8534bSLuigi Rizzo * another termination condition in addition to RL_RDESC_STAT_OWN 26268b8534bSLuigi Rizzo * cleared (all buffers could have it cleared. The easiest one 26368b8534bSLuigi Rizzo * is to limit the amount of data reported up to 'lim' 26468b8534bSLuigi Rizzo */ 265506cc70cSLuigi Rizzo l = sc->rl_ldata.rl_rx_prodidx; /* next pkt to check */ 266*5644ccecSLuigi Rizzo j = netmap_ridx_n2k(na, ring_nr, l); /* the kring index */ 26768b8534bSLuigi Rizzo for (n = kring->nr_hwavail; n < lim ; n++) { 268506cc70cSLuigi Rizzo struct rl_desc *cur_rx = &sc->rl_ldata.rl_rx_list[l]; 26968b8534bSLuigi Rizzo uint32_t rxstat = le32toh(cur_rx->rl_cmdstat); 27068b8534bSLuigi Rizzo uint32_t total_len; 27168b8534bSLuigi Rizzo 27268b8534bSLuigi Rizzo if ((rxstat & RL_RDESC_STAT_OWN) != 0) 27368b8534bSLuigi Rizzo break; 27468b8534bSLuigi Rizzo total_len = rxstat & sc->rl_rxlenmask; 27568b8534bSLuigi Rizzo /* XXX subtract crc */ 27668b8534bSLuigi Rizzo total_len = (total_len < 4) ? 0 : total_len - 4; 27768b8534bSLuigi Rizzo kring->ring->slot[j].len = total_len; 27868b8534bSLuigi Rizzo /* sync was in re_newbuf() */ 27968b8534bSLuigi Rizzo bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, 280506cc70cSLuigi Rizzo rxd[l].rx_dmamap, BUS_DMASYNC_POSTREAD); 281506cc70cSLuigi Rizzo j = (j == lim) ? 0 : j + 1; 282506cc70cSLuigi Rizzo l = (l == lim) ? 0 : l + 1; 28368b8534bSLuigi Rizzo } 28468b8534bSLuigi Rizzo if (n != kring->nr_hwavail) { 285506cc70cSLuigi Rizzo sc->rl_ldata.rl_rx_prodidx = l; 28668b8534bSLuigi Rizzo sc->rl_ifp->if_ipackets += n - kring->nr_hwavail; 28768b8534bSLuigi Rizzo kring->nr_hwavail = n; 28868b8534bSLuigi Rizzo } 28968b8534bSLuigi Rizzo 29068b8534bSLuigi Rizzo /* skip past packets that userspace has already processed, 29168b8534bSLuigi Rizzo * making them available for reception. 29268b8534bSLuigi Rizzo * advance nr_hwcur and issue a bus_dmamap_sync on the 29368b8534bSLuigi Rizzo * buffers so it is safe to write to them. 29468b8534bSLuigi Rizzo * Also increase nr_hwavail 29568b8534bSLuigi Rizzo */ 29668b8534bSLuigi Rizzo j = kring->nr_hwcur; 29768b8534bSLuigi Rizzo if (j != k) { /* userspace has read some packets. */ 29868b8534bSLuigi Rizzo n = 0; 299*5644ccecSLuigi Rizzo l = netmap_ridx_k2n(na, ring_nr, j); /* the NIC index */ 30068b8534bSLuigi Rizzo while (j != k) { 30168b8534bSLuigi Rizzo struct netmap_slot *slot = ring->slot + j; 302506cc70cSLuigi Rizzo struct rl_desc *desc = &sc->rl_ldata.rl_rx_list[l]; 30368b8534bSLuigi Rizzo int cmd = na->buff_size | RL_RDESC_CMD_OWN; 3046e10c8b8SLuigi Rizzo uint64_t paddr; 3056e10c8b8SLuigi Rizzo void *addr = PNMB(slot, &paddr); 30668b8534bSLuigi Rizzo 30768b8534bSLuigi Rizzo if (addr == netmap_buffer_base) { /* bad buf */ 30868b8534bSLuigi Rizzo if (do_lock) 30968b8534bSLuigi Rizzo RL_UNLOCK(sc); 31068b8534bSLuigi Rizzo return netmap_ring_reinit(kring); 31168b8534bSLuigi Rizzo } 31268b8534bSLuigi Rizzo 313506cc70cSLuigi Rizzo if (l == lim) /* mark end of ring */ 31468b8534bSLuigi Rizzo cmd |= RL_RDESC_CMD_EOR; 31568b8534bSLuigi Rizzo 31668b8534bSLuigi Rizzo desc->rl_cmdstat = htole32(cmd); 31768b8534bSLuigi Rizzo slot->flags &= ~NS_REPORT; 31868b8534bSLuigi Rizzo if (slot->flags & NS_BUF_CHANGED) { 31968b8534bSLuigi Rizzo desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr)); 32068b8534bSLuigi Rizzo desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr)); 32168b8534bSLuigi Rizzo netmap_reload_map(sc->rl_ldata.rl_rx_mtag, 3226e10c8b8SLuigi Rizzo rxd[l].rx_dmamap, addr); 32368b8534bSLuigi Rizzo slot->flags &= ~NS_BUF_CHANGED; 32468b8534bSLuigi Rizzo } 32568b8534bSLuigi Rizzo bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, 326506cc70cSLuigi Rizzo rxd[l].rx_dmamap, BUS_DMASYNC_PREREAD); 32768b8534bSLuigi Rizzo j = (j == lim) ? 0 : j + 1; 328506cc70cSLuigi Rizzo l = (l == lim) ? 0 : l + 1; 32968b8534bSLuigi Rizzo n++; 33068b8534bSLuigi Rizzo } 33168b8534bSLuigi Rizzo kring->nr_hwavail -= n; 33268b8534bSLuigi Rizzo kring->nr_hwcur = k; 33368b8534bSLuigi Rizzo /* Flush the RX DMA ring */ 33468b8534bSLuigi Rizzo 33568b8534bSLuigi Rizzo bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 33668b8534bSLuigi Rizzo sc->rl_ldata.rl_rx_list_map, 33768b8534bSLuigi Rizzo BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 33868b8534bSLuigi Rizzo } 33968b8534bSLuigi Rizzo /* tell userspace that there are new packets */ 34068b8534bSLuigi Rizzo ring->avail = kring->nr_hwavail; 34168b8534bSLuigi Rizzo if (do_lock) 34268b8534bSLuigi Rizzo RL_UNLOCK(sc); 34368b8534bSLuigi Rizzo return 0; 34468b8534bSLuigi Rizzo } 34568b8534bSLuigi Rizzo 346506cc70cSLuigi Rizzo /* 347506cc70cSLuigi Rizzo * Additional routines to init the tx and rx rings. 348506cc70cSLuigi Rizzo * In other drivers we do that inline in the main code. 349506cc70cSLuigi Rizzo */ 35068b8534bSLuigi Rizzo static void 35168b8534bSLuigi Rizzo re_netmap_tx_init(struct rl_softc *sc) 35268b8534bSLuigi Rizzo { 35368b8534bSLuigi Rizzo struct rl_txdesc *txd; 35468b8534bSLuigi Rizzo struct rl_desc *desc; 355506cc70cSLuigi Rizzo int i, n; 35668b8534bSLuigi Rizzo struct netmap_adapter *na = NA(sc->rl_ifp); 35768b8534bSLuigi Rizzo struct netmap_slot *slot = netmap_reset(na, NR_TX, 0, 0); 35868b8534bSLuigi Rizzo 35968b8534bSLuigi Rizzo /* slot is NULL if we are not in netmap mode */ 36068b8534bSLuigi Rizzo if (!slot) 36168b8534bSLuigi Rizzo return; 36268b8534bSLuigi Rizzo /* in netmap mode, overwrite addresses and maps */ 36368b8534bSLuigi Rizzo txd = sc->rl_ldata.rl_tx_desc; 36468b8534bSLuigi Rizzo desc = sc->rl_ldata.rl_tx_list; 365506cc70cSLuigi Rizzo n = sc->rl_ldata.rl_tx_desc_cnt; 36668b8534bSLuigi Rizzo 367506cc70cSLuigi Rizzo /* l points in the netmap ring, i points in the NIC ring */ 368506cc70cSLuigi Rizzo for (i = 0; i < n; i++) { 369506cc70cSLuigi Rizzo void *addr; 370506cc70cSLuigi Rizzo uint64_t paddr; 371*5644ccecSLuigi Rizzo int l = netmap_tidx_n2k(na, 0, i); 372506cc70cSLuigi Rizzo 3736e10c8b8SLuigi Rizzo addr = PNMB(slot + l, &paddr); 37468b8534bSLuigi Rizzo desc[i].rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr)); 37568b8534bSLuigi Rizzo desc[i].rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr)); 37668b8534bSLuigi Rizzo netmap_load_map(sc->rl_ldata.rl_tx_mtag, 3776e10c8b8SLuigi Rizzo txd[i].tx_dmamap, addr); 37868b8534bSLuigi Rizzo } 37968b8534bSLuigi Rizzo } 38068b8534bSLuigi Rizzo 38168b8534bSLuigi Rizzo static void 38268b8534bSLuigi Rizzo re_netmap_rx_init(struct rl_softc *sc) 38368b8534bSLuigi Rizzo { 38468b8534bSLuigi Rizzo struct netmap_adapter *na = NA(sc->rl_ifp); 38568b8534bSLuigi Rizzo struct netmap_slot *slot = netmap_reset(na, NR_RX, 0, 0); 38668b8534bSLuigi Rizzo struct rl_desc *desc = sc->rl_ldata.rl_rx_list; 38768b8534bSLuigi Rizzo uint32_t cmdstat; 388*5644ccecSLuigi Rizzo int i, n, max_avail; 38968b8534bSLuigi Rizzo 39068b8534bSLuigi Rizzo if (!slot) 39168b8534bSLuigi Rizzo return; 392506cc70cSLuigi Rizzo n = sc->rl_ldata.rl_rx_desc_cnt; 393*5644ccecSLuigi Rizzo /* 394*5644ccecSLuigi Rizzo * Userspace owned hwavail packets before the reset, 395*5644ccecSLuigi Rizzo * so the NIC that last hwavail descriptors of the ring 396*5644ccecSLuigi Rizzo * are still owned by the driver (and keep one empty). 397*5644ccecSLuigi Rizzo */ 398*5644ccecSLuigi Rizzo max_avail = n - 1 - na->rx_rings[0].nr_hwavail; 399506cc70cSLuigi Rizzo for (i = 0; i < n; i++) { 400506cc70cSLuigi Rizzo void *addr; 401506cc70cSLuigi Rizzo uint64_t paddr; 402*5644ccecSLuigi Rizzo int l = netmap_ridx_n2k(na, 0, i); 40368b8534bSLuigi Rizzo 4046e10c8b8SLuigi Rizzo addr = PNMB(slot + l, &paddr); 4056e10c8b8SLuigi Rizzo 4066e10c8b8SLuigi Rizzo netmap_reload_map(sc->rl_ldata.rl_rx_mtag, 4076e10c8b8SLuigi Rizzo sc->rl_ldata.rl_rx_desc[i].rx_dmamap, addr); 4086e10c8b8SLuigi Rizzo bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, 4096e10c8b8SLuigi Rizzo sc->rl_ldata.rl_rx_desc[i].rx_dmamap, BUS_DMASYNC_PREREAD); 41068b8534bSLuigi Rizzo desc[i].rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr)); 41168b8534bSLuigi Rizzo desc[i].rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr)); 412506cc70cSLuigi Rizzo cmdstat = na->buff_size; 413*5644ccecSLuigi Rizzo if (i == n - 1) /* mark the end of ring */ 41468b8534bSLuigi Rizzo cmdstat |= RL_RDESC_CMD_EOR; 415*5644ccecSLuigi Rizzo if (i < max_avail) 416506cc70cSLuigi Rizzo cmdstat |= RL_RDESC_CMD_OWN; 417506cc70cSLuigi Rizzo desc[i].rl_cmdstat = htole32(cmdstat); 41868b8534bSLuigi Rizzo } 41968b8534bSLuigi Rizzo } 420