1366f6083SPeter Grehan /*- 21de7b4b8SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 31de7b4b8SPedro F. Giffuni * 4366f6083SPeter Grehan * Copyright (c) 2011 NetApp, Inc. 5366f6083SPeter Grehan * All rights reserved. 6366f6083SPeter Grehan * 7366f6083SPeter Grehan * Redistribution and use in source and binary forms, with or without 8366f6083SPeter Grehan * modification, are permitted provided that the following conditions 9366f6083SPeter Grehan * are met: 10366f6083SPeter Grehan * 1. Redistributions of source code must retain the above copyright 11366f6083SPeter Grehan * notice, this list of conditions and the following disclaimer. 12366f6083SPeter Grehan * 2. Redistributions in binary form must reproduce the above copyright 13366f6083SPeter Grehan * notice, this list of conditions and the following disclaimer in the 14366f6083SPeter Grehan * documentation and/or other materials provided with the distribution. 15366f6083SPeter Grehan * 16366f6083SPeter Grehan * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 17366f6083SPeter Grehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18366f6083SPeter Grehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19366f6083SPeter Grehan * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 20366f6083SPeter Grehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21366f6083SPeter Grehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22366f6083SPeter Grehan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23366f6083SPeter Grehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24366f6083SPeter Grehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25366f6083SPeter Grehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26366f6083SPeter Grehan * SUCH DAMAGE. 27366f6083SPeter Grehan * 28366f6083SPeter Grehan * $FreeBSD$ 29366f6083SPeter Grehan */ 30366f6083SPeter Grehan 31366f6083SPeter Grehan #include <sys/cdefs.h> 32366f6083SPeter Grehan __FBSDID("$FreeBSD$"); 33366f6083SPeter Grehan 34366f6083SPeter Grehan #include <sys/param.h> 3500ef17beSBartek Rutkowski #ifndef WITHOUT_CAPSICUM 3600ef17beSBartek Rutkowski #include <sys/capsicum.h> 3700ef17beSBartek Rutkowski #endif 38366f6083SPeter Grehan #include <sys/types.h> 395c40acf8SJohn Baldwin #include <sys/mman.h> 40366f6083SPeter Grehan #include <sys/pciio.h> 41366f6083SPeter Grehan #include <sys/ioctl.h> 42e47fe318SCorvin Köhne #include <sys/stat.h> 43366f6083SPeter Grehan 44366f6083SPeter Grehan #include <dev/io/iodev.h> 452e81a7e8SNeel Natu #include <dev/pci/pcireg.h> 462e81a7e8SNeel Natu 477fa23353SMark Johnston #include <vm/vm.h> 487fa23353SMark Johnston 49366f6083SPeter Grehan #include <machine/iodev.h> 507fa23353SMark Johnston #include <machine/vm.h> 51366f6083SPeter Grehan 52abfa3c39SMarcelo Araujo #ifndef WITHOUT_CAPSICUM 53abfa3c39SMarcelo Araujo #include <capsicum_helpers.h> 54abfa3c39SMarcelo Araujo #endif 55366f6083SPeter Grehan #include <stdio.h> 56366f6083SPeter Grehan #include <stdlib.h> 57366f6083SPeter Grehan #include <string.h> 58cff92ffdSJohn Baldwin #include <err.h> 5900ef17beSBartek Rutkowski #include <errno.h> 60366f6083SPeter Grehan #include <fcntl.h> 6100ef17beSBartek Rutkowski #include <sysexits.h> 62366f6083SPeter Grehan #include <unistd.h> 63366f6083SPeter Grehan 64366f6083SPeter Grehan #include <machine/vmm.h> 65621b5090SJohn Baldwin 66621b5090SJohn Baldwin #include "config.h" 67621b5090SJohn Baldwin #include "debug.h" 684d1e669cSPeter Grehan #include "mem.h" 69563fd224SCorvin Köhne #include "pci_passthru.h" 70366f6083SPeter Grehan 71366f6083SPeter Grehan #ifndef _PATH_DEVPCI 72366f6083SPeter Grehan #define _PATH_DEVPCI "/dev/pci" 73366f6083SPeter Grehan #endif 74366f6083SPeter Grehan 75366f6083SPeter Grehan #define LEGACY_SUPPORT 1 76366f6083SPeter Grehan 772e81a7e8SNeel Natu #define MSIX_TABLE_COUNT(ctrl) (((ctrl) & PCIM_MSIXCTRL_TABLE_SIZE) + 1) 78cd942e0fSPeter Grehan #define MSIX_CAPLEN 12 79cd942e0fSPeter Grehan 80366f6083SPeter Grehan static int pcifd = -1; 81366f6083SPeter Grehan 82366f6083SPeter Grehan struct passthru_softc { 83366f6083SPeter Grehan struct pci_devinst *psc_pi; 84e47fe318SCorvin Köhne /* ROM is handled like a BAR */ 85e47fe318SCorvin Köhne struct pcibar psc_bar[PCI_BARMAX_WITH_ROM + 1]; 86366f6083SPeter Grehan struct { 87366f6083SPeter Grehan int capoff; 88366f6083SPeter Grehan int msgctrl; 89366f6083SPeter Grehan int emulated; 90366f6083SPeter Grehan } psc_msi; 91cd942e0fSPeter Grehan struct { 92cd942e0fSPeter Grehan int capoff; 93cd942e0fSPeter Grehan } psc_msix; 94366f6083SPeter Grehan struct pcisel psc_sel; 95366f6083SPeter Grehan }; 96366f6083SPeter Grehan 97366f6083SPeter Grehan static int 98366f6083SPeter Grehan msi_caplen(int msgctrl) 99366f6083SPeter Grehan { 100366f6083SPeter Grehan int len; 101366f6083SPeter Grehan 102366f6083SPeter Grehan len = 10; /* minimum length of msi capability */ 103366f6083SPeter Grehan 104366f6083SPeter Grehan if (msgctrl & PCIM_MSICTRL_64BIT) 105366f6083SPeter Grehan len += 4; 106366f6083SPeter Grehan 107366f6083SPeter Grehan #if 0 108366f6083SPeter Grehan /* 109366f6083SPeter Grehan * Ignore the 'mask' and 'pending' bits in the MSI capability. 110366f6083SPeter Grehan * We'll let the guest manipulate them directly. 111366f6083SPeter Grehan */ 112366f6083SPeter Grehan if (msgctrl & PCIM_MSICTRL_VECTOR) 113366f6083SPeter Grehan len += 10; 114366f6083SPeter Grehan #endif 115366f6083SPeter Grehan 116366f6083SPeter Grehan return (len); 117366f6083SPeter Grehan } 118366f6083SPeter Grehan 119563fd224SCorvin Köhne static int 120563fd224SCorvin Köhne pcifd_init() { 121563fd224SCorvin Köhne pcifd = open(_PATH_DEVPCI, O_RDWR, 0); 122563fd224SCorvin Köhne if (pcifd < 0) { 123563fd224SCorvin Köhne warn("failed to open %s", _PATH_DEVPCI); 124563fd224SCorvin Köhne return (1); 125563fd224SCorvin Köhne } 126563fd224SCorvin Köhne 127563fd224SCorvin Köhne #ifndef WITHOUT_CAPSICUM 128563fd224SCorvin Köhne cap_rights_t pcifd_rights; 129563fd224SCorvin Köhne cap_rights_init(&pcifd_rights, CAP_IOCTL, CAP_READ, CAP_WRITE); 130563fd224SCorvin Köhne if (caph_rights_limit(pcifd, &pcifd_rights) == -1) 131563fd224SCorvin Köhne errx(EX_OSERR, "Unable to apply rights for sandbox"); 132563fd224SCorvin Köhne 133563fd224SCorvin Köhne const cap_ioctl_t pcifd_ioctls[] = { PCIOCREAD, PCIOCWRITE, PCIOCGETBAR, 134563fd224SCorvin Köhne PCIOCBARIO, PCIOCBARMMAP }; 135563fd224SCorvin Köhne if (caph_ioctls_limit(pcifd, pcifd_ioctls, nitems(pcifd_ioctls)) == -1) 136563fd224SCorvin Köhne errx(EX_OSERR, "Unable to apply rights for sandbox"); 137563fd224SCorvin Köhne #endif 138563fd224SCorvin Köhne 139563fd224SCorvin Köhne return (0); 140563fd224SCorvin Köhne } 141563fd224SCorvin Köhne 142563fd224SCorvin Köhne uint32_t 143366f6083SPeter Grehan read_config(const struct pcisel *sel, long reg, int width) 144366f6083SPeter Grehan { 145563fd224SCorvin Köhne if (pcifd < 0 && pcifd_init()) { 146563fd224SCorvin Köhne return (0); 147563fd224SCorvin Köhne } 148563fd224SCorvin Köhne 149366f6083SPeter Grehan struct pci_io pi; 150366f6083SPeter Grehan 151366f6083SPeter Grehan bzero(&pi, sizeof(pi)); 152366f6083SPeter Grehan pi.pi_sel = *sel; 153366f6083SPeter Grehan pi.pi_reg = reg; 154366f6083SPeter Grehan pi.pi_width = width; 155366f6083SPeter Grehan 156366f6083SPeter Grehan if (ioctl(pcifd, PCIOCREAD, &pi) < 0) 157366f6083SPeter Grehan return (0); /* XXX */ 158366f6083SPeter Grehan else 159366f6083SPeter Grehan return (pi.pi_data); 160366f6083SPeter Grehan } 161366f6083SPeter Grehan 162563fd224SCorvin Köhne void 163366f6083SPeter Grehan write_config(const struct pcisel *sel, long reg, int width, uint32_t data) 164366f6083SPeter Grehan { 165563fd224SCorvin Köhne if (pcifd < 0 && pcifd_init()) { 166563fd224SCorvin Köhne return; 167563fd224SCorvin Köhne } 168563fd224SCorvin Köhne 169366f6083SPeter Grehan struct pci_io pi; 170366f6083SPeter Grehan 171366f6083SPeter Grehan bzero(&pi, sizeof(pi)); 172366f6083SPeter Grehan pi.pi_sel = *sel; 173366f6083SPeter Grehan pi.pi_reg = reg; 174366f6083SPeter Grehan pi.pi_width = width; 175366f6083SPeter Grehan pi.pi_data = data; 176366f6083SPeter Grehan 177366f6083SPeter Grehan (void)ioctl(pcifd, PCIOCWRITE, &pi); /* XXX */ 178366f6083SPeter Grehan } 179366f6083SPeter Grehan 180366f6083SPeter Grehan #ifdef LEGACY_SUPPORT 181366f6083SPeter Grehan static int 182366f6083SPeter Grehan passthru_add_msicap(struct pci_devinst *pi, int msgnum, int nextptr) 183366f6083SPeter Grehan { 184366f6083SPeter Grehan int capoff, i; 185366f6083SPeter Grehan struct msicap msicap; 186366f6083SPeter Grehan u_char *capdata; 187366f6083SPeter Grehan 188366f6083SPeter Grehan pci_populate_msicap(&msicap, msgnum, nextptr); 189366f6083SPeter Grehan 190366f6083SPeter Grehan /* 191366f6083SPeter Grehan * XXX 192366f6083SPeter Grehan * Copy the msi capability structure in the last 16 bytes of the 193366f6083SPeter Grehan * config space. This is wrong because it could shadow something 194366f6083SPeter Grehan * useful to the device. 195366f6083SPeter Grehan */ 196366f6083SPeter Grehan capoff = 256 - roundup(sizeof(msicap), 4); 197366f6083SPeter Grehan capdata = (u_char *)&msicap; 198366f6083SPeter Grehan for (i = 0; i < sizeof(msicap); i++) 199366f6083SPeter Grehan pci_set_cfgdata8(pi, capoff + i, capdata[i]); 200366f6083SPeter Grehan 201366f6083SPeter Grehan return (capoff); 202366f6083SPeter Grehan } 203366f6083SPeter Grehan #endif /* LEGACY_SUPPORT */ 204366f6083SPeter Grehan 205366f6083SPeter Grehan static int 206366f6083SPeter Grehan cfginitmsi(struct passthru_softc *sc) 207366f6083SPeter Grehan { 2082e81a7e8SNeel Natu int i, ptr, capptr, cap, sts, caplen, table_size; 209366f6083SPeter Grehan uint32_t u32; 210366f6083SPeter Grehan struct pcisel sel; 211366f6083SPeter Grehan struct pci_devinst *pi; 212cd942e0fSPeter Grehan struct msixcap msixcap; 213cd942e0fSPeter Grehan uint32_t *msixcap_ptr; 214366f6083SPeter Grehan 215366f6083SPeter Grehan pi = sc->psc_pi; 216366f6083SPeter Grehan sel = sc->psc_sel; 217366f6083SPeter Grehan 218366f6083SPeter Grehan /* 219366f6083SPeter Grehan * Parse the capabilities and cache the location of the MSI 220cd942e0fSPeter Grehan * and MSI-X capabilities. 221366f6083SPeter Grehan */ 222366f6083SPeter Grehan sts = read_config(&sel, PCIR_STATUS, 2); 223366f6083SPeter Grehan if (sts & PCIM_STATUS_CAPPRESENT) { 224366f6083SPeter Grehan ptr = read_config(&sel, PCIR_CAP_PTR, 1); 225366f6083SPeter Grehan while (ptr != 0 && ptr != 0xff) { 226366f6083SPeter Grehan cap = read_config(&sel, ptr + PCICAP_ID, 1); 227366f6083SPeter Grehan if (cap == PCIY_MSI) { 228366f6083SPeter Grehan /* 229366f6083SPeter Grehan * Copy the MSI capability into the config 230366f6083SPeter Grehan * space of the emulated pci device 231366f6083SPeter Grehan */ 232366f6083SPeter Grehan sc->psc_msi.capoff = ptr; 233366f6083SPeter Grehan sc->psc_msi.msgctrl = read_config(&sel, 234366f6083SPeter Grehan ptr + 2, 2); 235366f6083SPeter Grehan sc->psc_msi.emulated = 0; 236366f6083SPeter Grehan caplen = msi_caplen(sc->psc_msi.msgctrl); 237cd942e0fSPeter Grehan capptr = ptr; 238366f6083SPeter Grehan while (caplen > 0) { 239cd942e0fSPeter Grehan u32 = read_config(&sel, capptr, 4); 240cd942e0fSPeter Grehan pci_set_cfgdata32(pi, capptr, u32); 241366f6083SPeter Grehan caplen -= 4; 242cd942e0fSPeter Grehan capptr += 4; 243366f6083SPeter Grehan } 244cd942e0fSPeter Grehan } else if (cap == PCIY_MSIX) { 245cd942e0fSPeter Grehan /* 246cd942e0fSPeter Grehan * Copy the MSI-X capability 247cd942e0fSPeter Grehan */ 248cd942e0fSPeter Grehan sc->psc_msix.capoff = ptr; 249cd942e0fSPeter Grehan caplen = 12; 250cd942e0fSPeter Grehan msixcap_ptr = (uint32_t*) &msixcap; 251cd942e0fSPeter Grehan capptr = ptr; 252cd942e0fSPeter Grehan while (caplen > 0) { 253cd942e0fSPeter Grehan u32 = read_config(&sel, capptr, 4); 254cd942e0fSPeter Grehan *msixcap_ptr = u32; 255cd942e0fSPeter Grehan pci_set_cfgdata32(pi, capptr, u32); 256cd942e0fSPeter Grehan caplen -= 4; 257cd942e0fSPeter Grehan capptr += 4; 258cd942e0fSPeter Grehan msixcap_ptr++; 259cd942e0fSPeter Grehan } 260366f6083SPeter Grehan } 261366f6083SPeter Grehan ptr = read_config(&sel, ptr + PCICAP_NEXTPTR, 1); 262366f6083SPeter Grehan } 263366f6083SPeter Grehan } 264366f6083SPeter Grehan 2654d1e669cSPeter Grehan if (sc->psc_msix.capoff != 0) { 2664d1e669cSPeter Grehan pi->pi_msix.pba_bar = 2672e81a7e8SNeel Natu msixcap.pba_info & PCIM_MSIX_BIR_MASK; 2684d1e669cSPeter Grehan pi->pi_msix.pba_offset = 2692e81a7e8SNeel Natu msixcap.pba_info & ~PCIM_MSIX_BIR_MASK; 2704d1e669cSPeter Grehan pi->pi_msix.table_bar = 2712e81a7e8SNeel Natu msixcap.table_info & PCIM_MSIX_BIR_MASK; 2724d1e669cSPeter Grehan pi->pi_msix.table_offset = 2732e81a7e8SNeel Natu msixcap.table_info & ~PCIM_MSIX_BIR_MASK; 274cd942e0fSPeter Grehan pi->pi_msix.table_count = MSIX_TABLE_COUNT(msixcap.msgctrl); 2757a902ec0SNeel Natu pi->pi_msix.pba_size = PBA_SIZE(pi->pi_msix.table_count); 2762e81a7e8SNeel Natu 2772e81a7e8SNeel Natu /* Allocate the emulated MSI-X table array */ 2782e81a7e8SNeel Natu table_size = pi->pi_msix.table_count * MSIX_TABLE_ENTRY_SIZE; 279994f858aSXin LI pi->pi_msix.table = calloc(1, table_size); 2802e81a7e8SNeel Natu 2812e81a7e8SNeel Natu /* Mask all table entries */ 2822e81a7e8SNeel Natu for (i = 0; i < pi->pi_msix.table_count; i++) { 2832e81a7e8SNeel Natu pi->pi_msix.table[i].vector_control |= 2842e81a7e8SNeel Natu PCIM_MSIX_VCTRL_MASK; 2852e81a7e8SNeel Natu } 2864d1e669cSPeter Grehan } 287cd942e0fSPeter Grehan 288366f6083SPeter Grehan #ifdef LEGACY_SUPPORT 289366f6083SPeter Grehan /* 290366f6083SPeter Grehan * If the passthrough device does not support MSI then craft a 291366f6083SPeter Grehan * MSI capability for it. We link the new MSI capability at the 292366f6083SPeter Grehan * head of the list of capabilities. 293366f6083SPeter Grehan */ 294366f6083SPeter Grehan if ((sts & PCIM_STATUS_CAPPRESENT) != 0 && sc->psc_msi.capoff == 0) { 295366f6083SPeter Grehan int origptr, msiptr; 296366f6083SPeter Grehan origptr = read_config(&sel, PCIR_CAP_PTR, 1); 297366f6083SPeter Grehan msiptr = passthru_add_msicap(pi, 1, origptr); 298366f6083SPeter Grehan sc->psc_msi.capoff = msiptr; 299366f6083SPeter Grehan sc->psc_msi.msgctrl = pci_get_cfgdata16(pi, msiptr + 2); 300366f6083SPeter Grehan sc->psc_msi.emulated = 1; 301366f6083SPeter Grehan pci_set_cfgdata8(pi, PCIR_CAP_PTR, msiptr); 302366f6083SPeter Grehan } 303366f6083SPeter Grehan #endif 304366f6083SPeter Grehan 305cd942e0fSPeter Grehan /* Make sure one of the capabilities is present */ 306cd942e0fSPeter Grehan if (sc->psc_msi.capoff == 0 && sc->psc_msix.capoff == 0) 307366f6083SPeter Grehan return (-1); 308366f6083SPeter Grehan else 309366f6083SPeter Grehan return (0); 310366f6083SPeter Grehan } 311366f6083SPeter Grehan 3124d1e669cSPeter Grehan static uint64_t 3134d1e669cSPeter Grehan msix_table_read(struct passthru_softc *sc, uint64_t offset, int size) 314cd942e0fSPeter Grehan { 315cd942e0fSPeter Grehan struct pci_devinst *pi; 3164d1e669cSPeter Grehan struct msix_table_entry *entry; 317cd942e0fSPeter Grehan uint8_t *src8; 318cd942e0fSPeter Grehan uint16_t *src16; 319cd942e0fSPeter Grehan uint32_t *src32; 320cd942e0fSPeter Grehan uint64_t *src64; 3214d1e669cSPeter Grehan uint64_t data; 3224d1e669cSPeter Grehan size_t entry_offset; 3237fa23353SMark Johnston uint32_t table_offset; 3247fa23353SMark Johnston int index, table_count; 325cd942e0fSPeter Grehan 326cd942e0fSPeter Grehan pi = sc->psc_pi; 3277fa23353SMark Johnston 3287fa23353SMark Johnston table_offset = pi->pi_msix.table_offset; 3297fa23353SMark Johnston table_count = pi->pi_msix.table_count; 3307fa23353SMark Johnston if (offset < table_offset || 3317fa23353SMark Johnston offset >= table_offset + table_count * MSIX_TABLE_ENTRY_SIZE) { 3325c40acf8SJohn Baldwin switch (size) { 3335c40acf8SJohn Baldwin case 1: 3347fa23353SMark Johnston src8 = (uint8_t *)(pi->pi_msix.mapped_addr + offset); 3355c40acf8SJohn Baldwin data = *src8; 3365c40acf8SJohn Baldwin break; 3375c40acf8SJohn Baldwin case 2: 3387fa23353SMark Johnston src16 = (uint16_t *)(pi->pi_msix.mapped_addr + offset); 3395c40acf8SJohn Baldwin data = *src16; 3405c40acf8SJohn Baldwin break; 3415c40acf8SJohn Baldwin case 4: 3427fa23353SMark Johnston src32 = (uint32_t *)(pi->pi_msix.mapped_addr + offset); 3435c40acf8SJohn Baldwin data = *src32; 3445c40acf8SJohn Baldwin break; 3455c40acf8SJohn Baldwin case 8: 3467fa23353SMark Johnston src64 = (uint64_t *)(pi->pi_msix.mapped_addr + offset); 3475c40acf8SJohn Baldwin data = *src64; 3485c40acf8SJohn Baldwin break; 3495c40acf8SJohn Baldwin default: 3505c40acf8SJohn Baldwin return (-1); 3515c40acf8SJohn Baldwin } 3525c40acf8SJohn Baldwin return (data); 3535c40acf8SJohn Baldwin } 3545c40acf8SJohn Baldwin 3557fa23353SMark Johnston offset -= table_offset; 356cd942e0fSPeter Grehan index = offset / MSIX_TABLE_ENTRY_SIZE; 3577fa23353SMark Johnston assert(index < table_count); 3582e81a7e8SNeel Natu 359cd942e0fSPeter Grehan entry = &pi->pi_msix.table[index]; 3602e81a7e8SNeel Natu entry_offset = offset % MSIX_TABLE_ENTRY_SIZE; 361cd942e0fSPeter Grehan 362cd942e0fSPeter Grehan switch (size) { 363cd942e0fSPeter Grehan case 1: 3647fa23353SMark Johnston src8 = (uint8_t *)((uint8_t *)entry + entry_offset); 3654d1e669cSPeter Grehan data = *src8; 366cd942e0fSPeter Grehan break; 367cd942e0fSPeter Grehan case 2: 3687fa23353SMark Johnston src16 = (uint16_t *)((uint8_t *)entry + entry_offset); 3694d1e669cSPeter Grehan data = *src16; 370cd942e0fSPeter Grehan break; 371cd942e0fSPeter Grehan case 4: 3727fa23353SMark Johnston src32 = (uint32_t *)((uint8_t *)entry + entry_offset); 3734d1e669cSPeter Grehan data = *src32; 374cd942e0fSPeter Grehan break; 375cd942e0fSPeter Grehan case 8: 3767fa23353SMark Johnston src64 = (uint64_t *)((uint8_t *)entry + entry_offset); 3774d1e669cSPeter Grehan data = *src64; 378cd942e0fSPeter Grehan break; 379cd942e0fSPeter Grehan default: 380cd942e0fSPeter Grehan return (-1); 381cd942e0fSPeter Grehan } 382cd942e0fSPeter Grehan 3834d1e669cSPeter Grehan return (data); 384cd942e0fSPeter Grehan } 385cd942e0fSPeter Grehan 3864d1e669cSPeter Grehan static void 3874d1e669cSPeter Grehan msix_table_write(struct vmctx *ctx, int vcpu, struct passthru_softc *sc, 3884d1e669cSPeter Grehan uint64_t offset, int size, uint64_t data) 389cd942e0fSPeter Grehan { 390cd942e0fSPeter Grehan struct pci_devinst *pi; 391cd942e0fSPeter Grehan struct msix_table_entry *entry; 3925c40acf8SJohn Baldwin uint8_t *dest8; 3935c40acf8SJohn Baldwin uint16_t *dest16; 3945c40acf8SJohn Baldwin uint32_t *dest32; 3955c40acf8SJohn Baldwin uint64_t *dest64; 3964d1e669cSPeter Grehan size_t entry_offset; 3977fa23353SMark Johnston uint32_t table_offset, vector_control; 3987fa23353SMark Johnston int index, table_count; 399cd942e0fSPeter Grehan 400cd942e0fSPeter Grehan pi = sc->psc_pi; 4017fa23353SMark Johnston 4027fa23353SMark Johnston table_offset = pi->pi_msix.table_offset; 4037fa23353SMark Johnston table_count = pi->pi_msix.table_count; 4047fa23353SMark Johnston if (offset < table_offset || 4057fa23353SMark Johnston offset >= table_offset + table_count * MSIX_TABLE_ENTRY_SIZE) { 4065c40acf8SJohn Baldwin switch (size) { 4075c40acf8SJohn Baldwin case 1: 4087fa23353SMark Johnston dest8 = (uint8_t *)(pi->pi_msix.mapped_addr + offset); 4095c40acf8SJohn Baldwin *dest8 = data; 4105c40acf8SJohn Baldwin break; 4115c40acf8SJohn Baldwin case 2: 4127fa23353SMark Johnston dest16 = (uint16_t *)(pi->pi_msix.mapped_addr + offset); 4135c40acf8SJohn Baldwin *dest16 = data; 4145c40acf8SJohn Baldwin break; 4155c40acf8SJohn Baldwin case 4: 4167fa23353SMark Johnston dest32 = (uint32_t *)(pi->pi_msix.mapped_addr + offset); 4175c40acf8SJohn Baldwin *dest32 = data; 4185c40acf8SJohn Baldwin break; 4195c40acf8SJohn Baldwin case 8: 4207fa23353SMark Johnston dest64 = (uint64_t *)(pi->pi_msix.mapped_addr + offset); 4215c40acf8SJohn Baldwin *dest64 = data; 4225c40acf8SJohn Baldwin break; 4235c40acf8SJohn Baldwin } 4245c40acf8SJohn Baldwin return; 4255c40acf8SJohn Baldwin } 4265c40acf8SJohn Baldwin 4277fa23353SMark Johnston offset -= table_offset; 428cd942e0fSPeter Grehan index = offset / MSIX_TABLE_ENTRY_SIZE; 4297fa23353SMark Johnston assert(index < table_count); 4302e81a7e8SNeel Natu 431cd942e0fSPeter Grehan entry = &pi->pi_msix.table[index]; 4322e81a7e8SNeel Natu entry_offset = offset % MSIX_TABLE_ENTRY_SIZE; 433cd942e0fSPeter Grehan 434cd942e0fSPeter Grehan /* Only 4 byte naturally-aligned writes are supported */ 4354d1e669cSPeter Grehan assert(size == 4); 4364d1e669cSPeter Grehan assert(entry_offset % 4 == 0); 4374d1e669cSPeter Grehan 438cd942e0fSPeter Grehan vector_control = entry->vector_control; 4395c40acf8SJohn Baldwin dest32 = (uint32_t *)((void *)entry + entry_offset); 4405c40acf8SJohn Baldwin *dest32 = data; 441cd942e0fSPeter Grehan /* If MSI-X hasn't been enabled, do nothing */ 442cd942e0fSPeter Grehan if (pi->pi_msix.enabled) { 443cd942e0fSPeter Grehan /* If the entry is masked, don't set it up */ 444cd942e0fSPeter Grehan if ((entry->vector_control & PCIM_MSIX_VCTRL_MASK) == 0 || 445cd942e0fSPeter Grehan (vector_control & PCIM_MSIX_VCTRL_MASK) == 0) { 44698e21e80SEnji Cooper (void)vm_setup_pptdev_msix(ctx, vcpu, 44755888cfaSNeel Natu sc->psc_sel.pc_bus, sc->psc_sel.pc_dev, 44855888cfaSNeel Natu sc->psc_sel.pc_func, index, entry->addr, 44955888cfaSNeel Natu entry->msg_data, entry->vector_control); 450cd942e0fSPeter Grehan } 451cd942e0fSPeter Grehan } 452cd942e0fSPeter Grehan } 453cd942e0fSPeter Grehan 454cd942e0fSPeter Grehan static int 455f1442847SBjoern A. Zeeb init_msix_table(struct vmctx *ctx, struct passthru_softc *sc) 456cd942e0fSPeter Grehan { 457cd942e0fSPeter Grehan struct pci_devinst *pi = sc->psc_pi; 4587fa23353SMark Johnston struct pci_bar_mmap pbm; 4597fa23353SMark Johnston int b, s, f; 4607fa23353SMark Johnston uint32_t table_size, table_offset; 461cd942e0fSPeter Grehan 462aa12663fSNeel Natu assert(pci_msix_table_bar(pi) >= 0 && pci_msix_pba_bar(pi) >= 0); 463aa12663fSNeel Natu 4642b89a044SNeel Natu b = sc->psc_sel.pc_bus; 4652b89a044SNeel Natu s = sc->psc_sel.pc_dev; 4662b89a044SNeel Natu f = sc->psc_sel.pc_func; 4672b89a044SNeel Natu 468cd942e0fSPeter Grehan /* 4697fa23353SMark Johnston * Map the region of the BAR containing the MSI-X table. This is 4707fa23353SMark Johnston * necessary for two reasons: 4717fa23353SMark Johnston * 1. The PBA may reside in the first or last page containing the MSI-X 4727fa23353SMark Johnston * table. 4737fa23353SMark Johnston * 2. While PCI devices are not supposed to use the page(s) containing 4747fa23353SMark Johnston * the MSI-X table for other purposes, some do in practice. 475cd942e0fSPeter Grehan */ 4767fa23353SMark Johnston memset(&pbm, 0, sizeof(pbm)); 4777fa23353SMark Johnston pbm.pbm_sel = sc->psc_sel; 4787fa23353SMark Johnston pbm.pbm_flags = PCIIO_BAR_MMAP_RW; 47976b45e68SMark Johnston pbm.pbm_reg = PCIR_BAR(pi->pi_msix.table_bar); 4807fa23353SMark Johnston pbm.pbm_memattr = VM_MEMATTR_DEVICE; 4817fa23353SMark Johnston 4827fa23353SMark Johnston if (ioctl(pcifd, PCIOCBARMMAP, &pbm) != 0) { 4837fa23353SMark Johnston warn("Failed to map MSI-X table BAR on %d/%d/%d", b, s, f); 4847fa23353SMark Johnston return (-1); 4857fa23353SMark Johnston } 4867fa23353SMark Johnston assert(pbm.pbm_bar_off == 0); 4877fa23353SMark Johnston pi->pi_msix.mapped_addr = (uint8_t *)(uintptr_t)pbm.pbm_map_base; 4887fa23353SMark Johnston pi->pi_msix.mapped_size = pbm.pbm_map_length; 4897fa23353SMark Johnston 4907a902ec0SNeel Natu table_offset = rounddown2(pi->pi_msix.table_offset, 4096); 4917a902ec0SNeel Natu 4927a902ec0SNeel Natu table_size = pi->pi_msix.table_offset - table_offset; 4937a902ec0SNeel Natu table_size += pi->pi_msix.table_count * MSIX_TABLE_ENTRY_SIZE; 4947a902ec0SNeel Natu table_size = roundup2(table_size, 4096); 4957a902ec0SNeel Natu 4967a902ec0SNeel Natu /* 4974558c11fSMark Johnston * Unmap any pages not containing the table, we do not need to emulate 4987fa23353SMark Johnston * accesses to them. Avoid releasing address space to help ensure that 4997fa23353SMark Johnston * a buggy out-of-bounds access causes a crash. 5007a902ec0SNeel Natu */ 5017fa23353SMark Johnston if (table_offset != 0) 5027fa23353SMark Johnston if (mprotect(pi->pi_msix.mapped_addr, table_offset, 5037fa23353SMark Johnston PROT_NONE) != 0) 5047fa23353SMark Johnston warn("Failed to unmap MSI-X table BAR region"); 5057fa23353SMark Johnston if (table_offset + table_size != pi->pi_msix.mapped_size) 5064558c11fSMark Johnston if (mprotect( 5074558c11fSMark Johnston pi->pi_msix.mapped_addr + table_offset + table_size, 5087fa23353SMark Johnston pi->pi_msix.mapped_size - (table_offset + table_size), 5097fa23353SMark Johnston PROT_NONE) != 0) 5107fa23353SMark Johnston warn("Failed to unmap MSI-X table BAR region"); 5112b89a044SNeel Natu 5122b89a044SNeel Natu return (0); 513cd942e0fSPeter Grehan } 514cd942e0fSPeter Grehan 515cd942e0fSPeter Grehan static int 516366f6083SPeter Grehan cfginitbar(struct vmctx *ctx, struct passthru_softc *sc) 517366f6083SPeter Grehan { 518366f6083SPeter Grehan int i, error; 519366f6083SPeter Grehan struct pci_devinst *pi; 520366f6083SPeter Grehan struct pci_bar_io bar; 521366f6083SPeter Grehan enum pcibar_type bartype; 5227a902ec0SNeel Natu uint64_t base, size; 523366f6083SPeter Grehan 524366f6083SPeter Grehan pi = sc->psc_pi; 525366f6083SPeter Grehan 526366f6083SPeter Grehan /* 527366f6083SPeter Grehan * Initialize BAR registers 528366f6083SPeter Grehan */ 529366f6083SPeter Grehan for (i = 0; i <= PCI_BARMAX; i++) { 530366f6083SPeter Grehan bzero(&bar, sizeof(bar)); 531366f6083SPeter Grehan bar.pbi_sel = sc->psc_sel; 532366f6083SPeter Grehan bar.pbi_reg = PCIR_BAR(i); 533366f6083SPeter Grehan 534366f6083SPeter Grehan if (ioctl(pcifd, PCIOCGETBAR, &bar) < 0) 535366f6083SPeter Grehan continue; 536366f6083SPeter Grehan 537366f6083SPeter Grehan if (PCI_BAR_IO(bar.pbi_base)) { 538366f6083SPeter Grehan bartype = PCIBAR_IO; 539366f6083SPeter Grehan base = bar.pbi_base & PCIM_BAR_IO_BASE; 540366f6083SPeter Grehan } else { 541366f6083SPeter Grehan switch (bar.pbi_base & PCIM_BAR_MEM_TYPE) { 542366f6083SPeter Grehan case PCIM_BAR_MEM_64: 543366f6083SPeter Grehan bartype = PCIBAR_MEM64; 544366f6083SPeter Grehan break; 545366f6083SPeter Grehan default: 546366f6083SPeter Grehan bartype = PCIBAR_MEM32; 547366f6083SPeter Grehan break; 548366f6083SPeter Grehan } 549366f6083SPeter Grehan base = bar.pbi_base & PCIM_BAR_MEM_BASE; 550366f6083SPeter Grehan } 5517a902ec0SNeel Natu size = bar.pbi_length; 5527a902ec0SNeel Natu 5537a902ec0SNeel Natu if (bartype != PCIBAR_IO) { 5547a902ec0SNeel Natu if (((base | size) & PAGE_MASK) != 0) { 555cff92ffdSJohn Baldwin warnx("passthru device %d/%d/%d BAR %d: " 5567a902ec0SNeel Natu "base %#lx or size %#lx not page aligned\n", 5577a902ec0SNeel Natu sc->psc_sel.pc_bus, sc->psc_sel.pc_dev, 5587a902ec0SNeel Natu sc->psc_sel.pc_func, i, base, size); 5597a902ec0SNeel Natu return (-1); 5607a902ec0SNeel Natu } 5617a902ec0SNeel Natu } 562366f6083SPeter Grehan 563366f6083SPeter Grehan /* Cache information about the "real" BAR */ 564366f6083SPeter Grehan sc->psc_bar[i].type = bartype; 5657a902ec0SNeel Natu sc->psc_bar[i].size = size; 566366f6083SPeter Grehan sc->psc_bar[i].addr = base; 567e87a6f3eSCorvin Köhne sc->psc_bar[i].lobits = 0; 568366f6083SPeter Grehan 569366f6083SPeter Grehan /* Allocate the BAR in the guest I/O or MMIO space */ 570038f5c7bSKonstantin Belousov error = pci_emul_alloc_bar(pi, i, bartype, size); 571366f6083SPeter Grehan if (error) 572366f6083SPeter Grehan return (-1); 573366f6083SPeter Grehan 574e87a6f3eSCorvin Köhne /* Use same lobits as physical bar */ 575e87a6f3eSCorvin Köhne uint8_t lobits = read_config(&sc->psc_sel, PCIR_BAR(i), 0x01); 576e87a6f3eSCorvin Köhne if (bartype == PCIBAR_MEM32 || bartype == PCIBAR_MEM64) { 577e87a6f3eSCorvin Köhne lobits &= ~PCIM_BAR_MEM_BASE; 578e87a6f3eSCorvin Köhne } else { 579e87a6f3eSCorvin Köhne lobits &= ~PCIM_BAR_IO_BASE; 580e87a6f3eSCorvin Köhne } 581e87a6f3eSCorvin Köhne sc->psc_bar[i].lobits = lobits; 582e87a6f3eSCorvin Köhne pi->pi_bar[i].lobits = lobits; 583e87a6f3eSCorvin Köhne 584366f6083SPeter Grehan /* 585366f6083SPeter Grehan * 64-bit BAR takes up two slots so skip the next one. 586366f6083SPeter Grehan */ 587366f6083SPeter Grehan if (bartype == PCIBAR_MEM64) { 588366f6083SPeter Grehan i++; 589366f6083SPeter Grehan assert(i <= PCI_BARMAX); 590366f6083SPeter Grehan sc->psc_bar[i].type = PCIBAR_MEMHI64; 591366f6083SPeter Grehan } 592366f6083SPeter Grehan } 593366f6083SPeter Grehan return (0); 594366f6083SPeter Grehan } 595366f6083SPeter Grehan 596366f6083SPeter Grehan static int 597366f6083SPeter Grehan cfginit(struct vmctx *ctx, struct pci_devinst *pi, int bus, int slot, int func) 598366f6083SPeter Grehan { 599366f6083SPeter Grehan int error; 600366f6083SPeter Grehan struct passthru_softc *sc; 601366f6083SPeter Grehan 602366f6083SPeter Grehan error = 1; 603366f6083SPeter Grehan sc = pi->pi_arg; 604366f6083SPeter Grehan 605366f6083SPeter Grehan bzero(&sc->psc_sel, sizeof(struct pcisel)); 606366f6083SPeter Grehan sc->psc_sel.pc_bus = bus; 607366f6083SPeter Grehan sc->psc_sel.pc_dev = slot; 608366f6083SPeter Grehan sc->psc_sel.pc_func = func; 609366f6083SPeter Grehan 610cff92ffdSJohn Baldwin if (cfginitmsi(sc) != 0) { 611cff92ffdSJohn Baldwin warnx("failed to initialize MSI for PCI %d/%d/%d", 612cff92ffdSJohn Baldwin bus, slot, func); 613cd942e0fSPeter Grehan goto done; 614cff92ffdSJohn Baldwin } 615cd942e0fSPeter Grehan 616cff92ffdSJohn Baldwin if (cfginitbar(ctx, sc) != 0) { 617cff92ffdSJohn Baldwin warnx("failed to initialize BARs for PCI %d/%d/%d", 618cff92ffdSJohn Baldwin bus, slot, func); 619366f6083SPeter Grehan goto done; 620cff92ffdSJohn Baldwin } 621366f6083SPeter Grehan 6222eb20795SCorvin Köhne write_config(&sc->psc_sel, PCIR_COMMAND, 2, 6232eb20795SCorvin Köhne pci_get_cfgdata16(pi, PCIR_COMMAND)); 62456282675SJohn Baldwin 625f1442847SBjoern A. Zeeb /* 626f1442847SBjoern A. Zeeb * We need to do this after PCIR_COMMAND got possibly updated, e.g., 627f1442847SBjoern A. Zeeb * a BAR was enabled, as otherwise the PCIOCBARMMAP might fail on us. 628f1442847SBjoern A. Zeeb */ 629338a1be8SCorvin Köhne if (pci_msix_table_bar(pi) >= 0) { 630f1442847SBjoern A. Zeeb error = init_msix_table(ctx, sc); 631f1442847SBjoern A. Zeeb if (error != 0) { 632338a1be8SCorvin Köhne warnx( 633338a1be8SCorvin Köhne "failed to initialize MSI-X table for PCI %d/%d/%d: %d", 634f1442847SBjoern A. Zeeb bus, slot, func, error); 635f1442847SBjoern A. Zeeb goto done; 636f1442847SBjoern A. Zeeb } 637338a1be8SCorvin Köhne } 638f1442847SBjoern A. Zeeb 639338a1be8SCorvin Köhne error = 0; /* success */ 640366f6083SPeter Grehan done: 641366f6083SPeter Grehan return (error); 642366f6083SPeter Grehan } 643366f6083SPeter Grehan 644366f6083SPeter Grehan static int 645621b5090SJohn Baldwin passthru_legacy_config(nvlist_t *nvl, const char *opts) 646621b5090SJohn Baldwin { 647621b5090SJohn Baldwin char value[16]; 648621b5090SJohn Baldwin int bus, slot, func; 649621b5090SJohn Baldwin 650621b5090SJohn Baldwin if (opts == NULL) 651621b5090SJohn Baldwin return (0); 652621b5090SJohn Baldwin 653621b5090SJohn Baldwin if (sscanf(opts, "%d/%d/%d", &bus, &slot, &func) != 3) { 654621b5090SJohn Baldwin EPRINTLN("passthru: invalid options \"%s\"", opts); 655621b5090SJohn Baldwin return (-1); 656621b5090SJohn Baldwin } 657621b5090SJohn Baldwin 658621b5090SJohn Baldwin snprintf(value, sizeof(value), "%d", bus); 659621b5090SJohn Baldwin set_config_value_node(nvl, "bus", value); 660621b5090SJohn Baldwin snprintf(value, sizeof(value), "%d", slot); 661621b5090SJohn Baldwin set_config_value_node(nvl, "slot", value); 662621b5090SJohn Baldwin snprintf(value, sizeof(value), "%d", func); 663621b5090SJohn Baldwin set_config_value_node(nvl, "func", value); 664e47fe318SCorvin Köhne 6653256b7caSCorvin Köhne opts = strchr(opts, ','); 6663256b7caSCorvin Köhne if (opts == NULL) { 6673256b7caSCorvin Köhne return (0); 6683256b7caSCorvin Köhne } 6693256b7caSCorvin Köhne 6703256b7caSCorvin Köhne return pci_parse_legacy_config(nvl, opts + 1); 671e47fe318SCorvin Köhne } 672e47fe318SCorvin Köhne 673e47fe318SCorvin Köhne static int 674e47fe318SCorvin Köhne passthru_init_rom(struct vmctx *const ctx, struct passthru_softc *const sc, 675e47fe318SCorvin Köhne const char *const romfile) 676e47fe318SCorvin Köhne { 677e47fe318SCorvin Köhne if (romfile == NULL) { 678e47fe318SCorvin Köhne return (0); 679e47fe318SCorvin Köhne } 680e47fe318SCorvin Köhne 681e47fe318SCorvin Köhne const int fd = open(romfile, O_RDONLY); 682e47fe318SCorvin Köhne if (fd < 0) { 683e47fe318SCorvin Köhne warnx("%s: can't open romfile \"%s\"", __func__, romfile); 684e47fe318SCorvin Köhne return (-1); 685e47fe318SCorvin Köhne } 686e47fe318SCorvin Köhne 687e47fe318SCorvin Köhne struct stat sbuf; 688e47fe318SCorvin Köhne if (fstat(fd, &sbuf) < 0) { 689e47fe318SCorvin Köhne warnx("%s: can't fstat romfile \"%s\"", __func__, romfile); 690e47fe318SCorvin Köhne close(fd); 691e47fe318SCorvin Köhne return (-1); 692e47fe318SCorvin Köhne } 693e47fe318SCorvin Köhne const uint64_t rom_size = sbuf.st_size; 694e47fe318SCorvin Köhne 695e47fe318SCorvin Köhne void *const rom_data = mmap(NULL, rom_size, PROT_READ, MAP_SHARED, fd, 696e47fe318SCorvin Köhne 0); 697e47fe318SCorvin Köhne if (rom_data == MAP_FAILED) { 698e47fe318SCorvin Köhne warnx("%s: unable to mmap romfile \"%s\" (%d)", __func__, 699e47fe318SCorvin Köhne romfile, errno); 700e47fe318SCorvin Köhne close(fd); 701e47fe318SCorvin Köhne return (-1); 702e47fe318SCorvin Köhne } 703e47fe318SCorvin Köhne 704e47fe318SCorvin Köhne void *rom_addr; 705e47fe318SCorvin Köhne int error = pci_emul_alloc_rom(sc->psc_pi, rom_size, &rom_addr); 706e47fe318SCorvin Köhne if (error) { 707e47fe318SCorvin Köhne warnx("%s: failed to alloc rom segment", __func__); 708e47fe318SCorvin Köhne munmap(rom_data, rom_size); 709e47fe318SCorvin Köhne close(fd); 710e47fe318SCorvin Köhne return (error); 711e47fe318SCorvin Köhne } 712e47fe318SCorvin Köhne memcpy(rom_addr, rom_data, rom_size); 713e47fe318SCorvin Köhne 714e47fe318SCorvin Köhne sc->psc_bar[PCI_ROM_IDX].type = PCIBAR_ROM; 715e47fe318SCorvin Köhne sc->psc_bar[PCI_ROM_IDX].addr = (uint64_t)rom_addr; 716e47fe318SCorvin Köhne sc->psc_bar[PCI_ROM_IDX].size = rom_size; 717e47fe318SCorvin Köhne 718e47fe318SCorvin Köhne munmap(rom_data, rom_size); 719e47fe318SCorvin Köhne close(fd); 720e47fe318SCorvin Köhne 721621b5090SJohn Baldwin return (0); 722621b5090SJohn Baldwin } 723621b5090SJohn Baldwin 724621b5090SJohn Baldwin static int 725621b5090SJohn Baldwin passthru_init(struct vmctx *ctx, struct pci_devinst *pi, nvlist_t *nvl) 726366f6083SPeter Grehan { 7279b1aa8d6SNeel Natu int bus, slot, func, error, memflags; 728366f6083SPeter Grehan struct passthru_softc *sc; 729621b5090SJohn Baldwin const char *value; 730366f6083SPeter Grehan 731366f6083SPeter Grehan sc = NULL; 732366f6083SPeter Grehan error = 1; 733366f6083SPeter Grehan 7349b1aa8d6SNeel Natu memflags = vm_get_memflags(ctx); 7359b1aa8d6SNeel Natu if (!(memflags & VM_MEM_F_WIRED)) { 736cff92ffdSJohn Baldwin warnx("passthru requires guest memory to be wired"); 737dbb15211SSean Chittenden return (error); 7389b1aa8d6SNeel Natu } 7399b1aa8d6SNeel Natu 740563fd224SCorvin Köhne if (pcifd < 0 && pcifd_init()) { 741dbb15211SSean Chittenden return (error); 742366f6083SPeter Grehan } 74300ef17beSBartek Rutkowski 744621b5090SJohn Baldwin #define GET_INT_CONFIG(var, name) do { \ 745621b5090SJohn Baldwin value = get_config_value_node(nvl, name); \ 746621b5090SJohn Baldwin if (value == NULL) { \ 747621b5090SJohn Baldwin EPRINTLN("passthru: missing required %s setting", name); \ 748621b5090SJohn Baldwin return (error); \ 749621b5090SJohn Baldwin } \ 750621b5090SJohn Baldwin var = atoi(value); \ 751621b5090SJohn Baldwin } while (0) 752621b5090SJohn Baldwin 753621b5090SJohn Baldwin GET_INT_CONFIG(bus, "bus"); 754621b5090SJohn Baldwin GET_INT_CONFIG(slot, "slot"); 755621b5090SJohn Baldwin GET_INT_CONFIG(func, "func"); 756366f6083SPeter Grehan 757cff92ffdSJohn Baldwin if (vm_assign_pptdev(ctx, bus, slot, func) != 0) { 758cff92ffdSJohn Baldwin warnx("PCI device at %d/%d/%d is not using the ppt(4) driver", 759cff92ffdSJohn Baldwin bus, slot, func); 760366f6083SPeter Grehan goto done; 761cff92ffdSJohn Baldwin } 762366f6083SPeter Grehan 763994f858aSXin LI sc = calloc(1, sizeof(struct passthru_softc)); 764366f6083SPeter Grehan 765366f6083SPeter Grehan pi->pi_arg = sc; 766366f6083SPeter Grehan sc->psc_pi = pi; 767366f6083SPeter Grehan 768366f6083SPeter Grehan /* initialize config space */ 769e47fe318SCorvin Köhne if ((error = cfginit(ctx, pi, bus, slot, func)) != 0) 770e47fe318SCorvin Köhne goto done; 771e47fe318SCorvin Köhne 772e47fe318SCorvin Köhne /* initialize ROM */ 773e47fe318SCorvin Köhne if ((error = passthru_init_rom(ctx, sc, 774e47fe318SCorvin Köhne get_config_value_node(nvl, "rom"))) != 0) 775e47fe318SCorvin Köhne goto done; 776e47fe318SCorvin Köhne 777e47fe318SCorvin Köhne error = 0; /* success */ 778366f6083SPeter Grehan done: 779366f6083SPeter Grehan if (error) { 780366f6083SPeter Grehan free(sc); 781366f6083SPeter Grehan vm_unassign_pptdev(ctx, bus, slot, func); 782366f6083SPeter Grehan } 783366f6083SPeter Grehan return (error); 784366f6083SPeter Grehan } 785366f6083SPeter Grehan 786366f6083SPeter Grehan static int 787366f6083SPeter Grehan bar_access(int coff) 788366f6083SPeter Grehan { 789e47fe318SCorvin Köhne if ((coff >= PCIR_BAR(0) && coff < PCIR_BAR(PCI_BARMAX + 1)) || 790e47fe318SCorvin Köhne coff == PCIR_BIOS) 791366f6083SPeter Grehan return (1); 792366f6083SPeter Grehan else 793366f6083SPeter Grehan return (0); 794366f6083SPeter Grehan } 795366f6083SPeter Grehan 796366f6083SPeter Grehan static int 797366f6083SPeter Grehan msicap_access(struct passthru_softc *sc, int coff) 798366f6083SPeter Grehan { 799366f6083SPeter Grehan int caplen; 800366f6083SPeter Grehan 801366f6083SPeter Grehan if (sc->psc_msi.capoff == 0) 802366f6083SPeter Grehan return (0); 803366f6083SPeter Grehan 804366f6083SPeter Grehan caplen = msi_caplen(sc->psc_msi.msgctrl); 805366f6083SPeter Grehan 806366f6083SPeter Grehan if (coff >= sc->psc_msi.capoff && coff < sc->psc_msi.capoff + caplen) 807366f6083SPeter Grehan return (1); 808366f6083SPeter Grehan else 809366f6083SPeter Grehan return (0); 810366f6083SPeter Grehan } 811366f6083SPeter Grehan 812366f6083SPeter Grehan static int 813cd942e0fSPeter Grehan msixcap_access(struct passthru_softc *sc, int coff) 814cd942e0fSPeter Grehan { 815cd942e0fSPeter Grehan if (sc->psc_msix.capoff == 0) 816cd942e0fSPeter Grehan return (0); 817cd942e0fSPeter Grehan 818cd942e0fSPeter Grehan return (coff >= sc->psc_msix.capoff && 819cd942e0fSPeter Grehan coff < sc->psc_msix.capoff + MSIX_CAPLEN); 820cd942e0fSPeter Grehan } 821cd942e0fSPeter Grehan 822cd942e0fSPeter Grehan static int 8234d1e669cSPeter Grehan passthru_cfgread(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, 8244d1e669cSPeter Grehan int coff, int bytes, uint32_t *rv) 825366f6083SPeter Grehan { 826366f6083SPeter Grehan struct passthru_softc *sc; 827366f6083SPeter Grehan 828366f6083SPeter Grehan sc = pi->pi_arg; 829366f6083SPeter Grehan 830366f6083SPeter Grehan /* 831366f6083SPeter Grehan * PCI BARs and MSI capability is emulated. 832366f6083SPeter Grehan */ 833fe66bcf9SCorvin Köhne if (bar_access(coff) || msicap_access(sc, coff) || 834fe66bcf9SCorvin Köhne msixcap_access(sc, coff)) 835366f6083SPeter Grehan return (-1); 836366f6083SPeter Grehan 837366f6083SPeter Grehan #ifdef LEGACY_SUPPORT 838366f6083SPeter Grehan /* 839366f6083SPeter Grehan * Emulate PCIR_CAP_PTR if this device does not support MSI capability 840366f6083SPeter Grehan * natively. 841366f6083SPeter Grehan */ 842366f6083SPeter Grehan if (sc->psc_msi.emulated) { 843366f6083SPeter Grehan if (coff >= PCIR_CAP_PTR && coff < PCIR_CAP_PTR + 4) 844366f6083SPeter Grehan return (-1); 845366f6083SPeter Grehan } 846366f6083SPeter Grehan #endif 847366f6083SPeter Grehan 848c7ba149dSJohn Baldwin /* 849c7ba149dSJohn Baldwin * Emulate the command register. If a single read reads both the 850c7ba149dSJohn Baldwin * command and status registers, read the status register from the 851c7ba149dSJohn Baldwin * device's config space. 852c7ba149dSJohn Baldwin */ 853c7ba149dSJohn Baldwin if (coff == PCIR_COMMAND) { 854c7ba149dSJohn Baldwin if (bytes <= 2) 855c7ba149dSJohn Baldwin return (-1); 85621368498SPeter Grehan *rv = read_config(&sc->psc_sel, PCIR_STATUS, 2) << 16 | 85721368498SPeter Grehan pci_get_cfgdata16(pi, PCIR_COMMAND); 858c7ba149dSJohn Baldwin return (0); 859c7ba149dSJohn Baldwin } 860c7ba149dSJohn Baldwin 861366f6083SPeter Grehan /* Everything else just read from the device's config space */ 862366f6083SPeter Grehan *rv = read_config(&sc->psc_sel, coff, bytes); 863366f6083SPeter Grehan 864366f6083SPeter Grehan return (0); 865366f6083SPeter Grehan } 866366f6083SPeter Grehan 867366f6083SPeter Grehan static int 8684d1e669cSPeter Grehan passthru_cfgwrite(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, 8694d1e669cSPeter Grehan int coff, int bytes, uint32_t val) 870366f6083SPeter Grehan { 871cd942e0fSPeter Grehan int error, msix_table_entries, i; 872366f6083SPeter Grehan struct passthru_softc *sc; 87356282675SJohn Baldwin uint16_t cmd_old; 874366f6083SPeter Grehan 875366f6083SPeter Grehan sc = pi->pi_arg; 876366f6083SPeter Grehan 877366f6083SPeter Grehan /* 878366f6083SPeter Grehan * PCI BARs are emulated 879366f6083SPeter Grehan */ 880366f6083SPeter Grehan if (bar_access(coff)) 881366f6083SPeter Grehan return (-1); 882366f6083SPeter Grehan 883366f6083SPeter Grehan /* 884366f6083SPeter Grehan * MSI capability is emulated 885366f6083SPeter Grehan */ 886366f6083SPeter Grehan if (msicap_access(sc, coff)) { 88721368498SPeter Grehan pci_emul_capwrite(pi, coff, bytes, val, sc->psc_msi.capoff, 88821368498SPeter Grehan PCIY_MSI); 88955888cfaSNeel Natu error = vm_setup_pptdev_msi(ctx, vcpu, sc->psc_sel.pc_bus, 8904f8be175SNeel Natu sc->psc_sel.pc_dev, sc->psc_sel.pc_func, 8914f8be175SNeel Natu pi->pi_msi.addr, pi->pi_msi.msg_data, 8924f8be175SNeel Natu pi->pi_msi.maxmsgnum); 893cff92ffdSJohn Baldwin if (error != 0) 894cff92ffdSJohn Baldwin err(1, "vm_setup_pptdev_msi"); 895366f6083SPeter Grehan return (0); 896366f6083SPeter Grehan } 897366f6083SPeter Grehan 898cd942e0fSPeter Grehan if (msixcap_access(sc, coff)) { 89921368498SPeter Grehan pci_emul_capwrite(pi, coff, bytes, val, sc->psc_msix.capoff, 90021368498SPeter Grehan PCIY_MSIX); 901cd942e0fSPeter Grehan if (pi->pi_msix.enabled) { 902cd942e0fSPeter Grehan msix_table_entries = pi->pi_msix.table_count; 903cd942e0fSPeter Grehan for (i = 0; i < msix_table_entries; i++) { 90455888cfaSNeel Natu error = vm_setup_pptdev_msix(ctx, vcpu, 9054f8be175SNeel Natu sc->psc_sel.pc_bus, sc->psc_sel.pc_dev, 906cd942e0fSPeter Grehan sc->psc_sel.pc_func, i, 9074f8be175SNeel Natu pi->pi_msix.table[i].addr, 908cd942e0fSPeter Grehan pi->pi_msix.table[i].msg_data, 9094f8be175SNeel Natu pi->pi_msix.table[i].vector_control); 910cd942e0fSPeter Grehan 911cff92ffdSJohn Baldwin if (error) 912cff92ffdSJohn Baldwin err(1, "vm_setup_pptdev_msix"); 913cd942e0fSPeter Grehan } 9141925586eSJohn Baldwin } else { 9151925586eSJohn Baldwin error = vm_disable_pptdev_msix(ctx, sc->psc_sel.pc_bus, 9161925586eSJohn Baldwin sc->psc_sel.pc_dev, sc->psc_sel.pc_func); 9171925586eSJohn Baldwin if (error) 9181925586eSJohn Baldwin err(1, "vm_disable_pptdev_msix"); 919cd942e0fSPeter Grehan } 920cd942e0fSPeter Grehan return (0); 921cd942e0fSPeter Grehan } 922cd942e0fSPeter Grehan 923366f6083SPeter Grehan #ifdef LEGACY_SUPPORT 924366f6083SPeter Grehan /* 925366f6083SPeter Grehan * If this device does not support MSI natively then we cannot let 926366f6083SPeter Grehan * the guest disable legacy interrupts from the device. It is the 927366f6083SPeter Grehan * legacy interrupt that is triggering the virtual MSI to the guest. 928366f6083SPeter Grehan */ 929366f6083SPeter Grehan if (sc->psc_msi.emulated && pci_msi_enabled(pi)) { 930366f6083SPeter Grehan if (coff == PCIR_COMMAND && bytes == 2) 931366f6083SPeter Grehan val &= ~PCIM_CMD_INTxDIS; 932366f6083SPeter Grehan } 933366f6083SPeter Grehan #endif 934366f6083SPeter Grehan 935366f6083SPeter Grehan write_config(&sc->psc_sel, coff, bytes, val); 93656282675SJohn Baldwin if (coff == PCIR_COMMAND) { 93756282675SJohn Baldwin cmd_old = pci_get_cfgdata16(pi, PCIR_COMMAND); 93856282675SJohn Baldwin if (bytes == 1) 93956282675SJohn Baldwin pci_set_cfgdata8(pi, PCIR_COMMAND, val); 94056282675SJohn Baldwin else if (bytes == 2) 94156282675SJohn Baldwin pci_set_cfgdata16(pi, PCIR_COMMAND, val); 94256282675SJohn Baldwin pci_emul_cmd_changed(pi, cmd_old); 94356282675SJohn Baldwin } 944366f6083SPeter Grehan 945366f6083SPeter Grehan return (0); 946366f6083SPeter Grehan } 947366f6083SPeter Grehan 948366f6083SPeter Grehan static void 9494d1e669cSPeter Grehan passthru_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx, 9504d1e669cSPeter Grehan uint64_t offset, int size, uint64_t value) 951366f6083SPeter Grehan { 952366f6083SPeter Grehan struct passthru_softc *sc; 95342375556SMark Johnston struct pci_bar_ioreq pio; 954366f6083SPeter Grehan 955366f6083SPeter Grehan sc = pi->pi_arg; 956366f6083SPeter Grehan 957aa12663fSNeel Natu if (baridx == pci_msix_table_bar(pi)) { 9584d1e669cSPeter Grehan msix_table_write(ctx, vcpu, sc, offset, size, value); 9594d1e669cSPeter Grehan } else { 9604d1e669cSPeter Grehan assert(pi->pi_bar[baridx].type == PCIBAR_IO); 96142375556SMark Johnston assert(size == 1 || size == 2 || size == 4); 96242375556SMark Johnston assert(offset <= UINT32_MAX && offset + size <= UINT32_MAX); 963366f6083SPeter Grehan 96442375556SMark Johnston bzero(&pio, sizeof(pio)); 96542375556SMark Johnston pio.pbi_sel = sc->psc_sel; 96642375556SMark Johnston pio.pbi_op = PCIBARIO_WRITE; 96742375556SMark Johnston pio.pbi_bar = baridx; 96842375556SMark Johnston pio.pbi_offset = (uint32_t)offset; 96942375556SMark Johnston pio.pbi_width = size; 97042375556SMark Johnston pio.pbi_value = (uint32_t)value; 97142375556SMark Johnston 97242375556SMark Johnston (void)ioctl(pcifd, PCIOCBARIO, &pio); 973366f6083SPeter Grehan } 9744d1e669cSPeter Grehan } 975366f6083SPeter Grehan 9764d1e669cSPeter Grehan static uint64_t 9774d1e669cSPeter Grehan passthru_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, int baridx, 9784d1e669cSPeter Grehan uint64_t offset, int size) 979366f6083SPeter Grehan { 980366f6083SPeter Grehan struct passthru_softc *sc; 98142375556SMark Johnston struct pci_bar_ioreq pio; 9824d1e669cSPeter Grehan uint64_t val; 983366f6083SPeter Grehan 984366f6083SPeter Grehan sc = pi->pi_arg; 985366f6083SPeter Grehan 986aa12663fSNeel Natu if (baridx == pci_msix_table_bar(pi)) { 9874d1e669cSPeter Grehan val = msix_table_read(sc, offset, size); 9884d1e669cSPeter Grehan } else { 9894d1e669cSPeter Grehan assert(pi->pi_bar[baridx].type == PCIBAR_IO); 99042375556SMark Johnston assert(size == 1 || size == 2 || size == 4); 99142375556SMark Johnston assert(offset <= UINT32_MAX && offset + size <= UINT32_MAX); 992366f6083SPeter Grehan 99342375556SMark Johnston bzero(&pio, sizeof(pio)); 99442375556SMark Johnston pio.pbi_sel = sc->psc_sel; 99542375556SMark Johnston pio.pbi_op = PCIBARIO_READ; 99642375556SMark Johnston pio.pbi_bar = baridx; 99742375556SMark Johnston pio.pbi_offset = (uint32_t)offset; 99842375556SMark Johnston pio.pbi_width = size; 999366f6083SPeter Grehan 100042375556SMark Johnston (void)ioctl(pcifd, PCIOCBARIO, &pio); 100142375556SMark Johnston 100242375556SMark Johnston val = pio.pbi_value; 10034d1e669cSPeter Grehan } 10044d1e669cSPeter Grehan 10054d1e669cSPeter Grehan return (val); 1006366f6083SPeter Grehan } 1007366f6083SPeter Grehan 1008f8a6ec2dSD Scott Phillips static void 1009f8a6ec2dSD Scott Phillips passthru_msix_addr(struct vmctx *ctx, struct pci_devinst *pi, int baridx, 1010f8a6ec2dSD Scott Phillips int enabled, uint64_t address) 1011f8a6ec2dSD Scott Phillips { 1012f8a6ec2dSD Scott Phillips struct passthru_softc *sc; 1013f8a6ec2dSD Scott Phillips size_t remaining; 1014f8a6ec2dSD Scott Phillips uint32_t table_size, table_offset; 1015f8a6ec2dSD Scott Phillips 1016f8a6ec2dSD Scott Phillips sc = pi->pi_arg; 1017f8a6ec2dSD Scott Phillips table_offset = rounddown2(pi->pi_msix.table_offset, 4096); 1018f8a6ec2dSD Scott Phillips if (table_offset > 0) { 1019f8a6ec2dSD Scott Phillips if (!enabled) { 1020f8a6ec2dSD Scott Phillips if (vm_unmap_pptdev_mmio(ctx, sc->psc_sel.pc_bus, 1021f8a6ec2dSD Scott Phillips sc->psc_sel.pc_dev, 1022f8a6ec2dSD Scott Phillips sc->psc_sel.pc_func, address, 1023f8a6ec2dSD Scott Phillips table_offset) != 0) 1024f8a6ec2dSD Scott Phillips warnx("pci_passthru: unmap_pptdev_mmio failed"); 1025f8a6ec2dSD Scott Phillips } else { 1026f8a6ec2dSD Scott Phillips if (vm_map_pptdev_mmio(ctx, sc->psc_sel.pc_bus, 1027f8a6ec2dSD Scott Phillips sc->psc_sel.pc_dev, 1028f8a6ec2dSD Scott Phillips sc->psc_sel.pc_func, address, 1029f8a6ec2dSD Scott Phillips table_offset, 1030f8a6ec2dSD Scott Phillips sc->psc_bar[baridx].addr) != 0) 1031f8a6ec2dSD Scott Phillips warnx("pci_passthru: map_pptdev_mmio failed"); 1032f8a6ec2dSD Scott Phillips } 1033f8a6ec2dSD Scott Phillips } 1034f8a6ec2dSD Scott Phillips table_size = pi->pi_msix.table_offset - table_offset; 1035f8a6ec2dSD Scott Phillips table_size += pi->pi_msix.table_count * MSIX_TABLE_ENTRY_SIZE; 1036f8a6ec2dSD Scott Phillips table_size = roundup2(table_size, 4096); 1037f8a6ec2dSD Scott Phillips remaining = pi->pi_bar[baridx].size - table_offset - table_size; 1038f8a6ec2dSD Scott Phillips if (remaining > 0) { 1039f8a6ec2dSD Scott Phillips address += table_offset + table_size; 1040f8a6ec2dSD Scott Phillips if (!enabled) { 1041f8a6ec2dSD Scott Phillips if (vm_unmap_pptdev_mmio(ctx, sc->psc_sel.pc_bus, 1042f8a6ec2dSD Scott Phillips sc->psc_sel.pc_dev, 1043f8a6ec2dSD Scott Phillips sc->psc_sel.pc_func, address, 1044f8a6ec2dSD Scott Phillips remaining) != 0) 1045f8a6ec2dSD Scott Phillips warnx("pci_passthru: unmap_pptdev_mmio failed"); 1046f8a6ec2dSD Scott Phillips } else { 1047f8a6ec2dSD Scott Phillips if (vm_map_pptdev_mmio(ctx, sc->psc_sel.pc_bus, 1048f8a6ec2dSD Scott Phillips sc->psc_sel.pc_dev, 1049f8a6ec2dSD Scott Phillips sc->psc_sel.pc_func, address, 1050f8a6ec2dSD Scott Phillips remaining, 1051f8a6ec2dSD Scott Phillips sc->psc_bar[baridx].addr + 1052f8a6ec2dSD Scott Phillips table_offset + table_size) != 0) 1053f8a6ec2dSD Scott Phillips warnx("pci_passthru: map_pptdev_mmio failed"); 1054f8a6ec2dSD Scott Phillips } 1055f8a6ec2dSD Scott Phillips } 1056f8a6ec2dSD Scott Phillips } 1057f8a6ec2dSD Scott Phillips 1058f8a6ec2dSD Scott Phillips static void 1059f8a6ec2dSD Scott Phillips passthru_mmio_addr(struct vmctx *ctx, struct pci_devinst *pi, int baridx, 1060f8a6ec2dSD Scott Phillips int enabled, uint64_t address) 1061f8a6ec2dSD Scott Phillips { 1062f8a6ec2dSD Scott Phillips struct passthru_softc *sc; 1063f8a6ec2dSD Scott Phillips 1064f8a6ec2dSD Scott Phillips sc = pi->pi_arg; 1065f8a6ec2dSD Scott Phillips if (!enabled) { 1066f8a6ec2dSD Scott Phillips if (vm_unmap_pptdev_mmio(ctx, sc->psc_sel.pc_bus, 1067f8a6ec2dSD Scott Phillips sc->psc_sel.pc_dev, 1068f8a6ec2dSD Scott Phillips sc->psc_sel.pc_func, address, 1069f8a6ec2dSD Scott Phillips sc->psc_bar[baridx].size) != 0) 1070f8a6ec2dSD Scott Phillips warnx("pci_passthru: unmap_pptdev_mmio failed"); 1071f8a6ec2dSD Scott Phillips } else { 1072f8a6ec2dSD Scott Phillips if (vm_map_pptdev_mmio(ctx, sc->psc_sel.pc_bus, 1073f8a6ec2dSD Scott Phillips sc->psc_sel.pc_dev, 1074f8a6ec2dSD Scott Phillips sc->psc_sel.pc_func, address, 1075f8a6ec2dSD Scott Phillips sc->psc_bar[baridx].size, 1076f8a6ec2dSD Scott Phillips sc->psc_bar[baridx].addr) != 0) 1077f8a6ec2dSD Scott Phillips warnx("pci_passthru: map_pptdev_mmio failed"); 1078f8a6ec2dSD Scott Phillips } 1079f8a6ec2dSD Scott Phillips } 1080f8a6ec2dSD Scott Phillips 1081f8a6ec2dSD Scott Phillips static void 1082e47fe318SCorvin Köhne passthru_addr_rom(struct pci_devinst *const pi, const int idx, 1083e47fe318SCorvin Köhne const int enabled) 1084e47fe318SCorvin Köhne { 1085e47fe318SCorvin Köhne const uint64_t addr = pi->pi_bar[idx].addr; 1086e47fe318SCorvin Köhne const uint64_t size = pi->pi_bar[idx].size; 1087e47fe318SCorvin Köhne 1088e47fe318SCorvin Köhne if (!enabled) { 1089e47fe318SCorvin Köhne if (vm_munmap_memseg(pi->pi_vmctx, addr, size) != 0) { 1090e47fe318SCorvin Köhne errx(4, "%s: munmap_memseg @ [%016lx - %016lx] failed", 1091e47fe318SCorvin Köhne __func__, addr, addr + size); 1092e47fe318SCorvin Köhne } 1093e47fe318SCorvin Köhne 1094e47fe318SCorvin Köhne } else { 1095e47fe318SCorvin Köhne if (vm_mmap_memseg(pi->pi_vmctx, addr, VM_PCIROM, 1096e47fe318SCorvin Köhne pi->pi_romoffset, size, PROT_READ | PROT_EXEC) != 0) { 1097*50526f52SCorvin Köhne errx(4, "%s: mmap_memseg @ [%016lx - %016lx] failed", 1098e47fe318SCorvin Köhne __func__, addr, addr + size); 1099e47fe318SCorvin Köhne } 1100e47fe318SCorvin Köhne } 1101e47fe318SCorvin Köhne } 1102e47fe318SCorvin Köhne 1103e47fe318SCorvin Köhne static void 1104f8a6ec2dSD Scott Phillips passthru_addr(struct vmctx *ctx, struct pci_devinst *pi, int baridx, 1105f8a6ec2dSD Scott Phillips int enabled, uint64_t address) 1106f8a6ec2dSD Scott Phillips { 1107e47fe318SCorvin Köhne switch (pi->pi_bar[baridx].type) { 1108e47fe318SCorvin Köhne case PCIBAR_IO: 1109e47fe318SCorvin Köhne /* IO BARs are emulated */ 1110e47fe318SCorvin Köhne break; 1111e47fe318SCorvin Köhne case PCIBAR_ROM: 1112e47fe318SCorvin Köhne passthru_addr_rom(pi, baridx, enabled); 1113e47fe318SCorvin Köhne break; 1114e47fe318SCorvin Köhne case PCIBAR_MEM32: 1115e47fe318SCorvin Köhne case PCIBAR_MEM64: 1116f8a6ec2dSD Scott Phillips if (baridx == pci_msix_table_bar(pi)) 1117f8a6ec2dSD Scott Phillips passthru_msix_addr(ctx, pi, baridx, enabled, address); 1118f8a6ec2dSD Scott Phillips else 1119f8a6ec2dSD Scott Phillips passthru_mmio_addr(ctx, pi, baridx, enabled, address); 1120e47fe318SCorvin Köhne break; 1121e47fe318SCorvin Köhne default: 1122e47fe318SCorvin Köhne errx(4, "%s: invalid BAR type %d", __func__, 1123e47fe318SCorvin Köhne pi->pi_bar[baridx].type); 1124e47fe318SCorvin Köhne } 1125f8a6ec2dSD Scott Phillips } 1126f8a6ec2dSD Scott Phillips 1127366f6083SPeter Grehan struct pci_devemu passthru = { 1128366f6083SPeter Grehan .pe_emu = "passthru", 1129366f6083SPeter Grehan .pe_init = passthru_init, 1130621b5090SJohn Baldwin .pe_legacy_config = passthru_legacy_config, 1131366f6083SPeter Grehan .pe_cfgwrite = passthru_cfgwrite, 1132366f6083SPeter Grehan .pe_cfgread = passthru_cfgread, 11334d1e669cSPeter Grehan .pe_barwrite = passthru_write, 11344d1e669cSPeter Grehan .pe_barread = passthru_read, 1135f8a6ec2dSD Scott Phillips .pe_baraddr = passthru_addr, 1136366f6083SPeter Grehan }; 1137366f6083SPeter Grehan PCI_EMUL_SET(passthru); 1138