Commit | Line | Data |
---|---|---|
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 |
18 | struct sgio_cmd { |
19 | unsigned char cdb[10]; | |
20 | int nr; | |
21 | }; | |
22 | ||
23 | struct 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 | ||
33 | static 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 |
53 | static 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 |
64 | static 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 | */ | |
4a851614 | 80 | if (!min) |
3a35845f JA |
81 | sd->fd_flags[i] = fio_set_fd_nonblocking(f->fd, "sg"); |
82 | else | |
83 | sd->fd_flags[i] = -1; | |
4a851614 | 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 | |
112 | re_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 | 146 | if (!min) { |
affe05a9 | 147 | for_each_file(td, f, i) { |
3a35845f JA |
148 | if (sd->fd_flags[i] == -1) |
149 | continue; | |
150 | ||
affe05a9 JA |
151 | if (fcntl(f->fd, F_SETFL, sd->fd_flags[i]) < 0) |
152 | log_err("fio: sg failed to restore fcntl flags: %s\n", strerror(errno)); | |
153 | } | |
adee86c5 | 154 | } |
2866c82d | 155 | |
2866c82d JA |
156 | return r; |
157 | } | |
158 | ||
7a16dd02 JA |
159 | static int fio_sgio_ioctl_doio(struct thread_data *td, |
160 | struct fio_file *f, struct io_u *io_u) | |
2866c82d JA |
161 | { |
162 | struct sgio_data *sd = td->io_ops->data; | |
163 | struct sg_io_hdr *hdr = &io_u->hdr; | |
36167d82 | 164 | int ret; |
2866c82d JA |
165 | |
166 | sd->events[0] = io_u; | |
167 | ||
36167d82 JA |
168 | ret = ioctl(f->fd, SG_IO, hdr); |
169 | if (ret < 0) | |
a05bd42d | 170 | return ret; |
36167d82 JA |
171 | |
172 | return FIO_Q_COMPLETED; | |
2866c82d JA |
173 | } |
174 | ||
2b13e716 | 175 | static int fio_sgio_rw_doio(struct fio_file *f, struct io_u *io_u, int do_sync) |
2866c82d JA |
176 | { |
177 | struct sg_io_hdr *hdr = &io_u->hdr; | |
178 | int ret; | |
179 | ||
53cdc686 | 180 | ret = write(f->fd, hdr, sizeof(*hdr)); |
2866c82d | 181 | if (ret < 0) |
a05bd42d | 182 | return ret; |
2866c82d | 183 | |
2b13e716 | 184 | if (do_sync) { |
53cdc686 | 185 | ret = read(f->fd, hdr, sizeof(*hdr)); |
2866c82d | 186 | if (ret < 0) |
a05bd42d | 187 | return ret; |
36167d82 | 188 | return FIO_Q_COMPLETED; |
2866c82d JA |
189 | } |
190 | ||
36167d82 | 191 | return FIO_Q_QUEUED; |
2866c82d JA |
192 | } |
193 | ||
2b13e716 | 194 | static int fio_sgio_doio(struct thread_data *td, struct io_u *io_u, int do_sync) |
2866c82d | 195 | { |
53cdc686 JA |
196 | struct fio_file *f = io_u->file; |
197 | ||
af52b345 | 198 | if (f->filetype == FIO_TYPE_BD) |
53cdc686 | 199 | return fio_sgio_ioctl_doio(td, f, io_u); |
2866c82d | 200 | |
2b13e716 | 201 | return fio_sgio_rw_doio(f, io_u, do_sync); |
2866c82d JA |
202 | } |
203 | ||
2866c82d JA |
204 | static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u) |
205 | { | |
206 | struct sg_io_hdr *hdr = &io_u->hdr; | |
207 | struct sgio_data *sd = td->io_ops->data; | |
208 | int nr_blocks, lba; | |
209 | ||
cec6b55d | 210 | if (io_u->xfer_buflen & (sd->bs - 1)) { |
2866c82d JA |
211 | log_err("read/write not sector aligned\n"); |
212 | return EINVAL; | |
213 | } | |
214 | ||
2866c82d | 215 | if (io_u->ddir == DDIR_READ) { |
87dc1ab1 JA |
216 | sgio_hdr_init(sd, hdr, io_u, 1); |
217 | ||
2866c82d JA |
218 | hdr->dxfer_direction = SG_DXFER_FROM_DEV; |
219 | hdr->cmdp[0] = 0x28; | |
87dc1ab1 JA |
220 | } else if (io_u->ddir == DDIR_WRITE) { |
221 | sgio_hdr_init(sd, hdr, io_u, 1); | |
222 | ||
2866c82d JA |
223 | hdr->dxfer_direction = SG_DXFER_TO_DEV; |
224 | hdr->cmdp[0] = 0x2a; | |
87dc1ab1 JA |
225 | } else { |
226 | sgio_hdr_init(sd, hdr, io_u, 0); | |
227 | ||
228 | hdr->dxfer_direction = SG_DXFER_NONE; | |
229 | hdr->cmdp[0] = 0x35; | |
230 | } | |
231 | ||
232 | if (hdr->dxfer_direction != SG_DXFER_NONE) { | |
cec6b55d | 233 | nr_blocks = io_u->xfer_buflen / sd->bs; |
87dc1ab1 | 234 | lba = io_u->offset / sd->bs; |
1e97cce9 JA |
235 | hdr->cmdp[2] = (unsigned char) ((lba >> 24) & 0xff); |
236 | hdr->cmdp[3] = (unsigned char) ((lba >> 16) & 0xff); | |
237 | hdr->cmdp[4] = (unsigned char) ((lba >> 8) & 0xff); | |
238 | hdr->cmdp[5] = (unsigned char) (lba & 0xff); | |
239 | hdr->cmdp[7] = (unsigned char) ((nr_blocks >> 8) & 0xff); | |
240 | hdr->cmdp[8] = (unsigned char) (nr_blocks & 0xff); | |
2866c82d JA |
241 | } |
242 | ||
2866c82d JA |
243 | return 0; |
244 | } | |
245 | ||
246 | static int fio_sgio_queue(struct thread_data *td, struct io_u *io_u) | |
247 | { | |
248 | struct sg_io_hdr *hdr = &io_u->hdr; | |
f6db4fa5 | 249 | int ret, do_sync = 0; |
2866c82d | 250 | |
7101d9c2 JA |
251 | fio_ro_check(td, io_u); |
252 | ||
f6db4fa5 JA |
253 | if (td->o.sync_io || td->o.odirect || ddir_sync(io_u->ddir)) |
254 | do_sync = 1; | |
255 | ||
256 | ret = fio_sgio_doio(td, io_u, do_sync); | |
2866c82d JA |
257 | |
258 | if (ret < 0) | |
259 | io_u->error = errno; | |
260 | else if (hdr->status) { | |
261 | io_u->resid = hdr->resid; | |
262 | io_u->error = EIO; | |
263 | } | |
264 | ||
95bcd815 | 265 | if (io_u->error) { |
e1161c32 | 266 | td_verror(td, io_u->error, "xfer"); |
36167d82 | 267 | return FIO_Q_COMPLETED; |
95bcd815 JA |
268 | } |
269 | ||
36167d82 | 270 | return ret; |
2866c82d JA |
271 | } |
272 | ||
273 | static struct io_u *fio_sgio_event(struct thread_data *td, int event) | |
274 | { | |
275 | struct sgio_data *sd = td->io_ops->data; | |
276 | ||
277 | return sd->events[event]; | |
278 | } | |
279 | ||
280 | static int fio_sgio_get_bs(struct thread_data *td, unsigned int *bs) | |
281 | { | |
282 | struct sgio_data *sd = td->io_ops->data; | |
2039ab1d | 283 | struct io_u io_u; |
2866c82d JA |
284 | struct sg_io_hdr *hdr; |
285 | unsigned char buf[8]; | |
286 | int ret; | |
287 | ||
2039ab1d JA |
288 | memset(&io_u, 0, sizeof(io_u)); |
289 | io_u.file = td->files[0]; | |
2866c82d | 290 | |
2039ab1d JA |
291 | hdr = &io_u.hdr; |
292 | sgio_hdr_init(sd, hdr, &io_u, 0); | |
2866c82d JA |
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 | ||
2039ab1d JA |
300 | ret = fio_sgio_doio(td, &io_u, 1); |
301 | if (ret) | |
2866c82d | 302 | return ret; |
2866c82d JA |
303 | |
304 | *bs = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7]; | |
2866c82d JA |
305 | return 0; |
306 | } | |
307 | ||
308 | static void fio_sgio_cleanup(struct thread_data *td) | |
309 | { | |
dc0deca2 JA |
310 | struct sgio_data *sd = td->io_ops->data; |
311 | ||
312 | if (sd) { | |
313 | free(sd->events); | |
314 | free(sd->cmds); | |
315 | free(sd->fd_flags); | |
316 | free(sd->pfds); | |
317 | free(sd->sgbuf); | |
318 | free(sd); | |
2866c82d JA |
319 | } |
320 | } | |
321 | ||
322 | static int fio_sgio_init(struct thread_data *td) | |
323 | { | |
324 | struct sgio_data *sd; | |
2866c82d JA |
325 | |
326 | sd = malloc(sizeof(*sd)); | |
cb781c75 | 327 | memset(sd, 0, sizeof(*sd)); |
2dc1bbeb JA |
328 | sd->cmds = malloc(td->o.iodepth * sizeof(struct sgio_cmd)); |
329 | memset(sd->cmds, 0, td->o.iodepth * sizeof(struct sgio_cmd)); | |
330 | sd->events = malloc(td->o.iodepth * sizeof(struct io_u *)); | |
331 | memset(sd->events, 0, td->o.iodepth * sizeof(struct io_u *)); | |
332 | sd->pfds = malloc(sizeof(struct pollfd) * td->o.nr_files); | |
333 | memset(sd->pfds, 0, sizeof(struct pollfd) * td->o.nr_files); | |
334 | sd->fd_flags = malloc(sizeof(int) * td->o.nr_files); | |
335 | memset(sd->fd_flags, 0, sizeof(int) * td->o.nr_files); | |
336 | sd->sgbuf = malloc(sizeof(struct sg_io_hdr) * td->o.iodepth); | |
337 | memset(sd->sgbuf, 0, sizeof(struct sg_io_hdr) * td->o.iodepth); | |
dc0deca2 | 338 | |
2866c82d JA |
339 | td->io_ops->data = sd; |
340 | ||
b5af8293 JA |
341 | /* |
342 | * we want to do it, regardless of whether odirect is set or not | |
343 | */ | |
2dc1bbeb | 344 | td->o.override_sync = 1; |
b5af8293 JA |
345 | return 0; |
346 | } | |
347 | ||
348 | static int fio_sgio_type_check(struct thread_data *td, struct fio_file *f) | |
349 | { | |
350 | struct sgio_data *sd = td->io_ops->data; | |
351 | unsigned int bs; | |
352 | ||
af52b345 | 353 | if (f->filetype == FIO_TYPE_BD) { |
53cdc686 | 354 | if (ioctl(f->fd, BLKSSZGET, &bs) < 0) { |
e1161c32 | 355 | td_verror(td, errno, "ioctl"); |
b5af8293 | 356 | return 1; |
2866c82d | 357 | } |
af52b345 | 358 | } else if (f->filetype == FIO_TYPE_CHAR) { |
b5af8293 | 359 | int version, ret; |
2866c82d | 360 | |
53cdc686 | 361 | if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) { |
e1161c32 | 362 | td_verror(td, errno, "ioctl"); |
b5af8293 | 363 | return 1; |
2866c82d JA |
364 | } |
365 | ||
366 | ret = fio_sgio_get_bs(td, &bs); | |
367 | if (ret) | |
b5af8293 | 368 | return 1; |
2866c82d | 369 | } else { |
d0c70934 | 370 | log_err("ioengine sg only works on block devices\n"); |
b5af8293 | 371 | return 1; |
2866c82d JA |
372 | } |
373 | ||
374 | sd->bs = bs; | |
375 | ||
af52b345 | 376 | if (f->filetype == FIO_TYPE_BD) { |
36167d82 JA |
377 | td->io_ops->getevents = NULL; |
378 | td->io_ops->event = NULL; | |
379 | } | |
2866c82d | 380 | |
2866c82d | 381 | return 0; |
b5af8293 JA |
382 | } |
383 | ||
384 | static int fio_sgio_open(struct thread_data *td, struct fio_file *f) | |
385 | { | |
386 | struct sgio_data *sd = td->io_ops->data; | |
387 | int ret; | |
388 | ||
389 | ret = generic_open_file(td, f); | |
390 | if (ret) | |
391 | return ret; | |
392 | ||
15ba640a | 393 | if (sd && !sd->type_checked && fio_sgio_type_check(td, f)) { |
6977bcd0 | 394 | ret = generic_close_file(td, f); |
b5af8293 JA |
395 | return 1; |
396 | } | |
397 | ||
398 | return 0; | |
2866c82d JA |
399 | } |
400 | ||
5f350952 | 401 | static struct ioengine_ops ioengine = { |
2866c82d JA |
402 | .name = "sg", |
403 | .version = FIO_IOOPS_VERSION, | |
404 | .init = fio_sgio_init, | |
405 | .prep = fio_sgio_prep, | |
406 | .queue = fio_sgio_queue, | |
407 | .getevents = fio_sgio_getevents, | |
408 | .event = fio_sgio_event, | |
409 | .cleanup = fio_sgio_cleanup, | |
b5af8293 JA |
410 | .open_file = fio_sgio_open, |
411 | .close_file = generic_close_file, | |
df9c26b1 | 412 | .get_file_size = generic_get_file_size, |
b2a15192 | 413 | .flags = FIO_SYNCIO | FIO_RAWIO, |
2866c82d | 414 | }; |
34cfcdaf JA |
415 | |
416 | #else /* FIO_HAVE_SGIO */ | |
417 | ||
418 | /* | |
419 | * When we have a proper configure system in place, we simply wont build | |
420 | * and install this io engine. For now install a crippled version that | |
421 | * just complains and fails to load. | |
422 | */ | |
423 | static int fio_sgio_init(struct thread_data fio_unused *td) | |
424 | { | |
a3edaf76 | 425 | log_err("fio: ioengine sg not available\n"); |
34cfcdaf JA |
426 | return 1; |
427 | } | |
428 | ||
5f350952 | 429 | static struct ioengine_ops ioengine = { |
d0c70934 | 430 | .name = "sg", |
34cfcdaf JA |
431 | .version = FIO_IOOPS_VERSION, |
432 | .init = fio_sgio_init, | |
433 | }; | |
434 | ||
435 | #endif | |
5f350952 JA |
436 | |
437 | static void fio_init fio_sgio_register(void) | |
438 | { | |
439 | register_ioengine(&ioengine); | |
440 | } | |
441 | ||
442 | static void fio_exit fio_sgio_unregister(void) | |
443 | { | |
444 | unregister_ioengine(&ioengine); | |
445 | } |