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