4 * IO engine that uses the Linux SG v3 interface to talk to SCSI devices
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
11 * What value does queue() return for the different cases?
12 * queue() return value
14 * /dev/sdX RWT FIO_Q_COMPLETED
15 * /dev/sgY RWT FIO_Q_COMPLETED
16 * with direct=1 or sync=1
19 * /dev/sgY RWT FIO_Q_QUEUED
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()
25 * Where are the IO counting functions called for the different cases?
28 * /dev/sdX (commit==NULL)
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()
34 * /dev/sgY with direct=1 or sync=1 (commit does nothing)
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()
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()
59 #include "../optgroup.h"
63 #ifndef SGV4_FLAG_HIPRI
64 #define SGV4_FLAG_HIPRI 0x800
71 FIO_SG_WRITE_SAME_NDOB,
73 FIO_SG_VERIFY_BYTCHK_00,
74 FIO_SG_VERIFY_BYTCHK_01,
75 FIO_SG_VERIFY_BYTCHK_11,
82 unsigned int writefua;
83 unsigned int write_mode;
87 static struct fio_option options[] = {
90 .lname = "High Priority",
91 .type = FIO_OPT_STR_SET,
92 .off1 = offsetof(struct sg_options, hipri),
93 .help = "Use polled IO completions",
94 .category = FIO_OPT_C_ENGINE,
95 .group = FIO_OPT_G_SG,
99 .lname = "sg engine read fua flag support",
100 .type = FIO_OPT_BOOL,
101 .off1 = offsetof(struct sg_options, readfua),
102 .help = "Set FUA flag (force unit access) for all Read operations",
104 .category = FIO_OPT_C_ENGINE,
105 .group = FIO_OPT_G_SG,
109 .lname = "sg engine write fua flag support",
110 .type = FIO_OPT_BOOL,
111 .off1 = offsetof(struct sg_options, writefua),
112 .help = "Set FUA flag (force unit access) for all Write operations",
114 .category = FIO_OPT_C_ENGINE,
115 .group = FIO_OPT_G_SG,
118 .name = "sg_write_mode",
119 .lname = "specify sg write mode",
121 .off1 = offsetof(struct sg_options, write_mode),
122 .help = "Specify SCSI WRITE mode",
126 .oval = FIO_SG_WRITE,
127 .help = "Issue standard SCSI WRITE commands",
129 { .ival = "write_and_verify",
130 .oval = FIO_SG_WRITE_VERIFY,
131 .help = "Issue SCSI WRITE AND VERIFY commands",
134 .oval = FIO_SG_WRITE_VERIFY,
135 .help = "Issue SCSI WRITE AND VERIFY commands. This "
136 "option is deprecated. Use write_and_verify instead.",
138 { .ival = "write_same",
139 .oval = FIO_SG_WRITE_SAME,
140 .help = "Issue SCSI WRITE SAME commands",
143 .oval = FIO_SG_WRITE_SAME,
144 .help = "Issue SCSI WRITE SAME commands. This "
145 "option is deprecated. Use write_same instead.",
147 { .ival = "write_same_ndob",
148 .oval = FIO_SG_WRITE_SAME_NDOB,
149 .help = "Issue SCSI WRITE SAME(16) commands with NDOB flag set",
151 { .ival = "verify_bytchk_00",
152 .oval = FIO_SG_VERIFY_BYTCHK_00,
153 .help = "Issue SCSI VERIFY commands with BYTCHK set to 00",
155 { .ival = "verify_bytchk_01",
156 .oval = FIO_SG_VERIFY_BYTCHK_01,
157 .help = "Issue SCSI VERIFY commands with BYTCHK set to 01",
159 { .ival = "verify_bytchk_11",
160 .oval = FIO_SG_VERIFY_BYTCHK_11,
161 .help = "Issue SCSI VERIFY commands with BYTCHK set to 11",
163 { .ival = "write_stream",
164 .oval = FIO_SG_WRITE_STREAM,
165 .help = "Issue SCSI WRITE STREAM(16) commands",
168 .category = FIO_OPT_C_ENGINE,
169 .group = FIO_OPT_G_SG,
173 .lname = "stream id for WRITE STREAM(16) commands",
175 .off1 = offsetof(struct sg_options, stream_id),
176 .help = "Stream ID for WRITE STREAM(16) commands",
178 .category = FIO_OPT_C_ENGINE,
179 .group = FIO_OPT_G_SG,
186 #define MAX_10B_LBA 0xFFFFFFFFULL
187 #define SCSI_TIMEOUT_MS 30000 // 30 second timeout; currently no method to override
188 #define MAX_SB 64 // sense block maximum return size
190 #define FIO_SGIO_DEBUG
194 unsigned char cdb[16]; // enhanced from 10 to support 16 byte commands
195 unsigned char sb[MAX_SB]; // add sense block to commands
200 uint8_t *unmap_param;
201 unsigned int unmap_range_count;
202 struct io_u **trim_io_us;
206 struct sgio_cmd *cmds;
207 struct io_u **events;
213 struct sgio_trim **trim_queues;
215 #ifdef FIO_SGIO_DEBUG
216 unsigned int *trim_queue_map;
220 static inline uint16_t sgio_get_be16(uint8_t *buf)
222 return be16_to_cpu(*((uint16_t *) buf));
225 static inline uint32_t sgio_get_be32(uint8_t *buf)
227 return be32_to_cpu(*((uint32_t *) buf));
230 static inline uint64_t sgio_get_be64(uint8_t *buf)
232 return be64_to_cpu(*((uint64_t *) buf));
235 static inline void sgio_set_be16(uint16_t val, uint8_t *buf)
237 uint16_t t = cpu_to_be16(val);
239 memcpy(buf, &t, sizeof(uint16_t));
242 static inline void sgio_set_be32(uint32_t val, uint8_t *buf)
244 uint32_t t = cpu_to_be32(val);
246 memcpy(buf, &t, sizeof(uint32_t));
249 static inline void sgio_set_be64(uint64_t val, uint8_t *buf)
251 uint64_t t = cpu_to_be64(val);
253 memcpy(buf, &t, sizeof(uint64_t));
256 static inline bool sgio_unbuffered(struct thread_data *td)
258 return (td->o.odirect || td->o.sync_io);
261 static void sgio_hdr_init(struct sgio_data *sd, struct sg_io_hdr *hdr,
262 struct io_u *io_u, int fs)
264 struct sgio_cmd *sc = &sd->cmds[io_u->index];
266 memset(hdr, 0, sizeof(*hdr));
267 memset(sc->cdb, 0, sizeof(sc->cdb));
269 hdr->interface_id = 'S';
271 hdr->cmd_len = sizeof(sc->cdb);
273 hdr->mx_sb_len = sizeof(sc->sb);
274 hdr->pack_id = io_u->index;
276 hdr->timeout = SCSI_TIMEOUT_MS;
279 hdr->dxferp = io_u->xfer_buf;
280 hdr->dxfer_len = io_u->xfer_buflen;
284 static int pollin_events(struct pollfd *pfds, int fds)
288 for (i = 0; i < fds; i++)
289 if (pfds[i].revents & POLLIN)
295 static int sg_fd_read(int fd, void *data, size_t size)
302 ret = read(fd, data, size);
304 if (errno == EAGAIN || errno == EINTR)
324 static int fio_sgio_getevents(struct thread_data *td, unsigned int min,
326 const struct timespec fio_unused *t)
328 struct sgio_data *sd = td->io_ops_data;
329 int left = max, eventNum, ret, r = 0, trims = 0;
330 void *buf = sd->sgbuf;
331 unsigned int i, j, events;
336 * Fill in the file descriptors
338 for_each_file(td, f, i) {
340 * don't block for min events == 0
343 sd->fd_flags[i] = fio_set_fd_nonblocking(f->fd, "sg");
345 sd->fd_flags[i] = -1;
347 sd->pfds[i].fd = f->fd;
348 sd->pfds[i].events = POLLIN;
352 ** There are two counters here:
353 ** - number of SCSI commands completed
354 ** - number of io_us completed
356 ** These are the same with reads and writes, but
357 ** could differ with trim/unmap commands because
358 ** a single unmap can include multiple io_us
364 dprint(FD_IO, "sgio_getevents: sd %p: min=%d, max=%d, left=%d\n", sd, min, max, left);
370 ret = poll(sd->pfds, td->o.nr_files, -1);
374 td_verror(td, errno, "poll");
379 if (pollin_events(sd->pfds, td->o.nr_files))
389 for_each_file(td, f, i) {
390 for (eventNum = 0; eventNum < left; eventNum++) {
391 ret = sg_fd_read(f->fd, p, sizeof(struct sg_io_hdr));
392 dprint(FD_IO, "sgio_getevents: sg_fd_read ret: %d\n", ret);
395 td_verror(td, r, "sg_read");
398 io_u = ((struct sg_io_hdr *)p)->usr_ptr;
399 if (io_u->ddir == DDIR_TRIM) {
400 events += sd->trim_queues[io_u->index]->unmap_range_count;
401 eventNum += sd->trim_queues[io_u->index]->unmap_range_count - 1;
405 p += sizeof(struct sg_io_hdr);
406 dprint(FD_IO, "sgio_getevents: events: %d, eventNum: %d, left: %d\n", events, eventNum, left);
410 if (r < 0 && !events)
420 for (i = 0; i < events; i++) {
421 struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i;
422 sd->events[i + trims] = hdr->usr_ptr;
423 io_u = (struct io_u *)(hdr->usr_ptr);
425 if (hdr->info & SG_INFO_CHECK) {
426 /* record if an io error occurred, ignore resid */
427 memcpy(&io_u->hdr, hdr, sizeof(struct sg_io_hdr));
428 sd->events[i + trims]->error = EIO;
431 if (io_u->ddir == DDIR_TRIM) {
432 struct sgio_trim *st = sd->trim_queues[io_u->index];
433 #ifdef FIO_SGIO_DEBUG
434 assert(st->trim_io_us[0] == io_u);
435 assert(sd->trim_queue_map[io_u->index] == io_u->index);
436 dprint(FD_IO, "sgio_getevents: reaping %d io_us from trim queue %d\n", st->unmap_range_count, io_u->index);
437 dprint(FD_IO, "sgio_getevents: reaped io_u %d and stored in events[%d]\n", io_u->index, i+trims);
439 for (j = 1; j < st->unmap_range_count; j++) {
441 sd->events[i + trims] = st->trim_io_us[j];
442 #ifdef FIO_SGIO_DEBUG
443 dprint(FD_IO, "sgio_getevents: reaped io_u %d and stored in events[%d]\n", st->trim_io_us[j]->index, i+trims);
444 assert(sd->trim_queue_map[st->trim_io_us[j]->index] == io_u->index);
446 if (hdr->info & SG_INFO_CHECK) {
447 /* record if an io error occurred, ignore resid */
448 memcpy(&st->trim_io_us[j]->hdr, hdr, sizeof(struct sg_io_hdr));
449 sd->events[i + trims]->error = EIO;
452 events -= st->unmap_range_count - 1;
453 st->unmap_range_count = 0;
459 for_each_file(td, f, i) {
460 if (sd->fd_flags[i] == -1)
463 if (fcntl(f->fd, F_SETFL, sd->fd_flags[i]) < 0)
464 log_err("fio: sg failed to restore fcntl flags: %s\n", strerror(errno));
471 static enum fio_q_status fio_sgio_ioctl_doio(struct thread_data *td,
475 struct sgio_data *sd = td->io_ops_data;
476 struct sg_io_hdr *hdr = &io_u->hdr;
479 sd->events[0] = io_u;
481 ret = ioctl(f->fd, SG_IO, hdr);
485 /* record if an io error occurred */
486 if (hdr->info & SG_INFO_CHECK)
489 return FIO_Q_COMPLETED;
492 static enum fio_q_status fio_sgio_rw_doio(struct thread_data *td,
494 struct io_u *io_u, int do_sync)
496 struct sg_io_hdr *hdr = &io_u->hdr;
499 ret = write(f->fd, hdr, sizeof(*hdr));
505 * We can't just read back the first command that completes
506 * and assume it's the one we need, it could be any command
512 ret = read(f->fd, hdr, sizeof(*hdr));
516 __io_u = hdr->usr_ptr;
518 /* record if an io error occurred */
519 if (hdr->info & SG_INFO_CHECK)
525 if (io_u_sync_complete(td, __io_u))
530 return FIO_Q_COMPLETED;
536 static enum fio_q_status fio_sgio_doio(struct thread_data *td,
537 struct io_u *io_u, int do_sync)
539 struct fio_file *f = io_u->file;
540 enum fio_q_status ret;
542 if (f->filetype == FIO_TYPE_BLOCK) {
543 ret = fio_sgio_ioctl_doio(td, f, io_u);
545 td_verror(td, io_u->error, __func__);
547 ret = fio_sgio_rw_doio(td, f, io_u, do_sync);
548 if (io_u->error && do_sync)
549 td_verror(td, io_u->error, __func__);
555 static void fio_sgio_rw_lba(struct sg_io_hdr *hdr, unsigned long long lba,
556 unsigned long long nr_blocks, bool override16)
558 if (lba < MAX_10B_LBA && !override16) {
559 sgio_set_be32((uint32_t) lba, &hdr->cmdp[2]);
560 sgio_set_be16((uint16_t) nr_blocks, &hdr->cmdp[7]);
562 sgio_set_be64(lba, &hdr->cmdp[2]);
563 sgio_set_be32((uint32_t) nr_blocks, &hdr->cmdp[10]);
569 static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u)
571 struct sg_io_hdr *hdr = &io_u->hdr;
572 struct sg_options *o = td->eo;
573 struct sgio_data *sd = td->io_ops_data;
574 unsigned long long nr_blocks, lba;
577 if (io_u->xfer_buflen & (sd->bs - 1)) {
578 log_err("read/write not sector aligned\n");
582 nr_blocks = io_u->xfer_buflen / sd->bs;
583 lba = io_u->offset / sd->bs;
585 if (io_u->ddir == DDIR_READ) {
586 sgio_hdr_init(sd, hdr, io_u, 1);
588 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
589 if (lba < MAX_10B_LBA)
590 hdr->cmdp[0] = 0x28; // read(10)
592 hdr->cmdp[0] = 0x88; // read(16)
595 hdr->flags |= SGV4_FLAG_HIPRI;
597 hdr->cmdp[1] |= 0x08;
599 fio_sgio_rw_lba(hdr, lba, nr_blocks, false);
601 } else if (io_u->ddir == DDIR_WRITE) {
602 sgio_hdr_init(sd, hdr, io_u, 1);
604 hdr->dxfer_direction = SG_DXFER_TO_DEV;
605 switch(o->write_mode) {
607 if (lba < MAX_10B_LBA)
608 hdr->cmdp[0] = 0x2a; // write(10)
610 hdr->cmdp[0] = 0x8a; // write(16)
612 hdr->flags |= SGV4_FLAG_HIPRI;
614 hdr->cmdp[1] |= 0x08;
616 case FIO_SG_WRITE_VERIFY:
617 if (lba < MAX_10B_LBA)
618 hdr->cmdp[0] = 0x2e; // write and verify(10)
620 hdr->cmdp[0] = 0x8e; // write and verify(16)
622 // BYTCHK is disabled by virtue of the memset in sgio_hdr_init
623 case FIO_SG_WRITE_SAME:
624 hdr->dxfer_len = sd->bs;
625 if (lba < MAX_10B_LBA)
626 hdr->cmdp[0] = 0x41; // write same(10)
628 hdr->cmdp[0] = 0x93; // write same(16)
630 case FIO_SG_WRITE_SAME_NDOB:
631 hdr->cmdp[0] = 0x93; // write same(16)
632 hdr->cmdp[1] |= 0x1; // no data output buffer
635 case FIO_SG_WRITE_STREAM:
636 hdr->cmdp[0] = 0x9a; // write stream (16)
638 hdr->cmdp[1] |= 0x08;
639 sgio_set_be64(lba, &hdr->cmdp[2]);
640 sgio_set_be16((uint16_t) io_u->file->engine_pos, &hdr->cmdp[10]);
641 sgio_set_be16((uint16_t) nr_blocks, &hdr->cmdp[12]);
643 case FIO_SG_VERIFY_BYTCHK_00:
644 if (lba < MAX_10B_LBA)
645 hdr->cmdp[0] = 0x2f; // VERIFY(10)
647 hdr->cmdp[0] = 0x8f; // VERIFY(16)
650 case FIO_SG_VERIFY_BYTCHK_01:
651 if (lba < MAX_10B_LBA)
652 hdr->cmdp[0] = 0x2f; // VERIFY(10)
654 hdr->cmdp[0] = 0x8f; // VERIFY(16)
655 hdr->cmdp[1] |= 0x02; // BYTCHK = 01b
657 case FIO_SG_VERIFY_BYTCHK_11:
658 if (lba < MAX_10B_LBA)
659 hdr->cmdp[0] = 0x2f; // VERIFY(10)
661 hdr->cmdp[0] = 0x8f; // VERIFY(16)
662 hdr->cmdp[1] |= 0x06; // BYTCHK = 11b
663 hdr->dxfer_len = sd->bs;
667 if (o->write_mode != FIO_SG_WRITE_STREAM)
668 fio_sgio_rw_lba(hdr, lba, nr_blocks,
669 o->write_mode == FIO_SG_WRITE_SAME_NDOB);
671 } else if (io_u->ddir == DDIR_TRIM) {
672 struct sgio_trim *st;
674 if (sd->current_queue == -1) {
675 sgio_hdr_init(sd, hdr, io_u, 0);
678 hdr->dxfer_direction = SG_DXFER_TO_DEV;
679 hdr->cmdp[0] = 0x42; // unmap
680 sd->current_queue = io_u->index;
681 st = sd->trim_queues[sd->current_queue];
682 hdr->dxferp = st->unmap_param;
683 #ifdef FIO_SGIO_DEBUG
684 assert(sd->trim_queues[io_u->index]->unmap_range_count == 0);
685 dprint(FD_IO, "sg: creating new queue based on io_u %d\n", io_u->index);
689 st = sd->trim_queues[sd->current_queue];
691 dprint(FD_IO, "sg: adding io_u %d to trim queue %d\n", io_u->index, sd->current_queue);
692 st->trim_io_us[st->unmap_range_count] = io_u;
693 #ifdef FIO_SGIO_DEBUG
694 sd->trim_queue_map[io_u->index] = sd->current_queue;
697 offset = 8 + 16 * st->unmap_range_count;
698 sgio_set_be64(lba, &st->unmap_param[offset]);
699 sgio_set_be32((uint32_t) nr_blocks, &st->unmap_param[offset + 8]);
701 st->unmap_range_count++;
703 } else if (ddir_sync(io_u->ddir)) {
704 sgio_hdr_init(sd, hdr, io_u, 0);
705 hdr->dxfer_direction = SG_DXFER_NONE;
706 if (lba < MAX_10B_LBA)
707 hdr->cmdp[0] = 0x35; // synccache(10)
709 hdr->cmdp[0] = 0x91; // synccache(16)
716 static void fio_sgio_unmap_setup(struct sg_io_hdr *hdr, struct sgio_trim *st)
718 uint16_t cnt = st->unmap_range_count * 16;
720 hdr->dxfer_len = cnt + 8;
721 sgio_set_be16(cnt + 8, &hdr->cmdp[7]);
722 sgio_set_be16(cnt + 6, st->unmap_param);
723 sgio_set_be16(cnt, &st->unmap_param[2]);
728 static enum fio_q_status fio_sgio_queue(struct thread_data *td,
731 struct sg_io_hdr *hdr = &io_u->hdr;
732 struct sgio_data *sd = td->io_ops_data;
733 int ret, do_sync = 0;
735 fio_ro_check(td, io_u);
737 if (sgio_unbuffered(td) || ddir_sync(io_u->ddir))
740 if (io_u->ddir == DDIR_TRIM) {
741 if (do_sync || io_u->file->filetype == FIO_TYPE_BLOCK) {
742 struct sgio_trim *st = sd->trim_queues[sd->current_queue];
744 /* finish cdb setup for unmap because we are
745 ** doing unmap commands synchronously */
746 #ifdef FIO_SGIO_DEBUG
747 assert(st->unmap_range_count == 1);
748 assert(io_u == st->trim_io_us[0]);
752 fio_sgio_unmap_setup(hdr, st);
754 st->unmap_range_count = 0;
755 sd->current_queue = -1;
757 /* queue up trim ranges and submit in commit() */
761 ret = fio_sgio_doio(td, io_u, do_sync);
765 else if (hdr->status) {
766 io_u->resid = hdr->resid;
768 } else if (td->io_ops->commit != NULL) {
769 if (do_sync && !ddir_sync(io_u->ddir)) {
770 io_u_mark_submit(td, 1);
771 io_u_mark_complete(td, 1);
772 } else if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
773 io_u_mark_submit(td, 1);
774 io_u_queued(td, io_u);
779 td_verror(td, io_u->error, "xfer");
780 return FIO_Q_COMPLETED;
786 static int fio_sgio_commit(struct thread_data *td)
788 struct sgio_data *sd = td->io_ops_data;
789 struct sgio_trim *st;
791 struct sg_io_hdr *hdr;
796 if (sd->current_queue == -1)
799 st = sd->trim_queues[sd->current_queue];
800 io_u = st->trim_io_us[0];
803 fio_sgio_unmap_setup(hdr, st);
805 sd->current_queue = -1;
807 ret = fio_sgio_rw_doio(td, io_u->file, io_u, 0);
809 if (ret < 0 || hdr->status) {
819 for (i = 0; i < st->unmap_range_count; i++) {
820 st->trim_io_us[i]->error = error;
821 clear_io_u(td, st->trim_io_us[i]);
823 st->trim_io_us[i]->resid = hdr->resid;
826 td_verror(td, error, "xfer");
830 if (fio_fill_issue_time(td)) {
831 fio_gettime(&now, NULL);
832 for (i = 0; i < st->unmap_range_count; i++) {
833 memcpy(&st->trim_io_us[i]->issue_time, &now, sizeof(now));
834 io_u_queued(td, io_u);
837 io_u_mark_submit(td, st->unmap_range_count);
842 static struct io_u *fio_sgio_event(struct thread_data *td, int event)
844 struct sgio_data *sd = td->io_ops_data;
846 return sd->events[event];
849 static int fio_sgio_read_capacity(struct thread_data *td, unsigned int *bs,
850 unsigned long long *max_lba)
853 * need to do read capacity operation w/o benefit of sd or
854 * io_u structures, which are not initialized until later.
856 struct sg_io_hdr hdr;
857 unsigned long long hlba;
858 unsigned int blksz = 0;
859 unsigned char cmd[16];
860 unsigned char sb[64];
861 unsigned char buf[32]; // read capacity return
865 struct fio_file *f = td->files[0];
867 /* open file independent of rest of application */
868 fd = open(f->file_name, O_RDONLY);
872 memset(&hdr, 0, sizeof(hdr));
873 memset(cmd, 0, sizeof(cmd));
874 memset(sb, 0, sizeof(sb));
875 memset(buf, 0, sizeof(buf));
877 /* First let's try a 10 byte read capacity. */
878 hdr.interface_id = 'S';
882 hdr.mx_sb_len = sizeof(sb);
883 hdr.timeout = SCSI_TIMEOUT_MS;
884 hdr.cmdp[0] = 0x25; // Read Capacity(10)
885 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
887 hdr.dxfer_len = sizeof(buf);
889 ret = ioctl(fd, SG_IO, &hdr);
895 if (hdr.info & SG_INFO_CHECK) {
896 /* RCAP(10) might be unsupported by device. Force RCAP(16) */
899 blksz = sgio_get_be32(&buf[4]);
900 hlba = sgio_get_be32(buf);
904 * If max lba masked by MAX_10B_LBA equals MAX_10B_LBA,
905 * then need to retry with 16 byte Read Capacity command.
907 if (hlba == MAX_10B_LBA) {
909 hdr.cmdp[0] = 0x9e; // service action
910 hdr.cmdp[1] = 0x10; // Read Capacity(16)
911 sgio_set_be32(sizeof(buf), &hdr.cmdp[10]);
913 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
915 hdr.dxfer_len = sizeof(buf);
917 ret = ioctl(fd, SG_IO, &hdr);
923 /* record if an io error occurred */
924 if (hdr.info & SG_INFO_CHECK)
925 td_verror(td, EIO, "fio_sgio_read_capacity");
927 blksz = sgio_get_be32(&buf[8]);
928 hlba = sgio_get_be64(buf);
943 static void fio_sgio_cleanup(struct thread_data *td)
945 struct sgio_data *sd = td->io_ops_data;
954 #ifdef FIO_SGIO_DEBUG
955 free(sd->trim_queue_map);
958 for (i = 0; i < td->o.iodepth; i++) {
959 free(sd->trim_queues[i]->unmap_param);
960 free(sd->trim_queues[i]->trim_io_us);
961 free(sd->trim_queues[i]);
964 free(sd->trim_queues);
969 static int fio_sgio_init(struct thread_data *td)
971 struct sgio_data *sd;
972 struct sgio_trim *st;
973 struct sg_io_hdr *h3p;
976 sd = calloc(1, sizeof(*sd));
977 sd->cmds = calloc(td->o.iodepth, sizeof(struct sgio_cmd));
978 sd->sgbuf = calloc(td->o.iodepth, sizeof(struct sg_io_hdr));
979 sd->events = calloc(td->o.iodepth, sizeof(struct io_u *));
980 sd->pfds = calloc(td->o.nr_files, sizeof(struct pollfd));
981 sd->fd_flags = calloc(td->o.nr_files, sizeof(int));
982 sd->type_checked = 0;
984 sd->trim_queues = calloc(td->o.iodepth, sizeof(struct sgio_trim *));
985 sd->current_queue = -1;
986 #ifdef FIO_SGIO_DEBUG
987 sd->trim_queue_map = calloc(td->o.iodepth, sizeof(int));
989 for (i = 0, h3p = sd->sgbuf; i < td->o.iodepth; i++, ++h3p) {
990 sd->trim_queues[i] = calloc(1, sizeof(struct sgio_trim));
991 st = sd->trim_queues[i];
992 st->unmap_param = calloc(td->o.iodepth + 1, sizeof(char[16]));
993 st->unmap_range_count = 0;
994 st->trim_io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
995 h3p->interface_id = 'S';
998 td->io_ops_data = sd;
1001 * we want to do it, regardless of whether odirect is set or not
1003 td->o.override_sync = 1;
1007 static int fio_sgio_type_check(struct thread_data *td, struct fio_file *f)
1009 struct sgio_data *sd = td->io_ops_data;
1010 unsigned int bs = 0;
1011 unsigned long long max_lba = 0;
1013 if (f->filetype == FIO_TYPE_BLOCK) {
1014 if (ioctl(f->fd, BLKSSZGET, &bs) < 0) {
1015 td_verror(td, errno, "ioctl");
1018 } else if (f->filetype == FIO_TYPE_CHAR) {
1021 if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) {
1022 td_verror(td, errno, "ioctl");
1026 ret = fio_sgio_read_capacity(td, &bs, &max_lba);
1028 td_verror(td, td->error, "fio_sgio_read_capacity");
1029 log_err("ioengine sg unable to read capacity successfully\n");
1033 td_verror(td, EINVAL, "wrong file type");
1034 log_err("ioengine sg only works on block or character devices\n");
1039 // Determine size of commands needed based on max_lba
1040 if (max_lba >= MAX_10B_LBA) {
1041 dprint(FD_IO, "sgio_type_check: using 16 byte read/write "
1042 "commands for lba above 0x%016llx/0x%016llx\n",
1043 MAX_10B_LBA, max_lba);
1046 if (f->filetype == FIO_TYPE_BLOCK) {
1047 td->io_ops->getevents = NULL;
1048 td->io_ops->event = NULL;
1049 td->io_ops->commit = NULL;
1051 ** Setting these functions to null may cause problems
1052 ** with filename=/dev/sda:/dev/sg0 since we are only
1053 ** considering a single file
1056 sd->type_checked = 1;
1061 static int fio_sgio_stream_control(struct fio_file *f, bool open_stream, uint16_t *stream_id)
1063 struct sg_io_hdr hdr;
1064 unsigned char cmd[16];
1065 unsigned char sb[64];
1066 unsigned char buf[8];
1069 memset(&hdr, 0, sizeof(hdr));
1070 memset(cmd, 0, sizeof(cmd));
1071 memset(sb, 0, sizeof(sb));
1072 memset(buf, 0, sizeof(buf));
1074 hdr.interface_id = 'S';
1078 hdr.mx_sb_len = sizeof(sb);
1079 hdr.timeout = SCSI_TIMEOUT_MS;
1081 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
1083 hdr.dxfer_len = sizeof(buf);
1084 sgio_set_be32(sizeof(buf), &hdr.cmdp[10]);
1090 sgio_set_be16(*stream_id, &hdr.cmdp[4]);
1093 ret = ioctl(f->fd, SG_IO, &hdr);
1098 if (hdr.info & SG_INFO_CHECK)
1102 *stream_id = sgio_get_be16(&buf[4]);
1103 dprint(FD_FILE, "sgio_stream_control: opened stream %u\n", (unsigned int) *stream_id);
1104 assert(*stream_id != 0);
1106 dprint(FD_FILE, "sgio_stream_control: closed stream %u\n", (unsigned int) *stream_id);
1111 static int fio_sgio_open(struct thread_data *td, struct fio_file *f)
1113 struct sgio_data *sd = td->io_ops_data;
1114 struct sg_options *o = td->eo;
1117 ret = generic_open_file(td, f);
1121 if (sd && !sd->type_checked && fio_sgio_type_check(td, f)) {
1122 ret = generic_close_file(td, f);
1126 if (o->write_mode == FIO_SG_WRITE_STREAM) {
1128 f->engine_pos = o->stream_id;
1130 ret = fio_sgio_stream_control(f, true, (uint16_t *) &f->engine_pos);
1139 int fio_sgio_close(struct thread_data *td, struct fio_file *f)
1141 struct sg_options *o = td->eo;
1144 if (!o->stream_id && o->write_mode == FIO_SG_WRITE_STREAM) {
1145 ret = fio_sgio_stream_control(f, false, (uint16_t *) &f->engine_pos);
1150 return generic_close_file(td, f);
1154 * Build an error string with details about the driver, host or scsi
1155 * error contained in the sg header Caller will use as necessary.
1157 static char *fio_sgio_errdetails(struct io_u *io_u)
1159 struct sg_io_hdr *hdr = &io_u->hdr;
1160 #define MAXERRDETAIL 1024
1161 #define MAXMSGCHUNK 128
1162 char *msg, msgchunk[MAXMSGCHUNK];
1165 msg = calloc(1, MAXERRDETAIL);
1169 * can't seem to find sg_err.h, so I'll just echo the define values
1170 * so others can search on internet to find clearer clues of meaning.
1172 if (hdr->info & SG_INFO_CHECK) {
1173 if (hdr->host_status) {
1174 snprintf(msgchunk, MAXMSGCHUNK, "SG Host Status: 0x%02x; ", hdr->host_status);
1175 strlcat(msg, msgchunk, MAXERRDETAIL);
1176 switch (hdr->host_status) {
1178 strlcat(msg, "SG_ERR_DID_NO_CONNECT", MAXERRDETAIL);
1181 strlcat(msg, "SG_ERR_DID_BUS_BUSY", MAXERRDETAIL);
1184 strlcat(msg, "SG_ERR_DID_TIME_OUT", MAXERRDETAIL);
1187 strlcat(msg, "SG_ERR_DID_BAD_TARGET", MAXERRDETAIL);
1190 strlcat(msg, "SG_ERR_DID_ABORT", MAXERRDETAIL);
1193 strlcat(msg, "SG_ERR_DID_PARITY", MAXERRDETAIL);
1196 strlcat(msg, "SG_ERR_DID_ERROR (internal error)", MAXERRDETAIL);
1199 strlcat(msg, "SG_ERR_DID_RESET", MAXERRDETAIL);
1202 strlcat(msg, "SG_ERR_DID_BAD_INTR (unexpected)", MAXERRDETAIL);
1205 strlcat(msg, "SG_ERR_DID_PASSTHROUGH", MAXERRDETAIL);
1208 strlcat(msg, "SG_ERR_DID_SOFT_ERROR (driver retry?)", MAXERRDETAIL);
1211 strlcat(msg, "SG_ERR_DID_IMM_RETRY", MAXERRDETAIL);
1214 strlcat(msg, "SG_ERR_DID_REQUEUE", MAXERRDETAIL);
1217 strlcat(msg, "SG_ERR_DID_TRANSPORT_DISRUPTED", MAXERRDETAIL);
1220 strlcat(msg, "SG_ERR_DID_TRANSPORT_FAILFAST", MAXERRDETAIL);
1223 strlcat(msg, "SG_ERR_DID_TARGET_FAILURE", MAXERRDETAIL);
1226 strlcat(msg, "SG_ERR_DID_NEXUS_FAILURE", MAXERRDETAIL);
1229 strlcat(msg, "SG_ERR_DID_ALLOC_FAILURE", MAXERRDETAIL);
1232 strlcat(msg, "SG_ERR_DID_MEDIUM_ERROR", MAXERRDETAIL);
1235 strlcat(msg, "Unknown", MAXERRDETAIL);
1238 strlcat(msg, ". ", MAXERRDETAIL);
1240 if (hdr->driver_status) {
1241 snprintf(msgchunk, MAXMSGCHUNK, "SG Driver Status: 0x%02x; ", hdr->driver_status);
1242 strlcat(msg, msgchunk, MAXERRDETAIL);
1243 switch (hdr->driver_status & 0x0F) {
1245 strlcat(msg, "SG_ERR_DRIVER_BUSY", MAXERRDETAIL);
1248 strlcat(msg, "SG_ERR_DRIVER_SOFT", MAXERRDETAIL);
1251 strlcat(msg, "SG_ERR_DRIVER_MEDIA", MAXERRDETAIL);
1254 strlcat(msg, "SG_ERR_DRIVER_ERROR", MAXERRDETAIL);
1257 strlcat(msg, "SG_ERR_DRIVER_INVALID", MAXERRDETAIL);
1260 strlcat(msg, "SG_ERR_DRIVER_TIMEOUT", MAXERRDETAIL);
1263 strlcat(msg, "SG_ERR_DRIVER_HARD", MAXERRDETAIL);
1266 strlcat(msg, "SG_ERR_DRIVER_SENSE", MAXERRDETAIL);
1269 strlcat(msg, "Unknown", MAXERRDETAIL);
1272 strlcat(msg, "; ", MAXERRDETAIL);
1273 switch (hdr->driver_status & 0xF0) {
1275 strlcat(msg, "SG_ERR_SUGGEST_RETRY", MAXERRDETAIL);
1278 strlcat(msg, "SG_ERR_SUGGEST_ABORT", MAXERRDETAIL);
1281 strlcat(msg, "SG_ERR_SUGGEST_REMAP", MAXERRDETAIL);
1284 strlcat(msg, "SG_ERR_SUGGEST_DIE", MAXERRDETAIL);
1287 strlcat(msg, "SG_ERR_SUGGEST_SENSE", MAXERRDETAIL);
1290 strlcat(msg, ". ", MAXERRDETAIL);
1293 snprintf(msgchunk, MAXMSGCHUNK, "SG SCSI Status: 0x%02x; ", hdr->status);
1294 strlcat(msg, msgchunk, MAXERRDETAIL);
1295 // SCSI 3 status codes
1296 switch (hdr->status) {
1298 strlcat(msg, "CHECK_CONDITION", MAXERRDETAIL);
1301 strlcat(msg, "CONDITION_MET", MAXERRDETAIL);
1304 strlcat(msg, "BUSY", MAXERRDETAIL);
1307 strlcat(msg, "INTERMEDIATE", MAXERRDETAIL);
1310 strlcat(msg, "INTERMEDIATE_CONDITION_MET", MAXERRDETAIL);
1313 strlcat(msg, "RESERVATION_CONFLICT", MAXERRDETAIL);
1316 strlcat(msg, "COMMAND_TERMINATED", MAXERRDETAIL);
1319 strlcat(msg, "TASK_SET_FULL", MAXERRDETAIL);
1322 strlcat(msg, "ACA_ACTIVE", MAXERRDETAIL);
1325 strlcat(msg, "TASK_ABORTED", MAXERRDETAIL);
1328 strlcat(msg, "Unknown", MAXERRDETAIL);
1331 strlcat(msg, ". ", MAXERRDETAIL);
1333 if (hdr->sb_len_wr) {
1334 snprintf(msgchunk, MAXMSGCHUNK, "Sense Data (%d bytes):", hdr->sb_len_wr);
1335 strlcat(msg, msgchunk, MAXERRDETAIL);
1336 for (i = 0; i < hdr->sb_len_wr; i++) {
1337 snprintf(msgchunk, MAXMSGCHUNK, " %02x", hdr->sbp[i]);
1338 strlcat(msg, msgchunk, MAXERRDETAIL);
1340 strlcat(msg, ". ", MAXERRDETAIL);
1342 if (hdr->resid != 0) {
1343 snprintf(msgchunk, MAXMSGCHUNK, "SG Driver: %d bytes out of %d not transferred. ", hdr->resid, hdr->dxfer_len);
1344 strlcat(msg, msgchunk, MAXERRDETAIL);
1347 strlcat(msg, "cdb:", MAXERRDETAIL);
1348 for (i = 0; i < hdr->cmd_len; i++) {
1349 snprintf(msgchunk, MAXMSGCHUNK, " %02x", hdr->cmdp[i]);
1350 strlcat(msg, msgchunk, MAXERRDETAIL);
1352 strlcat(msg, ". ", MAXERRDETAIL);
1353 if (io_u->ddir == DDIR_TRIM) {
1354 unsigned char *param_list = hdr->dxferp;
1355 strlcat(msg, "dxferp:", MAXERRDETAIL);
1356 for (i = 0; i < hdr->dxfer_len; i++) {
1357 snprintf(msgchunk, MAXMSGCHUNK, " %02x", param_list[i]);
1358 strlcat(msg, msgchunk, MAXERRDETAIL);
1360 strlcat(msg, ". ", MAXERRDETAIL);
1365 if (!(hdr->info & SG_INFO_CHECK) && !strlen(msg))
1366 snprintf(msg, MAXERRDETAIL, "%s",
1367 "SG Driver did not report a Host, Driver or Device check");
1373 * get max file size from read capacity.
1375 static int fio_sgio_get_file_size(struct thread_data *td, struct fio_file *f)
1378 * get_file_size is being called even before sgio_init is
1379 * called, so none of the sg_io structures are
1380 * initialized in the thread_data yet. So we need to do the
1381 * ReadCapacity without any of those helpers. One of the effects
1382 * is that ReadCapacity may get called 4 times on each open:
1383 * readcap(10) followed by readcap(16) if needed - just to get
1384 * the file size after the init occurs - it will be called
1385 * again when "type_check" is called during structure
1386 * initialization I'm not sure how to prevent this little
1389 unsigned int bs = 0;
1390 unsigned long long max_lba = 0;
1393 if (fio_file_size_known(f))
1396 if (f->filetype != FIO_TYPE_BLOCK && f->filetype != FIO_TYPE_CHAR) {
1397 td_verror(td, EINVAL, "wrong file type");
1398 log_err("ioengine sg only works on block or character devices\n");
1402 ret = fio_sgio_read_capacity(td, &bs, &max_lba);
1404 td_verror(td, td->error, "fio_sgio_read_capacity");
1405 log_err("ioengine sg unable to successfully execute read capacity to get block size and maximum lba\n");
1409 f->real_file_size = (max_lba + 1) * bs;
1410 fio_file_set_size_known(f);
1415 static struct ioengine_ops ioengine = {
1417 .version = FIO_IOOPS_VERSION,
1418 .init = fio_sgio_init,
1419 .prep = fio_sgio_prep,
1420 .queue = fio_sgio_queue,
1421 .commit = fio_sgio_commit,
1422 .getevents = fio_sgio_getevents,
1423 .errdetails = fio_sgio_errdetails,
1424 .event = fio_sgio_event,
1425 .cleanup = fio_sgio_cleanup,
1426 .open_file = fio_sgio_open,
1427 .close_file = fio_sgio_close,
1428 .get_file_size = fio_sgio_get_file_size,
1429 .flags = FIO_SYNCIO | FIO_RAWIO,
1431 .option_struct_size = sizeof(struct sg_options)
1434 #else /* FIO_HAVE_SGIO */
1437 * When we have a proper configure system in place, we simply wont build
1438 * and install this io engine. For now install a crippled version that
1439 * just complains and fails to load.
1441 static int fio_sgio_init(struct thread_data fio_unused *td)
1443 log_err("fio: ioengine sg not available\n");
1447 static struct ioengine_ops ioengine = {
1449 .version = FIO_IOOPS_VERSION,
1450 .init = fio_sgio_init,
1455 static void fio_init fio_sgio_register(void)
1457 register_ioengine(&ioengine);
1460 static void fio_exit fio_sgio_unregister(void)
1462 unregister_ioengine(&ioengine);