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