pci_user.c revision 220195
1119418Sobrien/*- 269953Smsmith * Copyright (c) 1997, Stefan Esser <se@freebsd.org> 369953Smsmith * All rights reserved. 469953Smsmith * 569953Smsmith * Redistribution and use in source and binary forms, with or without 669953Smsmith * modification, are permitted provided that the following conditions 769953Smsmith * are met: 869953Smsmith * 1. Redistributions of source code must retain the above copyright 969953Smsmith * notice unmodified, this list of conditions, and the following 1069953Smsmith * disclaimer. 1169953Smsmith * 2. Redistributions in binary form must reproduce the above copyright 1269953Smsmith * notice, this list of conditions and the following disclaimer in the 1369953Smsmith * documentation and/or other materials provided with the distribution. 1469953Smsmith * 1569953Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1669953Smsmith * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1769953Smsmith * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1869953Smsmith * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1969953Smsmith * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2069953Smsmith * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2169953Smsmith * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2269953Smsmith * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2369953Smsmith * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2469953Smsmith * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2569953Smsmith */ 2669953Smsmith 27119418Sobrien#include <sys/cdefs.h> 28119418Sobrien__FBSDID("$FreeBSD: head/sys/dev/pci/pci_user.c 220195 2011-03-31 13:22:12Z jhb $"); 29119418Sobrien 3069953Smsmith#include "opt_bus.h" /* XXX trim includes */ 31149478Sps#include "opt_compat.h" 3269953Smsmith 3369953Smsmith#include <sys/param.h> 3469953Smsmith#include <sys/systm.h> 3569953Smsmith#include <sys/malloc.h> 3669953Smsmith#include <sys/module.h> 3769953Smsmith#include <sys/linker.h> 3869953Smsmith#include <sys/fcntl.h> 3969953Smsmith#include <sys/conf.h> 4069953Smsmith#include <sys/kernel.h> 4183975Srwatson#include <sys/proc.h> 4269953Smsmith#include <sys/queue.h> 4369953Smsmith#include <sys/types.h> 4469953Smsmith 4569953Smsmith#include <vm/vm.h> 4669953Smsmith#include <vm/pmap.h> 4769953Smsmith#include <vm/vm_extern.h> 4869953Smsmith 4969953Smsmith#include <sys/bus.h> 5069953Smsmith#include <machine/bus.h> 5169953Smsmith#include <sys/rman.h> 5269953Smsmith#include <machine/resource.h> 5369953Smsmith 5469953Smsmith#include <sys/pciio.h> 55119285Simp#include <dev/pci/pcireg.h> 56119285Simp#include <dev/pci/pcivar.h> 5769953Smsmith 5869953Smsmith#include "pcib_if.h" 5969953Smsmith#include "pci_if.h" 6069953Smsmith 6169953Smsmith/* 6269953Smsmith * This is the user interface to PCI configuration space. 6369953Smsmith */ 6469953Smsmith 6583366Sjulianstatic d_open_t pci_open; 6683366Sjulianstatic d_close_t pci_close; 6769953Smsmithstatic int pci_conf_match(struct pci_match_conf *matches, int num_matches, 6869953Smsmith struct pci_conf *match_buf); 6983366Sjulianstatic d_ioctl_t pci_ioctl; 7069953Smsmith 7169953Smsmithstruct cdevsw pcicdev = { 72126080Sphk .d_version = D_VERSION, 73126080Sphk .d_flags = D_NEEDGIANT, 74111815Sphk .d_open = pci_open, 75111815Sphk .d_close = pci_close, 76111815Sphk .d_ioctl = pci_ioctl, 77111815Sphk .d_name = "pci", 7869953Smsmith}; 7969953Smsmith 8069953Smsmithstatic int 81130585Sphkpci_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 8269953Smsmith{ 8383975Srwatson int error; 8483975Srwatson 8583975Srwatson if (oflags & FWRITE) { 8691406Sjhb error = securelevel_gt(td->td_ucred, 0); 8783975Srwatson if (error) 8883975Srwatson return (error); 8969953Smsmith } 9083975Srwatson 9183975Srwatson return (0); 9269953Smsmith} 9369953Smsmith 9469953Smsmithstatic int 95130585Sphkpci_close(struct cdev *dev, int flag, int devtype, struct thread *td) 9669953Smsmith{ 9769953Smsmith return 0; 9869953Smsmith} 9969953Smsmith 10069953Smsmith/* 10169953Smsmith * Match a single pci_conf structure against an array of pci_match_conf 10269953Smsmith * structures. The first argument, 'matches', is an array of num_matches 10369953Smsmith * pci_match_conf structures. match_buf is a pointer to the pci_conf 10469953Smsmith * structure that will be compared to every entry in the matches array. 10569953Smsmith * This function returns 1 on failure, 0 on success. 10669953Smsmith */ 10769953Smsmithstatic int 10869953Smsmithpci_conf_match(struct pci_match_conf *matches, int num_matches, 10969953Smsmith struct pci_conf *match_buf) 11069953Smsmith{ 11169953Smsmith int i; 11269953Smsmith 11369953Smsmith if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0)) 11469953Smsmith return(1); 11569953Smsmith 11669953Smsmith for (i = 0; i < num_matches; i++) { 11769953Smsmith /* 11869953Smsmith * I'm not sure why someone would do this...but... 11969953Smsmith */ 12069953Smsmith if (matches[i].flags == PCI_GETCONF_NO_MATCH) 12169953Smsmith continue; 12269953Smsmith 12369953Smsmith /* 12469953Smsmith * Look at each of the match flags. If it's set, do the 12569953Smsmith * comparison. If the comparison fails, we don't have a 12669953Smsmith * match, go on to the next item if there is one. 12769953Smsmith */ 128172394Smarius if (((matches[i].flags & PCI_GETCONF_MATCH_DOMAIN) != 0) 129172394Smarius && (match_buf->pc_sel.pc_domain != 130172394Smarius matches[i].pc_sel.pc_domain)) 131172394Smarius continue; 132172394Smarius 13369953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_BUS) != 0) 13469953Smsmith && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus)) 13569953Smsmith continue; 13669953Smsmith 13769953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_DEV) != 0) 13869953Smsmith && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev)) 13969953Smsmith continue; 14069953Smsmith 14169953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC) != 0) 14269953Smsmith && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func)) 14369953Smsmith continue; 14469953Smsmith 14569953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR) != 0) 14669953Smsmith && (match_buf->pc_vendor != matches[i].pc_vendor)) 14769953Smsmith continue; 14869953Smsmith 14969953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE) != 0) 15069953Smsmith && (match_buf->pc_device != matches[i].pc_device)) 15169953Smsmith continue; 15269953Smsmith 15369953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS) != 0) 15469953Smsmith && (match_buf->pc_class != matches[i].pc_class)) 15569953Smsmith continue; 15669953Smsmith 15769953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT) != 0) 15869953Smsmith && (match_buf->pd_unit != matches[i].pd_unit)) 15969953Smsmith continue; 16069953Smsmith 16169953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_NAME) != 0) 16269953Smsmith && (strncmp(matches[i].pd_name, match_buf->pd_name, 16369953Smsmith sizeof(match_buf->pd_name)) != 0)) 16469953Smsmith continue; 16569953Smsmith 16669953Smsmith return(0); 16769953Smsmith } 16869953Smsmith 16969953Smsmith return(1); 17069953Smsmith} 17169953Smsmith 172172932Smarius#if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \ 173172932Smarius defined(COMPAT_FREEBSD6) 174172999Simp#define PRE7_COMPAT 175172932Smarius 176172932Smariustypedef enum { 177172932Smarius PCI_GETCONF_NO_MATCH_OLD = 0x00, 178172932Smarius PCI_GETCONF_MATCH_BUS_OLD = 0x01, 179172932Smarius PCI_GETCONF_MATCH_DEV_OLD = 0x02, 180172932Smarius PCI_GETCONF_MATCH_FUNC_OLD = 0x04, 181172932Smarius PCI_GETCONF_MATCH_NAME_OLD = 0x08, 182172932Smarius PCI_GETCONF_MATCH_UNIT_OLD = 0x10, 183172932Smarius PCI_GETCONF_MATCH_VENDOR_OLD = 0x20, 184172932Smarius PCI_GETCONF_MATCH_DEVICE_OLD = 0x40, 185172932Smarius PCI_GETCONF_MATCH_CLASS_OLD = 0x80 186172932Smarius} pci_getconf_flags_old; 187172932Smarius 188172932Smariusstruct pcisel_old { 189172932Smarius u_int8_t pc_bus; /* bus number */ 190172932Smarius u_int8_t pc_dev; /* device on this bus */ 191172932Smarius u_int8_t pc_func; /* function on this device */ 192172932Smarius}; 193172932Smarius 194172932Smariusstruct pci_conf_old { 195172932Smarius struct pcisel_old pc_sel; /* bus+slot+function */ 196172932Smarius u_int8_t pc_hdr; /* PCI header type */ 197172932Smarius u_int16_t pc_subvendor; /* card vendor ID */ 198172932Smarius u_int16_t pc_subdevice; /* card device ID, assigned by 199172932Smarius card vendor */ 200172932Smarius u_int16_t pc_vendor; /* chip vendor ID */ 201172932Smarius u_int16_t pc_device; /* chip device ID, assigned by 202172932Smarius chip vendor */ 203172932Smarius u_int8_t pc_class; /* chip PCI class */ 204172932Smarius u_int8_t pc_subclass; /* chip PCI subclass */ 205172932Smarius u_int8_t pc_progif; /* chip PCI programming interface */ 206172932Smarius u_int8_t pc_revid; /* chip revision ID */ 207172932Smarius char pd_name[PCI_MAXNAMELEN + 1]; /* device name */ 208172932Smarius u_long pd_unit; /* device unit number */ 209172932Smarius}; 210172932Smarius 211172932Smariusstruct pci_match_conf_old { 212172932Smarius struct pcisel_old pc_sel; /* bus+slot+function */ 213172932Smarius char pd_name[PCI_MAXNAMELEN + 1]; /* device name */ 214172932Smarius u_long pd_unit; /* Unit number */ 215172932Smarius u_int16_t pc_vendor; /* PCI Vendor ID */ 216172932Smarius u_int16_t pc_device; /* PCI Device ID */ 217172932Smarius u_int8_t pc_class; /* PCI class */ 218174932Smarius pci_getconf_flags_old flags; /* Matching expression */ 219172932Smarius}; 220172932Smarius 221172932Smariusstruct pci_io_old { 222172932Smarius struct pcisel_old pi_sel; /* device to operate on */ 223172932Smarius int pi_reg; /* configuration register to examine */ 224172932Smarius int pi_width; /* width (in bytes) of read or write */ 225172932Smarius u_int32_t pi_data; /* data to write or result of read */ 226172932Smarius}; 227172932Smarius 228172932Smarius#define PCIOCGETCONF_OLD _IOWR('p', 1, struct pci_conf_io) 229172932Smarius#define PCIOCREAD_OLD _IOWR('p', 2, struct pci_io_old) 230172932Smarius#define PCIOCWRITE_OLD _IOWR('p', 3, struct pci_io_old) 231172932Smarius 232172932Smariusstatic int pci_conf_match_old(struct pci_match_conf_old *matches, 233172932Smarius int num_matches, struct pci_conf *match_buf); 234172932Smarius 23569953Smsmithstatic int 236172932Smariuspci_conf_match_old(struct pci_match_conf_old *matches, int num_matches, 237172932Smarius struct pci_conf *match_buf) 238172932Smarius{ 239172932Smarius int i; 240172932Smarius 241172932Smarius if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0)) 242172932Smarius return(1); 243172932Smarius 244172932Smarius for (i = 0; i < num_matches; i++) { 245172932Smarius if (match_buf->pc_sel.pc_domain != 0) 246172932Smarius continue; 247172932Smarius 248172932Smarius /* 249172932Smarius * I'm not sure why someone would do this...but... 250172932Smarius */ 251172932Smarius if (matches[i].flags == PCI_GETCONF_NO_MATCH_OLD) 252172932Smarius continue; 253172932Smarius 254172932Smarius /* 255172932Smarius * Look at each of the match flags. If it's set, do the 256172932Smarius * comparison. If the comparison fails, we don't have a 257172932Smarius * match, go on to the next item if there is one. 258172932Smarius */ 259172932Smarius if (((matches[i].flags & PCI_GETCONF_MATCH_BUS_OLD) != 0) 260172932Smarius && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus)) 261172932Smarius continue; 262172932Smarius 263172932Smarius if (((matches[i].flags & PCI_GETCONF_MATCH_DEV_OLD) != 0) 264172932Smarius && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev)) 265172932Smarius continue; 266172932Smarius 267172932Smarius if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC_OLD) != 0) 268172932Smarius && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func)) 269172932Smarius continue; 270172932Smarius 271172932Smarius if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR_OLD) != 0) 272172932Smarius && (match_buf->pc_vendor != matches[i].pc_vendor)) 273172932Smarius continue; 274172932Smarius 275172932Smarius if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE_OLD) != 0) 276172932Smarius && (match_buf->pc_device != matches[i].pc_device)) 277172932Smarius continue; 278172932Smarius 279172932Smarius if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS_OLD) != 0) 280172932Smarius && (match_buf->pc_class != matches[i].pc_class)) 281172932Smarius continue; 282172932Smarius 283172932Smarius if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT_OLD) != 0) 284172932Smarius && (match_buf->pd_unit != matches[i].pd_unit)) 285172932Smarius continue; 286172932Smarius 287172932Smarius if (((matches[i].flags & PCI_GETCONF_MATCH_NAME_OLD) != 0) 288172932Smarius && (strncmp(matches[i].pd_name, match_buf->pd_name, 289172932Smarius sizeof(match_buf->pd_name)) != 0)) 290172932Smarius continue; 291172932Smarius 292172932Smarius return(0); 293172932Smarius } 294172932Smarius 295172932Smarius return(1); 296172932Smarius} 297172932Smarius 298172932Smarius#endif 299172932Smarius 300172932Smariusstatic int 301130585Sphkpci_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 30269953Smsmith{ 303172932Smarius device_t pcidev, brdev; 304172932Smarius void *confdata; 305172932Smarius const char *name; 306172932Smarius struct devlist *devlist_head; 307172932Smarius struct pci_conf_io *cio; 308172932Smarius struct pci_devinfo *dinfo; 30969953Smsmith struct pci_io *io; 310188018Sjhb struct pci_bar_io *bio; 311172932Smarius struct pci_match_conf *pattern_buf; 312220195Sjhb struct pci_map *pm; 313172932Smarius size_t confsz, iolen, pbufsz; 314172932Smarius int error, ionum, i, num_patterns; 315172999Simp#ifdef PRE7_COMPAT 316172932Smarius struct pci_conf_old conf_old; 317172932Smarius struct pci_io iodata; 318172932Smarius struct pci_io_old *io_old; 319172932Smarius struct pci_match_conf_old *pattern_buf_old; 32069953Smsmith 321172932Smarius io_old = NULL; 322172932Smarius pattern_buf_old = NULL; 323172932Smarius 324188018Sjhb if (!(flag & FWRITE) && cmd != PCIOCGETBAR && 325188018Sjhb cmd != PCIOCGETCONF && cmd != PCIOCGETCONF_OLD) 326172932Smarius return EPERM; 327172932Smarius#else 328188018Sjhb if (!(flag & FWRITE) && cmd != PCIOCGETBAR && cmd != PCIOCGETCONF) 32969953Smsmith return EPERM; 330172932Smarius#endif 33169953Smsmith 33269953Smsmith switch(cmd) { 333172999Simp#ifdef PRE7_COMPAT 334172932Smarius case PCIOCGETCONF_OLD: 335172932Smarius /* FALLTHROUGH */ 336172932Smarius#endif 33769953Smsmith case PCIOCGETCONF: 33869953Smsmith cio = (struct pci_conf_io *)data; 33969953Smsmith 340172932Smarius pattern_buf = NULL; 34169953Smsmith num_patterns = 0; 34269953Smsmith dinfo = NULL; 34369953Smsmith 344172932Smarius cio->num_matches = 0; 345172932Smarius 34669953Smsmith /* 34769953Smsmith * If the user specified an offset into the device list, 34869953Smsmith * but the list has changed since they last called this 34969953Smsmith * ioctl, tell them that the list has changed. They will 35069953Smsmith * have to get the list from the beginning. 35169953Smsmith */ 35269953Smsmith if ((cio->offset != 0) 35369953Smsmith && (cio->generation != pci_generation)){ 35469953Smsmith cio->status = PCI_GETCONF_LIST_CHANGED; 35569953Smsmith error = 0; 35669953Smsmith break; 35769953Smsmith } 35869953Smsmith 35969953Smsmith /* 36069953Smsmith * Check to see whether the user has asked for an offset 36169953Smsmith * past the end of our list. 36269953Smsmith */ 36369953Smsmith if (cio->offset >= pci_numdevs) { 36469953Smsmith cio->status = PCI_GETCONF_LAST_DEVICE; 36569953Smsmith error = 0; 36669953Smsmith break; 36769953Smsmith } 36869953Smsmith 36969953Smsmith /* get the head of the device queue */ 37069953Smsmith devlist_head = &pci_devq; 37169953Smsmith 37269953Smsmith /* 37369953Smsmith * Determine how much room we have for pci_conf structures. 37469953Smsmith * Round the user's buffer size down to the nearest 37569953Smsmith * multiple of sizeof(struct pci_conf) in case the user 37669953Smsmith * didn't specify a multiple of that size. 37769953Smsmith */ 378172999Simp#ifdef PRE7_COMPAT 379172932Smarius if (cmd == PCIOCGETCONF_OLD) 380172932Smarius confsz = sizeof(struct pci_conf_old); 381172932Smarius else 382172932Smarius#endif 383172932Smarius confsz = sizeof(struct pci_conf); 384172932Smarius iolen = min(cio->match_buf_len - (cio->match_buf_len % confsz), 385172932Smarius pci_numdevs * confsz); 38669953Smsmith 38769953Smsmith /* 38869953Smsmith * Since we know that iolen is a multiple of the size of 38969953Smsmith * the pciconf union, it's okay to do this. 39069953Smsmith */ 391172932Smarius ionum = iolen / confsz; 39269953Smsmith 39369953Smsmith /* 39469953Smsmith * If this test is true, the user wants the pci_conf 39569953Smsmith * structures returned to match the supplied entries. 39669953Smsmith */ 397116704Sjmg if ((cio->num_patterns > 0) && (cio->num_patterns < pci_numdevs) 39869953Smsmith && (cio->pat_buf_len > 0)) { 39969953Smsmith /* 40069953Smsmith * pat_buf_len needs to be: 40169953Smsmith * num_patterns * sizeof(struct pci_match_conf) 40269953Smsmith * While it is certainly possible the user just 40369953Smsmith * allocated a large buffer, but set the number of 40469953Smsmith * matches correctly, it is far more likely that 40569953Smsmith * their kernel doesn't match the userland utility 40669953Smsmith * they're using. It's also possible that the user 40769953Smsmith * forgot to initialize some variables. Yes, this 40869953Smsmith * may be overly picky, but I hazard to guess that 40969953Smsmith * it's far more likely to just catch folks that 41069953Smsmith * updated their kernel but not their userland. 41169953Smsmith */ 412172999Simp#ifdef PRE7_COMPAT 413172932Smarius if (cmd == PCIOCGETCONF_OLD) 414172932Smarius pbufsz = sizeof(struct pci_match_conf_old); 415172932Smarius else 416172932Smarius#endif 417172932Smarius pbufsz = sizeof(struct pci_match_conf); 418172932Smarius if (cio->num_patterns * pbufsz != cio->pat_buf_len) { 419172932Smarius /* The user made a mistake, return an error. */ 42069953Smsmith cio->status = PCI_GETCONF_ERROR; 42169953Smsmith error = EINVAL; 42269953Smsmith break; 42369953Smsmith } 42469953Smsmith 42569953Smsmith /* 42669953Smsmith * Allocate a buffer to hold the patterns. 42769953Smsmith */ 428172999Simp#ifdef PRE7_COMPAT 429172932Smarius if (cmd == PCIOCGETCONF_OLD) { 430172932Smarius pattern_buf_old = malloc(cio->pat_buf_len, 431172932Smarius M_TEMP, M_WAITOK); 432172932Smarius error = copyin(cio->patterns, 433172932Smarius pattern_buf_old, cio->pat_buf_len); 434172932Smarius } else 435172932Smarius#endif 436172932Smarius { 437172932Smarius pattern_buf = malloc(cio->pat_buf_len, M_TEMP, 438172932Smarius M_WAITOK); 439172932Smarius error = copyin(cio->patterns, pattern_buf, 440172932Smarius cio->pat_buf_len); 441172932Smarius } 442116702Sjmg if (error != 0) { 443116702Sjmg error = EINVAL; 444116702Sjmg goto getconfexit; 445116702Sjmg } 44669953Smsmith num_patterns = cio->num_patterns; 44769953Smsmith } else if ((cio->num_patterns > 0) 44869953Smsmith || (cio->pat_buf_len > 0)) { 44969953Smsmith /* 45069953Smsmith * The user made a mistake, spit out an error. 45169953Smsmith */ 45269953Smsmith cio->status = PCI_GETCONF_ERROR; 45369953Smsmith error = EINVAL; 45469953Smsmith break; 455172932Smarius } 45669953Smsmith 45769953Smsmith /* 45869953Smsmith * Go through the list of devices and copy out the devices 45969953Smsmith * that match the user's criteria. 46069953Smsmith */ 46169953Smsmith for (cio->num_matches = 0, error = 0, i = 0, 46269953Smsmith dinfo = STAILQ_FIRST(devlist_head); 46369953Smsmith (dinfo != NULL) && (cio->num_matches < ionum) 464116702Sjmg && (error == 0) && (i < pci_numdevs) && (dinfo != NULL); 46569953Smsmith dinfo = STAILQ_NEXT(dinfo, pci_links), i++) { 46669953Smsmith 46769953Smsmith if (i < cio->offset) 46869953Smsmith continue; 46969953Smsmith 47069953Smsmith /* Populate pd_name and pd_unit */ 47169953Smsmith name = NULL; 472175368Sjhb if (dinfo->cfg.dev) 47369953Smsmith name = device_get_name(dinfo->cfg.dev); 47469953Smsmith if (name) { 47569953Smsmith strncpy(dinfo->conf.pd_name, name, 47669953Smsmith sizeof(dinfo->conf.pd_name)); 47769953Smsmith dinfo->conf.pd_name[PCI_MAXNAMELEN] = 0; 47869953Smsmith dinfo->conf.pd_unit = 47969953Smsmith device_get_unit(dinfo->cfg.dev); 480175368Sjhb } else { 481175368Sjhb dinfo->conf.pd_name[0] = '\0'; 482175368Sjhb dinfo->conf.pd_unit = 0; 48369953Smsmith } 48469953Smsmith 485172999Simp#ifdef PRE7_COMPAT 486172932Smarius if ((cmd == PCIOCGETCONF_OLD && 487172932Smarius (pattern_buf_old == NULL || 488172932Smarius pci_conf_match_old(pattern_buf_old, num_patterns, 489172932Smarius &dinfo->conf) == 0)) || 490172932Smarius (cmd == PCIOCGETCONF && 491172932Smarius (pattern_buf == NULL || 492172932Smarius pci_conf_match(pattern_buf, num_patterns, 493172932Smarius &dinfo->conf) == 0))) { 494172932Smarius#else 495172932Smarius if (pattern_buf == NULL || 496172932Smarius pci_conf_match(pattern_buf, num_patterns, 497172932Smarius &dinfo->conf) == 0) { 498172932Smarius#endif 49969953Smsmith /* 50069953Smsmith * If we've filled up the user's buffer, 50169953Smsmith * break out at this point. Since we've 50269953Smsmith * got a match here, we'll pick right back 50369953Smsmith * up at the matching entry. We can also 50469953Smsmith * tell the user that there are more matches 50569953Smsmith * left. 50669953Smsmith */ 50769953Smsmith if (cio->num_matches >= ionum) 50869953Smsmith break; 50969953Smsmith 510172999Simp#ifdef PRE7_COMPAT 511172932Smarius if (cmd == PCIOCGETCONF_OLD) { 512172932Smarius conf_old.pc_sel.pc_bus = 513172932Smarius dinfo->conf.pc_sel.pc_bus; 514172932Smarius conf_old.pc_sel.pc_dev = 515172932Smarius dinfo->conf.pc_sel.pc_dev; 516172932Smarius conf_old.pc_sel.pc_func = 517172932Smarius dinfo->conf.pc_sel.pc_func; 518172932Smarius conf_old.pc_hdr = dinfo->conf.pc_hdr; 519172932Smarius conf_old.pc_subvendor = 520172932Smarius dinfo->conf.pc_subvendor; 521172932Smarius conf_old.pc_subdevice = 522172932Smarius dinfo->conf.pc_subdevice; 523172932Smarius conf_old.pc_vendor = 524172932Smarius dinfo->conf.pc_vendor; 525172932Smarius conf_old.pc_device = 526172932Smarius dinfo->conf.pc_device; 527172932Smarius conf_old.pc_class = 528172932Smarius dinfo->conf.pc_class; 529172932Smarius conf_old.pc_subclass = 530172932Smarius dinfo->conf.pc_subclass; 531172932Smarius conf_old.pc_progif = 532172932Smarius dinfo->conf.pc_progif; 533172932Smarius conf_old.pc_revid = 534172932Smarius dinfo->conf.pc_revid; 535174932Smarius strncpy(conf_old.pd_name, 536174932Smarius dinfo->conf.pd_name, 537174932Smarius sizeof(conf_old.pd_name)); 538174932Smarius conf_old.pd_name[PCI_MAXNAMELEN] = 0; 539174932Smarius conf_old.pd_unit = 540174932Smarius dinfo->conf.pd_unit; 541172932Smarius confdata = &conf_old; 542172932Smarius } else 543172932Smarius#endif 544172932Smarius confdata = &dinfo->conf; 545172932Smarius /* Only if we can copy it out do we count it. */ 546172932Smarius if (!(error = copyout(confdata, 547174932Smarius (caddr_t)cio->matches + 548174932Smarius confsz * cio->num_matches, confsz))) 549116702Sjmg cio->num_matches++; 55069953Smsmith } 55169953Smsmith } 55269953Smsmith 55369953Smsmith /* 55469953Smsmith * Set the pointer into the list, so if the user is getting 55569953Smsmith * n records at a time, where n < pci_numdevs, 55669953Smsmith */ 55769953Smsmith cio->offset = i; 55869953Smsmith 55969953Smsmith /* 56069953Smsmith * Set the generation, the user will need this if they make 56169953Smsmith * another ioctl call with offset != 0. 56269953Smsmith */ 56369953Smsmith cio->generation = pci_generation; 564172932Smarius 56569953Smsmith /* 56669953Smsmith * If this is the last device, inform the user so he won't 56769953Smsmith * bother asking for more devices. If dinfo isn't NULL, we 56869953Smsmith * know that there are more matches in the list because of 56969953Smsmith * the way the traversal is done. 57069953Smsmith */ 57169953Smsmith if (dinfo == NULL) 57269953Smsmith cio->status = PCI_GETCONF_LAST_DEVICE; 57369953Smsmith else 57469953Smsmith cio->status = PCI_GETCONF_MORE_DEVS; 57569953Smsmith 576116702Sjmggetconfexit: 57769953Smsmith if (pattern_buf != NULL) 57869953Smsmith free(pattern_buf, M_TEMP); 579172999Simp#ifdef PRE7_COMPAT 580172932Smarius if (pattern_buf_old != NULL) 581172932Smarius free(pattern_buf_old, M_TEMP); 582172932Smarius#endif 58369953Smsmith 58469953Smsmith break; 585121013Sse 586172999Simp#ifdef PRE7_COMPAT 587172932Smarius case PCIOCREAD_OLD: 588172932Smarius case PCIOCWRITE_OLD: 589172932Smarius io_old = (struct pci_io_old *)data; 590172932Smarius iodata.pi_sel.pc_domain = 0; 591172932Smarius iodata.pi_sel.pc_bus = io_old->pi_sel.pc_bus; 592172932Smarius iodata.pi_sel.pc_dev = io_old->pi_sel.pc_dev; 593172932Smarius iodata.pi_sel.pc_func = io_old->pi_sel.pc_func; 594172932Smarius iodata.pi_reg = io_old->pi_reg; 595172932Smarius iodata.pi_width = io_old->pi_width; 596172932Smarius iodata.pi_data = io_old->pi_data; 597172932Smarius data = (caddr_t)&iodata; 598172932Smarius /* FALLTHROUGH */ 599172932Smarius#endif 60069953Smsmith case PCIOCREAD: 601121013Sse case PCIOCWRITE: 60269953Smsmith io = (struct pci_io *)data; 60369953Smsmith switch(io->pi_width) { 60469953Smsmith case 4: 60569953Smsmith case 2: 60669953Smsmith case 1: 607197099Savg /* Make sure register is not negative and aligned. */ 608172932Smarius if (io->pi_reg < 0 || 609172932Smarius io->pi_reg & (io->pi_width - 1)) { 610163055Sru error = EINVAL; 611163055Sru break; 612163055Sru } 61369953Smsmith /* 61469953Smsmith * Assume that the user-level bus number is 615145022Sbms * in fact the physical PCI bus number. 616145022Sbms * Look up the grandparent, i.e. the bridge device, 617145022Sbms * so that we can issue configuration space cycles. 61869953Smsmith */ 619172394Smarius pcidev = pci_find_dbsf(io->pi_sel.pc_domain, 620172394Smarius io->pi_sel.pc_bus, io->pi_sel.pc_dev, 621172394Smarius io->pi_sel.pc_func); 622145022Sbms if (pcidev) { 623172932Smarius brdev = device_get_parent( 624172932Smarius device_get_parent(pcidev)); 625145022Sbms 626172999Simp#ifdef PRE7_COMPAT 627172932Smarius if (cmd == PCIOCWRITE || cmd == PCIOCWRITE_OLD) 628172932Smarius#else 629121013Sse if (cmd == PCIOCWRITE) 630172932Smarius#endif 631145022Sbms PCIB_WRITE_CONFIG(brdev, 632145022Sbms io->pi_sel.pc_bus, 633121013Sse io->pi_sel.pc_dev, 634121013Sse io->pi_sel.pc_func, 635121013Sse io->pi_reg, 636121013Sse io->pi_data, 637121013Sse io->pi_width); 638172999Simp#ifdef PRE7_COMPAT 639172932Smarius else if (cmd == PCIOCREAD_OLD) 640172932Smarius io_old->pi_data = 641172932Smarius PCIB_READ_CONFIG(brdev, 642172932Smarius io->pi_sel.pc_bus, 643172932Smarius io->pi_sel.pc_dev, 644172932Smarius io->pi_sel.pc_func, 645172932Smarius io->pi_reg, 646172932Smarius io->pi_width); 647172932Smarius#endif 648121013Sse else 649121013Sse io->pi_data = 650145022Sbms PCIB_READ_CONFIG(brdev, 651145022Sbms io->pi_sel.pc_bus, 652121013Sse io->pi_sel.pc_dev, 653121013Sse io->pi_sel.pc_func, 654121013Sse io->pi_reg, 655121013Sse io->pi_width); 65669953Smsmith error = 0; 65769953Smsmith } else { 658149478Sps#ifdef COMPAT_FREEBSD4 659172932Smarius if (cmd == PCIOCREAD_OLD) { 660172932Smarius io_old->pi_data = -1; 661149478Sps error = 0; 662149478Sps } else 663149478Sps#endif 664149478Sps error = ENODEV; 66569953Smsmith } 66669953Smsmith break; 66769953Smsmith default: 668116702Sjmg error = EINVAL; 66969953Smsmith break; 67069953Smsmith } 67169953Smsmith break; 67269953Smsmith 673188018Sjhb case PCIOCGETBAR: 674188018Sjhb bio = (struct pci_bar_io *)data; 675188018Sjhb 676188018Sjhb /* 677188018Sjhb * Assume that the user-level bus number is 678188018Sjhb * in fact the physical PCI bus number. 679188018Sjhb */ 680188018Sjhb pcidev = pci_find_dbsf(bio->pbi_sel.pc_domain, 681188018Sjhb bio->pbi_sel.pc_bus, bio->pbi_sel.pc_dev, 682188018Sjhb bio->pbi_sel.pc_func); 683188018Sjhb if (pcidev == NULL) { 684188018Sjhb error = ENODEV; 685188018Sjhb break; 686188018Sjhb } 687220195Sjhb pm = pci_find_bar(pcidev, bio->pbi_reg); 688220195Sjhb if (pm == NULL) { 689188018Sjhb error = EINVAL; 690188018Sjhb break; 691188018Sjhb } 692220195Sjhb bio->pbi_base = pm->pm_value; 693220195Sjhb bio->pbi_length = (pci_addr_t)1 << pm->pm_size; 694220195Sjhb bio->pbi_enabled = pci_bar_enabled(pcidev, pm); 695188018Sjhb error = 0; 696188018Sjhb break; 697210597Sneel case PCIOCATTACHED: 698210597Sneel error = 0; 699210597Sneel io = (struct pci_io *)data; 700210597Sneel pcidev = pci_find_dbsf(io->pi_sel.pc_domain, io->pi_sel.pc_bus, 701210597Sneel io->pi_sel.pc_dev, io->pi_sel.pc_func); 702210597Sneel if (pcidev != NULL) 703210597Sneel io->pi_data = device_is_attached(pcidev); 704210597Sneel else 705210597Sneel error = ENODEV; 706210597Sneel break; 70769953Smsmith default: 70869953Smsmith error = ENOTTY; 70969953Smsmith break; 71069953Smsmith } 71169953Smsmith 71269953Smsmith return (error); 71369953Smsmith} 714