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