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