3c4e986de734f443598467708f7c91b1c03f6bcb
[fio.git] / engines / sg.c
1 /*
2  * sg engine
3  *
4  * IO engine that uses the Linux SG v3 interface to talk to SCSI devices
5  *
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
10  *
11  * What value does queue() return for the different cases?
12  *                              queue() return value
13  * In sync mode:
14  *  /dev/sdX            RWT     FIO_Q_COMPLETED
15  *  /dev/sgY            RWT     FIO_Q_COMPLETED
16  *   with direct=1 or sync=1
17  *
18  * In async mode:
19  *  /dev/sgY            RWT     FIO_Q_QUEUED
20  *   direct=0 and sync=0
21  *
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()
24  *
25  * Where are the IO counting functions called for the different cases?
26  *
27  * In sync mode:
28  *  /dev/sdX (commit==NULL)
29  *   RWT
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()
33  *
34  *  /dev/sgY with direct=1 or sync=1 (commit does nothing)
35  *   RWT
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()
39  *  
40  * In async mode:
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()
50  *
51  */
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <unistd.h>
55 #include <errno.h>
56 #include <poll.h>
57
58 #include "../fio.h"
59 #include "../optgroup.h"
60
61 #ifdef FIO_HAVE_SGIO
62
63 #ifndef SGV4_FLAG_HIPRI
64 #define SGV4_FLAG_HIPRI 0x800
65 #endif
66
67 enum {
68         FIO_SG_WRITE            = 1,
69         FIO_SG_WRITE_VERIFY,
70         FIO_SG_WRITE_SAME,
71         FIO_SG_WRITE_SAME_NDOB,
72         FIO_SG_VERIFY_BYTCHK_00,
73         FIO_SG_VERIFY_BYTCHK_01,
74         FIO_SG_VERIFY_BYTCHK_11,
75 };
76
77 struct sg_options {
78         void *pad;
79         unsigned int hipri;
80         unsigned int readfua;
81         unsigned int writefua;
82         unsigned int write_mode;
83 };
84
85 static struct fio_option options[] = {
86         {
87                 .name   = "hipri",
88                 .lname  = "High Priority",
89                 .type   = FIO_OPT_STR_SET,
90                 .off1   = offsetof(struct sg_options, hipri),
91                 .help   = "Use polled IO completions",
92                 .category = FIO_OPT_C_ENGINE,
93                 .group  = FIO_OPT_G_SG,
94         },
95         {
96                 .name   = "readfua",
97                 .lname  = "sg engine read fua flag support",
98                 .type   = FIO_OPT_BOOL,
99                 .off1   = offsetof(struct sg_options, readfua),
100                 .help   = "Set FUA flag (force unit access) for all Read operations",
101                 .def    = "0",
102                 .category = FIO_OPT_C_ENGINE,
103                 .group  = FIO_OPT_G_SG,
104         },
105         {
106                 .name   = "writefua",
107                 .lname  = "sg engine write fua flag support",
108                 .type   = FIO_OPT_BOOL,
109                 .off1   = offsetof(struct sg_options, writefua),
110                 .help   = "Set FUA flag (force unit access) for all Write operations",
111                 .def    = "0",
112                 .category = FIO_OPT_C_ENGINE,
113                 .group  = FIO_OPT_G_SG,
114         },
115         {
116                 .name   = "sg_write_mode",
117                 .lname  = "specify sg write mode",
118                 .type   = FIO_OPT_STR,
119                 .off1   = offsetof(struct sg_options, write_mode),
120                 .help   = "Specify SCSI WRITE mode",
121                 .def    = "write",
122                 .posval = {
123                           { .ival = "write",
124                             .oval = FIO_SG_WRITE,
125                             .help = "Issue standard SCSI WRITE commands",
126                           },
127                           { .ival = "verify",
128                             .oval = FIO_SG_WRITE_VERIFY,
129                             .help = "Issue SCSI WRITE AND VERIFY commands",
130                           },
131                           { .ival = "same",
132                             .oval = FIO_SG_WRITE_SAME,
133                             .help = "Issue SCSI WRITE SAME commands",
134                           },
135                           { .ival = "write_same_ndob",
136                             .oval = FIO_SG_WRITE_SAME_NDOB,
137                             .help = "Issue SCSI WRITE SAME(16) commands with NDOB flag set",
138                           },
139                           { .ival = "verify_bytchk_00",
140                             .oval = FIO_SG_VERIFY_BYTCHK_00,
141                             .help = "Issue SCSI VERIFY commands with BYTCHK set to 00",
142                           },
143                           { .ival = "verify_bytchk_01",
144                             .oval = FIO_SG_VERIFY_BYTCHK_01,
145                             .help = "Issue SCSI VERIFY commands with BYTCHK set to 01",
146                           },
147                           { .ival = "verify_bytchk_11",
148                             .oval = FIO_SG_VERIFY_BYTCHK_11,
149                             .help = "Issue SCSI VERIFY commands with BYTCHK set to 11",
150                           },
151                 },
152                 .category = FIO_OPT_C_ENGINE,
153                 .group  = FIO_OPT_G_SG,
154         },
155         {
156                 .name   = NULL,
157         },
158 };
159
160 #define MAX_10B_LBA  0xFFFFFFFFULL
161 #define SCSI_TIMEOUT_MS 30000   // 30 second timeout; currently no method to override
162 #define MAX_SB 64               // sense block maximum return size
163 /*
164 #define FIO_SGIO_DEBUG
165 */
166
167 struct sgio_cmd {
168         unsigned char cdb[16];      // enhanced from 10 to support 16 byte commands
169         unsigned char sb[MAX_SB];   // add sense block to commands
170         int nr;
171 };
172
173 struct sgio_trim {
174         uint8_t *unmap_param;
175         unsigned int unmap_range_count;
176         struct io_u **trim_io_us;
177 };
178
179 struct sgio_data {
180         struct sgio_cmd *cmds;
181         struct io_u **events;
182         struct pollfd *pfds;
183         int *fd_flags;
184         void *sgbuf;
185         unsigned int bs;
186         int type_checked;
187         struct sgio_trim **trim_queues;
188         int current_queue;
189 #ifdef FIO_SGIO_DEBUG
190         unsigned int *trim_queue_map;
191 #endif
192 };
193
194 static inline uint32_t sgio_get_be32(uint8_t *buf)
195 {
196         return be32_to_cpu(*((uint32_t *) buf));
197 }
198
199 static inline uint64_t sgio_get_be64(uint8_t *buf)
200 {
201         return be64_to_cpu(*((uint64_t *) buf));
202 }
203
204 static inline void sgio_set_be16(uint16_t val, uint8_t *buf)
205 {
206         uint16_t t = cpu_to_be16(val);
207
208         memcpy(buf, &t, sizeof(uint16_t));
209 }
210
211 static inline void sgio_set_be32(uint32_t val, uint8_t *buf)
212 {
213         uint32_t t = cpu_to_be32(val);
214
215         memcpy(buf, &t, sizeof(uint32_t));
216 }
217
218 static inline void sgio_set_be64(uint64_t val, uint8_t *buf)
219 {
220         uint64_t t = cpu_to_be64(val);
221
222         memcpy(buf, &t, sizeof(uint64_t));
223 }
224
225 static inline bool sgio_unbuffered(struct thread_data *td)
226 {
227         return (td->o.odirect || td->o.sync_io);
228 }
229
230 static void sgio_hdr_init(struct sgio_data *sd, struct sg_io_hdr *hdr,
231                           struct io_u *io_u, int fs)
232 {
233         struct sgio_cmd *sc = &sd->cmds[io_u->index];
234
235         memset(hdr, 0, sizeof(*hdr));
236         memset(sc->cdb, 0, sizeof(sc->cdb));
237
238         hdr->interface_id = 'S';
239         hdr->cmdp = sc->cdb;
240         hdr->cmd_len = sizeof(sc->cdb);
241         hdr->sbp = sc->sb;
242         hdr->mx_sb_len = sizeof(sc->sb);
243         hdr->pack_id = io_u->index;
244         hdr->usr_ptr = io_u;
245         hdr->timeout = SCSI_TIMEOUT_MS;
246
247         if (fs) {
248                 hdr->dxferp = io_u->xfer_buf;
249                 hdr->dxfer_len = io_u->xfer_buflen;
250         }
251 }
252
253 static int pollin_events(struct pollfd *pfds, int fds)
254 {
255         int i;
256
257         for (i = 0; i < fds; i++)
258                 if (pfds[i].revents & POLLIN)
259                         return 1;
260
261         return 0;
262 }
263
264 static int sg_fd_read(int fd, void *data, size_t size)
265 {
266         int err = 0;
267
268         while (size) {
269                 ssize_t ret;
270
271                 ret = read(fd, data, size);
272                 if (ret < 0) {
273                         if (errno == EAGAIN || errno == EINTR)
274                                 continue;
275                         err = errno;
276                         break;
277                 } else if (!ret)
278                         break;
279                 else {
280                         data += ret;
281                         size -= ret;
282                 }
283         }
284
285         if (err)
286                 return err;
287         if (size)
288                 return EAGAIN;
289
290         return 0;
291 }
292
293 static int fio_sgio_getevents(struct thread_data *td, unsigned int min,
294                               unsigned int max,
295                               const struct timespec fio_unused *t)
296 {
297         struct sgio_data *sd = td->io_ops_data;
298         int left = max, eventNum, ret, r = 0, trims = 0;
299         void *buf = sd->sgbuf;
300         unsigned int i, j, events;
301         struct fio_file *f;
302         struct io_u *io_u;
303
304         /*
305          * Fill in the file descriptors
306          */
307         for_each_file(td, f, i) {
308                 /*
309                  * don't block for min events == 0
310                  */
311                 if (!min)
312                         sd->fd_flags[i] = fio_set_fd_nonblocking(f->fd, "sg");
313                 else
314                         sd->fd_flags[i] = -1;
315
316                 sd->pfds[i].fd = f->fd;
317                 sd->pfds[i].events = POLLIN;
318         }
319
320         /*
321         ** There are two counters here:
322         **  - number of SCSI commands completed
323         **  - number of io_us completed
324         **
325         ** These are the same with reads and writes, but
326         ** could differ with trim/unmap commands because
327         ** a single unmap can include multiple io_us
328         */
329
330         while (left > 0) {
331                 char *p;
332
333                 dprint(FD_IO, "sgio_getevents: sd %p: min=%d, max=%d, left=%d\n", sd, min, max, left);
334
335                 do {
336                         if (!min)
337                                 break;
338
339                         ret = poll(sd->pfds, td->o.nr_files, -1);
340                         if (ret < 0) {
341                                 if (!r)
342                                         r = -errno;
343                                 td_verror(td, errno, "poll");
344                                 break;
345                         } else if (!ret)
346                                 continue;
347
348                         if (pollin_events(sd->pfds, td->o.nr_files))
349                                 break;
350                 } while (1);
351
352                 if (r < 0)
353                         break;
354
355 re_read:
356                 p = buf;
357                 events = 0;
358                 for_each_file(td, f, i) {
359                         for (eventNum = 0; eventNum < left; eventNum++) {
360                                 ret = sg_fd_read(f->fd, p, sizeof(struct sg_io_hdr));
361                                 dprint(FD_IO, "sgio_getevents: sg_fd_read ret: %d\n", ret);
362                                 if (ret) {
363                                         r = -ret;
364                                         td_verror(td, r, "sg_read");
365                                         break;
366                                 }
367                                 io_u = ((struct sg_io_hdr *)p)->usr_ptr;
368                                 if (io_u->ddir == DDIR_TRIM) {
369                                         events += sd->trim_queues[io_u->index]->unmap_range_count;
370                                         eventNum += sd->trim_queues[io_u->index]->unmap_range_count - 1;
371                                 } else
372                                         events++;
373
374                                 p += sizeof(struct sg_io_hdr);
375                                 dprint(FD_IO, "sgio_getevents: events: %d, eventNum: %d, left: %d\n", events, eventNum, left);
376                         }
377                 }
378
379                 if (r < 0 && !events)
380                         break;
381                 if (!events) {
382                         usleep(1000);
383                         goto re_read;
384                 }
385
386                 left -= events;
387                 r += events;
388
389                 for (i = 0; i < events; i++) {
390                         struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i;
391                         sd->events[i + trims] = hdr->usr_ptr;
392                         io_u = (struct io_u *)(hdr->usr_ptr);
393
394                         if (hdr->info & SG_INFO_CHECK) {
395                                 /* record if an io error occurred, ignore resid */
396                                 memcpy(&io_u->hdr, hdr, sizeof(struct sg_io_hdr));
397                                 sd->events[i + trims]->error = EIO;
398                         }
399
400                         if (io_u->ddir == DDIR_TRIM) {
401                                 struct sgio_trim *st = sd->trim_queues[io_u->index];
402 #ifdef FIO_SGIO_DEBUG
403                                 assert(st->trim_io_us[0] == io_u);
404                                 assert(sd->trim_queue_map[io_u->index] == io_u->index);
405                                 dprint(FD_IO, "sgio_getevents: reaping %d io_us from trim queue %d\n", st->unmap_range_count, io_u->index);
406                                 dprint(FD_IO, "sgio_getevents: reaped io_u %d and stored in events[%d]\n", io_u->index, i+trims);
407 #endif
408                                 for (j = 1; j < st->unmap_range_count; j++) {
409                                         ++trims;
410                                         sd->events[i + trims] = st->trim_io_us[j];
411 #ifdef FIO_SGIO_DEBUG
412                                         dprint(FD_IO, "sgio_getevents: reaped io_u %d and stored in events[%d]\n", st->trim_io_us[j]->index, i+trims);
413                                         assert(sd->trim_queue_map[st->trim_io_us[j]->index] == io_u->index);
414 #endif
415                                         if (hdr->info & SG_INFO_CHECK) {
416                                                 /* record if an io error occurred, ignore resid */
417                                                 memcpy(&st->trim_io_us[j]->hdr, hdr, sizeof(struct sg_io_hdr));
418                                                 sd->events[i + trims]->error = EIO;
419                                         }
420                                 }
421                                 events -= st->unmap_range_count - 1;
422                                 st->unmap_range_count = 0;
423                         }
424                 }
425         }
426
427         if (!min) {
428                 for_each_file(td, f, i) {
429                         if (sd->fd_flags[i] == -1)
430                                 continue;
431
432                         if (fcntl(f->fd, F_SETFL, sd->fd_flags[i]) < 0)
433                                 log_err("fio: sg failed to restore fcntl flags: %s\n", strerror(errno));
434                 }
435         }
436
437         return r;
438 }
439
440 static enum fio_q_status fio_sgio_ioctl_doio(struct thread_data *td,
441                                              struct fio_file *f,
442                                              struct io_u *io_u)
443 {
444         struct sgio_data *sd = td->io_ops_data;
445         struct sg_io_hdr *hdr = &io_u->hdr;
446         int ret;
447
448         sd->events[0] = io_u;
449
450         ret = ioctl(f->fd, SG_IO, hdr);
451         if (ret < 0)
452                 return ret;
453
454         /* record if an io error occurred */
455         if (hdr->info & SG_INFO_CHECK)
456                 io_u->error = EIO;
457
458         return FIO_Q_COMPLETED;
459 }
460
461 static enum fio_q_status fio_sgio_rw_doio(struct thread_data *td,
462                                           struct fio_file *f,
463                                           struct io_u *io_u, int do_sync)
464 {
465         struct sg_io_hdr *hdr = &io_u->hdr;
466         int ret;
467
468         ret = write(f->fd, hdr, sizeof(*hdr));
469         if (ret < 0)
470                 return ret;
471
472         if (do_sync) {
473                 /*
474                  * We can't just read back the first command that completes
475                  * and assume it's the one we need, it could be any command
476                  * that is inflight.
477                  */
478                 do {
479                         struct io_u *__io_u;
480
481                         ret = read(f->fd, hdr, sizeof(*hdr));
482                         if (ret < 0)
483                                 return ret;
484
485                         __io_u = hdr->usr_ptr;
486
487                         /* record if an io error occurred */
488                         if (hdr->info & SG_INFO_CHECK)
489                                 __io_u->error = EIO;
490
491                         if (__io_u == io_u)
492                                 break;
493
494                         if (io_u_sync_complete(td, __io_u))
495                                 break;
496
497                 } while (1);
498
499                 return FIO_Q_COMPLETED;
500         }
501
502         return FIO_Q_QUEUED;
503 }
504
505 static enum fio_q_status fio_sgio_doio(struct thread_data *td,
506                                        struct io_u *io_u, int do_sync)
507 {
508         struct fio_file *f = io_u->file;
509         enum fio_q_status ret;
510
511         if (f->filetype == FIO_TYPE_BLOCK) {
512                 ret = fio_sgio_ioctl_doio(td, f, io_u);
513                 if (io_u->error)
514                         td_verror(td, io_u->error, __func__);
515         } else {
516                 ret = fio_sgio_rw_doio(td, f, io_u, do_sync);
517                 if (io_u->error && do_sync)
518                         td_verror(td, io_u->error, __func__);
519         }
520
521         return ret;
522 }
523
524 static void fio_sgio_rw_lba(struct sg_io_hdr *hdr, unsigned long long lba,
525                             unsigned long long nr_blocks, bool override16)
526 {
527         if (lba < MAX_10B_LBA && !override16) {
528                 sgio_set_be32((uint32_t) lba, &hdr->cmdp[2]);
529                 sgio_set_be16((uint16_t) nr_blocks, &hdr->cmdp[7]);
530         } else {
531                 sgio_set_be64(lba, &hdr->cmdp[2]);
532                 sgio_set_be32((uint32_t) nr_blocks, &hdr->cmdp[10]);
533         }
534
535         return;
536 }
537
538 static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u)
539 {
540         struct sg_io_hdr *hdr = &io_u->hdr;
541         struct sg_options *o = td->eo;
542         struct sgio_data *sd = td->io_ops_data;
543         unsigned long long nr_blocks, lba;
544         int offset;
545
546         if (io_u->xfer_buflen & (sd->bs - 1)) {
547                 log_err("read/write not sector aligned\n");
548                 return EINVAL;
549         }
550
551         nr_blocks = io_u->xfer_buflen / sd->bs;
552         lba = io_u->offset / sd->bs;
553
554         if (io_u->ddir == DDIR_READ) {
555                 sgio_hdr_init(sd, hdr, io_u, 1);
556
557                 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
558                 if (lba < MAX_10B_LBA)
559                         hdr->cmdp[0] = 0x28; // read(10)
560                 else
561                         hdr->cmdp[0] = 0x88; // read(16)
562
563                 if (o->hipri)
564                         hdr->flags |= SGV4_FLAG_HIPRI;
565                 if (o->readfua)
566                         hdr->cmdp[1] |= 0x08;
567
568                 fio_sgio_rw_lba(hdr, lba, nr_blocks, false);
569
570         } else if (io_u->ddir == DDIR_WRITE) {
571                 sgio_hdr_init(sd, hdr, io_u, 1);
572
573                 hdr->dxfer_direction = SG_DXFER_TO_DEV;
574                 switch(o->write_mode) {
575                 case FIO_SG_WRITE:
576                         if (lba < MAX_10B_LBA)
577                                 hdr->cmdp[0] = 0x2a; // write(10)
578                         else
579                                 hdr->cmdp[0] = 0x8a; // write(16)
580                         if (o->hipri)
581                                 hdr->flags |= SGV4_FLAG_HIPRI;
582                         if (o->writefua)
583                                 hdr->cmdp[1] |= 0x08;
584                         break;
585                 case FIO_SG_WRITE_VERIFY:
586                         if (lba < MAX_10B_LBA)
587                                 hdr->cmdp[0] = 0x2e; // write and verify(10)
588                         else
589                                 hdr->cmdp[0] = 0x8e; // write and verify(16)
590                         break;
591                         // BYTCHK is disabled by virtue of the memset in sgio_hdr_init
592                 case FIO_SG_WRITE_SAME:
593                         hdr->dxfer_len = sd->bs;
594                         if (lba < MAX_10B_LBA)
595                                 hdr->cmdp[0] = 0x41; // write same(10)
596                         else
597                                 hdr->cmdp[0] = 0x93; // write same(16)
598                         break;
599                 case FIO_SG_WRITE_SAME_NDOB:
600                         hdr->cmdp[0] = 0x93; // write same(16)
601                         hdr->cmdp[1] |= 0x1; // no data output buffer
602                         hdr->dxfer_len = 0;
603                         break;
604                 case FIO_SG_VERIFY_BYTCHK_00:
605                         if (lba < MAX_10B_LBA)
606                                 hdr->cmdp[0] = 0x2f; // VERIFY(10)
607                         else
608                                 hdr->cmdp[0] = 0x8f; // VERIFY(16)
609                         hdr->dxfer_len = 0;
610                         break;
611                 case FIO_SG_VERIFY_BYTCHK_01:
612                         if (lba < MAX_10B_LBA)
613                                 hdr->cmdp[0] = 0x2f; // VERIFY(10)
614                         else
615                                 hdr->cmdp[0] = 0x8f; // VERIFY(16)
616                         hdr->cmdp[1] |= 0x02;           // BYTCHK = 01b
617                         break;
618                 case FIO_SG_VERIFY_BYTCHK_11:
619                         if (lba < MAX_10B_LBA)
620                                 hdr->cmdp[0] = 0x2f; // VERIFY(10)
621                         else
622                                 hdr->cmdp[0] = 0x8f; // VERIFY(16)
623                         hdr->cmdp[1] |= 0x06;           // BYTCHK = 11b
624                         hdr->dxfer_len = sd->bs;
625                         break;
626                 };
627
628                 fio_sgio_rw_lba(hdr, lba, nr_blocks,
629                         o->write_mode == FIO_SG_WRITE_SAME_NDOB);
630
631         } else if (io_u->ddir == DDIR_TRIM) {
632                 struct sgio_trim *st;
633
634                 if (sd->current_queue == -1) {
635                         sgio_hdr_init(sd, hdr, io_u, 0);
636
637                         hdr->cmd_len = 10;
638                         hdr->dxfer_direction = SG_DXFER_TO_DEV;
639                         hdr->cmdp[0] = 0x42; // unmap
640                         sd->current_queue = io_u->index;
641                         st = sd->trim_queues[sd->current_queue];
642                         hdr->dxferp = st->unmap_param;
643 #ifdef FIO_SGIO_DEBUG
644                         assert(sd->trim_queues[io_u->index]->unmap_range_count == 0);
645                         dprint(FD_IO, "sg: creating new queue based on io_u %d\n", io_u->index);
646 #endif
647                 }
648                 else
649                         st = sd->trim_queues[sd->current_queue];
650
651                 dprint(FD_IO, "sg: adding io_u %d to trim queue %d\n", io_u->index, sd->current_queue);
652                 st->trim_io_us[st->unmap_range_count] = io_u;
653 #ifdef FIO_SGIO_DEBUG
654                 sd->trim_queue_map[io_u->index] = sd->current_queue;
655 #endif
656
657                 offset = 8 + 16 * st->unmap_range_count;
658                 sgio_set_be64(lba, &st->unmap_param[offset]);
659                 sgio_set_be32((uint32_t) nr_blocks, &st->unmap_param[offset + 8]);
660
661                 st->unmap_range_count++;
662
663         } else if (ddir_sync(io_u->ddir)) {
664                 sgio_hdr_init(sd, hdr, io_u, 0);
665                 hdr->dxfer_direction = SG_DXFER_NONE;
666                 if (lba < MAX_10B_LBA)
667                         hdr->cmdp[0] = 0x35; // synccache(10)
668                 else
669                         hdr->cmdp[0] = 0x91; // synccache(16)
670         } else
671                 assert(0);
672
673         return 0;
674 }
675
676 static void fio_sgio_unmap_setup(struct sg_io_hdr *hdr, struct sgio_trim *st)
677 {
678         uint16_t cnt = st->unmap_range_count * 16;
679
680         hdr->dxfer_len = cnt + 8;
681         sgio_set_be16(cnt + 8, &hdr->cmdp[7]);
682         sgio_set_be16(cnt + 6, st->unmap_param);
683         sgio_set_be16(cnt, &st->unmap_param[2]);
684
685         return;
686 }
687
688 static enum fio_q_status fio_sgio_queue(struct thread_data *td,
689                                         struct io_u *io_u)
690 {
691         struct sg_io_hdr *hdr = &io_u->hdr;
692         struct sgio_data *sd = td->io_ops_data;
693         int ret, do_sync = 0;
694
695         fio_ro_check(td, io_u);
696
697         if (sgio_unbuffered(td) || ddir_sync(io_u->ddir))
698                 do_sync = 1;
699
700         if (io_u->ddir == DDIR_TRIM) {
701                 if (do_sync || io_u->file->filetype == FIO_TYPE_BLOCK) {
702                         struct sgio_trim *st = sd->trim_queues[sd->current_queue];
703
704                         /* finish cdb setup for unmap because we are
705                         ** doing unmap commands synchronously */
706 #ifdef FIO_SGIO_DEBUG
707                         assert(st->unmap_range_count == 1);
708                         assert(io_u == st->trim_io_us[0]);
709 #endif
710                         hdr = &io_u->hdr;
711
712                         fio_sgio_unmap_setup(hdr, st);
713
714                         st->unmap_range_count = 0;
715                         sd->current_queue = -1;
716                 } else
717                         /* queue up trim ranges and submit in commit() */
718                         return FIO_Q_QUEUED;
719         }
720
721         ret = fio_sgio_doio(td, io_u, do_sync);
722
723         if (ret < 0)
724                 io_u->error = errno;
725         else if (hdr->status) {
726                 io_u->resid = hdr->resid;
727                 io_u->error = EIO;
728         } else if (td->io_ops->commit != NULL) {
729                 if (do_sync && !ddir_sync(io_u->ddir)) {
730                         io_u_mark_submit(td, 1);
731                         io_u_mark_complete(td, 1);
732                 } else if (io_u->ddir == DDIR_READ || io_u->ddir == DDIR_WRITE) {
733                         io_u_mark_submit(td, 1);
734                         io_u_queued(td, io_u);
735                 }
736         }
737
738         if (io_u->error) {
739                 td_verror(td, io_u->error, "xfer");
740                 return FIO_Q_COMPLETED;
741         }
742
743         return ret;
744 }
745
746 static int fio_sgio_commit(struct thread_data *td)
747 {
748         struct sgio_data *sd = td->io_ops_data;
749         struct sgio_trim *st;
750         struct io_u *io_u;
751         struct sg_io_hdr *hdr;
752         struct timespec now;
753         unsigned int i;
754         int ret;
755
756         if (sd->current_queue == -1)
757                 return 0;
758
759         st = sd->trim_queues[sd->current_queue];
760         io_u = st->trim_io_us[0];
761         hdr = &io_u->hdr;
762
763         fio_sgio_unmap_setup(hdr, st);
764
765         sd->current_queue = -1;
766
767         ret = fio_sgio_rw_doio(td, io_u->file, io_u, 0);
768
769         if (ret < 0 || hdr->status) {
770                 int error;
771
772                 if (ret < 0)
773                         error = errno;
774                 else {
775                         error = EIO;
776                         ret = -EIO;
777                 }
778
779                 for (i = 0; i < st->unmap_range_count; i++) {
780                         st->trim_io_us[i]->error = error;
781                         clear_io_u(td, st->trim_io_us[i]);
782                         if (hdr->status)
783                                 st->trim_io_us[i]->resid = hdr->resid;
784                 }
785
786                 td_verror(td, error, "xfer");
787                 return ret;
788         }
789
790         if (fio_fill_issue_time(td)) {
791                 fio_gettime(&now, NULL);
792                 for (i = 0; i < st->unmap_range_count; i++) {
793                         memcpy(&st->trim_io_us[i]->issue_time, &now, sizeof(now));
794                         io_u_queued(td, io_u);
795                 }
796         }
797         io_u_mark_submit(td, st->unmap_range_count);
798
799         return 0;
800 }
801
802 static struct io_u *fio_sgio_event(struct thread_data *td, int event)
803 {
804         struct sgio_data *sd = td->io_ops_data;
805
806         return sd->events[event];
807 }
808
809 static int fio_sgio_read_capacity(struct thread_data *td, unsigned int *bs,
810                                   unsigned long long *max_lba)
811 {
812         /*
813          * need to do read capacity operation w/o benefit of sd or
814          * io_u structures, which are not initialized until later.
815          */
816         struct sg_io_hdr hdr;
817         unsigned long long hlba;
818         unsigned int blksz = 0;
819         unsigned char cmd[16];
820         unsigned char sb[64];
821         unsigned char buf[32];  // read capacity return
822         int ret;
823         int fd = -1;
824
825         struct fio_file *f = td->files[0];
826
827         /* open file independent of rest of application */
828         fd = open(f->file_name, O_RDONLY);
829         if (fd < 0)
830                 return -errno;
831
832         memset(&hdr, 0, sizeof(hdr));
833         memset(cmd, 0, sizeof(cmd));
834         memset(sb, 0, sizeof(sb));
835         memset(buf, 0, sizeof(buf));
836
837         /* First let's try a 10 byte read capacity. */
838         hdr.interface_id = 'S';
839         hdr.cmdp = cmd;
840         hdr.cmd_len = 10;
841         hdr.sbp = sb;
842         hdr.mx_sb_len = sizeof(sb);
843         hdr.timeout = SCSI_TIMEOUT_MS;
844         hdr.cmdp[0] = 0x25;  // Read Capacity(10)
845         hdr.dxfer_direction = SG_DXFER_FROM_DEV;
846         hdr.dxferp = buf;
847         hdr.dxfer_len = sizeof(buf);
848
849         ret = ioctl(fd, SG_IO, &hdr);
850         if (ret < 0) {
851                 close(fd);
852                 return ret;
853         }
854
855         if (hdr.info & SG_INFO_CHECK) {
856                 /* RCAP(10) might be unsupported by device. Force RCAP(16) */
857                 hlba = MAX_10B_LBA;
858         } else {
859                 blksz = sgio_get_be32(&buf[4]);
860                 hlba = sgio_get_be32(buf);
861         }
862
863         /*
864          * If max lba masked by MAX_10B_LBA equals MAX_10B_LBA,
865          * then need to retry with 16 byte Read Capacity command.
866          */
867         if (hlba == MAX_10B_LBA) {
868                 hdr.cmd_len = 16;
869                 hdr.cmdp[0] = 0x9e; // service action
870                 hdr.cmdp[1] = 0x10; // Read Capacity(16)
871                 sgio_set_be32(sizeof(buf), &hdr.cmdp[10]);
872
873                 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
874                 hdr.dxferp = buf;
875                 hdr.dxfer_len = sizeof(buf);
876
877                 ret = ioctl(fd, SG_IO, &hdr);
878                 if (ret < 0) {
879                         close(fd);
880                         return ret;
881                 }
882
883                 /* record if an io error occurred */
884                 if (hdr.info & SG_INFO_CHECK)
885                         td_verror(td, EIO, "fio_sgio_read_capacity");
886
887                 blksz = sgio_get_be32(&buf[8]);
888                 hlba = sgio_get_be64(buf);
889         }
890
891         if (blksz) {
892                 *bs = blksz;
893                 *max_lba = hlba;
894                 ret = 0;
895         } else {
896                 ret = EIO;
897         }
898
899         close(fd);
900         return ret;
901 }
902
903 static void fio_sgio_cleanup(struct thread_data *td)
904 {
905         struct sgio_data *sd = td->io_ops_data;
906         int i;
907
908         if (sd) {
909                 free(sd->events);
910                 free(sd->cmds);
911                 free(sd->fd_flags);
912                 free(sd->pfds);
913                 free(sd->sgbuf);
914 #ifdef FIO_SGIO_DEBUG
915                 free(sd->trim_queue_map);
916 #endif
917
918                 for (i = 0; i < td->o.iodepth; i++) {
919                         free(sd->trim_queues[i]->unmap_param);
920                         free(sd->trim_queues[i]->trim_io_us);
921                         free(sd->trim_queues[i]);
922                 }
923
924                 free(sd->trim_queues);
925                 free(sd);
926         }
927 }
928
929 static int fio_sgio_init(struct thread_data *td)
930 {
931         struct sgio_data *sd;
932         struct sgio_trim *st;
933         struct sg_io_hdr *h3p;
934         int i;
935
936         sd = calloc(1, sizeof(*sd));
937         sd->cmds = calloc(td->o.iodepth, sizeof(struct sgio_cmd));
938         sd->sgbuf = calloc(td->o.iodepth, sizeof(struct sg_io_hdr));
939         sd->events = calloc(td->o.iodepth, sizeof(struct io_u *));
940         sd->pfds = calloc(td->o.nr_files, sizeof(struct pollfd));
941         sd->fd_flags = calloc(td->o.nr_files, sizeof(int));
942         sd->type_checked = 0;
943
944         sd->trim_queues = calloc(td->o.iodepth, sizeof(struct sgio_trim *));
945         sd->current_queue = -1;
946 #ifdef FIO_SGIO_DEBUG
947         sd->trim_queue_map = calloc(td->o.iodepth, sizeof(int));
948 #endif
949         for (i = 0, h3p = sd->sgbuf; i < td->o.iodepth; i++, ++h3p) {
950                 sd->trim_queues[i] = calloc(1, sizeof(struct sgio_trim));
951                 st = sd->trim_queues[i];
952                 st->unmap_param = calloc(td->o.iodepth + 1, sizeof(char[16]));
953                 st->unmap_range_count = 0;
954                 st->trim_io_us = calloc(td->o.iodepth, sizeof(struct io_u *));
955                 h3p->interface_id = 'S';
956         }
957
958         td->io_ops_data = sd;
959
960         /*
961          * we want to do it, regardless of whether odirect is set or not
962          */
963         td->o.override_sync = 1;
964         return 0;
965 }
966
967 static int fio_sgio_type_check(struct thread_data *td, struct fio_file *f)
968 {
969         struct sgio_data *sd = td->io_ops_data;
970         unsigned int bs = 0;
971         unsigned long long max_lba = 0;
972
973         if (f->filetype == FIO_TYPE_BLOCK) {
974                 if (ioctl(f->fd, BLKSSZGET, &bs) < 0) {
975                         td_verror(td, errno, "ioctl");
976                         return 1;
977                 }
978         } else if (f->filetype == FIO_TYPE_CHAR) {
979                 int version, ret;
980
981                 if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) {
982                         td_verror(td, errno, "ioctl");
983                         return 1;
984                 }
985
986                 ret = fio_sgio_read_capacity(td, &bs, &max_lba);
987                 if (ret) {
988                         td_verror(td, td->error, "fio_sgio_read_capacity");
989                         log_err("ioengine sg unable to read capacity successfully\n");
990                         return 1;
991                 }
992         } else {
993                 td_verror(td, EINVAL, "wrong file type");
994                 log_err("ioengine sg only works on block or character devices\n");
995                 return 1;
996         }
997
998         sd->bs = bs;
999         // Determine size of commands needed based on max_lba
1000         if (max_lba >= MAX_10B_LBA) {
1001                 dprint(FD_IO, "sgio_type_check: using 16 byte read/write "
1002                         "commands for lba above 0x%016llx/0x%016llx\n",
1003                         MAX_10B_LBA, max_lba);
1004         }
1005
1006         if (f->filetype == FIO_TYPE_BLOCK) {
1007                 td->io_ops->getevents = NULL;
1008                 td->io_ops->event = NULL;
1009                 td->io_ops->commit = NULL;
1010                 /*
1011                 ** Setting these functions to null may cause problems
1012                 ** with filename=/dev/sda:/dev/sg0 since we are only
1013                 ** considering a single file
1014                 */
1015         }
1016         sd->type_checked = 1;
1017
1018         return 0;
1019 }
1020
1021 static int fio_sgio_open(struct thread_data *td, struct fio_file *f)
1022 {
1023         struct sgio_data *sd = td->io_ops_data;
1024         int ret;
1025
1026         ret = generic_open_file(td, f);
1027         if (ret)
1028                 return ret;
1029
1030         if (sd && !sd->type_checked && fio_sgio_type_check(td, f)) {
1031                 ret = generic_close_file(td, f);
1032                 return ret;
1033         }
1034
1035         return 0;
1036 }
1037
1038 /*
1039  * Build an error string with details about the driver, host or scsi
1040  * error contained in the sg header Caller will use as necessary.
1041  */
1042 static char *fio_sgio_errdetails(struct io_u *io_u)
1043 {
1044         struct sg_io_hdr *hdr = &io_u->hdr;
1045 #define MAXERRDETAIL 1024
1046 #define MAXMSGCHUNK  128
1047         char *msg, msgchunk[MAXMSGCHUNK];
1048         int i;
1049
1050         msg = calloc(1, MAXERRDETAIL);
1051         strcpy(msg, "");
1052
1053         /*
1054          * can't seem to find sg_err.h, so I'll just echo the define values
1055          * so others can search on internet to find clearer clues of meaning.
1056          */
1057         if (hdr->info & SG_INFO_CHECK) {
1058                 if (hdr->host_status) {
1059                         snprintf(msgchunk, MAXMSGCHUNK, "SG Host Status: 0x%02x; ", hdr->host_status);
1060                         strlcat(msg, msgchunk, MAXERRDETAIL);
1061                         switch (hdr->host_status) {
1062                         case 0x01:
1063                                 strlcat(msg, "SG_ERR_DID_NO_CONNECT", MAXERRDETAIL);
1064                                 break;
1065                         case 0x02:
1066                                 strlcat(msg, "SG_ERR_DID_BUS_BUSY", MAXERRDETAIL);
1067                                 break;
1068                         case 0x03:
1069                                 strlcat(msg, "SG_ERR_DID_TIME_OUT", MAXERRDETAIL);
1070                                 break;
1071                         case 0x04:
1072                                 strlcat(msg, "SG_ERR_DID_BAD_TARGET", MAXERRDETAIL);
1073                                 break;
1074                         case 0x05:
1075                                 strlcat(msg, "SG_ERR_DID_ABORT", MAXERRDETAIL);
1076                                 break;
1077                         case 0x06:
1078                                 strlcat(msg, "SG_ERR_DID_PARITY", MAXERRDETAIL);
1079                                 break;
1080                         case 0x07:
1081                                 strlcat(msg, "SG_ERR_DID_ERROR (internal error)", MAXERRDETAIL);
1082                                 break;
1083                         case 0x08:
1084                                 strlcat(msg, "SG_ERR_DID_RESET", MAXERRDETAIL);
1085                                 break;
1086                         case 0x09:
1087                                 strlcat(msg, "SG_ERR_DID_BAD_INTR (unexpected)", MAXERRDETAIL);
1088                                 break;
1089                         case 0x0a:
1090                                 strlcat(msg, "SG_ERR_DID_PASSTHROUGH", MAXERRDETAIL);
1091                                 break;
1092                         case 0x0b:
1093                                 strlcat(msg, "SG_ERR_DID_SOFT_ERROR (driver retry?)", MAXERRDETAIL);
1094                                 break;
1095                         case 0x0c:
1096                                 strlcat(msg, "SG_ERR_DID_IMM_RETRY", MAXERRDETAIL);
1097                                 break;
1098                         case 0x0d:
1099                                 strlcat(msg, "SG_ERR_DID_REQUEUE", MAXERRDETAIL);
1100                                 break;
1101                         case 0x0e:
1102                                 strlcat(msg, "SG_ERR_DID_TRANSPORT_DISRUPTED", MAXERRDETAIL);
1103                                 break;
1104                         case 0x0f:
1105                                 strlcat(msg, "SG_ERR_DID_TRANSPORT_FAILFAST", MAXERRDETAIL);
1106                                 break;
1107                         case 0x10:
1108                                 strlcat(msg, "SG_ERR_DID_TARGET_FAILURE", MAXERRDETAIL);
1109                                 break;
1110                         case 0x11:
1111                                 strlcat(msg, "SG_ERR_DID_NEXUS_FAILURE", MAXERRDETAIL);
1112                                 break;
1113                         case 0x12:
1114                                 strlcat(msg, "SG_ERR_DID_ALLOC_FAILURE", MAXERRDETAIL);
1115                                 break;
1116                         case 0x13:
1117                                 strlcat(msg, "SG_ERR_DID_MEDIUM_ERROR", MAXERRDETAIL);
1118                                 break;
1119                         default:
1120                                 strlcat(msg, "Unknown", MAXERRDETAIL);
1121                                 break;
1122                         }
1123                         strlcat(msg, ". ", MAXERRDETAIL);
1124                 }
1125                 if (hdr->driver_status) {
1126                         snprintf(msgchunk, MAXMSGCHUNK, "SG Driver Status: 0x%02x; ", hdr->driver_status);
1127                         strlcat(msg, msgchunk, MAXERRDETAIL);
1128                         switch (hdr->driver_status & 0x0F) {
1129                         case 0x01:
1130                                 strlcat(msg, "SG_ERR_DRIVER_BUSY", MAXERRDETAIL);
1131                                 break;
1132                         case 0x02:
1133                                 strlcat(msg, "SG_ERR_DRIVER_SOFT", MAXERRDETAIL);
1134                                 break;
1135                         case 0x03:
1136                                 strlcat(msg, "SG_ERR_DRIVER_MEDIA", MAXERRDETAIL);
1137                                 break;
1138                         case 0x04:
1139                                 strlcat(msg, "SG_ERR_DRIVER_ERROR", MAXERRDETAIL);
1140                                 break;
1141                         case 0x05:
1142                                 strlcat(msg, "SG_ERR_DRIVER_INVALID", MAXERRDETAIL);
1143                                 break;
1144                         case 0x06:
1145                                 strlcat(msg, "SG_ERR_DRIVER_TIMEOUT", MAXERRDETAIL);
1146                                 break;
1147                         case 0x07:
1148                                 strlcat(msg, "SG_ERR_DRIVER_HARD", MAXERRDETAIL);
1149                                 break;
1150                         case 0x08:
1151                                 strlcat(msg, "SG_ERR_DRIVER_SENSE", MAXERRDETAIL);
1152                                 break;
1153                         default:
1154                                 strlcat(msg, "Unknown", MAXERRDETAIL);
1155                                 break;
1156                         }
1157                         strlcat(msg, "; ", MAXERRDETAIL);
1158                         switch (hdr->driver_status & 0xF0) {
1159                         case 0x10:
1160                                 strlcat(msg, "SG_ERR_SUGGEST_RETRY", MAXERRDETAIL);
1161                                 break;
1162                         case 0x20:
1163                                 strlcat(msg, "SG_ERR_SUGGEST_ABORT", MAXERRDETAIL);
1164                                 break;
1165                         case 0x30:
1166                                 strlcat(msg, "SG_ERR_SUGGEST_REMAP", MAXERRDETAIL);
1167                                 break;
1168                         case 0x40:
1169                                 strlcat(msg, "SG_ERR_SUGGEST_DIE", MAXERRDETAIL);
1170                                 break;
1171                         case 0x80:
1172                                 strlcat(msg, "SG_ERR_SUGGEST_SENSE", MAXERRDETAIL);
1173                                 break;
1174                         }
1175                         strlcat(msg, ". ", MAXERRDETAIL);
1176                 }
1177                 if (hdr->status) {
1178                         snprintf(msgchunk, MAXMSGCHUNK, "SG SCSI Status: 0x%02x; ", hdr->status);
1179                         strlcat(msg, msgchunk, MAXERRDETAIL);
1180                         // SCSI 3 status codes
1181                         switch (hdr->status) {
1182                         case 0x02:
1183                                 strlcat(msg, "CHECK_CONDITION", MAXERRDETAIL);
1184                                 break;
1185                         case 0x04:
1186                                 strlcat(msg, "CONDITION_MET", MAXERRDETAIL);
1187                                 break;
1188                         case 0x08:
1189                                 strlcat(msg, "BUSY", MAXERRDETAIL);
1190                                 break;
1191                         case 0x10:
1192                                 strlcat(msg, "INTERMEDIATE", MAXERRDETAIL);
1193                                 break;
1194                         case 0x14:
1195                                 strlcat(msg, "INTERMEDIATE_CONDITION_MET", MAXERRDETAIL);
1196                                 break;
1197                         case 0x18:
1198                                 strlcat(msg, "RESERVATION_CONFLICT", MAXERRDETAIL);
1199                                 break;
1200                         case 0x22:
1201                                 strlcat(msg, "COMMAND_TERMINATED", MAXERRDETAIL);
1202                                 break;
1203                         case 0x28:
1204                                 strlcat(msg, "TASK_SET_FULL", MAXERRDETAIL);
1205                                 break;
1206                         case 0x30:
1207                                 strlcat(msg, "ACA_ACTIVE", MAXERRDETAIL);
1208                                 break;
1209                         case 0x40:
1210                                 strlcat(msg, "TASK_ABORTED", MAXERRDETAIL);
1211                                 break;
1212                         default:
1213                                 strlcat(msg, "Unknown", MAXERRDETAIL);
1214                                 break;
1215                         }
1216                         strlcat(msg, ". ", MAXERRDETAIL);
1217                 }
1218                 if (hdr->sb_len_wr) {
1219                         snprintf(msgchunk, MAXMSGCHUNK, "Sense Data (%d bytes):", hdr->sb_len_wr);
1220                         strlcat(msg, msgchunk, MAXERRDETAIL);
1221                         for (i = 0; i < hdr->sb_len_wr; i++) {
1222                                 snprintf(msgchunk, MAXMSGCHUNK, " %02x", hdr->sbp[i]);
1223                                 strlcat(msg, msgchunk, MAXERRDETAIL);
1224                         }
1225                         strlcat(msg, ". ", MAXERRDETAIL);
1226                 }
1227                 if (hdr->resid != 0) {
1228                         snprintf(msgchunk, MAXMSGCHUNK, "SG Driver: %d bytes out of %d not transferred. ", hdr->resid, hdr->dxfer_len);
1229                         strlcat(msg, msgchunk, MAXERRDETAIL);
1230                 }
1231                 if (hdr->cmdp) {
1232                         strlcat(msg, "cdb:", MAXERRDETAIL);
1233                         for (i = 0; i < hdr->cmd_len; i++) {
1234                                 snprintf(msgchunk, MAXMSGCHUNK, " %02x", hdr->cmdp[i]);
1235                                 strlcat(msg, msgchunk, MAXERRDETAIL);
1236                         }
1237                         strlcat(msg, ". ", MAXERRDETAIL);
1238                         if (io_u->ddir == DDIR_TRIM) {
1239                                 unsigned char *param_list = hdr->dxferp;
1240                                 strlcat(msg, "dxferp:", MAXERRDETAIL);
1241                                 for (i = 0; i < hdr->dxfer_len; i++) {
1242                                         snprintf(msgchunk, MAXMSGCHUNK, " %02x", param_list[i]);
1243                                         strlcat(msg, msgchunk, MAXERRDETAIL);
1244                                 }
1245                                 strlcat(msg, ". ", MAXERRDETAIL);
1246                         }
1247                 }
1248         }
1249
1250         if (!(hdr->info & SG_INFO_CHECK) && !strlen(msg))
1251                 snprintf(msg, MAXERRDETAIL, "%s",
1252                          "SG Driver did not report a Host, Driver or Device check");
1253
1254         return msg;
1255 }
1256
1257 /*
1258  * get max file size from read capacity.
1259  */
1260 static int fio_sgio_get_file_size(struct thread_data *td, struct fio_file *f)
1261 {
1262         /*
1263          * get_file_size is being called even before sgio_init is
1264          * called, so none of the sg_io structures are
1265          * initialized in the thread_data yet.  So we need to do the
1266          * ReadCapacity without any of those helpers.  One of the effects
1267          * is that ReadCapacity may get called 4 times on each open:
1268          * readcap(10) followed by readcap(16) if needed - just to get
1269          * the file size after the init occurs - it will be called
1270          * again when "type_check" is called during structure
1271          * initialization I'm not sure how to prevent this little
1272          * inefficiency.
1273          */
1274         unsigned int bs = 0;
1275         unsigned long long max_lba = 0;
1276         int ret;
1277
1278         if (fio_file_size_known(f))
1279                 return 0;
1280
1281         if (f->filetype != FIO_TYPE_BLOCK && f->filetype != FIO_TYPE_CHAR) {
1282                 td_verror(td, EINVAL, "wrong file type");
1283                 log_err("ioengine sg only works on block or character devices\n");
1284                 return 1;
1285         }
1286
1287         ret = fio_sgio_read_capacity(td, &bs, &max_lba);
1288         if (ret ) {
1289                 td_verror(td, td->error, "fio_sgio_read_capacity");
1290                 log_err("ioengine sg unable to successfully execute read capacity to get block size and maximum lba\n");
1291                 return 1;
1292         }
1293
1294         f->real_file_size = (max_lba + 1) * bs;
1295         fio_file_set_size_known(f);
1296         return 0;
1297 }
1298
1299
1300 static struct ioengine_ops ioengine = {
1301         .name           = "sg",
1302         .version        = FIO_IOOPS_VERSION,
1303         .init           = fio_sgio_init,
1304         .prep           = fio_sgio_prep,
1305         .queue          = fio_sgio_queue,
1306         .commit         = fio_sgio_commit,
1307         .getevents      = fio_sgio_getevents,
1308         .errdetails     = fio_sgio_errdetails,
1309         .event          = fio_sgio_event,
1310         .cleanup        = fio_sgio_cleanup,
1311         .open_file      = fio_sgio_open,
1312         .close_file     = generic_close_file,
1313         .get_file_size  = fio_sgio_get_file_size,
1314         .flags          = FIO_SYNCIO | FIO_RAWIO,
1315         .options        = options,
1316         .option_struct_size     = sizeof(struct sg_options)
1317 };
1318
1319 #else /* FIO_HAVE_SGIO */
1320
1321 /*
1322  * When we have a proper configure system in place, we simply wont build
1323  * and install this io engine. For now install a crippled version that
1324  * just complains and fails to load.
1325  */
1326 static int fio_sgio_init(struct thread_data fio_unused *td)
1327 {
1328         log_err("fio: ioengine sg not available\n");
1329         return 1;
1330 }
1331
1332 static struct ioengine_ops ioengine = {
1333         .name           = "sg",
1334         .version        = FIO_IOOPS_VERSION,
1335         .init           = fio_sgio_init,
1336 };
1337
1338 #endif
1339
1340 static void fio_init fio_sgio_register(void)
1341 {
1342         register_ioengine(&ioengine);
1343 }
1344
1345 static void fio_exit fio_sgio_unregister(void)
1346 {
1347         unregister_ioengine(&ioengine);
1348 }