1 // Virtio block boot support.
2 //
3 // Copyright (C) 2010 Red Hat Inc.
4 //
5 // Authors:
6 //  Gleb Natapov <gnatapov@redhat.com>
7 //
8 // This file may be distributed under the terms of the GNU LGPLv3 license.
9 
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 "pcidevice.h" // foreachpci
16 #include "pci_ids.h" // PCI_DEVICE_ID_VIRTIO_BLK
17 #include "pci_regs.h" // PCI_VENDOR_ID
18 #include "stacks.h" // run_thread
19 #include "std/disk.h" // DISK_RET_SUCCESS
20 #include "string.h" // memset
21 #include "util.h" // usleep
22 #include "virtio-pci.h"
23 #include "virtio-ring.h"
24 #include "virtio-blk.h"
25 
26 struct virtiodrive_s {
27     struct drive_s drive;
28     struct vring_virtqueue *vq;
29     struct vp_device vp;
30 };
31 
32 static int
virtio_blk_op(struct disk_op_s * op,int write)33 virtio_blk_op(struct disk_op_s *op, int write)
34 {
35     struct virtiodrive_s *vdrive =
36         container_of(op->drive_fl, struct virtiodrive_s, drive);
37     struct vring_virtqueue *vq = vdrive->vq;
38     struct virtio_blk_outhdr hdr = {
39         .type = write ? VIRTIO_BLK_T_OUT : VIRTIO_BLK_T_IN,
40         .ioprio = 0,
41         .sector = op->lba,
42     };
43     u8 status = VIRTIO_BLK_S_UNSUPP;
44     struct vring_list sg[] = {
45         {
46             .addr       = (void*)(&hdr),
47             .length     = sizeof(hdr),
48         },
49         {
50             .addr       = op->buf_fl,
51             .length     = vdrive->drive.blksize * op->count,
52         },
53         {
54             .addr       = (void*)(&status),
55             .length     = sizeof(status),
56         },
57     };
58 
59     /* Add to virtqueue and kick host */
60     if (write)
61         vring_add_buf(vq, sg, 2, 1, 0, 0);
62     else
63         vring_add_buf(vq, sg, 1, 2, 0, 0);
64     vring_kick(&vdrive->vp, vq, 1);
65 
66     /* Wait for reply */
67     while (!vring_more_used(vq))
68         usleep(5);
69 
70     /* Reclaim virtqueue element */
71     vring_get_buf(vq, NULL);
72 
73     /* Clear interrupt status register.  Avoid leaving interrupts stuck if
74      * VRING_AVAIL_F_NO_INTERRUPT was ignored and interrupts were raised.
75      */
76     vp_get_isr(&vdrive->vp);
77 
78     return status == VIRTIO_BLK_S_OK ? DISK_RET_SUCCESS : DISK_RET_EBADTRACK;
79 }
80 
81 int
virtio_blk_process_op(struct disk_op_s * op)82 virtio_blk_process_op(struct disk_op_s *op)
83 {
84     if (! CONFIG_VIRTIO_BLK)
85         return 0;
86     switch (op->command) {
87     case CMD_READ:
88         return virtio_blk_op(op, 0);
89     case CMD_WRITE:
90         return virtio_blk_op(op, 1);
91     default:
92         return default_process_op(op);
93     }
94 }
95 
96 static void
init_virtio_blk(void * data)97 init_virtio_blk(void *data)
98 {
99     struct pci_device *pci = data;
100     u8 status = VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER;
101     dprintf(1, "found virtio-blk at %pP\n", pci);
102     struct virtiodrive_s *vdrive = malloc_low(sizeof(*vdrive));
103     if (!vdrive) {
104         warn_noalloc();
105         return;
106     }
107     memset(vdrive, 0, sizeof(*vdrive));
108     vdrive->drive.type = DTYPE_VIRTIO_BLK;
109     vdrive->drive.cntl_id = pci->bdf;
110 
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 %pP\n", pci);
114         goto fail;
115     }
116 
117     if (vdrive->vp.use_modern) {
118         struct vp_device *vp = &vdrive->vp;
119         u64 features = vp_get_features(vp);
120         u64 version1 = 1ull << VIRTIO_F_VERSION_1;
121         u64 iommu_platform = 1ull << VIRTIO_F_IOMMU_PLATFORM;
122         u64 blk_size = 1ull << VIRTIO_BLK_F_BLK_SIZE;
123         if (!(features & version1)) {
124             dprintf(1, "modern device without virtio_1 feature bit: %pP\n", pci);
125             goto fail;
126         }
127 
128         features = features & (version1 | iommu_platform | blk_size);
129         vp_set_features(vp, features);
130         status |= VIRTIO_CONFIG_S_FEATURES_OK;
131         vp_set_status(vp, status);
132         if (!(vp_get_status(vp) & VIRTIO_CONFIG_S_FEATURES_OK)) {
133             dprintf(1, "device didn't accept features: %pP\n", pci);
134             goto fail;
135         }
136 
137         vdrive->drive.sectors =
138             vp_read(&vp->device, struct virtio_blk_config, capacity);
139         if (features & blk_size) {
140             vdrive->drive.blksize =
141                 vp_read(&vp->device, struct virtio_blk_config, blk_size);
142         } else {
143             vdrive->drive.blksize = DISK_SECTOR_SIZE;
144         }
145         if (vdrive->drive.blksize != DISK_SECTOR_SIZE) {
146             dprintf(1, "virtio-blk %pP block size %d is unsupported\n",
147                     pci, vdrive->drive.blksize);
148             goto fail;
149         }
150         dprintf(3, "virtio-blk %pP blksize=%d sectors=%u\n",
151                 pci, vdrive->drive.blksize, (u32)vdrive->drive.sectors);
152 
153         vdrive->drive.pchs.cylinder =
154             vp_read(&vp->device, struct virtio_blk_config, cylinders);
155         vdrive->drive.pchs.head =
156             vp_read(&vp->device, struct virtio_blk_config, heads);
157         vdrive->drive.pchs.sector =
158             vp_read(&vp->device, struct virtio_blk_config, sectors);
159     } else {
160         struct virtio_blk_config cfg;
161         vp_get_legacy(&vdrive->vp, 0, &cfg, sizeof(cfg));
162 
163         u64 f = vp_get_features(&vdrive->vp);
164         vdrive->drive.blksize = (f & (1 << VIRTIO_BLK_F_BLK_SIZE)) ?
165             cfg.blk_size : DISK_SECTOR_SIZE;
166 
167         vdrive->drive.sectors = cfg.capacity;
168         dprintf(3, "virtio-blk %pP blksize=%d sectors=%u\n",
169                 pci, vdrive->drive.blksize, (u32)vdrive->drive.sectors);
170 
171         if (vdrive->drive.blksize != DISK_SECTOR_SIZE) {
172             dprintf(1, "virtio-blk %pP block size %d is unsupported\n",
173                     pci, vdrive->drive.blksize);
174             goto fail;
175         }
176         vdrive->drive.pchs.cylinder = cfg.cylinders;
177         vdrive->drive.pchs.head = cfg.heads;
178         vdrive->drive.pchs.sector = cfg.sectors;
179     }
180 
181     char *desc = znprintf(MAXDESCSIZE, "Virtio disk PCI:%pP", pci);
182     boot_add_hd(&vdrive->drive, desc, bootprio_find_pci_device(pci));
183 
184     status |= VIRTIO_CONFIG_S_DRIVER_OK;
185     vp_set_status(&vdrive->vp, status);
186     return;
187 
188 fail:
189     vp_reset(&vdrive->vp);
190     free(vdrive->vq);
191     free(vdrive);
192 }
193 
194 void
virtio_blk_setup(void)195 virtio_blk_setup(void)
196 {
197     ASSERT32FLAT();
198     if (! CONFIG_VIRTIO_BLK)
199         return;
200 
201     dprintf(3, "init virtio-blk\n");
202 
203     struct pci_device *pci;
204     foreachpci(pci) {
205         if (pci->vendor != PCI_VENDOR_ID_REDHAT_QUMRANET ||
206             (pci->device != PCI_DEVICE_ID_VIRTIO_BLK_09 &&
207              pci->device != PCI_DEVICE_ID_VIRTIO_BLK_10))
208             continue;
209         run_thread(init_virtio_blk, pci);
210     }
211 }
212