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 = 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 = (void*)(&hdr),
46 .length = sizeof(hdr),
50 .length = vdrive_gf->drive.blksize * op->count,
53 .addr = (void*)(&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(&vdrive_gf->vp, 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(&vdrive_gf->vp);
77 return status == VIRTIO_BLK_S_OK ? DISK_RET_SUCCESS : DISK_RET_EBADTRACK;
81 virtio_blk_process_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);
91 return default_process_op(op);
96 init_virtio_blk(struct pci_device *pci)
99 u8 status = VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER;
100 dprintf(1, "found virtio-blk at %x:%x\n", pci_bdf_to_bus(bdf),
101 pci_bdf_to_dev(bdf));
102 struct virtiodrive_s *vdrive = malloc_fseg(sizeof(*vdrive));
107 memset(vdrive, 0, sizeof(*vdrive));
108 vdrive->drive.type = DTYPE_VIRTIO_BLK;
109 vdrive->drive.cntl_id = bdf;
111 vp_init_simple(&vdrive->vp, pci);
112 if (vp_find_vq(&vdrive->vp, 0, &vdrive->vq) < 0 ) {
113 dprintf(1, "fail to find vq for virtio-blk %x:%x\n",
114 pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf));
118 if (vdrive->vp.use_modern) {
119 struct vp_device *vp = &vdrive->vp;
120 u64 features = vp_get_features(vp);
121 u64 version1 = 1ull << VIRTIO_F_VERSION_1;
122 u64 blk_size = 1ull << VIRTIO_BLK_F_BLK_SIZE;
123 if (!(features & version1)) {
124 dprintf(1, "modern device without virtio_1 feature bit: %x:%x\n",
125 pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf));
129 features = features & (version1 | blk_size);
130 vp_set_features(vp, features);
131 status |= VIRTIO_CONFIG_S_FEATURES_OK;
132 vp_set_status(vp, status);
133 if (!(vp_get_status(vp) & VIRTIO_CONFIG_S_FEATURES_OK)) {
134 dprintf(1, "device didn't accept features: %x:%x\n",
135 pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf));
139 vdrive->drive.sectors =
140 vp_read(&vp->device, struct virtio_blk_config, capacity);
141 if (features & blk_size) {
142 vdrive->drive.blksize =
143 vp_read(&vp->device, struct virtio_blk_config, blk_size);
145 vdrive->drive.blksize = DISK_SECTOR_SIZE;
147 if (vdrive->drive.blksize != DISK_SECTOR_SIZE) {
148 dprintf(1, "virtio-blk %x:%x block size %d is unsupported\n",
149 pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf),
150 vdrive->drive.blksize);
153 dprintf(3, "virtio-blk %x:%x blksize=%d sectors=%u\n",
154 pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf),
155 vdrive->drive.blksize, (u32)vdrive->drive.sectors);
157 vdrive->drive.pchs.cylinder =
158 vp_read(&vp->device, struct virtio_blk_config, cylinders);
159 vdrive->drive.pchs.head =
160 vp_read(&vp->device, struct virtio_blk_config, heads);
161 vdrive->drive.pchs.sector =
162 vp_read(&vp->device, struct virtio_blk_config, sectors);
164 struct virtio_blk_config cfg;
165 vp_get_legacy(&vdrive->vp, 0, &cfg, sizeof(cfg));
167 u64 f = vp_get_features(&vdrive->vp);
168 vdrive->drive.blksize = (f & (1 << VIRTIO_BLK_F_BLK_SIZE)) ?
169 cfg.blk_size : DISK_SECTOR_SIZE;
171 vdrive->drive.sectors = cfg.capacity;
172 dprintf(3, "virtio-blk %x:%x blksize=%d sectors=%u\n",
173 pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf),
174 vdrive->drive.blksize, (u32)vdrive->drive.sectors);
176 if (vdrive->drive.blksize != DISK_SECTOR_SIZE) {
177 dprintf(1, "virtio-blk %x:%x block size %d is unsupported\n",
178 pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf),
179 vdrive->drive.blksize);
182 vdrive->drive.pchs.cylinder = cfg.cylinders;
183 vdrive->drive.pchs.head = cfg.heads;
184 vdrive->drive.pchs.sector = cfg.sectors;
187 char *desc = znprintf(MAXDESCSIZE, "Virtio disk PCI:%x:%x",
188 pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf));
190 boot_add_hd(&vdrive->drive, desc, bootprio_find_pci_device(pci));
192 status |= VIRTIO_CONFIG_S_DRIVER_OK;
193 vp_set_status(&vdrive->vp, status);
197 vp_reset(&vdrive->vp);
203 virtio_blk_setup(void)
206 if (! CONFIG_VIRTIO_BLK)
209 dprintf(3, "init virtio-blk\n");
211 struct pci_device *pci;
213 if (pci->vendor != PCI_VENDOR_ID_REDHAT_QUMRANET ||
214 (pci->device != PCI_DEVICE_ID_VIRTIO_BLK_09 &&
215 pci->device != PCI_DEVICE_ID_VIRTIO_BLK_10))
217 init_virtio_blk(pci);