]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - sys/dev/pci/pci_user.c
MFC r282652:
[FreeBSD/stable/8.git] / sys / dev / pci / pci_user.c
1 /*-
2  * Copyright (c) 1997, Stefan Esser <se@freebsd.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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_bus.h"    /* XXX trim includes */
31 #include "opt_compat.h"
32
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>
39 #include <sys/conf.h>
40 #include <sys/kernel.h>
41 #include <sys/proc.h>
42 #include <sys/queue.h>
43 #include <sys/types.h>
44
45 #include <vm/vm.h>
46 #include <vm/pmap.h>
47 #include <vm/vm_extern.h>
48
49 #include <sys/bus.h>
50 #include <machine/bus.h>
51 #include <sys/rman.h>
52 #include <machine/resource.h>
53
54 #include <sys/pciio.h>
55 #include <dev/pci/pcireg.h>
56 #include <dev/pci/pcivar.h>
57
58 #include "pcib_if.h"
59 #include "pci_if.h"
60
61 /*
62  * This is the user interface to PCI configuration space.
63  */
64
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;
70
71 struct cdevsw pcicdev = {
72         .d_version =    D_VERSION,
73         .d_flags =      D_NEEDGIANT,
74         .d_open =       pci_open,
75         .d_close =      pci_close,
76         .d_ioctl =      pci_ioctl,
77         .d_name =       "pci",
78 };
79   
80 static int
81 pci_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
82 {
83         int error;
84
85         if (oflags & FWRITE) {
86                 error = securelevel_gt(td->td_ucred, 0);
87                 if (error)
88                         return (error);
89         }
90
91         return (0);
92 }
93
94 static int
95 pci_close(struct cdev *dev, int flag, int devtype, struct thread *td)
96 {
97         return 0;
98 }
99
100 /*
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.
106  */
107 static int
108 pci_conf_match(struct pci_match_conf *matches, int num_matches, 
109                struct pci_conf *match_buf)
110 {
111         int i;
112
113         if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0))
114                 return(1);
115
116         for (i = 0; i < num_matches; i++) {
117                 /*
118                  * I'm not sure why someone would do this...but...
119                  */
120                 if (matches[i].flags == PCI_GETCONF_NO_MATCH)
121                         continue;
122
123                 /*
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.
127                  */
128                 if (((matches[i].flags & PCI_GETCONF_MATCH_DOMAIN) != 0)
129                  && (match_buf->pc_sel.pc_domain !=
130                  matches[i].pc_sel.pc_domain))
131                         continue;
132
133                 if (((matches[i].flags & PCI_GETCONF_MATCH_BUS) != 0)
134                  && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus))
135                         continue;
136
137                 if (((matches[i].flags & PCI_GETCONF_MATCH_DEV) != 0)
138                  && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev))
139                         continue;
140
141                 if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC) != 0)
142                  && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func))
143                         continue;
144
145                 if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR) != 0) 
146                  && (match_buf->pc_vendor != matches[i].pc_vendor))
147                         continue;
148
149                 if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE) != 0)
150                  && (match_buf->pc_device != matches[i].pc_device))
151                         continue;
152
153                 if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS) != 0)
154                  && (match_buf->pc_class != matches[i].pc_class))
155                         continue;
156
157                 if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT) != 0)
158                  && (match_buf->pd_unit != matches[i].pd_unit))
159                         continue;
160
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))
164                         continue;
165
166                 return(0);
167         }
168
169         return(1);
170 }
171
172 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
173     defined(COMPAT_FREEBSD6)
174 #define PRE7_COMPAT
175
176 typedef enum {
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;
187
188 struct pcisel_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 */
192 };
193
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
199                                            card vendor */
200         u_int16_t       pc_vendor;      /* chip vendor ID */
201         u_int16_t       pc_device;      /* chip device ID, assigned by
202                                            chip vendor */
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 */
209 };
210
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 */
219 };
220
221 struct pci_io_old {
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 */
226 };
227
228 #define PCIOCGETCONF_OLD        _IOWR('p', 1, struct pci_conf_io)
229 #define PCIOCREAD_OLD           _IOWR('p', 2, struct pci_io_old)
230 #define PCIOCWRITE_OLD          _IOWR('p', 3, struct pci_io_old)
231
232 static int      pci_conf_match_old(struct pci_match_conf_old *matches,
233                     int num_matches, struct pci_conf *match_buf);
234
235 static int
236 pci_conf_match_old(struct pci_match_conf_old *matches, int num_matches,
237     struct pci_conf *match_buf)
238 {
239         int i;
240
241         if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0))
242                 return(1);
243
244         for (i = 0; i < num_matches; i++) {
245                 if (match_buf->pc_sel.pc_domain != 0)
246                         continue;
247
248                 /*
249                  * I'm not sure why someone would do this...but...
250                  */
251                 if (matches[i].flags == PCI_GETCONF_NO_MATCH_OLD)
252                         continue;
253
254                 /*
255                  * Look at each of the match flags.  If it's set, do the
256                  * comparison.  If the comparison fails, we don't have a
257                  * match, go on to the next item if there is one.
258                  */
259                 if (((matches[i].flags & PCI_GETCONF_MATCH_BUS_OLD) != 0)
260                  && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus))
261                         continue;
262
263                 if (((matches[i].flags & PCI_GETCONF_MATCH_DEV_OLD) != 0)
264                  && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev))
265                         continue;
266
267                 if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC_OLD) != 0)
268                  && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func))
269                         continue;
270
271                 if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR_OLD) != 0)
272                  && (match_buf->pc_vendor != matches[i].pc_vendor))
273                         continue;
274
275                 if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE_OLD) != 0)
276                  && (match_buf->pc_device != matches[i].pc_device))
277                         continue;
278
279                 if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS_OLD) != 0)
280                  && (match_buf->pc_class != matches[i].pc_class))
281                         continue;
282
283                 if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT_OLD) != 0)
284                  && (match_buf->pd_unit != matches[i].pd_unit))
285                         continue;
286
287                 if (((matches[i].flags & PCI_GETCONF_MATCH_NAME_OLD) != 0)
288                  && (strncmp(matches[i].pd_name, match_buf->pd_name,
289                              sizeof(match_buf->pd_name)) != 0))
290                         continue;
291
292                 return(0);
293         }
294
295         return(1);
296 }
297
298 #endif
299
300 static int
301 pci_list_vpd(device_t dev, struct pci_list_vpd_io *lvio)
302 {
303         struct pci_vpd_element vpd_element, *vpd_user;
304         struct pcicfg_vpd *vpd;
305         size_t len;
306         int error, i;
307
308         vpd = pci_fetch_vpd_list(dev);
309         if (vpd->vpd_reg == 0 || vpd->vpd_ident == NULL)
310                 return (ENXIO);
311
312         /*
313          * Calculate the amount of space needed in the data buffer.  An
314          * identifier element is always present followed by the read-only
315          * and read-write keywords.
316          */
317         len = sizeof(struct pci_vpd_element) + strlen(vpd->vpd_ident);
318         for (i = 0; i < vpd->vpd_rocnt; i++)
319                 len += sizeof(struct pci_vpd_element) + vpd->vpd_ros[i].len;
320         for (i = 0; i < vpd->vpd_wcnt; i++)
321                 len += sizeof(struct pci_vpd_element) + vpd->vpd_w[i].len;
322
323         if (lvio->plvi_len == 0) {
324                 lvio->plvi_len = len;
325                 return (0);
326         }
327         if (lvio->plvi_len < len) {
328                 lvio->plvi_len = len;
329                 return (ENOMEM);
330         }
331
332         /*
333          * Copyout the identifier string followed by each keyword and
334          * value.
335          */
336         vpd_user = lvio->plvi_data;
337         vpd_element.pve_keyword[0] = '\0';
338         vpd_element.pve_keyword[1] = '\0';
339         vpd_element.pve_flags = PVE_FLAG_IDENT;
340         vpd_element.pve_datalen = strlen(vpd->vpd_ident);
341         error = copyout(&vpd_element, vpd_user, sizeof(vpd_element));
342         if (error)
343                 return (error);
344         error = copyout(vpd->vpd_ident, vpd_user->pve_data,
345             strlen(vpd->vpd_ident));
346         if (error)
347                 return (error);
348         vpd_user = PVE_NEXT(vpd_user);
349         vpd_element.pve_flags = 0;
350         for (i = 0; i < vpd->vpd_rocnt; i++) {
351                 vpd_element.pve_keyword[0] = vpd->vpd_ros[i].keyword[0];
352                 vpd_element.pve_keyword[1] = vpd->vpd_ros[i].keyword[1];
353                 vpd_element.pve_datalen = vpd->vpd_ros[i].len;
354                 error = copyout(&vpd_element, vpd_user, sizeof(vpd_element));
355                 if (error)
356                         return (error);
357                 error = copyout(vpd->vpd_ros[i].value, vpd_user->pve_data,
358                     vpd->vpd_ros[i].len);
359                 if (error)
360                         return (error);
361                 vpd_user = PVE_NEXT(vpd_user);
362         }
363         vpd_element.pve_flags = PVE_FLAG_RW;
364         for (i = 0; i < vpd->vpd_wcnt; i++) {
365                 vpd_element.pve_keyword[0] = vpd->vpd_w[i].keyword[0];
366                 vpd_element.pve_keyword[1] = vpd->vpd_w[i].keyword[1];
367                 vpd_element.pve_datalen = vpd->vpd_w[i].len;
368                 error = copyout(&vpd_element, vpd_user, sizeof(vpd_element));
369                 if (error)
370                         return (error);
371                 error = copyout(vpd->vpd_w[i].value, vpd_user->pve_data,
372                     vpd->vpd_w[i].len);
373                 if (error)
374                         return (error);
375                 vpd_user = PVE_NEXT(vpd_user);
376         }
377         KASSERT((char *)vpd_user - (char *)lvio->plvi_data == len,
378             ("length mismatch"));
379         lvio->plvi_len = len;
380         return (0);
381 }
382
383 static int
384 pci_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
385 {
386         device_t pcidev, brdev;
387         void *confdata;
388         const char *name;
389         struct devlist *devlist_head;
390         struct pci_conf_io *cio;
391         struct pci_devinfo *dinfo;
392         struct pci_io *io;
393         struct pci_bar_io *bio;
394         struct pci_list_vpd_io *lvio;
395         struct pci_match_conf *pattern_buf;
396         struct pci_map *pm;
397         size_t confsz, iolen, pbufsz;
398         int error, ionum, i, num_patterns;
399 #ifdef PRE7_COMPAT
400         struct pci_conf_old conf_old;
401         struct pci_io iodata;
402         struct pci_io_old *io_old;
403         struct pci_match_conf_old *pattern_buf_old;
404
405         io_old = NULL;
406         pattern_buf_old = NULL;
407 #endif
408
409         if (!(flag & FWRITE)) {
410                 switch (cmd) {
411 #ifdef PRE7_COMPAT
412                 case PCIOCGETCONF_OLD:
413 #endif
414                 case PCIOCGETCONF:
415                 case PCIOCGETBAR:
416                 case PCIOCLISTVPD:
417                         break;
418                 default:
419                         return (EPERM);
420                 }
421         }
422
423         switch (cmd) {
424 #ifdef PRE7_COMPAT
425         case PCIOCGETCONF_OLD:
426                 /* FALLTHROUGH */
427 #endif
428         case PCIOCGETCONF:
429                 cio = (struct pci_conf_io *)data;
430
431                 pattern_buf = NULL;
432                 num_patterns = 0;
433                 dinfo = NULL;
434
435                 cio->num_matches = 0;
436
437                 /*
438                  * If the user specified an offset into the device list,
439                  * but the list has changed since they last called this
440                  * ioctl, tell them that the list has changed.  They will
441                  * have to get the list from the beginning.
442                  */
443                 if ((cio->offset != 0)
444                  && (cio->generation != pci_generation)){
445                         cio->status = PCI_GETCONF_LIST_CHANGED;
446                         error = 0;
447                         break;
448                 }
449
450                 /*
451                  * Check to see whether the user has asked for an offset
452                  * past the end of our list.
453                  */
454                 if (cio->offset >= pci_numdevs) {
455                         cio->status = PCI_GETCONF_LAST_DEVICE;
456                         error = 0;
457                         break;
458                 }
459
460                 /* get the head of the device queue */
461                 devlist_head = &pci_devq;
462
463                 /*
464                  * Determine how much room we have for pci_conf structures.
465                  * Round the user's buffer size down to the nearest
466                  * multiple of sizeof(struct pci_conf) in case the user
467                  * didn't specify a multiple of that size.
468                  */
469 #ifdef PRE7_COMPAT
470                 if (cmd == PCIOCGETCONF_OLD)
471                         confsz = sizeof(struct pci_conf_old);
472                 else
473 #endif
474                         confsz = sizeof(struct pci_conf);
475                 iolen = min(cio->match_buf_len - (cio->match_buf_len % confsz),
476                     pci_numdevs * confsz);
477
478                 /*
479                  * Since we know that iolen is a multiple of the size of
480                  * the pciconf union, it's okay to do this.
481                  */
482                 ionum = iolen / confsz;
483
484                 /*
485                  * If this test is true, the user wants the pci_conf
486                  * structures returned to match the supplied entries.
487                  */
488                 if ((cio->num_patterns > 0) && (cio->num_patterns < pci_numdevs)
489                  && (cio->pat_buf_len > 0)) {
490                         /*
491                          * pat_buf_len needs to be:
492                          * num_patterns * sizeof(struct pci_match_conf)
493                          * While it is certainly possible the user just
494                          * allocated a large buffer, but set the number of
495                          * matches correctly, it is far more likely that
496                          * their kernel doesn't match the userland utility
497                          * they're using.  It's also possible that the user
498                          * forgot to initialize some variables.  Yes, this
499                          * may be overly picky, but I hazard to guess that
500                          * it's far more likely to just catch folks that
501                          * updated their kernel but not their userland.
502                          */
503 #ifdef PRE7_COMPAT
504                         if (cmd == PCIOCGETCONF_OLD)
505                                 pbufsz = sizeof(struct pci_match_conf_old);
506                         else
507 #endif
508                                 pbufsz = sizeof(struct pci_match_conf);
509                         if (cio->num_patterns * pbufsz != cio->pat_buf_len) {
510                                 /* The user made a mistake, return an error. */
511                                 cio->status = PCI_GETCONF_ERROR;
512                                 error = EINVAL;
513                                 break;
514                         }
515
516                         /*
517                          * Allocate a buffer to hold the patterns.
518                          */
519 #ifdef PRE7_COMPAT
520                         if (cmd == PCIOCGETCONF_OLD) {
521                                 pattern_buf_old = malloc(cio->pat_buf_len,
522                                     M_TEMP, M_WAITOK);
523                                 error = copyin(cio->patterns,
524                                     pattern_buf_old, cio->pat_buf_len);
525                         } else
526 #endif
527                         {
528                                 pattern_buf = malloc(cio->pat_buf_len, M_TEMP,
529                                     M_WAITOK);
530                                 error = copyin(cio->patterns, pattern_buf,
531                                     cio->pat_buf_len);
532                         }
533                         if (error != 0) {
534                                 error = EINVAL;
535                                 goto getconfexit;
536                         }
537                         num_patterns = cio->num_patterns;
538                 } else if ((cio->num_patterns > 0)
539                         || (cio->pat_buf_len > 0)) {
540                         /*
541                          * The user made a mistake, spit out an error.
542                          */
543                         cio->status = PCI_GETCONF_ERROR;
544                         error = EINVAL;
545                         break;
546                 }
547
548                 /*
549                  * Go through the list of devices and copy out the devices
550                  * that match the user's criteria.
551                  */
552                 for (cio->num_matches = 0, error = 0, i = 0,
553                      dinfo = STAILQ_FIRST(devlist_head);
554                      (dinfo != NULL) && (cio->num_matches < ionum)
555                      && (error == 0) && (i < pci_numdevs) && (dinfo != NULL);
556                      dinfo = STAILQ_NEXT(dinfo, pci_links), i++) {
557
558                         if (i < cio->offset)
559                                 continue;
560
561                         /* Populate pd_name and pd_unit */
562                         name = NULL;
563                         if (dinfo->cfg.dev)
564                                 name = device_get_name(dinfo->cfg.dev);
565                         if (name) {
566                                 strncpy(dinfo->conf.pd_name, name,
567                                         sizeof(dinfo->conf.pd_name));
568                                 dinfo->conf.pd_name[PCI_MAXNAMELEN] = 0;
569                                 dinfo->conf.pd_unit =
570                                         device_get_unit(dinfo->cfg.dev);
571                         } else {
572                                 dinfo->conf.pd_name[0] = '\0';
573                                 dinfo->conf.pd_unit = 0;
574                         }
575
576 #ifdef PRE7_COMPAT
577                         if ((cmd == PCIOCGETCONF_OLD &&
578                             (pattern_buf_old == NULL ||
579                             pci_conf_match_old(pattern_buf_old, num_patterns,
580                             &dinfo->conf) == 0)) ||
581                             (cmd == PCIOCGETCONF &&
582                             (pattern_buf == NULL ||
583                             pci_conf_match(pattern_buf, num_patterns,
584                             &dinfo->conf) == 0))) {
585 #else
586                         if (pattern_buf == NULL ||
587                             pci_conf_match(pattern_buf, num_patterns,
588                             &dinfo->conf) == 0) {
589 #endif
590                                 /*
591                                  * If we've filled up the user's buffer,
592                                  * break out at this point.  Since we've
593                                  * got a match here, we'll pick right back
594                                  * up at the matching entry.  We can also
595                                  * tell the user that there are more matches
596                                  * left.
597                                  */
598                                 if (cio->num_matches >= ionum)
599                                         break;
600
601 #ifdef PRE7_COMPAT
602                                 if (cmd == PCIOCGETCONF_OLD) {
603                                         conf_old.pc_sel.pc_bus =
604                                             dinfo->conf.pc_sel.pc_bus;
605                                         conf_old.pc_sel.pc_dev =
606                                             dinfo->conf.pc_sel.pc_dev;
607                                         conf_old.pc_sel.pc_func =
608                                             dinfo->conf.pc_sel.pc_func;
609                                         conf_old.pc_hdr = dinfo->conf.pc_hdr;
610                                         conf_old.pc_subvendor =
611                                             dinfo->conf.pc_subvendor;
612                                         conf_old.pc_subdevice =
613                                             dinfo->conf.pc_subdevice;
614                                         conf_old.pc_vendor =
615                                             dinfo->conf.pc_vendor;
616                                         conf_old.pc_device =
617                                             dinfo->conf.pc_device;
618                                         conf_old.pc_class =
619                                             dinfo->conf.pc_class;
620                                         conf_old.pc_subclass =
621                                             dinfo->conf.pc_subclass;
622                                         conf_old.pc_progif =
623                                             dinfo->conf.pc_progif;
624                                         conf_old.pc_revid =
625                                             dinfo->conf.pc_revid;
626                                         strncpy(conf_old.pd_name,
627                                             dinfo->conf.pd_name,
628                                             sizeof(conf_old.pd_name));
629                                         conf_old.pd_name[PCI_MAXNAMELEN] = 0;
630                                         conf_old.pd_unit =
631                                             dinfo->conf.pd_unit;
632                                         confdata = &conf_old;
633                                 } else
634 #endif
635                                         confdata = &dinfo->conf;
636                                 /* Only if we can copy it out do we count it. */
637                                 if (!(error = copyout(confdata,
638                                     (caddr_t)cio->matches +
639                                     confsz * cio->num_matches, confsz)))
640                                         cio->num_matches++;
641                         }
642                 }
643
644                 /*
645                  * Set the pointer into the list, so if the user is getting
646                  * n records at a time, where n < pci_numdevs,
647                  */
648                 cio->offset = i;
649
650                 /*
651                  * Set the generation, the user will need this if they make
652                  * another ioctl call with offset != 0.
653                  */
654                 cio->generation = pci_generation;
655
656                 /*
657                  * If this is the last device, inform the user so he won't
658                  * bother asking for more devices.  If dinfo isn't NULL, we
659                  * know that there are more matches in the list because of
660                  * the way the traversal is done.
661                  */
662                 if (dinfo == NULL)
663                         cio->status = PCI_GETCONF_LAST_DEVICE;
664                 else
665                         cio->status = PCI_GETCONF_MORE_DEVS;
666
667 getconfexit:
668                 if (pattern_buf != NULL)
669                         free(pattern_buf, M_TEMP);
670 #ifdef PRE7_COMPAT
671                 if (pattern_buf_old != NULL)
672                         free(pattern_buf_old, M_TEMP);
673 #endif
674
675                 break;
676
677 #ifdef PRE7_COMPAT
678         case PCIOCREAD_OLD:
679         case PCIOCWRITE_OLD:
680                 io_old = (struct pci_io_old *)data;
681                 iodata.pi_sel.pc_domain = 0;
682                 iodata.pi_sel.pc_bus = io_old->pi_sel.pc_bus;
683                 iodata.pi_sel.pc_dev = io_old->pi_sel.pc_dev;
684                 iodata.pi_sel.pc_func = io_old->pi_sel.pc_func;
685                 iodata.pi_reg = io_old->pi_reg;
686                 iodata.pi_width = io_old->pi_width;
687                 iodata.pi_data = io_old->pi_data;
688                 data = (caddr_t)&iodata;
689                 /* FALLTHROUGH */
690 #endif
691         case PCIOCREAD:
692         case PCIOCWRITE:
693                 io = (struct pci_io *)data;
694                 switch(io->pi_width) {
695                 case 4:
696                 case 2:
697                 case 1:
698                         /* Make sure register is not negative and aligned. */
699                         if (io->pi_reg < 0 ||
700                             io->pi_reg & (io->pi_width - 1)) {
701                                 error = EINVAL;
702                                 break;
703                         }
704                         /*
705                          * Assume that the user-level bus number is
706                          * in fact the physical PCI bus number.
707                          * Look up the grandparent, i.e. the bridge device,
708                          * so that we can issue configuration space cycles.
709                          */
710                         pcidev = pci_find_dbsf(io->pi_sel.pc_domain,
711                             io->pi_sel.pc_bus, io->pi_sel.pc_dev,
712                             io->pi_sel.pc_func);
713                         if (pcidev) {
714                                 brdev = device_get_parent(
715                                     device_get_parent(pcidev));
716
717 #ifdef PRE7_COMPAT
718                                 if (cmd == PCIOCWRITE || cmd == PCIOCWRITE_OLD)
719 #else
720                                 if (cmd == PCIOCWRITE)
721 #endif
722                                         PCIB_WRITE_CONFIG(brdev,
723                                                           io->pi_sel.pc_bus,
724                                                           io->pi_sel.pc_dev,
725                                                           io->pi_sel.pc_func,
726                                                           io->pi_reg,
727                                                           io->pi_data,
728                                                           io->pi_width);
729 #ifdef PRE7_COMPAT
730                                 else if (cmd == PCIOCREAD_OLD)
731                                         io_old->pi_data =
732                                                 PCIB_READ_CONFIG(brdev,
733                                                           io->pi_sel.pc_bus,
734                                                           io->pi_sel.pc_dev,
735                                                           io->pi_sel.pc_func,
736                                                           io->pi_reg,
737                                                           io->pi_width);
738 #endif
739                                 else
740                                         io->pi_data =
741                                                 PCIB_READ_CONFIG(brdev,
742                                                           io->pi_sel.pc_bus,
743                                                           io->pi_sel.pc_dev,
744                                                           io->pi_sel.pc_func,
745                                                           io->pi_reg,
746                                                           io->pi_width);
747                                 error = 0;
748                         } else {
749 #ifdef COMPAT_FREEBSD4
750                                 if (cmd == PCIOCREAD_OLD) {
751                                         io_old->pi_data = -1;
752                                         error = 0;
753                                 } else
754 #endif
755                                         error = ENODEV;
756                         }
757                         break;
758                 default:
759                         error = EINVAL;
760                         break;
761                 }
762                 break;
763
764         case PCIOCGETBAR:
765                 bio = (struct pci_bar_io *)data;
766
767                 /*
768                  * Assume that the user-level bus number is
769                  * in fact the physical PCI bus number.
770                  */
771                 pcidev = pci_find_dbsf(bio->pbi_sel.pc_domain,
772                     bio->pbi_sel.pc_bus, bio->pbi_sel.pc_dev,
773                     bio->pbi_sel.pc_func);
774                 if (pcidev == NULL) {
775                         error = ENODEV;
776                         break;
777                 }
778                 pm = pci_find_bar(pcidev, bio->pbi_reg);
779                 if (pm == NULL) {
780                         error = EINVAL;
781                         break;
782                 }
783                 bio->pbi_base = pm->pm_value;
784                 bio->pbi_length = (pci_addr_t)1 << pm->pm_size;
785                 bio->pbi_enabled = pci_bar_enabled(pcidev, pm);
786                 error = 0;
787                 break;
788         case PCIOCLISTVPD:
789                 lvio = (struct pci_list_vpd_io *)data;
790
791                 /*
792                  * Assume that the user-level bus number is
793                  * in fact the physical PCI bus number.
794                  */
795                 pcidev = pci_find_dbsf(lvio->plvi_sel.pc_domain,
796                     lvio->plvi_sel.pc_bus, lvio->plvi_sel.pc_dev,
797                     lvio->plvi_sel.pc_func);
798                 if (pcidev == NULL) {
799                         error = ENODEV;
800                         break;
801                 }
802                 error = pci_list_vpd(pcidev, lvio);
803                 break;
804         default:
805                 error = ENOTTY;
806                 break;
807         }
808
809         return (error);
810 }