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> | |
8393ca93 | 11 | #include <poll.h> |
5f350952 JA |
12 | |
13 | #include "../fio.h" | |
52b81b7c | 14 | #include "../optgroup.h" |
2866c82d | 15 | |
34cfcdaf JA |
16 | #ifdef FIO_HAVE_SGIO |
17 | ||
52b81b7c KD |
18 | |
19 | struct sg_options { | |
20 | void *pad; | |
21 | unsigned int readfua; | |
22 | unsigned int writefua; | |
23 | }; | |
24 | ||
25 | static struct fio_option options[] = { | |
26 | { | |
27 | .name = "readfua", | |
28 | .lname = "sg engine read fua flag support", | |
29 | .type = FIO_OPT_BOOL, | |
30 | .off1 = offsetof(struct sg_options, readfua), | |
31 | .help = "Set FUA flag (force unit access) for all Read operations", | |
32 | .def = "0", | |
33 | .category = FIO_OPT_C_ENGINE, | |
34 | .group = FIO_OPT_G_SG, | |
35 | }, | |
36 | { | |
37 | .name = "writefua", | |
38 | .lname = "sg engine write fua flag support", | |
39 | .type = FIO_OPT_BOOL, | |
40 | .off1 = offsetof(struct sg_options, writefua), | |
41 | .help = "Set FUA flag (force unit access) for all Write operations", | |
42 | .def = "0", | |
43 | .category = FIO_OPT_C_ENGINE, | |
44 | .group = FIO_OPT_G_SG, | |
45 | }, | |
46 | { | |
47 | .name = NULL, | |
48 | }, | |
49 | }; | |
50 | ||
5ad7be56 KD |
51 | #define MAX_10B_LBA 0xFFFFFFFFULL |
52 | #define SCSI_TIMEOUT_MS 30000 // 30 second timeout; currently no method to override | |
53 | #define MAX_SB 64 // sense block maximum return size | |
54 | ||
2866c82d | 55 | struct sgio_cmd { |
fde57152 | 56 | unsigned char cdb[16]; // enhanced from 10 to support 16 byte commands |
5ad7be56 | 57 | unsigned char sb[MAX_SB]; // add sense block to commands |
2866c82d JA |
58 | int nr; |
59 | }; | |
60 | ||
61 | struct sgio_data { | |
62 | struct sgio_cmd *cmds; | |
63 | struct io_u **events; | |
dc0deca2 JA |
64 | struct pollfd *pfds; |
65 | int *fd_flags; | |
66 | void *sgbuf; | |
2866c82d | 67 | unsigned int bs; |
b5af8293 | 68 | int type_checked; |
2866c82d JA |
69 | }; |
70 | ||
71 | static void sgio_hdr_init(struct sgio_data *sd, struct sg_io_hdr *hdr, | |
72 | struct io_u *io_u, int fs) | |
73 | { | |
74 | struct sgio_cmd *sc = &sd->cmds[io_u->index]; | |
75 | ||
76 | memset(hdr, 0, sizeof(*hdr)); | |
77 | memset(sc->cdb, 0, sizeof(sc->cdb)); | |
78 | ||
79 | hdr->interface_id = 'S'; | |
80 | hdr->cmdp = sc->cdb; | |
81 | hdr->cmd_len = sizeof(sc->cdb); | |
5ad7be56 KD |
82 | hdr->sbp = sc->sb; |
83 | hdr->mx_sb_len = sizeof(sc->sb); | |
2866c82d JA |
84 | hdr->pack_id = io_u->index; |
85 | hdr->usr_ptr = io_u; | |
86 | ||
87 | if (fs) { | |
cec6b55d JA |
88 | hdr->dxferp = io_u->xfer_buf; |
89 | hdr->dxfer_len = io_u->xfer_buflen; | |
2866c82d JA |
90 | } |
91 | } | |
92 | ||
adee86c5 JA |
93 | static int pollin_events(struct pollfd *pfds, int fds) |
94 | { | |
95 | int i; | |
96 | ||
97 | for (i = 0; i < fds; i++) | |
98 | if (pfds[i].revents & POLLIN) | |
99 | return 1; | |
100 | ||
101 | return 0; | |
102 | } | |
2866c82d | 103 | |
14d0261e JA |
104 | static int sg_fd_read(int fd, void *data, size_t size) |
105 | { | |
106 | int err = 0; | |
107 | ||
108 | while (size) { | |
109 | ssize_t ret; | |
110 | ||
111 | ret = read(fd, data, size); | |
112 | if (ret < 0) { | |
113 | if (errno == EAGAIN || errno == EINTR) | |
114 | continue; | |
115 | err = errno; | |
116 | break; | |
117 | } else if (!ret) | |
118 | break; | |
119 | else { | |
120 | data += ret; | |
121 | size -= ret; | |
122 | } | |
123 | } | |
124 | ||
125 | if (err) | |
126 | return err; | |
127 | if (size) | |
128 | return EAGAIN; | |
129 | ||
130 | return 0; | |
131 | } | |
132 | ||
e7d2e616 | 133 | static int fio_sgio_getevents(struct thread_data *td, unsigned int min, |
1f440ece JA |
134 | unsigned int max, |
135 | const struct timespec fio_unused *t) | |
2866c82d | 136 | { |
565e784d | 137 | struct sgio_data *sd = td->io_ops_data; |
5ad7be56 | 138 | int left = max, eventNum, ret, r = 0; |
dc0deca2 | 139 | void *buf = sd->sgbuf; |
af52b345 | 140 | unsigned int i, events; |
946ff865 | 141 | struct fio_file *f; |
2866c82d JA |
142 | |
143 | /* | |
adee86c5 | 144 | * Fill in the file descriptors |
2866c82d | 145 | */ |
adee86c5 JA |
146 | for_each_file(td, f, i) { |
147 | /* | |
148 | * don't block for min events == 0 | |
149 | */ | |
4a851614 | 150 | if (!min) |
3a35845f JA |
151 | sd->fd_flags[i] = fio_set_fd_nonblocking(f->fd, "sg"); |
152 | else | |
153 | sd->fd_flags[i] = -1; | |
4a851614 | 154 | |
dc0deca2 JA |
155 | sd->pfds[i].fd = f->fd; |
156 | sd->pfds[i].events = POLLIN; | |
2866c82d JA |
157 | } |
158 | ||
159 | while (left) { | |
c97e3cb0 | 160 | char *p; |
adee86c5 | 161 | |
5ad7be56 KD |
162 | dprint(FD_IO, "sgio_getevents: sd %p: left=%d\n", sd, left); |
163 | ||
2866c82d JA |
164 | do { |
165 | if (!min) | |
166 | break; | |
adee86c5 | 167 | |
2dc1bbeb | 168 | ret = poll(sd->pfds, td->o.nr_files, -1); |
adee86c5 | 169 | if (ret < 0) { |
adee86c5 | 170 | if (!r) |
22819ec2 | 171 | r = -errno; |
e1161c32 | 172 | td_verror(td, errno, "poll"); |
adee86c5 JA |
173 | break; |
174 | } else if (!ret) | |
175 | continue; | |
176 | ||
2dc1bbeb | 177 | if (pollin_events(sd->pfds, td->o.nr_files)) |
2866c82d JA |
178 | break; |
179 | } while (1); | |
180 | ||
adee86c5 | 181 | if (r < 0) |
2866c82d | 182 | break; |
adee86c5 JA |
183 | |
184 | re_read: | |
185 | p = buf; | |
186 | events = 0; | |
187 | for_each_file(td, f, i) { | |
5ad7be56 | 188 | for (eventNum = 0; eventNum < left; eventNum++) { |
14d0261e | 189 | ret = sg_fd_read(f->fd, p, sizeof(struct sg_io_hdr)); |
5ad7be56 | 190 | dprint(FD_IO, "sgio_getevents: ret: %d\n", ret); |
14d0261e JA |
191 | if (ret) { |
192 | r = -ret; | |
193 | td_verror(td, r, "sg_read"); | |
5ad7be56 | 194 | break; |
5ad7be56 | 195 | } |
14d0261e JA |
196 | p += sizeof(struct sg_io_hdr); |
197 | events++; | |
198 | dprint(FD_IO, "sgio_getevents: events: %d\n", events); | |
adee86c5 JA |
199 | } |
200 | } | |
201 | ||
14d0261e | 202 | if (r < 0 && !events) |
2866c82d | 203 | break; |
adee86c5 JA |
204 | if (!events) { |
205 | usleep(1000); | |
206 | goto re_read; | |
207 | } | |
2866c82d | 208 | |
2866c82d JA |
209 | left -= events; |
210 | r += events; | |
211 | ||
212 | for (i = 0; i < events; i++) { | |
213 | struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i; | |
2866c82d | 214 | sd->events[i] = hdr->usr_ptr; |
5ad7be56 KD |
215 | |
216 | /* record if an io error occurred, ignore resid */ | |
217 | if (hdr->info & SG_INFO_CHECK) { | |
218 | struct io_u *io_u; | |
219 | io_u = (struct io_u *)(hdr->usr_ptr); | |
be660713 | 220 | memcpy(&io_u->hdr, hdr, sizeof(struct sg_io_hdr)); |
5ad7be56 KD |
221 | sd->events[i]->error = EIO; |
222 | } | |
2866c82d JA |
223 | } |
224 | } | |
225 | ||
adee86c5 | 226 | if (!min) { |
affe05a9 | 227 | for_each_file(td, f, i) { |
3a35845f JA |
228 | if (sd->fd_flags[i] == -1) |
229 | continue; | |
230 | ||
affe05a9 JA |
231 | if (fcntl(f->fd, F_SETFL, sd->fd_flags[i]) < 0) |
232 | log_err("fio: sg failed to restore fcntl flags: %s\n", strerror(errno)); | |
233 | } | |
adee86c5 | 234 | } |
2866c82d | 235 | |
2866c82d JA |
236 | return r; |
237 | } | |
238 | ||
2e4ef4fb JA |
239 | static enum fio_q_status fio_sgio_ioctl_doio(struct thread_data *td, |
240 | struct fio_file *f, | |
241 | struct io_u *io_u) | |
2866c82d | 242 | { |
565e784d | 243 | struct sgio_data *sd = td->io_ops_data; |
2866c82d | 244 | struct sg_io_hdr *hdr = &io_u->hdr; |
36167d82 | 245 | int ret; |
2866c82d JA |
246 | |
247 | sd->events[0] = io_u; | |
248 | ||
36167d82 JA |
249 | ret = ioctl(f->fd, SG_IO, hdr); |
250 | if (ret < 0) | |
a05bd42d | 251 | return ret; |
36167d82 | 252 | |
5ad7be56 KD |
253 | /* record if an io error occurred */ |
254 | if (hdr->info & SG_INFO_CHECK) | |
255 | io_u->error = EIO; | |
256 | ||
36167d82 | 257 | return FIO_Q_COMPLETED; |
2866c82d JA |
258 | } |
259 | ||
2b13e716 | 260 | static int fio_sgio_rw_doio(struct fio_file *f, struct io_u *io_u, int do_sync) |
2866c82d JA |
261 | { |
262 | struct sg_io_hdr *hdr = &io_u->hdr; | |
263 | int ret; | |
264 | ||
53cdc686 | 265 | ret = write(f->fd, hdr, sizeof(*hdr)); |
2866c82d | 266 | if (ret < 0) |
a05bd42d | 267 | return ret; |
2866c82d | 268 | |
2b13e716 | 269 | if (do_sync) { |
53cdc686 | 270 | ret = read(f->fd, hdr, sizeof(*hdr)); |
2866c82d | 271 | if (ret < 0) |
a05bd42d | 272 | return ret; |
5ad7be56 KD |
273 | |
274 | /* record if an io error occurred */ | |
275 | if (hdr->info & SG_INFO_CHECK) | |
276 | io_u->error = EIO; | |
277 | ||
36167d82 | 278 | return FIO_Q_COMPLETED; |
2866c82d JA |
279 | } |
280 | ||
36167d82 | 281 | return FIO_Q_QUEUED; |
2866c82d JA |
282 | } |
283 | ||
2b13e716 | 284 | static int fio_sgio_doio(struct thread_data *td, struct io_u *io_u, int do_sync) |
2866c82d | 285 | { |
53cdc686 | 286 | struct fio_file *f = io_u->file; |
5ad7be56 | 287 | int ret; |
53cdc686 | 288 | |
686fbd31 | 289 | if (f->filetype == FIO_TYPE_BLOCK) { |
5ad7be56 | 290 | ret = fio_sgio_ioctl_doio(td, f, io_u); |
c9aeb797 | 291 | td_verror(td, io_u->error, __func__); |
5ad7be56 KD |
292 | } else { |
293 | ret = fio_sgio_rw_doio(f, io_u, do_sync); | |
294 | if (do_sync) | |
c9aeb797 | 295 | td_verror(td, io_u->error, __func__); |
5ad7be56 | 296 | } |
2866c82d | 297 | |
5ad7be56 | 298 | return ret; |
2866c82d JA |
299 | } |
300 | ||
2866c82d JA |
301 | static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u) |
302 | { | |
303 | struct sg_io_hdr *hdr = &io_u->hdr; | |
52b81b7c | 304 | struct sg_options *o = td->eo; |
565e784d | 305 | struct sgio_data *sd = td->io_ops_data; |
5ad7be56 | 306 | long long nr_blocks, lba; |
2866c82d | 307 | |
cec6b55d | 308 | if (io_u->xfer_buflen & (sd->bs - 1)) { |
2866c82d JA |
309 | log_err("read/write not sector aligned\n"); |
310 | return EINVAL; | |
311 | } | |
312 | ||
5ad7be56 KD |
313 | nr_blocks = io_u->xfer_buflen / sd->bs; |
314 | lba = io_u->offset / sd->bs; | |
315 | ||
2866c82d | 316 | if (io_u->ddir == DDIR_READ) { |
87dc1ab1 JA |
317 | sgio_hdr_init(sd, hdr, io_u, 1); |
318 | ||
2866c82d | 319 | hdr->dxfer_direction = SG_DXFER_FROM_DEV; |
5ad7be56 KD |
320 | if (lba < MAX_10B_LBA) |
321 | hdr->cmdp[0] = 0x28; // read(10) | |
322 | else | |
323 | hdr->cmdp[0] = 0x88; // read(16) | |
52b81b7c KD |
324 | |
325 | if (o->readfua) | |
326 | hdr->cmdp[1] |= 0x08; | |
327 | ||
87dc1ab1 JA |
328 | } else if (io_u->ddir == DDIR_WRITE) { |
329 | sgio_hdr_init(sd, hdr, io_u, 1); | |
330 | ||
2866c82d | 331 | hdr->dxfer_direction = SG_DXFER_TO_DEV; |
5ad7be56 KD |
332 | if (lba < MAX_10B_LBA) |
333 | hdr->cmdp[0] = 0x2a; // write(10) | |
334 | else | |
335 | hdr->cmdp[0] = 0x8a; // write(16) | |
52b81b7c KD |
336 | |
337 | if (o->writefua) | |
338 | hdr->cmdp[1] |= 0x08; | |
339 | ||
87dc1ab1 JA |
340 | } else { |
341 | sgio_hdr_init(sd, hdr, io_u, 0); | |
87dc1ab1 | 342 | hdr->dxfer_direction = SG_DXFER_NONE; |
5ad7be56 KD |
343 | if (lba < MAX_10B_LBA) |
344 | hdr->cmdp[0] = 0x35; // synccache(10) | |
345 | else | |
346 | hdr->cmdp[0] = 0x91; // synccache(16) | |
87dc1ab1 JA |
347 | } |
348 | ||
5ad7be56 KD |
349 | /* |
350 | * for synccache, we leave lba and length to 0 to sync all | |
351 | * blocks on medium. | |
352 | */ | |
87dc1ab1 | 353 | if (hdr->dxfer_direction != SG_DXFER_NONE) { |
5ad7be56 KD |
354 | if (lba < MAX_10B_LBA) { |
355 | hdr->cmdp[2] = (unsigned char) ((lba >> 24) & 0xff); | |
356 | hdr->cmdp[3] = (unsigned char) ((lba >> 16) & 0xff); | |
357 | hdr->cmdp[4] = (unsigned char) ((lba >> 8) & 0xff); | |
358 | hdr->cmdp[5] = (unsigned char) (lba & 0xff); | |
359 | hdr->cmdp[7] = (unsigned char) ((nr_blocks >> 8) & 0xff); | |
360 | hdr->cmdp[8] = (unsigned char) (nr_blocks & 0xff); | |
361 | } else { | |
362 | hdr->cmdp[2] = (unsigned char) ((lba >> 56) & 0xff); | |
363 | hdr->cmdp[3] = (unsigned char) ((lba >> 48) & 0xff); | |
364 | hdr->cmdp[4] = (unsigned char) ((lba >> 40) & 0xff); | |
365 | hdr->cmdp[5] = (unsigned char) ((lba >> 32) & 0xff); | |
366 | hdr->cmdp[6] = (unsigned char) ((lba >> 24) & 0xff); | |
367 | hdr->cmdp[7] = (unsigned char) ((lba >> 16) & 0xff); | |
368 | hdr->cmdp[8] = (unsigned char) ((lba >> 8) & 0xff); | |
369 | hdr->cmdp[9] = (unsigned char) (lba & 0xff); | |
370 | hdr->cmdp[10] = (unsigned char) ((nr_blocks >> 32) & 0xff); | |
371 | hdr->cmdp[11] = (unsigned char) ((nr_blocks >> 16) & 0xff); | |
372 | hdr->cmdp[12] = (unsigned char) ((nr_blocks >> 8) & 0xff); | |
373 | hdr->cmdp[13] = (unsigned char) (nr_blocks & 0xff); | |
374 | } | |
2866c82d JA |
375 | } |
376 | ||
5ad7be56 | 377 | hdr->timeout = SCSI_TIMEOUT_MS; |
2866c82d JA |
378 | return 0; |
379 | } | |
380 | ||
2e4ef4fb JA |
381 | static enum fio_q_status fio_sgio_queue(struct thread_data *td, |
382 | struct io_u *io_u) | |
2866c82d JA |
383 | { |
384 | struct sg_io_hdr *hdr = &io_u->hdr; | |
f6db4fa5 | 385 | int ret, do_sync = 0; |
2866c82d | 386 | |
7101d9c2 JA |
387 | fio_ro_check(td, io_u); |
388 | ||
f6db4fa5 JA |
389 | if (td->o.sync_io || td->o.odirect || ddir_sync(io_u->ddir)) |
390 | do_sync = 1; | |
391 | ||
392 | ret = fio_sgio_doio(td, io_u, do_sync); | |
2866c82d JA |
393 | |
394 | if (ret < 0) | |
395 | io_u->error = errno; | |
396 | else if (hdr->status) { | |
397 | io_u->resid = hdr->resid; | |
398 | io_u->error = EIO; | |
399 | } | |
400 | ||
95bcd815 | 401 | if (io_u->error) { |
e1161c32 | 402 | td_verror(td, io_u->error, "xfer"); |
36167d82 | 403 | return FIO_Q_COMPLETED; |
95bcd815 JA |
404 | } |
405 | ||
36167d82 | 406 | return ret; |
2866c82d JA |
407 | } |
408 | ||
409 | static struct io_u *fio_sgio_event(struct thread_data *td, int event) | |
410 | { | |
565e784d | 411 | struct sgio_data *sd = td->io_ops_data; |
2866c82d JA |
412 | |
413 | return sd->events[event]; | |
414 | } | |
415 | ||
5ad7be56 KD |
416 | static int fio_sgio_read_capacity(struct thread_data *td, unsigned int *bs, |
417 | unsigned long long *max_lba) | |
2866c82d | 418 | { |
5ad7be56 KD |
419 | /* |
420 | * need to do read capacity operation w/o benefit of sd or | |
421 | * io_u structures, which are not initialized until later. | |
422 | */ | |
423 | struct sg_io_hdr hdr; | |
424 | unsigned char cmd[16]; | |
425 | unsigned char sb[64]; | |
426 | unsigned char buf[32]; // read capacity return | |
2866c82d | 427 | int ret; |
5ad7be56 | 428 | int fd = -1; |
2866c82d | 429 | |
5ad7be56 | 430 | struct fio_file *f = td->files[0]; |
2866c82d | 431 | |
5ad7be56 KD |
432 | /* open file independent of rest of application */ |
433 | fd = open(f->file_name, O_RDONLY); | |
434 | if (fd < 0) | |
435 | return -errno; | |
2866c82d | 436 | |
5ad7be56 KD |
437 | memset(&hdr, 0, sizeof(hdr)); |
438 | memset(cmd, 0, sizeof(cmd)); | |
439 | memset(sb, 0, sizeof(sb)); | |
440 | memset(buf, 0, sizeof(buf)); | |
2866c82d | 441 | |
5ad7be56 KD |
442 | /* First let's try a 10 byte read capacity. */ |
443 | hdr.interface_id = 'S'; | |
444 | hdr.cmdp = cmd; | |
445 | hdr.cmd_len = 10; | |
446 | hdr.sbp = sb; | |
447 | hdr.mx_sb_len = sizeof(sb); | |
448 | hdr.timeout = SCSI_TIMEOUT_MS; | |
449 | hdr.cmdp[0] = 0x25; // Read Capacity(10) | |
450 | hdr.dxfer_direction = SG_DXFER_FROM_DEV; | |
451 | hdr.dxferp = buf; | |
452 | hdr.dxfer_len = sizeof(buf); | |
453 | ||
454 | ret = ioctl(fd, SG_IO, &hdr); | |
455 | if (ret < 0) { | |
456 | close(fd); | |
2866c82d | 457 | return ret; |
5ad7be56 | 458 | } |
2866c82d | 459 | |
7ad2ddff JA |
460 | *bs = ((unsigned long) buf[4] << 24) | ((unsigned long) buf[5] << 16) | |
461 | ((unsigned long) buf[6] << 8) | (unsigned long) buf[7]; | |
462 | *max_lba = ((unsigned long) buf[0] << 24) | ((unsigned long) buf[1] << 16) | | |
463 | ((unsigned long) buf[2] << 8) | (unsigned long) buf[3]; | |
5ad7be56 KD |
464 | |
465 | /* | |
fde57152 TK |
466 | * If max lba masked by MAX_10B_LBA equals MAX_10B_LBA, |
467 | * then need to retry with 16 byte Read Capacity command. | |
5ad7be56 KD |
468 | */ |
469 | if (*max_lba == MAX_10B_LBA) { | |
470 | hdr.cmd_len = 16; | |
28c43a89 TK |
471 | hdr.cmdp[0] = 0x9e; // service action |
472 | hdr.cmdp[1] = 0x10; // Read Capacity(16) | |
5ad7be56 KD |
473 | hdr.cmdp[10] = (unsigned char) ((sizeof(buf) >> 24) & 0xff); |
474 | hdr.cmdp[11] = (unsigned char) ((sizeof(buf) >> 16) & 0xff); | |
475 | hdr.cmdp[12] = (unsigned char) ((sizeof(buf) >> 8) & 0xff); | |
476 | hdr.cmdp[13] = (unsigned char) (sizeof(buf) & 0xff); | |
477 | ||
478 | hdr.dxfer_direction = SG_DXFER_FROM_DEV; | |
479 | hdr.dxferp = buf; | |
480 | hdr.dxfer_len = sizeof(buf); | |
481 | ||
482 | ret = ioctl(fd, SG_IO, &hdr); | |
483 | if (ret < 0) { | |
484 | close(fd); | |
485 | return ret; | |
486 | } | |
487 | ||
488 | /* record if an io error occurred */ | |
489 | if (hdr.info & SG_INFO_CHECK) | |
490 | td_verror(td, EIO, "fio_sgio_read_capacity"); | |
491 | ||
492 | *bs = (buf[8] << 24) | (buf[9] << 16) | (buf[10] << 8) | buf[11]; | |
493 | *max_lba = ((unsigned long long)buf[0] << 56) | | |
494 | ((unsigned long long)buf[1] << 48) | | |
495 | ((unsigned long long)buf[2] << 40) | | |
496 | ((unsigned long long)buf[3] << 32) | | |
497 | ((unsigned long long)buf[4] << 24) | | |
498 | ((unsigned long long)buf[5] << 16) | | |
499 | ((unsigned long long)buf[6] << 8) | | |
500 | (unsigned long long)buf[7]; | |
501 | } | |
502 | ||
503 | close(fd); | |
2866c82d JA |
504 | return 0; |
505 | } | |
506 | ||
507 | static void fio_sgio_cleanup(struct thread_data *td) | |
508 | { | |
565e784d | 509 | struct sgio_data *sd = td->io_ops_data; |
dc0deca2 JA |
510 | |
511 | if (sd) { | |
512 | free(sd->events); | |
513 | free(sd->cmds); | |
514 | free(sd->fd_flags); | |
515 | free(sd->pfds); | |
516 | free(sd->sgbuf); | |
517 | free(sd); | |
2866c82d JA |
518 | } |
519 | } | |
520 | ||
521 | static int fio_sgio_init(struct thread_data *td) | |
522 | { | |
523 | struct sgio_data *sd; | |
2866c82d JA |
524 | |
525 | sd = malloc(sizeof(*sd)); | |
cb781c75 | 526 | memset(sd, 0, sizeof(*sd)); |
2dc1bbeb JA |
527 | sd->cmds = malloc(td->o.iodepth * sizeof(struct sgio_cmd)); |
528 | memset(sd->cmds, 0, td->o.iodepth * sizeof(struct sgio_cmd)); | |
529 | sd->events = malloc(td->o.iodepth * sizeof(struct io_u *)); | |
530 | memset(sd->events, 0, td->o.iodepth * sizeof(struct io_u *)); | |
531 | sd->pfds = malloc(sizeof(struct pollfd) * td->o.nr_files); | |
532 | memset(sd->pfds, 0, sizeof(struct pollfd) * td->o.nr_files); | |
533 | sd->fd_flags = malloc(sizeof(int) * td->o.nr_files); | |
534 | memset(sd->fd_flags, 0, sizeof(int) * td->o.nr_files); | |
535 | sd->sgbuf = malloc(sizeof(struct sg_io_hdr) * td->o.iodepth); | |
536 | memset(sd->sgbuf, 0, sizeof(struct sg_io_hdr) * td->o.iodepth); | |
5ad7be56 | 537 | sd->type_checked = 0; |
565e784d | 538 | td->io_ops_data = sd; |
2866c82d | 539 | |
b5af8293 JA |
540 | /* |
541 | * we want to do it, regardless of whether odirect is set or not | |
542 | */ | |
2dc1bbeb | 543 | td->o.override_sync = 1; |
b5af8293 JA |
544 | return 0; |
545 | } | |
546 | ||
547 | static int fio_sgio_type_check(struct thread_data *td, struct fio_file *f) | |
548 | { | |
565e784d | 549 | struct sgio_data *sd = td->io_ops_data; |
5ad7be56 KD |
550 | unsigned int bs = 0; |
551 | unsigned long long max_lba = 0; | |
552 | ||
686fbd31 | 553 | if (f->filetype == FIO_TYPE_BLOCK) { |
53cdc686 | 554 | if (ioctl(f->fd, BLKSSZGET, &bs) < 0) { |
e1161c32 | 555 | td_verror(td, errno, "ioctl"); |
b5af8293 | 556 | return 1; |
2866c82d | 557 | } |
af52b345 | 558 | } else if (f->filetype == FIO_TYPE_CHAR) { |
b5af8293 | 559 | int version, ret; |
2866c82d | 560 | |
53cdc686 | 561 | if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) { |
e1161c32 | 562 | td_verror(td, errno, "ioctl"); |
b5af8293 | 563 | return 1; |
2866c82d JA |
564 | } |
565 | ||
5ad7be56 KD |
566 | ret = fio_sgio_read_capacity(td, &bs, &max_lba); |
567 | if (ret) { | |
568 | td_verror(td, td->error, "fio_sgio_read_capacity"); | |
569 | log_err("ioengine sg unable to read capacity successfully\n"); | |
b5af8293 | 570 | return 1; |
5ad7be56 | 571 | } |
2866c82d | 572 | } else { |
16ada754 | 573 | td_verror(td, EINVAL, "wrong file type"); |
30dac136 | 574 | log_err("ioengine sg only works on block or character devices\n"); |
b5af8293 | 575 | return 1; |
2866c82d JA |
576 | } |
577 | ||
578 | sd->bs = bs; | |
5ad7be56 | 579 | // Determine size of commands needed based on max_lba |
166c6b42 TK |
580 | if (max_lba >= MAX_10B_LBA) { |
581 | dprint(FD_IO, "sgio_type_check: using 16 byte read/write " | |
582 | "commands for lba above 0x%016llx/0x%016llx\n", | |
583 | MAX_10B_LBA, max_lba); | |
5ad7be56 KD |
584 | } |
585 | ||
686fbd31 | 586 | if (f->filetype == FIO_TYPE_BLOCK) { |
36167d82 JA |
587 | td->io_ops->getevents = NULL; |
588 | td->io_ops->event = NULL; | |
589 | } | |
5ad7be56 | 590 | sd->type_checked = 1; |
2866c82d | 591 | |
2866c82d | 592 | return 0; |
b5af8293 JA |
593 | } |
594 | ||
595 | static int fio_sgio_open(struct thread_data *td, struct fio_file *f) | |
596 | { | |
565e784d | 597 | struct sgio_data *sd = td->io_ops_data; |
b5af8293 JA |
598 | int ret; |
599 | ||
600 | ret = generic_open_file(td, f); | |
601 | if (ret) | |
602 | return ret; | |
603 | ||
15ba640a | 604 | if (sd && !sd->type_checked && fio_sgio_type_check(td, f)) { |
6977bcd0 | 605 | ret = generic_close_file(td, f); |
b5af8293 JA |
606 | return 1; |
607 | } | |
608 | ||
609 | return 0; | |
2866c82d JA |
610 | } |
611 | ||
5ad7be56 KD |
612 | /* |
613 | * Build an error string with details about the driver, host or scsi | |
614 | * error contained in the sg header Caller will use as necessary. | |
615 | */ | |
616 | static char *fio_sgio_errdetails(struct io_u *io_u) | |
617 | { | |
618 | struct sg_io_hdr *hdr = &io_u->hdr; | |
619 | #define MAXERRDETAIL 1024 | |
620 | #define MAXMSGCHUNK 128 | |
fd04fa03 | 621 | char *msg, msgchunk[MAXMSGCHUNK]; |
5ad7be56 KD |
622 | int i; |
623 | ||
efa72f25 | 624 | msg = calloc(1, MAXERRDETAIL); |
fd04fa03 | 625 | strcpy(msg, ""); |
5ad7be56 KD |
626 | |
627 | /* | |
628 | * can't seem to find sg_err.h, so I'll just echo the define values | |
629 | * so others can search on internet to find clearer clues of meaning. | |
630 | */ | |
631 | if (hdr->info & SG_INFO_CHECK) { | |
5ad7be56 KD |
632 | if (hdr->host_status) { |
633 | snprintf(msgchunk, MAXMSGCHUNK, "SG Host Status: 0x%02x; ", hdr->host_status); | |
634 | strlcat(msg, msgchunk, MAXERRDETAIL); | |
635 | switch (hdr->host_status) { | |
636 | case 0x01: | |
637 | strlcat(msg, "SG_ERR_DID_NO_CONNECT", MAXERRDETAIL); | |
638 | break; | |
639 | case 0x02: | |
640 | strlcat(msg, "SG_ERR_DID_BUS_BUSY", MAXERRDETAIL); | |
641 | break; | |
642 | case 0x03: | |
643 | strlcat(msg, "SG_ERR_DID_TIME_OUT", MAXERRDETAIL); | |
644 | break; | |
645 | case 0x04: | |
646 | strlcat(msg, "SG_ERR_DID_BAD_TARGET", MAXERRDETAIL); | |
647 | break; | |
648 | case 0x05: | |
649 | strlcat(msg, "SG_ERR_DID_ABORT", MAXERRDETAIL); | |
650 | break; | |
651 | case 0x06: | |
652 | strlcat(msg, "SG_ERR_DID_PARITY", MAXERRDETAIL); | |
653 | break; | |
654 | case 0x07: | |
655 | strlcat(msg, "SG_ERR_DID_ERROR (internal error)", MAXERRDETAIL); | |
656 | break; | |
657 | case 0x08: | |
658 | strlcat(msg, "SG_ERR_DID_RESET", MAXERRDETAIL); | |
659 | break; | |
660 | case 0x09: | |
661 | strlcat(msg, "SG_ERR_DID_BAD_INTR (unexpected)", MAXERRDETAIL); | |
662 | break; | |
663 | case 0x0a: | |
664 | strlcat(msg, "SG_ERR_DID_PASSTHROUGH", MAXERRDETAIL); | |
665 | break; | |
666 | case 0x0b: | |
667 | strlcat(msg, "SG_ERR_DID_SOFT_ERROR (driver retry?)", MAXERRDETAIL); | |
668 | break; | |
669 | case 0x0c: | |
670 | strlcat(msg, "SG_ERR_DID_IMM_RETRY", MAXERRDETAIL); | |
671 | break; | |
672 | case 0x0d: | |
673 | strlcat(msg, "SG_ERR_DID_REQUEUE", MAXERRDETAIL); | |
674 | break; | |
2ce6c6e5 TK |
675 | case 0x0e: |
676 | strlcat(msg, "SG_ERR_DID_TRANSPORT_DISRUPTED", MAXERRDETAIL); | |
677 | break; | |
678 | case 0x0f: | |
679 | strlcat(msg, "SG_ERR_DID_TRANSPORT_FAILFAST", MAXERRDETAIL); | |
680 | break; | |
681 | case 0x10: | |
682 | strlcat(msg, "SG_ERR_DID_TARGET_FAILURE", MAXERRDETAIL); | |
683 | break; | |
684 | case 0x11: | |
685 | strlcat(msg, "SG_ERR_DID_NEXUS_FAILURE", MAXERRDETAIL); | |
686 | break; | |
687 | case 0x12: | |
688 | strlcat(msg, "SG_ERR_DID_ALLOC_FAILURE", MAXERRDETAIL); | |
689 | break; | |
690 | case 0x13: | |
691 | strlcat(msg, "SG_ERR_DID_MEDIUM_ERROR", MAXERRDETAIL); | |
692 | break; | |
5ad7be56 KD |
693 | default: |
694 | strlcat(msg, "Unknown", MAXERRDETAIL); | |
695 | break; | |
696 | } | |
697 | strlcat(msg, ". ", MAXERRDETAIL); | |
698 | } | |
699 | if (hdr->driver_status) { | |
700 | snprintf(msgchunk, MAXMSGCHUNK, "SG Driver Status: 0x%02x; ", hdr->driver_status); | |
701 | strlcat(msg, msgchunk, MAXERRDETAIL); | |
702 | switch (hdr->driver_status & 0x0F) { | |
703 | case 0x01: | |
704 | strlcat(msg, "SG_ERR_DRIVER_BUSY", MAXERRDETAIL); | |
705 | break; | |
706 | case 0x02: | |
707 | strlcat(msg, "SG_ERR_DRIVER_SOFT", MAXERRDETAIL); | |
708 | break; | |
709 | case 0x03: | |
710 | strlcat(msg, "SG_ERR_DRIVER_MEDIA", MAXERRDETAIL); | |
711 | break; | |
712 | case 0x04: | |
713 | strlcat(msg, "SG_ERR_DRIVER_ERROR", MAXERRDETAIL); | |
714 | break; | |
715 | case 0x05: | |
716 | strlcat(msg, "SG_ERR_DRIVER_INVALID", MAXERRDETAIL); | |
717 | break; | |
718 | case 0x06: | |
719 | strlcat(msg, "SG_ERR_DRIVER_TIMEOUT", MAXERRDETAIL); | |
720 | break; | |
721 | case 0x07: | |
722 | strlcat(msg, "SG_ERR_DRIVER_HARD", MAXERRDETAIL); | |
723 | break; | |
724 | case 0x08: | |
725 | strlcat(msg, "SG_ERR_DRIVER_SENSE", MAXERRDETAIL); | |
726 | break; | |
727 | default: | |
728 | strlcat(msg, "Unknown", MAXERRDETAIL); | |
729 | break; | |
730 | } | |
731 | strlcat(msg, "; ", MAXERRDETAIL); | |
732 | switch (hdr->driver_status & 0xF0) { | |
733 | case 0x10: | |
734 | strlcat(msg, "SG_ERR_SUGGEST_RETRY", MAXERRDETAIL); | |
735 | break; | |
736 | case 0x20: | |
737 | strlcat(msg, "SG_ERR_SUGGEST_ABORT", MAXERRDETAIL); | |
738 | break; | |
739 | case 0x30: | |
740 | strlcat(msg, "SG_ERR_SUGGEST_REMAP", MAXERRDETAIL); | |
741 | break; | |
742 | case 0x40: | |
743 | strlcat(msg, "SG_ERR_SUGGEST_DIE", MAXERRDETAIL); | |
744 | break; | |
745 | case 0x80: | |
746 | strlcat(msg, "SG_ERR_SUGGEST_SENSE", MAXERRDETAIL); | |
747 | break; | |
748 | } | |
749 | strlcat(msg, ". ", MAXERRDETAIL); | |
750 | } | |
751 | if (hdr->status) { | |
752 | snprintf(msgchunk, MAXMSGCHUNK, "SG SCSI Status: 0x%02x; ", hdr->status); | |
753 | strlcat(msg, msgchunk, MAXERRDETAIL); | |
754 | // SCSI 3 status codes | |
755 | switch (hdr->status) { | |
756 | case 0x02: | |
757 | strlcat(msg, "CHECK_CONDITION", MAXERRDETAIL); | |
758 | break; | |
759 | case 0x04: | |
760 | strlcat(msg, "CONDITION_MET", MAXERRDETAIL); | |
761 | break; | |
762 | case 0x08: | |
763 | strlcat(msg, "BUSY", MAXERRDETAIL); | |
764 | break; | |
765 | case 0x10: | |
766 | strlcat(msg, "INTERMEDIATE", MAXERRDETAIL); | |
767 | break; | |
768 | case 0x14: | |
769 | strlcat(msg, "INTERMEDIATE_CONDITION_MET", MAXERRDETAIL); | |
770 | break; | |
771 | case 0x18: | |
772 | strlcat(msg, "RESERVATION_CONFLICT", MAXERRDETAIL); | |
773 | break; | |
774 | case 0x22: | |
775 | strlcat(msg, "COMMAND_TERMINATED", MAXERRDETAIL); | |
776 | break; | |
777 | case 0x28: | |
778 | strlcat(msg, "TASK_SET_FULL", MAXERRDETAIL); | |
779 | break; | |
780 | case 0x30: | |
781 | strlcat(msg, "ACA_ACTIVE", MAXERRDETAIL); | |
782 | break; | |
783 | case 0x40: | |
784 | strlcat(msg, "TASK_ABORTED", MAXERRDETAIL); | |
785 | break; | |
786 | default: | |
787 | strlcat(msg, "Unknown", MAXERRDETAIL); | |
788 | break; | |
789 | } | |
790 | strlcat(msg, ". ", MAXERRDETAIL); | |
791 | } | |
792 | if (hdr->sb_len_wr) { | |
793 | snprintf(msgchunk, MAXMSGCHUNK, "Sense Data (%d bytes):", hdr->sb_len_wr); | |
794 | strlcat(msg, msgchunk, MAXERRDETAIL); | |
795 | for (i = 0; i < hdr->sb_len_wr; i++) { | |
796 | snprintf(msgchunk, MAXMSGCHUNK, " %02x", hdr->sbp[i]); | |
797 | strlcat(msg, msgchunk, MAXERRDETAIL); | |
798 | } | |
799 | strlcat(msg, ". ", MAXERRDETAIL); | |
800 | } | |
801 | if (hdr->resid != 0) { | |
802 | snprintf(msgchunk, MAXMSGCHUNK, "SG Driver: %d bytes out of %d not transferred. ", hdr->resid, hdr->dxfer_len); | |
803 | strlcat(msg, msgchunk, MAXERRDETAIL); | |
5ad7be56 KD |
804 | } |
805 | } | |
806 | ||
fd04fa03 TK |
807 | if (!(hdr->info & SG_INFO_CHECK) && !strlen(msg)) |
808 | strncpy(msg, "SG Driver did not report a Host, Driver or Device check", | |
809 | MAXERRDETAIL - 1); | |
5ad7be56 | 810 | |
fd04fa03 | 811 | return msg; |
5ad7be56 KD |
812 | } |
813 | ||
814 | /* | |
815 | * get max file size from read capacity. | |
816 | */ | |
817 | static int fio_sgio_get_file_size(struct thread_data *td, struct fio_file *f) | |
818 | { | |
819 | /* | |
820 | * get_file_size is being called even before sgio_init is | |
821 | * called, so none of the sg_io structures are | |
822 | * initialized in the thread_data yet. So we need to do the | |
823 | * ReadCapacity without any of those helpers. One of the effects | |
824 | * is that ReadCapacity may get called 4 times on each open: | |
825 | * readcap(10) followed by readcap(16) if needed - just to get | |
826 | * the file size after the init occurs - it will be called | |
827 | * again when "type_check" is called during structure | |
828 | * initialization I'm not sure how to prevent this little | |
829 | * inefficiency. | |
830 | */ | |
831 | unsigned int bs = 0; | |
832 | unsigned long long max_lba = 0; | |
833 | int ret; | |
834 | ||
835 | if (fio_file_size_known(f)) | |
836 | return 0; | |
837 | ||
686fbd31 | 838 | if (f->filetype != FIO_TYPE_BLOCK && f->filetype != FIO_TYPE_CHAR) { |
30dac136 TK |
839 | td_verror(td, EINVAL, "wrong file type"); |
840 | log_err("ioengine sg only works on block or character devices\n"); | |
841 | return 1; | |
842 | } | |
843 | ||
5ad7be56 KD |
844 | ret = fio_sgio_read_capacity(td, &bs, &max_lba); |
845 | if (ret ) { | |
846 | td_verror(td, td->error, "fio_sgio_read_capacity"); | |
847 | log_err("ioengine sg unable to successfully execute read capacity to get block size and maximum lba\n"); | |
848 | return 1; | |
849 | } | |
850 | ||
851 | f->real_file_size = (max_lba + 1) * bs; | |
852 | fio_file_set_size_known(f); | |
853 | return 0; | |
854 | } | |
855 | ||
856 | ||
5f350952 | 857 | static struct ioengine_ops ioengine = { |
2866c82d JA |
858 | .name = "sg", |
859 | .version = FIO_IOOPS_VERSION, | |
860 | .init = fio_sgio_init, | |
861 | .prep = fio_sgio_prep, | |
862 | .queue = fio_sgio_queue, | |
863 | .getevents = fio_sgio_getevents, | |
5ad7be56 | 864 | .errdetails = fio_sgio_errdetails, |
2866c82d JA |
865 | .event = fio_sgio_event, |
866 | .cleanup = fio_sgio_cleanup, | |
b5af8293 JA |
867 | .open_file = fio_sgio_open, |
868 | .close_file = generic_close_file, | |
fde57152 | 869 | .get_file_size = fio_sgio_get_file_size, |
b2a15192 | 870 | .flags = FIO_SYNCIO | FIO_RAWIO, |
52b81b7c KD |
871 | .options = options, |
872 | .option_struct_size = sizeof(struct sg_options) | |
2866c82d | 873 | }; |
34cfcdaf JA |
874 | |
875 | #else /* FIO_HAVE_SGIO */ | |
876 | ||
877 | /* | |
878 | * When we have a proper configure system in place, we simply wont build | |
879 | * and install this io engine. For now install a crippled version that | |
880 | * just complains and fails to load. | |
881 | */ | |
882 | static int fio_sgio_init(struct thread_data fio_unused *td) | |
883 | { | |
a3edaf76 | 884 | log_err("fio: ioengine sg not available\n"); |
34cfcdaf JA |
885 | return 1; |
886 | } | |
887 | ||
5f350952 | 888 | static struct ioengine_ops ioengine = { |
d0c70934 | 889 | .name = "sg", |
34cfcdaf JA |
890 | .version = FIO_IOOPS_VERSION, |
891 | .init = fio_sgio_init, | |
892 | }; | |
893 | ||
894 | #endif | |
5f350952 JA |
895 | |
896 | static void fio_init fio_sgio_register(void) | |
897 | { | |
898 | register_ioengine(&ioengine); | |
899 | } | |
900 | ||
901 | static void fio_exit fio_sgio_unregister(void) | |
902 | { | |
903 | unregister_ioengine(&ioengine); | |
904 | } |