2 * scsi generic sg v3 io engine
18 unsigned char cdb[10];
23 struct sgio_cmd *cmds;
32 static void sgio_hdr_init(struct sgio_data *sd, struct sg_io_hdr *hdr,
33 struct io_u *io_u, int fs)
35 struct sgio_cmd *sc = &sd->cmds[io_u->index];
37 memset(hdr, 0, sizeof(*hdr));
38 memset(sc->cdb, 0, sizeof(sc->cdb));
40 hdr->interface_id = 'S';
42 hdr->cmd_len = sizeof(sc->cdb);
43 hdr->pack_id = io_u->index;
47 hdr->dxferp = io_u->xfer_buf;
48 hdr->dxfer_len = io_u->xfer_buflen;
52 static int pollin_events(struct pollfd *pfds, int fds)
56 for (i = 0; i < fds; i++)
57 if (pfds[i].revents & POLLIN)
63 static int fio_sgio_getevents(struct thread_data *td, int min, int max,
64 struct timespec fio_unused *t)
67 * normally hard coding &td->files[0] is a bug that needs to be fixed,
68 * but it's ok here as all files should point to the same device.
70 struct fio_file *f = &td->files[0];
71 struct sgio_data *sd = td->io_ops->data;
72 int left = max, ret, r = 0;
73 void *buf = sd->sgbuf;
74 unsigned int i, events;
77 * Fill in the file descriptors
79 for_each_file(td, f, i) {
81 * don't block for min events == 0
84 sd->fd_flags[i] = fcntl(f->fd, F_GETFL);
85 fcntl(f->fd, F_SETFL, sd->fd_flags[i] | O_NONBLOCK);
87 sd->pfds[i].fd = f->fd;
88 sd->pfds[i].events = POLLIN;
98 ret = poll(sd->pfds, td->nr_files, -1);
102 td_verror(td, errno, "poll");
107 if (pollin_events(sd->pfds, td->nr_files))
117 for_each_file(td, f, i) {
118 ret = read(f->fd, p, left * sizeof(struct sg_io_hdr));
123 td_verror(td, errno, "read");
127 events += ret / sizeof(struct sg_io_hdr);
141 for (i = 0; i < events; i++) {
142 struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i;
144 sd->events[i] = hdr->usr_ptr;
149 for_each_file(td, f, i)
150 fcntl(f->fd, F_SETFL, sd->fd_flags[i]);
156 static int fio_sgio_ioctl_doio(struct thread_data *td,
157 struct fio_file *f, struct io_u *io_u)
159 struct sgio_data *sd = td->io_ops->data;
160 struct sg_io_hdr *hdr = &io_u->hdr;
163 sd->events[0] = io_u;
165 ret = ioctl(f->fd, SG_IO, hdr);
169 return FIO_Q_COMPLETED;
172 static int fio_sgio_rw_doio(struct fio_file *f, struct io_u *io_u, int sync)
174 struct sg_io_hdr *hdr = &io_u->hdr;
177 ret = write(f->fd, hdr, sizeof(*hdr));
182 ret = read(f->fd, hdr, sizeof(*hdr));
185 return FIO_Q_COMPLETED;
191 static int fio_sgio_doio(struct thread_data *td, struct io_u *io_u, int sync)
193 struct fio_file *f = io_u->file;
195 if (f->filetype == FIO_TYPE_BD)
196 return fio_sgio_ioctl_doio(td, f, io_u);
198 return fio_sgio_rw_doio(f, io_u, sync);
201 static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u)
203 struct sg_io_hdr *hdr = &io_u->hdr;
204 struct sgio_data *sd = td->io_ops->data;
207 if (io_u->xfer_buflen & (sd->bs - 1)) {
208 log_err("read/write not sector aligned\n");
212 if (io_u->ddir == DDIR_READ) {
213 sgio_hdr_init(sd, hdr, io_u, 1);
215 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
217 } else if (io_u->ddir == DDIR_WRITE) {
218 sgio_hdr_init(sd, hdr, io_u, 1);
220 hdr->dxfer_direction = SG_DXFER_TO_DEV;
223 sgio_hdr_init(sd, hdr, io_u, 0);
225 hdr->dxfer_direction = SG_DXFER_NONE;
229 if (hdr->dxfer_direction != SG_DXFER_NONE) {
230 nr_blocks = io_u->xfer_buflen / sd->bs;
231 lba = io_u->offset / sd->bs;
232 hdr->cmdp[2] = (unsigned char) ((lba >> 24) & 0xff);
233 hdr->cmdp[3] = (unsigned char) ((lba >> 16) & 0xff);
234 hdr->cmdp[4] = (unsigned char) ((lba >> 8) & 0xff);
235 hdr->cmdp[5] = (unsigned char) (lba & 0xff);
236 hdr->cmdp[7] = (unsigned char) ((nr_blocks >> 8) & 0xff);
237 hdr->cmdp[8] = (unsigned char) (nr_blocks & 0xff);
243 static int fio_sgio_queue(struct thread_data *td, struct io_u *io_u)
245 struct sg_io_hdr *hdr = &io_u->hdr;
248 ret = fio_sgio_doio(td, io_u, io_u->ddir == DDIR_SYNC);
252 else if (hdr->status) {
253 io_u->resid = hdr->resid;
258 td_verror(td, io_u->error, "xfer");
259 return FIO_Q_COMPLETED;
265 static struct io_u *fio_sgio_event(struct thread_data *td, int event)
267 struct sgio_data *sd = td->io_ops->data;
269 return sd->events[event];
272 static int fio_sgio_get_bs(struct thread_data *td, unsigned int *bs)
274 struct sgio_data *sd = td->io_ops->data;
276 struct sg_io_hdr *hdr;
277 unsigned char buf[8];
280 io_u = __get_io_u(td);
281 io_u->file = &td->files[0];
285 sgio_hdr_init(sd, hdr, io_u, 0);
286 memset(buf, 0, sizeof(buf));
289 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
291 hdr->dxfer_len = sizeof(buf);
293 ret = fio_sgio_doio(td, io_u, 1);
299 *bs = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
304 static void fio_sgio_cleanup(struct thread_data *td)
306 struct sgio_data *sd = td->io_ops->data;
316 td->io_ops->data = NULL;
320 static int fio_sgio_init(struct thread_data *td)
322 struct sgio_data *sd;
324 sd = malloc(sizeof(*sd));
325 memset(sd, 0, sizeof(*sd));
326 sd->cmds = malloc(td->iodepth * sizeof(struct sgio_cmd));
327 memset(sd->cmds, 0, td->iodepth * sizeof(struct sgio_cmd));
328 sd->events = malloc(td->iodepth * sizeof(struct io_u *));
329 memset(sd->events, 0, td->iodepth * sizeof(struct io_u *));
330 sd->pfds = malloc(sizeof(struct pollfd) * td->nr_files);
331 memset(sd->pfds, 0, sizeof(struct pollfd) * td->nr_files);
332 sd->fd_flags = malloc(sizeof(int) * td->nr_files);
333 memset(sd->fd_flags, 0, sizeof(int) * td->nr_files);
334 sd->sgbuf = malloc(sizeof(struct sg_io_hdr) * td->iodepth);
335 memset(sd->sgbuf, 0, sizeof(struct sg_io_hdr) * td->iodepth);
337 td->io_ops->data = sd;
340 * we want to do it, regardless of whether odirect is set or not
342 td->override_sync = 1;
346 static int fio_sgio_type_check(struct thread_data *td, struct fio_file *f)
348 struct sgio_data *sd = td->io_ops->data;
351 if (f->filetype == FIO_TYPE_BD) {
352 if (ioctl(f->fd, BLKSSZGET, &bs) < 0) {
353 td_verror(td, errno, "ioctl");
356 } else if (f->filetype == FIO_TYPE_CHAR) {
359 if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) {
360 td_verror(td, errno, "ioctl");
364 ret = fio_sgio_get_bs(td, &bs);
368 log_err("ioengine sgio only works on block devices\n");
374 if (f->filetype == FIO_TYPE_BD) {
375 td->io_ops->getevents = NULL;
376 td->io_ops->event = NULL;
382 static int fio_sgio_open(struct thread_data *td, struct fio_file *f)
384 struct sgio_data *sd = td->io_ops->data;
387 ret = generic_open_file(td, f);
391 if (!sd->type_checked && fio_sgio_type_check(td, f)) {
392 generic_close_file(td, f);
399 static struct ioengine_ops ioengine = {
401 .version = FIO_IOOPS_VERSION,
402 .init = fio_sgio_init,
403 .prep = fio_sgio_prep,
404 .queue = fio_sgio_queue,
405 .getevents = fio_sgio_getevents,
406 .event = fio_sgio_event,
407 .cleanup = fio_sgio_cleanup,
408 .open_file = fio_sgio_open,
409 .close_file = generic_close_file,
410 .flags = FIO_SYNCIO | FIO_RAWIO,
413 #else /* FIO_HAVE_SGIO */
416 * When we have a proper configure system in place, we simply wont build
417 * and install this io engine. For now install a crippled version that
418 * just complains and fails to load.
420 static int fio_sgio_init(struct thread_data fio_unused *td)
422 fprintf(stderr, "fio: sgio not available\n");
426 static struct ioengine_ops ioengine = {
428 .version = FIO_IOOPS_VERSION,
429 .init = fio_sgio_init,
434 static void fio_init fio_sgio_register(void)
436 register_ioengine(&ioengine);
439 static void fio_exit fio_sgio_unregister(void)
441 unregister_ioengine(&ioengine);