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