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