2
0
mirror of https://git.missingno.dev/kolibrios-nvme-driver/ synced 2024-12-22 22:08:47 +01:00
kolibrios-nvme-driver/drivers/nvme/nvme.asm

1020 lines
26 KiB
NASM
Raw Normal View History

2024-04-17 21:44:10 +02:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; 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 ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2024-03-28 23:43:18 +01:00
format PE DLL native
entry START
API_VERSION equ 0 ;debug
SRV_GETVERSION equ 0
2024-03-30 04:42:01 +01:00
__DEBUG__ = 1
__DEBUG_LEVEL__ = 1
DRIVER_VERSION = 1
DBG_INFO = 1
NULLPTR = 0
2024-03-30 04:42:01 +01:00
2024-03-28 23:43:18 +01:00
section ".flat" code readable writable executable
include "../proc32.inc"
include "../struct.inc"
include "../macros.inc"
2024-03-30 04:42:01 +01:00
include "../fdo.inc"
2024-03-28 23:43:18 +01:00
include "../pci.inc"
2024-03-30 04:42:01 +01:00
include "../peimport.inc"
2024-03-31 21:43:38 +02:00
include "nvme.inc"
2024-04-21 02:29:06 +02:00
include "macros.inc"
2024-06-13 00:03:01 +02:00
include "lib.asm"
2024-03-28 23:43:18 +01:00
proc START c, reason:dword
2024-05-26 19:24:34 +02:00
cmp [reason], DRV_ENTRY
jne .err
2024-03-28 23:43:18 +01:00
.entry:
DEBUGF DBG_INFO, "Detecting NVMe hardware...\n"
2024-05-26 19:24:34 +02:00
call detect_nvme
2024-04-28 02:11:57 +02:00
test eax, eax
2024-05-26 19:24:34 +02:00
jz .err
mov eax, dword [p_nvme_devices]
test eax, eax
2024-04-28 02:11:57 +02:00
jz .err
2024-04-28 19:50:40 +02:00
xor ecx, ecx
2024-04-28 19:50:40 +02:00
.loop:
2024-05-26 00:56:58 +02:00
mov ebx, dword [p_nvme_devices]
stdcall device_is_compat, ebx
2024-04-28 19:50:40 +02:00
test eax, eax
jz @f
2024-05-26 00:56:58 +02:00
stdcall nvme_init, ebx
test eax, eax
jz .err
2024-03-28 23:43:18 +01:00
2024-05-26 22:54:59 +02:00
;@@:
;inc ecx
;cmp ecx, dword [pcidevs_len]
;jne .loop
2024-05-26 19:24:34 +02:00
invoke RegService, my_service, service_proc
ret
.err:
call nvme_cleanup
2024-05-26 19:24:34 +02:00
xor eax, eax
ret
2024-03-28 23:43:18 +01:00
endp
proc service_proc stdcall, ioctl:dword
2024-05-26 19:24:34 +02:00
mov ebx, [ioctl]
mov eax, [ebx+IOCTL.io_code]
cmp eax, SRV_GETVERSION
jne @F
mov eax, [ebx+IOCTL.output]
cmp [ebx+IOCTL.out_size], 4
jne @F
mov dword [eax], API_VERSION
xor eax, eax
ret
2024-03-28 23:43:18 +01:00
@@:
or eax, -1
ret
2024-03-28 23:43:18 +01:00
endp
proc set_cdw0 stdcall, pci:dword, y:dword, opcode:byte
2024-06-04 22:06:32 +02:00
stdcall get_new_cid, [pci], [y]
2024-06-04 22:06:32 +02:00
shl eax, 16
or al, [opcode]
2024-06-04 22:06:32 +02:00
ret
endp
2024-06-09 19:07:01 +02:00
; See pages 161-205 of the NVMe 1.4 specification for reference
proc nvme_identify stdcall, pci:dword, nsid:dword, dptr:dword, cns:byte
2024-06-14 22:12:53 +02:00
sub esp, sizeof.SQ_ENTRY
; It's important to check if CNS is a valid value here. In revision 1.0
; CNS is a 1 bit field and a two bit field in revision 1.1, using invalid
; values results in undefined behavior (see page 162 of NVMe 1.4 spec)
if __DEBUG__
push esi
2024-06-14 22:12:53 +02:00
mov esi, [pci]
mov esi, dword [esi + pcidev.io_addr]
mov eax, dword [esi + NVME_MMIO.VS]
cmp eax, VS110
jne @f
cmp [cns], 11b
jle .ok
DEBUGF DBG_INFO, "(NVMe) FATAL ERROR: INVALID CNS VALUE ON v1.1.0 CONTROLLERS\n"
jmp .err
@@:
cmp eax, VS100
jne .ok
cmp [cns], 1b
jle .ok
DEBUGF DBG_INFO, "(NVMe) FATAL ERROR: INVALID CNS VALUE ON v1.0.0 CONTROLLERS\n"
jmp .err
.err:
jmp @b
.ok:
pop esi
end if
2024-06-14 22:12:53 +02:00
stdcall memset, esp, 0, sizeof.SQ_ENTRY
2024-06-03 20:14:59 +02:00
2024-05-28 21:01:25 +02:00
mov eax, [nsid]
2024-06-14 22:12:53 +02:00
mov dword [esp + SQ_ENTRY.nsid], eax
2024-05-28 21:01:25 +02:00
mov eax, [dptr]
2024-06-14 22:12:53 +02:00
mov dword [esp + SQ_ENTRY.dptr], eax
stdcall set_cdw0, [pci], ADMIN_QUEUE, ADM_CMD_IDENTIFY
2024-06-14 22:12:53 +02:00
mov dword [esp + SQ_ENTRY.cdw0], eax
2024-05-28 21:01:25 +02:00
mov al, [cns]
2024-06-14 22:12:53 +02:00
mov byte [esp + SQ_ENTRY.cdw10], al
stdcall sqytdbl_write, [pci], ADMIN_QUEUE, esp
2024-06-03 20:14:59 +02:00
2024-06-14 22:12:53 +02:00
add esp, sizeof.SQ_ENTRY
ret
2024-03-30 04:42:01 +01:00
endp
; See pages 348-349 of the NVMe 1.4 specification for information on creating namespaces
proc create_namespace stdcall, pci:dword, cid:word
push esi
invoke AllocPage
test eax, eax
jz .fail
invoke GetPhysAddr
stdcall nvme_identify, [pci], 0xffffffff, eax, CNS_IDNS
.fail:
pop esi
ret
endp
; See page 248 of the NVMe 1.4 specification for reference
proc determine_active_nsids stdcall, pci:dword
push esi edi ebx
xor esi, esi
xor edi, edi
invoke KernelAlloc, 0x1000
test eax, eax
jz .fail
mov esi, eax
invoke GetPhysAddr
stdcall nvme_identify, [pci], 0, eax, CNS_IDCS
mov eax, dword [esi + IDENTC.nn]
mov edx, [pci]
mov dword [edx + pcidev.nn], eax
test eax, eax
jz .fail
invoke KernelAlloc, 0x1000
test eax, eax
jz .fail
mov edi, eax
invoke GetPhysAddr
mov ebx, eax
xor ecx, ecx
.loop:
cmp ecx, dword [esi + IDENTC.nn]
jg .success
stdcall nvme_identify, [pci], ecx, ebx, CNS_IDNS ; identify N'th namespace
push ecx
xor ecx, ecx
; We want to check if entire struct is zeroed out, if so, it is not an active NSID,
; otherwise it is
.inner_loop:
mov eax, dword [edi + ecx * 4]
test eax, eax
jnz .is_active_nsid
inc ecx
cmp ecx, 0x1000 / 4
jne .inner_loop
pop ecx
inc ecx
jmp .loop
.is_active_nsid:
pop ecx ; pop active NSID value into ecx
mov eax, [pci]
mov eax, dword [eax + pcidev.nsids]
mov dword [eax + ecx * 4], ecx
inc ecx
jmp .loop
.fail:
invoke KernelFree, esi ; assumes KernelFree can check if its nullptr (hopefully)
invoke KernelFree, edi
pop ebx edi esi
xor eax, eax
ret
.success:
invoke KernelFree, esi
invoke KernelFree, edi
pop ebx edi esi
xor eax, eax
inc eax
ret
endp
; See page 101 of the NVMe 1.4 specification for reference
proc create_io_completion_queue stdcall, pci:dword, prp1:dword, qid:dword, ien:byte
sub esp, sizeof.SQ_ENTRY
stdcall memset, esp, 0, sizeof.SQ_ENTRY
stdcall set_cdw0, [pci], ADMIN_QUEUE, ADM_CMD_CRE_IO_COMPLETION_QUEUE
mov dword [esp + SQ_ENTRY.cdw0], eax
mov eax, [prp1]
mov dword [esp + SQ_ENTRY.dptr], eax
mov eax, sizeof.CQ_ENTRY shl 16 ; CDW10.QSIZE
or eax, [qid] ; CDW10.QID
mov dword [esp + SQ_ENTRY.cdw10], eax
movzx eax, [ien] ; CDW11.IEN
or eax, 0x1 ; CDW11.PC
; Don't set CDW11.IV since we're not using MSI-X or MSI vector
mov dword [esp + SQ_ENTRY.cdw11], eax
stdcall sqytdbl_write, [pci], ADMIN_QUEUE, esp
add esp, sizeof.SQ_ENTRY
ret
endp
; See page 103-104 of the NVMe 1.4 specification for reference
proc create_io_submission_queue stdcall, pci:dword, prp1:dword, qid:dword, cqid:word
sub esp, sizeof.SQ_ENTRY
stdcall memset, esp, 0, sizeof.SQ_ENTRY
stdcall set_cdw0, [pci], ADMIN_QUEUE, ADM_CMD_CRE_IO_SUBMISSION_QUEUE
mov dword [esp + SQ_ENTRY.cdw0], eax
mov eax, [prp1]
mov dword [esp + SQ_ENTRY.dptr], eax
mov eax, sizeof.SQ_ENTRY shl 16 ; CDW10.QSIZE
or eax, [qid]
mov dword [esp + SQ_ENTRY.cdw10], eax
movzx eax, [cqid]
shl eax, 16 ; CDW11.CQID
or eax, 0x1 ; CDW11.PC (always set this to 1 as some devices may not support non-contiguous pages)
; TODO: Set CDW10.QPRIO
mov dword [esp + SQ_ENTRY.cdw11], eax
stdcall sqytdbl_write, [pci], ADMIN_QUEUE, esp
add esp, sizeof.SQ_ENTRY
ret
endp
; See page 95-96 of the NVMe 1.4 specification for reference
proc abort stdcall, pci:dword, cid:word, sqid:word
sub esp, sizeof.SQ_ENTRY
stdcall memset, esp, 0, sizeof.SQ_ENTRY
stdcall set_cdw0, [pci], ADMIN_QUEUE, ADM_CMD_ABORT
mov dword [esp + SQ_ENTRY.cdw0], eax
movzx eax, [cid]
shl eax, 16
or eax, word [sqid]
mov dword [esp + SQ_ENTRY.cdw10], eax
stdcall sqytdbl_write, [pci], ADMIN_QUEUE, esp
add esp, sizeof.SQ_ENTRY
ret
endp
; See page 205 of the NVMe 1.4 specification for reference
2024-06-14 22:12:53 +02:00
proc set_features stdcall, pci:dword, dptr:dword, fid:byte, cdw11:dword
2024-06-14 22:12:53 +02:00
sub esp, sizeof.SQ_ENTRY
stdcall memset, esp, 0, sizeof.SQ_ENTRY
stdcall set_cdw0, [pci], ADMIN_QUEUE, ADM_CMD_SET_FEATURES
2024-06-14 22:12:53 +02:00
mov dword [esp + SQ_ENTRY.cdw0], eax
mov eax, [dptr]
2024-06-14 22:12:53 +02:00
mov dword [esp + SQ_ENTRY.dptr], eax
movzx eax, [fid]
;or eax, 1 shl 31 ; CDW10.SV
2024-06-14 22:12:53 +02:00
mov dword [esp + SQ_ENTRY.cdw10], eax
mov eax, [cdw11]
mov dword [esp + SQ_ENTRY.cdw11], eax
stdcall sqytdbl_write, [pci], ADMIN_QUEUE, esp
2024-06-14 22:12:53 +02:00
add esp, sizeof.SQ_ENTRY
ret
endp
2024-06-04 21:43:12 +02:00
; See page 105 of the NVMe 1.4 specification for reference
proc delete_io_completion_queue stdcall, pci:dword, qid:word
sub esp, sizeof.SQ_ENTRY
stdcall memset, esp, 0, sizeof.SQ_ENTRY
stdcall set_cdw0, [pci], ADMIN_QUEUE, ADM_CMD_DEL_IO_COMPLETION_QUEUE
mov dword [esp + SQ_ENTRY.cdw0], eax
mov ax, [qid]
mov word [esp + SQ_ENTRY.cdw10], ax
stdcall sqytdbl_write, [pci], ADMIN_QUEUE, esp
add esp, sizeof.SQ_ENTRY
ret
endp
2024-06-04 21:43:12 +02:00
; See page 114-116 of the NVMe 1.4 specification for reference
proc get_features stdcall, pci:dword, dptr:dword, sel:byte, fid:byte
sub esp, sizeof.SQ_ENTRY
stdcall memset, esp, 0, sizeof.SQ_ENTRY
stdcall set_cdw0, [pci], ADMIN_QUEUE, ADM_CMD_GET_FEATURES
mov dword [esp + SQ_ENTRY.cdw0], eax
movzx eax, [sel]
and eax, 111b
shl eax, 8 ; CDW10.SEL
or eax, byte [fid] ; CDW10.FID
mov dword [esp + SQ_ENTRY.cdw10], eax
2024-06-04 21:43:12 +02:00
mov eax, [dptr]
mov dword [esp + SQ_ENTRY.dptr], eax
2024-06-04 21:43:12 +02:00
; TODO: Implement CDW14.UUID?
stdcall sqytdbl_write, [pci], ADMIN_QUEUE, esp
add esp, sizeof.SQ_ENTRY
2024-06-04 21:43:12 +02:00
ret
endp
; See page 105-106 of the NVMe 1.4 specification for reference
proc delete_io_submission_queue stdcall, pci:dword, qid:word
sub esp, sizeof.SQ_ENTRY
stdcall memset, esp, 0, sizeof.SQ_ENTRY
stdcall set_cdw0, [pci], ADMIN_QUEUE, ADM_CMD_DEL_IO_SUBMISSION_QUEUE
mov dword [esp + SQ_ENTRY.cdw0], eax
mov ax, [qid]
mov word [esp + SQ_ENTRY.cdw10], ax
stdcall sqytdbl_write, [pci], ADMIN_QUEUE, esp
add esp, sizeof.SQ_ENTRY
ret
endp
2024-06-08 18:25:29 +02:00
; See page 117-118 of the NVMe 1.4 specification for reference
; INCOMPLETE
proc get_log_page stdcall, pci:dword, dptr:dword, lid:byte
sub esp, sizeof.SQ_ENTRY
stdcall memset, esp, 0, sizeof.SQ_ENTRY
stdcall set_cdw0, [pci], ADMIN_QUEUE, ADM_CMD_GET_LOG_PAGE
mov dword [esp + SQ_ENTRY.cdw0], eax
2024-06-08 18:25:29 +02:00
mov eax, [dptr]
mov dword [esp + SQ_ENTRY.dptr], eax
add esp, sizeof.SQ_ENTRY
2024-06-08 18:25:29 +02:00
ret
endp
; See page 269-271 of the NVMe 1.4 specification for reference
proc nvme_write stdcall, pci:dword, qid:word, slba:qword, nlb:dword, dsm:byte
; TODO: Use IDENTC.NOIOB to construct read/write commands that don't
; cross the I/O boundary to achieve optimal performance
; Also add DPTR/MPTR
sub esp, sizeof.SQ_ENTRY
stdcall memset, esp, 0 sizeof.SQ_ENTRY
stdcall set_cdw0, [pci], [qid], NVM_CMD_WRITE
mov dword [esp + SQ_ENTRY.cdw0], eax ; CDW0
; Starting LBA (SLBA)
mov eax, dword [slba]
mov dword [esp + SQ_ENTRY.cdw10], eax
mov eax, dword [slba + 4]
mov dword [esp + SQ_ENTRY.cdw11], eax
mov ax, [nlb]
mov word [esp + SQ_ENTRY.cdw12], ax
mov al, [dsm]
mov byte [esp + SQ_ENTRY.cdw13], al
stdcall sqytdbl_write, [pci], [qid], esp
add esp, sizeof.SQ_ENTRY
ret
endp
2024-06-08 18:25:29 +02:00
proc detect_nvme
2024-05-26 19:24:34 +02:00
invoke GetPCIList
2024-03-28 23:43:18 +01:00
mov edx, eax
2024-03-28 23:43:18 +01:00
.check_dev:
mov ebx, dword [eax + PCIDEV.class]
and ebx, 0x00ffff00 ; retrieve class/subclass code only
cmp ebx, 0x00010800 ; Mass Storage Controller - Non-Volatile Memory Controller
je .found_dev
2024-03-28 23:43:18 +01:00
.next_dev:
mov eax, dword [eax + PCIDEV.fd]
2024-03-28 23:43:18 +01:00
cmp eax, edx
jne .check_dev
2024-04-28 02:11:57 +02:00
jmp .exit_success
.found_dev:
2024-04-28 02:11:57 +02:00
push edx eax
2024-04-29 03:06:13 +02:00
PDEBUGF DBG_INFO, "PCI(%u.%u.%u): Detected NVMe device...\n", byte [eax + PCIDEV.bus], byte [eax + PCIDEV.devfn]
2024-04-28 02:11:57 +02:00
cmp dword [pcidevs_len], TOTAL_PCIDEVS
jne @f
2024-04-28 02:11:57 +02:00
pop eax edx
jmp .exit_success
@@:
2024-04-28 02:11:57 +02:00
inc dword [pcidevs_len]
mov ebx, dword [p_nvme_devices]
test ebx, ebx
jnz @f
2024-05-26 00:56:58 +02:00
invoke KernelAlloc, sizeof.pcidev
test eax, eax
2024-04-28 02:11:57 +02:00
jz .err_no_mem
mov dword [p_nvme_devices], eax
2024-05-26 00:56:58 +02:00
DEBUGF DBG_INFO, "(NVMe) Allocated pcidev struct at 0x%x\n", [p_nvme_devices]
@@:
mov ecx, dword [pcidevs_len]
2024-04-28 02:11:57 +02:00
dec ecx
pop eax
2024-05-26 00:56:58 +02:00
mov ebx, dword [p_nvme_devices]
2024-04-28 02:11:57 +02:00
movzx edx, byte [eax + PCIDEV.bus]
2024-05-26 00:56:58 +02:00
mov byte [ebx + pcidev.bus], dl
2024-04-28 02:11:57 +02:00
movzx edx, byte [eax + PCIDEV.devfn]
2024-05-26 00:56:58 +02:00
mov byte [ebx + pcidev.devfn], dl
2024-04-28 02:11:57 +02:00
pop edx
jmp .next_dev
.err_no_mem:
pop eax edx
xor eax, eax
ret
2024-04-28 02:11:57 +02:00
.exit_success:
xor eax, eax
inc eax
ret
endp
2024-04-29 03:06:13 +02:00
proc device_is_compat stdcall, pci:dword
2024-05-30 01:29:01 +02:00
push esi edx ecx
2024-05-29 21:43:14 +02:00
mov esi, [pci]
2024-05-30 22:06:50 +02:00
invoke PciRead8, dword [esi + pcidev.bus], dword [esi + pcidev.devfn], PCI_header00.interrupt_line
mov byte [esi + pcidev.iline], al
2024-05-29 21:43:14 +02:00
invoke PciRead32, dword [esi + pcidev.bus], dword [esi + pcidev.devfn], PCI_header00.base_addr_0
and eax, 0xfffffff0
test eax, eax
2024-04-29 03:06:13 +02:00
jz .failure
2024-05-30 01:29:01 +02:00
mov edx, eax
push edx
2024-05-26 00:56:58 +02:00
invoke MapIoMem, eax, sizeof.NVME_MMIO, PG_SW+PG_NOCACHE
test eax, eax
2024-04-29 03:06:13 +02:00
jz .failure
2024-05-29 21:43:14 +02:00
;DEBUGF DBG_INFO, "(NVMe) MMIO allocated at: 0x%x\n", eax
mov dword [esi + pcidev.io_addr], eax
2024-05-29 21:43:14 +02:00
mov eax, dword [eax + NVME_MMIO.CAP + 4]
and eax, CAP_DSTRD
2024-06-09 19:07:01 +02:00
mov byte [esi + pcidev.dstrd], al
2024-05-29 21:43:14 +02:00
2024-05-30 01:29:01 +02:00
; 1003h + ((2y + 1) * (4 << CAP.DSTRD))
mov eax, 4
shl ax, cl
mov ecx, NVM_ASQS
shl ecx, 1
inc ecx
imul ecx, eax
add ecx, 0x1003
pop edx
invoke MapIoMem, edx, ecx, PG_SW+PG_NOCACHE
mov dword [esi + pcidev.io_addr], eax
2024-05-30 01:29:01 +02:00
mov eax, dword [eax + NVME_MMIO.VS]
2024-05-26 01:26:51 +02:00
DEBUGF DBG_INFO, "(NVMe) Controller version: 0x%x\n", eax
2024-06-13 00:03:01 +02:00
mov dword [esi + pcidev.version], eax
2024-05-30 01:29:01 +02:00
pop ecx edx esi
2024-04-28 02:11:57 +02:00
xor eax, eax
inc eax
ret
2024-04-29 03:06:13 +02:00
.failure:
2024-05-29 21:43:14 +02:00
PDEBUGF DBG_INFO, "PCI(%u.%u.%u): something went wrong checking NVMe device compatibility\n", byte [esi + pcidev.bus], byte [esi + pcidev.devfn]
2024-05-30 01:29:01 +02:00
pop ecx edx esi
2024-05-26 00:56:58 +02:00
xor eax, eax
ret
endp
; nvme_init: Initializes the NVMe controller
2024-04-29 03:06:13 +02:00
proc nvme_init stdcall, pci:dword
2024-05-30 22:06:50 +02:00
push ebx esi edi
2024-05-30 20:21:45 +02:00
mov esi, dword [pci]
mov edi, dword [esi + pcidev.io_addr]
2024-05-26 01:26:51 +02:00
2024-05-29 21:43:14 +02:00
if 0
2024-05-26 22:54:59 +02:00
mov eax, dword [edi + NVME_MMIO.CAP]
DEBUGF DBG_INFO, "(NVMe) CAP (0-31): 0x%x\n", eax
mov eax, dword [edi + NVME_MMIO.CAP + 4]
DEBUGF DBG_INFO, "(NVMe) CAP (32-63): 0x%x\n", eax
mov eax, dword [edi + NVME_MMIO.CC]
DEBUGF DBG_INFO, "(NVMe) CC: 0x%x\n", eax
mov eax, dword [edi + NVME_MMIO.CSTS]
DEBUGF DBG_INFO, "(NVMe) CSTS: 0x%x\n", eax
2024-05-26 01:26:51 +02:00
end if
2024-05-24 01:15:34 +02:00
; For some reason, bit 7 (No I/O command set supported) is also set to 1 despite bit 0 (NVM command set)
; being set to 1.. so I am not sure if bit 7 should be checked at all.. investigate later.
2024-05-26 22:54:59 +02:00
mov eax, dword [edi + NVME_MMIO.CAP + 4]
test eax, CAP_CSS_NVM_CMDSET
jz .exit_fail
2024-05-26 02:37:42 +02:00
; Reset controller before we configure it
2024-05-26 23:19:25 +02:00
stdcall nvme_controller_reset, edi
if __DEBUG__
stdcall nvme_wait, edi
end if
2024-05-26 22:54:59 +02:00
mov eax, dword [edi + NVME_MMIO.CAP + 4]
and eax, CAP_MPSMIN
shr eax, 16
cmp eax, NVM_MPS
jg .exit_fail
mov eax, dword [edi + NVME_MMIO.CAP + 4]
and eax, CAP_MPSMAX
shr eax, 20
cmp eax, NVM_MPS
2024-05-26 02:37:42 +02:00
jl .exit_fail
2024-06-13 00:03:01 +02:00
; Configure IOSQES, IOCQES, AMS, MPS, CSS
and dword [edi + NVME_MMIO.CC], not (CC_AMS or CC_MPS or CC_CSS or CC_IOSQES or CC_IOCQES)
2024-05-26 22:54:59 +02:00
mov eax, dword [edi + NVME_MMIO.CC]
2024-06-13 00:03:01 +02:00
; CSS = 0 (NVM Command Set)
; AMS = 0 (Round Robin)
; MPS = 0 (4KiB Pages)
; IOSQES = 6 (64B)
; IOCQES = 4 (16B)
or eax, (4 shl 20) or (6 shl 16)
mov dword [edi + NVME_MMIO.CC], eax
2024-05-26 02:37:42 +02:00
; Configure Admin Queue Attributes
2024-05-26 22:54:59 +02:00
mov eax, dword [edi + NVME_MMIO.AQA]
and eax, not (AQA_ASQS or AQA_ACQS)
or eax, NVM_ASQS or (NVM_ACQS shl 16)
2024-05-26 22:54:59 +02:00
mov dword [edi + NVME_MMIO.AQA], eax
2024-05-26 02:37:42 +02:00
; Allocate list of queues
invoke KernelAlloc, sizeof.NVM_QUEUE_ENTRY * NVM_ASQS
test eax, eax
jz .exit_fail
mov dword [esi + pcidev.queue_entries], eax
stdcall memset, eax, 0, sizeof.NVM_QUEUE_ENTRY * NVM_ASQS
2024-05-26 02:37:42 +02:00
; Configure Admin Submission/Completion Queue Base Address
push esi
mov esi, dword [esi + pcidev.queue_entries]
; TODO: Allocate ring buffer? (see page 8 of NVMe 1.4 spec)
invoke CreateRingBuffer, 0x1000, PG_SW
2024-05-26 22:54:59 +02:00
test eax, eax
jz .exit_fail
mov dword [esi + NVM_QUEUE_ENTRY.sq_ptr], eax
invoke GetPhysAddr
2024-05-26 22:54:59 +02:00
mov dword [edi + NVME_MMIO.ASQ], eax
2024-05-26 23:19:25 +02:00
and dword [edi + NVME_MMIO.ASQ + 4], 0
; TODO: Allocate ring buffer? (see page 8 of NVMe 1.4 spec)
invoke CreateRingBuffer, 0x1000, PG_SW
2024-05-26 22:54:59 +02:00
test eax, eax
jz .exit_fail
mov dword [esi + NVM_QUEUE_ENTRY.cq_ptr], eax
invoke GetPhysAddr
2024-05-26 22:54:59 +02:00
mov dword [edi + NVME_MMIO.ACQ], eax
2024-05-26 23:19:25 +02:00
and dword [edi + NVME_MMIO.ACQ + 4], 0
stdcall memset, dword [esi + NVM_QUEUE_ENTRY.sq_ptr], 0, sizeof.SQ_ENTRY * NVM_CMDS
stdcall memset, dword [esi + NVM_QUEUE_ENTRY.cq_ptr], 0, sizeof.CQ_ENTRY * NVM_CMDS
2024-06-10 00:13:01 +02:00
pop esi
2024-06-10 00:13:01 +02:00
; we want to disable all interrupts for now, since the controller randomly
; generates interrupts while starting up
;mov dword [edi + NVME_MMIO.INTMS], 0xffffffff
2024-06-10 00:13:01 +02:00
2024-05-30 22:06:50 +02:00
; Attach interrupt handler
movzx eax, byte [esi + pcidev.iline]
2024-06-03 20:14:59 +02:00
DEBUGF DBG_INFO, "(NVMe) Attaching interrupt handler to IRQ %u\n", eax
invoke AttachIntHandler, eax, irq_handler, 0
2024-05-30 22:06:50 +02:00
test eax, eax
jz .exit_fail
DEBUGF DBG_INFO, "(NVMe) Successfully attached interrupt handler\n"
2024-06-03 20:14:59 +02:00
; Restart the controller
stdcall nvme_controller_start, edi
;mov dword [edi + NVME_MMIO.INTMC], 0xffffffff ; re-enable interrupts
2024-06-03 20:14:59 +02:00
2024-06-09 19:07:01 +02:00
invoke KernelAlloc, 0x1000
2024-05-28 21:01:25 +02:00
test eax, eax
jz .exit_fail
mov ebx, eax
2024-06-09 19:07:01 +02:00
invoke GetPhysAddr
; pci:dword, nsid:dword, dptr:dword, cns:byte
stdcall nvme_identify, [pci], 0, eax, CNS_IDCS
mov edi, ebx
lea ebx, byte [edi + IDENTC.sn]
2024-06-13 00:03:01 +02:00
lea eax, byte [esi + pcidev.serial]
stdcall memcpy, eax, ebx, 20
2024-06-13 00:03:01 +02:00
DEBUGF DBG_INFO, "(NVMe) Serial Number: %s\n", eax
add ebx, 20
2024-06-13 00:03:01 +02:00
lea eax, byte [esi + pcidev.model]
stdcall memcpy, eax, ebx, 40
2024-06-13 00:03:01 +02:00
DEBUGF DBG_INFO, "(NVMe) Model: %s\n", eax
mov edx, dword [esi + pcidev.version]
2024-05-26 22:54:59 +02:00
cmp edx, VS140
2024-06-13 00:03:01 +02:00
jl @f
; This is a reserved field in pre-1.4 controllers
mov al, byte [edi + IDENTC.cntrltype]
2024-06-13 00:03:01 +02:00
cmp al, CNTRLTYPE_IO_CONTROLLER
jne .exit_fail
DEBUGF DBG_INFO, "(NVMe) I/O controller detected...\n"
@@:
mov al, byte [edi + IDENTC.sqes]
2024-06-13 00:03:01 +02:00
and al, 11110000b
cmp al, 0x60 ; maximum submission queue size should at least be 64 bytes
jl .exit_fail
mov al, byte [edi + IDENTC.cqes]
2024-06-13 00:03:01 +02:00
and al, 11110000b
and al, 0x40 ; maximum completion queue entry size should at least be 16 bytes
jl .exit_fail
invoke KernelFree, edi
2024-06-14 22:12:53 +02:00
mov eax, (NVM_ASQS - 1) or ((NVM_ACQS - 1) shl 16) ; CDW11 (set the number of queues we want)
stdcall set_features, [pci], NULLPTR, FID_NUMBER_OF_QUEUES, eax
mov esi, dword [p_nvme_devices]
mov esi, dword [esi + pcidev.queue_entries]
mov esi, dword [esi + NVM_QUEUE_ENTRY.cq_ptr]
mov eax, dword [esi + sizeof.CQ_ENTRY + CQ_ENTRY.cdw0]
DEBUGF DBG_INFO, "(NVMe) Set Features CDW0: 0x%x\n", eax
test ax, ax ; Number of I/O Submission Queues allocated
jz .exit_fail
shl eax, 16
test ax, ax ; Number of I/O Completion Queues allocated
jnz .exit_fail
; Create I/O Queues
; (TODO: create N queue pairs for N CPU cores, see page 8 of NVMe 1.4 spec for an explaination
mov esi, [pci]
mov esi, dword [esi + pcidev.queue_entries]
lea esi, [esi + sizeof.NVM_QUEUE_ENTRY]
invoke CreateRingBuffer, 0x1000, PG_SW
test eax, eax
jz .exit_fail
mov dword [esi + NVM_QUEUE_ENTRY.cq_ptr], eax
invoke GetPhysAddr
stdcall create_io_completion_queue, [pci], eax, 1, IEN_ON
invoke CreateRingBuffer, 0x1000, PG_SW
test eax, eax
jz .exit_fail
mov dword [esi + NVM_QUEUE_ENTRY.sq_ptr], eax
invoke GetPhysAddr
stdcall create_io_submission_queue, [pci], eax, 1, 1
2024-06-13 00:03:01 +02:00
DEBUGF DBG_INFO, "(NVMe) Successfully initialized driver!\n"
2024-05-26 19:24:34 +02:00
xor eax, eax
inc eax
2024-05-30 22:06:50 +02:00
pop edi esi ebx
2024-05-26 19:24:34 +02:00
ret
2024-03-31 21:43:38 +02:00
2024-04-02 01:47:14 +02:00
.exit_fail:
2024-05-30 22:06:50 +02:00
DEBUGF DBG_INFO, "(NVMe) failed to initialize controller\n"
xor eax, eax
2024-05-30 22:06:50 +02:00
pop edi esi ebx
2024-03-31 21:43:38 +02:00
ret
2024-03-31 21:43:38 +02:00
endp
2024-06-12 21:47:32 +02:00
proc get_new_cid stdcall, pci:dword, y:dword
2024-06-12 01:57:02 +02:00
push esi ebx
mov ecx, [y]
2024-06-12 21:47:32 +02:00
imul ecx, sizeof.NVM_QUEUE_ENTRY
mov esi, [pci]
mov esi, dword [esi + pcidev.queue_entries]
lea esi, dword [esi + ecx]
mov eax, dword [esi + NVM_QUEUE_ENTRY.cid_slots]
2024-06-12 01:57:02 +02:00
xor edx, edx
cmp eax, 0xffffffff
jne @f
2024-06-12 21:47:32 +02:00
mov eax, dword [esi + NVM_QUEUE_ENTRY.cid_slots + 4]
2024-06-12 01:57:02 +02:00
add edx, 4
cmp eax, 0xffffffff
jne @f
2024-06-12 21:47:32 +02:00
pop ebx esi
2024-06-12 01:57:02 +02:00
mov eax, -1
ret
@@:
mov ebx, eax
; Equivalant to (~(-X) & ~(X)), retrieves first free bit
not eax
mov ecx, eax
neg ecx
and eax, ecx
or ebx, eax
mov ecx, [y]
2024-06-12 21:47:32 +02:00
mov dword [esi + edx + NVM_QUEUE_ENTRY.cid_slots], ebx
bsf eax, eax
cmp edx, 4
jne @f
add eax, 32
@@:
2024-06-12 01:57:02 +02:00
pop ebx esi
ret
endp
2024-05-26 23:19:25 +02:00
proc nvme_controller_reset stdcall, mmio:dword
2024-05-24 01:15:34 +02:00
2024-05-26 00:56:58 +02:00
DEBUGF DBG_INFO, "(NVMe) Resetting Controller...\n"
2024-05-26 23:19:25 +02:00
push edi
mov edi, dword [mmio]
and dword [edi + NVME_MMIO.CC], 0xfffffffe ; CC.EN = 0
2024-05-24 01:15:34 +02:00
; Wait for controller to be brought to idle state, CSTS.RDY should be cleared to 0 when this happens
.wait:
2024-05-26 23:19:25 +02:00
test dword [edi + NVME_MMIO.CSTS], CSTS_RDY
2024-05-24 01:15:34 +02:00
jnz .wait
2024-05-26 00:56:58 +02:00
DEBUGF DBG_INFO, "(NVMe) Successfully reset controller...\n"
2024-05-26 23:19:25 +02:00
pop edi
2024-05-24 01:15:34 +02:00
ret
endp
2024-05-26 23:19:25 +02:00
proc nvme_controller_start stdcall, mmio:dword
DEBUGF DBG_INFO, "(NVMe) Starting Controller...\n"
2024-05-26 23:19:25 +02:00
push edi
mov edi, dword [mmio]
or dword [edi + NVME_MMIO.CC], 1 ;; CC.EN = 1
; Wait for controller to be brought into active state, CSTS.RDY should be set to 1 when this happens
.wait:
2024-05-26 23:19:25 +02:00
test dword [edi + NVME_MMIO.CSTS], CSTS_RDY
jz .wait
DEBUGF DBG_INFO, "(NVMe) Successfully started controller...\n"
2024-05-26 23:19:25 +02:00
pop edi
ret
endp
2024-05-26 00:56:58 +02:00
; Should be called only after the value of CC.EN has changed
2024-05-26 23:19:25 +02:00
proc nvme_wait stdcall, mmio:dword
2024-05-30 22:06:50 +02:00
push esi
2024-05-26 23:19:25 +02:00
mov esi, [mmio]
mov esi, dword [esi + NVME_MMIO.CAP]
and esi, CAP_TO
shr esi, 24
2024-06-12 01:57:02 +02:00
imul esi, 150 ; TODO: bad time delay, set to appropriate value later
2024-05-26 00:56:58 +02:00
invoke Sleep
2024-05-30 22:06:50 +02:00
pop esi
2024-05-24 01:15:34 +02:00
ret
endp
2024-06-09 19:07:01 +02:00
; Writes to completion queue 'y' head doorbell
2024-06-14 22:12:53 +02:00
proc cqyhdbl_write stdcall, pci:dword, y:word, cqh:word
2024-06-09 19:07:01 +02:00
2024-06-10 00:13:01 +02:00
push esi edi
2024-06-09 19:07:01 +02:00
mov esi, [pci]
; 1000h + ((2y + 1) * (4 << CAP.DSTRD))
2024-06-10 00:13:01 +02:00
movzx eax, [y]
shl al, 1
inc al
2024-06-12 01:57:02 +02:00
mov edx, 4
2024-06-09 19:07:01 +02:00
mov cl, byte [esi + pcidev.dstrd]
2024-06-10 00:13:01 +02:00
shl dx, cl
imul dx, ax
add dx, 0x1000
movzx ecx, [y]
2024-06-12 21:47:32 +02:00
imul ecx, sizeof.NVM_QUEUE_ENTRY
2024-06-12 01:57:02 +02:00
mov edi, dword [esi + pcidev.queue_entries]
2024-06-12 21:47:32 +02:00
lea edi, dword [edi + ecx]
2024-06-09 19:07:01 +02:00
mov esi, dword [esi + pcidev.io_addr]
2024-06-12 01:57:02 +02:00
mov ax, [cqh]
2024-06-10 00:13:01 +02:00
DEBUGF DBG_INFO, "(NVMe) Writing to completion queue doorbell register 0x%x: %u\n", dx, ax
2024-06-09 19:07:01 +02:00
mov word [esi + edx], ax ; Write to CQyHDBL
2024-06-12 21:47:32 +02:00
mov word [edi + NVM_QUEUE_ENTRY.head], ax
2024-06-10 00:13:01 +02:00
pop edi esi
2024-06-09 19:07:01 +02:00
ret
endp
; Writes to submission queue 'y' tail doorbell
proc sqytdbl_write stdcall, pci:dword, y:word, cmd:dword
push ebx esi edi
mov edi, [pci]
mov edi, dword [edi + pcidev.queue_entries]
movzx ecx, [y]
imul ecx, sizeof.NVM_QUEUE_ENTRY
mov edi, dword [edi + ecx + NVM_QUEUE_ENTRY.sq_ptr]
mov esi, [cmd]
mov ecx, dword [esi + SQ_ENTRY.cdw0]
shr ecx, 16 ; Get CID
imul ecx, sizeof.SQ_ENTRY
lea edi, [edi + ecx]
stdcall memcpy, edi, esi, sizeof.SQ_ENTRY
mov edi, [pci]
mov esi, dword [edi + pcidev.io_addr]
mov edi, dword [edi + pcidev.queue_entries]
mov ax, word [edi + NVM_QUEUE_ENTRY.tail]
cmp ax, NVM_ASQS
jl @f
xor ax, ax
@@:
mov esi, [pci]
inc ax
; 1000h + ((2y + 1) * (4 << CAP.DSTRD))
movzx ebx, [y]
shl bl, 1
mov edx, 4
mov cl, byte [esi + pcidev.dstrd]
shl dx, cl
imul dx, bx
add dx, 0x1000
DEBUGF DBG_INFO, "(NVMe) Writing to submission queue doorbell register 0x%x: %u\n", dx, ax
mov esi, dword [esi + pcidev.io_addr]
mov word [esi + edx], ax
movzx ecx, [y]
mov word [edi + NVM_QUEUE_ENTRY.tail], ax
dec ax
stdcall nvme_cmd_wait, [pci], ecx, eax
pop edi esi ebx
ret
endp
; Calculates 2^x
2024-05-30 01:29:01 +02:00
proc pow2 stdcall, x:byte
push ecx
2024-05-30 01:29:01 +02:00
mov cl, [x]
2024-05-29 21:43:14 +02:00
xor eax, eax
inc eax
2024-05-30 01:29:01 +02:00
test cl, cl
2024-05-29 21:43:14 +02:00
jnz @f
2024-05-30 01:29:01 +02:00
pop ecx
2024-05-29 21:43:14 +02:00
ret
2024-05-29 21:57:45 +02:00
@@:
2024-05-30 01:29:01 +02:00
shl eax, cl
pop ecx
ret
endp
proc nvme_cmd_wait stdcall, pci:dword, y:dword, cid:dword
push esi
mov esi, [pci]
movzx ecx, word [cid]
movzx edx, byte [y]
imul edx, sizeof.NVM_QUEUE_ENTRY
mov esi, dword [esi + pcidev.queue_entries]
lea esi, [esi + edx]
imul ecx, sizeof.CQ_ENTRY
mov esi, dword [esi + NVM_QUEUE_ENTRY.cq_ptr]
2024-06-14 22:12:53 +02:00
; WARNING: status bit flips on each pass, count for this later...
2024-06-14 22:12:53 +02:00
@@:
test byte [esi + ecx + CQ_ENTRY.status], CQ_PHASE_TAG
2024-06-14 22:12:53 +02:00
jz @b
pop esi
ret
endp
proc irq_handler
2024-06-14 22:12:53 +02:00
push esi edi
mov esi, dword [p_nvme_devices]
; check if the NVMe device generated an interrupt
invoke PciRead16, dword [esi + pcidev.bus], dword [esi + pcidev.devfn], PCI_header00.status
test al, 1000b ; check interrupt status
jz .not_our_irq
mov edi, esi
mov edi, dword [edi + pcidev.io_addr]
mov dword [edi + NVME_MMIO.INTMS], 0x1
mov esi, dword [esi + pcidev.queue_entries]
movzx ecx, word [esi + NVM_QUEUE_ENTRY.head]
mov dx, word [esi + NVM_QUEUE_ENTRY.tail]
DEBUGF DBG_INFO, "IRQ (head): 0x%x, (tail): 0x%x\n", cx, dx
cmp cx, dx
je .end
mov edx, ecx
imul edx, sizeof.CQ_ENTRY
mov esi, dword [p_nvme_devices]
mov esi, dword [esi + pcidev.queue_entries]
mov esi, dword [esi + NVM_QUEUE_ENTRY.cq_ptr]
mov ax, word [esi + edx + CQ_ENTRY.status]
2024-06-09 20:57:42 +02:00
and ax, not CQ_PHASE_TAG ; ignore phase tag bit
DEBUGF DBG_INFO, "(NVMe) Status: 0x%x\n", ax
2024-06-09 20:57:42 +02:00
test al, al ; check status code (0 on success)
jz .ok
2024-06-09 19:07:01 +02:00
.error:
2024-06-09 19:07:01 +02:00
; we have to initiate a controller reset if a admin command encounters
; a fatal error or if a completion is not received for a deletion
; of a submission or completion queue (section 10.1 - page 400 of NVMe 1.4 spec)
mov esi, dword [p_nvme_devices]
mov esi, dword [esi + pcidev.io_addr]
2024-06-09 19:07:01 +02:00
stdcall nvme_controller_reset, esi
stdcall nvme_controller_start, esi
2024-06-13 19:53:59 +02:00
jmp .end
.ok:
mov eax, dword [esi + edx + CQ_ENTRY.cdw0]
inc ecx
; TODO: Check how many commands were consumed later
stdcall cqyhdbl_write, dword [p_nvme_devices], 0, ecx
2024-06-13 19:53:59 +02:00
.end:
mov edi, dword [p_nvme_devices]
mov edi, dword [edi + pcidev.io_addr]
mov dword [edi + NVME_MMIO.INTMC], 0x1
2024-06-09 19:07:01 +02:00
; Interrupt handled by driver, return 1
2024-06-03 20:14:59 +02:00
xor eax, eax
2024-06-09 19:07:01 +02:00
inc eax
pop edi esi
2024-06-03 20:14:59 +02:00
ret
2024-06-03 18:02:36 +02:00
.not_our_irq:
; Interrupt not handled by driver, return 0
xor eax, eax
pop edi esi
ret
2024-05-30 22:06:50 +02:00
endp
2024-03-31 21:43:38 +02:00
proc nvme_cleanup
DEBUGF DBG_INFO, "(NVMe): Cleaning up...\n"
2024-05-26 22:54:59 +02:00
mov ecx, dword [pcidevs_len]
2024-04-29 03:06:13 +02:00
mov eax, dword [p_nvme_devices]
test eax, eax
jnz .loop
ret
2024-04-29 03:06:13 +02:00
.loop:
;invoke KernelFree, dword [p_nvme_devices + ecx * sizeof.pcidev + pcidev.ident_ptr]
2024-05-26 00:56:58 +02:00
dec ecx
test ecx, ecx
jnz .loop
2024-04-29 03:06:13 +02:00
invoke KernelFree, dword [p_nvme_devices]
2024-05-26 22:54:59 +02:00
@@:
2024-04-29 03:06:13 +02:00
ret
2024-03-28 23:43:18 +01:00
2024-04-29 03:06:13 +02:00
endp
2024-03-28 23:43:18 +01:00
;all initialized data place here
align 4
p_nvme_devices dd 0
pcidevs_len dd 0
2024-04-17 22:04:49 +02:00
my_service db "NVMe",0 ;max 16 chars include zero
if __DEBUG__
include_debug_strings
end if
2024-03-28 23:43:18 +01:00
align 4
data fixups
end data