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