xref: /aosp_15_r20/external/sg3_utils/testing/sg_queue_tst.c (revision 44704f698541f6367e81f991ef8bb54ccbf3fc18)
1*44704f69SBart Van Assche /*
2*44704f69SBart Van Assche  * Copyright (C) 2010-2019 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  *
11*44704f69SBart Van Assche  * This program was used to test SCSI mid level queue ordering.
12*44704f69SBart Van Assche  * The default behaviour is to "queue at head" which is useful for
13*44704f69SBart Van Assche  * error processing but not for streaming READ and WRITE commands.
14*44704f69SBart Van Assche  *
15*44704f69SBart Van Assche  * Invocation: sg_queue_tst [-l=Q_LEN] [-t] <sg_device>
16*44704f69SBart Van Assche  *      -t      queue at tail
17*44704f69SBart Van Assche  *
18*44704f69SBart Van Assche  * Version 0.96 (20190128)
19*44704f69SBart Van Assche  */
20*44704f69SBart Van Assche 
21*44704f69SBart Van Assche #include <unistd.h>
22*44704f69SBart Van Assche #include <fcntl.h>
23*44704f69SBart Van Assche #include <stdio.h>
24*44704f69SBart Van Assche #include <stdlib.h>
25*44704f69SBart Van Assche #include <stdbool.h>
26*44704f69SBart Van Assche #include <stdint.h>
27*44704f69SBart Van Assche #include <stdarg.h>
28*44704f69SBart Van Assche #include <string.h>
29*44704f69SBart Van Assche #include <errno.h>
30*44704f69SBart Van Assche #include <sys/ioctl.h>
31*44704f69SBart Van Assche #include <sys/types.h>
32*44704f69SBart Van Assche #include <sys/stat.h>
33*44704f69SBart Van Assche 
34*44704f69SBart Van Assche #ifndef HAVE_LINUX_SG_V4_HDR
35*44704f69SBart Van Assche 
36*44704f69SBart Van Assche /* Kernel uapi header contain __user decorations on user space pointers
37*44704f69SBart Van Assche  * to indicate they are unsafe in the kernel space. However glibc takes
38*44704f69SBart Van Assche  * all those __user decorations out from headers in /usr/include/linux .
39*44704f69SBart Van Assche  * So to stop compile errors when directly importing include/uapi/scsi/sg.h
40*44704f69SBart Van Assche  * undef __user before doing that include. */
41*44704f69SBart Van Assche #define __user
42*44704f69SBart Van Assche 
43*44704f69SBart Van Assche /* Want to block the original sg.h header from also being included. That
44*44704f69SBart Van Assche  * causes lots of multiple definition errors. This will only work if this
45*44704f69SBart Van Assche  * header is included _before_ the original sg.h header.  */
46*44704f69SBart Van Assche #define _SCSI_GENERIC_H         /* original kernel header guard */
47*44704f69SBart Van Assche #define _SCSI_SG_H              /* glibc header guard */
48*44704f69SBart Van Assche 
49*44704f69SBart Van Assche #include "uapi_sg.h"    /* local copy of include/uapi/scsi/sg.h */
50*44704f69SBart Van Assche 
51*44704f69SBart Van Assche #else
52*44704f69SBart Van Assche #define __user
53*44704f69SBart Van Assche #endif  /* end of: ifndef HAVE_LINUX_SG_V4_HDR */
54*44704f69SBart Van Assche 
55*44704f69SBart Van Assche #include "sg_lib.h"
56*44704f69SBart Van Assche #include "sg_io_linux.h"
57*44704f69SBart Van Assche #include "sg_linux_inc.h"
58*44704f69SBart Van Assche 
59*44704f69SBart Van Assche 
60*44704f69SBart Van Assche 
61*44704f69SBart Van Assche #define INQ_REPLY_LEN 96
62*44704f69SBart Van Assche #define INQ_CMD_LEN 6
63*44704f69SBart Van Assche #define SDIAG_CMD_LEN 6
64*44704f69SBart Van Assche #define SENSE_BUFFER_LEN 96
65*44704f69SBart Van Assche 
66*44704f69SBart Van Assche #define EBUFF_SZ 256
67*44704f69SBart Van Assche 
68*44704f69SBart Van Assche #ifndef SG_FLAG_Q_AT_TAIL
69*44704f69SBart Van Assche #define SG_FLAG_Q_AT_TAIL 0x10
70*44704f69SBart Van Assche #endif
71*44704f69SBart Van Assche 
72*44704f69SBart Van Assche #ifndef SG_FLAG_Q_AT_HEAD
73*44704f69SBart Van Assche #define SG_FLAG_Q_AT_HEAD 0x20
74*44704f69SBart Van Assche #endif
75*44704f69SBart Van Assche 
76*44704f69SBart Van Assche #define DEF_Q_LEN 16    /* max in sg v3 and earlier */
77*44704f69SBart Van Assche #define MAX_Q_LEN 256
78*44704f69SBart Van Assche 
79*44704f69SBart Van Assche static void
set_nanosecs(int sg_fd)80*44704f69SBart Van Assche set_nanosecs(int sg_fd)
81*44704f69SBart Van Assche {
82*44704f69SBart Van Assche     struct sg_extended_info sei;
83*44704f69SBart Van Assche     struct sg_extended_info * seip;
84*44704f69SBart Van Assche 
85*44704f69SBart Van Assche     seip = &sei;
86*44704f69SBart Van Assche     memset(seip, 0, sizeof(*seip));
87*44704f69SBart Van Assche     seip->sei_wr_mask |= SG_SEIM_CTL_FLAGS;
88*44704f69SBart Van Assche     seip->sei_rd_mask |= SG_SEIM_CTL_FLAGS; /* this or previous optional */
89*44704f69SBart Van Assche     seip->ctl_flags_wr_mask |= SG_CTL_FLAGM_TIME_IN_NS;
90*44704f69SBart Van Assche     seip->ctl_flags |= SG_CTL_FLAGM_TIME_IN_NS;
91*44704f69SBart Van Assche 
92*44704f69SBart Van Assche     if (ioctl(sg_fd, SG_SET_GET_EXTENDED, seip) < 0) {
93*44704f69SBart Van Assche         fprintf(stderr, "ioctl(SG_SET_GET_EXTENDED) failed, errno=%d %s\n",
94*44704f69SBart Van Assche                 errno, strerror(errno));
95*44704f69SBart Van Assche     }
96*44704f69SBart Van Assche }
97*44704f69SBart Van Assche 
98*44704f69SBart Van Assche 
main(int argc,char * argv[])99*44704f69SBart Van Assche int main(int argc, char * argv[])
100*44704f69SBart Van Assche {
101*44704f69SBart Van Assche     bool q_at_tail = false;
102*44704f69SBart Van Assche     bool dur_in_nanosecs = false;
103*44704f69SBart Van Assche     int sg_fd, k, ok;
104*44704f69SBart Van Assche     uint8_t inq_cdb[INQ_CMD_LEN] =
105*44704f69SBart Van Assche                                 {0x12, 0, 0, 0, INQ_REPLY_LEN, 0};
106*44704f69SBart Van Assche     uint8_t sdiag_cdb[SDIAG_CMD_LEN] =
107*44704f69SBart Van Assche                                 {0x1d, 0x10 /* PF */, 0, 0, 0, 0};
108*44704f69SBart Van Assche     uint8_t inqBuff[MAX_Q_LEN][INQ_REPLY_LEN];
109*44704f69SBart Van Assche     sg_io_hdr_t io_hdr[MAX_Q_LEN];
110*44704f69SBart Van Assche     sg_io_hdr_t rio_hdr;
111*44704f69SBart Van Assche     char * file_name = 0;
112*44704f69SBart Van Assche     char ebuff[EBUFF_SZ];
113*44704f69SBart Van Assche     uint8_t sense_buffer[MAX_Q_LEN][SENSE_BUFFER_LEN] SG_C_CPP_ZERO_INIT;
114*44704f69SBart Van Assche     int q_len = DEF_Q_LEN;
115*44704f69SBart Van Assche 
116*44704f69SBart Van Assche     for (k = 1; k < argc; ++k) {
117*44704f69SBart Van Assche         if (0 == memcmp("-n", argv[k], 2))
118*44704f69SBart Van Assche             dur_in_nanosecs = true;
119*44704f69SBart Van Assche         else if (0 == memcmp("-t", argv[k], 2))
120*44704f69SBart Van Assche             q_at_tail = true;
121*44704f69SBart Van Assche         else if (0 == memcmp("-l=", argv[k], 3)) {
122*44704f69SBart Van Assche             q_len = atoi(argv[k] + 3);
123*44704f69SBart Van Assche             if ((q_len > 511) || (q_len < 1)) {
124*44704f69SBart Van Assche                 printf("Expect -l= to take a number (q length) between 1 "
125*44704f69SBart Van Assche                        "and 511\n");
126*44704f69SBart Van Assche                 file_name = 0;
127*44704f69SBart Van Assche                 break;
128*44704f69SBart Van Assche             }
129*44704f69SBart Van Assche 
130*44704f69SBart Van Assche         } else if (*argv[k] == '-') {
131*44704f69SBart Van Assche             printf("Unrecognized switch: %s\n", argv[k]);
132*44704f69SBart Van Assche             file_name = 0;
133*44704f69SBart Van Assche             break;
134*44704f69SBart Van Assche         }
135*44704f69SBart Van Assche         else if (0 == file_name)
136*44704f69SBart Van Assche             file_name = argv[k];
137*44704f69SBart Van Assche         else {
138*44704f69SBart Van Assche             printf("too many arguments\n");
139*44704f69SBart Van Assche             file_name = 0;
140*44704f69SBart Van Assche             break;
141*44704f69SBart Van Assche         }
142*44704f69SBart Van Assche     }
143*44704f69SBart Van Assche     if (0 == file_name) {
144*44704f69SBart Van Assche         printf("Usage: 'sg_queue_tst [-l=Q_LEN] [-n] [-t] <sg_device>'\n"
145*44704f69SBart Van Assche                "where:\n"
146*44704f69SBart Van Assche                "      -l=Q_LEN    queue length, between 1 and 511 "
147*44704f69SBart Van Assche                "(def: 16)\n"
148*44704f69SBart Van Assche                "      -n    duration in nanosecs (def: milliseconds)\n"
149*44704f69SBart Van Assche                "      -t    queue_at_tail (def: q_at_head)\n");
150*44704f69SBart Van Assche         return 1;
151*44704f69SBart Van Assche     }
152*44704f69SBart Van Assche 
153*44704f69SBart Van Assche     /* An access mode of O_RDWR is required for write()/read() interface */
154*44704f69SBart Van Assche     if ((sg_fd = open(file_name, O_RDWR)) < 0) {
155*44704f69SBart Van Assche         snprintf(ebuff, EBUFF_SZ,
156*44704f69SBart Van Assche                  "sg_queue_tst: error opening file: %s", file_name);
157*44704f69SBart Van Assche         perror(ebuff);
158*44704f69SBart Van Assche         return 1;
159*44704f69SBart Van Assche     }
160*44704f69SBart Van Assche     if (dur_in_nanosecs)
161*44704f69SBart Van Assche         set_nanosecs(sg_fd);
162*44704f69SBart Van Assche 
163*44704f69SBart Van Assche     for (k = 0; k < q_len; ++k) {
164*44704f69SBart Van Assche         /* Prepare INQUIRY command */
165*44704f69SBart Van Assche         memset(&io_hdr[k], 0, sizeof(sg_io_hdr_t));
166*44704f69SBart Van Assche         io_hdr[k].interface_id = 'S';
167*44704f69SBart Van Assche         /* io_hdr[k].iovec_count = 0; */  /* memset takes care of this */
168*44704f69SBart Van Assche         io_hdr[k].mx_sb_len = (uint8_t)sizeof(sense_buffer);
169*44704f69SBart Van Assche         if (0 == (k % 3)) {
170*44704f69SBart Van Assche             io_hdr[k].cmd_len = sizeof(sdiag_cdb);
171*44704f69SBart Van Assche             io_hdr[k].cmdp = sdiag_cdb;
172*44704f69SBart Van Assche             io_hdr[k].dxfer_direction = SG_DXFER_NONE;
173*44704f69SBart Van Assche         } else {
174*44704f69SBart Van Assche             io_hdr[k].cmd_len = sizeof(inq_cdb);
175*44704f69SBart Van Assche             io_hdr[k].cmdp = inq_cdb;
176*44704f69SBart Van Assche             io_hdr[k].dxfer_direction = SG_DXFER_FROM_DEV;
177*44704f69SBart Van Assche             io_hdr[k].dxfer_len = INQ_REPLY_LEN;
178*44704f69SBart Van Assche             io_hdr[k].dxferp = inqBuff[k];
179*44704f69SBart Van Assche         }
180*44704f69SBart Van Assche         io_hdr[k].sbp = sense_buffer[k];
181*44704f69SBart Van Assche         io_hdr[k].mx_sb_len = SENSE_BUFFER_LEN;
182*44704f69SBart Van Assche         io_hdr[k].timeout = 20000;     /* 20000 millisecs == 20 seconds */
183*44704f69SBart Van Assche         io_hdr[k].pack_id = k;
184*44704f69SBart Van Assche         /* default is to queue at head (in SCSI mid level) */
185*44704f69SBart Van Assche         if (q_at_tail)
186*44704f69SBart Van Assche             io_hdr[k].flags |= SG_FLAG_Q_AT_TAIL;
187*44704f69SBart Van Assche         else
188*44704f69SBart Van Assche             io_hdr[k].flags |= SG_FLAG_Q_AT_HEAD;
189*44704f69SBart Van Assche         /* io_hdr[k].usr_ptr = NULL; */
190*44704f69SBart Van Assche 
191*44704f69SBart Van Assche         if (write(sg_fd, &io_hdr[k], sizeof(sg_io_hdr_t)) < 0) {
192*44704f69SBart Van Assche             perror("sg_queue_tst: sg write error");
193*44704f69SBart Van Assche             close(sg_fd);
194*44704f69SBart Van Assche             return 1;
195*44704f69SBart Van Assche         }
196*44704f69SBart Van Assche     }
197*44704f69SBart Van Assche     /* sleep(3); */
198*44704f69SBart Van Assche     for (k = 0; k < q_len; ++k) {
199*44704f69SBart Van Assche         memset(&rio_hdr, 0, sizeof(sg_io_hdr_t));
200*44704f69SBart Van Assche         rio_hdr.interface_id = 'S';
201*44704f69SBart Van Assche         if (read(sg_fd, &rio_hdr, sizeof(sg_io_hdr_t)) < 0) {
202*44704f69SBart Van Assche             perror("sg_queue_tst: sg read error");
203*44704f69SBart Van Assche             close(sg_fd);
204*44704f69SBart Van Assche             return 1;
205*44704f69SBart Van Assche         }
206*44704f69SBart Van Assche         /* now for the error processing */
207*44704f69SBart Van Assche         ok = 0;
208*44704f69SBart Van Assche         switch (sg_err_category3(&rio_hdr)) {
209*44704f69SBart Van Assche         case SG_LIB_CAT_CLEAN:
210*44704f69SBart Van Assche             ok = 1;
211*44704f69SBart Van Assche             break;
212*44704f69SBart Van Assche         case SG_LIB_CAT_RECOVERED:
213*44704f69SBart Van Assche             printf("Recovered error, continuing\n");
214*44704f69SBart Van Assche             ok = 1;
215*44704f69SBart Van Assche             break;
216*44704f69SBart Van Assche         default: /* won't bother decoding other categories */
217*44704f69SBart Van Assche             sg_chk_n_print3("command error", &rio_hdr, 1);
218*44704f69SBart Van Assche             break;
219*44704f69SBart Van Assche         }
220*44704f69SBart Van Assche 
221*44704f69SBart Van Assche         if (ok) { /* output result if it is available */
222*44704f69SBart Van Assche             /* if (0 == rio_hdr.pack_id) */
223*44704f69SBart Van Assche             if (0 == (rio_hdr.pack_id % 3))
224*44704f69SBart Van Assche                 printf("SEND DIAGNOSTIC %d duration=%u %s\n", rio_hdr.pack_id,
225*44704f69SBart Van Assche                        rio_hdr.duration, (dur_in_nanosecs ? "ns" : "ms"));
226*44704f69SBart Van Assche             else
227*44704f69SBart Van Assche                 printf("INQUIRY %d duration=%u %s\n", rio_hdr.pack_id,
228*44704f69SBart Van Assche                        rio_hdr.duration, (dur_in_nanosecs ? "ns" : "ms"));
229*44704f69SBart Van Assche         }
230*44704f69SBart Van Assche     }
231*44704f69SBart Van Assche 
232*44704f69SBart Van Assche     close(sg_fd);
233*44704f69SBart Van Assche     return 0;
234*44704f69SBart Van Assche }
235