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