Home | History | Annotate | Line # | Download | only in scsipi
scsipi_ioctl.c revision 1.32
      1 /*	$NetBSD: scsipi_ioctl.c,v 1.32 1998/08/15 03:02:48 mycroft Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994 Charles M. Hannum.  All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by Charles M. Hannum.
     17  * 4. The name of the author may not be used to endorse or promote products
     18  *    derived from this software without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Contributed by HD Associates (hd (at) world.std.com).
     34  * Copyright (c) 1992, 1993 HD Associates
     35  *
     36  * Berkeley style copyright.
     37  */
     38 
     39 #include "opt_compat_freebsd.h"
     40 #include "opt_compat_netbsd.h"
     41 
     42 #include <sys/types.h>
     43 #include <sys/errno.h>
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/malloc.h>
     47 #include <sys/buf.h>
     48 #include <sys/proc.h>
     49 #include <sys/device.h>
     50 #include <sys/fcntl.h>
     51 
     52 #include <dev/scsipi/scsipi_all.h>
     53 #include <dev/scsipi/scsi_all.h>
     54 #include <dev/scsipi/scsipiconf.h>
     55 #include <dev/scsipi/scsiconf.h>
     56 #include <sys/scsiio.h>
     57 
     58 #include "scsibus.h"
     59 #include "atapibus.h"
     60 
     61 struct scsi_ioctl {
     62 	LIST_ENTRY(scsi_ioctl) si_list;
     63 	struct buf si_bp;
     64 	struct uio si_uio;
     65 	struct iovec si_iov;
     66 	scsireq_t si_screq;
     67 	struct scsipi_link *si_sc_link;
     68 };
     69 
     70 LIST_HEAD(, scsi_ioctl) si_head;
     71 
     72 struct	scsi_ioctl *si_find __P((struct buf *));
     73 void	si_free __P((struct scsi_ioctl *));
     74 struct	scsi_ioctl *si_get __P((void));
     75 void	scsistrategy __P((struct buf *));
     76 
     77 struct scsi_ioctl *
     78 si_get()
     79 {
     80 	struct scsi_ioctl *si;
     81 	int s;
     82 
     83 	si = malloc(sizeof(struct scsi_ioctl), M_TEMP, M_WAITOK);
     84 	bzero(si, sizeof(struct scsi_ioctl));
     85 	s = splbio();
     86 	LIST_INSERT_HEAD(&si_head, si, si_list);
     87 	splx(s);
     88 	return (si);
     89 }
     90 
     91 void
     92 si_free(si)
     93 	struct scsi_ioctl *si;
     94 {
     95 	int s;
     96 
     97 	s = splbio();
     98 	LIST_REMOVE(si, si_list);
     99 	splx(s);
    100 	free(si, M_TEMP);
    101 }
    102 
    103 struct scsi_ioctl *
    104 si_find(bp)
    105 	struct buf *bp;
    106 {
    107 	struct scsi_ioctl *si;
    108 	int s;
    109 
    110 	s = splbio();
    111 	for (si = si_head.lh_first; si != 0; si = si->si_list.le_next)
    112 		if (bp == &si->si_bp)
    113 			break;
    114 	splx(s);
    115 	return (si);
    116 }
    117 
    118 /*
    119  * We let the user interpret his own sense in the generic scsi world.
    120  * This routine is called at interrupt time if the SCSI_USER bit was set
    121  * in the flags passed to scsi_scsipi_cmd(). No other completion processing
    122  * takes place, even if we are running over another device driver.
    123  * The lower level routines that call us here, will free the xs and restart
    124  * the device's queue if such exists.
    125  */
    126 void
    127 scsipi_user_done(xs)
    128 	struct scsipi_xfer *xs;
    129 {
    130 	struct buf *bp;
    131 	struct scsi_ioctl *si;
    132 	scsireq_t *screq;
    133 	struct scsipi_link *sc_link;
    134 
    135 	bp = xs->bp;
    136 	if (bp == NULL) {	/* ALL user requests must have a buf */
    137 		xs->sc_link->sc_print_addr(xs->sc_link);
    138 		printf("User command with no buf\n");
    139 		return;
    140 	}
    141 	si = si_find(bp);
    142 	if (si == NULL) {
    143 		xs->sc_link->sc_print_addr(xs->sc_link);
    144 		printf("User command with no ioctl\n");
    145 		return;
    146 	}
    147 	screq = &si->si_screq;
    148 	sc_link = si->si_sc_link;
    149 	SC_DEBUG(xs->sc_link, SDEV_DB2, ("user-done\n"));
    150 
    151 	screq->retsts = 0;
    152 	screq->status = xs->status;
    153 	switch (xs->error) {
    154 	case XS_NOERROR:
    155 		SC_DEBUG(sc_link, SDEV_DB3, ("no error\n"));
    156 		screq->datalen_used =
    157 		    xs->datalen - xs->resid;	/* probably rubbish */
    158 		screq->retsts = SCCMD_OK;
    159 		break;
    160 	case XS_SENSE:
    161 		SC_DEBUG(sc_link, SDEV_DB3, ("have sense\n"));
    162 		screq->senselen_used = min(sizeof(xs->sense.scsi_sense),
    163 		    SENSEBUFLEN);
    164 		bcopy(&xs->sense.scsi_sense, screq->sense, screq->senselen);
    165 		screq->retsts = SCCMD_SENSE;
    166 		break;
    167 	case XS_DRIVER_STUFFUP:
    168 		sc_link->sc_print_addr(sc_link);
    169 		printf("host adapter code inconsistency\n");
    170 		screq->retsts = SCCMD_UNKNOWN;
    171 		break;
    172 	case XS_TIMEOUT:
    173 		SC_DEBUG(sc_link, SDEV_DB3, ("timeout\n"));
    174 		screq->retsts = SCCMD_TIMEOUT;
    175 		break;
    176 	case XS_BUSY:
    177 		SC_DEBUG(sc_link, SDEV_DB3, ("busy\n"));
    178 		screq->retsts = SCCMD_BUSY;
    179 		break;
    180 	default:
    181 		sc_link->sc_print_addr(sc_link);
    182 		printf("unknown error category from host adapter code\n");
    183 		screq->retsts = SCCMD_UNKNOWN;
    184 		break;
    185 	}
    186 	biodone(bp); 	/* we're waiting on it in scsi_strategy() */
    187 }
    188 
    189 
    190 /* Pseudo strategy function
    191  * Called by scsipi_do_ioctl() via physio/physstrat if there is to
    192  * be data transfered, and directly if there is no data transfer.
    193  *
    194  * Should I reorganize this so it returns to physio instead
    195  * of sleeping in scsiio_scsipi_cmd?  Is there any advantage, other
    196  * than avoiding the probable duplicate wakeup in iodone? [PD]
    197  *
    198  * No, seems ok to me... [JRE]
    199  * (I don't see any duplicate wakeups)
    200  *
    201  * Can't be used with block devices or raw_read/raw_write directly
    202  * from the cdevsw/bdevsw tables because they couldn't have added
    203  * the screq structure. [JRE]
    204  */
    205 void
    206 scsistrategy(bp)
    207 	struct buf *bp;
    208 {
    209 	struct scsi_ioctl *si;
    210 	scsireq_t *screq;
    211 	struct scsipi_link *sc_link;
    212 	int error;
    213 	int flags = 0;
    214 	int s;
    215 
    216 	si = si_find(bp);
    217 	if (si == NULL) {
    218 		printf("user_strat: No ioctl\n");
    219 		error = EINVAL;
    220 		goto bad;
    221 	}
    222 	screq = &si->si_screq;
    223 	sc_link = si->si_sc_link;
    224 	SC_DEBUG(sc_link, SDEV_DB2, ("user_strategy\n"));
    225 
    226 	/*
    227 	 * We're in trouble if physio tried to break up the transfer.
    228 	 */
    229 	if (bp->b_bcount != screq->datalen) {
    230 		sc_link->sc_print_addr(sc_link);
    231 		printf("physio split the request.. cannot proceed\n");
    232 		error = EIO;
    233 		goto bad;
    234 	}
    235 
    236 	if (screq->timeout == 0) {
    237 		error = EINVAL;
    238 		goto bad;
    239 	}
    240 
    241 	if (screq->cmdlen > sizeof(struct scsipi_generic)) {
    242 		sc_link->sc_print_addr(sc_link);
    243 		printf("cmdlen too big\n");
    244 		error = EFAULT;
    245 		goto bad;
    246 	}
    247 
    248 	if (screq->flags & SCCMD_READ)
    249 		flags |= SCSI_DATA_IN;
    250 	if (screq->flags & SCCMD_WRITE)
    251 		flags |= SCSI_DATA_OUT;
    252 	if (screq->flags & SCCMD_TARGET)
    253 		flags |= SCSI_TARGET;
    254 	if (screq->flags & SCCMD_ESCAPE)
    255 		flags |= SCSI_ESCAPE;
    256 
    257 	error = scsipi_command(sc_link,
    258 	    (struct scsipi_generic *)screq->cmd, screq->cmdlen,
    259 	    (u_char *)bp->b_data, screq->datalen,
    260 	    0, /* user must do the retries *//* ignored */
    261 	    screq->timeout, bp, flags | SCSI_USER | SCSI_NOSLEEP);
    262 
    263 	/* because there is a bp, scsi_scsipi_cmd will return immediatly */
    264 	if (error)
    265 		goto bad;
    266 
    267 	SC_DEBUG(sc_link, SDEV_DB3, ("about to sleep\n"));
    268 	s = splbio();
    269 	while ((bp->b_flags & B_DONE) == 0)
    270 		tsleep(bp, PRIBIO, "scistr", 0);
    271 	splx(s);
    272 	SC_DEBUG(sc_link, SDEV_DB3, ("back from sleep\n"));
    273 
    274 	return;
    275 
    276 bad:
    277 	bp->b_flags |= B_ERROR;
    278 	bp->b_error = error;
    279 	biodone(bp);
    280 }
    281 
    282 /*
    283  * Something (e.g. another driver) has called us
    284  * with an sc_link for a target/lun/adapter, and a scsi
    285  * specific ioctl to perform, better try.
    286  * If user-level type command, we must still be running
    287  * in the context of the calling process
    288  */
    289 int
    290 scsipi_do_ioctl(sc_link, dev, cmd, addr, flag, p)
    291 	struct scsipi_link *sc_link;
    292 	dev_t dev;
    293 	u_long cmd;
    294 	caddr_t addr;
    295 	int flag;
    296 	struct proc *p;
    297 {
    298 	int error;
    299 
    300 	SC_DEBUG(sc_link, SDEV_DB2, ("scsipi_do_ioctl(0x%lx)\n", cmd));
    301 
    302 	/* Check for the safe-ness of this request. */
    303 	switch (cmd) {
    304 	case OSCIOCIDENTIFY:
    305 	case SCIOCIDENTIFY:
    306 		break;
    307 	case SCIOCCOMMAND:
    308 		if ((((scsireq_t *)addr)->flags & SCCMD_READ) == 0 &&
    309 		    (flag & FWRITE) == 0)
    310 			return (EBADF);
    311 		break;
    312 	default:
    313 		if ((flag & FWRITE) == 0)
    314 			return (EBADF);
    315 	}
    316 
    317 	switch (cmd) {
    318 	case SCIOCCOMMAND: {
    319 		scsireq_t *screq = (scsireq_t *)addr;
    320 		struct scsi_ioctl *si;
    321 		int len;
    322 
    323 		si = si_get();
    324 		si->si_screq = *screq;
    325 		si->si_sc_link = sc_link;
    326 		len = screq->datalen;
    327 		if (len) {
    328 			si->si_iov.iov_base = screq->databuf;
    329 			si->si_iov.iov_len = len;
    330 			si->si_uio.uio_iov = &si->si_iov;
    331 			si->si_uio.uio_iovcnt = 1;
    332 			si->si_uio.uio_resid = len;
    333 			si->si_uio.uio_offset = 0;
    334 			si->si_uio.uio_segflg = UIO_USERSPACE;
    335 			si->si_uio.uio_rw =
    336 			    (screq->flags & SCCMD_READ) ? UIO_READ : UIO_WRITE;
    337 			si->si_uio.uio_procp = p;
    338 			error = physio(scsistrategy, &si->si_bp, dev,
    339 			    (screq->flags & SCCMD_READ) ? B_READ : B_WRITE,
    340 			    sc_link->adapter->scsipi_minphys, &si->si_uio);
    341 		} else {
    342 			/* if no data, no need to translate it.. */
    343 			si->si_bp.b_flags = 0;
    344 			si->si_bp.b_data = 0;
    345 			si->si_bp.b_bcount = 0;
    346 			si->si_bp.b_dev = dev;
    347 			si->si_bp.b_proc = p;
    348 			scsistrategy(&si->si_bp);
    349 			error = si->si_bp.b_error;
    350 		}
    351 		*screq = si->si_screq;
    352 		si_free(si);
    353 		return (error);
    354 	}
    355 	case SCIOCDEBUG: {
    356 		int level = *((int *)addr);
    357 
    358 		SC_DEBUG(sc_link, SDEV_DB3, ("debug set to %d\n", level));
    359 		sc_link->flags &= ~SDEV_DBX; /* clear debug bits */
    360 		if (level & 1)
    361 			sc_link->flags |= SDEV_DB1;
    362 		if (level & 2)
    363 			sc_link->flags |= SDEV_DB2;
    364 		if (level & 4)
    365 			sc_link->flags |= SDEV_DB3;
    366 		if (level & 8)
    367 			sc_link->flags |= SDEV_DB4;
    368 		return (0);
    369 	}
    370 #if NSCSIBUS > 0
    371 	case SCIOCREPROBE: {
    372 		struct scsi_addr *sca = (struct scsi_addr *)addr;
    373 		if (sca->type != TYPE_SCSI)
    374 			return (ENODEV);
    375 		return (scsi_probe_busses(sca->addr.scsi.scbus,
    376 		    sca->addr.scsi.target, sca->addr.scsi.lun));
    377 	}
    378 #if defined(COMPAT_12) || defined(COMPAT_FREEBSD)
    379 	/* SCIOCREPROBE before ATAPI staff merge */
    380 	case OSCIOCREPROBE: {
    381 		struct oscsi_addr *sca = (struct oscsi_addr *)addr;
    382 
    383 		return (scsi_probe_busses(sca->scbus, sca->target, sca->lun));
    384 	}
    385 #endif
    386 #endif
    387 	case SCIOCRECONFIG:
    388 	case SCIOCDECONFIG:
    389 		return (EINVAL);
    390 	case SCIOCIDENTIFY: {
    391 		struct scsi_addr *sca = (struct scsi_addr *)addr;
    392 
    393 		switch (sc_link->type) {
    394 		case BUS_SCSI:
    395 			sca->type = TYPE_SCSI;
    396 			sca->addr.scsi.scbus = sc_link->scsipi_scsi.scsibus;
    397 			sca->addr.scsi.target = sc_link->scsipi_scsi.target;
    398 			sca->addr.scsi.lun = sc_link->scsipi_scsi.lun;
    399 			return (0);
    400 		case BUS_ATAPI:
    401 			sca->type = TYPE_ATAPI;
    402 			sca->addr.atapi.atbus = sc_link->scsipi_atapi.atapibus;
    403 			sca->addr.atapi.drive = sc_link->scsipi_atapi.drive;
    404 			return (0);
    405 		}
    406 		return (ENXIO);
    407 	}
    408 #if defined(COMPAT_12) || defined(COMPAT_FREEBSD)
    409 	/* SCIOCIDENTIFY before ATAPI staff merge */
    410 	case OSCIOCIDENTIFY: {
    411 		struct oscsi_addr *sca = (struct oscsi_addr *)addr;
    412 
    413 		switch (sc_link->type) {
    414 		case BUS_SCSI:
    415 			sca->scbus = sc_link->scsipi_scsi.scsibus;
    416 			sca->target = sc_link->scsipi_scsi.target;
    417 			sca->lun = sc_link->scsipi_scsi.lun;
    418 			return (0);
    419 		}
    420 		return (ENODEV);
    421 	}
    422 #endif
    423 	default:
    424 		return (ENOTTY);
    425 	}
    426 
    427 #ifdef DIAGNOSTIC
    428 	panic("scsipi_do_ioctl: impossible");
    429 #endif
    430 }
    431