xref: /titanic_50/usr/src/cmd/mt/mt.c (revision 8d489c7a815fcac696803219572e95aa01532b0f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
87c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley Software License Agreement
97c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
107c478bd9Sstevel@tonic-gate  */
117c478bd9Sstevel@tonic-gate 
127c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate /*
157c478bd9Sstevel@tonic-gate  * mt -- magnetic tape manipulation program
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate #include <stdio.h>
187c478bd9Sstevel@tonic-gate #include <ctype.h>
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate #include <errno.h>
217c478bd9Sstevel@tonic-gate #include <sys/types.h>
227c478bd9Sstevel@tonic-gate #include <sys/mtio.h>
237c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
247c478bd9Sstevel@tonic-gate #include <sys/param.h>
257c478bd9Sstevel@tonic-gate #include <sys/buf.h>
267c478bd9Sstevel@tonic-gate #include <sys/conf.h>
277c478bd9Sstevel@tonic-gate #include <sys/file.h>
287c478bd9Sstevel@tonic-gate #include <sys/uio.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <sys/stat.h>
337c478bd9Sstevel@tonic-gate #include <sys/scsi/targets/stdef.h>
347c478bd9Sstevel@tonic-gate #include <fcntl.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #define	equal(s1, s2)	(strcmp(s1, s2) == 0)
387c478bd9Sstevel@tonic-gate #define	MTASF		100	/* absolute file positioning; first file is 0 */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * This can't be DEFTAPE in mtio.h because that is currently the rewinding
427c478bd9Sstevel@tonic-gate  * unit which makes 'mt fsf' a questionable activity at best.
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate #define	DEFAULT_NRW_TAPE	"/dev/rmt/0n"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate static int mtfd;
477c478bd9Sstevel@tonic-gate static struct mtop mt_com;
487c478bd9Sstevel@tonic-gate static struct mtget mt_status;
497c478bd9Sstevel@tonic-gate static void print_config(void);
507c478bd9Sstevel@tonic-gate static char *print_key(short key_code);
517c478bd9Sstevel@tonic-gate static void printreg(char *, ushort_t, char *);
527c478bd9Sstevel@tonic-gate static void status(struct mtget *);
537c478bd9Sstevel@tonic-gate static char *tape;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /* Pseudo flag for open even if drive is not ready (Unloaded) or reserved */
567c478bd9Sstevel@tonic-gate #define	O_UNLOAD (O_RDWR | O_NDELAY)
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate static struct commands {
597c478bd9Sstevel@tonic-gate 	char *c_name;
607c478bd9Sstevel@tonic-gate 	int c_code;
617c478bd9Sstevel@tonic-gate 	int c_oflag;
627c478bd9Sstevel@tonic-gate 	int c_usecnt;
637c478bd9Sstevel@tonic-gate } com[] = {
647c478bd9Sstevel@tonic-gate 	{ "weof",		MTWEOF,			O_RDWR,		1 },
657c478bd9Sstevel@tonic-gate 	{ "eof",		MTWEOF,			O_RDWR,		1 },
667c478bd9Sstevel@tonic-gate 	{ "fsf",		MTFSF,			O_RDONLY,	1 },
677c478bd9Sstevel@tonic-gate 	{ "bsf",		MTBSF,			O_RDONLY,	1 },
687c478bd9Sstevel@tonic-gate 	{ "asf",		MTASF,			O_RDONLY,	1 },
697c478bd9Sstevel@tonic-gate 	{ "fsr",		MTFSR,			O_RDONLY,	1 },
707c478bd9Sstevel@tonic-gate 	{ "bsr",		MTBSR,			O_RDONLY,	1 },
717c478bd9Sstevel@tonic-gate 	{ "rewind",		MTREW,			O_RDONLY,	0 },
727c478bd9Sstevel@tonic-gate 	{ "offline",		MTOFFL,			O_RDONLY,	0 },
737c478bd9Sstevel@tonic-gate 	{ "rewoffl",		MTOFFL,			O_RDONLY,	0 },
747c478bd9Sstevel@tonic-gate 	{ "status",		MTNOP,			O_RDONLY,	0 },
757c478bd9Sstevel@tonic-gate 	{ "retension",		MTRETEN,		O_RDONLY,	0 },
767c478bd9Sstevel@tonic-gate 	{ "erase",		MTERASE,		O_RDWR,		0 },
777c478bd9Sstevel@tonic-gate 	{ "eom",		MTEOM,			O_RDONLY,	0 },
787c478bd9Sstevel@tonic-gate 	{ "nbsf",		MTNBSF,			O_RDONLY,	1 },
797c478bd9Sstevel@tonic-gate 	{ "reserve",		MTIOCRESERVE,		O_RDONLY,	0 },
807c478bd9Sstevel@tonic-gate 	{ "release",		MTIOCRELEASE,		O_RDONLY,	0 },
817c478bd9Sstevel@tonic-gate 	{ "forcereserve",	MTIOCFORCERESERVE,	O_UNLOAD,	0 },
827c478bd9Sstevel@tonic-gate 	{ "config",		MTIOCGETDRIVETYPE,	O_UNLOAD,	0 },
837c478bd9Sstevel@tonic-gate 	{ 0 }
847c478bd9Sstevel@tonic-gate };
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #ifdef sun
877c478bd9Sstevel@tonic-gate static struct mt_tape_info tapes[] = MT_TAPE_INFO;
887c478bd9Sstevel@tonic-gate #endif /* sun */
897c478bd9Sstevel@tonic-gate 
90*8d489c7aSmuffin int
917c478bd9Sstevel@tonic-gate main(int argc, char **argv)
927c478bd9Sstevel@tonic-gate {
93*8d489c7aSmuffin 	char *cp;
94*8d489c7aSmuffin 	struct commands *comp;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	if (argc > 2 && (equal(argv[1], "-t") || equal(argv[1], "-f"))) {
977c478bd9Sstevel@tonic-gate 		argc -= 2;
987c478bd9Sstevel@tonic-gate 		tape = argv[2];
997c478bd9Sstevel@tonic-gate 		argv += 2;
1007c478bd9Sstevel@tonic-gate 	} else
1017c478bd9Sstevel@tonic-gate 		if ((tape = getenv("TAPE")) == NULL)
1027c478bd9Sstevel@tonic-gate 			tape = DEFAULT_NRW_TAPE;
1037c478bd9Sstevel@tonic-gate 	if (argc < 2) {
1047c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1057c478bd9Sstevel@tonic-gate 		    "usage: mt [ -f device ] command [ count ]\n");
1067c478bd9Sstevel@tonic-gate 		exit(1);
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 	cp = argv[1];
1097c478bd9Sstevel@tonic-gate 	for (comp = com; comp->c_name != NULL; comp++)
1107c478bd9Sstevel@tonic-gate 		if (strncmp(cp, comp->c_name, strlen(cp)) == 0)
1117c478bd9Sstevel@tonic-gate 			break;
1127c478bd9Sstevel@tonic-gate 	if (comp->c_name == NULL) {
1137c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "mt: unknown command: %s\n", cp);
1147c478bd9Sstevel@tonic-gate 		exit(1);
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if ((mtfd = open(tape, comp->c_oflag)) < 0) {
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 		/*
1207c478bd9Sstevel@tonic-gate 		 * Provide additional error message decoding since
1217c478bd9Sstevel@tonic-gate 		 * we need additional error codes to fix them problem.
1227c478bd9Sstevel@tonic-gate 		 */
1237c478bd9Sstevel@tonic-gate 		if (errno == EIO) {
1247c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1257c478bd9Sstevel@tonic-gate 			    "%s: no tape loaded or drive offline\n",
1267c478bd9Sstevel@tonic-gate 			    tape);
1277c478bd9Sstevel@tonic-gate 		} else if (errno == EACCES) {
1287c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1297c478bd9Sstevel@tonic-gate 			    "%s: write protected or reserved.\n", tape);
1307c478bd9Sstevel@tonic-gate 		} else {
1317c478bd9Sstevel@tonic-gate 			perror(tape);
1327c478bd9Sstevel@tonic-gate 		}
1337c478bd9Sstevel@tonic-gate 		exit(1);
1347c478bd9Sstevel@tonic-gate 	}
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	if (comp->c_code == MTIOCFORCERESERVE ||
1377c478bd9Sstevel@tonic-gate 	    comp->c_code == MTIOCRESERVE ||
1387c478bd9Sstevel@tonic-gate 	    comp->c_code == MTIOCRELEASE)  {
1397c478bd9Sstevel@tonic-gate 		/*
1407c478bd9Sstevel@tonic-gate 		 * Handle all MTIOC ioctls used in
1417c478bd9Sstevel@tonic-gate 		 * reservation/release/takeownership.
1427c478bd9Sstevel@tonic-gate 		 */
1437c478bd9Sstevel@tonic-gate 		if (ioctl(mtfd, comp->c_code) < 0) {
1447c478bd9Sstevel@tonic-gate 			perror("mt");
1457c478bd9Sstevel@tonic-gate 			exit(2);
1467c478bd9Sstevel@tonic-gate 		}
1477c478bd9Sstevel@tonic-gate 	} else if (comp->c_code == MTASF) {
1487c478bd9Sstevel@tonic-gate 		/*
1497c478bd9Sstevel@tonic-gate 		 * Handle absolute file positioning.  Ask tape driver
1507c478bd9Sstevel@tonic-gate 		 * where tape is and then skip to desired file.  If
1517c478bd9Sstevel@tonic-gate 		 * driver doesn't support get location ioctl, rewind
1527c478bd9Sstevel@tonic-gate 		 * the tape and then space to the desired file.
1537c478bd9Sstevel@tonic-gate 		 */
1547c478bd9Sstevel@tonic-gate 		int usecnt;
1557c478bd9Sstevel@tonic-gate 		int mt_fileno;
1567c478bd9Sstevel@tonic-gate 		struct mtget mt_status;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 		usecnt = argc > 2 && comp->c_usecnt;
1597c478bd9Sstevel@tonic-gate 		mt_fileno = usecnt ? atoi(argv[2]) : 1;
1607c478bd9Sstevel@tonic-gate 		if (mt_fileno < 0) {
1617c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "mt: negative file number\n");
1627c478bd9Sstevel@tonic-gate 			exit(1);
1637c478bd9Sstevel@tonic-gate 		}
1647c478bd9Sstevel@tonic-gate 		(void) ioctl(mtfd, MTIOCGET, (char *)&mt_status);
1657c478bd9Sstevel@tonic-gate 		if (ioctl(mtfd, MTIOCGET, (char *)&mt_status) < 0) {
1667c478bd9Sstevel@tonic-gate 			perror("mt");
1677c478bd9Sstevel@tonic-gate 			exit(2);
1687c478bd9Sstevel@tonic-gate 		}
1697c478bd9Sstevel@tonic-gate 		/*
1707c478bd9Sstevel@tonic-gate 		 * Check if device supports reporting current file
1717c478bd9Sstevel@tonic-gate 		 * tape file position.  If not, rewind the tape, and
1727c478bd9Sstevel@tonic-gate 		 * space forward.
1737c478bd9Sstevel@tonic-gate 		 */
1747c478bd9Sstevel@tonic-gate 		if (!(mt_status.mt_flags & MTF_ASF)) {
1757c478bd9Sstevel@tonic-gate 			/* printf("mt: rewind\n"); */
1767c478bd9Sstevel@tonic-gate 			mt_status.mt_fileno = 0;
1777c478bd9Sstevel@tonic-gate 			mt_com.mt_count = 1;
1787c478bd9Sstevel@tonic-gate 			mt_com.mt_op = MTREW;
1797c478bd9Sstevel@tonic-gate 			if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0) {
1807c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "%s %s %d ",
1817c478bd9Sstevel@tonic-gate 				    tape, comp->c_name, mt_fileno);
1827c478bd9Sstevel@tonic-gate 				perror("mt");
1837c478bd9Sstevel@tonic-gate 				exit(2);
1847c478bd9Sstevel@tonic-gate 			}
1857c478bd9Sstevel@tonic-gate 		}
1867c478bd9Sstevel@tonic-gate 		if (mt_fileno < mt_status.mt_fileno) {
1877c478bd9Sstevel@tonic-gate 			mt_com.mt_op = MTNBSF;
1887c478bd9Sstevel@tonic-gate 			mt_com.mt_count =  mt_status.mt_fileno - mt_fileno;
1897c478bd9Sstevel@tonic-gate 			/* printf("mt: bsf= %d\n", mt_com.mt_count); */
1907c478bd9Sstevel@tonic-gate 		} else {
1917c478bd9Sstevel@tonic-gate 			mt_com.mt_op = MTFSF;
1927c478bd9Sstevel@tonic-gate 			mt_com.mt_count =  mt_fileno - mt_status.mt_fileno;
1937c478bd9Sstevel@tonic-gate 			/* printf("mt: fsf= %d\n", mt_com.mt_count); */
1947c478bd9Sstevel@tonic-gate 		}
1957c478bd9Sstevel@tonic-gate 		if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0) {
1967c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s %s %d ", tape, comp->c_name,
1977c478bd9Sstevel@tonic-gate 			    mt_fileno);
1987c478bd9Sstevel@tonic-gate 			perror("failed");
1997c478bd9Sstevel@tonic-gate 			exit(2);
2007c478bd9Sstevel@tonic-gate 		}
2017c478bd9Sstevel@tonic-gate 	} else if (comp->c_code == MTIOCGETDRIVETYPE) {
2027c478bd9Sstevel@tonic-gate 		print_config();
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	/* Handle regular mag tape ioctls */
2057c478bd9Sstevel@tonic-gate 	} else if (comp->c_code != MTNOP) {
2067c478bd9Sstevel@tonic-gate 		int usecnt;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 		mt_com.mt_op = comp->c_code;
2097c478bd9Sstevel@tonic-gate 		usecnt = argc > 2 && comp->c_usecnt;
2107c478bd9Sstevel@tonic-gate 		mt_com.mt_count = (usecnt ? atoi(argv[2]) : 1);
2117c478bd9Sstevel@tonic-gate 		if (mt_com.mt_count < 0) {
2127c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "mt: negative repeat count\n");
2137c478bd9Sstevel@tonic-gate 			exit(1);
2147c478bd9Sstevel@tonic-gate 		}
2157c478bd9Sstevel@tonic-gate 		if (ioctl(mtfd, MTIOCTOP, &mt_com) < 0) {
2167c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s %s %ld ", tape, comp->c_name,
2177c478bd9Sstevel@tonic-gate 			    mt_com.mt_count);
2187c478bd9Sstevel@tonic-gate 			perror("failed");
2197c478bd9Sstevel@tonic-gate 			exit(2);
2207c478bd9Sstevel@tonic-gate 		}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	/* Handle status ioctl */
2237c478bd9Sstevel@tonic-gate 	} else {
2247c478bd9Sstevel@tonic-gate 		if (ioctl(mtfd, MTIOCGET, (char *)&mt_status) < 0) {
2257c478bd9Sstevel@tonic-gate 			perror("mt");
2267c478bd9Sstevel@tonic-gate 			exit(2);
2277c478bd9Sstevel@tonic-gate 		}
2287c478bd9Sstevel@tonic-gate 		status(&mt_status);
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 	return (0);
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate static void
2347c478bd9Sstevel@tonic-gate print_config(void)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	struct mtdrivetype mdt;
2377c478bd9Sstevel@tonic-gate 	struct mtdrivetype_request mdt_req;
2387c478bd9Sstevel@tonic-gate 	char cfgname[48];
2397c478bd9Sstevel@tonic-gate 	char tmp[2];
2407c478bd9Sstevel@tonic-gate 	char *name;
2417c478bd9Sstevel@tonic-gate 	int i;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	mdt_req.size = sizeof (mdt);
2447c478bd9Sstevel@tonic-gate 	mdt_req.mtdtp = &mdt;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	if (ioctl(mtfd, MTIOCGETDRIVETYPE, &mdt_req) != 0) {
2477c478bd9Sstevel@tonic-gate 		perror("mt config");
2487c478bd9Sstevel@tonic-gate 		return;
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	/*
2527c478bd9Sstevel@tonic-gate 	 * remove trailing spaces from product id.
2537c478bd9Sstevel@tonic-gate 	 */
2547c478bd9Sstevel@tonic-gate 	for (i = VIDPIDLEN; i; i--) {
2557c478bd9Sstevel@tonic-gate 		if (isspace(mdt.vid[i]) || mdt.vid[i] == '*') {
2567c478bd9Sstevel@tonic-gate 			mdt.vid[i] = 0;
2577c478bd9Sstevel@tonic-gate 		} else if (mdt.vid[i] == 0) {
2587c478bd9Sstevel@tonic-gate 			continue;
2597c478bd9Sstevel@tonic-gate 		} else {
2607c478bd9Sstevel@tonic-gate 			break;
2617c478bd9Sstevel@tonic-gate 		}
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	/*
2657c478bd9Sstevel@tonic-gate 	 * If this is a generic name display the Vid and Pid instead.
2667c478bd9Sstevel@tonic-gate 	 */
2677c478bd9Sstevel@tonic-gate 	if (strstr(mdt.name, "Vendor '") == NULL) {
2687c478bd9Sstevel@tonic-gate 		name = mdt.name;
2697c478bd9Sstevel@tonic-gate 	} else {
2707c478bd9Sstevel@tonic-gate 		name = mdt.vid;
2717c478bd9Sstevel@tonic-gate 	}
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	/*
2747c478bd9Sstevel@tonic-gate 	 * Attempt to create a configuration name using vid and pid.
2757c478bd9Sstevel@tonic-gate 	 */
2767c478bd9Sstevel@tonic-gate 	(void) strcpy(cfgname, "CFG");
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	for (tmp[1] = i = 0; i < VIDPIDLEN; i++) {
2797c478bd9Sstevel@tonic-gate 		if (!isalnum(name[i]))
2807c478bd9Sstevel@tonic-gate 			continue;
2817c478bd9Sstevel@tonic-gate 		if (isspace(name[i]))
2827c478bd9Sstevel@tonic-gate 			continue;
2837c478bd9Sstevel@tonic-gate 		tmp[0] = toupper(name[i]);
2847c478bd9Sstevel@tonic-gate 		(void) strncat(cfgname, tmp, 1);
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	(void) printf("\"%s\", \"%s\", \"%s\";\n", mdt.vid, name, cfgname);
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	/*
2907c478bd9Sstevel@tonic-gate 	 * Don't want show some bits, ST_DYNAMIC is set in the driver
2917c478bd9Sstevel@tonic-gate 	 * so one can tell that its not a compiled in config.
2927c478bd9Sstevel@tonic-gate 	 * The ST_LONG_ERASE and ST_LONG_TIMEOUTS are not displayed
2937c478bd9Sstevel@tonic-gate 	 * becouse the timeout values below already reflect them being
2947c478bd9Sstevel@tonic-gate 	 * set.
2957c478bd9Sstevel@tonic-gate 	 * Also ST_KNOWS_MEDIA is not displayed as it can not be configured
2967c478bd9Sstevel@tonic-gate 	 * from an st.conf entry.
2977c478bd9Sstevel@tonic-gate 	 */
2987c478bd9Sstevel@tonic-gate 	(void) printf("%s = 2,0x%X,%d,0x%X,", cfgname,
2997c478bd9Sstevel@tonic-gate 	    mdt.type, mdt.bsize, mdt.options &
3007c478bd9Sstevel@tonic-gate 	    ~(ST_DYNAMIC | ST_LONG_ERASE | ST_LONG_TIMEOUTS | ST_KNOWS_MEDIA));
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	(void) printf("4,0x%2.2X,0x%2.2X,0x%2.2X,0x%2.2X,%d,",
3037c478bd9Sstevel@tonic-gate 	    mdt.densities[0], mdt.densities[1], mdt.densities[2],
3047c478bd9Sstevel@tonic-gate 	    mdt.densities[3], mdt.default_density >> 3);
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	(void) printf("%d,%d,%d,%d,%d,%d,%d;\n", mdt.non_motion_timeout,
3077c478bd9Sstevel@tonic-gate 	    mdt.io_timeout, mdt.rewind_timeout, mdt.space_timeout,
3087c478bd9Sstevel@tonic-gate 	    mdt.load_timeout, mdt.unload_timeout, mdt.erase_timeout);
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate /*
3127c478bd9Sstevel@tonic-gate  * Interpret the status buffer returned
3137c478bd9Sstevel@tonic-gate  */
3147c478bd9Sstevel@tonic-gate static void
315*8d489c7aSmuffin status(struct mtget *bp)
3167c478bd9Sstevel@tonic-gate {
317*8d489c7aSmuffin 	struct mt_tape_info *mt = NULL;
3187c478bd9Sstevel@tonic-gate 	struct mtdrivetype mdt;
3197c478bd9Sstevel@tonic-gate 	struct mtdrivetype_request mdt_req;
3207c478bd9Sstevel@tonic-gate 	char *name = (char *)NULL;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	/*
3237c478bd9Sstevel@tonic-gate 	 * Make a call to MTIOCGETDRIVETYPE ioctl, Also use old method
3247c478bd9Sstevel@tonic-gate 	 * of MT_TAPE_INFO for now, but MT_TAPE_INFO should dissapear in 2.7
3257c478bd9Sstevel@tonic-gate 	 */
3267c478bd9Sstevel@tonic-gate 	mdt_req.size = sizeof (struct mtdrivetype);
3277c478bd9Sstevel@tonic-gate 	mdt_req.mtdtp = &mdt;
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	if (ioctl(mtfd, MTIOCGETDRIVETYPE, &mdt_req) == 0) {
3307c478bd9Sstevel@tonic-gate 		name = mdt.name;
3317c478bd9Sstevel@tonic-gate 		if (strstr(mdt.name, "Vendor '") != NULL) {
3327c478bd9Sstevel@tonic-gate 			(void) printf("Unconfigured Drive: ");
3337c478bd9Sstevel@tonic-gate 		}
3347c478bd9Sstevel@tonic-gate 	} else {
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 		for (mt = tapes; mt->t_type; mt++) {
3377c478bd9Sstevel@tonic-gate 			if (mt->t_type == bp->mt_type) {
3387c478bd9Sstevel@tonic-gate 				break;
3397c478bd9Sstevel@tonic-gate 			}
3407c478bd9Sstevel@tonic-gate 		}
3417c478bd9Sstevel@tonic-gate 	}
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	/* Handle SCSI tape drives specially. */
3447c478bd9Sstevel@tonic-gate 	if ((bp->mt_flags & MTF_SCSI)) {
3457c478bd9Sstevel@tonic-gate 		if (name == (char *)NULL) {
3467c478bd9Sstevel@tonic-gate 			if (mt->t_type == 0)
3477c478bd9Sstevel@tonic-gate 				name = "SCSI";
3487c478bd9Sstevel@tonic-gate 			else
3497c478bd9Sstevel@tonic-gate 				name = mt->t_name;
3507c478bd9Sstevel@tonic-gate 		}
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 		(void) printf("%s tape drive:\n", name);
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 		(void) printf("   sense key(0x%x)= %s   residual= %ld   ",
3567c478bd9Sstevel@tonic-gate 		    bp->mt_erreg, print_key(bp->mt_erreg), bp->mt_resid);
3577c478bd9Sstevel@tonic-gate 		(void) printf("retries= %d\n", bp->mt_dsreg);
3587c478bd9Sstevel@tonic-gate 		(void) printf("   file no= %ld   block no= %ld\n",
3597c478bd9Sstevel@tonic-gate 		    bp->mt_fileno, bp->mt_blkno);
3607c478bd9Sstevel@tonic-gate 	} else {
3617c478bd9Sstevel@tonic-gate 		/* Handle non-SCSI drives here. */
3627c478bd9Sstevel@tonic-gate 		if (mt->t_type == 0) {
3637c478bd9Sstevel@tonic-gate 			(void) printf("unknown tape drive type (0x%x)\n",
3647c478bd9Sstevel@tonic-gate 			    bp->mt_type);
3657c478bd9Sstevel@tonic-gate 			return;
3667c478bd9Sstevel@tonic-gate 		}
3677c478bd9Sstevel@tonic-gate 		(void) printf("%s tape drive:\n   residual= %ld", mt->t_name,
3687c478bd9Sstevel@tonic-gate 		    bp->mt_resid);
3697c478bd9Sstevel@tonic-gate 		printreg("   ds", (ushort_t)bp->mt_dsreg, mt->t_dsbits);
3707c478bd9Sstevel@tonic-gate 		printreg("   er", (ushort_t)bp->mt_erreg, mt->t_erbits);
3717c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate #ifdef	sun
3777c478bd9Sstevel@tonic-gate /*
3787c478bd9Sstevel@tonic-gate  * Define SCSI sense key error messages.
3797c478bd9Sstevel@tonic-gate  *
3807c478bd9Sstevel@tonic-gate  * The first 16 sense keys are SCSI standard
3817c478bd9Sstevel@tonic-gate  * sense keys. The keys after this are
3827c478bd9Sstevel@tonic-gate  * Sun Specifice 'sense' keys- e.g., crap.
3837c478bd9Sstevel@tonic-gate  */
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate static char *standard_sense_keys[16] = {
3867c478bd9Sstevel@tonic-gate 	"No Additional Sense",		/* 0x00 */
3877c478bd9Sstevel@tonic-gate 	"Soft Error",			/* 0x01 */
3887c478bd9Sstevel@tonic-gate 	"Not Ready",			/* 0x02 */
3897c478bd9Sstevel@tonic-gate 	"Media Error",			/* 0x03 */
3907c478bd9Sstevel@tonic-gate 	"Hardware Error",		/* 0x04 */
3917c478bd9Sstevel@tonic-gate 	"Illegal Request",		/* 0x05 */
3927c478bd9Sstevel@tonic-gate 	"Unit Attention",		/* 0x06 */
3937c478bd9Sstevel@tonic-gate 	"Write Protected",		/* 0x07 */
3947c478bd9Sstevel@tonic-gate 	"Blank Check",			/* 0x08 */
3957c478bd9Sstevel@tonic-gate 	"Vendor Unique",		/* 0x09 */
3967c478bd9Sstevel@tonic-gate 	"Copy Aborted",			/* 0x0a */
3977c478bd9Sstevel@tonic-gate 	"Aborted Command",		/* 0x0b */
3987c478bd9Sstevel@tonic-gate 	"Equal Error",			/* 0x0c */
3997c478bd9Sstevel@tonic-gate 	"Volume Overflow",		/* 0x0d */
4007c478bd9Sstevel@tonic-gate 	"Miscompare Error",		/* 0x0e */
4017c478bd9Sstevel@tonic-gate 	"Reserved"			/* 0x0f */
4027c478bd9Sstevel@tonic-gate };
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate static char *sun_sense_keys[] = {
4057c478bd9Sstevel@tonic-gate 	"fatal",			/* 0x10 */
4067c478bd9Sstevel@tonic-gate 	"timeout",			/* 0x11 */
4077c478bd9Sstevel@tonic-gate 	"EOF",				/* 0x12 */
4087c478bd9Sstevel@tonic-gate 	"EOT",				/* 0x13 */
4097c478bd9Sstevel@tonic-gate 	"length error",			/* 0x14 */
4107c478bd9Sstevel@tonic-gate 	"BOT",				/* 0x15 */
4117c478bd9Sstevel@tonic-gate 	"wrong tape media",		/* 0x16 */
4127c478bd9Sstevel@tonic-gate 	0
4137c478bd9Sstevel@tonic-gate };
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate /*
4167c478bd9Sstevel@tonic-gate  * Return the text string associated with the sense key value.
4177c478bd9Sstevel@tonic-gate  */
4187c478bd9Sstevel@tonic-gate static char *
4197c478bd9Sstevel@tonic-gate print_key(short key_code)
4207c478bd9Sstevel@tonic-gate {
4217c478bd9Sstevel@tonic-gate 	static char unknown[32];
4227c478bd9Sstevel@tonic-gate 	short i;
4237c478bd9Sstevel@tonic-gate 	if (key_code >= 0 && key_code <= 0x10) {
4247c478bd9Sstevel@tonic-gate 		return (standard_sense_keys[key_code]);
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	i = 0;
4287c478bd9Sstevel@tonic-gate 	while (sun_sense_keys[i]) {
4297c478bd9Sstevel@tonic-gate 		if ((i + 0x10) == key_code) {
4307c478bd9Sstevel@tonic-gate 			return (sun_sense_keys[i]);
4317c478bd9Sstevel@tonic-gate 		} else i++;
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 	(void) sprintf(unknown, "unknown sense key: 0x%x",
4347c478bd9Sstevel@tonic-gate 	    (unsigned int) key_code);
4357c478bd9Sstevel@tonic-gate 	return (unknown);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate #endif
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate /*
4417c478bd9Sstevel@tonic-gate  * Print a register a la the %b format of the kernel's printf
4427c478bd9Sstevel@tonic-gate  */
4437c478bd9Sstevel@tonic-gate static void
4447c478bd9Sstevel@tonic-gate printreg(char *s, ushort_t v, char *bits)
4457c478bd9Sstevel@tonic-gate {
4467c478bd9Sstevel@tonic-gate 	int i, any = 0;
4477c478bd9Sstevel@tonic-gate 	char c;
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	if (bits && *bits == 8)
4507c478bd9Sstevel@tonic-gate 		(void) printf("%s = %o", s, v);
4517c478bd9Sstevel@tonic-gate 	else
4527c478bd9Sstevel@tonic-gate 		(void) printf("%s = %x", s, v);
4537c478bd9Sstevel@tonic-gate 	bits++;
4547c478bd9Sstevel@tonic-gate 	if (v && bits) {
4557c478bd9Sstevel@tonic-gate 		(void) putchar('<');
4567c478bd9Sstevel@tonic-gate 		while ((i = *bits++) != 0) {
4577c478bd9Sstevel@tonic-gate 			if (v & (1 << (i-1))) {
4587c478bd9Sstevel@tonic-gate 				if (any)
4597c478bd9Sstevel@tonic-gate 					(void) putchar(',');
4607c478bd9Sstevel@tonic-gate 				any = 1;
4617c478bd9Sstevel@tonic-gate 				for (; (c = *bits) > 32; bits++)
4627c478bd9Sstevel@tonic-gate 					(void) putchar(c);
4637c478bd9Sstevel@tonic-gate 			} else
4647c478bd9Sstevel@tonic-gate 				for (; *bits > 32; bits++)
4657c478bd9Sstevel@tonic-gate 					;
4667c478bd9Sstevel@tonic-gate 		}
4677c478bd9Sstevel@tonic-gate 		(void) putchar('>');
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate }
470