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