]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/dev/netmap/if_re_netmap.h
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / dev / netmap / if_re_netmap.h
1 /*
2  * Copyright (C) 2011 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 "re"
30  * For details on netmap support please see ixgbe_netmap.h
31  */
32
33
34 #include <net/netmap.h>
35 #include <sys/selinfo.h>
36 #include <vm/vm.h>
37 #include <vm/pmap.h>    /* vtophys ? */
38 #include <dev/netmap/netmap_kern.h>
39
40
41 /*
42  * wrapper to export locks to the generic code
43  * We should not use the tx/rx locks
44  */
45 static void
46 re_netmap_lock_wrapper(struct ifnet *ifp, int what, u_int queueid)
47 {
48         struct rl_softc *adapter = ifp->if_softc;
49
50         switch (what) {
51         case NETMAP_CORE_LOCK:
52                 RL_LOCK(adapter);
53                 break;
54         case NETMAP_CORE_UNLOCK:
55                 RL_UNLOCK(adapter);
56                 break;
57
58         case NETMAP_TX_LOCK:
59         case NETMAP_RX_LOCK:
60         case NETMAP_TX_UNLOCK:
61         case NETMAP_RX_UNLOCK:
62                 D("invalid lock call %d, no tx/rx locks here", what);
63                 break;
64         }
65 }
66
67
68 /*
69  * support for netmap register/unregisted. We are already under core lock.
70  * only called on the first register or the last unregister.
71  */
72 static int
73 re_netmap_reg(struct ifnet *ifp, int onoff)
74 {
75         struct rl_softc *adapter = ifp->if_softc;
76         struct netmap_adapter *na = NA(ifp);
77         int error = 0;
78
79         if (na == NULL)
80                 return EINVAL;
81         /* Tell the stack that the interface is no longer active */
82         ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
83
84         re_stop(adapter);
85
86         if (onoff) {
87                 ifp->if_capenable |= IFCAP_NETMAP;
88
89                 /* save if_transmit to restore it later */
90                 na->if_transmit = ifp->if_transmit;
91                 ifp->if_transmit = netmap_start;
92
93                 re_init_locked(adapter);
94
95                 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) == 0) {
96                         error = ENOMEM;
97                         goto fail;
98                 }
99         } else {
100 fail:
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 */
105         }
106         return (error);
107 }
108
109
110 /*
111  * Reconcile kernel and user view of the transmit ring.
112  */
113 static int
114 re_netmap_txsync(struct ifnet *ifp, u_int ring_nr, int do_lock)
115 {
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;
122
123         k = ring->cur;
124         if (k > lim)
125                 return netmap_ring_reinit(kring);
126
127         if (do_lock)
128                 RL_LOCK(sc);
129
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);
134
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)) {
140                 uint32_t cmdstat =
141                         le32toh(sc->rl_ldata.rl_tx_list[l].rl_cmdstat);
142                 if (cmdstat & RL_TDESC_STAT_OWN)
143                         break;
144         }
145         if (n > 0) {
146                 sc->rl_ldata.rl_tx_considx = l;
147                 sc->rl_ldata.rl_tx_free += n;
148                 kring->nr_hwavail += n;
149         }
150
151         /* update avail to what the kernel knows */
152         ring->avail = kring->nr_hwavail;
153
154         j = kring->nr_hwcur;
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 ;
162                         uint64_t paddr;
163                         void *addr = PNMB(slot, &paddr);
164                         int len = slot->len;
165
166                         if (addr == netmap_buffer_base || len > NETMAP_BUF_SIZE) {
167                                 if (do_lock)
168                                         RL_UNLOCK(sc);
169                                 // XXX what about prodidx ?
170                                 return netmap_ring_reinit(kring);
171                         }
172
173                         if (l == lim)   /* mark end of ring */
174                                 cmd |= RL_TDESC_CMD_EOR;
175
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;
183                         }
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;
190                 }
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;
195
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);
199
200                 /* start ? */
201                 CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
202         }
203         if (do_lock)
204                 RL_UNLOCK(sc);
205         return 0;
206 }
207
208
209 /*
210  * Reconcile kernel and user view of the receive ring.
211  */
212 static int
213 re_netmap_rxsync(struct ifnet *ifp, u_int ring_nr, int do_lock)
214 {
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;
223
224         k = ring->cur;
225         if (k > lim)
226                 return netmap_ring_reinit(kring);
227
228         if (do_lock)
229                 RL_LOCK(sc);
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);
234
235         /*
236          * Import newly received packets into the netmap ring.
237          * j is an index in the netmap ring, l in the NIC ring.
238          *
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'
243          */
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;
248
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);
252                         uint32_t total_len;
253
254                         if ((rxstat & RL_RDESC_STAT_OWN) != 0)
255                                 break;
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;
266                 }
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;
271                 }
272                 kring->nr_kflags &= ~NKR_PENDINTR;
273         }
274
275         /* skip past packets that userspace has released */
276         j = kring->nr_hwcur;
277         if (resvd > 0) {
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...
281                 }
282                 k = (k >= resvd) ? k - resvd : k + lim + 1 - resvd;
283         }
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;
290                         uint64_t paddr;
291                         void *addr = PNMB(slot, &paddr);
292
293                         if (addr == netmap_buffer_base) { /* bad buf */
294                                 if (do_lock)
295                                         RL_UNLOCK(sc);
296                                 return netmap_ring_reinit(kring);
297                         }
298
299                         if (l == lim)   /* mark end of ring */
300                                 cmd |= RL_RDESC_CMD_EOR;
301
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;
309                         }
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;
315                 }
316                 kring->nr_hwavail -= n;
317                 kring->nr_hwcur = k;
318                 /* Flush the RX DMA ring */
319
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);
323         }
324         /* tell userspace that there are new packets */
325         ring->avail = kring->nr_hwavail - resvd;
326         if (do_lock)
327                 RL_UNLOCK(sc);
328         return 0;
329 }
330
331 /*
332  * Additional routines to init the tx and rx rings.
333  * In other drivers we do that inline in the main code.
334  */
335 static void
336 re_netmap_tx_init(struct rl_softc *sc)
337 {
338         struct rl_txdesc *txd;
339         struct rl_desc *desc;
340         int i, n;
341         struct netmap_adapter *na = NA(sc->rl_ifp);
342         struct netmap_slot *slot = netmap_reset(na, NR_TX, 0, 0);
343
344         /* slot is NULL if we are not in netmap mode */
345         if (!slot)
346                 return;
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;
351
352         /* l points in the netmap ring, i points in the NIC ring */
353         for (i = 0; i < n; i++) {
354                 uint64_t paddr;
355                 int l = netmap_idx_n2k(&na->tx_rings[0], i);
356                 void *addr = PNMB(slot + l, &paddr);
357
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);
362         }
363 }
364
365 static void
366 re_netmap_rx_init(struct rl_softc *sc)
367 {
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;
371         uint32_t cmdstat;
372         int i, n, max_avail;
373
374         if (!slot)
375                 return;
376         n = sc->rl_ldata.rl_rx_desc_cnt;
377         /*
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).
381          */
382         max_avail = n - 1 - na->rx_rings[0].nr_hwavail;
383         for (i = 0; i < n; i++) {
384                 void *addr;
385                 uint64_t paddr;
386                 int l = netmap_idx_n2k(&na->rx_rings[0], i);
387
388                 addr = PNMB(slot + l, &paddr);
389
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;
399                 if (i < max_avail)
400                         cmdstat |= RL_RDESC_CMD_OWN;
401                 desc[i].rl_cmdstat = htole32(cmdstat);
402         }
403 }
404
405
406 static void
407 re_netmap_attach(struct rl_softc *sc)
408 {
409         struct netmap_adapter na;
410
411         bzero(&na, sizeof(na));
412
413         na.ifp = sc->rl_ifp;
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);
422 }
423 /* end of file */