Use POSIX path for poll.h and fcntl.h headers
[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  */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <poll.h>
12
13 #include "../fio.h"
14 #include "../optgroup.h"
15
16 #ifdef FIO_HAVE_SGIO
17
18
19 struct sg_options {
20         void *pad;
21         unsigned int readfua;
22         unsigned int writefua;
23 };
24
25 static struct fio_option options[] = {
26         {
27                 .name   = "readfua",
28                 .lname  = "sg engine read fua flag support",
29                 .type   = FIO_OPT_BOOL,
30                 .off1   = offsetof(struct sg_options, readfua),
31                 .help   = "Set FUA flag (force unit access) for all Read operations",
32                 .def    = "0",
33                 .category = FIO_OPT_C_ENGINE,
34                 .group  = FIO_OPT_G_SG,
35         },
36         {
37                 .name   = "writefua",
38                 .lname  = "sg engine write fua flag support",
39                 .type   = FIO_OPT_BOOL,
40                 .off1   = offsetof(struct sg_options, writefua),
41                 .help   = "Set FUA flag (force unit access) for all Write operations",
42                 .def    = "0",
43                 .category = FIO_OPT_C_ENGINE,
44                 .group  = FIO_OPT_G_SG,
45         },
46         {
47                 .name   = NULL,
48         },
49 };
50
51 #define MAX_10B_LBA  0xFFFFFFFFULL
52 #define SCSI_TIMEOUT_MS 30000   // 30 second timeout; currently no method to override
53 #define MAX_SB 64               // sense block maximum return size
54
55 struct sgio_cmd {
56         unsigned char cdb[16];      // enhanced from 10 to support 16 byte commands
57         unsigned char sb[MAX_SB];   // add sense block to commands
58         int nr;
59 };
60
61 struct sgio_data {
62         struct sgio_cmd *cmds;
63         struct io_u **events;
64         struct pollfd *pfds;
65         int *fd_flags;
66         void *sgbuf;
67         unsigned int bs;
68         int type_checked;
69 };
70
71 static void sgio_hdr_init(struct sgio_data *sd, struct sg_io_hdr *hdr,
72                           struct io_u *io_u, int fs)
73 {
74         struct sgio_cmd *sc = &sd->cmds[io_u->index];
75
76         memset(hdr, 0, sizeof(*hdr));
77         memset(sc->cdb, 0, sizeof(sc->cdb));
78
79         hdr->interface_id = 'S';
80         hdr->cmdp = sc->cdb;
81         hdr->cmd_len = sizeof(sc->cdb);
82         hdr->sbp = sc->sb;
83         hdr->mx_sb_len = sizeof(sc->sb);
84         hdr->pack_id = io_u->index;
85         hdr->usr_ptr = io_u;
86
87         if (fs) {
88                 hdr->dxferp = io_u->xfer_buf;
89                 hdr->dxfer_len = io_u->xfer_buflen;
90         }
91 }
92
93 static int pollin_events(struct pollfd *pfds, int fds)
94 {
95         int i;
96
97         for (i = 0; i < fds; i++)
98                 if (pfds[i].revents & POLLIN)
99                         return 1;
100
101         return 0;
102 }
103
104 static int sg_fd_read(int fd, void *data, size_t size)
105 {
106         int err = 0;
107
108         while (size) {
109                 ssize_t ret;
110
111                 ret = read(fd, data, size);
112                 if (ret < 0) {
113                         if (errno == EAGAIN || errno == EINTR)
114                                 continue;
115                         err = errno;
116                         break;
117                 } else if (!ret)
118                         break;
119                 else {
120                         data += ret;
121                         size -= ret;
122                 }
123         }
124
125         if (err)
126                 return err;
127         if (size)
128                 return EAGAIN;
129
130         return 0;
131 }
132
133 static int fio_sgio_getevents(struct thread_data *td, unsigned int min,
134                               unsigned int max,
135                               const struct timespec fio_unused *t)
136 {
137         struct sgio_data *sd = td->io_ops_data;
138         int left = max, eventNum, ret, r = 0;
139         void *buf = sd->sgbuf;
140         unsigned int i, events;
141         struct fio_file *f;
142
143         /*
144          * Fill in the file descriptors
145          */
146         for_each_file(td, f, i) {
147                 /*
148                  * don't block for min events == 0
149                  */
150                 if (!min)
151                         sd->fd_flags[i] = fio_set_fd_nonblocking(f->fd, "sg");
152                 else
153                         sd->fd_flags[i] = -1;
154
155                 sd->pfds[i].fd = f->fd;
156                 sd->pfds[i].events = POLLIN;
157         }
158
159         while (left) {
160                 char *p;
161
162                 dprint(FD_IO, "sgio_getevents: sd %p: left=%d\n", sd, left);
163
164                 do {
165                         if (!min)
166                                 break;
167
168                         ret = poll(sd->pfds, td->o.nr_files, -1);
169                         if (ret < 0) {
170                                 if (!r)
171                                         r = -errno;
172                                 td_verror(td, errno, "poll");
173                                 break;
174                         } else if (!ret)
175                                 continue;
176
177                         if (pollin_events(sd->pfds, td->o.nr_files))
178                                 break;
179                 } while (1);
180
181                 if (r < 0)
182                         break;
183
184 re_read:
185                 p = buf;
186                 events = 0;
187                 for_each_file(td, f, i) {
188                         for (eventNum = 0; eventNum < left; eventNum++) {
189                                 ret = sg_fd_read(f->fd, p, sizeof(struct sg_io_hdr));
190                                 dprint(FD_IO, "sgio_getevents: ret: %d\n", ret);
191                                 if (ret) {
192                                         r = -ret;
193                                         td_verror(td, r, "sg_read");
194                                         break;
195                                 }
196                                 p += sizeof(struct sg_io_hdr);
197                                 events++;
198                                 dprint(FD_IO, "sgio_getevents: events: %d\n", events);
199                         }
200                 }
201
202                 if (r < 0 && !events)
203                         break;
204                 if (!events) {
205                         usleep(1000);
206                         goto re_read;
207                 }
208
209                 left -= events;
210                 r += events;
211
212                 for (i = 0; i < events; i++) {
213                         struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i;
214                         sd->events[i] = hdr->usr_ptr;
215
216                         /* record if an io error occurred, ignore resid */
217                         if (hdr->info & SG_INFO_CHECK) {
218                                 struct io_u *io_u;
219                                 io_u = (struct io_u *)(hdr->usr_ptr);
220                                 memcpy(&io_u->hdr, hdr, sizeof(struct sg_io_hdr));
221                                 sd->events[i]->error = EIO;
222                         }
223                 }
224         }
225
226         if (!min) {
227                 for_each_file(td, f, i) {
228                         if (sd->fd_flags[i] == -1)
229                                 continue;
230
231                         if (fcntl(f->fd, F_SETFL, sd->fd_flags[i]) < 0)
232                                 log_err("fio: sg failed to restore fcntl flags: %s\n", strerror(errno));
233                 }
234         }
235
236         return r;
237 }
238
239 static int fio_sgio_ioctl_doio(struct thread_data *td,
240                                struct fio_file *f, struct io_u *io_u)
241 {
242         struct sgio_data *sd = td->io_ops_data;
243         struct sg_io_hdr *hdr = &io_u->hdr;
244         int ret;
245
246         sd->events[0] = io_u;
247
248         ret = ioctl(f->fd, SG_IO, hdr);
249         if (ret < 0)
250                 return ret;
251
252         /* record if an io error occurred */
253         if (hdr->info & SG_INFO_CHECK)
254                 io_u->error = EIO;
255
256         return FIO_Q_COMPLETED;
257 }
258
259 static int fio_sgio_rw_doio(struct fio_file *f, struct io_u *io_u, int do_sync)
260 {
261         struct sg_io_hdr *hdr = &io_u->hdr;
262         int ret;
263
264         ret = write(f->fd, hdr, sizeof(*hdr));
265         if (ret < 0)
266                 return ret;
267
268         if (do_sync) {
269                 ret = read(f->fd, hdr, sizeof(*hdr));
270                 if (ret < 0)
271                         return ret;
272
273                 /* record if an io error occurred */
274                 if (hdr->info & SG_INFO_CHECK)
275                         io_u->error = EIO;
276
277                 return FIO_Q_COMPLETED;
278         }
279
280         return FIO_Q_QUEUED;
281 }
282
283 static int fio_sgio_doio(struct thread_data *td, struct io_u *io_u, int do_sync)
284 {
285         struct fio_file *f = io_u->file;
286         int ret;
287
288         if (f->filetype == FIO_TYPE_BLOCK) {
289                 ret = fio_sgio_ioctl_doio(td, f, io_u);
290                 td->error = io_u->error;
291         } else {
292                 ret = fio_sgio_rw_doio(f, io_u, do_sync);
293                 if (do_sync)
294                         td->error = io_u->error;
295         }
296
297         return ret;
298 }
299
300 static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u)
301 {
302         struct sg_io_hdr *hdr = &io_u->hdr;
303         struct sg_options *o = td->eo;
304         struct sgio_data *sd = td->io_ops_data;
305         long long nr_blocks, lba;
306
307         if (io_u->xfer_buflen & (sd->bs - 1)) {
308                 log_err("read/write not sector aligned\n");
309                 return EINVAL;
310         }
311
312         nr_blocks = io_u->xfer_buflen / sd->bs;
313         lba = io_u->offset / sd->bs;
314
315         if (io_u->ddir == DDIR_READ) {
316                 sgio_hdr_init(sd, hdr, io_u, 1);
317
318                 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
319                 if (lba < MAX_10B_LBA)
320                         hdr->cmdp[0] = 0x28; // read(10)
321                 else
322                         hdr->cmdp[0] = 0x88; // read(16)
323
324                 if (o->readfua)
325                         hdr->cmdp[1] |= 0x08;
326
327         } else if (io_u->ddir == DDIR_WRITE) {
328                 sgio_hdr_init(sd, hdr, io_u, 1);
329
330                 hdr->dxfer_direction = SG_DXFER_TO_DEV;
331                 if (lba < MAX_10B_LBA)
332                         hdr->cmdp[0] = 0x2a; // write(10)
333                 else
334                         hdr->cmdp[0] = 0x8a; // write(16)
335
336                 if (o->writefua)
337                         hdr->cmdp[1] |= 0x08;
338
339         } else {
340                 sgio_hdr_init(sd, hdr, io_u, 0);
341                 hdr->dxfer_direction = SG_DXFER_NONE;
342                 if (lba < MAX_10B_LBA)
343                         hdr->cmdp[0] = 0x35; // synccache(10)
344                 else
345                         hdr->cmdp[0] = 0x91; // synccache(16)
346         }
347
348         /*
349          * for synccache, we leave lba and length to 0 to sync all
350          * blocks on medium.
351          */
352         if (hdr->dxfer_direction != SG_DXFER_NONE) {
353                 if (lba < MAX_10B_LBA) {
354                         hdr->cmdp[2] = (unsigned char) ((lba >> 24) & 0xff);
355                         hdr->cmdp[3] = (unsigned char) ((lba >> 16) & 0xff);
356                         hdr->cmdp[4] = (unsigned char) ((lba >>  8) & 0xff);
357                         hdr->cmdp[5] = (unsigned char) (lba & 0xff);
358                         hdr->cmdp[7] = (unsigned char) ((nr_blocks >> 8) & 0xff);
359                         hdr->cmdp[8] = (unsigned char) (nr_blocks & 0xff);
360                 } else {
361                         hdr->cmdp[2] = (unsigned char) ((lba >> 56) & 0xff);
362                         hdr->cmdp[3] = (unsigned char) ((lba >> 48) & 0xff);
363                         hdr->cmdp[4] = (unsigned char) ((lba >> 40) & 0xff);
364                         hdr->cmdp[5] = (unsigned char) ((lba >> 32) & 0xff);
365                         hdr->cmdp[6] = (unsigned char) ((lba >> 24) & 0xff);
366                         hdr->cmdp[7] = (unsigned char) ((lba >> 16) & 0xff);
367                         hdr->cmdp[8] = (unsigned char) ((lba >>  8) & 0xff);
368                         hdr->cmdp[9] = (unsigned char) (lba & 0xff);
369                         hdr->cmdp[10] = (unsigned char) ((nr_blocks >> 32) & 0xff);
370                         hdr->cmdp[11] = (unsigned char) ((nr_blocks >> 16) & 0xff);
371                         hdr->cmdp[12] = (unsigned char) ((nr_blocks >> 8) & 0xff);
372                         hdr->cmdp[13] = (unsigned char) (nr_blocks & 0xff);
373                 }
374         }
375
376         hdr->timeout = SCSI_TIMEOUT_MS;
377         return 0;
378 }
379
380 static int fio_sgio_queue(struct thread_data *td, struct io_u *io_u)
381 {
382         struct sg_io_hdr *hdr = &io_u->hdr;
383         int ret, do_sync = 0;
384
385         fio_ro_check(td, io_u);
386
387         if (td->o.sync_io || td->o.odirect || ddir_sync(io_u->ddir))
388                 do_sync = 1;
389
390         ret = fio_sgio_doio(td, io_u, do_sync);
391
392         if (ret < 0)
393                 io_u->error = errno;
394         else if (hdr->status) {
395                 io_u->resid = hdr->resid;
396                 io_u->error = EIO;
397         }
398
399         if (io_u->error) {
400                 td_verror(td, io_u->error, "xfer");
401                 return FIO_Q_COMPLETED;
402         }
403
404         return ret;
405 }
406
407 static struct io_u *fio_sgio_event(struct thread_data *td, int event)
408 {
409         struct sgio_data *sd = td->io_ops_data;
410
411         return sd->events[event];
412 }
413
414 static int fio_sgio_read_capacity(struct thread_data *td, unsigned int *bs,
415                                   unsigned long long *max_lba)
416 {
417         /*
418          * need to do read capacity operation w/o benefit of sd or
419          * io_u structures, which are not initialized until later.
420          */
421         struct sg_io_hdr hdr;
422         unsigned char cmd[16];
423         unsigned char sb[64];
424         unsigned char buf[32];  // read capacity return
425         int ret;
426         int fd = -1;
427
428         struct fio_file *f = td->files[0];
429
430         /* open file independent of rest of application */
431         fd = open(f->file_name, O_RDONLY);
432         if (fd < 0)
433                 return -errno;
434
435         memset(&hdr, 0, sizeof(hdr));
436         memset(cmd, 0, sizeof(cmd));
437         memset(sb, 0, sizeof(sb));
438         memset(buf, 0, sizeof(buf));
439
440         /* First let's try a 10 byte read capacity. */
441         hdr.interface_id = 'S';
442         hdr.cmdp = cmd;
443         hdr.cmd_len = 10;
444         hdr.sbp = sb;
445         hdr.mx_sb_len = sizeof(sb);
446         hdr.timeout = SCSI_TIMEOUT_MS;
447         hdr.cmdp[0] = 0x25;  // Read Capacity(10)
448         hdr.dxfer_direction = SG_DXFER_FROM_DEV;
449         hdr.dxferp = buf;
450         hdr.dxfer_len = sizeof(buf);
451
452         ret = ioctl(fd, SG_IO, &hdr);
453         if (ret < 0) {
454                 close(fd);
455                 return ret;
456         }
457
458         *bs      = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
459         *max_lba = ((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]) & MAX_10B_LBA;  // for some reason max_lba is being sign extended even though unsigned.
460
461         /*
462          * If max lba masked by MAX_10B_LBA equals MAX_10B_LBA,
463          * then need to retry with 16 byte Read Capacity command.
464          */
465         if (*max_lba == MAX_10B_LBA) {
466                 hdr.cmd_len = 16;
467                 hdr.cmdp[0] = 0x9e; // service action
468                 hdr.cmdp[1] = 0x10; // Read Capacity(16)
469                 hdr.cmdp[10] = (unsigned char) ((sizeof(buf) >> 24) & 0xff);
470                 hdr.cmdp[11] = (unsigned char) ((sizeof(buf) >> 16) & 0xff);
471                 hdr.cmdp[12] = (unsigned char) ((sizeof(buf) >> 8) & 0xff);
472                 hdr.cmdp[13] = (unsigned char) (sizeof(buf) & 0xff);
473
474                 hdr.dxfer_direction = SG_DXFER_FROM_DEV;
475                 hdr.dxferp = buf;
476                 hdr.dxfer_len = sizeof(buf);
477
478                 ret = ioctl(fd, SG_IO, &hdr);
479                 if (ret < 0) {
480                         close(fd);
481                         return ret;
482                 }
483
484                 /* record if an io error occurred */
485                 if (hdr.info & SG_INFO_CHECK)
486                         td_verror(td, EIO, "fio_sgio_read_capacity");
487
488                 *bs = (buf[8] << 24) | (buf[9] << 16) | (buf[10] << 8) | buf[11];
489                 *max_lba = ((unsigned long long)buf[0] << 56) |
490                                 ((unsigned long long)buf[1] << 48) |
491                                 ((unsigned long long)buf[2] << 40) |
492                                 ((unsigned long long)buf[3] << 32) |
493                                 ((unsigned long long)buf[4] << 24) |
494                                 ((unsigned long long)buf[5] << 16) |
495                                 ((unsigned long long)buf[6] << 8) |
496                                 (unsigned long long)buf[7];
497         }
498
499         close(fd);
500         return 0;
501 }
502
503 static void fio_sgio_cleanup(struct thread_data *td)
504 {
505         struct sgio_data *sd = td->io_ops_data;
506
507         if (sd) {
508                 free(sd->events);
509                 free(sd->cmds);
510                 free(sd->fd_flags);
511                 free(sd->pfds);
512                 free(sd->sgbuf);
513                 free(sd);
514         }
515 }
516
517 static int fio_sgio_init(struct thread_data *td)
518 {
519         struct sgio_data *sd;
520
521         sd = malloc(sizeof(*sd));
522         memset(sd, 0, sizeof(*sd));
523         sd->cmds = malloc(td->o.iodepth * sizeof(struct sgio_cmd));
524         memset(sd->cmds, 0, td->o.iodepth * sizeof(struct sgio_cmd));
525         sd->events = malloc(td->o.iodepth * sizeof(struct io_u *));
526         memset(sd->events, 0, td->o.iodepth * sizeof(struct io_u *));
527         sd->pfds = malloc(sizeof(struct pollfd) * td->o.nr_files);
528         memset(sd->pfds, 0, sizeof(struct pollfd) * td->o.nr_files);
529         sd->fd_flags = malloc(sizeof(int) * td->o.nr_files);
530         memset(sd->fd_flags, 0, sizeof(int) * td->o.nr_files);
531         sd->sgbuf = malloc(sizeof(struct sg_io_hdr) * td->o.iodepth);
532         memset(sd->sgbuf, 0, sizeof(struct sg_io_hdr) * td->o.iodepth);
533         sd->type_checked = 0;
534         td->io_ops_data = sd;
535
536         /*
537          * we want to do it, regardless of whether odirect is set or not
538          */
539         td->o.override_sync = 1;
540         return 0;
541 }
542
543 static int fio_sgio_type_check(struct thread_data *td, struct fio_file *f)
544 {
545         struct sgio_data *sd = td->io_ops_data;
546         unsigned int bs = 0;
547         unsigned long long max_lba = 0;
548
549         if (f->filetype == FIO_TYPE_BLOCK) {
550                 if (ioctl(f->fd, BLKSSZGET, &bs) < 0) {
551                         td_verror(td, errno, "ioctl");
552                         return 1;
553                 }
554         } else if (f->filetype == FIO_TYPE_CHAR) {
555                 int version, ret;
556
557                 if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) {
558                         td_verror(td, errno, "ioctl");
559                         return 1;
560                 }
561
562                 ret = fio_sgio_read_capacity(td, &bs, &max_lba);
563                 if (ret) {
564                         td_verror(td, td->error, "fio_sgio_read_capacity");
565                         log_err("ioengine sg unable to read capacity successfully\n");
566                         return 1;
567                 }
568         } else {
569                 td_verror(td, EINVAL, "wrong file type");
570                 log_err("ioengine sg only works on block or character devices\n");
571                 return 1;
572         }
573
574         sd->bs = bs;
575         // Determine size of commands needed based on max_lba
576         if (max_lba >= MAX_10B_LBA) {
577                 dprint(FD_IO, "sgio_type_check: using 16 byte read/write "
578                         "commands for lba above 0x%016llx/0x%016llx\n",
579                         MAX_10B_LBA, max_lba);
580         }
581
582         if (f->filetype == FIO_TYPE_BLOCK) {
583                 td->io_ops->getevents = NULL;
584                 td->io_ops->event = NULL;
585         }
586         sd->type_checked = 1;
587
588         return 0;
589 }
590
591 static int fio_sgio_open(struct thread_data *td, struct fio_file *f)
592 {
593         struct sgio_data *sd = td->io_ops_data;
594         int ret;
595
596         ret = generic_open_file(td, f);
597         if (ret)
598                 return ret;
599
600         if (sd && !sd->type_checked && fio_sgio_type_check(td, f)) {
601                 ret = generic_close_file(td, f);
602                 return 1;
603         }
604
605         return 0;
606 }
607
608 /*
609  * Build an error string with details about the driver, host or scsi
610  * error contained in the sg header Caller will use as necessary.
611  */
612 static char *fio_sgio_errdetails(struct io_u *io_u)
613 {
614         struct sg_io_hdr *hdr = &io_u->hdr;
615 #define MAXERRDETAIL 1024
616 #define MAXMSGCHUNK  128
617         char *msg, msgchunk[MAXMSGCHUNK];
618         int i;
619
620         msg = calloc(1, MAXERRDETAIL);
621         strcpy(msg, "");
622
623         /*
624          * can't seem to find sg_err.h, so I'll just echo the define values
625          * so others can search on internet to find clearer clues of meaning.
626          */
627         if (hdr->info & SG_INFO_CHECK) {
628                 if (hdr->host_status) {
629                         snprintf(msgchunk, MAXMSGCHUNK, "SG Host Status: 0x%02x; ", hdr->host_status);
630                         strlcat(msg, msgchunk, MAXERRDETAIL);
631                         switch (hdr->host_status) {
632                         case 0x01:
633                                 strlcat(msg, "SG_ERR_DID_NO_CONNECT", MAXERRDETAIL);
634                                 break;
635                         case 0x02:
636                                 strlcat(msg, "SG_ERR_DID_BUS_BUSY", MAXERRDETAIL);
637                                 break;
638                         case 0x03:
639                                 strlcat(msg, "SG_ERR_DID_TIME_OUT", MAXERRDETAIL);
640                                 break;
641                         case 0x04:
642                                 strlcat(msg, "SG_ERR_DID_BAD_TARGET", MAXERRDETAIL);
643                                 break;
644                         case 0x05:
645                                 strlcat(msg, "SG_ERR_DID_ABORT", MAXERRDETAIL);
646                                 break;
647                         case 0x06:
648                                 strlcat(msg, "SG_ERR_DID_PARITY", MAXERRDETAIL);
649                                 break;
650                         case 0x07:
651                                 strlcat(msg, "SG_ERR_DID_ERROR (internal error)", MAXERRDETAIL);
652                                 break;
653                         case 0x08:
654                                 strlcat(msg, "SG_ERR_DID_RESET", MAXERRDETAIL);
655                                 break;
656                         case 0x09:
657                                 strlcat(msg, "SG_ERR_DID_BAD_INTR (unexpected)", MAXERRDETAIL);
658                                 break;
659                         case 0x0a:
660                                 strlcat(msg, "SG_ERR_DID_PASSTHROUGH", MAXERRDETAIL);
661                                 break;
662                         case 0x0b:
663                                 strlcat(msg, "SG_ERR_DID_SOFT_ERROR (driver retry?)", MAXERRDETAIL);
664                                 break;
665                         case 0x0c:
666                                 strlcat(msg, "SG_ERR_DID_IMM_RETRY", MAXERRDETAIL);
667                                 break;
668                         case 0x0d:
669                                 strlcat(msg, "SG_ERR_DID_REQUEUE", MAXERRDETAIL);
670                                 break;
671                         case 0x0e:
672                                 strlcat(msg, "SG_ERR_DID_TRANSPORT_DISRUPTED", MAXERRDETAIL);
673                                 break;
674                         case 0x0f:
675                                 strlcat(msg, "SG_ERR_DID_TRANSPORT_FAILFAST", MAXERRDETAIL);
676                                 break;
677                         case 0x10:
678                                 strlcat(msg, "SG_ERR_DID_TARGET_FAILURE", MAXERRDETAIL);
679                                 break;
680                         case 0x11:
681                                 strlcat(msg, "SG_ERR_DID_NEXUS_FAILURE", MAXERRDETAIL);
682                                 break;
683                         case 0x12:
684                                 strlcat(msg, "SG_ERR_DID_ALLOC_FAILURE", MAXERRDETAIL);
685                                 break;
686                         case 0x13:
687                                 strlcat(msg, "SG_ERR_DID_MEDIUM_ERROR", MAXERRDETAIL);
688                                 break;
689                         default:
690                                 strlcat(msg, "Unknown", MAXERRDETAIL);
691                                 break;
692                         }
693                         strlcat(msg, ". ", MAXERRDETAIL);
694                 }
695                 if (hdr->driver_status) {
696                         snprintf(msgchunk, MAXMSGCHUNK, "SG Driver Status: 0x%02x; ", hdr->driver_status);
697                         strlcat(msg, msgchunk, MAXERRDETAIL);
698                         switch (hdr->driver_status & 0x0F) {
699                         case 0x01:
700                                 strlcat(msg, "SG_ERR_DRIVER_BUSY", MAXERRDETAIL);
701                                 break;
702                         case 0x02:
703                                 strlcat(msg, "SG_ERR_DRIVER_SOFT", MAXERRDETAIL);
704                                 break;
705                         case 0x03:
706                                 strlcat(msg, "SG_ERR_DRIVER_MEDIA", MAXERRDETAIL);
707                                 break;
708                         case 0x04:
709                                 strlcat(msg, "SG_ERR_DRIVER_ERROR", MAXERRDETAIL);
710                                 break;
711                         case 0x05:
712                                 strlcat(msg, "SG_ERR_DRIVER_INVALID", MAXERRDETAIL);
713                                 break;
714                         case 0x06:
715                                 strlcat(msg, "SG_ERR_DRIVER_TIMEOUT", MAXERRDETAIL);
716                                 break;
717                         case 0x07:
718                                 strlcat(msg, "SG_ERR_DRIVER_HARD", MAXERRDETAIL);
719                                 break;
720                         case 0x08:
721                                 strlcat(msg, "SG_ERR_DRIVER_SENSE", MAXERRDETAIL);
722                                 break;
723                         default:
724                                 strlcat(msg, "Unknown", MAXERRDETAIL);
725                                 break;
726                         }
727                         strlcat(msg, "; ", MAXERRDETAIL);
728                         switch (hdr->driver_status & 0xF0) {
729                         case 0x10:
730                                 strlcat(msg, "SG_ERR_SUGGEST_RETRY", MAXERRDETAIL);
731                                 break;
732                         case 0x20:
733                                 strlcat(msg, "SG_ERR_SUGGEST_ABORT", MAXERRDETAIL);
734                                 break;
735                         case 0x30:
736                                 strlcat(msg, "SG_ERR_SUGGEST_REMAP", MAXERRDETAIL);
737                                 break;
738                         case 0x40:
739                                 strlcat(msg, "SG_ERR_SUGGEST_DIE", MAXERRDETAIL);
740                                 break;
741                         case 0x80:
742                                 strlcat(msg, "SG_ERR_SUGGEST_SENSE", MAXERRDETAIL);
743                                 break;
744                         }
745                         strlcat(msg, ". ", MAXERRDETAIL);
746                 }
747                 if (hdr->status) {
748                         snprintf(msgchunk, MAXMSGCHUNK, "SG SCSI Status: 0x%02x; ", hdr->status);
749                         strlcat(msg, msgchunk, MAXERRDETAIL);
750                         // SCSI 3 status codes
751                         switch (hdr->status) {
752                         case 0x02:
753                                 strlcat(msg, "CHECK_CONDITION", MAXERRDETAIL);
754                                 break;
755                         case 0x04:
756                                 strlcat(msg, "CONDITION_MET", MAXERRDETAIL);
757                                 break;
758                         case 0x08:
759                                 strlcat(msg, "BUSY", MAXERRDETAIL);
760                                 break;
761                         case 0x10:
762                                 strlcat(msg, "INTERMEDIATE", MAXERRDETAIL);
763                                 break;
764                         case 0x14:
765                                 strlcat(msg, "INTERMEDIATE_CONDITION_MET", MAXERRDETAIL);
766                                 break;
767                         case 0x18:
768                                 strlcat(msg, "RESERVATION_CONFLICT", MAXERRDETAIL);
769                                 break;
770                         case 0x22:
771                                 strlcat(msg, "COMMAND_TERMINATED", MAXERRDETAIL);
772                                 break;
773                         case 0x28:
774                                 strlcat(msg, "TASK_SET_FULL", MAXERRDETAIL);
775                                 break;
776                         case 0x30:
777                                 strlcat(msg, "ACA_ACTIVE", MAXERRDETAIL);
778                                 break;
779                         case 0x40:
780                                 strlcat(msg, "TASK_ABORTED", MAXERRDETAIL);
781                                 break;
782                         default:
783                                 strlcat(msg, "Unknown", MAXERRDETAIL);
784                                 break;
785                         }
786                         strlcat(msg, ". ", MAXERRDETAIL);
787                 }
788                 if (hdr->sb_len_wr) {
789                         snprintf(msgchunk, MAXMSGCHUNK, "Sense Data (%d bytes):", hdr->sb_len_wr);
790                         strlcat(msg, msgchunk, MAXERRDETAIL);
791                         for (i = 0; i < hdr->sb_len_wr; i++) {
792                                 snprintf(msgchunk, MAXMSGCHUNK, " %02x", hdr->sbp[i]);
793                                 strlcat(msg, msgchunk, MAXERRDETAIL);
794                         }
795                         strlcat(msg, ". ", MAXERRDETAIL);
796                 }
797                 if (hdr->resid != 0) {
798                         snprintf(msgchunk, MAXMSGCHUNK, "SG Driver: %d bytes out of %d not transferred. ", hdr->resid, hdr->dxfer_len);
799                         strlcat(msg, msgchunk, MAXERRDETAIL);
800                 }
801         }
802
803         if (!(hdr->info & SG_INFO_CHECK) && !strlen(msg))
804                 strncpy(msg, "SG Driver did not report a Host, Driver or Device check",
805                         MAXERRDETAIL - 1);
806
807         return msg;
808 }
809
810 /*
811  * get max file size from read capacity.
812  */
813 static int fio_sgio_get_file_size(struct thread_data *td, struct fio_file *f)
814 {
815         /*
816          * get_file_size is being called even before sgio_init is
817          * called, so none of the sg_io structures are
818          * initialized in the thread_data yet.  So we need to do the
819          * ReadCapacity without any of those helpers.  One of the effects
820          * is that ReadCapacity may get called 4 times on each open:
821          * readcap(10) followed by readcap(16) if needed - just to get
822          * the file size after the init occurs - it will be called
823          * again when "type_check" is called during structure
824          * initialization I'm not sure how to prevent this little
825          * inefficiency.
826          */
827         unsigned int bs = 0;
828         unsigned long long max_lba = 0;
829         int ret;
830
831         if (fio_file_size_known(f))
832                 return 0;
833
834         if (f->filetype != FIO_TYPE_BLOCK && f->filetype != FIO_TYPE_CHAR) {
835                 td_verror(td, EINVAL, "wrong file type");
836                 log_err("ioengine sg only works on block or character devices\n");
837                 return 1;
838         }
839
840         ret = fio_sgio_read_capacity(td, &bs, &max_lba);
841         if (ret ) {
842                 td_verror(td, td->error, "fio_sgio_read_capacity");
843                 log_err("ioengine sg unable to successfully execute read capacity to get block size and maximum lba\n");
844                 return 1;
845         }
846
847         f->real_file_size = (max_lba + 1) * bs;
848         fio_file_set_size_known(f);
849         return 0;
850 }
851
852
853 static struct ioengine_ops ioengine = {
854         .name           = "sg",
855         .version        = FIO_IOOPS_VERSION,
856         .init           = fio_sgio_init,
857         .prep           = fio_sgio_prep,
858         .queue          = fio_sgio_queue,
859         .getevents      = fio_sgio_getevents,
860         .errdetails     = fio_sgio_errdetails,
861         .event          = fio_sgio_event,
862         .cleanup        = fio_sgio_cleanup,
863         .open_file      = fio_sgio_open,
864         .close_file     = generic_close_file,
865         .get_file_size  = fio_sgio_get_file_size,
866         .flags          = FIO_SYNCIO | FIO_RAWIO,
867         .options        = options,
868         .option_struct_size     = sizeof(struct sg_options)
869 };
870
871 #else /* FIO_HAVE_SGIO */
872
873 /*
874  * When we have a proper configure system in place, we simply wont build
875  * and install this io engine. For now install a crippled version that
876  * just complains and fails to load.
877  */
878 static int fio_sgio_init(struct thread_data fio_unused *td)
879 {
880         log_err("fio: ioengine sg not available\n");
881         return 1;
882 }
883
884 static struct ioengine_ops ioengine = {
885         .name           = "sg",
886         .version        = FIO_IOOPS_VERSION,
887         .init           = fio_sgio_init,
888 };
889
890 #endif
891
892 static void fio_init fio_sgio_register(void)
893 {
894         register_ioengine(&ioengine);
895 }
896
897 static void fio_exit fio_sgio_unregister(void)
898 {
899         unregister_ioengine(&ioengine);
900 }