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