mirror of
https://git.missingno.dev/kolibrios-nvme-driver/
synced 2024-12-22 13:58:47 +01:00
415 lines
12 KiB
PHP
415 lines
12 KiB
PHP
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
;; ;;
|
|
;; Copyright (C) KolibriOS team 2004-2024. All rights reserved. ;;
|
|
;; Distributed under terms of the GNU General Public License ;;
|
|
;; ;;
|
|
;; GNU GENERAL PUBLIC LICENSE ;;
|
|
;; Version 2, June 1991 ;;
|
|
;; ;;
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
; Supported NVMe Controller Version
|
|
NVM_SUPPORTED_CONTROLLER_VERSION = 0x00010400 ; (v1.4.0)
|
|
|
|
NVM_MPS = 0 ; Memory Page Size (2 ^ (12 + MPS))
|
|
NVM_ASQS = 64 ; Admin Submission Queue Size
|
|
NVM_ACQS = NVM_ASQS ; Admin Completion Queue Size
|
|
|
|
; Opcodes for NVM commands
|
|
NVM_CMD_FLUSH = 0x00
|
|
NVM_CMD_WRITE = 0x01
|
|
NVM_CMD_READ = 0x02
|
|
NVM_CMD_WRITE_UNCORRECTABLE = 0x04
|
|
NVM_CMD_COMPARE = 0x05
|
|
NVM_CMD_WRITE_ZEROES = 0x08
|
|
NVM_CMD_DATASET_MANAGEMENT = 0x09
|
|
NVM_CMD_VERIFY = 0x0C
|
|
NVM_CMD_RESERVATION_REG = 0x0D
|
|
NVM_CMD_RESERVATION_REPORT = 0x0E
|
|
NVM_CMD_RESERVATION_ACQUIRE = 0x11
|
|
NVM_CMD_RESERVATION_RELEASE = 0x15
|
|
NVM_CMD_COPY = 0x19
|
|
|
|
; Opcodes for admin commands
|
|
ADM_CMD_DEL_IO_SUBMISSION_QUEUE = 0x00
|
|
ADM_CMD_CRE_IO_SUBMISSION_QUEUE = 0x01
|
|
ADM_CMD_GET_LOG_PAGE = 0x02
|
|
ADM_CMD_DEL_IO_COMPLETION_QUEUE = 0x04
|
|
ADM_CMD_CRE_IO_COMPLETION_QUEUE = 0x05
|
|
ADM_CMD_IDENTIFY = 0x06
|
|
ADM_CMD_ABORT = 0x08
|
|
ADM_CMD_SET_FEATURES = 0x09
|
|
ADM_CMD_GET_FEATURES = 0x0A
|
|
|
|
; fuse (fused operation): In a fused operation, a complex command is created by 'fusing' together
|
|
; two simpler commands. This field specifies whether this command is part
|
|
; of a fused operation, and if so, which command it is in the sequence:
|
|
; 00b -> Normal operation
|
|
; 01b -> Fused operation, first command
|
|
; 10b -> Fused operation, second command
|
|
; 11b -> Reserved
|
|
NO_FUSE = 0
|
|
FUSE_OP_FIRST_CMD = 1 shl 8
|
|
FUSE_OP_SECOND_CMD = 2 shl 8
|
|
|
|
; sel (PRP or SGL for data transfer): This field specifies whether PRPs or SGLs are used for any
|
|
; data transfer associated with the command. PRPs shall be
|
|
; used for all Admin commands for NVMe over PCIe implementations.
|
|
; SGLs shall be used for all Admin and I/O commands for NVMe over
|
|
; Fabrics implementations (i.e., field set to 01b):
|
|
; 00b -> PRPs are used for this transfer
|
|
; 01b -> SGLs are used for this transfer, MPTR will contain address of
|
|
; a single contiguous physical buffer that is byte aligned
|
|
; 10b -> SGLs are used for this transfer. MPTR will contain address of
|
|
; an SGL segment containing exactly one SGL descriptor that is
|
|
; QWORD aligned
|
|
; 11b -> Reserved
|
|
SEL_PRP = 0
|
|
SEL_SGL = 1 shl 14
|
|
|
|
; Controller or Namespace Structure (CNS) specifies the information to be returned to the host.
|
|
CNS_IDNS = 0x0 ; Namespace data structure (NSID)
|
|
CNS_IDCS = 0x1 ; Controller data structure
|
|
CNS_ANIDL = 0x2 ; Active namespace ID list (NSID)
|
|
CNS_NIDL = 0x3 ; Namespace identification descriptor list (NSID)
|
|
CNS_NVM_SL = 0x4 ; NVM Set List
|
|
|
|
; Optional Admin Command Support (OACS) values
|
|
OACS_SEC_SEN_RECV_SUPPORTED = 1 shl 0
|
|
OACS_FMT_NVM_SUPPORTED = 1 shl 1
|
|
OACS_FIRM_COMDL_SUPPORTED = 1 shl 2
|
|
OACS_NSMAN_SUPPORTED = 1 shl 3
|
|
|
|
; scope is all attached namespaces or all namespaces in NVM subsystem
|
|
NSID_BROADCAST = 0xFFFFFFFF
|
|
|
|
NSSRC_RESET = 0x4E564D65 ; "NVMe" (initiates a NVMe subsystem reset)
|
|
|
|
; NVMe Capabilities
|
|
CAP_MQES = 0xff
|
|
CAP_CQR = 1 shl 16
|
|
CAP_AMS = (1 shl 17) or (1 shl 18)
|
|
CAP_TO = 0xff000000
|
|
CAP_DSTRD = 1 or (1 shl 1) or (1 shl 2) or (1 shl 3)
|
|
CAP_NSSRS = 1 shl 4
|
|
CAP_CSS_NVM_CMDSET = 1 shl 5
|
|
CAP_CSS_NOIO = 1 shl 12
|
|
CAP_BPS = 1 shl 14
|
|
CAP_CPS_COSCOP = 1 shl 15
|
|
CAP_CPS_DOSCOP = 1 shl 16
|
|
CAP_CPS_NVMSCOP = CAP_CPS_COSCOP or CAP_CPS_DOSCOP
|
|
CAP_MPSMIN = (1 shl 17) or (1 shl 18) or (1 shl 19) or (1 shl 20)
|
|
CAP_MPSMAX = (1 shl 21) or (1 shl 22) or (1 shl 23) or (1 shl 24)
|
|
CAP_PMRS = 1 shl 25
|
|
CAP_CMBS = 1 shl 26
|
|
CAP_NSSS = 1 shl 27
|
|
CAP_CRMS_CRWMS = 1 shl 28
|
|
CAP_CRMS_CRIMS = 1 shl 29
|
|
|
|
; Controller Configuration Bits
|
|
CC_EN = 1
|
|
CC_CSS = (1 shl 4) or (1 shl 5) or (1 shl 6)
|
|
CC_MPS = (1 shl 7) or (1 shl 8) or (1 shl 9) or (1 shl 10)
|
|
CC_AMS = (1 shl 11) or (1 shl 12) or (1 shl 13)
|
|
CC_SHN = (1 shl 14) or (1 shl 15)
|
|
CC_IOSQES = (1 shl 16) or (1 shl 17) or (1 shl 18) or (1 shl 19)
|
|
CC_IOCQES = (1 shl 20) or (1 shl 21) or (1 shl 22) or (1 shl 23)
|
|
CC_CRIME = 1 shl 24
|
|
|
|
CC_DEFAULT_IOSQES = 6 shl 16
|
|
CC_DEFAULT_IOCQES = 4 shl 16
|
|
|
|
; Completion Queue Entry Status Field Values
|
|
CQ_PHASE_TAG = 1 shl 0
|
|
CQ_STATUS_SC = 0xfe
|
|
CQ_STATUS_SCT = (1 shl 9) or (1 shl 10) or (1 shl 11)
|
|
CQ_STATUS_CRD = (1 shl 12) or (1 shl 13)
|
|
CQ_STATUS_M = 1 shl 14
|
|
CQ_STATUS_DNR = 1 shl 15
|
|
|
|
; Completion Queue Entry Status Field - Status Code Type Values
|
|
CQ_STATUS_SCT_GCS = 0x0 ; Generic Command Status
|
|
CQ_STATUS_SCT_CSS = 0x1 ; Command Specific Status
|
|
CQ_STATUS_SCT_MADIE = 0x2 ; Media and Data Integrity Errors
|
|
CQ_STATUS_SCT_PRS = 0x3 ; Path Related Status
|
|
|
|
; Completion Queue Entry Status Field - Status Code Generic Command Values
|
|
CQ_STATUS_SC_GCS_SUCCESS = 0x00 ; Successful Completion
|
|
CQ_STATUS_SC_GCS_ICOP = 0x01 ; Invalid Command Opcode
|
|
CQ_STATUS_SC_GCS_IFIC = 0x02 ; Invalid Field in Command
|
|
CQ_STATUS_SC_GCS_CIDC = 0x03 ; Command ID Conflict
|
|
CQ_STATUS_SC_GCS_DTE = 0x04 ; Data Transfer Error
|
|
CQ_STATUS_SC_GCS_CAPLN = 0x05 ; Commands Aborted due to Power Loss Notification
|
|
CQ_STATUS_SC_GCS_INERR = 0x06 ; Internal Error
|
|
CQ_STATUS_SC_GCS_CAR = 0x07 ; Command Abort Requested
|
|
CQ_STATUS_SC_GCS_CASQD = 0x08 ; Command Aborted due to SQ Deletion
|
|
CQ_STATUS_SC_GCS_CAFFC = 0x09 ; Command Aborted due to Failed Fused Command
|
|
CQ_STATUS_SC_GCS_CAMFC = 0x0A ; Command Aborted due to Missing Fused Command
|
|
CQ_STATUS_SC_GCS_INNOF = 0x0B ; Invalid Namespace or Format
|
|
CQ_STATUS_SC_GCS_CSE = 0x0C ; Command Sequence Error
|
|
CQ_STATUS_SC_GCS_INSGL = 0x0D ; Invalid SGL Segment Descriptor
|
|
CQ_STATUS_SC_GCS_INNSGL = 0x0E ; Invalid Number of SGL Descriptors
|
|
CQ_STATUS_SC_GCS_OPDEN = 0x15 ; Operation Denied
|
|
CQ_STATUS_SC_GCS_NSIWP = 0x20 ; Namespace is Write Protected
|
|
CQ_STATUS_SC_GCS_CINT = 0x21 ; Command Interrupted
|
|
CQ_STATUS_SC_GCS_TTE = 0x22 ; Transient Transport Error
|
|
|
|
; Completion Queue Entry Status Field - Status Code Media and Data Integrity Errors
|
|
CQ_STATUS_SC_MADIE_WF = 0x80 ; Write Fault
|
|
CQ_STATUS_SC_MADIE_URE = 0x81 ; Unrecovered Read Error
|
|
CQ_STATUS_SC_MADIE_ACDEN = 0x86 ; Access Denied
|
|
CQ_STATUS_SC_MADIE_DOULB = 0x87 ; Deallocated or Unwritten Logical Block
|
|
|
|
; Controller Status (CSTS) Values
|
|
CSTS_RDY = 1
|
|
CSTS_CFS = 1 shl 1
|
|
CSTS_SHST = (1 shl 2) or (1 shl 3)
|
|
CSTS_NSSRO = 1 shl 4
|
|
CSTS_PP = 1 shl 5
|
|
|
|
; Admin Queue Attributes (AQA) Values
|
|
AQA_ASQS = 0xfff
|
|
AQA_ACQS = 0xfff shl 16
|
|
|
|
struct NVME_MMIO
|
|
CAP dq ? ; Controller Capabilities
|
|
VS dd ? ; Version
|
|
INTMS dd ? ; Interrupt Mask Set
|
|
INTMC dd ? ; Interrupt Mask Clear
|
|
CC dd ? ; Controller Configuration
|
|
dd ? ; Reserved
|
|
CSTS dd ? ; Controller Status
|
|
NSSR dd ? ; NVM Subsystem Reset
|
|
AQA dd ? ; Admin Queue Attributes
|
|
ASQ dq ? ; Admin Submission Queue Base Address
|
|
ACQ dq ? ; Admin Completion Queue Base Address
|
|
CMBLOC dd ? ; Controller Memory Buffer Location
|
|
CMBSZ dd ? ; Controller Memory Buffer Size
|
|
BPINFO dd ? ; Boot Partition Information
|
|
BPRSEL dd ? ; Boot Partition Read Select
|
|
BPMBL dq ? ; Boot Partition Memory Buffer Location
|
|
CMBMSC dd ? ; Controller Memory Buffer Memory Space
|
|
CMBSTS dd ? ; Controller Memory Buffer Status
|
|
rb 3492 ; Reserved
|
|
PMRCAP dd ? ; Persistent Memory Capabilities
|
|
PMRCTL dd ? ; Persistent Memory Region Control
|
|
PMRSTS dd ? ; Persistent Memory Region Status
|
|
PMREBS dd ? ; Persistent Memory Region Elasticity Buffer Size
|
|
PMRSWTP dd ? ; Persistent Memory Region Sustained Write Throughput
|
|
PMRMSC dq ? ; Persistent Memory Region Controller Memory Space Control
|
|
rb 484 ; Reserved
|
|
SQ0TDBL dd ? ; Submission Queue 0 Tail Doorbell (Admin)
|
|
ends
|
|
|
|
; Submission Queue Entry (64 bytes)
|
|
struct SQ_ENTRY
|
|
cdw0 dd ?
|
|
nsid dd ?
|
|
cdw2 dd ?
|
|
cdw3 dd ?
|
|
mptr dq ?
|
|
dptr dq 2
|
|
cdw10 dd ?
|
|
cdw11 dd ?
|
|
cdw12 dd ?
|
|
cdw13 dd ?
|
|
cdw14 dd ?
|
|
cdw15 dd ?
|
|
ends
|
|
|
|
; Completion Queue Entry (16 bytes)
|
|
struct CQ_ENTRY
|
|
cdw0 dd ?
|
|
rd 1 ; reserved
|
|
sqhd dw ?
|
|
sqid dw ?
|
|
cid dw ?
|
|
status dw ?
|
|
ends
|
|
|
|
struct pcidev
|
|
bus db ?
|
|
devfn db ?
|
|
rw 1
|
|
mmio_ptr dd ?
|
|
sq_ptr dd ?
|
|
cq_ptr dd ?
|
|
|
|
ends
|
|
TOTAL_PCIDEVS = 4
|
|
TOTAL_PCIDEVS_MALLOC_SZ = TOTAL_PCIDEVS * sizeof.pcidev
|
|
|
|
; Identify Controller Data Structure
|
|
struct IDENTC
|
|
|
|
pci_vid dw ? ; checked
|
|
pci_ssvid dw ? ; checked
|
|
serial db 20 ; checked
|
|
model db 40 ; checked
|
|
firm_rev dq ? ; checked
|
|
rab db ? ; checked
|
|
ieee db 3 ; checked
|
|
cmic db ? ; checked
|
|
mdts db ? ; checked
|
|
cntlid dw ? ; checked
|
|
ver dd ? ; checked
|
|
rtd3r dd ? ; checked
|
|
rtd3e dd ? ; checked
|
|
oaes dd ? ; checked
|
|
ctratt dd ? ; checked
|
|
rrls dw ? ; checked
|
|
rb 9 ; reserved
|
|
cntrltyp db ? ; checked
|
|
fguid dq 2 ; checked
|
|
crdt1 dw ? ; checked
|
|
crdt2 dw ? ; checked
|
|
crdt3 dw ? ; checked
|
|
rb 106 ; reserved
|
|
rb 16 ; reserved (NVMMI)
|
|
oacs dw ? ; checked
|
|
acl db ? ; checked
|
|
aerl db ? ; checked
|
|
frmw db ? ; checked
|
|
lpa db ? ; checked
|
|
elpe db ? ; checked
|
|
npss db ? ; checked
|
|
avscc db ? ; checked
|
|
apsta db ? ; checked
|
|
wctemp dw ? ; checked
|
|
cctemp dw ? ; checked
|
|
mtfa dw ? ; checked
|
|
hmpre dd ? ; checked
|
|
hmmin dd ? ; checked
|
|
tnvmcap dq ? ; checked
|
|
unvmcap dq ? ; checked
|
|
rpmbs dd ? ; checked
|
|
edstt dw ? ; checked
|
|
dsto db ? ; checked
|
|
fwug db ? ; checked
|
|
kas dw ? ; checked
|
|
hctma dw ? ; checked
|
|
mntmt dw ? ; checked
|
|
mxtmt dw ? ; checked
|
|
sanicap dd ? ; checked
|
|
hmminds dd ? ; checked
|
|
hmmaxd dw ? ; checked
|
|
nsetidmax dw ? ; checked
|
|
endgidmax dw ? ; checked
|
|
anatt db ? ; checked
|
|
anacap db ? ; checked
|
|
anagrpmax dd ? ; checked
|
|
nanagrpid dd ? ; checked
|
|
pels dd ? ; checked
|
|
rb 156 ; checked
|
|
sqes db ? ; checked
|
|
cqes db ? ; checked
|
|
maxcmd dw ? ; checked
|
|
nn dd ? ; checked
|
|
oncs dw ? ; checked
|
|
fuses dw ? ; checked
|
|
fna db ? ; checked
|
|
vwc db ? ; checked
|
|
awun dw ? ; checked
|
|
awupf dw ? ; checked
|
|
nvscc db ? ; checked
|
|
nwpc db ? ; checked
|
|
acwu dw ? ; checked
|
|
rb 2 ; checked
|
|
sgls dd ? ; checked
|
|
mnan dd ? ; checked
|
|
rb 224 ; checked
|
|
subnqn dw 128 ; checked
|
|
rb 768 ; checked
|
|
rb 256 ; checked
|
|
psd0 db 32 ; checked
|
|
psd1 db 32
|
|
psd2 db 32
|
|
psd3 db 32
|
|
psd4 db 32
|
|
psd5 db 32
|
|
psd6 db 32
|
|
psd7 db 32
|
|
psd8 db 32
|
|
psd9 db 32
|
|
psd10 db 32
|
|
psd11 db 32
|
|
psd12 db 32
|
|
psd13 db 32
|
|
psd14 db 32
|
|
psd15 db 32
|
|
psd16 db 32
|
|
psd17 db 32
|
|
psd18 db 32
|
|
psd19 db 32
|
|
psd20 db 32
|
|
psd21 db 32
|
|
psd22 db 32
|
|
psd23 db 32
|
|
psd24 db 32
|
|
psd25 db 32
|
|
psd26 db 32
|
|
psd27 db 32
|
|
psd28 db 32
|
|
psd29 db 32
|
|
psd30 db 32
|
|
psd31 db 32
|
|
rb 1024
|
|
ends
|
|
|
|
; Identify Namespace Data Structure
|
|
struct IDENTN
|
|
nsze dq ?
|
|
ncap dq ?
|
|
nuse dq ?
|
|
nsfeat db ?
|
|
nlbaf db ?
|
|
flbas db ?
|
|
mc db ?
|
|
dpc db ?
|
|
dps db ?
|
|
nmic db ?
|
|
rescap db ?
|
|
fpi db ?
|
|
dlfeat db ?
|
|
nawun dw ?
|
|
nawupf dw ?
|
|
nacwu dw ?
|
|
nabsn dw ?
|
|
nabo dw ?
|
|
nabspf dw ?
|
|
noiob dw ?
|
|
nvmcap dq ?
|
|
dq ?
|
|
npwg dw ?
|
|
npwa dw ?
|
|
npdg dw ?
|
|
npda dw ?
|
|
nows dw ?
|
|
rb 18
|
|
anagrpid dd ?
|
|
rb 3
|
|
nsattr db ?
|
|
nvmsetid dw ?
|
|
endgid dw ?
|
|
nguid dq ?
|
|
dq ?
|
|
eui64 dq ?
|
|
lbaf0 dd ?
|
|
lbaf1 dd ?
|
|
lbaf2 dd ?
|
|
lbaf3 dd ?
|
|
lbaf4 dd ?
|
|
lbaf5 dd ?
|
|
lbaf6 dd ?
|
|
lbaf7 dd ?
|
|
lbaf8 dd ?
|
|
lbaf9 dd ?
|
|
lbaf10 dd ?
|
|
lbaf11 dd ?
|
|
lbaf12 dd ?
|
|
lbaf13 dd ?
|
|
lbaf14 dd ?
|
|
lbaf15 dd ?
|
|
rb 3904
|
|
ends
|