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