xref: /netbsd-src/sys/dev/ic/icp_ioctl.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: icp_ioctl.c,v 1.19 2008/06/08 12:43:51 tsutsui Exp $	*/
2 
3 /*-
4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of Wasabi Systems, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  *       Copyright (c) 2000-01 Intel Corporation
34  *       All Rights Reserved
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions, and the following disclaimer,
41  *    without modification, immediately at the beginning of the file.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. The name of the author may not be used to endorse or promote products
46  *    derived from this software without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
52  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  */
60 
61 /*
62  * icp_ioctl.c: Ioctl interface for the ICP-Vortex management tools.
63  *
64  * Based on ICP's FreeBSD "iir" driver ioctl interface, written by
65  * Achim Leubner <achim.leubner@intel.com>.
66  *
67  * This is intended to be ABI-compatile with the ioctl interface for
68  * other OSs.
69  */
70 
71 #include <sys/cdefs.h>
72 __KERNEL_RCSID(0, "$NetBSD: icp_ioctl.c,v 1.19 2008/06/08 12:43:51 tsutsui Exp $");
73 
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/kernel.h>
77 #include <sys/device.h>
78 #include <sys/proc.h>
79 #include <sys/conf.h>
80 #include <sys/ioctl.h>
81 #include <sys/kauth.h>
82 
83 #include <sys/bus.h>
84 
85 #include <dev/ic/icpreg.h>
86 #include <dev/ic/icpvar.h>
87 
88 /* These are simply the same as ICP's "iir" driver for FreeBSD. */
89 #define	ICP_DRIVER_VERSION		1
90 #define	ICP_DRIVER_SUBVERSION		3
91 
92 static dev_type_open(icpopen);
93 static dev_type_ioctl(icpioctl);
94 
95 const struct cdevsw icp_cdevsw = {
96 	icpopen, nullclose, noread, nowrite, icpioctl,
97 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
98 };
99 
100 extern struct cfdriver icp_cd;
101 
102 kmutex_t icp_ioctl_mutex;
103 
104 static int
105 icpopen(dev_t dev, int flag, int mode, struct lwp *l)
106 {
107 
108 	if (device_lookup(&icp_cd, minor(dev)) == NULL)
109 		return (ENXIO);
110 
111 	return (0);
112 }
113 
114 static int
115 icpioctl(dev_t dev, u_long cmd, void *data, int flag,
116     struct lwp *l)
117 {
118 	int error = 0;
119 
120 	mutex_enter(&icp_ioctl_mutex);
121 
122 	switch (cmd) {
123 	case GDT_IOCTL_GENERAL:
124 	    {
125 		struct icp_softc *icp;
126 		gdt_ucmd_t *ucmd = (void *) data;
127 
128 		error = kauth_authorize_device_passthru(l->l_cred, dev,
129 		    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_ALL, data);
130 		if (error)
131 			break;
132 
133 		icp = device_lookup_private(&icp_cd, ucmd->io_node);
134 		if (icp == NULL) {
135 			error = ENXIO;
136 			break;
137 		}
138 
139 		error = icp_ucmd(icp, ucmd);
140 		break;
141 	    }
142 
143 	case GDT_IOCTL_DRVERS:
144 		*(int *) data =
145 		    (ICP_DRIVER_VERSION << 8) | ICP_DRIVER_SUBVERSION;
146 		break;
147 
148 	case GDT_IOCTL_CTRTYPE:
149 	    {
150 		struct icp_softc *icp;
151 		gdt_ctrt_t *ctrt = (void *) data;
152 
153 		icp = device_lookup_private(&icp_cd, ctrt->io_node);
154 		if (icp == NULL) {
155 			error = ENXIO;
156 			break;
157 		}
158 
159 		/* XXX magic numbers */
160 		ctrt->oem_id = 0x8000;
161 		ctrt->type = 0xfd;
162 		ctrt->info = (icp->icp_pci_bus << 8) | (icp->icp_pci_device << 3);
163 		ctrt->ext_type = 0x6000 | icp->icp_pci_subdevice_id;
164 		ctrt->device_id = icp->icp_pci_device_id;
165 		ctrt->sub_device_id = icp->icp_pci_subdevice_id;
166 		break;
167 	    }
168 
169 	case GDT_IOCTL_OSVERS:
170 	    {
171 		gdt_osv_t *osv = (void *) data;
172 
173 		osv->oscode = 12;
174 
175 		/*
176 		 * __NetBSD_Version__ is encoded thusly:
177 		 *
178 		 *	MMmmrrpp00
179 		 *
180 		 * M = major version
181 		 * m = minor version
182 		 * r = release ["",A-Z[A-Z] but numeric]
183 		 * p = patchlevel
184 		 *
185 		 * Since the ABI is not supposed to change between
186 		 * patchlevels of the same major/minor version, we
187 		 * will encode major/minor/release into the returned
188 		 * data.
189 		 */
190 
191 		osv->version = __NetBSD_Version__ / 100000000;
192 		osv->subversion = (__NetBSD_Version__ / 1000000) % 100;
193 		osv->revision = (__NetBSD_Version__ / 10000) % 100;
194 
195 		strcpy(osv->name, ostype);
196 		break;
197 	    }
198 
199 	case GDT_IOCTL_CTRCNT:
200 		*(int *) data = icp_count;
201 		break;
202 
203 	case GDT_IOCTL_EVENT:
204 	    {
205 		struct icp_softc *icp;
206 		gdt_event_t *evt = (void *) data;
207 		gdt_evt_str *e = &evt->dvr;
208 		int s;
209 
210 		icp = device_lookup_private(&icp_cd, minor(dev));
211 
212 		switch (evt->erase) {
213 		case 0xff:
214 			switch (evt->dvr.event_source) {
215 			case GDT_ES_TEST:
216 				e->event_data.size =
217 				    sizeof(e->event_data.eu.test);
218 				break;
219 
220 			case GDT_ES_DRIVER:
221 				e->event_data.size =
222 				    sizeof(e->event_data.eu.driver);
223 				break;
224 
225 			case GDT_ES_SYNC:
226 				e->event_data.size =
227 				    sizeof(e->event_data.eu.sync);
228 				break;
229 
230 			default:
231 				e->event_data.size =
232 				    sizeof(e->event_data.eu.async);
233 				break;
234 			}
235 			s = splbio();
236 			icp_store_event(icp, e->event_source, e->event_idx,
237 			    &e->event_data);
238 			splx(s);
239 			break;
240 
241 		case 0xfe:
242 			s = splbio();
243 			icp_clear_events(icp);
244 			splx(s);
245 			break;
246 
247 		case 0:
248 			evt->handle = icp_read_event(icp, evt->handle, e);
249 			break;
250 
251 		default:
252 			icp_readapp_event(icp, (u_int8_t) evt->erase, e);
253 			break;
254 		}
255 		break;
256 	    }
257 
258 	case GDT_IOCTL_STATIST:
259 		memcpy(&icp_stats, data, sizeof(gdt_statist_t));
260 		break;
261 
262 
263 	case GDT_IOCTL_RESCAN:
264 	    {
265 		struct icp_softc *icp;
266 		gdt_rescan_t *rsc = (void *) data;
267 
268 		icp = device_lookup_private(&icp_cd, rsc->io_node);
269 		if (icp == NULL) {
270 			error = ENXIO;
271 			break;
272 		}
273 
274 		error = icp_freeze(icp);
275 		if (error)
276 			break;
277 		if (rsc->flag == 0)
278 			icp_rescan_all(icp);
279 		else
280 			icp_rescan(icp, rsc->hdr_no);
281 		icp_unfreeze(icp);
282 		break;
283 	    }
284 
285 	default:
286 		error = ENOTTY;
287 	}
288 
289 	mutex_exit(&icp_ioctl_mutex);
290 
291 	return (error);
292 }
293