->getevents() should take unsigned args
[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"
2866c82d 15
34cfcdaf
JA
16#ifdef FIO_HAVE_SGIO
17
2866c82d
JA
18struct 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;
dc0deca2
JA
26 struct pollfd *pfds;
27 int *fd_flags;
28 void *sgbuf;
2866c82d 29 unsigned int bs;
b5af8293 30 int type_checked;
2866c82d
JA
31};
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) {
cec6b55d
JA
48 hdr->dxferp = io_u->xfer_buf;
49 hdr->dxfer_len = io_u->xfer_buflen;
2866c82d
JA
50 }
51}
52
adee86c5
JA
53static 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}
2866c82d 63
e7d2e616
JA
64static int fio_sgio_getevents(struct thread_data *td, unsigned int min,
65 unsigned int max, struct timespec fio_unused *t)
2866c82d
JA
66{
67 struct sgio_data *sd = td->io_ops->data;
af52b345 68 int left = max, ret, r = 0;
dc0deca2 69 void *buf = sd->sgbuf;
af52b345 70 unsigned int i, events;
946ff865 71 struct fio_file *f;
2866c82d
JA
72
73 /*
adee86c5 74 * Fill in the file descriptors
2866c82d 75 */
adee86c5
JA
76 for_each_file(td, f, i) {
77 /*
78 * don't block for min events == 0
79 */
80 if (!min) {
dc0deca2
JA
81 sd->fd_flags[i] = fcntl(f->fd, F_GETFL);
82 fcntl(f->fd, F_SETFL, sd->fd_flags[i] | O_NONBLOCK);
adee86c5 83 }
dc0deca2
JA
84 sd->pfds[i].fd = f->fd;
85 sd->pfds[i].events = POLLIN;
2866c82d
JA
86 }
87
88 while (left) {
adee86c5
JA
89 void *p;
90
2866c82d
JA
91 do {
92 if (!min)
93 break;
adee86c5 94
2dc1bbeb 95 ret = poll(sd->pfds, td->o.nr_files, -1);
adee86c5 96 if (ret < 0) {
adee86c5 97 if (!r)
22819ec2 98 r = -errno;
e1161c32 99 td_verror(td, errno, "poll");
adee86c5
JA
100 break;
101 } else if (!ret)
102 continue;
103
2dc1bbeb 104 if (pollin_events(sd->pfds, td->o.nr_files))
2866c82d
JA
105 break;
106 } while (1);
107
adee86c5 108 if (r < 0)
2866c82d 109 break;
adee86c5
JA
110
111re_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;
22819ec2 119 r = -errno;
e1161c32 120 td_verror(td, errno, "read");
adee86c5
JA
121 break;
122 } else if (ret) {
123 p += ret;
124 events += ret / sizeof(struct sg_io_hdr);
125 }
126 }
127
128 if (r < 0)
2866c82d 129 break;
adee86c5
JA
130 if (!events) {
131 usleep(1000);
132 goto re_read;
133 }
2866c82d 134
2866c82d
JA
135 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
adee86c5
JA
145 if (!min) {
146 for_each_file(td, f, i)
dc0deca2 147 fcntl(f->fd, F_SETFL, sd->fd_flags[i]);
adee86c5 148 }
2866c82d 149
2866c82d
JA
150 return r;
151}
152
7a16dd02
JA
153static int fio_sgio_ioctl_doio(struct thread_data *td,
154 struct fio_file *f, struct io_u *io_u)
2866c82d
JA
155{
156 struct sgio_data *sd = td->io_ops->data;
157 struct sg_io_hdr *hdr = &io_u->hdr;
36167d82 158 int ret;
2866c82d
JA
159
160 sd->events[0] = io_u;
161
36167d82
JA
162 ret = ioctl(f->fd, SG_IO, hdr);
163 if (ret < 0)
a05bd42d 164 return ret;
36167d82
JA
165
166 return FIO_Q_COMPLETED;
2866c82d
JA
167}
168
7a16dd02 169static int fio_sgio_rw_doio(struct fio_file *f, struct io_u *io_u, int sync)
2866c82d
JA
170{
171 struct sg_io_hdr *hdr = &io_u->hdr;
172 int ret;
173
53cdc686 174 ret = write(f->fd, hdr, sizeof(*hdr));
2866c82d 175 if (ret < 0)
a05bd42d 176 return ret;
2866c82d
JA
177
178 if (sync) {
53cdc686 179 ret = read(f->fd, hdr, sizeof(*hdr));
2866c82d 180 if (ret < 0)
a05bd42d 181 return ret;
36167d82 182 return FIO_Q_COMPLETED;
2866c82d
JA
183 }
184
36167d82 185 return FIO_Q_QUEUED;
2866c82d
JA
186}
187
188static int fio_sgio_doio(struct thread_data *td, struct io_u *io_u, int sync)
189{
53cdc686
JA
190 struct fio_file *f = io_u->file;
191
af52b345 192 if (f->filetype == FIO_TYPE_BD)
53cdc686 193 return fio_sgio_ioctl_doio(td, f, io_u);
2866c82d 194
7a16dd02 195 return fio_sgio_rw_doio(f, io_u, sync);
2866c82d
JA
196}
197
2866c82d
JA
198static 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
cec6b55d 204 if (io_u->xfer_buflen & (sd->bs - 1)) {
2866c82d
JA
205 log_err("read/write not sector aligned\n");
206 return EINVAL;
207 }
208
2866c82d 209 if (io_u->ddir == DDIR_READ) {
87dc1ab1
JA
210 sgio_hdr_init(sd, hdr, io_u, 1);
211
2866c82d
JA
212 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
213 hdr->cmdp[0] = 0x28;
87dc1ab1
JA
214 } else if (io_u->ddir == DDIR_WRITE) {
215 sgio_hdr_init(sd, hdr, io_u, 1);
216
2866c82d
JA
217 hdr->dxfer_direction = SG_DXFER_TO_DEV;
218 hdr->cmdp[0] = 0x2a;
87dc1ab1
JA
219 } else {
220 sgio_hdr_init(sd, hdr, io_u, 0);
221
222 hdr->dxfer_direction = SG_DXFER_NONE;
223 hdr->cmdp[0] = 0x35;
224 }
225
226 if (hdr->dxfer_direction != SG_DXFER_NONE) {
cec6b55d 227 nr_blocks = io_u->xfer_buflen / sd->bs;
87dc1ab1 228 lba = io_u->offset / sd->bs;
1e97cce9
JA
229 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);
2866c82d
JA
235 }
236
2866c82d
JA
237 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
7101d9c2
JA
245 fio_ro_check(td, io_u);
246
87dc1ab1 247 ret = fio_sgio_doio(td, io_u, io_u->ddir == DDIR_SYNC);
2866c82d
JA
248
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
95bcd815 256 if (io_u->error) {
e1161c32 257 td_verror(td, io_u->error, "xfer");
36167d82 258 return FIO_Q_COMPLETED;
95bcd815
JA
259 }
260
36167d82 261 return ret;
2866c82d
JA
262}
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);
6ab9e267 280 io_u->file = &td->files[0];
2866c82d
JA
281 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{
dc0deca2
JA
305 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);
314
2866c82d
JA
315 td->io_ops->data = NULL;
316 }
317}
318
319static int fio_sgio_init(struct thread_data *td)
320{
321 struct sgio_data *sd;
2866c82d
JA
322
323 sd = malloc(sizeof(*sd));
cb781c75 324 memset(sd, 0, sizeof(*sd));
2dc1bbeb
JA
325 sd->cmds = malloc(td->o.iodepth * sizeof(struct sgio_cmd));
326 memset(sd->cmds, 0, td->o.iodepth * sizeof(struct sgio_cmd));
327 sd->events = malloc(td->o.iodepth * sizeof(struct io_u *));
328 memset(sd->events, 0, td->o.iodepth * sizeof(struct io_u *));
329 sd->pfds = malloc(sizeof(struct pollfd) * td->o.nr_files);
330 memset(sd->pfds, 0, sizeof(struct pollfd) * td->o.nr_files);
331 sd->fd_flags = malloc(sizeof(int) * td->o.nr_files);
332 memset(sd->fd_flags, 0, sizeof(int) * td->o.nr_files);
333 sd->sgbuf = malloc(sizeof(struct sg_io_hdr) * td->o.iodepth);
334 memset(sd->sgbuf, 0, sizeof(struct sg_io_hdr) * td->o.iodepth);
dc0deca2 335
2866c82d
JA
336 td->io_ops->data = sd;
337
b5af8293
JA
338 /*
339 * we want to do it, regardless of whether odirect is set or not
340 */
2dc1bbeb 341 td->o.override_sync = 1;
b5af8293
JA
342 return 0;
343}
344
345static int fio_sgio_type_check(struct thread_data *td, struct fio_file *f)
346{
347 struct sgio_data *sd = td->io_ops->data;
348 unsigned int bs;
349
af52b345 350 if (f->filetype == FIO_TYPE_BD) {
53cdc686 351 if (ioctl(f->fd, BLKSSZGET, &bs) < 0) {
e1161c32 352 td_verror(td, errno, "ioctl");
b5af8293 353 return 1;
2866c82d 354 }
af52b345 355 } else if (f->filetype == FIO_TYPE_CHAR) {
b5af8293 356 int version, ret;
2866c82d 357
53cdc686 358 if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) {
e1161c32 359 td_verror(td, errno, "ioctl");
b5af8293 360 return 1;
2866c82d
JA
361 }
362
363 ret = fio_sgio_get_bs(td, &bs);
364 if (ret)
b5af8293 365 return 1;
2866c82d
JA
366 } else {
367 log_err("ioengine sgio only works on block devices\n");
b5af8293 368 return 1;
2866c82d
JA
369 }
370
371 sd->bs = bs;
372
af52b345 373 if (f->filetype == FIO_TYPE_BD) {
36167d82
JA
374 td->io_ops->getevents = NULL;
375 td->io_ops->event = NULL;
376 }
2866c82d 377
2866c82d 378 return 0;
b5af8293
JA
379}
380
381static int fio_sgio_open(struct thread_data *td, struct fio_file *f)
382{
383 struct sgio_data *sd = td->io_ops->data;
384 int ret;
385
386 ret = generic_open_file(td, f);
387 if (ret)
388 return ret;
389
15ba640a 390 if (sd && !sd->type_checked && fio_sgio_type_check(td, f)) {
b5af8293
JA
391 generic_close_file(td, f);
392 return 1;
393 }
394
395 return 0;
2866c82d
JA
396}
397
5f350952 398static struct ioengine_ops ioengine = {
2866c82d
JA
399 .name = "sg",
400 .version = FIO_IOOPS_VERSION,
401 .init = fio_sgio_init,
402 .prep = fio_sgio_prep,
403 .queue = fio_sgio_queue,
404 .getevents = fio_sgio_getevents,
405 .event = fio_sgio_event,
406 .cleanup = fio_sgio_cleanup,
b5af8293
JA
407 .open_file = fio_sgio_open,
408 .close_file = generic_close_file,
b2a15192 409 .flags = FIO_SYNCIO | FIO_RAWIO,
2866c82d 410};
34cfcdaf
JA
411
412#else /* FIO_HAVE_SGIO */
413
414/*
415 * When we have a proper configure system in place, we simply wont build
416 * and install this io engine. For now install a crippled version that
417 * just complains and fails to load.
418 */
419static int fio_sgio_init(struct thread_data fio_unused *td)
420{
421 fprintf(stderr, "fio: sgio not available\n");
422 return 1;
423}
424
5f350952 425static struct ioengine_ops ioengine = {
34cfcdaf
JA
426 .name = "sgio",
427 .version = FIO_IOOPS_VERSION,
428 .init = fio_sgio_init,
429};
430
431#endif
5f350952
JA
432
433static void fio_init fio_sgio_register(void)
434{
435 register_ioengine(&ioengine);
436}
437
438static void fio_exit fio_sgio_unregister(void)
439{
440 unregister_ioengine(&ioengine);
441}