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