blob: 39f99d8d0922050eb1a3aa3d60305cd865a09113 [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 */
80 if (!min) {
Jens Axboedc0deca2007-02-08 20:59:31 +010081 sd->fd_flags[i] = fcntl(f->fd, F_GETFL);
82 fcntl(f->fd, F_SETFL, sd->fd_flags[i] | O_NONBLOCK);
Jens Axboeadee86c2007-02-08 13:04:36 +010083 }
Jens Axboedc0deca2007-02-08 20:59:31 +010084 sd->pfds[i].fd = f->fd;
85 sd->pfds[i].events = POLLIN;
Jens Axboe2866c822006-10-09 15:57:48 +020086 }
87
88 while (left) {
Jens Axboeadee86c2007-02-08 13:04:36 +010089 void *p;
90
Jens Axboe2866c822006-10-09 15:57:48 +020091 do {
92 if (!min)
93 break;
Jens Axboeadee86c2007-02-08 13:04:36 +010094
Jens Axboe2dc1bbe2007-03-15 15:01:33 +010095 ret = poll(sd->pfds, td->o.nr_files, -1);
Jens Axboeadee86c2007-02-08 13:04:36 +010096 if (ret < 0) {
Jens Axboeadee86c2007-02-08 13:04:36 +010097 if (!r)
Jens Axboe22819ec2007-02-18 07:47:14 +010098 r = -errno;
Jens Axboee1161c32007-02-22 19:36:48 +010099 td_verror(td, errno, "poll");
Jens Axboeadee86c2007-02-08 13:04:36 +0100100 break;
101 } else if (!ret)
102 continue;
103
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100104 if (pollin_events(sd->pfds, td->o.nr_files))
Jens Axboe2866c822006-10-09 15:57:48 +0200105 break;
106 } while (1);
107
Jens Axboeadee86c2007-02-08 13:04:36 +0100108 if (r < 0)
Jens Axboe2866c822006-10-09 15:57:48 +0200109 break;
110
Jens Axboeadee86c2007-02-08 13:04:36 +0100111re_read:
112 p = buf;
113 events = 0;
114 for_each_file(td, f, i) {
115 ret = read(f->fd, p, left * sizeof(struct sg_io_hdr));
116 if (ret < 0) {
117 if (errno == EAGAIN)
118 continue;
Jens Axboe22819ec2007-02-18 07:47:14 +0100119 r = -errno;
Jens Axboee1161c32007-02-22 19:36:48 +0100120 td_verror(td, errno, "read");
Jens Axboeadee86c2007-02-08 13:04:36 +0100121 break;
122 } else if (ret) {
123 p += ret;
124 events += ret / sizeof(struct sg_io_hdr);
125 }
126 }
127
128 if (r < 0)
129 break;
130 if (!events) {
131 usleep(1000);
132 goto re_read;
133 }
134
Jens Axboe2866c822006-10-09 15:57:48 +0200135 left -= events;
136 r += events;
137
138 for (i = 0; i < events; i++) {
139 struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i;
140
141 sd->events[i] = hdr->usr_ptr;
142 }
143 }
144
Jens Axboeadee86c2007-02-08 13:04:36 +0100145 if (!min) {
146 for_each_file(td, f, i)
Jens Axboedc0deca2007-02-08 20:59:31 +0100147 fcntl(f->fd, F_SETFL, sd->fd_flags[i]);
Jens Axboeadee86c2007-02-08 13:04:36 +0100148 }
Jens Axboe2866c822006-10-09 15:57:48 +0200149
Jens Axboe2866c822006-10-09 15:57:48 +0200150 return r;
151}
152
Jens Axboe7a16dd02006-10-18 17:21:58 +0200153static int fio_sgio_ioctl_doio(struct thread_data *td,
154 struct fio_file *f, struct io_u *io_u)
Jens Axboe2866c822006-10-09 15:57:48 +0200155{
156 struct sgio_data *sd = td->io_ops->data;
157 struct sg_io_hdr *hdr = &io_u->hdr;
Jens Axboe36167d82007-02-18 05:41:31 +0100158 int ret;
Jens Axboe2866c822006-10-09 15:57:48 +0200159
160 sd->events[0] = io_u;
161
Jens Axboe36167d82007-02-18 05:41:31 +0100162 ret = ioctl(f->fd, SG_IO, hdr);
163 if (ret < 0)
Jens Axboea05bd422007-04-02 10:42:33 +0200164 return ret;
Jens Axboe36167d82007-02-18 05:41:31 +0100165
166 return FIO_Q_COMPLETED;
Jens Axboe2866c822006-10-09 15:57:48 +0200167}
168
Jens Axboe7a16dd02006-10-18 17:21:58 +0200169static int fio_sgio_rw_doio(struct fio_file *f, struct io_u *io_u, int sync)
Jens Axboe2866c822006-10-09 15:57:48 +0200170{
171 struct sg_io_hdr *hdr = &io_u->hdr;
172 int ret;
173
Jens Axboe53cdc682006-10-18 11:50:58 +0200174 ret = write(f->fd, hdr, sizeof(*hdr));
Jens Axboe2866c822006-10-09 15:57:48 +0200175 if (ret < 0)
Jens Axboea05bd422007-04-02 10:42:33 +0200176 return ret;
Jens Axboe2866c822006-10-09 15:57:48 +0200177
178 if (sync) {
Jens Axboe53cdc682006-10-18 11:50:58 +0200179 ret = read(f->fd, hdr, sizeof(*hdr));
Jens Axboe2866c822006-10-09 15:57:48 +0200180 if (ret < 0)
Jens Axboea05bd422007-04-02 10:42:33 +0200181 return ret;
Jens Axboe36167d82007-02-18 05:41:31 +0100182 return FIO_Q_COMPLETED;
Jens Axboe2866c822006-10-09 15:57:48 +0200183 }
184
Jens Axboe36167d82007-02-18 05:41:31 +0100185 return FIO_Q_QUEUED;
Jens Axboe2866c822006-10-09 15:57:48 +0200186}
187
188static int fio_sgio_doio(struct thread_data *td, struct io_u *io_u, int sync)
189{
Jens Axboe53cdc682006-10-18 11:50:58 +0200190 struct fio_file *f = io_u->file;
Jens Axboe2866c822006-10-09 15:57:48 +0200191
Jens Axboeaf52b342007-03-13 10:07:47 +0100192 if (f->filetype == FIO_TYPE_BD)
Jens Axboe53cdc682006-10-18 11:50:58 +0200193 return fio_sgio_ioctl_doio(td, f, io_u);
194
Jens Axboe7a16dd02006-10-18 17:21:58 +0200195 return fio_sgio_rw_doio(f, io_u, sync);
Jens Axboe2866c822006-10-09 15:57:48 +0200196}
197
Jens Axboe2866c822006-10-09 15:57:48 +0200198static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u)
199{
200 struct sg_io_hdr *hdr = &io_u->hdr;
201 struct sgio_data *sd = td->io_ops->data;
202 int nr_blocks, lba;
203
Jens Axboecec6b552007-02-06 20:15:38 +0100204 if (io_u->xfer_buflen & (sd->bs - 1)) {
Jens Axboe2866c822006-10-09 15:57:48 +0200205 log_err("read/write not sector aligned\n");
206 return EINVAL;
207 }
208
Jens Axboe2866c822006-10-09 15:57:48 +0200209 if (io_u->ddir == DDIR_READ) {
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200210 sgio_hdr_init(sd, hdr, io_u, 1);
211
Jens Axboe2866c822006-10-09 15:57:48 +0200212 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
213 hdr->cmdp[0] = 0x28;
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200214 } else if (io_u->ddir == DDIR_WRITE) {
215 sgio_hdr_init(sd, hdr, io_u, 1);
216
Jens Axboe2866c822006-10-09 15:57:48 +0200217 hdr->dxfer_direction = SG_DXFER_TO_DEV;
218 hdr->cmdp[0] = 0x2a;
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200219 } else {
220 sgio_hdr_init(sd, hdr, io_u, 0);
221
222 hdr->dxfer_direction = SG_DXFER_NONE;
223 hdr->cmdp[0] = 0x35;
Jens Axboe2866c822006-10-09 15:57:48 +0200224 }
225
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200226 if (hdr->dxfer_direction != SG_DXFER_NONE) {
Jens Axboecec6b552007-02-06 20:15:38 +0100227 nr_blocks = io_u->xfer_buflen / sd->bs;
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200228 lba = io_u->offset / sd->bs;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100229 hdr->cmdp[2] = (unsigned char) ((lba >> 24) & 0xff);
230 hdr->cmdp[3] = (unsigned char) ((lba >> 16) & 0xff);
231 hdr->cmdp[4] = (unsigned char) ((lba >> 8) & 0xff);
232 hdr->cmdp[5] = (unsigned char) (lba & 0xff);
233 hdr->cmdp[7] = (unsigned char) ((nr_blocks >> 8) & 0xff);
234 hdr->cmdp[8] = (unsigned char) (nr_blocks & 0xff);
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200235 }
236
Jens Axboe2866c822006-10-09 15:57:48 +0200237 return 0;
238}
239
240static int fio_sgio_queue(struct thread_data *td, struct io_u *io_u)
241{
242 struct sg_io_hdr *hdr = &io_u->hdr;
243 int ret;
244
Jens Axboe7101d9c2007-09-12 13:12:39 +0200245 fio_ro_check(td, io_u);
246
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200247 ret = fio_sgio_doio(td, io_u, io_u->ddir == DDIR_SYNC);
Jens Axboe2866c822006-10-09 15:57:48 +0200248
249 if (ret < 0)
250 io_u->error = errno;
251 else if (hdr->status) {
252 io_u->resid = hdr->resid;
253 io_u->error = EIO;
254 }
255
Jens Axboe95bcd812007-02-11 01:01:57 +0100256 if (io_u->error) {
Jens Axboee1161c32007-02-22 19:36:48 +0100257 td_verror(td, io_u->error, "xfer");
Jens Axboe36167d82007-02-18 05:41:31 +0100258 return FIO_Q_COMPLETED;
Jens Axboe95bcd812007-02-11 01:01:57 +0100259 }
260
Jens Axboe36167d82007-02-18 05:41:31 +0100261 return ret;
Jens Axboe2866c822006-10-09 15:57:48 +0200262}
263
264static struct io_u *fio_sgio_event(struct thread_data *td, int event)
265{
266 struct sgio_data *sd = td->io_ops->data;
267
268 return sd->events[event];
269}
270
271static int fio_sgio_get_bs(struct thread_data *td, unsigned int *bs)
272{
273 struct sgio_data *sd = td->io_ops->data;
274 struct io_u *io_u;
275 struct sg_io_hdr *hdr;
276 unsigned char buf[8];
277 int ret;
278
279 io_u = __get_io_u(td);
Jens Axboe126d65c2008-03-01 18:04:31 +0100280 io_u->file = td->files[0];
Jens Axboe2866c822006-10-09 15:57:48 +0200281 assert(io_u);
282
283 hdr = &io_u->hdr;
284 sgio_hdr_init(sd, hdr, io_u, 0);
285 memset(buf, 0, sizeof(buf));
286
287 hdr->cmdp[0] = 0x25;
288 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
289 hdr->dxferp = buf;
290 hdr->dxfer_len = sizeof(buf);
291
292 ret = fio_sgio_doio(td, io_u, 1);
293 if (ret) {
294 put_io_u(td, io_u);
295 return ret;
296 }
297
298 *bs = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
299 put_io_u(td, io_u);
300 return 0;
301}
302
303static void fio_sgio_cleanup(struct thread_data *td)
304{
Jens Axboedc0deca2007-02-08 20:59:31 +0100305 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);
Jens Axboe2866c822006-10-09 15:57:48 +0200314 }
315}
316
317static int fio_sgio_init(struct thread_data *td)
318{
319 struct sgio_data *sd;
Jens Axboe2866c822006-10-09 15:57:48 +0200320
321 sd = malloc(sizeof(*sd));
Jens Axboecb781c72006-11-07 14:02:48 +0100322 memset(sd, 0, sizeof(*sd));
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100323 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);
Jens Axboedc0deca2007-02-08 20:59:31 +0100333
Jens Axboe2866c822006-10-09 15:57:48 +0200334 td->io_ops->data = sd;
335
Jens Axboeb5af8292007-03-08 12:43:13 +0100336 /*
337 * we want to do it, regardless of whether odirect is set or not
338 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100339 td->o.override_sync = 1;
Jens Axboeb5af8292007-03-08 12:43:13 +0100340 return 0;
341}
342
343static 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
Jens Axboeaf52b342007-03-13 10:07:47 +0100348 if (f->filetype == FIO_TYPE_BD) {
Jens Axboe53cdc682006-10-18 11:50:58 +0200349 if (ioctl(f->fd, BLKSSZGET, &bs) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100350 td_verror(td, errno, "ioctl");
Jens Axboeb5af8292007-03-08 12:43:13 +0100351 return 1;
Jens Axboe2866c822006-10-09 15:57:48 +0200352 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100353 } else if (f->filetype == FIO_TYPE_CHAR) {
Jens Axboeb5af8292007-03-08 12:43:13 +0100354 int version, ret;
Jens Axboe2866c822006-10-09 15:57:48 +0200355
Jens Axboe53cdc682006-10-18 11:50:58 +0200356 if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) {
Jens Axboee1161c32007-02-22 19:36:48 +0100357 td_verror(td, errno, "ioctl");
Jens Axboeb5af8292007-03-08 12:43:13 +0100358 return 1;
Jens Axboe2866c822006-10-09 15:57:48 +0200359 }
360
361 ret = fio_sgio_get_bs(td, &bs);
362 if (ret)
Jens Axboeb5af8292007-03-08 12:43:13 +0100363 return 1;
Jens Axboe2866c822006-10-09 15:57:48 +0200364 } else {
Gurudas Paid0c70932008-05-30 13:35:00 +0200365 log_err("ioengine sg only works on block devices\n");
Jens Axboeb5af8292007-03-08 12:43:13 +0100366 return 1;
Jens Axboe2866c822006-10-09 15:57:48 +0200367 }
368
369 sd->bs = bs;
370
Jens Axboeaf52b342007-03-13 10:07:47 +0100371 if (f->filetype == FIO_TYPE_BD) {
Jens Axboe36167d82007-02-18 05:41:31 +0100372 td->io_ops->getevents = NULL;
373 td->io_ops->event = NULL;
374 }
Jens Axboe2866c822006-10-09 15:57:48 +0200375
Jens Axboe2866c822006-10-09 15:57:48 +0200376 return 0;
Jens Axboeb5af8292007-03-08 12:43:13 +0100377}
378
379static 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
Jens Axboe15ba6402007-04-02 10:51:10 +0200388 if (sd && !sd->type_checked && fio_sgio_type_check(td, f)) {
Jens Axboe6977bcd2008-03-01 15:55:36 +0100389 ret = generic_close_file(td, f);
Jens Axboeb5af8292007-03-08 12:43:13 +0100390 return 1;
391 }
392
393 return 0;
Jens Axboe2866c822006-10-09 15:57:48 +0200394}
395
Jens Axboe5f350952006-11-07 15:20:59 +0100396static struct ioengine_ops ioengine = {
Jens Axboe2866c822006-10-09 15:57:48 +0200397 .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,
Jens Axboeb5af8292007-03-08 12:43:13 +0100405 .open_file = fio_sgio_open,
406 .close_file = generic_close_file,
Jens Axboedf9c26b2009-03-05 10:13:58 +0100407 .get_file_size = generic_get_file_size,
Jens Axboeb2a15192006-10-18 14:10:42 +0200408 .flags = FIO_SYNCIO | FIO_RAWIO,
Jens Axboe2866c822006-10-09 15:57:48 +0200409};
Jens Axboe34cfcda2006-11-03 14:00:45 +0100410
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 */
418static int fio_sgio_init(struct thread_data fio_unused *td)
419{
Gurudas Paid0c70932008-05-30 13:35:00 +0200420 fprintf(stderr, "fio: ioengine sg not available\n");
Jens Axboe34cfcda2006-11-03 14:00:45 +0100421 return 1;
422}
423
Jens Axboe5f350952006-11-07 15:20:59 +0100424static struct ioengine_ops ioengine = {
Gurudas Paid0c70932008-05-30 13:35:00 +0200425 .name = "sg",
Jens Axboe34cfcda2006-11-03 14:00:45 +0100426 .version = FIO_IOOPS_VERSION,
427 .init = fio_sgio_init,
428};
429
430#endif
Jens Axboe5f350952006-11-07 15:20:59 +0100431
432static void fio_init fio_sgio_register(void)
433{
434 register_ioengine(&ioengine);
435}
436
437static void fio_exit fio_sgio_unregister(void)
438{
439 unregister_ioengine(&ioengine);
440}