usr.sbin/makefs/ffs: Remove m_buf::b_is_hammer2
[dragonfly.git] / sys / bus / pci / pci_user.c
bloba97148c9c7669ea245129dabd17f5fe022ce4f51
1 /*-
2 * Copyright (c) 1997, Stefan Esser <se@kfreebsd.org>
3 * All rights reserved.
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.
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.
26 * $FreeBSD: src/sys/dev/pci/pci_user.c,v 1.22.2.4.2.1 2009/04/15 03:14:26 kensmith Exp $
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>
41 #include <vm/vm.h>
42 #include <vm/pmap.h>
43 #include <vm/vm_extern.h>
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>
52 #include "pcib_if.h"
53 #include "pci_if.h"
56 * This is the user interface to PCI configuration space.
58 static struct lwkt_token pci_token = LWKT_TOKEN_INITIALIZER(pci_token);
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;
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,
73 static int
74 pci_open(struct dev_open_args *ap)
76 int oflags = ap->a_oflags;
78 if (oflags & FWRITE) {
79 if (securelevel > 0)
80 return (EPERM);
83 return (0);
86 static int
87 pci_close(struct dev_close_args *ap)
89 return 0;
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.
99 static int
100 pci_conf_match(struct pci_match_conf *matches, int num_matches,
101 struct pci_conf *match_buf)
103 int i;
105 if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0))
106 return(1);
108 for (i = 0; i < num_matches; i++) {
110 * I'm not sure why someone would do this...but...
112 if (matches[i].flags == PCI_GETCONF_NO_MATCH)
113 continue;
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.
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;
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;
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;
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;
137 if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR) != 0)
138 && (match_buf->pc_vendor != matches[i].pc_vendor))
139 continue;
141 if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE) != 0)
142 && (match_buf->pc_device != matches[i].pc_device))
143 continue;
145 if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS) != 0)
146 && (match_buf->pc_class != matches[i].pc_class))
147 continue;
149 if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT) != 0)
150 && (match_buf->pd_unit != matches[i].pd_unit))
151 continue;
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;
158 return(0);
161 return(1);
164 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
165 defined(COMPAT_FREEBSD6) || defined(__DragonFly__)
166 #define PRE7_COMPAT
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;
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 */
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 */
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 */
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 */
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)
224 static int pci_conf_match_old(struct pci_match_conf_old *matches,
225 int num_matches, struct pci_conf *match_buf);
227 static int
228 pci_conf_match_old(struct pci_match_conf_old *matches, int num_matches,
229 struct pci_conf *match_buf)
231 int i;
233 if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0))
234 return(1);
236 for (i = 0; i < num_matches; i++) {
237 if (match_buf->pc_sel.pc_domain != 0)
238 continue;
241 * I'm not sure why someone would do this...but...
243 if (matches[i].flags == PCI_GETCONF_NO_MATCH_OLD)
244 continue;
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.
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;
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;
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;
263 if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR_OLD) != 0)
264 && (match_buf->pc_vendor != matches[i].pc_vendor))
265 continue;
267 if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE_OLD) != 0)
268 && (match_buf->pc_device != matches[i].pc_device))
269 continue;
271 if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS_OLD) != 0)
272 && (match_buf->pc_class != matches[i].pc_class))
273 continue;
275 if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT_OLD) != 0)
276 && (match_buf->pd_unit != matches[i].pd_unit))
277 continue;
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;
284 return(0);
287 return(1);
290 #endif
292 static int
293 pci_ioctl(struct dev_ioctl_args *ap)
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;
314 io_old = NULL;
315 pattern_buf_old = NULL;
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
325 lwkt_gettoken(&pci_token);
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;
335 pattern_buf = NULL;
336 num_patterns = 0;
337 dinfo = NULL;
339 cio->num_matches = 0;
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.
347 if ((cio->offset != 0)
348 && (cio->generation != pci_generation)){
349 cio->status = PCI_GETCONF_LIST_CHANGED;
350 error = 0;
351 break;
355 * Check to see whether the user has asked for an offset
356 * past the end of our list.
358 if (cio->offset >= pci_numdevs) {
359 cio->status = PCI_GETCONF_LAST_DEVICE;
360 error = 0;
361 break;
364 /* get the head of the device queue */
365 devlist_head = &pci_devq;
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.
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);
383 * Since we know that iolen is a multiple of the size of
384 * the pciconf union, it's okay to do this.
386 ionum = iolen / confsz;
389 * If this test is true, the user wants the pci_conf
390 * structures returned to match the supplied entries.
392 if ((cio->num_patterns > 0) && (cio->num_patterns < pci_numdevs)
393 && (cio->pat_buf_len > 0)) {
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.
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;
421 * Allocate a buffer to hold the patterns.
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
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);
437 if (error != 0) {
438 error = EINVAL;
439 goto getconfexit;
441 num_patterns = cio->num_patterns;
442 } else if ((cio->num_patterns > 0)
443 || (cio->pat_buf_len > 0)) {
445 * The user made a mistake, spit out an error.
447 cio->status = PCI_GETCONF_ERROR;
448 error = EINVAL;
449 break;
453 * Go through the list of devices and copy out the devices
454 * that match the user's criteria.
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++) {
462 if (i < cio->offset)
463 continue;
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;
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
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.
502 if (cio->num_matches >= ionum)
503 break;
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++;
549 * Set the pointer into the list, so if the user is getting
550 * n records at a time, where n < pci_numdevs,
552 cio->offset = i;
555 * Set the generation, the user will need this if they make
556 * another ioctl call with offset != 0.
558 cio->generation = pci_generation;
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.
566 if (dinfo == NULL)
567 cio->status = PCI_GETCONF_LAST_DEVICE;
568 else
569 cio->status = PCI_GETCONF_MORE_DEVS;
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
579 break;
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;
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.
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));
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;
661 break;
662 default:
663 error = EINVAL;
664 break;
666 break;
668 case PCIOCGETBAR:
669 bio = (struct pci_bar_io *)ap->a_data;
672 * Assume that the user-level bus number is
673 * in fact the physical PCI bus number.
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;
682 dinfo = device_get_ivars(pcidev);
685 * Look for a resource list entry matching the requested BAR.
687 * XXX: This will not find BARs that are not initialized, but
688 * maybe that is ok?
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;
701 * Ok, we have a resource for this BAR. Read the lower
702 * 32 bits to get any flags.
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;
710 value &= ~PCIM_BAR_MEM_BASE;
711 } else {
712 if (rle->type != SYS_RES_IOPORT) {
713 error = EINVAL;
714 break;
716 value &= ~PCIM_BAR_IO_BASE;
718 bio->pbi_base = rman_get_start(rle->res) | value;
719 bio->pbi_length = rman_get_size(rle->res);
722 * Check the command register to determine if this BAR
723 * is enabled.
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;
746 lwkt_reltoken(&pci_token);
748 return (error);