xref: /csrg-svn/sys/vax/mba/mba.c (revision 2403)
1*2403Skre /*	mba.c	4.5	81/02/10	*/
228Sbill 
32383Swnj /*
42383Swnj  * Massbus driver; arbitrates massbusses through device driver routines
52383Swnj  * and provides common functions.
62383Swnj  */
72383Swnj int	mbadebug = 0;
82383Swnj #define	dprintf if (mbadebug) printf
92383Swnj 
1028Sbill #include "../h/param.h"
112383Swnj #include "../h/systm.h"
122383Swnj #include "../h/dk.h"
1328Sbill #include "../h/buf.h"
1428Sbill #include "../h/conf.h"
1528Sbill #include "../h/dir.h"
1628Sbill #include "../h/user.h"
1728Sbill #include "../h/proc.h"
182383Swnj #include "../h/map.h"
1928Sbill #include "../h/pte.h"
2028Sbill #include "../h/mba.h"
2128Sbill #include "../h/mtpr.h"
2228Sbill #include "../h/vm.h"
2328Sbill 
2428Sbill /*
252383Swnj  * Start activity on a massbus device.
262383Swnj  * We are given the device's mba_info structure and activate
272383Swnj  * the device via the unit start routine.  The unit start
282383Swnj  * routine may indicate that it is finished (e.g. if the operation
292383Swnj  * was a ``sense'' on a tape drive), that the (multi-ported) unit
302383Swnj  * is busy (we will get an interrupt later), that it started the
312383Swnj  * unit (e.g. for a non-data transfer operation), or that it has
322383Swnj  * set up a data transfer operation and we should start the massbus adaptor.
3328Sbill  */
342383Swnj mbustart(mi)
352383Swnj 	register struct mba_info *mi;
362383Swnj {
372383Swnj 	register struct mba_drv *mdp;	/* drive registers */
382383Swnj 	register struct buf *bp;	/* i/o operation at head of queue */
392383Swnj 	register struct mba_hd *mhp;	/* header for mba device is on */
4028Sbill 
412383Swnj 	dprintf("enter mbustart\n");
422383Swnj loop:
432383Swnj 	/*
442383Swnj 	 * Get the first thing to do off device queue.
452383Swnj 	 */
462383Swnj 	bp = mi->mi_tab.b_actf;
472383Swnj 	if (bp == NULL)
482383Swnj 		return;
492383Swnj 	mdp = mi->mi_drv;
502383Swnj 	/*
512383Swnj 	 * Since we clear attentions on the drive when we are
522383Swnj 	 * finished processing it, the fact that an attention
532383Swnj 	 * status shows indicated confusion in the hardware or our logic.
542383Swnj 	 */
552383Swnj 	if (mdp->mbd_as & (1 << mi->mi_drive)) {
562383Swnj 		printf("mbustart: ata on for %d\n", mi->mi_drive);
572383Swnj 		mdp->mbd_as = 1 << mi->mi_drive;
582383Swnj 	}
592383Swnj 	/*
602383Swnj 	 * Let the drivers unit start routine have at it
612383Swnj 	 * and then process the request further, per its instructions.
622383Swnj 	 */
632383Swnj 	switch ((*mi->mi_driver->md_ustart)(mi)) {
642383Swnj 
652383Swnj 	case MBU_NEXT:		/* request is complete (e.g. ``sense'') */
662383Swnj 		dprintf("mbu_next\n");
672383Swnj 		mi->mi_tab.b_active = 0;
682383Swnj 		mi->mi_tab.b_actf = bp->av_forw;
692383Swnj 		iodone(bp);
702383Swnj 		goto loop;
712383Swnj 
722383Swnj 	case MBU_DODATA:	/* all ready to do data transfer */
732383Swnj 		dprintf("mbu_dodata\n");
742383Swnj 		/*
752383Swnj 		 * Queue the device mba_info structure on the massbus
762383Swnj 		 * mba_hd structure for processing as soon as the
772383Swnj 		 * data path is available.
782383Swnj 		 */
792383Swnj 		mhp = mi->mi_hd;
802383Swnj 		mi->mi_forw = NULL;
812383Swnj 		if (mhp->mh_actf == NULL)
822383Swnj 			mhp->mh_actf = mi;
832383Swnj 		else
842383Swnj 			mhp->mh_actl->mi_forw = mi;
852383Swnj 		mhp->mh_actl = mi;
862383Swnj 		/*
872383Swnj 		 * If data path is idle, start transfer now.
882383Swnj 		 * In any case the device is ``active'' waiting for the
892383Swnj 		 * data to transfer.
902383Swnj 		 */
912383Swnj 		if (mhp->mh_active == 0)
922383Swnj 			mbstart(mhp);
932383Swnj 		mi->mi_tab.b_active = 1;
942383Swnj 		return;
952383Swnj 
962383Swnj 	case MBU_STARTED:	/* driver started a non-data transfer */
972383Swnj 		dprintf("mbu_started\n");
982383Swnj 		/*
992383Swnj 		 * Mark device busy during non-data transfer
1002383Swnj 		 * and count this as a ``seek'' on the device.
1012383Swnj 		 */
1022383Swnj 		if (mi->mi_dk >= 0)
1032383Swnj 			dk_seek[mi->mi_dk]++;
1042383Swnj 		mi->mi_tab.b_active = 1;
1052383Swnj 		return;
1062383Swnj 
1072383Swnj 	case MBU_BUSY:		/* dual port drive busy */
1082383Swnj 		dprintf("mbu_busy\n");
1092383Swnj 		/*
1102383Swnj 		 * We mark the device structure so that when an
1112383Swnj 		 * interrupt occurs we will know to restart the unit.
1122383Swnj 		 */
1132383Swnj 		mi->mi_tab.b_flags |= B_BUSY;
1142383Swnj 		return;
1152383Swnj 
1162383Swnj 	default:
1172383Swnj 		panic("mbustart");
1182383Swnj 	}
119*2403Skre }
1202383Swnj 
1212383Swnj /*
1222383Swnj  * Start an i/o operation on the massbus specified by the argument.
1232383Swnj  * We peel the first operation off its queue and insure that the drive
1242383Swnj  * is present and on-line.  We then use the drivers start routine
1252383Swnj  * (if any) to prepare the drive, setup the massbus map for the transfer
1262383Swnj  * and start the transfer.
1272383Swnj  */
1282383Swnj mbstart(mhp)
1292383Swnj 	register struct mba_hd *mhp;
1302383Swnj {
1312383Swnj 	register struct mba_info *mi;
1322383Swnj 	struct buf *bp;
1332383Swnj 	register struct mba_drv *daddr;
1342383Swnj 	register struct mba_regs *mbp;
1352383Swnj 
1362383Swnj 	dprintf("mbstart\n");
1372383Swnj loop:
1382383Swnj 	/*
1392383Swnj 	 * Look for an operation at the front of the queue.
1402383Swnj 	 */
1412383Swnj 	if ((mi = mhp->mh_actf) == NULL) {
1422383Swnj 		dprintf("nothing to do\n");
1432383Swnj 		return;
1442383Swnj 	}
1452383Swnj 	if ((bp = mi->mi_tab.b_actf) == NULL) {
1462383Swnj 		dprintf("nothing on actf\n");
1472383Swnj 		mhp->mh_actf = mi->mi_forw;
1482383Swnj 		goto loop;
1492383Swnj 	}
1502383Swnj 	/*
1512383Swnj 	 * If this device isn't present and on-line, then
1522383Swnj 	 * we screwed up, and can't really do the operation.
1532383Swnj 	 */
1542383Swnj 	if ((mi->mi_drv->mbd_ds & (MBD_DPR|MBD_MOL)) != (MBD_DPR|MBD_MOL)) {
1552383Swnj 		dprintf("not on line ds %x\n", mi->mi_drv->mbd_ds);
1562383Swnj 		mi->mi_tab.b_actf = bp->av_forw;
1572383Swnj 		bp->b_flags |= B_ERROR;
1582383Swnj 		iodone(bp);
1592383Swnj 		goto loop;
1602383Swnj 	}
1612383Swnj 	/*
1622383Swnj 	 * We can do the operation; mark the massbus active
1632383Swnj 	 * and let the device start routine setup any necessary
1642383Swnj 	 * device state for the transfer (e.g. desired cylinder, etc
1652383Swnj 	 * on disks).
1662383Swnj 	 */
1672383Swnj 	mhp->mh_active = 1;
1682383Swnj 	if (mi->mi_driver->md_start) {
1692383Swnj 		dprintf("md_start\n");
1702383Swnj 		(*mi->mi_driver->md_start)(mi);
1712383Swnj 	}
1722383Swnj 
1732383Swnj 	/*
1742383Swnj 	 * Setup the massbus control and map registers and start
1752383Swnj 	 * the transfer.
1762383Swnj 	 */
1772383Swnj 	dprintf("start mba\n");
1782383Swnj 	mbp = mi->mi_mba;
1792383Swnj 	mbp->mba_sr = -1;	/* conservative */
1802383Swnj 	mbp->mba_var = mbasetup(mi);
1812383Swnj 	mbp->mba_bcr = -bp->b_bcount;
1822383Swnj 	mi->mi_drv->mbd_cs1 =
1832383Swnj 	    (bp->b_flags & B_READ) ? MBD_RCOM|MBD_GO : MBD_WCOM|MBD_GO;
1842383Swnj 	if (mi->mi_dk >= 0) {
1852383Swnj 		dk_busy |= 1 << mi->mi_dk;
1862383Swnj 		dk_xfer[mi->mi_dk]++;
1872383Swnj 		dk_wds[mi->mi_dk] += bp->b_bcount >> 6;
1882383Swnj 	}
1892383Swnj }
1902383Swnj 
1912383Swnj /*
1922383Swnj  * Take an interrupt off of massbus mbanum,
1932383Swnj  * and dispatch to drivers as appropriate.
1942383Swnj  */
1952383Swnj mbintr(mbanum)
1962383Swnj 	int mbanum;
1972383Swnj {
1982383Swnj 	register struct mba_hd *mhp = &mba_hd[mbanum];
1992383Swnj 	register struct mba_regs *mbp = mhp->mh_mba;
2002383Swnj 	register struct mba_info *mi;
201420Sbill 	register struct buf *bp;
2022383Swnj 	register int drive;
2032383Swnj 	int mbastat, as;
2042383Swnj 
2052383Swnj 	/*
2062383Swnj 	 * Read out the massbus status register
2072383Swnj 	 * and attention status register and clear
2082383Swnj 	 * the bits in same by writing them back.
2092383Swnj 	 */
2102383Swnj 	mbastat = mbp->mba_sr;
2112383Swnj 	mbp->mba_sr = mbastat;
2122383Swnj 	/* note: the mbd_as register is shared between drives */
2132383Swnj 	as = mbp->mba_drv[0].mbd_as;
2142383Swnj 	mbp->mba_drv[0].mbd_as = as;
2152383Swnj 	dprintf("mbintr mbastat %x as %x\n", mbastat, as);
2162383Swnj 
2172383Swnj 	/*
2182383Swnj 	 * Disable interrupts from the massbus adapter
2192383Swnj 	 * for the duration of the operation of the massbus
2202383Swnj 	 * driver, so that spurious interrupts won't be generated.
2212383Swnj 	 */
2222383Swnj 	mbp->mba_cr &= ~MBAIE;
2232383Swnj 
2242383Swnj 	/*
2252383Swnj 	 * If the mba was active, process the data transfer
2262383Swnj 	 * complete interrupt; otherwise just process units which
2272383Swnj 	 * are now finished.
2282383Swnj 	 */
2292383Swnj 	if (mhp->mh_active) {
2302383Swnj 		if ((mbastat & MBS_DTCMP) == 0) {
2312383Swnj 			printf("mbintr(%d),b_active,no DTCMP!\n", mbanum);
2322383Swnj 			goto doattn;
233*2403Skre 		}
2342383Swnj 		/*
2352383Swnj 		 * Clear attention status for drive whose data
2362383Swnj 		 * transfer completed, and give the dtint driver
2372383Swnj 		 * routine a chance to say what is next.
2382383Swnj 		 */
2392383Swnj 		mi = mhp->mh_actf;
2402383Swnj 		as &= ~(1 << mi->mi_drive);
2412383Swnj 		dk_busy &= ~(1 << mi->mi_dk);
2422383Swnj 		bp = mi->mi_tab.b_actf;
2432383Swnj 		switch((*mi->mi_driver->md_dtint)(mi, mbastat)) {
2442383Swnj 
2452383Swnj 		case MBD_DONE:		/* all done, for better or worse */
2462383Swnj 			dprintf("mbd_done\n");
2472383Swnj 			/*
2482383Swnj 			 * Flush request from drive queue.
2492383Swnj 			 */
2502383Swnj 			mi->mi_tab.b_errcnt = 0;
2512383Swnj 			mi->mi_tab.b_actf = bp->av_forw;
2522383Swnj 			iodone(bp);
2532383Swnj 			/* fall into... */
2542383Swnj 		case MBD_RETRY:		/* attempt the operation again */
2552383Swnj 			dprintf("mbd_retry\n");
2562383Swnj 			/*
2572383Swnj 			 * Dequeue data transfer from massbus queue;
2582383Swnj 			 * if there is still a i/o request on the device
2592383Swnj 			 * queue then start the next operation on the device.
2602383Swnj 			 * (Common code for DONE and RETRY).
2612383Swnj 			 */
2622383Swnj 			mhp->mh_active = 0;
2632383Swnj 			mi->mi_tab.b_active = 0;
2642383Swnj 			mhp->mh_actf = mi->mi_forw;
2652383Swnj 			if (mi->mi_tab.b_actf)
2662383Swnj 				mbustart(mi);
2672383Swnj 			break;
2682383Swnj 
2692383Swnj 		case MBD_RESTARTED:	/* driver restarted op (ecc, e.g.)
2702383Swnj 			dprintf("mbd_restarted\n");
2712383Swnj 			/*
2722383Swnj 			 * Note that mp->b_active is still on.
2732383Swnj 			 */
2742383Swnj 			break;
2752383Swnj 
2762383Swnj 		default:
2772383Swnj 			panic("mbaintr");
2782383Swnj 		}
2792383Swnj 	} else {
2802383Swnj 		dprintf("!dtcmp\n");
2812383Swnj 		if (mbastat & MBS_DTCMP)
2822383Swnj 			printf("mbaintr,DTCMP,!b_active\n");
2832383Swnj 	}
2842383Swnj doattn:
2852383Swnj 	/*
2862383Swnj 	 * Service drives which require attention
2872383Swnj 	 * after non-data-transfer operations.
2882383Swnj 	 */
2892383Swnj 	for (drive = 0; as && drive < 8; drive++)
2902383Swnj 		if (as & (1 << drive)) {
2912383Swnj 			dprintf("service as %d\n", drive);
2922383Swnj 			as &= ~(1 << drive);
2932383Swnj 			/*
2942383Swnj 			 * Consistency check the implied attention,
2952383Swnj 			 * to make sure the drive should have interrupted.
2962383Swnj 			 */
2972383Swnj 			mi = mhp->mh_mbip[drive];
2982383Swnj 			if (mi == NULL)
2992383Swnj 				goto random;		/* no such drive */
3002383Swnj 			if (mi->mi_tab.b_active == 0 &&
3012383Swnj 			    (mi->mi_tab.b_flags&B_BUSY) == 0)
3022383Swnj 				goto random;		/* not active */
3032383Swnj 			if ((bp = mi->mi_tab.b_actf) == NULL) {
3042383Swnj 							/* nothing doing */
3052383Swnj random:
3062383Swnj 				printf("random mbaintr %d %d\n",mbanum,drive);
3072383Swnj 				continue;
3082383Swnj 			}
3092383Swnj 			/*
3102383Swnj 			 * If this interrupt wasn't a notification that
3112383Swnj 			 * a dual ported drive is available, and if the
3122383Swnj 			 * driver has a handler for non-data transfer
3132383Swnj 			 * interrupts, give it a chance to tell us that
3142383Swnj 			 * the operation needs to be redone
3152383Swnj 			 */
3162383Swnj 			if ((mi->mi_tab.b_flags&B_BUSY) == 0 &&
3172383Swnj 			    mi->mi_driver->md_ndint) {
3182383Swnj 				mi->mi_tab.b_active = 0;
3192383Swnj 				switch((*mi->mi_driver->md_ndint)(mi)) {
3202383Swnj 
3212383Swnj 				case MBN_DONE:
3222383Swnj 					dprintf("mbn_done\n");
3232383Swnj 					/*
3242383Swnj 					 * Non-data transfer interrupt
3252383Swnj 					 * completed i/o request's processing.
3262383Swnj 					 */
3272383Swnj 					mi->mi_tab.b_errcnt = 0;
3282383Swnj 					mi->mi_tab.b_actf = bp->av_forw;
3292383Swnj 					iodone(bp);
3302383Swnj 					/* fall into... */
3312383Swnj 				case MBN_RETRY:
3322383Swnj 					dprintf("mbn_retry\n");
3332383Swnj 					if (mi->mi_tab.b_actf)
3342383Swnj 						mbustart(mi);
3352383Swnj 					break;
3362383Swnj 
3372383Swnj 				default:
3382383Swnj 					panic("mbintr ndint");
3392383Swnj 				}
3402383Swnj 			} else
3412383Swnj 				mbustart(mi);
3422383Swnj 		}
3432383Swnj 	/*
3442383Swnj 	 * If there is an operation available and
3452383Swnj 	 * the massbus isn't active, get it going.
3462383Swnj 	 */
3472383Swnj 	if (mhp->mh_actf && !mhp->mh_active)
3482383Swnj 		mbstart(mhp);
3492383Swnj 	mbp->mba_cr |= MBAIE;
3502383Swnj }
3512383Swnj 
3522383Swnj /*
3532383Swnj  * Setup the mapping registers for a transfer.
3542383Swnj  */
3552383Swnj mbasetup(mi)
3562383Swnj 	register struct mba_info *mi;
35728Sbill {
3582383Swnj 	register struct mba_regs *mbap = mi->mi_mba;
3592383Swnj 	struct buf *bp = mi->mi_tab.b_actf;
36028Sbill 	register int i;
36128Sbill 	int npf;
36228Sbill 	unsigned v;
36328Sbill 	register struct pte *pte, *io;
36428Sbill 	int o;
36528Sbill 	int vaddr;
36628Sbill 	struct proc *rp;
36728Sbill 
3681412Sbill 	io = mbap->mba_map;
3691412Sbill 	v = btop(bp->b_un.b_addr);
3701412Sbill 	o = (int)bp->b_un.b_addr & PGOFSET;
3711412Sbill 	npf = btoc(bp->b_bcount + o);
3721412Sbill 	rp = bp->b_flags&B_DIRTY ? &proc[2] : bp->b_proc;
3731412Sbill 	vaddr = o;
3741412Sbill 	if (bp->b_flags & B_UAREA) {
3751412Sbill 		for (i = 0; i < UPAGES; i++) {
3761412Sbill 			if (rp->p_addr[i].pg_pfnum == 0)
3771412Sbill 				panic("mba: zero upage");
3781412Sbill 			*(int *)io++ = rp->p_addr[i].pg_pfnum | PG_V;
37928Sbill 		}
3801412Sbill 	} else if ((bp->b_flags & B_PHYS) == 0) {
3811412Sbill 		pte = &Sysmap[btop(((int)bp->b_un.b_addr)&0x7fffffff)];
3821412Sbill 		while (--npf >= 0)
3831412Sbill 			*(int *)io++ = pte++->pg_pfnum | PG_V;
3841412Sbill 	} else {
3851412Sbill 		if (bp->b_flags & B_PAGET)
3861412Sbill 			pte = &Usrptmap[btokmx((struct pte *)bp->b_un.b_addr)];
3871412Sbill 		else
3881412Sbill 			pte = vtopte(rp, v);
3891412Sbill 		while (--npf >= 0) {
3901412Sbill 			if (pte->pg_pfnum == 0)
3911412Sbill 				panic("mba, zero entry");
3921412Sbill 			*(int *)io++ = pte++->pg_pfnum | PG_V;
3931412Sbill 		}
39428Sbill 	}
3951412Sbill 	*(int *)io++ = 0;
3962383Swnj 	return (vaddr);
39728Sbill }
398