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