2 * scsi generic sg v3 io engine
15 unsigned char cdb[10];
20 struct sgio_cmd *cmds;
25 static void sgio_hdr_init(struct sgio_data *sd, struct sg_io_hdr *hdr,
26 struct io_u *io_u, int fs)
28 struct sgio_cmd *sc = &sd->cmds[io_u->index];
30 memset(hdr, 0, sizeof(*hdr));
31 memset(sc->cdb, 0, sizeof(sc->cdb));
33 hdr->interface_id = 'S';
35 hdr->cmd_len = sizeof(sc->cdb);
36 hdr->pack_id = io_u->index;
40 hdr->dxferp = io_u->buf;
41 hdr->dxfer_len = io_u->buflen;
45 static int fio_sgio_ioctl_getevents(struct thread_data *td, int fio_unused min,
46 int max, struct timespec fio_unused *t)
51 * we can only have one finished io_u for sync io, since the depth
54 if (list_empty(&td->io_u_busylist))
61 static int fio_sgio_getevents(struct thread_data *td, int min, int max,
62 struct timespec fio_unused *t)
64 struct fio_file *f = &td->files[0];
65 struct sgio_data *sd = td->io_ops->data;
66 struct pollfd pfd = { .fd = f->fd, .events = POLLIN };
67 void *buf = malloc(max * sizeof(struct sg_io_hdr));
68 int left = max, ret, events, i, r = 0, fl = 0;
71 * don't block for !events
74 fl = fcntl(f->fd, F_GETFL);
75 fcntl(f->fd, F_SETFL, fl | O_NONBLOCK);
83 if (pfd.revents & POLLIN)
87 ret = read(f->fd, buf, left * sizeof(struct sg_io_hdr));
97 events = ret / sizeof(struct sg_io_hdr);
101 for (i = 0; i < events; i++) {
102 struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i;
104 sd->events[i] = hdr->usr_ptr;
109 fcntl(f->fd, F_SETFL, fl);
115 static int fio_sgio_ioctl_doio(struct thread_data *td,
116 struct fio_file *f, struct io_u *io_u)
118 struct sgio_data *sd = td->io_ops->data;
119 struct sg_io_hdr *hdr = &io_u->hdr;
121 sd->events[0] = io_u;
123 return ioctl(f->fd, SG_IO, hdr);
126 static int fio_sgio_rw_doio(struct fio_file *f, struct io_u *io_u, int sync)
128 struct sg_io_hdr *hdr = &io_u->hdr;
131 ret = write(f->fd, hdr, sizeof(*hdr));
136 ret = read(f->fd, hdr, sizeof(*hdr));
144 static int fio_sgio_doio(struct thread_data *td, struct io_u *io_u, int sync)
146 struct fio_file *f = io_u->file;
148 if (td->filetype == FIO_TYPE_BD)
149 return fio_sgio_ioctl_doio(td, f, io_u);
151 return fio_sgio_rw_doio(f, io_u, sync);
154 static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u)
156 struct sg_io_hdr *hdr = &io_u->hdr;
157 struct sgio_data *sd = td->io_ops->data;
160 if (io_u->buflen & (sd->bs - 1)) {
161 log_err("read/write not sector aligned\n");
165 if (io_u->ddir == DDIR_READ) {
166 sgio_hdr_init(sd, hdr, io_u, 1);
168 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
170 } else if (io_u->ddir == DDIR_WRITE) {
171 sgio_hdr_init(sd, hdr, io_u, 1);
173 hdr->dxfer_direction = SG_DXFER_TO_DEV;
176 sgio_hdr_init(sd, hdr, io_u, 0);
178 hdr->dxfer_direction = SG_DXFER_NONE;
182 if (hdr->dxfer_direction != SG_DXFER_NONE) {
183 nr_blocks = io_u->buflen / sd->bs;
184 lba = io_u->offset / sd->bs;
185 hdr->cmdp[2] = (lba >> 24) & 0xff;
186 hdr->cmdp[3] = (lba >> 16) & 0xff;
187 hdr->cmdp[4] = (lba >> 8) & 0xff;
188 hdr->cmdp[5] = lba & 0xff;
189 hdr->cmdp[7] = (nr_blocks >> 8) & 0xff;
190 hdr->cmdp[8] = nr_blocks & 0xff;
196 static int fio_sgio_queue(struct thread_data *td, struct io_u *io_u)
198 struct sg_io_hdr *hdr = &io_u->hdr;
201 ret = fio_sgio_doio(td, io_u, io_u->ddir == DDIR_SYNC);
205 else if (hdr->status) {
206 io_u->resid = hdr->resid;
213 static struct io_u *fio_sgio_event(struct thread_data *td, int event)
215 struct sgio_data *sd = td->io_ops->data;
217 return sd->events[event];
220 static int fio_sgio_get_bs(struct thread_data *td, unsigned int *bs)
222 struct sgio_data *sd = td->io_ops->data;
224 struct sg_io_hdr *hdr;
225 unsigned char buf[8];
228 io_u = __get_io_u(td);
232 sgio_hdr_init(sd, hdr, io_u, 0);
233 memset(buf, 0, sizeof(buf));
236 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
238 hdr->dxfer_len = sizeof(buf);
240 ret = fio_sgio_doio(td, io_u, 1);
246 *bs = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
251 static void fio_sgio_cleanup(struct thread_data *td)
253 if (td->io_ops->data) {
254 free(td->io_ops->data);
255 td->io_ops->data = NULL;
259 static int fio_sgio_init(struct thread_data *td)
261 struct fio_file *f = &td->files[0];
262 struct sgio_data *sd;
266 sd = malloc(sizeof(*sd));
267 sd->cmds = malloc(td->iodepth * sizeof(struct sgio_cmd));
268 sd->events = malloc(td->iodepth * sizeof(struct io_u *));
269 td->io_ops->data = sd;
271 if (td->filetype == FIO_TYPE_BD) {
272 if (ioctl(f->fd, BLKSSZGET, &bs) < 0) {
273 td_verror(td, errno);
276 } else if (td->filetype == FIO_TYPE_CHAR) {
279 if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) {
280 td_verror(td, errno);
284 ret = fio_sgio_get_bs(td, &bs);
288 log_err("ioengine sgio only works on block devices\n");
294 if (td->filetype == FIO_TYPE_BD)
295 td->io_ops->getevents = fio_sgio_ioctl_getevents;
297 td->io_ops->getevents = fio_sgio_getevents;
300 * we want to do it, regardless of whether odirect is set or not
302 td->override_sync = 1;
306 struct ioengine_ops ioengine = {
308 .version = FIO_IOOPS_VERSION,
309 .init = fio_sgio_init,
310 .prep = fio_sgio_prep,
311 .queue = fio_sgio_queue,
312 .getevents = fio_sgio_getevents,
313 .event = fio_sgio_event,
314 .cleanup = fio_sgio_cleanup,
315 .flags = FIO_SYNCIO | FIO_RAWIO,