source: rtems-libbsd/freebsd/dev/pci/pci_user.c @ fb4c8a9

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since fb4c8a9 was fb4c8a9, checked in by Jennifer Averett <jennifer.averett@…>, on 05/23/12 at 19:53:12

Added pcib for Nics.

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