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