xref: /titanic_51/usr/src/cmd/mt/mt.c (revision f218e94ba20e9f27fa304f7daae68e9129427398)
17c478bd9Sstevel@tonic-gate /*
2c6914c10Srralphs  * 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 
46c6914c10Srralphs 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 *);
49c6914c10Srralphs 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 
54c6914c10Srralphs 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 },
79c6914c10Srralphs 	{ "fssf",		MTFSSF,			O_RDONLY,	1 },
80c6914c10Srralphs 	{ "bssf",		MTBSSF,			O_RDONLY,	1 },
81c6914c10Srralphs 	{ "tell",		MTTELL,			O_RDONLY,	0 },
82c6914c10Srralphs 	{ "seek",		MTSEEK,			O_RDONLY,	1 },
83c6914c10Srralphs 	{ "load",		MTLOAD,			O_UNLOAD,	0 },
84c6914c10Srralphs 	{ "lock",		MTLOCK,			O_RDONLY,	0 },
85c6914c10Srralphs 	{ "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;
94c6914c10Srralphs 	char *tape;
95c6914c10Srralphs 	int mtfd;
96c6914c10Srralphs 	struct commands const *comp;
97c6914c10Srralphs 	struct mtget mt_status;
98c6914c10Srralphs 	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;
104c6914c10Srralphs 	} else {
105c6914c10Srralphs 		tape = getenv("TAPE");
106c6914c10Srralphs 		if (tape == NULL) {
1077c478bd9Sstevel@tonic-gate 			tape = DEFAULT_NRW_TAPE;
108c6914c10Srralphs 		}
109c6914c10Srralphs 	}
110c6914c10Srralphs 
1117c478bd9Sstevel@tonic-gate 	if (argc < 2) {
1127c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1137c478bd9Sstevel@tonic-gate 		    "usage: mt [ -f device ] command [ count ]\n");
114c6914c10Srralphs 		return (1);
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 
117c6914c10Srralphs 	cp = argv[1];
118c6914c10Srralphs 	for (comp = com; comp->c_name != NULL; comp++) {
119c6914c10Srralphs 		if (strncmp(cp, comp->c_name, strlen(cp)) == 0) {
120c6914c10Srralphs 			break;
121c6914c10Srralphs 		}
122c6914c10Srralphs 	}
123c6914c10Srralphs 
124c6914c10Srralphs 	if (comp->c_name == NULL) {
125c6914c10Srralphs 		(void) fprintf(stderr, "mt: unknown command: %s\n", cp);
126c6914c10Srralphs 		return (1);
127c6914c10Srralphs 	}
128c6914c10Srralphs 
129c6914c10Srralphs 	mtfd = open(tape, comp->c_oflag);
130c6914c10Srralphs 	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,
138c6914c10Srralphs 			    "%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 		}
145c6914c10Srralphs 		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");
157c6914c10Srralphs 			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;
167*f218e94bSrralphs 		daddr_t mt_fileno;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 		usecnt = argc > 2 && comp->c_usecnt;
170c6914c10Srralphs 		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");
173c6914c10Srralphs 			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");
178c6914c10Srralphs 			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.
184c6914c10Srralphs 		 *
185c6914c10Srralphs 		 * If file number is -1 tape position is unknown!
1867c478bd9Sstevel@tonic-gate 		 */
187c6914c10Srralphs 		if ((mt_status.mt_flags & MTF_ASF) == 0 ||
188c6914c10Srralphs 		    (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;
192c6914c10Srralphs 			if (ioctl(mtfd, MTIOCLTOP, &mt_com) < 0) {
193*f218e94bSrralphs 				(void) fprintf(stderr, "%s %s %ld ",
1947c478bd9Sstevel@tonic-gate 				    tape, comp->c_name, mt_fileno);
1957c478bd9Sstevel@tonic-gate 				perror("mt");
196c6914c10Srralphs 				return (2);
1977c478bd9Sstevel@tonic-gate 			}
198c6914c10Srralphs 			/* Needed to rewind which worked now correct fileno */
199c6914c10Srralphs 			mt_status.mt_fileno = 0;
200c6914c10Srralphs 			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 		}
211c6914c10Srralphs 		if (ioctl(mtfd, MTIOCLTOP, &mt_com) < 0) {
212*f218e94bSrralphs 			(void) fprintf(stderr, "%s %s %ld ", tape, comp->c_name,
2137c478bd9Sstevel@tonic-gate 			    mt_fileno);
2147c478bd9Sstevel@tonic-gate 			perror("failed");
215c6914c10Srralphs 			return (2);
2167c478bd9Sstevel@tonic-gate 		}
2177c478bd9Sstevel@tonic-gate 	} else if (comp->c_code == MTIOCGETDRIVETYPE) {
218c6914c10Srralphs 		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;
226c6914c10Srralphs 		mt_com.mt_count = (usecnt ? atoll(argv[2]) : 1);
2277c478bd9Sstevel@tonic-gate 		if (mt_com.mt_count < 0) {
228c6914c10Srralphs 			(void) fprintf(stderr, "mt: negative %s count\n",
229c6914c10Srralphs 			    comp->c_name);
230c6914c10Srralphs 			return (1);
2317c478bd9Sstevel@tonic-gate 		}
232c6914c10Srralphs 		if (ioctl(mtfd, MTIOCLTOP, &mt_com) < 0) {
233c6914c10Srralphs 			/*
234c6914c10Srralphs 			 * If we asked for a seek and it returns a tell
235c6914c10Srralphs 			 * we attempted to seek more then there was.
236c6914c10Srralphs 			 */
237c6914c10Srralphs 			if (mt_com.mt_op == MTTELL &&
238c6914c10Srralphs 			    comp->c_code == MTSEEK) {
239c6914c10Srralphs 				(void) printf("partial seek:at block = %llu.\n",
2407c478bd9Sstevel@tonic-gate 				    mt_com.mt_count);
241c6914c10Srralphs 			} else {
242c6914c10Srralphs 				(void) fprintf(stderr, "%s %s %lld ", tape,
243c6914c10Srralphs 				    comp->c_name, mt_com.mt_count);
2447c478bd9Sstevel@tonic-gate 				perror("failed");
2457c478bd9Sstevel@tonic-gate 			}
246c6914c10Srralphs 			return (2);
247c6914c10Srralphs 		}
248c6914c10Srralphs 		if (mt_com.mt_op == MTTELL) {
249c6914c10Srralphs 			(void) printf("At block = %llu.\n", mt_com.mt_count);
250c6914c10Srralphs 		}
251c6914c10Srralphs 
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");
257c6914c10Srralphs 			return (2);
2587c478bd9Sstevel@tonic-gate 		}
259c6914c10Srralphs 		return (status(mtfd, &mt_status));
2607c478bd9Sstevel@tonic-gate 	}
2617c478bd9Sstevel@tonic-gate 	return (0);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
264c6914c10Srralphs static int
265c6914c10Srralphs 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");
279c6914c10Srralphs 		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);
340c6914c10Srralphs 
341c6914c10Srralphs 	return (0);
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate /*
3457c478bd9Sstevel@tonic-gate  * Interpret the status buffer returned
3467c478bd9Sstevel@tonic-gate  */
347c6914c10Srralphs static int
348c6914c10Srralphs status(int mtfd, struct mtget *bp)
3497c478bd9Sstevel@tonic-gate {
3507c478bd9Sstevel@tonic-gate 	struct mtdrivetype mdt;
3517c478bd9Sstevel@tonic-gate 	struct mtdrivetype_request mdt_req;
352c6914c10Srralphs 	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 {
367c6914c10Srralphs 		perror("mt drivetype");
368c6914c10Srralphs 		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);
383*f218e94bSrralphs 		/*
384*f218e94bSrralphs 		 * Can overflow the signed numbers.
385*f218e94bSrralphs 		 * fileno will be -1 on error but all other positions are
386*f218e94bSrralphs 		 * positive. blkno will never be negative.
387*f218e94bSrralphs 		 */
388*f218e94bSrralphs 		if (bp->mt_fileno == -1) {
389*f218e94bSrralphs 			(void) printf("   file no= -1   block no= %lu\n",
390*f218e94bSrralphs 			    (unsigned long)bp->mt_blkno);
391*f218e94bSrralphs 		} else {
392*f218e94bSrralphs 			(void) printf("   file no= %lu   block no= %lu\n",
393*f218e94bSrralphs 			    (unsigned long)bp->mt_fileno,
394*f218e94bSrralphs 			    (unsigned long)bp->mt_blkno);
395*f218e94bSrralphs 		}
3965988135dSrralphs 		if ((bp->mt_flags & MTF_WORM_MEDIA) != 0) {
3975988135dSrralphs 			(void) printf("   WORM media\n");
3985988135dSrralphs 		}
3997c478bd9Sstevel@tonic-gate 	} else {
4007c478bd9Sstevel@tonic-gate 		/* Handle non-SCSI drives here. */
401c6914c10Srralphs 		if (name == NULL) {
4027c478bd9Sstevel@tonic-gate 			(void) printf("unknown tape drive type (0x%x)\n",
403c6914c10Srralphs 			    mdt.type);
404c6914c10Srralphs 			return (2);
4057c478bd9Sstevel@tonic-gate 		}
406c6914c10Srralphs 		(void) printf("%s tape drive:\n   residual= %ld", name,
4077c478bd9Sstevel@tonic-gate 		    bp->mt_resid);
408c6914c10Srralphs 		printreg("   ds", (ushort_t)bp->mt_dsreg, 0);
409c6914c10Srralphs 		printreg("   er", (ushort_t)bp->mt_erreg, 0);
4107c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
4117c478bd9Sstevel@tonic-gate 	}
412c6914c10Srralphs 	return (0);
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate /*
4177c478bd9Sstevel@tonic-gate  * Define SCSI sense key error messages.
4187c478bd9Sstevel@tonic-gate  *
4197c478bd9Sstevel@tonic-gate  * The first 16 sense keys are SCSI standard
4207c478bd9Sstevel@tonic-gate  * sense keys. The keys after this are
4217c478bd9Sstevel@tonic-gate  * Sun Specifice 'sense' keys- e.g., crap.
4227c478bd9Sstevel@tonic-gate  */
4237c478bd9Sstevel@tonic-gate 
424c6914c10Srralphs static char *sense_keys[] = {
4257c478bd9Sstevel@tonic-gate 	"No Additional Sense",		/* 0x00 */
4267c478bd9Sstevel@tonic-gate 	"Soft Error",			/* 0x01 */
4277c478bd9Sstevel@tonic-gate 	"Not Ready",			/* 0x02 */
4287c478bd9Sstevel@tonic-gate 	"Media Error",			/* 0x03 */
4297c478bd9Sstevel@tonic-gate 	"Hardware Error",		/* 0x04 */
4307c478bd9Sstevel@tonic-gate 	"Illegal Request",		/* 0x05 */
4317c478bd9Sstevel@tonic-gate 	"Unit Attention",		/* 0x06 */
4327c478bd9Sstevel@tonic-gate 	"Write Protected",		/* 0x07 */
4337c478bd9Sstevel@tonic-gate 	"Blank Check",			/* 0x08 */
4347c478bd9Sstevel@tonic-gate 	"Vendor Unique",		/* 0x09 */
4357c478bd9Sstevel@tonic-gate 	"Copy Aborted",			/* 0x0a */
4367c478bd9Sstevel@tonic-gate 	"Aborted Command",		/* 0x0b */
4377c478bd9Sstevel@tonic-gate 	"Equal Error",			/* 0x0c */
4387c478bd9Sstevel@tonic-gate 	"Volume Overflow",		/* 0x0d */
4397c478bd9Sstevel@tonic-gate 	"Miscompare Error",		/* 0x0e */
440c6914c10Srralphs 	"Reserved",			/* 0x0f */
441c6914c10Srralphs #ifdef	sun
4427c478bd9Sstevel@tonic-gate 	"fatal",			/* 0x10 */
4437c478bd9Sstevel@tonic-gate 	"timeout",			/* 0x11 */
4447c478bd9Sstevel@tonic-gate 	"EOF",				/* 0x12 */
4457c478bd9Sstevel@tonic-gate 	"EOT",				/* 0x13 */
4467c478bd9Sstevel@tonic-gate 	"length error",			/* 0x14 */
4477c478bd9Sstevel@tonic-gate 	"BOT",				/* 0x15 */
4487c478bd9Sstevel@tonic-gate 	"wrong tape media",		/* 0x16 */
449c6914c10Srralphs #endif
4507c478bd9Sstevel@tonic-gate };
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate /*
4537c478bd9Sstevel@tonic-gate  * Return the text string associated with the sense key value.
4547c478bd9Sstevel@tonic-gate  */
4557c478bd9Sstevel@tonic-gate static char *
4567c478bd9Sstevel@tonic-gate print_key(short key_code)
4577c478bd9Sstevel@tonic-gate {
4587c478bd9Sstevel@tonic-gate 	static char unknown[32];
459c6914c10Srralphs 
460c6914c10Srralphs 	if ((key_code >= 0) &&
461c6914c10Srralphs 	    (key_code < (sizeof (sense_keys) / sizeof (sense_keys[0])))) {
462c6914c10Srralphs 		return (sense_keys[key_code]);
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	(void) sprintf(unknown, "unknown sense key: 0x%x",
4667c478bd9Sstevel@tonic-gate 	    (unsigned int) key_code);
4677c478bd9Sstevel@tonic-gate 	return (unknown);
4687c478bd9Sstevel@tonic-gate }
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate /*
4727c478bd9Sstevel@tonic-gate  * Print a register a la the %b format of the kernel's printf
4737c478bd9Sstevel@tonic-gate  */
4747c478bd9Sstevel@tonic-gate static void
4757c478bd9Sstevel@tonic-gate printreg(char *s, ushort_t v, char *bits)
4767c478bd9Sstevel@tonic-gate {
4777c478bd9Sstevel@tonic-gate 	int i, any = 0;
4787c478bd9Sstevel@tonic-gate 	char c;
4797c478bd9Sstevel@tonic-gate 
480c6914c10Srralphs 	if (bits && *bits == 8) {
4817c478bd9Sstevel@tonic-gate 		(void) printf("%s = %o", s, v);
482c6914c10Srralphs 	} else {
4837c478bd9Sstevel@tonic-gate 		(void) printf("%s = %x", s, v);
484c6914c10Srralphs 	}
4857c478bd9Sstevel@tonic-gate 	bits++;
4867c478bd9Sstevel@tonic-gate 	if (v && bits) {
4877c478bd9Sstevel@tonic-gate 		(void) putchar('<');
4887c478bd9Sstevel@tonic-gate 		while ((i = *bits++) != 0) {
4897c478bd9Sstevel@tonic-gate 			if (v & (1 << (i-1))) {
490c6914c10Srralphs 				if (any) {
4917c478bd9Sstevel@tonic-gate 					(void) putchar(',');
492c6914c10Srralphs 				}
4937c478bd9Sstevel@tonic-gate 				any = 1;
494c6914c10Srralphs 				for (; (c = *bits) > 32; bits++) {
4957c478bd9Sstevel@tonic-gate 					(void) putchar(c);
496c6914c10Srralphs 				}
497c6914c10Srralphs 			} else {
4987c478bd9Sstevel@tonic-gate 				for (; *bits > 32; bits++)
4997c478bd9Sstevel@tonic-gate 					;
5007c478bd9Sstevel@tonic-gate 			}
501c6914c10Srralphs 		}
5027c478bd9Sstevel@tonic-gate 		(void) putchar('>');
5037c478bd9Sstevel@tonic-gate 	}
5047c478bd9Sstevel@tonic-gate }
505