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