xref: /freebsd/sys/dev/pci/pci_user.c (revision da1e0915c5216a69b6f87f3af09a6fdd94d9535e)
1aad970f1SDavid E. O'Brien /*-
28983cfbfSMike Smith  * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
38983cfbfSMike Smith  * All rights reserved.
48983cfbfSMike Smith  *
58983cfbfSMike Smith  * Redistribution and use in source and binary forms, with or without
68983cfbfSMike Smith  * modification, are permitted provided that the following conditions
78983cfbfSMike Smith  * are met:
88983cfbfSMike Smith  * 1. Redistributions of source code must retain the above copyright
98983cfbfSMike Smith  *    notice unmodified, this list of conditions, and the following
108983cfbfSMike Smith  *    disclaimer.
118983cfbfSMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
128983cfbfSMike Smith  *    notice, this list of conditions and the following disclaimer in the
138983cfbfSMike Smith  *    documentation and/or other materials provided with the distribution.
148983cfbfSMike Smith  *
158983cfbfSMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
168983cfbfSMike Smith  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
178983cfbfSMike Smith  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
188983cfbfSMike Smith  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
198983cfbfSMike Smith  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
208983cfbfSMike Smith  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
218983cfbfSMike Smith  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
228983cfbfSMike Smith  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
238983cfbfSMike Smith  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
248983cfbfSMike Smith  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
258983cfbfSMike Smith  */
268983cfbfSMike Smith 
27aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
28aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
29aad970f1SDavid E. O'Brien 
308983cfbfSMike Smith #include "opt_bus.h"	/* XXX trim includes */
318910aa92SPaul Saab #include "opt_compat.h"
328983cfbfSMike Smith 
338983cfbfSMike Smith #include <sys/param.h>
348983cfbfSMike Smith #include <sys/systm.h>
358983cfbfSMike Smith #include <sys/malloc.h>
368983cfbfSMike Smith #include <sys/module.h>
378983cfbfSMike Smith #include <sys/linker.h>
388983cfbfSMike Smith #include <sys/fcntl.h>
398983cfbfSMike Smith #include <sys/conf.h>
408983cfbfSMike Smith #include <sys/kernel.h>
418002488bSRobert Watson #include <sys/proc.h>
428983cfbfSMike Smith #include <sys/queue.h>
438983cfbfSMike Smith #include <sys/types.h>
448983cfbfSMike Smith 
458983cfbfSMike Smith #include <vm/vm.h>
468983cfbfSMike Smith #include <vm/pmap.h>
478983cfbfSMike Smith #include <vm/vm_extern.h>
488983cfbfSMike Smith 
498983cfbfSMike Smith #include <sys/bus.h>
508983cfbfSMike Smith #include <machine/bus.h>
518983cfbfSMike Smith #include <sys/rman.h>
528983cfbfSMike Smith #include <machine/resource.h>
538983cfbfSMike Smith 
548983cfbfSMike Smith #include <sys/pciio.h>
5538d8c994SWarner Losh #include <dev/pci/pcireg.h>
5638d8c994SWarner Losh #include <dev/pci/pcivar.h>
578983cfbfSMike Smith 
588983cfbfSMike Smith #include "pcib_if.h"
598983cfbfSMike Smith #include "pci_if.h"
608983cfbfSMike Smith 
618983cfbfSMike Smith /*
628983cfbfSMike Smith  * This is the user interface to PCI configuration space.
638983cfbfSMike Smith  */
648983cfbfSMike Smith 
65b40ce416SJulian Elischer static d_open_t 	pci_open;
66b40ce416SJulian Elischer static d_close_t	pci_close;
678983cfbfSMike Smith static int	pci_conf_match(struct pci_match_conf *matches, int num_matches,
688983cfbfSMike Smith 			       struct pci_conf *match_buf);
69b40ce416SJulian Elischer static d_ioctl_t	pci_ioctl;
708983cfbfSMike Smith 
718983cfbfSMike Smith struct cdevsw pcicdev = {
72dc08ffecSPoul-Henning Kamp 	.d_version =	D_VERSION,
73dc08ffecSPoul-Henning Kamp 	.d_flags =	D_NEEDGIANT,
747ac40f5fSPoul-Henning Kamp 	.d_open =	pci_open,
757ac40f5fSPoul-Henning Kamp 	.d_close =	pci_close,
767ac40f5fSPoul-Henning Kamp 	.d_ioctl =	pci_ioctl,
777ac40f5fSPoul-Henning Kamp 	.d_name =	"pci",
788983cfbfSMike Smith };
798983cfbfSMike Smith 
808983cfbfSMike Smith static int
8189c9c53dSPoul-Henning Kamp pci_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
828983cfbfSMike Smith {
838002488bSRobert Watson 	int error;
848002488bSRobert Watson 
858002488bSRobert Watson 	if (oflags & FWRITE) {
86a854ed98SJohn Baldwin 		error = securelevel_gt(td->td_ucred, 0);
878002488bSRobert Watson 		if (error)
888002488bSRobert Watson 			return (error);
898983cfbfSMike Smith 	}
908002488bSRobert Watson 
918002488bSRobert Watson 	return (0);
928983cfbfSMike Smith }
938983cfbfSMike Smith 
948983cfbfSMike Smith static int
9589c9c53dSPoul-Henning Kamp pci_close(struct cdev *dev, int flag, int devtype, struct thread *td)
968983cfbfSMike Smith {
978983cfbfSMike Smith 	return 0;
988983cfbfSMike Smith }
998983cfbfSMike Smith 
1008983cfbfSMike Smith /*
1018983cfbfSMike Smith  * Match a single pci_conf structure against an array of pci_match_conf
1028983cfbfSMike Smith  * structures.  The first argument, 'matches', is an array of num_matches
1038983cfbfSMike Smith  * pci_match_conf structures.  match_buf is a pointer to the pci_conf
1048983cfbfSMike Smith  * structure that will be compared to every entry in the matches array.
1058983cfbfSMike Smith  * This function returns 1 on failure, 0 on success.
1068983cfbfSMike Smith  */
1078983cfbfSMike Smith static int
1088983cfbfSMike Smith pci_conf_match(struct pci_match_conf *matches, int num_matches,
1098983cfbfSMike Smith 	       struct pci_conf *match_buf)
1108983cfbfSMike Smith {
1118983cfbfSMike Smith 	int i;
1128983cfbfSMike Smith 
1138983cfbfSMike Smith 	if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0))
1148983cfbfSMike Smith 		return(1);
1158983cfbfSMike Smith 
1168983cfbfSMike Smith 	for (i = 0; i < num_matches; i++) {
1178983cfbfSMike Smith 		/*
1188983cfbfSMike Smith 		 * I'm not sure why someone would do this...but...
1198983cfbfSMike Smith 		 */
1208983cfbfSMike Smith 		if (matches[i].flags == PCI_GETCONF_NO_MATCH)
1218983cfbfSMike Smith 			continue;
1228983cfbfSMike Smith 
1238983cfbfSMike Smith 		/*
1248983cfbfSMike Smith 		 * Look at each of the match flags.  If it's set, do the
1258983cfbfSMike Smith 		 * comparison.  If the comparison fails, we don't have a
1268983cfbfSMike Smith 		 * match, go on to the next item if there is one.
1278983cfbfSMike Smith 		 */
12855aaf894SMarius Strobl 		if (((matches[i].flags & PCI_GETCONF_MATCH_DOMAIN) != 0)
12955aaf894SMarius Strobl 		 && (match_buf->pc_sel.pc_domain !=
13055aaf894SMarius Strobl 		 matches[i].pc_sel.pc_domain))
13155aaf894SMarius Strobl 			continue;
13255aaf894SMarius Strobl 
1338983cfbfSMike Smith 		if (((matches[i].flags & PCI_GETCONF_MATCH_BUS) != 0)
1348983cfbfSMike Smith 		 && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus))
1358983cfbfSMike Smith 			continue;
1368983cfbfSMike Smith 
1378983cfbfSMike Smith 		if (((matches[i].flags & PCI_GETCONF_MATCH_DEV) != 0)
1388983cfbfSMike Smith 		 && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev))
1398983cfbfSMike Smith 			continue;
1408983cfbfSMike Smith 
1418983cfbfSMike Smith 		if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC) != 0)
1428983cfbfSMike Smith 		 && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func))
1438983cfbfSMike Smith 			continue;
1448983cfbfSMike Smith 
1458983cfbfSMike Smith 		if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR) != 0)
1468983cfbfSMike Smith 		 && (match_buf->pc_vendor != matches[i].pc_vendor))
1478983cfbfSMike Smith 			continue;
1488983cfbfSMike Smith 
1498983cfbfSMike Smith 		if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE) != 0)
1508983cfbfSMike Smith 		 && (match_buf->pc_device != matches[i].pc_device))
1518983cfbfSMike Smith 			continue;
1528983cfbfSMike Smith 
1538983cfbfSMike Smith 		if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS) != 0)
1548983cfbfSMike Smith 		 && (match_buf->pc_class != matches[i].pc_class))
1558983cfbfSMike Smith 			continue;
1568983cfbfSMike Smith 
1578983cfbfSMike Smith 		if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT) != 0)
1588983cfbfSMike Smith 		 && (match_buf->pd_unit != matches[i].pd_unit))
1598983cfbfSMike Smith 			continue;
1608983cfbfSMike Smith 
1618983cfbfSMike Smith 		if (((matches[i].flags & PCI_GETCONF_MATCH_NAME) != 0)
1628983cfbfSMike Smith 		 && (strncmp(matches[i].pd_name, match_buf->pd_name,
1638983cfbfSMike Smith 			     sizeof(match_buf->pd_name)) != 0))
1648983cfbfSMike Smith 			continue;
1658983cfbfSMike Smith 
1668983cfbfSMike Smith 		return(0);
1678983cfbfSMike Smith 	}
1688983cfbfSMike Smith 
1698983cfbfSMike Smith 	return(1);
1708983cfbfSMike Smith }
1718983cfbfSMike Smith 
17233d3fffaSMarius Strobl #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
17333d3fffaSMarius Strobl     defined(COMPAT_FREEBSD6)
174b2068c0cSWarner Losh #define PRE7_COMPAT
17533d3fffaSMarius Strobl 
17633d3fffaSMarius Strobl typedef enum {
17733d3fffaSMarius Strobl 	PCI_GETCONF_NO_MATCH_OLD	= 0x00,
17833d3fffaSMarius Strobl 	PCI_GETCONF_MATCH_BUS_OLD	= 0x01,
17933d3fffaSMarius Strobl 	PCI_GETCONF_MATCH_DEV_OLD	= 0x02,
18033d3fffaSMarius Strobl 	PCI_GETCONF_MATCH_FUNC_OLD	= 0x04,
18133d3fffaSMarius Strobl 	PCI_GETCONF_MATCH_NAME_OLD	= 0x08,
18233d3fffaSMarius Strobl 	PCI_GETCONF_MATCH_UNIT_OLD	= 0x10,
18333d3fffaSMarius Strobl 	PCI_GETCONF_MATCH_VENDOR_OLD	= 0x20,
18433d3fffaSMarius Strobl 	PCI_GETCONF_MATCH_DEVICE_OLD	= 0x40,
18533d3fffaSMarius Strobl 	PCI_GETCONF_MATCH_CLASS_OLD	= 0x80
18633d3fffaSMarius Strobl } pci_getconf_flags_old;
18733d3fffaSMarius Strobl 
18833d3fffaSMarius Strobl struct pcisel_old {
18933d3fffaSMarius Strobl 	u_int8_t	pc_bus;		/* bus number */
19033d3fffaSMarius Strobl 	u_int8_t	pc_dev;		/* device on this bus */
19133d3fffaSMarius Strobl 	u_int8_t	pc_func;	/* function on this device */
19233d3fffaSMarius Strobl };
19333d3fffaSMarius Strobl 
19433d3fffaSMarius Strobl struct pci_conf_old {
19533d3fffaSMarius Strobl 	struct pcisel_old pc_sel;	/* bus+slot+function */
19633d3fffaSMarius Strobl 	u_int8_t	pc_hdr;		/* PCI header type */
19733d3fffaSMarius Strobl 	u_int16_t	pc_subvendor;	/* card vendor ID */
19833d3fffaSMarius Strobl 	u_int16_t	pc_subdevice;	/* card device ID, assigned by
19933d3fffaSMarius Strobl 					   card vendor */
20033d3fffaSMarius Strobl 	u_int16_t	pc_vendor;	/* chip vendor ID */
20133d3fffaSMarius Strobl 	u_int16_t	pc_device;	/* chip device ID, assigned by
20233d3fffaSMarius Strobl 					   chip vendor */
20333d3fffaSMarius Strobl 	u_int8_t	pc_class;	/* chip PCI class */
20433d3fffaSMarius Strobl 	u_int8_t	pc_subclass;	/* chip PCI subclass */
20533d3fffaSMarius Strobl 	u_int8_t	pc_progif;	/* chip PCI programming interface */
20633d3fffaSMarius Strobl 	u_int8_t	pc_revid;	/* chip revision ID */
20733d3fffaSMarius Strobl 	char		pd_name[PCI_MAXNAMELEN + 1];  /* device name */
20833d3fffaSMarius Strobl 	u_long		pd_unit;	/* device unit number */
20933d3fffaSMarius Strobl };
21033d3fffaSMarius Strobl 
21133d3fffaSMarius Strobl struct pci_match_conf_old {
21233d3fffaSMarius Strobl 	struct pcisel_old	pc_sel;		/* bus+slot+function */
21333d3fffaSMarius Strobl 	char			pd_name[PCI_MAXNAMELEN + 1];  /* device name */
21433d3fffaSMarius Strobl 	u_long			pd_unit;	/* Unit number */
21533d3fffaSMarius Strobl 	u_int16_t		pc_vendor;	/* PCI Vendor ID */
21633d3fffaSMarius Strobl 	u_int16_t		pc_device;	/* PCI Device ID */
21733d3fffaSMarius Strobl 	u_int8_t		pc_class;	/* PCI class */
218c5860546SMarius Strobl 	pci_getconf_flags_old	flags;		/* Matching expression */
21933d3fffaSMarius Strobl };
22033d3fffaSMarius Strobl 
22133d3fffaSMarius Strobl struct pci_io_old {
22233d3fffaSMarius Strobl 	struct pcisel_old pi_sel;	/* device to operate on */
22333d3fffaSMarius Strobl 	int		pi_reg;		/* configuration register to examine */
22433d3fffaSMarius Strobl 	int		pi_width;	/* width (in bytes) of read or write */
22533d3fffaSMarius Strobl 	u_int32_t	pi_data;	/* data to write or result of read */
22633d3fffaSMarius Strobl };
22733d3fffaSMarius Strobl 
22833d3fffaSMarius Strobl #define	PCIOCGETCONF_OLD	_IOWR('p', 1, struct pci_conf_io)
22933d3fffaSMarius Strobl #define	PCIOCREAD_OLD		_IOWR('p', 2, struct pci_io_old)
23033d3fffaSMarius Strobl #define	PCIOCWRITE_OLD		_IOWR('p', 3, struct pci_io_old)
23133d3fffaSMarius Strobl 
23233d3fffaSMarius Strobl static int	pci_conf_match_old(struct pci_match_conf_old *matches,
23333d3fffaSMarius Strobl 		    int num_matches, struct pci_conf *match_buf);
23433d3fffaSMarius Strobl 
23533d3fffaSMarius Strobl static int
23633d3fffaSMarius Strobl pci_conf_match_old(struct pci_match_conf_old *matches, int num_matches,
23733d3fffaSMarius Strobl     struct pci_conf *match_buf)
23833d3fffaSMarius Strobl {
23933d3fffaSMarius Strobl 	int i;
24033d3fffaSMarius Strobl 
24133d3fffaSMarius Strobl 	if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0))
24233d3fffaSMarius Strobl 		return(1);
24333d3fffaSMarius Strobl 
24433d3fffaSMarius Strobl 	for (i = 0; i < num_matches; i++) {
24533d3fffaSMarius Strobl 		if (match_buf->pc_sel.pc_domain != 0)
24633d3fffaSMarius Strobl 			continue;
24733d3fffaSMarius Strobl 
24833d3fffaSMarius Strobl 		/*
24933d3fffaSMarius Strobl 		 * I'm not sure why someone would do this...but...
25033d3fffaSMarius Strobl 		 */
25133d3fffaSMarius Strobl 		if (matches[i].flags == PCI_GETCONF_NO_MATCH_OLD)
25233d3fffaSMarius Strobl 			continue;
25333d3fffaSMarius Strobl 
25433d3fffaSMarius Strobl 		/*
25533d3fffaSMarius Strobl 		 * Look at each of the match flags.  If it's set, do the
25633d3fffaSMarius Strobl 		 * comparison.  If the comparison fails, we don't have a
25733d3fffaSMarius Strobl 		 * match, go on to the next item if there is one.
25833d3fffaSMarius Strobl 		 */
25933d3fffaSMarius Strobl 		if (((matches[i].flags & PCI_GETCONF_MATCH_BUS_OLD) != 0)
26033d3fffaSMarius Strobl 		 && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus))
26133d3fffaSMarius Strobl 			continue;
26233d3fffaSMarius Strobl 
26333d3fffaSMarius Strobl 		if (((matches[i].flags & PCI_GETCONF_MATCH_DEV_OLD) != 0)
26433d3fffaSMarius Strobl 		 && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev))
26533d3fffaSMarius Strobl 			continue;
26633d3fffaSMarius Strobl 
26733d3fffaSMarius Strobl 		if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC_OLD) != 0)
26833d3fffaSMarius Strobl 		 && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func))
26933d3fffaSMarius Strobl 			continue;
27033d3fffaSMarius Strobl 
27133d3fffaSMarius Strobl 		if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR_OLD) != 0)
27233d3fffaSMarius Strobl 		 && (match_buf->pc_vendor != matches[i].pc_vendor))
27333d3fffaSMarius Strobl 			continue;
27433d3fffaSMarius Strobl 
27533d3fffaSMarius Strobl 		if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE_OLD) != 0)
27633d3fffaSMarius Strobl 		 && (match_buf->pc_device != matches[i].pc_device))
27733d3fffaSMarius Strobl 			continue;
27833d3fffaSMarius Strobl 
27933d3fffaSMarius Strobl 		if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS_OLD) != 0)
28033d3fffaSMarius Strobl 		 && (match_buf->pc_class != matches[i].pc_class))
28133d3fffaSMarius Strobl 			continue;
28233d3fffaSMarius Strobl 
28333d3fffaSMarius Strobl 		if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT_OLD) != 0)
28433d3fffaSMarius Strobl 		 && (match_buf->pd_unit != matches[i].pd_unit))
28533d3fffaSMarius Strobl 			continue;
28633d3fffaSMarius Strobl 
28733d3fffaSMarius Strobl 		if (((matches[i].flags & PCI_GETCONF_MATCH_NAME_OLD) != 0)
28833d3fffaSMarius Strobl 		 && (strncmp(matches[i].pd_name, match_buf->pd_name,
28933d3fffaSMarius Strobl 			     sizeof(match_buf->pd_name)) != 0))
29033d3fffaSMarius Strobl 			continue;
29133d3fffaSMarius Strobl 
29233d3fffaSMarius Strobl 		return(0);
29333d3fffaSMarius Strobl 	}
29433d3fffaSMarius Strobl 
29533d3fffaSMarius Strobl 	return(1);
29633d3fffaSMarius Strobl }
29733d3fffaSMarius Strobl 
29833d3fffaSMarius Strobl #endif
29933d3fffaSMarius Strobl 
3008983cfbfSMike Smith static int
30189c9c53dSPoul-Henning Kamp pci_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
3028983cfbfSMike Smith {
30333d3fffaSMarius Strobl 	device_t pcidev, brdev;
30433d3fffaSMarius Strobl 	void *confdata;
3058983cfbfSMike Smith 	const char *name;
30633d3fffaSMarius Strobl 	struct devlist *devlist_head;
30733d3fffaSMarius Strobl 	struct pci_conf_io *cio;
30833d3fffaSMarius Strobl 	struct pci_devinfo *dinfo;
30933d3fffaSMarius Strobl 	struct pci_io *io;
310da1e0915SJohn Baldwin 	struct pci_bar_io *bio;
31133d3fffaSMarius Strobl 	struct pci_match_conf *pattern_buf;
312da1e0915SJohn Baldwin 	struct resource_list_entry *rle;
313da1e0915SJohn Baldwin 	uint32_t value;
31433d3fffaSMarius Strobl 	size_t confsz, iolen, pbufsz;
31533d3fffaSMarius Strobl 	int error, ionum, i, num_patterns;
316b2068c0cSWarner Losh #ifdef PRE7_COMPAT
31733d3fffaSMarius Strobl 	struct pci_conf_old conf_old;
31833d3fffaSMarius Strobl 	struct pci_io iodata;
31933d3fffaSMarius Strobl 	struct pci_io_old *io_old;
32033d3fffaSMarius Strobl 	struct pci_match_conf_old *pattern_buf_old;
3218983cfbfSMike Smith 
32233d3fffaSMarius Strobl 	io_old = NULL;
32333d3fffaSMarius Strobl 	pattern_buf_old = NULL;
32433d3fffaSMarius Strobl 
325da1e0915SJohn Baldwin 	if (!(flag & FWRITE) && cmd != PCIOCGETBAR &&
326da1e0915SJohn Baldwin 	    cmd != PCIOCGETCONF && cmd != PCIOCGETCONF_OLD)
32733d3fffaSMarius Strobl 		return EPERM;
32833d3fffaSMarius Strobl #else
329da1e0915SJohn Baldwin 	if (!(flag & FWRITE) && cmd != PCIOCGETBAR && cmd != PCIOCGETCONF)
3308983cfbfSMike Smith 		return EPERM;
33133d3fffaSMarius Strobl #endif
3328983cfbfSMike Smith 
3338983cfbfSMike Smith 	switch(cmd) {
334b2068c0cSWarner Losh #ifdef PRE7_COMPAT
33533d3fffaSMarius Strobl 	case PCIOCGETCONF_OLD:
33633d3fffaSMarius Strobl 		/* FALLTHROUGH */
33733d3fffaSMarius Strobl #endif
3388983cfbfSMike Smith 	case PCIOCGETCONF:
3398983cfbfSMike Smith 		cio = (struct pci_conf_io *)data;
3408983cfbfSMike Smith 
34133d3fffaSMarius Strobl 		pattern_buf = NULL;
3428983cfbfSMike Smith 		num_patterns = 0;
3438983cfbfSMike Smith 		dinfo = NULL;
3448983cfbfSMike Smith 
34533d3fffaSMarius Strobl 		cio->num_matches = 0;
34633d3fffaSMarius Strobl 
3478983cfbfSMike Smith 		/*
3488983cfbfSMike Smith 		 * If the user specified an offset into the device list,
3498983cfbfSMike Smith 		 * but the list has changed since they last called this
3508983cfbfSMike Smith 		 * ioctl, tell them that the list has changed.  They will
3518983cfbfSMike Smith 		 * have to get the list from the beginning.
3528983cfbfSMike Smith 		 */
3538983cfbfSMike Smith 		if ((cio->offset != 0)
3548983cfbfSMike Smith 		 && (cio->generation != pci_generation)){
3558983cfbfSMike Smith 			cio->status = PCI_GETCONF_LIST_CHANGED;
3568983cfbfSMike Smith 			error = 0;
3578983cfbfSMike Smith 			break;
3588983cfbfSMike Smith 		}
3598983cfbfSMike Smith 
3608983cfbfSMike Smith 		/*
3618983cfbfSMike Smith 		 * Check to see whether the user has asked for an offset
3628983cfbfSMike Smith 		 * past the end of our list.
3638983cfbfSMike Smith 		 */
3648983cfbfSMike Smith 		if (cio->offset >= pci_numdevs) {
3658983cfbfSMike Smith 			cio->status = PCI_GETCONF_LAST_DEVICE;
3668983cfbfSMike Smith 			error = 0;
3678983cfbfSMike Smith 			break;
3688983cfbfSMike Smith 		}
3698983cfbfSMike Smith 
3708983cfbfSMike Smith 		/* get the head of the device queue */
3718983cfbfSMike Smith 		devlist_head = &pci_devq;
3728983cfbfSMike Smith 
3738983cfbfSMike Smith 		/*
3748983cfbfSMike Smith 		 * Determine how much room we have for pci_conf structures.
3758983cfbfSMike Smith 		 * Round the user's buffer size down to the nearest
3768983cfbfSMike Smith 		 * multiple of sizeof(struct pci_conf) in case the user
3778983cfbfSMike Smith 		 * didn't specify a multiple of that size.
3788983cfbfSMike Smith 		 */
379b2068c0cSWarner Losh #ifdef PRE7_COMPAT
38033d3fffaSMarius Strobl 		if (cmd == PCIOCGETCONF_OLD)
38133d3fffaSMarius Strobl 			confsz = sizeof(struct pci_conf_old);
38233d3fffaSMarius Strobl 		else
38333d3fffaSMarius Strobl #endif
38433d3fffaSMarius Strobl 			confsz = sizeof(struct pci_conf);
38533d3fffaSMarius Strobl 		iolen = min(cio->match_buf_len - (cio->match_buf_len % confsz),
38633d3fffaSMarius Strobl 		    pci_numdevs * confsz);
3878983cfbfSMike Smith 
3888983cfbfSMike Smith 		/*
3898983cfbfSMike Smith 		 * Since we know that iolen is a multiple of the size of
3908983cfbfSMike Smith 		 * the pciconf union, it's okay to do this.
3918983cfbfSMike Smith 		 */
39233d3fffaSMarius Strobl 		ionum = iolen / confsz;
3938983cfbfSMike Smith 
3948983cfbfSMike Smith 		/*
3958983cfbfSMike Smith 		 * If this test is true, the user wants the pci_conf
3968983cfbfSMike Smith 		 * structures returned to match the supplied entries.
3978983cfbfSMike Smith 		 */
398e3f932deSJohn-Mark Gurney 		if ((cio->num_patterns > 0) && (cio->num_patterns < pci_numdevs)
3998983cfbfSMike Smith 		 && (cio->pat_buf_len > 0)) {
4008983cfbfSMike Smith 			/*
4018983cfbfSMike Smith 			 * pat_buf_len needs to be:
4028983cfbfSMike Smith 			 * num_patterns * sizeof(struct pci_match_conf)
4038983cfbfSMike Smith 			 * While it is certainly possible the user just
4048983cfbfSMike Smith 			 * allocated a large buffer, but set the number of
4058983cfbfSMike Smith 			 * matches correctly, it is far more likely that
4068983cfbfSMike Smith 			 * their kernel doesn't match the userland utility
4078983cfbfSMike Smith 			 * they're using.  It's also possible that the user
4088983cfbfSMike Smith 			 * forgot to initialize some variables.  Yes, this
4098983cfbfSMike Smith 			 * may be overly picky, but I hazard to guess that
4108983cfbfSMike Smith 			 * it's far more likely to just catch folks that
4118983cfbfSMike Smith 			 * updated their kernel but not their userland.
4128983cfbfSMike Smith 			 */
413b2068c0cSWarner Losh #ifdef PRE7_COMPAT
41433d3fffaSMarius Strobl 			if (cmd == PCIOCGETCONF_OLD)
41533d3fffaSMarius Strobl 				pbufsz = sizeof(struct pci_match_conf_old);
41633d3fffaSMarius Strobl 			else
41733d3fffaSMarius Strobl #endif
41833d3fffaSMarius Strobl 				pbufsz = sizeof(struct pci_match_conf);
41933d3fffaSMarius Strobl 			if (cio->num_patterns * pbufsz != cio->pat_buf_len) {
42033d3fffaSMarius Strobl 				/* The user made a mistake, return an error. */
4218983cfbfSMike Smith 				cio->status = PCI_GETCONF_ERROR;
4228983cfbfSMike Smith 				error = EINVAL;
4238983cfbfSMike Smith 				break;
4248983cfbfSMike Smith 			}
4258983cfbfSMike Smith 
4268983cfbfSMike Smith 			/*
4278983cfbfSMike Smith 			 * Allocate a buffer to hold the patterns.
4288983cfbfSMike Smith 			 */
429b2068c0cSWarner Losh #ifdef PRE7_COMPAT
43033d3fffaSMarius Strobl 			if (cmd == PCIOCGETCONF_OLD) {
43133d3fffaSMarius Strobl 				pattern_buf_old = malloc(cio->pat_buf_len,
43233d3fffaSMarius Strobl 				    M_TEMP, M_WAITOK);
43333d3fffaSMarius Strobl 				error = copyin(cio->patterns,
43433d3fffaSMarius Strobl 				    pattern_buf_old, cio->pat_buf_len);
43533d3fffaSMarius Strobl 			} else
43633d3fffaSMarius Strobl #endif
43733d3fffaSMarius Strobl 			{
4388983cfbfSMike Smith 				pattern_buf = malloc(cio->pat_buf_len, M_TEMP,
439a163d034SWarner Losh 				    M_WAITOK);
4408983cfbfSMike Smith 				error = copyin(cio->patterns, pattern_buf,
4418983cfbfSMike Smith 				    cio->pat_buf_len);
44233d3fffaSMarius Strobl 			}
443d08239c1SJohn-Mark Gurney 			if (error != 0) {
444d08239c1SJohn-Mark Gurney 				error = EINVAL;
445d08239c1SJohn-Mark Gurney 				goto getconfexit;
446d08239c1SJohn-Mark Gurney 			}
4478983cfbfSMike Smith 			num_patterns = cio->num_patterns;
4488983cfbfSMike Smith 		} else if ((cio->num_patterns > 0)
4498983cfbfSMike Smith 			|| (cio->pat_buf_len > 0)) {
4508983cfbfSMike Smith 			/*
4518983cfbfSMike Smith 			 * The user made a mistake, spit out an error.
4528983cfbfSMike Smith 			 */
4538983cfbfSMike Smith 			cio->status = PCI_GETCONF_ERROR;
4548983cfbfSMike Smith 			error = EINVAL;
4558983cfbfSMike Smith 			break;
45633d3fffaSMarius Strobl 		}
4578983cfbfSMike Smith 
4588983cfbfSMike Smith 		/*
4598983cfbfSMike Smith 		 * Go through the list of devices and copy out the devices
4608983cfbfSMike Smith 		 * that match the user's criteria.
4618983cfbfSMike Smith 		 */
4628983cfbfSMike Smith 		for (cio->num_matches = 0, error = 0, i = 0,
4638983cfbfSMike Smith 		     dinfo = STAILQ_FIRST(devlist_head);
4648983cfbfSMike Smith 		     (dinfo != NULL) && (cio->num_matches < ionum)
465d08239c1SJohn-Mark Gurney 		     && (error == 0) && (i < pci_numdevs) && (dinfo != NULL);
4668983cfbfSMike Smith 		     dinfo = STAILQ_NEXT(dinfo, pci_links), i++) {
4678983cfbfSMike Smith 
4688983cfbfSMike Smith 			if (i < cio->offset)
4698983cfbfSMike Smith 				continue;
4708983cfbfSMike Smith 
4718983cfbfSMike Smith 			/* Populate pd_name and pd_unit */
4728983cfbfSMike Smith 			name = NULL;
4730678f786SJohn Baldwin 			if (dinfo->cfg.dev)
4748983cfbfSMike Smith 				name = device_get_name(dinfo->cfg.dev);
4758983cfbfSMike Smith 			if (name) {
4768983cfbfSMike Smith 				strncpy(dinfo->conf.pd_name, name,
4778983cfbfSMike Smith 					sizeof(dinfo->conf.pd_name));
4788983cfbfSMike Smith 				dinfo->conf.pd_name[PCI_MAXNAMELEN] = 0;
4798983cfbfSMike Smith 				dinfo->conf.pd_unit =
4808983cfbfSMike Smith 					device_get_unit(dinfo->cfg.dev);
4810678f786SJohn Baldwin 			} else {
4820678f786SJohn Baldwin 				dinfo->conf.pd_name[0] = '\0';
4830678f786SJohn Baldwin 				dinfo->conf.pd_unit = 0;
4848983cfbfSMike Smith 			}
4858983cfbfSMike Smith 
486b2068c0cSWarner Losh #ifdef PRE7_COMPAT
48733d3fffaSMarius Strobl 			if ((cmd == PCIOCGETCONF_OLD &&
48833d3fffaSMarius Strobl 			    (pattern_buf_old == NULL ||
48933d3fffaSMarius Strobl 			    pci_conf_match_old(pattern_buf_old, num_patterns,
49033d3fffaSMarius Strobl 			    &dinfo->conf) == 0)) ||
49133d3fffaSMarius Strobl 			    (cmd == PCIOCGETCONF &&
49233d3fffaSMarius Strobl 			    (pattern_buf == NULL ||
49333d3fffaSMarius Strobl 			    pci_conf_match(pattern_buf, num_patterns,
49433d3fffaSMarius Strobl 			    &dinfo->conf) == 0))) {
49533d3fffaSMarius Strobl #else
49633d3fffaSMarius Strobl 			if (pattern_buf == NULL ||
49733d3fffaSMarius Strobl 			    pci_conf_match(pattern_buf, num_patterns,
49833d3fffaSMarius Strobl 			    &dinfo->conf) == 0) {
49933d3fffaSMarius Strobl #endif
5008983cfbfSMike Smith 				/*
5018983cfbfSMike Smith 				 * If we've filled up the user's buffer,
5028983cfbfSMike Smith 				 * break out at this point.  Since we've
5038983cfbfSMike Smith 				 * got a match here, we'll pick right back
5048983cfbfSMike Smith 				 * up at the matching entry.  We can also
5058983cfbfSMike Smith 				 * tell the user that there are more matches
5068983cfbfSMike Smith 				 * left.
5078983cfbfSMike Smith 				 */
5088983cfbfSMike Smith 				if (cio->num_matches >= ionum)
5098983cfbfSMike Smith 					break;
5108983cfbfSMike Smith 
511b2068c0cSWarner Losh #ifdef PRE7_COMPAT
51233d3fffaSMarius Strobl 				if (cmd == PCIOCGETCONF_OLD) {
51333d3fffaSMarius Strobl 					conf_old.pc_sel.pc_bus =
51433d3fffaSMarius Strobl 					    dinfo->conf.pc_sel.pc_bus;
51533d3fffaSMarius Strobl 					conf_old.pc_sel.pc_dev =
51633d3fffaSMarius Strobl 					    dinfo->conf.pc_sel.pc_dev;
51733d3fffaSMarius Strobl 					conf_old.pc_sel.pc_func =
51833d3fffaSMarius Strobl 					    dinfo->conf.pc_sel.pc_func;
51933d3fffaSMarius Strobl 					conf_old.pc_hdr = dinfo->conf.pc_hdr;
52033d3fffaSMarius Strobl 					conf_old.pc_subvendor =
52133d3fffaSMarius Strobl 					    dinfo->conf.pc_subvendor;
52233d3fffaSMarius Strobl 					conf_old.pc_subdevice =
52333d3fffaSMarius Strobl 					    dinfo->conf.pc_subdevice;
52433d3fffaSMarius Strobl 					conf_old.pc_vendor =
52533d3fffaSMarius Strobl 					    dinfo->conf.pc_vendor;
52633d3fffaSMarius Strobl 					conf_old.pc_device =
52733d3fffaSMarius Strobl 					    dinfo->conf.pc_device;
52833d3fffaSMarius Strobl 					conf_old.pc_class =
52933d3fffaSMarius Strobl 					    dinfo->conf.pc_class;
53033d3fffaSMarius Strobl 					conf_old.pc_subclass =
53133d3fffaSMarius Strobl 					    dinfo->conf.pc_subclass;
53233d3fffaSMarius Strobl 					conf_old.pc_progif =
53333d3fffaSMarius Strobl 					    dinfo->conf.pc_progif;
53433d3fffaSMarius Strobl 					conf_old.pc_revid =
53533d3fffaSMarius Strobl 					    dinfo->conf.pc_revid;
536c5860546SMarius Strobl 					strncpy(conf_old.pd_name,
537c5860546SMarius Strobl 					    dinfo->conf.pd_name,
53833d3fffaSMarius Strobl 					    sizeof(conf_old.pd_name));
539c5860546SMarius Strobl 					conf_old.pd_name[PCI_MAXNAMELEN] = 0;
54033d3fffaSMarius Strobl 					conf_old.pd_unit =
54133d3fffaSMarius Strobl 					    dinfo->conf.pd_unit;
54233d3fffaSMarius Strobl 					confdata = &conf_old;
54333d3fffaSMarius Strobl 				} else
54433d3fffaSMarius Strobl #endif
54533d3fffaSMarius Strobl 					confdata = &dinfo->conf;
54633d3fffaSMarius Strobl 				/* Only if we can copy it out do we count it. */
54733d3fffaSMarius Strobl 				if (!(error = copyout(confdata,
548c5860546SMarius Strobl 				    (caddr_t)cio->matches +
549c5860546SMarius Strobl 				    confsz * cio->num_matches, confsz)))
55033d3fffaSMarius Strobl 					cio->num_matches++;
5518983cfbfSMike Smith 			}
552d08239c1SJohn-Mark Gurney 		}
5538983cfbfSMike Smith 
5548983cfbfSMike Smith 		/*
5558983cfbfSMike Smith 		 * Set the pointer into the list, so if the user is getting
5568983cfbfSMike Smith 		 * n records at a time, where n < pci_numdevs,
5578983cfbfSMike Smith 		 */
5588983cfbfSMike Smith 		cio->offset = i;
5598983cfbfSMike Smith 
5608983cfbfSMike Smith 		/*
5618983cfbfSMike Smith 		 * Set the generation, the user will need this if they make
5628983cfbfSMike Smith 		 * another ioctl call with offset != 0.
5638983cfbfSMike Smith 		 */
5648983cfbfSMike Smith 		cio->generation = pci_generation;
5658983cfbfSMike Smith 
5668983cfbfSMike Smith 		/*
5678983cfbfSMike Smith 		 * If this is the last device, inform the user so he won't
5688983cfbfSMike Smith 		 * bother asking for more devices.  If dinfo isn't NULL, we
5698983cfbfSMike Smith 		 * know that there are more matches in the list because of
5708983cfbfSMike Smith 		 * the way the traversal is done.
5718983cfbfSMike Smith 		 */
5728983cfbfSMike Smith 		if (dinfo == NULL)
5738983cfbfSMike Smith 			cio->status = PCI_GETCONF_LAST_DEVICE;
5748983cfbfSMike Smith 		else
5758983cfbfSMike Smith 			cio->status = PCI_GETCONF_MORE_DEVS;
5768983cfbfSMike Smith 
577d08239c1SJohn-Mark Gurney getconfexit:
5788983cfbfSMike Smith 		if (pattern_buf != NULL)
5798983cfbfSMike Smith 			free(pattern_buf, M_TEMP);
580b2068c0cSWarner Losh #ifdef PRE7_COMPAT
58133d3fffaSMarius Strobl 		if (pattern_buf_old != NULL)
58233d3fffaSMarius Strobl 			free(pattern_buf_old, M_TEMP);
58333d3fffaSMarius Strobl #endif
5848983cfbfSMike Smith 
5858983cfbfSMike Smith 		break;
5868983cfbfSMike Smith 
587b2068c0cSWarner Losh #ifdef PRE7_COMPAT
58833d3fffaSMarius Strobl 	case PCIOCREAD_OLD:
58933d3fffaSMarius Strobl 	case PCIOCWRITE_OLD:
59033d3fffaSMarius Strobl 		io_old = (struct pci_io_old *)data;
59133d3fffaSMarius Strobl 		iodata.pi_sel.pc_domain = 0;
59233d3fffaSMarius Strobl 		iodata.pi_sel.pc_bus = io_old->pi_sel.pc_bus;
59333d3fffaSMarius Strobl 		iodata.pi_sel.pc_dev = io_old->pi_sel.pc_dev;
59433d3fffaSMarius Strobl 		iodata.pi_sel.pc_func = io_old->pi_sel.pc_func;
59533d3fffaSMarius Strobl 		iodata.pi_reg = io_old->pi_reg;
59633d3fffaSMarius Strobl 		iodata.pi_width = io_old->pi_width;
59733d3fffaSMarius Strobl 		iodata.pi_data = io_old->pi_data;
59833d3fffaSMarius Strobl 		data = (caddr_t)&iodata;
59933d3fffaSMarius Strobl 		/* FALLTHROUGH */
60033d3fffaSMarius Strobl #endif
60166f314b5SStefan Eßer 	case PCIOCREAD:
6028983cfbfSMike Smith 	case PCIOCWRITE:
6038983cfbfSMike Smith 		io = (struct pci_io *)data;
6048983cfbfSMike Smith 		switch(io->pi_width) {
6058983cfbfSMike Smith 		case 4:
6068983cfbfSMike Smith 		case 2:
6078983cfbfSMike Smith 		case 1:
60840ed3f47SRuslan Ermilov 			/* Make sure register is in bounds and aligned. */
60933d3fffaSMarius Strobl 			if (io->pi_reg < 0 ||
61040ed3f47SRuslan Ermilov 			    io->pi_reg + io->pi_width > PCI_REGMAX + 1 ||
61133d3fffaSMarius Strobl 			    io->pi_reg & (io->pi_width - 1)) {
61266f314b5SStefan Eßer 				error = EINVAL;
61340ed3f47SRuslan Ermilov 				break;
61440ed3f47SRuslan Ermilov 			}
6158983cfbfSMike Smith 			/*
6168983cfbfSMike Smith 			 * Assume that the user-level bus number is
61737ce43b7SBruce M Simpson 			 * in fact the physical PCI bus number.
61837ce43b7SBruce M Simpson 			 * Look up the grandparent, i.e. the bridge device,
61937ce43b7SBruce M Simpson 			 * so that we can issue configuration space cycles.
6208983cfbfSMike Smith 			 */
62155aaf894SMarius Strobl 			pcidev = pci_find_dbsf(io->pi_sel.pc_domain,
62255aaf894SMarius Strobl 			    io->pi_sel.pc_bus, io->pi_sel.pc_dev,
62355aaf894SMarius Strobl 			    io->pi_sel.pc_func);
62437ce43b7SBruce M Simpson 			if (pcidev) {
62533d3fffaSMarius Strobl 				brdev = device_get_parent(
62633d3fffaSMarius Strobl 				    device_get_parent(pcidev));
62737ce43b7SBruce M Simpson 
628b2068c0cSWarner Losh #ifdef PRE7_COMPAT
62933d3fffaSMarius Strobl 				if (cmd == PCIOCWRITE || cmd == PCIOCWRITE_OLD)
63033d3fffaSMarius Strobl #else
63166f314b5SStefan Eßer 				if (cmd == PCIOCWRITE)
63233d3fffaSMarius Strobl #endif
63337ce43b7SBruce M Simpson 					PCIB_WRITE_CONFIG(brdev,
63437ce43b7SBruce M Simpson 							  io->pi_sel.pc_bus,
6358983cfbfSMike Smith 							  io->pi_sel.pc_dev,
6368983cfbfSMike Smith 							  io->pi_sel.pc_func,
6378983cfbfSMike Smith 							  io->pi_reg,
6388983cfbfSMike Smith 							  io->pi_data,
6398983cfbfSMike Smith 							  io->pi_width);
640b2068c0cSWarner Losh #ifdef PRE7_COMPAT
64133d3fffaSMarius Strobl 				else if (cmd == PCIOCREAD_OLD)
64233d3fffaSMarius Strobl 					io_old->pi_data =
64333d3fffaSMarius Strobl 						PCIB_READ_CONFIG(brdev,
64433d3fffaSMarius Strobl 							  io->pi_sel.pc_bus,
64533d3fffaSMarius Strobl 							  io->pi_sel.pc_dev,
64633d3fffaSMarius Strobl 							  io->pi_sel.pc_func,
64733d3fffaSMarius Strobl 							  io->pi_reg,
64833d3fffaSMarius Strobl 							  io->pi_width);
64933d3fffaSMarius Strobl #endif
65066f314b5SStefan Eßer 				else
65166f314b5SStefan Eßer 					io->pi_data =
65237ce43b7SBruce M Simpson 						PCIB_READ_CONFIG(brdev,
65337ce43b7SBruce M Simpson 							  io->pi_sel.pc_bus,
65466f314b5SStefan Eßer 							  io->pi_sel.pc_dev,
65566f314b5SStefan Eßer 							  io->pi_sel.pc_func,
65666f314b5SStefan Eßer 							  io->pi_reg,
65766f314b5SStefan Eßer 							  io->pi_width);
6588983cfbfSMike Smith 				error = 0;
6598983cfbfSMike Smith 			} else {
6608910aa92SPaul Saab #ifdef COMPAT_FREEBSD4
66133d3fffaSMarius Strobl 				if (cmd == PCIOCREAD_OLD) {
66233d3fffaSMarius Strobl 					io_old->pi_data = -1;
6638910aa92SPaul Saab 					error = 0;
6648910aa92SPaul Saab 				} else
6658910aa92SPaul Saab #endif
6668983cfbfSMike Smith 					error = ENODEV;
6678983cfbfSMike Smith 			}
6688983cfbfSMike Smith 			break;
6698983cfbfSMike Smith 		default:
670d08239c1SJohn-Mark Gurney 			error = EINVAL;
6718983cfbfSMike Smith 			break;
6728983cfbfSMike Smith 		}
6738983cfbfSMike Smith 		break;
6748983cfbfSMike Smith 
675da1e0915SJohn Baldwin 	case PCIOCGETBAR:
676da1e0915SJohn Baldwin 		bio = (struct pci_bar_io *)data;
677da1e0915SJohn Baldwin 
678da1e0915SJohn Baldwin 		/*
679da1e0915SJohn Baldwin 		 * Assume that the user-level bus number is
680da1e0915SJohn Baldwin 		 * in fact the physical PCI bus number.
681da1e0915SJohn Baldwin 		 */
682da1e0915SJohn Baldwin 		pcidev = pci_find_dbsf(bio->pbi_sel.pc_domain,
683da1e0915SJohn Baldwin 		    bio->pbi_sel.pc_bus, bio->pbi_sel.pc_dev,
684da1e0915SJohn Baldwin 		    bio->pbi_sel.pc_func);
685da1e0915SJohn Baldwin 		if (pcidev == NULL) {
686da1e0915SJohn Baldwin 			error = ENODEV;
687da1e0915SJohn Baldwin 			break;
688da1e0915SJohn Baldwin 		}
689da1e0915SJohn Baldwin 		dinfo = device_get_ivars(pcidev);
690da1e0915SJohn Baldwin 
691da1e0915SJohn Baldwin 		/*
692da1e0915SJohn Baldwin 		 * Look for a resource list entry matching the requested BAR.
693da1e0915SJohn Baldwin 		 *
694da1e0915SJohn Baldwin 		 * XXX: This will not find BARs that are not initialized, but
695da1e0915SJohn Baldwin 		 * maybe that is ok?
696da1e0915SJohn Baldwin 		 */
697da1e0915SJohn Baldwin 		rle = resource_list_find(&dinfo->resources, SYS_RES_MEMORY,
698da1e0915SJohn Baldwin 		    bio->pbi_reg);
699da1e0915SJohn Baldwin 		if (rle == NULL)
700da1e0915SJohn Baldwin 			rle = resource_list_find(&dinfo->resources,
701da1e0915SJohn Baldwin 			    SYS_RES_IOPORT, bio->pbi_reg);
702da1e0915SJohn Baldwin 		if (rle == NULL || rle->res == NULL) {
703da1e0915SJohn Baldwin 			error = EINVAL;
704da1e0915SJohn Baldwin 			break;
705da1e0915SJohn Baldwin 		}
706da1e0915SJohn Baldwin 
707da1e0915SJohn Baldwin 		/*
708da1e0915SJohn Baldwin 		 * Ok, we have a resource for this BAR.  Read the lower
709da1e0915SJohn Baldwin 		 * 32 bits to get any flags.
710da1e0915SJohn Baldwin 		 */
711da1e0915SJohn Baldwin 		value = pci_read_config(pcidev, bio->pbi_reg, 4);
712da1e0915SJohn Baldwin 		if (PCI_BAR_MEM(value)) {
713da1e0915SJohn Baldwin 			if (rle->type != SYS_RES_MEMORY) {
714da1e0915SJohn Baldwin 				error = EINVAL;
715da1e0915SJohn Baldwin 				break;
716da1e0915SJohn Baldwin 			}
717da1e0915SJohn Baldwin 			value &= ~PCIM_BAR_MEM_BASE;
718da1e0915SJohn Baldwin 		} else {
719da1e0915SJohn Baldwin 			if (rle->type != SYS_RES_IOPORT) {
720da1e0915SJohn Baldwin 				error = EINVAL;
721da1e0915SJohn Baldwin 				break;
722da1e0915SJohn Baldwin 			}
723da1e0915SJohn Baldwin 			value &= ~PCIM_BAR_IO_BASE;
724da1e0915SJohn Baldwin 		}
725da1e0915SJohn Baldwin 		bio->pbi_base = rman_get_start(rle->res) | value;
726da1e0915SJohn Baldwin 		bio->pbi_length = rman_get_size(rle->res);
727da1e0915SJohn Baldwin 
728da1e0915SJohn Baldwin 		/*
729da1e0915SJohn Baldwin 		 * Check the command register to determine if this BAR
730da1e0915SJohn Baldwin 		 * is enabled.
731da1e0915SJohn Baldwin 		 */
732da1e0915SJohn Baldwin 		value = pci_read_config(pcidev, PCIR_COMMAND, 2);
733da1e0915SJohn Baldwin 		if (rle->type == SYS_RES_MEMORY)
734da1e0915SJohn Baldwin 			bio->pbi_enabled = (value & PCIM_CMD_MEMEN) != 0;
735da1e0915SJohn Baldwin 		else
736da1e0915SJohn Baldwin 			bio->pbi_enabled = (value & PCIM_CMD_PORTEN) != 0;
737da1e0915SJohn Baldwin 		error = 0;
738da1e0915SJohn Baldwin 		break;
7398983cfbfSMike Smith 	default:
7408983cfbfSMike Smith 		error = ENOTTY;
7418983cfbfSMike Smith 		break;
7428983cfbfSMike Smith 	}
7438983cfbfSMike Smith 
7448983cfbfSMike Smith 	return (error);
7458983cfbfSMike Smith }
746