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