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