kernel/dev: Remove some #include duplicates.
[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/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         /* XXX(vsrinivas): Check out how to get MSI-X */
255 #ifdef OLD_MSI
256         if (pci_find_extcap(dev, PCIY_MSIX, NULL) == 0) {
257                 rid = PCIR_BAR(1);
258                 sc->vtpci_msix_res = bus_alloc_resource_any(dev,
259                     SYS_RES_MEMORY, &rid, RF_ACTIVE);
260         }
261 #endif
262         if (sc->vtpci_msix_res == NULL)
263                 sc->vtpci_flags |= VIRTIO_PCI_FLAG_NO_MSIX;
264
265         vtpci_reset(sc);
266
267         /* Tell the host we've noticed this device. */
268         vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_ACK);
269
270         if ((child = device_add_child(dev, NULL, -1)) == NULL) {
271                 device_printf(dev, "cannot create child device\n");
272                 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_FAILED);
273                 vtpci_detach(dev);
274                 return (ENOMEM);
275         }
276
277         sc->vtpci_child_dev = child;
278         vtpci_probe_and_attach_child(sc);
279
280         return (0);
281 }
282
283 static int
284 vtpci_detach(device_t dev)
285 {
286         struct vtpci_softc *sc;
287         device_t child;
288         int error;
289
290         sc = device_get_softc(dev);
291
292         if ((child = sc->vtpci_child_dev) != NULL) {
293                 error = device_delete_child(dev, child);
294                 if (error)
295                         return (error);
296                 sc->vtpci_child_dev = NULL;
297         }
298
299         vtpci_reset(sc);
300
301         if (sc->vtpci_msix_res != NULL) {
302                 bus_release_resource(dev, SYS_RES_MEMORY, PCIR_BAR(1),
303                     sc->vtpci_msix_res);
304                 sc->vtpci_msix_res = NULL;
305         }
306
307         if (sc->vtpci_res != NULL) {
308                 bus_release_resource(dev, SYS_RES_IOPORT, PCIR_BAR(0),
309                     sc->vtpci_res);
310                 sc->vtpci_res = NULL;
311         }
312
313         return (0);
314 }
315
316 static int
317 vtpci_suspend(device_t dev)
318 {
319
320         return (bus_generic_suspend(dev));
321 }
322
323 static int
324 vtpci_resume(device_t dev)
325 {
326
327         return (bus_generic_resume(dev));
328 }
329
330 static int
331 vtpci_shutdown(device_t dev)
332 {
333
334         (void) bus_generic_shutdown(dev);
335         /* Forcibly stop the host device. */
336         vtpci_stop(dev);
337
338         return (0);
339 }
340
341 static void
342 vtpci_driver_added(device_t dev, driver_t *driver)
343 {
344         struct vtpci_softc *sc;
345
346         sc = device_get_softc(dev);
347
348         vtpci_probe_and_attach_child(sc);
349 }
350
351 static void
352 vtpci_child_detached(device_t dev, device_t child)
353 {
354         struct vtpci_softc *sc;
355
356         sc = device_get_softc(dev);
357
358         vtpci_reset(sc);
359         vtpci_release_child_resources(sc);
360 }
361
362 static int
363 vtpci_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
364 {
365         struct vtpci_softc *sc;
366
367         sc = device_get_softc(dev);
368
369         if (sc->vtpci_child_dev != child)
370                 return (ENOENT);
371
372         switch (index) {
373         case VIRTIO_IVAR_DEVTYPE:
374                 *result = pci_get_subdevice(dev);
375                 break;
376         default:
377                 return (ENOENT);
378         }
379
380         return (0);
381 }
382
383 static int
384 vtpci_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
385 {
386         struct vtpci_softc *sc;
387
388         sc = device_get_softc(dev);
389
390         if (sc->vtpci_child_dev != child)
391                 return (ENOENT);
392
393         switch (index) {
394         case VIRTIO_IVAR_FEATURE_DESC:
395                 sc->vtpci_child_feat_desc = (void *) value;
396                 break;
397         default:
398                 return (ENOENT);
399         }
400
401         return (0);
402 }
403
404 static uint64_t
405 vtpci_negotiate_features(device_t dev, uint64_t child_features)
406 {
407         struct vtpci_softc *sc;
408         uint64_t host_features, features;
409
410         sc = device_get_softc(dev);
411
412         host_features = vtpci_read_config_4(sc, VIRTIO_PCI_HOST_FEATURES);
413         vtpci_describe_features(sc, "host", host_features);
414
415         /*
416          * Limit negotiated features to what the driver, virtqueue, and
417          * host all support.
418          */
419         features = host_features & child_features;
420         features = virtqueue_filter_features(features);
421         sc->vtpci_features = features;
422
423         vtpci_describe_features(sc, "negotiated", features);
424         vtpci_write_config_4(sc, VIRTIO_PCI_GUEST_FEATURES, features);
425
426         return (features);
427 }
428
429 static int
430 vtpci_with_feature(device_t dev, uint64_t feature)
431 {
432         struct vtpci_softc *sc;
433
434         sc = device_get_softc(dev);
435
436         return ((sc->vtpci_features & feature) != 0);
437 }
438
439 static int
440 vtpci_alloc_virtqueues(device_t dev, int flags, int nvqs,
441     struct vq_alloc_info *vq_info)
442 {
443         struct vtpci_softc *sc;
444         struct vtpci_virtqueue *vqx;
445         struct vq_alloc_info *info;
446         int queue, error;
447         uint16_t vq_size;
448
449         sc = device_get_softc(dev);
450
451         if (sc->vtpci_nvqs != 0 || nvqs <= 0 ||
452             nvqs > VIRTIO_MAX_VIRTQUEUES)
453                 return (EINVAL);
454
455         error = vtpci_alloc_interrupts(sc, flags, nvqs, vq_info);
456         if (error) {
457                 device_printf(dev, "cannot allocate interrupts\n");
458                 return (error);
459         }
460
461         if (sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) {
462                 error = vtpci_register_msix_vector(sc,
463                     VIRTIO_MSI_CONFIG_VECTOR, 0);
464                 if (error)
465                         return (error);
466         }
467
468         for (queue = 0; queue < nvqs; queue++) {
469                 vqx = &sc->vtpci_vqx[queue];
470                 info = &vq_info[queue];
471
472                 vtpci_write_config_2(sc, VIRTIO_PCI_QUEUE_SEL, queue);
473
474                 vq_size = vtpci_read_config_2(sc, VIRTIO_PCI_QUEUE_NUM);
475                 error = virtqueue_alloc(dev, queue, vq_size,
476                     VIRTIO_PCI_VRING_ALIGN, 0xFFFFFFFFUL, info, &vqx->vq);
477                 if (error)
478                         return (error);
479
480                 if (sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) {
481                         error = vtpci_register_msix_vector(sc,
482                             VIRTIO_MSI_QUEUE_VECTOR, vqx->ires_idx);
483                         if (error)
484                                 return (error);
485                 }
486
487                 vtpci_write_config_4(sc, VIRTIO_PCI_QUEUE_PFN,
488                     virtqueue_paddr(vqx->vq) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
489
490                 *info->vqai_vq = vqx->vq;
491                 sc->vtpci_nvqs++;
492         }
493
494         return (0);
495 }
496
497 static int
498 vtpci_setup_intr(device_t dev, lwkt_serialize_t slz)
499 {
500         struct vtpci_softc *sc;
501         struct vtpci_intr_resource *ires;
502         struct vtpci_virtqueue *vqx;
503         int i, flags, error;
504
505         sc = device_get_softc(dev);
506         flags = INTR_MPSAFE;
507         ires = &sc->vtpci_intr_res[0];
508
509         if ((sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) == 0) {
510                 error = bus_setup_intr(dev, ires->irq, flags,
511                                        (driver_intr_t *) vtpci_legacy_intr,
512                                        sc, &ires->intrhand, slz);
513                 return (error);
514         }
515
516         error = bus_setup_intr(dev, ires->irq, flags,
517                                (driver_intr_t *) vtpci_config_intr,
518                                sc, &ires->intrhand, slz);
519         if (error)
520                 return (error);
521
522         if (sc->vtpci_flags & VIRTIO_PCI_FLAG_SHARED_MSIX) {
523                 ires = &sc->vtpci_intr_res[1];
524                 error = bus_setup_intr(dev, ires->irq, flags,
525                                        (driver_intr_t *) vtpci_vq_shared_intr,
526                                        sc, &ires->intrhand, slz);
527
528                 return (error);
529         }
530
531         /* Setup an interrupt handler for each virtqueue. */
532         for (i = 0; i < sc->vtpci_nvqs; i++) {
533                 vqx = &sc->vtpci_vqx[i];
534                 if (vqx->ires_idx < 1)
535                         continue;
536
537                 ires = &sc->vtpci_intr_res[vqx->ires_idx];
538                 error = bus_setup_intr(dev, ires->irq, flags,
539                                        (driver_intr_t *) vtpci_vq_intr,
540                                        vqx->vq, &ires->intrhand, slz);
541                 if (error)
542                         return (error);
543         }
544
545         return (0);
546 }
547
548 static void
549 vtpci_stop(device_t dev)
550 {
551         vtpci_reset(device_get_softc(dev));
552 }
553
554 static int
555 vtpci_reinit(device_t dev, uint64_t features)
556 {
557         struct vtpci_softc *sc;
558         struct vtpci_virtqueue *vqx;
559         struct virtqueue *vq;
560         int queue, error;
561         uint16_t vq_size;
562
563         sc = device_get_softc(dev);
564
565         /*
566          * Redrive the device initialization. This is a bit of an abuse
567          * of the specification, but both VirtualBox and QEMU/KVM seem
568          * to play nice. We do not allow the host device to change from
569          * what was originally negotiated beyond what the guest driver
570          * changed (MSIX state should not change, number of virtqueues
571          * and their size remain the same, etc).
572          */
573
574         if (vtpci_get_status(dev) != VIRTIO_CONFIG_STATUS_RESET)
575                 vtpci_stop(dev);
576
577         /*
578          * Quickly drive the status through ACK and DRIVER. The device
579          * does not become usable again until vtpci_reinit_complete().
580          */
581         vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_ACK);
582         vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER);
583
584         vtpci_negotiate_features(dev, features);
585
586         if (sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) {
587                 error = vtpci_register_msix_vector(sc,
588                     VIRTIO_MSI_CONFIG_VECTOR, 0);
589                 if (error)
590                         return (error);
591         }
592
593         for (queue = 0; queue < sc->vtpci_nvqs; queue++) {
594                 vqx = &sc->vtpci_vqx[queue];
595                 vq = vqx->vq;
596
597                 KASSERT(vq != NULL, ("vq %d not allocated", queue));
598                 vtpci_write_config_2(sc, VIRTIO_PCI_QUEUE_SEL, queue);
599
600                 vq_size = vtpci_read_config_2(sc, VIRTIO_PCI_QUEUE_NUM);
601                 error = virtqueue_reinit(vq, vq_size);
602                 if (error)
603                         return (error);
604
605                 if (sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) {
606                         error = vtpci_register_msix_vector(sc,
607                             VIRTIO_MSI_QUEUE_VECTOR, vqx->ires_idx);
608                         if (error)
609                                 return (error);
610                 }
611
612                 vtpci_write_config_4(sc, VIRTIO_PCI_QUEUE_PFN,
613                     virtqueue_paddr(vqx->vq) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
614         }
615
616         return (0);
617 }
618
619 static void
620 vtpci_reinit_complete(device_t dev)
621 {
622
623         vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER_OK);
624 }
625
626 static void
627 vtpci_notify_virtqueue(device_t dev, uint16_t queue)
628 {
629         struct vtpci_softc *sc;
630
631         sc = device_get_softc(dev);
632
633         vtpci_write_config_2(sc, VIRTIO_PCI_QUEUE_NOTIFY, queue);
634 }
635
636 static uint8_t
637 vtpci_get_status(device_t dev)
638 {
639         struct vtpci_softc *sc;
640
641         sc = device_get_softc(dev);
642
643         return (vtpci_read_config_1(sc, VIRTIO_PCI_STATUS));
644 }
645
646 static void
647 vtpci_set_status(device_t dev, uint8_t status)
648 {
649         struct vtpci_softc *sc;
650
651         sc = device_get_softc(dev);
652
653         if (status != VIRTIO_CONFIG_STATUS_RESET)
654                 status |= vtpci_get_status(dev);
655
656         vtpci_write_config_1(sc, VIRTIO_PCI_STATUS, status);
657 }
658
659 static void
660 vtpci_read_dev_config(device_t dev, bus_size_t offset,
661     void *dst, int length)
662 {
663         struct vtpci_softc *sc;
664         bus_size_t off;
665         uint8_t *d;
666         int size;
667
668         sc = device_get_softc(dev);
669         off = VIRTIO_PCI_CONFIG(sc) + offset;
670
671         for (d = dst; length > 0; d += size, off += size, length -= size) {
672                 if (length >= 4) {
673                         size = 4;
674                         *(uint32_t *)d = vtpci_read_config_4(sc, off);
675                 } else if (length >= 2) {
676                         size = 2;
677                         *(uint16_t *)d = vtpci_read_config_2(sc, off);
678                 } else {
679                         size = 1;
680                         *d = vtpci_read_config_1(sc, off);
681                 }
682         }
683 }
684
685 static void
686 vtpci_write_dev_config(device_t dev, bus_size_t offset,
687     void *src, int length)
688 {
689         struct vtpci_softc *sc;
690         bus_size_t off;
691         uint8_t *s;
692         int size;
693
694         sc = device_get_softc(dev);
695         off = VIRTIO_PCI_CONFIG(sc) + offset;
696
697         for (s = src; length > 0; s += size, off += size, length -= size) {
698                 if (length >= 4) {
699                         size = 4;
700                         vtpci_write_config_4(sc, off, *(uint32_t *)s);
701                 } else if (length >= 2) {
702                         size = 2;
703                         vtpci_write_config_2(sc, off, *(uint16_t *)s);
704                 } else {
705                         size = 1;
706                         vtpci_write_config_1(sc, off, *s);
707                 }
708         }
709 }
710
711 static void
712 vtpci_describe_features(struct vtpci_softc *sc, const char *msg,
713     uint64_t features)
714 {
715         device_t dev, child;
716
717         dev = sc->vtpci_dev;
718         child = sc->vtpci_child_dev;
719
720         if (device_is_attached(child) && bootverbose == 0)
721                 return;
722
723         virtio_describe(dev, msg, features, sc->vtpci_child_feat_desc);
724 }
725
726 static void
727 vtpci_probe_and_attach_child(struct vtpci_softc *sc)
728 {
729         device_t dev, child;
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         if (device_probe_child(dev, child) != 0)
741                 return;
742
743         vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER);
744         if (DEVICE_ATTACH(child) != 0) {
745                 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_FAILED);
746                 vtpci_reset(sc);
747                 vtpci_release_child_resources(sc);
748
749                 /* Reset status for future attempt. */
750                 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_ACK);
751         } else
752                 vtpci_set_status(dev, VIRTIO_CONFIG_STATUS_DRIVER_OK);
753 }
754
755 static int
756 vtpci_alloc_interrupts(struct vtpci_softc *sc, int flags, int nvqs,
757     struct vq_alloc_info *vq_info)
758 {
759         int i, nvectors, error;
760
761         /*
762          * Only allocate a vector for virtqueues that are actually
763          * expecting an interrupt.
764          */
765         for (nvectors = 0, i = 0; i < nvqs; i++)
766                 if (vq_info[i].vqai_intr != NULL)
767                         nvectors++;
768
769         if (vtpci_disable_msix != 0 ||
770             sc->vtpci_flags & VIRTIO_PCI_FLAG_NO_MSIX ||
771             flags & VIRTIO_ALLOC_VQS_DISABLE_MSIX ||
772             vtpci_alloc_msix(sc, nvectors) != 0) {
773                 /*
774                  * Use MSI interrupts if available. Otherwise, we fallback
775                  * to legacy interrupts.
776                  */
777                 if ((sc->vtpci_flags & VIRTIO_PCI_FLAG_NO_MSI) == 0 &&
778                     vtpci_alloc_msi(sc) == 0)
779                         sc->vtpci_flags |= VIRTIO_PCI_FLAG_MSI;
780
781                 sc->vtpci_nintr_res = 1;
782         }
783
784         error = vtpci_alloc_intr_resources(sc, nvqs, vq_info);
785
786         return (error);
787 }
788
789 static int
790 vtpci_alloc_intr_resources(struct vtpci_softc *sc, int nvqs,
791     struct vq_alloc_info *vq_info)
792 {
793         device_t dev;
794         struct resource *irq;
795         struct vtpci_virtqueue *vqx;
796         int i, rid, flags, res_idx;
797
798         dev = sc->vtpci_dev;
799         flags = RF_ACTIVE;
800
801         if ((sc->vtpci_flags &
802             (VIRTIO_PCI_FLAG_MSI | VIRTIO_PCI_FLAG_MSIX)) == 0) {
803                 rid = 0;
804                 flags |= RF_SHAREABLE;
805         } else
806                 rid = 1;
807
808         for (i = 0; i < sc->vtpci_nintr_res; i++) {
809                 irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, flags);
810                 if (irq == NULL)
811                         return (ENXIO);
812
813                 sc->vtpci_intr_res[i].irq = irq;
814                 sc->vtpci_intr_res[i].rid = rid++;
815         }
816
817         /*
818          * Map the virtqueue into the correct index in vq_intr_res[]. Note the
819          * first index is reserved for configuration changes notifications.
820          */
821         for (i = 0, res_idx = 1; i < nvqs; i++) {
822                 vqx = &sc->vtpci_vqx[i];
823
824                 if (sc->vtpci_flags & VIRTIO_PCI_FLAG_MSIX) {
825                         if (vq_info[i].vqai_intr == NULL)
826                                 vqx->ires_idx = -1;
827                         else if (sc->vtpci_flags & VIRTIO_PCI_FLAG_SHARED_MSIX)
828                                 vqx->ires_idx = res_idx;
829                         else
830                                 vqx->ires_idx = res_idx++;
831                 } else
832                         vqx->ires_idx = -1;
833         }
834
835         return (0);
836 }
837
838 static int
839 vtpci_alloc_msi(struct vtpci_softc *sc)
840 {
841         device_t dev;
842         int nmsi;
843         u_int irq_flags;
844
845         dev = sc->vtpci_dev;
846         nmsi = pci_msi_count(dev);
847
848         if (nmsi < 1)
849                 return (1);
850
851         sc->vtpci_irq_rid = 0;
852         sc->vtpci_irq_type = pci_alloc_1intr(dev, 1,
853             &sc->vtpci_irq_rid, &irq_flags);
854
855
856         return (1);
857 }
858
859 static int
860 vtpci_alloc_msix(struct vtpci_softc *sc, int nvectors)
861 {
862         /* XXX(vsrinivas): Huh? Is this how MSI-X works?*/
863         /* XXX(vsrinivas): All of this was disabled... */
864 #ifdef OLD_MSI
865         device_t dev;
866         int nmsix, cnt, required;
867
868         dev = sc->vtpci_dev;
869
870         nmsix = pci_msix_count(dev);
871         if (nmsix < 1)
872                 return (1);
873
874         /* An additional vector is needed for the config changes. */
875         required = nvectors + 1;
876         if (nmsix >= required) {
877                 cnt = required;
878                 if (pci_alloc_msix(dev, &cnt) == 0 && cnt >= required)
879                         goto out;
880
881                 pci_release_msi(dev);
882         }
883
884         /* Attempt shared MSIX configuration. */
885         required = 2;
886         if (nmsix >= required) {
887                 cnt = required;
888                 if (pci_alloc_msix(dev, &cnt) == 0 && cnt >= required) {
889                         sc->vtpci_flags |= VIRTIO_PCI_FLAG_SHARED_MSIX;
890                         goto out;
891                 }
892
893                 pci_release_msi(dev);
894         }
895
896         return (1);
897
898 out:
899         sc->vtpci_nintr_res = required;
900         sc->vtpci_flags |= VIRTIO_PCI_FLAG_MSIX;
901
902         if (bootverbose) {
903                 if (sc->vtpci_flags & VIRTIO_PCI_FLAG_SHARED_MSIX)
904                         device_printf(dev, "using shared virtqueue MSIX\n");
905                 else
906                         device_printf(dev, "using per virtqueue MSIX\n");
907         }
908 #endif
909         return (0);
910 }
911
912 static int
913 vtpci_register_msix_vector(struct vtpci_softc *sc, int offset, int res_idx)
914 {
915         device_t dev;
916         uint16_t vector;
917
918         dev = sc->vtpci_dev;
919
920         if (offset != VIRTIO_MSI_CONFIG_VECTOR &&
921             offset != VIRTIO_MSI_QUEUE_VECTOR)
922                 return (EINVAL);
923
924         if (res_idx != -1) {
925                 /* Map from rid to host vector. */
926                 vector = sc->vtpci_intr_res[res_idx].rid - 1;
927         } else
928                 vector = VIRTIO_MSI_NO_VECTOR;
929
930         /* The first resource is special; make sure it is used correctly. */
931         if (res_idx == 0) {
932                 KASSERT(vector == 0, ("unexpected config vector"));
933                 KASSERT(offset == VIRTIO_MSI_CONFIG_VECTOR,
934                     ("unexpected config offset"));
935         }
936
937         vtpci_write_config_2(sc, offset, vector);
938
939         if (vtpci_read_config_2(sc, offset) != vector) {
940                 device_printf(dev, "insufficient host resources for "
941                     "MSIX interrupts\n");
942                 return (ENODEV);
943         }
944
945         return (0);
946 }
947
948 static void
949 vtpci_free_interrupts(struct vtpci_softc *sc)
950 {
951         device_t dev;
952         struct vtpci_intr_resource *ires;
953         int i;
954
955         dev = sc->vtpci_dev;
956         sc->vtpci_nintr_res = 0;
957
958         if (sc->vtpci_flags & (VIRTIO_PCI_FLAG_MSI | VIRTIO_PCI_FLAG_MSIX)) {
959                 pci_release_msi(dev);
960                 sc->vtpci_flags &= ~(VIRTIO_PCI_FLAG_MSI |
961                     VIRTIO_PCI_FLAG_MSIX | VIRTIO_PCI_FLAG_SHARED_MSIX);
962         }
963
964         for (i = 0; i < 1 + VIRTIO_MAX_VIRTQUEUES; i++) {
965                 ires = &sc->vtpci_intr_res[i];
966
967                 if (ires->intrhand != NULL) {
968                         bus_teardown_intr(dev, ires->irq, ires->intrhand);
969                         ires->intrhand = NULL;
970                 }
971
972                 if (ires->irq != NULL) {
973                         bus_release_resource(dev, SYS_RES_IRQ, ires->rid,
974                             ires->irq);
975                         ires->irq = NULL;
976                 }
977
978                 ires->rid = -1;
979         }
980 }
981
982 static void
983 vtpci_free_virtqueues(struct vtpci_softc *sc)
984 {
985         struct vtpci_virtqueue *vqx;
986         int i;
987
988         sc->vtpci_nvqs = 0;
989
990         for (i = 0; i < VIRTIO_MAX_VIRTQUEUES; i++) {
991                 vqx = &sc->vtpci_vqx[i];
992
993                 if (vqx->vq != NULL) {
994                         virtqueue_free(vqx->vq);
995                         vqx->vq = NULL;
996                 }
997         }
998 }
999
1000 static void
1001 vtpci_release_child_resources(struct vtpci_softc *sc)
1002 {
1003
1004         vtpci_free_interrupts(sc);
1005         vtpci_free_virtqueues(sc);
1006 }
1007
1008 static void
1009 vtpci_reset(struct vtpci_softc *sc)
1010 {
1011
1012         /*
1013          * Setting the status to RESET sets the host device to
1014          * the original, uninitialized state.
1015          */
1016         vtpci_set_status(sc->vtpci_dev, VIRTIO_CONFIG_STATUS_RESET);
1017 }
1018
1019 static int
1020 vtpci_legacy_intr(void *xsc)
1021 {
1022         struct vtpci_softc *sc;
1023         struct vtpci_virtqueue *vqx;
1024         int i;
1025         uint8_t isr;
1026
1027         sc = xsc;
1028         vqx = &sc->vtpci_vqx[0];
1029
1030         /* Reading the ISR also clears it. */
1031         isr = vtpci_read_config_1(sc, VIRTIO_PCI_ISR);
1032
1033         if (isr & VIRTIO_PCI_ISR_CONFIG)
1034                 vtpci_config_intr(sc);
1035
1036         if (isr & VIRTIO_PCI_ISR_INTR)
1037                 for (i = 0; i < sc->vtpci_nvqs; i++, vqx++)
1038                         virtqueue_intr(vqx->vq);
1039
1040         return isr;
1041 }
1042
1043 static int
1044 vtpci_vq_shared_intr(void *xsc)
1045 {
1046         struct vtpci_softc *sc;
1047         struct vtpci_virtqueue *vqx;
1048         int i, rc;
1049
1050         rc = 0;
1051         sc = xsc;
1052         vqx = &sc->vtpci_vqx[0];
1053
1054         for (i = 0; i < sc->vtpci_nvqs; i++, vqx++)
1055                 rc |= virtqueue_intr(vqx->vq);
1056
1057         return rc;
1058 }
1059
1060 static int
1061 vtpci_vq_intr(void *xvq)
1062 {
1063         struct virtqueue *vq;
1064         int rc;
1065
1066         vq = xvq;
1067         rc = virtqueue_intr(vq);
1068
1069         return rc;
1070 }
1071
1072 static int
1073 vtpci_config_intr(void *xsc)
1074 {
1075         struct vtpci_softc *sc;
1076         device_t child;
1077         int rc;
1078
1079         rc = 0;
1080         sc = xsc;
1081         child = sc->vtpci_child_dev;
1082
1083         if (child != NULL)
1084                 rc = VIRTIO_CONFIG_CHANGE(child);
1085
1086         return rc;
1087 }