xref: /csrg-svn/sys/hp300/dev/rd.c (revision 57327)
1 /*
2  * Copyright (c) 1988 University of Utah.
3  * Copyright (c) 1982, 1990 The Regents of the University of California.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * the Systems Programming Group of the University of Utah Computer
8  * Science Department.
9  *
10  * %sccs.include.redist.c%
11  *
12  * from: Utah $Hdr: rd.c 1.44 92/12/26$
13  *
14  *	@(#)rd.c	7.18 (Berkeley) 12/27/92
15  */
16 
17 /*
18  * CS80/SS80 disk driver
19  */
20 #include "rd.h"
21 #if NRD > 0
22 
23 #include <sys/param.h>
24 #include <sys/systm.h>
25 #include <sys/buf.h>
26 #include <sys/stat.h>
27 #include <sys/dkstat.h>
28 #include <sys/disklabel.h>
29 #include <sys/ioctl.h>
30 #include <sys/fcntl.h>
31 
32 #include <hp/dev/device.h>
33 #include <hp300/dev/rdreg.h>
34 #include <hp300/dev/rdvar.h>
35 #ifdef USELEDS
36 #include <hp300/hp300/led.h>
37 #endif
38 
39 #include <vm/vm_param.h>
40 #include <vm/lock.h>
41 #include <vm/vm_prot.h>
42 #include <vm/pmap.h>
43 
44 int	rdinit(), rdstart(), rdgo(), rdintr();
45 void	rdstrategy();
46 struct	driver rddriver = {
47 	rdinit, "rd", rdstart, rdgo, rdintr,
48 };
49 
50 struct	rd_softc rd_softc[NRD];
51 struct	buf rdtab[NRD];
52 int	rderrthresh = RDRETRY-1;	/* when to start reporting errors */
53 
54 #ifdef DEBUG
55 /* error message tables */
56 char *err_reject[] = {
57 	0, 0,
58 	"channel parity error",		/* 0x2000 */
59 	0, 0,
60 	"illegal opcode",		/* 0x0400 */
61 	"module addressing",		/* 0x0200 */
62 	"address bounds",		/* 0x0100 */
63 	"parameter bounds",		/* 0x0080 */
64 	"illegal parameter",		/* 0x0040 */
65 	"message sequence",		/* 0x0020 */
66 	0,
67 	"message length",		/* 0x0008 */
68 	0, 0, 0
69 };
70 
71 char *err_fault[] = {
72 	0,
73 	"cross unit",			/* 0x4000 */
74 	0,
75 	"controller fault",		/* 0x1000 */
76 	0, 0,
77 	"unit fault",			/* 0x0200 */
78 	0,
79 	"diagnostic result",		/* 0x0080 */
80 	0,
81 	"operator release request",	/* 0x0020 */
82 	"diagnostic release request",	/* 0x0010 */
83 	"internal maintenance release request",	/* 0x0008 */
84 	0,
85 	"power fail",			/* 0x0002 */
86 	"retransmit"			/* 0x0001 */
87 };
88 
89 char *err_access[] = {
90 	"illegal parallel operation",	/* 0x8000 */
91 	"uninitialized media",		/* 0x4000 */
92 	"no spares available",		/* 0x2000 */
93 	"not ready",			/* 0x1000 */
94 	"write protect",		/* 0x0800 */
95 	"no data found",		/* 0x0400 */
96 	0, 0,
97 	"unrecoverable data overflow",	/* 0x0080 */
98 	"unrecoverable data",		/* 0x0040 */
99 	0,
100 	"end of file",			/* 0x0010 */
101 	"end of volume",		/* 0x0008 */
102 	0, 0, 0
103 };
104 
105 char *err_info[] = {
106 	"operator release request",	/* 0x8000 */
107 	"diagnostic release request",	/* 0x4000 */
108 	"internal maintenance release request",	/* 0x2000 */
109 	"media wear",			/* 0x1000 */
110 	"latency induced",		/* 0x0800 */
111 	0, 0,
112 	"auto sparing invoked",		/* 0x0100 */
113 	0,
114 	"recoverable data overflow",	/* 0x0040 */
115 	"marginal data",		/* 0x0020 */
116 	"recoverable data",		/* 0x0010 */
117 	0,
118 	"maintenance track overflow",	/* 0x0004 */
119 	0, 0
120 };
121 
122 struct	rdstats rdstats[NRD];
123 int	rddebug = 0x80;
124 #define RDB_FOLLOW	0x01
125 #define RDB_STATUS	0x02
126 #define RDB_IDENT	0x04
127 #define RDB_IO		0x08
128 #define RDB_ASYNC	0x10
129 #define RDB_ERROR	0x80
130 #endif
131 
132 /*
133  * Misc. HW description, indexed by sc_type.
134  * Nothing really critical here, could do without it.
135  */
136 struct rdidentinfo rdidentinfo[] = {
137 	{ RD7946AID,	0,	"7945A",	 108416 },
138 	{ RD9134DID,	1,	"9134D",	  29088 },
139 	{ RD9134LID,	1,	"9122S",	   1232 },
140 	{ RD7912PID,	0,	"7912P",	 128128 },
141 	{ RD7914PID,	0,	"7914P",	 258048 },
142 	{ RD7958AID,	0,	"7958A",	 255276 },
143 	{ RD7957AID,	0,	"7957A",	 159544 },
144 	{ RD7933HID,	0,	"7933H",	 789958 },
145 	{ RD9134LID,	1,	"9134L",	  77840 },
146 	{ RD7936HID,	0,	"7936H",	 600978 },
147 	{ RD7937HID,	0,	"7937H",	1116102 },
148 	{ RD7914CTID,	0,	"7914CT",	 258048 },
149 	{ RD7946AID,	0,	"7946A",	 108416 },
150 	{ RD9134LID,	1,	"9122D",	   1232 },
151 	{ RD7957BID,	0,	"7957B",	 159894 },
152 	{ RD7958BID,	0,	"7958B",	 297108 },
153 	{ RD7959BID,	0,	"7959B",	 594216 },
154 	{ RD2200AID,	0,	"2200A",	 654948 },
155 	{ RD2203AID,	0,	"2203A",	1309896 }
156 };
157 int numrdidentinfo = sizeof(rdidentinfo) / sizeof(rdidentinfo[0]);
158 
159 rdinit(hd)
160 	register struct hp_device *hd;
161 {
162 	register struct rd_softc *rs = &rd_softc[hd->hp_unit];
163 
164 	rs->sc_hd = hd;
165 	rs->sc_punit = rdpunit(hd->hp_flags);
166 	rs->sc_type = rdident(rs, hd);
167 	if (rs->sc_type < 0)
168 		return(0);
169 	rs->sc_dq.dq_ctlr = hd->hp_ctlr;
170 	rs->sc_dq.dq_unit = hd->hp_unit;
171 	rs->sc_dq.dq_slave = hd->hp_slave;
172 	rs->sc_dq.dq_driver = &rddriver;
173 	rs->sc_flags = RDF_ALIVE;
174 #ifdef DEBUG
175 	/* always report errors */
176 	if (rddebug & RDB_ERROR)
177 		rderrthresh = 0;
178 #endif
179 	return(1);
180 }
181 
182 rdident(rs, hd)
183 	struct rd_softc *rs;
184 	struct hp_device *hd;
185 {
186 	struct rd_describe desc;
187 	u_char stat, cmd[3];
188 	int unit, lunit;
189 	char name[7];
190 	register int ctlr, slave, id, i;
191 
192 	ctlr = hd->hp_ctlr;
193 	slave = hd->hp_slave;
194 	unit = rs->sc_punit;
195 	lunit = hd->hp_unit;
196 
197 	/*
198 	 * Grab device id and make sure:
199 	 * 1. It is a CS80 device.
200 	 * 2. It is one of the types we support.
201 	 * 3. If it is a 7946, we are accessing the disk unit (0)
202 	 */
203 	id = hpibid(ctlr, slave);
204 #ifdef DEBUG
205 	if (rddebug & RDB_IDENT)
206 		printf("hpibid(%d, %d) -> %x\n", ctlr, slave, id);
207 #endif
208 	if ((id & 0x200) == 0)
209 		return(-1);
210 	for (i = 0; i < numrdidentinfo; i++)
211 		if (id == rdidentinfo[i].ri_hwid)
212 			break;
213 	if (i == numrdidentinfo || unit > rdidentinfo[i].ri_maxunum)
214 		return(-1);
215 	id = i;
216 
217 	/*
218 	 * Reset drive and collect device description.
219 	 * Don't really use the description info right now but
220 	 * might come in handy in the future (for disk labels).
221 	 */
222 	rdreset(rs, hd);
223 	cmd[0] = C_SUNIT(unit);
224 	cmd[1] = C_SVOL(0);
225 	cmd[2] = C_DESC;
226 	hpibsend(ctlr, slave, C_CMD, cmd, sizeof(cmd));
227 	hpibrecv(ctlr, slave, C_EXEC, &desc, 37);
228 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
229 	bzero(name, sizeof(name));
230 	if (!stat) {
231 		register int n = desc.d_name;
232 		for (i = 5; i >= 0; i--) {
233 			name[i] = (n & 0xf) + '0';
234 			n >>= 4;
235 		}
236 		/* use drive characteristics to calculate xfer rate */
237 		rs->sc_wpms = 1000000 * (desc.d_sectsize/2) / desc.d_blocktime;
238 	}
239 #ifdef DEBUG
240 	if (rddebug & RDB_IDENT) {
241 		printf("rd%d: name: %x ('%s')\n",
242 		       lunit, desc.d_name, name);
243 		printf("  iuw %x, maxxfr %d, ctype %d\n",
244 		       desc.d_iuw, desc.d_cmaxxfr, desc.d_ctype);
245 		printf("  utype %d, bps %d, blkbuf %d, burst %d, blktime %d\n",
246 		       desc.d_utype, desc.d_sectsize,
247 		       desc.d_blkbuf, desc.d_burstsize, desc.d_blocktime);
248 		printf("  avxfr %d, ort %d, atp %d, maxint %d, fv %x, rv %x\n",
249 		       desc.d_uavexfr, desc.d_retry, desc.d_access,
250 		       desc.d_maxint, desc.d_fvbyte, desc.d_rvbyte);
251 		printf("  maxcyl/head/sect %d/%d/%d, maxvsect %d, inter %d\n",
252 		       desc.d_maxcyl, desc.d_maxhead, desc.d_maxsect,
253 		       desc.d_maxvsectl, desc.d_interleave);
254 	}
255 #endif
256 	/*
257 	 * Take care of a couple of anomolies:
258 	 * 1. 7945A and 7946A both return same HW id
259 	 * 2. 9122S and 9134D both return same HW id
260 	 * 3. 9122D and 9134L both return same HW id
261 	 */
262 	switch (rdidentinfo[id].ri_hwid) {
263 	case RD7946AID:
264 		if (bcmp(name, "079450", 6) == 0)
265 			id = RD7945A;
266 		else
267 			id = RD7946A;
268 		break;
269 
270 	case RD9134LID:
271 		if (bcmp(name, "091340", 6) == 0)
272 			id = RD9134L;
273 		else
274 			id = RD9122D;
275 		break;
276 
277 	case RD9134DID:
278 		if (bcmp(name, "091220", 6) == 0)
279 			id = RD9122S;
280 		else
281 			id = RD9134D;
282 		break;
283 	}
284 	printf("rd%d: %s\n", lunit, rdidentinfo[id].ri_desc);
285 	return(id);
286 }
287 
288 rdreset(rs, hd)
289 	register struct rd_softc *rs;
290 	register struct hp_device *hd;
291 {
292 	u_char stat;
293 
294 	rs->sc_clear.c_unit = C_SUNIT(rs->sc_punit);
295 	rs->sc_clear.c_cmd = C_CLEAR;
296 	hpibsend(hd->hp_ctlr, hd->hp_slave, C_TCMD, &rs->sc_clear,
297 		sizeof(rs->sc_clear));
298 	hpibswait(hd->hp_ctlr, hd->hp_slave);
299 	hpibrecv(hd->hp_ctlr, hd->hp_slave, C_QSTAT, &stat, sizeof(stat));
300 	rs->sc_src.c_unit = C_SUNIT(RDCTLR);
301 	rs->sc_src.c_nop = C_NOP;
302 	rs->sc_src.c_cmd = C_SREL;
303 	rs->sc_src.c_param = C_REL;
304 	hpibsend(hd->hp_ctlr, hd->hp_slave, C_CMD, &rs->sc_src,
305 		sizeof(rs->sc_src));
306 	hpibswait(hd->hp_ctlr, hd->hp_slave);
307 	hpibrecv(hd->hp_ctlr, hd->hp_slave, C_QSTAT, &stat, sizeof(stat));
308 	rs->sc_ssmc.c_unit = C_SUNIT(rs->sc_punit);
309 	rs->sc_ssmc.c_cmd = C_SSM;
310 	rs->sc_ssmc.c_refm = REF_MASK;
311 	rs->sc_ssmc.c_fefm = FEF_MASK;
312 	rs->sc_ssmc.c_aefm = AEF_MASK;
313 	rs->sc_ssmc.c_iefm = IEF_MASK;
314 	hpibsend(hd->hp_ctlr, hd->hp_slave, C_CMD, &rs->sc_ssmc,
315 		sizeof(rs->sc_ssmc));
316 	hpibswait(hd->hp_ctlr, hd->hp_slave);
317 	hpibrecv(hd->hp_ctlr, hd->hp_slave, C_QSTAT, &stat, sizeof(stat));
318 #ifdef DEBUG
319 	rdstats[hd->hp_unit].rdresets++;
320 #endif
321 }
322 
323 /*
324  * Read or constuct a disklabel
325  */
326 int
327 rdgetinfo(dev)
328 	dev_t dev;
329 {
330 	int unit = rdunit(dev);
331 	register struct rd_softc *rs = &rd_softc[unit];
332 	register struct disklabel *lp = &rs->sc_info.ri_label;
333 	register struct partition *pi;
334 	char *msg, *readdisklabel();
335 
336 	/*
337 	 * Set some default values to use while reading the label
338 	 * or to use if there isn't a label.
339 	 */
340 	bzero((caddr_t)lp, sizeof *lp);
341 	lp->d_type = DTYPE_HPIB;
342 	lp->d_secsize = DEV_BSIZE;
343 	lp->d_nsectors = 32;
344 	lp->d_ntracks = 20;
345 	lp->d_secpercyl = 32*20;
346 	lp->d_npartitions = 3;
347 	lp->d_partitions[2].p_offset = 0;
348 	lp->d_partitions[2].p_size = LABELSECTOR+1;
349 
350 	/*
351 	 * Now try to read the disklabel
352 	 */
353 	msg = readdisklabel(rdlabdev(dev), rdstrategy, lp);
354 	if (msg == NULL)
355 		return(0);
356 
357 	pi = lp->d_partitions;
358 	printf("rd%d: WARNING: %s, ", unit, msg);
359 #ifdef COMPAT_NOLABEL
360 	printf("using old default partitioning\n");
361 	rdmakedisklabel(unit, lp);
362 #else
363 	printf("defining `c' partition as entire disk\n");
364 	pi[2].p_size = rdidentinfo[rs->sc_type].ri_nblocks;
365 #endif
366 	return(0);
367 }
368 
369 int
370 rdopen(dev, flags, mode, p)
371 	dev_t dev;
372 	int flags, mode;
373 	struct proc *p;
374 {
375 	register int unit = rdunit(dev);
376 	register struct rd_softc *rs = &rd_softc[unit];
377 	int error, mask;
378 
379 	if (unit >= NRD || (rs->sc_flags & RDF_ALIVE) == 0)
380 		return(ENXIO);
381 
382 	/*
383 	 * Wait for any pending opens/closes to complete
384 	 */
385 	while (rs->sc_flags & (RDF_OPENING|RDF_CLOSING))
386 		sleep((caddr_t)rs, PRIBIO);
387 
388 	/*
389 	 * On first open, get label and partition info.
390 	 * We may block reading the label, so be careful
391 	 * to stop any other opens.
392 	 */
393 	if (rs->sc_info.ri_open == 0) {
394 		rs->sc_flags |= RDF_OPENING;
395 		error = rdgetinfo(dev);
396 		rs->sc_flags &= ~RDF_OPENING;
397 		wakeup((caddr_t)rs);
398 		if (error)
399 			return(error);
400 	}
401 	if (rs->sc_hd->hp_dk >= 0) {
402 		/* guess at xfer rate based on 3600 rpm (60 rps) */
403 		if (rs->sc_wpms == 0)
404 			rs->sc_wpms = 60 * rs->sc_info.ri_label.d_nsectors
405 				* DEV_BSIZE / 2;
406 		dk_wpms[rs->sc_hd->hp_dk] = rs->sc_wpms;
407 	}
408 
409 	mask = 1 << rdpart(dev);
410 	if (mode == S_IFCHR)
411 		rs->sc_info.ri_copen |= mask;
412 	else
413 		rs->sc_info.ri_bopen |= mask;
414 	rs->sc_info.ri_open |= mask;
415 	return(0);
416 }
417 
418 int
419 rdclose(dev, flag, mode, p)
420 	dev_t dev;
421 	int flag, mode;
422 	struct proc *p;
423 {
424 	int unit = rdunit(dev);
425 	register struct rd_softc *rs = &rd_softc[unit];
426 	register struct rdinfo *ri = &rs->sc_info;
427 	int mask, s;
428 
429 	mask = 1 << rdpart(dev);
430 	if (mode == S_IFCHR)
431 		ri->ri_copen &= ~mask;
432 	else
433 		ri->ri_bopen &= ~mask;
434 	ri->ri_open = ri->ri_bopen | ri->ri_copen;
435 	/*
436 	 * On last close, we wait for all activity to cease since
437 	 * the label/parition info will become invalid.  Since we
438 	 * might sleep, we must block any opens while we are here.
439 	 * Note we don't have to about other closes since we know
440 	 * we are the last one.
441 	 */
442 	if (ri->ri_open == 0) {
443 		rs->sc_flags |= RDF_CLOSING;
444 		s = splbio();
445 		while (rdtab[unit].b_active) {
446 			rs->sc_flags |= RDF_WANTED;
447 			sleep((caddr_t)&rdtab[unit], PRIBIO);
448 		}
449 		splx(s);
450 		rs->sc_flags &= ~(RDF_CLOSING|RDF_WLABEL);
451 		wakeup((caddr_t)rs);
452 	}
453 	return(0);
454 }
455 
456 void
457 rdstrategy(bp)
458 	register struct buf *bp;
459 {
460 	int unit = rdunit(bp->b_dev);
461 	register struct rd_softc *rs = &rd_softc[unit];
462 	register struct buf *dp = &rdtab[unit];
463 	register struct partition *pinfo;
464 	register daddr_t bn;
465 	register int sz, s;
466 
467 #ifdef DEBUG
468 	if (rddebug & RDB_FOLLOW)
469 		printf("rdstrategy(%x): dev %x, bn %x, bcount %x, %c\n",
470 		       bp, bp->b_dev, bp->b_blkno, bp->b_bcount,
471 		       (bp->b_flags & B_READ) ? 'R' : 'W');
472 #endif
473 	bn = bp->b_blkno;
474 	sz = howmany(bp->b_bcount, DEV_BSIZE);
475 	pinfo = &rs->sc_info.ri_label.d_partitions[rdpart(bp->b_dev)];
476 	if (bn < 0 || bn + sz > pinfo->p_size) {
477 		sz = pinfo->p_size - bn;
478 		if (sz == 0) {
479 			bp->b_resid = bp->b_bcount;
480 			goto done;
481 		}
482 		if (sz < 0) {
483 			bp->b_error = EINVAL;
484 			goto bad;
485 		}
486 		bp->b_bcount = dbtob(sz);
487 	}
488 	/*
489 	 * Check for write to write protected label
490 	 */
491 	if (bn + pinfo->p_offset <= LABELSECTOR &&
492 #if LABELSECTOR != 0
493 	    bn + pinfo->p_offset + sz > LABELSECTOR &&
494 #endif
495 	    !(bp->b_flags & B_READ) && !(rs->sc_flags & RDF_WLABEL)) {
496 		bp->b_error = EROFS;
497 		goto bad;
498 	}
499 	bp->b_cylin = bn + pinfo->p_offset;
500 	s = splbio();
501 	disksort(dp, bp);
502 	if (dp->b_active == 0) {
503 		dp->b_active = 1;
504 		rdustart(unit);
505 	}
506 	splx(s);
507 	return;
508 bad:
509 	bp->b_flags |= B_ERROR;
510 done:
511 	biodone(bp);
512 }
513 
514 /*
515  * Called from timeout() when handling maintenance releases
516  */
517 void
518 rdrestart(arg)
519 	void *arg;
520 {
521 	int s = splbio();
522 	rdustart((int)arg);
523 	splx(s);
524 }
525 
526 rdustart(unit)
527 	register int unit;
528 {
529 	register struct buf *bp;
530 	register struct rd_softc *rs = &rd_softc[unit];
531 
532 	bp = rdtab[unit].b_actf;
533 	rs->sc_addr = bp->b_un.b_addr;
534 	rs->sc_resid = bp->b_bcount;
535 	if (hpibreq(&rs->sc_dq))
536 		rdstart(unit);
537 }
538 
539 struct buf *
540 rdfinish(unit, rs, bp)
541 	int unit;
542 	register struct rd_softc *rs;
543 	register struct buf *bp;
544 {
545 	register struct buf *dp = &rdtab[unit];
546 
547 	dp->b_errcnt = 0;
548 	dp->b_actf = bp->b_actf;
549 	bp->b_resid = 0;
550 	biodone(bp);
551 	hpibfree(&rs->sc_dq);
552 	if (dp->b_actf)
553 		return(dp->b_actf);
554 	dp->b_active = 0;
555 	if (rs->sc_flags & RDF_WANTED) {
556 		rs->sc_flags &= ~RDF_WANTED;
557 		wakeup((caddr_t)dp);
558 	}
559 	return(NULL);
560 }
561 
562 rdstart(unit)
563 	register int unit;
564 {
565 	register struct rd_softc *rs = &rd_softc[unit];
566 	register struct buf *bp = rdtab[unit].b_actf;
567 	register struct hp_device *hp = rs->sc_hd;
568 	register int part;
569 
570 again:
571 #ifdef DEBUG
572 	if (rddebug & RDB_FOLLOW)
573 		printf("rdstart(%d): bp %x, %c\n", unit, bp,
574 		       (bp->b_flags & B_READ) ? 'R' : 'W');
575 #endif
576 	part = rdpart(bp->b_dev);
577 	rs->sc_flags |= RDF_SEEK;
578 	rs->sc_ioc.c_unit = C_SUNIT(rs->sc_punit);
579 	rs->sc_ioc.c_volume = C_SVOL(0);
580 	rs->sc_ioc.c_saddr = C_SADDR;
581 	rs->sc_ioc.c_hiaddr = 0;
582 	rs->sc_ioc.c_addr = RDBTOS(bp->b_cylin);
583 	rs->sc_ioc.c_nop2 = C_NOP;
584 	rs->sc_ioc.c_slen = C_SLEN;
585 	rs->sc_ioc.c_len = rs->sc_resid;
586 	rs->sc_ioc.c_cmd = bp->b_flags & B_READ ? C_READ : C_WRITE;
587 #ifdef DEBUG
588 	if (rddebug & RDB_IO)
589 		printf("rdstart: hpibsend(%x, %x, %x, %x, %x)\n",
590 		       hp->hp_ctlr, hp->hp_slave, C_CMD,
591 		       &rs->sc_ioc.c_unit, sizeof(rs->sc_ioc)-2);
592 #endif
593 	if (hpibsend(hp->hp_ctlr, hp->hp_slave, C_CMD, &rs->sc_ioc.c_unit,
594 		     sizeof(rs->sc_ioc)-2) == sizeof(rs->sc_ioc)-2) {
595 		if (hp->hp_dk >= 0) {
596 			dk_busy |= 1 << hp->hp_dk;
597 			dk_seek[hp->hp_dk]++;
598 		}
599 #ifdef DEBUG
600 		if (rddebug & RDB_IO)
601 			printf("rdstart: hpibawait(%x)\n", hp->hp_ctlr);
602 #endif
603 		hpibawait(hp->hp_ctlr);
604 		return;
605 	}
606 	/*
607 	 * Experience has shown that the hpibwait in this hpibsend will
608 	 * occasionally timeout.  It appears to occur mostly on old 7914
609 	 * drives with full maintenance tracks.  We should probably
610 	 * integrate this with the backoff code in rderror.
611 	 */
612 #ifdef DEBUG
613 	if (rddebug & RDB_ERROR)
614 		printf("rd%d: rdstart: cmd %x adr %d blk %d len %d ecnt %d\n",
615 		       unit, rs->sc_ioc.c_cmd, rs->sc_ioc.c_addr,
616 		       bp->b_blkno, rs->sc_resid, rdtab[unit].b_errcnt);
617 	rdstats[unit].rdretries++;
618 #endif
619 	rs->sc_flags &= ~RDF_SEEK;
620 	rdreset(rs, hp);
621 	if (rdtab[unit].b_errcnt++ < RDRETRY)
622 		goto again;
623 	printf("rd%d: rdstart err: cmd 0x%x sect %d blk %d len %d\n",
624 	       unit, rs->sc_ioc.c_cmd, rs->sc_ioc.c_addr,
625 	       bp->b_blkno, rs->sc_resid);
626 	bp->b_flags |= B_ERROR;
627 	bp->b_error = EIO;
628 	bp = rdfinish(unit, rs, bp);
629 	if (bp) {
630 		rs->sc_addr = bp->b_un.b_addr;
631 		rs->sc_resid = bp->b_bcount;
632 		if (hpibreq(&rs->sc_dq))
633 			goto again;
634 	}
635 }
636 
637 rdgo(unit)
638 	register int unit;
639 {
640 	register struct rd_softc *rs = &rd_softc[unit];
641 	register struct hp_device *hp = rs->sc_hd;
642 	struct buf *bp = rdtab[unit].b_actf;
643 
644 	if (hp->hp_dk >= 0) {
645 		dk_busy |= 1 << hp->hp_dk;
646 		dk_xfer[hp->hp_dk]++;
647 		dk_wds[hp->hp_dk] += rs->sc_resid >> 6;
648 	}
649 #ifdef USELEDS
650 	if (inledcontrol == 0)
651 		ledcontrol(0, 0, LED_DISK);
652 #endif
653 	hpibgo(hp->hp_ctlr, hp->hp_slave, C_EXEC,
654 	       rs->sc_addr, rs->sc_resid, bp->b_flags & B_READ);
655 }
656 
657 rdintr(unit)
658 	register int unit;
659 {
660 	register struct rd_softc *rs = &rd_softc[unit];
661 	register struct buf *bp = rdtab[unit].b_actf;
662 	register struct hp_device *hp = rs->sc_hd;
663 	u_char stat = 13;	/* in case hpibrecv fails */
664 	int rv, restart;
665 
666 #ifdef DEBUG
667 	if (rddebug & RDB_FOLLOW)
668 		printf("rdintr(%d): bp %x, %c, flags %x\n", unit, bp,
669 		       (bp->b_flags & B_READ) ? 'R' : 'W', rs->sc_flags);
670 	if (bp == NULL) {
671 		printf("rd%d: bp == NULL\n", unit);
672 		return;
673 	}
674 #endif
675 	if (hp->hp_dk >= 0)
676 		dk_busy &= ~(1 << hp->hp_dk);
677 	if (rs->sc_flags & RDF_SEEK) {
678 		rs->sc_flags &= ~RDF_SEEK;
679 		if (hpibustart(hp->hp_ctlr))
680 			rdgo(unit);
681 		return;
682 	}
683 	if ((rs->sc_flags & RDF_SWAIT) == 0) {
684 #ifdef DEBUG
685 		rdstats[unit].rdpolltries++;
686 #endif
687 		if (hpibpptest(hp->hp_ctlr, hp->hp_slave) == 0) {
688 #ifdef DEBUG
689 			rdstats[unit].rdpollwaits++;
690 #endif
691 			if (hp->hp_dk >= 0)
692 				dk_busy |= 1 << hp->hp_dk;
693 			rs->sc_flags |= RDF_SWAIT;
694 			hpibawait(hp->hp_ctlr);
695 			return;
696 		}
697 	} else
698 		rs->sc_flags &= ~RDF_SWAIT;
699 	rv = hpibrecv(hp->hp_ctlr, hp->hp_slave, C_QSTAT, &stat, 1);
700 	if (rv != 1 || stat) {
701 #ifdef DEBUG
702 		if (rddebug & RDB_ERROR)
703 			printf("rdintr: recv failed or bad stat %d\n", stat);
704 #endif
705 		restart = rderror(unit);
706 #ifdef DEBUG
707 		rdstats[unit].rdretries++;
708 #endif
709 		if (rdtab[unit].b_errcnt++ < RDRETRY) {
710 			if (restart)
711 				rdstart(unit);
712 			return;
713 		}
714 		bp->b_flags |= B_ERROR;
715 		bp->b_error = EIO;
716 	}
717 	if (rdfinish(unit, rs, bp))
718 		rdustart(unit);
719 }
720 
721 rdstatus(rs)
722 	register struct rd_softc *rs;
723 {
724 	register int c, s;
725 	u_char stat;
726 	int rv;
727 
728 	c = rs->sc_hd->hp_ctlr;
729 	s = rs->sc_hd->hp_slave;
730 	rs->sc_rsc.c_unit = C_SUNIT(rs->sc_punit);
731 	rs->sc_rsc.c_sram = C_SRAM;
732 	rs->sc_rsc.c_ram = C_RAM;
733 	rs->sc_rsc.c_cmd = C_STATUS;
734 	bzero((caddr_t)&rs->sc_stat, sizeof(rs->sc_stat));
735 	rv = hpibsend(c, s, C_CMD, &rs->sc_rsc, sizeof(rs->sc_rsc));
736 	if (rv != sizeof(rs->sc_rsc)) {
737 #ifdef DEBUG
738 		if (rddebug & RDB_STATUS)
739 			printf("rdstatus: send C_CMD failed %d != %d\n",
740 			       rv, sizeof(rs->sc_rsc));
741 #endif
742 		return(1);
743 	}
744 	rv = hpibrecv(c, s, C_EXEC, &rs->sc_stat, sizeof(rs->sc_stat));
745 	if (rv != sizeof(rs->sc_stat)) {
746 #ifdef DEBUG
747 		if (rddebug & RDB_STATUS)
748 			printf("rdstatus: send C_EXEC failed %d != %d\n",
749 			       rv, sizeof(rs->sc_stat));
750 #endif
751 		return(1);
752 	}
753 	rv = hpibrecv(c, s, C_QSTAT, &stat, 1);
754 	if (rv != 1 || stat) {
755 #ifdef DEBUG
756 		if (rddebug & RDB_STATUS)
757 			printf("rdstatus: recv failed %d or bad stat %d\n",
758 			       rv, stat);
759 #endif
760 		return(1);
761 	}
762 	return(0);
763 }
764 
765 /*
766  * Deal with errors.
767  * Returns 1 if request should be restarted,
768  * 0 if we should just quietly give up.
769  */
770 rderror(unit)
771 	int unit;
772 {
773 	struct rd_softc *rs = &rd_softc[unit];
774 	register struct rd_stat *sp;
775 	struct buf *bp;
776 	daddr_t hwbn, pbn;
777 
778 	if (rdstatus(rs)) {
779 #ifdef DEBUG
780 		printf("rd%d: couldn't get status\n", unit);
781 #endif
782 		rdreset(rs, rs->sc_hd);
783 		return(1);
784 	}
785 	sp = &rs->sc_stat;
786 	if (sp->c_fef & FEF_REXMT)
787 		return(1);
788 	if (sp->c_fef & FEF_PF) {
789 		rdreset(rs, rs->sc_hd);
790 		return(1);
791 	}
792 	/*
793 	 * Unit requests release for internal maintenance.
794 	 * We just delay awhile and try again later.  Use expontially
795 	 * increasing backoff ala ethernet drivers since we don't really
796 	 * know how long the maintenance will take.  With RDWAITC and
797 	 * RDRETRY as defined, the range is 1 to 32 seconds.
798 	 */
799 	if (sp->c_fef & FEF_IMR) {
800 		extern int hz;
801 		int rdtimo = RDWAITC << rdtab[unit].b_errcnt;
802 #ifdef DEBUG
803 		printf("rd%d: internal maintenance, %d second timeout\n",
804 		       unit, rdtimo);
805 		rdstats[unit].rdtimeouts++;
806 #endif
807 		hpibfree(&rs->sc_dq);
808 		timeout(rdrestart, (void *)unit, rdtimo * hz);
809 		return(0);
810 	}
811 	/*
812 	 * Only report error if we have reached the error reporting
813 	 * threshhold.  By default, this will only report after the
814 	 * retry limit has been exceeded.
815 	 */
816 	if (rdtab[unit].b_errcnt < rderrthresh)
817 		return(1);
818 
819 	/*
820 	 * First conjure up the block number at which the error occured.
821 	 * Note that not all errors report a block number, in that case
822 	 * we just use b_blkno.
823  	 */
824 	bp = rdtab[unit].b_actf;
825 	pbn = rs->sc_info.ri_label.d_partitions[rdpart(bp->b_dev)].p_offset;
826 	if ((sp->c_fef & FEF_CU) || (sp->c_fef & FEF_DR) ||
827 	    (sp->c_ief & IEF_RRMASK)) {
828 		hwbn = RDBTOS(pbn + bp->b_blkno);
829 		pbn = bp->b_blkno;
830 	} else {
831 		hwbn = sp->c_blk;
832 		pbn = RDSTOB(hwbn) - pbn;
833 	}
834 	/*
835 	 * Now output a generic message suitable for badsect.
836 	 * Note that we don't use harderr cuz it just prints
837 	 * out b_blkno which is just the beginning block number
838 	 * of the transfer, not necessary where the error occured.
839 	 */
840 	printf("rd%d%c: hard error sn%d\n",
841 	       rdunit(bp->b_dev), 'a'+rdpart(bp->b_dev), pbn);
842 	/*
843 	 * Now report the status as returned by the hardware with
844 	 * attempt at interpretation (unless debugging).
845 	 */
846 	printf("rd%d %s error:",
847 	       unit, (bp->b_flags & B_READ) ? "read" : "write");
848 #ifdef DEBUG
849 	if (rddebug & RDB_ERROR) {
850 		/* status info */
851 		printf("\n    volume: %d, unit: %d\n",
852 		       (sp->c_vu>>4)&0xF, sp->c_vu&0xF);
853 		rdprinterr("reject", sp->c_ref, err_reject);
854 		rdprinterr("fault", sp->c_fef, err_fault);
855 		rdprinterr("access", sp->c_aef, err_access);
856 		rdprinterr("info", sp->c_ief, err_info);
857 		printf("    block: %d, P1-P10: ", hwbn);
858 		printf("%s", hexstr(*(u_int *)&sp->c_raw[0], 8));
859 		printf("%s", hexstr(*(u_int *)&sp->c_raw[4], 8));
860 		printf("%s\n", hexstr(*(u_short *)&sp->c_raw[8], 4));
861 		/* command */
862 		printf("    ioc: ");
863 		printf("%s", hexstr(*(u_int *)&rs->sc_ioc.c_pad, 8));
864 		printf("%s", hexstr(*(u_short *)&rs->sc_ioc.c_hiaddr, 4));
865 		printf("%s", hexstr(*(u_int *)&rs->sc_ioc.c_addr, 8));
866 		printf("%s", hexstr(*(u_short *)&rs->sc_ioc.c_nop2, 4));
867 		printf("%s", hexstr(*(u_int *)&rs->sc_ioc.c_len, 8));
868 		printf("%s\n", hexstr(*(u_short *)&rs->sc_ioc.c_cmd, 4));
869 		return(1);
870 	}
871 #endif
872 	printf(" v%d u%d, R0x%x F0x%x A0x%x I0x%x\n",
873 	       (sp->c_vu>>4)&0xF, sp->c_vu&0xF,
874 	       sp->c_ref, sp->c_fef, sp->c_aef, sp->c_ief);
875 	printf("P1-P10: ");
876 	printf("%s", hexstr(*(u_int *)&sp->c_raw[0], 8));
877 	printf("%s", hexstr(*(u_int *)&sp->c_raw[4], 8));
878 	printf("%s\n", hexstr(*(u_short *)&sp->c_raw[8], 4));
879 	return(1);
880 }
881 
882 int
883 rdread(dev, uio, flags)
884 	dev_t dev;
885 	struct uio *uio;
886 	int flags;
887 {
888 
889 	return (physio(rdstrategy, NULL, dev, B_READ, minphys, uio));
890 }
891 
892 int
893 rdwrite(dev, uio, flags)
894 	dev_t dev;
895 	struct uio *uio;
896 	int flags;
897 {
898 
899 	return (physio(rdstrategy, NULL, dev, B_WRITE, minphys, uio));
900 }
901 
902 int
903 rdioctl(dev, cmd, data, flag, p)
904 	dev_t dev;
905 	int cmd;
906 	caddr_t data;
907 	int flag;
908 	struct proc *p;
909 {
910 	int unit = rdunit(dev);
911 	register struct rd_softc *sc = &rd_softc[unit];
912 	register struct disklabel *lp = &sc->sc_info.ri_label;
913 	int error, flags;
914 
915 	switch (cmd) {
916 	case DIOCGDINFO:
917 		*(struct disklabel *)data = *lp;
918 		return (0);
919 
920 	case DIOCGPART:
921 		((struct partinfo *)data)->disklab = lp;
922 		((struct partinfo *)data)->part =
923 			&lp->d_partitions[rdpart(dev)];
924 		return (0);
925 
926         case DIOCWLABEL:
927                 if ((flag & FWRITE) == 0)
928                         return (EBADF);
929 		if (*(int *)data)
930 			sc->sc_flags |= RDF_WLABEL;
931 		else
932 			sc->sc_flags &= ~RDF_WLABEL;
933 		return (0);
934 
935         case DIOCSDINFO:
936                 if ((flag & FWRITE) == 0)
937                         return (EBADF);
938 		return (setdisklabel(lp, (struct disklabel *)data,
939 				     (sc->sc_flags & RDF_WLABEL) ? 0
940 				     : sc->sc_info.ri_open));
941 
942         case DIOCWDINFO:
943 		if ((flag & FWRITE) == 0)
944 			return (EBADF);
945 		error = setdisklabel(lp, (struct disklabel *)data,
946 				     (sc->sc_flags & RDF_WLABEL) ? 0
947 				     : sc->sc_info.ri_open);
948 		if (error)
949 			return (error);
950 		flags = sc->sc_flags;
951 		sc->sc_flags = RDF_ALIVE | RDF_WLABEL;
952 		error = writedisklabel(rdlabdev(dev), rdstrategy, lp);
953 		sc->sc_flags = flags;
954 		return (error);
955 	}
956 	return(EINVAL);
957 }
958 
959 int
960 rdsize(dev)
961 	dev_t dev;
962 {
963 	register int unit = rdunit(dev);
964 	register struct rd_softc *rs = &rd_softc[unit];
965 	int psize, didopen = 0;
966 
967 	if (unit >= NRD || (rs->sc_flags & RDF_ALIVE) == 0)
968 		return(-1);
969 
970 	/*
971 	 * We get called very early on (via swapconf)
972 	 * without the device being open so we may need
973 	 * to handle it here.
974 	 */
975 	if (rs->sc_info.ri_open == 0) {
976 		if (rdopen(dev, FREAD|FWRITE, S_IFBLK, NULL))
977 			return(-1);
978 		didopen = 1;
979 	}
980 	psize = rs->sc_info.ri_label.d_partitions[rdpart(dev)].p_size;
981 	if (didopen)
982 		(void) rdclose(dev, FREAD|FWRITE, S_IFBLK, NULL);
983 	return (psize);
984 }
985 
986 #ifdef DEBUG
987 rdprinterr(str, err, tab)
988 	char *str;
989 	short err;
990 	char *tab[];
991 {
992 	register int i;
993 	int printed;
994 
995 	if (err == 0)
996 		return;
997 	printf("    %s error field:", str, err);
998 	printed = 0;
999 	for (i = 0; i < 16; i++)
1000 		if (err & (0x8000 >> i))
1001 			printf("%s%s", printed++ ? " + " : " ", tab[i]);
1002 	printf("\n");
1003 }
1004 #endif
1005 
1006 /*
1007  * Non-interrupt driven, non-dma dump routine.
1008  */
1009 int
1010 rddump(dev)
1011 	dev_t dev;
1012 {
1013 	int part = rdpart(dev);
1014 	int unit = rdunit(dev);
1015 	register struct rd_softc *rs = &rd_softc[unit];
1016 	register struct hp_device *hp = rs->sc_hd;
1017 	register struct partition *pinfo;
1018 	register daddr_t baddr;
1019 	register int maddr, pages, i;
1020 	char stat;
1021 	extern int lowram, dumpsize;
1022 #ifdef DEBUG
1023 	extern int pmapdebug;
1024 	pmapdebug = 0;
1025 #endif
1026 
1027 	/* is drive ok? */
1028 	if (unit >= NRD || (rs->sc_flags & RDF_ALIVE) == 0)
1029 		return (ENXIO);
1030 	pinfo = &rs->sc_info.ri_label.d_partitions[part];
1031 	/* dump parameters in range? */
1032 	if (dumplo < 0 || dumplo >= pinfo->p_size ||
1033 	    pinfo->p_fstype != FS_SWAP)
1034 		return (EINVAL);
1035 	pages = dumpsize;
1036 	if (dumplo + ctod(pages) > pinfo->p_size)
1037 		pages = dtoc(pinfo->p_size - dumplo);
1038 	maddr = lowram;
1039 	baddr = dumplo + pinfo->p_offset;
1040 	/* HPIB idle? */
1041 	if (!hpibreq(&rs->sc_dq)) {
1042 		hpibreset(hp->hp_ctlr);
1043 		rdreset(rs, rs->sc_hd);
1044 		printf("[ drive %d reset ] ", unit);
1045 	}
1046 	for (i = 0; i < pages; i++) {
1047 #define NPGMB	(1024*1024/NBPG)
1048 		/* print out how many Mbs we have dumped */
1049 		if (i && (i % NPGMB) == 0)
1050 			printf("%d ", i / NPGMB);
1051 #undef NPBMG
1052 		rs->sc_ioc.c_unit = C_SUNIT(rs->sc_punit);
1053 		rs->sc_ioc.c_volume = C_SVOL(0);
1054 		rs->sc_ioc.c_saddr = C_SADDR;
1055 		rs->sc_ioc.c_hiaddr = 0;
1056 		rs->sc_ioc.c_addr = RDBTOS(baddr);
1057 		rs->sc_ioc.c_nop2 = C_NOP;
1058 		rs->sc_ioc.c_slen = C_SLEN;
1059 		rs->sc_ioc.c_len = NBPG;
1060 		rs->sc_ioc.c_cmd = C_WRITE;
1061 		hpibsend(hp->hp_ctlr, hp->hp_slave, C_CMD,
1062 			 &rs->sc_ioc.c_unit, sizeof(rs->sc_ioc)-2);
1063 		if (hpibswait(hp->hp_ctlr, hp->hp_slave))
1064 			return (EIO);
1065 		pmap_enter(kernel_pmap, (vm_offset_t)vmmap, maddr,
1066 		    VM_PROT_READ, TRUE);
1067 		hpibsend(hp->hp_ctlr, hp->hp_slave, C_EXEC, vmmap, NBPG);
1068 		(void) hpibswait(hp->hp_ctlr, hp->hp_slave);
1069 		hpibrecv(hp->hp_ctlr, hp->hp_slave, C_QSTAT, &stat, 1);
1070 		if (stat)
1071 			return (EIO);
1072 		maddr += NBPG;
1073 		baddr += ctod(1);
1074 	}
1075 	return (0);
1076 }
1077 #endif
1078