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