Home | History | Annotate | Line # | Download | only in dev
rd.c revision 1.34
      1 /*	$NetBSD: rd.c,v 1.34 1997/07/22 15:20:20 kleink 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 #include "opt_useleds.h"
     68 
     69 #ifdef USELEDS
     70 #include <hp300/hp300/leds.h>
     71 #endif
     72 
     73 int	rderrthresh = RDRETRY-1;	/* when to start reporting errors */
     74 
     75 #ifdef DEBUG
     76 /* error message tables */
     77 char *err_reject[] = {
     78 	0, 0,
     79 	"channel parity error",		/* 0x2000 */
     80 	0, 0,
     81 	"illegal opcode",		/* 0x0400 */
     82 	"module addressing",		/* 0x0200 */
     83 	"address bounds",		/* 0x0100 */
     84 	"parameter bounds",		/* 0x0080 */
     85 	"illegal parameter",		/* 0x0040 */
     86 	"message sequence",		/* 0x0020 */
     87 	0,
     88 	"message length",		/* 0x0008 */
     89 	0, 0, 0
     90 };
     91 
     92 char *err_fault[] = {
     93 	0,
     94 	"cross unit",			/* 0x4000 */
     95 	0,
     96 	"controller fault",		/* 0x1000 */
     97 	0, 0,
     98 	"unit fault",			/* 0x0200 */
     99 	0,
    100 	"diagnostic result",		/* 0x0080 */
    101 	0,
    102 	"operator release request",	/* 0x0020 */
    103 	"diagnostic release request",	/* 0x0010 */
    104 	"internal maintenance release request",	/* 0x0008 */
    105 	0,
    106 	"power fail",			/* 0x0002 */
    107 	"retransmit"			/* 0x0001 */
    108 };
    109 
    110 char *err_access[] = {
    111 	"illegal parallel operation",	/* 0x8000 */
    112 	"uninitialized media",		/* 0x4000 */
    113 	"no spares available",		/* 0x2000 */
    114 	"not ready",			/* 0x1000 */
    115 	"write protect",		/* 0x0800 */
    116 	"no data found",		/* 0x0400 */
    117 	0, 0,
    118 	"unrecoverable data overflow",	/* 0x0080 */
    119 	"unrecoverable data",		/* 0x0040 */
    120 	0,
    121 	"end of file",			/* 0x0010 */
    122 	"end of volume",		/* 0x0008 */
    123 	0, 0, 0
    124 };
    125 
    126 char *err_info[] = {
    127 	"operator release request",	/* 0x8000 */
    128 	"diagnostic release request",	/* 0x4000 */
    129 	"internal maintenance release request",	/* 0x2000 */
    130 	"media wear",			/* 0x1000 */
    131 	"latency induced",		/* 0x0800 */
    132 	0, 0,
    133 	"auto sparing invoked",		/* 0x0100 */
    134 	0,
    135 	"recoverable data overflow",	/* 0x0040 */
    136 	"marginal data",		/* 0x0020 */
    137 	"recoverable data",		/* 0x0010 */
    138 	0,
    139 	"maintenance track overflow",	/* 0x0004 */
    140 	0, 0
    141 };
    142 
    143 int	rddebug = 0x80;
    144 #define RDB_FOLLOW	0x01
    145 #define RDB_STATUS	0x02
    146 #define RDB_IDENT	0x04
    147 #define RDB_IO		0x08
    148 #define RDB_ASYNC	0x10
    149 #define RDB_ERROR	0x80
    150 #endif
    151 
    152 /*
    153  * Misc. HW description, indexed by sc_type.
    154  * Nothing really critical here, could do without it.
    155  */
    156 struct rdidentinfo rdidentinfo[] = {
    157 	{ RD7946AID,	0,	"7945A",	NRD7945ABPT,
    158 	  NRD7945ATRK,	968,	 108416 },
    159 
    160 	{ RD9134DID,	1,	"9134D",	NRD9134DBPT,
    161 	  NRD9134DTRK,	303,	  29088 },
    162 
    163 	{ RD9134LID,	1,	"9122S",	NRD9122SBPT,
    164 	  NRD9122STRK,	77,	   1232 },
    165 
    166 	{ RD7912PID,	0,	"7912P",	NRD7912PBPT,
    167 	  NRD7912PTRK,	572,	 128128 },
    168 
    169 	{ RD7914PID,	0,	"7914P",	NRD7914PBPT,
    170 	  NRD7914PTRK,	1152,	 258048 },
    171 
    172 	{ RD7958AID,	0,	"7958A",	NRD7958ABPT,
    173 	  NRD7958ATRK,	1013,	 255276 },
    174 
    175 	{ RD7957AID,	0,	"7957A",	NRD7957ABPT,
    176 	  NRD7957ATRK,	1036,	 159544 },
    177 
    178 	{ RD7933HID,	0,	"7933H",	NRD7933HBPT,
    179 	  NRD7933HTRK,	1321,	 789958 },
    180 
    181 	{ RD9134LID,	1,	"9134L",	NRD9134LBPT,
    182 	  NRD9134LTRK,	973,	  77840 },
    183 
    184 	{ RD7936HID,	0,	"7936H",	NRD7936HBPT,
    185 	  NRD7936HTRK,	698,	 600978 },
    186 
    187 	{ RD7937HID,	0,	"7937H",	NRD7937HBPT,
    188 	  NRD7937HTRK,	698,	1116102 },
    189 
    190 	{ RD7914CTID,	0,	"7914CT",	NRD7914PBPT,
    191 	  NRD7914PTRK,	1152,	 258048 },
    192 
    193 	{ RD7946AID,	0,	"7946A",	NRD7945ABPT,
    194 	  NRD7945ATRK,	968,	 108416 },
    195 
    196 	{ RD9134LID,	1,	"9122D",	NRD9122SBPT,
    197 	  NRD9122STRK,	77,	   1232 },
    198 
    199 	{ RD7957BID,	0,	"7957B",	NRD7957BBPT,
    200 	  NRD7957BTRK,	1269,	 159894 },
    201 
    202 	{ RD7958BID,	0,	"7958B",	NRD7958BBPT,
    203 	  NRD7958BTRK,	786,	 297108 },
    204 
    205 	{ RD7959BID,	0,	"7959B",	NRD7959BBPT,
    206 	  NRD7959BTRK,	1572,	 594216 },
    207 
    208 	{ RD2200AID,	0,	"2200A",	NRD2200ABPT,
    209 	  NRD2200ATRK,	1449,	 654948 },
    210 
    211 	{ RD2203AID,	0,	"2203A",	NRD2203ABPT,
    212 	  NRD2203ATRK,	1449,	1309896 }
    213 };
    214 int numrdidentinfo = sizeof(rdidentinfo) / sizeof(rdidentinfo[0]);
    215 
    216 bdev_decl(rd);
    217 cdev_decl(rd);
    218 
    219 int	rdident __P((struct device *, struct rd_softc *,
    220 	    struct hpibbus_attach_args *));
    221 void	rdreset __P((struct rd_softc *));
    222 void	rdustart __P((struct rd_softc *));
    223 int	rdgetinfo __P((dev_t));
    224 void	rdrestart __P((void *));
    225 struct buf *rdfinish __P((struct rd_softc *, struct buf *));
    226 
    227 void	rdrestart __P((void *));
    228 void	rdustart __P((struct rd_softc *));
    229 struct buf *rdfinish __P((struct rd_softc *, struct buf *));
    230 void	rdstart __P((void *));
    231 void	rdgo __P((void *));
    232 void	rdintr __P((void *));
    233 int	rdstatus __P((struct rd_softc *));
    234 int	rderror __P((int));
    235 #ifdef DEBUG
    236 void	rdprinterr __P((char *, short, char **));
    237 #endif
    238 
    239 int	rdmatch __P((struct device *, struct cfdata *, void *));
    240 void	rdattach __P((struct device *, struct device *, void *));
    241 
    242 struct cfattach rd_ca = {
    243 	sizeof(struct rd_softc), rdmatch, rdattach
    244 };
    245 
    246 struct cfdriver rd_cd = {
    247 	NULL, "rd", DV_DISK
    248 };
    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 != HPIBBUSCF_PUNIT_DEFAULT &&
    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(%p): dev %x, bn %x, bcount %lx, %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 %p, %c\n", rs->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, %p, %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 %lx blk %d len %d ecnt %ld\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 	ledcontrol(0, 0, LED_DISK);
    830 #endif
    831 	hpibgo(ctlr, slave, C_EXEC, rs->sc_addr, rs->sc_resid, rw, rw != 0);
    832 }
    833 
    834 /* ARGSUSED */
    835 void
    836 rdintr(arg)
    837 	void *arg;
    838 {
    839 	struct rd_softc *rs = arg;
    840 	int unit = rs->sc_dev.dv_unit;
    841 	struct buf *bp = rs->sc_tab.b_actf;
    842 	u_char stat = 13;	/* in case hpibrecv fails */
    843 	int rv, restart, ctlr, slave;
    844 
    845 	ctlr = rs->sc_dev.dv_parent->dv_unit;
    846 	slave = rs->sc_slave;
    847 
    848 #ifdef DEBUG
    849 	if (rddebug & RDB_FOLLOW)
    850 		printf("rdintr(%d): bp %p, %c, flags %x\n", unit, bp,
    851 		       (bp->b_flags & B_READ) ? 'R' : 'W', rs->sc_flags);
    852 	if (bp == NULL) {
    853 		printf("%s: bp == NULL\n", rs->sc_dev.dv_xname);
    854 		return;
    855 	}
    856 #endif
    857 	disk_unbusy(&rs->sc_dkdev, (bp->b_bcount - bp->b_resid));
    858 
    859 	if (rs->sc_flags & RDF_SEEK) {
    860 		rs->sc_flags &= ~RDF_SEEK;
    861 		if (hpibustart(ctlr))
    862 			rdgo(rs);
    863 		return;
    864 	}
    865 	if ((rs->sc_flags & RDF_SWAIT) == 0) {
    866 #ifdef DEBUG
    867 		rs->sc_stats.rdpolltries++;
    868 #endif
    869 		if (hpibpptest(ctlr, slave) == 0) {
    870 #ifdef DEBUG
    871 			rs->sc_stats.rdpollwaits++;
    872 #endif
    873 
    874 			/* Instrumentation. */
    875 			disk_busy(&rs->sc_dkdev);
    876 			rs->sc_flags |= RDF_SWAIT;
    877 			hpibawait(ctlr);
    878 			return;
    879 		}
    880 	} else
    881 		rs->sc_flags &= ~RDF_SWAIT;
    882 	rv = hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
    883 	if (rv != 1 || stat) {
    884 #ifdef DEBUG
    885 		if (rddebug & RDB_ERROR)
    886 			printf("rdintr: recv failed or bad stat %d\n", stat);
    887 #endif
    888 		restart = rderror(unit);
    889 #ifdef DEBUG
    890 		rs->sc_stats.rdretries++;
    891 #endif
    892 		if (rs->sc_tab.b_errcnt++ < RDRETRY) {
    893 			if (restart)
    894 				rdstart(rs);
    895 			return;
    896 		}
    897 		bp->b_flags |= B_ERROR;
    898 		bp->b_error = EIO;
    899 	}
    900 	if (rdfinish(rs, bp))
    901 		rdustart(rs);
    902 }
    903 
    904 int
    905 rdstatus(rs)
    906 	struct rd_softc *rs;
    907 {
    908 	int c, s;
    909 	u_char stat;
    910 	int rv;
    911 
    912 	c = rs->sc_dev.dv_parent->dv_unit;
    913 	s = rs->sc_slave;
    914 	rs->sc_rsc.c_unit = C_SUNIT(rs->sc_punit);
    915 	rs->sc_rsc.c_sram = C_SRAM;
    916 	rs->sc_rsc.c_ram = C_RAM;
    917 	rs->sc_rsc.c_cmd = C_STATUS;
    918 	bzero((caddr_t)&rs->sc_stat, sizeof(rs->sc_stat));
    919 	rv = hpibsend(c, s, C_CMD, &rs->sc_rsc, sizeof(rs->sc_rsc));
    920 	if (rv != sizeof(rs->sc_rsc)) {
    921 #ifdef DEBUG
    922 		if (rddebug & RDB_STATUS)
    923 			printf("rdstatus: send C_CMD failed %d != %d\n",
    924 			       rv, sizeof(rs->sc_rsc));
    925 #endif
    926 		return(1);
    927 	}
    928 	rv = hpibrecv(c, s, C_EXEC, &rs->sc_stat, sizeof(rs->sc_stat));
    929 	if (rv != sizeof(rs->sc_stat)) {
    930 #ifdef DEBUG
    931 		if (rddebug & RDB_STATUS)
    932 			printf("rdstatus: send C_EXEC failed %d != %d\n",
    933 			       rv, sizeof(rs->sc_stat));
    934 #endif
    935 		return(1);
    936 	}
    937 	rv = hpibrecv(c, s, C_QSTAT, &stat, 1);
    938 	if (rv != 1 || stat) {
    939 #ifdef DEBUG
    940 		if (rddebug & RDB_STATUS)
    941 			printf("rdstatus: recv failed %d or bad stat %d\n",
    942 			       rv, stat);
    943 #endif
    944 		return(1);
    945 	}
    946 	return(0);
    947 }
    948 
    949 /*
    950  * Deal with errors.
    951  * Returns 1 if request should be restarted,
    952  * 0 if we should just quietly give up.
    953  */
    954 int
    955 rderror(unit)
    956 	int unit;
    957 {
    958 	struct rd_softc *rs = rd_cd.cd_devs[unit];
    959 	struct rd_stat *sp;
    960 	struct buf *bp;
    961 	daddr_t hwbn, pbn;
    962 	char *hexstr __P((int, int)); /* XXX */
    963 
    964 	if (rdstatus(rs)) {
    965 #ifdef DEBUG
    966 		printf("%s: couldn't get status\n", rs->sc_dev.dv_xname);
    967 #endif
    968 		rdreset(rs);
    969 		return(1);
    970 	}
    971 	sp = &rs->sc_stat;
    972 	if (sp->c_fef & FEF_REXMT)
    973 		return(1);
    974 	if (sp->c_fef & FEF_PF) {
    975 		rdreset(rs);
    976 		return(1);
    977 	}
    978 	/*
    979 	 * Unit requests release for internal maintenance.
    980 	 * We just delay awhile and try again later.  Use expontially
    981 	 * increasing backoff ala ethernet drivers since we don't really
    982 	 * know how long the maintenance will take.  With RDWAITC and
    983 	 * RDRETRY as defined, the range is 1 to 32 seconds.
    984 	 */
    985 	if (sp->c_fef & FEF_IMR) {
    986 		extern int hz;
    987 		int rdtimo = RDWAITC << rs->sc_tab.b_errcnt;
    988 #ifdef DEBUG
    989 		printf("%s: internal maintenance, %d second timeout\n",
    990 		       rs->sc_dev.dv_xname, rdtimo);
    991 		rs->sc_stats.rdtimeouts++;
    992 #endif
    993 		hpibfree(rs->sc_dev.dv_parent, &rs->sc_hq);
    994 		timeout(rdrestart, rs, rdtimo * hz);
    995 		return(0);
    996 	}
    997 	/*
    998 	 * Only report error if we have reached the error reporting
    999 	 * threshhold.  By default, this will only report after the
   1000 	 * retry limit has been exceeded.
   1001 	 */
   1002 	if (rs->sc_tab.b_errcnt < rderrthresh)
   1003 		return(1);
   1004 
   1005 	/*
   1006 	 * First conjure up the block number at which the error occured.
   1007 	 * Note that not all errors report a block number, in that case
   1008 	 * we just use b_blkno.
   1009  	 */
   1010 	bp = rs->sc_tab.b_actf;
   1011 	pbn = rs->sc_dkdev.dk_label->d_partitions[rdpart(bp->b_dev)].p_offset;
   1012 	if ((sp->c_fef & FEF_CU) || (sp->c_fef & FEF_DR) ||
   1013 	    (sp->c_ief & IEF_RRMASK)) {
   1014 		hwbn = RDBTOS(pbn + bp->b_blkno);
   1015 		pbn = bp->b_blkno;
   1016 	} else {
   1017 		hwbn = sp->c_blk;
   1018 		pbn = RDSTOB(hwbn) - pbn;
   1019 	}
   1020 	/*
   1021 	 * Now output a generic message suitable for badsect.
   1022 	 * Note that we don't use harderr cuz it just prints
   1023 	 * out b_blkno which is just the beginning block number
   1024 	 * of the transfer, not necessary where the error occured.
   1025 	 */
   1026 	printf("%s%c: hard error sn%d\n", rs->sc_dev.dv_xname,
   1027 	    'a'+rdpart(bp->b_dev), pbn);
   1028 	/*
   1029 	 * Now report the status as returned by the hardware with
   1030 	 * attempt at interpretation (unless debugging).
   1031 	 */
   1032 	printf("%s %s error:", rs->sc_dev.dv_xname,
   1033 	    (bp->b_flags & B_READ) ? "read" : "write");
   1034 #ifdef DEBUG
   1035 	if (rddebug & RDB_ERROR) {
   1036 		/* status info */
   1037 		printf("\n    volume: %d, unit: %d\n",
   1038 		       (sp->c_vu>>4)&0xF, sp->c_vu&0xF);
   1039 		rdprinterr("reject", sp->c_ref, err_reject);
   1040 		rdprinterr("fault", sp->c_fef, err_fault);
   1041 		rdprinterr("access", sp->c_aef, err_access);
   1042 		rdprinterr("info", sp->c_ief, err_info);
   1043 		printf("    block: %d, P1-P10: ", hwbn);
   1044 		printf("0x%x", *(u_int *)&sp->c_raw[0]);
   1045 		printf("0x%x", *(u_int *)&sp->c_raw[4]);
   1046 		printf("0x%x\n", *(u_short *)&sp->c_raw[8]);
   1047 		/* command */
   1048 		printf("    ioc: ");
   1049 		printf("0x%x", *(u_int *)&rs->sc_ioc.c_pad);
   1050 		printf("0x%x", *(u_short *)&rs->sc_ioc.c_hiaddr);
   1051 		printf("0x%x", *(u_int *)&rs->sc_ioc.c_addr);
   1052 		printf("0x%x", *(u_short *)&rs->sc_ioc.c_nop2);
   1053 		printf("0x%x", *(u_int *)&rs->sc_ioc.c_len);
   1054 		printf("0x%x\n", *(u_short *)&rs->sc_ioc.c_cmd);
   1055 		return(1);
   1056 	}
   1057 #endif
   1058 	printf(" v%d u%d, R0x%x F0x%x A0x%x I0x%x\n",
   1059 	       (sp->c_vu>>4)&0xF, sp->c_vu&0xF,
   1060 	       sp->c_ref, sp->c_fef, sp->c_aef, sp->c_ief);
   1061 	printf("P1-P10: ");
   1062 	printf("0x%x", *(u_int *)&sp->c_raw[0]);
   1063 	printf("0x%x", *(u_int *)&sp->c_raw[4]);
   1064 	printf("0x%x\n", *(u_short *)&sp->c_raw[8]);
   1065 	return(1);
   1066 }
   1067 
   1068 int
   1069 rdread(dev, uio, flags)
   1070 	dev_t dev;
   1071 	struct uio *uio;
   1072 	int flags;
   1073 {
   1074 
   1075 	return (physio(rdstrategy, NULL, dev, B_READ, minphys, uio));
   1076 }
   1077 
   1078 int
   1079 rdwrite(dev, uio, flags)
   1080 	dev_t dev;
   1081 	struct uio *uio;
   1082 	int flags;
   1083 {
   1084 
   1085 	return (physio(rdstrategy, NULL, dev, B_WRITE, minphys, uio));
   1086 }
   1087 
   1088 int
   1089 rdioctl(dev, cmd, data, flag, p)
   1090 	dev_t dev;
   1091 	u_long cmd;
   1092 	caddr_t data;
   1093 	int flag;
   1094 	struct proc *p;
   1095 {
   1096 	int unit = rdunit(dev);
   1097 	struct rd_softc *sc = rd_cd.cd_devs[unit];
   1098 	struct disklabel *lp = sc->sc_dkdev.dk_label;
   1099 	int error, flags;
   1100 
   1101 	switch (cmd) {
   1102 	case DIOCGDINFO:
   1103 		*(struct disklabel *)data = *lp;
   1104 		return (0);
   1105 
   1106 	case DIOCGPART:
   1107 		((struct partinfo *)data)->disklab = lp;
   1108 		((struct partinfo *)data)->part =
   1109 			&lp->d_partitions[rdpart(dev)];
   1110 		return (0);
   1111 
   1112 	case DIOCWLABEL:
   1113 		if ((flag & FWRITE) == 0)
   1114 			return (EBADF);
   1115 		if (*(int *)data)
   1116 			sc->sc_flags |= RDF_WLABEL;
   1117 		else
   1118 			sc->sc_flags &= ~RDF_WLABEL;
   1119 		return (0);
   1120 
   1121 	case DIOCSDINFO:
   1122 		if ((flag & FWRITE) == 0)
   1123 			return (EBADF);
   1124 		return (setdisklabel(lp, (struct disklabel *)data,
   1125 				     (sc->sc_flags & RDF_WLABEL) ? 0
   1126 				     : sc->sc_dkdev.dk_openmask,
   1127 				     (struct cpu_disklabel *)0));
   1128 
   1129 	case DIOCWDINFO:
   1130 		if ((flag & FWRITE) == 0)
   1131 			return (EBADF);
   1132 		error = setdisklabel(lp, (struct disklabel *)data,
   1133 				     (sc->sc_flags & RDF_WLABEL) ? 0
   1134 				     : sc->sc_dkdev.dk_openmask,
   1135 				     (struct cpu_disklabel *)0);
   1136 		if (error)
   1137 			return (error);
   1138 		flags = sc->sc_flags;
   1139 		sc->sc_flags = RDF_ALIVE | RDF_WLABEL;
   1140 		error = writedisklabel(rdlabdev(dev), rdstrategy, lp,
   1141 				       (struct cpu_disklabel *)0);
   1142 		sc->sc_flags = flags;
   1143 		return (error);
   1144 	}
   1145 	return(EINVAL);
   1146 }
   1147 
   1148 int
   1149 rdsize(dev)
   1150 	dev_t dev;
   1151 {
   1152 	int unit = rdunit(dev);
   1153 	struct rd_softc *rs;
   1154 	int psize, didopen = 0;
   1155 
   1156 	if (unit >= rd_cd.cd_ndevs ||
   1157 	    (rs = rd_cd.cd_devs[unit]) == NULL ||
   1158 	    (rs->sc_flags & RDF_ALIVE) == 0)
   1159 		return (-1);
   1160 
   1161 	/*
   1162 	 * We get called very early on (via swapconf)
   1163 	 * without the device being open so we may need
   1164 	 * to handle it here.
   1165 	 */
   1166 	if (rs->sc_dkdev.dk_openmask == 0) {
   1167 		if (rdopen(dev, FREAD|FWRITE, S_IFBLK, NULL))
   1168 			return(-1);
   1169 		didopen = 1;
   1170 	}
   1171 	psize = rs->sc_dkdev.dk_label->d_partitions[rdpart(dev)].p_size *
   1172 	    (rs->sc_dkdev.dk_label->d_secsize / DEV_BSIZE);
   1173 	if (didopen)
   1174 		(void) rdclose(dev, FREAD|FWRITE, S_IFBLK, NULL);
   1175 	return (psize);
   1176 }
   1177 
   1178 #ifdef DEBUG
   1179 void
   1180 rdprinterr(str, err, tab)
   1181 	char *str;
   1182 	short err;
   1183 	char **tab;
   1184 {
   1185 	int i;
   1186 	int printed;
   1187 
   1188 	if (err == 0)
   1189 		return;
   1190 	printf("    %s error %d field:", str, err);
   1191 	printed = 0;
   1192 	for (i = 0; i < 16; i++)
   1193 		if (err & (0x8000 >> i))
   1194 			printf("%s%s", printed++ ? " + " : " ", tab[i]);
   1195 	printf("\n");
   1196 }
   1197 #endif
   1198 
   1199 static int rddoingadump;	/* simple mutex */
   1200 
   1201 /*
   1202  * Non-interrupt driven, non-dma dump routine.
   1203  */
   1204 int
   1205 rddump(dev, blkno, va, size)
   1206 	dev_t dev;
   1207 	daddr_t blkno;
   1208 	caddr_t va;
   1209 	size_t size;
   1210 {
   1211 	int sectorsize;		/* size of a disk sector */
   1212 	int nsects;		/* number of sectors in partition */
   1213 	int sectoff;		/* sector offset of partition */
   1214 	int totwrt;		/* total number of sectors left to write */
   1215 	int nwrt;		/* current number of sectors to write */
   1216 	int unit, part;
   1217 	int ctlr, slave;
   1218 	struct rd_softc *rs;
   1219 	struct disklabel *lp;
   1220 	char stat;
   1221 
   1222 	/* Check for recursive dump; if so, punt. */
   1223 	if (rddoingadump)
   1224 		return (EFAULT);
   1225 	rddoingadump = 1;
   1226 
   1227 	/* Decompose unit and partition. */
   1228 	unit = rdunit(dev);
   1229 	part = rdpart(dev);
   1230 
   1231 	/* Make sure dump device is ok. */
   1232 	if (unit >= rd_cd.cd_ndevs ||
   1233 	    (rs = rd_cd.cd_devs[unit]) == NULL ||
   1234 	    (rs->sc_flags & RDF_ALIVE) == 0)
   1235 		return (ENXIO);
   1236 
   1237 	ctlr = rs->sc_dev.dv_parent->dv_unit;
   1238 	slave = rs->sc_slave;
   1239 
   1240 	/*
   1241 	 * Convert to disk sectors.  Request must be a multiple of size.
   1242 	 */
   1243 	lp = rs->sc_dkdev.dk_label;
   1244 	sectorsize = lp->d_secsize;
   1245 	if ((size % sectorsize) != 0)
   1246 		return (EFAULT);
   1247 	totwrt = size / sectorsize;
   1248 	blkno = dbtob(blkno) / sectorsize;	/* blkno in DEV_BSIZE units */
   1249 
   1250 	nsects = lp->d_partitions[part].p_size;
   1251 	sectoff = lp->d_partitions[part].p_offset;
   1252 
   1253 	/* Check transfer bounds against partition size. */
   1254 	if ((blkno < 0) || (blkno + totwrt) > nsects)
   1255 		return (EINVAL);
   1256 
   1257 	/* Offset block number to start of partition. */
   1258 	blkno += sectoff;
   1259 
   1260 	while (totwrt > 0) {
   1261 		nwrt = totwrt;		/* XXX */
   1262 #ifndef RD_DUMP_NOT_TRUSTED
   1263 		/*
   1264 		 * Fill out and send HPIB command.
   1265 		 */
   1266 		rs->sc_ioc.c_unit = C_SUNIT(rs->sc_punit);
   1267 		rs->sc_ioc.c_volume = C_SVOL(0);
   1268 		rs->sc_ioc.c_saddr = C_SADDR;
   1269 		rs->sc_ioc.c_hiaddr = 0;
   1270 		rs->sc_ioc.c_addr = RDBTOS(blkno);
   1271 		rs->sc_ioc.c_nop2 = C_NOP;
   1272 		rs->sc_ioc.c_slen = C_SLEN;
   1273 		rs->sc_ioc.c_len = nwrt * sectorsize;
   1274 		rs->sc_ioc.c_cmd = C_WRITE;
   1275 		hpibsend(ctlr, slave, C_CMD, &rs->sc_ioc.c_unit,
   1276 		    sizeof(rs->sc_ioc)-2);
   1277 		if (hpibswait(ctlr, slave))
   1278 			return (EIO);
   1279 
   1280 		/*
   1281 		 * Send the data.
   1282 		 */
   1283 		hpibsend(ctlr, slave, C_EXEC, va, nwrt * sectorsize);
   1284 		(void) hpibswait(ctlr, slave);
   1285 		hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
   1286 		if (stat)
   1287 			return (EIO);
   1288 #else /* RD_DUMP_NOT_TRUSTED */
   1289 		/* Let's just talk about this first... */
   1290 		printf("%s: dump addr %p, blk %d\n", sc->sc_dev.dv_xname,
   1291 		    va, blkno);
   1292 		delay(500 * 1000);	/* half a second */
   1293 #endif /* RD_DUMP_NOT_TRUSTED */
   1294 
   1295 		/* update block count */
   1296 		totwrt -= nwrt;
   1297 		blkno += nwrt;
   1298 		va += sectorsize * nwrt;
   1299 	}
   1300 	rddoingadump = 0;
   1301 	return (0);
   1302 }
   1303