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;
24 slba = io_u->offset >> data->lba_shift;
25 nlb = (io_u->xfer_buflen >> data->lba_shift) - 1;
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 | (io_u->dtype << 20);
32 cmd->cdw13 = io_u->dspec << 16;
34 iov->iov_base = io_u->xfer_buf;
35 iov->iov_len = io_u->xfer_buflen;
36 cmd->addr = (__u64)(uintptr_t)iov;
39 cmd->addr = (__u64)(uintptr_t)io_u->xfer_buf;
40 cmd->data_len = io_u->xfer_buflen;
42 cmd->nsid = data->nsid;
46 static int nvme_trim(int fd, __u32 nsid, __u32 nr_range, __u32 data_len,
49 struct nvme_passthru_cmd cmd = {
50 .opcode = nvme_cmd_dsm,
52 .addr = (__u64)(uintptr_t)data,
54 .cdw10 = nr_range - 1,
55 .cdw11 = NVME_ATTRIBUTE_DEALLOCATE,
58 return ioctl(fd, NVME_IOCTL_IO_CMD, &cmd);
61 int fio_nvme_trim(const struct thread_data *td, struct fio_file *f,
62 unsigned long long offset, unsigned long long len)
64 struct nvme_data *data = FILE_ENG_DATA(f);
65 struct nvme_dsm_range dsm;
68 dsm.nlb = (len >> data->lba_shift);
69 dsm.slba = (offset >> data->lba_shift);
71 ret = nvme_trim(f->fd, data->nsid, 1, sizeof(struct nvme_dsm_range),
74 log_err("%s: nvme_trim failed for offset %llu and len %llu, err=%d\n",
75 f->file_name, offset, len, ret);
80 static int nvme_identify(int fd, __u32 nsid, enum nvme_identify_cns cns,
81 enum nvme_csi csi, void *data)
83 struct nvme_passthru_cmd cmd = {
84 .opcode = nvme_admin_identify,
86 .addr = (__u64)(uintptr_t)data,
87 .data_len = NVME_IDENTIFY_DATA_SIZE,
89 .cdw11 = csi << NVME_IDENTIFY_CSI_SHIFT,
90 .timeout_ms = NVME_DEFAULT_IOCTL_TIMEOUT,
93 return ioctl(fd, NVME_IOCTL_ADMIN_CMD, &cmd);
96 int fio_nvme_get_info(struct fio_file *f, __u32 *nsid, __u32 *lba_sz,
103 if (f->filetype != FIO_TYPE_CHAR) {
104 log_err("ioengine io_uring_cmd only works with nvme ns "
105 "generic char devices (/dev/ngXnY)\n");
109 fd = open(f->file_name, O_RDONLY);
113 namespace_id = ioctl(fd, NVME_IOCTL_ID);
114 if (namespace_id < 0) {
116 log_err("failed to fetch namespace-id");
122 * Identify namespace to get namespace-id, namespace size in LBA's
125 err = nvme_identify(fd, namespace_id, NVME_IDENTIFY_CNS_NS,
128 log_err("failed to fetch identify namespace\n");
133 *nsid = namespace_id;
134 *lba_sz = 1 << ns.lbaf[(ns.flbas & 0x0f)].ds;
141 int fio_nvme_get_zoned_model(struct thread_data *td, struct fio_file *f,
142 enum zbd_zoned_model *model)
144 struct nvme_data *data = FILE_ENG_DATA(f);
145 struct nvme_id_ns ns;
146 struct nvme_passthru_cmd cmd;
149 if (f->filetype != FIO_TYPE_CHAR)
152 /* File is not yet opened */
153 fd = open(f->file_name, O_RDONLY | O_LARGEFILE);
157 /* Using nvme_id_ns for data as sizes are same */
158 ret = nvme_identify(fd, data->nsid, NVME_IDENTIFY_CNS_CSI_CTRL,
165 memset(&cmd, 0, sizeof(struct nvme_passthru_cmd));
167 /* Using nvme_id_ns for data as sizes are same */
168 ret = nvme_identify(fd, data->nsid, NVME_IDENTIFY_CNS_CSI_NS,
175 *model = ZBD_HOST_MANAGED;
181 static int nvme_report_zones(int fd, __u32 nsid, __u64 slba, __u32 zras_feat,
182 __u32 data_len, void *data)
184 struct nvme_passthru_cmd cmd = {
185 .opcode = nvme_zns_cmd_mgmt_recv,
187 .addr = (__u64)(uintptr_t)data,
188 .data_len = data_len,
189 .cdw10 = slba & 0xffffffff,
191 .cdw12 = (data_len >> 2) - 1,
192 .cdw13 = NVME_ZNS_ZRA_REPORT_ZONES | zras_feat,
193 .timeout_ms = NVME_DEFAULT_IOCTL_TIMEOUT,
196 return ioctl(fd, NVME_IOCTL_IO_CMD, &cmd);
199 int fio_nvme_report_zones(struct thread_data *td, struct fio_file *f,
200 uint64_t offset, struct zbd_zone *zbdz,
201 unsigned int nr_zones)
203 struct nvme_data *data = FILE_ENG_DATA(f);
204 struct nvme_zone_report *zr;
205 struct nvme_zns_id_ns zns_ns;
206 struct nvme_id_ns ns;
207 unsigned int i = 0, j, zones_fetched = 0;
208 unsigned int max_zones, zones_chunks = 1024;
213 /* File is not yet opened */
214 fd = open(f->file_name, O_RDONLY | O_LARGEFILE);
219 zr_len = sizeof(*zr) + (zones_chunks * sizeof(struct nvme_zns_desc));
220 zr = calloc(1, zr_len);
226 ret = nvme_identify(fd, data->nsid, NVME_IDENTIFY_CNS_NS,
229 log_err("%s: nvme_identify_ns failed, err=%d\n", f->file_name,
234 ret = nvme_identify(fd, data->nsid, NVME_IDENTIFY_CNS_CSI_NS,
235 NVME_CSI_ZNS, &zns_ns);
237 log_err("%s: nvme_zns_identify_ns failed, err=%d\n",
241 zlen = zns_ns.lbafe[ns.flbas & 0x0f].zsze << data->lba_shift;
243 max_zones = (f->real_file_size - offset) / zlen;
244 if (max_zones < nr_zones)
245 nr_zones = max_zones;
247 if (nr_zones < zones_chunks)
248 zones_chunks = nr_zones;
250 while (zones_fetched < nr_zones) {
251 if (zones_fetched + zones_chunks >= nr_zones) {
252 zones_chunks = nr_zones - zones_fetched;
253 zr_len = sizeof(*zr) + (zones_chunks * sizeof(struct nvme_zns_desc));
255 ret = nvme_report_zones(fd, data->nsid, offset >> data->lba_shift,
256 NVME_ZNS_ZRAS_FEAT_ERZ, zr_len, (void *)zr);
258 log_err("%s: nvme_zns_report_zones failed, err=%d\n",
263 /* Transform the zone-report */
264 for (j = 0; j < zr->nr_zones; j++, i++) {
265 struct nvme_zns_desc *desc = (struct nvme_zns_desc *)&(zr->entries[j]);
267 zbdz[i].start = desc->zslba << data->lba_shift;
269 zbdz[i].wp = desc->wp << data->lba_shift;
270 zbdz[i].capacity = desc->zcap << data->lba_shift;
272 /* Zone Type is stored in first 4 bits. */
273 switch (desc->zt & 0x0f) {
274 case NVME_ZONE_TYPE_SEQWRITE_REQ:
275 zbdz[i].type = ZBD_ZONE_TYPE_SWR;
278 log_err("%s: invalid type for zone at offset %llu.\n",
279 f->file_name, (unsigned long long) desc->zslba);
284 /* Zone State is stored in last 4 bits. */
285 switch (desc->zs >> 4) {
286 case NVME_ZNS_ZS_EMPTY:
287 zbdz[i].cond = ZBD_ZONE_COND_EMPTY;
289 case NVME_ZNS_ZS_IMPL_OPEN:
290 zbdz[i].cond = ZBD_ZONE_COND_IMP_OPEN;
292 case NVME_ZNS_ZS_EXPL_OPEN:
293 zbdz[i].cond = ZBD_ZONE_COND_EXP_OPEN;
295 case NVME_ZNS_ZS_CLOSED:
296 zbdz[i].cond = ZBD_ZONE_COND_CLOSED;
298 case NVME_ZNS_ZS_FULL:
299 zbdz[i].cond = ZBD_ZONE_COND_FULL;
301 case NVME_ZNS_ZS_READ_ONLY:
302 case NVME_ZNS_ZS_OFFLINE:
304 /* Treat all these conditions as offline (don't use!) */
305 zbdz[i].cond = ZBD_ZONE_COND_OFFLINE;
306 zbdz[i].wp = zbdz[i].start;
309 zones_fetched += zr->nr_zones;
310 offset += zr->nr_zones * zlen;
321 int fio_nvme_reset_wp(struct thread_data *td, struct fio_file *f,
322 uint64_t offset, uint64_t length)
324 struct nvme_data *data = FILE_ENG_DATA(f);
325 unsigned int nr_zones;
326 unsigned long long zslba;
329 /* If the file is not yet opened, open it for this function. */
332 fd = open(f->file_name, O_RDWR | O_LARGEFILE);
337 zslba = offset >> data->lba_shift;
338 nr_zones = (length + td->o.zone_size - 1) / td->o.zone_size;
340 for (i = 0; i < nr_zones; i++, zslba += (td->o.zone_size >> data->lba_shift)) {
341 struct nvme_passthru_cmd cmd = {
342 .opcode = nvme_zns_cmd_mgmt_send,
344 .cdw10 = zslba & 0xffffffff,
345 .cdw11 = zslba >> 32,
346 .cdw13 = NVME_ZNS_ZSA_RESET,
347 .addr = (__u64)(uintptr_t)NULL,
349 .timeout_ms = NVME_DEFAULT_IOCTL_TIMEOUT,
352 ret = ioctl(fd, NVME_IOCTL_IO_CMD, &cmd);
360 int fio_nvme_get_max_open_zones(struct thread_data *td, struct fio_file *f,
361 unsigned int *max_open_zones)
363 struct nvme_data *data = FILE_ENG_DATA(f);
364 struct nvme_zns_id_ns zns_ns;
367 fd = open(f->file_name, O_RDONLY | O_LARGEFILE);
371 ret = nvme_identify(fd, data->nsid, NVME_IDENTIFY_CNS_CSI_NS,
372 NVME_CSI_ZNS, &zns_ns);
374 log_err("%s: nvme_zns_identify_ns failed, err=%d\n",
379 *max_open_zones = zns_ns.mor + 1;
385 static inline int nvme_fdp_reclaim_unit_handle_status(int fd, __u32 nsid,
386 __u32 data_len, void *data)
388 struct nvme_passthru_cmd cmd = {
389 .opcode = nvme_cmd_io_mgmt_recv,
391 .addr = (__u64)(uintptr_t)data,
392 .data_len = data_len,
394 .cdw11 = (data_len >> 2) - 1,
397 return ioctl(fd, NVME_IOCTL_IO_CMD, &cmd);
400 int fio_nvme_iomgmt_ruhs(struct thread_data *td, struct fio_file *f,
401 struct nvme_fdp_ruh_status *ruhs, __u32 bytes)
403 struct nvme_data *data = FILE_ENG_DATA(f);
406 fd = open(f->file_name, O_RDONLY | O_LARGEFILE);
410 ret = nvme_fdp_reclaim_unit_handle_status(fd, data->nsid, bytes, ruhs);
412 log_err("%s: nvme_fdp_reclaim_unit_handle_status failed, err=%d\n",