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