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