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