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