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