/*-
* Copyright (c) 1996-2003 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe.
*
* 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``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 FOUNDATION OR CONTRIBUTORS
* 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.
*/
/*
* Copyright (c) 1988 University of Utah.
* Copyright (c) 1982, 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* the Systems Programming Group of the University of Utah Computer
* Science Department.
*
* 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. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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.
*
* from: Utah $Hdr: rd.c 1.44 92/12/26$
*
* @(#)rd.c 8.2 (Berkeley) 5/19/94
*/
u_int16_t sc_type;
u_int8_t *sc_addr;
int sc_resid;
struct rd_iocmd sc_ioc;
struct bufq_state *sc_tab;
int sc_active;
int sc_errcnt;
struct callout sc_restart_ch;
krndsource_t rnd_source;
};
#define RDUNIT(dev) DISKUNIT(dev)
#define RDPART(dev) DISKPART(dev)
#define RDMAKEDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part)
#define RDLABELDEV(dev) (RDMAKEDEV(major(dev), RDUNIT(dev), RAW_PART))
#define RDRETRY 5
#define RDWAITC 1 /* min time for timeout in seconds */
int rderrthresh = RDRETRY-1; /* when to start reporting errors */
/*
* Misc. HW description, indexed by sc_type.
* Used for mapping 256-byte sectors for 512-byte sectors
*/
const struct rdidentinfo {
u_int16_t ri_hwid; /* 2 byte HW id */
u_int16_t ri_maxunum; /* maximum allowed unit number */
const char *ri_desc; /* drive type description */
int ri_nbpt; /* DEV_BSIZE blocks per track */
int ri_ntpc; /* tracks per cylinder */
int ri_ncyl; /* cylinders per unit */
int ri_nblocks; /* DEV_BSIZE blocks on disk */
} rdidentinfo[] = {
{ RD7946AID, 0, "7945A", NRD7945ABPT,
NRD7945ATRK, 968, 108416 },
int
rdlookup(int id, int slave, int punit)
{
int i;
for (i = 0; i < numrdidentinfo; i++) {
if (rdidentinfo[i].ri_hwid == id)
break;
}
if (i == numrdidentinfo || punit > rdidentinfo[i].ri_maxunum)
return (-1);
return (i);
}
/*
* Take care of a couple of anomalies:
* 1. 7945A and 7946A both return same HW id
* 2. 9122S and 9134D both return same HW id
* 3. 9122D and 9134L both return same HW id
*/
switch (ca->ca_id) {
case RD7946AID:
if (memcmp(name, "079450", 6) == 0)
type = RD7945A;
else
type = RD7946A;
break;
case RD9134LID:
if (memcmp(name, "091340", 6) == 0)
type = RD9134L;
else
type = RD9122D;
break;
case RD9134DID:
if (memcmp(name, "091220", 6) == 0)
type = RD9122S;
else
type = RD9134D;
break;
}
sc->sc_type = type;
/*
* XXX We use DEV_BSIZE instead of the sector size value pulled
* XXX off the driver because all of this code assumes 512 byte
* XXX blocks. ICK!
*/
printf(": %s\n", rdidentinfo[type].ri_desc);
printf("%s: %d cylinders, %d heads, %d blocks, %d bytes/block\n",
device_xname(sc->sc_dev), rdidentinfo[type].ri_ncyl,
rdidentinfo[type].ri_ntpc, rdidentinfo[type].ri_nblocks,
DEV_BSIZE);
bufq_alloc(&sc->sc_tab, "fcfs", 0);
/*
* Initialize and attach the disk structure.
*/
memset(&sc->sc_dk, 0, sizeof(sc->sc_dk));
disk_init(&sc->sc_dk, device_xname(sc->sc_dev), NULL);
disk_attach(&sc->sc_dk);
/*
* Wait for any pending opens/closes to complete
*/
while (sc->sc_flags & (RDF_OPENING | RDF_CLOSING))
(void) tsleep(sc, PRIBIO, "rdopen", 0);
/*
* On first open, get label and partition info.
* We may block reading the label, so be careful
* to stop any other opens.
*/
if (sc->sc_dk.dk_openmask == 0) {
sc->sc_flags |= RDF_OPENING;
error = rdgetinfo(sc);
sc->sc_flags &= ~RDF_OPENING;
wakeup((void *)sc);
if (error)
return (error);
}
part = RDPART(dev);
mask = 1 << part;
/* Check that the partition exists. */
if (part != RAW_PART && (part > sc->sc_dk.dk_label->d_npartitions ||
sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED))
return (ENXIO);
/* Ensure only one open at a time. */
switch (mode) {
case S_IFCHR:
sc->sc_dk.dk_copenmask |= mask;
break;
case S_IFBLK:
sc->sc_dk.dk_bopenmask |= mask;
break;
}
sc->sc_dk.dk_openmask =
sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
return (0);
}
int
rdclose(dev_t dev, int flag, int mode, struct lwp *l)
{
struct rd_softc *sc;
struct disk *dk;
int mask, s;
sc = device_lookup_private(&rd_cd, RDUNIT(dev));
if (sc == NULL)
return (ENXIO);
dk = &sc->sc_dk;
mask = 1 << RDPART(dev);
if (mode == S_IFCHR)
dk->dk_copenmask &= ~mask;
else
dk->dk_bopenmask &= ~mask;
dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
/*
* On last close, we wait for all activity to cease since
* the label/partition info will become invalid. Since we
* might sleep, we must block any opens while we are here.
* Note we don't have to about other closes since we know
* we are the last one.
*/
if (dk->dk_openmask == 0) {
sc->sc_flags |= RDF_CLOSING;
s = splbio();
while (sc->sc_active) {
sc->sc_flags |= RDF_WANTED;
(void) tsleep(&sc->sc_tab, PRIBIO, "rdclose", 0);
}
splx(s);
sc->sc_flags &= ~(RDF_CLOSING | RDF_WLABEL);
wakeup((void *)sc);
}
return (0);
}
void
rdstrategy(struct buf *bp)
{
struct rd_softc *sc;
struct partition *pinfo;
daddr_t bn;
int sz, s;
int offset;
/*
* Called from timeout() when handling maintenance releases
* callout from timeouts
*/
void
rdrestart(void *arg)
{
int s = splbio();
rdustart((struct rd_softc *)arg);
splx(s);
}
/* called by rdstrategy() to start a block transfer */
/* called by rdrestart() when handingly timeouts */
/* called by rdintr() */
void
rdustart(struct rd_softc *sc)
{
struct buf *bp;
bp = bufq_peek(sc->sc_tab);
sc->sc_addr = bp->b_data;
sc->sc_resid = bp->b_bcount;
if (gpibrequest(sc->sc_ic, sc->sc_hdl))
rdstart(sc);
}
/* called from rdustart() to start a transfer */
/* called from gpib interface as the initiator */
void
rdstart(struct rd_softc *sc)
{
struct buf *bp = bufq_peek(sc->sc_tab);
int slave, punit;
if (gpibsend(sc->sc_ic, slave, CS80CMD_SCMD, &sc->sc_ioc.c_unit,
sizeof(sc->sc_ioc)-1) == sizeof(sc->sc_ioc)-1) {
/* Instrumentation. */
disk_busy(&sc->sc_dk);
iostat_seek(sc->sc_dk.dk_stats);
gpibawait(sc->sc_ic);
return;
}
/*
* Experience has shown that the gpibwait in this gpibsend will
* occasionally timeout. It appears to occur mostly on old 7914
* drives with full maintenance tracks. We should probably
* integrate this with the backoff code in rderror.
*/
if (sc->sc_flags & RDF_SEEK) {
sc->sc_flags &= ~RDF_SEEK;
dir = (bp->b_flags & B_READ ? GPIB_READ : GPIB_WRITE);
gpibxfer(sc->sc_ic, slave, CS80CMD_EXEC, sc->sc_addr,
sc->sc_resid, dir, dir == GPIB_READ);
disk_busy(&sc->sc_dk);
return;
}
if ((sc->sc_flags & RDF_SWAIT) == 0) {
if (gpibpptest(sc->sc_ic, slave) == 0) {
/* Instrumentation. */
disk_busy(&sc->sc_dk);
sc->sc_flags |= RDF_SWAIT;
gpibawait(sc->sc_ic);
return;
}
} else
sc->sc_flags &= ~RDF_SWAIT;
rv = gpibrecv(sc->sc_ic, slave, CS80CMD_QSTAT, &stat, 1);
if (rv != 1 || stat) {
DPRINTF(RDB_ERROR,
("rdintr: receive failed (rv=%d) or bad stat %d\n", rv,
stat));
restart = rderror(sc);
if (sc->sc_errcnt++ < RDRETRY) {
if (restart)
rdstart(sc);
return;
}
bp->b_error = EIO;
}
if (rdfinish(sc, bp) != NULL)
rdustart(sc);
rnd_add_uint32(&sc->rnd_source, bp->b_blkno);
}
/*
* Deal with errors.
* Returns 1 if request should be restarted,
* 0 if we should just quietly give up.
*/
int
rderror(struct rd_softc *sc)
{
struct cs80_stat css;
struct buf *bp;
daddr_t hwbn, pbn;
DPRINTF(RDB_FOLLOW, ("rderror: sc=%p\n", sc));
if (cs80status(device_parent(sc->sc_dev), sc->sc_slave,
sc->sc_punit, &css)) {
cs80reset(device_parent(sc->sc_dev), sc->sc_slave,
sc->sc_punit);
return (1);
}
#ifdef DEBUG
if (rddebug & RDB_ERROR) { /* status info */
printf("\n volume: %d, unit: %d\n",
(css.c_vu>>4)&0xF, css.c_vu&0xF);
printf(" reject 0x%x\n", css.c_ref);
printf(" fault 0x%x\n", css.c_fef);
printf(" access 0x%x\n", css.c_aef);
printf(" info 0x%x\n", css.c_ief);
printf(" block, P1-P10: ");
printf("0x%x", *(u_int32_t *)&css.c_raw[0]);
printf("0x%x", *(u_int32_t *)&css.c_raw[4]);
printf("0x%x\n", *(u_int16_t *)&css.c_raw[8]);
}
#endif
if (css.c_fef & FEF_REXMT)
return (1);
if (css.c_fef & FEF_PF) {
cs80reset(device_parent(sc->sc_dev), sc->sc_slave,
sc->sc_punit);
return (1);
}
/*
* Unit requests release for internal maintenance.
* We just delay awhile and try again later. Use exponentially
* increasing backoff ala ethernet drivers since we don't really
* know how long the maintenance will take. With RDWAITC and
* RDRETRY as defined, the range is 1 to 32 seconds.
*/
if (css.c_fef & FEF_IMR) {
extern int hz;
int rdtimo = RDWAITC << sc->sc_errcnt;
DPRINTF(RDB_STATUS,
("%s: internal maintenance, %d-second timeout\n",
device_xname(sc->sc_dev), rdtimo));
gpibrelease(sc->sc_ic, sc->sc_hdl);
callout_reset(&sc->sc_restart_ch, rdtimo * hz, rdrestart, sc);
return (0);
}
/*
* Only report error if we have reached the error reporting
* threshold. By default, this will only report after the
* retry limit has been exceeded.
*/
if (sc->sc_errcnt < rderrthresh)
return (1);
/*
* First conjure up the block number at which the error occurred.
*/
bp = bufq_peek(sc->sc_tab);
pbn = sc->sc_dk.dk_label->d_partitions[RDPART(bp->b_dev)].p_offset;
if ((css.c_fef & FEF_CU) || (css.c_fef & FEF_DR) ||
(css.c_ief & IEF_RRMASK)) {
/*
* Not all errors report a block number, just use b_blkno.
*/
hwbn = RDBTOS(pbn + bp->b_blkno);
pbn = bp->b_blkno;
} else {
hwbn = css.c_blk;
pbn = RDSTOB(hwbn) - pbn;
}
#ifdef DEBUG
if (rddebug & RDB_ERROR) { /* status info */
printf("\n volume: %d, unit: %d\n",
(css.c_vu>>4)&0xF, css.c_vu&0xF);
printf(" reject 0x%x\n", css.c_ref);
printf(" fault 0x%x\n", css.c_fef);
printf(" access 0x%x\n", css.c_aef);
printf(" info 0x%x\n", css.c_ief);
printf(" block, P1-P10: ");
printf(" block: %" PRId64 ", P1-P10: ", hwbn);
printf("0x%x", *(u_int32_t *)&css.c_raw[0]);
printf("0x%x", *(u_int32_t *)&css.c_raw[4]);
printf("0x%x\n", *(u_int16_t *)&css.c_raw[8]);
}
#endif
#ifdef DEBUG
if (rddebug & RDB_ERROR) { /* command */
printf(" ioc: ");
printf("0x%x", *(u_int32_t *)&sc->sc_ioc.c_pad);
printf("0x%x", *(u_int16_t *)&sc->sc_ioc.c_hiaddr);
printf("0x%x", *(u_int32_t *)&sc->sc_ioc.c_addr);
printf("0x%x", *(u_int16_t *)&sc->sc_ioc.c_nop2);
printf("0x%x", *(u_int32_t *)&sc->sc_ioc.c_len);
printf("0x%x\n", *(u_int16_t *)&sc->sc_ioc.c_cmd);
return (1);
}
#endif
/*
* Now output a generic message suitable for badsect.
* Note that we don't use harderr because it just prints
* out b_blkno which is just the beginning block number
* of the transfer, not necessary where the error occurred.
*/
printf("%s%c: hard error, sector number %" PRId64 "\n",
device_xname(sc->sc_dev), 'a'+RDPART(bp->b_dev), pbn);
/*
* Now report the status as returned by the hardware with
* attempt at interpretation.
*/
printf("%s %s error:", device_xname(sc->sc_dev),
(bp->b_flags & B_READ) ? "read" : "write");
printf(" unit %d, volume %d R0x%x F0x%x A0x%x I0x%x\n",
css.c_vu&0xF, (css.c_vu>>4)&0xF,
css.c_ref, css.c_fef, css.c_aef, css.c_ief);
printf("P1-P10: ");
printf("0x%x ", *(u_int32_t *)&css.c_raw[0]);
printf("0x%x ", *(u_int32_t *)&css.c_raw[4]);
printf("0x%x\n", *(u_int16_t *)&css.c_raw[8]);
return (1);
}
int
rdread(dev_t dev, struct uio *uio, int flags)
{
/*
* We get called very early on (via swapconf)
* without the device being open so we may need
* to handle it here.
*/
if (sc->sc_dk.dk_openmask == 0) {
if (rdopen(dev, FREAD | FWRITE, S_IFBLK, NULL))
return (-1);
didopen = 1;
}
psize = sc->sc_dk.dk_label->d_partitions[RDPART(dev)].p_size *
(sc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
if (didopen)
(void) rdclose(dev, FREAD | FWRITE, S_IFBLK, NULL);
return (psize);
}
static int rddoingadump; /* simple mutex */
/*
* Non-interrupt driven, non-dma dump routine.
*/
int
rddump(dev_t dev, daddr_t blkno, void *va, size_t size)
{
struct rd_softc *sc;
int sectorsize; /* size of a disk sector */
int nsects; /* number of sectors in partition */
int sectoff; /* sector offset of partition */
int totwrt; /* total number of sectors left to write */
int nwrt; /* current number of sectors to write */
int slave;
struct disklabel *lp;
u_int8_t stat;
/* Check for recursive dump; if so, punt. */
if (rddoingadump)
return (EFAULT);
rddoingadump = 1;
/*
* Convert to disk sectors. Request must be a multiple of size.
*/
lp = sc->sc_dk.dk_label;
sectorsize = lp->d_secsize;
if ((size % sectorsize) != 0)
return (EFAULT);
totwrt = size / sectorsize;
blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */