xref: /csrg-svn/sys/vax/stand/ts.c (revision 33408)
1 /*
2  * Copyright (c) 1982, 1986 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  *
6  *	@(#)ts.c	7.1 (Berkeley) 6/5/86
7  */
8 
9 /*
10  * TS11 tape driver
11  */
12 #include "../machine/pte.h"
13 
14 #include "param.h"
15 #include "inode.h"
16 #include "fs.h"
17 
18 #include "../vaxuba/tsreg.h"
19 #include "../vaxuba/ubareg.h"
20 
21 #include "saio.h"
22 #include "savax.h"
23 
24 
25 u_short	tsstd[] = { 0772520 };
26 
27 struct	iob	ctsbuf;
28 
29 u_short	ts_uba;			/* Unibus address of ts structure */
30 
31 struct tsdevice *tsaddr = 0;
32 
33 struct ts {
34 	struct ts_cmd ts_cmd;
35 	struct ts_char ts_char;
36 	struct ts_sts ts_sts;
37 } ts;
38 
39 tsopen(io)
40 	register struct iob *io;
41 {
42 	static struct ts *ts_ubaddr;
43 	long i = 0;
44 
45 	if (tsaddr == 0)
46 		tsaddr = (struct tsdevice *)ubamem(io->i_unit, tsstd[0]);
47 	if (badaddr((char *)tsaddr, sizeof (short))) {
48 		printf("nonexistent device\n");
49 		return (ENXIO);
50 	}
51 	tsaddr->tssr = 0;
52 	while ((tsaddr->tssr & TS_SSR)==0) {
53 		DELAY(10);
54 		if (++i > 1000000) {
55 			printf("ts: not ready\n");
56 			return (EUNIT);
57 		}
58 	}
59 	if (tsaddr->tssr&TS_OFL) {
60 		printf("ts: offline\n");
61 		return (EUNIT);
62 	}
63 	if (tsaddr->tssr&TS_NBA) {
64 		int i;
65 
66 		ctsbuf.i_ma = (caddr_t) &ts;
67 		ctsbuf.i_cc = sizeof(ts);
68 		if (ts_ubaddr == 0)
69 			ts_ubaddr = (struct ts *)ubasetup(&ctsbuf, 2);
70 		ts_uba = (u_short)((long)ts_ubaddr + (((long)ts_ubaddr>>16)&03));
71 		ts.ts_char.char_addr = (int)&ts_ubaddr->ts_sts;
72 		ts.ts_char.char_size = sizeof(ts.ts_sts);
73 		ts.ts_char.char_mode = TS_ESS;
74 		ts.ts_cmd.c_cmd = TS_ACK|TS_SETCHR;
75 		i = (int)&ts_ubaddr->ts_char;
76 		ts.ts_cmd.c_loba = i;
77 		ts.ts_cmd.c_hiba = (i>>16)&3;
78 		ts.ts_cmd.c_size = sizeof(ts.ts_char);
79 		tsaddr->tsdb = ts_uba;
80 	}
81 	tsstrategy(io, TS_REW);
82 	if (io->i_cc = io->i_boff)
83 		tsstrategy(io, TS_SFORWF);
84 	return (0);
85 }
86 
87 tsclose(io)
88 	register struct iob *io;
89 {
90 
91 	tsstrategy(io, TS_REW);
92 }
93 
94 tsstrategy(io, func)
95 	register struct iob *io;
96 {
97 	register int errcnt, info = 0;
98 
99 	errcnt = 0;
100 retry:
101 	while ((tsaddr->tssr & TS_SSR) == 0)
102 		DELAY(100);
103 	if (func == TS_REW || func == TS_SFORWF)
104 		ts.ts_cmd.c_repcnt = io->i_cc;
105 	else {
106 		info = ubasetup(io, 1);
107 		ts.ts_cmd.c_size = io->i_cc;
108 		ts.ts_cmd.c_loba = info;
109 		ts.ts_cmd.c_hiba = (info>>16)&3;
110 	}
111 	if (func == READ)
112 		func = TS_RCOM;
113 	else if (func == WRITE)
114 		func = TS_WCOM;
115 	ts.ts_cmd.c_cmd = TS_ACK|TS_CVC|func;
116 	tsaddr->tsdb = ts_uba;
117 	do
118 		DELAY(100)
119 	while ((tsaddr->tssr & TS_SSR) == 0);
120 	if (info)
121 		ubafree(io, info);
122 	if (ts.ts_sts.s_xs0 & TS_TMK)
123 		return (0);
124 	if (tsaddr->tssr & TS_SC) {
125 		printf("ts tape error: er=%b, xs0=%b\n",
126 		    tsaddr->tssr, TSSR_BITS,
127 		    ts.ts_sts.s_xs0, TSXS0_BITS);
128 		if (errcnt==10) {
129 			printf("ts: unrecovered error\n");
130 			return (-1);
131 		}
132 		errcnt++;
133 		if (func == TS_RCOM || func == TS_WCOM)
134 			func |= TS_RETRY;
135 		goto retry;
136 	}
137 	if (errcnt)
138 		printf("ts: recovered by retry\n");
139 	return (io->i_cc - ts.ts_sts.s_rbpcr);
140 }
141