fix: io_uring sqpoll issue_time empty when kernel not yet read sq
[fio.git] / engines / nvme.h
1 /*
2  * nvme structure declarations and helper functions for the
3  * io_uring_cmd engine.
4  */
5
6 #ifndef FIO_NVME_H
7 #define FIO_NVME_H
8
9 #include <linux/nvme_ioctl.h>
10 #include "../fio.h"
11
12 /*
13  * If the uapi headers installed on the system lacks nvme uring command
14  * support, use the local version to prevent compilation issues.
15  */
16 #ifndef CONFIG_NVME_URING_CMD
17 struct nvme_uring_cmd {
18         __u8    opcode;
19         __u8    flags;
20         __u16   rsvd1;
21         __u32   nsid;
22         __u32   cdw2;
23         __u32   cdw3;
24         __u64   metadata;
25         __u64   addr;
26         __u32   metadata_len;
27         __u32   data_len;
28         __u32   cdw10;
29         __u32   cdw11;
30         __u32   cdw12;
31         __u32   cdw13;
32         __u32   cdw14;
33         __u32   cdw15;
34         __u32   timeout_ms;
35         __u32   rsvd2;
36 };
37
38 #define NVME_URING_CMD_IO       _IOWR('N', 0x80, struct nvme_uring_cmd)
39 #define NVME_URING_CMD_IO_VEC   _IOWR('N', 0x81, struct nvme_uring_cmd)
40 #endif /* CONFIG_NVME_URING_CMD */
41
42 #define NVME_DEFAULT_IOCTL_TIMEOUT 0
43 #define NVME_IDENTIFY_DATA_SIZE 4096
44 #define NVME_IDENTIFY_CSI_SHIFT 24
45
46 #define NVME_ZNS_ZRA_REPORT_ZONES 0
47 #define NVME_ZNS_ZRAS_FEAT_ERZ (1 << 16)
48 #define NVME_ZNS_ZSA_RESET 0x4
49 #define NVME_ZONE_TYPE_SEQWRITE_REQ 0x2
50
51 #define NVME_ATTRIBUTE_DEALLOCATE (1 << 2)
52
53 enum nvme_identify_cns {
54         NVME_IDENTIFY_CNS_NS            = 0x00,
55         NVME_IDENTIFY_CNS_CSI_NS        = 0x05,
56         NVME_IDENTIFY_CNS_CSI_CTRL      = 0x06,
57 };
58
59 enum nvme_csi {
60         NVME_CSI_NVM                    = 0,
61         NVME_CSI_KV                     = 1,
62         NVME_CSI_ZNS                    = 2,
63 };
64
65 enum nvme_admin_opcode {
66         nvme_admin_identify             = 0x06,
67 };
68
69 enum nvme_io_opcode {
70         nvme_cmd_write                  = 0x01,
71         nvme_cmd_read                   = 0x02,
72         nvme_cmd_dsm                    = 0x09,
73         nvme_cmd_io_mgmt_recv           = 0x12,
74         nvme_zns_cmd_mgmt_send          = 0x79,
75         nvme_zns_cmd_mgmt_recv          = 0x7a,
76 };
77
78 enum nvme_zns_zs {
79         NVME_ZNS_ZS_EMPTY               = 0x1,
80         NVME_ZNS_ZS_IMPL_OPEN           = 0x2,
81         NVME_ZNS_ZS_EXPL_OPEN           = 0x3,
82         NVME_ZNS_ZS_CLOSED              = 0x4,
83         NVME_ZNS_ZS_READ_ONLY           = 0xd,
84         NVME_ZNS_ZS_FULL                = 0xe,
85         NVME_ZNS_ZS_OFFLINE             = 0xf,
86 };
87
88 struct nvme_data {
89         __u32 nsid;
90         __u32 lba_shift;
91         __u32 lba_ext;
92 };
93
94 struct nvme_lbaf {
95         __le16                  ms;
96         __u8                    ds;
97         __u8                    rp;
98 };
99
100 struct nvme_id_ns {
101         __le64                  nsze;
102         __le64                  ncap;
103         __le64                  nuse;
104         __u8                    nsfeat;
105         __u8                    nlbaf;
106         __u8                    flbas;
107         __u8                    mc;
108         __u8                    dpc;
109         __u8                    dps;
110         __u8                    nmic;
111         __u8                    rescap;
112         __u8                    fpi;
113         __u8                    dlfeat;
114         __le16                  nawun;
115         __le16                  nawupf;
116         __le16                  nacwu;
117         __le16                  nabsn;
118         __le16                  nabo;
119         __le16                  nabspf;
120         __le16                  noiob;
121         __u8                    nvmcap[16];
122         __le16                  npwg;
123         __le16                  npwa;
124         __le16                  npdg;
125         __le16                  npda;
126         __le16                  nows;
127         __le16                  mssrl;
128         __le32                  mcl;
129         __u8                    msrc;
130         __u8                    rsvd81[11];
131         __le32                  anagrpid;
132         __u8                    rsvd96[3];
133         __u8                    nsattr;
134         __le16                  nvmsetid;
135         __le16                  endgid;
136         __u8                    nguid[16];
137         __u8                    eui64[8];
138         struct nvme_lbaf        lbaf[64];
139         __u8                    vs[3712];
140 };
141
142 static inline int ilog2(uint32_t i)
143 {
144         int log = -1;
145
146         while (i) {
147                 i >>= 1;
148                 log++;
149         }
150         return log;
151 }
152
153 struct nvme_zns_lbafe {
154         __le64  zsze;
155         __u8    zdes;
156         __u8    rsvd9[7];
157 };
158
159 struct nvme_zns_id_ns {
160         __le16                  zoc;
161         __le16                  ozcs;
162         __le32                  mar;
163         __le32                  mor;
164         __le32                  rrl;
165         __le32                  frl;
166         __le32                  rrl1;
167         __le32                  rrl2;
168         __le32                  rrl3;
169         __le32                  frl1;
170         __le32                  frl2;
171         __le32                  frl3;
172         __le32                  numzrwa;
173         __le16                  zrwafg;
174         __le16                  zrwasz;
175         __u8                    zrwacap;
176         __u8                    rsvd53[2763];
177         struct nvme_zns_lbafe   lbafe[64];
178         __u8                    vs[256];
179 };
180
181 struct nvme_zns_desc {
182         __u8    zt;
183         __u8    zs;
184         __u8    za;
185         __u8    zai;
186         __u8    rsvd4[4];
187         __le64  zcap;
188         __le64  zslba;
189         __le64  wp;
190         __u8    rsvd32[32];
191 };
192
193 struct nvme_zone_report {
194         __le64                  nr_zones;
195         __u8                    rsvd8[56];
196         struct nvme_zns_desc    entries[];
197 };
198
199 struct nvme_fdp_ruh_status_desc {
200         __u16 pid;
201         __u16 ruhid;
202         __u32 earutr;
203         __u64 ruamw;
204         __u8  rsvd16[16];
205 };
206
207 struct nvme_fdp_ruh_status {
208         __u8  rsvd0[14];
209         __le16 nruhsd;
210         struct nvme_fdp_ruh_status_desc ruhss[];
211 };
212
213 struct nvme_dsm_range {
214         __le32  cattr;
215         __le32  nlb;
216         __le64  slba;
217 };
218
219 int fio_nvme_iomgmt_ruhs(struct thread_data *td, struct fio_file *f,
220                          struct nvme_fdp_ruh_status *ruhs, __u32 bytes);
221
222 int fio_nvme_get_info(struct fio_file *f, __u32 *nsid, __u32 *lba_sz,
223                       __u32 *ms, __u64 *nlba);
224
225 int fio_nvme_uring_cmd_prep(struct nvme_uring_cmd *cmd, struct io_u *io_u,
226                             struct iovec *iov, struct nvme_dsm_range *dsm);
227
228 int fio_nvme_get_zoned_model(struct thread_data *td, struct fio_file *f,
229                              enum zbd_zoned_model *model);
230
231 int fio_nvme_report_zones(struct thread_data *td, struct fio_file *f,
232                           uint64_t offset, struct zbd_zone *zbdz,
233                           unsigned int nr_zones);
234
235 int fio_nvme_reset_wp(struct thread_data *td, struct fio_file *f,
236                       uint64_t offset, uint64_t length);
237
238 int fio_nvme_get_max_open_zones(struct thread_data *td, struct fio_file *f,
239                                 unsigned int *max_open_zones);
240
241 #endif