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