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