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