17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*c7e4935fSss  * Common Development and Distribution License (the "License").
6*c7e4935fSss  *  You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*c7e4935fSss  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Synchronous loop-back test program
287c478bd9Sstevel@tonic-gate  * For installation verification of synchronous lines and facilities
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <ctype.h>
337c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
347c478bd9Sstevel@tonic-gate #include <fcntl.h>
357c478bd9Sstevel@tonic-gate #include <sys/time.h>
367c478bd9Sstevel@tonic-gate #include <sys/file.h>
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <stdlib.h>
397c478bd9Sstevel@tonic-gate #include <unistd.h>
407c478bd9Sstevel@tonic-gate #include <string.h>
417c478bd9Sstevel@tonic-gate #include <errno.h>
427c478bd9Sstevel@tonic-gate #include <sys/stream.h>
437c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
447c478bd9Sstevel@tonic-gate #include <sys/poll.h>
457c478bd9Sstevel@tonic-gate #include <sys/ser_sync.h>
467c478bd9Sstevel@tonic-gate #include <libdlpi.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate static void Usage(void);
497c478bd9Sstevel@tonic-gate static void quiet_period(void);
507c478bd9Sstevel@tonic-gate static void first_packet();
517c478bd9Sstevel@tonic-gate static void many_packets();
527c478bd9Sstevel@tonic-gate static void printhex(char *cp, int len);
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate static unsigned int speed = 9600;
557c478bd9Sstevel@tonic-gate static int reccount = 100;
567c478bd9Sstevel@tonic-gate static int reclen = 100;
577c478bd9Sstevel@tonic-gate static char loopstr[MAX_INPUT];
587c478bd9Sstevel@tonic-gate static int looptype = 0;
597c478bd9Sstevel@tonic-gate static int loopchange = 0;
607c478bd9Sstevel@tonic-gate static int clockchange = 0;
617c478bd9Sstevel@tonic-gate static int cfd, dfd;		/* control and data descriptors */
627c478bd9Sstevel@tonic-gate static int data = -1;
637c478bd9Sstevel@tonic-gate static int verbose = 0;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static char *yesno[] = {
667c478bd9Sstevel@tonic-gate 	"no",
677c478bd9Sstevel@tonic-gate 	"yes",
687c478bd9Sstevel@tonic-gate 	"silent",
697c478bd9Sstevel@tonic-gate 	0,
707c478bd9Sstevel@tonic-gate };
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate static char *txnames[] = {
737c478bd9Sstevel@tonic-gate 	"txc",
747c478bd9Sstevel@tonic-gate 	"rxc",
757c478bd9Sstevel@tonic-gate 	"baud",
767c478bd9Sstevel@tonic-gate 	"pll",
777c478bd9Sstevel@tonic-gate 	"sysclk",
787c478bd9Sstevel@tonic-gate 	"-txc",
797c478bd9Sstevel@tonic-gate 	0,
807c478bd9Sstevel@tonic-gate };
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate static char *rxnames[] = {
837c478bd9Sstevel@tonic-gate 	"rxc",
847c478bd9Sstevel@tonic-gate 	"txc",
857c478bd9Sstevel@tonic-gate 	"baud",
867c478bd9Sstevel@tonic-gate 	"pll",
877c478bd9Sstevel@tonic-gate 	"sysclk",
887c478bd9Sstevel@tonic-gate 	"-rxc",
897c478bd9Sstevel@tonic-gate 	0,
907c478bd9Sstevel@tonic-gate };
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #define	MAXPACKET	4096
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)957c478bd9Sstevel@tonic-gate main(int argc, char **argv)
967c478bd9Sstevel@tonic-gate {
97*c7e4935fSss 	char *portname;
98*c7e4935fSss 	char dnambuf[MAXPATHLEN], *cp;
99*c7e4935fSss 	char device[DLPI_LINKNAME_MAX];
1007c478bd9Sstevel@tonic-gate 	struct scc_mode sm;
1017c478bd9Sstevel@tonic-gate 	struct strioctl sioc;
102*c7e4935fSss 	uint_t ppa;
1037c478bd9Sstevel@tonic-gate 	char *devstr = "/dev/";
1047c478bd9Sstevel@tonic-gate 	int devstrlen;
105*c7e4935fSss 	int retval;
106*c7e4935fSss 	dlpi_handle_t dh;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	argc--;
1097c478bd9Sstevel@tonic-gate 	argv++;
1107c478bd9Sstevel@tonic-gate 	while (argc > 0 && argv[0][0] == '-')
1117c478bd9Sstevel@tonic-gate 		switch (argv[0][1]) {
1127c478bd9Sstevel@tonic-gate 		case 'c':	/* rec count */
1137c478bd9Sstevel@tonic-gate 			if (argc < 2)
1147c478bd9Sstevel@tonic-gate 				Usage();
1157c478bd9Sstevel@tonic-gate 			reccount = atoi(argv[1]);
1167c478bd9Sstevel@tonic-gate 			argc -= 2;
1177c478bd9Sstevel@tonic-gate 			argv += 2;
1187c478bd9Sstevel@tonic-gate 			break;
1197c478bd9Sstevel@tonic-gate 		case 'd':
1207c478bd9Sstevel@tonic-gate 			if (sscanf(argv[1], "%x", (uint_t *)&data) != 1)
1217c478bd9Sstevel@tonic-gate 				Usage();
1227c478bd9Sstevel@tonic-gate 			argc -= 2;
1237c478bd9Sstevel@tonic-gate 			argv += 2;
1247c478bd9Sstevel@tonic-gate 			break;
1257c478bd9Sstevel@tonic-gate 		case 'l':	/* rec length */
1267c478bd9Sstevel@tonic-gate 			if (argc < 2)
1277c478bd9Sstevel@tonic-gate 				Usage();
1287c478bd9Sstevel@tonic-gate 			reclen = atoi(argv[1]);
1297c478bd9Sstevel@tonic-gate 			argc -= 2;
1307c478bd9Sstevel@tonic-gate 			argv += 2;
1317c478bd9Sstevel@tonic-gate 			break;
1327c478bd9Sstevel@tonic-gate 		case 's':	/* line speed */
1337c478bd9Sstevel@tonic-gate 			if (argc < 2)
1347c478bd9Sstevel@tonic-gate 				Usage();
1357c478bd9Sstevel@tonic-gate 			speed = atoi(argv[1]);
1367c478bd9Sstevel@tonic-gate 			argc -= 2;
1377c478bd9Sstevel@tonic-gate 			argv += 2;
1387c478bd9Sstevel@tonic-gate 			break;
1397c478bd9Sstevel@tonic-gate 		case 't':	/* test type */
1407c478bd9Sstevel@tonic-gate 			if (argc < 2)
1417c478bd9Sstevel@tonic-gate 				Usage();
1427c478bd9Sstevel@tonic-gate 			looptype = atoi(argv[1]);
1437c478bd9Sstevel@tonic-gate 			argc -= 2;
1447c478bd9Sstevel@tonic-gate 			argv += 2;
1457c478bd9Sstevel@tonic-gate 			break;
1467c478bd9Sstevel@tonic-gate 		case 'v':
1477c478bd9Sstevel@tonic-gate 			verbose = 1;
1487c478bd9Sstevel@tonic-gate 			argc--;
1497c478bd9Sstevel@tonic-gate 			argv++;
1507c478bd9Sstevel@tonic-gate 			break;
1517c478bd9Sstevel@tonic-gate 		}
1527c478bd9Sstevel@tonic-gate 	if (argc != 1)
1537c478bd9Sstevel@tonic-gate 		Usage();
1547c478bd9Sstevel@tonic-gate 	portname = argv[0];
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	devstrlen = strlen(devstr);
1577c478bd9Sstevel@tonic-gate 	if (strncmp(devstr, portname, devstrlen) != 0) {
1587c478bd9Sstevel@tonic-gate 		if (snprintf(dnambuf, sizeof (dnambuf), "%s%s", devstr,
1597c478bd9Sstevel@tonic-gate 		    portname) >= sizeof (dnambuf)) {
1607c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1617c478bd9Sstevel@tonic-gate 			    "syncloop: invalid device name (too long) %s\n",
1627c478bd9Sstevel@tonic-gate 			    portname);
1637c478bd9Sstevel@tonic-gate 			exit(1);
1647c478bd9Sstevel@tonic-gate 		}
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	dfd = open(dnambuf, O_RDWR);
1687c478bd9Sstevel@tonic-gate 	if (dfd < 0) {
1697c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "syncloop: cannot open %s\n", dnambuf);
1707c478bd9Sstevel@tonic-gate 		perror(dnambuf);
1717c478bd9Sstevel@tonic-gate 		exit(1);
1727c478bd9Sstevel@tonic-gate 	}
173*c7e4935fSss 
174*c7e4935fSss 	cp = portname;
175*c7e4935fSss 	while (*cp)			/* find the end of the name */
176*c7e4935fSss 		cp++;
177*c7e4935fSss 	cp--;
178*c7e4935fSss 	if (!isdigit(*cp)) {
1797c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
180*c7e4935fSss 		    "syncloop: %s missing minor device number\n", portname);
1817c478bd9Sstevel@tonic-gate 		exit(1);
1827c478bd9Sstevel@tonic-gate 	}
183*c7e4935fSss 
184*c7e4935fSss 	if (strlen(portname) >= DLPI_LINKNAME_MAX) {
185*c7e4935fSss 		(void) fprintf(stderr,
186*c7e4935fSss 		    "syncloop: invalid device name (too long) %s\n",
1877c478bd9Sstevel@tonic-gate 		    portname);
1887c478bd9Sstevel@tonic-gate 		exit(1);
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
191*c7e4935fSss 	if ((retval = dlpi_open(portname, &dh, DLPI_SERIAL)) != DLPI_SUCCESS) {
192*c7e4935fSss 		(void) fprintf(stderr, "syncloop: dlpi_open %s: %s\n", portname,
193*c7e4935fSss 		    dlpi_strerror(retval));
1947c478bd9Sstevel@tonic-gate 		exit(1);
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 
197*c7e4935fSss 	(void) dlpi_parselink(portname, device, &ppa);
198*c7e4935fSss 
1997c478bd9Sstevel@tonic-gate 	if (reclen < 0 || reclen > MAXPACKET) {
2007c478bd9Sstevel@tonic-gate 		(void) printf("invalid packet length: %d\n", reclen);
2017c478bd9Sstevel@tonic-gate 		exit(1);
2027c478bd9Sstevel@tonic-gate 	}
203*c7e4935fSss 	(void) printf("[ Data device: %s | Control device: %s, ppa=%u ]\n",
204*c7e4935fSss 		dnambuf, device, ppa);
205*c7e4935fSss 
206*c7e4935fSss 	cfd = dlpi_fd(dh);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCGETMODE;
2097c478bd9Sstevel@tonic-gate 	sioc.ic_timout = -1;
2107c478bd9Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct scc_mode);
2117c478bd9Sstevel@tonic-gate 	sioc.ic_dp = (char *)&sm;
2127c478bd9Sstevel@tonic-gate 	if (ioctl(cfd, I_STR, &sioc) < 0) {
2137c478bd9Sstevel@tonic-gate 		perror("S_IOCGETMODE");
214*c7e4935fSss 		(void) fprintf(stderr, "syncloop: can't get sync mode info "
215*c7e4935fSss 		    "for %s\n", portname);
2167c478bd9Sstevel@tonic-gate 		exit(1);
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 	while (looptype < 1 || looptype > 4) {
2197c478bd9Sstevel@tonic-gate 		(void) printf("Enter test type:\n");
2207c478bd9Sstevel@tonic-gate 		(void) printf("1: Internal Test\n");
2217c478bd9Sstevel@tonic-gate 		(void) printf(
2227c478bd9Sstevel@tonic-gate "            (internal data loop, internal clocking)\n");
2237c478bd9Sstevel@tonic-gate 		(void) printf("2: Test using loopback plugs\n");
2247c478bd9Sstevel@tonic-gate 		(void) printf(
2257c478bd9Sstevel@tonic-gate "            (external data loop, internal clocking)\n");
2267c478bd9Sstevel@tonic-gate 		(void) printf("3: Test using local or remote modem loopback\n");
2277c478bd9Sstevel@tonic-gate 		(void) printf(
2287c478bd9Sstevel@tonic-gate "            (external data loop, external clocking)\n");
2297c478bd9Sstevel@tonic-gate 		(void) printf("4: Other, previously set, special mode\n");
2307c478bd9Sstevel@tonic-gate 		(void) printf("> "); (void) fflush(stdout);
2317c478bd9Sstevel@tonic-gate 		(void) fgets(loopstr, sizeof (loopstr), stdin);
2327c478bd9Sstevel@tonic-gate 		(void) sscanf(loopstr, "%d", &looptype);
2337c478bd9Sstevel@tonic-gate 	}
2347c478bd9Sstevel@tonic-gate 	switch (looptype) {
2357c478bd9Sstevel@tonic-gate 	case 1:
2367c478bd9Sstevel@tonic-gate 		if ((sm.sm_txclock != TXC_IS_BAUD) ||
2377c478bd9Sstevel@tonic-gate 		    (sm.sm_rxclock != RXC_IS_BAUD))
2387c478bd9Sstevel@tonic-gate 			clockchange++;
2397c478bd9Sstevel@tonic-gate 		sm.sm_txclock = TXC_IS_BAUD;
2407c478bd9Sstevel@tonic-gate 		sm.sm_rxclock = RXC_IS_BAUD;
2417c478bd9Sstevel@tonic-gate 		if ((sm.sm_config & CONN_LPBK) == 0)
2427c478bd9Sstevel@tonic-gate 			loopchange++;
2437c478bd9Sstevel@tonic-gate 		sm.sm_config |= CONN_LPBK;
2447c478bd9Sstevel@tonic-gate 		break;
2457c478bd9Sstevel@tonic-gate 	case 2:
2467c478bd9Sstevel@tonic-gate 		if ((sm.sm_txclock != TXC_IS_BAUD) ||
2477c478bd9Sstevel@tonic-gate 		    (sm.sm_rxclock != RXC_IS_RXC))
2487c478bd9Sstevel@tonic-gate 			clockchange++;
2497c478bd9Sstevel@tonic-gate 		sm.sm_txclock = TXC_IS_BAUD;
2507c478bd9Sstevel@tonic-gate 		sm.sm_rxclock = RXC_IS_RXC;
2517c478bd9Sstevel@tonic-gate 		if ((sm.sm_config & CONN_LPBK) != 0)
2527c478bd9Sstevel@tonic-gate 			loopchange++;
2537c478bd9Sstevel@tonic-gate 		sm.sm_config &= ~CONN_LPBK;
2547c478bd9Sstevel@tonic-gate 		break;
2557c478bd9Sstevel@tonic-gate 	case 3:
2567c478bd9Sstevel@tonic-gate 		if ((sm.sm_txclock != TXC_IS_TXC) ||
2577c478bd9Sstevel@tonic-gate 		    (sm.sm_rxclock != RXC_IS_RXC))
2587c478bd9Sstevel@tonic-gate 			clockchange++;
2597c478bd9Sstevel@tonic-gate 		sm.sm_txclock = TXC_IS_TXC;
2607c478bd9Sstevel@tonic-gate 		sm.sm_rxclock = RXC_IS_RXC;
2617c478bd9Sstevel@tonic-gate 		if ((sm.sm_config & CONN_LPBK) != 0)
2627c478bd9Sstevel@tonic-gate 			loopchange++;
2637c478bd9Sstevel@tonic-gate 		sm.sm_config &= ~CONN_LPBK;
2647c478bd9Sstevel@tonic-gate 		break;
2657c478bd9Sstevel@tonic-gate 	case 4:
2667c478bd9Sstevel@tonic-gate 		goto no_params;
2677c478bd9Sstevel@tonic-gate 	}
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	sm.sm_baudrate = speed;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCSETMODE;
2727c478bd9Sstevel@tonic-gate 	sioc.ic_timout = -1;
2737c478bd9Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct scc_mode);
2747c478bd9Sstevel@tonic-gate 	sioc.ic_dp = (char *)&sm;
2757c478bd9Sstevel@tonic-gate 	if (ioctl(cfd, I_STR, &sioc) < 0) {
2767c478bd9Sstevel@tonic-gate 		perror("S_IOCSETMODE");
2777c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
278*c7e4935fSss 		    "syncloop: can't set sync mode info for %s\n", portname);
2797c478bd9Sstevel@tonic-gate 		exit(1);
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate no_params:
2837c478bd9Sstevel@tonic-gate 	/* report state */
2847c478bd9Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCGETMODE;
2857c478bd9Sstevel@tonic-gate 	sioc.ic_timout = -1;
2867c478bd9Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct scc_mode);
2877c478bd9Sstevel@tonic-gate 	sioc.ic_dp = (char *)&sm;
2887c478bd9Sstevel@tonic-gate 	if (ioctl(cfd, I_STR, &sioc) < 0) {
2897c478bd9Sstevel@tonic-gate 		perror("S_IOCGETMODE");
290*c7e4935fSss 		(void) fprintf(stderr, "syncloop: can't get sync mode info "
291*c7e4935fSss 			"for %s\n", portname);
2927c478bd9Sstevel@tonic-gate 		exit(1);
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 	(void) printf("speed=%d, loopback=%s, nrzi=%s, txc=%s, rxc=%s\n",
2957c478bd9Sstevel@tonic-gate 		sm.sm_baudrate,
2967c478bd9Sstevel@tonic-gate 		yesno[((int)(sm.sm_config & CONN_LPBK) > 0)],
2977c478bd9Sstevel@tonic-gate 		yesno[((int)(sm.sm_config & CONN_NRZI) > 0)],
2987c478bd9Sstevel@tonic-gate 		txnames[sm.sm_txclock],
2997c478bd9Sstevel@tonic-gate 		rxnames[sm.sm_rxclock]);
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	quiet_period();
3027c478bd9Sstevel@tonic-gate 	first_packet();
3037c478bd9Sstevel@tonic-gate 	many_packets();
3047c478bd9Sstevel@tonic-gate 	return (0);
3057c478bd9Sstevel@tonic-gate }
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate static void
Usage()3087c478bd9Sstevel@tonic-gate Usage()
3097c478bd9Sstevel@tonic-gate {
3107c478bd9Sstevel@tonic-gate 	(void) printf("Usage: syncloop [ options ] portname\n");
3117c478bd9Sstevel@tonic-gate 	(void) printf("Options: -c packet_count\n");
3127c478bd9Sstevel@tonic-gate 	(void) printf("         -l packet_length\n");
3137c478bd9Sstevel@tonic-gate 	(void) printf("         -s line_speed\n");
3147c478bd9Sstevel@tonic-gate 	(void) printf("         -t test_type\n");
3157c478bd9Sstevel@tonic-gate 	(void) printf("         -d hex_data_byte\n");
3167c478bd9Sstevel@tonic-gate 	exit(1);
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate static int zero_time = 0;
3207c478bd9Sstevel@tonic-gate static int short_time = 1000;
3217c478bd9Sstevel@tonic-gate static int long_time = 4000;
3227c478bd9Sstevel@tonic-gate static char bigbuf[4096];
3237c478bd9Sstevel@tonic-gate static char packet[MAXPACKET];
3247c478bd9Sstevel@tonic-gate static struct pollfd pfd;
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate static void
quiet_period()3277c478bd9Sstevel@tonic-gate quiet_period()
3287c478bd9Sstevel@tonic-gate {
3297c478bd9Sstevel@tonic-gate 	(void) printf("[ checking for quiet line ]\n");
3307c478bd9Sstevel@tonic-gate 	pfd.fd = dfd;
3317c478bd9Sstevel@tonic-gate 	pfd.events = POLLIN;
3327c478bd9Sstevel@tonic-gate 	pfd.revents = 0;
3337c478bd9Sstevel@tonic-gate 	while (poll(&pfd, 1, short_time) == 1) {
3347c478bd9Sstevel@tonic-gate 		(void) read(dfd, bigbuf, sizeof (bigbuf));
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 	if (poll(&pfd, 1, long_time) == 1) {
3377c478bd9Sstevel@tonic-gate 		(void) printf("packet received but none sent!\n");
3387c478bd9Sstevel@tonic-gate 		(void) printf("quiesce other end before starting syncloop\n");
3397c478bd9Sstevel@tonic-gate 		exit(1);
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate static void
first_packet()3447c478bd9Sstevel@tonic-gate first_packet()
3457c478bd9Sstevel@tonic-gate {
3467c478bd9Sstevel@tonic-gate 	int i, len;
3477c478bd9Sstevel@tonic-gate 	int pollret;
3487c478bd9Sstevel@tonic-gate 	struct strioctl sioc;
3497c478bd9Sstevel@tonic-gate 	struct sl_stats start_stats, end_stats;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	for (i = 0; i < reclen; i++)
3527c478bd9Sstevel@tonic-gate 		packet[i] = (data == -1) ? rand() : data;
3537c478bd9Sstevel@tonic-gate 	(void) printf("[ Trying first packet ]\n");
3547c478bd9Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCGETSTATS;
3557c478bd9Sstevel@tonic-gate 	sioc.ic_timout = -1;
3567c478bd9Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct sl_stats);
3577c478bd9Sstevel@tonic-gate 	sioc.ic_dp = (char *)&start_stats;
3587c478bd9Sstevel@tonic-gate 	if (ioctl(cfd, I_STR, &sioc) < 0) {
3597c478bd9Sstevel@tonic-gate 		perror("S_IOCGETSTATS");
3607c478bd9Sstevel@tonic-gate 		exit(1);
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	for (i = 0; i < 5; i++) {
3647c478bd9Sstevel@tonic-gate 		if (write(dfd, packet, reclen) != reclen) {
3657c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
3667c478bd9Sstevel@tonic-gate 				"packet write failed, errno %d\n",
3677c478bd9Sstevel@tonic-gate 				errno);
3687c478bd9Sstevel@tonic-gate 			exit(1);
3697c478bd9Sstevel@tonic-gate 		}
3707c478bd9Sstevel@tonic-gate 		pfd.fd = dfd;
3717c478bd9Sstevel@tonic-gate 		pfd.events = POLLIN;
3727c478bd9Sstevel@tonic-gate 		pollret = poll(&pfd, 1, long_time);
3737c478bd9Sstevel@tonic-gate 		if (pollret < 0) perror("poll");
3747c478bd9Sstevel@tonic-gate 		if (pollret == 0)
3757c478bd9Sstevel@tonic-gate 			(void) printf("poll: nothing to read.\n");
3767c478bd9Sstevel@tonic-gate 		if (pollret == 1) {
3777c478bd9Sstevel@tonic-gate 			len = read(dfd, bigbuf, reclen);
3787c478bd9Sstevel@tonic-gate 			if (len == reclen && memcmp(packet, bigbuf, len) == 0)
3797c478bd9Sstevel@tonic-gate 				return;	/* success */
3807c478bd9Sstevel@tonic-gate 			else {
3817c478bd9Sstevel@tonic-gate 				(void) printf("len %d should be %d\n",
3827c478bd9Sstevel@tonic-gate 					len, reclen);
3837c478bd9Sstevel@tonic-gate 				if (verbose) {
3847c478bd9Sstevel@tonic-gate 					(void) printf("           ");
3857c478bd9Sstevel@tonic-gate 					printhex(bigbuf, len);
3867c478bd9Sstevel@tonic-gate 					(void) printf("\nshould be ");
3877c478bd9Sstevel@tonic-gate 					printhex(packet, reclen);
3887c478bd9Sstevel@tonic-gate 					(void) printf("\n");
3897c478bd9Sstevel@tonic-gate 				}
3907c478bd9Sstevel@tonic-gate 			}
3917c478bd9Sstevel@tonic-gate 		}
3927c478bd9Sstevel@tonic-gate 	}
3937c478bd9Sstevel@tonic-gate 	(void) printf("Loopback has TOTALLY FAILED - ");
3947c478bd9Sstevel@tonic-gate 	(void) printf("no packets returned after 5 attempts\n");
3957c478bd9Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCGETSTATS;
3967c478bd9Sstevel@tonic-gate 	sioc.ic_timout = -1;
3977c478bd9Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct sl_stats);
3987c478bd9Sstevel@tonic-gate 	sioc.ic_dp = (char *)&end_stats;
3997c478bd9Sstevel@tonic-gate 	if (ioctl(cfd, I_STR, &sioc) < 0) {
4007c478bd9Sstevel@tonic-gate 		perror("S_IOCGETSTATS");
4017c478bd9Sstevel@tonic-gate 		exit(1);
4027c478bd9Sstevel@tonic-gate 	}
4037c478bd9Sstevel@tonic-gate 	if (start_stats.opack == end_stats.opack)
4047c478bd9Sstevel@tonic-gate 		(void) printf(
4057c478bd9Sstevel@tonic-gate 			"No packets transmitted - no transmit clock present\n");
4067c478bd9Sstevel@tonic-gate 	exit(1);
4077c478bd9Sstevel@tonic-gate }
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate static void
many_packets()4107c478bd9Sstevel@tonic-gate many_packets()
4117c478bd9Sstevel@tonic-gate {
4127c478bd9Sstevel@tonic-gate 	struct strioctl sioc;
4137c478bd9Sstevel@tonic-gate 	struct sl_stats start_stats, end_stats;
4147c478bd9Sstevel@tonic-gate 	struct timeval start_time, end_time;
4157c478bd9Sstevel@tonic-gate 	int baddata = 0;
4167c478bd9Sstevel@tonic-gate 	float secs, speed;
4177c478bd9Sstevel@tonic-gate 	int i, len;
4187c478bd9Sstevel@tonic-gate 	int incount = 0;
4197c478bd9Sstevel@tonic-gate 	long prev_sec = -1;
4207c478bd9Sstevel@tonic-gate 	int pollret;
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	(void) printf("[ Trying many packets ]\n");
4237c478bd9Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCGETSTATS;
4247c478bd9Sstevel@tonic-gate 	sioc.ic_timout = -1;
4257c478bd9Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct sl_stats);
4267c478bd9Sstevel@tonic-gate 	sioc.ic_dp = (char *)&start_stats;
4277c478bd9Sstevel@tonic-gate 	if (ioctl(cfd, I_STR, &sioc) < 0) {
4287c478bd9Sstevel@tonic-gate 		perror("S_IOCGETSTATS");
4297c478bd9Sstevel@tonic-gate 		exit(1);
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate 	(void) gettimeofday(&start_time, 0);
4327c478bd9Sstevel@tonic-gate 	end_time = start_time;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	i = 0;
4357c478bd9Sstevel@tonic-gate 	while (i < reccount) {
4367c478bd9Sstevel@tonic-gate 		if (end_time.tv_sec != prev_sec) {
4377c478bd9Sstevel@tonic-gate 			prev_sec = end_time.tv_sec;
4387c478bd9Sstevel@tonic-gate 			(void) printf("\r %d ", incount);
4397c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
4407c478bd9Sstevel@tonic-gate 		}
4417c478bd9Sstevel@tonic-gate 		pfd.fd = dfd;
4427c478bd9Sstevel@tonic-gate 		pfd.events = POLLIN;
4437c478bd9Sstevel@tonic-gate 		while (pollret = poll(&pfd, 1, zero_time)) {
4447c478bd9Sstevel@tonic-gate 			if (pollret < 0)
4457c478bd9Sstevel@tonic-gate 				perror("poll");
4467c478bd9Sstevel@tonic-gate 			else {
4477c478bd9Sstevel@tonic-gate 				(void) lseek(dfd, (long)0, 0);
4487c478bd9Sstevel@tonic-gate 				len = read(dfd, bigbuf, reclen);
4497c478bd9Sstevel@tonic-gate 				if (len != reclen ||
4507c478bd9Sstevel@tonic-gate 				    memcmp(packet, bigbuf, len) != 0) {
4517c478bd9Sstevel@tonic-gate 					(void) printf("len %d should be %d\n",
4527c478bd9Sstevel@tonic-gate 						len, reclen);
4537c478bd9Sstevel@tonic-gate 					if (verbose) {
4547c478bd9Sstevel@tonic-gate 						(void) printf("           ");
4557c478bd9Sstevel@tonic-gate 						printhex(bigbuf, len);
4567c478bd9Sstevel@tonic-gate 						(void) printf("\nshould be ");
4577c478bd9Sstevel@tonic-gate 						printhex(packet, reclen);
4587c478bd9Sstevel@tonic-gate 						(void) printf("\n");
4597c478bd9Sstevel@tonic-gate 					}
4607c478bd9Sstevel@tonic-gate 					baddata++;
4617c478bd9Sstevel@tonic-gate 				}
4627c478bd9Sstevel@tonic-gate 				incount++;
4637c478bd9Sstevel@tonic-gate 				(void) gettimeofday(&end_time, 0);
4647c478bd9Sstevel@tonic-gate 			}
4657c478bd9Sstevel@tonic-gate 		}
4667c478bd9Sstevel@tonic-gate 		pfd.fd = dfd;
4677c478bd9Sstevel@tonic-gate 		pfd.events = POLLIN|POLLOUT;
4687c478bd9Sstevel@tonic-gate 		pollret = poll(&pfd, 1, long_time);
4697c478bd9Sstevel@tonic-gate 		if (pollret < 0)
4707c478bd9Sstevel@tonic-gate 			perror("poll");
4717c478bd9Sstevel@tonic-gate 		if (pollret == 0)
4727c478bd9Sstevel@tonic-gate 			(void) printf("poll: nothing to read or write.\n");
4737c478bd9Sstevel@tonic-gate 		if (pollret == 1) {
4747c478bd9Sstevel@tonic-gate 			if (pfd.revents & POLLOUT) {
4757c478bd9Sstevel@tonic-gate 				(void) write(dfd, packet, reclen);
4767c478bd9Sstevel@tonic-gate 				i++;
4777c478bd9Sstevel@tonic-gate 			} else if (!(pfd.revents & POLLIN)) {
4787c478bd9Sstevel@tonic-gate 				(void) printf("OUTPUT HAS LOCKED UP!!!\n");
4797c478bd9Sstevel@tonic-gate 				break;
4807c478bd9Sstevel@tonic-gate 			}
4817c478bd9Sstevel@tonic-gate 		}
4827c478bd9Sstevel@tonic-gate 	}
4837c478bd9Sstevel@tonic-gate 	pfd.fd = dfd;
4847c478bd9Sstevel@tonic-gate 	pfd.events = POLLIN;
4857c478bd9Sstevel@tonic-gate 	while ((incount < reccount) && (poll(&pfd, 1, long_time) == 1)) {
4867c478bd9Sstevel@tonic-gate 		if (end_time.tv_sec != prev_sec) {
4877c478bd9Sstevel@tonic-gate 			prev_sec = end_time.tv_sec;
4887c478bd9Sstevel@tonic-gate 			(void) printf("\r %d ", incount);
4897c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
4907c478bd9Sstevel@tonic-gate 		}
4917c478bd9Sstevel@tonic-gate 		len = read(dfd, bigbuf, reclen);
4927c478bd9Sstevel@tonic-gate 		if (len != reclen || memcmp(packet, bigbuf, len) != 0) {
4937c478bd9Sstevel@tonic-gate 			(void) printf("len %d should be %d\n", len, reclen);
4947c478bd9Sstevel@tonic-gate 			if (verbose) {
4957c478bd9Sstevel@tonic-gate 				(void) printf("           ");
4967c478bd9Sstevel@tonic-gate 				printhex(bigbuf, len);
4977c478bd9Sstevel@tonic-gate 				(void) printf("\nshould be ");
4987c478bd9Sstevel@tonic-gate 				printhex(packet, reclen);
4997c478bd9Sstevel@tonic-gate 				(void) printf("\n");
5007c478bd9Sstevel@tonic-gate 			}
5017c478bd9Sstevel@tonic-gate 			baddata++;
5027c478bd9Sstevel@tonic-gate 		}
5037c478bd9Sstevel@tonic-gate 		incount++;
5047c478bd9Sstevel@tonic-gate 		(void) gettimeofday(&end_time, 0);
5057c478bd9Sstevel@tonic-gate 	}
5067c478bd9Sstevel@tonic-gate 	(void) printf("\r %d \n", incount);
5077c478bd9Sstevel@tonic-gate 	if (baddata)
5087c478bd9Sstevel@tonic-gate 		(void) printf("%d packets with wrong data received!\n",
5097c478bd9Sstevel@tonic-gate 			baddata);
5107c478bd9Sstevel@tonic-gate 	sioc.ic_cmd = S_IOCGETSTATS;
5117c478bd9Sstevel@tonic-gate 	sioc.ic_timout = -1;
5127c478bd9Sstevel@tonic-gate 	sioc.ic_len = sizeof (struct sl_stats);
5137c478bd9Sstevel@tonic-gate 	sioc.ic_dp = (char *)&end_stats;
5147c478bd9Sstevel@tonic-gate 	if (ioctl(cfd, I_STR, &sioc) < 0) {
5157c478bd9Sstevel@tonic-gate 		perror("S_IOCGETSTATS");
5167c478bd9Sstevel@tonic-gate 		exit(1);
5177c478bd9Sstevel@tonic-gate 	}
5187c478bd9Sstevel@tonic-gate 	end_stats.ipack -= start_stats.ipack;
5197c478bd9Sstevel@tonic-gate 	end_stats.opack -= start_stats.opack;
5207c478bd9Sstevel@tonic-gate 	end_stats.abort -= start_stats.abort;
5217c478bd9Sstevel@tonic-gate 	end_stats.crc -= start_stats.crc;
5227c478bd9Sstevel@tonic-gate 	end_stats.overrun -= start_stats.overrun;
5237c478bd9Sstevel@tonic-gate 	end_stats.underrun -= start_stats.underrun;
5247c478bd9Sstevel@tonic-gate 	end_stats.ierror -= start_stats.ierror;
5257c478bd9Sstevel@tonic-gate 	end_stats.oerror -= start_stats.oerror;
5267c478bd9Sstevel@tonic-gate 	if (reccount > end_stats.opack)
5277c478bd9Sstevel@tonic-gate 		(void) printf("%d packets lost in outbound queueing\n",
5287c478bd9Sstevel@tonic-gate 			reccount - end_stats.opack);
5297c478bd9Sstevel@tonic-gate 	if (incount < end_stats.ipack && incount < reccount)
5307c478bd9Sstevel@tonic-gate 		(void) printf("%d packets lost in inbound queueing\n",
5317c478bd9Sstevel@tonic-gate 			end_stats.ipack - incount);
5327c478bd9Sstevel@tonic-gate 	(void) printf("%d packets sent, %d received\n", reccount, incount);
5337c478bd9Sstevel@tonic-gate 	(void) printf("CRC errors    Aborts   Overruns  Underruns         ");
5347c478bd9Sstevel@tonic-gate 	(void) printf("   In <-Drops-> Out\n%9d  %9d  %9d  %9d  %12d  %12d\n",
5357c478bd9Sstevel@tonic-gate 		end_stats.crc, end_stats.abort,
5367c478bd9Sstevel@tonic-gate 		end_stats.overrun, end_stats.underrun,
5377c478bd9Sstevel@tonic-gate 		end_stats.ierror, end_stats.oerror);
5387c478bd9Sstevel@tonic-gate 	secs = (float)(end_time.tv_usec - start_time.tv_usec) / 1000000.0;
5397c478bd9Sstevel@tonic-gate 	secs += (float)(end_time.tv_sec - start_time.tv_sec);
5407c478bd9Sstevel@tonic-gate 	if (secs) {
5417c478bd9Sstevel@tonic-gate 		speed = 8 * incount * (4 + reclen) / secs;
5427c478bd9Sstevel@tonic-gate 		(void) printf("estimated line speed = %d bps\n", (int)speed);
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate }
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate static void
printhex(char * cp,int len)5477c478bd9Sstevel@tonic-gate printhex(char *cp, int len)
5487c478bd9Sstevel@tonic-gate {
5497c478bd9Sstevel@tonic-gate 	char c, *hex = "0123456789ABCDEF";
5507c478bd9Sstevel@tonic-gate 	int i;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i++) {
5537c478bd9Sstevel@tonic-gate 		c = *cp++;
5547c478bd9Sstevel@tonic-gate 		(void) putchar(hex[(c >> 4) & 0xF]);
5557c478bd9Sstevel@tonic-gate 		(void) putchar(hex[c & 0xF]);
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate }
558