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