Home | History | Annotate | Line # | Download | only in scsipi
scsipi_ioctl.c revision 1.18
      1 /*	$NetBSD: scsipi_ioctl.c,v 1.18 1995/01/30 15:54:42 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 	struct scsi_ioctl *si;
    117 	scsireq_t *screq;
    118 	struct scsi_link *sc_link;
    119 
    120 	bp = xs->bp;
    121 	if (!bp) {	/* ALL user requests must have a buf */
    122 		sc_print_addr(xs->sc_link);
    123 		printf("User command with no buf\n");
    124 		return;
    125 	}
    126 	si = si_find(bp);
    127 	if (!si) {
    128 		sc_print_addr(xs->sc_link);
    129 		printf("User command with no ioctl\n");
    130 		return;
    131 	}
    132 	screq = &si->si_screq;
    133 	sc_link = si->si_sc_link;
    134 	SC_DEBUG(xs->sc_link, SDEV_DB2, ("user-done\n"));
    135 
    136 	screq->retsts = 0;
    137 	screq->status = xs->status;
    138 	switch (xs->error) {
    139 	case XS_NOERROR:
    140 		SC_DEBUG(sc_link, SDEV_DB3, ("no error\n"));
    141 		screq->datalen_used = xs->datalen - xs->resid; /* probably rubbish */
    142 		screq->retsts = SCCMD_OK;
    143 		break;
    144 	case XS_SENSE:
    145 		SC_DEBUG(sc_link, SDEV_DB3, ("have sense\n"));
    146 		screq->senselen_used = min(sizeof(xs->sense), SENSEBUFLEN);
    147 		bcopy(&xs->sense, screq->sense, screq->senselen);
    148 		screq->retsts = SCCMD_SENSE;
    149 		break;
    150 	case XS_DRIVER_STUFFUP:
    151 		sc_print_addr(sc_link);
    152 		printf("host adapter code inconsistency\n");
    153 		screq->retsts = SCCMD_UNKNOWN;
    154 		break;
    155 	case XS_TIMEOUT:
    156 		SC_DEBUG(sc_link, SDEV_DB3, ("timeout\n"));
    157 		screq->retsts = SCCMD_TIMEOUT;
    158 		break;
    159 	case XS_BUSY:
    160 		SC_DEBUG(sc_link, SDEV_DB3, ("busy\n"));
    161 		screq->retsts = SCCMD_BUSY;
    162 		break;
    163 	default:
    164 		sc_print_addr(sc_link);
    165 		printf("unknown error category from host adapter code\n");
    166 		screq->retsts = SCCMD_UNKNOWN;
    167 		break;
    168 	}
    169 	biodone(bp); 	/* we're waiting on it in scsi_strategy() */
    170 }
    171 
    172 
    173 /* Pseudo strategy function
    174  * Called by scsi_do_ioctl() via physio/physstrat if there is to
    175  * be data transfered, and directly if there is no data transfer.
    176  *
    177  * Should I reorganize this so it returns to physio instead
    178  * of sleeping in scsiio_scsi_cmd?  Is there any advantage, other
    179  * than avoiding the probable duplicate wakeup in iodone? [PD]
    180  *
    181  * No, seems ok to me... [JRE]
    182  * (I don't see any duplicate wakeups)
    183  *
    184  * Can't be used with block devices or raw_read/raw_write directly
    185  * from the cdevsw/bdevsw tables because they couldn't have added
    186  * the screq structure. [JRE]
    187  */
    188 void
    189 scsistrategy(bp)
    190 	struct buf *bp;
    191 {
    192 	struct scsi_ioctl *si;
    193 	scsireq_t *screq;
    194 	struct scsi_link *sc_link;
    195 	int error;
    196 	int flags = 0;
    197 	int s;
    198 
    199 	si = si_find(bp);
    200 	if (!si) {
    201 		printf("user_strat: No ioctl\n");
    202 		error = EINVAL;
    203 		goto bad;
    204 	}
    205 	screq = &si->si_screq;
    206 	sc_link = si->si_sc_link;
    207 	SC_DEBUG(sc_link, SDEV_DB2, ("user_strategy\n"));
    208 
    209 	/*
    210 	 * We're in trouble if physio tried to break up the transfer.
    211 	 */
    212 	if (bp->b_bcount != screq->datalen) {
    213 		sc_print_addr(sc_link);
    214 		printf("physio split the request.. cannot proceed\n");
    215 		error = EIO;
    216 		goto bad;
    217 	}
    218 
    219 	if (screq->timeout == 0) {
    220 		error = EINVAL;
    221 		goto bad;
    222 	}
    223 
    224 	if (screq->cmdlen > sizeof(struct scsi_generic)) {
    225 		sc_print_addr(sc_link);
    226 		printf("cmdlen too big\n");
    227 		error = EFAULT;
    228 		goto bad;
    229 	}
    230 
    231 	if (screq->flags & SCCMD_READ)
    232 		flags |= SCSI_DATA_IN;
    233 	if (screq->flags & SCCMD_WRITE)
    234 		flags |= SCSI_DATA_OUT;
    235 	if (screq->flags & SCCMD_TARGET)
    236 		flags |= SCSI_TARGET;
    237 	if (screq->flags & SCCMD_ESCAPE)
    238 		flags |= SCSI_ESCAPE;
    239 
    240 	error = scsi_scsi_cmd(sc_link, (struct scsi_generic *)screq->cmd,
    241 	    screq->cmdlen, (u_char *)bp->b_data, screq->datalen,
    242 	    0, /* user must do the retries *//* ignored */
    243 	    screq->timeout, bp, flags | SCSI_USER | SCSI_NOSLEEP);
    244 
    245 	/* because there is a bp, scsi_scsi_cmd will return immediatly */
    246 	if (error)
    247 		goto bad;
    248 
    249 	SC_DEBUG(sc_link, SDEV_DB3, ("about to sleep\n"));
    250 	s = splbio();
    251 	while ((bp->b_flags & B_DONE) == 0)
    252 		tsleep(bp, PRIBIO, "scistr", 0);
    253 	splx(s);
    254 	SC_DEBUG(sc_link, SDEV_DB3, ("back from sleep\n"));
    255 
    256 	return;
    257 
    258 bad:
    259 	bp->b_flags |= B_ERROR;
    260 	bp->b_error = error;
    261 	biodone(bp);
    262 }
    263 
    264 /*
    265  * Something (e.g. another driver) has called us
    266  * with an sc_link for a target/lun/adapter, and a scsi
    267  * specific ioctl to perform, better try.
    268  * If user-level type command, we must still be running
    269  * in the context of the calling process
    270  */
    271 int
    272 scsi_do_ioctl(sc_link, dev, cmd, addr, flag, p)
    273 	struct scsi_link *sc_link;
    274 	dev_t dev;
    275 	u_long cmd;
    276 	caddr_t addr;
    277 	int flag;
    278 	struct proc *p;
    279 {
    280 	int error;
    281 
    282 	SC_DEBUG(sc_link, SDEV_DB2, ("scsi_do_ioctl(0x%lx)\n", cmd));
    283 
    284 	switch(cmd) {
    285 	case SCIOCCOMMAND: {
    286 		scsireq_t *screq = (scsireq_t *)addr;
    287 		struct scsi_ioctl *si;
    288 		int len;
    289 
    290 		si = si_get();
    291 		si->si_screq = *screq;
    292 		si->si_sc_link = sc_link;
    293 		len = screq->datalen;
    294 		if (len) {
    295 			si->si_iov.iov_base = screq->databuf;
    296 			si->si_iov.iov_len = len;
    297 			si->si_uio.uio_iov = &si->si_iov;
    298 			si->si_uio.uio_iovcnt = 1;
    299 			si->si_uio.uio_resid = len;
    300 			si->si_uio.uio_offset = 0;
    301 			si->si_uio.uio_segflg = UIO_USERSPACE;
    302 			si->si_uio.uio_rw =
    303 			    (screq->flags & SCCMD_READ) ? UIO_READ : UIO_WRITE;
    304 			si->si_uio.uio_procp = p;
    305 			error = physio(scsistrategy, &si->si_bp, dev,
    306 			    (screq->flags & SCCMD_READ) ? B_READ : B_WRITE,
    307 			    sc_link->adapter->scsi_minphys, &si->si_uio);
    308 		} else {
    309 			/* if no data, no need to translate it.. */
    310 			si->si_bp.b_flags = 0;
    311 			si->si_bp.b_data = 0;
    312 			si->si_bp.b_bcount = 0;
    313 			si->si_bp.b_dev = dev;
    314 			si->si_bp.b_proc = p;
    315 			scsistrategy(&si->si_bp);
    316 			error = si->si_bp.b_error;
    317 		}
    318 		*screq = si->si_screq;
    319 		si_free(si);
    320 		return error;
    321 	}
    322 	case SCIOCDEBUG: {
    323 		int level = *((int *)addr);
    324 
    325 		SC_DEBUG(sc_link, SDEV_DB3, ("debug set to %d\n", level));
    326 		sc_link->flags &= ~SDEV_DBX; /* clear debug bits */
    327 		if (level & 1)
    328 			sc_link->flags |= SDEV_DB1;
    329 		if (level & 2)
    330 			sc_link->flags |= SDEV_DB2;
    331 		if (level & 4)
    332 			sc_link->flags |= SDEV_DB3;
    333 		if (level & 8)
    334 			sc_link->flags |= SDEV_DB4;
    335 		return 0;
    336 	}
    337 	case SCIOCREPROBE: {
    338 		struct scsi_addr *sca = (struct scsi_addr *)addr;
    339 
    340 		return scsi_probe_busses(sca->scbus, sca->target, sca->lun);
    341 	}
    342 	case SCIOCRECONFIG:
    343 	case SCIOCDECONFIG:
    344 		return EINVAL;
    345 	case SCIOCIDENTIFY: {
    346 		struct scsi_addr *sca = (struct scsi_addr *)addr;
    347 
    348 		sca->scbus = sc_link->scsibus;
    349 		sca->target = sc_link->target;
    350 		sca->lun = sc_link->lun;
    351 		return 0;
    352 	}
    353 	default:
    354 		return ENOTTY;
    355 	}
    356 
    357 #ifdef DIAGNOSTIC
    358 	panic("scsi_do_ioctl: impossible");
    359 #endif
    360 }
    361