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