xref: /aosp_15_r20/external/sg3_utils/testing/sg_tst_bidi.c (revision 44704f698541f6367e81f991ef8bb54ccbf3fc18)
1 /*
2  *  Copyright (C) 2019 D. Gilbert
3  *  This program is free software; you can redistribute it and/or modify
4  *  it under the terms of the GNU General Public License as published by
5  *  the Free Software Foundation; either version 2, or (at your option)
6  *  any later version.
7  *
8  * SPDX-License-Identifier: BSD-2-Clause
9  *
10  * Invocation: See usage() function below.
11  *
12  */
13 
14 #include <unistd.h>
15 #include <fcntl.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <errno.h>
20 #include <sys/ioctl.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 
24 #ifndef HAVE_LINUX_SG_V4_HDR
25 
26 /* Kernel uapi header contain __user decorations on user space pointers
27  * to indicate they are unsafe in the kernel space. However glibc takes
28  * all those __user decorations out from headers in /usr/include/linux .
29  * So to stop compile errors when directly importing include/uapi/scsi/sg.h
30  * undef __user before doing that include. */
31 #define __user
32 
33 /* Want to block the original sg.h header from also being included. That
34  * causes lots of multiple definition errors. This will only work if this
35  * header is included _before_ the original sg.h header.  */
36 #define _SCSI_GENERIC_H         /* original kernel header guard */
37 #define _SCSI_SG_H              /* glibc header guard */
38 
39 #include "uapi_sg.h"    /* local copy of include/uapi/scsi/sg.h */
40 
41 #else
42 #define __user
43 #endif  /* end of: ifndef HAVE_LINUX_SG_V4_HDR */
44 
45 #include "sg_lib.h"
46 #include "sg_io_linux.h"
47 #include "sg_linux_inc.h"
48 #include "sg_pr2serr.h"
49 #include "sg_unaligned.h"
50 
51 /* This program tests bidirectional (bidi) SCSI command support in version 4.0
52  * and later of the Linux sg driver. The SBC-3 command XDWRITEREAD(10) that
53  is implemented by the scsi_debug driver is used.  */
54 
55 
56 static const char * version_str = "Version: 1.06  20191021";
57 
58 #define INQ_REPLY_LEN 96
59 #define INQ_CMD_OP 0x12
60 #define INQ_CMD_LEN 6
61 #define SENSE_BUFFER_LEN 96
62 #define XDWRITEREAD_10_OP 0x53
63 #define XDWRITEREAD_10_LEN 10
64 
65 #define EBUFF_SZ 256
66 
67 #ifndef SG_FLAG_Q_AT_TAIL
68 #define SG_FLAG_Q_AT_TAIL 0x10
69 #endif
70 
71 #ifndef SG_FLAG_Q_AT_HEAD
72 #define SG_FLAG_Q_AT_HEAD 0x20
73 #endif
74 
75 #define DEF_Q_LEN 16    /* max in sg v3 and earlier */
76 #define MAX_Q_LEN 256
77 
78 #define DEF_RESERVE_BUFF_SZ (256 * 1024)
79 
80 
81 static bool q_at_tail = false;
82 static int q_len = DEF_Q_LEN;
83 static int sleep_secs = 0;
84 static int reserve_buff_sz = DEF_RESERVE_BUFF_SZ;
85 static int verbose = 0;
86 
87 
88 static void
usage(void)89 usage(void)
90 {
91     printf("Usage: sg_tst_bidi [-b=LB_SZ] [-d=DIO_BLKS] [-D] [-h] -l=LBA [-N] "
92            "[-q=Q_LEN]\n"
93            "                   [-Q] [-r=SZ] [-R=RC] [-s=SEC] [-t] [-v] [-V] "
94            "[-w]\n"
95            "                   <sg_or_bsg_device>\n"
96            " where:\n"
97            "      -b=LB_SZ    logical block size (def: 512 bytes)\n"
98            "      -d=DIO_BLKS    data in and out length (unit: logical "
99            "blocks; def: 1)\n"
100            "      -D    do direct IO (def: indirect which is also "
101            "fallback)\n"
102            "      -h    help: print usage message then exit\n"
103            "      -l=LBA    logical block address (LDA) of first modded "
104            "block\n"
105            "      -N    durations in nanoseconds (def: milliseconds)\n"
106            "      -q=Q_LEN    queue length, between 1 and 511 (def: 16). "
107            " Calls\n"
108            "                  ioctl(SG_IO) when -q=1 else SG_IOSUBMIT "
109            "(async)\n"
110            "      -Q    quiet, suppress usual output\n"
111            "      -r=SZ     reserve buffer size in KB (def: 256 --> 256 "
112            "KB)\n"
113            "      -R=RC     repetition count (def: 0)\n"
114            "      -s=SEC    sleep between writes and reads (def: 0)\n"
115            "      -t    queue_at_tail (def: q_at_head)\n"
116            "      -v    increase verbosity of output\n"
117            "      -V    print version string then exit\n"
118            "      -w    sets DISABLE WRITE bit on cdb to 0 (def: 1)\n\n"
119            "Warning: this test utility writes to location LBA and Q_LEN "
120            "following\nblocks using the XDWRITEREAD(10) SBC-3 command. "
121            "When -q=1 does\nioctl(SG_IO) and that is only case when a "
122            "bsg device can be given.\n");
123 }
124 
125 static int
ext_ioctl(int sg_fd,bool nanosecs)126 ext_ioctl(int sg_fd, bool nanosecs)
127 {
128     struct sg_extended_info sei;
129     struct sg_extended_info * seip;
130 
131     seip = &sei;
132     memset(seip, 0, sizeof(*seip));
133     seip->sei_wr_mask |= SG_SEIM_RESERVED_SIZE;
134     seip->reserved_sz = reserve_buff_sz;
135     seip->sei_rd_mask |= SG_SEIM_RESERVED_SIZE;
136     seip->sei_wr_mask |= SG_SEIM_CTL_FLAGS;
137     seip->sei_rd_mask |= SG_SEIM_CTL_FLAGS; /* this or previous optional */
138     seip->ctl_flags_wr_mask |= SG_CTL_FLAGM_TIME_IN_NS;
139     seip->ctl_flags_rd_mask |= SG_CTL_FLAGM_TIME_IN_NS;
140     if (nanosecs)
141         seip->ctl_flags |= SG_CTL_FLAGM_TIME_IN_NS;
142     else
143         seip->ctl_flags &= ~SG_CTL_FLAGM_TIME_IN_NS;
144 
145     if (ioctl(sg_fd, SG_SET_GET_EXTENDED, seip) < 0) {
146         pr2serr("ioctl(SG_SET_GET_EXTENDED) failed, errno=%d %s\n", errno,
147                 strerror(errno));
148         return 1;
149     }
150     return 0;
151 }
152 
153 
154 int
main(int argc,char * argv[])155 main(int argc, char * argv[])
156 {
157     bool done;
158     bool direct_io = false;
159     bool lba_given = false;
160     bool nanosecs = false;
161     bool quiet = false;
162     bool disable_write = true;
163     int k, j, ok, ver_num, pack_id, num_waiting, din_len;
164     int dout_len, cat;
165     int ret = 0;
166     int rep_count = 0;
167     int sg_fd = -1;
168     int lb_sz = 512;
169     int dio_blks = 1;
170     int dirio_count = 0;
171     int64_t lba = 0;
172     uint8_t inq_cdb[INQ_CMD_LEN] = {INQ_CMD_OP, 0, 0, 0, INQ_REPLY_LEN, 0};
173     uint8_t xdwrrd10_cdb[XDWRITEREAD_10_LEN] =
174                         {XDWRITEREAD_10_OP, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
175     uint8_t inqBuff[MAX_Q_LEN][INQ_REPLY_LEN];
176     struct sg_io_v4 io_v4[MAX_Q_LEN];
177     struct sg_io_v4 rio_v4;
178     struct sg_io_v4 * io_v4p;
179     char * file_name = 0;
180     uint8_t * dinp;
181     uint8_t * free_dinp = NULL;
182     uint8_t * doutp;
183     uint8_t * free_doutp = NULL;
184     char ebuff[EBUFF_SZ];
185     uint8_t sense_buffer[MAX_Q_LEN][SENSE_BUFFER_LEN] SG_C_CPP_ZERO_INIT;
186 
187     for (k = 1; k < argc; ++k) {
188         if (0 == memcmp("-b=", argv[k], 3)) {
189             lb_sz = atoi(argv[k] + 3);
190             if (lb_sz < 512 || (0 != (lb_sz % 512))) {
191                 printf("Expect -b=LB_SZ be 512 or higher and a power of 2\n");
192                 file_name = 0;
193                 break;
194             }
195         } else if (0 == memcmp("-d=", argv[k], 3)) {
196             dio_blks = atoi(argv[k] + 3);
197             if ((dio_blks < 1) || (dio_blks > 0xffff)) {
198                 fprintf(stderr, "Expect -d=DIO_BLKS to be 1 or greater and "
199                         "less than 65536\n");
200                 file_name = 0;
201                 break;
202             }
203         } else if (0 == memcmp("-D", argv[k], 2))
204             direct_io = true;
205         else if (0 == memcmp("-h", argv[k], 2)) {
206             file_name = 0;
207             break;
208         } else if (0 == memcmp("-l=", argv[k], 3)) {
209             if (lba_given) {
210                 pr2serr("can only give -l=LBA option once\n");
211                 file_name = 0;
212                 break;
213             }
214             lba = sg_get_llnum(argv[k] + 3);
215             if ((lba < 0) || (lba > 0xffffffff)) {
216                 pr2serr("Expect -l= argument (LBA) to be non-negative and "
217                         "fit in 32 bits\n");
218                 return -1;
219             }
220             lba_given = true;
221         } else if (0 == memcmp("-N", argv[k], 2))
222             nanosecs = true;
223         else if (0 == memcmp("-q=", argv[k], 3)) {
224             q_len = atoi(argv[k] + 3);
225             if ((q_len > 511) || (q_len < 1)) {
226                 printf("Expect -q= to take a number (q length) between 1 "
227                        "and 511\n");
228                 file_name = 0;
229                 break;
230             }
231         } else if (0 == memcmp("-Q", argv[k], 2))
232             quiet = true;
233         else if (0 == memcmp("-r=", argv[k], 3)) {
234             reserve_buff_sz = atoi(argv[k] + 3);
235             if (reserve_buff_sz < 0) {
236                 printf("Expect -r= to take a number 0 or higher\n");
237                 file_name = 0;
238                 break;
239             }
240         } else if (0 == memcmp("-R=", argv[k], 3)) {
241             rep_count = atoi(argv[k] + 3);
242             if (rep_count < 0) {
243                 printf("Expect -R= to take a number 0 or higher\n");
244                 file_name = 0;
245                 break;
246             }
247         } else if (0 == memcmp("-s=", argv[k], 3)) {
248             sleep_secs = atoi(argv[k] + 3);
249             if (sleep_secs < 0) {
250                 printf("Expect -s= to take a number 0 or higher\n");
251                 file_name = 0;
252                 break;
253             }
254         } else if (0 == memcmp("-t", argv[k], 2))
255             q_at_tail = true;
256         else if (0 == memcmp("-vvvvv", argv[k], 5))
257             verbose += 5;
258         else if (0 == memcmp("-vvvv", argv[k], 5))
259             verbose += 4;
260         else if (0 == memcmp("-vvv", argv[k], 4))
261             verbose += 3;
262         else if (0 == memcmp("-vv", argv[k], 3))
263             verbose += 2;
264         else if (0 == memcmp("-v", argv[k], 2))
265             verbose += 1;
266         else if (0 == memcmp("-V", argv[k], 2)) {
267             printf("%s\n", version_str);
268             return 0;
269         } else if (0 == memcmp("-w", argv[k], 2))
270             disable_write = false;
271         else if (*argv[k] == '-') {
272             printf("Unrecognized switch: %s\n", argv[k]);
273             file_name = 0;
274             break;
275         }
276         else if (0 == file_name)
277             file_name = argv[k];
278         else {
279             printf("too many arguments\n");
280             file_name = 0;
281             break;
282         }
283     }
284     if (0 == file_name) {
285         printf("No filename (sg device) given\n\n");
286         usage();
287         return 1;
288     }
289     if (! lba_given) {
290         pr2serr("Needs the -l=LBA 'option' to be given, hex numbers "
291                 "prefixed by '0x';\nor with a trailing 'h'\n");
292         ret = 1;
293         goto out;
294     }
295     din_len = lb_sz * dio_blks;
296     dout_len = lb_sz * dio_blks;
297     dinp = sg_memalign(din_len * q_len, 0, &free_dinp, false);
298     if (NULL == dinp) {
299         fprintf(stderr, "Unable to allocate %d byte for din buffer\n",
300                 din_len * q_len);
301         ret = 1;
302         goto out;
303     }
304     doutp = sg_memalign(dout_len * q_len, 0, &free_doutp, false);
305     if (NULL == doutp) {
306         fprintf(stderr, "Unable to allocate %d byte for dout buffer\n",
307                 dout_len * q_len);
308         ret = 1;
309         goto out;
310     }
311 
312     /* An access mode of O_RDWR is required for write()/read() interface */
313     if ((sg_fd = open(file_name, O_RDWR)) < 0) {
314         snprintf(ebuff, EBUFF_SZ,
315                  "error opening file: %s", file_name);
316         perror(ebuff);
317         return 1;
318     }
319     if (verbose)
320         fprintf(stderr, "opened given file: %s successfully, fd=%d\n",
321                 file_name, sg_fd);
322 
323     if (ioctl(sg_fd, SG_GET_VERSION_NUM, &ver_num) < 0) {
324         pr2serr("ioctl(SG_GET_VERSION_NUM) failed, errno=%d %s\n", errno,
325                 strerror(errno));
326         goto out;
327     }
328     if (! quiet)
329         printf("Linux sg driver version: %d\n", ver_num);
330     if ((q_len > 1) && ext_ioctl(sg_fd, nanosecs))
331         goto out;
332 
333 
334     if (1 == q_len) {   /* do sync ioct(SG_IO) */
335         io_v4p = &io_v4[k];
336 rep_sg_io:
337         memset(io_v4p, 0, sizeof(*io_v4p));
338         io_v4p->guard = 'Q';
339         if (direct_io)
340             io_v4p->flags |= SG_FLAG_DIRECT_IO;
341         if (disable_write)
342             xdwrrd10_cdb[2] |= 0x4;
343         sg_put_unaligned_be16(dio_blks, xdwrrd10_cdb + 7);
344         sg_put_unaligned_be32(lba, xdwrrd10_cdb + 2);
345         if (verbose > 2) {
346             pr2serr("    %s cdb: ", "XDWRITE(10)");
347             for (j = 0; j < XDWRITEREAD_10_LEN; ++j)
348                 pr2serr("%02x ", xdwrrd10_cdb[j]);
349             pr2serr("\n");
350         }
351         io_v4p->request_len = XDWRITEREAD_10_LEN;
352         io_v4p->request = (uint64_t)(uintptr_t)xdwrrd10_cdb;
353         io_v4p->din_xfer_len = din_len;
354         io_v4p->din_xferp = (uint64_t)(uintptr_t)(dinp + (k * din_len));
355         io_v4p->dout_xfer_len = dout_len;
356         io_v4p->dout_xferp = (uint64_t)(uintptr_t)(doutp + (k * dout_len));
357         io_v4p->response = (uint64_t)(uintptr_t)sense_buffer[k];
358         io_v4p->max_response_len = SENSE_BUFFER_LEN;
359         io_v4p->timeout = 20000;     /* 20000 millisecs == 20 seconds */
360         io_v4p->request_extra = 99;  /* so pack_id doesn't start at 0 */
361         /* default is to queue at head (in SCSI mid level) */
362         if (q_at_tail)
363             io_v4p->flags |= SG_FLAG_Q_AT_TAIL;
364         else
365             io_v4p->flags |= SG_FLAG_Q_AT_HEAD;
366         /* io_v4p->usr_ptr = NULL; */
367 
368         if (ioctl(sg_fd, SG_IO, io_v4p) < 0) {
369             pr2serr("sg ioctl(SG_IO) errno=%d [%s]\n", errno,
370                     strerror(errno));
371             close(sg_fd);
372             return 1;
373         }
374         /* now for the error processing */
375         ok = 0;
376         rio_v4 = *io_v4p;
377         cat = sg_err_category_new(rio_v4.device_status,
378                                   rio_v4.transport_status,
379                                   rio_v4.driver_status,
380                           (const uint8_t *)(unsigned long)rio_v4.response,
381                                   rio_v4.response_len);
382         switch (cat) {
383         case SG_LIB_CAT_CLEAN:
384             ok = 1;
385             break;
386         case SG_LIB_CAT_RECOVERED:
387             printf("Recovered error, continuing\n");
388             ok = 1;
389             break;
390         default: /* won't bother decoding other categories */
391             sg_linux_sense_print(NULL, rio_v4.device_status,
392                                  rio_v4.transport_status,
393                                  rio_v4.driver_status,
394                          (const uint8_t *)(unsigned long)rio_v4.response,
395                                  rio_v4.response_len, true);
396             break;
397         }
398         if ((rio_v4.info & SG_INFO_DIRECT_IO_MASK) != SG_INFO_DIRECT_IO)
399             ++dirio_count;
400         if (verbose > 3) {
401             pr2serr(">> din_resid=%d, dout_resid=%d, info=0x%x\n",
402                     rio_v4.din_resid, rio_v4.dout_resid, rio_v4.info);
403             if (rio_v4.response_len > 0) {
404                 pr2serr("sense buffer: ");
405                 hex2stderr(sense_buffer[k], rio_v4.response_len, -1);
406             }
407         }
408         if ((! quiet) && ok)  /* output result if it is available */
409             printf("XDWRITEREAD(10) using ioctl(SG_IO) duration=%u\n",
410                    rio_v4.duration);
411         if (rep_count-- > 0)
412             goto rep_sg_io;
413         goto out;
414     }
415 
416 rep_async:
417     if (! quiet)
418         printf("start write() calls\n");
419     for (k = 0; k < q_len; ++k) {
420         io_v4p = &io_v4[k];
421         memset(io_v4p, 0, sizeof(*io_v4p));
422         io_v4p->guard = 'Q';
423         if (direct_io)
424             io_v4p->flags |= SG_FLAG_DIRECT_IO;
425         /* io_v4p->iovec_count = 0; */  /* memset takes care of this */
426         if (0 != (k % 64)) {
427             if (disable_write)
428                 xdwrrd10_cdb[2] |= 0x4;
429             sg_put_unaligned_be16(dio_blks, xdwrrd10_cdb + 7);
430             sg_put_unaligned_be32(lba, xdwrrd10_cdb + 2);
431             if (verbose > 2) {
432                 pr2serr("    %s cdb: ", "XDWRITE(10)");
433                 for (j = 0; j < XDWRITEREAD_10_LEN; ++j)
434                     pr2serr("%02x ", xdwrrd10_cdb[j]);
435                 pr2serr("\n");
436             }
437             io_v4p->request_len = XDWRITEREAD_10_LEN;
438             io_v4p->request = (uint64_t)(uintptr_t)xdwrrd10_cdb;
439             io_v4p->din_xfer_len = din_len;
440             io_v4p->din_xferp = (uint64_t)(uintptr_t)(dinp + (k * din_len));
441             io_v4p->dout_xfer_len = dout_len;
442             io_v4p->dout_xferp = (uint64_t)(uintptr_t)(doutp + (k * dout_len));
443         } else {
444             if (verbose > 2) {
445                 pr2serr("    %s cdb: ", "INQUIRY");
446                 for (j = 0; j < INQ_CMD_LEN; ++j)
447                     pr2serr("%02x ", inq_cdb[j]);
448                 pr2serr("\n");
449             }
450             io_v4p->request_len = sizeof(inq_cdb);
451             io_v4p->request = (uint64_t)(uintptr_t)inq_cdb;
452             io_v4p->din_xfer_len = INQ_REPLY_LEN;
453             io_v4p->din_xferp = (uint64_t)(uintptr_t)inqBuff[k];
454         }
455         io_v4p->response = (uint64_t)(uintptr_t)sense_buffer[k];
456         io_v4p->max_response_len = SENSE_BUFFER_LEN;
457         io_v4p->timeout = 20000;     /* 20000 millisecs == 20 seconds */
458         io_v4p->request_extra = k + 3;  /* so pack_id doesn't start at 0 */
459         /* default is to queue at head (in SCSI mid level) */
460         if (q_at_tail)
461             io_v4p->flags |= SG_FLAG_Q_AT_TAIL;
462         else
463             io_v4p->flags |= SG_FLAG_Q_AT_HEAD;
464         /* io_v4p->usr_ptr = NULL; */
465 
466         if (ioctl(sg_fd, SG_IOSUBMIT, io_v4p) < 0) {
467             pr2serr("sg ioctl(SG_IOSUBMIT) errno=%d [%s]\n", errno,
468                     strerror(errno));
469             close(sg_fd);
470             return 1;
471         }
472     }
473 
474 #if 0
475     {
476         struct sg_scsi_id ssi;
477 
478         memset(&ssi, 0, sizeof(ssi));
479         if (ioctl(sg_fd, SG_GET_SCSI_ID, &ssi) < 0)
480             pr2serr("ioctl(SG_GET_SCSI_ID) failed, errno=%d %s\n",
481                     errno, strerror(errno));
482         else {
483             printf("host_no: %d\n", ssi.host_no);
484             printf("  channel: %d\n", ssi.channel);
485             printf("  scsi_id: %d\n", ssi.scsi_id);
486             printf("  lun: %d\n", ssi.lun);
487             printf("  pdt: %d\n", ssi.scsi_type);
488             printf("  h_cmd_per_lun: %d\n", ssi.h_cmd_per_lun);
489             printf("  d_queue_depth: %d\n", ssi.d_queue_depth);
490         }
491     }
492 #endif
493     if (ioctl(sg_fd, SG_GET_PACK_ID, &pack_id) < 0)
494         pr2serr("ioctl(SG_GET_PACK_ID) failed, errno=%d %s\n",
495                 errno, strerror(errno));
496     else if (! quiet)
497         printf("first available pack_id: %d\n", pack_id);
498     if (ioctl(sg_fd, SG_GET_NUM_WAITING, &num_waiting) < 0)
499         pr2serr("ioctl(SG_GET_NUM_WAITING) failed, errno=%d %s\n",
500                 errno, strerror(errno));
501     else if (! quiet)
502         printf("num_waiting: %d\n", num_waiting);
503 
504     if (sleep_secs > 0)
505         sleep(sleep_secs);
506 
507     if (ioctl(sg_fd, SG_GET_PACK_ID, &pack_id) < 0)
508         pr2serr("ioctl(SG_GET_PACK_ID) failed, errno=%d %s\n",
509                 errno, strerror(errno));
510     else if (! quiet)
511         printf("first available pack_id: %d\n", pack_id);
512     if (ioctl(sg_fd, SG_GET_NUM_WAITING, &num_waiting) < 0)
513         pr2serr("ioctl(SG_GET_NUM_WAITING) failed, errno=%d %s\n",
514                 errno, strerror(errno));
515     else if (! quiet)
516         printf("num_waiting: %d\n", num_waiting);
517 
518     if (! quiet)
519         printf("\nstart read() calls\n");
520     for (k = 0, done = false; k < q_len; ++k) {
521         if ((! done) && (k == q_len / 2)) {
522             done = true;
523             if (! quiet)
524                 printf("\n>>> half way through read\n");
525             if (ioctl(sg_fd, SG_GET_PACK_ID, &pack_id) < 0)
526                 pr2serr("ioctl(SG_GET_PACK_ID) failed, errno=%d %s\n",
527                         errno, strerror(errno));
528             else if (! quiet)
529                 printf("first available pack_id: %d\n", pack_id);
530             if (ioctl(sg_fd, SG_GET_NUM_WAITING, &num_waiting) < 0)
531                 pr2serr("ioctl(SG_GET_NUM_WAITING) failed, errno=%d %s\n",
532                         errno, strerror(errno));
533             else if (! quiet)
534                 printf("num_waiting: %d\n", num_waiting);
535         }
536         memset(&rio_v4, 0, sizeof(struct sg_io_v4));
537         rio_v4.guard = 'Q';
538         if (ioctl(sg_fd, SG_IORECEIVE, &rio_v4) < 0) {
539             perror("sg ioctl(SG_IORECEIVE) error");
540             close(sg_fd);
541             return 1;
542         }
543         /* now for the error processing */
544         ok = 0;
545         cat = sg_err_category_new(rio_v4.device_status,
546                                   rio_v4.transport_status,
547                                   rio_v4.driver_status,
548                           (const uint8_t *)(unsigned long)rio_v4.response,
549                                   rio_v4.response_len);
550         switch (cat) {
551         case SG_LIB_CAT_CLEAN:
552             ok = 1;
553             break;
554         case SG_LIB_CAT_RECOVERED:
555             printf("Recovered error, continuing\n");
556             ok = 1;
557             break;
558         default: /* won't bother decoding other categories */
559             sg_linux_sense_print(NULL, rio_v4.device_status,
560                                  rio_v4.transport_status,
561                                  rio_v4.driver_status,
562                          (const uint8_t *)(unsigned long)rio_v4.response,
563                                  rio_v4.response_len, true);
564             break;
565         }
566         if ((rio_v4.info & SG_INFO_DIRECT_IO_MASK) != SG_INFO_DIRECT_IO)
567             ++dirio_count;
568         if (verbose > 3) {
569             pr2serr(">> din_resid=%d, dout_resid=%d, info=0x%x\n",
570                     rio_v4.din_resid, rio_v4.dout_resid, rio_v4.info);
571             if (rio_v4.response_len > 0) {
572                 pr2serr("sense buffer: ");
573                 hex2stderr(sense_buffer[k], rio_v4.response_len, -1);
574             }
575         }
576         if ((! quiet) && ok) { /* output result if it is available */
577             if (0 != ((rio_v4.request_extra - 3) % 64))
578                 printf("XDWRITEREAD(10) %d duration=%u\n",
579                        rio_v4.request_extra, rio_v4.duration);
580             else
581                 printf("INQUIRY %d duration=%u\n",
582                        rio_v4.request_extra,
583                        rio_v4.duration);
584         }
585     }
586     if (direct_io && (dirio_count < q_len)) {
587         pr2serr("Direct IO requested %d times, done %d times\nMaybe need "
588                 "'echo 1 > /sys/module/sg/parameters/allow_dio'\n", q_len, dirio_count);
589     }
590     if (rep_count-- > 0)
591         goto rep_async;
592     ret = 0;
593 
594 out:
595     if (sg_fd >= 0)
596         close(sg_fd);
597     if (free_dinp)
598         free(free_dinp);
599     if (free_doutp)
600         free(free_doutp);
601     return ret;
602 }
603