xref: /csrg-svn/sys/vax/uba/uda.c (revision 12421)
1*12421Ssam /*	uda.c	4.21	83/05/12	*/
24743Swnj 
34743Swnj #include "ra.h"
44743Swnj #if NUDA > 0
54743Swnj /*
64743Swnj  * UDA50/RAxx disk device driver
74743Swnj  *
84743Swnj  * Restrictions:
94743Swnj  *	Unit numbers must be less than 8.
104743Swnj  *
114743Swnj  * TO DO:
124743Swnj  *	write dump code
134743Swnj  */
149781Ssam #include "../machine/pte.h"
154743Swnj 
164743Swnj #include "../h/param.h"
174743Swnj #include "../h/systm.h"
184743Swnj #include "../h/buf.h"
194743Swnj #include "../h/conf.h"
204743Swnj #include "../h/dir.h"
214743Swnj #include "../h/user.h"
224743Swnj #include "../h/map.h"
234743Swnj #include "../h/vm.h"
244743Swnj #include "../h/dk.h"
254743Swnj #include "../h/cmap.h"
267734Sroot #include "../h/uio.h"
274743Swnj 
288482Sroot #include "../vax/cpu.h"
298482Sroot #include "../vaxuba/ubareg.h"
308482Sroot #include "../vaxuba/ubavar.h"
318613Sroot 
328613Sroot #define	NRSPL2	3		/* log2 number of response packets */
338613Sroot #define	NCMDL2	3		/* log2 number of command packets */
348613Sroot #define	NRSP	(1<<NRSPL2)
358613Sroot #define	NCMD	(1<<NCMDL2)
368613Sroot 
378482Sroot #include "../vaxuba/udareg.h"
388482Sroot #include "../vax/mscp.h"
394743Swnj 
404743Swnj struct uda_softc {
414743Swnj 	short	sc_state;	/* state of controller */
424743Swnj 	short	sc_mapped;	/* Unibus map allocated for uda struct? */
434743Swnj 	int	sc_ubainfo;	/* Unibus mapping info */
444743Swnj 	struct uda *sc_uda;	/* Unibus address of uda struct */
454743Swnj 	int	sc_ivec;	/* interrupt vector address */
464743Swnj 	short	sc_credits;	/* transfer credits */
474743Swnj 	short	sc_lastcmd;	/* pointer into command ring */
484743Swnj 	short	sc_lastrsp;	/* pointer into response ring */
494743Swnj } uda_softc[NUDA];
504743Swnj 
514743Swnj /*
524743Swnj  * Controller states
534743Swnj  */
544743Swnj #define	S_IDLE	0		/* hasn't been initialized */
554743Swnj #define	S_STEP1	1		/* doing step 1 init */
564743Swnj #define	S_STEP2	2		/* doing step 2 init */
574743Swnj #define	S_STEP3	3		/* doing step 3 init */
584743Swnj #define	S_SCHAR	4		/* doing "set controller characteristics" */
594743Swnj #define	S_RUN	5		/* running */
604743Swnj 
614743Swnj struct uda {
624743Swnj 	struct udaca	uda_ca;		/* communications area */
634743Swnj 	struct mscp	uda_rsp[NRSP];	/* response packets */
644743Swnj 	struct mscp	uda_cmd[NCMD];	/* command packets */
654743Swnj } uda[NUDA];
664743Swnj 
674743Swnj /* THIS SHOULD BE READ OFF THE PACK, PER DRIVE */
684743Swnj struct size {
694743Swnj 	daddr_t	nblocks;
704743Swnj 	daddr_t	blkoff;
714743Swnj } ra_sizes[8] ={
72*12421Ssam #ifdef notdef
734743Swnj 	15884,	0,		/* A=blk 0 thru 15883 */
744743Swnj 	33440,	15884,		/* B=blk 15884 thru 49323 */
754743Swnj 	-1,	0,		/* C=blk 0 thru end */
766964Ssam 	15884,	340670,		/* D=blk 340670 thru 356553 */
776964Ssam 	55936,	356554,		/* E=blk 356554 thru 412489 */
786964Ssam 	-1,	412490,		/* F=blk 412490 thru end */
794743Swnj 	82080,	49324,		/* G=blk 49324 thru 131403 */
804743Swnj 	-1,	131404,		/* H=blk 131404 thru end */
81*12421Ssam } ra81_sizes[8] = {
82*12421Ssam #endif
83*12421Ssam 	15884,	0,		/* A=cyl 0 thru 22 */
84*12421Ssam 	66880,	16422,		/* B=cyl 23 thru 116 */
85*12421Ssam 	-1,	0,		/* C=cyl 0 thru end */
86*12421Ssam 	15884,	375564,		/* D=cyl 526 thru 548 */
87*12421Ssam 	307200,	391986,		/* E=cyl 549 thru 979 */
88*12421Ssam 	-1,	699720,		/* F=cyl 980 thru end */
89*12421Ssam 	-1,	375564,		/* G=cyl 526 thru end */
90*12421Ssam 	291346,	83538,		/* H=cyl 117 thru 525 */
914743Swnj };
924743Swnj /* END OF STUFF WHICH SHOULD BE READ IN PER DISK */
934743Swnj 
94*12421Ssam int	udaerror = 0;			/* causes hex dump of packets */
95*12421Ssam int	udadebug = 0;
96*12421Ssam #define	printd	if (udadebug) printf
97*12421Ssam 
984743Swnj daddr_t	radsize[NRA];			/* disk size, from ONLINE end packet */
994743Swnj 
1004743Swnj int	udprobe(), udslave(), udattach(), udintr();
1014743Swnj struct	mscp *udgetcp();
1024743Swnj struct	uba_ctlr *udminfo[NUDA];
1034743Swnj struct	uba_device *uddinfo[NRA];
1044743Swnj struct	uba_device *udip[NUDA][8];	/* 8 == max number of drives */
1054743Swnj 
10611243Ssam u_short	udstd[] = { 0772150, 0772550, 0777550, 0 };
1074743Swnj struct	uba_driver udadriver =
1084743Swnj  { udprobe, udslave, udattach, 0, udstd, "ra", uddinfo, "uda", udminfo, 0 };
1094743Swnj struct	buf rudbuf[NRA];
1104743Swnj struct	buf udutab[NRA];
1114743Swnj struct	buf udwtab[NUDA];		/* I/O wait queue, per controller */
1124743Swnj 
1134743Swnj #define	b_qsize		b_resid		/* queue size per drive, in udutab */
1144743Swnj #define	b_ubinfo	b_resid		/* Unibus mapping info, per buffer */
1154743Swnj 
1164743Swnj udprobe(reg, ctlr)
1174743Swnj 	caddr_t reg;
1184743Swnj 	int ctlr;
1194743Swnj {
1204743Swnj 	register int br, cvec;
1214743Swnj 	register struct uda_softc *sc = &uda_softc[ctlr];
1224743Swnj 
1234743Swnj #ifdef lint
1246187Ssam 	br = 0; cvec = br; br = cvec; reg = reg;
125*12421Ssam 	udread(0); udwrite(0); udreset(0); udintr(0);
1264743Swnj #endif
1274743Swnj 	/* SHOULD CHECK THAT IT REALLY IS A UDA */
1284743Swnj 	br = 0x15;
1294743Swnj 	cvec = sc->sc_ivec = (uba_hd[numuba].uh_lastiv -= 4);
1307410Skre 	return(sizeof (struct udadevice));
1314743Swnj }
1324743Swnj 
1334743Swnj udslave(ui, reg)
1344743Swnj 	struct uba_device *ui;
1354743Swnj 	caddr_t reg;
1364743Swnj {
1374743Swnj 	/*
1384743Swnj 	 * TOO HARD TO FIND OUT IF DISK IS THERE UNTIL
1394743Swnj 	 * INITIALIZED.  WE'LL FIND OUT WHEN WE FIRST
1404743Swnj 	 * TRY TO ACCESS IT.
1414743Swnj 	 */
1426187Ssam #ifdef lint
1436187Ssam 	ui = ui; reg = reg;
1446187Ssam #endif
1454743Swnj 	return(1);
1464743Swnj }
1474743Swnj 
1484743Swnj udattach(ui)
1494743Swnj 	register struct uba_device *ui;
1504743Swnj {
1514743Swnj 
1524743Swnj 	if (ui->ui_dk > 0)
1534743Swnj 		dk_mspw[ui->ui_dk] = 1.0 / (60 * 31 * 256);	/* approx */
1544743Swnj 	ui->ui_flags = 0;
1554743Swnj 	udip[ui->ui_ctlr][ui->ui_slave] = ui;
1564743Swnj 	radsize[ui->ui_unit] = (daddr_t)0xffffff;	/* max possible size */
1574743Swnj }
1584743Swnj 
1594743Swnj /*
1604743Swnj  * Open a UDA.  Initialize the device and
1614743Swnj  * set the unit online.
1624743Swnj  */
1634743Swnj udopen(dev, flag)
1644743Swnj 	dev_t dev;
1654743Swnj 	int flag;
1664743Swnj {
1674743Swnj 	register int unit;
1684743Swnj 	register struct uba_device *ui;
1694743Swnj 	register struct uda_softc *sc;
1705434Sroot 	int s;
1714743Swnj 
1726187Ssam #ifdef lint
1736187Ssam 	flag = flag;
1746187Ssam #endif
1754743Swnj 	unit = minor(dev) >> 3;
1768576Sroot 	if (unit >= NRA || (ui = uddinfo[unit]) == 0 || ui->ui_alive == 0)
1778576Sroot 		return (ENXIO);
1784743Swnj 	sc = &uda_softc[ui->ui_ctlr];
1795434Sroot 	s = spl5();
1804743Swnj 	if (sc->sc_state != S_RUN) {
1814743Swnj 		if (sc->sc_state == S_IDLE)
1824743Swnj 			udinit(ui->ui_ctlr);
1836187Ssam 		/* wait for initialization to complete */
1846187Ssam 		sleep((caddr_t)ui->ui_mi, 0);
1858576Sroot 		if (sc->sc_state != S_RUN)
1868576Sroot 			return (EIO);
1874743Swnj 	}
1885434Sroot 	splx(s);
1894743Swnj 	/* SHOULD PROBABLY FORCE AN ONLINE ATTEMPT
1904743Swnj 	   TO SEE IF DISK IS REALLY THERE */
1918576Sroot 	return (0);
1924743Swnj }
1934743Swnj 
1944743Swnj /*
1954743Swnj  * Initialize a UDA.  Set up UBA mapping registers,
1964743Swnj  * initialize data structures, and start hardware
1974743Swnj  * initialization sequence.
1984743Swnj  */
1994743Swnj udinit(d)
2004743Swnj 	int d;
2014743Swnj {
2024743Swnj 	register struct uda_softc *sc;
2034743Swnj 	register struct uda *ud;
2044743Swnj 	struct udadevice *udaddr;
2054743Swnj 	struct uba_ctlr *um;
2064743Swnj 
2074743Swnj 	sc = &uda_softc[d];
2084743Swnj 	um = udminfo[d];
2094743Swnj 	um->um_tab.b_active++;
2104743Swnj 	ud = &uda[d];
2114743Swnj 	udaddr = (struct udadevice *)um->um_addr;
2124743Swnj 	if (sc->sc_mapped == 0) {
2134743Swnj 		/*
2144743Swnj 		 * Map the communications area and command
2154743Swnj 		 * and response packets into Unibus address
2164743Swnj 		 * space.
2174743Swnj 		 */
2184743Swnj 		sc->sc_ubainfo = uballoc(um->um_ubanum, (caddr_t)ud,
2194743Swnj 		    sizeof (struct uda), 0);
2204743Swnj 		sc->sc_uda = (struct uda *)(sc->sc_ubainfo & 0x3ffff);
2214743Swnj 		sc->sc_mapped = 1;
2224743Swnj 	}
2234743Swnj 
2244743Swnj 	/*
2254743Swnj 	 * Start the hardware initialization sequence.
2264743Swnj 	 */
2274743Swnj 	udaddr->udaip = 0;		/* start initialization */
2284743Swnj 	while ((udaddr->udasa & UDA_STEP1) == 0)
2294743Swnj 		;
2304743Swnj 	udaddr->udasa = UDA_ERR|(NCMDL2<<11)|(NRSPL2<<8)|UDA_IE|(sc->sc_ivec/4);
2314743Swnj 	/*
2324743Swnj 	 * Initialization continues in interrupt routine.
2334743Swnj 	 */
2344743Swnj 	sc->sc_state = S_STEP1;
2354743Swnj 	sc->sc_credits = 0;
2364743Swnj }
2374743Swnj 
2384743Swnj udstrategy(bp)
2394743Swnj 	register struct buf *bp;
2404743Swnj {
2414743Swnj 	register struct uba_device *ui;
2424743Swnj 	register struct uba_ctlr *um;
2434743Swnj 	register struct buf *dp;
2444743Swnj 	register int unit;
2454743Swnj 	int xunit = minor(bp->b_dev) & 07;
2464743Swnj 	daddr_t sz, maxsz;
2475434Sroot 	int s;
2484743Swnj 
2494743Swnj 	sz = (bp->b_bcount+511) >> 9;
2504743Swnj 	unit = dkunit(bp);
2514743Swnj 	if (unit >= NRA)
2524743Swnj 		goto bad;
2534743Swnj 	ui = uddinfo[unit];
2544743Swnj 	um = ui->ui_mi;
2554743Swnj 	if (ui == 0 || ui->ui_alive == 0)
2564743Swnj 		goto bad;
2574743Swnj 	if ((maxsz = ra_sizes[xunit].nblocks) < 0)
2584743Swnj 		maxsz = radsize[unit] - ra_sizes[xunit].blkoff;
2594743Swnj 	if (bp->b_blkno < 0 || bp->b_blkno+sz > maxsz ||
2604743Swnj 	    ra_sizes[xunit].blkoff >= radsize[unit])
2614743Swnj 		goto bad;
2625434Sroot 	s = spl5();
2634743Swnj 	/*
2644743Swnj 	 * Link the buffer onto the drive queue
2654743Swnj 	 */
2664743Swnj 	dp = &udutab[ui->ui_unit];
2674743Swnj 	if (dp->b_actf == 0)
2684743Swnj 		dp->b_actf = bp;
2694743Swnj 	else
2704743Swnj 		dp->b_actl->av_forw = bp;
2714743Swnj 	dp->b_actl = bp;
2724743Swnj 	bp->av_forw = 0;
2734743Swnj 	/*
2744743Swnj 	 * Link the drive onto the controller queue
2754743Swnj 	 */
2764743Swnj 	if (dp->b_active == 0) {
2774743Swnj 		dp->b_forw = NULL;
2784743Swnj 		if (um->um_tab.b_actf == NULL)
2794743Swnj 			um->um_tab.b_actf = dp;
2804743Swnj 		else
2814743Swnj 			um->um_tab.b_actl->b_forw = dp;
2824743Swnj 		um->um_tab.b_actl = dp;
2834743Swnj 		dp->b_active = 1;
2844743Swnj 	}
2854743Swnj 	if (um->um_tab.b_active == 0) {
2864743Swnj #if defined(VAX750)
287*12421Ssam 		if (cpu == VAX_750
288*12421Ssam 		    && udwtab[um->um_ctlr].av_forw == &udwtab[um->um_ctlr]) {
2894743Swnj 			if (um->um_ubinfo != 0)
290*12421Ssam 				printf("udastrat: ubinfo 0x%x\n",um->um_ubinfo);
2914743Swnj 			else
2924743Swnj 				um->um_ubinfo =
293*12421Ssam 				   uballoc(um->um_ubanum, (caddr_t)0, 0,
2946187Ssam 					UBA_NEEDBDP);
2954743Swnj 		}
2964743Swnj #endif
2974743Swnj 		(void) udstart(um);
2984743Swnj 	}
2995434Sroot 	splx(s);
3004743Swnj 	return;
3014743Swnj 
3024743Swnj bad:
3034743Swnj 	bp->b_flags |= B_ERROR;
3044743Swnj 	iodone(bp);
3054743Swnj 	return;
3064743Swnj }
3074743Swnj 
3084743Swnj udstart(um)
3094743Swnj 	register struct uba_ctlr *um;
3104743Swnj {
3114743Swnj 	register struct buf *bp, *dp;
3124743Swnj 	register struct mscp *mp;
3134743Swnj 	register struct uda_softc *sc;
3144743Swnj 	register struct uba_device *ui;
3154743Swnj 	struct udadevice *udaddr;
3164743Swnj 	int i;
3174743Swnj 
3184743Swnj 	sc = &uda_softc[um->um_ctlr];
3194743Swnj 
3204743Swnj loop:
3214743Swnj 	if ((dp = um->um_tab.b_actf) == NULL) {
3224743Swnj 		/*
3234743Swnj 		 * Release uneeded UBA resources and return
3244743Swnj 		 */
3254743Swnj 		um->um_tab.b_active = 0;
3266187Ssam 		return (0);
3274743Swnj 	}
3284743Swnj 	if ((bp = dp->b_actf) == NULL) {
3294743Swnj 		/*
3304743Swnj 		 * No more requests for this drive, remove
3314743Swnj 		 * from controller queue and look at next drive.
3324743Swnj 		 * We know we're at the head of the controller queue.
3334743Swnj 		 */
3344743Swnj 		dp->b_active = 0;
3354743Swnj 		um->um_tab.b_actf = dp->b_forw;
3364743Swnj 		goto loop;
3374743Swnj 	}
3384743Swnj 	um->um_tab.b_active++;
3394743Swnj 	udaddr = (struct udadevice *)um->um_addr;
3404743Swnj 	if ((udaddr->udasa&UDA_ERR) || sc->sc_state != S_RUN) {
3414743Swnj 		harderr(bp, "ra");
3424743Swnj 		printf("udasa %o, state %d\n", udaddr->udasa&0xffff, sc->sc_state);
3434743Swnj 		udinit(um->um_ctlr);
3444743Swnj 		/* SHOULD REQUEUE OUTSTANDING REQUESTS, LIKE UDRESET */
3456187Ssam 		return (0);
3464743Swnj 	}
3474743Swnj 	ui = uddinfo[dkunit(bp)];
3484743Swnj 	/*
3494743Swnj 	 * If no credits, can't issue any commands
3504743Swnj 	 * until some outstanding commands complete.
3514743Swnj 	 */
3524743Swnj 	if (sc->sc_credits < 2)
3536187Ssam 		return (0);
3544743Swnj 	if ((mp = udgetcp(um)) == NULL)
3556187Ssam 		return (0);
3564743Swnj 	sc->sc_credits--;	/* committed to issuing a command */
3574743Swnj 	if (ui->ui_flags == 0) {	/* not online */
3584743Swnj 		mp->mscp_opcode = M_OP_ONLIN;
3594743Swnj 		mp->mscp_unit = ui->ui_slave;
3604743Swnj 		dp->b_active = 2;
3614743Swnj 		um->um_tab.b_actf = dp->b_forw;	/* remove from controller q */
3624743Swnj 		printd("uda: bring unit %d online\n", ui->ui_slave);
3634743Swnj 		*((long *)mp->mscp_dscptr) |= UDA_OWN|UDA_INT;
3644743Swnj 		i = udaddr->udaip;
3654743Swnj 		goto loop;
3664743Swnj 	}
3674743Swnj 	switch (cpu) {
3684743Swnj 	case VAX_780:
3694743Swnj 		i = UBA_NEEDBDP|UBA_CANTWAIT;
3704743Swnj 		break;
3714743Swnj 
3724743Swnj 	case VAX_750:
3734743Swnj 		i = um->um_ubinfo|UBA_HAVEBDP|UBA_CANTWAIT;
3744743Swnj 		break;
3754743Swnj 
3766949Ssam 	case VAX_730:
3774743Swnj 		i = UBA_CANTWAIT;
3784743Swnj 		break;
3794743Swnj 	}
3804743Swnj 	if ((i = ubasetup(um->um_ubanum, bp, i)) == 0) {
3814743Swnj 		mp->mscp_opcode = M_OP_GTUNT;
3824743Swnj 		mp->mscp_unit = ui->ui_slave;
3834743Swnj 		*((long *)mp->mscp_dscptr) |= UDA_OWN|UDA_INT;
3844743Swnj 		i = udaddr->udaip;	/* initiate polling */
3854743Swnj 		return(1);		/* wait for interrupt */
3864743Swnj 	}
3874743Swnj 	mp->mscp_cmdref = (long)bp;	/* pointer to get back */
3884743Swnj 	mp->mscp_opcode = bp->b_flags&B_READ ? M_OP_READ : M_OP_WRITE;
3894743Swnj 	mp->mscp_unit = ui->ui_slave;
3904743Swnj 	mp->mscp_lbn = bp->b_blkno + ra_sizes[minor(bp->b_dev)&7].blkoff;
3914743Swnj 	mp->mscp_bytecnt = bp->b_bcount;
3924743Swnj 	mp->mscp_buffer = (i & 0x3ffff) | (((i>>28)&0xf)<<24);
3934743Swnj #if defined(VAX750)
3944743Swnj 	if (cpu == VAX_750)
3954743Swnj 		i &= 0xfffffff;		/* mask off bdp */
3964743Swnj #endif
3974743Swnj 	bp->b_ubinfo = i;		/* save mapping info */
3984743Swnj 	*((long *)mp->mscp_dscptr) |= UDA_OWN|UDA_INT;
3994743Swnj 	i = udaddr->udaip;		/* initiate polling */
4004743Swnj 	if (ui->ui_dk >= 0) {
4014743Swnj 		dk_busy |= 1<<ui->ui_dk;
4024743Swnj 		dp->b_qsize++;
4034743Swnj 		dk_xfer[ui->ui_dk]++;
4044743Swnj 		dk_wds[ui->ui_dk] += bp->b_bcount>>6;
4054743Swnj 	}
4064743Swnj 
4074743Swnj 	/*
4084743Swnj 	 * Move drive to the end of the controller queue
4094743Swnj 	 */
4104743Swnj 	if (dp->b_forw != NULL) {
4114743Swnj 		um->um_tab.b_actf = dp->b_forw;
4124743Swnj 		um->um_tab.b_actl->b_forw = dp;
4134743Swnj 		um->um_tab.b_actl = dp;
4144743Swnj 		dp->b_forw = NULL;
4154743Swnj 	}
4164743Swnj 	/*
4174743Swnj 	 * Move buffer to I/O wait queue
4184743Swnj 	 */
4194743Swnj 	dp->b_actf = bp->av_forw;
4204743Swnj 	dp = &udwtab[um->um_ctlr];
4214743Swnj 	bp->av_forw = dp;
4224743Swnj 	bp->av_back = dp->av_back;
4234743Swnj 	dp->av_back->av_forw = bp;
4244743Swnj 	dp->av_back = bp;
4254743Swnj 	goto loop;
4264743Swnj }
4274743Swnj 
4284743Swnj /*
4294743Swnj  * UDA interrupt routine.
4304743Swnj  */
4314743Swnj udintr(d)
4324743Swnj 	int d;
4334743Swnj {
4344743Swnj 	register struct uba_ctlr *um = udminfo[d];
4354743Swnj 	register struct udadevice *udaddr = (struct udadevice *)um->um_addr;
4364743Swnj 	struct buf *bp;
4374743Swnj 	register int i;
4384743Swnj 	register struct uda_softc *sc = &uda_softc[d];
4394743Swnj 	register struct uda *ud = &uda[d];
4404743Swnj 	struct uda *uud;
4414743Swnj 	struct mscp *mp;
4424743Swnj 
4434743Swnj 	printd("udintr: state %d, udasa %o\n", sc->sc_state, udaddr->udasa);
4444743Swnj 	switch (sc->sc_state) {
4454743Swnj 	case S_IDLE:
4464743Swnj 		printf("uda%d: random interrupt ignored\n", d);
4474743Swnj 		return;
4484743Swnj 
4494743Swnj 	case S_STEP1:
4506964Ssam #define	STEP1MASK	0174377
4514743Swnj #define	STEP1GOOD	(UDA_STEP2|UDA_IE|(NCMDL2<<3)|NRSPL2)
4526964Ssam 		if ((udaddr->udasa&STEP1MASK) != STEP1GOOD) {
4534743Swnj 			sc->sc_state = S_IDLE;
4546187Ssam 			wakeup((caddr_t)um);
4554743Swnj 			return;
4564743Swnj 		}
4574743Swnj 		udaddr->udasa = ((int)&sc->sc_uda->uda_ca.ca_ringbase)|
4584743Swnj 		    (cpu == VAX_780 ? UDA_PI : 0);
4594743Swnj 		sc->sc_state = S_STEP2;
4604743Swnj 		return;
4614743Swnj 
4624743Swnj 	case S_STEP2:
4636964Ssam #define	STEP2MASK	0174377
4644743Swnj #define	STEP2GOOD	(UDA_STEP3|UDA_IE|(sc->sc_ivec/4))
4656964Ssam 		if ((udaddr->udasa&STEP2MASK) != STEP2GOOD) {
4664743Swnj 			sc->sc_state = S_IDLE;
4676187Ssam 			wakeup((caddr_t)um);
4684743Swnj 			return;
4694743Swnj 		}
4704743Swnj 		udaddr->udasa = ((int)&sc->sc_uda->uda_ca.ca_ringbase)>>16;
4714743Swnj 		sc->sc_state = S_STEP3;
4724743Swnj 		return;
4734743Swnj 
4744743Swnj 	case S_STEP3:
4756964Ssam #define	STEP3MASK	0174000
4764743Swnj #define	STEP3GOOD	UDA_STEP4
4776964Ssam 		if ((udaddr->udasa&STEP3MASK) != STEP3GOOD) {
4784743Swnj 			sc->sc_state = S_IDLE;
4796187Ssam 			wakeup((caddr_t)um);
4804743Swnj 			return;
4814743Swnj 		}
4824743Swnj 		udaddr->udasa = UDA_GO;
4834743Swnj 		sc->sc_state = S_SCHAR;
4844743Swnj 
4854743Swnj 		/*
4864743Swnj 		 * Initialize the data structures.
4874743Swnj 		 */
4884743Swnj 		uud = sc->sc_uda;
4894743Swnj 		for (i = 0; i < NRSP; i++) {
4904743Swnj 			ud->uda_ca.ca_rspdsc[i] = UDA_OWN|UDA_INT|
4914743Swnj 				(long)&uud->uda_rsp[i].mscp_cmdref;
4924743Swnj 			ud->uda_rsp[i].mscp_dscptr = &ud->uda_ca.ca_rspdsc[i];
4934743Swnj 			ud->uda_rsp[i].mscp_header.uda_msglen = sizeof (struct mscp);
4944743Swnj 		}
4954743Swnj 		for (i = 0; i < NCMD; i++) {
4964743Swnj 			ud->uda_ca.ca_cmddsc[i] = UDA_INT|
4974743Swnj 				(long)&uud->uda_cmd[i].mscp_cmdref;
4984743Swnj 			ud->uda_cmd[i].mscp_dscptr = &ud->uda_ca.ca_cmddsc[i];
4994743Swnj 			ud->uda_cmd[i].mscp_header.uda_msglen = sizeof (struct mscp);
5004743Swnj 		}
5014743Swnj 		bp = &udwtab[d];
5024743Swnj 		bp->av_forw = bp->av_back = bp;
5034743Swnj 		sc->sc_lastcmd = 0;
5044743Swnj 		sc->sc_lastrsp = 0;
5054743Swnj 		if ((mp = udgetcp(um)) == NULL) {
5064743Swnj 			sc->sc_state = S_IDLE;
5076187Ssam 			wakeup((caddr_t)um);
5084743Swnj 			return;
5094743Swnj 		}
5104743Swnj 		mp->mscp_opcode = M_OP_STCON;
5114743Swnj 		mp->mscp_cntflgs = M_CF_ATTN|M_CF_MISC|M_CF_THIS;
5124743Swnj 		*((long *)mp->mscp_dscptr) |= UDA_OWN|UDA_INT;
5134743Swnj 		i = udaddr->udaip;	/* initiate polling */
5144743Swnj 		return;
5154743Swnj 
5164743Swnj 	case S_SCHAR:
5174743Swnj 	case S_RUN:
5184743Swnj 		break;
5194743Swnj 
5204743Swnj 	default:
5214743Swnj 		printf("uda%d: interrupt in unknown state %d ignored\n",
5224743Swnj 			d, sc->sc_state);
5234743Swnj 		return;
5244743Swnj 	}
5254743Swnj 
5264743Swnj 	if (udaddr->udasa&UDA_ERR) {
5274743Swnj 		printf("uda%d: fatal error (%o)\n", d, udaddr->udasa&0xffff);
5284743Swnj 		udaddr->udaip = 0;
5296187Ssam 		wakeup((caddr_t)um);
5304743Swnj 	}
5314743Swnj 
5324743Swnj 	/*
5334743Swnj 	 * Check for a buffer purge request.
5344743Swnj 	 */
5354743Swnj 	if (ud->uda_ca.ca_bdp) {
5364743Swnj 		/*
5374743Swnj 		 * THIS IS A KLUDGE.
5384743Swnj 		 * Maybe we should change the entire
5394743Swnj 		 * UBA interface structure.
5404743Swnj 		 */
5414743Swnj 		int s = spl7();
5424743Swnj 
5434743Swnj 		i = um->um_ubinfo;
5444743Swnj 		printd("uda: purge bdp %d\n", ud->uda_ca.ca_bdp);
5454743Swnj 		um->um_ubinfo = ud->uda_ca.ca_bdp<<28;
5464743Swnj 		ubapurge(um);
5474743Swnj 		um->um_ubinfo = i;
5484743Swnj 		(void) splx(s);
5494743Swnj 		ud->uda_ca.ca_bdp = 0;
5504743Swnj 		udaddr->udasa = 0;	/* signal purge complete */
5514743Swnj 	}
5524743Swnj 
5534743Swnj 	/*
5544743Swnj 	 * Check for response ring transition.
5554743Swnj 	 */
5564743Swnj 	if (ud->uda_ca.ca_rspint) {
5574743Swnj 		ud->uda_ca.ca_rspint = 0;
5584743Swnj 		for (i = sc->sc_lastrsp;; i++) {
5594743Swnj 			i %= NRSP;
5604743Swnj 			if (ud->uda_ca.ca_rspdsc[i]&UDA_OWN)
5614743Swnj 				break;
5624743Swnj 			udrsp(um, ud, sc, i);
5634743Swnj 			ud->uda_ca.ca_rspdsc[i] |= UDA_OWN;
5644743Swnj 		}
5654743Swnj 		sc->sc_lastrsp = i;
5664743Swnj 	}
5674743Swnj 
5684743Swnj 	/*
5694743Swnj 	 * Check for command ring transition.
5704743Swnj 	 */
5714743Swnj 	if (ud->uda_ca.ca_cmdint) {
5724743Swnj 		printd("uda: command ring transition\n");
5734743Swnj 		ud->uda_ca.ca_cmdint = 0;
5744743Swnj 	}
5756187Ssam 	(void) udstart(um);
5764743Swnj }
5774743Swnj 
5784743Swnj /*
5794743Swnj  * Process a response packet
5804743Swnj  */
5814743Swnj udrsp(um, ud, sc, i)
5824743Swnj 	register struct uba_ctlr *um;
5834743Swnj 	register struct uda *ud;
5844743Swnj 	register struct uda_softc *sc;
5854743Swnj 	int i;
5864743Swnj {
5874743Swnj 	register struct mscp *mp;
5884743Swnj 	struct uba_device *ui;
5894743Swnj 	struct buf *dp, *bp;
5904743Swnj 	int st;
5914743Swnj 
5924743Swnj 	mp = &ud->uda_rsp[i];
5934743Swnj 	mp->mscp_header.uda_msglen = sizeof (struct mscp);
5944743Swnj 	sc->sc_credits += mp->mscp_header.uda_credits & 0xf;
5954743Swnj 	if ((mp->mscp_header.uda_credits & 0xf0) > 0x10)
5964743Swnj 		return;
5974743Swnj 	/*
5984743Swnj 	 * If it's an error log message (datagram),
5994743Swnj 	 * pass it on for more extensive processing.
6004743Swnj 	 */
6014743Swnj 	if ((mp->mscp_header.uda_credits & 0xf0) == 0x10) {
6024743Swnj 		uderror(um, (struct mslg *)mp);
6034743Swnj 		return;
6044743Swnj 	}
6054743Swnj 	if (mp->mscp_unit >= 8)
6064743Swnj 		return;
6074743Swnj 	if ((ui = udip[um->um_ctlr][mp->mscp_unit]) == 0)
6084743Swnj 		return;
6094743Swnj 	st = mp->mscp_status&M_ST_MASK;
6104743Swnj 	switch (mp->mscp_opcode) {
6114743Swnj 	case M_OP_STCON|M_OP_END:
6124743Swnj 		if (st == M_ST_SUCC)
6134743Swnj 			sc->sc_state = S_RUN;
6144743Swnj 		else
6154743Swnj 			sc->sc_state = S_IDLE;
6164743Swnj 		um->um_tab.b_active = 0;
6176187Ssam 		wakeup((caddr_t)um);
6184743Swnj 		break;
6194743Swnj 
6204743Swnj 	case M_OP_ONLIN|M_OP_END:
6214743Swnj 		/*
6224743Swnj 		 * Link the drive onto the controller queue
6234743Swnj 		 */
6244743Swnj 		dp = &udutab[ui->ui_unit];
6254743Swnj 		dp->b_forw = NULL;
6264743Swnj 		if (um->um_tab.b_actf == NULL)
6274743Swnj 			um->um_tab.b_actf = dp;
6284743Swnj 		else
6294743Swnj 			um->um_tab.b_actl->b_forw = dp;
6304743Swnj 		um->um_tab.b_actl = dp;
6314743Swnj 		if (st == M_ST_SUCC) {
6324743Swnj 			ui->ui_flags = 1;	/* mark it online */
6334743Swnj 			radsize[ui->ui_unit] = (daddr_t)mp->mscp_untsize;
6344743Swnj 			printd("uda: unit %d online\n", mp->mscp_unit);
635*12421Ssam /*** New for ***/       printf("uda%d: online, size=%d\n",
636*12421Ssam /*** debugging **/              mp->mscp_unit, (daddr_t)mp->mscp_untsize);
6374743Swnj 		} else {
6384743Swnj 			harderr(dp->b_actf, "ra");
6394743Swnj 			printf("OFFLINE\n");
6404743Swnj 			while (bp = dp->b_actf) {
6414743Swnj 				dp->b_actf = bp->av_forw;
6424743Swnj 				bp->b_flags |= B_ERROR;
6434743Swnj 				iodone(bp);
6444743Swnj 			}
6454743Swnj 		}
6464743Swnj 		dp->b_active = 1;
6474743Swnj 		break;
6484743Swnj 
6494743Swnj 	case M_OP_AVATN:
6504743Swnj 		printd("uda: unit %d attention\n", mp->mscp_unit);
6514743Swnj 		ui->ui_flags = 0;	/* it went offline and we didn't notice */
6524743Swnj 		break;
6534743Swnj 
6544743Swnj 	case M_OP_READ|M_OP_END:
6554743Swnj 	case M_OP_WRITE|M_OP_END:
6564743Swnj 		bp = (struct buf *)mp->mscp_cmdref;
6576964Ssam 		ubarelse(um->um_ubanum, (int *)&bp->b_ubinfo);
6584743Swnj 		/*
6594743Swnj 		 * Unlink buffer from I/O wait queue.
6604743Swnj 		 */
6614743Swnj 		bp->av_back->av_forw = bp->av_forw;
6624743Swnj 		bp->av_forw->av_back = bp->av_back;
663*12421Ssam #if defined(VAX750)
664*12421Ssam 		if (cpu == VAX_750
665*12421Ssam 		    && udwtab[um->um_ctlr].av_forw == &udwtab[um->um_ctlr]) {
666*12421Ssam 			if (um->um_ubinfo == 0)
667*12421Ssam 				printf("udintr: um_ubinfo == 0\n");
668*12421Ssam 			else
669*12421Ssam 				ubarelse(um->um_ubanum, &um->um_ubinfo);
670*12421Ssam 		}
671*12421Ssam #endif
6724743Swnj 		dp = &udutab[ui->ui_unit];
6734743Swnj 		if (ui->ui_dk >= 0)
6744743Swnj 			if (--dp->b_qsize == 0)
6754743Swnj 				dk_busy &= ~(1<<ui->ui_dk);
6764743Swnj 		if (st == M_ST_OFFLN || st == M_ST_AVLBL) {
6774743Swnj 			ui->ui_flags = 0;	/* mark unit offline */
6784743Swnj 			/*
6794743Swnj 			 * Link the buffer onto the front of the drive queue
6804743Swnj 			 */
6814743Swnj 			if ((bp->av_forw = dp->b_actf) == 0)
6824743Swnj 				dp->b_actl = bp;
6834743Swnj 			dp->b_actf = bp;
6844743Swnj 			/*
6854743Swnj 			 * Link the drive onto the controller queue
6864743Swnj 			 */
6874743Swnj 			if (dp->b_active == 0) {
6884743Swnj 				dp->b_forw = NULL;
6894743Swnj 				if (um->um_tab.b_actf == NULL)
6904743Swnj 					um->um_tab.b_actf = dp;
6914743Swnj 				else
6924743Swnj 					um->um_tab.b_actl->b_forw = dp;
6934743Swnj 				um->um_tab.b_actl = dp;
6944743Swnj 				dp->b_active = 1;
6954743Swnj 			}
696*12421Ssam #if defined(VAX750)
697*12421Ssam 			if (cpu == VAX750 && um->um_ubinfo == 0)
698*12421Ssam 				um->um_ubinfo =
699*12421Ssam 				   uballoc(um->um_ubanum, (caddr_t)0, 0,
700*12421Ssam 					UBA_NEEDBDP);
701*12421Ssam #endif
7024743Swnj 			return;
7034743Swnj 		}
7044743Swnj 		if (st != M_ST_SUCC) {
7054743Swnj 			harderr(bp, "ra");
7064743Swnj 			printf("status %o\n", mp->mscp_status);
7074743Swnj 			bp->b_flags |= B_ERROR;
7084743Swnj 		}
7094743Swnj 		bp->b_resid = bp->b_bcount - mp->mscp_bytecnt;
7104743Swnj 		iodone(bp);
7114743Swnj 		break;
7124743Swnj 
7134743Swnj 	case M_OP_GTUNT|M_OP_END:
7144743Swnj 		break;
7154743Swnj 
7164743Swnj 	default:
7174743Swnj 		printf("uda: unknown packet\n");
7184743Swnj 	}
7194743Swnj }
7204743Swnj 
7214743Swnj 
7224743Swnj /*
7234743Swnj  * Process an error log message
7244743Swnj  *
7254743Swnj  * For now, just log the error on the console.
7264743Swnj  * Only minimal decoding is done, only "useful"
7274743Swnj  * information is printed.  Eventually should
7284743Swnj  * send message to an error logger.
7294743Swnj  */
7304743Swnj uderror(um, mp)
7314743Swnj 	register struct uba_ctlr *um;
7324743Swnj 	register struct mslg *mp;
7334743Swnj {
7346964Ssam 	printf("uda%d: %s error, ", um->um_ctlr,
7354743Swnj 		mp->mslg_flags&M_LF_SUCC ? "soft" : "hard");
7364743Swnj 	switch (mp->mslg_format) {
7374743Swnj 	case M_FM_CNTERR:
7384743Swnj 		printf("controller error, event 0%o\n", mp->mslg_event);
7394743Swnj 		break;
7404743Swnj 
7414743Swnj 	case M_FM_BUSADDR:
7424743Swnj 		printf("host memory access error, event 0%o, addr 0%o\n",
7439174Ssam 			mp->mslg_event, mp->mslg_busaddr);
7444743Swnj 		break;
7454743Swnj 
7464743Swnj 	case M_FM_DISKTRN:
747*12421Ssam 		printf("disk transfer error, unit %d, grp 0x%x, hdr 0x%x\n",
748*12421Ssam 			mp->mslg_unit, mp->mslg_group, mp->mslg_hdr);
7494743Swnj 		break;
7504743Swnj 
7514743Swnj 	case M_FM_SDI:
752*12421Ssam 		printf("SDI error, unit %d, event 0%o, hdr 0x%x\n",
753*12421Ssam 			mp->mslg_unit, mp->mslg_event, mp->mslg_hdr);
7544743Swnj 		break;
7554743Swnj 
7564743Swnj 	case M_FM_SMLDSK:
7574743Swnj 		printf("small disk error, unit %d, event 0%o, cyl %d\n",
7584743Swnj 			mp->mslg_unit, mp->mslg_event, mp->mslg_sdecyl);
7594743Swnj 		break;
7604743Swnj 
7614743Swnj 	default:
7624743Swnj 		printf("unknown error, unit %d, format 0%o, event 0%o\n",
7634743Swnj 			mp->mslg_unit, mp->mslg_format, mp->mslg_event);
7644743Swnj 	}
7656964Ssam 
7666964Ssam 	if (udaerror) {
7676964Ssam 		register long *p = (long *)mp;
7686964Ssam 		register int i;
7696964Ssam 
7706964Ssam 		for (i = 0; i < mp->mslg_header.uda_msglen; i += sizeof(*p))
7716964Ssam 			printf("%x ", *p++);
7726964Ssam 		printf("\n");
7736964Ssam 	}
7744743Swnj }
7754743Swnj 
7764743Swnj 
7774743Swnj /*
7784743Swnj  * Find an unused command packet
7794743Swnj  */
7804743Swnj struct mscp *
7814743Swnj udgetcp(um)
7824743Swnj 	struct uba_ctlr *um;
7834743Swnj {
7844743Swnj 	register struct mscp *mp;
7854743Swnj 	register struct udaca *cp;
7864743Swnj 	register struct uda_softc *sc;
7874743Swnj 	register int i;
7884743Swnj 
7894743Swnj 	cp = &uda[um->um_ctlr].uda_ca;
7904743Swnj 	sc = &uda_softc[um->um_ctlr];
7914743Swnj 	i = sc->sc_lastcmd;
7924743Swnj 	if ((cp->ca_cmddsc[i] & (UDA_OWN|UDA_INT)) == UDA_INT) {
7934743Swnj 		cp->ca_cmddsc[i] &= ~UDA_INT;
7944743Swnj 		mp = &uda[um->um_ctlr].uda_cmd[i];
7954743Swnj 		mp->mscp_unit = mp->mscp_modifier = 0;
7964743Swnj 		mp->mscp_opcode = mp->mscp_flags = 0;
7974743Swnj 		mp->mscp_bytecnt = mp->mscp_buffer = 0;
7984743Swnj 		mp->mscp_errlgfl = mp->mscp_copyspd = 0;
7994743Swnj 		sc->sc_lastcmd = (i + 1) % NCMD;
8004743Swnj 		return(mp);
8014743Swnj 	}
8024743Swnj 	return(NULL);
8034743Swnj }
8044743Swnj 
8057734Sroot udread(dev, uio)
8064743Swnj 	dev_t dev;
8077734Sroot 	struct uio *uio;
8084743Swnj {
8094743Swnj 	register int unit = minor(dev) >> 3;
8104743Swnj 
8114743Swnj 	if (unit >= NRA)
8128165Sroot 		return (ENXIO);
8138165Sroot 	return (physio(udstrategy, &rudbuf[unit], dev, B_READ, minphys, uio));
8144743Swnj }
8154743Swnj 
8167845Sroot udwrite(dev, uio)
8174743Swnj 	dev_t dev;
8187845Sroot 	struct uio *uio;
8194743Swnj {
8204743Swnj 	register int unit = minor(dev) >> 3;
8214743Swnj 
8224743Swnj 	if (unit >= NRA)
8238165Sroot 		return (ENXIO);
8248165Sroot 	return (physio(udstrategy, &rudbuf[unit], dev, B_WRITE, minphys, uio));
8254743Swnj }
8264743Swnj 
8274743Swnj udreset(uban)
8284743Swnj 	int uban;
8294743Swnj {
8304743Swnj 	register struct uba_ctlr *um;
8314743Swnj 	register struct uba_device *ui;
8324743Swnj 	register struct buf *bp, *dp;
8334743Swnj 	register int unit;
8344743Swnj 	struct buf *nbp;
8354743Swnj 	int d;
8364743Swnj 
8374743Swnj 	for (d = 0; d < NUDA; d++) {
8384743Swnj 		if ((um = udminfo[d]) == 0 || um->um_ubanum != uban ||
8394743Swnj 		    um->um_alive == 0)
8404743Swnj 			continue;
8414743Swnj 		printf(" uda%d", d);
8424743Swnj 		um->um_tab.b_active = 0;
8434743Swnj 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
8444743Swnj 		uda_softc[d].sc_state = S_IDLE;
8454743Swnj 		for (unit = 0; unit < NRA; unit++) {
8464743Swnj 			if ((ui = uddinfo[unit]) == 0)
8474743Swnj 				continue;
8484743Swnj 			if (ui->ui_alive == 0 || ui->ui_mi != um)
8494743Swnj 				continue;
8504743Swnj 			udutab[unit].b_active = 0;
8514743Swnj 			udutab[unit].b_qsize = 0;
8524743Swnj 		}
8534743Swnj 		for (bp = udwtab[d].av_forw; bp != &udwtab[d]; bp = nbp) {
8544743Swnj 			nbp = bp->av_forw;
8558576Sroot 			bp->b_ubinfo = 0;
8564743Swnj 			/*
8574743Swnj 			 * Link the buffer onto the drive queue
8584743Swnj 			 */
8594743Swnj 			dp = &udutab[dkunit(bp)];
8604743Swnj 			if (dp->b_actf == 0)
8614743Swnj 				dp->b_actf = bp;
8624743Swnj 			else
8634743Swnj 				dp->b_actl->av_forw = bp;
8644743Swnj 			dp->b_actl = bp;
8654743Swnj 			bp->av_forw = 0;
8664743Swnj 			/*
8674743Swnj 			 * Link the drive onto the controller queue
8684743Swnj 			 */
8694743Swnj 			if (dp->b_active == 0) {
8704743Swnj 				dp->b_forw = NULL;
8714743Swnj 				if (um->um_tab.b_actf == NULL)
8724743Swnj 					um->um_tab.b_actf = dp;
8734743Swnj 				else
8744743Swnj 					um->um_tab.b_actl->b_forw = dp;
8754743Swnj 				um->um_tab.b_actl = dp;
8764743Swnj 				dp->b_active = 1;
8774743Swnj 			}
8784743Swnj 		}
8794743Swnj 		udinit(d);
8804743Swnj 	}
8814743Swnj }
8824743Swnj 
8834743Swnj uddump()
8844743Swnj {
8854743Swnj 	return(ENXIO);
8864743Swnj }
887