xref: /csrg-svn/sys/vax/uba/up.c (revision 893)
1*893Sbill /*	up.c	3.22	09/20/80	*/
2264Sbill 
3264Sbill /*
4885Sbill  * UNIBUS disk driver with overlapped seeks and ECC recovery.
5264Sbill  *
6885Sbill  * This driver works marginally on an Emulex SC-11B controller with rev
7885Sbill  * level J microcode, defining:
8885Sbill  *	int	olducode = 1;
9885Sbill  * to force CPU stalling delays.
10266Sbill  *
11885Sbill  * It has worked with no delays and no problems on a prototype
12885Sbill  * SC-21 controller.  Emulex intends to upgrade all SC-11s on VAXes to SC-21s.
13885Sbill  * You should get a SC-21 to replace any SC-11 on a VAX.
14885Sbill  *
15885Sbill  * SC-11B Controller 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)
27341Sbill  *	SW2-3	4 wd dma burst	(on=closed, enable)
28264Sbill  *	SW2-4	unused		(off=open)
29264Sbill  */
30264Sbill 
31264Sbill #include "../h/param.h"
32264Sbill #include "../h/systm.h"
33308Sbill #include "../h/dk.h"
34264Sbill #include "../h/buf.h"
35264Sbill #include "../h/conf.h"
36264Sbill #include "../h/dir.h"
37264Sbill #include "../h/user.h"
38264Sbill #include "../h/map.h"
39420Sbill #include "../h/pte.h"
40264Sbill #include "../h/mba.h"
41264Sbill #include "../h/mtpr.h"
42264Sbill #include "../h/uba.h"
43264Sbill #include "../h/vm.h"
44264Sbill 
45264Sbill /*
46264Sbill  * Define number of drives, and range of sampling information to be used.
47264Sbill  *
48264Sbill  * Normally, DK_N .. DK_N+NUP-1 gather individual drive stats,
49264Sbill  * and DK_N+NUP gathers controller transferring stats.
50264Sbill  *
51264Sbill  * If DK_N+NUP > DK_NMAX, then transfer stats are divided per drive.
52264Sbill  * If DK_NMAX is yet smaller, some drives are not monitored.
53264Sbill  */
54308Sbill #define	DK_N	2
55308Sbill #define	DK_NMAX	3
56264Sbill 
57264Sbill #define	ushort	unsigned short
58264Sbill 
59264Sbill struct	device
60264Sbill {
61264Sbill 	ushort	upcs1;		/* control and status register 1 */
62264Sbill 	short	upwc;		/* word count register */
63264Sbill 	ushort	upba;		/* UNIBUS address register */
64264Sbill 	ushort	upda;		/* desired address register */
65264Sbill 	ushort	upcs2;		/* control and status register 2 */
66264Sbill 	ushort	upds;		/* drive Status */
67264Sbill 	ushort	uper1;		/* error register 1 */
68264Sbill 	ushort	upas;		/* attention summary */
69264Sbill 	ushort	upla;		/* look ahead */
70264Sbill 	ushort	updb;		/* data buffer */
71264Sbill 	ushort	upmr;		/* maintenance */
72264Sbill 	ushort	updt;		/* drive type */
73264Sbill 	ushort	upsn;		/* serial number */
74264Sbill 	ushort	upof;		/* offset register */
75264Sbill 	ushort	updc;		/* desired cylinder address register */
76264Sbill 	ushort	upcc;		/* current cylinder */
77264Sbill 	ushort	uper2;		/* error register 2 */
78264Sbill 	ushort	uper3;		/* error register 3 */
79264Sbill 	ushort	upec1;		/* burst error bit position */
80264Sbill 	ushort	upec2;		/* burst error bit pattern */
81264Sbill };
82264Sbill 
83275Sbill /*
84275Sbill  * Software extension to the upas register, so we can
85275Sbill  * postpone starting SEARCH commands until the controller
86275Sbill  * is not transferring.
87275Sbill  */
88341Sbill int	upsoftas;
89275Sbill 
90275Sbill /*
91275Sbill  * If upseek then we don't issue SEARCH commands but rather just
92275Sbill  * settle for a SEEK to the correct cylinder.
93275Sbill  */
94275Sbill int	upseek;
95275Sbill 
96264Sbill #define	UPADDR	((struct device *)(UBA0_DEV + 0176700))
97264Sbill 
98264Sbill #define	NUP	2		/* Number of drives this installation */
99264Sbill 
100264Sbill #define	NSECT	32
101264Sbill #define	NTRAC	19
102264Sbill 
103264Sbill /*
104264Sbill  * Constants controlling on-cylinder SEARCH usage.
105264Sbill  *
106308Sbill  * 	upSDIST/2 msec		time needed to start transfer
107308Sbill  * 	upRDIST/2 msec		tolerable rotational latency when on-cylinder
108275Sbill  *
109308Sbill  * If we are no closer than upSDIST sectors and no further than upSDIST+upRDIST
110275Sbill  * and in the driver then we take it as it is.  Otherwise we do a SEARCH
111308Sbill  * requesting an interrupt upSDIST sectors in advance.
112264Sbill  */
113308Sbill #define	_upSDIST	6		/* 3.0 msec */
114308Sbill #define	_upRDIST	6		/* 3.0 msec */
115264Sbill 
116308Sbill int	upSDIST = _upSDIST;
117308Sbill int	upRDIST = _upRDIST;
118275Sbill 
119264Sbill /*
120264Sbill  * To fill a 300M drive:
121264Sbill  *	A is designed to be used as a root.
122264Sbill  *	B is suitable for a swap area.
123264Sbill  *	H is the primary storage area.
124264Sbill  * On systems with RP06'es, we normally use only 291346 blocks of the H
125264Sbill  * area, and use DEF or G to cover the rest of the drive.  The C system
126264Sbill  * covers the whole drive and can be used for pack-pack copying.
127264Sbill  */
128264Sbill struct	size
129264Sbill {
130264Sbill 	daddr_t	nblocks;
131264Sbill 	int	cyloff;
132264Sbill } up_sizes[8] = {
133264Sbill 	15884,	0,		/* A=cyl 0 thru 26 */
134264Sbill 	33440,	27,		/* B=cyl 27 thru 81 */
135341Sbill 	495520,	0,		/* C=cyl 0 thru 814 */
136264Sbill 	15884,	562,		/* D=cyl 562 thru 588 */
137264Sbill 	55936,	589,		/* E=cyl 589 thru 680 */
138264Sbill 	81472,	681,		/* F=cyl 681 thru 814 */
139264Sbill 	153824,	562,		/* G=cyl 562 thru 814 */
140264Sbill 	445664,	82,		/* H=cyl 82 thru 814 */
141264Sbill /* Later, and more safely for H area...
142264Sbill 	291346,	82,		/* H=cyl 82 thru 561 */
143264Sbill };
144264Sbill 
145264Sbill /*
146264Sbill  * The following defines are used in offset positioning
147264Sbill  * when trying to recover disk errors, with the constants being
148264Sbill  * +/- microinches.  Note that header compare inhibit (HCI) is not
149264Sbill  * tried (this makes sense only during read, in any case.)
150264Sbill  *
151341Sbill  * NOT ALL OF THESE ARE IMPLEMENTED ON 9300!?!
152264Sbill  */
153264Sbill #define	P400	020
154264Sbill #define	M400	0220
155264Sbill #define	P800	040
156264Sbill #define	M800	0240
157264Sbill #define	P1200	060
158264Sbill #define	M1200	0260
159264Sbill #define	HCI	020000
160264Sbill 
161264Sbill int	up_offset[16] =
162264Sbill {
163264Sbill 	P400, M400, P400, M400,
164264Sbill 	P800, M800, P800, M800,
165264Sbill 	P1200, M1200, P1200, M1200,
166264Sbill 	0, 0, 0, 0,
167264Sbill };
168264Sbill 
169264Sbill /*
170264Sbill  * Each drive has a table uputab[i].  On this table are sorted the
171264Sbill  * pending requests implementing an elevator algorithm (see dsort.c.)
172264Sbill  * In the upustart() routine, each drive is independently advanced
173264Sbill  * until it is on the desired cylinder for the next transfer and near
174264Sbill  * the desired sector.  The drive is then chained onto the uptab
175264Sbill  * table, and the transfer is initiated by the upstart() routine.
176264Sbill  * When the transfer is completed the driver reinvokes the upustart()
177264Sbill  * routine to set up the next transfer.
178264Sbill  */
179264Sbill struct	buf	uptab;
180264Sbill struct	buf	uputab[NUP];
181264Sbill 
182264Sbill struct	buf	rupbuf;			/* Buffer for raw i/o */
183264Sbill 
184264Sbill /* Drive commands, placed in upcs1 */
185264Sbill #define	GO	01		/* Go bit, set in all commands */
186264Sbill #define	PRESET	020		/* Preset drive at init or after errors */
187264Sbill #define	OFFSET	014		/* Offset heads to try to recover error */
188264Sbill #define	RTC	016		/* Return to center-line after OFFSET */
189264Sbill #define	SEARCH	030		/* Search for cylinder+sector */
190275Sbill #define	SEEK	04		/* Seek to cylinder */
191264Sbill #define	RECAL	06		/* Recalibrate, needed after seek error */
192264Sbill #define	DCLR	010		/* Drive clear, after error */
193264Sbill #define	WCOM	060		/* Write */
194264Sbill #define	RCOM	070		/* Read */
195264Sbill 
196264Sbill /* Other bits of upcs1 */
197264Sbill #define	IE	0100		/* Controller wide interrupt enable */
198264Sbill #define	TRE	040000		/* Transfer error */
199345Sbill #define	RDY	0200		/* Transfer terminated */
200264Sbill 
201264Sbill /* Drive status bits of upds */
202264Sbill #define	PIP	020000		/* Positioning in progress */
203264Sbill #define	ERR	040000		/* Error has occurred, DCLR necessary */
204264Sbill #define	VV	0100		/* Volume is valid, set by PRESET */
205264Sbill #define	DPR	0400		/* Drive has been preset */
206264Sbill #define	MOL	010000		/* Drive is online, heads loaded, etc */
207264Sbill #define	DRY	0200		/* Drive ready */
208264Sbill 
209313Sbill /* Bits of upcs2 */
210313Sbill #define	CLR	040		/* Controller clear */
211264Sbill /* Bits of uper1 */
212264Sbill #define	DCK	0100000		/* Ecc error occurred */
213264Sbill #define	ECH	0100		/* Ecc error was unrecoverable */
214264Sbill #define	WLE	04000		/* Attempt to write read-only drive */
215264Sbill 
216264Sbill /* Bits of upof; the offset bits above are also in this register */
217264Sbill #define	FMT22	010000		/* 16 bits/word, must be always set */
218264Sbill 
219264Sbill #define	b_cylin b_resid
220264Sbill 
221264Sbill int	up_ubinfo;		/* Information about UBA usage saved here */
222264Sbill /*
223264Sbill  * The EMULEX controller balks if accessed quickly after
224341Sbill  * certain operations.  With rev J delays seem to be needed only
225341Sbill  * when selecting a new unit, and in drive initialization type
226341Sbill  * like PRESET and DCLR.  The following variables control the delay
227341Sbill  * DELAY(n) is approximately n usec.
228264Sbill  */
229367Sbill int	olducode = 1;
230264Sbill int	idelay = 500;		/* Delay after PRESET or DCLR */
231367Sbill int	osdelay = 150;		/* Old delay after selecting drive in upcs2 */
232367Sbill int	ordelay = 100;		/* Old delay after SEARCH */
233367Sbill int	oasdel = 100;		/* Old delay after clearing bit in upas */
234367Sbill int	nsdelay = 25;
235264Sbill 
236264Sbill #define	DELAY(N)		{ register int d; d = N; while (--d > 0); }
237264Sbill 
238264Sbill int	nwaitcs2;		/* How many sdelay loops ? */
239264Sbill int	neasycs2;		/* How many sdelay loops not needed ? */
240264Sbill 
241313Sbill int	up_wticks;		/* Ticks waiting for interrupt */
242313Sbill int	upwstart;		/* Have started guardian */
243313Sbill int	upwatch();
244313Sbill 
245264Sbill #ifdef INTRLVE
246264Sbill daddr_t dkblock();
247264Sbill #endif
248264Sbill 
249264Sbill /*
250264Sbill  * Queue an i/o request for a drive, checking first that it is in range.
251264Sbill  *
252264Sbill  * A unit start is issued if the drive is inactive, causing
253264Sbill  * a SEARCH for the correct cylinder/sector.  If the drive is
254264Sbill  * already nearly on the money and the controller is not transferring
255264Sbill  * we kick it to start the transfer.
256264Sbill  */
257264Sbill upstrategy(bp)
258264Sbill register struct buf *bp;
259264Sbill {
260264Sbill 	register struct buf *dp;
261264Sbill 	register unit, xunit;
262264Sbill 	long sz, bn;
263264Sbill 
264313Sbill 	if (upwstart == 0) {
265313Sbill 		timeout((caddr_t)upwatch, 0, HZ);
266313Sbill 		upwstart++;
267313Sbill 	}
268264Sbill 	xunit = minor(bp->b_dev) & 077;
269264Sbill 	sz = bp->b_bcount;
270264Sbill 	sz = (sz+511) >> 9;		/* transfer size in 512 byte sectors */
271264Sbill 	unit = dkunit(bp);
272264Sbill 	if (unit >= NUP ||
273264Sbill 	    bp->b_blkno < 0 ||
274264Sbill 	    (bn = dkblock(bp))+sz > up_sizes[xunit&07].nblocks) {
275264Sbill 		bp->b_flags |= B_ERROR;
276264Sbill 		iodone(bp);
277264Sbill 		return;
278264Sbill 	}
279264Sbill 	bp->b_cylin = bn/(NSECT*NTRAC) + up_sizes[xunit&07].cyloff;
280264Sbill 	dp = &uputab[unit];
281264Sbill 	(void) spl5();
282264Sbill 	disksort(dp, bp);
283264Sbill 	if (dp->b_active == 0) {
284268Sbill 		(void) upustart(unit);
285264Sbill 		if (uptab.b_actf && uptab.b_active == 0)
286268Sbill 			(void) upstart();
287264Sbill 	}
288264Sbill 	(void) spl0();
289264Sbill }
290264Sbill 
291264Sbill /*
292264Sbill  * Start activity on specified drive; called when drive is inactive
293264Sbill  * and new transfer request arrives and also when upas indicates that
294264Sbill  * a SEARCH command is complete.
295264Sbill  */
296264Sbill upustart(unit)
297264Sbill register unit;
298264Sbill {
299264Sbill 	register struct buf *bp, *dp;
300264Sbill 	register struct device *upaddr = UPADDR;
301264Sbill 	daddr_t bn;
302264Sbill 	int sn, cn, csn;
303268Sbill 	int didie = 0;
304264Sbill 
305275Sbill 	/*
306275Sbill 	 * Other drivers tend to say something like
307275Sbill 	 *	upaddr->upcs1 = IE;
308275Sbill 	 *	upaddr->upas = 1<<unit;
309275Sbill 	 * here, but the SC-11B will cancel a command which
310275Sbill 	 * happens to be sitting in the cs1 if you clear the go
311275Sbill 	 * bit by storing there (so the first is not safe),
312275Sbill 	 * and it also does not like being bothered with operations
313275Sbill 	 * such as clearing upas when a transfer is active (as
314275Sbill 	 * it may well be.)
315275Sbill 	 *
316275Sbill 	 * Thus we keep careful track of when we re-enable IE
317275Sbill 	 * after an interrupt and do it only if we didn't issue
318275Sbill 	 * a command which re-enabled it as a matter of course.
319275Sbill 	 * We clear bits in upas in the interrupt routine, when
320275Sbill 	 * no transfers are active.
321275Sbill 	 */
322266Sbill 	if (unit >= NUP)
323268Sbill 		goto out;
324264Sbill 	if (unit+DK_N <= DK_NMAX)
325264Sbill 		dk_busy &= ~(1<<(unit+DK_N));
326264Sbill 	dp = &uputab[unit];
327266Sbill 	if ((bp = dp->b_actf) == NULL)
328268Sbill 		goto out;
329275Sbill 	/*
330275Sbill 	 * The SC-11B doesn't start SEARCH commands when transfers are
331275Sbill 	 * in progress.  In fact, it tends to get confused when given
332275Sbill 	 * SEARCH'es during transfers, generating interrupts with neither
333275Sbill 	 * RDY nor a bit in the upas register.  Thus we defer
334275Sbill 	 * until an interrupt when a transfer is pending.
335275Sbill 	 */
336275Sbill 	if (uptab.b_active) {
337341Sbill 		upsoftas |= 1<<unit;
338275Sbill 		return (0);
339275Sbill 	}
340276Sbill 	if (dp->b_active)
341276Sbill 		goto done;
342276Sbill 	dp->b_active = 1;
343264Sbill 	if ((upaddr->upcs2 & 07) != unit) {
344264Sbill 		upaddr->upcs2 = unit;
345367Sbill 		DELAY(olducode ? osdelay : nsdelay);
346264Sbill 		nwaitcs2++;
347264Sbill 	} else
348264Sbill 		neasycs2++;
349266Sbill 	/*
350266Sbill 	 * If we have changed packs or just initialized,
351275Sbill 	 * then the volume will not be valid; if so, clear
352266Sbill 	 * the drive, preset it and put in 16bit/word mode.
353266Sbill 	 */
354266Sbill 	if ((upaddr->upds & VV) == 0) {
355266Sbill 		upaddr->upcs1 = IE|DCLR|GO;
356266Sbill 		DELAY(idelay);
357264Sbill 		upaddr->upcs1 = IE|PRESET|GO;
358264Sbill 		DELAY(idelay);
359264Sbill 		upaddr->upof = FMT22;
360268Sbill 		didie = 1;
361264Sbill 	}
362264Sbill 	if ((upaddr->upds & (DPR|MOL)) != (DPR|MOL))
363275Sbill 		goto done;
364266Sbill 	/*
365266Sbill 	 * Do enough of the disk address decoding to determine
366266Sbill 	 * which cylinder and sector the request is on.
367266Sbill 	 * If we are on the correct cylinder and the desired sector
368308Sbill 	 * lies between upSDIST and upSDIST+upRDIST sectors ahead of us, then
369266Sbill 	 * we don't bother to SEARCH but just begin the transfer asap.
370308Sbill 	 * Otherwise ask for a interrupt upSDIST sectors ahead.
371266Sbill 	 */
372264Sbill 	bn = dkblock(bp);
373264Sbill 	cn = bp->b_cylin;
374264Sbill 	sn = bn%(NSECT*NTRAC);
375308Sbill 	sn = (sn+NSECT-upSDIST)%NSECT;
376264Sbill 
377266Sbill 	if (cn - upaddr->updc)
378266Sbill 		goto search;		/* Not on-cylinder */
379275Sbill 	else if (upseek)
380275Sbill 		goto done;		/* Ok just to be on-cylinder */
381264Sbill 	csn = (upaddr->upla>>6) - sn - 1;
382266Sbill 	if (csn < 0)
383264Sbill 		csn += NSECT;
384308Sbill 	if (csn > NSECT-upRDIST)
385264Sbill 		goto done;
386264Sbill 
387264Sbill search:
388264Sbill 	upaddr->updc = cn;
389275Sbill 	if (upseek)
390275Sbill 		upaddr->upcs1 = IE|SEEK|GO;
391275Sbill 	else {
392275Sbill 		upaddr->upda = sn;
393275Sbill 		upaddr->upcs1 = IE|SEARCH|GO;
394275Sbill 	}
395268Sbill 	didie = 1;
396266Sbill 	/*
397266Sbill 	 * Mark this unit busy.
398266Sbill 	 */
399264Sbill 	unit += DK_N;
400345Sbill 	if (unit <= DK_NMAX && DK_N+NUP <= DK_NMAX) {
401264Sbill 		dk_busy |= 1<<unit;
402264Sbill 		dk_numb[unit]++;
403264Sbill 	}
404367Sbill 	if (olducode)
405367Sbill 		DELAY(ordelay);
406268Sbill 	goto out;
407264Sbill 
408264Sbill done:
409266Sbill 	/*
410275Sbill 	 * This unit is ready to go so
411275Sbill 	 * link it onto the chain of ready disks.
412266Sbill 	 */
413264Sbill 	dp->b_forw = NULL;
414266Sbill 	if (uptab.b_actf == NULL)
415264Sbill 		uptab.b_actf = dp;
416264Sbill 	else
417264Sbill 		uptab.b_actl->b_forw = dp;
418264Sbill 	uptab.b_actl = dp;
419268Sbill 
420268Sbill out:
421268Sbill 	return (didie);
422264Sbill }
423264Sbill 
424264Sbill /*
425264Sbill  * Start a transfer; call from top level at spl5() or on interrupt.
426264Sbill  */
427264Sbill upstart()
428264Sbill {
429264Sbill 	register struct buf *bp, *dp;
430264Sbill 	register unit;
431264Sbill 	register struct device *upaddr;
432264Sbill 	daddr_t bn;
433266Sbill 	int dn, sn, tn, cn, cmd;
434264Sbill 
435264Sbill loop:
436266Sbill 	/*
437266Sbill 	 * Pick a drive off the queue of ready drives, and
438266Sbill 	 * perform the first transfer on its queue.
439266Sbill 	 *
440266Sbill 	 * Looping here is completely for the sake of drives which
441266Sbill 	 * are not present and on-line, for which we completely clear the
442266Sbill 	 * request queue.
443266Sbill 	 */
444273Sbill 	if ((dp = uptab.b_actf) == NULL)
445268Sbill 		return (0);
446264Sbill 	if ((bp = dp->b_actf) == NULL) {
447264Sbill 		uptab.b_actf = dp->b_forw;
448264Sbill 		goto loop;
449264Sbill 	}
450266Sbill 	/*
451266Sbill 	 * Mark the controller busy, and multi-part disk address.
452266Sbill 	 * Select the unit on which the i/o is to take place.
453266Sbill 	 */
454264Sbill 	uptab.b_active++;
455264Sbill 	unit = minor(bp->b_dev) & 077;
456264Sbill 	dn = dkunit(bp);
457264Sbill 	bn = dkblock(bp);
458264Sbill 	cn = up_sizes[unit&07].cyloff;
459264Sbill 	cn += bn/(NSECT*NTRAC);
460264Sbill 	sn = bn%(NSECT*NTRAC);
461264Sbill 	tn = sn/NSECT;
462266Sbill 	sn %= NSECT;
463264Sbill 	upaddr = UPADDR;
464264Sbill 	if ((upaddr->upcs2 & 07) != dn) {
465264Sbill 		upaddr->upcs2 = dn;
466275Sbill 		/* DELAY(sdelay);		Provided by ubasetup() */
467264Sbill 		nwaitcs2++;
468264Sbill 	} else
469264Sbill 		neasycs2++;
470275Sbill 	up_ubinfo = ubasetup(bp, 1);	/* Providing delay */
471266Sbill 	/*
472266Sbill 	 * If drive is not present and on-line, then
473266Sbill 	 * get rid of this with an error and loop to get
474266Sbill 	 * rid of the rest of its queued requests.
475266Sbill 	 * (Then on to any other ready drives.)
476266Sbill 	 */
477264Sbill 	if ((upaddr->upds & (DPR|MOL)) != (DPR|MOL)) {
478*893Sbill 		printf("!DPR || !MOL, unit %d, ds %o", dn, upaddr->upds);
479*893Sbill 		if ((upaddr->upds & (DPR|MOL)) != (DPR|MOL)) {
480*893Sbill 			printf("-- hard\n");
481*893Sbill 			uptab.b_active = 0;
482*893Sbill 			uptab.b_errcnt = 0;
483*893Sbill 			dp->b_actf = bp->av_forw;
484*893Sbill 			dp->b_active = 0;
485*893Sbill 			bp->b_flags |= B_ERROR;
486*893Sbill 			iodone(bp);
487*893Sbill 			/* A funny place to do this ... */
488*893Sbill 			ubafree(up_ubinfo), up_ubinfo = 0;
489*893Sbill 			goto loop;
490*893Sbill 		}
491*893Sbill 		printf("-- came back\n");
492264Sbill 	}
493266Sbill 	/*
494266Sbill 	 * If this is a retry, then with the 16'th retry we
495266Sbill 	 * begin to try offsetting the heads to recover the data.
496266Sbill 	 */
497266Sbill 	if (uptab.b_errcnt >= 16) {
498264Sbill 		upaddr->upof = up_offset[uptab.b_errcnt & 017] | FMT22;
499266Sbill 		upaddr->upcs1 = IE|OFFSET|GO;
500264Sbill 		DELAY(idelay);
501266Sbill 		while (upaddr->upds & PIP)
502264Sbill 			DELAY(25);
503264Sbill 	}
504266Sbill 	/*
505266Sbill 	 * Now set up the transfer, retrieving the high
506266Sbill 	 * 2 bits of the UNIBUS address from the information
507266Sbill 	 * returned by ubasetup() for the cs1 register bits 8 and 9.
508266Sbill 	 */
509264Sbill 	upaddr->updc = cn;
510264Sbill 	upaddr->upda = (tn << 8) + sn;
511264Sbill 	upaddr->upba = up_ubinfo;
512264Sbill 	upaddr->upwc = -bp->b_bcount / sizeof (short);
513266Sbill 	cmd = (up_ubinfo >> 8) & 0x300;
514264Sbill 	if (bp->b_flags & B_READ)
515266Sbill 		cmd |= IE|RCOM|GO;
516264Sbill 	else
517266Sbill 		cmd |= IE|WCOM|GO;
518266Sbill 	upaddr->upcs1 = cmd;
519266Sbill 	/*
520266Sbill 	 * This is a controller busy situation.
521266Sbill 	 * Record in dk slot NUP+DK_N (after last drive)
522266Sbill 	 * unless there aren't that many slots reserved for
523266Sbill 	 * us in which case we record this as a drive busy
524266Sbill 	 * (if there is room for that).
525266Sbill 	 */
526264Sbill 	unit = dn+DK_N;
527264Sbill 	if (NUP+DK_N == DK_NMAX)
528264Sbill 		unit = NUP+DK_N;
529264Sbill 	if (unit <= DK_NMAX) {
530264Sbill 		dk_busy |= 1<<unit;
531264Sbill 		dk_numb[unit]++;
532264Sbill 		dk_wds[unit] += bp->b_bcount>>6;
533264Sbill 	}
534268Sbill 	return (1);
535264Sbill }
536264Sbill 
537264Sbill /*
538264Sbill  * Handle a device interrupt.
539264Sbill  *
540264Sbill  * If the transferring drive needs attention, service it
541264Sbill  * retrying on error or beginning next transfer.
542264Sbill  * Service all other ready drives, calling ustart to transfer
543264Sbill  * their blocks to the ready queue in uptab, and then restart
544264Sbill  * the controller if there is anything to do.
545264Sbill  */
546264Sbill upintr()
547264Sbill {
548264Sbill 	register struct buf *bp, *dp;
549264Sbill 	register unit;
550264Sbill 	register struct device *upaddr = UPADDR;
551264Sbill 	int as = upaddr->upas & 0377;
552341Sbill 	int oupsoftas;
553268Sbill 	int needie = 1;
554264Sbill 
555276Sbill 	(void) spl6();
556313Sbill 	up_wticks = 0;
557266Sbill 	if (uptab.b_active) {
558266Sbill 		/*
559266Sbill 		 * The drive is transferring, thus the hardware
560266Sbill 		 * (say the designers) will only interrupt when the transfer
561266Sbill 		 * completes; check for it anyways.
562266Sbill 		 */
563266Sbill 		if ((upaddr->upcs1 & RDY) == 0) {
564272Sbill 			printf("!RDY: cs1 %o, ds %o, wc %d\n", upaddr->upcs1,
565272Sbill 			    upaddr->upds, upaddr->upwc);
566341Sbill 			printf("as=%d act %d %d %d\n", as, uptab.b_active,
567341Sbill 			    uputab[0].b_active, uputab[1].b_active);
568269Sbill 		}
569266Sbill 		/*
570266Sbill 		 * Mark controller or drive not busy, and check for an
571266Sbill 		 * error condition which may have resulted from the transfer.
572266Sbill 		 */
573264Sbill 		dp = uptab.b_actf;
574264Sbill 		bp = dp->b_actf;
575264Sbill 		unit = dkunit(bp);
576264Sbill 		if (DK_N+NUP == DK_NMAX)
577264Sbill 			dk_busy &= ~(1<<(DK_N+NUP));
578264Sbill 		else if (DK_N+unit <= DK_NMAX)
579264Sbill 			dk_busy &= ~(1<<(DK_N+unit));
580275Sbill 		if ((upaddr->upcs2 & 07) != unit) {
581275Sbill 			upaddr->upcs2 = unit;
582367Sbill 			DELAY(olducode ? osdelay : nsdelay);
583275Sbill 			nwaitcs2++;
584275Sbill 		} else
585275Sbill 			neasycs2++;
586885Sbill 		if ((upaddr->upds&ERR) || (upaddr->upcs1&TRE)) {
587266Sbill 			/*
588266Sbill 			 * An error occurred, indeed.  Select this unit
589266Sbill 			 * to get at the drive status (a SEARCH may have
590266Sbill 			 * intervened to change the selected unit), and
591266Sbill 			 * wait for the command which caused the interrupt
592266Sbill 			 * to complete (DRY).
593266Sbill 			 */
594266Sbill 			while ((upaddr->upds & DRY) == 0)
595264Sbill 				DELAY(25);
596266Sbill 			/*
597266Sbill 			 * After 28 retries (16 w/o servo offsets, and then
598266Sbill 			 * 12 with servo offsets), or if we encountered
599266Sbill 			 * an error because the drive is write-protected,
600266Sbill 			 * give up.  Print an error message on the last 2
601266Sbill 			 * retries before a hard failure.
602266Sbill 			 */
603266Sbill 			if (++uptab.b_errcnt > 28 || upaddr->uper1&WLE)
604264Sbill 				bp->b_flags |= B_ERROR;
605264Sbill 			else
606266Sbill 				uptab.b_active = 0;	/* To force retry */
607266Sbill 			if (uptab.b_errcnt > 27)
608264Sbill 				deverror(bp, upaddr->upcs2, upaddr->uper1);
609266Sbill 			/*
610266Sbill 			 * If this was a correctible ECC error, let upecc
611266Sbill 			 * do the dirty work to correct it.  If upecc
612266Sbill 			 * starts another READ for the rest of the data
613266Sbill 			 * then it returns 1 (having set uptab.b_active).
614266Sbill 			 * Otherwise we are done and fall through to
615266Sbill 			 * finish up.
616266Sbill 			 */
617266Sbill 			if ((upaddr->uper1&(DCK|ECH))==DCK && upecc(upaddr, bp))
618266Sbill 				return;
619266Sbill 			/*
620266Sbill 			 * Clear the drive and, every 4 retries, recalibrate
621266Sbill 			 * to hopefully help clear up seek positioning problems.
622266Sbill 			 */
623264Sbill 			upaddr->upcs1 = TRE|IE|DCLR|GO;
624264Sbill 			DELAY(idelay);
625268Sbill 			needie = 0;
626266Sbill 			if ((uptab.b_errcnt&07) == 4) {
627264Sbill 				upaddr->upcs1 = RECAL|GO|IE;
628264Sbill 				DELAY(idelay);
629264Sbill 				while(upaddr->upds & PIP)
630264Sbill 					DELAY(25);
631264Sbill 			}
632264Sbill 		}
633266Sbill 		/*
634266Sbill 		 * If we are still noted as active, then no
635266Sbill 		 * (further) retries are necessary.
636266Sbill 		 *
637266Sbill 		 * Make sure the correct unit is selected,
638266Sbill 		 * return it to centerline if necessary, and mark
639266Sbill 		 * this i/o complete, starting the next transfer
640266Sbill 		 * on this drive with the upustart routine (if any).
641266Sbill 		 */
642266Sbill 		if (uptab.b_active) {
643266Sbill 			if (uptab.b_errcnt >= 16) {
644266Sbill 				upaddr->upcs1 = RTC|GO|IE;
645264Sbill 				DELAY(idelay);
646266Sbill 				while (upaddr->upds & PIP)
647264Sbill 					DELAY(25);
648268Sbill 				needie = 0;
649264Sbill 			}
650264Sbill 			uptab.b_active = 0;
651264Sbill 			uptab.b_errcnt = 0;
652264Sbill 			uptab.b_actf = dp->b_forw;
653264Sbill 			dp->b_active = 0;
654264Sbill 			dp->b_errcnt = 0;
655264Sbill 			dp->b_actf = bp->av_forw;
656266Sbill 			bp->b_resid = (-upaddr->upwc * sizeof(short));
657275Sbill 			if (bp->b_resid)
658341Sbill 				printf("resid %d ds %o er? %o %o %o\n",
659341Sbill 				    bp->b_resid, upaddr->upds,
660275Sbill 				    upaddr->uper1, upaddr->uper2, upaddr->uper3);
661264Sbill 			iodone(bp);
662264Sbill 			if(dp->b_actf)
663268Sbill 				if (upustart(unit))
664268Sbill 					needie = 0;
665264Sbill 		}
666264Sbill 		as &= ~(1<<unit);
667341Sbill 		upsoftas &= ~(1<<unit);
668264Sbill 		ubafree(up_ubinfo), up_ubinfo = 0;
669273Sbill 	} else {
670264Sbill 		if (upaddr->upcs1 & TRE) {
671264Sbill 			upaddr->upcs1 = TRE;
672264Sbill 			DELAY(idelay);
673264Sbill 		}
674264Sbill 	}
675266Sbill 	/*
676266Sbill 	 * If we have a unit with an outstanding SEARCH,
677266Sbill 	 * and the hardware indicates the unit requires attention,
678266Sbill 	 * the bring the drive to the ready queue.
679266Sbill 	 * Finally, if the controller is not transferring
680266Sbill 	 * start it if any drives are now ready to transfer.
681266Sbill 	 */
682341Sbill 	as |= upsoftas;
683341Sbill 	oupsoftas = upsoftas;
684341Sbill 	upsoftas = 0;
685266Sbill 	for (unit = 0; unit < NUP; unit++)
686341Sbill 		if ((as|oupsoftas) & (1<<unit)) {
687273Sbill 			if (as & (1<<unit)) {
688267Sbill 				upaddr->upas = 1<<unit;
689367Sbill 				if (olducode)
690367Sbill 					DELAY(oasdel);
691272Sbill 			}
692273Sbill 			if (upustart(unit))
693273Sbill 				needie = 0;
694273Sbill 		}
695266Sbill 	if (uptab.b_actf && uptab.b_active == 0)
696268Sbill 		if (upstart())
697268Sbill 			needie = 0;
698266Sbill out:
699275Sbill 	if (needie)
700266Sbill 		upaddr->upcs1 = IE;
701264Sbill }
702264Sbill 
703264Sbill upread(dev)
704264Sbill {
705264Sbill 
706264Sbill 	physio(upstrategy, &rupbuf, dev, B_READ, minphys);
707264Sbill }
708264Sbill 
709264Sbill upwrite(dev)
710264Sbill {
711264Sbill 
712264Sbill 	physio(upstrategy, &rupbuf, dev, B_WRITE, minphys);
713264Sbill }
714264Sbill 
715266Sbill /*
716266Sbill  * Correct an ECC error, and restart the i/o to complete
717266Sbill  * the transfer if necessary.  This is quite complicated because
718266Sbill  * the transfer may be going to an odd memory address base and/or
719266Sbill  * across a page boundary.
720266Sbill  */
721264Sbill upecc(up, bp)
722264Sbill register struct device *up;
723264Sbill register struct buf *bp;
724264Sbill {
725264Sbill 	struct uba_regs *ubp = (struct uba_regs *)UBA0;
726266Sbill 	register int i;
727264Sbill 	caddr_t addr;
728266Sbill 	int reg, bit, byte, npf, mask, o, cmd, ubaddr;
729264Sbill 	int bn, cn, tn, sn;
730264Sbill 
731264Sbill 	/*
732266Sbill 	 * Npf is the number of sectors transferred before the sector
733266Sbill 	 * containing the ECC error, and reg is the UBA register
734266Sbill 	 * mapping (the first part of) the transfer.
735266Sbill 	 * O is offset within a memory page of the first byte transferred.
736264Sbill 	 */
737266Sbill 	npf = btop((up->upwc * sizeof(short)) + bp->b_bcount) - 1;
738266Sbill 	reg = btop(up_ubinfo&0x3ffff) + npf;
739264Sbill 	o = (int)bp->b_un.b_addr & PGOFSET;
740264Sbill 	printf("%D ", bp->b_blkno+npf);
741264Sbill 	prdev("ECC", bp->b_dev);
742264Sbill 	mask = up->upec2;
743264Sbill 	if (mask == 0) {
744266Sbill 		up->upof = FMT22;		/* == RTC ???? */
745264Sbill 		DELAY(idelay);
746264Sbill 		return (0);
747264Sbill 	}
748266Sbill 	/*
749266Sbill 	 * Flush the buffered data path, and compute the
750266Sbill 	 * byte and bit position of the error.  The variable i
751266Sbill 	 * is the byte offset in the transfer, the variable byte
752266Sbill 	 * is the offset from a page boundary in main memory.
753266Sbill 	 */
754266Sbill 	ubp->uba_dpr[(up_ubinfo>>28)&0x0f] |= BNE;
755266Sbill 	i = up->upec1 - 1;		/* -1 makes 0 origin */
756266Sbill 	bit = i&07;
757266Sbill 	i = (i&~07)>>3;
758264Sbill 	byte = i + o;
759266Sbill 	/*
760266Sbill 	 * Correct while possible bits remain of mask.  Since mask
761266Sbill 	 * contains 11 bits, we continue while the bit offset is > -11.
762266Sbill 	 * Also watch out for end of this block and the end of the whole
763266Sbill 	 * transfer.
764266Sbill 	 */
765266Sbill 	while (i < 512 && (int)ptob(npf)+i < bp->b_bcount && bit > -11) {
766266Sbill 		addr = ptob(ubp->uba_map[reg+btop(byte)].pg_pfnum)+
767266Sbill 		    (byte & PGOFSET);
768266Sbill 		putmemc(addr, getmemc(addr)^(mask<<bit));
769266Sbill 		byte++;
770266Sbill 		i++;
771266Sbill 		bit -= 8;
772264Sbill 	}
773266Sbill 	uptab.b_active++;	/* Either complete or continuing... */
774264Sbill 	if (up->upwc == 0)
775264Sbill 		return (0);
776266Sbill 	/*
777266Sbill 	 * Have to continue the transfer... clear the drive,
778266Sbill 	 * and compute the position where the transfer is to continue.
779266Sbill 	 * We have completed npf+1 sectors of the transfer already;
780266Sbill 	 * restart at offset o of next sector (i.e. in UBA register reg+1).
781266Sbill 	 */
782266Sbill 	up->upcs1 = TRE|IE|DCLR|GO;
783264Sbill 	DELAY(idelay);
784264Sbill 	bn = dkblock(bp);
785264Sbill 	cn = bp->b_cylin;
786266Sbill 	sn = bn%(NSECT*NTRAC) + npf + 1;
787264Sbill 	tn = sn/NSECT;
788264Sbill 	sn %= NSECT;
789266Sbill 	cn += tn/NTRAC;
790266Sbill 	tn %= NTRAC;
791264Sbill 	up->updc = cn;
792266Sbill 	up->upda = (tn << 8) | sn;
793266Sbill 	ubaddr = (int)ptob(reg+1) + o;
794266Sbill 	up->upba = ubaddr;
795266Sbill 	cmd = (ubaddr >> 8) & 0x300;
796266Sbill 	cmd |= IE|GO|RCOM;
797266Sbill 	up->upcs1 = cmd;
798264Sbill 	return (1);
799264Sbill }
800286Sbill 
801286Sbill /*
802286Sbill  * Reset driver after UBA init.
803286Sbill  * Cancel software state of all pending transfers
804286Sbill  * and restart all units and the controller.
805286Sbill  */
806286Sbill upreset()
807286Sbill {
808286Sbill 	int unit;
809286Sbill 
810286Sbill 	printf(" up");
811286Sbill 	uptab.b_active = 0;
812286Sbill 	uptab.b_actf = uptab.b_actl = 0;
813286Sbill 	if (DK_N+NUP == DK_NMAX)
814286Sbill 		dk_busy &= ~(1<<(DK_N+NUP));
815286Sbill 	if (up_ubinfo) {
816286Sbill 		printf("<%d>", (up_ubinfo>>28)&0xf);
817286Sbill 		ubafree(up_ubinfo), up_ubinfo = 0;
818286Sbill 	}
819313Sbill 	UPADDR->upcs2 = CLR;		/* clear controller */
820313Sbill 	DELAY(idelay);
821286Sbill 	for (unit = 0; unit < NUP; unit++) {
822286Sbill 		uputab[unit].b_active = 0;
823286Sbill 		(void) upustart(unit);
824286Sbill 	}
825286Sbill 	(void) upstart();
826286Sbill }
827313Sbill 
828313Sbill /*
829313Sbill  * Wake up every second and if an interrupt is pending
830313Sbill  * but nothing has happened increment a counter.
831313Sbill  * If nothing happens for 20 seconds, reset the controller
832313Sbill  * and begin anew.
833313Sbill  */
834313Sbill upwatch()
835313Sbill {
836313Sbill 	int i;
837313Sbill 
838313Sbill 	timeout((caddr_t)upwatch, 0, HZ);
839313Sbill 	if (uptab.b_active == 0) {
840313Sbill 		for (i = 0; i < NUP; i++)
841313Sbill 			if (uputab[i].b_active)
842313Sbill 				goto active;
843313Sbill 		up_wticks = 0;		/* idling */
844313Sbill 		return;
845313Sbill 	}
846313Sbill active:
847313Sbill 	up_wticks++;
848313Sbill 	if (up_wticks >= 20) {
849313Sbill 		up_wticks = 0;
850313Sbill 		printf("LOST INTERRUPT RESET");
851313Sbill 		upreset();
852313Sbill 		printf("\n");
853313Sbill 	}
854313Sbill }
855