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