aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/virtio/pci/virtio_pci.c
blob: 4849affae58c7a1bd2205799097368b6e3390478 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2017, Bryan Venteicher <bryanv@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice unmodified, this list of conditions, and the following
 *    disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/sbuf.h>
#include <sys/sysctl.h>
#include <sys/module.h>
#include <sys/malloc.h>

#include <machine/bus.h>
#include <machine/resource.h>
#include <sys/bus.h>
#include <sys/rman.h>

#include <dev/pci/pcivar.h>
#include <dev/pci/pcireg.h>

#include <dev/virtio/virtio.h>
#include <dev/virtio/virtqueue.h>
#include <dev/virtio/pci/virtio_pci.h>
#include <dev/virtio/pci/virtio_pci_var.h>

#include "virtio_pci_if.h"
#include "virtio_if.h"

static void	vtpci_describe_features(struct vtpci_common *, const char *,
		    uint64_t);
static int	vtpci_alloc_msix(struct vtpci_common *, int);
static int	vtpci_alloc_msi(struct vtpci_common *);
static int	vtpci_alloc_intr_msix_pervq(struct vtpci_common *);
static int	vtpci_alloc_intr_msix_shared(struct vtpci_common *);
static int	vtpci_alloc_intr_msi(struct vtpci_common *);
static int	vtpci_alloc_intr_intx(struct vtpci_common *);
static int	vtpci_alloc_interrupt(struct vtpci_common *, int, int,
		    struct vtpci_interrupt *);
static void	vtpci_free_interrupt(struct vtpci_common *,
		    struct vtpci_interrupt *);

static void	vtpci_free_interrupts(struct vtpci_common *);
static void	vtpci_free_virtqueues(struct vtpci_common *);
static void	vtpci_cleanup_setup_intr_attempt(struct vtpci_common *);
static int	vtpci_alloc_intr_resources(struct vtpci_common *);
static int	vtpci_setup_intx_interrupt(struct vtpci_common *,
		    enum intr_type);
static int	vtpci_setup_pervq_msix_interrupts(struct vtpci_common *,
		    enum intr_type);
static int	vtpci_set_host_msix_vectors(struct vtpci_common *);
static int	vtpci_setup_msix_interrupts(struct vtpci_common *,
		    enum intr_type);
static int	vtpci_setup_intrs(struct vtpci_common *, enum intr_type);
static int	vtpci_reinit_virtqueue(struct vtpci_common *, int);
static void	vtpci_intx_intr(void *);
static int	vtpci_vq_shared_intr_filter(void *);
static void	vtpci_vq_shared_intr(void *);
static int	vtpci_vq_intr_filter(void *);
static void	vtpci_vq_intr(void *);
static void	vtpci_config_intr(void *);

static void	vtpci_setup_sysctl(struct vtpci_common *);

#define vtpci_setup_msi_interrupt vtpci_setup_intx_interrupt

/*
 * This module contains two drivers:
 *   - virtio_pci_legacy for pre-V1 support
 *   - virtio_pci_modern for V1 support
 */
MODULE_VERSION(virtio_pci, 1);
MODULE_DEPEND(virtio_pci, pci, 1, 1, 1);
MODULE_DEPEND(virtio_pci, virtio, 1, 1, 1);

int vtpci_disable_msix = 0;
TUNABLE_INT("hw.virtio.pci.disable_msix", &vtpci_disable_msix);

static uint8_t
vtpci_read_isr(struct vtpci_common *cn)
{
	return (VIRTIO_PCI_READ_ISR(cn->vtpci_dev));
}

static uint16_t
vtpci_get_vq_size(struct vtpci_common *cn, int idx)
{
	return (VIRTIO_PCI_GET_VQ_SIZE(cn->vtpci_dev, idx));
}

static bus_size_t
vtpci_get_vq_notify_off(struct vtpci_common *cn, int idx)
{
	return (VIRTIO_PCI_GET_VQ_NOTIFY_OFF(cn->vtpci_dev, idx));
}

static void
vtpci_set_vq(struct vtpci_common *cn, struct virtqueue *vq)
{
	VIRTIO_PCI_SET_VQ(cn->vtpci_dev, vq);
}

