fix for generic interlocking tm and ts
[unix-history] / usr / src / sys / vax / uba / ts.c
CommitLineData
23e0e9b8 1/* ts.c 4.10 81/04/14 */
9bad5ea5 2
66b4fb09 3#include "ts.h"
23e0e9b8 4#include "tm.h"
9bad5ea5 5#if NTS > 0
30e0abb3
BJ
6#define printd if(tsdebug)printf
7int tsdebug;
9bad5ea5
BJ
8/*
9 * TS11 tape driver
30a7bcbf
BJ
10 *
11 * TODO:
12 * test driver with more than one controller
13 * test reset code
14 * test dump code
15 * test rewinds without hanging in driver
16 * what happens if you offline tape during rewind?
17 * test using file system on tape
9bad5ea5 18 */
9bad5ea5
BJ
19#include "../h/param.h"
20#include "../h/systm.h"
21#include "../h/buf.h"
9bad5ea5 22#include "../h/dir.h"
30a7bcbf 23#include "../h/conf.h"
9bad5ea5 24#include "../h/user.h"
30a7bcbf 25#include "../h/file.h"
9bad5ea5 26#include "../h/map.h"
30a7bcbf 27#include "../h/pte.h"
e1b765e1 28#include "../h/vm.h"
30a7bcbf
BJ
29#include "../h/ubareg.h"
30#include "../h/ubavar.h"
31#include "../h/mtio.h"
32#include "../h/ioctl.h"
33#include "../h/cmap.h"
34#include "../h/cpu.h"
35
36#include "../h/tsreg.h"
37
38/*
39 * There is a ctsbuf per tape controller.
40 * It is used as the token to pass to the internal routines
41 * to execute tape ioctls.
42 * In particular, when the tape is rewinding on close we release
43 * the user process but any further attempts to use the tape drive
44 * before the rewind completes will hang waiting for ctsbuf.
45 */
46struct buf ctsbuf[NTS];
47
48/*
49 * Raw tape operations use rtsbuf. The driver
50 * notices when rtsbuf is being used and allows the user
51 * program to continue after errors and read records
52 * not of the standard length (BSIZE).
53 */
54struct buf rtsbuf[NTS];
55
56/*
57 * Driver unibus interface routines and variables.
58 */
59int tsprobe(), tsslave(), tsattach(), tsdgo(), tsintr();
60struct uba_ctlr *tsminfo[NTS];
61struct uba_device *tsdinfo[NTS];
30e0abb3 62struct buf tsbuf[NTS];
30a7bcbf
BJ
63u_short tsstd[] = { 0772520, 0 };
64/*** PROBABLY DON'T NEED ALL THESE SINCE CONTROLLER == DRIVE ***/
65struct uba_driver zsdriver =
30e0abb3 66 { tsprobe, tsslave, tsattach, tsdgo, tsstd, "ts", tsdinfo, "zs", tsminfo, 0 };
30a7bcbf
BJ
67
68/* bits in minor device */
69#define TSUNIT(dev) (minor(dev)&03)
70#define T_NOREWIND 04
9bad5ea5 71
30a7bcbf 72#define INF (daddr_t)1000000L
9bad5ea5 73
30a7bcbf
BJ
74/*
75 * Software state per tape transport.
76 * Also contains hardware state in message packets.
77 *
78 * 1. A tape drive is a unique-open device; we refuse opens when it is already.
79 * 2. We keep track of the current position on a block tape and seek
80 * before operations by forward/back spacing if necessary.
81 * 3. We remember if the last operation was a write on a tape, so if a tape
82 * is open read write and the last thing done is a write we can
83 * write a standard end of tape mark (two eofs).
84 * 4. We remember the status registers after the last command, using
85 * then internally and returning them to the SENSE ioctl.
86 */
87struct ts_softc {
88 char sc_openf; /* lock against multiple opens */
89 char sc_lastiow; /* last op was a write */
30e0abb3 90 short sc_resid; /* copy of last bc */
30a7bcbf
BJ
91 daddr_t sc_blkno; /* block number, for block device tape */
92 daddr_t sc_nxrec; /* position of end of tape, if known */
30a7bcbf
BJ
93 struct ts_cmd sc_cmd; /* the command packet - ADDR MUST BE 0 MOD 4 */
94 struct ts_sts sc_sts; /* status packet, for returned status */
95 struct ts_char sc_char; /* characteristics packet */
96 u_short sc_uba; /* Unibus addr of cmd pkt for tsdb */
97} ts_softc[NTS];
98
99struct ts_softc *ts_ubaddr; /* Unibus address of ts_softc */
100
101/*
102 * States for um->um_tab.b_active, the per controller state flag.
103 * This is used to sequence control in the driver.
104 */
105#define SSEEK 1 /* seeking */
106#define SIO 2 /* doing seq i/o */
107#define SCOM 3 /* sending control command */
108#define SREW 4 /* sending a drive rewind */
109
23e0e9b8
BJ
110#if NTM > 0
111/* kludge... see tm.c */
112extern havetm;
113#endif
30a7bcbf
BJ
114/*
115 * Determine if there is a controller for
116 * a ts at address reg. Our goal is to make the
117 * device interrupt.
118 */
119tsprobe(reg)
120 caddr_t reg;
121{
122 register int br, cvec; /* must be r11,r10; value-result */
123
124#ifdef lint
125 br = 0; cvec = br; br = cvec;
126#endif
127 /****************/
128 /* */
129 /* K L U D G E */
130 /* */
131 /****************/
132
23e0e9b8
BJ
133#if NTM > 0
134 if (havetm)
135 return (0);
136#endif
30a7bcbf
BJ
137 /* IT'S TOO HARD TO MAKE THIS THING INTERRUPT
138 JUST TO FIND ITS VECTOR */
139 cvec = 0224;
140 br = 0x15;
141}
142
143/*
144 * TS11 only supports one drive per controller;
145 * check for ui_slave == 0.
146 *
147 * DO WE REALLY NEED THIS ROUTINE???
148 */
149/*ARGSUSED*/
150tsslave(ui, reg)
151 struct uba_device *ui;
152 caddr_t reg;
153{
154
155 if (ui->ui_slave) /* non-zero slave not allowed */
156 return(0);
157 return (1);
158}
159
160/*
161 * Record attachment of the unit to the controller.
162 *
163 * SHOULD THIS ROUTINE DO ANYTHING???
164 */
165/*ARGSUSED*/
166tsattach(ui)
167 struct uba_device *ui;
168{
169
170}
171
172/*
173 * Open the device. Tapes are unique open
174 * devices, so we refuse if it is already open.
175 * We also check that a tape is available, and
176 * don't block waiting here; if you want to wait
177 * for a tape you should timeout in user code.
178 */
9bad5ea5 179tsopen(dev, flag)
30a7bcbf
BJ
180 dev_t dev;
181 int flag;
9bad5ea5 182{
30a7bcbf
BJ
183 register int tsunit;
184 register struct uba_device *ui;
185 register struct ts_softc *sc;
9bad5ea5 186
30a7bcbf
BJ
187 tsunit = TSUNIT(dev);
188 if (tsunit>=NTS || (sc = &ts_softc[tsunit])->sc_openf ||
189 (ui = tsdinfo[tsunit]) == 0 || ui->ui_alive == 0) {
9bad5ea5
BJ
190 u.u_error = ENXIO;
191 return;
192 }
30a7bcbf 193 if (tsinit(tsunit)) {
9bad5ea5 194 u.u_error = ENXIO;
30e0abb3 195 printd("init failed\n");
9bad5ea5
BJ
196 return;
197 }
30e0abb3 198 printd("init ok\n");
30a7bcbf 199 tscommand(dev, TS_SENSE, 1);
30e0abb3 200 printd("sense xs0 %o\n", sc->sc_sts.s_xs0);
30a7bcbf
BJ
201 if ((sc->sc_sts.s_xs0&TS_ONL) == 0 || ((flag&(FREAD|FWRITE)) ==
202 FWRITE && (sc->sc_sts.s_xs0&TS_WLK))) {
203 /*
204 * Not online or write locked.
205 */
206 u.u_error = EIO;
207 return;
9bad5ea5 208 }
30a7bcbf
BJ
209 sc->sc_openf = 1;
210 sc->sc_blkno = (daddr_t)0;
211 sc->sc_nxrec = INF;
212 sc->sc_lastiow = 0;
9bad5ea5
BJ
213}
214
30a7bcbf
BJ
215/*
216 * Close tape device.
217 *
218 * If tape was open for writing or last operation was
219 * a write, then write two EOF's and backspace over the last one.
220 * Unless this is a non-rewinding special file, rewind the tape.
221 * Make the tape available to others.
222 */
9bad5ea5 223tsclose(dev, flag)
30a7bcbf
BJ
224 register dev_t dev;
225 register flag;
9bad5ea5 226{
30a7bcbf
BJ
227 register struct ts_softc *sc = &ts_softc[TSUNIT(dev)];
228
229 if (flag == FWRITE || (flag&FWRITE) && sc->sc_lastiow) {
230 tscommand(dev, TS_WEOF, 1);
231 tscommand(dev, TS_WEOF, 1);
232 tscommand(dev, TS_SREV, 1);
233 }
234 if ((minor(dev)&T_NOREWIND) == 0)
235 /*
236 * 0 count means don't hang waiting for rewind complete
237 * rather ctsbuf stays busy until the operation completes
238 * preventing further opens from completing by
239 * preventing a TS_SENSE from completing.
240 */
241 tscommand(dev, TS_REW, 0);
242 sc->sc_openf = 0;
243}
9bad5ea5 244
30a7bcbf
BJ
245/*
246 * Initialize the TS11. Set up Unibus mapping for command
247 * packets and set device characteristics.
248 */
249tsinit(unit)
250 register int unit;
251{
252 register struct ts_softc *sc = &ts_softc[unit];
253 register struct uba_ctlr *um = tsminfo[unit];
254 register struct device *addr = (struct device *)um->um_addr;
255 register int i;
256
257 /*
258 * Map the command and message packets into Unibus
259 * address space. We do all the command and message
260 * packets at once to minimize the amount of Unibus
261 * mapping necessary.
262 */
263 if (ts_ubaddr == 0) {
264 ctsbuf[unit].b_un.b_addr = (caddr_t)ts_softc;
265 ctsbuf[unit].b_bcount = sizeof(ts_softc);
266 i = ubasetup(um->um_ubanum, &ctsbuf[unit], 0);
267 i &= 0777777;
268 ts_ubaddr = (struct ts_softc *)i;
269 /* MAKE SURE WE DON'T GET UNIBUS ADDRESS ZERO */
270 if (ts_ubaddr == 0)
271 printf("ts%d: zero ubaddr\n", unit);
272 }
273 /*
274 * Now initialize the TS11 controller.
275 * Set the characteristics.
276 */
277 if (addr->tssr & TS_NBA) {
278 addr->tssr = 0; /* subsystem initialize */
279 tswait(addr);
280 i = (int)&ts_ubaddr[unit].sc_cmd; /* Unibus addr of cmd */
30e0abb3
BJ
281 if (i&3) {
282 printf("addr mod 4 != 0\n");
283 return(1);
284 }
30a7bcbf
BJ
285 sc->sc_uba = (u_short)(i + ((i>>16)&3));
286 sc->sc_char.char_addr = (int)&ts_ubaddr[unit].sc_sts;
287 sc->sc_char.char_size = sizeof(struct ts_sts);
288 sc->sc_char.char_mode = TS_ESS;
289 sc->sc_cmd.c_cmd = TS_ACK | TS_SETCHR;
30e0abb3
BJ
290 i = (int)&ts_ubaddr[unit].sc_char;
291 sc->sc_cmd.c_loba = i;
292 sc->sc_cmd.c_hiba = (i>>16)&3;
30a7bcbf
BJ
293 sc->sc_cmd.c_size = sizeof(struct ts_char);
294 addr->tsdb = sc->sc_uba;
295 tswait(addr);
30e0abb3
BJ
296/*
297 printd("%o %o %o %o %o %o %o %o\n", addr->tssr, sc->sc_sts.s_sts, sc->sc_sts.s_len, sc->sc_sts.s_rbpcr, sc->sc_sts.s_xs0, sc->sc_sts.s_xs1,sc->sc_sts.s_xs1,sc->sc_sts.s_xs2,sc->sc_sts.s_xs3);
298*/
299 if (addr->tssr & TS_NBA)
300 return(1);
9bad5ea5 301 }
30a7bcbf 302 return(0);
9bad5ea5
BJ
303}
304
30a7bcbf
BJ
305/*
306 * Execute a command on the tape drive
307 * a specified number of times.
308 */
309tscommand(dev, com, count)
310 dev_t dev;
311 int com, count;
9bad5ea5
BJ
312{
313 register struct buf *bp;
314
30a7bcbf
BJ
315 bp = &ctsbuf[TSUNIT(dev)];
316 (void) spl5();
317 while (bp->b_flags&B_BUSY) {
318 /*
319 * This special check is because B_BUSY never
320 * gets cleared in the non-waiting rewind case.
321 */
322 if (bp->b_repcnt == 0 && (bp->b_flags&B_DONE))
323 break;
9bad5ea5
BJ
324 bp->b_flags |= B_WANTED;
325 sleep((caddr_t)bp, PRIBIO);
326 }
9bad5ea5 327 bp->b_flags = B_BUSY|B_READ;
30a7bcbf 328 (void) spl0();
30e0abb3 329 printd("command %o dev %x count %d\n", com, dev, count);
30a7bcbf
BJ
330 bp->b_dev = dev;
331 bp->b_repcnt = count;
332 bp->b_command = com;
333 bp->b_blkno = 0;
9bad5ea5 334 tsstrategy(bp);
30a7bcbf
BJ
335 /*
336 * In case of rewind from close, don't wait.
337 * This is the only case where count can be 0.
338 */
339 if (count == 0)
340 return;
9bad5ea5 341 iowait(bp);
30a7bcbf 342 if (bp->b_flags&B_WANTED)
9bad5ea5 343 wakeup((caddr_t)bp);
30a7bcbf 344 bp->b_flags &= B_ERROR;
9bad5ea5
BJ
345}
346
30a7bcbf
BJ
347/*
348 * Queue a tape operation.
349 */
9bad5ea5 350tsstrategy(bp)
30a7bcbf 351 register struct buf *bp;
9bad5ea5 352{
30a7bcbf
BJ
353 int tsunit = TSUNIT(bp->b_dev);
354 register struct uba_ctlr *um;
30e0abb3 355 register struct buf *dp;
30a7bcbf
BJ
356
357 /*
358 * Put transfer at end of controller queue
359 */
9bad5ea5 360 bp->av_forw = NULL;
30a7bcbf 361 um = tsdinfo[tsunit]->ui_mi;
30e0abb3 362 dp = &tsbuf[tsunit];
30a7bcbf 363 (void) spl5();
30e0abb3
BJ
364 if (dp->b_actf == NULL)
365 dp->b_actf = bp;
9bad5ea5 366 else
30e0abb3
BJ
367 dp->b_actl->av_forw = bp;
368 dp->b_actl = bp;
369 um->um_tab.b_actf = um->um_tab.b_actl = dp;
30a7bcbf
BJ
370 /*
371 * If the controller is not busy, get
372 * it going.
373 */
374 if (um->um_tab.b_active == 0)
375 tsstart(um);
376 (void) spl0();
9bad5ea5
BJ
377}
378
30a7bcbf
BJ
379/*
380 * Start activity on a ts controller.
381 */
382tsstart(um)
383 register struct uba_ctlr *um;
9bad5ea5
BJ
384{
385 register struct buf *bp;
30a7bcbf
BJ
386 register struct device *addr = (struct device *)um->um_addr;
387 register struct ts_softc *sc;
388 register struct ts_cmd *tc;
389 register struct uba_device *ui;
390 int tsunit, cmd;
9bad5ea5
BJ
391 daddr_t blkno;
392
30a7bcbf
BJ
393 /*
394 * Start the controller if there is something for it to do.
395 */
396loop:
30e0abb3 397 if ((bp = um->um_tab.b_actf->b_actf) == NULL)
9bad5ea5 398 return;
30a7bcbf
BJ
399 tsunit = TSUNIT(bp->b_dev);
400 ui = tsdinfo[tsunit];
401 sc = &ts_softc[tsunit];
402 tc = &sc->sc_cmd;
403 /*
404 * Default is that last command was NOT a write command;
405 * if we do a write command we will notice this in tsintr().
406 */
407 sc->sc_lastiow = 1;
408 if (sc->sc_openf < 0 || (addr->tssr&TS_OFL)) {
409 /*
410 * Have had a hard error on a non-raw tape
411 * or the tape unit is now unavailable
412 * (e.g. taken off line).
413 */
414 bp->b_flags |= B_ERROR;
415 goto next;
416 }
417 if (bp == &ctsbuf[TSUNIT(bp->b_dev)]) {
418 /*
419 * Execute control operation with the specified count.
420 */
421 um->um_tab.b_active =
422 bp->b_command == TS_REW ? SREW : SCOM;
423 tc->c_repcnt = bp->b_repcnt;
30e0abb3 424 printd("strat: do cmd\n");
30a7bcbf
BJ
425 goto dobpcmd;
426 }
427 /*
428 * The following checks handle boundary cases for operation
429 * on non-raw tapes. On raw tapes the initialization of
430 * sc->sc_nxrec by tsphys causes them to be skipped normally
431 * (except in the case of retries).
432 */
433 if (dbtofsb(bp->b_blkno) > sc->sc_nxrec) {
434 /*
435 * Can't read past known end-of-file.
436 */
437 bp->b_flags |= B_ERROR;
438 bp->b_error = ENXIO;
439 goto next;
440 }
441 if (dbtofsb(bp->b_blkno) == sc->sc_nxrec &&
442 bp->b_flags&B_READ) {
443 /*
444 * Reading at end of file returns 0 bytes.
445 */
446 bp->b_resid = bp->b_bcount;
447 clrbuf(bp);
448 goto next;
449 }
450 if ((bp->b_flags&B_READ) == 0)
451 /*
452 * Writing sets EOF
453 */
454 sc->sc_nxrec = dbtofsb(bp->b_blkno) + 1;
455 /*
456 * If the data transfer command is in the correct place,
457 * set up all the registers except the csr, and give
458 * control over to the UNIBUS adapter routines, to
459 * wait for resources to start the i/o.
460 */
461 if ((blkno = sc->sc_blkno) == dbtofsb(bp->b_blkno)) {
462 tc->c_size = bp->b_bcount;
463 if ((bp->b_flags&B_READ) == 0)
464 cmd = TS_WCOM;
9bad5ea5 465 else
30a7bcbf
BJ
466 cmd = TS_RCOM;
467 if (um->um_tab.b_errcnt)
468 cmd |= TS_RETRY;
469 um->um_tab.b_active = SIO;
30e0abb3
BJ
470 tc->c_cmd = TS_ACK | TS_CVC | TS_IE | cmd;
471 printd("r/w %o size %d\n", tc->c_cmd, tc->c_size);
30a7bcbf
BJ
472 (void) ubago(ui);
473 return;
474 }
475 /*
476 * Tape positioned incorrectly;
477 * set to seek forwards or backwards to the correct spot.
478 * This happens for raw tapes only on error retries.
479 */
480 um->um_tab.b_active = SSEEK;
30e0abb3 481 printd("seek blkno %d b_blkno %d\n", blkno, bp->b_blkno);
30a7bcbf
BJ
482 if (blkno < dbtofsb(bp->b_blkno)) {
483 bp->b_command = TS_SFORW;
484 tc->c_repcnt = dbtofsb(bp->b_blkno) - blkno;
9bad5ea5 485 } else {
30a7bcbf
BJ
486 bp->b_command = TS_SREV;
487 tc->c_repcnt = blkno - dbtofsb(bp->b_blkno);
9bad5ea5 488 }
30a7bcbf
BJ
489dobpcmd:
490 /*
491 * Do the command in bp.
492 */
30e0abb3 493 tc->c_cmd = TS_ACK | TS_CVC | TS_IE | bp->b_command;
30a7bcbf 494 addr->tsdb = sc->sc_uba;
9bad5ea5
BJ
495 return;
496
30a7bcbf
BJ
497next:
498 /*
499 * Done with this operation due to error or
500 * the fact that it doesn't do anything.
501 * Release UBA resources (if any), dequeue
502 * the transfer and continue processing this slave.
503 */
504 if (um->um_ubinfo)
505 ubadone(um);
506 um->um_tab.b_errcnt = 0;
30e0abb3 507 um->um_tab.b_actf->b_actf = bp->av_forw;
9bad5ea5
BJ
508 iodone(bp);
509 goto loop;
510}
511
30a7bcbf
BJ
512/*
513 * The UNIBUS resources we needed have been
514 * allocated to us; start the device.
515 */
516tsdgo(um)
517 register struct uba_ctlr *um;
518{
519 register struct device *addr = (struct device *)um->um_addr;
520 register struct ts_softc *sc = &ts_softc[um->um_ctlr];
30e0abb3 521 register int i;
30a7bcbf 522
30e0abb3
BJ
523 i = um->um_ubinfo & 0777777;
524 printd("dgo addr %o\n", i);
525 sc->sc_cmd.c_loba = i;
526 sc->sc_cmd.c_hiba = (i>>16)&3;
30a7bcbf
BJ
527 addr->tsdb = sc->sc_uba;
528}
529
530/*
531 * Ts interrupt routine.
532 */
533/*ARGSUSED*/
534tsintr(ts11)
535 int ts11;
9bad5ea5
BJ
536{
537 register struct buf *bp;
30a7bcbf
BJ
538 register struct uba_ctlr *um = tsminfo[ts11];
539 register struct device *addr;
540 register struct ts_softc *sc;
541 int tsunit;
542 register state;
9bad5ea5 543
30e0abb3
BJ
544 printd("intr\n");
545 if ((bp = um->um_tab.b_actf->b_actf) == NULL)
9bad5ea5 546 return;
30a7bcbf
BJ
547 tsunit = TSUNIT(bp->b_dev);
548 addr = (struct device *)tsdinfo[tsunit]->ui_addr;
549 /*
550 * If last command was a rewind, and tape is still
551 * rewinding, wait for the rewind complete interrupt.
552 *
553 * SHOULD NEVER GET AN INTERRUPT IN THIS STATE.
554 */
555 if (um->um_tab.b_active == SREW) {
556 um->um_tab.b_active = SCOM;
557 if ((addr->tssr&TS_SSR) == 0)
558 return;
559 }
560 /*
561 * An operation completed... record status
562 */
30e0abb3 563 printd(" ok1\n");
30a7bcbf
BJ
564 sc = &ts_softc[tsunit];
565 if ((bp->b_flags & B_READ) == 0)
566 sc->sc_lastiow = 1;
567 state = um->um_tab.b_active;
568 um->um_tab.b_active = 0;
569 /*
570 * Check for errors.
571 */
572 if (addr->tssr&TS_SC) {
573 switch (addr->tssr & TS_TC) {
574 case TS_UNREC: /* unrecoverable */
575 case TS_FATAL: /* fatal error */
576 case TS_ATTN: /* attention (shouldn't happen) */
577 case TS_RECNM: /* recoverable, no motion */
578 break;
579
580 case TS_SUCC: /* success termination */
581 printf("ts%d: success\n", TSUNIT(minor(bp->b_dev)));
582 goto ignoreerr;
583
584 case TS_ALERT: /* tape status alert */
585 /*
586 * If we hit the end of the tape file,
587 * update our position.
588 */
589 if (sc->sc_sts.s_xs0 & (TS_TMK|TS_EOT)) {
590 tsseteof(bp); /* set blkno and nxrec */
591 state = SCOM; /* force completion */
592 /*
593 * Stuff bc so it will be unstuffed correctly
594 * later to get resid.
595 */
596 sc->sc_sts.s_rbpcr = bp->b_bcount;
597 goto opdone;
598 }
599 /*
600 * If we were reading raw tape and the record was too long
601 * or too short, then we don't consider this an error.
602 */
603 if (bp == &rtsbuf[TSUNIT(bp->b_dev)] && (bp->b_flags&B_READ) &&
604 sc->sc_sts.s_xs0&(TS_RLS|TS_RLL))
605 goto ignoreerr;
606 case TS_RECOV: /* recoverable, tape moved */
607 /*
608 * If this was an i/o operation retry up to 8 times.
609 */
610 if (state==SIO) {
611 if (++um->um_tab.b_errcnt < 7) {
612 ubadone(um);
613 goto opcont;
614 } else
615 sc->sc_blkno++;
616 } else {
617 /*
618 * Non-i/o errors on non-raw tape
619 * cause it to close.
620 */
621 if (sc->sc_openf>0 && bp != &rtsbuf[TSUNIT(bp->b_dev)])
622 sc->sc_openf = -1;
623 }
9bad5ea5 624 break;
30a7bcbf
BJ
625
626 case TS_REJECT: /* function reject */
627 if (state == SIO && sc->sc_sts.s_xs0 & TS_WLE)
628 printf("ts%d: write locked\n", TSUNIT(bp->b_dev));
629 if ((sc->sc_sts.s_xs0 & TS_ONL) == 0)
630 printf("ts%d: offline\n", TSUNIT(bp->b_dev));
9bad5ea5
BJ
631 break;
632 }
30a7bcbf
BJ
633 /*
634 * Couldn't recover error
635 */
636 printf("ts%d: hard error bn%d xs0=%b\n", TSUNIT(bp->b_dev),
637 bp->b_blkno, sc->sc_sts.s_xs0, TSXS0_BITS);
638 bp->b_flags |= B_ERROR;
639 goto opdone;
9bad5ea5 640 }
30a7bcbf
BJ
641 /*
642 * Advance tape control FSM.
643 */
644ignoreerr:
645 switch (state) {
646
9bad5ea5 647 case SIO:
30a7bcbf
BJ
648 /*
649 * Read/write increments tape block number
650 */
651 sc->sc_blkno++;
652 goto opdone;
653
9bad5ea5 654 case SCOM:
30a7bcbf
BJ
655 /*
656 * For forward/backward space record update current position.
657 */
658 if (bp == &ctsbuf[TSUNIT(bp->b_dev)])
659 switch (bp->b_command) {
660
661 case TS_SFORW:
662 sc->sc_blkno += bp->b_repcnt;
663 break;
9bad5ea5 664
30a7bcbf
BJ
665 case TS_SREV:
666 sc->sc_blkno -= bp->b_repcnt;
667 break;
668 }
669 goto opdone;
670
671 case SSEEK:
672 sc->sc_blkno = dbtofsb(bp->b_blkno);
673 goto opcont;
9bad5ea5
BJ
674
675 default:
30a7bcbf 676 panic("tsintr");
9bad5ea5 677 }
30a7bcbf
BJ
678opdone:
679 /*
680 * Reset error count and remove
681 * from device queue.
682 */
683 um->um_tab.b_errcnt = 0;
30e0abb3 684 um->um_tab.b_actf->b_actf = bp->av_forw;
30a7bcbf
BJ
685 bp->b_resid = sc->sc_sts.s_rbpcr;
686 ubadone(um);
30e0abb3 687 printd(" iodone\n");
30a7bcbf 688 iodone(bp);
30e0abb3 689 if (um->um_tab.b_actf->b_actf == 0)
30a7bcbf
BJ
690 return;
691opcont:
692 tsstart(um);
693}
694
695tsseteof(bp)
696 register struct buf *bp;
697{
698 register int tsunit = TSUNIT(bp->b_dev);
699 register struct ts_softc *sc = &ts_softc[tsunit];
700
701 if (bp == &ctsbuf[TSUNIT(bp->b_dev)]) {
702 if (sc->sc_blkno > dbtofsb(bp->b_blkno)) {
703 /* reversing */
704 sc->sc_nxrec = dbtofsb(bp->b_blkno) - sc->sc_sts.s_rbpcr;
705 sc->sc_blkno = sc->sc_nxrec;
706 } else {
707 /* spacing forward */
708 sc->sc_blkno = dbtofsb(bp->b_blkno) + sc->sc_sts.s_rbpcr;
709 sc->sc_nxrec = sc->sc_blkno - 1;
9bad5ea5 710 }
30a7bcbf
BJ
711 return;
712 }
713 /* eof on read */
714 sc->sc_nxrec = dbtofsb(bp->b_blkno);
9bad5ea5
BJ
715}
716
717tsread(dev)
30a7bcbf 718 dev_t dev;
9bad5ea5 719{
30a7bcbf 720
9bad5ea5 721 tsphys(dev);
30a7bcbf
BJ
722 if (u.u_error)
723 return;
724 physio(tsstrategy, &rtsbuf[TSUNIT(dev)], dev, B_READ, minphys);
9bad5ea5
BJ
725}
726
727tswrite(dev)
30a7bcbf 728 dev_t dev;
9bad5ea5 729{
30a7bcbf 730
9bad5ea5 731 tsphys(dev);
30a7bcbf
BJ
732 if (u.u_error)
733 return;
734 physio(tsstrategy, &rtsbuf[TSUNIT(dev)], dev, B_WRITE, minphys);
9bad5ea5
BJ
735}
736
30a7bcbf
BJ
737/*
738 * Check that a raw device exists.
739 * If it does, set up sc_blkno and sc_nxrec
740 * so that the tape will appear positioned correctly.
741 */
9bad5ea5 742tsphys(dev)
30a7bcbf 743 dev_t dev;
9bad5ea5 744{
30a7bcbf
BJ
745 register int tsunit = TSUNIT(dev);
746 register daddr_t a;
747 register struct ts_softc *sc;
748 register struct uba_device *ui;
9bad5ea5 749
30a7bcbf
BJ
750 if (tsunit >= NTS || (ui=tsdinfo[tsunit]) == 0 || ui->ui_alive == 0) {
751 u.u_error = ENXIO;
752 return;
753 }
754 sc = &ts_softc[tsunit];
755 a = dbtofsb(u.u_offset >> 9);
756 sc->sc_blkno = a;
757 sc->sc_nxrec = a + 1;
9bad5ea5 758}
f0a3ddbd 759
30a7bcbf
BJ
760tsreset(uban)
761 int uban;
f0a3ddbd 762{
30a7bcbf
BJ
763 register struct uba_ctlr *um;
764 register ts11;
765 register struct buf *dp;
766
767 for (ts11 = 0; ts11 < NTS; ts11++) {
768 if ((um = tsminfo[ts11]) == 0 || um->um_alive == 0 ||
769 um->um_ubanum != uban)
770 continue;
771 printf(" ts%d", ts11);
772 um->um_tab.b_active = 0;
773 um->um_tab.b_actf = um->um_tab.b_actl = 0;
774 ts_softc[ts11].sc_openf = -1;
775 if (um->um_ubinfo) {
776 printf("<%d>", (um->um_ubinfo>>28)&0xf);
777 ubadone(um);
778 }
779 tsinit(ts11);
780 tsstart(um);
f0a3ddbd 781 }
f0a3ddbd
BJ
782}
783
30a7bcbf
BJ
784/*ARGSUSED*/
785tsioctl(dev, cmd, addr, flag)
786 caddr_t addr;
787 dev_t dev;
f0a3ddbd 788{
30a7bcbf
BJ
789 int tsunit = TSUNIT(dev);
790 register struct ts_softc *sc = &ts_softc[tsunit];
791 register struct buf *bp = &ctsbuf[TSUNIT(dev)];
792 register callcount;
793 int fcount;
794 struct mtop mtop;
795 struct mtget mtget;
796 /* we depend of the values and order of the MT codes here */
797 static tsops[] =
798 {TS_WEOF,TS_SFORW,TS_SREV,TS_SFORWF,TS_SREVF,TS_REW,TS_OFFL,TS_SENSE};
799
800 switch (cmd) {
801 case MTIOCTOP: /* tape operation */
802 if (copyin((caddr_t)addr, (caddr_t)&mtop, sizeof(mtop))) {
803 u.u_error = EFAULT;
804 return;
805 }
806 switch(mtop.mt_op) {
807 case MTWEOF:
808 callcount = mtop.mt_count;
809 fcount = 1;
810 break;
811 case MTFSF: case MTBSF:
812 case MTFSR: case MTBSR:
813 callcount = 1;
814 fcount = mtop.mt_count;
815 break;
816 case MTREW: case MTOFFL: case MTNOP:
817 callcount = 1;
818 fcount = 1;
819 break;
820 default:
821 u.u_error = ENXIO;
822 return;
823 }
824 if (callcount <= 0 || fcount <= 0) {
825 u.u_error = ENXIO;
826 return;
827 }
828 while (--callcount >= 0) {
829 tscommand(dev, tsops[mtop.mt_op], fcount);
830 if ((mtop.mt_op == MTFSR || mtop.mt_op == MTBSR) &&
831 bp->b_resid) {
832 u.u_error = EIO;
833 break;
834 }
835 if ((bp->b_flags&B_ERROR) || sc->sc_sts.s_xs0&TS_BOT)
836 break;
837 }
838 geterror(bp);
839 return;
840 case MTIOCGET:
841 mtget.mt_dsreg = 0;
842 mtget.mt_erreg = sc->sc_sts.s_xs0;
843 mtget.mt_resid = sc->sc_resid;
e320a1c9 844 mtget.mt_type = MT_ISTS;
30a7bcbf
BJ
845 if (copyout((caddr_t)&mtget, addr, sizeof(mtget)))
846 u.u_error = EFAULT;
847 return;
848 default:
849 u.u_error = ENXIO;
850 }
f0a3ddbd
BJ
851}
852
30a7bcbf 853#define DBSIZE 20
f0a3ddbd 854
30a7bcbf
BJ
855tsdump()
856{
857 register struct uba_device *ui;
858 register struct uba_regs *up;
859 register struct device *addr;
860 int blk, num;
861 int start;
862
863 start = 0;
864 num = maxfree;
865#define phys(a,b) ((b)((int)(a)&0x7fffffff))
866 if (tsdinfo[0] == 0)
867 return (ENXIO);
868 ui = phys(tsdinfo[0], struct uba_device *);
869 up = phys(ui->ui_hd, struct uba_hd *)->uh_physuba;
30e0abb3 870 ubainit(up);
30a7bcbf
BJ
871 DELAY(1000000);
872 addr = (struct device *)ui->ui_physaddr;
873 addr->tssr = 0;
874 tswait(addr);
875 while (num > 0) {
876 blk = num > DBSIZE ? DBSIZE : num;
877 tsdwrite(start, blk, addr, up);
878 start += blk;
879 num -= blk;
880 }
881 tseof(addr);
882 tseof(addr);
883 tswait(addr);
884 if (addr->tssr&TS_SC)
885 return (EIO);
886 addr->tssr = 0;
887 tswait(addr);
888 return (0);
f0a3ddbd
BJ
889}
890
30a7bcbf
BJ
891tsdwrite(dbuf, num, addr, up)
892 register dbuf, num;
893 register struct device *addr;
894 struct uba_regs *up;
f0a3ddbd 895{
30a7bcbf
BJ
896 register struct pte *io;
897 register int npf;
898
899 tswait(addr);
900 io = up->uba_map;
901 npf = num+1;
902 while (--npf != 0)
903 *(int *)io++ = (dbuf++ | (1<<UBAMR_DPSHIFT) | UBAMR_MRV);
904 *(int *)io = 0;
905#ifdef notyet
906 addr->tsbc = -(num*NBPG);
907 addr->tsba = 0;
908 addr->tscs = TS_WCOM | TM_GO;
909#endif
f0a3ddbd
BJ
910}
911
30a7bcbf
BJ
912tswait(addr)
913 register struct device *addr;
f0a3ddbd 914{
30a7bcbf 915 register s;
f0a3ddbd 916
30a7bcbf
BJ
917 do
918 s = addr->tssr;
919 while ((s & TS_SSR) == 0);
f0a3ddbd
BJ
920}
921
30a7bcbf
BJ
922tseof(addr)
923 struct device *addr;
f0a3ddbd 924{
30a7bcbf
BJ
925
926 tswait(addr);
927#ifdef notyet
928 addr->tscs = TS_WEOF | TM_GO;
929#endif
f0a3ddbd 930}
9bad5ea5 931#endif