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