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