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