ubafree() becomes ubarelse() to fix race conditions
[unix-history] / usr / src / sys / vax / uba / up.c
CommitLineData
b28deaf8 1/* up.c 4.10 %G% */
008c0481 2
66b4fb09 3#include "up.h"
a801d8f2 4#if NUP > 0
a0c1902e
BJ
5#if SC11 > 0
6#include "../dev/up.c.SC11"
63c35a63 7#else
008c0481 8/*
10fb932f 9 * UNIBUS disk driver with overlapped seeks and ECC recovery.
008c0481 10 */
355250d9 11#define DELAY(N) { register int d; d = N; while (--d > 0); }
008c0481
BJ
12
13#include "../h/param.h"
14#include "../h/systm.h"
41888f16 15#include "../h/dk.h"
008c0481
BJ
16#include "../h/buf.h"
17#include "../h/conf.h"
18#include "../h/dir.h"
19#include "../h/user.h"
20#include "../h/map.h"
80e7c811 21#include "../h/pte.h"
008c0481
BJ
22#include "../h/mba.h"
23#include "../h/mtpr.h"
008c0481
BJ
24#include "../h/uba.h"
25#include "../h/vm.h"
26
008c0481
BJ
27#define ushort unsigned short
28
29struct device
30{
31 ushort upcs1; /* control and status register 1 */
32 short upwc; /* word count register */
33 ushort upba; /* UNIBUS address register */
34 ushort upda; /* desired address register */
35 ushort upcs2; /* control and status register 2 */
36 ushort upds; /* drive Status */
37 ushort uper1; /* error register 1 */
38 ushort upas; /* attention summary */
39 ushort upla; /* look ahead */
40 ushort updb; /* data buffer */
41 ushort upmr; /* maintenance */
42 ushort updt; /* drive type */
43 ushort upsn; /* serial number */
44 ushort upof; /* offset register */
45 ushort updc; /* desired cylinder address register */
46 ushort upcc; /* current cylinder */
47 ushort uper2; /* error register 2 */
48 ushort uper3; /* error register 3 */
49 ushort upec1; /* burst error bit position */
50 ushort upec2; /* burst error bit pattern */
51};
52
2a3b9a7f
BJ
53/*
54 * Software extension to the upas register, so we can
55 * postpone starting SEARCH commands until the controller
56 * is not transferring.
57 */
d1778415 58int upsoftas;
2a3b9a7f
BJ
59
60/*
61 * If upseek then we don't issue SEARCH commands but rather just
62 * settle for a SEEK to the correct cylinder.
63 */
64int upseek;
65
008c0481
BJ
66#define NSECT 32
67#define NTRAC 19
68
69/*
70 * Constants controlling on-cylinder SEARCH usage.
71 *
41888f16
BJ
72 * upSDIST/2 msec time needed to start transfer
73 * upRDIST/2 msec tolerable rotational latency when on-cylinder
2a3b9a7f 74 *
41888f16 75 * If we are no closer than upSDIST sectors and no further than upSDIST+upRDIST
2a3b9a7f 76 * and in the driver then we take it as it is. Otherwise we do a SEARCH
41888f16 77 * requesting an interrupt upSDIST sectors in advance.
008c0481 78 */
3cab1981
BJ
79#define _upSDIST 2 /* 1.0 msec */
80#define _upRDIST 4 /* 2.0 msec */
2a3b9a7f 81
41888f16
BJ
82int upSDIST = _upSDIST;
83int upRDIST = _upRDIST;
008c0481
BJ
84
85/*
86 * To fill a 300M drive:
87 * A is designed to be used as a root.
88 * B is suitable for a swap area.
89 * H is the primary storage area.
90 * On systems with RP06'es, we normally use only 291346 blocks of the H
91 * area, and use DEF or G to cover the rest of the drive. The C system
92 * covers the whole drive and can be used for pack-pack copying.
355250d9
BJ
93 *
94 * Note: sizes here are for AMPEX drives with 815 cylinders.
95 * CDC drives can make the F,G, and H areas larger as they have 823 cylinders.
008c0481
BJ
96 */
97struct size
98{
99 daddr_t nblocks;
100 int cyloff;
101} up_sizes[8] = {
102 15884, 0, /* A=cyl 0 thru 26 */
103 33440, 27, /* B=cyl 27 thru 81 */
d1778415 104 495520, 0, /* C=cyl 0 thru 814 */
008c0481
BJ
105 15884, 562, /* D=cyl 562 thru 588 */
106 55936, 589, /* E=cyl 589 thru 680 */
107 81472, 681, /* F=cyl 681 thru 814 */
108 153824, 562, /* G=cyl 562 thru 814 */
008c0481
BJ
109 291346, 82, /* H=cyl 82 thru 561 */
110};
111
112/*
113 * The following defines are used in offset positioning
114 * when trying to recover disk errors, with the constants being
115 * +/- microinches. Note that header compare inhibit (HCI) is not
116 * tried (this makes sense only during read, in any case.)
117 *
355250d9 118 * NB: Not all drives/controllers emulate all of these.
008c0481
BJ
119 */
120#define P400 020
121#define M400 0220
122#define P800 040
123#define M800 0240
124#define P1200 060
125#define M1200 0260
126#define HCI 020000
127
128int up_offset[16] =
129{
130 P400, M400, P400, M400,
131 P800, M800, P800, M800,
132 P1200, M1200, P1200, M1200,
133 0, 0, 0, 0,
134};
135
136/*
137 * Each drive has a table uputab[i]. On this table are sorted the
138 * pending requests implementing an elevator algorithm (see dsort.c.)
139 * In the upustart() routine, each drive is independently advanced
140 * until it is on the desired cylinder for the next transfer and near
141 * the desired sector. The drive is then chained onto the uptab
142 * table, and the transfer is initiated by the upstart() routine.
143 * When the transfer is completed the driver reinvokes the upustart()
144 * routine to set up the next transfer.
145 */
146struct buf uptab;
147struct buf uputab[NUP];
148
149struct buf rupbuf; /* Buffer for raw i/o */
150
151/* Drive commands, placed in upcs1 */
152#define GO 01 /* Go bit, set in all commands */
153#define PRESET 020 /* Preset drive at init or after errors */
154#define OFFSET 014 /* Offset heads to try to recover error */
155#define RTC 016 /* Return to center-line after OFFSET */
156#define SEARCH 030 /* Search for cylinder+sector */
2a3b9a7f 157#define SEEK 04 /* Seek to cylinder */
008c0481
BJ
158#define RECAL 06 /* Recalibrate, needed after seek error */
159#define DCLR 010 /* Drive clear, after error */
160#define WCOM 060 /* Write */
161#define RCOM 070 /* Read */
162
163/* Other bits of upcs1 */
164#define IE 0100 /* Controller wide interrupt enable */
165#define TRE 040000 /* Transfer error */
ec1b1145 166#define RDY 0200 /* Transfer terminated */
008c0481
BJ
167
168/* Drive status bits of upds */
169#define PIP 020000 /* Positioning in progress */
170#define ERR 040000 /* Error has occurred, DCLR necessary */
171#define VV 0100 /* Volume is valid, set by PRESET */
172#define DPR 0400 /* Drive has been preset */
173#define MOL 010000 /* Drive is online, heads loaded, etc */
174#define DRY 0200 /* Drive ready */
175
6a81870e
BJ
176/* Bits of upcs2 */
177#define CLR 040 /* Controller clear */
0ad5097e
BJ
178#define MXF 01000
179#define NEM 04000
180
008c0481
BJ
181/* Bits of uper1 */
182#define DCK 0100000 /* Ecc error occurred */
183#define ECH 0100 /* Ecc error was unrecoverable */
184#define WLE 04000 /* Attempt to write read-only drive */
185
186/* Bits of upof; the offset bits above are also in this register */
187#define FMT22 010000 /* 16 bits/word, must be always set */
188
189#define b_cylin b_resid
190
191int up_ubinfo; /* Information about UBA usage saved here */
008c0481 192
6a81870e
BJ
193int up_wticks; /* Ticks waiting for interrupt */
194int upwstart; /* Have started guardian */
195int upwatch();
196
008c0481
BJ
197#ifdef INTRLVE
198daddr_t dkblock();
199#endif
200
201/*
202 * Queue an i/o request for a drive, checking first that it is in range.
203 *
204 * A unit start is issued if the drive is inactive, causing
205 * a SEARCH for the correct cylinder/sector. If the drive is
206 * already nearly on the money and the controller is not transferring
207 * we kick it to start the transfer.
208 */
209upstrategy(bp)
210register struct buf *bp;
211{
212 register struct buf *dp;
213 register unit, xunit;
214 long sz, bn;
215
6a81870e 216 if (upwstart == 0) {
49c84d3f 217 timeout(upwatch, (caddr_t)0, HZ);
6a81870e
BJ
218 upwstart++;
219 }
008c0481
BJ
220 xunit = minor(bp->b_dev) & 077;
221 sz = bp->b_bcount;
222 sz = (sz+511) >> 9; /* transfer size in 512 byte sectors */
223 unit = dkunit(bp);
224 if (unit >= NUP ||
225 bp->b_blkno < 0 ||
226 (bn = dkblock(bp))+sz > up_sizes[xunit&07].nblocks) {
227 bp->b_flags |= B_ERROR;
228 iodone(bp);
229 return;
230 }
c134650e
BJ
231 if (UPDK_N+unit <= UPDK_NMAX)
232 dk_mspw[UPDK_N+unit] = .0000020345;
008c0481
BJ
233 bp->b_cylin = bn/(NSECT*NTRAC) + up_sizes[xunit&07].cyloff;
234 dp = &uputab[unit];
235 (void) spl5();
236 disksort(dp, bp);
237 if (dp->b_active == 0) {
eb891eaa 238 (void) upustart(unit);
008c0481 239 if (uptab.b_actf && uptab.b_active == 0)
eb891eaa 240 (void) upstart();
008c0481
BJ
241 }
242 (void) spl0();
243}
244
245/*
246 * Start activity on specified drive; called when drive is inactive
247 * and new transfer request arrives and also when upas indicates that
248 * a SEARCH command is complete.
249 */
250upustart(unit)
251register unit;
252{
253 register struct buf *bp, *dp;
254 register struct device *upaddr = UPADDR;
255 daddr_t bn;
256 int sn, cn, csn;
eb891eaa 257 int didie = 0;
008c0481 258
7bc8d985 259 /*
2a3b9a7f
BJ
260 * Other drivers tend to say something like
261 * upaddr->upcs1 = IE;
262 * upaddr->upas = 1<<unit;
355250d9 263 * here, but some controllers will cancel a command
2a3b9a7f 264 * happens to be sitting in the cs1 if you clear the go
355250d9 265 * bit by storing there (so the first is not safe).
2a3b9a7f
BJ
266 *
267 * Thus we keep careful track of when we re-enable IE
268 * after an interrupt and do it only if we didn't issue
269 * a command which re-enabled it as a matter of course.
270 * We clear bits in upas in the interrupt routine, when
271 * no transfers are active.
7bc8d985 272 */
2a3b9a7f
BJ
273 if (unit >= NUP)
274 goto out;
c134650e
BJ
275 if (unit+UPDK_N <= UPDK_NMAX)
276 dk_busy &= ~(1<<(unit+UPDK_N));
008c0481 277 dp = &uputab[unit];
7bc8d985 278 if ((bp = dp->b_actf) == NULL)
eb891eaa 279 goto out;
2a3b9a7f 280 /*
355250d9
BJ
281 * Most controllers don't start SEARCH commands when transfers are
282 * in progress. In fact, some tend to get confused when given
2a3b9a7f
BJ
283 * SEARCH'es during transfers, generating interrupts with neither
284 * RDY nor a bit in the upas register. Thus we defer
285 * until an interrupt when a transfer is pending.
286 */
287 if (uptab.b_active) {
d1778415 288 upsoftas |= 1<<unit;
2a3b9a7f
BJ
289 return (0);
290 }
a3f430e0
BJ
291 if (dp->b_active)
292 goto done;
293 dp->b_active = 1;
355250d9 294 if ((upaddr->upcs2 & 07) != unit)
008c0481 295 upaddr->upcs2 = unit;
7bc8d985
BJ
296 /*
297 * If we have changed packs or just initialized,
2a3b9a7f 298 * then the volume will not be valid; if so, clear
7bc8d985
BJ
299 * the drive, preset it and put in 16bit/word mode.
300 */
301 if ((upaddr->upds & VV) == 0) {
302 upaddr->upcs1 = IE|DCLR|GO;
008c0481 303 upaddr->upcs1 = IE|PRESET|GO;
008c0481 304 upaddr->upof = FMT22;
eb891eaa 305 didie = 1;
008c0481 306 }
008c0481 307 if ((upaddr->upds & (DPR|MOL)) != (DPR|MOL))
2a3b9a7f 308 goto done;
7bc8d985
BJ
309 /*
310 * Do enough of the disk address decoding to determine
311 * which cylinder and sector the request is on.
7bc8d985 312 * If we are on the correct cylinder and the desired sector
41888f16 313 * lies between upSDIST and upSDIST+upRDIST sectors ahead of us, then
7bc8d985 314 * we don't bother to SEARCH but just begin the transfer asap.
41888f16 315 * Otherwise ask for a interrupt upSDIST sectors ahead.
7bc8d985 316 */
008c0481
BJ
317 bn = dkblock(bp);
318 cn = bp->b_cylin;
319 sn = bn%(NSECT*NTRAC);
41888f16 320 sn = (sn+NSECT-upSDIST)%NSECT;
008c0481 321
7bc8d985
BJ
322 if (cn - upaddr->updc)
323 goto search; /* Not on-cylinder */
2a3b9a7f
BJ
324 else if (upseek)
325 goto done; /* Ok just to be on-cylinder */
008c0481 326 csn = (upaddr->upla>>6) - sn - 1;
7bc8d985 327 if (csn < 0)
008c0481 328 csn += NSECT;
41888f16 329 if (csn > NSECT-upRDIST)
008c0481
BJ
330 goto done;
331
332search:
333 upaddr->updc = cn;
2a3b9a7f
BJ
334 if (upseek)
335 upaddr->upcs1 = IE|SEEK|GO;
336 else {
337 upaddr->upda = sn;
338 upaddr->upcs1 = IE|SEARCH|GO;
339 }
eb891eaa 340 didie = 1;
7bc8d985
BJ
341 /*
342 * Mark this unit busy.
343 */
c134650e
BJ
344 unit += UPDK_N;
345 if (unit <= UPDK_NMAX) {
008c0481 346 dk_busy |= 1<<unit;
f9b6e695 347 dk_seek[unit]++;
008c0481 348 }
eb891eaa 349 goto out;
008c0481
BJ
350
351done:
7bc8d985 352 /*
2a3b9a7f
BJ
353 * This unit is ready to go so
354 * link it onto the chain of ready disks.
7bc8d985 355 */
008c0481 356 dp->b_forw = NULL;
7bc8d985 357 if (uptab.b_actf == NULL)
008c0481
BJ
358 uptab.b_actf = dp;
359 else
360 uptab.b_actl->b_forw = dp;
361 uptab.b_actl = dp;
eb891eaa
BJ
362
363out:
364 return (didie);
008c0481
BJ
365}
366
367/*
368 * Start a transfer; call from top level at spl5() or on interrupt.
008c0481
BJ
369 */
370upstart()
371{
372 register struct buf *bp, *dp;
373 register unit;
374 register struct device *upaddr;
375 daddr_t bn;
7bc8d985 376 int dn, sn, tn, cn, cmd;
008c0481 377
008c0481 378loop:
7bc8d985
BJ
379 /*
380 * Pick a drive off the queue of ready drives, and
381 * perform the first transfer on its queue.
382 *
383 * Looping here is completely for the sake of drives which
384 * are not present and on-line, for which we completely clear the
385 * request queue.
386 */
1f3d30ee 387 if ((dp = uptab.b_actf) == NULL)
eb891eaa 388 return (0);
008c0481
BJ
389 if ((bp = dp->b_actf) == NULL) {
390 uptab.b_actf = dp->b_forw;
391 goto loop;
392 }
7bc8d985
BJ
393 /*
394 * Mark the controller busy, and multi-part disk address.
395 * Select the unit on which the i/o is to take place.
396 */
008c0481
BJ
397 uptab.b_active++;
398 unit = minor(bp->b_dev) & 077;
399 dn = dkunit(bp);
400 bn = dkblock(bp);
401 cn = up_sizes[unit&07].cyloff;
402 cn += bn/(NSECT*NTRAC);
403 sn = bn%(NSECT*NTRAC);
404 tn = sn/NSECT;
7bc8d985 405 sn %= NSECT;
008c0481 406 upaddr = UPADDR;
355250d9 407 if ((upaddr->upcs2 & 07) != dn)
008c0481 408 upaddr->upcs2 = dn;
355250d9 409 up_ubinfo = ubasetup(bp, 1);
7bc8d985
BJ
410 /*
411 * If drive is not present and on-line, then
412 * get rid of this with an error and loop to get
413 * rid of the rest of its queued requests.
414 * (Then on to any other ready drives.)
415 */
008c0481 416 if ((upaddr->upds & (DPR|MOL)) != (DPR|MOL)) {
88253fd2
BJ
417 printf("!DPR || !MOL, unit %d, ds %o", dn, upaddr->upds);
418 if ((upaddr->upds & (DPR|MOL)) != (DPR|MOL)) {
419 printf("-- hard\n");
420 uptab.b_active = 0;
421 uptab.b_errcnt = 0;
422 dp->b_actf = bp->av_forw;
423 dp->b_active = 0;
424 bp->b_flags |= B_ERROR;
425 iodone(bp);
426 /* A funny place to do this ... */
b28deaf8 427 ubarelse(&up_ubinfo);
88253fd2
BJ
428 goto loop;
429 }
430 printf("-- came back\n");
008c0481 431 }
7bc8d985
BJ
432 /*
433 * If this is a retry, then with the 16'th retry we
434 * begin to try offsetting the heads to recover the data.
435 */
15c4ad30 436 if (uptab.b_errcnt >= 16 && (bp->b_flags&B_WRITE) == 0) {
008c0481 437 upaddr->upof = up_offset[uptab.b_errcnt & 017] | FMT22;
7bc8d985 438 upaddr->upcs1 = IE|OFFSET|GO;
7bc8d985 439 while (upaddr->upds & PIP)
008c0481
BJ
440 DELAY(25);
441 }
7bc8d985
BJ
442 /*
443 * Now set up the transfer, retrieving the high
444 * 2 bits of the UNIBUS address from the information
445 * returned by ubasetup() for the cs1 register bits 8 and 9.
446 */
008c0481
BJ
447 upaddr->updc = cn;
448 upaddr->upda = (tn << 8) + sn;
449 upaddr->upba = up_ubinfo;
450 upaddr->upwc = -bp->b_bcount / sizeof (short);
7bc8d985 451 cmd = (up_ubinfo >> 8) & 0x300;
008c0481 452 if (bp->b_flags & B_READ)
7bc8d985 453 cmd |= IE|RCOM|GO;
008c0481 454 else
7bc8d985 455 cmd |= IE|WCOM|GO;
7bc8d985 456 upaddr->upcs1 = cmd;
7bc8d985
BJ
457 /*
458 * This is a controller busy situation.
c134650e 459 * Record in dk slot NUP+UPDK_N (after last drive)
7bc8d985
BJ
460 * unless there aren't that many slots reserved for
461 * us in which case we record this as a drive busy
462 * (if there is room for that).
463 */
c134650e
BJ
464 unit = dn+UPDK_N;
465 if (unit <= UPDK_NMAX) {
008c0481 466 dk_busy |= 1<<unit;
f9b6e695 467 dk_xfer[unit]++;
008c0481
BJ
468 dk_wds[unit] += bp->b_bcount>>6;
469 }
eb891eaa 470 return (1);
008c0481
BJ
471}
472
473/*
474 * Handle a device interrupt.
475 *
476 * If the transferring drive needs attention, service it
477 * retrying on error or beginning next transfer.
478 * Service all other ready drives, calling ustart to transfer
479 * their blocks to the ready queue in uptab, and then restart
480 * the controller if there is anything to do.
481 */
482upintr()
483{
484 register struct buf *bp, *dp;
485 register unit;
486 register struct device *upaddr = UPADDR;
487 int as = upaddr->upas & 0377;
d1778415 488 int oupsoftas;
eb891eaa 489 int needie = 1;
008c0481 490
a3f430e0 491 (void) spl6();
6a81870e 492 up_wticks = 0;
7bc8d985
BJ
493 if (uptab.b_active) {
494 /*
495 * The drive is transferring, thus the hardware
496 * (say the designers) will only interrupt when the transfer
497 * completes; check for it anyways.
498 */
499 if ((upaddr->upcs1 & RDY) == 0) {
39bd608f
BJ
500 printf("!RDY: cs1 %o, ds %o, wc %d\n", upaddr->upcs1,
501 upaddr->upds, upaddr->upwc);
d1778415
BJ
502 printf("as=%d act %d %d %d\n", as, uptab.b_active,
503 uputab[0].b_active, uputab[1].b_active);
6e179703 504 }
7bc8d985 505 /*
f9b6e695 506 * Mark drive not busy, and check for an
7bc8d985
BJ
507 * error condition which may have resulted from the transfer.
508 */
008c0481
BJ
509 dp = uptab.b_actf;
510 bp = dp->b_actf;
511 unit = dkunit(bp);
c134650e
BJ
512 if (UPDK_N+unit <= UPDK_NMAX)
513 dk_busy &= ~(1<<(UPDK_N+unit));
355250d9 514 if ((upaddr->upcs2 & 07) != unit)
2a3b9a7f 515 upaddr->upcs2 = unit;
10fb932f 516 if ((upaddr->upds&ERR) || (upaddr->upcs1&TRE)) {
0ad5097e 517 int cs2;
7bc8d985
BJ
518 /*
519 * An error occurred, indeed. Select this unit
520 * to get at the drive status (a SEARCH may have
521 * intervened to change the selected unit), and
522 * wait for the command which caused the interrupt
523 * to complete (DRY).
7bc8d985 524 */
7bc8d985 525 while ((upaddr->upds & DRY) == 0)
008c0481 526 DELAY(25);
7bc8d985
BJ
527 /*
528 * After 28 retries (16 w/o servo offsets, and then
529 * 12 with servo offsets), or if we encountered
530 * an error because the drive is write-protected,
531 * give up. Print an error message on the last 2
532 * retries before a hard failure.
533 */
534 if (++uptab.b_errcnt > 28 || upaddr->uper1&WLE)
008c0481
BJ
535 bp->b_flags |= B_ERROR;
536 else
7bc8d985
BJ
537 uptab.b_active = 0; /* To force retry */
538 if (uptab.b_errcnt > 27)
0ad5097e 539 cs2 = (int)upaddr->upcs2;
49c84d3f
BJ
540 deverror(bp, (int)upaddr->upcs2,
541 (int)upaddr->uper1);
7bc8d985
BJ
542 /*
543 * If this was a correctible ECC error, let upecc
544 * do the dirty work to correct it. If upecc
545 * starts another READ for the rest of the data
546 * then it returns 1 (having set uptab.b_active).
547 * Otherwise we are done and fall through to
548 * finish up.
549 */
550 if ((upaddr->uper1&(DCK|ECH))==DCK && upecc(upaddr, bp))
551 return;
552 /*
553 * Clear the drive and, every 4 retries, recalibrate
554 * to hopefully help clear up seek positioning problems.
555 */
008c0481 556 upaddr->upcs1 = TRE|IE|DCLR|GO;
eb891eaa 557 needie = 0;
7bc8d985 558 if ((uptab.b_errcnt&07) == 4) {
008c0481 559 upaddr->upcs1 = RECAL|GO|IE;
008c0481
BJ
560 while(upaddr->upds & PIP)
561 DELAY(25);
562 }
0ad5097e
BJ
563 if (uptab.b_errcnt == 28 && cs2&(NEM|MXF)) {
564 printf("FLAKEY UP ");
565 ubareset();
566 return;
567 }
008c0481 568 }
7bc8d985
BJ
569 /*
570 * If we are still noted as active, then no
571 * (further) retries are necessary.
572 *
573 * Make sure the correct unit is selected,
574 * return it to centerline if necessary, and mark
575 * this i/o complete, starting the next transfer
576 * on this drive with the upustart routine (if any).
577 */
578 if (uptab.b_active) {
7bc8d985
BJ
579 if (uptab.b_errcnt >= 16) {
580 upaddr->upcs1 = RTC|GO|IE;
7bc8d985 581 while (upaddr->upds & PIP)
008c0481 582 DELAY(25);
eb891eaa 583 needie = 0;
008c0481
BJ
584 }
585 uptab.b_active = 0;
586 uptab.b_errcnt = 0;
587 uptab.b_actf = dp->b_forw;
588 dp->b_active = 0;
589 dp->b_errcnt = 0;
590 dp->b_actf = bp->av_forw;
7bc8d985 591 bp->b_resid = (-upaddr->upwc * sizeof(short));
2a3b9a7f 592 if (bp->b_resid)
d1778415
BJ
593 printf("resid %d ds %o er? %o %o %o\n",
594 bp->b_resid, upaddr->upds,
2a3b9a7f 595 upaddr->uper1, upaddr->uper2, upaddr->uper3);
008c0481
BJ
596 iodone(bp);
597 if(dp->b_actf)
eb891eaa
BJ
598 if (upustart(unit))
599 needie = 0;
008c0481
BJ
600 }
601 as &= ~(1<<unit);
d1778415 602 upsoftas &= ~(1<<unit);
b28deaf8 603 ubarelse(&up_ubinfo);
1f3d30ee 604 } else {
355250d9 605 if (upaddr->upcs1 & TRE)
008c0481 606 upaddr->upcs1 = TRE;
008c0481 607 }
7bc8d985
BJ
608 /*
609 * If we have a unit with an outstanding SEARCH,
610 * and the hardware indicates the unit requires attention,
611 * the bring the drive to the ready queue.
612 * Finally, if the controller is not transferring
613 * start it if any drives are now ready to transfer.
614 */
d1778415
BJ
615 as |= upsoftas;
616 oupsoftas = upsoftas;
617 upsoftas = 0;
7bc8d985 618 for (unit = 0; unit < NUP; unit++)
d1778415 619 if ((as|oupsoftas) & (1<<unit)) {
355250d9 620 if (as & (1<<unit))
5c65daa3 621 upaddr->upas = 1<<unit;
1f3d30ee
BJ
622 if (upustart(unit))
623 needie = 0;
624 }
7bc8d985 625 if (uptab.b_actf && uptab.b_active == 0)
eb891eaa
BJ
626 if (upstart())
627 needie = 0;
2a3b9a7f 628 if (needie)
7bc8d985 629 upaddr->upcs1 = IE;
008c0481
BJ
630}
631
632upread(dev)
633{
634
635 physio(upstrategy, &rupbuf, dev, B_READ, minphys);
636}
637
638upwrite(dev)
639{
640
641 physio(upstrategy, &rupbuf, dev, B_WRITE, minphys);
642}
643
7bc8d985
BJ
644/*
645 * Correct an ECC error, and restart the i/o to complete
646 * the transfer if necessary. This is quite complicated because
647 * the transfer may be going to an odd memory address base and/or
648 * across a page boundary.
649 */
008c0481
BJ
650upecc(up, bp)
651register struct device *up;
652register struct buf *bp;
653{
654 struct uba_regs *ubp = (struct uba_regs *)UBA0;
7bc8d985 655 register int i;
008c0481 656 caddr_t addr;
7bc8d985 657 int reg, bit, byte, npf, mask, o, cmd, ubaddr;
008c0481
BJ
658 int bn, cn, tn, sn;
659
008c0481 660 /*
7bc8d985
BJ
661 * Npf is the number of sectors transferred before the sector
662 * containing the ECC error, and reg is the UBA register
663 * mapping (the first part of) the transfer.
664 * O is offset within a memory page of the first byte transferred.
008c0481 665 */
7bc8d985
BJ
666 npf = btop((up->upwc * sizeof(short)) + bp->b_bcount) - 1;
667 reg = btop(up_ubinfo&0x3ffff) + npf;
008c0481
BJ
668 o = (int)bp->b_un.b_addr & PGOFSET;
669 printf("%D ", bp->b_blkno+npf);
670 prdev("ECC", bp->b_dev);
671 mask = up->upec2;
672 if (mask == 0) {
7bc8d985 673 up->upof = FMT22; /* == RTC ???? */
008c0481
BJ
674 return (0);
675 }
7bc8d985
BJ
676 /*
677 * Flush the buffered data path, and compute the
678 * byte and bit position of the error. The variable i
679 * is the byte offset in the transfer, the variable byte
680 * is the offset from a page boundary in main memory.
681 */
682 ubp->uba_dpr[(up_ubinfo>>28)&0x0f] |= BNE;
683 i = up->upec1 - 1; /* -1 makes 0 origin */
684 bit = i&07;
685 i = (i&~07)>>3;
008c0481 686 byte = i + o;
7bc8d985
BJ
687 /*
688 * Correct while possible bits remain of mask. Since mask
689 * contains 11 bits, we continue while the bit offset is > -11.
690 * Also watch out for end of this block and the end of the whole
691 * transfer.
692 */
693 while (i < 512 && (int)ptob(npf)+i < bp->b_bcount && bit > -11) {
694 addr = ptob(ubp->uba_map[reg+btop(byte)].pg_pfnum)+
695 (byte & PGOFSET);
696 putmemc(addr, getmemc(addr)^(mask<<bit));
697 byte++;
698 i++;
699 bit -= 8;
008c0481 700 }
7bc8d985 701 uptab.b_active++; /* Either complete or continuing... */
008c0481
BJ
702 if (up->upwc == 0)
703 return (0);
7bc8d985
BJ
704 /*
705 * Have to continue the transfer... clear the drive,
706 * and compute the position where the transfer is to continue.
707 * We have completed npf+1 sectors of the transfer already;
708 * restart at offset o of next sector (i.e. in UBA register reg+1).
709 */
710 up->upcs1 = TRE|IE|DCLR|GO;
008c0481
BJ
711 bn = dkblock(bp);
712 cn = bp->b_cylin;
7bc8d985 713 sn = bn%(NSECT*NTRAC) + npf + 1;
008c0481
BJ
714 tn = sn/NSECT;
715 sn %= NSECT;
7bc8d985
BJ
716 cn += tn/NTRAC;
717 tn %= NTRAC;
008c0481 718 up->updc = cn;
7bc8d985
BJ
719 up->upda = (tn << 8) | sn;
720 ubaddr = (int)ptob(reg+1) + o;
721 up->upba = ubaddr;
722 cmd = (ubaddr >> 8) & 0x300;
723 cmd |= IE|GO|RCOM;
724 up->upcs1 = cmd;
008c0481
BJ
725 return (1);
726}
977c2848
BJ
727
728/*
729 * Reset driver after UBA init.
730 * Cancel software state of all pending transfers
731 * and restart all units and the controller.
732 */
733upreset()
734{
735 int unit;
736
737 printf(" up");
0ad5097e 738 DELAY(15000000); /* give it time to self-test */
977c2848
BJ
739 uptab.b_active = 0;
740 uptab.b_actf = uptab.b_actl = 0;
977c2848
BJ
741 if (up_ubinfo) {
742 printf("<%d>", (up_ubinfo>>28)&0xf);
b28deaf8 743 ubarelse(&up_ubinfo);
977c2848 744 }
6a81870e 745 UPADDR->upcs2 = CLR; /* clear controller */
977c2848
BJ
746 for (unit = 0; unit < NUP; unit++) {
747 uputab[unit].b_active = 0;
748 (void) upustart(unit);
749 }
750 (void) upstart();
751}
6a81870e
BJ
752
753/*
754 * Wake up every second and if an interrupt is pending
755 * but nothing has happened increment a counter.
756 * If nothing happens for 20 seconds, reset the controller
757 * and begin anew.
758 */
759upwatch()
760{
761 int i;
762
49c84d3f 763 timeout(upwatch, (caddr_t)0, HZ);
6a81870e
BJ
764 if (uptab.b_active == 0) {
765 for (i = 0; i < NUP; i++)
766 if (uputab[i].b_active)
767 goto active;
768 up_wticks = 0; /* idling */
769 return;
770 }
771active:
772 up_wticks++;
773 if (up_wticks >= 20) {
774 up_wticks = 0;
775 printf("LOST INTERRUPT RESET");
776 upreset();
777 printf("\n");
778 }
779}
a801d8f2 780#endif
63c35a63 781#endif