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_VERIFY_BYTCHK_00,
72 FIO_SG_VERIFY_BYTCHK_01,
73 FIO_SG_VERIFY_BYTCHK_11,
80 unsigned int writefua;
81 unsigned int write_mode;
84 static struct fio_option options[] = {
87 .lname = "High Priority",
88 .type = FIO_OPT_STR_SET,
89 .off1 = offsetof(struct sg_options, hipri),
90 .help = "Use polled IO completions",
91 .category = FIO_OPT_C_ENGINE,
92 .group = FIO_OPT_G_SG,
96 .lname = "sg engine read fua flag support",
98 .off1 = offsetof(struct sg_options, readfua),
99 .help = "Set FUA flag (force unit access) for all Read operations",
101 .category = FIO_OPT_C_ENGINE,
102 .group = FIO_OPT_G_SG,
106 .lname = "sg engine write fua flag support",
107 .type = FIO_OPT_BOOL,
108 .off1 = offsetof(struct sg_options, writefua),
109 .help = "Set FUA flag (force unit access) for all Write operations",
111 .category = FIO_OPT_C_ENGINE,
112 .group = FIO_OPT_G_SG,
115 .name = "sg_write_mode",
116 .lname = "specify sg write mode",
118 .off1 = offsetof(struct sg_options, write_mode),
119 .help = "Specify SCSI WRITE mode",
123 .oval = FIO_SG_WRITE,
124 .help = "Issue standard SCSI WRITE commands",
127 .oval = FIO_SG_WRITE_VERIFY,
128 .help = "Issue SCSI WRITE AND VERIFY commands",
131 .oval = FIO_SG_WRITE_SAME,
132 .help = "Issue SCSI WRITE SAME commands",
134 { .ival = "verify_bytchk_00",
135 .oval = FIO_SG_VERIFY_BYTCHK_00,
136 .help = "Issue SCSI VERIFY commands with BYTCHK set to 00",
138 { .ival = "verify_bytchk_01",
139 .oval = FIO_SG_VERIFY_BYTCHK_01,
140 .help = "Issue SCSI VERIFY commands with BYTCHK set to 01",
142 { .ival = "verify_bytchk_11",
143 .oval = FIO_SG_VERIFY_BYTCHK_11,
144 .help = "Issue SCSI VERIFY commands with BYTCHK set to 11",
147 .category = FIO_OPT_C_ENGINE,
148 .group = FIO_OPT_G_SG,
155 #define MAX_10B_LBA 0xFFFFFFFFULL
156 #define SCSI_TIMEOUT_MS 30000 // 30 second timeout; currently no method to override
157 #define MAX_SB 64 // sense block maximum return size
159 #define FIO_SGIO_DEBUG
163 unsigned char cdb[16]; // enhanced from 10 to support 16 byte commands
164 unsigned char sb[MAX_SB]; // add sense block to commands
169 uint8_t *unmap_param;
170 unsigned int unmap_range_count;
171 struct io_u **trim_io_us;
175 struct sgio_cmd *cmds;
176 struct io_u **events;
182 struct sgio_trim **trim_queues;
184 #ifdef FIO_SGIO_DEBUG
185 unsigned int *trim_queue_map;
189 static inline uint32_t sgio_get_be32(uint8_t *buf)
191 return be32_to_cpu(*((uint32_t *) buf));
194 static inline uint64_t sgio_get_be64(uint8_t *buf)
196 return be64_to_cpu(*((uint64_t *) buf));
199 static inline void sgio_set_be16(uint16_t val, uint8_t *buf)
201 uint16_t t = cpu_to_be16(val);
203 memcpy(buf, &t, sizeof(uint16_t));
206 static inline void sgio_set_be32(uint32_t val, uint8_t *buf)
208 uint32_t t = cpu_to_be32(val);
210 memcpy(buf, &t, sizeof(uint32_t));
213 static inline void sgio_set_be64(uint64_t val, uint8_t *buf)
215 uint64_t t = cpu_to_be64(val);
217 memcpy(buf, &t, sizeof(uint64_t));
220 static inline bool sgio_unbuffered(struct thread_data *td)
222 return (td->o.odirect || td->o.sync_io);
225 static void sgio_hdr_init(struct sgio_data *sd, struct sg_io_hdr *hdr,
226 struct io_u *io_u, int fs)
228 struct sgio_cmd *sc = &sd->cmds[io_u->index];
230 memset(hdr, 0, sizeof(*hdr));
231 memset(sc->cdb, 0, sizeof(sc->cdb));
233 hdr->interface_id = 'S';
235 hdr->cmd_len = sizeof(sc->cdb);
237 hdr->mx_sb_len = sizeof(sc->sb);
238 hdr->pack_id = io_u->index;
240 hdr->timeout = SCSI_TIMEOUT_MS;
243 hdr->dxferp = io_u->xfer_buf;
244 hdr->dxfer_len = io_u->xfer_buflen;
248 static int pollin_events(struct pollfd *pfds, int fds)
252 for (i = 0; i < fds; i++)
253 if (pfds[i].revents & POLLIN)
259 static int sg_fd_read(int fd, void *data, size_t size)
266 ret = read(fd, data, size);
268 if (errno == EAGAIN || errno == EINTR)
288 static int fio_sgio_getevents(struct thread_data *td, unsigned int min,
290 const struct timespec fio_unused *t)
292 struct sgio_data *sd = td->io_ops_data;
293 int left = max, eventNum, ret, r = 0, trims = 0;
294 void *buf = sd->sgbuf;
295 unsigned int i, j, events;
300 * Fill in the file descriptors
302 for_each_file(td, f, i) {
304 * don't block for min events == 0
307 sd->fd_flags[i] = fio_set_fd_nonblocking(f->fd, "sg");
309 sd->fd_flags[i] = -1;
311 sd->pfds[i].fd = f->fd;
312 sd->pfds[i].events = POLLIN;
316 ** There are two counters here:
317 ** - number of SCSI commands completed
318 ** - number of io_us completed
320 ** These are the same with reads and writes, but
321 ** could differ with trim/unmap commands because
322 ** a single unmap can include multiple io_us
328 dprint(FD_IO, "sgio_getevents: sd %p: min=%d, max=%d, left=%d\n", sd, min, max, left);
334 ret = poll(sd->pfds, td->o.nr_files, -1);
338 td_verror(td, errno, "poll");
343 if (pollin_events(sd->pfds, td->o.nr_files))
353 for_each_file(td, f, i) {
354 for (eventNum = 0; eventNum < left; eventNum++) {
355 ret = sg_fd_read(f->fd, p, sizeof(struct sg_io_hdr));
356 dprint(FD_IO, "sgio_getevents: sg_fd_read ret: %d\n", ret);
359 td_verror(td, r, "sg_read");
362 io_u = ((struct sg_io_hdr *)p)->usr_ptr;
363 if (io_u->ddir == DDIR_TRIM) {
364 events += sd->trim_queues[io_u->index]->unmap_range_count;
365 eventNum += sd->trim_queues[io_u->index]->unmap_range_count - 1;
369 p += sizeof(struct sg_io_hdr);
370 dprint(FD_IO, "sgio_getevents: events: %d, eventNum: %d, left: %d\n", events, eventNum, left);
374 if (r < 0 && !events)
384 for (i = 0; i < events; i++) {
385 struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i;
386 sd->events[i + trims] = hdr->usr_ptr;
387 io_u = (struct io_u *)(hdr->usr_ptr);
389 if (hdr->info & SG_INFO_CHECK) {
390 /* record if an io error occurred, ignore resid */
391 memcpy(&io_u->hdr, hdr, sizeof(struct sg_io_hdr));
392 sd->events[i + trims]->error = EIO;
395 if (io_u->ddir == DDIR_TRIM) {
396 struct sgio_trim *st = sd->trim_queues[io_u->index];
397 #ifdef FIO_SGIO_DEBUG
398 assert(st->trim_io_us[0] == io_u);
399 assert(sd->trim_queue_map[io_u->index] == io_u->index);
400 dprint(FD_IO, "sgio_getevents: reaping %d io_us from trim queue %d\n", st->unmap_range_count, io_u->index);
401 dprint(FD_IO, "sgio_getevents: reaped io_u %d and stored in events[%d]\n", io_u->index, i+trims);
403 for (j = 1; j < st->unmap_range_count; j++) {
405 sd->events[i + trims] = st->trim_io_us[j];
406 #ifdef FIO_SGIO_DEBUG
407 dprint(FD_IO, "sgio_getevents: reaped io_u %d and stored in events[%d]\n", st->trim_io_us[j]->index, i+trims);
408 assert(sd->trim_queue_map[st->trim_io_us[j]->index] == io_u->index);
410 if (hdr->info & SG_INFO_CHECK) {
411 /* record if an io error occurred, ignore resid */
412 memcpy(&st->trim_io_us[j]->hdr, hdr, sizeof(struct sg_io_hdr));
413 sd->events[i + trims]->error = EIO;
416 events -= st->unmap_range_count - 1;
417 st->unmap_range_count = 0;
423 for_each_file(td, f, i) {
424 if (sd->fd_flags[i] == -1)
427 if (fcntl(f->fd, F_SETFL, sd->fd_flags[i]) < 0)
428 log_err("fio: sg failed to restore fcntl flags: %s\n", strerror(errno));
435 static enum fio_q_status fio_sgio_ioctl_doio(struct thread_data *td,
439 struct sgio_data *sd = td->io_ops_data;
440 struct sg_io_hdr *hdr = &io_u->hdr;
443 sd->events[0] = io_u;
445 ret = ioctl(f->fd, SG_IO, hdr);
449 /* record if an io error occurred */
450 if (hdr->info & SG_INFO_CHECK)
453 return FIO_Q_COMPLETED;
456 static enum fio_q_status fio_sgio_rw_doio(struct thread_data *td,
458 struct io_u *io_u, int do_sync)
460 struct sg_io_hdr *hdr = &io_u->hdr;
463 ret = write(f->fd, hdr, sizeof(*hdr));
469 * We can't just read back the first command that completes
470 * and assume it's the one we need, it could be any command
476 ret = read(f->fd, hdr, sizeof(*hdr));
480 __io_u = hdr->usr_ptr;
482 /* record if an io error occurred */
483 if (hdr->info & SG_INFO_CHECK)
489 if (io_u_sync_complete(td, __io_u))
494 return FIO_Q_COMPLETED;
500 static enum fio_q_status fio_sgio_doio(struct thread_data *td,
501 struct io_u *io_u, int do_sync)
503 struct fio_file *f = io_u->file;
504 enum fio_q_status ret;
506 if (f->filetype == FIO_TYPE_BLOCK) {
507 ret = fio_sgio_ioctl_doio(td, f, io_u);
509 td_verror(td, io_u->error, __func__);
511 ret = fio_sgio_rw_doio(td, f, io_u, do_sync);
512 if (io_u->error && do_sync)
513 td_verror(td, io_u->error, __func__);
519 static void fio_sgio_rw_lba(struct sg_io_hdr *hdr, unsigned long long lba,
520 unsigned long long nr_blocks)
522 if (lba < MAX_10B_LBA) {
523 sgio_set_be32((uint32_t) lba, &hdr->cmdp[2]);
524 sgio_set_be16((uint16_t) nr_blocks, &hdr->cmdp[7]);
526 sgio_set_be64(lba, &hdr->cmdp[2]);
527 sgio_set_be32((uint32_t) nr_blocks, &hdr->cmdp[10]);
533 static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u)
535 struct sg_io_hdr *hdr = &io_u->hdr;
536 struct sg_options *o = td->eo;
537 struct sgio_data *sd = td->io_ops_data;
538 unsigned long long nr_blocks, lba;
541 if (io_u->xfer_buflen & (sd->bs - 1)) {
542 log_err("read/write not sector aligned\n");
546 nr_blocks = io_u->xfer_buflen / sd->bs;
547 lba = io_u->offset / sd->bs;
549 if (io_u->ddir == DDIR_READ) {
550 sgio_hdr_init(sd, hdr, io_u, 1);
552 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
553 if (lba < MAX_10B_LBA)
554 hdr->cmdp[0] = 0x28; // read(10)
556 hdr->cmdp[0] = 0x88; // read(16)
559 hdr->flags |= SGV4_FLAG_HIPRI;
561 hdr->cmdp[1] |= 0x08;
563 fio_sgio_rw_lba(hdr, lba, nr_blocks);
565 } else if (io_u->ddir == DDIR_WRITE) {
566 sgio_hdr_init(sd, hdr, io_u, 1);
568 hdr->dxfer_direction = SG_DXFER_TO_DEV;
569 switch(o->write_mode) {
571 if (lba < MAX_10B_LBA)
572 hdr->cmdp[0] = 0x2a; // write(10)
574 hdr->cmdp[0] = 0x8a; // write(16)
576 hdr->flags |= SGV4_FLAG_HIPRI;
578 hdr->cmdp[1] |= 0x08;
580 case FIO_SG_WRITE_VERIFY:
581 if (lba < MAX_10B_LBA)
582 hdr->cmdp[0] = 0x2e; // write and verify(10)
584 hdr->cmdp[0] = 0x8e; // write and verify(16)
586 // BYTCHK is disabled by virtue of the memset in sgio_hdr_init
587 case FIO_SG_WRITE_SAME:
588 hdr->dxfer_len = sd->bs;
589 if (lba < MAX_10B_LBA)
590 hdr->cmdp[0] = 0x41; // write same(10)
592 hdr->cmdp[0] = 0x93; // write same(16)
594 case FIO_SG_VERIFY_BYTCHK_00:
595 if (lba < MAX_10B_LBA)
596 hdr->cmdp[0] = 0x2f; // VERIFY(10)
598 hdr->cmdp[0] = 0x8f; // VERIFY(16)
601 case FIO_SG_VERIFY_BYTCHK_01:
602 if (lba < MAX_10B_LBA)
603 hdr->cmdp[0] = 0x2f; // VERIFY(10)
605 hdr->cmdp[0] = 0x8f; // VERIFY(16)
606 hdr->cmdp[1] |= 0x02; // BYTCHK = 01b
608 case FIO_SG_VERIFY_BYTCHK_11:
609 if (lba < MAX_10B_LBA)
610 hdr->cmdp[0] = 0x2f; // VERIFY(10)
612 hdr->cmdp[0] = 0x8f; // VERIFY(16)
613 hdr->cmdp[1] |= 0x06; // BYTCHK = 11b
614 hdr->dxfer_len = sd->bs;
618 fio_sgio_rw_lba(hdr, lba, nr_blocks);
620 } else if (io_u->ddir == DDIR_TRIM) {
621 struct sgio_trim *st;
623 if (sd->current_queue == -1) {
624 sgio_hdr_init(sd, hdr, io_u, 0);
627 hdr->dxfer_direction = SG_DXFER_TO_DEV;
628 hdr->cmdp[0] = 0x42; // unmap
629 sd->current_queue = io_u->index;
630 st = sd->trim_queues[sd->current_queue];
631 hdr->dxferp = st->unmap_param;
632 #ifdef FIO_SGIO_DEBUG
633 assert(sd->trim_queues[io_u->index]->unmap_range_count == 0);
634 dprint(FD_IO, "sg: creating new queue based on io_u %d\n", io_u->index);
638 st = sd->trim_queues[sd->current_queue];
640 dprint(FD_IO, "sg: adding io_u %d to trim queue %d\n", io_u->index, sd->current_queue);
641 st->trim_io_us[st->unmap_range_count] = io_u;
642 #ifdef FIO_SGIO_DEBUG
643 sd->trim_queue_map[io_u->index] = sd->current_queue;
646 offset = 8 + 16 * st->unmap_range_count;
647 sgio_set_be64(lba, &st->unmap_param[offset]);
648 sgio_set_be32((uint32_t) nr_blocks, &st->unmap_param[offset + 8]);
650 st->unmap_range_count++;
652 } else if (ddir_sync(io_u->ddir)) {
653 sgio_hdr_init(sd, hdr, io_u, 0);
654 hdr->dxfer_direction = SG_DXFER_NONE;
655 if (lba < MAX_10B_LBA)
656 hdr->cmdp[0] = 0x35; // synccache(10)
658 hdr->cmdp[0] = 0x91; // synccache(16)
665 static void fio_sgio_unmap_setup(struct sg_io_hdr *hdr, struct sgio_trim *st)
667 uint16_t cnt = st->unmap_range_count * 16;
669 hdr->dxfer_len = cnt + 8;
670 sgio_set_be16(cnt + 8, &hdr->cmdp[7]);
671 sgio_set_be16(cnt + 6, st->unmap_param);
672 sgio_set_be16(cnt, &st->unmap_param[2]);
677 static enum fio_q_status fio_sgio_queue(struct thread_data *td,
680 struct sg_io_hdr *hdr = &io_u->hdr;
681 struct sgio_data *sd = td->io_ops_data;
682 int ret, do_sync = 0;
684 fio_ro_check(td, io_u);
686 if (sgio_unbuffered(td) || ddir_sync(io_u->ddir))
689 if (io_u->ddir == DDIR_TRIM) {
690 if (do_sync || io_u->file->filetype == FIO_TYPE_BLOCK) {
691 struct sgio_trim *st = sd->trim_queues[sd->current_queue];
693 /* finish cdb setup for unmap because we are
694 ** doing unmap commands synchronously */
695 #ifdef FIO_SGIO_DEBUG
696 assert(st->unmap_range_count == 1);
697 assert(io_u == st->trim_io_us[0]);
701 fio_sgio_unmap_setup(hdr, st);
703 st->unmap_range_count = 0;
704 sd->current_queue = -1;
706 /* queue up trim ranges and submit in commit() */
710 ret = fio_sgio_doio(td, io_u, do_sync);
714 else if (hdr->status) {
715 io_u->resid = hdr->resid;
717 } else if (td->io_ops->commit != NULL) {
718 if (do_sync && !ddir_sync(io_u->ddir)) {
719 io_u_mark_submit(td, 1);
720 io_u_mark_complete(td, 1);
721 } else if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
722 io_u_mark_submit(td, 1);
723 io_u_queued(td, io_u);
728 td_verror(td, io_u->error, "xfer");
729 return FIO_Q_COMPLETED;
735 static int fio_sgio_commit(struct thread_data *td)
737 struct sgio_data *sd = td->io_ops_data;
738 struct sgio_trim *st;
740 struct sg_io_hdr *hdr;
745 if (sd->current_queue == -1)
748 st = sd->trim_queues[sd->current_queue];
749 io_u = st->trim_io_us[0];
752 fio_sgio_unmap_setup(hdr, st);
754 sd->current_queue = -1;
756 ret = fio_sgio_rw_doio(td, io_u->file, io_u, 0);
758 if (ret < 0 || hdr->status) {
768 for (i = 0; i < st->unmap_range_count; i++) {
769 st->trim_io_us[i]->error = error;
770 clear_io_u(td, st->trim_io_us[i]);
772 st->trim_io_us[i]->resid = hdr->resid;
775 td_verror(td, error, "xfer");
779 if (fio_fill_issue_time(td)) {
780 fio_gettime(&now, NULL);
781 for (i = 0; i < st->unmap_range_count; i++) {
782 memcpy(&st->trim_io_us[i]->issue_time, &now, sizeof(now));
783 io_u_queued(td, io_u);
786 io_u_mark_submit(td, st->unmap_range_count);
791 static struct io_u *fio_sgio_event(struct thread_data *td, int event)
793 struct sgio_data *sd = td->io_ops_data;
795 return sd->events[event];
798 static int fio_sgio_read_capacity(struct thread_data *td, unsigned int *bs,
799 unsigned long long *max_lba)
802 * need to do read capacity operation w/o benefit of sd or
803 * io_u structures, which are not initialized until later.
805 struct sg_io_hdr hdr;
806 unsigned long long hlba;
807 unsigned int blksz = 0;
808 unsigned char cmd[16];
809 unsigned char sb[64];
810 unsigned char buf[32]; // read capacity return
814 struct fio_file *f = td->files[0];
816 /* open file independent of rest of application */
817 fd = open(f->file_name, O_RDONLY);
821 memset(&hdr, 0, sizeof(hdr));
822 memset(cmd, 0, sizeof(cmd));
823 memset(sb, 0, sizeof(sb));
824 memset(buf, 0, sizeof(buf));
826 /* First let's try a 10 byte read capacity. */
827 hdr.interface_id = 'S';
831 hdr.mx_sb_len = sizeof(sb);
832 hdr.timeout = SCSI_TIMEOUT_MS;
833 hdr.cmdp[0] = 0x25; // Read Capacity(10)
834 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
836 hdr.dxfer_len = sizeof(buf);
838 ret = ioctl(fd, SG_IO, &hdr);
844 if (hdr.info & SG_INFO_CHECK) {
845 /* RCAP(10) might be unsupported by device. Force RCAP(16) */
848 blksz = sgio_get_be32(&buf[4]);
849 hlba = sgio_get_be32(buf);
853 * If max lba masked by MAX_10B_LBA equals MAX_10B_LBA,
854 * then need to retry with 16 byte Read Capacity command.
856 if (hlba == MAX_10B_LBA) {
858 hdr.cmdp[0] = 0x9e; // service action
859 hdr.cmdp[1] = 0x10; // Read Capacity(16)
860 sgio_set_be32(sizeof(buf), &hdr.cmdp[10]);
862 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
864 hdr.dxfer_len = sizeof(buf);
866 ret = ioctl(fd, SG_IO, &hdr);
872 /* record if an io error occurred */
873 if (hdr.info & SG_INFO_CHECK)
874 td_verror(td, EIO, "fio_sgio_read_capacity");
876 blksz = sgio_get_be32(&buf[8]);
877 hlba = sgio_get_be64(buf);
892 static void fio_sgio_cleanup(struct thread_data *td)
894 struct sgio_data *sd = td->io_ops_data;
903 #ifdef FIO_SGIO_DEBUG
904 free(sd->trim_queue_map);
907 for (i = 0; i < td->o.iodepth; i++) {
908 free(sd->trim_queues[i]->unmap_param);
909 free(sd->trim_queues[i]->trim_io_us);
910 free(sd->trim_queues[i]);
913 free(sd->trim_queues);
918 static int fio_sgio_init(struct thread_data *td)
920 struct sgio_data *sd;
921 struct sgio_trim *st;
922 struct sg_io_hdr *h3p;
925 sd = calloc(1, sizeof(*sd));
926 sd->cmds = calloc(td->o.iodepth, sizeof(struct sgio_cmd));
927 sd->sgbuf = calloc(td->o.iodepth, sizeof(struct sg_io_hdr));
928 sd->events = calloc(td->o.iodepth, sizeof(struct io_u *));
929 sd->pfds = calloc(td->o.nr_files, sizeof(struct pollfd));
930 sd->fd_flags = calloc(td->o.nr_files, sizeof(int));
931 sd->type_checked = 0;
933 sd->trim_queues = calloc(td->o.iodepth, sizeof(struct sgio_trim *));
934 sd->current_queue = -1;
935 #ifdef FIO_SGIO_DEBUG
936 sd->trim_queue_map = calloc(td->o.iodepth, sizeof(int));
938 for (i = 0, h3p = sd->sgbuf; i < td->o.iodepth; i++, ++h3p) {
939 sd->trim_queues[i] = calloc(1, sizeof(struct sgio_trim));
940 st = sd->trim_queues[i];
941 st->unmap_param = calloc(td->o.iodepth + 1, sizeof(char[16]));
942 st->unmap_range_count = 0;
943 st->trim_io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
944 h3p->interface_id = 'S';
947 td->io_ops_data = sd;
950 * we want to do it, regardless of whether odirect is set or not
952 td->o.override_sync = 1;
956 static int fio_sgio_type_check(struct thread_data *td, struct fio_file *f)
958 struct sgio_data *sd = td->io_ops_data;
960 unsigned long long max_lba = 0;
962 if (f->filetype == FIO_TYPE_BLOCK) {
963 if (ioctl(f->fd, BLKSSZGET, &bs) < 0) {
964 td_verror(td, errno, "ioctl");
967 } else if (f->filetype == FIO_TYPE_CHAR) {
970 if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) {
971 td_verror(td, errno, "ioctl");
975 ret = fio_sgio_read_capacity(td, &bs, &max_lba);
977 td_verror(td, td->error, "fio_sgio_read_capacity");
978 log_err("ioengine sg unable to read capacity successfully\n");
982 td_verror(td, EINVAL, "wrong file type");
983 log_err("ioengine sg only works on block or character devices\n");
988 // Determine size of commands needed based on max_lba
989 if (max_lba >= MAX_10B_LBA) {
990 dprint(FD_IO, "sgio_type_check: using 16 byte read/write "
991 "commands for lba above 0x%016llx/0x%016llx\n",
992 MAX_10B_LBA, max_lba);
995 if (f->filetype == FIO_TYPE_BLOCK) {
996 td->io_ops->getevents = NULL;
997 td->io_ops->event = NULL;
998 td->io_ops->commit = NULL;
1000 ** Setting these functions to null may cause problems
1001 ** with filename=/dev/sda:/dev/sg0 since we are only
1002 ** considering a single file
1005 sd->type_checked = 1;
1010 static int fio_sgio_open(struct thread_data *td, struct fio_file *f)
1012 struct sgio_data *sd = td->io_ops_data;
1015 ret = generic_open_file(td, f);
1019 if (sd && !sd->type_checked && fio_sgio_type_check(td, f)) {
1020 ret = generic_close_file(td, f);
1028 * Build an error string with details about the driver, host or scsi
1029 * error contained in the sg header Caller will use as necessary.
1031 static char *fio_sgio_errdetails(struct io_u *io_u)
1033 struct sg_io_hdr *hdr = &io_u->hdr;
1034 #define MAXERRDETAIL 1024
1035 #define MAXMSGCHUNK 128
1036 char *msg, msgchunk[MAXMSGCHUNK];
1039 msg = calloc(1, MAXERRDETAIL);
1043 * can't seem to find sg_err.h, so I'll just echo the define values
1044 * so others can search on internet to find clearer clues of meaning.
1046 if (hdr->info & SG_INFO_CHECK) {
1047 if (hdr->host_status) {
1048 snprintf(msgchunk, MAXMSGCHUNK, "SG Host Status: 0x%02x; ", hdr->host_status);
1049 strlcat(msg, msgchunk, MAXERRDETAIL);
1050 switch (hdr->host_status) {
1052 strlcat(msg, "SG_ERR_DID_NO_CONNECT", MAXERRDETAIL);
1055 strlcat(msg, "SG_ERR_DID_BUS_BUSY", MAXERRDETAIL);
1058 strlcat(msg, "SG_ERR_DID_TIME_OUT", MAXERRDETAIL);
1061 strlcat(msg, "SG_ERR_DID_BAD_TARGET", MAXERRDETAIL);
1064 strlcat(msg, "SG_ERR_DID_ABORT", MAXERRDETAIL);
1067 strlcat(msg, "SG_ERR_DID_PARITY", MAXERRDETAIL);
1070 strlcat(msg, "SG_ERR_DID_ERROR (internal error)", MAXERRDETAIL);
1073 strlcat(msg, "SG_ERR_DID_RESET", MAXERRDETAIL);
1076 strlcat(msg, "SG_ERR_DID_BAD_INTR (unexpected)", MAXERRDETAIL);
1079 strlcat(msg, "SG_ERR_DID_PASSTHROUGH", MAXERRDETAIL);
1082 strlcat(msg, "SG_ERR_DID_SOFT_ERROR (driver retry?)", MAXERRDETAIL);
1085 strlcat(msg, "SG_ERR_DID_IMM_RETRY", MAXERRDETAIL);
1088 strlcat(msg, "SG_ERR_DID_REQUEUE", MAXERRDETAIL);
1091 strlcat(msg, "SG_ERR_DID_TRANSPORT_DISRUPTED", MAXERRDETAIL);
1094 strlcat(msg, "SG_ERR_DID_TRANSPORT_FAILFAST", MAXERRDETAIL);
1097 strlcat(msg, "SG_ERR_DID_TARGET_FAILURE", MAXERRDETAIL);
1100 strlcat(msg, "SG_ERR_DID_NEXUS_FAILURE", MAXERRDETAIL);
1103 strlcat(msg, "SG_ERR_DID_ALLOC_FAILURE", MAXERRDETAIL);
1106 strlcat(msg, "SG_ERR_DID_MEDIUM_ERROR", MAXERRDETAIL);
1109 strlcat(msg, "Unknown", MAXERRDETAIL);
1112 strlcat(msg, ". ", MAXERRDETAIL);
1114 if (hdr->driver_status) {
1115 snprintf(msgchunk, MAXMSGCHUNK, "SG Driver Status: 0x%02x; ", hdr->driver_status);
1116 strlcat(msg, msgchunk, MAXERRDETAIL);
1117 switch (hdr->driver_status & 0x0F) {
1119 strlcat(msg, "SG_ERR_DRIVER_BUSY", MAXERRDETAIL);
1122 strlcat(msg, "SG_ERR_DRIVER_SOFT", MAXERRDETAIL);
1125 strlcat(msg, "SG_ERR_DRIVER_MEDIA", MAXERRDETAIL);
1128 strlcat(msg, "SG_ERR_DRIVER_ERROR", MAXERRDETAIL);
1131 strlcat(msg, "SG_ERR_DRIVER_INVALID", MAXERRDETAIL);
1134 strlcat(msg, "SG_ERR_DRIVER_TIMEOUT", MAXERRDETAIL);
1137 strlcat(msg, "SG_ERR_DRIVER_HARD", MAXERRDETAIL);
1140 strlcat(msg, "SG_ERR_DRIVER_SENSE", MAXERRDETAIL);
1143 strlcat(msg, "Unknown", MAXERRDETAIL);
1146 strlcat(msg, "; ", MAXERRDETAIL);
1147 switch (hdr->driver_status & 0xF0) {
1149 strlcat(msg, "SG_ERR_SUGGEST_RETRY", MAXERRDETAIL);
1152 strlcat(msg, "SG_ERR_SUGGEST_ABORT", MAXERRDETAIL);
1155 strlcat(msg, "SG_ERR_SUGGEST_REMAP", MAXERRDETAIL);
1158 strlcat(msg, "SG_ERR_SUGGEST_DIE", MAXERRDETAIL);
1161 strlcat(msg, "SG_ERR_SUGGEST_SENSE", MAXERRDETAIL);
1164 strlcat(msg, ". ", MAXERRDETAIL);
1167 snprintf(msgchunk, MAXMSGCHUNK, "SG SCSI Status: 0x%02x; ", hdr->status);
1168 strlcat(msg, msgchunk, MAXERRDETAIL);
1169 // SCSI 3 status codes
1170 switch (hdr->status) {
1172 strlcat(msg, "CHECK_CONDITION", MAXERRDETAIL);
1175 strlcat(msg, "CONDITION_MET", MAXERRDETAIL);
1178 strlcat(msg, "BUSY", MAXERRDETAIL);
1181 strlcat(msg, "INTERMEDIATE", MAXERRDETAIL);
1184 strlcat(msg, "INTERMEDIATE_CONDITION_MET", MAXERRDETAIL);
1187 strlcat(msg, "RESERVATION_CONFLICT", MAXERRDETAIL);
1190 strlcat(msg, "COMMAND_TERMINATED", MAXERRDETAIL);
1193 strlcat(msg, "TASK_SET_FULL", MAXERRDETAIL);
1196 strlcat(msg, "ACA_ACTIVE", MAXERRDETAIL);
1199 strlcat(msg, "TASK_ABORTED", MAXERRDETAIL);
1202 strlcat(msg, "Unknown", MAXERRDETAIL);
1205 strlcat(msg, ". ", MAXERRDETAIL);
1207 if (hdr->sb_len_wr) {
1208 snprintf(msgchunk, MAXMSGCHUNK, "Sense Data (%d bytes):", hdr->sb_len_wr);
1209 strlcat(msg, msgchunk, MAXERRDETAIL);
1210 for (i = 0; i < hdr->sb_len_wr; i++) {
1211 snprintf(msgchunk, MAXMSGCHUNK, " %02x", hdr->sbp[i]);
1212 strlcat(msg, msgchunk, MAXERRDETAIL);
1214 strlcat(msg, ". ", MAXERRDETAIL);
1216 if (hdr->resid != 0) {
1217 snprintf(msgchunk, MAXMSGCHUNK, "SG Driver: %d bytes out of %d not transferred. ", hdr->resid, hdr->dxfer_len);
1218 strlcat(msg, msgchunk, MAXERRDETAIL);
1221 strlcat(msg, "cdb:", MAXERRDETAIL);
1222 for (i = 0; i < hdr->cmd_len; i++) {
1223 snprintf(msgchunk, MAXMSGCHUNK, " %02x", hdr->cmdp[i]);
1224 strlcat(msg, msgchunk, MAXERRDETAIL);
1226 strlcat(msg, ". ", MAXERRDETAIL);
1227 if (io_u->ddir == DDIR_TRIM) {
1228 unsigned char *param_list = hdr->dxferp;
1229 strlcat(msg, "dxferp:", MAXERRDETAIL);
1230 for (i = 0; i < hdr->dxfer_len; i++) {
1231 snprintf(msgchunk, MAXMSGCHUNK, " %02x", param_list[i]);
1232 strlcat(msg, msgchunk, MAXERRDETAIL);
1234 strlcat(msg, ". ", MAXERRDETAIL);
1239 if (!(hdr->info & SG_INFO_CHECK) && !strlen(msg))
1240 snprintf(msg, MAXERRDETAIL, "%s",
1241 "SG Driver did not report a Host, Driver or Device check");
1247 * get max file size from read capacity.
1249 static int fio_sgio_get_file_size(struct thread_data *td, struct fio_file *f)
1252 * get_file_size is being called even before sgio_init is
1253 * called, so none of the sg_io structures are
1254 * initialized in the thread_data yet. So we need to do the
1255 * ReadCapacity without any of those helpers. One of the effects
1256 * is that ReadCapacity may get called 4 times on each open:
1257 * readcap(10) followed by readcap(16) if needed - just to get
1258 * the file size after the init occurs - it will be called
1259 * again when "type_check" is called during structure
1260 * initialization I'm not sure how to prevent this little
1263 unsigned int bs = 0;
1264 unsigned long long max_lba = 0;
1267 if (fio_file_size_known(f))
1270 if (f->filetype != FIO_TYPE_BLOCK && f->filetype != FIO_TYPE_CHAR) {
1271 td_verror(td, EINVAL, "wrong file type");
1272 log_err("ioengine sg only works on block or character devices\n");
1276 ret = fio_sgio_read_capacity(td, &bs, &max_lba);
1278 td_verror(td, td->error, "fio_sgio_read_capacity");
1279 log_err("ioengine sg unable to successfully execute read capacity to get block size and maximum lba\n");
1283 f->real_file_size = (max_lba + 1) * bs;
1284 fio_file_set_size_known(f);
1289 static struct ioengine_ops ioengine = {
1291 .version = FIO_IOOPS_VERSION,
1292 .init = fio_sgio_init,
1293 .prep = fio_sgio_prep,
1294 .queue = fio_sgio_queue,
1295 .commit = fio_sgio_commit,
1296 .getevents = fio_sgio_getevents,
1297 .errdetails = fio_sgio_errdetails,
1298 .event = fio_sgio_event,
1299 .cleanup = fio_sgio_cleanup,
1300 .open_file = fio_sgio_open,
1301 .close_file = generic_close_file,
1302 .get_file_size = fio_sgio_get_file_size,
1303 .flags = FIO_SYNCIO | FIO_RAWIO,
1305 .option_struct_size = sizeof(struct sg_options)
1308 #else /* FIO_HAVE_SGIO */
1311 * When we have a proper configure system in place, we simply wont build
1312 * and install this io engine. For now install a crippled version that
1313 * just complains and fails to load.
1315 static int fio_sgio_init(struct thread_data fio_unused *td)
1317 log_err("fio: ioengine sg not available\n");
1321 static struct ioengine_ops ioengine = {
1323 .version = FIO_IOOPS_VERSION,
1324 .init = fio_sgio_init,
1329 static void fio_init fio_sgio_register(void)
1331 register_ioengine(&ioengine);
1334 static void fio_exit fio_sgio_unregister(void)
1336 unregister_ioengine(&ioengine);