Home | History | Annotate | Line # | Download | only in dev
rd.c revision 1.105
      1 /*	$NetBSD: rd.c,v 1.105 2021/07/05 14:03:46 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.105 2021/07/05 14:03:46 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 = HDB_ERROR | HDB_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 
    324 	sc->sc_dev = self;
    325 	bufq_alloc(&sc->sc_tab, "disksort", BUFQ_SORT_RAWBLOCK);
    326 
    327 	if (rdident(parent, sc, ha) == 0) {
    328 		aprint_error(": didn't respond to describe command!\n");
    329 		return;
    330 	}
    331 
    332 	/*
    333 	 * Initialize and attach the disk structure.
    334 	 */
    335 	memset(&sc->sc_dkdev, 0, sizeof(sc->sc_dkdev));
    336 	disk_init(&sc->sc_dkdev, device_xname(sc->sc_dev), NULL);
    337 	disk_attach(&sc->sc_dkdev);
    338 
    339 	sc->sc_slave = ha->ha_slave;
    340 	sc->sc_punit = ha->ha_punit;
    341 
    342 	callout_init(&sc->sc_restart_ch, 0);
    343 
    344 	/* Initialize the hpib job queue entry */
    345 	sc->sc_hq.hq_softc = sc;
    346 	sc->sc_hq.hq_slave = sc->sc_slave;
    347 	sc->sc_hq.hq_start = rdstart;
    348 	sc->sc_hq.hq_go = rdgo;
    349 	sc->sc_hq.hq_intr = rdintr;
    350 
    351 	sc->sc_flags = RDF_ALIVE;
    352 #ifdef DEBUG
    353 	/* always report errors */
    354 	if (rddebug & RDB_ERROR)
    355 		rderrthresh = 0;
    356 #endif
    357 	/*
    358 	 * attach the device into the random source list
    359 	 */
    360 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
    361 	    RND_TYPE_DISK, RND_FLAG_DEFAULT);
    362 }
    363 
    364 static int
    365 rdident(device_t parent, struct rd_softc *sc, struct hpibbus_attach_args *ha)
    366 {
    367 	struct rd_describe *desc = sc != NULL ? &sc->sc_rddesc : NULL;
    368 	u_char stat, cmd[3];
    369 	char name[7], pbuf[9];
    370 	int i, id, n, ctlr, slave;
    371 
    372 	ctlr = device_unit(parent);
    373 	slave = ha->ha_slave;
    374 
    375 	/* Verify that we have a CS80 device. */
    376 	if ((ha->ha_id & 0x200) == 0)
    377 		return 0;
    378 
    379 	/* Is it one of the disks we support? */
    380 	for (id = 0; id < numrdidentinfo; id++)
    381 		if (ha->ha_id == rdidentinfo[id].ri_hwid &&
    382 		    ha->ha_punit <= rdidentinfo[id].ri_maxunum)
    383 			break;
    384 	if (id == numrdidentinfo)
    385 		return 0;
    386 
    387 	/*
    388 	 * If we're just probing for the device, that's all the
    389 	 * work we need to do.
    390 	 */
    391 	if (sc == NULL)
    392 		return 1;
    393 
    394 	/*
    395 	 * Reset device and collect description
    396 	 */
    397 	rdreset(sc);
    398 	cmd[0] = C_SUNIT(ha->ha_punit);
    399 	cmd[1] = C_SVOL(0);
    400 	cmd[2] = C_DESC;
    401 	hpibsend(ctlr, slave, C_CMD, cmd, sizeof(cmd));
    402 	hpibrecv(ctlr, slave, C_EXEC, desc, 37);
    403 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
    404 	memset(name, 0, sizeof(name));
    405 	if (stat == 0) {
    406 		n = desc->d_name;
    407 		for (i = 5; i >= 0; i--) {
    408 			name[i] = (n & 0xf) + '0';
    409 			n >>= 4;
    410 		}
    411 	}
    412 
    413 #ifdef DEBUG
    414 	if (rddebug & RDB_IDENT) {
    415 		aprint_normal("\n");
    416 		aprint_normal_dev(sc->sc_dev, "id: 0x%04x, name: %x ('%s')\n",
    417 		    ha->ha_id, desc->d_name, name);
    418 		aprint_normal("  iuw %x, maxxfr %d, ctype %d\n",
    419 		    desc->d_iuw, desc->d_cmaxxfr, desc->d_ctype);
    420 		aprint_normal("  utype %d, bps %d, blkbuf %d, burst %d,"
    421 		    " blktime %d\n",
    422 		    desc->d_utype, desc->d_sectsize,
    423 		    desc->d_blkbuf, desc->d_burstsize, desc->d_blocktime);
    424 		aprint_normal("  avxfr %d, ort %d, atp %d, maxint %d, fv %x"
    425 		    ", rv %x\n",
    426 		    desc->d_uavexfr, desc->d_retry, desc->d_access,
    427 		    desc->d_maxint, desc->d_fvbyte, desc->d_rvbyte);
    428 		aprint_normal("  maxcyl/head/sect %d/%d/%d, maxvsect %d,"
    429 		    " inter %d\n",
    430 		    desc->d_maxcyl, desc->d_maxhead, desc->d_maxsect,
    431 		    desc->d_maxvsectl, desc->d_interleave);
    432 		aprint_normal("%s", device_xname(sc->sc_dev));
    433 	}
    434 #endif
    435 
    436 	/*
    437 	 * Take care of a couple of anomolies:
    438 	 * 1. 7945A and 7946A both return same HW id
    439 	 * 2. 9122S and 9134D both return same HW id
    440 	 * 3. 9122D and 9134L both return same HW id
    441 	 */
    442 	switch (ha->ha_id) {
    443 	case RD7946AID:
    444 		if (memcmp(name, "079450", 6) == 0)
    445 			id = RD7945A;
    446 		else
    447 			id = RD7946A;
    448 		break;
    449 
    450 	case RD9134LID:
    451 		if (memcmp(name, "091340", 6) == 0)
    452 			id = RD9134L;
    453 		else
    454 			id = RD9122D;
    455 		break;
    456 
    457 	case RD9134DID:
    458 		if (memcmp(name, "091220", 6) == 0)
    459 			id = RD9122S;
    460 		else
    461 			id = RD9134D;
    462 		break;
    463 	}
    464 
    465 	sc->sc_type = id;
    466 
    467 	/*
    468 	 * XXX We use DEV_BSIZE instead of the sector size value pulled
    469 	 * XXX off the driver because all of this code assumes 512 byte
    470 	 * XXX blocks.  ICK!
    471 	 */
    472 	aprint_normal(": %s\n", rdidentinfo[id].ri_desc);
    473 	format_bytes(pbuf, sizeof(pbuf),
    474 	    rdidentinfo[id].ri_nblocks * DEV_BSIZE);
    475 	aprint_normal_dev(sc->sc_dev, "%s, %d cyl, %d head, %d sec,"
    476 	    " %d bytes/block x %u blocks\n",
    477 	    pbuf, rdidentinfo[id].ri_ncyl, rdidentinfo[id].ri_ntpc,
    478 	    rdidentinfo[id].ri_nbpt,
    479 	    DEV_BSIZE, rdidentinfo[id].ri_nblocks);
    480 
    481 	return 1;
    482 }
    483 
    484 static void
    485 rdreset(struct rd_softc *sc)
    486 {
    487 	int ctlr = device_unit(device_parent(sc->sc_dev));
    488 	int slave = sc->sc_slave;
    489 	u_char stat;
    490 
    491 	sc->sc_clear.c_unit = C_SUNIT(sc->sc_punit);
    492 	sc->sc_clear.c_cmd = C_CLEAR;
    493 	hpibsend(ctlr, slave, C_TCMD, &sc->sc_clear, sizeof(sc->sc_clear));
    494 	hpibswait(ctlr, slave);
    495 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
    496 
    497 	sc->sc_src.c_unit = C_SUNIT(RDCTLR);
    498 	sc->sc_src.c_nop = C_NOP;
    499 	sc->sc_src.c_cmd = C_SREL;
    500 	sc->sc_src.c_param = C_REL;
    501 	hpibsend(ctlr, slave, C_CMD, &sc->sc_src, sizeof(sc->sc_src));
    502 	hpibswait(ctlr, slave);
    503 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
    504 
    505 	sc->sc_ssmc.c_unit = C_SUNIT(sc->sc_punit);
    506 	sc->sc_ssmc.c_cmd = C_SSM;
    507 	sc->sc_ssmc.c_refm = REF_MASK;
    508 	sc->sc_ssmc.c_fefm = FEF_MASK;
    509 	sc->sc_ssmc.c_aefm = AEF_MASK;
    510 	sc->sc_ssmc.c_iefm = IEF_MASK;
    511 	hpibsend(ctlr, slave, C_CMD, &sc->sc_ssmc, sizeof(sc->sc_ssmc));
    512 	hpibswait(ctlr, slave);
    513 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
    514 #ifdef DEBUG
    515 	sc->sc_stats.rdresets++;
    516 #endif
    517 }
    518 
    519 /*
    520  * Read or construct a disklabel
    521  */
    522 static int
    523 rdgetinfo(dev_t dev)
    524 {
    525 	struct rd_softc *sc = device_lookup_private(&rd_cd, rdunit(dev));
    526 	struct disklabel *lp = sc->sc_dkdev.dk_label;
    527 	struct partition *pi;
    528 	const char *msg;
    529 
    530 	/*
    531 	 * Set some default values to use while reading the label
    532 	 * or to use if there isn't a label.
    533 	 */
    534 	memset((void *)lp, 0, sizeof *lp);
    535 	rdgetdefaultlabel(sc, lp);
    536 
    537 	/*
    538 	 * Now try to read the disklabel
    539 	 */
    540 	msg = readdisklabel(rdlabdev(dev), rdstrategy, lp, NULL);
    541 	if (msg == NULL)
    542 		return 0;
    543 
    544 	pi = lp->d_partitions;
    545 	printf("%s: WARNING: %s\n", device_xname(sc->sc_dev), msg);
    546 
    547 	pi[2].p_size = rdidentinfo[sc->sc_type].ri_nblocks;
    548 	/* XXX reset other info since readdisklabel screws with it */
    549 	lp->d_npartitions = 3;
    550 	pi[0].p_size = 0;
    551 
    552 	return 0;
    553 }
    554 
    555 static int
    556 rdopen(dev_t dev, int flags, int mode, struct lwp *l)
    557 {
    558 	struct rd_softc *sc;
    559 	int error, mask, part;
    560 
    561 	sc = device_lookup_private(&rd_cd, rdunit(dev));
    562 	if (sc == NULL)
    563 		return ENXIO;
    564 
    565 	if ((sc->sc_flags & RDF_ALIVE) == 0)
    566 		return ENXIO;
    567 
    568 	/*
    569 	 * Wait for any pending opens/closes to complete
    570 	 */
    571 	while (sc->sc_flags & (RDF_OPENING|RDF_CLOSING))
    572 		(void) tsleep(sc, PRIBIO, "rdopen", 0);
    573 
    574 	/*
    575 	 * On first open, get label and partition info.
    576 	 * We may block reading the label, so be careful
    577 	 * to stop any other opens.
    578 	 */
    579 	if (sc->sc_dkdev.dk_openmask == 0) {
    580 		sc->sc_flags |= RDF_OPENING;
    581 		error = rdgetinfo(dev);
    582 		sc->sc_flags &= ~RDF_OPENING;
    583 		wakeup((void *)sc);
    584 		if (error)
    585 			return error;
    586 	}
    587 
    588 	part = rdpart(dev);
    589 	mask = 1 << part;
    590 
    591 	/* Check that the partition exists. */
    592 	if (part != RAW_PART &&
    593 	    (part > sc->sc_dkdev.dk_label->d_npartitions ||
    594 	     sc->sc_dkdev.dk_label->d_partitions[part].p_fstype == FS_UNUSED))
    595 		return ENXIO;
    596 
    597 	/* Ensure only one open at a time. */
    598 	switch (mode) {
    599 	case S_IFCHR:
    600 		sc->sc_dkdev.dk_copenmask |= mask;
    601 		break;
    602 	case S_IFBLK:
    603 		sc->sc_dkdev.dk_bopenmask |= mask;
    604 		break;
    605 	}
    606 	sc->sc_dkdev.dk_openmask =
    607 	    sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
    608 
    609 	return 0;
    610 }
    611 
    612 static int
    613 rdclose(dev_t dev, int flag, int mode, struct lwp *l)
    614 {
    615 	struct rd_softc *sc = device_lookup_private(&rd_cd, rdunit(dev));
    616 	struct disk *dk = &sc->sc_dkdev;
    617 	int mask, s;
    618 
    619 	mask = 1 << rdpart(dev);
    620 	if (mode == S_IFCHR)
    621 		dk->dk_copenmask &= ~mask;
    622 	else
    623 		dk->dk_bopenmask &= ~mask;
    624 	dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
    625 	/*
    626 	 * On last close, we wait for all activity to cease since
    627 	 * the label/partition info will become invalid.  Since we
    628 	 * might sleep, we must block any opens while we are here.
    629 	 * Note we don't have to about other closes since we know
    630 	 * we are the last one.
    631 	 */
    632 	if (dk->dk_openmask == 0) {
    633 		sc->sc_flags |= RDF_CLOSING;
    634 		s = splbio();
    635 		while (sc->sc_active) {
    636 			sc->sc_flags |= RDF_WANTED;
    637 			(void) tsleep(&sc->sc_tab, PRIBIO, "rdclose", 0);
    638 		}
    639 		splx(s);
    640 		sc->sc_flags &= ~(RDF_CLOSING|RDF_WLABEL);
    641 		wakeup((void *)sc);
    642 	}
    643 	return 0;
    644 }
    645 
    646 static void
    647 rdstrategy(struct buf *bp)
    648 {
    649 	struct rd_softc *sc = device_lookup_private(&rd_cd, rdunit(bp->b_dev));
    650 	struct partition *pinfo;
    651 	daddr_t bn;
    652 	int sz, s;
    653 	int offset;
    654 
    655 #ifdef DEBUG
    656 	if (rddebug & RDB_FOLLOW)
    657 		printf("rdstrategy(%p): dev %"PRIx64", bn %llx, bcount %x, %c\n",
    658 		       bp, bp->b_dev, bp->b_blkno, bp->b_bcount,
    659 		       (bp->b_flags & B_READ) ? 'R' : 'W');
    660 #endif
    661 	bn = bp->b_blkno;
    662 	sz = howmany(bp->b_bcount, DEV_BSIZE);
    663 	pinfo = &sc->sc_dkdev.dk_label->d_partitions[rdpart(bp->b_dev)];
    664 
    665 	/* Don't perform partition translation on RAW_PART. */
    666 	offset = (rdpart(bp->b_dev) == RAW_PART) ? 0 : pinfo->p_offset;
    667 
    668 	if (rdpart(bp->b_dev) != RAW_PART) {
    669 		/*
    670 		 * XXX This block of code belongs in
    671 		 * XXX bounds_check_with_label()
    672 		 */
    673 
    674 		if (bn < 0 || bn + sz > pinfo->p_size) {
    675 			sz = pinfo->p_size - bn;
    676 			if (sz == 0) {
    677 				bp->b_resid = bp->b_bcount;
    678 				goto done;
    679 			}
    680 			if (sz < 0) {
    681 				bp->b_error = EINVAL;
    682 				goto done;
    683 			}
    684 			bp->b_bcount = dbtob(sz);
    685 		}
    686 		/*
    687 		 * Check for write to write protected label
    688 		 */
    689 		if (bn + offset <= LABELSECTOR &&
    690 #if LABELSECTOR != 0
    691 		    bn + offset + sz > LABELSECTOR &&
    692 #endif
    693 		    !(bp->b_flags & B_READ) && !(sc->sc_flags & RDF_WLABEL)) {
    694 			bp->b_error = EROFS;
    695 			goto done;
    696 		}
    697 	}
    698 	bp->b_rawblkno = bn + offset;
    699 	s = splbio();
    700 	bufq_put(sc->sc_tab, bp);
    701 	if (sc->sc_active == 0) {
    702 		sc->sc_active = 1;
    703 		rdustart(sc);
    704 	}
    705 	splx(s);
    706 	return;
    707 done:
    708 	biodone(bp);
    709 }
    710 
    711 /*
    712  * Called from timeout() when handling maintenance releases
    713  */
    714 static void
    715 rdrestart(void *arg)
    716 {
    717 	int s = splbio();
    718 	rdustart((struct rd_softc *)arg);
    719 	splx(s);
    720 }
    721 
    722 static void
    723 rdustart(struct rd_softc *sc)
    724 {
    725 	struct buf *bp;
    726 
    727 	bp = bufq_peek(sc->sc_tab);
    728 	sc->sc_addr = bp->b_data;
    729 	sc->sc_resid = bp->b_bcount;
    730 	if (hpibreq(device_parent(sc->sc_dev), &sc->sc_hq))
    731 		rdstart(sc);
    732 }
    733 
    734 static struct buf *
    735 rdfinish(struct rd_softc *sc, struct buf *bp)
    736 {
    737 
    738 	sc->sc_errcnt = 0;
    739 	(void)bufq_get(sc->sc_tab);
    740 	bp->b_resid = 0;
    741 	biodone(bp);
    742 	hpibfree(device_parent(sc->sc_dev), &sc->sc_hq);
    743 	if ((bp = bufq_peek(sc->sc_tab)) != NULL)
    744 		return bp;
    745 	sc->sc_active = 0;
    746 	if (sc->sc_flags & RDF_WANTED) {
    747 		sc->sc_flags &= ~RDF_WANTED;
    748 		wakeup((void *)&sc->sc_tab);
    749 	}
    750 	return NULL;
    751 }
    752 
    753 static void
    754 rdstart(void *arg)
    755 {
    756 	struct rd_softc *sc = arg;
    757 	struct buf *bp = bufq_peek(sc->sc_tab);
    758 	int ctlr, slave;
    759 
    760 	ctlr = device_unit(device_parent(sc->sc_dev));
    761 	slave = sc->sc_slave;
    762 
    763 again:
    764 #ifdef DEBUG
    765 	if (rddebug & RDB_FOLLOW)
    766 		printf("rdstart(%s): bp %p, %c\n", device_xname(sc->sc_dev), bp,
    767 		    (bp->b_flags & B_READ) ? 'R' : 'W');
    768 #endif
    769 	sc->sc_flags |= RDF_SEEK;
    770 	sc->sc_ioc.c_unit = C_SUNIT(sc->sc_punit);
    771 	sc->sc_ioc.c_volume = C_SVOL(0);
    772 	sc->sc_ioc.c_saddr = C_SADDR;
    773 	sc->sc_ioc.c_hiaddr = 0;
    774 	sc->sc_ioc.c_addr = RDBTOS(bp->b_rawblkno);
    775 	sc->sc_ioc.c_nop2 = C_NOP;
    776 	sc->sc_ioc.c_slen = C_SLEN;
    777 	sc->sc_ioc.c_len = sc->sc_resid;
    778 	sc->sc_ioc.c_cmd = bp->b_flags & B_READ ? C_READ : C_WRITE;
    779 #ifdef DEBUG
    780 	if (rddebug & RDB_IO)
    781 		printf("rdstart: hpibsend(%x, %x, %x, %p, %x)\n",
    782 		       ctlr, slave, C_CMD,
    783 		       &sc->sc_ioc.c_unit, sizeof(sc->sc_ioc) - 2);
    784 #endif
    785 	if (hpibsend(ctlr, slave, C_CMD, &sc->sc_ioc.c_unit,
    786 		     sizeof(sc->sc_ioc) - 2) == sizeof(sc->sc_ioc) - 2) {
    787 
    788 		/* Instrumentation. */
    789 		disk_busy(&sc->sc_dkdev);
    790 		iostat_seek(sc->sc_dkdev.dk_stats);
    791 
    792 #ifdef DEBUG
    793 		if (rddebug & RDB_IO)
    794 			printf("rdstart: hpibawait(%x)\n", ctlr);
    795 #endif
    796 		hpibawait(ctlr);
    797 		return;
    798 	}
    799 	/*
    800 	 * Experience has shown that the hpibwait in this hpibsend will
    801 	 * occasionally timeout.  It appears to occur mostly on old 7914
    802 	 * drives with full maintenance tracks.  We should probably
    803 	 * integrate this with the backoff code in rderror.
    804 	 */
    805 #ifdef DEBUG
    806 	if (rddebug & RDB_ERROR)
    807 		printf("%s: rdstart: cmd %x adr %lx blk %lld len %d ecnt %d\n",
    808 		    device_xname(sc->sc_dev),
    809 		    sc->sc_ioc.c_cmd, sc->sc_ioc.c_addr,
    810 		    bp->b_blkno, sc->sc_resid, sc->sc_errcnt);
    811 	sc->sc_stats.rdretries++;
    812 #endif
    813 	sc->sc_flags &= ~RDF_SEEK;
    814 	rdreset(sc);
    815 	if (sc->sc_errcnt++ < RDRETRY)
    816 		goto again;
    817 	printf("%s: rdstart err: cmd 0x%x sect %ld blk %" PRId64 " len %d\n",
    818 	    device_xname(sc->sc_dev), sc->sc_ioc.c_cmd, sc->sc_ioc.c_addr,
    819 	    bp->b_blkno, sc->sc_resid);
    820 	bp->b_error = EIO;
    821 	bp = rdfinish(sc, bp);
    822 	if (bp) {
    823 		sc->sc_addr = bp->b_data;
    824 		sc->sc_resid = bp->b_bcount;
    825 		if (hpibreq(device_parent(sc->sc_dev), &sc->sc_hq))
    826 			goto again;
    827 	}
    828 }
    829 
    830 static void
    831 rdgo(void *arg)
    832 {
    833 	struct rd_softc *sc = arg;
    834 	struct buf *bp = bufq_peek(sc->sc_tab);
    835 	int rw, ctlr, slave;
    836 
    837 	ctlr = device_unit(device_parent(sc->sc_dev));
    838 	slave = sc->sc_slave;
    839 
    840 	rw = bp->b_flags & B_READ;
    841 
    842 	/* Instrumentation. */
    843 	disk_busy(&sc->sc_dkdev);
    844 
    845 #ifdef USELEDS
    846 	ledcontrol(0, 0, LED_DISK);
    847 #endif
    848 	hpibgo(ctlr, slave, C_EXEC, sc->sc_addr, sc->sc_resid, rw, rw != 0);
    849 }
    850 
    851 /* ARGSUSED */
    852 static void
    853 rdintr(void *arg)
    854 {
    855 	struct rd_softc *sc = arg;
    856 	int unit = device_unit(sc->sc_dev);
    857 	struct buf *bp = bufq_peek(sc->sc_tab);
    858 	u_char stat = 13;	/* in case hpibrecv fails */
    859 	int rv, restart, ctlr, slave;
    860 
    861 	ctlr = device_unit(device_parent(sc->sc_dev));
    862 	slave = sc->sc_slave;
    863 
    864 #ifdef DEBUG
    865 	if (rddebug & RDB_FOLLOW)
    866 		printf("rdintr(%d): bp %p, %c, flags %x\n", unit, bp,
    867 		    (bp->b_flags & B_READ) ? 'R' : 'W', sc->sc_flags);
    868 	if (bp == NULL) {
    869 		printf("%s: bp == NULL\n", device_xname(sc->sc_dev));
    870 		return;
    871 	}
    872 #endif
    873 	disk_unbusy(&sc->sc_dkdev, (bp->b_bcount - bp->b_resid),
    874 	    (bp->b_flags & B_READ));
    875 
    876 	if (sc->sc_flags & RDF_SEEK) {
    877 		sc->sc_flags &= ~RDF_SEEK;
    878 		if (hpibustart(ctlr))
    879 			rdgo(sc);
    880 		return;
    881 	}
    882 	if ((sc->sc_flags & RDF_SWAIT) == 0) {
    883 #ifdef DEBUG
    884 		sc->sc_stats.rdpolltries++;
    885 #endif
    886 		if (hpibpptest(ctlr, slave) == 0) {
    887 #ifdef DEBUG
    888 			sc->sc_stats.rdpollwaits++;
    889 #endif
    890 
    891 			/* Instrumentation. */
    892 			disk_busy(&sc->sc_dkdev);
    893 			sc->sc_flags |= RDF_SWAIT;
    894 			hpibawait(ctlr);
    895 			return;
    896 		}
    897 	} else
    898 		sc->sc_flags &= ~RDF_SWAIT;
    899 	rv = hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
    900 	if (rv != 1 || stat) {
    901 #ifdef DEBUG
    902 		if (rddebug & RDB_ERROR)
    903 			printf("rdintr: recv failed or bad stat %d\n", stat);
    904 #endif
    905 		restart = rderror(unit);
    906 #ifdef DEBUG
    907 		sc->sc_stats.rdretries++;
    908 #endif
    909 		if (sc->sc_errcnt++ < RDRETRY) {
    910 			if (restart)
    911 				rdstart(sc);
    912 			return;
    913 		}
    914 		bp->b_error = EIO;
    915 	}
    916 	if (rdfinish(sc, bp))
    917 		rdustart(sc);
    918 	rnd_add_uint32(&sc->rnd_source, bp->b_blkno);
    919 }
    920 
    921 static int
    922 rdstatus(struct rd_softc *sc)
    923 {
    924 	int c, s;
    925 	u_char stat;
    926 	int rv;
    927 
    928 	c = device_unit(device_parent(sc->sc_dev));
    929 	s = sc->sc_slave;
    930 	sc->sc_rsc.c_unit = C_SUNIT(sc->sc_punit);
    931 	sc->sc_rsc.c_sram = C_SRAM;
    932 	sc->sc_rsc.c_ram = C_RAM;
    933 	sc->sc_rsc.c_cmd = C_STATUS;
    934 	memset((void *)&sc->sc_stat, 0, sizeof(sc->sc_stat));
    935 	rv = hpibsend(c, s, C_CMD, &sc->sc_rsc, sizeof(sc->sc_rsc));
    936 	if (rv != sizeof(sc->sc_rsc)) {
    937 #ifdef DEBUG
    938 		if (rddebug & RDB_STATUS)
    939 			printf("rdstatus: send C_CMD failed %d != %d\n",
    940 			    rv, sizeof(sc->sc_rsc));
    941 #endif
    942 		return 1;
    943 	}
    944 	rv = hpibrecv(c, s, C_EXEC, &sc->sc_stat, sizeof(sc->sc_stat));
    945 	if (rv != sizeof(sc->sc_stat)) {
    946 #ifdef DEBUG
    947 		if (rddebug & RDB_STATUS)
    948 			printf("rdstatus: send C_EXEC failed %d != %d\n",
    949 			    rv, sizeof(sc->sc_stat));
    950 #endif
    951 		return 1;
    952 	}
    953 	rv = hpibrecv(c, s, C_QSTAT, &stat, 1);
    954 	if (rv != 1 || stat) {
    955 #ifdef DEBUG
    956 		if (rddebug & RDB_STATUS)
    957 			printf("rdstatus: recv failed %d or bad stat %d\n",
    958 			    rv, stat);
    959 #endif
    960 		return 1;
    961 	}
    962 	return 0;
    963 }
    964 
    965 /*
    966  * Deal with errors.
    967  * Returns 1 if request should be restarted,
    968  * 0 if we should just quietly give up.
    969  */
    970 static int
    971 rderror(int unit)
    972 {
    973 	struct rd_softc *sc = device_lookup_private(&rd_cd,unit);
    974 	struct rd_stat *sp;
    975 	struct buf *bp;
    976 	daddr_t hwbn, pbn;
    977 	char *hexstr(int, int); /* XXX */
    978 
    979 	if (rdstatus(sc)) {
    980 #ifdef DEBUG
    981 		printf("%s: couldn't get status\n", device_xname(sc->sc_dev));
    982 #endif
    983 		rdreset(sc);
    984 		return 1;
    985 	}
    986 	sp = &sc->sc_stat;
    987 	if (sp->c_fef & FEF_REXMT)
    988 		return 1;
    989 	if (sp->c_fef & FEF_PF) {
    990 		rdreset(sc);
    991 		return 1;
    992 	}
    993 	/*
    994 	 * Unit requests release for internal maintenance.
    995 	 * We just delay awhile and try again later.  Use expontially
    996 	 * increasing backoff ala ethernet drivers since we don't really
    997 	 * know how long the maintenance will take.  With RDWAITC and
    998 	 * RDRETRY as defined, the range is 1 to 32 seconds.
    999 	 */
   1000 	if (sp->c_fef & FEF_IMR) {
   1001 		extern int hz;
   1002 		int rdtimo = RDWAITC << sc->sc_errcnt;
   1003 #ifdef DEBUG
   1004 		printf("%s: internal maintenance, %d second timeout\n",
   1005 		    device_xname(sc->sc_dev), rdtimo);
   1006 		sc->sc_stats.rdtimeouts++;
   1007 #endif
   1008 		hpibfree(device_parent(sc->sc_dev), &sc->sc_hq);
   1009 		callout_reset(&sc->sc_restart_ch, rdtimo * hz, rdrestart, sc);
   1010 		return 0;
   1011 	}
   1012 	/*
   1013 	 * Only report error if we have reached the error reporting
   1014 	 * threshhold.  By default, this will only report after the
   1015 	 * retry limit has been exceeded.
   1016 	 */
   1017 	if (sc->sc_errcnt < rderrthresh)
   1018 		return 1;
   1019 
   1020 	/*
   1021 	 * First conjure up the block number at which the error occurred.
   1022 	 * Note that not all errors report a block number, in that case
   1023 	 * we just use b_blkno.
   1024 	 */
   1025 	bp = bufq_peek(sc->sc_tab);
   1026 	pbn = sc->sc_dkdev.dk_label->d_partitions[rdpart(bp->b_dev)].p_offset;
   1027 	if ((sp->c_fef & FEF_CU) || (sp->c_fef & FEF_DR) ||
   1028 	    (sp->c_ief & IEF_RRMASK)) {
   1029 		hwbn = RDBTOS(pbn + bp->b_blkno);
   1030 		pbn = bp->b_blkno;
   1031 	} else {
   1032 		hwbn = sp->c_blk;
   1033 		pbn = RDSTOB(hwbn) - pbn;
   1034 	}
   1035 	/*
   1036 	 * Now output a generic message suitable for badsect.
   1037 	 * Note that we don't use harderr cuz it just prints
   1038 	 * out b_blkno which is just the beginning block number
   1039 	 * of the transfer, not necessary where the error occurred.
   1040 	 */
   1041 	printf("%s%c: hard error sn%" PRId64 "\n", device_xname(sc->sc_dev),
   1042 	    'a'+rdpart(bp->b_dev), pbn);
   1043 	/*
   1044 	 * Now report the status as returned by the hardware with
   1045 	 * attempt at interpretation (unless debugging).
   1046 	 */
   1047 	printf("%s %s error:", device_xname(sc->sc_dev),
   1048 	    (bp->b_flags & B_READ) ? "read" : "write");
   1049 #ifdef DEBUG
   1050 	if (rddebug & RDB_ERROR) {
   1051 		/* status info */
   1052 		printf("\n    volume: %d, unit: %d\n",
   1053 		    (sp->c_vu>>4)&0xF, sp->c_vu&0xF);
   1054 		rdprinterr("reject", sp->c_ref, err_reject);
   1055 		rdprinterr("fault", sp->c_fef, err_fault);
   1056 		rdprinterr("access", sp->c_aef, err_access);
   1057 		rdprinterr("info", sp->c_ief, err_info);
   1058 		printf("    block: %lld, P1-P10: ", hwbn);
   1059 		printf("0x%x", *(u_int *)&sp->c_raw[0]);
   1060 		printf("0x%x", *(u_int *)&sp->c_raw[4]);
   1061 		printf("0x%x\n", *(u_short *)&sp->c_raw[8]);
   1062 		/* command */
   1063 		printf("    ioc: ");
   1064 		printf("0x%x", *(u_int *)&sc->sc_ioc.c_pad);
   1065 		printf("0x%x", *(u_short *)&sc->sc_ioc.c_hiaddr);
   1066 		printf("0x%x", *(u_int *)&sc->sc_ioc.c_addr);
   1067 		printf("0x%x", *(u_short *)&sc->sc_ioc.c_nop2);
   1068 		printf("0x%x", *(u_int *)&sc->sc_ioc.c_len);
   1069 		printf("0x%x\n", *(u_short *)&sc->sc_ioc.c_cmd);
   1070 		return 1;
   1071 	}
   1072 #endif
   1073 	printf(" v%d u%d, R0x%x F0x%x A0x%x I0x%x\n",
   1074 	    (sp->c_vu>>4)&0xF, sp->c_vu&0xF,
   1075 	    sp->c_ref, sp->c_fef, sp->c_aef, sp->c_ief);
   1076 	printf("P1-P10: ");
   1077 	printf("0x%x", *(u_int *)&sp->c_raw[0]);
   1078 	printf("0x%x", *(u_int *)&sp->c_raw[4]);
   1079 	printf("0x%x\n", *(u_short *)&sp->c_raw[8]);
   1080 	return 1;
   1081 }
   1082 
   1083 static int
   1084 rdread(dev_t dev, struct uio *uio, int flags)
   1085 {
   1086 
   1087 	return physio(rdstrategy, NULL, dev, B_READ, minphys, uio);
   1088 }
   1089 
   1090 static int
   1091 rdwrite(dev_t dev, struct uio *uio, int flags)
   1092 {
   1093 
   1094 	return physio(rdstrategy, NULL, dev, B_WRITE, minphys, uio);
   1095 }
   1096 
   1097 static int
   1098 rdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
   1099 {
   1100 	struct rd_softc *sc = device_lookup_private(&rd_cd, rdunit(dev));
   1101 	struct disklabel *lp = sc->sc_dkdev.dk_label;
   1102 	int error, flags;
   1103 
   1104 	error = disk_ioctl(&sc->sc_dkdev, rdpart(dev), cmd, data, flag, l);
   1105 	if (error != EPASSTHROUGH)
   1106 		return error;
   1107 
   1108 	switch (cmd) {
   1109 	case DIOCWLABEL:
   1110 		if ((flag & FWRITE) == 0)
   1111 			return EBADF;
   1112 		if (*(int *)data)
   1113 			sc->sc_flags |= RDF_WLABEL;
   1114 		else
   1115 			sc->sc_flags &= ~RDF_WLABEL;
   1116 		return 0;
   1117 
   1118 	case DIOCSDINFO:
   1119 		if ((flag & FWRITE) == 0)
   1120 			return EBADF;
   1121 		return setdisklabel(lp, (struct disklabel *)data,
   1122 		    (sc->sc_flags & RDF_WLABEL) ? 0 : sc->sc_dkdev.dk_openmask,
   1123 		    NULL);
   1124 
   1125 	case DIOCWDINFO:
   1126 		if ((flag & FWRITE) == 0)
   1127 			return EBADF;
   1128 		error = setdisklabel(lp, (struct disklabel *)data,
   1129 		    (sc->sc_flags & RDF_WLABEL) ? 0 : sc->sc_dkdev.dk_openmask,
   1130 		    NULL);
   1131 		if (error)
   1132 			return error;
   1133 		flags = sc->sc_flags;
   1134 		sc->sc_flags = RDF_ALIVE | RDF_WLABEL;
   1135 		error = writedisklabel(rdlabdev(dev), rdstrategy, lp, NULL);
   1136 		sc->sc_flags = flags;
   1137 		return error;
   1138 
   1139 	case DIOCGDEFLABEL:
   1140 		rdgetdefaultlabel(sc, (struct disklabel *)data);
   1141 		return 0;
   1142 	}
   1143 	return EINVAL;
   1144 }
   1145 
   1146 static void
   1147 rdgetdefaultlabel(struct rd_softc *sc, struct disklabel *lp)
   1148 {
   1149 	int type = sc->sc_type;
   1150 
   1151 	memset((void *)lp, 0, sizeof(struct disklabel));
   1152 
   1153 	lp->d_type = DKTYPE_HPIB;
   1154 	lp->d_secsize = DEV_BSIZE;
   1155 	lp->d_nsectors = rdidentinfo[type].ri_nbpt;
   1156 	lp->d_ntracks = rdidentinfo[type].ri_ntpc;
   1157 	lp->d_ncylinders = rdidentinfo[type].ri_ncyl;
   1158 	lp->d_secperunit = rdidentinfo[type].ri_nblocks;
   1159 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
   1160 
   1161 	strlcpy(lp->d_typename, rdidentinfo[type].ri_desc,
   1162 	    sizeof(lp->d_typename));
   1163 	strlcpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
   1164 	lp->d_rpm = 3000;
   1165 	lp->d_interleave = 1;
   1166 	lp->d_flags = 0;
   1167 
   1168 	lp->d_partitions[RAW_PART].p_offset = 0;
   1169 	lp->d_partitions[RAW_PART].p_size =
   1170 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
   1171 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
   1172 	lp->d_npartitions = RAW_PART + 1;
   1173 
   1174 	lp->d_magic = DISKMAGIC;
   1175 	lp->d_magic2 = DISKMAGIC;
   1176 	lp->d_checksum = dkcksum(lp);
   1177 }
   1178 
   1179 int
   1180 rdsize(dev_t dev)
   1181 {
   1182 	struct rd_softc *sc;
   1183 	int psize, didopen = 0;
   1184 
   1185 	sc = device_lookup_private(&rd_cd, rdunit(dev));
   1186 	if (sc == NULL)
   1187 		return ENXIO;
   1188 
   1189 	if ((sc->sc_flags & RDF_ALIVE) == 0)
   1190 		return ENXIO;
   1191 
   1192 	/*
   1193 	 * We get called very early on (via swapconf)
   1194 	 * without the device being open so we may need
   1195 	 * to handle it here.
   1196 	 */
   1197 	if (sc->sc_dkdev.dk_openmask == 0) {
   1198 		if (rdopen(dev, FREAD|FWRITE, S_IFBLK, NULL))
   1199 			return -1;
   1200 		didopen = 1;
   1201 	}
   1202 	psize = sc->sc_dkdev.dk_label->d_partitions[rdpart(dev)].p_size *
   1203 	    (sc->sc_dkdev.dk_label->d_secsize / DEV_BSIZE);
   1204 	if (didopen)
   1205 		(void)rdclose(dev, FREAD|FWRITE, S_IFBLK, NULL);
   1206 	return psize;
   1207 }
   1208 
   1209 #ifdef DEBUG
   1210 static void
   1211 rdprinterr(const char *str, short err, const char **tab)
   1212 {
   1213 	int i;
   1214 	int printed;
   1215 
   1216 	if (err == 0)
   1217 		return;
   1218 	printf("    %s error %d field:", str, err);
   1219 	printed = 0;
   1220 	for (i = 0; i < 16; i++)
   1221 		if (err & (0x8000 >> i))
   1222 			printf("%s%s", printed++ ? " + " : " ", tab[i]);
   1223 	printf("\n");
   1224 }
   1225 #endif
   1226 
   1227 static int rddoingadump;	/* simple mutex */
   1228 
   1229 /*
   1230  * Non-interrupt driven, non-DMA dump routine.
   1231  */
   1232 static int
   1233 rddump(dev_t dev, daddr_t blkno, void *va, size_t size)
   1234 {
   1235 	int sectorsize;		/* size of a disk sector */
   1236 	int nsects;		/* number of sectors in partition */
   1237 	int sectoff;		/* sector offset of partition */
   1238 	int totwrt;		/* total number of sectors left to write */
   1239 	int nwrt;		/* current number of sectors to write */
   1240 	int part;
   1241 	int ctlr, slave;
   1242 	struct rd_softc *sc;
   1243 	struct disklabel *lp;
   1244 	char stat;
   1245 
   1246 	/* Check for recursive dump; if so, punt. */
   1247 	if (rddoingadump)
   1248 		return EFAULT;
   1249 	rddoingadump = 1;
   1250 
   1251 	/* Decompose unit and partition. */
   1252 	part = rdpart(dev);
   1253 
   1254 	/* Make sure dump device is ok. */
   1255 	sc = device_lookup_private(&rd_cd, rdunit(dev));
   1256 	if (sc == NULL)
   1257 		return ENXIO;
   1258 
   1259 	if ((sc->sc_flags & RDF_ALIVE) == 0)
   1260 		return ENXIO;
   1261 
   1262 	ctlr = device_unit(device_parent(sc->sc_dev));
   1263 	slave = sc->sc_slave;
   1264 
   1265 	/*
   1266 	 * Convert to disk sectors.  Request must be a multiple of size.
   1267 	 */
   1268 	lp = sc->sc_dkdev.dk_label;
   1269 	sectorsize = lp->d_secsize;
   1270 	if ((size % sectorsize) != 0)
   1271 		return EFAULT;
   1272 	totwrt = size / sectorsize;
   1273 	blkno = dbtob(blkno) / sectorsize;	/* blkno in DEV_BSIZE units */
   1274 
   1275 	nsects = lp->d_partitions[part].p_size;
   1276 	sectoff = lp->d_partitions[part].p_offset;
   1277 
   1278 	/* Check transfer bounds against partition size. */
   1279 	if ((blkno < 0) || (blkno + totwrt) > nsects)
   1280 		return EINVAL;
   1281 
   1282 	/* Offset block number to start of partition. */
   1283 	blkno += sectoff;
   1284 
   1285 	while (totwrt > 0) {
   1286 		nwrt = totwrt;		/* XXX */
   1287 #ifndef RD_DUMP_NOT_TRUSTED
   1288 		/*
   1289 		 * Fill out and send HPIB command.
   1290 		 */
   1291 		sc->sc_ioc.c_unit = C_SUNIT(sc->sc_punit);
   1292 		sc->sc_ioc.c_volume = C_SVOL(0);
   1293 		sc->sc_ioc.c_saddr = C_SADDR;
   1294 		sc->sc_ioc.c_hiaddr = 0;
   1295 		sc->sc_ioc.c_addr = RDBTOS(blkno);
   1296 		sc->sc_ioc.c_nop2 = C_NOP;
   1297 		sc->sc_ioc.c_slen = C_SLEN;
   1298 		sc->sc_ioc.c_len = nwrt * sectorsize;
   1299 		sc->sc_ioc.c_cmd = C_WRITE;
   1300 		hpibsend(ctlr, slave, C_CMD, &sc->sc_ioc.c_unit,
   1301 		    sizeof(sc->sc_ioc) - 2);
   1302 		if (hpibswait(ctlr, slave))
   1303 			return EIO;
   1304 
   1305 		/*
   1306 		 * Send the data.
   1307 		 */
   1308 		hpibsend(ctlr, slave, C_EXEC, va, nwrt * sectorsize);
   1309 		(void) hpibswait(ctlr, slave);
   1310 		hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
   1311 		if (stat)
   1312 			return EIO;
   1313 #else /* RD_DUMP_NOT_TRUSTED */
   1314 		/* Let's just talk about this first... */
   1315 		printf("%s: dump addr %p, blk %d\n", device_xname(sc->sc_dev),
   1316 		    va, blkno);
   1317 		delay(500 * 1000);	/* half a second */
   1318 #endif /* RD_DUMP_NOT_TRUSTED */
   1319 
   1320 		/* update block count */
   1321 		totwrt -= nwrt;
   1322 		blkno += nwrt;
   1323 		va = (char *)va + sectorsize * nwrt;
   1324 	}
   1325 	rddoingadump = 0;
   1326 	return 0;
   1327 }
   1328