1*44704f69SBart Van Assche #include <unistd.h>
2*44704f69SBart Van Assche #include <fcntl.h>
3*44704f69SBart Van Assche #include <stdio.h>
4*44704f69SBart Van Assche #include <stdlib.h>
5*44704f69SBart Van Assche #include <string.h>
6*44704f69SBart Van Assche #include <errno.h>
7*44704f69SBart Van Assche #include <sys/ioctl.h>
8*44704f69SBart Van Assche #include <sys/types.h>
9*44704f69SBart Van Assche #include <sys/stat.h>
10*44704f69SBart Van Assche #include "sg_linux_inc.h"
11*44704f69SBart Van Assche
12*44704f69SBart Van Assche /* This is a simple program executing a SCSI INQUIRY command and a
13*44704f69SBart Van Assche TEST UNIT READY command using the SCSI generic (sg) driver.
14*44704f69SBart Van Assche There is another variant of this program called "sg_simple1"
15*44704f69SBart Van Assche which includes the sg_lib.h header and logic and so has more
16*44704f69SBart Van Assche advanced error processing.
17*44704f69SBart Van Assche This version demonstrates the "sg3" interface.
18*44704f69SBart Van Assche In the lk 2.6 series devices nodes such as /dev/sda also support
19*44704f69SBart Van Assche the SG_IO ioctl.
20*44704f69SBart Van Assche
21*44704f69SBart Van Assche * Copyright (C) 1999-2016 D. Gilbert
22*44704f69SBart Van Assche * This program is free software; you can redistribute it and/or modify
23*44704f69SBart Van Assche * it under the terms of the GNU General Public License as published by
24*44704f69SBart Van Assche * the Free Software Foundation; either version 2, or (at your option)
25*44704f69SBart Van Assche * any later version.
26*44704f69SBart Van Assche
27*44704f69SBart Van Assche Invocation: sg_simple2 [-x] <scsi_device>
28*44704f69SBart Van Assche
29*44704f69SBart Van Assche Version 03.59 (20160528)
30*44704f69SBart Van Assche
31*44704f69SBart Van Assche 6 byte INQUIRY command:
32*44704f69SBart Van Assche [0x12][ |lu][pg cde][res ][al len][cntrl ]
33*44704f69SBart Van Assche
34*44704f69SBart Van Assche 6 byte TEST UNIT READY command:
35*44704f69SBart Van Assche [0x00][ |lu][res ][res ][res ][res ]
36*44704f69SBart Van Assche
37*44704f69SBart Van Assche */
38*44704f69SBart Van Assche
39*44704f69SBart Van Assche #define INQ_REPLY_LEN 96 /* logic assumes >= sizeof(inq_cdb) */
40*44704f69SBart Van Assche #define INQ_CMD_LEN 6
41*44704f69SBart Van Assche #define TUR_CMD_LEN 6
42*44704f69SBart Van Assche
43*44704f69SBart Van Assche #define EBUFF_SZ 256
44*44704f69SBart Van Assche
45*44704f69SBart Van Assche
main(int argc,char * argv[])46*44704f69SBart Van Assche int main(int argc, char * argv[])
47*44704f69SBart Van Assche {
48*44704f69SBart Van Assche int sg_fd, k;
49*44704f69SBart Van Assche unsigned char inq_cdb[INQ_CMD_LEN] =
50*44704f69SBart Van Assche {0x12, 0, 0, 0, INQ_REPLY_LEN, 0};
51*44704f69SBart Van Assche unsigned char tur_cdb[TUR_CMD_LEN] =
52*44704f69SBart Van Assche {0x00, 0, 0, 0, 0, 0};
53*44704f69SBart Van Assche unsigned char inqBuff[INQ_REPLY_LEN];
54*44704f69SBart Van Assche sg_io_hdr_t io_hdr;
55*44704f69SBart Van Assche char * file_name = 0;
56*44704f69SBart Van Assche char ebuff[EBUFF_SZ];
57*44704f69SBart Van Assche unsigned char sense_buffer[32];
58*44704f69SBart Van Assche int do_extra = 0;
59*44704f69SBart Van Assche
60*44704f69SBart Van Assche for (k = 1; k < argc; ++k) {
61*44704f69SBart Van Assche if (0 == memcmp("-x", argv[k], 2))
62*44704f69SBart Van Assche do_extra = 1;
63*44704f69SBart Van Assche else if (*argv[k] == '-') {
64*44704f69SBart Van Assche printf("Unrecognized switch: %s\n", argv[k]);
65*44704f69SBart Van Assche file_name = 0;
66*44704f69SBart Van Assche break;
67*44704f69SBart Van Assche }
68*44704f69SBart Van Assche else if (0 == file_name)
69*44704f69SBart Van Assche file_name = argv[k];
70*44704f69SBart Van Assche else {
71*44704f69SBart Van Assche printf("too many arguments\n");
72*44704f69SBart Van Assche file_name = 0;
73*44704f69SBart Van Assche break;
74*44704f69SBart Van Assche }
75*44704f69SBart Van Assche }
76*44704f69SBart Van Assche if (0 == file_name) {
77*44704f69SBart Van Assche printf("Usage: 'sg_simple2 [-x] <sg_device>'\n");
78*44704f69SBart Van Assche return 1;
79*44704f69SBart Van Assche }
80*44704f69SBart Van Assche
81*44704f69SBart Van Assche /* N.B. An access mode of O_RDWR is required for some SCSI commands */
82*44704f69SBart Van Assche if ((sg_fd = open(file_name, O_RDONLY)) < 0) {
83*44704f69SBart Van Assche snprintf(ebuff, EBUFF_SZ,
84*44704f69SBart Van Assche "sg_simple2: error opening file: %s", file_name);
85*44704f69SBart Van Assche perror(ebuff);
86*44704f69SBart Van Assche return 1;
87*44704f69SBart Van Assche }
88*44704f69SBart Van Assche /* Just to be safe, check we have a new sg device by trying an ioctl */
89*44704f69SBart Van Assche if ((ioctl(sg_fd, SG_GET_VERSION_NUM, &k) < 0) || (k < 30000)) {
90*44704f69SBart Van Assche printf("sg_simple2: %s doesn't seem to be an new sg device\n",
91*44704f69SBart Van Assche file_name);
92*44704f69SBart Van Assche close(sg_fd);
93*44704f69SBart Van Assche return 1;
94*44704f69SBart Van Assche }
95*44704f69SBart Van Assche
96*44704f69SBart Van Assche /* Prepare INQUIRY command */
97*44704f69SBart Van Assche memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
98*44704f69SBart Van Assche io_hdr.interface_id = 'S';
99*44704f69SBart Van Assche io_hdr.cmd_len = sizeof(inq_cdb);
100*44704f69SBart Van Assche /* io_hdr.iovec_count = 0; */ /* memset takes care of this */
101*44704f69SBart Van Assche io_hdr.mx_sb_len = sizeof(sense_buffer);
102*44704f69SBart Van Assche io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
103*44704f69SBart Van Assche io_hdr.dxfer_len = INQ_REPLY_LEN;
104*44704f69SBart Van Assche io_hdr.dxferp = inqBuff;
105*44704f69SBart Van Assche io_hdr.cmdp = inq_cdb;
106*44704f69SBart Van Assche io_hdr.sbp = sense_buffer;
107*44704f69SBart Van Assche io_hdr.timeout = 20000; /* 20000 millisecs == 20 seconds */
108*44704f69SBart Van Assche /* io_hdr.flags = 0; */ /* take defaults: indirect IO, etc */
109*44704f69SBart Van Assche /* io_hdr.pack_id = 0; */
110*44704f69SBart Van Assche /* io_hdr.usr_ptr = NULL; */
111*44704f69SBart Van Assche
112*44704f69SBart Van Assche if (ioctl(sg_fd, SG_IO, &io_hdr) < 0) {
113*44704f69SBart Van Assche perror("sg_simple2: Inquiry SG_IO ioctl error");
114*44704f69SBart Van Assche close(sg_fd);
115*44704f69SBart Van Assche return 1;
116*44704f69SBart Van Assche }
117*44704f69SBart Van Assche
118*44704f69SBart Van Assche /* now for the error processing */
119*44704f69SBart Van Assche if ((io_hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK) {
120*44704f69SBart Van Assche if (io_hdr.sb_len_wr > 0) {
121*44704f69SBart Van Assche printf("INQUIRY sense data: ");
122*44704f69SBart Van Assche for (k = 0; k < io_hdr.sb_len_wr; ++k) {
123*44704f69SBart Van Assche if ((k > 0) && (0 == (k % 10)))
124*44704f69SBart Van Assche printf("\n ");
125*44704f69SBart Van Assche printf("0x%02x ", sense_buffer[k]);
126*44704f69SBart Van Assche }
127*44704f69SBart Van Assche printf("\n");
128*44704f69SBart Van Assche }
129*44704f69SBart Van Assche if (io_hdr.masked_status)
130*44704f69SBart Van Assche printf("INQUIRY SCSI status=0x%x\n", io_hdr.status);
131*44704f69SBart Van Assche if (io_hdr.host_status)
132*44704f69SBart Van Assche printf("INQUIRY host_status=0x%x\n", io_hdr.host_status);
133*44704f69SBart Van Assche if (io_hdr.driver_status)
134*44704f69SBart Van Assche printf("INQUIRY driver_status=0x%x\n", io_hdr.driver_status);
135*44704f69SBart Van Assche }
136*44704f69SBart Van Assche else { /* output result if it is available */
137*44704f69SBart Van Assche char * p = (char *)inqBuff;
138*44704f69SBart Van Assche int f = (int)*(p + 7);
139*44704f69SBart Van Assche printf("Some of the INQUIRY command's results:\n");
140*44704f69SBart Van Assche printf(" %.8s %.16s %.4s ", p + 8, p + 16, p + 32);
141*44704f69SBart Van Assche printf("[wide=%d sync=%d cmdque=%d sftre=%d]\n",
142*44704f69SBart Van Assche !!(f & 0x20), !!(f & 0x10), !!(f & 2), !!(f & 1));
143*44704f69SBart Van Assche }
144*44704f69SBart Van Assche /* Extra info, not necessary to look at */
145*44704f69SBart Van Assche if (do_extra)
146*44704f69SBart Van Assche printf("INQUIRY duration=%u millisecs, resid=%d, msg_status=%d\n",
147*44704f69SBart Van Assche io_hdr.duration, io_hdr.resid, (int)io_hdr.msg_status);
148*44704f69SBart Van Assche
149*44704f69SBart Van Assche /* Prepare TEST UNIT READY command */
150*44704f69SBart Van Assche memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
151*44704f69SBart Van Assche io_hdr.interface_id = 'S';
152*44704f69SBart Van Assche io_hdr.cmd_len = sizeof(tur_cdb);
153*44704f69SBart Van Assche io_hdr.mx_sb_len = sizeof(sense_buffer);
154*44704f69SBart Van Assche io_hdr.dxfer_direction = SG_DXFER_NONE;
155*44704f69SBart Van Assche io_hdr.cmdp = tur_cdb;
156*44704f69SBart Van Assche io_hdr.sbp = sense_buffer;
157*44704f69SBart Van Assche io_hdr.timeout = 20000; /* 20000 millisecs == 20 seconds */
158*44704f69SBart Van Assche
159*44704f69SBart Van Assche if (ioctl(sg_fd, SG_IO, &io_hdr) < 0) {
160*44704f69SBart Van Assche perror("sg_simple2: Test Unit Ready SG_IO ioctl error");
161*44704f69SBart Van Assche close(sg_fd);
162*44704f69SBart Van Assche return 1;
163*44704f69SBart Van Assche }
164*44704f69SBart Van Assche
165*44704f69SBart Van Assche /* now for the error processing */
166*44704f69SBart Van Assche if ((io_hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK) {
167*44704f69SBart Van Assche if (io_hdr.sb_len_wr > 0) {
168*44704f69SBart Van Assche printf("TEST UNIT READY sense data: ");
169*44704f69SBart Van Assche for (k = 0; k < io_hdr.sb_len_wr; ++k) {
170*44704f69SBart Van Assche if ((k > 0) && (0 == (k % 10)))
171*44704f69SBart Van Assche printf("\n ");
172*44704f69SBart Van Assche printf("0x%02x ", sense_buffer[k]);
173*44704f69SBart Van Assche }
174*44704f69SBart Van Assche printf("\n");
175*44704f69SBart Van Assche }
176*44704f69SBart Van Assche else if (io_hdr.masked_status)
177*44704f69SBart Van Assche printf("TEST UNIT READY SCSI status=0x%x\n", io_hdr.status);
178*44704f69SBart Van Assche else if (io_hdr.host_status)
179*44704f69SBart Van Assche printf("TEST UNIT READY host_status=0x%x\n", io_hdr.host_status);
180*44704f69SBart Van Assche else if (io_hdr.driver_status)
181*44704f69SBart Van Assche printf("TEST UNIT READY driver_status=0x%x\n",
182*44704f69SBart Van Assche io_hdr.driver_status);
183*44704f69SBart Van Assche else
184*44704f69SBart Van Assche printf("TEST UNIT READY unexpected error\n");
185*44704f69SBart Van Assche printf("Test Unit Ready failed so unit may _not_ be ready!\n");
186*44704f69SBart Van Assche }
187*44704f69SBart Van Assche else
188*44704f69SBart Van Assche printf("Test Unit Ready successful so unit is ready!\n");
189*44704f69SBart Van Assche /* Extra info, not necessary to look at */
190*44704f69SBart Van Assche if (do_extra)
191*44704f69SBart Van Assche printf("TEST UNIT READY duration=%u millisecs, resid=%d, "
192*44704f69SBart Van Assche "msg_status=%d\n", io_hdr.duration, io_hdr.resid,
193*44704f69SBart Van Assche (int)io_hdr.msg_status);
194*44704f69SBart Van Assche
195*44704f69SBart Van Assche close(sg_fd);
196*44704f69SBart Van Assche return 0;
197*44704f69SBart Van Assche }
198