xref: /netbsd-src/sys/dev/ic/icp_ioctl.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: icp_ioctl.c,v 1.20 2014/03/16 05:20:27 dholland 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.20 2014/03/16 05:20:27 dholland 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 	.d_open = icpopen,
97 	.d_close = nullclose,
98 	.d_read = noread,
99 	.d_write = nowrite,
100 	.d_ioctl = icpioctl,
101 	.d_stop = nostop,
102 	.d_tty = notty,
103 	.d_poll = nopoll,
104 	.d_mmap = nommap,
105 	.d_kqfilter = nokqfilter,
106 	.d_flag = D_OTHER
107 };
108 
109 extern struct cfdriver icp_cd;
110 
111 kmutex_t icp_ioctl_mutex;
112 
113 static int
114 icpopen(dev_t dev, int flag, int mode, struct lwp *l)
115 {
116 
117 	if (device_lookup(&icp_cd, minor(dev)) == NULL)
118 		return (ENXIO);
119 
120 	return (0);
121 }
122 
123 static int
124 icpioctl(dev_t dev, u_long cmd, void *data, int flag,
125     struct lwp *l)
126 {
127 	int error = 0;
128 
129 	mutex_enter(&icp_ioctl_mutex);
130 
131 	switch (cmd) {
132 	case GDT_IOCTL_GENERAL:
133 	    {
134 		struct icp_softc *icp;
135 		gdt_ucmd_t *ucmd = (void *) data;
136 
137 		error = kauth_authorize_device_passthru(l->l_cred, dev,
138 		    KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_ALL, data);
139 		if (error)
140 			break;
141 
142 		icp = device_lookup_private(&icp_cd, ucmd->io_node);
143 		if (icp == NULL) {
144 			error = ENXIO;
145 			break;
146 		}
147 
148 		error = icp_ucmd(icp, ucmd);
149 		break;
150 	    }
151 
152 	case GDT_IOCTL_DRVERS:
153 		*(int *) data =
154 		    (ICP_DRIVER_VERSION << 8) | ICP_DRIVER_SUBVERSION;
155 		break;
156 
157 	case GDT_IOCTL_CTRTYPE:
158 	    {
159 		struct icp_softc *icp;
160 		gdt_ctrt_t *ctrt = (void *) data;
161 
162 		icp = device_lookup_private(&icp_cd, ctrt->io_node);
163 		if (icp == NULL) {
164 			error = ENXIO;
165 			break;
166 		}
167 
168 		/* XXX magic numbers */
169 		ctrt->oem_id = 0x8000;
170 		ctrt->type = 0xfd;
171 		ctrt->info = (icp->icp_pci_bus << 8) | (icp->icp_pci_device << 3);
172 		ctrt->ext_type = 0x6000 | icp->icp_pci_subdevice_id;
173 		ctrt->device_id = icp->icp_pci_device_id;
174 		ctrt->sub_device_id = icp->icp_pci_subdevice_id;
175 		break;
176 	    }
177 
178 	case GDT_IOCTL_OSVERS:
179 	    {
180 		gdt_osv_t *osv = (void *) data;
181 
182 		osv->oscode = 12;
183 
184 		/*
185 		 * __NetBSD_Version__ is encoded thusly:
186 		 *
187 		 *	MMmmrrpp00
188 		 *
189 		 * M = major version
190 		 * m = minor version
191 		 * r = release ["",A-Z[A-Z] but numeric]
192 		 * p = patchlevel
193 		 *
194 		 * Since the ABI is not supposed to change between
195 		 * patchlevels of the same major/minor version, we
196 		 * will encode major/minor/release into the returned
197 		 * data.
198 		 */
199 
200 		osv->version = __NetBSD_Version__ / 100000000;
201 		osv->subversion = (__NetBSD_Version__ / 1000000) % 100;
202 		osv->revision = (__NetBSD_Version__ / 10000) % 100;
203 
204 		strcpy(osv->name, ostype);
205 		break;
206 	    }
207 
208 	case GDT_IOCTL_CTRCNT:
209 		*(int *) data = icp_count;
210 		break;
211 
212 	case GDT_IOCTL_EVENT:
213 	    {
214 		struct icp_softc *icp;
215 		gdt_event_t *evt = (void *) data;
216 		gdt_evt_str *e = &evt->dvr;
217 		int s;
218 
219 		icp = device_lookup_private(&icp_cd, minor(dev));
220 
221 		switch (evt->erase) {
222 		case 0xff:
223 			switch (evt->dvr.event_source) {
224 			case GDT_ES_TEST:
225 				e->event_data.size =
226 				    sizeof(e->event_data.eu.test);
227 				break;
228 
229 			case GDT_ES_DRIVER:
230 				e->event_data.size =
231 				    sizeof(e->event_data.eu.driver);
232 				break;
233 
234 			case GDT_ES_SYNC:
235 				e->event_data.size =
236 				    sizeof(e->event_data.eu.sync);
237 				break;
238 
239 			default:
240 				e->event_data.size =
241 				    sizeof(e->event_data.eu.async);
242 				break;
243 			}
244 			s = splbio();
245 			icp_store_event(icp, e->event_source, e->event_idx,
246 			    &e->event_data);
247 			splx(s);
248 			break;
249 
250 		case 0xfe:
251 			s = splbio();
252 			icp_clear_events(icp);
253 			splx(s);
254 			break;
255 
256 		case 0:
257 			evt->handle = icp_read_event(icp, evt->handle, e);
258 			break;
259 
260 		default:
261 			icp_readapp_event(icp, (u_int8_t) evt->erase, e);
262 			break;
263 		}
264 		break;
265 	    }
266 
267 	case GDT_IOCTL_STATIST:
268 		memcpy(&icp_stats, data, sizeof(gdt_statist_t));
269 		break;
270 
271 
272 	case GDT_IOCTL_RESCAN:
273 	    {
274 		struct icp_softc *icp;
275 		gdt_rescan_t *rsc = (void *) data;
276 
277 		icp = device_lookup_private(&icp_cd, rsc->io_node);
278 		if (icp == NULL) {
279 			error = ENXIO;
280 			break;
281 		}
282 
283 		error = icp_freeze(icp);
284 		if (error)
285 			break;
286 		if (rsc->flag == 0)
287 			icp_rescan_all(icp);
288 		else
289 			icp_rescan(icp, rsc->hdr_no);
290 		icp_unfreeze(icp);
291 		break;
292 	    }
293 
294 	default:
295 		error = ENOTTY;
296 	}
297 
298 	mutex_exit(&icp_ioctl_mutex);
299 
300 	return (error);
301 }
302