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