]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/dev/pci/pci_user.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.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 #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
234                                            card vendor */
235         uint16_t        pc_vendor;      /* chip vendor ID */
236         uint16_t        pc_device;      /* chip device ID, assigned by
237                                            chip vendor */
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) */
244 };
245
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 */
254 };
255
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 */
268 };
269
270 #define PCIOCGETCONF_OLD32      _IOWR('p', 1, struct pci_conf_io32)
271 #endif  /* COMPAT_FREEBSD32 */
272
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)
276
277 static int      pci_conf_match_old(struct pci_match_conf_old *matches,
278                     int num_matches, struct pci_conf *match_buf);
279
280 static int
281 pci_conf_match_old(struct pci_match_conf_old *matches, int num_matches,
282     struct pci_conf *match_buf)
283 {
284         int i;
285
286         if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0))
287                 return(1);
288
289         for (i = 0; i < num_matches; i++) {
290                 if (match_buf->pc_sel.pc_domain != 0)
291                         continue;
292
293                 /*
294                  * I'm not sure why someone would do this...but...
295                  */
296                 if (matches[i].flags == PCI_GETCONF_NO_MATCH_OLD)
297                         continue;
298
299                 /*
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.
303                  */
304                 if (((matches[i].flags & PCI_GETCONF_MATCH_BUS_OLD) != 0)
305                  && (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus))
306                         continue;
307
308                 if (((matches[i].flags & PCI_GETCONF_MATCH_DEV_OLD) != 0)
309                  && (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev))
310                         continue;
311
312                 if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC_OLD) != 0)
313                  && (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func))
314                         continue;
315
316                 if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR_OLD) != 0)
317                  && (match_buf->pc_vendor != matches[i].pc_vendor))
318                         continue;
319
320                 if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE_OLD) != 0)
321                  && (match_buf->pc_device != matches[i].pc_device))
322                         continue;
323
324                 if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS_OLD) != 0)
325                  && (match_buf->pc_class != matches[i].pc_class))
326                         continue;
327
328                 if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT_OLD) != 0)
329                  && (match_buf->pd_unit != matches[i].pd_unit))
330                         continue;
331
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))
335                         continue;
336
337                 return(0);
338         }
339
340         return(1);
341 }
342
343 #ifdef COMPAT_FREEBSD32
344 static int
345 pci_conf_match_old32(struct pci_match_conf_old32 *matches, int num_matches,
346     struct pci_conf *match_buf)
347 {
348         int i;
349
350         if ((matches == NULL) || (match_buf == NULL) || (num_matches <= 0))
351                 return(1);
352
353         for (i = 0; i < num_matches; i++) {
354                 if (match_buf->pc_sel.pc_domain != 0)
355                         continue;
356
357                 /*
358                  * I'm not sure why someone would do this...but...
359                  */
360                 if (matches[i].flags == PCI_GETCONF_NO_MATCH_OLD)
361                         continue;
362
363                 /*
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.
367                  */
368                 if (((matches[i].flags & PCI_GETCONF_MATCH_BUS_OLD) != 0) &&
369                     (match_buf->pc_sel.pc_bus != matches[i].pc_sel.pc_bus))
370                         continue;
371
372                 if (((matches[i].flags & PCI_GETCONF_MATCH_DEV_OLD) != 0) &&
373                     (match_buf->pc_sel.pc_dev != matches[i].pc_sel.pc_dev))
374                         continue;
375
376                 if (((matches[i].flags & PCI_GETCONF_MATCH_FUNC_OLD) != 0) &&
377                     (match_buf->pc_sel.pc_func != matches[i].pc_sel.pc_func))
378                         continue;
379
380                 if (((matches[i].flags & PCI_GETCONF_MATCH_VENDOR_OLD) != 0) &&
381                     (match_buf->pc_vendor != matches[i].pc_vendor))
382                         continue;
383
384                 if (((matches[i].flags & PCI_GETCONF_MATCH_DEVICE_OLD) != 0) &&
385                     (match_buf->pc_device != matches[i].pc_device))
386                         continue;
387
388                 if (((matches[i].flags & PCI_GETCONF_MATCH_CLASS_OLD) != 0) &&
389                     (match_buf->pc_class != matches[i].pc_class))
390                         continue;
391
392                 if (((matches[i].flags & PCI_GETCONF_MATCH_UNIT_OLD) != 0) &&
393                     ((u_int32_t)match_buf->pd_unit != matches[i].pd_unit))
394                         continue;
395
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))
399                         continue;
400
401                 return (0);
402         }
403
404         return (1);
405 }
406 #endif  /* COMPAT_FREEBSD32 */
407 #endif  /* PRE7_COMPAT */
408
409 static int
410 pci_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
411 {
412         device_t pcidev, brdev;
413         void *confdata;
414         const char *name;
415         struct devlist *devlist_head;
416         struct pci_conf_io *cio = NULL;
417         struct pci_devinfo *dinfo;
418         struct pci_io *io;
419         struct pci_bar_io *bio;
420         struct pci_match_conf *pattern_buf;
421         struct pci_map *pm;
422         size_t confsz, iolen, pbufsz;
423         int error, ionum, i, num_patterns;
424 #ifdef PRE7_COMPAT
425 #ifdef COMPAT_FREEBSD32
426         struct pci_conf_io32 *cio32 = NULL;
427         struct pci_conf_old32 conf_old32;
428         struct pci_match_conf_old32 *pattern_buf_old32 = NULL;
429 #endif
430         struct pci_conf_old conf_old;
431         struct pci_io iodata;
432         struct pci_io_old *io_old;
433         struct pci_match_conf_old *pattern_buf_old = NULL;
434
435         io_old = NULL;
436
437         if (!(flag & FWRITE) && cmd != PCIOCGETBAR &&
438             cmd != PCIOCGETCONF && cmd != PCIOCGETCONF_OLD)
439                 return EPERM;
440 #else
441         if (!(flag & FWRITE) && cmd != PCIOCGETBAR && cmd != PCIOCGETCONF)
442                 return EPERM;
443 #endif
444
445         switch(cmd) {
446 #ifdef PRE7_COMPAT
447 #ifdef COMPAT_FREEBSD32
448        case PCIOCGETCONF_OLD32:
449                cio32 = (struct pci_conf_io32 *)data;
450                cio = malloc(sizeof(struct pci_conf_io), M_TEMP, M_WAITOK);
451                cio->pat_buf_len = cio32->pat_buf_len;
452                cio->num_patterns = cio32->num_patterns;
453                cio->patterns = (void *)(uintptr_t)cio32->patterns;
454                cio->match_buf_len = cio32->match_buf_len;
455                cio->num_matches = cio32->num_matches;
456                cio->matches = (void *)(uintptr_t)cio32->matches;
457                cio->offset = cio32->offset;
458                cio->generation = cio32->generation;
459                cio->status = cio32->status;
460                cio32->num_matches = 0;
461                break;
462 #endif
463         case PCIOCGETCONF_OLD:
464 #endif
465         case PCIOCGETCONF:
466                 cio = (struct pci_conf_io *)data;
467         }
468
469         switch(cmd) {
470 #ifdef PRE7_COMPAT
471 #ifdef COMPAT_FREEBSD32
472         case PCIOCGETCONF_OLD32:
473 #endif
474         case PCIOCGETCONF_OLD:
475 #endif
476         case PCIOCGETCONF:
477
478                 pattern_buf = NULL;
479                 num_patterns = 0;
480                 dinfo = NULL;
481
482                 cio->num_matches = 0;
483
484                 /*
485                  * If the user specified an offset into the device list,
486                  * but the list has changed since they last called this
487                  * ioctl, tell them that the list has changed.  They will
488                  * have to get the list from the beginning.
489                  */
490                 if ((cio->offset != 0)
491                  && (cio->generation != pci_generation)){
492                         cio->status = PCI_GETCONF_LIST_CHANGED;
493                         error = 0;
494                         goto getconfexit;
495                 }
496
497                 /*
498                  * Check to see whether the user has asked for an offset
499                  * past the end of our list.
500                  */
501                 if (cio->offset >= pci_numdevs) {
502                         cio->status = PCI_GETCONF_LAST_DEVICE;
503                         error = 0;
504                         goto getconfexit;
505                 }
506
507                 /* get the head of the device queue */
508                 devlist_head = &pci_devq;
509
510                 /*
511                  * Determine how much room we have for pci_conf structures.
512                  * Round the user's buffer size down to the nearest
513                  * multiple of sizeof(struct pci_conf) in case the user
514                  * didn't specify a multiple of that size.
515                  */
516 #ifdef PRE7_COMPAT
517 #ifdef COMPAT_FREEBSD32
518                 if (cmd == PCIOCGETCONF_OLD32)
519                         confsz = sizeof(struct pci_conf_old32);
520                 else
521 #endif
522                 if (cmd == PCIOCGETCONF_OLD)
523                         confsz = sizeof(struct pci_conf_old);
524                 else
525 #endif
526                         confsz = sizeof(struct pci_conf);
527                 iolen = min(cio->match_buf_len - (cio->match_buf_len % confsz),
528                     pci_numdevs * confsz);
529
530                 /*
531                  * Since we know that iolen is a multiple of the size of
532                  * the pciconf union, it's okay to do this.
533                  */
534                 ionum = iolen / confsz;
535
536                 /*
537                  * If this test is true, the user wants the pci_conf
538                  * structures returned to match the supplied entries.
539                  */
540                 if ((cio->num_patterns > 0) && (cio->num_patterns < pci_numdevs)
541                  && (cio->pat_buf_len > 0)) {
542                         /*
543                          * pat_buf_len needs to be:
544                          * num_patterns * sizeof(struct pci_match_conf)
545                          * While it is certainly possible the user just
546                          * allocated a large buffer, but set the number of
547                          * matches correctly, it is far more likely that
548                          * their kernel doesn't match the userland utility
549                          * they're using.  It's also possible that the user
550                          * forgot to initialize some variables.  Yes, this
551                          * may be overly picky, but I hazard to guess that
552                          * it's far more likely to just catch folks that
553                          * updated their kernel but not their userland.
554                          */
555 #ifdef PRE7_COMPAT
556 #ifdef COMPAT_FREEBSD32
557                         if (cmd == PCIOCGETCONF_OLD32)
558                                 pbufsz = sizeof(struct pci_match_conf_old32);
559                         else
560 #endif
561                         if (cmd == PCIOCGETCONF_OLD)
562                                 pbufsz = sizeof(struct pci_match_conf_old);
563                         else
564 #endif
565                                 pbufsz = sizeof(struct pci_match_conf);
566                         if (cio->num_patterns * pbufsz != cio->pat_buf_len) {
567                                 /* The user made a mistake, return an error. */
568                                 cio->status = PCI_GETCONF_ERROR;
569                                 error = EINVAL;
570                                 goto getconfexit;
571                         }
572
573                         /*
574                          * Allocate a buffer to hold the patterns.
575                          */
576 #ifdef PRE7_COMPAT
577 #ifdef COMPAT_FREEBSD32
578                         if (cmd == PCIOCGETCONF_OLD32) {
579                                 pattern_buf_old32 = malloc(cio->pat_buf_len,
580                                     M_TEMP, M_WAITOK);
581                                 error = copyin(cio->patterns,
582                                     pattern_buf_old32, cio->pat_buf_len);
583                         } else
584 #endif /* COMPAT_FREEBSD32 */
585                         if (cmd == PCIOCGETCONF_OLD) {
586                                 pattern_buf_old = malloc(cio->pat_buf_len,
587                                     M_TEMP, M_WAITOK);
588                                 error = copyin(cio->patterns,
589                                     pattern_buf_old, cio->pat_buf_len);
590                         } else
591 #endif /* PRE7_COMPAT */
592                         {
593                                 pattern_buf = malloc(cio->pat_buf_len, M_TEMP,
594                                     M_WAITOK);
595                                 error = copyin(cio->patterns, pattern_buf,
596                                     cio->pat_buf_len);
597                         }
598                         if (error != 0) {
599                                 error = EINVAL;
600                                 goto getconfexit;
601                         }
602                         num_patterns = cio->num_patterns;
603                 } else if ((cio->num_patterns > 0)
604                         || (cio->pat_buf_len > 0)) {
605                         /*
606                          * The user made a mistake, spit out an error.
607                          */
608                         cio->status = PCI_GETCONF_ERROR;
609                         error = EINVAL;
610                        goto getconfexit;
611                 }
612
613                 /*
614                  * Go through the list of devices and copy out the devices
615                  * that match the user's criteria.
616                  */
617                 for (cio->num_matches = 0, error = 0, i = 0,
618                      dinfo = STAILQ_FIRST(devlist_head);
619                      (dinfo != NULL) && (cio->num_matches < ionum)
620                      && (error == 0) && (i < pci_numdevs) && (dinfo != NULL);
621                      dinfo = STAILQ_NEXT(dinfo, pci_links), i++) {
622
623                         if (i < cio->offset)
624                                 continue;
625
626                         /* Populate pd_name and pd_unit */
627                         name = NULL;
628                         if (dinfo->cfg.dev)
629                                 name = device_get_name(dinfo->cfg.dev);
630                         if (name) {
631                                 strncpy(dinfo->conf.pd_name, name,
632                                         sizeof(dinfo->conf.pd_name));
633                                 dinfo->conf.pd_name[PCI_MAXNAMELEN] = 0;
634                                 dinfo->conf.pd_unit =
635                                         device_get_unit(dinfo->cfg.dev);
636                         } else {
637                                 dinfo->conf.pd_name[0] = '\0';
638                                 dinfo->conf.pd_unit = 0;
639                         }
640
641 #ifdef PRE7_COMPAT
642                         if (
643 #ifdef COMPAT_FREEBSD32
644                             (cmd == PCIOCGETCONF_OLD32 &&
645                             (pattern_buf_old32 == NULL ||
646                             pci_conf_match_old32(pattern_buf_old32,
647                             num_patterns, &dinfo->conf) == 0)) ||
648 #endif
649                             (cmd == PCIOCGETCONF_OLD &&
650                             (pattern_buf_old == NULL ||
651                             pci_conf_match_old(pattern_buf_old, num_patterns,
652                             &dinfo->conf) == 0)) ||
653                             (cmd == PCIOCGETCONF &&
654                             (pattern_buf == NULL ||
655                             pci_conf_match(pattern_buf, num_patterns,
656                             &dinfo->conf) == 0))) {
657 #else
658                         if (pattern_buf == NULL ||
659                             pci_conf_match(pattern_buf, num_patterns,
660                             &dinfo->conf) == 0) {
661 #endif
662                                 /*
663                                  * If we've filled up the user's buffer,
664                                  * break out at this point.  Since we've
665                                  * got a match here, we'll pick right back
666                                  * up at the matching entry.  We can also
667                                  * tell the user that there are more matches
668                                  * left.
669                                  */
670                                 if (cio->num_matches >= ionum)
671                                         break;
672
673 #ifdef PRE7_COMPAT
674 #ifdef COMPAT_FREEBSD32
675                                 if (cmd == PCIOCGETCONF_OLD32) {
676                                         conf_old32.pc_sel.pc_bus =
677                                             dinfo->conf.pc_sel.pc_bus;
678                                         conf_old32.pc_sel.pc_dev =
679                                             dinfo->conf.pc_sel.pc_dev;
680                                         conf_old32.pc_sel.pc_func =
681                                             dinfo->conf.pc_sel.pc_func;
682                                         conf_old32.pc_hdr = dinfo->conf.pc_hdr;
683                                         conf_old32.pc_subvendor =
684                                             dinfo->conf.pc_subvendor;
685                                         conf_old32.pc_subdevice =
686                                             dinfo->conf.pc_subdevice;
687                                         conf_old32.pc_vendor =
688                                             dinfo->conf.pc_vendor;
689                                         conf_old32.pc_device =
690                                             dinfo->conf.pc_device;
691                                         conf_old32.pc_class =
692                                             dinfo->conf.pc_class;
693                                         conf_old32.pc_subclass =
694                                             dinfo->conf.pc_subclass;
695                                         conf_old32.pc_progif =
696                                             dinfo->conf.pc_progif;
697                                         conf_old32.pc_revid =
698                                             dinfo->conf.pc_revid;
699                                         strncpy(conf_old32.pd_name,
700                                             dinfo->conf.pd_name,
701                                             sizeof(conf_old32.pd_name));
702                                         conf_old32.pd_name[PCI_MAXNAMELEN] = 0;
703                                         conf_old32.pd_unit =
704                                             (uint32_t)dinfo->conf.pd_unit;
705                                         confdata = &conf_old32;
706                                 } else
707 #endif /* COMPAT_FREEBSD32 */
708                                 if (cmd == PCIOCGETCONF_OLD) {
709                                         conf_old.pc_sel.pc_bus =
710                                             dinfo->conf.pc_sel.pc_bus;
711                                         conf_old.pc_sel.pc_dev =
712                                             dinfo->conf.pc_sel.pc_dev;
713                                         conf_old.pc_sel.pc_func =
714                                             dinfo->conf.pc_sel.pc_func;
715                                         conf_old.pc_hdr = dinfo->conf.pc_hdr;
716                                         conf_old.pc_subvendor =
717                                             dinfo->conf.pc_subvendor;
718                                         conf_old.pc_subdevice =
719                                             dinfo->conf.pc_subdevice;
720                                         conf_old.pc_vendor =
721                                             dinfo->conf.pc_vendor;
722                                         conf_old.pc_device =
723                                             dinfo->conf.pc_device;
724                                         conf_old.pc_class =
725                                             dinfo->conf.pc_class;
726                                         conf_old.pc_subclass =
727                                             dinfo->conf.pc_subclass;
728                                         conf_old.pc_progif =
729                                             dinfo->conf.pc_progif;
730                                         conf_old.pc_revid =
731                                             dinfo->conf.pc_revid;
732                                         strncpy(conf_old.pd_name,
733                                             dinfo->conf.pd_name,
734                                             sizeof(conf_old.pd_name));
735                                         conf_old.pd_name[PCI_MAXNAMELEN] = 0;
736                                         conf_old.pd_unit =
737                                             dinfo->conf.pd_unit;
738                                         confdata = &conf_old;
739                                 } else
740 #endif /* PRE7_COMPAT */
741                                         confdata = &dinfo->conf;
742                                 /* Only if we can copy it out do we count it. */
743                                 if (!(error = copyout(confdata,
744                                     (caddr_t)cio->matches +
745                                     confsz * cio->num_matches, confsz)))
746                                         cio->num_matches++;
747                         }
748                 }
749
750                 /*
751                  * Set the pointer into the list, so if the user is getting
752                  * n records at a time, where n < pci_numdevs,
753                  */
754                 cio->offset = i;
755
756                 /*
757                  * Set the generation, the user will need this if they make
758                  * another ioctl call with offset != 0.
759                  */
760                 cio->generation = pci_generation;
761
762                 /*
763                  * If this is the last device, inform the user so he won't
764                  * bother asking for more devices.  If dinfo isn't NULL, we
765                  * know that there are more matches in the list because of
766                  * the way the traversal is done.
767                  */
768                 if (dinfo == NULL)
769                         cio->status = PCI_GETCONF_LAST_DEVICE;
770                 else
771                         cio->status = PCI_GETCONF_MORE_DEVS;
772
773 getconfexit:
774 #ifdef PRE7_COMPAT
775 #ifdef COMPAT_FREEBSD32
776                 if (cmd == PCIOCGETCONF_OLD32) {
777                         cio32->status = cio->status;
778                         cio32->generation = cio->generation;
779                         cio32->offset = cio->offset;
780                         cio32->num_matches = cio->num_matches;
781                         free(cio, M_TEMP);
782                 }
783                 if (pattern_buf_old32 != NULL)
784                         free(pattern_buf_old32, M_TEMP);
785 #endif
786                 if (pattern_buf_old != NULL)
787                         free(pattern_buf_old, M_TEMP);
788 #endif
789                 if (pattern_buf != NULL)
790                         free(pattern_buf, M_TEMP);
791
792                 break;
793
794 #ifdef PRE7_COMPAT
795         case PCIOCREAD_OLD:
796         case PCIOCWRITE_OLD:
797                 io_old = (struct pci_io_old *)data;
798                 iodata.pi_sel.pc_domain = 0;
799                 iodata.pi_sel.pc_bus = io_old->pi_sel.pc_bus;
800                 iodata.pi_sel.pc_dev = io_old->pi_sel.pc_dev;
801                 iodata.pi_sel.pc_func = io_old->pi_sel.pc_func;
802                 iodata.pi_reg = io_old->pi_reg;
803                 iodata.pi_width = io_old->pi_width;
804                 iodata.pi_data = io_old->pi_data;
805                 data = (caddr_t)&iodata;
806                 /* FALLTHROUGH */
807 #endif
808         case PCIOCREAD:
809         case PCIOCWRITE:
810                 io = (struct pci_io *)data;
811                 switch(io->pi_width) {
812                 case 4:
813                 case 2:
814                 case 1:
815                         /* Make sure register is not negative and aligned. */
816                         if (io->pi_reg < 0 ||
817                             io->pi_reg & (io->pi_width - 1)) {
818                                 error = EINVAL;
819                                 break;
820                         }
821                         /*
822                          * Assume that the user-level bus number is
823                          * in fact the physical PCI bus number.
824                          * Look up the grandparent, i.e. the bridge device,
825                          * so that we can issue configuration space cycles.
826                          */
827                         pcidev = pci_find_dbsf(io->pi_sel.pc_domain,
828                             io->pi_sel.pc_bus, io->pi_sel.pc_dev,
829                             io->pi_sel.pc_func);
830                         if (pcidev) {
831                                 brdev = device_get_parent(
832                                     device_get_parent(pcidev));
833
834 #ifdef PRE7_COMPAT
835                                 if (cmd == PCIOCWRITE || cmd == PCIOCWRITE_OLD)
836 #else
837                                 if (cmd == PCIOCWRITE)
838 #endif
839                                         PCIB_WRITE_CONFIG(brdev,
840                                                           io->pi_sel.pc_bus,
841                                                           io->pi_sel.pc_dev,
842                                                           io->pi_sel.pc_func,
843                                                           io->pi_reg,
844                                                           io->pi_data,
845                                                           io->pi_width);
846 #ifdef PRE7_COMPAT
847                                 else if (cmd == PCIOCREAD_OLD)
848                                         io_old->pi_data =
849                                                 PCIB_READ_CONFIG(brdev,
850                                                           io->pi_sel.pc_bus,
851                                                           io->pi_sel.pc_dev,
852                                                           io->pi_sel.pc_func,
853                                                           io->pi_reg,
854                                                           io->pi_width);
855 #endif
856                                 else
857                                         io->pi_data =
858                                                 PCIB_READ_CONFIG(brdev,
859                                                           io->pi_sel.pc_bus,
860                                                           io->pi_sel.pc_dev,
861                                                           io->pi_sel.pc_func,
862                                                           io->pi_reg,
863                                                           io->pi_width);
864                                 error = 0;
865                         } else {
866 #ifdef COMPAT_FREEBSD4
867                                 if (cmd == PCIOCREAD_OLD) {
868                                         io_old->pi_data = -1;
869                                         error = 0;
870                                 } else
871 #endif
872                                         error = ENODEV;
873                         }
874                         break;
875                 default:
876                         error = EINVAL;
877                         break;
878                 }
879                 break;
880
881         case PCIOCGETBAR:
882                 bio = (struct pci_bar_io *)data;
883
884                 /*
885                  * Assume that the user-level bus number is
886                  * in fact the physical PCI bus number.
887                  */
888                 pcidev = pci_find_dbsf(bio->pbi_sel.pc_domain,
889                     bio->pbi_sel.pc_bus, bio->pbi_sel.pc_dev,
890                     bio->pbi_sel.pc_func);
891                 if (pcidev == NULL) {
892                         error = ENODEV;
893                         break;
894                 }
895                 pm = pci_find_bar(pcidev, bio->pbi_reg);
896                 if (pm == NULL) {
897                         error = EINVAL;
898                         break;
899                 }
900                 bio->pbi_base = pm->pm_value;
901                 bio->pbi_length = (pci_addr_t)1 << pm->pm_size;
902                 bio->pbi_enabled = pci_bar_enabled(pcidev, pm);
903                 error = 0;
904                 break;
905         case PCIOCATTACHED:
906                 error = 0;
907                 io = (struct pci_io *)data;
908                 pcidev = pci_find_dbsf(io->pi_sel.pc_domain, io->pi_sel.pc_bus,
909                                        io->pi_sel.pc_dev, io->pi_sel.pc_func);
910                 if (pcidev != NULL)
911                         io->pi_data = device_is_attached(pcidev);
912                 else
913                         error = ENODEV;
914                 break;
915         default:
916                 error = ENOTTY;
917                 break;
918         }
919
920         return (error);
921 }