1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2011, Bryan Venteicher <bryanv@FreeBSD.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
12 * disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /* Driver for the legacy VirtIO PCI interface. */
30
31 #include <sys/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/lock.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/endian.h>
39
40 #include <machine/bus.h>
41 #include <machine/resource.h>
42 #include <sys/bus.h>
43 #include <sys/rman.h>
44
45 #include <dev/pci/pcivar.h>
46 #include <dev/pci/pcireg.h>
47
48 #include <dev/virtio/virtio.h>
49 #include <dev/virtio/virtqueue.h>
50 #include <dev/virtio/pci/virtio_pci.h>
51 #include <dev/virtio/pci/virtio_pci_legacy_var.h>
52
53 #include "virtio_bus_if.h"
54 #include "virtio_pci_if.h"
55 #include "virtio_if.h"
56
57 struct vtpci_legacy_softc {
58 device_t vtpci_dev;
59 struct vtpci_common vtpci_common;
60 int vtpci_res_type;
61 struct resource *vtpci_res;
62 struct resource *vtpci_msix_table_res;
63 struct resource *vtpci_msix_pba_res;
64 };
65
66 static int vtpci_legacy_probe(device_t);
67 static int vtpci_legacy_attach(device_t);
68 static int vtpci_legacy_detach(device_t);
69 static int vtpci_legacy_suspend(device_t);
70 static int vtpci_legacy_resume(device_t);
71 static int vtpci_legacy_shutdown(device_t);
72
73 static void vtpci_legacy_driver_added(device_t, driver_t *);
74 static void vtpci_legacy_child_detached(device_t, device_t);
75 static int vtpci_legacy_read_ivar(device_t, device_t, int, uintptr_t *);
76 static int vtpci_legacy_write_ivar(device_t, device_t, int, uintptr_t);
77
78 static uint8_t vtpci_legacy_read_isr(device_t);
79 static uint16_t vtpci_legacy_get_vq_size(device_t, int);
80 static bus_size_t vtpci_legacy_get_vq_notify_off(device_t, int);
81 static void vtpci_legacy_set_vq(device_t, struct virtqueue *);
82 static void vtpci_legacy_disable_vq(device_t, int);
83 static int vtpci_legacy_register_cfg_msix(device_t,
84 struct vtpci_interrupt *);
85 static int vtpci_legacy_register_vq_msix(device_t, int idx,
86 struct vtpci_interrupt *);
87
88 static uint64_t vtpci_legacy_negotiate_features(device_t, uint64_t);
89 static int vtpci_legacy_with_feature(device_t, uint64_t);
90 static int vtpci_legacy_alloc_virtqueues(device_t, int, int,
91 struct vq_alloc_info *);
92 static int vtpci_legacy_setup_interrupts(device_t, enum intr_type);
93 static void vtpci_legacy_stop(device_t);
94 static int vtpci_legacy_reinit(device_t, uint64_t);
95 static void vtpci_legacy_reinit_complete(device_t);
96 static void vtpci_legacy_notify_vq(device_t, uint16_t, bus_size_t);
97 static void vtpci_legacy_read_dev_config(device_t, bus_size_t, void *, int);
98 static void vtpci_legacy_write_dev_config(device_t, bus_size_t, const void *, int);
99
100 static bool vtpci_legacy_setup_msix(struct vtpci_legacy_softc *sc);
101 static void vtpci_legacy_teardown_msix(struct vtpci_legacy_softc *sc);
102 static int vtpci_legacy_alloc_resources(struct vtpci_legacy_softc *);
103 static void vtpci_legacy_free_resources(struct vtpci_legacy_softc *);
104
105 static void vtpci_legacy_probe_and_attach_child(struct vtpci_legacy_softc *);
106
107 static uint8_t vtpci_legacy_get_status(struct vtpci_legacy_softc *);
108 static void vtpci_legacy_set_status(struct vtpci_legacy_softc *, uint8_t);
109 static void vtpci_legacy_select_virtqueue(struct vtpci_legacy_softc *, int);
110 static void vtpci_legacy_reset(struct vtpci_legacy_softc *);
111
112 #define VIRTIO_PCI_LEGACY_CONFIG(_sc) \
113 VIRTIO_PCI_CONFIG_OFF(vtpci_is_msix_enabled(&(_sc)->vtpci_common))
114
115 #define vtpci_legacy_read_config_1(sc, o) \
116 bus_read_1((sc)->vtpci_res, (o))
117 #define vtpci_legacy_write_config_1(sc, o, v) \
118 bus_write_1((sc)->vtpci_res, (o), (v))
119 /*
120 * VirtIO specifies that PCI Configuration area is guest endian. However,
121 * since PCI devices are inherently little-endian, on big-endian systems
122 * the bus layer transparently converts it to BE. For virtio-legacy, this
123 * conversion is undesired, so an extra byte swap is required to fix it.
124 */
125 #define vtpci_legacy_read_config_2(sc, o) \
126 le16toh(bus_read_2((sc)->vtpci_res, (o)))
127 #define vtpci_legacy_read_config_4(sc, o) \
128 le32toh(bus_read_4((sc)->vtpci_res, (o)))
129 #define vtpci_legacy_write_config_2(sc, o, v) \
130 bus_write_2((sc)->vtpci_res, (o), (htole16(v)))
131 #define vtpci_legacy_write_config_4(sc, o, v) \
132 bus_write_4((sc)->vtpci_res, (o), (htole32(v)))
133 /* PCI Header LE. On BE systems the bus layer takes care of byte swapping. */
134 #define vtpci_legacy_read_header_2(sc, o) \
135 bus_read_2((sc)->vtpci_res, (o))
136 #define vtpci_legacy_read_header_4(sc, o) \
137 bus_read_4((sc)->vtpci_res, (o))
138 #define vtpci_legacy_write_header_2(sc, o, v) \
139 bus_write_2((sc)->vtpci_res, (o), (v))
140 #define vtpci_legacy_write_header_4(sc, o, v) \
141 bus_write_4((sc)->vtpci_res, (o), (v))
142
143 static device_method_t vtpci_legacy_methods[] = {
144 /* Device interface. */
145 DEVMETHOD(device_probe, vtpci_legacy_probe),
146 DEVMETHOD(device_attach, vtpci_legacy_attach),
147 DEVMETHOD(device_detach, vtpci_legacy_detach),
148 DEVMETHOD(device_suspend, vtpci_legacy_suspend),
149 DEVMETHOD(device_resume, vtpci_legacy_resume),
150 DEVMETHOD(device_shutdown, vtpci_legacy_shutdown),
151
152 /* Bus interface. */
153 DEVMETHOD(bus_driver_added, vtpci_legacy_driver_added),
154 DEVMETHOD(bus_child_detached, vtpci_legacy_child_detached),
155 DEVMETHOD(bus_child_pnpinfo_str, virtio_child_pnpinfo_str),
156 DEVMETHOD(bus_read_ivar, vtpci_legacy_read_ivar),
157 DEVMETHOD(bus_write_ivar, vtpci_legacy_write_ivar),
158
159 /* VirtIO PCI interface. */
160 DEVMETHOD(virtio_pci_read_isr, vtpci_legacy_read_isr),
161 DEVMETHOD(virtio_pci_get_vq_size, vtpci_legacy_get_vq_size),
162 DEVMETHOD(virtio_pci_get_vq_notify_off, vtpci_legacy_get_vq_notify_off),
163 DEVMETHOD(virtio_pci_set_vq, vtpci_legacy_set_vq),
164 DEVMETHOD(virtio_pci_disable_vq, vtpci_legacy_disable_vq),
165 DEVMETHOD(virtio_pci_register_cfg_msix, vtpci_legacy_register_cfg_msix),
166 DEVMETHOD(virtio_pci_register_vq_msix, vtpci_legacy_register_vq_msix),
167
168 /* VirtIO bus interface. */
169 DEVMETHOD(virtio_bus_negotiate_features, vtpci_legacy_negotiate_features),
170 DEVMETHOD(virtio_bus_with_feature, vtpci_legacy_with_feature),
171 DEVMETHOD(virtio_bus_alloc_virtqueues, vtpci_legacy_alloc_virtqueues),
172 DEVMETHOD(virtio_bus_setup_intr, vtpci_legacy_setup_interrupts),
173 DEVMETHOD(virtio_bus_stop, vtpci_legacy_stop),
174 DEVMETHOD(virtio_bus_reinit, vtpci_legacy_reinit),
175 DEVMETHOD(virtio_bus_reinit_complete, vtpci_legacy_reinit_complete),
176 DEVMETHOD(virtio_bus_notify_vq, vtpci_legacy_notify_vq),
177 DEVMETHOD(virtio_bus_read_device_config, vtpci_legacy_read_dev_config),
178 DEVMETHOD(virtio_bus_write_device_config, vtpci_legacy_write_dev_config),
179
180 DEVMETHOD_END
181 };
182
183 static driver_t vtpci_legacy_driver = {
184 .name = "virtio_pci",
185 .methods = vtpci_legacy_methods,
186 .size = sizeof(struct vtpci_legacy_softc)
187 };
188
189 devclass_t vtpci_legacy_devclass;
190
191 DRIVER_MODULE(virtio_pci_legacy, pci, vtpci_legacy_driver,
192 vtpci_legacy_devclass, 0, 0);
193
194 static int
vtpci_legacy_probe(device_t dev)195 vtpci_legacy_probe(device_t dev)
196 {
197 char desc[64];
198 const char *name;
199
200 if (pci_get_vendor(dev) != VIRTIO_PCI_VENDORID)
201 return (ENXIO);
202
203 if (pci_get_device(dev) < VIRTIO_PCI_DEVICEID_MIN ||
204 pci_get_device(dev) > VIRTIO_PCI_DEVICEID_LEGACY_MAX)
205 return (ENXIO);
206
207 if (pci_get_revid(dev) != VIRTIO_PCI_ABI_VERSION)
208 return (ENXIO);
209
210 name = virtio_device_name(pci_get_subdevice(dev));
211 if (name == NULL)
212 name = "Unknown";
213
214 snprintf(desc, sizeof(desc), "VirtIO PCI (legacy) %s adapter", name);
215 device_set_desc_copy(dev, desc);
216
217 /* Prefer transitional modern VirtIO PCI. */
218 return (BUS_PROBE_LOW_PRIORITY);
219 }
220
221 static int
vtpci_legacy_attach(device_t dev)222 vtpci_legacy_attach(device_t dev)
223 {
224 struct vtpci_legacy_softc *sc;
225 int error;
226
227 sc = device_get_softc(dev);
228 sc->vtpci_dev = dev;
229 vtpci_init(&sc->vtpci_common, dev, false);
230
231 error = vtpci_legacy_alloc_resources(sc);
232 if (error) {
233 device_printf(dev, "cannot map I/O space nor memory space\n");
234 return (error);
235 }
236
237 if (vtpci_is_msix_available(&sc->vtpci_common) &&
238 !vtpci_legacy_setup_msix(sc)) {
239 device_printf(dev, "cannot setup MSI-x resources\n");
240 error = ENXIO;
241 goto fail;
242 }
243
244 vtpci_legacy_reset(sc);
245
246 /* Tell the host we've noticed this device. */
247 vtpci_legacy_set_status(sc, VIRTIO_CONFIG_STATUS_ACK);
248
249 error = vtpci_add_child(&sc->vtpci_common);
250 if (error)
251 goto fail;
252
253 vtpci_legacy_probe_and_attach_child(sc);
254
255 return (0);
256
257 fail:
258 vtpci_legacy_set_status(sc, VIRTIO_CONFIG_STATUS_FAILED);
259 vtpci_legacy_detach(dev);
260
261 return (error);
262 }
263
264 static int
vtpci_legacy_detach(device_t dev)265 vtpci_legacy_detach(device_t dev)
266 {
267 struct vtpci_legacy_softc *sc;
268 int error;
269
270 sc = device_get_softc(dev);
271
272 error = vtpci_delete_child(&sc->vtpci_common);
273 if (error)
274 return (error);
275
276 vtpci_legacy_reset(sc);
277 vtpci_legacy_teardown_msix(sc);
278 vtpci_legacy_free_resources(sc);
279
280 return (0);
281 }
282
283 static int
vtpci_legacy_suspend(device_t dev)284 vtpci_legacy_suspend(device_t dev)
285 {
286 return (bus_generic_suspend(dev));
287 }
288
289 static int
vtpci_legacy_resume(device_t dev)290 vtpci_legacy_resume(device_t dev)
291 {
292 return (bus_generic_resume(dev));
293 }
294
295 static int
vtpci_legacy_shutdown(device_t dev)296 vtpci_legacy_shutdown(device_t dev)
297 {
298 (void) bus_generic_shutdown(dev);
299 /* Forcibly stop the host device. */
300 vtpci_legacy_stop(dev);
301
302 return (0);
303 }
304
305 static void
vtpci_legacy_driver_added(device_t dev,driver_t * driver)306 vtpci_legacy_driver_added(device_t dev, driver_t *driver)
307 {
308 vtpci_legacy_probe_and_attach_child(device_get_softc(dev));
309 }
310
311 static void
vtpci_legacy_child_detached(device_t dev,device_t child)312 vtpci_legacy_child_detached(device_t dev, device_t child)
313 {
314 struct vtpci_legacy_softc *sc;
315
316 sc = device_get_softc(dev);
317
318 vtpci_legacy_reset(sc);
319 vtpci_child_detached(&sc->vtpci_common);
320
321 /* After the reset, retell the host we've noticed this device. */
322 vtpci_legacy_set_status(sc, VIRTIO_CONFIG_STATUS_ACK);
323 }
324
325 static int
vtpci_legacy_read_ivar(device_t dev,device_t child,int index,uintptr_t * result)326 vtpci_legacy_read_ivar(device_t dev, device_t child, int index,
327 uintptr_t *result)
328 {
329 struct vtpci_legacy_softc *sc;
330 struct vtpci_common *cn;
331
332 sc = device_get_softc(dev);
333 cn = &sc->vtpci_common;
334
335 if (vtpci_child_device(cn) != child)
336 return (ENOENT);
337
338 switch (index) {
339 case VIRTIO_IVAR_DEVTYPE:
340 *result = pci_get_subdevice(dev);
341 break;
342 default:
343 return (vtpci_read_ivar(cn, index, result));
344 }
345
346 return (0);
347 }
348
349 static int
vtpci_legacy_write_ivar(device_t dev,device_t child,int index,uintptr_t value)350 vtpci_legacy_write_ivar(device_t dev, device_t child, int index, uintptr_t value)
351 {
352 struct vtpci_legacy_softc *sc;
353 struct vtpci_common *cn;
354
355 sc = device_get_softc(dev);
356 cn = &sc->vtpci_common;
357
358 if (vtpci_child_device(cn) != child)
359 return (ENOENT);
360
361 switch (index) {
362 default:
363 return (vtpci_write_ivar(cn, index, value));
364 }
365
366 return (0);
367 }
368
369 static uint64_t
vtpci_legacy_negotiate_features(device_t dev,uint64_t child_features)370 vtpci_legacy_negotiate_features(device_t dev, uint64_t child_features)
371 {
372 struct vtpci_legacy_softc *sc;
373 uint64_t host_features, features;
374
375 sc = device_get_softc(dev);
376 host_features = vtpci_legacy_read_header_4(sc, VIRTIO_PCI_HOST_FEATURES);
377
378 features = vtpci_negotiate_features(&sc->vtpci_common,
379 child_features, host_features);
380 vtpci_legacy_write_header_4(sc, VIRTIO_PCI_GUEST_FEATURES, features);
381
382 return (features);
383 }
384
385 static int
vtpci_legacy_with_feature(device_t dev,uint64_t feature)386 vtpci_legacy_with_feature(device_t dev, uint64_t feature)
387 {
388 struct vtpci_legacy_softc *sc;
389
390 sc = device_get_softc(dev);
391
392 return (vtpci_with_feature(&sc->vtpci_common, feature));
393 }
394
395 static int
vtpci_legacy_alloc_virtqueues(device_t dev,int flags,int nvqs,struct vq_alloc_info * vq_info)396 vtpci_legacy_alloc_virtqueues(device_t dev, int flags, int nvqs,
397 struct vq_alloc_info *vq_info)
398 {
399 struct vtpci_legacy_softc *sc;
400 struct vtpci_common *cn;
401
402 sc = device_get_softc(dev);
403 cn = &sc->vtpci_common;
404
405 return (vtpci_alloc_virtqueues(cn, flags, nvqs, vq_info));
406 }
407
408 static int
vtpci_legacy_setup_interrupts(device_t dev,enum intr_type type)409 vtpci_legacy_setup_interrupts(device_t dev, enum intr_type type)
410 {
411 struct vtpci_legacy_softc *sc;
412
413 sc = device_get_softc(dev);
414
415 return (vtpci_setup_interrupts(&sc->vtpci_common, type));
416 }
417
418 static void
vtpci_legacy_stop(device_t dev)419 vtpci_legacy_stop(device_t dev)
420 {
421 vtpci_legacy_reset(device_get_softc(dev));
422 }
423
424 static int
vtpci_legacy_reinit(device_t dev,uint64_t features)425 vtpci_legacy_reinit(device_t dev, uint64_t features)
426 {
427 struct vtpci_legacy_softc *sc;
428 struct vtpci_common *cn;
429 int error;
430
431 sc = device_get_softc(dev);
432 cn = &sc->vtpci_common;
433
434 /*
435 * Redrive the device initialization. This is a bit of an abuse of
436 * the specification, but VirtualBox, QEMU/KVM, and BHyVe seem to
437 * play nice.
438 *
439 * We do not allow the host device to change from what was originally
440 * negotiated beyond what the guest driver changed. MSIX state should
441 * not change, number of virtqueues and their size remain the same, etc.
442 * This will need to be rethought when we want to support migration.
443 */
444
445 if (vtpci_legacy_get_status(sc) != VIRTIO_CONFIG_STATUS_RESET)
446 vtpci_legacy_stop(dev);
447
448 /*
449 * Quickly drive the status through ACK and DRIVER. The device does
450 * not become usable again until DRIVER_OK in reinit complete.
451 */
452 vtpci_legacy_set_status(sc, VIRTIO_CONFIG_STATUS_ACK);
453 vtpci_legacy_set_status(sc, VIRTIO_CONFIG_STATUS_DRIVER);
454
455 vtpci_legacy_negotiate_features(dev, features);
456
457 error = vtpci_reinit(cn);
458 if (error)
459 return (error);
460
461 return (0);
462 }
463
464 static void
vtpci_legacy_reinit_complete(device_t dev)465 vtpci_legacy_reinit_complete(device_t dev)
466 {
467 struct vtpci_legacy_softc *sc;
468
469 sc = device_get_softc(dev);
470
471 vtpci_legacy_set_status(sc, VIRTIO_CONFIG_STATUS_DRIVER_OK);
472 }
473
474 static void
vtpci_legacy_notify_vq(device_t dev,uint16_t queue,bus_size_t offset)475 vtpci_legacy_notify_vq(device_t dev, uint16_t queue, bus_size_t offset)
476 {
477 struct vtpci_legacy_softc *sc;
478
479 sc = device_get_softc(dev);
480 MPASS(offset == VIRTIO_PCI_QUEUE_NOTIFY);
481
482 vtpci_legacy_write_header_2(sc, offset, queue);
483 }
484
485 static uint8_t
vtpci_legacy_get_status(struct vtpci_legacy_softc * sc)486 vtpci_legacy_get_status(struct vtpci_legacy_softc *sc)
487 {
488 return (vtpci_legacy_read_config_1(sc, VIRTIO_PCI_STATUS));
489 }
490
491 static void
vtpci_legacy_set_status(struct vtpci_legacy_softc * sc,uint8_t status)492 vtpci_legacy_set_status(struct vtpci_legacy_softc *sc, uint8_t status)
493 {
494 if (status != VIRTIO_CONFIG_STATUS_RESET)
495 status |= vtpci_legacy_get_status(sc);
496
497 vtpci_legacy_write_config_1(sc, VIRTIO_PCI_STATUS, status);
498 }
499
500 static void
vtpci_legacy_read_dev_config(device_t dev,bus_size_t offset,void * dst,int length)501 vtpci_legacy_read_dev_config(device_t dev, bus_size_t offset,
502 void *dst, int length)
503 {
504 struct vtpci_legacy_softc *sc;
505 bus_size_t off;
506 uint8_t *d;
507 int i;
508
509 sc = device_get_softc(dev);
510 off = VIRTIO_PCI_LEGACY_CONFIG(sc) + offset;
511
512 d = dst;
513 for (i = 0; i < length; i++) {
514 d[i] = vtpci_legacy_read_config_1(sc, off + i);
515 }
516 }
517
518 static void
vtpci_legacy_write_dev_config(device_t dev,bus_size_t offset,const void * src,int length)519 vtpci_legacy_write_dev_config(device_t dev, bus_size_t offset,
520 const void *src, int length)
521 {
522 struct vtpci_legacy_softc *sc;
523 bus_size_t off;
524 const uint8_t *s;
525 int i;
526
527 sc = device_get_softc(dev);
528 off = VIRTIO_PCI_LEGACY_CONFIG(sc) + offset;
529
530 s = src;
531 for (i = 0; i < length; i++) {
532 vtpci_legacy_write_config_1(sc, off + i, s[i]);
533 }
534 }
535
536 static bool
vtpci_legacy_setup_msix(struct vtpci_legacy_softc * sc)537 vtpci_legacy_setup_msix(struct vtpci_legacy_softc *sc)
538 {
539 device_t dev;
540 int rid, table_rid;
541
542 dev = sc->vtpci_dev;
543
544 rid = table_rid = pci_msix_table_bar(dev);
545 if (rid != PCIR_BAR(0)) {
546 sc->vtpci_msix_table_res = bus_alloc_resource_any(
547 dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
548 if (sc->vtpci_msix_table_res == NULL)
549 return (false);
550 }
551
552 rid = pci_msix_pba_bar(dev);
553 if (rid != table_rid && rid != PCIR_BAR(0)) {
554 sc->vtpci_msix_pba_res = bus_alloc_resource_any(
555 dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
556 if (sc->vtpci_msix_pba_res == NULL)
557 return (false);
558 }
559
560 return (true);
561 }
562
563 static void
vtpci_legacy_teardown_msix(struct vtpci_legacy_softc * sc)564 vtpci_legacy_teardown_msix(struct vtpci_legacy_softc *sc)
565 {
566 device_t dev;
567
568 dev = sc->vtpci_dev;
569
570 if (sc->vtpci_msix_pba_res != NULL) {
571 bus_release_resource(dev, SYS_RES_MEMORY,
572 rman_get_rid(sc->vtpci_msix_pba_res),
573 sc->vtpci_msix_pba_res);
574 sc->vtpci_msix_pba_res = NULL;
575 }
576 if (sc->vtpci_msix_table_res != NULL) {
577 bus_release_resource(dev, SYS_RES_MEMORY,
578 rman_get_rid(sc->vtpci_msix_table_res),
579 sc->vtpci_msix_table_res);
580 sc->vtpci_msix_table_res = NULL;
581 }
582 }
583
584 static int
vtpci_legacy_alloc_resources(struct vtpci_legacy_softc * sc)585 vtpci_legacy_alloc_resources(struct vtpci_legacy_softc *sc)
586 {
587 const int res_types[] = { SYS_RES_IOPORT, SYS_RES_MEMORY };
588 device_t dev;
589 int rid, i;
590
591 dev = sc->vtpci_dev;
592
593 /*
594 * Most hypervisors export the common configuration structure in IO
595 * space, but some use memory space; try both.
596 */
597 for (i = 0; nitems(res_types); i++) {
598 rid = PCIR_BAR(0);
599 sc->vtpci_res_type = res_types[i];
600 sc->vtpci_res = bus_alloc_resource_any(dev, res_types[i], &rid,
601 RF_ACTIVE);
602 if (sc->vtpci_res != NULL)
603 break;
604 }
605 if (sc->vtpci_res == NULL)
606 return (ENXIO);
607
608 return (0);
609 }
610
611 static void
vtpci_legacy_free_resources(struct vtpci_legacy_softc * sc)612 vtpci_legacy_free_resources(struct vtpci_legacy_softc *sc)
613 {
614 device_t dev;
615
616 dev = sc->vtpci_dev;
617
618 if (sc->vtpci_res != NULL) {
619 bus_release_resource(dev, sc->vtpci_res_type, PCIR_BAR(0),
620 sc->vtpci_res);
621 sc->vtpci_res = NULL;
622 }
623 }
624
625 static void
vtpci_legacy_probe_and_attach_child(struct vtpci_legacy_softc * sc)626 vtpci_legacy_probe_and_attach_child(struct vtpci_legacy_softc *sc)
627 {
628 device_t dev, child;
629
630 dev = sc->vtpci_dev;
631 child = vtpci_child_device(&sc->vtpci_common);
632
633 if (child == NULL || device_get_state(child) != DS_NOTPRESENT)
634 return;
635
636 if (device_probe(child) != 0)
637 return;
638
639 vtpci_legacy_set_status(sc, VIRTIO_CONFIG_STATUS_DRIVER);
640
641 if (device_attach(child) != 0) {
642 vtpci_legacy_set_status(sc, VIRTIO_CONFIG_STATUS_FAILED);
643 /* Reset status for future attempt. */
644 vtpci_legacy_child_detached(dev, child);
645 } else {
646 vtpci_legacy_set_status(sc, VIRTIO_CONFIG_STATUS_DRIVER_OK);
647 VIRTIO_ATTACH_COMPLETED(child);
648 }
649 }
650
651 static int
vtpci_legacy_register_msix(struct vtpci_legacy_softc * sc,int offset,struct vtpci_interrupt * intr)652 vtpci_legacy_register_msix(struct vtpci_legacy_softc *sc, int offset,
653 struct vtpci_interrupt *intr)
654 {
655 uint16_t vector;
656
657 if (intr != NULL) {
658 /* Map from guest rid to host vector. */
659 vector = intr->vti_rid - 1;
660 } else
661 vector = VIRTIO_MSI_NO_VECTOR;
662
663 vtpci_legacy_write_header_2(sc, offset, vector);
664 return (vtpci_legacy_read_header_2(sc, offset) == vector ? 0 : ENODEV);
665 }
666
667 static int
vtpci_legacy_register_cfg_msix(device_t dev,struct vtpci_interrupt * intr)668 vtpci_legacy_register_cfg_msix(device_t dev, struct vtpci_interrupt *intr)
669 {
670 struct vtpci_legacy_softc *sc;
671 int error;
672
673 sc = device_get_softc(dev);
674
675 error = vtpci_legacy_register_msix(sc, VIRTIO_MSI_CONFIG_VECTOR, intr);
676 if (error) {
677 device_printf(dev,
678 "unable to register config MSIX interrupt\n");
679 return (error);
680 }
681
682 return (0);
683 }
684
685 static int
vtpci_legacy_register_vq_msix(device_t dev,int idx,struct vtpci_interrupt * intr)686 vtpci_legacy_register_vq_msix(device_t dev, int idx,
687 struct vtpci_interrupt *intr)
688 {
689 struct vtpci_legacy_softc *sc;
690 int error;
691
692 sc = device_get_softc(dev);
693
694 vtpci_legacy_select_virtqueue(sc, idx);
695 error = vtpci_legacy_register_msix(sc, VIRTIO_MSI_QUEUE_VECTOR, intr);
696 if (error) {
697 device_printf(dev,
698 "unable to register virtqueue MSIX interrupt\n");
699 return (error);
700 }
701
702 return (0);
703 }
704
705 static void
vtpci_legacy_reset(struct vtpci_legacy_softc * sc)706 vtpci_legacy_reset(struct vtpci_legacy_softc *sc)
707 {
708 /*
709 * Setting the status to RESET sets the host device to the
710 * original, uninitialized state.
711 */
712 vtpci_legacy_set_status(sc, VIRTIO_CONFIG_STATUS_RESET);
713 (void) vtpci_legacy_get_status(sc);
714 }
715
716 static void
vtpci_legacy_select_virtqueue(struct vtpci_legacy_softc * sc,int idx)717 vtpci_legacy_select_virtqueue(struct vtpci_legacy_softc *sc, int idx)
718 {
719 vtpci_legacy_write_header_2(sc, VIRTIO_PCI_QUEUE_SEL, idx);
720 }
721
722 static uint8_t
vtpci_legacy_read_isr(device_t dev)723 vtpci_legacy_read_isr(device_t dev)
724 {
725 struct vtpci_legacy_softc *sc;
726
727 sc = device_get_softc(dev);
728
729 return (vtpci_legacy_read_config_1(sc, VIRTIO_PCI_ISR));
730 }
731
732 static uint16_t
vtpci_legacy_get_vq_size(device_t dev,int idx)733 vtpci_legacy_get_vq_size(device_t dev, int idx)
734 {
735 struct vtpci_legacy_softc *sc;
736
737 sc = device_get_softc(dev);
738
739 vtpci_legacy_select_virtqueue(sc, idx);
740 return (vtpci_legacy_read_header_2(sc, VIRTIO_PCI_QUEUE_NUM));
741 }
742
743 static bus_size_t
vtpci_legacy_get_vq_notify_off(device_t dev,int idx)744 vtpci_legacy_get_vq_notify_off(device_t dev, int idx)
745 {
746 return (VIRTIO_PCI_QUEUE_NOTIFY);
747 }
748
749 static void
vtpci_legacy_set_vq(device_t dev,struct virtqueue * vq)750 vtpci_legacy_set_vq(device_t dev, struct virtqueue *vq)
751 {
752 struct vtpci_legacy_softc *sc;
753
754 sc = device_get_softc(dev);
755
756 vtpci_legacy_select_virtqueue(sc, virtqueue_index(vq));
757 vtpci_legacy_write_header_4(sc, VIRTIO_PCI_QUEUE_PFN,
758 virtqueue_paddr(vq) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT);
759 }
760
761 static void
vtpci_legacy_disable_vq(device_t dev,int idx)762 vtpci_legacy_disable_vq(device_t dev, int idx)
763 {
764 struct vtpci_legacy_softc *sc;
765
766 sc = device_get_softc(dev);
767
768 vtpci_legacy_select_virtqueue(sc, idx);
769 vtpci_legacy_write_header_4(sc, VIRTIO_PCI_QUEUE_PFN, 0);
770 }
771