xref: /netbsd-src/sys/arch/hp300/dev/rd.c (revision 122b5006ee1bd67145794b4cde92f4fe4781a5ec)
1 /*	$NetBSD: rd.c,v 1.110 2021/07/31 20:29:36 andvar 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.110 2021/07/31 20:29:36 andvar 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 #define RDB_FOLLOW	0x01
176 #define RDB_STATUS	0x02
177 #define RDB_IDENT	0x04
178 #define RDB_IO		0x08
179 #define RDB_ASYNC	0x10
180 #define RDB_ERROR	0x80
181 int	rddebug = RDB_ERROR | RDB_IDENT;
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 	[RD7945A] = {
190 		.ri_hwid = RD7946AID,
191 		.ri_desc = "7945A",
192 		.ri_nbpt = NRD7945ABPT,
193 		.ri_ntpc = NRD7945ATRK,
194 		.ri_ncyl = 968,
195 		.ri_nblocks = 108416
196 	},
197 
198 	[RD9134D] = {
199 		.ri_hwid = RD9134DID,
200 		.ri_desc = "9134D",
201 		.ri_nbpt = NRD9134DBPT,
202 		.ri_ntpc = NRD9134DTRK,
203 		.ri_ncyl = 303,
204 		.ri_nblocks = 29088
205 	},
206 
207 	[RD9122S] = {
208 		.ri_hwid = RD9134LID,
209 		.ri_desc = "9122S",
210 		.ri_nbpt = NRD9122SBPT,
211 		.ri_ntpc = NRD9122STRK,
212 		.ri_ncyl = 77,
213 		.ri_nblocks = 1232
214 	},
215 
216 	[RD7912P] = {
217 		.ri_hwid = RD7912PID,
218 		.ri_desc = "7912P",
219 		.ri_nbpt = NRD7912PBPT,
220 		.ri_ntpc = NRD7912PTRK,
221 		.ri_ncyl = 572,
222 		.ri_nblocks = 128128
223 	},
224 
225 	[RD7914P] = {
226 		.ri_hwid = RD7914PID,
227 		.ri_desc = "7914P",
228 		.ri_nbpt = NRD7914PBPT,
229 		.ri_ntpc = NRD7914PTRK,
230 		.ri_ncyl = 1152,
231 		.ri_nblocks = 258048
232 	},
233 
234 	[RD7958A] = {
235 		.ri_hwid = RD7958AID,
236 		.ri_desc = "7958A",
237 		.ri_nbpt = NRD7958ABPT,
238 		.ri_ntpc = NRD7958ATRK,
239 		.ri_ncyl = 1013,
240 		.ri_nblocks = 255276
241 	},
242 
243 	[RD7957A] = {
244 		.ri_hwid = RD7957AID,
245 		.ri_desc = "7957A",
246 		.ri_nbpt = NRD7957ABPT,
247 		.ri_ntpc = NRD7957ATRK,
248 		.ri_ncyl = 1036,
249 		.ri_nblocks = 159544
250 	},
251 
252 	[RD7933H] = {
253 		.ri_hwid = RD7933HID,
254 		.ri_desc = "7933H",
255 		.ri_nbpt = NRD7933HBPT,
256 		.ri_ntpc = NRD7933HTRK,
257 		.ri_ncyl = 1321,
258 		.ri_nblocks = 789958
259 	},
260 
261 	[RD9134L] = {
262 		.ri_hwid = RD9134LID,
263 		.ri_desc = "9134L",
264 		.ri_nbpt = NRD9134LBPT,
265 		.ri_ntpc = NRD9134LTRK,
266 		.ri_ncyl = 973,
267 		.ri_nblocks = 77840
268 	},
269 
270 	[RD7936H] = {
271 		.ri_hwid = RD7936HID,
272 		.ri_desc = "7936H",
273 		.ri_nbpt = NRD7936HBPT,
274 		.ri_ntpc = NRD7936HTRK,
275 		.ri_ncyl = 698,
276 		.ri_nblocks = 600978
277 	},
278 
279 	[RD7937H] = {
280 		.ri_hwid = RD7937HID,
281 		.ri_desc = "7937H",
282 		.ri_nbpt = NRD7937HBPT,
283 		.ri_ntpc = NRD7937HTRK,
284 		.ri_ncyl = 698,
285 		.ri_nblocks = 1116102
286 	},
287 
288 	[RD7914CT] = {
289 		.ri_hwid = RD7914CTID,
290 		.ri_desc = "7914CT",
291 		.ri_nbpt = NRD7914PBPT,
292 		.ri_ntpc = NRD7914PTRK,
293 		.ri_ncyl = 1152,
294 		.ri_nblocks = 258048
295 	},
296 
297 	[RD7946A] = {
298 		.ri_hwid = RD7946AID,
299 		.ri_desc = "7946A",
300 		.ri_nbpt = NRD7945ABPT,
301 		.ri_ntpc = NRD7945ATRK,
302 		.ri_ncyl = 968,
303 		.ri_nblocks = 108416
304 	},
305 
306 	[RD9122D] = {
307 		.ri_hwid = RD9134LID,
308 		.ri_desc = "9122D",
309 		.ri_nbpt = NRD9122SBPT,
310 		.ri_ntpc = NRD9122STRK,
311 		.ri_ncyl = 77,
312 		.ri_nblocks = 1232
313 	},
314 
315 	[RD7957B] = {
316 		.ri_hwid = RD7957BID,
317 		.ri_desc = "7957B",
318 		.ri_nbpt = NRD7957BBPT,
319 		.ri_ntpc = NRD7957BTRK,
320 		.ri_ncyl = 1269,
321 		.ri_nblocks = 159894
322 	},
323 
324 	[RD7958B] = {
325 		.ri_hwid = RD7958BID,
326 		.ri_desc = "7958B",
327 		.ri_nbpt = NRD7958BBPT,
328 		.ri_ntpc = NRD7958BTRK,
329 		.ri_ncyl = 786,
330 		.ri_nblocks = 297108
331 	},
332 
333 	[RD7959B] = {
334 		.ri_hwid = RD7959BID,
335 		.ri_desc = "7959B",
336 		.ri_nbpt = NRD7959BBPT,
337 		.ri_ntpc = NRD7959BTRK,
338 		.ri_ncyl = 1572,
339 		.ri_nblocks = 594216
340 	},
341 
342 	[RD2200A] = {
343 		.ri_hwid = RD2200AID,
344 		.ri_desc = "2200A",
345 		.ri_nbpt = NRD2200ABPT,
346 		.ri_ntpc = NRD2200ATRK,
347 		.ri_ncyl = 1449,
348 		.ri_nblocks = 654948
349 	},
350 
351 	[RD2203A] = {
352 		.ri_hwid = RD2203AID,
353 		.ri_desc = "2203A",
354 		.ri_nbpt = NRD2203ABPT,
355 		.ri_ntpc = NRD2203ATRK,
356 		.ri_ncyl = 1449,
357 		.ri_nblocks = 1309896
358 	},
359 
360 	[RD2202A] = {
361 		.ri_hwid = RD2202AID,
362 		.ri_desc = "2202A",
363 		.ri_nbpt = NRD2202ABPT,
364 		.ri_ntpc = NRD2202ATRK,
365 		.ri_ncyl = 1449,
366 		.ri_nblocks = 1309896
367 	},
368 
369 	[RD7908A] = {
370 		.ri_hwid = RD7908AID,
371 		.ri_desc = "7908A",
372 		.ri_nbpt = NRD7908ABPT,
373 		.ri_ntpc = NRD7908ATRK,
374 		.ri_ncyl = 185,
375 		.ri_nblocks = 32375
376 	},
377 
378 	[RD7911A] = {
379 		.ri_hwid = RD7911AID,
380 		.ri_desc = "7911A",
381 		.ri_nbpt = NRD7911ABPT,
382 		.ri_ntpc = NRD7911ATRK,
383 		.ri_ncyl = 572,
384 		.ri_nblocks = 54912
385 	},
386 
387 	[RD7941A] = {
388 		.ri_hwid = RD7946AID,
389 		.ri_desc = "7941A",
390 		.ri_nbpt = NRD7941ABPT,
391 		.ri_ntpc = NRD7941ATRK,
392 		.ri_ncyl = 968,
393 		.ri_nblocks = 46464
394 	}
395 };
396 static const int numrdidentinfo = __arraycount(rdidentinfo);
397 
398 struct rdname2id {
399 	const char *rn_name;
400 	int rn_id;
401 };
402 static const struct rdname2id rdname2id[] = {
403 	{ RD7945ANAME,	RD7945A },
404 	{ RD9134DNAME,	RD9134D },
405 	{ RD7912PNAME,	RD7912P },
406 	{ RD7914PNAME,	RD7914P },
407 	{ RD7958ANAME,	RD7958A },
408 	{ RD7957ANAME,	RD7957A },
409 	{ RD7933HNAME,	RD7933H },
410 	{ RD9134LNAME,	RD9134L },
411 	{ RD7936HNAME,	RD7936H },
412 	{ RD7937HNAME,	RD7937H },
413 	{ RD7914CTNAME,	RD7914CT },
414 	{ RD9122DNAME,	RD9122D },
415 	{ RD7957BNAME,	RD7957B },
416 	{ RD7958BNAME,	RD7958B },
417 	{ RD7959BNAME,	RD7959B },
418 	{ RD2200ANAME,	RD2200A },
419 	{ RD2203ANAME,	RD2203A },
420 	{ RD2202ANAME,	RD2202A },
421 	{ RD7908ANAME,	RD7908A },
422 	{ RD7911ANAME,	RD7911A },
423 	{ RD7941ANAME,	RD7941A }
424 };
425 static const int numrdname2id = __arraycount(rdname2id);
426 
427 static int	rdident(device_t, struct rd_softc *,
428 		    struct hpibbus_attach_args *);
429 static void	rdreset(struct rd_softc *);
430 static void	rdustart(struct rd_softc *);
431 static int	rdgetinfo(dev_t);
432 static void	rdrestart(void *);
433 static struct buf *rdfinish(struct rd_softc *, struct buf *);
434 
435 static void	rdgetdefaultlabel(struct rd_softc *, struct disklabel *);
436 static void	rdrestart(void *);
437 static void	rdustart(struct rd_softc *);
438 static struct buf *rdfinish(struct rd_softc *, struct buf *);
439 static void	rdstart(void *);
440 static void	rdgo(void *);
441 static void	rdintr(void *);
442 static int	rdstatus(struct rd_softc *);
443 static int	rderror(int);
444 #ifdef DEBUG
445 static void	rdprinterr(const char *, short, const char **);
446 #endif
447 
448 static int	rdmatch(device_t, cfdata_t, void *);
449 static void	rdattach(device_t, device_t, void *);
450 
451 CFATTACH_DECL_NEW(rd, sizeof(struct rd_softc),
452     rdmatch, rdattach, NULL, NULL);
453 
454 static dev_type_open(rdopen);
455 static dev_type_close(rdclose);
456 static dev_type_read(rdread);
457 static dev_type_write(rdwrite);
458 static dev_type_ioctl(rdioctl);
459 static dev_type_strategy(rdstrategy);
460 static dev_type_dump(rddump);
461 static dev_type_size(rdsize);
462 
463 const struct bdevsw rd_bdevsw = {
464 	.d_open = rdopen,
465 	.d_close = rdclose,
466 	.d_strategy = rdstrategy,
467 	.d_ioctl = rdioctl,
468 	.d_dump = rddump,
469 	.d_psize = rdsize,
470 	.d_discard = nodiscard,
471 	.d_flag = D_DISK
472 };
473 
474 const struct cdevsw rd_cdevsw = {
475 	.d_open = rdopen,
476 	.d_close = rdclose,
477 	.d_read = rdread,
478 	.d_write = rdwrite,
479 	.d_ioctl = rdioctl,
480 	.d_stop = nostop,
481 	.d_tty = notty,
482 	.d_poll = nopoll,
483 	.d_mmap = nommap,
484 	.d_kqfilter = nokqfilter,
485 	.d_discard = nodiscard,
486 	.d_flag = D_DISK
487 };
488 
489 static int
490 rdmatch(device_t parent, cfdata_t cf, void *aux)
491 {
492 	struct hpibbus_attach_args *ha = aux;
493 	struct rd_clearcmd ccmd;
494 	int ctlr, slave, punit;
495 	int rv;
496 	uint8_t stat;
497 
498 	rv = rdident(parent, NULL, ha);
499 
500 	if (rv == 0)
501 		return 0;
502 
503 	/*
504 	 * The supported device ID is probed.
505 	 * Check if the specified physical unit is actually supported
506 	 * by brandnew HP-IB emulator devices like HPDisk and HPDrive etc.
507 	 */
508 	ctlr  = device_unit(parent);
509 	slave = ha->ha_slave;
510 	punit = ha->ha_punit;
511 	if (punit == 0)
512 		return 1;
513 
514 	ccmd.c_unit = C_SUNIT(punit);
515 	ccmd.c_cmd  = C_CLEAR;
516 	hpibsend(ctlr, slave, C_TCMD, &ccmd, sizeof(ccmd));
517 	hpibswait(ctlr, slave);
518 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
519 	if (stat != 0)
520 		return 0;
521 
522 	return 1;
523 }
524 
525 static void
526 rdattach(device_t parent, device_t self, void *aux)
527 {
528 	struct rd_softc *sc = device_private(self);
529 	struct hpibbus_attach_args *ha = aux;
530 	int id;
531 	char pbuf[9];
532 
533 	sc->sc_dev = self;
534 	bufq_alloc(&sc->sc_tab, "disksort", BUFQ_SORT_RAWBLOCK);
535 
536 	if (rdident(parent, sc, ha) == 0) {
537 		aprint_error(": didn't respond to describe command!\n");
538 		return;
539 	}
540 
541 	/*
542 	 * XXX We use DEV_BSIZE instead of the sector size value pulled
543 	 * XXX off the driver because all of this code assumes 512 byte
544 	 * XXX blocks.  ICK!
545 	 */
546 	id = sc->sc_type;
547 	aprint_normal(": %s\n", rdidentinfo[id].ri_desc);
548 	format_bytes(pbuf, sizeof(pbuf),
549 	    rdidentinfo[id].ri_nblocks * DEV_BSIZE);
550 	aprint_normal_dev(sc->sc_dev, "%s, %d cyl, %d head, %d sec,"
551 	    " %d bytes/block x %u blocks\n",
552 	    pbuf, rdidentinfo[id].ri_ncyl, rdidentinfo[id].ri_ntpc,
553 	    rdidentinfo[id].ri_nbpt,
554 	    DEV_BSIZE, rdidentinfo[id].ri_nblocks);
555 
556 	/*
557 	 * Initialize and attach the disk structure.
558 	 */
559 	memset(&sc->sc_dkdev, 0, sizeof(sc->sc_dkdev));
560 	disk_init(&sc->sc_dkdev, device_xname(sc->sc_dev), NULL);
561 	disk_attach(&sc->sc_dkdev);
562 
563 	sc->sc_slave = ha->ha_slave;
564 	sc->sc_punit = ha->ha_punit;
565 
566 	callout_init(&sc->sc_restart_ch, 0);
567 
568 	/* Initialize the hpib job queue entry */
569 	sc->sc_hq.hq_softc = sc;
570 	sc->sc_hq.hq_slave = sc->sc_slave;
571 	sc->sc_hq.hq_start = rdstart;
572 	sc->sc_hq.hq_go = rdgo;
573 	sc->sc_hq.hq_intr = rdintr;
574 
575 	sc->sc_flags = RDF_ALIVE;
576 #ifdef DEBUG
577 	/* always report errors */
578 	if (rddebug & RDB_ERROR)
579 		rderrthresh = 0;
580 #endif
581 	/*
582 	 * attach the device into the random source list
583 	 */
584 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
585 	    RND_TYPE_DISK, RND_FLAG_DEFAULT);
586 }
587 
588 static int
589 rdident(device_t parent, struct rd_softc *sc, struct hpibbus_attach_args *ha)
590 {
591 	struct cs80_describe desc;
592 	u_char stat, cmd[3];
593 	char name[7];
594 	int i, id, n, ctlr, slave;
595 
596 	ctlr = device_unit(parent);
597 	slave = ha->ha_slave;
598 
599 	/* Verify that we have a CS80 device. */
600 	if ((ha->ha_id & 0x200) == 0)
601 		return 0;
602 
603 	/* Is it one of the disks we support? */
604 	for (id = 0; id < numrdidentinfo; id++)
605 		if (ha->ha_id == rdidentinfo[id].ri_hwid)
606 			break;
607 	if (id == numrdidentinfo)
608 		return 0;
609 
610 	/*
611 	 * If we're just probing for the device, that's all the
612 	 * work we need to do.
613 	 */
614 	if (sc == NULL)
615 		return 1;
616 
617 	/*
618 	 * Reset device and collect description
619 	 */
620 	rdreset(sc);
621 	cmd[0] = C_SUNIT(ha->ha_punit);
622 	cmd[1] = C_SVOL(0);
623 	cmd[2] = C_DESC;
624 	hpibsend(ctlr, slave, C_CMD, cmd, sizeof(cmd));
625 	hpibrecv(ctlr, slave, C_EXEC, &desc, sizeof(desc));
626 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
627 	memset(name, 0, sizeof(name));
628 	if (stat == 0) {
629 		n = desc.d_name;
630 		for (i = 5; i >= 0; i--) {
631 			name[i] = (n & 0xf) + '0';
632 			n >>= 4;
633 		}
634 	}
635 
636 #ifdef DEBUG
637 	if (rddebug & RDB_IDENT) {
638 		aprint_normal("\n");
639 		aprint_normal_dev(sc->sc_dev, "id: 0x%04x, name: %x ('%s')\n",
640 		    ha->ha_id, desc.d_name, name);
641 		aprint_normal("  iuw %x, maxxfr %d, ctype %d\n",
642 		    desc.d_iuw, desc.d_cmaxxfr, desc.d_ctype);
643 		aprint_normal("  utype %d, bps %d, blkbuf %d, burst %d,"
644 		    " blktime %d\n",
645 		    desc.d_utype, desc.d_sectsize,
646 		    desc.d_blkbuf, desc.d_burstsize, desc.d_blocktime);
647 		aprint_normal("  avxfr %d, ort %d, atp %d, maxint %d, fv %x"
648 		    ", rv %x\n",
649 		    desc.d_uavexfr, desc.d_retry, desc.d_access,
650 		    desc.d_maxint, desc.d_fvbyte, desc.d_rvbyte);
651 		aprint_normal("  maxcyl/head/sect %d/%d/%d, maxvsect %d,"
652 		    " inter %d\n",
653 		    desc.d_maxcyl, desc.d_maxhead, desc.d_maxsect,
654 		    desc.d_maxvsectl, desc.d_interleave);
655 		aprint_normal("%s", device_xname(sc->sc_dev));
656 	}
657 #endif
658 
659 	/*
660 	 * Take care of a couple of anomolies:
661 	 * 1. 7945A, 7946A, and 7941A all return same HW id
662 	 * 2. 9122S and 9134D both return same HW id
663 	 * 3. 9122D and 9134L both return same HW id
664 	 */
665 	switch (ha->ha_id) {
666 	case RD7946AID:
667 		if (memcmp(name, RD7945ANAME, RDNAMELEN) == 0)
668 			id = RD7945A;
669 		else if (memcmp(name, RD7941ANAME, RDNAMELEN) == 0)
670 			id = RD7941A;
671 		else
672 			id = RD7946A;
673 		break;
674 
675 	case RD9134LID:
676 		if (memcmp(name, RD9134LNAME, RDNAMELEN) == 0)
677 			id = RD9134L;
678 		else
679 			id = RD9122D;
680 		break;
681 
682 	case RD9134DID:
683 		if (memcmp(name, RD9122SNAME, RDNAMELEN) == 0)
684 			id = RD9122S;
685 		else
686 			id = RD9134D;
687 		break;
688 	}
689 
690 	/*
691 	 * HPDisk can have independent physical units that are not
692 	 * corresponding to device IDs.
693 	 * To handle this, we have to check names in the drive description
694 	 * data for punit >= 1.
695 	 */
696 	if (ha->ha_punit >= 1) {
697 		for (i = 0; i < numrdname2id; i++) {
698 			if (memcmp(name, rdname2id[i].rn_name,
699 			    RDNAMELEN) == 0) {
700 				id = rdname2id[i].rn_id;
701 				break;
702 			}
703 		}
704 	}
705 
706 	sc->sc_type = id;
707 
708 	return 1;
709 }
710 
711 static void
712 rdreset(struct rd_softc *sc)
713 {
714 	int ctlr = device_unit(device_parent(sc->sc_dev));
715 	int slave = sc->sc_slave;
716 	u_char stat;
717 
718 	sc->sc_clear.c_unit = C_SUNIT(sc->sc_punit);
719 	sc->sc_clear.c_cmd = C_CLEAR;
720 	hpibsend(ctlr, slave, C_TCMD, &sc->sc_clear, sizeof(sc->sc_clear));
721 	hpibswait(ctlr, slave);
722 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
723 
724 	sc->sc_src.c_unit = C_SUNIT(RDCTLR);
725 	sc->sc_src.c_nop = C_NOP;
726 	sc->sc_src.c_cmd = C_SREL;
727 	sc->sc_src.c_param = C_REL;
728 	hpibsend(ctlr, slave, C_CMD, &sc->sc_src, sizeof(sc->sc_src));
729 	hpibswait(ctlr, slave);
730 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
731 
732 	sc->sc_ssmc.c_unit = C_SUNIT(sc->sc_punit);
733 	sc->sc_ssmc.c_cmd = C_SSM;
734 	sc->sc_ssmc.c_refm = REF_MASK;
735 	sc->sc_ssmc.c_fefm = FEF_MASK;
736 	sc->sc_ssmc.c_aefm = AEF_MASK;
737 	sc->sc_ssmc.c_iefm = IEF_MASK;
738 	hpibsend(ctlr, slave, C_CMD, &sc->sc_ssmc, sizeof(sc->sc_ssmc));
739 	hpibswait(ctlr, slave);
740 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
741 #ifdef DEBUG
742 	sc->sc_stats.rdresets++;
743 #endif
744 }
745 
746 /*
747  * Read or construct a disklabel
748  */
749 static int
750 rdgetinfo(dev_t dev)
751 {
752 	struct rd_softc *sc = device_lookup_private(&rd_cd, rdunit(dev));
753 	struct disklabel *lp = sc->sc_dkdev.dk_label;
754 	struct partition *pi;
755 	const char *msg;
756 
757 	/*
758 	 * Set some default values to use while reading the label
759 	 * or to use if there isn't a label.
760 	 */
761 	memset((void *)lp, 0, sizeof *lp);
762 	rdgetdefaultlabel(sc, lp);
763 
764 	/*
765 	 * Now try to read the disklabel
766 	 */
767 	msg = readdisklabel(rdlabdev(dev), rdstrategy, lp, NULL);
768 	if (msg == NULL)
769 		return 0;
770 
771 	pi = lp->d_partitions;
772 	printf("%s: WARNING: %s\n", device_xname(sc->sc_dev), msg);
773 
774 	pi[2].p_size = rdidentinfo[sc->sc_type].ri_nblocks;
775 	/* XXX reset other info since readdisklabel screws with it */
776 	lp->d_npartitions = 3;
777 	pi[0].p_size = 0;
778 
779 	return 0;
780 }
781 
782 static int
783 rdopen(dev_t dev, int flags, int mode, struct lwp *l)
784 {
785 	struct rd_softc *sc;
786 	int error, mask, part;
787 
788 	sc = device_lookup_private(&rd_cd, rdunit(dev));
789 	if (sc == NULL)
790 		return ENXIO;
791 
792 	if ((sc->sc_flags & RDF_ALIVE) == 0)
793 		return ENXIO;
794 
795 	/*
796 	 * Wait for any pending opens/closes to complete
797 	 */
798 	while (sc->sc_flags & (RDF_OPENING|RDF_CLOSING))
799 		(void) tsleep(sc, PRIBIO, "rdopen", 0);
800 
801 	/*
802 	 * On first open, get label and partition info.
803 	 * We may block reading the label, so be careful
804 	 * to stop any other opens.
805 	 */
806 	if (sc->sc_dkdev.dk_openmask == 0) {
807 		sc->sc_flags |= RDF_OPENING;
808 		error = rdgetinfo(dev);
809 		sc->sc_flags &= ~RDF_OPENING;
810 		wakeup((void *)sc);
811 		if (error)
812 			return error;
813 	}
814 
815 	part = rdpart(dev);
816 	mask = 1 << part;
817 
818 	/* Check that the partition exists. */
819 	if (part != RAW_PART &&
820 	    (part > sc->sc_dkdev.dk_label->d_npartitions ||
821 	     sc->sc_dkdev.dk_label->d_partitions[part].p_fstype == FS_UNUSED))
822 		return ENXIO;
823 
824 	/* Ensure only one open at a time. */
825 	switch (mode) {
826 	case S_IFCHR:
827 		sc->sc_dkdev.dk_copenmask |= mask;
828 		break;
829 	case S_IFBLK:
830 		sc->sc_dkdev.dk_bopenmask |= mask;
831 		break;
832 	}
833 	sc->sc_dkdev.dk_openmask =
834 	    sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
835 
836 	return 0;
837 }
838 
839 static int
840 rdclose(dev_t dev, int flag, int mode, struct lwp *l)
841 {
842 	struct rd_softc *sc = device_lookup_private(&rd_cd, rdunit(dev));
843 	struct disk *dk = &sc->sc_dkdev;
844 	int mask, s;
845 
846 	mask = 1 << rdpart(dev);
847 	if (mode == S_IFCHR)
848 		dk->dk_copenmask &= ~mask;
849 	else
850 		dk->dk_bopenmask &= ~mask;
851 	dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
852 	/*
853 	 * On last close, we wait for all activity to cease since
854 	 * the label/partition info will become invalid.  Since we
855 	 * might sleep, we must block any opens while we are here.
856 	 * Note we don't have to about other closes since we know
857 	 * we are the last one.
858 	 */
859 	if (dk->dk_openmask == 0) {
860 		sc->sc_flags |= RDF_CLOSING;
861 		s = splbio();
862 		while (sc->sc_active) {
863 			sc->sc_flags |= RDF_WANTED;
864 			(void) tsleep(&sc->sc_tab, PRIBIO, "rdclose", 0);
865 		}
866 		splx(s);
867 		sc->sc_flags &= ~(RDF_CLOSING|RDF_WLABEL);
868 		wakeup((void *)sc);
869 	}
870 	return 0;
871 }
872 
873 static void
874 rdstrategy(struct buf *bp)
875 {
876 	struct rd_softc *sc = device_lookup_private(&rd_cd, rdunit(bp->b_dev));
877 	struct partition *pinfo;
878 	daddr_t bn;
879 	int sz, s;
880 	int offset;
881 
882 #ifdef DEBUG
883 	if (rddebug & RDB_FOLLOW)
884 		printf("rdstrategy(%p): dev %"PRIx64", bn %llx, bcount %x, %c\n",
885 		       bp, bp->b_dev, bp->b_blkno, bp->b_bcount,
886 		       (bp->b_flags & B_READ) ? 'R' : 'W');
887 #endif
888 	bn = bp->b_blkno;
889 	sz = howmany(bp->b_bcount, DEV_BSIZE);
890 	pinfo = &sc->sc_dkdev.dk_label->d_partitions[rdpart(bp->b_dev)];
891 
892 	/* Don't perform partition translation on RAW_PART. */
893 	offset = (rdpart(bp->b_dev) == RAW_PART) ? 0 : pinfo->p_offset;
894 
895 	if (rdpart(bp->b_dev) != RAW_PART) {
896 		/*
897 		 * XXX This block of code belongs in
898 		 * XXX bounds_check_with_label()
899 		 */
900 
901 		if (bn < 0 || bn + sz > pinfo->p_size) {
902 			sz = pinfo->p_size - bn;
903 			if (sz == 0) {
904 				bp->b_resid = bp->b_bcount;
905 				goto done;
906 			}
907 			if (sz < 0) {
908 				bp->b_error = EINVAL;
909 				goto done;
910 			}
911 			bp->b_bcount = dbtob(sz);
912 		}
913 		/*
914 		 * Check for write to write protected label
915 		 */
916 		if (bn + offset <= LABELSECTOR &&
917 #if LABELSECTOR != 0
918 		    bn + offset + sz > LABELSECTOR &&
919 #endif
920 		    !(bp->b_flags & B_READ) && !(sc->sc_flags & RDF_WLABEL)) {
921 			bp->b_error = EROFS;
922 			goto done;
923 		}
924 	}
925 	bp->b_rawblkno = bn + offset;
926 	s = splbio();
927 	bufq_put(sc->sc_tab, bp);
928 	if (sc->sc_active == 0) {
929 		sc->sc_active = 1;
930 		rdustart(sc);
931 	}
932 	splx(s);
933 	return;
934 done:
935 	biodone(bp);
936 }
937 
938 /*
939  * Called from timeout() when handling maintenance releases
940  */
941 static void
942 rdrestart(void *arg)
943 {
944 	int s = splbio();
945 	rdustart((struct rd_softc *)arg);
946 	splx(s);
947 }
948 
949 static void
950 rdustart(struct rd_softc *sc)
951 {
952 	struct buf *bp;
953 
954 	bp = bufq_peek(sc->sc_tab);
955 	sc->sc_addr = bp->b_data;
956 	sc->sc_resid = bp->b_bcount;
957 	if (hpibreq(device_parent(sc->sc_dev), &sc->sc_hq))
958 		rdstart(sc);
959 }
960 
961 static struct buf *
962 rdfinish(struct rd_softc *sc, struct buf *bp)
963 {
964 
965 	sc->sc_errcnt = 0;
966 	(void)bufq_get(sc->sc_tab);
967 	bp->b_resid = 0;
968 	biodone(bp);
969 	hpibfree(device_parent(sc->sc_dev), &sc->sc_hq);
970 	if ((bp = bufq_peek(sc->sc_tab)) != NULL)
971 		return bp;
972 	sc->sc_active = 0;
973 	if (sc->sc_flags & RDF_WANTED) {
974 		sc->sc_flags &= ~RDF_WANTED;
975 		wakeup((void *)&sc->sc_tab);
976 	}
977 	return NULL;
978 }
979 
980 static void
981 rdstart(void *arg)
982 {
983 	struct rd_softc *sc = arg;
984 	struct buf *bp = bufq_peek(sc->sc_tab);
985 	int ctlr, slave;
986 
987 	ctlr = device_unit(device_parent(sc->sc_dev));
988 	slave = sc->sc_slave;
989 
990 again:
991 #ifdef DEBUG
992 	if (rddebug & RDB_FOLLOW)
993 		printf("rdstart(%s): bp %p, %c\n", device_xname(sc->sc_dev), bp,
994 		    (bp->b_flags & B_READ) ? 'R' : 'W');
995 #endif
996 	sc->sc_flags |= RDF_SEEK;
997 	sc->sc_ioc.c_unit = C_SUNIT(sc->sc_punit);
998 	sc->sc_ioc.c_volume = C_SVOL(0);
999 	sc->sc_ioc.c_saddr = C_SADDR;
1000 	sc->sc_ioc.c_hiaddr = 0;
1001 	sc->sc_ioc.c_addr = RDBTOS(bp->b_rawblkno);
1002 	sc->sc_ioc.c_nop2 = C_NOP;
1003 	sc->sc_ioc.c_slen = C_SLEN;
1004 	sc->sc_ioc.c_len = sc->sc_resid;
1005 	sc->sc_ioc.c_cmd = bp->b_flags & B_READ ? C_READ : C_WRITE;
1006 #ifdef DEBUG
1007 	if (rddebug & RDB_IO)
1008 		printf("rdstart: hpibsend(%x, %x, %x, %p, %x)\n",
1009 		       ctlr, slave, C_CMD,
1010 		       &sc->sc_ioc.c_unit, sizeof(sc->sc_ioc) - 2);
1011 #endif
1012 	if (hpibsend(ctlr, slave, C_CMD, &sc->sc_ioc.c_unit,
1013 		     sizeof(sc->sc_ioc) - 2) == sizeof(sc->sc_ioc) - 2) {
1014 
1015 		/* Instrumentation. */
1016 		disk_busy(&sc->sc_dkdev);
1017 		iostat_seek(sc->sc_dkdev.dk_stats);
1018 
1019 #ifdef DEBUG
1020 		if (rddebug & RDB_IO)
1021 			printf("rdstart: hpibawait(%x)\n", ctlr);
1022 #endif
1023 		hpibawait(ctlr);
1024 		return;
1025 	}
1026 	/*
1027 	 * Experience has shown that the hpibwait in this hpibsend will
1028 	 * occasionally timeout.  It appears to occur mostly on old 7914
1029 	 * drives with full maintenance tracks.  We should probably
1030 	 * integrate this with the backoff code in rderror.
1031 	 */
1032 #ifdef DEBUG
1033 	if (rddebug & RDB_ERROR)
1034 		printf("%s: rdstart: cmd %x adr %lx blk %lld len %d ecnt %d\n",
1035 		    device_xname(sc->sc_dev),
1036 		    sc->sc_ioc.c_cmd, sc->sc_ioc.c_addr,
1037 		    bp->b_blkno, sc->sc_resid, sc->sc_errcnt);
1038 	sc->sc_stats.rdretries++;
1039 #endif
1040 	sc->sc_flags &= ~RDF_SEEK;
1041 	rdreset(sc);
1042 	if (sc->sc_errcnt++ < RDRETRY)
1043 		goto again;
1044 	printf("%s: rdstart err: cmd 0x%x sect %ld blk %" PRId64 " len %d\n",
1045 	    device_xname(sc->sc_dev), sc->sc_ioc.c_cmd, sc->sc_ioc.c_addr,
1046 	    bp->b_blkno, sc->sc_resid);
1047 	bp->b_error = EIO;
1048 	bp = rdfinish(sc, bp);
1049 	if (bp) {
1050 		sc->sc_addr = bp->b_data;
1051 		sc->sc_resid = bp->b_bcount;
1052 		if (hpibreq(device_parent(sc->sc_dev), &sc->sc_hq))
1053 			goto again;
1054 	}
1055 }
1056 
1057 static void
1058 rdgo(void *arg)
1059 {
1060 	struct rd_softc *sc = arg;
1061 	struct buf *bp = bufq_peek(sc->sc_tab);
1062 	int rw, ctlr, slave;
1063 
1064 	ctlr = device_unit(device_parent(sc->sc_dev));
1065 	slave = sc->sc_slave;
1066 
1067 	rw = bp->b_flags & B_READ;
1068 
1069 	/* Instrumentation. */
1070 	disk_busy(&sc->sc_dkdev);
1071 
1072 #ifdef USELEDS
1073 	ledcontrol(0, 0, LED_DISK);
1074 #endif
1075 	hpibgo(ctlr, slave, C_EXEC, sc->sc_addr, sc->sc_resid, rw, rw != 0);
1076 }
1077 
1078 /* ARGSUSED */
1079 static void
1080 rdintr(void *arg)
1081 {
1082 	struct rd_softc *sc = arg;
1083 	int unit = device_unit(sc->sc_dev);
1084 	struct buf *bp = bufq_peek(sc->sc_tab);
1085 	u_char stat = 13;	/* in case hpibrecv fails */
1086 	int rv, restart, ctlr, slave;
1087 
1088 	ctlr = device_unit(device_parent(sc->sc_dev));
1089 	slave = sc->sc_slave;
1090 
1091 #ifdef DEBUG
1092 	if (rddebug & RDB_FOLLOW)
1093 		printf("rdintr(%d): bp %p, %c, flags %x\n", unit, bp,
1094 		    (bp->b_flags & B_READ) ? 'R' : 'W', sc->sc_flags);
1095 	if (bp == NULL) {
1096 		printf("%s: bp == NULL\n", device_xname(sc->sc_dev));
1097 		return;
1098 	}
1099 #endif
1100 	disk_unbusy(&sc->sc_dkdev, (bp->b_bcount - bp->b_resid),
1101 	    (bp->b_flags & B_READ));
1102 
1103 	if (sc->sc_flags & RDF_SEEK) {
1104 		sc->sc_flags &= ~RDF_SEEK;
1105 		if (hpibustart(ctlr))
1106 			rdgo(sc);
1107 		return;
1108 	}
1109 	if ((sc->sc_flags & RDF_SWAIT) == 0) {
1110 #ifdef DEBUG
1111 		sc->sc_stats.rdpolltries++;
1112 #endif
1113 		if (hpibpptest(ctlr, slave) == 0) {
1114 #ifdef DEBUG
1115 			sc->sc_stats.rdpollwaits++;
1116 #endif
1117 
1118 			/* Instrumentation. */
1119 			disk_busy(&sc->sc_dkdev);
1120 			sc->sc_flags |= RDF_SWAIT;
1121 			hpibawait(ctlr);
1122 			return;
1123 		}
1124 	} else
1125 		sc->sc_flags &= ~RDF_SWAIT;
1126 	rv = hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
1127 	if (rv != 1 || stat) {
1128 #ifdef DEBUG
1129 		if (rddebug & RDB_ERROR)
1130 			printf("rdintr: recv failed or bad stat %d\n", stat);
1131 #endif
1132 		restart = rderror(unit);
1133 #ifdef DEBUG
1134 		sc->sc_stats.rdretries++;
1135 #endif
1136 		if (sc->sc_errcnt++ < RDRETRY) {
1137 			if (restart)
1138 				rdstart(sc);
1139 			return;
1140 		}
1141 		bp->b_error = EIO;
1142 	}
1143 	if (rdfinish(sc, bp))
1144 		rdustart(sc);
1145 	rnd_add_uint32(&sc->rnd_source, bp->b_blkno);
1146 }
1147 
1148 static int
1149 rdstatus(struct rd_softc *sc)
1150 {
1151 	int c, s;
1152 	u_char stat;
1153 	int rv;
1154 
1155 	c = device_unit(device_parent(sc->sc_dev));
1156 	s = sc->sc_slave;
1157 	sc->sc_rsc.c_unit = C_SUNIT(sc->sc_punit);
1158 	sc->sc_rsc.c_sram = C_SRAM;
1159 	sc->sc_rsc.c_ram = C_RAM;
1160 	sc->sc_rsc.c_cmd = C_STATUS;
1161 	memset((void *)&sc->sc_stat, 0, sizeof(sc->sc_stat));
1162 	rv = hpibsend(c, s, C_CMD, &sc->sc_rsc, sizeof(sc->sc_rsc));
1163 	if (rv != sizeof(sc->sc_rsc)) {
1164 #ifdef DEBUG
1165 		if (rddebug & RDB_STATUS)
1166 			printf("rdstatus: send C_CMD failed %d != %d\n",
1167 			    rv, sizeof(sc->sc_rsc));
1168 #endif
1169 		return 1;
1170 	}
1171 	rv = hpibrecv(c, s, C_EXEC, &sc->sc_stat, sizeof(sc->sc_stat));
1172 	if (rv != sizeof(sc->sc_stat)) {
1173 #ifdef DEBUG
1174 		if (rddebug & RDB_STATUS)
1175 			printf("rdstatus: send C_EXEC failed %d != %d\n",
1176 			    rv, sizeof(sc->sc_stat));
1177 #endif
1178 		return 1;
1179 	}
1180 	rv = hpibrecv(c, s, C_QSTAT, &stat, 1);
1181 	if (rv != 1 || stat) {
1182 #ifdef DEBUG
1183 		if (rddebug & RDB_STATUS)
1184 			printf("rdstatus: recv failed %d or bad stat %d\n",
1185 			    rv, stat);
1186 #endif
1187 		return 1;
1188 	}
1189 	return 0;
1190 }
1191 
1192 /*
1193  * Deal with errors.
1194  * Returns 1 if request should be restarted,
1195  * 0 if we should just quietly give up.
1196  */
1197 static int
1198 rderror(int unit)
1199 {
1200 	struct rd_softc *sc = device_lookup_private(&rd_cd,unit);
1201 	struct rd_stat *sp;
1202 	struct buf *bp;
1203 	daddr_t hwbn, pbn;
1204 	char *hexstr(int, int); /* XXX */
1205 
1206 	if (rdstatus(sc)) {
1207 #ifdef DEBUG
1208 		printf("%s: couldn't get status\n", device_xname(sc->sc_dev));
1209 #endif
1210 		rdreset(sc);
1211 		return 1;
1212 	}
1213 	sp = &sc->sc_stat;
1214 	if (sp->c_fef & FEF_REXMT)
1215 		return 1;
1216 	if (sp->c_fef & FEF_PF) {
1217 		rdreset(sc);
1218 		return 1;
1219 	}
1220 	/*
1221 	 * Unit requests release for internal maintenance.
1222 	 * We just delay awhile and try again later.  Use expontially
1223 	 * increasing backoff ala ethernet drivers since we don't really
1224 	 * know how long the maintenance will take.  With RDWAITC and
1225 	 * RDRETRY as defined, the range is 1 to 32 seconds.
1226 	 */
1227 	if (sp->c_fef & FEF_IMR) {
1228 		extern int hz;
1229 		int rdtimo = RDWAITC << sc->sc_errcnt;
1230 #ifdef DEBUG
1231 		printf("%s: internal maintenance, %d second timeout\n",
1232 		    device_xname(sc->sc_dev), rdtimo);
1233 		sc->sc_stats.rdtimeouts++;
1234 #endif
1235 		hpibfree(device_parent(sc->sc_dev), &sc->sc_hq);
1236 		callout_reset(&sc->sc_restart_ch, rdtimo * hz, rdrestart, sc);
1237 		return 0;
1238 	}
1239 	/*
1240 	 * Only report error if we have reached the error reporting
1241 	 * threshold.  By default, this will only report after the
1242 	 * retry limit has been exceeded.
1243 	 */
1244 	if (sc->sc_errcnt < rderrthresh)
1245 		return 1;
1246 
1247 	/*
1248 	 * First conjure up the block number at which the error occurred.
1249 	 * Note that not all errors report a block number, in that case
1250 	 * we just use b_blkno.
1251 	 */
1252 	bp = bufq_peek(sc->sc_tab);
1253 	pbn = sc->sc_dkdev.dk_label->d_partitions[rdpart(bp->b_dev)].p_offset;
1254 	if ((sp->c_fef & FEF_CU) || (sp->c_fef & FEF_DR) ||
1255 	    (sp->c_ief & IEF_RRMASK)) {
1256 		hwbn = RDBTOS(pbn + bp->b_blkno);
1257 		pbn = bp->b_blkno;
1258 	} else {
1259 		hwbn = sp->c_blk;
1260 		pbn = RDSTOB(hwbn) - pbn;
1261 	}
1262 	/*
1263 	 * Now output a generic message suitable for badsect.
1264 	 * Note that we don't use harderr cuz it just prints
1265 	 * out b_blkno which is just the beginning block number
1266 	 * of the transfer, not necessary where the error occurred.
1267 	 */
1268 	printf("%s%c: hard error sn%" PRId64 "\n", device_xname(sc->sc_dev),
1269 	    'a'+rdpart(bp->b_dev), pbn);
1270 	/*
1271 	 * Now report the status as returned by the hardware with
1272 	 * attempt at interpretation (unless debugging).
1273 	 */
1274 	printf("%s %s error:", device_xname(sc->sc_dev),
1275 	    (bp->b_flags & B_READ) ? "read" : "write");
1276 #ifdef DEBUG
1277 	if (rddebug & RDB_ERROR) {
1278 		/* status info */
1279 		printf("\n    volume: %d, unit: %d\n",
1280 		    (sp->c_vu>>4)&0xF, sp->c_vu&0xF);
1281 		rdprinterr("reject", sp->c_ref, err_reject);
1282 		rdprinterr("fault", sp->c_fef, err_fault);
1283 		rdprinterr("access", sp->c_aef, err_access);
1284 		rdprinterr("info", sp->c_ief, err_info);
1285 		printf("    block: %lld, P1-P10: ", hwbn);
1286 		printf("0x%x", *(u_int *)&sp->c_raw[0]);
1287 		printf("0x%x", *(u_int *)&sp->c_raw[4]);
1288 		printf("0x%x\n", *(u_short *)&sp->c_raw[8]);
1289 		/* command */
1290 		printf("    ioc: ");
1291 		printf("0x%x", *(u_int *)&sc->sc_ioc.c_pad);
1292 		printf("0x%x", *(u_short *)&sc->sc_ioc.c_hiaddr);
1293 		printf("0x%x", *(u_int *)&sc->sc_ioc.c_addr);
1294 		printf("0x%x", *(u_short *)&sc->sc_ioc.c_nop2);
1295 		printf("0x%x", *(u_int *)&sc->sc_ioc.c_len);
1296 		printf("0x%x\n", *(u_short *)&sc->sc_ioc.c_cmd);
1297 		return 1;
1298 	}
1299 #endif
1300 	printf(" v%d u%d, R0x%x F0x%x A0x%x I0x%x\n",
1301 	    (sp->c_vu>>4)&0xF, sp->c_vu&0xF,
1302 	    sp->c_ref, sp->c_fef, sp->c_aef, sp->c_ief);
1303 	printf("P1-P10: ");
1304 	printf("0x%x", *(u_int *)&sp->c_raw[0]);
1305 	printf("0x%x", *(u_int *)&sp->c_raw[4]);
1306 	printf("0x%x\n", *(u_short *)&sp->c_raw[8]);
1307 	return 1;
1308 }
1309 
1310 static int
1311 rdread(dev_t dev, struct uio *uio, int flags)
1312 {
1313 
1314 	return physio(rdstrategy, NULL, dev, B_READ, minphys, uio);
1315 }
1316 
1317 static int
1318 rdwrite(dev_t dev, struct uio *uio, int flags)
1319 {
1320 
1321 	return physio(rdstrategy, NULL, dev, B_WRITE, minphys, uio);
1322 }
1323 
1324 static int
1325 rdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
1326 {
1327 	struct rd_softc *sc = device_lookup_private(&rd_cd, rdunit(dev));
1328 	struct disklabel *lp = sc->sc_dkdev.dk_label;
1329 	int error, flags;
1330 
1331 	error = disk_ioctl(&sc->sc_dkdev, rdpart(dev), cmd, data, flag, l);
1332 	if (error != EPASSTHROUGH)
1333 		return error;
1334 
1335 	switch (cmd) {
1336 	case DIOCWLABEL:
1337 		if ((flag & FWRITE) == 0)
1338 			return EBADF;
1339 		if (*(int *)data)
1340 			sc->sc_flags |= RDF_WLABEL;
1341 		else
1342 			sc->sc_flags &= ~RDF_WLABEL;
1343 		return 0;
1344 
1345 	case DIOCSDINFO:
1346 		if ((flag & FWRITE) == 0)
1347 			return EBADF;
1348 		return setdisklabel(lp, (struct disklabel *)data,
1349 		    (sc->sc_flags & RDF_WLABEL) ? 0 : sc->sc_dkdev.dk_openmask,
1350 		    NULL);
1351 
1352 	case DIOCWDINFO:
1353 		if ((flag & FWRITE) == 0)
1354 			return EBADF;
1355 		error = setdisklabel(lp, (struct disklabel *)data,
1356 		    (sc->sc_flags & RDF_WLABEL) ? 0 : sc->sc_dkdev.dk_openmask,
1357 		    NULL);
1358 		if (error)
1359 			return error;
1360 		flags = sc->sc_flags;
1361 		sc->sc_flags = RDF_ALIVE | RDF_WLABEL;
1362 		error = writedisklabel(rdlabdev(dev), rdstrategy, lp, NULL);
1363 		sc->sc_flags = flags;
1364 		return error;
1365 
1366 	case DIOCGDEFLABEL:
1367 		rdgetdefaultlabel(sc, (struct disklabel *)data);
1368 		return 0;
1369 	}
1370 	return EINVAL;
1371 }
1372 
1373 static void
1374 rdgetdefaultlabel(struct rd_softc *sc, struct disklabel *lp)
1375 {
1376 	int type = sc->sc_type;
1377 
1378 	memset((void *)lp, 0, sizeof(struct disklabel));
1379 
1380 	lp->d_type = DKTYPE_HPIB;
1381 	lp->d_secsize = DEV_BSIZE;
1382 	lp->d_nsectors = rdidentinfo[type].ri_nbpt;
1383 	lp->d_ntracks = rdidentinfo[type].ri_ntpc;
1384 	lp->d_ncylinders = rdidentinfo[type].ri_ncyl;
1385 	lp->d_secperunit = rdidentinfo[type].ri_nblocks;
1386 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1387 
1388 	strlcpy(lp->d_typename, rdidentinfo[type].ri_desc,
1389 	    sizeof(lp->d_typename));
1390 	strlcpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
1391 	lp->d_rpm = 3000;
1392 	lp->d_interleave = 1;
1393 	lp->d_flags = 0;
1394 
1395 	lp->d_partitions[RAW_PART].p_offset = 0;
1396 	lp->d_partitions[RAW_PART].p_size =
1397 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
1398 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1399 	lp->d_npartitions = RAW_PART + 1;
1400 
1401 	lp->d_magic = DISKMAGIC;
1402 	lp->d_magic2 = DISKMAGIC;
1403 	lp->d_checksum = dkcksum(lp);
1404 }
1405 
1406 int
1407 rdsize(dev_t dev)
1408 {
1409 	struct rd_softc *sc;
1410 	int psize, didopen = 0;
1411 
1412 	sc = device_lookup_private(&rd_cd, rdunit(dev));
1413 	if (sc == NULL)
1414 		return ENXIO;
1415 
1416 	if ((sc->sc_flags & RDF_ALIVE) == 0)
1417 		return ENXIO;
1418 
1419 	/*
1420 	 * We get called very early on (via swapconf)
1421 	 * without the device being open so we may need
1422 	 * to handle it here.
1423 	 */
1424 	if (sc->sc_dkdev.dk_openmask == 0) {
1425 		if (rdopen(dev, FREAD|FWRITE, S_IFBLK, NULL))
1426 			return -1;
1427 		didopen = 1;
1428 	}
1429 	psize = sc->sc_dkdev.dk_label->d_partitions[rdpart(dev)].p_size *
1430 	    (sc->sc_dkdev.dk_label->d_secsize / DEV_BSIZE);
1431 	if (didopen)
1432 		(void)rdclose(dev, FREAD|FWRITE, S_IFBLK, NULL);
1433 	return psize;
1434 }
1435 
1436 #ifdef DEBUG
1437 static void
1438 rdprinterr(const char *str, short err, const char **tab)
1439 {
1440 	int i;
1441 	int printed;
1442 
1443 	if (err == 0)
1444 		return;
1445 	printf("    %s error %d field:", str, err);
1446 	printed = 0;
1447 	for (i = 0; i < 16; i++)
1448 		if (err & (0x8000 >> i))
1449 			printf("%s%s", printed++ ? " + " : " ", tab[i]);
1450 	printf("\n");
1451 }
1452 #endif
1453 
1454 static int rddoingadump;	/* simple mutex */
1455 
1456 /*
1457  * Non-interrupt driven, non-DMA dump routine.
1458  */
1459 static int
1460 rddump(dev_t dev, daddr_t blkno, void *va, size_t size)
1461 {
1462 	int sectorsize;		/* size of a disk sector */
1463 	int nsects;		/* number of sectors in partition */
1464 	int sectoff;		/* sector offset of partition */
1465 	int totwrt;		/* total number of sectors left to write */
1466 	int nwrt;		/* current number of sectors to write */
1467 	int part;
1468 	int ctlr, slave;
1469 	struct rd_softc *sc;
1470 	struct disklabel *lp;
1471 	char stat;
1472 
1473 	/* Check for recursive dump; if so, punt. */
1474 	if (rddoingadump)
1475 		return EFAULT;
1476 	rddoingadump = 1;
1477 
1478 	/* Decompose unit and partition. */
1479 	part = rdpart(dev);
1480 
1481 	/* Make sure dump device is ok. */
1482 	sc = device_lookup_private(&rd_cd, rdunit(dev));
1483 	if (sc == NULL)
1484 		return ENXIO;
1485 
1486 	if ((sc->sc_flags & RDF_ALIVE) == 0)
1487 		return ENXIO;
1488 
1489 	ctlr = device_unit(device_parent(sc->sc_dev));
1490 	slave = sc->sc_slave;
1491 
1492 	/*
1493 	 * Convert to disk sectors.  Request must be a multiple of size.
1494 	 */
1495 	lp = sc->sc_dkdev.dk_label;
1496 	sectorsize = lp->d_secsize;
1497 	if ((size % sectorsize) != 0)
1498 		return EFAULT;
1499 	totwrt = size / sectorsize;
1500 	blkno = dbtob(blkno) / sectorsize;	/* blkno in DEV_BSIZE units */
1501 
1502 	nsects = lp->d_partitions[part].p_size;
1503 	sectoff = lp->d_partitions[part].p_offset;
1504 
1505 	/* Check transfer bounds against partition size. */
1506 	if ((blkno < 0) || (blkno + totwrt) > nsects)
1507 		return EINVAL;
1508 
1509 	/* Offset block number to start of partition. */
1510 	blkno += sectoff;
1511 
1512 	while (totwrt > 0) {
1513 		nwrt = totwrt;		/* XXX */
1514 #ifndef RD_DUMP_NOT_TRUSTED
1515 		/*
1516 		 * Fill out and send HPIB command.
1517 		 */
1518 		sc->sc_ioc.c_unit = C_SUNIT(sc->sc_punit);
1519 		sc->sc_ioc.c_volume = C_SVOL(0);
1520 		sc->sc_ioc.c_saddr = C_SADDR;
1521 		sc->sc_ioc.c_hiaddr = 0;
1522 		sc->sc_ioc.c_addr = RDBTOS(blkno);
1523 		sc->sc_ioc.c_nop2 = C_NOP;
1524 		sc->sc_ioc.c_slen = C_SLEN;
1525 		sc->sc_ioc.c_len = nwrt * sectorsize;
1526 		sc->sc_ioc.c_cmd = C_WRITE;
1527 		hpibsend(ctlr, slave, C_CMD, &sc->sc_ioc.c_unit,
1528 		    sizeof(sc->sc_ioc) - 2);
1529 		if (hpibswait(ctlr, slave))
1530 			return EIO;
1531 
1532 		/*
1533 		 * Send the data.
1534 		 */
1535 		hpibsend(ctlr, slave, C_EXEC, va, nwrt * sectorsize);
1536 		(void) hpibswait(ctlr, slave);
1537 		hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
1538 		if (stat)
1539 			return EIO;
1540 #else /* RD_DUMP_NOT_TRUSTED */
1541 		/* Let's just talk about this first... */
1542 		printf("%s: dump addr %p, blk %d\n", device_xname(sc->sc_dev),
1543 		    va, blkno);
1544 		delay(500 * 1000);	/* half a second */
1545 #endif /* RD_DUMP_NOT_TRUSTED */
1546 
1547 		/* update block count */
1548 		totwrt -= nwrt;
1549 		blkno += nwrt;
1550 		va = (char *)va + sectorsize * nwrt;
1551 	}
1552 	rddoingadump = 0;
1553 	return 0;
1554 }
1555