consolidate random data structures
[unix-history] / usr / src / sys / vax / uba / up.c
CommitLineData
fa9bff38 1/* up.c 4.65 82/12/17 */
008c0481 2
66b4fb09 3#include "up.h"
a3cb8f60 4#if NSC > 0
008c0481 5/*
7ed3da3d
BJ
6 * UNIBUS disk driver with:
7 * overlapped seeks,
8 * ECC recovery, and
9 * bad sector forwarding.
0c48c799
BJ
10 *
11 * TODO:
299d67ed 12 * Check that offset recovery code works
008c0481 13 */
961945a8 14#include "../machine/pte.h"
008c0481
BJ
15
16#include "../h/param.h"
17#include "../h/systm.h"
41888f16 18#include "../h/dk.h"
3b186c1c 19#include "../h/dkbad.h"
008c0481
BJ
20#include "../h/buf.h"
21#include "../h/conf.h"
22#include "../h/dir.h"
23#include "../h/user.h"
24#include "../h/map.h"
008c0481 25#include "../h/vm.h"
0ff318b2 26#include "../h/cmap.h"
740e4029 27#include "../h/uio.h"
7ed3da3d 28#include "../h/dkbad.h"
18eded4b
BJ
29#include "../vax/cpu.h"
30#include "../vax/nexus.h"
31#include "../vaxuba/ubavar.h"
32#include "../vaxuba/ubareg.h"
33#include "../vaxuba/upreg.h"
008c0481 34
3f3a34c3
BJ
35struct up_softc {
36 int sc_softas;
71236e46 37 int sc_ndrive;
3f3a34c3 38 int sc_wticks;
736772ef 39 int sc_recal;
a3cb8f60 40} up_softc[NSC];
008c0481 41
3f3a34c3 42/* THIS SHOULD BE READ OFF THE PACK, PER DRIVE */
008c0481
BJ
43struct size
44{
45 daddr_t nblocks;
46 int cyloff;
47} up_sizes[8] = {
7ed3da3d
BJ
48#ifdef ERNIE
49 49324, 0, /* A=cyl 0 thru 26 */
50#else
c94d7fe4
BJ
51#ifdef ERNIE
52 49324, 0, /* A=cyl 0 thru 26 */
53#else
008c0481 54 15884, 0, /* A=cyl 0 thru 26 */
7ed3da3d 55#endif
c94d7fe4 56#endif
008c0481 57 33440, 27, /* B=cyl 27 thru 81 */
d1778415 58 495520, 0, /* C=cyl 0 thru 814 */
008c0481
BJ
59 15884, 562, /* D=cyl 562 thru 588 */
60 55936, 589, /* E=cyl 589 thru 680 */
0d728200
BJ
61#ifndef NOBADSECT
62 81376, 681, /* F=cyl 681 thru 814 */
63 153728, 562, /* G=cyl 562 thru 814 */
64#else
65 81472, 681,
66 153824, 562,
67#endif
008c0481 68 291346, 82, /* H=cyl 82 thru 561 */
3f3a34c3
BJ
69}, fj_sizes[8] = {
70 15884, 0, /* A=cyl 0 thru 49 */
71 33440, 50, /* B=cyl 50 thru 154 */
72 263360, 0, /* C=cyl 0 thru 822 */
73 0, 0,
74 0, 0,
75 0, 0,
76 0, 0,
0d728200
BJ
77#ifndef NOBADSECT
78 213664, 155, /* H=cyl 155 thru 822 */
79#else
80 213760, 155,
81#endif
a1ef7a52 82}, upam_sizes[8] = {
0cc9b5a9
BJ
83 15884, 0, /* A=cyl 0 thru 31 */
84 33440, 32, /* B=cyl 32 thru 97 */
85 524288, 0, /* C=cyl 0 thru 1023 */
eadd189e
SL
86 27786, 668,
87 27786, 723,
88 125440, 778,
0cc9b5a9
BJ
89 181760, 668, /* G=cyl 668 thru 1022 */
90 291346, 98, /* H=cyl 98 thru 667 */
008c0481 91};
3f3a34c3 92/* END OF STUFF WHICH SHOULD BE READ IN PER DISK */
008c0481 93
cc7ff771
BJ
94/*
95 * On a 780 upSDIST could be 2, but
96 * in the interest of 750's...
97 */
98#define _upSDIST 3 /* 1.5 msec */
3f3a34c3
BJ
99#define _upRDIST 4 /* 2.0 msec */
100
101int upSDIST = _upSDIST;
102int upRDIST = _upRDIST;
103
71236e46 104int upprobe(), upslave(), upattach(), updgo(), upintr();
89bd2f01
BJ
105struct uba_ctlr *upminfo[NSC];
106struct uba_device *updinfo[NUP];
c10bcf0a
BJ
107#define UPIPUNITS 8
108struct uba_device *upip[NSC][UPIPUNITS]; /* fuji w/fixed head gives n,n+4 */
d763a2b7 109
71236e46 110u_short upstd[] = { 0776700, 0774400, 0776300, 0 };
0801d37f 111struct uba_driver scdriver =
71236e46 112 { upprobe, upslave, upattach, updgo, upstd, "up", updinfo, "sc", upminfo };
3f3a34c3 113struct buf uputab[NUP];
7ed3da3d 114char upinit[NUP];
3b186c1c 115char upinit[NUP];
3f3a34c3
BJ
116
117struct upst {
118 short nsect;
119 short ntrak;
120 short nspc;
121 short ncyl;
122 struct size *sizes;
123} upst[] = {
71236e46
BJ
124 32, 19, 32*19, 823, up_sizes, /* 9300/cdc */
125/* 9300 actually has 815 cylinders... */
3f3a34c3 126 32, 10, 32*10, 823, fj_sizes, /* fujitsu 160m */
a1ef7a52 127 32, 16, 32*16, 1024, upam_sizes, /* ampex capricorn */
3f3a34c3 128};
008c0481 129
2601dfbd 130u_char up_offset[16] = {
7ed3da3d
BJ
131 UPOF_P400, UPOF_M400, UPOF_P400, UPOF_M400,
132 UPOF_P800, UPOF_M800, UPOF_P800, UPOF_M800,
133 UPOF_P1200, UPOF_M1200, UPOF_P1200, UPOF_M1200,
134 0, 0, 0, 0
2601dfbd 135};
008c0481 136
0801d37f 137struct buf rupbuf[NUP];
7ed3da3d
BJ
138#ifndef NOBADSECT
139struct buf bupbuf[NUP];
140struct dkbad upbad[NUP];
141#endif
3b186c1c
SL
142#ifndef NOBADSECT
143struct buf bupbuf[NUP];
144struct dkbad upbad[NUP];
145#endif
008c0481 146
008c0481
BJ
147#define b_cylin b_resid
148
008c0481
BJ
149#ifdef INTRLVE
150daddr_t dkblock();
151#endif
3f3a34c3
BJ
152
153int upwstart, upwatch(); /* Have started guardian */
7e00c42b 154int upseek;
f88f8fdb 155int upwaitdry;
3f3a34c3
BJ
156
157/*ARGSUSED*/
71236e46 158upprobe(reg)
3f3a34c3 159 caddr_t reg;
008c0481 160{
d763a2b7
BJ
161 register int br, cvec;
162
71236e46
BJ
163#ifdef lint
164 br = 0; cvec = br; br = cvec;
165#endif
2601dfbd 166 ((struct updevice *)reg)->upcs1 = UP_IE|UP_RDY;
71236e46 167 DELAY(10);
2601dfbd 168 ((struct updevice *)reg)->upcs1 = 0;
9c0adba0 169 return (sizeof (struct updevice));
3f3a34c3
BJ
170}
171
71236e46 172upslave(ui, reg)
89bd2f01 173 struct uba_device *ui;
3f3a34c3
BJ
174 caddr_t reg;
175{
2601dfbd 176 register struct updevice *upaddr = (struct updevice *)reg;
3f3a34c3
BJ
177
178 upaddr->upcs1 = 0; /* conservative */
71236e46 179 upaddr->upcs2 = ui->ui_slave;
c224ab94 180 upaddr->upcs1 = UP_NOP|UP_GO;
7ed3da3d 181 upaddr->upcs1 = UP_NOP|UP_GO;
299d67ed 182 if (upaddr->upcs2&UPCS2_NED) {
2601dfbd 183 upaddr->upcs1 = UP_DCLR|UP_GO;
3f3a34c3
BJ
184 return (0);
185 }
71236e46
BJ
186 return (1);
187}
188
189upattach(ui)
89bd2f01 190 register struct uba_device *ui;
71236e46 191{
2601dfbd 192 register struct updevice *upaddr;
71236e46 193
6a81870e 194 if (upwstart == 0) {
7780575a 195 timeout(upwatch, (caddr_t)0, hz);
6a81870e
BJ
196 upwstart++;
197 }
b7333467
BJ
198 if (ui->ui_dk >= 0)
199 dk_mspw[ui->ui_dk] = .0000020345;
71236e46
BJ
200 upip[ui->ui_ctlr][ui->ui_slave] = ui;
201 up_softc[ui->ui_ctlr].sc_ndrive++;
2601dfbd
BJ
202 upaddr = (struct updevice *)ui->ui_addr;
203 upaddr->upcs1 = 0;
204 upaddr->upcs2 = ui->ui_slave;
26e15512 205 upaddr->uphr = UPHR_MAXTRAK;
c6a51a7b 206 if (upaddr->uphr == 9)
26e15512 207 ui->ui_type = 1; /* fujitsu hack */
0cc9b5a9
BJ
208 else if (upaddr->uphr == 15)
209 ui->ui_type = 2; /* ampex hack */
26e15512 210 upaddr->upcs2 = UPCS2_CLR;
3f3a34c3
BJ
211}
212
3b186c1c
SL
213upopen(dev)
214 dev_t dev;
215{
216 register int unit = minor(dev) >> 3;
217 register struct uba_device *ui;
218
219 if (unit >= NUP || (ui = updinfo[unit]) == 0 || ui->ui_alive == 0)
220 return (ENXIO);
221 return (0);
222}
223
3f3a34c3
BJ
224upstrategy(bp)
225 register struct buf *bp;
226{
89bd2f01 227 register struct uba_device *ui;
3f3a34c3
BJ
228 register struct upst *st;
229 register int unit;
7e00c42b 230 register struct buf *dp;
3f3a34c3 231 int xunit = minor(bp->b_dev) & 07;
7e00c42b 232 long bn, sz;
3f3a34c3 233
7e00c42b 234 sz = (bp->b_bcount+511) >> 9;
008c0481 235 unit = dkunit(bp);
3f3a34c3
BJ
236 if (unit >= NUP)
237 goto bad;
238 ui = updinfo[unit];
239 if (ui == 0 || ui->ui_alive == 0)
240 goto bad;
241 st = &upst[ui->ui_type];
242 if (bp->b_blkno < 0 ||
243 (bn = dkblock(bp))+sz > st->sizes[xunit].nblocks)
244 goto bad;
245 bp->b_cylin = bn/st->nspc + st->sizes[xunit].cyloff;
0cc9b5a9 246 (void) spl5();
7e00c42b
BJ
247 dp = &uputab[ui->ui_unit];
248 disksort(dp, bp);
249 if (dp->b_active == 0) {
3f3a34c3
BJ
250 (void) upustart(ui);
251 bp = &ui->ui_mi->um_tab;
252 if (bp->b_actf && bp->b_active == 0)
253 (void) upstart(ui->ui_mi);
008c0481 254 }
0cc9b5a9 255 (void) spl0();
3f3a34c3
BJ
256 return;
257
258bad:
259 bp->b_flags |= B_ERROR;
260 iodone(bp);
261 return;
008c0481
BJ
262}
263
736772ef
BJ
264/*
265 * Unit start routine.
266 * Seek the drive to be where the data is
267 * and then generate another interrupt
268 * to actually start the transfer.
269 * If there is only one drive on the controller,
270 * or we are very close to the data, don't
271 * bother with the search. If called after
272 * searching once, don't bother to look where
273 * we are, just queue for transfer (to avoid
274 * positioning forever without transferrring.)
275 */
3f3a34c3 276upustart(ui)
89bd2f01 277 register struct uba_device *ui;
008c0481
BJ
278{
279 register struct buf *bp, *dp;
89bd2f01 280 register struct uba_ctlr *um;
2601dfbd 281 register struct updevice *upaddr;
3f3a34c3 282 register struct upst *st;
008c0481 283 daddr_t bn;
736772ef 284 int sn, csn;
71236e46
BJ
285 /*
286 * The SC21 cancels commands if you just say
2601dfbd 287 * cs1 = UP_IE
71236e46
BJ
288 * so we are cautious about handling of cs1.
289 * Also don't bother to clear as bits other than in upintr().
290 */
736772ef
BJ
291 int didie = 0;
292
293 if (ui == 0)
294 return (0);
89bd2f01 295 um = ui->ui_mi;
3f3a34c3
BJ
296 dk_busy &= ~(1<<ui->ui_dk);
297 dp = &uputab[ui->ui_unit];
7bc8d985 298 if ((bp = dp->b_actf) == NULL)
eb891eaa 299 goto out;
736772ef
BJ
300 /*
301 * If the controller is active, just remember
302 * that this device would like to be positioned...
303 * if we tried to position now we would confuse the SC21.
304 */
3f3a34c3 305 if (um->um_tab.b_active) {
d763a2b7 306 up_softc[um->um_ctlr].sc_softas |= 1<<ui->ui_slave;
2a3b9a7f
BJ
307 return (0);
308 }
736772ef
BJ
309 /*
310 * If we have already positioned this drive,
311 * then just put it on the ready queue.
312 */
a3f430e0
BJ
313 if (dp->b_active)
314 goto done;
315 dp->b_active = 1;
2601dfbd 316 upaddr = (struct updevice *)um->um_addr;
3f3a34c3 317 upaddr->upcs2 = ui->ui_slave;
736772ef
BJ
318 /*
319 * If drive has just come up,
320 * setup the pack.
321 */
7ed3da3d
BJ
322 if ((upaddr->upds & UPDS_VV) == 0 || upinit[ui->ui_unit] == 0) {
323#ifndef NOBADSECT
324 struct buf *bbp = &bupbuf[ui->ui_unit];
325#endif
71236e46 326 /* SHOULD WARN SYSTEM THAT THIS HAPPENED */
7ed3da3d 327 upinit[ui->ui_unit] = 1;
3b186c1c 328 upinit[ui->ui_unit] = 1;
2601dfbd
BJ
329 upaddr->upcs1 = UP_IE|UP_DCLR|UP_GO;
330 upaddr->upcs1 = UP_IE|UP_PRESET|UP_GO;
299d67ed 331 upaddr->upof = UPOF_FMT22;
eb891eaa 332 didie = 1;
3b186c1c
SL
333#ifndef NOBADSECT
334 st = &upst[ui->ui_type];
335 bbp->b_flags = B_READ|B_BUSY;
336 bbp->b_dev = bp->b_dev;
337 bbp->b_bcount = 512;
338 bbp->b_un.b_addr = (caddr_t)&upbad[ui->ui_unit];
339 bbp->b_blkno = st->ncyl * st->nspc - st->nsect;
340 bbp->b_cylin = st->ncyl - 1;
341 dp->b_actf = bbp;
342 bbp->av_forw = bp;
343 bp = bbp;
344#endif
7ed3da3d
BJ
345#ifndef NOBADSECT
346 st = &upst[ui->ui_type];
347 bbp->b_flags = B_READ|B_BUSY;
348 bbp->b_dev = bp->b_dev;
349 bbp->b_bcount = 512;
350 bbp->b_un.b_addr = (caddr_t)&upbad[ui->ui_unit];
351 bbp->b_blkno = st->ncyl * st->nspc - st->nsect;
352 bbp->b_cylin = st->ncyl - 1;
353 dp->b_actf = bbp;
354 bbp->av_forw = bp;
355 bp = bbp;
356#endif
008c0481 357 }
736772ef
BJ
358 /*
359 * If drive is offline, forget about positioning.
360 */
299d67ed 361 if ((upaddr->upds & (UPDS_DPR|UPDS_MOL)) != (UPDS_DPR|UPDS_MOL))
2a3b9a7f 362 goto done;
736772ef
BJ
363 /*
364 * If there is only one drive,
365 * dont bother searching.
366 */
71236e46
BJ
367 if (up_softc[um->um_ctlr].sc_ndrive == 1)
368 goto done;
736772ef
BJ
369 /*
370 * Figure out where this transfer is going to
371 * and see if we are close enough to justify not searching.
372 */
3f3a34c3 373 st = &upst[ui->ui_type];
008c0481 374 bn = dkblock(bp);
3f3a34c3
BJ
375 sn = bn%st->nspc;
376 sn = (sn + st->nsect - upSDIST) % st->nsect;
736772ef 377 if (bp->b_cylin - upaddr->updc)
7bc8d985 378 goto search; /* Not on-cylinder */
2a3b9a7f
BJ
379 else if (upseek)
380 goto done; /* Ok just to be on-cylinder */
008c0481 381 csn = (upaddr->upla>>6) - sn - 1;
7bc8d985 382 if (csn < 0)
3f3a34c3
BJ
383 csn += st->nsect;
384 if (csn > st->nsect - upRDIST)
008c0481 385 goto done;
008c0481 386search:
736772ef
BJ
387 upaddr->updc = bp->b_cylin;
388 /*
389 * Not on cylinder at correct position,
390 * seek/search.
391 */
2a3b9a7f 392 if (upseek)
2601dfbd 393 upaddr->upcs1 = UP_IE|UP_SEEK|UP_GO;
7e00c42b 394 else {
2a3b9a7f 395 upaddr->upda = sn;
2601dfbd 396 upaddr->upcs1 = UP_IE|UP_SEARCH|UP_GO;
2a3b9a7f 397 }
eb891eaa 398 didie = 1;
736772ef
BJ
399 /*
400 * Mark unit busy for iostat.
401 */
3f3a34c3
BJ
402 if (ui->ui_dk >= 0) {
403 dk_busy |= 1<<ui->ui_dk;
404 dk_seek[ui->ui_dk]++;
008c0481 405 }
eb891eaa 406 goto out;
008c0481 407done:
736772ef
BJ
408 /*
409 * Device is ready to go.
410 * Put it on the ready queue for the controller
411 * (unless its already there.)
412 */
2601dfbd
BJ
413 if (dp->b_active != 2) {
414 dp->b_forw = NULL;
415 if (um->um_tab.b_actf == NULL)
416 um->um_tab.b_actf = dp;
417 else
418 um->um_tab.b_actl->b_forw = dp;
419 um->um_tab.b_actl = dp;
420 dp->b_active = 2;
421 }
eb891eaa
BJ
422out:
423 return (didie);
008c0481
BJ
424}
425
736772ef
BJ
426/*
427 * Start up a transfer on a drive.
428 */
3f3a34c3 429upstart(um)
89bd2f01 430 register struct uba_ctlr *um;
008c0481
BJ
431{
432 register struct buf *bp, *dp;
89bd2f01 433 register struct uba_device *ui;
2601dfbd 434 register struct updevice *upaddr;
7e00c42b 435 struct upst *st;
008c0481 436 daddr_t bn;
f88f8fdb 437 int dn, sn, tn, cmd, waitdry;
008c0481 438
008c0481 439loop:
736772ef
BJ
440 /*
441 * Pull a request off the controller queue
442 */
3f3a34c3 443 if ((dp = um->um_tab.b_actf) == NULL)
eb891eaa 444 return (0);
008c0481 445 if ((bp = dp->b_actf) == NULL) {
3f3a34c3 446 um->um_tab.b_actf = dp->b_forw;
008c0481
BJ
447 goto loop;
448 }
736772ef
BJ
449 /*
450 * Mark controller busy, and
451 * determine destination of this request.
452 */
3f3a34c3
BJ
453 um->um_tab.b_active++;
454 ui = updinfo[dkunit(bp)];
008c0481 455 bn = dkblock(bp);
3f3a34c3
BJ
456 dn = ui->ui_slave;
457 st = &upst[ui->ui_type];
458 sn = bn%st->nspc;
459 tn = sn/st->nsect;
460 sn %= st->nsect;
2601dfbd 461 upaddr = (struct updevice *)ui->ui_addr;
736772ef
BJ
462 /*
463 * Select drive if not selected already.
464 */
465 if ((upaddr->upcs2&07) != dn)
466 upaddr->upcs2 = dn;
467 /*
468 * Check that it is ready and online
469 */
f88f8fdb 470 waitdry = 0;
299d67ed 471 while ((upaddr->upds&UPDS_DRY) == 0) {
7ed3da3d 472 printf("up%d: ds wait ds=%o\n",dkunit(bp),upaddr->upds);
ac1276f8 473 printf("up%d: ds wait ds=%o\n",dkunit(bp),upaddr->upds);
f88f8fdb
BJ
474 if (++waitdry > 512)
475 break;
476 upwaitdry++;
477 }
299d67ed 478 if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY) {
f6d201ff 479 printf("up%d: not ready", dkunit(bp));
299d67ed 480 if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY) {
71236e46 481 printf("\n");
3f3a34c3
BJ
482 um->um_tab.b_active = 0;
483 um->um_tab.b_errcnt = 0;
88253fd2
BJ
484 dp->b_actf = bp->av_forw;
485 dp->b_active = 0;
486 bp->b_flags |= B_ERROR;
487 iodone(bp);
88253fd2
BJ
488 goto loop;
489 }
736772ef
BJ
490 /*
491 * Oh, well, sometimes this
492 * happens, for reasons unknown.
493 */
2601dfbd 494 printf(" (flakey)\n");
008c0481 495 }
736772ef
BJ
496 /*
497 * Setup for the transfer, and get in the
498 * UNIBUS adaptor queue.
499 */
5aa9d5ea 500 upaddr->updc = bp->b_cylin;
008c0481 501 upaddr->upda = (tn << 8) + sn;
008c0481
BJ
502 upaddr->upwc = -bp->b_bcount / sizeof (short);
503 if (bp->b_flags & B_READ)
2601dfbd 504 cmd = UP_IE|UP_RCOM|UP_GO;
008c0481 505 else
2601dfbd 506 cmd = UP_IE|UP_WCOM|UP_GO;
b7333467 507 um->um_cmd = cmd;
a0eab615 508 (void) ubago(ui);
eb891eaa 509 return (1);
008c0481
BJ
510}
511
736772ef
BJ
512/*
513 * Now all ready to go, stuff the registers.
514 */
b7333467 515updgo(um)
89bd2f01 516 struct uba_ctlr *um;
3f3a34c3 517{
2601dfbd 518 register struct updevice *upaddr = (struct updevice *)um->um_addr;
7e00c42b 519
de59ccc2 520 um->um_tab.b_active = 2; /* should now be 2 */
b7333467
BJ
521 upaddr->upba = um->um_ubinfo;
522 upaddr->upcs1 = um->um_cmd|((um->um_ubinfo>>8)&0x300);
3f3a34c3
BJ
523}
524
736772ef
BJ
525/*
526 * Handle a disk interrupt.
527 */
443c8066 528upintr(sc21)
3f3a34c3 529 register sc21;
008c0481
BJ
530{
531 register struct buf *bp, *dp;
89bd2f01
BJ
532 register struct uba_ctlr *um = upminfo[sc21];
533 register struct uba_device *ui;
2601dfbd 534 register struct updevice *upaddr = (struct updevice *)um->um_addr;
008c0481 535 register unit;
7e00c42b 536 struct up_softc *sc = &up_softc[um->um_ctlr];
71236e46 537 int as = (upaddr->upas & 0377) | sc->sc_softas;
f88f8fdb 538 int needie = 1, waitdry;
008c0481 539
7e00c42b 540 sc->sc_wticks = 0;
71236e46 541 sc->sc_softas = 0;
736772ef
BJ
542 /*
543 * If controller wasn't transferring, then this is an
544 * interrupt for attention status on seeking drives.
545 * Just service them.
546 */
cc7ff771 547 if (um->um_tab.b_active != 2 && !sc->sc_recal) {
736772ef
BJ
548 if (upaddr->upcs1 & UP_TRE)
549 upaddr->upcs1 = UP_TRE;
550 goto doattn;
551 }
de59ccc2 552 um->um_tab.b_active = 1;
7ed3da3d 553 um->um_tab.b_active = 1;
736772ef
BJ
554 /*
555 * Get device and block structures, and a pointer
89bd2f01 556 * to the uba_device for the drive. Select the drive.
736772ef
BJ
557 */
558 dp = um->um_tab.b_actf;
559 bp = dp->b_actf;
560 ui = updinfo[dkunit(bp)];
561 dk_busy &= ~(1 << ui->ui_dk);
562 if ((upaddr->upcs2&07) != ui->ui_slave)
3f3a34c3 563 upaddr->upcs2 = ui->ui_slave;
3b186c1c
SL
564#ifndef NOBADSECT
565 if (bp->b_flags&B_BAD) {
566 if (upecc(ui, CONT))
567 return;
568 }
569#endif
7ed3da3d
BJ
570#ifndef NOBADSECT
571 if (bp->b_flags&B_BAD) {
572 if (upecc(ui, CONT))
573 return;
574 }
575#endif
736772ef
BJ
576 /*
577 * Check for and process errors on
578 * either the drive or the controller.
579 */
299d67ed 580 if ((upaddr->upds&UPDS_ERR) || (upaddr->upcs1&UP_TRE)) {
f88f8fdb 581 waitdry = 0;
299d67ed 582 while ((upaddr->upds & UPDS_DRY) == 0) {
f88f8fdb
BJ
583 if (++waitdry > 512)
584 break;
585 upwaitdry++;
586 }
299d67ed 587 if (upaddr->uper1&UPER1_WLE) {
736772ef
BJ
588 /*
589 * Give up on write locked devices
590 * immediately.
591 */
f6d201ff 592 printf("up%d: write locked\n", dkunit(bp));
736772ef
BJ
593 bp->b_flags |= B_ERROR;
594 } else if (++um->um_tab.b_errcnt > 27) {
595 /*
596 * After 28 retries (16 without offset, and
597 * 12 with offset positioning) give up.
598 */
3b186c1c 599 hard:
7ed3da3d 600 hard:
f6d201ff 601 harderr(bp, "up");
7ed3da3d
BJ
602 printf("cn=%d tn=%d sn=%d cs2=%b er1=%b er2=%b\n",
603 upaddr->updc, ((upaddr->upda)>>8)&077,
604 (upaddr->upda)&037,
605 upaddr->upcs2, UPCS2_BITS,
606 upaddr->uper1, UPER1_BITS,
607 upaddr->uper2, UPER2_BITS);
736772ef 608 bp->b_flags |= B_ERROR;
7ed3da3d
BJ
609 } else if (upaddr->uper2 & UPER2_BSE) {
610#ifndef NOBADSECT
611 if (upecc(ui, BSE))
612 return;
613 else
3b186c1c
SL
614#endif
615 goto hard;
616 } else if (upaddr->uper2 & UPER2_BSE) {
617#ifndef NOBADSECT
618 if (upecc(ui, BSE))
619 return;
620 else
7ed3da3d
BJ
621#endif
622 goto hard;
736772ef
BJ
623 } else {
624 /*
625 * Retriable error.
626 * If a soft ecc, correct it (continuing
627 * by returning if necessary.
628 * Otherwise fall through and retry the transfer
629 */
55de6ac9 630 if ((upaddr->uper1&(UPER1_DCK|UPER1_ECH))==UPER1_DCK) {
7ed3da3d 631 if (upecc(ui, ECC))
2601dfbd 632 return;
55de6ac9
BJ
633 } else
634 um->um_tab.b_active = 0; /* force retry */
7ed3da3d
BJ
635 } else
636 um->um_tab.b_active = 0; /* force retry */
736772ef
BJ
637 }
638 /*
639 * Clear drive error and, every eight attempts,
640 * (starting with the fourth)
641 * recalibrate to clear the slate.
642 */
643 upaddr->upcs1 = UP_TRE|UP_IE|UP_DCLR|UP_GO;
644 needie = 0;
fc4d0a69 645 if ((um->um_tab.b_errcnt&07) == 4 && um->um_tab.b_active == 0) {
736772ef 646 upaddr->upcs1 = UP_RECAL|UP_IE|UP_GO;
a6442a2f
BJ
647 sc->sc_recal = 0;
648 goto nextrecal;
736772ef
BJ
649 }
650 }
651 /*
a6442a2f
BJ
652 * Advance recalibration finite state machine
653 * if recalibrate in progress, through
654 * RECAL
655 * SEEK
656 * OFFSET (optional)
657 * RETRY
736772ef 658 */
a6442a2f
BJ
659 switch (sc->sc_recal) {
660
661 case 1:
662 upaddr->updc = bp->b_cylin;
663 upaddr->upcs1 = UP_SEEK|UP_IE|UP_GO;
664 goto nextrecal;
665 case 2:
666 if (um->um_tab.b_errcnt < 16 || (bp->b_flags&B_READ) == 0)
667 goto donerecal;
299d67ed 668 upaddr->upof = up_offset[um->um_tab.b_errcnt & 017] | UPOF_FMT22;
a6442a2f
BJ
669 upaddr->upcs1 = UP_IE|UP_OFFSET|UP_GO;
670 goto nextrecal;
671 nextrecal:
672 sc->sc_recal++;
673 um->um_tab.b_active = 1;
674 return;
675 donerecal:
676 case 3:
736772ef 677 sc->sc_recal = 0;
a6442a2f
BJ
678 um->um_tab.b_active = 0;
679 break;
736772ef
BJ
680 }
681 /*
682 * If still ``active'', then don't need any more retries.
683 */
684 if (um->um_tab.b_active) {
685 /*
686 * If we were offset positioning,
687 * return to centerline.
688 */
689 if (um->um_tab.b_errcnt >= 16) {
299d67ed 690 upaddr->upof = UPOF_FMT22;
736772ef 691 upaddr->upcs1 = UP_RTC|UP_GO|UP_IE;
299d67ed 692 while (upaddr->upds & UPDS_PIP)
736772ef 693 DELAY(25);
eb891eaa 694 needie = 0;
008c0481 695 }
736772ef
BJ
696 um->um_tab.b_active = 0;
697 um->um_tab.b_errcnt = 0;
698 um->um_tab.b_actf = dp->b_forw;
699 dp->b_active = 0;
700 dp->b_errcnt = 0;
701 dp->b_actf = bp->av_forw;
702 bp->b_resid = (-upaddr->upwc * sizeof(short));
703 iodone(bp);
704 /*
705 * If this unit has more work to do,
706 * then start it up right away.
707 */
708 if (dp->b_actf)
709 if (upustart(ui))
eb891eaa 710 needie = 0;
008c0481 711 }
736772ef 712 as &= ~(1<<ui->ui_slave);
8c58f40c
BJ
713 /*
714 * Release unibus resources and flush data paths.
715 */
716 ubadone(um);
736772ef
BJ
717doattn:
718 /*
719 * Process other units which need attention.
720 * For each unit which needs attention, call
721 * the unit start routine to place the slave
722 * on the controller device queue.
723 */
a6442a2f
BJ
724 while (unit = ffs(as)) {
725 unit--; /* was 1 origin */
726 as &= ~(1<<unit);
727 upaddr->upas = 1<<unit;
c10bcf0a 728 if (unit < UPIPUNITS && upustart(upip[sc21][unit]))
a6442a2f
BJ
729 needie = 0;
730 }
736772ef
BJ
731 /*
732 * If the controller is not transferring, but
733 * there are devices ready to transfer, start
734 * the controller.
735 */
3f3a34c3
BJ
736 if (um->um_tab.b_actf && um->um_tab.b_active == 0)
737 if (upstart(um))
eb891eaa 738 needie = 0;
2a3b9a7f 739 if (needie)
2601dfbd 740 upaddr->upcs1 = UP_IE;
008c0481
BJ
741}
742
740e4029 743upread(dev, uio)
0801d37f 744 dev_t dev;
740e4029 745 struct uio *uio;
008c0481 746{
0801d37f 747 register int unit = minor(dev) >> 3;
7e00c42b 748
0801d37f 749 if (unit >= NUP)
0cd5eac7
BJ
750 return (ENXIO);
751 return (physio(upstrategy, &rupbuf[unit], dev, B_READ, minphys, uio));
008c0481
BJ
752}
753
002227dd 754upwrite(dev, uio)
0801d37f 755 dev_t dev;
002227dd 756 struct uio *uio;
008c0481 757{
0801d37f 758 register int unit = minor(dev) >> 3;
7e00c42b 759
0801d37f 760 if (unit >= NUP)
0cd5eac7
BJ
761 return (ENXIO);
762 return (physio(upstrategy, &rupbuf[unit], dev, B_WRITE, minphys, uio));
008c0481
BJ
763}
764
7bc8d985
BJ
765/*
766 * Correct an ECC error, and restart the i/o to complete
767 * the transfer if necessary. This is quite complicated because
768 * the transfer may be going to an odd memory address base and/or
769 * across a page boundary.
770 */
7ed3da3d 771upecc(ui, flag)
89bd2f01 772 register struct uba_device *ui;
7ed3da3d 773 int flag;
3b186c1c 774 int flag;
008c0481 775{
2601dfbd 776 register struct updevice *up = (struct updevice *)ui->ui_addr;
3f3a34c3 777 register struct buf *bp = uputab[ui->ui_unit].b_actf;
89bd2f01 778 register struct uba_ctlr *um = ui->ui_mi;
3f3a34c3
BJ
779 register struct upst *st;
780 struct uba_regs *ubp = ui->ui_hd->uh_uba;
7bc8d985 781 register int i;
008c0481 782 caddr_t addr;
7bc8d985 783 int reg, bit, byte, npf, mask, o, cmd, ubaddr;
008c0481
BJ
784 int bn, cn, tn, sn;
785
008c0481 786 /*
7bc8d985
BJ
787 * Npf is the number of sectors transferred before the sector
788 * containing the ECC error, and reg is the UBA register
789 * mapping (the first part of) the transfer.
790 * O is offset within a memory page of the first byte transferred.
008c0481 791 */
7ed3da3d
BJ
792#ifndef NOBADSECT
793 if (flag == CONT)
794 npf = bp->b_error;
795 else
796#endif
797 npf = btop((up->upwc * sizeof(short)) + bp->b_bcount);
b7333467 798 reg = btop(um->um_ubinfo&0x3ffff) + npf;
008c0481 799 o = (int)bp->b_un.b_addr & PGOFSET;
008c0481 800 mask = up->upec2;
299d67ed 801#ifdef UPECCDEBUG
8c58f40c
BJ
802 printf("npf %d reg %x o %d mask %o pos %d\n", npf, reg, o, mask,
803 up->upec1);
299d67ed 804#endif
7ed3da3d
BJ
805 bn = dkblock(bp);
806 st = &upst[ui->ui_type];
807 cn = bp->b_cylin;
808 sn = bn%st->nspc + npf;
809 tn = sn/st->nsect;
810 sn %= st->nsect;
811 cn += tn/st->ntrak;
812 tn %= st->ntrak;
060afaf6 813 ubapurge(um);
7ed3da3d 814 um->um_tab.b_active=2;
7bc8d985 815 /*
7ed3da3d 816 * action taken depends on the flag
7bc8d985 817 */
7ed3da3d
BJ
818 switch(flag){
819 case ECC:
820 npf--;
821 reg--;
822 mask = up->upec2;
823 printf("up%d%c: soft ecc sn%d\n", dkunit(bp),
824 'a'+(minor(bp->b_dev)&07), bp->b_blkno + npf);
825 /*
826 * Flush the buffered data path, and compute the
827 * byte and bit position of the error. The variable i
828 * is the byte offset in the transfer, the variable byte
829 * is the offset from a page boundary in main memory.
830 */
831 i = up->upec1 - 1; /* -1 makes 0 origin */
832 bit = i&07;
833 i = (i&~07)>>3;
834 byte = i + o;
835 /*
836 * Correct while possible bits remain of mask. Since mask
837 * contains 11 bits, we continue while the bit offset is > -11.
838 * Also watch out for end of this block and the end of the whole
839 * transfer.
840 */
841 while (i < 512 && (int)ptob(npf)+i < bp->b_bcount && bit > -11) {
842 addr = ptob(ubp->uba_map[reg+btop(byte)].pg_pfnum)+
843 (byte & PGOFSET);
844#ifdef UPECCDEBUG
845 printf("addr %x map reg %x\n",
846 addr, *(int *)(&ubp->uba_map[reg+btop(byte)]));
847 printf("old: %x, ", getmemc(addr));
848#endif
849 putmemc(addr, getmemc(addr)^(mask<<bit));
299d67ed 850#ifdef UPECCDEBUG
7ed3da3d 851 printf("new: %x\n", getmemc(addr));
299d67ed 852#endif
7ed3da3d
BJ
853 byte++;
854 i++;
855 }
856 if (up->upwc == 0)
857 return (0);
858 npf++;
859 reg++;
860 break;
861#ifndef NOBADSECT
862 case BSE:
863 /*
864 * if not in bad sector table, return 0
865 */
866 if ((bn = isbad(&upbad[ui->ui_unit], cn, tn, sn)) < 0)
867 return(0);
868 /*
869 * flag this one as bad
870 */
871 bp->b_flags |= B_BAD;
872 bp->b_error = npf + 1;
299d67ed 873#ifdef UPECCDEBUG
7ed3da3d
BJ
874 printf("BSE: restart at %d\n",npf+1);
875#endif
876 bn = st->ncyl * st->nspc -st->nsect - 1 - bn;
877 cn = bn / st->nspc;
878 sn = bn % st->nspc;
879 tn = sn / st->nsect;
880 sn %= st->nsect;
881 up->upwc = -(512 / sizeof (short));
882#ifdef UPECCDEBUG
883 printf("revector to cn %d tn %d sn %d\n", cn, tn, sn);
884#endif
885 break;
886 case CONT:
887#ifdef UPECCDEBUG
888 printf("upecc, CONT: bn %d cn %d tn %d sn %d\n", bn, cn, tn, sn);
889#endif
890 bp->b_flags &= ~B_BAD;
891 up->upwc = -((bp->b_bcount - (int)ptob(npf)) / sizeof(short));
892 if (up->upwc == 0)
893 return(0);
894 break;
299d67ed 895#endif
008c0481 896 }
55de6ac9
BJ
897 if (up->upwc == 0) {
898 um->um_tab.b_active = 0;
008c0481 899 return (0);
55de6ac9 900 }
7ed3da3d 901 }
7bc8d985
BJ
902 /*
903 * Have to continue the transfer... clear the drive,
904 * and compute the position where the transfer is to continue.
905 * We have completed npf+1 sectors of the transfer already;
906 * restart at offset o of next sector (i.e. in UBA register reg+1).
907 */
2601dfbd
BJ
908#ifdef notdef
909 up->uper1 = 0;
910 up->upcs1 |= UP_GO;
911#else
912 up->upcs1 = UP_TRE|UP_IE|UP_DCLR|UP_GO;
008c0481 913 up->updc = cn;
7bc8d985 914 up->upda = (tn << 8) | sn;
7ed3da3d 915 ubaddr = (int)ptob(reg) + o;
7bc8d985
BJ
916 up->upba = ubaddr;
917 cmd = (ubaddr >> 8) & 0x300;
7ed3da3d
BJ
918 cmd |= ((bp->b_flags&B_READ)?UP_RCOM:UP_WCOM)|UP_IE|UP_GO;
919 um->um_tab.b_errcnt = 0;
7bc8d985 920 up->upcs1 = cmd;
2601dfbd 921#endif
008c0481
BJ
922 return (1);
923}
977c2848
BJ
924
925/*
926 * Reset driver after UBA init.
927 * Cancel software state of all pending transfers
928 * and restart all units and the controller.
929 */
3f3a34c3 930upreset(uban)
f6d201ff 931 int uban;
977c2848 932{
89bd2f01
BJ
933 register struct uba_ctlr *um;
934 register struct uba_device *ui;
3f3a34c3
BJ
935 register sc21, unit;
936
a3cb8f60 937 for (sc21 = 0; sc21 < NSC; sc21++) {
7e00c42b
BJ
938 if ((um = upminfo[sc21]) == 0 || um->um_ubanum != uban ||
939 um->um_alive == 0)
3f3a34c3 940 continue;
f6d201ff 941 printf(" sc%d", sc21);
3f3a34c3
BJ
942 um->um_tab.b_active = 0;
943 um->um_tab.b_actf = um->um_tab.b_actl = 0;
f6d201ff 944 up_softc[sc21].sc_recal = 0;
cc7ff771 945 up_softc[sc21].sc_wticks = 0;
b7333467
BJ
946 if (um->um_ubinfo) {
947 printf("<%d>", (um->um_ubinfo>>28)&0xf);
96b997eb 948 um->um_ubinfo = 0;
3f3a34c3 949 }
299d67ed 950 ((struct updevice *)(um->um_addr))->upcs2 = UPCS2_CLR;
3f3a34c3
BJ
951 for (unit = 0; unit < NUP; unit++) {
952 if ((ui = updinfo[unit]) == 0)
953 continue;
f6d201ff 954 if (ui->ui_alive == 0 || ui->ui_mi != um)
3f3a34c3
BJ
955 continue;
956 uputab[unit].b_active = 0;
957 (void) upustart(ui);
958 }
959 (void) upstart(um);
977c2848 960 }
977c2848 961}
6a81870e
BJ
962
963/*
964 * Wake up every second and if an interrupt is pending
965 * but nothing has happened increment a counter.
f6d201ff 966 * If nothing happens for 20 seconds, reset the UNIBUS
6a81870e
BJ
967 * and begin anew.
968 */
969upwatch()
970{
89bd2f01 971 register struct uba_ctlr *um;
3f3a34c3 972 register sc21, unit;
7e00c42b 973 register struct up_softc *sc;
6a81870e 974
7780575a 975 timeout(upwatch, (caddr_t)0, hz);
a3cb8f60 976 for (sc21 = 0; sc21 < NSC; sc21++) {
3f3a34c3 977 um = upminfo[sc21];
7e00c42b
BJ
978 if (um == 0 || um->um_alive == 0)
979 continue;
980 sc = &up_softc[sc21];
3f3a34c3
BJ
981 if (um->um_tab.b_active == 0) {
982 for (unit = 0; unit < NUP; unit++)
2601dfbd
BJ
983 if (uputab[unit].b_active &&
984 updinfo[unit]->ui_mi == um)
3f3a34c3 985 goto active;
7e00c42b 986 sc->sc_wticks = 0;
3f3a34c3
BJ
987 continue;
988 }
f6d201ff 989active:
7e00c42b
BJ
990 sc->sc_wticks++;
991 if (sc->sc_wticks >= 20) {
992 sc->sc_wticks = 0;
f6d201ff 993 printf("sc%d: lost interrupt\n", sc21);
a3cb8f60 994 ubareset(um->um_ubanum);
3f3a34c3 995 }
6a81870e
BJ
996 }
997}
0ff318b2
BJ
998
999#define DBSIZE 20
1000
1001updump(dev)
1002 dev_t dev;
1003{
2601dfbd 1004 struct updevice *upaddr;
0ff318b2 1005 char *start;
a0eab615 1006 int num, blk, unit;
0ff318b2 1007 struct size *sizes;
3f3a34c3 1008 register struct uba_regs *uba;
89bd2f01 1009 register struct uba_device *ui;
0ff318b2 1010 register short *rp;
3f3a34c3 1011 struct upst *st;
cd8ce595 1012 register int retry;
7ed3da3d 1013 register int retry;
0ff318b2 1014
0ff318b2 1015 unit = minor(dev) >> 3;
0c48c799
BJ
1016 if (unit >= NUP)
1017 return (ENXIO);
7e00c42b 1018#define phys(cast, addr) ((cast)((int)addr & 0x7fffffff))
89bd2f01 1019 ui = phys(struct uba_device *, updinfo[unit]);
0c48c799
BJ
1020 if (ui->ui_alive == 0)
1021 return (ENXIO);
3f3a34c3 1022 uba = phys(struct uba_hd *, ui->ui_hd)->uh_physuba;
89bd2f01 1023 ubainit(uba);
2601dfbd 1024 upaddr = (struct updevice *)ui->ui_physaddr;
cd8ce595 1025 DELAY(5000000);
3f3a34c3 1026 num = maxfree;
0ff318b2 1027 upaddr->upcs2 = unit;
89bd2f01 1028 DELAY(100);
cd8ce595
SL
1029 upaddr->upcs1 = UP_DCLR|UP_GO;
1030 upaddr->upcs1 = UP_PRESET|UP_GO;
1031 upaddr->upof = UPOF_FMT22;
1032 retry = 0;
1033 do {
1034 DELAY(25);
1035 if (++retry > 527)
1036 break;
0599cbf1 1037 } while ((upaddr->upds & UP_RDY) == 0);
299d67ed 1038 if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY)
0c48c799 1039 return (EFAULT);
cd8ce595 1040 start = 0;
7e00c42b 1041 st = &upst[ui->ui_type];
7ed3da3d 1042 start = 0;
3f3a34c3 1043 sizes = phys(struct size *, st->sizes);
0c48c799
BJ
1044 if (dumplo < 0 || dumplo + num >= sizes[minor(dev)&07].nblocks)
1045 return (EINVAL);
0ff318b2
BJ
1046 while (num > 0) {
1047 register struct pte *io;
1048 register int i;
1049 int cn, sn, tn;
1050 daddr_t bn;
1051
1052 blk = num > DBSIZE ? DBSIZE : num;
3f3a34c3 1053 io = uba->uba_map;
0ff318b2 1054 for (i = 0; i < blk; i++)
89bd2f01 1055 *(int *)io++ = (btop(start)+i) | (1<<21) | UBAMR_MRV;
0ff318b2
BJ
1056 *(int *)io = 0;
1057 bn = dumplo + btop(start);
71236e46
BJ
1058 cn = bn/st->nspc + sizes[minor(dev)&07].cyloff;
1059 sn = bn%st->nspc;
1060 tn = sn/st->nsect;
1061 sn = sn%st->nsect;
0ff318b2
BJ
1062 upaddr->updc = cn;
1063 rp = (short *) &upaddr->upda;
1064 *rp = (tn << 8) + sn;
1065 *--rp = 0;
1066 *--rp = -blk*NBPG / sizeof (short);
2601dfbd 1067 *--rp = UP_GO|UP_WCOM;
cd8ce595 1068 retry = 0;
7ed3da3d 1069 retry = 0;
0ff318b2
BJ
1070 do {
1071 DELAY(25);
cd8ce595
SL
1072 if (++retry > 527)
1073 break;
7ed3da3d
BJ
1074 if (++retry > 527)
1075 break;
2601dfbd 1076 } while ((upaddr->upcs1 & UP_RDY) == 0);
cd8ce595 1077 if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY) {
0599cbf1 1078 printf("up%d: not ready", unit);
cd8ce595
SL
1079 if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY) {
1080 printf("\n");
1081 return (EIO);
1082 }
1083 printf(" (flakey)\n");
1084 }
7ed3da3d
BJ
1085 if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY) {
1086 printf("up%d: not ready", unit);
1087 if ((upaddr->upds & UPDS_DREADY) != UPDS_DREADY) {
1088 printf("\n");
1089 return (EIO);
1090 }
1091 printf(" (flakey)\n");
1092 }
299d67ed 1093 if (upaddr->upds&UPDS_ERR)
0c48c799 1094 return (EIO);
0ff318b2
BJ
1095 start += blk*NBPG;
1096 num -= blk;
1097 }
0ff318b2
BJ
1098 return (0);
1099}
63c35a63 1100#endif