static void
vtpci_disable_vq(struct vtpci_common *cn, int idx)
{
	VIRTIO_PCI_DISABLE_VQ(cn->vtpci_dev, idx);
}

static int
vtpci_register_cfg_msix(struct vtpci_common *cn, struct vtpci_interrupt *intr)
{
	return (VIRTIO_PCI_REGISTER_CFG_MSIX(cn->vtpci_dev, intr));
}

static int
vtpci_register_vq_msix(struct vtpci_common *cn, int idx,
    struct vtpci_interrupt *intr)
{
	return (VIRTIO_PCI_REGISTER_VQ_MSIX(cn->vtpci_dev, idx, intr));
}

void
vtpci_init(struct vtpci_common *cn, device_t dev, bool modern)
{

	cn->vtpci_dev = dev;

	pci_enable_busmaster(dev);

	if (modern)
		cn->vtpci_flags |= VTPCI_FLAG_MODERN;
	if (pci_find_cap(dev, PCIY_MSI, NULL) != 0)
		cn->vtpci_flags |= VTPCI_FLAG_NO_MSI;
	if (pci_find_cap(dev, PCIY_MSIX, NULL) != 0)
		cn->vtpci_flags |= VTPCI_FLAG_NO_MSIX;

	vtpci_setup_sysctl(cn);
}

int
vtpci_add_child(struct vtpci_common *cn)
{
	device_t dev, child;

	dev = cn->vtpci_dev;

	child = device_add_child(dev, NULL, -1);
	if (child == NULL) {
		device_printf(dev, "cannot create child device\n");
		return (ENOMEM);
	}

	cn->vtpci_child_dev = child;

	return (0);
}

int
vtpci_delete_child(struct vtpci_common *cn)
{
	device_t dev, child;
	int error;

	dev = cn->vtpci_dev;

	child = cn->vtpci_child_dev;
	if (child != NULL) {
		error = device_delete_child(dev, child);
		if (error)
			return (error);
		cn->vtpci_child_dev = NULL;
	}

	return (0);
}

void
vtpci_child_detached(struct vtpci_common *cn)
{

	vtpci_release_child_resources(cn);

	cn->vtpci_child_feat_desc = NULL;
	cn->vtpci_host_features = 0;
	cn->vtpci_features = 0;
}

int
vtpci_reinit(struct vtpci_common *cn)
{
	int idx, error;

	for (idx = 0; idx < cn->vtpci_nvqs; idx++) {
		error = vtpci_reinit_virtqueue(cn, idx);
		if (error)
			return (error);
	}

	if (vtpci_is_msix_enabled(cn)) {
		error = vtpci_set_host_msix_vectors(cn);
		if (error)
			return (error);
	}

	return (0);
}

static void
vtpci_describe_features(struct vtpci_common *cn, const char *msg,
    uint64_t features)
{
	device_t dev, child;

	dev = cn->vtpci_dev;
	child = cn->vtpci_child_dev;

	if (device_is_attached(child) || bootverbose == 0)
		return;

	virtio_describe(dev, msg, features, cn->vtpci_child_feat_desc);
}

uint64_t
vtpci_negotiate_features(struct vtpci_common *cn,
    uint64_t child_features, uint64_t host_features)
{
	uint64_t features;

	cn->vtpci_host_features = host_features;
	vtpci_describe_features(cn, "host", host_features);

	/*
	 * Limit negotiated features to what the driver, virtqueue, and
	 * host all support.
	 */
	features = host_features & child_features;
	features = virtio_filter_transport_features(features);

	cn->vtpci_features = features;
	vtpci_describe_features(cn, "negotiated", features);

	return (features);
}

int
vtpci_with_feature(struct vtpci_common *cn, uint64_t feature)
{
	return ((cn->vtpci_features & feature) != 0);
}

