2 * Copyright (C) 2012-2013 Intel Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/types.h>
36 #include <sys/param.h>
38 #define NVME_PASSTHROUGH_CMD _IOWR('n', 0, struct nvme_pt_command)
39 #define NVME_RESET_CONTROLLER _IO('n', 1)
41 #define NVME_IO_TEST _IOWR('n', 100, struct nvme_io_test)
42 #define NVME_BIO_TEST _IOWR('n', 101, struct nvme_io_test)
45 * Macros to deal with NVME revisions, as defined VS register
47 #define NVME_REV(x, y) (((x) << 16) | ((y) << 8))
48 #define NVME_MAJOR(r) (((r) >> 16) & 0xffff)
49 #define NVME_MINOR(r) (((r) >> 8) & 0xff)
52 * Use to mark a command to apply to all namespaces, or to retrieve global
55 #define NVME_GLOBAL_NAMESPACE_TAG ((uint32_t)0xFFFFFFFF)
57 /* Cap nvme to 1MB transfers driver explodes with larger sizes */
58 #define NVME_MAX_XFER_SIZE (MAXPHYS < (1<<20) ? MAXPHYS : (1<<20))
60 union cap_lo_register {
63 /** maximum queue entries supported */
66 /** contiguous queues required */
69 /** arbitration mechanism supported */
72 uint32_t reserved1 : 5;
79 _Static_assert(sizeof(union cap_lo_register) == 4, "bad size for cap_lo_register");
81 union cap_hi_register {
84 /** doorbell stride */
87 uint32_t reserved3 : 1;
89 /** command sets supported */
92 uint32_t css_reserved : 3;
93 uint32_t reserved2 : 7;
95 /** memory page size minimum */
98 /** memory page size maximum */
101 uint32_t reserved1 : 8;
105 _Static_assert(sizeof(union cap_hi_register) == 4, "bad size of cap_hi_register");
113 uint32_t reserved1 : 3;
115 /** i/o command set selected */
118 /** memory page size */
121 /** arbitration mechanism selected */
124 /** shutdown notification */
127 /** i/o submission queue entry size */
130 /** i/o completion queue entry size */
133 uint32_t reserved2 : 8;
137 _Static_assert(sizeof(union cc_register) == 4, "bad size for cc_register");
140 NVME_SHN_NORMAL = 0x1,
141 NVME_SHN_ABRUPT = 0x2,
144 union csts_register {
150 /** controller fatal status */
153 /** shutdown status */
156 uint32_t reserved1 : 28;
160 _Static_assert(sizeof(union csts_register) == 4, "bad size for csts_register");
163 NVME_SHST_NORMAL = 0x0,
164 NVME_SHST_OCCURRING = 0x1,
165 NVME_SHST_COMPLETE = 0x2,
171 /** admin submission queue size */
174 uint32_t reserved1 : 4;
176 /** admin completion queue size */
179 uint32_t reserved2 : 4;
183 _Static_assert(sizeof(union aqa_register) == 4, "bad size for aqa_resgister");
185 struct nvme_registers
187 /** controller capabilities */
188 union cap_lo_register cap_lo;
189 union cap_hi_register cap_hi;
191 uint32_t vs; /* version */
192 uint32_t intms; /* interrupt mask set */
193 uint32_t intmc; /* interrupt mask clear */
195 /** controller configuration */
196 union cc_register cc;
200 /** controller status */
201 union csts_register csts;
205 /** admin queue attributes */
206 union aqa_register aqa;
208 uint64_t asq; /* admin submission queue base addr */
209 uint64_t acq; /* admin completion queue base addr */
210 uint32_t reserved3[0x3f2];
213 uint32_t sq_tdbl; /* submission queue tail doorbell */
214 uint32_t cq_hdbl; /* completion queue head doorbell */
215 } doorbell[1] __packed;
218 _Static_assert(sizeof(struct nvme_registers) == 0x1008, "bad size for nvme_registers");
223 uint16_t opc : 8; /* opcode */
224 uint16_t fuse : 2; /* fused operation */
226 uint16_t cid; /* command identifier */
229 uint32_t nsid; /* namespace identifier */
236 uint64_t mptr; /* metadata pointer */
239 uint64_t prp1; /* prp entry 1 */
242 uint64_t prp2; /* prp entry 2 */
245 uint32_t cdw10; /* command-specific */
246 uint32_t cdw11; /* command-specific */
247 uint32_t cdw12; /* command-specific */
248 uint32_t cdw13; /* command-specific */
249 uint32_t cdw14; /* command-specific */
250 uint32_t cdw15; /* command-specific */
253 _Static_assert(sizeof(struct nvme_command) == 16 * 4, "bad size for nvme_command");
257 uint16_t p : 1; /* phase tag */
258 uint16_t sc : 8; /* status code */
259 uint16_t sct : 3; /* status code type */
261 uint16_t m : 1; /* more */
262 uint16_t dnr : 1; /* do not retry */
265 _Static_assert(sizeof(struct nvme_status) == 2, "bad size for nvme_status");
267 struct nvme_completion {
270 uint32_t cdw0; /* command-specific */
276 uint16_t sqhd; /* submission queue head pointer */
277 uint16_t sqid; /* submission queue identifier */
280 uint16_t cid; /* command identifier */
281 struct nvme_status status;
284 _Static_assert(sizeof(struct nvme_completion) == 4 * 4, "bad size for nvme_completion");
286 struct nvme_dsm_range {
290 uint64_t starting_lba;
293 _Static_assert(sizeof(struct nvme_dsm_range) == 16, "bad size for nvme_dsm_ranage");
295 /* status code types */
296 enum nvme_status_code_type {
297 NVME_SCT_GENERIC = 0x0,
298 NVME_SCT_COMMAND_SPECIFIC = 0x1,
299 NVME_SCT_MEDIA_ERROR = 0x2,
300 /* 0x3-0x6 - reserved */
301 NVME_SCT_VENDOR_SPECIFIC = 0x7,
304 /* generic command status codes */
305 enum nvme_generic_command_status_code {
306 NVME_SC_SUCCESS = 0x00,
307 NVME_SC_INVALID_OPCODE = 0x01,
308 NVME_SC_INVALID_FIELD = 0x02,
309 NVME_SC_COMMAND_ID_CONFLICT = 0x03,
310 NVME_SC_DATA_TRANSFER_ERROR = 0x04,
311 NVME_SC_ABORTED_POWER_LOSS = 0x05,
312 NVME_SC_INTERNAL_DEVICE_ERROR = 0x06,
313 NVME_SC_ABORTED_BY_REQUEST = 0x07,
314 NVME_SC_ABORTED_SQ_DELETION = 0x08,
315 NVME_SC_ABORTED_FAILED_FUSED = 0x09,
316 NVME_SC_ABORTED_MISSING_FUSED = 0x0a,
317 NVME_SC_INVALID_NAMESPACE_OR_FORMAT = 0x0b,
318 NVME_SC_COMMAND_SEQUENCE_ERROR = 0x0c,
319 NVME_SC_INVALID_SGL_SEGMENT_DESCR = 0x0d,
320 NVME_SC_INVALID_NUMBER_OF_SGL_DESCR = 0x0e,
321 NVME_SC_DATA_SGL_LENGTH_INVALID = 0x0f,
322 NVME_SC_METADATA_SGL_LENGTH_INVALID = 0x10,
323 NVME_SC_SGL_DESCRIPTOR_TYPE_INVALID = 0x11,
324 NVME_SC_INVALID_USE_OF_CMB = 0x12,
325 NVME_SC_PRP_OFFET_INVALID = 0x13,
326 NVME_SC_ATOMIC_WRITE_UNIT_EXCEEDED = 0x14,
327 NVME_SC_OPERATION_DENIED = 0x15,
328 NVME_SC_SGL_OFFSET_INVALID = 0x16,
329 /* 0x17 - reserved */
330 NVME_SC_HOST_ID_INCONSISTENT_FORMAT = 0x18,
331 NVME_SC_KEEP_ALIVE_TIMEOUT_EXPIRED = 0x19,
332 NVME_SC_KEEP_ALIVE_TIMEOUT_INVALID = 0x1a,
333 NVME_SC_ABORTED_DUE_TO_PREEMPT = 0x1b,
334 NVME_SC_SANITIZE_FAILED = 0x1c,
335 NVME_SC_SANITIZE_IN_PROGRESS = 0x1d,
336 NVME_SC_SGL_DATA_BLOCK_GRAN_INVALID = 0x1e,
337 NVME_SC_NOT_SUPPORTED_IN_CMB = 0x1f,
339 NVME_SC_LBA_OUT_OF_RANGE = 0x80,
340 NVME_SC_CAPACITY_EXCEEDED = 0x81,
341 NVME_SC_NAMESPACE_NOT_READY = 0x82,
342 NVME_SC_RESERVATION_CONFLICT = 0x83,
343 NVME_SC_FORMAT_IN_PROGRESS = 0x84,
346 /* command specific status codes */
347 enum nvme_command_specific_status_code {
348 NVME_SC_COMPLETION_QUEUE_INVALID = 0x00,
349 NVME_SC_INVALID_QUEUE_IDENTIFIER = 0x01,
350 NVME_SC_MAXIMUM_QUEUE_SIZE_EXCEEDED = 0x02,
351 NVME_SC_ABORT_COMMAND_LIMIT_EXCEEDED = 0x03,
352 /* 0x04 - reserved */
353 NVME_SC_ASYNC_EVENT_REQUEST_LIMIT_EXCEEDED = 0x05,
354 NVME_SC_INVALID_FIRMWARE_SLOT = 0x06,
355 NVME_SC_INVALID_FIRMWARE_IMAGE = 0x07,
356 NVME_SC_INVALID_INTERRUPT_VECTOR = 0x08,
357 NVME_SC_INVALID_LOG_PAGE = 0x09,
358 NVME_SC_INVALID_FORMAT = 0x0a,
359 NVME_SC_FIRMWARE_REQUIRES_RESET = 0x0b,
360 NVME_SC_INVALID_QUEUE_DELETION = 0x0c,
361 NVME_SC_FEATURE_NOT_SAVEABLE = 0x0d,
362 NVME_SC_FEATURE_NOT_CHANGEABLE = 0x0e,
363 NVME_SC_FEATURE_NOT_NS_SPECIFIC = 0x0f,
364 NVME_SC_FW_ACT_REQUIRES_NVMS_RESET = 0x10,
365 NVME_SC_FW_ACT_REQUIRES_RESET = 0x11,
366 NVME_SC_FW_ACT_REQUIRES_TIME = 0x12,
367 NVME_SC_FW_ACT_PROHIBITED = 0x13,
368 NVME_SC_OVERLAPPING_RANGE = 0x14,
369 NVME_SC_NS_INSUFFICIENT_CAPACITY = 0x15,
370 NVME_SC_NS_ID_UNAVAILABLE = 0x16,
371 /* 0x17 - reserved */
372 NVME_SC_NS_ALREADY_ATTACHED = 0x18,
373 NVME_SC_NS_IS_PRIVATE = 0x19,
374 NVME_SC_NS_NOT_ATTACHED = 0x1a,
375 NVME_SC_THIN_PROV_NOT_SUPPORTED = 0x1b,
376 NVME_SC_CTRLR_LIST_INVALID = 0x1c,
377 NVME_SC_SELT_TEST_IN_PROGRESS = 0x1d,
378 NVME_SC_BOOT_PART_WRITE_PROHIB = 0x1e,
379 NVME_SC_INVALID_CTRLR_ID = 0x1f,
380 NVME_SC_INVALID_SEC_CTRLR_STATE = 0x20,
381 NVME_SC_INVALID_NUM_OF_CTRLR_RESRC = 0x21,
382 NVME_SC_INVALID_RESOURCE_ID = 0x22,
384 NVME_SC_CONFLICTING_ATTRIBUTES = 0x80,
385 NVME_SC_INVALID_PROTECTION_INFO = 0x81,
386 NVME_SC_ATTEMPTED_WRITE_TO_RO_PAGE = 0x82,
389 /* media error status codes */
390 enum nvme_media_error_status_code {
391 NVME_SC_WRITE_FAULTS = 0x80,
392 NVME_SC_UNRECOVERED_READ_ERROR = 0x81,
393 NVME_SC_GUARD_CHECK_ERROR = 0x82,
394 NVME_SC_APPLICATION_TAG_CHECK_ERROR = 0x83,
395 NVME_SC_REFERENCE_TAG_CHECK_ERROR = 0x84,
396 NVME_SC_COMPARE_FAILURE = 0x85,
397 NVME_SC_ACCESS_DENIED = 0x86,
398 NVME_SC_DEALLOCATED_OR_UNWRITTEN = 0x87,
402 enum nvme_admin_opcode {
403 NVME_OPC_DELETE_IO_SQ = 0x00,
404 NVME_OPC_CREATE_IO_SQ = 0x01,
405 NVME_OPC_GET_LOG_PAGE = 0x02,
406 /* 0x03 - reserved */
407 NVME_OPC_DELETE_IO_CQ = 0x04,
408 NVME_OPC_CREATE_IO_CQ = 0x05,
409 NVME_OPC_IDENTIFY = 0x06,
410 /* 0x07 - reserved */
411 NVME_OPC_ABORT = 0x08,
412 NVME_OPC_SET_FEATURES = 0x09,
413 NVME_OPC_GET_FEATURES = 0x0a,
414 /* 0x0b - reserved */
415 NVME_OPC_ASYNC_EVENT_REQUEST = 0x0c,
416 NVME_OPC_NAMESPACE_MANAGEMENT = 0x0d,
417 /* 0x0e-0x0f - reserved */
418 NVME_OPC_FIRMWARE_ACTIVATE = 0x10,
419 NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD = 0x11,
420 NVME_OPC_DEVICE_SELF_TEST = 0x14,
421 NVME_OPC_NAMESPACE_ATTACHMENT = 0x15,
422 NVME_OPC_KEEP_ALIVE = 0x18,
423 NVME_OPC_DIRECTIVE_SEND = 0x19,
424 NVME_OPC_DIRECTIVE_RECEIVE = 0x1a,
425 NVME_OPC_VIRTUALIZATION_MANAGEMENT = 0x1c,
426 NVME_OPC_NVME_MI_SEND = 0x1d,
427 NVME_OPC_NVME_MI_RECEIVE = 0x1e,
428 NVME_OPC_DOORBELL_BUFFER_CONFIG = 0x7c,
430 NVME_OPC_FORMAT_NVM = 0x80,
431 NVME_OPC_SECURITY_SEND = 0x81,
432 NVME_OPC_SECURITY_RECEIVE = 0x82,
433 NVME_OPC_SANITIZE = 0x84,
436 /* nvme nvm opcodes */
437 enum nvme_nvm_opcode {
438 NVME_OPC_FLUSH = 0x00,
439 NVME_OPC_WRITE = 0x01,
440 NVME_OPC_READ = 0x02,
441 /* 0x03 - reserved */
442 NVME_OPC_WRITE_UNCORRECTABLE = 0x04,
443 NVME_OPC_COMPARE = 0x05,
444 /* 0x06 - reserved */
445 NVME_OPC_WRITE_ZEROES = 0x08,
446 /* 0x07 - reserved */
447 NVME_OPC_DATASET_MANAGEMENT = 0x09,
448 /* 0x0a-0x0c - reserved */
449 NVME_OPC_RESERVATION_REGISTER = 0x0d,
450 NVME_OPC_RESERVATION_REPORT = 0x0e,
451 /* 0x0f-0x10 - reserved */
452 NVME_OPC_RESERVATION_ACQUIRE = 0x11,
453 /* 0x12-0x14 - reserved */
454 NVME_OPC_RESERVATION_RELEASE = 0x15,
458 /* 0x00 - reserved */
459 NVME_FEAT_ARBITRATION = 0x01,
460 NVME_FEAT_POWER_MANAGEMENT = 0x02,
461 NVME_FEAT_LBA_RANGE_TYPE = 0x03,
462 NVME_FEAT_TEMPERATURE_THRESHOLD = 0x04,
463 NVME_FEAT_ERROR_RECOVERY = 0x05,
464 NVME_FEAT_VOLATILE_WRITE_CACHE = 0x06,
465 NVME_FEAT_NUMBER_OF_QUEUES = 0x07,
466 NVME_FEAT_INTERRUPT_COALESCING = 0x08,
467 NVME_FEAT_INTERRUPT_VECTOR_CONFIGURATION = 0x09,
468 NVME_FEAT_WRITE_ATOMICITY = 0x0A,
469 NVME_FEAT_ASYNC_EVENT_CONFIGURATION = 0x0B,
470 NVME_FEAT_AUTONOMOUS_POWER_STATE_TRANSITION = 0x0C,
471 NVME_FEAT_HOST_MEMORY_BUFFER = 0x0D,
472 NVME_FEAT_TIMESTAMP = 0x0E,
473 NVME_FEAT_KEEP_ALIVE_TIMER = 0x0F,
474 NVME_FEAT_HOST_CONTROLLED_THERMAL_MGMT = 0x10,
475 NVME_FEAT_NON_OP_POWER_STATE_CONFIG = 0x11,
476 /* 0x12-0x77 - reserved */
477 /* 0x78-0x7f - NVMe Management Interface */
478 NVME_FEAT_SOFTWARE_PROGRESS_MARKER = 0x80,
479 /* 0x81-0xBF - command set specific (reserved) */
480 /* 0xC0-0xFF - vendor specific */
483 enum nvme_dsm_attribute {
484 NVME_DSM_ATTR_INTEGRAL_READ = 0x1,
485 NVME_DSM_ATTR_INTEGRAL_WRITE = 0x2,
486 NVME_DSM_ATTR_DEALLOCATE = 0x4,
489 enum nvme_activate_action {
490 NVME_AA_REPLACE_NO_ACTIVATE = 0x0,
491 NVME_AA_REPLACE_ACTIVATE = 0x1,
492 NVME_AA_ACTIVATE = 0x2,
495 struct nvme_power_state {
497 uint16_t mp; /* Maximum Power */
499 uint8_t mps : 1; /* Max Power Scale */
500 uint8_t nops : 1; /* Non-Operational State */
501 uint8_t ps_rsvd2 : 6;
502 uint32_t enlat; /* Entry Latency */
503 uint32_t exlat; /* Exit Latency */
504 uint8_t rrt : 5; /* Relative Read Throughput */
505 uint8_t ps_rsvd3 : 3;
506 uint8_t rrl : 5; /* Relative Read Latency */
507 uint8_t ps_rsvd4 : 3;
508 uint8_t rwt : 5; /* Relative Write Throughput */
509 uint8_t ps_rsvd5 : 3;
510 uint8_t rwl : 5; /* Relative Write Latency */
511 uint8_t ps_rsvd6 : 3;
512 uint16_t idlp; /* Idle Power */
513 uint8_t ps_rsvd7 : 6;
514 uint8_t ips : 2; /* Idle Power Scale */
516 uint16_t actp; /* Active Power */
517 uint8_t apw : 3; /* Active Power Workload */
518 uint8_t ps_rsvd9 : 3;
519 uint8_t aps : 2; /* Active Power Scale */
520 uint8_t ps_rsvd10[9];
523 _Static_assert(sizeof(struct nvme_power_state) == 32, "bad size for nvme_power_state");
525 #define NVME_SERIAL_NUMBER_LENGTH 20
526 #define NVME_MODEL_NUMBER_LENGTH 40
527 #define NVME_FIRMWARE_REVISION_LENGTH 8
529 struct nvme_controller_data {
531 /* bytes 0-255: controller capabilities and features */
536 /** pci subsystem vendor id */
540 uint8_t sn[NVME_SERIAL_NUMBER_LENGTH];
543 uint8_t mn[NVME_MODEL_NUMBER_LENGTH];
545 /** firmware revision */
546 uint8_t fr[NVME_FIRMWARE_REVISION_LENGTH];
548 /** recommended arbitration burst */
551 /** ieee oui identifier */
554 /** multi-interface capabilities */
557 /** maximum data transfer size */
566 /** RTD3 Resume Latency */
569 /** RTD3 Enter Latency */
572 /** Optional Asynchronous Events Supported */
573 uint32_t oaes; /* bitfield really */
575 /** Controller Attributes */
576 uint32_t ctratt; /* bitfield really */
578 uint8_t reserved1[12];
580 /** FRU Globally Unique Identifier */
583 uint8_t reserved2[128];
585 /* bytes 256-511: admin command set attributes */
587 /** optional admin command support */
589 /* supports security send/receive commands */
590 uint16_t security : 1;
592 /* supports format nvm command */
595 /* supports firmware activate/download commands */
596 uint16_t firmware : 1;
598 /* supports namespace management commands */
601 uint16_t oacs_rsvd : 12;
604 /** abort command limit */
607 /** asynchronous event request limit */
610 /** firmware updates */
612 /* first slot is read-only */
613 uint8_t slot1_ro : 1;
615 /* number of firmware slots */
616 uint8_t num_slots : 3;
618 uint8_t frmw_rsvd : 4;
621 /** log page attributes */
623 /* per namespace smart/health log page */
624 uint8_t ns_smart : 1;
626 uint8_t lpa_rsvd : 7;
629 /** error log page entries */
632 /** number of power states supported */
635 /** admin vendor specific command configuration */
637 /* admin vendor specific commands use spec format */
638 uint8_t spec_format : 1;
640 uint8_t avscc_rsvd : 7;
643 /** Autonomous Power State Transition Attributes */
645 /* Autonmous Power State Transitions supported */
646 uint8_t apst_supp : 1;
648 uint8_t apsta_rsvd : 7;
651 /** Warning Composite Temperature Threshold */
654 /** Critical Composite Temperature Threshold */
657 /** Maximum Time for Firmware Activation */
660 /** Host Memory Buffer Preferred Size */
663 /** Host Memory Buffer Minimum Size */
666 /** Name space capabilities */
668 /* if nsmgmt, report tnvmcap and unvmcap */
673 /** Replay Protected Memory Block Support */
674 uint32_t rpmbs; /* Really a bitfield */
676 /** Extended Device Self-test Time */
679 /** Device Self-test Options */
680 uint8_t dsto; /* Really a bitfield */
682 /** Firmware Update Granularity */
685 /** Keep Alive Support */
688 /** Host Controlled Thermal Management Attributes */
689 uint16_t hctma; /* Really a bitfield */
691 /** Minimum Thermal Management Temperature */
694 /** Maximum Thermal Management Temperature */
697 /** Sanitize Capabilities */
698 uint32_t sanicap; /* Really a bitfield */
700 uint8_t reserved3[180];
701 /* bytes 512-703: nvm command set attributes */
703 /** submission queue entry size */
709 /** completion queue entry size */
715 /** Maximum Outstanding Commands */
718 /** number of namespaces */
721 /** optional nvm command support */
723 uint16_t compare : 1;
724 uint16_t write_unc : 1;
726 uint16_t reserved: 13;
729 /** fused operation support */
732 /** format nvm attributes */
735 /** volatile write cache */
738 uint8_t reserved : 7;
741 /* TODO: flesh out remaining nvm command set attributes */
742 uint8_t reserved5[178];
744 /* bytes 704-2047: i/o command set attributes */
745 uint8_t reserved6[1344];
747 /* bytes 2048-3071: power state descriptors */
748 struct nvme_power_state power_state[32];
750 /* bytes 3072-4095: vendor specific */
752 } __packed __aligned(4);
754 _Static_assert(sizeof(struct nvme_controller_data) == 4096, "bad size for nvme_controller_data");
756 struct nvme_namespace_data {
758 /** namespace size */
761 /** namespace capacity */
764 /** namespace utilization */
767 /** namespace features */
769 /** thin provisioning */
770 uint8_t thin_prov : 1;
771 uint8_t reserved1 : 7;
774 /** number of lba formats */
777 /** formatted lba size */
780 uint8_t extended : 1;
781 uint8_t reserved2 : 3;
784 /** metadata capabilities */
786 /* metadata can be transferred as part of data prp list */
787 uint8_t extended : 1;
789 /* metadata can be transferred with separate metadata pointer */
792 uint8_t reserved3 : 6;
795 /** end-to-end data protection capabilities */
797 /* protection information type 1 */
800 /* protection information type 2 */
803 /* protection information type 3 */
806 /* first eight bytes of metadata */
807 uint8_t md_start : 1;
809 /* last eight bytes of metadata */
813 /** end-to-end data protection type settings */
815 /* protection information type */
818 /* 1 == protection info transferred at start of metadata */
819 /* 0 == protection info transferred at end of metadata */
820 uint8_t md_start : 1;
822 uint8_t reserved4 : 4;
825 uint8_t reserved5[98];
827 /** lba format support */
835 /** relative performance */
838 uint32_t reserved6 : 6;
841 uint8_t reserved6[192];
843 uint8_t vendor_specific[3712];
844 } __packed __aligned(4);
846 _Static_assert(sizeof(struct nvme_namespace_data) == 4096, "bad size for nvme_namepsace_data");
850 /* 0x00 - reserved */
851 NVME_LOG_ERROR = 0x01,
852 NVME_LOG_HEALTH_INFORMATION = 0x02,
853 NVME_LOG_FIRMWARE_SLOT = 0x03,
854 NVME_LOG_CHANGED_NAMESPACE = 0x04,
855 NVME_LOG_COMMAND_EFFECT = 0x05,
856 /* 0x06-0x7F - reserved */
857 /* 0x80-0xBF - I/O command set specific */
858 NVME_LOG_RES_NOTIFICATION = 0x80,
859 /* 0xC0-0xFF - vendor specific */
862 * The following are Intel Specific log pages, but they seem
863 * to be widely implemented.
865 INTEL_LOG_READ_LAT_LOG = 0xc1,
866 INTEL_LOG_WRITE_LAT_LOG = 0xc2,
867 INTEL_LOG_TEMP_STATS = 0xc5,
868 INTEL_LOG_ADD_SMART = 0xca,
869 INTEL_LOG_DRIVE_MKT_NAME = 0xdd,
872 * HGST log page, with lots ofs sub pages.
874 HGST_INFO_LOG = 0xc1,
877 struct nvme_error_information_entry {
879 uint64_t error_count;
882 struct nvme_status status;
883 uint16_t error_location;
886 uint8_t vendor_specific;
887 uint8_t reserved[35];
888 } __packed __aligned(4);
890 _Static_assert(sizeof(struct nvme_error_information_entry) == 64, "bad size for nvme_error_information_entry");
892 union nvme_critical_warning_state {
897 uint8_t available_spare : 1;
898 uint8_t temperature : 1;
899 uint8_t device_reliability : 1;
900 uint8_t read_only : 1;
901 uint8_t volatile_memory_backup : 1;
902 uint8_t reserved : 3;
906 _Static_assert(sizeof(union nvme_critical_warning_state) == 1, "bad size for nvme_critical_warning_state");
908 struct nvme_health_information_page {
910 union nvme_critical_warning_state critical_warning;
912 uint16_t temperature;
913 uint8_t available_spare;
914 uint8_t available_spare_threshold;
915 uint8_t percentage_used;
917 uint8_t reserved[26];
920 * Note that the following are 128-bit values, but are
921 * defined as an array of 2 64-bit values.
923 /* Data Units Read is always in 512-byte units. */
924 uint64_t data_units_read[2];
925 /* Data Units Written is always in 512-byte units. */
926 uint64_t data_units_written[2];
927 /* For NVM command set, this includes Compare commands. */
928 uint64_t host_read_commands[2];
929 uint64_t host_write_commands[2];
930 /* Controller Busy Time is reported in minutes. */
931 uint64_t controller_busy_time[2];
932 uint64_t power_cycles[2];
933 uint64_t power_on_hours[2];
934 uint64_t unsafe_shutdowns[2];
935 uint64_t media_errors[2];
936 uint64_t num_error_info_log_entries[2];
937 uint32_t warning_temp_time;
938 uint32_t error_temp_time;
939 uint16_t temp_sensor[8];
941 uint8_t reserved2[296];
942 } __packed __aligned(4);
944 _Static_assert(sizeof(struct nvme_health_information_page) == 512, "bad size for nvme_health_information_page");
946 struct nvme_firmware_page {
949 uint8_t slot : 3; /* slot for current FW */
950 uint8_t reserved : 5;
954 uint64_t revision[7]; /* revisions for 7 slots */
955 uint8_t reserved2[448];
956 } __packed __aligned(4);
958 _Static_assert(sizeof(struct nvme_firmware_page) == 512, "bad size for nvme_firmware_page");
960 struct intel_log_temp_stats
963 uint64_t overtemp_flag_last;
964 uint64_t overtemp_flag_life;
968 uint64_t max_oper_temp;
969 uint64_t min_oper_temp;
971 } __packed __aligned(4);
973 _Static_assert(sizeof(struct intel_log_temp_stats) == 13 * 8, "bad size for intel_log_temp_stats");
975 #define NVME_TEST_MAX_THREADS 128
977 struct nvme_io_test {
979 enum nvme_nvm_opcode opc;
981 uint32_t time; /* in seconds */
982 uint32_t num_threads;
984 uint64_t io_completed[NVME_TEST_MAX_THREADS];
987 enum nvme_io_test_flags {
990 * Specifies whether dev_refthread/dev_relthread should be
991 * called during NVME_BIO_TEST. Ignored for other test
994 NVME_TEST_FLAG_REFTHREAD = 0x1,
997 struct nvme_pt_command {
1000 * cmd is used to specify a passthrough command to a controller or
1003 * The following fields from cmd may be specified by the caller:
1005 * * nsid (namespace id) - for admin commands only
1008 * Remaining fields must be set to 0 by the caller.
1010 struct nvme_command cmd;
1013 * cpl returns completion status for the passthrough command
1016 * The following fields will be filled out by the driver, for
1017 * consumption by the caller:
1019 * * status (except for phase)
1021 * Remaining fields will be set to 0 by the driver.
1023 struct nvme_completion cpl;
1025 /* buf is the data buffer associated with this passthrough command. */
1029 * len is the length of the data buffer associated with this
1030 * passthrough command.
1035 * is_read = 1 if the passthrough command will read data into the
1036 * supplied buffer from the controller.
1038 * is_read = 0 if the passthrough command will write data from the
1039 * supplied buffer to the controller.
1044 * driver_lock is used by the driver only. It must be set to 0
1047 struct mtx * driver_lock;
1050 #define nvme_completion_is_error(cpl) \
1051 ((cpl)->status.sc != 0 || (cpl)->status.sct != 0)
1053 void nvme_strvis(uint8_t *dst, const uint8_t *src, int dstlen, int srclen);
1059 struct nvme_namespace;
1060 struct nvme_controller;
1061 struct nvme_consumer;
1063 typedef void (*nvme_cb_fn_t)(void *, const struct nvme_completion *);
1065 typedef void *(*nvme_cons_ns_fn_t)(struct nvme_namespace *, void *);
1066 typedef void *(*nvme_cons_ctrlr_fn_t)(struct nvme_controller *);
1067 typedef void (*nvme_cons_async_fn_t)(void *, const struct nvme_completion *,
1068 uint32_t, void *, uint32_t);
1069 typedef void (*nvme_cons_fail_fn_t)(void *);
1071 enum nvme_namespace_flags {
1072 NVME_NS_DEALLOCATE_SUPPORTED = 0x1,
1073 NVME_NS_FLUSH_SUPPORTED = 0x2,
1076 int nvme_ctrlr_passthrough_cmd(struct nvme_controller *ctrlr,
1077 struct nvme_pt_command *pt,
1078 uint32_t nsid, int is_user_buffer,
1081 /* Admin functions */
1082 void nvme_ctrlr_cmd_set_feature(struct nvme_controller *ctrlr,
1083 uint8_t feature, uint32_t cdw11,
1084 void *payload, uint32_t payload_size,
1085 nvme_cb_fn_t cb_fn, void *cb_arg);
1086 void nvme_ctrlr_cmd_get_feature(struct nvme_controller *ctrlr,
1087 uint8_t feature, uint32_t cdw11,
1088 void *payload, uint32_t payload_size,
1089 nvme_cb_fn_t cb_fn, void *cb_arg);
1090 void nvme_ctrlr_cmd_get_log_page(struct nvme_controller *ctrlr,
1091 uint8_t log_page, uint32_t nsid,
1092 void *payload, uint32_t payload_size,
1093 nvme_cb_fn_t cb_fn, void *cb_arg);
1095 /* NVM I/O functions */
1096 int nvme_ns_cmd_write(struct nvme_namespace *ns, void *payload,
1097 uint64_t lba, uint32_t lba_count, nvme_cb_fn_t cb_fn,
1099 int nvme_ns_cmd_write_bio(struct nvme_namespace *ns, struct bio *bp,
1100 nvme_cb_fn_t cb_fn, void *cb_arg);
1101 int nvme_ns_cmd_read(struct nvme_namespace *ns, void *payload,
1102 uint64_t lba, uint32_t lba_count, nvme_cb_fn_t cb_fn,
1104 int nvme_ns_cmd_read_bio(struct nvme_namespace *ns, struct bio *bp,
1105 nvme_cb_fn_t cb_fn, void *cb_arg);
1106 int nvme_ns_cmd_deallocate(struct nvme_namespace *ns, void *payload,
1107 uint8_t num_ranges, nvme_cb_fn_t cb_fn,
1109 int nvme_ns_cmd_flush(struct nvme_namespace *ns, nvme_cb_fn_t cb_fn,
1111 int nvme_ns_dump(struct nvme_namespace *ns, void *virt, off_t offset,
1114 /* Registration functions */
1115 struct nvme_consumer * nvme_register_consumer(nvme_cons_ns_fn_t ns_fn,
1116 nvme_cons_ctrlr_fn_t ctrlr_fn,
1117 nvme_cons_async_fn_t async_fn,
1118 nvme_cons_fail_fn_t fail_fn);
1119 void nvme_unregister_consumer(struct nvme_consumer *consumer);
1121 /* Controller helper functions */
1122 device_t nvme_ctrlr_get_device(struct nvme_controller *ctrlr);
1123 const struct nvme_controller_data *
1124 nvme_ctrlr_get_data(struct nvme_controller *ctrlr);
1126 /* Namespace helper functions */
1127 uint32_t nvme_ns_get_max_io_xfer_size(struct nvme_namespace *ns);
1128 uint32_t nvme_ns_get_sector_size(struct nvme_namespace *ns);
1129 uint64_t nvme_ns_get_num_sectors(struct nvme_namespace *ns);
1130 uint64_t nvme_ns_get_size(struct nvme_namespace *ns);
1131 uint32_t nvme_ns_get_flags(struct nvme_namespace *ns);
1132 const char * nvme_ns_get_serial_number(struct nvme_namespace *ns);
1133 const char * nvme_ns_get_model_number(struct nvme_namespace *ns);
1134 const struct nvme_namespace_data *
1135 nvme_ns_get_data(struct nvme_namespace *ns);
1136 uint32_t nvme_ns_get_stripesize(struct nvme_namespace *ns);
1138 int nvme_ns_bio_process(struct nvme_namespace *ns, struct bio *bp,
1139 nvme_cb_fn_t cb_fn);
1142 * Command building helper functions -- shared with CAM
1143 * These functions assume allocator zeros out cmd structure
1144 * CAM's xpt_get_ccb and the request allocator for nvme both
1145 * do zero'd allocations.
1148 void nvme_ns_flush_cmd(struct nvme_command *cmd, uint32_t nsid)
1151 cmd->opc = NVME_OPC_FLUSH;
1156 void nvme_ns_rw_cmd(struct nvme_command *cmd, uint32_t rwcmd, uint32_t nsid,
1157 uint64_t lba, uint32_t count)
1161 cmd->cdw10 = lba & 0xffffffffu;
1162 cmd->cdw11 = lba >> 32;
1163 cmd->cdw12 = count-1;
1167 void nvme_ns_write_cmd(struct nvme_command *cmd, uint32_t nsid,
1168 uint64_t lba, uint32_t count)
1170 nvme_ns_rw_cmd(cmd, NVME_OPC_WRITE, nsid, lba, count);
1174 void nvme_ns_read_cmd(struct nvme_command *cmd, uint32_t nsid,
1175 uint64_t lba, uint32_t count)
1177 nvme_ns_rw_cmd(cmd, NVME_OPC_READ, nsid, lba, count);
1181 void nvme_ns_trim_cmd(struct nvme_command *cmd, uint32_t nsid,
1182 uint32_t num_ranges)
1184 cmd->opc = NVME_OPC_DATASET_MANAGEMENT;
1186 cmd->cdw10 = num_ranges - 1;
1187 cmd->cdw11 = NVME_DSM_ATTR_DEALLOCATE;
1190 extern int nvme_use_nvd;
1192 #endif /* _KERNEL */
1194 #endif /* __NVME_H__ */