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
69 FIO_SG_WRITE_VERIFY = 2,
77 unsigned int writefua;
78 unsigned int write_mode;
81 static struct fio_option options[] = {
84 .lname = "High Priority",
85 .type = FIO_OPT_STR_SET,
86 .off1 = offsetof(struct sg_options, hipri),
87 .help = "Use polled IO completions",
88 .category = FIO_OPT_C_ENGINE,
89 .group = FIO_OPT_G_SG,
93 .lname = "sg engine read fua flag support",
95 .off1 = offsetof(struct sg_options, readfua),
96 .help = "Set FUA flag (force unit access) for all Read operations",
98 .category = FIO_OPT_C_ENGINE,
99 .group = FIO_OPT_G_SG,
103 .lname = "sg engine write fua flag support",
104 .type = FIO_OPT_BOOL,
105 .off1 = offsetof(struct sg_options, writefua),
106 .help = "Set FUA flag (force unit access) for all Write operations",
108 .category = FIO_OPT_C_ENGINE,
109 .group = FIO_OPT_G_SG,
112 .name = "sg_write_mode",
113 .lname = "specify sg write mode",
115 .off1 = offsetof(struct sg_options, write_mode),
116 .help = "Specify SCSI WRITE mode",
120 .oval = FIO_SG_WRITE,
121 .help = "Issue standard SCSI WRITE commands",
124 .oval = FIO_SG_WRITE_VERIFY,
125 .help = "Issue SCSI WRITE AND VERIFY commands",
128 .oval = FIO_SG_WRITE_SAME,
129 .help = "Issue SCSI WRITE SAME commands",
132 .category = FIO_OPT_C_ENGINE,
133 .group = FIO_OPT_G_SG,
140 #define MAX_10B_LBA 0xFFFFFFFFULL
141 #define SCSI_TIMEOUT_MS 30000 // 30 second timeout; currently no method to override
142 #define MAX_SB 64 // sense block maximum return size
144 #define FIO_SGIO_DEBUG
148 unsigned char cdb[16]; // enhanced from 10 to support 16 byte commands
149 unsigned char sb[MAX_SB]; // add sense block to commands
154 uint8_t *unmap_param;
155 unsigned int unmap_range_count;
156 struct io_u **trim_io_us;
160 struct sgio_cmd *cmds;
161 struct io_u **events;
167 struct sgio_trim **trim_queues;
169 #ifdef FIO_SGIO_DEBUG
170 unsigned int *trim_queue_map;
174 static inline uint32_t sgio_get_be32(uint8_t *buf)
176 return be32_to_cpu(*((uint32_t *) buf));
179 static inline uint64_t sgio_get_be64(uint8_t *buf)
181 return be64_to_cpu(*((uint64_t *) buf));
184 static inline void sgio_set_be16(uint16_t val, uint8_t *buf)
186 uint16_t t = cpu_to_be16(val);
188 memcpy(buf, &t, sizeof(uint16_t));
191 static inline void sgio_set_be32(uint32_t val, uint8_t *buf)
193 uint32_t t = cpu_to_be32(val);
195 memcpy(buf, &t, sizeof(uint32_t));
198 static inline void sgio_set_be64(uint64_t val, uint8_t *buf)
200 uint64_t t = cpu_to_be64(val);
202 memcpy(buf, &t, sizeof(uint64_t));
205 static inline bool sgio_unbuffered(struct thread_data *td)
207 return (td->o.odirect || td->o.sync_io);
210 static void sgio_hdr_init(struct sgio_data *sd, struct sg_io_hdr *hdr,
211 struct io_u *io_u, int fs)
213 struct sgio_cmd *sc = &sd->cmds[io_u->index];
215 memset(hdr, 0, sizeof(*hdr));
216 memset(sc->cdb, 0, sizeof(sc->cdb));
218 hdr->interface_id = 'S';
220 hdr->cmd_len = sizeof(sc->cdb);
222 hdr->mx_sb_len = sizeof(sc->sb);
223 hdr->pack_id = io_u->index;
225 hdr->timeout = SCSI_TIMEOUT_MS;
228 hdr->dxferp = io_u->xfer_buf;
229 hdr->dxfer_len = io_u->xfer_buflen;
233 static int pollin_events(struct pollfd *pfds, int fds)
237 for (i = 0; i < fds; i++)
238 if (pfds[i].revents & POLLIN)
244 static int sg_fd_read(int fd, void *data, size_t size)
251 ret = read(fd, data, size);
253 if (errno == EAGAIN || errno == EINTR)
273 static int fio_sgio_getevents(struct thread_data *td, unsigned int min,
275 const struct timespec fio_unused *t)
277 struct sgio_data *sd = td->io_ops_data;
278 int left = max, eventNum, ret, r = 0, trims = 0;
279 void *buf = sd->sgbuf;
280 unsigned int i, j, events;
285 * Fill in the file descriptors
287 for_each_file(td, f, i) {
289 * don't block for min events == 0
292 sd->fd_flags[i] = fio_set_fd_nonblocking(f->fd, "sg");
294 sd->fd_flags[i] = -1;
296 sd->pfds[i].fd = f->fd;
297 sd->pfds[i].events = POLLIN;
301 ** There are two counters here:
302 ** - number of SCSI commands completed
303 ** - number of io_us completed
305 ** These are the same with reads and writes, but
306 ** could differ with trim/unmap commands because
307 ** a single unmap can include multiple io_us
313 dprint(FD_IO, "sgio_getevents: sd %p: min=%d, max=%d, left=%d\n", sd, min, max, left);
319 ret = poll(sd->pfds, td->o.nr_files, -1);
323 td_verror(td, errno, "poll");
328 if (pollin_events(sd->pfds, td->o.nr_files))
338 for_each_file(td, f, i) {
339 for (eventNum = 0; eventNum < left; eventNum++) {
340 ret = sg_fd_read(f->fd, p, sizeof(struct sg_io_hdr));
341 dprint(FD_IO, "sgio_getevents: sg_fd_read ret: %d\n", ret);
344 td_verror(td, r, "sg_read");
347 io_u = ((struct sg_io_hdr *)p)->usr_ptr;
348 if (io_u->ddir == DDIR_TRIM) {
349 events += sd->trim_queues[io_u->index]->unmap_range_count;
350 eventNum += sd->trim_queues[io_u->index]->unmap_range_count - 1;
354 p += sizeof(struct sg_io_hdr);
355 dprint(FD_IO, "sgio_getevents: events: %d, eventNum: %d, left: %d\n", events, eventNum, left);
359 if (r < 0 && !events)
369 for (i = 0; i < events; i++) {
370 struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i;
371 sd->events[i + trims] = hdr->usr_ptr;
372 io_u = (struct io_u *)(hdr->usr_ptr);
374 if (hdr->info & SG_INFO_CHECK) {
375 /* record if an io error occurred, ignore resid */
376 memcpy(&io_u->hdr, hdr, sizeof(struct sg_io_hdr));
377 sd->events[i + trims]->error = EIO;
380 if (io_u->ddir == DDIR_TRIM) {
381 struct sgio_trim *st = sd->trim_queues[io_u->index];
382 #ifdef FIO_SGIO_DEBUG
383 assert(st->trim_io_us[0] == io_u);
384 assert(sd->trim_queue_map[io_u->index] == io_u->index);
385 dprint(FD_IO, "sgio_getevents: reaping %d io_us from trim queue %d\n", st->unmap_range_count, io_u->index);
386 dprint(FD_IO, "sgio_getevents: reaped io_u %d and stored in events[%d]\n", io_u->index, i+trims);
388 for (j = 1; j < st->unmap_range_count; j++) {
390 sd->events[i + trims] = st->trim_io_us[j];
391 #ifdef FIO_SGIO_DEBUG
392 dprint(FD_IO, "sgio_getevents: reaped io_u %d and stored in events[%d]\n", st->trim_io_us[j]->index, i+trims);
393 assert(sd->trim_queue_map[st->trim_io_us[j]->index] == io_u->index);
395 if (hdr->info & SG_INFO_CHECK) {
396 /* record if an io error occurred, ignore resid */
397 memcpy(&st->trim_io_us[j]->hdr, hdr, sizeof(struct sg_io_hdr));
398 sd->events[i + trims]->error = EIO;
401 events -= st->unmap_range_count - 1;
402 st->unmap_range_count = 0;
408 for_each_file(td, f, i) {
409 if (sd->fd_flags[i] == -1)
412 if (fcntl(f->fd, F_SETFL, sd->fd_flags[i]) < 0)
413 log_err("fio: sg failed to restore fcntl flags: %s\n", strerror(errno));
420 static enum fio_q_status fio_sgio_ioctl_doio(struct thread_data *td,
424 struct sgio_data *sd = td->io_ops_data;
425 struct sg_io_hdr *hdr = &io_u->hdr;
428 sd->events[0] = io_u;
430 ret = ioctl(f->fd, SG_IO, hdr);
434 /* record if an io error occurred */
435 if (hdr->info & SG_INFO_CHECK)
438 return FIO_Q_COMPLETED;
441 static enum fio_q_status fio_sgio_rw_doio(struct thread_data *td,
443 struct io_u *io_u, int do_sync)
445 struct sg_io_hdr *hdr = &io_u->hdr;
448 ret = write(f->fd, hdr, sizeof(*hdr));
454 * We can't just read back the first command that completes
455 * and assume it's the one we need, it could be any command
461 ret = read(f->fd, hdr, sizeof(*hdr));
465 __io_u = hdr->usr_ptr;
467 /* record if an io error occurred */
468 if (hdr->info & SG_INFO_CHECK)
474 if (io_u_sync_complete(td, __io_u))
479 return FIO_Q_COMPLETED;
485 static enum fio_q_status fio_sgio_doio(struct thread_data *td,
486 struct io_u *io_u, int do_sync)
488 struct fio_file *f = io_u->file;
489 enum fio_q_status ret;
491 if (f->filetype == FIO_TYPE_BLOCK) {
492 ret = fio_sgio_ioctl_doio(td, f, io_u);
494 td_verror(td, io_u->error, __func__);
496 ret = fio_sgio_rw_doio(td, f, io_u, do_sync);
497 if (io_u->error && do_sync)
498 td_verror(td, io_u->error, __func__);
504 static void fio_sgio_rw_lba(struct sg_io_hdr *hdr, unsigned long long lba,
505 unsigned long long nr_blocks)
507 if (lba < MAX_10B_LBA) {
508 sgio_set_be32((uint32_t) lba, &hdr->cmdp[2]);
509 sgio_set_be16((uint16_t) nr_blocks, &hdr->cmdp[7]);
511 sgio_set_be64(lba, &hdr->cmdp[2]);
512 sgio_set_be32((uint32_t) nr_blocks, &hdr->cmdp[10]);
518 static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u)
520 struct sg_io_hdr *hdr = &io_u->hdr;
521 struct sg_options *o = td->eo;
522 struct sgio_data *sd = td->io_ops_data;
523 unsigned long long nr_blocks, lba;
526 if (io_u->xfer_buflen & (sd->bs - 1)) {
527 log_err("read/write not sector aligned\n");
531 nr_blocks = io_u->xfer_buflen / sd->bs;
532 lba = io_u->offset / sd->bs;
534 if (io_u->ddir == DDIR_READ) {
535 sgio_hdr_init(sd, hdr, io_u, 1);
537 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
538 if (lba < MAX_10B_LBA)
539 hdr->cmdp[0] = 0x28; // read(10)
541 hdr->cmdp[0] = 0x88; // read(16)
544 hdr->flags |= SGV4_FLAG_HIPRI;
546 hdr->cmdp[1] |= 0x08;
548 fio_sgio_rw_lba(hdr, lba, nr_blocks);
550 } else if (io_u->ddir == DDIR_WRITE) {
551 sgio_hdr_init(sd, hdr, io_u, 1);
553 hdr->dxfer_direction = SG_DXFER_TO_DEV;
554 switch(o->write_mode) {
556 if (lba < MAX_10B_LBA)
557 hdr->cmdp[0] = 0x2a; // write(10)
559 hdr->cmdp[0] = 0x8a; // write(16)
561 hdr->flags |= SGV4_FLAG_HIPRI;
563 hdr->cmdp[1] |= 0x08;
565 case FIO_SG_WRITE_VERIFY:
566 if (lba < MAX_10B_LBA)
567 hdr->cmdp[0] = 0x2e; // write and verify(10)
569 hdr->cmdp[0] = 0x8e; // write and verify(16)
571 // BYTCHK is disabled by virtue of the memset in sgio_hdr_init
572 case FIO_SG_WRITE_SAME:
573 hdr->dxfer_len = sd->bs;
574 if (lba < MAX_10B_LBA)
575 hdr->cmdp[0] = 0x41; // write same(10)
577 hdr->cmdp[0] = 0x93; // write same(16)
581 fio_sgio_rw_lba(hdr, lba, nr_blocks);
583 } else if (io_u->ddir == DDIR_TRIM) {
584 struct sgio_trim *st;
586 if (sd->current_queue == -1) {
587 sgio_hdr_init(sd, hdr, io_u, 0);
590 hdr->dxfer_direction = SG_DXFER_TO_DEV;
591 hdr->cmdp[0] = 0x42; // unmap
592 sd->current_queue = io_u->index;
593 st = sd->trim_queues[sd->current_queue];
594 hdr->dxferp = st->unmap_param;
595 #ifdef FIO_SGIO_DEBUG
596 assert(sd->trim_queues[io_u->index]->unmap_range_count == 0);
597 dprint(FD_IO, "sg: creating new queue based on io_u %d\n", io_u->index);
601 st = sd->trim_queues[sd->current_queue];
603 dprint(FD_IO, "sg: adding io_u %d to trim queue %d\n", io_u->index, sd->current_queue);
604 st->trim_io_us[st->unmap_range_count] = io_u;
605 #ifdef FIO_SGIO_DEBUG
606 sd->trim_queue_map[io_u->index] = sd->current_queue;
609 offset = 8 + 16 * st->unmap_range_count;
610 sgio_set_be64(lba, &st->unmap_param[offset]);
611 sgio_set_be32((uint32_t) nr_blocks, &st->unmap_param[offset + 8]);
613 st->unmap_range_count++;
615 } else if (ddir_sync(io_u->ddir)) {
616 sgio_hdr_init(sd, hdr, io_u, 0);
617 hdr->dxfer_direction = SG_DXFER_NONE;
618 if (lba < MAX_10B_LBA)
619 hdr->cmdp[0] = 0x35; // synccache(10)
621 hdr->cmdp[0] = 0x91; // synccache(16)
628 static void fio_sgio_unmap_setup(struct sg_io_hdr *hdr, struct sgio_trim *st)
630 uint16_t cnt = st->unmap_range_count * 16;
632 hdr->dxfer_len = cnt + 8;
633 sgio_set_be16(cnt + 8, &hdr->cmdp[7]);
634 sgio_set_be16(cnt + 6, st->unmap_param);
635 sgio_set_be16(cnt, &st->unmap_param[2]);
640 static enum fio_q_status fio_sgio_queue(struct thread_data *td,
643 struct sg_io_hdr *hdr = &io_u->hdr;
644 struct sgio_data *sd = td->io_ops_data;
645 int ret, do_sync = 0;
647 fio_ro_check(td, io_u);
649 if (sgio_unbuffered(td) || ddir_sync(io_u->ddir))
652 if (io_u->ddir == DDIR_TRIM) {
653 if (do_sync || io_u->file->filetype == FIO_TYPE_BLOCK) {
654 struct sgio_trim *st = sd->trim_queues[sd->current_queue];
656 /* finish cdb setup for unmap because we are
657 ** doing unmap commands synchronously */
658 #ifdef FIO_SGIO_DEBUG
659 assert(st->unmap_range_count == 1);
660 assert(io_u == st->trim_io_us[0]);
664 fio_sgio_unmap_setup(hdr, st);
666 st->unmap_range_count = 0;
667 sd->current_queue = -1;
669 /* queue up trim ranges and submit in commit() */
673 ret = fio_sgio_doio(td, io_u, do_sync);
677 else if (hdr->status) {
678 io_u->resid = hdr->resid;
680 } else if (td->io_ops->commit != NULL) {
681 if (do_sync && !ddir_sync(io_u->ddir)) {
682 io_u_mark_submit(td, 1);
683 io_u_mark_complete(td, 1);
684 } else if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
685 io_u_mark_submit(td, 1);
686 io_u_queued(td, io_u);
691 td_verror(td, io_u->error, "xfer");
692 return FIO_Q_COMPLETED;
698 static int fio_sgio_commit(struct thread_data *td)
700 struct sgio_data *sd = td->io_ops_data;
701 struct sgio_trim *st;
703 struct sg_io_hdr *hdr;
708 if (sd->current_queue == -1)
711 st = sd->trim_queues[sd->current_queue];
712 io_u = st->trim_io_us[0];
715 fio_sgio_unmap_setup(hdr, st);
717 sd->current_queue = -1;
719 ret = fio_sgio_rw_doio(td, io_u->file, io_u, 0);
721 if (ret < 0 || hdr->status) {
731 for (i = 0; i < st->unmap_range_count; i++) {
732 st->trim_io_us[i]->error = error;
733 clear_io_u(td, st->trim_io_us[i]);
735 st->trim_io_us[i]->resid = hdr->resid;
738 td_verror(td, error, "xfer");
742 if (fio_fill_issue_time(td)) {
743 fio_gettime(&now, NULL);
744 for (i = 0; i < st->unmap_range_count; i++) {
745 memcpy(&st->trim_io_us[i]->issue_time, &now, sizeof(now));
746 io_u_queued(td, io_u);
749 io_u_mark_submit(td, st->unmap_range_count);
754 static struct io_u *fio_sgio_event(struct thread_data *td, int event)
756 struct sgio_data *sd = td->io_ops_data;
758 return sd->events[event];
761 static int fio_sgio_read_capacity(struct thread_data *td, unsigned int *bs,
762 unsigned long long *max_lba)
765 * need to do read capacity operation w/o benefit of sd or
766 * io_u structures, which are not initialized until later.
768 struct sg_io_hdr hdr;
769 unsigned long long hlba;
770 unsigned int blksz = 0;
771 unsigned char cmd[16];
772 unsigned char sb[64];
773 unsigned char buf[32]; // read capacity return
777 struct fio_file *f = td->files[0];
779 /* open file independent of rest of application */
780 fd = open(f->file_name, O_RDONLY);
784 memset(&hdr, 0, sizeof(hdr));
785 memset(cmd, 0, sizeof(cmd));
786 memset(sb, 0, sizeof(sb));
787 memset(buf, 0, sizeof(buf));
789 /* First let's try a 10 byte read capacity. */
790 hdr.interface_id = 'S';
794 hdr.mx_sb_len = sizeof(sb);
795 hdr.timeout = SCSI_TIMEOUT_MS;
796 hdr.cmdp[0] = 0x25; // Read Capacity(10)
797 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
799 hdr.dxfer_len = sizeof(buf);
801 ret = ioctl(fd, SG_IO, &hdr);
807 if (hdr.info & SG_INFO_CHECK) {
808 /* RCAP(10) might be unsupported by device. Force RCAP(16) */
811 blksz = sgio_get_be32(&buf[4]);
812 hlba = sgio_get_be32(buf);
816 * If max lba masked by MAX_10B_LBA equals MAX_10B_LBA,
817 * then need to retry with 16 byte Read Capacity command.
819 if (hlba == MAX_10B_LBA) {
821 hdr.cmdp[0] = 0x9e; // service action
822 hdr.cmdp[1] = 0x10; // Read Capacity(16)
823 sgio_set_be32(sizeof(buf), &hdr.cmdp[10]);
825 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
827 hdr.dxfer_len = sizeof(buf);
829 ret = ioctl(fd, SG_IO, &hdr);
835 /* record if an io error occurred */
836 if (hdr.info & SG_INFO_CHECK)
837 td_verror(td, EIO, "fio_sgio_read_capacity");
839 blksz = sgio_get_be32(&buf[8]);
840 hlba = sgio_get_be64(buf);
855 static void fio_sgio_cleanup(struct thread_data *td)
857 struct sgio_data *sd = td->io_ops_data;
866 #ifdef FIO_SGIO_DEBUG
867 free(sd->trim_queue_map);
870 for (i = 0; i < td->o.iodepth; i++) {
871 free(sd->trim_queues[i]->unmap_param);
872 free(sd->trim_queues[i]->trim_io_us);
873 free(sd->trim_queues[i]);
876 free(sd->trim_queues);
881 static int fio_sgio_init(struct thread_data *td)
883 struct sgio_data *sd;
884 struct sgio_trim *st;
885 struct sg_io_hdr *h3p;
888 sd = calloc(1, sizeof(*sd));
889 sd->cmds = calloc(td->o.iodepth, sizeof(struct sgio_cmd));
890 sd->sgbuf = calloc(td->o.iodepth, sizeof(struct sg_io_hdr));
891 sd->events = calloc(td->o.iodepth, sizeof(struct io_u *));
892 sd->pfds = calloc(td->o.nr_files, sizeof(struct pollfd));
893 sd->fd_flags = calloc(td->o.nr_files, sizeof(int));
894 sd->type_checked = 0;
896 sd->trim_queues = calloc(td->o.iodepth, sizeof(struct sgio_trim *));
897 sd->current_queue = -1;
898 #ifdef FIO_SGIO_DEBUG
899 sd->trim_queue_map = calloc(td->o.iodepth, sizeof(int));
901 for (i = 0, h3p = sd->sgbuf; i < td->o.iodepth; i++, ++h3p) {
902 sd->trim_queues[i] = calloc(1, sizeof(struct sgio_trim));
903 st = sd->trim_queues[i];
904 st->unmap_param = calloc(td->o.iodepth + 1, sizeof(char[16]));
905 st->unmap_range_count = 0;
906 st->trim_io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
907 h3p->interface_id = 'S';
910 td->io_ops_data = sd;
913 * we want to do it, regardless of whether odirect is set or not
915 td->o.override_sync = 1;
919 static int fio_sgio_type_check(struct thread_data *td, struct fio_file *f)
921 struct sgio_data *sd = td->io_ops_data;
923 unsigned long long max_lba = 0;
925 if (f->filetype == FIO_TYPE_BLOCK) {
926 if (ioctl(f->fd, BLKSSZGET, &bs) < 0) {
927 td_verror(td, errno, "ioctl");
930 } else if (f->filetype == FIO_TYPE_CHAR) {
933 if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) {
934 td_verror(td, errno, "ioctl");
938 ret = fio_sgio_read_capacity(td, &bs, &max_lba);
940 td_verror(td, td->error, "fio_sgio_read_capacity");
941 log_err("ioengine sg unable to read capacity successfully\n");
945 td_verror(td, EINVAL, "wrong file type");
946 log_err("ioengine sg only works on block or character devices\n");
951 // Determine size of commands needed based on max_lba
952 if (max_lba >= MAX_10B_LBA) {
953 dprint(FD_IO, "sgio_type_check: using 16 byte read/write "
954 "commands for lba above 0x%016llx/0x%016llx\n",
955 MAX_10B_LBA, max_lba);
958 if (f->filetype == FIO_TYPE_BLOCK) {
959 td->io_ops->getevents = NULL;
960 td->io_ops->event = NULL;
961 td->io_ops->commit = NULL;
963 ** Setting these functions to null may cause problems
964 ** with filename=/dev/sda:/dev/sg0 since we are only
965 ** considering a single file
968 sd->type_checked = 1;
973 static int fio_sgio_open(struct thread_data *td, struct fio_file *f)
975 struct sgio_data *sd = td->io_ops_data;
978 ret = generic_open_file(td, f);
982 if (sd && !sd->type_checked && fio_sgio_type_check(td, f)) {
983 ret = generic_close_file(td, f);
991 * Build an error string with details about the driver, host or scsi
992 * error contained in the sg header Caller will use as necessary.
994 static char *fio_sgio_errdetails(struct io_u *io_u)
996 struct sg_io_hdr *hdr = &io_u->hdr;
997 #define MAXERRDETAIL 1024
998 #define MAXMSGCHUNK 128
999 char *msg, msgchunk[MAXMSGCHUNK];
1002 msg = calloc(1, MAXERRDETAIL);
1006 * can't seem to find sg_err.h, so I'll just echo the define values
1007 * so others can search on internet to find clearer clues of meaning.
1009 if (hdr->info & SG_INFO_CHECK) {
1010 if (hdr->host_status) {
1011 snprintf(msgchunk, MAXMSGCHUNK, "SG Host Status: 0x%02x; ", hdr->host_status);
1012 strlcat(msg, msgchunk, MAXERRDETAIL);
1013 switch (hdr->host_status) {
1015 strlcat(msg, "SG_ERR_DID_NO_CONNECT", MAXERRDETAIL);
1018 strlcat(msg, "SG_ERR_DID_BUS_BUSY", MAXERRDETAIL);
1021 strlcat(msg, "SG_ERR_DID_TIME_OUT", MAXERRDETAIL);
1024 strlcat(msg, "SG_ERR_DID_BAD_TARGET", MAXERRDETAIL);
1027 strlcat(msg, "SG_ERR_DID_ABORT", MAXERRDETAIL);
1030 strlcat(msg, "SG_ERR_DID_PARITY", MAXERRDETAIL);
1033 strlcat(msg, "SG_ERR_DID_ERROR (internal error)", MAXERRDETAIL);
1036 strlcat(msg, "SG_ERR_DID_RESET", MAXERRDETAIL);
1039 strlcat(msg, "SG_ERR_DID_BAD_INTR (unexpected)", MAXERRDETAIL);
1042 strlcat(msg, "SG_ERR_DID_PASSTHROUGH", MAXERRDETAIL);
1045 strlcat(msg, "SG_ERR_DID_SOFT_ERROR (driver retry?)", MAXERRDETAIL);
1048 strlcat(msg, "SG_ERR_DID_IMM_RETRY", MAXERRDETAIL);
1051 strlcat(msg, "SG_ERR_DID_REQUEUE", MAXERRDETAIL);
1054 strlcat(msg, "SG_ERR_DID_TRANSPORT_DISRUPTED", MAXERRDETAIL);
1057 strlcat(msg, "SG_ERR_DID_TRANSPORT_FAILFAST", MAXERRDETAIL);
1060 strlcat(msg, "SG_ERR_DID_TARGET_FAILURE", MAXERRDETAIL);
1063 strlcat(msg, "SG_ERR_DID_NEXUS_FAILURE", MAXERRDETAIL);
1066 strlcat(msg, "SG_ERR_DID_ALLOC_FAILURE", MAXERRDETAIL);
1069 strlcat(msg, "SG_ERR_DID_MEDIUM_ERROR", MAXERRDETAIL);
1072 strlcat(msg, "Unknown", MAXERRDETAIL);
1075 strlcat(msg, ". ", MAXERRDETAIL);
1077 if (hdr->driver_status) {
1078 snprintf(msgchunk, MAXMSGCHUNK, "SG Driver Status: 0x%02x; ", hdr->driver_status);
1079 strlcat(msg, msgchunk, MAXERRDETAIL);
1080 switch (hdr->driver_status & 0x0F) {
1082 strlcat(msg, "SG_ERR_DRIVER_BUSY", MAXERRDETAIL);
1085 strlcat(msg, "SG_ERR_DRIVER_SOFT", MAXERRDETAIL);
1088 strlcat(msg, "SG_ERR_DRIVER_MEDIA", MAXERRDETAIL);
1091 strlcat(msg, "SG_ERR_DRIVER_ERROR", MAXERRDETAIL);
1094 strlcat(msg, "SG_ERR_DRIVER_INVALID", MAXERRDETAIL);
1097 strlcat(msg, "SG_ERR_DRIVER_TIMEOUT", MAXERRDETAIL);
1100 strlcat(msg, "SG_ERR_DRIVER_HARD", MAXERRDETAIL);
1103 strlcat(msg, "SG_ERR_DRIVER_SENSE", MAXERRDETAIL);
1106 strlcat(msg, "Unknown", MAXERRDETAIL);
1109 strlcat(msg, "; ", MAXERRDETAIL);
1110 switch (hdr->driver_status & 0xF0) {
1112 strlcat(msg, "SG_ERR_SUGGEST_RETRY", MAXERRDETAIL);
1115 strlcat(msg, "SG_ERR_SUGGEST_ABORT", MAXERRDETAIL);
1118 strlcat(msg, "SG_ERR_SUGGEST_REMAP", MAXERRDETAIL);
1121 strlcat(msg, "SG_ERR_SUGGEST_DIE", MAXERRDETAIL);
1124 strlcat(msg, "SG_ERR_SUGGEST_SENSE", MAXERRDETAIL);
1127 strlcat(msg, ". ", MAXERRDETAIL);
1130 snprintf(msgchunk, MAXMSGCHUNK, "SG SCSI Status: 0x%02x; ", hdr->status);
1131 strlcat(msg, msgchunk, MAXERRDETAIL);
1132 // SCSI 3 status codes
1133 switch (hdr->status) {
1135 strlcat(msg, "CHECK_CONDITION", MAXERRDETAIL);
1138 strlcat(msg, "CONDITION_MET", MAXERRDETAIL);
1141 strlcat(msg, "BUSY", MAXERRDETAIL);
1144 strlcat(msg, "INTERMEDIATE", MAXERRDETAIL);
1147 strlcat(msg, "INTERMEDIATE_CONDITION_MET", MAXERRDETAIL);
1150 strlcat(msg, "RESERVATION_CONFLICT", MAXERRDETAIL);
1153 strlcat(msg, "COMMAND_TERMINATED", MAXERRDETAIL);
1156 strlcat(msg, "TASK_SET_FULL", MAXERRDETAIL);
1159 strlcat(msg, "ACA_ACTIVE", MAXERRDETAIL);
1162 strlcat(msg, "TASK_ABORTED", MAXERRDETAIL);
1165 strlcat(msg, "Unknown", MAXERRDETAIL);
1168 strlcat(msg, ". ", MAXERRDETAIL);
1170 if (hdr->sb_len_wr) {
1171 snprintf(msgchunk, MAXMSGCHUNK, "Sense Data (%d bytes):", hdr->sb_len_wr);
1172 strlcat(msg, msgchunk, MAXERRDETAIL);
1173 for (i = 0; i < hdr->sb_len_wr; i++) {
1174 snprintf(msgchunk, MAXMSGCHUNK, " %02x", hdr->sbp[i]);
1175 strlcat(msg, msgchunk, MAXERRDETAIL);
1177 strlcat(msg, ". ", MAXERRDETAIL);
1179 if (hdr->resid != 0) {
1180 snprintf(msgchunk, MAXMSGCHUNK, "SG Driver: %d bytes out of %d not transferred. ", hdr->resid, hdr->dxfer_len);
1181 strlcat(msg, msgchunk, MAXERRDETAIL);
1184 strlcat(msg, "cdb:", MAXERRDETAIL);
1185 for (i = 0; i < hdr->cmd_len; i++) {
1186 snprintf(msgchunk, MAXMSGCHUNK, " %02x", hdr->cmdp[i]);
1187 strlcat(msg, msgchunk, MAXERRDETAIL);
1189 strlcat(msg, ". ", MAXERRDETAIL);
1190 if (io_u->ddir == DDIR_TRIM) {
1191 unsigned char *param_list = hdr->dxferp;
1192 strlcat(msg, "dxferp:", MAXERRDETAIL);
1193 for (i = 0; i < hdr->dxfer_len; i++) {
1194 snprintf(msgchunk, MAXMSGCHUNK, " %02x", param_list[i]);
1195 strlcat(msg, msgchunk, MAXERRDETAIL);
1197 strlcat(msg, ". ", MAXERRDETAIL);
1202 if (!(hdr->info & SG_INFO_CHECK) && !strlen(msg))
1203 snprintf(msg, MAXERRDETAIL, "%s",
1204 "SG Driver did not report a Host, Driver or Device check");
1210 * get max file size from read capacity.
1212 static int fio_sgio_get_file_size(struct thread_data *td, struct fio_file *f)
1215 * get_file_size is being called even before sgio_init is
1216 * called, so none of the sg_io structures are
1217 * initialized in the thread_data yet. So we need to do the
1218 * ReadCapacity without any of those helpers. One of the effects
1219 * is that ReadCapacity may get called 4 times on each open:
1220 * readcap(10) followed by readcap(16) if needed - just to get
1221 * the file size after the init occurs - it will be called
1222 * again when "type_check" is called during structure
1223 * initialization I'm not sure how to prevent this little
1226 unsigned int bs = 0;
1227 unsigned long long max_lba = 0;
1230 if (fio_file_size_known(f))
1233 if (f->filetype != FIO_TYPE_BLOCK && f->filetype != FIO_TYPE_CHAR) {
1234 td_verror(td, EINVAL, "wrong file type");
1235 log_err("ioengine sg only works on block or character devices\n");
1239 ret = fio_sgio_read_capacity(td, &bs, &max_lba);
1241 td_verror(td, td->error, "fio_sgio_read_capacity");
1242 log_err("ioengine sg unable to successfully execute read capacity to get block size and maximum lba\n");
1246 f->real_file_size = (max_lba + 1) * bs;
1247 fio_file_set_size_known(f);
1252 static struct ioengine_ops ioengine = {
1254 .version = FIO_IOOPS_VERSION,
1255 .init = fio_sgio_init,
1256 .prep = fio_sgio_prep,
1257 .queue = fio_sgio_queue,
1258 .commit = fio_sgio_commit,
1259 .getevents = fio_sgio_getevents,
1260 .errdetails = fio_sgio_errdetails,
1261 .event = fio_sgio_event,
1262 .cleanup = fio_sgio_cleanup,
1263 .open_file = fio_sgio_open,
1264 .close_file = generic_close_file,
1265 .get_file_size = fio_sgio_get_file_size,
1266 .flags = FIO_SYNCIO | FIO_RAWIO,
1268 .option_struct_size = sizeof(struct sg_options)
1271 #else /* FIO_HAVE_SGIO */
1274 * When we have a proper configure system in place, we simply wont build
1275 * and install this io engine. For now install a crippled version that
1276 * just complains and fails to load.
1278 static int fio_sgio_init(struct thread_data fio_unused *td)
1280 log_err("fio: ioengine sg not available\n");
1284 static struct ioengine_ops ioengine = {
1286 .version = FIO_IOOPS_VERSION,
1287 .init = fio_sgio_init,
1292 static void fio_init fio_sgio_register(void)
1294 register_ioengine(&ioengine);
1297 static void fio_exit fio_sgio_unregister(void)
1299 unregister_ioengine(&ioengine);