2 * scsi generic sg v3 io engine
17 unsigned char cdb[10];
22 struct sgio_cmd *cmds;
27 static void sgio_hdr_init(struct sgio_data *sd, struct sg_io_hdr *hdr,
28 struct io_u *io_u, int fs)
30 struct sgio_cmd *sc = &sd->cmds[io_u->index];
32 memset(hdr, 0, sizeof(*hdr));
33 memset(sc->cdb, 0, sizeof(sc->cdb));
35 hdr->interface_id = 'S';
37 hdr->cmd_len = sizeof(sc->cdb);
38 hdr->pack_id = io_u->index;
42 hdr->dxferp = io_u->buf;
43 hdr->dxfer_len = io_u->buflen;
47 static int fio_sgio_ioctl_getevents(struct thread_data *td, int fio_unused min,
48 int max, struct timespec fio_unused *t)
53 * we can only have one finished io_u for sync io, since the depth
56 if (list_empty(&td->io_u_busylist))
63 static int fio_sgio_getevents(struct thread_data *td, int min, int max,
64 struct timespec fio_unused *t)
66 struct fio_file *f = &td->files[0];
67 struct sgio_data *sd = td->io_ops->data;
68 struct pollfd pfd = { .fd = f->fd, .events = POLLIN };
69 void *buf = malloc(max * sizeof(struct sg_io_hdr));
70 int left = max, ret, events, i, r = 0, fl = 0;
73 * don't block for !events
76 fl = fcntl(f->fd, F_GETFL);
77 fcntl(f->fd, F_SETFL, fl | O_NONBLOCK);
85 if (pfd.revents & POLLIN)
89 ret = read(f->fd, buf, left * sizeof(struct sg_io_hdr));
99 events = ret / sizeof(struct sg_io_hdr);
103 for (i = 0; i < events; i++) {
104 struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i;
106 sd->events[i] = hdr->usr_ptr;
111 fcntl(f->fd, F_SETFL, fl);
117 static int fio_sgio_ioctl_doio(struct thread_data *td,
118 struct fio_file *f, struct io_u *io_u)
120 struct sgio_data *sd = td->io_ops->data;
121 struct sg_io_hdr *hdr = &io_u->hdr;
123 sd->events[0] = io_u;
125 return ioctl(f->fd, SG_IO, hdr);
128 static int fio_sgio_rw_doio(struct fio_file *f, struct io_u *io_u, int sync)
130 struct sg_io_hdr *hdr = &io_u->hdr;
133 ret = write(f->fd, hdr, sizeof(*hdr));
138 ret = read(f->fd, hdr, sizeof(*hdr));
146 static int fio_sgio_doio(struct thread_data *td, struct io_u *io_u, int sync)
148 struct fio_file *f = io_u->file;
150 if (td->filetype == FIO_TYPE_BD)
151 return fio_sgio_ioctl_doio(td, f, io_u);
153 return fio_sgio_rw_doio(f, io_u, sync);
156 static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u)
158 struct sg_io_hdr *hdr = &io_u->hdr;
159 struct sgio_data *sd = td->io_ops->data;
162 if (io_u->buflen & (sd->bs - 1)) {
163 log_err("read/write not sector aligned\n");
167 if (io_u->ddir == DDIR_READ) {
168 sgio_hdr_init(sd, hdr, io_u, 1);
170 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
172 } else if (io_u->ddir == DDIR_WRITE) {
173 sgio_hdr_init(sd, hdr, io_u, 1);
175 hdr->dxfer_direction = SG_DXFER_TO_DEV;
178 sgio_hdr_init(sd, hdr, io_u, 0);
180 hdr->dxfer_direction = SG_DXFER_NONE;
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;
198 static int fio_sgio_queue(struct thread_data *td, struct io_u *io_u)
200 struct sg_io_hdr *hdr = &io_u->hdr;
203 ret = fio_sgio_doio(td, io_u, io_u->ddir == DDIR_SYNC);
207 else if (hdr->status) {
208 io_u->resid = hdr->resid;
215 static struct io_u *fio_sgio_event(struct thread_data *td, int event)
217 struct sgio_data *sd = td->io_ops->data;
219 return sd->events[event];
222 static int fio_sgio_get_bs(struct thread_data *td, unsigned int *bs)
224 struct sgio_data *sd = td->io_ops->data;
226 struct sg_io_hdr *hdr;
227 unsigned char buf[8];
230 io_u = __get_io_u(td);
234 sgio_hdr_init(sd, hdr, io_u, 0);
235 memset(buf, 0, sizeof(buf));
238 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
240 hdr->dxfer_len = sizeof(buf);
242 ret = fio_sgio_doio(td, io_u, 1);
248 *bs = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
253 static void fio_sgio_cleanup(struct thread_data *td)
255 if (td->io_ops->data) {
256 free(td->io_ops->data);
257 td->io_ops->data = NULL;
261 static int fio_sgio_init(struct thread_data *td)
263 struct fio_file *f = &td->files[0];
264 struct sgio_data *sd;
268 sd = malloc(sizeof(*sd));
269 sd->cmds = malloc(td->iodepth * sizeof(struct sgio_cmd));
270 sd->events = malloc(td->iodepth * sizeof(struct io_u *));
271 td->io_ops->data = sd;
273 if (td->filetype == FIO_TYPE_BD) {
274 if (ioctl(f->fd, BLKSSZGET, &bs) < 0) {
275 td_verror(td, errno);
278 } else if (td->filetype == FIO_TYPE_CHAR) {
281 if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) {
282 td_verror(td, errno);
286 ret = fio_sgio_get_bs(td, &bs);
290 log_err("ioengine sgio only works on block devices\n");
296 if (td->filetype == FIO_TYPE_BD)
297 td->io_ops->getevents = fio_sgio_ioctl_getevents;
299 td->io_ops->getevents = fio_sgio_getevents;
302 * we want to do it, regardless of whether odirect is set or not
304 td->override_sync = 1;
308 struct ioengine_ops ioengine = {
310 .version = FIO_IOOPS_VERSION,
311 .init = fio_sgio_init,
312 .prep = fio_sgio_prep,
313 .queue = fio_sgio_queue,
314 .getevents = fio_sgio_getevents,
315 .event = fio_sgio_event,
316 .cleanup = fio_sgio_cleanup,
317 .flags = FIO_SYNCIO | FIO_RAWIO,
320 #else /* FIO_HAVE_SGIO */
323 * When we have a proper configure system in place, we simply wont build
324 * and install this io engine. For now install a crippled version that
325 * just complains and fails to load.
327 static int fio_sgio_init(struct thread_data fio_unused *td)
329 fprintf(stderr, "fio: sgio not available\n");
333 struct ioengine_ops ioengine = {
335 .version = FIO_IOOPS_VERSION,
336 .init = fio_sgio_init,