Commit | Line | Data |
---|---|---|
b3d5e3fd AK |
1 | /* |
2 | * nvme structure declarations and helper functions for the | |
3 | * io_uring_cmd engine. | |
4 | */ | |
5 | ||
6 | #include "nvme.h" | |
7 | ||
8 | int fio_nvme_uring_cmd_prep(struct nvme_uring_cmd *cmd, struct io_u *io_u, | |
9 | struct iovec *iov) | |
10 | { | |
11 | struct nvme_data *data = FILE_ENG_DATA(io_u->file); | |
12 | __u64 slba; | |
13 | __u32 nlb; | |
14 | ||
15 | memset(cmd, 0, sizeof(struct nvme_uring_cmd)); | |
16 | ||
17 | if (io_u->ddir == DDIR_READ) | |
18 | cmd->opcode = nvme_cmd_read; | |
19 | else if (io_u->ddir == DDIR_WRITE) | |
20 | cmd->opcode = nvme_cmd_write; | |
21 | else | |
22 | return -ENOTSUP; | |
23 | ||
24 | slba = io_u->offset >> data->lba_shift; | |
25 | nlb = (io_u->xfer_buflen >> data->lba_shift) - 1; | |
26 | ||
27 | /* cdw10 and cdw11 represent starting lba */ | |
28 | cmd->cdw10 = slba & 0xffffffff; | |
29 | cmd->cdw11 = slba >> 32; | |
30 | /* cdw12 represent number of lba's for read/write */ | |
31 | cmd->cdw12 = nlb; | |
32 | if (iov) { | |
33 | iov->iov_base = io_u->xfer_buf; | |
34 | iov->iov_len = io_u->xfer_buflen; | |
35 | cmd->addr = (__u64)(uintptr_t)iov; | |
36 | cmd->data_len = 1; | |
37 | } else { | |
38 | cmd->addr = (__u64)(uintptr_t)io_u->xfer_buf; | |
39 | cmd->data_len = io_u->xfer_buflen; | |
40 | } | |
41 | cmd->nsid = data->nsid; | |
42 | return 0; | |
43 | } | |
44 | ||
45 | static int nvme_identify(int fd, __u32 nsid, enum nvme_identify_cns cns, | |
46 | enum nvme_csi csi, void *data) | |
47 | { | |
48 | struct nvme_passthru_cmd cmd = { | |
49 | .opcode = nvme_admin_identify, | |
50 | .nsid = nsid, | |
51 | .addr = (__u64)(uintptr_t)data, | |
52 | .data_len = NVME_IDENTIFY_DATA_SIZE, | |
53 | .cdw10 = cns, | |
54 | .cdw11 = csi << NVME_IDENTIFY_CSI_SHIFT, | |
55 | .timeout_ms = NVME_DEFAULT_IOCTL_TIMEOUT, | |
56 | }; | |
57 | ||
58 | return ioctl(fd, NVME_IOCTL_ADMIN_CMD, &cmd); | |
59 | } | |
60 | ||
61 | int fio_nvme_get_info(struct fio_file *f, __u32 *nsid, __u32 *lba_sz, | |
62 | __u64 *nlba) | |
63 | { | |
64 | struct nvme_id_ns ns; | |
65 | unsigned int namespace_id; | |
66 | int fd, err; | |
67 | ||
68 | if (f->filetype != FIO_TYPE_CHAR) { | |
69 | log_err("ioengine io_uring_cmd only works with nvme ns " | |
70 | "generic char devices (/dev/ngXnY)\n"); | |
71 | return 1; | |
72 | } | |
73 | ||
74 | fd = open(f->file_name, O_RDONLY); | |
75 | if (fd < 0) | |
76 | return -errno; | |
77 | ||
78 | namespace_id = ioctl(fd, NVME_IOCTL_ID); | |
79 | if (namespace_id < 0) { | |
80 | log_err("failed to fetch namespace-id"); | |
81 | close(fd); | |
82 | return -errno; | |
83 | } | |
84 | ||
85 | /* | |
86 | * Identify namespace to get namespace-id, namespace size in LBA's | |
87 | * and LBA data size. | |
88 | */ | |
89 | err = nvme_identify(fd, namespace_id, NVME_IDENTIFY_CNS_NS, | |
90 | NVME_CSI_NVM, &ns); | |
91 | if (err) { | |
92 | log_err("failed to fetch identify namespace\n"); | |
93 | close(fd); | |
94 | return err; | |
95 | } | |
96 | ||
97 | *nsid = namespace_id; | |
98 | *lba_sz = 1 << ns.lbaf[(ns.flbas & 0x0f)].ds; | |
99 | *nlba = ns.nsze; | |
100 | ||
101 | close(fd); | |
102 | return 0; | |
103 | } | |
3d05e0ff AK |
104 | |
105 | int fio_nvme_get_zoned_model(struct thread_data *td, struct fio_file *f, | |
106 | enum zbd_zoned_model *model) | |
107 | { | |
108 | struct nvme_data *data = FILE_ENG_DATA(f); | |
109 | struct nvme_id_ns ns; | |
110 | struct nvme_passthru_cmd cmd; | |
111 | int fd, ret = 0; | |
112 | ||
113 | if (f->filetype != FIO_TYPE_CHAR) | |
114 | return -EINVAL; | |
115 | ||
116 | /* File is not yet opened */ | |
117 | fd = open(f->file_name, O_RDONLY | O_LARGEFILE); | |
118 | if (fd < 0) | |
119 | return -errno; | |
120 | ||
121 | /* Using nvme_id_ns for data as sizes are same */ | |
122 | ret = nvme_identify(fd, data->nsid, NVME_IDENTIFY_CNS_CSI_CTRL, | |
123 | NVME_CSI_ZNS, &ns); | |
124 | if (ret) { | |
125 | *model = ZBD_NONE; | |
126 | goto out; | |
127 | } | |
128 | ||
129 | memset(&cmd, 0, sizeof(struct nvme_passthru_cmd)); | |
130 | ||
131 | /* Using nvme_id_ns for data as sizes are same */ | |
132 | ret = nvme_identify(fd, data->nsid, NVME_IDENTIFY_CNS_CSI_NS, | |
133 | NVME_CSI_ZNS, &ns); | |
134 | if (ret) { | |
135 | *model = ZBD_NONE; | |
136 | goto out; | |
137 | } | |
138 | ||
139 | *model = ZBD_HOST_MANAGED; | |
140 | out: | |
141 | close(fd); | |
142 | return 0; | |
143 | } | |
144 | ||
145 | static int nvme_report_zones(int fd, __u32 nsid, __u64 slba, __u32 zras_feat, | |
146 | __u32 data_len, void *data) | |
147 | { | |
148 | struct nvme_passthru_cmd cmd = { | |
149 | .opcode = nvme_zns_cmd_mgmt_recv, | |
150 | .nsid = nsid, | |
151 | .addr = (__u64)(uintptr_t)data, | |
152 | .data_len = data_len, | |
153 | .cdw10 = slba & 0xffffffff, | |
154 | .cdw11 = slba >> 32, | |
155 | .cdw12 = (data_len >> 2) - 1, | |
156 | .cdw13 = NVME_ZNS_ZRA_REPORT_ZONES | zras_feat, | |
157 | .timeout_ms = NVME_DEFAULT_IOCTL_TIMEOUT, | |
158 | }; | |
159 | ||
160 | return ioctl(fd, NVME_IOCTL_IO_CMD, &cmd); | |
161 | } | |
162 | ||
163 | int fio_nvme_report_zones(struct thread_data *td, struct fio_file *f, | |
164 | uint64_t offset, struct zbd_zone *zbdz, | |
165 | unsigned int nr_zones) | |
166 | { | |
167 | struct nvme_data *data = FILE_ENG_DATA(f); | |
168 | struct nvme_zone_report *zr; | |
169 | struct nvme_zns_id_ns zns_ns; | |
170 | struct nvme_id_ns ns; | |
171 | unsigned int i = 0, j, zones_fetched = 0; | |
172 | unsigned int max_zones, zones_chunks = 1024; | |
173 | int fd, ret = 0; | |
174 | __u32 zr_len; | |
175 | __u64 zlen; | |
176 | ||
177 | /* File is not yet opened */ | |
178 | fd = open(f->file_name, O_RDONLY | O_LARGEFILE); | |
179 | if (fd < 0) | |
180 | return -errno; | |
181 | ||
182 | zones_fetched = 0; | |
183 | zr_len = sizeof(*zr) + (zones_chunks * sizeof(struct nvme_zns_desc)); | |
184 | zr = calloc(1, zr_len); | |
185 | if (!zr) | |
186 | return -ENOMEM; | |
187 | ||
188 | ret = nvme_identify(fd, data->nsid, NVME_IDENTIFY_CNS_NS, | |
189 | NVME_CSI_NVM, &ns); | |
190 | if (ret) { | |
191 | log_err("%s: nvme_identify_ns failed, err=%d\n", f->file_name, | |
192 | ret); | |
193 | goto out; | |
194 | } | |
195 | ||
196 | ret = nvme_identify(fd, data->nsid, NVME_IDENTIFY_CNS_CSI_NS, | |
197 | NVME_CSI_ZNS, &zns_ns); | |
198 | if (ret) { | |
199 | log_err("%s: nvme_zns_identify_ns failed, err=%d\n", | |
200 | f->file_name, ret); | |
201 | goto out; | |
202 | } | |
203 | zlen = zns_ns.lbafe[ns.flbas & 0x0f].zsze << data->lba_shift; | |
204 | ||
205 | max_zones = (f->real_file_size - offset) / zlen; | |
206 | if (max_zones < nr_zones) | |
207 | nr_zones = max_zones; | |
208 | ||
209 | if (nr_zones < zones_chunks) | |
210 | zones_chunks = nr_zones; | |
211 | ||
212 | while (zones_fetched < nr_zones) { | |
213 | if (zones_fetched + zones_chunks >= nr_zones) { | |
214 | zones_chunks = nr_zones - zones_fetched; | |
215 | zr_len = sizeof(*zr) + (zones_chunks * sizeof(struct nvme_zns_desc)); | |
216 | } | |
217 | ret = nvme_report_zones(fd, data->nsid, offset >> data->lba_shift, | |
218 | NVME_ZNS_ZRAS_FEAT_ERZ, zr_len, (void *)zr); | |
219 | if (ret) { | |
220 | log_err("%s: nvme_zns_report_zones failed, err=%d\n", | |
221 | f->file_name, ret); | |
222 | goto out; | |
223 | } | |
224 | ||
225 | /* Transform the zone-report */ | |
226 | for (j = 0; j < zr->nr_zones; j++, i++) { | |
227 | struct nvme_zns_desc *desc = (struct nvme_zns_desc *)&(zr->entries[j]); | |
228 | ||
229 | zbdz[i].start = desc->zslba << data->lba_shift; | |
230 | zbdz[i].len = zlen; | |
231 | zbdz[i].wp = desc->wp << data->lba_shift; | |
232 | zbdz[i].capacity = desc->zcap << data->lba_shift; | |
233 | ||
234 | /* Zone Type is stored in first 4 bits. */ | |
235 | switch (desc->zt & 0x0f) { | |
236 | case NVME_ZONE_TYPE_SEQWRITE_REQ: | |
237 | zbdz[i].type = ZBD_ZONE_TYPE_SWR; | |
238 | break; | |
239 | default: | |
240 | log_err("%s: invalid type for zone at offset %llu.\n", | |
241 | f->file_name, desc->zslba); | |
242 | ret = -EIO; | |
243 | goto out; | |
244 | } | |
245 | ||
246 | /* Zone State is stored in last 4 bits. */ | |
247 | switch (desc->zs >> 4) { | |
248 | case NVME_ZNS_ZS_EMPTY: | |
249 | zbdz[i].cond = ZBD_ZONE_COND_EMPTY; | |
250 | break; | |
251 | case NVME_ZNS_ZS_IMPL_OPEN: | |
252 | zbdz[i].cond = ZBD_ZONE_COND_IMP_OPEN; | |
253 | break; | |
254 | case NVME_ZNS_ZS_EXPL_OPEN: | |
255 | zbdz[i].cond = ZBD_ZONE_COND_EXP_OPEN; | |
256 | break; | |
257 | case NVME_ZNS_ZS_CLOSED: | |
258 | zbdz[i].cond = ZBD_ZONE_COND_CLOSED; | |
259 | break; | |
260 | case NVME_ZNS_ZS_FULL: | |
261 | zbdz[i].cond = ZBD_ZONE_COND_FULL; | |
262 | break; | |
263 | case NVME_ZNS_ZS_READ_ONLY: | |
264 | case NVME_ZNS_ZS_OFFLINE: | |
265 | default: | |
266 | /* Treat all these conditions as offline (don't use!) */ | |
267 | zbdz[i].cond = ZBD_ZONE_COND_OFFLINE; | |
268 | zbdz[i].wp = zbdz[i].start; | |
269 | } | |
270 | } | |
271 | zones_fetched += zr->nr_zones; | |
272 | offset += zr->nr_zones * zlen; | |
273 | } | |
274 | ||
275 | ret = zones_fetched; | |
276 | out: | |
277 | free(zr); | |
278 | close(fd); | |
279 | ||
280 | return ret; | |
281 | } | |
282 | ||
283 | int fio_nvme_reset_wp(struct thread_data *td, struct fio_file *f, | |
284 | uint64_t offset, uint64_t length) | |
285 | { | |
286 | struct nvme_data *data = FILE_ENG_DATA(f); | |
287 | unsigned int nr_zones; | |
288 | unsigned long long zslba; | |
289 | int i, fd, ret = 0; | |
290 | ||
291 | /* If the file is not yet opened, open it for this function. */ | |
292 | fd = f->fd; | |
293 | if (fd < 0) { | |
294 | fd = open(f->file_name, O_RDWR | O_LARGEFILE); | |
295 | if (fd < 0) | |
296 | return -errno; | |
297 | } | |
298 | ||
299 | zslba = offset >> data->lba_shift; | |
300 | nr_zones = (length + td->o.zone_size - 1) / td->o.zone_size; | |
301 | ||
302 | for (i = 0; i < nr_zones; i++, zslba += (td->o.zone_size >> data->lba_shift)) { | |
303 | struct nvme_passthru_cmd cmd = { | |
304 | .opcode = nvme_zns_cmd_mgmt_send, | |
305 | .nsid = data->nsid, | |
306 | .cdw10 = zslba & 0xffffffff, | |
307 | .cdw11 = zslba >> 32, | |
308 | .cdw13 = NVME_ZNS_ZSA_RESET, | |
309 | .addr = (__u64)(uintptr_t)NULL, | |
310 | .data_len = 0, | |
311 | .timeout_ms = NVME_DEFAULT_IOCTL_TIMEOUT, | |
312 | }; | |
313 | ||
314 | ret = ioctl(fd, NVME_IOCTL_IO_CMD, &cmd); | |
315 | } | |
316 | ||
317 | if (f->fd < 0) | |
318 | close(fd); | |
319 | return -ret; | |
320 | } | |
321 | ||
322 | int fio_nvme_get_max_open_zones(struct thread_data *td, struct fio_file *f, | |
323 | unsigned int *max_open_zones) | |
324 | { | |
325 | struct nvme_data *data = FILE_ENG_DATA(f); | |
326 | struct nvme_zns_id_ns zns_ns; | |
327 | int fd, ret = 0; | |
328 | ||
329 | fd = open(f->file_name, O_RDONLY | O_LARGEFILE); | |
330 | if (fd < 0) | |
331 | return -errno; | |
332 | ||
333 | ret = nvme_identify(fd, data->nsid, NVME_IDENTIFY_CNS_CSI_NS, | |
334 | NVME_CSI_ZNS, &zns_ns); | |
335 | if (ret) { | |
336 | log_err("%s: nvme_zns_identify_ns failed, err=%d\n", | |
337 | f->file_name, ret); | |
338 | goto out; | |
339 | } | |
340 | ||
341 | *max_open_zones = zns_ns.mor + 1; | |
342 | out: | |
343 | close(fd); | |
344 | return ret; | |
345 | } |