xref: /titanic_51/usr/src/cmd/mt/mt.c (revision c6914c10632dbf46820663057266b1f3a8b26276)
17c478bd9Sstevel@tonic-gate /*
2*c6914c10Srralphs  * Copyright 2007 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 
46*c6914c10Srralphs static int print_config(int mtfd);
477c478bd9Sstevel@tonic-gate static char *print_key(short key_code);
487c478bd9Sstevel@tonic-gate static void printreg(char *, ushort_t, char *);
49*c6914c10Srralphs static int status(int mtfd, struct mtget *);
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /* Pseudo flag for open even if drive is not ready (Unloaded) or reserved */
527c478bd9Sstevel@tonic-gate #define	O_UNLOAD (O_RDWR | O_NDELAY)
537c478bd9Sstevel@tonic-gate 
54*c6914c10Srralphs static const struct commands {
557c478bd9Sstevel@tonic-gate 	char *c_name;
567c478bd9Sstevel@tonic-gate 	int c_code;
577c478bd9Sstevel@tonic-gate 	int c_oflag;
587c478bd9Sstevel@tonic-gate 	int c_usecnt;
597c478bd9Sstevel@tonic-gate } com[] = {
607c478bd9Sstevel@tonic-gate 	{ "weof",		MTWEOF,			O_RDWR,		1 },
617c478bd9Sstevel@tonic-gate 	{ "eof",		MTWEOF,			O_RDWR,		1 },
627c478bd9Sstevel@tonic-gate 	{ "fsf",		MTFSF,			O_RDONLY,	1 },
637c478bd9Sstevel@tonic-gate 	{ "bsf",		MTBSF,			O_RDONLY,	1 },
647c478bd9Sstevel@tonic-gate 	{ "asf",		MTASF,			O_RDONLY,	1 },
657c478bd9Sstevel@tonic-gate 	{ "fsr",		MTFSR,			O_RDONLY,	1 },
667c478bd9Sstevel@tonic-gate 	{ "bsr",		MTBSR,			O_RDONLY,	1 },
677c478bd9Sstevel@tonic-gate 	{ "rewind",		MTREW,			O_RDONLY,	0 },
687c478bd9Sstevel@tonic-gate 	{ "offline",		MTOFFL,			O_RDONLY,	0 },
697c478bd9Sstevel@tonic-gate 	{ "rewoffl",		MTOFFL,			O_RDONLY,	0 },
707c478bd9Sstevel@tonic-gate 	{ "status",		MTNOP,			O_RDONLY,	0 },
717c478bd9Sstevel@tonic-gate 	{ "retension",		MTRETEN,		O_RDONLY,	0 },
727c478bd9Sstevel@tonic-gate 	{ "erase",		MTERASE,		O_RDWR,		0 },
737c478bd9Sstevel@tonic-gate 	{ "eom",		MTEOM,			O_RDONLY,	0 },
747c478bd9Sstevel@tonic-gate 	{ "nbsf",		MTNBSF,			O_RDONLY,	1 },
757c478bd9Sstevel@tonic-gate 	{ "reserve",		MTIOCRESERVE,		O_RDONLY,	0 },
767c478bd9Sstevel@tonic-gate 	{ "release",		MTIOCRELEASE,		O_RDONLY,	0 },
777c478bd9Sstevel@tonic-gate 	{ "forcereserve",	MTIOCFORCERESERVE,	O_UNLOAD,	0 },
787c478bd9Sstevel@tonic-gate 	{ "config",		MTIOCGETDRIVETYPE,	O_UNLOAD,	0 },
79*c6914c10Srralphs 	{ "fssf",		MTFSSF,			O_RDONLY,	1 },
80*c6914c10Srralphs 	{ "bssf",		MTBSSF,			O_RDONLY,	1 },
81*c6914c10Srralphs 	{ "tell",		MTTELL,			O_RDONLY,	0 },
82*c6914c10Srralphs 	{ "seek",		MTSEEK,			O_RDONLY,	1 },
83*c6914c10Srralphs 	{ "load",		MTLOAD,			O_UNLOAD,	0 },
84*c6914c10Srralphs 	{ "lock",		MTLOCK,			O_RDONLY,	0 },
85*c6914c10Srralphs 	{ "unlock",		MTUNLOCK,		O_RDONLY,	0 },
867c478bd9Sstevel@tonic-gate 	{ 0 }
877c478bd9Sstevel@tonic-gate };
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 
908d489c7aSmuffin int
917c478bd9Sstevel@tonic-gate main(int argc, char **argv)
927c478bd9Sstevel@tonic-gate {
938d489c7aSmuffin 	char *cp;
94*c6914c10Srralphs 	char *tape;
95*c6914c10Srralphs 	int mtfd;
96*c6914c10Srralphs 	struct commands const *comp;
97*c6914c10Srralphs 	struct mtget mt_status;
98*c6914c10Srralphs 	struct mtlop mt_com;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	if (argc > 2 && (equal(argv[1], "-t") || equal(argv[1], "-f"))) {
1017c478bd9Sstevel@tonic-gate 		argc -= 2;
1027c478bd9Sstevel@tonic-gate 		tape = argv[2];
1037c478bd9Sstevel@tonic-gate 		argv += 2;
104*c6914c10Srralphs 	} else {
105*c6914c10Srralphs 		tape = getenv("TAPE");
106*c6914c10Srralphs 		if (tape == NULL) {
1077c478bd9Sstevel@tonic-gate 			tape = DEFAULT_NRW_TAPE;
108*c6914c10Srralphs 		}
109*c6914c10Srralphs 	}
110*c6914c10Srralphs 
1117c478bd9Sstevel@tonic-gate 	if (argc < 2) {
1127c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1137c478bd9Sstevel@tonic-gate 		    "usage: mt [ -f device ] command [ count ]\n");
114*c6914c10Srralphs 		return (1);
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 
117*c6914c10Srralphs 	cp = argv[1];
118*c6914c10Srralphs 	for (comp = com; comp->c_name != NULL; comp++) {
119*c6914c10Srralphs 		if (strncmp(cp, comp->c_name, strlen(cp)) == 0) {
120*c6914c10Srralphs 			break;
121*c6914c10Srralphs 		}
122*c6914c10Srralphs 	}
123*c6914c10Srralphs 
124*c6914c10Srralphs 	if (comp->c_name == NULL) {
125*c6914c10Srralphs 		(void) fprintf(stderr, "mt: unknown command: %s\n", cp);
126*c6914c10Srralphs 		return (1);
127*c6914c10Srralphs 	}
128*c6914c10Srralphs 
129*c6914c10Srralphs 	mtfd = open(tape, comp->c_oflag);
130*c6914c10Srralphs 	if (mtfd < 0) {
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 		/*
1337c478bd9Sstevel@tonic-gate 		 * Provide additional error message decoding since
1347c478bd9Sstevel@tonic-gate 		 * we need additional error codes to fix them problem.
1357c478bd9Sstevel@tonic-gate 		 */
1367c478bd9Sstevel@tonic-gate 		if (errno == EIO) {
1377c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
138*c6914c10Srralphs 			    "%s: no tape loaded or drive offline\n", tape);
1397c478bd9Sstevel@tonic-gate 		} else if (errno == EACCES) {
1407c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
1417c478bd9Sstevel@tonic-gate 			    "%s: write protected or reserved.\n", tape);
1427c478bd9Sstevel@tonic-gate 		} else {
1437c478bd9Sstevel@tonic-gate 			perror(tape);
1447c478bd9Sstevel@tonic-gate 		}
145*c6914c10Srralphs 		return (1);
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	if (comp->c_code == MTIOCFORCERESERVE ||
1497c478bd9Sstevel@tonic-gate 	    comp->c_code == MTIOCRESERVE ||
1507c478bd9Sstevel@tonic-gate 	    comp->c_code == MTIOCRELEASE)  {
1517c478bd9Sstevel@tonic-gate 		/*
1527c478bd9Sstevel@tonic-gate 		 * Handle all MTIOC ioctls used in
1537c478bd9Sstevel@tonic-gate 		 * reservation/release/takeownership.
1547c478bd9Sstevel@tonic-gate 		 */
1557c478bd9Sstevel@tonic-gate 		if (ioctl(mtfd, comp->c_code) < 0) {
1567c478bd9Sstevel@tonic-gate 			perror("mt");
157*c6914c10Srralphs 			return (2);
1587c478bd9Sstevel@tonic-gate 		}
1597c478bd9Sstevel@tonic-gate 	} else if (comp->c_code == MTASF) {
1607c478bd9Sstevel@tonic-gate 		/*
1617c478bd9Sstevel@tonic-gate 		 * Handle absolute file positioning.  Ask tape driver
1627c478bd9Sstevel@tonic-gate 		 * where tape is and then skip to desired file.  If
1637c478bd9Sstevel@tonic-gate 		 * driver doesn't support get location ioctl, rewind
1647c478bd9Sstevel@tonic-gate 		 * the tape and then space to the desired file.
1657c478bd9Sstevel@tonic-gate 		 */
1667c478bd9Sstevel@tonic-gate 		int usecnt;
1677c478bd9Sstevel@tonic-gate 		int mt_fileno;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 		usecnt = argc > 2 && comp->c_usecnt;
170*c6914c10Srralphs 		mt_fileno = usecnt ? atol(argv[2]) : 1;
1717c478bd9Sstevel@tonic-gate 		if (mt_fileno < 0) {
1727c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "mt: negative file number\n");
173*c6914c10Srralphs 			return (1);
1747c478bd9Sstevel@tonic-gate 		}
1757c478bd9Sstevel@tonic-gate 		(void) ioctl(mtfd, MTIOCGET, (char *)&mt_status);
1767c478bd9Sstevel@tonic-gate 		if (ioctl(mtfd, MTIOCGET, (char *)&mt_status) < 0) {
1777c478bd9Sstevel@tonic-gate 			perror("mt");
178*c6914c10Srralphs 			return (2);
1797c478bd9Sstevel@tonic-gate 		}
1807c478bd9Sstevel@tonic-gate 		/*
1817c478bd9Sstevel@tonic-gate 		 * Check if device supports reporting current file
1827c478bd9Sstevel@tonic-gate 		 * tape file position.  If not, rewind the tape, and
1837c478bd9Sstevel@tonic-gate 		 * space forward.
184*c6914c10Srralphs 		 *
185*c6914c10Srralphs 		 * If file number is -1 tape position is unknown!
1867c478bd9Sstevel@tonic-gate 		 */
187*c6914c10Srralphs 		if ((mt_status.mt_flags & MTF_ASF) == 0 ||
188*c6914c10Srralphs 		    (mt_status.mt_fileno == -1)) {
1897c478bd9Sstevel@tonic-gate 			/* printf("mt: rewind\n"); */
1907c478bd9Sstevel@tonic-gate 			mt_com.mt_count = 1;
1917c478bd9Sstevel@tonic-gate 			mt_com.mt_op = MTREW;
192*c6914c10Srralphs 			if (ioctl(mtfd, MTIOCLTOP, &mt_com) < 0) {
1937c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "%s %s %d ",
1947c478bd9Sstevel@tonic-gate 				    tape, comp->c_name, mt_fileno);
1957c478bd9Sstevel@tonic-gate 				perror("mt");
196*c6914c10Srralphs 				return (2);
1977c478bd9Sstevel@tonic-gate 			}
198*c6914c10Srralphs 			/* Needed to rewind which worked now correct fileno */
199*c6914c10Srralphs 			mt_status.mt_fileno = 0;
200*c6914c10Srralphs 			mt_status.mt_blkno = 0;
2017c478bd9Sstevel@tonic-gate 		}
2027c478bd9Sstevel@tonic-gate 		if (mt_fileno < mt_status.mt_fileno) {
2037c478bd9Sstevel@tonic-gate 			mt_com.mt_op = MTNBSF;
2047c478bd9Sstevel@tonic-gate 			mt_com.mt_count = mt_status.mt_fileno - mt_fileno;
2057c478bd9Sstevel@tonic-gate 			/* printf("mt: bsf= %d\n", mt_com.mt_count); */
2067c478bd9Sstevel@tonic-gate 		} else {
2077c478bd9Sstevel@tonic-gate 			mt_com.mt_op = MTFSF;
2087c478bd9Sstevel@tonic-gate 			mt_com.mt_count =  mt_fileno - mt_status.mt_fileno;
2097c478bd9Sstevel@tonic-gate 			/* printf("mt: fsf= %d\n", mt_com.mt_count); */
2107c478bd9Sstevel@tonic-gate 		}
211*c6914c10Srralphs 		if (ioctl(mtfd, MTIOCLTOP, &mt_com) < 0) {
2127c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "%s %s %d ", tape, comp->c_name,
2137c478bd9Sstevel@tonic-gate 			    mt_fileno);
2147c478bd9Sstevel@tonic-gate 			perror("failed");
215*c6914c10Srralphs 			return (2);
2167c478bd9Sstevel@tonic-gate 		}
2177c478bd9Sstevel@tonic-gate 	} else if (comp->c_code == MTIOCGETDRIVETYPE) {
218*c6914c10Srralphs 		return (print_config(mtfd));
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	/* Handle regular mag tape ioctls */
2217c478bd9Sstevel@tonic-gate 	} else if (comp->c_code != MTNOP) {
2227c478bd9Sstevel@tonic-gate 		int usecnt;
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 		mt_com.mt_op = comp->c_code;
2257c478bd9Sstevel@tonic-gate 		usecnt = argc > 2 && comp->c_usecnt;
226*c6914c10Srralphs 		mt_com.mt_count = (usecnt ? atoll(argv[2]) : 1);
2277c478bd9Sstevel@tonic-gate 		if (mt_com.mt_count < 0) {
228*c6914c10Srralphs 			(void) fprintf(stderr, "mt: negative %s count\n",
229*c6914c10Srralphs 			    comp->c_name);
230*c6914c10Srralphs 			return (1);
2317c478bd9Sstevel@tonic-gate 		}
232*c6914c10Srralphs 		if (ioctl(mtfd, MTIOCLTOP, &mt_com) < 0) {
233*c6914c10Srralphs 			/*
234*c6914c10Srralphs 			 * If we asked for a seek and it returns a tell
235*c6914c10Srralphs 			 * we attempted to seek more then there was.
236*c6914c10Srralphs 			 */
237*c6914c10Srralphs 			if (mt_com.mt_op == MTTELL &&
238*c6914c10Srralphs 			    comp->c_code == MTSEEK) {
239*c6914c10Srralphs 				(void) printf("partial seek:at block = %llu.\n",
2407c478bd9Sstevel@tonic-gate 				    mt_com.mt_count);
241*c6914c10Srralphs 			} else {
242*c6914c10Srralphs 				(void) fprintf(stderr, "%s %s %lld ", tape,
243*c6914c10Srralphs 				    comp->c_name, mt_com.mt_count);
2447c478bd9Sstevel@tonic-gate 				perror("failed");
2457c478bd9Sstevel@tonic-gate 			}
246*c6914c10Srralphs 			return (2);
247*c6914c10Srralphs 		}
248*c6914c10Srralphs 		if (mt_com.mt_op == MTTELL) {
249*c6914c10Srralphs 			(void) printf("At block = %llu.\n", mt_com.mt_count);
250*c6914c10Srralphs 		}
251*c6914c10Srralphs 
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	/* Handle status ioctl */
2547c478bd9Sstevel@tonic-gate 	} else {
2557c478bd9Sstevel@tonic-gate 		if (ioctl(mtfd, MTIOCGET, (char *)&mt_status) < 0) {
2567c478bd9Sstevel@tonic-gate 			perror("mt");
257*c6914c10Srralphs 			return (2);
2587c478bd9Sstevel@tonic-gate 		}
259*c6914c10Srralphs 		return (status(mtfd, &mt_status));
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 	return (0);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
264*c6914c10Srralphs static int
265*c6914c10Srralphs print_config(int mtfd)
2667c478bd9Sstevel@tonic-gate {
2677c478bd9Sstevel@tonic-gate 	struct mtdrivetype mdt;
2687c478bd9Sstevel@tonic-gate 	struct mtdrivetype_request mdt_req;
2697c478bd9Sstevel@tonic-gate 	char cfgname[48];
2707c478bd9Sstevel@tonic-gate 	char tmp[2];
2717c478bd9Sstevel@tonic-gate 	char *name;
2727c478bd9Sstevel@tonic-gate 	int i;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	mdt_req.size = sizeof (mdt);
2757c478bd9Sstevel@tonic-gate 	mdt_req.mtdtp = &mdt;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	if (ioctl(mtfd, MTIOCGETDRIVETYPE, &mdt_req) != 0) {
2787c478bd9Sstevel@tonic-gate 		perror("mt config");
279*c6914c10Srralphs 		return (2);
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	/*
2837c478bd9Sstevel@tonic-gate 	 * remove trailing spaces from product id.
2847c478bd9Sstevel@tonic-gate 	 */
2857c478bd9Sstevel@tonic-gate 	for (i = VIDPIDLEN; i; i--) {
2867c478bd9Sstevel@tonic-gate 		if (isspace(mdt.vid[i]) || mdt.vid[i] == '*') {
2877c478bd9Sstevel@tonic-gate 			mdt.vid[i] = 0;
2887c478bd9Sstevel@tonic-gate 		} else if (mdt.vid[i] == 0) {
2897c478bd9Sstevel@tonic-gate 			continue;
2907c478bd9Sstevel@tonic-gate 		} else {
2917c478bd9Sstevel@tonic-gate 			break;
2927c478bd9Sstevel@tonic-gate 		}
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate 	/*
2967c478bd9Sstevel@tonic-gate 	 * If this is a generic name display the Vid and Pid instead.
2977c478bd9Sstevel@tonic-gate 	 */
2987c478bd9Sstevel@tonic-gate 	if (strstr(mdt.name, "Vendor '") == NULL) {
2997c478bd9Sstevel@tonic-gate 		name = mdt.name;
3007c478bd9Sstevel@tonic-gate 	} else {
3017c478bd9Sstevel@tonic-gate 		name = mdt.vid;
3027c478bd9Sstevel@tonic-gate 	}
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	/*
3057c478bd9Sstevel@tonic-gate 	 * Attempt to create a configuration name using vid and pid.
3067c478bd9Sstevel@tonic-gate 	 */
3077c478bd9Sstevel@tonic-gate 	(void) strcpy(cfgname, "CFG");
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	for (tmp[1] = i = 0; i < VIDPIDLEN; i++) {
3107c478bd9Sstevel@tonic-gate 		if (!isalnum(name[i]))
3117c478bd9Sstevel@tonic-gate 			continue;
3127c478bd9Sstevel@tonic-gate 		if (isspace(name[i]))
3137c478bd9Sstevel@tonic-gate 			continue;
3147c478bd9Sstevel@tonic-gate 		tmp[0] = toupper(name[i]);
3157c478bd9Sstevel@tonic-gate 		(void) strncat(cfgname, tmp, 1);
3167c478bd9Sstevel@tonic-gate 	}
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 	(void) printf("\"%s\", \"%s\", \"%s\";\n", mdt.vid, name, cfgname);
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	/*
3217c478bd9Sstevel@tonic-gate 	 * Don't want show some bits, ST_DYNAMIC is set in the driver
3227c478bd9Sstevel@tonic-gate 	 * so one can tell that its not a compiled in config.
3237c478bd9Sstevel@tonic-gate 	 * The ST_LONG_ERASE and ST_LONG_TIMEOUTS are not displayed
3247c478bd9Sstevel@tonic-gate 	 * becouse the timeout values below already reflect them being
3257c478bd9Sstevel@tonic-gate 	 * set.
3267c478bd9Sstevel@tonic-gate 	 * Also ST_KNOWS_MEDIA is not displayed as it can not be configured
3277c478bd9Sstevel@tonic-gate 	 * from an st.conf entry.
3287c478bd9Sstevel@tonic-gate 	 */
3297c478bd9Sstevel@tonic-gate 	(void) printf("%s = 2,0x%X,%d,0x%X,", cfgname,
3307c478bd9Sstevel@tonic-gate 	    mdt.type, mdt.bsize, mdt.options &
3317c478bd9Sstevel@tonic-gate 	    ~(ST_DYNAMIC | ST_LONG_ERASE | ST_LONG_TIMEOUTS | ST_KNOWS_MEDIA));
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	(void) printf("4,0x%2.2X,0x%2.2X,0x%2.2X,0x%2.2X,%d,",
3347c478bd9Sstevel@tonic-gate 	    mdt.densities[0], mdt.densities[1], mdt.densities[2],
3357c478bd9Sstevel@tonic-gate 	    mdt.densities[3], mdt.default_density >> 3);
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	(void) printf("%d,%d,%d,%d,%d,%d,%d;\n", mdt.non_motion_timeout,
3387c478bd9Sstevel@tonic-gate 	    mdt.io_timeout, mdt.rewind_timeout, mdt.space_timeout,
3397c478bd9Sstevel@tonic-gate 	    mdt.load_timeout, mdt.unload_timeout, mdt.erase_timeout);
340*c6914c10Srralphs 
341*c6914c10Srralphs 	return (0);
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate /*
3457c478bd9Sstevel@tonic-gate  * Interpret the status buffer returned
3467c478bd9Sstevel@tonic-gate  */
347*c6914c10Srralphs static int
348*c6914c10Srralphs status(int mtfd, struct mtget *bp)
3497c478bd9Sstevel@tonic-gate {
3507c478bd9Sstevel@tonic-gate 	struct mtdrivetype mdt;
3517c478bd9Sstevel@tonic-gate 	struct mtdrivetype_request mdt_req;
352*c6914c10Srralphs 	const char *name = (char *)NULL;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	/*
3557c478bd9Sstevel@tonic-gate 	 * Make a call to MTIOCGETDRIVETYPE ioctl, Also use old method
3567c478bd9Sstevel@tonic-gate 	 * of MT_TAPE_INFO for now, but MT_TAPE_INFO should dissapear in 2.7
3577c478bd9Sstevel@tonic-gate 	 */
3587c478bd9Sstevel@tonic-gate 	mdt_req.size = sizeof (struct mtdrivetype);
3597c478bd9Sstevel@tonic-gate 	mdt_req.mtdtp = &mdt;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	if (ioctl(mtfd, MTIOCGETDRIVETYPE, &mdt_req) == 0) {
3627c478bd9Sstevel@tonic-gate 		name = mdt.name;
3637c478bd9Sstevel@tonic-gate 		if (strstr(mdt.name, "Vendor '") != NULL) {
3647c478bd9Sstevel@tonic-gate 			(void) printf("Unconfigured Drive: ");
3657c478bd9Sstevel@tonic-gate 		}
3667c478bd9Sstevel@tonic-gate 	} else {
367*c6914c10Srralphs 		perror("mt drivetype");
368*c6914c10Srralphs 		return (2);
3697c478bd9Sstevel@tonic-gate 	}
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	/* Handle SCSI tape drives specially. */
3727c478bd9Sstevel@tonic-gate 	if ((bp->mt_flags & MTF_SCSI)) {
3737c478bd9Sstevel@tonic-gate 		if (name == (char *)NULL) {
3747c478bd9Sstevel@tonic-gate 			name = "SCSI";
3757c478bd9Sstevel@tonic-gate 		}
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 		(void) printf("%s tape drive:\n", name);
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 		(void) printf("   sense key(0x%x)= %s   residual= %ld   ",
3817c478bd9Sstevel@tonic-gate 		    bp->mt_erreg, print_key(bp->mt_erreg), bp->mt_resid);
3827c478bd9Sstevel@tonic-gate 		(void) printf("retries= %d\n", bp->mt_dsreg);
3837c478bd9Sstevel@tonic-gate 		(void) printf("   file no= %ld   block no= %ld\n",
3847c478bd9Sstevel@tonic-gate 		    bp->mt_fileno, bp->mt_blkno);
3855988135dSrralphs 		if ((bp->mt_flags & MTF_WORM_MEDIA) != 0) {
3865988135dSrralphs 			(void) printf("   WORM media\n");
3875988135dSrralphs 		}
3887c478bd9Sstevel@tonic-gate 	} else {
3897c478bd9Sstevel@tonic-gate 		/* Handle non-SCSI drives here. */
390*c6914c10Srralphs 		if (name == NULL) {
3917c478bd9Sstevel@tonic-gate 			(void) printf("unknown tape drive type (0x%x)\n",
392*c6914c10Srralphs 			    mdt.type);
393*c6914c10Srralphs 			return (2);
3947c478bd9Sstevel@tonic-gate 		}
395*c6914c10Srralphs 		(void) printf("%s tape drive:\n   residual= %ld", name,
3967c478bd9Sstevel@tonic-gate 		    bp->mt_resid);
397*c6914c10Srralphs 		printreg("   ds", (ushort_t)bp->mt_dsreg, 0);
398*c6914c10Srralphs 		printreg("   er", (ushort_t)bp->mt_erreg, 0);
3997c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
4007c478bd9Sstevel@tonic-gate 	}
401*c6914c10Srralphs 	return (0);
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate /*
4067c478bd9Sstevel@tonic-gate  * Define SCSI sense key error messages.
4077c478bd9Sstevel@tonic-gate  *
4087c478bd9Sstevel@tonic-gate  * The first 16 sense keys are SCSI standard
4097c478bd9Sstevel@tonic-gate  * sense keys. The keys after this are
4107c478bd9Sstevel@tonic-gate  * Sun Specifice 'sense' keys- e.g., crap.
4117c478bd9Sstevel@tonic-gate  */
4127c478bd9Sstevel@tonic-gate 
413*c6914c10Srralphs static char *sense_keys[] = {
4147c478bd9Sstevel@tonic-gate 	"No Additional Sense",		/* 0x00 */
4157c478bd9Sstevel@tonic-gate 	"Soft Error",			/* 0x01 */
4167c478bd9Sstevel@tonic-gate 	"Not Ready",			/* 0x02 */
4177c478bd9Sstevel@tonic-gate 	"Media Error",			/* 0x03 */
4187c478bd9Sstevel@tonic-gate 	"Hardware Error",		/* 0x04 */
4197c478bd9Sstevel@tonic-gate 	"Illegal Request",		/* 0x05 */
4207c478bd9Sstevel@tonic-gate 	"Unit Attention",		/* 0x06 */
4217c478bd9Sstevel@tonic-gate 	"Write Protected",		/* 0x07 */
4227c478bd9Sstevel@tonic-gate 	"Blank Check",			/* 0x08 */
4237c478bd9Sstevel@tonic-gate 	"Vendor Unique",		/* 0x09 */
4247c478bd9Sstevel@tonic-gate 	"Copy Aborted",			/* 0x0a */
4257c478bd9Sstevel@tonic-gate 	"Aborted Command",		/* 0x0b */
4267c478bd9Sstevel@tonic-gate 	"Equal Error",			/* 0x0c */
4277c478bd9Sstevel@tonic-gate 	"Volume Overflow",		/* 0x0d */
4287c478bd9Sstevel@tonic-gate 	"Miscompare Error",		/* 0x0e */
429*c6914c10Srralphs 	"Reserved",			/* 0x0f */
430*c6914c10Srralphs #ifdef	sun
4317c478bd9Sstevel@tonic-gate 	"fatal",			/* 0x10 */
4327c478bd9Sstevel@tonic-gate 	"timeout",			/* 0x11 */
4337c478bd9Sstevel@tonic-gate 	"EOF",				/* 0x12 */
4347c478bd9Sstevel@tonic-gate 	"EOT",				/* 0x13 */
4357c478bd9Sstevel@tonic-gate 	"length error",			/* 0x14 */
4367c478bd9Sstevel@tonic-gate 	"BOT",				/* 0x15 */
4377c478bd9Sstevel@tonic-gate 	"wrong tape media",		/* 0x16 */
438*c6914c10Srralphs #endif
4397c478bd9Sstevel@tonic-gate };
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate /*
4427c478bd9Sstevel@tonic-gate  * Return the text string associated with the sense key value.
4437c478bd9Sstevel@tonic-gate  */
4447c478bd9Sstevel@tonic-gate static char *
4457c478bd9Sstevel@tonic-gate print_key(short key_code)
4467c478bd9Sstevel@tonic-gate {
4477c478bd9Sstevel@tonic-gate 	static char unknown[32];
448*c6914c10Srralphs 
449*c6914c10Srralphs 	if ((key_code >= 0) &&
450*c6914c10Srralphs 	    (key_code < (sizeof (sense_keys) / sizeof (sense_keys[0])))) {
451*c6914c10Srralphs 		return (sense_keys[key_code]);
4527c478bd9Sstevel@tonic-gate 	}
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	(void) sprintf(unknown, "unknown sense key: 0x%x",
4557c478bd9Sstevel@tonic-gate 	    (unsigned int) key_code);
4567c478bd9Sstevel@tonic-gate 	return (unknown);
4577c478bd9Sstevel@tonic-gate }
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate /*
4617c478bd9Sstevel@tonic-gate  * Print a register a la the %b format of the kernel's printf
4627c478bd9Sstevel@tonic-gate  */
4637c478bd9Sstevel@tonic-gate static void
4647c478bd9Sstevel@tonic-gate printreg(char *s, ushort_t v, char *bits)
4657c478bd9Sstevel@tonic-gate {
4667c478bd9Sstevel@tonic-gate 	int i, any = 0;
4677c478bd9Sstevel@tonic-gate 	char c;
4687c478bd9Sstevel@tonic-gate 
469*c6914c10Srralphs 	if (bits && *bits == 8) {
4707c478bd9Sstevel@tonic-gate 		(void) printf("%s = %o", s, v);
471*c6914c10Srralphs 	} else {
4727c478bd9Sstevel@tonic-gate 		(void) printf("%s = %x", s, v);
473*c6914c10Srralphs 	}
4747c478bd9Sstevel@tonic-gate 	bits++;
4757c478bd9Sstevel@tonic-gate 	if (v && bits) {
4767c478bd9Sstevel@tonic-gate 		(void) putchar('<');
4777c478bd9Sstevel@tonic-gate 		while ((i = *bits++) != 0) {
4787c478bd9Sstevel@tonic-gate 			if (v & (1 << (i-1))) {
479*c6914c10Srralphs 				if (any) {
4807c478bd9Sstevel@tonic-gate 					(void) putchar(',');
481*c6914c10Srralphs 				}
4827c478bd9Sstevel@tonic-gate 				any = 1;
483*c6914c10Srralphs 				for (; (c = *bits) > 32; bits++) {
4847c478bd9Sstevel@tonic-gate 					(void) putchar(c);
485*c6914c10Srralphs 				}
486*c6914c10Srralphs 			} else {
4877c478bd9Sstevel@tonic-gate 				for (; *bits > 32; bits++)
4887c478bd9Sstevel@tonic-gate 					;
4897c478bd9Sstevel@tonic-gate 			}
490*c6914c10Srralphs 		}
4917c478bd9Sstevel@tonic-gate 		(void) putchar('>');
4927c478bd9Sstevel@tonic-gate 	}
4937c478bd9Sstevel@tonic-gate }
494