From 534103061c5e30aa4c2e087da56e52d70cb92a01 Mon Sep 17 00:00:00 2001 From: Abdur-Rahman Mansoor Date: Wed, 31 Jul 2024 15:10:13 -0400 Subject: [PATCH] important bug fix and some micro optimizations As for the fix, the submission queues weren't properly being zeroed out due to a small typo. The other stuff has some commented out stuff that I'm currently debugging and working on. --- drivers/nvme/nvme.asm | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/drivers/nvme/nvme.asm b/drivers/nvme/nvme.asm index 778f730..8c77c1d 100644 --- a/drivers/nvme/nvme.asm +++ b/drivers/nvme/nvme.asm @@ -972,7 +972,7 @@ proc nvme_init stdcall, pci:dword jz .exit_fail mov dword [edi + ecx + NVM_QUEUE_ENTRY.sq_ptr], eax push ecx - stdcall memsetdz, eax, sizeof.CQ_ENTRY * CQ_ENTRIES / 4 + stdcall memsetdz, eax, sizeof.SQ_ENTRY * SQ_ENTRIES / 4 invoke CreateRingBuffer, 0x1000, PG_SW pop ecx test eax, eax @@ -989,23 +989,19 @@ proc nvme_init stdcall, pci:dword mov dword [edi + ecx + NVM_QUEUE_ENTRY.cmd_ptr], eax mov esi, eax push ecx - xor ecx, ecx + xor ebx, ebx .init_cmd_entries: - push ecx ; BUG: Won't pop ecx if allocation fails invoke KernelAlloc, sizeof.MUTEX test eax, eax jz .exit_fail - pop ecx - mov dword [esi + ecx * NVMQCMD.mutex_ptr], eax - mov dword [esi + ecx * NVMQCMD.cid], ecx - push ecx + mov dword [esi + ebx * NVMQCMD.mutex_ptr], eax + mov dword [esi + ebx * NVMQCMD.cid], ebx mov ecx, eax invoke MutexInit - pop ecx - inc ecx - cmp ecx, CQ_ENTRIES + inc ebx + cmp ebx, CQ_ENTRIES jne .init_cmd_entries pop ecx @@ -1108,7 +1104,7 @@ proc nvme_init stdcall, pci:dword 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 + ; (TODO: create N queue pairs for N CPU cores, see page 8 of NVMe 1.4 spec for an explaination) mov esi, [pci] mov edi, esi mov esi, dword [esi + pcidev.queue_entries] @@ -1215,13 +1211,11 @@ endp proc get_new_cid stdcall, pci:dword, y:dword - push esi - mov esi, [pci] - mov esi, [esi + pcidev.queue_entries] + mov eax, [pci] + mov eax, dword [eax + pcidev.queue_entries] mov ecx, [y] imul ecx, sizeof.NVM_QUEUE_ENTRY - movzx eax, word [esi + ecx + NVM_QUEUE_ENTRY.tail] - pop esi + movzx eax, word [eax + ecx + NVM_QUEUE_ENTRY.head] ret endp @@ -1316,7 +1310,7 @@ proc cqyhdbl_write stdcall, pci:dword, y:dword, cqh:dword lea edi, dword [edi + ecx] mov esi, dword [esi + pcidev.io_addr] mov eax, [cqh] - ;DEBUGF DBG_INFO, "Writing to completion queue doorbell register: %u\n", ax + ;DEBUGF DBG_INFO, "Writing to completion queue doorbell register 0x%x: %u\n", edx, ax mov word [esi + edx], ax ; Write to CQyHDBL mov word [edi + NVM_QUEUE_ENTRY.head], ax pop edi esi @@ -1332,13 +1326,18 @@ proc sqytdbl_write stdcall, pci:dword, y:word, cmd:dword mov edi, dword [edi + pcidev.queue_entries] movzx ecx, [y] imul ecx, sizeof.NVM_QUEUE_ENTRY + ;mov eax, dword [edi + ecx + NVM_QUEUE_ENTRY.cmd_ptr] 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] + ;mov ecx, [eax + ecx + NVMQCMD.mutex_ptr] + ;push ecx stdcall memcpy, edi, esi, sizeof.SQ_ENTRY + ;pop ecx + ;invoke MutexLock mov edi, [pci] mov esi, dword [edi + pcidev.io_addr] @@ -1419,9 +1418,9 @@ proc consume_cq_entries stdcall, pci:dword, queue:dword mov edi, dword [esi + NVM_QUEUE_ENTRY.cq_ptr] movzx eax, word [esi + NVM_QUEUE_ENTRY.tail] movzx ecx, word [esi + NVM_QUEUE_ENTRY.head] - stdcall is_queue_full, eax, ecx - test eax, eax - jnz .end + ;stdcall is_queue_full, eax, ecx + ;test eax, eax + ;jnz .end movzx ecx, word [esi + NVM_QUEUE_ENTRY.head] cmp ecx, (NVM_ACQS - 1) jb .loop