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 * Use to mark a command to apply to all namespaces, or to retrieve global
48 #define NVME_GLOBAL_NAMESPACE_TAG ((uint32_t)0xFFFFFFFF)
50 /* Cap nvme to 1MB transfers driver explodes with larger sizes */
51 #define NVME_MAX_XFER_SIZE (MAXPHYS < (1<<20) ? MAXPHYS : (1<<20))
53 union cap_lo_register {
56 /** maximum queue entries supported */
59 /** contiguous queues required */
62 /** arbitration mechanism supported */
65 uint32_t reserved1 : 5;
72 _Static_assert(sizeof(union cap_lo_register) == 4, "bad size for cap_lo_register");
74 union cap_hi_register {
77 /** doorbell stride */
80 uint32_t reserved3 : 1;
82 /** command sets supported */
85 uint32_t css_reserved : 3;
86 uint32_t reserved2 : 7;
88 /** memory page size minimum */
91 /** memory page size maximum */
94 uint32_t reserved1 : 8;
98 _Static_assert(sizeof(union cap_hi_register) == 4, "bad size of cap_hi_register");
106 uint32_t reserved1 : 3;
108 /** i/o command set selected */
111 /** memory page size */
114 /** arbitration mechanism selected */
117 /** shutdown notification */
120 /** i/o submission queue entry size */
123 /** i/o completion queue entry size */
126 uint32_t reserved2 : 8;
130 _Static_assert(sizeof(union cc_register) == 4, "bad size for cc_register");
133 NVME_SHN_NORMAL = 0x1,
134 NVME_SHN_ABRUPT = 0x2,
137 union csts_register {
143 /** controller fatal status */
146 /** shutdown status */
149 uint32_t reserved1 : 28;
153 _Static_assert(sizeof(union csts_register) == 4, "bad size for csts_register");
156 NVME_SHST_NORMAL = 0x0,
157 NVME_SHST_OCCURRING = 0x1,
158 NVME_SHST_COMPLETE = 0x2,
164 /** admin submission queue size */
167 uint32_t reserved1 : 4;
169 /** admin completion queue size */
172 uint32_t reserved2 : 4;
176 _Static_assert(sizeof(union aqa_register) == 4, "bad size for aqa_resgister");
178 struct nvme_registers
180 /** controller capabilities */
181 union cap_lo_register cap_lo;
182 union cap_hi_register cap_hi;
184 uint32_t vs; /* version */
185 uint32_t intms; /* interrupt mask set */
186 uint32_t intmc; /* interrupt mask clear */
188 /** controller configuration */
189 union cc_register cc;
193 /** controller status */
194 union csts_register csts;
198 /** admin queue attributes */
199 union aqa_register aqa;
201 uint64_t asq; /* admin submission queue base addr */
202 uint64_t acq; /* admin completion queue base addr */
203 uint32_t reserved3[0x3f2];
206 uint32_t sq_tdbl; /* submission queue tail doorbell */
207 uint32_t cq_hdbl; /* completion queue head doorbell */
208 } doorbell[1] __packed;
211 _Static_assert(sizeof(struct nvme_registers) == 0x1008, "bad size for nvme_registers");
216 uint16_t opc : 8; /* opcode */
217 uint16_t fuse : 2; /* fused operation */
219 uint16_t cid; /* command identifier */
222 uint32_t nsid; /* namespace identifier */
229 uint64_t mptr; /* metadata pointer */
232 uint64_t prp1; /* prp entry 1 */
235 uint64_t prp2; /* prp entry 2 */
238 uint32_t cdw10; /* command-specific */
239 uint32_t cdw11; /* command-specific */
240 uint32_t cdw12; /* command-specific */
241 uint32_t cdw13; /* command-specific */
242 uint32_t cdw14; /* command-specific */
243 uint32_t cdw15; /* command-specific */
246 _Static_assert(sizeof(struct nvme_command) == 16 * 4, "bad size for nvme_command");
250 uint16_t p : 1; /* phase tag */
251 uint16_t sc : 8; /* status code */
252 uint16_t sct : 3; /* status code type */
254 uint16_t m : 1; /* more */
255 uint16_t dnr : 1; /* do not retry */
258 _Static_assert(sizeof(struct nvme_status) == 2, "bad size for nvme_status");
260 struct nvme_completion {
263 uint32_t cdw0; /* command-specific */
269 uint16_t sqhd; /* submission queue head pointer */
270 uint16_t sqid; /* submission queue identifier */
273 uint16_t cid; /* command identifier */
274 struct nvme_status status;
277 _Static_assert(sizeof(struct nvme_completion) == 4 * 4, "bad size for nvme_completion");
279 struct nvme_dsm_range {
283 uint64_t starting_lba;
286 _Static_assert(sizeof(struct nvme_dsm_range) == 16, "bad size for nvme_dsm_ranage");
288 /* status code types */
289 enum nvme_status_code_type {
290 NVME_SCT_GENERIC = 0x0,
291 NVME_SCT_COMMAND_SPECIFIC = 0x1,
292 NVME_SCT_MEDIA_ERROR = 0x2,
293 /* 0x3-0x6 - reserved */
294 NVME_SCT_VENDOR_SPECIFIC = 0x7,
297 /* generic command status codes */
298 enum nvme_generic_command_status_code {
299 NVME_SC_SUCCESS = 0x00,
300 NVME_SC_INVALID_OPCODE = 0x01,
301 NVME_SC_INVALID_FIELD = 0x02,
302 NVME_SC_COMMAND_ID_CONFLICT = 0x03,
303 NVME_SC_DATA_TRANSFER_ERROR = 0x04,
304 NVME_SC_ABORTED_POWER_LOSS = 0x05,
305 NVME_SC_INTERNAL_DEVICE_ERROR = 0x06,
306 NVME_SC_ABORTED_BY_REQUEST = 0x07,
307 NVME_SC_ABORTED_SQ_DELETION = 0x08,
308 NVME_SC_ABORTED_FAILED_FUSED = 0x09,
309 NVME_SC_ABORTED_MISSING_FUSED = 0x0a,
310 NVME_SC_INVALID_NAMESPACE_OR_FORMAT = 0x0b,
311 NVME_SC_COMMAND_SEQUENCE_ERROR = 0x0c,
313 NVME_SC_LBA_OUT_OF_RANGE = 0x80,
314 NVME_SC_CAPACITY_EXCEEDED = 0x81,
315 NVME_SC_NAMESPACE_NOT_READY = 0x82,
318 /* command specific status codes */
319 enum nvme_command_specific_status_code {
320 NVME_SC_COMPLETION_QUEUE_INVALID = 0x00,
321 NVME_SC_INVALID_QUEUE_IDENTIFIER = 0x01,
322 NVME_SC_MAXIMUM_QUEUE_SIZE_EXCEEDED = 0x02,
323 NVME_SC_ABORT_COMMAND_LIMIT_EXCEEDED = 0x03,
324 /* 0x04 - reserved */
325 NVME_SC_ASYNC_EVENT_REQUEST_LIMIT_EXCEEDED = 0x05,
326 NVME_SC_INVALID_FIRMWARE_SLOT = 0x06,
327 NVME_SC_INVALID_FIRMWARE_IMAGE = 0x07,
328 NVME_SC_INVALID_INTERRUPT_VECTOR = 0x08,
329 NVME_SC_INVALID_LOG_PAGE = 0x09,
330 NVME_SC_INVALID_FORMAT = 0x0a,
331 NVME_SC_FIRMWARE_REQUIRES_RESET = 0x0b,
333 NVME_SC_CONFLICTING_ATTRIBUTES = 0x80,
334 NVME_SC_INVALID_PROTECTION_INFO = 0x81,
335 NVME_SC_ATTEMPTED_WRITE_TO_RO_PAGE = 0x82,
338 /* media error status codes */
339 enum nvme_media_error_status_code {
340 NVME_SC_WRITE_FAULTS = 0x80,
341 NVME_SC_UNRECOVERED_READ_ERROR = 0x81,
342 NVME_SC_GUARD_CHECK_ERROR = 0x82,
343 NVME_SC_APPLICATION_TAG_CHECK_ERROR = 0x83,
344 NVME_SC_REFERENCE_TAG_CHECK_ERROR = 0x84,
345 NVME_SC_COMPARE_FAILURE = 0x85,
346 NVME_SC_ACCESS_DENIED = 0x86,
350 enum nvme_admin_opcode {
351 NVME_OPC_DELETE_IO_SQ = 0x00,
352 NVME_OPC_CREATE_IO_SQ = 0x01,
353 NVME_OPC_GET_LOG_PAGE = 0x02,
354 /* 0x03 - reserved */
355 NVME_OPC_DELETE_IO_CQ = 0x04,
356 NVME_OPC_CREATE_IO_CQ = 0x05,
357 NVME_OPC_IDENTIFY = 0x06,
358 /* 0x07 - reserved */
359 NVME_OPC_ABORT = 0x08,
360 NVME_OPC_SET_FEATURES = 0x09,
361 NVME_OPC_GET_FEATURES = 0x0a,
362 /* 0x0b - reserved */
363 NVME_OPC_ASYNC_EVENT_REQUEST = 0x0c,
364 NVME_OPC_NAMESPACE_MANAGEMENT = 0x0d,
365 /* 0x0e-0x0f - reserved */
366 NVME_OPC_FIRMWARE_ACTIVATE = 0x10,
367 NVME_OPC_FIRMWARE_IMAGE_DOWNLOAD = 0x11,
368 NVME_OPC_NAMESPACE_ATTACHMENT = 0x15,
370 NVME_OPC_FORMAT_NVM = 0x80,
371 NVME_OPC_SECURITY_SEND = 0x81,
372 NVME_OPC_SECURITY_RECEIVE = 0x82,
375 /* nvme nvm opcodes */
376 enum nvme_nvm_opcode {
377 NVME_OPC_FLUSH = 0x00,
378 NVME_OPC_WRITE = 0x01,
379 NVME_OPC_READ = 0x02,
380 /* 0x03 - reserved */
381 NVME_OPC_WRITE_UNCORRECTABLE = 0x04,
382 NVME_OPC_COMPARE = 0x05,
383 /* 0x06-0x07 - reserved */
384 NVME_OPC_DATASET_MANAGEMENT = 0x09,
388 /* 0x00 - reserved */
389 NVME_FEAT_ARBITRATION = 0x01,
390 NVME_FEAT_POWER_MANAGEMENT = 0x02,
391 NVME_FEAT_LBA_RANGE_TYPE = 0x03,
392 NVME_FEAT_TEMPERATURE_THRESHOLD = 0x04,
393 NVME_FEAT_ERROR_RECOVERY = 0x05,
394 NVME_FEAT_VOLATILE_WRITE_CACHE = 0x06,
395 NVME_FEAT_NUMBER_OF_QUEUES = 0x07,
396 NVME_FEAT_INTERRUPT_COALESCING = 0x08,
397 NVME_FEAT_INTERRUPT_VECTOR_CONFIGURATION = 0x09,
398 NVME_FEAT_WRITE_ATOMICITY = 0x0A,
399 NVME_FEAT_ASYNC_EVENT_CONFIGURATION = 0x0B,
400 NVME_FEAT_AUTONOMOUS_POWER_STATE_TRANSITION = 0x0C,
401 NVME_FEAT_HOST_MEMORY_BUFFER = 0x0D,
402 NVME_FEAT_TIMESTAMP = 0x0E,
403 NVME_FEAT_KEEP_ALIVE_TIMER = 0x0F,
404 NVME_FEAT_HOST_CONTROLLED_THERMAL_MGMT = 0x10,
405 NVME_FEAT_NON_OP_POWER_STATE_CONFIG = 0x11,
406 /* 0x12-0x77 - reserved */
407 /* 0x78-0x7f - NVMe Management Interface */
408 NVME_FEAT_SOFTWARE_PROGRESS_MARKER = 0x80,
409 /* 0x81-0xBF - command set specific (reserved) */
410 /* 0xC0-0xFF - vendor specific */
413 enum nvme_dsm_attribute {
414 NVME_DSM_ATTR_INTEGRAL_READ = 0x1,
415 NVME_DSM_ATTR_INTEGRAL_WRITE = 0x2,
416 NVME_DSM_ATTR_DEALLOCATE = 0x4,
419 enum nvme_activate_action {
420 NVME_AA_REPLACE_NO_ACTIVATE = 0x0,
421 NVME_AA_REPLACE_ACTIVATE = 0x1,
422 NVME_AA_ACTIVATE = 0x2,
425 struct nvme_power_state {
427 uint16_t mp; /* Maximum Power */
429 uint8_t mps : 1; /* Max Power Scale */
430 uint8_t nops : 1; /* Non-Operational State */
431 uint8_t ps_rsvd2 : 6;
432 uint32_t enlat; /* Entry Latency */
433 uint32_t exlat; /* Exit Latency */
434 uint8_t rrt : 5; /* Relative Read Throughput */
435 uint8_t ps_rsvd3 : 3;
436 uint8_t rrl : 5; /* Relative Read Latency */
437 uint8_t ps_rsvd4 : 3;
438 uint8_t rwt : 5; /* Relative Write Throughput */
439 uint8_t ps_rsvd5 : 3;
440 uint8_t rwl : 5; /* Relative Write Latency */
441 uint8_t ps_rsvd6 : 3;
442 uint16_t idlp; /* Idle Power */
443 uint8_t ps_rsvd7 : 6;
444 uint8_t ips : 2; /* Idle Power Scale */
446 uint16_t actp; /* Active Power */
447 uint8_t apw : 3; /* Active Power Workload */
448 uint8_t ps_rsvd9 : 3;
449 uint8_t aps : 2; /* Active Power Scale */
450 uint8_t ps_rsvd10[9];
453 _Static_assert(sizeof(struct nvme_power_state) == 32, "bad size for nvme_power_state");
455 #define NVME_SERIAL_NUMBER_LENGTH 20
456 #define NVME_MODEL_NUMBER_LENGTH 40
457 #define NVME_FIRMWARE_REVISION_LENGTH 8
459 struct nvme_controller_data {
461 /* bytes 0-255: controller capabilities and features */
466 /** pci subsystem vendor id */
470 uint8_t sn[NVME_SERIAL_NUMBER_LENGTH];
473 uint8_t mn[NVME_MODEL_NUMBER_LENGTH];
475 /** firmware revision */
476 uint8_t fr[NVME_FIRMWARE_REVISION_LENGTH];
478 /** recommended arbitration burst */
481 /** ieee oui identifier */
484 /** multi-interface capabilities */
487 /** maximum data transfer size */
496 /** RTD3 Resume Latency */
499 /** RTD3 Enter Latency */
502 /** Optional Asynchronous Events Supported */
503 uint32_t oaes; /* bitfield really */
505 /** Controller Attributes */
506 uint32_t ctratt; /* bitfield really */
508 uint8_t reserved1[12];
510 /** FRU Globally Unique Identifier */
513 uint8_t reserved2[128];
515 /* bytes 256-511: admin command set attributes */
517 /** optional admin command support */
519 /* supports security send/receive commands */
520 uint16_t security : 1;
522 /* supports format nvm command */
525 /* supports firmware activate/download commands */
526 uint16_t firmware : 1;
528 /* supports namespace management commands */
531 uint16_t oacs_rsvd : 12;
534 /** abort command limit */
537 /** asynchronous event request limit */
540 /** firmware updates */
542 /* first slot is read-only */
543 uint8_t slot1_ro : 1;
545 /* number of firmware slots */
546 uint8_t num_slots : 3;
548 uint8_t frmw_rsvd : 4;
551 /** log page attributes */
553 /* per namespace smart/health log page */
554 uint8_t ns_smart : 1;
556 uint8_t lpa_rsvd : 7;
559 /** error log page entries */
562 /** number of power states supported */
565 /** admin vendor specific command configuration */
567 /* admin vendor specific commands use spec format */
568 uint8_t spec_format : 1;
570 uint8_t avscc_rsvd : 7;
573 /** Autonomous Power State Transition Attributes */
575 /* Autonmous Power State Transitions supported */
576 uint8_t apst_supp : 1;
578 uint8_t apsta_rsvd : 7;
581 /** Warning Composite Temperature Threshold */
584 /** Critical Composite Temperature Threshold */
587 /** Maximum Time for Firmware Activation */
590 /** Host Memory Buffer Preferred Size */
593 /** Host Memory Buffer Minimum Size */
596 /** Name space capabilities */
598 /* if nsmgmt, report tnvmcap and unvmcap */
603 /** Replay Protected Memory Block Support */
604 uint32_t rpmbs; /* Really a bitfield */
606 /** Extended Device Self-test Time */
609 /** Device Self-test Options */
610 uint8_t dsto; /* Really a bitfield */
612 /** Firmware Update Granularity */
615 /** Keep Alive Support */
618 /** Host Controlled Thermal Management Attributes */
619 uint16_t hctma; /* Really a bitfield */
621 /** Minimum Thermal Management Temperature */
624 /** Maximum Thermal Management Temperature */
627 /** Sanitize Capabilities */
628 uint32_t sanicap; /* Really a bitfield */
630 uint8_t reserved3[180];
631 /* bytes 512-703: nvm command set attributes */
633 /** submission queue entry size */
639 /** completion queue entry size */
645 /** Maximum Outstanding Commands */
648 /** number of namespaces */
651 /** optional nvm command support */
653 uint16_t compare : 1;
654 uint16_t write_unc : 1;
656 uint16_t reserved: 13;
659 /** fused operation support */
662 /** format nvm attributes */
665 /** volatile write cache */
668 uint8_t reserved : 7;
671 /* TODO: flesh out remaining nvm command set attributes */
672 uint8_t reserved5[178];
674 /* bytes 704-2047: i/o command set attributes */
675 uint8_t reserved6[1344];
677 /* bytes 2048-3071: power state descriptors */
678 struct nvme_power_state power_state[32];
680 /* bytes 3072-4095: vendor specific */
682 } __packed __aligned(4);
684 _Static_assert(sizeof(struct nvme_controller_data) == 4096, "bad size for nvme_controller_data");
686 struct nvme_namespace_data {
688 /** namespace size */
691 /** namespace capacity */
694 /** namespace utilization */
697 /** namespace features */
699 /** thin provisioning */
700 uint8_t thin_prov : 1;
701 uint8_t reserved1 : 7;
704 /** number of lba formats */
707 /** formatted lba size */
710 uint8_t extended : 1;
711 uint8_t reserved2 : 3;
714 /** metadata capabilities */
716 /* metadata can be transferred as part of data prp list */
717 uint8_t extended : 1;
719 /* metadata can be transferred with separate metadata pointer */
722 uint8_t reserved3 : 6;
725 /** end-to-end data protection capabilities */
727 /* protection information type 1 */
730 /* protection information type 2 */
733 /* protection information type 3 */
736 /* first eight bytes of metadata */
737 uint8_t md_start : 1;
739 /* last eight bytes of metadata */
743 /** end-to-end data protection type settings */
745 /* protection information type */
748 /* 1 == protection info transferred at start of metadata */
749 /* 0 == protection info transferred at end of metadata */
750 uint8_t md_start : 1;
752 uint8_t reserved4 : 4;
755 uint8_t reserved5[98];
757 /** lba format support */
765 /** relative performance */
768 uint32_t reserved6 : 6;
771 uint8_t reserved6[192];
773 uint8_t vendor_specific[3712];
774 } __packed __aligned(4);
776 _Static_assert(sizeof(struct nvme_namespace_data) == 4096, "bad size for nvme_namepsace_data");
780 /* 0x00 - reserved */
781 NVME_LOG_ERROR = 0x01,
782 NVME_LOG_HEALTH_INFORMATION = 0x02,
783 NVME_LOG_FIRMWARE_SLOT = 0x03,
784 NVME_LOG_CHANGED_NAMESPACE = 0x04,
785 NVME_LOG_COMMAND_EFFECT = 0x05,
786 /* 0x06-0x7F - reserved */
787 /* 0x80-0xBF - I/O command set specific */
788 NVME_LOG_RES_NOTIFICATION = 0x80,
789 /* 0xC0-0xFF - vendor specific */
792 * The following are Intel Specific log pages, but they seem
793 * to be widely implemented.
795 INTEL_LOG_READ_LAT_LOG = 0xc1,
796 INTEL_LOG_WRITE_LAT_LOG = 0xc2,
797 INTEL_LOG_TEMP_STATS = 0xc5,
798 INTEL_LOG_ADD_SMART = 0xca,
799 INTEL_LOG_DRIVE_MKT_NAME = 0xdd,
802 * HGST log page, with lots ofs sub pages.
804 HGST_INFO_LOG = 0xc1,
807 struct nvme_error_information_entry {
809 uint64_t error_count;
812 struct nvme_status status;
813 uint16_t error_location;
816 uint8_t vendor_specific;
817 uint8_t reserved[35];
818 } __packed __aligned(4);
820 _Static_assert(sizeof(struct nvme_error_information_entry) == 64, "bad size for nvme_error_information_entry");
822 union nvme_critical_warning_state {
827 uint8_t available_spare : 1;
828 uint8_t temperature : 1;
829 uint8_t device_reliability : 1;
830 uint8_t read_only : 1;
831 uint8_t volatile_memory_backup : 1;
832 uint8_t reserved : 3;
836 _Static_assert(sizeof(union nvme_critical_warning_state) == 1, "bad size for nvme_critical_warning_state");
838 struct nvme_health_information_page {
840 union nvme_critical_warning_state critical_warning;
842 uint16_t temperature;
843 uint8_t available_spare;
844 uint8_t available_spare_threshold;
845 uint8_t percentage_used;
847 uint8_t reserved[26];
850 * Note that the following are 128-bit values, but are
851 * defined as an array of 2 64-bit values.
853 /* Data Units Read is always in 512-byte units. */
854 uint64_t data_units_read[2];
855 /* Data Units Written is always in 512-byte units. */
856 uint64_t data_units_written[2];
857 /* For NVM command set, this includes Compare commands. */
858 uint64_t host_read_commands[2];
859 uint64_t host_write_commands[2];
860 /* Controller Busy Time is reported in minutes. */
861 uint64_t controller_busy_time[2];
862 uint64_t power_cycles[2];
863 uint64_t power_on_hours[2];
864 uint64_t unsafe_shutdowns[2];
865 uint64_t media_errors[2];
866 uint64_t num_error_info_log_entries[2];
867 uint32_t warning_temp_time;
868 uint32_t error_temp_time;
869 uint16_t temp_sensor[8];
871 uint8_t reserved2[296];
872 } __packed __aligned(4);
874 _Static_assert(sizeof(struct nvme_health_information_page) == 512, "bad size for nvme_health_information_page");
876 struct nvme_firmware_page {
879 uint8_t slot : 3; /* slot for current FW */
880 uint8_t reserved : 5;
884 uint64_t revision[7]; /* revisions for 7 slots */
885 uint8_t reserved2[448];
886 } __packed __aligned(4);
888 _Static_assert(sizeof(struct nvme_firmware_page) == 512, "bad size for nvme_firmware_page");
890 struct intel_log_temp_stats
893 uint64_t overtemp_flag_last;
894 uint64_t overtemp_flag_life;
898 uint64_t max_oper_temp;
899 uint64_t min_oper_temp;
901 } __packed __aligned(4);
903 _Static_assert(sizeof(struct intel_log_temp_stats) == 13 * 8, "bad size for intel_log_temp_stats");
905 #define NVME_TEST_MAX_THREADS 128
907 struct nvme_io_test {
909 enum nvme_nvm_opcode opc;
911 uint32_t time; /* in seconds */
912 uint32_t num_threads;
914 uint64_t io_completed[NVME_TEST_MAX_THREADS];
917 enum nvme_io_test_flags {
920 * Specifies whether dev_refthread/dev_relthread should be
921 * called during NVME_BIO_TEST. Ignored for other test
924 NVME_TEST_FLAG_REFTHREAD = 0x1,
927 struct nvme_pt_command {
930 * cmd is used to specify a passthrough command to a controller or
933 * The following fields from cmd may be specified by the caller:
935 * * nsid (namespace id) - for admin commands only
938 * Remaining fields must be set to 0 by the caller.
940 struct nvme_command cmd;
943 * cpl returns completion status for the passthrough command
946 * The following fields will be filled out by the driver, for
947 * consumption by the caller:
949 * * status (except for phase)
951 * Remaining fields will be set to 0 by the driver.
953 struct nvme_completion cpl;
955 /* buf is the data buffer associated with this passthrough command. */
959 * len is the length of the data buffer associated with this
960 * passthrough command.
965 * is_read = 1 if the passthrough command will read data into the
966 * supplied buffer from the controller.
968 * is_read = 0 if the passthrough command will write data from the
969 * supplied buffer to the controller.
974 * driver_lock is used by the driver only. It must be set to 0
977 struct mtx * driver_lock;
980 #define nvme_completion_is_error(cpl) \
981 ((cpl)->status.sc != 0 || (cpl)->status.sct != 0)
983 void nvme_strvis(uint8_t *dst, const uint8_t *src, int dstlen, int srclen);
989 struct nvme_namespace;
990 struct nvme_controller;
991 struct nvme_consumer;
993 typedef void (*nvme_cb_fn_t)(void *, const struct nvme_completion *);
995 typedef void *(*nvme_cons_ns_fn_t)(struct nvme_namespace *, void *);
996 typedef void *(*nvme_cons_ctrlr_fn_t)(struct nvme_controller *);
997 typedef void (*nvme_cons_async_fn_t)(void *, const struct nvme_completion *,
998 uint32_t, void *, uint32_t);
999 typedef void (*nvme_cons_fail_fn_t)(void *);
1001 enum nvme_namespace_flags {
1002 NVME_NS_DEALLOCATE_SUPPORTED = 0x1,
1003 NVME_NS_FLUSH_SUPPORTED = 0x2,
1006 int nvme_ctrlr_passthrough_cmd(struct nvme_controller *ctrlr,
1007 struct nvme_pt_command *pt,
1008 uint32_t nsid, int is_user_buffer,
1011 /* Admin functions */
1012 void nvme_ctrlr_cmd_set_feature(struct nvme_controller *ctrlr,
1013 uint8_t feature, uint32_t cdw11,
1014 void *payload, uint32_t payload_size,
1015 nvme_cb_fn_t cb_fn, void *cb_arg);
1016 void nvme_ctrlr_cmd_get_feature(struct nvme_controller *ctrlr,
1017 uint8_t feature, uint32_t cdw11,
1018 void *payload, uint32_t payload_size,
1019 nvme_cb_fn_t cb_fn, void *cb_arg);
1020 void nvme_ctrlr_cmd_get_log_page(struct nvme_controller *ctrlr,
1021 uint8_t log_page, uint32_t nsid,
1022 void *payload, uint32_t payload_size,
1023 nvme_cb_fn_t cb_fn, void *cb_arg);
1025 /* NVM I/O functions */
1026 int nvme_ns_cmd_write(struct nvme_namespace *ns, void *payload,
1027 uint64_t lba, uint32_t lba_count, nvme_cb_fn_t cb_fn,
1029 int nvme_ns_cmd_write_bio(struct nvme_namespace *ns, struct bio *bp,
1030 nvme_cb_fn_t cb_fn, void *cb_arg);
1031 int nvme_ns_cmd_read(struct nvme_namespace *ns, void *payload,
1032 uint64_t lba, uint32_t lba_count, nvme_cb_fn_t cb_fn,
1034 int nvme_ns_cmd_read_bio(struct nvme_namespace *ns, struct bio *bp,
1035 nvme_cb_fn_t cb_fn, void *cb_arg);
1036 int nvme_ns_cmd_deallocate(struct nvme_namespace *ns, void *payload,
1037 uint8_t num_ranges, nvme_cb_fn_t cb_fn,
1039 int nvme_ns_cmd_flush(struct nvme_namespace *ns, nvme_cb_fn_t cb_fn,
1041 int nvme_ns_dump(struct nvme_namespace *ns, void *virt, off_t offset,
1044 /* Registration functions */
1045 struct nvme_consumer * nvme_register_consumer(nvme_cons_ns_fn_t ns_fn,
1046 nvme_cons_ctrlr_fn_t ctrlr_fn,
1047 nvme_cons_async_fn_t async_fn,
1048 nvme_cons_fail_fn_t fail_fn);
1049 void nvme_unregister_consumer(struct nvme_consumer *consumer);
1051 /* Controller helper functions */
1052 device_t nvme_ctrlr_get_device(struct nvme_controller *ctrlr);
1053 const struct nvme_controller_data *
1054 nvme_ctrlr_get_data(struct nvme_controller *ctrlr);
1056 /* Namespace helper functions */
1057 uint32_t nvme_ns_get_max_io_xfer_size(struct nvme_namespace *ns);
1058 uint32_t nvme_ns_get_sector_size(struct nvme_namespace *ns);
1059 uint64_t nvme_ns_get_num_sectors(struct nvme_namespace *ns);
1060 uint64_t nvme_ns_get_size(struct nvme_namespace *ns);
1061 uint32_t nvme_ns_get_flags(struct nvme_namespace *ns);
1062 const char * nvme_ns_get_serial_number(struct nvme_namespace *ns);
1063 const char * nvme_ns_get_model_number(struct nvme_namespace *ns);
1064 const struct nvme_namespace_data *
1065 nvme_ns_get_data(struct nvme_namespace *ns);
1066 uint32_t nvme_ns_get_stripesize(struct nvme_namespace *ns);
1068 int nvme_ns_bio_process(struct nvme_namespace *ns, struct bio *bp,
1069 nvme_cb_fn_t cb_fn);
1071 /* Command building helper functions -- shared with CAM */
1073 void nvme_ns_flush_cmd(struct nvme_command *cmd, uint32_t nsid)
1076 cmd->opc = NVME_OPC_FLUSH;
1081 void nvme_ns_rw_cmd(struct nvme_command *cmd, uint32_t rwcmd, uint32_t nsid,
1082 uint64_t lba, uint32_t count)
1086 cmd->cdw10 = lba & 0xffffffffu;
1087 cmd->cdw11 = lba >> 32;
1088 cmd->cdw12 = count-1;
1095 void nvme_ns_write_cmd(struct nvme_command *cmd, uint32_t nsid,
1096 uint64_t lba, uint32_t count)
1098 nvme_ns_rw_cmd(cmd, NVME_OPC_WRITE, nsid, lba, count);
1102 void nvme_ns_read_cmd(struct nvme_command *cmd, uint32_t nsid,
1103 uint64_t lba, uint32_t count)
1105 nvme_ns_rw_cmd(cmd, NVME_OPC_READ, nsid, lba, count);
1109 void nvme_ns_trim_cmd(struct nvme_command *cmd, uint32_t nsid,
1110 uint32_t num_ranges)
1112 cmd->opc = NVME_OPC_DATASET_MANAGEMENT;
1114 cmd->cdw10 = num_ranges - 1;
1115 cmd->cdw11 = NVME_DSM_ATTR_DEALLOCATE;
1118 extern int nvme_use_nvd;
1120 #endif /* _KERNEL */
1122 #endif /* __NVME_H__ */