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 /*
137c478bd9Sstevel@tonic-gate * mt -- magnetic tape manipulation program
147c478bd9Sstevel@tonic-gate */
157c478bd9Sstevel@tonic-gate #include <stdio.h>
167c478bd9Sstevel@tonic-gate #include <ctype.h>
177c478bd9Sstevel@tonic-gate
187c478bd9Sstevel@tonic-gate #include <errno.h>
197c478bd9Sstevel@tonic-gate #include <sys/types.h>
207c478bd9Sstevel@tonic-gate #include <sys/mtio.h>
217c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
227c478bd9Sstevel@tonic-gate #include <sys/param.h>
237c478bd9Sstevel@tonic-gate #include <sys/buf.h>
247c478bd9Sstevel@tonic-gate #include <sys/conf.h>
257c478bd9Sstevel@tonic-gate #include <sys/file.h>
267c478bd9Sstevel@tonic-gate #include <sys/uio.h>
277c478bd9Sstevel@tonic-gate #include <string.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <unistd.h>
307c478bd9Sstevel@tonic-gate #include <sys/stat.h>
317c478bd9Sstevel@tonic-gate #include <sys/scsi/targets/stdef.h>
327c478bd9Sstevel@tonic-gate #include <fcntl.h>
337c478bd9Sstevel@tonic-gate
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate #define equal(s1, s2) (strcmp(s1, s2) == 0)
367c478bd9Sstevel@tonic-gate #define MTASF 100 /* absolute file positioning; first file is 0 */
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate * This can't be DEFTAPE in mtio.h because that is currently the rewinding
407c478bd9Sstevel@tonic-gate * unit which makes 'mt fsf' a questionable activity at best.
417c478bd9Sstevel@tonic-gate */
427c478bd9Sstevel@tonic-gate #define DEFAULT_NRW_TAPE "/dev/rmt/0n"
437c478bd9Sstevel@tonic-gate
44c6914c10Srralphs static int print_config(int mtfd);
457c478bd9Sstevel@tonic-gate static char *print_key(short key_code);
467c478bd9Sstevel@tonic-gate static void printreg(char *, ushort_t, char *);
47c6914c10Srralphs static int status(int mtfd, struct mtget *);
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate /* Pseudo flag for open even if drive is not ready (Unloaded) or reserved */
507c478bd9Sstevel@tonic-gate #define O_UNLOAD (O_RDWR | O_NDELAY)
517c478bd9Sstevel@tonic-gate
52c6914c10Srralphs static const struct commands {
537c478bd9Sstevel@tonic-gate char *c_name;
547c478bd9Sstevel@tonic-gate int c_code;
557c478bd9Sstevel@tonic-gate int c_oflag;
567c478bd9Sstevel@tonic-gate int c_usecnt;
577c478bd9Sstevel@tonic-gate } com[] = {
587c478bd9Sstevel@tonic-gate { "weof", MTWEOF, O_RDWR, 1 },
597c478bd9Sstevel@tonic-gate { "eof", MTWEOF, O_RDWR, 1 },
607c478bd9Sstevel@tonic-gate { "fsf", MTFSF, O_RDONLY, 1 },
617c478bd9Sstevel@tonic-gate { "bsf", MTBSF, O_RDONLY, 1 },
627c478bd9Sstevel@tonic-gate { "asf", MTASF, O_RDONLY, 1 },
637c478bd9Sstevel@tonic-gate { "fsr", MTFSR, O_RDONLY, 1 },
647c478bd9Sstevel@tonic-gate { "bsr", MTBSR, O_RDONLY, 1 },
657c478bd9Sstevel@tonic-gate { "rewind", MTREW, O_RDONLY, 0 },
667c478bd9Sstevel@tonic-gate { "offline", MTOFFL, O_RDONLY, 0 },
677c478bd9Sstevel@tonic-gate { "rewoffl", MTOFFL, O_RDONLY, 0 },
687c478bd9Sstevel@tonic-gate { "status", MTNOP, O_RDONLY, 0 },
697c478bd9Sstevel@tonic-gate { "retension", MTRETEN, O_RDONLY, 0 },
707c478bd9Sstevel@tonic-gate { "erase", MTERASE, O_RDWR, 0 },
717c478bd9Sstevel@tonic-gate { "eom", MTEOM, O_RDONLY, 0 },
727c478bd9Sstevel@tonic-gate { "nbsf", MTNBSF, O_RDONLY, 1 },
737c478bd9Sstevel@tonic-gate { "reserve", MTIOCRESERVE, O_RDONLY, 0 },
747c478bd9Sstevel@tonic-gate { "release", MTIOCRELEASE, O_RDONLY, 0 },
757c478bd9Sstevel@tonic-gate { "forcereserve", MTIOCFORCERESERVE, O_UNLOAD, 0 },
767c478bd9Sstevel@tonic-gate { "config", MTIOCGETDRIVETYPE, O_UNLOAD, 0 },
77c6914c10Srralphs { "fssf", MTFSSF, O_RDONLY, 1 },
78c6914c10Srralphs { "bssf", MTBSSF, O_RDONLY, 1 },
79c6914c10Srralphs { "tell", MTTELL, O_RDONLY, 0 },
80c6914c10Srralphs { "seek", MTSEEK, O_RDONLY, 1 },
81c6914c10Srralphs { "load", MTLOAD, O_UNLOAD, 0 },
82c6914c10Srralphs { "lock", MTLOCK, O_RDONLY, 0 },
83c6914c10Srralphs { "unlock", MTUNLOCK, O_RDONLY, 0 },
847c478bd9Sstevel@tonic-gate { 0 }
857c478bd9Sstevel@tonic-gate };
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate
888d489c7aSmuffin int
main(int argc,char ** argv)897c478bd9Sstevel@tonic-gate main(int argc, char **argv)
907c478bd9Sstevel@tonic-gate {
918d489c7aSmuffin char *cp;
92c6914c10Srralphs char *tape;
93c6914c10Srralphs int mtfd;
94c6914c10Srralphs struct commands const *comp;
95c6914c10Srralphs struct mtget mt_status;
96c6914c10Srralphs struct mtlop mt_com;
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate if (argc > 2 && (equal(argv[1], "-t") || equal(argv[1], "-f"))) {
997c478bd9Sstevel@tonic-gate argc -= 2;
1007c478bd9Sstevel@tonic-gate tape = argv[2];
1017c478bd9Sstevel@tonic-gate argv += 2;
102c6914c10Srralphs } else {
103c6914c10Srralphs tape = getenv("TAPE");
104c6914c10Srralphs if (tape == NULL) {
1057c478bd9Sstevel@tonic-gate tape = DEFAULT_NRW_TAPE;
106c6914c10Srralphs }
107c6914c10Srralphs }
108c6914c10Srralphs
1097c478bd9Sstevel@tonic-gate if (argc < 2) {
1107c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1117c478bd9Sstevel@tonic-gate "usage: mt [ -f device ] command [ count ]\n");
112c6914c10Srralphs return (1);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate
115c6914c10Srralphs cp = argv[1];
116c6914c10Srralphs for (comp = com; comp->c_name != NULL; comp++) {
117c6914c10Srralphs if (strncmp(cp, comp->c_name, strlen(cp)) == 0) {
118c6914c10Srralphs break;
119c6914c10Srralphs }
120c6914c10Srralphs }
121c6914c10Srralphs
122c6914c10Srralphs if (comp->c_name == NULL) {
123c6914c10Srralphs (void) fprintf(stderr, "mt: unknown command: %s\n", cp);
124c6914c10Srralphs return (1);
125c6914c10Srralphs }
126c6914c10Srralphs
127c6914c10Srralphs mtfd = open(tape, comp->c_oflag);
128c6914c10Srralphs if (mtfd < 0) {
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate /*
1317c478bd9Sstevel@tonic-gate * Provide additional error message decoding since
1327c478bd9Sstevel@tonic-gate * we need additional error codes to fix them problem.
1337c478bd9Sstevel@tonic-gate */
1347c478bd9Sstevel@tonic-gate if (errno == EIO) {
1357c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
136c6914c10Srralphs "%s: no tape loaded or drive offline\n", tape);
1377c478bd9Sstevel@tonic-gate } else if (errno == EACCES) {
1387c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
1397c478bd9Sstevel@tonic-gate "%s: write protected or reserved.\n", tape);
1407c478bd9Sstevel@tonic-gate } else {
1417c478bd9Sstevel@tonic-gate perror(tape);
1427c478bd9Sstevel@tonic-gate }
143c6914c10Srralphs return (1);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate if (comp->c_code == MTIOCFORCERESERVE ||
1477c478bd9Sstevel@tonic-gate comp->c_code == MTIOCRESERVE ||
1487c478bd9Sstevel@tonic-gate comp->c_code == MTIOCRELEASE) {
1497c478bd9Sstevel@tonic-gate /*
1507c478bd9Sstevel@tonic-gate * Handle all MTIOC ioctls used in
1517c478bd9Sstevel@tonic-gate * reservation/release/takeownership.
1527c478bd9Sstevel@tonic-gate */
1537c478bd9Sstevel@tonic-gate if (ioctl(mtfd, comp->c_code) < 0) {
1547c478bd9Sstevel@tonic-gate perror("mt");
155c6914c10Srralphs return (2);
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate } else if (comp->c_code == MTASF) {
1587c478bd9Sstevel@tonic-gate /*
1597c478bd9Sstevel@tonic-gate * Handle absolute file positioning. Ask tape driver
1607c478bd9Sstevel@tonic-gate * where tape is and then skip to desired file. If
1617c478bd9Sstevel@tonic-gate * driver doesn't support get location ioctl, rewind
1627c478bd9Sstevel@tonic-gate * the tape and then space to the desired file.
1637c478bd9Sstevel@tonic-gate */
1647c478bd9Sstevel@tonic-gate int usecnt;
165*f218e94bSrralphs daddr_t mt_fileno;
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate usecnt = argc > 2 && comp->c_usecnt;
168c6914c10Srralphs mt_fileno = usecnt ? atol(argv[2]) : 1;
1697c478bd9Sstevel@tonic-gate if (mt_fileno < 0) {
1707c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "mt: negative file number\n");
171c6914c10Srralphs return (1);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate (void) ioctl(mtfd, MTIOCGET, (char *)&mt_status);
1747c478bd9Sstevel@tonic-gate if (ioctl(mtfd, MTIOCGET, (char *)&mt_status) < 0) {
1757c478bd9Sstevel@tonic-gate perror("mt");
176c6914c10Srralphs return (2);
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate /*
1797c478bd9Sstevel@tonic-gate * Check if device supports reporting current file
1807c478bd9Sstevel@tonic-gate * tape file position. If not, rewind the tape, and
1817c478bd9Sstevel@tonic-gate * space forward.
182c6914c10Srralphs *
183c6914c10Srralphs * If file number is -1 tape position is unknown!
1847c478bd9Sstevel@tonic-gate */
185c6914c10Srralphs if ((mt_status.mt_flags & MTF_ASF) == 0 ||
186c6914c10Srralphs (mt_status.mt_fileno == -1)) {
1877c478bd9Sstevel@tonic-gate /* printf("mt: rewind\n"); */
1887c478bd9Sstevel@tonic-gate mt_com.mt_count = 1;
1897c478bd9Sstevel@tonic-gate mt_com.mt_op = MTREW;
190c6914c10Srralphs if (ioctl(mtfd, MTIOCLTOP, &mt_com) < 0) {
191*f218e94bSrralphs (void) fprintf(stderr, "%s %s %ld ",
1927c478bd9Sstevel@tonic-gate tape, comp->c_name, mt_fileno);
1937c478bd9Sstevel@tonic-gate perror("mt");
194c6914c10Srralphs return (2);
1957c478bd9Sstevel@tonic-gate }
196c6914c10Srralphs /* Needed to rewind which worked now correct fileno */
197c6914c10Srralphs mt_status.mt_fileno = 0;
198c6914c10Srralphs mt_status.mt_blkno = 0;
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate if (mt_fileno < mt_status.mt_fileno) {
2017c478bd9Sstevel@tonic-gate mt_com.mt_op = MTNBSF;
2027c478bd9Sstevel@tonic-gate mt_com.mt_count = mt_status.mt_fileno - mt_fileno;
2037c478bd9Sstevel@tonic-gate /* printf("mt: bsf= %d\n", mt_com.mt_count); */
2047c478bd9Sstevel@tonic-gate } else {
2057c478bd9Sstevel@tonic-gate mt_com.mt_op = MTFSF;
2067c478bd9Sstevel@tonic-gate mt_com.mt_count = mt_fileno - mt_status.mt_fileno;
2077c478bd9Sstevel@tonic-gate /* printf("mt: fsf= %d\n", mt_com.mt_count); */
2087c478bd9Sstevel@tonic-gate }
209c6914c10Srralphs if (ioctl(mtfd, MTIOCLTOP, &mt_com) < 0) {
210*f218e94bSrralphs (void) fprintf(stderr, "%s %s %ld ", tape, comp->c_name,
2117c478bd9Sstevel@tonic-gate mt_fileno);
2127c478bd9Sstevel@tonic-gate perror("failed");
213c6914c10Srralphs return (2);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate } else if (comp->c_code == MTIOCGETDRIVETYPE) {
216c6914c10Srralphs return (print_config(mtfd));
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate /* Handle regular mag tape ioctls */
2197c478bd9Sstevel@tonic-gate } else if (comp->c_code != MTNOP) {
2207c478bd9Sstevel@tonic-gate int usecnt;
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate mt_com.mt_op = comp->c_code;
2237c478bd9Sstevel@tonic-gate usecnt = argc > 2 && comp->c_usecnt;
224c6914c10Srralphs mt_com.mt_count = (usecnt ? atoll(argv[2]) : 1);
2257c478bd9Sstevel@tonic-gate if (mt_com.mt_count < 0) {
226c6914c10Srralphs (void) fprintf(stderr, "mt: negative %s count\n",
227c6914c10Srralphs comp->c_name);
228c6914c10Srralphs return (1);
2297c478bd9Sstevel@tonic-gate }
230c6914c10Srralphs if (ioctl(mtfd, MTIOCLTOP, &mt_com) < 0) {
231c6914c10Srralphs /*
232c6914c10Srralphs * If we asked for a seek and it returns a tell
233c6914c10Srralphs * we attempted to seek more then there was.
234c6914c10Srralphs */
235c6914c10Srralphs if (mt_com.mt_op == MTTELL &&
236c6914c10Srralphs comp->c_code == MTSEEK) {
237c6914c10Srralphs (void) printf("partial seek:at block = %llu.\n",
2387c478bd9Sstevel@tonic-gate mt_com.mt_count);
239c6914c10Srralphs } else {
240c6914c10Srralphs (void) fprintf(stderr, "%s %s %lld ", tape,
241c6914c10Srralphs comp->c_name, mt_com.mt_count);
2427c478bd9Sstevel@tonic-gate perror("failed");
2437c478bd9Sstevel@tonic-gate }
244c6914c10Srralphs return (2);
245c6914c10Srralphs }
246c6914c10Srralphs if (mt_com.mt_op == MTTELL) {
247c6914c10Srralphs (void) printf("At block = %llu.\n", mt_com.mt_count);
248c6914c10Srralphs }
249c6914c10Srralphs
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate /* Handle status ioctl */
2527c478bd9Sstevel@tonic-gate } else {
2537c478bd9Sstevel@tonic-gate if (ioctl(mtfd, MTIOCGET, (char *)&mt_status) < 0) {
2547c478bd9Sstevel@tonic-gate perror("mt");
255c6914c10Srralphs return (2);
2567c478bd9Sstevel@tonic-gate }
257c6914c10Srralphs return (status(mtfd, &mt_status));
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate return (0);
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate
262c6914c10Srralphs static int
print_config(int mtfd)263c6914c10Srralphs print_config(int mtfd)
2647c478bd9Sstevel@tonic-gate {
2657c478bd9Sstevel@tonic-gate struct mtdrivetype mdt;
2667c478bd9Sstevel@tonic-gate struct mtdrivetype_request mdt_req;
2677c478bd9Sstevel@tonic-gate char cfgname[48];
2687c478bd9Sstevel@tonic-gate char tmp[2];
2697c478bd9Sstevel@tonic-gate char *name;
2707c478bd9Sstevel@tonic-gate int i;
2717c478bd9Sstevel@tonic-gate
2727c478bd9Sstevel@tonic-gate mdt_req.size = sizeof (mdt);
2737c478bd9Sstevel@tonic-gate mdt_req.mtdtp = &mdt;
2747c478bd9Sstevel@tonic-gate
2757c478bd9Sstevel@tonic-gate if (ioctl(mtfd, MTIOCGETDRIVETYPE, &mdt_req) != 0) {
2767c478bd9Sstevel@tonic-gate perror("mt config");
277c6914c10Srralphs return (2);
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate
2807c478bd9Sstevel@tonic-gate /*
2817c478bd9Sstevel@tonic-gate * remove trailing spaces from product id.
2827c478bd9Sstevel@tonic-gate */
2837c478bd9Sstevel@tonic-gate for (i = VIDPIDLEN; i; i--) {
2847c478bd9Sstevel@tonic-gate if (isspace(mdt.vid[i]) || mdt.vid[i] == '*') {
2857c478bd9Sstevel@tonic-gate mdt.vid[i] = 0;
2867c478bd9Sstevel@tonic-gate } else if (mdt.vid[i] == 0) {
2877c478bd9Sstevel@tonic-gate continue;
2887c478bd9Sstevel@tonic-gate } else {
2897c478bd9Sstevel@tonic-gate break;
2907c478bd9Sstevel@tonic-gate }
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate
2937c478bd9Sstevel@tonic-gate /*
2947c478bd9Sstevel@tonic-gate * If this is a generic name display the Vid and Pid instead.
2957c478bd9Sstevel@tonic-gate */
2967c478bd9Sstevel@tonic-gate if (strstr(mdt.name, "Vendor '") == NULL) {
2977c478bd9Sstevel@tonic-gate name = mdt.name;
2987c478bd9Sstevel@tonic-gate } else {
2997c478bd9Sstevel@tonic-gate name = mdt.vid;
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate
3027c478bd9Sstevel@tonic-gate /*
3037c478bd9Sstevel@tonic-gate * Attempt to create a configuration name using vid and pid.
3047c478bd9Sstevel@tonic-gate */
3057c478bd9Sstevel@tonic-gate (void) strcpy(cfgname, "CFG");
3067c478bd9Sstevel@tonic-gate
3077c478bd9Sstevel@tonic-gate for (tmp[1] = i = 0; i < VIDPIDLEN; i++) {
3087c478bd9Sstevel@tonic-gate if (!isalnum(name[i]))
3097c478bd9Sstevel@tonic-gate continue;
3107c478bd9Sstevel@tonic-gate if (isspace(name[i]))
3117c478bd9Sstevel@tonic-gate continue;
3127c478bd9Sstevel@tonic-gate tmp[0] = toupper(name[i]);
3137c478bd9Sstevel@tonic-gate (void) strncat(cfgname, tmp, 1);
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate (void) printf("\"%s\", \"%s\", \"%s\";\n", mdt.vid, name, cfgname);
3177c478bd9Sstevel@tonic-gate
3187c478bd9Sstevel@tonic-gate /*
3197c478bd9Sstevel@tonic-gate * Don't want show some bits, ST_DYNAMIC is set in the driver
3207c478bd9Sstevel@tonic-gate * so one can tell that its not a compiled in config.
3217c478bd9Sstevel@tonic-gate * The ST_LONG_ERASE and ST_LONG_TIMEOUTS are not displayed
3227c478bd9Sstevel@tonic-gate * becouse the timeout values below already reflect them being
3237c478bd9Sstevel@tonic-gate * set.
3247c478bd9Sstevel@tonic-gate * Also ST_KNOWS_MEDIA is not displayed as it can not be configured
3257c478bd9Sstevel@tonic-gate * from an st.conf entry.
3267c478bd9Sstevel@tonic-gate */
3277c478bd9Sstevel@tonic-gate (void) printf("%s = 2,0x%X,%d,0x%X,", cfgname,
3287c478bd9Sstevel@tonic-gate mdt.type, mdt.bsize, mdt.options &
3297c478bd9Sstevel@tonic-gate ~(ST_DYNAMIC | ST_LONG_ERASE | ST_LONG_TIMEOUTS | ST_KNOWS_MEDIA));
3307c478bd9Sstevel@tonic-gate
3317c478bd9Sstevel@tonic-gate (void) printf("4,0x%2.2X,0x%2.2X,0x%2.2X,0x%2.2X,%d,",
3327c478bd9Sstevel@tonic-gate mdt.densities[0], mdt.densities[1], mdt.densities[2],
3337c478bd9Sstevel@tonic-gate mdt.densities[3], mdt.default_density >> 3);
3347c478bd9Sstevel@tonic-gate
3357c478bd9Sstevel@tonic-gate (void) printf("%d,%d,%d,%d,%d,%d,%d;\n", mdt.non_motion_timeout,
3367c478bd9Sstevel@tonic-gate mdt.io_timeout, mdt.rewind_timeout, mdt.space_timeout,
3377c478bd9Sstevel@tonic-gate mdt.load_timeout, mdt.unload_timeout, mdt.erase_timeout);
338c6914c10Srralphs
339c6914c10Srralphs return (0);
3407c478bd9Sstevel@tonic-gate }
3417c478bd9Sstevel@tonic-gate
3427c478bd9Sstevel@tonic-gate /*
3437c478bd9Sstevel@tonic-gate * Interpret the status buffer returned
3447c478bd9Sstevel@tonic-gate */
345c6914c10Srralphs static int
status(int mtfd,struct mtget * bp)346c6914c10Srralphs status(int mtfd, struct mtget *bp)
3477c478bd9Sstevel@tonic-gate {
3487c478bd9Sstevel@tonic-gate struct mtdrivetype mdt;
3497c478bd9Sstevel@tonic-gate struct mtdrivetype_request mdt_req;
350c6914c10Srralphs const char *name = (char *)NULL;
3517c478bd9Sstevel@tonic-gate
3527c478bd9Sstevel@tonic-gate /*
3537c478bd9Sstevel@tonic-gate * Make a call to MTIOCGETDRIVETYPE ioctl, Also use old method
3547c478bd9Sstevel@tonic-gate * of MT_TAPE_INFO for now, but MT_TAPE_INFO should dissapear in 2.7
3557c478bd9Sstevel@tonic-gate */
3567c478bd9Sstevel@tonic-gate mdt_req.size = sizeof (struct mtdrivetype);
3577c478bd9Sstevel@tonic-gate mdt_req.mtdtp = &mdt;
3587c478bd9Sstevel@tonic-gate
3597c478bd9Sstevel@tonic-gate if (ioctl(mtfd, MTIOCGETDRIVETYPE, &mdt_req) == 0) {
3607c478bd9Sstevel@tonic-gate name = mdt.name;
3617c478bd9Sstevel@tonic-gate if (strstr(mdt.name, "Vendor '") != NULL) {
3627c478bd9Sstevel@tonic-gate (void) printf("Unconfigured Drive: ");
3637c478bd9Sstevel@tonic-gate }
3647c478bd9Sstevel@tonic-gate } else {
365c6914c10Srralphs perror("mt drivetype");
366c6914c10Srralphs return (2);
3677c478bd9Sstevel@tonic-gate }
3687c478bd9Sstevel@tonic-gate
3697c478bd9Sstevel@tonic-gate /* Handle SCSI tape drives specially. */
3707c478bd9Sstevel@tonic-gate if ((bp->mt_flags & MTF_SCSI)) {
3717c478bd9Sstevel@tonic-gate if (name == (char *)NULL) {
3727c478bd9Sstevel@tonic-gate name = "SCSI";
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate
3757c478bd9Sstevel@tonic-gate
3767c478bd9Sstevel@tonic-gate (void) printf("%s tape drive:\n", name);
3777c478bd9Sstevel@tonic-gate
3787c478bd9Sstevel@tonic-gate (void) printf(" sense key(0x%x)= %s residual= %ld ",
3797c478bd9Sstevel@tonic-gate bp->mt_erreg, print_key(bp->mt_erreg), bp->mt_resid);
3807c478bd9Sstevel@tonic-gate (void) printf("retries= %d\n", bp->mt_dsreg);
381*f218e94bSrralphs /*
382*f218e94bSrralphs * Can overflow the signed numbers.
383*f218e94bSrralphs * fileno will be -1 on error but all other positions are
384*f218e94bSrralphs * positive. blkno will never be negative.
385*f218e94bSrralphs */
386*f218e94bSrralphs if (bp->mt_fileno == -1) {
387*f218e94bSrralphs (void) printf(" file no= -1 block no= %lu\n",
388*f218e94bSrralphs (unsigned long)bp->mt_blkno);
389*f218e94bSrralphs } else {
390*f218e94bSrralphs (void) printf(" file no= %lu block no= %lu\n",
391*f218e94bSrralphs (unsigned long)bp->mt_fileno,
392*f218e94bSrralphs (unsigned long)bp->mt_blkno);
393*f218e94bSrralphs }
3945988135dSrralphs if ((bp->mt_flags & MTF_WORM_MEDIA) != 0) {
3955988135dSrralphs (void) printf(" WORM media\n");
3965988135dSrralphs }
3977c478bd9Sstevel@tonic-gate } else {
3987c478bd9Sstevel@tonic-gate /* Handle non-SCSI drives here. */
399c6914c10Srralphs if (name == NULL) {
4007c478bd9Sstevel@tonic-gate (void) printf("unknown tape drive type (0x%x)\n",
401c6914c10Srralphs mdt.type);
402c6914c10Srralphs return (2);
4037c478bd9Sstevel@tonic-gate }
404c6914c10Srralphs (void) printf("%s tape drive:\n residual= %ld", name,
4057c478bd9Sstevel@tonic-gate bp->mt_resid);
406c6914c10Srralphs printreg(" ds", (ushort_t)bp->mt_dsreg, 0);
407c6914c10Srralphs printreg(" er", (ushort_t)bp->mt_erreg, 0);
4087c478bd9Sstevel@tonic-gate (void) putchar('\n');
4097c478bd9Sstevel@tonic-gate }
410c6914c10Srralphs return (0);
4117c478bd9Sstevel@tonic-gate }
4127c478bd9Sstevel@tonic-gate
4137c478bd9Sstevel@tonic-gate
4147c478bd9Sstevel@tonic-gate /*
4157c478bd9Sstevel@tonic-gate * Define SCSI sense key error messages.
4167c478bd9Sstevel@tonic-gate *
4177c478bd9Sstevel@tonic-gate * The first 16 sense keys are SCSI standard
4187c478bd9Sstevel@tonic-gate * sense keys. The keys after this are
4197c478bd9Sstevel@tonic-gate * Sun Specifice 'sense' keys- e.g., crap.
4207c478bd9Sstevel@tonic-gate */
4217c478bd9Sstevel@tonic-gate
422c6914c10Srralphs static char *sense_keys[] = {
4237c478bd9Sstevel@tonic-gate "No Additional Sense", /* 0x00 */
4247c478bd9Sstevel@tonic-gate "Soft Error", /* 0x01 */
4257c478bd9Sstevel@tonic-gate "Not Ready", /* 0x02 */
4267c478bd9Sstevel@tonic-gate "Media Error", /* 0x03 */
4277c478bd9Sstevel@tonic-gate "Hardware Error", /* 0x04 */
4287c478bd9Sstevel@tonic-gate "Illegal Request", /* 0x05 */
4297c478bd9Sstevel@tonic-gate "Unit Attention", /* 0x06 */
4307c478bd9Sstevel@tonic-gate "Write Protected", /* 0x07 */
4317c478bd9Sstevel@tonic-gate "Blank Check", /* 0x08 */
4327c478bd9Sstevel@tonic-gate "Vendor Unique", /* 0x09 */
4337c478bd9Sstevel@tonic-gate "Copy Aborted", /* 0x0a */
4347c478bd9Sstevel@tonic-gate "Aborted Command", /* 0x0b */
4357c478bd9Sstevel@tonic-gate "Equal Error", /* 0x0c */
4367c478bd9Sstevel@tonic-gate "Volume Overflow", /* 0x0d */
4377c478bd9Sstevel@tonic-gate "Miscompare Error", /* 0x0e */
438c6914c10Srralphs "Reserved", /* 0x0f */
439c6914c10Srralphs #ifdef sun
4407c478bd9Sstevel@tonic-gate "fatal", /* 0x10 */
4417c478bd9Sstevel@tonic-gate "timeout", /* 0x11 */
4427c478bd9Sstevel@tonic-gate "EOF", /* 0x12 */
4437c478bd9Sstevel@tonic-gate "EOT", /* 0x13 */
4447c478bd9Sstevel@tonic-gate "length error", /* 0x14 */
4457c478bd9Sstevel@tonic-gate "BOT", /* 0x15 */
4467c478bd9Sstevel@tonic-gate "wrong tape media", /* 0x16 */
447c6914c10Srralphs #endif
4487c478bd9Sstevel@tonic-gate };
4497c478bd9Sstevel@tonic-gate
4507c478bd9Sstevel@tonic-gate /*
4517c478bd9Sstevel@tonic-gate * Return the text string associated with the sense key value.
4527c478bd9Sstevel@tonic-gate */
4537c478bd9Sstevel@tonic-gate static char *
print_key(short key_code)4547c478bd9Sstevel@tonic-gate print_key(short key_code)
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate static char unknown[32];
457c6914c10Srralphs
458c6914c10Srralphs if ((key_code >= 0) &&
459c6914c10Srralphs (key_code < (sizeof (sense_keys) / sizeof (sense_keys[0])))) {
460c6914c10Srralphs return (sense_keys[key_code]);
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate
4637c478bd9Sstevel@tonic-gate (void) sprintf(unknown, "unknown sense key: 0x%x",
4647c478bd9Sstevel@tonic-gate (unsigned int) key_code);
4657c478bd9Sstevel@tonic-gate return (unknown);
4667c478bd9Sstevel@tonic-gate }
4677c478bd9Sstevel@tonic-gate
4687c478bd9Sstevel@tonic-gate
4697c478bd9Sstevel@tonic-gate /*
4707c478bd9Sstevel@tonic-gate * Print a register a la the %b format of the kernel's printf
4717c478bd9Sstevel@tonic-gate */
4727c478bd9Sstevel@tonic-gate static void
printreg(char * s,ushort_t v,char * bits)4737c478bd9Sstevel@tonic-gate printreg(char *s, ushort_t v, char *bits)
4747c478bd9Sstevel@tonic-gate {
4757c478bd9Sstevel@tonic-gate int i, any = 0;
4767c478bd9Sstevel@tonic-gate char c;
4777c478bd9Sstevel@tonic-gate
478c6914c10Srralphs if (bits && *bits == 8) {
4797c478bd9Sstevel@tonic-gate (void) printf("%s = %o", s, v);
480c6914c10Srralphs } else {
4817c478bd9Sstevel@tonic-gate (void) printf("%s = %x", s, v);
482c6914c10Srralphs }
4837c478bd9Sstevel@tonic-gate bits++;
4847c478bd9Sstevel@tonic-gate if (v && bits) {
4857c478bd9Sstevel@tonic-gate (void) putchar('<');
4867c478bd9Sstevel@tonic-gate while ((i = *bits++) != 0) {
4877c478bd9Sstevel@tonic-gate if (v & (1 << (i-1))) {
488c6914c10Srralphs if (any) {
4897c478bd9Sstevel@tonic-gate (void) putchar(',');
490c6914c10Srralphs }
4917c478bd9Sstevel@tonic-gate any = 1;
492c6914c10Srralphs for (; (c = *bits) > 32; bits++) {
4937c478bd9Sstevel@tonic-gate (void) putchar(c);
494c6914c10Srralphs }
495c6914c10Srralphs } else {
4967c478bd9Sstevel@tonic-gate for (; *bits > 32; bits++)
4977c478bd9Sstevel@tonic-gate ;
4987c478bd9Sstevel@tonic-gate }
499c6914c10Srralphs }
5007c478bd9Sstevel@tonic-gate (void) putchar('>');
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate }
503