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