1*3248Swnj /* uba.c 4.23 81/03/13 */ 240Sbill 340Sbill #include "../h/param.h" 42395Swnj #include "../h/systm.h" 52395Swnj #include "../h/cpu.h" 640Sbill #include "../h/map.h" 740Sbill #include "../h/pte.h" 82395Swnj #include "../h/buf.h" 92570Swnj #include "../h/vm.h" 102958Swnj #include "../h/ubareg.h" 112958Swnj #include "../h/ubavar.h" 1240Sbill #include "../h/dir.h" 1340Sbill #include "../h/user.h" 1440Sbill #include "../h/proc.h" 15284Sbill #include "../h/conf.h" 161901Swnj #include "../h/mtpr.h" 172395Swnj #include "../h/nexus.h" 182570Swnj #include "../h/dk.h" 1940Sbill 202929Swnj #if VAX780 212929Swnj char ubasr_bits[] = UBASR_BITS; 222929Swnj #endif 232929Swnj 2440Sbill /* 252570Swnj * Do transfer on device argument. The controller 262570Swnj * and uba involved are implied by the device. 272570Swnj * We queue for resource wait in the uba code if necessary. 282570Swnj * We return 1 if the transfer was started, 0 if it was not. 292570Swnj * If you call this routine with the head of the queue for a 302570Swnj * UBA, it will automatically remove the device from the UBA 312570Swnj * queue before it returns. If some other device is given 322570Swnj * as argument, it will be added to the request queue if the 332570Swnj * request cannot be started immediately. This means that 342570Swnj * passing a device which is on the queue but not at the head 352570Swnj * of the request queue is likely to be a disaster. 362570Swnj */ 372570Swnj ubago(ui) 382958Swnj register struct uba_device *ui; 392570Swnj { 402958Swnj register struct uba_ctlr *um = ui->ui_mi; 412570Swnj register struct uba_hd *uh; 422570Swnj register int s, unit; 432570Swnj 442570Swnj uh = &uba_hd[um->um_ubanum]; 452570Swnj s = spl6(); 462628Swnj if (um->um_driver->ud_xclu && uh->uh_users > 0 || uh->uh_xclu) 472616Swnj goto rwait; 482570Swnj um->um_ubinfo = ubasetup(um->um_ubanum, um->um_tab.b_actf->b_actf, 492570Swnj UBA_NEEDBDP|UBA_CANTWAIT); 502616Swnj if (um->um_ubinfo == 0) 512616Swnj goto rwait; 522616Swnj uh->uh_users++; 532628Swnj if (um->um_driver->ud_xclu) 542616Swnj uh->uh_xclu = 1; 552570Swnj splx(s); 562570Swnj if (ui->ui_dk >= 0) { 572570Swnj unit = ui->ui_dk; 582570Swnj dk_busy |= 1<<unit; 592570Swnj } 602570Swnj if (uh->uh_actf == ui) 612570Swnj uh->uh_actf = ui->ui_forw; 622570Swnj (*um->um_driver->ud_dgo)(um); 632570Swnj if (ui->ui_dk >= 0) { 642570Swnj dk_xfer[unit]++; 652570Swnj dk_wds[unit] += um->um_tab.b_actf->b_bcount>>6; 662570Swnj } 672570Swnj return (1); 682616Swnj rwait: 692616Swnj if (uh->uh_actf != ui) { 702616Swnj ui->ui_forw = NULL; 712616Swnj if (uh->uh_actf == NULL) 722616Swnj uh->uh_actf = ui; 732616Swnj else 742616Swnj uh->uh_actl->ui_forw = ui; 752616Swnj uh->uh_actl = ui; 762616Swnj } 772616Swnj splx(s); 782616Swnj return (0); 792570Swnj } 802570Swnj 812616Swnj ubadone(um) 822958Swnj register struct uba_ctlr *um; 832616Swnj { 842616Swnj register struct uba_hd *uh = &uba_hd[um->um_ubanum]; 852616Swnj 862628Swnj if (um->um_driver->ud_xclu) 872616Swnj uh->uh_xclu = 0; 882616Swnj uh->uh_users--; 892616Swnj ubarelse(um->um_ubanum, &um->um_ubinfo); 902616Swnj } 912616Swnj 922570Swnj /* 932395Swnj * Allocate and setup UBA map registers, and bdp's 942395Swnj * Flags says whether bdp is needed, whether the caller can't 952395Swnj * wait (e.g. if the caller is at interrupt level). 9640Sbill * 972570Swnj * Return value: 9840Sbill * Bits 0-8 Byte offset 9940Sbill * Bits 9-17 Start map reg. no. 10040Sbill * Bits 18-27 No. mapping reg's 10140Sbill * Bits 28-31 BDP no. 10240Sbill */ 1032395Swnj ubasetup(uban, bp, flags) 1042395Swnj struct buf *bp; 10540Sbill { 1062395Swnj register struct uba_hd *uh = &uba_hd[uban]; 10740Sbill register int temp, i; 10840Sbill int npf, reg, bdp; 10940Sbill unsigned v; 11040Sbill register struct pte *pte, *io; 11140Sbill struct proc *rp; 11240Sbill int a, o, ubinfo; 11340Sbill 11440Sbill v = btop(bp->b_un.b_addr); 11540Sbill o = (int)bp->b_un.b_addr & PGOFSET; 11640Sbill npf = btoc(bp->b_bcount + o) + 1; 11740Sbill a = spl6(); 1182784Swnj while ((reg = rmalloc(uh->uh_map, npf)) == 0) { 1192395Swnj if (flags & UBA_CANTWAIT) 1202395Swnj return (0); 1212395Swnj uh->uh_mrwant++; 1222395Swnj sleep((caddr_t)uh->uh_map, PSWP); 12340Sbill } 12440Sbill bdp = 0; 1252395Swnj if (flags & UBA_NEEDBDP) { 1262395Swnj while ((bdp = ffs(uh->uh_bdpfree)) == 0) { 1272395Swnj if (flags & UBA_CANTWAIT) { 1282784Swnj rmfree(uh->uh_map, npf, reg); 1292395Swnj return (0); 1302395Swnj } 1312395Swnj uh->uh_bdpwant++; 1322395Swnj sleep((caddr_t)uh->uh_map, PSWP); 13340Sbill } 1342463Swnj uh->uh_bdpfree &= ~(1 << (bdp-1)); 1352395Swnj } 13640Sbill splx(a); 1372463Swnj reg--; 13840Sbill ubinfo = (bdp << 28) | (npf << 18) | (reg << 9) | o; 1392395Swnj io = &uh->uh_uba->uba_map[reg]; 1402958Swnj temp = (bdp << 21) | UBAMR_MRV; 14140Sbill rp = bp->b_flags&B_DIRTY ? &proc[2] : bp->b_proc; 14240Sbill if (bdp && (o & 01)) 1432958Swnj temp |= UBAMR_BO; 14440Sbill if (bp->b_flags & B_UAREA) { 14540Sbill for (i = UPAGES - bp->b_bcount / NBPG; i < UPAGES; i++) { 14640Sbill if (rp->p_addr[i].pg_pfnum == 0) 14740Sbill panic("uba: zero upage"); 14840Sbill *(int *)io++ = rp->p_addr[i].pg_pfnum | temp; 14940Sbill } 15040Sbill } else if ((bp->b_flags & B_PHYS) == 0) { 151728Sbill pte = &Sysmap[btop(((int)bp->b_un.b_addr)&0x7fffffff)]; 15240Sbill while (--npf != 0) 153728Sbill *(int *)io++ = pte++->pg_pfnum | temp; 15440Sbill } else { 15540Sbill if (bp->b_flags & B_PAGET) 15640Sbill pte = &Usrptmap[btokmx((struct pte *)bp->b_un.b_addr)]; 15740Sbill else 15840Sbill pte = vtopte(rp, v); 15940Sbill while (--npf != 0) { 16040Sbill if (pte->pg_pfnum == 0) 16140Sbill panic("uba zero uentry"); 16240Sbill *(int *)io++ = pte++->pg_pfnum | temp; 16340Sbill } 16440Sbill } 16540Sbill *(int *)io++ = 0; 16640Sbill return (ubinfo); 16740Sbill } 16840Sbill 16940Sbill /* 1702570Swnj * Non buffer setup interface... set up a buffer and call ubasetup. 17140Sbill */ 1722395Swnj uballoc(uban, addr, bcnt, flags) 1733107Swnj int uban; 17440Sbill caddr_t addr; 1753107Swnj int bcnt, flags; 17640Sbill { 177883Sbill struct buf ubabuf; 17840Sbill 17940Sbill ubabuf.b_un.b_addr = addr; 18040Sbill ubabuf.b_flags = B_BUSY; 18140Sbill ubabuf.b_bcount = bcnt; 182883Sbill /* that's all the fields ubasetup() needs */ 1832395Swnj return (ubasetup(uban, &ubabuf, flags)); 18440Sbill } 18540Sbill 1862053Swnj /* 1872570Swnj * Release resources on uba uban, and then unblock resource waiters. 1882570Swnj * The map register parameter is by value since we need to block 1892570Swnj * against uba resets on 11/780's. 1902053Swnj */ 1912395Swnj ubarelse(uban, amr) 1922053Swnj int *amr; 19340Sbill { 1942395Swnj register struct uba_hd *uh = &uba_hd[uban]; 1952570Swnj register int bdp, reg, npf, s; 1962053Swnj int mr; 19740Sbill 1982570Swnj /* 1992570Swnj * Carefully see if we should release the space, since 2002570Swnj * it may be released asynchronously at uba reset time. 2012570Swnj */ 2022570Swnj s = spl6(); 2032053Swnj mr = *amr; 2042053Swnj if (mr == 0) { 2052570Swnj /* 2062570Swnj * A ubareset() occurred before we got around 2072570Swnj * to releasing the space... no need to bother. 2082570Swnj */ 2092570Swnj splx(s); 2102053Swnj return; 2112053Swnj } 2122067Swnj *amr = 0; 2132570Swnj splx(s); /* let interrupts in, we're safe for a while */ 21440Sbill bdp = (mr >> 28) & 0x0f; 21540Sbill if (bdp) { 2162729Swnj switch (cpu) { 2172423Skre #if VAX780 2182423Skre case VAX_780: 2192958Swnj uh->uh_uba->uba_dpr[bdp] |= UBADPR_BNE; 2202423Skre break; 2212423Skre #endif 2222423Skre #if VAX750 2232423Skre case VAX_750: 2242958Swnj uh->uh_uba->uba_dpr[bdp] |= 2252958Swnj UBADPR_PURGE|UBADPR_NXM|UBADPR_UCE; 2262423Skre break; 2272423Skre #endif 2282423Skre } 2292570Swnj uh->uh_bdpfree |= 1 << (bdp-1); /* atomic */ 2302395Swnj if (uh->uh_bdpwant) { 2312395Swnj uh->uh_bdpwant = 0; 2322395Swnj wakeup((caddr_t)uh->uh_map); 23340Sbill } 23440Sbill } 2352570Swnj /* 2362570Swnj * Put back the registers in the resource map. 2372570Swnj * The map code must not be reentered, so we do this 2382570Swnj * at high ipl. 2392570Swnj */ 24040Sbill npf = (mr >> 18) & 0x3ff; 24140Sbill reg = ((mr >> 9) & 0x1ff) + 1; 2422570Swnj s = spl6(); 2432784Swnj rmfree(uh->uh_map, npf, reg); 2442570Swnj splx(s); 2452570Swnj 2462570Swnj /* 2472570Swnj * Wakeup sleepers for map registers, 2482570Swnj * and also, if there are processes blocked in dgo(), 2492570Swnj * give them a chance at the UNIBUS. 2502570Swnj */ 2512395Swnj if (uh->uh_mrwant) { 2522395Swnj uh->uh_mrwant = 0; 2532395Swnj wakeup((caddr_t)uh->uh_map); 25440Sbill } 2552570Swnj while (uh->uh_actf && ubago(uh->uh_actf)) 2562570Swnj ; 25740Sbill } 25840Sbill 2592729Swnj ubapurge(um) 2602958Swnj register struct uba_ctlr *um; 2612729Swnj { 2622729Swnj register struct uba_hd *uh = um->um_hd; 2632729Swnj register int bdp = (um->um_ubinfo >> 28) & 0x0f; 2642729Swnj 2652729Swnj switch (cpu) { 2662729Swnj #if VAX780 2672729Swnj case VAX_780: 2682958Swnj uh->uh_uba->uba_dpr[bdp] |= UBADPR_BNE; 2692729Swnj break; 2702729Swnj #endif 2712729Swnj #if VAX750 2722729Swnj case VAX_750: 2732958Swnj uh->uh_uba->uba_dpr[bdp] |= UBADPR_PURGE|UBADPR_NXM|UBADPR_UCE; 2742729Swnj break; 2752729Swnj #endif 2762729Swnj } 2772729Swnj } 2782729Swnj 2792570Swnj /* 2802570Swnj * Generate a reset on uba number uban. Then 2812570Swnj * call each device in the character device table, 2822570Swnj * giving it a chance to clean up so as to be able to continue. 2832570Swnj */ 2842395Swnj ubareset(uban) 2852570Swnj int uban; 286284Sbill { 287284Sbill register struct cdevsw *cdp; 2882646Swnj register struct uba_hd *uh = &uba_hd[uban]; 2891781Sbill int s; 290284Sbill 291302Sbill s = spl6(); 2922646Swnj uh->uh_users = 0; 2932646Swnj uh->uh_zvcnt = 0; 2942646Swnj uh->uh_xclu = 0; 2952646Swnj uh->uh_hangcnt = 0; 2962646Swnj uh->uh_actf = uh->uh_actl = 0; 2972646Swnj uh->uh_bdpwant = 0; 2982646Swnj uh->uh_mrwant = 0; 2992646Swnj wakeup((caddr_t)&uh->uh_bdpwant); 3002646Swnj wakeup((caddr_t)&uh->uh_mrwant); 3012958Swnj printf("uba%d: reset", uban); 3022958Swnj ubainit(uh->uh_uba); 303284Sbill for (cdp = cdevsw; cdp->d_open; cdp++) 3042395Swnj (*cdp->d_reset)(uban); 305284Sbill printf("\n"); 306302Sbill splx(s); 307284Sbill } 3082395Swnj 3092570Swnj /* 3102570Swnj * Init a uba. This is called with a pointer 3112570Swnj * rather than a virtual address since it is called 3122570Swnj * by code which runs with memory mapping disabled. 3132570Swnj * In these cases we really don't need the interrupts 3142570Swnj * enabled, but since we run with ipl high, we don't care 3152570Swnj * if they are, they will never happen anyways. 3162570Swnj */ 3172423Skre ubainit(uba) 3182423Skre register struct uba_regs *uba; 3192395Swnj { 3202395Swnj 3212958Swnj switch (cpu) { 3222958Swnj #if VAX780 323*3248Swnj case VAX_780: 3242958Swnj uba->uba_cr = UBACR_ADINIT; 3252958Swnj uba->uba_cr = UBACR_IFS|UBACR_BRIE|UBACR_USEFIE|UBACR_SUEFIE; 3262958Swnj while ((uba->uba_cnfgr & UBACNFGR_UBIC) == 0) 3272958Swnj ; 3282958Swnj break; 3292958Swnj #endif 3302958Swnj #if VAX750 331*3248Swnj case VAX_750: 3322958Swnj mtpr(IUR, 1); 3332958Swnj /* give devices time to recover from power fail */ 3342958Swnj DELAY(5000000); 3352958Swnj break; 3362958Swnj #endif 3372958Swnj } 3382395Swnj } 3392395Swnj 3402958Swnj #if VAX780 3412570Swnj /* 3422570Swnj * Check to make sure the UNIBUS adaptor is not hung, 3432570Swnj * with an interrupt in the register to be presented, 3442570Swnj * but not presenting it for an extended period (5 seconds). 3452570Swnj */ 3462395Swnj unhang() 3472395Swnj { 3482395Swnj register int uban; 3492395Swnj 3502395Swnj for (uban = 0; uban < numuba; uban++) { 3512395Swnj register struct uba_hd *uh = &uba_hd[uban]; 3522395Swnj register struct uba_regs *up = uh->uh_uba; 3532395Swnj 3542395Swnj if (up->uba_sr == 0) 3552395Swnj return; 3562395Swnj uh->uh_hangcnt++; 3572759Swnj if (uh->uh_hangcnt > 5*hz) { 3582395Swnj uh->uh_hangcnt = 0; 3592929Swnj printf("uba%d: hung\n", uban); 3602395Swnj ubareset(uban); 3612395Swnj } 3622395Swnj } 3632395Swnj } 3642395Swnj 3652570Swnj /* 3662570Swnj * This is a timeout routine which decrements the ``i forgot to 3672570Swnj * interrupt'' counts, on an 11/780. This prevents slowly growing 3682570Swnj * counts from causing a UBA reset since we are interested only 3692570Swnj * in hang situations. 3702570Swnj */ 3712395Swnj ubawatch() 3722395Swnj { 3732395Swnj register struct uba_hd *uh; 3742395Swnj register int uban; 3752395Swnj 3762784Swnj if (panicstr) 3772784Swnj return; 3782395Swnj for (uban = 0; uban < numuba; uban++) { 3792395Swnj uh = &uba_hd[uban]; 3802395Swnj if (uh->uh_hangcnt) 3812395Swnj uh->uh_hangcnt--; 3822395Swnj } 3832395Swnj } 3842395Swnj 3852570Swnj /* 3862570Swnj * This routine is called by the locore code to 3872570Swnj * process a UBA error on an 11/780. The arguments are passed 3882570Swnj * on the stack, and value-result (through some trickery). 3892570Swnj * In particular, the uvec argument is used for further 3902570Swnj * uba processing so the result aspect of it is very important. 3912570Swnj * It must not be declared register. 3922570Swnj */ 3932423Skre /*ARGSUSED*/ 3942395Swnj ubaerror(uban, uh, xx, uvec, uba) 3952395Swnj register int uban; 3962395Swnj register struct uba_hd *uh; 3972395Swnj int uvec; 3982395Swnj register struct uba_regs *uba; 3992395Swnj { 4002395Swnj register sr, s; 4012395Swnj 4022395Swnj if (uvec == 0) { 4032395Swnj uh->uh_zvcnt++; 4042395Swnj if (uh->uh_zvcnt > 250000) { 4052929Swnj printf("uba%d: too many zero vectors\n"); 4062395Swnj ubareset(uban); 4072395Swnj } 4082395Swnj uvec = 0; 4092395Swnj return; 4102395Swnj } 4112395Swnj if (uba->uba_cnfgr & NEX_CFGFLT) { 4122929Swnj printf("uba%d: sbi fault sr=%b cnfgr=%b\n", 4132929Swnj uban, uba->uba_sr, ubasr_bits, 414*3248Swnj uba->uba_cnfgr, NEXFLT_BITS); 4152395Swnj ubareset(uban); 4162395Swnj uvec = 0; 4172395Swnj return; 4182395Swnj } 4192395Swnj sr = uba->uba_sr; 4202395Swnj s = spl7(); 4212929Swnj printf("uba%d: uba error sr=%x fmer=%x fubar=%o\n", 4222470Swnj uban, uba->uba_sr, uba->uba_fmer, 4*uba->uba_fubar); 4232395Swnj splx(s); 4242395Swnj uba->uba_sr = sr; 4252958Swnj uvec &= UBABRRVR_DIV; 4262395Swnj return; 4272395Swnj } 4282395Swnj #endif 429