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 uint16_t sgio_get_be16(uint8_t *buf)
162 return be16_to_cpu(*((uint16_t *) buf));
165 static inline uint32_t sgio_get_be32(uint8_t *buf)
167 return be32_to_cpu(*((uint32_t *) buf));
170 static inline uint64_t sgio_get_be64(uint8_t *buf)
172 return be64_to_cpu(*((uint64_t *) buf));
175 static inline void sgio_set_be16(uint16_t val, uint8_t *buf)
177 uint16_t t = cpu_to_be16(val);
179 memcpy(buf, &t, sizeof(uint16_t));
182 static inline void sgio_set_be32(uint32_t val, uint8_t *buf)
184 uint32_t t = cpu_to_be32(val);
186 memcpy(buf, &t, sizeof(uint32_t));
189 static inline void sgio_set_be64(uint64_t val, uint8_t *buf)
191 uint64_t t = cpu_to_be64(val);
193 memcpy(buf, &t, sizeof(uint64_t));
196 static inline bool sgio_unbuffered(struct thread_data *td)
198 return (td->o.odirect || td->o.sync_io);
201 static void sgio_hdr_init(struct sgio_data *sd, struct sg_io_hdr *hdr,
202 struct io_u *io_u, int fs)
204 struct sgio_cmd *sc = &sd->cmds[io_u->index];
206 memset(hdr, 0, sizeof(*hdr));
207 memset(sc->cdb, 0, sizeof(sc->cdb));
209 hdr->interface_id = 'S';
211 hdr->cmd_len = sizeof(sc->cdb);
213 hdr->mx_sb_len = sizeof(sc->sb);
214 hdr->pack_id = io_u->index;
216 hdr->timeout = SCSI_TIMEOUT_MS;
219 hdr->dxferp = io_u->xfer_buf;
220 hdr->dxfer_len = io_u->xfer_buflen;
224 static int pollin_events(struct pollfd *pfds, int fds)
228 for (i = 0; i < fds; i++)
229 if (pfds[i].revents & POLLIN)
235 static int sg_fd_read(int fd, void *data, size_t size)
242 ret = read(fd, data, size);
244 if (errno == EAGAIN || errno == EINTR)
264 static int fio_sgio_getevents(struct thread_data *td, unsigned int min,
266 const struct timespec fio_unused *t)
268 struct sgio_data *sd = td->io_ops_data;
269 int left = max, eventNum, ret, r = 0, trims = 0;
270 void *buf = sd->sgbuf;
271 unsigned int i, j, events;
276 * Fill in the file descriptors
278 for_each_file(td, f, i) {
280 * don't block for min events == 0
283 sd->fd_flags[i] = fio_set_fd_nonblocking(f->fd, "sg");
285 sd->fd_flags[i] = -1;
287 sd->pfds[i].fd = f->fd;
288 sd->pfds[i].events = POLLIN;
292 ** There are two counters here:
293 ** - number of SCSI commands completed
294 ** - number of io_us completed
296 ** These are the same with reads and writes, but
297 ** could differ with trim/unmap commands because
298 ** a single unmap can include multiple io_us
304 dprint(FD_IO, "sgio_getevents: sd %p: min=%d, max=%d, left=%d\n", sd, min, max, left);
310 ret = poll(sd->pfds, td->o.nr_files, -1);
314 td_verror(td, errno, "poll");
319 if (pollin_events(sd->pfds, td->o.nr_files))
329 for_each_file(td, f, i) {
330 for (eventNum = 0; eventNum < left; eventNum++) {
331 ret = sg_fd_read(f->fd, p, sizeof(struct sg_io_hdr));
332 dprint(FD_IO, "sgio_getevents: sg_fd_read ret: %d\n", ret);
335 td_verror(td, r, "sg_read");
338 io_u = ((struct sg_io_hdr *)p)->usr_ptr;
339 if (io_u->ddir == DDIR_TRIM) {
340 events += sd->trim_queues[io_u->index]->unmap_range_count;
341 eventNum += sd->trim_queues[io_u->index]->unmap_range_count - 1;
345 p += sizeof(struct sg_io_hdr);
346 dprint(FD_IO, "sgio_getevents: events: %d, eventNum: %d, left: %d\n", events, eventNum, left);
350 if (r < 0 && !events)
360 for (i = 0; i < events; i++) {
361 struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i;
362 sd->events[i + trims] = hdr->usr_ptr;
363 io_u = (struct io_u *)(hdr->usr_ptr);
365 if (hdr->info & SG_INFO_CHECK) {
366 /* record if an io error occurred, ignore resid */
367 memcpy(&io_u->hdr, hdr, sizeof(struct sg_io_hdr));
368 sd->events[i + trims]->error = EIO;
371 if (io_u->ddir == DDIR_TRIM) {
372 struct sgio_trim *st = sd->trim_queues[io_u->index];
373 #ifdef FIO_SGIO_DEBUG
374 assert(st->trim_io_us[0] == io_u);
375 assert(sd->trim_queue_map[io_u->index] == io_u->index);
376 dprint(FD_IO, "sgio_getevents: reaping %d io_us from trim queue %d\n", st->unmap_range_count, io_u->index);
377 dprint(FD_IO, "sgio_getevents: reaped io_u %d and stored in events[%d]\n", io_u->index, i+trims);
379 for (j = 1; j < st->unmap_range_count; j++) {
381 sd->events[i + trims] = st->trim_io_us[j];
382 #ifdef FIO_SGIO_DEBUG
383 dprint(FD_IO, "sgio_getevents: reaped io_u %d and stored in events[%d]\n", st->trim_io_us[j]->index, i+trims);
384 assert(sd->trim_queue_map[st->trim_io_us[j]->index] == io_u->index);
386 if (hdr->info & SG_INFO_CHECK) {
387 /* record if an io error occurred, ignore resid */
388 memcpy(&st->trim_io_us[j]->hdr, hdr, sizeof(struct sg_io_hdr));
389 sd->events[i + trims]->error = EIO;
392 events -= st->unmap_range_count - 1;
393 st->unmap_range_count = 0;
399 for_each_file(td, f, i) {
400 if (sd->fd_flags[i] == -1)
403 if (fcntl(f->fd, F_SETFL, sd->fd_flags[i]) < 0)
404 log_err("fio: sg failed to restore fcntl flags: %s\n", strerror(errno));
411 static enum fio_q_status fio_sgio_ioctl_doio(struct thread_data *td,
415 struct sgio_data *sd = td->io_ops_data;
416 struct sg_io_hdr *hdr = &io_u->hdr;
419 sd->events[0] = io_u;
421 ret = ioctl(f->fd, SG_IO, hdr);
425 /* record if an io error occurred */
426 if (hdr->info & SG_INFO_CHECK)
429 return FIO_Q_COMPLETED;
432 static enum fio_q_status fio_sgio_rw_doio(struct fio_file *f,
433 struct io_u *io_u, int do_sync)
435 struct sg_io_hdr *hdr = &io_u->hdr;
438 ret = write(f->fd, hdr, sizeof(*hdr));
443 ret = read(f->fd, hdr, sizeof(*hdr));
447 /* record if an io error occurred */
448 if (hdr->info & SG_INFO_CHECK)
451 return FIO_Q_COMPLETED;
457 static enum fio_q_status fio_sgio_doio(struct thread_data *td,
458 struct io_u *io_u, int do_sync)
460 struct fio_file *f = io_u->file;
461 enum fio_q_status ret;
463 if (f->filetype == FIO_TYPE_BLOCK) {
464 ret = fio_sgio_ioctl_doio(td, f, io_u);
465 td_verror(td, io_u->error, __func__);
467 ret = fio_sgio_rw_doio(f, io_u, do_sync);
469 td_verror(td, io_u->error, __func__);
475 static void fio_sgio_rw_lba(struct sg_io_hdr *hdr, unsigned long long lba,
476 unsigned long long nr_blocks)
478 if (lba < MAX_10B_LBA) {
479 sgio_set_be32((uint32_t) lba, &hdr->cmdp[2]);
480 sgio_set_be16((uint16_t) nr_blocks, &hdr->cmdp[7]);
482 sgio_set_be64(lba, &hdr->cmdp[2]);
483 sgio_set_be32((uint32_t) nr_blocks, &hdr->cmdp[10]);
489 static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u)
491 struct sg_io_hdr *hdr = &io_u->hdr;
492 struct sg_options *o = td->eo;
493 struct sgio_data *sd = td->io_ops_data;
494 unsigned long long nr_blocks, lba;
497 if (io_u->xfer_buflen & (sd->bs - 1)) {
498 log_err("read/write not sector aligned\n");
502 nr_blocks = io_u->xfer_buflen / sd->bs;
503 lba = io_u->offset / sd->bs;
505 if (io_u->ddir == DDIR_READ) {
506 sgio_hdr_init(sd, hdr, io_u, 1);
508 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
509 if (lba < MAX_10B_LBA)
510 hdr->cmdp[0] = 0x28; // read(10)
512 hdr->cmdp[0] = 0x88; // read(16)
515 hdr->cmdp[1] |= 0x08;
517 fio_sgio_rw_lba(hdr, lba, nr_blocks);
519 } else if (io_u->ddir == DDIR_WRITE) {
520 sgio_hdr_init(sd, hdr, io_u, 1);
522 hdr->dxfer_direction = SG_DXFER_TO_DEV;
523 switch(o->write_mode) {
525 if (lba < MAX_10B_LBA)
526 hdr->cmdp[0] = 0x2a; // write(10)
528 hdr->cmdp[0] = 0x8a; // write(16)
530 hdr->cmdp[1] |= 0x08;
532 case FIO_SG_WRITE_VERIFY:
533 if (lba < MAX_10B_LBA)
534 hdr->cmdp[0] = 0x2e; // write and verify(10)
536 hdr->cmdp[0] = 0x8e; // write and verify(16)
538 // BYTCHK is disabled by virtue of the memset in sgio_hdr_init
539 case FIO_SG_WRITE_SAME:
540 hdr->dxfer_len = sd->bs;
541 if (lba < MAX_10B_LBA)
542 hdr->cmdp[0] = 0x41; // write same(10)
544 hdr->cmdp[0] = 0x93; // write same(16)
548 fio_sgio_rw_lba(hdr, lba, nr_blocks);
550 } else if (io_u->ddir == DDIR_TRIM) {
551 struct sgio_trim *st;
553 if (sd->current_queue == -1) {
554 sgio_hdr_init(sd, hdr, io_u, 0);
557 hdr->dxfer_direction = SG_DXFER_TO_DEV;
558 hdr->cmdp[0] = 0x42; // unmap
559 sd->current_queue = io_u->index;
560 st = sd->trim_queues[sd->current_queue];
561 hdr->dxferp = st->unmap_param;
562 #ifdef FIO_SGIO_DEBUG
563 assert(sd->trim_queues[io_u->index]->unmap_range_count == 0);
564 dprint(FD_IO, "sg: creating new queue based on io_u %d\n", io_u->index);
568 st = sd->trim_queues[sd->current_queue];
570 dprint(FD_IO, "sg: adding io_u %d to trim queue %d\n", io_u->index, sd->current_queue);
571 st->trim_io_us[st->unmap_range_count] = io_u;
572 #ifdef FIO_SGIO_DEBUG
573 sd->trim_queue_map[io_u->index] = sd->current_queue;
576 offset = 8 + 16 * st->unmap_range_count;
577 sgio_set_be64(lba, &st->unmap_param[offset]);
578 sgio_set_be32((uint32_t) nr_blocks, &st->unmap_param[offset + 8]);
580 st->unmap_range_count++;
582 } else if (ddir_sync(io_u->ddir)) {
583 sgio_hdr_init(sd, hdr, io_u, 0);
584 hdr->dxfer_direction = SG_DXFER_NONE;
585 if (lba < MAX_10B_LBA)
586 hdr->cmdp[0] = 0x35; // synccache(10)
588 hdr->cmdp[0] = 0x91; // synccache(16)
595 static void fio_sgio_unmap_setup(struct sg_io_hdr *hdr, struct sgio_trim *st)
597 uint16_t cnt = st->unmap_range_count * 16;
599 hdr->dxfer_len = cnt + 8;
600 sgio_set_be16(cnt + 8, &hdr->cmdp[7]);
601 sgio_set_be16(cnt + 6, st->unmap_param);
602 sgio_set_be16(cnt, &st->unmap_param[2]);
607 static enum fio_q_status fio_sgio_queue(struct thread_data *td,
610 struct sg_io_hdr *hdr = &io_u->hdr;
611 struct sgio_data *sd = td->io_ops_data;
612 int ret, do_sync = 0;
614 fio_ro_check(td, io_u);
616 if (sgio_unbuffered(td) || ddir_sync(io_u->ddir))
619 if (io_u->ddir == DDIR_TRIM) {
620 if (do_sync || io_u->file->filetype == FIO_TYPE_BLOCK) {
621 struct sgio_trim *st = sd->trim_queues[sd->current_queue];
623 /* finish cdb setup for unmap because we are
624 ** doing unmap commands synchronously */
625 #ifdef FIO_SGIO_DEBUG
626 assert(st->unmap_range_count == 1);
627 assert(io_u == st->trim_io_us[0]);
631 fio_sgio_unmap_setup(hdr, st);
633 st->unmap_range_count = 0;
634 sd->current_queue = -1;
636 /* queue up trim ranges and submit in commit() */
640 ret = fio_sgio_doio(td, io_u, do_sync);
644 else if (hdr->status) {
645 io_u->resid = hdr->resid;
647 } else if (td->io_ops->commit != NULL) {
648 if (do_sync && !ddir_sync(io_u->ddir)) {
649 io_u_mark_submit(td, 1);
650 io_u_mark_complete(td, 1);
651 } else if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
652 io_u_mark_submit(td, 1);
653 io_u_queued(td, io_u);
658 td_verror(td, io_u->error, "xfer");
659 return FIO_Q_COMPLETED;
665 static int fio_sgio_commit(struct thread_data *td)
667 struct sgio_data *sd = td->io_ops_data;
668 struct sgio_trim *st;
670 struct sg_io_hdr *hdr;
675 if (sd->current_queue == -1)
678 st = sd->trim_queues[sd->current_queue];
679 io_u = st->trim_io_us[0];
682 fio_sgio_unmap_setup(hdr, st);
684 sd->current_queue = -1;
686 ret = fio_sgio_rw_doio(io_u->file, io_u, 0);
688 if (ret < 0 || hdr->status) {
698 for (i = 0; i < st->unmap_range_count; i++) {
699 st->trim_io_us[i]->error = error;
700 clear_io_u(td, st->trim_io_us[i]);
702 st->trim_io_us[i]->resid = hdr->resid;
705 td_verror(td, error, "xfer");
709 if (fio_fill_issue_time(td)) {
710 fio_gettime(&now, NULL);
711 for (i = 0; i < st->unmap_range_count; i++) {
712 memcpy(&st->trim_io_us[i]->issue_time, &now, sizeof(now));
713 io_u_queued(td, io_u);
716 io_u_mark_submit(td, st->unmap_range_count);
721 static struct io_u *fio_sgio_event(struct thread_data *td, int event)
723 struct sgio_data *sd = td->io_ops_data;
725 return sd->events[event];
728 static int fio_sgio_read_capacity(struct thread_data *td, unsigned int *bs,
729 unsigned long long *max_lba)
732 * need to do read capacity operation w/o benefit of sd or
733 * io_u structures, which are not initialized until later.
735 struct sg_io_hdr hdr;
736 unsigned long long hlba;
737 unsigned int blksz = 0;
738 unsigned char cmd[16];
739 unsigned char sb[64];
740 unsigned char buf[32]; // read capacity return
744 struct fio_file *f = td->files[0];
746 /* open file independent of rest of application */
747 fd = open(f->file_name, O_RDONLY);
751 memset(&hdr, 0, sizeof(hdr));
752 memset(cmd, 0, sizeof(cmd));
753 memset(sb, 0, sizeof(sb));
754 memset(buf, 0, sizeof(buf));
756 /* First let's try a 10 byte read capacity. */
757 hdr.interface_id = 'S';
761 hdr.mx_sb_len = sizeof(sb);
762 hdr.timeout = SCSI_TIMEOUT_MS;
763 hdr.cmdp[0] = 0x25; // Read Capacity(10)
764 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
766 hdr.dxfer_len = sizeof(buf);
768 ret = ioctl(fd, SG_IO, &hdr);
774 if (hdr.info & SG_INFO_CHECK) {
775 /* RCAP(10) might be unsupported by device. Force RCAP(16) */
778 blksz = sgio_get_be32(&buf[4]);
779 hlba = sgio_get_be32(buf);
783 * If max lba masked by MAX_10B_LBA equals MAX_10B_LBA,
784 * then need to retry with 16 byte Read Capacity command.
786 if (hlba == MAX_10B_LBA) {
788 hdr.cmdp[0] = 0x9e; // service action
789 hdr.cmdp[1] = 0x10; // Read Capacity(16)
790 sgio_set_be32(sizeof(buf), &hdr.cmdp[10]);
792 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
794 hdr.dxfer_len = sizeof(buf);
796 ret = ioctl(fd, SG_IO, &hdr);
802 /* record if an io error occurred */
803 if (hdr.info & SG_INFO_CHECK)
804 td_verror(td, EIO, "fio_sgio_read_capacity");
806 blksz = sgio_get_be32(&buf[8]);
807 hlba = sgio_get_be64(buf);
822 static void fio_sgio_cleanup(struct thread_data *td)
824 struct sgio_data *sd = td->io_ops_data;
833 #ifdef FIO_SGIO_DEBUG
834 free(sd->trim_queue_map);
837 for (i = 0; i < td->o.iodepth; i++) {
838 free(sd->trim_queues[i]->unmap_param);
839 free(sd->trim_queues[i]->trim_io_us);
840 free(sd->trim_queues[i]);
843 free(sd->trim_queues);
848 static int fio_sgio_init(struct thread_data *td)
850 struct sgio_data *sd;
851 struct sgio_trim *st;
854 sd = calloc(1, sizeof(*sd));
855 sd->cmds = calloc(td->o.iodepth, sizeof(struct sgio_cmd));
856 sd->sgbuf = calloc(td->o.iodepth, sizeof(struct sg_io_hdr));
857 sd->events = calloc(td->o.iodepth, sizeof(struct io_u *));
858 sd->pfds = calloc(td->o.nr_files, sizeof(struct pollfd));
859 sd->fd_flags = calloc(td->o.nr_files, sizeof(int));
860 sd->type_checked = 0;
862 sd->trim_queues = calloc(td->o.iodepth, sizeof(struct sgio_trim *));
863 sd->current_queue = -1;
864 #ifdef FIO_SGIO_DEBUG
865 sd->trim_queue_map = calloc(td->o.iodepth, sizeof(int));
867 for (i = 0; i < td->o.iodepth; i++) {
868 sd->trim_queues[i] = calloc(1, sizeof(struct sgio_trim));
869 st = sd->trim_queues[i];
870 st->unmap_param = calloc(td->o.iodepth + 1, sizeof(char[16]));
871 st->unmap_range_count = 0;
872 st->trim_io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
875 td->io_ops_data = sd;
878 * we want to do it, regardless of whether odirect is set or not
880 td->o.override_sync = 1;
884 static int fio_sgio_type_check(struct thread_data *td, struct fio_file *f)
886 struct sgio_data *sd = td->io_ops_data;
888 unsigned long long max_lba = 0;
890 if (f->filetype == FIO_TYPE_BLOCK) {
891 if (ioctl(f->fd, BLKSSZGET, &bs) < 0) {
892 td_verror(td, errno, "ioctl");
895 } else if (f->filetype == FIO_TYPE_CHAR) {
898 if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) {
899 td_verror(td, errno, "ioctl");
903 ret = fio_sgio_read_capacity(td, &bs, &max_lba);
905 td_verror(td, td->error, "fio_sgio_read_capacity");
906 log_err("ioengine sg unable to read capacity successfully\n");
910 td_verror(td, EINVAL, "wrong file type");
911 log_err("ioengine sg only works on block or character devices\n");
916 // Determine size of commands needed based on max_lba
917 if (max_lba >= MAX_10B_LBA) {
918 dprint(FD_IO, "sgio_type_check: using 16 byte read/write "
919 "commands for lba above 0x%016llx/0x%016llx\n",
920 MAX_10B_LBA, max_lba);
923 if (f->filetype == FIO_TYPE_BLOCK) {
924 td->io_ops->getevents = NULL;
925 td->io_ops->event = NULL;
926 td->io_ops->commit = NULL;
928 ** Setting these functions to null may cause problems
929 ** with filename=/dev/sda:/dev/sg0 since we are only
930 ** considering a single file
933 sd->type_checked = 1;
938 static int fio_sgio_open(struct thread_data *td, struct fio_file *f)
940 struct sgio_data *sd = td->io_ops_data;
943 ret = generic_open_file(td, f);
947 if (sd && !sd->type_checked && fio_sgio_type_check(td, f)) {
948 ret = generic_close_file(td, f);
956 * Build an error string with details about the driver, host or scsi
957 * error contained in the sg header Caller will use as necessary.
959 static char *fio_sgio_errdetails(struct io_u *io_u)
961 struct sg_io_hdr *hdr = &io_u->hdr;
962 #define MAXERRDETAIL 1024
963 #define MAXMSGCHUNK 128
964 char *msg, msgchunk[MAXMSGCHUNK];
967 msg = calloc(1, MAXERRDETAIL);
971 * can't seem to find sg_err.h, so I'll just echo the define values
972 * so others can search on internet to find clearer clues of meaning.
974 if (hdr->info & SG_INFO_CHECK) {
975 if (hdr->host_status) {
976 snprintf(msgchunk, MAXMSGCHUNK, "SG Host Status: 0x%02x; ", hdr->host_status);
977 strlcat(msg, msgchunk, MAXERRDETAIL);
978 switch (hdr->host_status) {
980 strlcat(msg, "SG_ERR_DID_NO_CONNECT", MAXERRDETAIL);
983 strlcat(msg, "SG_ERR_DID_BUS_BUSY", MAXERRDETAIL);
986 strlcat(msg, "SG_ERR_DID_TIME_OUT", MAXERRDETAIL);
989 strlcat(msg, "SG_ERR_DID_BAD_TARGET", MAXERRDETAIL);
992 strlcat(msg, "SG_ERR_DID_ABORT", MAXERRDETAIL);
995 strlcat(msg, "SG_ERR_DID_PARITY", MAXERRDETAIL);
998 strlcat(msg, "SG_ERR_DID_ERROR (internal error)", MAXERRDETAIL);
1001 strlcat(msg, "SG_ERR_DID_RESET", MAXERRDETAIL);
1004 strlcat(msg, "SG_ERR_DID_BAD_INTR (unexpected)", MAXERRDETAIL);
1007 strlcat(msg, "SG_ERR_DID_PASSTHROUGH", MAXERRDETAIL);
1010 strlcat(msg, "SG_ERR_DID_SOFT_ERROR (driver retry?)", MAXERRDETAIL);
1013 strlcat(msg, "SG_ERR_DID_IMM_RETRY", MAXERRDETAIL);
1016 strlcat(msg, "SG_ERR_DID_REQUEUE", MAXERRDETAIL);
1019 strlcat(msg, "SG_ERR_DID_TRANSPORT_DISRUPTED", MAXERRDETAIL);
1022 strlcat(msg, "SG_ERR_DID_TRANSPORT_FAILFAST", MAXERRDETAIL);
1025 strlcat(msg, "SG_ERR_DID_TARGET_FAILURE", MAXERRDETAIL);
1028 strlcat(msg, "SG_ERR_DID_NEXUS_FAILURE", MAXERRDETAIL);
1031 strlcat(msg, "SG_ERR_DID_ALLOC_FAILURE", MAXERRDETAIL);
1034 strlcat(msg, "SG_ERR_DID_MEDIUM_ERROR", MAXERRDETAIL);
1037 strlcat(msg, "Unknown", MAXERRDETAIL);
1040 strlcat(msg, ". ", MAXERRDETAIL);
1042 if (hdr->driver_status) {
1043 snprintf(msgchunk, MAXMSGCHUNK, "SG Driver Status: 0x%02x; ", hdr->driver_status);
1044 strlcat(msg, msgchunk, MAXERRDETAIL);
1045 switch (hdr->driver_status & 0x0F) {
1047 strlcat(msg, "SG_ERR_DRIVER_BUSY", MAXERRDETAIL);
1050 strlcat(msg, "SG_ERR_DRIVER_SOFT", MAXERRDETAIL);
1053 strlcat(msg, "SG_ERR_DRIVER_MEDIA", MAXERRDETAIL);
1056 strlcat(msg, "SG_ERR_DRIVER_ERROR", MAXERRDETAIL);
1059 strlcat(msg, "SG_ERR_DRIVER_INVALID", MAXERRDETAIL);
1062 strlcat(msg, "SG_ERR_DRIVER_TIMEOUT", MAXERRDETAIL);
1065 strlcat(msg, "SG_ERR_DRIVER_HARD", MAXERRDETAIL);
1068 strlcat(msg, "SG_ERR_DRIVER_SENSE", MAXERRDETAIL);
1071 strlcat(msg, "Unknown", MAXERRDETAIL);
1074 strlcat(msg, "; ", MAXERRDETAIL);
1075 switch (hdr->driver_status & 0xF0) {
1077 strlcat(msg, "SG_ERR_SUGGEST_RETRY", MAXERRDETAIL);
1080 strlcat(msg, "SG_ERR_SUGGEST_ABORT", MAXERRDETAIL);
1083 strlcat(msg, "SG_ERR_SUGGEST_REMAP", MAXERRDETAIL);
1086 strlcat(msg, "SG_ERR_SUGGEST_DIE", MAXERRDETAIL);
1089 strlcat(msg, "SG_ERR_SUGGEST_SENSE", MAXERRDETAIL);
1092 strlcat(msg, ". ", MAXERRDETAIL);
1095 snprintf(msgchunk, MAXMSGCHUNK, "SG SCSI Status: 0x%02x; ", hdr->status);
1096 strlcat(msg, msgchunk, MAXERRDETAIL);
1097 // SCSI 3 status codes
1098 switch (hdr->status) {
1100 strlcat(msg, "CHECK_CONDITION", MAXERRDETAIL);
1103 strlcat(msg, "CONDITION_MET", MAXERRDETAIL);
1106 strlcat(msg, "BUSY", MAXERRDETAIL);
1109 strlcat(msg, "INTERMEDIATE", MAXERRDETAIL);
1112 strlcat(msg, "INTERMEDIATE_CONDITION_MET", MAXERRDETAIL);
1115 strlcat(msg, "RESERVATION_CONFLICT", MAXERRDETAIL);
1118 strlcat(msg, "COMMAND_TERMINATED", MAXERRDETAIL);
1121 strlcat(msg, "TASK_SET_FULL", MAXERRDETAIL);
1124 strlcat(msg, "ACA_ACTIVE", MAXERRDETAIL);
1127 strlcat(msg, "TASK_ABORTED", MAXERRDETAIL);
1130 strlcat(msg, "Unknown", MAXERRDETAIL);
1133 strlcat(msg, ". ", MAXERRDETAIL);
1135 if (hdr->sb_len_wr) {
1136 snprintf(msgchunk, MAXMSGCHUNK, "Sense Data (%d bytes):", hdr->sb_len_wr);
1137 strlcat(msg, msgchunk, MAXERRDETAIL);
1138 for (i = 0; i < hdr->sb_len_wr; i++) {
1139 snprintf(msgchunk, MAXMSGCHUNK, " %02x", hdr->sbp[i]);
1140 strlcat(msg, msgchunk, MAXERRDETAIL);
1142 strlcat(msg, ". ", MAXERRDETAIL);
1144 if (hdr->resid != 0) {
1145 snprintf(msgchunk, MAXMSGCHUNK, "SG Driver: %d bytes out of %d not transferred. ", hdr->resid, hdr->dxfer_len);
1146 strlcat(msg, msgchunk, MAXERRDETAIL);
1149 strlcat(msg, "cdb:", MAXERRDETAIL);
1150 for (i = 0; i < hdr->cmd_len; i++) {
1151 snprintf(msgchunk, MAXMSGCHUNK, " %02x", hdr->cmdp[i]);
1152 strlcat(msg, msgchunk, MAXERRDETAIL);
1154 strlcat(msg, ". ", MAXERRDETAIL);
1155 if (io_u->ddir == DDIR_TRIM) {
1156 unsigned char *param_list = hdr->dxferp;
1157 strlcat(msg, "dxferp:", MAXERRDETAIL);
1158 for (i = 0; i < hdr->dxfer_len; i++) {
1159 snprintf(msgchunk, MAXMSGCHUNK, " %02x", param_list[i]);
1160 strlcat(msg, msgchunk, MAXERRDETAIL);
1162 strlcat(msg, ". ", MAXERRDETAIL);
1167 if (!(hdr->info & SG_INFO_CHECK) && !strlen(msg))
1168 strncpy(msg, "SG Driver did not report a Host, Driver or Device check",
1175 * get max file size from read capacity.
1177 static int fio_sgio_get_file_size(struct thread_data *td, struct fio_file *f)
1180 * get_file_size is being called even before sgio_init is
1181 * called, so none of the sg_io structures are
1182 * initialized in the thread_data yet. So we need to do the
1183 * ReadCapacity without any of those helpers. One of the effects
1184 * is that ReadCapacity may get called 4 times on each open:
1185 * readcap(10) followed by readcap(16) if needed - just to get
1186 * the file size after the init occurs - it will be called
1187 * again when "type_check" is called during structure
1188 * initialization I'm not sure how to prevent this little
1191 unsigned int bs = 0;
1192 unsigned long long max_lba = 0;
1195 if (fio_file_size_known(f))
1198 if (f->filetype != FIO_TYPE_BLOCK && f->filetype != FIO_TYPE_CHAR) {
1199 td_verror(td, EINVAL, "wrong file type");
1200 log_err("ioengine sg only works on block or character devices\n");
1204 ret = fio_sgio_read_capacity(td, &bs, &max_lba);
1206 td_verror(td, td->error, "fio_sgio_read_capacity");
1207 log_err("ioengine sg unable to successfully execute read capacity to get block size and maximum lba\n");
1211 f->real_file_size = (max_lba + 1) * bs;
1212 fio_file_set_size_known(f);
1217 static struct ioengine_ops ioengine = {
1219 .version = FIO_IOOPS_VERSION,
1220 .init = fio_sgio_init,
1221 .prep = fio_sgio_prep,
1222 .queue = fio_sgio_queue,
1223 .commit = fio_sgio_commit,
1224 .getevents = fio_sgio_getevents,
1225 .errdetails = fio_sgio_errdetails,
1226 .event = fio_sgio_event,
1227 .cleanup = fio_sgio_cleanup,
1228 .open_file = fio_sgio_open,
1229 .close_file = generic_close_file,
1230 .get_file_size = fio_sgio_get_file_size,
1231 .flags = FIO_SYNCIO | FIO_RAWIO,
1233 .option_struct_size = sizeof(struct sg_options)
1236 #else /* FIO_HAVE_SGIO */
1239 * When we have a proper configure system in place, we simply wont build
1240 * and install this io engine. For now install a crippled version that
1241 * just complains and fails to load.
1243 static int fio_sgio_init(struct thread_data fio_unused *td)
1245 log_err("fio: ioengine sg not available\n");
1249 static struct ioengine_ops ioengine = {
1251 .version = FIO_IOOPS_VERSION,
1252 .init = fio_sgio_init,
1257 static void fio_init fio_sgio_register(void)
1259 register_ioengine(&ioengine);
1262 static void fio_exit fio_sgio_unregister(void)
1264 unregister_ioengine(&ioengine);