2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2011-2014 Matteo Landi, Luigi Rizzo. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * netmap support for: lem
34 * For details on netmap support please see ixgbe_netmap.h
38 #include <net/netmap.h>
39 #include <sys/selinfo.h>
40 #include <dev/netmap/netmap_kern.h>
43 * Register/unregister. We are already under netmap lock.
46 lem_netmap_reg(struct netmap_adapter *na, int onoff)
48 struct ifnet *ifp = na->ifp;
49 struct adapter *adapter = ifp->if_softc;
51 EM_CORE_LOCK(adapter);
53 lem_disable_intr(adapter);
55 /* Tell the stack that the interface is no longer active */
56 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
58 #ifndef EM_LEGACY_IRQ // XXX do we need this ?
59 taskqueue_block(adapter->tq);
60 taskqueue_drain(adapter->tq, &adapter->rxtx_task);
61 taskqueue_drain(adapter->tq, &adapter->link_task);
62 #endif /* !EM_LEGCY_IRQ */
64 /* enable or disable flags and callbacks in na and ifp */
66 nm_set_native_flags(na);
68 nm_clear_native_flags(na);
70 lem_init_locked(adapter); /* also enable intr */
73 taskqueue_unblock(adapter->tq); // XXX do we need this ?
74 #endif /* !EM_LEGCY_IRQ */
76 EM_CORE_UNLOCK(adapter);
78 return (ifp->if_drv_flags & IFF_DRV_RUNNING ? 0 : 1);
83 lem_netmap_intr(struct netmap_adapter *na, int onoff)
85 struct ifnet *ifp = na->ifp;
86 struct adapter *adapter = ifp->if_softc;
88 EM_CORE_LOCK(adapter);
90 lem_enable_intr(adapter);
92 lem_disable_intr(adapter);
94 EM_CORE_UNLOCK(adapter);
99 * Reconcile kernel and user view of the transmit ring.
102 lem_netmap_txsync(struct netmap_kring *kring, int flags)
104 struct netmap_adapter *na = kring->na;
105 struct ifnet *ifp = na->ifp;
106 struct netmap_ring *ring = kring->ring;
107 u_int nm_i; /* index into the netmap ring */
108 u_int nic_i; /* index into the NIC ring */
109 u_int const lim = kring->nkr_num_slots - 1;
110 u_int const head = kring->rhead;
111 /* generate an interrupt approximately every half ring */
112 u_int report_frequency = kring->nkr_num_slots >> 1;
114 /* device-specific */
115 struct adapter *adapter = ifp->if_softc;
117 bus_dmamap_sync(adapter->txdma.dma_tag, adapter->txdma.dma_map,
118 BUS_DMASYNC_POSTREAD);
121 * First part: process new packets to send.
124 nm_i = kring->nr_hwcur;
125 if (nm_i != head) { /* we have new packets to send */
126 nic_i = netmap_idx_k2n(kring, nm_i);
127 while (nm_i != head) {
128 struct netmap_slot *slot = &ring->slot[nm_i];
129 u_int len = slot->len;
131 void *addr = PNMB(na, slot, &paddr);
133 /* device-specific */
134 struct e1000_tx_desc *curr = &adapter->tx_desc_base[nic_i];
135 struct em_buffer *txbuf = &adapter->tx_buffer_area[nic_i];
136 int flags = (slot->flags & NS_REPORT ||
137 nic_i == 0 || nic_i == report_frequency) ?
138 E1000_TXD_CMD_RS : 0;
140 NM_CHECK_ADDR_LEN(na, addr, len);
142 if (slot->flags & NS_BUF_CHANGED) {
143 /* buffer has changed, reload map */
144 curr->buffer_addr = htole64(paddr);
145 netmap_reload_map(na, adapter->txtag, txbuf->map, addr);
147 slot->flags &= ~(NS_REPORT | NS_BUF_CHANGED);
149 /* Fill the slot in the NIC ring. */
150 curr->upper.data = 0;
151 curr->lower.data = htole32(adapter->txd_cmd | len |
152 (E1000_TXD_CMD_EOP | flags) );
153 bus_dmamap_sync(adapter->txtag, txbuf->map,
154 BUS_DMASYNC_PREWRITE);
156 nm_i = nm_next(nm_i, lim);
157 nic_i = nm_next(nic_i, lim);
158 // XXX might try an early kick
160 kring->nr_hwcur = head;
162 /* synchronize the NIC ring */
163 bus_dmamap_sync(adapter->txdma.dma_tag, adapter->txdma.dma_map,
164 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
166 /* (re)start the tx unit up to slot nic_i (excluded) */
167 E1000_WRITE_REG(&adapter->hw, E1000_TDT(0), nic_i);
171 * Second part: reclaim buffers for completed transmissions.
173 if (ticks != kring->last_reclaim || flags & NAF_FORCE_RECLAIM || nm_kr_txempty(kring)) {
174 kring->last_reclaim = ticks;
175 /* record completed transmissions using TDH */
176 nic_i = E1000_READ_REG(&adapter->hw, E1000_TDH(0));
177 if (nic_i >= kring->nkr_num_slots) { /* XXX can it happen ? */
178 D("TDH wrap %d", nic_i);
179 nic_i -= kring->nkr_num_slots;
181 adapter->next_tx_to_clean = nic_i;
182 kring->nr_hwtail = nm_prev(netmap_idx_n2k(kring, nic_i), lim);
190 * Reconcile kernel and user view of the receive ring.
193 lem_netmap_rxsync(struct netmap_kring *kring, int flags)
195 struct netmap_adapter *na = kring->na;
196 struct ifnet *ifp = na->ifp;
197 struct netmap_ring *ring = kring->ring;
198 u_int nm_i; /* index into the netmap ring */
199 u_int nic_i; /* index into the NIC ring */
201 u_int const lim = kring->nkr_num_slots - 1;
202 u_int const head = kring->rhead;
203 int force_update = (flags & NAF_FORCE_READ) || kring->nr_kflags & NKR_PENDINTR;
205 /* device-specific */
206 struct adapter *adapter = ifp->if_softc;
209 return netmap_ring_reinit(kring);
211 /* XXX check sync modes */
212 bus_dmamap_sync(adapter->rxdma.dma_tag, adapter->rxdma.dma_map,
213 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
216 * First part: import newly received packets.
218 if (netmap_no_pendintr || force_update) {
219 nic_i = adapter->next_rx_desc_to_check;
220 nm_i = netmap_idx_n2k(kring, nic_i);
223 struct e1000_rx_desc *curr = &adapter->rx_desc_base[nic_i];
224 uint32_t staterr = le32toh(curr->status);
227 if ((staterr & E1000_RXD_STAT_DD) == 0)
229 len = le16toh(curr->length) - 4; // CRC
231 RD(5, "bogus pkt (%d) size %d nic idx %d", n, len, nic_i);
234 ring->slot[nm_i].len = len;
235 ring->slot[nm_i].flags = 0;
236 bus_dmamap_sync(adapter->rxtag,
237 adapter->rx_buffer_area[nic_i].map,
238 BUS_DMASYNC_POSTREAD);
239 nm_i = nm_next(nm_i, lim);
240 nic_i = nm_next(nic_i, lim);
242 if (n) { /* update the state variables */
243 ND("%d new packets at nic %d nm %d tail %d",
245 adapter->next_rx_desc_to_check,
246 netmap_idx_n2k(kring, adapter->next_rx_desc_to_check),
248 adapter->next_rx_desc_to_check = nic_i;
249 // if_inc_counter(ifp, IFCOUNTER_IPACKETS, n);
250 kring->nr_hwtail = nm_i;
252 kring->nr_kflags &= ~NKR_PENDINTR;
256 * Second part: skip past packets that userspace has released.
258 nm_i = kring->nr_hwcur;
260 nic_i = netmap_idx_k2n(kring, nm_i);
261 for (n = 0; nm_i != head; n++) {
262 struct netmap_slot *slot = &ring->slot[nm_i];
264 void *addr = PNMB(na, slot, &paddr);
266 struct e1000_rx_desc *curr = &adapter->rx_desc_base[nic_i];
267 struct em_buffer *rxbuf = &adapter->rx_buffer_area[nic_i];
269 if (addr == NETMAP_BUF_BASE(na)) /* bad buf */
272 if (slot->flags & NS_BUF_CHANGED) {
273 /* buffer has changed, reload map */
274 curr->buffer_addr = htole64(paddr);
275 netmap_reload_map(na, adapter->rxtag, rxbuf->map, addr);
276 slot->flags &= ~NS_BUF_CHANGED;
279 bus_dmamap_sync(adapter->rxtag, rxbuf->map,
280 BUS_DMASYNC_PREREAD);
281 nm_i = nm_next(nm_i, lim);
282 nic_i = nm_next(nic_i, lim);
284 kring->nr_hwcur = head;
285 bus_dmamap_sync(adapter->rxdma.dma_tag, adapter->rxdma.dma_map,
286 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
288 * IMPORTANT: we must leave one free slot in the ring,
289 * so move nic_i back by one unit
291 nic_i = nm_prev(nic_i, lim);
292 E1000_WRITE_REG(&adapter->hw, E1000_RDT(0), nic_i);
298 return netmap_ring_reinit(kring);
303 lem_netmap_attach(struct adapter *adapter)
305 struct netmap_adapter na;
307 bzero(&na, sizeof(na));
309 na.ifp = adapter->ifp;
310 na.na_flags = NAF_BDG_MAYSLEEP;
311 na.num_tx_desc = adapter->num_tx_desc;
312 na.num_rx_desc = adapter->num_rx_desc;
313 na.nm_txsync = lem_netmap_txsync;
314 na.nm_rxsync = lem_netmap_rxsync;
315 na.nm_register = lem_netmap_reg;
316 na.num_tx_rings = na.num_rx_rings = 1;
317 na.nm_intr = lem_netmap_intr;