2 * Copyright (C) 2011 Matteo Landi, 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 * $Id: if_lem_netmap.h 10627 2012-02-23 19:37:15Z luigi $
31 * netmap support for "lem"
33 * For details on netmap support please see ixgbe_netmap.h
36 #include <net/netmap.h>
37 #include <sys/selinfo.h>
39 #include <vm/pmap.h> /* vtophys ? */
40 #include <dev/netmap/netmap_kern.h>
44 lem_netmap_lock_wrapper(struct ifnet *ifp, int what, u_int ringid)
46 struct adapter *adapter = ifp->if_softc;
48 /* only one ring here so ignore the ringid */
50 case NETMAP_CORE_LOCK:
51 EM_CORE_LOCK(adapter);
53 case NETMAP_CORE_UNLOCK:
54 EM_CORE_UNLOCK(adapter);
59 case NETMAP_TX_UNLOCK:
60 EM_TX_UNLOCK(adapter);
65 case NETMAP_RX_UNLOCK:
66 EM_RX_UNLOCK(adapter);
76 lem_netmap_reg(struct ifnet *ifp, int onoff)
78 struct adapter *adapter = ifp->if_softc;
79 struct netmap_adapter *na = NA(ifp);
85 lem_disable_intr(adapter);
87 /* Tell the stack that the interface is no longer active */
88 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
90 #ifndef EM_LEGACY_IRQ // XXX do we need this ?
91 taskqueue_block(adapter->tq);
92 taskqueue_drain(adapter->tq, &adapter->rxtx_task);
93 taskqueue_drain(adapter->tq, &adapter->link_task);
94 #endif /* !EM_LEGCY_IRQ */
96 ifp->if_capenable |= IFCAP_NETMAP;
98 na->if_transmit = ifp->if_transmit;
99 ifp->if_transmit = netmap_start;
101 lem_init_locked(adapter);
102 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) == 0) {
108 /* return to non-netmap mode */
109 ifp->if_transmit = na->if_transmit;
110 ifp->if_capenable &= ~IFCAP_NETMAP;
111 lem_init_locked(adapter); /* also enable intr */
114 #ifndef EM_LEGACY_IRQ
115 taskqueue_unblock(adapter->tq); // XXX do we need this ?
116 #endif /* !EM_LEGCY_IRQ */
123 * Reconcile kernel and user view of the transmit ring.
126 lem_netmap_txsync(struct ifnet *ifp, u_int ring_nr, int do_lock)
128 struct adapter *adapter = ifp->if_softc;
129 struct netmap_adapter *na = NA(ifp);
130 struct netmap_kring *kring = &na->tx_rings[ring_nr];
131 struct netmap_ring *ring = kring->ring;
132 u_int j, k, l, n = 0, lim = kring->nkr_num_slots - 1;
134 /* generate an interrupt approximately every half ring */
135 int report_frequency = kring->nkr_num_slots >> 1;
137 /* take a copy of ring->cur now, and never read it again */
140 return netmap_ring_reinit(kring);
144 bus_dmamap_sync(adapter->txdma.dma_tag, adapter->txdma.dma_map,
145 BUS_DMASYNC_POSTREAD);
147 * Process new packets to send. j is the current index in the
148 * netmap ring, l is the corresponding index in the NIC ring.
151 if (j != k) { /* we have new packets to send */
152 l = netmap_idx_k2n(kring, j);
153 for (n = 0; j != k; n++) {
154 /* slot is the current slot in the netmap ring */
155 struct netmap_slot *slot = &ring->slot[j];
156 /* curr is the current slot in the nic ring */
157 struct e1000_tx_desc *curr = &adapter->tx_desc_base[l];
158 struct em_buffer *txbuf = &adapter->tx_buffer_area[l];
159 int flags = ((slot->flags & NS_REPORT) ||
160 j == 0 || j == report_frequency) ?
161 E1000_TXD_CMD_RS : 0;
163 void *addr = PNMB(slot, &paddr);
164 u_int len = slot->len;
166 if (addr == netmap_buffer_base || len > NETMAP_BUF_SIZE) {
168 EM_TX_UNLOCK(adapter);
169 return netmap_ring_reinit(kring);
172 slot->flags &= ~NS_REPORT;
173 if (slot->flags & NS_BUF_CHANGED) {
174 /* buffer has changed, reload map */
175 netmap_reload_map(adapter->txtag, txbuf->map, addr);
176 curr->buffer_addr = htole64(paddr);
177 slot->flags &= ~NS_BUF_CHANGED;
179 curr->upper.data = 0;
181 htole32( adapter->txd_cmd | len |
182 (E1000_TXD_CMD_EOP | flags) );
184 bus_dmamap_sync(adapter->txtag, txbuf->map,
185 BUS_DMASYNC_PREWRITE);
186 j = (j == lim) ? 0 : j + 1;
187 l = (l == lim) ? 0 : l + 1;
189 kring->nr_hwcur = k; /* the saved ring->cur */
190 kring->nr_hwavail -= n;
192 bus_dmamap_sync(adapter->txdma.dma_tag, adapter->txdma.dma_map,
193 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
195 E1000_WRITE_REG(&adapter->hw, E1000_TDT(0), l);
198 if (n == 0 || kring->nr_hwavail < 1) {
201 /* record completed transmissions using TDH */
202 l = E1000_READ_REG(&adapter->hw, E1000_TDH(0));
203 if (l >= kring->nkr_num_slots) { /* XXX can it happen ? */
205 l -= kring->nkr_num_slots;
207 delta = l - adapter->next_tx_to_clean;
209 /* some tx completed, increment hwavail. */
211 delta += kring->nkr_num_slots;
212 adapter->next_tx_to_clean = l;
213 kring->nr_hwavail += delta;
216 /* update avail to what the kernel knows */
217 ring->avail = kring->nr_hwavail;
220 EM_TX_UNLOCK(adapter);
226 * Reconcile kernel and user view of the receive ring.
229 lem_netmap_rxsync(struct ifnet *ifp, u_int ring_nr, int do_lock)
231 struct adapter *adapter = ifp->if_softc;
232 struct netmap_adapter *na = NA(ifp);
233 struct netmap_kring *kring = &na->rx_rings[ring_nr];
234 struct netmap_ring *ring = kring->ring;
235 int j, l, n, lim = kring->nkr_num_slots - 1;
236 int force_update = do_lock || kring->nr_kflags & NKR_PENDINTR;
237 u_int k = ring->cur, resvd = ring->reserved;
240 return netmap_ring_reinit(kring);
245 /* XXX check sync modes */
246 bus_dmamap_sync(adapter->rxdma.dma_tag, adapter->rxdma.dma_map,
247 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
250 * Import newly received packets into the netmap ring.
251 * j is an index in the netmap ring, l in the NIC ring.
253 l = adapter->next_rx_desc_to_check;
254 j = netmap_idx_n2k(kring, l);
255 if (netmap_no_pendintr || force_update) {
257 struct e1000_rx_desc *curr = &adapter->rx_desc_base[l];
258 uint32_t staterr = le32toh(curr->status);
261 if ((staterr & E1000_RXD_STAT_DD) == 0)
263 len = le16toh(curr->length) - 4; // CRC
265 D("bogus pkt size at %d", j);
268 ring->slot[j].len = len;
269 bus_dmamap_sync(adapter->rxtag,
270 adapter->rx_buffer_area[l].map,
271 BUS_DMASYNC_POSTREAD);
272 j = (j == lim) ? 0 : j + 1;
273 l = (l == lim) ? 0 : l + 1;
275 if (n) { /* update the state variables */
276 adapter->next_rx_desc_to_check = l;
277 kring->nr_hwavail += n;
279 kring->nr_kflags &= ~NKR_PENDINTR;
282 /* skip past packets that userspace has released */
283 j = kring->nr_hwcur; /* netmap ring index */
285 if (resvd + ring->avail >= lim + 1) {
286 D("XXX invalid reserve/avail %d %d", resvd, ring->avail);
287 ring->reserved = resvd = 0; // XXX panic...
289 k = (k >= resvd) ? k - resvd : k + lim + 1 - resvd;
291 if (j != k) { /* userspace has released some packets. */
292 l = netmap_idx_k2n(kring, j); /* NIC ring index */
293 for (n = 0; j != k; n++) {
294 struct netmap_slot *slot = &ring->slot[j];
295 struct e1000_rx_desc *curr = &adapter->rx_desc_base[l];
296 struct em_buffer *rxbuf = &adapter->rx_buffer_area[l];
298 void *addr = PNMB(slot, &paddr);
300 if (addr == netmap_buffer_base) { /* bad buf */
302 EM_RX_UNLOCK(adapter);
303 return netmap_ring_reinit(kring);
306 if (slot->flags & NS_BUF_CHANGED) {
307 /* buffer has changed, reload map */
308 netmap_reload_map(adapter->rxtag, rxbuf->map, addr);
309 curr->buffer_addr = htole64(paddr);
310 slot->flags &= ~NS_BUF_CHANGED;
314 bus_dmamap_sync(adapter->rxtag, rxbuf->map,
315 BUS_DMASYNC_PREREAD);
317 j = (j == lim) ? 0 : j + 1;
318 l = (l == lim) ? 0 : l + 1;
320 kring->nr_hwavail -= n;
322 bus_dmamap_sync(adapter->rxdma.dma_tag, adapter->rxdma.dma_map,
323 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
325 * IMPORTANT: we must leave one free slot in the ring,
326 * so move l back by one unit
328 l = (l == 0) ? lim : l - 1;
329 E1000_WRITE_REG(&adapter->hw, E1000_RDT(0), l);
331 /* tell userspace that there are new packets */
332 ring->avail = kring->nr_hwavail - resvd;
334 EM_RX_UNLOCK(adapter);
340 lem_netmap_attach(struct adapter *adapter)
342 struct netmap_adapter na;
344 bzero(&na, sizeof(na));
346 na.ifp = adapter->ifp;
347 na.separate_locks = 1;
348 na.num_tx_desc = adapter->num_tx_desc;
349 na.num_rx_desc = adapter->num_rx_desc;
350 na.nm_txsync = lem_netmap_txsync;
351 na.nm_rxsync = lem_netmap_rxsync;
352 na.nm_lock = lem_netmap_lock_wrapper;
353 na.nm_register = lem_netmap_reg;
354 netmap_attach(&na, 1);