xref: /csrg-svn/sys/vax/uba/up.c (revision 420)
1*420Sbill /*	up.c	3.20	07/29/80	*/
2264Sbill 
3264Sbill /*
4264Sbill  * Emulex UNIBUS disk driver with overlapped seeks and ECC recovery.
5264Sbill  *
6341Sbill  * NB: This driver works reliably only on an SC-11B controller with
7341Sbill  *     rev. level at least J (in particular rev. level H will not work well).
8367Sbill  *     If you have an newer controller you should change olducode below to:
9367Sbill  *		int	olducode = 0;
10367Sbill  *     which saves time by stalling less in the system.
11266Sbill  *
12341Sbill  * Controller switch settings:
13264Sbill  *	SW1-1	5/19 surfaces	(off, 19 surfaces on Ampex 9300)
14264Sbill  *	SW1-2	chksum enable	(off, checksum disabled)
15264Sbill  *	SW1-3	volume select	(off, 815 cylinders)
16264Sbill  *	SW1-4	sector select	(on, 32 sectors)
17264Sbill  *	SW1-5	unused		(off)
18264Sbill  *	SW1-6	port select	(on, single port)
19264Sbill  *	SW1-7	npr delay	(off, disable)
20264Sbill  *	SW1-8	ecc test mode	(off, disable)
21264Sbill  * and top mounted switches:
22264Sbill  *	SW2-1	extend opcodes	(off=open, disable)
23264Sbill  *	SW2-2	extend diag	(off=open, disable)
24341Sbill  *	SW2-3	4 wd dma burst	(on=closed, enable)
25264Sbill  *	SW2-4	unused		(off=open)
26264Sbill  */
27264Sbill 
28264Sbill #include "../h/param.h"
29264Sbill #include "../h/systm.h"
30308Sbill #include "../h/dk.h"
31264Sbill #include "../h/buf.h"
32264Sbill #include "../h/conf.h"
33264Sbill #include "../h/dir.h"
34264Sbill #include "../h/user.h"
35264Sbill #include "../h/map.h"
36*420Sbill #include "../h/pte.h"
37264Sbill #include "../h/mba.h"
38264Sbill #include "../h/mtpr.h"
39264Sbill #include "../h/uba.h"
40264Sbill #include "../h/vm.h"
41264Sbill 
42264Sbill /*
43264Sbill  * Define number of drives, and range of sampling information to be used.
44264Sbill  *
45264Sbill  * Normally, DK_N .. DK_N+NUP-1 gather individual drive stats,
46264Sbill  * and DK_N+NUP gathers controller transferring stats.
47264Sbill  *
48264Sbill  * If DK_N+NUP > DK_NMAX, then transfer stats are divided per drive.
49264Sbill  * If DK_NMAX is yet smaller, some drives are not monitored.
50264Sbill  */
51308Sbill #define	DK_N	2
52308Sbill #define	DK_NMAX	3
53264Sbill 
54264Sbill #define	ushort	unsigned short
55264Sbill 
56264Sbill struct	device
57264Sbill {
58264Sbill 	ushort	upcs1;		/* control and status register 1 */
59264Sbill 	short	upwc;		/* word count register */
60264Sbill 	ushort	upba;		/* UNIBUS address register */
61264Sbill 	ushort	upda;		/* desired address register */
62264Sbill 	ushort	upcs2;		/* control and status register 2 */
63264Sbill 	ushort	upds;		/* drive Status */
64264Sbill 	ushort	uper1;		/* error register 1 */
65264Sbill 	ushort	upas;		/* attention summary */
66264Sbill 	ushort	upla;		/* look ahead */
67264Sbill 	ushort	updb;		/* data buffer */
68264Sbill 	ushort	upmr;		/* maintenance */
69264Sbill 	ushort	updt;		/* drive type */
70264Sbill 	ushort	upsn;		/* serial number */
71264Sbill 	ushort	upof;		/* offset register */
72264Sbill 	ushort	updc;		/* desired cylinder address register */
73264Sbill 	ushort	upcc;		/* current cylinder */
74264Sbill 	ushort	uper2;		/* error register 2 */
75264Sbill 	ushort	uper3;		/* error register 3 */
76264Sbill 	ushort	upec1;		/* burst error bit position */
77264Sbill 	ushort	upec2;		/* burst error bit pattern */
78264Sbill };
79264Sbill 
80275Sbill /*
81275Sbill  * Software extension to the upas register, so we can
82275Sbill  * postpone starting SEARCH commands until the controller
83275Sbill  * is not transferring.
84275Sbill  */
85341Sbill int	upsoftas;
86275Sbill 
87275Sbill /*
88275Sbill  * If upseek then we don't issue SEARCH commands but rather just
89275Sbill  * settle for a SEEK to the correct cylinder.
90275Sbill  */
91275Sbill int	upseek;
92275Sbill 
93264Sbill #define	UPADDR	((struct device *)(UBA0_DEV + 0176700))
94264Sbill 
95264Sbill #define	NUP	2		/* Number of drives this installation */
96264Sbill 
97264Sbill #define	NSECT	32
98264Sbill #define	NTRAC	19
99264Sbill 
100264Sbill /*
101264Sbill  * Constants controlling on-cylinder SEARCH usage.
102264Sbill  *
103308Sbill  * 	upSDIST/2 msec		time needed to start transfer
104308Sbill  * 	upRDIST/2 msec		tolerable rotational latency when on-cylinder
105275Sbill  *
106308Sbill  * If we are no closer than upSDIST sectors and no further than upSDIST+upRDIST
107275Sbill  * and in the driver then we take it as it is.  Otherwise we do a SEARCH
108308Sbill  * requesting an interrupt upSDIST sectors in advance.
109264Sbill  */
110308Sbill #define	_upSDIST	6		/* 3.0 msec */
111308Sbill #define	_upRDIST	6		/* 3.0 msec */
112264Sbill 
113308Sbill int	upSDIST = _upSDIST;
114308Sbill int	upRDIST = _upRDIST;
115275Sbill 
116264Sbill /*
117264Sbill  * To fill a 300M drive:
118264Sbill  *	A is designed to be used as a root.
119264Sbill  *	B is suitable for a swap area.
120264Sbill  *	H is the primary storage area.
121264Sbill  * On systems with RP06'es, we normally use only 291346 blocks of the H
122264Sbill  * area, and use DEF or G to cover the rest of the drive.  The C system
123264Sbill  * covers the whole drive and can be used for pack-pack copying.
124264Sbill  */
125264Sbill struct	size
126264Sbill {
127264Sbill 	daddr_t	nblocks;
128264Sbill 	int	cyloff;
129264Sbill } up_sizes[8] = {
130264Sbill 	15884,	0,		/* A=cyl 0 thru 26 */
131264Sbill 	33440,	27,		/* B=cyl 27 thru 81 */
132341Sbill 	495520,	0,		/* C=cyl 0 thru 814 */
133264Sbill 	15884,	562,		/* D=cyl 562 thru 588 */
134264Sbill 	55936,	589,		/* E=cyl 589 thru 680 */
135264Sbill 	81472,	681,		/* F=cyl 681 thru 814 */
136264Sbill 	153824,	562,		/* G=cyl 562 thru 814 */
137264Sbill 	445664,	82,		/* H=cyl 82 thru 814 */
138264Sbill /* Later, and more safely for H area...
139264Sbill 	291346,	82,		/* H=cyl 82 thru 561 */
140264Sbill };
141264Sbill 
142264Sbill /*
143264Sbill  * The following defines are used in offset positioning
144264Sbill  * when trying to recover disk errors, with the constants being
145264Sbill  * +/- microinches.  Note that header compare inhibit (HCI) is not
146264Sbill  * tried (this makes sense only during read, in any case.)
147264Sbill  *
148341Sbill  * NOT ALL OF THESE ARE IMPLEMENTED ON 9300!?!
149264Sbill  */
150264Sbill #define	P400	020
151264Sbill #define	M400	0220
152264Sbill #define	P800	040
153264Sbill #define	M800	0240
154264Sbill #define	P1200	060
155264Sbill #define	M1200	0260
156264Sbill #define	HCI	020000
157264Sbill 
158264Sbill int	up_offset[16] =
159264Sbill {
160264Sbill 	P400, M400, P400, M400,
161264Sbill 	P800, M800, P800, M800,
162264Sbill 	P1200, M1200, P1200, M1200,
163264Sbill 	0, 0, 0, 0,
164264Sbill };
165264Sbill 
166264Sbill /*
167264Sbill  * Each drive has a table uputab[i].  On this table are sorted the
168264Sbill  * pending requests implementing an elevator algorithm (see dsort.c.)
169264Sbill  * In the upustart() routine, each drive is independently advanced
170264Sbill  * until it is on the desired cylinder for the next transfer and near
171264Sbill  * the desired sector.  The drive is then chained onto the uptab
172264Sbill  * table, and the transfer is initiated by the upstart() routine.
173264Sbill  * When the transfer is completed the driver reinvokes the upustart()
174264Sbill  * routine to set up the next transfer.
175264Sbill  */
176264Sbill struct	buf	uptab;
177264Sbill struct	buf	uputab[NUP];
178264Sbill 
179264Sbill struct	buf	rupbuf;			/* Buffer for raw i/o */
180264Sbill 
181264Sbill /* Drive commands, placed in upcs1 */
182264Sbill #define	GO	01		/* Go bit, set in all commands */
183264Sbill #define	PRESET	020		/* Preset drive at init or after errors */
184264Sbill #define	OFFSET	014		/* Offset heads to try to recover error */
185264Sbill #define	RTC	016		/* Return to center-line after OFFSET */
186264Sbill #define	SEARCH	030		/* Search for cylinder+sector */
187275Sbill #define	SEEK	04		/* Seek to cylinder */
188264Sbill #define	RECAL	06		/* Recalibrate, needed after seek error */
189264Sbill #define	DCLR	010		/* Drive clear, after error */
190264Sbill #define	WCOM	060		/* Write */
191264Sbill #define	RCOM	070		/* Read */
192264Sbill 
193264Sbill /* Other bits of upcs1 */
194264Sbill #define	IE	0100		/* Controller wide interrupt enable */
195264Sbill #define	TRE	040000		/* Transfer error */
196345Sbill #define	RDY	0200		/* Transfer terminated */
197264Sbill 
198264Sbill /* Drive status bits of upds */
199264Sbill #define	PIP	020000		/* Positioning in progress */
200264Sbill #define	ERR	040000		/* Error has occurred, DCLR necessary */
201264Sbill #define	VV	0100		/* Volume is valid, set by PRESET */
202264Sbill #define	DPR	0400		/* Drive has been preset */
203264Sbill #define	MOL	010000		/* Drive is online, heads loaded, etc */
204264Sbill #define	DRY	0200		/* Drive ready */
205264Sbill 
206313Sbill /* Bits of upcs2 */
207313Sbill #define	CLR	040		/* Controller clear */
208264Sbill /* Bits of uper1 */
209264Sbill #define	DCK	0100000		/* Ecc error occurred */
210264Sbill #define	ECH	0100		/* Ecc error was unrecoverable */
211264Sbill #define	WLE	04000		/* Attempt to write read-only drive */
212264Sbill 
213264Sbill /* Bits of upof; the offset bits above are also in this register */
214264Sbill #define	FMT22	010000		/* 16 bits/word, must be always set */
215264Sbill 
216264Sbill #define	b_cylin b_resid
217264Sbill 
218264Sbill int	up_ubinfo;		/* Information about UBA usage saved here */
219264Sbill /*
220264Sbill  * The EMULEX controller balks if accessed quickly after
221341Sbill  * certain operations.  With rev J delays seem to be needed only
222341Sbill  * when selecting a new unit, and in drive initialization type
223341Sbill  * like PRESET and DCLR.  The following variables control the delay
224341Sbill  * DELAY(n) is approximately n usec.
225264Sbill  */
226367Sbill int	olducode = 1;
227264Sbill int	idelay = 500;		/* Delay after PRESET or DCLR */
228367Sbill int	osdelay = 150;		/* Old delay after selecting drive in upcs2 */
229367Sbill int	ordelay = 100;		/* Old delay after SEARCH */
230367Sbill int	oasdel = 100;		/* Old delay after clearing bit in upas */
231367Sbill int	nsdelay = 25;
232264Sbill 
233264Sbill #define	DELAY(N)		{ register int d; d = N; while (--d > 0); }
234264Sbill 
235264Sbill int	nwaitcs2;		/* How many sdelay loops ? */
236264Sbill int	neasycs2;		/* How many sdelay loops not needed ? */
237264Sbill 
238313Sbill int	up_wticks;		/* Ticks waiting for interrupt */
239313Sbill int	upwstart;		/* Have started guardian */
240313Sbill int	upwatch();
241313Sbill 
242264Sbill #ifdef INTRLVE
243264Sbill daddr_t dkblock();
244264Sbill #endif
245264Sbill 
246264Sbill /*
247264Sbill  * Queue an i/o request for a drive, checking first that it is in range.
248264Sbill  *
249264Sbill  * A unit start is issued if the drive is inactive, causing
250264Sbill  * a SEARCH for the correct cylinder/sector.  If the drive is
251264Sbill  * already nearly on the money and the controller is not transferring
252264Sbill  * we kick it to start the transfer.
253264Sbill  */
254264Sbill upstrategy(bp)
255264Sbill register struct buf *bp;
256264Sbill {
257264Sbill 	register struct buf *dp;
258264Sbill 	register unit, xunit;
259264Sbill 	long sz, bn;
260264Sbill 
261313Sbill 	if (upwstart == 0) {
262313Sbill 		timeout((caddr_t)upwatch, 0, HZ);
263313Sbill 		upwstart++;
264313Sbill 	}
265264Sbill 	xunit = minor(bp->b_dev) & 077;
266264Sbill 	sz = bp->b_bcount;
267264Sbill 	sz = (sz+511) >> 9;		/* transfer size in 512 byte sectors */
268264Sbill 	unit = dkunit(bp);
269264Sbill 	if (unit >= NUP ||
270264Sbill 	    bp->b_blkno < 0 ||
271264Sbill 	    (bn = dkblock(bp))+sz > up_sizes[xunit&07].nblocks) {
272264Sbill 		bp->b_flags |= B_ERROR;
273264Sbill 		iodone(bp);
274264Sbill 		return;
275264Sbill 	}
276264Sbill 	bp->b_cylin = bn/(NSECT*NTRAC) + up_sizes[xunit&07].cyloff;
277264Sbill 	dp = &uputab[unit];
278264Sbill 	(void) spl5();
279264Sbill 	disksort(dp, bp);
280264Sbill 	if (dp->b_active == 0) {
281268Sbill 		(void) upustart(unit);
282264Sbill 		if (uptab.b_actf && uptab.b_active == 0)
283268Sbill 			(void) upstart();
284264Sbill 	}
285264Sbill 	(void) spl0();
286264Sbill }
287264Sbill 
288264Sbill /*
289264Sbill  * Start activity on specified drive; called when drive is inactive
290264Sbill  * and new transfer request arrives and also when upas indicates that
291264Sbill  * a SEARCH command is complete.
292264Sbill  */
293264Sbill upustart(unit)
294264Sbill register unit;
295264Sbill {
296264Sbill 	register struct buf *bp, *dp;
297264Sbill 	register struct device *upaddr = UPADDR;
298264Sbill 	daddr_t bn;
299264Sbill 	int sn, cn, csn;
300268Sbill 	int didie = 0;
301264Sbill 
302275Sbill 	/*
303275Sbill 	 * Other drivers tend to say something like
304275Sbill 	 *	upaddr->upcs1 = IE;
305275Sbill 	 *	upaddr->upas = 1<<unit;
306275Sbill 	 * here, but the SC-11B will cancel a command which
307275Sbill 	 * happens to be sitting in the cs1 if you clear the go
308275Sbill 	 * bit by storing there (so the first is not safe),
309275Sbill 	 * and it also does not like being bothered with operations
310275Sbill 	 * such as clearing upas when a transfer is active (as
311275Sbill 	 * it may well be.)
312275Sbill 	 *
313275Sbill 	 * Thus we keep careful track of when we re-enable IE
314275Sbill 	 * after an interrupt and do it only if we didn't issue
315275Sbill 	 * a command which re-enabled it as a matter of course.
316275Sbill 	 * We clear bits in upas in the interrupt routine, when
317275Sbill 	 * no transfers are active.
318275Sbill 	 */
319266Sbill 	if (unit >= NUP)
320268Sbill 		goto out;
321264Sbill 	if (unit+DK_N <= DK_NMAX)
322264Sbill 		dk_busy &= ~(1<<(unit+DK_N));
323264Sbill 	dp = &uputab[unit];
324266Sbill 	if ((bp = dp->b_actf) == NULL)
325268Sbill 		goto out;
326275Sbill 	/*
327275Sbill 	 * The SC-11B doesn't start SEARCH commands when transfers are
328275Sbill 	 * in progress.  In fact, it tends to get confused when given
329275Sbill 	 * SEARCH'es during transfers, generating interrupts with neither
330275Sbill 	 * RDY nor a bit in the upas register.  Thus we defer
331275Sbill 	 * until an interrupt when a transfer is pending.
332275Sbill 	 */
333275Sbill 	if (uptab.b_active) {
334341Sbill 		upsoftas |= 1<<unit;
335275Sbill 		return (0);
336275Sbill 	}
337276Sbill 	if (dp->b_active)
338276Sbill 		goto done;
339276Sbill 	dp->b_active = 1;
340264Sbill 	if ((upaddr->upcs2 & 07) != unit) {
341264Sbill 		upaddr->upcs2 = unit;
342367Sbill 		DELAY(olducode ? osdelay : nsdelay);
343264Sbill 		nwaitcs2++;
344264Sbill 	} else
345264Sbill 		neasycs2++;
346266Sbill 	/*
347266Sbill 	 * If we have changed packs or just initialized,
348275Sbill 	 * then the volume will not be valid; if so, clear
349266Sbill 	 * the drive, preset it and put in 16bit/word mode.
350266Sbill 	 */
351266Sbill 	if ((upaddr->upds & VV) == 0) {
352266Sbill 		upaddr->upcs1 = IE|DCLR|GO;
353266Sbill 		DELAY(idelay);
354264Sbill 		upaddr->upcs1 = IE|PRESET|GO;
355264Sbill 		DELAY(idelay);
356264Sbill 		upaddr->upof = FMT22;
357268Sbill 		didie = 1;
358264Sbill 	}
359264Sbill 	if ((upaddr->upds & (DPR|MOL)) != (DPR|MOL))
360275Sbill 		goto done;
361266Sbill 	/*
362266Sbill 	 * Do enough of the disk address decoding to determine
363266Sbill 	 * which cylinder and sector the request is on.
364266Sbill 	 * If we are on the correct cylinder and the desired sector
365308Sbill 	 * lies between upSDIST and upSDIST+upRDIST sectors ahead of us, then
366266Sbill 	 * we don't bother to SEARCH but just begin the transfer asap.
367308Sbill 	 * Otherwise ask for a interrupt upSDIST sectors ahead.
368266Sbill 	 */
369264Sbill 	bn = dkblock(bp);
370264Sbill 	cn = bp->b_cylin;
371264Sbill 	sn = bn%(NSECT*NTRAC);
372308Sbill 	sn = (sn+NSECT-upSDIST)%NSECT;
373264Sbill 
374266Sbill 	if (cn - upaddr->updc)
375266Sbill 		goto search;		/* Not on-cylinder */
376275Sbill 	else if (upseek)
377275Sbill 		goto done;		/* Ok just to be on-cylinder */
378264Sbill 	csn = (upaddr->upla>>6) - sn - 1;
379266Sbill 	if (csn < 0)
380264Sbill 		csn += NSECT;
381308Sbill 	if (csn > NSECT-upRDIST)
382264Sbill 		goto done;
383264Sbill 
384264Sbill search:
385264Sbill 	upaddr->updc = cn;
386275Sbill 	if (upseek)
387275Sbill 		upaddr->upcs1 = IE|SEEK|GO;
388275Sbill 	else {
389275Sbill 		upaddr->upda = sn;
390275Sbill 		upaddr->upcs1 = IE|SEARCH|GO;
391275Sbill 	}
392268Sbill 	didie = 1;
393266Sbill 	/*
394266Sbill 	 * Mark this unit busy.
395266Sbill 	 */
396264Sbill 	unit += DK_N;
397345Sbill 	if (unit <= DK_NMAX && DK_N+NUP <= DK_NMAX) {
398264Sbill 		dk_busy |= 1<<unit;
399264Sbill 		dk_numb[unit]++;
400264Sbill 	}
401367Sbill 	if (olducode)
402367Sbill 		DELAY(ordelay);
403268Sbill 	goto out;
404264Sbill 
405264Sbill done:
406266Sbill 	/*
407275Sbill 	 * This unit is ready to go so
408275Sbill 	 * link it onto the chain of ready disks.
409266Sbill 	 */
410264Sbill 	dp->b_forw = NULL;
411266Sbill 	if (uptab.b_actf == NULL)
412264Sbill 		uptab.b_actf = dp;
413264Sbill 	else
414264Sbill 		uptab.b_actl->b_forw = dp;
415264Sbill 	uptab.b_actl = dp;
416268Sbill 
417268Sbill out:
418268Sbill 	return (didie);
419264Sbill }
420264Sbill 
421264Sbill /*
422264Sbill  * Start a transfer; call from top level at spl5() or on interrupt.
423264Sbill  */
424264Sbill upstart()
425264Sbill {
426264Sbill 	register struct buf *bp, *dp;
427264Sbill 	register unit;
428264Sbill 	register struct device *upaddr;
429264Sbill 	daddr_t bn;
430266Sbill 	int dn, sn, tn, cn, cmd;
431264Sbill 
432264Sbill loop:
433266Sbill 	/*
434266Sbill 	 * Pick a drive off the queue of ready drives, and
435266Sbill 	 * perform the first transfer on its queue.
436266Sbill 	 *
437266Sbill 	 * Looping here is completely for the sake of drives which
438266Sbill 	 * are not present and on-line, for which we completely clear the
439266Sbill 	 * request queue.
440266Sbill 	 */
441273Sbill 	if ((dp = uptab.b_actf) == NULL)
442268Sbill 		return (0);
443264Sbill 	if ((bp = dp->b_actf) == NULL) {
444264Sbill 		uptab.b_actf = dp->b_forw;
445264Sbill 		goto loop;
446264Sbill 	}
447266Sbill 	/*
448266Sbill 	 * Mark the controller busy, and multi-part disk address.
449266Sbill 	 * Select the unit on which the i/o is to take place.
450266Sbill 	 */
451264Sbill 	uptab.b_active++;
452264Sbill 	unit = minor(bp->b_dev) & 077;
453264Sbill 	dn = dkunit(bp);
454264Sbill 	bn = dkblock(bp);
455264Sbill 	cn = up_sizes[unit&07].cyloff;
456264Sbill 	cn += bn/(NSECT*NTRAC);
457264Sbill 	sn = bn%(NSECT*NTRAC);
458264Sbill 	tn = sn/NSECT;
459266Sbill 	sn %= NSECT;
460264Sbill 	upaddr = UPADDR;
461264Sbill 	if ((upaddr->upcs2 & 07) != dn) {
462264Sbill 		upaddr->upcs2 = dn;
463275Sbill 		/* DELAY(sdelay);		Provided by ubasetup() */
464264Sbill 		nwaitcs2++;
465264Sbill 	} else
466264Sbill 		neasycs2++;
467275Sbill 	up_ubinfo = ubasetup(bp, 1);	/* Providing delay */
468266Sbill 	/*
469266Sbill 	 * If drive is not present and on-line, then
470266Sbill 	 * get rid of this with an error and loop to get
471266Sbill 	 * rid of the rest of its queued requests.
472266Sbill 	 * (Then on to any other ready drives.)
473266Sbill 	 */
474264Sbill 	if ((upaddr->upds & (DPR|MOL)) != (DPR|MOL)) {
475275Sbill 		printf("!DPR || !MOL, unit %d, ds %o\n", dn, upaddr->upds);
476264Sbill 		uptab.b_active = 0;
477264Sbill 		uptab.b_errcnt = 0;
478264Sbill 		dp->b_actf = bp->av_forw;
479266Sbill 		dp->b_active = 0;
480264Sbill 		bp->b_flags |= B_ERROR;
481264Sbill 		iodone(bp);
482266Sbill 		ubafree(up_ubinfo), up_ubinfo = 0;	/* A funny place ... */
483264Sbill 		goto loop;
484264Sbill 	}
485266Sbill 	/*
486266Sbill 	 * If this is a retry, then with the 16'th retry we
487266Sbill 	 * begin to try offsetting the heads to recover the data.
488266Sbill 	 */
489266Sbill 	if (uptab.b_errcnt >= 16) {
490264Sbill 		upaddr->upof = up_offset[uptab.b_errcnt & 017] | FMT22;
491266Sbill 		upaddr->upcs1 = IE|OFFSET|GO;
492264Sbill 		DELAY(idelay);
493266Sbill 		while (upaddr->upds & PIP)
494264Sbill 			DELAY(25);
495264Sbill 	}
496266Sbill 	/*
497266Sbill 	 * Now set up the transfer, retrieving the high
498266Sbill 	 * 2 bits of the UNIBUS address from the information
499266Sbill 	 * returned by ubasetup() for the cs1 register bits 8 and 9.
500266Sbill 	 */
501264Sbill 	upaddr->updc = cn;
502264Sbill 	upaddr->upda = (tn << 8) + sn;
503264Sbill 	upaddr->upba = up_ubinfo;
504264Sbill 	upaddr->upwc = -bp->b_bcount / sizeof (short);
505266Sbill 	cmd = (up_ubinfo >> 8) & 0x300;
506264Sbill 	if (bp->b_flags & B_READ)
507266Sbill 		cmd |= IE|RCOM|GO;
508264Sbill 	else
509266Sbill 		cmd |= IE|WCOM|GO;
510266Sbill 	upaddr->upcs1 = cmd;
511266Sbill 	/*
512266Sbill 	 * This is a controller busy situation.
513266Sbill 	 * Record in dk slot NUP+DK_N (after last drive)
514266Sbill 	 * unless there aren't that many slots reserved for
515266Sbill 	 * us in which case we record this as a drive busy
516266Sbill 	 * (if there is room for that).
517266Sbill 	 */
518264Sbill 	unit = dn+DK_N;
519264Sbill 	if (NUP+DK_N == DK_NMAX)
520264Sbill 		unit = NUP+DK_N;
521264Sbill 	if (unit <= DK_NMAX) {
522264Sbill 		dk_busy |= 1<<unit;
523264Sbill 		dk_numb[unit]++;
524264Sbill 		dk_wds[unit] += bp->b_bcount>>6;
525264Sbill 	}
526268Sbill 	return (1);
527264Sbill }
528264Sbill 
529264Sbill /*
530264Sbill  * Handle a device interrupt.
531264Sbill  *
532264Sbill  * If the transferring drive needs attention, service it
533264Sbill  * retrying on error or beginning next transfer.
534264Sbill  * Service all other ready drives, calling ustart to transfer
535264Sbill  * their blocks to the ready queue in uptab, and then restart
536264Sbill  * the controller if there is anything to do.
537264Sbill  */
538264Sbill upintr()
539264Sbill {
540264Sbill 	register struct buf *bp, *dp;
541264Sbill 	register unit;
542264Sbill 	register struct device *upaddr = UPADDR;
543264Sbill 	int as = upaddr->upas & 0377;
544341Sbill 	int oupsoftas;
545268Sbill 	int needie = 1;
546264Sbill 
547276Sbill 	(void) spl6();
548313Sbill 	up_wticks = 0;
549266Sbill 	if (uptab.b_active) {
550266Sbill 		/*
551266Sbill 		 * The drive is transferring, thus the hardware
552266Sbill 		 * (say the designers) will only interrupt when the transfer
553266Sbill 		 * completes; check for it anyways.
554266Sbill 		 */
555266Sbill 		if ((upaddr->upcs1 & RDY) == 0) {
556272Sbill 			printf("!RDY: cs1 %o, ds %o, wc %d\n", upaddr->upcs1,
557272Sbill 			    upaddr->upds, upaddr->upwc);
558341Sbill 			printf("as=%d act %d %d %d\n", as, uptab.b_active,
559341Sbill 			    uputab[0].b_active, uputab[1].b_active);
560269Sbill 		}
561266Sbill 		/*
562266Sbill 		 * Mark controller or drive not busy, and check for an
563266Sbill 		 * error condition which may have resulted from the transfer.
564266Sbill 		 */
565264Sbill 		dp = uptab.b_actf;
566264Sbill 		bp = dp->b_actf;
567264Sbill 		unit = dkunit(bp);
568264Sbill 		if (DK_N+NUP == DK_NMAX)
569264Sbill 			dk_busy &= ~(1<<(DK_N+NUP));
570264Sbill 		else if (DK_N+unit <= DK_NMAX)
571264Sbill 			dk_busy &= ~(1<<(DK_N+unit));
572275Sbill 		if ((upaddr->upcs2 & 07) != unit) {
573275Sbill 			upaddr->upcs2 = unit;
574367Sbill 			DELAY(olducode ? osdelay : nsdelay);
575275Sbill 			nwaitcs2++;
576275Sbill 		} else
577275Sbill 			neasycs2++;
578275Sbill 		if (upaddr->upds & ERR) {
579266Sbill 			/*
580266Sbill 			 * An error occurred, indeed.  Select this unit
581266Sbill 			 * to get at the drive status (a SEARCH may have
582266Sbill 			 * intervened to change the selected unit), and
583266Sbill 			 * wait for the command which caused the interrupt
584266Sbill 			 * to complete (DRY).
585266Sbill 			 */
586266Sbill 			while ((upaddr->upds & DRY) == 0)
587264Sbill 				DELAY(25);
588266Sbill 			/*
589266Sbill 			 * After 28 retries (16 w/o servo offsets, and then
590266Sbill 			 * 12 with servo offsets), or if we encountered
591266Sbill 			 * an error because the drive is write-protected,
592266Sbill 			 * give up.  Print an error message on the last 2
593266Sbill 			 * retries before a hard failure.
594266Sbill 			 */
595266Sbill 			if (++uptab.b_errcnt > 28 || upaddr->uper1&WLE)
596264Sbill 				bp->b_flags |= B_ERROR;
597264Sbill 			else
598266Sbill 				uptab.b_active = 0;	/* To force retry */
599266Sbill 			if (uptab.b_errcnt > 27)
600264Sbill 				deverror(bp, upaddr->upcs2, upaddr->uper1);
601266Sbill 			/*
602266Sbill 			 * If this was a correctible ECC error, let upecc
603266Sbill 			 * do the dirty work to correct it.  If upecc
604266Sbill 			 * starts another READ for the rest of the data
605266Sbill 			 * then it returns 1 (having set uptab.b_active).
606266Sbill 			 * Otherwise we are done and fall through to
607266Sbill 			 * finish up.
608266Sbill 			 */
609266Sbill 			if ((upaddr->uper1&(DCK|ECH))==DCK && upecc(upaddr, bp))
610266Sbill 				return;
611266Sbill 			/*
612266Sbill 			 * Clear the drive and, every 4 retries, recalibrate
613266Sbill 			 * to hopefully help clear up seek positioning problems.
614266Sbill 			 */
615264Sbill 			upaddr->upcs1 = TRE|IE|DCLR|GO;
616264Sbill 			DELAY(idelay);
617268Sbill 			needie = 0;
618266Sbill 			if ((uptab.b_errcnt&07) == 4) {
619264Sbill 				upaddr->upcs1 = RECAL|GO|IE;
620264Sbill 				DELAY(idelay);
621264Sbill 				while(upaddr->upds & PIP)
622264Sbill 					DELAY(25);
623264Sbill 			}
624264Sbill 		}
625266Sbill 		/*
626266Sbill 		 * If we are still noted as active, then no
627266Sbill 		 * (further) retries are necessary.
628266Sbill 		 *
629266Sbill 		 * Make sure the correct unit is selected,
630266Sbill 		 * return it to centerline if necessary, and mark
631266Sbill 		 * this i/o complete, starting the next transfer
632266Sbill 		 * on this drive with the upustart routine (if any).
633266Sbill 		 */
634266Sbill 		if (uptab.b_active) {
635266Sbill 			if (uptab.b_errcnt >= 16) {
636266Sbill 				upaddr->upcs1 = RTC|GO|IE;
637264Sbill 				DELAY(idelay);
638266Sbill 				while (upaddr->upds & PIP)
639264Sbill 					DELAY(25);
640268Sbill 				needie = 0;
641264Sbill 			}
642264Sbill 			uptab.b_active = 0;
643264Sbill 			uptab.b_errcnt = 0;
644264Sbill 			uptab.b_actf = dp->b_forw;
645264Sbill 			dp->b_active = 0;
646264Sbill 			dp->b_errcnt = 0;
647264Sbill 			dp->b_actf = bp->av_forw;
648266Sbill 			bp->b_resid = (-upaddr->upwc * sizeof(short));
649275Sbill 			if (bp->b_resid)
650341Sbill 				printf("resid %d ds %o er? %o %o %o\n",
651341Sbill 				    bp->b_resid, upaddr->upds,
652275Sbill 				    upaddr->uper1, upaddr->uper2, upaddr->uper3);
653264Sbill 			iodone(bp);
654264Sbill 			if(dp->b_actf)
655268Sbill 				if (upustart(unit))
656268Sbill 					needie = 0;
657264Sbill 		}
658264Sbill 		as &= ~(1<<unit);
659341Sbill 		upsoftas &= ~(1<<unit);
660264Sbill 		ubafree(up_ubinfo), up_ubinfo = 0;
661273Sbill 	} else {
662264Sbill 		if (upaddr->upcs1 & TRE) {
663264Sbill 			upaddr->upcs1 = TRE;
664264Sbill 			DELAY(idelay);
665264Sbill 		}
666264Sbill 	}
667266Sbill 	/*
668266Sbill 	 * If we have a unit with an outstanding SEARCH,
669266Sbill 	 * and the hardware indicates the unit requires attention,
670266Sbill 	 * the bring the drive to the ready queue.
671266Sbill 	 * Finally, if the controller is not transferring
672266Sbill 	 * start it if any drives are now ready to transfer.
673266Sbill 	 */
674341Sbill 	as |= upsoftas;
675341Sbill 	oupsoftas = upsoftas;
676341Sbill 	upsoftas = 0;
677266Sbill 	for (unit = 0; unit < NUP; unit++)
678341Sbill 		if ((as|oupsoftas) & (1<<unit)) {
679273Sbill 			if (as & (1<<unit)) {
680267Sbill 				upaddr->upas = 1<<unit;
681367Sbill 				if (olducode)
682367Sbill 					DELAY(oasdel);
683272Sbill 			}
684273Sbill 			if (upustart(unit))
685273Sbill 				needie = 0;
686273Sbill 		}
687266Sbill 	if (uptab.b_actf && uptab.b_active == 0)
688268Sbill 		if (upstart())
689268Sbill 			needie = 0;
690266Sbill out:
691275Sbill 	if (needie)
692266Sbill 		upaddr->upcs1 = IE;
693264Sbill }
694264Sbill 
695264Sbill upread(dev)
696264Sbill {
697264Sbill 
698264Sbill 	physio(upstrategy, &rupbuf, dev, B_READ, minphys);
699264Sbill }
700264Sbill 
701264Sbill upwrite(dev)
702264Sbill {
703264Sbill 
704264Sbill 	physio(upstrategy, &rupbuf, dev, B_WRITE, minphys);
705264Sbill }
706264Sbill 
707266Sbill /*
708266Sbill  * Correct an ECC error, and restart the i/o to complete
709266Sbill  * the transfer if necessary.  This is quite complicated because
710266Sbill  * the transfer may be going to an odd memory address base and/or
711266Sbill  * across a page boundary.
712266Sbill  */
713264Sbill upecc(up, bp)
714264Sbill register struct device *up;
715264Sbill register struct buf *bp;
716264Sbill {
717264Sbill 	struct uba_regs *ubp = (struct uba_regs *)UBA0;
718266Sbill 	register int i;
719264Sbill 	caddr_t addr;
720266Sbill 	int reg, bit, byte, npf, mask, o, cmd, ubaddr;
721264Sbill 	int bn, cn, tn, sn;
722264Sbill 
723264Sbill 	/*
724266Sbill 	 * Npf is the number of sectors transferred before the sector
725266Sbill 	 * containing the ECC error, and reg is the UBA register
726266Sbill 	 * mapping (the first part of) the transfer.
727266Sbill 	 * O is offset within a memory page of the first byte transferred.
728264Sbill 	 */
729266Sbill 	npf = btop((up->upwc * sizeof(short)) + bp->b_bcount) - 1;
730266Sbill 	reg = btop(up_ubinfo&0x3ffff) + npf;
731264Sbill 	o = (int)bp->b_un.b_addr & PGOFSET;
732264Sbill 	printf("%D ", bp->b_blkno+npf);
733264Sbill 	prdev("ECC", bp->b_dev);
734264Sbill 	mask = up->upec2;
735264Sbill 	if (mask == 0) {
736266Sbill 		up->upof = FMT22;		/* == RTC ???? */
737264Sbill 		DELAY(idelay);
738264Sbill 		return (0);
739264Sbill 	}
740266Sbill 	/*
741266Sbill 	 * Flush the buffered data path, and compute the
742266Sbill 	 * byte and bit position of the error.  The variable i
743266Sbill 	 * is the byte offset in the transfer, the variable byte
744266Sbill 	 * is the offset from a page boundary in main memory.
745266Sbill 	 */
746266Sbill 	ubp->uba_dpr[(up_ubinfo>>28)&0x0f] |= BNE;
747266Sbill 	i = up->upec1 - 1;		/* -1 makes 0 origin */
748266Sbill 	bit = i&07;
749266Sbill 	i = (i&~07)>>3;
750264Sbill 	byte = i + o;
751266Sbill 	/*
752266Sbill 	 * Correct while possible bits remain of mask.  Since mask
753266Sbill 	 * contains 11 bits, we continue while the bit offset is > -11.
754266Sbill 	 * Also watch out for end of this block and the end of the whole
755266Sbill 	 * transfer.
756266Sbill 	 */
757266Sbill 	while (i < 512 && (int)ptob(npf)+i < bp->b_bcount && bit > -11) {
758266Sbill 		addr = ptob(ubp->uba_map[reg+btop(byte)].pg_pfnum)+
759266Sbill 		    (byte & PGOFSET);
760266Sbill 		putmemc(addr, getmemc(addr)^(mask<<bit));
761266Sbill 		byte++;
762266Sbill 		i++;
763266Sbill 		bit -= 8;
764264Sbill 	}
765266Sbill 	uptab.b_active++;	/* Either complete or continuing... */
766264Sbill 	if (up->upwc == 0)
767264Sbill 		return (0);
768266Sbill 	/*
769266Sbill 	 * Have to continue the transfer... clear the drive,
770266Sbill 	 * and compute the position where the transfer is to continue.
771266Sbill 	 * We have completed npf+1 sectors of the transfer already;
772266Sbill 	 * restart at offset o of next sector (i.e. in UBA register reg+1).
773266Sbill 	 */
774266Sbill 	up->upcs1 = TRE|IE|DCLR|GO;
775264Sbill 	DELAY(idelay);
776264Sbill 	bn = dkblock(bp);
777264Sbill 	cn = bp->b_cylin;
778266Sbill 	sn = bn%(NSECT*NTRAC) + npf + 1;
779264Sbill 	tn = sn/NSECT;
780264Sbill 	sn %= NSECT;
781266Sbill 	cn += tn/NTRAC;
782266Sbill 	tn %= NTRAC;
783264Sbill 	up->updc = cn;
784266Sbill 	up->upda = (tn << 8) | sn;
785266Sbill 	ubaddr = (int)ptob(reg+1) + o;
786266Sbill 	up->upba = ubaddr;
787266Sbill 	cmd = (ubaddr >> 8) & 0x300;
788266Sbill 	cmd |= IE|GO|RCOM;
789266Sbill 	up->upcs1 = cmd;
790264Sbill 	return (1);
791264Sbill }
792286Sbill 
793286Sbill /*
794286Sbill  * Reset driver after UBA init.
795286Sbill  * Cancel software state of all pending transfers
796286Sbill  * and restart all units and the controller.
797286Sbill  */
798286Sbill upreset()
799286Sbill {
800286Sbill 	int unit;
801286Sbill 
802286Sbill 	printf(" up");
803286Sbill 	uptab.b_active = 0;
804286Sbill 	uptab.b_actf = uptab.b_actl = 0;
805286Sbill 	if (DK_N+NUP == DK_NMAX)
806286Sbill 		dk_busy &= ~(1<<(DK_N+NUP));
807286Sbill 	if (up_ubinfo) {
808286Sbill 		printf("<%d>", (up_ubinfo>>28)&0xf);
809286Sbill 		ubafree(up_ubinfo), up_ubinfo = 0;
810286Sbill 	}
811313Sbill 	UPADDR->upcs2 = CLR;		/* clear controller */
812313Sbill 	DELAY(idelay);
813286Sbill 	for (unit = 0; unit < NUP; unit++) {
814286Sbill 		uputab[unit].b_active = 0;
815286Sbill 		(void) upustart(unit);
816286Sbill 	}
817286Sbill 	(void) upstart();
818286Sbill }
819313Sbill 
820313Sbill /*
821313Sbill  * Wake up every second and if an interrupt is pending
822313Sbill  * but nothing has happened increment a counter.
823313Sbill  * If nothing happens for 20 seconds, reset the controller
824313Sbill  * and begin anew.
825313Sbill  */
826313Sbill upwatch()
827313Sbill {
828313Sbill 	int i;
829313Sbill 
830313Sbill 	timeout((caddr_t)upwatch, 0, HZ);
831313Sbill 	if (uptab.b_active == 0) {
832313Sbill 		for (i = 0; i < NUP; i++)
833313Sbill 			if (uputab[i].b_active)
834313Sbill 				goto active;
835313Sbill 		up_wticks = 0;		/* idling */
836313Sbill 		return;
837313Sbill 	}
838313Sbill active:
839313Sbill 	up_wticks++;
840313Sbill 	if (up_wticks >= 20) {
841313Sbill 		up_wticks = 0;
842313Sbill 		printf("LOST INTERRUPT RESET");
843313Sbill 		upreset();
844313Sbill 		printf("\n");
845313Sbill 	}
846313Sbill }
847