pci_user.c revision 116704
169953Smsmith/* 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 * $FreeBSD: head/sys/dev/pci/pci_user.c 116704 2003-06-23 03:17:03Z jmg $ 2769953Smsmith * 2869953Smsmith */ 2969953Smsmith 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> 5469953Smsmith#include <pci/pcireg.h> 5569953Smsmith#include <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 = { 77111815Sphk .d_open = pci_open, 78111815Sphk .d_close = pci_close, 79111815Sphk .d_ioctl = pci_ioctl, 80111815Sphk .d_name = "pci", 81111815Sphk .d_maj = PCI_CDEV, 8269953Smsmith}; 8369953Smsmith 8469953Smsmithstatic int 8583366Sjulianpci_open(dev_t dev, int oflags, int devtype, struct thread *td) 8669953Smsmith{ 8783975Srwatson int error; 8883975Srwatson 8983975Srwatson if (oflags & FWRITE) { 9091406Sjhb error = securelevel_gt(td->td_ucred, 0); 9183975Srwatson if (error) 9283975Srwatson return (error); 9369953Smsmith } 9483975Srwatson 9583975Srwatson return (0); 9669953Smsmith} 9769953Smsmith 9869953Smsmithstatic int 9983366Sjulianpci_close(dev_t dev, int flag, int devtype, struct thread *td) 10069953Smsmith{ 10169953Smsmith return 0; 10269953Smsmith} 10369953Smsmith 10469953Smsmith/* 10569953Smsmith * Match a single pci_conf structure against an array of pci_match_conf 10669953Smsmith * structures. The first argument, 'matches', is an array of num_matches 10769953Smsmith * pci_match_conf structures. match_buf is a pointer to the pci_conf 10869953Smsmith * structure that will be compared to every entry in the matches array. 10969953Smsmith * This function returns 1 on failure, 0 on success. 11069953Smsmith */ 11169953Smsmithstatic int 11269953Smsmithpci_conf_match(struct pci_match_conf *matches, int num_matches, 11369953Smsmith struct pci_conf *match_buf) 11469953Smsmith{ 11569953Smsmith int i; 11669953Smsmith 11769953Smsmith if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0)) 11869953Smsmith return(1); 11969953Smsmith 12069953Smsmith for (i = 0; i < num_matches; i++) { 12169953Smsmith /* 12269953Smsmith * I'm not sure why someone would do this...but... 12369953Smsmith */ 12469953Smsmith if (matches[i].flags == PCI_GETCONF_NO_MATCH) 12569953Smsmith continue; 12669953Smsmith 12769953Smsmith /* 12869953Smsmith * Look at each of the match flags. If it's set, do the 12969953Smsmith * comparison. If the comparison fails, we don't have a 13069953Smsmith * match, go on to the next item if there is one. 13169953Smsmith */ 13269953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_BUS) != 0) 13369953Smsmith && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus)) 13469953Smsmith continue; 13569953Smsmith 13669953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_DEV) != 0) 13769953Smsmith && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev)) 13869953Smsmith continue; 13969953Smsmith 14069953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC) != 0) 14169953Smsmith && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func)) 14269953Smsmith continue; 14369953Smsmith 14469953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR) != 0) 14569953Smsmith && (match_buf->pc_vendor != matches[i].pc_vendor)) 14669953Smsmith continue; 14769953Smsmith 14869953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE) != 0) 14969953Smsmith && (match_buf->pc_device != matches[i].pc_device)) 15069953Smsmith continue; 15169953Smsmith 15269953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS) != 0) 15369953Smsmith && (match_buf->pc_class != matches[i].pc_class)) 15469953Smsmith continue; 15569953Smsmith 15669953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT) != 0) 15769953Smsmith && (match_buf->pd_unit != matches[i].pd_unit)) 15869953Smsmith continue; 15969953Smsmith 16069953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_NAME) != 0) 16169953Smsmith && (strncmp(matches[i].pd_name, match_buf->pd_name, 16269953Smsmith sizeof(match_buf->pd_name)) != 0)) 16369953Smsmith continue; 16469953Smsmith 16569953Smsmith return(0); 16669953Smsmith } 16769953Smsmith 16869953Smsmith return(1); 16969953Smsmith} 17069953Smsmith 17169953Smsmithstatic int 17283366Sjulianpci_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td) 17369953Smsmith{ 17469953Smsmith device_t pci, pcib; 17569953Smsmith struct pci_io *io; 17669953Smsmith const char *name; 17769953Smsmith int error; 17869953Smsmith 179116702Sjmg if (!(flag & FWRITE) && cmd != PCIOCGETCONF) 18069953Smsmith return EPERM; 18169953Smsmith 182116702Sjmg /* make sure register is in bounds and aligned */ 183116702Sjmg if (cmd == PCIOCREAD || cmd == PCIOCWRITE) 184116702Sjmg if (io->pi_reg < 0 || io->pi_reg + io->pi_width > PCI_REGMAX || 185116702Sjmg io->pi_reg & (io->pi_width - 1)) 186116702Sjmg error = EINVAL; 18769953Smsmith 18869953Smsmith switch(cmd) { 18969953Smsmith case PCIOCGETCONF: 19069953Smsmith { 19169953Smsmith struct pci_devinfo *dinfo; 19269953Smsmith struct pci_conf_io *cio; 19369953Smsmith struct devlist *devlist_head; 19469953Smsmith struct pci_match_conf *pattern_buf; 19569953Smsmith int num_patterns; 19669953Smsmith size_t iolen; 19769953Smsmith int ionum, i; 19869953Smsmith 19969953Smsmith cio = (struct pci_conf_io *)data; 20069953Smsmith 20169953Smsmith num_patterns = 0; 20269953Smsmith dinfo = NULL; 20369953Smsmith 20469953Smsmith /* 20569953Smsmith * If the user specified an offset into the device list, 20669953Smsmith * but the list has changed since they last called this 20769953Smsmith * ioctl, tell them that the list has changed. They will 20869953Smsmith * have to get the list from the beginning. 20969953Smsmith */ 21069953Smsmith if ((cio->offset != 0) 21169953Smsmith && (cio->generation != pci_generation)){ 21269953Smsmith cio->num_matches = 0; 21369953Smsmith cio->status = PCI_GETCONF_LIST_CHANGED; 21469953Smsmith error = 0; 21569953Smsmith break; 21669953Smsmith } 21769953Smsmith 21869953Smsmith /* 21969953Smsmith * Check to see whether the user has asked for an offset 22069953Smsmith * past the end of our list. 22169953Smsmith */ 22269953Smsmith if (cio->offset >= pci_numdevs) { 22369953Smsmith cio->num_matches = 0; 22469953Smsmith cio->status = PCI_GETCONF_LAST_DEVICE; 22569953Smsmith error = 0; 22669953Smsmith break; 22769953Smsmith } 22869953Smsmith 22969953Smsmith /* get the head of the device queue */ 23069953Smsmith devlist_head = &pci_devq; 23169953Smsmith 23269953Smsmith /* 23369953Smsmith * Determine how much room we have for pci_conf structures. 23469953Smsmith * Round the user's buffer size down to the nearest 23569953Smsmith * multiple of sizeof(struct pci_conf) in case the user 23669953Smsmith * didn't specify a multiple of that size. 23769953Smsmith */ 23869953Smsmith iolen = min(cio->match_buf_len - 23969953Smsmith (cio->match_buf_len % sizeof(struct pci_conf)), 24069953Smsmith pci_numdevs * sizeof(struct pci_conf)); 24169953Smsmith 24269953Smsmith /* 24369953Smsmith * Since we know that iolen is a multiple of the size of 24469953Smsmith * the pciconf union, it's okay to do this. 24569953Smsmith */ 24669953Smsmith ionum = iolen / sizeof(struct pci_conf); 24769953Smsmith 24869953Smsmith /* 24969953Smsmith * If this test is true, the user wants the pci_conf 25069953Smsmith * structures returned to match the supplied entries. 25169953Smsmith */ 252116704Sjmg if ((cio->num_patterns > 0) && (cio->num_patterns < pci_numdevs) 25369953Smsmith && (cio->pat_buf_len > 0)) { 25469953Smsmith /* 25569953Smsmith * pat_buf_len needs to be: 25669953Smsmith * num_patterns * sizeof(struct pci_match_conf) 25769953Smsmith * While it is certainly possible the user just 25869953Smsmith * allocated a large buffer, but set the number of 25969953Smsmith * matches correctly, it is far more likely that 26069953Smsmith * their kernel doesn't match the userland utility 26169953Smsmith * they're using. It's also possible that the user 26269953Smsmith * forgot to initialize some variables. Yes, this 26369953Smsmith * may be overly picky, but I hazard to guess that 26469953Smsmith * it's far more likely to just catch folks that 26569953Smsmith * updated their kernel but not their userland. 26669953Smsmith */ 26769953Smsmith if ((cio->num_patterns * 26869953Smsmith sizeof(struct pci_match_conf)) != cio->pat_buf_len){ 26969953Smsmith /* The user made a mistake, return an error*/ 27069953Smsmith cio->status = PCI_GETCONF_ERROR; 27169953Smsmith cio->num_matches = 0; 27269953Smsmith error = EINVAL; 27369953Smsmith break; 27469953Smsmith } 27569953Smsmith 27669953Smsmith /* 27769953Smsmith * Allocate a buffer to hold the patterns. 27869953Smsmith */ 27969953Smsmith pattern_buf = malloc(cio->pat_buf_len, M_TEMP, 280111119Simp M_WAITOK); 28169953Smsmith error = copyin(cio->patterns, pattern_buf, 28269953Smsmith cio->pat_buf_len); 283116702Sjmg if (error != 0) { 284116702Sjmg error = EINVAL; 285116702Sjmg goto getconfexit; 286116702Sjmg } 28769953Smsmith num_patterns = cio->num_patterns; 28869953Smsmith 28969953Smsmith } else if ((cio->num_patterns > 0) 29069953Smsmith || (cio->pat_buf_len > 0)) { 29169953Smsmith /* 29269953Smsmith * The user made a mistake, spit out an error. 29369953Smsmith */ 29469953Smsmith cio->status = PCI_GETCONF_ERROR; 29569953Smsmith cio->num_matches = 0; 29669953Smsmith error = EINVAL; 29769953Smsmith break; 29869953Smsmith } else 29969953Smsmith pattern_buf = NULL; 30069953Smsmith 30169953Smsmith /* 30269953Smsmith * Go through the list of devices and copy out the devices 30369953Smsmith * that match the user's criteria. 30469953Smsmith */ 30569953Smsmith for (cio->num_matches = 0, error = 0, i = 0, 30669953Smsmith dinfo = STAILQ_FIRST(devlist_head); 30769953Smsmith (dinfo != NULL) && (cio->num_matches < ionum) 308116702Sjmg && (error == 0) && (i < pci_numdevs) && (dinfo != NULL); 30969953Smsmith dinfo = STAILQ_NEXT(dinfo, pci_links), i++) { 31069953Smsmith 31169953Smsmith if (i < cio->offset) 31269953Smsmith continue; 31369953Smsmith 31469953Smsmith /* Populate pd_name and pd_unit */ 31569953Smsmith name = NULL; 31669953Smsmith if (dinfo->cfg.dev && dinfo->conf.pd_name[0] == '\0') 31769953Smsmith name = device_get_name(dinfo->cfg.dev); 31869953Smsmith if (name) { 31969953Smsmith strncpy(dinfo->conf.pd_name, name, 32069953Smsmith sizeof(dinfo->conf.pd_name)); 32169953Smsmith dinfo->conf.pd_name[PCI_MAXNAMELEN] = 0; 32269953Smsmith dinfo->conf.pd_unit = 32369953Smsmith device_get_unit(dinfo->cfg.dev); 32469953Smsmith } 32569953Smsmith 32669953Smsmith if ((pattern_buf == NULL) || 32769953Smsmith (pci_conf_match(pattern_buf, num_patterns, 32869953Smsmith &dinfo->conf) == 0)) { 32969953Smsmith 33069953Smsmith /* 33169953Smsmith * If we've filled up the user's buffer, 33269953Smsmith * break out at this point. Since we've 33369953Smsmith * got a match here, we'll pick right back 33469953Smsmith * up at the matching entry. We can also 33569953Smsmith * tell the user that there are more matches 33669953Smsmith * left. 33769953Smsmith */ 33869953Smsmith if (cio->num_matches >= ionum) 33969953Smsmith break; 34069953Smsmith 341116702Sjmg /* only if can copy it out do we count it */ 342116702Sjmg if (!(error = copyout(&dinfo->conf, 343116702Sjmg &cio->matches[cio->num_matches], 344116702Sjmg sizeof(struct pci_conf)))) { 345116702Sjmg cio->num_matches++; 346116702Sjmg } 34769953Smsmith } 34869953Smsmith } 34969953Smsmith 35069953Smsmith /* 35169953Smsmith * Set the pointer into the list, so if the user is getting 35269953Smsmith * n records at a time, where n < pci_numdevs, 35369953Smsmith */ 35469953Smsmith cio->offset = i; 35569953Smsmith 35669953Smsmith /* 35769953Smsmith * Set the generation, the user will need this if they make 35869953Smsmith * another ioctl call with offset != 0. 35969953Smsmith */ 36069953Smsmith cio->generation = pci_generation; 36169953Smsmith 36269953Smsmith /* 36369953Smsmith * If this is the last device, inform the user so he won't 36469953Smsmith * bother asking for more devices. If dinfo isn't NULL, we 36569953Smsmith * know that there are more matches in the list because of 36669953Smsmith * the way the traversal is done. 36769953Smsmith */ 36869953Smsmith if (dinfo == NULL) 36969953Smsmith cio->status = PCI_GETCONF_LAST_DEVICE; 37069953Smsmith else 37169953Smsmith cio->status = PCI_GETCONF_MORE_DEVS; 37269953Smsmith 373116702Sjmggetconfexit: 37469953Smsmith if (pattern_buf != NULL) 37569953Smsmith free(pattern_buf, M_TEMP); 37669953Smsmith 37769953Smsmith break; 37869953Smsmith } 37969953Smsmith case PCIOCREAD: 38069953Smsmith io = (struct pci_io *)data; 38169953Smsmith switch(io->pi_width) { 38269953Smsmith case 4: 38369953Smsmith case 2: 38469953Smsmith case 1: 38569953Smsmith /* 38669953Smsmith * Assume that the user-level bus number is 38769953Smsmith * actually the pciN instance number. We map 38869953Smsmith * from that to the real pcib+bus combination. 38969953Smsmith */ 39069953Smsmith pci = devclass_get_device(devclass_find("pci"), 39169953Smsmith io->pi_sel.pc_bus); 39269953Smsmith if (pci) { 39369953Smsmith int b = pcib_get_bus(pci); 39469953Smsmith pcib = device_get_parent(pci); 39569953Smsmith io->pi_data = 39669953Smsmith PCIB_READ_CONFIG(pcib, 39769953Smsmith b, 39869953Smsmith io->pi_sel.pc_dev, 39969953Smsmith io->pi_sel.pc_func, 40069953Smsmith io->pi_reg, 40169953Smsmith io->pi_width); 40269953Smsmith error = 0; 40369953Smsmith } else { 40469953Smsmith error = ENODEV; 40569953Smsmith } 40669953Smsmith break; 40769953Smsmith default: 408116702Sjmg error = EINVAL; 40969953Smsmith break; 41069953Smsmith } 41169953Smsmith break; 41269953Smsmith 41369953Smsmith case PCIOCWRITE: 41469953Smsmith io = (struct pci_io *)data; 41569953Smsmith switch(io->pi_width) { 41669953Smsmith case 4: 41769953Smsmith case 2: 41869953Smsmith case 1: 41969953Smsmith /* 42069953Smsmith * Assume that the user-level bus number is 42169953Smsmith * actually the pciN instance number. We map 42269953Smsmith * from that to the real pcib+bus combination. 42369953Smsmith */ 42469953Smsmith pci = devclass_get_device(devclass_find("pci"), 42569953Smsmith io->pi_sel.pc_bus); 42669953Smsmith if (pci) { 42769953Smsmith int b = pcib_get_bus(pci); 42869953Smsmith pcib = device_get_parent(pci); 42969953Smsmith PCIB_WRITE_CONFIG(pcib, 43069953Smsmith b, 43169953Smsmith io->pi_sel.pc_dev, 43269953Smsmith io->pi_sel.pc_func, 43369953Smsmith io->pi_reg, 43469953Smsmith io->pi_data, 43569953Smsmith io->pi_width); 43669953Smsmith error = 0; 43769953Smsmith } else { 43869953Smsmith error = ENODEV; 43969953Smsmith } 44069953Smsmith break; 44169953Smsmith default: 442116702Sjmg error = EINVAL; 44369953Smsmith break; 44469953Smsmith } 44569953Smsmith break; 44669953Smsmith 44769953Smsmith default: 44869953Smsmith error = ENOTTY; 44969953Smsmith break; 45069953Smsmith } 45169953Smsmith 45269953Smsmith return (error); 45369953Smsmith} 45469953Smsmith 455