|
ginnOS
The reference: libc, the libraries and the kernel
|
#include "nvme.h"#include "common/memory.h"#include "drivers/pci/pci.h"#include "drivers/pci/pci_irq.h"#include "kernel/klog/klog.h"#include "kernel/memory/mmio.h"#include "kernel/memory/page.h"#include "kernel/memory/pmm.h"Macros | |
| #define | REG_CAP 0x00 |
| the NVMe driver (NVM Express base specification 1.4). | |
| #define | REG_INTMS 0x0C |
| #define | REG_INTMC 0x10 |
| #define | REG_CC 0x14 |
| #define | REG_CSTS 0x1C |
| #define | REG_AQA 0x24 |
| #define | REG_ASQ 0x28 |
| #define | REG_ACQ 0x30 |
| #define | REG_DOORBELLS 0x1000 |
| #define | CC_ENABLE 0x1u |
| #define | CC_IOSQES (6u << 16) |
| #define | CC_IOCQES (4u << 20) |
| #define | CSTS_READY 0x1u |
| #define | CSTS_FATAL 0x2u |
| #define | ADMIN_CREATE_SQ 0x01 |
| #define | ADMIN_CREATE_CQ 0x05 |
| #define | ADMIN_IDENTIFY 0x06 |
| #define | IO_FLUSH 0x00 |
| #define | IO_WRITE 0x01 |
| #define | IO_READ 0x02 |
| #define | QUEUE_ENTRIES 16 |
| #define | SECTOR_SIZE 512u |
| #define | BOUNCE_SECTORS (PAGE_SIZE / SECTOR_SIZE) |
| #define | COMMAND_TIMEOUT_MS 5000u |
| #define | CQ_CONTIGUOUS 0x1u |
| #define | CQ_IRQ_ON 0x2u |
Functions | |
| nvme_disk_t * | nvme_init (void) |
| find and set up the first NVMe controller. | |
| #define REG_CAP 0x00 |
the NVMe driver (NVM Express base specification 1.4).
the host puts 64-byte commands into a submission queue and writes the new tail to the queue's doorbell. the controller puts a 16-byte entry into the completion queue for each command; the phase bit of an entry changes each time the queue wraps, so the host sees which entries are new.
queue 0 is the admin queue (identify, create the I/O queues). queue 1 is the one I/O queue. each queue is one page. the data goes through a bounce page (one PRP entry, 8 sectors), because a caller's buffer can be on a kernel stack, which is not physically contiguous.
both queues use interrupt vector 0 (MSI-X entry 0, MSI, or INTx). the waiting process sleeps until it; without an interrupt it polls (see kernel/sync/completion.h).
locking: one command at a time (a mutex).
| #define REG_INTMS 0x0C |
| #define REG_INTMC 0x10 |
| #define REG_CC 0x14 |
| #define REG_CSTS 0x1C |
| #define REG_AQA 0x24 |
| #define REG_ASQ 0x28 |
| #define REG_ACQ 0x30 |
| #define REG_DOORBELLS 0x1000 |
| #define CC_ENABLE 0x1u |
| #define CC_IOSQES (6u << 16) |
| #define CC_IOCQES (4u << 20) |
| #define CSTS_READY 0x1u |
| #define CSTS_FATAL 0x2u |
| #define ADMIN_CREATE_SQ 0x01 |
| #define ADMIN_CREATE_CQ 0x05 |
| #define ADMIN_IDENTIFY 0x06 |
| #define IO_FLUSH 0x00 |
| #define IO_WRITE 0x01 |
| #define IO_READ 0x02 |
| #define QUEUE_ENTRIES 16 |
| #define SECTOR_SIZE 512u |
| #define BOUNCE_SECTORS (PAGE_SIZE / SECTOR_SIZE) |
| #define COMMAND_TIMEOUT_MS 5000u |
| #define CQ_CONTIGUOUS 0x1u |
| #define CQ_IRQ_ON 0x2u |
| nvme_disk_t * nvme_init | ( | void | ) |
find and set up the first NVMe controller.
call after pci_init and mmio_init.