xref: /csrg-svn/sys/vax/uba/uda.c (revision 8576)
1*8576Sroot /*	uda.c	4.11	82/10/17	*/
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  *	test on 750
144743Swnj  */
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/pte.h"
234743Swnj #include "../h/map.h"
244743Swnj #include "../h/vm.h"
254743Swnj #include "../h/dk.h"
264743Swnj #include "../h/cmap.h"
277734Sroot #include "../h/uio.h"
284743Swnj 
298482Sroot #include "../vax/cpu.h"
308482Sroot #include "../vaxuba/ubareg.h"
318482Sroot #include "../vaxuba/ubavar.h"
328482Sroot #include "../vaxuba/udareg.h"
338482Sroot 
344743Swnj int udadebug;
354743Swnj #define	printd	if(udadebug&1)printf
364743Swnj 
376964Ssam int udaerror = 0;	/* set to cause hex dump of error log packets */
386964Ssam 
394743Swnj /*
404743Swnj  * Parameters for the communications area
414743Swnj  */
424743Swnj 
436964Ssam #define	NRSPL2	3		/* log2 number of response packets */
446964Ssam #define	NCMDL2	3		/* log2 number of command packets */
454743Swnj #define	NRSP	(1<<NRSPL2)
464743Swnj #define	NCMD	(1<<NCMDL2)
474743Swnj 
488482Sroot #include "../vax/mscp.h"
494743Swnj 
504743Swnj struct uda_softc {
514743Swnj 	short	sc_state;	/* state of controller */
524743Swnj 	short	sc_mapped;	/* Unibus map allocated for uda struct? */
534743Swnj 	int	sc_ubainfo;	/* Unibus mapping info */
544743Swnj 	struct uda *sc_uda;	/* Unibus address of uda struct */
554743Swnj 	int	sc_ivec;	/* interrupt vector address */
564743Swnj 	short	sc_credits;	/* transfer credits */
574743Swnj 	short	sc_lastcmd;	/* pointer into command ring */
584743Swnj 	short	sc_lastrsp;	/* pointer into response ring */
594743Swnj } uda_softc[NUDA];
604743Swnj 
614743Swnj /*
624743Swnj  * Controller states
634743Swnj  */
644743Swnj #define	S_IDLE	0		/* hasn't been initialized */
654743Swnj #define	S_STEP1	1		/* doing step 1 init */
664743Swnj #define	S_STEP2	2		/* doing step 2 init */
674743Swnj #define	S_STEP3	3		/* doing step 3 init */
684743Swnj #define	S_SCHAR	4		/* doing "set controller characteristics" */
694743Swnj #define	S_RUN	5		/* running */
704743Swnj 
714743Swnj struct uda {
724743Swnj 	struct udaca	uda_ca;		/* communications area */
734743Swnj 	struct mscp	uda_rsp[NRSP];	/* response packets */
744743Swnj 	struct mscp	uda_cmd[NCMD];	/* command packets */
754743Swnj } uda[NUDA];
764743Swnj 
774743Swnj /* THIS SHOULD BE READ OFF THE PACK, PER DRIVE */
784743Swnj struct size {
794743Swnj 	daddr_t	nblocks;
804743Swnj 	daddr_t	blkoff;
814743Swnj } ra_sizes[8] ={
824743Swnj 	15884,	0,		/* A=blk 0 thru 15883 */
834743Swnj 	33440,	15884,		/* B=blk 15884 thru 49323 */
844743Swnj 	-1,	0,		/* C=blk 0 thru end */
856964Ssam 	15884,	340670,		/* D=blk 340670 thru 356553 */
866964Ssam 	55936,	356554,		/* E=blk 356554 thru 412489 */
876964Ssam 	-1,	412490,		/* F=blk 412490 thru end */
884743Swnj 	82080,	49324,		/* G=blk 49324 thru 131403 */
894743Swnj 	-1,	131404,		/* H=blk 131404 thru end */
904743Swnj };
914743Swnj /* END OF STUFF WHICH SHOULD BE READ IN PER DISK */
924743Swnj 
934743Swnj daddr_t	radsize[NRA];			/* disk size, from ONLINE end packet */
944743Swnj 
954743Swnj int	udprobe(), udslave(), udattach(), udintr();
964743Swnj struct	mscp *udgetcp();
974743Swnj struct	uba_ctlr *udminfo[NUDA];
984743Swnj struct	uba_device *uddinfo[NRA];
994743Swnj struct	uba_device *udip[NUDA][8];	/* 8 == max number of drives */
1004743Swnj 
1016964Ssam u_short	udstd[] = { 0772150, 0 };
1024743Swnj struct	uba_driver udadriver =
1034743Swnj  { udprobe, udslave, udattach, 0, udstd, "ra", uddinfo, "uda", udminfo, 0 };
1044743Swnj struct	buf rudbuf[NRA];
1054743Swnj struct	buf udutab[NRA];
1064743Swnj struct	buf udwtab[NUDA];		/* I/O wait queue, per controller */
1074743Swnj 
1084743Swnj #define	b_qsize		b_resid		/* queue size per drive, in udutab */
1094743Swnj #define	b_ubinfo	b_resid		/* Unibus mapping info, per buffer */
1104743Swnj 
1114743Swnj udprobe(reg, ctlr)
1124743Swnj 	caddr_t reg;
1134743Swnj 	int ctlr;
1144743Swnj {
1154743Swnj 	register int br, cvec;
1164743Swnj 	register struct uda_softc *sc = &uda_softc[ctlr];
1174743Swnj 
1184743Swnj #ifdef lint
1196187Ssam 	br = 0; cvec = br; br = cvec; reg = reg;
1207845Sroot 	udread(0, 0); udwrite(0, 0); udreset(0); udintr(0);
1214743Swnj #endif
1224743Swnj 	/* SHOULD CHECK THAT IT REALLY IS A UDA */
1234743Swnj 	br = 0x15;
1244743Swnj 	cvec = sc->sc_ivec = (uba_hd[numuba].uh_lastiv -= 4);
1257410Skre 	return(sizeof (struct udadevice));
1264743Swnj }
1274743Swnj 
1284743Swnj udslave(ui, reg)
1294743Swnj 	struct uba_device *ui;
1304743Swnj 	caddr_t reg;
1314743Swnj {
1324743Swnj 	/*
1334743Swnj 	 * TOO HARD TO FIND OUT IF DISK IS THERE UNTIL
1344743Swnj 	 * INITIALIZED.  WE'LL FIND OUT WHEN WE FIRST
1354743Swnj 	 * TRY TO ACCESS IT.
1364743Swnj 	 */
1376187Ssam #ifdef lint
1386187Ssam 	ui = ui; reg = reg;
1396187Ssam #endif
1404743Swnj 	return(1);
1414743Swnj }
1424743Swnj 
1434743Swnj udattach(ui)
1444743Swnj 	register struct uba_device *ui;
1454743Swnj {
1464743Swnj 
1474743Swnj 	if (ui->ui_dk > 0)
1484743Swnj 		dk_mspw[ui->ui_dk] = 1.0 / (60 * 31 * 256);	/* approx */
1494743Swnj 	ui->ui_flags = 0;
1504743Swnj 	udip[ui->ui_ctlr][ui->ui_slave] = ui;
1514743Swnj 	radsize[ui->ui_unit] = (daddr_t)0xffffff;	/* max possible size */
1524743Swnj }
1534743Swnj 
1544743Swnj /*
1554743Swnj  * Open a UDA.  Initialize the device and
1564743Swnj  * set the unit online.
1574743Swnj  */
1584743Swnj udopen(dev, flag)
1594743Swnj 	dev_t dev;
1604743Swnj 	int flag;
1614743Swnj {
1624743Swnj 	register int unit;
1634743Swnj 	register struct uba_device *ui;
1644743Swnj 	register struct uda_softc *sc;
1655434Sroot 	int s;
1664743Swnj 
1676187Ssam #ifdef lint
1686187Ssam 	flag = flag;
1696187Ssam #endif
1704743Swnj 	unit = minor(dev) >> 3;
171*8576Sroot 	if (unit >= NRA || (ui = uddinfo[unit]) == 0 || ui->ui_alive == 0)
172*8576Sroot 		return (ENXIO);
1734743Swnj 	sc = &uda_softc[ui->ui_ctlr];
1745434Sroot 	s = spl5();
1754743Swnj 	if (sc->sc_state != S_RUN) {
1764743Swnj 		if (sc->sc_state == S_IDLE)
1774743Swnj 			udinit(ui->ui_ctlr);
1786187Ssam 		/* wait for initialization to complete */
1796187Ssam 		sleep((caddr_t)ui->ui_mi, 0);
180*8576Sroot 		if (sc->sc_state != S_RUN)
181*8576Sroot 			return (EIO);
1824743Swnj 	}
1835434Sroot 	splx(s);
1844743Swnj 	/* SHOULD PROBABLY FORCE AN ONLINE ATTEMPT
1854743Swnj 	   TO SEE IF DISK IS REALLY THERE */
186*8576Sroot 	return (0);
1874743Swnj }
1884743Swnj 
1894743Swnj /*
1904743Swnj  * Initialize a UDA.  Set up UBA mapping registers,
1914743Swnj  * initialize data structures, and start hardware
1924743Swnj  * initialization sequence.
1934743Swnj  */
1944743Swnj udinit(d)
1954743Swnj 	int d;
1964743Swnj {
1974743Swnj 	register struct uda_softc *sc;
1984743Swnj 	register struct uda *ud;
1994743Swnj 	struct udadevice *udaddr;
2004743Swnj 	struct uba_ctlr *um;
2014743Swnj 
2024743Swnj 	sc = &uda_softc[d];
2034743Swnj 	um = udminfo[d];
2044743Swnj 	um->um_tab.b_active++;
2054743Swnj 	ud = &uda[d];
2064743Swnj 	udaddr = (struct udadevice *)um->um_addr;
2074743Swnj 	if (sc->sc_mapped == 0) {
2084743Swnj 		/*
2094743Swnj 		 * Map the communications area and command
2104743Swnj 		 * and response packets into Unibus address
2114743Swnj 		 * space.
2124743Swnj 		 */
2134743Swnj 		sc->sc_ubainfo = uballoc(um->um_ubanum, (caddr_t)ud,
2144743Swnj 		    sizeof (struct uda), 0);
2154743Swnj 		sc->sc_uda = (struct uda *)(sc->sc_ubainfo & 0x3ffff);
2164743Swnj 		sc->sc_mapped = 1;
2174743Swnj 	}
2184743Swnj 
2194743Swnj 	/*
2204743Swnj 	 * Start the hardware initialization sequence.
2214743Swnj 	 */
2224743Swnj 	udaddr->udaip = 0;		/* start initialization */
2234743Swnj 	while ((udaddr->udasa & UDA_STEP1) == 0)
2244743Swnj 		;
2254743Swnj 	udaddr->udasa = UDA_ERR|(NCMDL2<<11)|(NRSPL2<<8)|UDA_IE|(sc->sc_ivec/4);
2264743Swnj 	/*
2274743Swnj 	 * Initialization continues in interrupt routine.
2284743Swnj 	 */
2294743Swnj 	sc->sc_state = S_STEP1;
2304743Swnj 	sc->sc_credits = 0;
2314743Swnj }
2324743Swnj 
2334743Swnj udstrategy(bp)
2344743Swnj 	register struct buf *bp;
2354743Swnj {
2364743Swnj 	register struct uba_device *ui;
2374743Swnj 	register struct uba_ctlr *um;
2384743Swnj 	register struct buf *dp;
2394743Swnj 	register int unit;
2404743Swnj 	int xunit = minor(bp->b_dev) & 07;
2414743Swnj 	daddr_t sz, maxsz;
2425434Sroot 	int s;
2434743Swnj 
2444743Swnj 	sz = (bp->b_bcount+511) >> 9;
2454743Swnj 	unit = dkunit(bp);
2464743Swnj 	if (unit >= NRA)
2474743Swnj 		goto bad;
2484743Swnj 	ui = uddinfo[unit];
2494743Swnj 	um = ui->ui_mi;
2504743Swnj 	if (ui == 0 || ui->ui_alive == 0)
2514743Swnj 		goto bad;
2524743Swnj 	if ((maxsz = ra_sizes[xunit].nblocks) < 0)
2534743Swnj 		maxsz = radsize[unit] - ra_sizes[xunit].blkoff;
2544743Swnj 	if (bp->b_blkno < 0 || bp->b_blkno+sz > maxsz ||
2554743Swnj 	    ra_sizes[xunit].blkoff >= radsize[unit])
2564743Swnj 		goto bad;
2575434Sroot 	s = spl5();
2584743Swnj 	/*
2594743Swnj 	 * Link the buffer onto the drive queue
2604743Swnj 	 */
2614743Swnj 	dp = &udutab[ui->ui_unit];
2624743Swnj 	if (dp->b_actf == 0)
2634743Swnj 		dp->b_actf = bp;
2644743Swnj 	else
2654743Swnj 		dp->b_actl->av_forw = bp;
2664743Swnj 	dp->b_actl = bp;
2674743Swnj 	bp->av_forw = 0;
2684743Swnj 	/*
2694743Swnj 	 * Link the drive onto the controller queue
2704743Swnj 	 */
2714743Swnj 	if (dp->b_active == 0) {
2724743Swnj 		dp->b_forw = NULL;
2734743Swnj 		if (um->um_tab.b_actf == NULL)
2744743Swnj 			um->um_tab.b_actf = dp;
2754743Swnj 		else
2764743Swnj 			um->um_tab.b_actl->b_forw = dp;
2774743Swnj 		um->um_tab.b_actl = dp;
2784743Swnj 		dp->b_active = 1;
2794743Swnj 	}
2804743Swnj 	if (um->um_tab.b_active == 0) {
2814743Swnj #if defined(VAX750)
2824743Swnj 		if (cpu == VAX_750) {
2834743Swnj 			if (um->um_ubinfo != 0)
2844743Swnj 				printf("uda: ubinfo %x\n",um->um_ubinfo);
2854743Swnj 			else
2864743Swnj 				um->um_ubinfo =
2876187Ssam 				uballoc(um->um_ubanum, (caddr_t)0, 0,
2886187Ssam 					UBA_NEEDBDP);
2894743Swnj 		}
2904743Swnj #endif
2914743Swnj 		(void) udstart(um);
2924743Swnj 	}
2935434Sroot 	splx(s);
2944743Swnj 	return;
2954743Swnj 
2964743Swnj bad:
2974743Swnj 	bp->b_flags |= B_ERROR;
2984743Swnj 	iodone(bp);
2994743Swnj 	return;
3004743Swnj }
3014743Swnj 
3024743Swnj udstart(um)
3034743Swnj 	register struct uba_ctlr *um;
3044743Swnj {
3054743Swnj 	register struct buf *bp, *dp;
3064743Swnj 	register struct mscp *mp;
3074743Swnj 	register struct uda_softc *sc;
3084743Swnj 	register struct uba_device *ui;
3094743Swnj 	struct udadevice *udaddr;
3104743Swnj 	int i;
3114743Swnj 
3124743Swnj 	sc = &uda_softc[um->um_ctlr];
3134743Swnj 
3144743Swnj loop:
3154743Swnj 	if ((dp = um->um_tab.b_actf) == NULL) {
3164743Swnj 		/*
3174743Swnj 		 * Release uneeded UBA resources and return
3184743Swnj 		 */
3194743Swnj 		um->um_tab.b_active = 0;
3204743Swnj #if defined(VAX750)
3214743Swnj 		if (cpu == VAX_750) {
3224743Swnj 			if (um->um_ubinfo == 0)
3234743Swnj 				printf("uda: um_ubinfo == 0\n");
3244743Swnj 			else
3254743Swnj 				ubarelse(um->um_ubanum, &um->um_ubinfo);
3264743Swnj 		}
3274743Swnj #endif
3286187Ssam 		return (0);
3294743Swnj 	}
3304743Swnj 	if ((bp = dp->b_actf) == NULL) {
3314743Swnj 		/*
3324743Swnj 		 * No more requests for this drive, remove
3334743Swnj 		 * from controller queue and look at next drive.
3344743Swnj 		 * We know we're at the head of the controller queue.
3354743Swnj 		 */
3364743Swnj 		dp->b_active = 0;
3374743Swnj 		um->um_tab.b_actf = dp->b_forw;
3384743Swnj 		goto loop;
3394743Swnj 	}
3404743Swnj 	um->um_tab.b_active++;
3414743Swnj 	udaddr = (struct udadevice *)um->um_addr;
3424743Swnj 	if ((udaddr->udasa&UDA_ERR) || sc->sc_state != S_RUN) {
3434743Swnj 		harderr(bp, "ra");
3444743Swnj 		printf("udasa %o, state %d\n", udaddr->udasa&0xffff, sc->sc_state);
3454743Swnj 		udinit(um->um_ctlr);
3464743Swnj 		/* SHOULD REQUEUE OUTSTANDING REQUESTS, LIKE UDRESET */
3476187Ssam 		return (0);
3484743Swnj 	}
3494743Swnj 	ui = uddinfo[dkunit(bp)];
3504743Swnj 	/*
3514743Swnj 	 * If no credits, can't issue any commands
3524743Swnj 	 * until some outstanding commands complete.
3534743Swnj 	 */
3544743Swnj 	if (sc->sc_credits < 2)
3556187Ssam 		return (0);
3564743Swnj 	if ((mp = udgetcp(um)) == NULL)
3576187Ssam 		return (0);
3584743Swnj 	sc->sc_credits--;	/* committed to issuing a command */
3594743Swnj 	if (ui->ui_flags == 0) {	/* not online */
3604743Swnj 		mp->mscp_opcode = M_OP_ONLIN;
3614743Swnj 		mp->mscp_unit = ui->ui_slave;
3624743Swnj 		dp->b_active = 2;
3634743Swnj 		um->um_tab.b_actf = dp->b_forw;	/* remove from controller q */
3644743Swnj 		printd("uda: bring unit %d online\n", ui->ui_slave);
3654743Swnj 		*((long *)mp->mscp_dscptr) |= UDA_OWN|UDA_INT;
3664743Swnj 		i = udaddr->udaip;
3674743Swnj 		goto loop;
3684743Swnj 	}
3694743Swnj 	switch (cpu) {
3704743Swnj 	case VAX_780:
3714743Swnj 		i = UBA_NEEDBDP|UBA_CANTWAIT;
3724743Swnj 		break;
3734743Swnj 
3744743Swnj 	case VAX_750:
3754743Swnj 		i = um->um_ubinfo|UBA_HAVEBDP|UBA_CANTWAIT;
3764743Swnj 		break;
3774743Swnj 
3786949Ssam 	case VAX_730:
3794743Swnj 		i = UBA_CANTWAIT;
3804743Swnj 		break;
3814743Swnj 	}
3824743Swnj 	if ((i = ubasetup(um->um_ubanum, bp, i)) == 0) {
3834743Swnj 		mp->mscp_opcode = M_OP_GTUNT;
3844743Swnj 		mp->mscp_unit = ui->ui_slave;
3854743Swnj 		*((long *)mp->mscp_dscptr) |= UDA_OWN|UDA_INT;
3864743Swnj 		i = udaddr->udaip;	/* initiate polling */
3874743Swnj 		return(1);		/* wait for interrupt */
3884743Swnj 	}
3894743Swnj 	mp->mscp_cmdref = (long)bp;	/* pointer to get back */
3904743Swnj 	mp->mscp_opcode = bp->b_flags&B_READ ? M_OP_READ : M_OP_WRITE;
3914743Swnj 	mp->mscp_unit = ui->ui_slave;
3924743Swnj 	mp->mscp_lbn = bp->b_blkno + ra_sizes[minor(bp->b_dev)&7].blkoff;
3934743Swnj 	mp->mscp_bytecnt = bp->b_bcount;
3944743Swnj 	mp->mscp_buffer = (i & 0x3ffff) | (((i>>28)&0xf)<<24);
3954743Swnj #if defined(VAX750)
3964743Swnj 	if (cpu == VAX_750)
3974743Swnj 		i &= 0xfffffff;		/* mask off bdp */
3984743Swnj #endif
3994743Swnj 	bp->b_ubinfo = i;		/* save mapping info */
4004743Swnj 	*((long *)mp->mscp_dscptr) |= UDA_OWN|UDA_INT;
4014743Swnj 	i = udaddr->udaip;		/* initiate polling */
4024743Swnj 	if (ui->ui_dk >= 0) {
4034743Swnj 		dk_busy |= 1<<ui->ui_dk;
4044743Swnj 		dp->b_qsize++;
4054743Swnj 		dk_xfer[ui->ui_dk]++;
4064743Swnj 		dk_wds[ui->ui_dk] += bp->b_bcount>>6;
4074743Swnj 	}
4084743Swnj 
4094743Swnj 	/*
4104743Swnj 	 * Move drive to the end of the controller queue
4114743Swnj 	 */
4124743Swnj 	if (dp->b_forw != NULL) {
4134743Swnj 		um->um_tab.b_actf = dp->b_forw;
4144743Swnj 		um->um_tab.b_actl->b_forw = dp;
4154743Swnj 		um->um_tab.b_actl = dp;
4164743Swnj 		dp->b_forw = NULL;
4174743Swnj 	}
4184743Swnj 	/*
4194743Swnj 	 * Move buffer to I/O wait queue
4204743Swnj 	 */
4214743Swnj 	dp->b_actf = bp->av_forw;
4224743Swnj 	dp = &udwtab[um->um_ctlr];
4234743Swnj 	bp->av_forw = dp;
4244743Swnj 	bp->av_back = dp->av_back;
4254743Swnj 	dp->av_back->av_forw = bp;
4264743Swnj 	dp->av_back = bp;
4274743Swnj 	goto loop;
4284743Swnj }
4294743Swnj 
4304743Swnj /*
4314743Swnj  * UDA interrupt routine.
4324743Swnj  */
4334743Swnj udintr(d)
4344743Swnj 	int d;
4354743Swnj {
4364743Swnj 	register struct uba_ctlr *um = udminfo[d];
4374743Swnj 	register struct udadevice *udaddr = (struct udadevice *)um->um_addr;
4384743Swnj 	struct buf *bp;
4394743Swnj 	register int i;
4404743Swnj 	register struct uda_softc *sc = &uda_softc[d];
4414743Swnj 	register struct uda *ud = &uda[d];
4424743Swnj 	struct uda *uud;
4434743Swnj 	struct mscp *mp;
4444743Swnj 
4454743Swnj 	printd("udintr: state %d, udasa %o\n", sc->sc_state, udaddr->udasa);
4464743Swnj 	switch (sc->sc_state) {
4474743Swnj 	case S_IDLE:
4484743Swnj 		printf("uda%d: random interrupt ignored\n", d);
4494743Swnj 		return;
4504743Swnj 
4514743Swnj 	case S_STEP1:
4526964Ssam #define	STEP1MASK	0174377
4534743Swnj #define	STEP1GOOD	(UDA_STEP2|UDA_IE|(NCMDL2<<3)|NRSPL2)
4546964Ssam 		if ((udaddr->udasa&STEP1MASK) != STEP1GOOD) {
4554743Swnj 			sc->sc_state = S_IDLE;
4566187Ssam 			wakeup((caddr_t)um);
4574743Swnj 			return;
4584743Swnj 		}
4594743Swnj 		udaddr->udasa = ((int)&sc->sc_uda->uda_ca.ca_ringbase)|
4604743Swnj 		    (cpu == VAX_780 ? UDA_PI : 0);
4614743Swnj 		sc->sc_state = S_STEP2;
4624743Swnj 		return;
4634743Swnj 
4644743Swnj 	case S_STEP2:
4656964Ssam #define	STEP2MASK	0174377
4664743Swnj #define	STEP2GOOD	(UDA_STEP3|UDA_IE|(sc->sc_ivec/4))
4676964Ssam 		if ((udaddr->udasa&STEP2MASK) != STEP2GOOD) {
4684743Swnj 			sc->sc_state = S_IDLE;
4696187Ssam 			wakeup((caddr_t)um);
4704743Swnj 			return;
4714743Swnj 		}
4724743Swnj 		udaddr->udasa = ((int)&sc->sc_uda->uda_ca.ca_ringbase)>>16;
4734743Swnj 		sc->sc_state = S_STEP3;
4744743Swnj 		return;
4754743Swnj 
4764743Swnj 	case S_STEP3:
4776964Ssam #define	STEP3MASK	0174000
4784743Swnj #define	STEP3GOOD	UDA_STEP4
4796964Ssam 		if ((udaddr->udasa&STEP3MASK) != STEP3GOOD) {
4804743Swnj 			sc->sc_state = S_IDLE;
4816187Ssam 			wakeup((caddr_t)um);
4824743Swnj 			return;
4834743Swnj 		}
4844743Swnj 		udaddr->udasa = UDA_GO;
4854743Swnj 		sc->sc_state = S_SCHAR;
4864743Swnj 
4874743Swnj 		/*
4884743Swnj 		 * Initialize the data structures.
4894743Swnj 		 */
4904743Swnj 		uud = sc->sc_uda;
4914743Swnj 		for (i = 0; i < NRSP; i++) {
4924743Swnj 			ud->uda_ca.ca_rspdsc[i] = UDA_OWN|UDA_INT|
4934743Swnj 				(long)&uud->uda_rsp[i].mscp_cmdref;
4944743Swnj 			ud->uda_rsp[i].mscp_dscptr = &ud->uda_ca.ca_rspdsc[i];
4954743Swnj 			ud->uda_rsp[i].mscp_header.uda_msglen = sizeof (struct mscp);
4964743Swnj 		}
4974743Swnj 		for (i = 0; i < NCMD; i++) {
4984743Swnj 			ud->uda_ca.ca_cmddsc[i] = UDA_INT|
4994743Swnj 				(long)&uud->uda_cmd[i].mscp_cmdref;
5004743Swnj 			ud->uda_cmd[i].mscp_dscptr = &ud->uda_ca.ca_cmddsc[i];
5014743Swnj 			ud->uda_cmd[i].mscp_header.uda_msglen = sizeof (struct mscp);
5024743Swnj 		}
5034743Swnj 		bp = &udwtab[d];
5044743Swnj 		bp->av_forw = bp->av_back = bp;
5054743Swnj 		sc->sc_lastcmd = 0;
5064743Swnj 		sc->sc_lastrsp = 0;
5074743Swnj 		if ((mp = udgetcp(um)) == NULL) {
5084743Swnj 			sc->sc_state = S_IDLE;
5096187Ssam 			wakeup((caddr_t)um);
5104743Swnj 			return;
5114743Swnj 		}
5124743Swnj 		mp->mscp_opcode = M_OP_STCON;
5134743Swnj 		mp->mscp_cntflgs = M_CF_ATTN|M_CF_MISC|M_CF_THIS;
5144743Swnj 		*((long *)mp->mscp_dscptr) |= UDA_OWN|UDA_INT;
5154743Swnj 		i = udaddr->udaip;	/* initiate polling */
5164743Swnj 		return;
5174743Swnj 
5184743Swnj 	case S_SCHAR:
5194743Swnj 	case S_RUN:
5204743Swnj 		break;
5214743Swnj 
5224743Swnj 	default:
5234743Swnj 		printf("uda%d: interrupt in unknown state %d ignored\n",
5244743Swnj 			d, sc->sc_state);
5254743Swnj 		return;
5264743Swnj 	}
5274743Swnj 
5284743Swnj 	if (udaddr->udasa&UDA_ERR) {
5294743Swnj 		printf("uda%d: fatal error (%o)\n", d, udaddr->udasa&0xffff);
5304743Swnj 		udaddr->udaip = 0;
5316187Ssam 		wakeup((caddr_t)um);
5324743Swnj 	}
5334743Swnj 
5344743Swnj 	/*
5354743Swnj 	 * Check for a buffer purge request.
5364743Swnj 	 */
5374743Swnj 	if (ud->uda_ca.ca_bdp) {
5384743Swnj 		/*
5394743Swnj 		 * THIS IS A KLUDGE.
5404743Swnj 		 * Maybe we should change the entire
5414743Swnj 		 * UBA interface structure.
5424743Swnj 		 */
5434743Swnj 		int s = spl7();
5444743Swnj 
5454743Swnj 		i = um->um_ubinfo;
5464743Swnj 		printd("uda: purge bdp %d\n", ud->uda_ca.ca_bdp);
5474743Swnj 		um->um_ubinfo = ud->uda_ca.ca_bdp<<28;
5484743Swnj 		ubapurge(um);
5494743Swnj 		um->um_ubinfo = i;
5504743Swnj 		(void) splx(s);
5514743Swnj 		ud->uda_ca.ca_bdp = 0;
5524743Swnj 		udaddr->udasa = 0;	/* signal purge complete */
5534743Swnj 	}
5544743Swnj 
5554743Swnj 	/*
5564743Swnj 	 * Check for response ring transition.
5574743Swnj 	 */
5584743Swnj 	if (ud->uda_ca.ca_rspint) {
5594743Swnj 		ud->uda_ca.ca_rspint = 0;
5604743Swnj 		for (i = sc->sc_lastrsp;; i++) {
5614743Swnj 			i %= NRSP;
5624743Swnj 			if (ud->uda_ca.ca_rspdsc[i]&UDA_OWN)
5634743Swnj 				break;
5644743Swnj 			udrsp(um, ud, sc, i);
5654743Swnj 			ud->uda_ca.ca_rspdsc[i] |= UDA_OWN;
5664743Swnj 		}
5674743Swnj 		sc->sc_lastrsp = i;
5684743Swnj 	}
5694743Swnj 
5704743Swnj 	/*
5714743Swnj 	 * Check for command ring transition.
5724743Swnj 	 */
5734743Swnj 	if (ud->uda_ca.ca_cmdint) {
5744743Swnj 		printd("uda: command ring transition\n");
5754743Swnj 		ud->uda_ca.ca_cmdint = 0;
5764743Swnj 	}
5776187Ssam 	(void) udstart(um);
5784743Swnj }
5794743Swnj 
5804743Swnj /*
5814743Swnj  * Process a response packet
5824743Swnj  */
5834743Swnj udrsp(um, ud, sc, i)
5844743Swnj 	register struct uba_ctlr *um;
5854743Swnj 	register struct uda *ud;
5864743Swnj 	register struct uda_softc *sc;
5874743Swnj 	int i;
5884743Swnj {
5894743Swnj 	register struct mscp *mp;
5904743Swnj 	struct uba_device *ui;
5914743Swnj 	struct buf *dp, *bp;
5924743Swnj 	int st;
5934743Swnj 
5944743Swnj 	mp = &ud->uda_rsp[i];
5954743Swnj 	mp->mscp_header.uda_msglen = sizeof (struct mscp);
5964743Swnj 	sc->sc_credits += mp->mscp_header.uda_credits & 0xf;
5974743Swnj 	if ((mp->mscp_header.uda_credits & 0xf0) > 0x10)
5984743Swnj 		return;
5994743Swnj 	/*
6004743Swnj 	 * If it's an error log message (datagram),
6014743Swnj 	 * pass it on for more extensive processing.
6024743Swnj 	 */
6034743Swnj 	if ((mp->mscp_header.uda_credits & 0xf0) == 0x10) {
6044743Swnj 		uderror(um, (struct mslg *)mp);
6054743Swnj 		return;
6064743Swnj 	}
6074743Swnj 	if (mp->mscp_unit >= 8)
6084743Swnj 		return;
6094743Swnj 	if ((ui = udip[um->um_ctlr][mp->mscp_unit]) == 0)
6104743Swnj 		return;
6114743Swnj 	st = mp->mscp_status&M_ST_MASK;
6124743Swnj 	switch (mp->mscp_opcode) {
6134743Swnj 	case M_OP_STCON|M_OP_END:
6144743Swnj 		if (st == M_ST_SUCC)
6154743Swnj 			sc->sc_state = S_RUN;
6164743Swnj 		else
6174743Swnj 			sc->sc_state = S_IDLE;
6184743Swnj 		um->um_tab.b_active = 0;
6196187Ssam 		wakeup((caddr_t)um);
6204743Swnj 		break;
6214743Swnj 
6224743Swnj 	case M_OP_ONLIN|M_OP_END:
6234743Swnj 		/*
6244743Swnj 		 * Link the drive onto the controller queue
6254743Swnj 		 */
6264743Swnj 		dp = &udutab[ui->ui_unit];
6274743Swnj 		dp->b_forw = NULL;
6284743Swnj 		if (um->um_tab.b_actf == NULL)
6294743Swnj 			um->um_tab.b_actf = dp;
6304743Swnj 		else
6314743Swnj 			um->um_tab.b_actl->b_forw = dp;
6324743Swnj 		um->um_tab.b_actl = dp;
6334743Swnj 		if (st == M_ST_SUCC) {
6344743Swnj 			ui->ui_flags = 1;	/* mark it online */
6354743Swnj 			radsize[ui->ui_unit] = (daddr_t)mp->mscp_untsize;
6364743Swnj 			printd("uda: unit %d online\n", mp->mscp_unit);
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;
6634743Swnj 		dp = &udutab[ui->ui_unit];
6644743Swnj 		if (ui->ui_dk >= 0)
6654743Swnj 			if (--dp->b_qsize == 0)
6664743Swnj 				dk_busy &= ~(1<<ui->ui_dk);
6674743Swnj 		if (st == M_ST_OFFLN || st == M_ST_AVLBL) {
6684743Swnj 			ui->ui_flags = 0;	/* mark unit offline */
6694743Swnj 			/*
6704743Swnj 			 * Link the buffer onto the front of the drive queue
6714743Swnj 			 */
6724743Swnj 			if ((bp->av_forw = dp->b_actf) == 0)
6734743Swnj 				dp->b_actl = bp;
6744743Swnj 			dp->b_actf = bp;
6754743Swnj 			/*
6764743Swnj 			 * Link the drive onto the controller queue
6774743Swnj 			 */
6784743Swnj 			if (dp->b_active == 0) {
6794743Swnj 				dp->b_forw = NULL;
6804743Swnj 				if (um->um_tab.b_actf == NULL)
6814743Swnj 					um->um_tab.b_actf = dp;
6824743Swnj 				else
6834743Swnj 					um->um_tab.b_actl->b_forw = dp;
6844743Swnj 				um->um_tab.b_actl = dp;
6854743Swnj 				dp->b_active = 1;
6864743Swnj 			}
6874743Swnj 			return;
6884743Swnj 		}
6894743Swnj 		if (st != M_ST_SUCC) {
6904743Swnj 			harderr(bp, "ra");
6914743Swnj 			printf("status %o\n", mp->mscp_status);
6924743Swnj 			bp->b_flags |= B_ERROR;
6934743Swnj 		}
6944743Swnj 		bp->b_resid = bp->b_bcount - mp->mscp_bytecnt;
6954743Swnj 		iodone(bp);
6964743Swnj 		break;
6974743Swnj 
6984743Swnj 	case M_OP_GTUNT|M_OP_END:
6994743Swnj 		break;
7004743Swnj 
7014743Swnj 	default:
7024743Swnj 		printf("uda: unknown packet\n");
7034743Swnj 	}
7044743Swnj }
7054743Swnj 
7064743Swnj 
7074743Swnj /*
7084743Swnj  * Process an error log message
7094743Swnj  *
7104743Swnj  * For now, just log the error on the console.
7114743Swnj  * Only minimal decoding is done, only "useful"
7124743Swnj  * information is printed.  Eventually should
7134743Swnj  * send message to an error logger.
7144743Swnj  */
7154743Swnj uderror(um, mp)
7164743Swnj 	register struct uba_ctlr *um;
7174743Swnj 	register struct mslg *mp;
7184743Swnj {
7196964Ssam 	printf("uda%d: %s error, ", um->um_ctlr,
7204743Swnj 		mp->mslg_flags&M_LF_SUCC ? "soft" : "hard");
7214743Swnj 	switch (mp->mslg_format) {
7224743Swnj 	case M_FM_CNTERR:
7234743Swnj 		printf("controller error, event 0%o\n", mp->mslg_event);
7244743Swnj 		break;
7254743Swnj 
7264743Swnj 	case M_FM_BUSADDR:
7274743Swnj 		printf("host memory access error, event 0%o, addr 0%o\n",
7284743Swnj 			mp->mslg_event, *((long *)&mp->mslg_busaddr[0]));
7294743Swnj 		break;
7304743Swnj 
7314743Swnj 	case M_FM_DISKTRN:
7326964Ssam 		printf("disk transfer error, unit %d\n", mp->mslg_unit);
7334743Swnj 		break;
7344743Swnj 
7354743Swnj 	case M_FM_SDI:
7366964Ssam 		printf("SDI error, unit %d, event 0%o\n", mp->mslg_unit,
7376964Ssam 			mp->mslg_event);
7384743Swnj 		break;
7394743Swnj 
7404743Swnj 	case M_FM_SMLDSK:
7414743Swnj 		printf("small disk error, unit %d, event 0%o, cyl %d\n",
7424743Swnj 			mp->mslg_unit, mp->mslg_event, mp->mslg_sdecyl);
7434743Swnj 		break;
7444743Swnj 
7454743Swnj 	default:
7464743Swnj 		printf("unknown error, unit %d, format 0%o, event 0%o\n",
7474743Swnj 			mp->mslg_unit, mp->mslg_format, mp->mslg_event);
7484743Swnj 	}
7496964Ssam 
7506964Ssam 	if (udaerror) {
7516964Ssam 		register long *p = (long *)mp;
7526964Ssam 		register int i;
7536964Ssam 
7546964Ssam 		for (i = 0; i < mp->mslg_header.uda_msglen; i += sizeof(*p))
7556964Ssam 			printf("%x ", *p++);
7566964Ssam 		printf("\n");
7576964Ssam 	}
7584743Swnj }
7594743Swnj 
7604743Swnj 
7614743Swnj /*
7624743Swnj  * Find an unused command packet
7634743Swnj  */
7644743Swnj struct mscp *
7654743Swnj udgetcp(um)
7664743Swnj 	struct uba_ctlr *um;
7674743Swnj {
7684743Swnj 	register struct mscp *mp;
7694743Swnj 	register struct udaca *cp;
7704743Swnj 	register struct uda_softc *sc;
7714743Swnj 	register int i;
7724743Swnj 
7734743Swnj 	cp = &uda[um->um_ctlr].uda_ca;
7744743Swnj 	sc = &uda_softc[um->um_ctlr];
7754743Swnj 	i = sc->sc_lastcmd;
7764743Swnj 	if ((cp->ca_cmddsc[i] & (UDA_OWN|UDA_INT)) == UDA_INT) {
7774743Swnj 		cp->ca_cmddsc[i] &= ~UDA_INT;
7784743Swnj 		mp = &uda[um->um_ctlr].uda_cmd[i];
7794743Swnj 		mp->mscp_unit = mp->mscp_modifier = 0;
7804743Swnj 		mp->mscp_opcode = mp->mscp_flags = 0;
7814743Swnj 		mp->mscp_bytecnt = mp->mscp_buffer = 0;
7824743Swnj 		mp->mscp_errlgfl = mp->mscp_copyspd = 0;
7834743Swnj 		sc->sc_lastcmd = (i + 1) % NCMD;
7844743Swnj 		return(mp);
7854743Swnj 	}
7864743Swnj 	return(NULL);
7874743Swnj }
7884743Swnj 
7897734Sroot udread(dev, uio)
7904743Swnj 	dev_t dev;
7917734Sroot 	struct uio *uio;
7924743Swnj {
7934743Swnj 	register int unit = minor(dev) >> 3;
7944743Swnj 
7954743Swnj 	if (unit >= NRA)
7968165Sroot 		return (ENXIO);
7978165Sroot 	return (physio(udstrategy, &rudbuf[unit], dev, B_READ, minphys, uio));
7984743Swnj }
7994743Swnj 
8007845Sroot udwrite(dev, uio)
8014743Swnj 	dev_t dev;
8027845Sroot 	struct uio *uio;
8034743Swnj {
8044743Swnj 	register int unit = minor(dev) >> 3;
8054743Swnj 
8064743Swnj 	if (unit >= NRA)
8078165Sroot 		return (ENXIO);
8088165Sroot 	return (physio(udstrategy, &rudbuf[unit], dev, B_WRITE, minphys, uio));
8094743Swnj }
8104743Swnj 
8114743Swnj udreset(uban)
8124743Swnj 	int uban;
8134743Swnj {
8144743Swnj 	register struct uba_ctlr *um;
8154743Swnj 	register struct uba_device *ui;
8164743Swnj 	register struct buf *bp, *dp;
8174743Swnj 	register int unit;
8184743Swnj 	struct buf *nbp;
8194743Swnj 	int d;
8204743Swnj 
8214743Swnj 	for (d = 0; d < NUDA; d++) {
8224743Swnj 		if ((um = udminfo[d]) == 0 || um->um_ubanum != uban ||
8234743Swnj 		    um->um_alive == 0)
8244743Swnj 			continue;
8254743Swnj 		printf(" uda%d", d);
8264743Swnj 		um->um_tab.b_active = 0;
8274743Swnj 		um->um_tab.b_actf = um->um_tab.b_actl = 0;
8284743Swnj 		uda_softc[d].sc_state = S_IDLE;
8294743Swnj 		for (unit = 0; unit < NRA; unit++) {
8304743Swnj 			if ((ui = uddinfo[unit]) == 0)
8314743Swnj 				continue;
8324743Swnj 			if (ui->ui_alive == 0 || ui->ui_mi != um)
8334743Swnj 				continue;
8344743Swnj 			udutab[unit].b_active = 0;
8354743Swnj 			udutab[unit].b_qsize = 0;
8364743Swnj 		}
8374743Swnj 		for (bp = udwtab[d].av_forw; bp != &udwtab[d]; bp = nbp) {
8384743Swnj 			nbp = bp->av_forw;
839*8576Sroot 			bp->b_ubinfo = 0;
8404743Swnj 			/*
8414743Swnj 			 * Link the buffer onto the drive queue
8424743Swnj 			 */
8434743Swnj 			dp = &udutab[dkunit(bp)];
8444743Swnj 			if (dp->b_actf == 0)
8454743Swnj 				dp->b_actf = bp;
8464743Swnj 			else
8474743Swnj 				dp->b_actl->av_forw = bp;
8484743Swnj 			dp->b_actl = bp;
8494743Swnj 			bp->av_forw = 0;
8504743Swnj 			/*
8514743Swnj 			 * Link the drive onto the controller queue
8524743Swnj 			 */
8534743Swnj 			if (dp->b_active == 0) {
8544743Swnj 				dp->b_forw = NULL;
8554743Swnj 				if (um->um_tab.b_actf == NULL)
8564743Swnj 					um->um_tab.b_actf = dp;
8574743Swnj 				else
8584743Swnj 					um->um_tab.b_actl->b_forw = dp;
8594743Swnj 				um->um_tab.b_actl = dp;
8604743Swnj 				dp->b_active = 1;
8614743Swnj 			}
8624743Swnj 		}
8634743Swnj 		udinit(d);
8644743Swnj 	}
8654743Swnj }
8664743Swnj 
8674743Swnj uddump()
8684743Swnj {
8694743Swnj 	return(ENXIO);
8704743Swnj }
871