blob: 877264acd15b3316f4abd6b2c5002f09177f83df [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;
25 unsigned int bs;
26};
27
28static void sgio_hdr_init(struct sgio_data *sd, struct sg_io_hdr *hdr,
29 struct io_u *io_u, int fs)
30{
31 struct sgio_cmd *sc = &sd->cmds[io_u->index];
32
33 memset(hdr, 0, sizeof(*hdr));
34 memset(sc->cdb, 0, sizeof(sc->cdb));
35
36 hdr->interface_id = 'S';
37 hdr->cmdp = sc->cdb;
38 hdr->cmd_len = sizeof(sc->cdb);
39 hdr->pack_id = io_u->index;
40 hdr->usr_ptr = io_u;
41
42 if (fs) {
Jens Axboecec6b552007-02-06 20:15:38 +010043 hdr->dxferp = io_u->xfer_buf;
44 hdr->dxfer_len = io_u->xfer_buflen;
Jens Axboe2866c822006-10-09 15:57:48 +020045 }
46}
47
48static int fio_sgio_ioctl_getevents(struct thread_data *td, int fio_unused min,
49 int max, struct timespec fio_unused *t)
50{
51 assert(max <= 1);
52
53 /*
54 * we can only have one finished io_u for sync io, since the depth
55 * is always 1
56 */
57 if (list_empty(&td->io_u_busylist))
58 return 0;
59
60 return 1;
61}
62
Jens Axboeadee86c2007-02-08 13:04:36 +010063static int pollin_events(struct pollfd *pfds, int fds)
64{
65 int i;
66
67 for (i = 0; i < fds; i++)
68 if (pfds[i].revents & POLLIN)
69 return 1;
70
71 return 0;
72}
Jens Axboe2866c822006-10-09 15:57:48 +020073
74static int fio_sgio_getevents(struct thread_data *td, int min, int max,
75 struct timespec fio_unused *t)
76{
Jens Axboeadee86c2007-02-08 13:04:36 +010077 /*
78 * normally hard coding &td->files[0] is a bug that needs to be fixed,
79 * but it's ok here as all files should point to the same device.
80 */
Jens Axboe53cdc682006-10-18 11:50:58 +020081 struct fio_file *f = &td->files[0];
Jens Axboe2866c822006-10-09 15:57:48 +020082 struct sgio_data *sd = td->io_ops->data;
Jens Axboeadee86c2007-02-08 13:04:36 +010083 int left = max, ret, events, i, r = 0, *fl;
84 struct pollfd *pfds;
85 void *buf;
Jens Axboe2866c822006-10-09 15:57:48 +020086
87 /*
Jens Axboeadee86c2007-02-08 13:04:36 +010088 * Fill in the file descriptors
Jens Axboe2866c822006-10-09 15:57:48 +020089 */
Jens Axboeadee86c2007-02-08 13:04:36 +010090 pfds = malloc(sizeof(struct pollfd) * td->nr_files);
91 fl = malloc(sizeof(int) * td->nr_files);
92
93 for_each_file(td, f, i) {
94 /*
95 * don't block for min events == 0
96 */
97 if (!min) {
98 fl[i] = fcntl(f->fd, F_GETFL);
99 fcntl(f->fd, F_SETFL, fl[i] | O_NONBLOCK);
100 }
101 pfds[i].fd = f->fd;
102 pfds[i].events = POLLIN;
Jens Axboe2866c822006-10-09 15:57:48 +0200103 }
104
Jens Axboeadee86c2007-02-08 13:04:36 +0100105 buf = malloc(max * sizeof(struct sg_io_hdr));
Jens Axboe2866c822006-10-09 15:57:48 +0200106 while (left) {
Jens Axboeadee86c2007-02-08 13:04:36 +0100107 void *p;
108
Jens Axboe2866c822006-10-09 15:57:48 +0200109 do {
110 if (!min)
111 break;
Jens Axboeadee86c2007-02-08 13:04:36 +0100112
113 ret = poll(pfds, td->nr_files, -1);
114 if (ret < 0) {
115 td_verror(td, errno);
116 if (!r)
117 r = -1;
118 break;
119 } else if (!ret)
120 continue;
121
122 if (pollin_events(pfds, td->nr_files))
Jens Axboe2866c822006-10-09 15:57:48 +0200123 break;
124 } while (1);
125
Jens Axboeadee86c2007-02-08 13:04:36 +0100126 if (r < 0)
Jens Axboe2866c822006-10-09 15:57:48 +0200127 break;
128
Jens Axboeadee86c2007-02-08 13:04:36 +0100129re_read:
130 p = buf;
131 events = 0;
132 for_each_file(td, f, i) {
133 ret = read(f->fd, p, left * sizeof(struct sg_io_hdr));
134 if (ret < 0) {
135 if (errno == EAGAIN)
136 continue;
137 td_verror(td, errno);
138 r = -1;
139 break;
140 } else if (ret) {
141 p += ret;
142 events += ret / sizeof(struct sg_io_hdr);
143 }
144 }
145
146 if (r < 0)
147 break;
148 if (!events) {
149 usleep(1000);
150 goto re_read;
151 }
152
Jens Axboe2866c822006-10-09 15:57:48 +0200153 left -= events;
154 r += events;
155
156 for (i = 0; i < events; i++) {
157 struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i;
158
159 sd->events[i] = hdr->usr_ptr;
160 }
161 }
162
Jens Axboeadee86c2007-02-08 13:04:36 +0100163 if (!min) {
164 for_each_file(td, f, i)
165 fcntl(f->fd, F_SETFL, fl[i]);
166 }
Jens Axboe2866c822006-10-09 15:57:48 +0200167
168 free(buf);
Jens Axboeadee86c2007-02-08 13:04:36 +0100169 free(pfds);
170 free(fl);
Jens Axboe2866c822006-10-09 15:57:48 +0200171 return r;
172}
173
Jens Axboe7a16dd02006-10-18 17:21:58 +0200174static int fio_sgio_ioctl_doio(struct thread_data *td,
175 struct fio_file *f, struct io_u *io_u)
Jens Axboe2866c822006-10-09 15:57:48 +0200176{
177 struct sgio_data *sd = td->io_ops->data;
178 struct sg_io_hdr *hdr = &io_u->hdr;
179
180 sd->events[0] = io_u;
181
Jens Axboe53cdc682006-10-18 11:50:58 +0200182 return ioctl(f->fd, SG_IO, hdr);
Jens Axboe2866c822006-10-09 15:57:48 +0200183}
184
Jens Axboe7a16dd02006-10-18 17:21:58 +0200185static int fio_sgio_rw_doio(struct fio_file *f, struct io_u *io_u, int sync)
Jens Axboe2866c822006-10-09 15:57:48 +0200186{
187 struct sg_io_hdr *hdr = &io_u->hdr;
188 int ret;
189
Jens Axboe53cdc682006-10-18 11:50:58 +0200190 ret = write(f->fd, hdr, sizeof(*hdr));
Jens Axboe2866c822006-10-09 15:57:48 +0200191 if (ret < 0)
192 return errno;
193
194 if (sync) {
Jens Axboe53cdc682006-10-18 11:50:58 +0200195 ret = read(f->fd, hdr, sizeof(*hdr));
Jens Axboe2866c822006-10-09 15:57:48 +0200196 if (ret < 0)
197 return errno;
198 }
199
200 return 0;
201}
202
203static int fio_sgio_doio(struct thread_data *td, struct io_u *io_u, int sync)
204{
Jens Axboe53cdc682006-10-18 11:50:58 +0200205 struct fio_file *f = io_u->file;
Jens Axboe2866c822006-10-09 15:57:48 +0200206
Jens Axboe53cdc682006-10-18 11:50:58 +0200207 if (td->filetype == FIO_TYPE_BD)
208 return fio_sgio_ioctl_doio(td, f, io_u);
209
Jens Axboe7a16dd02006-10-18 17:21:58 +0200210 return fio_sgio_rw_doio(f, io_u, sync);
Jens Axboe2866c822006-10-09 15:57:48 +0200211}
212
Jens Axboe2866c822006-10-09 15:57:48 +0200213static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u)
214{
215 struct sg_io_hdr *hdr = &io_u->hdr;
216 struct sgio_data *sd = td->io_ops->data;
217 int nr_blocks, lba;
218
Jens Axboecec6b552007-02-06 20:15:38 +0100219 if (io_u->xfer_buflen & (sd->bs - 1)) {
Jens Axboe2866c822006-10-09 15:57:48 +0200220 log_err("read/write not sector aligned\n");
221 return EINVAL;
222 }
223
Jens Axboe2866c822006-10-09 15:57:48 +0200224 if (io_u->ddir == DDIR_READ) {
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200225 sgio_hdr_init(sd, hdr, io_u, 1);
226
Jens Axboe2866c822006-10-09 15:57:48 +0200227 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
228 hdr->cmdp[0] = 0x28;
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200229 } else if (io_u->ddir == DDIR_WRITE) {
230 sgio_hdr_init(sd, hdr, io_u, 1);
231
Jens Axboe2866c822006-10-09 15:57:48 +0200232 hdr->dxfer_direction = SG_DXFER_TO_DEV;
233 hdr->cmdp[0] = 0x2a;
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200234 } else {
235 sgio_hdr_init(sd, hdr, io_u, 0);
236
237 hdr->dxfer_direction = SG_DXFER_NONE;
238 hdr->cmdp[0] = 0x35;
Jens Axboe2866c822006-10-09 15:57:48 +0200239 }
240
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200241 if (hdr->dxfer_direction != SG_DXFER_NONE) {
Jens Axboecec6b552007-02-06 20:15:38 +0100242 nr_blocks = io_u->xfer_buflen / sd->bs;
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200243 lba = io_u->offset / sd->bs;
Jens Axboe1e97cce2006-12-05 11:44:16 +0100244 hdr->cmdp[2] = (unsigned char) ((lba >> 24) & 0xff);
245 hdr->cmdp[3] = (unsigned char) ((lba >> 16) & 0xff);
246 hdr->cmdp[4] = (unsigned char) ((lba >> 8) & 0xff);
247 hdr->cmdp[5] = (unsigned char) (lba & 0xff);
248 hdr->cmdp[7] = (unsigned char) ((nr_blocks >> 8) & 0xff);
249 hdr->cmdp[8] = (unsigned char) (nr_blocks & 0xff);
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200250 }
251
Jens Axboe2866c822006-10-09 15:57:48 +0200252 return 0;
253}
254
255static int fio_sgio_queue(struct thread_data *td, struct io_u *io_u)
256{
257 struct sg_io_hdr *hdr = &io_u->hdr;
258 int ret;
259
Jens Axboe87dc1ab2006-10-24 14:41:26 +0200260 ret = fio_sgio_doio(td, io_u, io_u->ddir == DDIR_SYNC);
Jens Axboe2866c822006-10-09 15:57:48 +0200261
262 if (ret < 0)
263 io_u->error = errno;
264 else if (hdr->status) {
265 io_u->resid = hdr->resid;
266 io_u->error = EIO;
267 }
268
269 return io_u->error;
270}
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);
288 assert(io_u);
289
290 hdr = &io_u->hdr;
291 sgio_hdr_init(sd, hdr, io_u, 0);
292 memset(buf, 0, sizeof(buf));
293
294 hdr->cmdp[0] = 0x25;
295 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
296 hdr->dxferp = buf;
297 hdr->dxfer_len = sizeof(buf);
298
299 ret = fio_sgio_doio(td, io_u, 1);
300 if (ret) {
301 put_io_u(td, io_u);
302 return ret;
303 }
304
305 *bs = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
306 put_io_u(td, io_u);
307 return 0;
308}
309
310static void fio_sgio_cleanup(struct thread_data *td)
311{
312 if (td->io_ops->data) {
313 free(td->io_ops->data);
314 td->io_ops->data = NULL;
315 }
316}
317
318static int fio_sgio_init(struct thread_data *td)
319{
Jens Axboe53cdc682006-10-18 11:50:58 +0200320 struct fio_file *f = &td->files[0];
Jens Axboe2866c822006-10-09 15:57:48 +0200321 struct sgio_data *sd;
322 unsigned int bs;
323 int ret;
324
325 sd = malloc(sizeof(*sd));
Jens Axboecb781c72006-11-07 14:02:48 +0100326 memset(sd, 0, sizeof(*sd));
Jens Axboe2866c822006-10-09 15:57:48 +0200327 sd->cmds = malloc(td->iodepth * sizeof(struct sgio_cmd));
Jens Axboecb781c72006-11-07 14:02:48 +0100328 memset(sd->cmds, 0, td->iodepth * sizeof(struct sgio_cmd));
Jens Axboe2866c822006-10-09 15:57:48 +0200329 sd->events = malloc(td->iodepth * sizeof(struct io_u *));
Jens Axboecb781c72006-11-07 14:02:48 +0100330 memset(sd->events, 0, td->iodepth * sizeof(struct io_u *));
Jens Axboe2866c822006-10-09 15:57:48 +0200331 td->io_ops->data = sd;
332
333 if (td->filetype == FIO_TYPE_BD) {
Jens Axboe53cdc682006-10-18 11:50:58 +0200334 if (ioctl(f->fd, BLKSSZGET, &bs) < 0) {
Jens Axboe2866c822006-10-09 15:57:48 +0200335 td_verror(td, errno);
Jens Axboecb781c72006-11-07 14:02:48 +0100336 goto err;
Jens Axboe2866c822006-10-09 15:57:48 +0200337 }
338 } else if (td->filetype == FIO_TYPE_CHAR) {
339 int version;
340
Jens Axboe53cdc682006-10-18 11:50:58 +0200341 if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) {
Jens Axboe2866c822006-10-09 15:57:48 +0200342 td_verror(td, errno);
Jens Axboecb781c72006-11-07 14:02:48 +0100343 goto err;
Jens Axboe2866c822006-10-09 15:57:48 +0200344 }
345
346 ret = fio_sgio_get_bs(td, &bs);
347 if (ret)
Jens Axboecb781c72006-11-07 14:02:48 +0100348 goto err;
Jens Axboe2866c822006-10-09 15:57:48 +0200349 } else {
350 log_err("ioengine sgio only works on block devices\n");
Jens Axboecb781c72006-11-07 14:02:48 +0100351 goto err;
Jens Axboe2866c822006-10-09 15:57:48 +0200352 }
353
354 sd->bs = bs;
355
356 if (td->filetype == FIO_TYPE_BD)
357 td->io_ops->getevents = fio_sgio_ioctl_getevents;
358 else
359 td->io_ops->getevents = fio_sgio_getevents;
360
361 /*
362 * we want to do it, regardless of whether odirect is set or not
363 */
364 td->override_sync = 1;
365 return 0;
Jens Axboecb781c72006-11-07 14:02:48 +0100366err:
367 free(sd->events);
368 free(sd->cmds);
369 free(sd);
Jens Axboee8b8c9f2007-02-05 10:47:48 +0100370 td->io_ops->data = NULL;
Jens Axboecb781c72006-11-07 14:02:48 +0100371 return 1;
Jens Axboe2866c822006-10-09 15:57:48 +0200372}
373
Jens Axboe5f350952006-11-07 15:20:59 +0100374static struct ioengine_ops ioengine = {
Jens Axboe2866c822006-10-09 15:57:48 +0200375 .name = "sg",
376 .version = FIO_IOOPS_VERSION,
377 .init = fio_sgio_init,
378 .prep = fio_sgio_prep,
379 .queue = fio_sgio_queue,
380 .getevents = fio_sgio_getevents,
381 .event = fio_sgio_event,
382 .cleanup = fio_sgio_cleanup,
Jens Axboeb2a15192006-10-18 14:10:42 +0200383 .flags = FIO_SYNCIO | FIO_RAWIO,
Jens Axboe2866c822006-10-09 15:57:48 +0200384};
Jens Axboe34cfcda2006-11-03 14:00:45 +0100385
386#else /* FIO_HAVE_SGIO */
387
388/*
389 * When we have a proper configure system in place, we simply wont build
390 * and install this io engine. For now install a crippled version that
391 * just complains and fails to load.
392 */
393static int fio_sgio_init(struct thread_data fio_unused *td)
394{
395 fprintf(stderr, "fio: sgio not available\n");
396 return 1;
397}
398
Jens Axboe5f350952006-11-07 15:20:59 +0100399static struct ioengine_ops ioengine = {
Jens Axboe34cfcda2006-11-03 14:00:45 +0100400 .name = "sgio",
401 .version = FIO_IOOPS_VERSION,
402 .init = fio_sgio_init,
403};
404
405#endif
Jens Axboe5f350952006-11-07 15:20:59 +0100406
407static void fio_init fio_sgio_register(void)
408{
409 register_ioengine(&ioengine);
410}
411
412static void fio_exit fio_sgio_unregister(void)
413{
414 unregister_ioengine(&ioengine);
415}