NetBSD/sys/scsi/scsi_ioctl.c

423 lines
10 KiB
C
Raw Normal View History

/*
* Copyright (c) 1994 Charles Hannum. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Charles Hannum.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: scsi_ioctl.c,v 1.4 1994/03/29 04:29:35 mycroft Exp $
*/
1993-11-24 07:52:44 +03:00
/*
* Contributed by HD Associates (hd@world.std.com).
* Copyright (c) 1992, 1993 HD Associates
*
* Berkeley style copyright.
*/
1993-11-24 12:45:04 +03:00
1993-11-24 07:52:44 +03:00
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/buf.h>
#include <sys/proc.h>
#include <sys/device.h>
#include <scsi/scsi_all.h>
#include <scsi/scsiconf.h>
#include <sys/scsiio.h>
void scsierr __P((struct buf *, int));
/*
* We need to maintain an assocation between the buf and the SCSI request
* structure. We do this with a simple array of scsi_ioctl structures below.
* XXX The free structures should probably be in a linked list.
*/
#define NIOCTL 16
struct scsi_ioctl {
struct buf *si_bp;
scsireq_t *si_screq;
struct scsi_link *si_sc_link;
} scsi_ioctl[NIOCTL];
struct scsi_ioctl *
si_get(bp)
struct buf *bp;
{
int s = splbio();
struct scsi_ioctl *si;
int error;
for (;;) {
for (si = scsi_ioctl; si < &scsi_ioctl[NIOCTL]; si++)
if (si->si_bp == 0) {
si->si_bp = bp;
splx(s);
return si;
}
error = tsleep(scsi_ioctl, PRIBIO | PCATCH, "siget", 0);
if (error) {
splx(s);
return 0;
}
}
}
void
si_free(si)
struct scsi_ioctl *si;
{
int s = splbio();
si->si_bp = 0;
wakeup(scsi_ioctl);
splx(s);
}
struct scsi_ioctl *
si_find(bp)
struct buf *bp;
{
int s = splbio();
struct scsi_ioctl *si;
for (si = scsi_ioctl; si < &scsi_ioctl[NIOCTL]; si++)
if (si->si_bp == bp) {
splx(s);
return si;
}
splx(s);
return 0;
}
1993-11-24 07:52:44 +03:00
/*
* We let the user interpret his own sense in the generic scsi world.
* This routine is called at interrupt time if the SCSI_USER bit was set
* in the flags passed to scsi_scsi_cmd(). No other completion processing
* takes place, even if we are running over another device driver.
* The lower level routines that call us here, will free the xs and restart
* the device's queue if such exists.
*/
1993-11-24 12:45:04 +03:00
void
scsi_user_done(xs)
struct scsi_xfer *xs;
{
struct buf *bp;
1993-11-24 07:52:44 +03:00
scsireq_t *screq;
struct scsi_ioctl *si;
1993-11-24 07:52:44 +03:00
bp = xs->bp;
1993-11-24 12:45:04 +03:00
if (!bp) { /* ALL user requests must have a buf */
1993-11-24 07:52:44 +03:00
sc_print_addr(xs->sc_link);
printf("User command with no buf\n");
1993-11-24 12:45:04 +03:00
return;
1993-11-24 07:52:44 +03:00
}
si = si_find(bp);
if (!si) {
sc_print_addr(xs->sc_link);
printf("User command with no ioctl\n");
return;
}
screq = si->si_screq;
si_free(si);
1993-11-24 07:52:44 +03:00
if (!screq) { /* Is it one of ours? (the SCSI_USER bit says it is) */
sc_print_addr(xs->sc_link);
printf("User command with no request\n");
1993-11-24 12:45:04 +03:00
return;
1993-11-24 07:52:44 +03:00
}
1993-11-24 12:45:04 +03:00
SC_DEBUG(xs->sc_link, SDEV_DB2, ("user-done\n"));
1993-11-24 07:52:44 +03:00
screq->retsts = 0;
screq->status = xs->status;
switch(xs->error) {
1993-11-24 12:45:04 +03:00
case XS_NOERROR:
SC_DEBUG(xs->sc_link, SDEV_DB3, ("no error\n"));
1993-11-24 07:52:44 +03:00
screq->datalen_used = xs->datalen - xs->resid; /* probably rubbish */
screq->retsts = SCCMD_OK;
break;
1993-11-24 12:45:04 +03:00
case XS_SENSE:
SC_DEBUG(xs->sc_link, SDEV_DB3, ("have sense\n"));
screq->senselen_used = min(sizeof(xs->sense), SENSEBUFLEN);
bcopy(&xs->sense, screq->sense, screq->senselen);
1993-11-24 07:52:44 +03:00
screq->retsts = SCCMD_SENSE;
break;
1993-11-24 12:45:04 +03:00
case XS_DRIVER_STUFFUP:
1993-11-24 07:52:44 +03:00
sc_print_addr(xs->sc_link);
printf("host adapter code inconsistency\n");
screq->retsts = SCCMD_UNKNOWN;
break;
1993-11-24 12:45:04 +03:00
case XS_TIMEOUT:
SC_DEBUG(xs->sc_link, SDEV_DB3, ("timeout\n"));
1993-11-24 07:52:44 +03:00
screq->retsts = SCCMD_TIMEOUT;
break;
1993-11-24 12:45:04 +03:00
case XS_BUSY:
SC_DEBUG(xs->sc_link, SDEV_DB3, ("busy\n"));
1993-11-24 07:52:44 +03:00
screq->retsts = SCCMD_BUSY;
break;
default:
sc_print_addr(xs->sc_link);
printf("unknown error category from host adapter code\n");
screq->retsts = SCCMD_UNKNOWN;
break;
}
biodone(bp); /* we're waiting on it in scsi_strategy() */
return; /* it'll free the xs and restart any queue */
}
/* Pseudo strategy function
* Called by scsi_do_ioctl() via physio/physstrat if there is to
* be data transfered, and directly if there is no data transfer.
*
* Should I reorganize this so it returns to physio instead
* of sleeping in scsiio_scsi_cmd? Is there any advantage, other
* than avoiding the probable duplicate wakeup in iodone? [PD]
*
* No, seems ok to me... [JRE]
* (I don't see any duplicate wakeups)
*
* Can't be used with block devices or raw_read/raw_write directly
* from the cdevsw/bdevsw tables because they couldn't have added
* the screq structure. [JRE]
*/
void
scsistrategy(bp)
struct buf *bp;
1993-11-24 07:52:44 +03:00
{
1993-11-24 12:45:04 +03:00
int err;
struct scsi_ioctl *si;
1993-11-24 07:52:44 +03:00
scsireq_t *screq;
struct scsi_link *sc_link;
1993-11-24 07:52:44 +03:00
u_int32 flags = 0;
int s;
si = si_find(bp);
if (!si) {
printf("user_strat: No ioctl\n");
scsierr(bp, EINVAL);
return;
}
screq = si->si_screq;
sc_link = si->si_sc_link;
si_free(si);
1993-11-24 12:45:04 +03:00
if (!sc_link) {
1993-11-24 07:52:44 +03:00
printf("user_strat: No link pointer\n");
1993-11-24 12:45:04 +03:00
scsierr(bp, EINVAL);
1993-11-24 07:52:44 +03:00
return;
}
1993-11-24 12:45:04 +03:00
SC_DEBUG(sc_link, SDEV_DB2, ("user_strategy\n"));
if (!screq) {
1993-11-24 07:52:44 +03:00
sc_print_addr(sc_link);
printf("No request block\n");
1993-11-24 12:45:04 +03:00
scsierr(bp, EINVAL);
1993-11-24 07:52:44 +03:00
return;
}
/*
* We're in trouble if physio tried to break up the transfer.
1993-11-24 07:52:44 +03:00
*/
if (bp->b_bcount != screq->datalen) {
sc_print_addr(sc_link);
printf("physio split the request.. cannot proceed\n");
scsierr(bp, EIO);
return;
}
if (screq->timeout == 0) {
scsierr(bp, EINVAL);
return;
}
if (screq->cmdlen > sizeof(struct scsi_generic)) {
sc_print_addr(sc_link);
printf("cmdlen too big\n");
1993-11-24 07:52:44 +03:00
scsierr(bp, EFAULT);
return;
}
if (screq->flags & SCCMD_READ)
flags |= SCSI_DATA_IN;
if (screq->flags & SCCMD_WRITE)
flags |= SCSI_DATA_OUT;
if (screq->flags & SCCMD_TARGET)
flags |= SCSI_TARGET;
if (screq->flags & SCCMD_ESCAPE)
flags |= SCSI_ESCAPE;
1993-11-24 12:45:04 +03:00
err = scsi_scsi_cmd(sc_link, (struct scsi_generic *)screq->cmd,
screq->cmdlen, (u_char *)bp->b_un.b_addr, screq->datalen,
0, /* user must do the retries *//* ignored */
screq->timeout, bp, flags | SCSI_USER);
1993-11-24 07:52:44 +03:00
1993-11-24 12:45:04 +03:00
/* because there is a bp, scsi_scsi_cmd will return immediatly */
if (err) {
1993-11-24 07:52:44 +03:00
scsierr(bp, err);
return;
}
1993-11-24 12:45:04 +03:00
SC_DEBUG(sc_link, SDEV_DB3, ("about to sleep\n"));
1993-11-24 07:52:44 +03:00
s = splbio();
1993-11-24 12:45:04 +03:00
while (!(bp->b_flags & B_DONE))
tsleep(bp, PRIBIO, "scistr", 0);
1993-11-24 07:52:44 +03:00
splx(s);
1993-11-24 12:45:04 +03:00
SC_DEBUG(sc_link, SDEV_DB3, ("back from sleep\n"));
1993-11-24 07:52:44 +03:00
return;
}
void
scsiminphys(bp)
struct buf *bp;
1993-11-24 07:52:44 +03:00
{
1993-11-24 07:52:44 +03:00
/*XXX*//* call the adapter's minphys */
}
/*
* Something (e.g. another driver) has called us
* with an sc_link for a target/lun/adapter, and a scsi
* specific ioctl to perform, better try.
* If user-level type command, we must still be running
* in the context of the calling process
*/
1993-11-24 12:45:04 +03:00
int
scsi_do_ioctl(sc_link, cmd, addr, f)
struct scsi_link *sc_link;
int cmd;
caddr_t addr;
int f;
1993-11-24 07:52:44 +03:00
{
int error;
1993-11-24 07:52:44 +03:00
1993-11-24 12:45:04 +03:00
SC_DEBUG(sc_link, SDEV_DB2, ("scsi_do_ioctl(0x%x)\n", cmd));
switch(cmd) {
#ifdef notyet /* XXXX Needs to be redone to use copyin/out! */
case SCIOCCOMMAND: {
/*
* You won't believe this, but the arg copied in
* from the user space, is on the kernel stack
* for this process, so we can't write
* to it at interrupt time..
* we need to copy it in and out!
* Make a static copy using malloc!
*/
scsireq_t *screq2 = (scsireq_t *)addr;
scsireq_t *screq = (scsireq_t *)addr;
int rwflag = (screq->flags & SCCMD_READ) ? B_READ : B_WRITE;
struct buf *bp;
caddr_t d_addr;
int len;
if ((unsigned int)screq < KERNBASE) {
screq = malloc(sizeof(scsireq_t), M_TEMP, M_WAITOK);
bcopy(screq2, screq, sizeof(scsireq_t));
}
bp = malloc(sizeof(struct buf), M_TEMP, M_WAITOK);
bzero(bp, sizeof(struct buf));
d_addr = screq->databuf;
bp->b_bcount = len = screq->datalen;
si = si_get(bp);
if (!si) {
scsierr(bp, EINTR);
return EINTR;
}
si->si_screq = screq;
si->si_sc_link = sc_link;
if (len) {
1993-11-24 07:52:44 +03:00
#ifdef __NetBSD__
#error "dev, mincntfn & uio need defining"
error = physio(scsistrategy, bp, dev, rwflag,
mincntfn, uio);
1993-11-24 07:52:44 +03:00
#else
error = physio(scsistrategy, 0, bp, 0, rwflag, d_addr,
&len, curproc);
1993-11-24 07:52:44 +03:00
#endif
} else {
/* if no data, no need to translate it.. */
bp->b_un.b_addr = 0;
bp->b_dev = -1; /* irrelevant info */
bp->b_flags = 0;
scsistrategy(bp);
error = bp->b_error;
1993-11-24 07:52:44 +03:00
}
free(bp, M_TEMP);
if ((unsigned int)screq2 < KERNBASE) {
bcopy(screq, screq2, sizeof(scsireq_t));
free(screq, M_TEMP);
1993-11-24 07:52:44 +03:00
}
return error;
}
#endif
case SCIOCDEBUG: {
int level = *((int *)addr);
SC_DEBUG(sc_link, SDEV_DB3, ("debug set to %d\n", level));
sc_link->flags &= ~SDEV_DBX; /* clear debug bits */
if (level & 1)
sc_link->flags |= SDEV_DB1;
if (level & 2)
sc_link->flags |= SDEV_DB2;
if (level & 4)
sc_link->flags |= SDEV_DB3;
if (level & 8)
sc_link->flags |= SDEV_DB4;
return 0;
}
case SCIOCREPROBE: {
struct scsi_addr *sca = (struct scsi_addr *)addr;
1993-11-24 07:52:44 +03:00
return scsi_probe_busses(sca->scbus, sca->target, sca->lun);
}
case SCIOCRECONFIG:
case SCIOCDECONFIG:
return EINVAL;
case SCIOCIDENTIFY: {
struct scsi_addr *sca = (struct scsi_addr *)addr;
sca->scbus = sc_link->scsibus;
sca->target = sc_link->target;
sca->lun = sc_link->lun;
return 0;
}
default:
return ENOTTY;
1993-11-24 07:52:44 +03:00
}
#ifdef DIAGNOSTIC
panic("scsi_do_ioctl: impossible");
#endif
1993-11-24 07:52:44 +03:00
}
1993-11-24 12:45:04 +03:00
void
scsierr(bp, error)
struct buf *bp;
int error;
1993-11-24 07:52:44 +03:00
{
1993-11-24 12:45:04 +03:00
bp->b_flags |= B_ERROR;
bp->b_error = error;
biodone(bp);
return;
1993-11-24 07:52:44 +03:00
}