Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 1 | /* |
| 2 | * scsi generic sg v3 io engine |
| 3 | * |
| 4 | */ |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <unistd.h> |
| 8 | #include <errno.h> |
| 9 | #include <assert.h> |
| 10 | #include <sys/poll.h> |
| 11 | #include "fio.h" |
| 12 | #include "os.h" |
| 13 | |
Jens Axboe | 34cfcda | 2006-11-03 14:00:45 +0100 | [diff] [blame] | 14 | #ifdef FIO_HAVE_SGIO |
| 15 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 16 | struct sgio_cmd { |
| 17 | unsigned char cdb[10]; |
| 18 | int nr; |
| 19 | }; |
| 20 | |
| 21 | struct sgio_data { |
| 22 | struct sgio_cmd *cmds; |
| 23 | struct io_u **events; |
| 24 | unsigned int bs; |
| 25 | }; |
| 26 | |
| 27 | static void sgio_hdr_init(struct sgio_data *sd, struct sg_io_hdr *hdr, |
| 28 | struct io_u *io_u, int fs) |
| 29 | { |
| 30 | struct sgio_cmd *sc = &sd->cmds[io_u->index]; |
| 31 | |
| 32 | memset(hdr, 0, sizeof(*hdr)); |
| 33 | memset(sc->cdb, 0, sizeof(sc->cdb)); |
| 34 | |
| 35 | hdr->interface_id = 'S'; |
| 36 | hdr->cmdp = sc->cdb; |
| 37 | hdr->cmd_len = sizeof(sc->cdb); |
| 38 | hdr->pack_id = io_u->index; |
| 39 | hdr->usr_ptr = io_u; |
| 40 | |
| 41 | if (fs) { |
| 42 | hdr->dxferp = io_u->buf; |
| 43 | hdr->dxfer_len = io_u->buflen; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | static int fio_sgio_ioctl_getevents(struct thread_data *td, int fio_unused min, |
| 48 | int max, struct timespec fio_unused *t) |
| 49 | { |
| 50 | assert(max <= 1); |
| 51 | |
| 52 | /* |
| 53 | * we can only have one finished io_u for sync io, since the depth |
| 54 | * is always 1 |
| 55 | */ |
| 56 | if (list_empty(&td->io_u_busylist)) |
| 57 | return 0; |
| 58 | |
| 59 | return 1; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | static int fio_sgio_getevents(struct thread_data *td, int min, int max, |
| 64 | struct timespec fio_unused *t) |
| 65 | { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 66 | struct fio_file *f = &td->files[0]; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 67 | struct sgio_data *sd = td->io_ops->data; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 68 | struct pollfd pfd = { .fd = f->fd, .events = POLLIN }; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 69 | void *buf = malloc(max * sizeof(struct sg_io_hdr)); |
| 70 | int left = max, ret, events, i, r = 0, fl = 0; |
| 71 | |
| 72 | /* |
| 73 | * don't block for !events |
| 74 | */ |
| 75 | if (!min) { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 76 | fl = fcntl(f->fd, F_GETFL); |
| 77 | fcntl(f->fd, F_SETFL, fl | O_NONBLOCK); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | while (left) { |
| 81 | do { |
| 82 | if (!min) |
| 83 | break; |
| 84 | poll(&pfd, 1, -1); |
| 85 | if (pfd.revents & POLLIN) |
| 86 | break; |
| 87 | } while (1); |
| 88 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 89 | ret = read(f->fd, buf, left * sizeof(struct sg_io_hdr)); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 90 | if (ret < 0) { |
| 91 | if (errno == EAGAIN) |
| 92 | break; |
| 93 | td_verror(td, errno); |
| 94 | r = -1; |
| 95 | break; |
| 96 | } else if (!ret) |
| 97 | break; |
| 98 | |
| 99 | events = ret / sizeof(struct sg_io_hdr); |
| 100 | left -= events; |
| 101 | r += events; |
| 102 | |
| 103 | for (i = 0; i < events; i++) { |
| 104 | struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i; |
| 105 | |
| 106 | sd->events[i] = hdr->usr_ptr; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if (!min) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 111 | fcntl(f->fd, F_SETFL, fl); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 112 | |
| 113 | free(buf); |
| 114 | return r; |
| 115 | } |
| 116 | |
Jens Axboe | 7a16dd0 | 2006-10-18 17:21:58 +0200 | [diff] [blame] | 117 | static int fio_sgio_ioctl_doio(struct thread_data *td, |
| 118 | struct fio_file *f, struct io_u *io_u) |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 119 | { |
| 120 | struct sgio_data *sd = td->io_ops->data; |
| 121 | struct sg_io_hdr *hdr = &io_u->hdr; |
| 122 | |
| 123 | sd->events[0] = io_u; |
| 124 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 125 | return ioctl(f->fd, SG_IO, hdr); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 126 | } |
| 127 | |
Jens Axboe | 7a16dd0 | 2006-10-18 17:21:58 +0200 | [diff] [blame] | 128 | static int fio_sgio_rw_doio(struct fio_file *f, struct io_u *io_u, int sync) |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 129 | { |
| 130 | struct sg_io_hdr *hdr = &io_u->hdr; |
| 131 | int ret; |
| 132 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 133 | ret = write(f->fd, hdr, sizeof(*hdr)); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 134 | if (ret < 0) |
| 135 | return errno; |
| 136 | |
| 137 | if (sync) { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 138 | ret = read(f->fd, hdr, sizeof(*hdr)); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 139 | if (ret < 0) |
| 140 | return errno; |
| 141 | } |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static int fio_sgio_doio(struct thread_data *td, struct io_u *io_u, int sync) |
| 147 | { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 148 | struct fio_file *f = io_u->file; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 149 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 150 | if (td->filetype == FIO_TYPE_BD) |
| 151 | return fio_sgio_ioctl_doio(td, f, io_u); |
| 152 | |
Jens Axboe | 7a16dd0 | 2006-10-18 17:21:58 +0200 | [diff] [blame] | 153 | return fio_sgio_rw_doio(f, io_u, sync); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 156 | static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u) |
| 157 | { |
| 158 | struct sg_io_hdr *hdr = &io_u->hdr; |
| 159 | struct sgio_data *sd = td->io_ops->data; |
| 160 | int nr_blocks, lba; |
| 161 | |
| 162 | if (io_u->buflen & (sd->bs - 1)) { |
| 163 | log_err("read/write not sector aligned\n"); |
| 164 | return EINVAL; |
| 165 | } |
| 166 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 167 | if (io_u->ddir == DDIR_READ) { |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 168 | sgio_hdr_init(sd, hdr, io_u, 1); |
| 169 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 170 | hdr->dxfer_direction = SG_DXFER_FROM_DEV; |
| 171 | hdr->cmdp[0] = 0x28; |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 172 | } else if (io_u->ddir == DDIR_WRITE) { |
| 173 | sgio_hdr_init(sd, hdr, io_u, 1); |
| 174 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 175 | hdr->dxfer_direction = SG_DXFER_TO_DEV; |
| 176 | hdr->cmdp[0] = 0x2a; |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 177 | } else { |
| 178 | sgio_hdr_init(sd, hdr, io_u, 0); |
| 179 | |
| 180 | hdr->dxfer_direction = SG_DXFER_NONE; |
| 181 | hdr->cmdp[0] = 0x35; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 182 | } |
| 183 | |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 184 | if (hdr->dxfer_direction != SG_DXFER_NONE) { |
| 185 | nr_blocks = io_u->buflen / sd->bs; |
| 186 | lba = io_u->offset / sd->bs; |
| 187 | hdr->cmdp[2] = (lba >> 24) & 0xff; |
| 188 | hdr->cmdp[3] = (lba >> 16) & 0xff; |
| 189 | hdr->cmdp[4] = (lba >> 8) & 0xff; |
| 190 | hdr->cmdp[5] = lba & 0xff; |
| 191 | hdr->cmdp[7] = (nr_blocks >> 8) & 0xff; |
| 192 | hdr->cmdp[8] = nr_blocks & 0xff; |
| 193 | } |
| 194 | |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | static int fio_sgio_queue(struct thread_data *td, struct io_u *io_u) |
| 199 | { |
| 200 | struct sg_io_hdr *hdr = &io_u->hdr; |
| 201 | int ret; |
| 202 | |
Jens Axboe | 87dc1ab | 2006-10-24 14:41:26 +0200 | [diff] [blame] | 203 | ret = fio_sgio_doio(td, io_u, io_u->ddir == DDIR_SYNC); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 204 | |
| 205 | if (ret < 0) |
| 206 | io_u->error = errno; |
| 207 | else if (hdr->status) { |
| 208 | io_u->resid = hdr->resid; |
| 209 | io_u->error = EIO; |
| 210 | } |
| 211 | |
| 212 | return io_u->error; |
| 213 | } |
| 214 | |
| 215 | static struct io_u *fio_sgio_event(struct thread_data *td, int event) |
| 216 | { |
| 217 | struct sgio_data *sd = td->io_ops->data; |
| 218 | |
| 219 | return sd->events[event]; |
| 220 | } |
| 221 | |
| 222 | static int fio_sgio_get_bs(struct thread_data *td, unsigned int *bs) |
| 223 | { |
| 224 | struct sgio_data *sd = td->io_ops->data; |
| 225 | struct io_u *io_u; |
| 226 | struct sg_io_hdr *hdr; |
| 227 | unsigned char buf[8]; |
| 228 | int ret; |
| 229 | |
| 230 | io_u = __get_io_u(td); |
| 231 | assert(io_u); |
| 232 | |
| 233 | hdr = &io_u->hdr; |
| 234 | sgio_hdr_init(sd, hdr, io_u, 0); |
| 235 | memset(buf, 0, sizeof(buf)); |
| 236 | |
| 237 | hdr->cmdp[0] = 0x25; |
| 238 | hdr->dxfer_direction = SG_DXFER_FROM_DEV; |
| 239 | hdr->dxferp = buf; |
| 240 | hdr->dxfer_len = sizeof(buf); |
| 241 | |
| 242 | ret = fio_sgio_doio(td, io_u, 1); |
| 243 | if (ret) { |
| 244 | put_io_u(td, io_u); |
| 245 | return ret; |
| 246 | } |
| 247 | |
| 248 | *bs = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7]; |
| 249 | put_io_u(td, io_u); |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static void fio_sgio_cleanup(struct thread_data *td) |
| 254 | { |
| 255 | if (td->io_ops->data) { |
| 256 | free(td->io_ops->data); |
| 257 | td->io_ops->data = NULL; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | static int fio_sgio_init(struct thread_data *td) |
| 262 | { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 263 | struct fio_file *f = &td->files[0]; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 264 | struct sgio_data *sd; |
| 265 | unsigned int bs; |
| 266 | int ret; |
| 267 | |
| 268 | sd = malloc(sizeof(*sd)); |
Jens Axboe | cb781c7 | 2006-11-07 14:02:48 +0100 | [diff] [blame^] | 269 | memset(sd, 0, sizeof(*sd)); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 270 | sd->cmds = malloc(td->iodepth * sizeof(struct sgio_cmd)); |
Jens Axboe | cb781c7 | 2006-11-07 14:02:48 +0100 | [diff] [blame^] | 271 | memset(sd->cmds, 0, td->iodepth * sizeof(struct sgio_cmd)); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 272 | sd->events = malloc(td->iodepth * sizeof(struct io_u *)); |
Jens Axboe | cb781c7 | 2006-11-07 14:02:48 +0100 | [diff] [blame^] | 273 | memset(sd->events, 0, td->iodepth * sizeof(struct io_u *)); |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 274 | td->io_ops->data = sd; |
| 275 | |
| 276 | if (td->filetype == FIO_TYPE_BD) { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 277 | if (ioctl(f->fd, BLKSSZGET, &bs) < 0) { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 278 | td_verror(td, errno); |
Jens Axboe | cb781c7 | 2006-11-07 14:02:48 +0100 | [diff] [blame^] | 279 | goto err; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 280 | } |
| 281 | } else if (td->filetype == FIO_TYPE_CHAR) { |
| 282 | int version; |
| 283 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 284 | if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) { |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 285 | td_verror(td, errno); |
Jens Axboe | cb781c7 | 2006-11-07 14:02:48 +0100 | [diff] [blame^] | 286 | goto err; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | ret = fio_sgio_get_bs(td, &bs); |
| 290 | if (ret) |
Jens Axboe | cb781c7 | 2006-11-07 14:02:48 +0100 | [diff] [blame^] | 291 | goto err; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 292 | } else { |
| 293 | log_err("ioengine sgio only works on block devices\n"); |
Jens Axboe | cb781c7 | 2006-11-07 14:02:48 +0100 | [diff] [blame^] | 294 | goto err; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | sd->bs = bs; |
| 298 | |
| 299 | if (td->filetype == FIO_TYPE_BD) |
| 300 | td->io_ops->getevents = fio_sgio_ioctl_getevents; |
| 301 | else |
| 302 | td->io_ops->getevents = fio_sgio_getevents; |
| 303 | |
| 304 | /* |
| 305 | * we want to do it, regardless of whether odirect is set or not |
| 306 | */ |
| 307 | td->override_sync = 1; |
| 308 | return 0; |
Jens Axboe | cb781c7 | 2006-11-07 14:02:48 +0100 | [diff] [blame^] | 309 | err: |
| 310 | free(sd->events); |
| 311 | free(sd->cmds); |
| 312 | free(sd); |
| 313 | return 1; |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | struct ioengine_ops ioengine = { |
| 317 | .name = "sg", |
| 318 | .version = FIO_IOOPS_VERSION, |
| 319 | .init = fio_sgio_init, |
| 320 | .prep = fio_sgio_prep, |
| 321 | .queue = fio_sgio_queue, |
| 322 | .getevents = fio_sgio_getevents, |
| 323 | .event = fio_sgio_event, |
| 324 | .cleanup = fio_sgio_cleanup, |
Jens Axboe | b2a1519 | 2006-10-18 14:10:42 +0200 | [diff] [blame] | 325 | .flags = FIO_SYNCIO | FIO_RAWIO, |
Jens Axboe | 2866c82 | 2006-10-09 15:57:48 +0200 | [diff] [blame] | 326 | }; |
Jens Axboe | 34cfcda | 2006-11-03 14:00:45 +0100 | [diff] [blame] | 327 | |
| 328 | #else /* FIO_HAVE_SGIO */ |
| 329 | |
| 330 | /* |
| 331 | * When we have a proper configure system in place, we simply wont build |
| 332 | * and install this io engine. For now install a crippled version that |
| 333 | * just complains and fails to load. |
| 334 | */ |
| 335 | static int fio_sgio_init(struct thread_data fio_unused *td) |
| 336 | { |
| 337 | fprintf(stderr, "fio: sgio not available\n"); |
| 338 | return 1; |
| 339 | } |
| 340 | |
| 341 | struct ioengine_ops ioengine = { |
| 342 | .name = "sgio", |
| 343 | .version = FIO_IOOPS_VERSION, |
| 344 | .init = fio_sgio_init, |
| 345 | }; |
| 346 | |
| 347 | #endif |