Home | History | Annotate | Line # | Download | only in scsipi
scsipi_ioctl.c revision 1.8
      1 /*	$NetBSD: scsipi_ioctl.c,v 1.8 1994/10/20 20:31:27 mycroft 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/malloc.h>
     43 #include <sys/buf.h>
     44 #include <sys/proc.h>
     45 #include <sys/device.h>
     46 
     47 #include <scsi/scsi_all.h>
     48 #include <scsi/scsiconf.h>
     49 #include <sys/scsiio.h>
     50 
     51 void scsierr __P((struct buf *, int));
     52 
     53 /*
     54  * We need to maintain an assocation between the buf and the SCSI request
     55  * structure.  We do this with a simple array of scsi_ioctl structures below.
     56  * XXX The free structures should probably be in a linked list.
     57  */
     58 #define NIOCTL	16
     59 
     60 struct scsi_ioctl {
     61 	struct buf *si_bp;
     62 	scsireq_t *si_screq;
     63 	struct scsi_link *si_sc_link;
     64 	struct uio si_uio;
     65 	struct iovec si_iov;
     66 } scsi_ioctl[NIOCTL];
     67 
     68 struct scsi_ioctl *
     69 si_get(bp)
     70 	struct buf *bp;
     71 {
     72 	int s = splbio();
     73 	struct scsi_ioctl *si;
     74 	int error;
     75 
     76 	for (;;) {
     77 		for (si = scsi_ioctl; si < &scsi_ioctl[NIOCTL]; si++)
     78 			if (si->si_bp == 0) {
     79 				si->si_bp = bp;
     80 				splx(s);
     81 				return si;
     82 			}
     83 		error = tsleep(scsi_ioctl, PRIBIO | PCATCH, "siget", 0);
     84 		if (error) {
     85 			splx(s);
     86 			return 0;
     87 		}
     88 	}
     89 }
     90 
     91 void
     92 si_free(si)
     93 	struct scsi_ioctl *si;
     94 {
     95 	int s = splbio();
     96 
     97 	si->si_bp = 0;
     98 	wakeup(scsi_ioctl);
     99 	splx(s);
    100 }
    101 
    102 struct scsi_ioctl *
    103 si_find(bp)
    104 	struct buf *bp;
    105 {
    106 	int s = splbio();
    107 	struct scsi_ioctl *si;
    108 
    109 	for (si = scsi_ioctl; si < &scsi_ioctl[NIOCTL]; si++)
    110 		if (si->si_bp == bp) {
    111 			splx(s);
    112 			return si;
    113 		}
    114 	splx(s);
    115 	return 0;
    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_scsi_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 scsi_user_done(xs)
    128 	struct scsi_xfer *xs;
    129 {
    130 	struct buf *bp;
    131 	scsireq_t *screq;
    132 	struct scsi_ioctl *si;
    133 
    134 	bp = xs->bp;
    135 	if (!bp) {	/* ALL user requests must have a buf */
    136 		sc_print_addr(xs->sc_link);
    137 		printf("User command with no buf\n");
    138 		return;
    139 	}
    140 	si = si_find(bp);
    141 	if (!si) {
    142 		sc_print_addr(xs->sc_link);
    143 		printf("User command with no ioctl\n");
    144 		return;
    145 	}
    146 	screq = si->si_screq;
    147 	si_free(si);
    148 	if (!screq) {	/* Is it one of ours? (the SCSI_USER bit says it is) */
    149 		sc_print_addr(xs->sc_link);
    150 		printf("User command with no request\n");
    151 		return;
    152 	}
    153 
    154 	SC_DEBUG(xs->sc_link, SDEV_DB2, ("user-done\n"));
    155 	screq->retsts = 0;
    156 	screq->status = xs->status;
    157 	switch(xs->error) {
    158 	case XS_NOERROR:
    159 		SC_DEBUG(xs->sc_link, SDEV_DB3, ("no error\n"));
    160 		screq->datalen_used = xs->datalen - xs->resid; /* probably rubbish */
    161 		screq->retsts = SCCMD_OK;
    162 		break;
    163 	case XS_SENSE:
    164 		SC_DEBUG(xs->sc_link, SDEV_DB3, ("have sense\n"));
    165 		screq->senselen_used = min(sizeof(xs->sense), SENSEBUFLEN);
    166 		bcopy(&xs->sense, screq->sense, screq->senselen);
    167 		screq->retsts = SCCMD_SENSE;
    168 		break;
    169 	case XS_DRIVER_STUFFUP:
    170 		sc_print_addr(xs->sc_link);
    171 		printf("host adapter code inconsistency\n");
    172 		screq->retsts = SCCMD_UNKNOWN;
    173 		break;
    174 	case XS_TIMEOUT:
    175 		SC_DEBUG(xs->sc_link, SDEV_DB3, ("timeout\n"));
    176 		screq->retsts = SCCMD_TIMEOUT;
    177 		break;
    178 	case XS_BUSY:
    179 		SC_DEBUG(xs->sc_link, SDEV_DB3, ("busy\n"));
    180 		screq->retsts = SCCMD_BUSY;
    181 		break;
    182 	default:
    183 		sc_print_addr(xs->sc_link);
    184 		printf("unknown error category from host adapter code\n");
    185 		screq->retsts = SCCMD_UNKNOWN;
    186 		break;
    187 	}
    188 	biodone(bp); 	/* we're waiting on it in scsi_strategy() */
    189 	return;		/* it'll free the xs and restart any queue */
    190 }
    191 
    192 
    193 /* Pseudo strategy function
    194  * Called by scsi_do_ioctl() via physio/physstrat if there is to
    195  * be data transfered, and directly if there is no data transfer.
    196  *
    197  * Should I reorganize this so it returns to physio instead
    198  * of sleeping in scsiio_scsi_cmd?  Is there any advantage, other
    199  * than avoiding the probable duplicate wakeup in iodone? [PD]
    200  *
    201  * No, seems ok to me... [JRE]
    202  * (I don't see any duplicate wakeups)
    203  *
    204  * Can't be used with block devices or raw_read/raw_write directly
    205  * from the cdevsw/bdevsw tables because they couldn't have added
    206  * the screq structure. [JRE]
    207  */
    208 void
    209 scsistrategy(bp)
    210 	struct buf *bp;
    211 {
    212 	int err;
    213 	struct scsi_ioctl *si;
    214 	scsireq_t *screq;
    215 	struct scsi_link *sc_link;
    216 	int flags = 0;
    217 	int s;
    218 
    219 	si = si_find(bp);
    220 	if (!si) {
    221 		printf("user_strat: No ioctl\n");
    222 		scsierr(bp, EINVAL);
    223 		return;
    224 	}
    225 	screq = si->si_screq;
    226 	sc_link = si->si_sc_link;
    227 	si_free(si);
    228 	if (!sc_link) {
    229 		printf("user_strat: No link pointer\n");
    230 		scsierr(bp, EINVAL);
    231 		return;
    232 	}
    233 	SC_DEBUG(sc_link, SDEV_DB2, ("user_strategy\n"));
    234 	if (!screq) {
    235 		sc_print_addr(sc_link);
    236 		printf("No request block\n");
    237 		scsierr(bp, EINVAL);
    238 		return;
    239 	}
    240 
    241 	/*
    242 	 * We're in trouble if physio tried to break up the transfer.
    243 	 */
    244 	if (bp->b_bcount != screq->datalen) {
    245 		sc_print_addr(sc_link);
    246 		printf("physio split the request.. cannot proceed\n");
    247 		scsierr(bp, EIO);
    248 		return;
    249 	}
    250 
    251 	if (screq->timeout == 0) {
    252 		scsierr(bp, EINVAL);
    253 		return;
    254 	}
    255 
    256 	if (screq->cmdlen > sizeof(struct scsi_generic)) {
    257 		sc_print_addr(sc_link);
    258 		printf("cmdlen too big\n");
    259 		scsierr(bp, EFAULT);
    260 		return;
    261 	}
    262 
    263 	if (screq->flags & SCCMD_READ)
    264 		flags |= SCSI_DATA_IN;
    265 	if (screq->flags & SCCMD_WRITE)
    266 		flags |= SCSI_DATA_OUT;
    267 	if (screq->flags & SCCMD_TARGET)
    268 		flags |= SCSI_TARGET;
    269 	if (screq->flags & SCCMD_ESCAPE)
    270 		flags |= SCSI_ESCAPE;
    271 
    272 	err = scsi_scsi_cmd(sc_link, (struct scsi_generic *)screq->cmd,
    273 	    screq->cmdlen, (u_char *)bp->b_data, screq->datalen,
    274 	    0, /* user must do the retries *//* ignored */
    275 	    screq->timeout, bp, flags | SCSI_USER);
    276 
    277 	/* because there is a bp, scsi_scsi_cmd will return immediatly */
    278 	if (err) {
    279 		scsierr(bp, err);
    280 		return;
    281 	}
    282 	SC_DEBUG(sc_link, SDEV_DB3, ("about to  sleep\n"));
    283 	s = splbio();
    284 	while (!(bp->b_flags & B_DONE))
    285 		tsleep(bp, PRIBIO, "scistr", 0);
    286 	splx(s);
    287 	SC_DEBUG(sc_link, SDEV_DB3, ("back from sleep\n"));
    288 	return;
    289 }
    290 
    291 /*
    292  * Something (e.g. another driver) has called us
    293  * with an sc_link for a target/lun/adapter, and a scsi
    294  * specific ioctl to perform, better try.
    295  * If user-level type command, we must still be running
    296  * in the context of the calling process
    297  */
    298 int
    299 scsi_do_ioctl(sc_link, dev, cmd, addr, f)
    300 	struct scsi_link *sc_link;
    301 	dev_t dev;
    302 	int cmd;
    303 	caddr_t addr;
    304 	int f;
    305 {
    306 	int error;
    307 
    308 	SC_DEBUG(sc_link, SDEV_DB2, ("scsi_do_ioctl(0x%x)\n", cmd));
    309 	switch(cmd) {
    310 	case SCIOCCOMMAND: {
    311 		/*
    312 		 * You won't believe this, but the arg copied in
    313  		 * from the user space, is on the kernel stack
    314 		 * for this process, so we can't write
    315 		 * to it at interrupt time..
    316 		 * we need to copy it in and out!
    317 		 * Make a static copy using malloc!
    318 		 */
    319 		scsireq_t *screq = (scsireq_t *)addr;
    320 		struct buf *bp;
    321 		struct scsi_ioctl *si;
    322 		caddr_t	daddr;
    323 		int len;
    324 
    325 		bp = malloc(sizeof(struct buf), M_TEMP, M_WAITOK);
    326 		bzero(bp, sizeof(struct buf));
    327 		daddr = screq->databuf;
    328 		bp->b_bcount = len = screq->datalen;
    329 		si = si_get(bp);
    330 		if (!si) {
    331 			scsierr(bp, EINTR);
    332 			return EINTR;
    333 		}
    334 		si->si_screq = screq;
    335 		si->si_sc_link = sc_link;
    336 		if (len) {
    337 			si->si_iov.iov_base = daddr;
    338 			si->si_iov.iov_len = len;
    339 			si->si_uio.uio_iov = &si->si_iov;
    340 			si->si_uio.uio_iovcnt = 1;
    341 			si->si_uio.uio_offset = 0;
    342 			si->si_uio.uio_segflg = UIO_USERSPACE;
    343 			si->si_uio.uio_rw =
    344 			    (screq->flags & SCCMD_READ) ? UIO_READ : UIO_WRITE;
    345 			si->si_uio.uio_procp = curproc;	/* XXX */
    346 			error = physio(scsistrategy, bp, dev,
    347 			    (screq->flags & SCCMD_READ) ? B_READ : B_WRITE,
    348 			    sc_link->adapter->scsi_minphys, &si->si_uio);
    349 		} else {
    350 			/* if no data, no need to translate it.. */
    351 			bp->b_data = 0;
    352 			bp->b_dev = -1; /* irrelevant info */
    353 			bp->b_flags = 0;
    354 			scsistrategy(bp);
    355 			error = bp->b_error;
    356 		}
    357 		free(bp, M_TEMP);
    358 		return error;
    359 	}
    360 	case SCIOCDEBUG: {
    361 		int level = *((int *)addr);
    362 
    363 		SC_DEBUG(sc_link, SDEV_DB3, ("debug set to %d\n", level));
    364 		sc_link->flags &= ~SDEV_DBX; /* clear debug bits */
    365 		if (level & 1)
    366 			sc_link->flags |= SDEV_DB1;
    367 		if (level & 2)
    368 			sc_link->flags |= SDEV_DB2;
    369 		if (level & 4)
    370 			sc_link->flags |= SDEV_DB3;
    371 		if (level & 8)
    372 			sc_link->flags |= SDEV_DB4;
    373 		return 0;
    374 	}
    375 	case SCIOCREPROBE: {
    376 		struct scsi_addr *sca = (struct scsi_addr *)addr;
    377 
    378 		return scsi_probe_busses(sca->scbus, sca->target, sca->lun);
    379 	}
    380 	case SCIOCRECONFIG:
    381 	case SCIOCDECONFIG:
    382 		return EINVAL;
    383 	case SCIOCIDENTIFY: {
    384 		struct scsi_addr *sca = (struct scsi_addr *)addr;
    385 
    386 		sca->scbus = sc_link->scsibus;
    387 		sca->target = sc_link->target;
    388 		sca->lun = sc_link->lun;
    389 		return 0;
    390 	}
    391 	default:
    392 		return ENOTTY;
    393 	}
    394 
    395 #ifdef DIAGNOSTIC
    396 	panic("scsi_do_ioctl: impossible");
    397 #endif
    398 }
    399 
    400 void
    401 scsierr(bp, error)
    402 	struct buf *bp;
    403 	int error;
    404 {
    405 
    406 	bp->b_flags |= B_ERROR;
    407 	bp->b_error = error;
    408 	biodone(bp);
    409 	return;
    410 }
    411