Home | History | Annotate | Line # | Download | only in scsipi
scsipi_ioctl.c revision 1.58.6.2
      1 /*	$NetBSD: scsipi_ioctl.c,v 1.58.6.2 2006/03/31 09:45:25 tron Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 2004 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum.
      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  * Contributed by HD Associates (hd (at) world.std.com).
     41  * Copyright (c) 1992, 1993 HD Associates
     42  *
     43  * Berkeley style copyright.
     44  */
     45 
     46 #include <sys/cdefs.h>
     47 __KERNEL_RCSID(0, "$NetBSD: scsipi_ioctl.c,v 1.58.6.2 2006/03/31 09:45:25 tron Exp $");
     48 
     49 #include "opt_compat_freebsd.h"
     50 #include "opt_compat_netbsd.h"
     51 
     52 #include <sys/param.h>
     53 #include <sys/errno.h>
     54 #include <sys/systm.h>
     55 #include <sys/malloc.h>
     56 #include <sys/buf.h>
     57 #include <sys/proc.h>
     58 #include <sys/device.h>
     59 #include <sys/fcntl.h>
     60 
     61 #include <dev/scsipi/scsipi_all.h>
     62 #include <dev/scsipi/scsipiconf.h>
     63 #include <dev/scsipi/scsipi_base.h>
     64 #include <dev/scsipi/scsiconf.h>
     65 #include <sys/scsiio.h>
     66 
     67 #include "scsibus.h"
     68 #include "atapibus.h"
     69 
     70 struct scsi_ioctl {
     71 	LIST_ENTRY(scsi_ioctl) si_list;
     72 	struct buf si_bp;
     73 	struct uio si_uio;
     74 	struct iovec si_iov;
     75 	scsireq_t si_screq;
     76 	struct scsipi_periph *si_periph;
     77 };
     78 
     79 static LIST_HEAD(, scsi_ioctl) si_head;
     80 
     81 static struct scsi_ioctl *
     82 si_get(void)
     83 {
     84 	struct scsi_ioctl *si;
     85 	int s;
     86 
     87 	si = malloc(sizeof(struct scsi_ioctl), M_TEMP, M_WAITOK|M_ZERO);
     88 	simple_lock_init(&si->si_bp.b_interlock);
     89 	s = splbio();
     90 	LIST_INSERT_HEAD(&si_head, si, si_list);
     91 	splx(s);
     92 	return (si);
     93 }
     94 
     95 static void
     96 si_free(struct scsi_ioctl *si)
     97 {
     98 	int s;
     99 
    100 	s = splbio();
    101 	LIST_REMOVE(si, si_list);
    102 	splx(s);
    103 	free(si, M_TEMP);
    104 }
    105 
    106 static struct scsi_ioctl *
    107 si_find(struct buf *bp)
    108 {
    109 	struct scsi_ioctl *si;
    110 	int s;
    111 
    112 	s = splbio();
    113 	for (si = si_head.lh_first; si != 0; si = si->si_list.le_next)
    114 		if (bp == &si->si_bp)
    115 			break;
    116 	splx(s);
    117 	return (si);
    118 }
    119 
    120 /*
    121  * We let the user interpret his own sense in the generic scsi world.
    122  * This routine is called at interrupt time if the XS_CTL_USERCMD bit was set
    123  * in the flags passed to scsi_scsipi_cmd(). No other completion processing
    124  * takes place, even if we are running over another device driver.
    125  * The lower level routines that call us here, will free the xs and restart
    126  * the device's queue if such exists.
    127  */
    128 void
    129 scsipi_user_done(struct scsipi_xfer *xs)
    130 {
    131 	struct buf *bp;
    132 	struct scsi_ioctl *si;
    133 	scsireq_t *screq;
    134 	struct scsipi_periph *periph = xs->xs_periph;
    135 	int s;
    136 
    137 	bp = xs->bp;
    138 #ifdef DIAGNOSTIC
    139 	if (bp == NULL) {
    140 		scsipi_printaddr(periph);
    141 		printf("user command with no buf\n");
    142 		panic("scsipi_user_done");
    143 	}
    144 #endif
    145 	si = si_find(bp);
    146 #ifdef DIAGNOSTIC
    147 	if (si == NULL) {
    148 		scsipi_printaddr(periph);
    149 		printf("user command with no ioctl\n");
    150 		panic("scsipi_user_done");
    151 	}
    152 #endif
    153 
    154 	screq = &si->si_screq;
    155 
    156 	SC_DEBUG(xs->xs_periph, SCSIPI_DB2, ("user-done\n"));
    157 
    158 	screq->retsts = 0;
    159 	screq->status = xs->status;
    160 	switch (xs->error) {
    161 	case XS_NOERROR:
    162 		SC_DEBUG(periph, SCSIPI_DB3, ("no error\n"));
    163 		screq->datalen_used =
    164 		    xs->datalen - xs->resid;	/* probably rubbish */
    165 		screq->retsts = SCCMD_OK;
    166 		break;
    167 	case XS_SENSE:
    168 		SC_DEBUG(periph, SCSIPI_DB3, ("have sense\n"));
    169 		screq->senselen_used = min(sizeof(xs->sense.scsi_sense),
    170 		    SENSEBUFLEN);
    171 		memcpy(screq->sense, &xs->sense.scsi_sense, screq->senselen);
    172 		screq->retsts = SCCMD_SENSE;
    173 		break;
    174 	case XS_SHORTSENSE:
    175 		SC_DEBUG(periph, SCSIPI_DB3, ("have short sense\n"));
    176 		screq->senselen_used = min(sizeof(xs->sense.atapi_sense),
    177 		    SENSEBUFLEN);
    178 		memcpy(screq->sense, &xs->sense.scsi_sense, screq->senselen);
    179 		screq->retsts = SCCMD_UNKNOWN; /* XXX need a shortsense here */
    180 		break;
    181 	case XS_DRIVER_STUFFUP:
    182 		scsipi_printaddr(periph);
    183 		printf("passthrough: adapter inconsistency\n");
    184 		screq->retsts = SCCMD_UNKNOWN;
    185 		break;
    186 	case XS_SELTIMEOUT:
    187 		SC_DEBUG(periph, SCSIPI_DB3, ("seltimeout\n"));
    188 		screq->retsts = SCCMD_TIMEOUT;
    189 		break;
    190 	case XS_TIMEOUT:
    191 		SC_DEBUG(periph, SCSIPI_DB3, ("timeout\n"));
    192 		screq->retsts = SCCMD_TIMEOUT;
    193 		break;
    194 	case XS_BUSY:
    195 		SC_DEBUG(periph, SCSIPI_DB3, ("busy\n"));
    196 		screq->retsts = SCCMD_BUSY;
    197 		break;
    198 	default:
    199 		scsipi_printaddr(periph);
    200 		printf("unknown error category %d from adapter\n",
    201 		    xs->error);
    202 		screq->retsts = SCCMD_UNKNOWN;
    203 		break;
    204 	}
    205 
    206 	if (xs->xs_control & XS_CTL_ASYNC) {
    207 		s = splbio();
    208 		scsipi_put_xs(xs);
    209 		splx(s);
    210 	}
    211 }
    212 
    213 
    214 /* Pseudo strategy function
    215  * Called by scsipi_do_ioctl() via physio/physstrat if there is to
    216  * be data transfered, and directly if there is no data transfer.
    217  *
    218  * Should I reorganize this so it returns to physio instead
    219  * of sleeping in scsiio_scsipi_cmd?  Is there any advantage, other
    220  * than avoiding the probable duplicate wakeup in iodone? [PD]
    221  *
    222  * No, seems ok to me... [JRE]
    223  * (I don't see any duplicate wakeups)
    224  *
    225  * Can't be used with block devices or raw_read/raw_write directly
    226  * from the cdevsw/bdevsw tables because they couldn't have added
    227  * the screq structure. [JRE]
    228  */
    229 static void
    230 scsistrategy(struct buf *bp)
    231 {
    232 	struct scsi_ioctl *si;
    233 	scsireq_t *screq;
    234 	struct scsipi_periph *periph;
    235 	int error;
    236 	int flags = 0;
    237 
    238 	si = si_find(bp);
    239 	if (si == NULL) {
    240 		printf("scsistrategy: "
    241 		    "No matching ioctl request found in queue\n");
    242 		error = EINVAL;
    243 		goto bad;
    244 	}
    245 	screq = &si->si_screq;
    246 	periph = si->si_periph;
    247 	SC_DEBUG(periph, SCSIPI_DB2, ("user_strategy\n"));
    248 
    249 	/*
    250 	 * We're in trouble if physio tried to break up the transfer.
    251 	 */
    252 	if (bp->b_bcount != screq->datalen) {
    253 		scsipi_printaddr(periph);
    254 		printf("physio split the request.. cannot proceed\n");
    255 		error = EIO;
    256 		goto bad;
    257 	}
    258 
    259 	if (screq->timeout == 0) {
    260 		error = EINVAL;
    261 		goto bad;
    262 	}
    263 
    264 	if (screq->cmdlen > sizeof(struct scsipi_generic)) {
    265 		scsipi_printaddr(periph);
    266 		printf("cmdlen too big\n");
    267 		error = EFAULT;
    268 		goto bad;
    269 	}
    270 
    271 	if ((screq->flags & SCCMD_READ) && screq->datalen > 0)
    272 		flags |= XS_CTL_DATA_IN;
    273 	if ((screq->flags & SCCMD_WRITE) && screq->datalen > 0)
    274 		flags |= XS_CTL_DATA_OUT;
    275 	if (screq->flags & SCCMD_TARGET)
    276 		flags |= XS_CTL_TARGET;
    277 	if (screq->flags & SCCMD_ESCAPE)
    278 		flags |= XS_CTL_ESCAPE;
    279 
    280 	error = scsipi_command(periph, (void *)screq->cmd, screq->cmdlen,
    281 	    (void *)bp->b_data, screq->datalen,
    282 	    0, /* user must do the retries *//* ignored */
    283 	    screq->timeout, bp, flags | XS_CTL_USERCMD);
    284 
    285 bad:
    286 	if (error) {
    287 		bp->b_flags |= B_ERROR;
    288 		bp->b_error = error;
    289 	}
    290 	biodone(bp);
    291 	return;
    292 }
    293 
    294 /*
    295  * Something (e.g. another driver) has called us
    296  * with a periph and a scsi-specific ioctl to perform,
    297  * better try.  If user-level type command, we must
    298  * still be running in the context of the calling process
    299  */
    300 int
    301 scsipi_do_ioctl(struct scsipi_periph *periph, dev_t dev, u_long cmd,
    302     caddr_t addr, int flag, struct lwp *l)
    303 {
    304 	int error;
    305 
    306 	SC_DEBUG(periph, SCSIPI_DB2, ("scsipi_do_ioctl(0x%lx)\n", cmd));
    307 
    308 	/* Check for the safe-ness of this request. */
    309 	switch (cmd) {
    310 	case OSCIOCIDENTIFY:
    311 	case SCIOCIDENTIFY:
    312 		break;
    313 	case SCIOCCOMMAND:
    314 		if ((((scsireq_t *)addr)->flags & SCCMD_READ) == 0 &&
    315 		    (flag & FWRITE) == 0)
    316 			return (EBADF);
    317 		break;
    318 	default:
    319 		if ((flag & FWRITE) == 0)
    320 			return (EBADF);
    321 	}
    322 
    323 	switch (cmd) {
    324 	case SCIOCCOMMAND: {
    325 		scsireq_t *screq = (scsireq_t *)addr;
    326 		struct scsi_ioctl *si;
    327 		int len;
    328 
    329 		si = si_get();
    330 		si->si_screq = *screq;
    331 		si->si_periph = periph;
    332 		len = screq->datalen;
    333 		if (len) {
    334 			si->si_iov.iov_base = screq->databuf;
    335 			si->si_iov.iov_len = len;
    336 			si->si_uio.uio_iov = &si->si_iov;
    337 			si->si_uio.uio_iovcnt = 1;
    338 			si->si_uio.uio_resid = len;
    339 			si->si_uio.uio_offset = 0;
    340 			si->si_uio.uio_rw =
    341 			    (screq->flags & SCCMD_READ) ? UIO_READ : UIO_WRITE;
    342 			if ((flag & FKIOCTL) == 0) {
    343 				si->si_uio.uio_vmspace = l->l_proc->p_vmspace;
    344 			} else {
    345 				UIO_SETUP_SYSSPACE(&si->si_uio);
    346 			}
    347 			error = physio(scsistrategy, &si->si_bp, dev,
    348 			    (screq->flags & SCCMD_READ) ? B_READ : B_WRITE,
    349 			    periph->periph_channel->chan_adapter->adapt_minphys,
    350 			    &si->si_uio);
    351 		} else {
    352 			/* if no data, no need to translate it.. */
    353 			si->si_bp.b_flags = 0;
    354 			si->si_bp.b_data = 0;
    355 			si->si_bp.b_bcount = 0;
    356 			si->si_bp.b_dev = dev;
    357 			si->si_bp.b_proc = l->l_proc;
    358 			scsistrategy(&si->si_bp);
    359 			error = si->si_bp.b_error;
    360 		}
    361 		*screq = si->si_screq;
    362 		si_free(si);
    363 		return (error);
    364 	}
    365 	case SCIOCDEBUG: {
    366 		int level = *((int *)addr);
    367 
    368 		SC_DEBUG(periph, SCSIPI_DB3, ("debug set to %d\n", level));
    369 		periph->periph_dbflags = 0;
    370 		if (level & 1)
    371 			periph->periph_dbflags |= SCSIPI_DB1;
    372 		if (level & 2)
    373 			periph->periph_dbflags |= SCSIPI_DB2;
    374 		if (level & 4)
    375 			periph->periph_dbflags |= SCSIPI_DB3;
    376 		if (level & 8)
    377 			periph->periph_dbflags |= SCSIPI_DB4;
    378 		return (0);
    379 	}
    380 	case SCIOCRECONFIG:
    381 	case SCIOCDECONFIG:
    382 		return (EINVAL);
    383 	case SCIOCIDENTIFY: {
    384 		struct scsi_addr *sca = (struct scsi_addr *)addr;
    385 
    386 		switch (scsipi_periph_bustype(periph)) {
    387 		case SCSIPI_BUSTYPE_SCSI:
    388 			sca->type = TYPE_SCSI;
    389 			sca->addr.scsi.scbus =
    390 			    device_unit(device_parent(periph->periph_dev));
    391 			sca->addr.scsi.target = periph->periph_target;
    392 			sca->addr.scsi.lun = periph->periph_lun;
    393 			return (0);
    394 		case SCSIPI_BUSTYPE_ATAPI:
    395 			sca->type = TYPE_ATAPI;
    396 			sca->addr.atapi.atbus =
    397 			    device_unit(device_parent(periph->periph_dev));
    398 			sca->addr.atapi.drive = periph->periph_target;
    399 			return (0);
    400 		}
    401 		return (ENXIO);
    402 	}
    403 #if defined(COMPAT_12) || defined(COMPAT_FREEBSD)
    404 	/* SCIOCIDENTIFY before ATAPI staff merge */
    405 	case OSCIOCIDENTIFY: {
    406 		struct oscsi_addr *sca = (struct oscsi_addr *)addr;
    407 
    408 		switch (scsipi_periph_bustype(periph)) {
    409 		case SCSIPI_BUSTYPE_SCSI:
    410 			sca->scbus =
    411 			    device_unit(device_parent(periph->periph_dev));
    412 			sca->target = periph->periph_target;
    413 			sca->lun = periph->periph_lun;
    414 			return (0);
    415 		}
    416 		return (ENODEV);
    417 	}
    418 #endif
    419 	default:
    420 		return (ENOTTY);
    421 	}
    422 
    423 #ifdef DIAGNOSTIC
    424 	panic("scsipi_do_ioctl: impossible");
    425 #endif
    426 }
    427