Home | History | Annotate | Line # | Download | only in dev
rd.c revision 1.9
      1 /*
      2  * Copyright (c) 1988 University of Utah.
      3  * Copyright (c) 1982, 1990, 1993
      4  *	The Regents of the University of California.  All rights reserved.
      5  *
      6  * This code is derived from software contributed to Berkeley by
      7  * the Systems Programming Group of the University of Utah Computer
      8  * Science Department.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  * from: Utah $Hdr: rd.c 1.44 92/12/26$
     39  *
     40  *	from: @(#)rd.c	8.2 (Berkeley) 5/19/94
     41  *	$Id: rd.c,v 1.9 1994/05/27 17:18:50 mycroft Exp $
     42  */
     43 
     44 /*
     45  * CS80/SS80 disk driver
     46  */
     47 #include "rd.h"
     48 #if NRD > 0
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/buf.h>
     53 #include <sys/stat.h>
     54 #include <sys/dkstat.h>
     55 #include <sys/disklabel.h>
     56 #include <sys/ioctl.h>
     57 #include <sys/fcntl.h>
     58 
     59 #include <hp300/dev/device.h>
     60 #include <hp300/dev/rdreg.h>
     61 #include <hp300/dev/rdvar.h>
     62 #ifdef USELEDS
     63 #include <hp300/hp300/led.h>
     64 #endif
     65 
     66 #include <vm/vm_param.h>
     67 #include <vm/lock.h>
     68 #include <vm/vm_prot.h>
     69 #include <vm/pmap.h>
     70 
     71 int	rdinit(), rdstart(), rdgo(), rdintr();
     72 void	rdstrategy();
     73 struct	driver rddriver = {
     74 	rdinit, "rd", rdstart, rdgo, rdintr,
     75 };
     76 
     77 struct	rd_softc rd_softc[NRD];
     78 struct	buf rdtab[NRD];
     79 int	rderrthresh = RDRETRY-1;	/* when to start reporting errors */
     80 
     81 #ifdef DEBUG
     82 /* error message tables */
     83 char *err_reject[] = {
     84 	0, 0,
     85 	"channel parity error",		/* 0x2000 */
     86 	0, 0,
     87 	"illegal opcode",		/* 0x0400 */
     88 	"module addressing",		/* 0x0200 */
     89 	"address bounds",		/* 0x0100 */
     90 	"parameter bounds",		/* 0x0080 */
     91 	"illegal parameter",		/* 0x0040 */
     92 	"message sequence",		/* 0x0020 */
     93 	0,
     94 	"message length",		/* 0x0008 */
     95 	0, 0, 0
     96 };
     97 
     98 char *err_fault[] = {
     99 	0,
    100 	"cross unit",			/* 0x4000 */
    101 	0,
    102 	"controller fault",		/* 0x1000 */
    103 	0, 0,
    104 	"unit fault",			/* 0x0200 */
    105 	0,
    106 	"diagnostic result",		/* 0x0080 */
    107 	0,
    108 	"operator release request",	/* 0x0020 */
    109 	"diagnostic release request",	/* 0x0010 */
    110 	"internal maintenance release request",	/* 0x0008 */
    111 	0,
    112 	"power fail",			/* 0x0002 */
    113 	"retransmit"			/* 0x0001 */
    114 };
    115 
    116 char *err_access[] = {
    117 	"illegal parallel operation",	/* 0x8000 */
    118 	"uninitialized media",		/* 0x4000 */
    119 	"no spares available",		/* 0x2000 */
    120 	"not ready",			/* 0x1000 */
    121 	"write protect",		/* 0x0800 */
    122 	"no data found",		/* 0x0400 */
    123 	0, 0,
    124 	"unrecoverable data overflow",	/* 0x0080 */
    125 	"unrecoverable data",		/* 0x0040 */
    126 	0,
    127 	"end of file",			/* 0x0010 */
    128 	"end of volume",		/* 0x0008 */
    129 	0, 0, 0
    130 };
    131 
    132 char *err_info[] = {
    133 	"operator release request",	/* 0x8000 */
    134 	"diagnostic release request",	/* 0x4000 */
    135 	"internal maintenance release request",	/* 0x2000 */
    136 	"media wear",			/* 0x1000 */
    137 	"latency induced",		/* 0x0800 */
    138 	0, 0,
    139 	"auto sparing invoked",		/* 0x0100 */
    140 	0,
    141 	"recoverable data overflow",	/* 0x0040 */
    142 	"marginal data",		/* 0x0020 */
    143 	"recoverable data",		/* 0x0010 */
    144 	0,
    145 	"maintenance track overflow",	/* 0x0004 */
    146 	0, 0
    147 };
    148 
    149 struct	rdstats rdstats[NRD];
    150 int	rddebug = 0x80;
    151 #define RDB_FOLLOW	0x01
    152 #define RDB_STATUS	0x02
    153 #define RDB_IDENT	0x04
    154 #define RDB_IO		0x08
    155 #define RDB_ASYNC	0x10
    156 #define RDB_ERROR	0x80
    157 #endif
    158 
    159 /*
    160  * Misc. HW description, indexed by sc_type.
    161  * Nothing really critical here, could do without it.
    162  */
    163 struct rdidentinfo rdidentinfo[] = {
    164 	{ RD7946AID,	0,	"7945A",	 108416 },
    165 	{ RD9134DID,	1,	"9134D",	  29088 },
    166 	{ RD9134LID,	1,	"9122S",	   1232 },
    167 	{ RD7912PID,	0,	"7912P",	 128128 },
    168 	{ RD7914PID,	0,	"7914P",	 258048 },
    169 	{ RD7958AID,	0,	"7958A",	 255276 },
    170 	{ RD7957AID,	0,	"7957A",	 159544 },
    171 	{ RD7933HID,	0,	"7933H",	 789958 },
    172 	{ RD9134LID,	1,	"9134L",	  77840 },
    173 	{ RD7936HID,	0,	"7936H",	 600978 },
    174 	{ RD7937HID,	0,	"7937H",	1116102 },
    175 	{ RD7914CTID,	0,	"7914CT",	 258048 },
    176 	{ RD7946AID,	0,	"7946A",	 108416 },
    177 	{ RD9134LID,	1,	"9122D",	   1232 },
    178 	{ RD7957BID,	0,	"7957B",	 159894 },
    179 	{ RD7958BID,	0,	"7958B",	 297108 },
    180 	{ RD7959BID,	0,	"7959B",	 594216 },
    181 	{ RD2200AID,	0,	"2200A",	 654948 },
    182 	{ RD2203AID,	0,	"2203A",	1309896 }
    183 };
    184 int numrdidentinfo = sizeof(rdidentinfo) / sizeof(rdidentinfo[0]);
    185 
    186 rdinit(hd)
    187 	register struct hp_device *hd;
    188 {
    189 	register struct rd_softc *rs = &rd_softc[hd->hp_unit];
    190 
    191 	rs->sc_hd = hd;
    192 	rs->sc_punit = rdpunit(hd->hp_flags);
    193 	rs->sc_type = rdident(rs, hd);
    194 	if (rs->sc_type < 0)
    195 		return(0);
    196 	rs->sc_dq.dq_ctlr = hd->hp_ctlr;
    197 	rs->sc_dq.dq_unit = hd->hp_unit;
    198 	rs->sc_dq.dq_slave = hd->hp_slave;
    199 	rs->sc_dq.dq_driver = &rddriver;
    200 	rs->sc_flags = RDF_ALIVE;
    201 #ifdef DEBUG
    202 	/* always report errors */
    203 	if (rddebug & RDB_ERROR)
    204 		rderrthresh = 0;
    205 #endif
    206 	return(1);
    207 }
    208 
    209 rdident(rs, hd)
    210 	struct rd_softc *rs;
    211 	struct hp_device *hd;
    212 {
    213 	struct rd_describe desc;
    214 	u_char stat, cmd[3];
    215 	int unit, lunit;
    216 	char name[7];
    217 	register int ctlr, slave, id, i;
    218 
    219 	ctlr = hd->hp_ctlr;
    220 	slave = hd->hp_slave;
    221 	unit = rs->sc_punit;
    222 	lunit = hd->hp_unit;
    223 
    224 	/*
    225 	 * Grab device id and make sure:
    226 	 * 1. It is a CS80 device.
    227 	 * 2. It is one of the types we support.
    228 	 * 3. If it is a 7946, we are accessing the disk unit (0)
    229 	 */
    230 	id = hpibid(ctlr, slave);
    231 #ifdef DEBUG
    232 	if (rddebug & RDB_IDENT)
    233 		printf("hpibid(%d, %d) -> %x\n", ctlr, slave, id);
    234 #endif
    235 	if ((id & 0x200) == 0)
    236 		return(-1);
    237 	for (i = 0; i < numrdidentinfo; i++)
    238 		if (id == rdidentinfo[i].ri_hwid)
    239 			break;
    240 	if (i == numrdidentinfo || unit > rdidentinfo[i].ri_maxunum)
    241 		return(-1);
    242 	id = i;
    243 
    244 	/*
    245 	 * Reset drive and collect device description.
    246 	 * Don't really use the description info right now but
    247 	 * might come in handy in the future (for disk labels).
    248 	 */
    249 	rdreset(rs, hd);
    250 	cmd[0] = C_SUNIT(unit);
    251 	cmd[1] = C_SVOL(0);
    252 	cmd[2] = C_DESC;
    253 	hpibsend(ctlr, slave, C_CMD, cmd, sizeof(cmd));
    254 	hpibrecv(ctlr, slave, C_EXEC, &desc, 37);
    255 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
    256 	bzero(name, sizeof(name));
    257 	if (!stat) {
    258 		register int n = desc.d_name;
    259 		for (i = 5; i >= 0; i--) {
    260 			name[i] = (n & 0xf) + '0';
    261 			n >>= 4;
    262 		}
    263 		/* use drive characteristics to calculate xfer rate */
    264 		rs->sc_wpms = 1000000 * (desc.d_sectsize/2) / desc.d_blocktime;
    265 	}
    266 #ifdef DEBUG
    267 	if (rddebug & RDB_IDENT) {
    268 		printf("rd%d: name: %x ('%s')\n",
    269 		       lunit, desc.d_name, name);
    270 		printf("  iuw %x, maxxfr %d, ctype %d\n",
    271 		       desc.d_iuw, desc.d_cmaxxfr, desc.d_ctype);
    272 		printf("  utype %d, bps %d, blkbuf %d, burst %d, blktime %d\n",
    273 		       desc.d_utype, desc.d_sectsize,
    274 		       desc.d_blkbuf, desc.d_burstsize, desc.d_blocktime);
    275 		printf("  avxfr %d, ort %d, atp %d, maxint %d, fv %x, rv %x\n",
    276 		       desc.d_uavexfr, desc.d_retry, desc.d_access,
    277 		       desc.d_maxint, desc.d_fvbyte, desc.d_rvbyte);
    278 		printf("  maxcyl/head/sect %d/%d/%d, maxvsect %d, inter %d\n",
    279 		       desc.d_maxcyl, desc.d_maxhead, desc.d_maxsect,
    280 		       desc.d_maxvsectl, desc.d_interleave);
    281 	}
    282 #endif
    283 	/*
    284 	 * Take care of a couple of anomolies:
    285 	 * 1. 7945A and 7946A both return same HW id
    286 	 * 2. 9122S and 9134D both return same HW id
    287 	 * 3. 9122D and 9134L both return same HW id
    288 	 */
    289 	switch (rdidentinfo[id].ri_hwid) {
    290 	case RD7946AID:
    291 		if (bcmp(name, "079450", 6) == 0)
    292 			id = RD7945A;
    293 		else
    294 			id = RD7946A;
    295 		break;
    296 
    297 	case RD9134LID:
    298 		if (bcmp(name, "091340", 6) == 0)
    299 			id = RD9134L;
    300 		else
    301 			id = RD9122D;
    302 		break;
    303 
    304 	case RD9134DID:
    305 		if (bcmp(name, "091220", 6) == 0)
    306 			id = RD9122S;
    307 		else
    308 			id = RD9134D;
    309 		break;
    310 	}
    311 	printf("rd%d: %s\n", lunit, rdidentinfo[id].ri_desc);
    312 	return(id);
    313 }
    314 
    315 rdreset(rs, hd)
    316 	register struct rd_softc *rs;
    317 	register struct hp_device *hd;
    318 {
    319 	u_char stat;
    320 
    321 	rs->sc_clear.c_unit = C_SUNIT(rs->sc_punit);
    322 	rs->sc_clear.c_cmd = C_CLEAR;
    323 	hpibsend(hd->hp_ctlr, hd->hp_slave, C_TCMD, &rs->sc_clear,
    324 		sizeof(rs->sc_clear));
    325 	hpibswait(hd->hp_ctlr, hd->hp_slave);
    326 	hpibrecv(hd->hp_ctlr, hd->hp_slave, C_QSTAT, &stat, sizeof(stat));
    327 	rs->sc_src.c_unit = C_SUNIT(RDCTLR);
    328 	rs->sc_src.c_nop = C_NOP;
    329 	rs->sc_src.c_cmd = C_SREL;
    330 	rs->sc_src.c_param = C_REL;
    331 	hpibsend(hd->hp_ctlr, hd->hp_slave, C_CMD, &rs->sc_src,
    332 		sizeof(rs->sc_src));
    333 	hpibswait(hd->hp_ctlr, hd->hp_slave);
    334 	hpibrecv(hd->hp_ctlr, hd->hp_slave, C_QSTAT, &stat, sizeof(stat));
    335 	rs->sc_ssmc.c_unit = C_SUNIT(rs->sc_punit);
    336 	rs->sc_ssmc.c_cmd = C_SSM;
    337 	rs->sc_ssmc.c_refm = REF_MASK;
    338 	rs->sc_ssmc.c_fefm = FEF_MASK;
    339 	rs->sc_ssmc.c_aefm = AEF_MASK;
    340 	rs->sc_ssmc.c_iefm = IEF_MASK;
    341 	hpibsend(hd->hp_ctlr, hd->hp_slave, C_CMD, &rs->sc_ssmc,
    342 		sizeof(rs->sc_ssmc));
    343 	hpibswait(hd->hp_ctlr, hd->hp_slave);
    344 	hpibrecv(hd->hp_ctlr, hd->hp_slave, C_QSTAT, &stat, sizeof(stat));
    345 #ifdef DEBUG
    346 	rdstats[hd->hp_unit].rdresets++;
    347 #endif
    348 }
    349 
    350 /*
    351  * Read or constuct a disklabel
    352  */
    353 int
    354 rdgetinfo(dev)
    355 	dev_t dev;
    356 {
    357 	int unit = rdunit(dev);
    358 	register struct rd_softc *rs = &rd_softc[unit];
    359 	register struct disklabel *lp = &rs->sc_info.ri_label;
    360 	register struct partition *pi;
    361 	char *msg, *readdisklabel();
    362 
    363 	/*
    364 	 * Set some default values to use while reading the label
    365 	 * or to use if there isn't a label.
    366 	 */
    367 	bzero((caddr_t)lp, sizeof *lp);
    368 	lp->d_type = DTYPE_HPIB;
    369 	lp->d_secsize = DEV_BSIZE;
    370 	lp->d_nsectors = 32;
    371 	lp->d_ntracks = 20;
    372 	lp->d_ncylinders = 1;
    373 	lp->d_secpercyl = 32*20;
    374 	lp->d_npartitions = 3;
    375 	lp->d_partitions[2].p_offset = 0;
    376 	lp->d_partitions[2].p_size = LABELSECTOR+1;
    377 
    378 	/*
    379 	 * Now try to read the disklabel
    380 	 */
    381 	msg = readdisklabel(rdlabdev(dev), rdstrategy, lp);
    382 	if (msg == NULL)
    383 		return(0);
    384 
    385 	pi = lp->d_partitions;
    386 	printf("rd%d: WARNING: %s, ", unit, msg);
    387 #ifdef COMPAT_NOLABEL
    388 	printf("using old default partitioning\n");
    389 	rdmakedisklabel(unit, lp);
    390 #else
    391 	printf("defining `c' partition as entire disk\n");
    392 	pi[2].p_size = rdidentinfo[rs->sc_type].ri_nblocks;
    393 	/* XXX reset other info since readdisklabel screws with it */
    394 	lp->d_npartitions = 3;
    395 	pi[0].p_size = 0;
    396 #endif
    397 	return(0);
    398 }
    399 
    400 int
    401 rdopen(dev, flags, mode, p)
    402 	dev_t dev;
    403 	int flags, mode;
    404 	struct proc *p;
    405 {
    406 	register int unit = rdunit(dev);
    407 	register struct rd_softc *rs = &rd_softc[unit];
    408 	int error, mask;
    409 
    410 	if (unit >= NRD || (rs->sc_flags & RDF_ALIVE) == 0)
    411 		return(ENXIO);
    412 
    413 	/*
    414 	 * Wait for any pending opens/closes to complete
    415 	 */
    416 	while (rs->sc_flags & (RDF_OPENING|RDF_CLOSING))
    417 		sleep((caddr_t)rs, PRIBIO);
    418 
    419 	/*
    420 	 * On first open, get label and partition info.
    421 	 * We may block reading the label, so be careful
    422 	 * to stop any other opens.
    423 	 */
    424 	if (rs->sc_info.ri_open == 0) {
    425 		rs->sc_flags |= RDF_OPENING;
    426 		error = rdgetinfo(dev);
    427 		rs->sc_flags &= ~RDF_OPENING;
    428 		wakeup((caddr_t)rs);
    429 		if (error)
    430 			return(error);
    431 	}
    432 	if (rs->sc_hd->hp_dk >= 0) {
    433 		/* guess at xfer rate based on 3600 rpm (60 rps) */
    434 		if (rs->sc_wpms == 0)
    435 			rs->sc_wpms = 60 * rs->sc_info.ri_label.d_nsectors
    436 				* DEV_BSIZE / 2;
    437 		dk_wpms[rs->sc_hd->hp_dk] = rs->sc_wpms;
    438 	}
    439 
    440 	mask = 1 << rdpart(dev);
    441 	if (mode == S_IFCHR)
    442 		rs->sc_info.ri_copen |= mask;
    443 	else
    444 		rs->sc_info.ri_bopen |= mask;
    445 	rs->sc_info.ri_open |= mask;
    446 	return(0);
    447 }
    448 
    449 int
    450 rdclose(dev, flag, mode, p)
    451 	dev_t dev;
    452 	int flag, mode;
    453 	struct proc *p;
    454 {
    455 	int unit = rdunit(dev);
    456 	register struct rd_softc *rs = &rd_softc[unit];
    457 	register struct rdinfo *ri = &rs->sc_info;
    458 	int mask, s;
    459 
    460 	mask = 1 << rdpart(dev);
    461 	if (mode == S_IFCHR)
    462 		ri->ri_copen &= ~mask;
    463 	else
    464 		ri->ri_bopen &= ~mask;
    465 	ri->ri_open = ri->ri_bopen | ri->ri_copen;
    466 	/*
    467 	 * On last close, we wait for all activity to cease since
    468 	 * the label/parition info will become invalid.  Since we
    469 	 * might sleep, we must block any opens while we are here.
    470 	 * Note we don't have to about other closes since we know
    471 	 * we are the last one.
    472 	 */
    473 	if (ri->ri_open == 0) {
    474 		rs->sc_flags |= RDF_CLOSING;
    475 		s = splbio();
    476 		while (rdtab[unit].b_active) {
    477 			rs->sc_flags |= RDF_WANTED;
    478 			sleep((caddr_t)&rdtab[unit], PRIBIO);
    479 		}
    480 		splx(s);
    481 		rs->sc_flags &= ~(RDF_CLOSING|RDF_WLABEL);
    482 		wakeup((caddr_t)rs);
    483 	}
    484 	return(0);
    485 }
    486 
    487 void
    488 rdstrategy(bp)
    489 	register struct buf *bp;
    490 {
    491 	int unit = rdunit(bp->b_dev);
    492 	register struct rd_softc *rs = &rd_softc[unit];
    493 	register struct buf *dp = &rdtab[unit];
    494 	register struct partition *pinfo;
    495 	register daddr_t bn;
    496 	register int sz, s;
    497 
    498 #ifdef DEBUG
    499 	if (rddebug & RDB_FOLLOW)
    500 		printf("rdstrategy(%x): dev %x, bn %x, bcount %x, %c\n",
    501 		       bp, bp->b_dev, bp->b_blkno, bp->b_bcount,
    502 		       (bp->b_flags & B_READ) ? 'R' : 'W');
    503 #endif
    504 	bn = bp->b_blkno;
    505 	sz = howmany(bp->b_bcount, DEV_BSIZE);
    506 	pinfo = &rs->sc_info.ri_label.d_partitions[rdpart(bp->b_dev)];
    507 	if (bn < 0 || bn + sz > pinfo->p_size) {
    508 		sz = pinfo->p_size - bn;
    509 		if (sz == 0) {
    510 			bp->b_resid = bp->b_bcount;
    511 			goto done;
    512 		}
    513 		if (sz < 0) {
    514 			bp->b_error = EINVAL;
    515 			goto bad;
    516 		}
    517 		bp->b_bcount = dbtob(sz);
    518 	}
    519 	/*
    520 	 * Check for write to write protected label
    521 	 */
    522 	if (bn + pinfo->p_offset <= LABELSECTOR &&
    523 #if LABELSECTOR != 0
    524 	    bn + pinfo->p_offset + sz > LABELSECTOR &&
    525 #endif
    526 	    !(bp->b_flags & B_READ) && !(rs->sc_flags & RDF_WLABEL)) {
    527 		bp->b_error = EROFS;
    528 		goto bad;
    529 	}
    530 	bp->b_cylin = bn + pinfo->p_offset;
    531 	s = splbio();
    532 	disksort(dp, bp);
    533 	if (dp->b_active == 0) {
    534 		dp->b_active = 1;
    535 		rdustart(unit);
    536 	}
    537 	splx(s);
    538 	return;
    539 bad:
    540 	bp->b_flags |= B_ERROR;
    541 done:
    542 	biodone(bp);
    543 }
    544 
    545 /*
    546  * Called from timeout() when handling maintenance releases
    547  */
    548 void
    549 rdrestart(arg)
    550 	void *arg;
    551 {
    552 	int s = splbio();
    553 	rdustart((int)arg);
    554 	splx(s);
    555 }
    556 
    557 rdustart(unit)
    558 	register int unit;
    559 {
    560 	register struct buf *bp;
    561 	register struct rd_softc *rs = &rd_softc[unit];
    562 
    563 	bp = rdtab[unit].b_actf;
    564 	rs->sc_addr = bp->b_un.b_addr;
    565 	rs->sc_resid = bp->b_bcount;
    566 	if (hpibreq(&rs->sc_dq))
    567 		rdstart(unit);
    568 }
    569 
    570 struct buf *
    571 rdfinish(unit, rs, bp)
    572 	int unit;
    573 	register struct rd_softc *rs;
    574 	register struct buf *bp;
    575 {
    576 	register struct buf *dp = &rdtab[unit];
    577 
    578 	dp->b_errcnt = 0;
    579 	dp->b_actf = bp->b_actf;
    580 	bp->b_resid = 0;
    581 	biodone(bp);
    582 	hpibfree(&rs->sc_dq);
    583 	if (dp->b_actf)
    584 		return(dp->b_actf);
    585 	dp->b_active = 0;
    586 	if (rs->sc_flags & RDF_WANTED) {
    587 		rs->sc_flags &= ~RDF_WANTED;
    588 		wakeup((caddr_t)dp);
    589 	}
    590 	return(NULL);
    591 }
    592 
    593 rdstart(unit)
    594 	register int unit;
    595 {
    596 	register struct rd_softc *rs = &rd_softc[unit];
    597 	register struct buf *bp = rdtab[unit].b_actf;
    598 	register struct hp_device *hp = rs->sc_hd;
    599 	register int part;
    600 
    601 again:
    602 #ifdef DEBUG
    603 	if (rddebug & RDB_FOLLOW)
    604 		printf("rdstart(%d): bp %x, %c\n", unit, bp,
    605 		       (bp->b_flags & B_READ) ? 'R' : 'W');
    606 #endif
    607 	part = rdpart(bp->b_dev);
    608 	rs->sc_flags |= RDF_SEEK;
    609 	rs->sc_ioc.c_unit = C_SUNIT(rs->sc_punit);
    610 	rs->sc_ioc.c_volume = C_SVOL(0);
    611 	rs->sc_ioc.c_saddr = C_SADDR;
    612 	rs->sc_ioc.c_hiaddr = 0;
    613 	rs->sc_ioc.c_addr = RDBTOS(bp->b_cylin);
    614 	rs->sc_ioc.c_nop2 = C_NOP;
    615 	rs->sc_ioc.c_slen = C_SLEN;
    616 	rs->sc_ioc.c_len = rs->sc_resid;
    617 	rs->sc_ioc.c_cmd = bp->b_flags & B_READ ? C_READ : C_WRITE;
    618 #ifdef DEBUG
    619 	if (rddebug & RDB_IO)
    620 		printf("rdstart: hpibsend(%x, %x, %x, %x, %x)\n",
    621 		       hp->hp_ctlr, hp->hp_slave, C_CMD,
    622 		       &rs->sc_ioc.c_unit, sizeof(rs->sc_ioc)-2);
    623 #endif
    624 	if (hpibsend(hp->hp_ctlr, hp->hp_slave, C_CMD, &rs->sc_ioc.c_unit,
    625 		     sizeof(rs->sc_ioc)-2) == sizeof(rs->sc_ioc)-2) {
    626 		if (hp->hp_dk >= 0) {
    627 			dk_busy |= 1 << hp->hp_dk;
    628 			dk_seek[hp->hp_dk]++;
    629 		}
    630 #ifdef DEBUG
    631 		if (rddebug & RDB_IO)
    632 			printf("rdstart: hpibawait(%x)\n", hp->hp_ctlr);
    633 #endif
    634 		hpibawait(hp->hp_ctlr);
    635 		return;
    636 	}
    637 	/*
    638 	 * Experience has shown that the hpibwait in this hpibsend will
    639 	 * occasionally timeout.  It appears to occur mostly on old 7914
    640 	 * drives with full maintenance tracks.  We should probably
    641 	 * integrate this with the backoff code in rderror.
    642 	 */
    643 #ifdef DEBUG
    644 	if (rddebug & RDB_ERROR)
    645 		printf("rd%d: rdstart: cmd %x adr %d blk %d len %d ecnt %d\n",
    646 		       unit, rs->sc_ioc.c_cmd, rs->sc_ioc.c_addr,
    647 		       bp->b_blkno, rs->sc_resid, rdtab[unit].b_errcnt);
    648 	rdstats[unit].rdretries++;
    649 #endif
    650 	rs->sc_flags &= ~RDF_SEEK;
    651 	rdreset(rs, hp);
    652 	if (rdtab[unit].b_errcnt++ < RDRETRY)
    653 		goto again;
    654 	printf("rd%d: rdstart err: cmd 0x%x sect %d blk %d len %d\n",
    655 	       unit, rs->sc_ioc.c_cmd, rs->sc_ioc.c_addr,
    656 	       bp->b_blkno, rs->sc_resid);
    657 	bp->b_flags |= B_ERROR;
    658 	bp->b_error = EIO;
    659 	bp = rdfinish(unit, rs, bp);
    660 	if (bp) {
    661 		rs->sc_addr = bp->b_un.b_addr;
    662 		rs->sc_resid = bp->b_bcount;
    663 		if (hpibreq(&rs->sc_dq))
    664 			goto again;
    665 	}
    666 }
    667 
    668 rdgo(unit)
    669 	register int unit;
    670 {
    671 	register struct rd_softc *rs = &rd_softc[unit];
    672 	register struct hp_device *hp = rs->sc_hd;
    673 	struct buf *bp = rdtab[unit].b_actf;
    674 
    675 	if (hp->hp_dk >= 0) {
    676 		dk_busy |= 1 << hp->hp_dk;
    677 		dk_xfer[hp->hp_dk]++;
    678 		dk_wds[hp->hp_dk] += rs->sc_resid >> 6;
    679 	}
    680 #ifdef USELEDS
    681 	if (inledcontrol == 0)
    682 		ledcontrol(0, 0, LED_DISK);
    683 #endif
    684 	hpibgo(hp->hp_ctlr, hp->hp_slave, C_EXEC,
    685 	       rs->sc_addr, rs->sc_resid, bp->b_flags & B_READ);
    686 }
    687 
    688 rdintr(unit)
    689 	register int unit;
    690 {
    691 	register struct rd_softc *rs = &rd_softc[unit];
    692 	register struct buf *bp = rdtab[unit].b_actf;
    693 	register struct hp_device *hp = rs->sc_hd;
    694 	u_char stat = 13;	/* in case hpibrecv fails */
    695 	int rv, restart;
    696 
    697 #ifdef DEBUG
    698 	if (rddebug & RDB_FOLLOW)
    699 		printf("rdintr(%d): bp %x, %c, flags %x\n", unit, bp,
    700 		       (bp->b_flags & B_READ) ? 'R' : 'W', rs->sc_flags);
    701 	if (bp == NULL) {
    702 		printf("rd%d: bp == NULL\n", unit);
    703 		return;
    704 	}
    705 #endif
    706 	if (hp->hp_dk >= 0)
    707 		dk_busy &= ~(1 << hp->hp_dk);
    708 	if (rs->sc_flags & RDF_SEEK) {
    709 		rs->sc_flags &= ~RDF_SEEK;
    710 		if (hpibustart(hp->hp_ctlr))
    711 			rdgo(unit);
    712 		return;
    713 	}
    714 	if ((rs->sc_flags & RDF_SWAIT) == 0) {
    715 #ifdef DEBUG
    716 		rdstats[unit].rdpolltries++;
    717 #endif
    718 		if (hpibpptest(hp->hp_ctlr, hp->hp_slave) == 0) {
    719 #ifdef DEBUG
    720 			rdstats[unit].rdpollwaits++;
    721 #endif
    722 			if (hp->hp_dk >= 0)
    723 				dk_busy |= 1 << hp->hp_dk;
    724 			rs->sc_flags |= RDF_SWAIT;
    725 			hpibawait(hp->hp_ctlr);
    726 			return;
    727 		}
    728 	} else
    729 		rs->sc_flags &= ~RDF_SWAIT;
    730 	rv = hpibrecv(hp->hp_ctlr, hp->hp_slave, C_QSTAT, &stat, 1);
    731 	if (rv != 1 || stat) {
    732 #ifdef DEBUG
    733 		if (rddebug & RDB_ERROR)
    734 			printf("rdintr: recv failed or bad stat %d\n", stat);
    735 #endif
    736 		restart = rderror(unit);
    737 #ifdef DEBUG
    738 		rdstats[unit].rdretries++;
    739 #endif
    740 		if (rdtab[unit].b_errcnt++ < RDRETRY) {
    741 			if (restart)
    742 				rdstart(unit);
    743 			return;
    744 		}
    745 		bp->b_flags |= B_ERROR;
    746 		bp->b_error = EIO;
    747 	}
    748 	if (rdfinish(unit, rs, bp))
    749 		rdustart(unit);
    750 }
    751 
    752 rdstatus(rs)
    753 	register struct rd_softc *rs;
    754 {
    755 	register int c, s;
    756 	u_char stat;
    757 	int rv;
    758 
    759 	c = rs->sc_hd->hp_ctlr;
    760 	s = rs->sc_hd->hp_slave;
    761 	rs->sc_rsc.c_unit = C_SUNIT(rs->sc_punit);
    762 	rs->sc_rsc.c_sram = C_SRAM;
    763 	rs->sc_rsc.c_ram = C_RAM;
    764 	rs->sc_rsc.c_cmd = C_STATUS;
    765 	bzero((caddr_t)&rs->sc_stat, sizeof(rs->sc_stat));
    766 	rv = hpibsend(c, s, C_CMD, &rs->sc_rsc, sizeof(rs->sc_rsc));
    767 	if (rv != sizeof(rs->sc_rsc)) {
    768 #ifdef DEBUG
    769 		if (rddebug & RDB_STATUS)
    770 			printf("rdstatus: send C_CMD failed %d != %d\n",
    771 			       rv, sizeof(rs->sc_rsc));
    772 #endif
    773 		return(1);
    774 	}
    775 	rv = hpibrecv(c, s, C_EXEC, &rs->sc_stat, sizeof(rs->sc_stat));
    776 	if (rv != sizeof(rs->sc_stat)) {
    777 #ifdef DEBUG
    778 		if (rddebug & RDB_STATUS)
    779 			printf("rdstatus: send C_EXEC failed %d != %d\n",
    780 			       rv, sizeof(rs->sc_stat));
    781 #endif
    782 		return(1);
    783 	}
    784 	rv = hpibrecv(c, s, C_QSTAT, &stat, 1);
    785 	if (rv != 1 || stat) {
    786 #ifdef DEBUG
    787 		if (rddebug & RDB_STATUS)
    788 			printf("rdstatus: recv failed %d or bad stat %d\n",
    789 			       rv, stat);
    790 #endif
    791 		return(1);
    792 	}
    793 	return(0);
    794 }
    795 
    796 /*
    797  * Deal with errors.
    798  * Returns 1 if request should be restarted,
    799  * 0 if we should just quietly give up.
    800  */
    801 rderror(unit)
    802 	int unit;
    803 {
    804 	struct rd_softc *rs = &rd_softc[unit];
    805 	register struct rd_stat *sp;
    806 	struct buf *bp;
    807 	daddr_t hwbn, pbn;
    808 
    809 	if (rdstatus(rs)) {
    810 #ifdef DEBUG
    811 		printf("rd%d: couldn't get status\n", unit);
    812 #endif
    813 		rdreset(rs, rs->sc_hd);
    814 		return(1);
    815 	}
    816 	sp = &rs->sc_stat;
    817 	if (sp->c_fef & FEF_REXMT)
    818 		return(1);
    819 	if (sp->c_fef & FEF_PF) {
    820 		rdreset(rs, rs->sc_hd);
    821 		return(1);
    822 	}
    823 	/*
    824 	 * Unit requests release for internal maintenance.
    825 	 * We just delay awhile and try again later.  Use expontially
    826 	 * increasing backoff ala ethernet drivers since we don't really
    827 	 * know how long the maintenance will take.  With RDWAITC and
    828 	 * RDRETRY as defined, the range is 1 to 32 seconds.
    829 	 */
    830 	if (sp->c_fef & FEF_IMR) {
    831 		extern int hz;
    832 		int rdtimo = RDWAITC << rdtab[unit].b_errcnt;
    833 #ifdef DEBUG
    834 		printf("rd%d: internal maintenance, %d second timeout\n",
    835 		       unit, rdtimo);
    836 		rdstats[unit].rdtimeouts++;
    837 #endif
    838 		hpibfree(&rs->sc_dq);
    839 		timeout(rdrestart, (void *)unit, rdtimo * hz);
    840 		return(0);
    841 	}
    842 	/*
    843 	 * Only report error if we have reached the error reporting
    844 	 * threshhold.  By default, this will only report after the
    845 	 * retry limit has been exceeded.
    846 	 */
    847 	if (rdtab[unit].b_errcnt < rderrthresh)
    848 		return(1);
    849 
    850 	/*
    851 	 * First conjure up the block number at which the error occured.
    852 	 * Note that not all errors report a block number, in that case
    853 	 * we just use b_blkno.
    854  	 */
    855 	bp = rdtab[unit].b_actf;
    856 	pbn = rs->sc_info.ri_label.d_partitions[rdpart(bp->b_dev)].p_offset;
    857 	if ((sp->c_fef & FEF_CU) || (sp->c_fef & FEF_DR) ||
    858 	    (sp->c_ief & IEF_RRMASK)) {
    859 		hwbn = RDBTOS(pbn + bp->b_blkno);
    860 		pbn = bp->b_blkno;
    861 	} else {
    862 		hwbn = sp->c_blk;
    863 		pbn = RDSTOB(hwbn) - pbn;
    864 	}
    865 	/*
    866 	 * Now output a generic message suitable for badsect.
    867 	 * Note that we don't use harderr cuz it just prints
    868 	 * out b_blkno which is just the beginning block number
    869 	 * of the transfer, not necessary where the error occured.
    870 	 */
    871 	printf("rd%d%c: hard error sn%d\n",
    872 	       rdunit(bp->b_dev), 'a'+rdpart(bp->b_dev), pbn);
    873 	/*
    874 	 * Now report the status as returned by the hardware with
    875 	 * attempt at interpretation (unless debugging).
    876 	 */
    877 	printf("rd%d %s error:",
    878 	       unit, (bp->b_flags & B_READ) ? "read" : "write");
    879 #ifdef DEBUG
    880 	if (rddebug & RDB_ERROR) {
    881 		/* status info */
    882 		printf("\n    volume: %d, unit: %d\n",
    883 		       (sp->c_vu>>4)&0xF, sp->c_vu&0xF);
    884 		rdprinterr("reject", sp->c_ref, err_reject);
    885 		rdprinterr("fault", sp->c_fef, err_fault);
    886 		rdprinterr("access", sp->c_aef, err_access);
    887 		rdprinterr("info", sp->c_ief, err_info);
    888 		printf("    block: %d, P1-P10: ", hwbn);
    889 		printf("%s", hexstr(*(u_int *)&sp->c_raw[0], 8));
    890 		printf("%s", hexstr(*(u_int *)&sp->c_raw[4], 8));
    891 		printf("%s\n", hexstr(*(u_short *)&sp->c_raw[8], 4));
    892 		/* command */
    893 		printf("    ioc: ");
    894 		printf("%s", hexstr(*(u_int *)&rs->sc_ioc.c_pad, 8));
    895 		printf("%s", hexstr(*(u_short *)&rs->sc_ioc.c_hiaddr, 4));
    896 		printf("%s", hexstr(*(u_int *)&rs->sc_ioc.c_addr, 8));
    897 		printf("%s", hexstr(*(u_short *)&rs->sc_ioc.c_nop2, 4));
    898 		printf("%s", hexstr(*(u_int *)&rs->sc_ioc.c_len, 8));
    899 		printf("%s\n", hexstr(*(u_short *)&rs->sc_ioc.c_cmd, 4));
    900 		return(1);
    901 	}
    902 #endif
    903 	printf(" v%d u%d, R0x%x F0x%x A0x%x I0x%x\n",
    904 	       (sp->c_vu>>4)&0xF, sp->c_vu&0xF,
    905 	       sp->c_ref, sp->c_fef, sp->c_aef, sp->c_ief);
    906 	printf("P1-P10: ");
    907 	printf("%s", hexstr(*(u_int *)&sp->c_raw[0], 8));
    908 	printf("%s", hexstr(*(u_int *)&sp->c_raw[4], 8));
    909 	printf("%s\n", hexstr(*(u_short *)&sp->c_raw[8], 4));
    910 	return(1);
    911 }
    912 
    913 int
    914 rdioctl(dev, cmd, data, flag, p)
    915 	dev_t dev;
    916 	int cmd;
    917 	caddr_t data;
    918 	int flag;
    919 	struct proc *p;
    920 {
    921 	int unit = rdunit(dev);
    922 	register struct rd_softc *sc = &rd_softc[unit];
    923 	register struct disklabel *lp = &sc->sc_info.ri_label;
    924 	int error, flags;
    925 
    926 	switch (cmd) {
    927 	case DIOCGDINFO:
    928 		*(struct disklabel *)data = *lp;
    929 		return (0);
    930 
    931 	case DIOCGPART:
    932 		((struct partinfo *)data)->disklab = lp;
    933 		((struct partinfo *)data)->part =
    934 			&lp->d_partitions[rdpart(dev)];
    935 		return (0);
    936 
    937 	case DIOCWLABEL:
    938 		if ((flag & FWRITE) == 0)
    939 			return (EBADF);
    940 		if (*(int *)data)
    941 			sc->sc_flags |= RDF_WLABEL;
    942 		else
    943 			sc->sc_flags &= ~RDF_WLABEL;
    944 		return (0);
    945 
    946 	case DIOCSDINFO:
    947 		if ((flag & FWRITE) == 0)
    948 			return (EBADF);
    949 		return (setdisklabel(lp, (struct disklabel *)data,
    950 				     (sc->sc_flags & RDF_WLABEL) ? 0
    951 				     : sc->sc_info.ri_open,
    952 				     (struct cpu_disklabel *)0));
    953 
    954 	case DIOCWDINFO:
    955 		if ((flag & FWRITE) == 0)
    956 			return (EBADF);
    957 		error = setdisklabel(lp, (struct disklabel *)data,
    958 				     (sc->sc_flags & RDF_WLABEL) ? 0
    959 				     : sc->sc_info.ri_open,
    960 				     (struct cpu_disklabel *)0);
    961 		if (error)
    962 			return (error);
    963 		flags = sc->sc_flags;
    964 		sc->sc_flags = RDF_ALIVE | RDF_WLABEL;
    965 		error = writedisklabel(rdlabdev(dev), rdstrategy, lp,
    966 				       (struct cpu_disklabel *)0);
    967 		sc->sc_flags = flags;
    968 		return (error);
    969 	}
    970 	return(EINVAL);
    971 }
    972 
    973 int
    974 rdsize(dev)
    975 	dev_t dev;
    976 {
    977 	register int unit = rdunit(dev);
    978 	register struct rd_softc *rs = &rd_softc[unit];
    979 	int psize, didopen = 0;
    980 
    981 	if (unit >= NRD || (rs->sc_flags & RDF_ALIVE) == 0)
    982 		return(-1);
    983 
    984 	/*
    985 	 * We get called very early on (via swapconf)
    986 	 * without the device being open so we may need
    987 	 * to handle it here.
    988 	 */
    989 	if (rs->sc_info.ri_open == 0) {
    990 		if (rdopen(dev, FREAD|FWRITE, S_IFBLK, NULL))
    991 			return(-1);
    992 		didopen = 1;
    993 	}
    994 	psize = rs->sc_info.ri_label.d_partitions[rdpart(dev)].p_size;
    995 	if (didopen)
    996 		(void) rdclose(dev, FREAD|FWRITE, S_IFBLK, NULL);
    997 	return (psize);
    998 }
    999 
   1000 #ifdef DEBUG
   1001 rdprinterr(str, err, tab)
   1002 	char *str;
   1003 	short err;
   1004 	char *tab[];
   1005 {
   1006 	register int i;
   1007 	int printed;
   1008 
   1009 	if (err == 0)
   1010 		return;
   1011 	printf("    %s error field:", str, err);
   1012 	printed = 0;
   1013 	for (i = 0; i < 16; i++)
   1014 		if (err & (0x8000 >> i))
   1015 			printf("%s%s", printed++ ? " + " : " ", tab[i]);
   1016 	printf("\n");
   1017 }
   1018 #endif
   1019 
   1020 /*
   1021  * Non-interrupt driven, non-dma dump routine.
   1022  */
   1023 int
   1024 rddump(dev)
   1025 	dev_t dev;
   1026 {
   1027 	int part = rdpart(dev);
   1028 	int unit = rdunit(dev);
   1029 	register struct rd_softc *rs = &rd_softc[unit];
   1030 	register struct hp_device *hp = rs->sc_hd;
   1031 	register struct partition *pinfo;
   1032 	register daddr_t baddr;
   1033 	register int maddr, pages, i;
   1034 	char stat;
   1035 	extern int lowram, dumpsize;
   1036 #ifdef DEBUG
   1037 	extern int pmapdebug;
   1038 	pmapdebug = 0;
   1039 #endif
   1040 
   1041 	/* is drive ok? */
   1042 	if (unit >= NRD || (rs->sc_flags & RDF_ALIVE) == 0)
   1043 		return (ENXIO);
   1044 	pinfo = &rs->sc_info.ri_label.d_partitions[part];
   1045 	/* dump parameters in range? */
   1046 	if (dumplo < 0 || dumplo >= pinfo->p_size ||
   1047 	    pinfo->p_fstype != FS_SWAP)
   1048 		return (EINVAL);
   1049 	pages = dumpsize;
   1050 	if (dumplo + ctod(pages) > pinfo->p_size)
   1051 		pages = dtoc(pinfo->p_size - dumplo);
   1052 	maddr = lowram;
   1053 	baddr = dumplo + pinfo->p_offset;
   1054 	/* HPIB idle? */
   1055 	if (!hpibreq(&rs->sc_dq)) {
   1056 		hpibreset(hp->hp_ctlr);
   1057 		rdreset(rs, rs->sc_hd);
   1058 		printf("[ drive %d reset ] ", unit);
   1059 	}
   1060 	for (i = 0; i < pages; i++) {
   1061 #define NPGMB	(1024*1024/NBPG)
   1062 		/* print out how many Mbs we have dumped */
   1063 		if (i && (i % NPGMB) == 0)
   1064 			printf("%d ", i / NPGMB);
   1065 #undef NPBMG
   1066 		rs->sc_ioc.c_unit = C_SUNIT(rs->sc_punit);
   1067 		rs->sc_ioc.c_volume = C_SVOL(0);
   1068 		rs->sc_ioc.c_saddr = C_SADDR;
   1069 		rs->sc_ioc.c_hiaddr = 0;
   1070 		rs->sc_ioc.c_addr = RDBTOS(baddr);
   1071 		rs->sc_ioc.c_nop2 = C_NOP;
   1072 		rs->sc_ioc.c_slen = C_SLEN;
   1073 		rs->sc_ioc.c_len = NBPG;
   1074 		rs->sc_ioc.c_cmd = C_WRITE;
   1075 		hpibsend(hp->hp_ctlr, hp->hp_slave, C_CMD,
   1076 			 &rs->sc_ioc.c_unit, sizeof(rs->sc_ioc)-2);
   1077 		if (hpibswait(hp->hp_ctlr, hp->hp_slave))
   1078 			return (EIO);
   1079 		pmap_enter(kernel_pmap, (vm_offset_t)vmmap, maddr,
   1080 		    VM_PROT_READ, TRUE);
   1081 		hpibsend(hp->hp_ctlr, hp->hp_slave, C_EXEC, vmmap, NBPG);
   1082 		(void) hpibswait(hp->hp_ctlr, hp->hp_slave);
   1083 		hpibrecv(hp->hp_ctlr, hp->hp_slave, C_QSTAT, &stat, 1);
   1084 		if (stat)
   1085 			return (EIO);
   1086 		maddr += NBPG;
   1087 		baddr += ctod(1);
   1088 	}
   1089 	return (0);
   1090 }
   1091 #endif
   1092