2019-06-13 10:49:02 +00:00
|
|
|
//
|
|
|
|
// driver for qemu's virtio disk device.
|
|
|
|
// uses qemu's mmio interface to virtio.
|
|
|
|
// qemu presents a "legacy" virtio interface.
|
|
|
|
//
|
|
|
|
// qemu ... -drive file=fs.img,if=none,format=raw,id=x0 -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
#include "riscv.h"
|
|
|
|
#include "defs.h"
|
|
|
|
#include "param.h"
|
|
|
|
#include "memlayout.h"
|
|
|
|
#include "spinlock.h"
|
|
|
|
#include "sleeplock.h"
|
|
|
|
#include "fs.h"
|
|
|
|
#include "buf.h"
|
|
|
|
#include "virtio.h"
|
|
|
|
|
2019-06-13 13:40:17 +00:00
|
|
|
// the address of virtio mmio register r.
|
|
|
|
#define R(r) ((volatile uint32 *)(VIRTIO0 + (r)))
|
2019-06-13 10:49:02 +00:00
|
|
|
|
2019-08-12 11:20:32 +00:00
|
|
|
static struct disk {
|
|
|
|
// memory for virtio descriptors &c for queue 0.
|
|
|
|
// this is a global instead of allocated because it must
|
|
|
|
// be multiple contiguous pages, which kalloc()
|
|
|
|
// doesn't support, and page aligned.
|
|
|
|
char pages[2*PGSIZE];
|
|
|
|
struct VRingDesc *desc;
|
|
|
|
uint16 *avail;
|
|
|
|
struct UsedArea *used;
|
|
|
|
|
|
|
|
// our own book-keeping.
|
|
|
|
char free[NUM]; // is a descriptor free?
|
|
|
|
uint16 used_idx; // we've looked this far in used[2..NUM].
|
|
|
|
|
|
|
|
// track info about in-flight operations,
|
|
|
|
// for use when completion interrupt arrives.
|
|
|
|
// indexed by first descriptor index of chain.
|
|
|
|
struct {
|
|
|
|
struct buf *b;
|
|
|
|
char status;
|
|
|
|
} info[NUM];
|
|
|
|
|
|
|
|
struct spinlock vdisk_lock;
|
|
|
|
|
|
|
|
} __attribute__ ((aligned (PGSIZE))) disk;
|
2019-06-13 10:49:02 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
virtio_disk_init(void)
|
|
|
|
{
|
|
|
|
uint32 status = 0;
|
|
|
|
|
2019-08-12 11:20:32 +00:00
|
|
|
initlock(&disk.vdisk_lock, "virtio_disk");
|
2019-06-13 10:49:02 +00:00
|
|
|
|
|
|
|
if(*R(VIRTIO_MMIO_MAGIC_VALUE) != 0x74726976 ||
|
|
|
|
*R(VIRTIO_MMIO_VERSION) != 1 ||
|
|
|
|
*R(VIRTIO_MMIO_DEVICE_ID) != 2 ||
|
|
|
|
*R(VIRTIO_MMIO_VENDOR_ID) != 0x554d4551){
|
|
|
|
panic("could not find virtio disk");
|
|
|
|
}
|
|
|
|
|
|
|
|
status |= VIRTIO_CONFIG_S_ACKNOWLEDGE;
|
|
|
|
*R(VIRTIO_MMIO_STATUS) = status;
|
|
|
|
|
|
|
|
status |= VIRTIO_CONFIG_S_DRIVER;
|
|
|
|
*R(VIRTIO_MMIO_STATUS) = status;
|
|
|
|
|
|
|
|
// negotiate features
|
|
|
|
uint64 features = *R(VIRTIO_MMIO_DEVICE_FEATURES);
|
|
|
|
features &= ~(1 << VIRTIO_BLK_F_RO);
|
|
|
|
features &= ~(1 << VIRTIO_BLK_F_SCSI);
|
|
|
|
features &= ~(1 << VIRTIO_BLK_F_CONFIG_WCE);
|
|
|
|
features &= ~(1 << VIRTIO_BLK_F_MQ);
|
|
|
|
features &= ~(1 << VIRTIO_F_ANY_LAYOUT);
|
|
|
|
features &= ~(1 << VIRTIO_RING_F_EVENT_IDX);
|
|
|
|
features &= ~(1 << VIRTIO_RING_F_INDIRECT_DESC);
|
|
|
|
*R(VIRTIO_MMIO_DRIVER_FEATURES) = features;
|
|
|
|
|
|
|
|
// tell device that feature negotiation is complete.
|
|
|
|
status |= VIRTIO_CONFIG_S_FEATURES_OK;
|
|
|
|
*R(VIRTIO_MMIO_STATUS) = status;
|
|
|
|
|
|
|
|
// tell device we're completely ready.
|
|
|
|
status |= VIRTIO_CONFIG_S_DRIVER_OK;
|
|
|
|
*R(VIRTIO_MMIO_STATUS) = status;
|
|
|
|
|
|
|
|
*R(VIRTIO_MMIO_GUEST_PAGE_SIZE) = PGSIZE;
|
|
|
|
|
2019-06-13 13:40:17 +00:00
|
|
|
// initialize queue 0.
|
2019-06-13 10:49:02 +00:00
|
|
|
*R(VIRTIO_MMIO_QUEUE_SEL) = 0;
|
|
|
|
uint32 max = *R(VIRTIO_MMIO_QUEUE_NUM_MAX);
|
|
|
|
if(max == 0)
|
|
|
|
panic("virtio disk has no queue 0");
|
|
|
|
if(max < NUM)
|
|
|
|
panic("virtio disk max queue too short");
|
|
|
|
*R(VIRTIO_MMIO_QUEUE_NUM) = NUM;
|
2019-08-12 11:20:32 +00:00
|
|
|
memset(disk.pages, 0, sizeof(disk.pages));
|
|
|
|
*R(VIRTIO_MMIO_QUEUE_PFN) = ((uint64)disk.pages) >> PGSHIFT;
|
2019-06-13 10:49:02 +00:00
|
|
|
|
|
|
|
// desc = pages -- num * VRingDesc
|
|
|
|
// avail = pages + 0x40 -- 2 * uint16, then num * uint16
|
|
|
|
// used = pages + 4096 -- 2 * uint16, then num * vRingUsedElem
|
|
|
|
|
2019-08-12 11:20:32 +00:00
|
|
|
disk.desc = (struct VRingDesc *) disk.pages;
|
|
|
|
disk.avail = (uint16*)(((char*)disk.desc) + NUM*sizeof(struct VRingDesc));
|
|
|
|
disk.used = (struct UsedArea *) (disk.pages + PGSIZE);
|
2019-06-13 10:49:02 +00:00
|
|
|
|
|
|
|
for(int i = 0; i < NUM; i++)
|
2019-08-12 11:20:32 +00:00
|
|
|
disk.free[i] = 1;
|
2019-06-13 13:40:17 +00:00
|
|
|
|
|
|
|
// plic.c and trap.c arrange for interrupts from VIRTIO0_IRQ.
|
2019-06-13 10:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// find a free descriptor, mark it non-free, return its index.
|
|
|
|
static int
|
|
|
|
alloc_desc()
|
|
|
|
{
|
|
|
|
for(int i = 0; i < NUM; i++){
|
2019-08-12 11:20:32 +00:00
|
|
|
if(disk.free[i]){
|
|
|
|
disk.free[i] = 0;
|
2019-06-13 10:49:02 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-06-13 13:40:17 +00:00
|
|
|
// mark a descriptor as free.
|
|
|
|
static void
|
2019-06-13 10:49:02 +00:00
|
|
|
free_desc(int i)
|
|
|
|
{
|
|
|
|
if(i >= NUM)
|
|
|
|
panic("virtio_disk_intr 1");
|
2019-08-12 11:20:32 +00:00
|
|
|
if(disk.free[i])
|
2019-06-13 10:49:02 +00:00
|
|
|
panic("virtio_disk_intr 2");
|
2019-08-12 11:20:32 +00:00
|
|
|
disk.desc[i].addr = 0;
|
|
|
|
disk.free[i] = 1;
|
|
|
|
wakeup(&disk.free[0]);
|
2019-06-13 13:40:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// free a chain of descriptors.
|
|
|
|
static void
|
|
|
|
free_chain(int i)
|
|
|
|
{
|
|
|
|
while(1){
|
|
|
|
free_desc(i);
|
2019-08-12 11:20:32 +00:00
|
|
|
if(disk.desc[i].flags & VRING_DESC_F_NEXT)
|
|
|
|
i = disk.desc[i].next;
|
2019-06-13 13:40:17 +00:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2019-06-13 10:49:02 +00:00
|
|
|
}
|
|
|
|
|
2019-07-01 12:20:35 +00:00
|
|
|
static int
|
|
|
|
alloc3_desc(int *idx)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < 3; i++){
|
|
|
|
idx[i] = alloc_desc();
|
|
|
|
if(idx[i] < 0){
|
|
|
|
for(int j = 0; j < i; j++)
|
|
|
|
free_desc(idx[j]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-13 10:49:02 +00:00
|
|
|
void
|
2019-07-29 21:33:16 +00:00
|
|
|
virtio_disk_rw(struct buf *b, int write)
|
2019-06-13 10:49:02 +00:00
|
|
|
{
|
|
|
|
uint64 sector = b->blockno * (BSIZE / 512);
|
|
|
|
|
2019-08-12 11:20:32 +00:00
|
|
|
acquire(&disk.vdisk_lock);
|
2019-06-13 10:49:02 +00:00
|
|
|
|
2019-06-13 13:40:17 +00:00
|
|
|
// the spec says that legacy block operations use three
|
2019-06-13 10:49:02 +00:00
|
|
|
// descriptors: one for type/reserved/sector, one for
|
|
|
|
// the data, one for a 1-byte status result.
|
|
|
|
|
|
|
|
// allocate the three descriptors.
|
|
|
|
int idx[3];
|
|
|
|
while(1){
|
2019-07-01 12:20:35 +00:00
|
|
|
if(alloc3_desc(idx) == 0) {
|
2019-06-13 10:49:02 +00:00
|
|
|
break;
|
2019-07-01 12:20:35 +00:00
|
|
|
}
|
2019-08-12 11:20:32 +00:00
|
|
|
sleep(&disk.free[0], &disk.vdisk_lock);
|
2019-06-13 10:49:02 +00:00
|
|
|
}
|
2019-07-01 12:20:35 +00:00
|
|
|
|
2019-06-13 10:49:02 +00:00
|
|
|
// format the three descriptors.
|
|
|
|
// qemu's virtio-blk.c reads them.
|
|
|
|
|
|
|
|
struct virtio_blk_outhdr {
|
|
|
|
uint32 type;
|
|
|
|
uint32 reserved;
|
|
|
|
uint64 sector;
|
|
|
|
} buf0;
|
|
|
|
|
2019-07-29 21:33:16 +00:00
|
|
|
if(write)
|
2019-06-13 10:49:02 +00:00
|
|
|
buf0.type = VIRTIO_BLK_T_OUT; // write the disk
|
|
|
|
else
|
|
|
|
buf0.type = VIRTIO_BLK_T_IN; // read the disk
|
|
|
|
buf0.reserved = 0;
|
|
|
|
buf0.sector = sector;
|
|
|
|
|
2019-07-23 16:17:17 +00:00
|
|
|
// buf0 is on a kernel stack, which is not direct mapped,
|
2019-07-24 19:28:37 +00:00
|
|
|
// thus the call to kvmpa().
|
2019-08-12 11:20:32 +00:00
|
|
|
disk.desc[idx[0]].addr = (uint64) kvmpa((uint64) &buf0);
|
|
|
|
disk.desc[idx[0]].len = sizeof(buf0);
|
|
|
|
disk.desc[idx[0]].flags = VRING_DESC_F_NEXT;
|
|
|
|
disk.desc[idx[0]].next = idx[1];
|
2019-06-13 10:49:02 +00:00
|
|
|
|
2019-08-12 11:20:32 +00:00
|
|
|
disk.desc[idx[1]].addr = (uint64) b->data;
|
|
|
|
disk.desc[idx[1]].len = BSIZE;
|
2019-07-29 21:33:16 +00:00
|
|
|
if(write)
|
2019-08-12 11:20:32 +00:00
|
|
|
disk.desc[idx[1]].flags = 0; // device reads b->data
|
2019-06-13 10:49:02 +00:00
|
|
|
else
|
2019-08-12 11:20:32 +00:00
|
|
|
disk.desc[idx[1]].flags = VRING_DESC_F_WRITE; // device writes b->data
|
|
|
|
disk.desc[idx[1]].flags |= VRING_DESC_F_NEXT;
|
|
|
|
disk.desc[idx[1]].next = idx[2];
|
2019-06-13 10:49:02 +00:00
|
|
|
|
2019-08-12 11:20:32 +00:00
|
|
|
disk.info[idx[0]].status = 0;
|
|
|
|
disk.desc[idx[2]].addr = (uint64) &disk.info[idx[0]].status;
|
|
|
|
disk.desc[idx[2]].len = 1;
|
|
|
|
disk.desc[idx[2]].flags = VRING_DESC_F_WRITE; // device writes the status
|
|
|
|
disk.desc[idx[2]].next = 0;
|
2019-06-13 10:49:02 +00:00
|
|
|
|
|
|
|
// record struct buf for virtio_disk_intr().
|
2019-07-30 16:53:19 +00:00
|
|
|
b->disk = 1;
|
2019-08-12 11:20:32 +00:00
|
|
|
disk.info[idx[0]].b = b;
|
2019-06-13 10:49:02 +00:00
|
|
|
|
|
|
|
// avail[0] is flags
|
|
|
|
// avail[1] tells the device how far to look in avail[2...].
|
|
|
|
// avail[2...] are desc[] indices the device should process.
|
|
|
|
// we only tell device the first index in our chain of descriptors.
|
2019-08-12 11:20:32 +00:00
|
|
|
disk.avail[2 + (disk.avail[1] % NUM)] = idx[0];
|
2019-06-13 10:49:02 +00:00
|
|
|
__sync_synchronize();
|
2019-08-12 11:20:32 +00:00
|
|
|
disk.avail[1] = disk.avail[1] + 1;
|
2019-06-13 10:49:02 +00:00
|
|
|
|
|
|
|
*R(VIRTIO_MMIO_QUEUE_NOTIFY) = 0; // value is queue number
|
|
|
|
|
|
|
|
// Wait for virtio_disk_intr() to say request has finished.
|
2019-07-30 16:53:19 +00:00
|
|
|
while(b->disk == 1) {
|
2019-08-12 11:20:32 +00:00
|
|
|
sleep(b, &disk.vdisk_lock);
|
2019-06-13 10:49:02 +00:00
|
|
|
}
|
|
|
|
|
2019-08-12 11:20:32 +00:00
|
|
|
disk.info[idx[0]].b = 0;
|
2019-07-30 12:13:03 +00:00
|
|
|
free_chain(idx[0]);
|
|
|
|
|
2019-08-12 11:20:32 +00:00
|
|
|
release(&disk.vdisk_lock);
|
2019-06-13 10:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
virtio_disk_intr()
|
|
|
|
{
|
2019-08-12 11:20:32 +00:00
|
|
|
acquire(&disk.vdisk_lock);
|
2019-06-13 10:49:02 +00:00
|
|
|
|
2019-08-12 11:20:32 +00:00
|
|
|
while((disk.used_idx % NUM) != (disk.used->id % NUM)){
|
|
|
|
int id = disk.used->elems[disk.used_idx].id;
|
2019-06-13 13:40:17 +00:00
|
|
|
|
2019-08-12 11:20:32 +00:00
|
|
|
if(disk.info[id].status != 0)
|
2019-06-13 13:40:17 +00:00
|
|
|
panic("virtio_disk_intr status");
|
2019-07-30 16:53:19 +00:00
|
|
|
|
2019-08-12 11:20:32 +00:00
|
|
|
disk.info[id].b->disk = 0; // disk is done with buf
|
|
|
|
wakeup(disk.info[id].b);
|
2019-06-13 10:49:02 +00:00
|
|
|
|
2019-08-12 11:20:32 +00:00
|
|
|
disk.used_idx = (disk.used_idx + 1) % NUM;
|
2019-06-13 10:49:02 +00:00
|
|
|
}
|
|
|
|
|
2019-08-12 11:20:32 +00:00
|
|
|
release(&disk.vdisk_lock);
|
2019-06-13 10:49:02 +00:00
|
|
|
}
|