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