debugging code for NXM problem added
[unix-history] / usr / src / sys / vax / uba / tm.c
CommitLineData
bc9da2ca 1/* tm.c 4.33 81/04/08 */
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
d565635a
BJ
12 * what happens if you offline tape during rewind?
13 * test using file system on tape
d75e5c3e 14 */
d75e5c3e 15#include "../h/param.h"
27a897a2 16#include "../h/systm.h"
d75e5c3e
BJ
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 188 register struct te_softc *sc;
971424d9 189 int olddens, 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 }
971424d9
BJ
197 olddens = sc->sc_dens;
198 dens = TM_IE | TM_GO | (ui->ui_slave << 8);
199 if ((minor(dev) & T_1600BPI) == 0)
200 dens |= TM_D800;
201 sc->sc_dens = dens;
27a897a2 202get:
71236e46 203 tmcommand(dev, TM_SENSE, 1);
27a897a2
BJ
204 if (sc->sc_erreg&TMER_SDWN) {
205 sleep((caddr_t)&lbolt, PZERO+1);
206 goto get;
207 }
971424d9 208 sc->sc_dens = olddens;
d565635a 209 if ((sc->sc_erreg&(TMER_SELR|TMER_TUR)) != (TMER_SELR|TMER_TUR) ||
4fd4cf52 210 (flag&FWRITE) && (sc->sc_erreg&TMER_WRL) ||
d565635a 211 (sc->sc_erreg&TMER_BOT) == 0 && (flag&FWRITE) &&
4fd4cf52 212 dens != sc->sc_dens) {
d565635a
BJ
213 /*
214 * Not online or density switch in mid-tape or write locked.
215 */
71236e46 216 u.u_error = EIO;
d75e5c3e
BJ
217 return;
218 }
71236e46 219 sc->sc_openf = 1;
7e00c42b
BJ
220 sc->sc_blkno = (daddr_t)0;
221 sc->sc_nxrec = INF;
71236e46 222 sc->sc_lastiow = 0;
d565635a 223 sc->sc_dens = dens;
d75e5c3e
BJ
224}
225
71236e46
BJ
226/*
227 * Close tape device.
228 *
229 * If tape was open for writing or last operation was
230 * a write, then write two EOF's and backspace over the last one.
231 * Unless this is a non-rewinding special file, rewind the tape.
232 * Make the tape available to others.
233 */
d75e5c3e
BJ
234tmclose(dev, flag)
235 register dev_t dev;
236 register flag;
237{
d565635a 238 register struct te_softc *sc = &te_softc[TEUNIT(dev)];
d75e5c3e 239
71236e46
BJ
240 if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
241 tmcommand(dev, TM_WEOF, 1);
242 tmcommand(dev, TM_WEOF, 1);
243 tmcommand(dev, TM_SREV, 1);
d75e5c3e
BJ
244 }
245 if ((minor(dev)&T_NOREWIND) == 0)
d565635a
BJ
246 /*
247 * 0 count means don't hang waiting for rewind complete
248 * rather ctmbuf stays busy until the operation completes
249 * preventing further opens from completing by
250 * preventing a TM_SENSE from completing.
251 */
252 tmcommand(dev, TM_REW, 0);
7e00c42b 253 sc->sc_openf = 0;
d75e5c3e
BJ
254}
255
71236e46
BJ
256/*
257 * Execute a command on the tape drive
258 * a specified number of times.
259 */
e4271c68 260tmcommand(dev, com, count)
d75e5c3e
BJ
261 dev_t dev;
262 int com, count;
263{
264 register struct buf *bp;
265
71236e46 266 bp = &ctmbuf[TMUNIT(dev)];
d75e5c3e
BJ
267 (void) spl5();
268 while (bp->b_flags&B_BUSY) {
d565635a
BJ
269 /*
270 * This special check is because B_BUSY never
271 * gets cleared in the non-waiting rewind case.
272 */
27a897a2 273 if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
d565635a 274 break;
d75e5c3e
BJ
275 bp->b_flags |= B_WANTED;
276 sleep((caddr_t)bp, PRIBIO);
277 }
278 bp->b_flags = B_BUSY|B_READ;
279 (void) spl0();
280 bp->b_dev = dev;
281 bp->b_repcnt = -count;
282 bp->b_command = com;
283 bp->b_blkno = 0;
284 tmstrategy(bp);
d565635a
BJ
285 /*
286 * In case of rewind from close, don't wait.
287 * This is the only case where count can be 0.
288 */
289 if (count == 0)
290 return;
d75e5c3e
BJ
291 iowait(bp);
292 if (bp->b_flags&B_WANTED)
293 wakeup((caddr_t)bp);
294 bp->b_flags &= B_ERROR;
295}
296
71236e46 297/*
d565635a 298 * Queue a tape operation.
71236e46 299 */
d75e5c3e
BJ
300tmstrategy(bp)
301 register struct buf *bp;
302{
d565635a 303 int teunit = TEUNIT(bp->b_dev);
89bd2f01 304 register struct uba_ctlr *um;
71236e46 305 register struct buf *dp;
d75e5c3e 306
71236e46
BJ
307 /*
308 * Put transfer at end of unit queue
309 */
d565635a 310 dp = &teutab[teunit];
d75e5c3e
BJ
311 bp->av_forw = NULL;
312 (void) spl5();
71236e46
BJ
313 if (dp->b_actf == NULL) {
314 dp->b_actf = bp;
315 /*
316 * Transport not already active...
317 * put at end of controller queue.
318 */
319 dp->b_forw = NULL;
d565635a 320 um = tedinfo[teunit]->ui_mi;
71236e46
BJ
321 if (um->um_tab.b_actf == NULL)
322 um->um_tab.b_actf = dp;
323 else
324 um->um_tab.b_actl->b_forw = dp;
325 um->um_tab.b_actl = dp;
326 } else
327 dp->b_actl->av_forw = bp;
328 dp->b_actl = bp;
329 /*
330 * If the controller is not busy, get
331 * it going.
332 */
333 if (um->um_tab.b_active == 0)
334 tmstart(um);
d75e5c3e
BJ
335 (void) spl0();
336}
337
71236e46
BJ
338/*
339 * Start activity on a tm controller.
340 */
341tmstart(um)
89bd2f01 342 register struct uba_ctlr *um;
d75e5c3e 343{
71236e46
BJ
344 register struct buf *bp, *dp;
345 register struct device *addr = (struct device *)um->um_addr;
d565635a 346 register struct te_softc *sc;
89bd2f01 347 register struct uba_device *ui;
d565635a 348 int teunit, cmd;
7e00c42b 349 daddr_t blkno;
d75e5c3e 350
71236e46
BJ
351 /*
352 * Look for an idle transport on the controller.
353 */
d75e5c3e 354loop:
71236e46 355 if ((dp = um->um_tab.b_actf) == NULL)
d75e5c3e 356 return;
71236e46
BJ
357 if ((bp = dp->b_actf) == NULL) {
358 um->um_tab.b_actf = dp->b_forw;
359 goto loop;
360 }
d565635a
BJ
361 teunit = TEUNIT(bp->b_dev);
362 ui = tedinfo[teunit];
71236e46
BJ
363 /*
364 * Record pre-transfer status (e.g. for TM_SENSE)
365 */
d565635a 366 sc = &te_softc[teunit];
71236e46
BJ
367 addr = (struct device *)um->um_addr;
368 addr->tmcs = (ui->ui_slave << 8);
7e00c42b
BJ
369 sc->sc_dsreg = addr->tmcs;
370 sc->sc_erreg = addr->tmer;
371 sc->sc_resid = addr->tmbc;
71236e46
BJ
372 /*
373 * Default is that last command was NOT a write command;
374 * if we do a write command we will notice this in tmintr().
375 */
bc9da2ca 376 sc->sc_lastiow = 0;
71236e46
BJ
377 if (sc->sc_openf < 0 || (addr->tmcs&TM_CUR) == 0) {
378 /*
d565635a
BJ
379 * Have had a hard error on a non-raw tape
380 * or the tape unit is now unavailable
381 * (e.g. taken off line).
71236e46
BJ
382 */
383 bp->b_flags |= B_ERROR;
d75e5c3e
BJ
384 goto next;
385 }
d565635a
BJ
386 if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
387 /*
388 * Execute control operation with the specified count.
389 */
71236e46
BJ
390 if (bp->b_command == TM_SENSE)
391 goto next;
71236e46
BJ
392 um->um_tab.b_active =
393 bp->b_command == TM_REW ? SREW : SCOM;
394 if (bp->b_command == TM_SFORW || bp->b_command == TM_SREV)
395 addr->tmbc = bp->b_repcnt;
d1330646 396 goto dobpcmd;
71236e46 397 }
d565635a
BJ
398 /*
399 * The following checks handle boundary cases for operation
400 * on non-raw tapes. On raw tapes the initialization of
401 * sc->sc_nxrec by tmphys causes them to be skipped normally
402 * (except in the case of retries).
403 */
404 if (dbtofsb(bp->b_blkno) > sc->sc_nxrec) {
405 /*
406 * Can't read past known end-of-file.
407 */
408 bp->b_flags |= B_ERROR;
409 bp->b_error = ENXIO;
410 goto next;
411 }
412 if (dbtofsb(bp->b_blkno) == sc->sc_nxrec &&
413 bp->b_flags&B_READ) {
414 /*
415 * Reading at end of file returns 0 bytes.
416 */
417 bp->b_resid = bp->b_bcount;
418 clrbuf(bp);
419 goto next;
420 }
421 if ((bp->b_flags&B_READ) == 0)
422 /*
423 * Writing sets EOF
424 */
425 sc->sc_nxrec = dbtofsb(bp->b_blkno) + 1;
71236e46
BJ
426 /*
427 * If the data transfer command is in the correct place,
428 * set up all the registers except the csr, and give
429 * control over to the UNIBUS adapter routines, to
430 * wait for resources to start the i/o.
431 */
7e00c42b 432 if ((blkno = sc->sc_blkno) == dbtofsb(bp->b_blkno)) {
3f3a34c3 433 addr->tmbc = -bp->b_bcount;
d75e5c3e 434 if ((bp->b_flags&B_READ) == 0) {
7e00c42b 435 if (um->um_tab.b_errcnt)
d565635a 436 cmd = TM_WIRG;
d75e5c3e 437 else
d565635a 438 cmd = TM_WCOM;
d75e5c3e 439 } else
d565635a 440 cmd = TM_RCOM;
7e00c42b 441 um->um_tab.b_active = SIO;
d565635a 442 um->um_cmd = sc->sc_dens|cmd;
80905075 443#ifdef notdef
d1330646 444 if (tmreverseop(sc->sc_lastcmd))
d565635a 445 while (addr->tmer & TMER_SDWN)
d1330646 446 tmgapsdcnt++;
d1330646 447 sc->sc_lastcmd = TM_RCOM; /* will serve */
80905075 448#endif
a0eab615 449 (void) ubago(ui);
d75e5c3e
BJ
450 return;
451 }
71236e46 452 /*
d565635a
BJ
453 * Tape positioned incorrectly;
454 * set to seek forwards or backwards to the correct spot.
455 * This happens for raw tapes only on error retries.
71236e46 456 */
7e00c42b 457 um->um_tab.b_active = SSEEK;
d75e5c3e 458 if (blkno < dbtofsb(bp->b_blkno)) {
d1330646 459 bp->b_command = TM_SFORW;
3f3a34c3 460 addr->tmbc = blkno - dbtofsb(bp->b_blkno);
d75e5c3e 461 } else {
d1330646 462 bp->b_command = TM_SREV;
3f3a34c3 463 addr->tmbc = dbtofsb(bp->b_blkno) - blkno;
d75e5c3e 464 }
d1330646 465dobpcmd:
80905075 466#ifdef notdef
d565635a
BJ
467 /*
468 * It is strictly necessary to wait for the tape
469 * to stop before changing directions, but the TC11
470 * handles this for us.
471 */
d1330646
BJ
472 if (tmreverseop(sc->sc_lastcmd) != tmreverseop(bp->b_command))
473 while (addr->tmer & TM_SDWN)
474 tmgapsdcnt++;
d1330646 475 sc->sc_lastcmd = bp->b_command;
80905075 476#endif
d565635a
BJ
477 /*
478 * Do the command in bp.
479 */
480 addr->tmcs = (sc->sc_dens | bp->b_command);
d75e5c3e
BJ
481 return;
482
483next:
71236e46
BJ
484 /*
485 * Done with this operation due to error or
486 * the fact that it doesn't do anything.
487 * Release UBA resources (if any), dequeue
488 * the transfer and continue processing this slave.
489 */
490 if (um->um_ubinfo)
0801d37f 491 ubadone(um);
71236e46
BJ
492 um->um_tab.b_errcnt = 0;
493 dp->b_actf = bp->av_forw;
d75e5c3e
BJ
494 iodone(bp);
495 goto loop;
496}
497
71236e46
BJ
498/*
499 * The UNIBUS resources we needed have been
500 * allocated to us; start the device.
501 */
e4271c68 502tmdgo(um)
89bd2f01 503 register struct uba_ctlr *um;
3f3a34c3 504{
e4271c68 505 register struct device *addr = (struct device *)um->um_addr;
7e00c42b 506
e4271c68
BJ
507 addr->tmba = um->um_ubinfo;
508 addr->tmcs = um->um_cmd | ((um->um_ubinfo >> 12) & 0x30);
3f3a34c3
BJ
509}
510
71236e46
BJ
511/*
512 * Tm interrupt routine.
513 */
7e00c42b 514/*ARGSUSED*/
afb907f2
BJ
515tmintr(tm11)
516 int tm11;
d75e5c3e 517{
71236e46 518 struct buf *dp;
d75e5c3e 519 register struct buf *bp;
89bd2f01 520 register struct uba_ctlr *um = tmminfo[tm11];
d565635a
BJ
521 register struct device *addr;
522 register struct te_softc *sc;
523 int teunit;
d75e5c3e
BJ
524 register state;
525
d565635a
BJ
526 if ((dp = um->um_tab.b_actf) == NULL)
527 return;
528 bp = dp->b_actf;
529 teunit = TEUNIT(bp->b_dev);
530 addr = (struct device *)tedinfo[teunit]->ui_addr;
71236e46
BJ
531 /*
532 * If last command was a rewind, and tape is still
533 * rewinding, wait for the rewind complete interrupt.
534 */
535 if (um->um_tab.b_active == SREW) {
536 um->um_tab.b_active = SCOM;
d565635a 537 if (addr->tmer&TMER_RWS)
71236e46 538 return;
d75e5c3e 539 }
71236e46
BJ
540 /*
541 * An operation completed... record status
542 */
d565635a 543 sc = &te_softc[teunit];
7e00c42b
BJ
544 sc->sc_dsreg = addr->tmcs;
545 sc->sc_erreg = addr->tmer;
546 sc->sc_resid = addr->tmbc;
d75e5c3e 547 if ((bp->b_flags & B_READ) == 0)
71236e46 548 sc->sc_lastiow = 1;
7e00c42b
BJ
549 state = um->um_tab.b_active;
550 um->um_tab.b_active = 0;
71236e46
BJ
551 /*
552 * Check for errors.
553 */
554 if (addr->tmcs&TM_ERR) {
d565635a 555 while (addr->tmer & TMER_SDWN)
d75e5c3e 556 ; /* await settle down */
71236e46 557 /*
d565635a 558 * If we hit the end of the tape file, update our position.
71236e46 559 */
d565635a 560 if (addr->tmer&TMER_EOF) {
71236e46
BJ
561 tmseteof(bp); /* set blkno and nxrec */
562 state = SCOM; /* force completion */
563 /*
564 * Stuff bc so it will be unstuffed correctly
565 * later to get resid.
566 */
3f3a34c3 567 addr->tmbc = -bp->b_bcount;
71236e46 568 goto opdone;
d75e5c3e 569 }
71236e46 570 /*
d565635a
BJ
571 * If we were reading raw tape and the only error was that the
572 * record was too long, then we don't consider this an error.
71236e46 573 */
d565635a
BJ
574 if (bp == &rtmbuf[TMUNIT(bp->b_dev)] && (bp->b_flags&B_READ) &&
575 (addr->tmer&(TMER_HARD|TMER_SOFT)) == TMER_RLE)
71236e46
BJ
576 goto ignoreerr;
577 /*
578 * If error is not hard, and this was an i/o operation
579 * retry up to 8 times.
580 */
d565635a 581 if ((addr->tmer&TMER_HARD)==0 && state==SIO) {
7e00c42b 582 if (++um->um_tab.b_errcnt < 7) {
7e00c42b 583 sc->sc_blkno++;
0801d37f 584 ubadone(um);
71236e46 585 goto opcont;
d75e5c3e 586 }
71236e46
BJ
587 } else
588 /*
589 * Hard or non-i/o errors on non-raw tape
590 * cause it to close.
591 */
d565635a 592 if (sc->sc_openf>0 && bp != &rtmbuf[TMUNIT(bp->b_dev)])
71236e46
BJ
593 sc->sc_openf = -1;
594 /*
595 * Couldn't recover error
596 */
80905075 597 printf("te%d: hard error bn%d er=%b\n", minor(bp->b_dev)&03,
d565635a 598 bp->b_blkno, sc->sc_erreg, TMER_BITS);
d75e5c3e 599 bp->b_flags |= B_ERROR;
71236e46 600 goto opdone;
d75e5c3e 601 }
71236e46
BJ
602 /*
603 * Advance tape control FSM.
604 */
605ignoreerr:
d75e5c3e
BJ
606 switch (state) {
607
608 case SIO:
71236e46
BJ
609 /*
610 * Read/write increments tape block number
611 */
7e00c42b 612 sc->sc_blkno++;
71236e46 613 goto opdone;
d75e5c3e
BJ
614
615 case SCOM:
71236e46 616 /*
d565635a 617 * For forward/backward space record update current position.
71236e46 618 */
d565635a 619 if (bp == &ctmbuf[TMUNIT(bp->b_dev)])
71236e46
BJ
620 switch (bp->b_command) {
621
622 case TM_SFORW:
623 sc->sc_blkno -= bp->b_repcnt;
d565635a 624 break;
71236e46
BJ
625
626 case TM_SREV:
627 sc->sc_blkno += bp->b_repcnt;
d565635a 628 break;
d75e5c3e 629 }
d565635a 630 goto opdone;
d75e5c3e
BJ
631
632 case SSEEK:
7e00c42b 633 sc->sc_blkno = dbtofsb(bp->b_blkno);
71236e46 634 goto opcont;
d75e5c3e
BJ
635
636 default:
71236e46 637 panic("tmintr");
d75e5c3e 638 }
71236e46
BJ
639opdone:
640 /*
641 * Reset error count and remove
642 * from device queue.
643 */
644 um->um_tab.b_errcnt = 0;
645 dp->b_actf = bp->av_forw;
646 bp->b_resid = -addr->tmbc;
0801d37f 647 ubadone(um);
71236e46
BJ
648 iodone(bp);
649 /*
650 * Circulate slave to end of controller
651 * queue to give other slaves a chance.
652 */
653 um->um_tab.b_actf = dp->b_forw;
654 if (dp->b_actf) {
655 dp->b_forw = NULL;
656 if (um->um_tab.b_actf == NULL)
657 um->um_tab.b_actf = dp;
658 else
659 um->um_tab.b_actl->b_forw = dp;
660 um->um_tab.b_actl = dp;
661 }
662 if (um->um_tab.b_actf == 0)
663 return;
664opcont:
665 tmstart(um);
d75e5c3e
BJ
666}
667
668tmseteof(bp)
669 register struct buf *bp;
670{
d565635a 671 register int teunit = TEUNIT(bp->b_dev);
3f3a34c3 672 register struct device *addr =
d565635a
BJ
673 (struct device *)tedinfo[teunit]->ui_addr;
674 register struct te_softc *sc = &te_softc[teunit];
d75e5c3e 675
d565635a 676 if (bp == &ctmbuf[TMUNIT(bp->b_dev)]) {
7e00c42b 677 if (sc->sc_blkno > dbtofsb(bp->b_blkno)) {
d75e5c3e 678 /* reversing */
7e00c42b
BJ
679 sc->sc_nxrec = dbtofsb(bp->b_blkno) - addr->tmbc;
680 sc->sc_blkno = sc->sc_nxrec;
d75e5c3e
BJ
681 } else {
682 /* spacing forward */
7e00c42b
BJ
683 sc->sc_blkno = dbtofsb(bp->b_blkno) + addr->tmbc;
684 sc->sc_nxrec = sc->sc_blkno - 1;
d75e5c3e
BJ
685 }
686 return;
687 }
688 /* eof on read */
7e00c42b 689 sc->sc_nxrec = dbtofsb(bp->b_blkno);
d75e5c3e
BJ
690}
691
692tmread(dev)
71236e46 693 dev_t dev;
d75e5c3e
BJ
694{
695
696 tmphys(dev);
89bd2f01
BJ
697 if (u.u_error)
698 return;
71236e46 699 physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_READ, minphys);
d75e5c3e
BJ
700}
701
702tmwrite(dev)
71236e46 703 dev_t dev;
d75e5c3e
BJ
704{
705
706 tmphys(dev);
89bd2f01
BJ
707 if (u.u_error)
708 return;
71236e46 709 physio(tmstrategy, &rtmbuf[TMUNIT(dev)], dev, B_WRITE, minphys);
d75e5c3e
BJ
710}
711
d565635a
BJ
712/*
713 * Check that a raw device exists.
714 * If it does, set up sc_blkno and sc_nxrec
715 * so that the tape will appear positioned correctly.
716 */
d75e5c3e 717tmphys(dev)
71236e46 718 dev_t dev;
d75e5c3e 719{
d565635a 720 register int teunit = TEUNIT(dev);
d75e5c3e 721 register daddr_t a;
d565635a
BJ
722 register struct te_softc *sc;
723 register struct uba_device *ui;
d75e5c3e 724
d565635a 725 if (teunit >= NTE || (ui=tedinfo[teunit]) == 0 || ui->ui_alive == 0) {
89bd2f01
BJ
726 u.u_error = ENXIO;
727 return;
728 }
d565635a 729 sc = &te_softc[teunit];
d75e5c3e 730 a = dbtofsb(u.u_offset >> 9);
7e00c42b
BJ
731 sc->sc_blkno = a;
732 sc->sc_nxrec = a + 1;
d75e5c3e
BJ
733}
734
71236e46
BJ
735tmreset(uban)
736 int uban;
737{
89bd2f01 738 register struct uba_ctlr *um;
d565635a 739 register tm11, teunit;
89bd2f01 740 register struct uba_device *ui;
71236e46
BJ
741 register struct buf *dp;
742
afb907f2
BJ
743 for (tm11 = 0; tm11 < NTM; tm11++) {
744 if ((um = tmminfo[tm11]) == 0 || um->um_alive == 0 ||
71236e46
BJ
745 um->um_ubanum != uban)
746 continue;
80905075 747 printf(" tm%d", tm11);
71236e46
BJ
748 um->um_tab.b_active = 0;
749 um->um_tab.b_actf = um->um_tab.b_actl = 0;
750 if (um->um_ubinfo) {
751 printf("<%d>", (um->um_ubinfo>>28)&0xf);
0801d37f 752 ubadone(um);
71236e46
BJ
753 }
754 ((struct device *)(um->um_addr))->tmcs = TM_DCLR;
d565635a
BJ
755 for (teunit = 0; teunit < NTE; teunit++) {
756 if ((ui = tedinfo[teunit]) == 0 || ui->ui_mi != um ||
757 ui->ui_alive == 0)
71236e46 758 continue;
d565635a 759 dp = &teutab[teunit];
71236e46
BJ
760 dp->b_active = 0;
761 dp->b_forw = 0;
762 if (um->um_tab.b_actf == NULL)
763 um->um_tab.b_actf = dp;
764 else
765 um->um_tab.b_actl->b_forw = dp;
766 um->um_tab.b_actl = dp;
d565635a 767 te_softc[teunit].sc_openf = -1;
71236e46
BJ
768 }
769 tmstart(um);
770 }
771}
772
d75e5c3e
BJ
773/*ARGSUSED*/
774tmioctl(dev, cmd, addr, flag)
775 caddr_t addr;
776 dev_t dev;
777{
d565635a
BJ
778 int teunit = TEUNIT(dev);
779 register struct te_softc *sc = &te_softc[teunit];
780 register struct buf *bp = &ctmbuf[TMUNIT(dev)];
d75e5c3e
BJ
781 register callcount;
782 int fcount;
783 struct mtop mtop;
784 struct mtget mtget;
785 /* we depend of the values and order of the MT codes here */
71236e46
BJ
786 static tmops[] =
787 {TM_WEOF,TM_SFORW,TM_SREV,TM_SFORW,TM_SREV,TM_REW,TM_OFFL,TM_SENSE};
d75e5c3e 788
71236e46 789 switch (cmd) {
d75e5c3e
BJ
790 case MTIOCTOP: /* tape operation */
791 if (copyin((caddr_t)addr, (caddr_t)&mtop, sizeof(mtop))) {
792 u.u_error = EFAULT;
793 return;
794 }
795 switch(mtop.mt_op) {
71236e46
BJ
796 case MTWEOF:
797 callcount = mtop.mt_count;
798 fcount = 1;
799 break;
800 case MTFSF: case MTBSF:
d75e5c3e
BJ
801 callcount = mtop.mt_count;
802 fcount = INF;
803 break;
804 case MTFSR: case MTBSR:
805 callcount = 1;
806 fcount = mtop.mt_count;
807 break;
77115c00 808 case MTREW: case MTOFFL: case MTNOP:
d75e5c3e
BJ
809 callcount = 1;
810 fcount = 1;
811 break;
812 default:
813 u.u_error = ENXIO;
814 return;
815 }
71236e46 816 if (callcount <= 0 || fcount <= 0) {
d75e5c3e 817 u.u_error = ENXIO;
71236e46
BJ
818 return;
819 }
820 while (--callcount >= 0) {
e4271c68 821 tmcommand(dev, tmops[mtop.mt_op], fcount);
d75e5c3e 822 if ((mtop.mt_op == MTFSR || mtop.mt_op == MTBSR) &&
71236e46 823 bp->b_resid) {
d75e5c3e
BJ
824 u.u_error = EIO;
825 break;
826 }
d565635a 827 if ((bp->b_flags&B_ERROR) || sc->sc_erreg&TMER_BOT)
d75e5c3e
BJ
828 break;
829 }
71236e46 830 geterror(bp);
d75e5c3e
BJ
831 return;
832 case MTIOCGET:
7e00c42b
BJ
833 mtget.mt_dsreg = sc->sc_dsreg;
834 mtget.mt_erreg = sc->sc_erreg;
835 mtget.mt_resid = sc->sc_resid;
65e74b33 836 mtget.mt_type = MT_ISTM;
d75e5c3e
BJ
837 if (copyout((caddr_t)&mtget, addr, sizeof(mtget)))
838 u.u_error = EFAULT;
839 return;
840 default:
841 u.u_error = ENXIO;
842 }
843}
844
845#define DBSIZE 20
846
7455bf3e 847tmdump()
d75e5c3e 848{
89bd2f01 849 register struct uba_device *ui;
3f3a34c3
BJ
850 register struct uba_regs *up;
851 register struct device *addr;
5aa9d5ea
RE
852 int blk, num;
853 int start;
d75e5c3e 854
5aa9d5ea
RE
855 start = 0;
856 num = maxfree;
857#define phys(a,b) ((b)((int)(a)&0x7fffffff))
d565635a 858 if (tedinfo[0] == 0)
2536c16c 859 return (ENXIO);
d565635a 860 ui = phys(tedinfo[0], struct uba_device *);
3f3a34c3 861 up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
57229699 862 ubainit(up);
77115c00 863 DELAY(1000000);
3f3a34c3
BJ
864 addr = (struct device *)ui->ui_physaddr;
865 tmwait(addr);
71236e46 866 addr->tmcs = TM_DCLR | TM_GO;
d75e5c3e
BJ
867 while (num > 0) {
868 blk = num > DBSIZE ? DBSIZE : num;
3f3a34c3 869 tmdwrite(start, blk, addr, up);
d75e5c3e
BJ
870 start += blk;
871 num -= blk;
872 }
5aa9d5ea
RE
873 tmeof(addr);
874 tmeof(addr);
7e00c42b 875 tmwait(addr);
2536c16c
BJ
876 if (addr->tmcs&TM_ERR)
877 return (EIO);
71236e46 878 addr->tmcs = TM_REW | TM_GO;
5aa9d5ea 879 tmwait(addr);
7455bf3e 880 return (0);
d75e5c3e
BJ
881}
882
71236e46
BJ
883tmdwrite(dbuf, num, addr, up)
884 register dbuf, num;
3f3a34c3
BJ
885 register struct device *addr;
886 struct uba_regs *up;
d75e5c3e 887{
3f3a34c3
BJ
888 register struct pte *io;
889 register int npf;
0a51498e 890
3f3a34c3 891 tmwait(addr);
3f3a34c3 892 io = up->uba_map;
d75e5c3e 893 npf = num+1;
0a51498e 894 while (--npf != 0)
89bd2f01 895 *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV);
3f3a34c3
BJ
896 *(int *)io = 0;
897 addr->tmbc = -(num*NBPG);
898 addr->tmba = 0;
71236e46 899 addr->tmcs = TM_WCOM | TM_GO;
d75e5c3e
BJ
900}
901
3f3a34c3
BJ
902tmwait(addr)
903 register struct device *addr;
d75e5c3e 904{
0a51498e 905 register s;
d75e5c3e
BJ
906
907 do
3f3a34c3 908 s = addr->tmcs;
71236e46 909 while ((s & TM_CUR) == 0);
d75e5c3e
BJ
910}
911
3f3a34c3
BJ
912tmeof(addr)
913 struct device *addr;
d75e5c3e
BJ
914{
915
3f3a34c3 916 tmwait(addr);
71236e46 917 addr->tmcs = TM_WEOF | TM_GO;
d75e5c3e
BJ
918}
919#endif