nvme.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-02-25 GuEe-GUI the first version
  9. */
  10. #ifndef __NVME_H__
  11. #define __NVME_H__
  12. #include <rthw.h>
  13. #include <rtthread.h>
  14. #include <drivers/blk.h>
  15. #define NVME_RSVD(offset, bytes_size) rt_uint8_t __rsvd##offset[bytes_size]
  16. enum
  17. {
  18. /*
  19. * Generic Command Status:
  20. */
  21. RT_NVME_SC_SUCCESS = 0x0,
  22. RT_NVME_SC_INVALID_OPCODE = 0x1,
  23. RT_NVME_SC_INVALID_FIELD = 0x2,
  24. RT_NVME_SC_CMDID_CONFLICT = 0x3,
  25. RT_NVME_SC_DATA_XFER_ERROR = 0x4,
  26. RT_NVME_SC_POWER_LOSS = 0x5,
  27. RT_NVME_SC_INTERNAL = 0x6,
  28. RT_NVME_SC_ABORT_REQ = 0x7,
  29. RT_NVME_SC_ABORT_QUEUE = 0x8,
  30. RT_NVME_SC_FUSED_FAIL = 0x9,
  31. RT_NVME_SC_FUSED_MISSING = 0xa,
  32. RT_NVME_SC_INVALID_NS = 0xb,
  33. RT_NVME_SC_CMD_SEQ_ERROR = 0xc,
  34. RT_NVME_SC_SGL_INVALID_LAST = 0xd,
  35. RT_NVME_SC_SGL_INVALID_COUNT = 0xe,
  36. RT_NVME_SC_SGL_INVALID_DATA = 0xf,
  37. RT_NVME_SC_SGL_INVALID_METADATA = 0x10,
  38. RT_NVME_SC_SGL_INVALID_TYPE = 0x11,
  39. RT_NVME_SC_CMB_INVALID_USE = 0x12,
  40. RT_NVME_SC_PRP_INVALID_OFFSET = 0x13,
  41. RT_NVME_SC_ATOMIC_WU_EXCEEDED = 0x14,
  42. RT_NVME_SC_OP_DENIED = 0x15,
  43. RT_NVME_SC_SGL_INVALID_OFFSET = 0x16,
  44. RT_NVME_SC_RESERVED = 0x17,
  45. RT_NVME_SC_HOST_ID_INCONSIST = 0x18,
  46. RT_NVME_SC_KA_TIMEOUT_EXPIRED = 0x19,
  47. RT_NVME_SC_KA_TIMEOUT_INVALID = 0x1a,
  48. RT_NVME_SC_ABORTED_PREEMPT_ABORT = 0x1b,
  49. RT_NVME_SC_SANITIZE_FAILED = 0x1c,
  50. RT_NVME_SC_SANITIZE_IN_PROGRESS = 0x1d,
  51. RT_NVME_SC_SGL_INVALID_GRANULARITY = 0x1e,
  52. RT_NVME_SC_CMD_NOT_SUP_CMB_QUEUE = 0x1f,
  53. RT_NVME_SC_NS_WRITE_PROTECTED = 0x20,
  54. RT_NVME_SC_CMD_INTERRUPTED = 0x21,
  55. RT_NVME_SC_TRANSIENT_TR_ERR = 0x22,
  56. RT_NVME_SC_ADMIN_COMMAND_MEDIA_NOT_READY = 0x24,
  57. RT_NVME_SC_INVALID_IO_CMD_SET = 0x2c,
  58. RT_NVME_SC_LBA_RANGE = 0x80,
  59. RT_NVME_SC_CAP_EXCEEDED = 0x81,
  60. RT_NVME_SC_NS_NOT_READY = 0x82,
  61. RT_NVME_SC_RESERVATION_CONFLICT = 0x83,
  62. RT_NVME_SC_FORMAT_IN_PROGRESS = 0x84,
  63. /*
  64. * Command Specific Status:
  65. */
  66. RT_NVME_SC_CQ_INVALID = 0x100,
  67. RT_NVME_SC_QID_INVALID = 0x101,
  68. RT_NVME_SC_QUEUE_SIZE = 0x102,
  69. RT_NVME_SC_ABORT_LIMIT = 0x103,
  70. RT_NVME_SC_ABORT_MISSING = 0x104,
  71. RT_NVME_SC_ASYNC_LIMIT = 0x105,
  72. RT_NVME_SC_FIRMWARE_SLOT = 0x106,
  73. RT_NVME_SC_FIRMWARE_IMAGE = 0x107,
  74. RT_NVME_SC_INVALID_VECTOR = 0x108,
  75. RT_NVME_SC_INVALID_LOG_PAGE = 0x109,
  76. RT_NVME_SC_INVALID_FORMAT = 0x10a,
  77. RT_NVME_SC_FW_NEEDS_CONV_RESET = 0x10b,
  78. RT_NVME_SC_INVALID_QUEUE = 0x10c,
  79. RT_NVME_SC_FEATURE_NOT_SAVEABLE = 0x10d,
  80. RT_NVME_SC_FEATURE_NOT_CHANGEABLE = 0x10e,
  81. RT_NVME_SC_FEATURE_NOT_PER_NS = 0x10f,
  82. RT_NVME_SC_FW_NEEDS_SUBSYS_RESET = 0x110,
  83. RT_NVME_SC_FW_NEEDS_RESET = 0x111,
  84. RT_NVME_SC_FW_NEEDS_MAX_TIME = 0x112,
  85. RT_NVME_SC_FW_ACTIVATE_PROHIBITED = 0x113,
  86. RT_NVME_SC_OVERLAPPING_RANGE = 0x114,
  87. RT_NVME_SC_NS_INSUFFICIENT_CAP = 0x115,
  88. RT_NVME_SC_NS_ID_UNAVAILABLE = 0x116,
  89. RT_NVME_SC_NS_ALREADY_ATTACHED = 0x118,
  90. RT_NVME_SC_NS_IS_PRIVATE = 0x119,
  91. RT_NVME_SC_NS_NOT_ATTACHED = 0x11a,
  92. RT_NVME_SC_THIN_PROV_NOT_SUPP = 0x11b,
  93. RT_NVME_SC_CTRL_LIST_INVALID = 0x11c,
  94. RT_NVME_SC_SELT_TEST_IN_PROGRESS = 0x11d,
  95. RT_NVME_SC_BP_WRITE_PROHIBITED = 0x11e,
  96. RT_NVME_SC_CTRL_ID_INVALID = 0x11f,
  97. RT_NVME_SC_SEC_CTRL_STATE_INVALID = 0x120,
  98. RT_NVME_SC_CTRL_RES_NUM_INVALID = 0x121,
  99. RT_NVME_SC_RES_ID_INVALID = 0x122,
  100. RT_NVME_SC_PMR_SAN_PROHIBITED = 0x123,
  101. RT_NVME_SC_ANA_GROUP_ID_INVALID = 0x124,
  102. RT_NVME_SC_ANA_ATTACH_FAILED = 0x125,
  103. /*
  104. * I/O Command Set Specific - NVM commands:
  105. */
  106. RT_NVME_SC_BAD_ATTRIBUTES = 0x180,
  107. RT_NVME_SC_INVALID_PI = 0x181,
  108. RT_NVME_SC_READ_ONLY = 0x182,
  109. RT_NVME_SC_ONCS_NOT_SUPPORTED = 0x183,
  110. /*
  111. * I/O Command Set Specific - Fabrics commands:
  112. */
  113. RT_NVME_SC_CONNECT_FORMAT = 0x180,
  114. RT_NVME_SC_CONNECT_CTRL_BUSY = 0x181,
  115. RT_NVME_SC_CONNECT_INVALID_PARAM = 0x182,
  116. RT_NVME_SC_CONNECT_RESTART_DISC = 0x183,
  117. RT_NVME_SC_CONNECT_INVALID_HOST = 0x184,
  118. RT_NVME_SC_DISCOVERY_RESTART = 0x190,
  119. RT_NVME_SC_AUTH_REQUIRED = 0x191,
  120. /*
  121. * I/O Command Set Specific - Zoned commands:
  122. */
  123. RT_NVME_SC_ZONE_BOUNDARY_ERROR = 0x1b8,
  124. RT_NVME_SC_ZONE_FULL = 0x1b9,
  125. RT_NVME_SC_ZONE_READ_ONLY = 0x1ba,
  126. RT_NVME_SC_ZONE_OFFLINE = 0x1bb,
  127. RT_NVME_SC_ZONE_INVALID_WRITE = 0x1bc,
  128. RT_NVME_SC_ZONE_TOO_MANY_ACTIVE = 0x1bd,
  129. RT_NVME_SC_ZONE_TOO_MANY_OPEN = 0x1be,
  130. RT_NVME_SC_ZONE_INVALID_TRANSITION = 0x1bf,
  131. /*
  132. * Media and Data Integrity Errors:
  133. */
  134. RT_NVME_SC_WRITE_FAULT = 0x280,
  135. RT_NVME_SC_READ_ERROR = 0x281,
  136. RT_NVME_SC_GUARD_CHECK = 0x282,
  137. RT_NVME_SC_APPTAG_CHECK = 0x283,
  138. RT_NVME_SC_REFTAG_CHECK = 0x284,
  139. RT_NVME_SC_COMPARE_FAILED = 0x285,
  140. RT_NVME_SC_ACCESS_DENIED = 0x286,
  141. RT_NVME_SC_UNWRITTEN_BLOCK = 0x287,
  142. /*
  143. * Path-related Errors:
  144. */
  145. RT_NVME_SC_INTERNAL_PATH_ERROR = 0x300,
  146. RT_NVME_SC_ANA_PERSISTENT_LOSS = 0x301,
  147. RT_NVME_SC_ANA_INACCESSIBLE = 0x302,
  148. RT_NVME_SC_ANA_TRANSITION = 0x303,
  149. RT_NVME_SC_CTRL_PATH_ERROR = 0x360,
  150. RT_NVME_SC_HOST_PATH_ERROR = 0x370,
  151. RT_NVME_SC_HOST_ABORTED_CMD = 0x371,
  152. RT_NVME_SC_CRD = 0x1800,
  153. RT_NVME_SC_MORE = 0x2000,
  154. RT_NVME_SC_DNR = 0x4000,
  155. };
  156. /* Admin commands */
  157. enum
  158. {
  159. RT_NVME_ADMIN_OPCODE_DELETE_SQ = 0x00,
  160. RT_NVME_ADMIN_OPCODE_CREATE_SQ = 0x01,
  161. RT_NVME_ADMIN_OPCODE_GET_LOG_PAGE = 0x02,
  162. RT_NVME_ADMIN_OPCODE_DELETE_CQ = 0x04,
  163. RT_NVME_ADMIN_OPCODE_CREATE_CQ = 0x05,
  164. RT_NVME_ADMIN_OPCODE_IDENTIFY = 0x06,
  165. RT_NVME_ADMIN_OPCODE_ABORT_CMD = 0x08,
  166. RT_NVME_ADMIN_OPCODE_SET_FEATURES = 0x09,
  167. RT_NVME_ADMIN_OPCODE_GET_FEATURES = 0x0a,
  168. RT_NVME_ADMIN_OPCODE_ASYNC_EVENT = 0x0c,
  169. RT_NVME_ADMIN_OPCODE_NS_MGMT = 0x0d,
  170. RT_NVME_ADMIN_OPCODE_ACTIVATE_FW = 0x10,
  171. RT_NVME_ADMIN_OPCODE_DOWNLOAD_FW = 0x11,
  172. RT_NVME_ADMIN_OPCODE_DEV_SELF_TEST = 0x14,
  173. RT_NVME_ADMIN_OPCODE_NS_ATTACH = 0x15,
  174. RT_NVME_ADMIN_OPCODE_KEEP_ALIVE = 0x18,
  175. RT_NVME_ADMIN_OPCODE_DIRECTIVE_SEND = 0x19,
  176. RT_NVME_ADMIN_OPCODE_DIRECTIVE_RECV = 0x1a,
  177. RT_NVME_ADMIN_OPCODE_VIRTUAL_MGMT = 0x1c,
  178. RT_NVME_ADMIN_OPCODE_NVME_MI_SEND = 0x1d,
  179. RT_NVME_ADMIN_OPCODE_NVME_MI_RECV = 0x1e,
  180. RT_NVME_ADMIN_OPCODE_DBBUF = 0x7c,
  181. RT_NVME_ADMIN_OPCODE_FORMAT_NVM = 0x80,
  182. RT_NVME_ADMIN_OPCODE_SECURITY_SEND = 0x81,
  183. RT_NVME_ADMIN_OPCODE_SECURITY_RECV = 0x82,
  184. RT_NVME_ADMIN_OPCODE_SANITIZE_NVM = 0x84,
  185. RT_NVME_ADMIN_OPCODE_GET_LBA_STATUS = 0x86,
  186. RT_NVME_ADMIN_OPCODE_VENDOR_START = 0xc0,
  187. };
  188. /* I/O commands */
  189. enum
  190. {
  191. RT_NVME_CMD_FLUSH = 0x00,
  192. RT_NVME_CMD_WRITE = 0x01,
  193. RT_NVME_CMD_READ = 0x02,
  194. RT_NVME_CMD_WRITE_UNCOR = 0x04,
  195. RT_NVME_CMD_COMPARE = 0x05,
  196. RT_NVME_CMD_WRITE_ZEROES = 0x08,
  197. RT_NVME_CMD_DSM = 0x09,
  198. RT_NVME_CMD_VERIFY = 0x0c,
  199. RT_NVME_CMD_RESV_REGISTER = 0x0d,
  200. RT_NVME_CMD_RESV_REPORT = 0x0e,
  201. RT_NVME_CMD_RESV_ACQUIRE = 0x11,
  202. RT_NVME_CMD_RESV_RELEASE = 0x15,
  203. RT_NVME_CMD_ZONE_MGMT_SEND = 0x79,
  204. RT_NVME_CMD_ZONE_MGMT_RECV = 0x7a,
  205. RT_NVME_CMD_ZONE_APPEND = 0x7d,
  206. RT_NVME_CMD_VENDOR_START = 0x80,
  207. };
  208. enum
  209. {
  210. RT_NVME_PSDT_PRP = 0x0,
  211. RT_NVME_PSDT_SGL_MPTR_CONTIGUOUS = 0x1,
  212. RT_NVME_PSDT_SGL_MPTR_SGL = 0x2,
  213. };
  214. /* Commands flags */
  215. enum
  216. {
  217. RT_NVME_CMD_FLAGS_FUSE_SHIFT = 0x00,
  218. RT_NVME_CMD_FLAGS_PSDT_SHIFT = 0x06,
  219. };
  220. struct rt_nvme_command_common
  221. {
  222. rt_uint8_t opcode;
  223. rt_uint8_t flags;
  224. rt_uint16_t cmdid;
  225. rt_le32_t nsid;
  226. rt_le32_t cmd_dw2[2];
  227. rt_le64_t metadata;
  228. rt_le64_t prp1;
  229. rt_le64_t prp2;
  230. rt_le32_t cmd_dw10[6];
  231. };
  232. rt_packed(struct rt_nvme_sgl_desc
  233. {
  234. rt_le64_t adddress;
  235. rt_le32_t length;
  236. rt_uint8_t reserved[3];
  237. #define SGL_DESC_TYPE_DATA_BLOCK 0x0
  238. #define SGL_DESC_TYPE_BIT_BUCKET 0x1
  239. #define SGL_DESC_TYPE_SEGMENT 0x2
  240. #define SGL_DESC_TYPE_LAST_SEGMENT 0x3
  241. #define SGL_DESC_TYPE_KEYED_DATA_BLOCK 0x4
  242. #define SGL_DESC_TYPE_VENDOR_SPECIFIC 0xf
  243. rt_uint8_t sgl_identify;
  244. });
  245. struct rt_nvme_command_rw
  246. {
  247. rt_uint8_t opcode;
  248. rt_uint8_t flags;
  249. rt_uint16_t cmdid;
  250. rt_le32_t nsid;
  251. NVME_RSVD(8, 8);
  252. rt_le64_t metadata;
  253. union
  254. {
  255. struct
  256. {
  257. rt_le64_t prp1;
  258. rt_le64_t prp2;
  259. };
  260. struct rt_nvme_sgl_desc sgl;
  261. };
  262. rt_le64_t slba;
  263. rt_le16_t length;
  264. rt_le16_t control;
  265. rt_le32_t dsmgmt;
  266. rt_le32_t reftag;
  267. rt_le16_t apptag;
  268. rt_le16_t appmask;
  269. };
  270. enum
  271. {
  272. RT_NVME_RW_LR = 1 << 15,
  273. RT_NVME_RW_FUA = 1 << 14,
  274. RT_NVME_RW_APPEND_PIREMAP = 1 << 9,
  275. RT_NVME_RW_DSM_FREQ_UNSPEC = 0,
  276. RT_NVME_RW_DSM_FREQ_TYPICAL = 1,
  277. RT_NVME_RW_DSM_FREQ_RARE = 2,
  278. RT_NVME_RW_DSM_FREQ_READS = 3,
  279. RT_NVME_RW_DSM_FREQ_WRITES = 4,
  280. RT_NVME_RW_DSM_FREQ_RW = 5,
  281. RT_NVME_RW_DSM_FREQ_ONCE = 6,
  282. RT_NVME_RW_DSM_FREQ_PREFETCH = 7,
  283. RT_NVME_RW_DSM_FREQ_TEMP = 8,
  284. RT_NVME_RW_DSM_LATENCY_NONE = 0 << 4,
  285. RT_NVME_RW_DSM_LATENCY_IDLE = 1 << 4,
  286. RT_NVME_RW_DSM_LATENCY_NORM = 2 << 4,
  287. RT_NVME_RW_DSM_LATENCY_LOW = 3 << 4,
  288. RT_NVME_RW_DSM_SEQ_REQ = 1 << 6,
  289. RT_NVME_RW_DSM_COMPRESSED = 1 << 7,
  290. RT_NVME_RW_PRINFO_PRCHK_REF = 1 << 10,
  291. RT_NVME_RW_PRINFO_PRCHK_APP = 1 << 11,
  292. RT_NVME_RW_PRINFO_PRCHK_GUARD = 1 << 12,
  293. RT_NVME_RW_PRINFO_PRACT = 1 << 13,
  294. RT_NVME_RW_DTYPE_STREAMS = 1 << 4,
  295. RT_NVME_WZ_DEAC = 1 << 9,
  296. };
  297. enum
  298. {
  299. RT_NVME_QUEUE_PHYS_CONTIG = (1 << 0),
  300. RT_NVME_CQ_IRQ_ENABLED = (1 << 1),
  301. RT_NVME_SQ_PRIO_URGENT = (0 << 1),
  302. RT_NVME_SQ_PRIO_HIGH = (1 << 1),
  303. RT_NVME_SQ_PRIO_MEDIUM = (2 << 1),
  304. RT_NVME_SQ_PRIO_LOW = (3 << 1),
  305. RT_NVME_FEAT_ARBITRATION = 0x01,
  306. RT_NVME_FEAT_POWER_MGMT = 0x02,
  307. RT_NVME_FEAT_LBA_RANGE = 0x03,
  308. RT_NVME_FEAT_TEMP_THRESH = 0x04,
  309. RT_NVME_FEAT_ERR_RECOVERY = 0x05,
  310. RT_NVME_FEAT_VOLATILE_WC = 0x06,
  311. RT_NVME_FEAT_NUM_QUEUES = 0x07,
  312. RT_NVME_FEAT_IRQ_COALESCE = 0x08,
  313. RT_NVME_FEAT_IRQ_CONFIG = 0x09,
  314. RT_NVME_FEAT_WRITE_ATOMIC = 0x0a,
  315. RT_NVME_FEAT_ASYNC_EVENT = 0x0b,
  316. RT_NVME_FEAT_AUTO_PST = 0x0c,
  317. RT_NVME_FEAT_SW_PROGRESS = 0x80,
  318. RT_NVME_FEAT_HOST_ID = 0x81,
  319. RT_NVME_FEAT_RESV_MASK = 0x82,
  320. RT_NVME_FEAT_RESV_PERSIST = 0x83,
  321. RT_NVME_LOG_ERROR = 0x01,
  322. RT_NVME_LOG_SMART = 0x02,
  323. RT_NVME_LOG_FW_SLOT = 0x03,
  324. RT_NVME_LOG_RESERVATION = 0x80,
  325. RT_NVME_FWACT_REPL = (0 << 3),
  326. RT_NVME_FWACT_REPL_ACTV = (1 << 3),
  327. RT_NVME_FWACT_ACTV = (2 << 3),
  328. };
  329. struct rt_nvme_command_identify
  330. {
  331. rt_uint8_t opcode;
  332. rt_uint8_t flags;
  333. rt_uint16_t cmdid;
  334. rt_le32_t nsid;
  335. NVME_RSVD(8, 16);
  336. rt_le64_t prp1;
  337. rt_le64_t prp2;
  338. rt_le32_t cns;
  339. NVME_RSVD(64, 20);
  340. };
  341. struct rt_nvme_command_features
  342. {
  343. rt_uint8_t opcode;
  344. rt_uint8_t flags;
  345. rt_uint16_t cmdid;
  346. rt_le32_t nsid;
  347. NVME_RSVD(8, 16);
  348. rt_le64_t prp1;
  349. rt_le64_t prp2;
  350. rt_le32_t fid;
  351. rt_le32_t dword11;
  352. NVME_RSVD(68, 16);
  353. };
  354. struct rt_nvme_command_create_cq
  355. {
  356. rt_uint8_t opcode;
  357. rt_uint8_t flags;
  358. rt_uint16_t cmdid;
  359. NVME_RSVD(4, 20);
  360. rt_le64_t prp1;
  361. NVME_RSVD(32, 8);
  362. rt_le16_t cqid;
  363. rt_le16_t qsize;
  364. rt_le16_t cq_flags;
  365. rt_le16_t irq_vector;
  366. NVME_RSVD(104, 16);
  367. };
  368. struct rt_nvme_command_create_sq
  369. {
  370. rt_uint8_t opcode;
  371. rt_uint8_t flags;
  372. rt_uint16_t cmdid;
  373. NVME_RSVD(4, 20);
  374. rt_le64_t prp1;
  375. NVME_RSVD(32, 8);
  376. rt_le16_t sqid;
  377. rt_le16_t qsize;
  378. rt_le16_t sq_flags;
  379. rt_le16_t cqid;
  380. NVME_RSVD(104, 16);
  381. };
  382. struct rt_nvme_command_delete_queue
  383. {
  384. rt_uint8_t opcode;
  385. rt_uint8_t flags;
  386. rt_uint16_t cmdid;
  387. NVME_RSVD(4, 36);
  388. rt_le16_t qid;
  389. NVME_RSVD(42, 22);
  390. };
  391. struct rt_nvme_command_write_zeroes
  392. {
  393. rt_uint8_t opcode;
  394. rt_uint8_t flags;
  395. rt_uint16_t cmdid;
  396. rt_le32_t nsid;
  397. NVME_RSVD(8, 8);
  398. rt_le64_t metadata;
  399. rt_le64_t prp1;
  400. rt_le64_t prp2;
  401. rt_le64_t slba;
  402. rt_le16_t length;
  403. rt_le16_t control;
  404. rt_le32_t dsmgmt;
  405. rt_le32_t reftag;
  406. rt_le16_t apptag;
  407. rt_le16_t appmask;
  408. };
  409. struct rt_nvme_command
  410. {
  411. union
  412. {
  413. struct rt_nvme_command_common common;
  414. struct rt_nvme_command_rw rw;
  415. struct rt_nvme_command_identify identify;
  416. struct rt_nvme_command_features features;
  417. struct rt_nvme_command_create_cq create_cq;
  418. struct rt_nvme_command_create_sq create_sq;
  419. struct rt_nvme_command_delete_queue delete_queue;
  420. struct rt_nvme_command_write_zeroes write_zeroes;
  421. };
  422. };
  423. struct rt_nvme_completion
  424. {
  425. union
  426. {
  427. rt_le16_t u16;
  428. rt_le32_t u32;
  429. rt_le64_t u64;
  430. } result;
  431. rt_le16_t sq_head; /* How much of this queue may be reclaimed */
  432. rt_le16_t sq_id; /* Submission queue that generated this entry */
  433. rt_uint16_t cmdid; /* Which command completed */
  434. rt_le16_t status; /* Command status */
  435. };
  436. enum
  437. {
  438. RT_NVME_REG_CAP = 0x0000, /* Controller Capabilities */
  439. RT_NVME_REG_VS = 0x0008, /* Version */
  440. RT_NVME_REG_INTMS = 0x000c, /* Interrupt Mask Set */
  441. RT_NVME_REG_INTMC = 0x0010, /* Interrupt Mask Clear */
  442. RT_NVME_REG_CC = 0x0014, /* Controller Configuration */
  443. RT_NVME_REG_CSTS = 0x001c, /* Controller Status */
  444. RT_NVME_REG_NSSR = 0x0020, /* NVM Subsystem Reset */
  445. RT_NVME_REG_AQA = 0x0024, /* Admin Queue Attributes */
  446. RT_NVME_REG_ASQ = 0x0028, /* Admin SQ Base Address */
  447. RT_NVME_REG_ACQ = 0x0030, /* Admin CQ Base Address */
  448. RT_NVME_REG_CMBLOC = 0x0038, /* Controller Memory Buffer Location */
  449. RT_NVME_REG_CMBSZ = 0x003c, /* Controller Memory Buffer Size */
  450. RT_NVME_REG_BPINFO = 0x0040, /* Boot Partition Information */
  451. RT_NVME_REG_BPRSEL = 0x0044, /* Boot Partition Read Select */
  452. RT_NVME_REG_BPMBL = 0x0048, /* Boot Partition Memory Buffer Location */
  453. RT_NVME_REG_CMBMSC = 0x0050, /* Controller Memory Buffer Memory Space Control */
  454. RT_NVME_REG_CRTO = 0x0068, /* Controller Ready Timeouts */
  455. RT_NVME_REG_PMRCAP = 0x0e00, /* Persistent Memory Capabilities */
  456. RT_NVME_REG_PMRCTL = 0x0e04, /* Persistent Memory Region Control */
  457. RT_NVME_REG_PMRSTS = 0x0e08, /* Persistent Memory Region Status */
  458. RT_NVME_REG_PMREBS = 0x0e0c, /* Persistent Memory Region Elasticity Buffer Size */
  459. RT_NVME_REG_PMRSWTP = 0x0e10, /* Persistent Memory Region Sustained Write Throughput */
  460. RT_NVME_REG_DBS = 0x1000, /* SQ 0 Tail Doorbell */
  461. };
  462. #define RT_NVME_CAP_MQES(cap) ((cap) & 0xffff)
  463. #define RT_NVME_CAP_TIMEOUT(cap) (((cap) >> 24) & 0xff)
  464. #define RT_NVME_CAP_STRIDE(cap) (((cap) >> 32) & 0xf)
  465. #define RT_NVME_CAP_MPSMIN(cap) (((cap) >> 48) & 0xf)
  466. #define RT_NVME_CAP_MPSMAX(cap) (((cap) >> 52) & 0xf)
  467. #define RT_NVME_VS(major, minor) (((major) << 16) | ((minor) << 8))
  468. #define RT_NVME_AQ_DEPTH 32
  469. #define RT_NVME_NR_AEN_COMMANDS 1
  470. #define RT_NVME_AQ_BLK_MQ_DEPTH (RT_NVME_AQ_DEPTH - RT_NVME_NR_AEN_COMMANDS)
  471. #define RT_NVME_AQ_MQ_TAG_DEPTH (RT_NVME_AQ_BLK_MQ_DEPTH - 1)
  472. enum
  473. {
  474. RT_NVME_CC_ENABLE = 1 << 0,
  475. RT_NVME_CC_CSS_NVM = 0 << 4,
  476. RT_NVME_CC_MPS_SHIFT = 7,
  477. RT_NVME_CC_ARB_RR = 0 << 11,
  478. RT_NVME_CC_ARB_WRRU = 1 << 11,
  479. RT_NVME_CC_ARB_VS = 7 << 11,
  480. RT_NVME_CC_SHN_NONE = 0 << 14,
  481. RT_NVME_CC_SHN_NORMAL = 1 << 14,
  482. RT_NVME_CC_SHN_ABRUPT = 2 << 14,
  483. RT_NVME_CC_SHN_MASK = 3 << 14,
  484. RT_NVME_CC_IOSQES = 6 << 16,
  485. RT_NVME_CC_IOCQES = 4 << 20,
  486. RT_NVME_CSTS_RDY = 1 << 0,
  487. RT_NVME_CSTS_CFS = 1 << 1,
  488. RT_NVME_CSTS_SHST_NORMAL = 0 << 2,
  489. RT_NVME_CSTS_SHST_OCCUR = 1 << 2,
  490. RT_NVME_CSTS_SHST_CMPLT = 2 << 2,
  491. RT_NVME_CSTS_SHST_MASK = 3 << 2,
  492. };
  493. rt_packed(struct rt_nvme_id_power_state
  494. {
  495. rt_le16_t mp; /* Maximum Power */
  496. NVME_RSVD(1, 1);
  497. rt_uint8_t mxps_nops; /* Max Power Scale, Non-Operational State */
  498. rt_le32_t enlat; /* Entry Latency: microseconds */
  499. rt_le32_t exlat; /* Exit Latency: microseconds */
  500. rt_uint8_t rrt; /* Relative Read Throughput */
  501. rt_uint8_t rrl; /* Relative Read Latency */
  502. rt_uint8_t rwt; /* Relative Write Throughput */
  503. rt_uint8_t rwl; /* Relative Write Latency */
  504. rt_le16_t idlp; /* Idle Power */
  505. rt_uint8_t ips; /* Idle Power Scale */
  506. NVME_RSVD(19, 1);
  507. rt_le16_t actp; /* Active Power */
  508. rt_uint8_t apw_aps; /* Active Power Workload, Active Power Scale */
  509. NVME_RSVD(23, 9);
  510. });
  511. rt_packed(struct rt_nvme_id_ctrl
  512. {
  513. /* Controller Capabilities and Features */
  514. rt_le16_t vid; /* PCI Vendor ID */
  515. rt_le16_t ssvid; /* PCI Subsystem Vendor */
  516. char sn[20]; /* Serial Number */
  517. char mn[40]; /* Model Number */
  518. char fr[8]; /* Firmware Revision */
  519. rt_uint8_t rab; /* Recommended Arbitration Burst */
  520. rt_uint8_t ieee[3]; /* IEEE OUI Identifier */
  521. rt_uint8_t mic; /* Controller Multi-Path I/O and Namespace Sharing Capabilities */
  522. rt_uint8_t mdts; /* Maximum Data Transfer Size */
  523. rt_uint16_t cntlid; /* Controller ID */
  524. rt_uint32_t ver; /* Version */
  525. rt_uint32_t rtd3r; /* RTD3 Resume Latency */
  526. rt_uint32_t rtd3e; /* RTD3 Entry Latency */
  527. rt_uint32_t oaes; /* Optional Asynchronous Events Supported */
  528. #define RT_NVME_ID_CTRATT_ELBAS 15 /* Extended LBA Formats Supported */
  529. #define RT_NVME_ID_CTRATT_DNVMS 14 /* Delete NVM Set */
  530. #define RT_NVME_ID_CTRATT_DEG 13 /* Delete Endurance Group */
  531. #define RT_NVME_ID_CTRATT_VCM 12 /* Variable Capacity Management */
  532. #define RT_NVME_ID_CTRATT_FCM 11 /* Fixed Capacity Management */
  533. #define RT_NVME_ID_CTRATT_MDS 10 /* Multi-Domain Subsystem */
  534. #define RT_NVME_ID_CTRATT_UUIDL 9 /* UUID List */
  535. #define RT_NVME_ID_CTRATT_SQA 8 /* SQ Associations */
  536. #define RT_NVME_ID_CTRATT_NG 7 /* Namespace Granularity */
  537. #define RT_NVME_ID_CTRATT_TBKAS 6 /* Traffic Based Keep Alive Support */
  538. #define RT_NVME_ID_CTRATT_PLM 5 /* Predictable Latency Mode */
  539. #define RT_NVME_ID_CTRATT_EG 4 /* Endurance Groups */
  540. #define RT_NVME_ID_CTRATT_RRL 3 /* Read Recovery Levels */
  541. #define RT_NVME_ID_CTRATT_NVMS 2 /* NVM Sets */
  542. #define RT_NVME_ID_CTRATT_NOPSPM 1 /* Non-Operational Power State Permissive Mode */
  543. #define RT_NVME_ID_CTRATT_HIS 0 /* Host Identifier Support */
  544. rt_uint32_t ctratt; /* Controller Attributes */
  545. rt_uint16_t rrls; /* Read Recovery Levels Supported */
  546. NVME_RSVD(102, 9);
  547. rt_uint8_t cntrltype; /* Controller Type */
  548. rt_uint8_t fguid[16]; /* FRU Globally Unique Identifier */
  549. rt_uint16_t crdt1; /* Command Retry Delay Time 1 */
  550. rt_uint16_t crdt2; /* Command Retry Delay Time 2 */
  551. rt_uint16_t crdt3; /* Command Retry Delay Time 3 */
  552. NVME_RSVD(134, 119);
  553. #define RT_NVME_ID_NVMSR_NVMEE 1 /* NVMe Enclosure */
  554. #define RT_NVME_ID_NVMSR_NVMESD 0 /* NVMe Storage Device */
  555. rt_uint8_t nvmsr; /* NVM Subsystem Report */
  556. #define RT_NVME_ID_VWCI_VWCRV 7 /* VPD Write Cycles Remaining Valid */
  557. #define RT_NVME_ID_VWCI_VWCR 0 /* VPD Write Cycles Remaining */
  558. rt_uint8_t vwci; /* VPD Write Cycle Information */
  559. #define RT_NVME_ID_MEC_PCIEME 1 /* PCIe Port Management Endpoint */
  560. #define RT_NVME_ID_MEC_SMBUSME 0 /* SMBus/I2C Port Management Endpoint */
  561. rt_uint8_t mec; /* Management Endpoint Capabilities */
  562. /* Admin Command Set Attributes & Optional Controller Capabilities */
  563. rt_le16_t oacs; /* Optional Admin Command Support */
  564. rt_uint8_t acl; /* Abort Command Limit */
  565. rt_uint8_t aerl; /* Asynchronous Event Request Limit */
  566. #define RT_NVME_ID_FRMW_SMUD 5 /* Support Multiple Update Detection */
  567. #define RT_NVME_ID_FRMW_FAWR 4 /* Firmware Activation Without Reset */
  568. #define RT_NVME_ID_FRMW_NOFS 1 /* Number Of Firmware Slots */
  569. #define RT_NVME_ID_FRMW_FFSRO 0 /* First Firmware Slot Read Only */
  570. rt_uint8_t frmw; /* Firmware Updates */
  571. rt_uint8_t lpa; /* Log Page Attributes */
  572. rt_uint8_t elpe; /* Error Log Page Entries */
  573. rt_uint8_t npss; /* Number of Power States Support */
  574. rt_uint8_t avscc; /* Admin Vendor Specific Command Configuration */
  575. rt_uint8_t apsta; /* Autonomous Power State Transition Attributes */
  576. rt_le16_t wctemp; /* Warning Composite Temperature Threshold */
  577. rt_le16_t cctemp; /* Critical Composite Temperature Threshold */
  578. rt_uint16_t mtfa; /* Maximum Time for Firmware Activation */
  579. rt_uint32_t hmpre; /* Host Memory Buffer Preferred Size */
  580. rt_uint32_t hmmin; /* Host Memory Buffer Minimum Size */
  581. rt_uint8_t tnvmcap[16]; /* Total NVM Capacity */
  582. rt_uint8_t unvmcap[16]; /* Unallocated NVM Capacity */
  583. #define RT_NVME_ID_RPMBS_ASZ 24 /* Access Size */
  584. #define RT_NVME_ID_RPMBS_TSZ 16 /* Total Size */
  585. #define RT_NVME_ID_RPMBS_AM 3 /* Authentication Method */
  586. #define RT_NVME_ID_RPMBS_NORPMBU 2 /* Number of RPMB Units */
  587. rt_uint32_t rpmbs; /* Replay Protected Memory Block Support */
  588. rt_uint16_t edstt; /* Extended Device Self-test Time */
  589. rt_uint8_t dsto; /* Device Self-test Options */
  590. rt_uint8_t fwug; /* Firmware Update Granularity */
  591. rt_uint16_t kas; /* Keep Alive Support */
  592. rt_uint16_t hctma; /* Host Controlled Thermal Management Attributes */
  593. rt_uint16_t mntmt; /* Minimum Thermal Management Temperature */
  594. rt_uint16_t mxtmt; /* Maximum Thermal Management Temperature */
  595. #define RT_NVME_ID_SANICAP_NODMMAS 30 /* No-Deallocate Modifies Media After Sanitize */
  596. #define RT_NVME_ID_SANICAP_NDI 29 /* No-Deallocate Inhibited */
  597. #define RT_NVME_ID_SANICAP_OWS 2 /* Overwrite Support */
  598. #define RT_NVME_ID_SANICAP_BES 1 /* Block Erase Support */
  599. #define RT_NVME_ID_SANICAP_CES 0 /* Crypto Erase Support */
  600. rt_uint32_t sanicap; /* Sanitize Capabilities */
  601. rt_uint32_t hmminds; /* Host Memory Buffer Minimum Descriptor Entry Size */
  602. rt_uint16_t hmmaxd; /* Host Memory Maximum Descriptors Entries */
  603. rt_uint16_t nsetidmax; /* NVM Set Identifier Maximum */
  604. rt_uint16_t endgidmax; /* Endurance Group Identifier Maximum */
  605. rt_uint8_t anatt; /* ANA Transition Time */
  606. rt_uint8_t anacap; /* Asymmetric Namespace Access Capabilities */
  607. rt_uint32_t anagrpmax; /* ANA Group Identifier Maximum */
  608. rt_uint32_t nanagrpid; /* Number of ANA Group Identifiers */
  609. rt_uint32_t pels; /* Persistent Event Log Size */
  610. rt_uint16_t dmid; /* Domain Identifier */
  611. NVME_RSVD(358, 10);
  612. rt_uint8_t megcap[16]; /* Max Endurance Group Capacity */
  613. NVME_RSVD(384, 128);
  614. /* NVM Command Set Attributes */
  615. rt_uint8_t sqes; /* Submission Queue Entry Size */
  616. rt_uint8_t cqes; /* Completion Queue Entry Size */
  617. rt_le16_t maxcmd; /* Maximum Outstanding Commands */
  618. rt_le32_t nn; /* Number of Namespaces */
  619. rt_le16_t oncs; /* Optional NVM Command Support */
  620. rt_le16_t fuses; /* Fused Operation Support */
  621. rt_uint8_t fna; /* Format NVM Attributes */
  622. rt_uint8_t vwc; /* Volatile Write Cache */
  623. rt_le16_t awun; /* Atomic Write Unit Normal */
  624. rt_le16_t awupf; /* Atomic Write Unit Power Fail */
  625. rt_uint8_t nvscc; /* I/O Command Set Vendor Specific Command Configuration */
  626. rt_uint8_t nwpc; /* Namespace Write Protection Capabilities */
  627. rt_le16_t acwu; /* Atomic Compare & Write Unit */
  628. rt_le16_t cdfs; /* Copy Descriptor Formats Supported */
  629. #define RT_NVME_ID_SGL_SUPPORT_MASK 0x3
  630. rt_le32_t sgls; /* SGL Support */
  631. rt_uint32_t mnan; /* Maximum Number of Allowed Namespaces */
  632. char maxdna[16]; /* Maximum Domain Namespace Attachments */
  633. rt_le32_t maxcna; /* Maximum I/O Controller Namespace Attachments */
  634. NVME_RSVD(564, 204);
  635. rt_uint8_t subnqn[256]; /* NVM Subsystem NVMe Qualified Name */
  636. NVME_RSVD(1024, 768);
  637. rt_le32_t ioccsz; /* I/O Queue Command Capsule Supported Size */
  638. rt_le32_t iorcsz; /* I/O Queue Response Capsule Supported Size */
  639. rt_le16_t icdoff; /* In Capsule Data Offset */
  640. rt_uint8_t ctrattr; /* Fabrics Controller Attributes */
  641. rt_uint8_t msdbd; /* Maximum SGL Data Block Descriptors */
  642. rt_le16_t ofcs; /* Optional Fabric Commands Support */
  643. rt_uint8_t dctype;
  644. NVME_RSVD(1807, 241);
  645. /* Power State Descriptors */
  646. struct rt_nvme_id_power_state psd[32];
  647. /* Vendor Specific */
  648. rt_uint8_t vs[1024];
  649. });
  650. enum
  651. {
  652. RT_NVME_CTRL_CMIC_MULTI_PORT = 1 << 0,
  653. RT_NVME_CTRL_CMIC_MULTI_CTRL = 1 << 1,
  654. RT_NVME_CTRL_CMIC_ANA = 1 << 3,
  655. RT_NVME_CTRL_ONCS_COMPARE = 1 << 0,
  656. RT_NVME_CTRL_ONCS_WRITE_UNCORRECTABLE = 1 << 1,
  657. RT_NVME_CTRL_ONCS_DSM = 1 << 2,
  658. RT_NVME_CTRL_ONCS_WRITE_ZEROES = 1 << 3,
  659. RT_NVME_CTRL_ONCS_RESERVATIONS = 1 << 5,
  660. RT_NVME_CTRL_ONCS_TIMESTAMP = 1 << 6,
  661. RT_NVME_CTRL_VWC_PRESENT = 1 << 0,
  662. RT_NVME_CTRL_OACS_SEC_SUPP = 1 << 0,
  663. RT_NVME_CTRL_OACS_NS_MNGT_SUPP = 1 << 3,
  664. RT_NVME_CTRL_OACS_DIRECTIVES = 1 << 5,
  665. RT_NVME_CTRL_OACS_DBBUF_SUPP = 1 << 8,
  666. RT_NVME_CTRL_LPA_CMD_EFFECTS_LOG = 1 << 1,
  667. RT_NVME_CTRL_CTRATT_128_ID = 1 << 0,
  668. RT_NVME_CTRL_CTRATT_NON_OP_PSP = 1 << 1,
  669. RT_NVME_CTRL_CTRATT_NVM_SETS = 1 << 2,
  670. RT_NVME_CTRL_CTRATT_READ_RECV_LVLS = 1 << 3,
  671. RT_NVME_CTRL_CTRATT_ENDURANCE_GROUPS = 1 << 4,
  672. RT_NVME_CTRL_CTRATT_PREDICTABLE_LAT = 1 << 5,
  673. RT_NVME_CTRL_CTRATT_NAMESPACE_GRANULARITY = 1 << 7,
  674. RT_NVME_CTRL_CTRATT_UUID_LIST = 1 << 9,
  675. };
  676. struct rt_nvme_lba_format
  677. {
  678. rt_le16_t ms; /* Metadata size */
  679. rt_uint8_t ds; /* Data size */
  680. rt_uint8_t rp; /* Relative performance */
  681. };
  682. rt_packed(struct rt_nvme_id_ns
  683. {
  684. rt_le64_t nsze; /* Namespace size */
  685. rt_le64_t ncap; /* Namespace capacity */
  686. rt_le64_t nuse; /* Namespace utilization */
  687. rt_uint8_t nsfeat; /* Namespace features */
  688. rt_uint8_t nlbaf; /* Number of lba formats */
  689. rt_uint8_t flbas; /* Formatted lba size */
  690. rt_uint8_t mc; /* Metadata capabilities */
  691. rt_uint8_t dpc; /* End-to-end data protection capabilities */
  692. rt_uint8_t dps; /* End-to-end data protection type settings */
  693. rt_uint8_t nmic; /* Namespace Multi-path I/O and Namespace Sharing Capabilities */
  694. rt_uint8_t rescap; /* Reservation Capabilities */
  695. rt_uint8_t fpi; /* Format Progress Indicator */
  696. rt_uint8_t dlfeat; /* Deallocate Logical Block Features */
  697. rt_le16_t nawun; /* Namespace Atomic Write Unit Normal */
  698. rt_le16_t nawupf; /* Namespace Atomic Write Unit Power Fail */
  699. rt_le16_t nacwu; /* Namespace Atomic Compare & Write Unit */
  700. rt_le16_t nabsn; /* Namespace Atomic Boundary Size Normal */
  701. rt_le16_t nabo; /* Namespace Atomic Boundary Offset */
  702. rt_le16_t nabspf; /* Namespace Atomic Boundary Size Power Fail */
  703. rt_uint16_t noiob; /* Namespace Optimal IO Boundary */
  704. rt_le64_t nvmcap[2]; /* NVMe Capacity */
  705. rt_uint16_t npwg; /* Namespace Preferred Write Granularity */
  706. rt_uint16_t npwa; /* Namespace Preferred Write Alignment */
  707. rt_uint16_t npdg; /* Namespace Preferred Deallocate Granularity */
  708. rt_uint16_t npda; /* Namespace Preferred Deallocate Alignment */
  709. rt_uint16_t nows; /* Namespace Optimal Write Size */
  710. NVME_RSVD(118, 18);
  711. rt_uint32_t anagrpid; /* ANA Group Identifier */
  712. NVME_RSVD(139, 3);
  713. rt_uint8_t nsattr; /* Namespace Attributes */
  714. rt_uint16_t nvmsetid; /* NVMe Set Identifier */
  715. rt_uint16_t endgid; /* Endurance Group Identifier */
  716. rt_uint8_t nguid[16]; /* Namespace Globally Unique Identifier */
  717. rt_uint8_t eui64[8]; /* IEEE Extended Unique Identifier */
  718. /* Logical Block Address Format */
  719. struct rt_nvme_lba_format lbaf[16];
  720. NVME_RSVD(171, 192);
  721. /* Vendor specific */
  722. rt_uint8_t vs[3712];
  723. });
  724. enum
  725. {
  726. RT_NVME_NS_FEAT_THIN = 1 << 0,
  727. RT_NVME_NS_FLBAS_LBA_MASK = 0xf,
  728. RT_NVME_NS_FLBAS_LBA_UMASK = 0x60,
  729. RT_NVME_NS_FLBAS_LBA_SHIFT = 1,
  730. RT_NVME_NS_FLBAS_META_EXT = 0x10,
  731. RT_NVME_LBAF_RP_BEST = 0,
  732. RT_NVME_LBAF_RP_BETTER = 1,
  733. RT_NVME_LBAF_RP_GOOD = 2,
  734. RT_NVME_LBAF_RP_DEGRADED = 3,
  735. RT_NVME_NS_DPC_PI_LAST = 1 << 4,
  736. RT_NVME_NS_DPC_PI_FIRST = 1 << 3,
  737. RT_NVME_NS_DPC_PI_TYPE3 = 1 << 2,
  738. RT_NVME_NS_DPC_PI_TYPE2 = 1 << 1,
  739. RT_NVME_NS_DPC_PI_TYPE1 = 1 << 0,
  740. RT_NVME_NS_DPS_PI_FIRST = 1 << 3,
  741. RT_NVME_NS_DPS_PI_MASK = 0x7,
  742. RT_NVME_NS_DPS_PI_TYPE1 = 1,
  743. RT_NVME_NS_DPS_PI_TYPE2 = 2,
  744. RT_NVME_NS_DPS_PI_TYPE3 = 3,
  745. };
  746. struct rt_nvme_ops;
  747. struct rt_nvme_controller;
  748. /*
  749. * An NVM Express queue. Each device has at least two (one for admin commands
  750. * and one for I/O commands).
  751. */
  752. struct rt_nvme_queue
  753. {
  754. struct rt_nvme_controller *nvme;
  755. struct rt_nvme_command *sq_cmds;
  756. struct rt_nvme_completion *cq_entry;
  757. rt_ubase_t sq_cmds_phy;
  758. rt_ubase_t cq_entry_phy;
  759. rt_uint32_t *doorbell;
  760. rt_uint16_t qid;
  761. rt_uint16_t depth;
  762. rt_uint16_t sq_head;
  763. rt_uint16_t sq_tail;
  764. rt_uint16_t cq_head;
  765. rt_uint16_t cq_phase;
  766. rt_err_t err;
  767. struct rt_nvme_command *cmd;
  768. struct rt_completion done;
  769. struct rt_spinlock lock;
  770. };
  771. struct rt_nvme_controller
  772. {
  773. rt_list_t list;
  774. struct rt_device *dev;
  775. int nvme_id;
  776. char name[RT_NAME_MAX];
  777. void *regs;
  778. rt_uint64_t cap;
  779. rt_uint32_t page_shift;
  780. rt_uint32_t page_size;
  781. rt_uint32_t queue_depth;
  782. rt_uint32_t io_queue_max;
  783. rt_uint32_t ctrl_config;
  784. rt_uint32_t max_transfer_shift:8;
  785. rt_uint32_t volatile_write_cache:8;
  786. rt_uint32_t write_zeroes:1;
  787. rt_uint32_t sgl_mode:2;
  788. rt_uint32_t doorbell_stride;
  789. rt_uint32_t *doorbell_tbl;
  790. const struct rt_nvme_ops *ops;
  791. #define RT_USING_NVME_QUEUE (1 + (RT_USING_NVME_IO_QUEUE * RT_CPUS_NR))
  792. int irqs_nr;
  793. int irqs[RT_USING_NVME_QUEUE];
  794. union
  795. {
  796. struct
  797. {
  798. struct rt_nvme_queue admin_queue;
  799. struct rt_nvme_queue io_queues[RT_USING_NVME_IO_QUEUE * RT_CPUS_NR];
  800. };
  801. struct rt_nvme_queue queue[RT_USING_NVME_QUEUE];
  802. };
  803. volatile rt_atomic_t cmdid;
  804. volatile rt_atomic_t ioqid[RT_CPUS_NR];
  805. rt_list_t ns_nodes;
  806. };
  807. struct rt_nvme_device
  808. {
  809. struct rt_blk_disk parent;
  810. struct rt_nvme_controller *ctrl;
  811. rt_list_t list;
  812. rt_uint32_t nsid;
  813. rt_uint32_t lba_shift;
  814. struct rt_nvme_id_ns id;
  815. };
  816. #define rt_disk_to_nvme_device(disk) rt_container_of(disk, struct rt_nvme_device, parent)
  817. struct rt_nvme_ops
  818. {
  819. const char *name;
  820. /* Controller-specific NVM Express queue setup */
  821. rt_err_t (*setup_queue)(struct rt_nvme_queue *queue);
  822. /* Controller-specific NVM Express queue cleanup */
  823. rt_err_t (*cleanup_queue)(struct rt_nvme_queue *queue);
  824. /* Controller-specific NVM Express command submission */
  825. rt_err_t (*submit_cmd)(struct rt_nvme_queue *queue, struct rt_nvme_command *cmd);
  826. /* Controller-specific NVM Express command completion */
  827. void (*complete_cmd)(struct rt_nvme_queue *queue, struct rt_nvme_command *cmd);
  828. };
  829. rt_err_t rt_nvme_controller_register(struct rt_nvme_controller *nvme);
  830. rt_err_t rt_nvme_controller_unregister(struct rt_nvme_controller *nvme);
  831. #endif /* __NVME_H__ */