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, bootprio_find_pci_device, is_bootprio_strict
22 #include "virtio-pci.h"
23 #include "virtio-mmio.h"
24 #include "virtio-ring.h"
25 #include "virtio-blk.h"
26
27 struct virtiodrive_s {
28 struct drive_s drive;
29 struct vring_virtqueue *vq;
30 struct vp_device vp;
31 };
32
33 static int
virtio_blk_op(struct disk_op_s * op,int write)34 virtio_blk_op(struct disk_op_s *op, int write)
35 {
36 struct virtiodrive_s *vdrive =
37 container_of(op->drive_fl, struct virtiodrive_s, drive);
38 struct vring_virtqueue *vq = vdrive->vq;
39 struct virtio_blk_outhdr hdr = {
40 .type = write ? VIRTIO_BLK_T_OUT : VIRTIO_BLK_T_IN,
41 .ioprio = 0,
42 .sector = op->lba,
43 };
44 u8 status = VIRTIO_BLK_S_UNSUPP;
45 struct vring_list sg[] = {
46 {
47 .addr = (void*)(&hdr),
48 .length = sizeof(hdr),
49 },
50 {
51 .addr = op->buf_fl,
52 .length = vdrive->drive.blksize * op->count,
53 },
54 {
55 .addr = (void*)(&status),
56 .length = sizeof(status),
57 },
58 };
59
60 /* Add to virtqueue and kick host */
61 if (write)
62 vring_add_buf(vq, sg, 2, 1, 0, 0);
63 else
64 vring_add_buf(vq, sg, 1, 2, 0, 0);
65 vring_kick(&vdrive->vp, vq, 1);
66
67 /* Wait for reply */
68 while (!vring_more_used(vq))
69 usleep(5);
70
71 /* Reclaim virtqueue element */
72 vring_get_buf(vq, NULL);
73
74 /* Clear interrupt status register. Avoid leaving interrupts stuck if
75 * VRING_AVAIL_F_NO_INTERRUPT was ignored and interrupts were raised.
76 */
77 vp_get_isr(&vdrive->vp);
78
79 return status == VIRTIO_BLK_S_OK ? DISK_RET_SUCCESS : DISK_RET_EBADTRACK;
80 }
81
82 int
virtio_blk_process_op(struct disk_op_s * op)83 virtio_blk_process_op(struct disk_op_s *op)
84 {
85 if (! CONFIG_VIRTIO_BLK)
86 return 0;
87 switch (op->command) {
88 case CMD_READ:
89 return virtio_blk_op(op, 0);
90 case CMD_WRITE:
91 return virtio_blk_op(op, 1);
92 default:
93 return default_process_op(op);
94 }
95 }
96
97 static void
init_virtio_blk(void * data)98 init_virtio_blk(void *data)
99 {
100 struct pci_device *pci = data;
101 u8 status = VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER;
102 dprintf(1, "found virtio-blk at %pP\n", pci);
103 struct virtiodrive_s *vdrive = malloc_low(sizeof(*vdrive));
104 if (!vdrive) {
105 warn_noalloc();
106 return;
107 }
108 memset(vdrive, 0, sizeof(*vdrive));
109 vdrive->drive.type = DTYPE_VIRTIO_BLK;
110 vdrive->drive.cntl_id = pci->bdf;
111
112 vp_init_simple(&vdrive->vp, pci);
113 if (vp_find_vq(&vdrive->vp, 0, &vdrive->vq) < 0 ) {
114 dprintf(1, "fail to find vq for virtio-blk %pP\n", pci);
115 goto fail;
116 }
117
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 iommu_platform = 1ull << VIRTIO_F_IOMMU_PLATFORM;
123 u64 blk_size = 1ull << VIRTIO_BLK_F_BLK_SIZE;
124 if (!(features & version1)) {
125 dprintf(1, "modern device without virtio_1 feature bit: %pP\n", pci);
126 goto fail;
127 }
128
129 features = features & (version1 | iommu_platform | 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: %pP\n", pci);
135 goto fail;
136 }
137
138 vdrive->drive.sectors =
139 vp_read(&vp->device, struct virtio_blk_config, capacity);
140 if (features & blk_size) {
141 vdrive->drive.blksize =
142 vp_read(&vp->device, struct virtio_blk_config, blk_size);
143 } else {
144 vdrive->drive.blksize = DISK_SECTOR_SIZE;
145 }
146 if (vdrive->drive.blksize != DISK_SECTOR_SIZE) {
147 dprintf(1, "virtio-blk %pP block size %d is unsupported\n",
148 pci, vdrive->drive.blksize);
149 goto fail;
150 }
151 dprintf(3, "virtio-blk %pP blksize=%d sectors=%u\n",
152 pci, vdrive->drive.blksize, (u32)vdrive->drive.sectors);
153
154 vdrive->drive.pchs.cylinder =
155 vp_read(&vp->device, struct virtio_blk_config, cylinders);
156 vdrive->drive.pchs.head =
157 vp_read(&vp->device, struct virtio_blk_config, heads);
158 vdrive->drive.pchs.sector =
159 vp_read(&vp->device, struct virtio_blk_config, sectors);
160 } else {
161 struct virtio_blk_config cfg;
162 vp_get_legacy(&vdrive->vp, 0, &cfg, sizeof(cfg));
163
164 u64 f = vp_get_features(&vdrive->vp);
165 vdrive->drive.blksize = (f & (1 << VIRTIO_BLK_F_BLK_SIZE)) ?
166 cfg.blk_size : DISK_SECTOR_SIZE;
167
168 vdrive->drive.sectors = cfg.capacity;
169 dprintf(3, "virtio-blk %pP blksize=%d sectors=%u\n",
170 pci, vdrive->drive.blksize, (u32)vdrive->drive.sectors);
171
172 if (vdrive->drive.blksize != DISK_SECTOR_SIZE) {
173 dprintf(1, "virtio-blk %pP block size %d is unsupported\n",
174 pci, vdrive->drive.blksize);
175 goto fail;
176 }
177 vdrive->drive.pchs.cylinder = cfg.cylinders;
178 vdrive->drive.pchs.head = cfg.heads;
179 vdrive->drive.pchs.sector = cfg.sectors;
180 }
181
182 char *desc = znprintf(MAXDESCSIZE, "Virtio disk PCI:%pP", pci);
183 boot_add_hd(&vdrive->drive, desc, bootprio_find_pci_device(pci));
184
185 status |= VIRTIO_CONFIG_S_DRIVER_OK;
186 vp_set_status(&vdrive->vp, status);
187
188 boot_lchs_find_pci_device(pci, &vdrive->drive.lchs);
189 return;
190
191 fail:
192 vp_reset(&vdrive->vp);
193 free(vdrive->vq);
194 free(vdrive);
195 }
196
197 void
init_virtio_blk_mmio(void * mmio)198 init_virtio_blk_mmio(void *mmio)
199 {
200 u8 status = VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER;
201 dprintf(1, "found virtio-blk-mmio at %p\n", mmio);
202 struct virtiodrive_s *vdrive = malloc_low(sizeof(*vdrive));
203 if (!vdrive) {
204 warn_noalloc();
205 return;
206 }
207 memset(vdrive, 0, sizeof(*vdrive));
208 vdrive->drive.type = DTYPE_VIRTIO_BLK;
209 vdrive->drive.cntl_id = (u32)mmio;
210
211 vp_init_mmio(&vdrive->vp, mmio);
212 if (vp_find_vq(&vdrive->vp, 0, &vdrive->vq) < 0 ) {
213 dprintf(1, "fail to find vq for virtio-blk-mmio %p\n", mmio);
214 goto fail;
215 }
216
217 struct vp_device *vp = &vdrive->vp;
218 u64 features = vp_get_features(vp);
219 u64 version1 = 1ull << VIRTIO_F_VERSION_1;
220 u64 blk_size = 1ull << VIRTIO_BLK_F_BLK_SIZE;
221
222 features = features & (version1 | blk_size);
223 vp_set_features(vp, features);
224 status |= VIRTIO_CONFIG_S_FEATURES_OK;
225 vp_set_status(vp, status);
226 if (!(vp_get_status(vp) & VIRTIO_CONFIG_S_FEATURES_OK)) {
227 dprintf(1, "device didn't accept features: %p\n", mmio);
228 goto fail;
229 }
230
231 vdrive->drive.sectors =
232 vp_read(&vp->device, struct virtio_blk_config, capacity);
233 if (features & blk_size) {
234 vdrive->drive.blksize =
235 vp_read(&vp->device, struct virtio_blk_config, blk_size);
236 } else {
237 vdrive->drive.blksize = DISK_SECTOR_SIZE;
238 }
239 if (vdrive->drive.blksize != DISK_SECTOR_SIZE) {
240 dprintf(1, "virtio-blk-mmio %p block size %d is unsupported\n",
241 mmio, vdrive->drive.blksize);
242 goto fail;
243 }
244 dprintf(1, "virtio-blk-mmio %p blksize=%d sectors=%u\n",
245 mmio, vdrive->drive.blksize, (u32)vdrive->drive.sectors);
246
247 vdrive->drive.pchs.cylinder =
248 vp_read(&vp->device, struct virtio_blk_config, cylinders);
249 vdrive->drive.pchs.head =
250 vp_read(&vp->device, struct virtio_blk_config, heads);
251 vdrive->drive.pchs.sector =
252 vp_read(&vp->device, struct virtio_blk_config, sectors);
253
254 char *desc = znprintf(MAXDESCSIZE, "Virtio disk mmio:%p", mmio);
255 boot_add_hd(&vdrive->drive, desc, bootprio_find_mmio_device(mmio));
256
257 status |= VIRTIO_CONFIG_S_DRIVER_OK;
258 vp_set_status(&vdrive->vp, status);
259 return;
260
261 fail:
262 vp_reset(&vdrive->vp);
263 free(vdrive->vq);
264 free(vdrive);
265 }
266
267 void
virtio_blk_setup(void)268 virtio_blk_setup(void)
269 {
270 u8 skip_nonbootable = is_bootprio_strict();
271
272 ASSERT32FLAT();
273 if (! CONFIG_VIRTIO_BLK)
274 return;
275
276 dprintf(3, "init virtio-blk\n");
277
278 struct pci_device *pci;
279 foreachpci(pci) {
280 if (pci->vendor != PCI_VENDOR_ID_REDHAT_QUMRANET ||
281 (pci->device != PCI_DEVICE_ID_VIRTIO_BLK_09 &&
282 pci->device != PCI_DEVICE_ID_VIRTIO_BLK_10))
283 continue;
284
285 if (skip_nonbootable && bootprio_find_pci_device(pci) < 0) {
286 dprintf(1, "skipping init of a non-bootable virtio-blk at %pP\n",
287 pci);
288 continue;
289 }
290
291 run_thread(init_virtio_blk, pci);
292 }
293 }
294