1aad970f1SDavid E. O'Brien /*- 2718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3718cf2ccSPedro F. Giffuni * 4*fce5d19dSStefan Eßer * Copyright 1997, Stefan Esser <se@freebsd.org> 58983cfbfSMike Smith * 68983cfbfSMike Smith * Redistribution and use in source and binary forms, with or without 78983cfbfSMike Smith * modification, are permitted provided that the following conditions 88983cfbfSMike Smith * are met: 98983cfbfSMike Smith * 1. Redistributions of source code must retain the above copyright 108983cfbfSMike Smith * notice unmodified, this list of conditions, and the following 118983cfbfSMike Smith * disclaimer. 128983cfbfSMike Smith * 2. Redistributions in binary form must reproduce the above copyright 138983cfbfSMike Smith * notice, this list of conditions and the following disclaimer in the 148983cfbfSMike Smith * documentation and/or other materials provided with the distribution. 158983cfbfSMike Smith * 168983cfbfSMike Smith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 178983cfbfSMike Smith * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 188983cfbfSMike Smith * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 198983cfbfSMike Smith * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 208983cfbfSMike Smith * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 218983cfbfSMike Smith * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 228983cfbfSMike Smith * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 238983cfbfSMike Smith * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 248983cfbfSMike Smith * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 258983cfbfSMike Smith * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 268983cfbfSMike Smith */ 278983cfbfSMike Smith 28aad970f1SDavid E. O'Brien #include <sys/cdefs.h> 29aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 30aad970f1SDavid E. O'Brien 318983cfbfSMike Smith #include "opt_bus.h" /* XXX trim includes */ 328983cfbfSMike Smith 3387842989SKonstantin Belousov #include <sys/types.h> 348983cfbfSMike Smith #include <sys/param.h> 358983cfbfSMike Smith #include <sys/systm.h> 368983cfbfSMike Smith #include <sys/malloc.h> 378983cfbfSMike Smith #include <sys/module.h> 388983cfbfSMike Smith #include <sys/linker.h> 398983cfbfSMike Smith #include <sys/fcntl.h> 408983cfbfSMike Smith #include <sys/conf.h> 418983cfbfSMike Smith #include <sys/kernel.h> 4287842989SKonstantin Belousov #include <sys/mman.h> 438002488bSRobert Watson #include <sys/proc.h> 448983cfbfSMike Smith #include <sys/queue.h> 4587842989SKonstantin Belousov #include <sys/rwlock.h> 4687842989SKonstantin Belousov #include <sys/sglist.h> 478983cfbfSMike Smith 488983cfbfSMike Smith #include <vm/vm.h> 498983cfbfSMike Smith #include <vm/pmap.h> 508983cfbfSMike Smith #include <vm/vm_extern.h> 5187842989SKonstantin Belousov #include <vm/vm_map.h> 5287842989SKonstantin Belousov #include <vm/vm_object.h> 5387842989SKonstantin Belousov #include <vm/vm_page.h> 5487842989SKonstantin Belousov #include <vm/vm_pager.h> 558983cfbfSMike Smith 568983cfbfSMike Smith #include <sys/bus.h> 578983cfbfSMike Smith #include <machine/bus.h> 588983cfbfSMike Smith #include <sys/rman.h> 598983cfbfSMike Smith #include <machine/resource.h> 608983cfbfSMike Smith 618983cfbfSMike Smith #include <sys/pciio.h> 6238d8c994SWarner Losh #include <dev/pci/pcireg.h> 6338d8c994SWarner Losh #include <dev/pci/pcivar.h> 648983cfbfSMike Smith 658983cfbfSMike Smith #include "pcib_if.h" 668983cfbfSMike Smith #include "pci_if.h" 678983cfbfSMike Smith 68b7edb6faSBrooks Davis #ifdef COMPAT_FREEBSD32 69b7edb6faSBrooks Davis struct pci_conf32 { 70b7edb6faSBrooks Davis struct pcisel pc_sel; /* domain+bus+slot+function */ 71b7edb6faSBrooks Davis u_int8_t pc_hdr; /* PCI header type */ 72b7edb6faSBrooks Davis u_int16_t pc_subvendor; /* card vendor ID */ 73b7edb6faSBrooks Davis u_int16_t pc_subdevice; /* card device ID, assigned by 74b7edb6faSBrooks Davis card vendor */ 75b7edb6faSBrooks Davis u_int16_t pc_vendor; /* chip vendor ID */ 76b7edb6faSBrooks Davis u_int16_t pc_device; /* chip device ID, assigned by 77b7edb6faSBrooks Davis chip vendor */ 78b7edb6faSBrooks Davis u_int8_t pc_class; /* chip PCI class */ 79b7edb6faSBrooks Davis u_int8_t pc_subclass; /* chip PCI subclass */ 80b7edb6faSBrooks Davis u_int8_t pc_progif; /* chip PCI programming interface */ 81b7edb6faSBrooks Davis u_int8_t pc_revid; /* chip revision ID */ 82b7edb6faSBrooks Davis char pd_name[PCI_MAXNAMELEN + 1]; /* device name */ 83b7edb6faSBrooks Davis u_int32_t pd_unit; /* device unit number */ 84b7edb6faSBrooks Davis }; 85b7edb6faSBrooks Davis 86b7edb6faSBrooks Davis struct pci_match_conf32 { 87b7edb6faSBrooks Davis struct pcisel pc_sel; /* domain+bus+slot+function */ 88b7edb6faSBrooks Davis char pd_name[PCI_MAXNAMELEN + 1]; /* device name */ 89b7edb6faSBrooks Davis u_int32_t pd_unit; /* Unit number */ 90b7edb6faSBrooks Davis u_int16_t pc_vendor; /* PCI Vendor ID */ 91b7edb6faSBrooks Davis u_int16_t pc_device; /* PCI Device ID */ 92b7edb6faSBrooks Davis u_int8_t pc_class; /* PCI class */ 93b7edb6faSBrooks Davis u_int32_t flags; /* Matching expression */ 94b7edb6faSBrooks Davis }; 95b7edb6faSBrooks Davis 96b7edb6faSBrooks Davis struct pci_conf_io32 { 97b7edb6faSBrooks Davis u_int32_t pat_buf_len; /* pattern buffer length */ 98b7edb6faSBrooks Davis u_int32_t num_patterns; /* number of patterns */ 99b7edb6faSBrooks Davis u_int32_t patterns; /* struct pci_match_conf ptr */ 100b7edb6faSBrooks Davis u_int32_t match_buf_len; /* match buffer length */ 101b7edb6faSBrooks Davis u_int32_t num_matches; /* number of matches returned */ 102b7edb6faSBrooks Davis u_int32_t matches; /* struct pci_conf ptr */ 103b7edb6faSBrooks Davis u_int32_t offset; /* offset into device list */ 104b7edb6faSBrooks Davis u_int32_t generation; /* device list generation */ 105b7edb6faSBrooks Davis u_int32_t status; /* request status */ 106b7edb6faSBrooks Davis }; 107b7edb6faSBrooks Davis 108b7edb6faSBrooks Davis #define PCIOCGETCONF32 _IOC_NEWTYPE(PCIOCGETCONF, struct pci_conf_io32) 109b7edb6faSBrooks Davis #endif 110b7edb6faSBrooks Davis 1118983cfbfSMike Smith /* 1128983cfbfSMike Smith * This is the user interface to PCI configuration space. 1138983cfbfSMike Smith */ 1148983cfbfSMike Smith 115b40ce416SJulian Elischer static d_open_t pci_open; 116b40ce416SJulian Elischer static d_close_t pci_close; 117b40ce416SJulian Elischer static d_ioctl_t pci_ioctl; 1188983cfbfSMike Smith 1198983cfbfSMike Smith struct cdevsw pcicdev = { 120dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 121dd615d09SWarner Losh .d_flags = 0, 1227ac40f5fSPoul-Henning Kamp .d_open = pci_open, 1237ac40f5fSPoul-Henning Kamp .d_close = pci_close, 1247ac40f5fSPoul-Henning Kamp .d_ioctl = pci_ioctl, 1257ac40f5fSPoul-Henning Kamp .d_name = "pci", 1268983cfbfSMike Smith }; 1278983cfbfSMike Smith 1288983cfbfSMike Smith static int 12989c9c53dSPoul-Henning Kamp pci_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 1308983cfbfSMike Smith { 1318002488bSRobert Watson int error; 1328002488bSRobert Watson 1338002488bSRobert Watson if (oflags & FWRITE) { 134a854ed98SJohn Baldwin error = securelevel_gt(td->td_ucred, 0); 1358002488bSRobert Watson if (error) 1368002488bSRobert Watson return (error); 1378983cfbfSMike Smith } 1388002488bSRobert Watson 1398002488bSRobert Watson return (0); 1408983cfbfSMike Smith } 1418983cfbfSMike Smith 1428983cfbfSMike Smith static int 14389c9c53dSPoul-Henning Kamp pci_close(struct cdev *dev, int flag, int devtype, struct thread *td) 1448983cfbfSMike Smith { 1458983cfbfSMike Smith return 0; 1468983cfbfSMike Smith } 1478983cfbfSMike Smith 1488983cfbfSMike Smith /* 1498983cfbfSMike Smith * Match a single pci_conf structure against an array of pci_match_conf 1508983cfbfSMike Smith * structures. The first argument, 'matches', is an array of num_matches 1518983cfbfSMike Smith * pci_match_conf structures. match_buf is a pointer to the pci_conf 1528983cfbfSMike Smith * structure that will be compared to every entry in the matches array. 1538983cfbfSMike Smith * This function returns 1 on failure, 0 on success. 1548983cfbfSMike Smith */ 1558983cfbfSMike Smith static int 156e9ed3a70SBrooks Davis pci_conf_match_native(struct pci_match_conf *matches, int num_matches, 1578983cfbfSMike Smith struct pci_conf *match_buf) 1588983cfbfSMike Smith { 1598983cfbfSMike Smith int i; 1608983cfbfSMike Smith 1618983cfbfSMike Smith if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0)) 1628983cfbfSMike Smith return(1); 1638983cfbfSMike Smith 1648983cfbfSMike Smith for (i = 0; i < num_matches; i++) { 1658983cfbfSMike Smith /* 1668983cfbfSMike Smith * I'm not sure why someone would do this...but... 1678983cfbfSMike Smith */ 1688983cfbfSMike Smith if (matches[i].flags == PCI_GETCONF_NO_MATCH) 1698983cfbfSMike Smith continue; 1708983cfbfSMike Smith 1718983cfbfSMike Smith /* 1728983cfbfSMike Smith * Look at each of the match flags. If it's set, do the 1738983cfbfSMike Smith * comparison. If the comparison fails, we don't have a 1748983cfbfSMike Smith * match, go on to the next item if there is one. 1758983cfbfSMike Smith */ 17655aaf894SMarius Strobl if (((matches[i].flags & PCI_GETCONF_MATCH_DOMAIN) != 0) 17755aaf894SMarius Strobl && (match_buf->pc_sel.pc_domain != 17855aaf894SMarius Strobl matches[i].pc_sel.pc_domain)) 17955aaf894SMarius Strobl continue; 18055aaf894SMarius Strobl 1818983cfbfSMike Smith if (((matches[i].flags & PCI_GETCONF_MATCH_BUS) != 0) 1828983cfbfSMike Smith && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus)) 1838983cfbfSMike Smith continue; 1848983cfbfSMike Smith 1858983cfbfSMike Smith if (((matches[i].flags & PCI_GETCONF_MATCH_DEV) != 0) 1868983cfbfSMike Smith && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev)) 1878983cfbfSMike Smith continue; 1888983cfbfSMike Smith 1898983cfbfSMike Smith if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC) != 0) 1908983cfbfSMike Smith && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func)) 1918983cfbfSMike Smith continue; 1928983cfbfSMike Smith 1938983cfbfSMike Smith if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR) != 0) 1948983cfbfSMike Smith && (match_buf->pc_vendor != matches[i].pc_vendor)) 1958983cfbfSMike Smith continue; 1968983cfbfSMike Smith 1978983cfbfSMike Smith if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE) != 0) 1988983cfbfSMike Smith && (match_buf->pc_device != matches[i].pc_device)) 1998983cfbfSMike Smith continue; 2008983cfbfSMike Smith 2018983cfbfSMike Smith if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS) != 0) 2028983cfbfSMike Smith && (match_buf->pc_class != matches[i].pc_class)) 2038983cfbfSMike Smith continue; 2048983cfbfSMike Smith 2058983cfbfSMike Smith if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT) != 0) 2068983cfbfSMike Smith && (match_buf->pd_unit != matches[i].pd_unit)) 2078983cfbfSMike Smith continue; 2088983cfbfSMike Smith 2098983cfbfSMike Smith if (((matches[i].flags & PCI_GETCONF_MATCH_NAME) != 0) 2108983cfbfSMike Smith && (strncmp(matches[i].pd_name, match_buf->pd_name, 2118983cfbfSMike Smith sizeof(match_buf->pd_name)) != 0)) 2128983cfbfSMike Smith continue; 2138983cfbfSMike Smith 2148983cfbfSMike Smith return(0); 2158983cfbfSMike Smith } 2168983cfbfSMike Smith 2178983cfbfSMike Smith return(1); 2188983cfbfSMike Smith } 2198983cfbfSMike Smith 220b7edb6faSBrooks Davis #ifdef COMPAT_FREEBSD32 221b7edb6faSBrooks Davis static int 222b7edb6faSBrooks Davis pci_conf_match32(struct pci_match_conf32 *matches, int num_matches, 223b7edb6faSBrooks Davis struct pci_conf *match_buf) 224b7edb6faSBrooks Davis { 225b7edb6faSBrooks Davis int i; 226b7edb6faSBrooks Davis 227b7edb6faSBrooks Davis if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0)) 228b7edb6faSBrooks Davis return(1); 229b7edb6faSBrooks Davis 230b7edb6faSBrooks Davis for (i = 0; i < num_matches; i++) { 231b7edb6faSBrooks Davis /* 232b7edb6faSBrooks Davis * I'm not sure why someone would do this...but... 233b7edb6faSBrooks Davis */ 234b7edb6faSBrooks Davis if (matches[i].flags == PCI_GETCONF_NO_MATCH) 235b7edb6faSBrooks Davis continue; 236b7edb6faSBrooks Davis 237b7edb6faSBrooks Davis /* 238b7edb6faSBrooks Davis * Look at each of the match flags. If it's set, do the 239b7edb6faSBrooks Davis * comparison. If the comparison fails, we don't have a 240b7edb6faSBrooks Davis * match, go on to the next item if there is one. 241b7edb6faSBrooks Davis */ 242b7edb6faSBrooks Davis if (((matches[i].flags & PCI_GETCONF_MATCH_DOMAIN) != 0) 243b7edb6faSBrooks Davis && (match_buf->pc_sel.pc_domain != 244b7edb6faSBrooks Davis matches[i].pc_sel.pc_domain)) 245b7edb6faSBrooks Davis continue; 246b7edb6faSBrooks Davis 247b7edb6faSBrooks Davis if (((matches[i].flags & PCI_GETCONF_MATCH_BUS) != 0) 248b7edb6faSBrooks Davis && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus)) 249b7edb6faSBrooks Davis continue; 250b7edb6faSBrooks Davis 251b7edb6faSBrooks Davis if (((matches[i].flags & PCI_GETCONF_MATCH_DEV) != 0) 252b7edb6faSBrooks Davis && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev)) 253b7edb6faSBrooks Davis continue; 254b7edb6faSBrooks Davis 255b7edb6faSBrooks Davis if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC) != 0) 256b7edb6faSBrooks Davis && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func)) 257b7edb6faSBrooks Davis continue; 258b7edb6faSBrooks Davis 259b7edb6faSBrooks Davis if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR) != 0) 260b7edb6faSBrooks Davis && (match_buf->pc_vendor != matches[i].pc_vendor)) 261b7edb6faSBrooks Davis continue; 262b7edb6faSBrooks Davis 263b7edb6faSBrooks Davis if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE) != 0) 264b7edb6faSBrooks Davis && (match_buf->pc_device != matches[i].pc_device)) 265b7edb6faSBrooks Davis continue; 266b7edb6faSBrooks Davis 267b7edb6faSBrooks Davis if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS) != 0) 268b7edb6faSBrooks Davis && (match_buf->pc_class != matches[i].pc_class)) 269b7edb6faSBrooks Davis continue; 270b7edb6faSBrooks Davis 271b7edb6faSBrooks Davis if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT) != 0) 272b7edb6faSBrooks Davis && (match_buf->pd_unit != matches[i].pd_unit)) 273b7edb6faSBrooks Davis continue; 274b7edb6faSBrooks Davis 275b7edb6faSBrooks Davis if (((matches[i].flags & PCI_GETCONF_MATCH_NAME) != 0) 276b7edb6faSBrooks Davis && (strncmp(matches[i].pd_name, match_buf->pd_name, 277b7edb6faSBrooks Davis sizeof(match_buf->pd_name)) != 0)) 278b7edb6faSBrooks Davis continue; 279b7edb6faSBrooks Davis 280b7edb6faSBrooks Davis return(0); 281b7edb6faSBrooks Davis } 282b7edb6faSBrooks Davis 283b7edb6faSBrooks Davis return(1); 284b7edb6faSBrooks Davis } 285b7edb6faSBrooks Davis #endif /* COMPAT_FREEBSD32 */ 286b7edb6faSBrooks Davis 28733d3fffaSMarius Strobl #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \ 28833d3fffaSMarius Strobl defined(COMPAT_FREEBSD6) 289b2068c0cSWarner Losh #define PRE7_COMPAT 29033d3fffaSMarius Strobl 29133d3fffaSMarius Strobl typedef enum { 29233d3fffaSMarius Strobl PCI_GETCONF_NO_MATCH_OLD = 0x00, 29333d3fffaSMarius Strobl PCI_GETCONF_MATCH_BUS_OLD = 0x01, 29433d3fffaSMarius Strobl PCI_GETCONF_MATCH_DEV_OLD = 0x02, 29533d3fffaSMarius Strobl PCI_GETCONF_MATCH_FUNC_OLD = 0x04, 29633d3fffaSMarius Strobl PCI_GETCONF_MATCH_NAME_OLD = 0x08, 29733d3fffaSMarius Strobl PCI_GETCONF_MATCH_UNIT_OLD = 0x10, 29833d3fffaSMarius Strobl PCI_GETCONF_MATCH_VENDOR_OLD = 0x20, 29933d3fffaSMarius Strobl PCI_GETCONF_MATCH_DEVICE_OLD = 0x40, 30033d3fffaSMarius Strobl PCI_GETCONF_MATCH_CLASS_OLD = 0x80 30133d3fffaSMarius Strobl } pci_getconf_flags_old; 30233d3fffaSMarius Strobl 30333d3fffaSMarius Strobl struct pcisel_old { 30433d3fffaSMarius Strobl u_int8_t pc_bus; /* bus number */ 30533d3fffaSMarius Strobl u_int8_t pc_dev; /* device on this bus */ 30633d3fffaSMarius Strobl u_int8_t pc_func; /* function on this device */ 30733d3fffaSMarius Strobl }; 30833d3fffaSMarius Strobl 30933d3fffaSMarius Strobl struct pci_conf_old { 31033d3fffaSMarius Strobl struct pcisel_old pc_sel; /* bus+slot+function */ 31133d3fffaSMarius Strobl u_int8_t pc_hdr; /* PCI header type */ 31233d3fffaSMarius Strobl u_int16_t pc_subvendor; /* card vendor ID */ 31333d3fffaSMarius Strobl u_int16_t pc_subdevice; /* card device ID, assigned by 31433d3fffaSMarius Strobl card vendor */ 31533d3fffaSMarius Strobl u_int16_t pc_vendor; /* chip vendor ID */ 31633d3fffaSMarius Strobl u_int16_t pc_device; /* chip device ID, assigned by 31733d3fffaSMarius Strobl chip vendor */ 31833d3fffaSMarius Strobl u_int8_t pc_class; /* chip PCI class */ 31933d3fffaSMarius Strobl u_int8_t pc_subclass; /* chip PCI subclass */ 32033d3fffaSMarius Strobl u_int8_t pc_progif; /* chip PCI programming interface */ 32133d3fffaSMarius Strobl u_int8_t pc_revid; /* chip revision ID */ 32233d3fffaSMarius Strobl char pd_name[PCI_MAXNAMELEN + 1]; /* device name */ 32333d3fffaSMarius Strobl u_long pd_unit; /* device unit number */ 32433d3fffaSMarius Strobl }; 32533d3fffaSMarius Strobl 32633d3fffaSMarius Strobl struct pci_match_conf_old { 32733d3fffaSMarius Strobl struct pcisel_old pc_sel; /* bus+slot+function */ 32833d3fffaSMarius Strobl char pd_name[PCI_MAXNAMELEN + 1]; /* device name */ 32933d3fffaSMarius Strobl u_long pd_unit; /* Unit number */ 33033d3fffaSMarius Strobl u_int16_t pc_vendor; /* PCI Vendor ID */ 33133d3fffaSMarius Strobl u_int16_t pc_device; /* PCI Device ID */ 33233d3fffaSMarius Strobl u_int8_t pc_class; /* PCI class */ 333c5860546SMarius Strobl pci_getconf_flags_old flags; /* Matching expression */ 33433d3fffaSMarius Strobl }; 33533d3fffaSMarius Strobl 33633d3fffaSMarius Strobl struct pci_io_old { 33733d3fffaSMarius Strobl struct pcisel_old pi_sel; /* device to operate on */ 33833d3fffaSMarius Strobl int pi_reg; /* configuration register to examine */ 33933d3fffaSMarius Strobl int pi_width; /* width (in bytes) of read or write */ 34033d3fffaSMarius Strobl u_int32_t pi_data; /* data to write or result of read */ 34133d3fffaSMarius Strobl }; 34233d3fffaSMarius Strobl 343b01bf72bSMaxim Sobolev #ifdef COMPAT_FREEBSD32 344b01bf72bSMaxim Sobolev struct pci_conf_old32 { 345b01bf72bSMaxim Sobolev struct pcisel_old pc_sel; /* bus+slot+function */ 346e5280830SGleb Smirnoff uint8_t pc_hdr; /* PCI header type */ 347e5280830SGleb Smirnoff uint16_t pc_subvendor; /* card vendor ID */ 348e5280830SGleb Smirnoff uint16_t pc_subdevice; /* card device ID, assigned by 349b01bf72bSMaxim Sobolev card vendor */ 350e5280830SGleb Smirnoff uint16_t pc_vendor; /* chip vendor ID */ 351e5280830SGleb Smirnoff uint16_t pc_device; /* chip device ID, assigned by 352b01bf72bSMaxim Sobolev chip vendor */ 353e5280830SGleb Smirnoff uint8_t pc_class; /* chip PCI class */ 354e5280830SGleb Smirnoff uint8_t pc_subclass; /* chip PCI subclass */ 355e5280830SGleb Smirnoff uint8_t pc_progif; /* chip PCI programming interface */ 356e5280830SGleb Smirnoff uint8_t pc_revid; /* chip revision ID */ 357b01bf72bSMaxim Sobolev char pd_name[PCI_MAXNAMELEN + 1]; /* device name */ 358e5280830SGleb Smirnoff uint32_t pd_unit; /* device unit number (u_long) */ 359b01bf72bSMaxim Sobolev }; 360b01bf72bSMaxim Sobolev 361b01bf72bSMaxim Sobolev struct pci_match_conf_old32 { 362b01bf72bSMaxim Sobolev struct pcisel_old pc_sel; /* bus+slot+function */ 363b01bf72bSMaxim Sobolev char pd_name[PCI_MAXNAMELEN + 1]; /* device name */ 364e5280830SGleb Smirnoff uint32_t pd_unit; /* Unit number (u_long) */ 365e5280830SGleb Smirnoff uint16_t pc_vendor; /* PCI Vendor ID */ 366e5280830SGleb Smirnoff uint16_t pc_device; /* PCI Device ID */ 367e5280830SGleb Smirnoff uint8_t pc_class; /* PCI class */ 368b01bf72bSMaxim Sobolev pci_getconf_flags_old flags; /* Matching expression */ 369b01bf72bSMaxim Sobolev }; 370b01bf72bSMaxim Sobolev 371b01bf72bSMaxim Sobolev #define PCIOCGETCONF_OLD32 _IOWR('p', 1, struct pci_conf_io32) 372904c3909SGleb Smirnoff #endif /* COMPAT_FREEBSD32 */ 373b01bf72bSMaxim Sobolev 37433d3fffaSMarius Strobl #define PCIOCGETCONF_OLD _IOWR('p', 1, struct pci_conf_io) 37533d3fffaSMarius Strobl #define PCIOCREAD_OLD _IOWR('p', 2, struct pci_io_old) 37633d3fffaSMarius Strobl #define PCIOCWRITE_OLD _IOWR('p', 3, struct pci_io_old) 37733d3fffaSMarius Strobl 37833d3fffaSMarius Strobl static int 37933d3fffaSMarius Strobl pci_conf_match_old(struct pci_match_conf_old *matches, int num_matches, 38033d3fffaSMarius Strobl struct pci_conf *match_buf) 38133d3fffaSMarius Strobl { 38233d3fffaSMarius Strobl int i; 38333d3fffaSMarius Strobl 38433d3fffaSMarius Strobl if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0)) 38533d3fffaSMarius Strobl return(1); 38633d3fffaSMarius Strobl 38733d3fffaSMarius Strobl for (i = 0; i < num_matches; i++) { 38833d3fffaSMarius Strobl if (match_buf->pc_sel.pc_domain != 0) 38933d3fffaSMarius Strobl continue; 39033d3fffaSMarius Strobl 39133d3fffaSMarius Strobl /* 39233d3fffaSMarius Strobl * I'm not sure why someone would do this...but... 39333d3fffaSMarius Strobl */ 39433d3fffaSMarius Strobl if (matches[i].flags == PCI_GETCONF_NO_MATCH_OLD) 39533d3fffaSMarius Strobl continue; 39633d3fffaSMarius Strobl 39733d3fffaSMarius Strobl /* 39833d3fffaSMarius Strobl * Look at each of the match flags. If it's set, do the 39933d3fffaSMarius Strobl * comparison. If the comparison fails, we don't have a 40033d3fffaSMarius Strobl * match, go on to the next item if there is one. 40133d3fffaSMarius Strobl */ 40233d3fffaSMarius Strobl if (((matches[i].flags & PCI_GETCONF_MATCH_BUS_OLD) != 0) 40333d3fffaSMarius Strobl && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus)) 40433d3fffaSMarius Strobl continue; 40533d3fffaSMarius Strobl 40633d3fffaSMarius Strobl if (((matches[i].flags & PCI_GETCONF_MATCH_DEV_OLD) != 0) 40733d3fffaSMarius Strobl && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev)) 40833d3fffaSMarius Strobl continue; 40933d3fffaSMarius Strobl 41033d3fffaSMarius Strobl if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC_OLD) != 0) 41133d3fffaSMarius Strobl && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func)) 41233d3fffaSMarius Strobl continue; 41333d3fffaSMarius Strobl 41433d3fffaSMarius Strobl if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR_OLD) != 0) 41533d3fffaSMarius Strobl && (match_buf->pc_vendor != matches[i].pc_vendor)) 41633d3fffaSMarius Strobl continue; 41733d3fffaSMarius Strobl 41833d3fffaSMarius Strobl if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE_OLD) != 0) 41933d3fffaSMarius Strobl && (match_buf->pc_device != matches[i].pc_device)) 42033d3fffaSMarius Strobl continue; 42133d3fffaSMarius Strobl 42233d3fffaSMarius Strobl if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS_OLD) != 0) 42333d3fffaSMarius Strobl && (match_buf->pc_class != matches[i].pc_class)) 42433d3fffaSMarius Strobl continue; 42533d3fffaSMarius Strobl 42633d3fffaSMarius Strobl if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT_OLD) != 0) 42733d3fffaSMarius Strobl && (match_buf->pd_unit != matches[i].pd_unit)) 42833d3fffaSMarius Strobl continue; 42933d3fffaSMarius Strobl 43033d3fffaSMarius Strobl if (((matches[i].flags & PCI_GETCONF_MATCH_NAME_OLD) != 0) 43133d3fffaSMarius Strobl && (strncmp(matches[i].pd_name, match_buf->pd_name, 43233d3fffaSMarius Strobl sizeof(match_buf->pd_name)) != 0)) 43333d3fffaSMarius Strobl continue; 43433d3fffaSMarius Strobl 43533d3fffaSMarius Strobl return(0); 43633d3fffaSMarius Strobl } 43733d3fffaSMarius Strobl 43833d3fffaSMarius Strobl return(1); 43933d3fffaSMarius Strobl } 44033d3fffaSMarius Strobl 441904c3909SGleb Smirnoff #ifdef COMPAT_FREEBSD32 442b01bf72bSMaxim Sobolev static int 443b01bf72bSMaxim Sobolev pci_conf_match_old32(struct pci_match_conf_old32 *matches, int num_matches, 444b01bf72bSMaxim Sobolev struct pci_conf *match_buf) 445b01bf72bSMaxim Sobolev { 446b01bf72bSMaxim Sobolev int i; 447b01bf72bSMaxim Sobolev 448b01bf72bSMaxim Sobolev if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0)) 449b01bf72bSMaxim Sobolev return(1); 450b01bf72bSMaxim Sobolev 451b01bf72bSMaxim Sobolev for (i = 0; i < num_matches; i++) { 452b01bf72bSMaxim Sobolev if (match_buf->pc_sel.pc_domain != 0) 453b01bf72bSMaxim Sobolev continue; 454b01bf72bSMaxim Sobolev 455b01bf72bSMaxim Sobolev /* 456b01bf72bSMaxim Sobolev * I'm not sure why someone would do this...but... 457b01bf72bSMaxim Sobolev */ 458b01bf72bSMaxim Sobolev if (matches[i].flags == PCI_GETCONF_NO_MATCH_OLD) 459b01bf72bSMaxim Sobolev continue; 460b01bf72bSMaxim Sobolev 461b01bf72bSMaxim Sobolev /* 462b01bf72bSMaxim Sobolev * Look at each of the match flags. If it's set, do the 463b01bf72bSMaxim Sobolev * comparison. If the comparison fails, we don't have a 464b01bf72bSMaxim Sobolev * match, go on to the next item if there is one. 465b01bf72bSMaxim Sobolev */ 466e5280830SGleb Smirnoff if (((matches[i].flags & PCI_GETCONF_MATCH_BUS_OLD) != 0) && 467e5280830SGleb Smirnoff (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus)) 468b01bf72bSMaxim Sobolev continue; 469b01bf72bSMaxim Sobolev 470e5280830SGleb Smirnoff if (((matches[i].flags & PCI_GETCONF_MATCH_DEV_OLD) != 0) && 471e5280830SGleb Smirnoff (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev)) 472b01bf72bSMaxim Sobolev continue; 473b01bf72bSMaxim Sobolev 474e5280830SGleb Smirnoff if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC_OLD) != 0) && 475e5280830SGleb Smirnoff (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func)) 476b01bf72bSMaxim Sobolev continue; 477b01bf72bSMaxim Sobolev 478e5280830SGleb Smirnoff if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR_OLD) != 0) && 479e5280830SGleb Smirnoff (match_buf->pc_vendor != matches[i].pc_vendor)) 480b01bf72bSMaxim Sobolev continue; 481b01bf72bSMaxim Sobolev 482e5280830SGleb Smirnoff if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE_OLD) != 0) && 483e5280830SGleb Smirnoff (match_buf->pc_device != matches[i].pc_device)) 484b01bf72bSMaxim Sobolev continue; 485b01bf72bSMaxim Sobolev 486e5280830SGleb Smirnoff if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS_OLD) != 0) && 487e5280830SGleb Smirnoff (match_buf->pc_class != matches[i].pc_class)) 488b01bf72bSMaxim Sobolev continue; 489b01bf72bSMaxim Sobolev 490e5280830SGleb Smirnoff if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT_OLD) != 0) && 491e5280830SGleb Smirnoff ((u_int32_t)match_buf->pd_unit != matches[i].pd_unit)) 492b01bf72bSMaxim Sobolev continue; 493b01bf72bSMaxim Sobolev 494e5280830SGleb Smirnoff if (((matches[i].flags & PCI_GETCONF_MATCH_NAME_OLD) != 0) && 495e5280830SGleb Smirnoff (strncmp(matches[i].pd_name, match_buf->pd_name, 496b01bf72bSMaxim Sobolev sizeof(match_buf->pd_name)) != 0)) 497b01bf72bSMaxim Sobolev continue; 498b01bf72bSMaxim Sobolev 499b01bf72bSMaxim Sobolev return (0); 500b01bf72bSMaxim Sobolev } 501b01bf72bSMaxim Sobolev 502b01bf72bSMaxim Sobolev return (1); 503b01bf72bSMaxim Sobolev } 504904c3909SGleb Smirnoff #endif /* COMPAT_FREEBSD32 */ 505e9ed3a70SBrooks Davis #endif /* !PRE7_COMPAT */ 506e9ed3a70SBrooks Davis 507e9ed3a70SBrooks Davis union pci_conf_union { 508e9ed3a70SBrooks Davis struct pci_conf pc; 509b7edb6faSBrooks Davis #ifdef COMPAT_FREEBSD32 510b7edb6faSBrooks Davis struct pci_conf32 pc32; 511b7edb6faSBrooks Davis #endif 512e9ed3a70SBrooks Davis #ifdef PRE7_COMPAT 513e9ed3a70SBrooks Davis struct pci_conf_old pco; 514e9ed3a70SBrooks Davis #ifdef COMPAT_FREEBSD32 515e9ed3a70SBrooks Davis struct pci_conf_old32 pco32; 516e9ed3a70SBrooks Davis #endif 517e9ed3a70SBrooks Davis #endif 518e9ed3a70SBrooks Davis }; 519e9ed3a70SBrooks Davis 520e9ed3a70SBrooks Davis static int 521e9ed3a70SBrooks Davis pci_conf_match(u_long cmd, struct pci_match_conf *matches, int num_matches, 522e9ed3a70SBrooks Davis struct pci_conf *match_buf) 523e9ed3a70SBrooks Davis { 524e9ed3a70SBrooks Davis 525e9ed3a70SBrooks Davis switch (cmd) { 526e9ed3a70SBrooks Davis case PCIOCGETCONF: 527e9ed3a70SBrooks Davis return (pci_conf_match_native( 528e9ed3a70SBrooks Davis (struct pci_match_conf *)matches, num_matches, match_buf)); 529b7edb6faSBrooks Davis #ifdef COMPAT_FREEBSD32 530b7edb6faSBrooks Davis case PCIOCGETCONF32: 531b7edb6faSBrooks Davis return (pci_conf_match32((struct pci_match_conf32 *)matches, 532b7edb6faSBrooks Davis num_matches, match_buf)); 533b7edb6faSBrooks Davis #endif 534e9ed3a70SBrooks Davis #ifdef PRE7_COMPAT 535e9ed3a70SBrooks Davis case PCIOCGETCONF_OLD: 536e9ed3a70SBrooks Davis return (pci_conf_match_old( 537e9ed3a70SBrooks Davis (struct pci_match_conf_old *)matches, num_matches, 538e9ed3a70SBrooks Davis match_buf)); 539e9ed3a70SBrooks Davis #ifdef COMPAT_FREEBSD32 540e9ed3a70SBrooks Davis case PCIOCGETCONF_OLD32: 541e9ed3a70SBrooks Davis return (pci_conf_match_old32( 542e9ed3a70SBrooks Davis (struct pci_match_conf_old32 *)matches, num_matches, 543e9ed3a70SBrooks Davis match_buf)); 544e9ed3a70SBrooks Davis #endif 545e9ed3a70SBrooks Davis #endif 546e9ed3a70SBrooks Davis default: 547e9ed3a70SBrooks Davis /* programmer error */ 548e9ed3a70SBrooks Davis return (0); 549e9ed3a70SBrooks Davis } 550e9ed3a70SBrooks Davis } 55133d3fffaSMarius Strobl 55274aa2d49SJohn Baldwin /* 55374aa2d49SJohn Baldwin * Like PVE_NEXT but takes an explicit length since 'pve' is a user 55474aa2d49SJohn Baldwin * pointer that cannot be dereferenced. 55574aa2d49SJohn Baldwin */ 55674aa2d49SJohn Baldwin #define PVE_NEXT_LEN(pve, datalen) \ 55774aa2d49SJohn Baldwin ((struct pci_vpd_element *)((char *)(pve) + \ 55874aa2d49SJohn Baldwin sizeof(struct pci_vpd_element) + (datalen))) 55974aa2d49SJohn Baldwin 5608983cfbfSMike Smith static int 56184b755dfSJohn Baldwin pci_list_vpd(device_t dev, struct pci_list_vpd_io *lvio) 56284b755dfSJohn Baldwin { 56384b755dfSJohn Baldwin struct pci_vpd_element vpd_element, *vpd_user; 56484b755dfSJohn Baldwin struct pcicfg_vpd *vpd; 56584b755dfSJohn Baldwin size_t len; 56684b755dfSJohn Baldwin int error, i; 56784b755dfSJohn Baldwin 56884b755dfSJohn Baldwin vpd = pci_fetch_vpd_list(dev); 56984b755dfSJohn Baldwin if (vpd->vpd_reg == 0 || vpd->vpd_ident == NULL) 57084b755dfSJohn Baldwin return (ENXIO); 57184b755dfSJohn Baldwin 57284b755dfSJohn Baldwin /* 57384b755dfSJohn Baldwin * Calculate the amount of space needed in the data buffer. An 57484b755dfSJohn Baldwin * identifier element is always present followed by the read-only 57584b755dfSJohn Baldwin * and read-write keywords. 57684b755dfSJohn Baldwin */ 57784b755dfSJohn Baldwin len = sizeof(struct pci_vpd_element) + strlen(vpd->vpd_ident); 57884b755dfSJohn Baldwin for (i = 0; i < vpd->vpd_rocnt; i++) 57984b755dfSJohn Baldwin len += sizeof(struct pci_vpd_element) + vpd->vpd_ros[i].len; 58084b755dfSJohn Baldwin for (i = 0; i < vpd->vpd_wcnt; i++) 58184b755dfSJohn Baldwin len += sizeof(struct pci_vpd_element) + vpd->vpd_w[i].len; 58284b755dfSJohn Baldwin 58384b755dfSJohn Baldwin if (lvio->plvi_len == 0) { 58484b755dfSJohn Baldwin lvio->plvi_len = len; 58584b755dfSJohn Baldwin return (0); 58684b755dfSJohn Baldwin } 58784b755dfSJohn Baldwin if (lvio->plvi_len < len) { 58884b755dfSJohn Baldwin lvio->plvi_len = len; 58984b755dfSJohn Baldwin return (ENOMEM); 59084b755dfSJohn Baldwin } 59184b755dfSJohn Baldwin 59284b755dfSJohn Baldwin /* 59384b755dfSJohn Baldwin * Copyout the identifier string followed by each keyword and 59484b755dfSJohn Baldwin * value. 59584b755dfSJohn Baldwin */ 59684b755dfSJohn Baldwin vpd_user = lvio->plvi_data; 59784b755dfSJohn Baldwin vpd_element.pve_keyword[0] = '\0'; 59884b755dfSJohn Baldwin vpd_element.pve_keyword[1] = '\0'; 59984b755dfSJohn Baldwin vpd_element.pve_flags = PVE_FLAG_IDENT; 60084b755dfSJohn Baldwin vpd_element.pve_datalen = strlen(vpd->vpd_ident); 60184b755dfSJohn Baldwin error = copyout(&vpd_element, vpd_user, sizeof(vpd_element)); 60284b755dfSJohn Baldwin if (error) 60384b755dfSJohn Baldwin return (error); 60484b755dfSJohn Baldwin error = copyout(vpd->vpd_ident, vpd_user->pve_data, 60584b755dfSJohn Baldwin strlen(vpd->vpd_ident)); 60684b755dfSJohn Baldwin if (error) 60784b755dfSJohn Baldwin return (error); 60874aa2d49SJohn Baldwin vpd_user = PVE_NEXT_LEN(vpd_user, vpd_element.pve_datalen); 60984b755dfSJohn Baldwin vpd_element.pve_flags = 0; 61084b755dfSJohn Baldwin for (i = 0; i < vpd->vpd_rocnt; i++) { 61184b755dfSJohn Baldwin vpd_element.pve_keyword[0] = vpd->vpd_ros[i].keyword[0]; 61284b755dfSJohn Baldwin vpd_element.pve_keyword[1] = vpd->vpd_ros[i].keyword[1]; 61384b755dfSJohn Baldwin vpd_element.pve_datalen = vpd->vpd_ros[i].len; 61484b755dfSJohn Baldwin error = copyout(&vpd_element, vpd_user, sizeof(vpd_element)); 61584b755dfSJohn Baldwin if (error) 61684b755dfSJohn Baldwin return (error); 61784b755dfSJohn Baldwin error = copyout(vpd->vpd_ros[i].value, vpd_user->pve_data, 61884b755dfSJohn Baldwin vpd->vpd_ros[i].len); 61984b755dfSJohn Baldwin if (error) 62084b755dfSJohn Baldwin return (error); 62174aa2d49SJohn Baldwin vpd_user = PVE_NEXT_LEN(vpd_user, vpd_element.pve_datalen); 62284b755dfSJohn Baldwin } 62384b755dfSJohn Baldwin vpd_element.pve_flags = PVE_FLAG_RW; 62484b755dfSJohn Baldwin for (i = 0; i < vpd->vpd_wcnt; i++) { 62584b755dfSJohn Baldwin vpd_element.pve_keyword[0] = vpd->vpd_w[i].keyword[0]; 62684b755dfSJohn Baldwin vpd_element.pve_keyword[1] = vpd->vpd_w[i].keyword[1]; 62784b755dfSJohn Baldwin vpd_element.pve_datalen = vpd->vpd_w[i].len; 62884b755dfSJohn Baldwin error = copyout(&vpd_element, vpd_user, sizeof(vpd_element)); 62984b755dfSJohn Baldwin if (error) 63084b755dfSJohn Baldwin return (error); 63184b755dfSJohn Baldwin error = copyout(vpd->vpd_w[i].value, vpd_user->pve_data, 63284b755dfSJohn Baldwin vpd->vpd_w[i].len); 63384b755dfSJohn Baldwin if (error) 63484b755dfSJohn Baldwin return (error); 63574aa2d49SJohn Baldwin vpd_user = PVE_NEXT_LEN(vpd_user, vpd_element.pve_datalen); 63684b755dfSJohn Baldwin } 63784b755dfSJohn Baldwin KASSERT((char *)vpd_user - (char *)lvio->plvi_data == len, 63884b755dfSJohn Baldwin ("length mismatch")); 63984b755dfSJohn Baldwin lvio->plvi_len = len; 64084b755dfSJohn Baldwin return (0); 64184b755dfSJohn Baldwin } 64284b755dfSJohn Baldwin 643e9ed3a70SBrooks Davis static size_t 644e9ed3a70SBrooks Davis pci_match_conf_size(u_long cmd) 6458983cfbfSMike Smith { 6468983cfbfSMike Smith 64784b755dfSJohn Baldwin switch (cmd) { 648e9ed3a70SBrooks Davis case PCIOCGETCONF: 649e9ed3a70SBrooks Davis return (sizeof(struct pci_match_conf)); 650b7edb6faSBrooks Davis #ifdef COMPAT_FREEBSD32 651b7edb6faSBrooks Davis case PCIOCGETCONF32: 652b7edb6faSBrooks Davis return (sizeof(struct pci_match_conf32)); 653b7edb6faSBrooks Davis #endif 65484b755dfSJohn Baldwin #ifdef PRE7_COMPAT 655e9ed3a70SBrooks Davis case PCIOCGETCONF_OLD: 656e9ed3a70SBrooks Davis return (sizeof(struct pci_match_conf_old)); 65784b755dfSJohn Baldwin #ifdef COMPAT_FREEBSD32 65884b755dfSJohn Baldwin case PCIOCGETCONF_OLD32: 659e9ed3a70SBrooks Davis return (sizeof(struct pci_match_conf_old32)); 66084b755dfSJohn Baldwin #endif 661e9ed3a70SBrooks Davis #endif 662e9ed3a70SBrooks Davis default: 663e9ed3a70SBrooks Davis /* programmer error */ 664e9ed3a70SBrooks Davis return (0); 665e9ed3a70SBrooks Davis } 666e9ed3a70SBrooks Davis } 667e9ed3a70SBrooks Davis 668e9ed3a70SBrooks Davis static size_t 669e9ed3a70SBrooks Davis pci_conf_size(u_long cmd) 670e9ed3a70SBrooks Davis { 671e9ed3a70SBrooks Davis 672e9ed3a70SBrooks Davis switch (cmd) { 673e9ed3a70SBrooks Davis case PCIOCGETCONF: 674e9ed3a70SBrooks Davis return (sizeof(struct pci_conf)); 675b7edb6faSBrooks Davis #ifdef COMPAT_FREEBSD32 676b7edb6faSBrooks Davis case PCIOCGETCONF32: 677b7edb6faSBrooks Davis return (sizeof(struct pci_conf32)); 678b7edb6faSBrooks Davis #endif 679e9ed3a70SBrooks Davis #ifdef PRE7_COMPAT 680e9ed3a70SBrooks Davis case PCIOCGETCONF_OLD: 681e9ed3a70SBrooks Davis return (sizeof(struct pci_conf_old)); 682e9ed3a70SBrooks Davis #ifdef COMPAT_FREEBSD32 683e9ed3a70SBrooks Davis case PCIOCGETCONF_OLD32: 684e9ed3a70SBrooks Davis return (sizeof(struct pci_conf_old32)); 685e9ed3a70SBrooks Davis #endif 686e9ed3a70SBrooks Davis #endif 687e9ed3a70SBrooks Davis default: 688e9ed3a70SBrooks Davis /* programmer error */ 689e9ed3a70SBrooks Davis return (0); 690e9ed3a70SBrooks Davis } 691e9ed3a70SBrooks Davis } 692e9ed3a70SBrooks Davis 693e9ed3a70SBrooks Davis static void 694e9ed3a70SBrooks Davis pci_conf_io_init(struct pci_conf_io *cio, caddr_t data, u_long cmd) 695e9ed3a70SBrooks Davis { 696b7edb6faSBrooks Davis #if defined(COMPAT_FREEBSD32) 697e9ed3a70SBrooks Davis struct pci_conf_io32 *cio32; 698e9ed3a70SBrooks Davis #endif 699e9ed3a70SBrooks Davis 700e9ed3a70SBrooks Davis switch (cmd) { 701e9ed3a70SBrooks Davis case PCIOCGETCONF: 702e9ed3a70SBrooks Davis #ifdef PRE7_COMPAT 70384b755dfSJohn Baldwin case PCIOCGETCONF_OLD: 70484b755dfSJohn Baldwin #endif 705e9ed3a70SBrooks Davis *cio = *(struct pci_conf_io *)data; 706e9ed3a70SBrooks Davis return; 70784b755dfSJohn Baldwin 708b7edb6faSBrooks Davis #ifdef COMPAT_FREEBSD32 709b7edb6faSBrooks Davis case PCIOCGETCONF32: 710b7edb6faSBrooks Davis #ifdef PRE7_COMPAT 711b01bf72bSMaxim Sobolev case PCIOCGETCONF_OLD32: 712b7edb6faSBrooks Davis #endif 713b01bf72bSMaxim Sobolev cio32 = (struct pci_conf_io32 *)data; 714b01bf72bSMaxim Sobolev cio->pat_buf_len = cio32->pat_buf_len; 715b01bf72bSMaxim Sobolev cio->num_patterns = cio32->num_patterns; 716b01bf72bSMaxim Sobolev cio->patterns = (void *)(uintptr_t)cio32->patterns; 717b01bf72bSMaxim Sobolev cio->match_buf_len = cio32->match_buf_len; 718b01bf72bSMaxim Sobolev cio->num_matches = cio32->num_matches; 719b01bf72bSMaxim Sobolev cio->matches = (void *)(uintptr_t)cio32->matches; 720b01bf72bSMaxim Sobolev cio->offset = cio32->offset; 721b01bf72bSMaxim Sobolev cio->generation = cio32->generation; 722b01bf72bSMaxim Sobolev cio->status = cio32->status; 723e9ed3a70SBrooks Davis return; 724904c3909SGleb Smirnoff #endif 725e9ed3a70SBrooks Davis 726e9ed3a70SBrooks Davis default: 727e9ed3a70SBrooks Davis /* programmer error */ 728e9ed3a70SBrooks Davis return; 729e9ed3a70SBrooks Davis } 730904c3909SGleb Smirnoff } 731904c3909SGleb Smirnoff 732e9ed3a70SBrooks Davis static void 733e9ed3a70SBrooks Davis pci_conf_io_update_data(const struct pci_conf_io *cio, caddr_t data, 734e9ed3a70SBrooks Davis u_long cmd) 735e9ed3a70SBrooks Davis { 736e9ed3a70SBrooks Davis struct pci_conf_io *d_cio; 737b7edb6faSBrooks Davis #if defined(COMPAT_FREEBSD32) 738e9ed3a70SBrooks Davis struct pci_conf_io32 *cio32; 739e9ed3a70SBrooks Davis #endif 740e9ed3a70SBrooks Davis 741904c3909SGleb Smirnoff switch (cmd) { 742e9ed3a70SBrooks Davis case PCIOCGETCONF: 743e9ed3a70SBrooks Davis #ifdef PRE7_COMPAT 744e9ed3a70SBrooks Davis case PCIOCGETCONF_OLD: 745e9ed3a70SBrooks Davis #endif 746e9ed3a70SBrooks Davis d_cio = (struct pci_conf_io *)data; 747e9ed3a70SBrooks Davis d_cio->status = cio->status; 748e9ed3a70SBrooks Davis d_cio->generation = cio->generation; 749e9ed3a70SBrooks Davis d_cio->offset = cio->offset; 750e9ed3a70SBrooks Davis d_cio->num_matches = cio->num_matches; 751e9ed3a70SBrooks Davis return; 752e9ed3a70SBrooks Davis 753b7edb6faSBrooks Davis #ifdef COMPAT_FREEBSD32 754b7edb6faSBrooks Davis case PCIOCGETCONF32: 755b7edb6faSBrooks Davis #ifdef PRE7_COMPAT 756e9ed3a70SBrooks Davis case PCIOCGETCONF_OLD32: 757b7edb6faSBrooks Davis #endif 758e9ed3a70SBrooks Davis cio32 = (struct pci_conf_io32 *)data; 759e9ed3a70SBrooks Davis 760e9ed3a70SBrooks Davis cio32->status = cio->status; 761e9ed3a70SBrooks Davis cio32->generation = cio->generation; 762e9ed3a70SBrooks Davis cio32->offset = cio->offset; 763e9ed3a70SBrooks Davis cio32->num_matches = cio->num_matches; 764e9ed3a70SBrooks Davis return; 765e9ed3a70SBrooks Davis #endif 766e9ed3a70SBrooks Davis 767e9ed3a70SBrooks Davis default: 768e9ed3a70SBrooks Davis /* programmer error */ 769e9ed3a70SBrooks Davis return; 770e9ed3a70SBrooks Davis } 771e9ed3a70SBrooks Davis } 772e9ed3a70SBrooks Davis 773e9ed3a70SBrooks Davis static void 774e9ed3a70SBrooks Davis pci_conf_for_copyout(const struct pci_conf *pcp, union pci_conf_union *pcup, 775e9ed3a70SBrooks Davis u_long cmd) 776e9ed3a70SBrooks Davis { 777e9ed3a70SBrooks Davis 778e9ed3a70SBrooks Davis memset(pcup, 0, sizeof(*pcup)); 779e9ed3a70SBrooks Davis 780e9ed3a70SBrooks Davis switch (cmd) { 781e9ed3a70SBrooks Davis case PCIOCGETCONF: 782e9ed3a70SBrooks Davis pcup->pc = *pcp; 783e9ed3a70SBrooks Davis return; 784e9ed3a70SBrooks Davis 785b7edb6faSBrooks Davis #ifdef COMPAT_FREEBSD32 786b7edb6faSBrooks Davis case PCIOCGETCONF32: 787b7edb6faSBrooks Davis pcup->pc32.pc_sel = pcp->pc_sel; 788b7edb6faSBrooks Davis pcup->pc32.pc_hdr = pcp->pc_hdr; 789b7edb6faSBrooks Davis pcup->pc32.pc_subvendor = pcp->pc_subvendor; 790b7edb6faSBrooks Davis pcup->pc32.pc_subdevice = pcp->pc_subdevice; 791b7edb6faSBrooks Davis pcup->pc32.pc_vendor = pcp->pc_vendor; 792b7edb6faSBrooks Davis pcup->pc32.pc_device = pcp->pc_device; 793b7edb6faSBrooks Davis pcup->pc32.pc_class = pcp->pc_class; 794b7edb6faSBrooks Davis pcup->pc32.pc_subclass = pcp->pc_subclass; 795b7edb6faSBrooks Davis pcup->pc32.pc_progif = pcp->pc_progif; 796b7edb6faSBrooks Davis pcup->pc32.pc_revid = pcp->pc_revid; 797b7edb6faSBrooks Davis strlcpy(pcup->pc32.pd_name, pcp->pd_name, 798b7edb6faSBrooks Davis sizeof(pcup->pc32.pd_name)); 799b7edb6faSBrooks Davis pcup->pc32.pd_unit = (uint32_t)pcp->pd_unit; 800b7edb6faSBrooks Davis return; 801b7edb6faSBrooks Davis #endif 802b7edb6faSBrooks Davis 803904c3909SGleb Smirnoff #ifdef PRE7_COMPAT 804904c3909SGleb Smirnoff #ifdef COMPAT_FREEBSD32 805904c3909SGleb Smirnoff case PCIOCGETCONF_OLD32: 806e9ed3a70SBrooks Davis pcup->pco32.pc_sel.pc_bus = pcp->pc_sel.pc_bus; 807e9ed3a70SBrooks Davis pcup->pco32.pc_sel.pc_dev = pcp->pc_sel.pc_dev; 808e9ed3a70SBrooks Davis pcup->pco32.pc_sel.pc_func = pcp->pc_sel.pc_func; 809e9ed3a70SBrooks Davis pcup->pco32.pc_hdr = pcp->pc_hdr; 810e9ed3a70SBrooks Davis pcup->pco32.pc_subvendor = pcp->pc_subvendor; 811e9ed3a70SBrooks Davis pcup->pco32.pc_subdevice = pcp->pc_subdevice; 812e9ed3a70SBrooks Davis pcup->pco32.pc_vendor = pcp->pc_vendor; 813e9ed3a70SBrooks Davis pcup->pco32.pc_device = pcp->pc_device; 814e9ed3a70SBrooks Davis pcup->pco32.pc_class = pcp->pc_class; 815e9ed3a70SBrooks Davis pcup->pco32.pc_subclass = pcp->pc_subclass; 816e9ed3a70SBrooks Davis pcup->pco32.pc_progif = pcp->pc_progif; 817e9ed3a70SBrooks Davis pcup->pco32.pc_revid = pcp->pc_revid; 818e9ed3a70SBrooks Davis strlcpy(pcup->pco32.pd_name, pcp->pd_name, 819e9ed3a70SBrooks Davis sizeof(pcup->pco32.pd_name)); 820e9ed3a70SBrooks Davis pcup->pco32.pd_unit = (uint32_t)pcp->pd_unit; 821e9ed3a70SBrooks Davis return; 8228983cfbfSMike Smith 823e9ed3a70SBrooks Davis #endif /* COMPAT_FREEBSD32 */ 824e9ed3a70SBrooks Davis case PCIOCGETCONF_OLD: 825e9ed3a70SBrooks Davis pcup->pco.pc_sel.pc_bus = pcp->pc_sel.pc_bus; 826e9ed3a70SBrooks Davis pcup->pco.pc_sel.pc_dev = pcp->pc_sel.pc_dev; 827e9ed3a70SBrooks Davis pcup->pco.pc_sel.pc_func = pcp->pc_sel.pc_func; 828e9ed3a70SBrooks Davis pcup->pco.pc_hdr = pcp->pc_hdr; 829e9ed3a70SBrooks Davis pcup->pco.pc_subvendor = pcp->pc_subvendor; 830e9ed3a70SBrooks Davis pcup->pco.pc_subdevice = pcp->pc_subdevice; 831e9ed3a70SBrooks Davis pcup->pco.pc_vendor = pcp->pc_vendor; 832e9ed3a70SBrooks Davis pcup->pco.pc_device = pcp->pc_device; 833e9ed3a70SBrooks Davis pcup->pco.pc_class = pcp->pc_class; 834e9ed3a70SBrooks Davis pcup->pco.pc_subclass = pcp->pc_subclass; 835e9ed3a70SBrooks Davis pcup->pco.pc_progif = pcp->pc_progif; 836e9ed3a70SBrooks Davis pcup->pco.pc_revid = pcp->pc_revid; 837e9ed3a70SBrooks Davis strlcpy(pcup->pco.pd_name, pcp->pd_name, 838e9ed3a70SBrooks Davis sizeof(pcup->pco.pd_name)); 839e9ed3a70SBrooks Davis pcup->pco.pd_unit = pcp->pd_unit; 840e9ed3a70SBrooks Davis return; 841e9ed3a70SBrooks Davis #endif /* PRE7_COMPAT */ 842e9ed3a70SBrooks Davis 843e9ed3a70SBrooks Davis default: 844e9ed3a70SBrooks Davis /* programmer error */ 845e9ed3a70SBrooks Davis return; 846e9ed3a70SBrooks Davis } 847e9ed3a70SBrooks Davis } 848e9ed3a70SBrooks Davis 849e9ed3a70SBrooks Davis static int 85087842989SKonstantin Belousov pci_bar_mmap(device_t pcidev, struct pci_bar_mmap *pbm) 85187842989SKonstantin Belousov { 85287842989SKonstantin Belousov vm_map_t map; 85387842989SKonstantin Belousov vm_object_t obj; 85487842989SKonstantin Belousov struct thread *td; 85587842989SKonstantin Belousov struct sglist *sg; 85687842989SKonstantin Belousov struct pci_map *pm; 857f48c41acSHans Petter Selasky vm_paddr_t membase; 85887842989SKonstantin Belousov vm_paddr_t pbase; 85987842989SKonstantin Belousov vm_size_t plen; 86087842989SKonstantin Belousov vm_offset_t addr; 86187842989SKonstantin Belousov vm_prot_t prot; 86287842989SKonstantin Belousov int error, flags; 86387842989SKonstantin Belousov 86487842989SKonstantin Belousov td = curthread; 86587842989SKonstantin Belousov map = &td->td_proc->p_vmspace->vm_map; 86687842989SKonstantin Belousov if ((pbm->pbm_flags & ~(PCIIO_BAR_MMAP_FIXED | PCIIO_BAR_MMAP_EXCL | 86787842989SKonstantin Belousov PCIIO_BAR_MMAP_RW | PCIIO_BAR_MMAP_ACTIVATE)) != 0 || 86887842989SKonstantin Belousov pbm->pbm_memattr != (vm_memattr_t)pbm->pbm_memattr || 86987842989SKonstantin Belousov !pmap_is_valid_memattr(map->pmap, pbm->pbm_memattr)) 87087842989SKonstantin Belousov return (EINVAL); 87187842989SKonstantin Belousov 87287842989SKonstantin Belousov /* Fetch the BAR physical base and length. */ 87387842989SKonstantin Belousov pm = pci_find_bar(pcidev, pbm->pbm_reg); 87487842989SKonstantin Belousov if (pm == NULL) 87587842989SKonstantin Belousov return (EINVAL); 87687842989SKonstantin Belousov if (!pci_bar_enabled(pcidev, pm)) 87787842989SKonstantin Belousov return (EBUSY); /* XXXKIB enable if _ACTIVATE */ 87887842989SKonstantin Belousov if (!PCI_BAR_MEM(pm->pm_value)) 87987842989SKonstantin Belousov return (EIO); 880f48c41acSHans Petter Selasky membase = pm->pm_value & PCIM_BAR_MEM_BASE; 881f48c41acSHans Petter Selasky pbase = trunc_page(membase); 882f48c41acSHans Petter Selasky plen = round_page(membase + ((pci_addr_t)1 << pm->pm_size)) - 88387842989SKonstantin Belousov pbase; 88487842989SKonstantin Belousov prot = VM_PROT_READ | (((pbm->pbm_flags & PCIIO_BAR_MMAP_RW) != 0) ? 88587842989SKonstantin Belousov VM_PROT_WRITE : 0); 88687842989SKonstantin Belousov 88787842989SKonstantin Belousov /* Create vm structures and mmap. */ 88887842989SKonstantin Belousov sg = sglist_alloc(1, M_WAITOK); 88987842989SKonstantin Belousov error = sglist_append_phys(sg, pbase, plen); 89087842989SKonstantin Belousov if (error != 0) 89187842989SKonstantin Belousov goto out; 89287842989SKonstantin Belousov obj = vm_pager_allocate(OBJT_SG, sg, plen, prot, 0, td->td_ucred); 89387842989SKonstantin Belousov if (obj == NULL) { 89487842989SKonstantin Belousov error = EIO; 89587842989SKonstantin Belousov goto out; 89687842989SKonstantin Belousov } 89787842989SKonstantin Belousov obj->memattr = pbm->pbm_memattr; 89887842989SKonstantin Belousov flags = MAP_SHARED; 89987842989SKonstantin Belousov addr = 0; 90087842989SKonstantin Belousov if ((pbm->pbm_flags & PCIIO_BAR_MMAP_FIXED) != 0) { 90187842989SKonstantin Belousov addr = (uintptr_t)pbm->pbm_map_base; 90287842989SKonstantin Belousov flags |= MAP_FIXED; 90387842989SKonstantin Belousov } 90487842989SKonstantin Belousov if ((pbm->pbm_flags & PCIIO_BAR_MMAP_EXCL) != 0) 90587842989SKonstantin Belousov flags |= MAP_CHECK_EXCL; 90687842989SKonstantin Belousov error = vm_mmap_object(map, &addr, plen, prot, prot, flags, obj, 0, 90787842989SKonstantin Belousov FALSE, td); 90887842989SKonstantin Belousov if (error != 0) { 90987842989SKonstantin Belousov vm_object_deallocate(obj); 91087842989SKonstantin Belousov goto out; 91187842989SKonstantin Belousov } 91287842989SKonstantin Belousov pbm->pbm_map_base = (void *)addr; 91387842989SKonstantin Belousov pbm->pbm_map_length = plen; 914f48c41acSHans Petter Selasky pbm->pbm_bar_off = membase - pbase; 91587842989SKonstantin Belousov pbm->pbm_bar_length = (pci_addr_t)1 << pm->pm_size; 91687842989SKonstantin Belousov 91787842989SKonstantin Belousov out: 91887842989SKonstantin Belousov sglist_free(sg); 91987842989SKonstantin Belousov return (error); 92087842989SKonstantin Belousov } 92187842989SKonstantin Belousov 92287842989SKonstantin Belousov static int 923e9ed3a70SBrooks Davis pci_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 924e9ed3a70SBrooks Davis { 925e9ed3a70SBrooks Davis device_t pcidev; 926e9ed3a70SBrooks Davis const char *name; 927e9ed3a70SBrooks Davis struct devlist *devlist_head; 928e9ed3a70SBrooks Davis struct pci_conf_io *cio = NULL; 929e9ed3a70SBrooks Davis struct pci_devinfo *dinfo; 930e9ed3a70SBrooks Davis struct pci_io *io; 931e9ed3a70SBrooks Davis struct pci_bar_io *bio; 932e9ed3a70SBrooks Davis struct pci_list_vpd_io *lvio; 933e9ed3a70SBrooks Davis struct pci_match_conf *pattern_buf; 934e9ed3a70SBrooks Davis struct pci_map *pm; 93587842989SKonstantin Belousov struct pci_bar_mmap *pbm; 936e9ed3a70SBrooks Davis size_t confsz, iolen; 937e9ed3a70SBrooks Davis int error, ionum, i, num_patterns; 938e9ed3a70SBrooks Davis union pci_conf_union pcu; 939e9ed3a70SBrooks Davis #ifdef PRE7_COMPAT 940e9ed3a70SBrooks Davis struct pci_io iodata; 941e9ed3a70SBrooks Davis struct pci_io_old *io_old; 942e9ed3a70SBrooks Davis 943e9ed3a70SBrooks Davis io_old = NULL; 944e9ed3a70SBrooks Davis #endif 945e9ed3a70SBrooks Davis 946e9ed3a70SBrooks Davis if (!(flag & FWRITE)) { 947e9ed3a70SBrooks Davis switch (cmd) { 948e9ed3a70SBrooks Davis case PCIOCGETCONF: 949b56f51f1SBrooks Davis #ifdef COMPAT_FREEBSD32 950b56f51f1SBrooks Davis case PCIOCGETCONF32: 951b56f51f1SBrooks Davis #endif 952e9ed3a70SBrooks Davis #ifdef PRE7_COMPAT 953e9ed3a70SBrooks Davis case PCIOCGETCONF_OLD: 954e9ed3a70SBrooks Davis #ifdef COMPAT_FREEBSD32 955e9ed3a70SBrooks Davis case PCIOCGETCONF_OLD32: 956e9ed3a70SBrooks Davis #endif 957e9ed3a70SBrooks Davis #endif 958e9ed3a70SBrooks Davis case PCIOCGETBAR: 959e9ed3a70SBrooks Davis case PCIOCLISTVPD: 960e9ed3a70SBrooks Davis break; 961e9ed3a70SBrooks Davis default: 962e9ed3a70SBrooks Davis return (EPERM); 963e9ed3a70SBrooks Davis } 964e9ed3a70SBrooks Davis } 965e9ed3a70SBrooks Davis 966e9ed3a70SBrooks Davis 96796b506a5SWarner Losh /* Giant because newbus is Giant locked revisit with newbus locking */ 96896b506a5SWarner Losh mtx_lock(&Giant); 96996b506a5SWarner Losh 970e9ed3a70SBrooks Davis switch (cmd) { 971e9ed3a70SBrooks Davis case PCIOCGETCONF: 972b56f51f1SBrooks Davis #ifdef COMPAT_FREEBSD32 973b56f51f1SBrooks Davis case PCIOCGETCONF32: 974b56f51f1SBrooks Davis #endif 975e9ed3a70SBrooks Davis #ifdef PRE7_COMPAT 976e9ed3a70SBrooks Davis case PCIOCGETCONF_OLD: 977e9ed3a70SBrooks Davis #ifdef COMPAT_FREEBSD32 978e9ed3a70SBrooks Davis case PCIOCGETCONF_OLD32: 979e9ed3a70SBrooks Davis #endif 980e9ed3a70SBrooks Davis #endif 981e9ed3a70SBrooks Davis cio = malloc(sizeof(struct pci_conf_io), M_TEMP, 982e9ed3a70SBrooks Davis M_WAITOK | M_ZERO); 983e9ed3a70SBrooks Davis pci_conf_io_init(cio, data, cmd); 98433d3fffaSMarius Strobl pattern_buf = NULL; 9858983cfbfSMike Smith num_patterns = 0; 9868983cfbfSMike Smith dinfo = NULL; 9878983cfbfSMike Smith 98833d3fffaSMarius Strobl cio->num_matches = 0; 98933d3fffaSMarius Strobl 9908983cfbfSMike Smith /* 9918983cfbfSMike Smith * If the user specified an offset into the device list, 9928983cfbfSMike Smith * but the list has changed since they last called this 9938983cfbfSMike Smith * ioctl, tell them that the list has changed. They will 9948983cfbfSMike Smith * have to get the list from the beginning. 9958983cfbfSMike Smith */ 9968983cfbfSMike Smith if ((cio->offset != 0) 9978983cfbfSMike Smith && (cio->generation != pci_generation)){ 9988983cfbfSMike Smith cio->status = PCI_GETCONF_LIST_CHANGED; 9998983cfbfSMike Smith error = 0; 1000b01bf72bSMaxim Sobolev goto getconfexit; 10018983cfbfSMike Smith } 10028983cfbfSMike Smith 10038983cfbfSMike Smith /* 10048983cfbfSMike Smith * Check to see whether the user has asked for an offset 10058983cfbfSMike Smith * past the end of our list. 10068983cfbfSMike Smith */ 10078983cfbfSMike Smith if (cio->offset >= pci_numdevs) { 10088983cfbfSMike Smith cio->status = PCI_GETCONF_LAST_DEVICE; 10098983cfbfSMike Smith error = 0; 1010b01bf72bSMaxim Sobolev goto getconfexit; 10118983cfbfSMike Smith } 10128983cfbfSMike Smith 10138983cfbfSMike Smith /* get the head of the device queue */ 10148983cfbfSMike Smith devlist_head = &pci_devq; 10158983cfbfSMike Smith 10168983cfbfSMike Smith /* 10178983cfbfSMike Smith * Determine how much room we have for pci_conf structures. 10188983cfbfSMike Smith * Round the user's buffer size down to the nearest 10198983cfbfSMike Smith * multiple of sizeof(struct pci_conf) in case the user 10208983cfbfSMike Smith * didn't specify a multiple of that size. 10218983cfbfSMike Smith */ 1022e9ed3a70SBrooks Davis confsz = pci_conf_size(cmd); 102333d3fffaSMarius Strobl iolen = min(cio->match_buf_len - (cio->match_buf_len % confsz), 102433d3fffaSMarius Strobl pci_numdevs * confsz); 10258983cfbfSMike Smith 10268983cfbfSMike Smith /* 10278983cfbfSMike Smith * Since we know that iolen is a multiple of the size of 10288983cfbfSMike Smith * the pciconf union, it's okay to do this. 10298983cfbfSMike Smith */ 103033d3fffaSMarius Strobl ionum = iolen / confsz; 10318983cfbfSMike Smith 10328983cfbfSMike Smith /* 10338983cfbfSMike Smith * If this test is true, the user wants the pci_conf 10348983cfbfSMike Smith * structures returned to match the supplied entries. 10358983cfbfSMike Smith */ 1036e3f932deSJohn-Mark Gurney if ((cio->num_patterns > 0) && (cio->num_patterns < pci_numdevs) 10378983cfbfSMike Smith && (cio->pat_buf_len > 0)) { 10388983cfbfSMike Smith /* 10398983cfbfSMike Smith * pat_buf_len needs to be: 10408983cfbfSMike Smith * num_patterns * sizeof(struct pci_match_conf) 10418983cfbfSMike Smith * While it is certainly possible the user just 10428983cfbfSMike Smith * allocated a large buffer, but set the number of 10438983cfbfSMike Smith * matches correctly, it is far more likely that 10448983cfbfSMike Smith * their kernel doesn't match the userland utility 10458983cfbfSMike Smith * they're using. It's also possible that the user 10468983cfbfSMike Smith * forgot to initialize some variables. Yes, this 10478983cfbfSMike Smith * may be overly picky, but I hazard to guess that 10488983cfbfSMike Smith * it's far more likely to just catch folks that 10498983cfbfSMike Smith * updated their kernel but not their userland. 10508983cfbfSMike Smith */ 1051e9ed3a70SBrooks Davis if (cio->num_patterns * pci_match_conf_size(cmd) != 1052e9ed3a70SBrooks Davis cio->pat_buf_len) { 105333d3fffaSMarius Strobl /* The user made a mistake, return an error. */ 10548983cfbfSMike Smith cio->status = PCI_GETCONF_ERROR; 10558983cfbfSMike Smith error = EINVAL; 1056b01bf72bSMaxim Sobolev goto getconfexit; 10578983cfbfSMike Smith } 10588983cfbfSMike Smith 10598983cfbfSMike Smith /* 10608983cfbfSMike Smith * Allocate a buffer to hold the patterns. 10618983cfbfSMike Smith */ 10628983cfbfSMike Smith pattern_buf = malloc(cio->pat_buf_len, M_TEMP, 1063a163d034SWarner Losh M_WAITOK); 10648983cfbfSMike Smith error = copyin(cio->patterns, pattern_buf, 10658983cfbfSMike Smith cio->pat_buf_len); 1066d08239c1SJohn-Mark Gurney if (error != 0) { 1067d08239c1SJohn-Mark Gurney error = EINVAL; 1068d08239c1SJohn-Mark Gurney goto getconfexit; 1069d08239c1SJohn-Mark Gurney } 10708983cfbfSMike Smith num_patterns = cio->num_patterns; 10718983cfbfSMike Smith } else if ((cio->num_patterns > 0) 10728983cfbfSMike Smith || (cio->pat_buf_len > 0)) { 10738983cfbfSMike Smith /* 10748983cfbfSMike Smith * The user made a mistake, spit out an error. 10758983cfbfSMike Smith */ 10768983cfbfSMike Smith cio->status = PCI_GETCONF_ERROR; 10778983cfbfSMike Smith error = EINVAL; 1078b01bf72bSMaxim Sobolev goto getconfexit; 107933d3fffaSMarius Strobl } 10808983cfbfSMike Smith 10818983cfbfSMike Smith /* 10828983cfbfSMike Smith * Go through the list of devices and copy out the devices 10838983cfbfSMike Smith * that match the user's criteria. 10848983cfbfSMike Smith */ 108510012d53SJohn Baldwin for (cio->num_matches = 0, i = 0, 10868983cfbfSMike Smith dinfo = STAILQ_FIRST(devlist_head); 108710012d53SJohn Baldwin dinfo != NULL; 10888983cfbfSMike Smith dinfo = STAILQ_NEXT(dinfo, pci_links), i++) { 10898983cfbfSMike Smith 10908983cfbfSMike Smith if (i < cio->offset) 10918983cfbfSMike Smith continue; 10928983cfbfSMike Smith 10938983cfbfSMike Smith /* Populate pd_name and pd_unit */ 10948983cfbfSMike Smith name = NULL; 10950678f786SJohn Baldwin if (dinfo->cfg.dev) 10968983cfbfSMike Smith name = device_get_name(dinfo->cfg.dev); 10978983cfbfSMike Smith if (name) { 10988983cfbfSMike Smith strncpy(dinfo->conf.pd_name, name, 10998983cfbfSMike Smith sizeof(dinfo->conf.pd_name)); 11008983cfbfSMike Smith dinfo->conf.pd_name[PCI_MAXNAMELEN] = 0; 11018983cfbfSMike Smith dinfo->conf.pd_unit = 11028983cfbfSMike Smith device_get_unit(dinfo->cfg.dev); 11030678f786SJohn Baldwin } else { 11040678f786SJohn Baldwin dinfo->conf.pd_name[0] = '\0'; 11050678f786SJohn Baldwin dinfo->conf.pd_unit = 0; 11068983cfbfSMike Smith } 11078983cfbfSMike Smith 110833d3fffaSMarius Strobl if (pattern_buf == NULL || 1109e9ed3a70SBrooks Davis pci_conf_match(cmd, pattern_buf, num_patterns, 111033d3fffaSMarius Strobl &dinfo->conf) == 0) { 11118983cfbfSMike Smith /* 11128983cfbfSMike Smith * If we've filled up the user's buffer, 11138983cfbfSMike Smith * break out at this point. Since we've 11148983cfbfSMike Smith * got a match here, we'll pick right back 11158983cfbfSMike Smith * up at the matching entry. We can also 11168983cfbfSMike Smith * tell the user that there are more matches 11178983cfbfSMike Smith * left. 11188983cfbfSMike Smith */ 1119fadd3f8aSConrad Meyer if (cio->num_matches >= ionum) { 1120fadd3f8aSConrad Meyer error = 0; 11218983cfbfSMike Smith break; 1122fadd3f8aSConrad Meyer } 11238983cfbfSMike Smith 1124e9ed3a70SBrooks Davis pci_conf_for_copyout(&dinfo->conf, &pcu, cmd); 1125e9ed3a70SBrooks Davis error = copyout(&pcu, 1126c5860546SMarius Strobl (caddr_t)cio->matches + 112710012d53SJohn Baldwin confsz * cio->num_matches, confsz); 112810012d53SJohn Baldwin if (error) 112910012d53SJohn Baldwin break; 113033d3fffaSMarius Strobl cio->num_matches++; 11318983cfbfSMike Smith } 1132d08239c1SJohn-Mark Gurney } 11338983cfbfSMike Smith 11348983cfbfSMike Smith /* 11358983cfbfSMike Smith * Set the pointer into the list, so if the user is getting 11368983cfbfSMike Smith * n records at a time, where n < pci_numdevs, 11378983cfbfSMike Smith */ 11388983cfbfSMike Smith cio->offset = i; 11398983cfbfSMike Smith 11408983cfbfSMike Smith /* 11418983cfbfSMike Smith * Set the generation, the user will need this if they make 11428983cfbfSMike Smith * another ioctl call with offset != 0. 11438983cfbfSMike Smith */ 11448983cfbfSMike Smith cio->generation = pci_generation; 11458983cfbfSMike Smith 11468983cfbfSMike Smith /* 11478983cfbfSMike Smith * If this is the last device, inform the user so he won't 11488983cfbfSMike Smith * bother asking for more devices. If dinfo isn't NULL, we 11498983cfbfSMike Smith * know that there are more matches in the list because of 11508983cfbfSMike Smith * the way the traversal is done. 11518983cfbfSMike Smith */ 11528983cfbfSMike Smith if (dinfo == NULL) 11538983cfbfSMike Smith cio->status = PCI_GETCONF_LAST_DEVICE; 11548983cfbfSMike Smith else 11558983cfbfSMike Smith cio->status = PCI_GETCONF_MORE_DEVS; 11568983cfbfSMike Smith 1157d08239c1SJohn-Mark Gurney getconfexit: 1158e9ed3a70SBrooks Davis pci_conf_io_update_data(cio, data, cmd); 1159b01bf72bSMaxim Sobolev free(cio, M_TEMP); 1160904c3909SGleb Smirnoff free(pattern_buf, M_TEMP); 11618983cfbfSMike Smith 11628983cfbfSMike Smith break; 11638983cfbfSMike Smith 1164b2068c0cSWarner Losh #ifdef PRE7_COMPAT 116533d3fffaSMarius Strobl case PCIOCREAD_OLD: 116633d3fffaSMarius Strobl case PCIOCWRITE_OLD: 116733d3fffaSMarius Strobl io_old = (struct pci_io_old *)data; 116833d3fffaSMarius Strobl iodata.pi_sel.pc_domain = 0; 116933d3fffaSMarius Strobl iodata.pi_sel.pc_bus = io_old->pi_sel.pc_bus; 117033d3fffaSMarius Strobl iodata.pi_sel.pc_dev = io_old->pi_sel.pc_dev; 117133d3fffaSMarius Strobl iodata.pi_sel.pc_func = io_old->pi_sel.pc_func; 117233d3fffaSMarius Strobl iodata.pi_reg = io_old->pi_reg; 117333d3fffaSMarius Strobl iodata.pi_width = io_old->pi_width; 117433d3fffaSMarius Strobl iodata.pi_data = io_old->pi_data; 117533d3fffaSMarius Strobl data = (caddr_t)&iodata; 117633d3fffaSMarius Strobl /* FALLTHROUGH */ 117733d3fffaSMarius Strobl #endif 117866f314b5SStefan Eßer case PCIOCREAD: 11798983cfbfSMike Smith case PCIOCWRITE: 11808983cfbfSMike Smith io = (struct pci_io *)data; 11818983cfbfSMike Smith switch(io->pi_width) { 11828983cfbfSMike Smith case 4: 11838983cfbfSMike Smith case 2: 11848983cfbfSMike Smith case 1: 1185d16d35fdSAndriy Gapon /* Make sure register is not negative and aligned. */ 118633d3fffaSMarius Strobl if (io->pi_reg < 0 || 118733d3fffaSMarius Strobl io->pi_reg & (io->pi_width - 1)) { 118866f314b5SStefan Eßer error = EINVAL; 118940ed3f47SRuslan Ermilov break; 119040ed3f47SRuslan Ermilov } 11918983cfbfSMike Smith /* 11928983cfbfSMike Smith * Assume that the user-level bus number is 119337ce43b7SBruce M Simpson * in fact the physical PCI bus number. 119437ce43b7SBruce M Simpson * Look up the grandparent, i.e. the bridge device, 119537ce43b7SBruce M Simpson * so that we can issue configuration space cycles. 11968983cfbfSMike Smith */ 119755aaf894SMarius Strobl pcidev = pci_find_dbsf(io->pi_sel.pc_domain, 119855aaf894SMarius Strobl io->pi_sel.pc_bus, io->pi_sel.pc_dev, 119955aaf894SMarius Strobl io->pi_sel.pc_func); 120037ce43b7SBruce M Simpson if (pcidev) { 1201b2068c0cSWarner Losh #ifdef PRE7_COMPAT 120233d3fffaSMarius Strobl if (cmd == PCIOCWRITE || cmd == PCIOCWRITE_OLD) 120333d3fffaSMarius Strobl #else 120466f314b5SStefan Eßer if (cmd == PCIOCWRITE) 120533d3fffaSMarius Strobl #endif 12065060ec97SRyan Stone pci_write_config(pcidev, 12078983cfbfSMike Smith io->pi_reg, 12088983cfbfSMike Smith io->pi_data, 12098983cfbfSMike Smith io->pi_width); 1210b2068c0cSWarner Losh #ifdef PRE7_COMPAT 121133d3fffaSMarius Strobl else if (cmd == PCIOCREAD_OLD) 121233d3fffaSMarius Strobl io_old->pi_data = 12135060ec97SRyan Stone pci_read_config(pcidev, 121433d3fffaSMarius Strobl io->pi_reg, 121533d3fffaSMarius Strobl io->pi_width); 121633d3fffaSMarius Strobl #endif 121766f314b5SStefan Eßer else 121866f314b5SStefan Eßer io->pi_data = 12195060ec97SRyan Stone pci_read_config(pcidev, 122066f314b5SStefan Eßer io->pi_reg, 122166f314b5SStefan Eßer io->pi_width); 12228983cfbfSMike Smith error = 0; 12238983cfbfSMike Smith } else { 12248910aa92SPaul Saab #ifdef COMPAT_FREEBSD4 122533d3fffaSMarius Strobl if (cmd == PCIOCREAD_OLD) { 122633d3fffaSMarius Strobl io_old->pi_data = -1; 12278910aa92SPaul Saab error = 0; 12288910aa92SPaul Saab } else 12298910aa92SPaul Saab #endif 12308983cfbfSMike Smith error = ENODEV; 12318983cfbfSMike Smith } 12328983cfbfSMike Smith break; 12338983cfbfSMike Smith default: 1234d08239c1SJohn-Mark Gurney error = EINVAL; 12358983cfbfSMike Smith break; 12368983cfbfSMike Smith } 12378983cfbfSMike Smith break; 12388983cfbfSMike Smith 1239da1e0915SJohn Baldwin case PCIOCGETBAR: 1240da1e0915SJohn Baldwin bio = (struct pci_bar_io *)data; 1241da1e0915SJohn Baldwin 1242da1e0915SJohn Baldwin /* 1243da1e0915SJohn Baldwin * Assume that the user-level bus number is 1244da1e0915SJohn Baldwin * in fact the physical PCI bus number. 1245da1e0915SJohn Baldwin */ 1246da1e0915SJohn Baldwin pcidev = pci_find_dbsf(bio->pbi_sel.pc_domain, 1247da1e0915SJohn Baldwin bio->pbi_sel.pc_bus, bio->pbi_sel.pc_dev, 1248da1e0915SJohn Baldwin bio->pbi_sel.pc_func); 1249da1e0915SJohn Baldwin if (pcidev == NULL) { 1250da1e0915SJohn Baldwin error = ENODEV; 1251da1e0915SJohn Baldwin break; 1252da1e0915SJohn Baldwin } 1253a90dd577SJohn Baldwin pm = pci_find_bar(pcidev, bio->pbi_reg); 1254a90dd577SJohn Baldwin if (pm == NULL) { 1255da1e0915SJohn Baldwin error = EINVAL; 1256da1e0915SJohn Baldwin break; 1257da1e0915SJohn Baldwin } 1258a90dd577SJohn Baldwin bio->pbi_base = pm->pm_value; 1259a90dd577SJohn Baldwin bio->pbi_length = (pci_addr_t)1 << pm->pm_size; 1260a90dd577SJohn Baldwin bio->pbi_enabled = pci_bar_enabled(pcidev, pm); 1261da1e0915SJohn Baldwin error = 0; 1262da1e0915SJohn Baldwin break; 1263762aad81SNeel Natu case PCIOCATTACHED: 1264762aad81SNeel Natu error = 0; 1265762aad81SNeel Natu io = (struct pci_io *)data; 1266762aad81SNeel Natu pcidev = pci_find_dbsf(io->pi_sel.pc_domain, io->pi_sel.pc_bus, 1267762aad81SNeel Natu io->pi_sel.pc_dev, io->pi_sel.pc_func); 1268762aad81SNeel Natu if (pcidev != NULL) 1269762aad81SNeel Natu io->pi_data = device_is_attached(pcidev); 1270762aad81SNeel Natu else 1271762aad81SNeel Natu error = ENODEV; 1272762aad81SNeel Natu break; 127384b755dfSJohn Baldwin case PCIOCLISTVPD: 127484b755dfSJohn Baldwin lvio = (struct pci_list_vpd_io *)data; 127584b755dfSJohn Baldwin 127684b755dfSJohn Baldwin /* 127784b755dfSJohn Baldwin * Assume that the user-level bus number is 127884b755dfSJohn Baldwin * in fact the physical PCI bus number. 127984b755dfSJohn Baldwin */ 128084b755dfSJohn Baldwin pcidev = pci_find_dbsf(lvio->plvi_sel.pc_domain, 128184b755dfSJohn Baldwin lvio->plvi_sel.pc_bus, lvio->plvi_sel.pc_dev, 128284b755dfSJohn Baldwin lvio->plvi_sel.pc_func); 128384b755dfSJohn Baldwin if (pcidev == NULL) { 128484b755dfSJohn Baldwin error = ENODEV; 128584b755dfSJohn Baldwin break; 128684b755dfSJohn Baldwin } 128784b755dfSJohn Baldwin error = pci_list_vpd(pcidev, lvio); 128884b755dfSJohn Baldwin break; 128987842989SKonstantin Belousov 129087842989SKonstantin Belousov case PCIOCBARMMAP: 129187842989SKonstantin Belousov pbm = (struct pci_bar_mmap *)data; 129287842989SKonstantin Belousov if ((flag & FWRITE) == 0 && 129396b506a5SWarner Losh (pbm->pbm_flags & PCIIO_BAR_MMAP_RW) != 0) { 129496b506a5SWarner Losh error = EPERM; 129596b506a5SWarner Losh break; 129696b506a5SWarner Losh } 129787842989SKonstantin Belousov pcidev = pci_find_dbsf(pbm->pbm_sel.pc_domain, 129887842989SKonstantin Belousov pbm->pbm_sel.pc_bus, pbm->pbm_sel.pc_dev, 129987842989SKonstantin Belousov pbm->pbm_sel.pc_func); 130087842989SKonstantin Belousov error = pcidev == NULL ? ENODEV : pci_bar_mmap(pcidev, pbm); 130187842989SKonstantin Belousov break; 130287842989SKonstantin Belousov 13038983cfbfSMike Smith default: 13048983cfbfSMike Smith error = ENOTTY; 13058983cfbfSMike Smith break; 13068983cfbfSMike Smith } 13078983cfbfSMike Smith 130896b506a5SWarner Losh mtx_unlock(&Giant); 130996b506a5SWarner Losh 13108983cfbfSMike Smith return (error); 13118983cfbfSMike Smith } 1312