xref: /netbsd-src/sys/arch/hp300/dev/rd.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: rd.c,v 1.43 2000/05/27 04:52:28 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996, 1997 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/param.h>
86 #include <sys/systm.h>
87 #include <sys/buf.h>
88 #include <sys/conf.h>
89 #include <sys/device.h>
90 #include <sys/disk.h>
91 #include <sys/disklabel.h>
92 #include <sys/fcntl.h>
93 #include <sys/ioctl.h>
94 #include <sys/proc.h>
95 #include <sys/stat.h>
96 
97 #include <hp300/dev/hpibvar.h>
98 
99 #include <hp300/dev/rdreg.h>
100 #include <hp300/dev/rdvar.h>
101 
102 #include "opt_useleds.h"
103 
104 #ifdef USELEDS
105 #include <hp300/hp300/leds.h>
106 #endif
107 
108 int	rderrthresh = RDRETRY-1;	/* when to start reporting errors */
109 
110 #ifdef DEBUG
111 /* error message tables */
112 char *err_reject[] = {
113 	0, 0,
114 	"channel parity error",		/* 0x2000 */
115 	0, 0,
116 	"illegal opcode",		/* 0x0400 */
117 	"module addressing",		/* 0x0200 */
118 	"address bounds",		/* 0x0100 */
119 	"parameter bounds",		/* 0x0080 */
120 	"illegal parameter",		/* 0x0040 */
121 	"message sequence",		/* 0x0020 */
122 	0,
123 	"message length",		/* 0x0008 */
124 	0, 0, 0
125 };
126 
127 char *err_fault[] = {
128 	0,
129 	"cross unit",			/* 0x4000 */
130 	0,
131 	"controller fault",		/* 0x1000 */
132 	0, 0,
133 	"unit fault",			/* 0x0200 */
134 	0,
135 	"diagnostic result",		/* 0x0080 */
136 	0,
137 	"operator release request",	/* 0x0020 */
138 	"diagnostic release request",	/* 0x0010 */
139 	"internal maintenance release request",	/* 0x0008 */
140 	0,
141 	"power fail",			/* 0x0002 */
142 	"retransmit"			/* 0x0001 */
143 };
144 
145 char *err_access[] = {
146 	"illegal parallel operation",	/* 0x8000 */
147 	"uninitialized media",		/* 0x4000 */
148 	"no spares available",		/* 0x2000 */
149 	"not ready",			/* 0x1000 */
150 	"write protect",		/* 0x0800 */
151 	"no data found",		/* 0x0400 */
152 	0, 0,
153 	"unrecoverable data overflow",	/* 0x0080 */
154 	"unrecoverable data",		/* 0x0040 */
155 	0,
156 	"end of file",			/* 0x0010 */
157 	"end of volume",		/* 0x0008 */
158 	0, 0, 0
159 };
160 
161 char *err_info[] = {
162 	"operator release request",	/* 0x8000 */
163 	"diagnostic release request",	/* 0x4000 */
164 	"internal maintenance release request",	/* 0x2000 */
165 	"media wear",			/* 0x1000 */
166 	"latency induced",		/* 0x0800 */
167 	0, 0,
168 	"auto sparing invoked",		/* 0x0100 */
169 	0,
170 	"recoverable data overflow",	/* 0x0040 */
171 	"marginal data",		/* 0x0020 */
172 	"recoverable data",		/* 0x0010 */
173 	0,
174 	"maintenance track overflow",	/* 0x0004 */
175 	0, 0
176 };
177 
178 int	rddebug = 0x80;
179 #define RDB_FOLLOW	0x01
180 #define RDB_STATUS	0x02
181 #define RDB_IDENT	0x04
182 #define RDB_IO		0x08
183 #define RDB_ASYNC	0x10
184 #define RDB_ERROR	0x80
185 #endif
186 
187 /*
188  * Misc. HW description, indexed by sc_type.
189  * Nothing really critical here, could do without it.
190  */
191 struct rdidentinfo rdidentinfo[] = {
192 	{ RD7946AID,	0,	"7945A",	NRD7945ABPT,
193 	  NRD7945ATRK,	968,	 108416 },
194 
195 	{ RD9134DID,	1,	"9134D",	NRD9134DBPT,
196 	  NRD9134DTRK,	303,	  29088 },
197 
198 	{ RD9134LID,	1,	"9122S",	NRD9122SBPT,
199 	  NRD9122STRK,	77,	   1232 },
200 
201 	{ RD7912PID,	0,	"7912P",	NRD7912PBPT,
202 	  NRD7912PTRK,	572,	 128128 },
203 
204 	{ RD7914PID,	0,	"7914P",	NRD7914PBPT,
205 	  NRD7914PTRK,	1152,	 258048 },
206 
207 	{ RD7958AID,	0,	"7958A",	NRD7958ABPT,
208 	  NRD7958ATRK,	1013,	 255276 },
209 
210 	{ RD7957AID,	0,	"7957A",	NRD7957ABPT,
211 	  NRD7957ATRK,	1036,	 159544 },
212 
213 	{ RD7933HID,	0,	"7933H",	NRD7933HBPT,
214 	  NRD7933HTRK,	1321,	 789958 },
215 
216 	{ RD9134LID,	1,	"9134L",	NRD9134LBPT,
217 	  NRD9134LTRK,	973,	  77840 },
218 
219 	{ RD7936HID,	0,	"7936H",	NRD7936HBPT,
220 	  NRD7936HTRK,	698,	 600978 },
221 
222 	{ RD7937HID,	0,	"7937H",	NRD7937HBPT,
223 	  NRD7937HTRK,	698,	1116102 },
224 
225 	{ RD7914CTID,	0,	"7914CT",	NRD7914PBPT,
226 	  NRD7914PTRK,	1152,	 258048 },
227 
228 	{ RD7946AID,	0,	"7946A",	NRD7945ABPT,
229 	  NRD7945ATRK,	968,	 108416 },
230 
231 	{ RD9134LID,	1,	"9122D",	NRD9122SBPT,
232 	  NRD9122STRK,	77,	   1232 },
233 
234 	{ RD7957BID,	0,	"7957B",	NRD7957BBPT,
235 	  NRD7957BTRK,	1269,	 159894 },
236 
237 	{ RD7958BID,	0,	"7958B",	NRD7958BBPT,
238 	  NRD7958BTRK,	786,	 297108 },
239 
240 	{ RD7959BID,	0,	"7959B",	NRD7959BBPT,
241 	  NRD7959BTRK,	1572,	 594216 },
242 
243 	{ RD2200AID,	0,	"2200A",	NRD2200ABPT,
244 	  NRD2200ATRK,	1449,	 654948 },
245 
246 	{ RD2203AID,	0,	"2203A",	NRD2203ABPT,
247 	  NRD2203ATRK,	1449,	1309896 }
248 };
249 int numrdidentinfo = sizeof(rdidentinfo) / sizeof(rdidentinfo[0]);
250 
251 bdev_decl(rd);
252 cdev_decl(rd);
253 
254 int	rdident __P((struct device *, struct rd_softc *,
255 	    struct hpibbus_attach_args *));
256 void	rdreset __P((struct rd_softc *));
257 void	rdustart __P((struct rd_softc *));
258 int	rdgetinfo __P((dev_t));
259 void	rdrestart __P((void *));
260 struct buf *rdfinish __P((struct rd_softc *, struct buf *));
261 
262 void	rdrestart __P((void *));
263 void	rdustart __P((struct rd_softc *));
264 struct buf *rdfinish __P((struct rd_softc *, struct buf *));
265 void	rdstart __P((void *));
266 void	rdgo __P((void *));
267 void	rdintr __P((void *));
268 int	rdstatus __P((struct rd_softc *));
269 int	rderror __P((int));
270 #ifdef DEBUG
271 void	rdprinterr __P((char *, short, char **));
272 #endif
273 
274 int	rdmatch __P((struct device *, struct cfdata *, void *));
275 void	rdattach __P((struct device *, struct device *, void *));
276 
277 struct cfattach rd_ca = {
278 	sizeof(struct rd_softc), rdmatch, rdattach
279 };
280 
281 extern struct cfdriver rd_cd;
282 
283 int
284 rdmatch(parent, match, aux)
285 	struct device *parent;
286 	struct cfdata *match;
287 	void *aux;
288 {
289 	struct hpibbus_attach_args *ha = aux;
290 
291 	/*
292 	 * Set punit if operator specified one in the kernel
293 	 * configuration file.
294 	 */
295 	if (match->hpibbuscf_punit != HPIBBUSCF_PUNIT_DEFAULT &&
296 	    match->hpibbuscf_punit < HPIB_NPUNITS)
297 		ha->ha_punit = match->hpibbuscf_punit;
298 
299 	if (rdident(parent, NULL, ha) == 0) {
300 		/*
301 		 * XXX Some aging HP-IB drives are slow to
302 		 * XXX respond; give them a chance to catch
303 		 * XXX up and probe them again.
304 		 */
305 		delay(10000);
306 		ha->ha_id = hpibid(parent->dv_unit, ha->ha_slave);
307 		return (rdident(parent, NULL, ha));
308 	}
309 	return (1);
310 }
311 
312 void
313 rdattach(parent, self, aux)
314 	struct device *parent, *self;
315 	void *aux;
316 {
317 	struct rd_softc *sc = (struct rd_softc *)self;
318 	struct hpibbus_attach_args *ha = aux;
319 
320 	BUFQ_INIT(&sc->sc_tab);
321 
322 	if (rdident(parent, sc, ha) == 0) {
323 		printf("\n%s: didn't respond to describe command!\n",
324 		    sc->sc_dev.dv_xname);
325 		return;
326 	}
327 
328 	/*
329 	 * Initialize and attach the disk structure.
330 	 */
331 	bzero(&sc->sc_dkdev, sizeof(sc->sc_dkdev));
332 	sc->sc_dkdev.dk_name = sc->sc_dev.dv_xname;
333 	disk_attach(&sc->sc_dkdev);
334 
335 	sc->sc_slave = ha->ha_slave;
336 	sc->sc_punit = ha->ha_punit;
337 
338 	callout_init(&sc->sc_restart_ch);
339 
340 	/* Initialize the hpib job queue entry */
341 	sc->sc_hq.hq_softc = sc;
342 	sc->sc_hq.hq_slave = sc->sc_slave;
343 	sc->sc_hq.hq_start = rdstart;
344 	sc->sc_hq.hq_go = rdgo;
345 	sc->sc_hq.hq_intr = rdintr;
346 
347 	sc->sc_flags = RDF_ALIVE;
348 #ifdef DEBUG
349 	/* always report errors */
350 	if (rddebug & RDB_ERROR)
351 		rderrthresh = 0;
352 #endif
353 }
354 
355 int
356 rdident(parent, sc, ha)
357 	struct device *parent;
358 	struct rd_softc *sc;
359 	struct hpibbus_attach_args *ha;
360 {
361 	struct rd_describe *desc = sc != NULL ? &sc->sc_rddesc : NULL;
362 	u_char stat, cmd[3];
363 	char name[7];
364 	int i, id, n, ctlr, slave;
365 
366 	ctlr = parent->dv_unit;
367 	slave = ha->ha_slave;
368 
369 	/* Verify that we have a CS80 device. */
370 	if ((ha->ha_id & 0x200) == 0)
371 		return (0);
372 
373 	/* Is it one of the disks we support? */
374 	for (id = 0; id < numrdidentinfo; id++)
375 		if (ha->ha_id == rdidentinfo[id].ri_hwid)
376 			break;
377 	if (id == numrdidentinfo || ha->ha_punit > rdidentinfo[id].ri_maxunum)
378 		return (0);
379 
380 	/*
381 	 * If we're just probing for the device, that's all the
382 	 * work we need to do.
383 	 */
384 	if (sc == NULL)
385 		return (1);
386 
387 	/*
388 	 * Reset device and collect description
389 	 */
390 	rdreset(sc);
391 	cmd[0] = C_SUNIT(ha->ha_punit);
392 	cmd[1] = C_SVOL(0);
393 	cmd[2] = C_DESC;
394 	hpibsend(ctlr, slave, C_CMD, cmd, sizeof(cmd));
395 	hpibrecv(ctlr, slave, C_EXEC, desc, 37);
396 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
397 	bzero(name, sizeof(name));
398 	if (stat == 0) {
399 		n = desc->d_name;
400 		for (i = 5; i >= 0; i--) {
401 			name[i] = (n & 0xf) + '0';
402 			n >>= 4;
403 		}
404 	}
405 
406 #ifdef DEBUG
407 	if (rddebug & RDB_IDENT) {
408 		printf("\n%s: name: %x ('%s')\n",
409 		    sc->sc_dev.dv_xname, desc->d_name, name);
410 		printf("  iuw %x, maxxfr %d, ctype %d\n",
411 		    desc->d_iuw, desc->d_cmaxxfr, desc->d_ctype);
412 		printf("  utype %d, bps %d, blkbuf %d, burst %d, blktime %d\n",
413 		    desc->d_utype, desc->d_sectsize,
414 		    desc->d_blkbuf, desc->d_burstsize, desc->d_blocktime);
415 		printf("  avxfr %d, ort %d, atp %d, maxint %d, fv %x, rv %x\n",
416 		    desc->d_uavexfr, desc->d_retry, desc->d_access,
417 		    desc->d_maxint, desc->d_fvbyte, desc->d_rvbyte);
418 		printf("  maxcyl/head/sect %d/%d/%d, maxvsect %d, inter %d\n",
419 		    desc->d_maxcyl, desc->d_maxhead, desc->d_maxsect,
420 		    desc->d_maxvsectl, desc->d_interleave);
421 		printf("%s", sc->sc_dev.dv_xname);
422 	}
423 #endif
424 
425 	/*
426 	 * Take care of a couple of anomolies:
427 	 * 1. 7945A and 7946A both return same HW id
428 	 * 2. 9122S and 9134D both return same HW id
429 	 * 3. 9122D and 9134L both return same HW id
430 	 */
431 	switch (ha->ha_id) {
432 	case RD7946AID:
433 		if (bcmp(name, "079450", 6) == 0)
434 			id = RD7945A;
435 		else
436 			id = RD7946A;
437 		break;
438 
439 	case RD9134LID:
440 		if (bcmp(name, "091340", 6) == 0)
441 			id = RD9134L;
442 		else
443 			id = RD9122D;
444 		break;
445 
446 	case RD9134DID:
447 		if (bcmp(name, "091220", 6) == 0)
448 			id = RD9122S;
449 		else
450 			id = RD9134D;
451 		break;
452 	}
453 
454 	sc->sc_type = id;
455 
456 	/*
457 	 * XXX We use DEV_BSIZE instead of the sector size value pulled
458 	 * XXX off the driver because all of this code assumes 512 byte
459 	 * XXX blocks.  ICK!
460 	 */
461 	printf(": %s\n", rdidentinfo[id].ri_desc);
462 	printf("%s: %d cylinders, %d heads, %d blocks, %d bytes/block\n",
463 	    sc->sc_dev.dv_xname, rdidentinfo[id].ri_ncyl,
464 	    rdidentinfo[id].ri_ntpc, rdidentinfo[id].ri_nblocks,
465 	    DEV_BSIZE);
466 
467 	return (1);
468 }
469 
470 void
471 rdreset(rs)
472 	struct rd_softc *rs;
473 {
474 	int ctlr = rs->sc_dev.dv_parent->dv_unit;
475 	int slave = rs->sc_slave;
476 	u_char stat;
477 
478 	rs->sc_clear.c_unit = C_SUNIT(rs->sc_punit);
479 	rs->sc_clear.c_cmd = C_CLEAR;
480 	hpibsend(ctlr, slave, C_TCMD, &rs->sc_clear, sizeof(rs->sc_clear));
481 	hpibswait(ctlr, slave);
482 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
483 
484 	rs->sc_src.c_unit = C_SUNIT(RDCTLR);
485 	rs->sc_src.c_nop = C_NOP;
486 	rs->sc_src.c_cmd = C_SREL;
487 	rs->sc_src.c_param = C_REL;
488 	hpibsend(ctlr, slave, C_CMD, &rs->sc_src, sizeof(rs->sc_src));
489 	hpibswait(ctlr, slave);
490 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
491 
492 	rs->sc_ssmc.c_unit = C_SUNIT(rs->sc_punit);
493 	rs->sc_ssmc.c_cmd = C_SSM;
494 	rs->sc_ssmc.c_refm = REF_MASK;
495 	rs->sc_ssmc.c_fefm = FEF_MASK;
496 	rs->sc_ssmc.c_aefm = AEF_MASK;
497 	rs->sc_ssmc.c_iefm = IEF_MASK;
498 	hpibsend(ctlr, slave, C_CMD, &rs->sc_ssmc, sizeof(rs->sc_ssmc));
499 	hpibswait(ctlr, slave);
500 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
501 #ifdef DEBUG
502 	rs->sc_stats.rdresets++;
503 #endif
504 }
505 
506 /*
507  * Read or constuct a disklabel
508  */
509 int
510 rdgetinfo(dev)
511 	dev_t dev;
512 {
513 	int unit = rdunit(dev);
514 	struct rd_softc *rs = rd_cd.cd_devs[unit];
515 	struct disklabel *lp = rs->sc_dkdev.dk_label;
516 	struct partition *pi;
517 	char *msg;
518 
519 	/*
520 	 * Set some default values to use while reading the label
521 	 * or to use if there isn't a label.
522 	 */
523 	bzero((caddr_t)lp, sizeof *lp);
524 	lp->d_type = DTYPE_HPIB;
525 	lp->d_secsize = DEV_BSIZE;
526 	lp->d_nsectors = 32;
527 	lp->d_ntracks = 20;
528 	lp->d_ncylinders = 1;
529 	lp->d_secpercyl = 32*20;
530 	lp->d_npartitions = 3;
531 	lp->d_partitions[2].p_offset = 0;
532 	lp->d_partitions[2].p_size = LABELSECTOR+1;
533 
534 	/*
535 	 * Now try to read the disklabel
536 	 */
537 	msg = readdisklabel(rdlabdev(dev), rdstrategy, lp, NULL);
538 	if (msg == NULL)
539 		return (0);
540 
541 	pi = lp->d_partitions;
542 	printf("%s: WARNING: %s, ", rs->sc_dev.dv_xname, msg);
543 #ifdef COMPAT_NOLABEL
544 	printf("using old default partitioning\n");
545 	rdmakedisklabel(unit, lp);
546 #else
547 	printf("defining `c' partition as entire disk\n");
548 	pi[2].p_size = rdidentinfo[rs->sc_type].ri_nblocks;
549 	/* XXX reset other info since readdisklabel screws with it */
550 	lp->d_npartitions = 3;
551 	pi[0].p_size = 0;
552 #endif
553 	return(0);
554 }
555 
556 int
557 rdopen(dev, flags, mode, p)
558 	dev_t dev;
559 	int flags, mode;
560 	struct proc *p;
561 {
562 	int unit = rdunit(dev);
563 	struct rd_softc *rs;
564 	int error, mask, part;
565 
566 	if (unit >= rd_cd.cd_ndevs ||
567 	    (rs = rd_cd.cd_devs[unit]) == NULL ||
568 	    (rs->sc_flags & RDF_ALIVE) == 0)
569 		return (ENXIO);
570 
571 	/*
572 	 * Wait for any pending opens/closes to complete
573 	 */
574 	while (rs->sc_flags & (RDF_OPENING|RDF_CLOSING))
575 		(void) tsleep(rs, PRIBIO, "rdopen", 0);
576 
577 	/*
578 	 * On first open, get label and partition info.
579 	 * We may block reading the label, so be careful
580 	 * to stop any other opens.
581 	 */
582 	if (rs->sc_dkdev.dk_openmask == 0) {
583 		rs->sc_flags |= RDF_OPENING;
584 		error = rdgetinfo(dev);
585 		rs->sc_flags &= ~RDF_OPENING;
586 		wakeup((caddr_t)rs);
587 		if (error)
588 			return(error);
589 	}
590 
591 	part = rdpart(dev);
592 	mask = 1 << part;
593 
594 	/* Check that the partition exists. */
595 	if (part != RAW_PART &&
596 	    (part > rs->sc_dkdev.dk_label->d_npartitions ||
597 	     rs->sc_dkdev.dk_label->d_partitions[part].p_fstype == FS_UNUSED))
598 		return (ENXIO);
599 
600 	/* Ensure only one open at a time. */
601 	switch (mode) {
602 	case S_IFCHR:
603 		rs->sc_dkdev.dk_copenmask |= mask;
604 		break;
605 	case S_IFBLK:
606 		rs->sc_dkdev.dk_bopenmask |= mask;
607 		break;
608 	}
609 	rs->sc_dkdev.dk_openmask =
610 	    rs->sc_dkdev.dk_copenmask | rs->sc_dkdev.dk_bopenmask;
611 
612 	return(0);
613 }
614 
615 int
616 rdclose(dev, flag, mode, p)
617 	dev_t dev;
618 	int flag, mode;
619 	struct proc *p;
620 {
621 	int unit = rdunit(dev);
622 	struct rd_softc *rs = rd_cd.cd_devs[unit];
623 	struct disk *dk = &rs->sc_dkdev;
624 	int mask, s;
625 
626 	mask = 1 << rdpart(dev);
627 	if (mode == S_IFCHR)
628 		dk->dk_copenmask &= ~mask;
629 	else
630 		dk->dk_bopenmask &= ~mask;
631 	dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
632 	/*
633 	 * On last close, we wait for all activity to cease since
634 	 * the label/parition info will become invalid.  Since we
635 	 * might sleep, we must block any opens while we are here.
636 	 * Note we don't have to about other closes since we know
637 	 * we are the last one.
638 	 */
639 	if (dk->dk_openmask == 0) {
640 		rs->sc_flags |= RDF_CLOSING;
641 		s = splbio();
642 		while (rs->sc_active) {
643 			rs->sc_flags |= RDF_WANTED;
644 			(void) tsleep(&rs->sc_tab, PRIBIO, "rdclose", 0);
645 		}
646 		splx(s);
647 		rs->sc_flags &= ~(RDF_CLOSING|RDF_WLABEL);
648 		wakeup((caddr_t)rs);
649 	}
650 	return(0);
651 }
652 
653 void
654 rdstrategy(bp)
655 	struct buf *bp;
656 {
657 	int unit = rdunit(bp->b_dev);
658 	struct rd_softc *rs = rd_cd.cd_devs[unit];
659 	struct partition *pinfo;
660 	daddr_t bn;
661 	int sz, s;
662 	int offset;
663 
664 #ifdef DEBUG
665 	if (rddebug & RDB_FOLLOW)
666 		printf("rdstrategy(%p): dev %x, bn %x, bcount %lx, %c\n",
667 		       bp, bp->b_dev, bp->b_blkno, bp->b_bcount,
668 		       (bp->b_flags & B_READ) ? 'R' : 'W');
669 #endif
670 	bn = bp->b_blkno;
671 	sz = howmany(bp->b_bcount, DEV_BSIZE);
672 	pinfo = &rs->sc_dkdev.dk_label->d_partitions[rdpart(bp->b_dev)];
673 
674 	/* Don't perform partition translation on RAW_PART. */
675 	offset = (rdpart(bp->b_dev) == RAW_PART) ? 0 : pinfo->p_offset;
676 
677 	if (rdpart(bp->b_dev) != RAW_PART) {
678 		/*
679 		 * XXX This block of code belongs in
680 		 * XXX bounds_check_with_label()
681 		 */
682 
683 		if (bn < 0 || bn + sz > pinfo->p_size) {
684 			sz = pinfo->p_size - bn;
685 			if (sz == 0) {
686 				bp->b_resid = bp->b_bcount;
687 				goto done;
688 			}
689 			if (sz < 0) {
690 				bp->b_error = EINVAL;
691 				goto bad;
692 			}
693 			bp->b_bcount = dbtob(sz);
694 		}
695 		/*
696 		 * Check for write to write protected label
697 		 */
698 		if (bn + offset <= LABELSECTOR &&
699 #if LABELSECTOR != 0
700 		    bn + offset + sz > LABELSECTOR &&
701 #endif
702 		    !(bp->b_flags & B_READ) && !(rs->sc_flags & RDF_WLABEL)) {
703 			bp->b_error = EROFS;
704 			goto bad;
705 		}
706 	}
707 	bp->b_rawblkno = bn + offset;
708 	s = splbio();
709 	disksort_blkno(&rs->sc_tab, bp);
710 	if (rs->sc_active == 0) {
711 		rs->sc_active = 1;
712 		rdustart(rs);
713 	}
714 	splx(s);
715 	return;
716 bad:
717 	bp->b_flags |= B_ERROR;
718 done:
719 	biodone(bp);
720 }
721 
722 /*
723  * Called from timeout() when handling maintenance releases
724  */
725 void
726 rdrestart(arg)
727 	void *arg;
728 {
729 	int s = splbio();
730 	rdustart((struct rd_softc *)arg);
731 	splx(s);
732 }
733 
734 void
735 rdustart(rs)
736 	struct rd_softc *rs;
737 {
738 	struct buf *bp;
739 
740 	bp = BUFQ_FIRST(&rs->sc_tab);
741 	rs->sc_addr = bp->b_data;
742 	rs->sc_resid = bp->b_bcount;
743 	if (hpibreq(rs->sc_dev.dv_parent, &rs->sc_hq))
744 		rdstart(rs);
745 }
746 
747 struct buf *
748 rdfinish(rs, bp)
749 	struct rd_softc *rs;
750 	struct buf *bp;
751 {
752 
753 	rs->sc_errcnt = 0;
754 	BUFQ_REMOVE(&rs->sc_tab, bp);
755 	bp->b_resid = 0;
756 	biodone(bp);
757 	hpibfree(rs->sc_dev.dv_parent, &rs->sc_hq);
758 	if ((bp = BUFQ_FIRST(&rs->sc_tab)) != NULL)
759 		return (bp);
760 	rs->sc_active = 0;
761 	if (rs->sc_flags & RDF_WANTED) {
762 		rs->sc_flags &= ~RDF_WANTED;
763 		wakeup((caddr_t)&rs->sc_tab);
764 	}
765 	return (NULL);
766 }
767 
768 void
769 rdstart(arg)
770 	void *arg;
771 {
772 	struct rd_softc *rs = arg;
773 	struct buf *bp = BUFQ_FIRST(&rs->sc_tab);
774 	int part, ctlr, slave;
775 
776 	ctlr = rs->sc_dev.dv_parent->dv_unit;
777 	slave = rs->sc_slave;
778 
779 again:
780 #ifdef DEBUG
781 	if (rddebug & RDB_FOLLOW)
782 		printf("rdstart(%s): bp %p, %c\n", rs->sc_dev.dv_xname, bp,
783 		       (bp->b_flags & B_READ) ? 'R' : 'W');
784 #endif
785 	part = rdpart(bp->b_dev);
786 	rs->sc_flags |= RDF_SEEK;
787 	rs->sc_ioc.c_unit = C_SUNIT(rs->sc_punit);
788 	rs->sc_ioc.c_volume = C_SVOL(0);
789 	rs->sc_ioc.c_saddr = C_SADDR;
790 	rs->sc_ioc.c_hiaddr = 0;
791 	rs->sc_ioc.c_addr = RDBTOS(bp->b_rawblkno);
792 	rs->sc_ioc.c_nop2 = C_NOP;
793 	rs->sc_ioc.c_slen = C_SLEN;
794 	rs->sc_ioc.c_len = rs->sc_resid;
795 	rs->sc_ioc.c_cmd = bp->b_flags & B_READ ? C_READ : C_WRITE;
796 #ifdef DEBUG
797 	if (rddebug & RDB_IO)
798 		printf("rdstart: hpibsend(%x, %x, %x, %p, %x)\n",
799 		       ctlr, slave, C_CMD,
800 		       &rs->sc_ioc.c_unit, sizeof(rs->sc_ioc)-2);
801 #endif
802 	if (hpibsend(ctlr, slave, C_CMD, &rs->sc_ioc.c_unit,
803 		     sizeof(rs->sc_ioc)-2) == sizeof(rs->sc_ioc)-2) {
804 
805 		/* Instrumentation. */
806 		disk_busy(&rs->sc_dkdev);
807 		rs->sc_dkdev.dk_seek++;
808 
809 #ifdef DEBUG
810 		if (rddebug & RDB_IO)
811 			printf("rdstart: hpibawait(%x)\n", ctlr);
812 #endif
813 		hpibawait(ctlr);
814 		return;
815 	}
816 	/*
817 	 * Experience has shown that the hpibwait in this hpibsend will
818 	 * occasionally timeout.  It appears to occur mostly on old 7914
819 	 * drives with full maintenance tracks.  We should probably
820 	 * integrate this with the backoff code in rderror.
821 	 */
822 #ifdef DEBUG
823 	if (rddebug & RDB_ERROR)
824 		printf("%s: rdstart: cmd %x adr %lx blk %d len %d ecnt %d\n",
825 		       rs->sc_dev.dv_xname, rs->sc_ioc.c_cmd, rs->sc_ioc.c_addr,
826 		       bp->b_blkno, rs->sc_resid, rs->sc_errcnt);
827 	rs->sc_stats.rdretries++;
828 #endif
829 	rs->sc_flags &= ~RDF_SEEK;
830 	rdreset(rs);
831 	if (rs->sc_errcnt++ < RDRETRY)
832 		goto again;
833 	printf("%s: rdstart err: cmd 0x%x sect %ld blk %d len %d\n",
834 	       rs->sc_dev.dv_xname, rs->sc_ioc.c_cmd, rs->sc_ioc.c_addr,
835 	       bp->b_blkno, rs->sc_resid);
836 	bp->b_flags |= B_ERROR;
837 	bp->b_error = EIO;
838 	bp = rdfinish(rs, bp);
839 	if (bp) {
840 		rs->sc_addr = bp->b_data;
841 		rs->sc_resid = bp->b_bcount;
842 		if (hpibreq(rs->sc_dev.dv_parent, &rs->sc_hq))
843 			goto again;
844 	}
845 }
846 
847 void
848 rdgo(arg)
849 	void *arg;
850 {
851 	struct rd_softc *rs = arg;
852 	struct buf *bp = BUFQ_FIRST(&rs->sc_tab);
853 	int rw, ctlr, slave;
854 
855 	ctlr = rs->sc_dev.dv_parent->dv_unit;
856 	slave = rs->sc_slave;
857 
858 	rw = bp->b_flags & B_READ;
859 
860 	/* Instrumentation. */
861 	disk_busy(&rs->sc_dkdev);
862 
863 #ifdef USELEDS
864 	ledcontrol(0, 0, LED_DISK);
865 #endif
866 	hpibgo(ctlr, slave, C_EXEC, rs->sc_addr, rs->sc_resid, rw, rw != 0);
867 }
868 
869 /* ARGSUSED */
870 void
871 rdintr(arg)
872 	void *arg;
873 {
874 	struct rd_softc *rs = arg;
875 	int unit = rs->sc_dev.dv_unit;
876 	struct buf *bp = BUFQ_FIRST(&rs->sc_tab);
877 	u_char stat = 13;	/* in case hpibrecv fails */
878 	int rv, restart, ctlr, slave;
879 
880 	ctlr = rs->sc_dev.dv_parent->dv_unit;
881 	slave = rs->sc_slave;
882 
883 #ifdef DEBUG
884 	if (rddebug & RDB_FOLLOW)
885 		printf("rdintr(%d): bp %p, %c, flags %x\n", unit, bp,
886 		       (bp->b_flags & B_READ) ? 'R' : 'W', rs->sc_flags);
887 	if (bp == NULL) {
888 		printf("%s: bp == NULL\n", rs->sc_dev.dv_xname);
889 		return;
890 	}
891 #endif
892 	disk_unbusy(&rs->sc_dkdev, (bp->b_bcount - bp->b_resid));
893 
894 	if (rs->sc_flags & RDF_SEEK) {
895 		rs->sc_flags &= ~RDF_SEEK;
896 		if (hpibustart(ctlr))
897 			rdgo(rs);
898 		return;
899 	}
900 	if ((rs->sc_flags & RDF_SWAIT) == 0) {
901 #ifdef DEBUG
902 		rs->sc_stats.rdpolltries++;
903 #endif
904 		if (hpibpptest(ctlr, slave) == 0) {
905 #ifdef DEBUG
906 			rs->sc_stats.rdpollwaits++;
907 #endif
908 
909 			/* Instrumentation. */
910 			disk_busy(&rs->sc_dkdev);
911 			rs->sc_flags |= RDF_SWAIT;
912 			hpibawait(ctlr);
913 			return;
914 		}
915 	} else
916 		rs->sc_flags &= ~RDF_SWAIT;
917 	rv = hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
918 	if (rv != 1 || stat) {
919 #ifdef DEBUG
920 		if (rddebug & RDB_ERROR)
921 			printf("rdintr: recv failed or bad stat %d\n", stat);
922 #endif
923 		restart = rderror(unit);
924 #ifdef DEBUG
925 		rs->sc_stats.rdretries++;
926 #endif
927 		if (rs->sc_errcnt++ < RDRETRY) {
928 			if (restart)
929 				rdstart(rs);
930 			return;
931 		}
932 		bp->b_flags |= B_ERROR;
933 		bp->b_error = EIO;
934 	}
935 	if (rdfinish(rs, bp))
936 		rdustart(rs);
937 }
938 
939 int
940 rdstatus(rs)
941 	struct rd_softc *rs;
942 {
943 	int c, s;
944 	u_char stat;
945 	int rv;
946 
947 	c = rs->sc_dev.dv_parent->dv_unit;
948 	s = rs->sc_slave;
949 	rs->sc_rsc.c_unit = C_SUNIT(rs->sc_punit);
950 	rs->sc_rsc.c_sram = C_SRAM;
951 	rs->sc_rsc.c_ram = C_RAM;
952 	rs->sc_rsc.c_cmd = C_STATUS;
953 	bzero((caddr_t)&rs->sc_stat, sizeof(rs->sc_stat));
954 	rv = hpibsend(c, s, C_CMD, &rs->sc_rsc, sizeof(rs->sc_rsc));
955 	if (rv != sizeof(rs->sc_rsc)) {
956 #ifdef DEBUG
957 		if (rddebug & RDB_STATUS)
958 			printf("rdstatus: send C_CMD failed %d != %d\n",
959 			       rv, sizeof(rs->sc_rsc));
960 #endif
961 		return(1);
962 	}
963 	rv = hpibrecv(c, s, C_EXEC, &rs->sc_stat, sizeof(rs->sc_stat));
964 	if (rv != sizeof(rs->sc_stat)) {
965 #ifdef DEBUG
966 		if (rddebug & RDB_STATUS)
967 			printf("rdstatus: send C_EXEC failed %d != %d\n",
968 			       rv, sizeof(rs->sc_stat));
969 #endif
970 		return(1);
971 	}
972 	rv = hpibrecv(c, s, C_QSTAT, &stat, 1);
973 	if (rv != 1 || stat) {
974 #ifdef DEBUG
975 		if (rddebug & RDB_STATUS)
976 			printf("rdstatus: recv failed %d or bad stat %d\n",
977 			       rv, stat);
978 #endif
979 		return(1);
980 	}
981 	return(0);
982 }
983 
984 /*
985  * Deal with errors.
986  * Returns 1 if request should be restarted,
987  * 0 if we should just quietly give up.
988  */
989 int
990 rderror(unit)
991 	int unit;
992 {
993 	struct rd_softc *rs = rd_cd.cd_devs[unit];
994 	struct rd_stat *sp;
995 	struct buf *bp;
996 	daddr_t hwbn, pbn;
997 	char *hexstr __P((int, int)); /* XXX */
998 
999 	if (rdstatus(rs)) {
1000 #ifdef DEBUG
1001 		printf("%s: couldn't get status\n", rs->sc_dev.dv_xname);
1002 #endif
1003 		rdreset(rs);
1004 		return(1);
1005 	}
1006 	sp = &rs->sc_stat;
1007 	if (sp->c_fef & FEF_REXMT)
1008 		return(1);
1009 	if (sp->c_fef & FEF_PF) {
1010 		rdreset(rs);
1011 		return(1);
1012 	}
1013 	/*
1014 	 * Unit requests release for internal maintenance.
1015 	 * We just delay awhile and try again later.  Use expontially
1016 	 * increasing backoff ala ethernet drivers since we don't really
1017 	 * know how long the maintenance will take.  With RDWAITC and
1018 	 * RDRETRY as defined, the range is 1 to 32 seconds.
1019 	 */
1020 	if (sp->c_fef & FEF_IMR) {
1021 		extern int hz;
1022 		int rdtimo = RDWAITC << rs->sc_errcnt;
1023 #ifdef DEBUG
1024 		printf("%s: internal maintenance, %d second timeout\n",
1025 		       rs->sc_dev.dv_xname, rdtimo);
1026 		rs->sc_stats.rdtimeouts++;
1027 #endif
1028 		hpibfree(rs->sc_dev.dv_parent, &rs->sc_hq);
1029 		callout_reset(&rs->sc_restart_ch, rdtimo * hz, rdrestart, rs);
1030 		return(0);
1031 	}
1032 	/*
1033 	 * Only report error if we have reached the error reporting
1034 	 * threshhold.  By default, this will only report after the
1035 	 * retry limit has been exceeded.
1036 	 */
1037 	if (rs->sc_errcnt < rderrthresh)
1038 		return(1);
1039 
1040 	/*
1041 	 * First conjure up the block number at which the error occured.
1042 	 * Note that not all errors report a block number, in that case
1043 	 * we just use b_blkno.
1044  	 */
1045 	bp = BUFQ_FIRST(&rs->sc_tab);
1046 	pbn = rs->sc_dkdev.dk_label->d_partitions[rdpart(bp->b_dev)].p_offset;
1047 	if ((sp->c_fef & FEF_CU) || (sp->c_fef & FEF_DR) ||
1048 	    (sp->c_ief & IEF_RRMASK)) {
1049 		hwbn = RDBTOS(pbn + bp->b_blkno);
1050 		pbn = bp->b_blkno;
1051 	} else {
1052 		hwbn = sp->c_blk;
1053 		pbn = RDSTOB(hwbn) - pbn;
1054 	}
1055 	/*
1056 	 * Now output a generic message suitable for badsect.
1057 	 * Note that we don't use harderr cuz it just prints
1058 	 * out b_blkno which is just the beginning block number
1059 	 * of the transfer, not necessary where the error occured.
1060 	 */
1061 	printf("%s%c: hard error sn%d\n", rs->sc_dev.dv_xname,
1062 	    'a'+rdpart(bp->b_dev), pbn);
1063 	/*
1064 	 * Now report the status as returned by the hardware with
1065 	 * attempt at interpretation (unless debugging).
1066 	 */
1067 	printf("%s %s error:", rs->sc_dev.dv_xname,
1068 	    (bp->b_flags & B_READ) ? "read" : "write");
1069 #ifdef DEBUG
1070 	if (rddebug & RDB_ERROR) {
1071 		/* status info */
1072 		printf("\n    volume: %d, unit: %d\n",
1073 		       (sp->c_vu>>4)&0xF, sp->c_vu&0xF);
1074 		rdprinterr("reject", sp->c_ref, err_reject);
1075 		rdprinterr("fault", sp->c_fef, err_fault);
1076 		rdprinterr("access", sp->c_aef, err_access);
1077 		rdprinterr("info", sp->c_ief, err_info);
1078 		printf("    block: %d, P1-P10: ", hwbn);
1079 		printf("0x%x", *(u_int *)&sp->c_raw[0]);
1080 		printf("0x%x", *(u_int *)&sp->c_raw[4]);
1081 		printf("0x%x\n", *(u_short *)&sp->c_raw[8]);
1082 		/* command */
1083 		printf("    ioc: ");
1084 		printf("0x%x", *(u_int *)&rs->sc_ioc.c_pad);
1085 		printf("0x%x", *(u_short *)&rs->sc_ioc.c_hiaddr);
1086 		printf("0x%x", *(u_int *)&rs->sc_ioc.c_addr);
1087 		printf("0x%x", *(u_short *)&rs->sc_ioc.c_nop2);
1088 		printf("0x%x", *(u_int *)&rs->sc_ioc.c_len);
1089 		printf("0x%x\n", *(u_short *)&rs->sc_ioc.c_cmd);
1090 		return(1);
1091 	}
1092 #endif
1093 	printf(" v%d u%d, R0x%x F0x%x A0x%x I0x%x\n",
1094 	       (sp->c_vu>>4)&0xF, sp->c_vu&0xF,
1095 	       sp->c_ref, sp->c_fef, sp->c_aef, sp->c_ief);
1096 	printf("P1-P10: ");
1097 	printf("0x%x", *(u_int *)&sp->c_raw[0]);
1098 	printf("0x%x", *(u_int *)&sp->c_raw[4]);
1099 	printf("0x%x\n", *(u_short *)&sp->c_raw[8]);
1100 	return(1);
1101 }
1102 
1103 int
1104 rdread(dev, uio, flags)
1105 	dev_t dev;
1106 	struct uio *uio;
1107 	int flags;
1108 {
1109 
1110 	return (physio(rdstrategy, NULL, dev, B_READ, minphys, uio));
1111 }
1112 
1113 int
1114 rdwrite(dev, uio, flags)
1115 	dev_t dev;
1116 	struct uio *uio;
1117 	int flags;
1118 {
1119 
1120 	return (physio(rdstrategy, NULL, dev, B_WRITE, minphys, uio));
1121 }
1122 
1123 int
1124 rdioctl(dev, cmd, data, flag, p)
1125 	dev_t dev;
1126 	u_long cmd;
1127 	caddr_t data;
1128 	int flag;
1129 	struct proc *p;
1130 {
1131 	int unit = rdunit(dev);
1132 	struct rd_softc *sc = rd_cd.cd_devs[unit];
1133 	struct disklabel *lp = sc->sc_dkdev.dk_label;
1134 	int error, flags;
1135 
1136 	switch (cmd) {
1137 	case DIOCGDINFO:
1138 		*(struct disklabel *)data = *lp;
1139 		return (0);
1140 
1141 	case DIOCGPART:
1142 		((struct partinfo *)data)->disklab = lp;
1143 		((struct partinfo *)data)->part =
1144 			&lp->d_partitions[rdpart(dev)];
1145 		return (0);
1146 
1147 	case DIOCWLABEL:
1148 		if ((flag & FWRITE) == 0)
1149 			return (EBADF);
1150 		if (*(int *)data)
1151 			sc->sc_flags |= RDF_WLABEL;
1152 		else
1153 			sc->sc_flags &= ~RDF_WLABEL;
1154 		return (0);
1155 
1156 	case DIOCSDINFO:
1157 		if ((flag & FWRITE) == 0)
1158 			return (EBADF);
1159 		return (setdisklabel(lp, (struct disklabel *)data,
1160 				     (sc->sc_flags & RDF_WLABEL) ? 0
1161 				     : sc->sc_dkdev.dk_openmask,
1162 				     (struct cpu_disklabel *)0));
1163 
1164 	case DIOCWDINFO:
1165 		if ((flag & FWRITE) == 0)
1166 			return (EBADF);
1167 		error = setdisklabel(lp, (struct disklabel *)data,
1168 				     (sc->sc_flags & RDF_WLABEL) ? 0
1169 				     : sc->sc_dkdev.dk_openmask,
1170 				     (struct cpu_disklabel *)0);
1171 		if (error)
1172 			return (error);
1173 		flags = sc->sc_flags;
1174 		sc->sc_flags = RDF_ALIVE | RDF_WLABEL;
1175 		error = writedisklabel(rdlabdev(dev), rdstrategy, lp,
1176 				       (struct cpu_disklabel *)0);
1177 		sc->sc_flags = flags;
1178 		return (error);
1179 	}
1180 	return(EINVAL);
1181 }
1182 
1183 int
1184 rdsize(dev)
1185 	dev_t dev;
1186 {
1187 	int unit = rdunit(dev);
1188 	struct rd_softc *rs;
1189 	int psize, didopen = 0;
1190 
1191 	if (unit >= rd_cd.cd_ndevs ||
1192 	    (rs = rd_cd.cd_devs[unit]) == NULL ||
1193 	    (rs->sc_flags & RDF_ALIVE) == 0)
1194 		return (-1);
1195 
1196 	/*
1197 	 * We get called very early on (via swapconf)
1198 	 * without the device being open so we may need
1199 	 * to handle it here.
1200 	 */
1201 	if (rs->sc_dkdev.dk_openmask == 0) {
1202 		if (rdopen(dev, FREAD|FWRITE, S_IFBLK, NULL))
1203 			return(-1);
1204 		didopen = 1;
1205 	}
1206 	psize = rs->sc_dkdev.dk_label->d_partitions[rdpart(dev)].p_size *
1207 	    (rs->sc_dkdev.dk_label->d_secsize / DEV_BSIZE);
1208 	if (didopen)
1209 		(void) rdclose(dev, FREAD|FWRITE, S_IFBLK, NULL);
1210 	return (psize);
1211 }
1212 
1213 #ifdef DEBUG
1214 void
1215 rdprinterr(str, err, tab)
1216 	char *str;
1217 	short err;
1218 	char **tab;
1219 {
1220 	int i;
1221 	int printed;
1222 
1223 	if (err == 0)
1224 		return;
1225 	printf("    %s error %d field:", str, err);
1226 	printed = 0;
1227 	for (i = 0; i < 16; i++)
1228 		if (err & (0x8000 >> i))
1229 			printf("%s%s", printed++ ? " + " : " ", tab[i]);
1230 	printf("\n");
1231 }
1232 #endif
1233 
1234 static int rddoingadump;	/* simple mutex */
1235 
1236 /*
1237  * Non-interrupt driven, non-dma dump routine.
1238  */
1239 int
1240 rddump(dev, blkno, va, size)
1241 	dev_t dev;
1242 	daddr_t blkno;
1243 	caddr_t va;
1244 	size_t size;
1245 {
1246 	int sectorsize;		/* size of a disk sector */
1247 	int nsects;		/* number of sectors in partition */
1248 	int sectoff;		/* sector offset of partition */
1249 	int totwrt;		/* total number of sectors left to write */
1250 	int nwrt;		/* current number of sectors to write */
1251 	int unit, part;
1252 	int ctlr, slave;
1253 	struct rd_softc *rs;
1254 	struct disklabel *lp;
1255 	char stat;
1256 
1257 	/* Check for recursive dump; if so, punt. */
1258 	if (rddoingadump)
1259 		return (EFAULT);
1260 	rddoingadump = 1;
1261 
1262 	/* Decompose unit and partition. */
1263 	unit = rdunit(dev);
1264 	part = rdpart(dev);
1265 
1266 	/* Make sure dump device is ok. */
1267 	if (unit >= rd_cd.cd_ndevs ||
1268 	    (rs = rd_cd.cd_devs[unit]) == NULL ||
1269 	    (rs->sc_flags & RDF_ALIVE) == 0)
1270 		return (ENXIO);
1271 
1272 	ctlr = rs->sc_dev.dv_parent->dv_unit;
1273 	slave = rs->sc_slave;
1274 
1275 	/*
1276 	 * Convert to disk sectors.  Request must be a multiple of size.
1277 	 */
1278 	lp = rs->sc_dkdev.dk_label;
1279 	sectorsize = lp->d_secsize;
1280 	if ((size % sectorsize) != 0)
1281 		return (EFAULT);
1282 	totwrt = size / sectorsize;
1283 	blkno = dbtob(blkno) / sectorsize;	/* blkno in DEV_BSIZE units */
1284 
1285 	nsects = lp->d_partitions[part].p_size;
1286 	sectoff = lp->d_partitions[part].p_offset;
1287 
1288 	/* Check transfer bounds against partition size. */
1289 	if ((blkno < 0) || (blkno + totwrt) > nsects)
1290 		return (EINVAL);
1291 
1292 	/* Offset block number to start of partition. */
1293 	blkno += sectoff;
1294 
1295 	while (totwrt > 0) {
1296 		nwrt = totwrt;		/* XXX */
1297 #ifndef RD_DUMP_NOT_TRUSTED
1298 		/*
1299 		 * Fill out and send HPIB command.
1300 		 */
1301 		rs->sc_ioc.c_unit = C_SUNIT(rs->sc_punit);
1302 		rs->sc_ioc.c_volume = C_SVOL(0);
1303 		rs->sc_ioc.c_saddr = C_SADDR;
1304 		rs->sc_ioc.c_hiaddr = 0;
1305 		rs->sc_ioc.c_addr = RDBTOS(blkno);
1306 		rs->sc_ioc.c_nop2 = C_NOP;
1307 		rs->sc_ioc.c_slen = C_SLEN;
1308 		rs->sc_ioc.c_len = nwrt * sectorsize;
1309 		rs->sc_ioc.c_cmd = C_WRITE;
1310 		hpibsend(ctlr, slave, C_CMD, &rs->sc_ioc.c_unit,
1311 		    sizeof(rs->sc_ioc)-2);
1312 		if (hpibswait(ctlr, slave))
1313 			return (EIO);
1314 
1315 		/*
1316 		 * Send the data.
1317 		 */
1318 		hpibsend(ctlr, slave, C_EXEC, va, nwrt * sectorsize);
1319 		(void) hpibswait(ctlr, slave);
1320 		hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
1321 		if (stat)
1322 			return (EIO);
1323 #else /* RD_DUMP_NOT_TRUSTED */
1324 		/* Let's just talk about this first... */
1325 		printf("%s: dump addr %p, blk %d\n", sc->sc_dev.dv_xname,
1326 		    va, blkno);
1327 		delay(500 * 1000);	/* half a second */
1328 #endif /* RD_DUMP_NOT_TRUSTED */
1329 
1330 		/* update block count */
1331 		totwrt -= nwrt;
1332 		blkno += nwrt;
1333 		va += sectorsize * nwrt;
1334 	}
1335 	rddoingadump = 0;
1336 	return (0);
1337 }
1338