1 // Virtio block boot support.
3 // Copyright (C) 2010 Red Hat Inc.
6 // Gleb Natapov <gnatapov@redhat.com>
8 // This file may be distributed under the terms of the GNU LGPLv3 license.
10 #include "biosvar.h" // GET_GLOBALFLAT
11 #include "config.h" // CONFIG_*
12 #include "block.h" // struct drive_s
13 #include "malloc.h" // free
14 #include "output.h" // dprintf
15 #include "pci.h" // foreachpci
16 #include "pci_ids.h" // PCI_DEVICE_ID_VIRTIO_BLK
17 #include "pci_regs.h" // PCI_VENDOR_ID
18 #include "std/disk.h" // DISK_RET_SUCCESS
19 #include "string.h" // memset
20 #include "util.h" // usleep
21 #include "virtio-pci.h"
22 #include "virtio-ring.h"
23 #include "virtio-blk.h"
25 struct virtiodrive_s {
27 struct vring_virtqueue *vq;
32 virtio_blk_op(struct disk_op_s *op, int write)
34 struct virtiodrive_s *vdrive_gf =
35 container_of(op->drive_gf, struct virtiodrive_s, drive);
36 struct vring_virtqueue *vq = GET_GLOBALFLAT(vdrive_gf->vq);
37 struct virtio_blk_outhdr hdr = {
38 .type = write ? VIRTIO_BLK_T_OUT : VIRTIO_BLK_T_IN,
42 u8 status = VIRTIO_BLK_S_UNSUPP;
43 struct vring_list sg[] = {
45 .addr = MAKE_FLATPTR(GET_SEG(SS), &hdr),
46 .length = sizeof(hdr),
50 .length = GET_GLOBALFLAT(vdrive_gf->drive.blksize) * op->count,
53 .addr = MAKE_FLATPTR(GET_SEG(SS), &status),
54 .length = sizeof(status),
58 /* Add to virtqueue and kick host */
60 vring_add_buf(vq, sg, 2, 1, 0, 0);
62 vring_add_buf(vq, sg, 1, 2, 0, 0);
63 vring_kick(GET_GLOBALFLAT(vdrive_gf->ioaddr), vq, 1);
66 while (!vring_more_used(vq))
69 /* Reclaim virtqueue element */
70 vring_get_buf(vq, NULL);
72 /* Clear interrupt status register. Avoid leaving interrupts stuck if
73 * VRING_AVAIL_F_NO_INTERRUPT was ignored and interrupts were raised.
75 vp_get_isr(GET_GLOBALFLAT(vdrive_gf->ioaddr));
77 return status == VIRTIO_BLK_S_OK ? DISK_RET_SUCCESS : DISK_RET_EBADTRACK;
81 process_virtio_blk_op(struct disk_op_s *op)
83 if (! CONFIG_VIRTIO_BLK)
85 switch (op->command) {
87 return virtio_blk_op(op, 0);
89 return virtio_blk_op(op, 1);
95 return DISK_RET_SUCCESS;
97 return DISK_RET_EPARAM;
102 init_virtio_blk(struct pci_device *pci)
105 dprintf(1, "found virtio-blk at %x:%x\n", pci_bdf_to_bus(bdf),
106 pci_bdf_to_dev(bdf));
107 struct virtiodrive_s *vdrive = malloc_fseg(sizeof(*vdrive));
112 memset(vdrive, 0, sizeof(*vdrive));
113 vdrive->drive.type = DTYPE_VIRTIO_BLK;
114 vdrive->drive.cntl_id = bdf;
116 u16 ioaddr = vp_init_simple(bdf);
117 vdrive->ioaddr = ioaddr;
118 if (vp_find_vq(ioaddr, 0, &vdrive->vq) < 0 ) {
119 dprintf(1, "fail to find vq for virtio-blk %x:%x\n",
120 pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf));
124 struct virtio_blk_config cfg;
125 vp_get(ioaddr, 0, &cfg, sizeof(cfg));
127 u32 f = vp_get_features(ioaddr);
128 vdrive->drive.blksize = (f & (1 << VIRTIO_BLK_F_BLK_SIZE)) ?
129 cfg.blk_size : DISK_SECTOR_SIZE;
131 vdrive->drive.sectors = cfg.capacity;
132 dprintf(3, "virtio-blk %x:%x blksize=%d sectors=%u\n",
133 pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf),
134 vdrive->drive.blksize, (u32)vdrive->drive.sectors);
136 if (vdrive->drive.blksize != DISK_SECTOR_SIZE) {
137 dprintf(1, "virtio-blk %x:%x block size %d is unsupported\n",
138 pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf),
139 vdrive->drive.blksize);
143 vdrive->drive.pchs.cylinder = cfg.cylinders;
144 vdrive->drive.pchs.head = cfg.heads;
145 vdrive->drive.pchs.sector = cfg.sectors;
146 char *desc = znprintf(MAXDESCSIZE, "Virtio disk PCI:%x:%x",
147 pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf));
149 boot_add_hd(&vdrive->drive, desc, bootprio_find_pci_device(pci));
151 vp_set_status(ioaddr, VIRTIO_CONFIG_S_ACKNOWLEDGE |
152 VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_DRIVER_OK);
162 virtio_blk_setup(void)
165 if (! CONFIG_VIRTIO_BLK)
168 dprintf(3, "init virtio-blk\n");
170 struct pci_device *pci;
172 if (pci->vendor != PCI_VENDOR_ID_REDHAT_QUMRANET
173 || pci->device != PCI_DEVICE_ID_VIRTIO_BLK)
175 init_virtio_blk(pci);