xref: /aosp_15_r20/external/sg3_utils/examples/sg_simple4.c (revision 44704f698541f6367e81f991ef8bb54ccbf3fc18)
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 <sys/mman.h>
11*44704f69SBart Van Assche #include "sg_lib.h"
12*44704f69SBart Van Assche #include "sg_io_linux.h"
13*44704f69SBart Van Assche 
14*44704f69SBart Van Assche /* This is a simple program executing a SCSI INQUIRY command and a
15*44704f69SBart Van Assche    TEST UNIT READY command using the SCSI generic (sg) driver
16*44704f69SBart Van Assche    This variant shows mmap-ed IO being used to read the data returned
17*44704f69SBart Van Assche    by the INQUIRY command.
18*44704f69SBart Van Assche 
19*44704f69SBart Van Assche *  Copyright (C) 2001-2016 D. Gilbert
20*44704f69SBart Van Assche *  This program is free software; you can redistribute it and/or modify
21*44704f69SBart Van Assche *  it under the terms of the GNU General Public License as published by
22*44704f69SBart Van Assche *  the Free Software Foundation; either version 2, or (at your option)
23*44704f69SBart Van Assche *  any later version.
24*44704f69SBart Van Assche 
25*44704f69SBart Van Assche    Invocation: sg_simple4 [-x] <sg_device>
26*44704f69SBart Van Assche 
27*44704f69SBart Van Assche    Version 1.02 (20160528)
28*44704f69SBart Van Assche 
29*44704f69SBart Van Assche 6 byte INQUIRY command:
30*44704f69SBart Van Assche [0x12][   |lu][pg cde][res   ][al len][cntrl ]
31*44704f69SBart Van Assche 
32*44704f69SBart Van Assche 6 byte TEST UNIT READY command:
33*44704f69SBart Van Assche [0x00][   |lu][res   ][res   ][res   ][res   ]
34*44704f69SBart Van Assche 
35*44704f69SBart Van Assche */
36*44704f69SBart Van Assche 
37*44704f69SBart Van Assche #ifndef SG_FLAG_MMAP_IO
38*44704f69SBart Van Assche #define SG_FLAG_MMAP_IO 4
39*44704f69SBart Van Assche #endif  /* since /usr/include/scsi/sg.h doesn't know about this yet */
40*44704f69SBart Van Assche 
41*44704f69SBart Van Assche #define INQ_REPLY_LEN 96
42*44704f69SBart Van Assche #define INQ_CMD_LEN 6
43*44704f69SBart Van Assche #define TUR_CMD_LEN 6
44*44704f69SBart Van Assche 
45*44704f69SBart Van Assche #define EBUFF_SZ 256
46*44704f69SBart Van Assche 
main(int argc,char * argv[])47*44704f69SBart Van Assche int main(int argc, char * argv[])
48*44704f69SBart Van Assche {
49*44704f69SBart Van Assche     int sg_fd, k, ok;
50*44704f69SBart Van Assche     unsigned char inq_cdb[INQ_CMD_LEN] =
51*44704f69SBart Van Assche                                 {0x12, 0, 0, 0, INQ_REPLY_LEN, 0};
52*44704f69SBart Van Assche     unsigned char tur_cdb[TUR_CMD_LEN] =
53*44704f69SBart Van Assche                                 {0x00, 0, 0, 0, 0, 0};
54*44704f69SBart Van Assche     unsigned char * inqBuff;
55*44704f69SBart Van Assche     unsigned char * inqBuff2;
56*44704f69SBart Van Assche     sg_io_hdr_t io_hdr;
57*44704f69SBart Van Assche     char * file_name = 0;
58*44704f69SBart Van Assche     char ebuff[EBUFF_SZ];
59*44704f69SBart Van Assche     unsigned char sense_buffer[32];
60*44704f69SBart Van Assche     int do_extra = 0;
61*44704f69SBart Van Assche 
62*44704f69SBart Van Assche     for (k = 1; k < argc; ++k) {
63*44704f69SBart Van Assche         if (0 == memcmp("-x", argv[k], 2))
64*44704f69SBart Van Assche             do_extra = 1;
65*44704f69SBart Van Assche         else if (*argv[k] == '-') {
66*44704f69SBart Van Assche             printf("Unrecognized switch: %s\n", argv[k]);
67*44704f69SBart Van Assche             file_name = 0;
68*44704f69SBart Van Assche             break;
69*44704f69SBart Van Assche         }
70*44704f69SBart Van Assche         else if (0 == file_name)
71*44704f69SBart Van Assche             file_name = argv[k];
72*44704f69SBart Van Assche         else {
73*44704f69SBart Van Assche             printf("too many arguments\n");
74*44704f69SBart Van Assche             file_name = 0;
75*44704f69SBart Van Assche             break;
76*44704f69SBart Van Assche         }
77*44704f69SBart Van Assche     }
78*44704f69SBart Van Assche     if (0 == file_name) {
79*44704f69SBart Van Assche         printf("Usage: 'sg_simple4 [-x] <sg_device>'\n");
80*44704f69SBart Van Assche         return 1;
81*44704f69SBart Van Assche     }
82*44704f69SBart Van Assche 
83*44704f69SBart Van Assche     /* N.B. An access mode of O_RDWR is required for some SCSI commands */
84*44704f69SBart Van Assche     if ((sg_fd = open(file_name, O_RDWR)) < 0) {
85*44704f69SBart Van Assche         snprintf(ebuff, EBUFF_SZ,
86*44704f69SBart Van Assche                  "sg_simple4: error opening file: %s", file_name);
87*44704f69SBart Van Assche         perror(ebuff);
88*44704f69SBart Van Assche         return 1;
89*44704f69SBart Van Assche     }
90*44704f69SBart Van Assche     /* Just to be safe, check we have a new sg device by trying an ioctl */
91*44704f69SBart Van Assche     if ((ioctl(sg_fd, SG_GET_VERSION_NUM, &k) < 0) || (k < 30122)) {
92*44704f69SBart Van Assche         printf("sg_simple4: %s needs sg driver version >= 3.1.22\n",
93*44704f69SBart Van Assche                file_name);
94*44704f69SBart Van Assche         close(sg_fd);
95*44704f69SBart Van Assche         return 1;
96*44704f69SBart Van Assche     }
97*44704f69SBart Van Assche 
98*44704f69SBart Van Assche     /* since I know this program will only read from inqBuff then I use
99*44704f69SBart Van Assche        PROT_READ rather than PROT_READ | PROT_WRITE */
100*44704f69SBart Van Assche     inqBuff = (unsigned char *)mmap(NULL, 8000, PROT_READ | PROT_WRITE,
101*44704f69SBart Van Assche                                     MAP_SHARED, sg_fd, 0);
102*44704f69SBart Van Assche     if (MAP_FAILED == inqBuff) {
103*44704f69SBart Van Assche         snprintf(ebuff, EBUFF_SZ, "sg_simple4: error using mmap() on "
104*44704f69SBart Van Assche                  "file: %s", file_name);
105*44704f69SBart Van Assche         perror(ebuff);
106*44704f69SBart Van Assche         return 1;
107*44704f69SBart Van Assche     }
108*44704f69SBart Van Assche     if (inqBuff[0])
109*44704f69SBart Van Assche         printf("non-null char at inqBuff[0]\n");
110*44704f69SBart Van Assche     if (inqBuff[5000])
111*44704f69SBart Van Assche         printf("non-null char at inqBuff[5000]\n");
112*44704f69SBart Van Assche 
113*44704f69SBart Van Assche     /* Prepare INQUIRY command */
114*44704f69SBart Van Assche     memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
115*44704f69SBart Van Assche     io_hdr.interface_id = 'S';
116*44704f69SBart Van Assche     io_hdr.cmd_len = sizeof(inq_cdb);
117*44704f69SBart Van Assche     /* io_hdr.iovec_count = 0; */  /* memset takes care of this */
118*44704f69SBart Van Assche     io_hdr.mx_sb_len = sizeof(sense_buffer);
119*44704f69SBart Van Assche     io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
120*44704f69SBart Van Assche     io_hdr.dxfer_len = INQ_REPLY_LEN;
121*44704f69SBart Van Assche     /* io_hdr.dxferp = inqBuff; // ignored in mmap-ed IO */
122*44704f69SBart Van Assche     io_hdr.cmdp = inq_cdb;
123*44704f69SBart Van Assche     io_hdr.sbp = sense_buffer;
124*44704f69SBart Van Assche     io_hdr.timeout = 20000;     /* 20000 millisecs == 20 seconds */
125*44704f69SBart Van Assche     io_hdr.flags = SG_FLAG_MMAP_IO;
126*44704f69SBart Van Assche     /* io_hdr.pack_id = 0; */
127*44704f69SBart Van Assche     /* io_hdr.usr_ptr = NULL; */
128*44704f69SBart Van Assche 
129*44704f69SBart Van Assche     if (ioctl(sg_fd, SG_IO, &io_hdr) < 0) {
130*44704f69SBart Van Assche         perror("sg_simple4: Inquiry SG_IO ioctl error");
131*44704f69SBart Van Assche         close(sg_fd);
132*44704f69SBart Van Assche         return 1;
133*44704f69SBart Van Assche     }
134*44704f69SBart Van Assche 
135*44704f69SBart Van Assche     /* now for the error processing */
136*44704f69SBart Van Assche     ok = 0;
137*44704f69SBart Van Assche     switch (sg_err_category3(&io_hdr)) {
138*44704f69SBart Van Assche     case SG_LIB_CAT_CLEAN:
139*44704f69SBart Van Assche         ok = 1;
140*44704f69SBart Van Assche         break;
141*44704f69SBart Van Assche     case SG_LIB_CAT_RECOVERED:
142*44704f69SBart Van Assche         printf("Recovered error on INQUIRY, continuing\n");
143*44704f69SBart Van Assche         ok = 1;
144*44704f69SBart Van Assche         break;
145*44704f69SBart Van Assche     default: /* won't bother decoding other categories */
146*44704f69SBart Van Assche         sg_chk_n_print3("INQUIRY command error", &io_hdr, 1);
147*44704f69SBart Van Assche         break;
148*44704f69SBart Van Assche     }
149*44704f69SBart Van Assche 
150*44704f69SBart Van Assche     if (ok) { /* output result if it is available */
151*44704f69SBart Van Assche         char * p = (char *)inqBuff;
152*44704f69SBart Van Assche         int f = (int)*(p + 7);
153*44704f69SBart Van Assche         printf("Some of the INQUIRY command's results:\n");
154*44704f69SBart Van Assche         printf("    %.8s  %.16s  %.4s  ", p + 8, p + 16, p + 32);
155*44704f69SBart Van Assche         printf("[wide=%d sync=%d cmdque=%d sftre=%d]\n",
156*44704f69SBart Van Assche                !!(f & 0x20), !!(f & 0x10), !!(f & 2), !!(f & 1));
157*44704f69SBart Van Assche         /* Extra info, not necessary to look at */
158*44704f69SBart Van Assche         if (do_extra)
159*44704f69SBart Van Assche             printf("INQUIRY duration=%u millisecs, resid=%d, msg_status=%d\n",
160*44704f69SBart Van Assche                    io_hdr.duration, io_hdr.resid, (int)io_hdr.msg_status);
161*44704f69SBart Van Assche     }
162*44704f69SBart Van Assche 
163*44704f69SBart Van Assche 
164*44704f69SBart Van Assche     /* Prepare TEST UNIT READY command */
165*44704f69SBart Van Assche     memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
166*44704f69SBart Van Assche     io_hdr.interface_id = 'S';
167*44704f69SBart Van Assche     io_hdr.cmd_len = sizeof(tur_cdb);
168*44704f69SBart Van Assche     io_hdr.mx_sb_len = sizeof(sense_buffer);
169*44704f69SBart Van Assche     io_hdr.dxfer_direction = SG_DXFER_NONE;
170*44704f69SBart Van Assche     io_hdr.cmdp = tur_cdb;
171*44704f69SBart Van Assche     io_hdr.sbp = sense_buffer;
172*44704f69SBart Van Assche     io_hdr.timeout = 20000;     /* 20000 millisecs == 20 seconds */
173*44704f69SBart Van Assche 
174*44704f69SBart Van Assche     if (ioctl(sg_fd, SG_IO, &io_hdr) < 0) {
175*44704f69SBart Van Assche         perror("sg_simple4: Test Unit Ready SG_IO ioctl error");
176*44704f69SBart Van Assche         close(sg_fd);
177*44704f69SBart Van Assche         return 1;
178*44704f69SBart Van Assche     }
179*44704f69SBart Van Assche 
180*44704f69SBart Van Assche     /* now for the error processing */
181*44704f69SBart Van Assche     ok = 0;
182*44704f69SBart Van Assche     switch (sg_err_category3(&io_hdr)) {
183*44704f69SBart Van Assche     case SG_LIB_CAT_CLEAN:
184*44704f69SBart Van Assche         ok = 1;
185*44704f69SBart Van Assche         break;
186*44704f69SBart Van Assche     case SG_LIB_CAT_RECOVERED:
187*44704f69SBart Van Assche         printf("Recovered error on Test Unit Ready, continuing\n");
188*44704f69SBart Van Assche         ok = 1;
189*44704f69SBart Van Assche         break;
190*44704f69SBart Van Assche     default: /* won't bother decoding other categories */
191*44704f69SBart Van Assche         sg_chk_n_print3("Test Unit Ready command error", &io_hdr, 1);
192*44704f69SBart Van Assche         break;
193*44704f69SBart Van Assche     }
194*44704f69SBart Van Assche 
195*44704f69SBart Van Assche     if (ok)
196*44704f69SBart Van Assche         printf("Test Unit Ready successful so unit is ready!\n");
197*44704f69SBart Van Assche     else
198*44704f69SBart Van Assche         printf("Test Unit Ready failed so unit may _not_ be ready!\n");
199*44704f69SBart Van Assche 
200*44704f69SBart Van Assche     if (do_extra)
201*44704f69SBart Van Assche         printf("TEST UNIT READY duration=%u millisecs, resid=%d, "
202*44704f69SBart Van Assche                "msg_status=%d\n",
203*44704f69SBart Van Assche                io_hdr.duration, io_hdr.resid, (int)io_hdr.msg_status);
204*44704f69SBart Van Assche 
205*44704f69SBart Van Assche     /* munmap(inqBuff, 8000); */
206*44704f69SBart Van Assche     /* could call munmap(inqBuff, INQ_REPLY_LEN) here but following close()
207*44704f69SBart Van Assche        causes this too happen anyway */
208*44704f69SBart Van Assche #if 1
209*44704f69SBart Van Assche     inqBuff2 = (unsigned char *)mmap(NULL, 8000, PROT_READ | PROT_WRITE,
210*44704f69SBart Van Assche                                      MAP_SHARED, sg_fd, 0);
211*44704f69SBart Van Assche     if (MAP_FAILED == inqBuff2) {
212*44704f69SBart Van Assche         snprintf(ebuff, EBUFF_SZ, "sg_simple4: error using mmap() 2 on "
213*44704f69SBart Van Assche                  "file: %s", file_name);
214*44704f69SBart Van Assche         perror(ebuff);
215*44704f69SBart Van Assche         return 1;
216*44704f69SBart Van Assche     }
217*44704f69SBart Van Assche     if (inqBuff2[0])
218*44704f69SBart Van Assche         printf("non-null char at inqBuff2[0]\n");
219*44704f69SBart Van Assche     if (inqBuff2[5000])
220*44704f69SBart Van Assche         printf("non-null char at inqBuff2[5000]\n");
221*44704f69SBart Van Assche     {
222*44704f69SBart Van Assche         pid_t pid;
223*44704f69SBart Van Assche         pid = fork();
224*44704f69SBart Van Assche         if (pid) {
225*44704f69SBart Van Assche             inqBuff2[5000] = 33;
226*44704f69SBart Van Assche             munmap(inqBuff, 8000);
227*44704f69SBart Van Assche             sleep(3);
228*44704f69SBart Van Assche         }
229*44704f69SBart Van Assche         else {
230*44704f69SBart Van Assche             inqBuff[5000] = 0xaa;
231*44704f69SBart Van Assche             munmap(inqBuff, 8000);
232*44704f69SBart Van Assche             sleep(1);
233*44704f69SBart Van Assche         }
234*44704f69SBart Van Assche     }
235*44704f69SBart Van Assche #endif
236*44704f69SBart Van Assche     close(sg_fd);
237*44704f69SBart Van Assche     return 0;
238*44704f69SBart Van Assche }
239