xref: /freebsd/usr.sbin/bhyve/pci_passthru.c (revision e5551216d8e563043248bc7f3cb1c75b2dbd9791)
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 #ifndef WITHOUT_CAPSICUM
36 #include <sys/capsicum.h>
37 #endif
38 #include <sys/types.h>
39 #include <sys/mman.h>
40 #include <sys/pciio.h>
41 #include <sys/ioctl.h>
42 #include <sys/stat.h>
43 
44 #include <dev/io/iodev.h>
45 #include <dev/pci/pcireg.h>
46 
47 #include <vm/vm.h>
48 
49 #include <machine/iodev.h>
50 #include <machine/vm.h>
51 
52 #ifndef WITHOUT_CAPSICUM
53 #include <capsicum_helpers.h>
54 #endif
55 #include <ctype.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <err.h>
60 #include <errno.h>
61 #include <fcntl.h>
62 #include <sysexits.h>
63 #include <unistd.h>
64 
65 #include <machine/vmm.h>
66 
67 #include "config.h"
68 #include "debug.h"
69 #include "mem.h"
70 #include "pci_passthru.h"
71 
72 #ifndef _PATH_DEVPCI
73 #define	_PATH_DEVPCI	"/dev/pci"
74 #endif
75 
76 #define	LEGACY_SUPPORT	1
77 
78 #define MSIX_TABLE_COUNT(ctrl) (((ctrl) & PCIM_MSIXCTRL_TABLE_SIZE) + 1)
79 #define MSIX_CAPLEN 12
80 
81 static int pcifd = -1;
82 
83 struct passthru_softc {
84 	struct pci_devinst *psc_pi;
85 	/* ROM is handled like a BAR */
86 	struct pcibar psc_bar[PCI_BARMAX_WITH_ROM + 1];
87 	struct {
88 		int		capoff;
89 		int		msgctrl;
90 		int		emulated;
91 	} psc_msi;
92 	struct {
93 		int		capoff;
94 	} psc_msix;
95 	struct pcisel psc_sel;
96 
97 	cfgread_handler psc_pcir_rhandler[PCI_REGMAX + 1];
98 	cfgwrite_handler psc_pcir_whandler[PCI_REGMAX + 1];
99 };
100 
101 static int
102 msi_caplen(int msgctrl)
103 {
104 	int len;
105 
106 	len = 10;		/* minimum length of msi capability */
107 
108 	if (msgctrl & PCIM_MSICTRL_64BIT)
109 		len += 4;
110 
111 #if 0
112 	/*
113 	 * Ignore the 'mask' and 'pending' bits in the MSI capability.
114 	 * We'll let the guest manipulate them directly.
115 	 */
116 	if (msgctrl & PCIM_MSICTRL_VECTOR)
117 		len += 10;
118 #endif
119 
120 	return (len);
121 }
122 
123 static int
124 pcifd_init(void)
125 {
126 	pcifd = open(_PATH_DEVPCI, O_RDWR, 0);
127 	if (pcifd < 0) {
128 		warn("failed to open %s", _PATH_DEVPCI);
129 		return (1);
130 	}
131 
132 #ifndef WITHOUT_CAPSICUM
133 	cap_rights_t pcifd_rights;
134 	cap_rights_init(&pcifd_rights, CAP_IOCTL, CAP_READ, CAP_WRITE);
135 	if (caph_rights_limit(pcifd, &pcifd_rights) == -1)
136 		errx(EX_OSERR, "Unable to apply rights for sandbox");
137 
138 	const cap_ioctl_t pcifd_ioctls[] = { PCIOCREAD, PCIOCWRITE, PCIOCGETBAR,
139 		PCIOCBARIO, PCIOCBARMMAP, PCIOCGETCONF };
140 	if (caph_ioctls_limit(pcifd, pcifd_ioctls, nitems(pcifd_ioctls)) == -1)
141 		errx(EX_OSERR, "Unable to apply rights for sandbox");
142 #endif
143 
144 	return (0);
145 }
146 
147 uint32_t
148 read_config(const struct pcisel *sel, long reg, int width)
149 {
150 	struct pci_io pi;
151 
152 	if (pcifd < 0 && pcifd_init()) {
153 		return (0);
154 	}
155 
156 	bzero(&pi, sizeof(pi));
157 	pi.pi_sel = *sel;
158 	pi.pi_reg = reg;
159 	pi.pi_width = width;
160 
161 	if (ioctl(pcifd, PCIOCREAD, &pi) < 0)
162 		return (0);				/* XXX */
163 	else
164 		return (pi.pi_data);
165 }
166 
167 void
168 write_config(const struct pcisel *sel, long reg, int width, uint32_t data)
169 {
170 	struct pci_io pi;
171 
172 	if (pcifd < 0 && pcifd_init()) {
173 		return;
174 	}
175 
176 	bzero(&pi, sizeof(pi));
177 	pi.pi_sel = *sel;
178 	pi.pi_reg = reg;
179 	pi.pi_width = width;
180 	pi.pi_data = data;
181 
182 	(void)ioctl(pcifd, PCIOCWRITE, &pi);		/* XXX */
183 }
184 
185 #ifdef LEGACY_SUPPORT
186 static int
187 passthru_add_msicap(struct pci_devinst *pi, int msgnum, int nextptr)
188 {
189 	int capoff;
190 	struct msicap msicap;
191 	u_char *capdata;
192 
193 	pci_populate_msicap(&msicap, msgnum, nextptr);
194 
195 	/*
196 	 * XXX
197 	 * Copy the msi capability structure in the last 16 bytes of the
198 	 * config space. This is wrong because it could shadow something
199 	 * useful to the device.
200 	 */
201 	capoff = 256 - roundup(sizeof(msicap), 4);
202 	capdata = (u_char *)&msicap;
203 	for (size_t i = 0; i < sizeof(msicap); i++)
204 		pci_set_cfgdata8(pi, capoff + i, capdata[i]);
205 
206 	return (capoff);
207 }
208 #endif	/* LEGACY_SUPPORT */
209 
210 static int
211 cfginitmsi(struct passthru_softc *sc)
212 {
213 	int i, ptr, capptr, cap, sts, caplen, table_size;
214 	uint32_t u32;
215 	struct pcisel sel;
216 	struct pci_devinst *pi;
217 	struct msixcap msixcap;
218 	char *msixcap_ptr;
219 
220 	pi = sc->psc_pi;
221 	sel = sc->psc_sel;
222 
223 	/*
224 	 * Parse the capabilities and cache the location of the MSI
225 	 * and MSI-X capabilities.
226 	 */
227 	sts = read_config(&sel, PCIR_STATUS, 2);
228 	if (sts & PCIM_STATUS_CAPPRESENT) {
229 		ptr = read_config(&sel, PCIR_CAP_PTR, 1);
230 		while (ptr != 0 && ptr != 0xff) {
231 			cap = read_config(&sel, ptr + PCICAP_ID, 1);
232 			if (cap == PCIY_MSI) {
233 				/*
234 				 * Copy the MSI capability into the config
235 				 * space of the emulated pci device
236 				 */
237 				sc->psc_msi.capoff = ptr;
238 				sc->psc_msi.msgctrl = read_config(&sel,
239 								  ptr + 2, 2);
240 				sc->psc_msi.emulated = 0;
241 				caplen = msi_caplen(sc->psc_msi.msgctrl);
242 				capptr = ptr;
243 				while (caplen > 0) {
244 					u32 = read_config(&sel, capptr, 4);
245 					pci_set_cfgdata32(pi, capptr, u32);
246 					caplen -= 4;
247 					capptr += 4;
248 				}
249 			} else if (cap == PCIY_MSIX) {
250 				/*
251 				 * Copy the MSI-X capability
252 				 */
253 				sc->psc_msix.capoff = ptr;
254 				caplen = 12;
255 				msixcap_ptr = (char *)&msixcap;
256 				capptr = ptr;
257 				while (caplen > 0) {
258 					u32 = read_config(&sel, capptr, 4);
259 					memcpy(msixcap_ptr, &u32, 4);
260 					pci_set_cfgdata32(pi, capptr, u32);
261 					caplen -= 4;
262 					capptr += 4;
263 					msixcap_ptr += 4;
264 				}
265 			}
266 			ptr = read_config(&sel, ptr + PCICAP_NEXTPTR, 1);
267 		}
268 	}
269 
270 	if (sc->psc_msix.capoff != 0) {
271 		pi->pi_msix.pba_bar =
272 		    msixcap.pba_info & PCIM_MSIX_BIR_MASK;
273 		pi->pi_msix.pba_offset =
274 		    msixcap.pba_info & ~PCIM_MSIX_BIR_MASK;
275 		pi->pi_msix.table_bar =
276 		    msixcap.table_info & PCIM_MSIX_BIR_MASK;
277 		pi->pi_msix.table_offset =
278 		    msixcap.table_info & ~PCIM_MSIX_BIR_MASK;
279 		pi->pi_msix.table_count = MSIX_TABLE_COUNT(msixcap.msgctrl);
280 		pi->pi_msix.pba_size = PBA_SIZE(pi->pi_msix.table_count);
281 
282 		/* Allocate the emulated MSI-X table array */
283 		table_size = pi->pi_msix.table_count * MSIX_TABLE_ENTRY_SIZE;
284 		pi->pi_msix.table = calloc(1, table_size);
285 
286 		/* Mask all table entries */
287 		for (i = 0; i < pi->pi_msix.table_count; i++) {
288 			pi->pi_msix.table[i].vector_control |=
289 						PCIM_MSIX_VCTRL_MASK;
290 		}
291 	}
292 
293 #ifdef LEGACY_SUPPORT
294 	/*
295 	 * If the passthrough device does not support MSI then craft a
296 	 * MSI capability for it. We link the new MSI capability at the
297 	 * head of the list of capabilities.
298 	 */
299 	if ((sts & PCIM_STATUS_CAPPRESENT) != 0 && sc->psc_msi.capoff == 0) {
300 		int origptr, msiptr;
301 		origptr = read_config(&sel, PCIR_CAP_PTR, 1);
302 		msiptr = passthru_add_msicap(pi, 1, origptr);
303 		sc->psc_msi.capoff = msiptr;
304 		sc->psc_msi.msgctrl = pci_get_cfgdata16(pi, msiptr + 2);
305 		sc->psc_msi.emulated = 1;
306 		pci_set_cfgdata8(pi, PCIR_CAP_PTR, msiptr);
307 	}
308 #endif
309 
310 	/* Make sure one of the capabilities is present */
311 	if (sc->psc_msi.capoff == 0 && sc->psc_msix.capoff == 0)
312 		return (-1);
313 	else
314 		return (0);
315 }
316 
317 static uint64_t
318 msix_table_read(struct passthru_softc *sc, uint64_t offset, int size)
319 {
320 	struct pci_devinst *pi;
321 	struct msix_table_entry *entry;
322 	uint8_t *src8;
323 	uint16_t *src16;
324 	uint32_t *src32;
325 	uint64_t *src64;
326 	uint64_t data;
327 	size_t entry_offset;
328 	uint32_t table_offset;
329 	int index, table_count;
330 
331 	pi = sc->psc_pi;
332 
333 	table_offset = pi->pi_msix.table_offset;
334 	table_count = pi->pi_msix.table_count;
335 	if (offset < table_offset ||
336 	    offset >= table_offset + table_count * MSIX_TABLE_ENTRY_SIZE) {
337 		switch (size) {
338 		case 1:
339 			src8 = (uint8_t *)(pi->pi_msix.mapped_addr + offset);
340 			data = *src8;
341 			break;
342 		case 2:
343 			src16 = (uint16_t *)(pi->pi_msix.mapped_addr + offset);
344 			data = *src16;
345 			break;
346 		case 4:
347 			src32 = (uint32_t *)(pi->pi_msix.mapped_addr + offset);
348 			data = *src32;
349 			break;
350 		case 8:
351 			src64 = (uint64_t *)(pi->pi_msix.mapped_addr + offset);
352 			data = *src64;
353 			break;
354 		default:
355 			return (-1);
356 		}
357 		return (data);
358 	}
359 
360 	offset -= table_offset;
361 	index = offset / MSIX_TABLE_ENTRY_SIZE;
362 	assert(index < table_count);
363 
364 	entry = &pi->pi_msix.table[index];
365 	entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
366 
367 	switch (size) {
368 	case 1:
369 		src8 = (uint8_t *)((uint8_t *)entry + entry_offset);
370 		data = *src8;
371 		break;
372 	case 2:
373 		src16 = (uint16_t *)((uint8_t *)entry + entry_offset);
374 		data = *src16;
375 		break;
376 	case 4:
377 		src32 = (uint32_t *)((uint8_t *)entry + entry_offset);
378 		data = *src32;
379 		break;
380 	case 8:
381 		src64 = (uint64_t *)((uint8_t *)entry + entry_offset);
382 		data = *src64;
383 		break;
384 	default:
385 		return (-1);
386 	}
387 
388 	return (data);
389 }
390 
391 static void
392 msix_table_write(struct passthru_softc *sc, uint64_t offset, int size,
393     uint64_t data)
394 {
395 	struct pci_devinst *pi;
396 	struct msix_table_entry *entry;
397 	uint8_t *dest8;
398 	uint16_t *dest16;
399 	uint32_t *dest32;
400 	uint64_t *dest64;
401 	size_t entry_offset;
402 	uint32_t table_offset, vector_control;
403 	int index, table_count;
404 
405 	pi = sc->psc_pi;
406 
407 	table_offset = pi->pi_msix.table_offset;
408 	table_count = pi->pi_msix.table_count;
409 	if (offset < table_offset ||
410 	    offset >= table_offset + table_count * MSIX_TABLE_ENTRY_SIZE) {
411 		switch (size) {
412 		case 1:
413 			dest8 = (uint8_t *)(pi->pi_msix.mapped_addr + offset);
414 			*dest8 = data;
415 			break;
416 		case 2:
417 			dest16 = (uint16_t *)(pi->pi_msix.mapped_addr + offset);
418 			*dest16 = data;
419 			break;
420 		case 4:
421 			dest32 = (uint32_t *)(pi->pi_msix.mapped_addr + offset);
422 			*dest32 = data;
423 			break;
424 		case 8:
425 			dest64 = (uint64_t *)(pi->pi_msix.mapped_addr + offset);
426 			*dest64 = data;
427 			break;
428 		}
429 		return;
430 	}
431 
432 	offset -= table_offset;
433 	index = offset / MSIX_TABLE_ENTRY_SIZE;
434 	assert(index < table_count);
435 
436 	entry = &pi->pi_msix.table[index];
437 	entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
438 
439 	/* Only 4 byte naturally-aligned writes are supported */
440 	assert(size == 4);
441 	assert(entry_offset % 4 == 0);
442 
443 	vector_control = entry->vector_control;
444 	dest32 = (uint32_t *)((uint8_t *)entry + entry_offset);
445 	*dest32 = data;
446 	/* If MSI-X hasn't been enabled, do nothing */
447 	if (pi->pi_msix.enabled) {
448 		/* If the entry is masked, don't set it up */
449 		if ((entry->vector_control & PCIM_MSIX_VCTRL_MASK) == 0 ||
450 		    (vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
451 			(void)vm_setup_pptdev_msix(sc->psc_pi->pi_vmctx,
452 			    sc->psc_sel.pc_bus, sc->psc_sel.pc_dev,
453 			    sc->psc_sel.pc_func, index, entry->addr,
454 			    entry->msg_data, entry->vector_control);
455 		}
456 	}
457 }
458 
459 static int
460 init_msix_table(struct passthru_softc *sc)
461 {
462 	struct pci_devinst *pi = sc->psc_pi;
463 	struct pci_bar_mmap pbm;
464 	int b, s, f;
465 	uint32_t table_size, table_offset;
466 
467 	assert(pci_msix_table_bar(pi) >= 0 && pci_msix_pba_bar(pi) >= 0);
468 
469 	b = sc->psc_sel.pc_bus;
470 	s = sc->psc_sel.pc_dev;
471 	f = sc->psc_sel.pc_func;
472 
473 	/*
474 	 * Map the region of the BAR containing the MSI-X table.  This is
475 	 * necessary for two reasons:
476 	 * 1. The PBA may reside in the first or last page containing the MSI-X
477 	 *    table.
478 	 * 2. While PCI devices are not supposed to use the page(s) containing
479 	 *    the MSI-X table for other purposes, some do in practice.
480 	 */
481 	memset(&pbm, 0, sizeof(pbm));
482 	pbm.pbm_sel = sc->psc_sel;
483 	pbm.pbm_flags = PCIIO_BAR_MMAP_RW;
484 	pbm.pbm_reg = PCIR_BAR(pi->pi_msix.table_bar);
485 	pbm.pbm_memattr = VM_MEMATTR_DEVICE;
486 
487 	if (ioctl(pcifd, PCIOCBARMMAP, &pbm) != 0) {
488 		warn("Failed to map MSI-X table BAR on %d/%d/%d", b, s, f);
489 		return (-1);
490 	}
491 	assert(pbm.pbm_bar_off == 0);
492 	pi->pi_msix.mapped_addr = (uint8_t *)(uintptr_t)pbm.pbm_map_base;
493 	pi->pi_msix.mapped_size = pbm.pbm_map_length;
494 
495 	table_offset = rounddown2(pi->pi_msix.table_offset, 4096);
496 
497 	table_size = pi->pi_msix.table_offset - table_offset;
498 	table_size += pi->pi_msix.table_count * MSIX_TABLE_ENTRY_SIZE;
499 	table_size = roundup2(table_size, 4096);
500 
501 	/*
502 	 * Unmap any pages not containing the table, we do not need to emulate
503 	 * accesses to them.  Avoid releasing address space to help ensure that
504 	 * a buggy out-of-bounds access causes a crash.
505 	 */
506 	if (table_offset != 0)
507 		if (mprotect(pi->pi_msix.mapped_addr, table_offset,
508 		    PROT_NONE) != 0)
509 			warn("Failed to unmap MSI-X table BAR region");
510 	if (table_offset + table_size != pi->pi_msix.mapped_size)
511 		if (mprotect(
512 		    pi->pi_msix.mapped_addr + table_offset + table_size,
513 		    pi->pi_msix.mapped_size - (table_offset + table_size),
514 		    PROT_NONE) != 0)
515 			warn("Failed to unmap MSI-X table BAR region");
516 
517 	return (0);
518 }
519 
520 static int
521 cfginitbar(struct passthru_softc *sc)
522 {
523 	int i, error;
524 	struct pci_devinst *pi;
525 	struct pci_bar_io bar;
526 	enum pcibar_type bartype;
527 	uint64_t base, size;
528 
529 	pi = sc->psc_pi;
530 
531 	/*
532 	 * Initialize BAR registers
533 	 */
534 	for (i = 0; i <= PCI_BARMAX; i++) {
535 		bzero(&bar, sizeof(bar));
536 		bar.pbi_sel = sc->psc_sel;
537 		bar.pbi_reg = PCIR_BAR(i);
538 
539 		if (ioctl(pcifd, PCIOCGETBAR, &bar) < 0)
540 			continue;
541 
542 		if (PCI_BAR_IO(bar.pbi_base)) {
543 			bartype = PCIBAR_IO;
544 			base = bar.pbi_base & PCIM_BAR_IO_BASE;
545 		} else {
546 			switch (bar.pbi_base & PCIM_BAR_MEM_TYPE) {
547 			case PCIM_BAR_MEM_64:
548 				bartype = PCIBAR_MEM64;
549 				break;
550 			default:
551 				bartype = PCIBAR_MEM32;
552 				break;
553 			}
554 			base = bar.pbi_base & PCIM_BAR_MEM_BASE;
555 		}
556 		size = bar.pbi_length;
557 
558 		if (bartype != PCIBAR_IO) {
559 			if (((base | size) & PAGE_MASK) != 0) {
560 				warnx("passthru device %d/%d/%d BAR %d: "
561 				    "base %#lx or size %#lx not page aligned\n",
562 				    sc->psc_sel.pc_bus, sc->psc_sel.pc_dev,
563 				    sc->psc_sel.pc_func, i, base, size);
564 				return (-1);
565 			}
566 		}
567 
568 		/* Cache information about the "real" BAR */
569 		sc->psc_bar[i].type = bartype;
570 		sc->psc_bar[i].size = size;
571 		sc->psc_bar[i].addr = base;
572 		sc->psc_bar[i].lobits = 0;
573 
574 		/* Allocate the BAR in the guest I/O or MMIO space */
575 		error = pci_emul_alloc_bar(pi, i, bartype, size);
576 		if (error)
577 			return (-1);
578 
579 		/* Use same lobits as physical bar */
580 		uint8_t lobits = read_config(&sc->psc_sel, PCIR_BAR(i), 0x01);
581 		if (bartype == PCIBAR_MEM32 || bartype == PCIBAR_MEM64) {
582 			lobits &= ~PCIM_BAR_MEM_BASE;
583 		} else {
584 			lobits &= ~PCIM_BAR_IO_BASE;
585 		}
586 		sc->psc_bar[i].lobits = lobits;
587 		pi->pi_bar[i].lobits = lobits;
588 
589 		/*
590 		 * 64-bit BAR takes up two slots so skip the next one.
591 		 */
592 		if (bartype == PCIBAR_MEM64) {
593 			i++;
594 			assert(i <= PCI_BARMAX);
595 			sc->psc_bar[i].type = PCIBAR_MEMHI64;
596 		}
597 	}
598 	return (0);
599 }
600 
601 static int
602 cfginit(struct pci_devinst *pi, int bus, int slot, int func)
603 {
604 	int error;
605 	struct passthru_softc *sc;
606 	uint8_t intline, intpin;
607 
608 	error = 1;
609 	sc = pi->pi_arg;
610 
611 	bzero(&sc->psc_sel, sizeof(struct pcisel));
612 	sc->psc_sel.pc_bus = bus;
613 	sc->psc_sel.pc_dev = slot;
614 	sc->psc_sel.pc_func = func;
615 
616 	/*
617 	 * Copy physical PCI header to virtual config space. INTLINE and INTPIN
618 	 * shouldn't be aligned with their physical value and they are already set by
619 	 * pci_emul_init().
620 	 */
621 	intline = pci_get_cfgdata8(pi, PCIR_INTLINE);
622 	intpin = pci_get_cfgdata8(pi, PCIR_INTPIN);
623 	for (int i = 0; i <= PCIR_MAXLAT; i += 4) {
624 		pci_set_cfgdata32(pi, i, read_config(&sc->psc_sel, i, 4));
625 	}
626 	pci_set_cfgdata8(pi, PCIR_INTLINE, intline);
627 	pci_set_cfgdata8(pi, PCIR_INTPIN, intpin);
628 
629 	if (cfginitmsi(sc) != 0) {
630 		warnx("failed to initialize MSI for PCI %d/%d/%d",
631 		    bus, slot, func);
632 		goto done;
633 	}
634 
635 	if (cfginitbar(sc) != 0) {
636 		warnx("failed to initialize BARs for PCI %d/%d/%d",
637 		    bus, slot, func);
638 		goto done;
639 	}
640 
641 	write_config(&sc->psc_sel, PCIR_COMMAND, 2,
642 	    pci_get_cfgdata16(pi, PCIR_COMMAND));
643 
644 	/*
645 	 * We need to do this after PCIR_COMMAND got possibly updated, e.g.,
646 	 * a BAR was enabled, as otherwise the PCIOCBARMMAP might fail on us.
647 	 */
648 	if (pci_msix_table_bar(pi) >= 0) {
649 		error = init_msix_table(sc);
650 		if (error != 0) {
651 			warnx(
652 			    "failed to initialize MSI-X table for PCI %d/%d/%d: %d",
653 			    bus, slot, func, error);
654 			goto done;
655 		}
656 	}
657 
658 	error = 0;				/* success */
659 done:
660 	return (error);
661 }
662 
663 int
664 set_pcir_handler(struct passthru_softc *sc, int reg, int len,
665     cfgread_handler rhandler, cfgwrite_handler whandler)
666 {
667 	if (reg > PCI_REGMAX || reg + len > PCI_REGMAX + 1)
668 		return (-1);
669 
670 	for (int i = reg; i < reg + len; ++i) {
671 		assert(sc->psc_pcir_rhandler[i] == NULL || rhandler == NULL);
672 		assert(sc->psc_pcir_whandler[i] == NULL || whandler == NULL);
673 		sc->psc_pcir_rhandler[i] = rhandler;
674 		sc->psc_pcir_whandler[i] = whandler;
675 	}
676 
677 	return (0);
678 }
679 
680 static int
681 passthru_legacy_config(nvlist_t *nvl, const char *opts)
682 {
683 	const char *cp;
684 	char *tofree;
685 	char value[16];
686 	int bus, slot, func;
687 
688 	if (opts == NULL)
689 		return (0);
690 
691 	cp = strchr(opts, ',');
692 
693 	if (strncmp(opts, "ppt", strlen("ppt")) == 0) {
694 		tofree = strndup(opts, cp - opts);
695 		set_config_value_node(nvl, "pptdev", tofree);
696 		free(tofree);
697 	} else if (sscanf(opts, "pci0:%d:%d:%d", &bus, &slot, &func) == 3 ||
698 	    sscanf(opts, "pci%d:%d:%d", &bus, &slot, &func) == 3 ||
699 	    sscanf(opts, "%d/%d/%d", &bus, &slot, &func) == 3) {
700 		snprintf(value, sizeof(value), "%d", bus);
701 		set_config_value_node(nvl, "bus", value);
702 		snprintf(value, sizeof(value), "%d", slot);
703 		set_config_value_node(nvl, "slot", value);
704 		snprintf(value, sizeof(value), "%d", func);
705 		set_config_value_node(nvl, "func", value);
706 	} else {
707 		EPRINTLN("passthru: invalid options \"%s\"", opts);
708 		return (-1);
709 	}
710 
711 	if (cp == NULL) {
712 		return (0);
713 	}
714 
715 	return (pci_parse_legacy_config(nvl, cp + 1));
716 }
717 
718 static int
719 passthru_init_rom(struct passthru_softc *const sc, const char *const romfile)
720 {
721 	if (romfile == NULL) {
722 		return (0);
723 	}
724 
725 	const int fd = open(romfile, O_RDONLY);
726 	if (fd < 0) {
727 		warnx("%s: can't open romfile \"%s\"", __func__, romfile);
728 		return (-1);
729 	}
730 
731 	struct stat sbuf;
732 	if (fstat(fd, &sbuf) < 0) {
733 		warnx("%s: can't fstat romfile \"%s\"", __func__, romfile);
734 		close(fd);
735 		return (-1);
736 	}
737 	const uint64_t rom_size = sbuf.st_size;
738 
739 	void *const rom_data = mmap(NULL, rom_size, PROT_READ, MAP_SHARED, fd,
740 	    0);
741 	if (rom_data == MAP_FAILED) {
742 		warnx("%s: unable to mmap romfile \"%s\" (%d)", __func__,
743 		    romfile, errno);
744 		close(fd);
745 		return (-1);
746 	}
747 
748 	void *rom_addr;
749 	int error = pci_emul_alloc_rom(sc->psc_pi, rom_size, &rom_addr);
750 	if (error) {
751 		warnx("%s: failed to alloc rom segment", __func__);
752 		munmap(rom_data, rom_size);
753 		close(fd);
754 		return (error);
755 	}
756 	memcpy(rom_addr, rom_data, rom_size);
757 
758 	sc->psc_bar[PCI_ROM_IDX].type = PCIBAR_ROM;
759 	sc->psc_bar[PCI_ROM_IDX].addr = (uint64_t)rom_addr;
760 	sc->psc_bar[PCI_ROM_IDX].size = rom_size;
761 
762 	munmap(rom_data, rom_size);
763 	close(fd);
764 
765 	return (0);
766 }
767 
768 static bool
769 passthru_lookup_pptdev(const char *name, int *bus, int *slot, int *func)
770 {
771 	struct pci_conf_io pc;
772 	struct pci_conf conf[1];
773 	struct pci_match_conf patterns[1];
774 	char *cp;
775 
776 	bzero(&pc, sizeof(struct pci_conf_io));
777 	pc.match_buf_len = sizeof(conf);
778 	pc.matches = conf;
779 
780 	bzero(&patterns, sizeof(patterns));
781 
782 	/*
783 	 * The pattern structure requires the unit to be split out from
784 	 * the driver name.  Walk backwards from the end of the name to
785 	 * find the start of the unit.
786 	 */
787 	cp = strchr(name, '\0');
788 	assert(cp != NULL);
789 	while (cp != name && isdigit(cp[-1]))
790 		cp--;
791 	if (cp == name || !isdigit(*cp)) {
792 		EPRINTLN("Invalid passthru device name %s", name);
793 		return (false);
794 	}
795 	if ((size_t)(cp - name) + 1 > sizeof(patterns[0].pd_name)) {
796 		EPRINTLN("Passthru device name %s is too long", name);
797 		return (false);
798 	}
799 	memcpy(patterns[0].pd_name, name, cp - name);
800 	patterns[0].pd_unit = strtol(cp, &cp, 10);
801 	if (*cp != '\0') {
802 		EPRINTLN("Invalid passthru device name %s", name);
803 		return (false);
804 	}
805 	patterns[0].flags = PCI_GETCONF_MATCH_NAME | PCI_GETCONF_MATCH_UNIT;
806 	pc.num_patterns = 1;
807 	pc.pat_buf_len = sizeof(patterns);
808 	pc.patterns = patterns;
809 
810 	if (ioctl(pcifd, PCIOCGETCONF, &pc) == -1) {
811 		EPRINTLN("ioctl(PCIOCGETCONF): %s", strerror(errno));
812 		return (false);
813 	}
814 	if (pc.status != PCI_GETCONF_LAST_DEVICE &&
815 	    pc.status != PCI_GETCONF_MORE_DEVS) {
816 		EPRINTLN("error returned from PCIOCGETCONF ioctl");
817 		return (false);
818 	}
819 	if (pc.num_matches == 0) {
820 		EPRINTLN("Passthru device %s not found", name);
821 		return (false);
822 	}
823 
824 	if (conf[0].pc_sel.pc_domain != 0) {
825 		EPRINTLN("Passthru device %s on unsupported domain", name);
826 		return (false);
827 	}
828 	*bus = conf[0].pc_sel.pc_bus;
829 	*slot = conf[0].pc_sel.pc_dev;
830 	*func = conf[0].pc_sel.pc_func;
831 	return (true);
832 }
833 
834 static int
835 passthru_init(struct pci_devinst *pi, nvlist_t *nvl)
836 {
837 	int bus, slot, func, error, memflags;
838 	struct passthru_softc *sc;
839 	const char *value;
840 
841 	sc = NULL;
842 	error = 1;
843 
844 	memflags = vm_get_memflags(pi->pi_vmctx);
845 	if (!(memflags & VM_MEM_F_WIRED)) {
846 		warnx("passthru requires guest memory to be wired");
847 		return (error);
848 	}
849 
850 	if (pcifd < 0 && pcifd_init()) {
851 		return (error);
852 	}
853 
854 #define GET_INT_CONFIG(var, name) do {					\
855 	value = get_config_value_node(nvl, name);			\
856 	if (value == NULL) {						\
857 		EPRINTLN("passthru: missing required %s setting", name); \
858 		return (error);						\
859 	}								\
860 	var = atoi(value);						\
861 } while (0)
862 
863 	value = get_config_value_node(nvl, "pptdev");
864 	if (value != NULL) {
865 		if (!passthru_lookup_pptdev(value, &bus, &slot, &func))
866 			return (error);
867 	} else {
868 		GET_INT_CONFIG(bus, "bus");
869 		GET_INT_CONFIG(slot, "slot");
870 		GET_INT_CONFIG(func, "func");
871 	}
872 
873 	if (vm_assign_pptdev(pi->pi_vmctx, bus, slot, func) != 0) {
874 		warnx("PCI device at %d/%d/%d is not using the ppt(4) driver",
875 		    bus, slot, func);
876 		goto done;
877 	}
878 
879 	sc = calloc(1, sizeof(struct passthru_softc));
880 
881 	pi->pi_arg = sc;
882 	sc->psc_pi = pi;
883 
884 	/* initialize config space */
885 	if ((error = cfginit(pi, bus, slot, func)) != 0)
886 		goto done;
887 
888 	/* initialize ROM */
889 	if ((error = passthru_init_rom(sc,
890             get_config_value_node(nvl, "rom"))) != 0)
891 		goto done;
892 
893 	/* Emulate most PCI header register. */
894 	if ((error = set_pcir_handler(sc, 0, PCIR_MAXLAT + 1,
895 	    passthru_cfgread_emulate, passthru_cfgwrite_emulate)) != 0)
896 		goto done;
897 
898 	/* Allow access to the physical command and status register. */
899 	if ((error = set_pcir_handler(sc, PCIR_COMMAND, 0x04, NULL, NULL)) != 0)
900 		goto done;
901 
902 	error = 0;		/* success */
903 done:
904 	if (error) {
905 		free(sc);
906 		vm_unassign_pptdev(pi->pi_vmctx, bus, slot, func);
907 	}
908 	return (error);
909 }
910 
911 static int
912 msicap_access(struct passthru_softc *sc, int coff)
913 {
914 	int caplen;
915 
916 	if (sc->psc_msi.capoff == 0)
917 		return (0);
918 
919 	caplen = msi_caplen(sc->psc_msi.msgctrl);
920 
921 	if (coff >= sc->psc_msi.capoff && coff < sc->psc_msi.capoff + caplen)
922 		return (1);
923 	else
924 		return (0);
925 }
926 
927 static int
928 msixcap_access(struct passthru_softc *sc, int coff)
929 {
930 	if (sc->psc_msix.capoff == 0)
931 		return (0);
932 
933 	return (coff >= sc->psc_msix.capoff &&
934 	        coff < sc->psc_msix.capoff + MSIX_CAPLEN);
935 }
936 
937 static int
938 passthru_cfgread_default(struct passthru_softc *sc,
939     struct pci_devinst *pi __unused, int coff, int bytes, uint32_t *rv)
940 {
941 	/*
942 	 * MSI capability is emulated.
943 	 */
944 	if (msicap_access(sc, coff) || msixcap_access(sc, coff))
945 		return (-1);
946 
947 	/*
948 	 * Emulate the command register.  If a single read reads both the
949 	 * command and status registers, read the status register from the
950 	 * device's config space.
951 	 */
952 	if (coff == PCIR_COMMAND) {
953 		if (bytes <= 2)
954 			return (-1);
955 		*rv = read_config(&sc->psc_sel, PCIR_STATUS, 2) << 16 |
956 		    pci_get_cfgdata16(pi, PCIR_COMMAND);
957 		return (0);
958 	}
959 
960 	/* Everything else just read from the device's config space */
961 	*rv = read_config(&sc->psc_sel, coff, bytes);
962 
963 	return (0);
964 }
965 
966 int
967 passthru_cfgread_emulate(struct passthru_softc *sc __unused,
968     struct pci_devinst *pi __unused, int coff __unused, int bytes __unused,
969     uint32_t *rv __unused)
970 {
971 	return (-1);
972 }
973 
974 static int
975 passthru_cfgread(struct pci_devinst *pi, int coff, int bytes, uint32_t *rv)
976 {
977 	struct passthru_softc *sc;
978 
979 	sc = pi->pi_arg;
980 
981 	if (sc->psc_pcir_rhandler[coff] != NULL)
982 		return (sc->psc_pcir_rhandler[coff](sc, pi, coff, bytes, rv));
983 
984 	return (passthru_cfgread_default(sc, pi, coff, bytes, rv));
985 }
986 
987 static int
988 passthru_cfgwrite_default(struct passthru_softc *sc, struct pci_devinst *pi,
989     int coff, int bytes, uint32_t val)
990 {
991 	int error, msix_table_entries, i;
992 	uint16_t cmd_old;
993 
994 	/*
995 	 * MSI capability is emulated
996 	 */
997 	if (msicap_access(sc, coff)) {
998 		pci_emul_capwrite(pi, coff, bytes, val, sc->psc_msi.capoff,
999 		    PCIY_MSI);
1000 		error = vm_setup_pptdev_msi(pi->pi_vmctx, sc->psc_sel.pc_bus,
1001 			sc->psc_sel.pc_dev, sc->psc_sel.pc_func,
1002 			pi->pi_msi.addr, pi->pi_msi.msg_data,
1003 			pi->pi_msi.maxmsgnum);
1004 		if (error != 0)
1005 			err(1, "vm_setup_pptdev_msi");
1006 		return (0);
1007 	}
1008 
1009 	if (msixcap_access(sc, coff)) {
1010 		pci_emul_capwrite(pi, coff, bytes, val, sc->psc_msix.capoff,
1011 		    PCIY_MSIX);
1012 		if (pi->pi_msix.enabled) {
1013 			msix_table_entries = pi->pi_msix.table_count;
1014 			for (i = 0; i < msix_table_entries; i++) {
1015 				error = vm_setup_pptdev_msix(pi->pi_vmctx,
1016 				    sc->psc_sel.pc_bus, sc->psc_sel.pc_dev,
1017 				    sc->psc_sel.pc_func, i,
1018 				    pi->pi_msix.table[i].addr,
1019 				    pi->pi_msix.table[i].msg_data,
1020 				    pi->pi_msix.table[i].vector_control);
1021 
1022 				if (error)
1023 					err(1, "vm_setup_pptdev_msix");
1024 			}
1025 		} else {
1026 			error = vm_disable_pptdev_msix(pi->pi_vmctx,
1027 			    sc->psc_sel.pc_bus, sc->psc_sel.pc_dev,
1028 			    sc->psc_sel.pc_func);
1029 			if (error)
1030 				err(1, "vm_disable_pptdev_msix");
1031 		}
1032 		return (0);
1033 	}
1034 
1035 #ifdef LEGACY_SUPPORT
1036 	/*
1037 	 * If this device does not support MSI natively then we cannot let
1038 	 * the guest disable legacy interrupts from the device. It is the
1039 	 * legacy interrupt that is triggering the virtual MSI to the guest.
1040 	 */
1041 	if (sc->psc_msi.emulated && pci_msi_enabled(pi)) {
1042 		if (coff == PCIR_COMMAND && bytes == 2)
1043 			val &= ~PCIM_CMD_INTxDIS;
1044 	}
1045 #endif
1046 
1047 	write_config(&sc->psc_sel, coff, bytes, val);
1048 	if (coff == PCIR_COMMAND) {
1049 		cmd_old = pci_get_cfgdata16(pi, PCIR_COMMAND);
1050 		if (bytes == 1)
1051 			pci_set_cfgdata8(pi, PCIR_COMMAND, val);
1052 		else if (bytes == 2)
1053 			pci_set_cfgdata16(pi, PCIR_COMMAND, val);
1054 		pci_emul_cmd_changed(pi, cmd_old);
1055 	}
1056 
1057 	return (0);
1058 }
1059 
1060 int
1061 passthru_cfgwrite_emulate(struct passthru_softc *sc __unused,
1062     struct pci_devinst *pi __unused, int coff __unused, int bytes __unused,
1063     uint32_t val __unused)
1064 {
1065 	return (-1);
1066 }
1067 
1068 static int
1069 passthru_cfgwrite(struct pci_devinst *pi, int coff, int bytes, uint32_t val)
1070 {
1071 	struct passthru_softc *sc;
1072 
1073 	sc = pi->pi_arg;
1074 
1075 	if (sc->psc_pcir_whandler[coff] != NULL)
1076 		return (sc->psc_pcir_whandler[coff](sc, pi, coff, bytes, val));
1077 
1078 	return (passthru_cfgwrite_default(sc, pi, coff, bytes, val));
1079 }
1080 
1081 static void
1082 passthru_write(struct pci_devinst *pi, int baridx, uint64_t offset, int size,
1083     uint64_t value)
1084 {
1085 	struct passthru_softc *sc;
1086 	struct pci_bar_ioreq pio;
1087 
1088 	sc = pi->pi_arg;
1089 
1090 	if (baridx == pci_msix_table_bar(pi)) {
1091 		msix_table_write(sc, offset, size, value);
1092 	} else {
1093 		assert(pi->pi_bar[baridx].type == PCIBAR_IO);
1094 		assert(size == 1 || size == 2 || size == 4);
1095 		assert(offset <= UINT32_MAX && offset + size <= UINT32_MAX);
1096 
1097 		bzero(&pio, sizeof(pio));
1098 		pio.pbi_sel = sc->psc_sel;
1099 		pio.pbi_op = PCIBARIO_WRITE;
1100 		pio.pbi_bar = baridx;
1101 		pio.pbi_offset = (uint32_t)offset;
1102 		pio.pbi_width = size;
1103 		pio.pbi_value = (uint32_t)value;
1104 
1105 		(void)ioctl(pcifd, PCIOCBARIO, &pio);
1106 	}
1107 }
1108 
1109 static uint64_t
1110 passthru_read(struct pci_devinst *pi, int baridx, uint64_t offset, int size)
1111 {
1112 	struct passthru_softc *sc;
1113 	struct pci_bar_ioreq pio;
1114 	uint64_t val;
1115 
1116 	sc = pi->pi_arg;
1117 
1118 	if (baridx == pci_msix_table_bar(pi)) {
1119 		val = msix_table_read(sc, offset, size);
1120 	} else {
1121 		assert(pi->pi_bar[baridx].type == PCIBAR_IO);
1122 		assert(size == 1 || size == 2 || size == 4);
1123 		assert(offset <= UINT32_MAX && offset + size <= UINT32_MAX);
1124 
1125 		bzero(&pio, sizeof(pio));
1126 		pio.pbi_sel = sc->psc_sel;
1127 		pio.pbi_op = PCIBARIO_READ;
1128 		pio.pbi_bar = baridx;
1129 		pio.pbi_offset = (uint32_t)offset;
1130 		pio.pbi_width = size;
1131 
1132 		(void)ioctl(pcifd, PCIOCBARIO, &pio);
1133 
1134 		val = pio.pbi_value;
1135 	}
1136 
1137 	return (val);
1138 }
1139 
1140 static void
1141 passthru_msix_addr(struct pci_devinst *pi, int baridx, int enabled,
1142     uint64_t address)
1143 {
1144 	struct passthru_softc *sc;
1145 	size_t remaining;
1146 	uint32_t table_size, table_offset;
1147 
1148 	sc = pi->pi_arg;
1149 	table_offset = rounddown2(pi->pi_msix.table_offset, 4096);
1150 	if (table_offset > 0) {
1151 		if (!enabled) {
1152 			if (vm_unmap_pptdev_mmio(pi->pi_vmctx,
1153 						 sc->psc_sel.pc_bus,
1154 						 sc->psc_sel.pc_dev,
1155 						 sc->psc_sel.pc_func, address,
1156 						 table_offset) != 0)
1157 				warnx("pci_passthru: unmap_pptdev_mmio failed");
1158 		} else {
1159 			if (vm_map_pptdev_mmio(pi->pi_vmctx, sc->psc_sel.pc_bus,
1160 					       sc->psc_sel.pc_dev,
1161 					       sc->psc_sel.pc_func, address,
1162 					       table_offset,
1163 					       sc->psc_bar[baridx].addr) != 0)
1164 				warnx("pci_passthru: map_pptdev_mmio failed");
1165 		}
1166 	}
1167 	table_size = pi->pi_msix.table_offset - table_offset;
1168 	table_size += pi->pi_msix.table_count * MSIX_TABLE_ENTRY_SIZE;
1169 	table_size = roundup2(table_size, 4096);
1170 	remaining = pi->pi_bar[baridx].size - table_offset - table_size;
1171 	if (remaining > 0) {
1172 		address += table_offset + table_size;
1173 		if (!enabled) {
1174 			if (vm_unmap_pptdev_mmio(pi->pi_vmctx,
1175 						 sc->psc_sel.pc_bus,
1176 						 sc->psc_sel.pc_dev,
1177 						 sc->psc_sel.pc_func, address,
1178 						 remaining) != 0)
1179 				warnx("pci_passthru: unmap_pptdev_mmio failed");
1180 		} else {
1181 			if (vm_map_pptdev_mmio(pi->pi_vmctx, sc->psc_sel.pc_bus,
1182 					       sc->psc_sel.pc_dev,
1183 					       sc->psc_sel.pc_func, address,
1184 					       remaining,
1185 					       sc->psc_bar[baridx].addr +
1186 					       table_offset + table_size) != 0)
1187 				warnx("pci_passthru: map_pptdev_mmio failed");
1188 		}
1189 	}
1190 }
1191 
1192 static void
1193 passthru_mmio_addr(struct pci_devinst *pi, int baridx, int enabled,
1194     uint64_t address)
1195 {
1196 	struct passthru_softc *sc;
1197 
1198 	sc = pi->pi_arg;
1199 	if (!enabled) {
1200 		if (vm_unmap_pptdev_mmio(pi->pi_vmctx, sc->psc_sel.pc_bus,
1201 					 sc->psc_sel.pc_dev,
1202 					 sc->psc_sel.pc_func, address,
1203 					 sc->psc_bar[baridx].size) != 0)
1204 			warnx("pci_passthru: unmap_pptdev_mmio failed");
1205 	} else {
1206 		if (vm_map_pptdev_mmio(pi->pi_vmctx, sc->psc_sel.pc_bus,
1207 				       sc->psc_sel.pc_dev,
1208 				       sc->psc_sel.pc_func, address,
1209 				       sc->psc_bar[baridx].size,
1210 				       sc->psc_bar[baridx].addr) != 0)
1211 			warnx("pci_passthru: map_pptdev_mmio failed");
1212 	}
1213 }
1214 
1215 static void
1216 passthru_addr_rom(struct pci_devinst *const pi, const int idx,
1217     const int enabled)
1218 {
1219 	const uint64_t addr = pi->pi_bar[idx].addr;
1220 	const uint64_t size = pi->pi_bar[idx].size;
1221 
1222 	if (!enabled) {
1223 		if (vm_munmap_memseg(pi->pi_vmctx, addr, size) != 0) {
1224 			errx(4, "%s: munmap_memseg @ [%016lx - %016lx] failed",
1225 			    __func__, addr, addr + size);
1226 		}
1227 
1228 	} else {
1229 		if (vm_mmap_memseg(pi->pi_vmctx, addr, VM_PCIROM,
1230 			pi->pi_romoffset, size, PROT_READ | PROT_EXEC) != 0) {
1231 			errx(4, "%s: mmap_memseg @ [%016lx - %016lx]  failed",
1232 			    __func__, addr, addr + size);
1233 		}
1234 	}
1235 }
1236 
1237 static void
1238 passthru_addr(struct pci_devinst *pi, int baridx, int enabled, uint64_t address)
1239 {
1240 	switch (pi->pi_bar[baridx].type) {
1241 	case PCIBAR_IO:
1242 		/* IO BARs are emulated */
1243 		break;
1244 	case PCIBAR_ROM:
1245 		passthru_addr_rom(pi, baridx, enabled);
1246 		break;
1247 	case PCIBAR_MEM32:
1248 	case PCIBAR_MEM64:
1249 		if (baridx == pci_msix_table_bar(pi))
1250 			passthru_msix_addr(pi, baridx, enabled, address);
1251 		else
1252 			passthru_mmio_addr(pi, baridx, enabled, address);
1253 		break;
1254 	default:
1255 		errx(4, "%s: invalid BAR type %d", __func__,
1256 		    pi->pi_bar[baridx].type);
1257 	}
1258 }
1259 
1260 static const struct pci_devemu passthru = {
1261 	.pe_emu		= "passthru",
1262 	.pe_init	= passthru_init,
1263 	.pe_legacy_config = passthru_legacy_config,
1264 	.pe_cfgwrite	= passthru_cfgwrite,
1265 	.pe_cfgread	= passthru_cfgread,
1266 	.pe_barwrite 	= passthru_write,
1267 	.pe_barread    	= passthru_read,
1268 	.pe_baraddr	= passthru_addr,
1269 };
1270 PCI_EMUL_SET(passthru);
1271