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_sync(struct thread_data *td, struct fio_file fio_unused *f)
156 struct sgio_data *sd = td->io_ops->data;
157 struct sg_io_hdr *hdr;
161 io_u = __get_io_u(td);
166 sgio_hdr_init(sd, hdr, io_u, 0);
167 hdr->dxfer_direction = SG_DXFER_NONE;
171 ret = fio_sgio_doio(td, io_u, 1);
176 static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u)
178 struct sg_io_hdr *hdr = &io_u->hdr;
179 struct sgio_data *sd = td->io_ops->data;
182 if (io_u->buflen & (sd->bs - 1)) {
183 log_err("read/write not sector aligned\n");
187 sgio_hdr_init(sd, hdr, io_u, 1);
189 if (io_u->ddir == DDIR_READ) {
190 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
193 hdr->dxfer_direction = SG_DXFER_TO_DEV;
197 nr_blocks = io_u->buflen / sd->bs;
198 lba = io_u->offset / sd->bs;
199 hdr->cmdp[2] = (lba >> 24) & 0xff;
200 hdr->cmdp[3] = (lba >> 16) & 0xff;
201 hdr->cmdp[4] = (lba >> 8) & 0xff;
202 hdr->cmdp[5] = lba & 0xff;
203 hdr->cmdp[7] = (nr_blocks >> 8) & 0xff;
204 hdr->cmdp[8] = nr_blocks & 0xff;
208 static int fio_sgio_queue(struct thread_data *td, struct io_u *io_u)
210 struct sg_io_hdr *hdr = &io_u->hdr;
213 ret = fio_sgio_doio(td, io_u, 0);
217 else if (hdr->status) {
218 io_u->resid = hdr->resid;
225 static struct io_u *fio_sgio_event(struct thread_data *td, int event)
227 struct sgio_data *sd = td->io_ops->data;
229 return sd->events[event];
232 static int fio_sgio_get_bs(struct thread_data *td, unsigned int *bs)
234 struct sgio_data *sd = td->io_ops->data;
236 struct sg_io_hdr *hdr;
237 unsigned char buf[8];
240 io_u = __get_io_u(td);
244 sgio_hdr_init(sd, hdr, io_u, 0);
245 memset(buf, 0, sizeof(buf));
248 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
250 hdr->dxfer_len = sizeof(buf);
252 ret = fio_sgio_doio(td, io_u, 1);
258 *bs = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
263 static void fio_sgio_cleanup(struct thread_data *td)
265 if (td->io_ops->data) {
266 free(td->io_ops->data);
267 td->io_ops->data = NULL;
271 static int fio_sgio_init(struct thread_data *td)
273 struct fio_file *f = &td->files[0];
274 struct sgio_data *sd;
278 sd = malloc(sizeof(*sd));
279 sd->cmds = malloc(td->iodepth * sizeof(struct sgio_cmd));
280 sd->events = malloc(td->iodepth * sizeof(struct io_u *));
281 td->io_ops->data = sd;
283 if (td->filetype == FIO_TYPE_BD) {
284 if (ioctl(f->fd, BLKSSZGET, &bs) < 0) {
285 td_verror(td, errno);
288 } else if (td->filetype == FIO_TYPE_CHAR) {
291 if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) {
292 td_verror(td, errno);
296 ret = fio_sgio_get_bs(td, &bs);
300 log_err("ioengine sgio only works on block devices\n");
306 if (td->filetype == FIO_TYPE_BD)
307 td->io_ops->getevents = fio_sgio_ioctl_getevents;
309 td->io_ops->getevents = fio_sgio_getevents;
312 * we want to do it, regardless of whether odirect is set or not
314 td->override_sync = 1;
318 struct ioengine_ops ioengine = {
320 .version = FIO_IOOPS_VERSION,
321 .init = fio_sgio_init,
322 .prep = fio_sgio_prep,
323 .queue = fio_sgio_queue,
324 .getevents = fio_sgio_getevents,
325 .event = fio_sgio_event,
326 .cleanup = fio_sgio_cleanup,
327 .sync = fio_sgio_sync,
328 .flags = FIO_SYNCIO | FIO_RAWIO,