Home | History | Annotate | Line # | Download | only in dev
rd.c revision 1.123
      1 /*	$NetBSD: rd.c,v 1.123 2022/12/03 06:08:18 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.123 2022/12/03 06:08:18 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 
    434 static void	rdgetdefaultlabel(struct rd_softc *, struct disklabel *);
    435 static void	rdrestart(void *);
    436 static void	rdustart(struct rd_softc *);
    437 static struct buf *rdfinish(struct rd_softc *, struct buf *);
    438 static void	rdstart(void *);
    439 static void	rdgo(void *);
    440 static void	rdintr(void *);
    441 static int	rdstatus(struct rd_softc *);
    442 static int	rderror(int);
    443 #ifdef DEBUG
    444 static void	rdprinterr(const char *, short, const char **);
    445 #endif
    446 
    447 static int	rdmatch(device_t, cfdata_t, void *);
    448 static void	rdattach(device_t, device_t, void *);
    449 
    450 CFATTACH_DECL_NEW(rd, sizeof(struct rd_softc),
    451     rdmatch, rdattach, NULL, NULL);
    452 
    453 static dev_type_open(rdopen);
    454 static dev_type_close(rdclose);
    455 static dev_type_read(rdread);
    456 static dev_type_write(rdwrite);
    457 static dev_type_ioctl(rdioctl);
    458 static dev_type_strategy(rdstrategy);
    459 static dev_type_dump(rddump);
    460 static dev_type_size(rdsize);
    461 
    462 const struct bdevsw rd_bdevsw = {
    463 	.d_open = rdopen,
    464 	.d_close = rdclose,
    465 	.d_strategy = rdstrategy,
    466 	.d_ioctl = rdioctl,
    467 	.d_dump = rddump,
    468 	.d_psize = rdsize,
    469 	.d_discard = nodiscard,
    470 	.d_flag = D_DISK
    471 };
    472 
    473 const struct cdevsw rd_cdevsw = {
    474 	.d_open = rdopen,
    475 	.d_close = rdclose,
    476 	.d_read = rdread,
    477 	.d_write = rdwrite,
    478 	.d_ioctl = rdioctl,
    479 	.d_stop = nostop,
    480 	.d_tty = notty,
    481 	.d_poll = nopoll,
    482 	.d_mmap = nommap,
    483 	.d_kqfilter = nokqfilter,
    484 	.d_discard = nodiscard,
    485 	.d_flag = D_DISK
    486 };
    487 
    488 static int
    489 rdmatch(device_t parent, cfdata_t cf, void *aux)
    490 {
    491 	struct hpibbus_attach_args *ha = aux;
    492 
    493 	return rdident(parent, NULL, ha);
    494 }
    495 
    496 static void
    497 rdattach(device_t parent, device_t self, void *aux)
    498 {
    499 	struct rd_softc *sc = device_private(self);
    500 	struct hpibbus_attach_args *ha = aux;
    501 	int id;
    502 	char pbuf[9];
    503 
    504 	sc->sc_dev = self;
    505 	bufq_alloc(&sc->sc_tab, "disksort", BUFQ_SORT_RAWBLOCK);
    506 
    507 	if (rdident(parent, sc, ha) == 0) {
    508 		aprint_error(": didn't respond to describe command!\n");
    509 		return;
    510 	}
    511 
    512 	/*
    513 	 * XXX We use DEV_BSIZE instead of the sector size value pulled
    514 	 * XXX off the driver because all of this code assumes 512 byte
    515 	 * XXX blocks.  ICK!
    516 	 */
    517 	id = sc->sc_type;
    518 	aprint_normal(": %s\n", rdidentinfo[id].ri_desc);
    519 	format_bytes(pbuf, sizeof(pbuf),
    520 	    rdidentinfo[id].ri_nblocks * DEV_BSIZE);
    521 	aprint_normal_dev(sc->sc_dev, "%s, %d cyl, %d head, %d sec,"
    522 	    " %d bytes/block x %u blocks\n",
    523 	    pbuf, rdidentinfo[id].ri_ncyl, rdidentinfo[id].ri_ntpc,
    524 	    rdidentinfo[id].ri_nbpt,
    525 	    DEV_BSIZE, rdidentinfo[id].ri_nblocks);
    526 
    527 	/*
    528 	 * Initialize and attach the disk structure.
    529 	 */
    530 	memset(&sc->sc_dkdev, 0, sizeof(sc->sc_dkdev));
    531 	disk_init(&sc->sc_dkdev, device_xname(sc->sc_dev), NULL);
    532 	disk_attach(&sc->sc_dkdev);
    533 	rd_set_geom(sc);
    534 
    535 	sc->sc_slave = ha->ha_slave;
    536 	sc->sc_punit = ha->ha_punit;
    537 
    538 	callout_init(&sc->sc_restart_ch, 0);
    539 
    540 	/* Initialize the hpib job queue entry */
    541 	sc->sc_hq.hq_softc = sc;
    542 	sc->sc_hq.hq_slave = sc->sc_slave;
    543 	sc->sc_hq.hq_start = rdstart;
    544 	sc->sc_hq.hq_go = rdgo;
    545 	sc->sc_hq.hq_intr = rdintr;
    546 
    547 	sc->sc_flags = RDF_ALIVE;
    548 #ifdef DEBUG
    549 	/* always report errors */
    550 	if ((rddebug & RDB_ERROR) != 0)
    551 		rderrthresh = 0;
    552 #endif
    553 	/*
    554 	 * attach the device into the random source list
    555 	 */
    556 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
    557 	    RND_TYPE_DISK, RND_FLAG_DEFAULT);
    558 }
    559 
    560 static int
    561 rdident(device_t parent, struct rd_softc *sc, struct hpibbus_attach_args *ha)
    562 {
    563 	struct cs80_describe desc;
    564 	uint8_t stat, cmd[3];
    565 	char name[7];
    566 	int i, id, n, ctlr, slave;
    567 
    568 	ctlr = device_unit(parent);
    569 	slave = ha->ha_slave;
    570 
    571 	/* Verify that we have a CS80 device. */
    572 	if ((ha->ha_id & 0x200) == 0)
    573 		return 0;
    574 
    575 	/* Is it one of the disks we support? */
    576 	for (id = 0; id < numrdidentinfo; id++)
    577 		if (ha->ha_id == rdidentinfo[id].ri_hwid)
    578 			break;
    579 	if (id == numrdidentinfo)
    580 		return 0;
    581 
    582 	/*
    583 	 * The supported dvice ID is probed.
    584 	 * Check if the specified physical unit is actually supported
    585 	 * by brandnew HP-IB emulator devices like HPDisk and HPDrive etc.
    586 	 */
    587 	/*
    588 	 * Reset device and collect description
    589 	 */
    590 	memset(&desc, 0, sizeof(desc));
    591 	stat = 0;
    592 	rdreset_unit(ctlr, slave, ha->ha_punit);
    593 	cmd[0] = C_SUNIT(ha->ha_punit);
    594 	cmd[1] = C_SVOL(0);
    595 	cmd[2] = C_DESC;
    596 	hpibsend(ctlr, slave, C_CMD, cmd, sizeof(cmd));
    597 	hpibrecv(ctlr, slave, C_EXEC, &desc, sizeof(desc));
    598 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
    599 
    600 	if (stat != 0 || desc.d_name == 0) {
    601 		/*
    602 		 * No valid response from the specified punit.
    603 		 *
    604 		 * Note it looks HPDisk responds to commands against
    605 		 * supported but not-configured punits at 1 to 3.
    606 		 */
    607 		return 0;
    608 	}
    609 
    610 	/*
    611 	 * If we're just probing for the device, that's all the
    612 	 * work we need to do.
    613 	 */
    614 	if (sc == NULL)
    615 		return 1;
    616 
    617 	memset(name, 0, sizeof(name));
    618 	n = desc.d_name;
    619 	for (i = 5; i >= 0; i--) {
    620 		name[i] = (n & 0xf) + '0';
    621 		n >>= 4;
    622 	}
    623 
    624 #ifdef DEBUG
    625 	if (rddebug & RDB_IDENT) {
    626 		aprint_normal("\n");
    627 		aprint_normal_dev(sc->sc_dev, "id: 0x%04x, name: %x ('%s')\n",
    628 		    ha->ha_id, desc.d_name, name);
    629 		aprint_normal("  iuw %x, maxxfr %d, ctype %d\n",
    630 		    desc.d_iuw, desc.d_cmaxxfr, desc.d_ctype);
    631 		aprint_normal("  utype %d, bps %d, blkbuf %d, burst %d,"
    632 		    " blktime %d\n",
    633 		    desc.d_utype, desc.d_sectsize,
    634 		    desc.d_blkbuf, desc.d_burstsize, desc.d_blocktime);
    635 		aprint_normal("  avxfr %d, ort %d, atp %d, maxint %d, fv %x"
    636 		    ", rv %x\n",
    637 		    desc.d_uavexfr, desc.d_retry, desc.d_access,
    638 		    desc.d_maxint, desc.d_fvbyte, desc.d_rvbyte);
    639 		aprint_normal("  maxcyl/head/sect %d/%d/%d, maxvsect %d,"
    640 		    " inter %d\n",
    641 		    desc.d_maxcyl, desc.d_maxhead, desc.d_maxsect,
    642 		    desc.d_maxvsectl, desc.d_interleave);
    643 		aprint_normal("%s", device_xname(sc->sc_dev));
    644 	}
    645 #endif
    646 
    647 	/*
    648 	 * Take care of a couple of anomolies:
    649 	 * 1. 7945A, 7946A, and 7941A all return same HW id
    650 	 * 2. 9122S and 9134D both return same HW id
    651 	 * 3. 9122D and 9134L both return same HW id
    652 	 */
    653 	switch (ha->ha_id) {
    654 	case RD7946AID:
    655 		if (memcmp(name, RD7945ANAME, RDNAMELEN) == 0)
    656 			id = RD7945A;
    657 		else if (memcmp(name, RD7941ANAME, RDNAMELEN) == 0)
    658 			id = RD7941A;
    659 		else
    660 			id = RD7946A;
    661 		break;
    662 
    663 	case RD9134LID:
    664 		if (memcmp(name, RD9134LNAME, RDNAMELEN) == 0)
    665 			id = RD9134L;
    666 		else
    667 			id = RD9122D;
    668 		break;
    669 
    670 	case RD9134DID:
    671 		if (memcmp(name, RD9122SNAME, RDNAMELEN) == 0)
    672 			id = RD9122S;
    673 		else
    674 			id = RD9134D;
    675 		break;
    676 	}
    677 
    678 	/*
    679 	 * HPDisk can have independent physical units that are not
    680 	 * corresponding to device IDs.
    681 	 * To handle this, we have to check names in the drive description
    682 	 * data for punit >= 1.
    683 	 */
    684 	if (ha->ha_punit >= 1) {
    685 		for (i = 0; i < numrdname2id; i++) {
    686 			if (memcmp(name, rdname2id[i].rn_name,
    687 			    RDNAMELEN) == 0) {
    688 				id = rdname2id[i].rn_id;
    689 				break;
    690 			}
    691 		}
    692 	}
    693 
    694 	sc->sc_type = id;
    695 
    696 	return 1;
    697 }
    698 
    699 static void
    700 rdreset(struct rd_softc *sc)
    701 {
    702 	int ctlr, slave, punit;
    703 
    704 	ctlr = device_unit(device_parent(sc->sc_dev));
    705 	slave = sc->sc_slave;
    706 	punit = sc->sc_punit;
    707 	rdreset_unit(ctlr, slave, punit);
    708 #ifdef DEBUG
    709 	sc->sc_stats.rdresets++;
    710 #endif
    711 }
    712 
    713 static void
    714 rdreset_unit(int ctlr, int slave, int punit)
    715 {
    716 	struct rd_ssmcmd ssmc;
    717 	struct rd_srcmd src;
    718 	struct rd_clearcmd clear;
    719 	uint8_t stat;
    720 
    721 	clear.c_unit = C_SUNIT(punit);
    722 	clear.c_cmd = C_CLEAR;
    723 	hpibsend(ctlr, slave, C_TCMD, &clear, sizeof(clear));
    724 	hpibswait(ctlr, slave);
    725 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
    726 
    727 	src.c_unit = C_SUNIT(RDCTLR);
    728 	src.c_nop = C_NOP;
    729 	src.c_cmd = C_SREL;
    730 	src.c_param = C_REL;
    731 	hpibsend(ctlr, slave, C_CMD, &src, sizeof(src));
    732 	hpibswait(ctlr, slave);
    733 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
    734 
    735 	ssmc.c_unit = C_SUNIT(punit);
    736 	ssmc.c_cmd = C_SSM;
    737 	ssmc.c_refm = REF_MASK;
    738 	ssmc.c_fefm = FEF_MASK;
    739 	ssmc.c_aefm = AEF_MASK;
    740 	ssmc.c_iefm = IEF_MASK;
    741 	hpibsend(ctlr, slave, C_CMD, &ssmc, sizeof(ssmc));
    742 	hpibswait(ctlr, slave);
    743 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
    744 }
    745 
    746 static void
    747 rd_set_geom(struct rd_softc *sc)
    748 {
    749 	struct disk_geom *dg = &sc->sc_dkdev.dk_geom;
    750 	const struct rdidentinfo *ri = &rdidentinfo[sc->sc_type];
    751 
    752 	memset(dg, 0, sizeof(*dg));
    753 
    754 	dg->dg_secsize = DEV_BSIZE;
    755 	dg->dg_nsectors = ri->ri_nbpt;
    756 	dg->dg_ntracks = ri->ri_ntpc;
    757 	dg->dg_ncylinders = ri->ri_ncyl;
    758 	dg->dg_secperunit = ri->ri_nblocks;
    759 
    760 	disk_set_info(sc->sc_dev, &sc->sc_dkdev, ri->ri_desc);
    761 }
    762 
    763 /*
    764  * Read or construct a disklabel
    765  */
    766 static int
    767 rdgetinfo(dev_t dev)
    768 {
    769 	struct rd_softc *sc = device_lookup_private(&rd_cd, rdunit(dev));
    770 	struct disklabel *lp = sc->sc_dkdev.dk_label;
    771 	struct partition *pi;
    772 	const char *msg;
    773 
    774 	/*
    775 	 * Set some default values to use while reading the label
    776 	 * or to use if there isn't a label.
    777 	 */
    778 	memset((void *)lp, 0, sizeof *lp);
    779 	rdgetdefaultlabel(sc, lp);
    780 
    781 	/*
    782 	 * Now try to read the disklabel
    783 	 */
    784 	msg = readdisklabel(rdlabdev(dev), rdstrategy, lp, NULL);
    785 	if (msg == NULL)
    786 		return 0;
    787 
    788 	pi = lp->d_partitions;
    789 	printf("%s: WARNING: %s\n", device_xname(sc->sc_dev), msg);
    790 
    791 	pi[RAW_PART].p_size = rdidentinfo[sc->sc_type].ri_nblocks;
    792 	/* XXX reset other info since readdisklabel screws with it */
    793 	lp->d_npartitions = 3;
    794 	pi[0].p_size = 0;
    795 
    796 	return 0;
    797 }
    798 
    799 static int
    800 rdopen(dev_t dev, int flags, int mode, struct lwp *l)
    801 {
    802 	struct rd_softc *sc;
    803 	int error, mask, part;
    804 
    805 	sc = device_lookup_private(&rd_cd, rdunit(dev));
    806 	if (sc == NULL)
    807 		return ENXIO;
    808 
    809 	if ((sc->sc_flags & RDF_ALIVE) == 0)
    810 		return ENXIO;
    811 
    812 	/*
    813 	 * Wait for any pending opens/closes to complete
    814 	 */
    815 	while ((sc->sc_flags & (RDF_OPENING | RDF_CLOSING)) != 0)
    816 		(void)tsleep(sc, PRIBIO, "rdopen", 0);
    817 
    818 	/*
    819 	 * On first open, get label and partition info.
    820 	 * We may block reading the label, so be careful
    821 	 * to stop any other opens.
    822 	 */
    823 	if (sc->sc_dkdev.dk_openmask == 0) {
    824 		sc->sc_flags |= RDF_OPENING;
    825 		error = rdgetinfo(dev);
    826 		sc->sc_flags &= ~RDF_OPENING;
    827 		wakeup((void *)sc);
    828 		if (error)
    829 			return error;
    830 	}
    831 
    832 	part = rdpart(dev);
    833 	mask = 1 << part;
    834 
    835 	/* Check that the partition exists. */
    836 	if (part != RAW_PART &&
    837 	    (part > sc->sc_dkdev.dk_label->d_npartitions ||
    838 	     sc->sc_dkdev.dk_label->d_partitions[part].p_fstype == FS_UNUSED))
    839 		return ENXIO;
    840 
    841 	/* Ensure only one open at a time. */
    842 	switch (mode) {
    843 	case S_IFCHR:
    844 		sc->sc_dkdev.dk_copenmask |= mask;
    845 		break;
    846 	case S_IFBLK:
    847 		sc->sc_dkdev.dk_bopenmask |= mask;
    848 		break;
    849 	}
    850 	sc->sc_dkdev.dk_openmask =
    851 	    sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
    852 
    853 	return 0;
    854 }
    855 
    856 static int
    857 rdclose(dev_t dev, int flag, int mode, struct lwp *l)
    858 {
    859 	struct rd_softc *sc = device_lookup_private(&rd_cd, rdunit(dev));
    860 	struct disk *dk = &sc->sc_dkdev;
    861 	int mask, s;
    862 
    863 	mask = 1 << rdpart(dev);
    864 	if (mode == S_IFCHR)
    865 		dk->dk_copenmask &= ~mask;
    866 	else
    867 		dk->dk_bopenmask &= ~mask;
    868 	dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
    869 	/*
    870 	 * On last close, we wait for all activity to cease since
    871 	 * the label/partition info will become invalid.  Since we
    872 	 * might sleep, we must block any opens while we are here.
    873 	 * Note we don't have to about other closes since we know
    874 	 * we are the last one.
    875 	 */
    876 	if (dk->dk_openmask == 0) {
    877 		sc->sc_flags |= RDF_CLOSING;
    878 		s = splbio();
    879 		while (sc->sc_active) {
    880 			sc->sc_flags |= RDF_WANTED;
    881 			(void)tsleep(&sc->sc_tab, PRIBIO, "rdclose", 0);
    882 		}
    883 		splx(s);
    884 		sc->sc_flags &= ~(RDF_CLOSING | RDF_WLABEL);
    885 		wakeup((void *)sc);
    886 	}
    887 	return 0;
    888 }
    889 
    890 static void
    891 rdstrategy(struct buf *bp)
    892 {
    893 	struct rd_softc *sc = device_lookup_private(&rd_cd, rdunit(bp->b_dev));
    894 	struct partition *pinfo;
    895 	daddr_t bn;
    896 	int s;
    897 	int offset;
    898 
    899 #ifdef DEBUG
    900 	if ((rddebug & RDB_FOLLOW) != 0)
    901 		printf("rdstrategy(%p): dev %" PRIx64
    902 		    ", bn %llx, bcount %x, %c\n",
    903 		    bp, bp->b_dev, bp->b_blkno, bp->b_bcount,
    904 		    (bp->b_flags & B_READ) != 0 ? 'R' : 'W');
    905 #endif
    906 	bn = bp->b_blkno;
    907 	pinfo = &sc->sc_dkdev.dk_label->d_partitions[rdpart(bp->b_dev)];
    908 
    909 	/* Don't perform partition translation on RAW_PART. */
    910 	offset = (rdpart(bp->b_dev) == RAW_PART) ? 0 : pinfo->p_offset;
    911 
    912 	if (rdpart(bp->b_dev) == RAW_PART) {
    913 		if (bounds_check_with_mediasize(bp, DEV_BSIZE,
    914 		    rdidentinfo[sc->sc_type].ri_nblocks) <= 0)
    915 			goto done;
    916 	} else {
    917 		if (bounds_check_with_label(&sc->sc_dkdev, bp,
    918 		    (sc->sc_flags & RDF_WLABEL) != 0) <= 0)
    919 			goto done;
    920 	}
    921 	bp->b_rawblkno = bn + offset;
    922 	s = splbio();
    923 	bufq_put(sc->sc_tab, bp);
    924 	if (sc->sc_active == 0) {
    925 		sc->sc_active = 1;
    926 		rdustart(sc);
    927 	}
    928 	splx(s);
    929 	return;
    930  done:
    931 	biodone(bp);
    932 }
    933 
    934 /*
    935  * Called from timeout() when handling maintenance releases
    936  */
    937 static void
    938 rdrestart(void *arg)
    939 {
    940 	struct rd_softc *sc = arg;
    941 	int s;
    942 
    943 	s = splbio();
    944 	rdustart(sc);
    945 	splx(s);
    946 }
    947 
    948 static void
    949 rdustart(struct rd_softc *sc)
    950 {
    951 	struct buf *bp;
    952 
    953 	bp = bufq_peek(sc->sc_tab);
    954 	sc->sc_addr = bp->b_data;
    955 	sc->sc_resid = bp->b_bcount;
    956 	if (hpibreq(device_parent(sc->sc_dev), &sc->sc_hq))
    957 		rdstart(sc);
    958 }
    959 
    960 static struct buf *
    961 rdfinish(struct rd_softc *sc, struct buf *bp)
    962 {
    963 
    964 	sc->sc_errcnt = 0;
    965 	(void)bufq_get(sc->sc_tab);
    966 	bp->b_resid = 0;
    967 	biodone(bp);
    968 	hpibfree(device_parent(sc->sc_dev), &sc->sc_hq);
    969 	if ((bp = bufq_peek(sc->sc_tab)) != NULL)
    970 		return bp;
    971 	sc->sc_active = 0;
    972 	if ((sc->sc_flags & RDF_WANTED) != 0) {
    973 		sc->sc_flags &= ~RDF_WANTED;
    974 		wakeup((void *)&sc->sc_tab);
    975 	}
    976 	return NULL;
    977 }
    978 
    979 static void
    980 rdstart(void *arg)
    981 {
    982 	struct rd_softc *sc = arg;
    983 	struct buf *bp = bufq_peek(sc->sc_tab);
    984 	int ctlr, slave;
    985 
    986 	ctlr = device_unit(device_parent(sc->sc_dev));
    987 	slave = sc->sc_slave;
    988 
    989  again:
    990 #ifdef DEBUG
    991 	if (rddebug & RDB_FOLLOW)
    992 		printf("rdstart(%s): bp %p, %c\n", device_xname(sc->sc_dev), bp,
    993 		    (bp->b_flags & B_READ) ? 'R' : 'W');
    994 #endif
    995 	sc->sc_flags |= RDF_SEEK;
    996 	sc->sc_ioc.c_unit = C_SUNIT(sc->sc_punit);
    997 	sc->sc_ioc.c_volume = C_SVOL(0);
    998 	sc->sc_ioc.c_saddr = C_SADDR;
    999 	sc->sc_ioc.c_hiaddr = 0;
   1000 	sc->sc_ioc.c_addr = RDBTOS(bp->b_rawblkno);
   1001 	sc->sc_ioc.c_nop2 = C_NOP;
   1002 	sc->sc_ioc.c_slen = C_SLEN;
   1003 	sc->sc_ioc.c_len = sc->sc_resid;
   1004 	sc->sc_ioc.c_cmd = (bp->b_flags & B_READ) != 0 ? C_READ : C_WRITE;
   1005 #ifdef DEBUG
   1006 	if ((rddebug & RDB_IO) != 0)
   1007 		printf("rdstart: hpibsend(%x, %x, %x, %p, %x)\n",
   1008 		    ctlr, slave, C_CMD,
   1009 		    &sc->sc_ioc.c_unit, sizeof(sc->sc_ioc) - 2);
   1010 #endif
   1011 	if (hpibsend(ctlr, slave, C_CMD, &sc->sc_ioc.c_unit,
   1012 	    sizeof(sc->sc_ioc) - 2) == sizeof(sc->sc_ioc) - 2) {
   1013 
   1014 		/* Instrumentation. */
   1015 		disk_busy(&sc->sc_dkdev);
   1016 		iostat_seek(sc->sc_dkdev.dk_stats);
   1017 
   1018 #ifdef DEBUG
   1019 		if ((rddebug & RDB_IO) != 0)
   1020 			printf("rdstart: hpibawait(%x)\n", ctlr);
   1021 #endif
   1022 		hpibawait(ctlr);
   1023 		return;
   1024 	}
   1025 	/*
   1026 	 * Experience has shown that the hpibwait in this hpibsend will
   1027 	 * occasionally timeout.  It appears to occur mostly on old 7914
   1028 	 * drives with full maintenance tracks.  We should probably
   1029 	 * integrate this with the backoff code in rderror.
   1030 	 */
   1031 #ifdef DEBUG
   1032 	if ((rddebug & RDB_ERROR) != 0)
   1033 		printf("%s: rdstart: cmd %x adr %x blk %lld len %d ecnt %d\n",
   1034 		    device_xname(sc->sc_dev),
   1035 		    sc->sc_ioc.c_cmd, sc->sc_ioc.c_addr,
   1036 		    bp->b_blkno, sc->sc_resid, sc->sc_errcnt);
   1037 	sc->sc_stats.rdretries++;
   1038 #endif
   1039 	sc->sc_flags &= ~RDF_SEEK;
   1040 	rdreset(sc);
   1041 	if (sc->sc_errcnt++ < RDRETRY)
   1042 		goto again;
   1043 	printf("%s: rdstart err: cmd 0x%x sect %u blk %" PRId64 " len %d\n",
   1044 	    device_xname(sc->sc_dev), sc->sc_ioc.c_cmd, sc->sc_ioc.c_addr,
   1045 	    bp->b_blkno, sc->sc_resid);
   1046 	bp->b_error = EIO;
   1047 	bp = rdfinish(sc, bp);
   1048 	if (bp != NULL) {
   1049 		sc->sc_addr = bp->b_data;
   1050 		sc->sc_resid = bp->b_bcount;
   1051 		if (hpibreq(device_parent(sc->sc_dev), &sc->sc_hq))
   1052 			goto again;
   1053 	}
   1054 }
   1055 
   1056 static void
   1057 rdgo(void *arg)
   1058 {
   1059 	struct rd_softc *sc = arg;
   1060 	struct buf *bp = bufq_peek(sc->sc_tab);
   1061 	int rw, ctlr, slave;
   1062 
   1063 	ctlr = device_unit(device_parent(sc->sc_dev));
   1064 	slave = sc->sc_slave;
   1065 
   1066 	rw = bp->b_flags & B_READ;
   1067 
   1068 	/* Instrumentation. */
   1069 	disk_busy(&sc->sc_dkdev);
   1070 
   1071 #ifdef USELEDS
   1072 	ledcontrol(0, 0, LED_DISK);
   1073 #endif
   1074 	hpibgo(ctlr, slave, C_EXEC, sc->sc_addr, sc->sc_resid, rw, rw != 0);
   1075 }
   1076 
   1077 /* ARGSUSED */
   1078 static void
   1079 rdintr(void *arg)
   1080 {
   1081 	struct rd_softc *sc = arg;
   1082 	int unit = device_unit(sc->sc_dev);
   1083 	struct buf *bp = bufq_peek(sc->sc_tab);
   1084 	uint8_t stat = 13;	/* in case hpibrecv fails */
   1085 	int rv, restart, ctlr, slave;
   1086 
   1087 	ctlr = device_unit(device_parent(sc->sc_dev));
   1088 	slave = sc->sc_slave;
   1089 
   1090 #ifdef DEBUG
   1091 	if ((rddebug & RDB_FOLLOW) != 0)
   1092 		printf("rdintr(%d): bp %p, %c, flags %x\n", unit, bp,
   1093 		    (bp->b_flags & B_READ) ? 'R' : 'W', sc->sc_flags);
   1094 	if (bp == NULL) {
   1095 		printf("%s: bp == NULL\n", device_xname(sc->sc_dev));
   1096 		return;
   1097 	}
   1098 #endif
   1099 	disk_unbusy(&sc->sc_dkdev, (bp->b_bcount - bp->b_resid),
   1100 	    (bp->b_flags & B_READ));
   1101 
   1102 	if ((sc->sc_flags & RDF_SEEK) != 0) {
   1103 		sc->sc_flags &= ~RDF_SEEK;
   1104 		if (hpibustart(ctlr))
   1105 			rdgo(sc);
   1106 		return;
   1107 	}
   1108 	if ((sc->sc_flags & RDF_SWAIT) == 0) {
   1109 #ifdef DEBUG
   1110 		sc->sc_stats.rdpolltries++;
   1111 #endif
   1112 		if (hpibpptest(ctlr, slave) == 0) {
   1113 #ifdef DEBUG
   1114 			sc->sc_stats.rdpollwaits++;
   1115 #endif
   1116 
   1117 			/* Instrumentation. */
   1118 			disk_busy(&sc->sc_dkdev);
   1119 			sc->sc_flags |= RDF_SWAIT;
   1120 			hpibawait(ctlr);
   1121 			return;
   1122 		}
   1123 	} else
   1124 		sc->sc_flags &= ~RDF_SWAIT;
   1125 	rv = hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
   1126 	if (rv != 1 || stat != 0) {
   1127 #ifdef DEBUG
   1128 		if (rddebug & RDB_ERROR)
   1129 			printf("rdintr: recv failed or bad stat %d\n", stat);
   1130 #endif
   1131 		restart = rderror(unit);
   1132 #ifdef DEBUG
   1133 		sc->sc_stats.rdretries++;
   1134 #endif
   1135 		if (sc->sc_errcnt++ < RDRETRY) {
   1136 			if (restart)
   1137 				rdstart(sc);
   1138 			return;
   1139 		}
   1140 		bp->b_error = EIO;
   1141 	}
   1142 	if (rdfinish(sc, bp))
   1143 		rdustart(sc);
   1144 	rnd_add_uint32(&sc->rnd_source, bp->b_blkno);
   1145 }
   1146 
   1147 static int
   1148 rdstatus(struct rd_softc *sc)
   1149 {
   1150 	int c, s;
   1151 	uint8_t stat;
   1152 	int rv;
   1153 
   1154 	c = device_unit(device_parent(sc->sc_dev));
   1155 	s = sc->sc_slave;
   1156 	sc->sc_rsc.c_unit = C_SUNIT(sc->sc_punit);
   1157 	sc->sc_rsc.c_sram = C_SRAM;
   1158 	sc->sc_rsc.c_ram = C_RAM;
   1159 	sc->sc_rsc.c_cmd = C_STATUS;
   1160 	memset((void *)&sc->sc_stat, 0, sizeof(sc->sc_stat));
   1161 	rv = hpibsend(c, s, C_CMD, &sc->sc_rsc, sizeof(sc->sc_rsc));
   1162 	if (rv != sizeof(sc->sc_rsc)) {
   1163 #ifdef DEBUG
   1164 		if ((rddebug & RDB_STATUS) != 0)
   1165 			printf("rdstatus: send C_CMD failed %d != %d\n",
   1166 			    rv, sizeof(sc->sc_rsc));
   1167 #endif
   1168 		return 1;
   1169 	}
   1170 	rv = hpibrecv(c, s, C_EXEC, &sc->sc_stat, sizeof(sc->sc_stat));
   1171 	if (rv != sizeof(sc->sc_stat)) {
   1172 #ifdef DEBUG
   1173 		if ((rddebug & RDB_STATUS) != 0)
   1174 			printf("rdstatus: send C_EXEC failed %d != %d\n",
   1175 			    rv, sizeof(sc->sc_stat));
   1176 #endif
   1177 		return 1;
   1178 	}
   1179 	rv = hpibrecv(c, s, C_QSTAT, &stat, 1);
   1180 	if (rv != 1 || stat != 0) {
   1181 #ifdef DEBUG
   1182 		if ((rddebug & RDB_STATUS) != 0)
   1183 			printf("rdstatus: recv failed %d or bad stat %d\n",
   1184 			    rv, stat);
   1185 #endif
   1186 		return 1;
   1187 	}
   1188 	return 0;
   1189 }
   1190 
   1191 /*
   1192  * Deal with errors.
   1193  * Returns 1 if request should be restarted,
   1194  * 0 if we should just quietly give up.
   1195  */
   1196 static int
   1197 rderror(int unit)
   1198 {
   1199 	struct rd_softc *sc = device_lookup_private(&rd_cd, unit);
   1200 	struct rd_stat *sp;
   1201 	struct buf *bp;
   1202 	daddr_t hwbn, pbn;
   1203 
   1204 	if (rdstatus(sc) != 0) {
   1205 #ifdef DEBUG
   1206 		printf("%s: couldn't get status\n", device_xname(sc->sc_dev));
   1207 #endif
   1208 		rdreset(sc);
   1209 		return 1;
   1210 	}
   1211 	sp = &sc->sc_stat;
   1212 	if ((sp->c_fef & FEF_REXMT) != 0)
   1213 		return 1;
   1214 	if ((sp->c_fef & FEF_PF) != 0) {
   1215 		rdreset(sc);
   1216 		return 1;
   1217 	}
   1218 	/*
   1219 	 * Unit requests release for internal maintenance.
   1220 	 * We just delay awhile and try again later.  Use expontially
   1221 	 * increasing backoff ala ethernet drivers since we don't really
   1222 	 * know how long the maintenance will take.  With RDWAITC and
   1223 	 * RDRETRY as defined, the range is 1 to 32 seconds.
   1224 	 */
   1225 	if ((sp->c_fef & FEF_IMR) != 0) {
   1226 		int rdtimo = RDWAITC << sc->sc_errcnt;
   1227 #ifdef DEBUG
   1228 		printf("%s: internal maintenance, %d second timeout\n",
   1229 		    device_xname(sc->sc_dev), rdtimo);
   1230 		sc->sc_stats.rdtimeouts++;
   1231 #endif
   1232 		hpibfree(device_parent(sc->sc_dev), &sc->sc_hq);
   1233 		callout_reset(&sc->sc_restart_ch, rdtimo * hz, rdrestart, sc);
   1234 		return 0;
   1235 	}
   1236 	/*
   1237 	 * Only report error if we have reached the error reporting
   1238 	 * threshold.  By default, this will only report after the
   1239 	 * retry limit has been exceeded.
   1240 	 */
   1241 	if (sc->sc_errcnt < rderrthresh)
   1242 		return 1;
   1243 
   1244 	/*
   1245 	 * First conjure up the block number at which the error occurred.
   1246 	 * Note that not all errors report a block number, in that case
   1247 	 * we just use b_blkno.
   1248 	 */
   1249 	bp = bufq_peek(sc->sc_tab);
   1250 	pbn = sc->sc_dkdev.dk_label->d_partitions[rdpart(bp->b_dev)].p_offset;
   1251 	if ((sp->c_fef & FEF_CU) != 0 || (sp->c_fef & FEF_DR) != 0 ||
   1252 	    (sp->c_ief & IEF_RRMASK) != 0) {
   1253 		hwbn = RDBTOS(pbn + bp->b_blkno);
   1254 		pbn = bp->b_blkno;
   1255 	} else {
   1256 		hwbn = sp->c_blk;
   1257 		pbn = RDSTOB(hwbn) - pbn;
   1258 	}
   1259 	/*
   1260 	 * Now output a generic message suitable for badsect.
   1261 	 * Note that we don't use harderr cuz it just prints
   1262 	 * out b_blkno which is just the beginning block number
   1263 	 * of the transfer, not necessary where the error occurred.
   1264 	 */
   1265 	printf("%s%c: hard error sn%" PRId64 "\n", device_xname(sc->sc_dev),
   1266 	    'a' + rdpart(bp->b_dev), pbn);
   1267 	/*
   1268 	 * Now report the status as returned by the hardware with
   1269 	 * attempt at interpretation (unless debugging).
   1270 	 */
   1271 	printf("%s %s error:", device_xname(sc->sc_dev),
   1272 	    (bp->b_flags & B_READ) != 0 ? "read" : "write");
   1273 #ifdef DEBUG
   1274 	if (rddebug & RDB_ERROR) {
   1275 		/* status info */
   1276 		printf("\n    volume: %d, unit: %d\n",
   1277 		    (sp->c_vu >> 4) & 0xF, sp->c_vu & 0xF);
   1278 		rdprinterr("reject", sp->c_ref, err_reject);
   1279 		rdprinterr("fault", sp->c_fef, err_fault);
   1280 		rdprinterr("access", sp->c_aef, err_access);
   1281 		rdprinterr("info", sp->c_ief, err_info);
   1282 		printf("    block: %lld, P1-P10: ", hwbn);
   1283 		printf("0x%x", *(uint32_t *)&sp->c_raw[0]);
   1284 		printf("0x%x", *(uint32_t *)&sp->c_raw[4]);
   1285 		printf("0x%x\n", *(uint16_t *)&sp->c_raw[8]);
   1286 		/* command */
   1287 		printf("    ioc: ");
   1288 		printf("0x%x", *(uint32_t *)&sc->sc_ioc.c_pad);
   1289 		printf("0x%x", *(uint16_t *)&sc->sc_ioc.c_hiaddr);
   1290 		printf("0x%x", *(uint32_t *)&sc->sc_ioc.c_addr);
   1291 		printf("0x%x", *(uint16_t *)&sc->sc_ioc.c_nop2);
   1292 		printf("0x%x", *(uint32_t *)&sc->sc_ioc.c_len);
   1293 		printf("0x%x\n", *(uint16_t *)&sc->sc_ioc.c_cmd);
   1294 		return 1;
   1295 	}
   1296 #endif
   1297 	printf(" v%d u%d, R0x%x F0x%x A0x%x I0x%x\n",
   1298 	    (sp->c_vu >> 4) & 0xF, sp->c_vu & 0xF,
   1299 	    sp->c_ref, sp->c_fef, sp->c_aef, sp->c_ief);
   1300 	printf("P1-P10: ");
   1301 	printf("0x%x", *(uint32_t *)&sp->c_raw[0]);
   1302 	printf("0x%x", *(uint32_t *)&sp->c_raw[4]);
   1303 	printf("0x%x\n", *(uint16_t *)&sp->c_raw[8]);
   1304 	return 1;
   1305 }
   1306 
   1307 static int
   1308 rdread(dev_t dev, struct uio *uio, int flags)
   1309 {
   1310 
   1311 	return physio(rdstrategy, NULL, dev, B_READ, minphys, uio);
   1312 }
   1313 
   1314 static int
   1315 rdwrite(dev_t dev, struct uio *uio, int flags)
   1316 {
   1317 
   1318 	return physio(rdstrategy, NULL, dev, B_WRITE, minphys, uio);
   1319 }
   1320 
   1321 static int
   1322 rdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
   1323 {
   1324 	struct rd_softc *sc = device_lookup_private(&rd_cd, rdunit(dev));
   1325 	struct disklabel *lp = sc->sc_dkdev.dk_label;
   1326 	int error, flags;
   1327 
   1328 	error = disk_ioctl(&sc->sc_dkdev, dev, cmd, data, flag, l);
   1329 	if (error != EPASSTHROUGH)
   1330 		return error;
   1331 
   1332 	switch (cmd) {
   1333 	case DIOCWLABEL:
   1334 		if ((flag & FWRITE) == 0)
   1335 			return EBADF;
   1336 		if (*(int *)data)
   1337 			sc->sc_flags |= RDF_WLABEL;
   1338 		else
   1339 			sc->sc_flags &= ~RDF_WLABEL;
   1340 		return 0;
   1341 
   1342 	case DIOCSDINFO:
   1343 		if ((flag & FWRITE) == 0)
   1344 			return EBADF;
   1345 		return setdisklabel(lp, (struct disklabel *)data,
   1346 		    (sc->sc_flags & RDF_WLABEL) ? 0 : sc->sc_dkdev.dk_openmask,
   1347 		    NULL);
   1348 
   1349 	case DIOCWDINFO:
   1350 		if ((flag & FWRITE) == 0)
   1351 			return EBADF;
   1352 		error = setdisklabel(lp, (struct disklabel *)data,
   1353 		    (sc->sc_flags & RDF_WLABEL) ? 0 : sc->sc_dkdev.dk_openmask,
   1354 		    NULL);
   1355 		if (error != 0)
   1356 			return error;
   1357 		flags = sc->sc_flags;
   1358 		sc->sc_flags = RDF_ALIVE | RDF_WLABEL;
   1359 		error = writedisklabel(rdlabdev(dev), rdstrategy, lp, NULL);
   1360 		sc->sc_flags = flags;
   1361 		return error;
   1362 
   1363 	case DIOCGDEFLABEL:
   1364 		rdgetdefaultlabel(sc, (struct disklabel *)data);
   1365 		return 0;
   1366 
   1367 	case DIOCCACHESYNC:
   1368 		/* no cache to be flushed but required to appease raid(4) */
   1369 		return 0;
   1370 	}
   1371 	return EINVAL;
   1372 }
   1373 
   1374 static void
   1375 rdgetdefaultlabel(struct rd_softc *sc, struct disklabel *lp)
   1376 {
   1377 	int type = sc->sc_type;
   1378 
   1379 	memset((void *)lp, 0, sizeof(struct disklabel));
   1380 
   1381 	lp->d_type = DKTYPE_HPIB;
   1382 	lp->d_secsize = DEV_BSIZE;
   1383 	lp->d_nsectors = rdidentinfo[type].ri_nbpt;
   1384 	lp->d_ntracks = rdidentinfo[type].ri_ntpc;
   1385 	lp->d_ncylinders = rdidentinfo[type].ri_ncyl;
   1386 	lp->d_secperunit = rdidentinfo[type].ri_nblocks;
   1387 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
   1388 
   1389 	strlcpy(lp->d_typename, rdidentinfo[type].ri_desc,
   1390 	    sizeof(lp->d_typename));
   1391 	strlcpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
   1392 	lp->d_rpm = 3000;
   1393 	lp->d_interleave = 1;
   1394 	lp->d_flags = 0;
   1395 
   1396 	lp->d_partitions[RAW_PART].p_offset = 0;
   1397 	lp->d_partitions[RAW_PART].p_size =
   1398 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
   1399 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
   1400 	lp->d_npartitions = RAW_PART + 1;
   1401 
   1402 	lp->d_magic = DISKMAGIC;
   1403 	lp->d_magic2 = DISKMAGIC;
   1404 	lp->d_checksum = dkcksum(lp);
   1405 }
   1406 
   1407 static int
   1408 rdsize(dev_t dev)
   1409 {
   1410 	struct rd_softc *sc;
   1411 	int psize, didopen = 0;
   1412 
   1413 	sc = device_lookup_private(&rd_cd, rdunit(dev));
   1414 	if (sc == NULL)
   1415 		return ENXIO;
   1416 
   1417 	if ((sc->sc_flags & RDF_ALIVE) == 0)
   1418 		return ENXIO;
   1419 
   1420 	/*
   1421 	 * We get called very early on (via swapconf)
   1422 	 * without the device being open so we may need
   1423 	 * to handle it here.
   1424 	 */
   1425 	if (sc->sc_dkdev.dk_openmask == 0) {
   1426 		if (rdopen(dev, FREAD | FWRITE, S_IFBLK, NULL))
   1427 			return -1;
   1428 		didopen = 1;
   1429 	}
   1430 	psize = sc->sc_dkdev.dk_label->d_partitions[rdpart(dev)].p_size *
   1431 	    (sc->sc_dkdev.dk_label->d_secsize / DEV_BSIZE);
   1432 	if (didopen)
   1433 		(void)rdclose(dev, FREAD | FWRITE, S_IFBLK, NULL);
   1434 	return psize;
   1435 }
   1436 
   1437 #ifdef DEBUG
   1438 static void
   1439 rdprinterr(const char *str, short err, const char **tab)
   1440 {
   1441 	int i;
   1442 	int printed;
   1443 
   1444 	if (err == 0)
   1445 		return;
   1446 	printf("    %s error %d field:", str, err);
   1447 	printed = 0;
   1448 	for (i = 0; i < 16; i++)
   1449 		if ((err & (0x8000 >> i)) != 0)
   1450 			printf("%s%s", printed++ ? " + " : " ", tab[i]);
   1451 	printf("\n");
   1452 }
   1453 #endif
   1454 
   1455 static int rddoingadump;	/* simple mutex */
   1456 
   1457 /*
   1458  * Non-interrupt driven, non-DMA dump routine.
   1459  */
   1460 static int
   1461 rddump(dev_t dev, daddr_t blkno, void *va, size_t size)
   1462 {
   1463 	int sectorsize;		/* size of a disk sector */
   1464 	int nsects;		/* number of sectors in partition */
   1465 	int sectoff;		/* sector offset of partition */
   1466 	int totwrt;		/* total number of sectors left to write */
   1467 	int nwrt;		/* current number of sectors to write */
   1468 	int part;
   1469 	int ctlr, slave;
   1470 	struct rd_softc *sc;
   1471 	struct disklabel *lp;
   1472 	char stat;
   1473 
   1474 	/* Check for recursive dump; if so, punt. */
   1475 	if (rddoingadump)
   1476 		return EFAULT;
   1477 	rddoingadump = 1;
   1478 
   1479 	/* Decompose unit and partition. */
   1480 	part = rdpart(dev);
   1481 
   1482 	/* Make sure dump device is ok. */
   1483 	sc = device_lookup_private(&rd_cd, rdunit(dev));
   1484 	if (sc == NULL)
   1485 		return ENXIO;
   1486 
   1487 	if ((sc->sc_flags & RDF_ALIVE) == 0)
   1488 		return ENXIO;
   1489 
   1490 	ctlr = device_unit(device_parent(sc->sc_dev));
   1491 	slave = sc->sc_slave;
   1492 
   1493 	/*
   1494 	 * Convert to disk sectors.  Request must be a multiple of size.
   1495 	 */
   1496 	lp = sc->sc_dkdev.dk_label;
   1497 	sectorsize = lp->d_secsize;
   1498 	if ((size % sectorsize) != 0)
   1499 		return EFAULT;
   1500 	totwrt = size / sectorsize;
   1501 	blkno = dbtob(blkno) / sectorsize;	/* blkno in DEV_BSIZE units */
   1502 
   1503 	nsects = lp->d_partitions[part].p_size;
   1504 	sectoff = lp->d_partitions[part].p_offset;
   1505 
   1506 	/* Check transfer bounds against partition size. */
   1507 	if ((blkno < 0) || (blkno + totwrt) > nsects)
   1508 		return EINVAL;
   1509 
   1510 	/* Offset block number to start of partition. */
   1511 	blkno += sectoff;
   1512 
   1513 	while (totwrt > 0) {
   1514 		nwrt = totwrt;		/* XXX */
   1515 #ifndef RD_DUMP_NOT_TRUSTED
   1516 		/*
   1517 		 * Fill out and send HPIB command.
   1518 		 */
   1519 		sc->sc_ioc.c_unit = C_SUNIT(sc->sc_punit);
   1520 		sc->sc_ioc.c_volume = C_SVOL(0);
   1521 		sc->sc_ioc.c_saddr = C_SADDR;
   1522 		sc->sc_ioc.c_hiaddr = 0;
   1523 		sc->sc_ioc.c_addr = RDBTOS(blkno);
   1524 		sc->sc_ioc.c_nop2 = C_NOP;
   1525 		sc->sc_ioc.c_slen = C_SLEN;
   1526 		sc->sc_ioc.c_len = nwrt * sectorsize;
   1527 		sc->sc_ioc.c_cmd = C_WRITE;
   1528 		hpibsend(ctlr, slave, C_CMD, &sc->sc_ioc.c_unit,
   1529 		    sizeof(sc->sc_ioc) - 2);
   1530 		if (hpibswait(ctlr, slave))
   1531 			return EIO;
   1532 
   1533 		/*
   1534 		 * Send the data.
   1535 		 */
   1536 		hpibsend(ctlr, slave, C_EXEC, va, nwrt * sectorsize);
   1537 		(void)hpibswait(ctlr, slave);
   1538 		hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
   1539 		if (stat)
   1540 			return EIO;
   1541 #else /* RD_DUMP_NOT_TRUSTED */
   1542 		/* Let's just talk about this first... */
   1543 		printf("%s: dump addr %p, blk %d\n", device_xname(sc->sc_dev),
   1544 		    va, blkno);
   1545 		delay(500 * 1000);	/* half a second */
   1546 #endif /* RD_DUMP_NOT_TRUSTED */
   1547 
   1548 		/* update block count */
   1549 		totwrt -= nwrt;
   1550 		blkno += nwrt;
   1551 		va = (uint8_t *)va + sectorsize * nwrt;
   1552 	}
   1553 	rddoingadump = 0;
   1554 	return 0;
   1555 }
   1556