blob: fcd9c41397a8bf20644b04336c2020bdc63b2215 [file] [log] [blame]
Jens Axboe2866c822006-10-09 15:57:48 +02001/*
Jens Axboeda751ca2007-03-14 10:59:33 +01002 * sg engine
3 *
4 * IO engine that uses the Linux SG v3 interface to talk to SCSI devices
Jens Axboe2866c822006-10-09 15:57:48 +02005 *
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>
Jens Axboe5f350952006-11-07 15:20:59 +010013
14#include "../fio.h"
Jens Axboe2866c822006-10-09 15:57:48 +020015
Jens Axboe34cfcda2006-11-03 14:00:45 +010016#ifdef FIO_HAVE_SGIO
17
Jens Axboe2866c822006-10-09 15:57:48 +020018struct sgio_cmd {
19 unsigned char cdb[10];
20 int nr;
21};
22
23struct sgio_data {
24 struct sgio_cmd *cmds;
25 struct io_u **events;
Jens Axboedc0deca2007-02-08 20:59:31 +010026 struct pollfd *pfds;
27 int *fd_flags;
28 void *sgbuf;
Jens Axboe2866c822006-10-09 15:57:48 +020029 unsigned int bs;
Jens Axboeb5af8292007-03-08 12:43:13 +010030 int type_checked;
Jens Axboe2866c822006-10-09 15:57:48 +020031};
32
33static 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) {
Jens Axboecec6b552007-02-06 20:15:38 +010048 hdr->dxferp = io_u->xfer_buf;
49 hdr->dxfer_len = io_u->xfer_buflen;
Jens Axboe2866c822006-10-09 15:57:48 +020050 }
51}
52
Jens Axboeadee86c2007-02-08 13:04:36 +010053static 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}
Jens Axboe2866c822006-10-09 15:57:48 +020063
Jens Axboee7d2e612007-12-11 10:49:39 +010064static int fio_sgio_getevents(struct thread_data *td, unsigned int min,
65 unsigned int max, struct timespec fio_unused *t)
Jens Axboe2866c822006-10-09 15:57:48 +020066{
67 struct sgio_data *sd = td->io_ops->data;
Jens Axboeaf52b342007-03-13 10:07:47 +010068 int left = max, ret, r = 0;
Jens Axboedc0deca2007-02-08 20:59:31 +010069 void *buf = sd->sgbuf;
Jens Axboeaf52b342007-03-13 10:07:47 +010070 unsigned int i, events;
Jens Axboe946ff862007-04-02 11:04:47 +020071 struct fio_file *f;
Jens Axboe2866c822006-10-09 15:57:48 +020072
73 /*
Jens Axboeadee86c2007-02-08 13:04:36 +010074 * Fill in the file descriptors
Jens Axboe2866c822006-10-09 15:57:48 +020075 */
Jens Axboeadee86c2007-02-08 13:04:36 +010076 for_each_file(td, f, i) {
77 /*
78 * don't block for min events == 0
79 */
Jens Axboe4a851612014-04-14 10:04:21 -060080 if (!min)
81 fio_set_fd_nonblocking(f->fd, "sg");
82
Jens Axboedc0deca2007-02-08 20:59:31 +010083 sd->pfds[i].fd = f->fd;
84 sd->pfds[i].events = POLLIN;
Jens Axboe2866c822006-10-09 15:57:48 +020085 }
86
87 while (left) {
Jens Axboeadee86c2007-02-08 13:04:36 +010088 void *p;
89
Jens Axboe2866c822006-10-09 15:57:48 +020090 do {
91 if (!min)
92 break;
Jens Axboeadee86c2007-02-08 13:04:36 +010093
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010094 ret = poll(sd->pfds, td->o.nr_files, -1);
Jens Axboeadee86c2007-02-08 13:04:36 +010095 if (ret < 0) {
Jens Axboeadee86c2007-02-08 13:04:36 +010096 if (!r)
Jens Axboe22819ec2007-02-18 07:47:14 +010097 r = -errno;
Jens Axboee1161c32007-02-22 19:36:48 +010098 td_verror(td, errno, "poll");
Jens Axboeadee86c2007-02-08 13:04:36 +010099 break;
100 } else if (!ret)
101 continue;
102
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100103 if (pollin_events(sd->pfds, td->o.nr_files))
Jens Axboe2866c822006-10-09 15:57:48 +0200104 break;
105 } while (1);
106
Jens Axboeadee86c2007-02-08 13:04:36 +0100107 if (r < 0)
Jens Axboe2866c822006-10-09 15:57:48 +0200108 break;
109
Jens Axboeadee86c2007-02-08 13:04:36 +0100110re_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;
Jens Axboe22819ec2007-02-18 07:47:14 +0100118 r = -errno;
Jens Axboee1161c32007-02-22 19:36:48 +0100119 td_verror(td, errno, "read");
Jens Axboeadee86c2007-02-08 13:04:36 +0100120 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
Jens Axboe2866c822006-10-09 15:57:48 +0200134 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
Jens Axboeadee86c2007-02-08 13:04:36 +0100144 if (!min) {
145 for_each_file(td, f, i)
Jens Axboedc0deca2007-02-08 20:59:31 +0100146 fcntl(f->fd, F_SETFL, sd->fd_flags[i]);
Jens Axboeadee86c2007-02-08 13:04:36 +0100147 }
Jens Axboe2866c822006-10-09 15:57:48 +0200148
Jens Axboe2866c822006-10-09 15:57:48 +0200149 return r;
150}
151
Jens Axboe7a16dd02006-10-18 17:21:58 +0200152static int fio_sgio_ioctl_doio(struct thread_data *td,
153 struct fio_file *f, struct io_u *io_u)
Jens Axboe2866c822006-10-09 15:57:48 +0200154{
155 struct sgio_data *sd = td->io_ops->data;
156 struct sg_io_hdr *hdr = &io_u->hdr;
Jens Axboe36167d82007-02-18 05:41:31 +0100157 int ret;
Jens Axboe2866c822006-10-09 15:57:48 +0200158
159 sd->events[0] = io_u;
160
Jens Axboe36167d82007-02-18 05:41:31 +0100161 ret = ioctl(f->fd, SG_IO, hdr);
162 if (ret < 0)
Jens Axboea05bd422007-04-02 10:42:33 +0200163 return ret;
Jens Axboe36167d82007-02-18 05:41:31 +0100164
165 return FIO_Q_COMPLETED;
Jens Axboe2866c822006-10-09 15:57:48 +0200166}
167
Jens Axboe2b13e712011-01-19 14:04:16 -0700168static int fio_sgio_rw_doio(struct fio_file *f, struct io_u *io_u, int do_sync)
Jens Axboe2866c822006-10-09 15:57:48 +0200169{
170 struct sg_io_hdr *hdr = &io_u->hdr;
171 int ret;
172
Jens Axboe53cdc682006-10-18 11:50:58 +0200173 ret = write(f->fd, hdr, sizeof(*hdr));
Jens Axboe2866c822006-10-09 15:57:48 +0200174 if (ret < 0)
Jens Axboea05bd422007-04-02 10:42:33 +0200175 return ret;
Jens Axboe2866c822006-10-09 15:57:48 +0200176
Jens Axboe2b13e712011-01-19 14:04:16 -0700177 if (do_sync) {
Jens Axboe53cdc682006-10-18 11:50:58 +0200178 ret = read(f->fd, hdr, sizeof(*hdr));
Jens Axboe2866c822006-10-09 15:57:48 +0200179 if (ret < 0)
Jens Axboea05bd422007-04-02 10:42:33 +0200180 return ret;
Jens Axboe36167d82007-02-18 05:41:31 +0100181 return FIO_Q_COMPLETED;
Jens Axboe2866c822006-10-09 15:57:48 +0200182 }
183
Jens Axboe36167d82007-02-18 05:41:31 +0100184 return FIO_Q_QUEUED;
Jens Axboe2866c822006-10-09 15:57:48 +0200185}
186
Jens Axboe2b13e712011-01-19 14:04:16 -0700187static int fio_sgio_doio(struct thread_data *td, struct io_u *io_u, int do_sync)
Jens Axboe2866c822006-10-09 15:57:48 +0200188{
Jens Axboe53cdc682006-10-18 11:50:58 +0200189 struct fio_file *f = io_u->file;
Jens Axboe2866c822006-10-09 15:57:48 +0200190
Jens Axboeaf52b342007-03-13 10:07:47 +0100191 if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200192 return fio_sgio_ioctl_doio(td, f, io_u);
193
Jens Axboe2b13e712011-01-19 14:04:16 -0700194 return fio_sgio_rw_doio(f, io_u, do_sync);
Jens Axboe2866c822006-10-09 15:57:48 +0200195}
196
Jens Axboe2866c822006-10-09 15:57:48 +0200197static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u)
198{
199 struct sg_io_hdr *hdr = &io_u->hdr;
200 struct sgio_data *sd = td->io_ops->data;
201 int nr_blocks, lba;
202
Jens Axboecec6b552007-02-06 20:15:38 +0100203 if (io_u->xfer_buflen & (sd->bs - 1)) {
Jens Axboe2866c822006-10-09 15:57:48 +0200204 log_err("read/write not sector aligned\n");
205 return EINVAL;
206 }
207
Jens Axboe2866c822006-10-09 15:57:48 +0200208 if (io_u->ddir == DDIR_READ) {
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200209 sgio_hdr_init(sd, hdr, io_u, 1);
210
Jens Axboe2866c822006-10-09 15:57:48 +0200211 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
212 hdr->cmdp[0] = 0x28;
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200213 } else if (io_u->ddir == DDIR_WRITE) {
214 sgio_hdr_init(sd, hdr, io_u, 1);
215
Jens Axboe2866c822006-10-09 15:57:48 +0200216 hdr->dxfer_direction = SG_DXFER_TO_DEV;
217 hdr->cmdp[0] = 0x2a;
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200218 } else {
219 sgio_hdr_init(sd, hdr, io_u, 0);
220
221 hdr->dxfer_direction = SG_DXFER_NONE;
222 hdr->cmdp[0] = 0x35;
Jens Axboe2866c822006-10-09 15:57:48 +0200223 }
224
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200225 if (hdr->dxfer_direction != SG_DXFER_NONE) {
Jens Axboecec6b552007-02-06 20:15:38 +0100226 nr_blocks = io_u->xfer_buflen / sd->bs;
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200227 lba = io_u->offset / sd->bs;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100228 hdr->cmdp[2] = (unsigned char) ((lba >> 24) & 0xff);
229 hdr->cmdp[3] = (unsigned char) ((lba >> 16) & 0xff);
230 hdr->cmdp[4] = (unsigned char) ((lba >> 8) & 0xff);
231 hdr->cmdp[5] = (unsigned char) (lba & 0xff);
232 hdr->cmdp[7] = (unsigned char) ((nr_blocks >> 8) & 0xff);
233 hdr->cmdp[8] = (unsigned char) (nr_blocks & 0xff);
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200234 }
235
Jens Axboe2866c822006-10-09 15:57:48 +0200236 return 0;
237}
238
239static int fio_sgio_queue(struct thread_data *td, struct io_u *io_u)
240{
241 struct sg_io_hdr *hdr = &io_u->hdr;
Jens Axboef6db4fa2009-06-16 23:23:01 +0200242 int ret, do_sync = 0;
Jens Axboe2866c822006-10-09 15:57:48 +0200243
Jens Axboe7101d9c2007-09-12 13:12:39 +0200244 fio_ro_check(td, io_u);
245
Jens Axboef6db4fa2009-06-16 23:23:01 +0200246 if (td->o.sync_io || td->o.odirect || ddir_sync(io_u->ddir))
247 do_sync = 1;
248
249 ret = fio_sgio_doio(td, io_u, do_sync);
Jens Axboe2866c822006-10-09 15:57:48 +0200250
251 if (ret < 0)
252 io_u->error = errno;
253 else if (hdr->status) {
254 io_u->resid = hdr->resid;
255 io_u->error = EIO;
256 }
257
Jens Axboe95bcd812007-02-11 01:01:57 +0100258 if (io_u->error) {
Jens Axboee1161c32007-02-22 19:36:48 +0100259 td_verror(td, io_u->error, "xfer");
Jens Axboe36167d82007-02-18 05:41:31 +0100260 return FIO_Q_COMPLETED;
Jens Axboe95bcd812007-02-11 01:01:57 +0100261 }
262
Jens Axboe36167d82007-02-18 05:41:31 +0100263 return ret;
Jens Axboe2866c822006-10-09 15:57:48 +0200264}
265
266static struct io_u *fio_sgio_event(struct thread_data *td, int event)
267{
268 struct sgio_data *sd = td->io_ops->data;
269
270 return sd->events[event];
271}
272
273static int fio_sgio_get_bs(struct thread_data *td, unsigned int *bs)
274{
275 struct sgio_data *sd = td->io_ops->data;
Jens Axboe2039ab12010-08-27 09:04:00 +0200276 struct io_u io_u;
Jens Axboe2866c822006-10-09 15:57:48 +0200277 struct sg_io_hdr *hdr;
278 unsigned char buf[8];
279 int ret;
280
Jens Axboe2039ab12010-08-27 09:04:00 +0200281 memset(&io_u, 0, sizeof(io_u));
282 io_u.file = td->files[0];
Jens Axboe2866c822006-10-09 15:57:48 +0200283
Jens Axboe2039ab12010-08-27 09:04:00 +0200284 hdr = &io_u.hdr;
285 sgio_hdr_init(sd, hdr, &io_u, 0);
Jens Axboe2866c822006-10-09 15:57:48 +0200286 memset(buf, 0, sizeof(buf));
287
288 hdr->cmdp[0] = 0x25;
289 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
290 hdr->dxferp = buf;
291 hdr->dxfer_len = sizeof(buf);
292
Jens Axboe2039ab12010-08-27 09:04:00 +0200293 ret = fio_sgio_doio(td, &io_u, 1);
294 if (ret)
Jens Axboe2866c822006-10-09 15:57:48 +0200295 return ret;
Jens Axboe2866c822006-10-09 15:57:48 +0200296
297 *bs = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
Jens Axboe2866c822006-10-09 15:57:48 +0200298 return 0;
299}
300
301static void fio_sgio_cleanup(struct thread_data *td)
302{
Jens Axboedc0deca2007-02-08 20:59:31 +0100303 struct sgio_data *sd = td->io_ops->data;
304
305 if (sd) {
306 free(sd->events);
307 free(sd->cmds);
308 free(sd->fd_flags);
309 free(sd->pfds);
310 free(sd->sgbuf);
311 free(sd);
Jens Axboe2866c822006-10-09 15:57:48 +0200312 }
313}
314
315static int fio_sgio_init(struct thread_data *td)
316{
317 struct sgio_data *sd;
Jens Axboe2866c822006-10-09 15:57:48 +0200318
319 sd = malloc(sizeof(*sd));
Jens Axboecb781c72006-11-07 14:02:48 +0100320 memset(sd, 0, sizeof(*sd));
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100321 sd->cmds = malloc(td->o.iodepth * sizeof(struct sgio_cmd));
322 memset(sd->cmds, 0, td->o.iodepth * sizeof(struct sgio_cmd));
323 sd->events = malloc(td->o.iodepth * sizeof(struct io_u *));
324 memset(sd->events, 0, td->o.iodepth * sizeof(struct io_u *));
325 sd->pfds = malloc(sizeof(struct pollfd) * td->o.nr_files);
326 memset(sd->pfds, 0, sizeof(struct pollfd) * td->o.nr_files);
327 sd->fd_flags = malloc(sizeof(int) * td->o.nr_files);
328 memset(sd->fd_flags, 0, sizeof(int) * td->o.nr_files);
329 sd->sgbuf = malloc(sizeof(struct sg_io_hdr) * td->o.iodepth);
330 memset(sd->sgbuf, 0, sizeof(struct sg_io_hdr) * td->o.iodepth);
Jens Axboedc0deca2007-02-08 20:59:31 +0100331
Jens Axboe2866c822006-10-09 15:57:48 +0200332 td->io_ops->data = sd;
333
Jens Axboeb5af8292007-03-08 12:43:13 +0100334 /*
335 * we want to do it, regardless of whether odirect is set or not
336 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100337 td->o.override_sync = 1;
Jens Axboeb5af8292007-03-08 12:43:13 +0100338 return 0;
339}
340
341static int fio_sgio_type_check(struct thread_data *td, struct fio_file *f)
342{
343 struct sgio_data *sd = td->io_ops->data;
344 unsigned int bs;
345
Jens Axboeaf52b342007-03-13 10:07:47 +0100346 if (f->filetype == FIO_TYPE_BD) {
Jens Axboe53cdc682006-10-18 11:50:58 +0200347 if (ioctl(f->fd, BLKSSZGET, &bs) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100348 td_verror(td, errno, "ioctl");
Jens Axboeb5af8292007-03-08 12:43:13 +0100349 return 1;
Jens Axboe2866c822006-10-09 15:57:48 +0200350 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100351 } else if (f->filetype == FIO_TYPE_CHAR) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100352 int version, ret;
Jens Axboe2866c822006-10-09 15:57:48 +0200353
Jens Axboe53cdc682006-10-18 11:50:58 +0200354 if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100355 td_verror(td, errno, "ioctl");
Jens Axboeb5af8292007-03-08 12:43:13 +0100356 return 1;
Jens Axboe2866c822006-10-09 15:57:48 +0200357 }
358
359 ret = fio_sgio_get_bs(td, &bs);
360 if (ret)
Jens Axboeb5af8292007-03-08 12:43:13 +0100361 return 1;
Jens Axboe2866c822006-10-09 15:57:48 +0200362 } else {
Gurudas Paid0c70932008-05-30 13:35:00 +0200363 log_err("ioengine sg only works on block devices\n");
Jens Axboeb5af8292007-03-08 12:43:13 +0100364 return 1;
Jens Axboe2866c822006-10-09 15:57:48 +0200365 }
366
367 sd->bs = bs;
368
Jens Axboeaf52b342007-03-13 10:07:47 +0100369 if (f->filetype == FIO_TYPE_BD) {
Jens Axboe36167d82007-02-18 05:41:31 +0100370 td->io_ops->getevents = NULL;
371 td->io_ops->event = NULL;
372 }
Jens Axboe2866c822006-10-09 15:57:48 +0200373
Jens Axboe2866c822006-10-09 15:57:48 +0200374 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100375}
376
377static int fio_sgio_open(struct thread_data *td, struct fio_file *f)
378{
379 struct sgio_data *sd = td->io_ops->data;
380 int ret;
381
382 ret = generic_open_file(td, f);
383 if (ret)
384 return ret;
385
Jens Axboe15ba6402007-04-02 10:51:10 +0200386 if (sd && !sd->type_checked && fio_sgio_type_check(td, f)) {
Jens Axboe6977bcd2008-03-01 15:55:36 +0100387 ret = generic_close_file(td, f);
Jens Axboeb5af8292007-03-08 12:43:13 +0100388 return 1;
389 }
390
391 return 0;
Jens Axboe2866c822006-10-09 15:57:48 +0200392}
393
Jens Axboe5f350952006-11-07 15:20:59 +0100394static struct ioengine_ops ioengine = {
Jens Axboe2866c822006-10-09 15:57:48 +0200395 .name = "sg",
396 .version = FIO_IOOPS_VERSION,
397 .init = fio_sgio_init,
398 .prep = fio_sgio_prep,
399 .queue = fio_sgio_queue,
400 .getevents = fio_sgio_getevents,
401 .event = fio_sgio_event,
402 .cleanup = fio_sgio_cleanup,
Jens Axboeb5af8292007-03-08 12:43:13 +0100403 .open_file = fio_sgio_open,
404 .close_file = generic_close_file,
Jens Axboedf9c26b2009-03-05 10:13:58 +0100405 .get_file_size = generic_get_file_size,
Jens Axboeb2a15192006-10-18 14:10:42 +0200406 .flags = FIO_SYNCIO | FIO_RAWIO,
Jens Axboe2866c822006-10-09 15:57:48 +0200407};
Jens Axboe34cfcda2006-11-03 14:00:45 +0100408
409#else /* FIO_HAVE_SGIO */
410
411/*
412 * When we have a proper configure system in place, we simply wont build
413 * and install this io engine. For now install a crippled version that
414 * just complains and fails to load.
415 */
416static int fio_sgio_init(struct thread_data fio_unused *td)
417{
Jens Axboea3edaf72010-09-26 10:53:40 +0900418 log_err("fio: ioengine sg not available\n");
Jens Axboe34cfcda2006-11-03 14:00:45 +0100419 return 1;
420}
421
Jens Axboe5f350952006-11-07 15:20:59 +0100422static struct ioengine_ops ioengine = {
Gurudas Paid0c70932008-05-30 13:35:00 +0200423 .name = "sg",
Jens Axboe34cfcda2006-11-03 14:00:45 +0100424 .version = FIO_IOOPS_VERSION,
425 .init = fio_sgio_init,
426};
427
428#endif
Jens Axboe5f350952006-11-07 15:20:59 +0100429
430static void fio_init fio_sgio_register(void)
431{
432 register_ioengine(&ioengine);
433}
434
435static void fio_exit fio_sgio_unregister(void)
436{
437 unregister_ioengine(&ioengine);
438}