The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
nvme.c File Reference
#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.

Macro Definition Documentation

◆ REG_CAP

#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).

◆ REG_INTMS

#define REG_INTMS   0x0C

◆ REG_INTMC

#define REG_INTMC   0x10

◆ REG_CC

#define REG_CC   0x14

◆ REG_CSTS

#define REG_CSTS   0x1C

◆ REG_AQA

#define REG_AQA   0x24

◆ REG_ASQ

#define REG_ASQ   0x28

◆ REG_ACQ

#define REG_ACQ   0x30

◆ REG_DOORBELLS

#define REG_DOORBELLS   0x1000

◆ CC_ENABLE

#define CC_ENABLE   0x1u

◆ CC_IOSQES

#define CC_IOSQES   (6u << 16)

◆ CC_IOCQES

#define CC_IOCQES   (4u << 20)

◆ CSTS_READY

#define CSTS_READY   0x1u

◆ CSTS_FATAL

#define CSTS_FATAL   0x2u

◆ ADMIN_CREATE_SQ

#define ADMIN_CREATE_SQ   0x01

◆ ADMIN_CREATE_CQ

#define ADMIN_CREATE_CQ   0x05

◆ ADMIN_IDENTIFY

#define ADMIN_IDENTIFY   0x06

◆ IO_FLUSH

#define IO_FLUSH   0x00

◆ IO_WRITE

#define IO_WRITE   0x01

◆ IO_READ

#define IO_READ   0x02

◆ QUEUE_ENTRIES

#define QUEUE_ENTRIES   16

◆ SECTOR_SIZE

#define SECTOR_SIZE   512u

◆ BOUNCE_SECTORS

#define BOUNCE_SECTORS   (PAGE_SIZE / SECTOR_SIZE)

◆ COMMAND_TIMEOUT_MS

#define COMMAND_TIMEOUT_MS   5000u

◆ CQ_CONTIGUOUS

#define CQ_CONTIGUOUS   0x1u

◆ CQ_IRQ_ON

#define CQ_IRQ_ON   0x2u

Function Documentation

◆ nvme_init()

nvme_disk_t * nvme_init ( void )

find and set up the first NVMe controller.

call after pci_init and mmio_init.

Returns
the disk, or NULL if there is none or it cannot be used.