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