xref: /dragonfly/sys/bus/pci/pci_user.c (revision 335b9e93)
1 /*-
2  * Copyright (c) 1997, Stefan Esser <se@kfreebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/pci/pci_user.c,v 1.22.2.4.2.1 2009/04/15 03:14:26 kensmith Exp $
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/malloc.h>
32 #include <sys/module.h>
33 #include <sys/linker.h>
34 #include <sys/fcntl.h>
35 #include <sys/conf.h>
36 #include <sys/kernel.h>
37 #include <sys/proc.h>
38 #include <sys/queue.h>
39 #include <sys/types.h>
40 
41 #include <vm/vm.h>
42 #include <vm/pmap.h>
43 #include <vm/vm_extern.h>
44 
45 #include <sys/bus.h>
46 #include <sys/rman.h>
47 #include <sys/device.h>
48 #include <sys/pciio.h>
49 #include <bus/pci/pcireg.h>
50 #include <bus/pci/pcivar.h>
51 
52 #include "pcib_if.h"
53 #include "pci_if.h"
54 
55 /*
56  * This is the user interface to PCI configuration space.
57  */
58 static struct lwkt_token pci_token = LWKT_TOKEN_INITIALIZER(pci_token);
59 
60 static d_open_t 	pci_open;
61 static d_close_t	pci_close;
62 static int	pci_conf_match(struct pci_match_conf *matches, int num_matches,
63 			       struct pci_conf *match_buf);
64 static d_ioctl_t	pci_ioctl;
65 
66 struct dev_ops pci_ops = {
67 	{ "pci", 0, D_MPSAFE },
68 	.d_open =       pci_open,
69 	.d_close =      pci_close,
70 	.d_ioctl =      pci_ioctl,
71 };
72 
73 static int
74 pci_open(struct dev_open_args *ap)
75 {
76 	int oflags = ap->a_oflags;
77 
78 	if (oflags & FWRITE) {
79 		if (securelevel > 0)
80 			return (EPERM);
81 	}
82 
83 	return (0);
84 }
85 
86 static int
87 pci_close(struct dev_close_args *ap)
88 {
89 	return 0;
90 }
91 
92 /*
93  * Match a single pci_conf structure against an array of pci_match_conf
94  * structures.  The first argument, 'matches', is an array of num_matches
95  * pci_match_conf structures.  match_buf is a pointer to the pci_conf
96  * structure that will be compared to every entry in the matches array.
97  * This function returns 1 on failure, 0 on success.
98  */
99 static int
100 pci_conf_match(struct pci_match_conf *matches, int num_matches,
101 	       struct pci_conf *match_buf)
102 {
103 	int i;
104 
105 	if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0))
106 		return(1);
107 
108 	for (i = 0; i < num_matches; i++) {
109 		/*
110 		 * I'm not sure why someone would do this...but...
111 		 */
112 		if (matches[i].flags == PCI_GETCONF_NO_MATCH)
113 			continue;
114 
115 		/*
116 		 * Look at each of the match flags.  If it's set, do the
117 		 * comparison.  If the comparison fails, we don't have a
118 		 * match, go on to the next item if there is one.
119 		 */
120 		if (((matches[i].flags & PCI_GETCONF_MATCH_DOMAIN) != 0)
121 		 && (match_buf->pc_sel.pc_domain !=
122 		 matches[i].pc_sel.pc_domain))
123 			continue;
124 
125 		if (((matches[i].flags & PCI_GETCONF_MATCH_BUS) != 0)
126 		 && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus))
127 			continue;
128 
129 		if (((matches[i].flags & PCI_GETCONF_MATCH_DEV) != 0)
130 		 && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev))
131 			continue;
132 
133 		if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC) != 0)
134 		 && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func))
135 			continue;
136 
137 		if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR) != 0)
138 		 && (match_buf->pc_vendor != matches[i].pc_vendor))
139 			continue;
140 
141 		if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE) != 0)
142 		 && (match_buf->pc_device != matches[i].pc_device))
143 			continue;
144 
145 		if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS) != 0)
146 		 && (match_buf->pc_class != matches[i].pc_class))
147 			continue;
148 
149 		if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT) != 0)
150 		 && (match_buf->pd_unit != matches[i].pd_unit))
151 			continue;
152 
153 		if (((matches[i].flags & PCI_GETCONF_MATCH_NAME) != 0)
154 		 && (strncmp(matches[i].pd_name, match_buf->pd_name,
155 			     sizeof(match_buf->pd_name)) != 0))
156 			continue;
157 
158 		return(0);
159 	}
160 
161 	return(1);
162 }
163 
164 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
165     defined(COMPAT_FREEBSD6) || defined(__DragonFly__)
166 #define PRE7_COMPAT
167 
168 typedef enum {
169 	PCI_GETCONF_NO_MATCH_OLD	= 0x00,
170 	PCI_GETCONF_MATCH_BUS_OLD	= 0x01,
171 	PCI_GETCONF_MATCH_DEV_OLD	= 0x02,
172 	PCI_GETCONF_MATCH_FUNC_OLD	= 0x04,
173 	PCI_GETCONF_MATCH_NAME_OLD	= 0x08,
174 	PCI_GETCONF_MATCH_UNIT_OLD	= 0x10,
175 	PCI_GETCONF_MATCH_VENDOR_OLD	= 0x20,
176 	PCI_GETCONF_MATCH_DEVICE_OLD	= 0x40,
177 	PCI_GETCONF_MATCH_CLASS_OLD	= 0x80
178 } pci_getconf_flags_old;
179 
180 struct pcisel_old {
181 	u_int8_t	pc_bus;		/* bus number */
182 	u_int8_t	pc_dev;		/* device on this bus */
183 	u_int8_t	pc_func;	/* function on this device */
184 };
185 
186 struct pci_conf_old {
187 	struct pcisel_old pc_sel;	/* bus+slot+function */
188 	u_int8_t	pc_hdr;		/* PCI header type */
189 	u_int16_t	pc_subvendor;	/* card vendor ID */
190 	u_int16_t	pc_subdevice;	/* card device ID, assigned by
191 					   card vendor */
192 	u_int16_t	pc_vendor;	/* chip vendor ID */
193 	u_int16_t	pc_device;	/* chip device ID, assigned by
194 					   chip vendor */
195 	u_int8_t	pc_class;	/* chip PCI class */
196 	u_int8_t	pc_subclass;	/* chip PCI subclass */
197 	u_int8_t	pc_progif;	/* chip PCI programming interface */
198 	u_int8_t	pc_revid;	/* chip revision ID */
199 	char		pd_name[PCI_MAXNAMELEN + 1];  /* device name */
200 	u_long		pd_unit;	/* device unit number */
201 };
202 
203 struct pci_match_conf_old {
204 	struct pcisel_old	pc_sel;		/* bus+slot+function */
205 	char			pd_name[PCI_MAXNAMELEN + 1];  /* device name */
206 	u_long			pd_unit;	/* Unit number */
207 	u_int16_t		pc_vendor;	/* PCI Vendor ID */
208 	u_int16_t		pc_device;	/* PCI Device ID */
209 	u_int8_t		pc_class;	/* PCI class */
210 	pci_getconf_flags_old	flags;		/* Matching expression */
211 };
212 
213 struct pci_io_old {
214 	struct pcisel_old pi_sel;	/* device to operate on */
215 	int		pi_reg;		/* configuration register to examine */
216 	int		pi_width;	/* width (in bytes) of read or write */
217 	u_int32_t	pi_data;	/* data to write or result of read */
218 };
219 
220 #define	PCIOCGETCONF_OLD	_IOWR('p', 1, struct pci_conf_io)
221 #define	PCIOCREAD_OLD		_IOWR('p', 2, struct pci_io_old)
222 #define	PCIOCWRITE_OLD		_IOWR('p', 3, struct pci_io_old)
223 
224 static int	pci_conf_match_old(struct pci_match_conf_old *matches,
225 		    int num_matches, struct pci_conf *match_buf);
226 
227 static int
228 pci_conf_match_old(struct pci_match_conf_old *matches, int num_matches,
229     struct pci_conf *match_buf)
230 {
231 	int i;
232 
233 	if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0))
234 		return(1);
235 
236 	for (i = 0; i < num_matches; i++) {
237 		if (match_buf->pc_sel.pc_domain != 0)
238 			continue;
239 
240 		/*
241 		 * I'm not sure why someone would do this...but...
242 		 */
243 		if (matches[i].flags == PCI_GETCONF_NO_MATCH_OLD)
244 			continue;
245 
246 		/*
247 		 * Look at each of the match flags.  If it's set, do the
248 		 * comparison.  If the comparison fails, we don't have a
249 		 * match, go on to the next item if there is one.
250 		 */
251 		if (((matches[i].flags & PCI_GETCONF_MATCH_BUS_OLD) != 0)
252 		 && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus))
253 			continue;
254 
255 		if (((matches[i].flags & PCI_GETCONF_MATCH_DEV_OLD) != 0)
256 		 && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev))
257 			continue;
258 
259 		if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC_OLD) != 0)
260 		 && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func))
261 			continue;
262 
263 		if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR_OLD) != 0)
264 		 && (match_buf->pc_vendor != matches[i].pc_vendor))
265 			continue;
266 
267 		if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE_OLD) != 0)
268 		 && (match_buf->pc_device != matches[i].pc_device))
269 			continue;
270 
271 		if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS_OLD) != 0)
272 		 && (match_buf->pc_class != matches[i].pc_class))
273 			continue;
274 
275 		if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT_OLD) != 0)
276 		 && (match_buf->pd_unit != matches[i].pd_unit))
277 			continue;
278 
279 		if (((matches[i].flags & PCI_GETCONF_MATCH_NAME_OLD) != 0)
280 		 && (strncmp(matches[i].pd_name, match_buf->pd_name,
281 			     sizeof(match_buf->pd_name)) != 0))
282 			continue;
283 
284 		return(0);
285 	}
286 
287 	return(1);
288 }
289 
290 #endif
291 
292 static int
293 pci_ioctl(struct dev_ioctl_args *ap)
294 {
295 	device_t pcidev, brdev;
296 	void *confdata;
297 	const char *name;
298 	struct devlist *devlist_head;
299 	struct pci_conf_io *cio;
300 	struct pci_devinfo *dinfo;
301 	struct pci_io *io;
302 	struct pci_bar_io *bio;
303 	struct pci_match_conf *pattern_buf;
304 	struct resource_list_entry *rle;
305 	uint32_t value;
306 	size_t confsz, iolen, pbufsz;
307 	int error, ionum, i, num_patterns;
308 #ifdef PRE7_COMPAT
309 	struct pci_conf_old conf_old;
310 	struct pci_io iodata;
311 	struct pci_io_old *io_old;
312 	struct pci_match_conf_old *pattern_buf_old;
313 
314 	io_old = NULL;
315 	pattern_buf_old = NULL;
316 
317 	if (!(ap->a_fflag & FWRITE) && ap->a_cmd != PCIOCGETBAR &&
318 	    ap->a_cmd != PCIOCGETCONF && ap->a_cmd != PCIOCGETCONF_OLD)
319 		return EPERM;
320 #else
321 	if (!(ap->a_fflag & FWRITE) && ap->a_cmd != PCIOCGETBAR && ap->a_cmd != PCIOCGETCONF)
322 		return EPERM;
323 #endif
324 
325 	lwkt_gettoken(&pci_token);
326 
327 	switch(ap->a_cmd) {
328 #ifdef PRE7_COMPAT
329 	case PCIOCGETCONF_OLD:
330 		/* FALLTHROUGH */
331 #endif
332 	case PCIOCGETCONF:
333 		cio = (struct pci_conf_io *)ap->a_data;
334 
335 		pattern_buf = NULL;
336 		num_patterns = 0;
337 		dinfo = NULL;
338 
339 		cio->num_matches = 0;
340 
341 		/*
342 		 * If the user specified an offset into the device list,
343 		 * but the list has changed since they last called this
344 		 * ioctl, tell them that the list has changed.  They will
345 		 * have to get the list from the beginning.
346 		 */
347 		if ((cio->offset != 0)
348 		 && (cio->generation != pci_generation)){
349 			cio->status = PCI_GETCONF_LIST_CHANGED;
350 			error = 0;
351 			break;
352 		}
353 
354 		/*
355 		 * Check to see whether the user has asked for an offset
356 		 * past the end of our list.
357 		 */
358 		if (cio->offset >= pci_numdevs) {
359 			cio->status = PCI_GETCONF_LAST_DEVICE;
360 			error = 0;
361 			break;
362 		}
363 
364 		/* get the head of the device queue */
365 		devlist_head = &pci_devq;
366 
367 		/*
368 		 * Determine how much room we have for pci_conf structures.
369 		 * Round the user's buffer size down to the nearest
370 		 * multiple of sizeof(struct pci_conf) in case the user
371 		 * didn't specify a multiple of that size.
372 		 */
373 #ifdef PRE7_COMPAT
374 		if (ap->a_cmd == PCIOCGETCONF_OLD)
375 			confsz = sizeof(struct pci_conf_old);
376 		else
377 #endif
378 			confsz = sizeof(struct pci_conf);
379 		iolen = min(cio->match_buf_len - (cio->match_buf_len % confsz),
380 		    pci_numdevs * confsz);
381 
382 		/*
383 		 * Since we know that iolen is a multiple of the size of
384 		 * the pciconf union, it's okay to do this.
385 		 */
386 		ionum = iolen / confsz;
387 
388 		/*
389 		 * If this test is true, the user wants the pci_conf
390 		 * structures returned to match the supplied entries.
391 		 */
392 		if ((cio->num_patterns > 0) && (cio->num_patterns < pci_numdevs)
393 		 && (cio->pat_buf_len > 0)) {
394 			/*
395 			 * pat_buf_len needs to be:
396 			 * num_patterns * sizeof(struct pci_match_conf)
397 			 * While it is certainly possible the user just
398 			 * allocated a large buffer, but set the number of
399 			 * matches correctly, it is far more likely that
400 			 * their kernel doesn't match the userland utility
401 			 * they're using.  It's also possible that the user
402 			 * forgot to initialize some variables.  Yes, this
403 			 * may be overly picky, but I hazard to guess that
404 			 * it's far more likely to just catch folks that
405 			 * updated their kernel but not their userland.
406 			 */
407 #ifdef PRE7_COMPAT
408 			if (ap->a_cmd == PCIOCGETCONF_OLD)
409 				pbufsz = sizeof(struct pci_match_conf_old);
410 			else
411 #endif
412 				pbufsz = sizeof(struct pci_match_conf);
413 			if (cio->num_patterns * pbufsz != cio->pat_buf_len) {
414 				/* The user made a mistake, return an error. */
415 				cio->status = PCI_GETCONF_ERROR;
416 				error = EINVAL;
417 				break;
418 			}
419 
420 			/*
421 			 * Allocate a buffer to hold the patterns.
422 			 */
423 #ifdef PRE7_COMPAT
424 			if (ap->a_cmd == PCIOCGETCONF_OLD) {
425 				pattern_buf_old = kmalloc(cio->pat_buf_len,
426 				    M_TEMP, M_WAITOK);
427 				error = copyin(cio->patterns,
428 				    pattern_buf_old, cio->pat_buf_len);
429 			} else
430 #endif
431 			{
432 				pattern_buf = kmalloc(cio->pat_buf_len, M_TEMP,
433 				    M_WAITOK);
434 				error = copyin(cio->patterns, pattern_buf,
435 				    cio->pat_buf_len);
436 			}
437 			if (error != 0) {
438 				error = EINVAL;
439 				goto getconfexit;
440 			}
441 			num_patterns = cio->num_patterns;
442 		} else if ((cio->num_patterns > 0)
443 			|| (cio->pat_buf_len > 0)) {
444 			/*
445 			 * The user made a mistake, spit out an error.
446 			 */
447 			cio->status = PCI_GETCONF_ERROR;
448 			error = EINVAL;
449 			break;
450 		}
451 
452 		/*
453 		 * Go through the list of devices and copy out the devices
454 		 * that match the user's criteria.
455 		 */
456 		for (cio->num_matches = 0, error = 0, i = 0,
457 		     dinfo = STAILQ_FIRST(devlist_head);
458 		     (dinfo != NULL) && (cio->num_matches < ionum)
459 		     && (error == 0) && (i < pci_numdevs) && (dinfo != NULL);
460 		     dinfo = STAILQ_NEXT(dinfo, pci_links), i++) {
461 
462 			if (i < cio->offset)
463 				continue;
464 
465 			/* Populate pd_name and pd_unit */
466 			name = NULL;
467 			if (dinfo->cfg.dev)
468 				name = device_get_name(dinfo->cfg.dev);
469 			if (name) {
470 				strncpy(dinfo->conf.pd_name, name,
471 					sizeof(dinfo->conf.pd_name));
472 				dinfo->conf.pd_name[PCI_MAXNAMELEN] = 0;
473 				dinfo->conf.pd_unit =
474 					device_get_unit(dinfo->cfg.dev);
475 			} else {
476 				dinfo->conf.pd_name[0] = '\0';
477 				dinfo->conf.pd_unit = 0;
478 			}
479 
480 #ifdef PRE7_COMPAT
481 			if ((ap->a_cmd == PCIOCGETCONF_OLD &&
482 			    (pattern_buf_old == NULL ||
483 			    pci_conf_match_old(pattern_buf_old, num_patterns,
484 			    &dinfo->conf) == 0)) ||
485 			    (ap->a_cmd == PCIOCGETCONF &&
486 			    (pattern_buf == NULL ||
487 			    pci_conf_match(pattern_buf, num_patterns,
488 			    &dinfo->conf) == 0))) {
489 #else
490 			if (pattern_buf == NULL ||
491 			    pci_conf_match(pattern_buf, num_patterns,
492 			    &dinfo->conf) == 0) {
493 #endif
494 				/*
495 				 * If we've filled up the user's buffer,
496 				 * break out at this point.  Since we've
497 				 * got a match here, we'll pick right back
498 				 * up at the matching entry.  We can also
499 				 * tell the user that there are more matches
500 				 * left.
501 				 */
502 				if (cio->num_matches >= ionum)
503 					break;
504 
505 #ifdef PRE7_COMPAT
506 				if (ap->a_cmd == PCIOCGETCONF_OLD) {
507 					conf_old.pc_sel.pc_bus =
508 					    dinfo->conf.pc_sel.pc_bus;
509 					conf_old.pc_sel.pc_dev =
510 					    dinfo->conf.pc_sel.pc_dev;
511 					conf_old.pc_sel.pc_func =
512 					    dinfo->conf.pc_sel.pc_func;
513 					conf_old.pc_hdr = dinfo->conf.pc_hdr;
514 					conf_old.pc_subvendor =
515 					    dinfo->conf.pc_subvendor;
516 					conf_old.pc_subdevice =
517 					    dinfo->conf.pc_subdevice;
518 					conf_old.pc_vendor =
519 					    dinfo->conf.pc_vendor;
520 					conf_old.pc_device =
521 					    dinfo->conf.pc_device;
522 					conf_old.pc_class =
523 					    dinfo->conf.pc_class;
524 					conf_old.pc_subclass =
525 					    dinfo->conf.pc_subclass;
526 					conf_old.pc_progif =
527 					    dinfo->conf.pc_progif;
528 					conf_old.pc_revid =
529 					    dinfo->conf.pc_revid;
530 					strncpy(conf_old.pd_name,
531 					    dinfo->conf.pd_name,
532 					    sizeof(conf_old.pd_name));
533 					conf_old.pd_name[PCI_MAXNAMELEN] = 0;
534 					conf_old.pd_unit =
535 					    dinfo->conf.pd_unit;
536 					confdata = &conf_old;
537 				} else
538 #endif
539 					confdata = &dinfo->conf;
540 				/* Only if we can copy it out do we count it. */
541 				if (!(error = copyout(confdata,
542 				    (caddr_t)cio->matches +
543 				    confsz * cio->num_matches, confsz)))
544 					cio->num_matches++;
545 			}
546 		}
547 
548 		/*
549 		 * Set the pointer into the list, so if the user is getting
550 		 * n records at a time, where n < pci_numdevs,
551 		 */
552 		cio->offset = i;
553 
554 		/*
555 		 * Set the generation, the user will need this if they make
556 		 * another ioctl call with offset != 0.
557 		 */
558 		cio->generation = pci_generation;
559 
560 		/*
561 		 * If this is the last device, inform the user so he won't
562 		 * bother asking for more devices.  If dinfo isn't NULL, we
563 		 * know that there are more matches in the list because of
564 		 * the way the traversal is done.
565 		 */
566 		if (dinfo == NULL)
567 			cio->status = PCI_GETCONF_LAST_DEVICE;
568 		else
569 			cio->status = PCI_GETCONF_MORE_DEVS;
570 
571 getconfexit:
572 		if (pattern_buf != NULL)
573 			kfree(pattern_buf, M_TEMP);
574 #ifdef PRE7_COMPAT
575 		if (pattern_buf_old != NULL)
576 			kfree(pattern_buf_old, M_TEMP);
577 #endif
578 
579 		break;
580 
581 #ifdef PRE7_COMPAT
582 	case PCIOCREAD_OLD:
583 	case PCIOCWRITE_OLD:
584 		io_old = (struct pci_io_old *)ap->a_data;
585 		iodata.pi_sel.pc_domain = 0;
586 		iodata.pi_sel.pc_bus = io_old->pi_sel.pc_bus;
587 		iodata.pi_sel.pc_dev = io_old->pi_sel.pc_dev;
588 		iodata.pi_sel.pc_func = io_old->pi_sel.pc_func;
589 		iodata.pi_reg = io_old->pi_reg;
590 		iodata.pi_width = io_old->pi_width;
591 		iodata.pi_data = io_old->pi_data;
592 		ap->a_data = (caddr_t)&iodata;
593 		/* FALLTHROUGH */
594 #endif
595 	case PCIOCREAD:
596 	case PCIOCWRITE:
597 		io = (struct pci_io *)ap->a_data;
598 		switch(io->pi_width) {
599 		case 4:
600 		case 2:
601 		case 1:
602 			/* Make sure register is not negative and aligned. */
603 			if (io->pi_reg < 0 ||
604 			    io->pi_reg & (io->pi_width - 1)) {
605 				error = EINVAL;
606 				break;
607 			}
608 			/*
609 			 * Assume that the user-level bus number is
610 			 * in fact the physical PCI bus number.
611 			 * Look up the grandparent, i.e. the bridge device,
612 			 * so that we can issue configuration space cycles.
613 			 */
614 			pcidev = pci_find_dbsf(io->pi_sel.pc_domain,
615 			    io->pi_sel.pc_bus, io->pi_sel.pc_dev,
616 			    io->pi_sel.pc_func);
617 			if (pcidev) {
618 				brdev = device_get_parent(
619 				    device_get_parent(pcidev));
620 
621 #ifdef PRE7_COMPAT
622 				if (ap->a_cmd == PCIOCWRITE || ap->a_cmd == PCIOCWRITE_OLD)
623 #else
624 				if (ap->a_cmd == PCIOCWRITE)
625 #endif
626 					PCIB_WRITE_CONFIG(brdev,
627 							  io->pi_sel.pc_bus,
628 							  io->pi_sel.pc_dev,
629 							  io->pi_sel.pc_func,
630 							  io->pi_reg,
631 							  io->pi_data,
632 							  io->pi_width);
633 #ifdef PRE7_COMPAT
634 				else if (ap->a_cmd == PCIOCREAD_OLD)
635 					io_old->pi_data =
636 						PCIB_READ_CONFIG(brdev,
637 							  io->pi_sel.pc_bus,
638 							  io->pi_sel.pc_dev,
639 							  io->pi_sel.pc_func,
640 							  io->pi_reg,
641 							  io->pi_width);
642 #endif
643 				else
644 					io->pi_data =
645 						PCIB_READ_CONFIG(brdev,
646 							  io->pi_sel.pc_bus,
647 							  io->pi_sel.pc_dev,
648 							  io->pi_sel.pc_func,
649 							  io->pi_reg,
650 							  io->pi_width);
651 				error = 0;
652 			} else {
653 #ifdef COMPAT_FREEBSD4
654 				if (cmd == PCIOCREAD_OLD) {
655 					io_old->pi_data = -1;
656 					error = 0;
657 				} else
658 #endif
659 					error = ENODEV;
660 			}
661 			break;
662 		default:
663 			error = EINVAL;
664 			break;
665 		}
666 		break;
667 
668 	case PCIOCGETBAR:
669 		bio = (struct pci_bar_io *)ap->a_data;
670 
671 		/*
672 		 * Assume that the user-level bus number is
673 		 * in fact the physical PCI bus number.
674 		 */
675 		pcidev = pci_find_dbsf(bio->pbi_sel.pc_domain,
676 		    bio->pbi_sel.pc_bus, bio->pbi_sel.pc_dev,
677 		    bio->pbi_sel.pc_func);
678 		if (pcidev == NULL) {
679 			error = ENODEV;
680 			break;
681 		}
682 		dinfo = device_get_ivars(pcidev);
683 
684 		/*
685 		 * Look for a resource list entry matching the requested BAR.
686 		 *
687 		 * XXX: This will not find BARs that are not initialized, but
688 		 * maybe that is ok?
689 		 */
690 		rle = resource_list_find(&dinfo->resources, SYS_RES_MEMORY,
691 		    bio->pbi_reg);
692 		if (rle == NULL)
693 			rle = resource_list_find(&dinfo->resources,
694 			    SYS_RES_IOPORT, bio->pbi_reg);
695 		if (rle == NULL || rle->res == NULL) {
696 			error = EINVAL;
697 			break;
698 		}
699 
700 		/*
701 		 * Ok, we have a resource for this BAR.  Read the lower
702 		 * 32 bits to get any flags.
703 		 */
704 		value = pci_read_config(pcidev, bio->pbi_reg, 4);
705 		if (PCI_BAR_MEM(value)) {
706 			if (rle->type != SYS_RES_MEMORY) {
707 				error = EINVAL;
708 				break;
709 			}
710 			value &= ~PCIM_BAR_MEM_BASE;
711 		} else {
712 			if (rle->type != SYS_RES_IOPORT) {
713 				error = EINVAL;
714 				break;
715 			}
716 			value &= ~PCIM_BAR_IO_BASE;
717 		}
718 		bio->pbi_base = rman_get_start(rle->res) | value;
719 		bio->pbi_length = rman_get_size(rle->res);
720 
721 		/*
722 		 * Check the command register to determine if this BAR
723 		 * is enabled.
724 		 */
725 		value = pci_read_config(pcidev, PCIR_COMMAND, 2);
726 		if (rle->type == SYS_RES_MEMORY)
727 			bio->pbi_enabled = (value & PCIM_CMD_MEMEN) != 0;
728 		else
729 			bio->pbi_enabled = (value & PCIM_CMD_PORTEN) != 0;
730 		error = 0;
731 		break;
732 	case PCIOCATTACHED:
733 		error = 0;
734 		io = (struct pci_io *)ap->a_data;
735 		pcidev = pci_find_dbsf(io->pi_sel.pc_domain, io->pi_sel.pc_bus,
736 				       io->pi_sel.pc_dev, io->pi_sel.pc_func);
737 		if (pcidev != NULL)
738 			io->pi_data = device_is_attached(pcidev);
739 		else
740 			error = ENODEV;
741 		break;
742 	default:
743 		error = ENOTTY;
744 		break;
745 	}
746 	lwkt_reltoken(&pci_token);
747 
748 	return (error);
749 }
750