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"
65 FIO_SG_WRITE_VERIFY = 2,
72 unsigned int writefua;
73 unsigned int write_mode;
76 static struct fio_option options[] = {
79 .lname = "sg engine read fua flag support",
81 .off1 = offsetof(struct sg_options, readfua),
82 .help = "Set FUA flag (force unit access) for all Read operations",
84 .category = FIO_OPT_C_ENGINE,
85 .group = FIO_OPT_G_SG,
89 .lname = "sg engine write fua flag support",
91 .off1 = offsetof(struct sg_options, writefua),
92 .help = "Set FUA flag (force unit access) for all Write operations",
94 .category = FIO_OPT_C_ENGINE,
95 .group = FIO_OPT_G_SG,
98 .name = "sg_write_mode",
99 .lname = "specify sg write mode",
101 .off1 = offsetof(struct sg_options, write_mode),
102 .help = "Specify SCSI WRITE mode",
106 .oval = FIO_SG_WRITE,
107 .help = "Issue standard SCSI WRITE commands",
110 .oval = FIO_SG_WRITE_VERIFY,
111 .help = "Issue SCSI WRITE AND VERIFY commands",
114 .oval = FIO_SG_WRITE_SAME,
115 .help = "Issue SCSI WRITE SAME commands",
118 .category = FIO_OPT_C_ENGINE,
119 .group = FIO_OPT_G_SG,
126 #define MAX_10B_LBA 0xFFFFFFFFULL
127 #define SCSI_TIMEOUT_MS 30000 // 30 second timeout; currently no method to override
128 #define MAX_SB 64 // sense block maximum return size
130 #define FIO_SGIO_DEBUG
134 unsigned char cdb[16]; // enhanced from 10 to support 16 byte commands
135 unsigned char sb[MAX_SB]; // add sense block to commands
140 uint8_t *unmap_param;
141 unsigned int unmap_range_count;
142 struct io_u **trim_io_us;
146 struct sgio_cmd *cmds;
147 struct io_u **events;
153 struct sgio_trim **trim_queues;
155 #ifdef FIO_SGIO_DEBUG
156 unsigned int *trim_queue_map;
160 static inline uint32_t sgio_get_be32(uint8_t *buf)
162 return be32_to_cpu(*((uint32_t *) buf));
165 static inline uint64_t sgio_get_be64(uint8_t *buf)
167 return be64_to_cpu(*((uint64_t *) buf));
170 static inline void sgio_set_be16(uint16_t val, uint8_t *buf)
172 uint16_t t = cpu_to_be16(val);
174 memcpy(buf, &t, sizeof(uint16_t));
177 static inline void sgio_set_be32(uint32_t val, uint8_t *buf)
179 uint32_t t = cpu_to_be32(val);
181 memcpy(buf, &t, sizeof(uint32_t));
184 static inline void sgio_set_be64(uint64_t val, uint8_t *buf)
186 uint64_t t = cpu_to_be64(val);
188 memcpy(buf, &t, sizeof(uint64_t));
191 static inline bool sgio_unbuffered(struct thread_data *td)
193 return (td->o.odirect || td->o.sync_io);
196 static void sgio_hdr_init(struct sgio_data *sd, struct sg_io_hdr *hdr,
197 struct io_u *io_u, int fs)
199 struct sgio_cmd *sc = &sd->cmds[io_u->index];
201 memset(hdr, 0, sizeof(*hdr));
202 memset(sc->cdb, 0, sizeof(sc->cdb));
204 hdr->interface_id = 'S';
206 hdr->cmd_len = sizeof(sc->cdb);
208 hdr->mx_sb_len = sizeof(sc->sb);
209 hdr->pack_id = io_u->index;
211 hdr->timeout = SCSI_TIMEOUT_MS;
214 hdr->dxferp = io_u->xfer_buf;
215 hdr->dxfer_len = io_u->xfer_buflen;
219 static int pollin_events(struct pollfd *pfds, int fds)
223 for (i = 0; i < fds; i++)
224 if (pfds[i].revents & POLLIN)
230 static int sg_fd_read(int fd, void *data, size_t size)
237 ret = read(fd, data, size);
239 if (errno == EAGAIN || errno == EINTR)
259 static int fio_sgio_getevents(struct thread_data *td, unsigned int min,
261 const struct timespec fio_unused *t)
263 struct sgio_data *sd = td->io_ops_data;
264 int left = max, eventNum, ret, r = 0, trims = 0;
265 void *buf = sd->sgbuf;
266 unsigned int i, j, events;
271 * Fill in the file descriptors
273 for_each_file(td, f, i) {
275 * don't block for min events == 0
278 sd->fd_flags[i] = fio_set_fd_nonblocking(f->fd, "sg");
280 sd->fd_flags[i] = -1;
282 sd->pfds[i].fd = f->fd;
283 sd->pfds[i].events = POLLIN;
287 ** There are two counters here:
288 ** - number of SCSI commands completed
289 ** - number of io_us completed
291 ** These are the same with reads and writes, but
292 ** could differ with trim/unmap commands because
293 ** a single unmap can include multiple io_us
299 dprint(FD_IO, "sgio_getevents: sd %p: min=%d, max=%d, left=%d\n", sd, min, max, left);
305 ret = poll(sd->pfds, td->o.nr_files, -1);
309 td_verror(td, errno, "poll");
314 if (pollin_events(sd->pfds, td->o.nr_files))
324 for_each_file(td, f, i) {
325 for (eventNum = 0; eventNum < left; eventNum++) {
326 ret = sg_fd_read(f->fd, p, sizeof(struct sg_io_hdr));
327 dprint(FD_IO, "sgio_getevents: sg_fd_read ret: %d\n", ret);
330 td_verror(td, r, "sg_read");
333 io_u = ((struct sg_io_hdr *)p)->usr_ptr;
334 if (io_u->ddir == DDIR_TRIM) {
335 events += sd->trim_queues[io_u->index]->unmap_range_count;
336 eventNum += sd->trim_queues[io_u->index]->unmap_range_count - 1;
340 p += sizeof(struct sg_io_hdr);
341 dprint(FD_IO, "sgio_getevents: events: %d, eventNum: %d, left: %d\n", events, eventNum, left);
345 if (r < 0 && !events)
355 for (i = 0; i < events; i++) {
356 struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i;
357 sd->events[i + trims] = hdr->usr_ptr;
358 io_u = (struct io_u *)(hdr->usr_ptr);
360 if (hdr->info & SG_INFO_CHECK) {
361 /* record if an io error occurred, ignore resid */
362 memcpy(&io_u->hdr, hdr, sizeof(struct sg_io_hdr));
363 sd->events[i + trims]->error = EIO;
366 if (io_u->ddir == DDIR_TRIM) {
367 struct sgio_trim *st = sd->trim_queues[io_u->index];
368 #ifdef FIO_SGIO_DEBUG
369 assert(st->trim_io_us[0] == io_u);
370 assert(sd->trim_queue_map[io_u->index] == io_u->index);
371 dprint(FD_IO, "sgio_getevents: reaping %d io_us from trim queue %d\n", st->unmap_range_count, io_u->index);
372 dprint(FD_IO, "sgio_getevents: reaped io_u %d and stored in events[%d]\n", io_u->index, i+trims);
374 for (j = 1; j < st->unmap_range_count; j++) {
376 sd->events[i + trims] = st->trim_io_us[j];
377 #ifdef FIO_SGIO_DEBUG
378 dprint(FD_IO, "sgio_getevents: reaped io_u %d and stored in events[%d]\n", st->trim_io_us[j]->index, i+trims);
379 assert(sd->trim_queue_map[st->trim_io_us[j]->index] == io_u->index);
381 if (hdr->info & SG_INFO_CHECK) {
382 /* record if an io error occurred, ignore resid */
383 memcpy(&st->trim_io_us[j]->hdr, hdr, sizeof(struct sg_io_hdr));
384 sd->events[i + trims]->error = EIO;
387 events -= st->unmap_range_count - 1;
388 st->unmap_range_count = 0;
394 for_each_file(td, f, i) {
395 if (sd->fd_flags[i] == -1)
398 if (fcntl(f->fd, F_SETFL, sd->fd_flags[i]) < 0)
399 log_err("fio: sg failed to restore fcntl flags: %s\n", strerror(errno));
406 static enum fio_q_status fio_sgio_ioctl_doio(struct thread_data *td,
410 struct sgio_data *sd = td->io_ops_data;
411 struct sg_io_hdr *hdr = &io_u->hdr;
414 sd->events[0] = io_u;
416 ret = ioctl(f->fd, SG_IO, hdr);
420 /* record if an io error occurred */
421 if (hdr->info & SG_INFO_CHECK)
424 return FIO_Q_COMPLETED;
427 static enum fio_q_status fio_sgio_rw_doio(struct thread_data *td,
429 struct io_u *io_u, int do_sync)
431 struct sg_io_hdr *hdr = &io_u->hdr;
434 ret = write(f->fd, hdr, sizeof(*hdr));
440 * We can't just read back the first command that completes
441 * and assume it's the one we need, it could be any command
447 ret = read(f->fd, hdr, sizeof(*hdr));
451 /* record if an io error occurred */
452 if (hdr->info & SG_INFO_CHECK)
455 __io_u = hdr->usr_ptr;
459 if (io_u_sync_complete(td, __io_u)) {
465 return FIO_Q_COMPLETED;
471 static enum fio_q_status fio_sgio_doio(struct thread_data *td,
472 struct io_u *io_u, int do_sync)
474 struct fio_file *f = io_u->file;
475 enum fio_q_status ret;
477 if (f->filetype == FIO_TYPE_BLOCK) {
478 ret = fio_sgio_ioctl_doio(td, f, io_u);
480 td_verror(td, io_u->error, __func__);
482 ret = fio_sgio_rw_doio(td, f, io_u, do_sync);
483 if (io_u->error && do_sync)
484 td_verror(td, io_u->error, __func__);
490 static void fio_sgio_rw_lba(struct sg_io_hdr *hdr, unsigned long long lba,
491 unsigned long long nr_blocks)
493 if (lba < MAX_10B_LBA) {
494 sgio_set_be32((uint32_t) lba, &hdr->cmdp[2]);
495 sgio_set_be16((uint16_t) nr_blocks, &hdr->cmdp[7]);
497 sgio_set_be64(lba, &hdr->cmdp[2]);
498 sgio_set_be32((uint32_t) nr_blocks, &hdr->cmdp[10]);
504 static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u)
506 struct sg_io_hdr *hdr = &io_u->hdr;
507 struct sg_options *o = td->eo;
508 struct sgio_data *sd = td->io_ops_data;
509 unsigned long long nr_blocks, lba;
512 if (io_u->xfer_buflen & (sd->bs - 1)) {
513 log_err("read/write not sector aligned\n");
517 nr_blocks = io_u->xfer_buflen / sd->bs;
518 lba = io_u->offset / sd->bs;
520 if (io_u->ddir == DDIR_READ) {
521 sgio_hdr_init(sd, hdr, io_u, 1);
523 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
524 if (lba < MAX_10B_LBA)
525 hdr->cmdp[0] = 0x28; // read(10)
527 hdr->cmdp[0] = 0x88; // read(16)
530 hdr->cmdp[1] |= 0x08;
532 fio_sgio_rw_lba(hdr, lba, nr_blocks);
534 } else if (io_u->ddir == DDIR_WRITE) {
535 sgio_hdr_init(sd, hdr, io_u, 1);
537 hdr->dxfer_direction = SG_DXFER_TO_DEV;
538 switch(o->write_mode) {
540 if (lba < MAX_10B_LBA)
541 hdr->cmdp[0] = 0x2a; // write(10)
543 hdr->cmdp[0] = 0x8a; // write(16)
545 hdr->cmdp[1] |= 0x08;
547 case FIO_SG_WRITE_VERIFY:
548 if (lba < MAX_10B_LBA)
549 hdr->cmdp[0] = 0x2e; // write and verify(10)
551 hdr->cmdp[0] = 0x8e; // write and verify(16)
553 // BYTCHK is disabled by virtue of the memset in sgio_hdr_init
554 case FIO_SG_WRITE_SAME:
555 hdr->dxfer_len = sd->bs;
556 if (lba < MAX_10B_LBA)
557 hdr->cmdp[0] = 0x41; // write same(10)
559 hdr->cmdp[0] = 0x93; // write same(16)
563 fio_sgio_rw_lba(hdr, lba, nr_blocks);
565 } else if (io_u->ddir == DDIR_TRIM) {
566 struct sgio_trim *st;
568 if (sd->current_queue == -1) {
569 sgio_hdr_init(sd, hdr, io_u, 0);
572 hdr->dxfer_direction = SG_DXFER_TO_DEV;
573 hdr->cmdp[0] = 0x42; // unmap
574 sd->current_queue = io_u->index;
575 st = sd->trim_queues[sd->current_queue];
576 hdr->dxferp = st->unmap_param;
577 #ifdef FIO_SGIO_DEBUG
578 assert(sd->trim_queues[io_u->index]->unmap_range_count == 0);
579 dprint(FD_IO, "sg: creating new queue based on io_u %d\n", io_u->index);
583 st = sd->trim_queues[sd->current_queue];
585 dprint(FD_IO, "sg: adding io_u %d to trim queue %d\n", io_u->index, sd->current_queue);
586 st->trim_io_us[st->unmap_range_count] = io_u;
587 #ifdef FIO_SGIO_DEBUG
588 sd->trim_queue_map[io_u->index] = sd->current_queue;
591 offset = 8 + 16 * st->unmap_range_count;
592 sgio_set_be64(lba, &st->unmap_param[offset]);
593 sgio_set_be32((uint32_t) nr_blocks, &st->unmap_param[offset + 8]);
595 st->unmap_range_count++;
597 } else if (ddir_sync(io_u->ddir)) {
598 sgio_hdr_init(sd, hdr, io_u, 0);
599 hdr->dxfer_direction = SG_DXFER_NONE;
600 if (lba < MAX_10B_LBA)
601 hdr->cmdp[0] = 0x35; // synccache(10)
603 hdr->cmdp[0] = 0x91; // synccache(16)
610 static void fio_sgio_unmap_setup(struct sg_io_hdr *hdr, struct sgio_trim *st)
612 uint16_t cnt = st->unmap_range_count * 16;
614 hdr->dxfer_len = cnt + 8;
615 sgio_set_be16(cnt + 8, &hdr->cmdp[7]);
616 sgio_set_be16(cnt + 6, st->unmap_param);
617 sgio_set_be16(cnt, &st->unmap_param[2]);
622 static enum fio_q_status fio_sgio_queue(struct thread_data *td,
625 struct sg_io_hdr *hdr = &io_u->hdr;
626 struct sgio_data *sd = td->io_ops_data;
627 int ret, do_sync = 0;
629 fio_ro_check(td, io_u);
631 if (sgio_unbuffered(td) || ddir_sync(io_u->ddir))
634 if (io_u->ddir == DDIR_TRIM) {
635 if (do_sync || io_u->file->filetype == FIO_TYPE_BLOCK) {
636 struct sgio_trim *st = sd->trim_queues[sd->current_queue];
638 /* finish cdb setup for unmap because we are
639 ** doing unmap commands synchronously */
640 #ifdef FIO_SGIO_DEBUG
641 assert(st->unmap_range_count == 1);
642 assert(io_u == st->trim_io_us[0]);
646 fio_sgio_unmap_setup(hdr, st);
648 st->unmap_range_count = 0;
649 sd->current_queue = -1;
651 /* queue up trim ranges and submit in commit() */
655 ret = fio_sgio_doio(td, io_u, do_sync);
659 else if (hdr->status) {
660 io_u->resid = hdr->resid;
662 } else if (td->io_ops->commit != NULL) {
663 if (do_sync && !ddir_sync(io_u->ddir)) {
664 io_u_mark_submit(td, 1);
665 io_u_mark_complete(td, 1);
666 } else if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
667 io_u_mark_submit(td, 1);
668 io_u_queued(td, io_u);
673 td_verror(td, io_u->error, "xfer");
674 return FIO_Q_COMPLETED;
680 static int fio_sgio_commit(struct thread_data *td)
682 struct sgio_data *sd = td->io_ops_data;
683 struct sgio_trim *st;
685 struct sg_io_hdr *hdr;
690 if (sd->current_queue == -1)
693 st = sd->trim_queues[sd->current_queue];
694 io_u = st->trim_io_us[0];
697 fio_sgio_unmap_setup(hdr, st);
699 sd->current_queue = -1;
701 ret = fio_sgio_rw_doio(td, io_u->file, io_u, 0);
703 if (ret < 0 || hdr->status) {
713 for (i = 0; i < st->unmap_range_count; i++) {
714 st->trim_io_us[i]->error = error;
715 clear_io_u(td, st->trim_io_us[i]);
717 st->trim_io_us[i]->resid = hdr->resid;
720 td_verror(td, error, "xfer");
724 if (fio_fill_issue_time(td)) {
725 fio_gettime(&now, NULL);
726 for (i = 0; i < st->unmap_range_count; i++) {
727 memcpy(&st->trim_io_us[i]->issue_time, &now, sizeof(now));
728 io_u_queued(td, io_u);
731 io_u_mark_submit(td, st->unmap_range_count);
736 static struct io_u *fio_sgio_event(struct thread_data *td, int event)
738 struct sgio_data *sd = td->io_ops_data;
740 return sd->events[event];
743 static int fio_sgio_read_capacity(struct thread_data *td, unsigned int *bs,
744 unsigned long long *max_lba)
747 * need to do read capacity operation w/o benefit of sd or
748 * io_u structures, which are not initialized until later.
750 struct sg_io_hdr hdr;
751 unsigned long long hlba;
752 unsigned int blksz = 0;
753 unsigned char cmd[16];
754 unsigned char sb[64];
755 unsigned char buf[32]; // read capacity return
759 struct fio_file *f = td->files[0];
761 /* open file independent of rest of application */
762 fd = open(f->file_name, O_RDONLY);
766 memset(&hdr, 0, sizeof(hdr));
767 memset(cmd, 0, sizeof(cmd));
768 memset(sb, 0, sizeof(sb));
769 memset(buf, 0, sizeof(buf));
771 /* First let's try a 10 byte read capacity. */
772 hdr.interface_id = 'S';
776 hdr.mx_sb_len = sizeof(sb);
777 hdr.timeout = SCSI_TIMEOUT_MS;
778 hdr.cmdp[0] = 0x25; // Read Capacity(10)
779 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
781 hdr.dxfer_len = sizeof(buf);
783 ret = ioctl(fd, SG_IO, &hdr);
789 if (hdr.info & SG_INFO_CHECK) {
790 /* RCAP(10) might be unsupported by device. Force RCAP(16) */
793 blksz = sgio_get_be32(&buf[4]);
794 hlba = sgio_get_be32(buf);
798 * If max lba masked by MAX_10B_LBA equals MAX_10B_LBA,
799 * then need to retry with 16 byte Read Capacity command.
801 if (hlba == MAX_10B_LBA) {
803 hdr.cmdp[0] = 0x9e; // service action
804 hdr.cmdp[1] = 0x10; // Read Capacity(16)
805 sgio_set_be32(sizeof(buf), &hdr.cmdp[10]);
807 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
809 hdr.dxfer_len = sizeof(buf);
811 ret = ioctl(fd, SG_IO, &hdr);
817 /* record if an io error occurred */
818 if (hdr.info & SG_INFO_CHECK)
819 td_verror(td, EIO, "fio_sgio_read_capacity");
821 blksz = sgio_get_be32(&buf[8]);
822 hlba = sgio_get_be64(buf);
837 static void fio_sgio_cleanup(struct thread_data *td)
839 struct sgio_data *sd = td->io_ops_data;
848 #ifdef FIO_SGIO_DEBUG
849 free(sd->trim_queue_map);
852 for (i = 0; i < td->o.iodepth; i++) {
853 free(sd->trim_queues[i]->unmap_param);
854 free(sd->trim_queues[i]->trim_io_us);
855 free(sd->trim_queues[i]);
858 free(sd->trim_queues);
863 static int fio_sgio_init(struct thread_data *td)
865 struct sgio_data *sd;
866 struct sgio_trim *st;
869 sd = calloc(1, sizeof(*sd));
870 sd->cmds = calloc(td->o.iodepth, sizeof(struct sgio_cmd));
871 sd->sgbuf = calloc(td->o.iodepth, sizeof(struct sg_io_hdr));
872 sd->events = calloc(td->o.iodepth, sizeof(struct io_u *));
873 sd->pfds = calloc(td->o.nr_files, sizeof(struct pollfd));
874 sd->fd_flags = calloc(td->o.nr_files, sizeof(int));
875 sd->type_checked = 0;
877 sd->trim_queues = calloc(td->o.iodepth, sizeof(struct sgio_trim *));
878 sd->current_queue = -1;
879 #ifdef FIO_SGIO_DEBUG
880 sd->trim_queue_map = calloc(td->o.iodepth, sizeof(int));
882 for (i = 0; i < td->o.iodepth; i++) {
883 sd->trim_queues[i] = calloc(1, sizeof(struct sgio_trim));
884 st = sd->trim_queues[i];
885 st->unmap_param = calloc(td->o.iodepth + 1, sizeof(char[16]));
886 st->unmap_range_count = 0;
887 st->trim_io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
890 td->io_ops_data = sd;
893 * we want to do it, regardless of whether odirect is set or not
895 td->o.override_sync = 1;
899 static int fio_sgio_type_check(struct thread_data *td, struct fio_file *f)
901 struct sgio_data *sd = td->io_ops_data;
903 unsigned long long max_lba = 0;
905 if (f->filetype == FIO_TYPE_BLOCK) {
906 if (ioctl(f->fd, BLKSSZGET, &bs) < 0) {
907 td_verror(td, errno, "ioctl");
910 } else if (f->filetype == FIO_TYPE_CHAR) {
913 if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) {
914 td_verror(td, errno, "ioctl");
918 ret = fio_sgio_read_capacity(td, &bs, &max_lba);
920 td_verror(td, td->error, "fio_sgio_read_capacity");
921 log_err("ioengine sg unable to read capacity successfully\n");
925 td_verror(td, EINVAL, "wrong file type");
926 log_err("ioengine sg only works on block or character devices\n");
931 // Determine size of commands needed based on max_lba
932 if (max_lba >= MAX_10B_LBA) {
933 dprint(FD_IO, "sgio_type_check: using 16 byte read/write "
934 "commands for lba above 0x%016llx/0x%016llx\n",
935 MAX_10B_LBA, max_lba);
938 if (f->filetype == FIO_TYPE_BLOCK) {
939 td->io_ops->getevents = NULL;
940 td->io_ops->event = NULL;
941 td->io_ops->commit = NULL;
943 ** Setting these functions to null may cause problems
944 ** with filename=/dev/sda:/dev/sg0 since we are only
945 ** considering a single file
948 sd->type_checked = 1;
953 static int fio_sgio_open(struct thread_data *td, struct fio_file *f)
955 struct sgio_data *sd = td->io_ops_data;
958 ret = generic_open_file(td, f);
962 if (sd && !sd->type_checked && fio_sgio_type_check(td, f)) {
963 ret = generic_close_file(td, f);
971 * Build an error string with details about the driver, host or scsi
972 * error contained in the sg header Caller will use as necessary.
974 static char *fio_sgio_errdetails(struct io_u *io_u)
976 struct sg_io_hdr *hdr = &io_u->hdr;
977 #define MAXERRDETAIL 1024
978 #define MAXMSGCHUNK 128
979 char *msg, msgchunk[MAXMSGCHUNK];
982 msg = calloc(1, MAXERRDETAIL);
986 * can't seem to find sg_err.h, so I'll just echo the define values
987 * so others can search on internet to find clearer clues of meaning.
989 if (hdr->info & SG_INFO_CHECK) {
990 if (hdr->host_status) {
991 snprintf(msgchunk, MAXMSGCHUNK, "SG Host Status: 0x%02x; ", hdr->host_status);
992 strlcat(msg, msgchunk, MAXERRDETAIL);
993 switch (hdr->host_status) {
995 strlcat(msg, "SG_ERR_DID_NO_CONNECT", MAXERRDETAIL);
998 strlcat(msg, "SG_ERR_DID_BUS_BUSY", MAXERRDETAIL);
1001 strlcat(msg, "SG_ERR_DID_TIME_OUT", MAXERRDETAIL);
1004 strlcat(msg, "SG_ERR_DID_BAD_TARGET", MAXERRDETAIL);
1007 strlcat(msg, "SG_ERR_DID_ABORT", MAXERRDETAIL);
1010 strlcat(msg, "SG_ERR_DID_PARITY", MAXERRDETAIL);
1013 strlcat(msg, "SG_ERR_DID_ERROR (internal error)", MAXERRDETAIL);
1016 strlcat(msg, "SG_ERR_DID_RESET", MAXERRDETAIL);
1019 strlcat(msg, "SG_ERR_DID_BAD_INTR (unexpected)", MAXERRDETAIL);
1022 strlcat(msg, "SG_ERR_DID_PASSTHROUGH", MAXERRDETAIL);
1025 strlcat(msg, "SG_ERR_DID_SOFT_ERROR (driver retry?)", MAXERRDETAIL);
1028 strlcat(msg, "SG_ERR_DID_IMM_RETRY", MAXERRDETAIL);
1031 strlcat(msg, "SG_ERR_DID_REQUEUE", MAXERRDETAIL);
1034 strlcat(msg, "SG_ERR_DID_TRANSPORT_DISRUPTED", MAXERRDETAIL);
1037 strlcat(msg, "SG_ERR_DID_TRANSPORT_FAILFAST", MAXERRDETAIL);
1040 strlcat(msg, "SG_ERR_DID_TARGET_FAILURE", MAXERRDETAIL);
1043 strlcat(msg, "SG_ERR_DID_NEXUS_FAILURE", MAXERRDETAIL);
1046 strlcat(msg, "SG_ERR_DID_ALLOC_FAILURE", MAXERRDETAIL);
1049 strlcat(msg, "SG_ERR_DID_MEDIUM_ERROR", MAXERRDETAIL);
1052 strlcat(msg, "Unknown", MAXERRDETAIL);
1055 strlcat(msg, ". ", MAXERRDETAIL);
1057 if (hdr->driver_status) {
1058 snprintf(msgchunk, MAXMSGCHUNK, "SG Driver Status: 0x%02x; ", hdr->driver_status);
1059 strlcat(msg, msgchunk, MAXERRDETAIL);
1060 switch (hdr->driver_status & 0x0F) {
1062 strlcat(msg, "SG_ERR_DRIVER_BUSY", MAXERRDETAIL);
1065 strlcat(msg, "SG_ERR_DRIVER_SOFT", MAXERRDETAIL);
1068 strlcat(msg, "SG_ERR_DRIVER_MEDIA", MAXERRDETAIL);
1071 strlcat(msg, "SG_ERR_DRIVER_ERROR", MAXERRDETAIL);
1074 strlcat(msg, "SG_ERR_DRIVER_INVALID", MAXERRDETAIL);
1077 strlcat(msg, "SG_ERR_DRIVER_TIMEOUT", MAXERRDETAIL);
1080 strlcat(msg, "SG_ERR_DRIVER_HARD", MAXERRDETAIL);
1083 strlcat(msg, "SG_ERR_DRIVER_SENSE", MAXERRDETAIL);
1086 strlcat(msg, "Unknown", MAXERRDETAIL);
1089 strlcat(msg, "; ", MAXERRDETAIL);
1090 switch (hdr->driver_status & 0xF0) {
1092 strlcat(msg, "SG_ERR_SUGGEST_RETRY", MAXERRDETAIL);
1095 strlcat(msg, "SG_ERR_SUGGEST_ABORT", MAXERRDETAIL);
1098 strlcat(msg, "SG_ERR_SUGGEST_REMAP", MAXERRDETAIL);
1101 strlcat(msg, "SG_ERR_SUGGEST_DIE", MAXERRDETAIL);
1104 strlcat(msg, "SG_ERR_SUGGEST_SENSE", MAXERRDETAIL);
1107 strlcat(msg, ". ", MAXERRDETAIL);
1110 snprintf(msgchunk, MAXMSGCHUNK, "SG SCSI Status: 0x%02x; ", hdr->status);
1111 strlcat(msg, msgchunk, MAXERRDETAIL);
1112 // SCSI 3 status codes
1113 switch (hdr->status) {
1115 strlcat(msg, "CHECK_CONDITION", MAXERRDETAIL);
1118 strlcat(msg, "CONDITION_MET", MAXERRDETAIL);
1121 strlcat(msg, "BUSY", MAXERRDETAIL);
1124 strlcat(msg, "INTERMEDIATE", MAXERRDETAIL);
1127 strlcat(msg, "INTERMEDIATE_CONDITION_MET", MAXERRDETAIL);
1130 strlcat(msg, "RESERVATION_CONFLICT", MAXERRDETAIL);
1133 strlcat(msg, "COMMAND_TERMINATED", MAXERRDETAIL);
1136 strlcat(msg, "TASK_SET_FULL", MAXERRDETAIL);
1139 strlcat(msg, "ACA_ACTIVE", MAXERRDETAIL);
1142 strlcat(msg, "TASK_ABORTED", MAXERRDETAIL);
1145 strlcat(msg, "Unknown", MAXERRDETAIL);
1148 strlcat(msg, ". ", MAXERRDETAIL);
1150 if (hdr->sb_len_wr) {
1151 snprintf(msgchunk, MAXMSGCHUNK, "Sense Data (%d bytes):", hdr->sb_len_wr);
1152 strlcat(msg, msgchunk, MAXERRDETAIL);
1153 for (i = 0; i < hdr->sb_len_wr; i++) {
1154 snprintf(msgchunk, MAXMSGCHUNK, " %02x", hdr->sbp[i]);
1155 strlcat(msg, msgchunk, MAXERRDETAIL);
1157 strlcat(msg, ". ", MAXERRDETAIL);
1159 if (hdr->resid != 0) {
1160 snprintf(msgchunk, MAXMSGCHUNK, "SG Driver: %d bytes out of %d not transferred. ", hdr->resid, hdr->dxfer_len);
1161 strlcat(msg, msgchunk, MAXERRDETAIL);
1164 strlcat(msg, "cdb:", MAXERRDETAIL);
1165 for (i = 0; i < hdr->cmd_len; i++) {
1166 snprintf(msgchunk, MAXMSGCHUNK, " %02x", hdr->cmdp[i]);
1167 strlcat(msg, msgchunk, MAXERRDETAIL);
1169 strlcat(msg, ". ", MAXERRDETAIL);
1170 if (io_u->ddir == DDIR_TRIM) {
1171 unsigned char *param_list = hdr->dxferp;
1172 strlcat(msg, "dxferp:", MAXERRDETAIL);
1173 for (i = 0; i < hdr->dxfer_len; i++) {
1174 snprintf(msgchunk, MAXMSGCHUNK, " %02x", param_list[i]);
1175 strlcat(msg, msgchunk, MAXERRDETAIL);
1177 strlcat(msg, ". ", MAXERRDETAIL);
1182 if (!(hdr->info & SG_INFO_CHECK) && !strlen(msg))
1183 strncpy(msg, "SG Driver did not report a Host, Driver or Device check",
1190 * get max file size from read capacity.
1192 static int fio_sgio_get_file_size(struct thread_data *td, struct fio_file *f)
1195 * get_file_size is being called even before sgio_init is
1196 * called, so none of the sg_io structures are
1197 * initialized in the thread_data yet. So we need to do the
1198 * ReadCapacity without any of those helpers. One of the effects
1199 * is that ReadCapacity may get called 4 times on each open:
1200 * readcap(10) followed by readcap(16) if needed - just to get
1201 * the file size after the init occurs - it will be called
1202 * again when "type_check" is called during structure
1203 * initialization I'm not sure how to prevent this little
1206 unsigned int bs = 0;
1207 unsigned long long max_lba = 0;
1210 if (fio_file_size_known(f))
1213 if (f->filetype != FIO_TYPE_BLOCK && f->filetype != FIO_TYPE_CHAR) {
1214 td_verror(td, EINVAL, "wrong file type");
1215 log_err("ioengine sg only works on block or character devices\n");
1219 ret = fio_sgio_read_capacity(td, &bs, &max_lba);
1221 td_verror(td, td->error, "fio_sgio_read_capacity");
1222 log_err("ioengine sg unable to successfully execute read capacity to get block size and maximum lba\n");
1226 f->real_file_size = (max_lba + 1) * bs;
1227 fio_file_set_size_known(f);
1232 static struct ioengine_ops ioengine = {
1234 .version = FIO_IOOPS_VERSION,
1235 .init = fio_sgio_init,
1236 .prep = fio_sgio_prep,
1237 .queue = fio_sgio_queue,
1238 .commit = fio_sgio_commit,
1239 .getevents = fio_sgio_getevents,
1240 .errdetails = fio_sgio_errdetails,
1241 .event = fio_sgio_event,
1242 .cleanup = fio_sgio_cleanup,
1243 .open_file = fio_sgio_open,
1244 .close_file = generic_close_file,
1245 .get_file_size = fio_sgio_get_file_size,
1246 .flags = FIO_SYNCIO | FIO_RAWIO,
1248 .option_struct_size = sizeof(struct sg_options)
1251 #else /* FIO_HAVE_SGIO */
1254 * When we have a proper configure system in place, we simply wont build
1255 * and install this io engine. For now install a crippled version that
1256 * just complains and fails to load.
1258 static int fio_sgio_init(struct thread_data fio_unused *td)
1260 log_err("fio: ioengine sg not available\n");
1264 static struct ioengine_ops ioengine = {
1266 .version = FIO_IOOPS_VERSION,
1267 .init = fio_sgio_init,
1272 static void fio_init fio_sgio_register(void)
1274 register_ioengine(&ioengine);
1277 static void fio_exit fio_sgio_unregister(void)
1279 unregister_ioengine(&ioengine);