Commit | Line | Data |
---|---|---|
2866c82d JA |
1 | /* |
2 | * scsi generic sg v3 io engine | |
3 | * | |
4 | */ | |
5 | #include <stdio.h> | |
6 | #include <stdlib.h> | |
7 | #include <unistd.h> | |
8 | #include <errno.h> | |
9 | #include <assert.h> | |
10 | #include <sys/poll.h> | |
11 | #include "fio.h" | |
12 | #include "os.h" | |
13 | ||
14 | struct sgio_cmd { | |
15 | unsigned char cdb[10]; | |
16 | int nr; | |
17 | }; | |
18 | ||
19 | struct sgio_data { | |
20 | struct sgio_cmd *cmds; | |
21 | struct io_u **events; | |
22 | unsigned int bs; | |
23 | }; | |
24 | ||
25 | static void sgio_hdr_init(struct sgio_data *sd, struct sg_io_hdr *hdr, | |
26 | struct io_u *io_u, int fs) | |
27 | { | |
28 | struct sgio_cmd *sc = &sd->cmds[io_u->index]; | |
29 | ||
30 | memset(hdr, 0, sizeof(*hdr)); | |
31 | memset(sc->cdb, 0, sizeof(sc->cdb)); | |
32 | ||
33 | hdr->interface_id = 'S'; | |
34 | hdr->cmdp = sc->cdb; | |
35 | hdr->cmd_len = sizeof(sc->cdb); | |
36 | hdr->pack_id = io_u->index; | |
37 | hdr->usr_ptr = io_u; | |
38 | ||
39 | if (fs) { | |
40 | hdr->dxferp = io_u->buf; | |
41 | hdr->dxfer_len = io_u->buflen; | |
42 | } | |
43 | } | |
44 | ||
45 | static int fio_sgio_ioctl_getevents(struct thread_data *td, int fio_unused min, | |
46 | int max, struct timespec fio_unused *t) | |
47 | { | |
48 | assert(max <= 1); | |
49 | ||
50 | /* | |
51 | * we can only have one finished io_u for sync io, since the depth | |
52 | * is always 1 | |
53 | */ | |
54 | if (list_empty(&td->io_u_busylist)) | |
55 | return 0; | |
56 | ||
57 | return 1; | |
58 | } | |
59 | ||
60 | ||
61 | static int fio_sgio_getevents(struct thread_data *td, int min, int max, | |
62 | struct timespec fio_unused *t) | |
63 | { | |
53cdc686 | 64 | struct fio_file *f = &td->files[0]; |
2866c82d | 65 | struct sgio_data *sd = td->io_ops->data; |
53cdc686 | 66 | struct pollfd pfd = { .fd = f->fd, .events = POLLIN }; |
2866c82d JA |
67 | void *buf = malloc(max * sizeof(struct sg_io_hdr)); |
68 | int left = max, ret, events, i, r = 0, fl = 0; | |
69 | ||
70 | /* | |
71 | * don't block for !events | |
72 | */ | |
73 | if (!min) { | |
53cdc686 JA |
74 | fl = fcntl(f->fd, F_GETFL); |
75 | fcntl(f->fd, F_SETFL, fl | O_NONBLOCK); | |
2866c82d JA |
76 | } |
77 | ||
78 | while (left) { | |
79 | do { | |
80 | if (!min) | |
81 | break; | |
82 | poll(&pfd, 1, -1); | |
83 | if (pfd.revents & POLLIN) | |
84 | break; | |
85 | } while (1); | |
86 | ||
53cdc686 | 87 | ret = read(f->fd, buf, left * sizeof(struct sg_io_hdr)); |
2866c82d JA |
88 | if (ret < 0) { |
89 | if (errno == EAGAIN) | |
90 | break; | |
91 | td_verror(td, errno); | |
92 | r = -1; | |
93 | break; | |
94 | } else if (!ret) | |
95 | break; | |
96 | ||
97 | events = ret / sizeof(struct sg_io_hdr); | |
98 | left -= events; | |
99 | r += events; | |
100 | ||
101 | for (i = 0; i < events; i++) { | |
102 | struct sg_io_hdr *hdr = (struct sg_io_hdr *) buf + i; | |
103 | ||
104 | sd->events[i] = hdr->usr_ptr; | |
105 | } | |
106 | } | |
107 | ||
108 | if (!min) | |
53cdc686 | 109 | fcntl(f->fd, F_SETFL, fl); |
2866c82d JA |
110 | |
111 | free(buf); | |
112 | return r; | |
113 | } | |
114 | ||
7a16dd02 JA |
115 | static int fio_sgio_ioctl_doio(struct thread_data *td, |
116 | struct fio_file *f, struct io_u *io_u) | |
2866c82d JA |
117 | { |
118 | struct sgio_data *sd = td->io_ops->data; | |
119 | struct sg_io_hdr *hdr = &io_u->hdr; | |
120 | ||
121 | sd->events[0] = io_u; | |
122 | ||
53cdc686 | 123 | return ioctl(f->fd, SG_IO, hdr); |
2866c82d JA |
124 | } |
125 | ||
7a16dd02 | 126 | static int fio_sgio_rw_doio(struct fio_file *f, struct io_u *io_u, int sync) |
2866c82d JA |
127 | { |
128 | struct sg_io_hdr *hdr = &io_u->hdr; | |
129 | int ret; | |
130 | ||
53cdc686 | 131 | ret = write(f->fd, hdr, sizeof(*hdr)); |
2866c82d JA |
132 | if (ret < 0) |
133 | return errno; | |
134 | ||
135 | if (sync) { | |
53cdc686 | 136 | ret = read(f->fd, hdr, sizeof(*hdr)); |
2866c82d JA |
137 | if (ret < 0) |
138 | return errno; | |
139 | } | |
140 | ||
141 | return 0; | |
142 | } | |
143 | ||
144 | static int fio_sgio_doio(struct thread_data *td, struct io_u *io_u, int sync) | |
145 | { | |
53cdc686 JA |
146 | struct fio_file *f = io_u->file; |
147 | ||
2866c82d | 148 | if (td->filetype == FIO_TYPE_BD) |
53cdc686 | 149 | return fio_sgio_ioctl_doio(td, f, io_u); |
2866c82d | 150 | |
7a16dd02 | 151 | return fio_sgio_rw_doio(f, io_u, sync); |
2866c82d JA |
152 | } |
153 | ||
2866c82d JA |
154 | static int fio_sgio_prep(struct thread_data *td, struct io_u *io_u) |
155 | { | |
156 | struct sg_io_hdr *hdr = &io_u->hdr; | |
157 | struct sgio_data *sd = td->io_ops->data; | |
158 | int nr_blocks, lba; | |
159 | ||
160 | if (io_u->buflen & (sd->bs - 1)) { | |
161 | log_err("read/write not sector aligned\n"); | |
162 | return EINVAL; | |
163 | } | |
164 | ||
2866c82d | 165 | if (io_u->ddir == DDIR_READ) { |
87dc1ab1 JA |
166 | sgio_hdr_init(sd, hdr, io_u, 1); |
167 | ||
2866c82d JA |
168 | hdr->dxfer_direction = SG_DXFER_FROM_DEV; |
169 | hdr->cmdp[0] = 0x28; | |
87dc1ab1 JA |
170 | } else if (io_u->ddir == DDIR_WRITE) { |
171 | sgio_hdr_init(sd, hdr, io_u, 1); | |
172 | ||
2866c82d JA |
173 | hdr->dxfer_direction = SG_DXFER_TO_DEV; |
174 | hdr->cmdp[0] = 0x2a; | |
87dc1ab1 JA |
175 | } else { |
176 | sgio_hdr_init(sd, hdr, io_u, 0); | |
177 | ||
178 | hdr->dxfer_direction = SG_DXFER_NONE; | |
179 | hdr->cmdp[0] = 0x35; | |
180 | } | |
181 | ||
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; | |
2866c82d JA |
191 | } |
192 | ||
2866c82d JA |
193 | return 0; |
194 | } | |
195 | ||
196 | static int fio_sgio_queue(struct thread_data *td, struct io_u *io_u) | |
197 | { | |
198 | struct sg_io_hdr *hdr = &io_u->hdr; | |
199 | int ret; | |
200 | ||
87dc1ab1 | 201 | ret = fio_sgio_doio(td, io_u, io_u->ddir == DDIR_SYNC); |
2866c82d JA |
202 | |
203 | if (ret < 0) | |
204 | io_u->error = errno; | |
205 | else if (hdr->status) { | |
206 | io_u->resid = hdr->resid; | |
207 | io_u->error = EIO; | |
208 | } | |
209 | ||
210 | return io_u->error; | |
211 | } | |
212 | ||
213 | static struct io_u *fio_sgio_event(struct thread_data *td, int event) | |
214 | { | |
215 | struct sgio_data *sd = td->io_ops->data; | |
216 | ||
217 | return sd->events[event]; | |
218 | } | |
219 | ||
220 | static int fio_sgio_get_bs(struct thread_data *td, unsigned int *bs) | |
221 | { | |
222 | struct sgio_data *sd = td->io_ops->data; | |
223 | struct io_u *io_u; | |
224 | struct sg_io_hdr *hdr; | |
225 | unsigned char buf[8]; | |
226 | int ret; | |
227 | ||
228 | io_u = __get_io_u(td); | |
229 | assert(io_u); | |
230 | ||
231 | hdr = &io_u->hdr; | |
232 | sgio_hdr_init(sd, hdr, io_u, 0); | |
233 | memset(buf, 0, sizeof(buf)); | |
234 | ||
235 | hdr->cmdp[0] = 0x25; | |
236 | hdr->dxfer_direction = SG_DXFER_FROM_DEV; | |
237 | hdr->dxferp = buf; | |
238 | hdr->dxfer_len = sizeof(buf); | |
239 | ||
240 | ret = fio_sgio_doio(td, io_u, 1); | |
241 | if (ret) { | |
242 | put_io_u(td, io_u); | |
243 | return ret; | |
244 | } | |
245 | ||
246 | *bs = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7]; | |
247 | put_io_u(td, io_u); | |
248 | return 0; | |
249 | } | |
250 | ||
251 | static void fio_sgio_cleanup(struct thread_data *td) | |
252 | { | |
253 | if (td->io_ops->data) { | |
254 | free(td->io_ops->data); | |
255 | td->io_ops->data = NULL; | |
256 | } | |
257 | } | |
258 | ||
259 | static int fio_sgio_init(struct thread_data *td) | |
260 | { | |
53cdc686 | 261 | struct fio_file *f = &td->files[0]; |
2866c82d JA |
262 | struct sgio_data *sd; |
263 | unsigned int bs; | |
264 | int ret; | |
265 | ||
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; | |
270 | ||
271 | if (td->filetype == FIO_TYPE_BD) { | |
53cdc686 | 272 | if (ioctl(f->fd, BLKSSZGET, &bs) < 0) { |
2866c82d JA |
273 | td_verror(td, errno); |
274 | return 1; | |
275 | } | |
276 | } else if (td->filetype == FIO_TYPE_CHAR) { | |
277 | int version; | |
278 | ||
53cdc686 | 279 | if (ioctl(f->fd, SG_GET_VERSION_NUM, &version) < 0) { |
2866c82d JA |
280 | td_verror(td, errno); |
281 | return 1; | |
282 | } | |
283 | ||
284 | ret = fio_sgio_get_bs(td, &bs); | |
285 | if (ret) | |
286 | return ret; | |
287 | } else { | |
288 | log_err("ioengine sgio only works on block devices\n"); | |
289 | return 1; | |
290 | } | |
291 | ||
292 | sd->bs = bs; | |
293 | ||
294 | if (td->filetype == FIO_TYPE_BD) | |
295 | td->io_ops->getevents = fio_sgio_ioctl_getevents; | |
296 | else | |
297 | td->io_ops->getevents = fio_sgio_getevents; | |
298 | ||
299 | /* | |
300 | * we want to do it, regardless of whether odirect is set or not | |
301 | */ | |
302 | td->override_sync = 1; | |
303 | return 0; | |
304 | } | |
305 | ||
306 | struct ioengine_ops ioengine = { | |
307 | .name = "sg", | |
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, | |
b2a15192 | 315 | .flags = FIO_SYNCIO | FIO_RAWIO, |
2866c82d | 316 | }; |