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