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