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
|
|
|
|
|
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-03-28 23:43:18 +01:00
|
|
|
|
|
|
|
proc START c, reason:dword
|
2024-04-17 21:35:01 +02:00
|
|
|
|
2024-03-28 23:43:18 +01:00
|
|
|
cmp [reason], DRV_ENTRY
|
2024-04-17 21:35:01 +02:00
|
|
|
jne .err
|
|
|
|
|
2024-03-28 23:43:18 +01:00
|
|
|
.entry:
|
2024-04-17 21:35:01 +02:00
|
|
|
DEBUGF DBG_INFO, "Detecting NVMe hardware...\n"
|
|
|
|
call detect_nvme
|
2024-04-28 02:11:57 +02:00
|
|
|
test eax, eax
|
|
|
|
jz .err
|
2024-04-20 05:39:34 +02:00
|
|
|
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
|
|
|
|
mov ebx, eax
|
2024-04-17 21:35:01 +02:00
|
|
|
|
2024-04-28 19:50:40 +02:00
|
|
|
.loop:
|
|
|
|
stdcall device_is_compat, dword [ebx + ecx * sizeof.pcidev + pcidev.bus], dword [ebx + ecx * sizeof.pcidev + pcidev.devfn]
|
|
|
|
test eax, eax
|
|
|
|
jz @f
|
|
|
|
stdcall nvme_init, dword [ebx + ecx * sizeof.pcidev + pcidev.bus], dword [ebx + ecx * sizeof.pcidev + pcidev.devfn]
|
2024-03-28 23:43:18 +01:00
|
|
|
|
2024-04-28 19:50:40 +02:00
|
|
|
@@:
|
|
|
|
inc ecx
|
|
|
|
cmp ecx, dword [pcidevs_len]
|
|
|
|
jne .loop
|
2024-03-28 23:43:18 +01:00
|
|
|
invoke RegService, my_service, service_proc
|
|
|
|
ret
|
2024-04-17 21:35:01 +02:00
|
|
|
|
|
|
|
.err:
|
|
|
|
call nvme_cleanup
|
2024-03-28 23:43:18 +01:00
|
|
|
xor eax, eax
|
|
|
|
ret
|
2024-04-17 21:35:01 +02:00
|
|
|
|
2024-03-28 23:43:18 +01:00
|
|
|
endp
|
|
|
|
|
|
|
|
proc service_proc stdcall, ioctl:dword
|
|
|
|
|
|
|
|
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
|
2024-04-17 21:35:01 +02:00
|
|
|
jne @F
|
2024-03-28 23:43:18 +01:00
|
|
|
mov dword [eax], API_VERSION
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
@@:
|
|
|
|
or eax, -1
|
|
|
|
ret
|
2024-04-17 21:35:01 +02:00
|
|
|
|
2024-03-28 23:43:18 +01:00
|
|
|
endp
|
|
|
|
|
2024-03-31 21:43:38 +02:00
|
|
|
proc memset stdcall, p_data:dword, val:byte, sz:dword
|
2024-04-17 21:35:01 +02:00
|
|
|
|
2024-04-28 02:11:57 +02:00
|
|
|
mov edx, [sz]
|
|
|
|
mov bh, [val]
|
2024-04-17 21:35:01 +02:00
|
|
|
xor ecx, ecx
|
2024-03-31 21:43:38 +02:00
|
|
|
@@:
|
2024-04-17 21:35:01 +02:00
|
|
|
mov byte [p_data + ecx], bh
|
|
|
|
inc ecx
|
2024-04-28 02:11:57 +02:00
|
|
|
cmp ecx, edx
|
2024-04-17 21:35:01 +02:00
|
|
|
jne @b
|
|
|
|
ret
|
|
|
|
|
2024-03-31 21:43:38 +02:00
|
|
|
endp
|
|
|
|
|
|
|
|
; Submit a Command in the Admin Submission Queue
|
|
|
|
proc submit_asq stdcall, p_sq:dword
|
2024-04-17 21:35:01 +02:00
|
|
|
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
2024-03-31 21:43:38 +02:00
|
|
|
endp
|
|
|
|
|
|
|
|
proc nvme_identify stdcall, nsid:dword, dptr:dword, cns:byte
|
2024-04-17 21:35:01 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2024-03-30 04:42:01 +01:00
|
|
|
endp
|
|
|
|
|
2024-04-17 21:35:01 +02:00
|
|
|
proc detect_nvme
|
|
|
|
|
2024-03-28 23:43:18 +01:00
|
|
|
invoke GetPCIList
|
|
|
|
mov edx, eax
|
2024-04-17 21:35:01 +02:00
|
|
|
|
2024-03-28 23:43:18 +01:00
|
|
|
.check_dev:
|
2024-04-17 21:35:01 +02:00
|
|
|
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:
|
2024-04-17 21:35:01 +02:00
|
|
|
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
|
2024-04-17 21:35:01 +02:00
|
|
|
|
|
|
|
.found_dev:
|
2024-04-28 02:11:57 +02:00
|
|
|
push edx eax
|
|
|
|
PDEBUGF DBG_INFO, "PCI(%u.%u.%u): Detected NVMe device...\n", byte [eax + PCIDEV.bus], [eax + PCIDEV.devfn]
|
|
|
|
cmp dword [pcidevs_len], TOTAL_PCIDEVS
|
2024-04-20 05:39:34 +02:00
|
|
|
jne @f
|
2024-04-28 02:11:57 +02:00
|
|
|
pop eax edx
|
|
|
|
jmp .exit_success
|
2024-04-20 05:39:34 +02:00
|
|
|
|
|
|
|
@@:
|
2024-04-28 02:11:57 +02:00
|
|
|
inc dword [pcidevs_len]
|
2024-04-20 05:39:34 +02:00
|
|
|
mov ebx, dword [p_nvme_devices]
|
|
|
|
test ebx, ebx
|
|
|
|
jnz @f
|
2024-04-28 02:11:57 +02:00
|
|
|
invoke KernelAlloc, TOTAL_PCIDEVS_MALLOC_SZ
|
2024-04-20 05:39:34 +02:00
|
|
|
test eax, eax
|
2024-04-28 02:11:57 +02:00
|
|
|
jz .err_no_mem
|
|
|
|
mov dword [p_nvme_devices], eax
|
2024-04-20 05:39:34 +02:00
|
|
|
|
|
|
|
@@:
|
2024-04-28 02:11:57 +02:00
|
|
|
mov ebx, dword [p_nvme_devices]
|
2024-04-20 05:39:34 +02:00
|
|
|
mov ecx, dword [pcidevs_len]
|
2024-04-28 02:11:57 +02:00
|
|
|
dec ecx
|
2024-04-20 05:39:34 +02:00
|
|
|
pop eax
|
2024-04-28 02:11:57 +02:00
|
|
|
movzx edx, byte [eax + PCIDEV.bus]
|
|
|
|
mov byte [ebx + ecx * sizeof.pcidev + pcidev.bus], dl
|
|
|
|
movzx edx, byte [eax + PCIDEV.devfn]
|
|
|
|
mov byte [ebx + ecx * sizeof.pcidev + pcidev.devfn], dl
|
|
|
|
pop edx
|
|
|
|
jmp .next_dev
|
|
|
|
|
|
|
|
.err_no_mem:
|
|
|
|
pop eax edx
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
2024-04-20 05:39:34 +02:00
|
|
|
|
2024-04-28 02:11:57 +02:00
|
|
|
.exit_success:
|
|
|
|
xor eax, eax
|
|
|
|
inc eax
|
2024-04-17 21:35:01 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
endp
|
|
|
|
|
2024-04-28 19:50:40 +02:00
|
|
|
proc device_is_compat, bus:byte, devfn:byte
|
2024-04-17 21:35:01 +02:00
|
|
|
|
2024-04-28 19:50:40 +02:00
|
|
|
invoke PciRead32, dword [bus], dword [devfn], PCI_header00.base_addr_0
|
2024-04-17 21:35:01 +02:00
|
|
|
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
|
2024-04-28 19:50:40 +02:00
|
|
|
mov dword [p_mmap], eax
|
2024-04-28 02:11:57 +02:00
|
|
|
xor eax, eax
|
|
|
|
inc eax
|
|
|
|
ret
|
2024-04-20 05:39:34 +02:00
|
|
|
|
|
|
|
.exit_fail:
|
2024-04-28 19:50:40 +02:00
|
|
|
PDEBUGF DBG_INFO, "PCI(%u.%u.%u): incompatible NVMe device\n", [bus], [devfn]
|
2024-04-21 23:10:09 +02:00
|
|
|
xor eax, eax
|
|
|
|
ret
|
2024-04-20 05:39:34 +02:00
|
|
|
|
|
|
|
endp
|
|
|
|
|
|
|
|
; nvme_init: Initializes the NVMe controller
|
2024-04-28 19:50:40 +02:00
|
|
|
proc nvme_init, bus:byte, devfn:byte
|
2024-04-20 05:39:34 +02:00
|
|
|
|
|
|
|
test eax, eax
|
|
|
|
jz .exit_fail
|
|
|
|
mov eax, dword [p_mmap]
|
2024-04-17 21:35:01 +02:00
|
|
|
mov ebx, dword [eax + NVME_REG_MAP.CAP]
|
2024-04-28 02:11:57 +02:00
|
|
|
PDEBUGF DBG_INFO, "PCI(%u.%u.%u): NVMe maximum queue entries supported: %u\n", byte [bus], byte [devfn], bx
|
2024-04-17 21:35:01 +02:00
|
|
|
test ebx, CAP_CQR
|
|
|
|
jz .cqr_not_req
|
2024-04-28 02:11:57 +02:00
|
|
|
PDEBUGF DBG_INFO, "PCI(%u.%u.%u): NVMe contiguous queues required\n", byte [bus], byte [devfn]
|
2024-03-28 23:43:18 +01:00
|
|
|
|
2024-04-02 01:47:14 +02:00
|
|
|
.cqr_not_req:
|
2024-04-17 21:35:01 +02:00
|
|
|
mov ebx, dword [eax + NVME_REG_MAP.CAP + 4]
|
|
|
|
mov ecx, ebx
|
|
|
|
test ebx, CAP_CSS_NVM_CMDSET
|
|
|
|
jz .exit_fail
|
|
|
|
|
2024-04-21 02:29:06 +02:00
|
|
|
if __DEBUG__ eq 1
|
2024-04-17 21:35:01 +02:00
|
|
|
and ebx, CAP_MPSMIN
|
|
|
|
and ecx, CAP_MPSMAX
|
|
|
|
shr ebx, 16
|
|
|
|
shr ecx, 16
|
2024-04-28 02:11:57 +02:00
|
|
|
PDEBUGF DBG_INFO, "PCI(%u.%u.%u): NVMe memory page size minimum: %u\n", byte [bus], byte [devfn], ebx
|
|
|
|
PDEBUGF DBG_INFO, "PCI(%u.%u.%u): NVMe memory page size maximum: %u\n", byte [bus], byte [devfn], ecx
|
2024-04-17 21:35:01 +02:00
|
|
|
|
|
|
|
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
|
2024-04-28 02:11:57 +02:00
|
|
|
PDEBUGF DBG_INFO, "PCI(%u.%u.%u): NVMe I/O submission queue entry size: %u\n", byte [bus], byte [devfn], ebx
|
|
|
|
PDEBUGF DBG_INFO, "PCI(%u.%u.%u): NVMe I/O completion queue entry size: %u\n", byte [bus], byte [devfn], ecx
|
2024-04-17 21:35:01 +02:00
|
|
|
end if
|
|
|
|
|
2024-03-28 23:43:18 +01:00
|
|
|
xor eax, eax
|
|
|
|
inc eax
|
|
|
|
ret
|
2024-03-31 21:43:38 +02:00
|
|
|
|
2024-04-02 01:47:14 +02:00
|
|
|
.exit_fail:
|
2024-04-28 02:11:57 +02:00
|
|
|
PDEBUGF DBG_INFO, "PCI(%u.%u.%u): failed to initialize NVMe controller\n", byte [bus], byte [devfn]
|
2024-04-20 05:39:34 +02:00
|
|
|
xor eax, eax
|
2024-03-31 21:43:38 +02:00
|
|
|
ret
|
2024-04-17 21:35:01 +02:00
|
|
|
|
2024-03-31 21:43:38 +02:00
|
|
|
endp
|
|
|
|
|
|
|
|
proc nvme_cleanup
|
2024-04-17 21:35:01 +02:00
|
|
|
|
2024-04-21 23:10:09 +02:00
|
|
|
DEBUGF DBG_INFO, "(NVMe): Cleaning up...\n"
|
2024-04-28 02:11:57 +02:00
|
|
|
invoke KernelFree, [p_nvme_devices]
|
|
|
|
invoke KernelFree, [p_ident]
|
2024-04-17 21:35:01 +02:00
|
|
|
ret
|
|
|
|
|
2024-03-28 23:43:18 +01:00
|
|
|
endp
|
|
|
|
|
|
|
|
; uninitialized data
|
2024-03-31 21:43:38 +02:00
|
|
|
align 4
|
2024-04-17 21:35:01 +02:00
|
|
|
p_mmap dd ?
|
|
|
|
p_ident dd ?
|
2024-03-28 23:43:18 +01:00
|
|
|
|
|
|
|
;all initialized data place here
|
|
|
|
align 4
|
2024-04-20 05:39:34 +02:00
|
|
|
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
|
2024-04-17 21:35:01 +02:00
|
|
|
include_debug_strings
|
2024-03-28 23:43:18 +01:00
|
|
|
|
|
|
|
align 4
|
|
|
|
data fixups
|
|
|
|
end data
|