int
vtpci_read_ivar(struct vtpci_common *cn, int index, uintptr_t *result)
{
	device_t dev;
	int error;

	dev = cn->vtpci_dev;
	error = 0;

	switch (index) {
	case VIRTIO_IVAR_SUBDEVICE:
		*result = pci_get_subdevice(dev);
		break;
	case VIRTIO_IVAR_VENDOR:
		*result = pci_get_vendor(dev);
		break;
	case VIRTIO_IVAR_DEVICE:
		*result = pci_get_device(dev);
		break;
	case VIRTIO_IVAR_SUBVENDOR:
		*result = pci_get_subvendor(dev);
		break;
	case VIRTIO_IVAR_MODERN:
		*result = vtpci_is_modern(cn);
		break;
	default:
		error = ENOENT;
	}

	return (error);
}

int
vtpci_write_ivar(struct vtpci_common *cn, int index, uintptr_t value)
{
	int error;

	error = 0;

	switch (index) {
	case VIRTIO_IVAR_FEATURE_DESC:
		cn->vtpci_child_feat_desc = (void *) value;
		break;
	default:
		error = ENOENT;
	}

	return (error);
}

int
vtpci_alloc_virtqueues(struct vtpci_common *cn, int flags, int nvqs,
    struct vq_alloc_info *vq_info)
{
	device_t dev;
	int idx, align, error;

	dev = cn->vtpci_dev;

	/*
	 * This is VIRTIO_PCI_VRING_ALIGN from legacy VirtIO. In modern VirtIO,
	 * the tables do not have to be allocated contiguously, but we do so
	 * anyways.
	 */
	align = 4096;

	if (cn->vtpci_nvqs != 0)
		return (EALREADY);
	if (nvqs <= 0)
		return (EINVAL);

	cn->vtpci_vqs = malloc(nvqs * sizeof(struct vtpci_virtqueue),
	    M_DEVBUF, M_NOWAIT | M_ZERO);
	if (cn->vtpci_vqs == NULL)
		return (ENOMEM);

	for (idx = 0; idx < nvqs; idx++) {
		struct vtpci_virtqueue *vqx;
		struct vq_alloc_info *info;
		struct virtqueue *vq;
		bus_size_t notify_offset;
		uint16_t size;

		vqx = &cn->vtpci_vqs[idx];
		info = &vq_info[idx];

		size = vtpci_get_vq_size(cn, idx);
		notify_offset = vtpci_get_vq_notify_off(cn, idx);

		error = virtqueue_alloc(dev, idx, size, notify_offset, align,
		    ~(vm_paddr_t)0, info, &vq);
		if (error) {
			device_printf(dev,
			    "cannot allocate virtqueue %d: %d\n", idx, error);
			break;
		}

		vtpci_set_vq(cn, vq);

		vqx->vtv_vq = *info->vqai_vq = vq;
		vqx->vtv_no_intr = info->vqai_intr == NULL;

		cn->vtpci_nvqs++;
	}

	if (error)
		vtpci_free_virtqueues(cn);

	return (error);
}

static int
vtpci_alloc_msix(struct vtpci_common *cn, int nvectors)
{
	device_t dev;
	int nmsix, cnt, required;

	dev = cn->vtpci_dev;

	/* Allocate an additional vector for the config changes. */
	required = nvectors + 1;

	nmsix = pci_msix_count(dev);
	if (nmsix < required)
		return (1);

	cnt = required;
	if (pci_alloc_msix(dev, &cnt) == 0 && cnt >= required) {
		cn->vtpci_nmsix_resources = required;
		return (0);
	}

	pci_release_msi(dev);

	return (1);
}

static int
vtpci_alloc_msi(struct vtpci_common *cn)
{
	device_t dev;
	int nmsi, cnt, required;

	dev = cn->vtpci_dev;
	required = 1;

	nmsi = pci_msi_count(dev);
	if (nmsi < required)
		return (1);

	cnt = required;
	if (pci_alloc_msi(dev, &cnt) == 0 && cnt >= required)
		return (0);

	pci_release_msi(dev);

	return (1);
}

static int
vtpci_alloc_intr_msix_pervq(struct vtpci_common *cn)
{
	int i, nvectors, error;

	if (vtpci_disable_msix != 0 || cn->vtpci_flags & VTPCI_FLAG_NO_MSIX)
		return (ENOTSUP);

	for (nvectors = 0, i = 0; i < cn->vtpci_nvqs; i++) {
		if (cn->vtpci_vqs[i].vtv_no_intr == 0)
			nvectors++;
	}

	error = vtpci_alloc_msix(cn, nvectors);
	if (error)
		return (error);

	cn->vtpci_flags |= VTPCI_FLAG_MSIX;

	return (0);
}

