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