2 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include "opt_bus.h" /* XXX trim includes */
31 #include "opt_compat.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/linker.h>
38 #include <sys/fcntl.h>
40 #include <sys/kernel.h>
42 #include <sys/queue.h>
43 #include <sys/types.h>
47 #include <vm/vm_extern.h>
50 #include <machine/bus.h>
52 #include <machine/resource.h>
54 #include <sys/pciio.h>
55 #include <dev/pci/pcireg.h>
56 #include <dev/pci/pcivar.h>
62 * This is the user interface to PCI configuration space.
65 static d_open_t pci_open;
66 static d_close_t pci_close;
67 static int pci_conf_match(struct pci_match_conf *matches, int num_matches,
68 struct pci_conf *match_buf);
69 static d_ioctl_t pci_ioctl;
71 struct cdevsw pcicdev = {
72 .d_version = D_VERSION,
73 .d_flags = D_NEEDGIANT,
81 pci_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
85 if (oflags & FWRITE) {
86 error = securelevel_gt(td->td_ucred, 0);
95 pci_close(struct cdev *dev, int flag, int devtype, struct thread *td)
101 * Match a single pci_conf structure against an array of pci_match_conf
102 * structures. The first argument, 'matches', is an array of num_matches
103 * pci_match_conf structures. match_buf is a pointer to the pci_conf
104 * structure that will be compared to every entry in the matches array.
105 * This function returns 1 on failure, 0 on success.
108 pci_conf_match(struct pci_match_conf *matches, int num_matches,
109 struct pci_conf *match_buf)
113 if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0))
116 for (i = 0; i < num_matches; i++) {
118 * I'm not sure why someone would do this...but...
120 if (matches[i].flags == PCI_GETCONF_NO_MATCH)
124 * Look at each of the match flags. If it's set, do the
125 * comparison. If the comparison fails, we don't have a
126 * match, go on to the next item if there is one.
128 if (((matches[i].flags & PCI_GETCONF_MATCH_DOMAIN) != 0)
129 && (match_buf->pc_sel.pc_domain !=
130 matches[i].pc_sel.pc_domain))
133 if (((matches[i].flags & PCI_GETCONF_MATCH_BUS) != 0)
134 && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus))
137 if (((matches[i].flags & PCI_GETCONF_MATCH_DEV) != 0)
138 && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev))
141 if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC) != 0)
142 && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func))
145 if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR) != 0)
146 && (match_buf->pc_vendor != matches[i].pc_vendor))
149 if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE) != 0)
150 && (match_buf->pc_device != matches[i].pc_device))
153 if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS) != 0)
154 && (match_buf->pc_class != matches[i].pc_class))
157 if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT) != 0)
158 && (match_buf->pd_unit != matches[i].pd_unit))
161 if (((matches[i].flags & PCI_GETCONF_MATCH_NAME) != 0)
162 && (strncmp(matches[i].pd_name, match_buf->pd_name,
163 sizeof(match_buf->pd_name)) != 0))
172 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
173 defined(COMPAT_FREEBSD6)
177 PCI_GETCONF_NO_MATCH_OLD = 0x00,
178 PCI_GETCONF_MATCH_BUS_OLD = 0x01,
179 PCI_GETCONF_MATCH_DEV_OLD = 0x02,
180 PCI_GETCONF_MATCH_FUNC_OLD = 0x04,
181 PCI_GETCONF_MATCH_NAME_OLD = 0x08,
182 PCI_GETCONF_MATCH_UNIT_OLD = 0x10,
183 PCI_GETCONF_MATCH_VENDOR_OLD = 0x20,
184 PCI_GETCONF_MATCH_DEVICE_OLD = 0x40,
185 PCI_GETCONF_MATCH_CLASS_OLD = 0x80
186 } pci_getconf_flags_old;
189 u_int8_t pc_bus; /* bus number */
190 u_int8_t pc_dev; /* device on this bus */
191 u_int8_t pc_func; /* function on this device */
194 struct pci_conf_old {
195 struct pcisel_old pc_sel; /* bus+slot+function */
196 u_int8_t pc_hdr; /* PCI header type */
197 u_int16_t pc_subvendor; /* card vendor ID */
198 u_int16_t pc_subdevice; /* card device ID, assigned by
200 u_int16_t pc_vendor; /* chip vendor ID */
201 u_int16_t pc_device; /* chip device ID, assigned by
203 u_int8_t pc_class; /* chip PCI class */
204 u_int8_t pc_subclass; /* chip PCI subclass */
205 u_int8_t pc_progif; /* chip PCI programming interface */
206 u_int8_t pc_revid; /* chip revision ID */
207 char pd_name[PCI_MAXNAMELEN + 1]; /* device name */
208 u_long pd_unit; /* device unit number */
211 struct pci_match_conf_old {
212 struct pcisel_old pc_sel; /* bus+slot+function */
213 char pd_name[PCI_MAXNAMELEN + 1]; /* device name */
214 u_long pd_unit; /* Unit number */
215 u_int16_t pc_vendor; /* PCI Vendor ID */
216 u_int16_t pc_device; /* PCI Device ID */
217 u_int8_t pc_class; /* PCI class */
218 pci_getconf_flags_old flags; /* Matching expression */
222 struct pcisel_old pi_sel; /* device to operate on */
223 int pi_reg; /* configuration register to examine */
224 int pi_width; /* width (in bytes) of read or write */
225 u_int32_t pi_data; /* data to write or result of read */
228 #ifdef COMPAT_FREEBSD32
229 struct pci_conf_old32 {
230 struct pcisel_old pc_sel; /* bus+slot+function */
231 uint8_t pc_hdr; /* PCI header type */
232 uint16_t pc_subvendor; /* card vendor ID */
233 uint16_t pc_subdevice; /* card device ID, assigned by
235 uint16_t pc_vendor; /* chip vendor ID */
236 uint16_t pc_device; /* chip device ID, assigned by
238 uint8_t pc_class; /* chip PCI class */
239 uint8_t pc_subclass; /* chip PCI subclass */
240 uint8_t pc_progif; /* chip PCI programming interface */
241 uint8_t pc_revid; /* chip revision ID */
242 char pd_name[PCI_MAXNAMELEN + 1]; /* device name */
243 uint32_t pd_unit; /* device unit number (u_long) */
246 struct pci_match_conf_old32 {
247 struct pcisel_old pc_sel; /* bus+slot+function */
248 char pd_name[PCI_MAXNAMELEN + 1]; /* device name */
249 uint32_t pd_unit; /* Unit number (u_long) */
250 uint16_t pc_vendor; /* PCI Vendor ID */
251 uint16_t pc_device; /* PCI Device ID */
252 uint8_t pc_class; /* PCI class */
253 pci_getconf_flags_old flags; /* Matching expression */
256 struct pci_conf_io32 {
257 uint32_t pat_buf_len; /* pattern buffer length */
258 uint32_t num_patterns; /* number of patterns */
259 uint32_t patterns; /* pattern buffer
260 (struct pci_match_conf_old32 *) */
261 uint32_t match_buf_len; /* match buffer length */
262 uint32_t num_matches; /* number of matches returned */
263 uint32_t matches; /* match buffer
264 (struct pci_conf_old32 *) */
265 uint32_t offset; /* offset into device list */
266 uint32_t generation; /* device list generation */
267 pci_getconf_status status; /* request status */
270 #define PCIOCGETCONF_OLD32 _IOWR('p', 1, struct pci_conf_io32)
271 #endif /* COMPAT_FREEBSD32 */
273 #define PCIOCGETCONF_OLD _IOWR('p', 1, struct pci_conf_io)
274 #define PCIOCREAD_OLD _IOWR('p', 2, struct pci_io_old)
275 #define PCIOCWRITE_OLD _IOWR('p', 3, struct pci_io_old)
277 static int pci_conf_match_old(struct pci_match_conf_old *matches,
278 int num_matches, struct pci_conf *match_buf);
281 pci_conf_match_old(struct pci_match_conf_old *matches, int num_matches,
282 struct pci_conf *match_buf)
286 if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0))
289 for (i = 0; i < num_matches; i++) {
290 if (match_buf->pc_sel.pc_domain != 0)
294 * I'm not sure why someone would do this...but...
296 if (matches[i].flags == PCI_GETCONF_NO_MATCH_OLD)
300 * Look at each of the match flags. If it's set, do the
301 * comparison. If the comparison fails, we don't have a
302 * match, go on to the next item if there is one.
304 if (((matches[i].flags & PCI_GETCONF_MATCH_BUS_OLD) != 0)
305 && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus))
308 if (((matches[i].flags & PCI_GETCONF_MATCH_DEV_OLD) != 0)
309 && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev))
312 if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC_OLD) != 0)
313 && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func))
316 if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR_OLD) != 0)
317 && (match_buf->pc_vendor != matches[i].pc_vendor))
320 if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE_OLD) != 0)
321 && (match_buf->pc_device != matches[i].pc_device))
324 if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS_OLD) != 0)
325 && (match_buf->pc_class != matches[i].pc_class))
328 if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT_OLD) != 0)
329 && (match_buf->pd_unit != matches[i].pd_unit))
332 if (((matches[i].flags & PCI_GETCONF_MATCH_NAME_OLD) != 0)
333 && (strncmp(matches[i].pd_name, match_buf->pd_name,
334 sizeof(match_buf->pd_name)) != 0))
343 #ifdef COMPAT_FREEBSD32
345 pci_conf_match_old32(struct pci_match_conf_old32 *matches, int num_matches,
346 struct pci_conf *match_buf)
350 if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0))
353 for (i = 0; i < num_matches; i++) {
354 if (match_buf->pc_sel.pc_domain != 0)
358 * I'm not sure why someone would do this...but...
360 if (matches[i].flags == PCI_GETCONF_NO_MATCH_OLD)
364 * Look at each of the match flags. If it's set, do the
365 * comparison. If the comparison fails, we don't have a
366 * match, go on to the next item if there is one.
368 if (((matches[i].flags & PCI_GETCONF_MATCH_BUS_OLD) != 0) &&
369 (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus))
372 if (((matches[i].flags & PCI_GETCONF_MATCH_DEV_OLD) != 0) &&
373 (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev))
376 if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC_OLD) != 0) &&
377 (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func))
380 if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR_OLD) != 0) &&
381 (match_buf->pc_vendor != matches[i].pc_vendor))
384 if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE_OLD) != 0) &&
385 (match_buf->pc_device != matches[i].pc_device))
388 if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS_OLD) != 0) &&
389 (match_buf->pc_class != matches[i].pc_class))
392 if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT_OLD) != 0) &&
393 ((u_int32_t)match_buf->pd_unit != matches[i].pd_unit))
396 if (((matches[i].flags & PCI_GETCONF_MATCH_NAME_OLD) != 0) &&
397 (strncmp(matches[i].pd_name, match_buf->pd_name,
398 sizeof(match_buf->pd_name)) != 0))
406 #endif /* COMPAT_FREEBSD32 */
407 #endif /* PRE7_COMPAT */
410 pci_list_vpd(device_t dev, struct pci_list_vpd_io *lvio)
412 struct pci_vpd_element vpd_element, *vpd_user;
413 struct pcicfg_vpd *vpd;
417 vpd = pci_fetch_vpd_list(dev);
418 if (vpd->vpd_reg == 0 || vpd->vpd_ident == NULL)
422 * Calculate the amount of space needed in the data buffer. An
423 * identifier element is always present followed by the read-only
424 * and read-write keywords.
426 len = sizeof(struct pci_vpd_element) + strlen(vpd->vpd_ident);
427 for (i = 0; i < vpd->vpd_rocnt; i++)
428 len += sizeof(struct pci_vpd_element) + vpd->vpd_ros[i].len;
429 for (i = 0; i < vpd->vpd_wcnt; i++)
430 len += sizeof(struct pci_vpd_element) + vpd->vpd_w[i].len;
432 if (lvio->plvi_len == 0) {
433 lvio->plvi_len = len;
436 if (lvio->plvi_len < len) {
437 lvio->plvi_len = len;
442 * Copyout the identifier string followed by each keyword and
445 vpd_user = lvio->plvi_data;
446 vpd_element.pve_keyword[0] = '\0';
447 vpd_element.pve_keyword[1] = '\0';
448 vpd_element.pve_flags = PVE_FLAG_IDENT;
449 vpd_element.pve_datalen = strlen(vpd->vpd_ident);
450 error = copyout(&vpd_element, vpd_user, sizeof(vpd_element));
453 error = copyout(vpd->vpd_ident, vpd_user->pve_data,
454 strlen(vpd->vpd_ident));
457 vpd_user = PVE_NEXT(vpd_user);
458 vpd_element.pve_flags = 0;
459 for (i = 0; i < vpd->vpd_rocnt; i++) {
460 vpd_element.pve_keyword[0] = vpd->vpd_ros[i].keyword[0];
461 vpd_element.pve_keyword[1] = vpd->vpd_ros[i].keyword[1];
462 vpd_element.pve_datalen = vpd->vpd_ros[i].len;
463 error = copyout(&vpd_element, vpd_user, sizeof(vpd_element));
466 error = copyout(vpd->vpd_ros[i].value, vpd_user->pve_data,
467 vpd->vpd_ros[i].len);
470 vpd_user = PVE_NEXT(vpd_user);
472 vpd_element.pve_flags = PVE_FLAG_RW;
473 for (i = 0; i < vpd->vpd_wcnt; i++) {
474 vpd_element.pve_keyword[0] = vpd->vpd_w[i].keyword[0];
475 vpd_element.pve_keyword[1] = vpd->vpd_w[i].keyword[1];
476 vpd_element.pve_datalen = vpd->vpd_w[i].len;
477 error = copyout(&vpd_element, vpd_user, sizeof(vpd_element));
480 error = copyout(vpd->vpd_w[i].value, vpd_user->pve_data,
484 vpd_user = PVE_NEXT(vpd_user);
486 KASSERT((char *)vpd_user - (char *)lvio->plvi_data == len,
487 ("length mismatch"));
488 lvio->plvi_len = len;
493 pci_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
498 struct devlist *devlist_head;
499 struct pci_conf_io *cio = NULL;
500 struct pci_devinfo *dinfo;
502 struct pci_bar_io *bio;
503 struct pci_list_vpd_io *lvio;
504 struct pci_match_conf *pattern_buf;
506 size_t confsz, iolen, pbufsz;
507 int error, ionum, i, num_patterns;
509 #ifdef COMPAT_FREEBSD32
510 struct pci_conf_io32 *cio32 = NULL;
511 struct pci_conf_old32 conf_old32;
512 struct pci_match_conf_old32 *pattern_buf_old32 = NULL;
514 struct pci_conf_old conf_old;
515 struct pci_io iodata;
516 struct pci_io_old *io_old;
517 struct pci_match_conf_old *pattern_buf_old = NULL;
522 if (!(flag & FWRITE)) {
525 #ifdef COMPAT_FREEBSD32
526 case PCIOCGETCONF_OLD32:
528 case PCIOCGETCONF_OLD:
541 #ifdef COMPAT_FREEBSD32
542 case PCIOCGETCONF_OLD32:
543 cio32 = (struct pci_conf_io32 *)data;
544 cio = malloc(sizeof(struct pci_conf_io), M_TEMP, M_WAITOK);
545 cio->pat_buf_len = cio32->pat_buf_len;
546 cio->num_patterns = cio32->num_patterns;
547 cio->patterns = (void *)(uintptr_t)cio32->patterns;
548 cio->match_buf_len = cio32->match_buf_len;
549 cio->num_matches = cio32->num_matches;
550 cio->matches = (void *)(uintptr_t)cio32->matches;
551 cio->offset = cio32->offset;
552 cio->generation = cio32->generation;
553 cio->status = cio32->status;
554 cio32->num_matches = 0;
557 case PCIOCGETCONF_OLD:
560 cio = (struct pci_conf_io *)data;
565 #ifdef COMPAT_FREEBSD32
566 case PCIOCGETCONF_OLD32:
568 case PCIOCGETCONF_OLD:
576 cio->num_matches = 0;
579 * If the user specified an offset into the device list,
580 * but the list has changed since they last called this
581 * ioctl, tell them that the list has changed. They will
582 * have to get the list from the beginning.
584 if ((cio->offset != 0)
585 && (cio->generation != pci_generation)){
586 cio->status = PCI_GETCONF_LIST_CHANGED;
592 * Check to see whether the user has asked for an offset
593 * past the end of our list.
595 if (cio->offset >= pci_numdevs) {
596 cio->status = PCI_GETCONF_LAST_DEVICE;
601 /* get the head of the device queue */
602 devlist_head = &pci_devq;
605 * Determine how much room we have for pci_conf structures.
606 * Round the user's buffer size down to the nearest
607 * multiple of sizeof(struct pci_conf) in case the user
608 * didn't specify a multiple of that size.
611 #ifdef COMPAT_FREEBSD32
612 if (cmd == PCIOCGETCONF_OLD32)
613 confsz = sizeof(struct pci_conf_old32);
616 if (cmd == PCIOCGETCONF_OLD)
617 confsz = sizeof(struct pci_conf_old);
620 confsz = sizeof(struct pci_conf);
621 iolen = min(cio->match_buf_len - (cio->match_buf_len % confsz),
622 pci_numdevs * confsz);
625 * Since we know that iolen is a multiple of the size of
626 * the pciconf union, it's okay to do this.
628 ionum = iolen / confsz;
631 * If this test is true, the user wants the pci_conf
632 * structures returned to match the supplied entries.
634 if ((cio->num_patterns > 0) && (cio->num_patterns < pci_numdevs)
635 && (cio->pat_buf_len > 0)) {
637 * pat_buf_len needs to be:
638 * num_patterns * sizeof(struct pci_match_conf)
639 * While it is certainly possible the user just
640 * allocated a large buffer, but set the number of
641 * matches correctly, it is far more likely that
642 * their kernel doesn't match the userland utility
643 * they're using. It's also possible that the user
644 * forgot to initialize some variables. Yes, this
645 * may be overly picky, but I hazard to guess that
646 * it's far more likely to just catch folks that
647 * updated their kernel but not their userland.
650 #ifdef COMPAT_FREEBSD32
651 if (cmd == PCIOCGETCONF_OLD32)
652 pbufsz = sizeof(struct pci_match_conf_old32);
655 if (cmd == PCIOCGETCONF_OLD)
656 pbufsz = sizeof(struct pci_match_conf_old);
659 pbufsz = sizeof(struct pci_match_conf);
660 if (cio->num_patterns * pbufsz != cio->pat_buf_len) {
661 /* The user made a mistake, return an error. */
662 cio->status = PCI_GETCONF_ERROR;
668 * Allocate a buffer to hold the patterns.
671 #ifdef COMPAT_FREEBSD32
672 if (cmd == PCIOCGETCONF_OLD32) {
673 pattern_buf_old32 = malloc(cio->pat_buf_len,
675 error = copyin(cio->patterns,
676 pattern_buf_old32, cio->pat_buf_len);
678 #endif /* COMPAT_FREEBSD32 */
679 if (cmd == PCIOCGETCONF_OLD) {
680 pattern_buf_old = malloc(cio->pat_buf_len,
682 error = copyin(cio->patterns,
683 pattern_buf_old, cio->pat_buf_len);
685 #endif /* PRE7_COMPAT */
687 pattern_buf = malloc(cio->pat_buf_len, M_TEMP,
689 error = copyin(cio->patterns, pattern_buf,
696 num_patterns = cio->num_patterns;
697 } else if ((cio->num_patterns > 0)
698 || (cio->pat_buf_len > 0)) {
700 * The user made a mistake, spit out an error.
702 cio->status = PCI_GETCONF_ERROR;
708 * Go through the list of devices and copy out the devices
709 * that match the user's criteria.
711 for (cio->num_matches = 0, i = 0,
712 dinfo = STAILQ_FIRST(devlist_head);
714 dinfo = STAILQ_NEXT(dinfo, pci_links), i++) {
719 /* Populate pd_name and pd_unit */
722 name = device_get_name(dinfo->cfg.dev);
724 strncpy(dinfo->conf.pd_name, name,
725 sizeof(dinfo->conf.pd_name));
726 dinfo->conf.pd_name[PCI_MAXNAMELEN] = 0;
727 dinfo->conf.pd_unit =
728 device_get_unit(dinfo->cfg.dev);
730 dinfo->conf.pd_name[0] = '\0';
731 dinfo->conf.pd_unit = 0;
736 #ifdef COMPAT_FREEBSD32
737 (cmd == PCIOCGETCONF_OLD32 &&
738 (pattern_buf_old32 == NULL ||
739 pci_conf_match_old32(pattern_buf_old32,
740 num_patterns, &dinfo->conf) == 0)) ||
742 (cmd == PCIOCGETCONF_OLD &&
743 (pattern_buf_old == NULL ||
744 pci_conf_match_old(pattern_buf_old, num_patterns,
745 &dinfo->conf) == 0)) ||
746 (cmd == PCIOCGETCONF &&
747 (pattern_buf == NULL ||
748 pci_conf_match(pattern_buf, num_patterns,
749 &dinfo->conf) == 0))) {
751 if (pattern_buf == NULL ||
752 pci_conf_match(pattern_buf, num_patterns,
753 &dinfo->conf) == 0) {
756 * If we've filled up the user's buffer,
757 * break out at this point. Since we've
758 * got a match here, we'll pick right back
759 * up at the matching entry. We can also
760 * tell the user that there are more matches
763 if (cio->num_matches >= ionum)
767 #ifdef COMPAT_FREEBSD32
768 if (cmd == PCIOCGETCONF_OLD32) {
769 conf_old32.pc_sel.pc_bus =
770 dinfo->conf.pc_sel.pc_bus;
771 conf_old32.pc_sel.pc_dev =
772 dinfo->conf.pc_sel.pc_dev;
773 conf_old32.pc_sel.pc_func =
774 dinfo->conf.pc_sel.pc_func;
775 conf_old32.pc_hdr = dinfo->conf.pc_hdr;
776 conf_old32.pc_subvendor =
777 dinfo->conf.pc_subvendor;
778 conf_old32.pc_subdevice =
779 dinfo->conf.pc_subdevice;
780 conf_old32.pc_vendor =
781 dinfo->conf.pc_vendor;
782 conf_old32.pc_device =
783 dinfo->conf.pc_device;
784 conf_old32.pc_class =
785 dinfo->conf.pc_class;
786 conf_old32.pc_subclass =
787 dinfo->conf.pc_subclass;
788 conf_old32.pc_progif =
789 dinfo->conf.pc_progif;
790 conf_old32.pc_revid =
791 dinfo->conf.pc_revid;
792 strncpy(conf_old32.pd_name,
794 sizeof(conf_old32.pd_name));
795 conf_old32.pd_name[PCI_MAXNAMELEN] = 0;
797 (uint32_t)dinfo->conf.pd_unit;
798 confdata = &conf_old32;
800 #endif /* COMPAT_FREEBSD32 */
801 if (cmd == PCIOCGETCONF_OLD) {
802 conf_old.pc_sel.pc_bus =
803 dinfo->conf.pc_sel.pc_bus;
804 conf_old.pc_sel.pc_dev =
805 dinfo->conf.pc_sel.pc_dev;
806 conf_old.pc_sel.pc_func =
807 dinfo->conf.pc_sel.pc_func;
808 conf_old.pc_hdr = dinfo->conf.pc_hdr;
809 conf_old.pc_subvendor =
810 dinfo->conf.pc_subvendor;
811 conf_old.pc_subdevice =
812 dinfo->conf.pc_subdevice;
814 dinfo->conf.pc_vendor;
816 dinfo->conf.pc_device;
818 dinfo->conf.pc_class;
819 conf_old.pc_subclass =
820 dinfo->conf.pc_subclass;
822 dinfo->conf.pc_progif;
824 dinfo->conf.pc_revid;
825 strncpy(conf_old.pd_name,
827 sizeof(conf_old.pd_name));
828 conf_old.pd_name[PCI_MAXNAMELEN] = 0;
831 confdata = &conf_old;
833 #endif /* PRE7_COMPAT */
834 confdata = &dinfo->conf;
835 error = copyout(confdata,
836 (caddr_t)cio->matches +
837 confsz * cio->num_matches, confsz);
845 * Set the pointer into the list, so if the user is getting
846 * n records at a time, where n < pci_numdevs,
851 * Set the generation, the user will need this if they make
852 * another ioctl call with offset != 0.
854 cio->generation = pci_generation;
857 * If this is the last device, inform the user so he won't
858 * bother asking for more devices. If dinfo isn't NULL, we
859 * know that there are more matches in the list because of
860 * the way the traversal is done.
863 cio->status = PCI_GETCONF_LAST_DEVICE;
865 cio->status = PCI_GETCONF_MORE_DEVS;
869 #ifdef COMPAT_FREEBSD32
870 if (cmd == PCIOCGETCONF_OLD32) {
871 cio32->status = cio->status;
872 cio32->generation = cio->generation;
873 cio32->offset = cio->offset;
874 cio32->num_matches = cio->num_matches;
877 if (pattern_buf_old32 != NULL)
878 free(pattern_buf_old32, M_TEMP);
880 if (pattern_buf_old != NULL)
881 free(pattern_buf_old, M_TEMP);
883 if (pattern_buf != NULL)
884 free(pattern_buf, M_TEMP);
891 io_old = (struct pci_io_old *)data;
892 iodata.pi_sel.pc_domain = 0;
893 iodata.pi_sel.pc_bus = io_old->pi_sel.pc_bus;
894 iodata.pi_sel.pc_dev = io_old->pi_sel.pc_dev;
895 iodata.pi_sel.pc_func = io_old->pi_sel.pc_func;
896 iodata.pi_reg = io_old->pi_reg;
897 iodata.pi_width = io_old->pi_width;
898 iodata.pi_data = io_old->pi_data;
899 data = (caddr_t)&iodata;
904 io = (struct pci_io *)data;
905 switch(io->pi_width) {
909 /* Make sure register is not negative and aligned. */
910 if (io->pi_reg < 0 ||
911 io->pi_reg & (io->pi_width - 1)) {
916 * Assume that the user-level bus number is
917 * in fact the physical PCI bus number.
918 * Look up the grandparent, i.e. the bridge device,
919 * so that we can issue configuration space cycles.
921 pcidev = pci_find_dbsf(io->pi_sel.pc_domain,
922 io->pi_sel.pc_bus, io->pi_sel.pc_dev,
926 if (cmd == PCIOCWRITE || cmd == PCIOCWRITE_OLD)
928 if (cmd == PCIOCWRITE)
930 pci_write_config(pcidev,
935 else if (cmd == PCIOCREAD_OLD)
937 pci_read_config(pcidev,
943 pci_read_config(pcidev,
948 #ifdef COMPAT_FREEBSD4
949 if (cmd == PCIOCREAD_OLD) {
950 io_old->pi_data = -1;
964 bio = (struct pci_bar_io *)data;
967 * Assume that the user-level bus number is
968 * in fact the physical PCI bus number.
970 pcidev = pci_find_dbsf(bio->pbi_sel.pc_domain,
971 bio->pbi_sel.pc_bus, bio->pbi_sel.pc_dev,
972 bio->pbi_sel.pc_func);
973 if (pcidev == NULL) {
977 pm = pci_find_bar(pcidev, bio->pbi_reg);
982 bio->pbi_base = pm->pm_value;
983 bio->pbi_length = (pci_addr_t)1 << pm->pm_size;
984 bio->pbi_enabled = pci_bar_enabled(pcidev, pm);
989 io = (struct pci_io *)data;
990 pcidev = pci_find_dbsf(io->pi_sel.pc_domain, io->pi_sel.pc_bus,
991 io->pi_sel.pc_dev, io->pi_sel.pc_func);
993 io->pi_data = device_is_attached(pcidev);
998 lvio = (struct pci_list_vpd_io *)data;
1001 * Assume that the user-level bus number is
1002 * in fact the physical PCI bus number.
1004 pcidev = pci_find_dbsf(lvio->plvi_sel.pc_domain,
1005 lvio->plvi_sel.pc_bus, lvio->plvi_sel.pc_dev,
1006 lvio->plvi_sel.pc_func);
1007 if (pcidev == NULL) {
1011 error = pci_list_vpd(pcidev, lvio);