2
0
mirror of https://git.missingno.dev/kolibrios-nvme-driver/ synced 2025-02-06 20:26:49 +01:00

refactor: organize code into proper components

This commit is contained in:
ramenu 2024-04-17 15:35:01 -04:00
parent bc11bcfa37
commit d0ddd1233d

View File

@ -5,8 +5,6 @@
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;driver sceletone
format PE DLL native
entry START
@ -27,21 +25,29 @@ include "../peimport.inc"
include "nvme.inc"
proc START c, reason:dword
cmp [reason], DRV_ENTRY
jne .exit
jne .err
.entry:
push esi
DEBUGF DBG_INFO,"Detecting NVMe hardware...\n"
call detect
pop esi
DEBUGF DBG_INFO, "Detecting NVMe hardware...\n"
call detect_nvme
test eax, eax
jz .exit
jz .err ; no NVMe device found?
DEBUGF DBG_INFO, "Found NVMe device...\n"
stdcall nvme_init, dword [eax + PCIDEV.bus], dword [eax + PCIDEV.devfn]
test eax, eax
jz .err ; failed to initialize controller?
invoke RegService, my_service, service_proc
ret
.exit:
.err:
call nvme_cleanup
xor eax, eax
ret
endp
proc service_proc stdcall, ioctl:dword
@ -53,177 +59,156 @@ proc service_proc stdcall, ioctl:dword
mov eax, [ebx+IOCTL.output]
cmp [ebx+IOCTL.out_size], 4
jne .fail
jne @F
mov dword [eax], API_VERSION
xor eax, eax
ret
@@:
.fail:
or eax, -1
ret
endp
proc memset stdcall, p_data:dword, val:byte, sz:dword
mov bh, byte [val]
xor eax, eax
mov bh, byte [val]
xor ecx, ecx
@@:
mov byte [p_data + eax], bh
inc eax
test eax, dword [sz]
jnz @b
ret
mov byte [p_data + ecx], bh
inc ecx
cmp ecx, dword [sz]
jne @b
xor eax, eax
ret
endp
; Submit a Command in the Admin Submission Queue
proc submit_asq stdcall, p_sq:dword
xor eax, eax
ret
xor eax, eax
ret
endp
proc nvme_identify stdcall, nsid:dword, dptr:dword, cns:byte
sub esp, sizeof.SQ_ENTRY
stdcall memset, esp, 0, sizeof.SQ_ENTRY
mov eax, dword [nsid]
mov [esp + SQ_ENTRY.nsid], eax
mov eax, dword [dptr]
mov dword [esp + SQ_ENTRY.dptr], eax
; TODO: setting CID to 0 for now but later on keep a list of unique list of identifiers
mov dword [esp + SQ_ENTRY.cdw0], ADM_CMD_IDENTIFY
mov ah, byte [cns]
mov byte [esp + SQ_ENTRY.cdw10], ah
stdcall submit_asq, esp
add esp, sizeof.SQ_ENTRY
xor eax, eax
ret
sub esp, sizeof.SQ_ENTRY
stdcall memset, esp, 0, sizeof.SQ_ENTRY
mov eax, dword [nsid]
mov dword [esp + SQ_ENTRY.nsid], eax
mov eax, dword [dptr]
mov dword [esp + SQ_ENTRY.dptr], eax
mov dword [esp + SQ_ENTRY.cdw0], ADM_CMD_IDENTIFY ; TODO: setting CID to 0 for now but later on keep a unique list of identifiers
mov ah, byte [cns]
mov byte [esp + SQ_ENTRY.cdw10], ah
stdcall submit_asq, esp
add esp, sizeof.SQ_ENTRY
xor eax, eax
ret
endp
proc detect
push ebx
;
proc detect_nvme
invoke GetPCIList
mov edx, eax
.check_dev:
mov ecx, [eax+PCIDEV.class]
and ecx, 0x00ffff00 ; retrieve class/subclass code only
cmp ecx, 0x00010800 ; Mass Storage Controller - Non-Volatile Memory Controller
je .check_cap
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
.next_dev:
mov eax, [eax + PCIDEV.fd]
mov eax, dword [eax + PCIDEV.fd]
cmp eax, edx
jne .check_dev
xor eax, eax ; no more PCI devices to enumerate?
; no more PCI devices to enumerate?
xor eax, eax
pop ebx
ret
.check_cap:
push eax
movzx ebx, [eax + PCIDEV.bus]
mov [pcidev_bus], ebx
movzx ebx, [eax + PCIDEV.devfn]
mov [pcidev_devfn], ebx
invoke PciRead16, [pcidev_bus], [pcidev_devfn], PCI_header00.status
test ax, 0x10 ; check capabilities list bit
jnz .got_cap
pop eax
.got_cap:
DEBUGF DBG_INFO,"Found NVMe device with capabilities\n"
invoke PciRead8, [pcidev_bus], [pcidev_devfn], PCI_header00.cap_ptr
and eax, 11111100b
mov edi, eax
@@:
invoke PciRead32, [pcidev_bus], [pcidev_devfn], edi
mov ecx, eax
;and ecx, 0xff
mov eax, ecx
movzx edi, ah
test edi, edi
jnz @b
.found_dev:
ret
endp
; nvme_init: Initializes the NVMe controller
proc nvme_init, bus:dword, devfn:dword
invoke PciRead32, [bus], [devfn], PCI_header00.base_addr_0
and eax, 0xfffffff0
test eax, eax
jz .exit_fail
invoke MapIoMem, eax, sizeof.NVME_REG_MAP, PG_SW+PG_NOCACHE
test eax, eax
jz .exit_fail
mov [p_mmap], eax
mov ebx, dword [eax + NVME_REG_MAP.CAP]
DEBUGF DBG_INFO, "(NVMe) Maximum queue entries supported: %u\n", bx
test ebx, CAP_CQR
jz .cqr_not_req
DEBUGF DBG_INFO, "(NVMe) Contiguous queues required\n"
; read BAR0
invoke PciRead32, [pcidev_bus], [pcidev_devfn], PCI_header00.base_addr_0
and eax, 0xfffffff0
test eax, eax
jz .no_mmio
invoke MapIoMem, eax, sizeof.NVME_REG_MAP, PG_SW+PG_NOCACHE
test eax, eax
jz .no_mmio
mov [p_mmap], eax
mov ebx, dword [eax + NVME_REG_MAP.CAP]
DEBUGF DBG_INFO,"(NVMe) Maximum queue entries supported: %u\n", bx
test ebx, CAP_CQR
jz .cqr_not_req
DEBUGF DBG_INFO,"(NVMe) Contiguous queues required\n"
.cqr_not_req:
mov ebx, dword [eax + NVME_REG_MAP.CAP + 4]
mov ecx, ebx
test ebx, CAP_CSS_NVM_CMDSET
jz .exit_fail
and ebx, CAP_MPSMIN
and ecx, CAP_MPSMAX
shr ebx, 16
shr ecx, 16
DEBUGF DBG_INFO,"(NVMe) Memory page size minimum: %u\n", ebx
DEBUGF DBG_INFO,"(NVMe) Memory page size maximum: %u\n", ecx
mov ebx, dword [eax + NVME_REG_MAP.CAP + 4]
mov ecx, ebx
test ebx, CAP_CSS_NVM_CMDSET
jz .exit_fail
mov ebx, dword [eax + NVME_REG_MAP.CC]
mov ecx, ebx
and ebx, CC_IOSQES
and ecx, CC_IOCQES
shl ebx, 16
shl ecx, 16
DEBUGF DBG_INFO,"(NVMe) I/O Submission Queue Entry Size: %u\n", ebx
DEBUGF DBG_INFO,"(NVMe) I/O completion queue entry size: %u\n", ecx
; Initialize the controller
if __DEBUG__
and ebx, CAP_MPSMIN
and ecx, CAP_MPSMAX
shr ebx, 16
shr ecx, 16
DEBUGF DBG_INFO,"(NVMe) Memory page size minimum: %u\n", ebx
DEBUGF DBG_INFO,"(NVMe) Memory page size maximum: %u\n", ecx
invoke KernelAlloc, sizeof.NVME_IDENT_CONTROLLER
test eax, eax
jz .alloc_ident_controller_fail
mov [p_ident], eax
stdcall nvme_identify, 0, dword [p_ident], CNS_IDCS
mov ecx, dword [p_ident]
mov edx, dword [ecx + NVME_IDENT_CONTROLLER.tnvmcap]
DEBUGF DBG_INFO,"Total NVMe SSD capacity: %ub\n", edx
mov ebx, dword [eax + NVME_REG_MAP.CC]
mov ecx, ebx
and ebx, CC_IOSQES
and ecx, CC_IOCQES
shl ebx, 16
shl ecx, 16
; TODO: Change entry sizes to their appropriate values
DEBUGF DBG_INFO,"(NVMe) I/O Submission Queue entry Size: %u\n", ebx
DEBUGF DBG_INFO,"(NVMe) I/O completion queue entry size: %u\n", ecx
end if
; return successfully
call nvme_cleanup
pop eax
pop ebx
xor eax, eax
inc eax
ret
.alloc_ident_controller_fail:
DEBUGF DBG_INFO,"ERROR: failed to allocate %u bytes for 'NVME_IDENT_CONTROLLER'\n", sizeof.NVME_IDENT_CONTROLLER
jmp .exit_fail
.no_mmio:
DEBUGF DBG_INFO,"ERROR: NVMe Device has no MMIO\n"
.exit_fail:
pop eax
pop ebx
DEBUGF DBG_INFO, "ERROR: failed to initialize NVMe controller\n"
xor eax, eax
ret
endp
proc nvme_cleanup
mov eax, [p_ident]
test eax, eax
jz @f
invoke Kfree, eax
mov eax, [p_ident]
test eax, eax
jz @f
invoke KernelFree, eax
@@:
ret
ret
endp
; uninitialized data
align 4
pcidev_bus dd ?
pcidev_devfn dd ?
p_mmap dd ?
p_ident dd ?
p_mmap dd ?
p_ident dd ?
;all initialized data place here
align 4
my_service db "NVMe Service",0 ;max 16 chars include zero
include_debug_strings
my_service db "NVME",0 ;max 16 chars include zero
include_debug_strings
align 4
data fixups