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