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