87fe88352f2fd0d4eb77430e827cb4ffab8dbc92
[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
16 #ifdef FIO_HAVE_SGIO
17
18 struct sgio_cmd {
19         unsigned char cdb[10];
20         int nr;
21 };
22
23 struct sgio_data {
24         struct sgio_cmd *cmds;
25         struct io_u **events;
26         struct pollfd *pfds;
27         int *fd_flags;
28         void *sgbuf;
29         unsigned int bs;
30         int type_checked;
31 };
32
33 static void sgio_hdr_init(struct sgio_data *sd, struct sg_io_hdr *hdr,
34                           struct io_u *io_u, int fs)
35 {
36         struct sgio_cmd *sc = &sd->cmds[io_u->index];
37
38         memset(hdr, 0, sizeof(*hdr));
39         memset(sc->cdb, 0, sizeof(sc->cdb));
40
41         hdr->interface_id = 'S';
42         hdr->cmdp = sc->cdb;
43         hdr->cmd_len = sizeof(sc->cdb);
44         hdr->pack_id = io_u->index;
45         hdr->usr_ptr = io_u;
46
47         if (fs) {
48                 hdr->dxferp = io_u->xfer_buf;
49                 hdr->dxfer_len = io_u->xfer_buflen;
50         }
51 }
52
53 static int pollin_events(struct pollfd *pfds, int fds)
54 {
55         int i;
56
57         for (i = 0; i < fds; i++)
58                 if (pfds[i].revents & POLLIN)
59                         return 1;
60
61         return 0;
62 }
63
64 static int fio_sgio_getevents(struct thread_data *td, unsigned int min,
65                               unsigned int max, struct timespec fio_unused *t)
66 {
67         struct sgio_data *sd = td->io_ops->data;
68         int left = max, ret, r = 0;
69         void *buf = sd->sgbuf;
70         unsigned int i, events;
71         struct fio_file *f;
72
73         /*
74          * Fill in the file descriptors
75          */
76         for_each_file(td, f, i) {
77                 /*
78                  * don't block for min events == 0
79                  */
80                 if (!min)
81                         fio_set_fd_nonblocking(f->fd, "sg");
82
83                 sd->pfds[i].fd = f->fd;
84                 sd->pfds[i].events = POLLIN;
85         }
86
87         while (left) {
88                 void *p;
89
90                 do {
91                         if (!min)
92                                 break;
93
94                         ret = poll(sd->pfds, td->o.nr_files, -1);
95                         if (ret < 0) {
96                                 if (!r)
97                                         r = -errno;
98                                 td_verror(td, errno, "poll");
99                                 break;
100                         } else if (!ret)
101                                 continue;
102
103                         if (pollin_events(sd->pfds, td->o.nr_files))
104                                 break;
105                 } while (1);
106
107                 if (r < 0)
108                         break;
109
110 re_read:
111                 p = buf;
112                 events = 0;
113                 for_each_file(td, f, i) {
114                         ret = read(f->fd, p, left * sizeof(struct sg_io_hdr));
115                         if (ret < 0) {
116                                 if (errno == EAGAIN)
117                                         continue;
118                                 r = -errno;
119                                 td_verror(td, errno, "read");
120                                 break;
121                         } else if (ret) {
122                                 p += ret;
123                                 events += ret / sizeof(struct sg_io_hdr);
124                         }
125                 }
126
127                 if (r < 0)
128                         break;
129                 if (!events) {
130                         usleep(1000);
131                         goto re_read;
132                 }
133
134                 left -= events;
135                 r += events;
136
137                 for (i = 0; i < events; i++) {
138                         struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i;
139
140                         sd->events[i] = hdr->usr_ptr;
141                 }
142         }
143
144         if (!min) {
145                 for_each_file(td, f, i) {
146                         if (fcntl(f->fd, F_SETFL, sd->fd_flags[i]) < 0)
147                                 log_err("fio: sg failed to restore fcntl flags: %s\n", strerror(errno));
148                 }
149         }
150
151         return r;
152 }
153
154 static int fio_sgio_ioctl_doio(struct thread_data *td,
155                                struct fio_file *f, struct io_u *io_u)
156 {
157         struct sgio_data *sd = td->io_ops->data;
158         struct sg_io_hdr *hdr = &io_u->hdr;
159         int ret;
160
161         sd->events[0] = io_u;
162
163         ret = ioctl(f->fd, SG_IO, hdr);
164         if (ret < 0)
165                 return ret;
166
167         return FIO_Q_COMPLETED;
168 }
169
170 static int fio_sgio_rw_doio(struct fio_file *f, struct io_u *io_u, int do_sync)
171 {
172         struct sg_io_hdr *hdr = &io_u->hdr;
173         int ret;
174
175         ret = write(f->fd, hdr, sizeof(*hdr));
176         if (ret < 0)
177                 return ret;
178
179         if (do_sync) {
180                 ret = read(f->fd, hdr, sizeof(*hdr));
181                 if (ret < 0)
182                         return ret;
183                 return FIO_Q_COMPLETED;
184         }
185
186         return FIO_Q_QUEUED;
187 }
188
189 static int fio_sgio_doio(struct thread_data *td, struct io_u *io_u, int do_sync)
190 {
191         struct fio_file *f = io_u->file;
192
193         if (f->filetype == FIO_TYPE_BD)
194                 return fio_sgio_ioctl_doio(td, f, io_u);
195
196         return fio_sgio_rw_doio(f, io_u, do_sync);
197 }
198
199 static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u)
200 {
201         struct sg_io_hdr *hdr = &io_u->hdr;
202         struct sgio_data *sd = td->io_ops->data;
203         int nr_blocks, lba;
204
205         if (io_u->xfer_buflen & (sd->bs - 1)) {
206                 log_err("read/write not sector aligned\n");
207                 return EINVAL;
208         }
209
210         if (io_u->ddir == DDIR_READ) {
211                 sgio_hdr_init(sd, hdr, io_u, 1);
212
213                 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
214                 hdr->cmdp[0] = 0x28;
215         } else if (io_u->ddir == DDIR_WRITE) {
216                 sgio_hdr_init(sd, hdr, io_u, 1);
217
218                 hdr->dxfer_direction = SG_DXFER_TO_DEV;
219                 hdr->cmdp[0] = 0x2a;
220         } else {
221                 sgio_hdr_init(sd, hdr, io_u, 0);
222
223                 hdr->dxfer_direction = SG_DXFER_NONE;
224                 hdr->cmdp[0] = 0x35;
225         }
226
227         if (hdr->dxfer_direction != SG_DXFER_NONE) {
228                 nr_blocks = io_u->xfer_buflen / sd->bs;
229                 lba = io_u->offset / sd->bs;
230                 hdr->cmdp[2] = (unsigned char) ((lba >> 24) & 0xff);
231                 hdr->cmdp[3] = (unsigned char) ((lba >> 16) & 0xff);
232                 hdr->cmdp[4] = (unsigned char) ((lba >>  8) & 0xff);
233                 hdr->cmdp[5] = (unsigned char) (lba & 0xff);
234                 hdr->cmdp[7] = (unsigned char) ((nr_blocks >> 8) & 0xff);
235                 hdr->cmdp[8] = (unsigned char) (nr_blocks & 0xff);
236         }
237
238         return 0;
239 }
240
241 static int fio_sgio_queue(struct thread_data *td, struct io_u *io_u)
242 {
243         struct sg_io_hdr *hdr = &io_u->hdr;
244         int ret, do_sync = 0;
245
246         fio_ro_check(td, io_u);
247
248         if (td->o.sync_io || td->o.odirect || ddir_sync(io_u->ddir))
249                 do_sync = 1;
250
251         ret = fio_sgio_doio(td, io_u, do_sync);
252
253         if (ret < 0)
254                 io_u->error = errno;
255         else if (hdr->status) {
256                 io_u->resid = hdr->resid;
257                 io_u->error = EIO;
258         }
259
260         if (io_u->error) {
261                 td_verror(td, io_u->error, "xfer");
262                 return FIO_Q_COMPLETED;
263         }
264
265         return ret;
266 }
267
268 static struct io_u *fio_sgio_event(struct thread_data *td, int event)
269 {
270         struct sgio_data *sd = td->io_ops->data;
271
272         return sd->events[event];
273 }
274
275 static int fio_sgio_get_bs(struct thread_data *td, unsigned int *bs)
276 {
277         struct sgio_data *sd = td->io_ops->data;
278         struct io_u io_u;
279         struct sg_io_hdr *hdr;
280         unsigned char buf[8];
281         int ret;
282
283         memset(&io_u, 0, sizeof(io_u));
284         io_u.file = td->files[0];
285
286         hdr = &io_u.hdr;
287         sgio_hdr_init(sd, hdr, &io_u, 0);
288         memset(buf, 0, sizeof(buf));
289
290         hdr->cmdp[0] = 0x25;
291         hdr->dxfer_direction = SG_DXFER_FROM_DEV;
292         hdr->dxferp = buf;
293         hdr->dxfer_len = sizeof(buf);
294
295         ret = fio_sgio_doio(td, &io_u, 1);
296         if (ret)
297                 return ret;
298
299         *bs = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
300         return 0;
301 }
302
303 static void fio_sgio_cleanup(struct thread_data *td)
304 {
305         struct sgio_data *sd = td->io_ops->data;
306
307         if (sd) {
308                 free(sd->events);
309                 free(sd->cmds);
310                 free(sd->fd_flags);
311                 free(sd->pfds);
312                 free(sd->sgbuf);
313                 free(sd);
314         }
315 }
316
317 static int fio_sgio_init(struct thread_data *td)
318 {
319         struct sgio_data *sd;
320
321         sd = malloc(sizeof(*sd));
322         memset(sd, 0, sizeof(*sd));
323         sd->cmds = malloc(td->o.iodepth * sizeof(struct sgio_cmd));
324         memset(sd->cmds, 0, td->o.iodepth * sizeof(struct sgio_cmd));
325         sd->events = malloc(td->o.iodepth * sizeof(struct io_u *));
326         memset(sd->events, 0, td->o.iodepth * sizeof(struct io_u *));
327         sd->pfds = malloc(sizeof(struct pollfd) * td->o.nr_files);
328         memset(sd->pfds, 0, sizeof(struct pollfd) * td->o.nr_files);
329         sd->fd_flags = malloc(sizeof(int) * td->o.nr_files);
330         memset(sd->fd_flags, 0, sizeof(int) * td->o.nr_files);
331         sd->sgbuf = malloc(sizeof(struct sg_io_hdr) * td->o.iodepth);
332         memset(sd->sgbuf, 0, sizeof(struct sg_io_hdr) * td->o.iodepth);
333
334         td->io_ops->data = sd;
335
336         /*
337          * we want to do it, regardless of whether odirect is set or not
338          */
339         td->o.override_sync = 1;
340         return 0;
341 }
342
343 static int fio_sgio_type_check(struct thread_data *td, struct fio_file *f)
344 {
345         struct sgio_data *sd = td->io_ops->data;
346         unsigned int bs;
347
348         if (f->filetype == FIO_TYPE_BD) {
349                 if (ioctl(f->fd, BLKSSZGET, &bs) < 0) {
350                         td_verror(td, errno, "ioctl");
351                         return 1;
352                 }
353         } else if (f->filetype == FIO_TYPE_CHAR) {
354                 int version, ret;
355
356                 if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) {
357                         td_verror(td, errno, "ioctl");
358                         return 1;
359                 }
360
361                 ret = fio_sgio_get_bs(td, &bs);
362                 if (ret)
363                         return 1;
364         } else {
365                 log_err("ioengine sg only works on block devices\n");
366                 return 1;
367         }
368
369         sd->bs = bs;
370
371         if (f->filetype == FIO_TYPE_BD) {
372                 td->io_ops->getevents = NULL;
373                 td->io_ops->event = NULL;
374         }
375
376         return 0;
377 }
378
379 static int fio_sgio_open(struct thread_data *td, struct fio_file *f)
380 {
381         struct sgio_data *sd = td->io_ops->data;
382         int ret;
383
384         ret = generic_open_file(td, f);
385         if (ret)
386                 return ret;
387
388         if (sd && !sd->type_checked && fio_sgio_type_check(td, f)) {
389                 ret = generic_close_file(td, f);
390                 return 1;
391         }
392
393         return 0;
394 }
395
396 static struct ioengine_ops ioengine = {
397         .name           = "sg",
398         .version        = FIO_IOOPS_VERSION,
399         .init           = fio_sgio_init,
400         .prep           = fio_sgio_prep,
401         .queue          = fio_sgio_queue,
402         .getevents      = fio_sgio_getevents,
403         .event          = fio_sgio_event,
404         .cleanup        = fio_sgio_cleanup,
405         .open_file      = fio_sgio_open,
406         .close_file     = generic_close_file,
407         .get_file_size  = generic_get_file_size,
408         .flags          = FIO_SYNCIO | FIO_RAWIO,
409 };
410
411 #else /* FIO_HAVE_SGIO */
412
413 /*
414  * When we have a proper configure system in place, we simply wont build
415  * and install this io engine. For now install a crippled version that
416  * just complains and fails to load.
417  */
418 static int fio_sgio_init(struct thread_data fio_unused *td)
419 {
420         log_err("fio: ioengine sg not available\n");
421         return 1;
422 }
423
424 static struct ioengine_ops ioengine = {
425         .name           = "sg",
426         .version        = FIO_IOOPS_VERSION,
427         .init           = fio_sgio_init,
428 };
429
430 #endif
431
432 static void fio_init fio_sgio_register(void)
433 {
434         register_ioengine(&ioengine);
435 }
436
437 static void fio_exit fio_sgio_unregister(void)
438 {
439         unregister_ioengine(&ioengine);
440 }