xref: /csrg-svn/sys/vax/uba/up.c (revision 341)
1*341Sbill /*	10/14/12	3.16	07/01/80	*/
2264Sbill 
3264Sbill /*
4264Sbill  * Emulex UNIBUS disk driver with overlapped seeks and ECC recovery.
5264Sbill  *
6*341Sbill  * NB: This driver works reliably only on an SC-11B controller with
7*341Sbill  *     rev. level at least J (in particular rev. level H will not work well).
8*341Sbill  *     If you have an older controller you may be able to get by if you
9*341Sbill  *		#define	OLDUCODE
10*341Sbill  *     which implements larger delays for slow ucode.
11266Sbill  *
12*341Sbill  * 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)
24*341Sbill  *	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"
36264Sbill #include "../h/mba.h"
37264Sbill #include "../h/mtpr.h"
38264Sbill #include "../h/pte.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  */
85*341Sbill 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 */
132*341Sbill 	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  *
148*341Sbill  * 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 */
196266Sbill #define	RDY	020		/* 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
221*341Sbill  * certain operations.  With rev J delays seem to be needed only
222*341Sbill  * when selecting a new unit, and in drive initialization type
223*341Sbill  * like PRESET and DCLR.  The following variables control the delay
224*341Sbill  * DELAY(n) is approximately n usec.
225264Sbill  */
226264Sbill int	idelay = 500;		/* Delay after PRESET or DCLR */
227*341Sbill #ifdef OLDUCODE
228268Sbill int	sdelay = 150;		/* Delay after selecting drive in upcs2 */
229275Sbill int	rdelay = 100;		/* Delay after SEARCH */
230275Sbill int	asdel = 100;		/* Delay after clearing bit in upas */
231*341Sbill #else
232*341Sbill int	sdelay = 25;
233*341Sbill #endif
234264Sbill 
235264Sbill #define	DELAY(N)		{ register int d; d = N; while (--d > 0); }
236264Sbill 
237264Sbill int	nwaitcs2;		/* How many sdelay loops ? */
238264Sbill int	neasycs2;		/* How many sdelay loops not needed ? */
239264Sbill 
240313Sbill int	up_wticks;		/* Ticks waiting for interrupt */
241313Sbill int	upwstart;		/* Have started guardian */
242313Sbill int	upwatch();
243313Sbill 
244264Sbill #ifdef INTRLVE
245264Sbill daddr_t dkblock();
246264Sbill #endif
247264Sbill 
248264Sbill /*
249264Sbill  * Queue an i/o request for a drive, checking first that it is in range.
250264Sbill  *
251264Sbill  * A unit start is issued if the drive is inactive, causing
252264Sbill  * a SEARCH for the correct cylinder/sector.  If the drive is
253264Sbill  * already nearly on the money and the controller is not transferring
254264Sbill  * we kick it to start the transfer.
255264Sbill  */
256264Sbill upstrategy(bp)
257264Sbill register struct buf *bp;
258264Sbill {
259264Sbill 	register struct buf *dp;
260264Sbill 	register unit, xunit;
261264Sbill 	long sz, bn;
262264Sbill 
263313Sbill 	if (upwstart == 0) {
264313Sbill 		timeout((caddr_t)upwatch, 0, HZ);
265313Sbill 		upwstart++;
266313Sbill 	}
267264Sbill 	xunit = minor(bp->b_dev) & 077;
268264Sbill 	sz = bp->b_bcount;
269264Sbill 	sz = (sz+511) >> 9;		/* transfer size in 512 byte sectors */
270264Sbill 	unit = dkunit(bp);
271264Sbill 	if (unit >= NUP ||
272264Sbill 	    bp->b_blkno < 0 ||
273264Sbill 	    (bn = dkblock(bp))+sz > up_sizes[xunit&07].nblocks) {
274264Sbill 		bp->b_flags |= B_ERROR;
275264Sbill 		iodone(bp);
276264Sbill 		return;
277264Sbill 	}
278264Sbill 	bp->b_cylin = bn/(NSECT*NTRAC) + up_sizes[xunit&07].cyloff;
279264Sbill 	dp = &uputab[unit];
280264Sbill 	(void) spl5();
281264Sbill 	disksort(dp, bp);
282264Sbill 	if (dp->b_active == 0) {
283268Sbill 		(void) upustart(unit);
284264Sbill 		if (uptab.b_actf && uptab.b_active == 0)
285268Sbill 			(void) upstart();
286264Sbill 	}
287264Sbill 	(void) spl0();
288264Sbill }
289264Sbill 
290264Sbill /*
291264Sbill  * Start activity on specified drive; called when drive is inactive
292264Sbill  * and new transfer request arrives and also when upas indicates that
293264Sbill  * a SEARCH command is complete.
294264Sbill  */
295264Sbill upustart(unit)
296264Sbill register unit;
297264Sbill {
298264Sbill 	register struct buf *bp, *dp;
299264Sbill 	register struct device *upaddr = UPADDR;
300264Sbill 	daddr_t bn;
301264Sbill 	int sn, cn, csn;
302268Sbill 	int didie = 0;
303264Sbill 
304275Sbill 	/*
305275Sbill 	 * Other drivers tend to say something like
306275Sbill 	 *	upaddr->upcs1 = IE;
307275Sbill 	 *	upaddr->upas = 1<<unit;
308275Sbill 	 * here, but the SC-11B will cancel a command which
309275Sbill 	 * happens to be sitting in the cs1 if you clear the go
310275Sbill 	 * bit by storing there (so the first is not safe),
311275Sbill 	 * and it also does not like being bothered with operations
312275Sbill 	 * such as clearing upas when a transfer is active (as
313275Sbill 	 * it may well be.)
314275Sbill 	 *
315275Sbill 	 * Thus we keep careful track of when we re-enable IE
316275Sbill 	 * after an interrupt and do it only if we didn't issue
317275Sbill 	 * a command which re-enabled it as a matter of course.
318275Sbill 	 * We clear bits in upas in the interrupt routine, when
319275Sbill 	 * no transfers are active.
320275Sbill 	 */
321266Sbill 	if (unit >= NUP)
322268Sbill 		goto out;
323264Sbill 	if (unit+DK_N <= DK_NMAX)
324264Sbill 		dk_busy &= ~(1<<(unit+DK_N));
325264Sbill 	dp = &uputab[unit];
326266Sbill 	if ((bp = dp->b_actf) == NULL)
327268Sbill 		goto out;
328275Sbill 	/*
329275Sbill 	 * The SC-11B doesn't start SEARCH commands when transfers are
330275Sbill 	 * in progress.  In fact, it tends to get confused when given
331275Sbill 	 * SEARCH'es during transfers, generating interrupts with neither
332275Sbill 	 * RDY nor a bit in the upas register.  Thus we defer
333275Sbill 	 * until an interrupt when a transfer is pending.
334275Sbill 	 */
335275Sbill 	if (uptab.b_active) {
336*341Sbill 		upsoftas |= 1<<unit;
337275Sbill 		return (0);
338275Sbill 	}
339276Sbill 	if (dp->b_active)
340276Sbill 		goto done;
341276Sbill 	dp->b_active = 1;
342264Sbill 	if ((upaddr->upcs2 & 07) != unit) {
343264Sbill 		upaddr->upcs2 = unit;
344264Sbill 		DELAY(sdelay);
345264Sbill 		nwaitcs2++;
346264Sbill 	} else
347264Sbill 		neasycs2++;
348266Sbill 	/*
349266Sbill 	 * If we have changed packs or just initialized,
350275Sbill 	 * then the volume will not be valid; if so, clear
351266Sbill 	 * the drive, preset it and put in 16bit/word mode.
352266Sbill 	 */
353266Sbill 	if ((upaddr->upds & VV) == 0) {
354266Sbill 		upaddr->upcs1 = IE|DCLR|GO;
355266Sbill 		DELAY(idelay);
356264Sbill 		upaddr->upcs1 = IE|PRESET|GO;
357264Sbill 		DELAY(idelay);
358264Sbill 		upaddr->upof = FMT22;
359268Sbill 		didie = 1;
360264Sbill 	}
361264Sbill 	if ((upaddr->upds & (DPR|MOL)) != (DPR|MOL))
362275Sbill 		goto done;
363266Sbill 	/*
364266Sbill 	 * Do enough of the disk address decoding to determine
365266Sbill 	 * which cylinder and sector the request is on.
366266Sbill 	 * If we are on the correct cylinder and the desired sector
367308Sbill 	 * lies between upSDIST and upSDIST+upRDIST sectors ahead of us, then
368266Sbill 	 * we don't bother to SEARCH but just begin the transfer asap.
369308Sbill 	 * Otherwise ask for a interrupt upSDIST sectors ahead.
370266Sbill 	 */
371264Sbill 	bn = dkblock(bp);
372264Sbill 	cn = bp->b_cylin;
373264Sbill 	sn = bn%(NSECT*NTRAC);
374308Sbill 	sn = (sn+NSECT-upSDIST)%NSECT;
375264Sbill 
376266Sbill 	if (cn - upaddr->updc)
377266Sbill 		goto search;		/* Not on-cylinder */
378275Sbill 	else if (upseek)
379275Sbill 		goto done;		/* Ok just to be on-cylinder */
380264Sbill 	csn = (upaddr->upla>>6) - sn - 1;
381266Sbill 	if (csn < 0)
382264Sbill 		csn += NSECT;
383308Sbill 	if (csn > NSECT-upRDIST)
384264Sbill 		goto done;
385264Sbill 
386264Sbill search:
387264Sbill 	upaddr->updc = cn;
388275Sbill 	if (upseek)
389275Sbill 		upaddr->upcs1 = IE|SEEK|GO;
390275Sbill 	else {
391275Sbill 		upaddr->upda = sn;
392275Sbill 		upaddr->upcs1 = IE|SEARCH|GO;
393275Sbill 	}
394268Sbill 	didie = 1;
395266Sbill 	/*
396266Sbill 	 * Mark this unit busy.
397266Sbill 	 */
398264Sbill 	unit += DK_N;
399264Sbill 	if (unit <= DK_NMAX) {
400264Sbill 		dk_busy |= 1<<unit;
401264Sbill 		dk_numb[unit]++;
402264Sbill 	}
403*341Sbill #ifdef OLDUCODE
404275Sbill 	DELAY(rdelay);
405*341Sbill #endif
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)) {
478275Sbill 		printf("!DPR || !MOL, unit %d, ds %o\n", dn, upaddr->upds);
479264Sbill 		uptab.b_active = 0;
480264Sbill 		uptab.b_errcnt = 0;
481264Sbill 		dp->b_actf = bp->av_forw;
482266Sbill 		dp->b_active = 0;
483264Sbill 		bp->b_flags |= B_ERROR;
484264Sbill 		iodone(bp);
485266Sbill 		ubafree(up_ubinfo), up_ubinfo = 0;	/* A funny place ... */
486264Sbill 		goto loop;
487264Sbill 	}
488266Sbill 	/*
489266Sbill 	 * If this is a retry, then with the 16'th retry we
490266Sbill 	 * begin to try offsetting the heads to recover the data.
491266Sbill 	 */
492266Sbill 	if (uptab.b_errcnt >= 16) {
493264Sbill 		upaddr->upof = up_offset[uptab.b_errcnt & 017] | FMT22;
494266Sbill 		upaddr->upcs1 = IE|OFFSET|GO;
495264Sbill 		DELAY(idelay);
496266Sbill 		while (upaddr->upds & PIP)
497264Sbill 			DELAY(25);
498264Sbill 	}
499266Sbill 	/*
500266Sbill 	 * Now set up the transfer, retrieving the high
501266Sbill 	 * 2 bits of the UNIBUS address from the information
502266Sbill 	 * returned by ubasetup() for the cs1 register bits 8 and 9.
503266Sbill 	 */
504264Sbill 	upaddr->updc = cn;
505264Sbill 	upaddr->upda = (tn << 8) + sn;
506264Sbill 	upaddr->upba = up_ubinfo;
507264Sbill 	upaddr->upwc = -bp->b_bcount / sizeof (short);
508266Sbill 	cmd = (up_ubinfo >> 8) & 0x300;
509264Sbill 	if (bp->b_flags & B_READ)
510266Sbill 		cmd |= IE|RCOM|GO;
511264Sbill 	else
512266Sbill 		cmd |= IE|WCOM|GO;
513266Sbill 	upaddr->upcs1 = cmd;
514266Sbill 	/*
515266Sbill 	 * This is a controller busy situation.
516266Sbill 	 * Record in dk slot NUP+DK_N (after last drive)
517266Sbill 	 * unless there aren't that many slots reserved for
518266Sbill 	 * us in which case we record this as a drive busy
519266Sbill 	 * (if there is room for that).
520266Sbill 	 */
521264Sbill 	unit = dn+DK_N;
522264Sbill 	if (NUP+DK_N == DK_NMAX)
523264Sbill 		unit = NUP+DK_N;
524264Sbill 	if (unit <= DK_NMAX) {
525264Sbill 		dk_busy |= 1<<unit;
526264Sbill 		dk_numb[unit]++;
527264Sbill 		dk_wds[unit] += bp->b_bcount>>6;
528264Sbill 	}
529268Sbill 	return (1);
530264Sbill }
531264Sbill 
532264Sbill /*
533264Sbill  * Handle a device interrupt.
534264Sbill  *
535264Sbill  * If the transferring drive needs attention, service it
536264Sbill  * retrying on error or beginning next transfer.
537264Sbill  * Service all other ready drives, calling ustart to transfer
538264Sbill  * their blocks to the ready queue in uptab, and then restart
539264Sbill  * the controller if there is anything to do.
540264Sbill  */
541264Sbill upintr()
542264Sbill {
543264Sbill 	register struct buf *bp, *dp;
544264Sbill 	register unit;
545264Sbill 	register struct device *upaddr = UPADDR;
546264Sbill 	int as = upaddr->upas & 0377;
547*341Sbill 	int oupsoftas;
548268Sbill 	int needie = 1;
549264Sbill 
550*341Sbill #ifdef OLDUCODE
551276Sbill 	(void) spl6();
552*341Sbill #endif
553313Sbill 	up_wticks = 0;
554266Sbill 	if (uptab.b_active) {
555266Sbill 		/*
556266Sbill 		 * The drive is transferring, thus the hardware
557266Sbill 		 * (say the designers) will only interrupt when the transfer
558266Sbill 		 * completes; check for it anyways.
559266Sbill 		 */
560266Sbill 		if ((upaddr->upcs1 & RDY) == 0) {
561272Sbill 			printf("!RDY: cs1 %o, ds %o, wc %d\n", upaddr->upcs1,
562272Sbill 			    upaddr->upds, upaddr->upwc);
563*341Sbill 			printf("as=%d act %d %d %d\n", as, uptab.b_active,
564*341Sbill 			    uputab[0].b_active, uputab[1].b_active);
565269Sbill 		}
566266Sbill 		/*
567266Sbill 		 * Mark controller or drive not busy, and check for an
568266Sbill 		 * error condition which may have resulted from the transfer.
569266Sbill 		 */
570264Sbill 		dp = uptab.b_actf;
571264Sbill 		bp = dp->b_actf;
572264Sbill 		unit = dkunit(bp);
573264Sbill 		if (DK_N+NUP == DK_NMAX)
574264Sbill 			dk_busy &= ~(1<<(DK_N+NUP));
575264Sbill 		else if (DK_N+unit <= DK_NMAX)
576264Sbill 			dk_busy &= ~(1<<(DK_N+unit));
577275Sbill 		if ((upaddr->upcs2 & 07) != unit) {
578275Sbill 			upaddr->upcs2 = unit;
579275Sbill 			DELAY(sdelay);
580275Sbill 			nwaitcs2++;
581275Sbill 		} else
582275Sbill 			neasycs2++;
583275Sbill 		if (upaddr->upds & ERR) {
584266Sbill 			/*
585266Sbill 			 * An error occurred, indeed.  Select this unit
586266Sbill 			 * to get at the drive status (a SEARCH may have
587266Sbill 			 * intervened to change the selected unit), and
588266Sbill 			 * wait for the command which caused the interrupt
589266Sbill 			 * to complete (DRY).
590266Sbill 			 */
591266Sbill 			while ((upaddr->upds & DRY) == 0)
592264Sbill 				DELAY(25);
593266Sbill 			/*
594266Sbill 			 * After 28 retries (16 w/o servo offsets, and then
595266Sbill 			 * 12 with servo offsets), or if we encountered
596266Sbill 			 * an error because the drive is write-protected,
597266Sbill 			 * give up.  Print an error message on the last 2
598266Sbill 			 * retries before a hard failure.
599266Sbill 			 */
600266Sbill 			if (++uptab.b_errcnt > 28 || upaddr->uper1&WLE)
601264Sbill 				bp->b_flags |= B_ERROR;
602264Sbill 			else
603266Sbill 				uptab.b_active = 0;	/* To force retry */
604266Sbill 			if (uptab.b_errcnt > 27)
605264Sbill 				deverror(bp, upaddr->upcs2, upaddr->uper1);
606266Sbill 			/*
607266Sbill 			 * If this was a correctible ECC error, let upecc
608266Sbill 			 * do the dirty work to correct it.  If upecc
609266Sbill 			 * starts another READ for the rest of the data
610266Sbill 			 * then it returns 1 (having set uptab.b_active).
611266Sbill 			 * Otherwise we are done and fall through to
612266Sbill 			 * finish up.
613266Sbill 			 */
614266Sbill 			if ((upaddr->uper1&(DCK|ECH))==DCK && upecc(upaddr, bp))
615266Sbill 				return;
616266Sbill 			/*
617266Sbill 			 * Clear the drive and, every 4 retries, recalibrate
618266Sbill 			 * to hopefully help clear up seek positioning problems.
619266Sbill 			 */
620264Sbill 			upaddr->upcs1 = TRE|IE|DCLR|GO;
621264Sbill 			DELAY(idelay);
622268Sbill 			needie = 0;
623266Sbill 			if ((uptab.b_errcnt&07) == 4) {
624264Sbill 				upaddr->upcs1 = RECAL|GO|IE;
625264Sbill 				DELAY(idelay);
626264Sbill 				while(upaddr->upds & PIP)
627264Sbill 					DELAY(25);
628264Sbill 			}
629264Sbill 		}
630266Sbill 		/*
631266Sbill 		 * If we are still noted as active, then no
632266Sbill 		 * (further) retries are necessary.
633266Sbill 		 *
634266Sbill 		 * Make sure the correct unit is selected,
635266Sbill 		 * return it to centerline if necessary, and mark
636266Sbill 		 * this i/o complete, starting the next transfer
637266Sbill 		 * on this drive with the upustart routine (if any).
638266Sbill 		 */
639266Sbill 		if (uptab.b_active) {
640266Sbill 			if (uptab.b_errcnt >= 16) {
641266Sbill 				upaddr->upcs1 = RTC|GO|IE;
642264Sbill 				DELAY(idelay);
643266Sbill 				while (upaddr->upds & PIP)
644264Sbill 					DELAY(25);
645268Sbill 				needie = 0;
646264Sbill 			}
647264Sbill 			uptab.b_active = 0;
648264Sbill 			uptab.b_errcnt = 0;
649264Sbill 			uptab.b_actf = dp->b_forw;
650264Sbill 			dp->b_active = 0;
651264Sbill 			dp->b_errcnt = 0;
652264Sbill 			dp->b_actf = bp->av_forw;
653266Sbill 			bp->b_resid = (-upaddr->upwc * sizeof(short));
654275Sbill 			if (bp->b_resid)
655*341Sbill 				printf("resid %d ds %o er? %o %o %o\n",
656*341Sbill 				    bp->b_resid, upaddr->upds,
657275Sbill 				    upaddr->uper1, upaddr->uper2, upaddr->uper3);
658264Sbill 			iodone(bp);
659264Sbill 			if(dp->b_actf)
660268Sbill 				if (upustart(unit))
661268Sbill 					needie = 0;
662264Sbill 		}
663264Sbill 		as &= ~(1<<unit);
664*341Sbill 		upsoftas &= ~(1<<unit);
665264Sbill 		ubafree(up_ubinfo), up_ubinfo = 0;
666273Sbill 	} else {
667264Sbill 		if (upaddr->upcs1 & TRE) {
668264Sbill 			upaddr->upcs1 = TRE;
669264Sbill 			DELAY(idelay);
670264Sbill 		}
671264Sbill 	}
672266Sbill 	/*
673266Sbill 	 * If we have a unit with an outstanding SEARCH,
674266Sbill 	 * and the hardware indicates the unit requires attention,
675266Sbill 	 * the bring the drive to the ready queue.
676266Sbill 	 * Finally, if the controller is not transferring
677266Sbill 	 * start it if any drives are now ready to transfer.
678266Sbill 	 */
679*341Sbill 	as |= upsoftas;
680*341Sbill 	oupsoftas = upsoftas;
681*341Sbill 	upsoftas = 0;
682266Sbill 	for (unit = 0; unit < NUP; unit++)
683*341Sbill 		if ((as|oupsoftas) & (1<<unit)) {
684273Sbill 			if (as & (1<<unit)) {
685267Sbill 				upaddr->upas = 1<<unit;
686*341Sbill #ifdef OLDUCODE
687*341Sbill 				DELAY(asdel);
688*341Sbill #endif
689272Sbill 			}
690273Sbill 			if (upustart(unit))
691273Sbill 				needie = 0;
692273Sbill 		}
693266Sbill 	if (uptab.b_actf && uptab.b_active == 0)
694268Sbill 		if (upstart())
695268Sbill 			needie = 0;
696266Sbill out:
697275Sbill 	if (needie)
698266Sbill 		upaddr->upcs1 = IE;
699264Sbill }
700264Sbill 
701264Sbill upread(dev)
702264Sbill {
703264Sbill 
704264Sbill 	physio(upstrategy, &rupbuf, dev, B_READ, minphys);
705264Sbill }
706264Sbill 
707264Sbill upwrite(dev)
708264Sbill {
709264Sbill 
710264Sbill 	physio(upstrategy, &rupbuf, dev, B_WRITE, minphys);
711264Sbill }
712264Sbill 
713266Sbill /*
714266Sbill  * Correct an ECC error, and restart the i/o to complete
715266Sbill  * the transfer if necessary.  This is quite complicated because
716266Sbill  * the transfer may be going to an odd memory address base and/or
717266Sbill  * across a page boundary.
718266Sbill  */
719264Sbill upecc(up, bp)
720264Sbill register struct device *up;
721264Sbill register struct buf *bp;
722264Sbill {
723264Sbill 	struct uba_regs *ubp = (struct uba_regs *)UBA0;
724266Sbill 	register int i;
725264Sbill 	caddr_t addr;
726266Sbill 	int reg, bit, byte, npf, mask, o, cmd, ubaddr;
727264Sbill 	int bn, cn, tn, sn;
728264Sbill 
729264Sbill 	/*
730266Sbill 	 * Npf is the number of sectors transferred before the sector
731266Sbill 	 * containing the ECC error, and reg is the UBA register
732266Sbill 	 * mapping (the first part of) the transfer.
733266Sbill 	 * O is offset within a memory page of the first byte transferred.
734264Sbill 	 */
735266Sbill 	npf = btop((up->upwc * sizeof(short)) + bp->b_bcount) - 1;
736266Sbill 	reg = btop(up_ubinfo&0x3ffff) + npf;
737264Sbill 	o = (int)bp->b_un.b_addr & PGOFSET;
738264Sbill 	printf("%D ", bp->b_blkno+npf);
739264Sbill 	prdev("ECC", bp->b_dev);
740264Sbill 	mask = up->upec2;
741264Sbill 	if (mask == 0) {
742266Sbill 		up->upof = FMT22;		/* == RTC ???? */
743264Sbill 		DELAY(idelay);
744264Sbill 		return (0);
745264Sbill 	}
746266Sbill 	/*
747266Sbill 	 * Flush the buffered data path, and compute the
748266Sbill 	 * byte and bit position of the error.  The variable i
749266Sbill 	 * is the byte offset in the transfer, the variable byte
750266Sbill 	 * is the offset from a page boundary in main memory.
751266Sbill 	 */
752266Sbill 	ubp->uba_dpr[(up_ubinfo>>28)&0x0f] |= BNE;
753266Sbill 	i = up->upec1 - 1;		/* -1 makes 0 origin */
754266Sbill 	bit = i&07;
755266Sbill 	i = (i&~07)>>3;
756264Sbill 	byte = i + o;
757266Sbill 	/*
758266Sbill 	 * Correct while possible bits remain of mask.  Since mask
759266Sbill 	 * contains 11 bits, we continue while the bit offset is > -11.
760266Sbill 	 * Also watch out for end of this block and the end of the whole
761266Sbill 	 * transfer.
762266Sbill 	 */
763266Sbill 	while (i < 512 && (int)ptob(npf)+i < bp->b_bcount && bit > -11) {
764266Sbill 		addr = ptob(ubp->uba_map[reg+btop(byte)].pg_pfnum)+
765266Sbill 		    (byte & PGOFSET);
766266Sbill 		putmemc(addr, getmemc(addr)^(mask<<bit));
767266Sbill 		byte++;
768266Sbill 		i++;
769266Sbill 		bit -= 8;
770264Sbill 	}
771266Sbill 	uptab.b_active++;	/* Either complete or continuing... */
772264Sbill 	if (up->upwc == 0)
773264Sbill 		return (0);
774266Sbill 	/*
775266Sbill 	 * Have to continue the transfer... clear the drive,
776266Sbill 	 * and compute the position where the transfer is to continue.
777266Sbill 	 * We have completed npf+1 sectors of the transfer already;
778266Sbill 	 * restart at offset o of next sector (i.e. in UBA register reg+1).
779266Sbill 	 */
780266Sbill 	up->upcs1 = TRE|IE|DCLR|GO;
781264Sbill 	DELAY(idelay);
782264Sbill 	bn = dkblock(bp);
783264Sbill 	cn = bp->b_cylin;
784266Sbill 	sn = bn%(NSECT*NTRAC) + npf + 1;
785264Sbill 	tn = sn/NSECT;
786264Sbill 	sn %= NSECT;
787266Sbill 	cn += tn/NTRAC;
788266Sbill 	tn %= NTRAC;
789264Sbill 	up->updc = cn;
790266Sbill 	up->upda = (tn << 8) | sn;
791266Sbill 	ubaddr = (int)ptob(reg+1) + o;
792266Sbill 	up->upba = ubaddr;
793266Sbill 	cmd = (ubaddr >> 8) & 0x300;
794266Sbill 	cmd |= IE|GO|RCOM;
795266Sbill 	up->upcs1 = cmd;
796264Sbill 	return (1);
797264Sbill }
798286Sbill 
799286Sbill /*
800286Sbill  * Reset driver after UBA init.
801286Sbill  * Cancel software state of all pending transfers
802286Sbill  * and restart all units and the controller.
803286Sbill  */
804286Sbill upreset()
805286Sbill {
806286Sbill 	int unit;
807286Sbill 
808286Sbill 	printf(" up");
809286Sbill 	uptab.b_active = 0;
810286Sbill 	uptab.b_actf = uptab.b_actl = 0;
811286Sbill 	if (DK_N+NUP == DK_NMAX)
812286Sbill 		dk_busy &= ~(1<<(DK_N+NUP));
813286Sbill 	if (up_ubinfo) {
814286Sbill 		printf("<%d>", (up_ubinfo>>28)&0xf);
815286Sbill 		ubafree(up_ubinfo), up_ubinfo = 0;
816286Sbill 	}
817313Sbill 	UPADDR->upcs2 = CLR;		/* clear controller */
818313Sbill 	DELAY(idelay);
819286Sbill 	for (unit = 0; unit < NUP; unit++) {
820286Sbill 		uputab[unit].b_active = 0;
821286Sbill 		(void) upustart(unit);
822286Sbill 	}
823286Sbill 	(void) upstart();
824286Sbill }
825313Sbill 
826313Sbill /*
827313Sbill  * Wake up every second and if an interrupt is pending
828313Sbill  * but nothing has happened increment a counter.
829313Sbill  * If nothing happens for 20 seconds, reset the controller
830313Sbill  * and begin anew.
831313Sbill  */
832313Sbill upwatch()
833313Sbill {
834313Sbill 	int i;
835313Sbill 
836313Sbill 	timeout((caddr_t)upwatch, 0, HZ);
837313Sbill 	if (uptab.b_active == 0) {
838313Sbill 		for (i = 0; i < NUP; i++)
839313Sbill 			if (uputab[i].b_active)
840313Sbill 				goto active;
841313Sbill 		up_wticks = 0;		/* idling */
842313Sbill 		return;
843313Sbill 	}
844313Sbill active:
845313Sbill 	up_wticks++;
846313Sbill 	if (up_wticks >= 20) {
847313Sbill 		up_wticks = 0;
848313Sbill 		printf("LOST INTERRUPT RESET");
849313Sbill 		upreset();
850313Sbill 		printf("\n");
851313Sbill 	}
852313Sbill }
853