clean up conditional machinecheck stuff
[unix-history] / usr / src / sys / vax / uba / up.c
CommitLineData
443c8066 1/* up.c 4.24 81/02/26 */
008c0481 2
66b4fb09 3#include "up.h"
a3cb8f60 4#if NSC > 0
008c0481 5/*
10fb932f 6 * UNIBUS disk driver with overlapped seeks and ECC recovery.
008c0481 7 */
355250d9 8#define DELAY(N) { register int d; d = N; while (--d > 0); }
008c0481
BJ
9
10#include "../h/param.h"
11#include "../h/systm.h"
3f3a34c3
BJ
12#include "../h/cpu.h"
13#include "../h/nexus.h"
41888f16 14#include "../h/dk.h"
008c0481
BJ
15#include "../h/buf.h"
16#include "../h/conf.h"
17#include "../h/dir.h"
18#include "../h/user.h"
19#include "../h/map.h"
80e7c811 20#include "../h/pte.h"
008c0481
BJ
21#include "../h/mba.h"
22#include "../h/mtpr.h"
008c0481 23#include "../h/vm.h"
b7333467 24#include "../h/uba.h"
0ff318b2 25#include "../h/cmap.h"
008c0481 26
0ff318b2 27#include "../h/upreg.h"
008c0481 28
3f3a34c3
BJ
29struct up_softc {
30 int sc_softas;
71236e46 31 int sc_ndrive;
3f3a34c3 32 int sc_wticks;
736772ef 33 int sc_recal;
a3cb8f60 34} up_softc[NSC];
008c0481 35
3f3a34c3 36/* THIS SHOULD BE READ OFF THE PACK, PER DRIVE */
008c0481
BJ
37struct size
38{
39 daddr_t nblocks;
40 int cyloff;
41} up_sizes[8] = {
42 15884, 0, /* A=cyl 0 thru 26 */
43 33440, 27, /* B=cyl 27 thru 81 */
d1778415 44 495520, 0, /* C=cyl 0 thru 814 */
008c0481
BJ
45 15884, 562, /* D=cyl 562 thru 588 */
46 55936, 589, /* E=cyl 589 thru 680 */
47 81472, 681, /* F=cyl 681 thru 814 */
48 153824, 562, /* G=cyl 562 thru 814 */
008c0481 49 291346, 82, /* H=cyl 82 thru 561 */
3f3a34c3
BJ
50}, fj_sizes[8] = {
51 15884, 0, /* A=cyl 0 thru 49 */
52 33440, 50, /* B=cyl 50 thru 154 */
53 263360, 0, /* C=cyl 0 thru 822 */
54 0, 0,
55 0, 0,
56 0, 0,
57 0, 0,
58 213760, 155, /* H=cyl 155 thru 822 */
008c0481 59};
3f3a34c3 60/* END OF STUFF WHICH SHOULD BE READ IN PER DISK */
008c0481 61
3f3a34c3
BJ
62#define _upSDIST 2 /* 1.0 msec */
63#define _upRDIST 4 /* 2.0 msec */
64
65int upSDIST = _upSDIST;
66int upRDIST = _upRDIST;
67
71236e46 68int upprobe(), upslave(), upattach(), updgo(), upintr();
a3cb8f60 69struct uba_minfo *upminfo[NSC];
3f3a34c3 70struct uba_dinfo *updinfo[NUP];
a3cb8f60 71struct uba_dinfo *upip[NSC][4];
d763a2b7 72
71236e46 73u_short upstd[] = { 0776700, 0774400, 0776300, 0 };
0801d37f 74struct uba_driver scdriver =
71236e46 75 { upprobe, upslave, upattach, updgo, upstd, "up", updinfo, "sc", upminfo };
3f3a34c3
BJ
76struct buf uputab[NUP];
77
78struct upst {
79 short nsect;
80 short ntrak;
81 short nspc;
82 short ncyl;
83 struct size *sizes;
84} upst[] = {
71236e46
BJ
85 32, 19, 32*19, 823, up_sizes, /* 9300/cdc */
86/* 9300 actually has 815 cylinders... */
3f3a34c3
BJ
87 32, 10, 32*10, 823, fj_sizes, /* fujitsu 160m */
88};
008c0481 89
2601dfbd
BJ
90u_char up_offset[16] = {
91 UP_P400, UP_M400, UP_P400, UP_M400, UP_P800, UP_M800, UP_P800, UP_M800,
92 UP_P1200, UP_M1200, UP_P1200, UP_M1200, 0, 0, 0, 0
93};
008c0481 94
0801d37f 95struct buf rupbuf[NUP];
008c0481 96
008c0481
BJ
97#define b_cylin b_resid
98
008c0481
BJ
99#ifdef INTRLVE
100daddr_t dkblock();
101#endif
3f3a34c3
BJ
102
103int upwstart, upwatch(); /* Have started guardian */
7e00c42b 104int upseek;
f88f8fdb 105int upwaitdry;
3f3a34c3
BJ
106
107/*ARGSUSED*/
71236e46 108upprobe(reg)
3f3a34c3 109 caddr_t reg;
008c0481 110{
d763a2b7
BJ
111 register int br, cvec;
112
71236e46
BJ
113#ifdef lint
114 br = 0; cvec = br; br = cvec;
115#endif
2601dfbd 116 ((struct updevice *)reg)->upcs1 = UP_IE|UP_RDY;
71236e46 117 DELAY(10);
2601dfbd 118 ((struct updevice *)reg)->upcs1 = 0;
d763a2b7 119 return (1);
3f3a34c3
BJ
120}
121
71236e46 122upslave(ui, reg)
3f3a34c3
BJ
123 struct uba_dinfo *ui;
124 caddr_t reg;
125{
2601dfbd 126 register struct updevice *upaddr = (struct updevice *)reg;
3f3a34c3
BJ
127
128 upaddr->upcs1 = 0; /* conservative */
71236e46 129 upaddr->upcs2 = ui->ui_slave;
2601dfbd
BJ
130 if (upaddr->upcs2&UP_NED) {
131 upaddr->upcs1 = UP_DCLR|UP_GO;
3f3a34c3
BJ
132 return (0);
133 }
71236e46
BJ
134 return (1);
135}
136
137upattach(ui)
138 register struct uba_dinfo *ui;
139{
2601dfbd
BJ
140#ifdef notdef
141 register struct updevice *upaddr;
142#endif
71236e46 143
6a81870e 144 if (upwstart == 0) {
49c84d3f 145 timeout(upwatch, (caddr_t)0, HZ);
6a81870e
BJ
146 upwstart++;
147 }
b7333467
BJ
148 if (ui->ui_dk >= 0)
149 dk_mspw[ui->ui_dk] = .0000020345;
71236e46
BJ
150 upip[ui->ui_ctlr][ui->ui_slave] = ui;
151 up_softc[ui->ui_ctlr].sc_ndrive++;
2601dfbd
BJ
152#ifdef notdef
153 upaddr = (struct updevice *)ui->ui_addr;
154 upaddr->upcs1 = 0;
155 upaddr->upcs2 = ui->ui_slave;
156 upaddr->uphr = -1;
157 /* ... */
158 if (upaddr-> ... == 10)
159 ui->ui_type = 1;
160#endif
3f3a34c3
BJ
161}
162
3f3a34c3
BJ
163upstrategy(bp)
164 register struct buf *bp;
165{
166 register struct uba_dinfo *ui;
3f3a34c3
BJ
167 register struct upst *st;
168 register int unit;
7e00c42b 169 register struct buf *dp;
3f3a34c3 170 int xunit = minor(bp->b_dev) & 07;
7e00c42b 171 long bn, sz;
3f3a34c3 172
7e00c42b 173 sz = (bp->b_bcount+511) >> 9;
008c0481 174 unit = dkunit(bp);
3f3a34c3
BJ
175 if (unit >= NUP)
176 goto bad;
177 ui = updinfo[unit];
178 if (ui == 0 || ui->ui_alive == 0)
179 goto bad;
180 st = &upst[ui->ui_type];
181 if (bp->b_blkno < 0 ||
182 (bn = dkblock(bp))+sz > st->sizes[xunit].nblocks)
183 goto bad;
184 bp->b_cylin = bn/st->nspc + st->sizes[xunit].cyloff;
008c0481 185 (void) spl5();
7e00c42b
BJ
186 dp = &uputab[ui->ui_unit];
187 disksort(dp, bp);
188 if (dp->b_active == 0) {
3f3a34c3
BJ
189 (void) upustart(ui);
190 bp = &ui->ui_mi->um_tab;
191 if (bp->b_actf && bp->b_active == 0)
192 (void) upstart(ui->ui_mi);
008c0481
BJ
193 }
194 (void) spl0();
3f3a34c3
BJ
195 return;
196
197bad:
198 bp->b_flags |= B_ERROR;
199 iodone(bp);
200 return;
008c0481
BJ
201}
202
736772ef
BJ
203/*
204 * Unit start routine.
205 * Seek the drive to be where the data is
206 * and then generate another interrupt
207 * to actually start the transfer.
208 * If there is only one drive on the controller,
209 * or we are very close to the data, don't
210 * bother with the search. If called after
211 * searching once, don't bother to look where
212 * we are, just queue for transfer (to avoid
213 * positioning forever without transferrring.)
214 */
3f3a34c3
BJ
215upustart(ui)
216 register struct uba_dinfo *ui;
008c0481
BJ
217{
218 register struct buf *bp, *dp;
736772ef 219 register struct uba_minfo *um = ui->ui_mi;
2601dfbd 220 register struct updevice *upaddr;
3f3a34c3 221 register struct upst *st;
008c0481 222 daddr_t bn;
736772ef 223 int sn, csn;
71236e46
BJ
224 /*
225 * The SC21 cancels commands if you just say
2601dfbd 226 * cs1 = UP_IE
71236e46
BJ
227 * so we are cautious about handling of cs1.
228 * Also don't bother to clear as bits other than in upintr().
229 */
736772ef
BJ
230 int didie = 0;
231
232 if (ui == 0)
233 return (0);
3f3a34c3
BJ
234 dk_busy &= ~(1<<ui->ui_dk);
235 dp = &uputab[ui->ui_unit];
7bc8d985 236 if ((bp = dp->b_actf) == NULL)
eb891eaa 237 goto out;
736772ef
BJ
238 /*
239 * If the controller is active, just remember
240 * that this device would like to be positioned...
241 * if we tried to position now we would confuse the SC21.
242 */
3f3a34c3 243 if (um->um_tab.b_active) {
d763a2b7 244 up_softc[um->um_ctlr].sc_softas |= 1<<ui->ui_slave;
2a3b9a7f
BJ
245 return (0);
246 }
736772ef
BJ
247 /*
248 * If we have already positioned this drive,
249 * then just put it on the ready queue.
250 */
a3f430e0
BJ
251 if (dp->b_active)
252 goto done;
253 dp->b_active = 1;
2601dfbd 254 upaddr = (struct updevice *)um->um_addr;
3f3a34c3 255 upaddr->upcs2 = ui->ui_slave;
736772ef
BJ
256 /*
257 * If drive has just come up,
258 * setup the pack.
259 */
2601dfbd 260 if ((upaddr->upds & UP_VV) == 0) {
71236e46 261 /* SHOULD WARN SYSTEM THAT THIS HAPPENED */
2601dfbd
BJ
262 upaddr->upcs1 = UP_IE|UP_DCLR|UP_GO;
263 upaddr->upcs1 = UP_IE|UP_PRESET|UP_GO;
264 upaddr->upof = UP_FMT22;
eb891eaa 265 didie = 1;
008c0481 266 }
736772ef
BJ
267 /*
268 * If drive is offline, forget about positioning.
269 */
2601dfbd 270 if ((upaddr->upds & (UP_DPR|UP_MOL)) != (UP_DPR|UP_MOL))
2a3b9a7f 271 goto done;
736772ef
BJ
272 /*
273 * If there is only one drive,
274 * dont bother searching.
275 */
71236e46
BJ
276 if (up_softc[um->um_ctlr].sc_ndrive == 1)
277 goto done;
736772ef
BJ
278 /*
279 * Figure out where this transfer is going to
280 * and see if we are close enough to justify not searching.
281 */
3f3a34c3 282 st = &upst[ui->ui_type];
008c0481 283 bn = dkblock(bp);
3f3a34c3
BJ
284 sn = bn%st->nspc;
285 sn = (sn + st->nsect - upSDIST) % st->nsect;
736772ef 286 if (bp->b_cylin - upaddr->updc)
7bc8d985 287 goto search; /* Not on-cylinder */
2a3b9a7f
BJ
288 else if (upseek)
289 goto done; /* Ok just to be on-cylinder */
008c0481 290 csn = (upaddr->upla>>6) - sn - 1;
7bc8d985 291 if (csn < 0)
3f3a34c3
BJ
292 csn += st->nsect;
293 if (csn > st->nsect - upRDIST)
008c0481 294 goto done;
008c0481 295search:
736772ef
BJ
296 upaddr->updc = bp->b_cylin;
297 /*
298 * Not on cylinder at correct position,
299 * seek/search.
300 */
2a3b9a7f 301 if (upseek)
2601dfbd 302 upaddr->upcs1 = UP_IE|UP_SEEK|UP_GO;
7e00c42b 303 else {
2a3b9a7f 304 upaddr->upda = sn;
2601dfbd 305 upaddr->upcs1 = UP_IE|UP_SEARCH|UP_GO;
2a3b9a7f 306 }
eb891eaa 307 didie = 1;
736772ef
BJ
308 /*
309 * Mark unit busy for iostat.
310 */
3f3a34c3
BJ
311 if (ui->ui_dk >= 0) {
312 dk_busy |= 1<<ui->ui_dk;
313 dk_seek[ui->ui_dk]++;
008c0481 314 }
eb891eaa 315 goto out;
008c0481 316done:
736772ef
BJ
317 /*
318 * Device is ready to go.
319 * Put it on the ready queue for the controller
320 * (unless its already there.)
321 */
2601dfbd
BJ
322 if (dp->b_active != 2) {
323 dp->b_forw = NULL;
324 if (um->um_tab.b_actf == NULL)
325 um->um_tab.b_actf = dp;
326 else
327 um->um_tab.b_actl->b_forw = dp;
328 um->um_tab.b_actl = dp;
329 dp->b_active = 2;
330 }
eb891eaa
BJ
331out:
332 return (didie);
008c0481
BJ
333}
334
736772ef
BJ
335/*
336 * Start up a transfer on a drive.
337 */
3f3a34c3
BJ
338upstart(um)
339 register struct uba_minfo *um;
008c0481
BJ
340{
341 register struct buf *bp, *dp;
3f3a34c3 342 register struct uba_dinfo *ui;
2601dfbd 343 register struct updevice *upaddr;
7e00c42b 344 struct upst *st;
008c0481 345 daddr_t bn;
f88f8fdb 346 int dn, sn, tn, cmd, waitdry;
008c0481 347
008c0481 348loop:
736772ef
BJ
349 /*
350 * Pull a request off the controller queue
351 */
3f3a34c3 352 if ((dp = um->um_tab.b_actf) == NULL)
eb891eaa 353 return (0);
008c0481 354 if ((bp = dp->b_actf) == NULL) {
3f3a34c3 355 um->um_tab.b_actf = dp->b_forw;
008c0481
BJ
356 goto loop;
357 }
736772ef
BJ
358 /*
359 * Mark controller busy, and
360 * determine destination of this request.
361 */
3f3a34c3
BJ
362 um->um_tab.b_active++;
363 ui = updinfo[dkunit(bp)];
008c0481 364 bn = dkblock(bp);
3f3a34c3
BJ
365 dn = ui->ui_slave;
366 st = &upst[ui->ui_type];
367 sn = bn%st->nspc;
368 tn = sn/st->nsect;
369 sn %= st->nsect;
2601dfbd 370 upaddr = (struct updevice *)ui->ui_addr;
736772ef
BJ
371 /*
372 * Select drive if not selected already.
373 */
374 if ((upaddr->upcs2&07) != dn)
375 upaddr->upcs2 = dn;
376 /*
377 * Check that it is ready and online
378 */
f88f8fdb
BJ
379 waitdry = 0;
380 while ((upaddr->upds&UP_DRY) == 0) {
381 if (++waitdry > 512)
382 break;
383 upwaitdry++;
384 }
385 if ((upaddr->upds & UP_DREADY) != UP_DREADY) {
71236e46 386 printf("up%d not ready", dkunit(bp));
f88f8fdb 387 if ((upaddr->upds & UP_DREADY) != UP_DREADY) {
71236e46 388 printf("\n");
3f3a34c3
BJ
389 um->um_tab.b_active = 0;
390 um->um_tab.b_errcnt = 0;
88253fd2
BJ
391 dp->b_actf = bp->av_forw;
392 dp->b_active = 0;
393 bp->b_flags |= B_ERROR;
394 iodone(bp);
88253fd2
BJ
395 goto loop;
396 }
736772ef
BJ
397 /*
398 * Oh, well, sometimes this
399 * happens, for reasons unknown.
400 */
2601dfbd 401 printf(" (flakey)\n");
008c0481 402 }
736772ef
BJ
403 /*
404 * After 16th retry, do offset positioning
405 */
3f3a34c3 406 if (um->um_tab.b_errcnt >= 16 && (bp->b_flags&B_READ) != 0) {
2601dfbd
BJ
407 upaddr->upof = up_offset[um->um_tab.b_errcnt & 017] | UP_FMT22;
408 upaddr->upcs1 = UP_IE|UP_OFFSET|UP_GO;
409 while (upaddr->upds & UP_PIP)
008c0481
BJ
410 DELAY(25);
411 }
736772ef
BJ
412 /*
413 * Setup for the transfer, and get in the
414 * UNIBUS adaptor queue.
415 */
5aa9d5ea 416 upaddr->updc = bp->b_cylin;
008c0481 417 upaddr->upda = (tn << 8) + sn;
008c0481
BJ
418 upaddr->upwc = -bp->b_bcount / sizeof (short);
419 if (bp->b_flags & B_READ)
2601dfbd 420 cmd = UP_IE|UP_RCOM|UP_GO;
008c0481 421 else
2601dfbd 422 cmd = UP_IE|UP_WCOM|UP_GO;
b7333467
BJ
423 um->um_cmd = cmd;
424 ubago(ui);
eb891eaa 425 return (1);
008c0481
BJ
426}
427
736772ef
BJ
428/*
429 * Now all ready to go, stuff the registers.
430 */
b7333467
BJ
431updgo(um)
432 struct uba_minfo *um;
3f3a34c3 433{
2601dfbd 434 register struct updevice *upaddr = (struct updevice *)um->um_addr;
7e00c42b 435
b7333467
BJ
436 upaddr->upba = um->um_ubinfo;
437 upaddr->upcs1 = um->um_cmd|((um->um_ubinfo>>8)&0x300);
3f3a34c3
BJ
438}
439
736772ef
BJ
440/*
441 * Handle a disk interrupt.
442 */
443c8066 443upintr(sc21)
3f3a34c3 444 register sc21;
008c0481
BJ
445{
446 register struct buf *bp, *dp;
3f3a34c3
BJ
447 register struct uba_minfo *um = upminfo[sc21];
448 register struct uba_dinfo *ui;
2601dfbd 449 register struct updevice *upaddr = (struct updevice *)um->um_addr;
008c0481 450 register unit;
7e00c42b 451 struct up_softc *sc = &up_softc[um->um_ctlr];
71236e46 452 int as = (upaddr->upas & 0377) | sc->sc_softas;
f88f8fdb 453 int needie = 1, waitdry;
008c0481 454
7e00c42b 455 sc->sc_wticks = 0;
71236e46 456 sc->sc_softas = 0;
736772ef
BJ
457 /*
458 * If controller wasn't transferring, then this is an
459 * interrupt for attention status on seeking drives.
460 * Just service them.
461 */
462 if (um->um_tab.b_active == 0) {
463 if (upaddr->upcs1 & UP_TRE)
464 upaddr->upcs1 = UP_TRE;
465 goto doattn;
466 }
467 if ((upaddr->upcs1 & UP_RDY) == 0)
468 printf("upintr !RDY\n"); /* shouldn't happen */
469 /*
470 * Get device and block structures, and a pointer
471 * to the uba_dinfo for the drive. Select the drive.
472 */
473 dp = um->um_tab.b_actf;
474 bp = dp->b_actf;
475 ui = updinfo[dkunit(bp)];
476 dk_busy &= ~(1 << ui->ui_dk);
477 if ((upaddr->upcs2&07) != ui->ui_slave)
3f3a34c3 478 upaddr->upcs2 = ui->ui_slave;
736772ef
BJ
479 /*
480 * Check for and process errors on
481 * either the drive or the controller.
482 */
483 if ((upaddr->upds&UP_ERR) || (upaddr->upcs1&UP_TRE)) {
f88f8fdb
BJ
484 waitdry = 0;
485 while ((upaddr->upds & UP_DRY) == 0) {
486 if (++waitdry > 512)
487 break;
488 upwaitdry++;
489 }
490 if ((upaddr->upds&UP_DREADY) != UP_DREADY) {
491 printf("up%d not ready", dkunit(bp));
492 bp->b_flags |= B_ERROR;
493 } else if (upaddr->uper1&UP_WLE) {
736772ef
BJ
494 /*
495 * Give up on write locked devices
496 * immediately.
497 */
498 printf("up%d is write locked\n", dkunit(bp));
499 bp->b_flags |= B_ERROR;
500 } else if (++um->um_tab.b_errcnt > 27) {
501 /*
502 * After 28 retries (16 without offset, and
503 * 12 with offset positioning) give up.
504 */
505 if (upaddr->upcs2&(UP_NEM|UP_MXF)) {
506 printf("FLAKEY UP ");
507 ubareset(um->um_ubanum);
508 return;
3f3a34c3 509 }
736772ef
BJ
510 harderr(bp);
511 printf("up%d cs2 %b er1 %b er2 %b\n",
512 dkunit(bp), upaddr->upcs2, UPCS2_BITS, upaddr->uper1,
513 UPER1_BITS, upaddr->uper2, UPER2_BITS);
514 bp->b_flags |= B_ERROR;
515 } else {
516 /*
517 * Retriable error.
518 * If a soft ecc, correct it (continuing
519 * by returning if necessary.
520 * Otherwise fall through and retry the transfer
521 */
522 um->um_tab.b_active = 0; /* force retry */
2601dfbd
BJ
523 if ((upaddr->uper1&(UP_DCK|UP_ECH))==UP_DCK)
524 if (upecc(ui))
525 return;
736772ef
BJ
526 }
527 /*
528 * Clear drive error and, every eight attempts,
529 * (starting with the fourth)
530 * recalibrate to clear the slate.
531 */
532 upaddr->upcs1 = UP_TRE|UP_IE|UP_DCLR|UP_GO;
533 needie = 0;
534 if ((um->um_tab.b_errcnt&07) == 4) {
535 upaddr->upcs1 = UP_RECAL|UP_IE|UP_GO;
536 um->um_tab.b_active = 1;
537 sc->sc_recal = 1;
538 return;
539 }
540 }
541 /*
542 * Done retrying transfer... release
543 * resources... if we were recalibrating,
544 * then retry the transfer.
545 * Mathematical note: 28%8 != 4.
546 */
547 ubadone(um);
548 if (sc->sc_recal) {
549 sc->sc_recal = 0;
550 um->um_tab.b_active = 0; /* force retry */
551 }
552 /*
553 * If still ``active'', then don't need any more retries.
554 */
555 if (um->um_tab.b_active) {
556 /*
557 * If we were offset positioning,
558 * return to centerline.
559 */
560 if (um->um_tab.b_errcnt >= 16) {
561 upaddr->upof = UP_FMT22;
562 upaddr->upcs1 = UP_RTC|UP_GO|UP_IE;
563 while (upaddr->upds & UP_PIP)
564 DELAY(25);
eb891eaa 565 needie = 0;
008c0481 566 }
736772ef
BJ
567 um->um_tab.b_active = 0;
568 um->um_tab.b_errcnt = 0;
569 um->um_tab.b_actf = dp->b_forw;
570 dp->b_active = 0;
571 dp->b_errcnt = 0;
572 dp->b_actf = bp->av_forw;
573 bp->b_resid = (-upaddr->upwc * sizeof(short));
574 iodone(bp);
575 /*
576 * If this unit has more work to do,
577 * then start it up right away.
578 */
579 if (dp->b_actf)
580 if (upustart(ui))
eb891eaa 581 needie = 0;
008c0481 582 }
736772ef
BJ
583 as &= ~(1<<ui->ui_slave);
584doattn:
585 /*
586 * Process other units which need attention.
587 * For each unit which needs attention, call
588 * the unit start routine to place the slave
589 * on the controller device queue.
590 */
71236e46
BJ
591 for (unit = 0; as; as >>= 1, unit++)
592 if (as & 1) {
7e00c42b 593 upaddr->upas = 1<<unit;
71236e46 594 if (upustart(upip[sc21][unit]))
1f3d30ee
BJ
595 needie = 0;
596 }
736772ef
BJ
597 /*
598 * If the controller is not transferring, but
599 * there are devices ready to transfer, start
600 * the controller.
601 */
3f3a34c3
BJ
602 if (um->um_tab.b_actf && um->um_tab.b_active == 0)
603 if (upstart(um))
eb891eaa 604 needie = 0;
2a3b9a7f 605 if (needie)
2601dfbd 606 upaddr->upcs1 = UP_IE;
008c0481
BJ
607}
608
609upread(dev)
0801d37f 610 dev_t dev;
008c0481 611{
0801d37f 612 register int unit = minor(dev) >> 3;
7e00c42b 613
0801d37f
BJ
614 if (unit >= NUP)
615 u.u_error = ENXIO;
616 else
617 physio(upstrategy, &rupbuf[unit], dev, B_READ, minphys);
008c0481
BJ
618}
619
620upwrite(dev)
0801d37f 621 dev_t dev;
008c0481 622{
0801d37f 623 register int unit = minor(dev) >> 3;
7e00c42b 624
0801d37f
BJ
625 if (unit >= NUP)
626 u.u_error = ENXIO;
627 else
628 physio(upstrategy, &rupbuf[unit], dev, B_WRITE, minphys);
008c0481
BJ
629}
630
7bc8d985
BJ
631/*
632 * Correct an ECC error, and restart the i/o to complete
633 * the transfer if necessary. This is quite complicated because
634 * the transfer may be going to an odd memory address base and/or
635 * across a page boundary.
636 */
3f3a34c3
BJ
637upecc(ui)
638 register struct uba_dinfo *ui;
008c0481 639{
2601dfbd 640 register struct updevice *up = (struct updevice *)ui->ui_addr;
3f3a34c3
BJ
641 register struct buf *bp = uputab[ui->ui_unit].b_actf;
642 register struct uba_minfo *um = ui->ui_mi;
643 register struct upst *st;
644 struct uba_regs *ubp = ui->ui_hd->uh_uba;
7bc8d985 645 register int i;
008c0481 646 caddr_t addr;
7bc8d985 647 int reg, bit, byte, npf, mask, o, cmd, ubaddr;
008c0481
BJ
648 int bn, cn, tn, sn;
649
008c0481 650 /*
7bc8d985
BJ
651 * Npf is the number of sectors transferred before the sector
652 * containing the ECC error, and reg is the UBA register
653 * mapping (the first part of) the transfer.
654 * O is offset within a memory page of the first byte transferred.
008c0481 655 */
7bc8d985 656 npf = btop((up->upwc * sizeof(short)) + bp->b_bcount) - 1;
b7333467 657 reg = btop(um->um_ubinfo&0x3ffff) + npf;
008c0481
BJ
658 o = (int)bp->b_un.b_addr & PGOFSET;
659 printf("%D ", bp->b_blkno+npf);
660 prdev("ECC", bp->b_dev);
661 mask = up->upec2;
662 if (mask == 0) {
2601dfbd 663 up->upof = UP_FMT22; /* == RTC ???? */
008c0481
BJ
664 return (0);
665 }
7bc8d985
BJ
666 /*
667 * Flush the buffered data path, and compute the
668 * byte and bit position of the error. The variable i
669 * is the byte offset in the transfer, the variable byte
670 * is the offset from a page boundary in main memory.
671 */
b7333467 672 ubp->uba_dpr[(um->um_ubinfo>>28)&0x0f] |= UBA_BNE;
7bc8d985
BJ
673 i = up->upec1 - 1; /* -1 makes 0 origin */
674 bit = i&07;
675 i = (i&~07)>>3;
008c0481 676 byte = i + o;
7bc8d985
BJ
677 /*
678 * Correct while possible bits remain of mask. Since mask
679 * contains 11 bits, we continue while the bit offset is > -11.
680 * Also watch out for end of this block and the end of the whole
681 * transfer.
682 */
683 while (i < 512 && (int)ptob(npf)+i < bp->b_bcount && bit > -11) {
684 addr = ptob(ubp->uba_map[reg+btop(byte)].pg_pfnum)+
685 (byte & PGOFSET);
686 putmemc(addr, getmemc(addr)^(mask<<bit));
687 byte++;
688 i++;
689 bit -= 8;
008c0481 690 }
3f3a34c3 691 um->um_tab.b_active++; /* Either complete or continuing... */
008c0481
BJ
692 if (up->upwc == 0)
693 return (0);
7bc8d985
BJ
694 /*
695 * Have to continue the transfer... clear the drive,
696 * and compute the position where the transfer is to continue.
697 * We have completed npf+1 sectors of the transfer already;
698 * restart at offset o of next sector (i.e. in UBA register reg+1).
699 */
2601dfbd
BJ
700#ifdef notdef
701 up->uper1 = 0;
702 up->upcs1 |= UP_GO;
703#else
704 up->upcs1 = UP_TRE|UP_IE|UP_DCLR|UP_GO;
008c0481 705 bn = dkblock(bp);
3f3a34c3 706 st = &upst[ui->ui_type];
008c0481 707 cn = bp->b_cylin;
3f3a34c3
BJ
708 sn = bn%st->nspc + npf + 1;
709 tn = sn/st->nsect;
710 sn %= st->nsect;
711 cn += tn/st->ntrak;
712 tn %= st->ntrak;
008c0481 713 up->updc = cn;
7bc8d985
BJ
714 up->upda = (tn << 8) | sn;
715 ubaddr = (int)ptob(reg+1) + o;
716 up->upba = ubaddr;
717 cmd = (ubaddr >> 8) & 0x300;
2601dfbd 718 cmd |= UP_IE|UP_GO|UP_RCOM;
7bc8d985 719 up->upcs1 = cmd;
2601dfbd 720#endif
008c0481
BJ
721 return (1);
722}
977c2848
BJ
723
724/*
725 * Reset driver after UBA init.
726 * Cancel software state of all pending transfers
727 * and restart all units and the controller.
728 */
3f3a34c3 729upreset(uban)
977c2848 730{
3f3a34c3
BJ
731 register struct uba_minfo *um;
732 register struct uba_dinfo *ui;
733 register sc21, unit;
5aa9d5ea 734 int any = 0;
3f3a34c3 735
a3cb8f60 736 for (sc21 = 0; sc21 < NSC; sc21++) {
7e00c42b
BJ
737 if ((um = upminfo[sc21]) == 0 || um->um_ubanum != uban ||
738 um->um_alive == 0)
3f3a34c3 739 continue;
5aa9d5ea
RE
740 if (any == 0) {
741 printf(" up");
7e00c42b 742 DELAY(10000000); /* give it time to self-test */
5aa9d5ea
RE
743 any++;
744 }
3f3a34c3
BJ
745 um->um_tab.b_active = 0;
746 um->um_tab.b_actf = um->um_tab.b_actl = 0;
b7333467
BJ
747 if (um->um_ubinfo) {
748 printf("<%d>", (um->um_ubinfo>>28)&0xf);
0801d37f 749 ubadone(um);
3f3a34c3 750 }
2601dfbd 751 ((struct updevice *)(um->um_addr))->upcs2 = UP_CLR;
3f3a34c3
BJ
752 for (unit = 0; unit < NUP; unit++) {
753 if ((ui = updinfo[unit]) == 0)
754 continue;
755 if (ui->ui_alive == 0)
756 continue;
757 uputab[unit].b_active = 0;
758 (void) upustart(ui);
759 }
760 (void) upstart(um);
977c2848 761 }
977c2848 762}
6a81870e
BJ
763
764/*
765 * Wake up every second and if an interrupt is pending
766 * but nothing has happened increment a counter.
767 * If nothing happens for 20 seconds, reset the controller
768 * and begin anew.
769 */
770upwatch()
771{
3f3a34c3
BJ
772 register struct uba_minfo *um;
773 register sc21, unit;
7e00c42b 774 register struct up_softc *sc;
6a81870e 775
49c84d3f 776 timeout(upwatch, (caddr_t)0, HZ);
a3cb8f60 777 for (sc21 = 0; sc21 < NSC; sc21++) {
3f3a34c3 778 um = upminfo[sc21];
7e00c42b
BJ
779 if (um == 0 || um->um_alive == 0)
780 continue;
781 sc = &up_softc[sc21];
3f3a34c3
BJ
782 if (um->um_tab.b_active == 0) {
783 for (unit = 0; unit < NUP; unit++)
2601dfbd
BJ
784 if (uputab[unit].b_active &&
785 updinfo[unit]->ui_mi == um)
3f3a34c3 786 goto active;
7e00c42b 787 sc->sc_wticks = 0;
3f3a34c3
BJ
788 continue;
789 }
790 active:
7e00c42b
BJ
791 sc->sc_wticks++;
792 if (sc->sc_wticks >= 20) {
793 sc->sc_wticks = 0;
a3cb8f60
BJ
794 printf("LOST upintr ");
795 ubareset(um->um_ubanum);
3f3a34c3 796 }
6a81870e
BJ
797 }
798}
0ff318b2
BJ
799
800#define DBSIZE 20
801
802updump(dev)
803 dev_t dev;
804{
2601dfbd 805 struct updevice *upaddr;
0ff318b2 806 char *start;
71236e46 807 int num, blk, unit;
0ff318b2 808 struct size *sizes;
3f3a34c3
BJ
809 register struct uba_regs *uba;
810 register struct uba_dinfo *ui;
0ff318b2 811 register short *rp;
3f3a34c3 812 struct upst *st;
0ff318b2 813
0ff318b2
BJ
814 unit = minor(dev) >> 3;
815 if (unit >= NUP) {
816 printf("bad unit\n");
817 return (-1);
818 }
7e00c42b 819#define phys(cast, addr) ((cast)((int)addr & 0x7fffffff))
3f3a34c3
BJ
820 ui = phys(struct uba_dinfo *, updinfo[unit]);
821 if (ui->ui_alive == 0) {
822 printf("dna\n");
823 return(-1);
824 }
825 uba = phys(struct uba_hd *, ui->ui_hd)->uh_physuba;
826#if VAX780
7e00c42b
BJ
827 if (cpu == VAX_780)
828 ubainit(uba);
3f3a34c3
BJ
829#endif
830 DELAY(1000000);
2601dfbd
BJ
831 upaddr = (struct updevice *)ui->ui_physaddr;
832 while ((upaddr->upcs1&UP_DVA) == 0)
3f3a34c3
BJ
833 ;
834 num = maxfree;
835 start = 0;
0ff318b2 836 upaddr->upcs2 = unit;
2601dfbd
BJ
837 if ((upaddr->upds & UP_VV) == 0) {
838 upaddr->upcs1 = UP_DCLR|UP_GO;
839 upaddr->upcs1 = UP_PRESET|UP_GO;
840 upaddr->upof = UP_FMT22;
0ff318b2 841 }
2601dfbd
BJ
842 if ((upaddr->upds & (UP_DPR|UP_MOL)) != (UP_DPR|UP_MOL)) {
843 printf("dna\n");
0ff318b2
BJ
844 return (-1);
845 }
7e00c42b 846 st = &upst[ui->ui_type];
3f3a34c3 847 sizes = phys(struct size *, st->sizes);
0ff318b2 848 if (dumplo < 0 || dumplo + num >= sizes[minor(dev)&07].nblocks) {
3f3a34c3 849 printf("oor\n");
0ff318b2
BJ
850 return (-1);
851 }
0ff318b2
BJ
852 while (num > 0) {
853 register struct pte *io;
854 register int i;
855 int cn, sn, tn;
856 daddr_t bn;
857
858 blk = num > DBSIZE ? DBSIZE : num;
3f3a34c3 859 io = uba->uba_map;
0ff318b2 860 for (i = 0; i < blk; i++)
3f3a34c3 861 *(int *)io++ = (btop(start)+i) | (1<<21) | UBA_MRV;
0ff318b2
BJ
862 *(int *)io = 0;
863 bn = dumplo + btop(start);
71236e46
BJ
864 cn = bn/st->nspc + sizes[minor(dev)&07].cyloff;
865 sn = bn%st->nspc;
866 tn = sn/st->nsect;
867 sn = sn%st->nsect;
0ff318b2
BJ
868 upaddr->updc = cn;
869 rp = (short *) &upaddr->upda;
870 *rp = (tn << 8) + sn;
871 *--rp = 0;
872 *--rp = -blk*NBPG / sizeof (short);
2601dfbd 873 *--rp = UP_GO|UP_WCOM;
0ff318b2
BJ
874 do {
875 DELAY(25);
2601dfbd
BJ
876 } while ((upaddr->upcs1 & UP_RDY) == 0);
877 if (upaddr->upcs1&UP_ERR) {
0ff318b2
BJ
878 printf("up dump dsk err: (%d,%d,%d) cs1=%x, er1=%x\n",
879 cn, tn, sn, upaddr->upcs1, upaddr->uper1);
880 return (-1);
881 }
882 start += blk*NBPG;
883 num -= blk;
884 }
0ff318b2
BJ
885 return (0);
886}
63c35a63 887#endif