123312Smckusick /* 230535Skarels * Copyright (c) 1982 Regents of the University of California. 323312Smckusick * All rights reserved. The Berkeley software License Agreement 423312Smckusick * specifies the terms and conditions for redistribution. 523312Smckusick * 6*45802Sbostic * @(#)mba.c 7.5 (Berkeley) 12/16/90 723312Smckusick */ 828Sbill 92704Swnj #include "mba.h" 102704Swnj #if NMBA > 0 112383Swnj /* 123095Swnj * Massbus driver, arbitrates a massbus among attached devices. 132383Swnj */ 14*45802Sbostic #include "../include/pte.h" 159788Ssam 16*45802Sbostic #include "sys/param.h" 17*45802Sbostic #include "sys/systm.h" 18*45802Sbostic #include "sys/dkstat.h" 19*45802Sbostic #include "sys/buf.h" 20*45802Sbostic #include "sys/conf.h" 21*45802Sbostic #include "sys/user.h" 22*45802Sbostic #include "sys/proc.h" 23*45802Sbostic #include "sys/map.h" 24*45802Sbostic #include "../include/mtpr.h" 25*45802Sbostic #include "sys/vm.h" 2628Sbill 2717121Sbloom #include "mbareg.h" 2817121Sbloom #include "mbavar.h" 298470Sroot 3024779Skarels /* mbunit should be the same as hpunit, etc.! */ 3124779Skarels #define mbunit(dev) (minor(dev) >> 3) 3224779Skarels 333095Swnj char mbsr_bits[] = MBSR_BITS; 3428Sbill /* 352383Swnj * Start activity on a massbus device. 362981Swnj * We are given the device's mba_device structure and activate 372383Swnj * the device via the unit start routine. The unit start 382383Swnj * routine may indicate that it is finished (e.g. if the operation 392383Swnj * was a ``sense'' on a tape drive), that the (multi-ported) unit 402383Swnj * is busy (we will get an interrupt later), that it started the 412383Swnj * unit (e.g. for a non-data transfer operation), or that it has 422383Swnj * set up a data transfer operation and we should start the massbus adaptor. 4328Sbill */ 442383Swnj mbustart(mi) 452981Swnj register struct mba_device *mi; 462383Swnj { 472383Swnj register struct buf *bp; /* i/o operation at head of queue */ 482383Swnj register struct mba_hd *mhp; /* header for mba device is on */ 4928Sbill 502383Swnj loop: 512383Swnj /* 522383Swnj * Get the first thing to do off device queue. 532383Swnj */ 542383Swnj bp = mi->mi_tab.b_actf; 552383Swnj if (bp == NULL) 562383Swnj return; 572383Swnj /* 582383Swnj * Let the drivers unit start routine have at it 592383Swnj * and then process the request further, per its instructions. 602383Swnj */ 612383Swnj switch ((*mi->mi_driver->md_ustart)(mi)) { 622383Swnj 632383Swnj case MBU_NEXT: /* request is complete (e.g. ``sense'') */ 642383Swnj mi->mi_tab.b_active = 0; 652955Swnj mi->mi_tab.b_errcnt = 0; 662383Swnj mi->mi_tab.b_actf = bp->av_forw; 6730535Skarels biodone(bp); 682383Swnj goto loop; 692383Swnj 702383Swnj case MBU_DODATA: /* all ready to do data transfer */ 712383Swnj /* 722981Swnj * Queue the device mba_device structure on the massbus 732383Swnj * mba_hd structure for processing as soon as the 742383Swnj * data path is available. 752383Swnj */ 762383Swnj mhp = mi->mi_hd; 772383Swnj mi->mi_forw = NULL; 782383Swnj if (mhp->mh_actf == NULL) 792383Swnj mhp->mh_actf = mi; 802383Swnj else 812383Swnj mhp->mh_actl->mi_forw = mi; 822383Swnj mhp->mh_actl = mi; 832383Swnj /* 842383Swnj * If data path is idle, start transfer now. 852383Swnj * In any case the device is ``active'' waiting for the 862383Swnj * data to transfer. 872383Swnj */ 882893Swnj mi->mi_tab.b_active = 1; 892383Swnj if (mhp->mh_active == 0) 902383Swnj mbstart(mhp); 912383Swnj return; 922383Swnj 932383Swnj case MBU_STARTED: /* driver started a non-data transfer */ 942383Swnj /* 952383Swnj * Mark device busy during non-data transfer 962383Swnj * and count this as a ``seek'' on the device. 972383Swnj */ 983182Swnj if (mi->mi_dk >= 0) { 992383Swnj dk_seek[mi->mi_dk]++; 1003182Swnj dk_busy |= (1 << mi->mi_dk); 1013182Swnj } 1022383Swnj mi->mi_tab.b_active = 1; 1032383Swnj return; 1042383Swnj 1052383Swnj case MBU_BUSY: /* dual port drive busy */ 1062383Swnj /* 1072383Swnj * We mark the device structure so that when an 1082383Swnj * interrupt occurs we will know to restart the unit. 1092383Swnj */ 1102383Swnj mi->mi_tab.b_flags |= B_BUSY; 1112383Swnj return; 1122383Swnj 1132383Swnj default: 1142383Swnj panic("mbustart"); 1152383Swnj } 1162403Skre } 1172383Swnj 1182383Swnj /* 1192383Swnj * Start an i/o operation on the massbus specified by the argument. 1202383Swnj * We peel the first operation off its queue and insure that the drive 1212383Swnj * is present and on-line. We then use the drivers start routine 1222383Swnj * (if any) to prepare the drive, setup the massbus map for the transfer 1232383Swnj * and start the transfer. 1242383Swnj */ 1252383Swnj mbstart(mhp) 1262383Swnj register struct mba_hd *mhp; 1272383Swnj { 1282981Swnj register struct mba_device *mi; 1292383Swnj struct buf *bp; 1302383Swnj register struct mba_regs *mbp; 1313708Sroot register int com; 13230535Skarels extern int cold; 1332383Swnj 1342383Swnj loop: 1352383Swnj /* 1362383Swnj * Look for an operation at the front of the queue. 1372383Swnj */ 1382955Swnj if ((mi = mhp->mh_actf) == NULL) { 1392383Swnj return; 1402955Swnj } 1412383Swnj if ((bp = mi->mi_tab.b_actf) == NULL) { 1422383Swnj mhp->mh_actf = mi->mi_forw; 1432383Swnj goto loop; 1442383Swnj } 1452383Swnj /* 1462383Swnj * If this device isn't present and on-line, then 1472383Swnj * we screwed up, and can't really do the operation. 1484757Swnj * Only check for non-tapes because tape drivers check 1494757Swnj * ONLINE themselves and because TU78 registers are 1504757Swnj * different. 15130535Skarels * No complaints during autoconfiguration, 15230535Skarels * when we try to read disk labels from anything on line. 1532383Swnj */ 1546537Ssam if (((com = mi->mi_drv->mbd_dt) & MBDT_TAP) == 0) 1553095Swnj if ((mi->mi_drv->mbd_ds & MBDS_DREADY) != MBDS_DREADY) { 15630535Skarels if (!cold) { 15730535Skarels if ((com & MBDT_TYPE) == 0) { 1586537Ssam mi->mi_alive = 0; 1596537Ssam printf("%s%d: nonexistent\n", mi->mi_driver->md_dname, 16024779Skarels mbunit(bp->b_dev)); 16130535Skarels } else 1626537Ssam printf("%s%d: not ready\n", mi->mi_driver->md_dname, 16324779Skarels mbunit(bp->b_dev)); 16430535Skarels } 1652383Swnj mi->mi_tab.b_actf = bp->av_forw; 1662893Swnj mi->mi_tab.b_errcnt = 0; 1672893Swnj mi->mi_tab.b_active = 0; 1682383Swnj bp->b_flags |= B_ERROR; 16930535Skarels biodone(bp); 1702383Swnj goto loop; 1712383Swnj } 1722383Swnj /* 1732383Swnj * We can do the operation; mark the massbus active 1742383Swnj * and let the device start routine setup any necessary 1752383Swnj * device state for the transfer (e.g. desired cylinder, etc 1762383Swnj * on disks). 1772383Swnj */ 1782383Swnj mhp->mh_active = 1; 17926040Skarels if (mi->mi_driver->md_start == (int (*)())0 || 18026040Skarels (com = (*mi->mi_driver->md_start)(mi)) == 0) 1813708Sroot com = (bp->b_flags & B_READ) ? MB_RCOM|MB_GO : MB_WCOM|MB_GO; 1822383Swnj 1832383Swnj /* 1842383Swnj * Setup the massbus control and map registers and start 1852383Swnj * the transfer. 1862383Swnj */ 1872383Swnj mbp = mi->mi_mba; 1882383Swnj mbp->mba_sr = -1; /* conservative */ 18917215Smckusick if (bp->b_bcount >= 0) { 19026040Skarels mbp->mba_var = mbasetup(mi) + mi->mi_tab.b_bdone; 19126040Skarels mbp->mba_bcr = -(bp->b_bcount - mi->mi_tab.b_bdone); 19217215Smckusick } else { 19326040Skarels mbp->mba_var = mbasetup(mi) - bp->b_bcount - mi->mi_tab.b_bdone - 1; 19426040Skarels mbp->mba_bcr = bp->b_bcount + mi->mi_tab.b_bdone; 19517215Smckusick } 1963708Sroot mi->mi_drv->mbd_cs1 = com; 1972383Swnj if (mi->mi_dk >= 0) { 1982383Swnj dk_busy |= 1 << mi->mi_dk; 1992383Swnj dk_xfer[mi->mi_dk]++; 20017215Smckusick if (bp->b_bcount >= 0) 20117215Smckusick dk_wds[mi->mi_dk] += bp->b_bcount >> 6; 20217215Smckusick else 20317215Smckusick dk_wds[mi->mi_dk] += -(bp->b_bcount) >> 6; 2042383Swnj } 2052383Swnj } 2062383Swnj 2072383Swnj /* 2082383Swnj * Take an interrupt off of massbus mbanum, 2092383Swnj * and dispatch to drivers as appropriate. 2102383Swnj */ 2112383Swnj mbintr(mbanum) 2122383Swnj int mbanum; 2132383Swnj { 2142383Swnj register struct mba_hd *mhp = &mba_hd[mbanum]; 2152383Swnj register struct mba_regs *mbp = mhp->mh_mba; 2162981Swnj register struct mba_device *mi; 217420Sbill register struct buf *bp; 2182383Swnj register int drive; 2192955Swnj int mbasr, as; 2206537Ssam extern struct mba_device *mbaconfig(); 2212383Swnj 2222383Swnj /* 2232383Swnj * Read out the massbus status register 2242383Swnj * and attention status register and clear 2252383Swnj * the bits in same by writing them back. 2262383Swnj */ 2272955Swnj mbasr = mbp->mba_sr; 2282955Swnj mbp->mba_sr = mbasr; 2292884Swnj #if VAX750 2303095Swnj if (mbasr&MBSR_CBHUNG) { 2312930Swnj printf("mba%d: control bus hung\n", mbanum); 2322930Swnj panic("cbhung"); 2332930Swnj } 2342884Swnj #endif 2352383Swnj /* note: the mbd_as register is shared between drives */ 2362955Swnj as = mbp->mba_drv[0].mbd_as & 0xff; 2372383Swnj mbp->mba_drv[0].mbd_as = as; 2382383Swnj 2392383Swnj /* 2402383Swnj * If the mba was active, process the data transfer 2412383Swnj * complete interrupt; otherwise just process units which 2422383Swnj * are now finished. 2432383Swnj */ 2442383Swnj if (mhp->mh_active) { 2452383Swnj /* 2462383Swnj * Clear attention status for drive whose data 2473095Swnj * transfer related operation completed, 2483095Swnj * and give the dtint driver 2492383Swnj * routine a chance to say what is next. 2502383Swnj */ 2512383Swnj mi = mhp->mh_actf; 2522383Swnj as &= ~(1 << mi->mi_drive); 2532383Swnj dk_busy &= ~(1 << mi->mi_dk); 2542383Swnj bp = mi->mi_tab.b_actf; 2553095Swnj switch ((*mi->mi_driver->md_dtint)(mi, mbasr)) { 2562383Swnj 2572383Swnj case MBD_DONE: /* all done, for better or worse */ 2582383Swnj /* 2592383Swnj * Flush request from drive queue. 2602383Swnj */ 2612383Swnj mi->mi_tab.b_errcnt = 0; 2622383Swnj mi->mi_tab.b_actf = bp->av_forw; 26330535Skarels biodone(bp); 2642383Swnj /* fall into... */ 2652383Swnj case MBD_RETRY: /* attempt the operation again */ 2662383Swnj /* 2672383Swnj * Dequeue data transfer from massbus queue; 2682383Swnj * if there is still a i/o request on the device 2692383Swnj * queue then start the next operation on the device. 2702383Swnj * (Common code for DONE and RETRY). 2712383Swnj */ 2722383Swnj mhp->mh_active = 0; 2732383Swnj mi->mi_tab.b_active = 0; 2742383Swnj mhp->mh_actf = mi->mi_forw; 2752383Swnj if (mi->mi_tab.b_actf) 2762383Swnj mbustart(mi); 2772383Swnj break; 2782383Swnj 27925199Skarels case MBD_REPOSITION: /* driver started repositioning */ 2802383Swnj /* 28125199Skarels * Drive is repositioning, not doing data transfer. 28225199Skarels * Free controller, but don't have to restart drive. 28325199Skarels */ 28425199Skarels mhp->mh_active = 0; 28525199Skarels mhp->mh_actf = mi->mi_forw; 28625199Skarels break; 28725199Skarels 28825199Skarels case MBD_RESTARTED: /* driver restarted op (ecc, e.g.) */ 28925199Skarels /* 2902893Swnj * Note that mhp->mh_active is still on. 2912383Swnj */ 2922383Swnj break; 2932383Swnj 2942383Swnj default: 2952884Swnj panic("mbintr"); 2962383Swnj } 2972383Swnj } 2982383Swnj /* 2992383Swnj * Service drives which require attention 3002383Swnj * after non-data-transfer operations. 3012383Swnj */ 30226376Skarels while (drive = ffs((long)as)) { 3032955Swnj drive--; /* was 1 origin */ 3042955Swnj as &= ~(1 << drive); 3052981Swnj mi = mhp->mh_mbip[drive]; 3066537Ssam if (mi == NULL || mi->mi_alive == 0) { 3076537Ssam struct mba_device fnd; 3086537Ssam struct mba_drv *mbd = &mhp->mh_mba->mba_drv[drive]; 3096537Ssam int dt = mbd->mbd_dt & 0xffff; 3106537Ssam 3116537Ssam if (dt == 0 || dt == MBDT_MOH) 3126537Ssam continue; 3136537Ssam fnd.mi_mba = mhp->mh_mba; 3146537Ssam fnd.mi_mbanum = mbanum; 3156537Ssam fnd.mi_drive = drive; 3166537Ssam if ((mi = mbaconfig(&fnd, dt)) == NULL) 3176537Ssam continue; 31812507Ssam /* 31912507Ssam * If a tape, poke the slave attach routines. 32012507Ssam * Otherwise, could be a disk which we want 32112507Ssam * to swap on, so make a pass over the swap 32212507Ssam * configuration table in case the size of 32312507Ssam * the swap area must be determined by drive type. 32412507Ssam */ 32512507Ssam if (dt & MBDT_TAP) 32612507Ssam mbaddtape(mi, drive); 32712507Ssam else 32812507Ssam swapconf(); 3296537Ssam } 3302955Swnj /* 3312981Swnj * If driver has a handler for non-data transfer 3323095Swnj * interrupts, give it a chance to tell us what to do. 3332955Swnj */ 3342955Swnj if (mi->mi_driver->md_ndint) { 3352955Swnj switch ((*mi->mi_driver->md_ndint)(mi)) { 3362383Swnj 3373095Swnj case MBN_DONE: /* operation completed */ 3386537Ssam mi->mi_tab.b_active = 0; 3392955Swnj mi->mi_tab.b_errcnt = 0; 3402981Swnj bp = mi->mi_tab.b_actf; 3412955Swnj mi->mi_tab.b_actf = bp->av_forw; 34230535Skarels biodone(bp); 3433095Swnj /* fall into common code */ 3443095Swnj case MBN_RETRY: /* operation continues */ 3452955Swnj if (mi->mi_tab.b_actf) 3462955Swnj mbustart(mi); 3472955Swnj break; 3483095Swnj case MBN_SKIP: /* ignore unsol. interrupt */ 3492981Swnj break; 3502955Swnj default: 3512955Swnj panic("mbintr"); 3522955Swnj } 3532955Swnj } else 3543095Swnj /* 3553095Swnj * If there is no non-data transfer interrupt 3563095Swnj * routine, then we should just 3573095Swnj * restart the unit, leading to a mbstart() soon. 3583095Swnj */ 3592955Swnj mbustart(mi); 3602955Swnj } 3612383Swnj /* 3622383Swnj * If there is an operation available and 3632383Swnj * the massbus isn't active, get it going. 3642383Swnj */ 3652383Swnj if (mhp->mh_actf && !mhp->mh_active) 3662383Swnj mbstart(mhp); 3673095Swnj /* THHHHATS all folks... */ 3682383Swnj } 3692383Swnj 3702383Swnj /* 37112507Ssam * For autoconfig'ng tape drives on the fly. 37212507Ssam */ 37312507Ssam mbaddtape(mi, drive) 37412507Ssam struct mba_device *mi; 37512507Ssam int drive; 37612507Ssam { 37712507Ssam register struct mba_slave *ms; 37812507Ssam 37912507Ssam for (ms = mbsinit; ms->ms_driver; ms++) 38012507Ssam if (ms->ms_driver == mi->mi_driver && ms->ms_alive == 0 && 38112507Ssam (ms->ms_ctlr == mi->mi_unit || 38212507Ssam ms->ms_ctlr == '?')) { 38312507Ssam if ((*ms->ms_driver->md_slave)(mi, ms, drive)) { 38412507Ssam printf("%s%d at %s%d slave %d\n", 38512507Ssam ms->ms_driver->md_sname, 38612507Ssam ms->ms_unit, 38712507Ssam mi->mi_driver->md_dname, 38812507Ssam mi->mi_unit, 38912507Ssam ms->ms_slave); 39012507Ssam ms->ms_alive = 1; 39112507Ssam ms->ms_ctlr = mi->mi_unit; 39212507Ssam } 39312507Ssam } 39412507Ssam } 39512507Ssam 39612507Ssam /* 3972383Swnj * Setup the mapping registers for a transfer. 3982383Swnj */ 3992383Swnj mbasetup(mi) 4002981Swnj register struct mba_device *mi; 40128Sbill { 4022383Swnj register struct mba_regs *mbap = mi->mi_mba; 4032383Swnj struct buf *bp = mi->mi_tab.b_actf; 4046381Swnj register int npf; 40528Sbill unsigned v; 40628Sbill register struct pte *pte, *io; 40728Sbill int o; 40828Sbill struct proc *rp; 40928Sbill 4101412Sbill v = btop(bp->b_un.b_addr); 41126040Skarels o = (int)(bp->b_un.b_addr) & PGOFSET; 41217215Smckusick if (bp->b_bcount >= 0) 41317215Smckusick npf = btoc(bp->b_bcount + o); 41417215Smckusick else 41517215Smckusick npf = btoc(-(bp->b_bcount) + o); 4161412Sbill rp = bp->b_flags&B_DIRTY ? &proc[2] : bp->b_proc; 4176381Swnj if ((bp->b_flags & B_PHYS) == 0) 4181412Sbill pte = &Sysmap[btop(((int)bp->b_un.b_addr)&0x7fffffff)]; 4196381Swnj else if (bp->b_flags & B_UAREA) 4206381Swnj pte = &rp->p_addr[v]; 4216381Swnj else if (bp->b_flags & B_PAGET) 4226381Swnj pte = &Usrptmap[btokmx((struct pte *)bp->b_un.b_addr)]; 4236381Swnj else 4246381Swnj pte = vtopte(rp, v); 4256381Swnj io = mbap->mba_map; 4266381Swnj while (--npf >= 0) { 4276381Swnj if (pte->pg_pfnum == 0) 4286381Swnj panic("mba, zero entry"); 4296381Swnj *(int *)io++ = pte++->pg_pfnum | PG_V; 43028Sbill } 4311412Sbill *(int *)io++ = 0; 4326381Swnj return (o); 43328Sbill } 4342930Swnj 4356181Sroot #if notdef 4363095Swnj /* 4373095Swnj * Init and interrupt enable a massbus adapter. 4383095Swnj */ 4392930Swnj mbainit(mp) 4402930Swnj struct mba_regs *mp; 4412930Swnj { 4422930Swnj 4433095Swnj mp->mba_cr = MBCR_INIT; 4443095Swnj mp->mba_cr = MBCR_IE; 4452930Swnj } 4462704Swnj #endif 4474966Swnj #endif 448