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,
72 FIO_SG_VERIFY_BYTCHK_00,
73 FIO_SG_VERIFY_BYTCHK_01,
74 FIO_SG_VERIFY_BYTCHK_11,
81 unsigned int writefua;
82 unsigned int write_mode;
85 static struct fio_option options[] = {
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,
97 .lname = "sg engine read fua flag support",
99 .off1 = offsetof(struct sg_options, readfua),
100 .help = "Set FUA flag (force unit access) for all Read operations",
102 .category = FIO_OPT_C_ENGINE,
103 .group = FIO_OPT_G_SG,
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",
112 .category = FIO_OPT_C_ENGINE,
113 .group = FIO_OPT_G_SG,
116 .name = "sg_write_mode",
117 .lname = "specify sg write mode",
119 .off1 = offsetof(struct sg_options, write_mode),
120 .help = "Specify SCSI WRITE mode",
124 .oval = FIO_SG_WRITE,
125 .help = "Issue standard SCSI WRITE commands",
128 .oval = FIO_SG_WRITE_VERIFY,
129 .help = "Issue SCSI WRITE AND VERIFY commands",
132 .oval = FIO_SG_WRITE_SAME,
133 .help = "Issue SCSI WRITE SAME commands",
135 { .ival = "write_same_ndob",
136 .oval = FIO_SG_WRITE_SAME_NDOB,
137 .help = "Issue SCSI WRITE SAME(16) commands with NDOB flag set",
139 { .ival = "verify_bytchk_00",
140 .oval = FIO_SG_VERIFY_BYTCHK_00,
141 .help = "Issue SCSI VERIFY commands with BYTCHK set to 00",
143 { .ival = "verify_bytchk_01",
144 .oval = FIO_SG_VERIFY_BYTCHK_01,
145 .help = "Issue SCSI VERIFY commands with BYTCHK set to 01",
147 { .ival = "verify_bytchk_11",
148 .oval = FIO_SG_VERIFY_BYTCHK_11,
149 .help = "Issue SCSI VERIFY commands with BYTCHK set to 11",
152 .category = FIO_OPT_C_ENGINE,
153 .group = FIO_OPT_G_SG,
160 #define MAX_10B_LBA 0xFFFFFFFFULL
161 #define SCSI_TIMEOUT_MS 30000 // 30 second timeout; currently no method to override
162 #define MAX_SB 64 // sense block maximum return size
164 #define FIO_SGIO_DEBUG
168 unsigned char cdb[16]; // enhanced from 10 to support 16 byte commands
169 unsigned char sb[MAX_SB]; // add sense block to commands
174 uint8_t *unmap_param;
175 unsigned int unmap_range_count;
176 struct io_u **trim_io_us;
180 struct sgio_cmd *cmds;
181 struct io_u **events;
187 struct sgio_trim **trim_queues;
189 #ifdef FIO_SGIO_DEBUG
190 unsigned int *trim_queue_map;
194 static inline uint32_t sgio_get_be32(uint8_t *buf)
196 return be32_to_cpu(*((uint32_t *) buf));
199 static inline uint64_t sgio_get_be64(uint8_t *buf)
201 return be64_to_cpu(*((uint64_t *) buf));
204 static inline void sgio_set_be16(uint16_t val, uint8_t *buf)
206 uint16_t t = cpu_to_be16(val);
208 memcpy(buf, &t, sizeof(uint16_t));
211 static inline void sgio_set_be32(uint32_t val, uint8_t *buf)
213 uint32_t t = cpu_to_be32(val);
215 memcpy(buf, &t, sizeof(uint32_t));
218 static inline void sgio_set_be64(uint64_t val, uint8_t *buf)
220 uint64_t t = cpu_to_be64(val);
222 memcpy(buf, &t, sizeof(uint64_t));
225 static inline bool sgio_unbuffered(struct thread_data *td)
227 return (td->o.odirect || td->o.sync_io);
230 static void sgio_hdr_init(struct sgio_data *sd, struct sg_io_hdr *hdr,
231 struct io_u *io_u, int fs)
233 struct sgio_cmd *sc = &sd->cmds[io_u->index];
235 memset(hdr, 0, sizeof(*hdr));
236 memset(sc->cdb, 0, sizeof(sc->cdb));
238 hdr->interface_id = 'S';
240 hdr->cmd_len = sizeof(sc->cdb);
242 hdr->mx_sb_len = sizeof(sc->sb);
243 hdr->pack_id = io_u->index;
245 hdr->timeout = SCSI_TIMEOUT_MS;
248 hdr->dxferp = io_u->xfer_buf;
249 hdr->dxfer_len = io_u->xfer_buflen;
253 static int pollin_events(struct pollfd *pfds, int fds)
257 for (i = 0; i < fds; i++)
258 if (pfds[i].revents & POLLIN)
264 static int sg_fd_read(int fd, void *data, size_t size)
271 ret = read(fd, data, size);
273 if (errno == EAGAIN || errno == EINTR)
293 static int fio_sgio_getevents(struct thread_data *td, unsigned int min,
295 const struct timespec fio_unused *t)
297 struct sgio_data *sd = td->io_ops_data;
298 int left = max, eventNum, ret, r = 0, trims = 0;
299 void *buf = sd->sgbuf;
300 unsigned int i, j, events;
305 * Fill in the file descriptors
307 for_each_file(td, f, i) {
309 * don't block for min events == 0
312 sd->fd_flags[i] = fio_set_fd_nonblocking(f->fd, "sg");
314 sd->fd_flags[i] = -1;
316 sd->pfds[i].fd = f->fd;
317 sd->pfds[i].events = POLLIN;
321 ** There are two counters here:
322 ** - number of SCSI commands completed
323 ** - number of io_us completed
325 ** These are the same with reads and writes, but
326 ** could differ with trim/unmap commands because
327 ** a single unmap can include multiple io_us
333 dprint(FD_IO, "sgio_getevents: sd %p: min=%d, max=%d, left=%d\n", sd, min, max, left);
339 ret = poll(sd->pfds, td->o.nr_files, -1);
343 td_verror(td, errno, "poll");
348 if (pollin_events(sd->pfds, td->o.nr_files))
358 for_each_file(td, f, i) {
359 for (eventNum = 0; eventNum < left; eventNum++) {
360 ret = sg_fd_read(f->fd, p, sizeof(struct sg_io_hdr));
361 dprint(FD_IO, "sgio_getevents: sg_fd_read ret: %d\n", ret);
364 td_verror(td, r, "sg_read");
367 io_u = ((struct sg_io_hdr *)p)->usr_ptr;
368 if (io_u->ddir == DDIR_TRIM) {
369 events += sd->trim_queues[io_u->index]->unmap_range_count;
370 eventNum += sd->trim_queues[io_u->index]->unmap_range_count - 1;
374 p += sizeof(struct sg_io_hdr);
375 dprint(FD_IO, "sgio_getevents: events: %d, eventNum: %d, left: %d\n", events, eventNum, left);
379 if (r < 0 && !events)
389 for (i = 0; i < events; i++) {
390 struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i;
391 sd->events[i + trims] = hdr->usr_ptr;
392 io_u = (struct io_u *)(hdr->usr_ptr);
394 if (hdr->info & SG_INFO_CHECK) {
395 /* record if an io error occurred, ignore resid */
396 memcpy(&io_u->hdr, hdr, sizeof(struct sg_io_hdr));
397 sd->events[i + trims]->error = EIO;
400 if (io_u->ddir == DDIR_TRIM) {
401 struct sgio_trim *st = sd->trim_queues[io_u->index];
402 #ifdef FIO_SGIO_DEBUG
403 assert(st->trim_io_us[0] == io_u);
404 assert(sd->trim_queue_map[io_u->index] == io_u->index);
405 dprint(FD_IO, "sgio_getevents: reaping %d io_us from trim queue %d\n", st->unmap_range_count, io_u->index);
406 dprint(FD_IO, "sgio_getevents: reaped io_u %d and stored in events[%d]\n", io_u->index, i+trims);
408 for (j = 1; j < st->unmap_range_count; j++) {
410 sd->events[i + trims] = st->trim_io_us[j];
411 #ifdef FIO_SGIO_DEBUG
412 dprint(FD_IO, "sgio_getevents: reaped io_u %d and stored in events[%d]\n", st->trim_io_us[j]->index, i+trims);
413 assert(sd->trim_queue_map[st->trim_io_us[j]->index] == io_u->index);
415 if (hdr->info & SG_INFO_CHECK) {
416 /* record if an io error occurred, ignore resid */
417 memcpy(&st->trim_io_us[j]->hdr, hdr, sizeof(struct sg_io_hdr));
418 sd->events[i + trims]->error = EIO;
421 events -= st->unmap_range_count - 1;
422 st->unmap_range_count = 0;
428 for_each_file(td, f, i) {
429 if (sd->fd_flags[i] == -1)
432 if (fcntl(f->fd, F_SETFL, sd->fd_flags[i]) < 0)
433 log_err("fio: sg failed to restore fcntl flags: %s\n", strerror(errno));
440 static enum fio_q_status fio_sgio_ioctl_doio(struct thread_data *td,
444 struct sgio_data *sd = td->io_ops_data;
445 struct sg_io_hdr *hdr = &io_u->hdr;
448 sd->events[0] = io_u;
450 ret = ioctl(f->fd, SG_IO, hdr);
454 /* record if an io error occurred */
455 if (hdr->info & SG_INFO_CHECK)
458 return FIO_Q_COMPLETED;
461 static enum fio_q_status fio_sgio_rw_doio(struct thread_data *td,
463 struct io_u *io_u, int do_sync)
465 struct sg_io_hdr *hdr = &io_u->hdr;
468 ret = write(f->fd, hdr, sizeof(*hdr));
474 * We can't just read back the first command that completes
475 * and assume it's the one we need, it could be any command
481 ret = read(f->fd, hdr, sizeof(*hdr));
485 __io_u = hdr->usr_ptr;
487 /* record if an io error occurred */
488 if (hdr->info & SG_INFO_CHECK)
494 if (io_u_sync_complete(td, __io_u))
499 return FIO_Q_COMPLETED;
505 static enum fio_q_status fio_sgio_doio(struct thread_data *td,
506 struct io_u *io_u, int do_sync)
508 struct fio_file *f = io_u->file;
509 enum fio_q_status ret;
511 if (f->filetype == FIO_TYPE_BLOCK) {
512 ret = fio_sgio_ioctl_doio(td, f, io_u);
514 td_verror(td, io_u->error, __func__);
516 ret = fio_sgio_rw_doio(td, f, io_u, do_sync);
517 if (io_u->error && do_sync)
518 td_verror(td, io_u->error, __func__);
524 static void fio_sgio_rw_lba(struct sg_io_hdr *hdr, unsigned long long lba,
525 unsigned long long nr_blocks, bool override16)
527 if (lba < MAX_10B_LBA && !override16) {
528 sgio_set_be32((uint32_t) lba, &hdr->cmdp[2]);
529 sgio_set_be16((uint16_t) nr_blocks, &hdr->cmdp[7]);
531 sgio_set_be64(lba, &hdr->cmdp[2]);
532 sgio_set_be32((uint32_t) nr_blocks, &hdr->cmdp[10]);
538 static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u)
540 struct sg_io_hdr *hdr = &io_u->hdr;
541 struct sg_options *o = td->eo;
542 struct sgio_data *sd = td->io_ops_data;
543 unsigned long long nr_blocks, lba;
546 if (io_u->xfer_buflen & (sd->bs - 1)) {
547 log_err("read/write not sector aligned\n");
551 nr_blocks = io_u->xfer_buflen / sd->bs;
552 lba = io_u->offset / sd->bs;
554 if (io_u->ddir == DDIR_READ) {
555 sgio_hdr_init(sd, hdr, io_u, 1);
557 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
558 if (lba < MAX_10B_LBA)
559 hdr->cmdp[0] = 0x28; // read(10)
561 hdr->cmdp[0] = 0x88; // read(16)
564 hdr->flags |= SGV4_FLAG_HIPRI;
566 hdr->cmdp[1] |= 0x08;
568 fio_sgio_rw_lba(hdr, lba, nr_blocks, false);
570 } else if (io_u->ddir == DDIR_WRITE) {
571 sgio_hdr_init(sd, hdr, io_u, 1);
573 hdr->dxfer_direction = SG_DXFER_TO_DEV;
574 switch(o->write_mode) {
576 if (lba < MAX_10B_LBA)
577 hdr->cmdp[0] = 0x2a; // write(10)
579 hdr->cmdp[0] = 0x8a; // write(16)
581 hdr->flags |= SGV4_FLAG_HIPRI;
583 hdr->cmdp[1] |= 0x08;
585 case FIO_SG_WRITE_VERIFY:
586 if (lba < MAX_10B_LBA)
587 hdr->cmdp[0] = 0x2e; // write and verify(10)
589 hdr->cmdp[0] = 0x8e; // write and verify(16)
591 // BYTCHK is disabled by virtue of the memset in sgio_hdr_init
592 case FIO_SG_WRITE_SAME:
593 hdr->dxfer_len = sd->bs;
594 if (lba < MAX_10B_LBA)
595 hdr->cmdp[0] = 0x41; // write same(10)
597 hdr->cmdp[0] = 0x93; // write same(16)
599 case FIO_SG_WRITE_SAME_NDOB:
600 hdr->cmdp[0] = 0x93; // write same(16)
601 hdr->cmdp[1] |= 0x1; // no data output buffer
604 case FIO_SG_VERIFY_BYTCHK_00:
605 if (lba < MAX_10B_LBA)
606 hdr->cmdp[0] = 0x2f; // VERIFY(10)
608 hdr->cmdp[0] = 0x8f; // VERIFY(16)
611 case FIO_SG_VERIFY_BYTCHK_01:
612 if (lba < MAX_10B_LBA)
613 hdr->cmdp[0] = 0x2f; // VERIFY(10)
615 hdr->cmdp[0] = 0x8f; // VERIFY(16)
616 hdr->cmdp[1] |= 0x02; // BYTCHK = 01b
618 case FIO_SG_VERIFY_BYTCHK_11:
619 if (lba < MAX_10B_LBA)
620 hdr->cmdp[0] = 0x2f; // VERIFY(10)
622 hdr->cmdp[0] = 0x8f; // VERIFY(16)
623 hdr->cmdp[1] |= 0x06; // BYTCHK = 11b
624 hdr->dxfer_len = sd->bs;
628 fio_sgio_rw_lba(hdr, lba, nr_blocks,
629 o->write_mode == FIO_SG_WRITE_SAME_NDOB);
631 } else if (io_u->ddir == DDIR_TRIM) {
632 struct sgio_trim *st;
634 if (sd->current_queue == -1) {
635 sgio_hdr_init(sd, hdr, io_u, 0);
638 hdr->dxfer_direction = SG_DXFER_TO_DEV;
639 hdr->cmdp[0] = 0x42; // unmap
640 sd->current_queue = io_u->index;
641 st = sd->trim_queues[sd->current_queue];
642 hdr->dxferp = st->unmap_param;
643 #ifdef FIO_SGIO_DEBUG
644 assert(sd->trim_queues[io_u->index]->unmap_range_count == 0);
645 dprint(FD_IO, "sg: creating new queue based on io_u %d\n", io_u->index);
649 st = sd->trim_queues[sd->current_queue];
651 dprint(FD_IO, "sg: adding io_u %d to trim queue %d\n", io_u->index, sd->current_queue);
652 st->trim_io_us[st->unmap_range_count] = io_u;
653 #ifdef FIO_SGIO_DEBUG
654 sd->trim_queue_map[io_u->index] = sd->current_queue;
657 offset = 8 + 16 * st->unmap_range_count;
658 sgio_set_be64(lba, &st->unmap_param[offset]);
659 sgio_set_be32((uint32_t) nr_blocks, &st->unmap_param[offset + 8]);
661 st->unmap_range_count++;
663 } else if (ddir_sync(io_u->ddir)) {
664 sgio_hdr_init(sd, hdr, io_u, 0);
665 hdr->dxfer_direction = SG_DXFER_NONE;
666 if (lba < MAX_10B_LBA)
667 hdr->cmdp[0] = 0x35; // synccache(10)
669 hdr->cmdp[0] = 0x91; // synccache(16)
676 static void fio_sgio_unmap_setup(struct sg_io_hdr *hdr, struct sgio_trim *st)
678 uint16_t cnt = st->unmap_range_count * 16;
680 hdr->dxfer_len = cnt + 8;
681 sgio_set_be16(cnt + 8, &hdr->cmdp[7]);
682 sgio_set_be16(cnt + 6, st->unmap_param);
683 sgio_set_be16(cnt, &st->unmap_param[2]);
688 static enum fio_q_status fio_sgio_queue(struct thread_data *td,
691 struct sg_io_hdr *hdr = &io_u->hdr;
692 struct sgio_data *sd = td->io_ops_data;
693 int ret, do_sync = 0;
695 fio_ro_check(td, io_u);
697 if (sgio_unbuffered(td) || ddir_sync(io_u->ddir))
700 if (io_u->ddir == DDIR_TRIM) {
701 if (do_sync || io_u->file->filetype == FIO_TYPE_BLOCK) {
702 struct sgio_trim *st = sd->trim_queues[sd->current_queue];
704 /* finish cdb setup for unmap because we are
705 ** doing unmap commands synchronously */
706 #ifdef FIO_SGIO_DEBUG
707 assert(st->unmap_range_count == 1);
708 assert(io_u == st->trim_io_us[0]);
712 fio_sgio_unmap_setup(hdr, st);
714 st->unmap_range_count = 0;
715 sd->current_queue = -1;
717 /* queue up trim ranges and submit in commit() */
721 ret = fio_sgio_doio(td, io_u, do_sync);
725 else if (hdr->status) {
726 io_u->resid = hdr->resid;
728 } else if (td->io_ops->commit != NULL) {
729 if (do_sync && !ddir_sync(io_u->ddir)) {
730 io_u_mark_submit(td, 1);
731 io_u_mark_complete(td, 1);
732 } else if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
733 io_u_mark_submit(td, 1);
734 io_u_queued(td, io_u);
739 td_verror(td, io_u->error, "xfer");
740 return FIO_Q_COMPLETED;
746 static int fio_sgio_commit(struct thread_data *td)
748 struct sgio_data *sd = td->io_ops_data;
749 struct sgio_trim *st;
751 struct sg_io_hdr *hdr;
756 if (sd->current_queue == -1)
759 st = sd->trim_queues[sd->current_queue];
760 io_u = st->trim_io_us[0];
763 fio_sgio_unmap_setup(hdr, st);
765 sd->current_queue = -1;
767 ret = fio_sgio_rw_doio(td, io_u->file, io_u, 0);
769 if (ret < 0 || hdr->status) {
779 for (i = 0; i < st->unmap_range_count; i++) {
780 st->trim_io_us[i]->error = error;
781 clear_io_u(td, st->trim_io_us[i]);
783 st->trim_io_us[i]->resid = hdr->resid;
786 td_verror(td, error, "xfer");
790 if (fio_fill_issue_time(td)) {
791 fio_gettime(&now, NULL);
792 for (i = 0; i < st->unmap_range_count; i++) {
793 memcpy(&st->trim_io_us[i]->issue_time, &now, sizeof(now));
794 io_u_queued(td, io_u);
797 io_u_mark_submit(td, st->unmap_range_count);
802 static struct io_u *fio_sgio_event(struct thread_data *td, int event)
804 struct sgio_data *sd = td->io_ops_data;
806 return sd->events[event];
809 static int fio_sgio_read_capacity(struct thread_data *td, unsigned int *bs,
810 unsigned long long *max_lba)
813 * need to do read capacity operation w/o benefit of sd or
814 * io_u structures, which are not initialized until later.
816 struct sg_io_hdr hdr;
817 unsigned long long hlba;
818 unsigned int blksz = 0;
819 unsigned char cmd[16];
820 unsigned char sb[64];
821 unsigned char buf[32]; // read capacity return
825 struct fio_file *f = td->files[0];
827 /* open file independent of rest of application */
828 fd = open(f->file_name, O_RDONLY);
832 memset(&hdr, 0, sizeof(hdr));
833 memset(cmd, 0, sizeof(cmd));
834 memset(sb, 0, sizeof(sb));
835 memset(buf, 0, sizeof(buf));
837 /* First let's try a 10 byte read capacity. */
838 hdr.interface_id = 'S';
842 hdr.mx_sb_len = sizeof(sb);
843 hdr.timeout = SCSI_TIMEOUT_MS;
844 hdr.cmdp[0] = 0x25; // Read Capacity(10)
845 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
847 hdr.dxfer_len = sizeof(buf);
849 ret = ioctl(fd, SG_IO, &hdr);
855 if (hdr.info & SG_INFO_CHECK) {
856 /* RCAP(10) might be unsupported by device. Force RCAP(16) */
859 blksz = sgio_get_be32(&buf[4]);
860 hlba = sgio_get_be32(buf);
864 * If max lba masked by MAX_10B_LBA equals MAX_10B_LBA,
865 * then need to retry with 16 byte Read Capacity command.
867 if (hlba == MAX_10B_LBA) {
869 hdr.cmdp[0] = 0x9e; // service action
870 hdr.cmdp[1] = 0x10; // Read Capacity(16)
871 sgio_set_be32(sizeof(buf), &hdr.cmdp[10]);
873 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
875 hdr.dxfer_len = sizeof(buf);
877 ret = ioctl(fd, SG_IO, &hdr);
883 /* record if an io error occurred */
884 if (hdr.info & SG_INFO_CHECK)
885 td_verror(td, EIO, "fio_sgio_read_capacity");
887 blksz = sgio_get_be32(&buf[8]);
888 hlba = sgio_get_be64(buf);
903 static void fio_sgio_cleanup(struct thread_data *td)
905 struct sgio_data *sd = td->io_ops_data;
914 #ifdef FIO_SGIO_DEBUG
915 free(sd->trim_queue_map);
918 for (i = 0; i < td->o.iodepth; i++) {
919 free(sd->trim_queues[i]->unmap_param);
920 free(sd->trim_queues[i]->trim_io_us);
921 free(sd->trim_queues[i]);
924 free(sd->trim_queues);
929 static int fio_sgio_init(struct thread_data *td)
931 struct sgio_data *sd;
932 struct sgio_trim *st;
933 struct sg_io_hdr *h3p;
936 sd = calloc(1, sizeof(*sd));
937 sd->cmds = calloc(td->o.iodepth, sizeof(struct sgio_cmd));
938 sd->sgbuf = calloc(td->o.iodepth, sizeof(struct sg_io_hdr));
939 sd->events = calloc(td->o.iodepth, sizeof(struct io_u *));
940 sd->pfds = calloc(td->o.nr_files, sizeof(struct pollfd));
941 sd->fd_flags = calloc(td->o.nr_files, sizeof(int));
942 sd->type_checked = 0;
944 sd->trim_queues = calloc(td->o.iodepth, sizeof(struct sgio_trim *));
945 sd->current_queue = -1;
946 #ifdef FIO_SGIO_DEBUG
947 sd->trim_queue_map = calloc(td->o.iodepth, sizeof(int));
949 for (i = 0, h3p = sd->sgbuf; i < td->o.iodepth; i++, ++h3p) {
950 sd->trim_queues[i] = calloc(1, sizeof(struct sgio_trim));
951 st = sd->trim_queues[i];
952 st->unmap_param = calloc(td->o.iodepth + 1, sizeof(char[16]));
953 st->unmap_range_count = 0;
954 st->trim_io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
955 h3p->interface_id = 'S';
958 td->io_ops_data = sd;
961 * we want to do it, regardless of whether odirect is set or not
963 td->o.override_sync = 1;
967 static int fio_sgio_type_check(struct thread_data *td, struct fio_file *f)
969 struct sgio_data *sd = td->io_ops_data;
971 unsigned long long max_lba = 0;
973 if (f->filetype == FIO_TYPE_BLOCK) {
974 if (ioctl(f->fd, BLKSSZGET, &bs) < 0) {
975 td_verror(td, errno, "ioctl");
978 } else if (f->filetype == FIO_TYPE_CHAR) {
981 if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) {
982 td_verror(td, errno, "ioctl");
986 ret = fio_sgio_read_capacity(td, &bs, &max_lba);
988 td_verror(td, td->error, "fio_sgio_read_capacity");
989 log_err("ioengine sg unable to read capacity successfully\n");
993 td_verror(td, EINVAL, "wrong file type");
994 log_err("ioengine sg only works on block or character devices\n");
999 // Determine size of commands needed based on max_lba
1000 if (max_lba >= MAX_10B_LBA) {
1001 dprint(FD_IO, "sgio_type_check: using 16 byte read/write "
1002 "commands for lba above 0x%016llx/0x%016llx\n",
1003 MAX_10B_LBA, max_lba);
1006 if (f->filetype == FIO_TYPE_BLOCK) {
1007 td->io_ops->getevents = NULL;
1008 td->io_ops->event = NULL;
1009 td->io_ops->commit = NULL;
1011 ** Setting these functions to null may cause problems
1012 ** with filename=/dev/sda:/dev/sg0 since we are only
1013 ** considering a single file
1016 sd->type_checked = 1;
1021 static int fio_sgio_open(struct thread_data *td, struct fio_file *f)
1023 struct sgio_data *sd = td->io_ops_data;
1026 ret = generic_open_file(td, f);
1030 if (sd && !sd->type_checked && fio_sgio_type_check(td, f)) {
1031 ret = generic_close_file(td, f);
1039 * Build an error string with details about the driver, host or scsi
1040 * error contained in the sg header Caller will use as necessary.
1042 static char *fio_sgio_errdetails(struct io_u *io_u)
1044 struct sg_io_hdr *hdr = &io_u->hdr;
1045 #define MAXERRDETAIL 1024
1046 #define MAXMSGCHUNK 128
1047 char *msg, msgchunk[MAXMSGCHUNK];
1050 msg = calloc(1, MAXERRDETAIL);
1054 * can't seem to find sg_err.h, so I'll just echo the define values
1055 * so others can search on internet to find clearer clues of meaning.
1057 if (hdr->info & SG_INFO_CHECK) {
1058 if (hdr->host_status) {
1059 snprintf(msgchunk, MAXMSGCHUNK, "SG Host Status: 0x%02x; ", hdr->host_status);
1060 strlcat(msg, msgchunk, MAXERRDETAIL);
1061 switch (hdr->host_status) {
1063 strlcat(msg, "SG_ERR_DID_NO_CONNECT", MAXERRDETAIL);
1066 strlcat(msg, "SG_ERR_DID_BUS_BUSY", MAXERRDETAIL);
1069 strlcat(msg, "SG_ERR_DID_TIME_OUT", MAXERRDETAIL);
1072 strlcat(msg, "SG_ERR_DID_BAD_TARGET", MAXERRDETAIL);
1075 strlcat(msg, "SG_ERR_DID_ABORT", MAXERRDETAIL);
1078 strlcat(msg, "SG_ERR_DID_PARITY", MAXERRDETAIL);
1081 strlcat(msg, "SG_ERR_DID_ERROR (internal error)", MAXERRDETAIL);
1084 strlcat(msg, "SG_ERR_DID_RESET", MAXERRDETAIL);
1087 strlcat(msg, "SG_ERR_DID_BAD_INTR (unexpected)", MAXERRDETAIL);
1090 strlcat(msg, "SG_ERR_DID_PASSTHROUGH", MAXERRDETAIL);
1093 strlcat(msg, "SG_ERR_DID_SOFT_ERROR (driver retry?)", MAXERRDETAIL);
1096 strlcat(msg, "SG_ERR_DID_IMM_RETRY", MAXERRDETAIL);
1099 strlcat(msg, "SG_ERR_DID_REQUEUE", MAXERRDETAIL);
1102 strlcat(msg, "SG_ERR_DID_TRANSPORT_DISRUPTED", MAXERRDETAIL);
1105 strlcat(msg, "SG_ERR_DID_TRANSPORT_FAILFAST", MAXERRDETAIL);
1108 strlcat(msg, "SG_ERR_DID_TARGET_FAILURE", MAXERRDETAIL);
1111 strlcat(msg, "SG_ERR_DID_NEXUS_FAILURE", MAXERRDETAIL);
1114 strlcat(msg, "SG_ERR_DID_ALLOC_FAILURE", MAXERRDETAIL);
1117 strlcat(msg, "SG_ERR_DID_MEDIUM_ERROR", MAXERRDETAIL);
1120 strlcat(msg, "Unknown", MAXERRDETAIL);
1123 strlcat(msg, ". ", MAXERRDETAIL);
1125 if (hdr->driver_status) {
1126 snprintf(msgchunk, MAXMSGCHUNK, "SG Driver Status: 0x%02x; ", hdr->driver_status);
1127 strlcat(msg, msgchunk, MAXERRDETAIL);
1128 switch (hdr->driver_status & 0x0F) {
1130 strlcat(msg, "SG_ERR_DRIVER_BUSY", MAXERRDETAIL);
1133 strlcat(msg, "SG_ERR_DRIVER_SOFT", MAXERRDETAIL);
1136 strlcat(msg, "SG_ERR_DRIVER_MEDIA", MAXERRDETAIL);
1139 strlcat(msg, "SG_ERR_DRIVER_ERROR", MAXERRDETAIL);
1142 strlcat(msg, "SG_ERR_DRIVER_INVALID", MAXERRDETAIL);
1145 strlcat(msg, "SG_ERR_DRIVER_TIMEOUT", MAXERRDETAIL);
1148 strlcat(msg, "SG_ERR_DRIVER_HARD", MAXERRDETAIL);
1151 strlcat(msg, "SG_ERR_DRIVER_SENSE", MAXERRDETAIL);
1154 strlcat(msg, "Unknown", MAXERRDETAIL);
1157 strlcat(msg, "; ", MAXERRDETAIL);
1158 switch (hdr->driver_status & 0xF0) {
1160 strlcat(msg, "SG_ERR_SUGGEST_RETRY", MAXERRDETAIL);
1163 strlcat(msg, "SG_ERR_SUGGEST_ABORT", MAXERRDETAIL);
1166 strlcat(msg, "SG_ERR_SUGGEST_REMAP", MAXERRDETAIL);
1169 strlcat(msg, "SG_ERR_SUGGEST_DIE", MAXERRDETAIL);
1172 strlcat(msg, "SG_ERR_SUGGEST_SENSE", MAXERRDETAIL);
1175 strlcat(msg, ". ", MAXERRDETAIL);
1178 snprintf(msgchunk, MAXMSGCHUNK, "SG SCSI Status: 0x%02x; ", hdr->status);
1179 strlcat(msg, msgchunk, MAXERRDETAIL);
1180 // SCSI 3 status codes
1181 switch (hdr->status) {
1183 strlcat(msg, "CHECK_CONDITION", MAXERRDETAIL);
1186 strlcat(msg, "CONDITION_MET", MAXERRDETAIL);
1189 strlcat(msg, "BUSY", MAXERRDETAIL);
1192 strlcat(msg, "INTERMEDIATE", MAXERRDETAIL);
1195 strlcat(msg, "INTERMEDIATE_CONDITION_MET", MAXERRDETAIL);
1198 strlcat(msg, "RESERVATION_CONFLICT", MAXERRDETAIL);
1201 strlcat(msg, "COMMAND_TERMINATED", MAXERRDETAIL);
1204 strlcat(msg, "TASK_SET_FULL", MAXERRDETAIL);
1207 strlcat(msg, "ACA_ACTIVE", MAXERRDETAIL);
1210 strlcat(msg, "TASK_ABORTED", MAXERRDETAIL);
1213 strlcat(msg, "Unknown", MAXERRDETAIL);
1216 strlcat(msg, ". ", MAXERRDETAIL);
1218 if (hdr->sb_len_wr) {
1219 snprintf(msgchunk, MAXMSGCHUNK, "Sense Data (%d bytes):", hdr->sb_len_wr);
1220 strlcat(msg, msgchunk, MAXERRDETAIL);
1221 for (i = 0; i < hdr->sb_len_wr; i++) {
1222 snprintf(msgchunk, MAXMSGCHUNK, " %02x", hdr->sbp[i]);
1223 strlcat(msg, msgchunk, MAXERRDETAIL);
1225 strlcat(msg, ". ", MAXERRDETAIL);
1227 if (hdr->resid != 0) {
1228 snprintf(msgchunk, MAXMSGCHUNK, "SG Driver: %d bytes out of %d not transferred. ", hdr->resid, hdr->dxfer_len);
1229 strlcat(msg, msgchunk, MAXERRDETAIL);
1232 strlcat(msg, "cdb:", MAXERRDETAIL);
1233 for (i = 0; i < hdr->cmd_len; i++) {
1234 snprintf(msgchunk, MAXMSGCHUNK, " %02x", hdr->cmdp[i]);
1235 strlcat(msg, msgchunk, MAXERRDETAIL);
1237 strlcat(msg, ". ", MAXERRDETAIL);
1238 if (io_u->ddir == DDIR_TRIM) {
1239 unsigned char *param_list = hdr->dxferp;
1240 strlcat(msg, "dxferp:", MAXERRDETAIL);
1241 for (i = 0; i < hdr->dxfer_len; i++) {
1242 snprintf(msgchunk, MAXMSGCHUNK, " %02x", param_list[i]);
1243 strlcat(msg, msgchunk, MAXERRDETAIL);
1245 strlcat(msg, ". ", MAXERRDETAIL);
1250 if (!(hdr->info & SG_INFO_CHECK) && !strlen(msg))
1251 snprintf(msg, MAXERRDETAIL, "%s",
1252 "SG Driver did not report a Host, Driver or Device check");
1258 * get max file size from read capacity.
1260 static int fio_sgio_get_file_size(struct thread_data *td, struct fio_file *f)
1263 * get_file_size is being called even before sgio_init is
1264 * called, so none of the sg_io structures are
1265 * initialized in the thread_data yet. So we need to do the
1266 * ReadCapacity without any of those helpers. One of the effects
1267 * is that ReadCapacity may get called 4 times on each open:
1268 * readcap(10) followed by readcap(16) if needed - just to get
1269 * the file size after the init occurs - it will be called
1270 * again when "type_check" is called during structure
1271 * initialization I'm not sure how to prevent this little
1274 unsigned int bs = 0;
1275 unsigned long long max_lba = 0;
1278 if (fio_file_size_known(f))
1281 if (f->filetype != FIO_TYPE_BLOCK && f->filetype != FIO_TYPE_CHAR) {
1282 td_verror(td, EINVAL, "wrong file type");
1283 log_err("ioengine sg only works on block or character devices\n");
1287 ret = fio_sgio_read_capacity(td, &bs, &max_lba);
1289 td_verror(td, td->error, "fio_sgio_read_capacity");
1290 log_err("ioengine sg unable to successfully execute read capacity to get block size and maximum lba\n");
1294 f->real_file_size = (max_lba + 1) * bs;
1295 fio_file_set_size_known(f);
1300 static struct ioengine_ops ioengine = {
1302 .version = FIO_IOOPS_VERSION,
1303 .init = fio_sgio_init,
1304 .prep = fio_sgio_prep,
1305 .queue = fio_sgio_queue,
1306 .commit = fio_sgio_commit,
1307 .getevents = fio_sgio_getevents,
1308 .errdetails = fio_sgio_errdetails,
1309 .event = fio_sgio_event,
1310 .cleanup = fio_sgio_cleanup,
1311 .open_file = fio_sgio_open,
1312 .close_file = generic_close_file,
1313 .get_file_size = fio_sgio_get_file_size,
1314 .flags = FIO_SYNCIO | FIO_RAWIO,
1316 .option_struct_size = sizeof(struct sg_options)
1319 #else /* FIO_HAVE_SGIO */
1322 * When we have a proper configure system in place, we simply wont build
1323 * and install this io engine. For now install a crippled version that
1324 * just complains and fails to load.
1326 static int fio_sgio_init(struct thread_data fio_unused *td)
1328 log_err("fio: ioengine sg not available\n");
1332 static struct ioengine_ops ioengine = {
1334 .version = FIO_IOOPS_VERSION,
1335 .init = fio_sgio_init,
1340 static void fio_init fio_sgio_register(void)
1342 register_ioengine(&ioengine);
1345 static void fio_exit fio_sgio_unregister(void)
1347 unregister_ioengine(&ioengine);