1 /* 2 * Copyright (C) 2011 Luigi Rizzo. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 /* 27 * $FreeBSD$ 28 * $Id: if_re_netmap.h 10075 2011-12-25 22:55:48Z luigi $ 29 * 30 * netmap support for if_re 31 */ 32 33 #include <net/netmap.h> 34 #include <sys/selinfo.h> 35 #include <vm/vm.h> 36 #include <vm/pmap.h> /* vtophys ? */ 37 #include <dev/netmap/netmap_kern.h> 38 39 static int re_netmap_reg(struct ifnet *, int onoff); 40 static int re_netmap_txsync(struct ifnet *, u_int, int); 41 static int re_netmap_rxsync(struct ifnet *, u_int, int); 42 static void re_netmap_lock_wrapper(struct ifnet *, int, u_int); 43 44 static void 45 re_netmap_attach(struct rl_softc *sc) 46 { 47 struct netmap_adapter na; 48 49 bzero(&na, sizeof(na)); 50 51 na.ifp = sc->rl_ifp; 52 na.separate_locks = 0; 53 na.num_tx_desc = sc->rl_ldata.rl_tx_desc_cnt; 54 na.num_rx_desc = sc->rl_ldata.rl_rx_desc_cnt; 55 na.nm_txsync = re_netmap_txsync; 56 na.nm_rxsync = re_netmap_rxsync; 57 na.nm_lock = re_netmap_lock_wrapper; 58 na.nm_register = re_netmap_reg; 59 netmap_attach(&na, 1); 60 } 61 62 63 /* 64 * wrapper to export locks to the generic code 65 * We should not use the tx/rx locks 66 */ 67 static void 68 re_netmap_lock_wrapper(struct ifnet *ifp, int what, u_int queueid) 69 { 70 struct rl_softc *adapter = ifp->if_softc; 71 72 switch (what) { 73 case NETMAP_CORE_LOCK: 74 RL_LOCK(adapter); 75 break; 76 case NETMAP_CORE_UNLOCK: 77 RL_UNLOCK(adapter); 78 break; 79 80 case NETMAP_TX_LOCK: 81 case NETMAP_RX_LOCK: 82 case NETMAP_TX_UNLOCK: 83 case NETMAP_RX_UNLOCK: 84 D("invalid lock call %d, no tx/rx locks here", what); 85 break; 86 } 87 } 88 89 90 /* 91 * support for netmap register/unregisted. We are already under core lock. 92 * only called on the first register or the last unregister. 93 */ 94 static int 95 re_netmap_reg(struct ifnet *ifp, int onoff) 96 { 97 struct rl_softc *adapter = ifp->if_softc; 98 struct netmap_adapter *na = NA(ifp); 99 int error = 0; 100 101 if (na == NULL) 102 return EINVAL; 103 /* Tell the stack that the interface is no longer active */ 104 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 105 106 re_stop(adapter); 107 108 if (onoff) { 109 ifp->if_capenable |= IFCAP_NETMAP; 110 111 /* save if_transmit to restore it later */ 112 na->if_transmit = ifp->if_transmit; 113 ifp->if_transmit = netmap_start; 114 115 re_init_locked(adapter); 116 117 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) == 0) { 118 error = ENOMEM; 119 goto fail; 120 } 121 } else { 122 fail: 123 /* restore if_transmit */ 124 ifp->if_transmit = na->if_transmit; 125 ifp->if_capenable &= ~IFCAP_NETMAP; 126 re_init_locked(adapter); /* also enables intr */ 127 } 128 return (error); 129 } 130 131 132 /* 133 * Reconcile kernel and user view of the transmit ring. 134 */ 135 static int 136 re_netmap_txsync(struct ifnet *ifp, u_int ring_nr, int do_lock) 137 { 138 struct rl_softc *sc = ifp->if_softc; 139 struct rl_txdesc *txd = sc->rl_ldata.rl_tx_desc; 140 struct netmap_adapter *na = NA(sc->rl_ifp); 141 struct netmap_kring *kring = &na->tx_rings[ring_nr]; 142 struct netmap_ring *ring = kring->ring; 143 int j, k, l, n, lim = kring->nkr_num_slots - 1; 144 145 k = ring->cur; 146 if (k > lim) 147 return netmap_ring_reinit(kring); 148 149 if (do_lock) 150 RL_LOCK(sc); 151 152 /* Sync the TX descriptor list */ 153 bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag, 154 sc->rl_ldata.rl_tx_list_map, 155 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 156 157 /* XXX move after the transmissions */ 158 /* record completed transmissions */ 159 for (n = 0, l = sc->rl_ldata.rl_tx_considx; 160 l != sc->rl_ldata.rl_tx_prodidx; 161 n++, l = RL_TX_DESC_NXT(sc, l)) { 162 uint32_t cmdstat = 163 le32toh(sc->rl_ldata.rl_tx_list[l].rl_cmdstat); 164 if (cmdstat & RL_TDESC_STAT_OWN) 165 break; 166 } 167 if (n > 0) { 168 sc->rl_ldata.rl_tx_considx = l; 169 sc->rl_ldata.rl_tx_free += n; 170 kring->nr_hwavail += n; 171 } 172 173 /* update avail to what the hardware knows */ 174 ring->avail = kring->nr_hwavail; 175 176 j = kring->nr_hwcur; 177 if (j != k) { /* we have new packets to send */ 178 l = sc->rl_ldata.rl_tx_prodidx; 179 for (n = 0; j != k; n++) { 180 struct netmap_slot *slot = &ring->slot[j]; 181 struct rl_desc *desc = &sc->rl_ldata.rl_tx_list[l]; 182 int cmd = slot->len | RL_TDESC_CMD_EOF | 183 RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF ; 184 uint64_t paddr; 185 void *addr = PNMB(slot, &paddr); 186 int len = slot->len; 187 188 if (addr == netmap_buffer_base || len > NETMAP_BUF_SIZE) { 189 if (do_lock) 190 RL_UNLOCK(sc); 191 // XXX what about prodidx ? 192 return netmap_ring_reinit(kring); 193 } 194 195 if (l == lim) /* mark end of ring */ 196 cmd |= RL_TDESC_CMD_EOR; 197 198 if (slot->flags & NS_BUF_CHANGED) { 199 desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr)); 200 desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr)); 201 /* buffer has changed, unload and reload map */ 202 netmap_reload_map(sc->rl_ldata.rl_tx_mtag, 203 txd[l].tx_dmamap, addr); 204 slot->flags &= ~NS_BUF_CHANGED; 205 } 206 slot->flags &= ~NS_REPORT; 207 desc->rl_cmdstat = htole32(cmd); 208 bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, 209 txd[l].tx_dmamap, BUS_DMASYNC_PREWRITE); 210 j = (j == lim) ? 0 : j + 1; 211 l = (l == lim) ? 0 : l + 1; 212 } 213 sc->rl_ldata.rl_tx_prodidx = l; 214 kring->nr_hwcur = k; 215 216 /* decrease avail by number of sent packets */ 217 ring->avail -= n; 218 kring->nr_hwavail = ring->avail; 219 220 bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag, 221 sc->rl_ldata.rl_tx_list_map, 222 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 223 224 /* start ? */ 225 CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START); 226 } 227 if (do_lock) 228 RL_UNLOCK(sc); 229 return 0; 230 } 231 232 233 /* 234 * Reconcile kernel and user view of the receive ring. 235 */ 236 static int 237 re_netmap_rxsync(struct ifnet *ifp, u_int ring_nr, int do_lock) 238 { 239 struct rl_softc *sc = ifp->if_softc; 240 struct rl_rxdesc *rxd = sc->rl_ldata.rl_rx_desc; 241 struct netmap_adapter *na = NA(sc->rl_ifp); 242 struct netmap_kring *kring = &na->rx_rings[ring_nr]; 243 struct netmap_ring *ring = kring->ring; 244 int j, k, l, n, lim = kring->nkr_num_slots - 1; 245 246 k = ring->cur; 247 if (k > lim) 248 return netmap_ring_reinit(kring); 249 250 if (do_lock) 251 RL_LOCK(sc); 252 /* XXX check sync modes */ 253 bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 254 sc->rl_ldata.rl_rx_list_map, 255 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 256 257 /* 258 * The device uses all the buffers in the ring, so we need 259 * another termination condition in addition to RL_RDESC_STAT_OWN 260 * cleared (all buffers could have it cleared. The easiest one 261 * is to limit the amount of data reported up to 'lim' 262 */ 263 l = sc->rl_ldata.rl_rx_prodidx; /* next pkt to check */ 264 j = netmap_ridx_n2k(na, ring_nr, l); /* the kring index */ 265 for (n = kring->nr_hwavail; n < lim ; n++) { 266 struct rl_desc *cur_rx = &sc->rl_ldata.rl_rx_list[l]; 267 uint32_t rxstat = le32toh(cur_rx->rl_cmdstat); 268 uint32_t total_len; 269 270 if ((rxstat & RL_RDESC_STAT_OWN) != 0) 271 break; 272 total_len = rxstat & sc->rl_rxlenmask; 273 /* XXX subtract crc */ 274 total_len = (total_len < 4) ? 0 : total_len - 4; 275 kring->ring->slot[j].len = total_len; 276 /* sync was in re_newbuf() */ 277 bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, 278 rxd[l].rx_dmamap, BUS_DMASYNC_POSTREAD); 279 j = (j == lim) ? 0 : j + 1; 280 l = (l == lim) ? 0 : l + 1; 281 } 282 if (n != kring->nr_hwavail) { 283 sc->rl_ldata.rl_rx_prodidx = l; 284 sc->rl_ifp->if_ipackets += n - kring->nr_hwavail; 285 kring->nr_hwavail = n; 286 } 287 288 /* skip past packets that userspace has already processed, 289 * making them available for reception. 290 * advance nr_hwcur and issue a bus_dmamap_sync on the 291 * buffers so it is safe to write to them. 292 * Also increase nr_hwavail 293 */ 294 j = kring->nr_hwcur; 295 if (j != k) { /* userspace has read some packets. */ 296 l = netmap_ridx_k2n(na, ring_nr, j); /* the NIC index */ 297 for (n = 0; j != k; n++) { 298 struct netmap_slot *slot = ring->slot + j; 299 struct rl_desc *desc = &sc->rl_ldata.rl_rx_list[l]; 300 int cmd = na->buff_size | RL_RDESC_CMD_OWN; 301 uint64_t paddr; 302 void *addr = PNMB(slot, &paddr); 303 304 if (addr == netmap_buffer_base) { /* bad buf */ 305 if (do_lock) 306 RL_UNLOCK(sc); 307 return netmap_ring_reinit(kring); 308 } 309 310 if (l == lim) /* mark end of ring */ 311 cmd |= RL_RDESC_CMD_EOR; 312 313 desc->rl_cmdstat = htole32(cmd); 314 slot->flags &= ~NS_REPORT; 315 if (slot->flags & NS_BUF_CHANGED) { 316 desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr)); 317 desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr)); 318 netmap_reload_map(sc->rl_ldata.rl_rx_mtag, 319 rxd[l].rx_dmamap, addr); 320 slot->flags &= ~NS_BUF_CHANGED; 321 } 322 bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, 323 rxd[l].rx_dmamap, BUS_DMASYNC_PREREAD); 324 j = (j == lim) ? 0 : j + 1; 325 l = (l == lim) ? 0 : l + 1; 326 } 327 kring->nr_hwavail -= n; 328 kring->nr_hwcur = k; 329 /* Flush the RX DMA ring */ 330 331 bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag, 332 sc->rl_ldata.rl_rx_list_map, 333 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD); 334 } 335 /* tell userspace that there are new packets */ 336 ring->avail = kring->nr_hwavail; 337 if (do_lock) 338 RL_UNLOCK(sc); 339 return 0; 340 } 341 342 /* 343 * Additional routines to init the tx and rx rings. 344 * In other drivers we do that inline in the main code. 345 */ 346 static void 347 re_netmap_tx_init(struct rl_softc *sc) 348 { 349 struct rl_txdesc *txd; 350 struct rl_desc *desc; 351 int i, n; 352 struct netmap_adapter *na = NA(sc->rl_ifp); 353 struct netmap_slot *slot = netmap_reset(na, NR_TX, 0, 0); 354 355 /* slot is NULL if we are not in netmap mode */ 356 if (!slot) 357 return; 358 /* in netmap mode, overwrite addresses and maps */ 359 txd = sc->rl_ldata.rl_tx_desc; 360 desc = sc->rl_ldata.rl_tx_list; 361 n = sc->rl_ldata.rl_tx_desc_cnt; 362 363 /* l points in the netmap ring, i points in the NIC ring */ 364 for (i = 0; i < n; i++) { 365 uint64_t paddr; 366 int l = netmap_tidx_n2k(na, 0, i); 367 void *addr = PNMB(slot + l, &paddr); 368 369 desc[i].rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr)); 370 desc[i].rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr)); 371 netmap_load_map(sc->rl_ldata.rl_tx_mtag, 372 txd[i].tx_dmamap, addr); 373 } 374 } 375 376 static void 377 re_netmap_rx_init(struct rl_softc *sc) 378 { 379 struct netmap_adapter *na = NA(sc->rl_ifp); 380 struct netmap_slot *slot = netmap_reset(na, NR_RX, 0, 0); 381 struct rl_desc *desc = sc->rl_ldata.rl_rx_list; 382 uint32_t cmdstat; 383 int i, n, max_avail; 384 385 if (!slot) 386 return; 387 n = sc->rl_ldata.rl_rx_desc_cnt; 388 /* 389 * Userspace owned hwavail packets before the reset, 390 * so the NIC that last hwavail descriptors of the ring 391 * are still owned by the driver (and keep one empty). 392 */ 393 max_avail = n - 1 - na->rx_rings[0].nr_hwavail; 394 for (i = 0; i < n; i++) { 395 void *addr; 396 uint64_t paddr; 397 int l = netmap_ridx_n2k(na, 0, i); 398 399 addr = PNMB(slot + l, &paddr); 400 401 netmap_reload_map(sc->rl_ldata.rl_rx_mtag, 402 sc->rl_ldata.rl_rx_desc[i].rx_dmamap, addr); 403 bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, 404 sc->rl_ldata.rl_rx_desc[i].rx_dmamap, BUS_DMASYNC_PREREAD); 405 desc[i].rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr)); 406 desc[i].rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr)); 407 cmdstat = na->buff_size; 408 if (i == n - 1) /* mark the end of ring */ 409 cmdstat |= RL_RDESC_CMD_EOR; 410 if (i < max_avail) 411 cmdstat |= RL_RDESC_CMD_OWN; 412 desc[i].rl_cmdstat = htole32(cmdstat); 413 } 414 } 415