blob: 8d086bffe6cad7431cd9243932538d735e133cfa [file] [log] [blame]
Jens Axboe2866c822006-10-09 15:57:48 +02001/*
2 * scsi generic sg v3 io engine
3 *
4 */
5#include <stdio.h>
6#include <stdlib.h>
7#include <unistd.h>
8#include <errno.h>
9#include <assert.h>
10#include <sys/poll.h>
Jens Axboe5f350952006-11-07 15:20:59 +010011
12#include "../fio.h"
13#include "../os.h"
Jens Axboe2866c822006-10-09 15:57:48 +020014
Jens Axboe34cfcda2006-11-03 14:00:45 +010015#ifdef FIO_HAVE_SGIO
16
Jens Axboe2866c822006-10-09 15:57:48 +020017struct sgio_cmd {
18 unsigned char cdb[10];
19 int nr;
20};
21
22struct sgio_data {
23 struct sgio_cmd *cmds;
24 struct io_u **events;
Jens Axboedc0deca2007-02-08 20:59:31 +010025 struct pollfd *pfds;
26 int *fd_flags;
27 void *sgbuf;
Jens Axboe2866c822006-10-09 15:57:48 +020028 unsigned int bs;
29};
30
31static void sgio_hdr_init(struct sgio_data *sd, struct sg_io_hdr *hdr,
32 struct io_u *io_u, int fs)
33{
34 struct sgio_cmd *sc = &sd->cmds[io_u->index];
35
36 memset(hdr, 0, sizeof(*hdr));
37 memset(sc->cdb, 0, sizeof(sc->cdb));
38
39 hdr->interface_id = 'S';
40 hdr->cmdp = sc->cdb;
41 hdr->cmd_len = sizeof(sc->cdb);
42 hdr->pack_id = io_u->index;
43 hdr->usr_ptr = io_u;
44
45 if (fs) {
Jens Axboecec6b552007-02-06 20:15:38 +010046 hdr->dxferp = io_u->xfer_buf;
47 hdr->dxfer_len = io_u->xfer_buflen;
Jens Axboe2866c822006-10-09 15:57:48 +020048 }
49}
50
51static int fio_sgio_ioctl_getevents(struct thread_data *td, int fio_unused min,
52 int max, struct timespec fio_unused *t)
53{
54 assert(max <= 1);
55
56 /*
57 * we can only have one finished io_u for sync io, since the depth
58 * is always 1
59 */
60 if (list_empty(&td->io_u_busylist))
61 return 0;
62
63 return 1;
64}
65
Jens Axboeadee86c2007-02-08 13:04:36 +010066static int pollin_events(struct pollfd *pfds, int fds)
67{
68 int i;
69
70 for (i = 0; i < fds; i++)
71 if (pfds[i].revents & POLLIN)
72 return 1;
73
74 return 0;
75}
Jens Axboe2866c822006-10-09 15:57:48 +020076
77static int fio_sgio_getevents(struct thread_data *td, int min, int max,
78 struct timespec fio_unused *t)
79{
Jens Axboeadee86c2007-02-08 13:04:36 +010080 /*
81 * normally hard coding &td->files[0] is a bug that needs to be fixed,
82 * but it's ok here as all files should point to the same device.
83 */
Jens Axboe53cdc682006-10-18 11:50:58 +020084 struct fio_file *f = &td->files[0];
Jens Axboe2866c822006-10-09 15:57:48 +020085 struct sgio_data *sd = td->io_ops->data;
Jens Axboedc0deca2007-02-08 20:59:31 +010086 int left = max, ret, events, i, r = 0;
87 void *buf = sd->sgbuf;
Jens Axboe2866c822006-10-09 15:57:48 +020088
89 /*
Jens Axboeadee86c2007-02-08 13:04:36 +010090 * Fill in the file descriptors
Jens Axboe2866c822006-10-09 15:57:48 +020091 */
Jens Axboeadee86c2007-02-08 13:04:36 +010092 for_each_file(td, f, i) {
93 /*
94 * don't block for min events == 0
95 */
96 if (!min) {
Jens Axboedc0deca2007-02-08 20:59:31 +010097 sd->fd_flags[i] = fcntl(f->fd, F_GETFL);
98 fcntl(f->fd, F_SETFL, sd->fd_flags[i] | O_NONBLOCK);
Jens Axboeadee86c2007-02-08 13:04:36 +010099 }
Jens Axboedc0deca2007-02-08 20:59:31 +0100100 sd->pfds[i].fd = f->fd;
101 sd->pfds[i].events = POLLIN;
Jens Axboe2866c822006-10-09 15:57:48 +0200102 }
103
104 while (left) {
Jens Axboeadee86c2007-02-08 13:04:36 +0100105 void *p;
106
Jens Axboe2866c822006-10-09 15:57:48 +0200107 do {
108 if (!min)
109 break;
Jens Axboeadee86c2007-02-08 13:04:36 +0100110
Jens Axboedc0deca2007-02-08 20:59:31 +0100111 ret = poll(sd->pfds, td->nr_files, -1);
Jens Axboeadee86c2007-02-08 13:04:36 +0100112 if (ret < 0) {
113 td_verror(td, errno);
114 if (!r)
115 r = -1;
116 break;
117 } else if (!ret)
118 continue;
119
Jens Axboedc0deca2007-02-08 20:59:31 +0100120 if (pollin_events(sd->pfds, td->nr_files))
Jens Axboe2866c822006-10-09 15:57:48 +0200121 break;
122 } while (1);
123
Jens Axboeadee86c2007-02-08 13:04:36 +0100124 if (r < 0)
Jens Axboe2866c822006-10-09 15:57:48 +0200125 break;
126
Jens Axboeadee86c2007-02-08 13:04:36 +0100127re_read:
128 p = buf;
129 events = 0;
130 for_each_file(td, f, i) {
131 ret = read(f->fd, p, left * sizeof(struct sg_io_hdr));
132 if (ret < 0) {
133 if (errno == EAGAIN)
134 continue;
135 td_verror(td, errno);
136 r = -1;
137 break;
138 } else if (ret) {
139 p += ret;
140 events += ret / sizeof(struct sg_io_hdr);
141 }
142 }
143
144 if (r < 0)
145 break;
146 if (!events) {
147 usleep(1000);
148 goto re_read;
149 }
150
Jens Axboe2866c822006-10-09 15:57:48 +0200151 left -= events;
152 r += events;
153
154 for (i = 0; i < events; i++) {
155 struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i;
156
157 sd->events[i] = hdr->usr_ptr;
158 }
159 }
160
Jens Axboeadee86c2007-02-08 13:04:36 +0100161 if (!min) {
162 for_each_file(td, f, i)
Jens Axboedc0deca2007-02-08 20:59:31 +0100163 fcntl(f->fd, F_SETFL, sd->fd_flags[i]);
Jens Axboeadee86c2007-02-08 13:04:36 +0100164 }
Jens Axboe2866c822006-10-09 15:57:48 +0200165
Jens Axboe2866c822006-10-09 15:57:48 +0200166 return r;
167}
168
Jens Axboe7a16dd02006-10-18 17:21:58 +0200169static int fio_sgio_ioctl_doio(struct thread_data *td,
170 struct fio_file *f, struct io_u *io_u)
Jens Axboe2866c822006-10-09 15:57:48 +0200171{
172 struct sgio_data *sd = td->io_ops->data;
173 struct sg_io_hdr *hdr = &io_u->hdr;
174
175 sd->events[0] = io_u;
176
Jens Axboe53cdc682006-10-18 11:50:58 +0200177 return ioctl(f->fd, SG_IO, hdr);
Jens Axboe2866c822006-10-09 15:57:48 +0200178}
179
Jens Axboe7a16dd02006-10-18 17:21:58 +0200180static int fio_sgio_rw_doio(struct fio_file *f, struct io_u *io_u, int sync)
Jens Axboe2866c822006-10-09 15:57:48 +0200181{
182 struct sg_io_hdr *hdr = &io_u->hdr;
183 int ret;
184
Jens Axboe53cdc682006-10-18 11:50:58 +0200185 ret = write(f->fd, hdr, sizeof(*hdr));
Jens Axboe2866c822006-10-09 15:57:48 +0200186 if (ret < 0)
187 return errno;
188
189 if (sync) {
Jens Axboe53cdc682006-10-18 11:50:58 +0200190 ret = read(f->fd, hdr, sizeof(*hdr));
Jens Axboe2866c822006-10-09 15:57:48 +0200191 if (ret < 0)
192 return errno;
193 }
194
195 return 0;
196}
197
198static int fio_sgio_doio(struct thread_data *td, struct io_u *io_u, int sync)
199{
Jens Axboe53cdc682006-10-18 11:50:58 +0200200 struct fio_file *f = io_u->file;
Jens Axboe2866c822006-10-09 15:57:48 +0200201
Jens Axboe53cdc682006-10-18 11:50:58 +0200202 if (td->filetype == FIO_TYPE_BD)
203 return fio_sgio_ioctl_doio(td, f, io_u);
204
Jens Axboe7a16dd02006-10-18 17:21:58 +0200205 return fio_sgio_rw_doio(f, io_u, sync);
Jens Axboe2866c822006-10-09 15:57:48 +0200206}
207
Jens Axboe2866c822006-10-09 15:57:48 +0200208static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u)
209{
210 struct sg_io_hdr *hdr = &io_u->hdr;
211 struct sgio_data *sd = td->io_ops->data;
212 int nr_blocks, lba;
213
Jens Axboecec6b552007-02-06 20:15:38 +0100214 if (io_u->xfer_buflen & (sd->bs - 1)) {
Jens Axboe2866c822006-10-09 15:57:48 +0200215 log_err("read/write not sector aligned\n");
216 return EINVAL;
217 }
218
Jens Axboe2866c822006-10-09 15:57:48 +0200219 if (io_u->ddir == DDIR_READ) {
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200220 sgio_hdr_init(sd, hdr, io_u, 1);
221
Jens Axboe2866c822006-10-09 15:57:48 +0200222 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
223 hdr->cmdp[0] = 0x28;
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200224 } else if (io_u->ddir == DDIR_WRITE) {
225 sgio_hdr_init(sd, hdr, io_u, 1);
226
Jens Axboe2866c822006-10-09 15:57:48 +0200227 hdr->dxfer_direction = SG_DXFER_TO_DEV;
228 hdr->cmdp[0] = 0x2a;
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200229 } else {
230 sgio_hdr_init(sd, hdr, io_u, 0);
231
232 hdr->dxfer_direction = SG_DXFER_NONE;
233 hdr->cmdp[0] = 0x35;
Jens Axboe2866c822006-10-09 15:57:48 +0200234 }
235
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200236 if (hdr->dxfer_direction != SG_DXFER_NONE) {
Jens Axboecec6b552007-02-06 20:15:38 +0100237 nr_blocks = io_u->xfer_buflen / sd->bs;
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200238 lba = io_u->offset / sd->bs;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100239 hdr->cmdp[2] = (unsigned char) ((lba >> 24) & 0xff);
240 hdr->cmdp[3] = (unsigned char) ((lba >> 16) & 0xff);
241 hdr->cmdp[4] = (unsigned char) ((lba >> 8) & 0xff);
242 hdr->cmdp[5] = (unsigned char) (lba & 0xff);
243 hdr->cmdp[7] = (unsigned char) ((nr_blocks >> 8) & 0xff);
244 hdr->cmdp[8] = (unsigned char) (nr_blocks & 0xff);
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200245 }
246
Jens Axboe2866c822006-10-09 15:57:48 +0200247 return 0;
248}
249
250static int fio_sgio_queue(struct thread_data *td, struct io_u *io_u)
251{
252 struct sg_io_hdr *hdr = &io_u->hdr;
253 int ret;
254
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200255 ret = fio_sgio_doio(td, io_u, io_u->ddir == DDIR_SYNC);
Jens Axboe2866c822006-10-09 15:57:48 +0200256
257 if (ret < 0)
258 io_u->error = errno;
259 else if (hdr->status) {
260 io_u->resid = hdr->resid;
261 io_u->error = EIO;
262 }
263
Jens Axboe95bcd812007-02-11 01:01:57 +0100264 if (io_u->error) {
265 td_verror(td, io_u->error);
266 return io_u->error;
267 }
268
269 return 0;
Jens Axboe2866c822006-10-09 15:57:48 +0200270}
271
272static struct io_u *fio_sgio_event(struct thread_data *td, int event)
273{
274 struct sgio_data *sd = td->io_ops->data;
275
276 return sd->events[event];
277}
278
279static int fio_sgio_get_bs(struct thread_data *td, unsigned int *bs)
280{
281 struct sgio_data *sd = td->io_ops->data;
282 struct io_u *io_u;
283 struct sg_io_hdr *hdr;
284 unsigned char buf[8];
285 int ret;
286
287 io_u = __get_io_u(td);
Jens Axboe6ab9e262007-02-08 20:29:41 +0100288 io_u->file = &td->files[0];
Jens Axboe2866c822006-10-09 15:57:48 +0200289 assert(io_u);
290
291 hdr = &io_u->hdr;
292 sgio_hdr_init(sd, hdr, io_u, 0);
293 memset(buf, 0, sizeof(buf));
294
295 hdr->cmdp[0] = 0x25;
296 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
297 hdr->dxferp = buf;
298 hdr->dxfer_len = sizeof(buf);
299
300 ret = fio_sgio_doio(td, io_u, 1);
301 if (ret) {
302 put_io_u(td, io_u);
303 return ret;
304 }
305
306 *bs = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
307 put_io_u(td, io_u);
308 return 0;
309}
310
311static void fio_sgio_cleanup(struct thread_data *td)
312{
Jens Axboedc0deca2007-02-08 20:59:31 +0100313 struct sgio_data *sd = td->io_ops->data;
314
315 if (sd) {
316 free(sd->events);
317 free(sd->cmds);
318 free(sd->fd_flags);
319 free(sd->pfds);
320 free(sd->sgbuf);
321 free(sd);
322
Jens Axboe2866c822006-10-09 15:57:48 +0200323 td->io_ops->data = NULL;
324 }
325}
326
327static int fio_sgio_init(struct thread_data *td)
328{
Jens Axboe53cdc682006-10-18 11:50:58 +0200329 struct fio_file *f = &td->files[0];
Jens Axboe2866c822006-10-09 15:57:48 +0200330 struct sgio_data *sd;
331 unsigned int bs;
332 int ret;
333
334 sd = malloc(sizeof(*sd));
Jens Axboecb781c72006-11-07 14:02:48 +0100335 memset(sd, 0, sizeof(*sd));
Jens Axboe2866c822006-10-09 15:57:48 +0200336 sd->cmds = malloc(td->iodepth * sizeof(struct sgio_cmd));
Jens Axboecb781c72006-11-07 14:02:48 +0100337 memset(sd->cmds, 0, td->iodepth * sizeof(struct sgio_cmd));
Jens Axboe2866c822006-10-09 15:57:48 +0200338 sd->events = malloc(td->iodepth * sizeof(struct io_u *));
Jens Axboecb781c72006-11-07 14:02:48 +0100339 memset(sd->events, 0, td->iodepth * sizeof(struct io_u *));
Jens Axboedc0deca2007-02-08 20:59:31 +0100340 sd->pfds = malloc(sizeof(struct pollfd) * td->nr_files);
341 memset(sd->pfds, 0, sizeof(struct pollfd) * td->nr_files);
342 sd->fd_flags = malloc(sizeof(int) * td->nr_files);
343 memset(sd->fd_flags, 0, sizeof(int) * td->nr_files);
344 sd->sgbuf = malloc(sizeof(struct sg_io_hdr) * td->iodepth);
345 memset(sd->sgbuf, 0, sizeof(struct sg_io_hdr) * td->iodepth);
346
Jens Axboe2866c822006-10-09 15:57:48 +0200347 td->io_ops->data = sd;
348
349 if (td->filetype == FIO_TYPE_BD) {
Jens Axboe53cdc682006-10-18 11:50:58 +0200350 if (ioctl(f->fd, BLKSSZGET, &bs) < 0) {
Jens Axboe2866c822006-10-09 15:57:48 +0200351 td_verror(td, errno);
Jens Axboecb781c72006-11-07 14:02:48 +0100352 goto err;
Jens Axboe2866c822006-10-09 15:57:48 +0200353 }
354 } else if (td->filetype == FIO_TYPE_CHAR) {
355 int version;
356
Jens Axboe53cdc682006-10-18 11:50:58 +0200357 if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) {
Jens Axboe2866c822006-10-09 15:57:48 +0200358 td_verror(td, errno);
Jens Axboecb781c72006-11-07 14:02:48 +0100359 goto err;
Jens Axboe2866c822006-10-09 15:57:48 +0200360 }
361
362 ret = fio_sgio_get_bs(td, &bs);
363 if (ret)
Jens Axboecb781c72006-11-07 14:02:48 +0100364 goto err;
Jens Axboe2866c822006-10-09 15:57:48 +0200365 } else {
366 log_err("ioengine sgio only works on block devices\n");
Jens Axboecb781c72006-11-07 14:02:48 +0100367 goto err;
Jens Axboe2866c822006-10-09 15:57:48 +0200368 }
369
370 sd->bs = bs;
371
372 if (td->filetype == FIO_TYPE_BD)
373 td->io_ops->getevents = fio_sgio_ioctl_getevents;
374 else
375 td->io_ops->getevents = fio_sgio_getevents;
376
377 /*
378 * we want to do it, regardless of whether odirect is set or not
379 */
380 td->override_sync = 1;
381 return 0;
Jens Axboecb781c72006-11-07 14:02:48 +0100382err:
383 free(sd->events);
384 free(sd->cmds);
Jens Axboedc0deca2007-02-08 20:59:31 +0100385 free(sd->fd_flags);
386 free(sd->pfds);
387 free(sd->sgbuf);
Jens Axboecb781c72006-11-07 14:02:48 +0100388 free(sd);
Jens Axboee8b8c9f2007-02-05 10:47:48 +0100389 td->io_ops->data = NULL;
Jens Axboecb781c72006-11-07 14:02:48 +0100390 return 1;
Jens Axboe2866c822006-10-09 15:57:48 +0200391}
392
Jens Axboe5f350952006-11-07 15:20:59 +0100393static struct ioengine_ops ioengine = {
Jens Axboe2866c822006-10-09 15:57:48 +0200394 .name = "sg",
395 .version = FIO_IOOPS_VERSION,
396 .init = fio_sgio_init,
397 .prep = fio_sgio_prep,
398 .queue = fio_sgio_queue,
399 .getevents = fio_sgio_getevents,
400 .event = fio_sgio_event,
401 .cleanup = fio_sgio_cleanup,
Jens Axboeb2a15192006-10-18 14:10:42 +0200402 .flags = FIO_SYNCIO | FIO_RAWIO,
Jens Axboe2866c822006-10-09 15:57:48 +0200403};
Jens Axboe34cfcda2006-11-03 14:00:45 +0100404
405#else /* FIO_HAVE_SGIO */
406
407/*
408 * When we have a proper configure system in place, we simply wont build
409 * and install this io engine. For now install a crippled version that
410 * just complains and fails to load.
411 */
412static int fio_sgio_init(struct thread_data fio_unused *td)
413{
414 fprintf(stderr, "fio: sgio not available\n");
415 return 1;
416}
417
Jens Axboe5f350952006-11-07 15:20:59 +0100418static struct ioengine_ops ioengine = {
Jens Axboe34cfcda2006-11-03 14:00:45 +0100419 .name = "sgio",
420 .version = FIO_IOOPS_VERSION,
421 .init = fio_sgio_init,
422};
423
424#endif
Jens Axboe5f350952006-11-07 15:20:59 +0100425
426static void fio_init fio_sgio_register(void)
427{
428 register_ioengine(&ioengine);
429}
430
431static void fio_exit fio_sgio_unregister(void)
432{
433 unregister_ioengine(&ioengine);
434}