xref: /original-bsd/sys/vax/stand/imptst.c (revision 62734ea8)
1 #include "../h/param.h"
2 #include "../h/pte.h"
3 #include "../h/inode.h"
4 
5 #include "../vaxuba/ubareg.h"
6 #include "../netinet/in.h"
7 #include "../netinet/in_systm.h"
8 #define	IMPLEADERS
9 #include "../netimp/if_imp.h"
10 #include "../vaxif/if_acc.h"
11 
12 #include "saio.h"
13 #include "savax.h"
14 
15 #define min(a,b) (a<b ? a : b)
16 #define BUFSIZ 512
17 
18 char	input[132];
19 struct	imp_leader imps, *ip = &imps;
20 char	inbuf[BUFSIZ];
21 int	writeflg = 0;
22 
23 main()
24 {
25 	register error = 0, i, len;
26 	short type, host, impno, link;
27 	register struct accdevice *addr =
28 		(struct accdevice *)ubamem(0, 0767700);
29 
30 	printf("imp interface diagnostic\n");
31 	printf("read or write test(r or w)? ");
32 	gets(input);
33 	while (*input != 'r' && *input != 'w') {
34 		printf("reply r or w: ");
35 		gets(input);
36 	}
37 	if (*input == 'w') {
38 		writeflg++;
39 		printf("enter destination host number: ");
40 		gets(input);
41 		while ((host = (short)atol(input)) < 0 || host > 255) {
42 			printf("range [0, 255], re-enter: ");
43 			gets(input);
44 		}
45 		printf("imp number: ");
46 		gets(input);
47 		while ((impno = (short)atol(input)) < 0 || impno > 32767) {
48 			printf("range [0, 32767], re-enter: ");
49 			gets(input);
50 		}
51 		printf("link number: ");
52 		gets(input);
53 		while ((link = (short)atol(input)) < 0 || link > 255) {
54 			printf("range [0, 255], re-enter: ");
55 			gets(input);
56 		}
57 	}
58 	printf("initialization starting...\n");
59 	impinit();
60 	/* send 3 noops and init imp leader buffer */
61 	impnoops((struct control_leader *)ip);
62 	printf("initialization complete\n");
63 	if (writeflg) {
64 		printf("starting write test...\n");
65 		ip->il_host = host;
66 		ip->il_imp = htons((u_short)impno);
67 		ip->il_link = link;
68 		while (!error)
69 			error = impwrite(ip, sizeof (*ip));
70         	printf("imp write error, ocsr=%b\n", (short)error,
71 			ACC_OUTBITS);
72 	} else {
73 		printf("starting read test...\n");
74 		while (!error) {
75 printf("impread(%d)\n", sizeof (*ip));
76 			error = impread(ip, sizeof (*ip));
77 printf("impread, error=%b\n", error, ACC_INBITS);
78 			printleader(ip);
79 			len = ntohs(ip->il_length);
80 printf("length=%d\n", len);
81 			/* read any data */
82 			while ((error & IN_EOM) == 0 &&
83 			  (error & ~IN_EOM) == 0 && len > 0) {
84 				i = min(len, BUFSIZ);
85 printf("impread(%d)\n", i);
86 				error = impread(inbuf, i);
87 				len -= i;
88 printf("error=%b, len=%d\n", error, ACC_INBITS, len);
89 			}
90 			error &= ~IN_EOM;
91 			if (error == 0 && (len > 0 || addr->iwc))
92 				printf("imp input length mismatch\n");
93 		}
94 		printf("imp read error, icsr=%b\n", (short)error, ACC_INBITS);
95 	}
96 	printf("...imptest exiting\n");
97 }
98 
99 impnoops(cp)
100 	register struct control_leader *cp;
101 {
102 	register i, error;
103 
104 	bzero((caddr_t)cp, sizeof (struct control_leader));
105 	cp->dl_format = IMP_NFF;
106 	cp->dl_mtype = IMPTYPE_NOOP;
107 	for (i = 0; i < IMP_DROPCNT + 1; i++ ) {
108                 cp->dl_link = i;
109 		if ((error = impwrite(ip, sizeof (*ip))) != 0) {
110 			printf("imp init error, ocsr=%b\n", (short)error,
111 				ACC_OUTBITS);
112 			_stop();
113 		}
114 	}
115 }
116 
117 impwrite(buf, len)
118 	register struct imp *buf;
119 	register len;
120 {
121 	register uba, error;
122 	struct iob io;
123 	register struct accdevice *addr =
124 	    (struct accdevice *)ubamem(0, 0767600);
125 
126 	/* set up uba mapping */
127 	io.i_ma = (caddr_t)buf;
128 	io.i_cc = len;
129 	uba = ubasetup(&io, 0);
130 
131 	/* set regs and perform i/o */
132 	addr->oba = (u_short)uba;
133 	addr->owc = -((io.i_cc + 1) >> 1);
134 	addr->ocsr = ((short) ((uba & 0x30000) >> 12) | OUT_ENLB | ACC_GO);
135 	while ((addr->ocsr & ACC_RDY) == 0)
136 		;
137 	error = addr->ocsr & (ACC_NXM|ACC_ERR);
138 	ubafree(uba);
139 	return(error);
140 }
141 
142 impread(buf, len)
143 	register struct imp *buf;
144 	register len;
145 {
146 	register uba, error;
147 	struct iob io;
148 	register struct accdevice *addr =
149 	    (struct accdevice *)ubamem(0, 0767600);
150 
151 	/* set up uba mapping */
152 	io.i_ma = (caddr_t)buf;
153 	io.i_cc = len;
154 	uba = ubasetup(&io, 0);
155 	/* set regs and perform i/o */
156 	addr->iba = (u_short)uba;
157 	addr->iwc = -(io.i_cc >> 1);
158 	addr->icsr = IN_MRDY | IN_WEN | ((uba & 0x30000) >> 12) | ACC_GO;
159 	while ((addr->icsr & ACC_RDY) == 0)
160 		;
161 	error = addr->icsr & (IN_EOM|ACC_ERR|IN_RMR|ACC_NXM);
162 	ubafree(uba);
163 	return(error);
164 }
165 
166 impinit()
167 {
168 	register struct accdevice *addr =
169 		(struct accdevice *)ubamem(0, 0767600);
170 	register int i;
171 
172 	/*
173 	 * Reset the imp interface;
174 	 * the delays are pure guesswork.
175 	 */
176 	addr->icsr = ACC_RESET; DELAY(5000);
177         addr->ocsr = ACC_RESET; DELAY(5000);
178 	addr->ocsr = OUT_BBACK;	DELAY(5000);	/* reset host master ready */
179 	addr->ocsr = 0;
180 	addr->icsr = IN_MRDY | IN_WEN;		/* close the relay */
181 	DELAY(10000);
182 	/* YECH!!! */
183 	for (i = 0; i < 500; i++) {
184 		if ((addr->icsr & IN_HRDY) &&
185 		    (addr->icsr & (IN_RMR | IN_IMPBSY)) == 0)
186 			return;
187 		addr->icsr = IN_MRDY | IN_WEN; DELAY(10000);
188 		/* keep turning IN_RMR off */
189 	}
190 	printf("imp doesn't respond, icsr=%b, ocsr=%b\n",
191 		addr->icsr, ACC_INBITS, addr->ocsr, ACC_OUTBITS);
192 }
193 
194 /*
195  *  Convert null-terminated ascii string to binary
196  *  and return value.
197  *  1st char in string :
198  *	0 -> octal
199  *	x -> hex
200  *	else decimal
201  */
202 atol(as)
203 	register char *as;
204 {
205 	register value = 0;
206 	register base = 10;
207 	register sign = 1;
208 	register digit = 0;
209 
210 aloop :
211 	if ((digit = (*as++)) == 0)
212 		return(value) ; /* null */
213 	if (digit == '-') {
214 		sign = -sign;
215 		goto aloop ;
216 	}
217 	if (digit == '0')
218 		base = 8 ;
219 	else if (digit == 'x')
220 		base = 16 ;
221 	else
222 		value = digit - '0';
223 	while (digit = (*as++)) {
224 		if (digit < '0')
225 			return(0);
226 		switch (base) {
227 
228 		case 8 :
229 			if (digit > '7')
230 				return(0);
231 			digit -= '0';
232 			break;
233 
234 		case 10 :
235 			if (digit > '9')
236 				return(0);
237 			digit -= '0';
238 			break;
239 
240 		case 16 :
241 			if (digit <= '9') {
242 				digit -= 060 ;
243 				break;
244 			}
245 			if ((digit >= 'A') && (digit <= 'F')) {
246 				digit -= 'A' + 10;
247 				break;
248 			}
249 			if ((digit >= 'a') && (digit <= 'f')) {
250 				digit -= 'a' + 10 ;
251 				break;
252 			}
253 			return(0);
254 		}
255 		value = (value * base) + digit;
256 	}
257 	return (value * sign);
258 }
259 
260 printleader(ip)
261 	register struct imp_leader *ip;
262 {
263 	printbyte((char *)ip, 12);
264 	printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network,
265 		ip->il_flags);
266 	if (ip->il_mtype <= IMPTYPE_READY)
267 		printf("%s,", impleaders[ip->il_mtype]);
268 	else
269 		printf("%x,", ip->il_mtype);
270 	printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host,
271 		ntohs(ip->il_imp));
272 	if (ip->il_link == IMPLINK_IP)
273 		printf("ip,");
274 	else
275 		printf("%x,", ip->il_link);
276 	printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3);
277 }
278 
279 printbyte(cp, n)
280 	register char *cp;
281 	int n;
282 {
283 	register i, j, c;
284 
285 	for (i=0; i<n; i++) {
286 		c = *cp++;
287 		for (j=0; j<2; j++)
288 			putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf]);
289 		putchar(' ');
290 	}
291 	putchar('\n');
292 }
293