pci_user.c revision 83975
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 83975 2001-09-26 20:14:03Z rwatson $ 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 7069953Smsmith#define PCI_CDEV 78 7169953Smsmith 7269953Smsmithstruct cdevsw pcicdev = { 7369953Smsmith /* open */ pci_open, 7469953Smsmith /* close */ pci_close, 7569953Smsmith /* read */ noread, 7669953Smsmith /* write */ nowrite, 7769953Smsmith /* ioctl */ pci_ioctl, 7869953Smsmith /* poll */ nopoll, 7969953Smsmith /* mmap */ nommap, 8069953Smsmith /* strategy */ nostrategy, 8169953Smsmith /* name */ "pci", 8269953Smsmith /* maj */ PCI_CDEV, 8369953Smsmith /* dump */ nodump, 8469953Smsmith /* psize */ nopsize, 8569953Smsmith /* flags */ 0, 8669953Smsmith}; 8769953Smsmith 8869953Smsmithstatic int 8983366Sjulianpci_open(dev_t dev, int oflags, int devtype, struct thread *td) 9069953Smsmith{ 9183975Srwatson int error; 9283975Srwatson 9383975Srwatson if (oflags & FWRITE) { 9483975Srwatson error = securelevel_gt(td->td_proc->p_ucred, 0); 9583975Srwatson if (error) 9683975Srwatson return (error); 9769953Smsmith } 9883975Srwatson 9983975Srwatson return (0); 10069953Smsmith} 10169953Smsmith 10269953Smsmithstatic int 10383366Sjulianpci_close(dev_t dev, int flag, int devtype, struct thread *td) 10469953Smsmith{ 10569953Smsmith return 0; 10669953Smsmith} 10769953Smsmith 10869953Smsmith/* 10969953Smsmith * Match a single pci_conf structure against an array of pci_match_conf 11069953Smsmith * structures. The first argument, 'matches', is an array of num_matches 11169953Smsmith * pci_match_conf structures. match_buf is a pointer to the pci_conf 11269953Smsmith * structure that will be compared to every entry in the matches array. 11369953Smsmith * This function returns 1 on failure, 0 on success. 11469953Smsmith */ 11569953Smsmithstatic int 11669953Smsmithpci_conf_match(struct pci_match_conf *matches, int num_matches, 11769953Smsmith struct pci_conf *match_buf) 11869953Smsmith{ 11969953Smsmith int i; 12069953Smsmith 12169953Smsmith if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0)) 12269953Smsmith return(1); 12369953Smsmith 12469953Smsmith for (i = 0; i < num_matches; i++) { 12569953Smsmith /* 12669953Smsmith * I'm not sure why someone would do this...but... 12769953Smsmith */ 12869953Smsmith if (matches[i].flags == PCI_GETCONF_NO_MATCH) 12969953Smsmith continue; 13069953Smsmith 13169953Smsmith /* 13269953Smsmith * Look at each of the match flags. If it's set, do the 13369953Smsmith * comparison. If the comparison fails, we don't have a 13469953Smsmith * match, go on to the next item if there is one. 13569953Smsmith */ 13669953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_BUS) != 0) 13769953Smsmith && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus)) 13869953Smsmith continue; 13969953Smsmith 14069953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_DEV) != 0) 14169953Smsmith && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev)) 14269953Smsmith continue; 14369953Smsmith 14469953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC) != 0) 14569953Smsmith && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func)) 14669953Smsmith continue; 14769953Smsmith 14869953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR) != 0) 14969953Smsmith && (match_buf->pc_vendor != matches[i].pc_vendor)) 15069953Smsmith continue; 15169953Smsmith 15269953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE) != 0) 15369953Smsmith && (match_buf->pc_device != matches[i].pc_device)) 15469953Smsmith continue; 15569953Smsmith 15669953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS) != 0) 15769953Smsmith && (match_buf->pc_class != matches[i].pc_class)) 15869953Smsmith continue; 15969953Smsmith 16069953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT) != 0) 16169953Smsmith && (match_buf->pd_unit != matches[i].pd_unit)) 16269953Smsmith continue; 16369953Smsmith 16469953Smsmith if (((matches[i].flags & PCI_GETCONF_MATCH_NAME) != 0) 16569953Smsmith && (strncmp(matches[i].pd_name, match_buf->pd_name, 16669953Smsmith sizeof(match_buf->pd_name)) != 0)) 16769953Smsmith continue; 16869953Smsmith 16969953Smsmith return(0); 17069953Smsmith } 17169953Smsmith 17269953Smsmith return(1); 17369953Smsmith} 17469953Smsmith 17569953Smsmithstatic int 17683366Sjulianpci_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td) 17769953Smsmith{ 17869953Smsmith device_t pci, pcib; 17969953Smsmith struct pci_io *io; 18069953Smsmith const char *name; 18169953Smsmith int error; 18269953Smsmith 18369953Smsmith if (!(flag & FWRITE)) 18469953Smsmith return EPERM; 18569953Smsmith 18669953Smsmith 18769953Smsmith switch(cmd) { 18869953Smsmith case PCIOCGETCONF: 18969953Smsmith { 19069953Smsmith struct pci_devinfo *dinfo; 19169953Smsmith struct pci_conf_io *cio; 19269953Smsmith struct devlist *devlist_head; 19369953Smsmith struct pci_match_conf *pattern_buf; 19469953Smsmith int num_patterns; 19569953Smsmith size_t iolen; 19669953Smsmith int ionum, i; 19769953Smsmith 19869953Smsmith cio = (struct pci_conf_io *)data; 19969953Smsmith 20069953Smsmith num_patterns = 0; 20169953Smsmith dinfo = NULL; 20269953Smsmith 20369953Smsmith /* 20469953Smsmith * Hopefully the user won't pass in a null pointer, but it 20569953Smsmith * can't hurt to check. 20669953Smsmith */ 20769953Smsmith if (cio == NULL) { 20869953Smsmith error = EINVAL; 20969953Smsmith break; 21069953Smsmith } 21169953Smsmith 21269953Smsmith /* 21369953Smsmith * If the user specified an offset into the device list, 21469953Smsmith * but the list has changed since they last called this 21569953Smsmith * ioctl, tell them that the list has changed. They will 21669953Smsmith * have to get the list from the beginning. 21769953Smsmith */ 21869953Smsmith if ((cio->offset != 0) 21969953Smsmith && (cio->generation != pci_generation)){ 22069953Smsmith cio->num_matches = 0; 22169953Smsmith cio->status = PCI_GETCONF_LIST_CHANGED; 22269953Smsmith error = 0; 22369953Smsmith break; 22469953Smsmith } 22569953Smsmith 22669953Smsmith /* 22769953Smsmith * Check to see whether the user has asked for an offset 22869953Smsmith * past the end of our list. 22969953Smsmith */ 23069953Smsmith if (cio->offset >= pci_numdevs) { 23169953Smsmith cio->num_matches = 0; 23269953Smsmith cio->status = PCI_GETCONF_LAST_DEVICE; 23369953Smsmith error = 0; 23469953Smsmith break; 23569953Smsmith } 23669953Smsmith 23769953Smsmith /* get the head of the device queue */ 23869953Smsmith devlist_head = &pci_devq; 23969953Smsmith 24069953Smsmith /* 24169953Smsmith * Determine how much room we have for pci_conf structures. 24269953Smsmith * Round the user's buffer size down to the nearest 24369953Smsmith * multiple of sizeof(struct pci_conf) in case the user 24469953Smsmith * didn't specify a multiple of that size. 24569953Smsmith */ 24669953Smsmith iolen = min(cio->match_buf_len - 24769953Smsmith (cio->match_buf_len % sizeof(struct pci_conf)), 24869953Smsmith pci_numdevs * sizeof(struct pci_conf)); 24969953Smsmith 25069953Smsmith /* 25169953Smsmith * Since we know that iolen is a multiple of the size of 25269953Smsmith * the pciconf union, it's okay to do this. 25369953Smsmith */ 25469953Smsmith ionum = iolen / sizeof(struct pci_conf); 25569953Smsmith 25669953Smsmith /* 25769953Smsmith * If this test is true, the user wants the pci_conf 25869953Smsmith * structures returned to match the supplied entries. 25969953Smsmith */ 26069953Smsmith if ((cio->num_patterns > 0) 26169953Smsmith && (cio->pat_buf_len > 0)) { 26269953Smsmith /* 26369953Smsmith * pat_buf_len needs to be: 26469953Smsmith * num_patterns * sizeof(struct pci_match_conf) 26569953Smsmith * While it is certainly possible the user just 26669953Smsmith * allocated a large buffer, but set the number of 26769953Smsmith * matches correctly, it is far more likely that 26869953Smsmith * their kernel doesn't match the userland utility 26969953Smsmith * they're using. It's also possible that the user 27069953Smsmith * forgot to initialize some variables. Yes, this 27169953Smsmith * may be overly picky, but I hazard to guess that 27269953Smsmith * it's far more likely to just catch folks that 27369953Smsmith * updated their kernel but not their userland. 27469953Smsmith */ 27569953Smsmith if ((cio->num_patterns * 27669953Smsmith sizeof(struct pci_match_conf)) != cio->pat_buf_len){ 27769953Smsmith /* The user made a mistake, return an error*/ 27869953Smsmith cio->status = PCI_GETCONF_ERROR; 27969953Smsmith printf("pci_ioctl: pat_buf_len %d != " 28069953Smsmith "num_patterns (%d) * sizeof(struct " 28169953Smsmith "pci_match_conf) (%d)\npci_ioctl: " 28269953Smsmith "pat_buf_len should be = %d\n", 28369953Smsmith cio->pat_buf_len, cio->num_patterns, 28469953Smsmith (int)sizeof(struct pci_match_conf), 28569953Smsmith (int)sizeof(struct pci_match_conf) * 28669953Smsmith cio->num_patterns); 28769953Smsmith printf("pci_ioctl: do your headers match your " 28869953Smsmith "kernel?\n"); 28969953Smsmith cio->num_matches = 0; 29069953Smsmith error = EINVAL; 29169953Smsmith break; 29269953Smsmith } 29369953Smsmith 29469953Smsmith /* 29569953Smsmith * Check the user's buffer to make sure it's readable. 29669953Smsmith */ 29769953Smsmith if (!useracc((caddr_t)cio->patterns, 29869953Smsmith cio->pat_buf_len, VM_PROT_READ)) { 29969953Smsmith printf("pci_ioctl: pattern buffer %p, " 30069953Smsmith "length %u isn't user accessible for" 30169953Smsmith " READ\n", cio->patterns, 30269953Smsmith cio->pat_buf_len); 30369953Smsmith error = EACCES; 30469953Smsmith break; 30569953Smsmith } 30669953Smsmith /* 30769953Smsmith * Allocate a buffer to hold the patterns. 30869953Smsmith */ 30969953Smsmith pattern_buf = malloc(cio->pat_buf_len, M_TEMP, 31069953Smsmith M_WAITOK); 31169953Smsmith error = copyin(cio->patterns, pattern_buf, 31269953Smsmith cio->pat_buf_len); 31369953Smsmith if (error != 0) 31469953Smsmith break; 31569953Smsmith num_patterns = cio->num_patterns; 31669953Smsmith 31769953Smsmith } else if ((cio->num_patterns > 0) 31869953Smsmith || (cio->pat_buf_len > 0)) { 31969953Smsmith /* 32069953Smsmith * The user made a mistake, spit out an error. 32169953Smsmith */ 32269953Smsmith cio->status = PCI_GETCONF_ERROR; 32369953Smsmith cio->num_matches = 0; 32469953Smsmith printf("pci_ioctl: invalid GETCONF arguments\n"); 32569953Smsmith error = EINVAL; 32669953Smsmith break; 32769953Smsmith } else 32869953Smsmith pattern_buf = NULL; 32969953Smsmith 33069953Smsmith /* 33169953Smsmith * Make sure we can write to the match buffer. 33269953Smsmith */ 33369953Smsmith if (!useracc((caddr_t)cio->matches, 33469953Smsmith cio->match_buf_len, VM_PROT_WRITE)) { 33569953Smsmith printf("pci_ioctl: match buffer %p, length %u " 33669953Smsmith "isn't user accessible for WRITE\n", 33769953Smsmith cio->matches, cio->match_buf_len); 33869953Smsmith error = EACCES; 33969953Smsmith break; 34069953Smsmith } 34169953Smsmith 34269953Smsmith /* 34369953Smsmith * Go through the list of devices and copy out the devices 34469953Smsmith * that match the user's criteria. 34569953Smsmith */ 34669953Smsmith for (cio->num_matches = 0, error = 0, i = 0, 34769953Smsmith dinfo = STAILQ_FIRST(devlist_head); 34869953Smsmith (dinfo != NULL) && (cio->num_matches < ionum) 34969953Smsmith && (error == 0) && (i < pci_numdevs); 35069953Smsmith dinfo = STAILQ_NEXT(dinfo, pci_links), i++) { 35169953Smsmith 35269953Smsmith if (i < cio->offset) 35369953Smsmith continue; 35469953Smsmith 35569953Smsmith /* Populate pd_name and pd_unit */ 35669953Smsmith name = NULL; 35769953Smsmith if (dinfo->cfg.dev && dinfo->conf.pd_name[0] == '\0') 35869953Smsmith name = device_get_name(dinfo->cfg.dev); 35969953Smsmith if (name) { 36069953Smsmith strncpy(dinfo->conf.pd_name, name, 36169953Smsmith sizeof(dinfo->conf.pd_name)); 36269953Smsmith dinfo->conf.pd_name[PCI_MAXNAMELEN] = 0; 36369953Smsmith dinfo->conf.pd_unit = 36469953Smsmith device_get_unit(dinfo->cfg.dev); 36569953Smsmith } 36669953Smsmith 36769953Smsmith if ((pattern_buf == NULL) || 36869953Smsmith (pci_conf_match(pattern_buf, num_patterns, 36969953Smsmith &dinfo->conf) == 0)) { 37069953Smsmith 37169953Smsmith /* 37269953Smsmith * If we've filled up the user's buffer, 37369953Smsmith * break out at this point. Since we've 37469953Smsmith * got a match here, we'll pick right back 37569953Smsmith * up at the matching entry. We can also 37669953Smsmith * tell the user that there are more matches 37769953Smsmith * left. 37869953Smsmith */ 37969953Smsmith if (cio->num_matches >= ionum) 38069953Smsmith break; 38169953Smsmith 38269953Smsmith error = copyout(&dinfo->conf, 38369953Smsmith &cio->matches[cio->num_matches], 38469953Smsmith sizeof(struct pci_conf)); 38569953Smsmith cio->num_matches++; 38669953Smsmith } 38769953Smsmith } 38869953Smsmith 38969953Smsmith /* 39069953Smsmith * Set the pointer into the list, so if the user is getting 39169953Smsmith * n records at a time, where n < pci_numdevs, 39269953Smsmith */ 39369953Smsmith cio->offset = i; 39469953Smsmith 39569953Smsmith /* 39669953Smsmith * Set the generation, the user will need this if they make 39769953Smsmith * another ioctl call with offset != 0. 39869953Smsmith */ 39969953Smsmith cio->generation = pci_generation; 40069953Smsmith 40169953Smsmith /* 40269953Smsmith * If this is the last device, inform the user so he won't 40369953Smsmith * bother asking for more devices. If dinfo isn't NULL, we 40469953Smsmith * know that there are more matches in the list because of 40569953Smsmith * the way the traversal is done. 40669953Smsmith */ 40769953Smsmith if (dinfo == NULL) 40869953Smsmith cio->status = PCI_GETCONF_LAST_DEVICE; 40969953Smsmith else 41069953Smsmith cio->status = PCI_GETCONF_MORE_DEVS; 41169953Smsmith 41269953Smsmith if (pattern_buf != NULL) 41369953Smsmith free(pattern_buf, M_TEMP); 41469953Smsmith 41569953Smsmith break; 41669953Smsmith } 41769953Smsmith case PCIOCREAD: 41869953Smsmith io = (struct pci_io *)data; 41969953Smsmith switch(io->pi_width) { 42069953Smsmith case 4: 42169953Smsmith case 2: 42269953Smsmith case 1: 42369953Smsmith /* 42469953Smsmith * Assume that the user-level bus number is 42569953Smsmith * actually the pciN instance number. We map 42669953Smsmith * from that to the real pcib+bus combination. 42769953Smsmith */ 42869953Smsmith pci = devclass_get_device(devclass_find("pci"), 42969953Smsmith io->pi_sel.pc_bus); 43069953Smsmith if (pci) { 43169953Smsmith int b = pcib_get_bus(pci); 43269953Smsmith pcib = device_get_parent(pci); 43369953Smsmith io->pi_data = 43469953Smsmith PCIB_READ_CONFIG(pcib, 43569953Smsmith b, 43669953Smsmith io->pi_sel.pc_dev, 43769953Smsmith io->pi_sel.pc_func, 43869953Smsmith io->pi_reg, 43969953Smsmith io->pi_width); 44069953Smsmith error = 0; 44169953Smsmith } else { 44269953Smsmith error = ENODEV; 44369953Smsmith } 44469953Smsmith break; 44569953Smsmith default: 44669953Smsmith error = ENODEV; 44769953Smsmith break; 44869953Smsmith } 44969953Smsmith break; 45069953Smsmith 45169953Smsmith case PCIOCWRITE: 45269953Smsmith io = (struct pci_io *)data; 45369953Smsmith switch(io->pi_width) { 45469953Smsmith case 4: 45569953Smsmith case 2: 45669953Smsmith case 1: 45769953Smsmith /* 45869953Smsmith * Assume that the user-level bus number is 45969953Smsmith * actually the pciN instance number. We map 46069953Smsmith * from that to the real pcib+bus combination. 46169953Smsmith */ 46269953Smsmith pci = devclass_get_device(devclass_find("pci"), 46369953Smsmith io->pi_sel.pc_bus); 46469953Smsmith if (pci) { 46569953Smsmith int b = pcib_get_bus(pci); 46669953Smsmith pcib = device_get_parent(pci); 46769953Smsmith PCIB_WRITE_CONFIG(pcib, 46869953Smsmith b, 46969953Smsmith io->pi_sel.pc_dev, 47069953Smsmith io->pi_sel.pc_func, 47169953Smsmith io->pi_reg, 47269953Smsmith io->pi_data, 47369953Smsmith io->pi_width); 47469953Smsmith error = 0; 47569953Smsmith } else { 47669953Smsmith error = ENODEV; 47769953Smsmith } 47869953Smsmith break; 47969953Smsmith default: 48069953Smsmith error = ENODEV; 48169953Smsmith break; 48269953Smsmith } 48369953Smsmith break; 48469953Smsmith 48569953Smsmith default: 48669953Smsmith error = ENOTTY; 48769953Smsmith break; 48869953Smsmith } 48969953Smsmith 49069953Smsmith return (error); 49169953Smsmith} 49269953Smsmith 493