pci_user.c revision 111815
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 111815 2003-03-03 12:15:54Z phk $ 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 17969953Smsmith if (!(flag & FWRITE)) 18069953Smsmith return EPERM; 18169953Smsmith 18269953Smsmith 18369953Smsmith switch(cmd) { 18469953Smsmith case PCIOCGETCONF: 18569953Smsmith { 18669953Smsmith struct pci_devinfo *dinfo; 18769953Smsmith struct pci_conf_io *cio; 18869953Smsmith struct devlist *devlist_head; 18969953Smsmith struct pci_match_conf *pattern_buf; 19069953Smsmith int num_patterns; 19169953Smsmith size_t iolen; 19269953Smsmith int ionum, i; 19369953Smsmith 19469953Smsmith cio = (struct pci_conf_io *)data; 19569953Smsmith 19669953Smsmith num_patterns = 0; 19769953Smsmith dinfo = NULL; 19869953Smsmith 19969953Smsmith /* 20069953Smsmith * Hopefully the user won't pass in a null pointer, but it 20169953Smsmith * can't hurt to check. 20269953Smsmith */ 20369953Smsmith if (cio == NULL) { 20469953Smsmith error = EINVAL; 20569953Smsmith break; 20669953Smsmith } 20769953Smsmith 20869953Smsmith /* 20969953Smsmith * If the user specified an offset into the device list, 21069953Smsmith * but the list has changed since they last called this 21169953Smsmith * ioctl, tell them that the list has changed. They will 21269953Smsmith * have to get the list from the beginning. 21369953Smsmith */ 21469953Smsmith if ((cio->offset != 0) 21569953Smsmith && (cio->generation != pci_generation)){ 21669953Smsmith cio->num_matches = 0; 21769953Smsmith cio->status = PCI_GETCONF_LIST_CHANGED; 21869953Smsmith error = 0; 21969953Smsmith break; 22069953Smsmith } 22169953Smsmith 22269953Smsmith /* 22369953Smsmith * Check to see whether the user has asked for an offset 22469953Smsmith * past the end of our list. 22569953Smsmith */ 22669953Smsmith if (cio->offset >= pci_numdevs) { 22769953Smsmith cio->num_matches = 0; 22869953Smsmith cio->status = PCI_GETCONF_LAST_DEVICE; 22969953Smsmith error = 0; 23069953Smsmith break; 23169953Smsmith } 23269953Smsmith 23369953Smsmith /* get the head of the device queue */ 23469953Smsmith devlist_head = &pci_devq; 23569953Smsmith 23669953Smsmith /* 23769953Smsmith * Determine how much room we have for pci_conf structures. 23869953Smsmith * Round the user's buffer size down to the nearest 23969953Smsmith * multiple of sizeof(struct pci_conf) in case the user 24069953Smsmith * didn't specify a multiple of that size. 24169953Smsmith */ 24269953Smsmith iolen = min(cio->match_buf_len - 24369953Smsmith (cio->match_buf_len % sizeof(struct pci_conf)), 24469953Smsmith pci_numdevs * sizeof(struct pci_conf)); 24569953Smsmith 24669953Smsmith /* 24769953Smsmith * Since we know that iolen is a multiple of the size of 24869953Smsmith * the pciconf union, it's okay to do this. 24969953Smsmith */ 25069953Smsmith ionum = iolen / sizeof(struct pci_conf); 25169953Smsmith 25269953Smsmith /* 25369953Smsmith * If this test is true, the user wants the pci_conf 25469953Smsmith * structures returned to match the supplied entries. 25569953Smsmith */ 25669953Smsmith if ((cio->num_patterns > 0) 25769953Smsmith && (cio->pat_buf_len > 0)) { 25869953Smsmith /* 25969953Smsmith * pat_buf_len needs to be: 26069953Smsmith * num_patterns * sizeof(struct pci_match_conf) 26169953Smsmith * While it is certainly possible the user just 26269953Smsmith * allocated a large buffer, but set the number of 26369953Smsmith * matches correctly, it is far more likely that 26469953Smsmith * their kernel doesn't match the userland utility 26569953Smsmith * they're using. It's also possible that the user 26669953Smsmith * forgot to initialize some variables. Yes, this 26769953Smsmith * may be overly picky, but I hazard to guess that 26869953Smsmith * it's far more likely to just catch folks that 26969953Smsmith * updated their kernel but not their userland. 27069953Smsmith */ 27169953Smsmith if ((cio->num_patterns * 27269953Smsmith sizeof(struct pci_match_conf)) != cio->pat_buf_len){ 27369953Smsmith /* The user made a mistake, return an error*/ 27469953Smsmith cio->status = PCI_GETCONF_ERROR; 27569953Smsmith printf("pci_ioctl: pat_buf_len %d != " 27669953Smsmith "num_patterns (%d) * sizeof(struct " 27769953Smsmith "pci_match_conf) (%d)\npci_ioctl: " 27869953Smsmith "pat_buf_len should be = %d\n", 27969953Smsmith cio->pat_buf_len, cio->num_patterns, 28069953Smsmith (int)sizeof(struct pci_match_conf), 28169953Smsmith (int)sizeof(struct pci_match_conf) * 28269953Smsmith cio->num_patterns); 28369953Smsmith printf("pci_ioctl: do your headers match your " 28469953Smsmith "kernel?\n"); 28569953Smsmith cio->num_matches = 0; 28669953Smsmith error = EINVAL; 28769953Smsmith break; 28869953Smsmith } 28969953Smsmith 29069953Smsmith /* 29169953Smsmith * Check the user's buffer to make sure it's readable. 29269953Smsmith */ 29369953Smsmith if (!useracc((caddr_t)cio->patterns, 29469953Smsmith cio->pat_buf_len, VM_PROT_READ)) { 29569953Smsmith printf("pci_ioctl: pattern buffer %p, " 29669953Smsmith "length %u isn't user accessible for" 29769953Smsmith " READ\n", cio->patterns, 29869953Smsmith cio->pat_buf_len); 29969953Smsmith error = EACCES; 30069953Smsmith break; 30169953Smsmith } 30269953Smsmith /* 30369953Smsmith * Allocate a buffer to hold the patterns. 30469953Smsmith */ 30569953Smsmith pattern_buf = malloc(cio->pat_buf_len, M_TEMP, 306111119Simp M_WAITOK); 30769953Smsmith error = copyin(cio->patterns, pattern_buf, 30869953Smsmith cio->pat_buf_len); 30969953Smsmith if (error != 0) 31069953Smsmith break; 31169953Smsmith num_patterns = cio->num_patterns; 31269953Smsmith 31369953Smsmith } else if ((cio->num_patterns > 0) 31469953Smsmith || (cio->pat_buf_len > 0)) { 31569953Smsmith /* 31669953Smsmith * The user made a mistake, spit out an error. 31769953Smsmith */ 31869953Smsmith cio->status = PCI_GETCONF_ERROR; 31969953Smsmith cio->num_matches = 0; 32069953Smsmith printf("pci_ioctl: invalid GETCONF arguments\n"); 32169953Smsmith error = EINVAL; 32269953Smsmith break; 32369953Smsmith } else 32469953Smsmith pattern_buf = NULL; 32569953Smsmith 32669953Smsmith /* 32769953Smsmith * Make sure we can write to the match buffer. 32869953Smsmith */ 32969953Smsmith if (!useracc((caddr_t)cio->matches, 33069953Smsmith cio->match_buf_len, VM_PROT_WRITE)) { 33169953Smsmith printf("pci_ioctl: match buffer %p, length %u " 33269953Smsmith "isn't user accessible for WRITE\n", 33369953Smsmith cio->matches, cio->match_buf_len); 33469953Smsmith error = EACCES; 33569953Smsmith break; 33669953Smsmith } 33769953Smsmith 33869953Smsmith /* 33969953Smsmith * Go through the list of devices and copy out the devices 34069953Smsmith * that match the user's criteria. 34169953Smsmith */ 34269953Smsmith for (cio->num_matches = 0, error = 0, i = 0, 34369953Smsmith dinfo = STAILQ_FIRST(devlist_head); 34469953Smsmith (dinfo != NULL) && (cio->num_matches < ionum) 34569953Smsmith && (error == 0) && (i < pci_numdevs); 34669953Smsmith dinfo = STAILQ_NEXT(dinfo, pci_links), i++) { 34769953Smsmith 34869953Smsmith if (i < cio->offset) 34969953Smsmith continue; 35069953Smsmith 35169953Smsmith /* Populate pd_name and pd_unit */ 35269953Smsmith name = NULL; 35369953Smsmith if (dinfo->cfg.dev && dinfo->conf.pd_name[0] == '\0') 35469953Smsmith name = device_get_name(dinfo->cfg.dev); 35569953Smsmith if (name) { 35669953Smsmith strncpy(dinfo->conf.pd_name, name, 35769953Smsmith sizeof(dinfo->conf.pd_name)); 35869953Smsmith dinfo->conf.pd_name[PCI_MAXNAMELEN] = 0; 35969953Smsmith dinfo->conf.pd_unit = 36069953Smsmith device_get_unit(dinfo->cfg.dev); 36169953Smsmith } 36269953Smsmith 36369953Smsmith if ((pattern_buf == NULL) || 36469953Smsmith (pci_conf_match(pattern_buf, num_patterns, 36569953Smsmith &dinfo->conf) == 0)) { 36669953Smsmith 36769953Smsmith /* 36869953Smsmith * If we've filled up the user's buffer, 36969953Smsmith * break out at this point. Since we've 37069953Smsmith * got a match here, we'll pick right back 37169953Smsmith * up at the matching entry. We can also 37269953Smsmith * tell the user that there are more matches 37369953Smsmith * left. 37469953Smsmith */ 37569953Smsmith if (cio->num_matches >= ionum) 37669953Smsmith break; 37769953Smsmith 37869953Smsmith error = copyout(&dinfo->conf, 37969953Smsmith &cio->matches[cio->num_matches], 38069953Smsmith sizeof(struct pci_conf)); 38169953Smsmith cio->num_matches++; 38269953Smsmith } 38369953Smsmith } 38469953Smsmith 38569953Smsmith /* 38669953Smsmith * Set the pointer into the list, so if the user is getting 38769953Smsmith * n records at a time, where n < pci_numdevs, 38869953Smsmith */ 38969953Smsmith cio->offset = i; 39069953Smsmith 39169953Smsmith /* 39269953Smsmith * Set the generation, the user will need this if they make 39369953Smsmith * another ioctl call with offset != 0. 39469953Smsmith */ 39569953Smsmith cio->generation = pci_generation; 39669953Smsmith 39769953Smsmith /* 39869953Smsmith * If this is the last device, inform the user so he won't 39969953Smsmith * bother asking for more devices. If dinfo isn't NULL, we 40069953Smsmith * know that there are more matches in the list because of 40169953Smsmith * the way the traversal is done. 40269953Smsmith */ 40369953Smsmith if (dinfo == NULL) 40469953Smsmith cio->status = PCI_GETCONF_LAST_DEVICE; 40569953Smsmith else 40669953Smsmith cio->status = PCI_GETCONF_MORE_DEVS; 40769953Smsmith 40869953Smsmith if (pattern_buf != NULL) 40969953Smsmith free(pattern_buf, M_TEMP); 41069953Smsmith 41169953Smsmith break; 41269953Smsmith } 41369953Smsmith case PCIOCREAD: 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 io->pi_data = 43069953Smsmith PCIB_READ_CONFIG(pcib, 43169953Smsmith b, 43269953Smsmith io->pi_sel.pc_dev, 43369953Smsmith io->pi_sel.pc_func, 43469953Smsmith io->pi_reg, 43569953Smsmith io->pi_width); 43669953Smsmith error = 0; 43769953Smsmith } else { 43869953Smsmith error = ENODEV; 43969953Smsmith } 44069953Smsmith break; 44169953Smsmith default: 44269953Smsmith error = ENODEV; 44369953Smsmith break; 44469953Smsmith } 44569953Smsmith break; 44669953Smsmith 44769953Smsmith case PCIOCWRITE: 44869953Smsmith io = (struct pci_io *)data; 44969953Smsmith switch(io->pi_width) { 45069953Smsmith case 4: 45169953Smsmith case 2: 45269953Smsmith case 1: 45369953Smsmith /* 45469953Smsmith * Assume that the user-level bus number is 45569953Smsmith * actually the pciN instance number. We map 45669953Smsmith * from that to the real pcib+bus combination. 45769953Smsmith */ 45869953Smsmith pci = devclass_get_device(devclass_find("pci"), 45969953Smsmith io->pi_sel.pc_bus); 46069953Smsmith if (pci) { 46169953Smsmith int b = pcib_get_bus(pci); 46269953Smsmith pcib = device_get_parent(pci); 46369953Smsmith PCIB_WRITE_CONFIG(pcib, 46469953Smsmith b, 46569953Smsmith io->pi_sel.pc_dev, 46669953Smsmith io->pi_sel.pc_func, 46769953Smsmith io->pi_reg, 46869953Smsmith io->pi_data, 46969953Smsmith io->pi_width); 47069953Smsmith error = 0; 47169953Smsmith } else { 47269953Smsmith error = ENODEV; 47369953Smsmith } 47469953Smsmith break; 47569953Smsmith default: 47669953Smsmith error = ENODEV; 47769953Smsmith break; 47869953Smsmith } 47969953Smsmith break; 48069953Smsmith 48169953Smsmith default: 48269953Smsmith error = ENOTTY; 48369953Smsmith break; 48469953Smsmith } 48569953Smsmith 48669953Smsmith return (error); 48769953Smsmith} 48869953Smsmith 489