1aad970f1SDavid E. O'Brien /*- 28983cfbfSMike Smith * Copyright (c) 1997, Stefan Esser <se@freebsd.org> 38983cfbfSMike Smith * All rights reserved. 48983cfbfSMike Smith * 58983cfbfSMike Smith * Redistribution and use in source and binary forms, with or without 68983cfbfSMike Smith * modification, are permitted provided that the following conditions 78983cfbfSMike Smith * are met: 88983cfbfSMike Smith * 1. Redistributions of source code must retain the above copyright 98983cfbfSMike Smith * notice unmodified, this list of conditions, and the following 108983cfbfSMike Smith * disclaimer. 118983cfbfSMike Smith * 2. Redistributions in binary form must reproduce the above copyright 128983cfbfSMike Smith * notice, this list of conditions and the following disclaimer in the 138983cfbfSMike Smith * documentation and/or other materials provided with the distribution. 148983cfbfSMike Smith * 158983cfbfSMike Smith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 168983cfbfSMike Smith * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 178983cfbfSMike Smith * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 188983cfbfSMike Smith * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 198983cfbfSMike Smith * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 208983cfbfSMike Smith * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 218983cfbfSMike Smith * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 228983cfbfSMike Smith * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 238983cfbfSMike Smith * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 248983cfbfSMike Smith * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 258983cfbfSMike Smith */ 268983cfbfSMike Smith 27aad970f1SDavid E. O'Brien #include <sys/cdefs.h> 28aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 29aad970f1SDavid E. O'Brien 308983cfbfSMike Smith #include "opt_bus.h" /* XXX trim includes */ 318910aa92SPaul Saab #include "opt_compat.h" 328983cfbfSMike Smith 338983cfbfSMike Smith #include <sys/param.h> 348983cfbfSMike Smith #include <sys/systm.h> 358983cfbfSMike Smith #include <sys/malloc.h> 368983cfbfSMike Smith #include <sys/module.h> 378983cfbfSMike Smith #include <sys/linker.h> 388983cfbfSMike Smith #include <sys/fcntl.h> 398983cfbfSMike Smith #include <sys/conf.h> 408983cfbfSMike Smith #include <sys/kernel.h> 418002488bSRobert Watson #include <sys/proc.h> 428983cfbfSMike Smith #include <sys/queue.h> 438983cfbfSMike Smith #include <sys/types.h> 448983cfbfSMike Smith 458983cfbfSMike Smith #include <vm/vm.h> 468983cfbfSMike Smith #include <vm/pmap.h> 478983cfbfSMike Smith #include <vm/vm_extern.h> 488983cfbfSMike Smith 498983cfbfSMike Smith #include <sys/bus.h> 508983cfbfSMike Smith #include <machine/bus.h> 518983cfbfSMike Smith #include <sys/rman.h> 528983cfbfSMike Smith #include <machine/resource.h> 538983cfbfSMike Smith 548983cfbfSMike Smith #include <sys/pciio.h> 5538d8c994SWarner Losh #include <dev/pci/pcireg.h> 5638d8c994SWarner Losh #include <dev/pci/pcivar.h> 578983cfbfSMike Smith 588983cfbfSMike Smith #include "pcib_if.h" 598983cfbfSMike Smith #include "pci_if.h" 608983cfbfSMike Smith 618983cfbfSMike Smith /* 628983cfbfSMike Smith * This is the user interface to PCI configuration space. 638983cfbfSMike Smith */ 648983cfbfSMike Smith 65b40ce416SJulian Elischer static d_open_t pci_open; 66b40ce416SJulian Elischer static d_close_t pci_close; 678983cfbfSMike Smith static int pci_conf_match(struct pci_match_conf *matches, int num_matches, 688983cfbfSMike Smith struct pci_conf *match_buf); 69b40ce416SJulian Elischer static d_ioctl_t pci_ioctl; 708983cfbfSMike Smith 718983cfbfSMike Smith struct cdevsw pcicdev = { 72dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 73dc08ffecSPoul-Henning Kamp .d_flags = D_NEEDGIANT, 747ac40f5fSPoul-Henning Kamp .d_open = pci_open, 757ac40f5fSPoul-Henning Kamp .d_close = pci_close, 767ac40f5fSPoul-Henning Kamp .d_ioctl = pci_ioctl, 777ac40f5fSPoul-Henning Kamp .d_name = "pci", 788983cfbfSMike Smith }; 798983cfbfSMike Smith 808983cfbfSMike Smith static int 8189c9c53dSPoul-Henning Kamp pci_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 828983cfbfSMike Smith { 838002488bSRobert Watson int error; 848002488bSRobert Watson 858002488bSRobert Watson if (oflags & FWRITE) { 86a854ed98SJohn Baldwin error = securelevel_gt(td->td_ucred, 0); 878002488bSRobert Watson if (error) 888002488bSRobert Watson return (error); 898983cfbfSMike Smith } 908002488bSRobert Watson 918002488bSRobert Watson return (0); 928983cfbfSMike Smith } 938983cfbfSMike Smith 948983cfbfSMike Smith static int 9589c9c53dSPoul-Henning Kamp pci_close(struct cdev *dev, int flag, int devtype, struct thread *td) 968983cfbfSMike Smith { 978983cfbfSMike Smith return 0; 988983cfbfSMike Smith } 998983cfbfSMike Smith 1008983cfbfSMike Smith /* 1018983cfbfSMike Smith * Match a single pci_conf structure against an array of pci_match_conf 1028983cfbfSMike Smith * structures. The first argument, 'matches', is an array of num_matches 1038983cfbfSMike Smith * pci_match_conf structures. match_buf is a pointer to the pci_conf 1048983cfbfSMike Smith * structure that will be compared to every entry in the matches array. 1058983cfbfSMike Smith * This function returns 1 on failure, 0 on success. 1068983cfbfSMike Smith */ 1078983cfbfSMike Smith static int 1088983cfbfSMike Smith pci_conf_match(struct pci_match_conf *matches, int num_matches, 1098983cfbfSMike Smith struct pci_conf *match_buf) 1108983cfbfSMike Smith { 1118983cfbfSMike Smith int i; 1128983cfbfSMike Smith 1138983cfbfSMike Smith if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0)) 1148983cfbfSMike Smith return(1); 1158983cfbfSMike Smith 1168983cfbfSMike Smith for (i = 0; i < num_matches; i++) { 1178983cfbfSMike Smith /* 1188983cfbfSMike Smith * I'm not sure why someone would do this...but... 1198983cfbfSMike Smith */ 1208983cfbfSMike Smith if (matches[i].flags == PCI_GETCONF_NO_MATCH) 1218983cfbfSMike Smith continue; 1228983cfbfSMike Smith 1238983cfbfSMike Smith /* 1248983cfbfSMike Smith * Look at each of the match flags. If it's set, do the 1258983cfbfSMike Smith * comparison. If the comparison fails, we don't have a 1268983cfbfSMike Smith * match, go on to the next item if there is one. 1278983cfbfSMike Smith */ 12855aaf894SMarius Strobl if (((matches[i].flags & PCI_GETCONF_MATCH_DOMAIN) != 0) 12955aaf894SMarius Strobl && (match_buf->pc_sel.pc_domain != 13055aaf894SMarius Strobl matches[i].pc_sel.pc_domain)) 13155aaf894SMarius Strobl continue; 13255aaf894SMarius Strobl 1338983cfbfSMike Smith if (((matches[i].flags & PCI_GETCONF_MATCH_BUS) != 0) 1348983cfbfSMike Smith && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus)) 1358983cfbfSMike Smith continue; 1368983cfbfSMike Smith 1378983cfbfSMike Smith if (((matches[i].flags & PCI_GETCONF_MATCH_DEV) != 0) 1388983cfbfSMike Smith && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev)) 1398983cfbfSMike Smith continue; 1408983cfbfSMike Smith 1418983cfbfSMike Smith if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC) != 0) 1428983cfbfSMike Smith && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func)) 1438983cfbfSMike Smith continue; 1448983cfbfSMike Smith 1458983cfbfSMike Smith if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR) != 0) 1468983cfbfSMike Smith && (match_buf->pc_vendor != matches[i].pc_vendor)) 1478983cfbfSMike Smith continue; 1488983cfbfSMike Smith 1498983cfbfSMike Smith if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE) != 0) 1508983cfbfSMike Smith && (match_buf->pc_device != matches[i].pc_device)) 1518983cfbfSMike Smith continue; 1528983cfbfSMike Smith 1538983cfbfSMike Smith if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS) != 0) 1548983cfbfSMike Smith && (match_buf->pc_class != matches[i].pc_class)) 1558983cfbfSMike Smith continue; 1568983cfbfSMike Smith 1578983cfbfSMike Smith if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT) != 0) 1588983cfbfSMike Smith && (match_buf->pd_unit != matches[i].pd_unit)) 1598983cfbfSMike Smith continue; 1608983cfbfSMike Smith 1618983cfbfSMike Smith if (((matches[i].flags & PCI_GETCONF_MATCH_NAME) != 0) 1628983cfbfSMike Smith && (strncmp(matches[i].pd_name, match_buf->pd_name, 1638983cfbfSMike Smith sizeof(match_buf->pd_name)) != 0)) 1648983cfbfSMike Smith continue; 1658983cfbfSMike Smith 1668983cfbfSMike Smith return(0); 1678983cfbfSMike Smith } 1688983cfbfSMike Smith 1698983cfbfSMike Smith return(1); 1708983cfbfSMike Smith } 1718983cfbfSMike Smith 17233d3fffaSMarius Strobl #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \ 17333d3fffaSMarius Strobl defined(COMPAT_FREEBSD6) 174b2068c0cSWarner Losh #define PRE7_COMPAT 17533d3fffaSMarius Strobl 17633d3fffaSMarius Strobl typedef enum { 17733d3fffaSMarius Strobl PCI_GETCONF_NO_MATCH_OLD = 0x00, 17833d3fffaSMarius Strobl PCI_GETCONF_MATCH_BUS_OLD = 0x01, 17933d3fffaSMarius Strobl PCI_GETCONF_MATCH_DEV_OLD = 0x02, 18033d3fffaSMarius Strobl PCI_GETCONF_MATCH_FUNC_OLD = 0x04, 18133d3fffaSMarius Strobl PCI_GETCONF_MATCH_NAME_OLD = 0x08, 18233d3fffaSMarius Strobl PCI_GETCONF_MATCH_UNIT_OLD = 0x10, 18333d3fffaSMarius Strobl PCI_GETCONF_MATCH_VENDOR_OLD = 0x20, 18433d3fffaSMarius Strobl PCI_GETCONF_MATCH_DEVICE_OLD = 0x40, 18533d3fffaSMarius Strobl PCI_GETCONF_MATCH_CLASS_OLD = 0x80 18633d3fffaSMarius Strobl } pci_getconf_flags_old; 18733d3fffaSMarius Strobl 18833d3fffaSMarius Strobl struct pcisel_old { 18933d3fffaSMarius Strobl u_int8_t pc_bus; /* bus number */ 19033d3fffaSMarius Strobl u_int8_t pc_dev; /* device on this bus */ 19133d3fffaSMarius Strobl u_int8_t pc_func; /* function on this device */ 19233d3fffaSMarius Strobl }; 19333d3fffaSMarius Strobl 19433d3fffaSMarius Strobl struct pci_conf_old { 19533d3fffaSMarius Strobl struct pcisel_old pc_sel; /* bus+slot+function */ 19633d3fffaSMarius Strobl u_int8_t pc_hdr; /* PCI header type */ 19733d3fffaSMarius Strobl u_int16_t pc_subvendor; /* card vendor ID */ 19833d3fffaSMarius Strobl u_int16_t pc_subdevice; /* card device ID, assigned by 19933d3fffaSMarius Strobl card vendor */ 20033d3fffaSMarius Strobl u_int16_t pc_vendor; /* chip vendor ID */ 20133d3fffaSMarius Strobl u_int16_t pc_device; /* chip device ID, assigned by 20233d3fffaSMarius Strobl chip vendor */ 20333d3fffaSMarius Strobl u_int8_t pc_class; /* chip PCI class */ 20433d3fffaSMarius Strobl u_int8_t pc_subclass; /* chip PCI subclass */ 20533d3fffaSMarius Strobl u_int8_t pc_progif; /* chip PCI programming interface */ 20633d3fffaSMarius Strobl u_int8_t pc_revid; /* chip revision ID */ 20733d3fffaSMarius Strobl char pd_name[PCI_MAXNAMELEN + 1]; /* device name */ 20833d3fffaSMarius Strobl u_long pd_unit; /* device unit number */ 20933d3fffaSMarius Strobl }; 21033d3fffaSMarius Strobl 21133d3fffaSMarius Strobl struct pci_match_conf_old { 21233d3fffaSMarius Strobl struct pcisel_old pc_sel; /* bus+slot+function */ 21333d3fffaSMarius Strobl char pd_name[PCI_MAXNAMELEN + 1]; /* device name */ 21433d3fffaSMarius Strobl u_long pd_unit; /* Unit number */ 21533d3fffaSMarius Strobl u_int16_t pc_vendor; /* PCI Vendor ID */ 21633d3fffaSMarius Strobl u_int16_t pc_device; /* PCI Device ID */ 21733d3fffaSMarius Strobl u_int8_t pc_class; /* PCI class */ 218c5860546SMarius Strobl pci_getconf_flags_old flags; /* Matching expression */ 21933d3fffaSMarius Strobl }; 22033d3fffaSMarius Strobl 22133d3fffaSMarius Strobl struct pci_io_old { 22233d3fffaSMarius Strobl struct pcisel_old pi_sel; /* device to operate on */ 22333d3fffaSMarius Strobl int pi_reg; /* configuration register to examine */ 22433d3fffaSMarius Strobl int pi_width; /* width (in bytes) of read or write */ 22533d3fffaSMarius Strobl u_int32_t pi_data; /* data to write or result of read */ 22633d3fffaSMarius Strobl }; 22733d3fffaSMarius Strobl 228b01bf72bSMaxim Sobolev #ifdef COMPAT_FREEBSD32 229b01bf72bSMaxim Sobolev struct pci_conf_old32 { 230b01bf72bSMaxim Sobolev struct pcisel_old pc_sel; /* bus+slot+function */ 231e5280830SGleb Smirnoff uint8_t pc_hdr; /* PCI header type */ 232e5280830SGleb Smirnoff uint16_t pc_subvendor; /* card vendor ID */ 233e5280830SGleb Smirnoff uint16_t pc_subdevice; /* card device ID, assigned by 234b01bf72bSMaxim Sobolev card vendor */ 235e5280830SGleb Smirnoff uint16_t pc_vendor; /* chip vendor ID */ 236e5280830SGleb Smirnoff uint16_t pc_device; /* chip device ID, assigned by 237b01bf72bSMaxim Sobolev chip vendor */ 238e5280830SGleb Smirnoff uint8_t pc_class; /* chip PCI class */ 239e5280830SGleb Smirnoff uint8_t pc_subclass; /* chip PCI subclass */ 240e5280830SGleb Smirnoff uint8_t pc_progif; /* chip PCI programming interface */ 241e5280830SGleb Smirnoff uint8_t pc_revid; /* chip revision ID */ 242b01bf72bSMaxim Sobolev char pd_name[PCI_MAXNAMELEN + 1]; /* device name */ 243e5280830SGleb Smirnoff uint32_t pd_unit; /* device unit number (u_long) */ 244b01bf72bSMaxim Sobolev }; 245b01bf72bSMaxim Sobolev 246b01bf72bSMaxim Sobolev struct pci_match_conf_old32 { 247b01bf72bSMaxim Sobolev struct pcisel_old pc_sel; /* bus+slot+function */ 248b01bf72bSMaxim Sobolev char pd_name[PCI_MAXNAMELEN + 1]; /* device name */ 249e5280830SGleb Smirnoff uint32_t pd_unit; /* Unit number (u_long) */ 250e5280830SGleb Smirnoff uint16_t pc_vendor; /* PCI Vendor ID */ 251e5280830SGleb Smirnoff uint16_t pc_device; /* PCI Device ID */ 252e5280830SGleb Smirnoff uint8_t pc_class; /* PCI class */ 253b01bf72bSMaxim Sobolev pci_getconf_flags_old flags; /* Matching expression */ 254b01bf72bSMaxim Sobolev }; 255b01bf72bSMaxim Sobolev 256b01bf72bSMaxim Sobolev struct pci_conf_io32 { 257e5280830SGleb Smirnoff uint32_t pat_buf_len; /* pattern buffer length */ 258e5280830SGleb Smirnoff uint32_t num_patterns; /* number of patterns */ 259e5280830SGleb Smirnoff uint32_t patterns; /* pattern buffer 260e5280830SGleb Smirnoff (struct pci_match_conf_old32 *) */ 261e5280830SGleb Smirnoff uint32_t match_buf_len; /* match buffer length */ 262e5280830SGleb Smirnoff uint32_t num_matches; /* number of matches returned */ 263e5280830SGleb Smirnoff uint32_t matches; /* match buffer 264e5280830SGleb Smirnoff (struct pci_conf_old32 *) */ 265e5280830SGleb Smirnoff uint32_t offset; /* offset into device list */ 266e5280830SGleb Smirnoff uint32_t generation; /* device list generation */ 267b01bf72bSMaxim Sobolev pci_getconf_status status; /* request status */ 268b01bf72bSMaxim Sobolev }; 269b01bf72bSMaxim Sobolev 270b01bf72bSMaxim Sobolev #define PCIOCGETCONF_OLD32 _IOWR('p', 1, struct pci_conf_io32) 271904c3909SGleb Smirnoff #endif /* COMPAT_FREEBSD32 */ 272b01bf72bSMaxim Sobolev 27333d3fffaSMarius Strobl #define PCIOCGETCONF_OLD _IOWR('p', 1, struct pci_conf_io) 27433d3fffaSMarius Strobl #define PCIOCREAD_OLD _IOWR('p', 2, struct pci_io_old) 27533d3fffaSMarius Strobl #define PCIOCWRITE_OLD _IOWR('p', 3, struct pci_io_old) 27633d3fffaSMarius Strobl 27733d3fffaSMarius Strobl static int pci_conf_match_old(struct pci_match_conf_old *matches, 27833d3fffaSMarius Strobl int num_matches, struct pci_conf *match_buf); 27933d3fffaSMarius Strobl 28033d3fffaSMarius Strobl static int 28133d3fffaSMarius Strobl pci_conf_match_old(struct pci_match_conf_old *matches, int num_matches, 28233d3fffaSMarius Strobl struct pci_conf *match_buf) 28333d3fffaSMarius Strobl { 28433d3fffaSMarius Strobl int i; 28533d3fffaSMarius Strobl 28633d3fffaSMarius Strobl if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0)) 28733d3fffaSMarius Strobl return(1); 28833d3fffaSMarius Strobl 28933d3fffaSMarius Strobl for (i = 0; i < num_matches; i++) { 29033d3fffaSMarius Strobl if (match_buf->pc_sel.pc_domain != 0) 29133d3fffaSMarius Strobl continue; 29233d3fffaSMarius Strobl 29333d3fffaSMarius Strobl /* 29433d3fffaSMarius Strobl * I'm not sure why someone would do this...but... 29533d3fffaSMarius Strobl */ 29633d3fffaSMarius Strobl if (matches[i].flags == PCI_GETCONF_NO_MATCH_OLD) 29733d3fffaSMarius Strobl continue; 29833d3fffaSMarius Strobl 29933d3fffaSMarius Strobl /* 30033d3fffaSMarius Strobl * Look at each of the match flags. If it's set, do the 30133d3fffaSMarius Strobl * comparison. If the comparison fails, we don't have a 30233d3fffaSMarius Strobl * match, go on to the next item if there is one. 30333d3fffaSMarius Strobl */ 30433d3fffaSMarius Strobl if (((matches[i].flags & PCI_GETCONF_MATCH_BUS_OLD) != 0) 30533d3fffaSMarius Strobl && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus)) 30633d3fffaSMarius Strobl continue; 30733d3fffaSMarius Strobl 30833d3fffaSMarius Strobl if (((matches[i].flags & PCI_GETCONF_MATCH_DEV_OLD) != 0) 30933d3fffaSMarius Strobl && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev)) 31033d3fffaSMarius Strobl continue; 31133d3fffaSMarius Strobl 31233d3fffaSMarius Strobl if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC_OLD) != 0) 31333d3fffaSMarius Strobl && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func)) 31433d3fffaSMarius Strobl continue; 31533d3fffaSMarius Strobl 31633d3fffaSMarius Strobl if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR_OLD) != 0) 31733d3fffaSMarius Strobl && (match_buf->pc_vendor != matches[i].pc_vendor)) 31833d3fffaSMarius Strobl continue; 31933d3fffaSMarius Strobl 32033d3fffaSMarius Strobl if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE_OLD) != 0) 32133d3fffaSMarius Strobl && (match_buf->pc_device != matches[i].pc_device)) 32233d3fffaSMarius Strobl continue; 32333d3fffaSMarius Strobl 32433d3fffaSMarius Strobl if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS_OLD) != 0) 32533d3fffaSMarius Strobl && (match_buf->pc_class != matches[i].pc_class)) 32633d3fffaSMarius Strobl continue; 32733d3fffaSMarius Strobl 32833d3fffaSMarius Strobl if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT_OLD) != 0) 32933d3fffaSMarius Strobl && (match_buf->pd_unit != matches[i].pd_unit)) 33033d3fffaSMarius Strobl continue; 33133d3fffaSMarius Strobl 33233d3fffaSMarius Strobl if (((matches[i].flags & PCI_GETCONF_MATCH_NAME_OLD) != 0) 33333d3fffaSMarius Strobl && (strncmp(matches[i].pd_name, match_buf->pd_name, 33433d3fffaSMarius Strobl sizeof(match_buf->pd_name)) != 0)) 33533d3fffaSMarius Strobl continue; 33633d3fffaSMarius Strobl 33733d3fffaSMarius Strobl return(0); 33833d3fffaSMarius Strobl } 33933d3fffaSMarius Strobl 34033d3fffaSMarius Strobl return(1); 34133d3fffaSMarius Strobl } 34233d3fffaSMarius Strobl 343904c3909SGleb Smirnoff #ifdef COMPAT_FREEBSD32 344b01bf72bSMaxim Sobolev static int 345b01bf72bSMaxim Sobolev pci_conf_match_old32(struct pci_match_conf_old32 *matches, int num_matches, 346b01bf72bSMaxim Sobolev struct pci_conf *match_buf) 347b01bf72bSMaxim Sobolev { 348b01bf72bSMaxim Sobolev int i; 349b01bf72bSMaxim Sobolev 350b01bf72bSMaxim Sobolev if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0)) 351b01bf72bSMaxim Sobolev return(1); 352b01bf72bSMaxim Sobolev 353b01bf72bSMaxim Sobolev for (i = 0; i < num_matches; i++) { 354b01bf72bSMaxim Sobolev if (match_buf->pc_sel.pc_domain != 0) 355b01bf72bSMaxim Sobolev continue; 356b01bf72bSMaxim Sobolev 357b01bf72bSMaxim Sobolev /* 358b01bf72bSMaxim Sobolev * I'm not sure why someone would do this...but... 359b01bf72bSMaxim Sobolev */ 360b01bf72bSMaxim Sobolev if (matches[i].flags == PCI_GETCONF_NO_MATCH_OLD) 361b01bf72bSMaxim Sobolev continue; 362b01bf72bSMaxim Sobolev 363b01bf72bSMaxim Sobolev /* 364b01bf72bSMaxim Sobolev * Look at each of the match flags. If it's set, do the 365b01bf72bSMaxim Sobolev * comparison. If the comparison fails, we don't have a 366b01bf72bSMaxim Sobolev * match, go on to the next item if there is one. 367b01bf72bSMaxim Sobolev */ 368e5280830SGleb Smirnoff if (((matches[i].flags & PCI_GETCONF_MATCH_BUS_OLD) != 0) && 369e5280830SGleb Smirnoff (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus)) 370b01bf72bSMaxim Sobolev continue; 371b01bf72bSMaxim Sobolev 372e5280830SGleb Smirnoff if (((matches[i].flags & PCI_GETCONF_MATCH_DEV_OLD) != 0) && 373e5280830SGleb Smirnoff (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev)) 374b01bf72bSMaxim Sobolev continue; 375b01bf72bSMaxim Sobolev 376e5280830SGleb Smirnoff if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC_OLD) != 0) && 377e5280830SGleb Smirnoff (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func)) 378b01bf72bSMaxim Sobolev continue; 379b01bf72bSMaxim Sobolev 380e5280830SGleb Smirnoff if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR_OLD) != 0) && 381e5280830SGleb Smirnoff (match_buf->pc_vendor != matches[i].pc_vendor)) 382b01bf72bSMaxim Sobolev continue; 383b01bf72bSMaxim Sobolev 384e5280830SGleb Smirnoff if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE_OLD) != 0) && 385e5280830SGleb Smirnoff (match_buf->pc_device != matches[i].pc_device)) 386b01bf72bSMaxim Sobolev continue; 387b01bf72bSMaxim Sobolev 388e5280830SGleb Smirnoff if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS_OLD) != 0) && 389e5280830SGleb Smirnoff (match_buf->pc_class != matches[i].pc_class)) 390b01bf72bSMaxim Sobolev continue; 391b01bf72bSMaxim Sobolev 392e5280830SGleb Smirnoff if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT_OLD) != 0) && 393e5280830SGleb Smirnoff ((u_int32_t)match_buf->pd_unit != matches[i].pd_unit)) 394b01bf72bSMaxim Sobolev continue; 395b01bf72bSMaxim Sobolev 396e5280830SGleb Smirnoff if (((matches[i].flags & PCI_GETCONF_MATCH_NAME_OLD) != 0) && 397e5280830SGleb Smirnoff (strncmp(matches[i].pd_name, match_buf->pd_name, 398b01bf72bSMaxim Sobolev sizeof(match_buf->pd_name)) != 0)) 399b01bf72bSMaxim Sobolev continue; 400b01bf72bSMaxim Sobolev 401b01bf72bSMaxim Sobolev return (0); 402b01bf72bSMaxim Sobolev } 403b01bf72bSMaxim Sobolev 404b01bf72bSMaxim Sobolev return (1); 405b01bf72bSMaxim Sobolev } 406904c3909SGleb Smirnoff #endif /* COMPAT_FREEBSD32 */ 407904c3909SGleb Smirnoff #endif /* PRE7_COMPAT */ 40833d3fffaSMarius Strobl 4098983cfbfSMike Smith static int 410*84b755dfSJohn Baldwin pci_list_vpd(device_t dev, struct pci_list_vpd_io *lvio) 411*84b755dfSJohn Baldwin { 412*84b755dfSJohn Baldwin struct pci_vpd_element vpd_element, *vpd_user; 413*84b755dfSJohn Baldwin struct pcicfg_vpd *vpd; 414*84b755dfSJohn Baldwin size_t len; 415*84b755dfSJohn Baldwin int error, i; 416*84b755dfSJohn Baldwin 417*84b755dfSJohn Baldwin vpd = pci_fetch_vpd_list(dev); 418*84b755dfSJohn Baldwin if (vpd->vpd_reg == 0 || vpd->vpd_ident == NULL) 419*84b755dfSJohn Baldwin return (ENXIO); 420*84b755dfSJohn Baldwin 421*84b755dfSJohn Baldwin /* 422*84b755dfSJohn Baldwin * Calculate the amount of space needed in the data buffer. An 423*84b755dfSJohn Baldwin * identifier element is always present followed by the read-only 424*84b755dfSJohn Baldwin * and read-write keywords. 425*84b755dfSJohn Baldwin */ 426*84b755dfSJohn Baldwin len = sizeof(struct pci_vpd_element) + strlen(vpd->vpd_ident); 427*84b755dfSJohn Baldwin for (i = 0; i < vpd->vpd_rocnt; i++) 428*84b755dfSJohn Baldwin len += sizeof(struct pci_vpd_element) + vpd->vpd_ros[i].len; 429*84b755dfSJohn Baldwin for (i = 0; i < vpd->vpd_wcnt; i++) 430*84b755dfSJohn Baldwin len += sizeof(struct pci_vpd_element) + vpd->vpd_w[i].len; 431*84b755dfSJohn Baldwin 432*84b755dfSJohn Baldwin if (lvio->plvi_len == 0) { 433*84b755dfSJohn Baldwin lvio->plvi_len = len; 434*84b755dfSJohn Baldwin return (0); 435*84b755dfSJohn Baldwin } 436*84b755dfSJohn Baldwin if (lvio->plvi_len < len) { 437*84b755dfSJohn Baldwin lvio->plvi_len = len; 438*84b755dfSJohn Baldwin return (ENOMEM); 439*84b755dfSJohn Baldwin } 440*84b755dfSJohn Baldwin 441*84b755dfSJohn Baldwin /* 442*84b755dfSJohn Baldwin * Copyout the identifier string followed by each keyword and 443*84b755dfSJohn Baldwin * value. 444*84b755dfSJohn Baldwin */ 445*84b755dfSJohn Baldwin vpd_user = lvio->plvi_data; 446*84b755dfSJohn Baldwin vpd_element.pve_keyword[0] = '\0'; 447*84b755dfSJohn Baldwin vpd_element.pve_keyword[1] = '\0'; 448*84b755dfSJohn Baldwin vpd_element.pve_flags = PVE_FLAG_IDENT; 449*84b755dfSJohn Baldwin vpd_element.pve_datalen = strlen(vpd->vpd_ident); 450*84b755dfSJohn Baldwin error = copyout(&vpd_element, vpd_user, sizeof(vpd_element)); 451*84b755dfSJohn Baldwin if (error) 452*84b755dfSJohn Baldwin return (error); 453*84b755dfSJohn Baldwin error = copyout(vpd->vpd_ident, vpd_user->pve_data, 454*84b755dfSJohn Baldwin strlen(vpd->vpd_ident)); 455*84b755dfSJohn Baldwin if (error) 456*84b755dfSJohn Baldwin return (error); 457*84b755dfSJohn Baldwin vpd_user = PVE_NEXT(vpd_user); 458*84b755dfSJohn Baldwin vpd_element.pve_flags = 0; 459*84b755dfSJohn Baldwin for (i = 0; i < vpd->vpd_rocnt; i++) { 460*84b755dfSJohn Baldwin vpd_element.pve_keyword[0] = vpd->vpd_ros[i].keyword[0]; 461*84b755dfSJohn Baldwin vpd_element.pve_keyword[1] = vpd->vpd_ros[i].keyword[1]; 462*84b755dfSJohn Baldwin vpd_element.pve_datalen = vpd->vpd_ros[i].len; 463*84b755dfSJohn Baldwin error = copyout(&vpd_element, vpd_user, sizeof(vpd_element)); 464*84b755dfSJohn Baldwin if (error) 465*84b755dfSJohn Baldwin return (error); 466*84b755dfSJohn Baldwin error = copyout(vpd->vpd_ros[i].value, vpd_user->pve_data, 467*84b755dfSJohn Baldwin vpd->vpd_ros[i].len); 468*84b755dfSJohn Baldwin if (error) 469*84b755dfSJohn Baldwin return (error); 470*84b755dfSJohn Baldwin vpd_user = PVE_NEXT(vpd_user); 471*84b755dfSJohn Baldwin } 472*84b755dfSJohn Baldwin vpd_element.pve_flags = PVE_FLAG_RW; 473*84b755dfSJohn Baldwin for (i = 0; i < vpd->vpd_wcnt; i++) { 474*84b755dfSJohn Baldwin vpd_element.pve_keyword[0] = vpd->vpd_w[i].keyword[0]; 475*84b755dfSJohn Baldwin vpd_element.pve_keyword[1] = vpd->vpd_w[i].keyword[1]; 476*84b755dfSJohn Baldwin vpd_element.pve_datalen = vpd->vpd_w[i].len; 477*84b755dfSJohn Baldwin error = copyout(&vpd_element, vpd_user, sizeof(vpd_element)); 478*84b755dfSJohn Baldwin if (error) 479*84b755dfSJohn Baldwin return (error); 480*84b755dfSJohn Baldwin error = copyout(vpd->vpd_w[i].value, vpd_user->pve_data, 481*84b755dfSJohn Baldwin vpd->vpd_w[i].len); 482*84b755dfSJohn Baldwin if (error) 483*84b755dfSJohn Baldwin return (error); 484*84b755dfSJohn Baldwin vpd_user = PVE_NEXT(vpd_user); 485*84b755dfSJohn Baldwin } 486*84b755dfSJohn Baldwin KASSERT((char *)vpd_user - (char *)lvio->plvi_data == len, 487*84b755dfSJohn Baldwin ("length mismatch")); 488*84b755dfSJohn Baldwin lvio->plvi_len = len; 489*84b755dfSJohn Baldwin return (0); 490*84b755dfSJohn Baldwin } 491*84b755dfSJohn Baldwin 492*84b755dfSJohn Baldwin static int 49389c9c53dSPoul-Henning Kamp pci_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 4948983cfbfSMike Smith { 49533d3fffaSMarius Strobl device_t pcidev, brdev; 49633d3fffaSMarius Strobl void *confdata; 4978983cfbfSMike Smith const char *name; 49833d3fffaSMarius Strobl struct devlist *devlist_head; 499904c3909SGleb Smirnoff struct pci_conf_io *cio = NULL; 50033d3fffaSMarius Strobl struct pci_devinfo *dinfo; 50133d3fffaSMarius Strobl struct pci_io *io; 502da1e0915SJohn Baldwin struct pci_bar_io *bio; 503*84b755dfSJohn Baldwin struct pci_list_vpd_io *lvio; 50433d3fffaSMarius Strobl struct pci_match_conf *pattern_buf; 505a90dd577SJohn Baldwin struct pci_map *pm; 50633d3fffaSMarius Strobl size_t confsz, iolen, pbufsz; 50733d3fffaSMarius Strobl int error, ionum, i, num_patterns; 508b2068c0cSWarner Losh #ifdef PRE7_COMPAT 509b01bf72bSMaxim Sobolev #ifdef COMPAT_FREEBSD32 510904c3909SGleb Smirnoff struct pci_conf_io32 *cio32 = NULL; 511904c3909SGleb Smirnoff struct pci_conf_old32 conf_old32; 512fee3029cSDavid Xu struct pci_match_conf_old32 *pattern_buf_old32 = NULL; 513b01bf72bSMaxim Sobolev #endif 51433d3fffaSMarius Strobl struct pci_conf_old conf_old; 51533d3fffaSMarius Strobl struct pci_io iodata; 51633d3fffaSMarius Strobl struct pci_io_old *io_old; 517fee3029cSDavid Xu struct pci_match_conf_old *pattern_buf_old = NULL; 5188983cfbfSMike Smith 51933d3fffaSMarius Strobl io_old = NULL; 52033d3fffaSMarius Strobl #endif 5218983cfbfSMike Smith 522*84b755dfSJohn Baldwin if (!(flag & FWRITE)) { 523*84b755dfSJohn Baldwin switch (cmd) { 524*84b755dfSJohn Baldwin #ifdef PRE7_COMPAT 525*84b755dfSJohn Baldwin #ifdef COMPAT_FREEBSD32 526*84b755dfSJohn Baldwin case PCIOCGETCONF_OLD32: 527*84b755dfSJohn Baldwin #endif 528*84b755dfSJohn Baldwin case PCIOCGETCONF_OLD: 529*84b755dfSJohn Baldwin #endif 530*84b755dfSJohn Baldwin case PCIOCGETCONF: 531*84b755dfSJohn Baldwin case PCIOCGETBAR: 532*84b755dfSJohn Baldwin case PCIOCLISTVPD: 533*84b755dfSJohn Baldwin break; 534*84b755dfSJohn Baldwin default: 535*84b755dfSJohn Baldwin return (EPERM); 536*84b755dfSJohn Baldwin } 537*84b755dfSJohn Baldwin } 538*84b755dfSJohn Baldwin 5398983cfbfSMike Smith switch (cmd) { 540b2068c0cSWarner Losh #ifdef PRE7_COMPAT 541904c3909SGleb Smirnoff #ifdef COMPAT_FREEBSD32 542b01bf72bSMaxim Sobolev case PCIOCGETCONF_OLD32: 543b01bf72bSMaxim Sobolev cio32 = (struct pci_conf_io32 *)data; 544b01bf72bSMaxim Sobolev cio = malloc(sizeof(struct pci_conf_io), M_TEMP, M_WAITOK); 545b01bf72bSMaxim Sobolev cio->pat_buf_len = cio32->pat_buf_len; 546b01bf72bSMaxim Sobolev cio->num_patterns = cio32->num_patterns; 547b01bf72bSMaxim Sobolev cio->patterns = (void *)(uintptr_t)cio32->patterns; 548b01bf72bSMaxim Sobolev cio->match_buf_len = cio32->match_buf_len; 549b01bf72bSMaxim Sobolev cio->num_matches = cio32->num_matches; 550b01bf72bSMaxim Sobolev cio->matches = (void *)(uintptr_t)cio32->matches; 551b01bf72bSMaxim Sobolev cio->offset = cio32->offset; 552b01bf72bSMaxim Sobolev cio->generation = cio32->generation; 553b01bf72bSMaxim Sobolev cio->status = cio32->status; 554b01bf72bSMaxim Sobolev cio32->num_matches = 0; 555904c3909SGleb Smirnoff break; 556904c3909SGleb Smirnoff #endif 55733d3fffaSMarius Strobl case PCIOCGETCONF_OLD: 55833d3fffaSMarius Strobl #endif 5598983cfbfSMike Smith case PCIOCGETCONF: 5608983cfbfSMike Smith cio = (struct pci_conf_io *)data; 561904c3909SGleb Smirnoff } 562904c3909SGleb Smirnoff 563904c3909SGleb Smirnoff switch (cmd) { 564904c3909SGleb Smirnoff #ifdef PRE7_COMPAT 565904c3909SGleb Smirnoff #ifdef COMPAT_FREEBSD32 566904c3909SGleb Smirnoff case PCIOCGETCONF_OLD32: 567904c3909SGleb Smirnoff #endif 568904c3909SGleb Smirnoff case PCIOCGETCONF_OLD: 569904c3909SGleb Smirnoff #endif 570904c3909SGleb Smirnoff case PCIOCGETCONF: 5718983cfbfSMike Smith 57233d3fffaSMarius Strobl pattern_buf = NULL; 5738983cfbfSMike Smith num_patterns = 0; 5748983cfbfSMike Smith dinfo = NULL; 5758983cfbfSMike Smith 57633d3fffaSMarius Strobl cio->num_matches = 0; 57733d3fffaSMarius Strobl 5788983cfbfSMike Smith /* 5798983cfbfSMike Smith * If the user specified an offset into the device list, 5808983cfbfSMike Smith * but the list has changed since they last called this 5818983cfbfSMike Smith * ioctl, tell them that the list has changed. They will 5828983cfbfSMike Smith * have to get the list from the beginning. 5838983cfbfSMike Smith */ 5848983cfbfSMike Smith if ((cio->offset != 0) 5858983cfbfSMike Smith && (cio->generation != pci_generation)){ 5868983cfbfSMike Smith cio->status = PCI_GETCONF_LIST_CHANGED; 5878983cfbfSMike Smith error = 0; 588b01bf72bSMaxim Sobolev goto getconfexit; 5898983cfbfSMike Smith } 5908983cfbfSMike Smith 5918983cfbfSMike Smith /* 5928983cfbfSMike Smith * Check to see whether the user has asked for an offset 5938983cfbfSMike Smith * past the end of our list. 5948983cfbfSMike Smith */ 5958983cfbfSMike Smith if (cio->offset >= pci_numdevs) { 5968983cfbfSMike Smith cio->status = PCI_GETCONF_LAST_DEVICE; 5978983cfbfSMike Smith error = 0; 598b01bf72bSMaxim Sobolev goto getconfexit; 5998983cfbfSMike Smith } 6008983cfbfSMike Smith 6018983cfbfSMike Smith /* get the head of the device queue */ 6028983cfbfSMike Smith devlist_head = &pci_devq; 6038983cfbfSMike Smith 6048983cfbfSMike Smith /* 6058983cfbfSMike Smith * Determine how much room we have for pci_conf structures. 6068983cfbfSMike Smith * Round the user's buffer size down to the nearest 6078983cfbfSMike Smith * multiple of sizeof(struct pci_conf) in case the user 6088983cfbfSMike Smith * didn't specify a multiple of that size. 6098983cfbfSMike Smith */ 610b2068c0cSWarner Losh #ifdef PRE7_COMPAT 611b01bf72bSMaxim Sobolev #ifdef COMPAT_FREEBSD32 612b01bf72bSMaxim Sobolev if (cmd == PCIOCGETCONF_OLD32) 613b01bf72bSMaxim Sobolev confsz = sizeof(struct pci_conf_old32); 614b01bf72bSMaxim Sobolev else 615b01bf72bSMaxim Sobolev #endif 61633d3fffaSMarius Strobl if (cmd == PCIOCGETCONF_OLD) 61733d3fffaSMarius Strobl confsz = sizeof(struct pci_conf_old); 61833d3fffaSMarius Strobl else 61933d3fffaSMarius Strobl #endif 62033d3fffaSMarius Strobl confsz = sizeof(struct pci_conf); 62133d3fffaSMarius Strobl iolen = min(cio->match_buf_len - (cio->match_buf_len % confsz), 62233d3fffaSMarius Strobl pci_numdevs * confsz); 6238983cfbfSMike Smith 6248983cfbfSMike Smith /* 6258983cfbfSMike Smith * Since we know that iolen is a multiple of the size of 6268983cfbfSMike Smith * the pciconf union, it's okay to do this. 6278983cfbfSMike Smith */ 62833d3fffaSMarius Strobl ionum = iolen / confsz; 6298983cfbfSMike Smith 6308983cfbfSMike Smith /* 6318983cfbfSMike Smith * If this test is true, the user wants the pci_conf 6328983cfbfSMike Smith * structures returned to match the supplied entries. 6338983cfbfSMike Smith */ 634e3f932deSJohn-Mark Gurney if ((cio->num_patterns > 0) && (cio->num_patterns < pci_numdevs) 6358983cfbfSMike Smith && (cio->pat_buf_len > 0)) { 6368983cfbfSMike Smith /* 6378983cfbfSMike Smith * pat_buf_len needs to be: 6388983cfbfSMike Smith * num_patterns * sizeof(struct pci_match_conf) 6398983cfbfSMike Smith * While it is certainly possible the user just 6408983cfbfSMike Smith * allocated a large buffer, but set the number of 6418983cfbfSMike Smith * matches correctly, it is far more likely that 6428983cfbfSMike Smith * their kernel doesn't match the userland utility 6438983cfbfSMike Smith * they're using. It's also possible that the user 6448983cfbfSMike Smith * forgot to initialize some variables. Yes, this 6458983cfbfSMike Smith * may be overly picky, but I hazard to guess that 6468983cfbfSMike Smith * it's far more likely to just catch folks that 6478983cfbfSMike Smith * updated their kernel but not their userland. 6488983cfbfSMike Smith */ 649b2068c0cSWarner Losh #ifdef PRE7_COMPAT 650b01bf72bSMaxim Sobolev #ifdef COMPAT_FREEBSD32 651b01bf72bSMaxim Sobolev if (cmd == PCIOCGETCONF_OLD32) 652b01bf72bSMaxim Sobolev pbufsz = sizeof(struct pci_match_conf_old32); 653b01bf72bSMaxim Sobolev else 654b01bf72bSMaxim Sobolev #endif 65533d3fffaSMarius Strobl if (cmd == PCIOCGETCONF_OLD) 65633d3fffaSMarius Strobl pbufsz = sizeof(struct pci_match_conf_old); 65733d3fffaSMarius Strobl else 65833d3fffaSMarius Strobl #endif 65933d3fffaSMarius Strobl pbufsz = sizeof(struct pci_match_conf); 66033d3fffaSMarius Strobl if (cio->num_patterns * pbufsz != cio->pat_buf_len) { 66133d3fffaSMarius Strobl /* The user made a mistake, return an error. */ 6628983cfbfSMike Smith cio->status = PCI_GETCONF_ERROR; 6638983cfbfSMike Smith error = EINVAL; 664b01bf72bSMaxim Sobolev goto getconfexit; 6658983cfbfSMike Smith } 6668983cfbfSMike Smith 6678983cfbfSMike Smith /* 6688983cfbfSMike Smith * Allocate a buffer to hold the patterns. 6698983cfbfSMike Smith */ 670b2068c0cSWarner Losh #ifdef PRE7_COMPAT 671904c3909SGleb Smirnoff #ifdef COMPAT_FREEBSD32 672b01bf72bSMaxim Sobolev if (cmd == PCIOCGETCONF_OLD32) { 673b01bf72bSMaxim Sobolev pattern_buf_old32 = malloc(cio->pat_buf_len, 674b01bf72bSMaxim Sobolev M_TEMP, M_WAITOK); 675b01bf72bSMaxim Sobolev error = copyin(cio->patterns, 676b01bf72bSMaxim Sobolev pattern_buf_old32, cio->pat_buf_len); 677b01bf72bSMaxim Sobolev } else 678904c3909SGleb Smirnoff #endif /* COMPAT_FREEBSD32 */ 67933d3fffaSMarius Strobl if (cmd == PCIOCGETCONF_OLD) { 68033d3fffaSMarius Strobl pattern_buf_old = malloc(cio->pat_buf_len, 68133d3fffaSMarius Strobl M_TEMP, M_WAITOK); 68233d3fffaSMarius Strobl error = copyin(cio->patterns, 68333d3fffaSMarius Strobl pattern_buf_old, cio->pat_buf_len); 68433d3fffaSMarius Strobl } else 685904c3909SGleb Smirnoff #endif /* PRE7_COMPAT */ 68633d3fffaSMarius Strobl { 6878983cfbfSMike Smith pattern_buf = malloc(cio->pat_buf_len, M_TEMP, 688a163d034SWarner Losh M_WAITOK); 6898983cfbfSMike Smith error = copyin(cio->patterns, pattern_buf, 6908983cfbfSMike Smith cio->pat_buf_len); 69133d3fffaSMarius Strobl } 692d08239c1SJohn-Mark Gurney if (error != 0) { 693d08239c1SJohn-Mark Gurney error = EINVAL; 694d08239c1SJohn-Mark Gurney goto getconfexit; 695d08239c1SJohn-Mark Gurney } 6968983cfbfSMike Smith num_patterns = cio->num_patterns; 6978983cfbfSMike Smith } else if ((cio->num_patterns > 0) 6988983cfbfSMike Smith || (cio->pat_buf_len > 0)) { 6998983cfbfSMike Smith /* 7008983cfbfSMike Smith * The user made a mistake, spit out an error. 7018983cfbfSMike Smith */ 7028983cfbfSMike Smith cio->status = PCI_GETCONF_ERROR; 7038983cfbfSMike Smith error = EINVAL; 704b01bf72bSMaxim Sobolev goto getconfexit; 70533d3fffaSMarius Strobl } 7068983cfbfSMike Smith 7078983cfbfSMike Smith /* 7088983cfbfSMike Smith * Go through the list of devices and copy out the devices 7098983cfbfSMike Smith * that match the user's criteria. 7108983cfbfSMike Smith */ 7118983cfbfSMike Smith for (cio->num_matches = 0, error = 0, i = 0, 7128983cfbfSMike Smith dinfo = STAILQ_FIRST(devlist_head); 7138983cfbfSMike Smith (dinfo != NULL) && (cio->num_matches < ionum) 714d08239c1SJohn-Mark Gurney && (error == 0) && (i < pci_numdevs) && (dinfo != NULL); 7158983cfbfSMike Smith dinfo = STAILQ_NEXT(dinfo, pci_links), i++) { 7168983cfbfSMike Smith 7178983cfbfSMike Smith if (i < cio->offset) 7188983cfbfSMike Smith continue; 7198983cfbfSMike Smith 7208983cfbfSMike Smith /* Populate pd_name and pd_unit */ 7218983cfbfSMike Smith name = NULL; 7220678f786SJohn Baldwin if (dinfo->cfg.dev) 7238983cfbfSMike Smith name = device_get_name(dinfo->cfg.dev); 7248983cfbfSMike Smith if (name) { 7258983cfbfSMike Smith strncpy(dinfo->conf.pd_name, name, 7268983cfbfSMike Smith sizeof(dinfo->conf.pd_name)); 7278983cfbfSMike Smith dinfo->conf.pd_name[PCI_MAXNAMELEN] = 0; 7288983cfbfSMike Smith dinfo->conf.pd_unit = 7298983cfbfSMike Smith device_get_unit(dinfo->cfg.dev); 7300678f786SJohn Baldwin } else { 7310678f786SJohn Baldwin dinfo->conf.pd_name[0] = '\0'; 7320678f786SJohn Baldwin dinfo->conf.pd_unit = 0; 7338983cfbfSMike Smith } 7348983cfbfSMike Smith 735b2068c0cSWarner Losh #ifdef PRE7_COMPAT 736904c3909SGleb Smirnoff if ( 737904c3909SGleb Smirnoff #ifdef COMPAT_FREEBSD32 738904c3909SGleb Smirnoff (cmd == PCIOCGETCONF_OLD32 && 739b01bf72bSMaxim Sobolev (pattern_buf_old32 == NULL || 740b01bf72bSMaxim Sobolev pci_conf_match_old32(pattern_buf_old32, 741b01bf72bSMaxim Sobolev num_patterns, &dinfo->conf) == 0)) || 742904c3909SGleb Smirnoff #endif 743b01bf72bSMaxim Sobolev (cmd == PCIOCGETCONF_OLD && 74433d3fffaSMarius Strobl (pattern_buf_old == NULL || 74533d3fffaSMarius Strobl pci_conf_match_old(pattern_buf_old, num_patterns, 74633d3fffaSMarius Strobl &dinfo->conf) == 0)) || 74733d3fffaSMarius Strobl (cmd == PCIOCGETCONF && 74833d3fffaSMarius Strobl (pattern_buf == NULL || 74933d3fffaSMarius Strobl pci_conf_match(pattern_buf, num_patterns, 75033d3fffaSMarius Strobl &dinfo->conf) == 0))) { 75133d3fffaSMarius Strobl #else 75233d3fffaSMarius Strobl if (pattern_buf == NULL || 75333d3fffaSMarius Strobl pci_conf_match(pattern_buf, num_patterns, 75433d3fffaSMarius Strobl &dinfo->conf) == 0) { 75533d3fffaSMarius Strobl #endif 7568983cfbfSMike Smith /* 7578983cfbfSMike Smith * If we've filled up the user's buffer, 7588983cfbfSMike Smith * break out at this point. Since we've 7598983cfbfSMike Smith * got a match here, we'll pick right back 7608983cfbfSMike Smith * up at the matching entry. We can also 7618983cfbfSMike Smith * tell the user that there are more matches 7628983cfbfSMike Smith * left. 7638983cfbfSMike Smith */ 7648983cfbfSMike Smith if (cio->num_matches >= ionum) 7658983cfbfSMike Smith break; 7668983cfbfSMike Smith 767b2068c0cSWarner Losh #ifdef PRE7_COMPAT 768904c3909SGleb Smirnoff #ifdef COMPAT_FREEBSD32 769b01bf72bSMaxim Sobolev if (cmd == PCIOCGETCONF_OLD32) { 770b01bf72bSMaxim Sobolev conf_old32.pc_sel.pc_bus = 771b01bf72bSMaxim Sobolev dinfo->conf.pc_sel.pc_bus; 772b01bf72bSMaxim Sobolev conf_old32.pc_sel.pc_dev = 773b01bf72bSMaxim Sobolev dinfo->conf.pc_sel.pc_dev; 774b01bf72bSMaxim Sobolev conf_old32.pc_sel.pc_func = 775b01bf72bSMaxim Sobolev dinfo->conf.pc_sel.pc_func; 776b01bf72bSMaxim Sobolev conf_old32.pc_hdr = dinfo->conf.pc_hdr; 777b01bf72bSMaxim Sobolev conf_old32.pc_subvendor = 778b01bf72bSMaxim Sobolev dinfo->conf.pc_subvendor; 779b01bf72bSMaxim Sobolev conf_old32.pc_subdevice = 780b01bf72bSMaxim Sobolev dinfo->conf.pc_subdevice; 781b01bf72bSMaxim Sobolev conf_old32.pc_vendor = 782b01bf72bSMaxim Sobolev dinfo->conf.pc_vendor; 783b01bf72bSMaxim Sobolev conf_old32.pc_device = 784b01bf72bSMaxim Sobolev dinfo->conf.pc_device; 785b01bf72bSMaxim Sobolev conf_old32.pc_class = 786b01bf72bSMaxim Sobolev dinfo->conf.pc_class; 787b01bf72bSMaxim Sobolev conf_old32.pc_subclass = 788b01bf72bSMaxim Sobolev dinfo->conf.pc_subclass; 789b01bf72bSMaxim Sobolev conf_old32.pc_progif = 790b01bf72bSMaxim Sobolev dinfo->conf.pc_progif; 791b01bf72bSMaxim Sobolev conf_old32.pc_revid = 792b01bf72bSMaxim Sobolev dinfo->conf.pc_revid; 793b01bf72bSMaxim Sobolev strncpy(conf_old32.pd_name, 794b01bf72bSMaxim Sobolev dinfo->conf.pd_name, 795b01bf72bSMaxim Sobolev sizeof(conf_old32.pd_name)); 796b01bf72bSMaxim Sobolev conf_old32.pd_name[PCI_MAXNAMELEN] = 0; 797b01bf72bSMaxim Sobolev conf_old32.pd_unit = 798e5280830SGleb Smirnoff (uint32_t)dinfo->conf.pd_unit; 799b01bf72bSMaxim Sobolev confdata = &conf_old32; 800b01bf72bSMaxim Sobolev } else 801904c3909SGleb Smirnoff #endif /* COMPAT_FREEBSD32 */ 80233d3fffaSMarius Strobl if (cmd == PCIOCGETCONF_OLD) { 80333d3fffaSMarius Strobl conf_old.pc_sel.pc_bus = 80433d3fffaSMarius Strobl dinfo->conf.pc_sel.pc_bus; 80533d3fffaSMarius Strobl conf_old.pc_sel.pc_dev = 80633d3fffaSMarius Strobl dinfo->conf.pc_sel.pc_dev; 80733d3fffaSMarius Strobl conf_old.pc_sel.pc_func = 80833d3fffaSMarius Strobl dinfo->conf.pc_sel.pc_func; 80933d3fffaSMarius Strobl conf_old.pc_hdr = dinfo->conf.pc_hdr; 81033d3fffaSMarius Strobl conf_old.pc_subvendor = 81133d3fffaSMarius Strobl dinfo->conf.pc_subvendor; 81233d3fffaSMarius Strobl conf_old.pc_subdevice = 81333d3fffaSMarius Strobl dinfo->conf.pc_subdevice; 81433d3fffaSMarius Strobl conf_old.pc_vendor = 81533d3fffaSMarius Strobl dinfo->conf.pc_vendor; 81633d3fffaSMarius Strobl conf_old.pc_device = 81733d3fffaSMarius Strobl dinfo->conf.pc_device; 81833d3fffaSMarius Strobl conf_old.pc_class = 81933d3fffaSMarius Strobl dinfo->conf.pc_class; 82033d3fffaSMarius Strobl conf_old.pc_subclass = 82133d3fffaSMarius Strobl dinfo->conf.pc_subclass; 82233d3fffaSMarius Strobl conf_old.pc_progif = 82333d3fffaSMarius Strobl dinfo->conf.pc_progif; 82433d3fffaSMarius Strobl conf_old.pc_revid = 82533d3fffaSMarius Strobl dinfo->conf.pc_revid; 826c5860546SMarius Strobl strncpy(conf_old.pd_name, 827c5860546SMarius Strobl dinfo->conf.pd_name, 82833d3fffaSMarius Strobl sizeof(conf_old.pd_name)); 829c5860546SMarius Strobl conf_old.pd_name[PCI_MAXNAMELEN] = 0; 83033d3fffaSMarius Strobl conf_old.pd_unit = 83133d3fffaSMarius Strobl dinfo->conf.pd_unit; 83233d3fffaSMarius Strobl confdata = &conf_old; 83333d3fffaSMarius Strobl } else 834904c3909SGleb Smirnoff #endif /* PRE7_COMPAT */ 83533d3fffaSMarius Strobl confdata = &dinfo->conf; 83633d3fffaSMarius Strobl /* Only if we can copy it out do we count it. */ 83733d3fffaSMarius Strobl if (!(error = copyout(confdata, 838c5860546SMarius Strobl (caddr_t)cio->matches + 839c5860546SMarius Strobl confsz * cio->num_matches, confsz))) 84033d3fffaSMarius Strobl cio->num_matches++; 8418983cfbfSMike Smith } 842d08239c1SJohn-Mark Gurney } 8438983cfbfSMike Smith 8448983cfbfSMike Smith /* 8458983cfbfSMike Smith * Set the pointer into the list, so if the user is getting 8468983cfbfSMike Smith * n records at a time, where n < pci_numdevs, 8478983cfbfSMike Smith */ 8488983cfbfSMike Smith cio->offset = i; 8498983cfbfSMike Smith 8508983cfbfSMike Smith /* 8518983cfbfSMike Smith * Set the generation, the user will need this if they make 8528983cfbfSMike Smith * another ioctl call with offset != 0. 8538983cfbfSMike Smith */ 8548983cfbfSMike Smith cio->generation = pci_generation; 8558983cfbfSMike Smith 8568983cfbfSMike Smith /* 8578983cfbfSMike Smith * If this is the last device, inform the user so he won't 8588983cfbfSMike Smith * bother asking for more devices. If dinfo isn't NULL, we 8598983cfbfSMike Smith * know that there are more matches in the list because of 8608983cfbfSMike Smith * the way the traversal is done. 8618983cfbfSMike Smith */ 8628983cfbfSMike Smith if (dinfo == NULL) 8638983cfbfSMike Smith cio->status = PCI_GETCONF_LAST_DEVICE; 8648983cfbfSMike Smith else 8658983cfbfSMike Smith cio->status = PCI_GETCONF_MORE_DEVS; 8668983cfbfSMike Smith 867d08239c1SJohn-Mark Gurney getconfexit: 868904c3909SGleb Smirnoff #ifdef PRE7_COMPAT 869b01bf72bSMaxim Sobolev #ifdef COMPAT_FREEBSD32 870b01bf72bSMaxim Sobolev if (cmd == PCIOCGETCONF_OLD32) { 871b01bf72bSMaxim Sobolev cio32->status = cio->status; 872b01bf72bSMaxim Sobolev cio32->generation = cio->generation; 873b01bf72bSMaxim Sobolev cio32->offset = cio->offset; 874b01bf72bSMaxim Sobolev cio32->num_matches = cio->num_matches; 875b01bf72bSMaxim Sobolev free(cio, M_TEMP); 876b01bf72bSMaxim Sobolev } 877b01bf72bSMaxim Sobolev if (pattern_buf_old32 != NULL) 878b01bf72bSMaxim Sobolev free(pattern_buf_old32, M_TEMP); 879904c3909SGleb Smirnoff #endif 88033d3fffaSMarius Strobl if (pattern_buf_old != NULL) 88133d3fffaSMarius Strobl free(pattern_buf_old, M_TEMP); 88233d3fffaSMarius Strobl #endif 883904c3909SGleb Smirnoff if (pattern_buf != NULL) 884904c3909SGleb Smirnoff free(pattern_buf, M_TEMP); 8858983cfbfSMike Smith 8868983cfbfSMike Smith break; 8878983cfbfSMike Smith 888b2068c0cSWarner Losh #ifdef PRE7_COMPAT 88933d3fffaSMarius Strobl case PCIOCREAD_OLD: 89033d3fffaSMarius Strobl case PCIOCWRITE_OLD: 89133d3fffaSMarius Strobl io_old = (struct pci_io_old *)data; 89233d3fffaSMarius Strobl iodata.pi_sel.pc_domain = 0; 89333d3fffaSMarius Strobl iodata.pi_sel.pc_bus = io_old->pi_sel.pc_bus; 89433d3fffaSMarius Strobl iodata.pi_sel.pc_dev = io_old->pi_sel.pc_dev; 89533d3fffaSMarius Strobl iodata.pi_sel.pc_func = io_old->pi_sel.pc_func; 89633d3fffaSMarius Strobl iodata.pi_reg = io_old->pi_reg; 89733d3fffaSMarius Strobl iodata.pi_width = io_old->pi_width; 89833d3fffaSMarius Strobl iodata.pi_data = io_old->pi_data; 89933d3fffaSMarius Strobl data = (caddr_t)&iodata; 90033d3fffaSMarius Strobl /* FALLTHROUGH */ 90133d3fffaSMarius Strobl #endif 90266f314b5SStefan Eßer case PCIOCREAD: 9038983cfbfSMike Smith case PCIOCWRITE: 9048983cfbfSMike Smith io = (struct pci_io *)data; 9058983cfbfSMike Smith switch(io->pi_width) { 9068983cfbfSMike Smith case 4: 9078983cfbfSMike Smith case 2: 9088983cfbfSMike Smith case 1: 909d16d35fdSAndriy Gapon /* Make sure register is not negative and aligned. */ 91033d3fffaSMarius Strobl if (io->pi_reg < 0 || 91133d3fffaSMarius Strobl io->pi_reg & (io->pi_width - 1)) { 91266f314b5SStefan Eßer error = EINVAL; 91340ed3f47SRuslan Ermilov break; 91440ed3f47SRuslan Ermilov } 9158983cfbfSMike Smith /* 9168983cfbfSMike Smith * Assume that the user-level bus number is 91737ce43b7SBruce M Simpson * in fact the physical PCI bus number. 91837ce43b7SBruce M Simpson * Look up the grandparent, i.e. the bridge device, 91937ce43b7SBruce M Simpson * so that we can issue configuration space cycles. 9208983cfbfSMike Smith */ 92155aaf894SMarius Strobl pcidev = pci_find_dbsf(io->pi_sel.pc_domain, 92255aaf894SMarius Strobl io->pi_sel.pc_bus, io->pi_sel.pc_dev, 92355aaf894SMarius Strobl io->pi_sel.pc_func); 92437ce43b7SBruce M Simpson if (pcidev) { 92533d3fffaSMarius Strobl brdev = device_get_parent( 92633d3fffaSMarius Strobl device_get_parent(pcidev)); 92737ce43b7SBruce M Simpson 928b2068c0cSWarner Losh #ifdef PRE7_COMPAT 92933d3fffaSMarius Strobl if (cmd == PCIOCWRITE || cmd == PCIOCWRITE_OLD) 93033d3fffaSMarius Strobl #else 93166f314b5SStefan Eßer if (cmd == PCIOCWRITE) 93233d3fffaSMarius Strobl #endif 93337ce43b7SBruce M Simpson PCIB_WRITE_CONFIG(brdev, 93437ce43b7SBruce M Simpson io->pi_sel.pc_bus, 9358983cfbfSMike Smith io->pi_sel.pc_dev, 9368983cfbfSMike Smith io->pi_sel.pc_func, 9378983cfbfSMike Smith io->pi_reg, 9388983cfbfSMike Smith io->pi_data, 9398983cfbfSMike Smith io->pi_width); 940b2068c0cSWarner Losh #ifdef PRE7_COMPAT 94133d3fffaSMarius Strobl else if (cmd == PCIOCREAD_OLD) 94233d3fffaSMarius Strobl io_old->pi_data = 94333d3fffaSMarius Strobl PCIB_READ_CONFIG(brdev, 94433d3fffaSMarius Strobl io->pi_sel.pc_bus, 94533d3fffaSMarius Strobl io->pi_sel.pc_dev, 94633d3fffaSMarius Strobl io->pi_sel.pc_func, 94733d3fffaSMarius Strobl io->pi_reg, 94833d3fffaSMarius Strobl io->pi_width); 94933d3fffaSMarius Strobl #endif 95066f314b5SStefan Eßer else 95166f314b5SStefan Eßer io->pi_data = 95237ce43b7SBruce M Simpson PCIB_READ_CONFIG(brdev, 95337ce43b7SBruce M Simpson io->pi_sel.pc_bus, 95466f314b5SStefan Eßer io->pi_sel.pc_dev, 95566f314b5SStefan Eßer io->pi_sel.pc_func, 95666f314b5SStefan Eßer io->pi_reg, 95766f314b5SStefan Eßer io->pi_width); 9588983cfbfSMike Smith error = 0; 9598983cfbfSMike Smith } else { 9608910aa92SPaul Saab #ifdef COMPAT_FREEBSD4 96133d3fffaSMarius Strobl if (cmd == PCIOCREAD_OLD) { 96233d3fffaSMarius Strobl io_old->pi_data = -1; 9638910aa92SPaul Saab error = 0; 9648910aa92SPaul Saab } else 9658910aa92SPaul Saab #endif 9668983cfbfSMike Smith error = ENODEV; 9678983cfbfSMike Smith } 9688983cfbfSMike Smith break; 9698983cfbfSMike Smith default: 970d08239c1SJohn-Mark Gurney error = EINVAL; 9718983cfbfSMike Smith break; 9728983cfbfSMike Smith } 9738983cfbfSMike Smith break; 9748983cfbfSMike Smith 975da1e0915SJohn Baldwin case PCIOCGETBAR: 976da1e0915SJohn Baldwin bio = (struct pci_bar_io *)data; 977da1e0915SJohn Baldwin 978da1e0915SJohn Baldwin /* 979da1e0915SJohn Baldwin * Assume that the user-level bus number is 980da1e0915SJohn Baldwin * in fact the physical PCI bus number. 981da1e0915SJohn Baldwin */ 982da1e0915SJohn Baldwin pcidev = pci_find_dbsf(bio->pbi_sel.pc_domain, 983da1e0915SJohn Baldwin bio->pbi_sel.pc_bus, bio->pbi_sel.pc_dev, 984da1e0915SJohn Baldwin bio->pbi_sel.pc_func); 985da1e0915SJohn Baldwin if (pcidev == NULL) { 986da1e0915SJohn Baldwin error = ENODEV; 987da1e0915SJohn Baldwin break; 988da1e0915SJohn Baldwin } 989a90dd577SJohn Baldwin pm = pci_find_bar(pcidev, bio->pbi_reg); 990a90dd577SJohn Baldwin if (pm == NULL) { 991da1e0915SJohn Baldwin error = EINVAL; 992da1e0915SJohn Baldwin break; 993da1e0915SJohn Baldwin } 994a90dd577SJohn Baldwin bio->pbi_base = pm->pm_value; 995a90dd577SJohn Baldwin bio->pbi_length = (pci_addr_t)1 << pm->pm_size; 996a90dd577SJohn Baldwin bio->pbi_enabled = pci_bar_enabled(pcidev, pm); 997da1e0915SJohn Baldwin error = 0; 998da1e0915SJohn Baldwin break; 999762aad81SNeel Natu case PCIOCATTACHED: 1000762aad81SNeel Natu error = 0; 1001762aad81SNeel Natu io = (struct pci_io *)data; 1002762aad81SNeel Natu pcidev = pci_find_dbsf(io->pi_sel.pc_domain, io->pi_sel.pc_bus, 1003762aad81SNeel Natu io->pi_sel.pc_dev, io->pi_sel.pc_func); 1004762aad81SNeel Natu if (pcidev != NULL) 1005762aad81SNeel Natu io->pi_data = device_is_attached(pcidev); 1006762aad81SNeel Natu else 1007762aad81SNeel Natu error = ENODEV; 1008762aad81SNeel Natu break; 1009*84b755dfSJohn Baldwin case PCIOCLISTVPD: 1010*84b755dfSJohn Baldwin lvio = (struct pci_list_vpd_io *)data; 1011*84b755dfSJohn Baldwin 1012*84b755dfSJohn Baldwin /* 1013*84b755dfSJohn Baldwin * Assume that the user-level bus number is 1014*84b755dfSJohn Baldwin * in fact the physical PCI bus number. 1015*84b755dfSJohn Baldwin */ 1016*84b755dfSJohn Baldwin pcidev = pci_find_dbsf(lvio->plvi_sel.pc_domain, 1017*84b755dfSJohn Baldwin lvio->plvi_sel.pc_bus, lvio->plvi_sel.pc_dev, 1018*84b755dfSJohn Baldwin lvio->plvi_sel.pc_func); 1019*84b755dfSJohn Baldwin if (pcidev == NULL) { 1020*84b755dfSJohn Baldwin error = ENODEV; 1021*84b755dfSJohn Baldwin break; 1022*84b755dfSJohn Baldwin } 1023*84b755dfSJohn Baldwin error = pci_list_vpd(pcidev, lvio); 1024*84b755dfSJohn Baldwin break; 10258983cfbfSMike Smith default: 10268983cfbfSMike Smith error = ENOTTY; 10278983cfbfSMike Smith break; 10288983cfbfSMike Smith } 10298983cfbfSMike Smith 10308983cfbfSMike Smith return (error); 10318983cfbfSMike Smith } 1032