xref: /csrg-svn/sys/vax/uba/up.c (revision 267)
1*267Sbill int	csdel3 = 100;
2264Sbill int	printsw;
3*267Sbill /*	10/14/12	3.3	06/18/80	*/
4264Sbill 
5264Sbill /*
6264Sbill  * Emulex UNIBUS disk driver with overlapped seeks and ECC recovery.
7264Sbill  *
8266Sbill  * NB: This device is very sensitive: be aware that the code is the way
9266Sbill  *     it is for good reason and that there are delay loops here which may
10266Sbill  *     have to be lengthened if your processor is faster and which should
11266Sbill  *     probably be shortened if your processor is slower.
12266Sbill  *
13264Sbill  * This driver has been tested on a SC-11B Controller, configured
14264Sbill  * with the following internal switch settings:
15264Sbill  *	SW1-1	5/19 surfaces	(off, 19 surfaces on Ampex 9300)
16264Sbill  *	SW1-2	chksum enable	(off, checksum disabled)
17264Sbill  *	SW1-3	volume select	(off, 815 cylinders)
18264Sbill  *	SW1-4	sector select	(on, 32 sectors)
19264Sbill  *	SW1-5	unused		(off)
20264Sbill  *	SW1-6	port select	(on, single port)
21264Sbill  *	SW1-7	npr delay	(off, disable)
22264Sbill  *	SW1-8	ecc test mode	(off, disable)
23264Sbill  * and top mounted switches:
24264Sbill  *	SW2-1	extend opcodes	(off=open, disable)
25264Sbill  *	SW2-2	extend diag	(off=open, disable)
26264Sbill  *	SW2-3	4 wd dma burst	(off=open, disable)
27264Sbill  *	SW2-4	unused		(off=open)
28264Sbill  *
29264Sbill  * The controller transfers data much more rapidly with SW2-3 set,
30264Sbill  * but we have previously experienced problems with it set this way.
31264Sbill  * We intend to try this again in the near future.
32264Sbill  *
33264Sbill  *	wnj	June 14, 1980
34264Sbill  */
35264Sbill 
36264Sbill #include "../h/param.h"
37264Sbill #include "../h/systm.h"
38264Sbill #include "../h/buf.h"
39264Sbill #include "../h/conf.h"
40264Sbill #include "../h/dir.h"
41264Sbill #include "../h/user.h"
42264Sbill #include "../h/map.h"
43264Sbill #include "../h/mba.h"
44264Sbill #include "../h/mtpr.h"
45264Sbill #include "../h/pte.h"
46264Sbill #include "../h/uba.h"
47264Sbill #include "../h/vm.h"
48264Sbill 
49264Sbill /*
50264Sbill  * Define number of drives, and range of sampling information to be used.
51264Sbill  *
52264Sbill  * Normally, DK_N .. DK_N+NUP-1 gather individual drive stats,
53264Sbill  * and DK_N+NUP gathers controller transferring stats.
54264Sbill  *
55264Sbill  * If DK_N+NUP > DK_NMAX, then transfer stats are divided per drive.
56264Sbill  * If DK_NMAX is yet smaller, some drives are not monitored.
57264Sbill  */
58264Sbill #define	DK_N	1
59264Sbill #define	DK_NMAX	2
60264Sbill 
61264Sbill #define	ushort	unsigned short
62264Sbill 
63264Sbill struct	device
64264Sbill {
65264Sbill 	ushort	upcs1;		/* control and status register 1 */
66264Sbill 	short	upwc;		/* word count register */
67264Sbill 	ushort	upba;		/* UNIBUS address register */
68264Sbill 	ushort	upda;		/* desired address register */
69264Sbill 	ushort	upcs2;		/* control and status register 2 */
70264Sbill 	ushort	upds;		/* drive Status */
71264Sbill 	ushort	uper1;		/* error register 1 */
72264Sbill 	ushort	upas;		/* attention summary */
73264Sbill 	ushort	upla;		/* look ahead */
74264Sbill 	ushort	updb;		/* data buffer */
75264Sbill 	ushort	upmr;		/* maintenance */
76264Sbill 	ushort	updt;		/* drive type */
77264Sbill 	ushort	upsn;		/* serial number */
78264Sbill 	ushort	upof;		/* offset register */
79264Sbill 	ushort	updc;		/* desired cylinder address register */
80264Sbill 	ushort	upcc;		/* current cylinder */
81264Sbill 	ushort	uper2;		/* error register 2 */
82264Sbill 	ushort	uper3;		/* error register 3 */
83264Sbill 	ushort	upec1;		/* burst error bit position */
84264Sbill 	ushort	upec2;		/* burst error bit pattern */
85264Sbill };
86264Sbill 
87264Sbill #define	UPADDR	((struct device *)(UBA0_DEV + 0176700))
88264Sbill 
89264Sbill #define	NUP	2		/* Number of drives this installation */
90264Sbill 
91264Sbill #define	NSECT	32
92264Sbill #define	NTRAC	19
93264Sbill 
94264Sbill /*
95264Sbill  * Constants controlling on-cylinder SEARCH usage.
96264Sbill  *
97264Sbill  * We assume that it takes SDIST sectors of time to set up a transfer.
98264Sbill  * If a drive is on-cylinder, and between SDIST and SDIST+RDIST sectors
99264Sbill  * from the first sector to be transferred, then we just perform the
100264Sbill  * transfer.  SDIST represents interrupt latency, RDIST the amount
101264Sbill  * of rotation which is tolerable to avoid another interrupt.
102264Sbill  */
103266Sbill #define	SDIST	3		/* 2-3 sectors 1-1.5 msec */
104266Sbill #define	RDIST	6		/* 5-6 sectors 2.5-3 msec */
105264Sbill 
106264Sbill /*
107264Sbill  * To fill a 300M drive:
108264Sbill  *	A is designed to be used as a root.
109264Sbill  *	B is suitable for a swap area.
110264Sbill  *	H is the primary storage area.
111264Sbill  * On systems with RP06'es, we normally use only 291346 blocks of the H
112264Sbill  * area, and use DEF or G to cover the rest of the drive.  The C system
113264Sbill  * covers the whole drive and can be used for pack-pack copying.
114264Sbill  */
115264Sbill struct	size
116264Sbill {
117264Sbill 	daddr_t	nblocks;
118264Sbill 	int	cyloff;
119264Sbill } up_sizes[8] = {
120264Sbill 	15884,	0,		/* A=cyl 0 thru 26 */
121264Sbill 	33440,	27,		/* B=cyl 27 thru 81 */
122264Sbill 	494912,	0,		/* C=cyl 0 thru 814 */
123264Sbill 	15884,	562,		/* D=cyl 562 thru 588 */
124264Sbill 	55936,	589,		/* E=cyl 589 thru 680 */
125264Sbill 	81472,	681,		/* F=cyl 681 thru 814 */
126264Sbill 	153824,	562,		/* G=cyl 562 thru 814 */
127264Sbill 	445664,	82,		/* H=cyl 82 thru 814 */
128264Sbill /* Later, and more safely for H area...
129264Sbill 	291346,	82,		/* H=cyl 82 thru 561 */
130264Sbill };
131264Sbill 
132264Sbill /*
133264Sbill  * The following defines are used in offset positioning
134264Sbill  * when trying to recover disk errors, with the constants being
135264Sbill  * +/- microinches.  Note that header compare inhibit (HCI) is not
136264Sbill  * tried (this makes sense only during read, in any case.)
137264Sbill  *
138264Sbill  * ARE ALL THESE IMPLEMENTED ON 9300?
139264Sbill  */
140264Sbill #define	P400	020
141264Sbill #define	M400	0220
142264Sbill #define	P800	040
143264Sbill #define	M800	0240
144264Sbill #define	P1200	060
145264Sbill #define	M1200	0260
146264Sbill #define	HCI	020000
147264Sbill 
148264Sbill int	up_offset[16] =
149264Sbill {
150264Sbill 	P400, M400, P400, M400,
151264Sbill 	P800, M800, P800, M800,
152264Sbill 	P1200, M1200, P1200, M1200,
153264Sbill 	0, 0, 0, 0,
154264Sbill };
155264Sbill 
156264Sbill /*
157264Sbill  * Each drive has a table uputab[i].  On this table are sorted the
158264Sbill  * pending requests implementing an elevator algorithm (see dsort.c.)
159264Sbill  * In the upustart() routine, each drive is independently advanced
160264Sbill  * until it is on the desired cylinder for the next transfer and near
161264Sbill  * the desired sector.  The drive is then chained onto the uptab
162264Sbill  * table, and the transfer is initiated by the upstart() routine.
163264Sbill  * When the transfer is completed the driver reinvokes the upustart()
164264Sbill  * routine to set up the next transfer.
165264Sbill  */
166264Sbill struct	buf	uptab;
167264Sbill struct	buf	uputab[NUP];
168264Sbill 
169264Sbill struct	buf	rupbuf;			/* Buffer for raw i/o */
170264Sbill 
171264Sbill /* Drive commands, placed in upcs1 */
172264Sbill #define	GO	01		/* Go bit, set in all commands */
173264Sbill #define	PRESET	020		/* Preset drive at init or after errors */
174264Sbill #define	OFFSET	014		/* Offset heads to try to recover error */
175264Sbill #define	RTC	016		/* Return to center-line after OFFSET */
176264Sbill #define	SEARCH	030		/* Search for cylinder+sector */
177264Sbill #define	RECAL	06		/* Recalibrate, needed after seek error */
178264Sbill #define	DCLR	010		/* Drive clear, after error */
179264Sbill #define	WCOM	060		/* Write */
180264Sbill #define	RCOM	070		/* Read */
181264Sbill 
182264Sbill /* Other bits of upcs1 */
183264Sbill #define	IE	0100		/* Controller wide interrupt enable */
184264Sbill #define	TRE	040000		/* Transfer error */
185266Sbill #define	RDY	020		/* Transfer terminated */
186264Sbill 
187264Sbill /* Drive status bits of upds */
188264Sbill #define	PIP	020000		/* Positioning in progress */
189264Sbill #define	ERR	040000		/* Error has occurred, DCLR necessary */
190264Sbill #define	VV	0100		/* Volume is valid, set by PRESET */
191264Sbill #define	DPR	0400		/* Drive has been preset */
192264Sbill #define	MOL	010000		/* Drive is online, heads loaded, etc */
193264Sbill #define	DRY	0200		/* Drive ready */
194264Sbill 
195264Sbill /* Bits of uper1 */
196264Sbill #define	DCK	0100000		/* Ecc error occurred */
197264Sbill #define	ECH	0100		/* Ecc error was unrecoverable */
198264Sbill #define	WLE	04000		/* Attempt to write read-only drive */
199264Sbill 
200264Sbill /* Bits of upof; the offset bits above are also in this register */
201264Sbill #define	FMT22	010000		/* 16 bits/word, must be always set */
202264Sbill 
203264Sbill #define	b_cylin b_resid
204264Sbill 
205264Sbill int	up_ubinfo;		/* Information about UBA usage saved here */
206264Sbill /*
207264Sbill  * The EMULEX controller balks if accessed quickly after
208264Sbill  * certain operations.  The exact timing has not yet been
209264Sbill  * determined, but delays are known to be needed when changing
210264Sbill  * the selected drive (by writing in upcs2), and thought to be
211264Sbill  * needed after operations like PRESET and DCLR.  The following
212264Sbill  * variables control the delay, DELAY(n) is approximately n usec.
213264Sbill  */
214264Sbill int	idelay = 500;		/* Delay after PRESET or DCLR */
215*267Sbill int	sdelay = 125;		/* Delay after selecting drive in upcs2 */
216266Sbill int	iedel1 = 500;
217266Sbill int	iedel2 = 500;
218266Sbill int	iedel3 = 0;
219266Sbill int	iedel4 = 500;
220264Sbill 
221264Sbill #define	DELAY(N)		{ register int d; d = N; while (--d > 0); }
222264Sbill 
223264Sbill int	nwaitcs2;		/* How many sdelay loops ? */
224264Sbill int	neasycs2;		/* How many sdelay loops not needed ? */
225264Sbill 
226264Sbill #ifdef INTRLVE
227264Sbill daddr_t dkblock();
228264Sbill #endif
229264Sbill 
230264Sbill /*
231264Sbill  * Queue an i/o request for a drive, checking first that it is in range.
232264Sbill  *
233264Sbill  * A unit start is issued if the drive is inactive, causing
234264Sbill  * a SEARCH for the correct cylinder/sector.  If the drive is
235264Sbill  * already nearly on the money and the controller is not transferring
236264Sbill  * we kick it to start the transfer.
237264Sbill  */
238264Sbill upstrategy(bp)
239264Sbill register struct buf *bp;
240264Sbill {
241264Sbill 	register struct buf *dp;
242264Sbill 	register unit, xunit;
243264Sbill 	long sz, bn;
244264Sbill 
245264Sbill 	xunit = minor(bp->b_dev) & 077;
246264Sbill 	sz = bp->b_bcount;
247264Sbill 	sz = (sz+511) >> 9;		/* transfer size in 512 byte sectors */
248264Sbill 	unit = dkunit(bp);
249264Sbill 	if (unit >= NUP ||
250264Sbill 	    bp->b_blkno < 0 ||
251264Sbill 	    (bn = dkblock(bp))+sz > up_sizes[xunit&07].nblocks) {
252264Sbill 		bp->b_flags |= B_ERROR;
253264Sbill 		iodone(bp);
254264Sbill 		return;
255264Sbill 	}
256264Sbill 	bp->b_cylin = bn/(NSECT*NTRAC) + up_sizes[xunit&07].cyloff;
257264Sbill 	dp = &uputab[unit];
258264Sbill 	(void) spl5();
259264Sbill 	disksort(dp, bp);
260264Sbill 	if (dp->b_active == 0) {
261264Sbill 		upustart(unit);
262264Sbill 		if (uptab.b_actf && uptab.b_active == 0)
263264Sbill 			upstart();
264264Sbill 	}
265264Sbill 	(void) spl0();
266264Sbill }
267264Sbill 
268264Sbill /*
269264Sbill  * Start activity on specified drive; called when drive is inactive
270264Sbill  * and new transfer request arrives and also when upas indicates that
271264Sbill  * a SEARCH command is complete.
272264Sbill  */
273264Sbill upustart(unit)
274264Sbill register unit;
275264Sbill {
276264Sbill 	register struct buf *bp, *dp;
277264Sbill 	register struct device *upaddr = UPADDR;
278264Sbill 	daddr_t bn;
279264Sbill 	int sn, cn, csn;
280264Sbill 
281264Sbill 	if (printsw&1) printf("upustart\n");
282266Sbill 	if (unit >= NUP)
283264Sbill 		return;
284266Sbill 	/*
285266Sbill 	 * Whether or not it was before, this unit is no longer busy.
286266Sbill 	 * Check to see if there is (still or now) a request in this
287266Sbill 	 * drives queue, and if there is, select this unit.
288266Sbill 	 */
289264Sbill 	if (unit+DK_N <= DK_NMAX)
290264Sbill 		dk_busy &= ~(1<<(unit+DK_N));
291264Sbill 	dp = &uputab[unit];
292266Sbill 	if ((bp = dp->b_actf) == NULL)
293264Sbill 		return;
294264Sbill 	if ((upaddr->upcs2 & 07) != unit) {
295264Sbill 		upaddr->upcs2 = unit;
296264Sbill 		DELAY(sdelay);
297264Sbill 		nwaitcs2++;
298264Sbill 	} else
299264Sbill 		neasycs2++;
300266Sbill 	/*
301266Sbill 	 * If we have changed packs or just initialized,
302266Sbill 	 * the the volume will not be valid; if so, clear
303266Sbill 	 * the drive, preset it and put in 16bit/word mode.
304266Sbill 	 */
305266Sbill 	if ((upaddr->upds & VV) == 0) {
306266Sbill 		upaddr->upcs1 = IE|DCLR|GO;
307266Sbill 		DELAY(idelay);
308264Sbill 		upaddr->upcs1 = IE|PRESET|GO;
309264Sbill 		DELAY(idelay);
310264Sbill 		upaddr->upof = FMT22;
311264Sbill 	}
312264Sbill 	/*
313266Sbill 	 * We are called from upstrategy when a new request arrives
314266Sbill 	 * if we are not already active (with dp->b_active == 0),
315266Sbill 	 * and we then set dp->b_active to 1 if we are to SEARCH
316266Sbill 	 * for the desired cylinder, or 2 if we are on-cylinder.
317266Sbill 	 * If we SEARCH then we will later be called from upintr()
318266Sbill 	 * when the search is complete, and will link this disk onto
319266Sbill 	 * the uptab.  We then set dp->b_active to 2 so that upintr()
320266Sbill 	 * will not call us again.
321266Sbill 	 *
322266Sbill 	 * NB: Other drives clear the bit in the attention status
323266Sbill 	 * (i.e. upas) register corresponding to the drive when they
324266Sbill 	 * place the drive on the ready (i.e. uptab) queue.  This does
325266Sbill 	 * not work with the Emulex, as the controller hangs the UBA
326266Sbill 	 * of the VAX shortly after the upas register is set, for
327266Sbill 	 * reasons unknown.  This only occurs in multi-spindle configurations,
328266Sbill 	 * but to avoid the problem we use the fact that dp->b_active is
329266Sbill 	 * 2 to replace the clearing of the upas bit.
330264Sbill 	 */
331266Sbill 	if (dp->b_active)
332264Sbill 		goto done;
333266Sbill 	dp->b_active = 1;
334264Sbill 	if ((upaddr->upds & (DPR|MOL)) != (DPR|MOL))
335266Sbill 		goto done;	/* Will redetect error in upstart() soon */
336264Sbill 
337266Sbill 	/*
338266Sbill 	 * Do enough of the disk address decoding to determine
339266Sbill 	 * which cylinder and sector the request is on.
340266Sbill 	 * Then compute the number of the sector SDIST sectors before
341266Sbill 	 * the one where the transfer is to start, this being the
342266Sbill 	 * point where we wish to attempt to begin the transfer,
343266Sbill 	 * allowing approximately SDIST/2 msec for interrupt latency
344266Sbill 	 * and preparation of the request.
345266Sbill 	 *
346266Sbill 	 * If we are on the correct cylinder and the desired sector
347266Sbill 	 * lies between SDIST and SDIST+RDIST sectors ahead of us, then
348266Sbill 	 * we don't bother to SEARCH but just begin the transfer asap.
349266Sbill 	 */
350264Sbill 	bn = dkblock(bp);
351264Sbill 	cn = bp->b_cylin;
352264Sbill 	sn = bn%(NSECT*NTRAC);
353264Sbill 	sn = (sn+NSECT-SDIST)%NSECT;
354264Sbill 
355266Sbill 	if (cn - upaddr->updc)
356266Sbill 		goto search;		/* Not on-cylinder */
357264Sbill 	csn = (upaddr->upla>>6) - sn - 1;
358266Sbill 	if (csn < 0)
359264Sbill 		csn += NSECT;
360266Sbill 	if (csn > NSECT-RDIST)
361264Sbill 		goto done;
362264Sbill 
363264Sbill search:
364264Sbill 	upaddr->updc = cn;
365264Sbill 	upaddr->upda = sn;
366264Sbill 	upaddr->upcs1 = IE|SEARCH|GO;
367266Sbill 	/*
368266Sbill 	 * Mark this unit busy.
369266Sbill 	 */
370264Sbill 	unit += DK_N;
371264Sbill 	if (unit <= DK_NMAX) {
372264Sbill 		dk_busy |= 1<<unit;
373264Sbill 		dk_numb[unit]++;
374264Sbill 	}
375264Sbill 	return;
376264Sbill 
377264Sbill done:
378266Sbill 	/*
379266Sbill 	 * This unit is ready to go.  Make active == 2 so
380266Sbill 	 * we won't get called again (by upintr() because upas&(1<<unit))
381266Sbill 	 * and link us onto the chain of ready disks.
382266Sbill 	 */
383266Sbill 	dp->b_active = 2;
384264Sbill 	dp->b_forw = NULL;
385266Sbill 	if (uptab.b_actf == NULL)
386264Sbill 		uptab.b_actf = dp;
387264Sbill 	else
388264Sbill 		uptab.b_actl->b_forw = dp;
389264Sbill 	uptab.b_actl = dp;
390264Sbill }
391264Sbill 
392264Sbill /*
393264Sbill  * Start a transfer; call from top level at spl5() or on interrupt.
394264Sbill  */
395264Sbill upstart()
396264Sbill {
397264Sbill 	register struct buf *bp, *dp;
398264Sbill 	register unit;
399264Sbill 	register struct device *upaddr;
400264Sbill 	daddr_t bn;
401266Sbill 	int dn, sn, tn, cn, cmd;
402264Sbill 
403264Sbill 	if (printsw&2) printf("upstart\n");
404264Sbill loop:
405266Sbill 	/*
406266Sbill 	 * Pick a drive off the queue of ready drives, and
407266Sbill 	 * perform the first transfer on its queue.
408266Sbill 	 *
409266Sbill 	 * Looping here is completely for the sake of drives which
410266Sbill 	 * are not present and on-line, for which we completely clear the
411266Sbill 	 * request queue.
412266Sbill 	 */
413264Sbill 	if ((dp = uptab.b_actf) == NULL)
414264Sbill 		return;
415264Sbill 	if ((bp = dp->b_actf) == NULL) {
416264Sbill 		uptab.b_actf = dp->b_forw;
417264Sbill 		goto loop;
418264Sbill 	}
419266Sbill 	/*
420266Sbill 	 * Mark the controller busy, and multi-part disk address.
421266Sbill 	 * Select the unit on which the i/o is to take place.
422266Sbill 	 */
423264Sbill 	uptab.b_active++;
424264Sbill 	unit = minor(bp->b_dev) & 077;
425264Sbill 	dn = dkunit(bp);
426264Sbill 	bn = dkblock(bp);
427264Sbill 	cn = up_sizes[unit&07].cyloff;
428264Sbill 	cn += bn/(NSECT*NTRAC);
429264Sbill 	sn = bn%(NSECT*NTRAC);
430264Sbill 	tn = sn/NSECT;
431266Sbill 	sn %= NSECT;
432264Sbill 	upaddr = UPADDR;
433264Sbill 	if ((upaddr->upcs2 & 07) != dn) {
434264Sbill 		upaddr->upcs2 = dn;
435264Sbill 		DELAY(sdelay);
436264Sbill 		nwaitcs2++;
437264Sbill 	} else
438264Sbill 		neasycs2++;
439266Sbill 	up_ubinfo = ubasetup(bp, 1);	/* In a funny place for delay... */
440266Sbill 	/*
441266Sbill 	 * If drive is not present and on-line, then
442266Sbill 	 * get rid of this with an error and loop to get
443266Sbill 	 * rid of the rest of its queued requests.
444266Sbill 	 * (Then on to any other ready drives.)
445266Sbill 	 */
446264Sbill 	if ((upaddr->upds & (DPR|MOL)) != (DPR|MOL)) {
447264Sbill 		uptab.b_active = 0;
448264Sbill 		uptab.b_errcnt = 0;
449264Sbill 		dp->b_actf = bp->av_forw;
450266Sbill 		dp->b_active = 0;
451264Sbill 		bp->b_flags |= B_ERROR;
452264Sbill 		iodone(bp);
453266Sbill 		ubafree(up_ubinfo), up_ubinfo = 0;	/* A funny place ... */
454264Sbill 		goto loop;
455264Sbill 	}
456266Sbill 	/*
457266Sbill 	 * If this is a retry, then with the 16'th retry we
458266Sbill 	 * begin to try offsetting the heads to recover the data.
459266Sbill 	 */
460266Sbill 	if (uptab.b_errcnt >= 16) {
461264Sbill 		upaddr->upof = up_offset[uptab.b_errcnt & 017] | FMT22;
462266Sbill 		upaddr->upcs1 = IE|OFFSET|GO;
463264Sbill 		DELAY(idelay);
464266Sbill 		while (upaddr->upds & PIP)
465264Sbill 			DELAY(25);
466264Sbill 	}
467266Sbill 	/*
468266Sbill 	 * Now set up the transfer, retrieving the high
469266Sbill 	 * 2 bits of the UNIBUS address from the information
470266Sbill 	 * returned by ubasetup() for the cs1 register bits 8 and 9.
471266Sbill 	 */
472264Sbill 	upaddr->updc = cn;
473264Sbill 	upaddr->upda = (tn << 8) + sn;
474264Sbill 	upaddr->upba = up_ubinfo;
475264Sbill 	upaddr->upwc = -bp->b_bcount / sizeof (short);
476266Sbill 	cmd = (up_ubinfo >> 8) & 0x300;
477264Sbill 	if (bp->b_flags & B_READ)
478266Sbill 		cmd |= IE|RCOM|GO;
479264Sbill 	else
480266Sbill 		cmd |= IE|WCOM|GO;
481266Sbill 	upaddr->upcs1 = cmd;
482*267Sbill 	if (csdel3) DELAY(csdel3);
483266Sbill 	/*
484266Sbill 	 * This is a controller busy situation.
485266Sbill 	 * Record in dk slot NUP+DK_N (after last drive)
486266Sbill 	 * unless there aren't that many slots reserved for
487266Sbill 	 * us in which case we record this as a drive busy
488266Sbill 	 * (if there is room for that).
489266Sbill 	 */
490264Sbill 	unit = dn+DK_N;
491264Sbill 	if (NUP+DK_N == DK_NMAX)
492264Sbill 		unit = NUP+DK_N;
493264Sbill 	if (unit <= DK_NMAX) {
494264Sbill 		dk_busy |= 1<<unit;
495264Sbill 		dk_numb[unit]++;
496264Sbill 		dk_wds[unit] += bp->b_bcount>>6;
497264Sbill 	}
498264Sbill }
499264Sbill 
500264Sbill /*
501264Sbill  * Handle a device interrupt.
502264Sbill  *
503264Sbill  * If the transferring drive needs attention, service it
504264Sbill  * retrying on error or beginning next transfer.
505264Sbill  * Service all other ready drives, calling ustart to transfer
506264Sbill  * their blocks to the ready queue in uptab, and then restart
507264Sbill  * the controller if there is anything to do.
508264Sbill  */
509264Sbill upintr()
510264Sbill {
511264Sbill 	register struct buf *bp, *dp;
512264Sbill 	register unit;
513264Sbill 	register struct device *upaddr = UPADDR;
514264Sbill 	int as = upaddr->upas & 0377;
515264Sbill 
516266Sbill 	if (printsw&4) printf("upintr as=%d act %d %d %d\n", as, uptab.b_active, uputab[0].b_active, uputab[1].b_active);
517266Sbill 	if (uptab.b_active) {
518266Sbill 		/*
519266Sbill 		 * The drive is transferring, thus the hardware
520266Sbill 		 * (say the designers) will only interrupt when the transfer
521266Sbill 		 * completes; check for it anyways.
522266Sbill 		 */
523266Sbill 		if ((upaddr->upcs1 & RDY) == 0) {
524*267Sbill 			printf("!RDY in upintr: cs1 %o\n", upaddr->upcs1);
525*267Sbill printf("as=%d act %d %d %d\n", as, uptab.b_active, uputab[0].b_active, uputab[1].b_active);
526*267Sbill }
527266Sbill 		/*
528266Sbill 		 * Mark controller or drive not busy, and check for an
529266Sbill 		 * error condition which may have resulted from the transfer.
530266Sbill 		 */
531264Sbill 		dp = uptab.b_actf;
532264Sbill 		bp = dp->b_actf;
533264Sbill 		unit = dkunit(bp);
534264Sbill 		if (DK_N+NUP == DK_NMAX)
535264Sbill 			dk_busy &= ~(1<<(DK_N+NUP));
536264Sbill 		else if (DK_N+unit <= DK_NMAX)
537264Sbill 			dk_busy &= ~(1<<(DK_N+unit));
538264Sbill 		if (upaddr->upcs1 & TRE) {
539266Sbill 			/*
540266Sbill 			 * An error occurred, indeed.  Select this unit
541266Sbill 			 * to get at the drive status (a SEARCH may have
542266Sbill 			 * intervened to change the selected unit), and
543266Sbill 			 * wait for the command which caused the interrupt
544266Sbill 			 * to complete (DRY).
545266Sbill 			 *
546266Sbill 			 * WHY IS THE WAIT NECESSARY?
547266Sbill 			 */
548264Sbill 			if ((upaddr->upcs2 & 07) != unit) {
549264Sbill 				upaddr->upcs2 = unit;
550264Sbill 				DELAY(sdelay);
551264Sbill 				nwaitcs2++;
552264Sbill 			} else
553264Sbill 				neasycs2++;
554266Sbill 			while ((upaddr->upds & DRY) == 0)
555264Sbill 				DELAY(25);
556266Sbill 			/*
557266Sbill 			 * After 28 retries (16 w/o servo offsets, and then
558266Sbill 			 * 12 with servo offsets), or if we encountered
559266Sbill 			 * an error because the drive is write-protected,
560266Sbill 			 * give up.  Print an error message on the last 2
561266Sbill 			 * retries before a hard failure.
562266Sbill 			 */
563266Sbill 			if (++uptab.b_errcnt > 28 || upaddr->uper1&WLE)
564264Sbill 				bp->b_flags |= B_ERROR;
565264Sbill 			else
566266Sbill 				uptab.b_active = 0;	/* To force retry */
567266Sbill 			if (uptab.b_errcnt > 27)
568264Sbill 				deverror(bp, upaddr->upcs2, upaddr->uper1);
569266Sbill 			/*
570266Sbill 			 * If this was a correctible ECC error, let upecc
571266Sbill 			 * do the dirty work to correct it.  If upecc
572266Sbill 			 * starts another READ for the rest of the data
573266Sbill 			 * then it returns 1 (having set uptab.b_active).
574266Sbill 			 * Otherwise we are done and fall through to
575266Sbill 			 * finish up.
576266Sbill 			 */
577266Sbill 			if ((upaddr->uper1&(DCK|ECH))==DCK && upecc(upaddr, bp))
578266Sbill 				return;
579266Sbill 			/*
580266Sbill 			 * Clear the drive and, every 4 retries, recalibrate
581266Sbill 			 * to hopefully help clear up seek positioning problems.
582266Sbill 			 */
583264Sbill 			upaddr->upcs1 = TRE|IE|DCLR|GO;
584264Sbill 			DELAY(idelay);
585266Sbill 			if ((uptab.b_errcnt&07) == 4) {
586264Sbill 				upaddr->upcs1 = RECAL|GO|IE;
587264Sbill 				DELAY(idelay);
588264Sbill 				while(upaddr->upds & PIP)
589264Sbill 					DELAY(25);
590264Sbill 			}
591264Sbill 		}
592266Sbill 		/*
593266Sbill 		 * If we are still noted as active, then no
594266Sbill 		 * (further) retries are necessary.
595266Sbill 		 *
596266Sbill 		 * Make sure the correct unit is selected,
597266Sbill 		 * return it to centerline if necessary, and mark
598266Sbill 		 * this i/o complete, starting the next transfer
599266Sbill 		 * on this drive with the upustart routine (if any).
600266Sbill 		 */
601266Sbill 		if (uptab.b_active) {
602266Sbill 			if ((upaddr->upcs2 & 07) != unit) {
603266Sbill 				upaddr->upcs2 = unit;
604266Sbill 				DELAY(sdelay);
605266Sbill 				nwaitcs2++;
606266Sbill 			} else
607266Sbill 				neasycs2++;
608266Sbill 			if (uptab.b_errcnt >= 16) {
609266Sbill 				upaddr->upcs1 = RTC|GO|IE;
610264Sbill 				DELAY(idelay);
611266Sbill 				while (upaddr->upds & PIP)
612264Sbill 					DELAY(25);
613264Sbill 			}
614264Sbill 			uptab.b_active = 0;
615264Sbill 			uptab.b_errcnt = 0;
616264Sbill 			uptab.b_actf = dp->b_forw;
617264Sbill 			dp->b_active = 0;
618264Sbill 			dp->b_errcnt = 0;
619264Sbill 			dp->b_actf = bp->av_forw;
620266Sbill 			bp->b_resid = (-upaddr->upwc * sizeof(short));
621266Sbill 			upaddr->upcs1 = IE;
622264Sbill 			iodone(bp);
623264Sbill 			if(dp->b_actf)
624264Sbill 				upustart(unit);
625264Sbill 		}
626264Sbill 		as &= ~(1<<unit);
627264Sbill 		ubafree(up_ubinfo), up_ubinfo = 0;
628266Sbill 	}
629266Sbill #ifndef notdef
630266Sbill 	else {
631266Sbill 		if (printsw&64) printf("cs1 %o\n", upaddr->upcs1);
632264Sbill 		if (upaddr->upcs1 & TRE) {
633*267Sbill 			printf("TRE in upintr: cs1 %o\n", upaddr->upcs1);
634*267Sbill printf("as=%d act %d %d %d\n", as, uptab.b_active, uputab[0].b_active, uputab[1].b_active);
635264Sbill 			upaddr->upcs1 = TRE;
636264Sbill 			DELAY(idelay);
637266Sbill 			if (printsw&64) printf("after TRE cs1 %o\n", upaddr->upcs1);
638264Sbill 		}
639264Sbill 	}
640266Sbill #endif
641266Sbill 	/*
642266Sbill 	 * If we have a unit with an outstanding SEARCH,
643266Sbill 	 * and the hardware indicates the unit requires attention,
644266Sbill 	 * the bring the drive to the ready queue.
645266Sbill 	 * Finally, if the controller is not transferring
646266Sbill 	 * start it if any drives are now ready to transfer.
647266Sbill 	 */
648266Sbill 	for (unit = 0; unit < NUP; unit++)
649266Sbill 		if (as & (1<<unit))
650*267Sbill 			if (uputab[unit].b_active == 1) {
651*267Sbill 				upaddr->upas = 1<<unit;
652266Sbill 				upustart(unit);
653*267Sbill 			} else {
654*267Sbill 			printf("as in upintr: cs1 %o\n", upaddr->upcs1);
655*267Sbill printf("as=%d act %d %d %d\n", as, uptab.b_active, uputab[0].b_active, uputab[1].b_active);
656266Sbill 				upaddr->upas = 1<<unit;
657266Sbill 				DELAY(1000);
658266Sbill 			}
659266Sbill 	if (uptab.b_actf && uptab.b_active == 0)
660266Sbill 		upstart();
661266Sbill out:
662266Sbill 	if ((upaddr->upcs1&IE) == 0)
663266Sbill 		upaddr->upcs1 = IE;
664266Sbill 	if (printsw&128) printf("exit cs1 %o\n", upaddr->upcs1);
665264Sbill }
666264Sbill 
667264Sbill upread(dev)
668264Sbill {
669264Sbill 
670264Sbill 	physio(upstrategy, &rupbuf, dev, B_READ, minphys);
671264Sbill }
672264Sbill 
673264Sbill upwrite(dev)
674264Sbill {
675264Sbill 
676264Sbill 	physio(upstrategy, &rupbuf, dev, B_WRITE, minphys);
677264Sbill }
678264Sbill 
679266Sbill /*
680266Sbill  * Correct an ECC error, and restart the i/o to complete
681266Sbill  * the transfer if necessary.  This is quite complicated because
682266Sbill  * the transfer may be going to an odd memory address base and/or
683266Sbill  * across a page boundary.
684266Sbill  */
685264Sbill upecc(up, bp)
686264Sbill register struct device *up;
687264Sbill register struct buf *bp;
688264Sbill {
689264Sbill 	struct uba_regs *ubp = (struct uba_regs *)UBA0;
690266Sbill 	register int i;
691264Sbill 	caddr_t addr;
692266Sbill 	int reg, bit, byte, npf, mask, o, cmd, ubaddr;
693264Sbill 	int bn, cn, tn, sn;
694264Sbill 
695264Sbill 	if (printsw&8) printf("upecc\n");
696264Sbill 	/*
697266Sbill 	 * Npf is the number of sectors transferred before the sector
698266Sbill 	 * containing the ECC error, and reg is the UBA register
699266Sbill 	 * mapping (the first part of) the transfer.
700266Sbill 	 * O is offset within a memory page of the first byte transferred.
701264Sbill 	 */
702266Sbill 	npf = btop((up->upwc * sizeof(short)) + bp->b_bcount) - 1;
703266Sbill 	reg = btop(up_ubinfo&0x3ffff) + npf;
704264Sbill 	o = (int)bp->b_un.b_addr & PGOFSET;
705264Sbill 	printf("%D ", bp->b_blkno+npf);
706264Sbill 	prdev("ECC", bp->b_dev);
707264Sbill 	mask = up->upec2;
708264Sbill 	if (mask == 0) {
709266Sbill 		up->upof = FMT22;		/* == RTC ???? */
710264Sbill 		DELAY(idelay);
711264Sbill 		return (0);
712264Sbill 	}
713266Sbill 	/*
714266Sbill 	 * Flush the buffered data path, and compute the
715266Sbill 	 * byte and bit position of the error.  The variable i
716266Sbill 	 * is the byte offset in the transfer, the variable byte
717266Sbill 	 * is the offset from a page boundary in main memory.
718266Sbill 	 */
719266Sbill 	ubp->uba_dpr[(up_ubinfo>>28)&0x0f] |= BNE;
720266Sbill 	i = up->upec1 - 1;		/* -1 makes 0 origin */
721266Sbill 	bit = i&07;
722266Sbill 	i = (i&~07)>>3;
723264Sbill 	byte = i + o;
724266Sbill 	/*
725266Sbill 	 * Correct while possible bits remain of mask.  Since mask
726266Sbill 	 * contains 11 bits, we continue while the bit offset is > -11.
727266Sbill 	 * Also watch out for end of this block and the end of the whole
728266Sbill 	 * transfer.
729266Sbill 	 */
730266Sbill 	while (i < 512 && (int)ptob(npf)+i < bp->b_bcount && bit > -11) {
731266Sbill 		addr = ptob(ubp->uba_map[reg+btop(byte)].pg_pfnum)+
732266Sbill 		    (byte & PGOFSET);
733266Sbill 		putmemc(addr, getmemc(addr)^(mask<<bit));
734266Sbill 		byte++;
735266Sbill 		i++;
736266Sbill 		bit -= 8;
737264Sbill 	}
738266Sbill 	uptab.b_active++;	/* Either complete or continuing... */
739264Sbill 	if (up->upwc == 0)
740264Sbill 		return (0);
741266Sbill 	/*
742266Sbill 	 * Have to continue the transfer... clear the drive,
743266Sbill 	 * and compute the position where the transfer is to continue.
744266Sbill 	 * We have completed npf+1 sectors of the transfer already;
745266Sbill 	 * restart at offset o of next sector (i.e. in UBA register reg+1).
746266Sbill 	 */
747266Sbill 	up->upcs1 = TRE|IE|DCLR|GO;
748264Sbill 	DELAY(idelay);
749264Sbill 	bn = dkblock(bp);
750264Sbill 	cn = bp->b_cylin;
751266Sbill 	sn = bn%(NSECT*NTRAC) + npf + 1;
752264Sbill 	tn = sn/NSECT;
753264Sbill 	sn %= NSECT;
754266Sbill 	cn += tn/NTRAC;
755266Sbill 	tn %= NTRAC;
756264Sbill 	up->updc = cn;
757266Sbill 	up->upda = (tn << 8) | sn;
758266Sbill 	ubaddr = (int)ptob(reg+1) + o;
759266Sbill 	up->upba = ubaddr;
760266Sbill 	cmd = (ubaddr >> 8) & 0x300;
761266Sbill 	cmd |= IE|GO|RCOM;
762266Sbill 	up->upcs1 = cmd;
763264Sbill 	return (1);
764264Sbill }
765