Home | History | Annotate | Line # | Download | only in scsipi
scsipi_ioctl.c revision 1.12
      1  1.12  mycroft /*	$NetBSD: scsipi_ioctl.c,v 1.12 1994/12/01 12:04:45 mycroft Exp $	*/
      2   1.7      cgd 
      3   1.4  mycroft /*
      4   1.4  mycroft  * Copyright (c) 1994 Charles Hannum.  All rights reserved.
      5   1.4  mycroft  *
      6   1.4  mycroft  * Redistribution and use in source and binary forms, with or without
      7   1.4  mycroft  * modification, are permitted provided that the following conditions
      8   1.4  mycroft  * are met:
      9   1.4  mycroft  * 1. Redistributions of source code must retain the above copyright
     10   1.4  mycroft  *    notice, this list of conditions and the following disclaimer.
     11   1.4  mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.4  mycroft  *    notice, this list of conditions and the following disclaimer in the
     13   1.4  mycroft  *    documentation and/or other materials provided with the distribution.
     14   1.4  mycroft  * 3. All advertising materials mentioning features or use of this software
     15   1.4  mycroft  *    must display the following acknowledgement:
     16   1.4  mycroft  *	This product includes software developed by Charles Hannum.
     17   1.4  mycroft  * 4. The name of the author may not be used to endorse or promote products
     18   1.4  mycroft  *    derived from this software without specific prior written permission.
     19   1.4  mycroft  *
     20   1.4  mycroft  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21   1.4  mycroft  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22   1.4  mycroft  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23   1.4  mycroft  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24   1.4  mycroft  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25   1.4  mycroft  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26   1.4  mycroft  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27   1.4  mycroft  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28   1.4  mycroft  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29   1.4  mycroft  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30   1.4  mycroft  */
     31   1.4  mycroft 
     32   1.1  mycroft /*
     33   1.1  mycroft  * Contributed by HD Associates (hd (at) world.std.com).
     34   1.1  mycroft  * Copyright (c) 1992, 1993 HD Associates
     35   1.1  mycroft  *
     36   1.1  mycroft  * Berkeley style copyright.
     37   1.1  mycroft  */
     38   1.2  mycroft 
     39   1.1  mycroft #include <sys/types.h>
     40   1.1  mycroft #include <sys/errno.h>
     41   1.1  mycroft #include <sys/param.h>
     42   1.1  mycroft #include <sys/malloc.h>
     43   1.1  mycroft #include <sys/buf.h>
     44   1.1  mycroft #include <sys/proc.h>
     45   1.1  mycroft #include <sys/device.h>
     46   1.1  mycroft 
     47   1.1  mycroft #include <scsi/scsi_all.h>
     48   1.1  mycroft #include <scsi/scsiconf.h>
     49   1.1  mycroft #include <sys/scsiio.h>
     50   1.1  mycroft 
     51   1.4  mycroft struct scsi_ioctl {
     52  1.11  mycroft 	LIST_ENTRY(scsi_ioctl) si_list;
     53  1.11  mycroft 	struct buf si_bp;
     54  1.11  mycroft 	struct uio si_uio;
     55  1.11  mycroft 	struct iovec si_iov;
     56   1.9  mycroft 	scsireq_t si_screq;
     57   1.4  mycroft 	struct scsi_link *si_sc_link;
     58  1.11  mycroft };
     59  1.11  mycroft 
     60  1.11  mycroft LIST_HEAD(, scsi_ioctl) si_head;
     61   1.4  mycroft 
     62   1.4  mycroft struct scsi_ioctl *
     63  1.11  mycroft si_get()
     64   1.4  mycroft {
     65   1.4  mycroft 	struct scsi_ioctl *si;
     66  1.11  mycroft 	int s;
     67   1.4  mycroft 
     68  1.11  mycroft 	si = malloc(sizeof(struct scsi_ioctl), M_TEMP, M_WAITOK);
     69  1.11  mycroft 	bzero(si, sizeof(struct scsi_ioctl));
     70  1.11  mycroft 	s = splbio();
     71  1.11  mycroft 	LIST_INSERT_HEAD(&si_head, si, si_list);
     72  1.11  mycroft 	splx(s);
     73  1.11  mycroft 	return (si);
     74   1.4  mycroft }
     75   1.4  mycroft 
     76   1.4  mycroft void
     77   1.4  mycroft si_free(si)
     78   1.4  mycroft 	struct scsi_ioctl *si;
     79   1.4  mycroft {
     80  1.11  mycroft 	int s;
     81   1.4  mycroft 
     82  1.11  mycroft 	s = splbio();
     83  1.11  mycroft 	LIST_REMOVE(si, si_list);
     84   1.4  mycroft 	splx(s);
     85  1.11  mycroft 	free(si, M_TEMP);
     86   1.4  mycroft }
     87   1.4  mycroft 
     88   1.4  mycroft struct scsi_ioctl *
     89   1.4  mycroft si_find(bp)
     90   1.4  mycroft 	struct buf *bp;
     91   1.4  mycroft {
     92   1.4  mycroft 	struct scsi_ioctl *si;
     93  1.11  mycroft 	int s;
     94   1.4  mycroft 
     95  1.11  mycroft 	s = splbio();
     96  1.11  mycroft 	for (si = si_head.lh_first; si != 0; si = si->si_list.le_next)
     97  1.11  mycroft 		if (bp == &si->si_bp)
     98  1.11  mycroft 			break;
     99   1.4  mycroft 	splx(s);
    100  1.11  mycroft 	return (si);
    101   1.4  mycroft }
    102   1.1  mycroft 
    103   1.1  mycroft /*
    104   1.1  mycroft  * We let the user interpret his own sense in the generic scsi world.
    105   1.1  mycroft  * This routine is called at interrupt time if the SCSI_USER bit was set
    106   1.1  mycroft  * in the flags passed to scsi_scsi_cmd(). No other completion processing
    107   1.1  mycroft  * takes place, even if we are running over another device driver.
    108   1.1  mycroft  * The lower level routines that call us here, will free the xs and restart
    109   1.1  mycroft  * the device's queue if such exists.
    110   1.1  mycroft  */
    111   1.2  mycroft void
    112   1.2  mycroft scsi_user_done(xs)
    113   1.2  mycroft 	struct scsi_xfer *xs;
    114   1.1  mycroft {
    115   1.2  mycroft 	struct buf *bp;
    116   1.1  mycroft 	scsireq_t *screq;
    117   1.4  mycroft 	struct scsi_ioctl *si;
    118   1.1  mycroft 
    119   1.1  mycroft 	bp = xs->bp;
    120   1.2  mycroft 	if (!bp) {	/* ALL user requests must have a buf */
    121   1.1  mycroft 		sc_print_addr(xs->sc_link);
    122   1.1  mycroft 		printf("User command with no buf\n");
    123   1.2  mycroft 		return;
    124   1.1  mycroft 	}
    125   1.4  mycroft 	si = si_find(bp);
    126   1.4  mycroft 	if (!si) {
    127   1.4  mycroft 		sc_print_addr(xs->sc_link);
    128   1.4  mycroft 		printf("User command with no ioctl\n");
    129   1.4  mycroft 		return;
    130   1.4  mycroft 	}
    131   1.9  mycroft 	screq = &si->si_screq;
    132   1.1  mycroft 
    133   1.2  mycroft 	SC_DEBUG(xs->sc_link, SDEV_DB2, ("user-done\n"));
    134   1.1  mycroft 	screq->retsts = 0;
    135   1.1  mycroft 	screq->status = xs->status;
    136   1.9  mycroft 	switch (xs->error) {
    137   1.2  mycroft 	case XS_NOERROR:
    138   1.2  mycroft 		SC_DEBUG(xs->sc_link, SDEV_DB3, ("no error\n"));
    139   1.1  mycroft 		screq->datalen_used = xs->datalen - xs->resid; /* probably rubbish */
    140   1.1  mycroft 		screq->retsts = SCCMD_OK;
    141   1.1  mycroft 		break;
    142   1.2  mycroft 	case XS_SENSE:
    143   1.2  mycroft 		SC_DEBUG(xs->sc_link, SDEV_DB3, ("have sense\n"));
    144   1.2  mycroft 		screq->senselen_used = min(sizeof(xs->sense), SENSEBUFLEN);
    145   1.2  mycroft 		bcopy(&xs->sense, screq->sense, screq->senselen);
    146   1.1  mycroft 		screq->retsts = SCCMD_SENSE;
    147   1.1  mycroft 		break;
    148   1.2  mycroft 	case XS_DRIVER_STUFFUP:
    149   1.1  mycroft 		sc_print_addr(xs->sc_link);
    150   1.1  mycroft 		printf("host adapter code inconsistency\n");
    151   1.1  mycroft 		screq->retsts = SCCMD_UNKNOWN;
    152   1.1  mycroft 		break;
    153   1.2  mycroft 	case XS_TIMEOUT:
    154   1.2  mycroft 		SC_DEBUG(xs->sc_link, SDEV_DB3, ("timeout\n"));
    155   1.1  mycroft 		screq->retsts = SCCMD_TIMEOUT;
    156   1.1  mycroft 		break;
    157   1.2  mycroft 	case XS_BUSY:
    158   1.2  mycroft 		SC_DEBUG(xs->sc_link, SDEV_DB3, ("busy\n"));
    159   1.1  mycroft 		screq->retsts = SCCMD_BUSY;
    160   1.1  mycroft 		break;
    161   1.1  mycroft 	default:
    162   1.1  mycroft 		sc_print_addr(xs->sc_link);
    163   1.1  mycroft 		printf("unknown error category from host adapter code\n");
    164   1.1  mycroft 		screq->retsts = SCCMD_UNKNOWN;
    165   1.1  mycroft 		break;
    166   1.1  mycroft 	}
    167   1.1  mycroft 	biodone(bp); 	/* we're waiting on it in scsi_strategy() */
    168   1.1  mycroft }
    169   1.1  mycroft 
    170   1.1  mycroft 
    171   1.1  mycroft /* Pseudo strategy function
    172   1.1  mycroft  * Called by scsi_do_ioctl() via physio/physstrat if there is to
    173   1.1  mycroft  * be data transfered, and directly if there is no data transfer.
    174   1.1  mycroft  *
    175   1.1  mycroft  * Should I reorganize this so it returns to physio instead
    176   1.1  mycroft  * of sleeping in scsiio_scsi_cmd?  Is there any advantage, other
    177   1.1  mycroft  * than avoiding the probable duplicate wakeup in iodone? [PD]
    178   1.1  mycroft  *
    179   1.1  mycroft  * No, seems ok to me... [JRE]
    180   1.1  mycroft  * (I don't see any duplicate wakeups)
    181   1.1  mycroft  *
    182   1.1  mycroft  * Can't be used with block devices or raw_read/raw_write directly
    183   1.1  mycroft  * from the cdevsw/bdevsw tables because they couldn't have added
    184   1.1  mycroft  * the screq structure. [JRE]
    185   1.1  mycroft  */
    186   1.4  mycroft void
    187   1.4  mycroft scsistrategy(bp)
    188   1.4  mycroft 	struct buf *bp;
    189   1.1  mycroft {
    190  1.11  mycroft 	int error;
    191   1.4  mycroft 	struct scsi_ioctl *si;
    192   1.1  mycroft 	scsireq_t *screq;
    193   1.4  mycroft 	struct scsi_link *sc_link;
    194   1.5  mycroft 	int flags = 0;
    195   1.1  mycroft 	int s;
    196   1.1  mycroft 
    197   1.4  mycroft 	si = si_find(bp);
    198   1.4  mycroft 	if (!si) {
    199   1.4  mycroft 		printf("user_strat: No ioctl\n");
    200  1.12  mycroft 		error = EINVAL;
    201  1.12  mycroft 		goto bad;
    202   1.4  mycroft 	}
    203   1.9  mycroft 	screq = &si->si_screq;
    204   1.9  mycroft 
    205   1.4  mycroft 	sc_link = si->si_sc_link;
    206   1.2  mycroft 	if (!sc_link) {
    207   1.1  mycroft 		printf("user_strat: No link pointer\n");
    208  1.12  mycroft 		error = EINVAL;
    209  1.12  mycroft 		goto bad;
    210   1.1  mycroft 	}
    211   1.2  mycroft 	SC_DEBUG(sc_link, SDEV_DB2, ("user_strategy\n"));
    212   1.1  mycroft 
    213   1.4  mycroft 	/*
    214   1.4  mycroft 	 * We're in trouble if physio tried to break up the transfer.
    215   1.1  mycroft 	 */
    216   1.1  mycroft 	if (bp->b_bcount != screq->datalen) {
    217   1.1  mycroft 		sc_print_addr(sc_link);
    218   1.1  mycroft 		printf("physio split the request.. cannot proceed\n");
    219  1.12  mycroft 		error = EIO;
    220  1.12  mycroft 		goto bad;
    221   1.1  mycroft 	}
    222   1.1  mycroft 
    223   1.1  mycroft 	if (screq->timeout == 0) {
    224  1.12  mycroft 		error = EINVAL;
    225  1.12  mycroft 		goto bad;
    226   1.1  mycroft 	}
    227   1.1  mycroft 
    228   1.1  mycroft 	if (screq->cmdlen > sizeof(struct scsi_generic)) {
    229   1.1  mycroft 		sc_print_addr(sc_link);
    230   1.4  mycroft 		printf("cmdlen too big\n");
    231  1.12  mycroft 		error = EFAULT;
    232  1.12  mycroft 		goto bad;
    233   1.1  mycroft 	}
    234   1.1  mycroft 
    235   1.1  mycroft 	if (screq->flags & SCCMD_READ)
    236   1.1  mycroft 		flags |= SCSI_DATA_IN;
    237   1.1  mycroft 	if (screq->flags & SCCMD_WRITE)
    238   1.1  mycroft 		flags |= SCSI_DATA_OUT;
    239   1.1  mycroft 	if (screq->flags & SCCMD_TARGET)
    240   1.1  mycroft 		flags |= SCSI_TARGET;
    241   1.1  mycroft 	if (screq->flags & SCCMD_ESCAPE)
    242   1.1  mycroft 		flags |= SCSI_ESCAPE;
    243   1.2  mycroft 
    244  1.11  mycroft 	error = scsi_scsi_cmd(sc_link, (struct scsi_generic *)screq->cmd,
    245   1.6  mycroft 	    screq->cmdlen, (u_char *)bp->b_data, screq->datalen,
    246   1.4  mycroft 	    0, /* user must do the retries *//* ignored */
    247   1.4  mycroft 	    screq->timeout, bp, flags | SCSI_USER);
    248   1.1  mycroft 
    249   1.2  mycroft 	/* because there is a bp, scsi_scsi_cmd will return immediatly */
    250  1.12  mycroft 	if (error)
    251  1.12  mycroft 		goto bad;
    252  1.12  mycroft 
    253  1.11  mycroft 	SC_DEBUG(sc_link, SDEV_DB3, ("about to sleep\n"));
    254   1.1  mycroft 	s = splbio();
    255  1.12  mycroft 	while ((bp->b_flags & B_DONE) == 0)
    256   1.2  mycroft 		tsleep(bp, PRIBIO, "scistr", 0);
    257   1.1  mycroft 	splx(s);
    258   1.2  mycroft 	SC_DEBUG(sc_link, SDEV_DB3, ("back from sleep\n"));
    259  1.12  mycroft 
    260  1.12  mycroft 	return;
    261  1.12  mycroft 
    262  1.12  mycroft bad:
    263  1.12  mycroft 	bp->b_flags |= B_ERROR;
    264  1.12  mycroft 	bp->b_error = error;
    265  1.12  mycroft 	biodone(bp);
    266   1.1  mycroft }
    267   1.1  mycroft 
    268   1.1  mycroft /*
    269   1.1  mycroft  * Something (e.g. another driver) has called us
    270   1.1  mycroft  * with an sc_link for a target/lun/adapter, and a scsi
    271   1.1  mycroft  * specific ioctl to perform, better try.
    272   1.1  mycroft  * If user-level type command, we must still be running
    273   1.1  mycroft  * in the context of the calling process
    274   1.1  mycroft  */
    275   1.2  mycroft int
    276   1.8  mycroft scsi_do_ioctl(sc_link, dev, cmd, addr, f)
    277   1.4  mycroft 	struct scsi_link *sc_link;
    278   1.8  mycroft 	dev_t dev;
    279  1.10      cgd 	u_long cmd;
    280   1.4  mycroft 	caddr_t addr;
    281   1.4  mycroft 	int f;
    282   1.1  mycroft {
    283   1.4  mycroft 	int error;
    284   1.1  mycroft 
    285  1.10      cgd 	SC_DEBUG(sc_link, SDEV_DB2, ("scsi_do_ioctl(0x%lx)\n", cmd));
    286  1.11  mycroft 
    287   1.2  mycroft 	switch(cmd) {
    288   1.4  mycroft 	case SCIOCCOMMAND: {
    289   1.4  mycroft 		/*
    290   1.4  mycroft 		 * You won't believe this, but the arg copied in
    291   1.4  mycroft  		 * from the user space, is on the kernel stack
    292   1.4  mycroft 		 * for this process, so we can't write
    293   1.4  mycroft 		 * to it at interrupt time..
    294   1.4  mycroft 		 * we need to copy it in and out!
    295   1.4  mycroft 		 * Make a static copy using malloc!
    296   1.4  mycroft 		 */
    297   1.4  mycroft 		scsireq_t *screq = (scsireq_t *)addr;
    298   1.8  mycroft 		struct scsi_ioctl *si;
    299   1.4  mycroft 		int len;
    300   1.4  mycroft 
    301  1.11  mycroft 		si = si_get();
    302   1.9  mycroft 		si->si_screq = *screq;
    303   1.4  mycroft 		si->si_sc_link = sc_link;
    304  1.11  mycroft 		si->si_bp.b_bcount = len = screq->datalen;
    305   1.4  mycroft 		if (len) {
    306  1.11  mycroft 			si->si_iov.iov_base = screq->databuf;
    307   1.8  mycroft 			si->si_iov.iov_len = len;
    308   1.8  mycroft 			si->si_uio.uio_iov = &si->si_iov;
    309   1.8  mycroft 			si->si_uio.uio_iovcnt = 1;
    310   1.8  mycroft 			si->si_uio.uio_offset = 0;
    311   1.8  mycroft 			si->si_uio.uio_segflg = UIO_USERSPACE;
    312   1.8  mycroft 			si->si_uio.uio_rw =
    313   1.8  mycroft 			    (screq->flags & SCCMD_READ) ? UIO_READ : UIO_WRITE;
    314   1.8  mycroft 			si->si_uio.uio_procp = curproc;	/* XXX */
    315  1.11  mycroft 			error = physio(scsistrategy, &si->si_bp, dev,
    316   1.8  mycroft 			    (screq->flags & SCCMD_READ) ? B_READ : B_WRITE,
    317   1.8  mycroft 			    sc_link->adapter->scsi_minphys, &si->si_uio);
    318   1.4  mycroft 		} else {
    319   1.4  mycroft 			/* if no data, no need to translate it.. */
    320  1.11  mycroft 			si->si_bp.b_data = 0;
    321  1.11  mycroft 			si->si_bp.b_dev = -1; /* irrelevant info */
    322  1.11  mycroft 			si->si_bp.b_flags = 0;
    323  1.11  mycroft 			scsistrategy(&si->si_bp);
    324  1.11  mycroft 			error = si->si_bp.b_error;
    325   1.1  mycroft 		}
    326  1.11  mycroft 		si_free(si);
    327   1.4  mycroft 		return error;
    328   1.4  mycroft 	}
    329   1.4  mycroft 	case SCIOCDEBUG: {
    330   1.4  mycroft 		int level = *((int *)addr);
    331   1.1  mycroft 
    332   1.4  mycroft 		SC_DEBUG(sc_link, SDEV_DB3, ("debug set to %d\n", level));
    333   1.4  mycroft 		sc_link->flags &= ~SDEV_DBX; /* clear debug bits */
    334   1.4  mycroft 		if (level & 1)
    335   1.4  mycroft 			sc_link->flags |= SDEV_DB1;
    336   1.4  mycroft 		if (level & 2)
    337   1.4  mycroft 			sc_link->flags |= SDEV_DB2;
    338   1.4  mycroft 		if (level & 4)
    339   1.4  mycroft 			sc_link->flags |= SDEV_DB3;
    340   1.4  mycroft 		if (level & 8)
    341   1.4  mycroft 			sc_link->flags |= SDEV_DB4;
    342   1.4  mycroft 		return 0;
    343   1.4  mycroft 	}
    344   1.4  mycroft 	case SCIOCREPROBE: {
    345   1.4  mycroft 		struct scsi_addr *sca = (struct scsi_addr *)addr;
    346   1.1  mycroft 
    347   1.4  mycroft 		return scsi_probe_busses(sca->scbus, sca->target, sca->lun);
    348   1.4  mycroft 	}
    349   1.4  mycroft 	case SCIOCRECONFIG:
    350   1.4  mycroft 	case SCIOCDECONFIG:
    351   1.4  mycroft 		return EINVAL;
    352   1.4  mycroft 	case SCIOCIDENTIFY: {
    353   1.4  mycroft 		struct scsi_addr *sca = (struct scsi_addr *)addr;
    354   1.4  mycroft 
    355   1.4  mycroft 		sca->scbus = sc_link->scsibus;
    356   1.4  mycroft 		sca->target = sc_link->target;
    357   1.4  mycroft 		sca->lun = sc_link->lun;
    358   1.4  mycroft 		return 0;
    359   1.4  mycroft 	}
    360   1.4  mycroft 	default:
    361   1.4  mycroft 		return ENOTTY;
    362   1.1  mycroft 	}
    363   1.1  mycroft 
    364   1.4  mycroft #ifdef DIAGNOSTIC
    365   1.4  mycroft 	panic("scsi_do_ioctl: impossible");
    366   1.4  mycroft #endif
    367   1.1  mycroft }
    368