Home | History | Annotate | Line # | Download | only in gpib
rd.c revision 1.30.6.3
      1 /*	$NetBSD: rd.c,v 1.30.6.3 2014/08/20 00:03:37 tls Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1996-2003 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.30.6.3 2014/08/20 00:03:37 tls Exp $");
     76 
     77 #include <sys/param.h>
     78 #include <sys/systm.h>
     79 #include <sys/buf.h>
     80 #include <sys/bufq.h>
     81 #include <sys/callout.h>
     82 #include <sys/conf.h>
     83 #include <sys/device.h>
     84 #include <sys/disk.h>
     85 #include <sys/disklabel.h>
     86 #include <sys/endian.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/rnd.h>
     93 
     94 #include <dev/gpib/gpibvar.h>
     95 #include <dev/gpib/cs80busvar.h>
     96 
     97 #include <dev/gpib/rdreg.h>
     98 
     99 #ifdef DEBUG
    100 int	rddebug = 0xff;
    101 #define RDB_FOLLOW	0x01
    102 #define RDB_STATUS	0x02
    103 #define RDB_IDENT	0x04
    104 #define RDB_IO		0x08
    105 #define RDB_ASYNC	0x10
    106 #define RDB_ERROR	0x80
    107 #define DPRINTF(mask, str)	if (rddebug & (mask)) printf str
    108 #else
    109 #define DPRINTF(mask, str)	/* nothing */
    110 #endif
    111 
    112 struct	rd_softc {
    113 	device_t sc_dev;
    114 	gpib_chipset_tag_t sc_ic;
    115 	gpib_handle_t sc_hdl;
    116 
    117 	struct	disk sc_dk;
    118 
    119 	int	sc_slave;		/* GPIB slave */
    120 	int	sc_punit;		/* physical unit on slave */
    121 
    122 	int	sc_flags;
    123 #define	RDF_ALIVE	0x01
    124 #define	RDF_SEEK	0x02
    125 #define RDF_SWAIT	0x04
    126 #define RDF_OPENING	0x08
    127 #define RDF_CLOSING	0x10
    128 #define RDF_WANTED	0x20
    129 #define RDF_WLABEL	0x40
    130 
    131 	u_int16_t sc_type;
    132 	u_int8_t *sc_addr;
    133 	int	sc_resid;
    134 	struct	rd_iocmd sc_ioc;
    135 	struct	bufq_state *sc_tab;
    136 	int	sc_active;
    137 	int	sc_errcnt;
    138 
    139 	struct	callout sc_restart_ch;
    140 
    141 	krndsource_t rnd_source;
    142 };
    143 
    144 #define RDUNIT(dev)			DISKUNIT(dev)
    145 #define RDPART(dev)			DISKPART(dev)
    146 #define RDMAKEDEV(maj, unit, part)	MAKEDISKDEV(maj, unit, part)
    147 #define RDLABELDEV(dev)	(RDMAKEDEV(major(dev), RDUNIT(dev), RAW_PART))
    148 
    149 #define	RDRETRY		5
    150 #define RDWAITC		1	/* min time for timeout in seconds */
    151 
    152 int	rderrthresh = RDRETRY-1;	/* when to start reporting errors */
    153 
    154 /*
    155  * Misc. HW description, indexed by sc_type.
    156  * Used for mapping 256-byte sectors for 512-byte sectors
    157  */
    158 const struct rdidentinfo {
    159 	u_int16_t ri_hwid;		/* 2 byte HW id */
    160 	u_int16_t ri_maxunum;		/* maximum allowed unit number */
    161 	const char *ri_desc;		/* drive type description */
    162 	int	ri_nbpt;		/* DEV_BSIZE blocks per track */
    163 	int	ri_ntpc;		/* tracks per cylinder */
    164 	int	ri_ncyl;		/* cylinders per unit */
    165 	int	ri_nblocks;		/* DEV_BSIZE blocks on disk */
    166 } rdidentinfo[] = {
    167 	{ RD7946AID,	0,	"7945A",	NRD7945ABPT,
    168 	  NRD7945ATRK,	968,	 108416 },
    169 
    170 	{ RD9134DID,	1,	"9134D",	NRD9134DBPT,
    171 	  NRD9134DTRK,	303,	  29088 },
    172 
    173 	{ RD9134LID,	1,	"9122S",	NRD9122SBPT,
    174 	  NRD9122STRK,	77,	   1232 },
    175 
    176 	{ RD7912PID,	0,	"7912P",	NRD7912PBPT,
    177 	  NRD7912PTRK,	572,	 128128 },
    178 
    179 	{ RD7914PID,	0,	"7914P",	NRD7914PBPT,
    180 	  NRD7914PTRK,	1152,	 258048 },
    181 
    182 	{ RD7958AID,	0,	"7958A",	NRD7958ABPT,
    183 	  NRD7958ATRK,	1013,	 255276 },
    184 
    185 	{ RD7957AID,	0,	"7957A",	NRD7957ABPT,
    186 	  NRD7957ATRK,	1036,	 159544 },
    187 
    188 	{ RD7933HID,	0,	"7933H",	NRD7933HBPT,
    189 	  NRD7933HTRK,	1321,	 789958 },
    190 
    191 	{ RD9134LID,	1,	"9134L",	NRD9134LBPT,
    192 	  NRD9134LTRK,	973,	  77840 },
    193 
    194 	{ RD7936HID,	0,	"7936H",	NRD7936HBPT,
    195 	  NRD7936HTRK,	698,	 600978 },
    196 
    197 	{ RD7937HID,	0,	"7937H",	NRD7937HBPT,
    198 	  NRD7937HTRK,	698,	1116102 },
    199 
    200 	{ RD7914CTID,	0,	"7914CT",	NRD7914PBPT,
    201 	  NRD7914PTRK,	1152,	 258048 },
    202 
    203 	{ RD7946AID,	0,	"7946A",	NRD7945ABPT,
    204 	  NRD7945ATRK,	968,	 108416 },
    205 
    206 	{ RD9134LID,	1,	"9122D",	NRD9122SBPT,
    207 	  NRD9122STRK,	77,	   1232 },
    208 
    209 	{ RD7957BID,	0,	"7957B",	NRD7957BBPT,
    210 	  NRD7957BTRK,	1269,	 159894 },
    211 
    212 	{ RD7958BID,	0,	"7958B",	NRD7958BBPT,
    213 	  NRD7958BTRK,	786,	 297108 },
    214 
    215 	{ RD7959BID,	0,	"7959B",	NRD7959BBPT,
    216 	  NRD7959BTRK,	1572,	 594216 },
    217 
    218 	{ RD2200AID,	0,	"2200A",	NRD2200ABPT,
    219 	  NRD2200ATRK,	1449,	 654948 },
    220 
    221 	{ RD2203AID,	0,	"2203A",	NRD2203ABPT,
    222 	  NRD2203ATRK,	1449,	1309896 }
    223 };
    224 int numrdidentinfo = sizeof(rdidentinfo) / sizeof(rdidentinfo[0]);
    225 
    226 int	rdlookup(int, int, int);
    227 int	rdgetinfo(struct rd_softc *);
    228 void	rdrestart(void *);
    229 struct buf *rdfinish(struct rd_softc *, struct buf *);
    230 
    231 void	rdgetcompatlabel(struct rd_softc *, struct disklabel *);
    232 void	rdgetdefaultlabel(struct rd_softc *, struct disklabel *);
    233 void	rdrestart(void *);
    234 void	rdustart(struct rd_softc *);
    235 struct buf *rdfinish(struct rd_softc *, struct buf *);
    236 void	rdcallback(void *, int);
    237 void	rdstart(struct rd_softc *);
    238 void	rdintr(struct rd_softc *);
    239 int	rderror(struct rd_softc *);
    240 
    241 int	rdmatch(device_t, cfdata_t, void *);
    242 void	rdattach(device_t, device_t, void *);
    243 
    244 CFATTACH_DECL_NEW(rd, sizeof(struct rd_softc),
    245 	rdmatch, rdattach, NULL, NULL);
    246 
    247 
    248 dev_type_open(rdopen);
    249 dev_type_close(rdclose);
    250 dev_type_read(rdread);
    251 dev_type_write(rdwrite);
    252 dev_type_ioctl(rdioctl);
    253 dev_type_strategy(rdstrategy);
    254 dev_type_dump(rddump);
    255 dev_type_size(rdsize);
    256 
    257 const struct bdevsw rd_bdevsw = {
    258 	.d_open = rdopen,
    259 	.d_close = rdclose,
    260 	.d_strategy = rdstrategy,
    261 	.d_ioctl = rdioctl,
    262 	.d_dump = rddump,
    263 	.d_psize = rdsize,
    264 	.d_discard = nodiscard,
    265 	.d_flag = D_DISK
    266 };
    267 
    268 const struct cdevsw rd_cdevsw = {
    269 	.d_open = rdopen,
    270 	.d_close = rdclose,
    271 	.d_read = rdread,
    272 	.d_write = rdwrite,
    273 	.d_ioctl = rdioctl,
    274 	.d_stop = nostop,
    275 	.d_tty = notty,
    276 	.d_poll = nopoll,
    277 	.d_mmap = nommap,
    278 	.d_kqfilter = nokqfilter,
    279 	.d_discard = nodiscard,
    280 	.d_flag = D_DISK
    281 };
    282 
    283 extern struct cfdriver rd_cd;
    284 
    285 static void
    286 rdminphys(struct buf *bp)
    287 {
    288     struct rd_softc *sc = device_lookup_private(&rd_cd, RDUNIT(bp->b_dev));
    289 
    290     long xmax;
    291     xmax = rd->sc_dev->dv_maxphys;
    292     if (bp->b_bcount > xmax)
    293         bp->b_bcount = xmax;
    294 }
    295 
    296 const struct dkdriver rd_dkdriver = { rdstrategy, rdminphys };
    297 
    298 int
    299 rdlookup(int id, int slave, int punit)
    300 {
    301 	int i;
    302 
    303 	for (i = 0; i < numrdidentinfo; i++) {
    304 		if (rdidentinfo[i].ri_hwid == id)
    305 			break;
    306 	}
    307 	if (i == numrdidentinfo || punit > rdidentinfo[i].ri_maxunum)
    308 		return (-1);
    309 	return (i);
    310 }
    311 
    312 int
    313 rdmatch(device_t parent, cfdata_t match, void *aux)
    314 {
    315 	struct cs80bus_attach_args *ca = aux;
    316 
    317 	if (rdlookup(ca->ca_id, ca->ca_slave, ca->ca_punit) < 0)
    318 		return (0);
    319 	return (1);
    320 }
    321 
    322 void
    323 rdattach(device_t parent, device_t self, void *aux)
    324 {
    325 	struct rd_softc *sc = device_private(self);
    326 	struct cs80bus_attach_args *ca = aux;
    327 	struct cs80_description csd;
    328 	char name[7];
    329 	int type, i, n;
    330 
    331 	sc->sc_dev = self;
    332 	sc->sc_ic = ca->ca_ic;
    333 	sc->sc_slave = ca->ca_slave;
    334 	sc->sc_punit = ca->ca_punit;
    335 
    336 	if ((type = rdlookup(ca->ca_id, ca->ca_slave, ca->ca_punit)) < 0)
    337 		return;
    338 
    339 	if (cs80reset(parent, sc->sc_slave, sc->sc_punit)) {
    340 		aprint_normal("\n");
    341 		aprint_error_dev(sc->sc_dev, "can't reset device\n");
    342 		return;
    343 	}
    344 
    345 	if (cs80describe(parent, sc->sc_slave, sc->sc_punit, &csd)) {
    346 		aprint_normal("\n");
    347 		aprint_error_dev(sc->sc_dev, "didn't respond to describe command\n");
    348 		return;
    349 	}
    350 	memset(name, 0, sizeof(name));
    351 	for (i=0, n=0; i<3; i++) {
    352 		name[n++] = (csd.d_name[i] >> 4) + '0';
    353 		name[n++] = (csd.d_name[i] & 0x0f) + '0';
    354 	}
    355 
    356 #ifdef DEBUG
    357 	if (rddebug & RDB_IDENT) {
    358 		printf("\n%s: name: ('%s')\n",
    359 		    device_xname(sc->sc_dev), name);
    360 		printf("  iuw %x, maxxfr %d, ctype %d\n",
    361 		    csd.d_iuw, csd.d_cmaxxfr, csd.d_ctype);
    362 		printf("  utype %d, bps %d, blkbuf %d, burst %d, blktime %d\n",
    363 		    csd.d_utype, csd.d_sectsize,
    364 		    csd.d_blkbuf, csd.d_burstsize, csd.d_blocktime);
    365 		printf("  avxfr %d, ort %d, atp %d, maxint %d, fv %x, rv %x\n",
    366 		    csd.d_uavexfr, csd.d_retry, csd.d_access,
    367 		    csd.d_maxint, csd.d_fvbyte, csd.d_rvbyte);
    368 		printf("  maxcyl/head/sect %d/%d/%d, maxvsect %d, inter %d\n",
    369 		    csd.d_maxcylhead >> 8, csd.d_maxcylhead & 0xff,
    370 		    csd.d_maxsect, csd.d_maxvsectl, csd.d_interleave);
    371 		printf("%s", device_xname(sc->sc_dev));
    372 	}
    373 #endif
    374 
    375 	/*
    376 	 * Take care of a couple of anomolies:
    377 	 * 1. 7945A and 7946A both return same HW id
    378 	 * 2. 9122S and 9134D both return same HW id
    379 	 * 3. 9122D and 9134L both return same HW id
    380 	 */
    381 	switch (ca->ca_id) {
    382 	case RD7946AID:
    383 		if (memcmp(name, "079450", 6) == 0)
    384 			type = RD7945A;
    385 		else
    386 			type = RD7946A;
    387 		break;
    388 
    389 	case RD9134LID:
    390 		if (memcmp(name, "091340", 6) == 0)
    391 			type = RD9134L;
    392 		else
    393 			type = RD9122D;
    394 		break;
    395 
    396 	case RD9134DID:
    397 		if (memcmp(name, "091220", 6) == 0)
    398 			type = RD9122S;
    399 		else
    400 			type = RD9134D;
    401 		break;
    402 	}
    403 
    404 	sc->sc_type = type;
    405 
    406 	/*
    407 	 * XXX We use DEV_BSIZE instead of the sector size value pulled
    408 	 * XXX off the driver because all of this code assumes 512 byte
    409 	 * XXX blocks.  ICK!
    410 	 */
    411 	printf(": %s\n", rdidentinfo[type].ri_desc);
    412 	printf("%s: %d cylinders, %d heads, %d blocks, %d bytes/block\n",
    413 	    device_xname(sc->sc_dev), rdidentinfo[type].ri_ncyl,
    414 	    rdidentinfo[type].ri_ntpc, rdidentinfo[type].ri_nblocks,
    415 	    DEV_BSIZE);
    416 
    417 	bufq_alloc(&sc->sc_tab, "fcfs", 0);
    418 
    419 	/*
    420 	 * Initialize and attach the disk structure.
    421 	 */
    422 	memset(&sc->sc_dk, 0, sizeof(sc->sc_dk));
    423 	disk_init(&sc->sc_dk, device_xname(sc->sc_dev), rd_dkdriver);
    424 	disk_attach(&sc->sc_dk);
    425 
    426 	callout_init(&sc->sc_restart_ch, 0);
    427 
    428 	if (gpibregister(sc->sc_ic, sc->sc_slave, rdcallback, sc,
    429 	    &sc->sc_hdl)) {
    430 		aprint_error_dev(sc->sc_dev, "can't register callback\n");
    431 		return;
    432 	}
    433 
    434 	sc->sc_flags = RDF_ALIVE;
    435 #ifdef DEBUG
    436 	/* always report errors */
    437 	if (rddebug & RDB_ERROR)
    438 		rderrthresh = 0;
    439 #endif
    440 	/*
    441 	 * attach the device into the random source list
    442 	 */
    443 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
    444 			  RND_TYPE_DISK, RND_FLAG_DEFAULT);
    445 }
    446 
    447 /*
    448  * Read or construct a disklabel
    449  */
    450 int
    451 rdgetinfo(struct rd_softc *sc)
    452 {
    453 	struct disklabel *lp = sc->sc_dk.dk_label;
    454 	struct partition *pi;
    455 	const char *msg;
    456 
    457 	memset(sc->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
    458 
    459 	rdgetdefaultlabel(sc, lp);
    460 
    461 	/*
    462 	 * Call the generic disklabel extraction routine
    463 	 */
    464 	msg = readdisklabel(RDMAKEDEV(0, device_unit(sc->sc_dev), RAW_PART),
    465 	    rdstrategy, lp, NULL);
    466 	if (msg == NULL)
    467 		return (0);
    468 
    469 	pi = lp->d_partitions;
    470 	printf("%s: WARNING: %s\n", device_xname(sc->sc_dev), msg);
    471 
    472 	pi[RAW_PART].p_size = rdidentinfo[sc->sc_type].ri_nblocks;
    473 	lp->d_npartitions = RAW_PART+1;
    474 	pi[0].p_size = 0;
    475 
    476 	return (0);
    477 }
    478 
    479 int
    480 rdopen(dev_t dev, int flags, int mode, struct lwp *l)
    481 {
    482 	struct rd_softc *sc;
    483 	int error, mask, part;
    484 
    485 	sc = device_lookup_private(&rd_cd, RDUNIT(dev));
    486 	if (sc == NULL || (sc->sc_flags & RDF_ALIVE) ==0)
    487 		return (ENXIO);
    488 
    489 	/*
    490 	 * Wait for any pending opens/closes to complete
    491 	 */
    492 	while (sc->sc_flags & (RDF_OPENING | RDF_CLOSING))
    493 		(void) tsleep(sc, PRIBIO, "rdopen", 0);
    494 
    495 	/*
    496 	 * On first open, get label and partition info.
    497 	 * We may block reading the label, so be careful
    498 	 * to stop any other opens.
    499 	 */
    500 	if (sc->sc_dk.dk_openmask == 0) {
    501 		sc->sc_flags |= RDF_OPENING;
    502 		error = rdgetinfo(sc);
    503 		sc->sc_flags &= ~RDF_OPENING;
    504 		wakeup((void *)sc);
    505 		if (error)
    506 			return (error);
    507 	}
    508 
    509 	part = RDPART(dev);
    510 	mask = 1 << part;
    511 
    512 	/* Check that the partition exists. */
    513 	if (part != RAW_PART && (part > sc->sc_dk.dk_label->d_npartitions ||
    514 	    sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED))
    515 		return (ENXIO);
    516 
    517 	/* Ensure only one open at a time. */
    518 	switch (mode) {
    519 	case S_IFCHR:
    520 		sc->sc_dk.dk_copenmask |= mask;
    521 		break;
    522 	case S_IFBLK:
    523 		sc->sc_dk.dk_bopenmask |= mask;
    524 		break;
    525 	}
    526 	sc->sc_dk.dk_openmask =
    527 	    sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
    528 
    529 	return (0);
    530 }
    531 
    532 int
    533 rdclose(dev_t dev, int flag, int mode, struct lwp *l)
    534 {
    535 	struct rd_softc *sc;
    536 	struct disk *dk;
    537 	int mask, s;
    538 
    539 	sc = device_lookup_private(&rd_cd, RDUNIT(dev));
    540 	if (sc == NULL)
    541 		return (ENXIO);
    542 
    543 	dk = &sc->sc_dk;
    544 
    545 	mask = 1 << RDPART(dev);
    546 	if (mode == S_IFCHR)
    547 		dk->dk_copenmask &= ~mask;
    548 	else
    549 		dk->dk_bopenmask &= ~mask;
    550 	dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
    551 	/*
    552 	 * On last close, we wait for all activity to cease since
    553 	 * the label/parition info will become invalid.  Since we
    554 	 * might sleep, we must block any opens while we are here.
    555 	 * Note we don't have to about other closes since we know
    556 	 * we are the last one.
    557 	 */
    558 	if (dk->dk_openmask == 0) {
    559 		sc->sc_flags |= RDF_CLOSING;
    560 		s = splbio();
    561 		while (sc->sc_active) {
    562 			sc->sc_flags |= RDF_WANTED;
    563 			(void) tsleep(&sc->sc_tab, PRIBIO, "rdclose", 0);
    564 		}
    565 		splx(s);
    566 		sc->sc_flags &= ~(RDF_CLOSING | RDF_WLABEL);
    567 		wakeup((void *)sc);
    568 	}
    569 	return (0);
    570 }
    571 
    572 void
    573 rdstrategy(struct buf *bp)
    574 {
    575 	struct rd_softc *sc;
    576 	struct partition *pinfo;
    577 	daddr_t bn;
    578 	int sz, s;
    579 	int offset;
    580 
    581 	sc = device_lookup_private(&rd_cd, RDUNIT(bp->b_dev));
    582 
    583 	DPRINTF(RDB_FOLLOW,
    584 	    ("rdstrategy(%p): dev %" PRIx64 ", bn %" PRId64 ", bcount %d, %c\n",
    585 	    bp, bp->b_dev, bp->b_blkno, bp->b_bcount,
    586 	    (bp->b_flags & B_READ) ? 'R' : 'W'));
    587 
    588 	bn = bp->b_blkno;
    589 	sz = howmany(bp->b_bcount, DEV_BSIZE);
    590 	pinfo = &sc->sc_dk.dk_label->d_partitions[RDPART(bp->b_dev)];
    591 
    592 	/* Don't perform partition translation on RAW_PART. */
    593 	offset = (RDPART(bp->b_dev) == RAW_PART) ? 0 : pinfo->p_offset;
    594 
    595 	if (RDPART(bp->b_dev) != RAW_PART) {
    596 		/*
    597 		 * XXX This block of code belongs in
    598 		 * XXX bounds_check_with_label()
    599 		 */
    600 
    601 		if (bn < 0 || bn + sz > pinfo->p_size) {
    602 			sz = pinfo->p_size - bn;
    603 			if (sz == 0) {
    604 				bp->b_resid = bp->b_bcount;
    605 				goto done;
    606 			}
    607 			if (sz < 0) {
    608 				bp->b_error = EINVAL;
    609 				goto done;
    610 			}
    611 			bp->b_bcount = dbtob(sz);
    612 		}
    613 		/*
    614 		 * Check for write to write protected label
    615 		 */
    616 		if (bn + offset <= LABELSECTOR &&
    617 #if LABELSECTOR != 0
    618 		    bn + offset + sz > LABELSECTOR &&
    619 #endif
    620 		    !(bp->b_flags & B_READ) && !(sc->sc_flags & RDF_WLABEL)) {
    621 			bp->b_error = EROFS;
    622 			goto done;
    623 		}
    624 	}
    625 	bp->b_rawblkno = bn + offset;
    626 	s = splbio();
    627 	bufq_put(sc->sc_tab, bp);
    628 	if (sc->sc_active == 0) {
    629 		sc->sc_active = 1;
    630 		rdustart(sc);
    631 	}
    632 	splx(s);
    633 	return;
    634 done:
    635 	biodone(bp);
    636 }
    637 
    638 /*
    639  * Called from timeout() when handling maintenance releases
    640  * callout from timeouts
    641  */
    642 void
    643 rdrestart(void *arg)
    644 {
    645 	int s = splbio();
    646 	rdustart((struct rd_softc *)arg);
    647 	splx(s);
    648 }
    649 
    650 
    651 /* called by rdstrategy() to start a block transfer */
    652 /* called by rdrestart() when handingly timeouts */
    653 /* called by rdintr() */
    654 void
    655 rdustart(struct rd_softc *sc)
    656 {
    657 	struct buf *bp;
    658 
    659 	bp = bufq_peek(sc->sc_tab);
    660 	sc->sc_addr = bp->b_data;
    661 	sc->sc_resid = bp->b_bcount;
    662 	if (gpibrequest(sc->sc_ic, sc->sc_hdl))
    663 		rdstart(sc);
    664 }
    665 
    666 struct buf *
    667 rdfinish(struct rd_softc *sc, struct buf *bp)
    668 {
    669 
    670 	sc->sc_errcnt = 0;
    671 	(void)bufq_get(sc->sc_tab);
    672 	bp->b_resid = 0;
    673 	biodone(bp);
    674 	gpibrelease(sc->sc_ic, sc->sc_hdl);
    675 	if ((bp = bufq_peek(sc->sc_tab)) != NULL)
    676 		return (bp);
    677 	sc->sc_active = 0;
    678 	if (sc->sc_flags & RDF_WANTED) {
    679 		sc->sc_flags &= ~RDF_WANTED;
    680 		wakeup((void *)&sc->sc_tab);
    681 	}
    682 	return (NULL);
    683 }
    684 
    685 void
    686 rdcallback(void *v, int action)
    687 {
    688 	struct rd_softc *sc = v;
    689 
    690 	DPRINTF(RDB_FOLLOW, ("rdcallback: v=%p, action=%d\n", v, action));
    691 
    692 	switch (action) {
    693 	case GPIBCBF_START:
    694 		rdstart(sc);
    695 		break;
    696 	case GPIBCBF_INTR:
    697 		rdintr(sc);
    698 		break;
    699 #ifdef DEBUG
    700 	default:
    701 		DPRINTF(RDB_ERROR, ("rdcallback: unknown action %d\n",
    702 		    action));
    703 		break;
    704 #endif
    705 	}
    706 }
    707 
    708 
    709 /* called from rdustart() to start a transfer */
    710 /* called from gpib interface as the initiator */
    711 void
    712 rdstart(struct rd_softc *sc)
    713 {
    714 	struct buf *bp = bufq_peek(sc->sc_tab);
    715 	int slave, punit;
    716 
    717 	slave = sc->sc_slave;
    718 	punit = sc->sc_punit;
    719 
    720 	DPRINTF(RDB_FOLLOW, ("rdstart(%s): bp %p, %c\n",
    721 	    device_xname(sc->sc_dev), bp, (bp->b_flags & B_READ) ? 'R' : 'W'));
    722 
    723 again:
    724 
    725 	sc->sc_flags |= RDF_SEEK;
    726 	sc->sc_ioc.c_unit = CS80CMD_SUNIT(punit);
    727 	sc->sc_ioc.c_volume = CS80CMD_SVOL(0);
    728 	sc->sc_ioc.c_saddr = CS80CMD_SADDR;
    729 	sc->sc_ioc.c_hiaddr = htobe16(0);
    730 	sc->sc_ioc.c_addr = htobe32(RDBTOS(bp->b_rawblkno));
    731 	sc->sc_ioc.c_nop2 = CS80CMD_NOP;
    732 	sc->sc_ioc.c_slen = CS80CMD_SLEN;
    733 	sc->sc_ioc.c_len = htobe32(sc->sc_resid);
    734 	sc->sc_ioc.c_cmd = bp->b_flags & B_READ ? CS80CMD_READ : CS80CMD_WRITE;
    735 
    736 	if (gpibsend(sc->sc_ic, slave, CS80CMD_SCMD, &sc->sc_ioc.c_unit,
    737 	    sizeof(sc->sc_ioc)-1) == sizeof(sc->sc_ioc)-1) {
    738 		/* Instrumentation. */
    739 		disk_busy(&sc->sc_dk);
    740 		iostat_seek(sc->sc_dk.dk_stats);
    741 		gpibawait(sc->sc_ic);
    742 		return;
    743 	}
    744 	/*
    745 	 * Experience has shown that the gpibwait in this gpibsend will
    746 	 * occasionally timeout.  It appears to occur mostly on old 7914
    747 	 * drives with full maintenance tracks.  We should probably
    748 	 * integrate this with the backoff code in rderror.
    749 	 */
    750 
    751 	DPRINTF(RDB_ERROR,
    752 	    ("rdstart: cmd %x adr %ul blk %" PRId64 " len %d ecnt %d\n",
    753 	    sc->sc_ioc.c_cmd, sc->sc_ioc.c_addr, bp->b_blkno, sc->sc_resid,
    754 	     sc->sc_errcnt));
    755 
    756 	sc->sc_flags &= ~RDF_SEEK;
    757 	cs80reset(device_parent(sc->sc_dev), slave, punit);
    758 	if (sc->sc_errcnt++ < RDRETRY)
    759 		goto again;
    760 	printf("%s: rdstart err: cmd 0x%x sect %uld blk %" PRId64 " len %d\n",
    761 	       device_xname(sc->sc_dev), sc->sc_ioc.c_cmd, sc->sc_ioc.c_addr,
    762 	       bp->b_blkno, sc->sc_resid);
    763 	bp->b_error = EIO;
    764 	bp = rdfinish(sc, bp);
    765 	if (bp) {
    766 		sc->sc_addr = bp->b_data;
    767 		sc->sc_resid = bp->b_bcount;
    768 		if (gpibrequest(sc->sc_ic, sc->sc_hdl))
    769 			goto again;
    770 	}
    771 }
    772 
    773 void
    774 rdintr(struct rd_softc *sc)
    775 {
    776 	struct buf *bp;
    777 	u_int8_t stat = 13;	/* in case gpibrecv fails */
    778 	int rv, dir, restart, slave;
    779 
    780 	slave = sc->sc_slave;
    781 	bp = bufq_peek(sc->sc_tab);
    782 
    783 	DPRINTF(RDB_FOLLOW, ("rdintr(%s): bp %p, %c, flags %x\n",
    784 	    device_xname(sc->sc_dev), bp, (bp->b_flags & B_READ) ? 'R' : 'W',
    785 	    sc->sc_flags));
    786 
    787 	disk_unbusy(&sc->sc_dk, (bp->b_bcount - bp->b_resid),
    788 		(bp->b_flags & B_READ));
    789 
    790 	if (sc->sc_flags & RDF_SEEK) {
    791 		sc->sc_flags &= ~RDF_SEEK;
    792 		dir = (bp->b_flags & B_READ ? GPIB_READ : GPIB_WRITE);
    793 		gpibxfer(sc->sc_ic, slave, CS80CMD_EXEC, sc->sc_addr,
    794 		    sc->sc_resid, dir, dir == GPIB_READ);
    795 		disk_busy(&sc->sc_dk);
    796 		return;
    797 	}
    798 	if ((sc->sc_flags & RDF_SWAIT) == 0) {
    799 		if (gpibpptest(sc->sc_ic, slave) == 0) {
    800 			/* Instrumentation. */
    801 			disk_busy(&sc->sc_dk);
    802 			sc->sc_flags |= RDF_SWAIT;
    803 			gpibawait(sc->sc_ic);
    804 			return;
    805 		}
    806 	} else
    807 		sc->sc_flags &= ~RDF_SWAIT;
    808 	rv = gpibrecv(sc->sc_ic, slave, CS80CMD_QSTAT, &stat, 1);
    809 	if (rv != 1 || stat) {
    810 		DPRINTF(RDB_ERROR,
    811 		    ("rdintr: receive failed (rv=%d) or bad stat %d\n", rv,
    812 		     stat));
    813 		restart = rderror(sc);
    814 		if (sc->sc_errcnt++ < RDRETRY) {
    815 			if (restart)
    816 				rdstart(sc);
    817 			return;
    818 		}
    819 		bp->b_error = EIO;
    820 	}
    821 	if (rdfinish(sc, bp) != NULL)
    822 		rdustart(sc);
    823 	rnd_add_uint32(&sc->rnd_source, bp->b_blkno);
    824 }
    825 
    826 /*
    827  * Deal with errors.
    828  * Returns 1 if request should be restarted,
    829  * 0 if we should just quietly give up.
    830  */
    831 int
    832 rderror(struct rd_softc *sc)
    833 {
    834 	struct cs80_stat css;
    835 	struct buf *bp;
    836 	daddr_t hwbn, pbn;
    837 
    838 	DPRINTF(RDB_FOLLOW, ("rderror: sc=%p\n", sc));
    839 
    840 	if (cs80status(device_parent(sc->sc_dev), sc->sc_slave,
    841 	    sc->sc_punit, &css)) {
    842 		cs80reset(device_parent(sc->sc_dev), sc->sc_slave,
    843 		    sc->sc_punit);
    844 		return (1);
    845 	}
    846 #ifdef DEBUG
    847 	if (rddebug & RDB_ERROR) {			/* status info */
    848 		printf("\n    volume: %d, unit: %d\n",
    849 		       (css.c_vu>>4)&0xF, css.c_vu&0xF);
    850 		printf("    reject 0x%x\n", css.c_ref);
    851 		printf("    fault 0x%x\n", css.c_fef);
    852 		printf("    access 0x%x\n", css.c_aef);
    853 		printf("    info 0x%x\n", css.c_ief);
    854 		printf("    block,  P1-P10: ");
    855 		printf("0x%x", *(u_int32_t *)&css.c_raw[0]);
    856 		printf("0x%x", *(u_int32_t *)&css.c_raw[4]);
    857 		printf("0x%x\n", *(u_int16_t *)&css.c_raw[8]);
    858 	}
    859 #endif
    860 	if (css.c_fef & FEF_REXMT)
    861 		return (1);
    862 	if (css.c_fef & FEF_PF) {
    863 		cs80reset(device_parent(sc->sc_dev), sc->sc_slave,
    864 		    sc->sc_punit);
    865 		return (1);
    866 	}
    867 	/*
    868 	 * Unit requests release for internal maintenance.
    869 	 * We just delay awhile and try again later.  Use expontially
    870 	 * increasing backoff ala ethernet drivers since we don't really
    871 	 * know how long the maintenance will take.  With RDWAITC and
    872 	 * RDRETRY as defined, the range is 1 to 32 seconds.
    873 	 */
    874 	if (css.c_fef & FEF_IMR) {
    875 		extern int hz;
    876 		int rdtimo = RDWAITC << sc->sc_errcnt;
    877 		DPRINTF(RDB_STATUS,
    878 		    ("%s: internal maintenance, %d-second timeout\n",
    879 		    device_xname(sc->sc_dev), rdtimo));
    880 		gpibrelease(sc->sc_ic, sc->sc_hdl);
    881 		callout_reset(&sc->sc_restart_ch, rdtimo * hz, rdrestart, sc);
    882 		return (0);
    883 	}
    884 	/*
    885 	 * Only report error if we have reached the error reporting
    886 	 * threshhold.  By default, this will only report after the
    887 	 * retry limit has been exceeded.
    888 	 */
    889 	if (sc->sc_errcnt < rderrthresh)
    890 		return (1);
    891 
    892 	/*
    893 	 * First conjure up the block number at which the error occurred.
    894  	 */
    895 	bp = bufq_peek(sc->sc_tab);
    896 	pbn = sc->sc_dk.dk_label->d_partitions[RDPART(bp->b_dev)].p_offset;
    897 	if ((css.c_fef & FEF_CU) || (css.c_fef & FEF_DR) ||
    898 	    (css.c_ief & IEF_RRMASK)) {
    899 		/*
    900 		 * Not all errors report a block number, just use b_blkno.
    901 		 */
    902 		hwbn = RDBTOS(pbn + bp->b_blkno);
    903 		pbn = bp->b_blkno;
    904 	} else {
    905 		hwbn = css.c_blk;
    906 		pbn = RDSTOB(hwbn) - pbn;
    907 	}
    908 #ifdef DEBUG
    909 	if (rddebug & RDB_ERROR) {			/* status info */
    910 		printf("\n    volume: %d, unit: %d\n",
    911 		       (css.c_vu>>4)&0xF, css.c_vu&0xF);
    912 		printf("    reject 0x%x\n", css.c_ref);
    913 		printf("    fault 0x%x\n", css.c_fef);
    914 		printf("    access 0x%x\n", css.c_aef);
    915 		printf("    info 0x%x\n", css.c_ief);
    916 		printf("    block,  P1-P10: ");
    917 		printf("    block: %" PRId64 ", P1-P10: ", hwbn);
    918 		printf("0x%x", *(u_int32_t *)&css.c_raw[0]);
    919 		printf("0x%x", *(u_int32_t *)&css.c_raw[4]);
    920 		printf("0x%x\n", *(u_int16_t *)&css.c_raw[8]);
    921 	}
    922 #endif
    923 #ifdef DEBUG
    924 	if (rddebug & RDB_ERROR) {			/* command */
    925 		printf("    ioc: ");
    926 		printf("0x%x", *(u_int32_t *)&sc->sc_ioc.c_pad);
    927 		printf("0x%x", *(u_int16_t *)&sc->sc_ioc.c_hiaddr);
    928 		printf("0x%x", *(u_int32_t *)&sc->sc_ioc.c_addr);
    929 		printf("0x%x", *(u_int16_t *)&sc->sc_ioc.c_nop2);
    930 		printf("0x%x", *(u_int32_t *)&sc->sc_ioc.c_len);
    931 		printf("0x%x\n", *(u_int16_t *)&sc->sc_ioc.c_cmd);
    932 		return (1);
    933 	}
    934 #endif
    935 	/*
    936 	 * Now output a generic message suitable for badsect.
    937 	 * Note that we don't use harderr because it just prints
    938 	 * out b_blkno which is just the beginning block number
    939 	 * of the transfer, not necessary where the error occurred.
    940 	 */
    941 	printf("%s%c: hard error, sector number %" PRId64 "\n",
    942 	    device_xname(sc->sc_dev), 'a'+RDPART(bp->b_dev), pbn);
    943 	/*
    944 	 * Now report the status as returned by the hardware with
    945 	 * attempt at interpretation.
    946 	 */
    947 	printf("%s %s error:", device_xname(sc->sc_dev),
    948 	    (bp->b_flags & B_READ) ? "read" : "write");
    949 	printf(" unit %d, volume %d R0x%x F0x%x A0x%x I0x%x\n",
    950 	       css.c_vu&0xF, (css.c_vu>>4)&0xF,
    951 	       css.c_ref, css.c_fef, css.c_aef, css.c_ief);
    952 	printf("P1-P10: ");
    953 	printf("0x%x ", *(u_int32_t *)&css.c_raw[0]);
    954 	printf("0x%x ", *(u_int32_t *)&css.c_raw[4]);
    955 	printf("0x%x\n", *(u_int16_t *)&css.c_raw[8]);
    956 
    957 	return (1);
    958 }
    959 
    960 int
    961 rdread(dev_t dev, struct uio *uio, int flags)
    962 {
    963 
    964 	return (physio(rdstrategy, NULL, dev, B_READ, minphys, uio));
    965 }
    966 
    967 int
    968 rdwrite(dev_t dev, struct uio *uio, int flags)
    969 {
    970 
    971 	return (physio(rdstrategy, NULL, dev, B_WRITE, minphys, uio));
    972 }
    973 
    974 int
    975 rdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    976 {
    977 	struct rd_softc *sc;
    978 	struct disklabel *lp;
    979 	int error, flags;
    980 
    981 	sc = device_lookup_private(&rd_cd, RDUNIT(dev));
    982 	if (sc == NULL)
    983 		return (ENXIO);
    984 	lp = sc->sc_dk.dk_label;
    985 
    986 	DPRINTF(RDB_FOLLOW, ("rdioctl: sc=%p\n", sc));
    987 
    988 	switch (cmd) {
    989 	case DIOCGDINFO:
    990 		*(struct disklabel *)data = *lp;
    991 		return (0);
    992 
    993 	case DIOCGPART:
    994 		((struct partinfo *)data)->disklab = lp;
    995 		((struct partinfo *)data)->part =
    996 		    &lp->d_partitions[RDPART(dev)];
    997 		return (0);
    998 
    999 	case DIOCWLABEL:
   1000 		if ((flag & FWRITE) == 0)
   1001 			return (EBADF);
   1002 		if (*(int *)data)
   1003 			sc->sc_flags |= RDF_WLABEL;
   1004 		else
   1005 			sc->sc_flags &= ~RDF_WLABEL;
   1006 		return (0);
   1007 
   1008 	case DIOCSDINFO:
   1009 		if ((flag & FWRITE) == 0)
   1010 			return (EBADF);
   1011 		return (setdisklabel(lp, (struct disklabel *)data,
   1012 		    (sc->sc_flags & RDF_WLABEL) ? 0 : sc->sc_dk.dk_openmask,
   1013 		    (struct cpu_disklabel *)0));
   1014 
   1015 	case DIOCWDINFO:
   1016 		if ((flag & FWRITE) == 0)
   1017 			return (EBADF);
   1018 		error = setdisklabel(lp, (struct disklabel *)data,
   1019 		    (sc->sc_flags & RDF_WLABEL) ? 0 : sc->sc_dk.dk_openmask,
   1020 		    (struct cpu_disklabel *)0);
   1021 		if (error)
   1022 			return (error);
   1023 		flags = sc->sc_flags;
   1024 		sc->sc_flags = RDF_ALIVE | RDF_WLABEL;
   1025 		error = writedisklabel(RDLABELDEV(dev), rdstrategy, lp,
   1026 		    (struct cpu_disklabel *)0);
   1027 		sc->sc_flags = flags;
   1028 		return (error);
   1029 
   1030 	case DIOCGDEFLABEL:
   1031 		rdgetdefaultlabel(sc, (struct disklabel *)data);
   1032 		return (0);
   1033 	}
   1034 	return (EINVAL);
   1035 }
   1036 
   1037 void
   1038 rdgetdefaultlabel(struct rd_softc *sc, struct disklabel *lp)
   1039 {
   1040 	int type = sc->sc_type;
   1041 
   1042 	memset((void *)lp, 0, sizeof(struct disklabel));
   1043 
   1044 	lp->d_type = DTYPE_HPIB /* DTYPE_GPIB */;
   1045 	lp->d_secsize = DEV_BSIZE;
   1046 	lp->d_nsectors = rdidentinfo[type].ri_nbpt;
   1047 	lp->d_ntracks = rdidentinfo[type].ri_ntpc;
   1048 	lp->d_ncylinders = rdidentinfo[type].ri_ncyl;
   1049 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
   1050 	lp->d_secperunit = lp->d_ncylinders * lp->d_secpercyl;
   1051 
   1052 	strncpy(lp->d_typename, rdidentinfo[type].ri_desc, 16);
   1053 	strncpy(lp->d_packname, "fictitious", 16);
   1054 	lp->d_rpm = 3000;
   1055 	lp->d_interleave = 1;
   1056 	lp->d_flags = 0;
   1057 
   1058 	lp->d_partitions[RAW_PART].p_offset = 0;
   1059 	lp->d_partitions[RAW_PART].p_size =
   1060 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
   1061 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
   1062 	lp->d_npartitions = RAW_PART + 1;
   1063 
   1064 	lp->d_magic = DISKMAGIC;
   1065 	lp->d_magic2 = DISKMAGIC;
   1066 	lp->d_checksum = dkcksum(lp);
   1067 }
   1068 
   1069 int
   1070 rdsize(dev_t dev)
   1071 {
   1072 	struct rd_softc *sc;
   1073 	int psize, didopen = 0;
   1074 
   1075 	sc = device_lookup_private(&rd_cd, RDUNIT(dev));
   1076 	if (sc == NULL || (sc->sc_flags & RDF_ALIVE) == 0)
   1077 		return (-1);
   1078 
   1079 	/*
   1080 	 * We get called very early on (via swapconf)
   1081 	 * without the device being open so we may need
   1082 	 * to handle it here.
   1083 	 */
   1084 	if (sc->sc_dk.dk_openmask == 0) {
   1085 		if (rdopen(dev, FREAD | FWRITE, S_IFBLK, NULL))
   1086 			return (-1);
   1087 		didopen = 1;
   1088 	}
   1089 	psize = sc->sc_dk.dk_label->d_partitions[RDPART(dev)].p_size *
   1090 	    (sc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
   1091 	if (didopen)
   1092 		(void) rdclose(dev, FREAD | FWRITE, S_IFBLK, NULL);
   1093 	return (psize);
   1094 }
   1095 
   1096 
   1097 static int rddoingadump;	/* simple mutex */
   1098 
   1099 /*
   1100  * Non-interrupt driven, non-dma dump routine.
   1101  */
   1102 int
   1103 rddump(dev_t dev, daddr_t blkno, void *va, size_t size)
   1104 {
   1105 	struct rd_softc *sc;
   1106 	int sectorsize;		/* size of a disk sector */
   1107 	int nsects;		/* number of sectors in partition */
   1108 	int sectoff;		/* sector offset of partition */
   1109 	int totwrt;		/* total number of sectors left to write */
   1110 	int nwrt;		/* current number of sectors to write */
   1111 	int slave;
   1112 	struct disklabel *lp;
   1113 	u_int8_t stat;
   1114 
   1115 	/* Check for recursive dump; if so, punt. */
   1116 	if (rddoingadump)
   1117 		return (EFAULT);
   1118 	rddoingadump = 1;
   1119 
   1120 	sc = device_lookup_private(&rd_cd, RDUNIT(dev));
   1121 	if (sc == NULL || (sc->sc_flags & RDF_ALIVE) == 0)
   1122 		return (ENXIO);
   1123 
   1124 	DPRINTF(RDB_FOLLOW, ("rddump: sc=%p\n", sc));
   1125 
   1126 	slave = sc->sc_slave;
   1127 
   1128 	/*
   1129 	 * Convert to disk sectors.  Request must be a multiple of size.
   1130 	 */
   1131 	lp = sc->sc_dk.dk_label;
   1132 	sectorsize = lp->d_secsize;
   1133 	if ((size % sectorsize) != 0)
   1134 		return (EFAULT);
   1135 	totwrt = size / sectorsize;
   1136 	blkno = dbtob(blkno) / sectorsize;	/* blkno in DEV_BSIZE units */
   1137 
   1138 	nsects = lp->d_partitions[RDPART(dev)].p_size;
   1139 	sectoff = lp->d_partitions[RDPART(dev)].p_offset;
   1140 
   1141 	/* Check transfer bounds against partition size. */
   1142 	if ((blkno < 0) || (blkno + totwrt) > nsects)
   1143 		return (EINVAL);
   1144 
   1145 	/* Offset block number to start of partition. */
   1146 	blkno += sectoff;
   1147 
   1148 	while (totwrt > 0) {
   1149 		nwrt = totwrt;		/* XXX */
   1150 #ifndef RD_DUMP_NOT_TRUSTED
   1151 		/*
   1152 		 * Fill out and send GPIB command.
   1153 		 */
   1154 		sc->sc_ioc.c_unit = CS80CMD_SUNIT(sc->sc_punit);
   1155 		sc->sc_ioc.c_volume = CS80CMD_SVOL(0);
   1156 		sc->sc_ioc.c_saddr = CS80CMD_SADDR;
   1157 		sc->sc_ioc.c_hiaddr = 0;
   1158 		sc->sc_ioc.c_addr = RDBTOS(blkno);
   1159 		sc->sc_ioc.c_nop2 = CS80CMD_NOP;
   1160 		sc->sc_ioc.c_slen = CS80CMD_SLEN;
   1161 		sc->sc_ioc.c_len = nwrt * sectorsize;
   1162 		sc->sc_ioc.c_cmd = CS80CMD_WRITE;
   1163 		(void) gpibsend(sc->sc_ic, slave, CS80CMD_SCMD,
   1164 		    &sc->sc_ioc.c_unit, sizeof(sc->sc_ioc)-3);
   1165 		if (gpibswait(sc->sc_ic, slave))
   1166 			return (EIO);
   1167 		/*
   1168 		 * Send the data.
   1169 		 */
   1170 		(void) gpibsend(sc->sc_ic, slave, CS80CMD_EXEC, va,
   1171 		    nwrt * sectorsize);
   1172 		(void) gpibswait(sc->sc_ic, slave);
   1173 		(void) gpibrecv(sc->sc_ic, slave, CS80CMD_QSTAT, &stat, 1);
   1174 		if (stat)
   1175 			return (EIO);
   1176 #else /* RD_DUMP_NOT_TRUSTED */
   1177 		/* Let's just talk about this first... */
   1178 		printf("%s: dump addr %p, blk %d\n", device_xname(sc->sc_dev),
   1179 		    va, blkno);
   1180 		delay(500 * 1000);	/* half a second */
   1181 #endif /* RD_DUMP_NOT_TRUSTED */
   1182 
   1183 		/* update block count */
   1184 		totwrt -= nwrt;
   1185 		blkno += nwrt;
   1186 		va = (char *)va + sectorsize * nwrt;
   1187 	}
   1188 	rddoingadump = 0;
   1189 	return (0);
   1190 }
   1191