blob: ade56a1d2cc5b45ee209079d9ef0bc415b586f88 [file] [log] [blame]
Douglas Gilbert4445eec2018-12-07 16:42:06 +00001/*
Douglas Gilbert6b32eac2022-02-03 02:38:23 +00002 * Copyright (C) 2018-2022 D. Gilbert
Douglas Gilbert4445eec2018-12-07 16:42:06 +00003 * 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
Douglas Gilbert01a118f2018-10-18 15:57:29 +000014#include <unistd.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
Douglas Gilbert072494a2019-03-26 03:54:53 +000018#include <stdarg.h>
19#include <stdbool.h>
20#include <ctype.h>
Douglas Gilbert01a118f2018-10-18 15:57:29 +000021#include <string.h>
22#include <errno.h>
Douglas Gilbert3dfb85f2019-06-13 05:15:40 +000023#include <time.h>
Douglas Gilbert01a118f2018-10-18 15:57:29 +000024#include <sys/ioctl.h>
25#include <sys/types.h>
26#include <sys/stat.h>
Douglas Gilbert28903a12019-05-15 14:30:29 +000027#include <sys/utsname.h>
Douglas Gilbert207e5252019-11-18 22:32:39 +000028#define __STDC_FORMAT_MACROS 1
29#include <inttypes.h>
Douglas Gilbert01a118f2018-10-18 15:57:29 +000030
Douglas Gilbert18c98522018-12-17 21:16:36 +000031#include <sys/socket.h> /* For passing fd_s via Unix sockets */
32
Douglas Gilbert2433a222019-01-11 06:11:25 +000033#ifndef HAVE_LINUX_SG_V4_HDR
34
Douglas Gilbertd6e191b2018-11-12 10:21:43 +000035/* 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 */
Douglas Gilbert01a118f2018-10-18 15:57:29 +000049
Douglas Gilbert2433a222019-01-11 06:11:25 +000050#else
51#define __user
52#endif /* end of: ifndef HAVE_LINUX_SG_V4_HDR */
53
Douglas Gilbert01a118f2018-10-18 15:57:29 +000054#include "sg_lib.h"
55#include "sg_io_linux.h"
56#include "sg_linux_inc.h"
57#include "sg_pr2serr.h"
58
Douglas Gilbert2433a222019-01-11 06:11:25 +000059/* This program tests ioctl() calls added and modified in version 4.0 and
Douglas Gilbert01a118f2018-10-18 15:57:29 +000060 * later of the Linux sg driver. */
61
Douglas Gilbert01a118f2018-10-18 15:57:29 +000062
Douglas Gilbert6b32eac2022-02-03 02:38:23 +000063static const char * version_str = "Version: 1.21 20220202";
Douglas Gilbertd6e191b2018-11-12 10:21:43 +000064
Douglas Gilbert072494a2019-03-26 03:54:53 +000065#define INQ_REPLY_LEN 128
Douglas Gilbert01a118f2018-10-18 15:57:29 +000066#define INQ_CMD_LEN 6
67#define SDIAG_CMD_LEN 6
68#define SENSE_BUFFER_LEN 96
69
Douglas Gilbert985b5cd2020-07-24 18:56:21 +000070#define EBUFF_SZ 512
Douglas Gilbert01a118f2018-10-18 15:57:29 +000071
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 */
Douglas Gilbert3e030c22019-05-18 15:37:19 +000081#define MAX_Q_LEN 512
Douglas Gilbert01a118f2018-10-18 15:57:29 +000082
Douglas Gilbertd6e191b2018-11-12 10:21:43 +000083#define DEF_RESERVE_BUFF_SZ (256 * 1024)
84
Douglas Gilbert207e5252019-11-18 22:32:39 +000085static bool create_time = false;
Douglas Gilbert18c98522018-12-17 21:16:36 +000086static bool is_parent = false;
87static bool do_fork = false;
88static bool ioctl_only = false;
Douglas Gilbert28903a12019-05-15 14:30:29 +000089static bool more_async = false;
Douglas Gilbert80c24fa2019-07-02 14:34:36 +000090static bool no_duration = false;
Douglas Gilbert18c98522018-12-17 21:16:36 +000091static bool q_at_tail = false;
92static bool write_only = false;
Douglas Gilbert072494a2019-03-26 03:54:53 +000093static bool mrq_immed = false; /* if set, also sets mrq_iosubmit */
Douglas Gilbert7dff7ae2019-04-19 18:15:45 +000094static bool mrq_half_immed = false;
Douglas Gilbert072494a2019-03-26 03:54:53 +000095static bool mrq_iosubmit = false;
Douglas Gilbert28903a12019-05-15 14:30:29 +000096static bool show_size_value = false;
Douglas Gilbert3e030c22019-05-18 15:37:19 +000097static bool do_v3_only = false;
Douglas Gilbert18c98522018-12-17 21:16:36 +000098
99static int childs_pid = 0;
Douglas Gilbert62c85c82020-06-05 16:19:48 +0000100static int iterator_test = -1;
101static int object_walk_test = -1;
Douglas Gilbert28903a12019-05-15 14:30:29 +0000102static int sg_drv_ver_num = 0;
Douglas Gilbert18c98522018-12-17 21:16:36 +0000103static int q_len = DEF_Q_LEN;
104static int sleep_secs = 0;
105static int reserve_buff_sz = DEF_RESERVE_BUFF_SZ;
Douglas Gilbert072494a2019-03-26 03:54:53 +0000106static int num_mrqs = 0;
Douglas Gilbert3dfb85f2019-06-13 05:15:40 +0000107static int num_sgnw = 0;
Douglas Gilbert985b5cd2020-07-24 18:56:21 +0000108static int dname_current = 0;
109static int dname_last = 0;
110static int dname_pos = 0;
Douglas Gilbert18c98522018-12-17 21:16:36 +0000111static int verbose = 0;
112
Douglas Gilberta37cbfd2020-09-03 15:32:35 +0000113static const char * relative_cp = "";
Douglas Gilbert207e5252019-11-18 22:32:39 +0000114static char * file_name = NULL;
Douglas Gilbert18c98522018-12-17 21:16:36 +0000115
Douglas Gilbert01a118f2018-10-18 15:57:29 +0000116
Douglas Gilbert81018502018-11-27 18:38:56 +0000117static void
118usage(void)
119{
Douglas Gilbert62c85c82020-06-05 16:19:48 +0000120 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"
Douglas Gilbert985b5cd2020-07-24 18:56:21 +0000125 " <sg_device>[-<num>] [<sg_device2>]\n"
Douglas Gilbert81018502018-11-27 18:38:56 +0000126 " where:\n"
Douglas Gilbert985b5cd2020-07-24 18:56:21 +0000127 " -3 use sg v3 interface (def: sg v4 if available)\n"
Douglas Gilbert207e5252019-11-18 22:32:39 +0000128 " -c timestamp when sg driver created <sg_device>\n"
Douglas Gilbert18c98522018-12-17 21:16:36 +0000129 " -f fork and test share between processes\n"
Douglas Gilbert81018502018-11-27 18:38:56 +0000130 " -h help: print usage message then exit\n"
Douglas Gilbert62c85c82020-06-05 16:19:48 +0000131 " -I=0|1 iterator test of mid-level; 0: unlocked, 1: "
132 "locked\n"
133 " does test -T=NUM times, outputs duration\n"
Douglas Gilbert985b5cd2020-07-24 18:56:21 +0000134 " -J=0|1 object walk up then 2 lookups; 0: no logging; "
135 "1: log\n"
136 " up-scan once per 1000 iterations\n"
Douglas Gilbert81018502018-11-27 18:38:56 +0000137 " -l=Q_LEN queue length, between 1 and 511 (def: 16)\n"
Douglas Gilbert072494a2019-03-26 03:54:53 +0000138 " -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"
Douglas Gilbert7dff7ae2019-04-19 18:15:45 +0000142 " 'i' IMMED on submission, non-IMMED on "
143 "receive;\n"
Douglas Gilbert072494a2019-03-26 03:54:53 +0000144 " 'S' is appended, then use "
145 "ioctl(SG_IOSUBMIT)\n"
Douglas Gilbert28903a12019-05-15 14:30:29 +0000146 " -M set 'more async' flag\n"
Douglas Gilbert80c24fa2019-07-02 14:34:36 +0000147 " -n do not calculate per command duration (def: do)\n"
Douglas Gilbert81018502018-11-27 18:38:56 +0000148 " -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"
Douglas Gilbert28903a12019-05-15 14:30:29 +0000152 " -S size of interface structures plus ioctl "
153 "values\n"
Douglas Gilbert81018502018-11-27 18:38:56 +0000154 " -t queue_at_tail (def: q_at_head)\n"
Douglas Gilbert3dfb85f2019-06-13 05:15:40 +0000155 " -T=NUM time overhead of NUM invocations of\n"
156 " ioctl(SG_GET_NUM_WAITING); then exit\n"
Douglas Gilbert81018502018-11-27 18:38:56 +0000157 " -v increase verbosity of output\n"
158 " -V print version string then exit\n"
Douglas Gilbert985b5cd2020-07-24 18:56:21 +0000159 " -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 );
Douglas Gilbert81018502018-11-27 18:38:56 +0000167}
168
Douglas Gilbert207e5252019-11-18 22:32:39 +0000169static void
170timespec_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
182static void
183timespec_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. */
Douglas Gilbert6b32eac2022-02-03 02:38:23 +0000196int timespec2str(char *buf, unsigned int len, struct timespec *ts)
Douglas Gilbert207e5252019-11-18 22:32:39 +0000197{
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
Douglas Gilbert2433a222019-01-11 06:11:25 +0000216/* This function taken from Keith Parkard's blog dated 20121005 */
Douglas Gilbert18c98522018-12-17 21:16:36 +0000217static ssize_t
218sock_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 */
262static ssize_t
263sock_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
Douglas Gilbert28903a12019-05-15 14:30:29 +0000317static void
Douglas Gilbert80c24fa2019-07-02 14:34:36 +0000318set_more_async(int fd, bool more_asy, bool no_dur)
Douglas Gilbert28903a12019-05-15 14:30:29 +0000319{
Douglas Gilbert3dfb85f2019-06-13 05:15:40 +0000320 if (sg_drv_ver_num > 40030) {
Douglas Gilbert28903a12019-05-15 14:30:29 +0000321 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;
Douglas Gilbert80c24fa2019-07-02 14:34:36 +0000328 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 }
Douglas Gilbert28903a12019-05-15 14:30:29 +0000336 if (ioctl(fd, SG_SET_GET_EXTENDED, seip) < 0) {
Douglas Gilbert80c24fa2019-07-02 14:34:36 +0000337 pr2serr("ioctl(SG_SET_GET_EXTENDED, MORE_ASYNC(|NO_DUR)) failed, "
Douglas Gilbert28903a12019-05-15 14:30:29 +0000338 "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
Douglas Gilbert207e5252019-11-18 22:32:39 +0000345static void
346pr_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
Douglas Gilbert18c98522018-12-17 21:16:36 +0000400static int
Douglas Gilbert3e030c22019-05-18 15:37:19 +0000401tst_extended_ioctl(const char * fnp, int sg_fd, const char * fn2p, int sg_fd2,
402 int sock, const char * cp)
Douglas Gilbert18c98522018-12-17 21:16:36 +0000403{
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));
Douglas Gilbert04adb7f2019-01-31 14:29:54 +0000410 seip->sei_wr_mask |= SG_SEIM_RESERVED_SIZE;
Douglas Gilbert18c98522018-12-17 21:16:36 +0000411 seip->reserved_sz = reserve_buff_sz;
Douglas Gilberte168d372019-01-16 22:12:55 +0000412 seip->sgat_elem_sz = 64 * 1024;
Douglas Gilbert04adb7f2019-01-31 14:29:54 +0000413 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;
Douglas Gilbert18c98522018-12-17 21:16:36 +0000419 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;
Douglas Gilbert5d213822020-07-17 03:50:13 +0000425 seip->ctl_flags_rd_mask |= SG_CTL_FLAGM_IS_READ_SIDE;
Douglas Gilbert18c98522018-12-17 21:16:36 +0000426 seip->ctl_flags_rd_mask |= SG_CTL_FLAGM_UNSHARE;
Douglas Gilbert5d213822020-07-17 03:50:13 +0000427 seip->ctl_flags_rd_mask |= SG_CTL_FLAGM_READ_SIDE_FINI;
428 seip->ctl_flags_rd_mask |= SG_CTL_FLAGM_READ_SIDE_ERR;
Douglas Gilbert3a5deb42019-05-01 15:36:08 +0000429 seip->ctl_flags_wr_mask |= SG_CTL_FLAGM_NO_DURATION;
430 seip->ctl_flags_rd_mask |= SG_CTL_FLAGM_NO_DURATION;
Douglas Gilbert18c98522018-12-17 21:16:36 +0000431 seip->ctl_flags |= SG_CTL_FLAGM_TIME_IN_NS;
Douglas Gilbert3a5deb42019-05-01 15:36:08 +0000432 seip->ctl_flags |= SG_CTL_FLAGM_NO_DURATION;
Douglas Gilbert18c98522018-12-17 21:16:36 +0000433
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);
Douglas Gilbert04adb7f2019-01-31 14:29:54 +0000441 if (SG_SEIM_RESERVED_SIZE & seip->sei_rd_mask)
Douglas Gilbert18c98522018-12-17 21:16:36 +0000442 printf(" %sreserved size: %u\n", cp, seip->reserved_sz);
Douglas Gilbert04adb7f2019-01-31 14:29:54 +0000443 if (SG_SEIM_MINOR_INDEX & seip->sei_rd_mask)
Douglas Gilbert18c98522018-12-17 21:16:36 +0000444 printf(" %sminor index: %u\n", cp, seip->minor_index);
Douglas Gilbert04adb7f2019-01-31 14:29:54 +0000445 if (SG_SEIM_TOT_FD_THRESH & seip->sei_rd_mask)
Douglas Gilbert18c98522018-12-17 21:16:36 +0000446 printf(" %stot_fd_thresh: %u\n", cp, seip->tot_fd_thresh);
Douglas Gilbert04adb7f2019-01-31 14:29:54 +0000447 if ((SG_SEIM_CTL_FLAGS & seip->sei_rd_mask) ||
448 (SG_SEIM_CTL_FLAGS & seip->sei_wr_mask)) {
Douglas Gilbert18c98522018-12-17 21:16:36 +0000449 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");
Douglas Gilbert5d213822020-07-17 03:50:13 +0000465 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");
Douglas Gilbert18c98522018-12-17 21:16:36 +0000468 if (SG_CTL_FLAGM_UNSHARE & seip->ctl_flags_rd_mask)
469 printf(" %sUNSHARE: %s\n", cp,
470 (SG_CTL_FLAGM_UNSHARE & cflags) ? "true" : "false");
Douglas Gilbert5d213822020-07-17 03:50:13 +0000471 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");
Douglas Gilbert3a5deb42019-05-01 15:36:08 +0000477 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");
Douglas Gilbert18c98522018-12-17 21:16:36 +0000480 }
Douglas Gilbert04adb7f2019-01-31 14:29:54 +0000481 if (SG_SEIM_MINOR_INDEX & seip->sei_rd_mask)
Douglas Gilbert18c98522018-12-17 21:16:36 +0000482 printf(" %sminor_index: %u\n", cp, seip->minor_index);
483 printf("\n");
484#endif
485
486 memset(seip, 0, sizeof(*seip));
Douglas Gilbert04adb7f2019-01-31 14:29:54 +0000487 seip->sei_wr_mask |= SG_SEIM_READ_VAL;
488 seip->sei_rd_mask |= SG_SEIM_READ_VAL;
Douglas Gilbert18c98522018-12-17 21:16:36 +0000489 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));
Douglas Gilbert04adb7f2019-01-31 14:29:54 +0000498 seip->sei_wr_mask |= SG_SEIM_READ_VAL;
499 seip->sei_rd_mask |= SG_SEIM_READ_VAL;
Douglas Gilbert18c98522018-12-17 21:16:36 +0000500 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));
Douglas Gilbert04adb7f2019-01-31 14:29:54 +0000509 seip->sei_wr_mask |= SG_SEIM_READ_VAL;
510 seip->sei_rd_mask |= SG_SEIM_READ_VAL;
Douglas Gilbert18c98522018-12-17 21:16:36 +0000511 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));
Douglas Gilbert04adb7f2019-01-31 14:29:54 +0000520 seip->sei_wr_mask |= SG_SEIM_READ_VAL;
521 seip->sei_rd_mask |= SG_SEIM_READ_VAL;
Douglas Gilbert29fbcc32019-10-27 21:44:39 +0000522 seip->read_value = SG_SEIRV_INACT_RQS;
Douglas Gilbert18c98522018-12-17 21:16:36 +0000523 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 }
Douglas Gilbert29fbcc32019-10-27 21:44:39 +0000528 printf(" %sread_value[SG_SEIRV_INACT_RQS]= %u\n", cp, seip->read_value);
Douglas Gilbert18c98522018-12-17 21:16:36 +0000529
530 memset(seip, 0, sizeof(*seip));
Douglas Gilbert04adb7f2019-01-31 14:29:54 +0000531 seip->sei_wr_mask |= SG_SEIM_READ_VAL;
532 seip->sei_rd_mask |= SG_SEIM_READ_VAL;
Douglas Gilbert29fbcc32019-10-27 21:44:39 +0000533 seip->read_value = SG_SEIRV_DEV_INACT_RQS;
Douglas Gilbert18c98522018-12-17 21:16:36 +0000534 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 }
Douglas Gilbert29fbcc32019-10-27 21:44:39 +0000539 printf(" %sread_value[SG_SEIRV_DEV_INACT_RQS]= %u\n", cp,
540 seip->read_value);
Douglas Gilbert18c98522018-12-17 21:16:36 +0000541
542 memset(seip, 0, sizeof(*seip));
Douglas Gilbert04adb7f2019-01-31 14:29:54 +0000543 seip->sei_wr_mask |= SG_SEIM_READ_VAL;
544 seip->sei_rd_mask |= SG_SEIM_READ_VAL;
Douglas Gilbert3905f5c2019-04-04 02:59:52 +0000545 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));
Douglas Gilbertcc730c82019-04-11 00:23:23 +0000554 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,
Douglas Gilbert7dff7ae2019-04-19 18:15:45 +0000563 seip->read_value);
Douglas Gilbertcc730c82019-04-11 00:23:23 +0000564
565 memset(seip, 0, sizeof(*seip));
Douglas Gilbert04adb7f2019-01-31 14:29:54 +0000566 seip->sei_wr_mask |= SG_SEIM_SHARE_FD;
567 seip->sei_rd_mask |= SG_SEIM_SHARE_FD;
Douglas Gilbert18c98522018-12-17 21:16:36 +0000568#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 }
Douglas Gilbert04adb7f2019-01-31 14:29:54 +0000579 printf(" %sshare successful, read back previous shared_fd= %d\n", cp,
Douglas Gilbert18c98522018-12-17 21:16:36 +0000580 (int)seip->share_fd);
581bypass_share:
582
Douglas Gilbert18c98522018-12-17 21:16:36 +0000583 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);
Douglas Gilbertc11e8332018-12-25 04:37:05 +0000623 /* yes it works! */
Douglas Gilbert18c98522018-12-17 21:16:36 +0000624 }
625 return 0;
626}
627
Douglas Gilbert072494a2019-03-26 03:54:53 +0000628static int
629do_mrqs(int sg_fd, int sg_fd2, int mrqs)
630{
631 bool both = (sg_fd2 >= 0);
Douglas Gilbert7dff7ae2019-04-19 18:15:45 +0000632 int k, j, arr_v4_sz, good;
Douglas Gilbert072494a2019-03-26 03:54:53 +0000633 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;
Douglas Gilbert048bd122022-08-12 02:12:48 +0000638 uint8_t sense_buffer[SENSE_BUFFER_LEN] SG_C_CPP_ZERO_INIT;
Douglas Gilbert072494a2019-03-26 03:54:53 +0000639 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;
Douglas Gilberta37cbfd2020-09-03 15:32:35 +0000668 arr_v4 = (struct sg_io_v4 *)calloc(mrqs, sizeof(struct sg_io_v4));
Douglas Gilbert072494a2019-03-26 03:54:53 +0000669 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);
Douglas Gilbert28903a12019-05-15 14:30:29 +0000683 h4p->request = (uint64_t)(uintptr_t)sdiag_cdb;
Douglas Gilbert072494a2019-03-26 03:54:53 +0000684 /* all din and dout fields are zero */
685 } else {
686 h4p->request_len = sizeof(inq_cdb);
Douglas Gilbert28903a12019-05-15 14:30:29 +0000687 h4p->request = (uint64_t)(uintptr_t)inq_cdb;
Douglas Gilbert072494a2019-03-26 03:54:53 +0000688 h4p->din_xfer_len = INQ_REPLY_LEN;
Douglas Gilbert28903a12019-05-15 14:30:29 +0000689 h4p->din_xferp = (uint64_t)(uintptr_t)inqBuff;
Douglas Gilbert072494a2019-03-26 03:54:53 +0000690 if (both)
691 h4p->flags |= SGV4_FLAG_DO_ON_OTHER;
692 }
Douglas Gilbert28903a12019-05-15 14:30:29 +0000693 h4p->response = (uint64_t)(uintptr_t)sense_buffer;
Douglas Gilbert072494a2019-03-26 03:54:53 +0000694 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 }
Douglas Gilbert28903a12019-05-15 14:30:29 +0000703 mrq_h4p->dout_xferp = (uint64_t)(uintptr_t)arr_v4;
Douglas Gilbert3905f5c2019-04-04 02:59:52 +0000704 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;
Douglas Gilbert072494a2019-03-26 03:54:53 +0000707 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 }
Douglas Gilbert7dff7ae2019-04-19 18:15:45 +0000713 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;
Douglas Gilbert072494a2019-03-26 03:54:53 +0000720 if (mrq_immed) {
Douglas Gilbert7dff7ae2019-04-19 18:15:45 +0000721receive_more:
722 if (mrq_half_immed)
723 mrq_h4p->flags = SGV4_FLAG_MULTIPLE_REQS; // zap SGV4_FLAG_IMMED
Douglas Gilbert072494a2019-03-26 03:54:53 +0000724 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 }
Douglas Gilbert7dff7ae2019-04-19 18:15:45 +0000730 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);
Douglas Gilbert072494a2019-03-26 03:54:53 +0000733 }
734
Douglas Gilbert7dff7ae2019-04-19 18:15:45 +0000735 for (k = 0; k < (int)mrq_h4p->info; ++k, ++j) {
Douglas Gilbert072494a2019-03-26 03:54:53 +0000736 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 }
Douglas Gilbert7dff7ae2019-04-19 18:15:45 +0000742 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);
Douglas Gilbert072494a2019-03-26 03:54:53 +0000745 }
Douglas Gilbert7dff7ae2019-04-19 18:15:45 +0000746 if (mrq_immed && (j < mrqs))
747 goto receive_more;
748
Douglas Gilbert072494a2019-03-26 03:54:53 +0000749 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
761fini:
762 if (arr_v4)
763 free(arr_v4);
764 return res;
765}
766
Douglas Gilbert81018502018-11-27 18:38:56 +0000767
768int
769main(int argc, char * argv[])
Douglas Gilbert01a118f2018-10-18 15:57:29 +0000770{
Douglas Gilbert985b5cd2020-07-24 18:56:21 +0000771 bool done, is_first;
Douglas Gilbert3dfb85f2019-06-13 05:15:40 +0000772 bool nw_given = false;
Douglas Gilbert985b5cd2020-07-24 18:56:21 +0000773 bool has_dname_range = false;
774 int k, ok, pack_id, num_waiting;
Douglas Gilbert072494a2019-03-26 03:54:53 +0000775 int res = 0;
Douglas Gilbert985b5cd2020-07-24 18:56:21 +0000776 int sum_nw = 0;
777 int sg_fd = -1;
Douglas Gilbertd6e191b2018-11-12 10:21:43 +0000778 int sg_fd2 = -1;
Douglas Gilbert18c98522018-12-17 21:16:36 +0000779 int sock = -1;
Douglas Gilbert01a118f2018-10-18 15:57:29 +0000780 uint8_t inq_cdb[INQ_CMD_LEN] =
781 {0x12, 0, 0, 0, INQ_REPLY_LEN, 0};
782 uint8_t sdiag_cdb[SDIAG_CMD_LEN] =
Douglas Gilbert0b07dc92019-01-14 02:42:05 +0000783 {0x1d, 0x10 /* PF */, 0, 0, 0, 0};
Douglas Gilbert01a118f2018-10-18 15:57:29 +0000784 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;
Douglas Gilbert01a118f2018-10-18 15:57:29 +0000787 char ebuff[EBUFF_SZ];
Douglas Gilbert985b5cd2020-07-24 18:56:21 +0000788 char dname[256];
Douglas Gilbert048bd122022-08-12 02:12:48 +0000789 uint8_t sense_buffer[MAX_Q_LEN][SENSE_BUFFER_LEN] SG_C_CPP_ZERO_INIT;
Douglas Gilbert18c98522018-12-17 21:16:36 +0000790 const char * second_fname = NULL;
791 const char * cp;
Douglas Gilbert985b5cd2020-07-24 18:56:21 +0000792 char * chp;
Douglas Gilbert01a118f2018-10-18 15:57:29 +0000793 struct sg_scsi_id ssi;
794
Douglas Gilbert04adb7f2019-01-31 14:29:54 +0000795
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));
Douglas Gilbert01a118f2018-10-18 15:57:29 +0000799 for (k = 1; k < argc; ++k) {
Douglas Gilbert3e030c22019-05-18 15:37:19 +0000800 if (0 == memcmp("-3", argv[k], 2))
801 do_v3_only = true;
Douglas Gilbert207e5252019-11-18 22:32:39 +0000802 else if (0 == memcmp("-c", argv[k], 2))
803 create_time = true;
Douglas Gilbert3e030c22019-05-18 15:37:19 +0000804 else if (0 == memcmp("-f", argv[k], 2))
Douglas Gilbert18c98522018-12-17 21:16:36 +0000805 do_fork = true;
Douglas Gilbertd6e191b2018-11-12 10:21:43 +0000806 else if (0 == memcmp("-h", argv[k], 2)) {
807 file_name = 0;
808 break;
Douglas Gilbert62c85c82020-06-05 16:19:48 +0000809 } 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 }
Douglas Gilbertd6e191b2018-11-12 10:21:43 +0000823 } else if (0 == memcmp("-l=", argv[k], 3)) {
Douglas Gilbert01a118f2018-10-18 15:57:29 +0000824 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 }
Douglas Gilbert072494a2019-03-26 03:54:53 +0000831 } 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;
Douglas Gilbert7dff7ae2019-04-19 18:15:45 +0000840 if (cp[1] == 'I')
Douglas Gilbert072494a2019-03-26 03:54:53 +0000841 mrq_immed = true;
Douglas Gilbert7dff7ae2019-04-19 18:15:45 +0000842 else if (cp[1] == 'i') {
843 mrq_immed = true;
844 mrq_half_immed = true;
845 } else if (toupper(cp[1]) == 'S')
Douglas Gilbert072494a2019-03-26 03:54:53 +0000846 ;
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 }
Douglas Gilbert28903a12019-05-15 14:30:29 +0000854 } else if (0 == memcmp("-M", argv[k], 2))
855 more_async = true;
Douglas Gilbert80c24fa2019-07-02 14:34:36 +0000856 else if (0 == memcmp("-n", argv[k], 2))
857 no_duration = true;
Douglas Gilbert28903a12019-05-15 14:30:29 +0000858 else if (0 == memcmp("-o", argv[k], 2))
Douglas Gilbert81018502018-11-27 18:38:56 +0000859 ioctl_only = true;
860 else if (0 == memcmp("-r=", argv[k], 3)) {
Douglas Gilbertd6e191b2018-11-12 10:21:43 +0000861 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 }
Douglas Gilbert01a118f2018-10-18 15:57:29 +0000867 } 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 }
Douglas Gilbert28903a12019-05-15 14:30:29 +0000874 } else if (0 == memcmp("-S", argv[k], 2))
875 show_size_value = true;
876 else if (0 == memcmp("-t", argv[k], 2))
Douglas Gilbert18c98522018-12-17 21:16:36 +0000877 q_at_tail = true;
Douglas Gilbert3dfb85f2019-06-13 05:15:40 +0000878 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))
Douglas Gilbert28903a12019-05-15 14:30:29 +0000887 verbose += 7;
Douglas Gilbert7dff7ae2019-04-19 18:15:45 +0000888 else if (0 == memcmp("-vvvvvv", argv[k], 7))
Douglas Gilbert28903a12019-05-15 14:30:29 +0000889 verbose += 6;
Douglas Gilbert7dff7ae2019-04-19 18:15:45 +0000890 else if (0 == memcmp("-vvvvv", argv[k], 6))
Douglas Gilbert28903a12019-05-15 14:30:29 +0000891 verbose += 5;
Douglas Gilbert18c98522018-12-17 21:16:36 +0000892 else if (0 == memcmp("-vvvv", argv[k], 5))
Douglas Gilbertd6e191b2018-11-12 10:21:43 +0000893 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);
Douglas Gilbert29fbcc32019-10-27 21:44:39 +0000902 return 0;
Douglas Gilbert81018502018-11-27 18:38:56 +0000903 } else if (0 == memcmp("-w", argv[k], 2))
904 write_only = true;
905 else if (*argv[k] == '-') {
Douglas Gilbert01a118f2018-10-18 15:57:29 +0000906 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];
Douglas Gilbert18c98522018-12-17 21:16:36 +0000912 else if (NULL == second_fname)
913 second_fname = argv[k];
Douglas Gilbert01a118f2018-10-18 15:57:29 +0000914 else {
915 printf("too many arguments\n");
916 file_name = 0;
917 break;
918 }
919 }
Douglas Gilbert985b5cd2020-07-24 18:56:21 +0000920 if ((iterator_test >= 0) || (object_walk_test >= 0))
Douglas Gilbert62c85c82020-06-05 16:19:48 +0000921 nw_given = false;
922
Douglas Gilbert28903a12019-05-15 14:30:29 +0000923 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 }
Douglas Gilbert01a118f2018-10-18 15:57:29 +0000968 if (0 == file_name) {
Douglas Gilbert18c98522018-12-17 21:16:36 +0000969 printf("No filename (sg device) given\n\n");
Douglas Gilbert81018502018-11-27 18:38:56 +0000970 usage();
Douglas Gilbert01a118f2018-10-18 15:57:29 +0000971 return 1;
972 }
Douglas Gilbert985b5cd2020-07-24 18:56:21 +0000973 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
999dname_range_loop:
1000 if (has_dname_range)
1001 sprintf(dname + dname_pos, "%d", dname_current);
Douglas Gilbert01a118f2018-10-18 15:57:29 +00001002
1003 /* An access mode of O_RDWR is required for write()/read() interface */
Douglas Gilbert985b5cd2020-07-24 18:56:21 +00001004 if ((sg_fd = open(dname, O_RDWR)) < 0) {
1005 snprintf(ebuff, EBUFF_SZ, "error opening file: %s", dname);
Douglas Gilbert01a118f2018-10-18 15:57:29 +00001006 perror(ebuff);
1007 return 1;
1008 }
Douglas Gilbertd6e191b2018-11-12 10:21:43 +00001009 if (verbose)
1010 fprintf(stderr, "opened given file: %s successfully, fd=%d\n",
Douglas Gilbert985b5cd2020-07-24 18:56:21 +00001011 dname, sg_fd);
Douglas Gilbert01a118f2018-10-18 15:57:29 +00001012
Douglas Gilbert28903a12019-05-15 14:30:29 +00001013 if (ioctl(sg_fd, SG_GET_VERSION_NUM, &sg_drv_ver_num) < 0) {
Douglas Gilbert01a118f2018-10-18 15:57:29 +00001014 pr2serr("ioctl(SG_GET_VERSION_NUM) failed, errno=%d %s\n", errno,
1015 strerror(errno));
1016 goto out;
1017 }
Douglas Gilbert985b5cd2020-07-24 18:56:21 +00001018 if (is_first)
1019 printf("Linux sg driver version: %d\n", sg_drv_ver_num);
Douglas Gilbert62c85c82020-06-05 16:19:48 +00001020
Douglas Gilbert207e5252019-11-18 22:32:39 +00001021 if (create_time && (sg_drv_ver_num > 40030)) {
Douglas Gilbert985b5cd2020-07-24 18:56:21 +00001022 pr_create_dev_time(sg_fd, dname);
Douglas Gilbert207e5252019-11-18 22:32:39 +00001023 goto out;
1024 }
1025
Douglas Gilbert985b5cd2020-07-24 18:56:21 +00001026 if (nw_given || (iterator_test >= 0) || (object_walk_test >= 0)) {
1027 /* -T=NUM and/or -I=0|1 or -j=0|1 */
Douglas Gilbert62c85c82020-06-05 16:19:48 +00001028 /* time ioctl(SG_GET_NUM_WAITING) or do iterator_test */
Douglas Gilbert985b5cd2020-07-24 18:56:21 +00001029 int nw;
Douglas Gilbert3dfb85f2019-06-13 05:15:40 +00001030 struct timespec start_tm, fin_tm, res_tm;
1031
Douglas Gilbert985b5cd2020-07-24 18:56:21 +00001032 if (is_first) {
1033 int rang = has_dname_range ? (1 + dname_last - dname_current) : 1;
1034
1035 is_first = false;
1036 if (nw_given)
Douglas Gilberta37cbfd2020-09-03 15:32:35 +00001037 printf("Timing %d x %d calls to ioctl(SG_GET_NUM_WAITING)\n",
Douglas Gilbert985b5cd2020-07-24 18:56:21 +00001038 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 }
Douglas Gilbert3dfb85f2019-06-13 05:15:40 +00001051 }
Douglas Gilbert62c85c82020-06-05 16:19:48 +00001052 if (nw_given) {
Douglas Gilbert985b5cd2020-07-24 18:56:21 +00001053 for (k = 0; k < num_sgnw; ++k, sum_nw += nw) {
Douglas Gilbert62c85c82020-06-05 16:19:48 +00001054 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 }
Douglas Gilbert985b5cd2020-07-24 18:56:21 +00001061 } else if (iterator_test >= 0) {
Douglas Gilbert62c85c82020-06-05 16:19:48 +00001062 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) {
Douglas Gilbert5d213822020-07-17 03:50:13 +00001069 snprintf(ebuff, EBUFF_SZ, "%s: error opening file: %s",
1070 __func__, second_fname);
Douglas Gilbert62c85c82020-06-05 16:19:48 +00001071 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) {
Douglas Gilbert3dfb85f2019-06-13 05:15:40 +00001094 res = errno;
Douglas Gilbert62c85c82020-06-05 16:19:48 +00001095 fprintf(stderr, "%s%d: ioctl(SG_SET_DEBUG) failed errno=%d\n",
1096 relative_cp, k, res);
Douglas Gilbert3dfb85f2019-06-13 05:15:40 +00001097 goto out;
Douglas Gilbert985b5cd2020-07-24 18:56:21 +00001098 } 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;
Douglas Gilbert3dfb85f2019-06-13 05:15:40 +00001123 }
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) {
Douglas Gilbert62c85c82020-06-05 16:19:48 +00001137 if (nw_given && (verbose > 1))
Douglas Gilbert3dfb85f2019-06-13 05:15:40 +00001138 printf("sum of num_waiting_s=%d\n", sum_nw);
Douglas Gilbert62c85c82020-06-05 16:19:48 +00001139 printf("%selapsed time (nanosecond precision): %d.%09d secs\n",
1140 relative_cp, (int)res_tm.tv_sec, (int)res_tm.tv_nsec);
Douglas Gilbert3dfb85f2019-06-13 05:15:40 +00001141 } else
Douglas Gilbert62c85c82020-06-05 16:19:48 +00001142 printf("%selapsed time: %d.%06d secs\n", relative_cp,
1143 (int)res_tm.tv_sec, (int)(res_tm.tv_nsec / 1000));
Douglas Gilbert3dfb85f2019-06-13 05:15:40 +00001144 if (num_sgnw >= 100) {
1145 double m = (double)res_tm.tv_sec +
1146 ((double)res_tm.tv_nsec / 1000000000.0);
Douglas Gilberta37cbfd2020-09-03 15:32:35 +00001147 double num = num_sgnw;
Douglas Gilbert3dfb85f2019-06-13 05:15:40 +00001148
1149 if (m > 0.000001)
Douglas Gilberta37cbfd2020-09-03 15:32:35 +00001150 printf("%sCalls per second: %.2f\n", relative_cp, num / m);
Douglas Gilbert3dfb85f2019-06-13 05:15:40 +00001151 }
Douglas Gilbert3dfb85f2019-06-13 05:15:40 +00001152 res = 0;
1153 goto out;
1154 }
Douglas Gilbert80c24fa2019-07-02 14:34:36 +00001155 if ((more_async || no_duration) && !do_v3_only)
1156 set_more_async(sg_fd, more_async, no_duration);
Douglas Gilbert01a118f2018-10-18 15:57:29 +00001157
Douglas Gilbert18c98522018-12-17 21:16:36 +00001158 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);
Douglas Gilbert3e030c22019-05-18 15:37:19 +00001168 if (more_async && !do_v3_only)
Douglas Gilbert80c24fa2019-07-02 14:34:36 +00001169 set_more_async(sg_fd2, more_async, no_duration);
Douglas Gilbertd6e191b2018-11-12 10:21:43 +00001170 }
Douglas Gilbertd6e191b2018-11-12 10:21:43 +00001171
Douglas Gilbert3e030c22019-05-18 15:37:19 +00001172 if ((num_mrqs > 0) && !do_v3_only) {
Douglas Gilbert072494a2019-03-26 03:54:53 +00001173 res = do_mrqs(sg_fd, sg_fd2, num_mrqs);
1174 goto out;
1175 }
1176
Douglas Gilbert18c98522018-12-17 21:16:36 +00001177 if (do_fork) {
1178 int pid;
1179 int sv[2];
Douglas Gilbert01a118f2018-10-18 15:57:29 +00001180
Douglas Gilbert18c98522018-12-17 21:16:36 +00001181 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 : "";
Douglas Gilbert7901cc42021-04-08 01:08:07 +00001206 if (! do_v3_only && (sg_drv_ver_num > 40030)) {
Douglas Gilbert985b5cd2020-07-24 18:56:21 +00001207 if (tst_extended_ioctl(dname, sg_fd, second_fname, sg_fd2, sock,
Douglas Gilbert3e030c22019-05-18 15:37:19 +00001208 cp))
1209 goto out;
1210 }
Douglas Gilbert81018502018-11-27 18:38:56 +00001211 if (ioctl_only)
1212 goto out;
1213
Douglas Gilbert18c98522018-12-17 21:16:36 +00001214 if (do_fork && !is_parent)
1215 return 0;
1216
Douglas Gilbert3e030c22019-05-18 15:37:19 +00001217 printf("start write() calls [submits]\n");
Douglas Gilbert01a118f2018-10-18 15:57:29 +00001218 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) {
Douglas Gilbert18c98522018-12-17 21:16:36 +00001247 pr2serr("%ssg write errno=%d [%s]\n", cp, errno, strerror(errno));
Douglas Gilbert01a118f2018-10-18 15:57:29 +00001248 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);
Douglas Gilbertd491f4c2019-02-06 23:24:46 +00001265 printf(" SCSI 8 byte LUN: ");
1266 hex2stdout(ssi.scsi_lun, 8, -1);
Douglas Gilbert01a118f2018-10-18 15:57:29 +00001267 }
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
Douglas Gilbertd6e191b2018-11-12 10:21:43 +00001272 printf("first available pack_id: %d\n", pack_id);
Douglas Gilbert01a118f2018-10-18 15:57:29 +00001273 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
Douglas Gilbert81018502018-11-27 18:38:56 +00001281 if (write_only)
1282 goto out;
1283
Douglas Gilbert18c98522018-12-17 21:16:36 +00001284 if (do_fork)
1285 printf("\n\nFollowing starting with get_pack_id are all CHILD\n");
Douglas Gilbert01a118f2018-10-18 15:57:29 +00001286 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
Douglas Gilbertd6e191b2018-11-12 10:21:43 +00001290 printf("first available pack_id: %d\n", pack_id);
Douglas Gilbert01a118f2018-10-18 15:57:29 +00001291 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
Douglas Gilbert3e030c22019-05-18 15:37:19 +00001297 printf("\nstart read() calls [io receive]\n");
Douglas Gilbert01a118f2018-10-18 15:57:29 +00001298 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
Douglas Gilbertd6e191b2018-11-12 10:21:43 +00001306 printf("first available pack_id: %d\n", pack_id);
Douglas Gilbert01a118f2018-10-18 15:57:29 +00001307 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);
Douglas Gilbert01a118f2018-10-18 15:57:29 +00001312 }
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) {
Douglas Gilbert18c98522018-12-17 21:16:36 +00001316 perror("sg read error");
Douglas Gilbert01a118f2018-10-18 15:57:29 +00001317 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 */
Douglas Gilbert01a118f2018-10-18 15:57:29 +00001336 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
1345out:
Douglas Gilbert62c85c82020-06-05 16:19:48 +00001346 if (sg_fd >= 0)
1347 close(sg_fd);
Douglas Gilbertd6e191b2018-11-12 10:21:43 +00001348 if (sg_fd2 >= 0)
1349 close(sg_fd2);
Douglas Gilbert072494a2019-03-26 03:54:53 +00001350 return res;
Douglas Gilbert01a118f2018-10-18 15:57:29 +00001351}