xref: /freebsd/sys/amd64/vmm/io/ppt.c (revision 38a52bd3b5cac3da6f7f6eef3dd050e6aa08ebb3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2011 NetApp, Inc.
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, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/bus.h>
40 #include <sys/pciio.h>
41 #include <sys/rman.h>
42 #include <sys/smp.h>
43 #include <sys/sysctl.h>
44 
45 #include <dev/pci/pcivar.h>
46 #include <dev/pci/pcireg.h>
47 
48 #include <machine/resource.h>
49 
50 #include <machine/vmm.h>
51 #include <machine/vmm_dev.h>
52 
53 #include "vmm_lapic.h"
54 #include "vmm_ktr.h"
55 
56 #include "iommu.h"
57 #include "ppt.h"
58 
59 /* XXX locking */
60 
61 #define	MAX_MSIMSGS	32
62 
63 /*
64  * If the MSI-X table is located in the middle of a BAR then that MMIO
65  * region gets split into two segments - one segment above the MSI-X table
66  * and the other segment below the MSI-X table - with a hole in place of
67  * the MSI-X table so accesses to it can be trapped and emulated.
68  *
69  * So, allocate a MMIO segment for each BAR register + 1 additional segment.
70  */
71 #define	MAX_MMIOSEGS	((PCIR_MAX_BAR_0 + 1) + 1)
72 
73 MALLOC_DEFINE(M_PPTMSIX, "pptmsix", "Passthru MSI-X resources");
74 
75 struct pptintr_arg {				/* pptintr(pptintr_arg) */
76 	struct pptdev	*pptdev;
77 	uint64_t	addr;
78 	uint64_t	msg_data;
79 };
80 
81 struct pptseg {
82 	vm_paddr_t	gpa;
83 	size_t		len;
84 	int		wired;
85 };
86 
87 struct pptdev {
88 	device_t	dev;
89 	struct vm	*vm;			/* owner of this device */
90 	TAILQ_ENTRY(pptdev)	next;
91 	struct pptseg mmio[MAX_MMIOSEGS];
92 	struct {
93 		int	num_msgs;		/* guest state */
94 
95 		int	startrid;		/* host state */
96 		struct resource *res[MAX_MSIMSGS];
97 		void	*cookie[MAX_MSIMSGS];
98 		struct pptintr_arg arg[MAX_MSIMSGS];
99 	} msi;
100 
101 	struct {
102 		int num_msgs;
103 		int startrid;
104 		int msix_table_rid;
105 		int msix_pba_rid;
106 		struct resource *msix_table_res;
107 		struct resource *msix_pba_res;
108 		struct resource **res;
109 		void **cookie;
110 		struct pptintr_arg *arg;
111 	} msix;
112 };
113 
114 SYSCTL_DECL(_hw_vmm);
115 SYSCTL_NODE(_hw_vmm, OID_AUTO, ppt, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
116     "bhyve passthru devices");
117 
118 static int num_pptdevs;
119 SYSCTL_INT(_hw_vmm_ppt, OID_AUTO, devices, CTLFLAG_RD, &num_pptdevs, 0,
120     "number of pci passthru devices");
121 
122 static TAILQ_HEAD(, pptdev) pptdev_list = TAILQ_HEAD_INITIALIZER(pptdev_list);
123 
124 static int
125 ppt_probe(device_t dev)
126 {
127 	int bus, slot, func;
128 	struct pci_devinfo *dinfo;
129 
130 	dinfo = (struct pci_devinfo *)device_get_ivars(dev);
131 
132 	bus = pci_get_bus(dev);
133 	slot = pci_get_slot(dev);
134 	func = pci_get_function(dev);
135 
136 	/*
137 	 * To qualify as a pci passthrough device a device must:
138 	 * - be allowed by administrator to be used in this role
139 	 * - be an endpoint device
140 	 */
141 	if ((dinfo->cfg.hdrtype & PCIM_HDRTYPE) != PCIM_HDRTYPE_NORMAL)
142 		return (ENXIO);
143 	else if (vmm_is_pptdev(bus, slot, func))
144 		return (0);
145 	else
146 		/*
147 		 * Returning BUS_PROBE_NOWILDCARD here matches devices that the
148 		 * SR-IOV infrastructure specified as "ppt" passthrough devices.
149 		 * All normal devices that did not have "ppt" specified as their
150 		 * driver will not be matched by this.
151 		 */
152 		return (BUS_PROBE_NOWILDCARD);
153 }
154 
155 static int
156 ppt_attach(device_t dev)
157 {
158 	struct pptdev *ppt;
159 
160 	ppt = device_get_softc(dev);
161 
162 	iommu_remove_device(iommu_host_domain(), pci_get_rid(dev));
163 	num_pptdevs++;
164 	TAILQ_INSERT_TAIL(&pptdev_list, ppt, next);
165 	ppt->dev = dev;
166 
167 	if (bootverbose)
168 		device_printf(dev, "attached\n");
169 
170 	return (0);
171 }
172 
173 static int
174 ppt_detach(device_t dev)
175 {
176 	struct pptdev *ppt;
177 
178 	ppt = device_get_softc(dev);
179 
180 	if (ppt->vm != NULL)
181 		return (EBUSY);
182 	num_pptdevs--;
183 	TAILQ_REMOVE(&pptdev_list, ppt, next);
184 	pci_disable_busmaster(dev);
185 	iommu_add_device(iommu_host_domain(), pci_get_rid(dev));
186 
187 	return (0);
188 }
189 
190 static device_method_t ppt_methods[] = {
191 	/* Device interface */
192 	DEVMETHOD(device_probe,		ppt_probe),
193 	DEVMETHOD(device_attach,	ppt_attach),
194 	DEVMETHOD(device_detach,	ppt_detach),
195 	{0, 0}
196 };
197 
198 DEFINE_CLASS_0(ppt, ppt_driver, ppt_methods, sizeof(struct pptdev));
199 DRIVER_MODULE(ppt, pci, ppt_driver, NULL, NULL);
200 
201 static int
202 ppt_find(struct vm *vm, int bus, int slot, int func, struct pptdev **pptp)
203 {
204 	device_t dev;
205 	struct pptdev *ppt;
206 	int b, s, f;
207 
208 	TAILQ_FOREACH(ppt, &pptdev_list, next) {
209 		dev = ppt->dev;
210 		b = pci_get_bus(dev);
211 		s = pci_get_slot(dev);
212 		f = pci_get_function(dev);
213 		if (bus == b && slot == s && func == f)
214 			break;
215 	}
216 
217 	if (ppt == NULL)
218 		return (ENOENT);
219 	if (ppt->vm != vm)		/* Make sure we own this device */
220 		return (EBUSY);
221 	*pptp = ppt;
222 	return (0);
223 }
224 
225 static void
226 ppt_unmap_all_mmio(struct vm *vm, struct pptdev *ppt)
227 {
228 	int i;
229 	struct pptseg *seg;
230 
231 	for (i = 0; i < MAX_MMIOSEGS; i++) {
232 		seg = &ppt->mmio[i];
233 		if (seg->len == 0)
234 			continue;
235 		(void)vm_unmap_mmio(vm, seg->gpa, seg->len);
236 		bzero(seg, sizeof(struct pptseg));
237 	}
238 }
239 
240 static void
241 ppt_teardown_msi(struct pptdev *ppt)
242 {
243 	int i, rid;
244 	void *cookie;
245 	struct resource *res;
246 
247 	if (ppt->msi.num_msgs == 0)
248 		return;
249 
250 	for (i = 0; i < ppt->msi.num_msgs; i++) {
251 		rid = ppt->msi.startrid + i;
252 		res = ppt->msi.res[i];
253 		cookie = ppt->msi.cookie[i];
254 
255 		if (cookie != NULL)
256 			bus_teardown_intr(ppt->dev, res, cookie);
257 
258 		if (res != NULL)
259 			bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, res);
260 
261 		ppt->msi.res[i] = NULL;
262 		ppt->msi.cookie[i] = NULL;
263 	}
264 
265 	if (ppt->msi.startrid == 1)
266 		pci_release_msi(ppt->dev);
267 
268 	ppt->msi.num_msgs = 0;
269 }
270 
271 static void
272 ppt_teardown_msix_intr(struct pptdev *ppt, int idx)
273 {
274 	int rid;
275 	struct resource *res;
276 	void *cookie;
277 
278 	rid = ppt->msix.startrid + idx;
279 	res = ppt->msix.res[idx];
280 	cookie = ppt->msix.cookie[idx];
281 
282 	if (cookie != NULL)
283 		bus_teardown_intr(ppt->dev, res, cookie);
284 
285 	if (res != NULL)
286 		bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, res);
287 
288 	ppt->msix.res[idx] = NULL;
289 	ppt->msix.cookie[idx] = NULL;
290 }
291 
292 static void
293 ppt_teardown_msix(struct pptdev *ppt)
294 {
295 	int i;
296 
297 	if (ppt->msix.num_msgs == 0)
298 		return;
299 
300 	for (i = 0; i < ppt->msix.num_msgs; i++)
301 		ppt_teardown_msix_intr(ppt, i);
302 
303 	free(ppt->msix.res, M_PPTMSIX);
304 	free(ppt->msix.cookie, M_PPTMSIX);
305 	free(ppt->msix.arg, M_PPTMSIX);
306 
307 	pci_release_msi(ppt->dev);
308 
309 	if (ppt->msix.msix_table_res) {
310 		bus_release_resource(ppt->dev, SYS_RES_MEMORY,
311 				     ppt->msix.msix_table_rid,
312 				     ppt->msix.msix_table_res);
313 		ppt->msix.msix_table_res = NULL;
314 		ppt->msix.msix_table_rid = 0;
315 	}
316 	if (ppt->msix.msix_pba_res) {
317 		bus_release_resource(ppt->dev, SYS_RES_MEMORY,
318 				     ppt->msix.msix_pba_rid,
319 				     ppt->msix.msix_pba_res);
320 		ppt->msix.msix_pba_res = NULL;
321 		ppt->msix.msix_pba_rid = 0;
322 	}
323 
324 	ppt->msix.num_msgs = 0;
325 }
326 
327 int
328 ppt_avail_devices(void)
329 {
330 
331 	return (num_pptdevs);
332 }
333 
334 int
335 ppt_assigned_devices(struct vm *vm)
336 {
337 	struct pptdev *ppt;
338 	int num;
339 
340 	num = 0;
341 	TAILQ_FOREACH(ppt, &pptdev_list, next) {
342 		if (ppt->vm == vm)
343 			num++;
344 	}
345 	return (num);
346 }
347 
348 bool
349 ppt_is_mmio(struct vm *vm, vm_paddr_t gpa)
350 {
351 	int i;
352 	struct pptdev *ppt;
353 	struct pptseg *seg;
354 
355 	TAILQ_FOREACH(ppt, &pptdev_list, next) {
356 		if (ppt->vm != vm)
357 			continue;
358 
359 		for (i = 0; i < MAX_MMIOSEGS; i++) {
360 			seg = &ppt->mmio[i];
361 			if (seg->len == 0)
362 				continue;
363 			if (gpa >= seg->gpa && gpa < seg->gpa + seg->len)
364 				return (true);
365 		}
366 	}
367 
368 	return (false);
369 }
370 
371 static void
372 ppt_pci_reset(device_t dev)
373 {
374 
375 	if (pcie_flr(dev,
376 	     max(pcie_get_max_completion_timeout(dev) / 1000, 10), true))
377 		return;
378 
379 	pci_power_reset(dev);
380 }
381 
382 int
383 ppt_assign_device(struct vm *vm, int bus, int slot, int func)
384 {
385 	struct pptdev *ppt;
386 	int error;
387 
388 	/* Passing NULL requires the device to be unowned. */
389 	error = ppt_find(NULL, bus, slot, func, &ppt);
390 	if (error)
391 		return (error);
392 
393 	pci_save_state(ppt->dev);
394 	ppt_pci_reset(ppt->dev);
395 	pci_restore_state(ppt->dev);
396 	ppt->vm = vm;
397 	iommu_add_device(vm_iommu_domain(vm), pci_get_rid(ppt->dev));
398 	return (0);
399 }
400 
401 int
402 ppt_unassign_device(struct vm *vm, int bus, int slot, int func)
403 {
404 	struct pptdev *ppt;
405 	int error;
406 
407 	error = ppt_find(vm, bus, slot, func, &ppt);
408 	if (error)
409 		return (error);
410 
411 	pci_save_state(ppt->dev);
412 	ppt_pci_reset(ppt->dev);
413 	pci_restore_state(ppt->dev);
414 	ppt_unmap_all_mmio(vm, ppt);
415 	ppt_teardown_msi(ppt);
416 	ppt_teardown_msix(ppt);
417 	iommu_remove_device(vm_iommu_domain(vm), pci_get_rid(ppt->dev));
418 	ppt->vm = NULL;
419 	return (0);
420 }
421 
422 int
423 ppt_unassign_all(struct vm *vm)
424 {
425 	struct pptdev *ppt;
426 	int bus, slot, func;
427 	device_t dev;
428 
429 	TAILQ_FOREACH(ppt, &pptdev_list, next) {
430 		if (ppt->vm == vm) {
431 			dev = ppt->dev;
432 			bus = pci_get_bus(dev);
433 			slot = pci_get_slot(dev);
434 			func = pci_get_function(dev);
435 			vm_unassign_pptdev(vm, bus, slot, func);
436 		}
437 	}
438 
439 	return (0);
440 }
441 
442 static bool
443 ppt_valid_bar_mapping(struct pptdev *ppt, vm_paddr_t hpa, size_t len)
444 {
445 	struct pci_map *pm;
446 	pci_addr_t base, size;
447 
448 	for (pm = pci_first_bar(ppt->dev); pm != NULL; pm = pci_next_bar(pm)) {
449 		if (!PCI_BAR_MEM(pm->pm_value))
450 			continue;
451 		base = pm->pm_value & PCIM_BAR_MEM_BASE;
452 		size = (pci_addr_t)1 << pm->pm_size;
453 		if (hpa >= base && hpa + len <= base + size)
454 			return (true);
455 	}
456 	return (false);
457 }
458 
459 int
460 ppt_map_mmio(struct vm *vm, int bus, int slot, int func,
461 	     vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
462 {
463 	int i, error;
464 	struct pptseg *seg;
465 	struct pptdev *ppt;
466 
467 	if (len % PAGE_SIZE != 0 || len == 0 || gpa % PAGE_SIZE != 0 ||
468 	    hpa % PAGE_SIZE != 0 || gpa + len < gpa || hpa + len < hpa)
469 		return (EINVAL);
470 
471 	error = ppt_find(vm, bus, slot, func, &ppt);
472 	if (error)
473 		return (error);
474 
475 	if (!ppt_valid_bar_mapping(ppt, hpa, len))
476 		return (EINVAL);
477 
478 	for (i = 0; i < MAX_MMIOSEGS; i++) {
479 		seg = &ppt->mmio[i];
480 		if (seg->len == 0) {
481 			error = vm_map_mmio(vm, gpa, len, hpa);
482 			if (error == 0) {
483 				seg->gpa = gpa;
484 				seg->len = len;
485 			}
486 			return (error);
487 		}
488 	}
489 	return (ENOSPC);
490 }
491 
492 int
493 ppt_unmap_mmio(struct vm *vm, int bus, int slot, int func,
494 	       vm_paddr_t gpa, size_t len)
495 {
496 	int i, error;
497 	struct pptseg *seg;
498 	struct pptdev *ppt;
499 
500 	error = ppt_find(vm, bus, slot, func, &ppt);
501 	if (error)
502 		return (error);
503 
504 	for (i = 0; i < MAX_MMIOSEGS; i++) {
505 		seg = &ppt->mmio[i];
506 		if (seg->gpa == gpa && seg->len == len) {
507 			error = vm_unmap_mmio(vm, seg->gpa, seg->len);
508 			if (error == 0) {
509 				seg->gpa = 0;
510 				seg->len = 0;
511 			}
512 			return (error);
513 		}
514 	}
515 	return (ENOENT);
516 }
517 
518 static int
519 pptintr(void *arg)
520 {
521 	struct pptdev *ppt;
522 	struct pptintr_arg *pptarg;
523 
524 	pptarg = arg;
525 	ppt = pptarg->pptdev;
526 
527 	if (ppt->vm != NULL)
528 		lapic_intr_msi(ppt->vm, pptarg->addr, pptarg->msg_data);
529 	else {
530 		/*
531 		 * XXX
532 		 * This is not expected to happen - panic?
533 		 */
534 	}
535 
536 	/*
537 	 * For legacy interrupts give other filters a chance in case
538 	 * the interrupt was not generated by the passthrough device.
539 	 */
540 	if (ppt->msi.startrid == 0)
541 		return (FILTER_STRAY);
542 	else
543 		return (FILTER_HANDLED);
544 }
545 
546 int
547 ppt_setup_msi(struct vm *vm, int vcpu, int bus, int slot, int func,
548 	      uint64_t addr, uint64_t msg, int numvec)
549 {
550 	int i, rid, flags;
551 	int msi_count, startrid, error, tmp;
552 	struct pptdev *ppt;
553 
554 	if (numvec < 0 || numvec > MAX_MSIMSGS)
555 		return (EINVAL);
556 
557 	error = ppt_find(vm, bus, slot, func, &ppt);
558 	if (error)
559 		return (error);
560 
561 	/* Reject attempts to enable MSI while MSI-X is active. */
562 	if (ppt->msix.num_msgs != 0 && numvec != 0)
563 		return (EBUSY);
564 
565 	/* Free any allocated resources */
566 	ppt_teardown_msi(ppt);
567 
568 	if (numvec == 0)		/* nothing more to do */
569 		return (0);
570 
571 	flags = RF_ACTIVE;
572 	msi_count = pci_msi_count(ppt->dev);
573 	if (msi_count == 0) {
574 		startrid = 0;		/* legacy interrupt */
575 		msi_count = 1;
576 		flags |= RF_SHAREABLE;
577 	} else
578 		startrid = 1;		/* MSI */
579 
580 	/*
581 	 * The device must be capable of supporting the number of vectors
582 	 * the guest wants to allocate.
583 	 */
584 	if (numvec > msi_count)
585 		return (EINVAL);
586 
587 	/*
588 	 * Make sure that we can allocate all the MSI vectors that are needed
589 	 * by the guest.
590 	 */
591 	if (startrid == 1) {
592 		tmp = numvec;
593 		error = pci_alloc_msi(ppt->dev, &tmp);
594 		if (error)
595 			return (error);
596 		else if (tmp != numvec) {
597 			pci_release_msi(ppt->dev);
598 			return (ENOSPC);
599 		} else {
600 			/* success */
601 		}
602 	}
603 
604 	ppt->msi.startrid = startrid;
605 
606 	/*
607 	 * Allocate the irq resource and attach it to the interrupt handler.
608 	 */
609 	for (i = 0; i < numvec; i++) {
610 		ppt->msi.num_msgs = i + 1;
611 		ppt->msi.cookie[i] = NULL;
612 
613 		rid = startrid + i;
614 		ppt->msi.res[i] = bus_alloc_resource_any(ppt->dev, SYS_RES_IRQ,
615 							 &rid, flags);
616 		if (ppt->msi.res[i] == NULL)
617 			break;
618 
619 		ppt->msi.arg[i].pptdev = ppt;
620 		ppt->msi.arg[i].addr = addr;
621 		ppt->msi.arg[i].msg_data = msg + i;
622 
623 		error = bus_setup_intr(ppt->dev, ppt->msi.res[i],
624 				       INTR_TYPE_NET | INTR_MPSAFE,
625 				       pptintr, NULL, &ppt->msi.arg[i],
626 				       &ppt->msi.cookie[i]);
627 		if (error != 0)
628 			break;
629 	}
630 
631 	if (i < numvec) {
632 		ppt_teardown_msi(ppt);
633 		return (ENXIO);
634 	}
635 
636 	return (0);
637 }
638 
639 int
640 ppt_setup_msix(struct vm *vm, int vcpu, int bus, int slot, int func,
641 	       int idx, uint64_t addr, uint64_t msg, uint32_t vector_control)
642 {
643 	struct pptdev *ppt;
644 	struct pci_devinfo *dinfo;
645 	int numvec, alloced, rid, error;
646 	size_t res_size, cookie_size, arg_size;
647 
648 	error = ppt_find(vm, bus, slot, func, &ppt);
649 	if (error)
650 		return (error);
651 
652 	/* Reject attempts to enable MSI-X while MSI is active. */
653 	if (ppt->msi.num_msgs != 0)
654 		return (EBUSY);
655 
656 	dinfo = device_get_ivars(ppt->dev);
657 	if (!dinfo)
658 		return (ENXIO);
659 
660 	/*
661 	 * First-time configuration:
662 	 * 	Allocate the MSI-X table
663 	 *	Allocate the IRQ resources
664 	 *	Set up some variables in ppt->msix
665 	 */
666 	if (ppt->msix.num_msgs == 0) {
667 		numvec = pci_msix_count(ppt->dev);
668 		if (numvec <= 0)
669 			return (EINVAL);
670 
671 		ppt->msix.startrid = 1;
672 		ppt->msix.num_msgs = numvec;
673 
674 		res_size = numvec * sizeof(ppt->msix.res[0]);
675 		cookie_size = numvec * sizeof(ppt->msix.cookie[0]);
676 		arg_size = numvec * sizeof(ppt->msix.arg[0]);
677 
678 		ppt->msix.res = malloc(res_size, M_PPTMSIX, M_WAITOK | M_ZERO);
679 		ppt->msix.cookie = malloc(cookie_size, M_PPTMSIX,
680 					  M_WAITOK | M_ZERO);
681 		ppt->msix.arg = malloc(arg_size, M_PPTMSIX, M_WAITOK | M_ZERO);
682 
683 		rid = dinfo->cfg.msix.msix_table_bar;
684 		ppt->msix.msix_table_res = bus_alloc_resource_any(ppt->dev,
685 					       SYS_RES_MEMORY, &rid, RF_ACTIVE);
686 
687 		if (ppt->msix.msix_table_res == NULL) {
688 			ppt_teardown_msix(ppt);
689 			return (ENOSPC);
690 		}
691 		ppt->msix.msix_table_rid = rid;
692 
693 		if (dinfo->cfg.msix.msix_table_bar !=
694 		    dinfo->cfg.msix.msix_pba_bar) {
695 			rid = dinfo->cfg.msix.msix_pba_bar;
696 			ppt->msix.msix_pba_res = bus_alloc_resource_any(
697 			    ppt->dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
698 
699 			if (ppt->msix.msix_pba_res == NULL) {
700 				ppt_teardown_msix(ppt);
701 				return (ENOSPC);
702 			}
703 			ppt->msix.msix_pba_rid = rid;
704 		}
705 
706 		alloced = numvec;
707 		error = pci_alloc_msix(ppt->dev, &alloced);
708 		if (error || alloced != numvec) {
709 			ppt_teardown_msix(ppt);
710 			return (error == 0 ? ENOSPC: error);
711 		}
712 	}
713 
714 	if ((vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
715 		/* Tear down the IRQ if it's already set up */
716 		ppt_teardown_msix_intr(ppt, idx);
717 
718 		/* Allocate the IRQ resource */
719 		ppt->msix.cookie[idx] = NULL;
720 		rid = ppt->msix.startrid + idx;
721 		ppt->msix.res[idx] = bus_alloc_resource_any(ppt->dev, SYS_RES_IRQ,
722 							    &rid, RF_ACTIVE);
723 		if (ppt->msix.res[idx] == NULL)
724 			return (ENXIO);
725 
726 		ppt->msix.arg[idx].pptdev = ppt;
727 		ppt->msix.arg[idx].addr = addr;
728 		ppt->msix.arg[idx].msg_data = msg;
729 
730 		/* Setup the MSI-X interrupt */
731 		error = bus_setup_intr(ppt->dev, ppt->msix.res[idx],
732 				       INTR_TYPE_NET | INTR_MPSAFE,
733 				       pptintr, NULL, &ppt->msix.arg[idx],
734 				       &ppt->msix.cookie[idx]);
735 
736 		if (error != 0) {
737 			bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, ppt->msix.res[idx]);
738 			ppt->msix.cookie[idx] = NULL;
739 			ppt->msix.res[idx] = NULL;
740 			return (ENXIO);
741 		}
742 	} else {
743 		/* Masked, tear it down if it's already been set up */
744 		ppt_teardown_msix_intr(ppt, idx);
745 	}
746 
747 	return (0);
748 }
749 
750 int
751 ppt_disable_msix(struct vm *vm, int bus, int slot, int func)
752 {
753 	struct pptdev *ppt;
754 	int error;
755 
756 	error = ppt_find(vm, bus, slot, func, &ppt);
757 	if (error)
758 		return (error);
759 
760 	ppt_teardown_msix(ppt);
761 	return (0);
762 }
763