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