/*
* Copyright (c) 1995 Charles D. Cranor
* 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.
*
* 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.
*/
/*
*
* x d . c x y l o g i c s 7 5 3 / 7 0 5 3 v m e / s m d d r i v e r
*
* author: Chuck Cranor <chuck@netbsd>
* id: &Id: xd.c,v 1.9 1995/09/25 20:12:44 chuck Exp &
* started: 27-Feb-95
* references: [1] Xylogics Model 753 User's Manual
* part number: 166-753-001, Revision B, May 21, 1988.
* "Your Partner For Performance"
* [2] other NetBSD disk device drivers
*
* Special thanks go to Scott E. Campbell of Xylogics, Inc. for taking
* the time to answer some of my questions about the 753/7053.
*
* note: the 753 and the 7053 are programmed the same way, but are
* different sizes. the 753 is a 6U VME card, while the 7053 is a 9U
* VME card (found in many VME based suns).
*/
/*
* Print a complaint when no xd children were specified
* in the config file. Better than a link error...
*
* XXX: Some folks say this driver should be split in two,
* but that seems pointless with ONLY one type of child.
*/
#include "xd.h"
#if NXD == 0
#error "xdc but no xd?"
#endif
/*
* macros
*/
/*
* XDC_TWAIT: add iorq "N" to tail of SC's wait queue
*/
#define XDC_TWAIT(SC, N) \
do { \
(SC)->waitq[(SC)->waitend] = (N); \
(SC)->waitend = ((SC)->waitend + 1) % XDC_MAXIOPB; \
(SC)->nwait++; \
} while (/* CONSTCOND */ 0)
/*
* XDC_HWAIT: add iorq "N" to head of SC's wait queue
*/
#define XDC_HWAIT(SC, N) \
do { \
(SC)->waithead = ((SC)->waithead == 0) ? \
(XDC_MAXIOPB - 1) : ((SC)->waithead - 1); \
(SC)->waitq[(SC)->waithead] = (N); \
(SC)->nwait++; \
} while (/* CONSTCOND */ 0)
/*
* XDC_GET_WAITER: gets the first request waiting on the waitq
* and removes it (so it can be submitted)
*/
#define XDC_GET_WAITER(XDCSC, RQ) \
do { \
(RQ) = (XDCSC)->waitq[(XDCSC)->waithead]; \
(XDCSC)->waithead = ((XDCSC)->waithead + 1) % XDC_MAXIOPB; \
xdcsc->nwait--; \
} while (/* CONSTCOND */ 0)
/*
* XDC_FREE: add iorq "N" to SC's free list
*/
#define XDC_FREE(SC, N) \
do { \
(SC)->freereq[(SC)->nfree++] = (N); \
(SC)->reqs[N].mode = 0; \
if ((SC)->nfree == 1) \
wakeup(&(SC)->nfree); \
} while (/* CONSTCOND */ 0)
/*
* XDC_RQALLOC: allocate an iorq off the free list (assume nfree > 0).
*/
#define XDC_RQALLOC(XDCSC) (XDCSC)->freereq[--((XDCSC)->nfree)]
/*
* XDC_WAIT: wait for XDC's csr "BITS" to come on in "TIME".
* LCV is a counter. If it goes to zero then we timed out.
*/
#define XDC_WAIT(XDC, LCV, TIME, BITS) \
do { \
(LCV) = (TIME); \
while ((LCV) > 0) { \
if ((XDC)->xdc_csr & (BITS)) \
break; \
(LCV) = (LCV) - 1; \
DELAY(1); \
} \
} while (/* CONSTCOND */ 0)
/*
* XDC_DONE: don't need IORQ, get error code and free (done after xdc_cmd)
*/
#define XDC_DONE(SC,RQ,ER) \
do { \
if ((RQ) == XD_ERR_FAIL) { \
(ER) = (RQ); \
} else { \
if ((SC)->ndone-- == XDC_SUBWAITLIM) \
wakeup(&(SC)->ndone); \
(ER) = (SC)->reqs[RQ].errno; \
XDC_FREE((SC), (RQ)); \
} \
} while (/* CONSTCOND */ 0)
/*
* XDC_ADVANCE: advance iorq's pointers by a number of sectors
*/
#define XDC_ADVANCE(IORQ, N) \
do { \
if (N) { \
(IORQ)->sectcnt -= (N); \
(IORQ)->blockno += (N); \
(IORQ)->dbuf += ((N) * XDFM_BPS); \
} \
} while (/* CONSTCOND */ 0)
/*
* note - addresses you can sleep on:
* [1] & of xd_softc's "state" (waiting for a chance to attach a drive)
* [2] & of xdc_softc's "nfree" (waiting for a free iorq/iopb)
* [3] & of xdc_softc's "ndone" (waiting for number of done iorq/iopb's
* to drop below XDC_SUBWAITLIM)
* [4] & an iorq (waiting for an XD_SUB_WAIT iorq to finish)
*/
/*
* function prototypes
* "xdc_*" functions are internal, all others are external interfaces
*/
struct xdc_attach_args { /* this is the "aux" args to xdattach */
int driveno; /* unit number */
char *dvmabuf; /* scratch buffer for reading disk label */
int fullmode; /* submit mode */
int booting; /* are we booting or not? */
};
/* Ok, we have the label; fill in `pcyl' if there's SunOS magic */
sdl = (struct sun_disklabel *)xd->sc_dk.dk_cpulabel->cd_block;
if (sdl->sl_magic == SUN_DKMAGIC)
xd->pcyl = sdl->sl_pcyl;
else {
printf("%s: WARNING: no `pcyl' in disk label.\n",
device_xname(xd->sc_dev));
xd->pcyl = xd->sc_dk.dk_label->d_ncylinders +
xd->sc_dk.dk_label->d_acylinders;
printf("%s: WARNING: guessing pcyl=%d (ncyl+acyl)\n",
device_xname(xd->sc_dev), xd->pcyl);
}
/* allocate and zero buffers
*
* note: we simplify the code by allocating the max number of iopbs and
* iorq's up front. thus, we avoid linked lists and the costs
* associated with them in exchange for wasting a little memory. */
/* link in interrupt with higher level software */
isr_add_vectored(xdcintr, xdc, ca->ca_intpri, ca->ca_intvec);
evcnt_attach_dynamic(&xdc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
device_xname(self), "intr");
/* now we must look for disks using autoconfig */
xa.booting = 1;
for (xa.driveno = 0; xa.driveno < XDC_MAXDEV; xa.driveno++)
(void)config_found(self, (void *)&xa, xdc_print, CFARGS_NONE);
if (xa->driveno != -1)
aprint_normal(" drive %d", xa->driveno);
return UNCONF;
}
/*
* xdmatch: probe for disk.
*
* note: we almost always say disk is present. this allows us to
* spin up and configure a disk after the system is booted (we can
* call xdattach!). Also, wire down the relationship between the
* xd* and xdc* devices, to simplify boot device identification.
*/
int
xdmatch(device_t parent, cfdata_t cf, void *aux)
{
struct xdc_attach_args *xa = aux;
int xd_unit;
/* Match only on the "wired-down" controller+disk. */
xd_unit = device_unit(parent) * 2 + xa->driveno;
if (cf->cf_unit != xd_unit)
return 0;
/*
* Always re-initialize the disk structure. We want statistics
* to start with a clean slate.
*/
memset(&xd->sc_dk, 0, sizeof(xd->sc_dk));
disk_init(&xd->sc_dk, device_xname(self), &xddkdriver);
/* Do init work common to attach and open. */
xd_init(xd);
}
/*
* end of autoconfig functions
*/
/*
* Initialize a disk. This can be called from both autoconf and
* also from xdopen/xdstrategy.
*/
static void
xd_init(struct xd_softc *xd)
{
struct xdc_softc *xdc;
struct dkbad *dkb;
struct xd_iopb_drive *driopb;
void *dvmabuf;
int rqno, err, spt, mb, blk, lcv, fullmode, newstate;
xd->hw_spt = spt;
/* Attach the disk: must be before getdisklabel to malloc label */
disk_attach(&xd->sc_dk);
if (xdgetdisklabel(xd, dvmabuf) != XD_ERR_AOK)
goto done;
/* inform the user of what is up */
printf("%s: <%s>, pcyl %d, hw_spt %d\n",
device_xname(xd->sc_dev), (char *)dvmabuf, xd->pcyl, spt);
mb = xd->ncyl * (xd->nhead * xd->nsect) / (1048576 / XDFM_BPS);
printf("%s: %dMB, %d cyl, %d head, %d sec\n",
device_xname(xd->sc_dev), mb,
xd->ncyl, xd->nhead, xd->nsect);
/* now set the real drive parameters! */
rqno = xdc_cmd(xdc, XDCMD_WRP, XDFUN_DRV, xd->xd_drive,
0, 0, 0, fullmode);
XDC_DONE(xdc, rqno, err);
if (err) {
printf("%s: write real drive parameters failed: %s\n",
device_xname(xd->sc_dev), xdc_e2str(err));
goto done;
}
newstate = XD_DRIVE_ONLINE;
/*
* read bad144 table. this table resides on the first sector of the
* last track of the disk (i.e. second cyl of "acyl" area).
*/
blk = (xd->ncyl + xd->acyl - 1) * (xd->nhead * xd->nsect) + /* last cyl */
(xd->nhead - 1) * xd->nsect; /* last head */
rqno = xdc_cmd(xdc, XDCMD_RD, 0, xd->xd_drive,
blk, 1, dvmabuf, fullmode);
XDC_DONE(xdc, rqno, err);
if (err) {
printf("%s: reading bad144 failed: %s\n",
device_xname(xd->sc_dev), xdc_e2str(err));
goto done;
}
/*
* xdclose: close device
*/
static int
xdclose(dev_t dev, int flag, int fmt, struct lwp *l)
{
struct xd_softc *xd = device_lookup_private(&xd_cd, DISKUNIT(dev));
int part = DISKPART(dev);
/*
* xddump: crash dump system
*/
static int
xddump(dev_t dev, daddr_t blkno, void *va, size_t sz)
{
int unit, part;
struct xd_softc *xd;
unit = DISKUNIT(dev);
part = DISKPART(dev);
xd = device_lookup_private(&xd_cd, unit);
if (xd == NULL)
return ENXIO;
printf("%s%c: crash dump not supported (yet)\n",
device_xname(xd->sc_dev), 'a' + part);
return ENXIO;
/* outline: globals: "dumplo" == sector number of partition to start
* dump at (convert to physical sector with partition table)
* "dumpsize" == size of dump in clicks "physmem" == size of physical
* memory (clicks, ctob() to get bytes) (normal case: dumpsize ==
* physmem)
*
* dump a copy of physical memory to the dump device starting at sector
* "dumplo" in the swap partition (make sure > 0). map in pages as
* we go. use polled I/O.
*
* XXX how to handle NON_CONTIG?
*/
}
switch (cmd) {
case XDCMD_WR:
case XDCMD_XWR:
req = KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITE;
break;
case XDCMD_RD:
case XDCMD_XRD:
req = KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READ;
break;
case XDCMD_RDP:
req = KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_READCONF;
break;
case XDCMD_WRP:
case XDCMD_RST:
req = KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_WRITECONF;
break;
case XDCMD_NOP:
case XDCMD_SK:
case XDCMD_TST:
default:
req = 0;
break;
}
return req;
}
/*
* xdioctl: ioctls on XD drives. based on ioctl's of other netbsd disks.
*/
static int
xdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
{
struct xd_softc *xd;
struct xd_iocmd *xio;
int error, s, unit;
unit = DISKUNIT(dev);
xd = device_lookup_private(&xd_cd, unit);
if (xd == NULL)
return (ENXIO);
switch (cmd) {
case DIOCSBAD: /* set bad144 info */
if ((flag & FWRITE) == 0)
return EBADF;
s = splbio();
memcpy(&xd->dkb, addr, sizeof(xd->dkb));
splx(s);
return 0;
case DIOCSDINFO: /* set disk label */
if ((flag & FWRITE) == 0)
return EBADF;
error = setdisklabel(xd->sc_dk.dk_label,
(struct disklabel *)addr, /* xd->sc_dk.dk_openmask : */ 0,
xd->sc_dk.dk_cpulabel);
if (error == 0) {
if (xd->state == XD_DRIVE_NOLABEL)
xd->state = XD_DRIVE_ONLINE;
}
return error;
case DIOCWLABEL: /* change write status of disk label */
if ((flag & FWRITE) == 0)
return EBADF;
if (*(int *)addr)
xd->flags |= XD_WLABEL;
else
xd->flags &= ~XD_WLABEL;
return 0;
case DIOCWDINFO: /* write disk label */
if ((flag & FWRITE) == 0)
return EBADF;
error = setdisklabel(xd->sc_dk.dk_label,
(struct disklabel *)addr, /* xd->sc_dk.dk_openmask : */ 0,
xd->sc_dk.dk_cpulabel);
if (error == 0) {
if (xd->state == XD_DRIVE_NOLABEL)
xd->state = XD_DRIVE_ONLINE;
/*
* xdopen: open drive
*/
static int
xdopen(dev_t dev, int flag, int fmt, struct lwp *l)
{
int err, unit, part, s;
struct xd_softc *xd;
/* first, could it be a valid target? */
unit = DISKUNIT(dev);
xd = device_lookup_private(&xd_cd, unit);
if (xd == NULL)
return ENXIO;
part = DISKPART(dev);
err = 0;
/*
* If some other processing is doing init, sleep.
*/
s = splbio();
while (xd->state == XD_DRIVE_ATTACHING) {
if (tsleep(&xd->state, PRIBIO, "xdopen", 0)) {
err = EINTR;
goto done;
}
}
/* Do we need to init the drive? */
if (xd->state == XD_DRIVE_UNKNOWN) {
xd_init(xd);
wakeup(&xd->state);
}
/* Was the init successful? */
if (xd->state == XD_DRIVE_UNKNOWN) {
err = EIO;
goto done;
}
/* There should always be an open first. */
if (xd->state == XD_DRIVE_UNKNOWN) {
bp->b_error = EIO;
goto done;
}
if (xd->state != XD_DRIVE_ONLINE && DISKPART(bp->b_dev) != RAW_PART) {
/* no I/O to unlabeled disks, unless raw partition */
bp->b_error = EIO;
goto done;
}
/* short circuit zero length request */
if (bp->b_bcount == 0)
goto done;
/* check bounds with label (disksubr.c). Determine the size of the
* transfer, and make sure it is within the boundaries of the
* partition. Adjust transfer if needed, and signal errors or early
* completion. */
/*
* now we know we have a valid buf structure that we need to do I/O
* on.
*
* note that we don't disksort because the controller has a sorting
* algorithm built into the hardware.
*/
s = splbio(); /* protect the queues */
/* first, give jobs in front of us a chance */
parent = xd->parent;
while (parent->nfree > 0 && bufq_peek(parent->sc_wq) != NULL)
if (xdc_startbuf(parent, NULL, NULL) != XD_ERR_AOK)
break;
/*
* if there are no free iorq's, then we just queue and return. the
* buffs will get picked up later by xdcintr().
*/
if (parent->nfree == 0) {
bufq_put(parent->sc_wq, bp);
splx(s);
return;
}
/* now we have free iopb's and we are at splbio... start 'em up */
if (xdc_startbuf(parent, xd, bp) != XD_ERR_AOK) {
return;
}
/* done! */
splx(s);
return;
done:
/* tells upper layers we are done with this buf */
bp->b_resid = bp->b_bcount;
biodone(bp);
}
/*
* end of {b,c}devsw functions
*/
/*
* i n t e r r u p t f u n c t i o n
*
* xdcintr: hardware interrupt.
*/
int
xdcintr(void *v)
{
struct xdc_softc *xdcsc = v;
/* kick the event counter */
xdcsc->sc_intrcnt.ev_count++;
/* remove as many done IOPBs as possible */
xdc_remove_iorq(xdcsc);
/* start any iorq's already waiting */
xdc_start(xdcsc, XDC_MAXIOPB);
/* fill up any remaining iorq's with queue'd buffers */
while (xdcsc->nfree > 0 && bufq_peek(xdcsc->sc_wq) != NULL)
if (xdc_startbuf(xdcsc, NULL, NULL) != XD_ERR_AOK)
break;
return 1;
}
/*
* end of interrupt function
*/
/*
* i n t e r n a l f u n c t i o n s
*/
/*
* xdc_rqinit: fill out the fields of an I/O request
*/
/*
* xdc_rqtopb: load up an IOPB based on an iorq
*/
void
xdc_rqtopb(struct xd_iorq *iorq, struct xd_iopb *iopb, int cmd, int subfun)
{
u_long block, dp;
/*
* xdc_cmd: front end for POLL'd and WAIT'd commands. Returns rqno.
* If you've already got an IORQ, you can call submit directly (currently
* there is no need to do this). NORM requests are handled separately.
*/
int
xdc_cmd(struct xdc_softc *xdcsc, int cmd, int subfn, int unit, int block,
int scnt, char *dptr, int fullmode)
{
struct xd_iorq *iorq;
struct xd_iopb *iopb;
int rqno, retry;
int submode = XD_STATE(fullmode);
/* get iorq/iopb */
switch (submode) {
case XD_SUB_POLL:
while (xdcsc->nfree == 0) {
if (xdc_piodriver(xdcsc, 0, 1) != XD_ERR_AOK)
return XD_ERR_FAIL;
}
break;
case XD_SUB_WAIT:
retry = 1;
while (retry) {
while (xdcsc->nfree == 0) {
if (tsleep(&xdcsc->nfree, PRIBIO, "xdnfree", 0))
return XD_ERR_FAIL;
}
while (xdcsc->ndone > XDC_SUBWAITLIM) {
if (tsleep(&xdcsc->ndone, PRIBIO, "xdsubwait", 0))
return XD_ERR_FAIL;
}
if (xdcsc->nfree)
retry = 0; /* got it */
}
break;
default:
return XD_ERR_FAIL; /* illegal */
}
if (xdcsc->nfree == 0)
panic("xdcmd nfree");
rqno = XDC_RQALLOC(xdcsc);
iorq = &xdcsc->reqs[rqno];
iopb = iorq->iopb;
/*
* load request. we have to calculate the correct block number based
* on partition info.
*
* also, note that there are two kinds of buf structures, those with
* B_PHYS set and those without B_PHYS. if B_PHYS is set, then it is
* a raw I/O (to a cdevsw) and we are doing I/O directly to the users'
* buffer which has already been mapped into DVMA space. (Not on sun3)
* However, if B_PHYS is not set, then the buffer is a normal system
* buffer which does *not* live in DVMA space. In that case we call
* dvma_mapin to map it into DVMA space so we can do the DMA to it.
*
* in cases where we do a dvma_mapin, note that iorq points to the
* buffer as mapped into DVMA space, where as the bp->b_data points
* to its non-DVMA mapping.
*
* XXX - On the sun3, B_PHYS does NOT mean the buffer is mapped
* into dvma space, only that it was remapped into the kernel.
* We ALWAYS have to remap the kernel buf into DVMA space.
* (It is done inexpensively, using whole segments!)
*/
dbuf = dvma_mapin(bp->b_data, bp->b_bcount, 0);
if (dbuf == NULL) { /* out of DVMA space */
printf("%s: warning: out of DVMA space\n",
device_xname(xdcsc->sc_dev));
XDC_FREE(xdcsc, rqno);
bufq_put(xdcsc->sc_wq, bp);
return XD_ERR_FAIL; /* XXX: need some sort of
* call-back scheme here? */
}
/*
* xdc_submit_iorq: submit an iorq for processing. returns XD_ERR_AOK
* if ok. if it fail returns an error code. type is XD_SUB_*.
*
* note: caller frees iorq in all cases except NORM
*
* return value:
* NORM: XD_AOK (req pending), XD_FAIL (couldn't submit request)
* WAIT: XD_AOK (success), <error-code> (failed)
* POLL: <same as WAIT>
* NOQ : <same as NORM>
*
* there are three sources for i/o requests:
* [1] xdstrategy: normal block I/O, using "struct buf" system.
* [2] autoconfig/crash dump: these are polled I/O requests, no interrupts.
* [3] open/ioctl: these are I/O requests done in the context of a process,
* and the process should block until they are done.
*
* software state is stored in the iorq structure. each iorq has an
* iopb structure. the hardware understands the iopb structure.
* every command must go through an iopb. a 7053 can only handle
* XDC_MAXIOPB (31) active iopbs at one time. iopbs are allocated in
* DVMA space at boot up time. what happens if we run out of iopb's?
* for i/o type [1], the buffers are queued at the "buff" layer and
* picked up later by the interrupt routine. for case [2] the
* programmed i/o driver is called with a special flag that says
* return when one iopb is free. for case [3] the process can sleep
* on the iorq free list until some iopbs are available.
*/
int
xdc_submit_iorq(struct xdc_softc *xdcsc, int iorqno, int type)
{
u_long iopbaddr;
struct xd_iorq *iorq = &xdcsc->reqs[iorqno];
/* first check and see if controller is busy */
if (xdcsc->xdc->xdc_csr & XDC_ADDING) {
#ifdef XDC_DEBUG
printf("%s: XDC not ready (ADDING)\n", __func__);
#endif
if (type == XD_SUB_NOQ)
return XD_ERR_FAIL; /* failed */
XDC_TWAIT(xdcsc, iorqno); /* put at end of waitq */
switch (type) {
case XD_SUB_NORM:
return XD_ERR_AOK; /* success */
case XD_SUB_WAIT:
while (iorq->iopb->done == 0) {
(void)tsleep(iorq, PRIBIO, "xdciorq", 0);
}
return iorq->errno;
case XD_SUB_POLL:
return xdc_piodriver(xdcsc, iorqno, 0);
default:
panic("%s adding", __func__);
}
}
#ifdef XDC_DEBUG
{
u_char *rio = (u_char *)iorq->iopb;
int sz = sizeof(struct xd_iopb), lcv;
printf("%s: aio #%d [",
device_xname(xdcsc->sc_dev), iorq - xdcsc->reqs);
for (lcv = 0; lcv < sz; lcv++)
printf(" %02x", rio[lcv]);
printf("]\n");
}
#endif /* XDC_DEBUG */
/* controller not busy, start command */
iopbaddr = dvma_kvtopa(iorq->iopb, xdcsc->bustype);
XDC_GO(xdcsc->xdc, iopbaddr); /* go! */
xdcsc->nrun++;
/* command now running, wrap it up */
switch (type) {
case XD_SUB_NORM:
case XD_SUB_NOQ:
return XD_ERR_AOK; /* success */
case XD_SUB_WAIT:
while (iorq->iopb->done == 0) {
(void)tsleep(iorq, PRIBIO, "xdciorq", 0);
}
return iorq->errno;
case XD_SUB_POLL:
return xdc_piodriver(xdcsc, iorqno, 0);
default:
panic("%s wrap up", __func__);
}
panic("%s: impossible", __func__);
return 0; /* not reached */
}
/*
* xdc_piodriver
*
* programmed i/o driver. this function takes over the computer
* and drains off all i/o requests. it returns the status of the iorq
* the caller is interesting in. if freeone is true, then it returns
* when there is a free iorq.
*/
int
xdc_piodriver(struct xdc_softc *xdcsc, int iorqno, int freeone)
{
int nreset = 0;
int retval = 0;
u_long count;
struct xdc *xdc = xdcsc->xdc;
#ifdef XDC_DEBUG
printf("%s(%s, %d, freeone=%d)\n", __func__,
device_xname(xdcsc->sc_dev), iorqno, freeone);
#endif
while (xdcsc->nwait || xdcsc->nrun) {
#ifdef XDC_DEBUG
printf("%s: wait=%d, run=%d\n", __func__,
xdcsc->nwait, xdcsc->nrun);
#endif
XDC_WAIT(xdc, count, XDC_MAXTIME, (XDC_REMIOPB | XDC_F_ERROR));
#ifdef XDC_DEBUG
printf("%s: done wait with count = %d\n", __func__, count);
#endif
/* we expect some progress soon */
if (count == 0 && nreset >= 2) {
xdc_reset(xdcsc, 0, XD_RSET_ALL, XD_ERR_FAIL, 0);
#ifdef XDC_DEBUG
printf("%s: timeout\n", __func__);
#endif
return XD_ERR_FAIL;
}
if (count == 0) {
if (xdc_reset(xdcsc, 0,
(nreset++ == 0) ? XD_RSET_NONE : iorqno,
XD_ERR_FAIL, 0) == XD_ERR_FAIL)
return XD_ERR_FAIL; /* flushes all but POLL
* requests, resets */
continue;
}
xdc_remove_iorq(xdcsc); /* could resubmit request */
if (freeone) {
if (xdcsc->nrun < XDC_MAXIOPB) {
#ifdef XDC_DEBUG
printf("%s: done: one free\n", __func__);
#endif
return XD_ERR_AOK;
}
continue; /* don't xdc_start */
}
xdc_start(xdcsc, XDC_MAXIOPB);
}
/*
* now that we've drained everything, start up any bufs that have
* queued
*/
while (xdcsc->nfree > 0 && bufq_peek(xdcsc->sc_wq) != NULL)
if (xdc_startbuf(xdcsc, NULL, NULL) != XD_ERR_AOK)
break;
return retval;
}
/*
* xdc_reset: reset one drive. NOTE: assumes xdc was just reset.
* we steal iopb[0] for this, but we put it back when we are done.
*/
void
xdc_xdreset(struct xdc_softc *xdcsc, struct xd_softc *xdsc)
{
struct xd_iopb tmpiopb;
u_long addr;
int del;
/*
* xdc_reset: reset everything: requests are marked as errors except
* a polled request (which is resubmitted)
*/
int
xdc_reset(struct xdc_softc *xdcsc, int quiet, int blastmode, int error,
struct xd_softc *xdsc)
{
int del = 0, lcv, retval = XD_ERR_AOK;
int oldfree = xdcsc->nfree;
struct xd_iorq *iorq;
/* soft reset hardware */
if (quiet == 0)
printf("%s: soft reset\n", device_xname(xdcsc->sc_dev));
xdcsc->xdc->xdc_csr = XDC_RESET;
XDC_WAIT(xdcsc->xdc, del, XDC_RESETUSEC, XDC_RESET);
if (del <= 0) {
blastmode = XD_RSET_ALL; /* dead, flush all requests */
retval = XD_ERR_FAIL;
}
if (xdsc)
xdc_xdreset(xdcsc, xdsc);
if (xdc->xdc_csr & XDC_F_ERROR) {
/*
* FATAL ERROR: should never happen under normal use. This
* error is so bad, you can't even tell which IOPB is bad, so
* we dump them all.
*/
errno = xdc->xdc_f_err;
printf("%s: fatal error 0x%02x: %s\n",
device_xname(xdcsc->sc_dev), errno, xdc_e2str(errno));
if (xdc_reset(xdcsc, 0, XD_RSET_ALL, errno, 0) != XD_ERR_AOK) {
printf("%s: soft reset failed!\n",
device_xname(xdcsc->sc_dev));
panic("%s: controller DEAD", __func__);
}
return XD_ERR_AOK;
}
/*
* get iopb that is done
*
* hmm... I used to read the address of the done IOPB off the VME
* registers and calculate the rqno directly from that. that worked
* until I started putting a load on the controller. when loaded, i
* would get interrupts but neither the REMIOPB or F_ERROR bits would
* be set, even after DELAY'ing a while! later on the timeout
* routine would detect IOPBs that were marked "running" but their
* "done" bit was set. rather than dealing directly with this
* problem, it is just easier to look at all running IOPB's for the
* done bit.
*/
if (xdc->xdc_csr & XDC_REMIOPB) {
xdc->xdc_csr = XDC_CLRRIO;
}
for (rqno = 0; rqno < XDC_MAXIOPB; rqno++) {
iorq = &xdcsc->reqs[rqno];
if (iorq->mode == 0 || XD_STATE(iorq->mode) == XD_SUB_DONE)
continue; /* free, or done */
iopb = &xdcsc->iopbase[rqno];
if (iopb->done == 0)
continue; /* not done yet */
if (errs)
iorq->errno = iopb->errno;
else
iorq->errno = 0;
/* handle non-fatal errors */
if (errs &&
xdc_error(xdcsc, iorq, iopb, rqno, comm) == XD_ERR_AOK)
continue; /* AOK: we resubmitted it */
/* this iorq is now done (hasn't been restarted or anything) */
if ((iorq->mode & XD_MODE_VERBO) && iorq->lasterror)
xdc_perror(iorq, iopb, 0);
/* now, if read/write check to make sure we got all the data
* we needed. (this may not be the case if we got an error in
* the middle of a multisector request). */
if ((iorq->mode & XD_MODE_B144) != 0 && errs == 0 &&
(comm == XDCMD_RD || comm == XDCMD_WR)) {
/* we just successfully processed a bad144 sector
* note: if we are in bad 144 mode, the pointers have
* been advanced already (see above) and are pointing
* at the bad144 sector. to exit bad144 mode, we
* must advance the pointers 1 sector and issue a new
* request if there are still sectors left to process
*
*/
XDC_ADVANCE(iorq, 1); /* advance 1 sector */
/*
* xdc_perror: print error.
* - if still_trying is true: we got an error, retried and got a
* different error. in that case lasterror is the old error,
* and errno is the new one.
* - if still_trying is not true, then if we ever had an error it
* is in lasterror. also, if iorq->errno == 0, then we recovered
* from that error (otherwise iorq->errno == iorq->lasterror).
*/
void
xdc_perror(struct xd_iorq *iorq, struct xd_iopb *iopb, int still_trying)
{
int error = iorq->lasterror;
if (still_trying)
printf(" [still trying, new error=%s]", xdc_e2str(iorq->errno));
else
if (iorq->errno == 0)
printf(" [recovered in %d tries]", iorq->tries);
printf("\n");
}
/*
* xdc_error: non-fatal error encountered... recover.
* return AOK if resubmitted, return FAIL if this iopb is done
*/
int
xdc_error(struct xdc_softc *xdcsc, struct xd_iorq *iorq, struct xd_iopb *iopb,
int rqno, int comm)
{
int errno = iorq->errno;
int erract = errno & XD_ERA_MASK;
int oldmode, advance, i;
if (erract == XD_ERA_RSET) { /* some errors require a reset */
oldmode = iorq->mode;
iorq->mode = XD_SUB_DONE | (~XD_SUB_MASK & oldmode);
xdcsc->ndone++;
/* make xdc_start ignore us */
xdc_reset(xdcsc, 1, XD_RSET_NONE, errno, iorq->xd);
iorq->mode = oldmode;
xdcsc->ndone--;
}
/* check for read/write to a sector in bad144 table if bad: redirect
* request to bad144 area */
/*
* it isn't a bad144 sector, must be real error! see if we can retry
* it?
*/
if ((iorq->mode & XD_MODE_VERBO) && iorq->lasterror)
xdc_perror(iorq, iopb, 1); /* inform of error state
* change */
iorq->lasterror = errno;
/*
* xdc_ioctlcmd: this function provides a user level interface to the
* controller via ioctl. this allows "format" programs to be written
* in user code, and is also useful for some debugging. we return
* an error code. called at user priority.
*/
int
xdc_ioctlcmd(struct xd_softc *xd, dev_t dev, struct xd_iocmd *xio)
{
int s, err, rqno;
void *dvmabuf = NULL;
struct xdc_softc *xdcsc;
/* check sanity of requested command */
switch (xio->cmd) {
case XDCMD_NOP: /* no op: everything should be zero */
if (xio->subfn || xio->dptr || xio->dlen ||
xio->block || xio->sectcnt)
return EINVAL;
break;
case XDCMD_RD: /* read / write sectors (up to XD_IOCMD_MAXS) */
case XDCMD_WR:
if (xio->subfn || xio->sectcnt > XD_IOCMD_MAXS ||
xio->sectcnt * XDFM_BPS != xio->dlen || xio->dptr == NULL)
return EINVAL;
break;
case XDCMD_SK: /* seek: doesn't seem useful to export this */
return EINVAL;
case XDCMD_WRP: /* write parameters */
return EINVAL; /* not useful, except maybe drive
* parameters... but drive parameters should
* go via disklabel changes */
case XDCMD_RDP: /* read parameters */
if (xio->subfn != XDFUN_DRV ||
xio->dlen || xio->block || xio->dptr)
return EINVAL; /* allow read drive params to
* get hw_spt */
xio->sectcnt = xd->hw_spt; /* we already know the answer */
return 0;
break;
case XDCMD_XRD: /* extended read/write */
case XDCMD_XWR:
done:
splx(s);
if (dvmabuf)
dvma_free(dvmabuf, xio->dlen);
return err;
}
/*
* xdc_e2str: convert error code number into an error string
*/
const char *
xdc_e2str(int no)
{
switch (no) {
case XD_ERR_FAIL:
return "Software fatal error";
case XD_ERR_AOK:
return "Successful completion";
case XD_ERR_ICYL:
return "Illegal cylinder address";
case XD_ERR_IHD:
return "Illegal head address";
case XD_ERR_ISEC:
return "Illgal sector address";
case XD_ERR_CZER:
return "Count zero";
case XD_ERR_UIMP:
return "Unimplemented command";
case XD_ERR_IF1:
return "Illegal field length 1";
case XD_ERR_IF2:
return "Illegal field length 2";
case XD_ERR_IF3:
return "Illegal field length 3";
case XD_ERR_IF4:
return "Illegal field length 4";
case XD_ERR_IF5:
return "Illegal field length 5";
case XD_ERR_IF6:
return "Illegal field length 6";
case XD_ERR_IF7:
return "Illegal field length 7";
case XD_ERR_ISG:
return "Illegal scatter/gather length";
case XD_ERR_ISPT:
return "Not enough sectors per track";
case XD_ERR_ALGN:
return "Next IOPB address alignment error";
case XD_ERR_SGAL:
return "Scatter/gather address alignment error";
case XD_ERR_SGEC:
return "Scatter/gather with auto-ECC";
case XD_ERR_SECC:
return "Soft ECC corrected";
case XD_ERR_SIGN:
return "ECC ignored";
case XD_ERR_ASEK:
return "Auto-seek retry recovered";
case XD_ERR_RTRY:
return "Soft retry recovered";
case XD_ERR_HECC:
return "Hard data ECC";
case XD_ERR_NHDR:
return "Header not found";
case XD_ERR_NRDY:
return "Drive not ready";
case XD_ERR_TOUT:
return "Operation timeout";
case XD_ERR_VTIM:
return "VMEDMA timeout";
case XD_ERR_DSEQ:
return "Disk sequencer error";
case XD_ERR_HDEC:
return "Header ECC error";
case XD_ERR_RVFY:
return "Read verify";
case XD_ERR_VFER:
return "Fatail VMEDMA error";
case XD_ERR_VBUS:
return "VMEbus error";
case XD_ERR_DFLT:
return "Drive faulted";
case XD_ERR_HECY:
return "Header error/cylinder";
case XD_ERR_HEHD:
return "Header error/head";
case XD_ERR_NOCY:
return "Drive not on-cylinder";
case XD_ERR_SEEK:
return "Seek error";
case XD_ERR_ILSS:
return "Illegal sector size";
case XD_ERR_SEC:
return "Soft ECC";
case XD_ERR_WPER:
return "Write-protect error";
case XD_ERR_IRAM:
return "IRAM self test failure";
case XD_ERR_MT3:
return "Maintenance test 3 failure (DSKCEL RAM)";
case XD_ERR_MT4:
return "Maintenance test 4 failure (header shift reg)";
case XD_ERR_MT5:
return "Maintenance test 5 failure (VMEDMA regs)";
case XD_ERR_MT6:
return "Maintenance test 6 failure (REGCEL chip)";
case XD_ERR_MT7:
return "Maintenance test 7 failure (buffer parity)";
case XD_ERR_MT8:
return "Maintenance test 8 failure (disk FIFO)";
case XD_ERR_IOCK:
return "IOPB checksum miscompare";
case XD_ERR_IODM:
return "IOPB DMA fatal";
case XD_ERR_IOAL:
return "IOPB address alignment error";
case XD_ERR_FIRM:
return "Firmware error";
case XD_ERR_MMOD:
return "Illegal maintenance mode test number";
case XD_ERR_ACFL:
return "ACFAIL asserted";
default:
return "Unknown error";
}
}