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