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 sgio_data *sd = td->io_ops->data;
65 struct pollfd pfd = { .fd = td->fd, .events = POLLIN };
66 void *buf = malloc(max * sizeof(struct sg_io_hdr));
67 int left = max, ret, events, i, r = 0, fl = 0;
70 * don't block for !events
73 fl = fcntl(td->fd, F_GETFL);
74 fcntl(td->fd, F_SETFL, fl | O_NONBLOCK);
82 if (pfd.revents & POLLIN)
86 ret = read(td->fd, buf, left * sizeof(struct sg_io_hdr));
96 events = ret / sizeof(struct sg_io_hdr);
100 for (i = 0; i < events; i++) {
101 struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i;
103 sd->events[i] = hdr->usr_ptr;
108 fcntl(td->fd, F_SETFL, fl);
114 static int fio_sgio_ioctl_doio(struct thread_data *td, struct io_u *io_u)
116 struct sgio_data *sd = td->io_ops->data;
117 struct sg_io_hdr *hdr = &io_u->hdr;
119 sd->events[0] = io_u;
121 return ioctl(td->fd, SG_IO, hdr);
124 static int fio_sgio_rw_doio(struct thread_data *td, struct io_u *io_u, int sync)
126 struct sg_io_hdr *hdr = &io_u->hdr;
129 ret = write(td->fd, hdr, sizeof(*hdr));
134 ret = read(td->fd, hdr, sizeof(*hdr));
142 static int fio_sgio_doio(struct thread_data *td, struct io_u *io_u, int sync)
144 if (td->filetype == FIO_TYPE_BD)
145 return fio_sgio_ioctl_doio(td, io_u);
147 return fio_sgio_rw_doio(td, io_u, sync);
150 static int fio_sgio_sync(struct thread_data *td)
152 struct sgio_data *sd = td->io_ops->data;
153 struct sg_io_hdr *hdr;
157 io_u = __get_io_u(td);
162 sgio_hdr_init(sd, hdr, io_u, 0);
163 hdr->dxfer_direction = SG_DXFER_NONE;
167 ret = fio_sgio_doio(td, io_u, 1);
172 static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u)
174 struct sg_io_hdr *hdr = &io_u->hdr;
175 struct sgio_data *sd = td->io_ops->data;
178 if (io_u->buflen & (sd->bs - 1)) {
179 log_err("read/write not sector aligned\n");
183 sgio_hdr_init(sd, hdr, io_u, 1);
185 if (io_u->ddir == DDIR_READ) {
186 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
189 hdr->dxfer_direction = SG_DXFER_TO_DEV;
193 nr_blocks = io_u->buflen / sd->bs;
194 lba = io_u->offset / sd->bs;
195 hdr->cmdp[2] = (lba >> 24) & 0xff;
196 hdr->cmdp[3] = (lba >> 16) & 0xff;
197 hdr->cmdp[4] = (lba >> 8) & 0xff;
198 hdr->cmdp[5] = lba & 0xff;
199 hdr->cmdp[7] = (nr_blocks >> 8) & 0xff;
200 hdr->cmdp[8] = nr_blocks & 0xff;
204 static int fio_sgio_queue(struct thread_data *td, struct io_u *io_u)
206 struct sg_io_hdr *hdr = &io_u->hdr;
209 ret = fio_sgio_doio(td, io_u, 0);
213 else if (hdr->status) {
214 io_u->resid = hdr->resid;
221 static struct io_u *fio_sgio_event(struct thread_data *td, int event)
223 struct sgio_data *sd = td->io_ops->data;
225 return sd->events[event];
228 static int fio_sgio_get_bs(struct thread_data *td, unsigned int *bs)
230 struct sgio_data *sd = td->io_ops->data;
232 struct sg_io_hdr *hdr;
233 unsigned char buf[8];
236 io_u = __get_io_u(td);
240 sgio_hdr_init(sd, hdr, io_u, 0);
241 memset(buf, 0, sizeof(buf));
244 hdr->dxfer_direction = SG_DXFER_FROM_DEV;
246 hdr->dxfer_len = sizeof(buf);
248 ret = fio_sgio_doio(td, io_u, 1);
254 *bs = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
259 static void fio_sgio_cleanup(struct thread_data *td)
261 if (td->io_ops->data) {
262 free(td->io_ops->data);
263 td->io_ops->data = NULL;
267 static int fio_sgio_init(struct thread_data *td)
269 struct sgio_data *sd;
273 sd = malloc(sizeof(*sd));
274 sd->cmds = malloc(td->iodepth * sizeof(struct sgio_cmd));
275 sd->events = malloc(td->iodepth * sizeof(struct io_u *));
276 td->io_ops->data = sd;
278 if (td->filetype == FIO_TYPE_BD) {
279 if (ioctl(td->fd, BLKSSZGET, &bs) < 0) {
280 td_verror(td, errno);
283 } else if (td->filetype == FIO_TYPE_CHAR) {
286 if (ioctl(td->fd, SG_GET_VERSION_NUM, &version) < 0) {
287 td_verror(td, errno);
291 ret = fio_sgio_get_bs(td, &bs);
295 log_err("ioengine sgio only works on block devices\n");
301 if (td->filetype == FIO_TYPE_BD)
302 td->io_ops->getevents = fio_sgio_ioctl_getevents;
304 td->io_ops->getevents = fio_sgio_getevents;
307 * we want to do it, regardless of whether odirect is set or not
309 td->override_sync = 1;
313 struct ioengine_ops ioengine = {
315 .version = FIO_IOOPS_VERSION,
316 .init = fio_sgio_init,
317 .prep = fio_sgio_prep,
318 .queue = fio_sgio_queue,
319 .getevents = fio_sgio_getevents,
320 .event = fio_sgio_event,
321 .cleanup = fio_sgio_cleanup,
322 .sync = fio_sgio_sync,