static int
vtpci_alloc_intr_msix_shared(struct vtpci_common *cn)
{
	int error;

	if (vtpci_disable_msix != 0 || cn->vtpci_flags & VTPCI_FLAG_NO_MSIX)
		return (ENOTSUP);

	error = vtpci_alloc_msix(cn, 1);
	if (error)
		return (error);

	cn->vtpci_flags |= VTPCI_FLAG_MSIX | VTPCI_FLAG_SHARED_MSIX;

	return (0);
}

static int
vtpci_alloc_intr_msi(struct vtpci_common *cn)
{
	int error;

	/* Only BHyVe supports MSI. */
	if (cn->vtpci_flags & VTPCI_FLAG_NO_MSI)
		return (ENOTSUP);

	error = vtpci_alloc_msi(cn);
	if (error)
		return (error);

	cn->vtpci_flags |= VTPCI_FLAG_MSI;

	return (0);
}

static int
vtpci_alloc_intr_intx(struct vtpci_common *cn)
{

	cn->vtpci_flags |= VTPCI_FLAG_INTX;

	return (0);
}

static int
vtpci_alloc_interrupt(struct vtpci_common *cn, int rid, int flags,
    struct vtpci_interrupt *intr)
{
	struct resource *irq;

	irq = bus_alloc_resource_any(cn->vtpci_dev, SYS_RES_IRQ, &rid, flags);
	if (irq == NULL)
		return (ENXIO);

	intr->vti_irq = irq;
	intr->vti_rid = rid;

	return (0);
}

static void
vtpci_free_interrupt(struct vtpci_common *cn, struct vtpci_interrupt *intr)
{
	device_t dev;

	dev = cn->vtpci_dev;

	if (intr->vti_handler != NULL) {
		bus_teardown_intr(dev, intr->vti_irq, intr->vti_handler);
		intr->vti_handler = NULL;
	}

	if (intr->vti_irq != NULL) {
		bus_release_resource(dev, SYS_RES_IRQ, intr->vti_rid,
		    intr->vti_irq);
		intr->vti_irq = NULL;
		intr->vti_rid = -1;
	}
}

static void
vtpci_free_interrupts(struct vtpci_common *cn)
{
	struct vtpci_interrupt *intr;
	int i, nvq_intrs;

	vtpci_free_interrupt(cn, &cn->vtpci_device_interrupt);

	if (cn->vtpci_nmsix_resources != 0) {
		nvq_intrs = cn->vtpci_nmsix_resources - 1;
		cn->vtpci_nmsix_resources = 0;

		if ((intr = cn->vtpci_msix_vq_interrupts) != NULL) {
			for (i = 0; i < nvq_intrs; i++, intr++)
				vtpci_free_interrupt(cn, intr);

			free(cn->vtpci_msix_vq_interrupts, M_DEVBUF);
			cn->vtpci_msix_vq_interrupts = NULL;
		}
	}

	if (cn->vtpci_flags & (VTPCI_FLAG_MSI | VTPCI_FLAG_MSIX))
		pci_release_msi(cn->vtpci_dev);

	cn->vtpci_flags &= ~VTPCI_FLAG_ITYPE_MASK;
}

static void
vtpci_free_virtqueues(struct vtpci_common *cn)
{
	struct vtpci_virtqueue *vqx;
	int idx;

	for (idx = 0; idx < cn->vtpci_nvqs; idx++) {
		vtpci_disable_vq(cn, idx);

		vqx = &cn->vtpci_vqs[idx];
		virtqueue_free(vqx->vtv_vq);
		vqx->vtv_vq = NULL;
	}

	free(cn->vtpci_vqs, M_DEVBUF);
	cn->vtpci_vqs = NULL;
	cn->vtpci_nvqs = 0;
}

