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