2 * nvme structure declarations and helper functions for the
8 int fio_nvme_uring_cmd_prep(struct nvme_uring_cmd *cmd, struct io_u *io_u,
11 struct nvme_data *data = FILE_ENG_DATA(io_u->file);
15 memset(cmd, 0, sizeof(struct nvme_uring_cmd));
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;
25 slba = io_u->offset / data->lba_ext;
26 nlb = (io_u->xfer_buflen / data->lba_ext) - 1;
28 slba = io_u->offset >> data->lba_shift;
29 nlb = (io_u->xfer_buflen >> data->lba_shift) - 1;
32 /* cdw10 and cdw11 represent starting lba */
33 cmd->cdw10 = slba & 0xffffffff;
34 cmd->cdw11 = slba >> 32;
35 /* cdw12 represent number of lba's for read/write */
36 cmd->cdw12 = nlb | (io_u->dtype << 20);
37 cmd->cdw13 = io_u->dspec << 16;
39 iov->iov_base = io_u->xfer_buf;
40 iov->iov_len = io_u->xfer_buflen;
41 cmd->addr = (__u64)(uintptr_t)iov;
44 cmd->addr = (__u64)(uintptr_t)io_u->xfer_buf;
45 cmd->data_len = io_u->xfer_buflen;
47 cmd->nsid = data->nsid;
51 static int nvme_trim(int fd, __u32 nsid, __u32 nr_range, __u32 data_len,
54 struct nvme_passthru_cmd cmd = {
55 .opcode = nvme_cmd_dsm,
57 .addr = (__u64)(uintptr_t)data,
59 .cdw10 = nr_range - 1,
60 .cdw11 = NVME_ATTRIBUTE_DEALLOCATE,
63 return ioctl(fd, NVME_IOCTL_IO_CMD, &cmd);
66 int fio_nvme_trim(const struct thread_data *td, struct fio_file *f,
67 unsigned long long offset, unsigned long long len)
69 struct nvme_data *data = FILE_ENG_DATA(f);
70 struct nvme_dsm_range dsm;
74 dsm.nlb = len / data->lba_ext;
75 dsm.slba = offset / data->lba_ext;
77 dsm.nlb = len >> data->lba_shift;
78 dsm.slba = offset >> data->lba_shift;
81 ret = nvme_trim(f->fd, data->nsid, 1, sizeof(struct nvme_dsm_range),
84 log_err("%s: nvme_trim failed for offset %llu and len %llu, err=%d\n",
85 f->file_name, offset, len, ret);
90 static int nvme_identify(int fd, __u32 nsid, enum nvme_identify_cns cns,
91 enum nvme_csi csi, void *data)
93 struct nvme_passthru_cmd cmd = {
94 .opcode = nvme_admin_identify,
96 .addr = (__u64)(uintptr_t)data,
97 .data_len = NVME_IDENTIFY_DATA_SIZE,
99 .cdw11 = csi << NVME_IDENTIFY_CSI_SHIFT,
100 .timeout_ms = NVME_DEFAULT_IOCTL_TIMEOUT,
103 return ioctl(fd, NVME_IOCTL_ADMIN_CMD, &cmd);
106 int fio_nvme_get_info(struct fio_file *f, __u32 *nsid, __u32 *lba_sz,
107 __u32 *ms, __u64 *nlba)
109 struct nvme_id_ns ns;
114 if (f->filetype != FIO_TYPE_CHAR) {
115 log_err("ioengine io_uring_cmd only works with nvme ns "
116 "generic char devices (/dev/ngXnY)\n");
120 fd = open(f->file_name, O_RDONLY);
124 namespace_id = ioctl(fd, NVME_IOCTL_ID);
125 if (namespace_id < 0) {
127 log_err("%s: failed to fetch namespace-id\n", f->file_name);
132 * Identify namespace to get namespace-id, namespace size in LBA's
135 err = nvme_identify(fd, namespace_id, NVME_IDENTIFY_CNS_NS,
138 log_err("%s: failed to fetch identify namespace\n",
144 *nsid = namespace_id;
147 * 16 or 64 as maximum number of supported LBA formats.
148 * From flbas bit 0-3 indicates lsb and bit 5-6 indicates msb
149 * of the format index used to format the namespace.
152 format_idx = ns.flbas & 0xf;
154 format_idx = (ns.flbas & 0xf) + (((ns.flbas >> 5) & 0x3) << 4);
156 *lba_sz = 1 << ns.lbaf[format_idx].ds;
159 * Only extended LBA can be supported.
160 * Bit 4 for flbas indicates if metadata is transferred at the end of
161 * logical block creating an extended LBA.
163 *ms = le16_to_cpu(ns.lbaf[format_idx].ms);
164 if (*ms && !((ns.flbas >> 4) & 0x1)) {
165 log_err("%s: only extended logical block can be supported\n",
171 /* Check for end to end data protection support */
173 log_err("%s: end to end data protection not supported\n",
185 int fio_nvme_get_zoned_model(struct thread_data *td, struct fio_file *f,
186 enum zbd_zoned_model *model)
188 struct nvme_data *data = FILE_ENG_DATA(f);
189 struct nvme_id_ns ns;
190 struct nvme_passthru_cmd cmd;
193 if (f->filetype != FIO_TYPE_CHAR)
196 /* File is not yet opened */
197 fd = open(f->file_name, O_RDONLY | O_LARGEFILE);
201 /* Using nvme_id_ns for data as sizes are same */
202 ret = nvme_identify(fd, data->nsid, NVME_IDENTIFY_CNS_CSI_CTRL,
209 memset(&cmd, 0, sizeof(struct nvme_passthru_cmd));
211 /* Using nvme_id_ns for data as sizes are same */
212 ret = nvme_identify(fd, data->nsid, NVME_IDENTIFY_CNS_CSI_NS,
219 *model = ZBD_HOST_MANAGED;
225 static int nvme_report_zones(int fd, __u32 nsid, __u64 slba, __u32 zras_feat,
226 __u32 data_len, void *data)
228 struct nvme_passthru_cmd cmd = {
229 .opcode = nvme_zns_cmd_mgmt_recv,
231 .addr = (__u64)(uintptr_t)data,
232 .data_len = data_len,
233 .cdw10 = slba & 0xffffffff,
235 .cdw12 = (data_len >> 2) - 1,
236 .cdw13 = NVME_ZNS_ZRA_REPORT_ZONES | zras_feat,
237 .timeout_ms = NVME_DEFAULT_IOCTL_TIMEOUT,
240 return ioctl(fd, NVME_IOCTL_IO_CMD, &cmd);
243 int fio_nvme_report_zones(struct thread_data *td, struct fio_file *f,
244 uint64_t offset, struct zbd_zone *zbdz,
245 unsigned int nr_zones)
247 struct nvme_data *data = FILE_ENG_DATA(f);
248 struct nvme_zone_report *zr;
249 struct nvme_zns_id_ns zns_ns;
250 struct nvme_id_ns ns;
251 unsigned int i = 0, j, zones_fetched = 0;
252 unsigned int max_zones, zones_chunks = 1024;
257 /* File is not yet opened */
258 fd = open(f->file_name, O_RDONLY | O_LARGEFILE);
263 zr_len = sizeof(*zr) + (zones_chunks * sizeof(struct nvme_zns_desc));
264 zr = calloc(1, zr_len);
270 ret = nvme_identify(fd, data->nsid, NVME_IDENTIFY_CNS_NS,
273 log_err("%s: nvme_identify_ns failed, err=%d\n", f->file_name,
278 ret = nvme_identify(fd, data->nsid, NVME_IDENTIFY_CNS_CSI_NS,
279 NVME_CSI_ZNS, &zns_ns);
281 log_err("%s: nvme_zns_identify_ns failed, err=%d\n",
285 zlen = zns_ns.lbafe[ns.flbas & 0x0f].zsze << data->lba_shift;
287 max_zones = (f->real_file_size - offset) / zlen;
288 if (max_zones < nr_zones)
289 nr_zones = max_zones;
291 if (nr_zones < zones_chunks)
292 zones_chunks = nr_zones;
294 while (zones_fetched < nr_zones) {
295 if (zones_fetched + zones_chunks >= nr_zones) {
296 zones_chunks = nr_zones - zones_fetched;
297 zr_len = sizeof(*zr) + (zones_chunks * sizeof(struct nvme_zns_desc));
299 ret = nvme_report_zones(fd, data->nsid, offset >> data->lba_shift,
300 NVME_ZNS_ZRAS_FEAT_ERZ, zr_len, (void *)zr);
302 log_err("%s: nvme_zns_report_zones failed, err=%d\n",
307 /* Transform the zone-report */
308 for (j = 0; j < zr->nr_zones; j++, i++) {
309 struct nvme_zns_desc *desc = (struct nvme_zns_desc *)&(zr->entries[j]);
311 zbdz[i].start = desc->zslba << data->lba_shift;
313 zbdz[i].wp = desc->wp << data->lba_shift;
314 zbdz[i].capacity = desc->zcap << data->lba_shift;
316 /* Zone Type is stored in first 4 bits. */
317 switch (desc->zt & 0x0f) {
318 case NVME_ZONE_TYPE_SEQWRITE_REQ:
319 zbdz[i].type = ZBD_ZONE_TYPE_SWR;
322 log_err("%s: invalid type for zone at offset %llu.\n",
323 f->file_name, (unsigned long long) desc->zslba);
328 /* Zone State is stored in last 4 bits. */
329 switch (desc->zs >> 4) {
330 case NVME_ZNS_ZS_EMPTY:
331 zbdz[i].cond = ZBD_ZONE_COND_EMPTY;
333 case NVME_ZNS_ZS_IMPL_OPEN:
334 zbdz[i].cond = ZBD_ZONE_COND_IMP_OPEN;
336 case NVME_ZNS_ZS_EXPL_OPEN:
337 zbdz[i].cond = ZBD_ZONE_COND_EXP_OPEN;
339 case NVME_ZNS_ZS_CLOSED:
340 zbdz[i].cond = ZBD_ZONE_COND_CLOSED;
342 case NVME_ZNS_ZS_FULL:
343 zbdz[i].cond = ZBD_ZONE_COND_FULL;
345 case NVME_ZNS_ZS_READ_ONLY:
346 case NVME_ZNS_ZS_OFFLINE:
348 /* Treat all these conditions as offline (don't use!) */
349 zbdz[i].cond = ZBD_ZONE_COND_OFFLINE;
350 zbdz[i].wp = zbdz[i].start;
353 zones_fetched += zr->nr_zones;
354 offset += zr->nr_zones * zlen;
365 int fio_nvme_reset_wp(struct thread_data *td, struct fio_file *f,
366 uint64_t offset, uint64_t length)
368 struct nvme_data *data = FILE_ENG_DATA(f);
369 unsigned int nr_zones;
370 unsigned long long zslba;
373 /* If the file is not yet opened, open it for this function. */
376 fd = open(f->file_name, O_RDWR | O_LARGEFILE);
381 zslba = offset >> data->lba_shift;
382 nr_zones = (length + td->o.zone_size - 1) / td->o.zone_size;
384 for (i = 0; i < nr_zones; i++, zslba += (td->o.zone_size >> data->lba_shift)) {
385 struct nvme_passthru_cmd cmd = {
386 .opcode = nvme_zns_cmd_mgmt_send,
388 .cdw10 = zslba & 0xffffffff,
389 .cdw11 = zslba >> 32,
390 .cdw13 = NVME_ZNS_ZSA_RESET,
391 .addr = (__u64)(uintptr_t)NULL,
393 .timeout_ms = NVME_DEFAULT_IOCTL_TIMEOUT,
396 ret = ioctl(fd, NVME_IOCTL_IO_CMD, &cmd);
404 int fio_nvme_get_max_open_zones(struct thread_data *td, struct fio_file *f,
405 unsigned int *max_open_zones)
407 struct nvme_data *data = FILE_ENG_DATA(f);
408 struct nvme_zns_id_ns zns_ns;
411 fd = open(f->file_name, O_RDONLY | O_LARGEFILE);
415 ret = nvme_identify(fd, data->nsid, NVME_IDENTIFY_CNS_CSI_NS,
416 NVME_CSI_ZNS, &zns_ns);
418 log_err("%s: nvme_zns_identify_ns failed, err=%d\n",
423 *max_open_zones = zns_ns.mor + 1;
429 static inline int nvme_fdp_reclaim_unit_handle_status(int fd, __u32 nsid,
430 __u32 data_len, void *data)
432 struct nvme_passthru_cmd cmd = {
433 .opcode = nvme_cmd_io_mgmt_recv,
435 .addr = (__u64)(uintptr_t)data,
436 .data_len = data_len,
438 .cdw11 = (data_len >> 2) - 1,
441 return ioctl(fd, NVME_IOCTL_IO_CMD, &cmd);
444 int fio_nvme_iomgmt_ruhs(struct thread_data *td, struct fio_file *f,
445 struct nvme_fdp_ruh_status *ruhs, __u32 bytes)
447 struct nvme_data *data = FILE_ENG_DATA(f);
450 fd = open(f->file_name, O_RDONLY | O_LARGEFILE);
454 ret = nvme_fdp_reclaim_unit_handle_status(fd, data->nsid, bytes, ruhs);
456 log_err("%s: nvme_fdp_reclaim_unit_handle_status failed, err=%d\n",