2 * Copyright (C) 2015, 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: ixl
32 * netmap support for a network driver.
33 * This file contains code but only static or inline functions used
34 * by a single driver. To avoid replication of code we just #include
35 * it near the beginning of the standard driver.
36 * For ixl the file is imported in two places, hence the conditional at the
40 #include <net/netmap.h>
41 #include <sys/selinfo.h>
44 * Some drivers may need the following headers. Others
45 * already include them by default
51 #include <dev/netmap/netmap_kern.h>
53 int ixl_netmap_txsync(struct netmap_kring *kring, int flags);
54 int ixl_netmap_rxsync(struct netmap_kring *kring, int flags);
56 extern int ixl_rx_miss, ixl_rx_miss_bufs, ixl_crcstrip;
58 #ifdef NETMAP_IXL_MAIN
60 * device-specific sysctl variables:
62 * ixl_crcstrip: 0: keep CRC in rx frames (default), 1: strip it.
63 * During regular operations the CRC is stripped, but on some
64 * hardware reception of frames not multiple of 64 is slower,
65 * so using crcstrip=0 helps in benchmarks.
67 * ixl_rx_miss, ixl_rx_miss_bufs:
68 * count packets that might be missed due to lost interrupts.
70 SYSCTL_DECL(_dev_netmap);
72 * The xl driver by default strips CRCs and we do not override it.
74 int ixl_rx_miss, ixl_rx_miss_bufs, ixl_crcstrip = 1;
76 SYSCTL_INT(_dev_netmap, OID_AUTO, ixl_crcstrip,
77 CTLFLAG_RW, &ixl_crcstrip, 1, "strip CRC on rx frames");
79 SYSCTL_INT(_dev_netmap, OID_AUTO, ixl_rx_miss,
80 CTLFLAG_RW, &ixl_rx_miss, 0, "potentially missed rx intr");
81 SYSCTL_INT(_dev_netmap, OID_AUTO, ixl_rx_miss_bufs,
82 CTLFLAG_RW, &ixl_rx_miss_bufs, 0, "potentially missed rx intr bufs");
86 * Register/unregister. We are already under netmap lock.
87 * Only called on the first register or the last unregister.
90 ixl_netmap_reg(struct netmap_adapter *na, int onoff)
92 struct ifnet *ifp = na->ifp;
93 struct ixl_vsi *vsi = ifp->if_softc;
94 struct ixl_pf *pf = (struct ixl_pf *)vsi->back;
97 ixl_disable_intr(vsi);
99 /* Tell the stack that the interface is no longer active */
100 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
102 //set_crcstrip(&adapter->hw, onoff);
103 /* enable or disable flags and callbacks in na and ifp */
105 nm_set_native_flags(na);
107 nm_clear_native_flags(na);
109 ixl_init_locked(pf); /* also enables intr */
110 //set_crcstrip(&adapter->hw, onoff); // XXX why twice ?
112 return (ifp->if_drv_flags & IFF_DRV_RUNNING ? 0 : 1);
117 * The attach routine, called near the end of ixl_attach(),
118 * fills the parameters for netmap_attach() and calls it.
119 * It cannot fail, in the worst case (such as no memory)
120 * netmap mode will be disabled and the driver will only
121 * operate in standard mode.
124 ixl_netmap_attach(struct ixl_vsi *vsi)
126 struct netmap_adapter na;
128 bzero(&na, sizeof(na));
131 na.na_flags = NAF_BDG_MAYSLEEP;
132 // XXX check that queues is set.
133 printf("queues is %p\n", vsi->queues);
135 na.num_tx_desc = vsi->queues[0].num_desc;
136 na.num_rx_desc = vsi->queues[0].num_desc;
138 na.nm_txsync = ixl_netmap_txsync;
139 na.nm_rxsync = ixl_netmap_rxsync;
140 na.nm_register = ixl_netmap_reg;
141 na.num_tx_rings = na.num_rx_rings = vsi->num_queues;
146 #else /* !NETMAP_IXL_MAIN, code for ixl_txrx.c */
149 * Reconcile kernel and user view of the transmit ring.
151 * All information is in the kring.
152 * Userspace wants to send packets up to the one before kring->rhead,
153 * kernel knows kring->nr_hwcur is the first unsent packet.
155 * Here we push packets out (as many as possible), and possibly
156 * reclaim buffers from previously completed transmission.
158 * The caller (netmap) guarantees that there is only one instance
159 * running at any time. Any interference with other driver
160 * methods should be handled by the individual drivers.
163 ixl_netmap_txsync(struct netmap_kring *kring, int flags)
165 struct netmap_adapter *na = kring->na;
166 struct ifnet *ifp = na->ifp;
167 struct netmap_ring *ring = kring->ring;
168 u_int nm_i; /* index into the netmap ring */
169 u_int nic_i; /* index into the NIC ring */
171 u_int const lim = kring->nkr_num_slots - 1;
172 u_int const head = kring->rhead;
174 * interrupts on every tx packet are expensive so request
175 * them every half ring, or where NS_REPORT is set
177 u_int report_frequency = kring->nkr_num_slots >> 1;
179 /* device-specific */
180 struct ixl_vsi *vsi = ifp->if_softc;
181 struct ixl_queue *que = &vsi->queues[kring->ring_id];
182 struct tx_ring *txr = &que->txr;
184 bus_dmamap_sync(txr->dma.tag, txr->dma.map,
185 BUS_DMASYNC_POSTREAD);
188 * First part: process new packets to send.
189 * nm_i is the current index in the netmap ring,
190 * nic_i is the corresponding index in the NIC ring.
192 * If we have packets to send (nm_i != head)
193 * iterate over the netmap ring, fetch length and update
194 * the corresponding slot in the NIC ring. Some drivers also
195 * need to update the buffer's physical address in the NIC slot
196 * even NS_BUF_CHANGED is not set (PNMB computes the addresses).
198 * The netmap_reload_map() calls is especially expensive,
199 * even when (as in this case) the tag is 0, so do only
200 * when the buffer has actually changed.
202 * If possible do not set the report/intr bit on all slots,
203 * but only a few times per ring or when NS_REPORT is set.
205 * Finally, on 10G and faster drivers, it might be useful
206 * to prefetch the next slot and txr entry.
209 nm_i = kring->nr_hwcur;
210 if (nm_i != head) { /* we have new packets to send */
211 nic_i = netmap_idx_k2n(kring, nm_i);
213 __builtin_prefetch(&ring->slot[nm_i]);
214 __builtin_prefetch(&txr->buffers[nic_i]);
216 for (n = 0; nm_i != head; n++) {
217 struct netmap_slot *slot = &ring->slot[nm_i];
218 u_int len = slot->len;
220 void *addr = PNMB(na, slot, &paddr);
222 /* device-specific */
223 struct i40e_tx_desc *curr = &txr->base[nic_i];
224 struct ixl_tx_buf *txbuf = &txr->buffers[nic_i];
225 u64 flags = (slot->flags & NS_REPORT ||
226 nic_i == 0 || nic_i == report_frequency) ?
227 ((u64)I40E_TX_DESC_CMD_RS << I40E_TXD_QW1_CMD_SHIFT) : 0;
229 /* prefetch for next round */
230 __builtin_prefetch(&ring->slot[nm_i + 1]);
231 __builtin_prefetch(&txr->buffers[nic_i + 1]);
233 NM_CHECK_ADDR_LEN(na, addr, len);
235 if (slot->flags & NS_BUF_CHANGED) {
236 /* buffer has changed, reload map */
237 netmap_reload_map(na, txr->dma.tag, txbuf->map, addr);
239 slot->flags &= ~(NS_REPORT | NS_BUF_CHANGED);
241 /* Fill the slot in the NIC ring. */
242 curr->buffer_addr = htole64(paddr);
243 curr->cmd_type_offset_bsz = htole64(
244 ((u64)len << I40E_TXD_QW1_TX_BUF_SZ_SHIFT) |
246 ((u64)I40E_TX_DESC_CMD_EOP << I40E_TXD_QW1_CMD_SHIFT)
249 /* make sure changes to the buffer are synced */
250 bus_dmamap_sync(txr->dma.tag, txbuf->map,
251 BUS_DMASYNC_PREWRITE);
253 nm_i = nm_next(nm_i, lim);
254 nic_i = nm_next(nic_i, lim);
256 kring->nr_hwcur = head;
258 /* synchronize the NIC ring */
259 bus_dmamap_sync(txr->dma.tag, txr->dma.map,
260 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
262 /* (re)start the tx unit up to slot nic_i (excluded) */
263 wr32(vsi->hw, txr->tail, nic_i);
267 * Second part: reclaim buffers for completed transmissions.
269 nic_i = LE32_TO_CPU(*(volatile __le32 *)&txr->base[que->num_desc]);
270 if (nic_i != txr->next_to_clean) {
271 /* some tx completed, increment avail */
272 txr->next_to_clean = nic_i;
273 kring->nr_hwtail = nm_prev(netmap_idx_n2k(kring, nic_i), lim);
281 * Reconcile kernel and user view of the receive ring.
282 * Same as for the txsync, this routine must be efficient.
283 * The caller guarantees a single invocations, but races against
284 * the rest of the driver should be handled here.
286 * On call, kring->rhead is the first packet that userspace wants
287 * to keep, and kring->rcur is the wakeup point.
288 * The kernel has previously reported packets up to kring->rtail.
290 * If (flags & NAF_FORCE_READ) also check for incoming packets irrespective
291 * of whether or not we received an interrupt.
294 ixl_netmap_rxsync(struct netmap_kring *kring, int flags)
296 struct netmap_adapter *na = kring->na;
297 struct ifnet *ifp = na->ifp;
298 struct netmap_ring *ring = kring->ring;
299 u_int nm_i; /* index into the netmap ring */
300 u_int nic_i; /* index into the NIC ring */
302 u_int const lim = kring->nkr_num_slots - 1;
303 u_int const head = kring->rhead;
304 int force_update = (flags & NAF_FORCE_READ) || kring->nr_kflags & NKR_PENDINTR;
306 /* device-specific */
307 struct ixl_vsi *vsi = ifp->if_softc;
308 struct ixl_queue *que = &vsi->queues[kring->ring_id];
309 struct rx_ring *rxr = &que->rxr;
312 return netmap_ring_reinit(kring);
314 /* XXX check sync modes */
315 bus_dmamap_sync(rxr->dma.tag, rxr->dma.map,
316 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
319 * First part: import newly received packets.
321 * nm_i is the index of the next free slot in the netmap ring,
322 * nic_i is the index of the next received packet in the NIC ring,
323 * and they may differ in case if_init() has been called while
324 * in netmap mode. For the receive ring we have
326 * nic_i = rxr->next_check;
327 * nm_i = kring->nr_hwtail (previous)
329 * nm_i == (nic_i + kring->nkr_hwofs) % ring_size
331 * rxr->next_check is set to 0 on a ring reinit
333 if (netmap_no_pendintr || force_update) {
334 int crclen = ixl_crcstrip ? 0 : 4;
335 uint16_t slot_flags = kring->nkr_slot_flags;
337 nic_i = rxr->next_check; // or also k2n(kring->nr_hwtail)
338 nm_i = netmap_idx_n2k(kring, nic_i);
341 union i40e_32byte_rx_desc *curr = &rxr->base[nic_i];
342 uint64_t qword = le64toh(curr->wb.qword1.status_error_len);
343 uint32_t staterr = (qword & I40E_RXD_QW1_STATUS_MASK)
344 >> I40E_RXD_QW1_STATUS_SHIFT;
346 if ((staterr & (1<<I40E_RX_DESC_STATUS_DD_SHIFT)) == 0)
348 ring->slot[nm_i].len = ((qword & I40E_RXD_QW1_LENGTH_PBUF_MASK)
349 >> I40E_RXD_QW1_LENGTH_PBUF_SHIFT) - crclen;
350 ring->slot[nm_i].flags = slot_flags;
351 bus_dmamap_sync(rxr->ptag,
352 rxr->buffers[nic_i].pmap, BUS_DMASYNC_POSTREAD);
353 nm_i = nm_next(nm_i, lim);
354 nic_i = nm_next(nic_i, lim);
356 if (n) { /* update the state variables */
357 if (netmap_no_pendintr && !force_update) {
360 ixl_rx_miss_bufs += n;
362 rxr->next_check = nic_i;
363 kring->nr_hwtail = nm_i;
365 kring->nr_kflags &= ~NKR_PENDINTR;
369 * Second part: skip past packets that userspace has released.
370 * (kring->nr_hwcur to head excluded),
371 * and make the buffers available for reception.
372 * As usual nm_i is the index in the netmap ring,
373 * nic_i is the index in the NIC ring, and
374 * nm_i == (nic_i + kring->nkr_hwofs) % ring_size
376 nm_i = kring->nr_hwcur;
378 nic_i = netmap_idx_k2n(kring, nm_i);
379 for (n = 0; nm_i != head; n++) {
380 struct netmap_slot *slot = &ring->slot[nm_i];
382 void *addr = PNMB(na, slot, &paddr);
384 union i40e_32byte_rx_desc *curr = &rxr->base[nic_i];
385 struct ixl_rx_buf *rxbuf = &rxr->buffers[nic_i];
387 if (addr == NETMAP_BUF_BASE(na)) /* bad buf */
390 if (slot->flags & NS_BUF_CHANGED) {
391 /* buffer has changed, reload map */
392 netmap_reload_map(na, rxr->ptag, rxbuf->pmap, addr);
393 slot->flags &= ~NS_BUF_CHANGED;
395 curr->read.pkt_addr = htole64(paddr);
396 curr->read.hdr_addr = 0; // XXX needed
397 bus_dmamap_sync(rxr->ptag, rxbuf->pmap,
398 BUS_DMASYNC_PREREAD);
399 nm_i = nm_next(nm_i, lim);
400 nic_i = nm_next(nic_i, lim);
402 kring->nr_hwcur = head;
404 bus_dmamap_sync(rxr->dma.tag, rxr->dma.map,
405 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
407 * IMPORTANT: we must leave one free slot in the ring,
408 * so move nic_i back by one unit
410 nic_i = nm_prev(nic_i, lim);
411 wr32(vsi->hw, rxr->tail, nic_i);
417 return netmap_ring_reinit(kring);
420 #endif /* !NETMAP_IXL_MAIN */