xref: /netbsd-src/sys/dev/gpib/rd.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: rd.c,v 1.1 2003/06/02 03:53:02 gmcgarry Exp $ */
2 
3 /*-
4  * Copyright (c) 1996-2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Copyright (c) 1988 University of Utah.
41  * Copyright (c) 1982, 1990, 1993
42  *	The Regents of the University of California.  All rights reserved.
43  *
44  * This code is derived from software contributed to Berkeley by
45  * the Systems Programming Group of the University of Utah Computer
46  * Science Department.
47  *
48  * Redistribution and use in source and binary forms, with or without
49  * modification, are permitted provided that the following conditions
50  * are met:
51  * 1. Redistributions of source code must retain the above copyright
52  *    notice, this list of conditions and the following disclaimer.
53  * 2. Redistributions in binary form must reproduce the above copyright
54  *    notice, this list of conditions and the following disclaimer in the
55  *    documentation and/or other materials provided with the distribution.
56  * 3. All advertising materials mentioning features or use of this software
57  *    must display the following acknowledgement:
58  *	This product includes software developed by the University of
59  *	California, Berkeley and its contributors.
60  * 4. Neither the name of the University nor the names of its contributors
61  *    may be used to endorse or promote products derived from this software
62  *    without specific prior written permission.
63  *
64  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
65  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
66  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
67  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
68  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
69  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
70  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
71  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
72  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
73  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
74  * SUCH DAMAGE.
75  *
76  * from: Utah $Hdr: rd.c 1.44 92/12/26$
77  *
78  *	@(#)rd.c	8.2 (Berkeley) 5/19/94
79  */
80 
81 /*
82  * CS80/SS80 disk driver
83  */
84 
85 #include <sys/cdefs.h>
86 __KERNEL_RCSID(0, "$NetBSD: rd.c,v 1.1 2003/06/02 03:53:02 gmcgarry Exp $");
87 
88 #include "rnd.h"
89 
90 #include <sys/param.h>
91 #include <sys/systm.h>
92 #include <sys/buf.h>
93 #include <sys/callout.h>
94 #include <sys/conf.h>
95 #include <sys/device.h>
96 #include <sys/disk.h>
97 #include <sys/disklabel.h>
98 #include <sys/endian.h>
99 #include <sys/fcntl.h>
100 #include <sys/ioctl.h>
101 #include <sys/proc.h>
102 #include <sys/stat.h>
103 
104 #if NRND > 0
105 #include <sys/rnd.h>
106 #endif
107 
108 #include <dev/gpib/gpibvar.h>
109 #include <dev/gpib/cs80busvar.h>
110 
111 #include <dev/gpib/rdreg.h>
112 
113 #ifdef DEBUG
114 int	rddebug = 0xff;
115 #define RDB_FOLLOW	0x01
116 #define RDB_STATUS	0x02
117 #define RDB_IDENT	0x04
118 #define RDB_IO		0x08
119 #define RDB_ASYNC	0x10
120 #define RDB_ERROR	0x80
121 #define DPRINTF(mask, str)	if (rddebug & (mask)) printf str
122 #else
123 #define DPRINTF(mask, str)	/* nothing */
124 #endif
125 
126 struct	rd_softc {
127 	struct	device sc_dev;
128 	gpib_chipset_tag_t sc_ic;
129 	gpib_handle_t sc_hdl;
130 
131 	struct	disk sc_dk;
132 
133 	int	sc_slave;		/* GPIB slave */
134 	int	sc_punit;		/* physical unit on slave */
135 
136 	int	sc_flags;
137 #define	RDF_ALIVE	0x01
138 #define	RDF_SEEK	0x02
139 #define RDF_SWAIT	0x04
140 #define RDF_OPENING	0x08
141 #define RDF_CLOSING	0x10
142 #define RDF_WANTED	0x20
143 #define RDF_WLABEL	0x40
144 
145 	u_int16_t sc_type;
146 	u_int8_t *sc_addr;
147 	int	sc_resid;
148 	struct	rd_iocmd sc_ioc;
149 	struct	bufq_state sc_tab;
150 	int	sc_active;
151 	int	sc_errcnt;
152 
153 	struct	callout sc_restart_ch;
154 
155 #if NRND > 0
156 	rndsource_element_t rnd_source;
157 #endif
158 };
159 
160 #define RDUNIT(dev)			DISKUNIT(dev)
161 #define RDPART(dev)			DISKPART(dev)
162 #define RDMAKEDEV(maj, unit, part)	MAKEDISKDEV(maj, unit, part)
163 #define RDLABELDEV(dev)	(RDMAKEDEV(major(dev), RDUNIT(dev), RAW_PART))
164 
165 #define	RDRETRY		5
166 #define RDWAITC		1	/* min time for timeout in seconds */
167 
168 int	rderrthresh = RDRETRY-1;	/* when to start reporting errors */
169 
170 /*
171  * Misc. HW description, indexed by sc_type.
172  * Used for mapping 256-byte sectors for 512-byte sectors
173  */
174 const struct rdidentinfo {
175 	u_int16_t ri_hwid;		/* 2 byte HW id */
176 	u_int16_t ri_maxunum;		/* maximum allowed unit number */
177 	char	*ri_desc;		/* drive type description */
178 	int	ri_nbpt;		/* DEV_BSIZE blocks per track */
179 	int	ri_ntpc;		/* tracks per cylinder */
180 	int	ri_ncyl;		/* cylinders per unit */
181 	int	ri_nblocks;		/* DEV_BSIZE blocks on disk */
182 } rdidentinfo[] = {
183 	{ RD7946AID,	0,	"7945A",	NRD7945ABPT,
184 	  NRD7945ATRK,	968,	 108416 },
185 
186 	{ RD9134DID,	1,	"9134D",	NRD9134DBPT,
187 	  NRD9134DTRK,	303,	  29088 },
188 
189 	{ RD9134LID,	1,	"9122S",	NRD9122SBPT,
190 	  NRD9122STRK,	77,	   1232 },
191 
192 	{ RD7912PID,	0,	"7912P",	NRD7912PBPT,
193 	  NRD7912PTRK,	572,	 128128 },
194 
195 	{ RD7914PID,	0,	"7914P",	NRD7914PBPT,
196 	  NRD7914PTRK,	1152,	 258048 },
197 
198 	{ RD7958AID,	0,	"7958A",	NRD7958ABPT,
199 	  NRD7958ATRK,	1013,	 255276 },
200 
201 	{ RD7957AID,	0,	"7957A",	NRD7957ABPT,
202 	  NRD7957ATRK,	1036,	 159544 },
203 
204 	{ RD7933HID,	0,	"7933H",	NRD7933HBPT,
205 	  NRD7933HTRK,	1321,	 789958 },
206 
207 	{ RD9134LID,	1,	"9134L",	NRD9134LBPT,
208 	  NRD9134LTRK,	973,	  77840 },
209 
210 	{ RD7936HID,	0,	"7936H",	NRD7936HBPT,
211 	  NRD7936HTRK,	698,	 600978 },
212 
213 	{ RD7937HID,	0,	"7937H",	NRD7937HBPT,
214 	  NRD7937HTRK,	698,	1116102 },
215 
216 	{ RD7914CTID,	0,	"7914CT",	NRD7914PBPT,
217 	  NRD7914PTRK,	1152,	 258048 },
218 
219 	{ RD7946AID,	0,	"7946A",	NRD7945ABPT,
220 	  NRD7945ATRK,	968,	 108416 },
221 
222 	{ RD9134LID,	1,	"9122D",	NRD9122SBPT,
223 	  NRD9122STRK,	77,	   1232 },
224 
225 	{ RD7957BID,	0,	"7957B",	NRD7957BBPT,
226 	  NRD7957BTRK,	1269,	 159894 },
227 
228 	{ RD7958BID,	0,	"7958B",	NRD7958BBPT,
229 	  NRD7958BTRK,	786,	 297108 },
230 
231 	{ RD7959BID,	0,	"7959B",	NRD7959BBPT,
232 	  NRD7959BTRK,	1572,	 594216 },
233 
234 	{ RD2200AID,	0,	"2200A",	NRD2200ABPT,
235 	  NRD2200ATRK,	1449,	 654948 },
236 
237 	{ RD2203AID,	0,	"2203A",	NRD2203ABPT,
238 	  NRD2203ATRK,	1449,	1309896 }
239 };
240 int numrdidentinfo = sizeof(rdidentinfo) / sizeof(rdidentinfo[0]);
241 
242 int	rdlookup(int, int, int);
243 int	rdgetinfo(struct rd_softc *);
244 void	rdrestart(void *);
245 struct buf *rdfinish(struct rd_softc *, struct buf *);
246 
247 void	rdgetcompatlabel(struct rd_softc *, struct disklabel *);
248 void	rdgetdefaultlabel(struct rd_softc *, struct disklabel *);
249 void	rdrestart(void *);
250 void	rdustart(struct rd_softc *);
251 struct buf *rdfinish(struct rd_softc *, struct buf *);
252 void	rdcallback(void *, int);
253 void	rdstart(struct rd_softc *);
254 void	rdintr(struct rd_softc *);
255 int	rderror(struct rd_softc *);
256 
257 int	rdmatch(struct device *, struct cfdata *, void *);
258 void	rdattach(struct device *, struct device *, void *);
259 
260 CFATTACH_DECL(rd, sizeof(struct rd_softc),
261 	rdmatch, rdattach, NULL, NULL);
262 
263 
264 dev_type_open(rdopen);
265 dev_type_close(rdclose);
266 dev_type_read(rdread);
267 dev_type_write(rdwrite);
268 dev_type_ioctl(rdioctl);
269 dev_type_strategy(rdstrategy);
270 dev_type_dump(rddump);
271 dev_type_size(rdsize);
272 
273 const struct bdevsw rd_bdevsw = {
274 	rdopen, rdclose, rdstrategy, rdioctl, rddump, rdsize, D_DISK
275 };
276 
277 const struct cdevsw rd_cdevsw = {
278 	rdopen, rdclose, rdread, rdwrite, rdioctl,
279 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
280 };
281 
282 extern struct cfdriver rd_cd;
283 
284 int
285 rdlookup(id, slave, punit)
286 	int id;
287 	int slave;
288 	int punit;
289 {
290 	int i;
291 
292 	for (i = 0; i < numrdidentinfo; i++) {
293 		if (rdidentinfo[i].ri_hwid == id)
294 			break;
295 	}
296 	if (i == numrdidentinfo || punit > rdidentinfo[i].ri_maxunum)
297 		return (-1);
298 	return (i);
299 }
300 
301 int
302 rdmatch(parent, match, aux)
303 	struct device *parent;
304 	struct cfdata *match;
305 	void *aux;
306 {
307 	struct cs80bus_attach_args *ca = aux;
308 
309 	if (rdlookup(ca->ca_id, ca->ca_slave, ca->ca_punit) < 0)
310 		return (0);
311 	return (1);
312 }
313 
314 void
315 rdattach(parent, self, aux)
316 	struct device *parent, *self;
317 	void *aux;
318 {
319 	struct rd_softc *sc = (struct rd_softc *)self;
320 	struct cs80bus_attach_args *ca = aux;
321 	struct cs80_description csd;
322 	char name[7];
323 	int type, i, n;
324 
325 	sc->sc_ic = ca->ca_ic;
326 	sc->sc_slave = ca->ca_slave;
327 	sc->sc_punit = ca->ca_punit;
328 
329 	if ((type = rdlookup(ca->ca_id, ca->ca_slave, ca->ca_punit)) < 0)
330 		return;
331 
332 	if (cs80reset(parent, sc->sc_slave, sc->sc_punit)) {
333 		printf("\n%s: can't reset device\n", sc->sc_dev.dv_xname);
334 		return;
335 	}
336 
337 	if (cs80describe(parent, sc->sc_slave, sc->sc_punit, &csd)) {
338 		printf("\n%s: didn't respond to describe command\n",
339 		    sc->sc_dev.dv_xname);
340 		return;
341 	}
342 	memset(name, 0, sizeof(name));
343 	for (i=0, n=0; i<3; i++) {
344 		name[n++] = (csd.d_name[i] >> 4) + '0';
345 		name[n++] = (csd.d_name[i] & 0x0f) + '0';
346 	}
347 
348 #ifdef DEBUG
349 	if (rddebug & RDB_IDENT) {
350 		printf("\n%s: name: ('%s')\n",
351 		    sc->sc_dev.dv_xname, name);
352 		printf("  iuw %x, maxxfr %d, ctype %d\n",
353 		    csd.d_iuw, csd.d_cmaxxfr, csd.d_ctype);
354 		printf("  utype %d, bps %d, blkbuf %d, burst %d, blktime %d\n",
355 		    csd.d_utype, csd.d_sectsize,
356 		    csd.d_blkbuf, csd.d_burstsize, csd.d_blocktime);
357 		printf("  avxfr %d, ort %d, atp %d, maxint %d, fv %x, rv %x\n",
358 		    csd.d_uavexfr, csd.d_retry, csd.d_access,
359 		    csd.d_maxint, csd.d_fvbyte, csd.d_rvbyte);
360 		printf("  maxcyl/head/sect %d/%d/%d, maxvsect %d, inter %d\n",
361 		    csd.d_maxcylhead >> 8, csd.d_maxcylhead & 0xff,
362 		    csd.d_maxsect, csd.d_maxvsectl, csd.d_interleave);
363 		printf("%s", sc->sc_dev.dv_xname);
364 	}
365 #endif
366 
367 	/*
368 	 * Take care of a couple of anomolies:
369 	 * 1. 7945A and 7946A both return same HW id
370 	 * 2. 9122S and 9134D both return same HW id
371 	 * 3. 9122D and 9134L both return same HW id
372 	 */
373 	switch (ca->ca_id) {
374 	case RD7946AID:
375 		if (memcmp(name, "079450", 6) == 0)
376 			type = RD7945A;
377 		else
378 			type = RD7946A;
379 		break;
380 
381 	case RD9134LID:
382 		if (memcmp(name, "091340", 6) == 0)
383 			type = RD9134L;
384 		else
385 			type = RD9122D;
386 		break;
387 
388 	case RD9134DID:
389 		if (memcmp(name, "091220", 6) == 0)
390 			type = RD9122S;
391 		else
392 			type = RD9134D;
393 		break;
394 	}
395 
396 	sc->sc_type = type;
397 
398 	/*
399 	 * XXX We use DEV_BSIZE instead of the sector size value pulled
400 	 * XXX off the driver because all of this code assumes 512 byte
401 	 * XXX blocks.  ICK!
402 	 */
403 	printf(": %s\n", rdidentinfo[type].ri_desc);
404 	printf("%s: %d cylinders, %d heads, %d blocks, %d bytes/block\n",
405 	    sc->sc_dev.dv_xname, rdidentinfo[type].ri_ncyl,
406 	    rdidentinfo[type].ri_ntpc, rdidentinfo[type].ri_nblocks,
407 	    DEV_BSIZE);
408 
409 	bufq_alloc(&sc->sc_tab, BUFQ_FCFS);
410 
411 	/*
412 	 * Initialize and attach the disk structure.
413 	 */
414 	memset(&sc->sc_dk, 0, sizeof(sc->sc_dk));
415 	sc->sc_dk.dk_name = sc->sc_dev.dv_xname;
416 	disk_attach(&sc->sc_dk);
417 
418 	callout_init(&sc->sc_restart_ch);
419 
420 	if (gpibregister(sc->sc_ic, sc->sc_slave, rdcallback, sc,
421 	    &sc->sc_hdl)) {
422 		printf("%s: can't register callback\n", sc->sc_dev.dv_xname);
423 		return;
424 	}
425 
426 	sc->sc_flags = RDF_ALIVE;
427 #ifdef DEBUG
428 	/* always report errors */
429 	if (rddebug & RDB_ERROR)
430 		rderrthresh = 0;
431 #endif
432 #if NRND > 0
433 	/*
434 	 * attach the device into the random source list
435 	 */
436 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
437 			  RND_TYPE_DISK, 0);
438 #endif
439 }
440 
441 /*
442  * Read or constuct a disklabel
443  */
444 int
445 rdgetinfo(sc)
446 	struct rd_softc *sc;
447 {
448 	struct disklabel *lp = sc->sc_dk.dk_label;
449 	struct partition *pi;
450 	const char *msg;
451 
452 	memset(sc->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
453 
454 	rdgetdefaultlabel(sc, lp);
455 
456 	/*
457 	 * Call the generic disklabel extraction routine
458 	 */
459 	msg = readdisklabel(RDMAKEDEV(0, sc->sc_dev.dv_unit, RAW_PART),
460 	    rdstrategy, lp, NULL);
461 	if (msg == NULL)
462 		return (0);
463 
464 	pi = lp->d_partitions;
465 	printf("%s: WARNING: %s, ", sc->sc_dev.dv_xname, msg);
466 #ifdef COMPAT_NOLABEL
467 	printf("using old default partitioning\n");
468 	rdgetcompatlabel(sc, lp);
469 #else
470 	printf("defining '%c' partition as entire disk\n", 'a' + RAW_PART);
471 	pi[RAW_PART].p_size = rdidentinfo[sc->sc_type].ri_nblocks;
472 	lp->d_npartitions = RAW_PART+1;
473 	pi[0].p_size = 0;
474 #endif
475 	return (0);
476 }
477 
478 int
479 rdopen(dev, flags, mode, p)
480 	dev_t dev;
481 	int flags, mode;
482 	struct proc *p;
483 {
484 	struct rd_softc *sc;
485 	int error, mask, part;
486 
487 	sc = device_lookup(&rd_cd, RDUNIT(dev));
488 	if (sc == NULL || (sc->sc_flags & RDF_ALIVE) ==0)
489 		return (ENXIO);
490 
491 	/*
492 	 * Wait for any pending opens/closes to complete
493 	 */
494 	while (sc->sc_flags & (RDF_OPENING | RDF_CLOSING))
495 		(void) tsleep(sc, PRIBIO, "rdopen", 0);
496 
497 	/*
498 	 * On first open, get label and partition info.
499 	 * We may block reading the label, so be careful
500 	 * to stop any other opens.
501 	 */
502 	if (sc->sc_dk.dk_openmask == 0) {
503 		sc->sc_flags |= RDF_OPENING;
504 		error = rdgetinfo(sc);
505 		sc->sc_flags &= ~RDF_OPENING;
506 		wakeup((caddr_t)sc);
507 		if (error)
508 			return (error);
509 	}
510 
511 	part = RDPART(dev);
512 	mask = 1 << part;
513 
514 	/* Check that the partition exists. */
515 	if (part != RAW_PART && (part > sc->sc_dk.dk_label->d_npartitions ||
516 	    sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED))
517 		return (ENXIO);
518 
519 	/* Ensure only one open at a time. */
520 	switch (mode) {
521 	case S_IFCHR:
522 		sc->sc_dk.dk_copenmask |= mask;
523 		break;
524 	case S_IFBLK:
525 		sc->sc_dk.dk_bopenmask |= mask;
526 		break;
527 	}
528 	sc->sc_dk.dk_openmask =
529 	    sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
530 
531 	return (0);
532 }
533 
534 int
535 rdclose(dev, flag, mode, p)
536 	dev_t dev;
537 	int flag, mode;
538 	struct proc *p;
539 {
540 	struct rd_softc *sc;
541 	struct disk *dk;
542 	int mask, s;
543 
544 	sc = device_lookup(&rd_cd, RDUNIT(dev));
545 	if (sc == NULL)
546 		return (ENXIO);
547 
548 	dk = &sc->sc_dk;
549 
550 	mask = 1 << RDPART(dev);
551 	if (mode == S_IFCHR)
552 		dk->dk_copenmask &= ~mask;
553 	else
554 		dk->dk_bopenmask &= ~mask;
555 	dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
556 	/*
557 	 * On last close, we wait for all activity to cease since
558 	 * the label/parition info will become invalid.  Since we
559 	 * might sleep, we must block any opens while we are here.
560 	 * Note we don't have to about other closes since we know
561 	 * we are the last one.
562 	 */
563 	if (dk->dk_openmask == 0) {
564 		sc->sc_flags |= RDF_CLOSING;
565 		s = splbio();
566 		while (sc->sc_active) {
567 			sc->sc_flags |= RDF_WANTED;
568 			(void) tsleep(&sc->sc_tab, PRIBIO, "rdclose", 0);
569 		}
570 		splx(s);
571 		sc->sc_flags &= ~(RDF_CLOSING | RDF_WLABEL);
572 		wakeup((caddr_t)sc);
573 	}
574 	return (0);
575 }
576 
577 void
578 rdstrategy(bp)
579 	struct buf *bp;
580 {
581 	struct rd_softc *sc;
582 	struct partition *pinfo;
583 	daddr_t bn;
584 	int sz, s;
585 	int offset;
586 
587 	sc = device_lookup(&rd_cd, RDUNIT(bp->b_dev));
588 
589 	DPRINTF(RDB_FOLLOW,
590 	    ("rdstrategy(%p): dev %x, bn %" PRId64 ", bcount %ld, %c\n",
591 	    bp, bp->b_dev, bp->b_blkno, bp->b_bcount,
592 	    (bp->b_flags & B_READ) ? 'R' : 'W'));
593 
594 	bn = bp->b_blkno;
595 	sz = howmany(bp->b_bcount, DEV_BSIZE);
596 	pinfo = &sc->sc_dk.dk_label->d_partitions[RDPART(bp->b_dev)];
597 
598 	/* Don't perform partition translation on RAW_PART. */
599 	offset = (RDPART(bp->b_dev) == RAW_PART) ? 0 : pinfo->p_offset;
600 
601 	if (RDPART(bp->b_dev) != RAW_PART) {
602 		/*
603 		 * XXX This block of code belongs in
604 		 * XXX bounds_check_with_label()
605 		 */
606 
607 		if (bn < 0 || bn + sz > pinfo->p_size) {
608 			sz = pinfo->p_size - bn;
609 			if (sz == 0) {
610 				bp->b_resid = bp->b_bcount;
611 				goto done;
612 			}
613 			if (sz < 0) {
614 				bp->b_error = EINVAL;
615 				goto bad;
616 			}
617 			bp->b_bcount = dbtob(sz);
618 		}
619 		/*
620 		 * Check for write to write protected label
621 		 */
622 		if (bn + offset <= LABELSECTOR &&
623 #if LABELSECTOR != 0
624 		    bn + offset + sz > LABELSECTOR &&
625 #endif
626 		    !(bp->b_flags & B_READ) && !(sc->sc_flags & RDF_WLABEL)) {
627 			bp->b_error = EROFS;
628 			goto bad;
629 		}
630 	}
631 	bp->b_rawblkno = bn + offset;
632 	s = splbio();
633 	BUFQ_PUT(&sc->sc_tab, bp);
634 	if (sc->sc_active == 0) {
635 		sc->sc_active = 1;
636 		rdustart(sc);
637 	}
638 	splx(s);
639 	return;
640 bad:
641 	bp->b_flags |= B_ERROR;
642 done:
643 	biodone(bp);
644 }
645 
646 /*
647  * Called from timeout() when handling maintenance releases
648  * callout from timeouts
649  */
650 void
651 rdrestart(arg)
652 	void *arg;
653 {
654 	int s = splbio();
655 	rdustart((struct rd_softc *)arg);
656 	splx(s);
657 }
658 
659 
660 /* called by rdstrategy() to start a block transfer */
661 /* called by rdrestart() when handingly timeouts */
662 /* called by rdintr() */
663 void
664 rdustart(sc)
665 	struct rd_softc *sc;
666 {
667 	struct buf *bp;
668 
669 	bp = BUFQ_PEEK(&sc->sc_tab);
670 	sc->sc_addr = bp->b_data;
671 	sc->sc_resid = bp->b_bcount;
672 	if (gpibrequest(sc->sc_ic, sc->sc_hdl))
673 		rdstart(sc);
674 }
675 
676 struct buf *
677 rdfinish(sc, bp)
678 	struct rd_softc *sc;
679 	struct buf *bp;
680 {
681 
682 	sc->sc_errcnt = 0;
683 	(void)BUFQ_GET(&sc->sc_tab);
684 	bp->b_resid = 0;
685 	biodone(bp);
686 	gpibrelease(sc->sc_ic, sc->sc_hdl);
687 	if ((bp = BUFQ_PEEK(&sc->sc_tab)) != NULL)
688 		return (bp);
689 	sc->sc_active = 0;
690 	if (sc->sc_flags & RDF_WANTED) {
691 		sc->sc_flags &= ~RDF_WANTED;
692 		wakeup((caddr_t)&sc->sc_tab);
693 	}
694 	return (NULL);
695 }
696 
697 void
698 rdcallback(v, action)
699 	void *v;
700 	int action;
701 {
702 	struct rd_softc *sc = v;
703 
704 	DPRINTF(RDB_FOLLOW, ("rdcallback: v=%p, action=%d\n", v, action));
705 
706 	switch (action) {
707 	case GPIBCBF_START:
708 		rdstart(sc);
709 		break;
710 	case GPIBCBF_INTR:
711 		rdintr(sc);
712 		break;
713 #ifdef DEBUG
714 	default:
715 		DPRINTF(RDB_ERROR, ("rdcallback: unknown action %d\n",
716 		    action));
717 		break;
718 #endif
719 	}
720 }
721 
722 
723 /* called from rdustart() to start a transfer */
724 /* called from gpib interface as the initiator */
725 void
726 rdstart(sc)
727 	struct rd_softc *sc;
728 {
729 	struct buf *bp = BUFQ_PEEK(&sc->sc_tab);
730 	int part, slave, punit;
731 
732 	slave = sc->sc_slave;
733 	punit = sc->sc_punit;
734 
735 	DPRINTF(RDB_FOLLOW, ("rdstart(%s): bp %p, %c\n",
736 	    sc->sc_dev.dv_xname, bp, (bp->b_flags & B_READ) ? 'R' : 'W'));
737 
738 again:
739 
740 	part = RDPART(bp->b_dev);
741 	sc->sc_flags |= RDF_SEEK;
742 	sc->sc_ioc.c_unit = CS80CMD_SUNIT(punit);
743 	sc->sc_ioc.c_volume = CS80CMD_SVOL(0);
744 	sc->sc_ioc.c_saddr = CS80CMD_SADDR;
745 	sc->sc_ioc.c_hiaddr = htobe16(0);
746 	sc->sc_ioc.c_addr = htobe32(RDBTOS(bp->b_rawblkno));
747 	sc->sc_ioc.c_nop2 = CS80CMD_NOP;
748 	sc->sc_ioc.c_slen = CS80CMD_SLEN;
749 	sc->sc_ioc.c_len = htobe32(sc->sc_resid);
750 	sc->sc_ioc.c_cmd = bp->b_flags & B_READ ? CS80CMD_READ : CS80CMD_WRITE;
751 
752 	if (gpibsend(sc->sc_ic, slave, CS80CMD_SCMD, &sc->sc_ioc.c_unit,
753 	    sizeof(sc->sc_ioc)-1) == sizeof(sc->sc_ioc)-1) {
754 		/* Instrumentation. */
755 		disk_busy(&sc->sc_dk);
756 		sc->sc_dk.dk_seek++;
757 		gpibawait(sc->sc_ic);
758 		return;
759 	}
760 	/*
761 	 * Experience has shown that the gpibwait in this gpibsend will
762 	 * occasionally timeout.  It appears to occur mostly on old 7914
763 	 * drives with full maintenance tracks.  We should probably
764 	 * integrate this with the backoff code in rderror.
765 	 */
766 
767 	DPRINTF(RDB_ERROR,
768 	    ("rdstart: cmd %x adr %ul blk %" PRId64 " len %d ecnt %d\n",
769 	    sc->sc_ioc.c_cmd, sc->sc_ioc.c_addr, bp->b_blkno, sc->sc_resid,
770 	     sc->sc_errcnt));
771 
772 	sc->sc_flags &= ~RDF_SEEK;
773 	cs80reset(sc->sc_dev.dv_parent, slave, punit);
774 	if (sc->sc_errcnt++ < RDRETRY)
775 		goto again;
776 	printf("%s: rdstart err: cmd 0x%x sect %uld blk %" PRId64 " len %d\n",
777 	       sc->sc_dev.dv_xname, sc->sc_ioc.c_cmd, sc->sc_ioc.c_addr,
778 	       bp->b_blkno, sc->sc_resid);
779 	bp->b_flags |= B_ERROR;
780 	bp->b_error = EIO;
781 	bp = rdfinish(sc, bp);
782 	if (bp) {
783 		sc->sc_addr = bp->b_data;
784 		sc->sc_resid = bp->b_bcount;
785 		if (gpibrequest(sc->sc_ic, sc->sc_hdl))
786 			goto again;
787 	}
788 }
789 
790 void
791 rdintr(sc)
792 	struct rd_softc *sc;
793 {
794 	struct buf *bp;
795 	u_int8_t stat = 13;	/* in case gpibrecv fails */
796 	int rv, dir, restart, slave;
797 
798 	slave = sc->sc_slave;
799 	bp = BUFQ_PEEK(&sc->sc_tab);
800 
801 	DPRINTF(RDB_FOLLOW, ("rdintr(%s): bp %p, %c, flags %x\n",
802 	    sc->sc_dev.dv_xname, bp, (bp->b_flags & B_READ) ? 'R' : 'W',
803 	    sc->sc_flags));
804 
805 	disk_unbusy(&sc->sc_dk, (bp->b_bcount - bp->b_resid),
806 		(bp->b_flags & B_READ));
807 
808 	if (sc->sc_flags & RDF_SEEK) {
809 		sc->sc_flags &= ~RDF_SEEK;
810 		dir = (bp->b_flags & B_READ ? GPIB_READ : GPIB_WRITE);
811 		gpibxfer(sc->sc_ic, slave, CS80CMD_EXEC, sc->sc_addr,
812 		    sc->sc_resid, dir, dir == GPIB_READ);
813 		disk_busy(&sc->sc_dk);
814 		return;
815 	}
816 	if ((sc->sc_flags & RDF_SWAIT) == 0) {
817 		if (gpibpptest(sc->sc_ic, slave) == 0) {
818 			/* Instrumentation. */
819 			disk_busy(&sc->sc_dk);
820 			sc->sc_flags |= RDF_SWAIT;
821 			gpibawait(sc->sc_ic);
822 			return;
823 		}
824 	} else
825 		sc->sc_flags &= ~RDF_SWAIT;
826 	rv = gpibrecv(sc->sc_ic, slave, CS80CMD_QSTAT, &stat, 1);
827 	if (rv != 1 || stat) {
828 		DPRINTF(RDB_ERROR,
829 		    ("rdintr: receive failed (rv=%d) or bad stat %d\n", rv,
830 		     stat));
831 		restart = rderror(sc);
832 		if (sc->sc_errcnt++ < RDRETRY) {
833 			if (restart)
834 				rdstart(sc);
835 			return;
836 		}
837 		bp->b_flags |= B_ERROR;
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(sc)
854 	struct rd_softc *sc;
855 {
856 	struct cs80_stat css;
857 	struct buf *bp;
858 	daddr_t hwbn, pbn;
859 
860 	DPRINTF(RDB_FOLLOW, ("rderror: sc=%p\n", sc));
861 
862 	if (cs80status(sc->sc_dev.dv_parent, sc->sc_slave,
863 	    sc->sc_punit, &css)) {
864 		cs80reset(sc->sc_dev.dv_parent, sc->sc_slave, 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(sc->sc_dev.dv_parent, sc->sc_slave, sc->sc_punit);
885 		return (1);
886 	}
887 	/*
888 	 * Unit requests release for internal maintenance.
889 	 * We just delay awhile and try again later.  Use expontially
890 	 * increasing backoff ala ethernet drivers since we don't really
891 	 * know how long the maintenance will take.  With RDWAITC and
892 	 * RDRETRY as defined, the range is 1 to 32 seconds.
893 	 */
894 	if (css.c_fef & FEF_IMR) {
895 		extern int hz;
896 		int rdtimo = RDWAITC << sc->sc_errcnt;
897 		DPRINTF(RDB_STATUS,
898 		    ("%s: internal maintenance, %d-second timeout\n",
899 		    sc->sc_dev.dv_xname, rdtimo));
900 		gpibrelease(sc->sc_ic, sc->sc_hdl);
901 		callout_reset(&sc->sc_restart_ch, rdtimo * hz, rdrestart, sc);
902 		return (0);
903 	}
904 	/*
905 	 * Only report error if we have reached the error reporting
906 	 * threshhold.  By default, this will only report after the
907 	 * retry limit has been exceeded.
908 	 */
909 	if (sc->sc_errcnt < rderrthresh)
910 		return (1);
911 
912 	/*
913 	 * First conjure up the block number at which the error occurred.
914  	 */
915 	bp = BUFQ_PEEK(&sc->sc_tab);
916 	pbn = sc->sc_dk.dk_label->d_partitions[RDPART(bp->b_dev)].p_offset;
917 	if ((css.c_fef & FEF_CU) || (css.c_fef & FEF_DR) ||
918 	    (css.c_ief & IEF_RRMASK)) {
919 		/*
920 		 * Not all errors report a block number, just use b_blkno.
921 		 */
922 		hwbn = RDBTOS(pbn + bp->b_blkno);
923 		pbn = bp->b_blkno;
924 	} else {
925 		hwbn = css.c_blk;
926 		pbn = RDSTOB(hwbn) - pbn;
927 	}
928 #ifdef DEBUG
929 	if (rddebug & RDB_ERROR) {			/* status info */
930 		printf("\n    volume: %d, unit: %d\n",
931 		       (css.c_vu>>4)&0xF, css.c_vu&0xF);
932 		printf("    reject 0x%x\n", css.c_ref);
933 		printf("    fault 0x%x\n", css.c_fef);
934 		printf("    access 0x%x\n", css.c_aef);
935 		printf("    info 0x%x\n", css.c_ief);
936 		printf("    block,  P1-P10: ");
937 		printf("    block: %" PRId64 ", P1-P10: ", hwbn);
938 		printf("0x%x", *(u_int32_t *)&css.c_raw[0]);
939 		printf("0x%x", *(u_int32_t *)&css.c_raw[4]);
940 		printf("0x%x\n", *(u_int16_t *)&css.c_raw[8]);
941 	}
942 #endif
943 #ifdef DEBUG
944 	if (rddebug & RDB_ERROR) {			/* command */
945 		printf("    ioc: ");
946 		printf("0x%x", *(u_int32_t *)&sc->sc_ioc.c_pad);
947 		printf("0x%x", *(u_int16_t *)&sc->sc_ioc.c_hiaddr);
948 		printf("0x%x", *(u_int32_t *)&sc->sc_ioc.c_addr);
949 		printf("0x%x", *(u_int16_t *)&sc->sc_ioc.c_nop2);
950 		printf("0x%x", *(u_int32_t *)&sc->sc_ioc.c_len);
951 		printf("0x%x\n", *(u_int16_t *)&sc->sc_ioc.c_cmd);
952 		return (1);
953 	}
954 #endif
955 	/*
956 	 * Now output a generic message suitable for badsect.
957 	 * Note that we don't use harderr because it just prints
958 	 * out b_blkno which is just the beginning block number
959 	 * of the transfer, not necessary where the error occurred.
960 	 */
961 	printf("%s%c: hard error, sector number %" PRId64 "\n",
962 	    sc->sc_dev.dv_xname, 'a'+RDPART(bp->b_dev), pbn);
963 	/*
964 	 * Now report the status as returned by the hardware with
965 	 * attempt at interpretation.
966 	 */
967 	printf("%s %s error:", sc->sc_dev.dv_xname,
968 	    (bp->b_flags & B_READ) ? "read" : "write");
969 	printf(" unit %d, volume %d R0x%x F0x%x A0x%x I0x%x\n",
970 	       css.c_vu&0xF, (css.c_vu>>4)&0xF,
971 	       css.c_ref, css.c_fef, css.c_aef, css.c_ief);
972 	printf("P1-P10: ");
973 	printf("0x%x ", *(u_int32_t *)&css.c_raw[0]);
974 	printf("0x%x ", *(u_int32_t *)&css.c_raw[4]);
975 	printf("0x%x\n", *(u_int16_t *)&css.c_raw[8]);
976 
977 	return (1);
978 }
979 
980 int
981 rdread(dev, uio, flags)
982 	dev_t dev;
983 	struct uio *uio;
984 	int flags;
985 {
986 
987 	return (physio(rdstrategy, NULL, dev, B_READ, minphys, uio));
988 }
989 
990 int
991 rdwrite(dev, uio, flags)
992 	dev_t dev;
993 	struct uio *uio;
994 	int flags;
995 {
996 
997 	return (physio(rdstrategy, NULL, dev, B_WRITE, minphys, uio));
998 }
999 
1000 int
1001 rdioctl(dev, cmd, data, flag, p)
1002 	dev_t dev;
1003 	u_long cmd;
1004 	caddr_t data;
1005 	int flag;
1006 	struct proc *p;
1007 {
1008 	struct rd_softc *sc;
1009 	struct disklabel *lp;
1010 	int error, flags;
1011 
1012 	sc = device_lookup(&rd_cd, RDUNIT(dev));
1013 	if (sc == NULL)
1014 		return (ENXIO);
1015 	lp = sc->sc_dk.dk_label;
1016 
1017 	DPRINTF(RDB_FOLLOW, ("rdioctl: sc=%p\n", sc));
1018 
1019 	switch (cmd) {
1020 	case DIOCGDINFO:
1021 		*(struct disklabel *)data = *lp;
1022 		return (0);
1023 
1024 	case DIOCGPART:
1025 		((struct partinfo *)data)->disklab = lp;
1026 		((struct partinfo *)data)->part =
1027 		    &lp->d_partitions[RDPART(dev)];
1028 		return (0);
1029 
1030 	case DIOCWLABEL:
1031 		if ((flag & FWRITE) == 0)
1032 			return (EBADF);
1033 		if (*(int *)data)
1034 			sc->sc_flags |= RDF_WLABEL;
1035 		else
1036 			sc->sc_flags &= ~RDF_WLABEL;
1037 		return (0);
1038 
1039 	case DIOCSDINFO:
1040 		if ((flag & FWRITE) == 0)
1041 			return (EBADF);
1042 		return (setdisklabel(lp, (struct disklabel *)data,
1043 		    (sc->sc_flags & RDF_WLABEL) ? 0 : sc->sc_dk.dk_openmask,
1044 		    (struct cpu_disklabel *)0));
1045 
1046 	case DIOCWDINFO:
1047 		if ((flag & FWRITE) == 0)
1048 			return (EBADF);
1049 		error = setdisklabel(lp, (struct disklabel *)data,
1050 		    (sc->sc_flags & RDF_WLABEL) ? 0 : sc->sc_dk.dk_openmask,
1051 		    (struct cpu_disklabel *)0);
1052 		if (error)
1053 			return (error);
1054 		flags = sc->sc_flags;
1055 		sc->sc_flags = RDF_ALIVE | RDF_WLABEL;
1056 		error = writedisklabel(RDLABELDEV(dev), rdstrategy, lp,
1057 		    (struct cpu_disklabel *)0);
1058 		sc->sc_flags = flags;
1059 		return (error);
1060 
1061 	case DIOCGDEFLABEL:
1062 		rdgetdefaultlabel(sc, (struct disklabel *)data);
1063 		return (0);
1064 	}
1065 	return (EINVAL);
1066 }
1067 
1068 void
1069 rdgetdefaultlabel(sc, lp)
1070 	struct rd_softc *sc;
1071 	struct disklabel *lp;
1072 {
1073 	int type = sc->sc_type;
1074 
1075 	memset((caddr_t)lp, 0, sizeof(struct disklabel));
1076 
1077 	lp->d_type = DTYPE_GPIB;
1078 	lp->d_secsize = DEV_BSIZE;
1079 	lp->d_nsectors = rdidentinfo[type].ri_nbpt;
1080 	lp->d_ntracks = rdidentinfo[type].ri_ntpc;
1081 	lp->d_ncylinders = rdidentinfo[type].ri_ncyl;
1082 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1083 	lp->d_secperunit = lp->d_ncylinders * lp->d_secpercyl;
1084 
1085 	strncpy(lp->d_typename, rdidentinfo[type].ri_desc, 16);
1086 	strncpy(lp->d_packname, "fictitious", 16);
1087 	lp->d_rpm = 3000;
1088 	lp->d_interleave = 1;
1089 	lp->d_flags = 0;
1090 
1091 	lp->d_partitions[RAW_PART].p_offset = 0;
1092 	lp->d_partitions[RAW_PART].p_size =
1093 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
1094 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1095 	lp->d_npartitions = RAW_PART + 1;
1096 
1097 	lp->d_magic = DISKMAGIC;
1098 	lp->d_magic2 = DISKMAGIC;
1099 	lp->d_checksum = dkcksum(lp);
1100 }
1101 
1102 int
1103 rdsize(dev)
1104 	dev_t dev;
1105 {
1106 	struct rd_softc *sc;
1107 	int psize, didopen = 0;
1108 
1109 	sc = device_lookup(&rd_cd, RDUNIT(dev));
1110 	if (sc == NULL || (sc->sc_flags & RDF_ALIVE) == 0)
1111 		return (-1);
1112 
1113 	/*
1114 	 * We get called very early on (via swapconf)
1115 	 * without the device being open so we may need
1116 	 * to handle it here.
1117 	 */
1118 	if (sc->sc_dk.dk_openmask == 0) {
1119 		if (rdopen(dev, FREAD | FWRITE, S_IFBLK, NULL))
1120 			return (-1);
1121 		didopen = 1;
1122 	}
1123 	psize = sc->sc_dk.dk_label->d_partitions[RDPART(dev)].p_size *
1124 	    (sc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1125 	if (didopen)
1126 		(void) rdclose(dev, FREAD | FWRITE, S_IFBLK, NULL);
1127 	return (psize);
1128 }
1129 
1130 
1131 static int rddoingadump;	/* simple mutex */
1132 
1133 /*
1134  * Non-interrupt driven, non-dma dump routine.
1135  */
1136 int
1137 rddump(dev, blkno, va, size)
1138 	dev_t dev;
1139 	daddr_t blkno;
1140 	caddr_t va;
1141 	size_t size;
1142 {
1143 	struct rd_softc *sc;
1144 	int sectorsize;		/* size of a disk sector */
1145 	int nsects;		/* number of sectors in partition */
1146 	int sectoff;		/* sector offset of partition */
1147 	int totwrt;		/* total number of sectors left to write */
1148 	int nwrt;		/* current number of sectors to write */
1149 	int slave;
1150 	struct disklabel *lp;
1151 	u_int8_t stat;
1152 
1153 	/* Check for recursive dump; if so, punt. */
1154 	if (rddoingadump)
1155 		return (EFAULT);
1156 	rddoingadump = 1;
1157 
1158 	sc = device_lookup(&rd_cd, RDUNIT(dev));
1159 	if (sc == NULL || (sc->sc_flags & RDF_ALIVE) == 0)
1160 		return (ENXIO);
1161 
1162 	DPRINTF(RDB_FOLLOW, ("rddump: sc=%p\n", sc));
1163 
1164 	slave = sc->sc_slave;
1165 
1166 	/*
1167 	 * Convert to disk sectors.  Request must be a multiple of size.
1168 	 */
1169 	lp = sc->sc_dk.dk_label;
1170 	sectorsize = lp->d_secsize;
1171 	if ((size % sectorsize) != 0)
1172 		return (EFAULT);
1173 	totwrt = size / sectorsize;
1174 	blkno = dbtob(blkno) / sectorsize;	/* blkno in DEV_BSIZE units */
1175 
1176 	nsects = lp->d_partitions[RDPART(dev)].p_size;
1177 	sectoff = lp->d_partitions[RDPART(dev)].p_offset;
1178 
1179 	/* Check transfer bounds against partition size. */
1180 	if ((blkno < 0) || (blkno + totwrt) > nsects)
1181 		return (EINVAL);
1182 
1183 	/* Offset block number to start of partition. */
1184 	blkno += sectoff;
1185 
1186 	while (totwrt > 0) {
1187 		nwrt = totwrt;		/* XXX */
1188 #ifndef RD_DUMP_NOT_TRUSTED
1189 		/*
1190 		 * Fill out and send GPIB command.
1191 		 */
1192 		sc->sc_ioc.c_unit = CS80CMD_SUNIT(sc->sc_punit);
1193 		sc->sc_ioc.c_volume = CS80CMD_SVOL(0);
1194 		sc->sc_ioc.c_saddr = CS80CMD_SADDR;
1195 		sc->sc_ioc.c_hiaddr = 0;
1196 		sc->sc_ioc.c_addr = RDBTOS(blkno);
1197 		sc->sc_ioc.c_nop2 = CS80CMD_NOP;
1198 		sc->sc_ioc.c_slen = CS80CMD_SLEN;
1199 		sc->sc_ioc.c_len = nwrt * sectorsize;
1200 		sc->sc_ioc.c_cmd = CS80CMD_WRITE;
1201 		(void) gpibsend(sc->sc_ic, slave, CS80CMD_SCMD,
1202 		    &sc->sc_ioc.c_unit, sizeof(sc->sc_ioc)-3);
1203 		if (gpibswait(sc->sc_ic, slave))
1204 			return (EIO);
1205 		/*
1206 		 * Send the data.
1207 		 */
1208 		(void) gpibsend(sc->sc_ic, slave, CS80CMD_EXEC, va,
1209 		    nwrt * sectorsize);
1210 		(void) gpibswait(sc->sc_ic, slave);
1211 		(void) gpibrecv(sc->sc_ic, slave, CS80CMD_QSTAT, &stat, 1);
1212 		if (stat)
1213 			return (EIO);
1214 #else /* RD_DUMP_NOT_TRUSTED */
1215 		/* Let's just talk about this first... */
1216 		printf("%s: dump addr %p, blk %d\n", sc->sc_dev.dv_xname,
1217 		    va, blkno);
1218 		delay(500 * 1000);	/* half a second */
1219 #endif /* RD_DUMP_NOT_TRUSTED */
1220 
1221 		/* update block count */
1222 		totwrt -= nwrt;
1223 		blkno += nwrt;
1224 		va += sectorsize * nwrt;
1225 	}
1226 	rddoingadump = 0;
1227 	return (0);
1228 }
1229 
1230 #ifdef COMPAT_NOLABEL
1231 
1232 /*
1233  * CS/80 partitions.  We reserve the first cylinder for a LIF
1234  * style boot directory (the 8k allowed in the BSD filesystem
1235  * is just way too small).  This boot area is outside of all but
1236  * the C partition.  This implies that you cannot use the C
1237  * partition on a bootable disk since the filesystem would overlay
1238  * the boot area.  You must use the A partition.
1239  *
1240  * These maps support four basic layouts:
1241  *
1242  *	A/B/G:   This is the "traditional" setup for a bootable disk.
1243  *	         A is the root partition, B the swap, and G a user partition.
1244  *	A/D/H:   This is a setup for bootable systems requiring more swap
1245  *		 (e.g. those who use HPCL).  It has A as the root, D as a
1246  *		 larger swap, and H as a smaller user partition.
1247  *	A/D/E/F: Similar to A/D/H with E and F breaking H into two partitions.
1248  *		 E could be used for /usr and F for users.
1249  *	C:       This gives a single, non-bootable, large user filesystem.
1250  *	         Good for second drives on a machine (e.g. /usr/src).
1251  */
1252 struct	size {
1253 	daddr_t	nblocks;
1254 	int	cyloff;
1255 } rd7945A_sizes[8] = {
1256 	{ RDSZ(15904),	1	},	/* A=cyl 1 thru 142 */
1257 	{ RDSZ(20160),	143	},	/* B=cyl 143 thru 322 */
1258 	{ RDSZ(108416),	0	},	/* C=cyl 0 thru 967 */
1259 	{ RDSZ(40320),	143	},	/* D=cyl 143 thru 502 */
1260 	{ RDSZ(0),	0	},	/* E=<undefined> */
1261 	{ RDSZ(0),	0	},	/* F=<undefined> */
1262 	{ RDSZ(72240),	323	},	/* G=cyl 323 thru 967 */
1263 	{ RDSZ(52080),	503	},	/* H=cyl 503 thru 967 */
1264 }, rd9134D_sizes[8] = {
1265 	{ RDSZ(15936),	1	},	/* A=cyl 1 thru 166 */
1266 	{ RDSZ(13056),	167	},	/* B=cyl 167 thru 302 */
1267 	{ RDSZ(29088),	0	},	/* C=cyl 0 thru 302 */
1268 	{ RDSZ(0),	0	},	/* D=<undefined> */
1269 	{ RDSZ(0),	0	},	/* E=<undefined> */
1270 	{ RDSZ(0),	0	},	/* F=<undefined> */
1271 	{ RDSZ(0),	0	},	/* G=<undefined> */
1272 	{ RDSZ(0),	0	},	/* H=<undefined> */
1273 }, rd9122S_sizes[8] = {
1274 	{ RDSZ(0),	0	},	/* A=<undefined> */
1275 	{ RDSZ(0),	0	},	/* B=<undefined> */
1276 	{ RDSZ(1232),	0	},	/* C=cyl 0 thru 76 */
1277 	{ RDSZ(0),	0	},	/* D=<undefined> */
1278 	{ RDSZ(0),	0	},	/* E=<undefined> */
1279 	{ RDSZ(0),	0	},	/* F=<undefined> */
1280 	{ RDSZ(0),	0	},	/* G=<undefined> */
1281 	{ RDSZ(0),	0	},	/* H=<undefined> */
1282 }, rd7912P_sizes[8] = {
1283 	{ RDSZ(15904),	0	},	/* A=cyl 1 thru 71 */
1284 	{ RDSZ(22400),	72	},	/* B=cyl 72 thru 171 */
1285 	{ RDSZ(128128),	0	},	/* C=cyl 0 thru 571 */
1286 	{ RDSZ(42560),	72	},	/* D=cyl 72 thru 261 */
1287 	{ RDSZ(0),	292	},	/* E=<undefined> */
1288 	{ RDSZ(0),	542	},	/* F=<undefined> */
1289 	{ RDSZ(89600),	172	},	/* G=cyl 221 thru 571 */
1290 	{ RDSZ(69440),	262	},	/* H=cyl 262 thru 571 */
1291 }, rd7914P_sizes[8] = {
1292 	{ RDSZ(15904),	1	},	/* A=cyl 1 thru 71 */
1293 	{ RDSZ(40320),	72	},	/* B=cyl 72 thru 251 */
1294 	{ RDSZ(258048),	0	},	/* C=cyl 0 thru 1151 */
1295 	{ RDSZ(64960),	72	},	/* D=cyl 72 thru 361 */
1296 	{ RDSZ(98560),	362	},	/* E=cyl 362 thru 801 */
1297 	{ RDSZ(78400),	802	},	/* F=cyl 802 thru 1151 */
1298 	{ RDSZ(201600),	252	},	/* G=cyl 221 thru 1151 */
1299 	{ RDSZ(176960),	362	},	/* H=cyl 362 thru 1151 */
1300 }, rd7933H_sizes[8] = {
1301 	{ RDSZ(16146),	1	},	/* A=cyl 1 thru 27 */
1302 	{ RDSZ(66976),	28	},	/* B=cyl 28 thru 139 */
1303 	{ RDSZ(789958),	0	},	/* C=cyl 0 thru 1320 */
1304 	{ RDSZ(16146),	140	},	/* D=cyl 140 thru 166 */
1305 	{ RDSZ(165646),	167	},	/* E=cyl 167 thru 443 */
1306 	{ RDSZ(165646),	444	},	/* F=cyl 444 thru 720 */
1307 	{ RDSZ(706238),	140	},	/* G=cyl 140 thru 1320 */
1308 	{ RDSZ(358800),	721	},	/* H=cyl 721 thru 1320 */
1309 }, rd9134L_sizes[8] = {
1310 	{ RDSZ(15920),	1	},	/* A=cyl 1 thru 199 */
1311 	{ RDSZ(20000),	200	},	/* B=cyl 200 thru 449 */
1312 	{ RDSZ(77840),	0	},	/* C=cyl 0 thru 972 */
1313 	{ RDSZ(32000),	200	},	/* D=cyl 200 thru 599 */
1314 	{ RDSZ(0),	0	},	/* E=<undefined> */
1315 	{ RDSZ(0),	0	},	/* F=<undefined> */
1316 	{ RDSZ(41840),	450	},	/* G=cyl 450 thru 972 */
1317 	{ RDSZ(29840),	600	},	/* H=cyl 600 thru 972 */
1318 }, rd7957A_sizes[8] = {
1319 	{ RDSZ(16016),	1	},	/* A=cyl 1 thru 104 */
1320 	{ RDSZ(24640),	105	},	/* B=cyl 105 thru 264 */
1321 	{ RDSZ(159544),	0	},	/* C=cyl 0 thru 1035 */
1322 	{ RDSZ(42350),	105	},	/* D=cyl 105 thru 379 */
1323 	{ RDSZ(54824),	380	},	/* E=cyl 380 thru 735 */
1324 	{ RDSZ(46200),	736	},	/* F=cyl 736 thru 1035 */
1325 	{ RDSZ(118734),	265	},	/* G=cyl 265 thru 1035 */
1326 	{ RDSZ(101024),	380	},	/* H=cyl 380 thru 1035 */
1327 }, rd7958A_sizes[8] = {
1328 	{ RDSZ(16128),	1	},	/* A=cyl 1 thru 64 */
1329 	{ RDSZ(32256),	65	},	/* B=cyl 65 thru 192 */
1330 	{ RDSZ(255276),	0	},	/* C=cyl 0 thru 1012 */
1331 	{ RDSZ(48384),	65	},	/* D=cyl 65 thru 256 */
1332 	{ RDSZ(100800),	257	},	/* E=cyl 257 thru 656 */
1333 	{ RDSZ(89712),	657	},	/* F=cyl 657 thru 1012 */
1334 	{ RDSZ(206640),	193	},	/* G=cyl 193 thru 1012 */
1335 	{ RDSZ(190512),	257	},	/* H=cyl 257 thru 1012 */
1336 }, rd7957B_sizes[8] = {
1337 	{ RDSZ(16002),	1	},	/* A=cyl 1 thru 127 */
1338 	{ RDSZ(32760),	128	},	/* B=cyl 128 thru 387 */
1339 	{ RDSZ(159894),	0	},	/* C=cyl 0 thru 1268 */
1340 	{ RDSZ(49140),	128	},	/* D=cyl 128 thru 517 */
1341 	{ RDSZ(50400),	518	},	/* E=cyl 518 thru 917 */
1342 	{ RDSZ(44226),	918	},	/* F=cyl 918 thru 1268 */
1343 	{ RDSZ(111006),	388	},	/* G=cyl 388 thru 1268 */
1344 	{ RDSZ(94626),	518	},	/* H=cyl 518 thru 1268 */
1345 }, rd7958B_sizes[8] = {
1346 	{ RDSZ(16254),	1	},	/* A=cyl 1 thru 43 */
1347 	{ RDSZ(32886),	44	},	/* B=cyl 44 thru 130 */
1348 	{ RDSZ(297108),	0	},	/* C=cyl 0 thru 785 */
1349 	{ RDSZ(49140),	44	},	/* D=cyl 44 thru 173 */
1350 	{ RDSZ(121716),	174	},	/* E=cyl 174 thru 495 */
1351 	{ RDSZ(109620),	496	},	/* F=cyl 496 thru 785 */
1352 	{ RDSZ(247590),	131	},	/* G=cyl 131 thru 785 */
1353 	{ RDSZ(231336),	174	},	/* H=cyl 174 thru 785 */
1354 }, rd7959B_sizes[8] = {
1355 	{ RDSZ(16254),	1	},	/* A=cyl 1 thru 43 */
1356 	{ RDSZ(49140),	44	},	/* B=cyl 44 thru 173 */
1357 	{ RDSZ(594216),	0	},	/* C=cyl 0 thru 1571 */
1358 	{ RDSZ(65772),	44	},	/* D=cyl 44 thru 217 */
1359 	{ RDSZ(303912),	218	},	/* E=cyl 218 thru 1021 */
1360 	{ RDSZ(207900),	1022	},	/* F=cyl 1022 thru 1571 */
1361 	{ RDSZ(528444),	174	},	/* G=cyl 174 thru 1571 */
1362 	{ RDSZ(511812),	218	},	/* H=cyl 218 thru 1571 */
1363 }, rd2200A_sizes[8] = {
1364 	{ RDSZ(16272),	1	},	/* A=cyl 1 thru 36 */
1365 	{ RDSZ(49720),	37	},	/* B=cyl 37 thru 146 */
1366 	{ RDSZ(654948),	0	},	/* C=cyl 0 thru 1448 */
1367 	{ RDSZ(65992),	37	},	/* D=cyl 37 thru 182 */
1368 	{ RDSZ(304648),	183	},	/* E=cyl 183 thru 856 */
1369 	{ RDSZ(267584),	857	},	/* F=cyl 857 thru 1448 */
1370 	{ RDSZ(588504),	147	},	/* G=cyl 147 thru 1448 */
1371 	{ RDSZ(572232),	183	},	/* H=cyl 183 thru 1448 */
1372 }, rd2203A_sizes[8] = {
1373 	/* modelled after the 7937; i.e. bogus */
1374 	{ RDSZ(16272),	1	},	/* A=cyl 1 thru 18 */
1375 	{ RDSZ(67800),	19	},	/* B=cyl 19 thru 93 */
1376 	{ RDSZ(1309896), 0	},	/* C=cyl 0 thru 1448 */
1377 	{ RDSZ(16272),	94	},	/* D=cyl 19 thru 111 */
1378 	{ RDSZ(305552),	112	},	/* E=cyl 112 thru 449 */
1379 	{ RDSZ(305552),	450	},	/* F=cyl 450 thru 787 */
1380 	{ RDSZ(1224920), 94	},	/* G=cyl 94 thru 1448 */
1381 	{ RDSZ(597544),	788	},	/* H=cyl 788 thru 1448 */
1382 }, rd7936H_sizes[8] = {
1383 	{ RDSZ(16359),	1	},	/* A=cyl 1 thru 19 */
1384 	{ RDSZ(67158),	20	},	/* B=cyl 20 thru 97 */
1385 	{ RDSZ(600978),	0	},	/* C=cyl 0 thru 697 */
1386 	{ RDSZ(16359),	98	},	/* D=cyl 98 thru 116 */
1387 	{ RDSZ(120540),	117	},	/* E=cyl 117 thru 256 */
1388 	{ RDSZ(120540),	256	},	/* F=cyl 256 thru 396 */
1389 	{ RDSZ(516600),	98	},	/* G=cyl 98 thru 697 */
1390 	{ RDSZ(259161),	397	},	/* H=cyl 397 thru 697 */
1391 }, rd7937H_sizes[8] = {
1392 	{ RDSZ(15990),	1	},	/* A=cyl 1 thru 10 */
1393 	{ RDSZ(67158),	11	},	/* B=cyl 11 thru 52 */
1394 	{ RDSZ(1116102), 0	},	/* C=cyl 0 thru 697 */
1395 	{ RDSZ(124722),	53	},	/* D=cyl 53 thru 130 */
1396 	{ RDSZ(163098),	131	},	/* E=cyl 131 thru 232 */
1397 	{ RDSZ(287820),	233	},	/* F=cyl 233 thru 412 */
1398 	{ RDSZ(1031355), 53	},	/* G=cyl 53 thru 697 */
1399 	{ RDSZ(455715),	413	},	/* H=cyl 413 thru 697 */
1400 };
1401 
1402 /*
1403  * Indexed the same as rdidentinfo array.
1404  */
1405 struct rdcompatinfo {
1406 	struct size *sizes;	/* partition info */
1407 } rdcompatinfo[] = {
1408 	{ rd7945A_sizes },
1409 	{ rd9134D_sizes },
1410 	{ rd9122S_sizes },
1411 	{ rd7912P_sizes },
1412 	{ rd7914P_sizes },
1413 	{ rd7958A_sizes },
1414 	{ rd7957A_sizes },
1415 	{ rd7933H_sizes },
1416 	{ rd9134L_sizes },
1417 	{ rd7936H_sizes },
1418 	{ rd7937H_sizes },
1419 	{ rd7914P_sizes },
1420 	{ rd7945A_sizes },
1421 	{ rd9122S_sizes },
1422 	{ rd7957B_sizes },
1423 	{ rd7958B_sizes },
1424 	{ rd7959B_sizes },
1425 	{ rd2200A_sizes },
1426 	{ rd2203A_sizes },
1427 };
1428 int nrdcompatinfo = sizeof(rdcompatinfo) / sizeof(rdcompatinfo[0]);
1429 
1430 void
1431 rdgetcompatlabel(sc, lp)
1432 	struct rd_softc *sc;
1433 	struct disklabel *lp;
1434 {
1435 	struct rdcompatinfo *ci = &rdcompatinfo[sc->sc_type];
1436 	const struct rdidentinfo *ri = &rdidentinfo[sc->sc_type];
1437 	struct partition *pi;
1438 	int dcount;
1439 
1440 	rdgetdefaultlabel(sc, lp);
1441 
1442 	lp->d_npartitions = 8;
1443 	pi = lp->d_partitions;
1444 	for (dcount = 0; dcount < lp->d_npartitions; dcount++) {
1445 		pi->p_size = ci->sizes[dcount].nblocks;
1446 		pi->p_offset = ci->sizes[dcount].cyloff * lp->d_secpercyl;
1447 		pi->p_fsize = 1024;
1448 		if (dcount == 1 || dcount == 3)
1449 			pi->p_fstype = FS_SWAP;
1450 		else if (dcount == 2)
1451 			pi->p_fstype = FS_BOOT;
1452 		else
1453 			pi->p_fstype = FS_BSDFFS;
1454 		pi->p_frag = 8;
1455 		pi++;
1456 	}
1457 }
1458 
1459 #endif /* COMPAT_NOLABEL */
1460