pci_user.c revision 130585
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 130585 2004-06-16 09:47:26Z phk $"); 29119418Sobrien 3069953Smsmith#include "opt_bus.h" /* XXX trim includes */ 3169953Smsmith 3269953Smsmith#include <sys/param.h> 3369953Smsmith#include <sys/systm.h> 3469953Smsmith#include <sys/malloc.h> 3569953Smsmith#include <sys/module.h> 3669953Smsmith#include <sys/linker.h> 3769953Smsmith#include <sys/fcntl.h> 3869953Smsmith#include <sys/conf.h> 3969953Smsmith#include <sys/kernel.h> 4083975Srwatson#include <sys/proc.h> 4169953Smsmith#include <sys/queue.h> 4269953Smsmith#include <sys/types.h> 4369953Smsmith 4469953Smsmith#include <vm/vm.h> 4569953Smsmith#include <vm/pmap.h> 4669953Smsmith#include <vm/vm_extern.h> 4769953Smsmith 4869953Smsmith#include <sys/bus.h> 4969953Smsmith#include <machine/bus.h> 5069953Smsmith#include <sys/rman.h> 5169953Smsmith#include <machine/resource.h> 5269953Smsmith 5369953Smsmith#include <sys/pciio.h> 54119285Simp#include <dev/pci/pcireg.h> 55119285Simp#include <dev/pci/pcivar.h> 5669953Smsmith 5769953Smsmith#include "pcib_if.h" 5869953Smsmith#include "pci_if.h" 5969953Smsmith 6069953Smsmith/* 6169953Smsmith * This is the user interface to PCI configuration space. 6269953Smsmith */ 6369953Smsmith 6483366Sjulianstatic d_open_t pci_open; 6583366Sjulianstatic d_close_t pci_close; 6669953Smsmithstatic int pci_conf_match(struct pci_match_conf *matches, int num_matches, 6769953Smsmith struct pci_conf *match_buf); 6883366Sjulianstatic d_ioctl_t pci_ioctl; 6969953Smsmith 70111695Sse#if __FreeBSD_version < 500000 7169953Smsmith#define PCI_CDEV 78 72111695Sse#else 73111695Sse#define PCI_CDEV MAJOR_AUTO 74111695Sse#endif 7569953Smsmith 7669953Smsmithstruct cdevsw pcicdev = { 77126080Sphk .d_version = D_VERSION, 78126080Sphk .d_flags = D_NEEDGIANT, 79111815Sphk .d_open = pci_open, 80111815Sphk .d_close = pci_close, 81111815Sphk .d_ioctl = pci_ioctl, 82111815Sphk .d_name = "pci", 83111815Sphk .d_maj = PCI_CDEV, 8469953Smsmith}; 8569953Smsmith 8669953Smsmithstatic int 87130585Sphkpci_open(struct cdev *dev, int oflags, int devtype, struct thread *td) 8869953Smsmith{ 8983975Srwatson int error; 9083975Srwatson 9183975Srwatson if (oflags & FWRITE) { 9291406Sjhb error = securelevel_gt(td->td_ucred, 0); 9383975Srwatson if (error) 9483975Srwatson return (error); 9569953Smsmith } 9683975Srwatson 9783975Srwatson return (0); 9869953Smsmith} 9969953Smsmith 10069953Smsmithstatic int 101130585Sphkpci_close(struct cdev *dev, int flag, int devtype, struct thread *td) 10269953Smsmith{ 10369953Smsmith return 0; 10469953Smsmith} 10569953Smsmith 10669953Smsmith/* 10769953Smsmith * Match a single pci_conf structure against an array of pci_match_conf 10869953Smsmith * structures. The first argument, 'matches', is an array of num_matches 10969953Smsmith * pci_match_conf structures. match_buf is a pointer to the pci_conf 11069953Smsmith * structure that will be compared to every entry in the matches array. 11169953Smsmith * This function returns 1 on failure, 0 on success. 11269953Smsmith */ 11369953Smsmithstatic int 11469953Smsmithpci_conf_match(struct pci_match_conf *matches, int num_matches, 11569953Smsmith struct pci_conf *match_buf) 11669953Smsmith{ 11769953Smsmith int i; 11869953Smsmith 11969953Smsmith if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0)) 12069953Smsmith return(1); 12169953Smsmith 12269953Smsmith for (i = 0; i < num_matches; i++) { 12369953Smsmith /* 12469953Smsmith * I'm not sure why someone would do this...but... 12569953Smsmith */ 12669953Smsmith if (matches[i].flags == PCI_GETCONF_NO_MATCH) 12769953Smsmith continue; 12869953Smsmith 12969953Smsmith /* 13069953Smsmith * Look at each of the match flags. If it's set, do the 13169953Smsmith * comparison. If the comparison fails, we don't have a 13269953Smsmith * match, go on to the next item if there is one. 13369953Smsmith */ 13469953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_BUS) != 0) 13569953Smsmith && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus)) 13669953Smsmith continue; 13769953Smsmith 13869953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_DEV) != 0) 13969953Smsmith && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev)) 14069953Smsmith continue; 14169953Smsmith 14269953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC) != 0) 14369953Smsmith && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func)) 14469953Smsmith continue; 14569953Smsmith 14669953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR) != 0) 14769953Smsmith && (match_buf->pc_vendor != matches[i].pc_vendor)) 14869953Smsmith continue; 14969953Smsmith 15069953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE) != 0) 15169953Smsmith && (match_buf->pc_device != matches[i].pc_device)) 15269953Smsmith continue; 15369953Smsmith 15469953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS) != 0) 15569953Smsmith && (match_buf->pc_class != matches[i].pc_class)) 15669953Smsmith continue; 15769953Smsmith 15869953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT) != 0) 15969953Smsmith && (match_buf->pd_unit != matches[i].pd_unit)) 16069953Smsmith continue; 16169953Smsmith 16269953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_NAME) != 0) 16369953Smsmith && (strncmp(matches[i].pd_name, match_buf->pd_name, 16469953Smsmith sizeof(match_buf->pd_name)) != 0)) 16569953Smsmith continue; 16669953Smsmith 16769953Smsmith return(0); 16869953Smsmith } 16969953Smsmith 17069953Smsmith return(1); 17169953Smsmith} 17269953Smsmith 17369953Smsmithstatic int 174130585Sphkpci_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td) 17569953Smsmith{ 17669953Smsmith device_t pci, pcib; 17769953Smsmith struct pci_io *io; 17869953Smsmith const char *name; 17969953Smsmith int error; 18069953Smsmith 181116702Sjmg if (!(flag & FWRITE) && cmd != PCIOCGETCONF) 18269953Smsmith return EPERM; 18369953Smsmith 18469953Smsmith switch(cmd) { 18569953Smsmith case PCIOCGETCONF: 18669953Smsmith { 18769953Smsmith struct pci_devinfo *dinfo; 18869953Smsmith struct pci_conf_io *cio; 18969953Smsmith struct devlist *devlist_head; 19069953Smsmith struct pci_match_conf *pattern_buf; 19169953Smsmith int num_patterns; 19269953Smsmith size_t iolen; 19369953Smsmith int ionum, i; 19469953Smsmith 19569953Smsmith cio = (struct pci_conf_io *)data; 19669953Smsmith 19769953Smsmith num_patterns = 0; 19869953Smsmith dinfo = NULL; 19969953Smsmith 20069953Smsmith /* 20169953Smsmith * If the user specified an offset into the device list, 20269953Smsmith * but the list has changed since they last called this 20369953Smsmith * ioctl, tell them that the list has changed. They will 20469953Smsmith * have to get the list from the beginning. 20569953Smsmith */ 20669953Smsmith if ((cio->offset != 0) 20769953Smsmith && (cio->generation != pci_generation)){ 20869953Smsmith cio->num_matches = 0; 20969953Smsmith cio->status = PCI_GETCONF_LIST_CHANGED; 21069953Smsmith error = 0; 21169953Smsmith break; 21269953Smsmith } 21369953Smsmith 21469953Smsmith /* 21569953Smsmith * Check to see whether the user has asked for an offset 21669953Smsmith * past the end of our list. 21769953Smsmith */ 21869953Smsmith if (cio->offset >= pci_numdevs) { 21969953Smsmith cio->num_matches = 0; 22069953Smsmith cio->status = PCI_GETCONF_LAST_DEVICE; 22169953Smsmith error = 0; 22269953Smsmith break; 22369953Smsmith } 22469953Smsmith 22569953Smsmith /* get the head of the device queue */ 22669953Smsmith devlist_head = &pci_devq; 22769953Smsmith 22869953Smsmith /* 22969953Smsmith * Determine how much room we have for pci_conf structures. 23069953Smsmith * Round the user's buffer size down to the nearest 23169953Smsmith * multiple of sizeof(struct pci_conf) in case the user 23269953Smsmith * didn't specify a multiple of that size. 23369953Smsmith */ 23469953Smsmith iolen = min(cio->match_buf_len - 23569953Smsmith (cio->match_buf_len % sizeof(struct pci_conf)), 23669953Smsmith pci_numdevs * sizeof(struct pci_conf)); 23769953Smsmith 23869953Smsmith /* 23969953Smsmith * Since we know that iolen is a multiple of the size of 24069953Smsmith * the pciconf union, it's okay to do this. 24169953Smsmith */ 24269953Smsmith ionum = iolen / sizeof(struct pci_conf); 24369953Smsmith 24469953Smsmith /* 24569953Smsmith * If this test is true, the user wants the pci_conf 24669953Smsmith * structures returned to match the supplied entries. 24769953Smsmith */ 248116704Sjmg if ((cio->num_patterns > 0) && (cio->num_patterns < pci_numdevs) 24969953Smsmith && (cio->pat_buf_len > 0)) { 25069953Smsmith /* 25169953Smsmith * pat_buf_len needs to be: 25269953Smsmith * num_patterns * sizeof(struct pci_match_conf) 25369953Smsmith * While it is certainly possible the user just 25469953Smsmith * allocated a large buffer, but set the number of 25569953Smsmith * matches correctly, it is far more likely that 25669953Smsmith * their kernel doesn't match the userland utility 25769953Smsmith * they're using. It's also possible that the user 25869953Smsmith * forgot to initialize some variables. Yes, this 25969953Smsmith * may be overly picky, but I hazard to guess that 26069953Smsmith * it's far more likely to just catch folks that 26169953Smsmith * updated their kernel but not their userland. 26269953Smsmith */ 26369953Smsmith if ((cio->num_patterns * 26469953Smsmith sizeof(struct pci_match_conf)) != cio->pat_buf_len){ 26569953Smsmith /* The user made a mistake, return an error*/ 26669953Smsmith cio->status = PCI_GETCONF_ERROR; 26769953Smsmith cio->num_matches = 0; 26869953Smsmith error = EINVAL; 26969953Smsmith break; 27069953Smsmith } 27169953Smsmith 27269953Smsmith /* 27369953Smsmith * Allocate a buffer to hold the patterns. 27469953Smsmith */ 27569953Smsmith pattern_buf = malloc(cio->pat_buf_len, M_TEMP, 276111119Simp M_WAITOK); 27769953Smsmith error = copyin(cio->patterns, pattern_buf, 27869953Smsmith cio->pat_buf_len); 279116702Sjmg if (error != 0) { 280116702Sjmg error = EINVAL; 281116702Sjmg goto getconfexit; 282116702Sjmg } 28369953Smsmith num_patterns = cio->num_patterns; 28469953Smsmith 28569953Smsmith } else if ((cio->num_patterns > 0) 28669953Smsmith || (cio->pat_buf_len > 0)) { 28769953Smsmith /* 28869953Smsmith * The user made a mistake, spit out an error. 28969953Smsmith */ 29069953Smsmith cio->status = PCI_GETCONF_ERROR; 29169953Smsmith cio->num_matches = 0; 29269953Smsmith error = EINVAL; 29369953Smsmith break; 29469953Smsmith } else 29569953Smsmith pattern_buf = NULL; 29669953Smsmith 29769953Smsmith /* 29869953Smsmith * Go through the list of devices and copy out the devices 29969953Smsmith * that match the user's criteria. 30069953Smsmith */ 30169953Smsmith for (cio->num_matches = 0, error = 0, i = 0, 30269953Smsmith dinfo = STAILQ_FIRST(devlist_head); 30369953Smsmith (dinfo != NULL) && (cio->num_matches < ionum) 304116702Sjmg && (error == 0) && (i < pci_numdevs) && (dinfo != NULL); 30569953Smsmith dinfo = STAILQ_NEXT(dinfo, pci_links), i++) { 30669953Smsmith 30769953Smsmith if (i < cio->offset) 30869953Smsmith continue; 30969953Smsmith 31069953Smsmith /* Populate pd_name and pd_unit */ 31169953Smsmith name = NULL; 31269953Smsmith if (dinfo->cfg.dev && dinfo->conf.pd_name[0] == '\0') 31369953Smsmith name = device_get_name(dinfo->cfg.dev); 31469953Smsmith if (name) { 31569953Smsmith strncpy(dinfo->conf.pd_name, name, 31669953Smsmith sizeof(dinfo->conf.pd_name)); 31769953Smsmith dinfo->conf.pd_name[PCI_MAXNAMELEN] = 0; 31869953Smsmith dinfo->conf.pd_unit = 31969953Smsmith device_get_unit(dinfo->cfg.dev); 32069953Smsmith } 32169953Smsmith 32269953Smsmith if ((pattern_buf == NULL) || 32369953Smsmith (pci_conf_match(pattern_buf, num_patterns, 32469953Smsmith &dinfo->conf) == 0)) { 32569953Smsmith 32669953Smsmith /* 32769953Smsmith * If we've filled up the user's buffer, 32869953Smsmith * break out at this point. Since we've 32969953Smsmith * got a match here, we'll pick right back 33069953Smsmith * up at the matching entry. We can also 33169953Smsmith * tell the user that there are more matches 33269953Smsmith * left. 33369953Smsmith */ 33469953Smsmith if (cio->num_matches >= ionum) 33569953Smsmith break; 33669953Smsmith 337116702Sjmg /* only if can copy it out do we count it */ 338116702Sjmg if (!(error = copyout(&dinfo->conf, 339116702Sjmg &cio->matches[cio->num_matches], 340116702Sjmg sizeof(struct pci_conf)))) { 341116702Sjmg cio->num_matches++; 342116702Sjmg } 34369953Smsmith } 34469953Smsmith } 34569953Smsmith 34669953Smsmith /* 34769953Smsmith * Set the pointer into the list, so if the user is getting 34869953Smsmith * n records at a time, where n < pci_numdevs, 34969953Smsmith */ 35069953Smsmith cio->offset = i; 35169953Smsmith 35269953Smsmith /* 35369953Smsmith * Set the generation, the user will need this if they make 35469953Smsmith * another ioctl call with offset != 0. 35569953Smsmith */ 35669953Smsmith cio->generation = pci_generation; 35769953Smsmith 35869953Smsmith /* 35969953Smsmith * If this is the last device, inform the user so he won't 36069953Smsmith * bother asking for more devices. If dinfo isn't NULL, we 36169953Smsmith * know that there are more matches in the list because of 36269953Smsmith * the way the traversal is done. 36369953Smsmith */ 36469953Smsmith if (dinfo == NULL) 36569953Smsmith cio->status = PCI_GETCONF_LAST_DEVICE; 36669953Smsmith else 36769953Smsmith cio->status = PCI_GETCONF_MORE_DEVS; 36869953Smsmith 369116702Sjmggetconfexit: 37069953Smsmith if (pattern_buf != NULL) 37169953Smsmith free(pattern_buf, M_TEMP); 37269953Smsmith 37369953Smsmith break; 37469953Smsmith } 375121013Sse 37669953Smsmith case PCIOCREAD: 377121013Sse case PCIOCWRITE: 37869953Smsmith io = (struct pci_io *)data; 37969953Smsmith switch(io->pi_width) { 38069953Smsmith case 4: 38169953Smsmith case 2: 38269953Smsmith case 1: 383121013Sse /* make sure register is in bounds and aligned */ 384121013Sse if (cmd == PCIOCREAD || cmd == PCIOCWRITE) 385121013Sse if (io->pi_reg < 0 || 386121013Sse io->pi_reg + io->pi_width > PCI_REGMAX || 387121013Sse io->pi_reg & (io->pi_width - 1)) 388121013Sse error = EINVAL; 38969953Smsmith 39069953Smsmith /* 39169953Smsmith * Assume that the user-level bus number is 39269953Smsmith * actually the pciN instance number. We map 39369953Smsmith * from that to the real pcib+bus combination. 39469953Smsmith */ 39569953Smsmith pci = devclass_get_device(devclass_find("pci"), 39669953Smsmith io->pi_sel.pc_bus); 39769953Smsmith if (pci) { 39869953Smsmith int b = pcib_get_bus(pci); 39969953Smsmith pcib = device_get_parent(pci); 400121013Sse if (cmd == PCIOCWRITE) 401121013Sse PCIB_WRITE_CONFIG(pcib, 402121013Sse b, 403121013Sse io->pi_sel.pc_dev, 404121013Sse io->pi_sel.pc_func, 405121013Sse io->pi_reg, 406121013Sse io->pi_data, 407121013Sse io->pi_width); 408121013Sse else 409121013Sse io->pi_data = 410121013Sse PCIB_READ_CONFIG(pcib, 411121013Sse b, 412121013Sse io->pi_sel.pc_dev, 413121013Sse io->pi_sel.pc_func, 414121013Sse io->pi_reg, 415121013Sse io->pi_width); 41669953Smsmith error = 0; 41769953Smsmith } else { 41869953Smsmith error = ENODEV; 41969953Smsmith } 42069953Smsmith break; 42169953Smsmith default: 422116702Sjmg error = EINVAL; 42369953Smsmith break; 42469953Smsmith } 42569953Smsmith break; 42669953Smsmith 42769953Smsmith default: 42869953Smsmith error = ENOTTY; 42969953Smsmith break; 43069953Smsmith } 43169953Smsmith 43269953Smsmith return (error); 43369953Smsmith} 434