1 /*
2  * SPDX-License-Identifier: Apache-2.0
3  * Copyright (c) 2022 Intel Corp.
4  */
5 
6 #ifndef ZEPHYR_DRIVERS_DISK_NVME_NHME_HELPERS_H_
7 #define ZEPHYR_DRIVERS_DISK_NVME_NHME_HELPERS_H_
8 
9 #define NVME_GONE		0xfffffffful
10 
11 /*
12  * Macros to deal with NVME revisions, as defined VS register
13  */
14 #define NVME_REV(x, y)			(((x) << 16) | ((y) << 8))
15 #define NVME_MAJOR(r)			(((r) >> 16) & 0xffff)
16 #define NVME_MINOR(r)			(((r) >> 8) & 0xff)
17 
18 /*
19  * Use to mark a command to apply to all namespaces, or to retrieve global
20  * log pages.
21  */
22 #define NVME_GLOBAL_NAMESPACE_TAG	((uint32_t)0xFFFFFFFF)
23 
24 /* Many items are expressed in terms of power of two times MPS */
25 #define NVME_MPS_SHIFT			12
26 
27 /* Register field definitions */
28 #define NVME_CAP_LO_REG_MQES_SHIFT	(0)
29 #define NVME_CAP_LO_REG_MQES_MASK	(0xFFFF)
30 #define NVME_CAP_LO_REG_CQR_SHIFT	(16)
31 #define NVME_CAP_LO_REG_CQR_MASK	(0x1)
32 #define NVME_CAP_LO_REG_AMS_SHIFT	(17)
33 #define NVME_CAP_LO_REG_AMS_MASK	(0x3)
34 #define NVME_CAP_LO_REG_TO_SHIFT	(24)
35 #define NVME_CAP_LO_REG_TO_MASK		(0xFF)
36 #define NVME_CAP_LO_MQES(x)						\
37 	(((x) >> NVME_CAP_LO_REG_MQES_SHIFT) & NVME_CAP_LO_REG_MQES_MASK)
38 #define NVME_CAP_LO_CQR(x)						\
39 	(((x) >> NVME_CAP_LO_REG_CQR_SHIFT) & NVME_CAP_LO_REG_CQR_MASK)
40 #define NVME_CAP_LO_AMS(x)						\
41 	(((x) >> NVME_CAP_LO_REG_AMS_SHIFT) & NVME_CAP_LO_REG_AMS_MASK)
42 #define NVME_CAP_LO_TO(x)						\
43 	(((x) >> NVME_CAP_LO_REG_TO_SHIFT) & NVME_CAP_LO_REG_TO_MASK)
44 
45 #define NVME_CAP_HI_REG_DSTRD_SHIFT	(0)
46 #define NVME_CAP_HI_REG_DSTRD_MASK	(0xF)
47 #define NVME_CAP_HI_REG_NSSRS_SHIFT	(4)
48 #define NVME_CAP_HI_REG_NSSRS_MASK	(0x1)
49 #define NVME_CAP_HI_REG_CSS_SHIFT	(5)
50 #define NVME_CAP_HI_REG_CSS_MASK	(0xff)
51 #define NVME_CAP_HI_REG_CSS_NVM_SHIFT	(5)
52 #define NVME_CAP_HI_REG_CSS_NVM_MASK	(0x1)
53 #define NVME_CAP_HI_REG_BPS_SHIFT	(13)
54 #define NVME_CAP_HI_REG_BPS_MASK	(0x1)
55 #define NVME_CAP_HI_REG_MPSMIN_SHIFT	(16)
56 #define NVME_CAP_HI_REG_MPSMIN_MASK	(0xF)
57 #define NVME_CAP_HI_REG_MPSMAX_SHIFT	(20)
58 #define NVME_CAP_HI_REG_MPSMAX_MASK	(0xF)
59 #define NVME_CAP_HI_REG_PMRS_SHIFT	(24)
60 #define NVME_CAP_HI_REG_PMRS_MASK	(0x1)
61 #define NVME_CAP_HI_REG_CMBS_SHIFT	(25)
62 #define NVME_CAP_HI_REG_CMBS_MASK	(0x1)
63 #define NVME_CAP_HI_DSTRD(x)						\
64 	(((x) >> NVME_CAP_HI_REG_DSTRD_SHIFT) & NVME_CAP_HI_REG_DSTRD_MASK)
65 #define NVME_CAP_HI_NSSRS(x)						\
66 	(((x) >> NVME_CAP_HI_REG_NSSRS_SHIFT) & NVME_CAP_HI_REG_NSSRS_MASK)
67 #define NVME_CAP_HI_CSS(x)						\
68 	(((x) >> NVME_CAP_HI_REG_CSS_SHIFT) & NVME_CAP_HI_REG_CSS_MASK)
69 #define NVME_CAP_HI_CSS_NVM(x)						\
70 	(((x) >> NVME_CAP_HI_REG_CSS_NVM_SHIFT) & NVME_CAP_HI_REG_CSS_NVM_MASK)
71 #define NVME_CAP_HI_BPS(x)						\
72 	(((x) >> NVME_CAP_HI_REG_BPS_SHIFT) & NVME_CAP_HI_REG_BPS_MASK)
73 #define NVME_CAP_HI_MPSMIN(x)						\
74 	(((x) >> NVME_CAP_HI_REG_MPSMIN_SHIFT) & NVME_CAP_HI_REG_MPSMIN_MASK)
75 #define NVME_CAP_HI_MPSMAX(x)						\
76 	(((x) >> NVME_CAP_HI_REG_MPSMAX_SHIFT) & NVME_CAP_HI_REG_MPSMAX_MASK)
77 #define NVME_CAP_HI_PMRS(x)						\
78 	(((x) >> NVME_CAP_HI_REG_PMRS_SHIFT) & NVME_CAP_HI_REG_PMRS_MASK)
79 #define NVME_CAP_HI_CMBS(x)						\
80 	(((x) >> NVME_CAP_HI_REG_CMBS_SHIFT) & NVME_CAP_HI_REG_CMBS_MASK)
81 
82 #define NVME_CC_REG_EN_SHIFT		(0)
83 #define NVME_CC_REG_EN_MASK		(0x1)
84 #define NVME_CC_REG_CSS_SHIFT		(4)
85 #define NVME_CC_REG_CSS_MASK		(0x7)
86 #define NVME_CC_REG_MPS_SHIFT		(7)
87 #define NVME_CC_REG_MPS_MASK		(0xF)
88 #define NVME_CC_REG_AMS_SHIFT		(11)
89 #define NVME_CC_REG_AMS_MASK		(0x7)
90 #define NVME_CC_REG_SHN_SHIFT		(14)
91 #define NVME_CC_REG_SHN_MASK		(0x3)
92 #define NVME_CC_REG_IOSQES_SHIFT	(16)
93 #define NVME_CC_REG_IOSQES_MASK		(0xF)
94 #define NVME_CC_REG_IOCQES_SHIFT	(20)
95 #define NVME_CC_REG_IOCQES_MASK		(0xF)
96 
97 #define NVME_CSTS_REG_RDY_SHIFT		(0)
98 #define NVME_CSTS_REG_RDY_MASK		(0x1)
99 #define NVME_CSTS_REG_CFS_SHIFT		(1)
100 #define NVME_CSTS_REG_CFS_MASK		(0x1)
101 #define NVME_CSTS_REG_SHST_SHIFT	(2)
102 #define NVME_CSTS_REG_SHST_MASK		(0x3)
103 #define NVME_CSTS_REG_NVSRO_SHIFT	(4)
104 #define NVME_CSTS_REG_NVSRO_MASK	(0x1)
105 #define NVME_CSTS_REG_PP_SHIFT		(5)
106 #define NVME_CSTS_REG_PP_MASK		(0x1)
107 
108 #define NVME_CSTS_GET_SHST(csts)					\
109 	(((csts) >> NVME_CSTS_REG_SHST_SHIFT) & NVME_CSTS_REG_SHST_MASK)
110 
111 #define NVME_AQA_REG_ASQS_SHIFT		(0)
112 #define NVME_AQA_REG_ASQS_MASK		(0xFFF)
113 #define NVME_AQA_REG_ACQS_SHIFT		(16)
114 #define NVME_AQA_REG_ACQS_MASK		(0xFFF)
115 
116 #define NVME_PMRCAP_REG_RDS_SHIFT	(3)
117 #define NVME_PMRCAP_REG_RDS_MASK	(0x1)
118 #define NVME_PMRCAP_REG_WDS_SHIFT	(4)
119 #define NVME_PMRCAP_REG_WDS_MASK	(0x1)
120 #define NVME_PMRCAP_REG_BIR_SHIFT	(5)
121 #define NVME_PMRCAP_REG_BIR_MASK	(0x7)
122 #define NVME_PMRCAP_REG_PMRTU_SHIFT	(8)
123 #define NVME_PMRCAP_REG_PMRTU_MASK	(0x3)
124 #define NVME_PMRCAP_REG_PMRWBM_SHIFT	(10)
125 #define NVME_PMRCAP_REG_PMRWBM_MASK	(0xf)
126 #define NVME_PMRCAP_REG_PMRTO_SHIFT	(16)
127 #define NVME_PMRCAP_REG_PMRTO_MASK	(0xff)
128 #define NVME_PMRCAP_REG_CMSS_SHIFT	(24)
129 #define NVME_PMRCAP_REG_CMSS_MASK	(0x1)
130 
131 #define NVME_PMRCAP_RDS(x)						\
132 	(((x) >> NVME_PMRCAP_REG_RDS_SHIFT) & NVME_PMRCAP_REG_RDS_MASK)
133 #define NVME_PMRCAP_WDS(x)						\
134 	(((x) >> NVME_PMRCAP_REG_WDS_SHIFT) & NVME_PMRCAP_REG_WDS_MASK)
135 #define NVME_PMRCAP_BIR(x)						\
136 	(((x) >> NVME_PMRCAP_REG_BIR_SHIFT) & NVME_PMRCAP_REG_BIR_MASK)
137 #define NVME_PMRCAP_PMRTU(x)						\
138 	(((x) >> NVME_PMRCAP_REG_PMRTU_SHIFT) & NVME_PMRCAP_REG_PMRTU_MASK)
139 #define NVME_PMRCAP_PMRWBM(x)						\
140 	(((x) >> NVME_PMRCAP_REG_PMRWBM_SHIFT) & NVME_PMRCAP_REG_PMRWBM_MASK)
141 #define NVME_PMRCAP_PMRTO(x)						\
142 	(((x) >> NVME_PMRCAP_REG_PMRTO_SHIFT) & NVME_PMRCAP_REG_PMRTO_MASK)
143 #define NVME_PMRCAP_CMSS(x)						\
144 	(((x) >> NVME_PMRCAP_REG_CMSS_SHIFT) & NVME_PMRCAP_REG_CMSS_MASK)
145 
146 /* Command field definitions */
147 
148 #define NVME_CMD_FUSE_SHIFT		(8)
149 #define NVME_CMD_FUSE_MASK		(0x3)
150 
151 #define NVME_STATUS_P_SHIFT		(0)
152 #define NVME_STATUS_P_MASK		(0x1)
153 #define NVME_STATUS_SC_SHIFT		(1)
154 #define NVME_STATUS_SC_MASK		(0xFF)
155 #define NVME_STATUS_SCT_SHIFT		(9)
156 #define NVME_STATUS_SCT_MASK		(0x7)
157 #define NVME_STATUS_CRD_SHIFT		(12)
158 #define NVME_STATUS_CRD_MASK		(0x3)
159 #define NVME_STATUS_M_SHIFT		(14)
160 #define NVME_STATUS_M_MASK		(0x1)
161 #define NVME_STATUS_DNR_SHIFT		(15)
162 #define NVME_STATUS_DNR_MASK		(0x1)
163 
164 #define NVME_STATUS_GET_P(st)					\
165 	(((st) >> NVME_STATUS_P_SHIFT) & NVME_STATUS_P_MASK)
166 #define NVME_STATUS_GET_SC(st)					\
167 	(((st) >> NVME_STATUS_SC_SHIFT) & NVME_STATUS_SC_MASK)
168 #define NVME_STATUS_GET_SCT(st)					\
169 	(((st) >> NVME_STATUS_SCT_SHIFT) & NVME_STATUS_SCT_MASK)
170 #define NVME_STATUS_GET_CRD(st)					\
171 	(((st) >> NVME_STATUS_CRD_SHIFT) & NVME_STATUS_CRD_MASK)
172 #define NVME_STATUS_GET_M(st)					\
173 	(((st) >> NVME_STATUS_M_SHIFT) & NVME_STATUS_M_MASK)
174 #define NVME_STATUS_GET_DNR(st)					\
175 	(((st) >> NVME_STATUS_DNR_SHIFT) & NVME_STATUS_DNR_MASK)
176 
177 /** Controller Multi-path I/O and Namespace Sharing Capabilities */
178 /* More then one port */
179 #define NVME_CTRLR_DATA_MIC_MPORTS_SHIFT		(0)
180 #define NVME_CTRLR_DATA_MIC_MPORTS_MASK			(0x1)
181 /* More then one controller */
182 #define NVME_CTRLR_DATA_MIC_MCTRLRS_SHIFT		(1)
183 #define NVME_CTRLR_DATA_MIC_MCTRLRS_MASK		(0x1)
184 /* SR-IOV Virtual Function */
185 #define NVME_CTRLR_DATA_MIC_SRIOVVF_SHIFT		(2)
186 #define NVME_CTRLR_DATA_MIC_SRIOVVF_MASK		(0x1)
187 /* Asymmetric Namespace Access Reporting */
188 #define NVME_CTRLR_DATA_MIC_ANAR_SHIFT			(3)
189 #define NVME_CTRLR_DATA_MIC_ANAR_MASK			(0x1)
190 
191 /** OAES - Optional Asynchronous Events Supported */
192 /* supports Namespace Attribute Notices event */
193 #define NVME_CTRLR_DATA_OAES_NS_ATTR_SHIFT		(8)
194 #define NVME_CTRLR_DATA_OAES_NS_ATTR_MASK		(0x1)
195 /* supports Firmware Activation Notices event */
196 #define NVME_CTRLR_DATA_OAES_FW_ACTIVATE_SHIFT		(9)
197 #define NVME_CTRLR_DATA_OAES_FW_ACTIVATE_MASK		(0x1)
198 /* supports Asymmetric Namespace Access Change Notices event */
199 #define NVME_CTRLR_DATA_OAES_ASYM_NS_CHANGE_SHIFT	(11)
200 #define NVME_CTRLR_DATA_OAES_ASYM_NS_CHANGE_MASK	(0x1)
201 /* supports Predictable Latency Event Aggregate Log Change Notices event */
202 #define NVME_CTRLR_DATA_OAES_PREDICT_LATENCY_SHIFT	(12)
203 #define NVME_CTRLR_DATA_OAES_PREDICT_LATENCY_MASK	(0x1)
204 /* supports LBA Status Information Notices event */
205 #define NVME_CTRLR_DATA_OAES_LBA_STATUS_SHIFT		(13)
206 #define NVME_CTRLR_DATA_OAES_LBA_STATUS_MASK		(0x1)
207 /* supports Endurance Group Event Aggregate Log Page Changes Notices event */
208 #define NVME_CTRLR_DATA_OAES_ENDURANCE_GROUP_SHIFT	(14)
209 #define NVME_CTRLR_DATA_OAES_ENDURANCE_GROUP_MASK	(0x1)
210 /* supports Normal NVM Subsystem Shutdown event */
211 #define NVME_CTRLR_DATA_OAES_NORMAL_SHUTDOWN_SHIFT	(15)
212 #define NVME_CTRLR_DATA_OAES_NORMAL_SHUTDOWN_MASK	(0x1)
213 /* supports Zone Descriptor Changed Notices event */
214 #define NVME_CTRLR_DATA_OAES_ZONE_DESC_CHANGE_SHIFT	(27)
215 #define NVME_CTRLR_DATA_OAES_ZONE_DESC_CHANGE_MASK	(0x1)
216 /* supports Discovery Log Page Change Notification event */
217 #define NVME_CTRLR_DATA_OAES_LOG_PAGE_CHANGE_SHIFT	(31)
218 #define NVME_CTRLR_DATA_OAES_LOG_PAGE_CHANGE_MASK	(0x1)
219 
220 /** OACS - optional admin command support */
221 /* supports security send/receive commands */
222 #define NVME_CTRLR_DATA_OACS_SECURITY_SHIFT		(0)
223 #define NVME_CTRLR_DATA_OACS_SECURITY_MASK		(0x1)
224 /* supports format nvm command */
225 #define NVME_CTRLR_DATA_OACS_FORMAT_SHIFT		(1)
226 #define NVME_CTRLR_DATA_OACS_FORMAT_MASK		(0x1)
227 /* supports firmware activate/download commands */
228 #define NVME_CTRLR_DATA_OACS_FIRMWARE_SHIFT		(2)
229 #define NVME_CTRLR_DATA_OACS_FIRMWARE_MASK		(0x1)
230 /* supports namespace management commands */
231 #define NVME_CTRLR_DATA_OACS_NSMGMT_SHIFT		(3)
232 #define NVME_CTRLR_DATA_OACS_NSMGMT_MASK		(0x1)
233 /* supports Device Self-test command */
234 #define NVME_CTRLR_DATA_OACS_SELFTEST_SHIFT		(4)
235 #define NVME_CTRLR_DATA_OACS_SELFTEST_MASK		(0x1)
236 /* supports Directives */
237 #define NVME_CTRLR_DATA_OACS_DIRECTIVES_SHIFT		(5)
238 #define NVME_CTRLR_DATA_OACS_DIRECTIVES_MASK		(0x1)
239 /* supports NVMe-MI Send/Receive */
240 #define NVME_CTRLR_DATA_OACS_NVMEMI_SHIFT		(6)
241 #define NVME_CTRLR_DATA_OACS_NVMEMI_MASK		(0x1)
242 /* supports Virtualization Management */
243 #define NVME_CTRLR_DATA_OACS_VM_SHIFT			(7)
244 #define NVME_CTRLR_DATA_OACS_VM_MASK			(0x1)
245 /* supports Doorbell Buffer Config */
246 #define NVME_CTRLR_DATA_OACS_DBBUFFER_SHIFT		(8)
247 #define NVME_CTRLR_DATA_OACS_DBBUFFER_MASK		(0x1)
248 /* supports Get LBA Status */
249 #define NVME_CTRLR_DATA_OACS_GETLBA_SHIFT		(9)
250 #define NVME_CTRLR_DATA_OACS_GETLBA_MASK		(0x1)
251 
252 /** firmware updates */
253 /* first slot is read-only */
254 #define NVME_CTRLR_DATA_FRMW_SLOT1_RO_SHIFT		(0)
255 #define NVME_CTRLR_DATA_FRMW_SLOT1_RO_MASK		(0x1)
256 /* number of firmware slots */
257 #define NVME_CTRLR_DATA_FRMW_NUM_SLOTS_SHIFT		(1)
258 #define NVME_CTRLR_DATA_FRMW_NUM_SLOTS_MASK		(0x7)
259 /* firmware activation without reset */
260 #define NVME_CTRLR_DATA_FRMW_ACT_WO_RESET_SHIFT		(4)
261 #define NVME_CTRLR_DATA_FRMW_ACT_WO_RESET_MASK		(0x1)
262 
263 /** log page attributes */
264 /* per namespace smart/health log page */
265 #define NVME_CTRLR_DATA_LPA_NS_SMART_SHIFT		(0)
266 #define NVME_CTRLR_DATA_LPA_NS_SMART_MASK		(0x1)
267 
268 /** AVSCC - admin vendor specific command configuration */
269 /* admin vendor specific commands use spec format */
270 #define NVME_CTRLR_DATA_AVSCC_SPEC_FORMAT_SHIFT		(0)
271 #define NVME_CTRLR_DATA_AVSCC_SPEC_FORMAT_MASK		(0x1)
272 
273 /** Autonomous Power State Transition Attributes */
274 /* Autonomous Power State Transitions supported */
275 #define NVME_CTRLR_DATA_APSTA_APST_SUPP_SHIFT		(0)
276 #define NVME_CTRLR_DATA_APSTA_APST_SUPP_MASK		(0x1)
277 
278 /** Sanitize Capabilities */
279 /* Crypto Erase Support  */
280 #define NVME_CTRLR_DATA_SANICAP_CES_SHIFT		(0)
281 #define NVME_CTRLR_DATA_SANICAP_CES_MASK		(0x1)
282 /* Block Erase Support */
283 #define NVME_CTRLR_DATA_SANICAP_BES_SHIFT		(1)
284 #define NVME_CTRLR_DATA_SANICAP_BES_MASK		(0x1)
285 /* Overwrite Support */
286 #define NVME_CTRLR_DATA_SANICAP_OWS_SHIFT		(2)
287 #define NVME_CTRLR_DATA_SANICAP_OWS_MASK		(0x1)
288 /* No-Deallocate Inhibited  */
289 #define NVME_CTRLR_DATA_SANICAP_NDI_SHIFT		(29)
290 #define NVME_CTRLR_DATA_SANICAP_NDI_MASK		(0x1)
291 /* No-Deallocate Modifies Media After Sanitize */
292 #define NVME_CTRLR_DATA_SANICAP_NODMMAS_SHIFT		(30)
293 #define NVME_CTRLR_DATA_SANICAP_NODMMAS_MASK		(0x3)
294 #define NVME_CTRLR_DATA_SANICAP_NODMMAS_UNDEF		(0)
295 #define NVME_CTRLR_DATA_SANICAP_NODMMAS_NO		(1)
296 #define NVME_CTRLR_DATA_SANICAP_NODMMAS_YES		(2)
297 
298 /** submission queue entry size */
299 #define NVME_CTRLR_DATA_SQES_MIN_SHIFT			(0)
300 #define NVME_CTRLR_DATA_SQES_MIN_MASK			(0xF)
301 #define NVME_CTRLR_DATA_SQES_MAX_SHIFT			(4)
302 #define NVME_CTRLR_DATA_SQES_MAX_MASK			(0xF)
303 
304 /** completion queue entry size */
305 #define NVME_CTRLR_DATA_CQES_MIN_SHIFT			(0)
306 #define NVME_CTRLR_DATA_CQES_MIN_MASK			(0xF)
307 #define NVME_CTRLR_DATA_CQES_MAX_SHIFT			(4)
308 #define NVME_CTRLR_DATA_CQES_MAX_MASK			(0xF)
309 
310 /** optional nvm command support */
311 #define NVME_CTRLR_DATA_ONCS_COMPARE_SHIFT		(0)
312 #define NVME_CTRLR_DATA_ONCS_COMPARE_MASK		(0x1)
313 #define NVME_CTRLR_DATA_ONCS_WRITE_UNC_SHIFT		(1)
314 #define NVME_CTRLR_DATA_ONCS_WRITE_UNC_MASK		(0x1)
315 #define NVME_CTRLR_DATA_ONCS_DSM_SHIFT			(2)
316 #define NVME_CTRLR_DATA_ONCS_DSM_MASK			(0x1)
317 #define NVME_CTRLR_DATA_ONCS_WRZERO_SHIFT		(3)
318 #define NVME_CTRLR_DATA_ONCS_WRZERO_MASK		(0x1)
319 #define NVME_CTRLR_DATA_ONCS_SAVEFEAT_SHIFT		(4)
320 #define NVME_CTRLR_DATA_ONCS_SAVEFEAT_MASK		(0x1)
321 #define NVME_CTRLR_DATA_ONCS_RESERV_SHIFT		(5)
322 #define NVME_CTRLR_DATA_ONCS_RESERV_MASK		(0x1)
323 #define NVME_CTRLR_DATA_ONCS_TIMESTAMP_SHIFT		(6)
324 #define NVME_CTRLR_DATA_ONCS_TIMESTAMP_MASK		(0x1)
325 #define NVME_CTRLR_DATA_ONCS_VERIFY_SHIFT		(7)
326 #define NVME_CTRLR_DATA_ONCS_VERIFY_MASK		(0x1)
327 
328 /** Fused Operation Support */
329 #define NVME_CTRLR_DATA_FUSES_CNW_SHIFT		(0)
330 #define NVME_CTRLR_DATA_FUSES_CNW_MASK		(0x1)
331 
332 /** Format NVM Attributes */
333 #define NVME_CTRLR_DATA_FNA_FORMAT_ALL_SHIFT		(0)
334 #define NVME_CTRLR_DATA_FNA_FORMAT_ALL_MASK		(0x1)
335 #define NVME_CTRLR_DATA_FNA_ERASE_ALL_SHIFT		(1)
336 #define NVME_CTRLR_DATA_FNA_ERASE_ALL_MASK		(0x1)
337 #define NVME_CTRLR_DATA_FNA_CRYPTO_ERASE_SHIFT		(2)
338 #define NVME_CTRLR_DATA_FNA_CRYPTO_ERASE_MASK		(0x1)
339 
340 /** volatile write cache */
341 /* volatile write cache present */
342 #define NVME_CTRLR_DATA_VWC_PRESENT_SHIFT		(0)
343 #define NVME_CTRLR_DATA_VWC_PRESENT_MASK		(0x1)
344 /* flush all namespaces supported */
345 #define NVME_CTRLR_DATA_VWC_ALL_SHIFT			(1)
346 #define NVME_CTRLR_DATA_VWC_ALL_MASK			(0x3)
347 #define NVME_CTRLR_DATA_VWC_ALL_UNKNOWN			(0)
348 #define NVME_CTRLR_DATA_VWC_ALL_NO			(2)
349 #define NVME_CTRLR_DATA_VWC_ALL_YES			(3)
350 
351 /** namespace features */
352 /* thin provisioning */
353 #define NVME_NS_DATA_NSFEAT_THIN_PROV_SHIFT		(0)
354 #define NVME_NS_DATA_NSFEAT_THIN_PROV_MASK		(0x1)
355 /* NAWUN, NAWUPF, and NACWU fields are valid */
356 #define NVME_NS_DATA_NSFEAT_NA_FIELDS_SHIFT		(1)
357 #define NVME_NS_DATA_NSFEAT_NA_FIELDS_MASK		(0x1)
358 /* Deallocated or Unwritten Logical Block errors supported */
359 #define NVME_NS_DATA_NSFEAT_DEALLOC_SHIFT		(2)
360 #define NVME_NS_DATA_NSFEAT_DEALLOC_MASK		(0x1)
361 /* NGUID and EUI64 fields are not reusable */
362 #define NVME_NS_DATA_NSFEAT_NO_ID_REUSE_SHIFT		(3)
363 #define NVME_NS_DATA_NSFEAT_NO_ID_REUSE_MASK		(0x1)
364 /* NPWG, NPWA, NPDG, NPDA, and NOWS are valid */
365 #define NVME_NS_DATA_NSFEAT_NPVALID_SHIFT		(4)
366 #define NVME_NS_DATA_NSFEAT_NPVALID_MASK		(0x1)
367 
368 /** formatted lba size */
369 #define NVME_NS_DATA_FLBAS_FORMAT_SHIFT			(0)
370 #define NVME_NS_DATA_FLBAS_FORMAT_MASK			(0xF)
371 #define NVME_NS_DATA_FLBAS_EXTENDED_SHIFT		(4)
372 #define NVME_NS_DATA_FLBAS_EXTENDED_MASK		(0x1)
373 
374 /** metadata capabilities */
375 /* metadata can be transferred as part of data prp list */
376 #define NVME_NS_DATA_MC_EXTENDED_SHIFT			(0)
377 #define NVME_NS_DATA_MC_EXTENDED_MASK			(0x1)
378 /* metadata can be transferred with separate metadata pointer */
379 #define NVME_NS_DATA_MC_POINTER_SHIFT			(1)
380 #define NVME_NS_DATA_MC_POINTER_MASK			(0x1)
381 
382 /** end-to-end data protection capabilities */
383 /* protection information type 1 */
384 #define NVME_NS_DATA_DPC_PIT1_SHIFT			(0)
385 #define NVME_NS_DATA_DPC_PIT1_MASK			(0x1)
386 /* protection information type 2 */
387 #define NVME_NS_DATA_DPC_PIT2_SHIFT			(1)
388 #define NVME_NS_DATA_DPC_PIT2_MASK			(0x1)
389 /* protection information type 3 */
390 #define NVME_NS_DATA_DPC_PIT3_SHIFT			(2)
391 #define NVME_NS_DATA_DPC_PIT3_MASK			(0x1)
392 /* first eight bytes of metadata */
393 #define NVME_NS_DATA_DPC_MD_START_SHIFT			(3)
394 #define NVME_NS_DATA_DPC_MD_START_MASK			(0x1)
395 /* last eight bytes of metadata */
396 #define NVME_NS_DATA_DPC_MD_END_SHIFT			(4)
397 #define NVME_NS_DATA_DPC_MD_END_MASK			(0x1)
398 
399 /** end-to-end data protection type settings */
400 /* protection information type */
401 #define NVME_NS_DATA_DPS_PIT_SHIFT			(0)
402 #define NVME_NS_DATA_DPS_PIT_MASK			(0x7)
403 /* 1 == protection info transferred at start of metadata */
404 /* 0 == protection info transferred at end of metadata */
405 #define NVME_NS_DATA_DPS_MD_START_SHIFT			(3)
406 #define NVME_NS_DATA_DPS_MD_START_MASK			(0x1)
407 
408 /** Namespace Multi-path I/O and Namespace Sharing Capabilities */
409 /* the namespace may be attached to two or more controllers */
410 #define NVME_NS_DATA_NMIC_MAY_BE_SHARED_SHIFT		(0)
411 #define NVME_NS_DATA_NMIC_MAY_BE_SHARED_MASK		(0x1)
412 
413 /** Reservation Capabilities */
414 /* Persist Through Power Loss */
415 #define NVME_NS_DATA_RESCAP_PTPL_SHIFT		(0)
416 #define NVME_NS_DATA_RESCAP_PTPL_MASK		(0x1)
417 /* supports the Write Exclusive */
418 #define NVME_NS_DATA_RESCAP_WR_EX_SHIFT		(1)
419 #define NVME_NS_DATA_RESCAP_WR_EX_MASK		(0x1)
420 /* supports the Exclusive Access */
421 #define NVME_NS_DATA_RESCAP_EX_AC_SHIFT		(2)
422 #define NVME_NS_DATA_RESCAP_EX_AC_MASK		(0x1)
423 /* supports the Write Exclusive – Registrants Only */
424 #define NVME_NS_DATA_RESCAP_WR_EX_RO_SHIFT	(3)
425 #define NVME_NS_DATA_RESCAP_WR_EX_RO_MASK	(0x1)
426 /* supports the Exclusive Access - Registrants Only */
427 #define NVME_NS_DATA_RESCAP_EX_AC_RO_SHIFT	(4)
428 #define NVME_NS_DATA_RESCAP_EX_AC_RO_MASK	(0x1)
429 /* supports the Write Exclusive – All Registrants */
430 #define NVME_NS_DATA_RESCAP_WR_EX_AR_SHIFT	(5)
431 #define NVME_NS_DATA_RESCAP_WR_EX_AR_MASK	(0x1)
432 /* supports the Exclusive Access - All Registrants */
433 #define NVME_NS_DATA_RESCAP_EX_AC_AR_SHIFT	(6)
434 #define NVME_NS_DATA_RESCAP_EX_AC_AR_MASK	(0x1)
435 /* Ignore Existing Key is used as defined in revision 1.3 or later */
436 #define NVME_NS_DATA_RESCAP_IEKEY13_SHIFT	(7)
437 #define NVME_NS_DATA_RESCAP_IEKEY13_MASK	(0x1)
438 
439 /** Format Progress Indicator */
440 /* percentage of the Format NVM command that remains to be completed */
441 #define NVME_NS_DATA_FPI_PERC_SHIFT		(0)
442 #define NVME_NS_DATA_FPI_PERC_MASK		(0x7f)
443 /* namespace supports the Format Progress Indicator */
444 #define NVME_NS_DATA_FPI_SUPP_SHIFT		(7)
445 #define NVME_NS_DATA_FPI_SUPP_MASK		(0x1)
446 
447 /** Deallocate Logical Block Features */
448 /* deallocated logical block read behavior */
449 #define NVME_NS_DATA_DLFEAT_READ_SHIFT		(0)
450 #define NVME_NS_DATA_DLFEAT_READ_MASK		(0x07)
451 #define NVME_NS_DATA_DLFEAT_READ_NR		(0x00)
452 #define NVME_NS_DATA_DLFEAT_READ_00		(0x01)
453 #define NVME_NS_DATA_DLFEAT_READ_FF		(0x02)
454 /* supports the Deallocate bit in the Write Zeroes */
455 #define NVME_NS_DATA_DLFEAT_DWZ_SHIFT		(3)
456 #define NVME_NS_DATA_DLFEAT_DWZ_MASK		(0x01)
457 /* Guard field for deallocated logical blocks is set to the CRC  */
458 #define NVME_NS_DATA_DLFEAT_GCRC_SHIFT		(4)
459 #define NVME_NS_DATA_DLFEAT_GCRC_MASK		(0x01)
460 
461 /** lba format support */
462 /* metadata size */
463 #define NVME_NS_DATA_LBAF_MS_SHIFT		(0)
464 #define NVME_NS_DATA_LBAF_MS_MASK		(0xFFFF)
465 /* lba data size */
466 #define NVME_NS_DATA_LBAF_LBADS_SHIFT		(16)
467 #define NVME_NS_DATA_LBAF_LBADS_MASK		(0xFF)
468 /* relative performance */
469 #define NVME_NS_DATA_LBAF_RP_SHIFT		(24)
470 #define NVME_NS_DATA_LBAF_RP_MASK		(0x3)
471 
472 enum nvme_critical_warning_state {
473 	NVME_CRIT_WARN_ST_AVAILABLE_SPARE		= 0x1,
474 	NVME_CRIT_WARN_ST_TEMPERATURE			= 0x2,
475 	NVME_CRIT_WARN_ST_DEVICE_RELIABILITY		= 0x4,
476 	NVME_CRIT_WARN_ST_READ_ONLY			= 0x8,
477 	NVME_CRIT_WARN_ST_VOLATILE_MEMORY_BACKUP	= 0x10,
478 };
479 #define NVME_CRIT_WARN_ST_RESERVED_MASK		(0xE0)
480 #define	NVME_ASYNC_EVENT_NS_ATTRIBUTE		(0x100)
481 #define	NVME_ASYNC_EVENT_FW_ACTIVATE		(0x200)
482 
483 
484 /* Helper macro to combine *_MASK and *_SHIFT defines */
485 #define NVMEB(name)	(name##_MASK << name##_SHIFT)
486 
487 /* CC register SHN field values */
488 enum shn_value {
489 	NVME_SHN_NORMAL		= 0x1,
490 	NVME_SHN_ABRUPT		= 0x2,
491 };
492 
493 /* CSTS register SHST field values */
494 enum shst_value {
495 	NVME_SHST_NORMAL	= 0x0,
496 	NVME_SHST_OCCURRING	= 0x1,
497 	NVME_SHST_COMPLETE	= 0x2,
498 };
499 
500 #define nvme_mmio_offsetof(reg)						\
501 	offsetof(struct nvme_registers, reg)
502 
503 #define nvme_mmio_read_4(b_a, reg)					\
504 	sys_read32((mm_reg_t)b_a +  nvme_mmio_offsetof(reg))
505 
506 #define nvme_mmio_write_4(b_a, reg, val)				\
507 	sys_write32(val, (mm_reg_t)b_a + nvme_mmio_offsetof(reg))
508 
509 #define nvme_mmio_write_8(b_a, reg, val)				\
510 	do {								\
511 		sys_write32(val & 0xFFFFFFFF,				\
512 			    (mm_reg_t)b_a + nvme_mmio_offsetof(reg));	\
513 		sys_write32((val & 0xFFFFFFFF00000000ULL) >> 32,	\
514 			    (mm_reg_t)b_a + nvme_mmio_offsetof(reg) + 4); \
515 	} while (0)
516 
517 #define NVME_IS_BUFFER_DWORD_ALIGNED(_buf_addr) (!((uintptr_t)_buf_addr & 0x3))
518 
519 #endif /* ZEPHYR_DRIVERS_DISK_NVME_NHME_HELPERS_H_ */
520