xref: /freebsd/sys/amd64/vmm/io/ppt.c (revision 83eaf7ae0a7b502de1b08850324b447591bd6916)
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 int
443 ppt_map_mmio(struct vm *vm, int bus, int slot, int func,
444 	     vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
445 {
446 	int i, error;
447 	struct pptseg *seg;
448 	struct pptdev *ppt;
449 
450 	error = ppt_find(vm, bus, slot, func, &ppt);
451 	if (error)
452 		return (error);
453 
454 	for (i = 0; i < MAX_MMIOSEGS; i++) {
455 		seg = &ppt->mmio[i];
456 		if (seg->len == 0) {
457 			error = vm_map_mmio(vm, gpa, len, hpa);
458 			if (error == 0) {
459 				seg->gpa = gpa;
460 				seg->len = len;
461 			}
462 			return (error);
463 		}
464 	}
465 	return (ENOSPC);
466 }
467 
468 int
469 ppt_unmap_mmio(struct vm *vm, int bus, int slot, int func,
470 	       vm_paddr_t gpa, size_t len)
471 {
472 	int i, error;
473 	struct pptseg *seg;
474 	struct pptdev *ppt;
475 
476 	error = ppt_find(vm, bus, slot, func, &ppt);
477 	if (error)
478 		return (error);
479 
480 	for (i = 0; i < MAX_MMIOSEGS; i++) {
481 		seg = &ppt->mmio[i];
482 		if (seg->gpa == gpa && seg->len == len) {
483 			error = vm_unmap_mmio(vm, seg->gpa, seg->len);
484 			if (error == 0) {
485 				seg->gpa = 0;
486 				seg->len = 0;
487 			}
488 			return (error);
489 		}
490 	}
491 	return (ENOENT);
492 }
493 
494 static int
495 pptintr(void *arg)
496 {
497 	struct pptdev *ppt;
498 	struct pptintr_arg *pptarg;
499 
500 	pptarg = arg;
501 	ppt = pptarg->pptdev;
502 
503 	if (ppt->vm != NULL)
504 		lapic_intr_msi(ppt->vm, pptarg->addr, pptarg->msg_data);
505 	else {
506 		/*
507 		 * XXX
508 		 * This is not expected to happen - panic?
509 		 */
510 	}
511 
512 	/*
513 	 * For legacy interrupts give other filters a chance in case
514 	 * the interrupt was not generated by the passthrough device.
515 	 */
516 	if (ppt->msi.startrid == 0)
517 		return (FILTER_STRAY);
518 	else
519 		return (FILTER_HANDLED);
520 }
521 
522 int
523 ppt_setup_msi(struct vm *vm, int vcpu, int bus, int slot, int func,
524 	      uint64_t addr, uint64_t msg, int numvec)
525 {
526 	int i, rid, flags;
527 	int msi_count, startrid, error, tmp;
528 	struct pptdev *ppt;
529 
530 	if (numvec < 0 || numvec > MAX_MSIMSGS)
531 		return (EINVAL);
532 
533 	error = ppt_find(vm, bus, slot, func, &ppt);
534 	if (error)
535 		return (error);
536 
537 	/* Reject attempts to enable MSI while MSI-X is active. */
538 	if (ppt->msix.num_msgs != 0 && numvec != 0)
539 		return (EBUSY);
540 
541 	/* Free any allocated resources */
542 	ppt_teardown_msi(ppt);
543 
544 	if (numvec == 0)		/* nothing more to do */
545 		return (0);
546 
547 	flags = RF_ACTIVE;
548 	msi_count = pci_msi_count(ppt->dev);
549 	if (msi_count == 0) {
550 		startrid = 0;		/* legacy interrupt */
551 		msi_count = 1;
552 		flags |= RF_SHAREABLE;
553 	} else
554 		startrid = 1;		/* MSI */
555 
556 	/*
557 	 * The device must be capable of supporting the number of vectors
558 	 * the guest wants to allocate.
559 	 */
560 	if (numvec > msi_count)
561 		return (EINVAL);
562 
563 	/*
564 	 * Make sure that we can allocate all the MSI vectors that are needed
565 	 * by the guest.
566 	 */
567 	if (startrid == 1) {
568 		tmp = numvec;
569 		error = pci_alloc_msi(ppt->dev, &tmp);
570 		if (error)
571 			return (error);
572 		else if (tmp != numvec) {
573 			pci_release_msi(ppt->dev);
574 			return (ENOSPC);
575 		} else {
576 			/* success */
577 		}
578 	}
579 
580 	ppt->msi.startrid = startrid;
581 
582 	/*
583 	 * Allocate the irq resource and attach it to the interrupt handler.
584 	 */
585 	for (i = 0; i < numvec; i++) {
586 		ppt->msi.num_msgs = i + 1;
587 		ppt->msi.cookie[i] = NULL;
588 
589 		rid = startrid + i;
590 		ppt->msi.res[i] = bus_alloc_resource_any(ppt->dev, SYS_RES_IRQ,
591 							 &rid, flags);
592 		if (ppt->msi.res[i] == NULL)
593 			break;
594 
595 		ppt->msi.arg[i].pptdev = ppt;
596 		ppt->msi.arg[i].addr = addr;
597 		ppt->msi.arg[i].msg_data = msg + i;
598 
599 		error = bus_setup_intr(ppt->dev, ppt->msi.res[i],
600 				       INTR_TYPE_NET | INTR_MPSAFE,
601 				       pptintr, NULL, &ppt->msi.arg[i],
602 				       &ppt->msi.cookie[i]);
603 		if (error != 0)
604 			break;
605 	}
606 
607 	if (i < numvec) {
608 		ppt_teardown_msi(ppt);
609 		return (ENXIO);
610 	}
611 
612 	return (0);
613 }
614 
615 int
616 ppt_setup_msix(struct vm *vm, int vcpu, int bus, int slot, int func,
617 	       int idx, uint64_t addr, uint64_t msg, uint32_t vector_control)
618 {
619 	struct pptdev *ppt;
620 	struct pci_devinfo *dinfo;
621 	int numvec, alloced, rid, error;
622 	size_t res_size, cookie_size, arg_size;
623 
624 	error = ppt_find(vm, bus, slot, func, &ppt);
625 	if (error)
626 		return (error);
627 
628 	/* Reject attempts to enable MSI-X while MSI is active. */
629 	if (ppt->msi.num_msgs != 0)
630 		return (EBUSY);
631 
632 	dinfo = device_get_ivars(ppt->dev);
633 	if (!dinfo)
634 		return (ENXIO);
635 
636 	/*
637 	 * First-time configuration:
638 	 * 	Allocate the MSI-X table
639 	 *	Allocate the IRQ resources
640 	 *	Set up some variables in ppt->msix
641 	 */
642 	if (ppt->msix.num_msgs == 0) {
643 		numvec = pci_msix_count(ppt->dev);
644 		if (numvec <= 0)
645 			return (EINVAL);
646 
647 		ppt->msix.startrid = 1;
648 		ppt->msix.num_msgs = numvec;
649 
650 		res_size = numvec * sizeof(ppt->msix.res[0]);
651 		cookie_size = numvec * sizeof(ppt->msix.cookie[0]);
652 		arg_size = numvec * sizeof(ppt->msix.arg[0]);
653 
654 		ppt->msix.res = malloc(res_size, M_PPTMSIX, M_WAITOK | M_ZERO);
655 		ppt->msix.cookie = malloc(cookie_size, M_PPTMSIX,
656 					  M_WAITOK | M_ZERO);
657 		ppt->msix.arg = malloc(arg_size, M_PPTMSIX, M_WAITOK | M_ZERO);
658 
659 		rid = dinfo->cfg.msix.msix_table_bar;
660 		ppt->msix.msix_table_res = bus_alloc_resource_any(ppt->dev,
661 					       SYS_RES_MEMORY, &rid, RF_ACTIVE);
662 
663 		if (ppt->msix.msix_table_res == NULL) {
664 			ppt_teardown_msix(ppt);
665 			return (ENOSPC);
666 		}
667 		ppt->msix.msix_table_rid = rid;
668 
669 		if (dinfo->cfg.msix.msix_table_bar !=
670 		    dinfo->cfg.msix.msix_pba_bar) {
671 			rid = dinfo->cfg.msix.msix_pba_bar;
672 			ppt->msix.msix_pba_res = bus_alloc_resource_any(
673 			    ppt->dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
674 
675 			if (ppt->msix.msix_pba_res == NULL) {
676 				ppt_teardown_msix(ppt);
677 				return (ENOSPC);
678 			}
679 			ppt->msix.msix_pba_rid = rid;
680 		}
681 
682 		alloced = numvec;
683 		error = pci_alloc_msix(ppt->dev, &alloced);
684 		if (error || alloced != numvec) {
685 			ppt_teardown_msix(ppt);
686 			return (error == 0 ? ENOSPC: error);
687 		}
688 	}
689 
690 	if ((vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
691 		/* Tear down the IRQ if it's already set up */
692 		ppt_teardown_msix_intr(ppt, idx);
693 
694 		/* Allocate the IRQ resource */
695 		ppt->msix.cookie[idx] = NULL;
696 		rid = ppt->msix.startrid + idx;
697 		ppt->msix.res[idx] = bus_alloc_resource_any(ppt->dev, SYS_RES_IRQ,
698 							    &rid, RF_ACTIVE);
699 		if (ppt->msix.res[idx] == NULL)
700 			return (ENXIO);
701 
702 		ppt->msix.arg[idx].pptdev = ppt;
703 		ppt->msix.arg[idx].addr = addr;
704 		ppt->msix.arg[idx].msg_data = msg;
705 
706 		/* Setup the MSI-X interrupt */
707 		error = bus_setup_intr(ppt->dev, ppt->msix.res[idx],
708 				       INTR_TYPE_NET | INTR_MPSAFE,
709 				       pptintr, NULL, &ppt->msix.arg[idx],
710 				       &ppt->msix.cookie[idx]);
711 
712 		if (error != 0) {
713 			bus_release_resource(ppt->dev, SYS_RES_IRQ, rid, ppt->msix.res[idx]);
714 			ppt->msix.cookie[idx] = NULL;
715 			ppt->msix.res[idx] = NULL;
716 			return (ENXIO);
717 		}
718 	} else {
719 		/* Masked, tear it down if it's already been set up */
720 		ppt_teardown_msix_intr(ppt, idx);
721 	}
722 
723 	return (0);
724 }
725 
726 int
727 ppt_disable_msix(struct vm *vm, int bus, int slot, int func)
728 {
729 	struct pptdev *ppt;
730 	int error;
731 
732 	error = ppt_find(vm, bus, slot, func, &ppt);
733 	if (error)
734 		return (error);
735 
736 	ppt_teardown_msix(ppt);
737 	return (0);
738 }
739