xref: /netbsd/sys/arch/hp300/dev/rd.c (revision c4a72b64)
1 /*	$NetBSD: rd.c,v 1.55 2002/11/01 11:31:52 mrg 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.55 2002/11/01 11:31:52 mrg 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, nokqfilter, 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 	    (bp->b_flags & B_READ));
916 
917 	if (rs->sc_flags & RDF_SEEK) {
918 		rs->sc_flags &= ~RDF_SEEK;
919 		if (hpibustart(ctlr))
920 			rdgo(rs);
921 		return;
922 	}
923 	if ((rs->sc_flags & RDF_SWAIT) == 0) {
924 #ifdef DEBUG
925 		rs->sc_stats.rdpolltries++;
926 #endif
927 		if (hpibpptest(ctlr, slave) == 0) {
928 #ifdef DEBUG
929 			rs->sc_stats.rdpollwaits++;
930 #endif
931 
932 			/* Instrumentation. */
933 			disk_busy(&rs->sc_dkdev);
934 			rs->sc_flags |= RDF_SWAIT;
935 			hpibawait(ctlr);
936 			return;
937 		}
938 	} else
939 		rs->sc_flags &= ~RDF_SWAIT;
940 	rv = hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
941 	if (rv != 1 || stat) {
942 #ifdef DEBUG
943 		if (rddebug & RDB_ERROR)
944 			printf("rdintr: recv failed or bad stat %d\n", stat);
945 #endif
946 		restart = rderror(unit);
947 #ifdef DEBUG
948 		rs->sc_stats.rdretries++;
949 #endif
950 		if (rs->sc_errcnt++ < RDRETRY) {
951 			if (restart)
952 				rdstart(rs);
953 			return;
954 		}
955 		bp->b_flags |= B_ERROR;
956 		bp->b_error = EIO;
957 	}
958 	if (rdfinish(rs, bp))
959 		rdustart(rs);
960 #if NRND > 0
961 	rnd_add_uint32(&rs->rnd_source, bp->b_blkno);
962 #endif
963 }
964 
965 int
966 rdstatus(rs)
967 	struct rd_softc *rs;
968 {
969 	int c, s;
970 	u_char stat;
971 	int rv;
972 
973 	c = rs->sc_dev.dv_parent->dv_unit;
974 	s = rs->sc_slave;
975 	rs->sc_rsc.c_unit = C_SUNIT(rs->sc_punit);
976 	rs->sc_rsc.c_sram = C_SRAM;
977 	rs->sc_rsc.c_ram = C_RAM;
978 	rs->sc_rsc.c_cmd = C_STATUS;
979 	memset((caddr_t)&rs->sc_stat, 0, sizeof(rs->sc_stat));
980 	rv = hpibsend(c, s, C_CMD, &rs->sc_rsc, sizeof(rs->sc_rsc));
981 	if (rv != sizeof(rs->sc_rsc)) {
982 #ifdef DEBUG
983 		if (rddebug & RDB_STATUS)
984 			printf("rdstatus: send C_CMD failed %d != %d\n",
985 			       rv, sizeof(rs->sc_rsc));
986 #endif
987 		return(1);
988 	}
989 	rv = hpibrecv(c, s, C_EXEC, &rs->sc_stat, sizeof(rs->sc_stat));
990 	if (rv != sizeof(rs->sc_stat)) {
991 #ifdef DEBUG
992 		if (rddebug & RDB_STATUS)
993 			printf("rdstatus: send C_EXEC failed %d != %d\n",
994 			       rv, sizeof(rs->sc_stat));
995 #endif
996 		return(1);
997 	}
998 	rv = hpibrecv(c, s, C_QSTAT, &stat, 1);
999 	if (rv != 1 || stat) {
1000 #ifdef DEBUG
1001 		if (rddebug & RDB_STATUS)
1002 			printf("rdstatus: recv failed %d or bad stat %d\n",
1003 			       rv, stat);
1004 #endif
1005 		return(1);
1006 	}
1007 	return(0);
1008 }
1009 
1010 /*
1011  * Deal with errors.
1012  * Returns 1 if request should be restarted,
1013  * 0 if we should just quietly give up.
1014  */
1015 int
1016 rderror(unit)
1017 	int unit;
1018 {
1019 	struct rd_softc *rs = rd_cd.cd_devs[unit];
1020 	struct rd_stat *sp;
1021 	struct buf *bp;
1022 	daddr_t hwbn, pbn;
1023 	char *hexstr __P((int, int)); /* XXX */
1024 
1025 	if (rdstatus(rs)) {
1026 #ifdef DEBUG
1027 		printf("%s: couldn't get status\n", rs->sc_dev.dv_xname);
1028 #endif
1029 		rdreset(rs);
1030 		return(1);
1031 	}
1032 	sp = &rs->sc_stat;
1033 	if (sp->c_fef & FEF_REXMT)
1034 		return(1);
1035 	if (sp->c_fef & FEF_PF) {
1036 		rdreset(rs);
1037 		return(1);
1038 	}
1039 	/*
1040 	 * Unit requests release for internal maintenance.
1041 	 * We just delay awhile and try again later.  Use expontially
1042 	 * increasing backoff ala ethernet drivers since we don't really
1043 	 * know how long the maintenance will take.  With RDWAITC and
1044 	 * RDRETRY as defined, the range is 1 to 32 seconds.
1045 	 */
1046 	if (sp->c_fef & FEF_IMR) {
1047 		extern int hz;
1048 		int rdtimo = RDWAITC << rs->sc_errcnt;
1049 #ifdef DEBUG
1050 		printf("%s: internal maintenance, %d second timeout\n",
1051 		       rs->sc_dev.dv_xname, rdtimo);
1052 		rs->sc_stats.rdtimeouts++;
1053 #endif
1054 		hpibfree(rs->sc_dev.dv_parent, &rs->sc_hq);
1055 		callout_reset(&rs->sc_restart_ch, rdtimo * hz, rdrestart, rs);
1056 		return(0);
1057 	}
1058 	/*
1059 	 * Only report error if we have reached the error reporting
1060 	 * threshhold.  By default, this will only report after the
1061 	 * retry limit has been exceeded.
1062 	 */
1063 	if (rs->sc_errcnt < rderrthresh)
1064 		return(1);
1065 
1066 	/*
1067 	 * First conjure up the block number at which the error occurred.
1068 	 * Note that not all errors report a block number, in that case
1069 	 * we just use b_blkno.
1070  	 */
1071 	bp = BUFQ_PEEK(&rs->sc_tab);
1072 	pbn = rs->sc_dkdev.dk_label->d_partitions[rdpart(bp->b_dev)].p_offset;
1073 	if ((sp->c_fef & FEF_CU) || (sp->c_fef & FEF_DR) ||
1074 	    (sp->c_ief & IEF_RRMASK)) {
1075 		hwbn = RDBTOS(pbn + bp->b_blkno);
1076 		pbn = bp->b_blkno;
1077 	} else {
1078 		hwbn = sp->c_blk;
1079 		pbn = RDSTOB(hwbn) - pbn;
1080 	}
1081 	/*
1082 	 * Now output a generic message suitable for badsect.
1083 	 * Note that we don't use harderr cuz it just prints
1084 	 * out b_blkno which is just the beginning block number
1085 	 * of the transfer, not necessary where the error occurred.
1086 	 */
1087 	printf("%s%c: hard error sn%d\n", rs->sc_dev.dv_xname,
1088 	    'a'+rdpart(bp->b_dev), pbn);
1089 	/*
1090 	 * Now report the status as returned by the hardware with
1091 	 * attempt at interpretation (unless debugging).
1092 	 */
1093 	printf("%s %s error:", rs->sc_dev.dv_xname,
1094 	    (bp->b_flags & B_READ) ? "read" : "write");
1095 #ifdef DEBUG
1096 	if (rddebug & RDB_ERROR) {
1097 		/* status info */
1098 		printf("\n    volume: %d, unit: %d\n",
1099 		       (sp->c_vu>>4)&0xF, sp->c_vu&0xF);
1100 		rdprinterr("reject", sp->c_ref, err_reject);
1101 		rdprinterr("fault", sp->c_fef, err_fault);
1102 		rdprinterr("access", sp->c_aef, err_access);
1103 		rdprinterr("info", sp->c_ief, err_info);
1104 		printf("    block: %d, P1-P10: ", hwbn);
1105 		printf("0x%x", *(u_int *)&sp->c_raw[0]);
1106 		printf("0x%x", *(u_int *)&sp->c_raw[4]);
1107 		printf("0x%x\n", *(u_short *)&sp->c_raw[8]);
1108 		/* command */
1109 		printf("    ioc: ");
1110 		printf("0x%x", *(u_int *)&rs->sc_ioc.c_pad);
1111 		printf("0x%x", *(u_short *)&rs->sc_ioc.c_hiaddr);
1112 		printf("0x%x", *(u_int *)&rs->sc_ioc.c_addr);
1113 		printf("0x%x", *(u_short *)&rs->sc_ioc.c_nop2);
1114 		printf("0x%x", *(u_int *)&rs->sc_ioc.c_len);
1115 		printf("0x%x\n", *(u_short *)&rs->sc_ioc.c_cmd);
1116 		return(1);
1117 	}
1118 #endif
1119 	printf(" v%d u%d, R0x%x F0x%x A0x%x I0x%x\n",
1120 	       (sp->c_vu>>4)&0xF, sp->c_vu&0xF,
1121 	       sp->c_ref, sp->c_fef, sp->c_aef, sp->c_ief);
1122 	printf("P1-P10: ");
1123 	printf("0x%x", *(u_int *)&sp->c_raw[0]);
1124 	printf("0x%x", *(u_int *)&sp->c_raw[4]);
1125 	printf("0x%x\n", *(u_short *)&sp->c_raw[8]);
1126 	return(1);
1127 }
1128 
1129 int
1130 rdread(dev, uio, flags)
1131 	dev_t dev;
1132 	struct uio *uio;
1133 	int flags;
1134 {
1135 
1136 	return (physio(rdstrategy, NULL, dev, B_READ, minphys, uio));
1137 }
1138 
1139 int
1140 rdwrite(dev, uio, flags)
1141 	dev_t dev;
1142 	struct uio *uio;
1143 	int flags;
1144 {
1145 
1146 	return (physio(rdstrategy, NULL, dev, B_WRITE, minphys, uio));
1147 }
1148 
1149 int
1150 rdioctl(dev, cmd, data, flag, p)
1151 	dev_t dev;
1152 	u_long cmd;
1153 	caddr_t data;
1154 	int flag;
1155 	struct proc *p;
1156 {
1157 	int unit = rdunit(dev);
1158 	struct rd_softc *sc = rd_cd.cd_devs[unit];
1159 	struct disklabel *lp = sc->sc_dkdev.dk_label;
1160 	int error, flags;
1161 
1162 	switch (cmd) {
1163 	case DIOCGDINFO:
1164 		*(struct disklabel *)data = *lp;
1165 		return (0);
1166 
1167 	case DIOCGPART:
1168 		((struct partinfo *)data)->disklab = lp;
1169 		((struct partinfo *)data)->part =
1170 			&lp->d_partitions[rdpart(dev)];
1171 		return (0);
1172 
1173 	case DIOCWLABEL:
1174 		if ((flag & FWRITE) == 0)
1175 			return (EBADF);
1176 		if (*(int *)data)
1177 			sc->sc_flags |= RDF_WLABEL;
1178 		else
1179 			sc->sc_flags &= ~RDF_WLABEL;
1180 		return (0);
1181 
1182 	case DIOCSDINFO:
1183 		if ((flag & FWRITE) == 0)
1184 			return (EBADF);
1185 		return (setdisklabel(lp, (struct disklabel *)data,
1186 				     (sc->sc_flags & RDF_WLABEL) ? 0
1187 				     : sc->sc_dkdev.dk_openmask,
1188 				     (struct cpu_disklabel *)0));
1189 
1190 	case DIOCWDINFO:
1191 		if ((flag & FWRITE) == 0)
1192 			return (EBADF);
1193 		error = setdisklabel(lp, (struct disklabel *)data,
1194 				     (sc->sc_flags & RDF_WLABEL) ? 0
1195 				     : sc->sc_dkdev.dk_openmask,
1196 				     (struct cpu_disklabel *)0);
1197 		if (error)
1198 			return (error);
1199 		flags = sc->sc_flags;
1200 		sc->sc_flags = RDF_ALIVE | RDF_WLABEL;
1201 		error = writedisklabel(rdlabdev(dev), rdstrategy, lp,
1202 				       (struct cpu_disklabel *)0);
1203 		sc->sc_flags = flags;
1204 		return (error);
1205 
1206 	case DIOCGDEFLABEL:
1207 		rdgetdefaultlabel(sc, (struct disklabel *)data);
1208 		return (0);
1209 	}
1210 	return(EINVAL);
1211 }
1212 
1213 void
1214 rdgetdefaultlabel(sc, lp)
1215 	struct rd_softc *sc;
1216 	struct disklabel *lp;
1217 {
1218 	int type = sc->sc_type;
1219 
1220 	memset((caddr_t)lp, 0, sizeof(struct disklabel));
1221 
1222 	lp->d_type = DTYPE_HPIB;
1223 	lp->d_secsize = DEV_BSIZE;
1224 	lp->d_nsectors = rdidentinfo[type].ri_nbpt;
1225 	lp->d_ntracks = rdidentinfo[type].ri_ntpc;
1226 	lp->d_ncylinders = rdidentinfo[type].ri_ncyl;
1227 	lp->d_secperunit = rdidentinfo[type].ri_nblocks;
1228 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1229 
1230 	strncpy(lp->d_typename, rdidentinfo[type].ri_desc, 16);
1231 	strncpy(lp->d_packname, "fictitious", 16);
1232 	lp->d_rpm = 3000;
1233 	lp->d_interleave = 1;
1234 	lp->d_flags = 0;
1235 
1236 	lp->d_partitions[RAW_PART].p_offset = 0;
1237 	lp->d_partitions[RAW_PART].p_size =
1238 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
1239 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1240 	lp->d_npartitions = RAW_PART + 1;
1241 
1242 	lp->d_magic = DISKMAGIC;
1243 	lp->d_magic2 = DISKMAGIC;
1244 	lp->d_checksum = dkcksum(lp);
1245 }
1246 
1247 int
1248 rdsize(dev)
1249 	dev_t dev;
1250 {
1251 	int unit = rdunit(dev);
1252 	struct rd_softc *rs;
1253 	int psize, didopen = 0;
1254 
1255 	if (unit >= rd_cd.cd_ndevs ||
1256 	    (rs = rd_cd.cd_devs[unit]) == NULL ||
1257 	    (rs->sc_flags & RDF_ALIVE) == 0)
1258 		return (-1);
1259 
1260 	/*
1261 	 * We get called very early on (via swapconf)
1262 	 * without the device being open so we may need
1263 	 * to handle it here.
1264 	 */
1265 	if (rs->sc_dkdev.dk_openmask == 0) {
1266 		if (rdopen(dev, FREAD|FWRITE, S_IFBLK, NULL))
1267 			return(-1);
1268 		didopen = 1;
1269 	}
1270 	psize = rs->sc_dkdev.dk_label->d_partitions[rdpart(dev)].p_size *
1271 	    (rs->sc_dkdev.dk_label->d_secsize / DEV_BSIZE);
1272 	if (didopen)
1273 		(void) rdclose(dev, FREAD|FWRITE, S_IFBLK, NULL);
1274 	return (psize);
1275 }
1276 
1277 #ifdef DEBUG
1278 void
1279 rdprinterr(str, err, tab)
1280 	char *str;
1281 	short err;
1282 	char **tab;
1283 {
1284 	int i;
1285 	int printed;
1286 
1287 	if (err == 0)
1288 		return;
1289 	printf("    %s error %d field:", str, err);
1290 	printed = 0;
1291 	for (i = 0; i < 16; i++)
1292 		if (err & (0x8000 >> i))
1293 			printf("%s%s", printed++ ? " + " : " ", tab[i]);
1294 	printf("\n");
1295 }
1296 #endif
1297 
1298 static int rddoingadump;	/* simple mutex */
1299 
1300 /*
1301  * Non-interrupt driven, non-dma dump routine.
1302  */
1303 int
1304 rddump(dev, blkno, va, size)
1305 	dev_t dev;
1306 	daddr_t blkno;
1307 	caddr_t va;
1308 	size_t size;
1309 {
1310 	int sectorsize;		/* size of a disk sector */
1311 	int nsects;		/* number of sectors in partition */
1312 	int sectoff;		/* sector offset of partition */
1313 	int totwrt;		/* total number of sectors left to write */
1314 	int nwrt;		/* current number of sectors to write */
1315 	int unit, part;
1316 	int ctlr, slave;
1317 	struct rd_softc *rs;
1318 	struct disklabel *lp;
1319 	char stat;
1320 
1321 	/* Check for recursive dump; if so, punt. */
1322 	if (rddoingadump)
1323 		return (EFAULT);
1324 	rddoingadump = 1;
1325 
1326 	/* Decompose unit and partition. */
1327 	unit = rdunit(dev);
1328 	part = rdpart(dev);
1329 
1330 	/* Make sure dump device is ok. */
1331 	if (unit >= rd_cd.cd_ndevs ||
1332 	    (rs = rd_cd.cd_devs[unit]) == NULL ||
1333 	    (rs->sc_flags & RDF_ALIVE) == 0)
1334 		return (ENXIO);
1335 
1336 	ctlr = rs->sc_dev.dv_parent->dv_unit;
1337 	slave = rs->sc_slave;
1338 
1339 	/*
1340 	 * Convert to disk sectors.  Request must be a multiple of size.
1341 	 */
1342 	lp = rs->sc_dkdev.dk_label;
1343 	sectorsize = lp->d_secsize;
1344 	if ((size % sectorsize) != 0)
1345 		return (EFAULT);
1346 	totwrt = size / sectorsize;
1347 	blkno = dbtob(blkno) / sectorsize;	/* blkno in DEV_BSIZE units */
1348 
1349 	nsects = lp->d_partitions[part].p_size;
1350 	sectoff = lp->d_partitions[part].p_offset;
1351 
1352 	/* Check transfer bounds against partition size. */
1353 	if ((blkno < 0) || (blkno + totwrt) > nsects)
1354 		return (EINVAL);
1355 
1356 	/* Offset block number to start of partition. */
1357 	blkno += sectoff;
1358 
1359 	while (totwrt > 0) {
1360 		nwrt = totwrt;		/* XXX */
1361 #ifndef RD_DUMP_NOT_TRUSTED
1362 		/*
1363 		 * Fill out and send HPIB command.
1364 		 */
1365 		rs->sc_ioc.c_unit = C_SUNIT(rs->sc_punit);
1366 		rs->sc_ioc.c_volume = C_SVOL(0);
1367 		rs->sc_ioc.c_saddr = C_SADDR;
1368 		rs->sc_ioc.c_hiaddr = 0;
1369 		rs->sc_ioc.c_addr = RDBTOS(blkno);
1370 		rs->sc_ioc.c_nop2 = C_NOP;
1371 		rs->sc_ioc.c_slen = C_SLEN;
1372 		rs->sc_ioc.c_len = nwrt * sectorsize;
1373 		rs->sc_ioc.c_cmd = C_WRITE;
1374 		hpibsend(ctlr, slave, C_CMD, &rs->sc_ioc.c_unit,
1375 		    sizeof(rs->sc_ioc)-2);
1376 		if (hpibswait(ctlr, slave))
1377 			return (EIO);
1378 
1379 		/*
1380 		 * Send the data.
1381 		 */
1382 		hpibsend(ctlr, slave, C_EXEC, va, nwrt * sectorsize);
1383 		(void) hpibswait(ctlr, slave);
1384 		hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
1385 		if (stat)
1386 			return (EIO);
1387 #else /* RD_DUMP_NOT_TRUSTED */
1388 		/* Let's just talk about this first... */
1389 		printf("%s: dump addr %p, blk %d\n", sc->sc_dev.dv_xname,
1390 		    va, blkno);
1391 		delay(500 * 1000);	/* half a second */
1392 #endif /* RD_DUMP_NOT_TRUSTED */
1393 
1394 		/* update block count */
1395 		totwrt -= nwrt;
1396 		blkno += nwrt;
1397 		va += sectorsize * nwrt;
1398 	}
1399 	rddoingadump = 0;
1400 	return (0);
1401 }
1402