]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/netmap/if_ixl_netmap.h
Merge ACPICA 20180313.
[FreeBSD/FreeBSD.git] / sys / dev / netmap / if_ixl_netmap.h
1 /*
2  * Copyright (C) 2015, 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  *
29  * netmap support for: ixl
30  *
31  * derived from ixgbe
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
37  * beginning.
38  */
39
40 #include <net/netmap.h>
41 #include <sys/selinfo.h>
42
43 /*
44  * Some drivers may need the following headers. Others
45  * already include them by default
46
47 #include <vm/vm.h>
48 #include <vm/pmap.h>
49
50  */
51 #include <dev/netmap/netmap_kern.h>
52
53 int ixl_netmap_txsync(struct netmap_kring *kring, int flags);
54 int ixl_netmap_rxsync(struct netmap_kring *kring, int flags);
55
56 extern int ixl_rx_miss, ixl_rx_miss_bufs, ixl_crcstrip;
57
58 #ifdef NETMAP_IXL_MAIN
59 /*
60  * device-specific sysctl variables:
61  *
62  * ixl_crcstrip: 0: NIC keeps CRC in rx frames, 1: NIC strips it (default).
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.
66  *
67  * ixl_rx_miss, ixl_rx_miss_bufs:
68  *      count packets that might be missed due to lost interrupts.
69  */
70 SYSCTL_DECL(_dev_netmap);
71 /*
72  * The xl driver by default strips CRCs and we do not override it.
73  */
74 #if 0
75 SYSCTL_INT(_dev_netmap, OID_AUTO, ixl_crcstrip,
76     CTLFLAG_RW, &ixl_crcstrip, 1, "NIC strips CRC on rx frames");
77 #endif
78 SYSCTL_INT(_dev_netmap, OID_AUTO, ixl_rx_miss,
79     CTLFLAG_RW, &ixl_rx_miss, 0, "potentially missed rx intr");
80 SYSCTL_INT(_dev_netmap, OID_AUTO, ixl_rx_miss_bufs,
81     CTLFLAG_RW, &ixl_rx_miss_bufs, 0, "potentially missed rx intr bufs");
82
83
84 /*
85  * Register/unregister. We are already under netmap lock.
86  * Only called on the first register or the last unregister.
87  */
88 static int
89 ixl_netmap_reg(struct netmap_adapter *na, int onoff)
90 {
91         struct ifnet *ifp = na->ifp;
92         struct ixl_vsi  *vsi = ifp->if_softc;
93         struct ixl_pf   *pf = (struct ixl_pf *)vsi->back;
94
95         IXL_PF_LOCK(pf);
96         ixl_disable_intr(vsi);
97
98         /* Tell the stack that the interface is no longer active */
99         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
100
101         //set_crcstrip(&adapter->hw, onoff);
102         /* enable or disable flags and callbacks in na and ifp */
103         if (onoff) {
104                 nm_set_native_flags(na);
105         } else {
106                 nm_clear_native_flags(na);
107         }
108         ixl_init_locked(pf);    /* also enables intr */
109         //set_crcstrip(&adapter->hw, onoff); // XXX why twice ?
110         IXL_PF_UNLOCK(pf);
111         return (ifp->if_drv_flags & IFF_DRV_RUNNING ? 0 : 1);
112 }
113
114
115 /*
116  * The attach routine, called near the end of ixl_attach(),
117  * fills the parameters for netmap_attach() and calls it.
118  * It cannot fail, in the worst case (such as no memory)
119  * netmap mode will be disabled and the driver will only
120  * operate in standard mode.
121  */
122 static void
123 ixl_netmap_attach(struct ixl_vsi *vsi)
124 {
125         struct netmap_adapter na;
126
127         bzero(&na, sizeof(na));
128
129         na.ifp = vsi->ifp;
130         na.na_flags = NAF_BDG_MAYSLEEP;
131         // XXX check that queues is set.
132         nm_prinf("queues is %p\n", vsi->queues);
133         if (vsi->queues) {
134                 na.num_tx_desc = vsi->queues[0].num_desc;
135                 na.num_rx_desc = vsi->queues[0].num_desc;
136         }
137         na.nm_txsync = ixl_netmap_txsync;
138         na.nm_rxsync = ixl_netmap_rxsync;
139         na.nm_register = ixl_netmap_reg;
140         na.num_tx_rings = na.num_rx_rings = vsi->num_queues;
141         netmap_attach(&na);
142 }
143
144
145 #else /* !NETMAP_IXL_MAIN, code for ixl_txrx.c */
146
147 /*
148  * Reconcile kernel and user view of the transmit ring.
149  *
150  * All information is in the kring.
151  * Userspace wants to send packets up to the one before kring->rhead,
152  * kernel knows kring->nr_hwcur is the first unsent packet.
153  *
154  * Here we push packets out (as many as possible), and possibly
155  * reclaim buffers from previously completed transmission.
156  *
157  * The caller (netmap) guarantees that there is only one instance
158  * running at any time. Any interference with other driver
159  * methods should be handled by the individual drivers.
160  */
161 int
162 ixl_netmap_txsync(struct netmap_kring *kring, int flags)
163 {
164         struct netmap_adapter *na = kring->na;
165         struct ifnet *ifp = na->ifp;
166         struct netmap_ring *ring = kring->ring;
167         u_int nm_i;     /* index into the netmap ring */
168         u_int nic_i;    /* index into the NIC ring */
169         u_int n;
170         u_int const lim = kring->nkr_num_slots - 1;
171         u_int const head = kring->rhead;
172         /*
173          * interrupts on every tx packet are expensive so request
174          * them every half ring, or where NS_REPORT is set
175          */
176         u_int report_frequency = kring->nkr_num_slots >> 1;
177
178         /* device-specific */
179         struct ixl_vsi *vsi = ifp->if_softc;
180         struct ixl_queue *que = &vsi->queues[kring->ring_id];
181         struct tx_ring *txr = &que->txr;
182
183         bus_dmamap_sync(txr->dma.tag, txr->dma.map,
184                         BUS_DMASYNC_POSTREAD);
185
186         /*
187          * First part: process new packets to send.
188          * nm_i is the current index in the netmap ring,
189          * nic_i is the corresponding index in the NIC ring.
190          *
191          * If we have packets to send (nm_i != head)
192          * iterate over the netmap ring, fetch length and update
193          * the corresponding slot in the NIC ring. Some drivers also
194          * need to update the buffer's physical address in the NIC slot
195          * even NS_BUF_CHANGED is not set (PNMB computes the addresses).
196          *
197          * The netmap_reload_map() calls is especially expensive,
198          * even when (as in this case) the tag is 0, so do only
199          * when the buffer has actually changed.
200          *
201          * If possible do not set the report/intr bit on all slots,
202          * but only a few times per ring or when NS_REPORT is set.
203          *
204          * Finally, on 10G and faster drivers, it might be useful
205          * to prefetch the next slot and txr entry.
206          */
207
208         nm_i = kring->nr_hwcur;
209         if (nm_i != head) {     /* we have new packets to send */
210                 nic_i = netmap_idx_k2n(kring, nm_i);
211
212                 __builtin_prefetch(&ring->slot[nm_i]);
213                 __builtin_prefetch(&txr->buffers[nic_i]);
214
215                 for (n = 0; nm_i != head; n++) {
216                         struct netmap_slot *slot = &ring->slot[nm_i];
217                         u_int len = slot->len;
218                         uint64_t paddr;
219                         void *addr = PNMB(na, slot, &paddr);
220
221                         /* device-specific */
222                         struct i40e_tx_desc *curr = &txr->base[nic_i];
223                         struct ixl_tx_buf *txbuf = &txr->buffers[nic_i];
224                         u64 flags = (slot->flags & NS_REPORT ||
225                                 nic_i == 0 || nic_i == report_frequency) ?
226                                 ((u64)I40E_TX_DESC_CMD_RS << I40E_TXD_QW1_CMD_SHIFT) : 0;
227
228                         /* prefetch for next round */
229                         __builtin_prefetch(&ring->slot[nm_i + 1]);
230                         __builtin_prefetch(&txr->buffers[nic_i + 1]);
231
232                         NM_CHECK_ADDR_LEN(na, addr, len);
233
234                         if (slot->flags & NS_BUF_CHANGED) {
235                                 /* buffer has changed, reload map */
236                                 netmap_reload_map(na, txr->dma.tag, txbuf->map, addr);
237                         }
238                         slot->flags &= ~(NS_REPORT | NS_BUF_CHANGED);
239
240                         /* Fill the slot in the NIC ring. */
241                         curr->buffer_addr = htole64(paddr);
242                         curr->cmd_type_offset_bsz = htole64(
243                             ((u64)len << I40E_TXD_QW1_TX_BUF_SZ_SHIFT) |
244                             flags |
245                             ((u64)I40E_TX_DESC_CMD_EOP << I40E_TXD_QW1_CMD_SHIFT)
246                           ); // XXX more ?
247
248                         /* make sure changes to the buffer are synced */
249                         bus_dmamap_sync(txr->dma.tag, txbuf->map,
250                                 BUS_DMASYNC_PREWRITE);
251
252                         nm_i = nm_next(nm_i, lim);
253                         nic_i = nm_next(nic_i, lim);
254                 }
255                 kring->nr_hwcur = head;
256
257                 /* synchronize the NIC ring */
258                 bus_dmamap_sync(txr->dma.tag, txr->dma.map,
259                         BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
260
261                 /* (re)start the tx unit up to slot nic_i (excluded) */
262                 wr32(vsi->hw, txr->tail, nic_i);
263         }
264
265         /*
266          * Second part: reclaim buffers for completed transmissions.
267          */
268         nic_i = LE32_TO_CPU(*(volatile __le32 *)&txr->base[que->num_desc]);
269         if (nic_i != txr->next_to_clean) {
270                 /* some tx completed, increment avail */
271                 txr->next_to_clean = nic_i;
272                 kring->nr_hwtail = nm_prev(netmap_idx_n2k(kring, nic_i), lim);
273         }
274
275         return 0;
276 }
277
278
279 /*
280  * Reconcile kernel and user view of the receive ring.
281  * Same as for the txsync, this routine must be efficient.
282  * The caller guarantees a single invocations, but races against
283  * the rest of the driver should be handled here.
284  *
285  * On call, kring->rhead is the first packet that userspace wants
286  * to keep, and kring->rcur is the wakeup point.
287  * The kernel has previously reported packets up to kring->rtail.
288  *
289  * If (flags & NAF_FORCE_READ) also check for incoming packets irrespective
290  * of whether or not we received an interrupt.
291  */
292 int
293 ixl_netmap_rxsync(struct netmap_kring *kring, int flags)
294 {
295         struct netmap_adapter *na = kring->na;
296         struct ifnet *ifp = na->ifp;
297         struct netmap_ring *ring = kring->ring;
298         u_int nm_i;     /* index into the netmap ring */
299         u_int nic_i;    /* index into the NIC ring */
300         u_int n;
301         u_int const lim = kring->nkr_num_slots - 1;
302         u_int const head = kring->rhead;
303         int force_update = (flags & NAF_FORCE_READ) || kring->nr_kflags & NKR_PENDINTR;
304
305         /* device-specific */
306         struct ixl_vsi *vsi = ifp->if_softc;
307         struct ixl_queue *que = &vsi->queues[kring->ring_id];
308         struct rx_ring *rxr = &que->rxr;
309
310         if (head > lim)
311                 return netmap_ring_reinit(kring);
312
313         /* XXX check sync modes */
314         bus_dmamap_sync(rxr->dma.tag, rxr->dma.map,
315                         BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
316
317         /*
318          * First part: import newly received packets.
319          *
320          * nm_i is the index of the next free slot in the netmap ring,
321          * nic_i is the index of the next received packet in the NIC ring,
322          * and they may differ in case if_init() has been called while
323          * in netmap mode. For the receive ring we have
324          *
325          *      nic_i = rxr->next_check;
326          *      nm_i = kring->nr_hwtail (previous)
327          * and
328          *      nm_i == (nic_i + kring->nkr_hwofs) % ring_size
329          *
330          * rxr->next_check is set to 0 on a ring reinit
331          */
332         if (netmap_no_pendintr || force_update) {
333                 int crclen = ixl_crcstrip ? 0 : 4;
334                 uint16_t slot_flags = kring->nkr_slot_flags;
335
336                 nic_i = rxr->next_check; // or also k2n(kring->nr_hwtail)
337                 nm_i = netmap_idx_n2k(kring, nic_i);
338
339                 for (n = 0; ; n++) {
340                         union i40e_32byte_rx_desc *curr = &rxr->base[nic_i];
341                         uint64_t qword = le64toh(curr->wb.qword1.status_error_len);
342                         uint32_t staterr = (qword & I40E_RXD_QW1_STATUS_MASK)
343                                  >> I40E_RXD_QW1_STATUS_SHIFT;
344
345                         if ((staterr & (1<<I40E_RX_DESC_STATUS_DD_SHIFT)) == 0)
346                                 break;
347                         ring->slot[nm_i].len = ((qword & I40E_RXD_QW1_LENGTH_PBUF_MASK)
348                             >> I40E_RXD_QW1_LENGTH_PBUF_SHIFT) - crclen;
349                         ring->slot[nm_i].flags = slot_flags;
350                         bus_dmamap_sync(rxr->ptag,
351                             rxr->buffers[nic_i].pmap, BUS_DMASYNC_POSTREAD);
352                         nm_i = nm_next(nm_i, lim);
353                         nic_i = nm_next(nic_i, lim);
354                 }
355                 if (n) { /* update the state variables */
356                         if (netmap_no_pendintr && !force_update) {
357                                 /* diagnostics */
358                                 ixl_rx_miss ++;
359                                 ixl_rx_miss_bufs += n;
360                         }
361                         rxr->next_check = nic_i;
362                         kring->nr_hwtail = nm_i;
363                 }
364                 kring->nr_kflags &= ~NKR_PENDINTR;
365         }
366
367         /*
368          * Second part: skip past packets that userspace has released.
369          * (kring->nr_hwcur to head excluded),
370          * and make the buffers available for reception.
371          * As usual nm_i is the index in the netmap ring,
372          * nic_i is the index in the NIC ring, and
373          * nm_i == (nic_i + kring->nkr_hwofs) % ring_size
374          */
375         nm_i = kring->nr_hwcur;
376         if (nm_i != head) {
377                 nic_i = netmap_idx_k2n(kring, nm_i);
378                 for (n = 0; nm_i != head; n++) {
379                         struct netmap_slot *slot = &ring->slot[nm_i];
380                         uint64_t paddr;
381                         void *addr = PNMB(na, slot, &paddr);
382
383                         union i40e_32byte_rx_desc *curr = &rxr->base[nic_i];
384                         struct ixl_rx_buf *rxbuf = &rxr->buffers[nic_i];
385
386                         if (addr == NETMAP_BUF_BASE(na)) /* bad buf */
387                                 goto ring_reset;
388
389                         if (slot->flags & NS_BUF_CHANGED) {
390                                 /* buffer has changed, reload map */
391                                 netmap_reload_map(na, rxr->ptag, rxbuf->pmap, addr);
392                                 slot->flags &= ~NS_BUF_CHANGED;
393                         }
394                         curr->read.pkt_addr = htole64(paddr);
395                         curr->read.hdr_addr = 0; // XXX needed
396                         bus_dmamap_sync(rxr->ptag, rxbuf->pmap,
397                             BUS_DMASYNC_PREREAD);
398                         nm_i = nm_next(nm_i, lim);
399                         nic_i = nm_next(nic_i, lim);
400                 }
401                 kring->nr_hwcur = head;
402
403                 bus_dmamap_sync(rxr->dma.tag, rxr->dma.map,
404                     BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
405                 /*
406                  * IMPORTANT: we must leave one free slot in the ring,
407                  * so move nic_i back by one unit
408                  */
409                 nic_i = nm_prev(nic_i, lim);
410                 wr32(vsi->hw, rxr->tail, nic_i);
411         }
412
413         return 0;
414
415 ring_reset:
416         return netmap_ring_reinit(kring);
417 }
418
419 #endif /* !NETMAP_IXL_MAIN */
420
421 /* end of file */