xref: /csrg-svn/sys/vax/uba/up.c (revision 308)
1*308Sbill /*	10/14/12	3.14	06/24/80	*/
2264Sbill 
3*308Sbill #define	spl5	spl6		/* block clock, for delay loop's sake */
4264Sbill /*
5264Sbill  * Emulex UNIBUS disk driver with overlapped seeks and ECC recovery.
6264Sbill  *
7266Sbill  * NB: This device is very sensitive: be aware that the code is the way
8266Sbill  *     it is for good reason and that there are delay loops here which may
9266Sbill  *     have to be lengthened if your processor is faster and which should
10266Sbill  *     probably be shortened if your processor is slower.
11266Sbill  *
12264Sbill  * This driver has been tested on a SC-11B Controller, configured
13264Sbill  * with the following internal switch settings:
14264Sbill  *	SW1-1	5/19 surfaces	(off, 19 surfaces on Ampex 9300)
15264Sbill  *	SW1-2	chksum enable	(off, checksum disabled)
16264Sbill  *	SW1-3	volume select	(off, 815 cylinders)
17264Sbill  *	SW1-4	sector select	(on, 32 sectors)
18264Sbill  *	SW1-5	unused		(off)
19264Sbill  *	SW1-6	port select	(on, single port)
20264Sbill  *	SW1-7	npr delay	(off, disable)
21264Sbill  *	SW1-8	ecc test mode	(off, disable)
22264Sbill  * and top mounted switches:
23264Sbill  *	SW2-1	extend opcodes	(off=open, disable)
24264Sbill  *	SW2-2	extend diag	(off=open, disable)
25264Sbill  *	SW2-3	4 wd dma burst	(off=open, disable)
26264Sbill  *	SW2-4	unused		(off=open)
27264Sbill  *
28264Sbill  * The controller transfers data much more rapidly with SW2-3 set,
29264Sbill  * but we have previously experienced problems with it set this way.
30264Sbill  * We intend to try this again in the near future.
31264Sbill  *
32*308Sbill  * NB: OUR SYSTEM CURRENTLY GETS UBA ERRORS WHEN RUNNING THIS DRIVER
33*308Sbill  *     AND THE BUS OCCASIONALLY HANGS, NECESSITATING THE DEVIE RESET
34*308Sbill  *     CODE WHICH RE-INITS THE UNIBUS.  YECHHH.
35264Sbill  */
36264Sbill 
37264Sbill #include "../h/param.h"
38264Sbill #include "../h/systm.h"
39*308Sbill #include "../h/dk.h"
40264Sbill #include "../h/buf.h"
41264Sbill #include "../h/conf.h"
42264Sbill #include "../h/dir.h"
43264Sbill #include "../h/user.h"
44264Sbill #include "../h/map.h"
45264Sbill #include "../h/mba.h"
46264Sbill #include "../h/mtpr.h"
47264Sbill #include "../h/pte.h"
48264Sbill #include "../h/uba.h"
49264Sbill #include "../h/vm.h"
50264Sbill 
51264Sbill /*
52264Sbill  * Define number of drives, and range of sampling information to be used.
53264Sbill  *
54264Sbill  * Normally, DK_N .. DK_N+NUP-1 gather individual drive stats,
55264Sbill  * and DK_N+NUP gathers controller transferring stats.
56264Sbill  *
57264Sbill  * If DK_N+NUP > DK_NMAX, then transfer stats are divided per drive.
58264Sbill  * If DK_NMAX is yet smaller, some drives are not monitored.
59264Sbill  */
60*308Sbill #define	DK_N	2
61*308Sbill #define	DK_NMAX	3
62264Sbill 
63264Sbill #define	ushort	unsigned short
64264Sbill 
65264Sbill struct	device
66264Sbill {
67264Sbill 	ushort	upcs1;		/* control and status register 1 */
68264Sbill 	short	upwc;		/* word count register */
69264Sbill 	ushort	upba;		/* UNIBUS address register */
70264Sbill 	ushort	upda;		/* desired address register */
71264Sbill 	ushort	upcs2;		/* control and status register 2 */
72264Sbill 	ushort	upds;		/* drive Status */
73264Sbill 	ushort	uper1;		/* error register 1 */
74264Sbill 	ushort	upas;		/* attention summary */
75264Sbill 	ushort	upla;		/* look ahead */
76264Sbill 	ushort	updb;		/* data buffer */
77264Sbill 	ushort	upmr;		/* maintenance */
78264Sbill 	ushort	updt;		/* drive type */
79264Sbill 	ushort	upsn;		/* serial number */
80264Sbill 	ushort	upof;		/* offset register */
81264Sbill 	ushort	updc;		/* desired cylinder address register */
82264Sbill 	ushort	upcc;		/* current cylinder */
83264Sbill 	ushort	uper2;		/* error register 2 */
84264Sbill 	ushort	uper3;		/* error register 3 */
85264Sbill 	ushort	upec1;		/* burst error bit position */
86264Sbill 	ushort	upec2;		/* burst error bit pattern */
87264Sbill };
88264Sbill 
89275Sbill /*
90275Sbill  * Software extension to the upas register, so we can
91275Sbill  * postpone starting SEARCH commands until the controller
92275Sbill  * is not transferring.
93275Sbill  */
94275Sbill int	softas;
95275Sbill 
96275Sbill /*
97275Sbill  * If upseek then we don't issue SEARCH commands but rather just
98275Sbill  * settle for a SEEK to the correct cylinder.
99275Sbill  */
100275Sbill int	upseek;
101275Sbill 
102264Sbill #define	UPADDR	((struct device *)(UBA0_DEV + 0176700))
103264Sbill 
104264Sbill #define	NUP	2		/* Number of drives this installation */
105264Sbill 
106264Sbill #define	NSECT	32
107264Sbill #define	NTRAC	19
108264Sbill 
109264Sbill /*
110264Sbill  * Constants controlling on-cylinder SEARCH usage.
111264Sbill  *
112*308Sbill  * 	upSDIST/2 msec		time needed to start transfer
113*308Sbill  * 	upRDIST/2 msec		tolerable rotational latency when on-cylinder
114275Sbill  *
115*308Sbill  * If we are no closer than upSDIST sectors and no further than upSDIST+upRDIST
116275Sbill  * and in the driver then we take it as it is.  Otherwise we do a SEARCH
117*308Sbill  * requesting an interrupt upSDIST sectors in advance.
118264Sbill  */
119*308Sbill #define	_upSDIST	6		/* 3.0 msec */
120*308Sbill #define	_upRDIST	6		/* 3.0 msec */
121264Sbill 
122*308Sbill int	upSDIST = _upSDIST;
123*308Sbill int	upRDIST = _upRDIST;
124275Sbill 
125264Sbill /*
126264Sbill  * To fill a 300M drive:
127264Sbill  *	A is designed to be used as a root.
128264Sbill  *	B is suitable for a swap area.
129264Sbill  *	H is the primary storage area.
130264Sbill  * On systems with RP06'es, we normally use only 291346 blocks of the H
131264Sbill  * area, and use DEF or G to cover the rest of the drive.  The C system
132264Sbill  * covers the whole drive and can be used for pack-pack copying.
133264Sbill  */
134264Sbill struct	size
135264Sbill {
136264Sbill 	daddr_t	nblocks;
137264Sbill 	int	cyloff;
138264Sbill } up_sizes[8] = {
139264Sbill 	15884,	0,		/* A=cyl 0 thru 26 */
140264Sbill 	33440,	27,		/* B=cyl 27 thru 81 */
141264Sbill 	494912,	0,		/* C=cyl 0 thru 814 */
142264Sbill 	15884,	562,		/* D=cyl 562 thru 588 */
143264Sbill 	55936,	589,		/* E=cyl 589 thru 680 */
144264Sbill 	81472,	681,		/* F=cyl 681 thru 814 */
145264Sbill 	153824,	562,		/* G=cyl 562 thru 814 */
146264Sbill 	445664,	82,		/* H=cyl 82 thru 814 */
147264Sbill /* Later, and more safely for H area...
148264Sbill 	291346,	82,		/* H=cyl 82 thru 561 */
149264Sbill };
150264Sbill 
151264Sbill /*
152264Sbill  * The following defines are used in offset positioning
153264Sbill  * when trying to recover disk errors, with the constants being
154264Sbill  * +/- microinches.  Note that header compare inhibit (HCI) is not
155264Sbill  * tried (this makes sense only during read, in any case.)
156264Sbill  *
157264Sbill  * ARE ALL THESE IMPLEMENTED ON 9300?
158264Sbill  */
159264Sbill #define	P400	020
160264Sbill #define	M400	0220
161264Sbill #define	P800	040
162264Sbill #define	M800	0240
163264Sbill #define	P1200	060
164264Sbill #define	M1200	0260
165264Sbill #define	HCI	020000
166264Sbill 
167264Sbill int	up_offset[16] =
168264Sbill {
169264Sbill 	P400, M400, P400, M400,
170264Sbill 	P800, M800, P800, M800,
171264Sbill 	P1200, M1200, P1200, M1200,
172264Sbill 	0, 0, 0, 0,
173264Sbill };
174264Sbill 
175264Sbill /*
176264Sbill  * Each drive has a table uputab[i].  On this table are sorted the
177264Sbill  * pending requests implementing an elevator algorithm (see dsort.c.)
178264Sbill  * In the upustart() routine, each drive is independently advanced
179264Sbill  * until it is on the desired cylinder for the next transfer and near
180264Sbill  * the desired sector.  The drive is then chained onto the uptab
181264Sbill  * table, and the transfer is initiated by the upstart() routine.
182264Sbill  * When the transfer is completed the driver reinvokes the upustart()
183264Sbill  * routine to set up the next transfer.
184264Sbill  */
185264Sbill struct	buf	uptab;
186264Sbill struct	buf	uputab[NUP];
187264Sbill 
188264Sbill struct	buf	rupbuf;			/* Buffer for raw i/o */
189264Sbill 
190264Sbill /* Drive commands, placed in upcs1 */
191264Sbill #define	GO	01		/* Go bit, set in all commands */
192264Sbill #define	PRESET	020		/* Preset drive at init or after errors */
193264Sbill #define	OFFSET	014		/* Offset heads to try to recover error */
194264Sbill #define	RTC	016		/* Return to center-line after OFFSET */
195264Sbill #define	SEARCH	030		/* Search for cylinder+sector */
196275Sbill #define	SEEK	04		/* Seek to cylinder */
197264Sbill #define	RECAL	06		/* Recalibrate, needed after seek error */
198264Sbill #define	DCLR	010		/* Drive clear, after error */
199264Sbill #define	WCOM	060		/* Write */
200264Sbill #define	RCOM	070		/* Read */
201264Sbill 
202264Sbill /* Other bits of upcs1 */
203264Sbill #define	IE	0100		/* Controller wide interrupt enable */
204264Sbill #define	TRE	040000		/* Transfer error */
205266Sbill #define	RDY	020		/* Transfer terminated */
206264Sbill 
207264Sbill /* Drive status bits of upds */
208264Sbill #define	PIP	020000		/* Positioning in progress */
209264Sbill #define	ERR	040000		/* Error has occurred, DCLR necessary */
210264Sbill #define	VV	0100		/* Volume is valid, set by PRESET */
211264Sbill #define	DPR	0400		/* Drive has been preset */
212264Sbill #define	MOL	010000		/* Drive is online, heads loaded, etc */
213264Sbill #define	DRY	0200		/* Drive ready */
214264Sbill 
215264Sbill /* Bits of uper1 */
216264Sbill #define	DCK	0100000		/* Ecc error occurred */
217264Sbill #define	ECH	0100		/* Ecc error was unrecoverable */
218264Sbill #define	WLE	04000		/* Attempt to write read-only drive */
219264Sbill 
220264Sbill /* Bits of upof; the offset bits above are also in this register */
221264Sbill #define	FMT22	010000		/* 16 bits/word, must be always set */
222264Sbill 
223264Sbill #define	b_cylin b_resid
224264Sbill 
225264Sbill int	up_ubinfo;		/* Information about UBA usage saved here */
226264Sbill /*
227264Sbill  * The EMULEX controller balks if accessed quickly after
228264Sbill  * certain operations.  The exact timing has not yet been
229264Sbill  * determined, but delays are known to be needed when changing
230264Sbill  * the selected drive (by writing in upcs2), and thought to be
231264Sbill  * needed after operations like PRESET and DCLR.  The following
232264Sbill  * variables control the delay, DELAY(n) is approximately n usec.
233264Sbill  */
234264Sbill int	idelay = 500;		/* Delay after PRESET or DCLR */
235268Sbill int	sdelay = 150;		/* Delay after selecting drive in upcs2 */
236275Sbill int	rdelay = 100;		/* Delay after SEARCH */
237275Sbill int	asdel = 100;		/* Delay after clearing bit in upas */
238264Sbill 
239275Sbill int	csdel2 = 0;		/* ??? Delay in upstart ??? */
240275Sbill 
241264Sbill #define	DELAY(N)		{ register int d; d = N; while (--d > 0); }
242264Sbill 
243264Sbill int	nwaitcs2;		/* How many sdelay loops ? */
244264Sbill int	neasycs2;		/* How many sdelay loops not needed ? */
245264Sbill 
246264Sbill #ifdef INTRLVE
247264Sbill daddr_t dkblock();
248264Sbill #endif
249264Sbill 
250264Sbill /*
251264Sbill  * Queue an i/o request for a drive, checking first that it is in range.
252264Sbill  *
253264Sbill  * A unit start is issued if the drive is inactive, causing
254264Sbill  * a SEARCH for the correct cylinder/sector.  If the drive is
255264Sbill  * already nearly on the money and the controller is not transferring
256264Sbill  * we kick it to start the transfer.
257264Sbill  */
258264Sbill upstrategy(bp)
259264Sbill register struct buf *bp;
260264Sbill {
261264Sbill 	register struct buf *dp;
262264Sbill 	register unit, xunit;
263264Sbill 	long sz, bn;
264264Sbill 
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) {
334275Sbill 		softas |= 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;
342264Sbill 		DELAY(sdelay);
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;
357275Sbill 		printf("VV done ds %o, er? %o %o %o\n", upaddr->upds, upaddr->uper1, upaddr->uper2, upaddr->uper3);
358268Sbill 		didie = 1;
359264Sbill 	}
360264Sbill 	if ((upaddr->upds & (DPR|MOL)) != (DPR|MOL))
361275Sbill 		goto done;
362266Sbill 	/*
363266Sbill 	 * Do enough of the disk address decoding to determine
364266Sbill 	 * which cylinder and sector the request is on.
365266Sbill 	 * If we are on the correct cylinder and the desired sector
366*308Sbill 	 * lies between upSDIST and upSDIST+upRDIST sectors ahead of us, then
367266Sbill 	 * we don't bother to SEARCH but just begin the transfer asap.
368*308Sbill 	 * Otherwise ask for a interrupt upSDIST sectors ahead.
369266Sbill 	 */
370264Sbill 	bn = dkblock(bp);
371264Sbill 	cn = bp->b_cylin;
372264Sbill 	sn = bn%(NSECT*NTRAC);
373*308Sbill 	sn = (sn+NSECT-upSDIST)%NSECT;
374264Sbill 
375266Sbill 	if (cn - upaddr->updc)
376266Sbill 		goto search;		/* Not on-cylinder */
377275Sbill 	else if (upseek)
378275Sbill 		goto done;		/* Ok just to be on-cylinder */
379264Sbill 	csn = (upaddr->upla>>6) - sn - 1;
380266Sbill 	if (csn < 0)
381264Sbill 		csn += NSECT;
382*308Sbill 	if (csn > NSECT-upRDIST)
383264Sbill 		goto done;
384264Sbill 
385264Sbill search:
386264Sbill 	upaddr->updc = cn;
387275Sbill 	if (upseek)
388275Sbill 		upaddr->upcs1 = IE|SEEK|GO;
389275Sbill 	else {
390275Sbill 		upaddr->upda = sn;
391275Sbill 		upaddr->upcs1 = IE|SEARCH|GO;
392275Sbill 	}
393268Sbill 	didie = 1;
394266Sbill 	/*
395266Sbill 	 * Mark this unit busy.
396266Sbill 	 */
397264Sbill 	unit += DK_N;
398264Sbill 	if (unit <= DK_NMAX) {
399264Sbill 		dk_busy |= 1<<unit;
400264Sbill 		dk_numb[unit]++;
401264Sbill 	}
402275Sbill 	DELAY(rdelay);
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:
433272Sbill 	if (csdel2) DELAY(csdel2);
434266Sbill 	/*
435266Sbill 	 * Pick a drive off the queue of ready drives, and
436266Sbill 	 * perform the first transfer on its queue.
437266Sbill 	 *
438266Sbill 	 * Looping here is completely for the sake of drives which
439266Sbill 	 * are not present and on-line, for which we completely clear the
440266Sbill 	 * request queue.
441266Sbill 	 */
442273Sbill 	if ((dp = uptab.b_actf) == NULL)
443268Sbill 		return (0);
444264Sbill 	if ((bp = dp->b_actf) == NULL) {
445264Sbill 		uptab.b_actf = dp->b_forw;
446264Sbill 		goto loop;
447264Sbill 	}
448266Sbill 	/*
449266Sbill 	 * Mark the controller busy, and multi-part disk address.
450266Sbill 	 * Select the unit on which the i/o is to take place.
451266Sbill 	 */
452264Sbill 	uptab.b_active++;
453264Sbill 	unit = minor(bp->b_dev) & 077;
454264Sbill 	dn = dkunit(bp);
455264Sbill 	bn = dkblock(bp);
456264Sbill 	cn = up_sizes[unit&07].cyloff;
457264Sbill 	cn += bn/(NSECT*NTRAC);
458264Sbill 	sn = bn%(NSECT*NTRAC);
459264Sbill 	tn = sn/NSECT;
460266Sbill 	sn %= NSECT;
461264Sbill 	upaddr = UPADDR;
462264Sbill 	if ((upaddr->upcs2 & 07) != dn) {
463264Sbill 		upaddr->upcs2 = dn;
464275Sbill 		/* DELAY(sdelay);		Provided by ubasetup() */
465264Sbill 		nwaitcs2++;
466264Sbill 	} else
467264Sbill 		neasycs2++;
468275Sbill 	up_ubinfo = ubasetup(bp, 1);	/* Providing delay */
469266Sbill 	/*
470266Sbill 	 * If drive is not present and on-line, then
471266Sbill 	 * get rid of this with an error and loop to get
472266Sbill 	 * rid of the rest of its queued requests.
473266Sbill 	 * (Then on to any other ready drives.)
474266Sbill 	 */
475264Sbill 	if ((upaddr->upds & (DPR|MOL)) != (DPR|MOL)) {
476275Sbill 		printf("!DPR || !MOL, unit %d, ds %o\n", dn, upaddr->upds);
477264Sbill 		uptab.b_active = 0;
478264Sbill 		uptab.b_errcnt = 0;
479264Sbill 		dp->b_actf = bp->av_forw;
480266Sbill 		dp->b_active = 0;
481264Sbill 		bp->b_flags |= B_ERROR;
482264Sbill 		iodone(bp);
483266Sbill 		ubafree(up_ubinfo), up_ubinfo = 0;	/* A funny place ... */
484264Sbill 		goto loop;
485264Sbill 	}
486266Sbill 	/*
487266Sbill 	 * If this is a retry, then with the 16'th retry we
488266Sbill 	 * begin to try offsetting the heads to recover the data.
489266Sbill 	 */
490266Sbill 	if (uptab.b_errcnt >= 16) {
491264Sbill 		upaddr->upof = up_offset[uptab.b_errcnt & 017] | FMT22;
492266Sbill 		upaddr->upcs1 = IE|OFFSET|GO;
493264Sbill 		DELAY(idelay);
494266Sbill 		while (upaddr->upds & PIP)
495264Sbill 			DELAY(25);
496264Sbill 	}
497266Sbill 	/*
498266Sbill 	 * Now set up the transfer, retrieving the high
499266Sbill 	 * 2 bits of the UNIBUS address from the information
500266Sbill 	 * returned by ubasetup() for the cs1 register bits 8 and 9.
501266Sbill 	 */
502264Sbill 	upaddr->updc = cn;
503264Sbill 	upaddr->upda = (tn << 8) + sn;
504264Sbill 	upaddr->upba = up_ubinfo;
505264Sbill 	upaddr->upwc = -bp->b_bcount / sizeof (short);
506266Sbill 	cmd = (up_ubinfo >> 8) & 0x300;
507264Sbill 	if (bp->b_flags & B_READ)
508266Sbill 		cmd |= IE|RCOM|GO;
509264Sbill 	else
510266Sbill 		cmd |= IE|WCOM|GO;
511266Sbill 	upaddr->upcs1 = cmd;
512266Sbill 	/*
513266Sbill 	 * This is a controller busy situation.
514266Sbill 	 * Record in dk slot NUP+DK_N (after last drive)
515266Sbill 	 * unless there aren't that many slots reserved for
516266Sbill 	 * us in which case we record this as a drive busy
517266Sbill 	 * (if there is room for that).
518266Sbill 	 */
519264Sbill 	unit = dn+DK_N;
520264Sbill 	if (NUP+DK_N == DK_NMAX)
521264Sbill 		unit = NUP+DK_N;
522264Sbill 	if (unit <= DK_NMAX) {
523264Sbill 		dk_busy |= 1<<unit;
524264Sbill 		dk_numb[unit]++;
525264Sbill 		dk_wds[unit] += bp->b_bcount>>6;
526264Sbill 	}
527268Sbill 	return (1);
528264Sbill }
529264Sbill 
530264Sbill /*
531264Sbill  * Handle a device interrupt.
532264Sbill  *
533264Sbill  * If the transferring drive needs attention, service it
534264Sbill  * retrying on error or beginning next transfer.
535264Sbill  * Service all other ready drives, calling ustart to transfer
536264Sbill  * their blocks to the ready queue in uptab, and then restart
537264Sbill  * the controller if there is anything to do.
538264Sbill  */
539264Sbill upintr()
540264Sbill {
541264Sbill 	register struct buf *bp, *dp;
542264Sbill 	register unit;
543264Sbill 	register struct device *upaddr = UPADDR;
544264Sbill 	int as = upaddr->upas & 0377;
545272Sbill 	int osoftas;
546268Sbill 	int needie = 1;
547264Sbill 
548276Sbill 	(void) spl6();
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);
558267Sbill printf("as=%d act %d %d %d\n", as, uptab.b_active, uputab[0].b_active, uputab[1].b_active);
559269Sbill 		}
560266Sbill 		/*
561266Sbill 		 * Mark controller or drive not busy, and check for an
562266Sbill 		 * error condition which may have resulted from the transfer.
563266Sbill 		 */
564264Sbill 		dp = uptab.b_actf;
565264Sbill 		bp = dp->b_actf;
566264Sbill 		unit = dkunit(bp);
567264Sbill 		if (DK_N+NUP == DK_NMAX)
568264Sbill 			dk_busy &= ~(1<<(DK_N+NUP));
569264Sbill 		else if (DK_N+unit <= DK_NMAX)
570264Sbill 			dk_busy &= ~(1<<(DK_N+unit));
571275Sbill 		if ((upaddr->upcs2 & 07) != unit) {
572275Sbill 			upaddr->upcs2 = unit;
573275Sbill 			DELAY(sdelay);
574275Sbill 			nwaitcs2++;
575275Sbill 		} else
576275Sbill 			neasycs2++;
577275Sbill 		if (upaddr->upds & ERR) {
578266Sbill 			/*
579266Sbill 			 * An error occurred, indeed.  Select this unit
580266Sbill 			 * to get at the drive status (a SEARCH may have
581266Sbill 			 * intervened to change the selected unit), and
582266Sbill 			 * wait for the command which caused the interrupt
583266Sbill 			 * to complete (DRY).
584266Sbill 			 */
585266Sbill 			while ((upaddr->upds & DRY) == 0)
586264Sbill 				DELAY(25);
587266Sbill 			/*
588266Sbill 			 * After 28 retries (16 w/o servo offsets, and then
589266Sbill 			 * 12 with servo offsets), or if we encountered
590266Sbill 			 * an error because the drive is write-protected,
591266Sbill 			 * give up.  Print an error message on the last 2
592266Sbill 			 * retries before a hard failure.
593266Sbill 			 */
594266Sbill 			if (++uptab.b_errcnt > 28 || upaddr->uper1&WLE)
595264Sbill 				bp->b_flags |= B_ERROR;
596264Sbill 			else
597266Sbill 				uptab.b_active = 0;	/* To force retry */
598266Sbill 			if (uptab.b_errcnt > 27)
599264Sbill 				deverror(bp, upaddr->upcs2, upaddr->uper1);
600266Sbill 			/*
601266Sbill 			 * If this was a correctible ECC error, let upecc
602266Sbill 			 * do the dirty work to correct it.  If upecc
603266Sbill 			 * starts another READ for the rest of the data
604266Sbill 			 * then it returns 1 (having set uptab.b_active).
605266Sbill 			 * Otherwise we are done and fall through to
606266Sbill 			 * finish up.
607266Sbill 			 */
608266Sbill 			if ((upaddr->uper1&(DCK|ECH))==DCK && upecc(upaddr, bp))
609266Sbill 				return;
610266Sbill 			/*
611266Sbill 			 * Clear the drive and, every 4 retries, recalibrate
612266Sbill 			 * to hopefully help clear up seek positioning problems.
613266Sbill 			 */
614264Sbill 			upaddr->upcs1 = TRE|IE|DCLR|GO;
615264Sbill 			DELAY(idelay);
616268Sbill 			needie = 0;
617266Sbill 			if ((uptab.b_errcnt&07) == 4) {
618264Sbill 				upaddr->upcs1 = RECAL|GO|IE;
619264Sbill 				DELAY(idelay);
620264Sbill 				while(upaddr->upds & PIP)
621264Sbill 					DELAY(25);
622264Sbill 			}
623264Sbill 		}
624266Sbill 		/*
625266Sbill 		 * If we are still noted as active, then no
626266Sbill 		 * (further) retries are necessary.
627266Sbill 		 *
628266Sbill 		 * Make sure the correct unit is selected,
629266Sbill 		 * return it to centerline if necessary, and mark
630266Sbill 		 * this i/o complete, starting the next transfer
631266Sbill 		 * on this drive with the upustart routine (if any).
632266Sbill 		 */
633266Sbill 		if (uptab.b_active) {
634266Sbill 			if (uptab.b_errcnt >= 16) {
635266Sbill 				upaddr->upcs1 = RTC|GO|IE;
636264Sbill 				DELAY(idelay);
637266Sbill 				while (upaddr->upds & PIP)
638264Sbill 					DELAY(25);
639268Sbill 				needie = 0;
640264Sbill 			}
641264Sbill 			uptab.b_active = 0;
642264Sbill 			uptab.b_errcnt = 0;
643264Sbill 			uptab.b_actf = dp->b_forw;
644264Sbill 			dp->b_active = 0;
645264Sbill 			dp->b_errcnt = 0;
646264Sbill 			dp->b_actf = bp->av_forw;
647266Sbill 			bp->b_resid = (-upaddr->upwc * sizeof(short));
648275Sbill 			if (bp->b_resid)
649275Sbill 				printf("resid %d ds %o er? %o %o %o\n", bp->b_resid, upaddr->upds,
650275Sbill 				    upaddr->uper1, upaddr->uper2, upaddr->uper3);
651264Sbill 			iodone(bp);
652264Sbill 			if(dp->b_actf)
653268Sbill 				if (upustart(unit))
654268Sbill 					needie = 0;
655264Sbill 		}
656264Sbill 		as &= ~(1<<unit);
657272Sbill 		softas &= ~(1<<unit);
658264Sbill 		ubafree(up_ubinfo), up_ubinfo = 0;
659273Sbill 	} else {
660264Sbill 		if (upaddr->upcs1 & TRE) {
661264Sbill 			upaddr->upcs1 = TRE;
662264Sbill 			DELAY(idelay);
663264Sbill 		}
664264Sbill 	}
665266Sbill 	/*
666266Sbill 	 * If we have a unit with an outstanding SEARCH,
667266Sbill 	 * and the hardware indicates the unit requires attention,
668266Sbill 	 * the bring the drive to the ready queue.
669266Sbill 	 * Finally, if the controller is not transferring
670266Sbill 	 * start it if any drives are now ready to transfer.
671266Sbill 	 */
672272Sbill 	as |= softas;
673272Sbill 	osoftas = softas;
674272Sbill 	softas = 0;
675266Sbill 	for (unit = 0; unit < NUP; unit++)
676273Sbill 		if ((as|osoftas) & (1<<unit)) {
677273Sbill 			if (as & (1<<unit)) {
678267Sbill 				upaddr->upas = 1<<unit;
679268Sbill 				if (asdel) DELAY(asdel);
680272Sbill 			}
681273Sbill 			if (upustart(unit))
682273Sbill 				needie = 0;
683273Sbill 		}
684266Sbill 	if (uptab.b_actf && uptab.b_active == 0)
685268Sbill 		if (upstart())
686268Sbill 			needie = 0;
687266Sbill out:
688275Sbill 	if (needie)
689266Sbill 		upaddr->upcs1 = IE;
690264Sbill }
691264Sbill 
692264Sbill upread(dev)
693264Sbill {
694264Sbill 
695264Sbill 	physio(upstrategy, &rupbuf, dev, B_READ, minphys);
696264Sbill }
697264Sbill 
698264Sbill upwrite(dev)
699264Sbill {
700264Sbill 
701264Sbill 	physio(upstrategy, &rupbuf, dev, B_WRITE, minphys);
702264Sbill }
703264Sbill 
704266Sbill /*
705266Sbill  * Correct an ECC error, and restart the i/o to complete
706266Sbill  * the transfer if necessary.  This is quite complicated because
707266Sbill  * the transfer may be going to an odd memory address base and/or
708266Sbill  * across a page boundary.
709266Sbill  */
710264Sbill upecc(up, bp)
711264Sbill register struct device *up;
712264Sbill register struct buf *bp;
713264Sbill {
714264Sbill 	struct uba_regs *ubp = (struct uba_regs *)UBA0;
715266Sbill 	register int i;
716264Sbill 	caddr_t addr;
717266Sbill 	int reg, bit, byte, npf, mask, o, cmd, ubaddr;
718264Sbill 	int bn, cn, tn, sn;
719264Sbill 
720264Sbill 	/*
721266Sbill 	 * Npf is the number of sectors transferred before the sector
722266Sbill 	 * containing the ECC error, and reg is the UBA register
723266Sbill 	 * mapping (the first part of) the transfer.
724266Sbill 	 * O is offset within a memory page of the first byte transferred.
725264Sbill 	 */
726266Sbill 	npf = btop((up->upwc * sizeof(short)) + bp->b_bcount) - 1;
727266Sbill 	reg = btop(up_ubinfo&0x3ffff) + npf;
728264Sbill 	o = (int)bp->b_un.b_addr & PGOFSET;
729264Sbill 	printf("%D ", bp->b_blkno+npf);
730264Sbill 	prdev("ECC", bp->b_dev);
731264Sbill 	mask = up->upec2;
732264Sbill 	if (mask == 0) {
733266Sbill 		up->upof = FMT22;		/* == RTC ???? */
734264Sbill 		DELAY(idelay);
735264Sbill 		return (0);
736264Sbill 	}
737266Sbill 	/*
738266Sbill 	 * Flush the buffered data path, and compute the
739266Sbill 	 * byte and bit position of the error.  The variable i
740266Sbill 	 * is the byte offset in the transfer, the variable byte
741266Sbill 	 * is the offset from a page boundary in main memory.
742266Sbill 	 */
743266Sbill 	ubp->uba_dpr[(up_ubinfo>>28)&0x0f] |= BNE;
744266Sbill 	i = up->upec1 - 1;		/* -1 makes 0 origin */
745266Sbill 	bit = i&07;
746266Sbill 	i = (i&~07)>>3;
747264Sbill 	byte = i + o;
748266Sbill 	/*
749266Sbill 	 * Correct while possible bits remain of mask.  Since mask
750266Sbill 	 * contains 11 bits, we continue while the bit offset is > -11.
751266Sbill 	 * Also watch out for end of this block and the end of the whole
752266Sbill 	 * transfer.
753266Sbill 	 */
754266Sbill 	while (i < 512 && (int)ptob(npf)+i < bp->b_bcount && bit > -11) {
755266Sbill 		addr = ptob(ubp->uba_map[reg+btop(byte)].pg_pfnum)+
756266Sbill 		    (byte & PGOFSET);
757266Sbill 		putmemc(addr, getmemc(addr)^(mask<<bit));
758266Sbill 		byte++;
759266Sbill 		i++;
760266Sbill 		bit -= 8;
761264Sbill 	}
762266Sbill 	uptab.b_active++;	/* Either complete or continuing... */
763264Sbill 	if (up->upwc == 0)
764264Sbill 		return (0);
765266Sbill 	/*
766266Sbill 	 * Have to continue the transfer... clear the drive,
767266Sbill 	 * and compute the position where the transfer is to continue.
768266Sbill 	 * We have completed npf+1 sectors of the transfer already;
769266Sbill 	 * restart at offset o of next sector (i.e. in UBA register reg+1).
770266Sbill 	 */
771266Sbill 	up->upcs1 = TRE|IE|DCLR|GO;
772264Sbill 	DELAY(idelay);
773264Sbill 	bn = dkblock(bp);
774264Sbill 	cn = bp->b_cylin;
775266Sbill 	sn = bn%(NSECT*NTRAC) + npf + 1;
776264Sbill 	tn = sn/NSECT;
777264Sbill 	sn %= NSECT;
778266Sbill 	cn += tn/NTRAC;
779266Sbill 	tn %= NTRAC;
780264Sbill 	up->updc = cn;
781266Sbill 	up->upda = (tn << 8) | sn;
782266Sbill 	ubaddr = (int)ptob(reg+1) + o;
783266Sbill 	up->upba = ubaddr;
784266Sbill 	cmd = (ubaddr >> 8) & 0x300;
785266Sbill 	cmd |= IE|GO|RCOM;
786266Sbill 	up->upcs1 = cmd;
787264Sbill 	return (1);
788264Sbill }
789286Sbill 
790286Sbill /*
791286Sbill  * Reset driver after UBA init.
792286Sbill  * Cancel software state of all pending transfers
793286Sbill  * and restart all units and the controller.
794286Sbill  */
795286Sbill upreset()
796286Sbill {
797286Sbill 	int unit;
798286Sbill 
799286Sbill 	printf(" up");
800286Sbill 	uptab.b_active = 0;
801286Sbill 	uptab.b_actf = uptab.b_actl = 0;
802286Sbill 	if (DK_N+NUP == DK_NMAX)
803286Sbill 		dk_busy &= ~(1<<(DK_N+NUP));
804286Sbill 	if (up_ubinfo) {
805286Sbill 		printf("<%d>", (up_ubinfo>>28)&0xf);
806286Sbill 		ubafree(up_ubinfo), up_ubinfo = 0;
807286Sbill 	}
808286Sbill 	for (unit = 0; unit < NUP; unit++) {
809286Sbill 		uputab[unit].b_active = 0;
810286Sbill 		(void) upustart(unit);
811286Sbill 	}
812286Sbill 	(void) upstart();
813286Sbill }
814