pci_usrreq.c revision 1.2.4.2 1 1.2.4.2 fvdl /* $NetBSD: pci_usrreq.c,v 1.2.4.2 2001/10/01 12:45:58 fvdl Exp $ */
2 1.2.4.2 fvdl
3 1.2.4.2 fvdl /*
4 1.2.4.2 fvdl * Copyright 2001 Wasabi Systems, Inc.
5 1.2.4.2 fvdl * All rights reserved.
6 1.2.4.2 fvdl *
7 1.2.4.2 fvdl * Written by Jason R. Thorpe for Wasabi Systems, Inc.
8 1.2.4.2 fvdl *
9 1.2.4.2 fvdl * Redistribution and use in source and binary forms, with or without
10 1.2.4.2 fvdl * modification, are permitted provided that the following conditions
11 1.2.4.2 fvdl * are met:
12 1.2.4.2 fvdl * 1. Redistributions of source code must retain the above copyright
13 1.2.4.2 fvdl * notice, this list of conditions and the following disclaimer.
14 1.2.4.2 fvdl * 2. Redistributions in binary form must reproduce the above copyright
15 1.2.4.2 fvdl * notice, this list of conditions and the following disclaimer in the
16 1.2.4.2 fvdl * documentation and/or other materials provided with the distribution.
17 1.2.4.2 fvdl * 3. All advertising materials mentioning features or use of this software
18 1.2.4.2 fvdl * must display the following acknowledgement:
19 1.2.4.2 fvdl * This product includes software developed for the NetBSD Project by
20 1.2.4.2 fvdl * Wasabi Systems, Inc.
21 1.2.4.2 fvdl * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 1.2.4.2 fvdl * or promote products derived from this software without specific prior
23 1.2.4.2 fvdl * written permission.
24 1.2.4.2 fvdl *
25 1.2.4.2 fvdl * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 1.2.4.2 fvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 1.2.4.2 fvdl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 1.2.4.2 fvdl * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 1.2.4.2 fvdl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 1.2.4.2 fvdl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 1.2.4.2 fvdl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 1.2.4.2 fvdl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 1.2.4.2 fvdl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 1.2.4.2 fvdl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 1.2.4.2 fvdl * POSSIBILITY OF SUCH DAMAGE.
36 1.2.4.2 fvdl */
37 1.2.4.2 fvdl
38 1.2.4.2 fvdl /*
39 1.2.4.2 fvdl * User -> kernel interface for PCI bus access.
40 1.2.4.2 fvdl */
41 1.2.4.2 fvdl
42 1.2.4.2 fvdl #include <sys/param.h>
43 1.2.4.2 fvdl #include <sys/conf.h>
44 1.2.4.2 fvdl #include <sys/device.h>
45 1.2.4.2 fvdl #include <sys/ioctl.h>
46 1.2.4.2 fvdl #include <sys/proc.h>
47 1.2.4.2 fvdl #include <sys/systm.h>
48 1.2.4.2 fvdl #include <sys/errno.h>
49 1.2.4.2 fvdl #include <sys/fcntl.h>
50 1.2.4.2 fvdl #include <sys/vnode.h>
51 1.2.4.2 fvdl
52 1.2.4.2 fvdl #include <dev/pci/pcireg.h>
53 1.2.4.2 fvdl #include <dev/pci/pcivar.h>
54 1.2.4.2 fvdl #include <dev/pci/pciio.h>
55 1.2.4.2 fvdl
56 1.2.4.2 fvdl cdev_decl(pci);
57 1.2.4.2 fvdl
58 1.2.4.2 fvdl int
59 1.2.4.2 fvdl pciopen(struct vnode *devvp, int flags, int mode, struct proc *p)
60 1.2.4.2 fvdl {
61 1.2.4.2 fvdl struct pci_softc *sc;
62 1.2.4.2 fvdl int unit;
63 1.2.4.2 fvdl
64 1.2.4.2 fvdl unit = minor(vdev_rdev(devvp));
65 1.2.4.2 fvdl sc = device_lookup(&pci_cd, unit);
66 1.2.4.2 fvdl if (sc == NULL)
67 1.2.4.2 fvdl return (ENXIO);
68 1.2.4.2 fvdl vdev_setprivdata(devvp, sc);
69 1.2.4.2 fvdl
70 1.2.4.2 fvdl return (0);
71 1.2.4.2 fvdl }
72 1.2.4.2 fvdl
73 1.2.4.2 fvdl int
74 1.2.4.2 fvdl pciclose(struct vnode *devvp, int flags, int mode, struct proc *p)
75 1.2.4.2 fvdl {
76 1.2.4.2 fvdl
77 1.2.4.2 fvdl return (0);
78 1.2.4.2 fvdl }
79 1.2.4.2 fvdl
80 1.2.4.2 fvdl int
81 1.2.4.2 fvdl pciioctl(struct vnode *devvp, u_long cmd, caddr_t data, int flag,
82 1.2.4.2 fvdl struct proc *p)
83 1.2.4.2 fvdl {
84 1.2.4.2 fvdl struct pci_softc *sc = vdev_privdata(devvp);
85 1.2.4.2 fvdl struct pciio_bdf_cfgreg *bdfr = (void *) data;
86 1.2.4.2 fvdl struct pciio_businfo *binfo = (void *) data;
87 1.2.4.2 fvdl pcitag_t tag;
88 1.2.4.2 fvdl
89 1.2.4.2 fvdl switch (cmd) {
90 1.2.4.2 fvdl case PCI_IOC_BDF_CFGREAD:
91 1.2.4.2 fvdl case PCI_IOC_BDF_CFGWRITE:
92 1.2.4.2 fvdl if (bdfr->bus > 255 || bdfr->device >= sc->sc_maxndevs ||
93 1.2.4.2 fvdl bdfr->function > 7)
94 1.2.4.2 fvdl return (EINVAL);
95 1.2.4.2 fvdl tag = pci_make_tag(sc->sc_pc, bdfr->bus, bdfr->device,
96 1.2.4.2 fvdl bdfr->function);
97 1.2.4.2 fvdl if (cmd == PCI_IOC_BDF_CFGREAD)
98 1.2.4.2 fvdl bdfr->cfgreg.val = pci_conf_read(sc->sc_pc, tag,
99 1.2.4.2 fvdl bdfr->cfgreg.reg);
100 1.2.4.2 fvdl else {
101 1.2.4.2 fvdl if ((flag & FWRITE) == 0)
102 1.2.4.2 fvdl return (EBADF);
103 1.2.4.2 fvdl pci_conf_write(sc->sc_pc, tag, bdfr->cfgreg.reg,
104 1.2.4.2 fvdl bdfr->cfgreg.val);
105 1.2.4.2 fvdl }
106 1.2.4.2 fvdl break;
107 1.2.4.2 fvdl
108 1.2.4.2 fvdl case PCI_IOC_BUSINFO:
109 1.2.4.2 fvdl binfo->busno = sc->sc_bus;
110 1.2.4.2 fvdl binfo->maxdevs = sc->sc_maxndevs;
111 1.2.4.2 fvdl break;
112 1.2.4.2 fvdl
113 1.2.4.2 fvdl default:
114 1.2.4.2 fvdl return (ENOTTY);
115 1.2.4.2 fvdl }
116 1.2.4.2 fvdl
117 1.2.4.2 fvdl return (0);
118 1.2.4.2 fvdl }
119 1.2.4.2 fvdl
120 1.2.4.2 fvdl paddr_t
121 1.2.4.2 fvdl pcimmap(struct vnode *devvp, off_t offset, int prot)
122 1.2.4.2 fvdl {
123 1.2.4.2 fvdl #if 0
124 1.2.4.2 fvdl struct pci_softc *sc = vdev_privdata(devvp);
125 1.2.4.2 fvdl
126 1.2.4.2 fvdl /*
127 1.2.4.2 fvdl * Since we allow mapping of the entire bus, we
128 1.2.4.2 fvdl * take the offset to be the address on the bus,
129 1.2.4.2 fvdl * and pass 0 as the offset into that range.
130 1.2.4.2 fvdl *
131 1.2.4.2 fvdl * XXX Need a way to deal with linear/prefetchable/etc.
132 1.2.4.2 fvdl */
133 1.2.4.2 fvdl return (bus_space_mmap(sc->sc_memt, offset, 0, prot, 0));
134 1.2.4.2 fvdl #else
135 1.2.4.2 fvdl /* XXX Consider this further. */
136 1.2.4.2 fvdl return (-1);
137 1.2.4.2 fvdl #endif
138 1.2.4.2 fvdl }
139 1.2.4.2 fvdl
140 1.2.4.2 fvdl /*
141 1.2.4.2 fvdl * pci_devioctl:
142 1.2.4.2 fvdl *
143 1.2.4.2 fvdl * PCI ioctls that can be performed on devices directly.
144 1.2.4.2 fvdl */
145 1.2.4.2 fvdl int
146 1.2.4.2 fvdl pci_devioctl(pci_chipset_tag_t pc, pcitag_t tag, u_long cmd, caddr_t data,
147 1.2.4.2 fvdl int flag, struct proc *p)
148 1.2.4.2 fvdl {
149 1.2.4.2 fvdl struct pciio_cfgreg *r = (void *) data;
150 1.2.4.2 fvdl
151 1.2.4.2 fvdl switch (cmd) {
152 1.2.4.2 fvdl case PCI_IOC_CFGREAD:
153 1.2.4.2 fvdl case PCI_IOC_CFGWRITE:
154 1.2.4.2 fvdl if (cmd == PCI_IOC_CFGREAD)
155 1.2.4.2 fvdl r->val = pci_conf_read(pc, tag, r->reg);
156 1.2.4.2 fvdl else {
157 1.2.4.2 fvdl if ((flag & FWRITE) == 0)
158 1.2.4.2 fvdl return (EBADF);
159 1.2.4.2 fvdl pci_conf_write(pc, tag, r->reg, r->val);
160 1.2.4.2 fvdl }
161 1.2.4.2 fvdl break;
162 1.2.4.2 fvdl
163 1.2.4.2 fvdl default:
164 1.2.4.2 fvdl return (ENOTTY);
165 1.2.4.2 fvdl }
166 1.2.4.2 fvdl
167 1.2.4.2 fvdl return (0);
168 1.2.4.2 fvdl }
169