]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bhyve/pci_passthru.c
IFC @ r242684
[FreeBSD/FreeBSD.git] / usr.sbin / bhyve / pci_passthru.c
1 /*-
2  * Copyright (c) 2011 NetApp, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/pciio.h>
35 #include <sys/ioctl.h>
36
37 #include <dev/io/iodev.h>
38 #include <machine/iodev.h>
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <unistd.h>
46
47 #include <machine/vmm.h>
48 #include <vmmapi.h>
49 #include "pci_emul.h"
50 #include "mem.h"
51 #include "instruction_emul.h"
52
53 #ifndef _PATH_DEVPCI
54 #define _PATH_DEVPCI    "/dev/pci"
55 #endif
56
57 #ifndef _PATH_DEVIO
58 #define _PATH_DEVIO     "/dev/io"
59 #endif
60
61 #define LEGACY_SUPPORT  1
62
63 #define MSIX_TABLE_BIR_MASK 7
64 #define MSIX_TABLE_OFFSET_MASK (~MSIX_TABLE_BIR_MASK);
65 #define MSIX_TABLE_COUNT(x) (((x) & 0x7FF) + 1)
66 #define MSIX_CAPLEN 12
67
68 static int pcifd = -1;
69 static int iofd = -1;
70
71 struct passthru_softc {
72         struct pci_devinst *psc_pi;
73         struct pcibar psc_bar[PCI_BARMAX + 1];
74         struct {
75                 int             capoff;
76                 int             msgctrl;
77                 int             emulated;
78         } psc_msi;
79         struct {
80                 int             capoff;
81         } psc_msix;
82         struct pcisel psc_sel;
83 };
84
85 static int
86 msi_caplen(int msgctrl)
87 {
88         int len;
89         
90         len = 10;               /* minimum length of msi capability */
91
92         if (msgctrl & PCIM_MSICTRL_64BIT)
93                 len += 4;
94
95 #if 0
96         /*
97          * Ignore the 'mask' and 'pending' bits in the MSI capability.
98          * We'll let the guest manipulate them directly.
99          */
100         if (msgctrl & PCIM_MSICTRL_VECTOR)
101                 len += 10;
102 #endif
103
104         return (len);
105 }
106
107 static uint32_t
108 read_config(const struct pcisel *sel, long reg, int width)
109 {
110         struct pci_io pi;
111
112         bzero(&pi, sizeof(pi));
113         pi.pi_sel = *sel;
114         pi.pi_reg = reg;
115         pi.pi_width = width;
116
117         if (ioctl(pcifd, PCIOCREAD, &pi) < 0)
118                 return (0);                             /* XXX */
119         else
120                 return (pi.pi_data);
121 }
122
123 static void
124 write_config(const struct pcisel *sel, long reg, int width, uint32_t data)
125 {
126         struct pci_io pi;
127
128         bzero(&pi, sizeof(pi));
129         pi.pi_sel = *sel;
130         pi.pi_reg = reg;
131         pi.pi_width = width;
132         pi.pi_data = data;
133
134         (void)ioctl(pcifd, PCIOCWRITE, &pi);            /* XXX */
135 }
136
137 #ifdef LEGACY_SUPPORT
138 static int
139 passthru_add_msicap(struct pci_devinst *pi, int msgnum, int nextptr)
140 {
141         int capoff, i;
142         struct msicap msicap;
143         u_char *capdata;
144
145         pci_populate_msicap(&msicap, msgnum, nextptr);
146
147         /*
148          * XXX
149          * Copy the msi capability structure in the last 16 bytes of the
150          * config space. This is wrong because it could shadow something
151          * useful to the device.
152          */
153         capoff = 256 - roundup(sizeof(msicap), 4);
154         capdata = (u_char *)&msicap;
155         for (i = 0; i < sizeof(msicap); i++)
156                 pci_set_cfgdata8(pi, capoff + i, capdata[i]);
157
158         return (capoff);
159 }
160 #endif  /* LEGACY_SUPPORT */
161
162 static int
163 cfginitmsi(struct passthru_softc *sc)
164 {
165         int ptr, capptr, cap, sts, caplen;
166         uint32_t u32;
167         struct pcisel sel;
168         struct pci_devinst *pi;
169         struct msixcap msixcap;
170         uint32_t *msixcap_ptr;
171
172         pi = sc->psc_pi;
173         sel = sc->psc_sel;
174
175         /*
176          * Parse the capabilities and cache the location of the MSI
177          * and MSI-X capabilities.
178          */
179         sts = read_config(&sel, PCIR_STATUS, 2);
180         if (sts & PCIM_STATUS_CAPPRESENT) {
181                 ptr = read_config(&sel, PCIR_CAP_PTR, 1);
182                 while (ptr != 0 && ptr != 0xff) {
183                         cap = read_config(&sel, ptr + PCICAP_ID, 1);
184                         if (cap == PCIY_MSI) {
185                                 /*
186                                  * Copy the MSI capability into the config
187                                  * space of the emulated pci device
188                                  */
189                                 sc->psc_msi.capoff = ptr;
190                                 sc->psc_msi.msgctrl = read_config(&sel,
191                                                                   ptr + 2, 2);
192                                 sc->psc_msi.emulated = 0;
193                                 caplen = msi_caplen(sc->psc_msi.msgctrl);
194                                 capptr = ptr;
195                                 while (caplen > 0) {
196                                         u32 = read_config(&sel, capptr, 4);
197                                         pci_set_cfgdata32(pi, capptr, u32);
198                                         caplen -= 4;
199                                         capptr += 4;
200                                 }
201                         } else if (cap == PCIY_MSIX) {
202                                 /*
203                                  * Copy the MSI-X capability 
204                                  */
205                                 sc->psc_msix.capoff = ptr;
206                                 caplen = 12;
207                                 msixcap_ptr = (uint32_t*) &msixcap;
208                                 capptr = ptr;
209                                 while (caplen > 0) {
210                                         u32 = read_config(&sel, capptr, 4);
211                                         *msixcap_ptr = u32;
212                                         pci_set_cfgdata32(pi, capptr, u32);
213                                         caplen -= 4;
214                                         capptr += 4;
215                                         msixcap_ptr++;
216                                 }
217                         }
218                         ptr = read_config(&sel, ptr + PCICAP_NEXTPTR, 1);
219                 }
220         }
221
222         if (sc->psc_msix.capoff != 0) {
223                 pi->pi_msix.pba_bar =
224                     msixcap.pba_offset & MSIX_TABLE_BIR_MASK;
225                 pi->pi_msix.pba_offset =
226                     msixcap.pba_offset & MSIX_TABLE_OFFSET_MASK;
227                 pi->pi_msix.table_bar =
228                     msixcap.table_offset & MSIX_TABLE_BIR_MASK;
229                 pi->pi_msix.table_offset =
230                     msixcap.table_offset & MSIX_TABLE_OFFSET_MASK;
231                 pi->pi_msix.table_count = MSIX_TABLE_COUNT(msixcap.msgctrl);
232         }
233
234 #ifdef LEGACY_SUPPORT
235         /*
236          * If the passthrough device does not support MSI then craft a
237          * MSI capability for it. We link the new MSI capability at the
238          * head of the list of capabilities.
239          */
240         if ((sts & PCIM_STATUS_CAPPRESENT) != 0 && sc->psc_msi.capoff == 0) {
241                 int origptr, msiptr;
242                 origptr = read_config(&sel, PCIR_CAP_PTR, 1);
243                 msiptr = passthru_add_msicap(pi, 1, origptr);
244                 sc->psc_msi.capoff = msiptr;
245                 sc->psc_msi.msgctrl = pci_get_cfgdata16(pi, msiptr + 2);
246                 sc->psc_msi.emulated = 1;
247                 pci_set_cfgdata8(pi, PCIR_CAP_PTR, msiptr);
248         }
249 #endif
250
251         /* Make sure one of the capabilities is present */
252         if (sc->psc_msi.capoff == 0 && sc->psc_msix.capoff == 0) 
253                 return (-1);
254         else
255                 return (0);
256 }
257
258 static uint64_t
259 msix_table_read(struct passthru_softc *sc, uint64_t offset, int size)
260 {
261         struct pci_devinst *pi;
262         struct msix_table_entry *entry;
263         uint8_t *src8;
264         uint16_t *src16;
265         uint32_t *src32;
266         uint64_t *src64;
267         uint64_t data;
268         size_t entry_offset;
269         int index;
270
271         pi = sc->psc_pi;
272         entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
273         index = offset / MSIX_TABLE_ENTRY_SIZE;
274         entry = &pi->pi_msix.table[index];
275
276         switch(size) {
277         case 1:
278                 src8 = (uint8_t *)((void *)entry + entry_offset);
279                 data = *src8;
280                 break;
281         case 2:
282                 src16 = (uint16_t *)((void *)entry + entry_offset);
283                 data = *src16;
284                 break;
285         case 4:
286                 src32 = (uint32_t *)((void *)entry + entry_offset);
287                 data = *src32;
288                 break;
289         case 8:
290                 src64 = (uint64_t *)((void *)entry + entry_offset);
291                 data = *src64;
292                 break;
293         default:
294                 return (-1);
295         }
296
297         return (data);
298 }
299
300 static void
301 msix_table_write(struct vmctx *ctx, int vcpu, struct passthru_softc *sc,
302                  uint64_t offset, int size, uint64_t data)
303 {
304         struct pci_devinst *pi;
305         struct msix_table_entry *entry;
306         uint32_t *dest;
307         size_t entry_offset;
308         uint32_t vector_control;
309         int error, index;
310
311         pi = sc->psc_pi;
312         entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
313         index = offset / MSIX_TABLE_ENTRY_SIZE;
314         entry = &pi->pi_msix.table[index];
315
316         /* Only 4 byte naturally-aligned writes are supported */
317         assert(size == 4);
318         assert(entry_offset % 4 == 0);
319
320         vector_control = entry->vector_control;
321         dest = (uint32_t *)((void *)entry + entry_offset);
322         *dest = data;
323         /* If MSI-X hasn't been enabled, do nothing */
324         if (pi->pi_msix.enabled) {
325                 /* If the entry is masked, don't set it up */
326                 if ((entry->vector_control & PCIM_MSIX_VCTRL_MASK) == 0 ||
327                     (vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
328                         error = vm_setup_msix(ctx, vcpu, sc->psc_sel.pc_bus,
329                                               sc->psc_sel.pc_dev, 
330                                               sc->psc_sel.pc_func,
331                                               index, entry->msg_data, 
332                                               entry->vector_control,
333                                               entry->addr);
334                 }
335         }
336 }
337
338 static int
339 init_msix_table(struct vmctx *ctx, struct passthru_softc *sc, uint64_t base)
340 {
341         int idx;
342         size_t table_size;
343         vm_paddr_t start;
344         size_t len;
345         struct pci_devinst *pi = sc->psc_pi;
346
347         /* 
348          * If the MSI-X table BAR maps memory intended for
349          * other uses, it is at least assured that the table 
350          * either resides in its own page within the region, 
351          * or it resides in a page shared with only the PBA.
352          */
353         if (pi->pi_msix.pba_bar == pi->pi_msix.table_bar && 
354             ((pi->pi_msix.pba_offset - pi->pi_msix.table_offset) < 4096)) {
355                 /* Need to also emulate the PBA, not supported yet */
356                 printf("Unsupported MSI-X table and PBA in same page\n");
357                 return (-1);
358         }
359
360         /* 
361          * May need to split the BAR into 3 regions:
362          * Before the MSI-X table, the MSI-X table, and after it
363          * XXX for now, assume that the table is not in the middle
364          */
365         table_size = pi->pi_msix.table_count * MSIX_TABLE_ENTRY_SIZE;
366         pi->pi_msix.table_size = table_size;
367         idx = pi->pi_msix.table_bar;
368
369         /* Round up to page size */
370         table_size = (table_size + 0x1000) & ~0xFFF;
371         if (pi->pi_msix.table_offset == 0) {            
372                 /* Map everything after the MSI-X table */
373                 start = pi->pi_bar[idx].addr + table_size;
374                 len = pi->pi_bar[idx].size - table_size;
375         } else {
376                 /* Map everything before the MSI-X table */
377                 start = pi->pi_bar[idx].addr;
378                 len = pi->pi_msix.table_offset;
379         }
380         return (vm_map_pptdev_mmio(ctx, sc->psc_sel.pc_bus, 
381                                    sc->psc_sel.pc_dev, sc->psc_sel.pc_func, 
382                                    start, len, base + table_size));
383 }
384
385 static int
386 cfginitbar(struct vmctx *ctx, struct passthru_softc *sc)
387 {
388         int i, error;
389         struct pci_devinst *pi;
390         struct pci_bar_io bar;
391         enum pcibar_type bartype;
392         uint64_t base;
393
394         pi = sc->psc_pi;
395
396         /*
397          * Initialize BAR registers
398          */
399         for (i = 0; i <= PCI_BARMAX; i++) {
400                 bzero(&bar, sizeof(bar));
401                 bar.pbi_sel = sc->psc_sel;
402                 bar.pbi_reg = PCIR_BAR(i);
403
404                 if (ioctl(pcifd, PCIOCGETBAR, &bar) < 0)
405                         continue;
406
407                 if (PCI_BAR_IO(bar.pbi_base)) {
408                         bartype = PCIBAR_IO;
409                         base = bar.pbi_base & PCIM_BAR_IO_BASE;
410                 } else {
411                         switch (bar.pbi_base & PCIM_BAR_MEM_TYPE) {
412                         case PCIM_BAR_MEM_64:
413                                 bartype = PCIBAR_MEM64;
414                                 break;
415                         default:
416                                 bartype = PCIBAR_MEM32;
417                                 break;
418                         }
419                         base = bar.pbi_base & PCIM_BAR_MEM_BASE;
420                 }
421
422                 /* Cache information about the "real" BAR */
423                 sc->psc_bar[i].type = bartype;
424                 sc->psc_bar[i].size = bar.pbi_length;
425                 sc->psc_bar[i].addr = base;
426
427                 /* Allocate the BAR in the guest I/O or MMIO space */
428                 error = pci_emul_alloc_pbar(pi, i, base, bartype,
429                                             bar.pbi_length);
430                 if (error)
431                         return (-1);
432
433                 /* The MSI-X table needs special handling */
434                 if (i == pi->pi_msix.table_bar) {
435                         error = init_msix_table(ctx, sc, base);
436                         if (error) 
437                                 return (-1);
438                 } else if (bartype != PCIBAR_IO) {
439                         /* Map the physical MMIO space in the guest MMIO space */
440                         error = vm_map_pptdev_mmio(ctx, sc->psc_sel.pc_bus,
441                                 sc->psc_sel.pc_dev, sc->psc_sel.pc_func,
442                                 pi->pi_bar[i].addr, pi->pi_bar[i].size, base);
443                         if (error)
444                                 return (-1);
445                 }
446
447                 /*
448                  * 64-bit BAR takes up two slots so skip the next one.
449                  */
450                 if (bartype == PCIBAR_MEM64) {
451                         i++;
452                         assert(i <= PCI_BARMAX);
453                         sc->psc_bar[i].type = PCIBAR_MEMHI64;
454                 }
455         }
456         return (0);
457 }
458
459 static int
460 cfginit(struct vmctx *ctx, struct pci_devinst *pi, int bus, int slot, int func)
461 {
462         int error;
463         struct passthru_softc *sc;
464
465         error = 1;
466         sc = pi->pi_arg;
467
468         bzero(&sc->psc_sel, sizeof(struct pcisel));
469         sc->psc_sel.pc_bus = bus;
470         sc->psc_sel.pc_dev = slot;
471         sc->psc_sel.pc_func = func;
472
473         if (cfginitmsi(sc) != 0)
474                 goto done;
475
476         if (cfginitbar(ctx, sc) != 0)
477                 goto done;
478
479         error = 0;                              /* success */
480 done:
481         return (error);
482 }
483
484 static int
485 passthru_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
486 {
487         int bus, slot, func, error;
488         struct passthru_softc *sc;
489
490         sc = NULL;
491         error = 1;
492
493         if (pcifd < 0) {
494                 pcifd = open(_PATH_DEVPCI, O_RDWR, 0);
495                 if (pcifd < 0)
496                         goto done;
497         }
498
499         if (iofd < 0) {
500                 iofd = open(_PATH_DEVIO, O_RDWR, 0);
501                 if (iofd < 0)
502                         goto done;
503         }
504
505         if (opts == NULL ||
506             sscanf(opts, "%d/%d/%d", &bus, &slot, &func) != 3)
507                 goto done;
508
509         if (vm_assign_pptdev(ctx, bus, slot, func) != 0)
510                 goto done;
511
512         sc = malloc(sizeof(struct passthru_softc));
513         memset(sc, 0, sizeof(struct passthru_softc));
514
515         pi->pi_arg = sc;
516         sc->psc_pi = pi;
517
518         /* initialize config space */
519         if ((error = cfginit(ctx, pi, bus, slot, func)) != 0)
520                 goto done;
521         
522         error = 0;              /* success */
523 done:
524         if (error) {
525                 free(sc);
526                 vm_unassign_pptdev(ctx, bus, slot, func);
527         }
528         return (error);
529 }
530
531 static int
532 bar_access(int coff)
533 {
534         if (coff >= PCIR_BAR(0) && coff < PCIR_BAR(PCI_BARMAX + 1))
535                 return (1);
536         else
537                 return (0);
538 }
539
540 static int
541 msicap_access(struct passthru_softc *sc, int coff)
542 {
543         int caplen;
544
545         if (sc->psc_msi.capoff == 0)
546                 return (0);
547
548         caplen = msi_caplen(sc->psc_msi.msgctrl);
549
550         if (coff >= sc->psc_msi.capoff && coff < sc->psc_msi.capoff + caplen)
551                 return (1);
552         else
553                 return (0);
554 }
555
556 static int 
557 msixcap_access(struct passthru_softc *sc, int coff)
558 {
559         if (sc->psc_msix.capoff == 0) 
560                 return (0);
561
562         return (coff >= sc->psc_msix.capoff && 
563                 coff < sc->psc_msix.capoff + MSIX_CAPLEN);
564 }
565
566 static int
567 passthru_cfgread(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
568                  int coff, int bytes, uint32_t *rv)
569 {
570         struct passthru_softc *sc;
571
572         sc = pi->pi_arg;
573
574         /*
575          * PCI BARs and MSI capability is emulated.
576          */
577         if (bar_access(coff) || msicap_access(sc, coff))
578                 return (-1);
579
580 #ifdef LEGACY_SUPPORT
581         /*
582          * Emulate PCIR_CAP_PTR if this device does not support MSI capability
583          * natively.
584          */
585         if (sc->psc_msi.emulated) {
586                 if (coff >= PCIR_CAP_PTR && coff < PCIR_CAP_PTR + 4)
587                         return (-1);
588         }
589 #endif
590
591         /* Everything else just read from the device's config space */
592         *rv = read_config(&sc->psc_sel, coff, bytes);
593
594         return (0);
595 }
596
597 static int
598 passthru_cfgwrite(struct vmctx *ctx, int vcpu, struct pci_devinst *pi,
599                   int coff, int bytes, uint32_t val)
600 {
601         int error, msix_table_entries, i;
602         struct passthru_softc *sc;
603
604         sc = pi->pi_arg;
605
606         /*
607          * PCI BARs are emulated
608          */
609         if (bar_access(coff))
610                 return (-1);
611
612         /*
613          * MSI capability is emulated
614          */
615         if (msicap_access(sc, coff)) {
616                 msicap_cfgwrite(pi, sc->psc_msi.capoff, coff, bytes, val);
617
618                 error = vm_setup_msi(ctx, vcpu, sc->psc_sel.pc_bus,
619                         sc->psc_sel.pc_dev, sc->psc_sel.pc_func, pi->pi_msi.cpu,
620                         pi->pi_msi.vector, pi->pi_msi.msgnum);
621                 if (error != 0) {
622                         printf("vm_setup_msi returned error %d\r\n", errno);
623                         exit(1);
624                 }
625                 return (0);
626         }
627
628         if (msixcap_access(sc, coff)) {
629                 msixcap_cfgwrite(pi, sc->psc_msix.capoff, coff, bytes, val);
630                 if (pi->pi_msix.enabled) {
631                         msix_table_entries = pi->pi_msix.table_count;
632                         for (i = 0; i < msix_table_entries; i++) {
633                                 error = vm_setup_msix(ctx, vcpu, sc->psc_sel.pc_bus,
634                                                       sc->psc_sel.pc_dev, 
635                                                       sc->psc_sel.pc_func, i, 
636                                                       pi->pi_msix.table[i].msg_data,
637                                                       pi->pi_msix.table[i].vector_control,
638                                                       pi->pi_msix.table[i].addr);
639                 
640                                 if (error) {
641                                         printf("vm_setup_msix returned error %d\r\n", errno);
642                                         exit(1);        
643                                 }
644                         }
645                 }
646                 return (0);
647         }
648
649 #ifdef LEGACY_SUPPORT
650         /*
651          * If this device does not support MSI natively then we cannot let
652          * the guest disable legacy interrupts from the device. It is the
653          * legacy interrupt that is triggering the virtual MSI to the guest.
654          */
655         if (sc->psc_msi.emulated && pci_msi_enabled(pi)) {
656                 if (coff == PCIR_COMMAND && bytes == 2)
657                         val &= ~PCIM_CMD_INTxDIS;
658         }
659 #endif
660
661         write_config(&sc->psc_sel, coff, bytes, val);
662
663         return (0);
664 }
665
666 static void
667 passthru_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
668                uint64_t offset, int size, uint64_t value)
669 {
670         struct passthru_softc *sc;
671         struct iodev_pio_req pio;
672
673         sc = pi->pi_arg;
674
675         if (pi->pi_msix.enabled && pi->pi_msix.table_bar == baridx) {
676                 msix_table_write(ctx, vcpu, sc, offset, size, value);
677         } else {
678                 assert(pi->pi_bar[baridx].type == PCIBAR_IO);
679                 bzero(&pio, sizeof(struct iodev_pio_req));
680                 pio.access = IODEV_PIO_WRITE;
681                 pio.port = sc->psc_bar[baridx].addr + offset;
682                 pio.width = size;
683                 pio.val = value;
684                 
685                 (void)ioctl(iofd, IODEV_PIO, &pio);
686         }
687 }
688
689 static uint64_t
690 passthru_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx,
691               uint64_t offset, int size)
692 {
693         struct passthru_softc *sc;
694         struct iodev_pio_req pio;
695         uint64_t val;
696
697         sc = pi->pi_arg;
698
699         if (pi->pi_msix.enabled && pi->pi_msix.table_bar == baridx) {
700                 val = msix_table_read(sc, offset, size);
701         } else {
702                 assert(pi->pi_bar[baridx].type == PCIBAR_IO);
703                 bzero(&pio, sizeof(struct iodev_pio_req));
704                 pio.access = IODEV_PIO_READ;
705                 pio.port = sc->psc_bar[baridx].addr + offset;
706                 pio.width = size;
707                 pio.val = 0;
708
709                 (void)ioctl(iofd, IODEV_PIO, &pio);
710
711                 val = pio.val;
712         }
713
714         return (val);
715 }
716
717 struct pci_devemu passthru = {
718         .pe_emu         = "passthru",
719         .pe_init        = passthru_init,
720         .pe_cfgwrite    = passthru_cfgwrite,
721         .pe_cfgread     = passthru_cfgread,
722         .pe_barwrite    = passthru_write,
723         .pe_barread     = passthru_read,
724 };
725 PCI_EMUL_SET(passthru);