123229Smckusick /*
229302Smckusick * Copyright (c) 1982, 1986 Regents of the University of California.
323229Smckusick * All rights reserved. The Berkeley software License Agreement
423229Smckusick * specifies the terms and conditions for redistribution.
523229Smckusick *
6*45803Sbostic * @(#)imptst.c 7.4 (Berkeley) 12/16/90
723229Smckusick */
89805Ssam
9*45803Sbostic #include "sys/param.h"
109186Ssam
11*45803Sbostic #include "../uba/ubareg.h"
12*45803Sbostic #include "netinet/in.h"
13*45803Sbostic #include "netinet/in_systm.h"
147328Ssam #define IMPLEADERS
15*45803Sbostic #include "netimp/if_imp.h"
16*45803Sbostic #include "../if/if_acc.h"
179186Ssam
18*45803Sbostic #include "stand/saio.h"
197328Ssam #include "savax.h"
207328Ssam
217328Ssam #define min(a,b) (a<b ? a : b)
227328Ssam #define BUFSIZ 512
237328Ssam
247328Ssam char input[132];
257328Ssam struct imp_leader imps, *ip = &imps;
267328Ssam char inbuf[BUFSIZ];
277328Ssam int writeflg = 0;
287328Ssam
main()297328Ssam main()
307328Ssam {
317328Ssam register error = 0, i, len;
327328Ssam short type, host, impno, link;
337328Ssam register struct accdevice *addr =
347328Ssam (struct accdevice *)ubamem(0, 0767700);
357328Ssam
367328Ssam printf("imp interface diagnostic\n");
377328Ssam printf("read or write test(r or w)? ");
387328Ssam gets(input);
397328Ssam while (*input != 'r' && *input != 'w') {
407328Ssam printf("reply r or w: ");
417328Ssam gets(input);
427328Ssam }
437328Ssam if (*input == 'w') {
447328Ssam writeflg++;
457328Ssam printf("enter destination host number: ");
467328Ssam gets(input);
477328Ssam while ((host = (short)atol(input)) < 0 || host > 255) {
487328Ssam printf("range [0, 255], re-enter: ");
497328Ssam gets(input);
507328Ssam }
517328Ssam printf("imp number: ");
527328Ssam gets(input);
537328Ssam while ((impno = (short)atol(input)) < 0 || impno > 32767) {
547328Ssam printf("range [0, 32767], re-enter: ");
557328Ssam gets(input);
567328Ssam }
577328Ssam printf("link number: ");
587328Ssam gets(input);
597328Ssam while ((link = (short)atol(input)) < 0 || link > 255) {
607328Ssam printf("range [0, 255], re-enter: ");
617328Ssam gets(input);
627328Ssam }
637328Ssam }
647328Ssam printf("initialization starting...\n");
657328Ssam impinit();
667328Ssam /* send 3 noops and init imp leader buffer */
677328Ssam impnoops((struct control_leader *)ip);
687328Ssam printf("initialization complete\n");
697328Ssam if (writeflg) {
707328Ssam printf("starting write test...\n");
717328Ssam ip->il_host = host;
727328Ssam ip->il_imp = htons((u_short)impno);
737328Ssam ip->il_link = link;
747328Ssam while (!error)
757328Ssam error = impwrite(ip, sizeof (*ip));
767328Ssam printf("imp write error, ocsr=%b\n", (short)error,
777328Ssam ACC_OUTBITS);
787328Ssam } else {
797328Ssam printf("starting read test...\n");
807328Ssam while (!error) {
817328Ssam printf("impread(%d)\n", sizeof (*ip));
827328Ssam error = impread(ip, sizeof (*ip));
837328Ssam printf("impread, error=%b\n", error, ACC_INBITS);
847328Ssam printleader(ip);
857328Ssam len = ntohs(ip->il_length);
867328Ssam printf("length=%d\n", len);
877328Ssam /* read any data */
887328Ssam while ((error & IN_EOM) == 0 &&
897328Ssam (error & ~IN_EOM) == 0 && len > 0) {
907328Ssam i = min(len, BUFSIZ);
917328Ssam printf("impread(%d)\n", i);
927328Ssam error = impread(inbuf, i);
937328Ssam len -= i;
947328Ssam printf("error=%b, len=%d\n", error, ACC_INBITS, len);
957328Ssam }
967328Ssam error &= ~IN_EOM;
977328Ssam if (error == 0 && (len > 0 || addr->iwc))
987328Ssam printf("imp input length mismatch\n");
997328Ssam }
1007328Ssam printf("imp read error, icsr=%b\n", (short)error, ACC_INBITS);
1017328Ssam }
1027328Ssam printf("...imptest exiting\n");
1037328Ssam }
1047328Ssam
impnoops(cp)1057328Ssam impnoops(cp)
1067328Ssam register struct control_leader *cp;
1077328Ssam {
1087328Ssam register i, error;
1097328Ssam
1107328Ssam bzero((caddr_t)cp, sizeof (struct control_leader));
1117328Ssam cp->dl_format = IMP_NFF;
1127328Ssam cp->dl_mtype = IMPTYPE_NOOP;
1137328Ssam for (i = 0; i < IMP_DROPCNT + 1; i++ ) {
1147328Ssam cp->dl_link = i;
1157328Ssam if ((error = impwrite(ip, sizeof (*ip))) != 0) {
1167328Ssam printf("imp init error, ocsr=%b\n", (short)error,
1177328Ssam ACC_OUTBITS);
1187328Ssam _stop();
1197328Ssam }
1207328Ssam }
1217328Ssam }
1227328Ssam
impwrite(buf,len)1237328Ssam impwrite(buf, len)
1247328Ssam register struct imp *buf;
1257328Ssam register len;
1267328Ssam {
1277328Ssam register uba, error;
1287328Ssam struct iob io;
1297328Ssam register struct accdevice *addr =
1307328Ssam (struct accdevice *)ubamem(0, 0767600);
1317328Ssam
1327328Ssam /* set up uba mapping */
1337328Ssam io.i_ma = (caddr_t)buf;
1347328Ssam io.i_cc = len;
1357328Ssam uba = ubasetup(&io, 0);
1367328Ssam
1377328Ssam /* set regs and perform i/o */
1387328Ssam addr->oba = (u_short)uba;
1397328Ssam addr->owc = -((io.i_cc + 1) >> 1);
1407328Ssam addr->ocsr = ((short) ((uba & 0x30000) >> 12) | OUT_ENLB | ACC_GO);
1417328Ssam while ((addr->ocsr & ACC_RDY) == 0)
1427328Ssam ;
1437328Ssam error = addr->ocsr & (ACC_NXM|ACC_ERR);
1447328Ssam ubafree(uba);
1457328Ssam return(error);
1467328Ssam }
1477328Ssam
impread(buf,len)1487328Ssam impread(buf, len)
1497328Ssam register struct imp *buf;
1507328Ssam register len;
1517328Ssam {
1527328Ssam register uba, error;
1537328Ssam struct iob io;
1547328Ssam register struct accdevice *addr =
1557328Ssam (struct accdevice *)ubamem(0, 0767600);
1567328Ssam
1577328Ssam /* set up uba mapping */
1587328Ssam io.i_ma = (caddr_t)buf;
1597328Ssam io.i_cc = len;
1607328Ssam uba = ubasetup(&io, 0);
1617328Ssam /* set regs and perform i/o */
1627328Ssam addr->iba = (u_short)uba;
1637328Ssam addr->iwc = -(io.i_cc >> 1);
1647328Ssam addr->icsr = IN_MRDY | IN_WEN | ((uba & 0x30000) >> 12) | ACC_GO;
1657328Ssam while ((addr->icsr & ACC_RDY) == 0)
1667328Ssam ;
1677328Ssam error = addr->icsr & (IN_EOM|ACC_ERR|IN_RMR|ACC_NXM);
1687328Ssam ubafree(uba);
1697328Ssam return(error);
1707328Ssam }
1717328Ssam
impinit()1727328Ssam impinit()
1737328Ssam {
1747328Ssam register struct accdevice *addr =
1757328Ssam (struct accdevice *)ubamem(0, 0767600);
1767328Ssam register int i;
1777328Ssam
1787328Ssam /*
1797328Ssam * Reset the imp interface;
1807328Ssam * the delays are pure guesswork.
1817328Ssam */
1827328Ssam addr->icsr = ACC_RESET; DELAY(5000);
1837328Ssam addr->ocsr = ACC_RESET; DELAY(5000);
1847328Ssam addr->ocsr = OUT_BBACK; DELAY(5000); /* reset host master ready */
1857328Ssam addr->ocsr = 0;
1867328Ssam addr->icsr = IN_MRDY | IN_WEN; /* close the relay */
1877328Ssam DELAY(10000);
1887328Ssam /* YECH!!! */
1897328Ssam for (i = 0; i < 500; i++) {
1907328Ssam if ((addr->icsr & IN_HRDY) &&
1917328Ssam (addr->icsr & (IN_RMR | IN_IMPBSY)) == 0)
1927328Ssam return;
1937328Ssam addr->icsr = IN_MRDY | IN_WEN; DELAY(10000);
1947328Ssam /* keep turning IN_RMR off */
1957328Ssam }
1967328Ssam printf("imp doesn't respond, icsr=%b, ocsr=%b\n",
1977328Ssam addr->icsr, ACC_INBITS, addr->ocsr, ACC_OUTBITS);
1987328Ssam }
1997328Ssam
2007328Ssam /*
2017328Ssam * Convert null-terminated ascii string to binary
2027328Ssam * and return value.
2037328Ssam * 1st char in string :
2047328Ssam * 0 -> octal
2057328Ssam * x -> hex
2067328Ssam * else decimal
2077328Ssam */
atol(as)2087328Ssam atol(as)
2097328Ssam register char *as;
2107328Ssam {
2117328Ssam register value = 0;
2127328Ssam register base = 10;
2137328Ssam register sign = 1;
2147328Ssam register digit = 0;
2157328Ssam
2167328Ssam aloop :
2177328Ssam if ((digit = (*as++)) == 0)
2187328Ssam return(value) ; /* null */
2197328Ssam if (digit == '-') {
2207328Ssam sign = -sign;
2217328Ssam goto aloop ;
2227328Ssam }
2237328Ssam if (digit == '0')
2247328Ssam base = 8 ;
2257328Ssam else if (digit == 'x')
2267328Ssam base = 16 ;
2277328Ssam else
2287328Ssam value = digit - '0';
2297328Ssam while (digit = (*as++)) {
2307328Ssam if (digit < '0')
2317328Ssam return(0);
2327328Ssam switch (base) {
2337328Ssam
2347328Ssam case 8 :
2357328Ssam if (digit > '7')
2367328Ssam return(0);
2377328Ssam digit -= '0';
2387328Ssam break;
2397328Ssam
2407328Ssam case 10 :
2417328Ssam if (digit > '9')
2427328Ssam return(0);
2437328Ssam digit -= '0';
2447328Ssam break;
2457328Ssam
2467328Ssam case 16 :
2477328Ssam if (digit <= '9') {
2487328Ssam digit -= 060 ;
2497328Ssam break;
2507328Ssam }
2517328Ssam if ((digit >= 'A') && (digit <= 'F')) {
2527328Ssam digit -= 'A' + 10;
2537328Ssam break;
2547328Ssam }
2557328Ssam if ((digit >= 'a') && (digit <= 'f')) {
2567328Ssam digit -= 'a' + 10 ;
2577328Ssam break;
2587328Ssam }
2597328Ssam return(0);
2607328Ssam }
2617328Ssam value = (value * base) + digit;
2627328Ssam }
2637328Ssam return (value * sign);
2647328Ssam }
2657328Ssam
printleader(ip)2667328Ssam printleader(ip)
2677328Ssam register struct imp_leader *ip;
2687328Ssam {
2697328Ssam printbyte((char *)ip, 12);
2707328Ssam printf("<fmt=%x,net=%x,flags=%x,mtype=", ip->il_format, ip->il_network,
2717328Ssam ip->il_flags);
2727328Ssam if (ip->il_mtype <= IMPTYPE_READY)
2737328Ssam printf("%s,", impleaders[ip->il_mtype]);
2747328Ssam else
2757328Ssam printf("%x,", ip->il_mtype);
2767328Ssam printf("htype=%x,host=%x,imp=%x,link=", ip->il_htype, ip->il_host,
2777328Ssam ntohs(ip->il_imp));
2787328Ssam if (ip->il_link == IMPLINK_IP)
2797328Ssam printf("ip,");
2807328Ssam else
2817328Ssam printf("%x,", ip->il_link);
2827328Ssam printf("subtype=%x,len=%x>\n",ip->il_subtype,ntohs(ip->il_length)>>3);
2837328Ssam }
2847328Ssam
printbyte(cp,n)2857328Ssam printbyte(cp, n)
2867328Ssam register char *cp;
2877328Ssam int n;
2887328Ssam {
2897328Ssam register i, j, c;
2907328Ssam
2917328Ssam for (i=0; i<n; i++) {
2927328Ssam c = *cp++;
2937328Ssam for (j=0; j<2; j++)
2947328Ssam putchar("0123456789abcdef"[(c>>((1-j)*4))&0xf]);
2957328Ssam putchar(' ');
2967328Ssam }
2977328Ssam putchar('\n');
2987328Ssam }
299