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