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