xref: /csrg-svn/sys/vax/mba/mba.c (revision 2893)
1*2893Swnj /*	mba.c	4.13	81/03/03	*/
228Sbill 
32704Swnj #include "mba.h"
42704Swnj #if NMBA > 0
52383Swnj /*
62884Swnj  * Massbus driver; arbitrates massbus using device
72884Swnj  * driver routines.  This module provides common functions.
82383Swnj  */
928Sbill #include "../h/param.h"
102383Swnj #include "../h/systm.h"
112383Swnj #include "../h/dk.h"
1228Sbill #include "../h/buf.h"
1328Sbill #include "../h/conf.h"
1428Sbill #include "../h/dir.h"
1528Sbill #include "../h/user.h"
1628Sbill #include "../h/proc.h"
172383Swnj #include "../h/map.h"
1828Sbill #include "../h/pte.h"
1928Sbill #include "../h/mba.h"
2028Sbill #include "../h/mtpr.h"
2128Sbill #include "../h/vm.h"
2228Sbill 
232673Swnj char	mbasr_bits[] = MBASR_BITS;
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 loop:
422383Swnj 	/*
432383Swnj 	 * Get the first thing to do off device queue.
442383Swnj 	 */
452383Swnj 	bp = mi->mi_tab.b_actf;
462383Swnj 	if (bp == NULL)
472383Swnj 		return;
482383Swnj 	mdp = mi->mi_drv;
492383Swnj 	/*
502383Swnj 	 * Let the drivers unit start routine have at it
512383Swnj 	 * and then process the request further, per its instructions.
522383Swnj 	 */
532383Swnj 	switch ((*mi->mi_driver->md_ustart)(mi)) {
542383Swnj 
552383Swnj 	case MBU_NEXT:		/* request is complete (e.g. ``sense'') */
562383Swnj 		mi->mi_tab.b_active = 0;
572383Swnj 		mi->mi_tab.b_actf = bp->av_forw;
582383Swnj 		iodone(bp);
592383Swnj 		goto loop;
602383Swnj 
612383Swnj 	case MBU_DODATA:	/* all ready to do data transfer */
622383Swnj 		/*
632383Swnj 		 * Queue the device mba_info structure on the massbus
642383Swnj 		 * mba_hd structure for processing as soon as the
652383Swnj 		 * data path is available.
662383Swnj 		 */
672383Swnj 		mhp = mi->mi_hd;
682383Swnj 		mi->mi_forw = NULL;
692383Swnj 		if (mhp->mh_actf == NULL)
702383Swnj 			mhp->mh_actf = mi;
712383Swnj 		else
722383Swnj 			mhp->mh_actl->mi_forw = mi;
732383Swnj 		mhp->mh_actl = mi;
742383Swnj 		/*
752383Swnj 		 * If data path is idle, start transfer now.
762383Swnj 		 * In any case the device is ``active'' waiting for the
772383Swnj 		 * data to transfer.
782383Swnj 		 */
79*2893Swnj 		mi->mi_tab.b_active = 1;
802383Swnj 		if (mhp->mh_active == 0)
812383Swnj 			mbstart(mhp);
822383Swnj 		return;
832383Swnj 
842383Swnj 	case MBU_STARTED:	/* driver started a non-data transfer */
852383Swnj 		/*
862383Swnj 		 * Mark device busy during non-data transfer
872383Swnj 		 * and count this as a ``seek'' on the device.
882383Swnj 		 */
892383Swnj 		if (mi->mi_dk >= 0)
902383Swnj 			dk_seek[mi->mi_dk]++;
912383Swnj 		mi->mi_tab.b_active = 1;
922383Swnj 		return;
932383Swnj 
942383Swnj 	case MBU_BUSY:		/* dual port drive busy */
952383Swnj 		/*
962383Swnj 		 * We mark the device structure so that when an
972383Swnj 		 * interrupt occurs we will know to restart the unit.
982383Swnj 		 */
992383Swnj 		mi->mi_tab.b_flags |= B_BUSY;
1002383Swnj 		return;
1012383Swnj 
1022383Swnj 	default:
1032383Swnj 		panic("mbustart");
1042383Swnj 	}
1052403Skre }
1062383Swnj 
1072383Swnj /*
1082383Swnj  * Start an i/o operation on the massbus specified by the argument.
1092383Swnj  * We peel the first operation off its queue and insure that the drive
1102383Swnj  * is present and on-line.  We then use the drivers start routine
1112383Swnj  * (if any) to prepare the drive, setup the massbus map for the transfer
1122383Swnj  * and start the transfer.
1132383Swnj  */
1142383Swnj mbstart(mhp)
1152383Swnj 	register struct mba_hd *mhp;
1162383Swnj {
1172383Swnj 	register struct mba_info *mi;
1182383Swnj 	struct buf *bp;
1192383Swnj 	register struct mba_regs *mbp;
1202383Swnj 
1212383Swnj loop:
1222383Swnj 	/*
1232383Swnj 	 * Look for an operation at the front of the queue.
1242383Swnj 	 */
1252884Swnj 	if ((mi = mhp->mh_actf) == NULL)
1262383Swnj 		return;
1272383Swnj 	if ((bp = mi->mi_tab.b_actf) == NULL) {
1282383Swnj 		mhp->mh_actf = mi->mi_forw;
1292383Swnj 		goto loop;
1302383Swnj 	}
1312383Swnj 	/*
1322383Swnj 	 * If this device isn't present and on-line, then
1332383Swnj 	 * we screwed up, and can't really do the operation.
1342383Swnj 	 */
1352383Swnj 	if ((mi->mi_drv->mbd_ds & (MBD_DPR|MBD_MOL)) != (MBD_DPR|MBD_MOL)) {
1362884Swnj 		printf("%c%d not ready\n", mi->mi_name, dkunit(bp));
1372383Swnj 		mi->mi_tab.b_actf = bp->av_forw;
138*2893Swnj 		mi->mi_tab.b_errcnt = 0;
139*2893Swnj 		mi->mi_tab.b_active = 0;
1402383Swnj 		bp->b_flags |= B_ERROR;
1412383Swnj 		iodone(bp);
1422383Swnj 		goto loop;
1432383Swnj 	}
1442383Swnj 	/*
1452383Swnj 	 * We can do the operation; mark the massbus active
1462383Swnj 	 * and let the device start routine setup any necessary
1472383Swnj 	 * device state for the transfer (e.g. desired cylinder, etc
1482383Swnj 	 * on disks).
1492383Swnj 	 */
1502383Swnj 	mhp->mh_active = 1;
1512884Swnj 	if (mi->mi_driver->md_start)
1522383Swnj 		(*mi->mi_driver->md_start)(mi);
1532383Swnj 
1542383Swnj 	/*
1552383Swnj 	 * Setup the massbus control and map registers and start
1562383Swnj 	 * the transfer.
1572383Swnj 	 */
1582383Swnj 	mbp = mi->mi_mba;
1592383Swnj 	mbp->mba_sr = -1;	/* conservative */
1602383Swnj 	mbp->mba_var = mbasetup(mi);
1612383Swnj 	mbp->mba_bcr = -bp->b_bcount;
1622383Swnj 	mi->mi_drv->mbd_cs1 =
1632383Swnj 	    (bp->b_flags & B_READ) ? MBD_RCOM|MBD_GO : MBD_WCOM|MBD_GO;
1642383Swnj 	if (mi->mi_dk >= 0) {
1652383Swnj 		dk_busy |= 1 << mi->mi_dk;
1662383Swnj 		dk_xfer[mi->mi_dk]++;
1672383Swnj 		dk_wds[mi->mi_dk] += bp->b_bcount >> 6;
1682383Swnj 	}
1692383Swnj }
1702383Swnj 
1712383Swnj /*
1722383Swnj  * Take an interrupt off of massbus mbanum,
1732383Swnj  * and dispatch to drivers as appropriate.
1742383Swnj  */
1752383Swnj mbintr(mbanum)
1762383Swnj 	int mbanum;
1772383Swnj {
1782383Swnj 	register struct mba_hd *mhp = &mba_hd[mbanum];
1792383Swnj 	register struct mba_regs *mbp = mhp->mh_mba;
1802383Swnj 	register struct mba_info *mi;
181420Sbill 	register struct buf *bp;
1822383Swnj 	register int drive;
1832383Swnj 	int mbastat, as;
1842383Swnj 
1852383Swnj 	/*
1862383Swnj 	 * Read out the massbus status register
1872383Swnj 	 * and attention status register and clear
1882383Swnj 	 * the bits in same by writing them back.
1892383Swnj 	 */
1902383Swnj 	mbastat = mbp->mba_sr;
1912383Swnj 	mbp->mba_sr = mbastat;
1922884Swnj #if VAX750
1932884Swnj 	if (mbastat&MBS_CBHUNG)
1942884Swnj 		panic("mba CBHUNG");
1952884Swnj #endif
1962383Swnj 	/* note: the mbd_as register is shared between drives */
1972383Swnj 	as = mbp->mba_drv[0].mbd_as;
1982383Swnj 	mbp->mba_drv[0].mbd_as = as;
1992383Swnj 
2002383Swnj 	/*
2012383Swnj 	 * Disable interrupts from the massbus adapter
2022383Swnj 	 * for the duration of the operation of the massbus
2032383Swnj 	 * driver, so that spurious interrupts won't be generated.
2042383Swnj 	 */
2052383Swnj 	mbp->mba_cr &= ~MBAIE;
2062383Swnj 
2072383Swnj 	/*
2082383Swnj 	 * If the mba was active, process the data transfer
2092383Swnj 	 * complete interrupt; otherwise just process units which
2102383Swnj 	 * are now finished.
2112383Swnj 	 */
2122383Swnj 	if (mhp->mh_active) {
2132383Swnj 		/*
2142383Swnj 		 * Clear attention status for drive whose data
2152383Swnj 		 * transfer completed, and give the dtint driver
2162383Swnj 		 * routine a chance to say what is next.
2172383Swnj 		 */
2182383Swnj 		mi = mhp->mh_actf;
2192383Swnj 		as &= ~(1 << mi->mi_drive);
2202383Swnj 		dk_busy &= ~(1 << mi->mi_dk);
2212383Swnj 		bp = mi->mi_tab.b_actf;
2222383Swnj 		switch((*mi->mi_driver->md_dtint)(mi, mbastat)) {
2232383Swnj 
2242383Swnj 		case MBD_DONE:		/* all done, for better or worse */
2252383Swnj 			/*
2262383Swnj 			 * Flush request from drive queue.
2272383Swnj 			 */
2282383Swnj 			mi->mi_tab.b_errcnt = 0;
2292383Swnj 			mi->mi_tab.b_actf = bp->av_forw;
2302383Swnj 			iodone(bp);
2312383Swnj 			/* fall into... */
2322383Swnj 		case MBD_RETRY:		/* attempt the operation again */
2332383Swnj 			/*
2342383Swnj 			 * Dequeue data transfer from massbus queue;
2352383Swnj 			 * if there is still a i/o request on the device
2362383Swnj 			 * queue then start the next operation on the device.
2372383Swnj 			 * (Common code for DONE and RETRY).
2382383Swnj 			 */
2392383Swnj 			mhp->mh_active = 0;
2402383Swnj 			mi->mi_tab.b_active = 0;
2412383Swnj 			mhp->mh_actf = mi->mi_forw;
2422383Swnj 			if (mi->mi_tab.b_actf)
2432383Swnj 				mbustart(mi);
2442383Swnj 			break;
2452383Swnj 
2462383Swnj 		case MBD_RESTARTED:	/* driver restarted op (ecc, e.g.)
2472383Swnj 			/*
248*2893Swnj 			 * Note that mhp->mh_active is still on.
2492383Swnj 			 */
2502383Swnj 			break;
2512383Swnj 
2522383Swnj 		default:
2532884Swnj 			panic("mbintr");
2542383Swnj 		}
2552383Swnj 	}
2562383Swnj doattn:
2572383Swnj 	/*
2582383Swnj 	 * Service drives which require attention
2592383Swnj 	 * after non-data-transfer operations.
2602383Swnj 	 */
2612383Swnj 	for (drive = 0; as && drive < 8; drive++)
2622383Swnj 		if (as & (1 << drive)) {
2632383Swnj 			as &= ~(1 << drive);
2642383Swnj 			/*
2652383Swnj 			 * Consistency check the implied attention,
2662383Swnj 			 * to make sure the drive should have interrupted.
2672383Swnj 			 */
2682383Swnj 			mi = mhp->mh_mbip[drive];
2692884Swnj 			if (mi == NULL || mi->mi_tab.b_active == 0 &&
2702884Swnj 			    (mi->mi_tab.b_flags&B_BUSY) == 0 ||
2712884Swnj 			    (bp = mi->mi_tab.b_actf) == NULL)
2722884Swnj 				continue;		/* unsolicited */
2732383Swnj 			/*
2742383Swnj 			 * If this interrupt wasn't a notification that
2752383Swnj 			 * a dual ported drive is available, and if the
2762383Swnj 			 * driver has a handler for non-data transfer
2772383Swnj 			 * interrupts, give it a chance to tell us that
2782383Swnj 			 * the operation needs to be redone
2792383Swnj 			 */
280*2893Swnj 			if (mi->mi_driver->md_ndint &&
281*2893Swnj 			    (mi->mi_tab.b_flags&B_BUSY) == 0) {
2822383Swnj 				mi->mi_tab.b_active = 0;
2832383Swnj 				switch((*mi->mi_driver->md_ndint)(mi)) {
2842383Swnj 
2852383Swnj 				case MBN_DONE:
2862383Swnj 					/*
2872383Swnj 					 * Non-data transfer interrupt
2882383Swnj 					 * completed i/o request's processing.
2892383Swnj 					 */
2902383Swnj 					mi->mi_tab.b_errcnt = 0;
2912383Swnj 					mi->mi_tab.b_actf = bp->av_forw;
2922383Swnj 					iodone(bp);
2932383Swnj 					/* fall into... */
2942383Swnj 				case MBN_RETRY:
2952383Swnj 					if (mi->mi_tab.b_actf)
2962383Swnj 						mbustart(mi);
2972383Swnj 					break;
2982383Swnj 
2992383Swnj 				default:
3002884Swnj 					panic("mbintr");
3012383Swnj 				}
3022383Swnj 			} else
3032383Swnj 				mbustart(mi);
3042383Swnj 		}
3052383Swnj 	/*
3062383Swnj 	 * If there is an operation available and
3072383Swnj 	 * the massbus isn't active, get it going.
3082383Swnj 	 */
3092383Swnj 	if (mhp->mh_actf && !mhp->mh_active)
3102383Swnj 		mbstart(mhp);
3112383Swnj 	mbp->mba_cr |= MBAIE;
3122383Swnj }
3132383Swnj 
3142383Swnj /*
3152383Swnj  * Setup the mapping registers for a transfer.
3162383Swnj  */
3172383Swnj mbasetup(mi)
3182383Swnj 	register struct mba_info *mi;
31928Sbill {
3202383Swnj 	register struct mba_regs *mbap = mi->mi_mba;
3212383Swnj 	struct buf *bp = mi->mi_tab.b_actf;
32228Sbill 	register int i;
32328Sbill 	int npf;
32428Sbill 	unsigned v;
32528Sbill 	register struct pte *pte, *io;
32628Sbill 	int o;
32728Sbill 	int vaddr;
32828Sbill 	struct proc *rp;
32928Sbill 
3301412Sbill 	io = mbap->mba_map;
3311412Sbill 	v = btop(bp->b_un.b_addr);
3321412Sbill 	o = (int)bp->b_un.b_addr & PGOFSET;
3331412Sbill 	npf = btoc(bp->b_bcount + o);
3341412Sbill 	rp = bp->b_flags&B_DIRTY ? &proc[2] : bp->b_proc;
3351412Sbill 	vaddr = o;
3361412Sbill 	if (bp->b_flags & B_UAREA) {
3371412Sbill 		for (i = 0; i < UPAGES; i++) {
3381412Sbill 			if (rp->p_addr[i].pg_pfnum == 0)
3391412Sbill 				panic("mba: zero upage");
3401412Sbill 			*(int *)io++ = rp->p_addr[i].pg_pfnum | PG_V;
34128Sbill 		}
3421412Sbill 	} else if ((bp->b_flags & B_PHYS) == 0) {
3431412Sbill 		pte = &Sysmap[btop(((int)bp->b_un.b_addr)&0x7fffffff)];
3441412Sbill 		while (--npf >= 0)
3451412Sbill 			*(int *)io++ = pte++->pg_pfnum | PG_V;
3461412Sbill 	} else {
3471412Sbill 		if (bp->b_flags & B_PAGET)
3481412Sbill 			pte = &Usrptmap[btokmx((struct pte *)bp->b_un.b_addr)];
3491412Sbill 		else
3501412Sbill 			pte = vtopte(rp, v);
3511412Sbill 		while (--npf >= 0) {
3521412Sbill 			if (pte->pg_pfnum == 0)
3531412Sbill 				panic("mba, zero entry");
3541412Sbill 			*(int *)io++ = pte++->pg_pfnum | PG_V;
3551412Sbill 		}
35628Sbill 	}
3571412Sbill 	*(int *)io++ = 0;
3582383Swnj 	return (vaddr);
35928Sbill }
3602704Swnj #endif
361