void
vtpci_release_child_resources(struct vtpci_common *cn)
{

	vtpci_free_interrupts(cn);
	vtpci_free_virtqueues(cn);
}

static void
vtpci_cleanup_setup_intr_attempt(struct vtpci_common *cn)
{
	int idx;

	if (cn->vtpci_flags & VTPCI_FLAG_MSIX) {
		vtpci_register_cfg_msix(cn, NULL);

		for (idx = 0; idx < cn->vtpci_nvqs; idx++)
			vtpci_register_vq_msix(cn, idx, NULL);
	}

	vtpci_free_interrupts(cn);
}

static int
vtpci_alloc_intr_resources(struct vtpci_common *cn)
{
	struct vtpci_interrupt *intr;
	int i, rid, flags, nvq_intrs, error;

	flags = RF_ACTIVE;

	if (cn->vtpci_flags & VTPCI_FLAG_INTX) {
		rid = 0;
		flags |= RF_SHAREABLE;
	} else
		rid = 1;

	/*
	 * When using INTX or MSI interrupts, this resource handles all
	 * interrupts. When using MSIX, this resource handles just the
	 * configuration changed interrupt.
	 */
	intr = &cn->vtpci_device_interrupt;

	error = vtpci_alloc_interrupt(cn, rid, flags, intr);
	if (error || cn->vtpci_flags & (VTPCI_FLAG_INTX | VTPCI_FLAG_MSI))
		return (error);

	/*
	 * Now allocate the interrupts for the virtqueues. This may be one
	 * for all the virtqueues, or one for each virtqueue. Subtract one
	 * below for because of the configuration changed interrupt.
	 */
	nvq_intrs = cn->vtpci_nmsix_resources - 1;

	cn->vtpci_msix_vq_interrupts = malloc(nvq_intrs *
	    sizeof(struct vtpci_interrupt), M_DEVBUF, M_NOWAIT | M_ZERO);
	if (cn->vtpci_msix_vq_interrupts == NULL)
		return (ENOMEM);

	intr = cn->vtpci_msix_vq_interrupts;

	for (i = 0, rid++; i < nvq_intrs; i++, rid++, intr++) {
		error = vtpci_alloc_interrupt(cn, rid, flags, intr);
		if (error)
			return (error);
	}

	return (0);
}

static int
vtpci_setup_intx_interrupt(struct vtpci_common *cn, enum intr_type type)
{
	struct vtpci_interrupt *intr;
	int error;

	intr = &cn->vtpci_device_interrupt;

	error = bus_setup_intr(cn->vtpci_dev, intr->vti_irq, type, NULL,
	    vtpci_intx_intr, cn, &intr->vti_handler);

	return (error);
}

static int
vtpci_setup_pervq_msix_interrupts(struct vtpci_common *cn, enum intr_type type)
{
	struct vtpci_virtqueue *vqx;
	struct vtpci_interrupt *intr;
	int i, error;

	intr = cn->vtpci_msix_vq_interrupts;

	for (i = 0; i < cn->vtpci_nvqs; i++) {
		vqx = &cn->vtpci_vqs[i];

		if (vqx->vtv_no_intr)
			continue;

		error = bus_setup_intr(cn->vtpci_dev, intr->vti_irq, type,
		    vtpci_vq_intr_filter, vtpci_vq_intr, vqx->vtv_vq,
		    &intr->vti_handler);
		if (error)
			return (error);

		intr++;
	}

	return (0);
}

static int
vtpci_set_host_msix_vectors(struct vtpci_common *cn)
{
	struct vtpci_interrupt *intr, *tintr;
	int idx, error;

	intr = &cn->vtpci_device_interrupt;
	error = vtpci_register_cfg_msix(cn, intr);
	if (error)
		return (error);

	intr = cn->vtpci_msix_vq_interrupts;
	for (idx = 0; idx < cn->vtpci_nvqs; idx++) {
		if (cn->vtpci_vqs[idx].vtv_no_intr)
			tintr = NULL;
		else
			tintr = intr;

		error = vtpci_register_vq_msix(cn, idx, tintr);
		if (error)
			break;

		/*
		 * For shared MSIX, all the virtqueues share the first
		 * interrupt.
		 */
		if (!cn->vtpci_vqs[idx].vtv_no_intr &&
		    (cn->vtpci_flags & VTPCI_FLAG_SHARED_MSIX) == 0)
			intr++;
	}

	return (error);
}

