Home | History | Annotate | Line # | Download | only in gpib
rd.c revision 1.19.2.2
      1 /*	$NetBSD: rd.c,v 1.19.2.2 2008/06/17 09:14:33 yamt 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.19.2.2 2008/06/17 09:14:33 yamt 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(struct device *, struct cfdata *, void *);
    287 void	rdattach(struct device *, struct device *, 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(id, slave, punit)
    315 	int id;
    316 	int slave;
    317 	int punit;
    318 {
    319 	int i;
    320 
    321 	for (i = 0; i < numrdidentinfo; i++) {
    322 		if (rdidentinfo[i].ri_hwid == id)
    323 			break;
    324 	}
    325 	if (i == numrdidentinfo || punit > rdidentinfo[i].ri_maxunum)
    326 		return (-1);
    327 	return (i);
    328 }
    329 
    330 int
    331 rdmatch(parent, match, aux)
    332 	struct device *parent;
    333 	struct cfdata *match;
    334 	void *aux;
    335 {
    336 	struct cs80bus_attach_args *ca = aux;
    337 
    338 	if (rdlookup(ca->ca_id, ca->ca_slave, ca->ca_punit) < 0)
    339 		return (0);
    340 	return (1);
    341 }
    342 
    343 void
    344 rdattach(parent, self, aux)
    345 	struct device *parent, *self;
    346 	void *aux;
    347 {
    348 	struct rd_softc *sc = device_private(self);
    349 	struct cs80bus_attach_args *ca = aux;
    350 	struct cs80_description csd;
    351 	char name[7];
    352 	int type, i, n;
    353 
    354 	sc->sc_ic = ca->ca_ic;
    355 	sc->sc_slave = ca->ca_slave;
    356 	sc->sc_punit = ca->ca_punit;
    357 
    358 	if ((type = rdlookup(ca->ca_id, ca->ca_slave, ca->ca_punit)) < 0)
    359 		return;
    360 
    361 	if (cs80reset(parent, sc->sc_slave, sc->sc_punit)) {
    362 		aprint_normal("\n");
    363 		aprint_error_dev(&sc->sc_dev, "can't reset device\n");
    364 		return;
    365 	}
    366 
    367 	if (cs80describe(parent, sc->sc_slave, sc->sc_punit, &csd)) {
    368 		aprint_normal("\n");
    369 		aprint_error_dev(&sc->sc_dev, "didn't respond to describe command\n");
    370 		return;
    371 	}
    372 	memset(name, 0, sizeof(name));
    373 	for (i=0, n=0; i<3; i++) {
    374 		name[n++] = (csd.d_name[i] >> 4) + '0';
    375 		name[n++] = (csd.d_name[i] & 0x0f) + '0';
    376 	}
    377 
    378 #ifdef DEBUG
    379 	if (rddebug & RDB_IDENT) {
    380 		printf("\n%s: name: ('%s')\n",
    381 		    device_xname(&sc->sc_dev), name);
    382 		printf("  iuw %x, maxxfr %d, ctype %d\n",
    383 		    csd.d_iuw, csd.d_cmaxxfr, csd.d_ctype);
    384 		printf("  utype %d, bps %d, blkbuf %d, burst %d, blktime %d\n",
    385 		    csd.d_utype, csd.d_sectsize,
    386 		    csd.d_blkbuf, csd.d_burstsize, csd.d_blocktime);
    387 		printf("  avxfr %d, ort %d, atp %d, maxint %d, fv %x, rv %x\n",
    388 		    csd.d_uavexfr, csd.d_retry, csd.d_access,
    389 		    csd.d_maxint, csd.d_fvbyte, csd.d_rvbyte);
    390 		printf("  maxcyl/head/sect %d/%d/%d, maxvsect %d, inter %d\n",
    391 		    csd.d_maxcylhead >> 8, csd.d_maxcylhead & 0xff,
    392 		    csd.d_maxsect, csd.d_maxvsectl, csd.d_interleave);
    393 		printf("%s", device_xname(&sc->sc_dev));
    394 	}
    395 #endif
    396 
    397 	/*
    398 	 * Take care of a couple of anomolies:
    399 	 * 1. 7945A and 7946A both return same HW id
    400 	 * 2. 9122S and 9134D both return same HW id
    401 	 * 3. 9122D and 9134L both return same HW id
    402 	 */
    403 	switch (ca->ca_id) {
    404 	case RD7946AID:
    405 		if (memcmp(name, "079450", 6) == 0)
    406 			type = RD7945A;
    407 		else
    408 			type = RD7946A;
    409 		break;
    410 
    411 	case RD9134LID:
    412 		if (memcmp(name, "091340", 6) == 0)
    413 			type = RD9134L;
    414 		else
    415 			type = RD9122D;
    416 		break;
    417 
    418 	case RD9134DID:
    419 		if (memcmp(name, "091220", 6) == 0)
    420 			type = RD9122S;
    421 		else
    422 			type = RD9134D;
    423 		break;
    424 	}
    425 
    426 	sc->sc_type = type;
    427 
    428 	/*
    429 	 * XXX We use DEV_BSIZE instead of the sector size value pulled
    430 	 * XXX off the driver because all of this code assumes 512 byte
    431 	 * XXX blocks.  ICK!
    432 	 */
    433 	printf(": %s\n", rdidentinfo[type].ri_desc);
    434 	printf("%s: %d cylinders, %d heads, %d blocks, %d bytes/block\n",
    435 	    device_xname(&sc->sc_dev), rdidentinfo[type].ri_ncyl,
    436 	    rdidentinfo[type].ri_ntpc, rdidentinfo[type].ri_nblocks,
    437 	    DEV_BSIZE);
    438 
    439 	bufq_alloc(&sc->sc_tab, "fcfs", 0);
    440 
    441 	/*
    442 	 * Initialize and attach the disk structure.
    443 	 */
    444 	memset(&sc->sc_dk, 0, sizeof(sc->sc_dk));
    445 	disk_init(&sc->sc_dk, device_xname(&sc->sc_dev), NULL);
    446 	disk_attach(&sc->sc_dk);
    447 
    448 	callout_init(&sc->sc_restart_ch, 0);
    449 
    450 	if (gpibregister(sc->sc_ic, sc->sc_slave, rdcallback, sc,
    451 	    &sc->sc_hdl)) {
    452 		aprint_error_dev(&sc->sc_dev, "can't register callback\n");
    453 		return;
    454 	}
    455 
    456 	sc->sc_flags = RDF_ALIVE;
    457 #ifdef DEBUG
    458 	/* always report errors */
    459 	if (rddebug & RDB_ERROR)
    460 		rderrthresh = 0;
    461 #endif
    462 #if NRND > 0
    463 	/*
    464 	 * attach the device into the random source list
    465 	 */
    466 	rnd_attach_source(&sc->rnd_source, device_xname(&sc->sc_dev),
    467 			  RND_TYPE_DISK, 0);
    468 #endif
    469 }
    470 
    471 /*
    472  * Read or construct a disklabel
    473  */
    474 int
    475 rdgetinfo(sc)
    476 	struct rd_softc *sc;
    477 {
    478 	struct disklabel *lp = sc->sc_dk.dk_label;
    479 	struct partition *pi;
    480 	const char *msg;
    481 
    482 	memset(sc->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
    483 
    484 	rdgetdefaultlabel(sc, lp);
    485 
    486 	/*
    487 	 * Call the generic disklabel extraction routine
    488 	 */
    489 	msg = readdisklabel(RDMAKEDEV(0, device_unit(&sc->sc_dev), RAW_PART),
    490 	    rdstrategy, lp, NULL);
    491 	if (msg == NULL)
    492 		return (0);
    493 
    494 	pi = lp->d_partitions;
    495 	printf("%s: WARNING: %s\n", device_xname(&sc->sc_dev), msg);
    496 
    497 	pi[RAW_PART].p_size = rdidentinfo[sc->sc_type].ri_nblocks;
    498 	lp->d_npartitions = RAW_PART+1;
    499 	pi[0].p_size = 0;
    500 
    501 	return (0);
    502 }
    503 
    504 int
    505 rdopen(dev_t dev, int flags, int mode, struct lwp *l)
    506 {
    507 	struct rd_softc *sc;
    508 	int error, mask, part;
    509 
    510 	sc = device_lookup_private(&rd_cd, RDUNIT(dev));
    511 	if (sc == NULL || (sc->sc_flags & RDF_ALIVE) ==0)
    512 		return (ENXIO);
    513 
    514 	/*
    515 	 * Wait for any pending opens/closes to complete
    516 	 */
    517 	while (sc->sc_flags & (RDF_OPENING | RDF_CLOSING))
    518 		(void) tsleep(sc, PRIBIO, "rdopen", 0);
    519 
    520 	/*
    521 	 * On first open, get label and partition info.
    522 	 * We may block reading the label, so be careful
    523 	 * to stop any other opens.
    524 	 */
    525 	if (sc->sc_dk.dk_openmask == 0) {
    526 		sc->sc_flags |= RDF_OPENING;
    527 		error = rdgetinfo(sc);
    528 		sc->sc_flags &= ~RDF_OPENING;
    529 		wakeup((void *)sc);
    530 		if (error)
    531 			return (error);
    532 	}
    533 
    534 	part = RDPART(dev);
    535 	mask = 1 << part;
    536 
    537 	/* Check that the partition exists. */
    538 	if (part != RAW_PART && (part > sc->sc_dk.dk_label->d_npartitions ||
    539 	    sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED))
    540 		return (ENXIO);
    541 
    542 	/* Ensure only one open at a time. */
    543 	switch (mode) {
    544 	case S_IFCHR:
    545 		sc->sc_dk.dk_copenmask |= mask;
    546 		break;
    547 	case S_IFBLK:
    548 		sc->sc_dk.dk_bopenmask |= mask;
    549 		break;
    550 	}
    551 	sc->sc_dk.dk_openmask =
    552 	    sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
    553 
    554 	return (0);
    555 }
    556 
    557 int
    558 rdclose(dev_t dev, int flag, int mode, struct lwp *l)
    559 {
    560 	struct rd_softc *sc;
    561 	struct disk *dk;
    562 	int mask, s;
    563 
    564 	sc = device_lookup_private(&rd_cd, RDUNIT(dev));
    565 	if (sc == NULL)
    566 		return (ENXIO);
    567 
    568 	dk = &sc->sc_dk;
    569 
    570 	mask = 1 << RDPART(dev);
    571 	if (mode == S_IFCHR)
    572 		dk->dk_copenmask &= ~mask;
    573 	else
    574 		dk->dk_bopenmask &= ~mask;
    575 	dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
    576 	/*
    577 	 * On last close, we wait for all activity to cease since
    578 	 * the label/parition info will become invalid.  Since we
    579 	 * might sleep, we must block any opens while we are here.
    580 	 * Note we don't have to about other closes since we know
    581 	 * we are the last one.
    582 	 */
    583 	if (dk->dk_openmask == 0) {
    584 		sc->sc_flags |= RDF_CLOSING;
    585 		s = splbio();
    586 		while (sc->sc_active) {
    587 			sc->sc_flags |= RDF_WANTED;
    588 			(void) tsleep(&sc->sc_tab, PRIBIO, "rdclose", 0);
    589 		}
    590 		splx(s);
    591 		sc->sc_flags &= ~(RDF_CLOSING | RDF_WLABEL);
    592 		wakeup((void *)sc);
    593 	}
    594 	return (0);
    595 }
    596 
    597 void
    598 rdstrategy(struct buf *bp)
    599 {
    600 	struct rd_softc *sc;
    601 	struct partition *pinfo;
    602 	daddr_t bn;
    603 	int sz, s;
    604 	int offset;
    605 
    606 	sc = device_lookup_private(&rd_cd, RDUNIT(bp->b_dev));
    607 
    608 	DPRINTF(RDB_FOLLOW,
    609 	    ("rdstrategy(%p): dev %x, bn %" PRId64 ", bcount %ld, %c\n",
    610 	    bp, bp->b_dev, bp->b_blkno, bp->b_bcount,
    611 	    (bp->b_flags & B_READ) ? 'R' : 'W'));
    612 
    613 	bn = bp->b_blkno;
    614 	sz = howmany(bp->b_bcount, DEV_BSIZE);
    615 	pinfo = &sc->sc_dk.dk_label->d_partitions[RDPART(bp->b_dev)];
    616 
    617 	/* Don't perform partition translation on RAW_PART. */
    618 	offset = (RDPART(bp->b_dev) == RAW_PART) ? 0 : pinfo->p_offset;
    619 
    620 	if (RDPART(bp->b_dev) != RAW_PART) {
    621 		/*
    622 		 * XXX This block of code belongs in
    623 		 * XXX bounds_check_with_label()
    624 		 */
    625 
    626 		if (bn < 0 || bn + sz > pinfo->p_size) {
    627 			sz = pinfo->p_size - bn;
    628 			if (sz == 0) {
    629 				bp->b_resid = bp->b_bcount;
    630 				goto done;
    631 			}
    632 			if (sz < 0) {
    633 				bp->b_error = EINVAL;
    634 				goto done;
    635 			}
    636 			bp->b_bcount = dbtob(sz);
    637 		}
    638 		/*
    639 		 * Check for write to write protected label
    640 		 */
    641 		if (bn + offset <= LABELSECTOR &&
    642 #if LABELSECTOR != 0
    643 		    bn + offset + sz > LABELSECTOR &&
    644 #endif
    645 		    !(bp->b_flags & B_READ) && !(sc->sc_flags & RDF_WLABEL)) {
    646 			bp->b_error = EROFS;
    647 			goto done;
    648 		}
    649 	}
    650 	bp->b_rawblkno = bn + offset;
    651 	s = splbio();
    652 	BUFQ_PUT(sc->sc_tab, bp);
    653 	if (sc->sc_active == 0) {
    654 		sc->sc_active = 1;
    655 		rdustart(sc);
    656 	}
    657 	splx(s);
    658 	return;
    659 done:
    660 	biodone(bp);
    661 }
    662 
    663 /*
    664  * Called from timeout() when handling maintenance releases
    665  * callout from timeouts
    666  */
    667 void
    668 rdrestart(arg)
    669 	void *arg;
    670 {
    671 	int s = splbio();
    672 	rdustart((struct rd_softc *)arg);
    673 	splx(s);
    674 }
    675 
    676 
    677 /* called by rdstrategy() to start a block transfer */
    678 /* called by rdrestart() when handingly timeouts */
    679 /* called by rdintr() */
    680 void
    681 rdustart(sc)
    682 	struct rd_softc *sc;
    683 {
    684 	struct buf *bp;
    685 
    686 	bp = BUFQ_PEEK(sc->sc_tab);
    687 	sc->sc_addr = bp->b_data;
    688 	sc->sc_resid = bp->b_bcount;
    689 	if (gpibrequest(sc->sc_ic, sc->sc_hdl))
    690 		rdstart(sc);
    691 }
    692 
    693 struct buf *
    694 rdfinish(sc, bp)
    695 	struct rd_softc *sc;
    696 	struct buf *bp;
    697 {
    698 
    699 	sc->sc_errcnt = 0;
    700 	(void)BUFQ_GET(sc->sc_tab);
    701 	bp->b_resid = 0;
    702 	biodone(bp);
    703 	gpibrelease(sc->sc_ic, sc->sc_hdl);
    704 	if ((bp = BUFQ_PEEK(sc->sc_tab)) != NULL)
    705 		return (bp);
    706 	sc->sc_active = 0;
    707 	if (sc->sc_flags & RDF_WANTED) {
    708 		sc->sc_flags &= ~RDF_WANTED;
    709 		wakeup((void *)&sc->sc_tab);
    710 	}
    711 	return (NULL);
    712 }
    713 
    714 void
    715 rdcallback(v, action)
    716 	void *v;
    717 	int action;
    718 {
    719 	struct rd_softc *sc = v;
    720 
    721 	DPRINTF(RDB_FOLLOW, ("rdcallback: v=%p, action=%d\n", v, action));
    722 
    723 	switch (action) {
    724 	case GPIBCBF_START:
    725 		rdstart(sc);
    726 		break;
    727 	case GPIBCBF_INTR:
    728 		rdintr(sc);
    729 		break;
    730 #ifdef DEBUG
    731 	default:
    732 		DPRINTF(RDB_ERROR, ("rdcallback: unknown action %d\n",
    733 		    action));
    734 		break;
    735 #endif
    736 	}
    737 }
    738 
    739 
    740 /* called from rdustart() to start a transfer */
    741 /* called from gpib interface as the initiator */
    742 void
    743 rdstart(sc)
    744 	struct rd_softc *sc;
    745 {
    746 	struct buf *bp = BUFQ_PEEK(sc->sc_tab);
    747 	int part, slave, punit;
    748 
    749 	slave = sc->sc_slave;
    750 	punit = sc->sc_punit;
    751 
    752 	DPRINTF(RDB_FOLLOW, ("rdstart(%s): bp %p, %c\n",
    753 	    device_xname(&sc->sc_dev), bp, (bp->b_flags & B_READ) ? 'R' : 'W'));
    754 
    755 again:
    756 
    757 	part = RDPART(bp->b_dev);
    758 	sc->sc_flags |= RDF_SEEK;
    759 	sc->sc_ioc.c_unit = CS80CMD_SUNIT(punit);
    760 	sc->sc_ioc.c_volume = CS80CMD_SVOL(0);
    761 	sc->sc_ioc.c_saddr = CS80CMD_SADDR;
    762 	sc->sc_ioc.c_hiaddr = htobe16(0);
    763 	sc->sc_ioc.c_addr = htobe32(RDBTOS(bp->b_rawblkno));
    764 	sc->sc_ioc.c_nop2 = CS80CMD_NOP;
    765 	sc->sc_ioc.c_slen = CS80CMD_SLEN;
    766 	sc->sc_ioc.c_len = htobe32(sc->sc_resid);
    767 	sc->sc_ioc.c_cmd = bp->b_flags & B_READ ? CS80CMD_READ : CS80CMD_WRITE;
    768 
    769 	if (gpibsend(sc->sc_ic, slave, CS80CMD_SCMD, &sc->sc_ioc.c_unit,
    770 	    sizeof(sc->sc_ioc)-1) == sizeof(sc->sc_ioc)-1) {
    771 		/* Instrumentation. */
    772 		disk_busy(&sc->sc_dk);
    773 		iostat_seek(sc->sc_dk.dk_stats);
    774 		gpibawait(sc->sc_ic);
    775 		return;
    776 	}
    777 	/*
    778 	 * Experience has shown that the gpibwait in this gpibsend will
    779 	 * occasionally timeout.  It appears to occur mostly on old 7914
    780 	 * drives with full maintenance tracks.  We should probably
    781 	 * integrate this with the backoff code in rderror.
    782 	 */
    783 
    784 	DPRINTF(RDB_ERROR,
    785 	    ("rdstart: cmd %x adr %ul blk %" PRId64 " len %d ecnt %d\n",
    786 	    sc->sc_ioc.c_cmd, sc->sc_ioc.c_addr, bp->b_blkno, sc->sc_resid,
    787 	     sc->sc_errcnt));
    788 
    789 	sc->sc_flags &= ~RDF_SEEK;
    790 	cs80reset(device_parent(&sc->sc_dev), slave, punit);
    791 	if (sc->sc_errcnt++ < RDRETRY)
    792 		goto again;
    793 	printf("%s: rdstart err: cmd 0x%x sect %uld blk %" PRId64 " len %d\n",
    794 	       device_xname(&sc->sc_dev), sc->sc_ioc.c_cmd, sc->sc_ioc.c_addr,
    795 	       bp->b_blkno, sc->sc_resid);
    796 	bp->b_error = EIO;
    797 	bp = rdfinish(sc, bp);
    798 	if (bp) {
    799 		sc->sc_addr = bp->b_data;
    800 		sc->sc_resid = bp->b_bcount;
    801 		if (gpibrequest(sc->sc_ic, sc->sc_hdl))
    802 			goto again;
    803 	}
    804 }
    805 
    806 void
    807 rdintr(sc)
    808 	struct rd_softc *sc;
    809 {
    810 	struct buf *bp;
    811 	u_int8_t stat = 13;	/* in case gpibrecv fails */
    812 	int rv, dir, restart, slave;
    813 
    814 	slave = sc->sc_slave;
    815 	bp = BUFQ_PEEK(sc->sc_tab);
    816 
    817 	DPRINTF(RDB_FOLLOW, ("rdintr(%s): bp %p, %c, flags %x\n",
    818 	    device_xname(&sc->sc_dev), bp, (bp->b_flags & B_READ) ? 'R' : 'W',
    819 	    sc->sc_flags));
    820 
    821 	disk_unbusy(&sc->sc_dk, (bp->b_bcount - bp->b_resid),
    822 		(bp->b_flags & B_READ));
    823 
    824 	if (sc->sc_flags & RDF_SEEK) {
    825 		sc->sc_flags &= ~RDF_SEEK;
    826 		dir = (bp->b_flags & B_READ ? GPIB_READ : GPIB_WRITE);
    827 		gpibxfer(sc->sc_ic, slave, CS80CMD_EXEC, sc->sc_addr,
    828 		    sc->sc_resid, dir, dir == GPIB_READ);
    829 		disk_busy(&sc->sc_dk);
    830 		return;
    831 	}
    832 	if ((sc->sc_flags & RDF_SWAIT) == 0) {
    833 		if (gpibpptest(sc->sc_ic, slave) == 0) {
    834 			/* Instrumentation. */
    835 			disk_busy(&sc->sc_dk);
    836 			sc->sc_flags |= RDF_SWAIT;
    837 			gpibawait(sc->sc_ic);
    838 			return;
    839 		}
    840 	} else
    841 		sc->sc_flags &= ~RDF_SWAIT;
    842 	rv = gpibrecv(sc->sc_ic, slave, CS80CMD_QSTAT, &stat, 1);
    843 	if (rv != 1 || stat) {
    844 		DPRINTF(RDB_ERROR,
    845 		    ("rdintr: receive failed (rv=%d) or bad stat %d\n", rv,
    846 		     stat));
    847 		restart = rderror(sc);
    848 		if (sc->sc_errcnt++ < RDRETRY) {
    849 			if (restart)
    850 				rdstart(sc);
    851 			return;
    852 		}
    853 		bp->b_error = EIO;
    854 	}
    855 	if (rdfinish(sc, bp) != NULL)
    856 		rdustart(sc);
    857 #if NRND > 0
    858 	rnd_add_uint32(&sc->rnd_source, bp->b_blkno);
    859 #endif
    860 }
    861 
    862 /*
    863  * Deal with errors.
    864  * Returns 1 if request should be restarted,
    865  * 0 if we should just quietly give up.
    866  */
    867 int
    868 rderror(sc)
    869 	struct rd_softc *sc;
    870 {
    871 	struct cs80_stat css;
    872 	struct buf *bp;
    873 	daddr_t hwbn, pbn;
    874 
    875 	DPRINTF(RDB_FOLLOW, ("rderror: sc=%p\n", sc));
    876 
    877 	if (cs80status(device_parent(&sc->sc_dev), sc->sc_slave,
    878 	    sc->sc_punit, &css)) {
    879 		cs80reset(device_parent(&sc->sc_dev), sc->sc_slave,
    880 		    sc->sc_punit);
    881 		return (1);
    882 	}
    883 #ifdef DEBUG
    884 	if (rddebug & RDB_ERROR) {			/* status info */
    885 		printf("\n    volume: %d, unit: %d\n",
    886 		       (css.c_vu>>4)&0xF, css.c_vu&0xF);
    887 		printf("    reject 0x%x\n", css.c_ref);
    888 		printf("    fault 0x%x\n", css.c_fef);
    889 		printf("    access 0x%x\n", css.c_aef);
    890 		printf("    info 0x%x\n", css.c_ief);
    891 		printf("    block,  P1-P10: ");
    892 		printf("0x%x", *(u_int32_t *)&css.c_raw[0]);
    893 		printf("0x%x", *(u_int32_t *)&css.c_raw[4]);
    894 		printf("0x%x\n", *(u_int16_t *)&css.c_raw[8]);
    895 	}
    896 #endif
    897 	if (css.c_fef & FEF_REXMT)
    898 		return (1);
    899 	if (css.c_fef & FEF_PF) {
    900 		cs80reset(device_parent(&sc->sc_dev), sc->sc_slave,
    901 		    sc->sc_punit);
    902 		return (1);
    903 	}
    904 	/*
    905 	 * Unit requests release for internal maintenance.
    906 	 * We just delay awhile and try again later.  Use expontially
    907 	 * increasing backoff ala ethernet drivers since we don't really
    908 	 * know how long the maintenance will take.  With RDWAITC and
    909 	 * RDRETRY as defined, the range is 1 to 32 seconds.
    910 	 */
    911 	if (css.c_fef & FEF_IMR) {
    912 		extern int hz;
    913 		int rdtimo = RDWAITC << sc->sc_errcnt;
    914 		DPRINTF(RDB_STATUS,
    915 		    ("%s: internal maintenance, %d-second timeout\n",
    916 		    device_xname(&sc->sc_dev), rdtimo));
    917 		gpibrelease(sc->sc_ic, sc->sc_hdl);
    918 		callout_reset(&sc->sc_restart_ch, rdtimo * hz, rdrestart, sc);
    919 		return (0);
    920 	}
    921 	/*
    922 	 * Only report error if we have reached the error reporting
    923 	 * threshhold.  By default, this will only report after the
    924 	 * retry limit has been exceeded.
    925 	 */
    926 	if (sc->sc_errcnt < rderrthresh)
    927 		return (1);
    928 
    929 	/*
    930 	 * First conjure up the block number at which the error occurred.
    931  	 */
    932 	bp = BUFQ_PEEK(sc->sc_tab);
    933 	pbn = sc->sc_dk.dk_label->d_partitions[RDPART(bp->b_dev)].p_offset;
    934 	if ((css.c_fef & FEF_CU) || (css.c_fef & FEF_DR) ||
    935 	    (css.c_ief & IEF_RRMASK)) {
    936 		/*
    937 		 * Not all errors report a block number, just use b_blkno.
    938 		 */
    939 		hwbn = RDBTOS(pbn + bp->b_blkno);
    940 		pbn = bp->b_blkno;
    941 	} else {
    942 		hwbn = css.c_blk;
    943 		pbn = RDSTOB(hwbn) - pbn;
    944 	}
    945 #ifdef DEBUG
    946 	if (rddebug & RDB_ERROR) {			/* status info */
    947 		printf("\n    volume: %d, unit: %d\n",
    948 		       (css.c_vu>>4)&0xF, css.c_vu&0xF);
    949 		printf("    reject 0x%x\n", css.c_ref);
    950 		printf("    fault 0x%x\n", css.c_fef);
    951 		printf("    access 0x%x\n", css.c_aef);
    952 		printf("    info 0x%x\n", css.c_ief);
    953 		printf("    block,  P1-P10: ");
    954 		printf("    block: %" PRId64 ", P1-P10: ", hwbn);
    955 		printf("0x%x", *(u_int32_t *)&css.c_raw[0]);
    956 		printf("0x%x", *(u_int32_t *)&css.c_raw[4]);
    957 		printf("0x%x\n", *(u_int16_t *)&css.c_raw[8]);
    958 	}
    959 #endif
    960 #ifdef DEBUG
    961 	if (rddebug & RDB_ERROR) {			/* command */
    962 		printf("    ioc: ");
    963 		printf("0x%x", *(u_int32_t *)&sc->sc_ioc.c_pad);
    964 		printf("0x%x", *(u_int16_t *)&sc->sc_ioc.c_hiaddr);
    965 		printf("0x%x", *(u_int32_t *)&sc->sc_ioc.c_addr);
    966 		printf("0x%x", *(u_int16_t *)&sc->sc_ioc.c_nop2);
    967 		printf("0x%x", *(u_int32_t *)&sc->sc_ioc.c_len);
    968 		printf("0x%x\n", *(u_int16_t *)&sc->sc_ioc.c_cmd);
    969 		return (1);
    970 	}
    971 #endif
    972 	/*
    973 	 * Now output a generic message suitable for badsect.
    974 	 * Note that we don't use harderr because it just prints
    975 	 * out b_blkno which is just the beginning block number
    976 	 * of the transfer, not necessary where the error occurred.
    977 	 */
    978 	printf("%s%c: hard error, sector number %" PRId64 "\n",
    979 	    device_xname(&sc->sc_dev), 'a'+RDPART(bp->b_dev), pbn);
    980 	/*
    981 	 * Now report the status as returned by the hardware with
    982 	 * attempt at interpretation.
    983 	 */
    984 	printf("%s %s error:", device_xname(&sc->sc_dev),
    985 	    (bp->b_flags & B_READ) ? "read" : "write");
    986 	printf(" unit %d, volume %d R0x%x F0x%x A0x%x I0x%x\n",
    987 	       css.c_vu&0xF, (css.c_vu>>4)&0xF,
    988 	       css.c_ref, css.c_fef, css.c_aef, css.c_ief);
    989 	printf("P1-P10: ");
    990 	printf("0x%x ", *(u_int32_t *)&css.c_raw[0]);
    991 	printf("0x%x ", *(u_int32_t *)&css.c_raw[4]);
    992 	printf("0x%x\n", *(u_int16_t *)&css.c_raw[8]);
    993 
    994 	return (1);
    995 }
    996 
    997 int
    998 rdread(dev, uio, flags)
    999 	dev_t dev;
   1000 	struct uio *uio;
   1001 	int flags;
   1002 {
   1003 
   1004 	return (physio(rdstrategy, NULL, dev, B_READ, minphys, uio));
   1005 }
   1006 
   1007 int
   1008 rdwrite(dev, uio, flags)
   1009 	dev_t dev;
   1010 	struct uio *uio;
   1011 	int flags;
   1012 {
   1013 
   1014 	return (physio(rdstrategy, NULL, dev, B_WRITE, minphys, uio));
   1015 }
   1016 
   1017 int
   1018 rdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
   1019 {
   1020 	struct rd_softc *sc;
   1021 	struct disklabel *lp;
   1022 	int error, flags;
   1023 
   1024 	sc = device_lookup_private(&rd_cd, RDUNIT(dev));
   1025 	if (sc == NULL)
   1026 		return (ENXIO);
   1027 	lp = sc->sc_dk.dk_label;
   1028 
   1029 	DPRINTF(RDB_FOLLOW, ("rdioctl: sc=%p\n", sc));
   1030 
   1031 	switch (cmd) {
   1032 	case DIOCGDINFO:
   1033 		*(struct disklabel *)data = *lp;
   1034 		return (0);
   1035 
   1036 	case DIOCGPART:
   1037 		((struct partinfo *)data)->disklab = lp;
   1038 		((struct partinfo *)data)->part =
   1039 		    &lp->d_partitions[RDPART(dev)];
   1040 		return (0);
   1041 
   1042 	case DIOCWLABEL:
   1043 		if ((flag & FWRITE) == 0)
   1044 			return (EBADF);
   1045 		if (*(int *)data)
   1046 			sc->sc_flags |= RDF_WLABEL;
   1047 		else
   1048 			sc->sc_flags &= ~RDF_WLABEL;
   1049 		return (0);
   1050 
   1051 	case DIOCSDINFO:
   1052 		if ((flag & FWRITE) == 0)
   1053 			return (EBADF);
   1054 		return (setdisklabel(lp, (struct disklabel *)data,
   1055 		    (sc->sc_flags & RDF_WLABEL) ? 0 : sc->sc_dk.dk_openmask,
   1056 		    (struct cpu_disklabel *)0));
   1057 
   1058 	case DIOCWDINFO:
   1059 		if ((flag & FWRITE) == 0)
   1060 			return (EBADF);
   1061 		error = setdisklabel(lp, (struct disklabel *)data,
   1062 		    (sc->sc_flags & RDF_WLABEL) ? 0 : sc->sc_dk.dk_openmask,
   1063 		    (struct cpu_disklabel *)0);
   1064 		if (error)
   1065 			return (error);
   1066 		flags = sc->sc_flags;
   1067 		sc->sc_flags = RDF_ALIVE | RDF_WLABEL;
   1068 		error = writedisklabel(RDLABELDEV(dev), rdstrategy, lp,
   1069 		    (struct cpu_disklabel *)0);
   1070 		sc->sc_flags = flags;
   1071 		return (error);
   1072 
   1073 	case DIOCGDEFLABEL:
   1074 		rdgetdefaultlabel(sc, (struct disklabel *)data);
   1075 		return (0);
   1076 	}
   1077 	return (EINVAL);
   1078 }
   1079 
   1080 void
   1081 rdgetdefaultlabel(sc, lp)
   1082 	struct rd_softc *sc;
   1083 	struct disklabel *lp;
   1084 {
   1085 	int type = sc->sc_type;
   1086 
   1087 	memset((void *)lp, 0, sizeof(struct disklabel));
   1088 
   1089 	lp->d_type = DTYPE_GPIB;
   1090 	lp->d_secsize = DEV_BSIZE;
   1091 	lp->d_nsectors = rdidentinfo[type].ri_nbpt;
   1092 	lp->d_ntracks = rdidentinfo[type].ri_ntpc;
   1093 	lp->d_ncylinders = rdidentinfo[type].ri_ncyl;
   1094 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
   1095 	lp->d_secperunit = lp->d_ncylinders * lp->d_secpercyl;
   1096 
   1097 	strncpy(lp->d_typename, rdidentinfo[type].ri_desc, 16);
   1098 	strncpy(lp->d_packname, "fictitious", 16);
   1099 	lp->d_rpm = 3000;
   1100 	lp->d_interleave = 1;
   1101 	lp->d_flags = 0;
   1102 
   1103 	lp->d_partitions[RAW_PART].p_offset = 0;
   1104 	lp->d_partitions[RAW_PART].p_size =
   1105 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
   1106 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
   1107 	lp->d_npartitions = RAW_PART + 1;
   1108 
   1109 	lp->d_magic = DISKMAGIC;
   1110 	lp->d_magic2 = DISKMAGIC;
   1111 	lp->d_checksum = dkcksum(lp);
   1112 }
   1113 
   1114 int
   1115 rdsize(dev_t dev)
   1116 {
   1117 	struct rd_softc *sc;
   1118 	int psize, didopen = 0;
   1119 
   1120 	sc = device_lookup_private(&rd_cd, RDUNIT(dev));
   1121 	if (sc == NULL || (sc->sc_flags & RDF_ALIVE) == 0)
   1122 		return (-1);
   1123 
   1124 	/*
   1125 	 * We get called very early on (via swapconf)
   1126 	 * without the device being open so we may need
   1127 	 * to handle it here.
   1128 	 */
   1129 	if (sc->sc_dk.dk_openmask == 0) {
   1130 		if (rdopen(dev, FREAD | FWRITE, S_IFBLK, NULL))
   1131 			return (-1);
   1132 		didopen = 1;
   1133 	}
   1134 	psize = sc->sc_dk.dk_label->d_partitions[RDPART(dev)].p_size *
   1135 	    (sc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
   1136 	if (didopen)
   1137 		(void) rdclose(dev, FREAD | FWRITE, S_IFBLK, NULL);
   1138 	return (psize);
   1139 }
   1140 
   1141 
   1142 static int rddoingadump;	/* simple mutex */
   1143 
   1144 /*
   1145  * Non-interrupt driven, non-dma dump routine.
   1146  */
   1147 int
   1148 rddump(dev_t dev, daddr_t blkno, void *va, size_t size)
   1149 {
   1150 	struct rd_softc *sc;
   1151 	int sectorsize;		/* size of a disk sector */
   1152 	int nsects;		/* number of sectors in partition */
   1153 	int sectoff;		/* sector offset of partition */
   1154 	int totwrt;		/* total number of sectors left to write */
   1155 	int nwrt;		/* current number of sectors to write */
   1156 	int slave;
   1157 	struct disklabel *lp;
   1158 	u_int8_t stat;
   1159 
   1160 	/* Check for recursive dump; if so, punt. */
   1161 	if (rddoingadump)
   1162 		return (EFAULT);
   1163 	rddoingadump = 1;
   1164 
   1165 	sc = device_lookup_private(&rd_cd, RDUNIT(dev));
   1166 	if (sc == NULL || (sc->sc_flags & RDF_ALIVE) == 0)
   1167 		return (ENXIO);
   1168 
   1169 	DPRINTF(RDB_FOLLOW, ("rddump: sc=%p\n", sc));
   1170 
   1171 	slave = sc->sc_slave;
   1172 
   1173 	/*
   1174 	 * Convert to disk sectors.  Request must be a multiple of size.
   1175 	 */
   1176 	lp = sc->sc_dk.dk_label;
   1177 	sectorsize = lp->d_secsize;
   1178 	if ((size % sectorsize) != 0)
   1179 		return (EFAULT);
   1180 	totwrt = size / sectorsize;
   1181 	blkno = dbtob(blkno) / sectorsize;	/* blkno in DEV_BSIZE units */
   1182 
   1183 	nsects = lp->d_partitions[RDPART(dev)].p_size;
   1184 	sectoff = lp->d_partitions[RDPART(dev)].p_offset;
   1185 
   1186 	/* Check transfer bounds against partition size. */
   1187 	if ((blkno < 0) || (blkno + totwrt) > nsects)
   1188 		return (EINVAL);
   1189 
   1190 	/* Offset block number to start of partition. */
   1191 	blkno += sectoff;
   1192 
   1193 	while (totwrt > 0) {
   1194 		nwrt = totwrt;		/* XXX */
   1195 #ifndef RD_DUMP_NOT_TRUSTED
   1196 		/*
   1197 		 * Fill out and send GPIB command.
   1198 		 */
   1199 		sc->sc_ioc.c_unit = CS80CMD_SUNIT(sc->sc_punit);
   1200 		sc->sc_ioc.c_volume = CS80CMD_SVOL(0);
   1201 		sc->sc_ioc.c_saddr = CS80CMD_SADDR;
   1202 		sc->sc_ioc.c_hiaddr = 0;
   1203 		sc->sc_ioc.c_addr = RDBTOS(blkno);
   1204 		sc->sc_ioc.c_nop2 = CS80CMD_NOP;
   1205 		sc->sc_ioc.c_slen = CS80CMD_SLEN;
   1206 		sc->sc_ioc.c_len = nwrt * sectorsize;
   1207 		sc->sc_ioc.c_cmd = CS80CMD_WRITE;
   1208 		(void) gpibsend(sc->sc_ic, slave, CS80CMD_SCMD,
   1209 		    &sc->sc_ioc.c_unit, sizeof(sc->sc_ioc)-3);
   1210 		if (gpibswait(sc->sc_ic, slave))
   1211 			return (EIO);
   1212 		/*
   1213 		 * Send the data.
   1214 		 */
   1215 		(void) gpibsend(sc->sc_ic, slave, CS80CMD_EXEC, va,
   1216 		    nwrt * sectorsize);
   1217 		(void) gpibswait(sc->sc_ic, slave);
   1218 		(void) gpibrecv(sc->sc_ic, slave, CS80CMD_QSTAT, &stat, 1);
   1219 		if (stat)
   1220 			return (EIO);
   1221 #else /* RD_DUMP_NOT_TRUSTED */
   1222 		/* Let's just talk about this first... */
   1223 		printf("%s: dump addr %p, blk %d\n", device_xname(&sc->sc_dev),
   1224 		    va, blkno);
   1225 		delay(500 * 1000);	/* half a second */
   1226 #endif /* RD_DUMP_NOT_TRUSTED */
   1227 
   1228 		/* update block count */
   1229 		totwrt -= nwrt;
   1230 		blkno += nwrt;
   1231 		va += sectorsize * nwrt;
   1232 	}
   1233 	rddoingadump = 0;
   1234 	return (0);
   1235 }
   1236