2 * Copyright (C) 2011 Luigi Rizzo. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
29 * netmap support for "re"
30 * For details on netmap support please see ixgbe_netmap.h
34 #include <net/netmap.h>
35 #include <sys/selinfo.h>
37 #include <vm/pmap.h> /* vtophys ? */
38 #include <dev/netmap/netmap_kern.h>
42 * wrapper to export locks to the generic code
43 * We should not use the tx/rx locks
46 re_netmap_lock_wrapper(struct ifnet *ifp, int what, u_int queueid)
48 struct rl_softc *adapter = ifp->if_softc;
51 case NETMAP_CORE_LOCK:
54 case NETMAP_CORE_UNLOCK:
60 case NETMAP_TX_UNLOCK:
61 case NETMAP_RX_UNLOCK:
62 D("invalid lock call %d, no tx/rx locks here", what);
69 * support for netmap register/unregisted. We are already under core lock.
70 * only called on the first register or the last unregister.
73 re_netmap_reg(struct ifnet *ifp, int onoff)
75 struct rl_softc *adapter = ifp->if_softc;
76 struct netmap_adapter *na = NA(ifp);
81 /* Tell the stack that the interface is no longer active */
82 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
87 ifp->if_capenable |= IFCAP_NETMAP;
89 /* save if_transmit to restore it later */
90 na->if_transmit = ifp->if_transmit;
91 ifp->if_transmit = netmap_start;
93 re_init_locked(adapter);
95 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) == 0) {
101 /* restore if_transmit */
102 ifp->if_transmit = na->if_transmit;
103 ifp->if_capenable &= ~IFCAP_NETMAP;
104 re_init_locked(adapter); /* also enables intr */
111 * Reconcile kernel and user view of the transmit ring.
114 re_netmap_txsync(struct ifnet *ifp, u_int ring_nr, int do_lock)
116 struct rl_softc *sc = ifp->if_softc;
117 struct rl_txdesc *txd = sc->rl_ldata.rl_tx_desc;
118 struct netmap_adapter *na = NA(sc->rl_ifp);
119 struct netmap_kring *kring = &na->tx_rings[ring_nr];
120 struct netmap_ring *ring = kring->ring;
121 int j, k, l, n, lim = kring->nkr_num_slots - 1;
125 return netmap_ring_reinit(kring);
130 /* Sync the TX descriptor list */
131 bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
132 sc->rl_ldata.rl_tx_list_map,
133 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
135 /* XXX move after the transmissions */
136 /* record completed transmissions */
137 for (n = 0, l = sc->rl_ldata.rl_tx_considx;
138 l != sc->rl_ldata.rl_tx_prodidx;
139 n++, l = RL_TX_DESC_NXT(sc, l)) {
141 le32toh(sc->rl_ldata.rl_tx_list[l].rl_cmdstat);
142 if (cmdstat & RL_TDESC_STAT_OWN)
146 sc->rl_ldata.rl_tx_considx = l;
147 sc->rl_ldata.rl_tx_free += n;
148 kring->nr_hwavail += n;
151 /* update avail to what the kernel knows */
152 ring->avail = kring->nr_hwavail;
155 if (j != k) { /* we have new packets to send */
156 l = sc->rl_ldata.rl_tx_prodidx;
157 for (n = 0; j != k; n++) {
158 struct netmap_slot *slot = &ring->slot[j];
159 struct rl_desc *desc = &sc->rl_ldata.rl_tx_list[l];
160 int cmd = slot->len | RL_TDESC_CMD_EOF |
161 RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF ;
163 void *addr = PNMB(slot, &paddr);
166 if (addr == netmap_buffer_base || len > NETMAP_BUF_SIZE) {
169 // XXX what about prodidx ?
170 return netmap_ring_reinit(kring);
173 if (l == lim) /* mark end of ring */
174 cmd |= RL_TDESC_CMD_EOR;
176 if (slot->flags & NS_BUF_CHANGED) {
177 desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr));
178 desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr));
179 /* buffer has changed, unload and reload map */
180 netmap_reload_map(sc->rl_ldata.rl_tx_mtag,
181 txd[l].tx_dmamap, addr);
182 slot->flags &= ~NS_BUF_CHANGED;
184 slot->flags &= ~NS_REPORT;
185 desc->rl_cmdstat = htole32(cmd);
186 bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
187 txd[l].tx_dmamap, BUS_DMASYNC_PREWRITE);
188 j = (j == lim) ? 0 : j + 1;
189 l = (l == lim) ? 0 : l + 1;
191 sc->rl_ldata.rl_tx_prodidx = l;
192 kring->nr_hwcur = k; /* the saved ring->cur */
193 ring->avail -= n; // XXX see others
194 kring->nr_hwavail = ring->avail;
196 bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
197 sc->rl_ldata.rl_tx_list_map,
198 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
201 CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
210 * Reconcile kernel and user view of the receive ring.
213 re_netmap_rxsync(struct ifnet *ifp, u_int ring_nr, int do_lock)
215 struct rl_softc *sc = ifp->if_softc;
216 struct rl_rxdesc *rxd = sc->rl_ldata.rl_rx_desc;
217 struct netmap_adapter *na = NA(sc->rl_ifp);
218 struct netmap_kring *kring = &na->rx_rings[ring_nr];
219 struct netmap_ring *ring = kring->ring;
220 int j, l, n, lim = kring->nkr_num_slots - 1;
221 int force_update = do_lock || kring->nr_kflags & NKR_PENDINTR;
222 u_int k = ring->cur, resvd = ring->reserved;
226 return netmap_ring_reinit(kring);
230 /* XXX check sync modes */
231 bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
232 sc->rl_ldata.rl_rx_list_map,
233 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
236 * Import newly received packets into the netmap ring.
237 * j is an index in the netmap ring, l in the NIC ring.
239 * The device uses all the buffers in the ring, so we need
240 * another termination condition in addition to RL_RDESC_STAT_OWN
241 * cleared (all buffers could have it cleared. The easiest one
242 * is to limit the amount of data reported up to 'lim'
244 l = sc->rl_ldata.rl_rx_prodidx; /* next pkt to check */
245 j = netmap_idx_n2k(kring, l); /* the kring index */
246 if (netmap_no_pendintr || force_update) {
247 uint16_t slot_flags = kring->nkr_slot_flags;
249 for (n = kring->nr_hwavail; n < lim ; n++) {
250 struct rl_desc *cur_rx = &sc->rl_ldata.rl_rx_list[l];
251 uint32_t rxstat = le32toh(cur_rx->rl_cmdstat);
254 if ((rxstat & RL_RDESC_STAT_OWN) != 0)
256 total_len = rxstat & sc->rl_rxlenmask;
257 /* XXX subtract crc */
258 total_len = (total_len < 4) ? 0 : total_len - 4;
259 kring->ring->slot[j].len = total_len;
260 kring->ring->slot[j].flags = slot_flags;
261 /* sync was in re_newbuf() */
262 bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
263 rxd[l].rx_dmamap, BUS_DMASYNC_POSTREAD);
264 j = (j == lim) ? 0 : j + 1;
265 l = (l == lim) ? 0 : l + 1;
267 if (n != kring->nr_hwavail) {
268 sc->rl_ldata.rl_rx_prodidx = l;
269 sc->rl_ifp->if_ipackets += n - kring->nr_hwavail;
270 kring->nr_hwavail = n;
272 kring->nr_kflags &= ~NKR_PENDINTR;
275 /* skip past packets that userspace has released */
278 if (resvd + ring->avail >= lim + 1) {
279 D("XXX invalid reserve/avail %d %d", resvd, ring->avail);
280 ring->reserved = resvd = 0; // XXX panic...
282 k = (k >= resvd) ? k - resvd : k + lim + 1 - resvd;
284 if (j != k) { /* userspace has released some packets. */
285 l = netmap_idx_k2n(kring, j); /* the NIC index */
286 for (n = 0; j != k; n++) {
287 struct netmap_slot *slot = ring->slot + j;
288 struct rl_desc *desc = &sc->rl_ldata.rl_rx_list[l];
289 int cmd = NETMAP_BUF_SIZE | RL_RDESC_CMD_OWN;
291 void *addr = PNMB(slot, &paddr);
293 if (addr == netmap_buffer_base) { /* bad buf */
296 return netmap_ring_reinit(kring);
299 if (l == lim) /* mark end of ring */
300 cmd |= RL_RDESC_CMD_EOR;
302 slot->flags &= ~NS_REPORT;
303 if (slot->flags & NS_BUF_CHANGED) {
304 netmap_reload_map(sc->rl_ldata.rl_rx_mtag,
305 rxd[l].rx_dmamap, addr);
306 desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr));
307 desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr));
308 slot->flags &= ~NS_BUF_CHANGED;
310 desc->rl_cmdstat = htole32(cmd);
311 bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
312 rxd[l].rx_dmamap, BUS_DMASYNC_PREREAD);
313 j = (j == lim) ? 0 : j + 1;
314 l = (l == lim) ? 0 : l + 1;
316 kring->nr_hwavail -= n;
318 /* Flush the RX DMA ring */
320 bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
321 sc->rl_ldata.rl_rx_list_map,
322 BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
324 /* tell userspace that there are new packets */
325 ring->avail = kring->nr_hwavail - resvd;
332 * Additional routines to init the tx and rx rings.
333 * In other drivers we do that inline in the main code.
336 re_netmap_tx_init(struct rl_softc *sc)
338 struct rl_txdesc *txd;
339 struct rl_desc *desc;
341 struct netmap_adapter *na = NA(sc->rl_ifp);
342 struct netmap_slot *slot = netmap_reset(na, NR_TX, 0, 0);
344 /* slot is NULL if we are not in netmap mode */
347 /* in netmap mode, overwrite addresses and maps */
348 txd = sc->rl_ldata.rl_tx_desc;
349 desc = sc->rl_ldata.rl_tx_list;
350 n = sc->rl_ldata.rl_tx_desc_cnt;
352 /* l points in the netmap ring, i points in the NIC ring */
353 for (i = 0; i < n; i++) {
355 int l = netmap_idx_n2k(&na->tx_rings[0], i);
356 void *addr = PNMB(slot + l, &paddr);
358 desc[i].rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr));
359 desc[i].rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr));
360 netmap_load_map(sc->rl_ldata.rl_tx_mtag,
361 txd[i].tx_dmamap, addr);
366 re_netmap_rx_init(struct rl_softc *sc)
368 struct netmap_adapter *na = NA(sc->rl_ifp);
369 struct netmap_slot *slot = netmap_reset(na, NR_RX, 0, 0);
370 struct rl_desc *desc = sc->rl_ldata.rl_rx_list;
376 n = sc->rl_ldata.rl_rx_desc_cnt;
378 * Userspace owned hwavail packets before the reset,
379 * so the NIC that last hwavail descriptors of the ring
380 * are still owned by the driver (and keep one empty).
382 max_avail = n - 1 - na->rx_rings[0].nr_hwavail;
383 for (i = 0; i < n; i++) {
386 int l = netmap_idx_n2k(&na->rx_rings[0], i);
388 addr = PNMB(slot + l, &paddr);
390 netmap_reload_map(sc->rl_ldata.rl_rx_mtag,
391 sc->rl_ldata.rl_rx_desc[i].rx_dmamap, addr);
392 bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
393 sc->rl_ldata.rl_rx_desc[i].rx_dmamap, BUS_DMASYNC_PREREAD);
394 desc[i].rl_bufaddr_lo = htole32(RL_ADDR_LO(paddr));
395 desc[i].rl_bufaddr_hi = htole32(RL_ADDR_HI(paddr));
396 cmdstat = NETMAP_BUF_SIZE;
397 if (i == n - 1) /* mark the end of ring */
398 cmdstat |= RL_RDESC_CMD_EOR;
400 cmdstat |= RL_RDESC_CMD_OWN;
401 desc[i].rl_cmdstat = htole32(cmdstat);
407 re_netmap_attach(struct rl_softc *sc)
409 struct netmap_adapter na;
411 bzero(&na, sizeof(na));
414 na.separate_locks = 0;
415 na.num_tx_desc = sc->rl_ldata.rl_tx_desc_cnt;
416 na.num_rx_desc = sc->rl_ldata.rl_rx_desc_cnt;
417 na.nm_txsync = re_netmap_txsync;
418 na.nm_rxsync = re_netmap_rxsync;
419 na.nm_lock = re_netmap_lock_wrapper;
420 na.nm_register = re_netmap_reg;
421 netmap_attach(&na, 1);