static int
vtpci_setup_msix_interrupts(struct vtpci_common *cn, enum intr_type type)
{
	struct vtpci_interrupt *intr;
	int error;

	intr = &cn->vtpci_device_interrupt;

	error = bus_setup_intr(cn->vtpci_dev, intr->vti_irq, type, NULL,
	    vtpci_config_intr, cn, &intr->vti_handler);
	if (error)
		return (error);

	if (cn->vtpci_flags & VTPCI_FLAG_SHARED_MSIX) {
		intr = &cn->vtpci_msix_vq_interrupts[0];

		error = bus_setup_intr(cn->vtpci_dev, intr->vti_irq, type,
		    vtpci_vq_shared_intr_filter, vtpci_vq_shared_intr, cn,
		    &intr->vti_handler);
	} else
		error = vtpci_setup_pervq_msix_interrupts(cn, type);

	return (error ? error : vtpci_set_host_msix_vectors(cn));
}

static int
vtpci_setup_intrs(struct vtpci_common *cn, enum intr_type type)
{
	int error;

	type |= INTR_MPSAFE;
	KASSERT(cn->vtpci_flags & VTPCI_FLAG_ITYPE_MASK,
	    ("%s: no interrupt type selected %#x", __func__, cn->vtpci_flags));

	error = vtpci_alloc_intr_resources(cn);
	if (error)
		return (error);

	if (cn->vtpci_flags & VTPCI_FLAG_INTX)
		error = vtpci_setup_intx_interrupt(cn, type);
	else if (cn->vtpci_flags & VTPCI_FLAG_MSI)
		error = vtpci_setup_msi_interrupt(cn, type);
	else
		error = vtpci_setup_msix_interrupts(cn, type);

	return (error);
}

int
vtpci_setup_interrupts(struct vtpci_common *cn, enum intr_type type)
{
	device_t dev;
	int attempt, error;

	dev = cn->vtpci_dev;

	for (attempt = 0; attempt < 5; attempt++) {
		/*
		 * Start with the most desirable interrupt configuration and
		 * fallback towards less desirable ones.
		 */
		switch (attempt) {
		case 0:
			error = vtpci_alloc_intr_msix_pervq(cn);
			break;
		case 1:
			error = vtpci_alloc_intr_msix_shared(cn);
			break;
		case 2:
			error = vtpci_alloc_intr_msi(cn);
			break;
		case 3:
			error = vtpci_alloc_intr_intx(cn);
			break;
		default:
			device_printf(dev,
			    "exhausted all interrupt allocation attempts\n");
			return (ENXIO);
		}

		if (error == 0 && vtpci_setup_intrs(cn, type) == 0)
			break;

		vtpci_cleanup_setup_intr_attempt(cn);
	}

	if (bootverbose) {
		if (cn->vtpci_flags & VTPCI_FLAG_INTX)
			device_printf(dev, "using legacy interrupt\n");
		else if (cn->vtpci_flags & VTPCI_FLAG_MSI)
			device_printf(dev, "using MSI interrupt\n");
		else if (cn->vtpci_flags & VTPCI_FLAG_SHARED_MSIX)
			device_printf(dev, "using shared MSIX interrupts\n");
		else
			device_printf(dev, "using per VQ MSIX interrupts\n");
	}

	return (0);
}

static int
vtpci_reinit_virtqueue(struct vtpci_common *cn, int idx)
{
	struct vtpci_virtqueue *vqx;
	struct virtqueue *vq;
	int error;

	vqx = &cn->vtpci_vqs[idx];
	vq = vqx->vtv_vq;

	KASSERT(vq != NULL, ("%s: vq %d not allocated", __func__, idx));

	error = virtqueue_reinit(vq, vtpci_get_vq_size(cn, idx));
	if (error == 0)
		vtpci_set_vq(cn, vq);

	return (error);
}

