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