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