xref: /onnv-gate/usr/src/cmd/mt/mt.c (revision 5628:ddcf8536c1f9)
10Sstevel@tonic-gate /*
24549Srralphs  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
80Sstevel@tonic-gate  * All rights reserved.  The Berkeley Software License Agreement
90Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
100Sstevel@tonic-gate  */
110Sstevel@tonic-gate 
120Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
130Sstevel@tonic-gate 
140Sstevel@tonic-gate /*
150Sstevel@tonic-gate  * mt -- magnetic tape manipulation program
160Sstevel@tonic-gate  */
170Sstevel@tonic-gate #include <stdio.h>
180Sstevel@tonic-gate #include <ctype.h>
190Sstevel@tonic-gate 
200Sstevel@tonic-gate #include <errno.h>
210Sstevel@tonic-gate #include <sys/types.h>
220Sstevel@tonic-gate #include <sys/mtio.h>
230Sstevel@tonic-gate #include <sys/ioctl.h>
240Sstevel@tonic-gate #include <sys/param.h>
250Sstevel@tonic-gate #include <sys/buf.h>
260Sstevel@tonic-gate #include <sys/conf.h>
270Sstevel@tonic-gate #include <sys/file.h>
280Sstevel@tonic-gate #include <sys/uio.h>
290Sstevel@tonic-gate #include <string.h>
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <unistd.h>
320Sstevel@tonic-gate #include <sys/stat.h>
330Sstevel@tonic-gate #include <sys/scsi/targets/stdef.h>
340Sstevel@tonic-gate #include <fcntl.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #define	equal(s1, s2)	(strcmp(s1, s2) == 0)
380Sstevel@tonic-gate #define	MTASF		100	/* absolute file positioning; first file is 0 */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate /*
410Sstevel@tonic-gate  * This can't be DEFTAPE in mtio.h because that is currently the rewinding
420Sstevel@tonic-gate  * unit which makes 'mt fsf' a questionable activity at best.
430Sstevel@tonic-gate  */
440Sstevel@tonic-gate #define	DEFAULT_NRW_TAPE	"/dev/rmt/0n"
450Sstevel@tonic-gate 
464549Srralphs static int print_config(int mtfd);
470Sstevel@tonic-gate static char *print_key(short key_code);
480Sstevel@tonic-gate static void printreg(char *, ushort_t, char *);
494549Srralphs static int status(int mtfd, struct mtget *);
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /* Pseudo flag for open even if drive is not ready (Unloaded) or reserved */
520Sstevel@tonic-gate #define	O_UNLOAD (O_RDWR | O_NDELAY)
530Sstevel@tonic-gate 
544549Srralphs static const struct commands {
550Sstevel@tonic-gate 	char *c_name;
560Sstevel@tonic-gate 	int c_code;
570Sstevel@tonic-gate 	int c_oflag;
580Sstevel@tonic-gate 	int c_usecnt;
590Sstevel@tonic-gate } com[] = {
600Sstevel@tonic-gate 	{ "weof",		MTWEOF,			O_RDWR,		1 },
610Sstevel@tonic-gate 	{ "eof",		MTWEOF,			O_RDWR,		1 },
620Sstevel@tonic-gate 	{ "fsf",		MTFSF,			O_RDONLY,	1 },
630Sstevel@tonic-gate 	{ "bsf",		MTBSF,			O_RDONLY,	1 },
640Sstevel@tonic-gate 	{ "asf",		MTASF,			O_RDONLY,	1 },
650Sstevel@tonic-gate 	{ "fsr",		MTFSR,			O_RDONLY,	1 },
660Sstevel@tonic-gate 	{ "bsr",		MTBSR,			O_RDONLY,	1 },
670Sstevel@tonic-gate 	{ "rewind",		MTREW,			O_RDONLY,	0 },
680Sstevel@tonic-gate 	{ "offline",		MTOFFL,			O_RDONLY,	0 },
690Sstevel@tonic-gate 	{ "rewoffl",		MTOFFL,			O_RDONLY,	0 },
700Sstevel@tonic-gate 	{ "status",		MTNOP,			O_RDONLY,	0 },
710Sstevel@tonic-gate 	{ "retension",		MTRETEN,		O_RDONLY,	0 },
720Sstevel@tonic-gate 	{ "erase",		MTERASE,		O_RDWR,		0 },
730Sstevel@tonic-gate 	{ "eom",		MTEOM,			O_RDONLY,	0 },
740Sstevel@tonic-gate 	{ "nbsf",		MTNBSF,			O_RDONLY,	1 },
750Sstevel@tonic-gate 	{ "reserve",		MTIOCRESERVE,		O_RDONLY,	0 },
760Sstevel@tonic-gate 	{ "release",		MTIOCRELEASE,		O_RDONLY,	0 },
770Sstevel@tonic-gate 	{ "forcereserve",	MTIOCFORCERESERVE,	O_UNLOAD,	0 },
780Sstevel@tonic-gate 	{ "config",		MTIOCGETDRIVETYPE,	O_UNLOAD,	0 },
794549Srralphs 	{ "fssf",		MTFSSF,			O_RDONLY,	1 },
804549Srralphs 	{ "bssf",		MTBSSF,			O_RDONLY,	1 },
814549Srralphs 	{ "tell",		MTTELL,			O_RDONLY,	0 },
824549Srralphs 	{ "seek",		MTSEEK,			O_RDONLY,	1 },
834549Srralphs 	{ "load",		MTLOAD,			O_UNLOAD,	0 },
844549Srralphs 	{ "lock",		MTLOCK,			O_RDONLY,	0 },
854549Srralphs 	{ "unlock",		MTUNLOCK,		O_RDONLY,	0 },
860Sstevel@tonic-gate 	{ 0 }
870Sstevel@tonic-gate };
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 
90549Smuffin int
main(int argc,char ** argv)910Sstevel@tonic-gate main(int argc, char **argv)
920Sstevel@tonic-gate {
93549Smuffin 	char *cp;
944549Srralphs 	char *tape;
954549Srralphs 	int mtfd;
964549Srralphs 	struct commands const *comp;
974549Srralphs 	struct mtget mt_status;
984549Srralphs 	struct mtlop mt_com;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	if (argc > 2 && (equal(argv[1], "-t") || equal(argv[1], "-f"))) {
1010Sstevel@tonic-gate 		argc -= 2;
1020Sstevel@tonic-gate 		tape = argv[2];
1030Sstevel@tonic-gate 		argv += 2;
1044549Srralphs 	} else {
1054549Srralphs 		tape = getenv("TAPE");
1064549Srralphs 		if (tape == NULL) {
1070Sstevel@tonic-gate 			tape = DEFAULT_NRW_TAPE;
1084549Srralphs 		}
1094549Srralphs 	}
1104549Srralphs 
1110Sstevel@tonic-gate 	if (argc < 2) {
1120Sstevel@tonic-gate 		(void) fprintf(stderr,
1130Sstevel@tonic-gate 		    "usage: mt [ -f device ] command [ count ]\n");
1144549Srralphs 		return (1);
1150Sstevel@tonic-gate 	}
1164549Srralphs 
1170Sstevel@tonic-gate 	cp = argv[1];
1184549Srralphs 	for (comp = com; comp->c_name != NULL; comp++) {
1194549Srralphs 		if (strncmp(cp, comp->c_name, strlen(cp)) == 0) {
1200Sstevel@tonic-gate 			break;
1214549Srralphs 		}
1224549Srralphs 	}
1234549Srralphs 
1240Sstevel@tonic-gate 	if (comp->c_name == NULL) {
1250Sstevel@tonic-gate 		(void) fprintf(stderr, "mt: unknown command: %s\n", cp);
1264549Srralphs 		return (1);
1270Sstevel@tonic-gate 	}
1280Sstevel@tonic-gate 
1294549Srralphs 	mtfd = open(tape, comp->c_oflag);
1304549Srralphs 	if (mtfd < 0) {
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 		/*
1330Sstevel@tonic-gate 		 * Provide additional error message decoding since
1340Sstevel@tonic-gate 		 * we need additional error codes to fix them problem.
1350Sstevel@tonic-gate 		 */
1360Sstevel@tonic-gate 		if (errno == EIO) {
1370Sstevel@tonic-gate 			(void) fprintf(stderr,
1384549Srralphs 			    "%s: no tape loaded or drive offline\n", tape);
1390Sstevel@tonic-gate 		} else if (errno == EACCES) {
1400Sstevel@tonic-gate 			(void) fprintf(stderr,
141549Smuffin 			    "%s: write protected or reserved.\n", tape);
1420Sstevel@tonic-gate 		} else {
1430Sstevel@tonic-gate 			perror(tape);
1440Sstevel@tonic-gate 		}
1454549Srralphs 		return (1);
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	if (comp->c_code == MTIOCFORCERESERVE ||
149549Smuffin 	    comp->c_code == MTIOCRESERVE ||
150549Smuffin 	    comp->c_code == MTIOCRELEASE)  {
1510Sstevel@tonic-gate 		/*
1520Sstevel@tonic-gate 		 * Handle all MTIOC ioctls used in
1530Sstevel@tonic-gate 		 * reservation/release/takeownership.
1540Sstevel@tonic-gate 		 */
1550Sstevel@tonic-gate 		if (ioctl(mtfd, comp->c_code) < 0) {
1560Sstevel@tonic-gate 			perror("mt");
1574549Srralphs 			return (2);
1580Sstevel@tonic-gate 		}
1590Sstevel@tonic-gate 	} else if (comp->c_code == MTASF) {
1600Sstevel@tonic-gate 		/*
1610Sstevel@tonic-gate 		 * Handle absolute file positioning.  Ask tape driver
1620Sstevel@tonic-gate 		 * where tape is and then skip to desired file.  If
1630Sstevel@tonic-gate 		 * driver doesn't support get location ioctl, rewind
1640Sstevel@tonic-gate 		 * the tape and then space to the desired file.
1650Sstevel@tonic-gate 		 */
1660Sstevel@tonic-gate 		int usecnt;
167*5628Srralphs 		daddr_t mt_fileno;
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 		usecnt = argc > 2 && comp->c_usecnt;
1704549Srralphs 		mt_fileno = usecnt ? atol(argv[2]) : 1;
1710Sstevel@tonic-gate 		if (mt_fileno < 0) {
1720Sstevel@tonic-gate 			(void) fprintf(stderr, "mt: negative file number\n");
1734549Srralphs 			return (1);
1740Sstevel@tonic-gate 		}
1750Sstevel@tonic-gate 		(void) ioctl(mtfd, MTIOCGET, (char *)&mt_status);
1760Sstevel@tonic-gate 		if (ioctl(mtfd, MTIOCGET, (char *)&mt_status) < 0) {
1770Sstevel@tonic-gate 			perror("mt");
1784549Srralphs 			return (2);
1790Sstevel@tonic-gate 		}
1800Sstevel@tonic-gate 		/*
1810Sstevel@tonic-gate 		 * Check if device supports reporting current file
1820Sstevel@tonic-gate 		 * tape file position.  If not, rewind the tape, and
1830Sstevel@tonic-gate 		 * space forward.
1844549Srralphs 		 *
1854549Srralphs 		 * If file number is -1 tape position is unknown!
1860Sstevel@tonic-gate 		 */
1874549Srralphs 		if ((mt_status.mt_flags & MTF_ASF) == 0 ||
1884549Srralphs 		    (mt_status.mt_fileno == -1)) {
1890Sstevel@tonic-gate 			/* printf("mt: rewind\n"); */
1900Sstevel@tonic-gate 			mt_com.mt_count = 1;
1910Sstevel@tonic-gate 			mt_com.mt_op = MTREW;
1924549Srralphs 			if (ioctl(mtfd, MTIOCLTOP, &mt_com) < 0) {
193*5628Srralphs 				(void) fprintf(stderr, "%s %s %ld ",
194549Smuffin 				    tape, comp->c_name, mt_fileno);
1950Sstevel@tonic-gate 				perror("mt");
1964549Srralphs 				return (2);
1970Sstevel@tonic-gate 			}
1984549Srralphs 			/* Needed to rewind which worked now correct fileno */
1994549Srralphs 			mt_status.mt_fileno = 0;
2004549Srralphs 			mt_status.mt_blkno = 0;
2010Sstevel@tonic-gate 		}
2020Sstevel@tonic-gate 		if (mt_fileno < mt_status.mt_fileno) {
2030Sstevel@tonic-gate 			mt_com.mt_op = MTNBSF;
2044549Srralphs 			mt_com.mt_count = mt_status.mt_fileno - mt_fileno;
2050Sstevel@tonic-gate 			/* printf("mt: bsf= %d\n", mt_com.mt_count); */
2060Sstevel@tonic-gate 		} else {
2070Sstevel@tonic-gate 			mt_com.mt_op = MTFSF;
2080Sstevel@tonic-gate 			mt_com.mt_count =  mt_fileno - mt_status.mt_fileno;
2090Sstevel@tonic-gate 			/* printf("mt: fsf= %d\n", mt_com.mt_count); */
2100Sstevel@tonic-gate 		}
2114549Srralphs 		if (ioctl(mtfd, MTIOCLTOP, &mt_com) < 0) {
212*5628Srralphs 			(void) fprintf(stderr, "%s %s %ld ", tape, comp->c_name,
213549Smuffin 			    mt_fileno);
2140Sstevel@tonic-gate 			perror("failed");
2154549Srralphs 			return (2);
2160Sstevel@tonic-gate 		}
2170Sstevel@tonic-gate 	} else if (comp->c_code == MTIOCGETDRIVETYPE) {
2184549Srralphs 		return (print_config(mtfd));
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate 	/* Handle regular mag tape ioctls */
2210Sstevel@tonic-gate 	} else if (comp->c_code != MTNOP) {
2220Sstevel@tonic-gate 		int usecnt;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 		mt_com.mt_op = comp->c_code;
2250Sstevel@tonic-gate 		usecnt = argc > 2 && comp->c_usecnt;
2264549Srralphs 		mt_com.mt_count = (usecnt ? atoll(argv[2]) : 1);
2270Sstevel@tonic-gate 		if (mt_com.mt_count < 0) {
2284549Srralphs 			(void) fprintf(stderr, "mt: negative %s count\n",
2294549Srralphs 			    comp->c_name);
2304549Srralphs 			return (1);
2310Sstevel@tonic-gate 		}
2324549Srralphs 		if (ioctl(mtfd, MTIOCLTOP, &mt_com) < 0) {
2334549Srralphs 			/*
2344549Srralphs 			 * If we asked for a seek and it returns a tell
2354549Srralphs 			 * we attempted to seek more then there was.
2364549Srralphs 			 */
2374549Srralphs 			if (mt_com.mt_op == MTTELL &&
2384549Srralphs 			    comp->c_code == MTSEEK) {
2394549Srralphs 				(void) printf("partial seek:at block = %llu.\n",
2404549Srralphs 				    mt_com.mt_count);
2414549Srralphs 			} else {
2424549Srralphs 				(void) fprintf(stderr, "%s %s %lld ", tape,
2434549Srralphs 				    comp->c_name, mt_com.mt_count);
2444549Srralphs 				perror("failed");
2454549Srralphs 			}
2464549Srralphs 			return (2);
2470Sstevel@tonic-gate 		}
2484549Srralphs 		if (mt_com.mt_op == MTTELL) {
2494549Srralphs 			(void) printf("At block = %llu.\n", mt_com.mt_count);
2504549Srralphs 		}
2514549Srralphs 
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 	/* Handle status ioctl */
2540Sstevel@tonic-gate 	} else {
2550Sstevel@tonic-gate 		if (ioctl(mtfd, MTIOCGET, (char *)&mt_status) < 0) {
2560Sstevel@tonic-gate 			perror("mt");
2574549Srralphs 			return (2);
2580Sstevel@tonic-gate 		}
2594549Srralphs 		return (status(mtfd, &mt_status));
2600Sstevel@tonic-gate 	}
2610Sstevel@tonic-gate 	return (0);
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate 
2644549Srralphs static int
print_config(int mtfd)2654549Srralphs print_config(int mtfd)
2660Sstevel@tonic-gate {
2670Sstevel@tonic-gate 	struct mtdrivetype mdt;
2680Sstevel@tonic-gate 	struct mtdrivetype_request mdt_req;
2690Sstevel@tonic-gate 	char cfgname[48];
2700Sstevel@tonic-gate 	char tmp[2];
2710Sstevel@tonic-gate 	char *name;
2720Sstevel@tonic-gate 	int i;
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	mdt_req.size = sizeof (mdt);
2750Sstevel@tonic-gate 	mdt_req.mtdtp = &mdt;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	if (ioctl(mtfd, MTIOCGETDRIVETYPE, &mdt_req) != 0) {
2780Sstevel@tonic-gate 		perror("mt config");
2794549Srralphs 		return (2);
2800Sstevel@tonic-gate 	}
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	/*
2830Sstevel@tonic-gate 	 * remove trailing spaces from product id.
2840Sstevel@tonic-gate 	 */
2850Sstevel@tonic-gate 	for (i = VIDPIDLEN; i; i--) {
2860Sstevel@tonic-gate 		if (isspace(mdt.vid[i]) || mdt.vid[i] == '*') {
2870Sstevel@tonic-gate 			mdt.vid[i] = 0;
2880Sstevel@tonic-gate 		} else if (mdt.vid[i] == 0) {
2890Sstevel@tonic-gate 			continue;
2900Sstevel@tonic-gate 		} else {
2910Sstevel@tonic-gate 			break;
2920Sstevel@tonic-gate 		}
2930Sstevel@tonic-gate 	}
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	/*
2960Sstevel@tonic-gate 	 * If this is a generic name display the Vid and Pid instead.
2970Sstevel@tonic-gate 	 */
2980Sstevel@tonic-gate 	if (strstr(mdt.name, "Vendor '") == NULL) {
2990Sstevel@tonic-gate 		name = mdt.name;
3000Sstevel@tonic-gate 	} else {
3010Sstevel@tonic-gate 		name = mdt.vid;
3020Sstevel@tonic-gate 	}
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	/*
3050Sstevel@tonic-gate 	 * Attempt to create a configuration name using vid and pid.
3060Sstevel@tonic-gate 	 */
3070Sstevel@tonic-gate 	(void) strcpy(cfgname, "CFG");
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	for (tmp[1] = i = 0; i < VIDPIDLEN; i++) {
3100Sstevel@tonic-gate 		if (!isalnum(name[i]))
3110Sstevel@tonic-gate 			continue;
3120Sstevel@tonic-gate 		if (isspace(name[i]))
3130Sstevel@tonic-gate 			continue;
3140Sstevel@tonic-gate 		tmp[0] = toupper(name[i]);
3150Sstevel@tonic-gate 		(void) strncat(cfgname, tmp, 1);
3160Sstevel@tonic-gate 	}
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	(void) printf("\"%s\", \"%s\", \"%s\";\n", mdt.vid, name, cfgname);
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	/*
3210Sstevel@tonic-gate 	 * Don't want show some bits, ST_DYNAMIC is set in the driver
3220Sstevel@tonic-gate 	 * so one can tell that its not a compiled in config.
3230Sstevel@tonic-gate 	 * The ST_LONG_ERASE and ST_LONG_TIMEOUTS are not displayed
3240Sstevel@tonic-gate 	 * becouse the timeout values below already reflect them being
3250Sstevel@tonic-gate 	 * set.
3260Sstevel@tonic-gate 	 * Also ST_KNOWS_MEDIA is not displayed as it can not be configured
3270Sstevel@tonic-gate 	 * from an st.conf entry.
3280Sstevel@tonic-gate 	 */
3290Sstevel@tonic-gate 	(void) printf("%s = 2,0x%X,%d,0x%X,", cfgname,
3300Sstevel@tonic-gate 	    mdt.type, mdt.bsize, mdt.options &
3310Sstevel@tonic-gate 	    ~(ST_DYNAMIC | ST_LONG_ERASE | ST_LONG_TIMEOUTS | ST_KNOWS_MEDIA));
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	(void) printf("4,0x%2.2X,0x%2.2X,0x%2.2X,0x%2.2X,%d,",
3340Sstevel@tonic-gate 	    mdt.densities[0], mdt.densities[1], mdt.densities[2],
3350Sstevel@tonic-gate 	    mdt.densities[3], mdt.default_density >> 3);
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	(void) printf("%d,%d,%d,%d,%d,%d,%d;\n", mdt.non_motion_timeout,
3380Sstevel@tonic-gate 	    mdt.io_timeout, mdt.rewind_timeout, mdt.space_timeout,
3390Sstevel@tonic-gate 	    mdt.load_timeout, mdt.unload_timeout, mdt.erase_timeout);
3404549Srralphs 
3414549Srralphs 	return (0);
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate /*
3450Sstevel@tonic-gate  * Interpret the status buffer returned
3460Sstevel@tonic-gate  */
3474549Srralphs static int
status(int mtfd,struct mtget * bp)3484549Srralphs status(int mtfd, struct mtget *bp)
3490Sstevel@tonic-gate {
3500Sstevel@tonic-gate 	struct mtdrivetype mdt;
3510Sstevel@tonic-gate 	struct mtdrivetype_request mdt_req;
3524549Srralphs 	const char *name = (char *)NULL;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	/*
3550Sstevel@tonic-gate 	 * Make a call to MTIOCGETDRIVETYPE ioctl, Also use old method
3560Sstevel@tonic-gate 	 * of MT_TAPE_INFO for now, but MT_TAPE_INFO should dissapear in 2.7
3570Sstevel@tonic-gate 	 */
3580Sstevel@tonic-gate 	mdt_req.size = sizeof (struct mtdrivetype);
3590Sstevel@tonic-gate 	mdt_req.mtdtp = &mdt;
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate 	if (ioctl(mtfd, MTIOCGETDRIVETYPE, &mdt_req) == 0) {
3620Sstevel@tonic-gate 		name = mdt.name;
3630Sstevel@tonic-gate 		if (strstr(mdt.name, "Vendor '") != NULL) {
3640Sstevel@tonic-gate 			(void) printf("Unconfigured Drive: ");
3650Sstevel@tonic-gate 		}
3660Sstevel@tonic-gate 	} else {
3674549Srralphs 		perror("mt drivetype");
3684549Srralphs 		return (2);
3690Sstevel@tonic-gate 	}
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 	/* Handle SCSI tape drives specially. */
3720Sstevel@tonic-gate 	if ((bp->mt_flags & MTF_SCSI)) {
3730Sstevel@tonic-gate 		if (name == (char *)NULL) {
3744549Srralphs 			name = "SCSI";
3750Sstevel@tonic-gate 		}
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 		(void) printf("%s tape drive:\n", name);
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate 		(void) printf("   sense key(0x%x)= %s   residual= %ld   ",
381549Smuffin 		    bp->mt_erreg, print_key(bp->mt_erreg), bp->mt_resid);
3820Sstevel@tonic-gate 		(void) printf("retries= %d\n", bp->mt_dsreg);
383*5628Srralphs 		/*
384*5628Srralphs 		 * Can overflow the signed numbers.
385*5628Srralphs 		 * fileno will be -1 on error but all other positions are
386*5628Srralphs 		 * positive. blkno will never be negative.
387*5628Srralphs 		 */
388*5628Srralphs 		if (bp->mt_fileno == -1) {
389*5628Srralphs 			(void) printf("   file no= -1   block no= %lu\n",
390*5628Srralphs 			    (unsigned long)bp->mt_blkno);
391*5628Srralphs 		} else {
392*5628Srralphs 			(void) printf("   file no= %lu   block no= %lu\n",
393*5628Srralphs 			    (unsigned long)bp->mt_fileno,
394*5628Srralphs 			    (unsigned long)bp->mt_blkno);
395*5628Srralphs 		}
3962537Srralphs 		if ((bp->mt_flags & MTF_WORM_MEDIA) != 0) {
3972537Srralphs 			(void) printf("   WORM media\n");
3982537Srralphs 		}
3990Sstevel@tonic-gate 	} else {
4000Sstevel@tonic-gate 		/* Handle non-SCSI drives here. */
4014549Srralphs 		if (name == NULL) {
4020Sstevel@tonic-gate 			(void) printf("unknown tape drive type (0x%x)\n",
4034549Srralphs 			    mdt.type);
4044549Srralphs 			return (2);
4050Sstevel@tonic-gate 		}
4064549Srralphs 		(void) printf("%s tape drive:\n   residual= %ld", name,
4070Sstevel@tonic-gate 		    bp->mt_resid);
4084549Srralphs 		printreg("   ds", (ushort_t)bp->mt_dsreg, 0);
4094549Srralphs 		printreg("   er", (ushort_t)bp->mt_erreg, 0);
4100Sstevel@tonic-gate 		(void) putchar('\n');
4110Sstevel@tonic-gate 	}
4124549Srralphs 	return (0);
4130Sstevel@tonic-gate }
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate /*
4170Sstevel@tonic-gate  * Define SCSI sense key error messages.
4180Sstevel@tonic-gate  *
4190Sstevel@tonic-gate  * The first 16 sense keys are SCSI standard
4200Sstevel@tonic-gate  * sense keys. The keys after this are
4210Sstevel@tonic-gate  * Sun Specifice 'sense' keys- e.g., crap.
4220Sstevel@tonic-gate  */
4230Sstevel@tonic-gate 
4244549Srralphs static char *sense_keys[] = {
4250Sstevel@tonic-gate 	"No Additional Sense",		/* 0x00 */
4260Sstevel@tonic-gate 	"Soft Error",			/* 0x01 */
4270Sstevel@tonic-gate 	"Not Ready",			/* 0x02 */
4280Sstevel@tonic-gate 	"Media Error",			/* 0x03 */
4290Sstevel@tonic-gate 	"Hardware Error",		/* 0x04 */
4300Sstevel@tonic-gate 	"Illegal Request",		/* 0x05 */
4310Sstevel@tonic-gate 	"Unit Attention",		/* 0x06 */
4320Sstevel@tonic-gate 	"Write Protected",		/* 0x07 */
4330Sstevel@tonic-gate 	"Blank Check",			/* 0x08 */
4340Sstevel@tonic-gate 	"Vendor Unique",		/* 0x09 */
4350Sstevel@tonic-gate 	"Copy Aborted",			/* 0x0a */
4360Sstevel@tonic-gate 	"Aborted Command",		/* 0x0b */
4370Sstevel@tonic-gate 	"Equal Error",			/* 0x0c */
4380Sstevel@tonic-gate 	"Volume Overflow",		/* 0x0d */
4390Sstevel@tonic-gate 	"Miscompare Error",		/* 0x0e */
4404549Srralphs 	"Reserved",			/* 0x0f */
4414549Srralphs #ifdef	sun
4420Sstevel@tonic-gate 	"fatal",			/* 0x10 */
4430Sstevel@tonic-gate 	"timeout",			/* 0x11 */
4440Sstevel@tonic-gate 	"EOF",				/* 0x12 */
4450Sstevel@tonic-gate 	"EOT",				/* 0x13 */
4460Sstevel@tonic-gate 	"length error",			/* 0x14 */
4470Sstevel@tonic-gate 	"BOT",				/* 0x15 */
4480Sstevel@tonic-gate 	"wrong tape media",		/* 0x16 */
4494549Srralphs #endif
4500Sstevel@tonic-gate };
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate /*
4530Sstevel@tonic-gate  * Return the text string associated with the sense key value.
4540Sstevel@tonic-gate  */
4550Sstevel@tonic-gate static char *
print_key(short key_code)4560Sstevel@tonic-gate print_key(short key_code)
4570Sstevel@tonic-gate {
4580Sstevel@tonic-gate 	static char unknown[32];
4594549Srralphs 
4604549Srralphs 	if ((key_code >= 0) &&
4614549Srralphs 	    (key_code < (sizeof (sense_keys) / sizeof (sense_keys[0])))) {
4624549Srralphs 		return (sense_keys[key_code]);
4630Sstevel@tonic-gate 	}
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	(void) sprintf(unknown, "unknown sense key: 0x%x",
4660Sstevel@tonic-gate 	    (unsigned int) key_code);
4670Sstevel@tonic-gate 	return (unknown);
4680Sstevel@tonic-gate }
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate /*
4720Sstevel@tonic-gate  * Print a register a la the %b format of the kernel's printf
4730Sstevel@tonic-gate  */
4740Sstevel@tonic-gate static void
printreg(char * s,ushort_t v,char * bits)4750Sstevel@tonic-gate printreg(char *s, ushort_t v, char *bits)
4760Sstevel@tonic-gate {
4770Sstevel@tonic-gate 	int i, any = 0;
4780Sstevel@tonic-gate 	char c;
4790Sstevel@tonic-gate 
4804549Srralphs 	if (bits && *bits == 8) {
4810Sstevel@tonic-gate 		(void) printf("%s = %o", s, v);
4824549Srralphs 	} else {
4830Sstevel@tonic-gate 		(void) printf("%s = %x", s, v);
4844549Srralphs 	}
4850Sstevel@tonic-gate 	bits++;
4860Sstevel@tonic-gate 	if (v && bits) {
4870Sstevel@tonic-gate 		(void) putchar('<');
4880Sstevel@tonic-gate 		while ((i = *bits++) != 0) {
4890Sstevel@tonic-gate 			if (v & (1 << (i-1))) {
4904549Srralphs 				if (any) {
4910Sstevel@tonic-gate 					(void) putchar(',');
4924549Srralphs 				}
4930Sstevel@tonic-gate 				any = 1;
4944549Srralphs 				for (; (c = *bits) > 32; bits++) {
4950Sstevel@tonic-gate 					(void) putchar(c);
4964549Srralphs 				}
4974549Srralphs 			} else {
4980Sstevel@tonic-gate 				for (; *bits > 32; bits++)
4990Sstevel@tonic-gate 					;
5004549Srralphs 			}
5010Sstevel@tonic-gate 		}
5020Sstevel@tonic-gate 		(void) putchar('>');
5030Sstevel@tonic-gate 	}
5040Sstevel@tonic-gate }
505