static void
vtpci_intx_intr(void *xcn)
{
	struct vtpci_common *cn;
	struct vtpci_virtqueue *vqx;
	int i;
	uint8_t isr;

	cn = xcn;
	isr = vtpci_read_isr(cn);

	if (isr & VIRTIO_PCI_ISR_CONFIG)
		vtpci_config_intr(cn);

	if (isr & VIRTIO_PCI_ISR_INTR) {
		vqx = &cn->vtpci_vqs[0];
		for (i = 0; i < cn->vtpci_nvqs; i++, vqx++) {
			if (vqx->vtv_no_intr == 0)
				virtqueue_intr(vqx->vtv_vq);
		}
	}
}

static int
vtpci_vq_shared_intr_filter(void *xcn)
{
	struct vtpci_common *cn;
	struct vtpci_virtqueue *vqx;
	int i, rc;

	cn = xcn;
	vqx = &cn->vtpci_vqs[0];
	rc = 0;

	for (i = 0; i < cn->vtpci_nvqs; i++, vqx++) {
		if (vqx->vtv_no_intr == 0)
			rc |= virtqueue_intr_filter(vqx->vtv_vq);
	}

	return (rc ? FILTER_SCHEDULE_THREAD : FILTER_STRAY);
}

static void
vtpci_vq_shared_intr(void *xcn)
{
	struct vtpci_common *cn;
	struct vtpci_virtqueue *vqx;
	int i;

	cn = xcn;
	vqx = &cn->vtpci_vqs[0];

	for (i = 0; i < cn->vtpci_nvqs; i++, vqx++) {
		if (vqx->vtv_no_intr == 0)
			virtqueue_intr(vqx->vtv_vq);
	}
}

static int
vtpci_vq_intr_filter(void *xvq)
{
	struct virtqueue *vq;
	int rc;

	vq = xvq;
	rc = virtqueue_intr_filter(vq);

	return (rc ? FILTER_SCHEDULE_THREAD : FILTER_STRAY);
}

static void
vtpci_vq_intr(void *xvq)
{
	struct virtqueue *vq;

	vq = xvq;
	virtqueue_intr(vq);
}

static void
vtpci_config_intr(void *xcn)
{
	struct vtpci_common *cn;
	device_t child;

	cn = xcn;
	child = cn->vtpci_child_dev;

	if (child != NULL)
		VIRTIO_CONFIG_CHANGE(child);
}

static int
vtpci_feature_sysctl(struct sysctl_req *req, struct vtpci_common *cn,
    uint64_t features)
{
	struct sbuf *sb;
	int error;

	sb = sbuf_new_for_sysctl(NULL, NULL, 256, req);
	if (sb == NULL)
		return (ENOMEM);

	error = virtio_describe_sbuf(sb, features, cn->vtpci_child_feat_desc);
	sbuf_delete(sb);

	return (error);
}

static int
vtpci_host_features_sysctl(SYSCTL_HANDLER_ARGS)
{
	struct vtpci_common *cn;

	cn = arg1;

	return (vtpci_feature_sysctl(req, cn, cn->vtpci_host_features));
}

static int
vtpci_negotiated_features_sysctl(SYSCTL_HANDLER_ARGS)
{
	struct vtpci_common *cn;

	cn = arg1;

	return (vtpci_feature_sysctl(req, cn, cn->vtpci_features));
}

static void
vtpci_setup_sysctl(struct vtpci_common *cn)
{
	device_t dev;
	struct sysctl_ctx_list *ctx;
	struct sysctl_oid *tree;
	struct sysctl_oid_list *child;

	dev = cn->vtpci_dev;
	ctx = device_get_sysctl_ctx(dev);
	tree = device_get_sysctl_tree(dev);
	child = SYSCTL_CHILDREN(tree);

	SYSCTL_ADD_INT(ctx, child, OID_AUTO, "nvqs",
	    CTLFLAG_RD, &cn->vtpci_nvqs, 0, "Number of virtqueues");

	SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "host_features",
	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, cn, 0,
	    vtpci_host_features_sysctl, "A", "Features supported by the host");
	SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "negotiated_features",
	    CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, cn, 0,
	    vtpci_negotiated_features_sysctl, "A", "Features negotiated");
}