1 /*-
2  * Copyright (c) 2011, Bryan Venteicher <bryanv@daemoninthecloset.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/virtio/pci/virtio_pci.c,v 1.3 2012/04/14 05:48:04 grehan Exp $
27  */
28 
29 /* Driver for the VirtIO PCI interface. */
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/malloc.h>
37 #include <sys/serialize.h>
38 
39 #include <bus/pci/pcivar.h>
40 #include <bus/pci/pcireg.h>
41 
42 #include <sys/rman.h>
43 
44 #include <dev/virtual/virtio/virtio/virtio.h>
45 #include <dev/virtual/virtio/virtio/virtqueue.h>
46 #include "virtio_pci.h"
47 #include "virtio_if.h"
48 #include "virtio_bus_if.h"
49 
50 struct vtpci_softc {
51 	device_t			 vtpci_dev;
52 	struct resource			*vtpci_res;
53 	struct resource			*vtpci_msix_res;
54 	uint64_t			 vtpci_features;
55 	uint32_t			 vtpci_flags;
56 	int				 vtpci_irq_type;
57 	int				 vtpci_irq_rid;
58 #define VIRTIO_PCI_FLAG_NO_MSI		 0x0001
59 #define VIRTIO_PCI_FLAG_MSI		 0x0002
60 #define VIRTIO_PCI_FLAG_NO_MSIX		 0x0010
61 #define VIRTIO_PCI_FLAG_MSIX		 0x0020
62 #define VIRTIO_PCI_FLAG_SHARED_MSIX	 0x0040
63 
64 	device_t			 vtpci_child_dev;
65 	struct virtio_feature_desc	*vtpci_child_feat_desc;
66 
67 	/*
68 	 * Ideally, each virtqueue that the driver provides a callback for
69 	 * will receive its own MSIX vector. If there are not sufficient
70 	 * vectors available, we will then attempt to have all the VQs
71 	 * share one vector. Note that when using MSIX, the configuration
72 	 * changed notifications must be on their own vector.
73 	 *
74 	 * If MSIX is not available, we will attempt to have the whole
75 	 * device share one MSI vector, and then, finally, one legacy
76 	 * interrupt.
77 	 */
78 	int				 vtpci_nvqs;
79 	struct vtpci_virtqueue {
80 		struct virtqueue *vq;
81 
82 		/* Index into vtpci_intr_res[] below. Unused, then -1. */
83 		int		  ires_idx;
84 	} vtpci_vqx[VIRTIO_MAX_VIRTQUEUES];
85 
86 	/*
87 	 * When using MSIX interrupts, the first element of vtpci_intr_res[]
88 	 * is always the configuration changed notifications. The remaining
89 	 * element(s) are used for the virtqueues.
90 	 *
91 	 * With MSI and legacy interrupts, only the first element of
92 	 * vtpci_intr_res[] is used.
93 	 */
94 	int				 vtpci_nintr_res;
95 	struct vtpci_intr_resource {
96 		struct resource	*irq;
97 		int		 rid;
98 		void		*intrhand;
99 	} vtpci_intr_res[1 + VIRTIO_MAX_VIRTQUEUES];
100 };
101 
102 static int	vtpci_probe(device_t);
103 static int	vtpci_attach(device_t);
104 static int	vtpci_detach(device_t);
105 static int	vtpci_suspend(device_t);
106 static int	vtpci_resume(device_t);
107 static int	vtpci_shutdown(device_t);
108 static void	vtpci_driver_added(device_t, driver_t *);
109 static void	vtpci_child_detached(device_t, device_t);
110 static int	vtpci_read_ivar(device_t, device_t, int, uintptr_t *);
111 static int	vtpci_write_ivar(device_t, device_t, int, uintptr_t);
112 
113 static uint64_t	vtpci_negotiate_features(device_t, uint64_t);
114 static int	vtpci_with_feature(device_t, uint64_t);
115 static int	vtpci_alloc_virtqueues(device_t, int, int,
116 		    struct vq_alloc_info *);
117 static int	vtpci_setup_intr(device_t, lwkt_serialize_t);
118 static void	vtpci_stop(device_t);
119 static int	vtpci_reinit(device_t, uint64_t);
120 static void	vtpci_reinit_complete(device_t);
121 static void	vtpci_notify_virtqueue(device_t, uint16_t);
122 static uint8_t	vtpci_get_status(device_t);
123 static void	vtpci_set_status(device_t, uint8_t);
124 static void	vtpci_read_dev_config(device_t, bus_size_t, void *, int);
125 static void	vtpci_write_dev_config(device_t, bus_size_t, void *, int);
126 
127 static void	vtpci_describe_features(struct vtpci_softc *, const char *,
128 		    uint64_t);
129 static void	vtpci_probe_and_attach_child(struct vtpci_softc *);
130 
131 static int	vtpci_alloc_interrupts(struct vtpci_softc *, int, int,
132 		    struct vq_alloc_info *);
133 static int	vtpci_alloc_intr_resources(struct vtpci_softc *, int,
134 		    struct vq_alloc_info *);
135 static int	vtpci_alloc_msi(struct vtpci_softc *);
136 static int	vtpci_alloc_msix(struct vtpci_softc *, int);
137 static int	vtpci_register_msix_vector(struct vtpci_softc *, int, int);
138 
139 static void	vtpci_free_interrupts(struct vtpci_softc *);
140 static void	vtpci_free_virtqueues(struct vtpci_softc *);
141 static void	vtpci_release_child_resources(struct vtpci_softc *);
142 static void	vtpci_reset(struct vtpci_softc *);
143 
144 static int	vtpci_legacy_intr(void *);
145 static int	vtpci_vq_shared_intr(void *);
146 static int	vtpci_vq_intr(void *);
147 static int	vtpci_config_intr(void *);
148 
149 /*
150  * I/O port read/write wrappers.
151  */
152 #define vtpci_read_config_1(sc, o)	bus_read_1((sc)->vtpci_res, (o))
153 #define vtpci_read_config_2(sc, o)	bus_read_2((sc)->vtpci_res, (o))
154 #define vtpci_read_config_4(sc, o)	bus_read_4((sc)->vtpci_res, (o))
155 #define vtpci_write_config_1(sc, o, v)	bus_write_1((sc)->vtpci_res, (o), (v))
156 #define vtpci_write_config_2(sc, o, v)	bus_write_2((sc)->vtpci_res, (o), (v))
157 #define vtpci_write_config_4(sc, o, v)	bus_write_4((sc)->vtpci_res, (o), (v))
158 
159 /* Tunables. */
160 static int vtpci_disable_msix = 0;
161 TUNABLE_INT("hw.virtio.pci.disable_msix", &vtpci_disable_msix);
162 
163 static device_method_t vtpci_methods[] = {
164 	/* Device interface. */
165 	DEVMETHOD(device_probe,			  vtpci_probe),
166 	DEVMETHOD(device_attach,		  vtpci_attach),
167 	DEVMETHOD(device_detach,		  vtpci_detach),
168 	DEVMETHOD(device_suspend,		  vtpci_suspend),
169 	DEVMETHOD(device_resume,		  vtpci_resume),
170 	DEVMETHOD(device_shutdown,		  vtpci_shutdown),
171 
172 	/* Bus interface. */
173 	DEVMETHOD(bus_driver_added,		  vtpci_driver_added),
174 	DEVMETHOD(bus_child_detached,		  vtpci_child_detached),
175 	DEVMETHOD(bus_read_ivar,		  vtpci_read_ivar),
176 	DEVMETHOD(bus_write_ivar,		  vtpci_write_ivar),
177 
178 	/* VirtIO bus interface. */
179 	DEVMETHOD(virtio_bus_negotiate_features,  vtpci_negotiate_features),
180 	DEVMETHOD(virtio_bus_with_feature,	  vtpci_with_feature),
181 	DEVMETHOD(virtio_bus_alloc_virtqueues,	  vtpci_alloc_virtqueues),
182 	DEVMETHOD(virtio_bus_setup_intr,	  vtpci_setup_intr),
183 	DEVMETHOD(virtio_bus_stop,		  vtpci_stop),
184 	DEVMETHOD(virtio_bus_reinit,		  vtpci_reinit),
185 	DEVMETHOD(virtio_bus_reinit_complete,	  vtpci_reinit_complete),
186 	DEVMETHOD(virtio_bus_notify_vq,		  vtpci_notify_virtqueue),
187 	DEVMETHOD(virtio_bus_read_device_config,  vtpci_read_dev_config),
188 	DEVMETHOD(virtio_bus_write_device_config, vtpci_write_dev_config),
189 
190 	DEVMETHOD_END
191 };
192 
193 static driver_t vtpci_driver = {
194 	"virtio_pci",
195 	vtpci_methods,
196 	sizeof(struct vtpci_softc)
197 };
198 
199 devclass_t vtpci_devclass;
200 
201 DRIVER_MODULE(virtio_pci, pci, vtpci_driver, vtpci_devclass, NULL, NULL);
202 MODULE_VERSION(virtio_pci, 1);
203 MODULE_DEPEND(virtio_pci, pci, 1, 1, 1);
204 MODULE_DEPEND(virtio_pci, virtio, 1, 1, 1);
205 
206 static int
207 vtpci_probe(device_t dev)
208 {
209 	char desc[36];
210 	const char *name;
211 
212 	if (pci_get_vendor(dev) != VIRTIO_PCI_VENDORID)
213 		return (ENXIO);
214 
215 	if (pci_get_device(dev) < VIRTIO_PCI_DEVICEID_MIN ||
216 	    pci_get_device(dev) > VIRTIO_PCI_DEVICEID_MAX)
217 		return (ENXIO);
218 
219 	if (pci_get_revid(dev) != VIRTIO_PCI_ABI_VERSION)
220 		return (ENXIO);
221 
222 	name = virtio_device_name(pci_get_subdevice(dev));
223 	if (name == NULL)
224 		name = "Unknown";
225 
226 	ksnprintf(desc, sizeof(desc), "VirtIO PCI %s adapter", name);
227 	device_set_desc_copy(dev, desc);
228 
229 	return (BUS_PROBE_DEFAULT);
230 }
231 
232 static int
233 vtpci_attach(device_t dev)
234 {
235 	struct vtpci_softc *sc;
236 	device_t child;
237 	int rid;
238 
239 	sc = device_get_softc(dev);
240 	sc->vtpci_dev = dev;
241 
242 	pci_enable_busmaster(dev);
243 
244 	rid = PCIR_BAR(0);
245 	sc->vtpci_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
246 	    RF_ACTIVE);
247 	if (sc->vtpci_res == NULL) {
248 		device_printf(dev, "cannot map I/O space\n");
249 		return (ENXIO);
250 	}
251 
252 	if (pci_find_extcap(dev, PCIY_MSI, NULL) != 0)
253 		sc->vtpci_flags |= VIRTIO_PCI_FLAG_NO_MSI;
254 #if 0 /* XXX(vsrinivas): Check out how to get MSI-X */
255 	if (pci_find_extcap(dev, PCIY_MSIX, NULL) == 0) {
256 		rid = PCIR_BAR(1);
257 		sc->vtpci_msix_res = bus_alloc_resource_any(dev,
258 		    SYS_RES_MEMORY, &rid, RF_ACTIVE);
259 	}
260 #endif
261 	if (sc->vtpci_msix_res == NULL)
262 		sc->vtpci_flags |= VIRTIO_PCI_FLAG_NO_MSIX;
263 
264 	vtpci_reset(sc);
265 
266 	/* Tell the host we've noticed this device. */
267 	vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_ACK);
268 
269 	if ((child = device_add_child(dev, NULL, -1)) == NULL) {
270 		device_printf(dev, "cannot create child device\n");
271 		vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_FAILED);
272 		vtpci_detach(dev);
273 		return (ENOMEM);
274 	}
275 
276 	sc->vtpci_child_dev = child;
277 	vtpci_probe_and_attach_child(sc);
278 
279 	return (0);
280 }
281 
282 static int
283 vtpci_detach(device_t dev)
284 {
285 	struct vtpci_softc *sc;
286 	device_t child;
287 	int error;
288 
289 	sc = device_get_softc(dev);
290 
291 	if ((child = sc->vtpci_child_dev) != NULL) {
292 		error = device_delete_child(dev, child);
293 		if (error)
294 			return (error);
295 		sc->vtpci_child_dev = NULL;
296 	}
297 
298 	vtpci_reset(sc);
299 
300 	if (sc->vtpci_msix_res != NULL) {
301 		bus_release_resource(dev, SYS_RES_MEMORY, PCIR_BAR(1),
302 		    sc->vtpci_msix_res);
303 		sc->vtpci_msix_res = NULL;
304 	}
305 
306 	if (sc->vtpci_res != NULL) {
307 		bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(0),
308 		    sc->vtpci_res);
309 		sc->vtpci_res = NULL;
310 	}
311 
312 	return (0);
313 }
314 
315 static int
316 vtpci_suspend(device_t dev)
317 {
318 
319 	return (bus_generic_suspend(dev));
320 }
321 
322 static int
323 vtpci_resume(device_t dev)
324 {
325 
326 	return (bus_generic_resume(dev));
327 }
328 
329 static int
330 vtpci_shutdown(device_t dev)
331 {
332 
333 	(void) bus_generic_shutdown(dev);
334 	/* Forcibly stop the host device. */
335 	vtpci_stop(dev);
336 
337 	return (0);
338 }
339 
340 static void
341 vtpci_driver_added(device_t dev, driver_t *driver)
342 {
343 	struct vtpci_softc *sc;
344 
345 	sc = device_get_softc(dev);
346 
347 	vtpci_probe_and_attach_child(sc);
348 }
349 
350 static void
351 vtpci_child_detached(device_t dev, device_t child)
352 {
353 	struct vtpci_softc *sc;
354 
355 	sc = device_get_softc(dev);
356 
357 	vtpci_reset(sc);
358 	vtpci_release_child_resources(sc);
359 }
360 
361 static int
362 vtpci_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
363 {
364 	struct vtpci_softc *sc;
365 
366 	sc = device_get_softc(dev);
367 
368 	if (sc->vtpci_child_dev != child)
369 		return (ENOENT);
370 
371 	switch (index) {
372 	case VIRTIO_IVAR_DEVTYPE:
373 		*result = pci_get_subdevice(dev);
374 		break;
375 	default:
376 		return (ENOENT);
377 	}
378 
379 	return (0);
380 }
381 
382 static int
383 vtpci_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
384 {
385 	struct vtpci_softc *sc;
386 
387 	sc = device_get_softc(dev);
388 
389 	if (sc->vtpci_child_dev != child)
390 		return (ENOENT);
391 
392 	switch (index) {
393 	case VIRTIO_IVAR_FEATURE_DESC:
394 		sc->vtpci_child_feat_desc = (void *) value;
395 		break;
396 	default:
397 		return (ENOENT);
398 	}
399 
400 	return (0);
401 }
402 
403 static uint64_t
404 vtpci_negotiate_features(device_t dev, uint64_t child_features)
405 {
406 	struct vtpci_softc *sc;
407 	uint64_t host_features, features;
408 
409 	sc = device_get_softc(dev);
410 
411 	host_features = vtpci_read_config_4(sc, VIRTIO_PCI_HOST_FEATURES);
412 	vtpci_describe_features(sc, "host", host_features);
413 
414 	/*
415 	 * Limit negotiated features to what the driver, virtqueue, and
416 	 * host all support.
417 	 */
418 	features = host_features & child_features;
419 	features = virtqueue_filter_features(features);
420 	sc->vtpci_features = features;
421 
422 	vtpci_describe_features(sc, "negotiated", features);
423 	vtpci_write_config_4(sc, VIRTIO_PCI_GUEST_FEATURES, features);
424 
425 	return (features);
426 }
427 
428 static int
429 vtpci_with_feature(device_t dev, uint64_t feature)
430 {
431 	struct vtpci_softc *sc;
432 
433 	sc = device_get_softc(dev);
434 
435 	return ((sc->vtpci_features & feature) != 0);
436 }
437 
438 static int
439 vtpci_alloc_virtqueues(device_t dev, int flags, int nvqs,
440     struct vq_alloc_info *vq_info)
441 {
442 	struct vtpci_softc *sc;
443 	struct vtpci_virtqueue *vqx;
444 	struct vq_alloc_info *info;
445 	int queue, error;
446 	uint16_t vq_size;
447 
448 	sc = device_get_softc(dev);
449 
450 	if (sc->vtpci_nvqs != 0 || nvqs <= 0 ||
451 	    nvqs > VIRTIO_MAX_VIRTQUEUES)
452 		return (EINVAL);
453 
454 	error = vtpci_alloc_interrupts(sc, flags, nvqs, vq_info);
455 	if (error) {
456 		device_printf(dev, "cannot allocate interrupts\n");
457 		return (error);
458 	}
459 
460 	if (sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) {
461 		error = vtpci_register_msix_vector(sc,
462 		    VIRTIO_MSI_CONFIG_VECTOR, 0);
463 		if (error)
464 			return (error);
465 	}
466 
467 	for (queue = 0; queue < nvqs; queue++) {
468 		vqx = &sc->vtpci_vqx[queue];
469 		info = &vq_info[queue];
470 
471 		vtpci_write_config_2(sc, VIRTIO_PCI_QUEUE_SEL, queue);
472 
473 		vq_size = vtpci_read_config_2(sc, VIRTIO_PCI_QUEUE_NUM);
474 		error = virtqueue_alloc(dev, queue, vq_size,
475 		    VIRTIO_PCI_VRING_ALIGN, 0xFFFFFFFFUL, info, &vqx->vq);
476 		if (error)
477 			return (error);
478 
479 		if (sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) {
480 			error = vtpci_register_msix_vector(sc,
481 			    VIRTIO_MSI_QUEUE_VECTOR, vqx->ires_idx);
482 			if (error)
483 				return (error);
484 		}
485 
486 		vtpci_write_config_4(sc, VIRTIO_PCI_QUEUE_PFN,
487 		    virtqueue_paddr(vqx->vq) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
488 
489 		*info->vqai_vq = vqx->vq;
490 		sc->vtpci_nvqs++;
491 	}
492 
493 	return (0);
494 }
495 
496 static int
497 vtpci_setup_intr(device_t dev, lwkt_serialize_t slz)
498 {
499 	struct vtpci_softc *sc;
500 	struct vtpci_intr_resource *ires;
501 	struct vtpci_virtqueue *vqx;
502 	int i, flags, error;
503 
504 	sc = device_get_softc(dev);
505 	flags = INTR_MPSAFE;
506 	ires = &sc->vtpci_intr_res[0];
507 
508 	if ((sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) == 0) {
509 		error = bus_setup_intr(dev, ires->irq, flags,
510 				       (driver_intr_t *) vtpci_legacy_intr,
511 				       sc, &ires->intrhand, slz);
512 		return (error);
513 	}
514 
515 	error = bus_setup_intr(dev, ires->irq, flags,
516 			       (driver_intr_t *) vtpci_config_intr,
517 			       sc, &ires->intrhand, slz);
518 	if (error)
519 		return (error);
520 
521 	if (sc->vtpci_flags & VIRTIO_PCI_FLAG_SHARED_MSIX) {
522 		ires = &sc->vtpci_intr_res[1];
523 		error = bus_setup_intr(dev, ires->irq, flags,
524 				       (driver_intr_t *) vtpci_vq_shared_intr,
525 				       sc, &ires->intrhand, slz);
526 
527 		return (error);
528 	}
529 
530 	/* Setup an interrupt handler for each virtqueue. */
531 	for (i = 0; i < sc->vtpci_nvqs; i++) {
532 		vqx = &sc->vtpci_vqx[i];
533 		if (vqx->ires_idx < 1)
534 			continue;
535 
536 		ires = &sc->vtpci_intr_res[vqx->ires_idx];
537 		error = bus_setup_intr(dev, ires->irq, flags,
538 				       (driver_intr_t *) vtpci_vq_intr,
539 				       vqx->vq, &ires->intrhand, slz);
540 		if (error)
541 			return (error);
542 	}
543 
544 	return (0);
545 }
546 
547 static void
548 vtpci_stop(device_t dev)
549 {
550 	vtpci_reset(device_get_softc(dev));
551 }
552 
553 static int
554 vtpci_reinit(device_t dev, uint64_t features)
555 {
556 	struct vtpci_softc *sc;
557 	struct vtpci_virtqueue *vqx;
558 	struct virtqueue *vq;
559 	int queue, error;
560 	uint16_t vq_size;
561 
562 	sc = device_get_softc(dev);
563 
564 	/*
565 	 * Redrive the device initialization. This is a bit of an abuse
566 	 * of the specification, but both VirtualBox and QEMU/KVM seem
567 	 * to play nice. We do not allow the host device to change from
568 	 * what was originally negotiated beyond what the guest driver
569 	 * changed (MSIX state should not change, number of virtqueues
570 	 * and their size remain the same, etc).
571 	 */
572 
573 	if (vtpci_get_status(dev) != VIRTIO_CONFIG_STATUS_RESET)
574 		vtpci_stop(dev);
575 
576 	/*
577 	 * Quickly drive the status through ACK and DRIVER. The device
578 	 * does not become usable again until vtpci_reinit_complete().
579 	 */
580 	vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_ACK);
581 	vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER);
582 
583 	vtpci_negotiate_features(dev, features);
584 
585 	if (sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) {
586 		error = vtpci_register_msix_vector(sc,
587 		    VIRTIO_MSI_CONFIG_VECTOR, 0);
588 		if (error)
589 			return (error);
590 	}
591 
592 	for (queue = 0; queue < sc->vtpci_nvqs; queue++) {
593 		vqx = &sc->vtpci_vqx[queue];
594 		vq = vqx->vq;
595 
596 		KASSERT(vq != NULL, ("vq %d not allocated", queue));
597 		vtpci_write_config_2(sc, VIRTIO_PCI_QUEUE_SEL, queue);
598 
599 		vq_size = vtpci_read_config_2(sc, VIRTIO_PCI_QUEUE_NUM);
600 		error = virtqueue_reinit(vq, vq_size);
601 		if (error)
602 			return (error);
603 
604 		if (sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) {
605 			error = vtpci_register_msix_vector(sc,
606 			    VIRTIO_MSI_QUEUE_VECTOR, vqx->ires_idx);
607 			if (error)
608 				return (error);
609 		}
610 
611 		vtpci_write_config_4(sc, VIRTIO_PCI_QUEUE_PFN,
612 		    virtqueue_paddr(vqx->vq) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
613 	}
614 
615 	return (0);
616 }
617 
618 static void
619 vtpci_reinit_complete(device_t dev)
620 {
621 
622 	vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER_OK);
623 }
624 
625 static void
626 vtpci_notify_virtqueue(device_t dev, uint16_t queue)
627 {
628 	struct vtpci_softc *sc;
629 
630 	sc = device_get_softc(dev);
631 
632 	vtpci_write_config_2(sc, VIRTIO_PCI_QUEUE_NOTIFY, queue);
633 }
634 
635 static uint8_t
636 vtpci_get_status(device_t dev)
637 {
638 	struct vtpci_softc *sc;
639 
640 	sc = device_get_softc(dev);
641 
642 	return (vtpci_read_config_1(sc, VIRTIO_PCI_STATUS));
643 }
644 
645 static void
646 vtpci_set_status(device_t dev, uint8_t status)
647 {
648 	struct vtpci_softc *sc;
649 
650 	sc = device_get_softc(dev);
651 
652 	if (status != VIRTIO_CONFIG_STATUS_RESET)
653 		status |= vtpci_get_status(dev);
654 
655 	vtpci_write_config_1(sc, VIRTIO_PCI_STATUS, status);
656 }
657 
658 static void
659 vtpci_read_dev_config(device_t dev, bus_size_t offset,
660     void *dst, int length)
661 {
662 	struct vtpci_softc *sc;
663 	bus_size_t off;
664 	uint8_t *d;
665 	int size;
666 
667 	sc = device_get_softc(dev);
668 	off = VIRTIO_PCI_CONFIG(sc) + offset;
669 
670 	for (d = dst; length > 0; d += size, off += size, length -= size) {
671 		if (length >= 4) {
672 			size = 4;
673 			*(uint32_t *)d = vtpci_read_config_4(sc, off);
674 		} else if (length >= 2) {
675 			size = 2;
676 			*(uint16_t *)d = vtpci_read_config_2(sc, off);
677 		} else {
678 			size = 1;
679 			*d = vtpci_read_config_1(sc, off);
680 		}
681 	}
682 }
683 
684 static void
685 vtpci_write_dev_config(device_t dev, bus_size_t offset,
686     void *src, int length)
687 {
688 	struct vtpci_softc *sc;
689 	bus_size_t off;
690 	uint8_t *s;
691 	int size;
692 
693 	sc = device_get_softc(dev);
694 	off = VIRTIO_PCI_CONFIG(sc) + offset;
695 
696 	for (s = src; length > 0; s += size, off += size, length -= size) {
697 		if (length >= 4) {
698 			size = 4;
699 			vtpci_write_config_4(sc, off, *(uint32_t *)s);
700 		} else if (length >= 2) {
701 			size = 2;
702 			vtpci_write_config_2(sc, off, *(uint16_t *)s);
703 		} else {
704 			size = 1;
705 			vtpci_write_config_1(sc, off, *s);
706 		}
707 	}
708 }
709 
710 static void
711 vtpci_describe_features(struct vtpci_softc *sc, const char *msg,
712     uint64_t features)
713 {
714 	device_t dev, child;
715 
716 	dev = sc->vtpci_dev;
717 	child = sc->vtpci_child_dev;
718 
719 	if (device_is_attached(child) && bootverbose == 0)
720 		return;
721 
722 	virtio_describe(dev, msg, features, sc->vtpci_child_feat_desc);
723 }
724 
725 static void
726 vtpci_probe_and_attach_child(struct vtpci_softc *sc)
727 {
728 	device_t dev, child;
729 	int error;
730 
731 	dev = sc->vtpci_dev;
732 	child = sc->vtpci_child_dev;
733 
734 	if (child == NULL)
735 		return;
736 
737 	if (device_get_state(child) != DS_NOTPRESENT)
738 		return;
739 
740 	vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER);
741 	error = device_probe_and_attach(child);
742 	if (error != 0 || device_get_state(child) == DS_NOTPRESENT) {
743 		vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_FAILED);
744 		vtpci_reset(sc);
745 		vtpci_release_child_resources(sc);
746 
747 		/* Reset status for future attempt. */
748 		vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_ACK);
749 	} else
750 		vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER_OK);
751 }
752 
753 static int
754 vtpci_alloc_interrupts(struct vtpci_softc *sc, int flags, int nvqs,
755     struct vq_alloc_info *vq_info)
756 {
757 	int i, nvectors, error;
758 
759 	/*
760 	 * Only allocate a vector for virtqueues that are actually
761 	 * expecting an interrupt.
762 	 */
763 	for (nvectors = 0, i = 0; i < nvqs; i++)
764 		if (vq_info[i].vqai_intr != NULL)
765 			nvectors++;
766 
767 	if (vtpci_disable_msix != 0 ||
768 	    sc->vtpci_flags & VIRTIO_PCI_FLAG_NO_MSIX ||
769 	    flags & VIRTIO_ALLOC_VQS_DISABLE_MSIX ||
770 	    vtpci_alloc_msix(sc, nvectors) != 0) {
771 		/*
772 		 * Use MSI interrupts if available. Otherwise, we fallback
773 		 * to legacy interrupts.
774 		 */
775 		if ((sc->vtpci_flags & VIRTIO_PCI_FLAG_NO_MSI) == 0 &&
776 		    vtpci_alloc_msi(sc) == 0)
777 			sc->vtpci_flags |= VIRTIO_PCI_FLAG_MSI;
778 
779 		sc->vtpci_nintr_res = 1;
780 	}
781 
782 	error = vtpci_alloc_intr_resources(sc, nvqs, vq_info);
783 
784 	return (error);
785 }
786 
787 static int
788 vtpci_alloc_intr_resources(struct vtpci_softc *sc, int nvqs,
789     struct vq_alloc_info *vq_info)
790 {
791 	device_t dev;
792 	struct resource *irq;
793 	struct vtpci_virtqueue *vqx;
794 	int i, rid, flags, res_idx;
795 
796 	dev = sc->vtpci_dev;
797 	flags = RF_ACTIVE;
798 
799 	if ((sc->vtpci_flags &
800 	    (VIRTIO_PCI_FLAG_MSI | VIRTIO_PCI_FLAG_MSIX)) == 0) {
801 		rid = 0;
802 		flags |= RF_SHAREABLE;
803 	} else
804 		rid = 1;
805 
806 	for (i = 0; i < sc->vtpci_nintr_res; i++) {
807 		irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, flags);
808 		if (irq == NULL)
809 			return (ENXIO);
810 
811 		sc->vtpci_intr_res[i].irq = irq;
812 		sc->vtpci_intr_res[i].rid = rid++;
813 	}
814 
815 	/*
816 	 * Map the virtqueue into the correct index in vq_intr_res[]. Note the
817 	 * first index is reserved for configuration changes notifications.
818 	 */
819 	for (i = 0, res_idx = 1; i < nvqs; i++) {
820 		vqx = &sc->vtpci_vqx[i];
821 
822 		if (sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) {
823 			if (vq_info[i].vqai_intr == NULL)
824 				vqx->ires_idx = -1;
825 			else if (sc->vtpci_flags & VIRTIO_PCI_FLAG_SHARED_MSIX)
826 				vqx->ires_idx = res_idx;
827 			else
828 				vqx->ires_idx = res_idx++;
829 		} else
830 			vqx->ires_idx = -1;
831 	}
832 
833 	return (0);
834 }
835 
836 static int
837 vtpci_alloc_msi(struct vtpci_softc *sc)
838 {
839 	device_t dev;
840 	int nmsi;
841 	u_int irq_flags;
842 
843 	dev = sc->vtpci_dev;
844 	nmsi = pci_msi_count(dev);
845 
846 	if (nmsi < 1)
847 		return (1);
848 
849 	sc->vtpci_irq_rid = 0;
850         sc->vtpci_irq_type = pci_alloc_1intr(dev, 1,
851             &sc->vtpci_irq_rid, &irq_flags);
852 
853 
854 	return (0);
855 }
856 
857 static int
858 vtpci_alloc_msix(struct vtpci_softc *sc, int nvectors)
859 {
860 	/* XXX(vsrinivas): Huh? Is this how MSI-X works?*/
861 	/* XXX(vsrinivas): All of this was disabled... */
862 #if 0
863 	device_t dev;
864 	int nmsix, cnt, required;
865 
866 	dev = sc->vtpci_dev;
867 
868 	nmsix = pci_msix_count(dev);
869 	if (nmsix < 1)
870 		return (1);
871 
872 	/* An additional vector is needed for the config changes. */
873 	required = nvectors + 1;
874 	if (nmsix >= required) {
875 		cnt = required;
876 		if (pci_alloc_msix(dev, &cnt) == 0 && cnt >= required)
877 			goto out;
878 
879 		pci_release_msi(dev);
880 	}
881 
882 	/* Attempt shared MSIX configuration. */
883 	required = 2;
884 	if (nmsix >= required) {
885 		cnt = required;
886 		if (pci_alloc_msix(dev, &cnt) == 0 && cnt >= required) {
887 			sc->vtpci_flags |= VIRTIO_PCI_FLAG_SHARED_MSIX;
888 			goto out;
889 		}
890 
891 		pci_release_msi(dev);
892 	}
893 
894 	return (1);
895 
896 out:
897 	sc->vtpci_nintr_res = required;
898 	sc->vtpci_flags |= VIRTIO_PCI_FLAG_MSIX;
899 
900 	if (bootverbose) {
901 		if (sc->vtpci_flags & VIRTIO_PCI_FLAG_SHARED_MSIX)
902 			device_printf(dev, "using shared virtqueue MSIX\n");
903 		else
904 			device_printf(dev, "using per virtqueue MSIX\n");
905 	}
906 #endif
907 	return (0);
908 }
909 
910 static int
911 vtpci_register_msix_vector(struct vtpci_softc *sc, int offset, int res_idx)
912 {
913 	device_t dev;
914 	uint16_t vector;
915 
916 	dev = sc->vtpci_dev;
917 
918 	if (offset != VIRTIO_MSI_CONFIG_VECTOR &&
919 	    offset != VIRTIO_MSI_QUEUE_VECTOR)
920 		return (EINVAL);
921 
922 	if (res_idx != -1) {
923 		/* Map from rid to host vector. */
924 		vector = sc->vtpci_intr_res[res_idx].rid - 1;
925 	} else
926 		vector = VIRTIO_MSI_NO_VECTOR;
927 
928 	/* The first resource is special; make sure it is used correctly. */
929 	if (res_idx == 0) {
930 		KASSERT(vector == 0, ("unexpected config vector"));
931 		KASSERT(offset == VIRTIO_MSI_CONFIG_VECTOR,
932 		    ("unexpected config offset"));
933 	}
934 
935 	vtpci_write_config_2(sc, offset, vector);
936 
937 	if (vtpci_read_config_2(sc, offset) != vector) {
938 		device_printf(dev, "insufficient host resources for "
939 		    "MSIX interrupts\n");
940 		return (ENODEV);
941 	}
942 
943 	return (0);
944 }
945 
946 static void
947 vtpci_free_interrupts(struct vtpci_softc *sc)
948 {
949 	device_t dev;
950 	struct vtpci_intr_resource *ires;
951 	int i;
952 
953 	dev = sc->vtpci_dev;
954 	sc->vtpci_nintr_res = 0;
955 
956 	if (sc->vtpci_flags & (VIRTIO_PCI_FLAG_MSI | VIRTIO_PCI_FLAG_MSIX)) {
957 		pci_release_msi(dev);
958 		sc->vtpci_flags &= ~(VIRTIO_PCI_FLAG_MSI |
959 		    VIRTIO_PCI_FLAG_MSIX | VIRTIO_PCI_FLAG_SHARED_MSIX);
960 	}
961 
962 	for (i = 0; i < 1 + VIRTIO_MAX_VIRTQUEUES; i++) {
963 		ires = &sc->vtpci_intr_res[i];
964 
965 		if (ires->intrhand != NULL) {
966 			bus_teardown_intr(dev, ires->irq, ires->intrhand);
967 			ires->intrhand = NULL;
968 		}
969 
970 		if (ires->irq != NULL) {
971 			bus_release_resource(dev, SYS_RES_IRQ, ires->rid,
972 			    ires->irq);
973 			ires->irq = NULL;
974 		}
975 
976 		ires->rid = -1;
977 	}
978 }
979 
980 static void
981 vtpci_free_virtqueues(struct vtpci_softc *sc)
982 {
983 	struct vtpci_virtqueue *vqx;
984 	int i;
985 
986 	sc->vtpci_nvqs = 0;
987 
988 	for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; i++) {
989 		vqx = &sc->vtpci_vqx[i];
990 
991 		if (vqx->vq != NULL) {
992 			virtqueue_free(vqx->vq);
993 			vqx->vq = NULL;
994 		}
995 	}
996 }
997 
998 static void
999 vtpci_release_child_resources(struct vtpci_softc *sc)
1000 {
1001 
1002 	vtpci_free_interrupts(sc);
1003 	vtpci_free_virtqueues(sc);
1004 }
1005 
1006 static void
1007 vtpci_reset(struct vtpci_softc *sc)
1008 {
1009 
1010 	/*
1011 	 * Setting the status to RESET sets the host device to
1012 	 * the original, uninitialized state.
1013 	 */
1014 	vtpci_set_status(sc->vtpci_dev, VIRTIO_CONFIG_STATUS_RESET);
1015 }
1016 
1017 static int
1018 vtpci_legacy_intr(void *xsc)
1019 {
1020 	struct vtpci_softc *sc;
1021 	struct vtpci_virtqueue *vqx;
1022 	int i;
1023 	uint8_t isr;
1024 
1025 	sc = xsc;
1026 	vqx = &sc->vtpci_vqx[0];
1027 
1028 	/* Reading the ISR also clears it. */
1029 	isr = vtpci_read_config_1(sc, VIRTIO_PCI_ISR);
1030 
1031 	if (isr & VIRTIO_PCI_ISR_CONFIG)
1032 		vtpci_config_intr(sc);
1033 
1034 	if (isr & VIRTIO_PCI_ISR_INTR)
1035 		for (i = 0; i < sc->vtpci_nvqs; i++, vqx++)
1036 			virtqueue_intr(vqx->vq);
1037 
1038 	return isr;
1039 }
1040 
1041 static int
1042 vtpci_vq_shared_intr(void *xsc)
1043 {
1044 	struct vtpci_softc *sc;
1045 	struct vtpci_virtqueue *vqx;
1046 	int i, rc;
1047 
1048 	rc = 0;
1049 	sc = xsc;
1050 	vqx = &sc->vtpci_vqx[0];
1051 
1052 	for (i = 0; i < sc->vtpci_nvqs; i++, vqx++)
1053 		rc |= virtqueue_intr(vqx->vq);
1054 
1055 	return rc;
1056 }
1057 
1058 static int
1059 vtpci_vq_intr(void *xvq)
1060 {
1061 	struct virtqueue *vq;
1062 	int rc;
1063 
1064 	vq = xvq;
1065 	rc = virtqueue_intr(vq);
1066 
1067 	return rc;
1068 }
1069 
1070 static int
1071 vtpci_config_intr(void *xsc)
1072 {
1073 	struct vtpci_softc *sc;
1074 	device_t child;
1075 	int rc;
1076 
1077 	rc = 0;
1078 	sc = xsc;
1079 	child = sc->vtpci_child_dev;
1080 
1081 	if (child != NULL)
1082 		rc = VIRTIO_CONFIG_CHANGE(child);
1083 
1084 	return rc;
1085 }
1086