fix bool type errors
[unix-history] / usr / src / sys / vax / uba / tm.c
CommitLineData
cd1b1cd4 1/* tm.c 4.26 81/03/09 */
d75e5c3e 2
443c8066 3#include "te.h"
afb907f2 4#if NTM > 0
d75e5c3e 5/*
afb907f2 6 * TM11/TE10 tape driver
7e00c42b 7 *
d565635a
BJ
8 * TODO:
9 * test driver with more than one slave
10 * test driver with more than one controller
11 * test reset code
12 * test rewinds without hanging in driver
13 * what happens if you offline tape during rewind?
14 * test using file system on tape
d75e5c3e 15 */
d75e5c3e
BJ
16#include "../h/param.h"
17#include "../h/buf.h"
18#include "../h/dir.h"
19#include "../h/conf.h"
20#include "../h/user.h"
21#include "../h/file.h"
22#include "../h/map.h"
23#include "../h/pte.h"
e4271c68 24#include "../h/vm.h"
89bd2f01
BJ
25#include "../h/ubareg.h"
26#include "../h/ubavar.h"
d75e5c3e
BJ
27#include "../h/mtio.h"
28#include "../h/ioctl.h"
7455bf3e 29#include "../h/cmap.h"
3f3a34c3 30#include "../h/cpu.h"
d75e5c3e 31
3f3a34c3 32#include "../h/tmreg.h"
d75e5c3e 33
d565635a
BJ
34/*
35 * There is a ctmbuf per tape controller.
36 * It is used as the token to pass to the internal routines
37 * to execute tape ioctls, and also acts as a lock on the slaves
38 * on the controller, since there is only one per controller.
39 * In particular, when the tape is rewinding on close we release
40 * the user process but any further attempts to use the tape drive
41 * before the rewind completes will hang waiting for ctmbuf.
42 */
43struct buf ctmbuf[NTM];
44
45/*
46 * Raw tape operations use rtmbuf. The driver
47 * notices when rtmbuf is being used and allows the user
48 * program to continue after errors and read records
49 * not of the standard length (BSIZE).
50 */
51struct buf rtmbuf[NTM];
d75e5c3e 52
d565635a
BJ
53/*
54 * Driver unibus interface routines and variables.
55 */
71236e46 56int tmprobe(), tmslave(), tmattach(), tmdgo(), tmintr();
89bd2f01 57struct uba_ctlr *tmminfo[NTM];
d565635a
BJ
58struct uba_device *tedinfo[NTE];
59struct buf teutab[NTE];
60short tetotm[NTE];
d763a2b7 61u_short tmstd[] = { 0772520, 0 };
3f3a34c3 62struct uba_driver tmdriver =
d565635a 63 { tmprobe, tmslave, tmattach, tmdgo, tmstd, "te", tedinfo, "tm", tmminfo, 0 };
d75e5c3e
BJ
64
65/* bits in minor device */
d565635a
BJ
66#define TEUNIT(dev) (minor(dev)&03)
67#define TMUNIT(dev) (tetotm[TEUNIT(dev)])
d75e5c3e
BJ
68#define T_NOREWIND 04
69#define T_1600BPI 08
70
71#define INF (daddr_t)1000000L
72
71236e46
BJ
73/*
74 * Software state per tape transport.
d565635a
BJ
75 *
76 * 1. A tape drive is a unique-open device; we refuse opens when it is already.
77 * 2. We keep track of the current position on a block tape and seek
78 * before operations by forward/back spacing if necessary.
79 * 3. We remember if the last operation was a write on a tape, so if a tape
80 * is open read write and the last thing done is a write we can
81 * write a standard end of tape mark (two eofs).
82 * 4. We remember the status registers after the last command, using
83 * then internally and returning them to the SENSE ioctl.
84 * 5. We remember the last density the tape was used at. If it is
85 * not a BOT when we start using it and we are writing, we don't
86 * let the density be changed.
71236e46 87 */
d565635a 88struct te_softc {
71236e46
BJ
89 char sc_openf; /* lock against multiple opens */
90 char sc_lastiow; /* last op was a write */
91 daddr_t sc_blkno; /* block number, for block device tape */
d565635a 92 daddr_t sc_nxrec; /* position of end of tape, if known */
71236e46
BJ
93 u_short sc_erreg; /* copy of last erreg */
94 u_short sc_dsreg; /* copy of last dsreg */
95 short sc_resid; /* copy of last bc */
a0eab615 96#ifdef unneeded
d1330646 97 short sc_lastcmd; /* last command to handle direction changes */
80905075 98#endif
d565635a
BJ
99 u_short sc_dens; /* prototype command with density info */
100} te_softc[NTM];
a0eab615
BJ
101#ifdef unneeded
102int tmgapsdcnt; /* DEBUG */
103#endif
d75e5c3e 104
71236e46 105/*
d565635a
BJ
106 * States for um->um_tab.b_active, the per controller state flag.
107 * This is used to sequence control in the driver.
71236e46 108 */
d75e5c3e
BJ
109#define SSEEK 1 /* seeking */
110#define SIO 2 /* doing seq i/o */
111#define SCOM 3 /* sending control command */
71236e46 112#define SREW 4 /* sending a drive rewind */
d75e5c3e 113
5aa9d5ea
RE
114/*
115 * Determine if there is a controller for
116 * a tm at address reg. Our goal is to make the
117 * device interrupt.
5aa9d5ea 118 */
71236e46 119tmprobe(reg)
3f3a34c3
BJ
120 caddr_t reg;
121{
d565635a 122 register int br, cvec; /* must be r11,r10; value-result */
5aa9d5ea 123
71236e46 124#ifdef lint
a0eab615 125 br = 0; cvec = br; br = cvec;
71236e46
BJ
126#endif
127 ((struct device *)reg)->tmcs = TM_IE;
3f3a34c3 128 /*
afb907f2 129 * If this is a tm11, it ought to have interrupted
3f3a34c3 130 * by now, if it isn't (ie: it is a ts04) then we just
d763a2b7
BJ
131 * hope that it didn't interrupt, so autoconf will ignore it.
132 * Just in case, we will reference one
3f3a34c3 133 * of the more distant registers, and hope for a machine
d763a2b7 134 * check, or similar disaster if this is a ts.
7e00c42b
BJ
135 *
136 * Note: on an 11/780, badaddr will just generate
137 * a uba error for a ts; but our caller will notice that
138 * so we won't check for it.
3f3a34c3 139 */
a0eab615 140 if (badaddr((caddr_t)&((struct device *)reg)->tmrd, 2))
d763a2b7
BJ
141 return (0);
142 return (1);
3f3a34c3
BJ
143}
144
71236e46
BJ
145/*
146 * Due to a design flaw, we cannot ascertain if the tape
147 * exists or not unless it is on line - ie: unless a tape is
148 * mounted. This is too servere a restriction to bear,
149 * so all units are assumed to exist.
150 */
151/*ARGSUSED*/
e4271c68 152tmslave(ui, reg)
89bd2f01 153 struct uba_device *ui;
3f3a34c3
BJ
154 caddr_t reg;
155{
d763a2b7 156
d763a2b7 157 return (1);
3f3a34c3
BJ
158}
159
71236e46 160/*
d565635a 161 * Record attachment of the unit to the controller.
71236e46
BJ
162 */
163/*ARGSUSED*/
164tmattach(ui)
89bd2f01 165 struct uba_device *ui;
71236e46
BJ
166{
167
d565635a
BJ
168 /*
169 * Tetotm is used in TMUNIT to index the ctmbuf and rtmbuf
170 * arrays given a te unit number.
171 */
172 tetotm[ui->ui_unit] = ui->ui_mi->um_ctlr;
71236e46
BJ
173}
174
175/*
176 * Open the device. Tapes are unique open
177 * devices, so we refuse if it is already open.
178 * We also check that a tape is available, and
d565635a
BJ
179 * don't block waiting here; if you want to wait
180 * for a tape you should timeout in user code.
71236e46 181 */
d75e5c3e
BJ
182tmopen(dev, flag)
183 dev_t dev;
184 int flag;
185{
d565635a 186 register int teunit;
89bd2f01 187 register struct uba_device *ui;
d565635a
BJ
188 register struct te_softc *sc;
189 int dens;
d75e5c3e 190
d565635a
BJ
191 teunit = TEUNIT(dev);
192 if (teunit>=NTE || (sc = &te_softc[teunit])->sc_openf ||
193 (ui = tedinfo[teunit]) == 0 || ui->ui_alive == 0) {
71236e46 194 u.u_error = ENXIO;
d75e5c3e
BJ
195 return;
196 }
71236e46 197 tmcommand(dev, TM_SENSE, 1);
d565635a
BJ
198 dens = TM_IE | TM_GO | (ui->ui_slave << 8);
199 if ((minor(dev) & T_1600BPI) == 0)
200 dens |= TM_D800;
201 if ((sc->sc_erreg&(TMER_SELR|TMER_TUR)) != (TMER_SELR|TMER_TUR) ||
202 (sc->sc_erreg&TMER_BOT) == 0 && (flag&FWRITE) &&
203 dens != sc->sc_dens ||
204 (flag&(FREAD|FWRITE)) == FWRITE && sc->sc_erreg&TMER_WRL) {
205 /*
206 * Not online or density switch in mid-tape or write locked.
207 */
71236e46 208 u.u_error = EIO;
d75e5c3e
BJ
209 return;
210 }
71236e46 211 sc->sc_openf = 1;
7e00c42b
BJ
212 sc->sc_blkno = (daddr_t)0;
213 sc->sc_nxrec = INF;
71236e46 214 sc->sc_lastiow = 0;
d565635a 215 sc->sc_dens = dens;
d75e5c3e
BJ
216}
217
71236e46
BJ
218/*
219 * Close tape device.
220 *
221 * If tape was open for writing or last operation was
222 * a write, then write two EOF's and backspace over the last one.
223 * Unless this is a non-rewinding special file, rewind the tape.
224 * Make the tape available to others.
225 */
d75e5c3e
BJ
226tmclose(dev, flag)
227 register dev_t dev;
228 register flag;
229{
d565635a 230 register struct te_softc *sc = &te_softc[TEUNIT(dev)];
d75e5c3e 231
71236e46
BJ
232 if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
233 tmcommand(dev, TM_WEOF, 1);
234 tmcommand(dev, TM_WEOF, 1);
235 tmcommand(dev, TM_SREV, 1);
d75e5c3e
BJ
236 }
237 if ((minor(dev)&T_NOREWIND) == 0)
d565635a
BJ
238 /*
239 * 0 count means don't hang waiting for rewind complete
240 * rather ctmbuf stays busy until the operation completes
241 * preventing further opens from completing by
242 * preventing a TM_SENSE from completing.
243 */
244 tmcommand(dev, TM_REW, 0);
7e00c42b 245 sc->sc_openf = 0;
d75e5c3e
BJ
246}
247
71236e46
BJ
248/*
249 * Execute a command on the tape drive
250 * a specified number of times.
251 */
e4271c68 252tmcommand(dev, com, count)
d75e5c3e
BJ
253 dev_t dev;
254 int com, count;
255{
256 register struct buf *bp;
257
71236e46 258 bp = &ctmbuf[TMUNIT(dev)];
d75e5c3e
BJ
259 (void) spl5();
260 while (bp->b_flags&B_BUSY) {
d565635a
BJ
261 /*
262 * This special check is because B_BUSY never
263 * gets cleared in the non-waiting rewind case.
264 */
265 if (bp->b_command == TM_REW && bp->b_repcnt == 0 &&
266 (bp->b_flags&B_DONE))
267 break;
d75e5c3e
BJ
268 bp->b_flags |= B_WANTED;
269 sleep((caddr_t)bp, PRIBIO);
270 }
271 bp->b_flags = B_BUSY|B_READ;
272 (void) spl0();
273 bp->b_dev = dev;
274 bp->b_repcnt = -count;
275 bp->b_command = com;
276 bp->b_blkno = 0;
277 tmstrategy(bp);
d565635a
BJ
278 /*
279 * In case of rewind from close, don't wait.
280 * This is the only case where count can be 0.
281 */
282 if (count == 0)
283 return;
d75e5c3e
BJ
284 iowait(bp);
285 if (bp->b_flags&B_WANTED)
286 wakeup((caddr_t)bp);
287 bp->b_flags &= B_ERROR;
288}
289
71236e46 290/*
d565635a 291 * Queue a tape operation.
71236e46 292 */
d75e5c3e
BJ
293tmstrategy(bp)
294 register struct buf *bp;
295{
d565635a 296 int teunit = TEUNIT(bp->b_dev);
89bd2f01 297 register struct uba_ctlr *um;
71236e46 298 register struct buf *dp;
d75e5c3e 299
71236e46
BJ
300 /*
301 * Put transfer at end of unit queue
302 */
d565635a 303 dp = &teutab[teunit];
d75e5c3e
BJ
304 bp->av_forw = NULL;
305 (void) spl5();
71236e46
BJ
306 if (dp->b_actf == NULL) {
307 dp->b_actf = bp;
308 /*
309 * Transport not already active...
310 * put at end of controller queue.
311 */
312 dp->b_forw = NULL;
d565635a 313 um = tedinfo[teunit]->ui_mi;
71236e46
BJ
314 if (um->um_tab.b_actf == NULL)
315 um->um_tab.b_actf = dp;
316 else
317 um->um_tab.b_actl->b_forw = dp;
318 um->um_tab.b_actl = dp;
319 } else
320 dp->b_actl->av_forw = bp;
321 dp->b_actl = bp;
322 /*
323 * If the controller is not busy, get
324 * it going.
325 */
326 if (um->um_tab.b_active == 0)
327 tmstart(um);
d75e5c3e
BJ
328 (void) spl0();
329}
330
71236e46
BJ
331/*
332 * Start activity on a tm controller.
333 */
334tmstart(um)
89bd2f01 335 register struct uba_ctlr *um;
d75e5c3e 336{
71236e46
BJ
337 register struct buf *bp, *dp;
338 register struct device *addr = (struct device *)um->um_addr;
d565635a 339 register struct te_softc *sc;
89bd2f01 340 register struct uba_device *ui;
d565635a 341 int teunit, cmd;
7e00c42b 342 daddr_t blkno;
d75e5c3e 343
71236e46
BJ
344 /*
345 * Look for an idle transport on the controller.
346 */
d75e5c3e 347loop:
71236e46 348 if ((dp = um->um_tab.b_actf) == NULL)
d75e5c3e 349 return;
71236e46
BJ
350 if ((bp = dp->b_actf) == NULL) {
351 um->um_tab.b_actf = dp->b_forw;
352 goto loop;
353 }
d565635a
BJ
354 teunit = TEUNIT(bp->b_dev);
355 ui = tedinfo[teunit];
71236e46
BJ
356 /*
357 * Record pre-transfer status (e.g. for TM_SENSE)
358 */
d565635a 359 sc = &te_softc[teunit];
71236e46
BJ
360 addr = (struct device *)um->um_addr;
361 addr->tmcs = (ui->ui_slave << 8);
7e00c42b
BJ
362 sc->sc_dsreg = addr->tmcs;
363 sc->sc_erreg = addr->tmer;
364 sc->sc_resid = addr->tmbc;
71236e46
BJ
365 /*
366 * Default is that last command was NOT a write command;
367 * if we do a write command we will notice this in tmintr().
368 */
369 sc->sc_lastiow = 1;
370 if (sc->sc_openf < 0 || (addr->tmcs&TM_CUR) == 0) {
371 /*
d565635a
BJ
372 * Have had a hard error on a non-raw tape
373 * or the tape unit is now unavailable
374 * (e.g. taken off line).
71236e46
BJ
375 */
376 bp->b_flags |= B_ERROR;
d75e5c3e
BJ
377 goto next;
378 }
d565635a
BJ
379 if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
380 /*
381 * Execute control operation with the specified count.
382 */
71236e46
BJ
383 if (bp->b_command == TM_SENSE)
384 goto next;
71236e46
BJ
385 um->um_tab.b_active =
386 bp->b_command == TM_REW ? SREW : SCOM;
387 if (bp->b_command == TM_SFORW || bp->b_command == TM_SREV)
388 addr->tmbc = bp->b_repcnt;
d1330646 389 goto dobpcmd;
71236e46 390 }
d565635a
BJ
391 /*
392 * The following checks handle boundary cases for operation
393 * on non-raw tapes. On raw tapes the initialization of
394 * sc->sc_nxrec by tmphys causes them to be skipped normally
395 * (except in the case of retries).
396 */
397 if (dbtofsb(bp->b_blkno) > sc->sc_nxrec) {
398 /*
399 * Can't read past known end-of-file.
400 */
401 bp->b_flags |= B_ERROR;
402 bp->b_error = ENXIO;
403 goto next;
404 }
405 if (dbtofsb(bp->b_blkno) == sc->sc_nxrec &&
406 bp->b_flags&B_READ) {
407 /*
408 * Reading at end of file returns 0 bytes.
409 */
410 bp->b_resid = bp->b_bcount;
411 clrbuf(bp);
412 goto next;
413 }
414 if ((bp->b_flags&B_READ) == 0)
415 /*
416 * Writing sets EOF
417 */
418 sc->sc_nxrec = dbtofsb(bp->b_blkno) + 1;
71236e46
BJ
419 /*
420 * If the data transfer command is in the correct place,
421 * set up all the registers except the csr, and give
422 * control over to the UNIBUS adapter routines, to
423 * wait for resources to start the i/o.
424 */
7e00c42b 425 if ((blkno = sc->sc_blkno) == dbtofsb(bp->b_blkno)) {
3f3a34c3 426 addr->tmbc = -bp->b_bcount;
d75e5c3e 427 if ((bp->b_flags&B_READ) == 0) {
7e00c42b 428 if (um->um_tab.b_errcnt)
d565635a 429 cmd = TM_WIRG;
d75e5c3e 430 else
d565635a 431 cmd = TM_WCOM;
d75e5c3e 432 } else
d565635a 433 cmd = TM_RCOM;
7e00c42b 434 um->um_tab.b_active = SIO;
d565635a 435 um->um_cmd = sc->sc_dens|cmd;
80905075 436#ifdef notdef
d1330646 437 if (tmreverseop(sc->sc_lastcmd))
d565635a 438 while (addr->tmer & TMER_SDWN)
d1330646 439 tmgapsdcnt++;
d1330646 440 sc->sc_lastcmd = TM_RCOM; /* will serve */
80905075 441#endif
a0eab615 442 (void) ubago(ui);
d75e5c3e
BJ
443 return;
444 }
71236e46 445 /*
d565635a
BJ
446 * Tape positioned incorrectly;
447 * set to seek forwards or backwards to the correct spot.
448 * This happens for raw tapes only on error retries.
71236e46 449 */
7e00c42b 450 um->um_tab.b_active = SSEEK;
d75e5c3e 451 if (blkno < dbtofsb(bp->b_blkno)) {
d1330646 452 bp->b_command = TM_SFORW;
3f3a34c3 453 addr->tmbc = blkno - dbtofsb(bp->b_blkno);
d75e5c3e 454 } else {
d1330646 455 bp->b_command = TM_SREV;
3f3a34c3 456 addr->tmbc = dbtofsb(bp->b_blkno) - blkno;
d75e5c3e 457 }
d1330646 458dobpcmd:
80905075 459#ifdef notdef
d565635a
BJ
460 /*
461 * It is strictly necessary to wait for the tape
462 * to stop before changing directions, but the TC11
463 * handles this for us.
464 */
d1330646
BJ
465 if (tmreverseop(sc->sc_lastcmd) != tmreverseop(bp->b_command))
466 while (addr->tmer & TM_SDWN)
467 tmgapsdcnt++;
d1330646 468 sc->sc_lastcmd = bp->b_command;
80905075 469#endif
d565635a
BJ
470 /*
471 * Do the command in bp.
472 */
473 addr->tmcs = (sc->sc_dens | bp->b_command);
d75e5c3e
BJ
474 return;
475
476next:
71236e46
BJ
477 /*
478 * Done with this operation due to error or
479 * the fact that it doesn't do anything.
480 * Release UBA resources (if any), dequeue
481 * the transfer and continue processing this slave.
482 */
483 if (um->um_ubinfo)
0801d37f 484 ubadone(um);
71236e46
BJ
485 um->um_tab.b_errcnt = 0;
486 dp->b_actf = bp->av_forw;
d75e5c3e
BJ
487 iodone(bp);
488 goto loop;
489}
490
71236e46
BJ
491/*
492 * The UNIBUS resources we needed have been
493 * allocated to us; start the device.
494 */
e4271c68 495tmdgo(um)
89bd2f01 496 register struct uba_ctlr *um;
3f3a34c3 497{
e4271c68 498 register struct device *addr = (struct device *)um->um_addr;
7e00c42b 499
e4271c68
BJ
500 addr->tmba = um->um_ubinfo;
501 addr->tmcs = um->um_cmd | ((um->um_ubinfo >> 12) & 0x30);
3f3a34c3
BJ
502}
503
71236e46
BJ
504/*
505 * Tm interrupt routine.
506 */
7e00c42b 507/*ARGSUSED*/
afb907f2
BJ
508tmintr(tm11)
509 int tm11;
d75e5c3e 510{
71236e46 511 struct buf *dp;
d75e5c3e 512 register struct buf *bp;
89bd2f01 513 register struct uba_ctlr *um = tmminfo[tm11];
d565635a
BJ
514 register struct device *addr;
515 register struct te_softc *sc;
516 int teunit;
d75e5c3e
BJ
517 register state;
518
d565635a
BJ
519 if ((dp = um->um_tab.b_actf) == NULL)
520 return;
521 bp = dp->b_actf;
522 teunit = TEUNIT(bp->b_dev);
523 addr = (struct device *)tedinfo[teunit]->ui_addr;
71236e46
BJ
524 /*
525 * If last command was a rewind, and tape is still
526 * rewinding, wait for the rewind complete interrupt.
527 */
528 if (um->um_tab.b_active == SREW) {
529 um->um_tab.b_active = SCOM;
d565635a 530 if (addr->tmer&TMER_RWS)
71236e46 531 return;
d75e5c3e 532 }
71236e46
BJ
533 /*
534 * An operation completed... record status
535 */
d565635a 536 sc = &te_softc[teunit];
7e00c42b
BJ
537 sc->sc_dsreg = addr->tmcs;
538 sc->sc_erreg = addr->tmer;
539 sc->sc_resid = addr->tmbc;
d75e5c3e 540 if ((bp->b_flags & B_READ) == 0)
71236e46 541 sc->sc_lastiow = 1;
7e00c42b
BJ
542 state = um->um_tab.b_active;
543 um->um_tab.b_active = 0;
71236e46
BJ
544 /*
545 * Check for errors.
546 */
547 if (addr->tmcs&TM_ERR) {
d565635a 548 while (addr->tmer & TMER_SDWN)
d75e5c3e 549 ; /* await settle down */
71236e46 550 /*
d565635a 551 * If we hit the end of the tape file, update our position.
71236e46 552 */
d565635a 553 if (addr->tmer&TMER_EOF) {
71236e46
BJ
554 tmseteof(bp); /* set blkno and nxrec */
555 state = SCOM; /* force completion */
556 /*
557 * Stuff bc so it will be unstuffed correctly
558 * later to get resid.
559 */
3f3a34c3 560 addr->tmbc = -bp->b_bcount;
71236e46 561 goto opdone;
d75e5c3e 562 }
71236e46 563 /*
d565635a
BJ
564 * If we were reading raw tape and the only error was that the
565 * record was too long, then we don't consider this an error.
71236e46 566 */
d565635a
BJ
567 if (bp == &rtmbuf[TMUNIT(bp->b_dev)] && (bp->b_flags&B_READ) &&
568 (addr->tmer&(TMER_HARD|TMER_SOFT)) == TMER_RLE)
71236e46
BJ
569 goto ignoreerr;
570 /*
571 * If error is not hard, and this was an i/o operation
572 * retry up to 8 times.
573 */
d565635a 574 if ((addr->tmer&TMER_HARD)==0 && state==SIO) {
7e00c42b 575 if (++um->um_tab.b_errcnt < 7) {
7e00c42b 576 sc->sc_blkno++;
0801d37f 577 ubadone(um);
71236e46 578 goto opcont;
d75e5c3e 579 }
71236e46
BJ
580 } else
581 /*
582 * Hard or non-i/o errors on non-raw tape
583 * cause it to close.
584 */
d565635a 585 if (sc->sc_openf>0 && bp != &rtmbuf[TMUNIT(bp->b_dev)])
71236e46
BJ
586 sc->sc_openf = -1;
587 /*
588 * Couldn't recover error
589 */
80905075 590 printf("te%d: hard error bn%d er=%b\n", minor(bp->b_dev)&03,
d565635a 591 bp->b_blkno, sc->sc_erreg, TMER_BITS);
d75e5c3e 592 bp->b_flags |= B_ERROR;
71236e46 593 goto opdone;
d75e5c3e 594 }
71236e46
BJ
595 /*
596 * Advance tape control FSM.
597 */
598ignoreerr:
d75e5c3e
BJ
599 switch (state) {
600
601 case SIO:
71236e46
BJ
602 /*
603 * Read/write increments tape block number
604 */
7e00c42b 605 sc->sc_blkno++;
71236e46 606 goto opdone;
d75e5c3e
BJ
607
608 case SCOM:
71236e46 609 /*
d565635a 610 * For forward/backward space record update current position.
71236e46 611 */
d565635a 612 if (bp == &ctmbuf[TMUNIT(bp->b_dev)])
71236e46
BJ
613 switch (bp->b_command) {
614
615 case TM_SFORW:
616 sc->sc_blkno -= bp->b_repcnt;
d565635a 617 break;
71236e46
BJ
618
619 case TM_SREV:
620 sc->sc_blkno += bp->b_repcnt;
d565635a 621 break;
d75e5c3e 622 }
d565635a 623 goto opdone;
d75e5c3e
BJ
624
625 case SSEEK:
7e00c42b 626 sc->sc_blkno = dbtofsb(bp->b_blkno);
71236e46 627 goto opcont;
d75e5c3e
BJ
628
629 default:
71236e46 630 panic("tmintr");
d75e5c3e 631 }
71236e46
BJ
632opdone:
633 /*
634 * Reset error count and remove
635 * from device queue.
636 */
637 um->um_tab.b_errcnt = 0;
638 dp->b_actf = bp->av_forw;
639 bp->b_resid = -addr->tmbc;
0801d37f 640 ubadone(um);
71236e46
BJ
641 iodone(bp);
642 /*
643 * Circulate slave to end of controller
644 * queue to give other slaves a chance.
645 */
646 um->um_tab.b_actf = dp->b_forw;
647 if (dp->b_actf) {
648 dp->b_forw = NULL;
649 if (um->um_tab.b_actf == NULL)
650 um->um_tab.b_actf = dp;
651 else
652 um->um_tab.b_actl->b_forw = dp;
653 um->um_tab.b_actl = dp;
654 }
655 if (um->um_tab.b_actf == 0)
656 return;
657opcont:
658 tmstart(um);
d75e5c3e
BJ
659}
660
661tmseteof(bp)
662 register struct buf *bp;
663{
d565635a 664 register int teunit = TEUNIT(bp->b_dev);
3f3a34c3 665 register struct device *addr =
d565635a
BJ
666 (struct device *)tedinfo[teunit]->ui_addr;
667 register struct te_softc *sc = &te_softc[teunit];
d75e5c3e 668
d565635a 669 if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
7e00c42b 670 if (sc->sc_blkno > dbtofsb(bp->b_blkno)) {
d75e5c3e 671 /* reversing */
7e00c42b
BJ
672 sc->sc_nxrec = dbtofsb(bp->b_blkno) - addr->tmbc;
673 sc->sc_blkno = sc->sc_nxrec;
d75e5c3e
BJ
674 } else {
675 /* spacing forward */
7e00c42b
BJ
676 sc->sc_blkno = dbtofsb(bp->b_blkno) + addr->tmbc;
677 sc->sc_nxrec = sc->sc_blkno - 1;
d75e5c3e
BJ
678 }
679 return;
680 }
681 /* eof on read */
7e00c42b 682 sc->sc_nxrec = dbtofsb(bp->b_blkno);
d75e5c3e
BJ
683}
684
685tmread(dev)
71236e46 686 dev_t dev;
d75e5c3e
BJ
687{
688
689 tmphys(dev);
89bd2f01
BJ
690 if (u.u_error)
691 return;
71236e46 692 physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_READ, minphys);
d75e5c3e
BJ
693}
694
695tmwrite(dev)
71236e46 696 dev_t dev;
d75e5c3e
BJ
697{
698
699 tmphys(dev);
89bd2f01
BJ
700 if (u.u_error)
701 return;
71236e46 702 physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_WRITE, minphys);
d75e5c3e
BJ
703}
704
d565635a
BJ
705/*
706 * Check that a raw device exists.
707 * If it does, set up sc_blkno and sc_nxrec
708 * so that the tape will appear positioned correctly.
709 */
d75e5c3e 710tmphys(dev)
71236e46 711 dev_t dev;
d75e5c3e 712{
d565635a 713 register int teunit = TEUNIT(dev);
d75e5c3e 714 register daddr_t a;
d565635a
BJ
715 register struct te_softc *sc;
716 register struct uba_device *ui;
d75e5c3e 717
d565635a 718 if (teunit >= NTE || (ui=tedinfo[teunit]) == 0 || ui->ui_alive == 0) {
89bd2f01
BJ
719 u.u_error = ENXIO;
720 return;
721 }
d565635a 722 sc = &te_softc[teunit];
d75e5c3e 723 a = dbtofsb(u.u_offset >> 9);
7e00c42b
BJ
724 sc->sc_blkno = a;
725 sc->sc_nxrec = a + 1;
d75e5c3e
BJ
726}
727
71236e46
BJ
728tmreset(uban)
729 int uban;
730{
89bd2f01 731 register struct uba_ctlr *um;
d565635a 732 register tm11, teunit;
89bd2f01 733 register struct uba_device *ui;
71236e46
BJ
734 register struct buf *dp;
735
afb907f2
BJ
736 for (tm11 = 0; tm11 < NTM; tm11++) {
737 if ((um = tmminfo[tm11]) == 0 || um->um_alive == 0 ||
71236e46
BJ
738 um->um_ubanum != uban)
739 continue;
80905075 740 printf(" tm%d", tm11);
71236e46
BJ
741 um->um_tab.b_active = 0;
742 um->um_tab.b_actf = um->um_tab.b_actl = 0;
743 if (um->um_ubinfo) {
744 printf("<%d>", (um->um_ubinfo>>28)&0xf);
0801d37f 745 ubadone(um);
71236e46
BJ
746 }
747 ((struct device *)(um->um_addr))->tmcs = TM_DCLR;
d565635a
BJ
748 for (teunit = 0; teunit < NTE; teunit++) {
749 if ((ui = tedinfo[teunit]) == 0 || ui->ui_mi != um ||
750 ui->ui_alive == 0)
71236e46 751 continue;
d565635a 752 dp = &teutab[teunit];
71236e46
BJ
753 dp->b_active = 0;
754 dp->b_forw = 0;
755 if (um->um_tab.b_actf == NULL)
756 um->um_tab.b_actf = dp;
757 else
758 um->um_tab.b_actl->b_forw = dp;
759 um->um_tab.b_actl = dp;
d565635a 760 te_softc[teunit].sc_openf = -1;
71236e46
BJ
761 }
762 tmstart(um);
763 }
764}
765
d75e5c3e
BJ
766/*ARGSUSED*/
767tmioctl(dev, cmd, addr, flag)
768 caddr_t addr;
769 dev_t dev;
770{
d565635a
BJ
771 int teunit = TEUNIT(dev);
772 register struct te_softc *sc = &te_softc[teunit];
773 register struct buf *bp = &ctmbuf[TMUNIT(dev)];
d75e5c3e
BJ
774 register callcount;
775 int fcount;
776 struct mtop mtop;
777 struct mtget mtget;
778 /* we depend of the values and order of the MT codes here */
71236e46
BJ
779 static tmops[] =
780 {TM_WEOF,TM_SFORW,TM_SREV,TM_SFORW,TM_SREV,TM_REW,TM_OFFL,TM_SENSE};
d75e5c3e 781
71236e46 782 switch (cmd) {
d75e5c3e
BJ
783 case MTIOCTOP: /* tape operation */
784 if (copyin((caddr_t)addr, (caddr_t)&mtop, sizeof(mtop))) {
785 u.u_error = EFAULT;
786 return;
787 }
788 switch(mtop.mt_op) {
71236e46
BJ
789 case MTWEOF:
790 callcount = mtop.mt_count;
791 fcount = 1;
792 break;
793 case MTFSF: case MTBSF:
d75e5c3e
BJ
794 callcount = mtop.mt_count;
795 fcount = INF;
796 break;
797 case MTFSR: case MTBSR:
798 callcount = 1;
799 fcount = mtop.mt_count;
800 break;
77115c00 801 case MTREW: case MTOFFL: case MTNOP:
d75e5c3e
BJ
802 callcount = 1;
803 fcount = 1;
804 break;
805 default:
806 u.u_error = ENXIO;
807 return;
808 }
71236e46 809 if (callcount <= 0 || fcount <= 0) {
d75e5c3e 810 u.u_error = ENXIO;
71236e46
BJ
811 return;
812 }
813 while (--callcount >= 0) {
e4271c68 814 tmcommand(dev, tmops[mtop.mt_op], fcount);
d75e5c3e 815 if ((mtop.mt_op == MTFSR || mtop.mt_op == MTBSR) &&
71236e46 816 bp->b_resid) {
d75e5c3e
BJ
817 u.u_error = EIO;
818 break;
819 }
d565635a 820 if ((bp->b_flags&B_ERROR) || sc->sc_erreg&TMER_BOT)
d75e5c3e
BJ
821 break;
822 }
71236e46 823 geterror(bp);
d75e5c3e
BJ
824 return;
825 case MTIOCGET:
7e00c42b
BJ
826 mtget.mt_dsreg = sc->sc_dsreg;
827 mtget.mt_erreg = sc->sc_erreg;
828 mtget.mt_resid = sc->sc_resid;
d75e5c3e
BJ
829 if (copyout((caddr_t)&mtget, addr, sizeof(mtget)))
830 u.u_error = EFAULT;
831 return;
832 default:
833 u.u_error = ENXIO;
834 }
835}
836
837#define DBSIZE 20
838
7455bf3e 839tmdump()
d75e5c3e 840{
89bd2f01 841 register struct uba_device *ui;
3f3a34c3
BJ
842 register struct uba_regs *up;
843 register struct device *addr;
5aa9d5ea
RE
844 int blk, num;
845 int start;
d75e5c3e 846
5aa9d5ea
RE
847 start = 0;
848 num = maxfree;
849#define phys(a,b) ((b)((int)(a)&0x7fffffff))
d565635a 850 if (tedinfo[0] == 0)
2536c16c 851 return (ENXIO);
d565635a 852 ui = phys(tedinfo[0], struct uba_device *);
3f3a34c3
BJ
853 up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
854#if VAX780
855 if (cpu == VAX_780)
856 ubainit(up);
d75e5c3e 857#endif
77115c00 858 DELAY(1000000);
3f3a34c3
BJ
859 addr = (struct device *)ui->ui_physaddr;
860 tmwait(addr);
71236e46 861 addr->tmcs = TM_DCLR | TM_GO;
d75e5c3e
BJ
862 while (num > 0) {
863 blk = num > DBSIZE ? DBSIZE : num;
3f3a34c3 864 tmdwrite(start, blk, addr, up);
d75e5c3e
BJ
865 start += blk;
866 num -= blk;
867 }
5aa9d5ea
RE
868 tmeof(addr);
869 tmeof(addr);
7e00c42b 870 tmwait(addr);
2536c16c
BJ
871 if (addr->tmcs&TM_ERR)
872 return (EIO);
71236e46 873 addr->tmcs = TM_REW | TM_GO;
5aa9d5ea 874 tmwait(addr);
7455bf3e 875 return (0);
d75e5c3e
BJ
876}
877
71236e46
BJ
878tmdwrite(dbuf, num, addr, up)
879 register dbuf, num;
3f3a34c3
BJ
880 register struct device *addr;
881 struct uba_regs *up;
d75e5c3e 882{
3f3a34c3
BJ
883 register struct pte *io;
884 register int npf;
0a51498e 885
3f3a34c3 886 tmwait(addr);
3f3a34c3 887 io = up->uba_map;
d75e5c3e 888 npf = num+1;
0a51498e 889 while (--npf != 0)
89bd2f01 890 *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV);
3f3a34c3
BJ
891 *(int *)io = 0;
892 addr->tmbc = -(num*NBPG);
893 addr->tmba = 0;
71236e46 894 addr->tmcs = TM_WCOM | TM_GO;
d75e5c3e
BJ
895}
896
3f3a34c3
BJ
897tmwait(addr)
898 register struct device *addr;
d75e5c3e 899{
0a51498e 900 register s;
d75e5c3e
BJ
901
902 do
3f3a34c3 903 s = addr->tmcs;
71236e46 904 while ((s & TM_CUR) == 0);
d75e5c3e
BJ
905}
906
3f3a34c3
BJ
907tmeof(addr)
908 struct device *addr;
d75e5c3e
BJ
909{
910
3f3a34c3 911 tmwait(addr);
71236e46 912 addr->tmcs = TM_WEOF | TM_GO;
d75e5c3e
BJ
913}
914#endif