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