Commit | Line | Data |
---|---|---|
2866c82d | 1 | /* |
da751ca9 JA |
2 | * sg engine |
3 | * | |
4 | * IO engine that uses the Linux SG v3 interface to talk to SCSI devices | |
2866c82d | 5 | * |
b4b9665e VF |
6 | * This ioengine can operate in two modes: |
7 | * sync with block devices (/dev/sdX) or | |
8 | * with character devices (/dev/sgY) with direct=1 or sync=1 | |
9 | * async with character devices with direct=0 and sync=0 | |
10 | * | |
11 | * What value does queue() return for the different cases? | |
12 | * queue() return value | |
13 | * In sync mode: | |
14 | * /dev/sdX RWT FIO_Q_COMPLETED | |
15 | * /dev/sgY RWT FIO_Q_COMPLETED | |
16 | * with direct=1 or sync=1 | |
17 | * | |
18 | * In async mode: | |
19 | * /dev/sgY RWT FIO_Q_QUEUED | |
20 | * direct=0 and sync=0 | |
21 | * | |
22 | * Because FIO_SYNCIO is set for this ioengine td_io_queue() will fill in | |
23 | * issue_time *before* each IO is sent to queue() | |
24 | * | |
25 | * Where are the IO counting functions called for the different cases? | |
26 | * | |
27 | * In sync mode: | |
28 | * /dev/sdX (commit==NULL) | |
29 | * RWT | |
30 | * io_u_mark_depth() called in td_io_queue() | |
31 | * io_u_mark_submit/complete() called in td_io_queue() | |
32 | * issue_time set in td_io_queue() | |
33 | * | |
34 | * /dev/sgY with direct=1 or sync=1 (commit does nothing) | |
35 | * RWT | |
36 | * io_u_mark_depth() called in td_io_queue() | |
37 | * io_u_mark_submit/complete() called in queue() | |
38 | * issue_time set in td_io_queue() | |
39 | * | |
40 | * In async mode: | |
41 | * /dev/sgY with direct=0 and sync=0 | |
42 | * RW: read and write operations are submitted in queue() | |
43 | * io_u_mark_depth() called in td_io_commit() | |
44 | * io_u_mark_submit() called in queue() | |
45 | * issue_time set in td_io_queue() | |
46 | * T: trim operations are queued in queue() and submitted in commit() | |
47 | * io_u_mark_depth() called in td_io_commit() | |
48 | * io_u_mark_submit() called in commit() | |
49 | * issue_time set in commit() | |
50 | * | |
2866c82d JA |
51 | */ |
52 | #include <stdio.h> | |
53 | #include <stdlib.h> | |
54 | #include <unistd.h> | |
55 | #include <errno.h> | |
8393ca93 | 56 | #include <poll.h> |
5f350952 JA |
57 | |
58 | #include "../fio.h" | |
52b81b7c | 59 | #include "../optgroup.h" |
2866c82d | 60 | |
34cfcdaf JA |
61 | #ifdef FIO_HAVE_SGIO |
62 | ||
b0dc148e DG |
63 | #ifndef SGV4_FLAG_HIPRI |
64 | #define SGV4_FLAG_HIPRI 0x800 | |
65 | #endif | |
66 | ||
cbdc9353 VF |
67 | enum { |
68 | FIO_SG_WRITE = 1, | |
e8ab121c VF |
69 | FIO_SG_WRITE_VERIFY, |
70 | FIO_SG_WRITE_SAME, | |
91e13ff5 | 71 | FIO_SG_WRITE_SAME_NDOB, |
e8ab121c VF |
72 | FIO_SG_VERIFY_BYTCHK_00, |
73 | FIO_SG_VERIFY_BYTCHK_01, | |
74 | FIO_SG_VERIFY_BYTCHK_11, | |
cbdc9353 | 75 | }; |
52b81b7c KD |
76 | |
77 | struct sg_options { | |
78 | void *pad; | |
b0dc148e | 79 | unsigned int hipri; |
52b81b7c KD |
80 | unsigned int readfua; |
81 | unsigned int writefua; | |
cbdc9353 | 82 | unsigned int write_mode; |
52b81b7c KD |
83 | }; |
84 | ||
85 | static struct fio_option options[] = { | |
b0dc148e DG |
86 | { |
87 | .name = "hipri", | |
88 | .lname = "High Priority", | |
89 | .type = FIO_OPT_STR_SET, | |
90 | .off1 = offsetof(struct sg_options, hipri), | |
91 | .help = "Use polled IO completions", | |
92 | .category = FIO_OPT_C_ENGINE, | |
93 | .group = FIO_OPT_G_SG, | |
94 | }, | |
52b81b7c KD |
95 | { |
96 | .name = "readfua", | |
97 | .lname = "sg engine read fua flag support", | |
98 | .type = FIO_OPT_BOOL, | |
99 | .off1 = offsetof(struct sg_options, readfua), | |
100 | .help = "Set FUA flag (force unit access) for all Read operations", | |
101 | .def = "0", | |
102 | .category = FIO_OPT_C_ENGINE, | |
103 | .group = FIO_OPT_G_SG, | |
104 | }, | |
105 | { | |
106 | .name = "writefua", | |
107 | .lname = "sg engine write fua flag support", | |
108 | .type = FIO_OPT_BOOL, | |
109 | .off1 = offsetof(struct sg_options, writefua), | |
110 | .help = "Set FUA flag (force unit access) for all Write operations", | |
111 | .def = "0", | |
112 | .category = FIO_OPT_C_ENGINE, | |
113 | .group = FIO_OPT_G_SG, | |
114 | }, | |
cbdc9353 VF |
115 | { |
116 | .name = "sg_write_mode", | |
117 | .lname = "specify sg write mode", | |
118 | .type = FIO_OPT_STR, | |
119 | .off1 = offsetof(struct sg_options, write_mode), | |
120 | .help = "Specify SCSI WRITE mode", | |
121 | .def = "write", | |
122 | .posval = { | |
123 | { .ival = "write", | |
124 | .oval = FIO_SG_WRITE, | |
125 | .help = "Issue standard SCSI WRITE commands", | |
126 | }, | |
eadf3260 | 127 | { .ival = "write_and_verify", |
cbdc9353 VF |
128 | .oval = FIO_SG_WRITE_VERIFY, |
129 | .help = "Issue SCSI WRITE AND VERIFY commands", | |
130 | }, | |
eadf3260 VF |
131 | { .ival = "verify", |
132 | .oval = FIO_SG_WRITE_VERIFY, | |
133 | .help = "Issue SCSI WRITE AND VERIFY commands. This " | |
134 | "option is deprecated. Use write_and_verify instead.", | |
135 | }, | |
136 | { .ival = "write_same", | |
cbdc9353 VF |
137 | .oval = FIO_SG_WRITE_SAME, |
138 | .help = "Issue SCSI WRITE SAME commands", | |
139 | }, | |
eadf3260 VF |
140 | { .ival = "same", |
141 | .oval = FIO_SG_WRITE_SAME, | |
142 | .help = "Issue SCSI WRITE SAME commands. This " | |
143 | "option is deprecated. Use write_same instead.", | |
144 | }, | |
91e13ff5 VF |
145 | { .ival = "write_same_ndob", |
146 | .oval = FIO_SG_WRITE_SAME_NDOB, | |
147 | .help = "Issue SCSI WRITE SAME(16) commands with NDOB flag set", | |
148 | }, | |
e8ab121c VF |
149 | { .ival = "verify_bytchk_00", |
150 | .oval = FIO_SG_VERIFY_BYTCHK_00, | |
151 | .help = "Issue SCSI VERIFY commands with BYTCHK set to 00", | |
152 | }, | |
153 | { .ival = "verify_bytchk_01", | |
154 | .oval = FIO_SG_VERIFY_BYTCHK_01, | |
155 | .help = "Issue SCSI VERIFY commands with BYTCHK set to 01", | |
156 | }, | |
157 | { .ival = "verify_bytchk_11", | |
158 | .oval = FIO_SG_VERIFY_BYTCHK_11, | |
159 | .help = "Issue SCSI VERIFY commands with BYTCHK set to 11", | |
160 | }, | |
cbdc9353 VF |
161 | }, |
162 | .category = FIO_OPT_C_ENGINE, | |
163 | .group = FIO_OPT_G_SG, | |
164 | }, | |
52b81b7c KD |
165 | { |
166 | .name = NULL, | |
167 | }, | |
168 | }; | |
169 | ||
5ad7be56 KD |
170 | #define MAX_10B_LBA 0xFFFFFFFFULL |
171 | #define SCSI_TIMEOUT_MS 30000 // 30 second timeout; currently no method to override | |
172 | #define MAX_SB 64 // sense block maximum return size | |
aa18e0ec VF |
173 | /* |
174 | #define FIO_SGIO_DEBUG | |
175 | */ | |
5ad7be56 | 176 | |
2866c82d | 177 | struct sgio_cmd { |
fde57152 | 178 | unsigned char cdb[16]; // enhanced from 10 to support 16 byte commands |
5ad7be56 | 179 | unsigned char sb[MAX_SB]; // add sense block to commands |
2866c82d JA |
180 | int nr; |
181 | }; | |
182 | ||
b4b9665e | 183 | struct sgio_trim { |
a824149a | 184 | uint8_t *unmap_param; |
b4b9665e VF |
185 | unsigned int unmap_range_count; |
186 | struct io_u **trim_io_us; | |
187 | }; | |
188 | ||
2866c82d JA |
189 | struct sgio_data { |
190 | struct sgio_cmd *cmds; | |
191 | struct io_u **events; | |
dc0deca2 JA |
192 | struct pollfd *pfds; |
193 | int *fd_flags; | |
194 | void *sgbuf; | |
2866c82d | 195 | unsigned int bs; |
b5af8293 | 196 | int type_checked; |
b4b9665e VF |
197 | struct sgio_trim **trim_queues; |
198 | int current_queue; | |
aa18e0ec | 199 | #ifdef FIO_SGIO_DEBUG |
b4b9665e | 200 | unsigned int *trim_queue_map; |
aa18e0ec | 201 | #endif |
2866c82d JA |
202 | }; |
203 | ||
a824149a DF |
204 | static inline uint32_t sgio_get_be32(uint8_t *buf) |
205 | { | |
206 | return be32_to_cpu(*((uint32_t *) buf)); | |
207 | } | |
208 | ||
209 | static inline uint64_t sgio_get_be64(uint8_t *buf) | |
210 | { | |
211 | return be64_to_cpu(*((uint64_t *) buf)); | |
212 | } | |
213 | ||
214 | static inline void sgio_set_be16(uint16_t val, uint8_t *buf) | |
215 | { | |
216 | uint16_t t = cpu_to_be16(val); | |
217 | ||
218 | memcpy(buf, &t, sizeof(uint16_t)); | |
219 | } | |
220 | ||
221 | static inline void sgio_set_be32(uint32_t val, uint8_t *buf) | |
222 | { | |
223 | uint32_t t = cpu_to_be32(val); | |
224 | ||
225 | memcpy(buf, &t, sizeof(uint32_t)); | |
226 | } | |
227 | ||
228 | static inline void sgio_set_be64(uint64_t val, uint8_t *buf) | |
229 | { | |
230 | uint64_t t = cpu_to_be64(val); | |
231 | ||
232 | memcpy(buf, &t, sizeof(uint64_t)); | |
233 | } | |
234 | ||
b4b9665e VF |
235 | static inline bool sgio_unbuffered(struct thread_data *td) |
236 | { | |
237 | return (td->o.odirect || td->o.sync_io); | |
238 | } | |
239 | ||
2866c82d JA |
240 | static void sgio_hdr_init(struct sgio_data *sd, struct sg_io_hdr *hdr, |
241 | struct io_u *io_u, int fs) | |
242 | { | |
243 | struct sgio_cmd *sc = &sd->cmds[io_u->index]; | |
244 | ||
245 | memset(hdr, 0, sizeof(*hdr)); | |
246 | memset(sc->cdb, 0, sizeof(sc->cdb)); | |
247 | ||
248 | hdr->interface_id = 'S'; | |
249 | hdr->cmdp = sc->cdb; | |
250 | hdr->cmd_len = sizeof(sc->cdb); | |
5ad7be56 KD |
251 | hdr->sbp = sc->sb; |
252 | hdr->mx_sb_len = sizeof(sc->sb); | |
2866c82d JA |
253 | hdr->pack_id = io_u->index; |
254 | hdr->usr_ptr = io_u; | |
b4b9665e | 255 | hdr->timeout = SCSI_TIMEOUT_MS; |
2866c82d JA |
256 | |
257 | if (fs) { | |
cec6b55d JA |
258 | hdr->dxferp = io_u->xfer_buf; |
259 | hdr->dxfer_len = io_u->xfer_buflen; | |
2866c82d JA |
260 | } |
261 | } | |
262 | ||
adee86c5 JA |
263 | static int pollin_events(struct pollfd *pfds, int fds) |
264 | { | |
265 | int i; | |
266 | ||
267 | for (i = 0; i < fds; i++) | |
268 | if (pfds[i].revents & POLLIN) | |
269 | return 1; | |
270 | ||
271 | return 0; | |
272 | } | |
2866c82d | 273 | |
14d0261e JA |
274 | static int sg_fd_read(int fd, void *data, size_t size) |
275 | { | |
276 | int err = 0; | |
277 | ||
278 | while (size) { | |
279 | ssize_t ret; | |
280 | ||
281 | ret = read(fd, data, size); | |
282 | if (ret < 0) { | |
283 | if (errno == EAGAIN || errno == EINTR) | |
284 | continue; | |
285 | err = errno; | |
286 | break; | |
287 | } else if (!ret) | |
288 | break; | |
289 | else { | |
290 | data += ret; | |
291 | size -= ret; | |
292 | } | |
293 | } | |
294 | ||
295 | if (err) | |
296 | return err; | |
297 | if (size) | |
298 | return EAGAIN; | |
299 | ||
300 | return 0; | |
301 | } | |
302 | ||
e7d2e616 | 303 | static int fio_sgio_getevents(struct thread_data *td, unsigned int min, |
1f440ece JA |
304 | unsigned int max, |
305 | const struct timespec fio_unused *t) | |
2866c82d | 306 | { |
565e784d | 307 | struct sgio_data *sd = td->io_ops_data; |
b4b9665e | 308 | int left = max, eventNum, ret, r = 0, trims = 0; |
dc0deca2 | 309 | void *buf = sd->sgbuf; |
b4b9665e | 310 | unsigned int i, j, events; |
946ff865 | 311 | struct fio_file *f; |
b4b9665e | 312 | struct io_u *io_u; |
2866c82d JA |
313 | |
314 | /* | |
adee86c5 | 315 | * Fill in the file descriptors |
2866c82d | 316 | */ |
adee86c5 JA |
317 | for_each_file(td, f, i) { |
318 | /* | |
319 | * don't block for min events == 0 | |
320 | */ | |
4a851614 | 321 | if (!min) |
3a35845f JA |
322 | sd->fd_flags[i] = fio_set_fd_nonblocking(f->fd, "sg"); |
323 | else | |
324 | sd->fd_flags[i] = -1; | |
4a851614 | 325 | |
dc0deca2 JA |
326 | sd->pfds[i].fd = f->fd; |
327 | sd->pfds[i].events = POLLIN; | |
2866c82d JA |
328 | } |
329 | ||
b4b9665e VF |
330 | /* |
331 | ** There are two counters here: | |
332 | ** - number of SCSI commands completed | |
333 | ** - number of io_us completed | |
334 | ** | |
335 | ** These are the same with reads and writes, but | |
336 | ** could differ with trim/unmap commands because | |
337 | ** a single unmap can include multiple io_us | |
338 | */ | |
339 | ||
340 | while (left > 0) { | |
c97e3cb0 | 341 | char *p; |
adee86c5 | 342 | |
b4b9665e | 343 | dprint(FD_IO, "sgio_getevents: sd %p: min=%d, max=%d, left=%d\n", sd, min, max, left); |
5ad7be56 | 344 | |
2866c82d JA |
345 | do { |
346 | if (!min) | |
347 | break; | |
adee86c5 | 348 | |
2dc1bbeb | 349 | ret = poll(sd->pfds, td->o.nr_files, -1); |
adee86c5 | 350 | if (ret < 0) { |
adee86c5 | 351 | if (!r) |
22819ec2 | 352 | r = -errno; |
e1161c32 | 353 | td_verror(td, errno, "poll"); |
adee86c5 JA |
354 | break; |
355 | } else if (!ret) | |
356 | continue; | |
357 | ||
2dc1bbeb | 358 | if (pollin_events(sd->pfds, td->o.nr_files)) |
2866c82d JA |
359 | break; |
360 | } while (1); | |
361 | ||
adee86c5 | 362 | if (r < 0) |
2866c82d | 363 | break; |
adee86c5 JA |
364 | |
365 | re_read: | |
366 | p = buf; | |
367 | events = 0; | |
368 | for_each_file(td, f, i) { | |
5ad7be56 | 369 | for (eventNum = 0; eventNum < left; eventNum++) { |
14d0261e | 370 | ret = sg_fd_read(f->fd, p, sizeof(struct sg_io_hdr)); |
b4b9665e | 371 | dprint(FD_IO, "sgio_getevents: sg_fd_read ret: %d\n", ret); |
14d0261e JA |
372 | if (ret) { |
373 | r = -ret; | |
374 | td_verror(td, r, "sg_read"); | |
5ad7be56 | 375 | break; |
5ad7be56 | 376 | } |
b4b9665e VF |
377 | io_u = ((struct sg_io_hdr *)p)->usr_ptr; |
378 | if (io_u->ddir == DDIR_TRIM) { | |
379 | events += sd->trim_queues[io_u->index]->unmap_range_count; | |
380 | eventNum += sd->trim_queues[io_u->index]->unmap_range_count - 1; | |
381 | } else | |
382 | events++; | |
383 | ||
14d0261e | 384 | p += sizeof(struct sg_io_hdr); |
b4b9665e | 385 | dprint(FD_IO, "sgio_getevents: events: %d, eventNum: %d, left: %d\n", events, eventNum, left); |
adee86c5 JA |
386 | } |
387 | } | |
388 | ||
14d0261e | 389 | if (r < 0 && !events) |
2866c82d | 390 | break; |
adee86c5 JA |
391 | if (!events) { |
392 | usleep(1000); | |
393 | goto re_read; | |
394 | } | |
2866c82d | 395 | |
2866c82d JA |
396 | left -= events; |
397 | r += events; | |
398 | ||
399 | for (i = 0; i < events; i++) { | |
400 | struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i; | |
b4b9665e VF |
401 | sd->events[i + trims] = hdr->usr_ptr; |
402 | io_u = (struct io_u *)(hdr->usr_ptr); | |
5ad7be56 | 403 | |
5ad7be56 | 404 | if (hdr->info & SG_INFO_CHECK) { |
b4b9665e | 405 | /* record if an io error occurred, ignore resid */ |
be660713 | 406 | memcpy(&io_u->hdr, hdr, sizeof(struct sg_io_hdr)); |
b4b9665e VF |
407 | sd->events[i + trims]->error = EIO; |
408 | } | |
409 | ||
410 | if (io_u->ddir == DDIR_TRIM) { | |
411 | struct sgio_trim *st = sd->trim_queues[io_u->index]; | |
aa18e0ec | 412 | #ifdef FIO_SGIO_DEBUG |
b4b9665e | 413 | assert(st->trim_io_us[0] == io_u); |
aa18e0ec | 414 | assert(sd->trim_queue_map[io_u->index] == io_u->index); |
b4b9665e VF |
415 | dprint(FD_IO, "sgio_getevents: reaping %d io_us from trim queue %d\n", st->unmap_range_count, io_u->index); |
416 | dprint(FD_IO, "sgio_getevents: reaped io_u %d and stored in events[%d]\n", io_u->index, i+trims); | |
aa18e0ec | 417 | #endif |
b4b9665e VF |
418 | for (j = 1; j < st->unmap_range_count; j++) { |
419 | ++trims; | |
420 | sd->events[i + trims] = st->trim_io_us[j]; | |
aa18e0ec | 421 | #ifdef FIO_SGIO_DEBUG |
b4b9665e | 422 | dprint(FD_IO, "sgio_getevents: reaped io_u %d and stored in events[%d]\n", st->trim_io_us[j]->index, i+trims); |
aa18e0ec VF |
423 | assert(sd->trim_queue_map[st->trim_io_us[j]->index] == io_u->index); |
424 | #endif | |
b4b9665e VF |
425 | if (hdr->info & SG_INFO_CHECK) { |
426 | /* record if an io error occurred, ignore resid */ | |
427 | memcpy(&st->trim_io_us[j]->hdr, hdr, sizeof(struct sg_io_hdr)); | |
428 | sd->events[i + trims]->error = EIO; | |
429 | } | |
430 | } | |
431 | events -= st->unmap_range_count - 1; | |
432 | st->unmap_range_count = 0; | |
5ad7be56 | 433 | } |
2866c82d JA |
434 | } |
435 | } | |
436 | ||
adee86c5 | 437 | if (!min) { |
affe05a9 | 438 | for_each_file(td, f, i) { |
3a35845f JA |
439 | if (sd->fd_flags[i] == -1) |
440 | continue; | |
441 | ||
affe05a9 JA |
442 | if (fcntl(f->fd, F_SETFL, sd->fd_flags[i]) < 0) |
443 | log_err("fio: sg failed to restore fcntl flags: %s\n", strerror(errno)); | |
444 | } | |
adee86c5 | 445 | } |
2866c82d | 446 | |
2866c82d JA |
447 | return r; |
448 | } | |
449 | ||
2e4ef4fb JA |
450 | static enum fio_q_status fio_sgio_ioctl_doio(struct thread_data *td, |
451 | struct fio_file *f, | |
452 | struct io_u *io_u) | |
2866c82d | 453 | { |
565e784d | 454 | struct sgio_data *sd = td->io_ops_data; |
2866c82d | 455 | struct sg_io_hdr *hdr = &io_u->hdr; |
36167d82 | 456 | int ret; |
2866c82d JA |
457 | |
458 | sd->events[0] = io_u; | |
459 | ||
36167d82 JA |
460 | ret = ioctl(f->fd, SG_IO, hdr); |
461 | if (ret < 0) | |
a05bd42d | 462 | return ret; |
36167d82 | 463 | |
5ad7be56 KD |
464 | /* record if an io error occurred */ |
465 | if (hdr->info & SG_INFO_CHECK) | |
466 | io_u->error = EIO; | |
467 | ||
36167d82 | 468 | return FIO_Q_COMPLETED; |
2866c82d JA |
469 | } |
470 | ||
a999bc49 JA |
471 | static enum fio_q_status fio_sgio_rw_doio(struct thread_data *td, |
472 | struct fio_file *f, | |
b4b9665e | 473 | struct io_u *io_u, int do_sync) |
2866c82d JA |
474 | { |
475 | struct sg_io_hdr *hdr = &io_u->hdr; | |
476 | int ret; | |
477 | ||
53cdc686 | 478 | ret = write(f->fd, hdr, sizeof(*hdr)); |
2866c82d | 479 | if (ret < 0) |
a05bd42d | 480 | return ret; |
2866c82d | 481 | |
2b13e716 | 482 | if (do_sync) { |
a999bc49 JA |
483 | /* |
484 | * We can't just read back the first command that completes | |
485 | * and assume it's the one we need, it could be any command | |
486 | * that is inflight. | |
487 | */ | |
488 | do { | |
489 | struct io_u *__io_u; | |
490 | ||
491 | ret = read(f->fd, hdr, sizeof(*hdr)); | |
492 | if (ret < 0) | |
493 | return ret; | |
494 | ||
7508b394 JA |
495 | __io_u = hdr->usr_ptr; |
496 | ||
a999bc49 JA |
497 | /* record if an io error occurred */ |
498 | if (hdr->info & SG_INFO_CHECK) | |
7508b394 | 499 | __io_u->error = EIO; |
5ad7be56 | 500 | |
a999bc49 JA |
501 | if (__io_u == io_u) |
502 | break; | |
503 | ||
691df596 | 504 | if (io_u_sync_complete(td, __io_u)) |
a999bc49 | 505 | break; |
691df596 | 506 | |
a999bc49 | 507 | } while (1); |
5ad7be56 | 508 | |
36167d82 | 509 | return FIO_Q_COMPLETED; |
2866c82d JA |
510 | } |
511 | ||
36167d82 | 512 | return FIO_Q_QUEUED; |
2866c82d JA |
513 | } |
514 | ||
b4b9665e VF |
515 | static enum fio_q_status fio_sgio_doio(struct thread_data *td, |
516 | struct io_u *io_u, int do_sync) | |
2866c82d | 517 | { |
53cdc686 | 518 | struct fio_file *f = io_u->file; |
b4b9665e | 519 | enum fio_q_status ret; |
53cdc686 | 520 | |
686fbd31 | 521 | if (f->filetype == FIO_TYPE_BLOCK) { |
5ad7be56 | 522 | ret = fio_sgio_ioctl_doio(td, f, io_u); |
a999bc49 JA |
523 | if (io_u->error) |
524 | td_verror(td, io_u->error, __func__); | |
5ad7be56 | 525 | } else { |
a999bc49 JA |
526 | ret = fio_sgio_rw_doio(td, f, io_u, do_sync); |
527 | if (io_u->error && do_sync) | |
c9aeb797 | 528 | td_verror(td, io_u->error, __func__); |
5ad7be56 | 529 | } |
2866c82d | 530 | |
5ad7be56 | 531 | return ret; |
2866c82d JA |
532 | } |
533 | ||
b4b9665e | 534 | static void fio_sgio_rw_lba(struct sg_io_hdr *hdr, unsigned long long lba, |
91e13ff5 | 535 | unsigned long long nr_blocks, bool override16) |
b4b9665e | 536 | { |
91e13ff5 | 537 | if (lba < MAX_10B_LBA && !override16) { |
a824149a DF |
538 | sgio_set_be32((uint32_t) lba, &hdr->cmdp[2]); |
539 | sgio_set_be16((uint16_t) nr_blocks, &hdr->cmdp[7]); | |
b4b9665e | 540 | } else { |
a824149a DF |
541 | sgio_set_be64(lba, &hdr->cmdp[2]); |
542 | sgio_set_be32((uint32_t) nr_blocks, &hdr->cmdp[10]); | |
b4b9665e VF |
543 | } |
544 | ||
545 | return; | |
546 | } | |
547 | ||
2866c82d JA |
548 | static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u) |
549 | { | |
550 | struct sg_io_hdr *hdr = &io_u->hdr; | |
52b81b7c | 551 | struct sg_options *o = td->eo; |
565e784d | 552 | struct sgio_data *sd = td->io_ops_data; |
b4b9665e VF |
553 | unsigned long long nr_blocks, lba; |
554 | int offset; | |
2866c82d | 555 | |
cec6b55d | 556 | if (io_u->xfer_buflen & (sd->bs - 1)) { |
2866c82d JA |
557 | log_err("read/write not sector aligned\n"); |
558 | return EINVAL; | |
559 | } | |
560 | ||
5ad7be56 KD |
561 | nr_blocks = io_u->xfer_buflen / sd->bs; |
562 | lba = io_u->offset / sd->bs; | |
563 | ||
2866c82d | 564 | if (io_u->ddir == DDIR_READ) { |
87dc1ab1 JA |
565 | sgio_hdr_init(sd, hdr, io_u, 1); |
566 | ||
2866c82d | 567 | hdr->dxfer_direction = SG_DXFER_FROM_DEV; |
5ad7be56 KD |
568 | if (lba < MAX_10B_LBA) |
569 | hdr->cmdp[0] = 0x28; // read(10) | |
570 | else | |
571 | hdr->cmdp[0] = 0x88; // read(16) | |
52b81b7c | 572 | |
b0dc148e DG |
573 | if (o->hipri) |
574 | hdr->flags |= SGV4_FLAG_HIPRI; | |
52b81b7c KD |
575 | if (o->readfua) |
576 | hdr->cmdp[1] |= 0x08; | |
577 | ||
91e13ff5 | 578 | fio_sgio_rw_lba(hdr, lba, nr_blocks, false); |
b4b9665e | 579 | |
87dc1ab1 JA |
580 | } else if (io_u->ddir == DDIR_WRITE) { |
581 | sgio_hdr_init(sd, hdr, io_u, 1); | |
582 | ||
2866c82d | 583 | hdr->dxfer_direction = SG_DXFER_TO_DEV; |
cbdc9353 VF |
584 | switch(o->write_mode) { |
585 | case FIO_SG_WRITE: | |
586 | if (lba < MAX_10B_LBA) | |
587 | hdr->cmdp[0] = 0x2a; // write(10) | |
588 | else | |
589 | hdr->cmdp[0] = 0x8a; // write(16) | |
b0dc148e DG |
590 | if (o->hipri) |
591 | hdr->flags |= SGV4_FLAG_HIPRI; | |
cbdc9353 VF |
592 | if (o->writefua) |
593 | hdr->cmdp[1] |= 0x08; | |
594 | break; | |
595 | case FIO_SG_WRITE_VERIFY: | |
596 | if (lba < MAX_10B_LBA) | |
597 | hdr->cmdp[0] = 0x2e; // write and verify(10) | |
598 | else | |
599 | hdr->cmdp[0] = 0x8e; // write and verify(16) | |
600 | break; | |
601 | // BYTCHK is disabled by virtue of the memset in sgio_hdr_init | |
602 | case FIO_SG_WRITE_SAME: | |
603 | hdr->dxfer_len = sd->bs; | |
604 | if (lba < MAX_10B_LBA) | |
605 | hdr->cmdp[0] = 0x41; // write same(10) | |
606 | else | |
607 | hdr->cmdp[0] = 0x93; // write same(16) | |
608 | break; | |
91e13ff5 VF |
609 | case FIO_SG_WRITE_SAME_NDOB: |
610 | hdr->cmdp[0] = 0x93; // write same(16) | |
611 | hdr->cmdp[1] |= 0x1; // no data output buffer | |
612 | hdr->dxfer_len = 0; | |
613 | break; | |
e8ab121c VF |
614 | case FIO_SG_VERIFY_BYTCHK_00: |
615 | if (lba < MAX_10B_LBA) | |
616 | hdr->cmdp[0] = 0x2f; // VERIFY(10) | |
617 | else | |
618 | hdr->cmdp[0] = 0x8f; // VERIFY(16) | |
619 | hdr->dxfer_len = 0; | |
620 | break; | |
621 | case FIO_SG_VERIFY_BYTCHK_01: | |
622 | if (lba < MAX_10B_LBA) | |
623 | hdr->cmdp[0] = 0x2f; // VERIFY(10) | |
624 | else | |
625 | hdr->cmdp[0] = 0x8f; // VERIFY(16) | |
626 | hdr->cmdp[1] |= 0x02; // BYTCHK = 01b | |
627 | break; | |
628 | case FIO_SG_VERIFY_BYTCHK_11: | |
629 | if (lba < MAX_10B_LBA) | |
630 | hdr->cmdp[0] = 0x2f; // VERIFY(10) | |
631 | else | |
632 | hdr->cmdp[0] = 0x8f; // VERIFY(16) | |
633 | hdr->cmdp[1] |= 0x06; // BYTCHK = 11b | |
634 | hdr->dxfer_len = sd->bs; | |
635 | break; | |
cbdc9353 | 636 | }; |
b4b9665e | 637 | |
91e13ff5 VF |
638 | fio_sgio_rw_lba(hdr, lba, nr_blocks, |
639 | o->write_mode == FIO_SG_WRITE_SAME_NDOB); | |
b4b9665e VF |
640 | |
641 | } else if (io_u->ddir == DDIR_TRIM) { | |
642 | struct sgio_trim *st; | |
643 | ||
644 | if (sd->current_queue == -1) { | |
645 | sgio_hdr_init(sd, hdr, io_u, 0); | |
646 | ||
647 | hdr->cmd_len = 10; | |
648 | hdr->dxfer_direction = SG_DXFER_TO_DEV; | |
649 | hdr->cmdp[0] = 0x42; // unmap | |
650 | sd->current_queue = io_u->index; | |
651 | st = sd->trim_queues[sd->current_queue]; | |
652 | hdr->dxferp = st->unmap_param; | |
aa18e0ec | 653 | #ifdef FIO_SGIO_DEBUG |
b4b9665e VF |
654 | assert(sd->trim_queues[io_u->index]->unmap_range_count == 0); |
655 | dprint(FD_IO, "sg: creating new queue based on io_u %d\n", io_u->index); | |
aa18e0ec | 656 | #endif |
b4b9665e VF |
657 | } |
658 | else | |
659 | st = sd->trim_queues[sd->current_queue]; | |
660 | ||
661 | dprint(FD_IO, "sg: adding io_u %d to trim queue %d\n", io_u->index, sd->current_queue); | |
662 | st->trim_io_us[st->unmap_range_count] = io_u; | |
aa18e0ec | 663 | #ifdef FIO_SGIO_DEBUG |
b4b9665e | 664 | sd->trim_queue_map[io_u->index] = sd->current_queue; |
aa18e0ec | 665 | #endif |
b4b9665e VF |
666 | |
667 | offset = 8 + 16 * st->unmap_range_count; | |
a824149a DF |
668 | sgio_set_be64(lba, &st->unmap_param[offset]); |
669 | sgio_set_be32((uint32_t) nr_blocks, &st->unmap_param[offset + 8]); | |
b4b9665e VF |
670 | |
671 | st->unmap_range_count++; | |
672 | ||
673 | } else if (ddir_sync(io_u->ddir)) { | |
87dc1ab1 | 674 | sgio_hdr_init(sd, hdr, io_u, 0); |
87dc1ab1 | 675 | hdr->dxfer_direction = SG_DXFER_NONE; |
5ad7be56 KD |
676 | if (lba < MAX_10B_LBA) |
677 | hdr->cmdp[0] = 0x35; // synccache(10) | |
678 | else | |
679 | hdr->cmdp[0] = 0x91; // synccache(16) | |
b4b9665e VF |
680 | } else |
681 | assert(0); | |
2866c82d | 682 | |
2866c82d JA |
683 | return 0; |
684 | } | |
685 | ||
b4b9665e VF |
686 | static void fio_sgio_unmap_setup(struct sg_io_hdr *hdr, struct sgio_trim *st) |
687 | { | |
a824149a | 688 | uint16_t cnt = st->unmap_range_count * 16; |
b4b9665e | 689 | |
a824149a DF |
690 | hdr->dxfer_len = cnt + 8; |
691 | sgio_set_be16(cnt + 8, &hdr->cmdp[7]); | |
692 | sgio_set_be16(cnt + 6, st->unmap_param); | |
693 | sgio_set_be16(cnt, &st->unmap_param[2]); | |
b4b9665e VF |
694 | |
695 | return; | |
696 | } | |
697 | ||
2e4ef4fb JA |
698 | static enum fio_q_status fio_sgio_queue(struct thread_data *td, |
699 | struct io_u *io_u) | |
2866c82d JA |
700 | { |
701 | struct sg_io_hdr *hdr = &io_u->hdr; | |
b4b9665e | 702 | struct sgio_data *sd = td->io_ops_data; |
f6db4fa5 | 703 | int ret, do_sync = 0; |
2866c82d | 704 | |
7101d9c2 JA |
705 | fio_ro_check(td, io_u); |
706 | ||
b4b9665e | 707 | if (sgio_unbuffered(td) || ddir_sync(io_u->ddir)) |
f6db4fa5 JA |
708 | do_sync = 1; |
709 | ||
b4b9665e VF |
710 | if (io_u->ddir == DDIR_TRIM) { |
711 | if (do_sync || io_u->file->filetype == FIO_TYPE_BLOCK) { | |
712 | struct sgio_trim *st = sd->trim_queues[sd->current_queue]; | |
713 | ||
714 | /* finish cdb setup for unmap because we are | |
715 | ** doing unmap commands synchronously */ | |
aa18e0ec | 716 | #ifdef FIO_SGIO_DEBUG |
b4b9665e VF |
717 | assert(st->unmap_range_count == 1); |
718 | assert(io_u == st->trim_io_us[0]); | |
aa18e0ec | 719 | #endif |
b4b9665e VF |
720 | hdr = &io_u->hdr; |
721 | ||
722 | fio_sgio_unmap_setup(hdr, st); | |
723 | ||
724 | st->unmap_range_count = 0; | |
725 | sd->current_queue = -1; | |
726 | } else | |
727 | /* queue up trim ranges and submit in commit() */ | |
728 | return FIO_Q_QUEUED; | |
729 | } | |
730 | ||
f6db4fa5 | 731 | ret = fio_sgio_doio(td, io_u, do_sync); |
2866c82d JA |
732 | |
733 | if (ret < 0) | |
734 | io_u->error = errno; | |
735 | else if (hdr->status) { | |
736 | io_u->resid = hdr->resid; | |
737 | io_u->error = EIO; | |
b4b9665e VF |
738 | } else if (td->io_ops->commit != NULL) { |
739 | if (do_sync && !ddir_sync(io_u->ddir)) { | |
740 | io_u_mark_submit(td, 1); | |
741 | io_u_mark_complete(td, 1); | |
742 | } else if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) { | |
743 | io_u_mark_submit(td, 1); | |
744 | io_u_queued(td, io_u); | |
745 | } | |
2866c82d JA |
746 | } |
747 | ||
95bcd815 | 748 | if (io_u->error) { |
e1161c32 | 749 | td_verror(td, io_u->error, "xfer"); |
36167d82 | 750 | return FIO_Q_COMPLETED; |
95bcd815 JA |
751 | } |
752 | ||
36167d82 | 753 | return ret; |
2866c82d JA |
754 | } |
755 | ||
b4b9665e VF |
756 | static int fio_sgio_commit(struct thread_data *td) |
757 | { | |
758 | struct sgio_data *sd = td->io_ops_data; | |
759 | struct sgio_trim *st; | |
760 | struct io_u *io_u; | |
761 | struct sg_io_hdr *hdr; | |
762 | struct timespec now; | |
763 | unsigned int i; | |
764 | int ret; | |
765 | ||
766 | if (sd->current_queue == -1) | |
767 | return 0; | |
768 | ||
769 | st = sd->trim_queues[sd->current_queue]; | |
770 | io_u = st->trim_io_us[0]; | |
771 | hdr = &io_u->hdr; | |
772 | ||
773 | fio_sgio_unmap_setup(hdr, st); | |
774 | ||
775 | sd->current_queue = -1; | |
776 | ||
a999bc49 | 777 | ret = fio_sgio_rw_doio(td, io_u->file, io_u, 0); |
b4b9665e | 778 | |
53ee8c17 VF |
779 | if (ret < 0 || hdr->status) { |
780 | int error; | |
781 | ||
782 | if (ret < 0) | |
783 | error = errno; | |
784 | else { | |
785 | error = EIO; | |
786 | ret = -EIO; | |
b4b9665e | 787 | } |
53ee8c17 VF |
788 | |
789 | for (i = 0; i < st->unmap_range_count; i++) { | |
790 | st->trim_io_us[i]->error = error; | |
791 | clear_io_u(td, st->trim_io_us[i]); | |
792 | if (hdr->status) | |
793 | st->trim_io_us[i]->resid = hdr->resid; | |
b4b9665e | 794 | } |
53ee8c17 VF |
795 | |
796 | td_verror(td, error, "xfer"); | |
797 | return ret; | |
b4b9665e VF |
798 | } |
799 | ||
53ee8c17 VF |
800 | if (fio_fill_issue_time(td)) { |
801 | fio_gettime(&now, NULL); | |
802 | for (i = 0; i < st->unmap_range_count; i++) { | |
803 | memcpy(&st->trim_io_us[i]->issue_time, &now, sizeof(now)); | |
804 | io_u_queued(td, io_u); | |
805 | } | |
b4b9665e | 806 | } |
53ee8c17 | 807 | io_u_mark_submit(td, st->unmap_range_count); |
b4b9665e | 808 | |
53ee8c17 | 809 | return 0; |
b4b9665e VF |
810 | } |
811 | ||
2866c82d JA |
812 | static struct io_u *fio_sgio_event(struct thread_data *td, int event) |
813 | { | |
565e784d | 814 | struct sgio_data *sd = td->io_ops_data; |
2866c82d JA |
815 | |
816 | return sd->events[event]; | |
817 | } | |
818 | ||
5ad7be56 KD |
819 | static int fio_sgio_read_capacity(struct thread_data *td, unsigned int *bs, |
820 | unsigned long long *max_lba) | |
2866c82d | 821 | { |
5ad7be56 KD |
822 | /* |
823 | * need to do read capacity operation w/o benefit of sd or | |
824 | * io_u structures, which are not initialized until later. | |
825 | */ | |
826 | struct sg_io_hdr hdr; | |
02ae7bd8 DF |
827 | unsigned long long hlba; |
828 | unsigned int blksz = 0; | |
5ad7be56 KD |
829 | unsigned char cmd[16]; |
830 | unsigned char sb[64]; | |
831 | unsigned char buf[32]; // read capacity return | |
2866c82d | 832 | int ret; |
5ad7be56 | 833 | int fd = -1; |
2866c82d | 834 | |
5ad7be56 | 835 | struct fio_file *f = td->files[0]; |
2866c82d | 836 | |
5ad7be56 KD |
837 | /* open file independent of rest of application */ |
838 | fd = open(f->file_name, O_RDONLY); | |
839 | if (fd < 0) | |
840 | return -errno; | |
2866c82d | 841 | |
5ad7be56 KD |
842 | memset(&hdr, 0, sizeof(hdr)); |
843 | memset(cmd, 0, sizeof(cmd)); | |
844 | memset(sb, 0, sizeof(sb)); | |
845 | memset(buf, 0, sizeof(buf)); | |
2866c82d | 846 | |
5ad7be56 KD |
847 | /* First let's try a 10 byte read capacity. */ |
848 | hdr.interface_id = 'S'; | |
849 | hdr.cmdp = cmd; | |
850 | hdr.cmd_len = 10; | |
851 | hdr.sbp = sb; | |
852 | hdr.mx_sb_len = sizeof(sb); | |
853 | hdr.timeout = SCSI_TIMEOUT_MS; | |
854 | hdr.cmdp[0] = 0x25; // Read Capacity(10) | |
855 | hdr.dxfer_direction = SG_DXFER_FROM_DEV; | |
856 | hdr.dxferp = buf; | |
857 | hdr.dxfer_len = sizeof(buf); | |
858 | ||
859 | ret = ioctl(fd, SG_IO, &hdr); | |
860 | if (ret < 0) { | |
861 | close(fd); | |
2866c82d | 862 | return ret; |
5ad7be56 | 863 | } |
2866c82d | 864 | |
02ae7bd8 DF |
865 | if (hdr.info & SG_INFO_CHECK) { |
866 | /* RCAP(10) might be unsupported by device. Force RCAP(16) */ | |
867 | hlba = MAX_10B_LBA; | |
868 | } else { | |
a824149a DF |
869 | blksz = sgio_get_be32(&buf[4]); |
870 | hlba = sgio_get_be32(buf); | |
02ae7bd8 | 871 | } |
5ad7be56 KD |
872 | |
873 | /* | |
fde57152 TK |
874 | * If max lba masked by MAX_10B_LBA equals MAX_10B_LBA, |
875 | * then need to retry with 16 byte Read Capacity command. | |
5ad7be56 | 876 | */ |
02ae7bd8 | 877 | if (hlba == MAX_10B_LBA) { |
5ad7be56 | 878 | hdr.cmd_len = 16; |
28c43a89 TK |
879 | hdr.cmdp[0] = 0x9e; // service action |
880 | hdr.cmdp[1] = 0x10; // Read Capacity(16) | |
a824149a | 881 | sgio_set_be32(sizeof(buf), &hdr.cmdp[10]); |
5ad7be56 KD |
882 | |
883 | hdr.dxfer_direction = SG_DXFER_FROM_DEV; | |
884 | hdr.dxferp = buf; | |
885 | hdr.dxfer_len = sizeof(buf); | |
886 | ||
887 | ret = ioctl(fd, SG_IO, &hdr); | |
888 | if (ret < 0) { | |
889 | close(fd); | |
890 | return ret; | |
891 | } | |
892 | ||
893 | /* record if an io error occurred */ | |
894 | if (hdr.info & SG_INFO_CHECK) | |
895 | td_verror(td, EIO, "fio_sgio_read_capacity"); | |
896 | ||
a824149a DF |
897 | blksz = sgio_get_be32(&buf[8]); |
898 | hlba = sgio_get_be64(buf); | |
02ae7bd8 DF |
899 | } |
900 | ||
901 | if (blksz) { | |
902 | *bs = blksz; | |
903 | *max_lba = hlba; | |
904 | ret = 0; | |
905 | } else { | |
906 | ret = EIO; | |
5ad7be56 KD |
907 | } |
908 | ||
909 | close(fd); | |
02ae7bd8 | 910 | return ret; |
2866c82d JA |
911 | } |
912 | ||
913 | static void fio_sgio_cleanup(struct thread_data *td) | |
914 | { | |
565e784d | 915 | struct sgio_data *sd = td->io_ops_data; |
b4b9665e | 916 | int i; |
dc0deca2 JA |
917 | |
918 | if (sd) { | |
919 | free(sd->events); | |
920 | free(sd->cmds); | |
921 | free(sd->fd_flags); | |
922 | free(sd->pfds); | |
923 | free(sd->sgbuf); | |
aa18e0ec | 924 | #ifdef FIO_SGIO_DEBUG |
b4b9665e | 925 | free(sd->trim_queue_map); |
aa18e0ec | 926 | #endif |
b4b9665e VF |
927 | |
928 | for (i = 0; i < td->o.iodepth; i++) { | |
929 | free(sd->trim_queues[i]->unmap_param); | |
930 | free(sd->trim_queues[i]->trim_io_us); | |
931 | free(sd->trim_queues[i]); | |
932 | } | |
933 | ||
934 | free(sd->trim_queues); | |
dc0deca2 | 935 | free(sd); |
2866c82d JA |
936 | } |
937 | } | |
938 | ||
939 | static int fio_sgio_init(struct thread_data *td) | |
940 | { | |
941 | struct sgio_data *sd; | |
b4b9665e | 942 | struct sgio_trim *st; |
b0dc148e | 943 | struct sg_io_hdr *h3p; |
b4b9665e | 944 | int i; |
2866c82d | 945 | |
b4b9665e VF |
946 | sd = calloc(1, sizeof(*sd)); |
947 | sd->cmds = calloc(td->o.iodepth, sizeof(struct sgio_cmd)); | |
948 | sd->sgbuf = calloc(td->o.iodepth, sizeof(struct sg_io_hdr)); | |
949 | sd->events = calloc(td->o.iodepth, sizeof(struct io_u *)); | |
950 | sd->pfds = calloc(td->o.nr_files, sizeof(struct pollfd)); | |
951 | sd->fd_flags = calloc(td->o.nr_files, sizeof(int)); | |
5ad7be56 | 952 | sd->type_checked = 0; |
b4b9665e VF |
953 | |
954 | sd->trim_queues = calloc(td->o.iodepth, sizeof(struct sgio_trim *)); | |
955 | sd->current_queue = -1; | |
aa18e0ec | 956 | #ifdef FIO_SGIO_DEBUG |
b4b9665e | 957 | sd->trim_queue_map = calloc(td->o.iodepth, sizeof(int)); |
aa18e0ec | 958 | #endif |
b0dc148e | 959 | for (i = 0, h3p = sd->sgbuf; i < td->o.iodepth; i++, ++h3p) { |
b4b9665e VF |
960 | sd->trim_queues[i] = calloc(1, sizeof(struct sgio_trim)); |
961 | st = sd->trim_queues[i]; | |
962 | st->unmap_param = calloc(td->o.iodepth + 1, sizeof(char[16])); | |
963 | st->unmap_range_count = 0; | |
964 | st->trim_io_us = calloc(td->o.iodepth, sizeof(struct io_u *)); | |
b0dc148e | 965 | h3p->interface_id = 'S'; |
b4b9665e VF |
966 | } |
967 | ||
565e784d | 968 | td->io_ops_data = sd; |
2866c82d | 969 | |
b5af8293 JA |
970 | /* |
971 | * we want to do it, regardless of whether odirect is set or not | |
972 | */ | |
2dc1bbeb | 973 | td->o.override_sync = 1; |
b5af8293 JA |
974 | return 0; |
975 | } | |
976 | ||
977 | static int fio_sgio_type_check(struct thread_data *td, struct fio_file *f) | |
978 | { | |
565e784d | 979 | struct sgio_data *sd = td->io_ops_data; |
5ad7be56 KD |
980 | unsigned int bs = 0; |
981 | unsigned long long max_lba = 0; | |
982 | ||
686fbd31 | 983 | if (f->filetype == FIO_TYPE_BLOCK) { |
53cdc686 | 984 | if (ioctl(f->fd, BLKSSZGET, &bs) < 0) { |
e1161c32 | 985 | td_verror(td, errno, "ioctl"); |
b5af8293 | 986 | return 1; |
2866c82d | 987 | } |
af52b345 | 988 | } else if (f->filetype == FIO_TYPE_CHAR) { |
b5af8293 | 989 | int version, ret; |
2866c82d | 990 | |
53cdc686 | 991 | if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) { |
e1161c32 | 992 | td_verror(td, errno, "ioctl"); |
b5af8293 | 993 | return 1; |
2866c82d JA |
994 | } |
995 | ||
5ad7be56 KD |
996 | ret = fio_sgio_read_capacity(td, &bs, &max_lba); |
997 | if (ret) { | |
998 | td_verror(td, td->error, "fio_sgio_read_capacity"); | |
999 | log_err("ioengine sg unable to read capacity successfully\n"); | |
b5af8293 | 1000 | return 1; |
5ad7be56 | 1001 | } |
2866c82d | 1002 | } else { |
16ada754 | 1003 | td_verror(td, EINVAL, "wrong file type"); |
30dac136 | 1004 | log_err("ioengine sg only works on block or character devices\n"); |
b5af8293 | 1005 | return 1; |
2866c82d JA |
1006 | } |
1007 | ||
1008 | sd->bs = bs; | |
5ad7be56 | 1009 | // Determine size of commands needed based on max_lba |
166c6b42 TK |
1010 | if (max_lba >= MAX_10B_LBA) { |
1011 | dprint(FD_IO, "sgio_type_check: using 16 byte read/write " | |
1012 | "commands for lba above 0x%016llx/0x%016llx\n", | |
1013 | MAX_10B_LBA, max_lba); | |
5ad7be56 KD |
1014 | } |
1015 | ||
686fbd31 | 1016 | if (f->filetype == FIO_TYPE_BLOCK) { |
36167d82 JA |
1017 | td->io_ops->getevents = NULL; |
1018 | td->io_ops->event = NULL; | |
b4b9665e VF |
1019 | td->io_ops->commit = NULL; |
1020 | /* | |
1021 | ** Setting these functions to null may cause problems | |
1022 | ** with filename=/dev/sda:/dev/sg0 since we are only | |
1023 | ** considering a single file | |
1024 | */ | |
36167d82 | 1025 | } |
5ad7be56 | 1026 | sd->type_checked = 1; |
2866c82d | 1027 | |
2866c82d | 1028 | return 0; |
b5af8293 JA |
1029 | } |
1030 | ||
1031 | static int fio_sgio_open(struct thread_data *td, struct fio_file *f) | |
1032 | { | |
565e784d | 1033 | struct sgio_data *sd = td->io_ops_data; |
b5af8293 JA |
1034 | int ret; |
1035 | ||
1036 | ret = generic_open_file(td, f); | |
1037 | if (ret) | |
1038 | return ret; | |
1039 | ||
15ba640a | 1040 | if (sd && !sd->type_checked && fio_sgio_type_check(td, f)) { |
6977bcd0 | 1041 | ret = generic_close_file(td, f); |
67486df3 | 1042 | return ret; |
b5af8293 JA |
1043 | } |
1044 | ||
1045 | return 0; | |
2866c82d JA |
1046 | } |
1047 | ||
5ad7be56 KD |
1048 | /* |
1049 | * Build an error string with details about the driver, host or scsi | |
1050 | * error contained in the sg header Caller will use as necessary. | |
1051 | */ | |
1052 | static char *fio_sgio_errdetails(struct io_u *io_u) | |
1053 | { | |
1054 | struct sg_io_hdr *hdr = &io_u->hdr; | |
1055 | #define MAXERRDETAIL 1024 | |
1056 | #define MAXMSGCHUNK 128 | |
fd04fa03 | 1057 | char *msg, msgchunk[MAXMSGCHUNK]; |
5ad7be56 KD |
1058 | int i; |
1059 | ||
efa72f25 | 1060 | msg = calloc(1, MAXERRDETAIL); |
fd04fa03 | 1061 | strcpy(msg, ""); |
5ad7be56 KD |
1062 | |
1063 | /* | |
1064 | * can't seem to find sg_err.h, so I'll just echo the define values | |
1065 | * so others can search on internet to find clearer clues of meaning. | |
1066 | */ | |
1067 | if (hdr->info & SG_INFO_CHECK) { | |
5ad7be56 KD |
1068 | if (hdr->host_status) { |
1069 | snprintf(msgchunk, MAXMSGCHUNK, "SG Host Status: 0x%02x; ", hdr->host_status); | |
1070 | strlcat(msg, msgchunk, MAXERRDETAIL); | |
1071 | switch (hdr->host_status) { | |
1072 | case 0x01: | |
1073 | strlcat(msg, "SG_ERR_DID_NO_CONNECT", MAXERRDETAIL); | |
1074 | break; | |
1075 | case 0x02: | |
1076 | strlcat(msg, "SG_ERR_DID_BUS_BUSY", MAXERRDETAIL); | |
1077 | break; | |
1078 | case 0x03: | |
1079 | strlcat(msg, "SG_ERR_DID_TIME_OUT", MAXERRDETAIL); | |
1080 | break; | |
1081 | case 0x04: | |
1082 | strlcat(msg, "SG_ERR_DID_BAD_TARGET", MAXERRDETAIL); | |
1083 | break; | |
1084 | case 0x05: | |
1085 | strlcat(msg, "SG_ERR_DID_ABORT", MAXERRDETAIL); | |
1086 | break; | |
1087 | case 0x06: | |
1088 | strlcat(msg, "SG_ERR_DID_PARITY", MAXERRDETAIL); | |
1089 | break; | |
1090 | case 0x07: | |
1091 | strlcat(msg, "SG_ERR_DID_ERROR (internal error)", MAXERRDETAIL); | |
1092 | break; | |
1093 | case 0x08: | |
1094 | strlcat(msg, "SG_ERR_DID_RESET", MAXERRDETAIL); | |
1095 | break; | |
1096 | case 0x09: | |
1097 | strlcat(msg, "SG_ERR_DID_BAD_INTR (unexpected)", MAXERRDETAIL); | |
1098 | break; | |
1099 | case 0x0a: | |
1100 | strlcat(msg, "SG_ERR_DID_PASSTHROUGH", MAXERRDETAIL); | |
1101 | break; | |
1102 | case 0x0b: | |
1103 | strlcat(msg, "SG_ERR_DID_SOFT_ERROR (driver retry?)", MAXERRDETAIL); | |
1104 | break; | |
1105 | case 0x0c: | |
1106 | strlcat(msg, "SG_ERR_DID_IMM_RETRY", MAXERRDETAIL); | |
1107 | break; | |
1108 | case 0x0d: | |
1109 | strlcat(msg, "SG_ERR_DID_REQUEUE", MAXERRDETAIL); | |
1110 | break; | |
2ce6c6e5 TK |
1111 | case 0x0e: |
1112 | strlcat(msg, "SG_ERR_DID_TRANSPORT_DISRUPTED", MAXERRDETAIL); | |
1113 | break; | |
1114 | case 0x0f: | |
1115 | strlcat(msg, "SG_ERR_DID_TRANSPORT_FAILFAST", MAXERRDETAIL); | |
1116 | break; | |
1117 | case 0x10: | |
1118 | strlcat(msg, "SG_ERR_DID_TARGET_FAILURE", MAXERRDETAIL); | |
1119 | break; | |
1120 | case 0x11: | |
1121 | strlcat(msg, "SG_ERR_DID_NEXUS_FAILURE", MAXERRDETAIL); | |
1122 | break; | |
1123 | case 0x12: | |
1124 | strlcat(msg, "SG_ERR_DID_ALLOC_FAILURE", MAXERRDETAIL); | |
1125 | break; | |
1126 | case 0x13: | |
1127 | strlcat(msg, "SG_ERR_DID_MEDIUM_ERROR", MAXERRDETAIL); | |
1128 | break; | |
5ad7be56 KD |
1129 | default: |
1130 | strlcat(msg, "Unknown", MAXERRDETAIL); | |
1131 | break; | |
1132 | } | |
1133 | strlcat(msg, ". ", MAXERRDETAIL); | |
1134 | } | |
1135 | if (hdr->driver_status) { | |
1136 | snprintf(msgchunk, MAXMSGCHUNK, "SG Driver Status: 0x%02x; ", hdr->driver_status); | |
1137 | strlcat(msg, msgchunk, MAXERRDETAIL); | |
1138 | switch (hdr->driver_status & 0x0F) { | |
1139 | case 0x01: | |
1140 | strlcat(msg, "SG_ERR_DRIVER_BUSY", MAXERRDETAIL); | |
1141 | break; | |
1142 | case 0x02: | |
1143 | strlcat(msg, "SG_ERR_DRIVER_SOFT", MAXERRDETAIL); | |
1144 | break; | |
1145 | case 0x03: | |
1146 | strlcat(msg, "SG_ERR_DRIVER_MEDIA", MAXERRDETAIL); | |
1147 | break; | |
1148 | case 0x04: | |
1149 | strlcat(msg, "SG_ERR_DRIVER_ERROR", MAXERRDETAIL); | |
1150 | break; | |
1151 | case 0x05: | |
1152 | strlcat(msg, "SG_ERR_DRIVER_INVALID", MAXERRDETAIL); | |
1153 | break; | |
1154 | case 0x06: | |
1155 | strlcat(msg, "SG_ERR_DRIVER_TIMEOUT", MAXERRDETAIL); | |
1156 | break; | |
1157 | case 0x07: | |
1158 | strlcat(msg, "SG_ERR_DRIVER_HARD", MAXERRDETAIL); | |
1159 | break; | |
1160 | case 0x08: | |
1161 | strlcat(msg, "SG_ERR_DRIVER_SENSE", MAXERRDETAIL); | |
1162 | break; | |
1163 | default: | |
1164 | strlcat(msg, "Unknown", MAXERRDETAIL); | |
1165 | break; | |
1166 | } | |
1167 | strlcat(msg, "; ", MAXERRDETAIL); | |
1168 | switch (hdr->driver_status & 0xF0) { | |
1169 | case 0x10: | |
1170 | strlcat(msg, "SG_ERR_SUGGEST_RETRY", MAXERRDETAIL); | |
1171 | break; | |
1172 | case 0x20: | |
1173 | strlcat(msg, "SG_ERR_SUGGEST_ABORT", MAXERRDETAIL); | |
1174 | break; | |
1175 | case 0x30: | |
1176 | strlcat(msg, "SG_ERR_SUGGEST_REMAP", MAXERRDETAIL); | |
1177 | break; | |
1178 | case 0x40: | |
1179 | strlcat(msg, "SG_ERR_SUGGEST_DIE", MAXERRDETAIL); | |
1180 | break; | |
1181 | case 0x80: | |
1182 | strlcat(msg, "SG_ERR_SUGGEST_SENSE", MAXERRDETAIL); | |
1183 | break; | |
1184 | } | |
1185 | strlcat(msg, ". ", MAXERRDETAIL); | |
1186 | } | |
1187 | if (hdr->status) { | |
1188 | snprintf(msgchunk, MAXMSGCHUNK, "SG SCSI Status: 0x%02x; ", hdr->status); | |
1189 | strlcat(msg, msgchunk, MAXERRDETAIL); | |
1190 | // SCSI 3 status codes | |
1191 | switch (hdr->status) { | |
1192 | case 0x02: | |
1193 | strlcat(msg, "CHECK_CONDITION", MAXERRDETAIL); | |
1194 | break; | |
1195 | case 0x04: | |
1196 | strlcat(msg, "CONDITION_MET", MAXERRDETAIL); | |
1197 | break; | |
1198 | case 0x08: | |
1199 | strlcat(msg, "BUSY", MAXERRDETAIL); | |
1200 | break; | |
1201 | case 0x10: | |
1202 | strlcat(msg, "INTERMEDIATE", MAXERRDETAIL); | |
1203 | break; | |
1204 | case 0x14: | |
1205 | strlcat(msg, "INTERMEDIATE_CONDITION_MET", MAXERRDETAIL); | |
1206 | break; | |
1207 | case 0x18: | |
1208 | strlcat(msg, "RESERVATION_CONFLICT", MAXERRDETAIL); | |
1209 | break; | |
1210 | case 0x22: | |
1211 | strlcat(msg, "COMMAND_TERMINATED", MAXERRDETAIL); | |
1212 | break; | |
1213 | case 0x28: | |
1214 | strlcat(msg, "TASK_SET_FULL", MAXERRDETAIL); | |
1215 | break; | |
1216 | case 0x30: | |
1217 | strlcat(msg, "ACA_ACTIVE", MAXERRDETAIL); | |
1218 | break; | |
1219 | case 0x40: | |
1220 | strlcat(msg, "TASK_ABORTED", MAXERRDETAIL); | |
1221 | break; | |
1222 | default: | |
1223 | strlcat(msg, "Unknown", MAXERRDETAIL); | |
1224 | break; | |
1225 | } | |
1226 | strlcat(msg, ". ", MAXERRDETAIL); | |
1227 | } | |
1228 | if (hdr->sb_len_wr) { | |
1229 | snprintf(msgchunk, MAXMSGCHUNK, "Sense Data (%d bytes):", hdr->sb_len_wr); | |
1230 | strlcat(msg, msgchunk, MAXERRDETAIL); | |
1231 | for (i = 0; i < hdr->sb_len_wr; i++) { | |
1232 | snprintf(msgchunk, MAXMSGCHUNK, " %02x", hdr->sbp[i]); | |
1233 | strlcat(msg, msgchunk, MAXERRDETAIL); | |
1234 | } | |
1235 | strlcat(msg, ". ", MAXERRDETAIL); | |
1236 | } | |
1237 | if (hdr->resid != 0) { | |
1238 | snprintf(msgchunk, MAXMSGCHUNK, "SG Driver: %d bytes out of %d not transferred. ", hdr->resid, hdr->dxfer_len); | |
1239 | strlcat(msg, msgchunk, MAXERRDETAIL); | |
5ad7be56 | 1240 | } |
b4dbb3ce VF |
1241 | if (hdr->cmdp) { |
1242 | strlcat(msg, "cdb:", MAXERRDETAIL); | |
1243 | for (i = 0; i < hdr->cmd_len; i++) { | |
1244 | snprintf(msgchunk, MAXMSGCHUNK, " %02x", hdr->cmdp[i]); | |
1245 | strlcat(msg, msgchunk, MAXERRDETAIL); | |
1246 | } | |
1247 | strlcat(msg, ". ", MAXERRDETAIL); | |
1248 | if (io_u->ddir == DDIR_TRIM) { | |
1249 | unsigned char *param_list = hdr->dxferp; | |
1250 | strlcat(msg, "dxferp:", MAXERRDETAIL); | |
1251 | for (i = 0; i < hdr->dxfer_len; i++) { | |
1252 | snprintf(msgchunk, MAXMSGCHUNK, " %02x", param_list[i]); | |
1253 | strlcat(msg, msgchunk, MAXERRDETAIL); | |
1254 | } | |
1255 | strlcat(msg, ". ", MAXERRDETAIL); | |
1256 | } | |
1257 | } | |
5ad7be56 KD |
1258 | } |
1259 | ||
fd04fa03 | 1260 | if (!(hdr->info & SG_INFO_CHECK) && !strlen(msg)) |
36833fb0 BVA |
1261 | snprintf(msg, MAXERRDETAIL, "%s", |
1262 | "SG Driver did not report a Host, Driver or Device check"); | |
5ad7be56 | 1263 | |
fd04fa03 | 1264 | return msg; |
5ad7be56 KD |
1265 | } |
1266 | ||
1267 | /* | |
1268 | * get max file size from read capacity. | |
1269 | */ | |
1270 | static int fio_sgio_get_file_size(struct thread_data *td, struct fio_file *f) | |
1271 | { | |
1272 | /* | |
1273 | * get_file_size is being called even before sgio_init is | |
1274 | * called, so none of the sg_io structures are | |
1275 | * initialized in the thread_data yet. So we need to do the | |
1276 | * ReadCapacity without any of those helpers. One of the effects | |
1277 | * is that ReadCapacity may get called 4 times on each open: | |
1278 | * readcap(10) followed by readcap(16) if needed - just to get | |
1279 | * the file size after the init occurs - it will be called | |
1280 | * again when "type_check" is called during structure | |
1281 | * initialization I'm not sure how to prevent this little | |
1282 | * inefficiency. | |
1283 | */ | |
1284 | unsigned int bs = 0; | |
1285 | unsigned long long max_lba = 0; | |
1286 | int ret; | |
1287 | ||
1288 | if (fio_file_size_known(f)) | |
1289 | return 0; | |
1290 | ||
686fbd31 | 1291 | if (f->filetype != FIO_TYPE_BLOCK && f->filetype != FIO_TYPE_CHAR) { |
30dac136 TK |
1292 | td_verror(td, EINVAL, "wrong file type"); |
1293 | log_err("ioengine sg only works on block or character devices\n"); | |
1294 | return 1; | |
1295 | } | |
1296 | ||
5ad7be56 KD |
1297 | ret = fio_sgio_read_capacity(td, &bs, &max_lba); |
1298 | if (ret ) { | |
1299 | td_verror(td, td->error, "fio_sgio_read_capacity"); | |
1300 | log_err("ioengine sg unable to successfully execute read capacity to get block size and maximum lba\n"); | |
1301 | return 1; | |
1302 | } | |
1303 | ||
1304 | f->real_file_size = (max_lba + 1) * bs; | |
1305 | fio_file_set_size_known(f); | |
1306 | return 0; | |
1307 | } | |
1308 | ||
1309 | ||
5f350952 | 1310 | static struct ioengine_ops ioengine = { |
2866c82d JA |
1311 | .name = "sg", |
1312 | .version = FIO_IOOPS_VERSION, | |
1313 | .init = fio_sgio_init, | |
1314 | .prep = fio_sgio_prep, | |
1315 | .queue = fio_sgio_queue, | |
b4b9665e | 1316 | .commit = fio_sgio_commit, |
2866c82d | 1317 | .getevents = fio_sgio_getevents, |
5ad7be56 | 1318 | .errdetails = fio_sgio_errdetails, |
2866c82d JA |
1319 | .event = fio_sgio_event, |
1320 | .cleanup = fio_sgio_cleanup, | |
b5af8293 JA |
1321 | .open_file = fio_sgio_open, |
1322 | .close_file = generic_close_file, | |
fde57152 | 1323 | .get_file_size = fio_sgio_get_file_size, |
b2a15192 | 1324 | .flags = FIO_SYNCIO | FIO_RAWIO, |
52b81b7c KD |
1325 | .options = options, |
1326 | .option_struct_size = sizeof(struct sg_options) | |
2866c82d | 1327 | }; |
34cfcdaf JA |
1328 | |
1329 | #else /* FIO_HAVE_SGIO */ | |
1330 | ||
1331 | /* | |
1332 | * When we have a proper configure system in place, we simply wont build | |
1333 | * and install this io engine. For now install a crippled version that | |
1334 | * just complains and fails to load. | |
1335 | */ | |
1336 | static int fio_sgio_init(struct thread_data fio_unused *td) | |
1337 | { | |
a3edaf76 | 1338 | log_err("fio: ioengine sg not available\n"); |
34cfcdaf JA |
1339 | return 1; |
1340 | } | |
1341 | ||
5f350952 | 1342 | static struct ioengine_ops ioengine = { |
d0c70934 | 1343 | .name = "sg", |
34cfcdaf JA |
1344 | .version = FIO_IOOPS_VERSION, |
1345 | .init = fio_sgio_init, | |
1346 | }; | |
1347 | ||
1348 | #endif | |
5f350952 JA |
1349 | |
1350 | static void fio_init fio_sgio_register(void) | |
1351 | { | |
1352 | register_ioengine(&ioengine); | |
1353 | } | |
1354 | ||
1355 | static void fio_exit fio_sgio_unregister(void) | |
1356 | { | |
1357 | unregister_ioengine(&ioengine); | |
1358 | } |