if_vioif.c revision 1.11.2.8 1 /* $NetBSD: if_vioif.c,v 1.11.2.8 2016/12/05 10:55:02 skrll Exp $ */
2
3 /*
4 * Copyright (c) 2010 Minoura Makoto.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: if_vioif.c,v 1.11.2.8 2016/12/05 10:55:02 skrll Exp $");
30
31 #ifdef _KERNEL_OPT
32 #include "opt_net_mpsafe.h"
33 #endif
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/bus.h>
39 #include <sys/condvar.h>
40 #include <sys/device.h>
41 #include <sys/intr.h>
42 #include <sys/kmem.h>
43 #include <sys/mbuf.h>
44 #include <sys/mutex.h>
45 #include <sys/sockio.h>
46 #include <sys/cpu.h>
47 #include <sys/module.h>
48
49 #include <dev/pci/pcidevs.h>
50 #include <dev/pci/pcireg.h>
51 #include <dev/pci/pcivar.h>
52 #include <dev/pci/virtioreg.h>
53 #include <dev/pci/virtiovar.h>
54
55 #include <net/if.h>
56 #include <net/if_media.h>
57 #include <net/if_ether.h>
58
59 #include <net/bpf.h>
60
61 #include "ioconf.h"
62
63 #ifdef NET_MPSAFE
64 #define VIOIF_MPSAFE 1
65 #endif
66
67 #ifdef SOFTINT_INTR
68 #define VIOIF_SOFTINT_INTR 1
69 #endif
70
71 /*
72 * if_vioifreg.h:
73 */
74 /* Configuration registers */
75 #define VIRTIO_NET_CONFIG_MAC 0 /* 8bit x 6byte */
76 #define VIRTIO_NET_CONFIG_STATUS 6 /* 16bit */
77
78 /* Feature bits */
79 #define VIRTIO_NET_F_CSUM (1<<0)
80 #define VIRTIO_NET_F_GUEST_CSUM (1<<1)
81 #define VIRTIO_NET_F_MAC (1<<5)
82 #define VIRTIO_NET_F_GSO (1<<6)
83 #define VIRTIO_NET_F_GUEST_TSO4 (1<<7)
84 #define VIRTIO_NET_F_GUEST_TSO6 (1<<8)
85 #define VIRTIO_NET_F_GUEST_ECN (1<<9)
86 #define VIRTIO_NET_F_GUEST_UFO (1<<10)
87 #define VIRTIO_NET_F_HOST_TSO4 (1<<11)
88 #define VIRTIO_NET_F_HOST_TSO6 (1<<12)
89 #define VIRTIO_NET_F_HOST_ECN (1<<13)
90 #define VIRTIO_NET_F_HOST_UFO (1<<14)
91 #define VIRTIO_NET_F_MRG_RXBUF (1<<15)
92 #define VIRTIO_NET_F_STATUS (1<<16)
93 #define VIRTIO_NET_F_CTRL_VQ (1<<17)
94 #define VIRTIO_NET_F_CTRL_RX (1<<18)
95 #define VIRTIO_NET_F_CTRL_VLAN (1<<19)
96
97 #define VIRTIO_NET_FLAG_BITS \
98 VIRTIO_COMMON_FLAG_BITS \
99 "\x14""CTRL_VLAN" \
100 "\x13""CTRL_RX" \
101 "\x12""CTRL_VQ" \
102 "\x11""STATUS" \
103 "\x10""MRG_RXBUF" \
104 "\x0f""HOST_UFO" \
105 "\x0e""HOST_ECN" \
106 "\x0d""HOST_TSO6" \
107 "\x0c""HOST_TSO4" \
108 "\x0b""GUEST_UFO" \
109 "\x0a""GUEST_ECN" \
110 "\x09""GUEST_TSO6" \
111 "\x08""GUEST_TSO4" \
112 "\x07""GSO" \
113 "\x06""MAC" \
114 "\x02""GUEST_CSUM" \
115 "\x01""CSUM"
116
117 /* Status */
118 #define VIRTIO_NET_S_LINK_UP 1
119
120 /* Packet header structure */
121 struct virtio_net_hdr {
122 uint8_t flags;
123 uint8_t gso_type;
124 uint16_t hdr_len;
125 uint16_t gso_size;
126 uint16_t csum_start;
127 uint16_t csum_offset;
128 #if 0
129 uint16_t num_buffers; /* if VIRTIO_NET_F_MRG_RXBUF enabled */
130 #endif
131 } __packed;
132
133 #define VIRTIO_NET_HDR_F_NEEDS_CSUM 1 /* flags */
134 #define VIRTIO_NET_HDR_GSO_NONE 0 /* gso_type */
135 #define VIRTIO_NET_HDR_GSO_TCPV4 1 /* gso_type */
136 #define VIRTIO_NET_HDR_GSO_UDP 3 /* gso_type */
137 #define VIRTIO_NET_HDR_GSO_TCPV6 4 /* gso_type */
138 #define VIRTIO_NET_HDR_GSO_ECN 0x80 /* gso_type, |'ed */
139
140 #define VIRTIO_NET_MAX_GSO_LEN (65536+ETHER_HDR_LEN)
141
142 /* Control virtqueue */
143 struct virtio_net_ctrl_cmd {
144 uint8_t class;
145 uint8_t command;
146 } __packed;
147 #define VIRTIO_NET_CTRL_RX 0
148 # define VIRTIO_NET_CTRL_RX_PROMISC 0
149 # define VIRTIO_NET_CTRL_RX_ALLMULTI 1
150
151 #define VIRTIO_NET_CTRL_MAC 1
152 # define VIRTIO_NET_CTRL_MAC_TABLE_SET 0
153
154 #define VIRTIO_NET_CTRL_VLAN 2
155 # define VIRTIO_NET_CTRL_VLAN_ADD 0
156 # define VIRTIO_NET_CTRL_VLAN_DEL 1
157
158 struct virtio_net_ctrl_status {
159 uint8_t ack;
160 } __packed;
161 #define VIRTIO_NET_OK 0
162 #define VIRTIO_NET_ERR 1
163
164 struct virtio_net_ctrl_rx {
165 uint8_t onoff;
166 } __packed;
167
168 struct virtio_net_ctrl_mac_tbl {
169 uint32_t nentries;
170 uint8_t macs[][ETHER_ADDR_LEN];
171 } __packed;
172
173 struct virtio_net_ctrl_vlan {
174 uint16_t id;
175 } __packed;
176
177
178 /*
179 * if_vioifvar.h:
180 */
181 struct vioif_softc {
182 device_t sc_dev;
183
184 struct virtio_softc *sc_virtio;
185 struct virtqueue sc_vq[3];
186 #define VQ_RX 0
187 #define VQ_TX 1
188 #define VQ_CTRL 2
189
190 uint8_t sc_mac[ETHER_ADDR_LEN];
191 struct ethercom sc_ethercom;
192 short sc_deferred_init_done;
193
194 /* bus_dmamem */
195 bus_dma_segment_t sc_hdr_segs[1];
196 struct virtio_net_hdr *sc_hdrs;
197 #define sc_rx_hdrs sc_hdrs
198 struct virtio_net_hdr *sc_tx_hdrs;
199 struct virtio_net_ctrl_cmd *sc_ctrl_cmd;
200 struct virtio_net_ctrl_status *sc_ctrl_status;
201 struct virtio_net_ctrl_rx *sc_ctrl_rx;
202 struct virtio_net_ctrl_mac_tbl *sc_ctrl_mac_tbl_uc;
203 struct virtio_net_ctrl_mac_tbl *sc_ctrl_mac_tbl_mc;
204
205 /* kmem */
206 bus_dmamap_t *sc_arrays;
207 #define sc_rxhdr_dmamaps sc_arrays
208 bus_dmamap_t *sc_txhdr_dmamaps;
209 bus_dmamap_t *sc_rx_dmamaps;
210 bus_dmamap_t *sc_tx_dmamaps;
211 struct mbuf **sc_rx_mbufs;
212 struct mbuf **sc_tx_mbufs;
213
214 bus_dmamap_t sc_ctrl_cmd_dmamap;
215 bus_dmamap_t sc_ctrl_status_dmamap;
216 bus_dmamap_t sc_ctrl_rx_dmamap;
217 bus_dmamap_t sc_ctrl_tbl_uc_dmamap;
218 bus_dmamap_t sc_ctrl_tbl_mc_dmamap;
219
220 void *sc_rx_softint;
221
222 enum {
223 FREE, INUSE, DONE
224 } sc_ctrl_inuse;
225 kcondvar_t sc_ctrl_wait;
226 kmutex_t sc_ctrl_wait_lock;
227 kmutex_t *sc_tx_lock;
228 kmutex_t *sc_rx_lock;
229 bool sc_stopping;
230 };
231 #define VIRTIO_NET_TX_MAXNSEGS (16) /* XXX */
232 #define VIRTIO_NET_CTRL_MAC_MAXENTRIES (64) /* XXX */
233
234 #define VIOIF_TX_LOCK(_sc) if ((_sc)->sc_tx_lock) mutex_enter((_sc)->sc_tx_lock)
235 #define VIOIF_TX_UNLOCK(_sc) if ((_sc)->sc_tx_lock) mutex_exit((_sc)->sc_tx_lock)
236 #define VIOIF_TX_LOCKED(_sc) (!(_sc)->sc_tx_lock || mutex_owned((_sc)->sc_tx_lock))
237 #define VIOIF_RX_LOCK(_sc) if ((_sc)->sc_rx_lock) mutex_enter((_sc)->sc_rx_lock)
238 #define VIOIF_RX_UNLOCK(_sc) if ((_sc)->sc_rx_lock) mutex_exit((_sc)->sc_rx_lock)
239 #define VIOIF_RX_LOCKED(_sc) (!(_sc)->sc_rx_lock || mutex_owned((_sc)->sc_rx_lock))
240
241 /* cfattach interface functions */
242 static int vioif_match(device_t, cfdata_t, void *);
243 static void vioif_attach(device_t, device_t, void *);
244 static void vioif_deferred_init(device_t);
245
246 /* ifnet interface functions */
247 static int vioif_init(struct ifnet *);
248 static void vioif_stop(struct ifnet *, int);
249 static void vioif_start(struct ifnet *);
250 static int vioif_ioctl(struct ifnet *, u_long, void *);
251 static void vioif_watchdog(struct ifnet *);
252
253 /* rx */
254 static int vioif_add_rx_mbuf(struct vioif_softc *, int);
255 static void vioif_free_rx_mbuf(struct vioif_softc *, int);
256 static void vioif_populate_rx_mbufs(struct vioif_softc *);
257 static void vioif_populate_rx_mbufs_locked(struct vioif_softc *);
258 static int vioif_rx_deq(struct vioif_softc *);
259 static int vioif_rx_deq_locked(struct vioif_softc *);
260 static int vioif_rx_vq_done(struct virtqueue *);
261 static void vioif_rx_softint(void *);
262 static void vioif_rx_drain(struct vioif_softc *);
263
264 /* tx */
265 static int vioif_tx_vq_done(struct virtqueue *);
266 static int vioif_tx_vq_done_locked(struct virtqueue *);
267 static void vioif_tx_drain(struct vioif_softc *);
268
269 /* other control */
270 static int vioif_updown(struct vioif_softc *, bool);
271 static int vioif_ctrl_rx(struct vioif_softc *, int, bool);
272 static int vioif_set_promisc(struct vioif_softc *, bool);
273 static int vioif_set_allmulti(struct vioif_softc *, bool);
274 static int vioif_set_rx_filter(struct vioif_softc *);
275 static int vioif_rx_filter(struct vioif_softc *);
276 static int vioif_ctrl_vq_done(struct virtqueue *);
277
278 CFATTACH_DECL_NEW(vioif, sizeof(struct vioif_softc),
279 vioif_match, vioif_attach, NULL, NULL);
280
281 static int
282 vioif_match(device_t parent, cfdata_t match, void *aux)
283 {
284 struct virtio_softc *va = aux;
285
286 if (va->sc_childdevid == PCI_PRODUCT_VIRTIO_NETWORK)
287 return 1;
288
289 return 0;
290 }
291
292 /* allocate memory */
293 /*
294 * dma memory is used for:
295 * sc_rx_hdrs[slot]: metadata array for recieved frames (READ)
296 * sc_tx_hdrs[slot]: metadata array for frames to be sent (WRITE)
297 * sc_ctrl_cmd: command to be sent via ctrl vq (WRITE)
298 * sc_ctrl_status: return value for a command via ctrl vq (READ)
299 * sc_ctrl_rx: parameter for a VIRTIO_NET_CTRL_RX class command
300 * (WRITE)
301 * sc_ctrl_mac_tbl_uc: unicast MAC address filter for a VIRTIO_NET_CTRL_MAC
302 * class command (WRITE)
303 * sc_ctrl_mac_tbl_mc: multicast MAC address filter for a VIRTIO_NET_CTRL_MAC
304 * class command (WRITE)
305 * sc_ctrl_* structures are allocated only one each; they are protected by
306 * sc_ctrl_inuse variable and sc_ctrl_wait condvar.
307 */
308 /*
309 * dynamically allocated memory is used for:
310 * sc_rxhdr_dmamaps[slot]: bus_dmamap_t array for sc_rx_hdrs[slot]
311 * sc_txhdr_dmamaps[slot]: bus_dmamap_t array for sc_tx_hdrs[slot]
312 * sc_rx_dmamaps[slot]: bus_dmamap_t array for recieved payload
313 * sc_tx_dmamaps[slot]: bus_dmamap_t array for sent payload
314 * sc_rx_mbufs[slot]: mbuf pointer array for recieved frames
315 * sc_tx_mbufs[slot]: mbuf pointer array for sent frames
316 */
317 static int
318 vioif_alloc_mems(struct vioif_softc *sc)
319 {
320 struct virtio_softc *vsc = sc->sc_virtio;
321 int allocsize, allocsize2, r, rsegs, i;
322 void *vaddr;
323 intptr_t p;
324 int rxqsize, txqsize;
325
326 rxqsize = vsc->sc_vqs[VQ_RX].vq_num;
327 txqsize = vsc->sc_vqs[VQ_TX].vq_num;
328
329 allocsize = sizeof(struct virtio_net_hdr) * rxqsize;
330 allocsize += sizeof(struct virtio_net_hdr) * txqsize;
331 if (vsc->sc_nvqs == 3) {
332 allocsize += sizeof(struct virtio_net_ctrl_cmd) * 1;
333 allocsize += sizeof(struct virtio_net_ctrl_status) * 1;
334 allocsize += sizeof(struct virtio_net_ctrl_rx) * 1;
335 allocsize += sizeof(struct virtio_net_ctrl_mac_tbl)
336 + sizeof(struct virtio_net_ctrl_mac_tbl)
337 + ETHER_ADDR_LEN * VIRTIO_NET_CTRL_MAC_MAXENTRIES;
338 }
339 r = bus_dmamem_alloc(vsc->sc_dmat, allocsize, 0, 0,
340 &sc->sc_hdr_segs[0], 1, &rsegs, BUS_DMA_NOWAIT);
341 if (r != 0) {
342 aprint_error_dev(sc->sc_dev,
343 "DMA memory allocation failed, size %d, "
344 "error code %d\n", allocsize, r);
345 goto err_none;
346 }
347 r = bus_dmamem_map(vsc->sc_dmat,
348 &sc->sc_hdr_segs[0], 1, allocsize,
349 &vaddr, BUS_DMA_NOWAIT);
350 if (r != 0) {
351 aprint_error_dev(sc->sc_dev,
352 "DMA memory map failed, "
353 "error code %d\n", r);
354 goto err_dmamem_alloc;
355 }
356 sc->sc_hdrs = vaddr;
357 memset(vaddr, 0, allocsize);
358 p = (intptr_t) vaddr;
359 p += sizeof(struct virtio_net_hdr) * rxqsize;
360 #define P(name,size) do { sc->sc_ ##name = (void*) p; \
361 p += size; } while (0)
362 P(tx_hdrs, sizeof(struct virtio_net_hdr) * txqsize);
363 if (vsc->sc_nvqs == 3) {
364 P(ctrl_cmd, sizeof(struct virtio_net_ctrl_cmd));
365 P(ctrl_status, sizeof(struct virtio_net_ctrl_status));
366 P(ctrl_rx, sizeof(struct virtio_net_ctrl_rx));
367 P(ctrl_mac_tbl_uc, sizeof(struct virtio_net_ctrl_mac_tbl));
368 P(ctrl_mac_tbl_mc,
369 (sizeof(struct virtio_net_ctrl_mac_tbl)
370 + ETHER_ADDR_LEN * VIRTIO_NET_CTRL_MAC_MAXENTRIES));
371 }
372 #undef P
373
374 allocsize2 = sizeof(bus_dmamap_t) * (rxqsize + txqsize);
375 allocsize2 += sizeof(bus_dmamap_t) * (rxqsize + txqsize);
376 allocsize2 += sizeof(struct mbuf*) * (rxqsize + txqsize);
377 sc->sc_arrays = kmem_zalloc(allocsize2, KM_SLEEP);
378 if (sc->sc_arrays == NULL)
379 goto err_dmamem_map;
380 sc->sc_txhdr_dmamaps = sc->sc_arrays + rxqsize;
381 sc->sc_rx_dmamaps = sc->sc_txhdr_dmamaps + txqsize;
382 sc->sc_tx_dmamaps = sc->sc_rx_dmamaps + rxqsize;
383 sc->sc_rx_mbufs = (void*) (sc->sc_tx_dmamaps + txqsize);
384 sc->sc_tx_mbufs = sc->sc_rx_mbufs + rxqsize;
385
386 #define C(map, buf, size, nsegs, rw, usage) \
387 do { \
388 r = bus_dmamap_create(vsc->sc_dmat, size, nsegs, size, 0, \
389 BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, \
390 &sc->sc_ ##map); \
391 if (r != 0) { \
392 aprint_error_dev(sc->sc_dev, \
393 usage " dmamap creation failed, " \
394 "error code %d\n", r); \
395 goto err_reqs; \
396 } \
397 } while (0)
398 #define C_L1(map, buf, size, nsegs, rw, usage) \
399 C(map, buf, size, nsegs, rw, usage); \
400 do { \
401 r = bus_dmamap_load(vsc->sc_dmat, sc->sc_ ##map, \
402 &sc->sc_ ##buf, size, NULL, \
403 BUS_DMA_ ##rw | BUS_DMA_NOWAIT); \
404 if (r != 0) { \
405 aprint_error_dev(sc->sc_dev, \
406 usage " dmamap load failed, " \
407 "error code %d\n", r); \
408 goto err_reqs; \
409 } \
410 } while (0)
411 #define C_L2(map, buf, size, nsegs, rw, usage) \
412 C(map, buf, size, nsegs, rw, usage); \
413 do { \
414 r = bus_dmamap_load(vsc->sc_dmat, sc->sc_ ##map, \
415 sc->sc_ ##buf, size, NULL, \
416 BUS_DMA_ ##rw | BUS_DMA_NOWAIT); \
417 if (r != 0) { \
418 aprint_error_dev(sc->sc_dev, \
419 usage " dmamap load failed, " \
420 "error code %d\n", r); \
421 goto err_reqs; \
422 } \
423 } while (0)
424 for (i = 0; i < rxqsize; i++) {
425 C_L1(rxhdr_dmamaps[i], rx_hdrs[i],
426 sizeof(struct virtio_net_hdr), 1,
427 READ, "rx header");
428 C(rx_dmamaps[i], NULL, MCLBYTES, 1, 0, "rx payload");
429 }
430
431 for (i = 0; i < txqsize; i++) {
432 C_L1(txhdr_dmamaps[i], tx_hdrs[i],
433 sizeof(struct virtio_net_hdr), 1,
434 WRITE, "tx header");
435 C(tx_dmamaps[i], NULL, ETHER_MAX_LEN, 16 /* XXX */, 0,
436 "tx payload");
437 }
438
439 if (vsc->sc_nvqs == 3) {
440 /* control vq class & command */
441 C_L2(ctrl_cmd_dmamap, ctrl_cmd,
442 sizeof(struct virtio_net_ctrl_cmd), 1, WRITE,
443 "control command");
444
445 /* control vq status */
446 C_L2(ctrl_status_dmamap, ctrl_status,
447 sizeof(struct virtio_net_ctrl_status), 1, READ,
448 "control status");
449
450 /* control vq rx mode command parameter */
451 C_L2(ctrl_rx_dmamap, ctrl_rx,
452 sizeof(struct virtio_net_ctrl_rx), 1, WRITE,
453 "rx mode control command");
454
455 /* control vq MAC filter table for unicast */
456 /* do not load now since its length is variable */
457 C(ctrl_tbl_uc_dmamap, NULL,
458 sizeof(struct virtio_net_ctrl_mac_tbl) + 0, 1, WRITE,
459 "unicast MAC address filter command");
460
461 /* control vq MAC filter table for multicast */
462 C(ctrl_tbl_mc_dmamap, NULL,
463 (sizeof(struct virtio_net_ctrl_mac_tbl)
464 + ETHER_ADDR_LEN * VIRTIO_NET_CTRL_MAC_MAXENTRIES),
465 1, WRITE, "multicast MAC address filter command");
466 }
467 #undef C_L2
468 #undef C_L1
469 #undef C
470
471 return 0;
472
473 err_reqs:
474 #define D(map) \
475 do { \
476 if (sc->sc_ ##map) { \
477 bus_dmamap_destroy(vsc->sc_dmat, sc->sc_ ##map); \
478 sc->sc_ ##map = NULL; \
479 } \
480 } while (0)
481 D(ctrl_tbl_mc_dmamap);
482 D(ctrl_tbl_uc_dmamap);
483 D(ctrl_rx_dmamap);
484 D(ctrl_status_dmamap);
485 D(ctrl_cmd_dmamap);
486 for (i = 0; i < txqsize; i++) {
487 D(tx_dmamaps[i]);
488 D(txhdr_dmamaps[i]);
489 }
490 for (i = 0; i < rxqsize; i++) {
491 D(rx_dmamaps[i]);
492 D(rxhdr_dmamaps[i]);
493 }
494 #undef D
495 if (sc->sc_arrays) {
496 kmem_free(sc->sc_arrays, allocsize2);
497 sc->sc_arrays = 0;
498 }
499 err_dmamem_map:
500 bus_dmamem_unmap(vsc->sc_dmat, sc->sc_hdrs, allocsize);
501 err_dmamem_alloc:
502 bus_dmamem_free(vsc->sc_dmat, &sc->sc_hdr_segs[0], 1);
503 err_none:
504 return -1;
505 }
506
507 static void
508 vioif_attach(device_t parent, device_t self, void *aux)
509 {
510 struct vioif_softc *sc = device_private(self);
511 struct virtio_softc *vsc = device_private(parent);
512 uint32_t features;
513 char buf[256];
514 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
515 u_int flags;
516 int r;
517
518 if (vsc->sc_child != NULL) {
519 aprint_normal(": child already attached for %s; "
520 "something wrong...\n",
521 device_xname(parent));
522 return;
523 }
524
525 sc->sc_dev = self;
526 sc->sc_virtio = vsc;
527
528 vsc->sc_child = self;
529 vsc->sc_ipl = IPL_NET;
530 vsc->sc_vqs = &sc->sc_vq[0];
531 vsc->sc_config_change = NULL;
532 vsc->sc_intrhand = virtio_vq_intr;
533 vsc->sc_flags = 0;
534
535 #ifdef VIOIF_MPSAFE
536 vsc->sc_flags |= VIRTIO_F_PCI_INTR_MPSAFE;
537 #endif
538 #ifdef VIOIF_SOFTINT_INTR
539 vsc->sc_flags |= VIRTIO_F_PCI_INTR_SOFTINT;
540 #endif
541 vsc->sc_flags |= VIRTIO_F_PCI_INTR_MSIX;
542
543 features = virtio_negotiate_features(vsc,
544 (VIRTIO_NET_F_MAC |
545 VIRTIO_NET_F_STATUS |
546 VIRTIO_NET_F_CTRL_VQ |
547 VIRTIO_NET_F_CTRL_RX |
548 VIRTIO_F_NOTIFY_ON_EMPTY));
549 if (features & VIRTIO_NET_F_MAC) {
550 sc->sc_mac[0] = virtio_read_device_config_1(vsc,
551 VIRTIO_NET_CONFIG_MAC+0);
552 sc->sc_mac[1] = virtio_read_device_config_1(vsc,
553 VIRTIO_NET_CONFIG_MAC+1);
554 sc->sc_mac[2] = virtio_read_device_config_1(vsc,
555 VIRTIO_NET_CONFIG_MAC+2);
556 sc->sc_mac[3] = virtio_read_device_config_1(vsc,
557 VIRTIO_NET_CONFIG_MAC+3);
558 sc->sc_mac[4] = virtio_read_device_config_1(vsc,
559 VIRTIO_NET_CONFIG_MAC+4);
560 sc->sc_mac[5] = virtio_read_device_config_1(vsc,
561 VIRTIO_NET_CONFIG_MAC+5);
562 } else {
563 /* code stolen from sys/net/if_tap.c */
564 struct timeval tv;
565 uint32_t ui;
566 getmicrouptime(&tv);
567 ui = (tv.tv_sec ^ tv.tv_usec) & 0xffffff;
568 memcpy(sc->sc_mac+3, (uint8_t *)&ui, 3);
569 virtio_write_device_config_1(vsc,
570 VIRTIO_NET_CONFIG_MAC+0,
571 sc->sc_mac[0]);
572 virtio_write_device_config_1(vsc,
573 VIRTIO_NET_CONFIG_MAC+1,
574 sc->sc_mac[1]);
575 virtio_write_device_config_1(vsc,
576 VIRTIO_NET_CONFIG_MAC+2,
577 sc->sc_mac[2]);
578 virtio_write_device_config_1(vsc,
579 VIRTIO_NET_CONFIG_MAC+3,
580 sc->sc_mac[3]);
581 virtio_write_device_config_1(vsc,
582 VIRTIO_NET_CONFIG_MAC+4,
583 sc->sc_mac[4]);
584 virtio_write_device_config_1(vsc,
585 VIRTIO_NET_CONFIG_MAC+5,
586 sc->sc_mac[5]);
587 }
588 aprint_normal(": Ethernet address %s\n", ether_sprintf(sc->sc_mac));
589 snprintb(buf, sizeof(buf), VIRTIO_NET_FLAG_BITS, features);
590 aprint_normal_dev(self, "Features: %s\n", buf);
591 aprint_naive("\n");
592
593 #ifdef VIOIF_MPSAFE
594 sc->sc_tx_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
595 sc->sc_rx_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
596 #else
597 sc->sc_tx_lock = NULL;
598 sc->sc_rx_lock = NULL;
599 #endif
600 sc->sc_stopping = false;
601
602 /*
603 * Allocating a virtqueue for Rx
604 */
605 r = virtio_alloc_vq(vsc, &sc->sc_vq[VQ_RX], 0,
606 MCLBYTES+sizeof(struct virtio_net_hdr), 2, "rx");
607 if (r != 0)
608 goto err;
609 vsc->sc_nvqs = 1;
610 sc->sc_vq[VQ_RX].vq_done = vioif_rx_vq_done;
611
612 /*
613 * Allocating a virtqueue for Tx
614 */
615 r = virtio_alloc_vq(vsc, &sc->sc_vq[VQ_TX], 1,
616 (sizeof(struct virtio_net_hdr) + (ETHER_MAX_LEN - ETHER_HDR_LEN)),
617 VIRTIO_NET_TX_MAXNSEGS + 1, "tx");
618 if (r != 0)
619 goto err;
620 vsc->sc_nvqs = 2;
621 sc->sc_vq[VQ_TX].vq_done = vioif_tx_vq_done;
622
623 virtio_start_vq_intr(vsc, &sc->sc_vq[VQ_RX]);
624 virtio_stop_vq_intr(vsc, &sc->sc_vq[VQ_TX]); /* not urgent; do it later */
625
626 if ((features & VIRTIO_NET_F_CTRL_VQ) &&
627 (features & VIRTIO_NET_F_CTRL_RX)) {
628 /*
629 * Allocating a virtqueue for control channel
630 */
631 r = virtio_alloc_vq(vsc, &sc->sc_vq[VQ_CTRL], 2,
632 NBPG, 1, "control");
633 if (r != 0) {
634 aprint_error_dev(self, "failed to allocate "
635 "a virtqueue for control channel\n");
636 goto skip;
637 }
638
639 sc->sc_vq[VQ_CTRL].vq_done = vioif_ctrl_vq_done;
640 cv_init(&sc->sc_ctrl_wait, "ctrl_vq");
641 mutex_init(&sc->sc_ctrl_wait_lock, MUTEX_DEFAULT, IPL_NET);
642 sc->sc_ctrl_inuse = FREE;
643 virtio_start_vq_intr(vsc, &sc->sc_vq[VQ_CTRL]);
644 vsc->sc_nvqs = 3;
645 }
646 skip:
647
648 #ifdef VIOIF_MPSAFE
649 flags = SOFTINT_NET | SOFTINT_MPSAFE;
650 #else
651 flags = SOFTINT_NET;
652 #endif
653 sc->sc_rx_softint = softint_establish(flags, vioif_rx_softint, sc);
654 if (sc->sc_rx_softint == NULL) {
655 aprint_error_dev(self, "cannot establish softint\n");
656 goto err;
657 }
658
659 if (vioif_alloc_mems(sc) < 0)
660 goto err;
661
662 strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
663 ifp->if_softc = sc;
664 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
665 ifp->if_start = vioif_start;
666 ifp->if_ioctl = vioif_ioctl;
667 ifp->if_init = vioif_init;
668 ifp->if_stop = vioif_stop;
669 ifp->if_capabilities = 0;
670 ifp->if_watchdog = vioif_watchdog;
671
672 sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
673
674 if_attach(ifp);
675 ether_ifattach(ifp, sc->sc_mac);
676
677 return;
678
679 err:
680 if (sc->sc_tx_lock)
681 mutex_obj_free(sc->sc_tx_lock);
682 if (sc->sc_rx_lock)
683 mutex_obj_free(sc->sc_rx_lock);
684
685 if (vsc->sc_nvqs == 3) {
686 cv_destroy(&sc->sc_ctrl_wait);
687 mutex_destroy(&sc->sc_ctrl_wait_lock);
688 }
689
690 while (vsc->sc_nvqs > 0)
691 virtio_free_vq(vsc, &sc->sc_vq[--vsc->sc_nvqs]);
692
693 vsc->sc_child = (void*)1;
694 return;
695 }
696
697 /* we need interrupts to make promiscuous mode off */
698 static void
699 vioif_deferred_init(device_t self)
700 {
701 struct vioif_softc *sc = device_private(self);
702 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
703 int r;
704
705 if (ifp->if_flags & IFF_PROMISC)
706 return;
707
708 r = vioif_set_promisc(sc, false);
709 if (r != 0)
710 aprint_error_dev(self, "resetting promisc mode failed, "
711 "errror code %d\n", r);
712 }
713
714 /*
715 * Interface functions for ifnet
716 */
717 static int
718 vioif_init(struct ifnet *ifp)
719 {
720 struct vioif_softc *sc = ifp->if_softc;
721
722 vioif_stop(ifp, 0);
723
724 if (!sc->sc_deferred_init_done) {
725 struct virtio_softc *vsc = sc->sc_virtio;
726
727 sc->sc_deferred_init_done = 1;
728 if (vsc->sc_nvqs == 3)
729 vioif_deferred_init(sc->sc_dev);
730 }
731
732 /* Have to set false before vioif_populate_rx_mbufs */
733 sc->sc_stopping = false;
734
735 vioif_populate_rx_mbufs(sc);
736
737 vioif_updown(sc, true);
738 ifp->if_flags |= IFF_RUNNING;
739 ifp->if_flags &= ~IFF_OACTIVE;
740 vioif_rx_filter(sc);
741
742 return 0;
743 }
744
745 static void
746 vioif_stop(struct ifnet *ifp, int disable)
747 {
748 struct vioif_softc *sc = ifp->if_softc;
749 struct virtio_softc *vsc = sc->sc_virtio;
750
751 /* Take the locks to ensure that ongoing TX/RX finish */
752 VIOIF_TX_LOCK(sc);
753 VIOIF_RX_LOCK(sc);
754 sc->sc_stopping = true;
755 VIOIF_RX_UNLOCK(sc);
756 VIOIF_TX_UNLOCK(sc);
757
758 /* only way to stop I/O and DMA is resetting... */
759 virtio_reset(vsc);
760 vioif_rx_deq(sc);
761 vioif_tx_drain(sc);
762 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
763
764 if (disable)
765 vioif_rx_drain(sc);
766
767 virtio_reinit_start(vsc);
768 virtio_negotiate_features(vsc, vsc->sc_features);
769 virtio_start_vq_intr(vsc, &sc->sc_vq[VQ_RX]);
770 virtio_stop_vq_intr(vsc, &sc->sc_vq[VQ_TX]);
771 if (vsc->sc_nvqs >= 3)
772 virtio_start_vq_intr(vsc, &sc->sc_vq[VQ_CTRL]);
773 virtio_reinit_end(vsc);
774 vioif_updown(sc, false);
775 }
776
777 static void
778 vioif_start(struct ifnet *ifp)
779 {
780 struct vioif_softc *sc = ifp->if_softc;
781 struct virtio_softc *vsc = sc->sc_virtio;
782 struct virtqueue *vq = &sc->sc_vq[VQ_TX];
783 struct mbuf *m;
784 int queued = 0, retry = 0;
785
786 VIOIF_TX_LOCK(sc);
787
788 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
789 goto out;
790
791 if (sc->sc_stopping)
792 goto out;
793
794 for (;;) {
795 int slot, r;
796
797 IFQ_DEQUEUE(&ifp->if_snd, m);
798
799 if (m == NULL)
800 break;
801
802 retry:
803 r = virtio_enqueue_prep(vsc, vq, &slot);
804 if (r == EAGAIN) {
805 ifp->if_flags |= IFF_OACTIVE;
806 vioif_tx_vq_done_locked(vq);
807 if (retry++ == 0)
808 goto retry;
809 else
810 break;
811 }
812 if (r != 0)
813 panic("enqueue_prep for a tx buffer");
814 r = bus_dmamap_load_mbuf(vsc->sc_dmat,
815 sc->sc_tx_dmamaps[slot],
816 m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
817 if (r != 0) {
818 virtio_enqueue_abort(vsc, vq, slot);
819 aprint_error_dev(sc->sc_dev,
820 "tx dmamap load failed, error code %d\n", r);
821 break;
822 }
823 r = virtio_enqueue_reserve(vsc, vq, slot,
824 sc->sc_tx_dmamaps[slot]->dm_nsegs + 1);
825 if (r != 0) {
826 bus_dmamap_unload(vsc->sc_dmat,
827 sc->sc_tx_dmamaps[slot]);
828 ifp->if_flags |= IFF_OACTIVE;
829 vioif_tx_vq_done_locked(vq);
830 if (retry++ == 0)
831 goto retry;
832 else
833 break;
834 }
835
836 sc->sc_tx_mbufs[slot] = m;
837
838 memset(&sc->sc_tx_hdrs[slot], 0, sizeof(struct virtio_net_hdr));
839 bus_dmamap_sync(vsc->sc_dmat, sc->sc_tx_dmamaps[slot],
840 0, sc->sc_tx_dmamaps[slot]->dm_mapsize,
841 BUS_DMASYNC_PREWRITE);
842 bus_dmamap_sync(vsc->sc_dmat, sc->sc_txhdr_dmamaps[slot],
843 0, sc->sc_txhdr_dmamaps[slot]->dm_mapsize,
844 BUS_DMASYNC_PREWRITE);
845 virtio_enqueue(vsc, vq, slot, sc->sc_txhdr_dmamaps[slot], true);
846 virtio_enqueue(vsc, vq, slot, sc->sc_tx_dmamaps[slot], true);
847 virtio_enqueue_commit(vsc, vq, slot, false);
848 queued++;
849 bpf_mtap(ifp, m);
850 }
851
852 if (m != NULL) {
853 ifp->if_flags |= IFF_OACTIVE;
854 m_freem(m);
855 }
856
857 if (queued > 0) {
858 virtio_enqueue_commit(vsc, vq, -1, true);
859 ifp->if_timer = 5;
860 }
861
862 out:
863 VIOIF_TX_UNLOCK(sc);
864 }
865
866 static int
867 vioif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
868 {
869 int s, r;
870
871 s = splnet();
872
873 r = ether_ioctl(ifp, cmd, data);
874 if ((r == 0 && cmd == SIOCSIFFLAGS) ||
875 (r == ENETRESET && (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI))) {
876 if (ifp->if_flags & IFF_RUNNING)
877 r = vioif_rx_filter(ifp->if_softc);
878 else
879 r = 0;
880 }
881
882 splx(s);
883
884 return r;
885 }
886
887 void
888 vioif_watchdog(struct ifnet *ifp)
889 {
890 struct vioif_softc *sc = ifp->if_softc;
891
892 if (ifp->if_flags & IFF_RUNNING)
893 vioif_tx_vq_done(&sc->sc_vq[VQ_TX]);
894 }
895
896
897 /*
898 * Recieve implementation
899 */
900 /* allocate and initialize a mbuf for recieve */
901 static int
902 vioif_add_rx_mbuf(struct vioif_softc *sc, int i)
903 {
904 struct mbuf *m;
905 int r;
906
907 MGETHDR(m, M_DONTWAIT, MT_DATA);
908 if (m == NULL)
909 return ENOBUFS;
910 MCLGET(m, M_DONTWAIT);
911 if ((m->m_flags & M_EXT) == 0) {
912 m_freem(m);
913 return ENOBUFS;
914 }
915 sc->sc_rx_mbufs[i] = m;
916 m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
917 r = bus_dmamap_load_mbuf(sc->sc_virtio->sc_dmat,
918 sc->sc_rx_dmamaps[i],
919 m, BUS_DMA_READ|BUS_DMA_NOWAIT);
920 if (r) {
921 m_freem(m);
922 sc->sc_rx_mbufs[i] = 0;
923 return r;
924 }
925
926 return 0;
927 }
928
929 /* free a mbuf for recieve */
930 static void
931 vioif_free_rx_mbuf(struct vioif_softc *sc, int i)
932 {
933 bus_dmamap_unload(sc->sc_virtio->sc_dmat, sc->sc_rx_dmamaps[i]);
934 m_freem(sc->sc_rx_mbufs[i]);
935 sc->sc_rx_mbufs[i] = NULL;
936 }
937
938 /* add mbufs for all the empty recieve slots */
939 static void
940 vioif_populate_rx_mbufs(struct vioif_softc *sc)
941 {
942 VIOIF_RX_LOCK(sc);
943 vioif_populate_rx_mbufs_locked(sc);
944 VIOIF_RX_UNLOCK(sc);
945 }
946
947 static void
948 vioif_populate_rx_mbufs_locked(struct vioif_softc *sc)
949 {
950 struct virtio_softc *vsc = sc->sc_virtio;
951 int i, r, ndone = 0;
952 struct virtqueue *vq = &sc->sc_vq[VQ_RX];
953
954 KASSERT(VIOIF_RX_LOCKED(sc));
955
956 if (sc->sc_stopping)
957 return;
958
959 for (i = 0; i < vq->vq_num; i++) {
960 int slot;
961 r = virtio_enqueue_prep(vsc, vq, &slot);
962 if (r == EAGAIN)
963 break;
964 if (r != 0)
965 panic("enqueue_prep for rx buffers");
966 if (sc->sc_rx_mbufs[slot] == NULL) {
967 r = vioif_add_rx_mbuf(sc, slot);
968 if (r != 0) {
969 printf("%s: rx mbuf allocation failed, "
970 "error code %d\n",
971 device_xname(sc->sc_dev), r);
972 break;
973 }
974 }
975 r = virtio_enqueue_reserve(vsc, vq, slot,
976 sc->sc_rx_dmamaps[slot]->dm_nsegs + 1);
977 if (r != 0) {
978 vioif_free_rx_mbuf(sc, slot);
979 break;
980 }
981 bus_dmamap_sync(vsc->sc_dmat, sc->sc_rxhdr_dmamaps[slot],
982 0, sizeof(struct virtio_net_hdr), BUS_DMASYNC_PREREAD);
983 bus_dmamap_sync(vsc->sc_dmat, sc->sc_rx_dmamaps[slot],
984 0, MCLBYTES, BUS_DMASYNC_PREREAD);
985 virtio_enqueue(vsc, vq, slot, sc->sc_rxhdr_dmamaps[slot], false);
986 virtio_enqueue(vsc, vq, slot, sc->sc_rx_dmamaps[slot], false);
987 virtio_enqueue_commit(vsc, vq, slot, false);
988 ndone++;
989 }
990 if (ndone > 0)
991 virtio_enqueue_commit(vsc, vq, -1, true);
992 }
993
994 /* dequeue recieved packets */
995 static int
996 vioif_rx_deq(struct vioif_softc *sc)
997 {
998 int r;
999
1000 KASSERT(sc->sc_stopping);
1001
1002 VIOIF_RX_LOCK(sc);
1003 r = vioif_rx_deq_locked(sc);
1004 VIOIF_RX_UNLOCK(sc);
1005
1006 return r;
1007 }
1008
1009 /* dequeue recieved packets */
1010 static int
1011 vioif_rx_deq_locked(struct vioif_softc *sc)
1012 {
1013 struct virtio_softc *vsc = sc->sc_virtio;
1014 struct virtqueue *vq = &sc->sc_vq[VQ_RX];
1015 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1016 struct mbuf *m;
1017 int r = 0;
1018 int slot, len;
1019
1020 KASSERT(VIOIF_RX_LOCKED(sc));
1021
1022 while (virtio_dequeue(vsc, vq, &slot, &len) == 0) {
1023 len -= sizeof(struct virtio_net_hdr);
1024 r = 1;
1025 bus_dmamap_sync(vsc->sc_dmat, sc->sc_rxhdr_dmamaps[slot],
1026 0, sizeof(struct virtio_net_hdr),
1027 BUS_DMASYNC_POSTREAD);
1028 bus_dmamap_sync(vsc->sc_dmat, sc->sc_rx_dmamaps[slot],
1029 0, MCLBYTES,
1030 BUS_DMASYNC_POSTREAD);
1031 m = sc->sc_rx_mbufs[slot];
1032 KASSERT(m != NULL);
1033 bus_dmamap_unload(vsc->sc_dmat, sc->sc_rx_dmamaps[slot]);
1034 sc->sc_rx_mbufs[slot] = 0;
1035 virtio_dequeue_commit(vsc, vq, slot);
1036 m_set_rcvif(m, ifp);
1037 m->m_len = m->m_pkthdr.len = len;
1038 ifp->if_ipackets++;
1039 bpf_mtap(ifp, m);
1040
1041 VIOIF_RX_UNLOCK(sc);
1042 if_percpuq_enqueue(ifp->if_percpuq, m);
1043 VIOIF_RX_LOCK(sc);
1044
1045 if (sc->sc_stopping)
1046 break;
1047 }
1048
1049 return r;
1050 }
1051
1052 /* rx interrupt; call _dequeue above and schedule a softint */
1053 static int
1054 vioif_rx_vq_done(struct virtqueue *vq)
1055 {
1056 struct virtio_softc *vsc = vq->vq_owner;
1057 struct vioif_softc *sc = device_private(vsc->sc_child);
1058 int r = 0;
1059
1060 #ifdef VIOIF_SOFTINT_INTR
1061 KASSERT(!cpu_intr_p());
1062 #endif
1063
1064 VIOIF_RX_LOCK(sc);
1065
1066 if (sc->sc_stopping)
1067 goto out;
1068
1069 r = vioif_rx_deq_locked(sc);
1070 if (r)
1071 #ifdef VIOIF_SOFTINT_INTR
1072 vioif_populate_rx_mbufs_locked(sc);
1073 #else
1074 softint_schedule(sc->sc_rx_softint);
1075 #endif
1076
1077 out:
1078 VIOIF_RX_UNLOCK(sc);
1079 return r;
1080 }
1081
1082 /* softint: enqueue recieve requests for new incoming packets */
1083 static void
1084 vioif_rx_softint(void *arg)
1085 {
1086 struct vioif_softc *sc = arg;
1087
1088 vioif_populate_rx_mbufs(sc);
1089 }
1090
1091 /* free all the mbufs; called from if_stop(disable) */
1092 static void
1093 vioif_rx_drain(struct vioif_softc *sc)
1094 {
1095 struct virtqueue *vq = &sc->sc_vq[VQ_RX];
1096 int i;
1097
1098 for (i = 0; i < vq->vq_num; i++) {
1099 if (sc->sc_rx_mbufs[i] == NULL)
1100 continue;
1101 vioif_free_rx_mbuf(sc, i);
1102 }
1103 }
1104
1105
1106 /*
1107 * Transmition implementation
1108 */
1109 /* actual transmission is done in if_start */
1110 /* tx interrupt; dequeue and free mbufs */
1111 /*
1112 * tx interrupt is actually disabled; this should be called upon
1113 * tx vq full and watchdog
1114 */
1115 static int
1116 vioif_tx_vq_done(struct virtqueue *vq)
1117 {
1118 struct virtio_softc *vsc = vq->vq_owner;
1119 struct vioif_softc *sc = device_private(vsc->sc_child);
1120 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1121 int r = 0;
1122
1123 VIOIF_TX_LOCK(sc);
1124
1125 if (sc->sc_stopping)
1126 goto out;
1127
1128 r = vioif_tx_vq_done_locked(vq);
1129
1130 out:
1131 VIOIF_TX_UNLOCK(sc);
1132 if (r)
1133 vioif_start(ifp);
1134 return r;
1135 }
1136
1137 static int
1138 vioif_tx_vq_done_locked(struct virtqueue *vq)
1139 {
1140 struct virtio_softc *vsc = vq->vq_owner;
1141 struct vioif_softc *sc = device_private(vsc->sc_child);
1142 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1143 struct mbuf *m;
1144 int r = 0;
1145 int slot, len;
1146
1147 KASSERT(VIOIF_TX_LOCKED(sc));
1148
1149 while (virtio_dequeue(vsc, vq, &slot, &len) == 0) {
1150 r++;
1151 bus_dmamap_sync(vsc->sc_dmat, sc->sc_txhdr_dmamaps[slot],
1152 0, sizeof(struct virtio_net_hdr),
1153 BUS_DMASYNC_POSTWRITE);
1154 bus_dmamap_sync(vsc->sc_dmat, sc->sc_tx_dmamaps[slot],
1155 0, sc->sc_tx_dmamaps[slot]->dm_mapsize,
1156 BUS_DMASYNC_POSTWRITE);
1157 m = sc->sc_tx_mbufs[slot];
1158 bus_dmamap_unload(vsc->sc_dmat, sc->sc_tx_dmamaps[slot]);
1159 sc->sc_tx_mbufs[slot] = 0;
1160 virtio_dequeue_commit(vsc, vq, slot);
1161 ifp->if_opackets++;
1162 m_freem(m);
1163 }
1164
1165 if (r)
1166 ifp->if_flags &= ~IFF_OACTIVE;
1167 return r;
1168 }
1169
1170 /* free all the mbufs already put on vq; called from if_stop(disable) */
1171 static void
1172 vioif_tx_drain(struct vioif_softc *sc)
1173 {
1174 struct virtio_softc *vsc = sc->sc_virtio;
1175 struct virtqueue *vq = &sc->sc_vq[VQ_TX];
1176 int i;
1177
1178 KASSERT(sc->sc_stopping);
1179
1180 for (i = 0; i < vq->vq_num; i++) {
1181 if (sc->sc_tx_mbufs[i] == NULL)
1182 continue;
1183 bus_dmamap_unload(vsc->sc_dmat, sc->sc_tx_dmamaps[i]);
1184 m_freem(sc->sc_tx_mbufs[i]);
1185 sc->sc_tx_mbufs[i] = NULL;
1186 }
1187 }
1188
1189 /*
1190 * Control vq
1191 */
1192 /* issue a VIRTIO_NET_CTRL_RX class command and wait for completion */
1193 static int
1194 vioif_ctrl_rx(struct vioif_softc *sc, int cmd, bool onoff)
1195 {
1196 struct virtio_softc *vsc = sc->sc_virtio;
1197 struct virtqueue *vq = &sc->sc_vq[VQ_CTRL];
1198 int r, slot;
1199
1200 if (vsc->sc_nvqs < 3)
1201 return ENOTSUP;
1202
1203 mutex_enter(&sc->sc_ctrl_wait_lock);
1204 while (sc->sc_ctrl_inuse != FREE)
1205 cv_wait(&sc->sc_ctrl_wait, &sc->sc_ctrl_wait_lock);
1206 sc->sc_ctrl_inuse = INUSE;
1207 mutex_exit(&sc->sc_ctrl_wait_lock);
1208
1209 sc->sc_ctrl_cmd->class = VIRTIO_NET_CTRL_RX;
1210 sc->sc_ctrl_cmd->command = cmd;
1211 sc->sc_ctrl_rx->onoff = onoff;
1212
1213 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_cmd_dmamap,
1214 0, sizeof(struct virtio_net_ctrl_cmd),
1215 BUS_DMASYNC_PREWRITE);
1216 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_rx_dmamap,
1217 0, sizeof(struct virtio_net_ctrl_rx),
1218 BUS_DMASYNC_PREWRITE);
1219 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_status_dmamap,
1220 0, sizeof(struct virtio_net_ctrl_status),
1221 BUS_DMASYNC_PREREAD);
1222
1223 r = virtio_enqueue_prep(vsc, vq, &slot);
1224 if (r != 0)
1225 panic("%s: control vq busy!?", device_xname(sc->sc_dev));
1226 r = virtio_enqueue_reserve(vsc, vq, slot, 3);
1227 if (r != 0)
1228 panic("%s: control vq busy!?", device_xname(sc->sc_dev));
1229 virtio_enqueue(vsc, vq, slot, sc->sc_ctrl_cmd_dmamap, true);
1230 virtio_enqueue(vsc, vq, slot, sc->sc_ctrl_rx_dmamap, true);
1231 virtio_enqueue(vsc, vq, slot, sc->sc_ctrl_status_dmamap, false);
1232 virtio_enqueue_commit(vsc, vq, slot, true);
1233
1234 /* wait for done */
1235 mutex_enter(&sc->sc_ctrl_wait_lock);
1236 while (sc->sc_ctrl_inuse != DONE)
1237 cv_wait(&sc->sc_ctrl_wait, &sc->sc_ctrl_wait_lock);
1238 mutex_exit(&sc->sc_ctrl_wait_lock);
1239 /* already dequeueued */
1240
1241 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_cmd_dmamap, 0,
1242 sizeof(struct virtio_net_ctrl_cmd),
1243 BUS_DMASYNC_POSTWRITE);
1244 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_rx_dmamap, 0,
1245 sizeof(struct virtio_net_ctrl_rx),
1246 BUS_DMASYNC_POSTWRITE);
1247 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_status_dmamap, 0,
1248 sizeof(struct virtio_net_ctrl_status),
1249 BUS_DMASYNC_POSTREAD);
1250
1251 if (sc->sc_ctrl_status->ack == VIRTIO_NET_OK)
1252 r = 0;
1253 else {
1254 printf("%s: failed setting rx mode\n",
1255 device_xname(sc->sc_dev));
1256 r = EIO;
1257 }
1258
1259 mutex_enter(&sc->sc_ctrl_wait_lock);
1260 sc->sc_ctrl_inuse = FREE;
1261 cv_signal(&sc->sc_ctrl_wait);
1262 mutex_exit(&sc->sc_ctrl_wait_lock);
1263
1264 return r;
1265 }
1266
1267 static int
1268 vioif_set_promisc(struct vioif_softc *sc, bool onoff)
1269 {
1270 int r;
1271
1272 r = vioif_ctrl_rx(sc, VIRTIO_NET_CTRL_RX_PROMISC, onoff);
1273
1274 return r;
1275 }
1276
1277 static int
1278 vioif_set_allmulti(struct vioif_softc *sc, bool onoff)
1279 {
1280 int r;
1281
1282 r = vioif_ctrl_rx(sc, VIRTIO_NET_CTRL_RX_ALLMULTI, onoff);
1283
1284 return r;
1285 }
1286
1287 /* issue VIRTIO_NET_CTRL_MAC_TABLE_SET command and wait for completion */
1288 static int
1289 vioif_set_rx_filter(struct vioif_softc *sc)
1290 {
1291 /* filter already set in sc_ctrl_mac_tbl */
1292 struct virtio_softc *vsc = sc->sc_virtio;
1293 struct virtqueue *vq = &sc->sc_vq[VQ_CTRL];
1294 int r, slot;
1295
1296 if (vsc->sc_nvqs < 3)
1297 return ENOTSUP;
1298
1299 mutex_enter(&sc->sc_ctrl_wait_lock);
1300 while (sc->sc_ctrl_inuse != FREE)
1301 cv_wait(&sc->sc_ctrl_wait, &sc->sc_ctrl_wait_lock);
1302 sc->sc_ctrl_inuse = INUSE;
1303 mutex_exit(&sc->sc_ctrl_wait_lock);
1304
1305 sc->sc_ctrl_cmd->class = VIRTIO_NET_CTRL_MAC;
1306 sc->sc_ctrl_cmd->command = VIRTIO_NET_CTRL_MAC_TABLE_SET;
1307
1308 r = bus_dmamap_load(vsc->sc_dmat, sc->sc_ctrl_tbl_uc_dmamap,
1309 sc->sc_ctrl_mac_tbl_uc,
1310 (sizeof(struct virtio_net_ctrl_mac_tbl)
1311 + ETHER_ADDR_LEN * sc->sc_ctrl_mac_tbl_uc->nentries),
1312 NULL, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
1313 if (r) {
1314 printf("%s: control command dmamap load failed, "
1315 "error code %d\n", device_xname(sc->sc_dev), r);
1316 goto out;
1317 }
1318 r = bus_dmamap_load(vsc->sc_dmat, sc->sc_ctrl_tbl_mc_dmamap,
1319 sc->sc_ctrl_mac_tbl_mc,
1320 (sizeof(struct virtio_net_ctrl_mac_tbl)
1321 + ETHER_ADDR_LEN * sc->sc_ctrl_mac_tbl_mc->nentries),
1322 NULL, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
1323 if (r) {
1324 printf("%s: control command dmamap load failed, "
1325 "error code %d\n", device_xname(sc->sc_dev), r);
1326 bus_dmamap_unload(vsc->sc_dmat, sc->sc_ctrl_tbl_uc_dmamap);
1327 goto out;
1328 }
1329
1330 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_cmd_dmamap,
1331 0, sizeof(struct virtio_net_ctrl_cmd),
1332 BUS_DMASYNC_PREWRITE);
1333 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_tbl_uc_dmamap, 0,
1334 (sizeof(struct virtio_net_ctrl_mac_tbl)
1335 + ETHER_ADDR_LEN * sc->sc_ctrl_mac_tbl_uc->nentries),
1336 BUS_DMASYNC_PREWRITE);
1337 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_tbl_mc_dmamap, 0,
1338 (sizeof(struct virtio_net_ctrl_mac_tbl)
1339 + ETHER_ADDR_LEN * sc->sc_ctrl_mac_tbl_mc->nentries),
1340 BUS_DMASYNC_PREWRITE);
1341 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_status_dmamap,
1342 0, sizeof(struct virtio_net_ctrl_status),
1343 BUS_DMASYNC_PREREAD);
1344
1345 r = virtio_enqueue_prep(vsc, vq, &slot);
1346 if (r != 0)
1347 panic("%s: control vq busy!?", device_xname(sc->sc_dev));
1348 r = virtio_enqueue_reserve(vsc, vq, slot, 4);
1349 if (r != 0)
1350 panic("%s: control vq busy!?", device_xname(sc->sc_dev));
1351 virtio_enqueue(vsc, vq, slot, sc->sc_ctrl_cmd_dmamap, true);
1352 virtio_enqueue(vsc, vq, slot, sc->sc_ctrl_tbl_uc_dmamap, true);
1353 virtio_enqueue(vsc, vq, slot, sc->sc_ctrl_tbl_mc_dmamap, true);
1354 virtio_enqueue(vsc, vq, slot, sc->sc_ctrl_status_dmamap, false);
1355 virtio_enqueue_commit(vsc, vq, slot, true);
1356
1357 /* wait for done */
1358 mutex_enter(&sc->sc_ctrl_wait_lock);
1359 while (sc->sc_ctrl_inuse != DONE)
1360 cv_wait(&sc->sc_ctrl_wait, &sc->sc_ctrl_wait_lock);
1361 mutex_exit(&sc->sc_ctrl_wait_lock);
1362 /* already dequeueued */
1363
1364 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_cmd_dmamap, 0,
1365 sizeof(struct virtio_net_ctrl_cmd),
1366 BUS_DMASYNC_POSTWRITE);
1367 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_tbl_uc_dmamap, 0,
1368 (sizeof(struct virtio_net_ctrl_mac_tbl)
1369 + ETHER_ADDR_LEN * sc->sc_ctrl_mac_tbl_uc->nentries),
1370 BUS_DMASYNC_POSTWRITE);
1371 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_tbl_mc_dmamap, 0,
1372 (sizeof(struct virtio_net_ctrl_mac_tbl)
1373 + ETHER_ADDR_LEN * sc->sc_ctrl_mac_tbl_mc->nentries),
1374 BUS_DMASYNC_POSTWRITE);
1375 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_status_dmamap, 0,
1376 sizeof(struct virtio_net_ctrl_status),
1377 BUS_DMASYNC_POSTREAD);
1378 bus_dmamap_unload(vsc->sc_dmat, sc->sc_ctrl_tbl_uc_dmamap);
1379 bus_dmamap_unload(vsc->sc_dmat, sc->sc_ctrl_tbl_mc_dmamap);
1380
1381 if (sc->sc_ctrl_status->ack == VIRTIO_NET_OK)
1382 r = 0;
1383 else {
1384 printf("%s: failed setting rx filter\n",
1385 device_xname(sc->sc_dev));
1386 r = EIO;
1387 }
1388
1389 out:
1390 mutex_enter(&sc->sc_ctrl_wait_lock);
1391 sc->sc_ctrl_inuse = FREE;
1392 cv_signal(&sc->sc_ctrl_wait);
1393 mutex_exit(&sc->sc_ctrl_wait_lock);
1394
1395 return r;
1396 }
1397
1398 /* ctrl vq interrupt; wake up the command issuer */
1399 static int
1400 vioif_ctrl_vq_done(struct virtqueue *vq)
1401 {
1402 struct virtio_softc *vsc = vq->vq_owner;
1403 struct vioif_softc *sc = device_private(vsc->sc_child);
1404 int r, slot;
1405
1406 r = virtio_dequeue(vsc, vq, &slot, NULL);
1407 if (r == ENOENT)
1408 return 0;
1409 virtio_dequeue_commit(vsc, vq, slot);
1410
1411 mutex_enter(&sc->sc_ctrl_wait_lock);
1412 sc->sc_ctrl_inuse = DONE;
1413 cv_signal(&sc->sc_ctrl_wait);
1414 mutex_exit(&sc->sc_ctrl_wait_lock);
1415
1416 return 1;
1417 }
1418
1419 /*
1420 * If IFF_PROMISC requested, set promiscuous
1421 * If multicast filter small enough (<=MAXENTRIES) set rx filter
1422 * If large multicast filter exist use ALLMULTI
1423 */
1424 /*
1425 * If setting rx filter fails fall back to ALLMULTI
1426 * If ALLMULTI fails fall back to PROMISC
1427 */
1428 static int
1429 vioif_rx_filter(struct vioif_softc *sc)
1430 {
1431 struct virtio_softc *vsc = sc->sc_virtio;
1432 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1433 struct ether_multi *enm;
1434 struct ether_multistep step;
1435 int nentries;
1436 int promisc = 0, allmulti = 0, rxfilter = 0;
1437 int r;
1438
1439 if (vsc->sc_nvqs < 3) { /* no ctrl vq; always promisc */
1440 ifp->if_flags |= IFF_PROMISC;
1441 return 0;
1442 }
1443
1444 if (ifp->if_flags & IFF_PROMISC) {
1445 promisc = 1;
1446 goto set;
1447 }
1448
1449 nentries = -1;
1450 ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
1451 while (nentries++, enm != NULL) {
1452 if (nentries >= VIRTIO_NET_CTRL_MAC_MAXENTRIES) {
1453 allmulti = 1;
1454 goto set;
1455 }
1456 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
1457 ETHER_ADDR_LEN)) {
1458 allmulti = 1;
1459 goto set;
1460 }
1461 memcpy(sc->sc_ctrl_mac_tbl_mc->macs[nentries],
1462 enm->enm_addrlo, ETHER_ADDR_LEN);
1463 ETHER_NEXT_MULTI(step, enm);
1464 }
1465 rxfilter = 1;
1466
1467 set:
1468 if (rxfilter) {
1469 sc->sc_ctrl_mac_tbl_uc->nentries = 0;
1470 sc->sc_ctrl_mac_tbl_mc->nentries = nentries;
1471 r = vioif_set_rx_filter(sc);
1472 if (r != 0) {
1473 rxfilter = 0;
1474 allmulti = 1; /* fallback */
1475 }
1476 } else {
1477 /* remove rx filter */
1478 sc->sc_ctrl_mac_tbl_uc->nentries = 0;
1479 sc->sc_ctrl_mac_tbl_mc->nentries = 0;
1480 r = vioif_set_rx_filter(sc);
1481 /* what to do on failure? */
1482 }
1483 if (allmulti) {
1484 r = vioif_set_allmulti(sc, true);
1485 if (r != 0) {
1486 allmulti = 0;
1487 promisc = 1; /* fallback */
1488 }
1489 } else {
1490 r = vioif_set_allmulti(sc, false);
1491 /* what to do on failure? */
1492 }
1493 if (promisc) {
1494 r = vioif_set_promisc(sc, true);
1495 } else {
1496 r = vioif_set_promisc(sc, false);
1497 }
1498
1499 return r;
1500 }
1501
1502 /* change link status */
1503 static int
1504 vioif_updown(struct vioif_softc *sc, bool isup)
1505 {
1506 struct virtio_softc *vsc = sc->sc_virtio;
1507
1508 if (!(vsc->sc_features & VIRTIO_NET_F_STATUS))
1509 return ENODEV;
1510 virtio_write_device_config_1(vsc,
1511 VIRTIO_NET_CONFIG_STATUS,
1512 isup?VIRTIO_NET_S_LINK_UP:0);
1513 return 0;
1514 }
1515
1516 MODULE(MODULE_CLASS_DRIVER, if_vioif, "virtio");
1517
1518 #ifdef _MODULE
1519 #include "ioconf.c"
1520 #endif
1521
1522 static int
1523 if_vioif_modcmd(modcmd_t cmd, void *opaque)
1524 {
1525 int error = 0;
1526
1527 #ifdef _MODULE
1528 switch (cmd) {
1529 case MODULE_CMD_INIT:
1530 error = config_init_component(cfdriver_ioconf_if_vioif,
1531 cfattach_ioconf_if_vioif, cfdata_ioconf_if_vioif);
1532 break;
1533 case MODULE_CMD_FINI:
1534 error = config_fini_component(cfdriver_ioconf_if_vioif,
1535 cfattach_ioconf_if_vioif, cfdata_ioconf_if_vioif);
1536 break;
1537 default:
1538 error = ENOTTY;
1539 break;
1540 }
1541 #endif
1542
1543 return error;
1544 }
1545