1 /*
2 * Copyright (C) 2018-2022 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 <stdarg.h>
19 #include <stdbool.h>
20 #include <ctype.h>
21 #include <string.h>
22 #include <errno.h>
23 #include <time.h>
24 #include <sys/ioctl.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/utsname.h>
28 #define __STDC_FORMAT_MACROS 1
29 #include <inttypes.h>
30
31 #include <sys/socket.h> /* For passing fd_s via Unix sockets */
32
33 #ifndef HAVE_LINUX_SG_V4_HDR
34
35 /* Kernel uapi header contain __user decorations on user space pointers
36 * to indicate they are unsafe in the kernel space. However glibc takes
37 * all those __user decorations out from headers in /usr/include/linux .
38 * So to stop compile errors when directly importing include/uapi/scsi/sg.h
39 * undef __user before doing that include. */
40 #define __user
41
42 /* Want to block the original sg.h header from also being included. That
43 * causes lots of multiple definition errors. This will only work if this
44 * header is included _before_ the original sg.h header. */
45 #define _SCSI_GENERIC_H /* original kernel header guard */
46 #define _SCSI_SG_H /* glibc header guard */
47
48 #include "uapi_sg.h" /* local copy of include/uapi/scsi/sg.h */
49
50 #else
51 #define __user
52 #endif /* end of: ifndef HAVE_LINUX_SG_V4_HDR */
53
54 #include "sg_lib.h"
55 #include "sg_io_linux.h"
56 #include "sg_linux_inc.h"
57 #include "sg_pr2serr.h"
58
59 /* This program tests ioctl() calls added and modified in version 4.0 and
60 * later of the Linux sg driver. */
61
62
63 static const char * version_str = "Version: 1.21 20220202";
64
65 #define INQ_REPLY_LEN 128
66 #define INQ_CMD_LEN 6
67 #define SDIAG_CMD_LEN 6
68 #define SENSE_BUFFER_LEN 96
69
70 #define EBUFF_SZ 512
71
72 #ifndef SG_FLAG_Q_AT_TAIL
73 #define SG_FLAG_Q_AT_TAIL 0x10
74 #endif
75
76 #ifndef SG_FLAG_Q_AT_HEAD
77 #define SG_FLAG_Q_AT_HEAD 0x20
78 #endif
79
80 #define DEF_Q_LEN 16 /* max in sg v3 and earlier */
81 #define MAX_Q_LEN 512
82
83 #define DEF_RESERVE_BUFF_SZ (256 * 1024)
84
85 static bool create_time = false;
86 static bool is_parent = false;
87 static bool do_fork = false;
88 static bool ioctl_only = false;
89 static bool more_async = false;
90 static bool no_duration = false;
91 static bool q_at_tail = false;
92 static bool write_only = false;
93 static bool mrq_immed = false; /* if set, also sets mrq_iosubmit */
94 static bool mrq_half_immed = false;
95 static bool mrq_iosubmit = false;
96 static bool show_size_value = false;
97 static bool do_v3_only = false;
98
99 static int childs_pid = 0;
100 static int iterator_test = -1;
101 static int object_walk_test = -1;
102 static int sg_drv_ver_num = 0;
103 static int q_len = DEF_Q_LEN;
104 static int sleep_secs = 0;
105 static int reserve_buff_sz = DEF_RESERVE_BUFF_SZ;
106 static int num_mrqs = 0;
107 static int num_sgnw = 0;
108 static int dname_current = 0;
109 static int dname_last = 0;
110 static int dname_pos = 0;
111 static int verbose = 0;
112
113 static const char * relative_cp = "";
114 static char * file_name = NULL;
115
116
117 static void
usage(void)118 usage(void)
119 {
120 printf("Usage: sg_tst_ioctl [-3] [-c] [-f] [-h] [-I=0|1] [-J=0|1] "
121 "[-l=Q_LEN]\n"
122 " [-m=MRQS[,I|S]] [-M] [-n] [-o] [-r=SZ] "
123 "[-s=SEC]\n"
124 " [-S] [-t] [-T=NUM] [-v] [-V] [-w]\n"
125 " <sg_device>[-<num>] [<sg_device2>]\n"
126 " where:\n"
127 " -3 use sg v3 interface (def: sg v4 if available)\n"
128 " -c timestamp when sg driver created <sg_device>\n"
129 " -f fork and test share between processes\n"
130 " -h help: print usage message then exit\n"
131 " -I=0|1 iterator test of mid-level; 0: unlocked, 1: "
132 "locked\n"
133 " does test -T=NUM times, outputs duration\n"
134 " -J=0|1 object walk up then 2 lookups; 0: no logging; "
135 "1: log\n"
136 " up-scan once per 1000 iterations\n"
137 " -l=Q_LEN queue length, between 1 and 511 (def: 16)\n"
138 " -m=MRQS[,I|S] test multi-req, MRQS number to do; if "
139 "the letter\n"
140 " 'I' is appended after a comma, then do "
141 "IMMED mrq;\n"
142 " 'i' IMMED on submission, non-IMMED on "
143 "receive;\n"
144 " 'S' is appended, then use "
145 "ioctl(SG_IOSUBMIT)\n"
146 " -M set 'more async' flag\n"
147 " -n do not calculate per command duration (def: do)\n"
148 " -o ioctls only, then exit\n"
149 " -r=SZ reserve buffer size in KB (def: 256 --> 256 "
150 "KB)\n"
151 " -s=SEC sleep between writes and reads (def: 0)\n"
152 " -S size of interface structures plus ioctl "
153 "values\n"
154 " -t queue_at_tail (def: q_at_head)\n"
155 " -T=NUM time overhead of NUM invocations of\n"
156 " ioctl(SG_GET_NUM_WAITING); then exit\n"
157 " -v increase verbosity of output\n"
158 " -V print version string then exit\n"
159 " -w write (submit) only then exit\n\n");
160 printf("There are various groups of options for different tests. The "
161 "get_num_waiting\ngroup needs '-T=NUM' given. When '-I=0|1' is "
162 "also given then an object tree\niterator test is done NUM "
163 "times. If instead '-J=0|1' is given then an\nobject tree "
164 "traversal (up/down) is done 10,000 times (and NUM is\n"
165 "ignored).\n"
166 );
167 }
168
169 static void
timespec_add(const struct timespec * lhs_p,const struct timespec * rhs_p,struct timespec * res_p)170 timespec_add(const struct timespec *lhs_p, const struct timespec *rhs_p,
171 struct timespec *res_p)
172 {
173 if ((lhs_p->tv_nsec + rhs_p->tv_nsec) > 1000000000L) {
174 res_p->tv_sec = lhs_p->tv_sec + rhs_p->tv_sec + 1;
175 res_p->tv_nsec = lhs_p->tv_nsec + rhs_p->tv_nsec - 1000000000L;
176 } else {
177 res_p->tv_sec = lhs_p->tv_sec + rhs_p->tv_sec;
178 res_p->tv_nsec = lhs_p->tv_nsec + rhs_p->tv_nsec;
179 }
180 }
181
182 static void
timespec_diff(const struct timespec * lhs_p,const struct timespec * rhs_p,struct timespec * res_p)183 timespec_diff(const struct timespec *lhs_p, const struct timespec *rhs_p,
184 struct timespec *res_p)
185 {
186 if ((lhs_p->tv_nsec - rhs_p->tv_nsec) < 0) {
187 res_p->tv_sec = lhs_p->tv_sec - rhs_p->tv_sec - 1;
188 res_p->tv_nsec = lhs_p->tv_nsec - rhs_p->tv_nsec + 1000000000L;
189 } else {
190 res_p->tv_sec = lhs_p->tv_sec - rhs_p->tv_sec;
191 res_p->tv_nsec = lhs_p->tv_nsec - rhs_p->tv_nsec;
192 }
193 }
194
195 /* Returns 0 on success. */
timespec2str(char * buf,unsigned int len,struct timespec * ts)196 int timespec2str(char *buf, unsigned int len, struct timespec *ts)
197 {
198 int ret;
199 struct tm t;
200
201 tzset();
202 if (localtime_r(&(ts->tv_sec), &t) == NULL)
203 return 1;
204
205 ret = strftime(buf, len, "%F %T", &t);
206 if (ret == 0)
207 return 2;
208 len -= ret - 1;
209
210 ret = snprintf(&buf[strlen(buf)], len, ".%09ld", ts->tv_nsec);
211 if (ret >= (int)len)
212 return 3;
213 return 0;
214 }
215
216 /* This function taken from Keith Parkard's blog dated 20121005 */
217 static ssize_t
sock_fd_write(int sock,const void * buf,ssize_t buflen,int fd)218 sock_fd_write(int sock, const void *buf, ssize_t buflen, int fd)
219 {
220 ssize_t size;
221 struct msghdr msg;
222 struct iovec iov;
223 union {
224 struct cmsghdr cmsghdr;
225 char control[CMSG_SPACE(sizeof (int))];
226 } cmsgu;
227 struct cmsghdr *cmsg;
228
229 iov.iov_base = (void *)buf; /* OS shouldn't write back in this */
230 iov.iov_len = buflen;
231
232 msg.msg_name = NULL;
233 msg.msg_namelen = 0;
234 msg.msg_iov = &iov;
235 msg.msg_iovlen = 1;
236
237 if (fd != -1) {
238 msg.msg_control = cmsgu.control;
239 msg.msg_controllen = sizeof(cmsgu.control);
240
241 cmsg = CMSG_FIRSTHDR(&msg);
242 cmsg->cmsg_len = CMSG_LEN(sizeof (int));
243 cmsg->cmsg_level = SOL_SOCKET;
244 cmsg->cmsg_type = SCM_RIGHTS;
245
246 printf ("passing fd %d\n", fd);
247 *((int *) CMSG_DATA(cmsg)) = fd;
248 } else {
249 msg.msg_control = NULL;
250 msg.msg_controllen = 0;
251 printf ("not passing fd\n");
252 }
253
254 size = sendmsg(sock, &msg, 0);
255
256 if (size < 0)
257 perror ("sendmsg");
258 return size;
259 }
260
261 /* This function taken from Keith Parkard's blog dated 2101205 */
262 static ssize_t
sock_fd_read(int sock,void * buf,ssize_t bufsize,int * fd)263 sock_fd_read(int sock, void *buf, ssize_t bufsize, int *fd)
264 {
265 ssize_t size;
266
267 if (fd) {
268 struct msghdr msg;
269 struct iovec iov;
270 union {
271 struct cmsghdr cmsghdr;
272 char control[CMSG_SPACE(sizeof (int))];
273 } cmsgu;
274 struct cmsghdr *cmsg;
275
276 iov.iov_base = buf;
277 iov.iov_len = bufsize;
278
279 msg.msg_name = NULL;
280 msg.msg_namelen = 0;
281 msg.msg_iov = &iov;
282 msg.msg_iovlen = 1;
283 msg.msg_control = cmsgu.control;
284 msg.msg_controllen = sizeof(cmsgu.control);
285 size = recvmsg (sock, &msg, 0);
286 if (size < 0) {
287 perror ("recvmsg");
288 exit(1);
289 }
290 cmsg = CMSG_FIRSTHDR(&msg);
291 if (cmsg && cmsg->cmsg_len == CMSG_LEN(sizeof(int))) {
292 if (cmsg->cmsg_level != SOL_SOCKET) {
293 fprintf (stderr, "invalid cmsg_level %d\n",
294 cmsg->cmsg_level);
295 exit(1);
296 }
297 if (cmsg->cmsg_type != SCM_RIGHTS) {
298 fprintf (stderr, "invalid cmsg_type %d\n",
299 cmsg->cmsg_type);
300 exit(1);
301 }
302
303 *fd = *((int *) CMSG_DATA(cmsg));
304 printf ("received fd %d\n", *fd);
305 } else
306 *fd = -1;
307 } else {
308 size = read (sock, buf, bufsize);
309 if (size < 0) {
310 perror("read");
311 exit(1);
312 }
313 }
314 return size;
315 }
316
317 static void
set_more_async(int fd,bool more_asy,bool no_dur)318 set_more_async(int fd, bool more_asy, bool no_dur)
319 {
320 if (sg_drv_ver_num > 40030) {
321 struct sg_extended_info sei;
322 struct sg_extended_info * seip;
323
324 seip = &sei;
325 memset(seip, 0, sizeof(*seip));
326 seip->sei_wr_mask |= SG_SEIM_CTL_FLAGS;
327 seip->sei_rd_mask |= SG_SEIM_CTL_FLAGS;
328 if (more_asy) {
329 seip->ctl_flags_wr_mask |= SG_CTL_FLAGM_MORE_ASYNC;
330 seip->ctl_flags = SG_CTL_FLAGM_MORE_ASYNC;
331 }
332 if (no_dur) {
333 seip->ctl_flags_wr_mask |= SG_CTL_FLAGM_NO_DURATION;
334 seip->ctl_flags = SG_CTL_FLAGM_NO_DURATION;
335 }
336 if (ioctl(fd, SG_SET_GET_EXTENDED, seip) < 0) {
337 pr2serr("ioctl(SG_SET_GET_EXTENDED, MORE_ASYNC(|NO_DUR)) failed, "
338 "errno=%d %s\n", errno, strerror(errno));
339 return;
340 }
341 } else
342 pr2serr("sg driver too old for ioctl(SG_SET_GET_EXTENDED)\n");
343 }
344
345 static void
pr_create_dev_time(int sg_fd,const char * dev_name)346 pr_create_dev_time(int sg_fd, const char * dev_name)
347 {
348 uint32_t u;
349 uint64_t l;
350 struct sg_extended_info sei;
351 struct sg_extended_info * seip;
352 struct timespec time_up, realtime, boottime, createtime, tmp;
353 char b[64];
354
355 seip = &sei;
356 memset(seip, 0, sizeof(*seip));
357 seip->sei_wr_mask |= SG_SEIM_READ_VAL;
358 seip->sei_rd_mask |= SG_SEIM_READ_VAL;
359 seip->read_value = SG_SEIRV_DEV_TS_LOWER;
360 if (ioctl(sg_fd, SG_SET_GET_EXTENDED, seip) < 0) {
361 pr2serr("%s: ioctl(SG_SET_GET_EXTENDED) failed, errno=%d %s\n",
362 __func__, errno, strerror(errno));
363 return;
364 }
365 u = seip->read_value;
366 seip->read_value = SG_SEIRV_DEV_TS_UPPER;
367 if (ioctl(sg_fd, SG_SET_GET_EXTENDED, seip) < 0) {
368 pr2serr("%s: ioctl(SG_SET_GET_EXTENDED) failed, errno=%d %s\n",
369 __func__, errno, strerror(errno));
370 return;
371 }
372 l = seip->read_value;
373 l <<= 32;
374 l |= u;
375 time_up.tv_sec = l / 1000000000UL;
376 time_up.tv_nsec = l % 1000000000UL;
377 /* printf("create time nanoseconds=%" PRIu64 "\n", l); */
378 if (clock_gettime(CLOCK_REALTIME, &realtime) < 0) {
379 pr2serr("%s: clock_gettime(CLOCK_REALTIME) failed, errno=%d %s\n",
380 __func__, errno, strerror(errno));
381 return;
382 }
383 if (clock_gettime(CLOCK_BOOTTIME, &boottime) < 0) {
384 pr2serr("%s: clock_gettime(CLOCK_REALTIME) failed, errno=%d %s\n",
385 __func__, errno, strerror(errno));
386 return;
387 }
388 timespec_diff(&realtime, &boottime, &tmp);
389 timespec_add(&tmp, &time_up, &createtime);
390 #if 0
391 printf("real time: %ld,%ld\n", realtime.tv_sec, realtime.tv_nsec);
392 printf("boot time: %ld,%ld\n", boottime.tv_sec, boottime.tv_nsec);
393 printf("time up: %ld,%ld\n", time_up.tv_sec, time_up.tv_nsec);
394 printf("create time: %ld,%ld\n", createtime.tv_sec, createtime.tv_nsec);
395 #endif
396 timespec2str(b, sizeof(b), &createtime);
397 printf("Create time of %s was %s\n", dev_name, b);
398 }
399
400 static int
tst_extended_ioctl(const char * fnp,int sg_fd,const char * fn2p,int sg_fd2,int sock,const char * cp)401 tst_extended_ioctl(const char * fnp, int sg_fd, const char * fn2p, int sg_fd2,
402 int sock, const char * cp)
403 {
404 uint32_t cflags;
405 struct sg_extended_info sei;
406 struct sg_extended_info * seip;
407
408 seip = &sei;
409 memset(seip, 0, sizeof(*seip));
410 seip->sei_wr_mask |= SG_SEIM_RESERVED_SIZE;
411 seip->reserved_sz = reserve_buff_sz;
412 seip->sgat_elem_sz = 64 * 1024;
413 seip->sei_rd_mask |= SG_SEIM_RESERVED_SIZE;
414 seip->sei_rd_mask |= SG_SEIM_TOT_FD_THRESH;
415 seip->sei_wr_mask |= SG_SEIM_CTL_FLAGS;
416 seip->sei_rd_mask |= SG_SEIM_CTL_FLAGS; /* this or previous optional */
417 seip->sei_rd_mask |= SG_SEIM_MINOR_INDEX;
418 seip->sei_wr_mask |= SG_SEIM_SGAT_ELEM_SZ;
419 seip->ctl_flags_wr_mask |= SG_CTL_FLAGM_TIME_IN_NS;
420 seip->ctl_flags_rd_mask |= SG_CTL_FLAGM_TIME_IN_NS;
421 seip->ctl_flags_rd_mask |= SG_CTL_FLAGM_OTHER_OPENS;
422 seip->ctl_flags_rd_mask |= SG_CTL_FLAGM_ORPHANS;
423 seip->ctl_flags_rd_mask |= SG_CTL_FLAGM_Q_TAIL;
424 seip->ctl_flags_rd_mask |= SG_CTL_FLAGM_IS_SHARE;
425 seip->ctl_flags_rd_mask |= SG_CTL_FLAGM_IS_READ_SIDE;
426 seip->ctl_flags_rd_mask |= SG_CTL_FLAGM_UNSHARE;
427 seip->ctl_flags_rd_mask |= SG_CTL_FLAGM_READ_SIDE_FINI;
428 seip->ctl_flags_rd_mask |= SG_CTL_FLAGM_READ_SIDE_ERR;
429 seip->ctl_flags_wr_mask |= SG_CTL_FLAGM_NO_DURATION;
430 seip->ctl_flags_rd_mask |= SG_CTL_FLAGM_NO_DURATION;
431 seip->ctl_flags |= SG_CTL_FLAGM_TIME_IN_NS;
432 seip->ctl_flags |= SG_CTL_FLAGM_NO_DURATION;
433
434 if (ioctl(sg_fd, SG_SET_GET_EXTENDED, seip) < 0) {
435 pr2serr("ioctl(SG_SET_GET_EXTENDED) failed, errno=%d %s\n", errno,
436 strerror(errno));
437 return 1;
438 }
439 #if 1
440 printf("%sSG_SET_GET_EXTENDED ioctl ok\n", cp);
441 if (SG_SEIM_RESERVED_SIZE & seip->sei_rd_mask)
442 printf(" %sreserved size: %u\n", cp, seip->reserved_sz);
443 if (SG_SEIM_MINOR_INDEX & seip->sei_rd_mask)
444 printf(" %sminor index: %u\n", cp, seip->minor_index);
445 if (SG_SEIM_TOT_FD_THRESH & seip->sei_rd_mask)
446 printf(" %stot_fd_thresh: %u\n", cp, seip->tot_fd_thresh);
447 if ((SG_SEIM_CTL_FLAGS & seip->sei_rd_mask) ||
448 (SG_SEIM_CTL_FLAGS & seip->sei_wr_mask)) {
449 cflags = seip->ctl_flags;
450 if (SG_CTL_FLAGM_TIME_IN_NS & seip->ctl_flags_rd_mask)
451 printf(" %sTIME_IN_NS: %s\n", cp,
452 (SG_CTL_FLAGM_TIME_IN_NS & cflags) ? "true" : "false");
453 if (SG_CTL_FLAGM_OTHER_OPENS & seip->ctl_flags_rd_mask)
454 printf(" %sOTHER_OPENS: %s\n", cp,
455 (SG_CTL_FLAGM_OTHER_OPENS & cflags) ? "true" : "false");
456 if (SG_CTL_FLAGM_ORPHANS & seip->ctl_flags_rd_mask)
457 printf(" %sORPHANS: %s\n", cp,
458 (SG_CTL_FLAGM_ORPHANS & cflags) ? "true" : "false");
459 if (SG_CTL_FLAGM_Q_TAIL & seip->ctl_flags_rd_mask)
460 printf(" %sQ_TAIL: %s\n", cp,
461 (SG_CTL_FLAGM_Q_TAIL & cflags) ? "true" : "false");
462 if (SG_CTL_FLAGM_IS_SHARE & seip->ctl_flags_rd_mask)
463 printf(" %sIS_SHARE: %s\n", cp,
464 (SG_CTL_FLAGM_IS_SHARE & cflags) ? "true" : "false");
465 if (SG_CTL_FLAGM_IS_READ_SIDE & seip->ctl_flags_rd_mask)
466 printf(" %sIS_READ_SIDE: %s\n", cp,
467 (SG_CTL_FLAGM_IS_READ_SIDE & cflags) ? "true" : "false");
468 if (SG_CTL_FLAGM_UNSHARE & seip->ctl_flags_rd_mask)
469 printf(" %sUNSHARE: %s\n", cp,
470 (SG_CTL_FLAGM_UNSHARE & cflags) ? "true" : "false");
471 if (SG_CTL_FLAGM_READ_SIDE_FINI & seip->ctl_flags_rd_mask)
472 printf(" %sREAD_SIDE_FINI: %s\n", cp,
473 (SG_CTL_FLAGM_READ_SIDE_FINI & cflags) ? "true" : "false");
474 if (SG_CTL_FLAGM_READ_SIDE_ERR & seip->ctl_flags_rd_mask)
475 printf(" %sREAD_SIDE_ERR: %s\n", cp,
476 (SG_CTL_FLAGM_READ_SIDE_ERR & cflags) ? "true" : "false");
477 if (SG_CTL_FLAGM_NO_DURATION & seip->ctl_flags_rd_mask)
478 printf(" %sNO_DURATION: %s\n", cp,
479 (SG_CTL_FLAGM_NO_DURATION & cflags) ? "true" : "false");
480 }
481 if (SG_SEIM_MINOR_INDEX & seip->sei_rd_mask)
482 printf(" %sminor_index: %u\n", cp, seip->minor_index);
483 printf("\n");
484 #endif
485
486 memset(seip, 0, sizeof(*seip));
487 seip->sei_wr_mask |= SG_SEIM_READ_VAL;
488 seip->sei_rd_mask |= SG_SEIM_READ_VAL;
489 seip->read_value = SG_SEIRV_INT_MASK;
490 if (ioctl(sg_fd, SG_SET_GET_EXTENDED, seip) < 0) {
491 pr2serr("ioctl(SG_SET_GET_EXTENDED) failed, errno=%d %s\n", errno,
492 strerror(errno));
493 return 1;
494 }
495 printf(" %sread_value[SG_SEIRV_INT_MASK]= %u\n", cp, seip->read_value);
496
497 memset(seip, 0, sizeof(*seip));
498 seip->sei_wr_mask |= SG_SEIM_READ_VAL;
499 seip->sei_rd_mask |= SG_SEIM_READ_VAL;
500 seip->read_value = SG_SEIRV_BOOL_MASK;
501 if (ioctl(sg_fd, SG_SET_GET_EXTENDED, seip) < 0) {
502 pr2serr("ioctl(SG_SET_GET_EXTENDED) failed, errno=%d %s\n", errno,
503 strerror(errno));
504 return 1;
505 }
506 printf(" %sread_value[SG_SEIRV_BOOL_MASK]= %u\n", cp, seip->read_value);
507
508 memset(seip, 0, sizeof(*seip));
509 seip->sei_wr_mask |= SG_SEIM_READ_VAL;
510 seip->sei_rd_mask |= SG_SEIM_READ_VAL;
511 seip->read_value = SG_SEIRV_VERS_NUM;
512 if (ioctl(sg_fd, SG_SET_GET_EXTENDED, seip) < 0) {
513 pr2serr("ioctl(SG_SET_GET_EXTENDED) failed, errno=%d %s\n", errno,
514 strerror(errno));
515 return 1;
516 }
517 printf(" %sread_value[SG_SEIRV_VERS_NUM]= %u\n", cp, seip->read_value);
518
519 memset(seip, 0, sizeof(*seip));
520 seip->sei_wr_mask |= SG_SEIM_READ_VAL;
521 seip->sei_rd_mask |= SG_SEIM_READ_VAL;
522 seip->read_value = SG_SEIRV_INACT_RQS;
523 if (ioctl(sg_fd, SG_SET_GET_EXTENDED, seip) < 0) {
524 pr2serr("ioctl(SG_SET_GET_EXTENDED) failed, errno=%d %s\n", errno,
525 strerror(errno));
526 return 1;
527 }
528 printf(" %sread_value[SG_SEIRV_INACT_RQS]= %u\n", cp, seip->read_value);
529
530 memset(seip, 0, sizeof(*seip));
531 seip->sei_wr_mask |= SG_SEIM_READ_VAL;
532 seip->sei_rd_mask |= SG_SEIM_READ_VAL;
533 seip->read_value = SG_SEIRV_DEV_INACT_RQS;
534 if (ioctl(sg_fd, SG_SET_GET_EXTENDED, seip) < 0) {
535 pr2serr("ioctl(SG_SET_GET_EXTENDED) failed, errno=%d %s\n", errno,
536 strerror(errno));
537 return 1;
538 }
539 printf(" %sread_value[SG_SEIRV_DEV_INACT_RQS]= %u\n", cp,
540 seip->read_value);
541
542 memset(seip, 0, sizeof(*seip));
543 seip->sei_wr_mask |= SG_SEIM_READ_VAL;
544 seip->sei_rd_mask |= SG_SEIM_READ_VAL;
545 seip->read_value = SG_SEIRV_SUBMITTED;
546 if (ioctl(sg_fd, SG_SET_GET_EXTENDED, seip) < 0) {
547 pr2serr("ioctl(SG_SET_GET_EXTENDED) failed, errno=%d %s\n", errno,
548 strerror(errno));
549 return 1;
550 }
551 printf(" %sread_value[SG_SEIRV_SUBMITTED]= %u\n", cp, seip->read_value);
552
553 memset(seip, 0, sizeof(*seip));
554 seip->sei_wr_mask |= SG_SEIM_READ_VAL;
555 seip->sei_rd_mask |= SG_SEIM_READ_VAL;
556 seip->read_value = SG_SEIRV_DEV_SUBMITTED;
557 if (ioctl(sg_fd, SG_SET_GET_EXTENDED, seip) < 0) {
558 pr2serr("ioctl(SG_SET_GET_EXTENDED) failed, errno=%d %s\n", errno,
559 strerror(errno));
560 return 1;
561 }
562 printf(" %sread_value[SG_SEIRV_DEV_SUBMITTED]= %u\n", cp,
563 seip->read_value);
564
565 memset(seip, 0, sizeof(*seip));
566 seip->sei_wr_mask |= SG_SEIM_SHARE_FD;
567 seip->sei_rd_mask |= SG_SEIM_SHARE_FD;
568 #if 1
569 seip->share_fd = sg_fd2;
570 #else
571 seip->share_fd = sg_fd;
572 #endif
573 if (do_fork && is_parent)
574 goto bypass_share;
575 if (ioctl(sg_fd, SG_SET_GET_EXTENDED, seip) < 0) {
576 pr2serr("%sioctl(SG_SET_GET_EXTENDED) shared_fd=%d, failed errno=%d "
577 "%s\n", cp, sg_fd2, errno, strerror(errno));
578 }
579 printf(" %sshare successful, read back previous shared_fd= %d\n", cp,
580 (int)seip->share_fd);
581 bypass_share:
582
583 if (ioctl(sg_fd, SG_GET_TRANSFORM, NULL) < 0)
584 pr2serr("ioctl(SG_GET_TRANSFORM) fail expected, errno=%d %s\n",
585 errno, strerror(errno));
586 else
587 printf("%sSG_GET_TRANSFORM okay (does nothing)\n", cp);
588 if (ioctl(sg_fd, SG_SET_TRANSFORM, NULL) < 0)
589 pr2serr("ioctl(SG_SET_TRANSFORM) fail expected, errno=%d %s\n",
590 errno, strerror(errno));
591 else
592 printf("%sSG_SET_TRANSFORM okay (does nothing)\n", cp);
593 printf("\n");
594
595 /* test sending a sg file descriptor between 2 processes using UNIX
596 * sockets */
597 if (do_fork && is_parent && fnp && (sock >= 0)) { /* master/READ side */
598 int res;
599 int fd_ma = open(fnp, O_RDWR);
600
601 if (fd_ma < 0) {
602 pr2serr("%s: opening %s failed: %s\n", __func__, fnp,
603 strerror(errno));
604 return 1;
605 }
606 res = sock_fd_write(sock, "boo", 4, fd_ma);
607 if (res < 0)
608 pr2serr("%s: sock_fd_write() failed\n", __func__);
609 else
610 printf("%s: sock_fd_write() returned: %d\n", __func__, res);
611 } else if (do_fork && !is_parent && fn2p && (sock >= 0)) {
612 int res, fd_ma;
613 /* int fd_sl = open(fn2p, O_RDWR); not needed */
614 uint8_t b[32];
615
616 fd_ma = -1;
617 res = sock_fd_read(sock, b, sizeof(b), &fd_ma);
618 if (res < 0)
619 pr2serr("%s: sock_fd_read() failed\n", __func__);
620 else
621 printf("%s: sock_fd_read() returned: %d, fd_ma=%d\n", __func__,
622 res, fd_ma);
623 /* yes it works! */
624 }
625 return 0;
626 }
627
628 static int
do_mrqs(int sg_fd,int sg_fd2,int mrqs)629 do_mrqs(int sg_fd, int sg_fd2, int mrqs)
630 {
631 bool both = (sg_fd2 >= 0);
632 int k, j, arr_v4_sz, good;
633 int res = 0;
634 struct sg_io_v4 * arr_v4;
635 struct sg_io_v4 * h4p;
636 struct sg_io_v4 * mrq_h4p;
637 struct sg_io_v4 mrq_h4;
638 uint8_t sense_buffer[SENSE_BUFFER_LEN] SG_C_CPP_ZERO_INIT;
639 uint8_t inq_cdb[INQ_CMD_LEN] = /* Device Id VPD page */
640 {0x12, 0x1, 0x83, 0, INQ_REPLY_LEN, 0};
641 uint8_t sdiag_cdb[SDIAG_CMD_LEN] =
642 {0x1d, 0x10 /* PF */, 0, 0, 0, 0};
643 uint8_t inqBuff[INQ_REPLY_LEN];
644
645 if (both) {
646 struct sg_extended_info sei;
647 struct sg_extended_info * seip;
648
649 seip = &sei;
650 memset(seip, 0, sizeof(*seip));
651 seip->sei_wr_mask |= SG_SEIM_SHARE_FD;
652 seip->sei_rd_mask |= SG_SEIM_SHARE_FD;
653 seip->share_fd = sg_fd; /* master */
654 if (ioctl(sg_fd2, SG_SET_GET_EXTENDED, seip) < 0) {
655 res = errno;
656 pr2serr("ioctl(sg_fd2, SG_SET_GET_EXTENDED) shared_fd, "
657 "failed errno=%d %s\n", res, strerror(res));
658 return res;
659 }
660 }
661 memset(inqBuff, 0, sizeof(inqBuff));
662 mrq_h4p = &mrq_h4;
663 memset(mrq_h4p, 0, sizeof(*mrq_h4p));
664 mrq_h4p->guard = 'Q';
665 mrq_h4p->flags = SGV4_FLAG_MULTIPLE_REQS;
666 if (mrq_immed)
667 mrq_h4p->flags |= SGV4_FLAG_IMMED;
668 arr_v4 = (struct sg_io_v4 *)calloc(mrqs, sizeof(struct sg_io_v4));
669 if (NULL == arr_v4) {
670 res = ENOMEM;
671 goto fini;
672 }
673 arr_v4_sz = mrqs * sizeof(struct sg_io_v4);
674
675 for (k = 0; k < mrqs; ++k) {
676 h4p = arr_v4 + k;
677
678 h4p->guard = 'Q';
679 /* ->protocol and ->subprotocol are already zero */
680 /* io_hdr[k].iovec_count = 0; */ /* memset takes care of this */
681 if (0 == (k % 2)) {
682 h4p->request_len = sizeof(sdiag_cdb);
683 h4p->request = (uint64_t)(uintptr_t)sdiag_cdb;
684 /* all din and dout fields are zero */
685 } else {
686 h4p->request_len = sizeof(inq_cdb);
687 h4p->request = (uint64_t)(uintptr_t)inq_cdb;
688 h4p->din_xfer_len = INQ_REPLY_LEN;
689 h4p->din_xferp = (uint64_t)(uintptr_t)inqBuff;
690 if (both)
691 h4p->flags |= SGV4_FLAG_DO_ON_OTHER;
692 }
693 h4p->response = (uint64_t)(uintptr_t)sense_buffer;
694 h4p->max_response_len = sizeof(sense_buffer);
695 h4p->timeout = 20000; /* 20000 millisecs == 20 seconds */
696 h4p->request_extra = k + 3; /* so pack_id doesn't start at 0 */
697 /* default is to queue at head (in SCSI mid level) */
698 if (q_at_tail)
699 h4p->flags |= SG_FLAG_Q_AT_TAIL;
700 else
701 h4p->flags |= SG_FLAG_Q_AT_HEAD;
702 }
703 mrq_h4p->dout_xferp = (uint64_t)(uintptr_t)arr_v4;
704 mrq_h4p->dout_xfer_len = arr_v4_sz;
705 mrq_h4p->din_xferp = mrq_h4p->dout_xferp;
706 mrq_h4p->din_xfer_len = mrq_h4p->dout_xfer_len;
707 if (ioctl(sg_fd, (mrq_iosubmit ? SG_IOSUBMIT : SG_IO), mrq_h4p) < 0) {
708 res = errno;
709 pr2serr("ioctl(SG_IO%s, mrq) failed, errno=%d %s\n",
710 (mrq_iosubmit ? "SUBMIT" : ""), res, strerror(res));
711 goto fini;
712 }
713 if ((mrq_h4p->dout_resid > 0) || ((int)mrq_h4p->info < mrqs))
714 pr2serr("ioctl(SG_IO%s, mrq) dout_resid=%d, info=%d\n\n",
715 (mrq_iosubmit ? "SUBMIT" : ""), mrq_h4p->dout_resid,
716 mrq_h4p->info);
717
718 good = 0;
719 j = 0;
720 if (mrq_immed) {
721 receive_more:
722 if (mrq_half_immed)
723 mrq_h4p->flags = SGV4_FLAG_MULTIPLE_REQS; // zap SGV4_FLAG_IMMED
724 if (ioctl(sg_fd, SG_IORECEIVE, mrq_h4p) < 0) {
725 res = errno;
726 pr2serr("ioctl(SG_IORECEIVE, mrq) failed, errno=%d %s\n",
727 res, strerror(res));
728 goto fini;
729 }
730 if ((mrq_h4p->din_resid > 0) || ((int)mrq_h4p->info < mrqs))
731 pr2serr("ioctl(SG_IORECEIVE, mrq) din_resid=%d, info=%d\n",
732 mrq_h4p->din_resid, mrq_h4p->info);
733 }
734
735 for (k = 0; k < (int)mrq_h4p->info; ++k, ++j) {
736 h4p = arr_v4 + k;
737 if (! (h4p->driver_status || h4p->transport_status ||
738 h4p->device_status)) {
739 if (h4p->info & SG_INFO_MRQ_FINI)
740 ++good;
741 }
742 if ((! (h4p->info & SG_INFO_MRQ_FINI)) && (verbose > 1))
743 pr2serr("%s: k=%d: SG_INFO_MRQ_FINI not set on response\n",
744 __func__, k);
745 }
746 if (mrq_immed && (j < mrqs))
747 goto receive_more;
748
749 if (good > 0) {
750 printf("Final INQUIRY response:\n");
751 hex2stdout(inqBuff, INQ_REPLY_LEN, 0);
752 }
753 printf("Good responses: %d, bad responses: %d\n", good, mrqs - good);
754 if (mrq_h4p->driver_status != 0)
755 printf("Master mrq object: driver_status=%d\n",
756 mrq_h4p->driver_status);
757 h4p = arr_v4 + mrqs - 1;
758 if (h4p->driver_status != 0)
759 printf("Last mrq object: driver_status=%d\n", h4p->driver_status);
760
761 fini:
762 if (arr_v4)
763 free(arr_v4);
764 return res;
765 }
766
767
768 int
main(int argc,char * argv[])769 main(int argc, char * argv[])
770 {
771 bool done, is_first;
772 bool nw_given = false;
773 bool has_dname_range = false;
774 int k, ok, pack_id, num_waiting;
775 int res = 0;
776 int sum_nw = 0;
777 int sg_fd = -1;
778 int sg_fd2 = -1;
779 int sock = -1;
780 uint8_t inq_cdb[INQ_CMD_LEN] =
781 {0x12, 0, 0, 0, INQ_REPLY_LEN, 0};
782 uint8_t sdiag_cdb[SDIAG_CMD_LEN] =
783 {0x1d, 0x10 /* PF */, 0, 0, 0, 0};
784 uint8_t inqBuff[MAX_Q_LEN][INQ_REPLY_LEN];
785 sg_io_hdr_t io_hdr[MAX_Q_LEN];
786 sg_io_hdr_t rio_hdr;
787 char ebuff[EBUFF_SZ];
788 char dname[256];
789 uint8_t sense_buffer[MAX_Q_LEN][SENSE_BUFFER_LEN] SG_C_CPP_ZERO_INIT;
790 const char * second_fname = NULL;
791 const char * cp;
792 char * chp;
793 struct sg_scsi_id ssi;
794
795
796 if (sizeof(struct sg_extended_info) != 96)
797 pr2serr("Warning <<<< sizeof(struct sg_extended_info)=%zu not 96\n",
798 sizeof(struct sg_extended_info));
799 for (k = 1; k < argc; ++k) {
800 if (0 == memcmp("-3", argv[k], 2))
801 do_v3_only = true;
802 else if (0 == memcmp("-c", argv[k], 2))
803 create_time = true;
804 else if (0 == memcmp("-f", argv[k], 2))
805 do_fork = true;
806 else if (0 == memcmp("-h", argv[k], 2)) {
807 file_name = 0;
808 break;
809 } else if (0 == memcmp("-I=", argv[k], 3)) {
810 iterator_test = atoi(argv[k] + 3);
811 if ((iterator_test > 1) || (iterator_test < -1)) {
812 printf("Expect -I= to take a number, either 0 or 1\n");
813 file_name = 0;
814 break;
815 }
816 } else if (0 == memcmp("-J=", argv[k], 3)) {
817 object_walk_test = atoi(argv[k] + 3);
818 if ((object_walk_test > 1) || (object_walk_test < -1)) {
819 printf("Expect -J= to take a number, either 0 or 1\n");
820 file_name = 0;
821 break;
822 }
823 } else if (0 == memcmp("-l=", argv[k], 3)) {
824 q_len = atoi(argv[k] + 3);
825 if ((q_len > 511) || (q_len < 1)) {
826 printf("Expect -l= to take a number (q length) between 1 "
827 "and 511\n");
828 file_name = 0;
829 break;
830 }
831 } else if (0 == memcmp("-m=", argv[k], 3)) {
832 num_mrqs = sg_get_num(argv[k] + 3);
833 if (num_mrqs < 1) {
834 printf("Expect -m= to take a number greater than 0\n");
835 file_name = 0;
836 break;
837 }
838 if ((cp = strchr(argv[k] + 3, ','))) {
839 mrq_iosubmit = true;
840 if (cp[1] == 'I')
841 mrq_immed = true;
842 else if (cp[1] == 'i') {
843 mrq_immed = true;
844 mrq_half_immed = true;
845 } else if (toupper(cp[1]) == 'S')
846 ;
847 else {
848 printf("-m= option expects 'A' or 'a' as a suffix, "
849 "after comma\n");
850 file_name = 0;
851 break;
852 }
853 }
854 } else if (0 == memcmp("-M", argv[k], 2))
855 more_async = true;
856 else if (0 == memcmp("-n", argv[k], 2))
857 no_duration = true;
858 else if (0 == memcmp("-o", argv[k], 2))
859 ioctl_only = true;
860 else if (0 == memcmp("-r=", argv[k], 3)) {
861 reserve_buff_sz = atoi(argv[k] + 3);
862 if (reserve_buff_sz < 0) {
863 printf("Expect -r= to take a number 0 or higher\n");
864 file_name = 0;
865 break;
866 }
867 } else if (0 == memcmp("-s=", argv[k], 3)) {
868 sleep_secs = atoi(argv[k] + 3);
869 if (sleep_secs < 0) {
870 printf("Expect -s= to take a number 0 or higher\n");
871 file_name = 0;
872 break;
873 }
874 } else if (0 == memcmp("-S", argv[k], 2))
875 show_size_value = true;
876 else if (0 == memcmp("-t", argv[k], 2))
877 q_at_tail = true;
878 else if (0 == memcmp("-T=", argv[k], 3)) {
879 num_sgnw = sg_get_num(argv[k] + 3);
880 if (num_sgnw < 0) {
881 printf("Expect -T= to take a number >= 0\n");
882 file_name = 0;
883 break;
884 }
885 nw_given = true;
886 } else if (0 == memcmp("-vvvvvvv", argv[k], 8))
887 verbose += 7;
888 else if (0 == memcmp("-vvvvvv", argv[k], 7))
889 verbose += 6;
890 else if (0 == memcmp("-vvvvv", argv[k], 6))
891 verbose += 5;
892 else if (0 == memcmp("-vvvv", argv[k], 5))
893 verbose += 4;
894 else if (0 == memcmp("-vvv", argv[k], 4))
895 verbose += 3;
896 else if (0 == memcmp("-vv", argv[k], 3))
897 verbose += 2;
898 else if (0 == memcmp("-v", argv[k], 2))
899 verbose += 1;
900 else if (0 == memcmp("-V", argv[k], 2)) {
901 printf("%s\n", version_str);
902 return 0;
903 } else if (0 == memcmp("-w", argv[k], 2))
904 write_only = true;
905 else if (*argv[k] == '-') {
906 printf("Unrecognized switch: %s\n", argv[k]);
907 file_name = 0;
908 break;
909 }
910 else if (0 == file_name)
911 file_name = argv[k];
912 else if (NULL == second_fname)
913 second_fname = argv[k];
914 else {
915 printf("too many arguments\n");
916 file_name = 0;
917 break;
918 }
919 }
920 if ((iterator_test >= 0) || (object_walk_test >= 0))
921 nw_given = false;
922
923 if (show_size_value) {
924 struct utsname unam;
925
926 printf("Size in bytes:\n");
927 printf("\t%zu\tsizeof(struct sg_header) Version 2 interface "
928 "structure\n", sizeof(struct sg_header));
929 printf("\t%zu\tsizeof(struct sg_io_hdr) Version 3 interface "
930 "structure\n", sizeof(struct sg_io_hdr));
931 printf("\t%zu\tsizeof(struct sg_io_v4) Version 4 interface "
932 "structure\n", sizeof(struct sg_io_v4));
933 printf("\t%zu\tsizeof(struct sg_iovec) scatter gather element\n",
934 sizeof(struct sg_iovec));
935 printf("\t%zu\tsizeof(struct sg_scsi_id) topological device id\n",
936 sizeof(struct sg_scsi_id));
937 printf("\t%zu\tsizeof(struct sg_req_info) request information\n",
938 sizeof(struct sg_req_info));
939 printf("\t%zu\tsizeof(struct sg_extended_info) for "
940 "SG_SET_GET_EXTENDED\n",
941 sizeof(struct sg_extended_info));
942 printf("\nioctl values (i.e. second argument to ioctl()):\n");
943 printf("\t0x%lx\t\tvalue of SG_GET_NUM_WAITING ioctl\n",
944 (unsigned long)SG_GET_NUM_WAITING);
945 printf("\t0x%lx\t\tvalue of SG_IO ioctl\n",
946 (unsigned long)SG_IO);
947 printf("\t0x%lx\tvalue of SG_IOABORT ioctl\n",
948 (unsigned long)SG_IOABORT);
949 printf("\t0x%lx\tvalue of SG_IORECEIVE ioctl\n",
950 (unsigned long)SG_IORECEIVE);
951 printf("\t0x%lx\tvalue of SG_IORECEIVE_V3 ioctl\n",
952 (unsigned long)SG_IORECEIVE_V3);
953 printf("\t0x%lx\tvalue of SG_IOSUBMIT ioctl\n",
954 (unsigned long)SG_IOSUBMIT);
955 printf("\t0x%lx\tvalue of SG_IOSUBMIT_V3 ioctl\n",
956 (unsigned long)SG_IOSUBMIT_V3);
957 printf("\t0x%lx\tvalue of SG_SET_GET_EXTENDED ioctl\n",
958 (unsigned long)SG_SET_GET_EXTENDED);
959 printf("\n\t0x%x\t\tbase value of most SG_* ioctls\n",
960 SG_IOCTL_MAGIC_NUM);
961 printf("\nsizeof(void *) [a pointer] on this machine: %u bytes\n",
962 (unsigned)sizeof(void *));
963 if (0 == uname(&unam))
964 printf("Machine name: %s\n", unam.machine);
965
966 return 0;
967 }
968 if (0 == file_name) {
969 printf("No filename (sg device) given\n\n");
970 usage();
971 return 1;
972 }
973 memset(dname, 0, sizeof(dname));
974 if (strlen(file_name) > 255) {
975 fprintf(stderr, "file_name too long\n");
976 goto out;
977 }
978 strncpy(dname, file_name, sizeof(dname) - 1);
979 if ((chp = strchr(dname, '-'))) {
980 if (1 != sscanf(chp + 1, "%d", &dname_last)) {
981 fprintf(stderr, "can't code number after '-' in file_name\n");
982 goto out;
983 }
984 *chp = '\0';
985 --chp;
986 while (isdigit(*chp))
987 --chp;
988 ++chp;
989 if (1 != sscanf(chp, "%d", &dname_current)) {
990 fprintf(stderr, "can't code number before '-' in file_name\n");
991 goto out;
992 }
993 *chp = '\0';
994 has_dname_range = true;
995 dname_pos = strlen(dname);
996 }
997 is_first = true;
998
999 dname_range_loop:
1000 if (has_dname_range)
1001 sprintf(dname + dname_pos, "%d", dname_current);
1002
1003 /* An access mode of O_RDWR is required for write()/read() interface */
1004 if ((sg_fd = open(dname, O_RDWR)) < 0) {
1005 snprintf(ebuff, EBUFF_SZ, "error opening file: %s", dname);
1006 perror(ebuff);
1007 return 1;
1008 }
1009 if (verbose)
1010 fprintf(stderr, "opened given file: %s successfully, fd=%d\n",
1011 dname, sg_fd);
1012
1013 if (ioctl(sg_fd, SG_GET_VERSION_NUM, &sg_drv_ver_num) < 0) {
1014 pr2serr("ioctl(SG_GET_VERSION_NUM) failed, errno=%d %s\n", errno,
1015 strerror(errno));
1016 goto out;
1017 }
1018 if (is_first)
1019 printf("Linux sg driver version: %d\n", sg_drv_ver_num);
1020
1021 if (create_time && (sg_drv_ver_num > 40030)) {
1022 pr_create_dev_time(sg_fd, dname);
1023 goto out;
1024 }
1025
1026 if (nw_given || (iterator_test >= 0) || (object_walk_test >= 0)) {
1027 /* -T=NUM and/or -I=0|1 or -j=0|1 */
1028 /* time ioctl(SG_GET_NUM_WAITING) or do iterator_test */
1029 int nw;
1030 struct timespec start_tm, fin_tm, res_tm;
1031
1032 if (is_first) {
1033 int rang = has_dname_range ? (1 + dname_last - dname_current) : 1;
1034
1035 is_first = false;
1036 if (nw_given)
1037 printf("Timing %d x %d calls to ioctl(SG_GET_NUM_WAITING)\n",
1038 rang, num_sgnw);
1039 else if (iterator_test >= 0) {
1040 k = num_sgnw + 1000;
1041 printf("Timing %d calls to ioctl(SG_SET_DEBUG, %d)\n",
1042 rang, ((0 == iterator_test) ? -k : k));
1043 } else
1044 printf("Timing %d calls to ioctl(SG_SET_DEBUG, %d)\n",
1045 rang, (object_walk_test == 0) ? 999 : -999);
1046 if (0 != clock_gettime(CLOCK_MONOTONIC, &start_tm)) {
1047 res = errno;
1048 perror("start clock_gettime() failed:");
1049 goto out;
1050 }
1051 }
1052 if (nw_given) {
1053 for (k = 0; k < num_sgnw; ++k, sum_nw += nw) {
1054 if (ioctl(sg_fd, SG_GET_NUM_WAITING, &nw) < 0) {
1055 res = errno;
1056 fprintf(stderr, "%d: ioctl(SG_GET_NUM_WAITING) failed "
1057 "errno=%d\n", k, res);
1058 goto out;
1059 }
1060 }
1061 } else if (iterator_test >= 0) {
1062 int fd, pid;
1063
1064 k = num_sgnw + 1000;
1065 if (0 == iterator_test)
1066 k = -k;
1067 if (second_fname) {
1068 if ((sg_fd2 = open(second_fname, O_RDWR)) < 0) {
1069 snprintf(ebuff, EBUFF_SZ, "%s: error opening file: %s",
1070 __func__, second_fname);
1071 perror(ebuff);
1072 return 1;
1073 }
1074 printf("About to fork due to second filename\n");
1075 pid = fork();
1076 if (pid < 0) {
1077 perror("fork() failed");
1078 goto out;
1079 } else if (0 == pid) {
1080 relative_cp = "child: ";
1081 is_parent = false;
1082 fd = sg_fd2;
1083 } else {
1084 relative_cp = "parent: ";
1085 is_parent = true;
1086 childs_pid = pid;
1087 fd = sg_fd;
1088 }
1089 } else {
1090 fd = sg_fd;
1091 relative_cp = "";
1092 }
1093 if (ioctl(fd, SG_SET_DEBUG, &k) < 0) {
1094 res = errno;
1095 fprintf(stderr, "%s%d: ioctl(SG_SET_DEBUG) failed errno=%d\n",
1096 relative_cp, k, res);
1097 goto out;
1098 } else if (verbose)
1099 fprintf(stderr, "%siterator_test good ioctl(SG_SET_DEBUG, "
1100 "%d)\n", relative_cp, k);
1101 sum_nw += num_sgnw;
1102 } else if (object_walk_test >= 0) {
1103 const char * ccp = "object_walk_test";
1104
1105 relative_cp = "";
1106 k = (object_walk_test == 0) ? 999 : -999;
1107 if (ioctl(sg_fd, SG_SET_DEBUG, &k) < 0) {
1108 res = errno;
1109 fprintf(stderr, "%s: ioctl(SG_SET_DEBUG, %d) failed "
1110 "errno=%d\n", ccp, k, res);
1111 } else if (verbose)
1112 fprintf(stderr, "%s: good call to ioctl(SG_SET_DEBUG, %d)\n",
1113 ccp, k);
1114 sum_nw += 10000; /* (1_up-scan + 2_lookups) * 10,000 times */
1115 }
1116
1117 if (has_dname_range) {
1118 ++dname_current;
1119 if (dname_current <= dname_last) {
1120 if (sg_fd >= 0)
1121 close(sg_fd);
1122 goto dname_range_loop;
1123 }
1124 }
1125 if (0 != clock_gettime(CLOCK_MONOTONIC, &fin_tm)) {
1126 res = errno;
1127 perror("finish clock_gettime() failed:");
1128 goto out;
1129 }
1130 res_tm.tv_sec = fin_tm.tv_sec - start_tm.tv_sec;
1131 res_tm.tv_nsec = fin_tm.tv_nsec - start_tm.tv_nsec;
1132 if (res_tm.tv_nsec < 0) {
1133 --res_tm.tv_sec;
1134 res_tm.tv_nsec += 1000000000;
1135 }
1136 if (verbose) {
1137 if (nw_given && (verbose > 1))
1138 printf("sum of num_waiting_s=%d\n", sum_nw);
1139 printf("%selapsed time (nanosecond precision): %d.%09d secs\n",
1140 relative_cp, (int)res_tm.tv_sec, (int)res_tm.tv_nsec);
1141 } else
1142 printf("%selapsed time: %d.%06d secs\n", relative_cp,
1143 (int)res_tm.tv_sec, (int)(res_tm.tv_nsec / 1000));
1144 if (num_sgnw >= 100) {
1145 double m = (double)res_tm.tv_sec +
1146 ((double)res_tm.tv_nsec / 1000000000.0);
1147 double num = num_sgnw;
1148
1149 if (m > 0.000001)
1150 printf("%sCalls per second: %.2f\n", relative_cp, num / m);
1151 }
1152 res = 0;
1153 goto out;
1154 }
1155 if ((more_async || no_duration) && !do_v3_only)
1156 set_more_async(sg_fd, more_async, no_duration);
1157
1158 if (second_fname) {
1159 if ((sg_fd2 = open(second_fname, O_RDWR)) < 0) {
1160 snprintf(ebuff, EBUFF_SZ,
1161 "%s: error opening file: %s", __func__, second_fname);
1162 perror(ebuff);
1163 return 1;
1164 }
1165 if (verbose)
1166 fprintf(stderr, "opened second file: %s successfully, fd=%d\n",
1167 second_fname, sg_fd2);
1168 if (more_async && !do_v3_only)
1169 set_more_async(sg_fd2, more_async, no_duration);
1170 }
1171
1172 if ((num_mrqs > 0) && !do_v3_only) {
1173 res = do_mrqs(sg_fd, sg_fd2, num_mrqs);
1174 goto out;
1175 }
1176
1177 if (do_fork) {
1178 int pid;
1179 int sv[2];
1180
1181 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sv) < 0) {
1182 perror("socketpair");
1183 exit(1);
1184 }
1185 printf("socketpair: sv[0]=%d, sv[1]=%d sg_fd=%d\n", sv[0], sv[1],
1186 sg_fd);
1187 pid = fork();
1188 if (pid < 0) {
1189 perror("fork() failed");
1190 goto out;
1191 } else if (0 == pid) {
1192 relative_cp = "child ";
1193 is_parent = false;
1194 close(sv[0]);
1195 sock = sv[1];
1196 } else {
1197 relative_cp = "parent ";
1198 is_parent = true;
1199 childs_pid = pid;
1200 close(sv[1]);
1201 sock = sv[0];
1202 }
1203 }
1204
1205 cp = do_fork ? relative_cp : "";
1206 if (! do_v3_only && (sg_drv_ver_num > 40030)) {
1207 if (tst_extended_ioctl(dname, sg_fd, second_fname, sg_fd2, sock,
1208 cp))
1209 goto out;
1210 }
1211 if (ioctl_only)
1212 goto out;
1213
1214 if (do_fork && !is_parent)
1215 return 0;
1216
1217 printf("start write() calls [submits]\n");
1218 for (k = 0; k < q_len; ++k) {
1219 /* Prepare INQUIRY command */
1220 memset(&io_hdr[k], 0, sizeof(sg_io_hdr_t));
1221 io_hdr[k].interface_id = 'S';
1222 /* io_hdr[k].iovec_count = 0; */ /* memset takes care of this */
1223 io_hdr[k].mx_sb_len = (uint8_t)sizeof(sense_buffer);
1224 if (0 == (k % 3)) {
1225 io_hdr[k].cmd_len = sizeof(sdiag_cdb);
1226 io_hdr[k].cmdp = sdiag_cdb;
1227 io_hdr[k].dxfer_direction = SG_DXFER_NONE;
1228 } else {
1229 io_hdr[k].cmd_len = sizeof(inq_cdb);
1230 io_hdr[k].cmdp = inq_cdb;
1231 io_hdr[k].dxfer_direction = SG_DXFER_FROM_DEV;
1232 io_hdr[k].dxfer_len = INQ_REPLY_LEN;
1233 io_hdr[k].dxferp = inqBuff[k];
1234 }
1235 io_hdr[k].sbp = sense_buffer[k];
1236 io_hdr[k].mx_sb_len = SENSE_BUFFER_LEN;
1237 io_hdr[k].timeout = 20000; /* 20000 millisecs == 20 seconds */
1238 io_hdr[k].pack_id = k + 3; /* so pack_id doesn't start at 0 */
1239 /* default is to queue at head (in SCSI mid level) */
1240 if (q_at_tail)
1241 io_hdr[k].flags |= SG_FLAG_Q_AT_TAIL;
1242 else
1243 io_hdr[k].flags |= SG_FLAG_Q_AT_HEAD;
1244 /* io_hdr[k].usr_ptr = NULL; */
1245
1246 if (write(sg_fd, &io_hdr[k], sizeof(sg_io_hdr_t)) < 0) {
1247 pr2serr("%ssg write errno=%d [%s]\n", cp, errno, strerror(errno));
1248 close(sg_fd);
1249 return 1;
1250 }
1251 }
1252
1253 memset(&ssi, 0, sizeof(ssi));
1254 if (ioctl(sg_fd, SG_GET_SCSI_ID, &ssi) < 0)
1255 pr2serr("ioctl(SG_GET_SCSI_ID) failed, errno=%d %s\n",
1256 errno, strerror(errno));
1257 else {
1258 printf("host_no: %d\n", ssi.host_no);
1259 printf(" channel: %d\n", ssi.channel);
1260 printf(" scsi_id: %d\n", ssi.scsi_id);
1261 printf(" lun: %d\n", ssi.lun);
1262 printf(" pdt: %d\n", ssi.scsi_type);
1263 printf(" h_cmd_per_lun: %d\n", ssi.h_cmd_per_lun);
1264 printf(" d_queue_depth: %d\n", ssi.d_queue_depth);
1265 printf(" SCSI 8 byte LUN: ");
1266 hex2stdout(ssi.scsi_lun, 8, -1);
1267 }
1268 if (ioctl(sg_fd, SG_GET_PACK_ID, &pack_id) < 0)
1269 pr2serr("ioctl(SG_GET_PACK_ID) failed, errno=%d %s\n",
1270 errno, strerror(errno));
1271 else
1272 printf("first available pack_id: %d\n", pack_id);
1273 if (ioctl(sg_fd, SG_GET_NUM_WAITING, &num_waiting) < 0)
1274 pr2serr("ioctl(SG_GET_NUM_WAITING) failed, errno=%d %s\n",
1275 errno, strerror(errno));
1276 else
1277 printf("num_waiting: %d\n", num_waiting);
1278
1279 sleep(sleep_secs);
1280
1281 if (write_only)
1282 goto out;
1283
1284 if (do_fork)
1285 printf("\n\nFollowing starting with get_pack_id are all CHILD\n");
1286 if (ioctl(sg_fd, SG_GET_PACK_ID, &pack_id) < 0)
1287 pr2serr("ioctl(SG_GET_PACK_ID) failed, errno=%d %s\n",
1288 errno, strerror(errno));
1289 else
1290 printf("first available pack_id: %d\n", pack_id);
1291 if (ioctl(sg_fd, SG_GET_NUM_WAITING, &num_waiting) < 0)
1292 pr2serr("ioctl(SG_GET_NUM_WAITING) failed, errno=%d %s\n",
1293 errno, strerror(errno));
1294 else
1295 printf("num_waiting: %d\n", num_waiting);
1296
1297 printf("\nstart read() calls [io receive]\n");
1298 for (k = 0, done = false; k < q_len; ++k) {
1299 if ((! done) && (k == q_len / 2)) {
1300 done = true;
1301 printf("\n>>> half way through read\n");
1302 if (ioctl(sg_fd, SG_GET_PACK_ID, &pack_id) < 0)
1303 pr2serr("ioctl(SG_GET_PACK_ID) failed, errno=%d %s\n",
1304 errno, strerror(errno));
1305 else
1306 printf("first available pack_id: %d\n", pack_id);
1307 if (ioctl(sg_fd, SG_GET_NUM_WAITING, &num_waiting) < 0)
1308 pr2serr("ioctl(SG_GET_NUM_WAITING) failed, errno=%d %s\n",
1309 errno, strerror(errno));
1310 else
1311 printf("num_waiting: %d\n", num_waiting);
1312 }
1313 memset(&rio_hdr, 0, sizeof(sg_io_hdr_t));
1314 rio_hdr.interface_id = 'S';
1315 if (read(sg_fd, &rio_hdr, sizeof(sg_io_hdr_t)) < 0) {
1316 perror("sg read error");
1317 close(sg_fd);
1318 return 1;
1319 }
1320 /* now for the error processing */
1321 ok = 0;
1322 switch (sg_err_category3(&rio_hdr)) {
1323 case SG_LIB_CAT_CLEAN:
1324 ok = 1;
1325 break;
1326 case SG_LIB_CAT_RECOVERED:
1327 printf("Recovered error, continuing\n");
1328 ok = 1;
1329 break;
1330 default: /* won't bother decoding other categories */
1331 sg_chk_n_print3("command error", &rio_hdr, 1);
1332 break;
1333 }
1334
1335 if (ok) { /* output result if it is available */
1336 if (0 == (rio_hdr.pack_id % 3))
1337 printf("SEND DIAGNOSTIC %d duration=%u\n", rio_hdr.pack_id,
1338 rio_hdr.duration);
1339 else
1340 printf("INQUIRY %d duration=%u\n", rio_hdr.pack_id,
1341 rio_hdr.duration);
1342 }
1343 }
1344
1345 out:
1346 if (sg_fd >= 0)
1347 close(sg_fd);
1348 if (sg_fd2 >= 0)
1349 close(sg_fd2);
1350 return res;
1351 }
1352