if_vioif.c revision 1.7.2.1 1 /* $NetBSD: if_vioif.c,v 1.7.2.1 2014/12/29 17:01:01 martin 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.7.2.1 2014/12/29 17:01:01 martin 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_ifflags;
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 if (vsc->sc_nvqs == 3)
606 config_interrupts(self, vioif_deferred_init);
607
608 strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
609 ifp->if_softc = sc;
610 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
611 ifp->if_start = vioif_start;
612 ifp->if_ioctl = vioif_ioctl;
613 ifp->if_init = vioif_init;
614 ifp->if_stop = vioif_stop;
615 ifp->if_capabilities = 0;
616 ifp->if_watchdog = vioif_watchdog;
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 r = vioif_set_promisc(sc, false);
656 if (r != 0)
657 aprint_error_dev(self, "resetting promisc mode failed, "
658 "errror code %d\n", r);
659 else
660 ifp->if_flags &= ~IFF_PROMISC;
661 }
662
663 /*
664 * Interface functions for ifnet
665 */
666 static int
667 vioif_init(struct ifnet *ifp)
668 {
669 struct vioif_softc *sc = ifp->if_softc;
670
671 vioif_stop(ifp, 0);
672
673 /* Have to set false before vioif_populate_rx_mbufs */
674 sc->sc_stopping = false;
675
676 vioif_populate_rx_mbufs(sc);
677
678 vioif_updown(sc, true);
679 ifp->if_flags |= IFF_RUNNING;
680 ifp->if_flags &= ~IFF_OACTIVE;
681 vioif_rx_filter(sc);
682
683 return 0;
684 }
685
686 static void
687 vioif_stop(struct ifnet *ifp, int disable)
688 {
689 struct vioif_softc *sc = ifp->if_softc;
690 struct virtio_softc *vsc = sc->sc_virtio;
691
692 sc->sc_stopping = true;
693
694 /* only way to stop I/O and DMA is resetting... */
695 virtio_reset(vsc);
696 vioif_rx_deq(sc);
697 vioif_tx_drain(sc);
698 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
699
700 if (disable)
701 vioif_rx_drain(sc);
702
703 virtio_reinit_start(vsc);
704 virtio_negotiate_features(vsc, vsc->sc_features);
705 virtio_start_vq_intr(vsc, &sc->sc_vq[0]);
706 virtio_stop_vq_intr(vsc, &sc->sc_vq[1]);
707 if (vsc->sc_nvqs >= 3)
708 virtio_start_vq_intr(vsc, &sc->sc_vq[2]);
709 virtio_reinit_end(vsc);
710 vioif_updown(sc, false);
711 }
712
713 static void
714 vioif_start(struct ifnet *ifp)
715 {
716 struct vioif_softc *sc = ifp->if_softc;
717 struct virtio_softc *vsc = sc->sc_virtio;
718 struct virtqueue *vq = &sc->sc_vq[1]; /* tx vq */
719 struct mbuf *m;
720 int queued = 0, retry = 0;
721
722 VIOIF_TX_LOCK(sc);
723
724 if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
725 goto out;
726
727 if (sc->sc_stopping)
728 goto out;
729
730 for (;;) {
731 int slot, r;
732
733 IFQ_DEQUEUE(&ifp->if_snd, m);
734
735 if (m == NULL)
736 break;
737
738 retry:
739 r = virtio_enqueue_prep(vsc, vq, &slot);
740 if (r == EAGAIN) {
741 ifp->if_flags |= IFF_OACTIVE;
742 vioif_tx_vq_done_locked(vq);
743 if (retry++ == 0)
744 goto retry;
745 else
746 break;
747 }
748 if (r != 0)
749 panic("enqueue_prep for a tx buffer");
750 r = bus_dmamap_load_mbuf(vsc->sc_dmat,
751 sc->sc_tx_dmamaps[slot],
752 m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
753 if (r != 0) {
754 virtio_enqueue_abort(vsc, vq, slot);
755 printf("%s: tx dmamap load failed, error code %d\n",
756 device_xname(sc->sc_dev), r);
757 break;
758 }
759 r = virtio_enqueue_reserve(vsc, vq, slot,
760 sc->sc_tx_dmamaps[slot]->dm_nsegs + 1);
761 if (r != 0) {
762 bus_dmamap_unload(vsc->sc_dmat,
763 sc->sc_tx_dmamaps[slot]);
764 ifp->if_flags |= IFF_OACTIVE;
765 vioif_tx_vq_done_locked(vq);
766 if (retry++ == 0)
767 goto retry;
768 else
769 break;
770 }
771
772 sc->sc_tx_mbufs[slot] = m;
773
774 memset(&sc->sc_tx_hdrs[slot], 0, sizeof(struct virtio_net_hdr));
775 bus_dmamap_sync(vsc->sc_dmat, sc->sc_tx_dmamaps[slot],
776 0, sc->sc_tx_dmamaps[slot]->dm_mapsize,
777 BUS_DMASYNC_PREWRITE);
778 bus_dmamap_sync(vsc->sc_dmat, sc->sc_txhdr_dmamaps[slot],
779 0, sc->sc_txhdr_dmamaps[slot]->dm_mapsize,
780 BUS_DMASYNC_PREWRITE);
781 virtio_enqueue(vsc, vq, slot, sc->sc_txhdr_dmamaps[slot], true);
782 virtio_enqueue(vsc, vq, slot, sc->sc_tx_dmamaps[slot], true);
783 virtio_enqueue_commit(vsc, vq, slot, false);
784 queued++;
785 bpf_mtap(ifp, m);
786 }
787
788 if (m != NULL) {
789 ifp->if_flags |= IFF_OACTIVE;
790 m_freem(m);
791 }
792
793 if (queued > 0) {
794 virtio_enqueue_commit(vsc, vq, -1, true);
795 ifp->if_timer = 5;
796 }
797
798 out:
799 VIOIF_TX_UNLOCK(sc);
800 }
801
802 static int
803 vioif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
804 {
805 int s, r;
806
807 s = splnet();
808
809 r = ether_ioctl(ifp, cmd, data);
810 if ((r == 0 && cmd == SIOCSIFFLAGS) ||
811 (r == ENETRESET && (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI))) {
812 if (ifp->if_flags & IFF_RUNNING)
813 r = vioif_rx_filter(ifp->if_softc);
814 else
815 r = 0;
816 }
817
818 splx(s);
819
820 return r;
821 }
822
823 void
824 vioif_watchdog(struct ifnet *ifp)
825 {
826 struct vioif_softc *sc = ifp->if_softc;
827
828 if (ifp->if_flags & IFF_RUNNING)
829 vioif_tx_vq_done(&sc->sc_vq[1]);
830 }
831
832
833 /*
834 * Recieve implementation
835 */
836 /* allocate and initialize a mbuf for recieve */
837 static int
838 vioif_add_rx_mbuf(struct vioif_softc *sc, int i)
839 {
840 struct mbuf *m;
841 int r;
842
843 MGETHDR(m, M_DONTWAIT, MT_DATA);
844 if (m == NULL)
845 return ENOBUFS;
846 MCLGET(m, M_DONTWAIT);
847 if ((m->m_flags & M_EXT) == 0) {
848 m_freem(m);
849 return ENOBUFS;
850 }
851 sc->sc_rx_mbufs[i] = m;
852 m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
853 r = bus_dmamap_load_mbuf(sc->sc_virtio->sc_dmat,
854 sc->sc_rx_dmamaps[i],
855 m, BUS_DMA_READ|BUS_DMA_NOWAIT);
856 if (r) {
857 m_freem(m);
858 sc->sc_rx_mbufs[i] = 0;
859 return r;
860 }
861
862 return 0;
863 }
864
865 /* free a mbuf for recieve */
866 static void
867 vioif_free_rx_mbuf(struct vioif_softc *sc, int i)
868 {
869 bus_dmamap_unload(sc->sc_virtio->sc_dmat, sc->sc_rx_dmamaps[i]);
870 m_freem(sc->sc_rx_mbufs[i]);
871 sc->sc_rx_mbufs[i] = NULL;
872 }
873
874 /* add mbufs for all the empty recieve slots */
875 static void
876 vioif_populate_rx_mbufs(struct vioif_softc *sc)
877 {
878 struct virtio_softc *vsc = sc->sc_virtio;
879 int i, r, ndone = 0;
880 struct virtqueue *vq = &sc->sc_vq[0]; /* rx vq */
881
882 VIOIF_RX_LOCK(sc);
883
884 if (sc->sc_stopping)
885 goto out;
886
887 for (i = 0; i < vq->vq_num; i++) {
888 int slot;
889 r = virtio_enqueue_prep(vsc, vq, &slot);
890 if (r == EAGAIN)
891 break;
892 if (r != 0)
893 panic("enqueue_prep for rx buffers");
894 if (sc->sc_rx_mbufs[slot] == NULL) {
895 r = vioif_add_rx_mbuf(sc, slot);
896 if (r != 0) {
897 printf("%s: rx mbuf allocation failed, "
898 "error code %d\n",
899 device_xname(sc->sc_dev), r);
900 break;
901 }
902 }
903 r = virtio_enqueue_reserve(vsc, vq, slot,
904 sc->sc_rx_dmamaps[slot]->dm_nsegs + 1);
905 if (r != 0) {
906 vioif_free_rx_mbuf(sc, slot);
907 break;
908 }
909 bus_dmamap_sync(vsc->sc_dmat, sc->sc_rxhdr_dmamaps[slot],
910 0, sizeof(struct virtio_net_hdr), BUS_DMASYNC_PREREAD);
911 bus_dmamap_sync(vsc->sc_dmat, sc->sc_rx_dmamaps[slot],
912 0, MCLBYTES, BUS_DMASYNC_PREREAD);
913 virtio_enqueue(vsc, vq, slot, sc->sc_rxhdr_dmamaps[slot], false);
914 virtio_enqueue(vsc, vq, slot, sc->sc_rx_dmamaps[slot], false);
915 virtio_enqueue_commit(vsc, vq, slot, false);
916 ndone++;
917 }
918 if (ndone > 0)
919 virtio_enqueue_commit(vsc, vq, -1, true);
920
921 out:
922 VIOIF_RX_UNLOCK(sc);
923 }
924
925 /* dequeue recieved packets */
926 static int
927 vioif_rx_deq(struct vioif_softc *sc)
928 {
929 int r;
930
931 KASSERT(sc->sc_stopping);
932
933 VIOIF_RX_LOCK(sc);
934 r = vioif_rx_deq_locked(sc);
935 VIOIF_RX_UNLOCK(sc);
936
937 return r;
938 }
939
940 /* dequeue recieved packets */
941 static int
942 vioif_rx_deq_locked(struct vioif_softc *sc)
943 {
944 struct virtio_softc *vsc = sc->sc_virtio;
945 struct virtqueue *vq = &sc->sc_vq[0];
946 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
947 struct mbuf *m;
948 int r = 0;
949 int slot, len;
950
951 KASSERT(VIOIF_RX_LOCKED(sc));
952
953 while (virtio_dequeue(vsc, vq, &slot, &len) == 0) {
954 len -= sizeof(struct virtio_net_hdr);
955 r = 1;
956 bus_dmamap_sync(vsc->sc_dmat, sc->sc_rxhdr_dmamaps[slot],
957 0, sizeof(struct virtio_net_hdr),
958 BUS_DMASYNC_POSTREAD);
959 bus_dmamap_sync(vsc->sc_dmat, sc->sc_rx_dmamaps[slot],
960 0, MCLBYTES,
961 BUS_DMASYNC_POSTREAD);
962 m = sc->sc_rx_mbufs[slot];
963 KASSERT(m != NULL);
964 bus_dmamap_unload(vsc->sc_dmat, sc->sc_rx_dmamaps[slot]);
965 sc->sc_rx_mbufs[slot] = 0;
966 virtio_dequeue_commit(vsc, vq, slot);
967 m->m_pkthdr.rcvif = ifp;
968 m->m_len = m->m_pkthdr.len = len;
969 ifp->if_ipackets++;
970 bpf_mtap(ifp, m);
971
972 VIOIF_RX_UNLOCK(sc);
973 (*ifp->if_input)(ifp, m);
974 VIOIF_RX_LOCK(sc);
975
976 if (sc->sc_stopping)
977 break;
978 }
979
980 return r;
981 }
982
983 /* rx interrupt; call _dequeue above and schedule a softint */
984 static int
985 vioif_rx_vq_done(struct virtqueue *vq)
986 {
987 struct virtio_softc *vsc = vq->vq_owner;
988 struct vioif_softc *sc = device_private(vsc->sc_child);
989 int r = 0;
990
991 VIOIF_RX_LOCK(sc);
992
993 if (sc->sc_stopping)
994 goto out;
995
996 r = vioif_rx_deq_locked(sc);
997 if (r)
998 softint_schedule(sc->sc_rx_softint);
999
1000 out:
1001 VIOIF_RX_UNLOCK(sc);
1002 return r;
1003 }
1004
1005 /* softint: enqueue recieve requests for new incoming packets */
1006 static void
1007 vioif_rx_softint(void *arg)
1008 {
1009 struct vioif_softc *sc = arg;
1010
1011 vioif_populate_rx_mbufs(sc);
1012 }
1013
1014 /* free all the mbufs; called from if_stop(disable) */
1015 static void
1016 vioif_rx_drain(struct vioif_softc *sc)
1017 {
1018 struct virtqueue *vq = &sc->sc_vq[0];
1019 int i;
1020
1021 for (i = 0; i < vq->vq_num; i++) {
1022 if (sc->sc_rx_mbufs[i] == NULL)
1023 continue;
1024 vioif_free_rx_mbuf(sc, i);
1025 }
1026 }
1027
1028
1029 /*
1030 * Transmition implementation
1031 */
1032 /* actual transmission is done in if_start */
1033 /* tx interrupt; dequeue and free mbufs */
1034 /*
1035 * tx interrupt is actually disabled; this should be called upon
1036 * tx vq full and watchdog
1037 */
1038 static int
1039 vioif_tx_vq_done(struct virtqueue *vq)
1040 {
1041 struct virtio_softc *vsc = vq->vq_owner;
1042 struct vioif_softc *sc = device_private(vsc->sc_child);
1043 int r = 0;
1044
1045 VIOIF_TX_LOCK(sc);
1046
1047 if (sc->sc_stopping)
1048 goto out;
1049
1050 r = vioif_tx_vq_done_locked(vq);
1051
1052 out:
1053 VIOIF_TX_UNLOCK(sc);
1054 return r;
1055 }
1056
1057 static int
1058 vioif_tx_vq_done_locked(struct virtqueue *vq)
1059 {
1060 struct virtio_softc *vsc = vq->vq_owner;
1061 struct vioif_softc *sc = device_private(vsc->sc_child);
1062 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1063 struct mbuf *m;
1064 int r = 0;
1065 int slot, len;
1066
1067 KASSERT(VIOIF_TX_LOCKED(sc));
1068
1069 while (virtio_dequeue(vsc, vq, &slot, &len) == 0) {
1070 r++;
1071 bus_dmamap_sync(vsc->sc_dmat, sc->sc_txhdr_dmamaps[slot],
1072 0, sizeof(struct virtio_net_hdr),
1073 BUS_DMASYNC_POSTWRITE);
1074 bus_dmamap_sync(vsc->sc_dmat, sc->sc_tx_dmamaps[slot],
1075 0, sc->sc_tx_dmamaps[slot]->dm_mapsize,
1076 BUS_DMASYNC_POSTWRITE);
1077 m = sc->sc_tx_mbufs[slot];
1078 bus_dmamap_unload(vsc->sc_dmat, sc->sc_tx_dmamaps[slot]);
1079 sc->sc_tx_mbufs[slot] = 0;
1080 virtio_dequeue_commit(vsc, vq, slot);
1081 ifp->if_opackets++;
1082 m_freem(m);
1083 }
1084
1085 if (r)
1086 ifp->if_flags &= ~IFF_OACTIVE;
1087 return r;
1088 }
1089
1090 /* free all the mbufs already put on vq; called from if_stop(disable) */
1091 static void
1092 vioif_tx_drain(struct vioif_softc *sc)
1093 {
1094 struct virtio_softc *vsc = sc->sc_virtio;
1095 struct virtqueue *vq = &sc->sc_vq[1];
1096 int i;
1097
1098 KASSERT(sc->sc_stopping);
1099
1100 for (i = 0; i < vq->vq_num; i++) {
1101 if (sc->sc_tx_mbufs[i] == NULL)
1102 continue;
1103 bus_dmamap_unload(vsc->sc_dmat, sc->sc_tx_dmamaps[i]);
1104 m_freem(sc->sc_tx_mbufs[i]);
1105 sc->sc_tx_mbufs[i] = NULL;
1106 }
1107 }
1108
1109 /*
1110 * Control vq
1111 */
1112 /* issue a VIRTIO_NET_CTRL_RX class command and wait for completion */
1113 static int
1114 vioif_ctrl_rx(struct vioif_softc *sc, int cmd, bool onoff)
1115 {
1116 struct virtio_softc *vsc = sc->sc_virtio;
1117 struct virtqueue *vq = &sc->sc_vq[2];
1118 int r, slot;
1119
1120 if (vsc->sc_nvqs < 3)
1121 return ENOTSUP;
1122
1123 mutex_enter(&sc->sc_ctrl_wait_lock);
1124 while (sc->sc_ctrl_inuse != FREE)
1125 cv_wait(&sc->sc_ctrl_wait, &sc->sc_ctrl_wait_lock);
1126 sc->sc_ctrl_inuse = INUSE;
1127 mutex_exit(&sc->sc_ctrl_wait_lock);
1128
1129 sc->sc_ctrl_cmd->class = VIRTIO_NET_CTRL_RX;
1130 sc->sc_ctrl_cmd->command = cmd;
1131 sc->sc_ctrl_rx->onoff = onoff;
1132
1133 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_cmd_dmamap,
1134 0, sizeof(struct virtio_net_ctrl_cmd),
1135 BUS_DMASYNC_PREWRITE);
1136 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_rx_dmamap,
1137 0, sizeof(struct virtio_net_ctrl_rx),
1138 BUS_DMASYNC_PREWRITE);
1139 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_status_dmamap,
1140 0, sizeof(struct virtio_net_ctrl_status),
1141 BUS_DMASYNC_PREREAD);
1142
1143 r = virtio_enqueue_prep(vsc, vq, &slot);
1144 if (r != 0)
1145 panic("%s: control vq busy!?", device_xname(sc->sc_dev));
1146 r = virtio_enqueue_reserve(vsc, vq, slot, 3);
1147 if (r != 0)
1148 panic("%s: control vq busy!?", device_xname(sc->sc_dev));
1149 virtio_enqueue(vsc, vq, slot, sc->sc_ctrl_cmd_dmamap, true);
1150 virtio_enqueue(vsc, vq, slot, sc->sc_ctrl_rx_dmamap, true);
1151 virtio_enqueue(vsc, vq, slot, sc->sc_ctrl_status_dmamap, false);
1152 virtio_enqueue_commit(vsc, vq, slot, true);
1153
1154 /* wait for done */
1155 mutex_enter(&sc->sc_ctrl_wait_lock);
1156 while (sc->sc_ctrl_inuse != DONE)
1157 cv_wait(&sc->sc_ctrl_wait, &sc->sc_ctrl_wait_lock);
1158 mutex_exit(&sc->sc_ctrl_wait_lock);
1159 /* already dequeueued */
1160
1161 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_cmd_dmamap, 0,
1162 sizeof(struct virtio_net_ctrl_cmd),
1163 BUS_DMASYNC_POSTWRITE);
1164 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_rx_dmamap, 0,
1165 sizeof(struct virtio_net_ctrl_rx),
1166 BUS_DMASYNC_POSTWRITE);
1167 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_status_dmamap, 0,
1168 sizeof(struct virtio_net_ctrl_status),
1169 BUS_DMASYNC_POSTREAD);
1170
1171 if (sc->sc_ctrl_status->ack == VIRTIO_NET_OK)
1172 r = 0;
1173 else {
1174 printf("%s: failed setting rx mode\n",
1175 device_xname(sc->sc_dev));
1176 r = EIO;
1177 }
1178
1179 mutex_enter(&sc->sc_ctrl_wait_lock);
1180 sc->sc_ctrl_inuse = FREE;
1181 cv_signal(&sc->sc_ctrl_wait);
1182 mutex_exit(&sc->sc_ctrl_wait_lock);
1183
1184 return r;
1185 }
1186
1187 static int
1188 vioif_set_promisc(struct vioif_softc *sc, bool onoff)
1189 {
1190 int r;
1191
1192 r = vioif_ctrl_rx(sc, VIRTIO_NET_CTRL_RX_PROMISC, onoff);
1193
1194 return r;
1195 }
1196
1197 static int
1198 vioif_set_allmulti(struct vioif_softc *sc, bool onoff)
1199 {
1200 int r;
1201
1202 r = vioif_ctrl_rx(sc, VIRTIO_NET_CTRL_RX_ALLMULTI, onoff);
1203
1204 return r;
1205 }
1206
1207 /* issue VIRTIO_NET_CTRL_MAC_TABLE_SET command and wait for completion */
1208 static int
1209 vioif_set_rx_filter(struct vioif_softc *sc)
1210 {
1211 /* filter already set in sc_ctrl_mac_tbl */
1212 struct virtio_softc *vsc = sc->sc_virtio;
1213 struct virtqueue *vq = &sc->sc_vq[2];
1214 int r, slot;
1215
1216 if (vsc->sc_nvqs < 3)
1217 return ENOTSUP;
1218
1219 mutex_enter(&sc->sc_ctrl_wait_lock);
1220 while (sc->sc_ctrl_inuse != FREE)
1221 cv_wait(&sc->sc_ctrl_wait, &sc->sc_ctrl_wait_lock);
1222 sc->sc_ctrl_inuse = INUSE;
1223 mutex_exit(&sc->sc_ctrl_wait_lock);
1224
1225 sc->sc_ctrl_cmd->class = VIRTIO_NET_CTRL_MAC;
1226 sc->sc_ctrl_cmd->command = VIRTIO_NET_CTRL_MAC_TABLE_SET;
1227
1228 r = bus_dmamap_load(vsc->sc_dmat, sc->sc_ctrl_tbl_uc_dmamap,
1229 sc->sc_ctrl_mac_tbl_uc,
1230 (sizeof(struct virtio_net_ctrl_mac_tbl)
1231 + ETHER_ADDR_LEN * sc->sc_ctrl_mac_tbl_uc->nentries),
1232 NULL, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
1233 if (r) {
1234 printf("%s: control command dmamap load failed, "
1235 "error code %d\n", device_xname(sc->sc_dev), r);
1236 goto out;
1237 }
1238 r = bus_dmamap_load(vsc->sc_dmat, sc->sc_ctrl_tbl_mc_dmamap,
1239 sc->sc_ctrl_mac_tbl_mc,
1240 (sizeof(struct virtio_net_ctrl_mac_tbl)
1241 + ETHER_ADDR_LEN * sc->sc_ctrl_mac_tbl_mc->nentries),
1242 NULL, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
1243 if (r) {
1244 printf("%s: control command dmamap load failed, "
1245 "error code %d\n", device_xname(sc->sc_dev), r);
1246 bus_dmamap_unload(vsc->sc_dmat, sc->sc_ctrl_tbl_uc_dmamap);
1247 goto out;
1248 }
1249
1250 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_cmd_dmamap,
1251 0, sizeof(struct virtio_net_ctrl_cmd),
1252 BUS_DMASYNC_PREWRITE);
1253 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_tbl_uc_dmamap, 0,
1254 (sizeof(struct virtio_net_ctrl_mac_tbl)
1255 + ETHER_ADDR_LEN * sc->sc_ctrl_mac_tbl_uc->nentries),
1256 BUS_DMASYNC_PREWRITE);
1257 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_tbl_mc_dmamap, 0,
1258 (sizeof(struct virtio_net_ctrl_mac_tbl)
1259 + ETHER_ADDR_LEN * sc->sc_ctrl_mac_tbl_mc->nentries),
1260 BUS_DMASYNC_PREWRITE);
1261 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_status_dmamap,
1262 0, sizeof(struct virtio_net_ctrl_status),
1263 BUS_DMASYNC_PREREAD);
1264
1265 r = virtio_enqueue_prep(vsc, vq, &slot);
1266 if (r != 0)
1267 panic("%s: control vq busy!?", device_xname(sc->sc_dev));
1268 r = virtio_enqueue_reserve(vsc, vq, slot, 4);
1269 if (r != 0)
1270 panic("%s: control vq busy!?", device_xname(sc->sc_dev));
1271 virtio_enqueue(vsc, vq, slot, sc->sc_ctrl_cmd_dmamap, true);
1272 virtio_enqueue(vsc, vq, slot, sc->sc_ctrl_tbl_uc_dmamap, true);
1273 virtio_enqueue(vsc, vq, slot, sc->sc_ctrl_tbl_mc_dmamap, true);
1274 virtio_enqueue(vsc, vq, slot, sc->sc_ctrl_status_dmamap, false);
1275 virtio_enqueue_commit(vsc, vq, slot, true);
1276
1277 /* wait for done */
1278 mutex_enter(&sc->sc_ctrl_wait_lock);
1279 while (sc->sc_ctrl_inuse != DONE)
1280 cv_wait(&sc->sc_ctrl_wait, &sc->sc_ctrl_wait_lock);
1281 mutex_exit(&sc->sc_ctrl_wait_lock);
1282 /* already dequeueued */
1283
1284 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_cmd_dmamap, 0,
1285 sizeof(struct virtio_net_ctrl_cmd),
1286 BUS_DMASYNC_POSTWRITE);
1287 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_tbl_uc_dmamap, 0,
1288 (sizeof(struct virtio_net_ctrl_mac_tbl)
1289 + ETHER_ADDR_LEN * sc->sc_ctrl_mac_tbl_uc->nentries),
1290 BUS_DMASYNC_POSTWRITE);
1291 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_tbl_mc_dmamap, 0,
1292 (sizeof(struct virtio_net_ctrl_mac_tbl)
1293 + ETHER_ADDR_LEN * sc->sc_ctrl_mac_tbl_mc->nentries),
1294 BUS_DMASYNC_POSTWRITE);
1295 bus_dmamap_sync(vsc->sc_dmat, sc->sc_ctrl_status_dmamap, 0,
1296 sizeof(struct virtio_net_ctrl_status),
1297 BUS_DMASYNC_POSTREAD);
1298 bus_dmamap_unload(vsc->sc_dmat, sc->sc_ctrl_tbl_uc_dmamap);
1299 bus_dmamap_unload(vsc->sc_dmat, sc->sc_ctrl_tbl_mc_dmamap);
1300
1301 if (sc->sc_ctrl_status->ack == VIRTIO_NET_OK)
1302 r = 0;
1303 else {
1304 printf("%s: failed setting rx filter\n",
1305 device_xname(sc->sc_dev));
1306 r = EIO;
1307 }
1308
1309 out:
1310 mutex_enter(&sc->sc_ctrl_wait_lock);
1311 sc->sc_ctrl_inuse = FREE;
1312 cv_signal(&sc->sc_ctrl_wait);
1313 mutex_exit(&sc->sc_ctrl_wait_lock);
1314
1315 return r;
1316 }
1317
1318 /* ctrl vq interrupt; wake up the command issuer */
1319 static int
1320 vioif_ctrl_vq_done(struct virtqueue *vq)
1321 {
1322 struct virtio_softc *vsc = vq->vq_owner;
1323 struct vioif_softc *sc = device_private(vsc->sc_child);
1324 int r, slot;
1325
1326 r = virtio_dequeue(vsc, vq, &slot, NULL);
1327 if (r == ENOENT)
1328 return 0;
1329 virtio_dequeue_commit(vsc, vq, slot);
1330
1331 mutex_enter(&sc->sc_ctrl_wait_lock);
1332 sc->sc_ctrl_inuse = DONE;
1333 cv_signal(&sc->sc_ctrl_wait);
1334 mutex_exit(&sc->sc_ctrl_wait_lock);
1335
1336 return 1;
1337 }
1338
1339 /*
1340 * If IFF_PROMISC requested, set promiscuous
1341 * If multicast filter small enough (<=MAXENTRIES) set rx filter
1342 * If large multicast filter exist use ALLMULTI
1343 */
1344 /*
1345 * If setting rx filter fails fall back to ALLMULTI
1346 * If ALLMULTI fails fall back to PROMISC
1347 */
1348 static int
1349 vioif_rx_filter(struct vioif_softc *sc)
1350 {
1351 struct virtio_softc *vsc = sc->sc_virtio;
1352 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1353 struct ether_multi *enm;
1354 struct ether_multistep step;
1355 int nentries;
1356 int promisc = 0, allmulti = 0, rxfilter = 0;
1357 int r;
1358
1359 if (vsc->sc_nvqs < 3) { /* no ctrl vq; always promisc */
1360 ifp->if_flags |= IFF_PROMISC;
1361 return 0;
1362 }
1363
1364 if (ifp->if_flags & IFF_PROMISC) {
1365 promisc = 1;
1366 goto set;
1367 }
1368
1369 nentries = -1;
1370 ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
1371 while (nentries++, enm != NULL) {
1372 if (nentries >= VIRTIO_NET_CTRL_MAC_MAXENTRIES) {
1373 allmulti = 1;
1374 goto set;
1375 }
1376 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
1377 ETHER_ADDR_LEN)) {
1378 allmulti = 1;
1379 goto set;
1380 }
1381 memcpy(sc->sc_ctrl_mac_tbl_mc->macs[nentries],
1382 enm->enm_addrlo, ETHER_ADDR_LEN);
1383 ETHER_NEXT_MULTI(step, enm);
1384 }
1385 rxfilter = 1;
1386
1387 set:
1388 if (rxfilter) {
1389 sc->sc_ctrl_mac_tbl_uc->nentries = 0;
1390 sc->sc_ctrl_mac_tbl_mc->nentries = nentries;
1391 r = vioif_set_rx_filter(sc);
1392 if (r != 0) {
1393 rxfilter = 0;
1394 allmulti = 1; /* fallback */
1395 }
1396 } else {
1397 /* remove rx filter */
1398 sc->sc_ctrl_mac_tbl_uc->nentries = 0;
1399 sc->sc_ctrl_mac_tbl_mc->nentries = 0;
1400 r = vioif_set_rx_filter(sc);
1401 /* what to do on failure? */
1402 }
1403 if (allmulti) {
1404 r = vioif_set_allmulti(sc, true);
1405 if (r != 0) {
1406 allmulti = 0;
1407 promisc = 1; /* fallback */
1408 }
1409 } else {
1410 r = vioif_set_allmulti(sc, false);
1411 /* what to do on failure? */
1412 }
1413 if (promisc) {
1414 r = vioif_set_promisc(sc, true);
1415 } else {
1416 r = vioif_set_promisc(sc, false);
1417 }
1418
1419 return r;
1420 }
1421
1422 /* change link status */
1423 static int
1424 vioif_updown(struct vioif_softc *sc, bool isup)
1425 {
1426 struct virtio_softc *vsc = sc->sc_virtio;
1427
1428 if (!(vsc->sc_features & VIRTIO_NET_F_STATUS))
1429 return ENODEV;
1430 virtio_write_device_config_1(vsc,
1431 VIRTIO_NET_CONFIG_STATUS,
1432 isup?VIRTIO_NET_S_LINK_UP:0);
1433 return 0;
1434 }
1435