Douglas Gilbert | 4445eec | 2018-12-07 16:42:06 +0000 | [diff] [blame] | 1 | /* |
Douglas Gilbert | 6b32eac | 2022-02-03 02:38:23 +0000 | [diff] [blame] | 2 | * Copyright (C) 2018-2022 D. Gilbert |
Douglas Gilbert | 4445eec | 2018-12-07 16:42:06 +0000 | [diff] [blame] | 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 | |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 14 | #include <unistd.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 18 | #include <stdarg.h> |
| 19 | #include <stdbool.h> |
| 20 | #include <ctype.h> |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 21 | #include <string.h> |
| 22 | #include <errno.h> |
Douglas Gilbert | 3dfb85f | 2019-06-13 05:15:40 +0000 | [diff] [blame] | 23 | #include <time.h> |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 24 | #include <sys/ioctl.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/stat.h> |
Douglas Gilbert | 28903a1 | 2019-05-15 14:30:29 +0000 | [diff] [blame] | 27 | #include <sys/utsname.h> |
Douglas Gilbert | 207e525 | 2019-11-18 22:32:39 +0000 | [diff] [blame] | 28 | #define __STDC_FORMAT_MACROS 1 |
| 29 | #include <inttypes.h> |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 30 | |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 31 | #include <sys/socket.h> /* For passing fd_s via Unix sockets */ |
| 32 | |
Douglas Gilbert | 2433a22 | 2019-01-11 06:11:25 +0000 | [diff] [blame] | 33 | #ifndef HAVE_LINUX_SG_V4_HDR |
| 34 | |
Douglas Gilbert | d6e191b | 2018-11-12 10:21:43 +0000 | [diff] [blame] | 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 */ |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 49 | |
Douglas Gilbert | 2433a22 | 2019-01-11 06:11:25 +0000 | [diff] [blame] | 50 | #else |
| 51 | #define __user |
| 52 | #endif /* end of: ifndef HAVE_LINUX_SG_V4_HDR */ |
| 53 | |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 54 | #include "sg_lib.h" |
| 55 | #include "sg_io_linux.h" |
| 56 | #include "sg_linux_inc.h" |
| 57 | #include "sg_pr2serr.h" |
| 58 | |
Douglas Gilbert | 2433a22 | 2019-01-11 06:11:25 +0000 | [diff] [blame] | 59 | /* This program tests ioctl() calls added and modified in version 4.0 and |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 60 | * later of the Linux sg driver. */ |
| 61 | |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 62 | |
Douglas Gilbert | 6b32eac | 2022-02-03 02:38:23 +0000 | [diff] [blame] | 63 | static const char * version_str = "Version: 1.21 20220202"; |
Douglas Gilbert | d6e191b | 2018-11-12 10:21:43 +0000 | [diff] [blame] | 64 | |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 65 | #define INQ_REPLY_LEN 128 |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 66 | #define INQ_CMD_LEN 6 |
| 67 | #define SDIAG_CMD_LEN 6 |
| 68 | #define SENSE_BUFFER_LEN 96 |
| 69 | |
Douglas Gilbert | 985b5cd | 2020-07-24 18:56:21 +0000 | [diff] [blame] | 70 | #define EBUFF_SZ 512 |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 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 */ |
Douglas Gilbert | 3e030c2 | 2019-05-18 15:37:19 +0000 | [diff] [blame] | 81 | #define MAX_Q_LEN 512 |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 82 | |
Douglas Gilbert | d6e191b | 2018-11-12 10:21:43 +0000 | [diff] [blame] | 83 | #define DEF_RESERVE_BUFF_SZ (256 * 1024) |
| 84 | |
Douglas Gilbert | 207e525 | 2019-11-18 22:32:39 +0000 | [diff] [blame] | 85 | static bool create_time = false; |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 86 | static bool is_parent = false; |
| 87 | static bool do_fork = false; |
| 88 | static bool ioctl_only = false; |
Douglas Gilbert | 28903a1 | 2019-05-15 14:30:29 +0000 | [diff] [blame] | 89 | static bool more_async = false; |
Douglas Gilbert | 80c24fa | 2019-07-02 14:34:36 +0000 | [diff] [blame] | 90 | static bool no_duration = false; |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 91 | static bool q_at_tail = false; |
| 92 | static bool write_only = false; |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 93 | static bool mrq_immed = false; /* if set, also sets mrq_iosubmit */ |
Douglas Gilbert | 7dff7ae | 2019-04-19 18:15:45 +0000 | [diff] [blame] | 94 | static bool mrq_half_immed = false; |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 95 | static bool mrq_iosubmit = false; |
Douglas Gilbert | 28903a1 | 2019-05-15 14:30:29 +0000 | [diff] [blame] | 96 | static bool show_size_value = false; |
Douglas Gilbert | 3e030c2 | 2019-05-18 15:37:19 +0000 | [diff] [blame] | 97 | static bool do_v3_only = false; |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 98 | |
| 99 | static int childs_pid = 0; |
Douglas Gilbert | 62c85c8 | 2020-06-05 16:19:48 +0000 | [diff] [blame] | 100 | static int iterator_test = -1; |
| 101 | static int object_walk_test = -1; |
Douglas Gilbert | 28903a1 | 2019-05-15 14:30:29 +0000 | [diff] [blame] | 102 | static int sg_drv_ver_num = 0; |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 103 | static int q_len = DEF_Q_LEN; |
| 104 | static int sleep_secs = 0; |
| 105 | static int reserve_buff_sz = DEF_RESERVE_BUFF_SZ; |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 106 | static int num_mrqs = 0; |
Douglas Gilbert | 3dfb85f | 2019-06-13 05:15:40 +0000 | [diff] [blame] | 107 | static int num_sgnw = 0; |
Douglas Gilbert | 985b5cd | 2020-07-24 18:56:21 +0000 | [diff] [blame] | 108 | static int dname_current = 0; |
| 109 | static int dname_last = 0; |
| 110 | static int dname_pos = 0; |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 111 | static int verbose = 0; |
| 112 | |
Douglas Gilbert | a37cbfd | 2020-09-03 15:32:35 +0000 | [diff] [blame] | 113 | static const char * relative_cp = ""; |
Douglas Gilbert | 207e525 | 2019-11-18 22:32:39 +0000 | [diff] [blame] | 114 | static char * file_name = NULL; |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 115 | |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 116 | |
Douglas Gilbert | 8101850 | 2018-11-27 18:38:56 +0000 | [diff] [blame] | 117 | static void |
| 118 | usage(void) |
| 119 | { |
Douglas Gilbert | 62c85c8 | 2020-06-05 16:19:48 +0000 | [diff] [blame] | 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" |
Douglas Gilbert | 985b5cd | 2020-07-24 18:56:21 +0000 | [diff] [blame] | 125 | " <sg_device>[-<num>] [<sg_device2>]\n" |
Douglas Gilbert | 8101850 | 2018-11-27 18:38:56 +0000 | [diff] [blame] | 126 | " where:\n" |
Douglas Gilbert | 985b5cd | 2020-07-24 18:56:21 +0000 | [diff] [blame] | 127 | " -3 use sg v3 interface (def: sg v4 if available)\n" |
Douglas Gilbert | 207e525 | 2019-11-18 22:32:39 +0000 | [diff] [blame] | 128 | " -c timestamp when sg driver created <sg_device>\n" |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 129 | " -f fork and test share between processes\n" |
Douglas Gilbert | 8101850 | 2018-11-27 18:38:56 +0000 | [diff] [blame] | 130 | " -h help: print usage message then exit\n" |
Douglas Gilbert | 62c85c8 | 2020-06-05 16:19:48 +0000 | [diff] [blame] | 131 | " -I=0|1 iterator test of mid-level; 0: unlocked, 1: " |
| 132 | "locked\n" |
| 133 | " does test -T=NUM times, outputs duration\n" |
Douglas Gilbert | 985b5cd | 2020-07-24 18:56:21 +0000 | [diff] [blame] | 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" |
Douglas Gilbert | 8101850 | 2018-11-27 18:38:56 +0000 | [diff] [blame] | 137 | " -l=Q_LEN queue length, between 1 and 511 (def: 16)\n" |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 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" |
Douglas Gilbert | 7dff7ae | 2019-04-19 18:15:45 +0000 | [diff] [blame] | 142 | " 'i' IMMED on submission, non-IMMED on " |
| 143 | "receive;\n" |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 144 | " 'S' is appended, then use " |
| 145 | "ioctl(SG_IOSUBMIT)\n" |
Douglas Gilbert | 28903a1 | 2019-05-15 14:30:29 +0000 | [diff] [blame] | 146 | " -M set 'more async' flag\n" |
Douglas Gilbert | 80c24fa | 2019-07-02 14:34:36 +0000 | [diff] [blame] | 147 | " -n do not calculate per command duration (def: do)\n" |
Douglas Gilbert | 8101850 | 2018-11-27 18:38:56 +0000 | [diff] [blame] | 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" |
Douglas Gilbert | 28903a1 | 2019-05-15 14:30:29 +0000 | [diff] [blame] | 152 | " -S size of interface structures plus ioctl " |
| 153 | "values\n" |
Douglas Gilbert | 8101850 | 2018-11-27 18:38:56 +0000 | [diff] [blame] | 154 | " -t queue_at_tail (def: q_at_head)\n" |
Douglas Gilbert | 3dfb85f | 2019-06-13 05:15:40 +0000 | [diff] [blame] | 155 | " -T=NUM time overhead of NUM invocations of\n" |
| 156 | " ioctl(SG_GET_NUM_WAITING); then exit\n" |
Douglas Gilbert | 8101850 | 2018-11-27 18:38:56 +0000 | [diff] [blame] | 157 | " -v increase verbosity of output\n" |
| 158 | " -V print version string then exit\n" |
Douglas Gilbert | 985b5cd | 2020-07-24 18:56:21 +0000 | [diff] [blame] | 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 | ); |
Douglas Gilbert | 8101850 | 2018-11-27 18:38:56 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Douglas Gilbert | 207e525 | 2019-11-18 22:32:39 +0000 | [diff] [blame] | 169 | static void |
| 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 |
| 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. */ |
Douglas Gilbert | 6b32eac | 2022-02-03 02:38:23 +0000 | [diff] [blame] | 196 | int timespec2str(char *buf, unsigned int len, struct timespec *ts) |
Douglas Gilbert | 207e525 | 2019-11-18 22:32:39 +0000 | [diff] [blame] | 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 | |
Douglas Gilbert | 2433a22 | 2019-01-11 06:11:25 +0000 | [diff] [blame] | 216 | /* This function taken from Keith Parkard's blog dated 20121005 */ |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 217 | static ssize_t |
| 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 |
| 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 | |
Douglas Gilbert | 28903a1 | 2019-05-15 14:30:29 +0000 | [diff] [blame] | 317 | static void |
Douglas Gilbert | 80c24fa | 2019-07-02 14:34:36 +0000 | [diff] [blame] | 318 | set_more_async(int fd, bool more_asy, bool no_dur) |
Douglas Gilbert | 28903a1 | 2019-05-15 14:30:29 +0000 | [diff] [blame] | 319 | { |
Douglas Gilbert | 3dfb85f | 2019-06-13 05:15:40 +0000 | [diff] [blame] | 320 | if (sg_drv_ver_num > 40030) { |
Douglas Gilbert | 28903a1 | 2019-05-15 14:30:29 +0000 | [diff] [blame] | 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; |
Douglas Gilbert | 80c24fa | 2019-07-02 14:34:36 +0000 | [diff] [blame] | 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 | } |
Douglas Gilbert | 28903a1 | 2019-05-15 14:30:29 +0000 | [diff] [blame] | 336 | if (ioctl(fd, SG_SET_GET_EXTENDED, seip) < 0) { |
Douglas Gilbert | 80c24fa | 2019-07-02 14:34:36 +0000 | [diff] [blame] | 337 | pr2serr("ioctl(SG_SET_GET_EXTENDED, MORE_ASYNC(|NO_DUR)) failed, " |
Douglas Gilbert | 28903a1 | 2019-05-15 14:30:29 +0000 | [diff] [blame] | 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 | |
Douglas Gilbert | 207e525 | 2019-11-18 22:32:39 +0000 | [diff] [blame] | 345 | static void |
| 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 | |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 400 | static int |
Douglas Gilbert | 3e030c2 | 2019-05-18 15:37:19 +0000 | [diff] [blame] | 401 | tst_extended_ioctl(const char * fnp, int sg_fd, const char * fn2p, int sg_fd2, |
| 402 | int sock, const char * cp) |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 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)); |
Douglas Gilbert | 04adb7f | 2019-01-31 14:29:54 +0000 | [diff] [blame] | 410 | seip->sei_wr_mask |= SG_SEIM_RESERVED_SIZE; |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 411 | seip->reserved_sz = reserve_buff_sz; |
Douglas Gilbert | e168d37 | 2019-01-16 22:12:55 +0000 | [diff] [blame] | 412 | seip->sgat_elem_sz = 64 * 1024; |
Douglas Gilbert | 04adb7f | 2019-01-31 14:29:54 +0000 | [diff] [blame] | 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; |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 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; |
Douglas Gilbert | 5d21382 | 2020-07-17 03:50:13 +0000 | [diff] [blame] | 425 | seip->ctl_flags_rd_mask |= SG_CTL_FLAGM_IS_READ_SIDE; |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 426 | seip->ctl_flags_rd_mask |= SG_CTL_FLAGM_UNSHARE; |
Douglas Gilbert | 5d21382 | 2020-07-17 03:50:13 +0000 | [diff] [blame] | 427 | seip->ctl_flags_rd_mask |= SG_CTL_FLAGM_READ_SIDE_FINI; |
| 428 | seip->ctl_flags_rd_mask |= SG_CTL_FLAGM_READ_SIDE_ERR; |
Douglas Gilbert | 3a5deb4 | 2019-05-01 15:36:08 +0000 | [diff] [blame] | 429 | seip->ctl_flags_wr_mask |= SG_CTL_FLAGM_NO_DURATION; |
| 430 | seip->ctl_flags_rd_mask |= SG_CTL_FLAGM_NO_DURATION; |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 431 | seip->ctl_flags |= SG_CTL_FLAGM_TIME_IN_NS; |
Douglas Gilbert | 3a5deb4 | 2019-05-01 15:36:08 +0000 | [diff] [blame] | 432 | seip->ctl_flags |= SG_CTL_FLAGM_NO_DURATION; |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 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); |
Douglas Gilbert | 04adb7f | 2019-01-31 14:29:54 +0000 | [diff] [blame] | 441 | if (SG_SEIM_RESERVED_SIZE & seip->sei_rd_mask) |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 442 | printf(" %sreserved size: %u\n", cp, seip->reserved_sz); |
Douglas Gilbert | 04adb7f | 2019-01-31 14:29:54 +0000 | [diff] [blame] | 443 | if (SG_SEIM_MINOR_INDEX & seip->sei_rd_mask) |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 444 | printf(" %sminor index: %u\n", cp, seip->minor_index); |
Douglas Gilbert | 04adb7f | 2019-01-31 14:29:54 +0000 | [diff] [blame] | 445 | if (SG_SEIM_TOT_FD_THRESH & seip->sei_rd_mask) |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 446 | printf(" %stot_fd_thresh: %u\n", cp, seip->tot_fd_thresh); |
Douglas Gilbert | 04adb7f | 2019-01-31 14:29:54 +0000 | [diff] [blame] | 447 | if ((SG_SEIM_CTL_FLAGS & seip->sei_rd_mask) || |
| 448 | (SG_SEIM_CTL_FLAGS & seip->sei_wr_mask)) { |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 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"); |
Douglas Gilbert | 5d21382 | 2020-07-17 03:50:13 +0000 | [diff] [blame] | 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"); |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 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"); |
Douglas Gilbert | 5d21382 | 2020-07-17 03:50:13 +0000 | [diff] [blame] | 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"); |
Douglas Gilbert | 3a5deb4 | 2019-05-01 15:36:08 +0000 | [diff] [blame] | 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"); |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 480 | } |
Douglas Gilbert | 04adb7f | 2019-01-31 14:29:54 +0000 | [diff] [blame] | 481 | if (SG_SEIM_MINOR_INDEX & seip->sei_rd_mask) |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 482 | printf(" %sminor_index: %u\n", cp, seip->minor_index); |
| 483 | printf("\n"); |
| 484 | #endif |
| 485 | |
| 486 | memset(seip, 0, sizeof(*seip)); |
Douglas Gilbert | 04adb7f | 2019-01-31 14:29:54 +0000 | [diff] [blame] | 487 | seip->sei_wr_mask |= SG_SEIM_READ_VAL; |
| 488 | seip->sei_rd_mask |= SG_SEIM_READ_VAL; |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 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)); |
Douglas Gilbert | 04adb7f | 2019-01-31 14:29:54 +0000 | [diff] [blame] | 498 | seip->sei_wr_mask |= SG_SEIM_READ_VAL; |
| 499 | seip->sei_rd_mask |= SG_SEIM_READ_VAL; |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 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)); |
Douglas Gilbert | 04adb7f | 2019-01-31 14:29:54 +0000 | [diff] [blame] | 509 | seip->sei_wr_mask |= SG_SEIM_READ_VAL; |
| 510 | seip->sei_rd_mask |= SG_SEIM_READ_VAL; |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 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)); |
Douglas Gilbert | 04adb7f | 2019-01-31 14:29:54 +0000 | [diff] [blame] | 520 | seip->sei_wr_mask |= SG_SEIM_READ_VAL; |
| 521 | seip->sei_rd_mask |= SG_SEIM_READ_VAL; |
Douglas Gilbert | 29fbcc3 | 2019-10-27 21:44:39 +0000 | [diff] [blame] | 522 | seip->read_value = SG_SEIRV_INACT_RQS; |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 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 | } |
Douglas Gilbert | 29fbcc3 | 2019-10-27 21:44:39 +0000 | [diff] [blame] | 528 | printf(" %sread_value[SG_SEIRV_INACT_RQS]= %u\n", cp, seip->read_value); |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 529 | |
| 530 | memset(seip, 0, sizeof(*seip)); |
Douglas Gilbert | 04adb7f | 2019-01-31 14:29:54 +0000 | [diff] [blame] | 531 | seip->sei_wr_mask |= SG_SEIM_READ_VAL; |
| 532 | seip->sei_rd_mask |= SG_SEIM_READ_VAL; |
Douglas Gilbert | 29fbcc3 | 2019-10-27 21:44:39 +0000 | [diff] [blame] | 533 | seip->read_value = SG_SEIRV_DEV_INACT_RQS; |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 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 | } |
Douglas Gilbert | 29fbcc3 | 2019-10-27 21:44:39 +0000 | [diff] [blame] | 539 | printf(" %sread_value[SG_SEIRV_DEV_INACT_RQS]= %u\n", cp, |
| 540 | seip->read_value); |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 541 | |
| 542 | memset(seip, 0, sizeof(*seip)); |
Douglas Gilbert | 04adb7f | 2019-01-31 14:29:54 +0000 | [diff] [blame] | 543 | seip->sei_wr_mask |= SG_SEIM_READ_VAL; |
| 544 | seip->sei_rd_mask |= SG_SEIM_READ_VAL; |
Douglas Gilbert | 3905f5c | 2019-04-04 02:59:52 +0000 | [diff] [blame] | 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)); |
Douglas Gilbert | cc730c8 | 2019-04-11 00:23:23 +0000 | [diff] [blame] | 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, |
Douglas Gilbert | 7dff7ae | 2019-04-19 18:15:45 +0000 | [diff] [blame] | 563 | seip->read_value); |
Douglas Gilbert | cc730c8 | 2019-04-11 00:23:23 +0000 | [diff] [blame] | 564 | |
| 565 | memset(seip, 0, sizeof(*seip)); |
Douglas Gilbert | 04adb7f | 2019-01-31 14:29:54 +0000 | [diff] [blame] | 566 | seip->sei_wr_mask |= SG_SEIM_SHARE_FD; |
| 567 | seip->sei_rd_mask |= SG_SEIM_SHARE_FD; |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 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 | } |
Douglas Gilbert | 04adb7f | 2019-01-31 14:29:54 +0000 | [diff] [blame] | 579 | printf(" %sshare successful, read back previous shared_fd= %d\n", cp, |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 580 | (int)seip->share_fd); |
| 581 | bypass_share: |
| 582 | |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 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); |
Douglas Gilbert | c11e833 | 2018-12-25 04:37:05 +0000 | [diff] [blame] | 623 | /* yes it works! */ |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 624 | } |
| 625 | return 0; |
| 626 | } |
| 627 | |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 628 | static int |
| 629 | do_mrqs(int sg_fd, int sg_fd2, int mrqs) |
| 630 | { |
| 631 | bool both = (sg_fd2 >= 0); |
Douglas Gilbert | 7dff7ae | 2019-04-19 18:15:45 +0000 | [diff] [blame] | 632 | int k, j, arr_v4_sz, good; |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 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; |
Douglas Gilbert | 048bd12 | 2022-08-12 02:12:48 +0000 | [diff] [blame] | 638 | uint8_t sense_buffer[SENSE_BUFFER_LEN] SG_C_CPP_ZERO_INIT; |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 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; |
Douglas Gilbert | a37cbfd | 2020-09-03 15:32:35 +0000 | [diff] [blame] | 668 | arr_v4 = (struct sg_io_v4 *)calloc(mrqs, sizeof(struct sg_io_v4)); |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 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); |
Douglas Gilbert | 28903a1 | 2019-05-15 14:30:29 +0000 | [diff] [blame] | 683 | h4p->request = (uint64_t)(uintptr_t)sdiag_cdb; |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 684 | /* all din and dout fields are zero */ |
| 685 | } else { |
| 686 | h4p->request_len = sizeof(inq_cdb); |
Douglas Gilbert | 28903a1 | 2019-05-15 14:30:29 +0000 | [diff] [blame] | 687 | h4p->request = (uint64_t)(uintptr_t)inq_cdb; |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 688 | h4p->din_xfer_len = INQ_REPLY_LEN; |
Douglas Gilbert | 28903a1 | 2019-05-15 14:30:29 +0000 | [diff] [blame] | 689 | h4p->din_xferp = (uint64_t)(uintptr_t)inqBuff; |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 690 | if (both) |
| 691 | h4p->flags |= SGV4_FLAG_DO_ON_OTHER; |
| 692 | } |
Douglas Gilbert | 28903a1 | 2019-05-15 14:30:29 +0000 | [diff] [blame] | 693 | h4p->response = (uint64_t)(uintptr_t)sense_buffer; |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 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 | } |
Douglas Gilbert | 28903a1 | 2019-05-15 14:30:29 +0000 | [diff] [blame] | 703 | mrq_h4p->dout_xferp = (uint64_t)(uintptr_t)arr_v4; |
Douglas Gilbert | 3905f5c | 2019-04-04 02:59:52 +0000 | [diff] [blame] | 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; |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 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 | } |
Douglas Gilbert | 7dff7ae | 2019-04-19 18:15:45 +0000 | [diff] [blame] | 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; |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 720 | if (mrq_immed) { |
Douglas Gilbert | 7dff7ae | 2019-04-19 18:15:45 +0000 | [diff] [blame] | 721 | receive_more: |
| 722 | if (mrq_half_immed) |
| 723 | mrq_h4p->flags = SGV4_FLAG_MULTIPLE_REQS; // zap SGV4_FLAG_IMMED |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 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 | } |
Douglas Gilbert | 7dff7ae | 2019-04-19 18:15:45 +0000 | [diff] [blame] | 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); |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Douglas Gilbert | 7dff7ae | 2019-04-19 18:15:45 +0000 | [diff] [blame] | 735 | for (k = 0; k < (int)mrq_h4p->info; ++k, ++j) { |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 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 | } |
Douglas Gilbert | 7dff7ae | 2019-04-19 18:15:45 +0000 | [diff] [blame] | 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); |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 745 | } |
Douglas Gilbert | 7dff7ae | 2019-04-19 18:15:45 +0000 | [diff] [blame] | 746 | if (mrq_immed && (j < mrqs)) |
| 747 | goto receive_more; |
| 748 | |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 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 | |
Douglas Gilbert | 8101850 | 2018-11-27 18:38:56 +0000 | [diff] [blame] | 767 | |
| 768 | int |
| 769 | main(int argc, char * argv[]) |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 770 | { |
Douglas Gilbert | 985b5cd | 2020-07-24 18:56:21 +0000 | [diff] [blame] | 771 | bool done, is_first; |
Douglas Gilbert | 3dfb85f | 2019-06-13 05:15:40 +0000 | [diff] [blame] | 772 | bool nw_given = false; |
Douglas Gilbert | 985b5cd | 2020-07-24 18:56:21 +0000 | [diff] [blame] | 773 | bool has_dname_range = false; |
| 774 | int k, ok, pack_id, num_waiting; |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 775 | int res = 0; |
Douglas Gilbert | 985b5cd | 2020-07-24 18:56:21 +0000 | [diff] [blame] | 776 | int sum_nw = 0; |
| 777 | int sg_fd = -1; |
Douglas Gilbert | d6e191b | 2018-11-12 10:21:43 +0000 | [diff] [blame] | 778 | int sg_fd2 = -1; |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 779 | int sock = -1; |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 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] = |
Douglas Gilbert | 0b07dc9 | 2019-01-14 02:42:05 +0000 | [diff] [blame] | 783 | {0x1d, 0x10 /* PF */, 0, 0, 0, 0}; |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 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; |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 787 | char ebuff[EBUFF_SZ]; |
Douglas Gilbert | 985b5cd | 2020-07-24 18:56:21 +0000 | [diff] [blame] | 788 | char dname[256]; |
Douglas Gilbert | 048bd12 | 2022-08-12 02:12:48 +0000 | [diff] [blame] | 789 | uint8_t sense_buffer[MAX_Q_LEN][SENSE_BUFFER_LEN] SG_C_CPP_ZERO_INIT; |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 790 | const char * second_fname = NULL; |
| 791 | const char * cp; |
Douglas Gilbert | 985b5cd | 2020-07-24 18:56:21 +0000 | [diff] [blame] | 792 | char * chp; |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 793 | struct sg_scsi_id ssi; |
| 794 | |
Douglas Gilbert | 04adb7f | 2019-01-31 14:29:54 +0000 | [diff] [blame] | 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)); |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 799 | for (k = 1; k < argc; ++k) { |
Douglas Gilbert | 3e030c2 | 2019-05-18 15:37:19 +0000 | [diff] [blame] | 800 | if (0 == memcmp("-3", argv[k], 2)) |
| 801 | do_v3_only = true; |
Douglas Gilbert | 207e525 | 2019-11-18 22:32:39 +0000 | [diff] [blame] | 802 | else if (0 == memcmp("-c", argv[k], 2)) |
| 803 | create_time = true; |
Douglas Gilbert | 3e030c2 | 2019-05-18 15:37:19 +0000 | [diff] [blame] | 804 | else if (0 == memcmp("-f", argv[k], 2)) |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 805 | do_fork = true; |
Douglas Gilbert | d6e191b | 2018-11-12 10:21:43 +0000 | [diff] [blame] | 806 | else if (0 == memcmp("-h", argv[k], 2)) { |
| 807 | file_name = 0; |
| 808 | break; |
Douglas Gilbert | 62c85c8 | 2020-06-05 16:19:48 +0000 | [diff] [blame] | 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 | } |
Douglas Gilbert | d6e191b | 2018-11-12 10:21:43 +0000 | [diff] [blame] | 823 | } else if (0 == memcmp("-l=", argv[k], 3)) { |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 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 | } |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 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; |
Douglas Gilbert | 7dff7ae | 2019-04-19 18:15:45 +0000 | [diff] [blame] | 840 | if (cp[1] == 'I') |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 841 | mrq_immed = true; |
Douglas Gilbert | 7dff7ae | 2019-04-19 18:15:45 +0000 | [diff] [blame] | 842 | else if (cp[1] == 'i') { |
| 843 | mrq_immed = true; |
| 844 | mrq_half_immed = true; |
| 845 | } else if (toupper(cp[1]) == 'S') |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 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 | } |
Douglas Gilbert | 28903a1 | 2019-05-15 14:30:29 +0000 | [diff] [blame] | 854 | } else if (0 == memcmp("-M", argv[k], 2)) |
| 855 | more_async = true; |
Douglas Gilbert | 80c24fa | 2019-07-02 14:34:36 +0000 | [diff] [blame] | 856 | else if (0 == memcmp("-n", argv[k], 2)) |
| 857 | no_duration = true; |
Douglas Gilbert | 28903a1 | 2019-05-15 14:30:29 +0000 | [diff] [blame] | 858 | else if (0 == memcmp("-o", argv[k], 2)) |
Douglas Gilbert | 8101850 | 2018-11-27 18:38:56 +0000 | [diff] [blame] | 859 | ioctl_only = true; |
| 860 | else if (0 == memcmp("-r=", argv[k], 3)) { |
Douglas Gilbert | d6e191b | 2018-11-12 10:21:43 +0000 | [diff] [blame] | 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 | } |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 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 | } |
Douglas Gilbert | 28903a1 | 2019-05-15 14:30:29 +0000 | [diff] [blame] | 874 | } else if (0 == memcmp("-S", argv[k], 2)) |
| 875 | show_size_value = true; |
| 876 | else if (0 == memcmp("-t", argv[k], 2)) |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 877 | q_at_tail = true; |
Douglas Gilbert | 3dfb85f | 2019-06-13 05:15:40 +0000 | [diff] [blame] | 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)) |
Douglas Gilbert | 28903a1 | 2019-05-15 14:30:29 +0000 | [diff] [blame] | 887 | verbose += 7; |
Douglas Gilbert | 7dff7ae | 2019-04-19 18:15:45 +0000 | [diff] [blame] | 888 | else if (0 == memcmp("-vvvvvv", argv[k], 7)) |
Douglas Gilbert | 28903a1 | 2019-05-15 14:30:29 +0000 | [diff] [blame] | 889 | verbose += 6; |
Douglas Gilbert | 7dff7ae | 2019-04-19 18:15:45 +0000 | [diff] [blame] | 890 | else if (0 == memcmp("-vvvvv", argv[k], 6)) |
Douglas Gilbert | 28903a1 | 2019-05-15 14:30:29 +0000 | [diff] [blame] | 891 | verbose += 5; |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 892 | else if (0 == memcmp("-vvvv", argv[k], 5)) |
Douglas Gilbert | d6e191b | 2018-11-12 10:21:43 +0000 | [diff] [blame] | 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); |
Douglas Gilbert | 29fbcc3 | 2019-10-27 21:44:39 +0000 | [diff] [blame] | 902 | return 0; |
Douglas Gilbert | 8101850 | 2018-11-27 18:38:56 +0000 | [diff] [blame] | 903 | } else if (0 == memcmp("-w", argv[k], 2)) |
| 904 | write_only = true; |
| 905 | else if (*argv[k] == '-') { |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 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]; |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 912 | else if (NULL == second_fname) |
| 913 | second_fname = argv[k]; |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 914 | else { |
| 915 | printf("too many arguments\n"); |
| 916 | file_name = 0; |
| 917 | break; |
| 918 | } |
| 919 | } |
Douglas Gilbert | 985b5cd | 2020-07-24 18:56:21 +0000 | [diff] [blame] | 920 | if ((iterator_test >= 0) || (object_walk_test >= 0)) |
Douglas Gilbert | 62c85c8 | 2020-06-05 16:19:48 +0000 | [diff] [blame] | 921 | nw_given = false; |
| 922 | |
Douglas Gilbert | 28903a1 | 2019-05-15 14:30:29 +0000 | [diff] [blame] | 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 | } |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 968 | if (0 == file_name) { |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 969 | printf("No filename (sg device) given\n\n"); |
Douglas Gilbert | 8101850 | 2018-11-27 18:38:56 +0000 | [diff] [blame] | 970 | usage(); |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 971 | return 1; |
| 972 | } |
Douglas Gilbert | 985b5cd | 2020-07-24 18:56:21 +0000 | [diff] [blame] | 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); |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 1002 | |
| 1003 | /* An access mode of O_RDWR is required for write()/read() interface */ |
Douglas Gilbert | 985b5cd | 2020-07-24 18:56:21 +0000 | [diff] [blame] | 1004 | if ((sg_fd = open(dname, O_RDWR)) < 0) { |
| 1005 | snprintf(ebuff, EBUFF_SZ, "error opening file: %s", dname); |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 1006 | perror(ebuff); |
| 1007 | return 1; |
| 1008 | } |
Douglas Gilbert | d6e191b | 2018-11-12 10:21:43 +0000 | [diff] [blame] | 1009 | if (verbose) |
| 1010 | fprintf(stderr, "opened given file: %s successfully, fd=%d\n", |
Douglas Gilbert | 985b5cd | 2020-07-24 18:56:21 +0000 | [diff] [blame] | 1011 | dname, sg_fd); |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 1012 | |
Douglas Gilbert | 28903a1 | 2019-05-15 14:30:29 +0000 | [diff] [blame] | 1013 | if (ioctl(sg_fd, SG_GET_VERSION_NUM, &sg_drv_ver_num) < 0) { |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 1014 | pr2serr("ioctl(SG_GET_VERSION_NUM) failed, errno=%d %s\n", errno, |
| 1015 | strerror(errno)); |
| 1016 | goto out; |
| 1017 | } |
Douglas Gilbert | 985b5cd | 2020-07-24 18:56:21 +0000 | [diff] [blame] | 1018 | if (is_first) |
| 1019 | printf("Linux sg driver version: %d\n", sg_drv_ver_num); |
Douglas Gilbert | 62c85c8 | 2020-06-05 16:19:48 +0000 | [diff] [blame] | 1020 | |
Douglas Gilbert | 207e525 | 2019-11-18 22:32:39 +0000 | [diff] [blame] | 1021 | if (create_time && (sg_drv_ver_num > 40030)) { |
Douglas Gilbert | 985b5cd | 2020-07-24 18:56:21 +0000 | [diff] [blame] | 1022 | pr_create_dev_time(sg_fd, dname); |
Douglas Gilbert | 207e525 | 2019-11-18 22:32:39 +0000 | [diff] [blame] | 1023 | goto out; |
| 1024 | } |
| 1025 | |
Douglas Gilbert | 985b5cd | 2020-07-24 18:56:21 +0000 | [diff] [blame] | 1026 | if (nw_given || (iterator_test >= 0) || (object_walk_test >= 0)) { |
| 1027 | /* -T=NUM and/or -I=0|1 or -j=0|1 */ |
Douglas Gilbert | 62c85c8 | 2020-06-05 16:19:48 +0000 | [diff] [blame] | 1028 | /* time ioctl(SG_GET_NUM_WAITING) or do iterator_test */ |
Douglas Gilbert | 985b5cd | 2020-07-24 18:56:21 +0000 | [diff] [blame] | 1029 | int nw; |
Douglas Gilbert | 3dfb85f | 2019-06-13 05:15:40 +0000 | [diff] [blame] | 1030 | struct timespec start_tm, fin_tm, res_tm; |
| 1031 | |
Douglas Gilbert | 985b5cd | 2020-07-24 18:56:21 +0000 | [diff] [blame] | 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) |
Douglas Gilbert | a37cbfd | 2020-09-03 15:32:35 +0000 | [diff] [blame] | 1037 | printf("Timing %d x %d calls to ioctl(SG_GET_NUM_WAITING)\n", |
Douglas Gilbert | 985b5cd | 2020-07-24 18:56:21 +0000 | [diff] [blame] | 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 | } |
Douglas Gilbert | 3dfb85f | 2019-06-13 05:15:40 +0000 | [diff] [blame] | 1051 | } |
Douglas Gilbert | 62c85c8 | 2020-06-05 16:19:48 +0000 | [diff] [blame] | 1052 | if (nw_given) { |
Douglas Gilbert | 985b5cd | 2020-07-24 18:56:21 +0000 | [diff] [blame] | 1053 | for (k = 0; k < num_sgnw; ++k, sum_nw += nw) { |
Douglas Gilbert | 62c85c8 | 2020-06-05 16:19:48 +0000 | [diff] [blame] | 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 | } |
Douglas Gilbert | 985b5cd | 2020-07-24 18:56:21 +0000 | [diff] [blame] | 1061 | } else if (iterator_test >= 0) { |
Douglas Gilbert | 62c85c8 | 2020-06-05 16:19:48 +0000 | [diff] [blame] | 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) { |
Douglas Gilbert | 5d21382 | 2020-07-17 03:50:13 +0000 | [diff] [blame] | 1069 | snprintf(ebuff, EBUFF_SZ, "%s: error opening file: %s", |
| 1070 | __func__, second_fname); |
Douglas Gilbert | 62c85c8 | 2020-06-05 16:19:48 +0000 | [diff] [blame] | 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) { |
Douglas Gilbert | 3dfb85f | 2019-06-13 05:15:40 +0000 | [diff] [blame] | 1094 | res = errno; |
Douglas Gilbert | 62c85c8 | 2020-06-05 16:19:48 +0000 | [diff] [blame] | 1095 | fprintf(stderr, "%s%d: ioctl(SG_SET_DEBUG) failed errno=%d\n", |
| 1096 | relative_cp, k, res); |
Douglas Gilbert | 3dfb85f | 2019-06-13 05:15:40 +0000 | [diff] [blame] | 1097 | goto out; |
Douglas Gilbert | 985b5cd | 2020-07-24 18:56:21 +0000 | [diff] [blame] | 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; |
Douglas Gilbert | 3dfb85f | 2019-06-13 05:15:40 +0000 | [diff] [blame] | 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) { |
Douglas Gilbert | 62c85c8 | 2020-06-05 16:19:48 +0000 | [diff] [blame] | 1137 | if (nw_given && (verbose > 1)) |
Douglas Gilbert | 3dfb85f | 2019-06-13 05:15:40 +0000 | [diff] [blame] | 1138 | printf("sum of num_waiting_s=%d\n", sum_nw); |
Douglas Gilbert | 62c85c8 | 2020-06-05 16:19:48 +0000 | [diff] [blame] | 1139 | printf("%selapsed time (nanosecond precision): %d.%09d secs\n", |
| 1140 | relative_cp, (int)res_tm.tv_sec, (int)res_tm.tv_nsec); |
Douglas Gilbert | 3dfb85f | 2019-06-13 05:15:40 +0000 | [diff] [blame] | 1141 | } else |
Douglas Gilbert | 62c85c8 | 2020-06-05 16:19:48 +0000 | [diff] [blame] | 1142 | printf("%selapsed time: %d.%06d secs\n", relative_cp, |
| 1143 | (int)res_tm.tv_sec, (int)(res_tm.tv_nsec / 1000)); |
Douglas Gilbert | 3dfb85f | 2019-06-13 05:15:40 +0000 | [diff] [blame] | 1144 | if (num_sgnw >= 100) { |
| 1145 | double m = (double)res_tm.tv_sec + |
| 1146 | ((double)res_tm.tv_nsec / 1000000000.0); |
Douglas Gilbert | a37cbfd | 2020-09-03 15:32:35 +0000 | [diff] [blame] | 1147 | double num = num_sgnw; |
Douglas Gilbert | 3dfb85f | 2019-06-13 05:15:40 +0000 | [diff] [blame] | 1148 | |
| 1149 | if (m > 0.000001) |
Douglas Gilbert | a37cbfd | 2020-09-03 15:32:35 +0000 | [diff] [blame] | 1150 | printf("%sCalls per second: %.2f\n", relative_cp, num / m); |
Douglas Gilbert | 3dfb85f | 2019-06-13 05:15:40 +0000 | [diff] [blame] | 1151 | } |
Douglas Gilbert | 3dfb85f | 2019-06-13 05:15:40 +0000 | [diff] [blame] | 1152 | res = 0; |
| 1153 | goto out; |
| 1154 | } |
Douglas Gilbert | 80c24fa | 2019-07-02 14:34:36 +0000 | [diff] [blame] | 1155 | if ((more_async || no_duration) && !do_v3_only) |
| 1156 | set_more_async(sg_fd, more_async, no_duration); |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 1157 | |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 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); |
Douglas Gilbert | 3e030c2 | 2019-05-18 15:37:19 +0000 | [diff] [blame] | 1168 | if (more_async && !do_v3_only) |
Douglas Gilbert | 80c24fa | 2019-07-02 14:34:36 +0000 | [diff] [blame] | 1169 | set_more_async(sg_fd2, more_async, no_duration); |
Douglas Gilbert | d6e191b | 2018-11-12 10:21:43 +0000 | [diff] [blame] | 1170 | } |
Douglas Gilbert | d6e191b | 2018-11-12 10:21:43 +0000 | [diff] [blame] | 1171 | |
Douglas Gilbert | 3e030c2 | 2019-05-18 15:37:19 +0000 | [diff] [blame] | 1172 | if ((num_mrqs > 0) && !do_v3_only) { |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 1173 | res = do_mrqs(sg_fd, sg_fd2, num_mrqs); |
| 1174 | goto out; |
| 1175 | } |
| 1176 | |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 1177 | if (do_fork) { |
| 1178 | int pid; |
| 1179 | int sv[2]; |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 1180 | |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 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 : ""; |
Douglas Gilbert | 7901cc4 | 2021-04-08 01:08:07 +0000 | [diff] [blame] | 1206 | if (! do_v3_only && (sg_drv_ver_num > 40030)) { |
Douglas Gilbert | 985b5cd | 2020-07-24 18:56:21 +0000 | [diff] [blame] | 1207 | if (tst_extended_ioctl(dname, sg_fd, second_fname, sg_fd2, sock, |
Douglas Gilbert | 3e030c2 | 2019-05-18 15:37:19 +0000 | [diff] [blame] | 1208 | cp)) |
| 1209 | goto out; |
| 1210 | } |
Douglas Gilbert | 8101850 | 2018-11-27 18:38:56 +0000 | [diff] [blame] | 1211 | if (ioctl_only) |
| 1212 | goto out; |
| 1213 | |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 1214 | if (do_fork && !is_parent) |
| 1215 | return 0; |
| 1216 | |
Douglas Gilbert | 3e030c2 | 2019-05-18 15:37:19 +0000 | [diff] [blame] | 1217 | printf("start write() calls [submits]\n"); |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 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) { |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 1247 | pr2serr("%ssg write errno=%d [%s]\n", cp, errno, strerror(errno)); |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 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); |
Douglas Gilbert | d491f4c | 2019-02-06 23:24:46 +0000 | [diff] [blame] | 1265 | printf(" SCSI 8 byte LUN: "); |
| 1266 | hex2stdout(ssi.scsi_lun, 8, -1); |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 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 |
Douglas Gilbert | d6e191b | 2018-11-12 10:21:43 +0000 | [diff] [blame] | 1272 | printf("first available pack_id: %d\n", pack_id); |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 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 | |
Douglas Gilbert | 8101850 | 2018-11-27 18:38:56 +0000 | [diff] [blame] | 1281 | if (write_only) |
| 1282 | goto out; |
| 1283 | |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 1284 | if (do_fork) |
| 1285 | printf("\n\nFollowing starting with get_pack_id are all CHILD\n"); |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 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 |
Douglas Gilbert | d6e191b | 2018-11-12 10:21:43 +0000 | [diff] [blame] | 1290 | printf("first available pack_id: %d\n", pack_id); |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 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 | |
Douglas Gilbert | 3e030c2 | 2019-05-18 15:37:19 +0000 | [diff] [blame] | 1297 | printf("\nstart read() calls [io receive]\n"); |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 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 |
Douglas Gilbert | d6e191b | 2018-11-12 10:21:43 +0000 | [diff] [blame] | 1306 | printf("first available pack_id: %d\n", pack_id); |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 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); |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 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) { |
Douglas Gilbert | 18c9852 | 2018-12-17 21:16:36 +0000 | [diff] [blame] | 1316 | perror("sg read error"); |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 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 */ |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 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: |
Douglas Gilbert | 62c85c8 | 2020-06-05 16:19:48 +0000 | [diff] [blame] | 1346 | if (sg_fd >= 0) |
| 1347 | close(sg_fd); |
Douglas Gilbert | d6e191b | 2018-11-12 10:21:43 +0000 | [diff] [blame] | 1348 | if (sg_fd2 >= 0) |
| 1349 | close(sg_fd2); |
Douglas Gilbert | 072494a | 2019-03-26 03:54:53 +0000 | [diff] [blame] | 1350 | return res; |
Douglas Gilbert | 01a118f | 2018-10-18 15:57:29 +0000 | [diff] [blame] | 1351 | } |