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