xref: /csrg-svn/sys/vax/uba/up.c (revision 268)
1*268Sbill int	asdel = 500;
2267Sbill int	csdel3 = 100;
3264Sbill int	printsw;
4*268Sbill /*	10/14/12	3.4	06/18/80	*/
5264Sbill 
6264Sbill /*
7264Sbill  * Emulex UNIBUS disk driver with overlapped seeks and ECC recovery.
8264Sbill  *
9266Sbill  * NB: This device is very sensitive: be aware that the code is the way
10266Sbill  *     it is for good reason and that there are delay loops here which may
11266Sbill  *     have to be lengthened if your processor is faster and which should
12266Sbill  *     probably be shortened if your processor is slower.
13266Sbill  *
14264Sbill  * This driver has been tested on a SC-11B Controller, configured
15264Sbill  * with the following internal switch settings:
16264Sbill  *	SW1-1	5/19 surfaces	(off, 19 surfaces on Ampex 9300)
17264Sbill  *	SW1-2	chksum enable	(off, checksum disabled)
18264Sbill  *	SW1-3	volume select	(off, 815 cylinders)
19264Sbill  *	SW1-4	sector select	(on, 32 sectors)
20264Sbill  *	SW1-5	unused		(off)
21264Sbill  *	SW1-6	port select	(on, single port)
22264Sbill  *	SW1-7	npr delay	(off, disable)
23264Sbill  *	SW1-8	ecc test mode	(off, disable)
24264Sbill  * and top mounted switches:
25264Sbill  *	SW2-1	extend opcodes	(off=open, disable)
26264Sbill  *	SW2-2	extend diag	(off=open, disable)
27264Sbill  *	SW2-3	4 wd dma burst	(off=open, disable)
28264Sbill  *	SW2-4	unused		(off=open)
29264Sbill  *
30264Sbill  * The controller transfers data much more rapidly with SW2-3 set,
31264Sbill  * but we have previously experienced problems with it set this way.
32264Sbill  * We intend to try this again in the near future.
33264Sbill  *
34264Sbill  *	wnj	June 14, 1980
35264Sbill  */
36264Sbill 
37264Sbill #include "../h/param.h"
38264Sbill #include "../h/systm.h"
39264Sbill #include "../h/buf.h"
40264Sbill #include "../h/conf.h"
41264Sbill #include "../h/dir.h"
42264Sbill #include "../h/user.h"
43264Sbill #include "../h/map.h"
44264Sbill #include "../h/mba.h"
45264Sbill #include "../h/mtpr.h"
46264Sbill #include "../h/pte.h"
47264Sbill #include "../h/uba.h"
48264Sbill #include "../h/vm.h"
49264Sbill 
50264Sbill /*
51264Sbill  * Define number of drives, and range of sampling information to be used.
52264Sbill  *
53264Sbill  * Normally, DK_N .. DK_N+NUP-1 gather individual drive stats,
54264Sbill  * and DK_N+NUP gathers controller transferring stats.
55264Sbill  *
56264Sbill  * If DK_N+NUP > DK_NMAX, then transfer stats are divided per drive.
57264Sbill  * If DK_NMAX is yet smaller, some drives are not monitored.
58264Sbill  */
59264Sbill #define	DK_N	1
60264Sbill #define	DK_NMAX	2
61264Sbill 
62264Sbill #define	ushort	unsigned short
63264Sbill 
64264Sbill struct	device
65264Sbill {
66264Sbill 	ushort	upcs1;		/* control and status register 1 */
67264Sbill 	short	upwc;		/* word count register */
68264Sbill 	ushort	upba;		/* UNIBUS address register */
69264Sbill 	ushort	upda;		/* desired address register */
70264Sbill 	ushort	upcs2;		/* control and status register 2 */
71264Sbill 	ushort	upds;		/* drive Status */
72264Sbill 	ushort	uper1;		/* error register 1 */
73264Sbill 	ushort	upas;		/* attention summary */
74264Sbill 	ushort	upla;		/* look ahead */
75264Sbill 	ushort	updb;		/* data buffer */
76264Sbill 	ushort	upmr;		/* maintenance */
77264Sbill 	ushort	updt;		/* drive type */
78264Sbill 	ushort	upsn;		/* serial number */
79264Sbill 	ushort	upof;		/* offset register */
80264Sbill 	ushort	updc;		/* desired cylinder address register */
81264Sbill 	ushort	upcc;		/* current cylinder */
82264Sbill 	ushort	uper2;		/* error register 2 */
83264Sbill 	ushort	uper3;		/* error register 3 */
84264Sbill 	ushort	upec1;		/* burst error bit position */
85264Sbill 	ushort	upec2;		/* burst error bit pattern */
86264Sbill };
87264Sbill 
88264Sbill #define	UPADDR	((struct device *)(UBA0_DEV + 0176700))
89264Sbill 
90264Sbill #define	NUP	2		/* Number of drives this installation */
91264Sbill 
92264Sbill #define	NSECT	32
93264Sbill #define	NTRAC	19
94264Sbill 
95264Sbill /*
96264Sbill  * Constants controlling on-cylinder SEARCH usage.
97264Sbill  *
98264Sbill  * We assume that it takes SDIST sectors of time to set up a transfer.
99264Sbill  * If a drive is on-cylinder, and between SDIST and SDIST+RDIST sectors
100264Sbill  * from the first sector to be transferred, then we just perform the
101264Sbill  * transfer.  SDIST represents interrupt latency, RDIST the amount
102264Sbill  * of rotation which is tolerable to avoid another interrupt.
103264Sbill  */
104266Sbill #define	SDIST	3		/* 2-3 sectors 1-1.5 msec */
105266Sbill #define	RDIST	6		/* 5-6 sectors 2.5-3 msec */
106264Sbill 
107264Sbill /*
108264Sbill  * To fill a 300M drive:
109264Sbill  *	A is designed to be used as a root.
110264Sbill  *	B is suitable for a swap area.
111264Sbill  *	H is the primary storage area.
112264Sbill  * On systems with RP06'es, we normally use only 291346 blocks of the H
113264Sbill  * area, and use DEF or G to cover the rest of the drive.  The C system
114264Sbill  * covers the whole drive and can be used for pack-pack copying.
115264Sbill  */
116264Sbill struct	size
117264Sbill {
118264Sbill 	daddr_t	nblocks;
119264Sbill 	int	cyloff;
120264Sbill } up_sizes[8] = {
121264Sbill 	15884,	0,		/* A=cyl 0 thru 26 */
122264Sbill 	33440,	27,		/* B=cyl 27 thru 81 */
123264Sbill 	494912,	0,		/* C=cyl 0 thru 814 */
124264Sbill 	15884,	562,		/* D=cyl 562 thru 588 */
125264Sbill 	55936,	589,		/* E=cyl 589 thru 680 */
126264Sbill 	81472,	681,		/* F=cyl 681 thru 814 */
127264Sbill 	153824,	562,		/* G=cyl 562 thru 814 */
128264Sbill 	445664,	82,		/* H=cyl 82 thru 814 */
129264Sbill /* Later, and more safely for H area...
130264Sbill 	291346,	82,		/* H=cyl 82 thru 561 */
131264Sbill };
132264Sbill 
133264Sbill /*
134264Sbill  * The following defines are used in offset positioning
135264Sbill  * when trying to recover disk errors, with the constants being
136264Sbill  * +/- microinches.  Note that header compare inhibit (HCI) is not
137264Sbill  * tried (this makes sense only during read, in any case.)
138264Sbill  *
139264Sbill  * ARE ALL THESE IMPLEMENTED ON 9300?
140264Sbill  */
141264Sbill #define	P400	020
142264Sbill #define	M400	0220
143264Sbill #define	P800	040
144264Sbill #define	M800	0240
145264Sbill #define	P1200	060
146264Sbill #define	M1200	0260
147264Sbill #define	HCI	020000
148264Sbill 
149264Sbill int	up_offset[16] =
150264Sbill {
151264Sbill 	P400, M400, P400, M400,
152264Sbill 	P800, M800, P800, M800,
153264Sbill 	P1200, M1200, P1200, M1200,
154264Sbill 	0, 0, 0, 0,
155264Sbill };
156264Sbill 
157264Sbill /*
158264Sbill  * Each drive has a table uputab[i].  On this table are sorted the
159264Sbill  * pending requests implementing an elevator algorithm (see dsort.c.)
160264Sbill  * In the upustart() routine, each drive is independently advanced
161264Sbill  * until it is on the desired cylinder for the next transfer and near
162264Sbill  * the desired sector.  The drive is then chained onto the uptab
163264Sbill  * table, and the transfer is initiated by the upstart() routine.
164264Sbill  * When the transfer is completed the driver reinvokes the upustart()
165264Sbill  * routine to set up the next transfer.
166264Sbill  */
167264Sbill struct	buf	uptab;
168264Sbill struct	buf	uputab[NUP];
169264Sbill 
170264Sbill struct	buf	rupbuf;			/* Buffer for raw i/o */
171264Sbill 
172264Sbill /* Drive commands, placed in upcs1 */
173264Sbill #define	GO	01		/* Go bit, set in all commands */
174264Sbill #define	PRESET	020		/* Preset drive at init or after errors */
175264Sbill #define	OFFSET	014		/* Offset heads to try to recover error */
176264Sbill #define	RTC	016		/* Return to center-line after OFFSET */
177264Sbill #define	SEARCH	030		/* Search for cylinder+sector */
178264Sbill #define	RECAL	06		/* Recalibrate, needed after seek error */
179264Sbill #define	DCLR	010		/* Drive clear, after error */
180264Sbill #define	WCOM	060		/* Write */
181264Sbill #define	RCOM	070		/* Read */
182264Sbill 
183264Sbill /* Other bits of upcs1 */
184264Sbill #define	IE	0100		/* Controller wide interrupt enable */
185264Sbill #define	TRE	040000		/* Transfer error */
186266Sbill #define	RDY	020		/* Transfer terminated */
187264Sbill 
188264Sbill /* Drive status bits of upds */
189264Sbill #define	PIP	020000		/* Positioning in progress */
190264Sbill #define	ERR	040000		/* Error has occurred, DCLR necessary */
191264Sbill #define	VV	0100		/* Volume is valid, set by PRESET */
192264Sbill #define	DPR	0400		/* Drive has been preset */
193264Sbill #define	MOL	010000		/* Drive is online, heads loaded, etc */
194264Sbill #define	DRY	0200		/* Drive ready */
195264Sbill 
196264Sbill /* Bits of uper1 */
197264Sbill #define	DCK	0100000		/* Ecc error occurred */
198264Sbill #define	ECH	0100		/* Ecc error was unrecoverable */
199264Sbill #define	WLE	04000		/* Attempt to write read-only drive */
200264Sbill 
201264Sbill /* Bits of upof; the offset bits above are also in this register */
202264Sbill #define	FMT22	010000		/* 16 bits/word, must be always set */
203264Sbill 
204264Sbill #define	b_cylin b_resid
205264Sbill 
206264Sbill int	up_ubinfo;		/* Information about UBA usage saved here */
207264Sbill /*
208264Sbill  * The EMULEX controller balks if accessed quickly after
209264Sbill  * certain operations.  The exact timing has not yet been
210264Sbill  * determined, but delays are known to be needed when changing
211264Sbill  * the selected drive (by writing in upcs2), and thought to be
212264Sbill  * needed after operations like PRESET and DCLR.  The following
213264Sbill  * variables control the delay, DELAY(n) is approximately n usec.
214264Sbill  */
215264Sbill int	idelay = 500;		/* Delay after PRESET or DCLR */
216*268Sbill int	sdelay = 150;		/* Delay after selecting drive in upcs2 */
217264Sbill 
218264Sbill #define	DELAY(N)		{ register int d; d = N; while (--d > 0); }
219264Sbill 
220264Sbill int	nwaitcs2;		/* How many sdelay loops ? */
221264Sbill int	neasycs2;		/* How many sdelay loops not needed ? */
222264Sbill 
223264Sbill #ifdef INTRLVE
224264Sbill daddr_t dkblock();
225264Sbill #endif
226264Sbill 
227264Sbill /*
228264Sbill  * Queue an i/o request for a drive, checking first that it is in range.
229264Sbill  *
230264Sbill  * A unit start is issued if the drive is inactive, causing
231264Sbill  * a SEARCH for the correct cylinder/sector.  If the drive is
232264Sbill  * already nearly on the money and the controller is not transferring
233264Sbill  * we kick it to start the transfer.
234264Sbill  */
235264Sbill upstrategy(bp)
236264Sbill register struct buf *bp;
237264Sbill {
238264Sbill 	register struct buf *dp;
239264Sbill 	register unit, xunit;
240264Sbill 	long sz, bn;
241264Sbill 
242264Sbill 	xunit = minor(bp->b_dev) & 077;
243264Sbill 	sz = bp->b_bcount;
244264Sbill 	sz = (sz+511) >> 9;		/* transfer size in 512 byte sectors */
245264Sbill 	unit = dkunit(bp);
246264Sbill 	if (unit >= NUP ||
247264Sbill 	    bp->b_blkno < 0 ||
248264Sbill 	    (bn = dkblock(bp))+sz > up_sizes[xunit&07].nblocks) {
249264Sbill 		bp->b_flags |= B_ERROR;
250264Sbill 		iodone(bp);
251264Sbill 		return;
252264Sbill 	}
253264Sbill 	bp->b_cylin = bn/(NSECT*NTRAC) + up_sizes[xunit&07].cyloff;
254264Sbill 	dp = &uputab[unit];
255264Sbill 	(void) spl5();
256264Sbill 	disksort(dp, bp);
257264Sbill 	if (dp->b_active == 0) {
258*268Sbill 		(void) upustart(unit);
259264Sbill 		if (uptab.b_actf && uptab.b_active == 0)
260*268Sbill 			(void) upstart();
261264Sbill 	}
262264Sbill 	(void) spl0();
263264Sbill }
264264Sbill 
265264Sbill /*
266264Sbill  * Start activity on specified drive; called when drive is inactive
267264Sbill  * and new transfer request arrives and also when upas indicates that
268264Sbill  * a SEARCH command is complete.
269264Sbill  */
270264Sbill upustart(unit)
271264Sbill register unit;
272264Sbill {
273264Sbill 	register struct buf *bp, *dp;
274264Sbill 	register struct device *upaddr = UPADDR;
275264Sbill 	daddr_t bn;
276264Sbill 	int sn, cn, csn;
277*268Sbill 	int didie = 0;
278264Sbill 
279264Sbill 	if (printsw&1) printf("upustart\n");
280266Sbill 	if (unit >= NUP)
281*268Sbill 		goto out;
282266Sbill 	/*
283266Sbill 	 * Whether or not it was before, this unit is no longer busy.
284266Sbill 	 * Check to see if there is (still or now) a request in this
285266Sbill 	 * drives queue, and if there is, select this unit.
286266Sbill 	 */
287264Sbill 	if (unit+DK_N <= DK_NMAX)
288264Sbill 		dk_busy &= ~(1<<(unit+DK_N));
289264Sbill 	dp = &uputab[unit];
290266Sbill 	if ((bp = dp->b_actf) == NULL)
291*268Sbill 		goto out;
292264Sbill 	if ((upaddr->upcs2 & 07) != unit) {
293264Sbill 		upaddr->upcs2 = unit;
294264Sbill 		DELAY(sdelay);
295264Sbill 		nwaitcs2++;
296264Sbill 	} else
297264Sbill 		neasycs2++;
298266Sbill 	/*
299266Sbill 	 * If we have changed packs or just initialized,
300266Sbill 	 * the the volume will not be valid; if so, clear
301266Sbill 	 * the drive, preset it and put in 16bit/word mode.
302266Sbill 	 */
303266Sbill 	if ((upaddr->upds & VV) == 0) {
304266Sbill 		upaddr->upcs1 = IE|DCLR|GO;
305266Sbill 		DELAY(idelay);
306264Sbill 		upaddr->upcs1 = IE|PRESET|GO;
307264Sbill 		DELAY(idelay);
308264Sbill 		upaddr->upof = FMT22;
309*268Sbill 		didie = 1;
310264Sbill 	}
311264Sbill 	/*
312266Sbill 	 * We are called from upstrategy when a new request arrives
313266Sbill 	 * if we are not already active (with dp->b_active == 0),
314266Sbill 	 * and we then set dp->b_active to 1 if we are to SEARCH
315266Sbill 	 * for the desired cylinder, or 2 if we are on-cylinder.
316266Sbill 	 * If we SEARCH then we will later be called from upintr()
317266Sbill 	 * when the search is complete, and will link this disk onto
318266Sbill 	 * the uptab.  We then set dp->b_active to 2 so that upintr()
319266Sbill 	 * will not call us again.
320266Sbill 	 *
321266Sbill 	 * NB: Other drives clear the bit in the attention status
322266Sbill 	 * (i.e. upas) register corresponding to the drive when they
323266Sbill 	 * place the drive on the ready (i.e. uptab) queue.  This does
324266Sbill 	 * not work with the Emulex, as the controller hangs the UBA
325266Sbill 	 * of the VAX shortly after the upas register is set, for
326266Sbill 	 * reasons unknown.  This only occurs in multi-spindle configurations,
327266Sbill 	 * but to avoid the problem we use the fact that dp->b_active is
328266Sbill 	 * 2 to replace the clearing of the upas bit.
329264Sbill 	 */
330266Sbill 	if (dp->b_active)
331264Sbill 		goto done;
332266Sbill 	dp->b_active = 1;
333264Sbill 	if ((upaddr->upds & (DPR|MOL)) != (DPR|MOL))
334266Sbill 		goto done;	/* Will redetect error in upstart() soon */
335264Sbill 
336266Sbill 	/*
337266Sbill 	 * Do enough of the disk address decoding to determine
338266Sbill 	 * which cylinder and sector the request is on.
339266Sbill 	 * Then compute the number of the sector SDIST sectors before
340266Sbill 	 * the one where the transfer is to start, this being the
341266Sbill 	 * point where we wish to attempt to begin the transfer,
342266Sbill 	 * allowing approximately SDIST/2 msec for interrupt latency
343266Sbill 	 * and preparation of the request.
344266Sbill 	 *
345266Sbill 	 * If we are on the correct cylinder and the desired sector
346266Sbill 	 * lies between SDIST and SDIST+RDIST sectors ahead of us, then
347266Sbill 	 * we don't bother to SEARCH but just begin the transfer asap.
348266Sbill 	 */
349264Sbill 	bn = dkblock(bp);
350264Sbill 	cn = bp->b_cylin;
351264Sbill 	sn = bn%(NSECT*NTRAC);
352264Sbill 	sn = (sn+NSECT-SDIST)%NSECT;
353264Sbill 
354266Sbill 	if (cn - upaddr->updc)
355266Sbill 		goto search;		/* Not on-cylinder */
356264Sbill 	csn = (upaddr->upla>>6) - sn - 1;
357266Sbill 	if (csn < 0)
358264Sbill 		csn += NSECT;
359266Sbill 	if (csn > NSECT-RDIST)
360264Sbill 		goto done;
361264Sbill 
362264Sbill search:
363264Sbill 	upaddr->updc = cn;
364264Sbill 	upaddr->upda = sn;
365264Sbill 	upaddr->upcs1 = IE|SEARCH|GO;
366*268Sbill 	didie = 1;
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 	}
375*268Sbill 	goto out;
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;
390*268Sbill 
391*268Sbill out:
392*268Sbill 	return (didie);
393264Sbill }
394264Sbill 
395264Sbill /*
396264Sbill  * Start a transfer; call from top level at spl5() or on interrupt.
397264Sbill  */
398264Sbill upstart()
399264Sbill {
400264Sbill 	register struct buf *bp, *dp;
401264Sbill 	register unit;
402264Sbill 	register struct device *upaddr;
403264Sbill 	daddr_t bn;
404266Sbill 	int dn, sn, tn, cn, cmd;
405264Sbill 
406264Sbill 	if (printsw&2) printf("upstart\n");
407264Sbill loop:
408266Sbill 	/*
409266Sbill 	 * Pick a drive off the queue of ready drives, and
410266Sbill 	 * perform the first transfer on its queue.
411266Sbill 	 *
412266Sbill 	 * Looping here is completely for the sake of drives which
413266Sbill 	 * are not present and on-line, for which we completely clear the
414266Sbill 	 * request queue.
415266Sbill 	 */
416264Sbill 	if ((dp = uptab.b_actf) == NULL)
417*268Sbill 		return (0);
418264Sbill 	if ((bp = dp->b_actf) == NULL) {
419264Sbill 		uptab.b_actf = dp->b_forw;
420264Sbill 		goto loop;
421264Sbill 	}
422266Sbill 	/*
423266Sbill 	 * Mark the controller busy, and multi-part disk address.
424266Sbill 	 * Select the unit on which the i/o is to take place.
425266Sbill 	 */
426264Sbill 	uptab.b_active++;
427264Sbill 	unit = minor(bp->b_dev) & 077;
428264Sbill 	dn = dkunit(bp);
429264Sbill 	bn = dkblock(bp);
430264Sbill 	cn = up_sizes[unit&07].cyloff;
431264Sbill 	cn += bn/(NSECT*NTRAC);
432264Sbill 	sn = bn%(NSECT*NTRAC);
433264Sbill 	tn = sn/NSECT;
434266Sbill 	sn %= NSECT;
435264Sbill 	upaddr = UPADDR;
436264Sbill 	if ((upaddr->upcs2 & 07) != dn) {
437264Sbill 		upaddr->upcs2 = dn;
438264Sbill 		DELAY(sdelay);
439264Sbill 		nwaitcs2++;
440264Sbill 	} else
441264Sbill 		neasycs2++;
442266Sbill 	up_ubinfo = ubasetup(bp, 1);	/* In a funny place for delay... */
443266Sbill 	/*
444266Sbill 	 * If drive is not present and on-line, then
445266Sbill 	 * get rid of this with an error and loop to get
446266Sbill 	 * rid of the rest of its queued requests.
447266Sbill 	 * (Then on to any other ready drives.)
448266Sbill 	 */
449264Sbill 	if ((upaddr->upds & (DPR|MOL)) != (DPR|MOL)) {
450264Sbill 		uptab.b_active = 0;
451264Sbill 		uptab.b_errcnt = 0;
452264Sbill 		dp->b_actf = bp->av_forw;
453266Sbill 		dp->b_active = 0;
454264Sbill 		bp->b_flags |= B_ERROR;
455264Sbill 		iodone(bp);
456266Sbill 		ubafree(up_ubinfo), up_ubinfo = 0;	/* A funny place ... */
457264Sbill 		goto loop;
458264Sbill 	}
459266Sbill 	/*
460266Sbill 	 * If this is a retry, then with the 16'th retry we
461266Sbill 	 * begin to try offsetting the heads to recover the data.
462266Sbill 	 */
463266Sbill 	if (uptab.b_errcnt >= 16) {
464264Sbill 		upaddr->upof = up_offset[uptab.b_errcnt & 017] | FMT22;
465266Sbill 		upaddr->upcs1 = IE|OFFSET|GO;
466264Sbill 		DELAY(idelay);
467266Sbill 		while (upaddr->upds & PIP)
468264Sbill 			DELAY(25);
469264Sbill 	}
470266Sbill 	/*
471266Sbill 	 * Now set up the transfer, retrieving the high
472266Sbill 	 * 2 bits of the UNIBUS address from the information
473266Sbill 	 * returned by ubasetup() for the cs1 register bits 8 and 9.
474266Sbill 	 */
475264Sbill 	upaddr->updc = cn;
476264Sbill 	upaddr->upda = (tn << 8) + sn;
477264Sbill 	upaddr->upba = up_ubinfo;
478264Sbill 	upaddr->upwc = -bp->b_bcount / sizeof (short);
479266Sbill 	cmd = (up_ubinfo >> 8) & 0x300;
480264Sbill 	if (bp->b_flags & B_READ)
481266Sbill 		cmd |= IE|RCOM|GO;
482264Sbill 	else
483266Sbill 		cmd |= IE|WCOM|GO;
484266Sbill 	upaddr->upcs1 = cmd;
485*268Sbill #ifdef notdef
486267Sbill 	if (csdel3) DELAY(csdel3);
487*268Sbill #endif
488266Sbill 	/*
489266Sbill 	 * This is a controller busy situation.
490266Sbill 	 * Record in dk slot NUP+DK_N (after last drive)
491266Sbill 	 * unless there aren't that many slots reserved for
492266Sbill 	 * us in which case we record this as a drive busy
493266Sbill 	 * (if there is room for that).
494266Sbill 	 */
495264Sbill 	unit = dn+DK_N;
496264Sbill 	if (NUP+DK_N == DK_NMAX)
497264Sbill 		unit = NUP+DK_N;
498264Sbill 	if (unit <= DK_NMAX) {
499264Sbill 		dk_busy |= 1<<unit;
500264Sbill 		dk_numb[unit]++;
501264Sbill 		dk_wds[unit] += bp->b_bcount>>6;
502264Sbill 	}
503*268Sbill 	return (1);
504264Sbill }
505264Sbill 
506264Sbill /*
507264Sbill  * Handle a device interrupt.
508264Sbill  *
509264Sbill  * If the transferring drive needs attention, service it
510264Sbill  * retrying on error or beginning next transfer.
511264Sbill  * Service all other ready drives, calling ustart to transfer
512264Sbill  * their blocks to the ready queue in uptab, and then restart
513264Sbill  * the controller if there is anything to do.
514264Sbill  */
515264Sbill upintr()
516264Sbill {
517264Sbill 	register struct buf *bp, *dp;
518264Sbill 	register unit;
519264Sbill 	register struct device *upaddr = UPADDR;
520264Sbill 	int as = upaddr->upas & 0377;
521*268Sbill 	int needie = 1;
522264Sbill 
523266Sbill 	if (printsw&4) printf("upintr as=%d act %d %d %d\n", as, uptab.b_active, uputab[0].b_active, uputab[1].b_active);
524266Sbill 	if (uptab.b_active) {
525266Sbill 		/*
526266Sbill 		 * The drive is transferring, thus the hardware
527266Sbill 		 * (say the designers) will only interrupt when the transfer
528266Sbill 		 * completes; check for it anyways.
529266Sbill 		 */
530266Sbill 		if ((upaddr->upcs1 & RDY) == 0) {
531267Sbill 			printf("!RDY in upintr: cs1 %o\n", upaddr->upcs1);
532267Sbill printf("as=%d act %d %d %d\n", as, uptab.b_active, uputab[0].b_active, uputab[1].b_active);
533267Sbill }
534266Sbill 		/*
535266Sbill 		 * Mark controller or drive not busy, and check for an
536266Sbill 		 * error condition which may have resulted from the transfer.
537266Sbill 		 */
538264Sbill 		dp = uptab.b_actf;
539264Sbill 		bp = dp->b_actf;
540264Sbill 		unit = dkunit(bp);
541264Sbill 		if (DK_N+NUP == DK_NMAX)
542264Sbill 			dk_busy &= ~(1<<(DK_N+NUP));
543264Sbill 		else if (DK_N+unit <= DK_NMAX)
544264Sbill 			dk_busy &= ~(1<<(DK_N+unit));
545264Sbill 		if (upaddr->upcs1 & TRE) {
546266Sbill 			/*
547266Sbill 			 * An error occurred, indeed.  Select this unit
548266Sbill 			 * to get at the drive status (a SEARCH may have
549266Sbill 			 * intervened to change the selected unit), and
550266Sbill 			 * wait for the command which caused the interrupt
551266Sbill 			 * to complete (DRY).
552266Sbill 			 *
553266Sbill 			 * WHY IS THE WAIT NECESSARY?
554266Sbill 			 */
555264Sbill 			if ((upaddr->upcs2 & 07) != unit) {
556264Sbill 				upaddr->upcs2 = unit;
557264Sbill 				DELAY(sdelay);
558264Sbill 				nwaitcs2++;
559264Sbill 			} else
560264Sbill 				neasycs2++;
561266Sbill 			while ((upaddr->upds & DRY) == 0)
562264Sbill 				DELAY(25);
563266Sbill 			/*
564266Sbill 			 * After 28 retries (16 w/o servo offsets, and then
565266Sbill 			 * 12 with servo offsets), or if we encountered
566266Sbill 			 * an error because the drive is write-protected,
567266Sbill 			 * give up.  Print an error message on the last 2
568266Sbill 			 * retries before a hard failure.
569266Sbill 			 */
570266Sbill 			if (++uptab.b_errcnt > 28 || upaddr->uper1&WLE)
571264Sbill 				bp->b_flags |= B_ERROR;
572264Sbill 			else
573266Sbill 				uptab.b_active = 0;	/* To force retry */
574266Sbill 			if (uptab.b_errcnt > 27)
575264Sbill 				deverror(bp, upaddr->upcs2, upaddr->uper1);
576266Sbill 			/*
577266Sbill 			 * If this was a correctible ECC error, let upecc
578266Sbill 			 * do the dirty work to correct it.  If upecc
579266Sbill 			 * starts another READ for the rest of the data
580266Sbill 			 * then it returns 1 (having set uptab.b_active).
581266Sbill 			 * Otherwise we are done and fall through to
582266Sbill 			 * finish up.
583266Sbill 			 */
584266Sbill 			if ((upaddr->uper1&(DCK|ECH))==DCK && upecc(upaddr, bp))
585266Sbill 				return;
586266Sbill 			/*
587266Sbill 			 * Clear the drive and, every 4 retries, recalibrate
588266Sbill 			 * to hopefully help clear up seek positioning problems.
589266Sbill 			 */
590264Sbill 			upaddr->upcs1 = TRE|IE|DCLR|GO;
591264Sbill 			DELAY(idelay);
592*268Sbill 			needie = 0;
593266Sbill 			if ((uptab.b_errcnt&07) == 4) {
594264Sbill 				upaddr->upcs1 = RECAL|GO|IE;
595264Sbill 				DELAY(idelay);
596264Sbill 				while(upaddr->upds & PIP)
597264Sbill 					DELAY(25);
598264Sbill 			}
599264Sbill 		}
600266Sbill 		/*
601266Sbill 		 * If we are still noted as active, then no
602266Sbill 		 * (further) retries are necessary.
603266Sbill 		 *
604266Sbill 		 * Make sure the correct unit is selected,
605266Sbill 		 * return it to centerline if necessary, and mark
606266Sbill 		 * this i/o complete, starting the next transfer
607266Sbill 		 * on this drive with the upustart routine (if any).
608266Sbill 		 */
609266Sbill 		if (uptab.b_active) {
610266Sbill 			if ((upaddr->upcs2 & 07) != unit) {
611266Sbill 				upaddr->upcs2 = unit;
612266Sbill 				DELAY(sdelay);
613266Sbill 				nwaitcs2++;
614266Sbill 			} else
615266Sbill 				neasycs2++;
616266Sbill 			if (uptab.b_errcnt >= 16) {
617266Sbill 				upaddr->upcs1 = RTC|GO|IE;
618264Sbill 				DELAY(idelay);
619266Sbill 				while (upaddr->upds & PIP)
620264Sbill 					DELAY(25);
621*268Sbill 				needie = 0;
622264Sbill 			}
623264Sbill 			uptab.b_active = 0;
624264Sbill 			uptab.b_errcnt = 0;
625264Sbill 			uptab.b_actf = dp->b_forw;
626264Sbill 			dp->b_active = 0;
627264Sbill 			dp->b_errcnt = 0;
628264Sbill 			dp->b_actf = bp->av_forw;
629266Sbill 			bp->b_resid = (-upaddr->upwc * sizeof(short));
630264Sbill 			iodone(bp);
631264Sbill 			if(dp->b_actf)
632*268Sbill 				if (upustart(unit))
633*268Sbill 					needie = 0;
634264Sbill 		}
635264Sbill 		as &= ~(1<<unit);
636264Sbill 		ubafree(up_ubinfo), up_ubinfo = 0;
637266Sbill 	}
638266Sbill #ifndef notdef
639266Sbill 	else {
640266Sbill 		if (printsw&64) printf("cs1 %o\n", upaddr->upcs1);
641264Sbill 		if (upaddr->upcs1 & TRE) {
642267Sbill 			printf("TRE in upintr: cs1 %o\n", upaddr->upcs1);
643267Sbill printf("as=%d act %d %d %d\n", as, uptab.b_active, uputab[0].b_active, uputab[1].b_active);
644264Sbill 			upaddr->upcs1 = TRE;
645264Sbill 			DELAY(idelay);
646266Sbill 			if (printsw&64) printf("after TRE cs1 %o\n", upaddr->upcs1);
647264Sbill 		}
648264Sbill 	}
649266Sbill #endif
650266Sbill 	/*
651266Sbill 	 * If we have a unit with an outstanding SEARCH,
652266Sbill 	 * and the hardware indicates the unit requires attention,
653266Sbill 	 * the bring the drive to the ready queue.
654266Sbill 	 * Finally, if the controller is not transferring
655266Sbill 	 * start it if any drives are now ready to transfer.
656266Sbill 	 */
657266Sbill 	for (unit = 0; unit < NUP; unit++)
658266Sbill 		if (as & (1<<unit))
659267Sbill 			if (uputab[unit].b_active == 1) {
660267Sbill 				upaddr->upas = 1<<unit;
661*268Sbill 				if (asdel) DELAY(asdel);
662*268Sbill 				if (upustart(unit))
663*268Sbill 					needie = 0;
664267Sbill 			} else {
665267Sbill 			printf("as in upintr: cs1 %o\n", upaddr->upcs1);
666267Sbill printf("as=%d act %d %d %d\n", as, uptab.b_active, uputab[0].b_active, uputab[1].b_active);
667266Sbill 				upaddr->upas = 1<<unit;
668266Sbill 				DELAY(1000);
669266Sbill 			}
670266Sbill 	if (uptab.b_actf && uptab.b_active == 0)
671*268Sbill 		if (upstart())
672*268Sbill 			needie = 0;
673266Sbill out:
674*268Sbill 	if (needie)
675266Sbill 		upaddr->upcs1 = IE;
676266Sbill 	if (printsw&128) printf("exit cs1 %o\n", upaddr->upcs1);
677264Sbill }
678264Sbill 
679264Sbill upread(dev)
680264Sbill {
681264Sbill 
682264Sbill 	physio(upstrategy, &rupbuf, dev, B_READ, minphys);
683264Sbill }
684264Sbill 
685264Sbill upwrite(dev)
686264Sbill {
687264Sbill 
688264Sbill 	physio(upstrategy, &rupbuf, dev, B_WRITE, minphys);
689264Sbill }
690264Sbill 
691266Sbill /*
692266Sbill  * Correct an ECC error, and restart the i/o to complete
693266Sbill  * the transfer if necessary.  This is quite complicated because
694266Sbill  * the transfer may be going to an odd memory address base and/or
695266Sbill  * across a page boundary.
696266Sbill  */
697264Sbill upecc(up, bp)
698264Sbill register struct device *up;
699264Sbill register struct buf *bp;
700264Sbill {
701264Sbill 	struct uba_regs *ubp = (struct uba_regs *)UBA0;
702266Sbill 	register int i;
703264Sbill 	caddr_t addr;
704266Sbill 	int reg, bit, byte, npf, mask, o, cmd, ubaddr;
705264Sbill 	int bn, cn, tn, sn;
706264Sbill 
707264Sbill 	if (printsw&8) printf("upecc\n");
708264Sbill 	/*
709266Sbill 	 * Npf is the number of sectors transferred before the sector
710266Sbill 	 * containing the ECC error, and reg is the UBA register
711266Sbill 	 * mapping (the first part of) the transfer.
712266Sbill 	 * O is offset within a memory page of the first byte transferred.
713264Sbill 	 */
714266Sbill 	npf = btop((up->upwc * sizeof(short)) + bp->b_bcount) - 1;
715266Sbill 	reg = btop(up_ubinfo&0x3ffff) + npf;
716264Sbill 	o = (int)bp->b_un.b_addr & PGOFSET;
717264Sbill 	printf("%D ", bp->b_blkno+npf);
718264Sbill 	prdev("ECC", bp->b_dev);
719264Sbill 	mask = up->upec2;
720264Sbill 	if (mask == 0) {
721266Sbill 		up->upof = FMT22;		/* == RTC ???? */
722264Sbill 		DELAY(idelay);
723264Sbill 		return (0);
724264Sbill 	}
725266Sbill 	/*
726266Sbill 	 * Flush the buffered data path, and compute the
727266Sbill 	 * byte and bit position of the error.  The variable i
728266Sbill 	 * is the byte offset in the transfer, the variable byte
729266Sbill 	 * is the offset from a page boundary in main memory.
730266Sbill 	 */
731266Sbill 	ubp->uba_dpr[(up_ubinfo>>28)&0x0f] |= BNE;
732266Sbill 	i = up->upec1 - 1;		/* -1 makes 0 origin */
733266Sbill 	bit = i&07;
734266Sbill 	i = (i&~07)>>3;
735264Sbill 	byte = i + o;
736266Sbill 	/*
737266Sbill 	 * Correct while possible bits remain of mask.  Since mask
738266Sbill 	 * contains 11 bits, we continue while the bit offset is > -11.
739266Sbill 	 * Also watch out for end of this block and the end of the whole
740266Sbill 	 * transfer.
741266Sbill 	 */
742266Sbill 	while (i < 512 && (int)ptob(npf)+i < bp->b_bcount && bit > -11) {
743266Sbill 		addr = ptob(ubp->uba_map[reg+btop(byte)].pg_pfnum)+
744266Sbill 		    (byte & PGOFSET);
745266Sbill 		putmemc(addr, getmemc(addr)^(mask<<bit));
746266Sbill 		byte++;
747266Sbill 		i++;
748266Sbill 		bit -= 8;
749264Sbill 	}
750266Sbill 	uptab.b_active++;	/* Either complete or continuing... */
751264Sbill 	if (up->upwc == 0)
752264Sbill 		return (0);
753266Sbill 	/*
754266Sbill 	 * Have to continue the transfer... clear the drive,
755266Sbill 	 * and compute the position where the transfer is to continue.
756266Sbill 	 * We have completed npf+1 sectors of the transfer already;
757266Sbill 	 * restart at offset o of next sector (i.e. in UBA register reg+1).
758266Sbill 	 */
759266Sbill 	up->upcs1 = TRE|IE|DCLR|GO;
760264Sbill 	DELAY(idelay);
761264Sbill 	bn = dkblock(bp);
762264Sbill 	cn = bp->b_cylin;
763266Sbill 	sn = bn%(NSECT*NTRAC) + npf + 1;
764264Sbill 	tn = sn/NSECT;
765264Sbill 	sn %= NSECT;
766266Sbill 	cn += tn/NTRAC;
767266Sbill 	tn %= NTRAC;
768264Sbill 	up->updc = cn;
769266Sbill 	up->upda = (tn << 8) | sn;
770266Sbill 	ubaddr = (int)ptob(reg+1) + o;
771266Sbill 	up->upba = ubaddr;
772266Sbill 	cmd = (ubaddr >> 8) & 0x300;
773266Sbill 	cmd |= IE|GO|RCOM;
774266Sbill 	up->upcs1 = cmd;
775264Sbill 	return (1);
776264Sbill }
777