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