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