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