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