1*11119Ssam /* up.c 4.70 83/02/17 */ 2264Sbill 31937Swnj #include "up.h" 42646Swnj #if NSC > 0 5264Sbill /* 69548Ssam * UNIBUS disk driver with: 79548Ssam * overlapped seeks, 89548Ssam * ECC recovery, and 99548Ssam * bad sector forwarding. 102889Swnj * 112889Swnj * TODO: 123445Sroot * Check that offset recovery code works 13264Sbill */ 149782Ssam #include "../machine/pte.h" 15264Sbill 16264Sbill #include "../h/param.h" 17264Sbill #include "../h/systm.h" 18308Sbill #include "../h/dk.h" 199548Ssam #include "../h/dkbad.h" 20264Sbill #include "../h/buf.h" 21264Sbill #include "../h/conf.h" 22264Sbill #include "../h/dir.h" 23264Sbill #include "../h/user.h" 24264Sbill #include "../h/map.h" 252571Swnj #include "../h/vm.h" 262379Swnj #include "../h/cmap.h" 279357Ssam #include "../h/uio.h" 2810871Ssam #include "../h/kernel.h" 29264Sbill 309357Ssam #include "../vax/cpu.h" 319357Ssam #include "../vax/nexus.h" 329357Ssam #include "../vaxuba/ubavar.h" 339357Ssam #include "../vaxuba/ubareg.h" 349357Ssam #include "../vaxuba/upreg.h" 359357Ssam 362395Swnj struct up_softc { 372395Swnj int sc_softas; 382607Swnj int sc_ndrive; 392395Swnj int sc_wticks; 402674Swnj int sc_recal; 412646Swnj } up_softc[NSC]; 42275Sbill 432395Swnj /* THIS SHOULD BE READ OFF THE PACK, PER DRIVE */ 4410858Ssam struct size { 45264Sbill daddr_t nblocks; 46264Sbill int cyloff; 47264Sbill } up_sizes[8] = { 48264Sbill 15884, 0, /* A=cyl 0 thru 26 */ 49264Sbill 33440, 27, /* B=cyl 27 thru 81 */ 50341Sbill 495520, 0, /* C=cyl 0 thru 814 */ 51264Sbill 15884, 562, /* D=cyl 562 thru 588 */ 52264Sbill 55936, 589, /* E=cyl 589 thru 680 */ 533730Sroot 81376, 681, /* F=cyl 681 thru 814 */ 543730Sroot 153728, 562, /* G=cyl 562 thru 814 */ 55264Sbill 291346, 82, /* H=cyl 82 thru 561 */ 562395Swnj }, fj_sizes[8] = { 572395Swnj 15884, 0, /* A=cyl 0 thru 49 */ 582395Swnj 33440, 50, /* B=cyl 50 thru 154 */ 592395Swnj 263360, 0, /* C=cyl 0 thru 822 */ 602395Swnj 0, 0, 612395Swnj 0, 0, 622395Swnj 0, 0, 632395Swnj 0, 0, 643730Sroot 213664, 155, /* H=cyl 155 thru 822 */ 656851Ssam }, upam_sizes[8] = { 666305Sroot 15884, 0, /* A=cyl 0 thru 31 */ 676305Sroot 33440, 32, /* B=cyl 32 thru 97 */ 686305Sroot 524288, 0, /* C=cyl 0 thru 1023 */ 696602Ssam 27786, 668, 706602Ssam 27786, 723, 716602Ssam 125440, 778, 726305Sroot 181760, 668, /* G=cyl 668 thru 1022 */ 736305Sroot 291346, 98, /* H=cyl 98 thru 667 */ 74264Sbill }; 752395Swnj /* END OF STUFF WHICH SHOULD BE READ IN PER DISK */ 76264Sbill 776346Swnj /* 786346Swnj * On a 780 upSDIST could be 2, but 796346Swnj * in the interest of 750's... 806346Swnj */ 816346Swnj #define _upSDIST 3 /* 1.5 msec */ 822395Swnj #define _upRDIST 4 /* 2.0 msec */ 83264Sbill 842395Swnj int upSDIST = _upSDIST; 852395Swnj int upRDIST = _upRDIST; 862395Swnj 872607Swnj int upprobe(), upslave(), upattach(), updgo(), upintr(); 882983Swnj struct uba_ctlr *upminfo[NSC]; 892983Swnj struct uba_device *updinfo[NUP]; 906383Swnj #define UPIPUNITS 8 916383Swnj struct uba_device *upip[NSC][UPIPUNITS]; /* fuji w/fixed head gives n,n+4 */ 922395Swnj 932607Swnj u_short upstd[] = { 0776700, 0774400, 0776300, 0 }; 942616Swnj struct uba_driver scdriver = 952607Swnj { upprobe, upslave, upattach, updgo, upstd, "up", updinfo, "sc", upminfo }; 962395Swnj struct buf uputab[NUP]; 979548Ssam char upinit[NUP]; 982395Swnj 992395Swnj struct upst { 1002395Swnj short nsect; 1012395Swnj short ntrak; 1022395Swnj short nspc; 1032395Swnj short ncyl; 1042395Swnj struct size *sizes; 1052395Swnj } upst[] = { 10611112Shelge 32, 19, 32*19, 815, up_sizes, /* 9300 */ 107*11119Ssam 32, 19, 32*19, 823, up_sizes, /* 9766 */ 1082395Swnj 32, 10, 32*10, 823, fj_sizes, /* fujitsu 160m */ 1096851Ssam 32, 16, 32*16, 1024, upam_sizes, /* ampex capricorn */ 110*11119Ssam 0, 0, 0, 0, 0 1112395Swnj }; 1122395Swnj 1132629Swnj u_char up_offset[16] = { 1149548Ssam UPOF_P400, UPOF_M400, UPOF_P400, UPOF_M400, 1159548Ssam UPOF_P800, UPOF_M800, UPOF_P800, UPOF_M800, 1169548Ssam UPOF_P1200, UPOF_M1200, UPOF_P1200, UPOF_M1200, 1179548Ssam 0, 0, 0, 0 1182629Swnj }; 119264Sbill 1202616Swnj struct buf rupbuf[NUP]; 1219548Ssam struct buf bupbuf[NUP]; 1229548Ssam struct dkbad upbad[NUP]; 123264Sbill 124264Sbill #define b_cylin b_resid 125264Sbill 126264Sbill #ifdef INTRLVE 127264Sbill daddr_t dkblock(); 128264Sbill #endif 1292395Swnj 1302395Swnj int upwstart, upwatch(); /* Have started guardian */ 1312470Swnj int upseek; 1322681Swnj int upwaitdry; 1332395Swnj 1342395Swnj /*ARGSUSED*/ 1352607Swnj upprobe(reg) 1362395Swnj caddr_t reg; 1372395Swnj { 1382459Swnj register int br, cvec; 1392459Swnj 1402607Swnj #ifdef lint 1412607Swnj br = 0; cvec = br; br = cvec; 1422607Swnj #endif 1432629Swnj ((struct updevice *)reg)->upcs1 = UP_IE|UP_RDY; 1442607Swnj DELAY(10); 1452629Swnj ((struct updevice *)reg)->upcs1 = 0; 1469357Ssam return (sizeof (struct updevice)); 1472395Swnj } 1482395Swnj 1492607Swnj upslave(ui, reg) 1502983Swnj struct uba_device *ui; 1512395Swnj caddr_t reg; 1522395Swnj { 1532629Swnj register struct updevice *upaddr = (struct updevice *)reg; 1542395Swnj 1552395Swnj upaddr->upcs1 = 0; /* conservative */ 1562607Swnj upaddr->upcs2 = ui->ui_slave; 1576843Swnj upaddr->upcs1 = UP_NOP|UP_GO; 1583445Sroot if (upaddr->upcs2&UPCS2_NED) { 1592629Swnj upaddr->upcs1 = UP_DCLR|UP_GO; 1602395Swnj return (0); 1612395Swnj } 1622607Swnj return (1); 1632607Swnj } 1642607Swnj 1652607Swnj upattach(ui) 1662983Swnj register struct uba_device *ui; 1672607Swnj { 1682629Swnj register struct updevice *upaddr; 1692607Swnj 1702395Swnj if (upwstart == 0) { 1712759Swnj timeout(upwatch, (caddr_t)0, hz); 1722395Swnj upwstart++; 1732395Swnj } 1742571Swnj if (ui->ui_dk >= 0) 1752571Swnj dk_mspw[ui->ui_dk] = .0000020345; 1762607Swnj upip[ui->ui_ctlr][ui->ui_slave] = ui; 1772607Swnj up_softc[ui->ui_ctlr].sc_ndrive++; 1782629Swnj upaddr = (struct updevice *)ui->ui_addr; 179*11119Ssam ui->ui_type = upmaptype(ui); 180*11119Ssam } 181*11119Ssam 182*11119Ssam upmaptype(ui) 183*11119Ssam register struct uba_device *ui; 184*11119Ssam { 185*11119Ssam register struct updevice *upaddr = (struct updevice *)ui->ui_addr; 186*11119Ssam int type = ui->ui_type; 187*11119Ssam register struct upst *st; 188*11119Ssam 1892629Swnj upaddr->upcs1 = 0; 1902629Swnj upaddr->upcs2 = ui->ui_slave; 1913496Sroot upaddr->uphr = UPHR_MAXTRAK; 192*11119Ssam for (st = upst; st->nsect != 0; st++) 193*11119Ssam if (upaddr->uphr == st->ntrak - 1) { 194*11119Ssam type = st - upst; 195*11119Ssam break; 196*11119Ssam } 197*11119Ssam if (st->nsect == 0) 198*11119Ssam printf("up%d: uphr=%x\n", ui->ui_slave, upaddr->uphr); 199*11119Ssam if (type == 0) { 20011112Shelge upaddr->uphr = UPHR_MAXCYL; 20111112Shelge if (upaddr->uphr == 822) 202*11119Ssam type++; 20311112Shelge } 2043496Sroot upaddr->upcs2 = UPCS2_CLR; 205*11119Ssam return (type); 2062395Swnj } 207264Sbill 2089548Ssam upopen(dev) 2099548Ssam dev_t dev; 2109548Ssam { 2119548Ssam register int unit = minor(dev) >> 3; 2129548Ssam register struct uba_device *ui; 2139548Ssam 2149548Ssam if (unit >= NUP || (ui = updinfo[unit]) == 0 || ui->ui_alive == 0) 2159548Ssam return (ENXIO); 2169548Ssam return (0); 2179548Ssam } 2189548Ssam 219264Sbill upstrategy(bp) 2202395Swnj register struct buf *bp; 221264Sbill { 2222983Swnj register struct uba_device *ui; 2232395Swnj register struct upst *st; 2242395Swnj register int unit; 2252470Swnj register struct buf *dp; 2262395Swnj int xunit = minor(bp->b_dev) & 07; 2272470Swnj long bn, sz; 228264Sbill 2292470Swnj sz = (bp->b_bcount+511) >> 9; 230264Sbill unit = dkunit(bp); 2312395Swnj if (unit >= NUP) 2322395Swnj goto bad; 2332395Swnj ui = updinfo[unit]; 2342395Swnj if (ui == 0 || ui->ui_alive == 0) 2352395Swnj goto bad; 2362395Swnj st = &upst[ui->ui_type]; 2372395Swnj if (bp->b_blkno < 0 || 2382395Swnj (bn = dkblock(bp))+sz > st->sizes[xunit].nblocks) 2392395Swnj goto bad; 2402395Swnj bp->b_cylin = bn/st->nspc + st->sizes[xunit].cyloff; 2416305Sroot (void) spl5(); 2422470Swnj dp = &uputab[ui->ui_unit]; 2432470Swnj disksort(dp, bp); 2442470Swnj if (dp->b_active == 0) { 2452395Swnj (void) upustart(ui); 2462395Swnj bp = &ui->ui_mi->um_tab; 2472395Swnj if (bp->b_actf && bp->b_active == 0) 2482395Swnj (void) upstart(ui->ui_mi); 249264Sbill } 2506305Sroot (void) spl0(); 2512395Swnj return; 2522395Swnj 2532395Swnj bad: 2542395Swnj bp->b_flags |= B_ERROR; 2552395Swnj iodone(bp); 2562395Swnj return; 257264Sbill } 258264Sbill 2592674Swnj /* 2602674Swnj * Unit start routine. 2612674Swnj * Seek the drive to be where the data is 2622674Swnj * and then generate another interrupt 2632674Swnj * to actually start the transfer. 2642674Swnj * If there is only one drive on the controller, 2652674Swnj * or we are very close to the data, don't 2662674Swnj * bother with the search. If called after 2672674Swnj * searching once, don't bother to look where 2682674Swnj * we are, just queue for transfer (to avoid 2692674Swnj * positioning forever without transferrring.) 2702674Swnj */ 2712395Swnj upustart(ui) 2722983Swnj register struct uba_device *ui; 273264Sbill { 274264Sbill register struct buf *bp, *dp; 2752983Swnj register struct uba_ctlr *um; 2762629Swnj register struct updevice *upaddr; 2772395Swnj register struct upst *st; 278264Sbill daddr_t bn; 2792674Swnj int sn, csn; 2802607Swnj /* 2812607Swnj * The SC21 cancels commands if you just say 2822629Swnj * cs1 = UP_IE 2832607Swnj * so we are cautious about handling of cs1. 2842607Swnj * Also don't bother to clear as bits other than in upintr(). 2852607Swnj */ 2862674Swnj int didie = 0; 2872674Swnj 2882674Swnj if (ui == 0) 2892674Swnj return (0); 2902983Swnj um = ui->ui_mi; 2912395Swnj dk_busy &= ~(1<<ui->ui_dk); 2922395Swnj dp = &uputab[ui->ui_unit]; 293266Sbill if ((bp = dp->b_actf) == NULL) 294268Sbill goto out; 2952674Swnj /* 2962674Swnj * If the controller is active, just remember 2972674Swnj * that this device would like to be positioned... 2982674Swnj * if we tried to position now we would confuse the SC21. 2992674Swnj */ 3002395Swnj if (um->um_tab.b_active) { 3012459Swnj up_softc[um->um_ctlr].sc_softas |= 1<<ui->ui_slave; 302275Sbill return (0); 303275Sbill } 3042674Swnj /* 3052674Swnj * If we have already positioned this drive, 3062674Swnj * then just put it on the ready queue. 3072674Swnj */ 308276Sbill if (dp->b_active) 309276Sbill goto done; 310276Sbill dp->b_active = 1; 3112629Swnj upaddr = (struct updevice *)um->um_addr; 3122395Swnj upaddr->upcs2 = ui->ui_slave; 3132674Swnj /* 3142674Swnj * If drive has just come up, 3152674Swnj * setup the pack. 3162674Swnj */ 3179548Ssam if ((upaddr->upds & UPDS_VV) == 0 || upinit[ui->ui_unit] == 0) { 3189548Ssam struct buf *bbp = &bupbuf[ui->ui_unit]; 31910858Ssam 3202607Swnj /* SHOULD WARN SYSTEM THAT THIS HAPPENED */ 3219548Ssam upinit[ui->ui_unit] = 1; 3222629Swnj upaddr->upcs1 = UP_IE|UP_DCLR|UP_GO; 3232629Swnj upaddr->upcs1 = UP_IE|UP_PRESET|UP_GO; 3243445Sroot upaddr->upof = UPOF_FMT22; 325268Sbill didie = 1; 3269548Ssam st = &upst[ui->ui_type]; 3279548Ssam bbp->b_flags = B_READ|B_BUSY; 3289548Ssam bbp->b_dev = bp->b_dev; 3299548Ssam bbp->b_bcount = 512; 3309548Ssam bbp->b_un.b_addr = (caddr_t)&upbad[ui->ui_unit]; 3319548Ssam bbp->b_blkno = st->ncyl * st->nspc - st->nsect; 3329548Ssam bbp->b_cylin = st->ncyl - 1; 3339548Ssam dp->b_actf = bbp; 3349548Ssam bbp->av_forw = bp; 3359548Ssam bp = bbp; 336264Sbill } 3372674Swnj /* 3382674Swnj * If drive is offline, forget about positioning. 3392674Swnj */ 3403445Sroot if ((upaddr->upds & (UPDS_DPR|UPDS_MOL)) != (UPDS_DPR|UPDS_MOL)) 341275Sbill goto done; 3422674Swnj /* 3432674Swnj * If there is only one drive, 3442674Swnj * dont bother searching. 3452674Swnj */ 3462607Swnj if (up_softc[um->um_ctlr].sc_ndrive == 1) 3472607Swnj goto done; 3482674Swnj /* 3492674Swnj * Figure out where this transfer is going to 3502674Swnj * and see if we are close enough to justify not searching. 3512674Swnj */ 3522395Swnj st = &upst[ui->ui_type]; 353264Sbill bn = dkblock(bp); 3542395Swnj sn = bn%st->nspc; 3552395Swnj sn = (sn + st->nsect - upSDIST) % st->nsect; 3562674Swnj if (bp->b_cylin - upaddr->updc) 357266Sbill goto search; /* Not on-cylinder */ 358275Sbill else if (upseek) 359275Sbill goto done; /* Ok just to be on-cylinder */ 360264Sbill csn = (upaddr->upla>>6) - sn - 1; 361266Sbill if (csn < 0) 3622395Swnj csn += st->nsect; 3632395Swnj if (csn > st->nsect - upRDIST) 364264Sbill goto done; 365264Sbill search: 3662674Swnj upaddr->updc = bp->b_cylin; 3672674Swnj /* 3682674Swnj * Not on cylinder at correct position, 3692674Swnj * seek/search. 3702674Swnj */ 371275Sbill if (upseek) 3722629Swnj upaddr->upcs1 = UP_IE|UP_SEEK|UP_GO; 3732470Swnj else { 374275Sbill upaddr->upda = sn; 3752629Swnj upaddr->upcs1 = UP_IE|UP_SEARCH|UP_GO; 376275Sbill } 377268Sbill didie = 1; 3782674Swnj /* 3792674Swnj * Mark unit busy for iostat. 3802674Swnj */ 3812395Swnj if (ui->ui_dk >= 0) { 3822395Swnj dk_busy |= 1<<ui->ui_dk; 3832395Swnj dk_seek[ui->ui_dk]++; 384264Sbill } 385268Sbill goto out; 386264Sbill done: 3872674Swnj /* 3882674Swnj * Device is ready to go. 3892674Swnj * Put it on the ready queue for the controller 3902674Swnj * (unless its already there.) 3912674Swnj */ 3922629Swnj if (dp->b_active != 2) { 3932629Swnj dp->b_forw = NULL; 3942629Swnj if (um->um_tab.b_actf == NULL) 3952629Swnj um->um_tab.b_actf = dp; 3962629Swnj else 3972629Swnj um->um_tab.b_actl->b_forw = dp; 3982629Swnj um->um_tab.b_actl = dp; 3992629Swnj dp->b_active = 2; 4002629Swnj } 401268Sbill out: 402268Sbill return (didie); 403264Sbill } 404264Sbill 4052674Swnj /* 4062674Swnj * Start up a transfer on a drive. 4072674Swnj */ 4082395Swnj upstart(um) 4092983Swnj register struct uba_ctlr *um; 410264Sbill { 411264Sbill register struct buf *bp, *dp; 4122983Swnj register struct uba_device *ui; 4132629Swnj register struct updevice *upaddr; 4142470Swnj struct upst *st; 415264Sbill daddr_t bn; 4162681Swnj int dn, sn, tn, cmd, waitdry; 417264Sbill 418264Sbill loop: 4192674Swnj /* 4202674Swnj * Pull a request off the controller queue 4212674Swnj */ 4222395Swnj if ((dp = um->um_tab.b_actf) == NULL) 423268Sbill return (0); 424264Sbill if ((bp = dp->b_actf) == NULL) { 4252395Swnj um->um_tab.b_actf = dp->b_forw; 426264Sbill goto loop; 427264Sbill } 4282674Swnj /* 4292674Swnj * Mark controller busy, and 4302674Swnj * determine destination of this request. 4312674Swnj */ 4322395Swnj um->um_tab.b_active++; 4332395Swnj ui = updinfo[dkunit(bp)]; 434264Sbill bn = dkblock(bp); 4352395Swnj dn = ui->ui_slave; 4362395Swnj st = &upst[ui->ui_type]; 4372395Swnj sn = bn%st->nspc; 4382395Swnj tn = sn/st->nsect; 4392395Swnj sn %= st->nsect; 4402629Swnj upaddr = (struct updevice *)ui->ui_addr; 4412674Swnj /* 4422674Swnj * Select drive if not selected already. 4432674Swnj */ 4442674Swnj if ((upaddr->upcs2&07) != dn) 4452674Swnj upaddr->upcs2 = dn; 4462674Swnj /* 4472674Swnj * Check that it is ready and online 4482674Swnj */ 4492681Swnj waitdry = 0; 4503445Sroot while ((upaddr->upds&UPDS_DRY) == 0) { 4517036Swnj printf("up%d: ds wait ds=%o\n",dkunit(bp),upaddr->upds); 4522681Swnj if (++waitdry > 512) 4532681Swnj break; 4542681Swnj upwaitdry++; 4552681Swnj } 4563445Sroot if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY) { 4572931Swnj printf("up%d: not ready", dkunit(bp)); 4583445Sroot if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY) { 4592607Swnj printf("\n"); 4602395Swnj um->um_tab.b_active = 0; 4612395Swnj um->um_tab.b_errcnt = 0; 462893Sbill dp->b_actf = bp->av_forw; 463893Sbill dp->b_active = 0; 464893Sbill bp->b_flags |= B_ERROR; 465893Sbill iodone(bp); 466893Sbill goto loop; 467893Sbill } 4682674Swnj /* 4692674Swnj * Oh, well, sometimes this 4702674Swnj * happens, for reasons unknown. 4712674Swnj */ 4722629Swnj printf(" (flakey)\n"); 473264Sbill } 4742674Swnj /* 4752674Swnj * Setup for the transfer, and get in the 4762674Swnj * UNIBUS adaptor queue. 4772674Swnj */ 4782424Skre upaddr->updc = bp->b_cylin; 479264Sbill upaddr->upda = (tn << 8) + sn; 480264Sbill upaddr->upwc = -bp->b_bcount / sizeof (short); 481264Sbill if (bp->b_flags & B_READ) 4822629Swnj cmd = UP_IE|UP_RCOM|UP_GO; 483264Sbill else 4842629Swnj cmd = UP_IE|UP_WCOM|UP_GO; 4852571Swnj um->um_cmd = cmd; 4863107Swnj (void) ubago(ui); 487268Sbill return (1); 488264Sbill } 489264Sbill 4902674Swnj /* 4912674Swnj * Now all ready to go, stuff the registers. 4922674Swnj */ 4932571Swnj updgo(um) 4942983Swnj struct uba_ctlr *um; 4952395Swnj { 4962629Swnj register struct updevice *upaddr = (struct updevice *)um->um_addr; 4972470Swnj 4986953Swnj um->um_tab.b_active = 2; /* should now be 2 */ 4992571Swnj upaddr->upba = um->um_ubinfo; 5002571Swnj upaddr->upcs1 = um->um_cmd|((um->um_ubinfo>>8)&0x300); 5012395Swnj } 5022395Swnj 5032674Swnj /* 5042674Swnj * Handle a disk interrupt. 5052674Swnj */ 5062707Swnj upintr(sc21) 5072395Swnj register sc21; 508264Sbill { 509264Sbill register struct buf *bp, *dp; 5102983Swnj register struct uba_ctlr *um = upminfo[sc21]; 5112983Swnj register struct uba_device *ui; 5122629Swnj register struct updevice *upaddr = (struct updevice *)um->um_addr; 513264Sbill register unit; 5142470Swnj struct up_softc *sc = &up_softc[um->um_ctlr]; 5152607Swnj int as = (upaddr->upas & 0377) | sc->sc_softas; 5162681Swnj int needie = 1, waitdry; 517264Sbill 5182470Swnj sc->sc_wticks = 0; 5192607Swnj sc->sc_softas = 0; 5202674Swnj /* 5212674Swnj * If controller wasn't transferring, then this is an 5222674Swnj * interrupt for attention status on seeking drives. 5232674Swnj * Just service them. 5242674Swnj */ 5256346Swnj if (um->um_tab.b_active != 2 && !sc->sc_recal) { 5262674Swnj if (upaddr->upcs1 & UP_TRE) 5272674Swnj upaddr->upcs1 = UP_TRE; 5282674Swnj goto doattn; 5292674Swnj } 5306953Swnj um->um_tab.b_active = 1; 5312674Swnj /* 5322674Swnj * Get device and block structures, and a pointer 5332983Swnj * to the uba_device for the drive. Select the drive. 5342674Swnj */ 5352674Swnj dp = um->um_tab.b_actf; 5362674Swnj bp = dp->b_actf; 5372674Swnj ui = updinfo[dkunit(bp)]; 5382674Swnj dk_busy &= ~(1 << ui->ui_dk); 5392674Swnj if ((upaddr->upcs2&07) != ui->ui_slave) 5402395Swnj upaddr->upcs2 = ui->ui_slave; 5419548Ssam if (bp->b_flags&B_BAD) { 5429548Ssam if (upecc(ui, CONT)) 5439548Ssam return; 5449548Ssam } 5452674Swnj /* 5462674Swnj * Check for and process errors on 5472674Swnj * either the drive or the controller. 5482674Swnj */ 5493445Sroot if ((upaddr->upds&UPDS_ERR) || (upaddr->upcs1&UP_TRE)) { 5502681Swnj waitdry = 0; 5513445Sroot while ((upaddr->upds & UPDS_DRY) == 0) { 5522681Swnj if (++waitdry > 512) 5532681Swnj break; 5542681Swnj upwaitdry++; 5552681Swnj } 5563445Sroot if (upaddr->uper1&UPER1_WLE) { 5572674Swnj /* 5582674Swnj * Give up on write locked devices 5592674Swnj * immediately. 5602674Swnj */ 5612931Swnj printf("up%d: write locked\n", dkunit(bp)); 5622674Swnj bp->b_flags |= B_ERROR; 5632674Swnj } else if (++um->um_tab.b_errcnt > 27) { 5642674Swnj /* 5652674Swnj * After 28 retries (16 without offset, and 5662674Swnj * 12 with offset positioning) give up. 56710904Shelge * If the error was header CRC, the header is 56810904Shelge * screwed up, and the sector may in fact exist 56910904Shelge * in the bad sector table, better check... 5702674Swnj */ 57110904Shelge if (upaddr->uper1&UPER1_HCRC) { 57210904Shelge if (upecc(ui, BSE)) 57310904Shelge return; 57410904Shelge } 5759548Ssam hard: 5762931Swnj harderr(bp, "up"); 5779548Ssam printf("cn=%d tn=%d sn=%d cs2=%b er1=%b er2=%b\n", 5789548Ssam upaddr->updc, ((upaddr->upda)>>8)&077, 5799548Ssam (upaddr->upda)&037, 5809548Ssam upaddr->upcs2, UPCS2_BITS, 5819548Ssam upaddr->uper1, UPER1_BITS, 5829548Ssam upaddr->uper2, UPER2_BITS); 5832674Swnj bp->b_flags |= B_ERROR; 5849548Ssam } else if (upaddr->uper2 & UPER2_BSE) { 5859548Ssam if (upecc(ui, BSE)) 5869548Ssam return; 5879548Ssam else 5889548Ssam goto hard; 5892674Swnj } else { 5902674Swnj /* 5912674Swnj * Retriable error. 5922674Swnj * If a soft ecc, correct it (continuing 5932674Swnj * by returning if necessary. 5942674Swnj * Otherwise fall through and retry the transfer 5952674Swnj */ 5967183Sroot if ((upaddr->uper1&(UPER1_DCK|UPER1_ECH))==UPER1_DCK) { 5979548Ssam if (upecc(ui, ECC)) 5982629Swnj return; 5997183Sroot } else 6007183Sroot um->um_tab.b_active = 0; /* force retry */ 6012674Swnj } 6022674Swnj /* 6032674Swnj * Clear drive error and, every eight attempts, 6042674Swnj * (starting with the fourth) 6052674Swnj * recalibrate to clear the slate. 6062674Swnj */ 6072674Swnj upaddr->upcs1 = UP_TRE|UP_IE|UP_DCLR|UP_GO; 6082674Swnj needie = 0; 6093182Swnj if ((um->um_tab.b_errcnt&07) == 4 && um->um_tab.b_active == 0) { 6102674Swnj upaddr->upcs1 = UP_RECAL|UP_IE|UP_GO; 6113160Swnj sc->sc_recal = 0; 6123160Swnj goto nextrecal; 6132674Swnj } 6142674Swnj } 6152674Swnj /* 6163160Swnj * Advance recalibration finite state machine 6173160Swnj * if recalibrate in progress, through 6183160Swnj * RECAL 6193160Swnj * SEEK 6203160Swnj * OFFSET (optional) 6213160Swnj * RETRY 6222674Swnj */ 6233160Swnj switch (sc->sc_recal) { 6243160Swnj 6253160Swnj case 1: 6263160Swnj upaddr->updc = bp->b_cylin; 6273160Swnj upaddr->upcs1 = UP_SEEK|UP_IE|UP_GO; 6283160Swnj goto nextrecal; 6293160Swnj case 2: 6303160Swnj if (um->um_tab.b_errcnt < 16 || (bp->b_flags&B_READ) == 0) 6313160Swnj goto donerecal; 6323445Sroot upaddr->upof = up_offset[um->um_tab.b_errcnt & 017] | UPOF_FMT22; 6333160Swnj upaddr->upcs1 = UP_IE|UP_OFFSET|UP_GO; 6343160Swnj goto nextrecal; 6353160Swnj nextrecal: 6363160Swnj sc->sc_recal++; 6373160Swnj um->um_tab.b_active = 1; 6383160Swnj return; 6393160Swnj donerecal: 6403160Swnj case 3: 6412674Swnj sc->sc_recal = 0; 6423160Swnj um->um_tab.b_active = 0; 6433160Swnj break; 6442674Swnj } 6452674Swnj /* 6462674Swnj * If still ``active'', then don't need any more retries. 6472674Swnj */ 6482674Swnj if (um->um_tab.b_active) { 6492674Swnj /* 6502674Swnj * If we were offset positioning, 6512674Swnj * return to centerline. 6522674Swnj */ 6532674Swnj if (um->um_tab.b_errcnt >= 16) { 6543445Sroot upaddr->upof = UPOF_FMT22; 6552674Swnj upaddr->upcs1 = UP_RTC|UP_GO|UP_IE; 6563445Sroot while (upaddr->upds & UPDS_PIP) 6572674Swnj DELAY(25); 658268Sbill needie = 0; 659264Sbill } 6602674Swnj um->um_tab.b_active = 0; 6612674Swnj um->um_tab.b_errcnt = 0; 6622674Swnj um->um_tab.b_actf = dp->b_forw; 6632674Swnj dp->b_active = 0; 6642674Swnj dp->b_errcnt = 0; 6652674Swnj dp->b_actf = bp->av_forw; 6662674Swnj bp->b_resid = (-upaddr->upwc * sizeof(short)); 6672674Swnj iodone(bp); 6682674Swnj /* 6692674Swnj * If this unit has more work to do, 6702674Swnj * then start it up right away. 6712674Swnj */ 6722674Swnj if (dp->b_actf) 6732674Swnj if (upustart(ui)) 674268Sbill needie = 0; 675264Sbill } 6762674Swnj as &= ~(1<<ui->ui_slave); 6773403Swnj /* 6783403Swnj * Release unibus resources and flush data paths. 6793403Swnj */ 6803403Swnj ubadone(um); 6812674Swnj doattn: 6822674Swnj /* 6832674Swnj * Process other units which need attention. 6842674Swnj * For each unit which needs attention, call 6852674Swnj * the unit start routine to place the slave 6862674Swnj * on the controller device queue. 6872674Swnj */ 6883160Swnj while (unit = ffs(as)) { 6893160Swnj unit--; /* was 1 origin */ 6903160Swnj as &= ~(1<<unit); 6913160Swnj upaddr->upas = 1<<unit; 6926383Swnj if (unit < UPIPUNITS && upustart(upip[sc21][unit])) 6933160Swnj needie = 0; 6943160Swnj } 6952674Swnj /* 6962674Swnj * If the controller is not transferring, but 6972674Swnj * there are devices ready to transfer, start 6982674Swnj * the controller. 6992674Swnj */ 7002395Swnj if (um->um_tab.b_actf && um->um_tab.b_active == 0) 7012395Swnj if (upstart(um)) 702268Sbill needie = 0; 703275Sbill if (needie) 7042629Swnj upaddr->upcs1 = UP_IE; 705264Sbill } 706264Sbill 7079357Ssam upread(dev, uio) 7082616Swnj dev_t dev; 7099357Ssam struct uio *uio; 710264Sbill { 7112616Swnj register int unit = minor(dev) >> 3; 7122470Swnj 7132616Swnj if (unit >= NUP) 7149357Ssam return (ENXIO); 7159357Ssam return (physio(upstrategy, &rupbuf[unit], dev, B_READ, minphys, uio)); 716264Sbill } 717264Sbill 7189357Ssam upwrite(dev, uio) 7192616Swnj dev_t dev; 7209357Ssam struct uio *uio; 721264Sbill { 7222616Swnj register int unit = minor(dev) >> 3; 7232470Swnj 7242616Swnj if (unit >= NUP) 7259357Ssam return (ENXIO); 7269357Ssam return (physio(upstrategy, &rupbuf[unit], dev, B_WRITE, minphys, uio)); 727264Sbill } 728264Sbill 729266Sbill /* 730266Sbill * Correct an ECC error, and restart the i/o to complete 731266Sbill * the transfer if necessary. This is quite complicated because 732266Sbill * the transfer may be going to an odd memory address base and/or 733266Sbill * across a page boundary. 734266Sbill */ 7359548Ssam upecc(ui, flag) 7362983Swnj register struct uba_device *ui; 7379548Ssam int flag; 738264Sbill { 7392629Swnj register struct updevice *up = (struct updevice *)ui->ui_addr; 7402395Swnj register struct buf *bp = uputab[ui->ui_unit].b_actf; 7412983Swnj register struct uba_ctlr *um = ui->ui_mi; 7422395Swnj register struct upst *st; 7432395Swnj struct uba_regs *ubp = ui->ui_hd->uh_uba; 744266Sbill register int i; 745264Sbill caddr_t addr; 746266Sbill int reg, bit, byte, npf, mask, o, cmd, ubaddr; 747264Sbill int bn, cn, tn, sn; 748264Sbill 749264Sbill /* 750266Sbill * Npf is the number of sectors transferred before the sector 751266Sbill * containing the ECC error, and reg is the UBA register 752266Sbill * mapping (the first part of) the transfer. 753266Sbill * O is offset within a memory page of the first byte transferred. 754264Sbill */ 7559548Ssam if (flag == CONT) 7569548Ssam npf = bp->b_error; 7579548Ssam else 75810858Ssam npf = btop((up->upwc * sizeof(short)) + bp->b_bcount); 7592571Swnj reg = btop(um->um_ubinfo&0x3ffff) + npf; 760264Sbill o = (int)bp->b_un.b_addr & PGOFSET; 761264Sbill mask = up->upec2; 7623445Sroot #ifdef UPECCDEBUG 7633403Swnj printf("npf %d reg %x o %d mask %o pos %d\n", npf, reg, o, mask, 7643403Swnj up->upec1); 7653445Sroot #endif 7669548Ssam bn = dkblock(bp); 7679548Ssam st = &upst[ui->ui_type]; 7689548Ssam cn = bp->b_cylin; 7699548Ssam sn = bn%st->nspc + npf; 7709548Ssam tn = sn/st->nsect; 7719548Ssam sn %= st->nsect; 7729548Ssam cn += tn/st->ntrak; 7739548Ssam tn %= st->ntrak; 7742725Swnj ubapurge(um); 7759548Ssam um->um_tab.b_active=2; 776266Sbill /* 7779548Ssam * action taken depends on the flag 778266Sbill */ 7799548Ssam switch(flag){ 7809548Ssam case ECC: 7819548Ssam npf--; 7829548Ssam reg--; 7839548Ssam mask = up->upec2; 7849548Ssam printf("up%d%c: soft ecc sn%d\n", dkunit(bp), 7859548Ssam 'a'+(minor(bp->b_dev)&07), bp->b_blkno + npf); 7869548Ssam /* 7879548Ssam * Flush the buffered data path, and compute the 7889548Ssam * byte and bit position of the error. The variable i 7899548Ssam * is the byte offset in the transfer, the variable byte 7909548Ssam * is the offset from a page boundary in main memory. 7919548Ssam */ 7929548Ssam i = up->upec1 - 1; /* -1 makes 0 origin */ 7939548Ssam bit = i&07; 7949548Ssam i = (i&~07)>>3; 7959548Ssam byte = i + o; 7969548Ssam /* 7979548Ssam * Correct while possible bits remain of mask. Since mask 7989548Ssam * contains 11 bits, we continue while the bit offset is > -11. 7999548Ssam * Also watch out for end of this block and the end of the whole 8009548Ssam * transfer. 8019548Ssam */ 8029548Ssam while (i < 512 && (int)ptob(npf)+i < bp->b_bcount && bit > -11) { 8039548Ssam addr = ptob(ubp->uba_map[reg+btop(byte)].pg_pfnum)+ 8049548Ssam (byte & PGOFSET); 8053445Sroot #ifdef UPECCDEBUG 8069548Ssam printf("addr %x map reg %x\n", 8079548Ssam addr, *(int *)(&ubp->uba_map[reg+btop(byte)])); 8089548Ssam printf("old: %x, ", getmemc(addr)); 8093445Sroot #endif 8109548Ssam putmemc(addr, getmemc(addr)^(mask<<bit)); 8113445Sroot #ifdef UPECCDEBUG 8129548Ssam printf("new: %x\n", getmemc(addr)); 8133445Sroot #endif 8149548Ssam byte++; 8159548Ssam i++; 8169580Shelge bit -= 8; 8179548Ssam } 8189548Ssam if (up->upwc == 0) 8199548Ssam return (0); 8209548Ssam npf++; 8219548Ssam reg++; 8229548Ssam break; 8239548Ssam case BSE: 8249548Ssam /* 8259548Ssam * if not in bad sector table, return 0 8269548Ssam */ 8279548Ssam if ((bn = isbad(&upbad[ui->ui_unit], cn, tn, sn)) < 0) 8289548Ssam return(0); 8299548Ssam /* 8309548Ssam * flag this one as bad 8319548Ssam */ 8329548Ssam bp->b_flags |= B_BAD; 8339548Ssam bp->b_error = npf + 1; 8349548Ssam #ifdef UPECCDEBUG 8359548Ssam printf("BSE: restart at %d\n",npf+1); 8369548Ssam #endif 8379548Ssam bn = st->ncyl * st->nspc -st->nsect - 1 - bn; 8389548Ssam cn = bn / st->nspc; 8399548Ssam sn = bn % st->nspc; 8409548Ssam tn = sn / st->nsect; 8419548Ssam sn %= st->nsect; 8429548Ssam up->upwc = -(512 / sizeof (short)); 8439548Ssam #ifdef UPECCDEBUG 8449548Ssam printf("revector to cn %d tn %d sn %d\n", cn, tn, sn); 8459548Ssam #endif 8469548Ssam break; 8479548Ssam case CONT: 8489548Ssam #ifdef UPECCDEBUG 8499548Ssam printf("upecc, CONT: bn %d cn %d tn %d sn %d\n", bn, cn, tn, sn); 8509548Ssam #endif 8519548Ssam bp->b_flags &= ~B_BAD; 8529548Ssam up->upwc = -((bp->b_bcount - (int)ptob(npf)) / sizeof(short)); 8539548Ssam if (up->upwc == 0) 8549548Ssam return(0); 8559548Ssam break; 856264Sbill } 8577183Sroot if (up->upwc == 0) { 8587183Sroot um->um_tab.b_active = 0; 859264Sbill return (0); 8607183Sroot } 861266Sbill /* 862266Sbill * Have to continue the transfer... clear the drive, 863266Sbill * and compute the position where the transfer is to continue. 864266Sbill * We have completed npf+1 sectors of the transfer already; 865266Sbill * restart at offset o of next sector (i.e. in UBA register reg+1). 866266Sbill */ 8672629Swnj #ifdef notdef 8682629Swnj up->uper1 = 0; 8692629Swnj up->upcs1 |= UP_GO; 8702629Swnj #else 8712629Swnj up->upcs1 = UP_TRE|UP_IE|UP_DCLR|UP_GO; 872264Sbill up->updc = cn; 873266Sbill up->upda = (tn << 8) | sn; 8749548Ssam ubaddr = (int)ptob(reg) + o; 875266Sbill up->upba = ubaddr; 876266Sbill cmd = (ubaddr >> 8) & 0x300; 8779548Ssam cmd |= ((bp->b_flags&B_READ)?UP_RCOM:UP_WCOM)|UP_IE|UP_GO; 8789548Ssam um->um_tab.b_errcnt = 0; 879266Sbill up->upcs1 = cmd; 8802629Swnj #endif 881264Sbill return (1); 882264Sbill } 883286Sbill 884286Sbill /* 885286Sbill * Reset driver after UBA init. 886286Sbill * Cancel software state of all pending transfers 887286Sbill * and restart all units and the controller. 888286Sbill */ 8892395Swnj upreset(uban) 8902931Swnj int uban; 891286Sbill { 8922983Swnj register struct uba_ctlr *um; 8932983Swnj register struct uba_device *ui; 8942395Swnj register sc21, unit; 895286Sbill 8962646Swnj for (sc21 = 0; sc21 < NSC; sc21++) { 8972470Swnj if ((um = upminfo[sc21]) == 0 || um->um_ubanum != uban || 8982470Swnj um->um_alive == 0) 8992395Swnj continue; 9002931Swnj printf(" sc%d", sc21); 9012395Swnj um->um_tab.b_active = 0; 9022395Swnj um->um_tab.b_actf = um->um_tab.b_actl = 0; 9032931Swnj up_softc[sc21].sc_recal = 0; 9046346Swnj up_softc[sc21].sc_wticks = 0; 9052571Swnj if (um->um_ubinfo) { 9062571Swnj printf("<%d>", (um->um_ubinfo>>28)&0xf); 9079357Ssam um->um_ubinfo = 0; 9082395Swnj } 9093445Sroot ((struct updevice *)(um->um_addr))->upcs2 = UPCS2_CLR; 9102395Swnj for (unit = 0; unit < NUP; unit++) { 9112395Swnj if ((ui = updinfo[unit]) == 0) 9122395Swnj continue; 9132931Swnj if (ui->ui_alive == 0 || ui->ui_mi != um) 9142395Swnj continue; 9152395Swnj uputab[unit].b_active = 0; 9162395Swnj (void) upustart(ui); 9172395Swnj } 9182395Swnj (void) upstart(um); 919286Sbill } 920286Sbill } 921313Sbill 922313Sbill /* 923313Sbill * Wake up every second and if an interrupt is pending 924313Sbill * but nothing has happened increment a counter. 9252931Swnj * If nothing happens for 20 seconds, reset the UNIBUS 926313Sbill * and begin anew. 927313Sbill */ 928313Sbill upwatch() 929313Sbill { 9302983Swnj register struct uba_ctlr *um; 9312395Swnj register sc21, unit; 9322470Swnj register struct up_softc *sc; 933313Sbill 9342759Swnj timeout(upwatch, (caddr_t)0, hz); 9352646Swnj for (sc21 = 0; sc21 < NSC; sc21++) { 9362395Swnj um = upminfo[sc21]; 9372470Swnj if (um == 0 || um->um_alive == 0) 9382470Swnj continue; 9392470Swnj sc = &up_softc[sc21]; 9402395Swnj if (um->um_tab.b_active == 0) { 9412395Swnj for (unit = 0; unit < NUP; unit++) 9422629Swnj if (uputab[unit].b_active && 9432629Swnj updinfo[unit]->ui_mi == um) 9442395Swnj goto active; 9452470Swnj sc->sc_wticks = 0; 9462395Swnj continue; 9472395Swnj } 9482931Swnj active: 9492470Swnj sc->sc_wticks++; 9502470Swnj if (sc->sc_wticks >= 20) { 9512470Swnj sc->sc_wticks = 0; 9522931Swnj printf("sc%d: lost interrupt\n", sc21); 9532646Swnj ubareset(um->um_ubanum); 9542395Swnj } 955313Sbill } 956313Sbill } 9572379Swnj 9582379Swnj #define DBSIZE 20 9592379Swnj 9602379Swnj updump(dev) 9612379Swnj dev_t dev; 9622379Swnj { 9632629Swnj struct updevice *upaddr; 9642379Swnj char *start; 9653107Swnj int num, blk, unit; 9662379Swnj struct size *sizes; 9672395Swnj register struct uba_regs *uba; 9682983Swnj register struct uba_device *ui; 9692379Swnj register short *rp; 9702395Swnj struct upst *st; 9716848Ssam register int retry; 9722379Swnj 9732395Swnj unit = minor(dev) >> 3; 9742889Swnj if (unit >= NUP) 9752889Swnj return (ENXIO); 9762470Swnj #define phys(cast, addr) ((cast)((int)addr & 0x7fffffff)) 9772983Swnj ui = phys(struct uba_device *, updinfo[unit]); 9782889Swnj if (ui->ui_alive == 0) 9792889Swnj return (ENXIO); 9802395Swnj uba = phys(struct uba_hd *, ui->ui_hd)->uh_physuba; 9812983Swnj ubainit(uba); 9822629Swnj upaddr = (struct updevice *)ui->ui_physaddr; 9836848Ssam DELAY(5000000); 9842379Swnj num = maxfree; 9852379Swnj upaddr->upcs2 = unit; 9862983Swnj DELAY(100); 9876848Ssam upaddr->upcs1 = UP_DCLR|UP_GO; 9886848Ssam upaddr->upcs1 = UP_PRESET|UP_GO; 9896848Ssam upaddr->upof = UPOF_FMT22; 9906848Ssam retry = 0; 9916848Ssam do { 9926848Ssam DELAY(25); 9936848Ssam if (++retry > 527) 9946848Ssam break; 9956861Ssam } while ((upaddr->upds & UP_RDY) == 0); 9963445Sroot if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY) 9972889Swnj return (EFAULT); 9989357Ssam start = 0; 9998489Sroot st = &upst[ui->ui_type]; 10002395Swnj sizes = phys(struct size *, st->sizes); 10012889Swnj if (dumplo < 0 || dumplo + num >= sizes[minor(dev)&07].nblocks) 10022889Swnj return (EINVAL); 10032379Swnj while (num > 0) { 10042379Swnj register struct pte *io; 10052379Swnj register int i; 10062379Swnj int cn, sn, tn; 10072379Swnj daddr_t bn; 10082379Swnj 10092379Swnj blk = num > DBSIZE ? DBSIZE : num; 10102395Swnj io = uba->uba_map; 10112379Swnj for (i = 0; i < blk; i++) 10122983Swnj *(int *)io++ = (btop(start)+i) | (1<<21) | UBAMR_MRV; 10132379Swnj *(int *)io = 0; 10142379Swnj bn = dumplo + btop(start); 10152607Swnj cn = bn/st->nspc + sizes[minor(dev)&07].cyloff; 10162607Swnj sn = bn%st->nspc; 10172607Swnj tn = sn/st->nsect; 10182607Swnj sn = sn%st->nsect; 10192379Swnj upaddr->updc = cn; 10202379Swnj rp = (short *) &upaddr->upda; 10212379Swnj *rp = (tn << 8) + sn; 10222379Swnj *--rp = 0; 10232379Swnj *--rp = -blk*NBPG / sizeof (short); 10242629Swnj *--rp = UP_GO|UP_WCOM; 10256848Ssam retry = 0; 10262379Swnj do { 10272379Swnj DELAY(25); 10286848Ssam if (++retry > 527) 10296848Ssam break; 10302629Swnj } while ((upaddr->upcs1 & UP_RDY) == 0); 10316848Ssam if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY) { 10326861Ssam printf("up%d: not ready", unit); 10336848Ssam if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY) { 10346848Ssam printf("\n"); 10356848Ssam return (EIO); 10366848Ssam } 10376848Ssam printf(" (flakey)\n"); 10386848Ssam } 10393445Sroot if (upaddr->upds&UPDS_ERR) 10402889Swnj return (EIO); 10412379Swnj start += blk*NBPG; 10422379Swnj num -= blk; 10432379Swnj } 10442379Swnj return (0); 10452379Swnj } 10461902Swnj #endif 1047