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