-#ifndef CONFIG_DEBUG_VIRTIO_BLK
+#ifndef V3_CONFIG_DEBUG_VIRTIO_BLK
#undef PrintDebug
#define PrintDebug(fmt, args...)
#endif
};
-static int virtio_free(struct vm_device * dev) {
- return -1;
-}
+
static int blk_reset(struct virtio_blk_state * virtio) {
if (!(q->avail->flags & VIRTIO_NO_IRQ_FLAG)) {
PrintDebug("Raising IRQ %d\n", blk_state->pci_dev->config_header.intr_line);
- v3_pci_raise_irq(blk_state->virtio_dev->pci_bus, 0, blk_state->pci_dev);
+ v3_pci_raise_irq(blk_state->virtio_dev->pci_bus, blk_state->pci_dev, 0);
blk_state->virtio_cfg.pci_isr = 1;
}
PrintDebug("VIRTIO BLOCK Read for port %d (index =%d), length=%d\n",
port, port_idx, length);
+
switch (port_idx) {
case HOST_FEATURES_PORT:
- if (length != 4) {
- PrintError("Illegal read length for host features\n");
+ case HOST_FEATURES_PORT + 1:
+ case HOST_FEATURES_PORT + 2:
+ case HOST_FEATURES_PORT + 3:
+ if (port_idx + length > HOST_FEATURES_PORT + 4) {
+ PrintError("Illegal read length for host features (len=%d)\n", length);
return -1;
}
- *(uint32_t *)dst = blk_state->virtio_cfg.host_features;
-
+ memcpy(dst, &(blk_state->virtio_cfg.host_features), length);
break;
case VRING_PG_NUM_PORT:
- if (length != 4) {
- PrintError("Illegal read length for page frame number\n");
+ case VRING_PG_NUM_PORT + 1:
+ case VRING_PG_NUM_PORT + 2:
+ case VRING_PG_NUM_PORT + 3:
+ if (port_idx + length > VRING_PG_NUM_PORT + 4) {
+ PrintError("Illegal read length for vring pg num (len=%d)\n", length);
return -1;
}
- *(uint32_t *)dst = blk_state->queue.pfn;
-
+ memcpy(dst, &(blk_state->queue.pfn), length);
break;
case VRING_SIZE_PORT:
- if (length != 2) {
- PrintError("Illegal read length for vring size\n");
+ case VRING_SIZE_PORT + 1:
+ if (length > 2) {
+ PrintError("Illegal read length for vring size (len=%d)\n", length);
return -1;
}
-
- *(uint16_t *)dst = blk_state->queue.queue_size;
+
+ memcpy(dst, &(blk_state->queue.queue_size), length);
break;
case VIRTIO_STATUS_PORT:
if (length != 1) {
- PrintError("Illegal read length for status\n");
+ PrintError("Illegal read length for status (len=%d)\n", length);
return -1;
}
case VIRTIO_ISR_PORT:
*(uint8_t *)dst = blk_state->virtio_cfg.pci_isr;
blk_state->virtio_cfg.pci_isr = 0;
- v3_pci_lower_irq(blk_state->virtio_dev->pci_bus, 0, blk_state->pci_dev);
+ v3_pci_lower_irq(blk_state->virtio_dev->pci_bus, blk_state->pci_dev, 0);
break;
default:
}
+static int virtio_free(struct virtio_dev_state * virtio) {
+ struct virtio_blk_state * blk_state = NULL;
+ struct virtio_blk_state * tmp = NULL;
+
+ list_for_each_entry_safe(blk_state, tmp, &(virtio->dev_list), dev_link) {
+
+ // unregister from PCI
+
+ list_del(&(blk_state->dev_link));
+ V3_Free(blk_state);
+ }
+
+
+ V3_Free(virtio);
+
+ return 0;
+}
+
static struct v3_device_ops dev_ops = {
- .free = virtio_free,
+ .free = (int (*)(void *))virtio_free,
};
pci_dev = v3_pci_register_device(virtio->pci_bus, PCI_STD_DEVICE,
0, PCI_AUTO_DEV_NUM, 0,
"LNX_VIRTIO_BLK", bars,
- NULL, NULL, NULL, blk_state);
+ NULL, NULL, NULL, NULL, blk_state);
if (!pci_dev) {
PrintError("Could not register PCI Device\n");
blk_state->pci_dev = pci_dev;
+
+
+ /* Add backend to list of devices */
+ list_add(&(blk_state->dev_link), &(virtio->dev_list));
/* Block configuration */
blk_state->virtio_cfg.host_features = VIRTIO_SEG_MAX;
virtio_state->pci_bus = pci_bus;
- struct vm_device * dev = v3_allocate_device(dev_id, &dev_ops, virtio_state);
- if (v3_attach_device(vm, dev) == -1) {
+ struct vm_device * dev = v3_add_device(vm, dev_id, &dev_ops, virtio_state);
+
+ if (dev == NULL) {
PrintError("Could not attach device %s\n", dev_id);
+ V3_Free(virtio_state);
return -1;
}
if (v3_dev_add_blk_frontend(vm, dev_id, connect_fn, (void *)virtio_state) == -1) {
PrintError("Could not register %s as block frontend\n", dev_id);
+ v3_remove_device(dev);
return -1;
}