These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / seabios / src / hw / virtio-blk.c
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 "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"
24
25 struct virtiodrive_s {
26     struct drive_s drive;
27     struct vring_virtqueue *vq;
28     struct vp_device vp;
29 };
30
31 static int
32 virtio_blk_op(struct disk_op_s *op, int write)
33 {
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,
39         .ioprio = 0,
40         .sector = op->lba,
41     };
42     u8 status = VIRTIO_BLK_S_UNSUPP;
43     struct vring_list sg[] = {
44         {
45             .addr       = (void*)(&hdr),
46             .length     = sizeof(hdr),
47         },
48         {
49             .addr       = op->buf_fl,
50             .length     = vdrive_gf->drive.blksize * op->count,
51         },
52         {
53             .addr       = (void*)(&status),
54             .length     = sizeof(status),
55         },
56     };
57
58     /* Add to virtqueue and kick host */
59     if (write)
60         vring_add_buf(vq, sg, 2, 1, 0, 0);
61     else
62         vring_add_buf(vq, sg, 1, 2, 0, 0);
63     vring_kick(&vdrive_gf->vp, vq, 1);
64
65     /* Wait for reply */
66     while (!vring_more_used(vq))
67         usleep(5);
68
69     /* Reclaim virtqueue element */
70     vring_get_buf(vq, NULL);
71
72     /* Clear interrupt status register.  Avoid leaving interrupts stuck if
73      * VRING_AVAIL_F_NO_INTERRUPT was ignored and interrupts were raised.
74      */
75     vp_get_isr(&vdrive_gf->vp);
76
77     return status == VIRTIO_BLK_S_OK ? DISK_RET_SUCCESS : DISK_RET_EBADTRACK;
78 }
79
80 int
81 virtio_blk_process_op(struct disk_op_s *op)
82 {
83     if (! CONFIG_VIRTIO_BLK)
84         return 0;
85     switch (op->command) {
86     case CMD_READ:
87         return virtio_blk_op(op, 0);
88     case CMD_WRITE:
89         return virtio_blk_op(op, 1);
90     default:
91         return default_process_op(op);
92     }
93 }
94
95 static void
96 init_virtio_blk(struct pci_device *pci)
97 {
98     u16 bdf = pci->bdf;
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));
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 = 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 %x:%x\n",
114                 pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf));
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 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));
126             goto fail;
127         }
128
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));
136             goto fail;
137         }
138
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);
144         } else {
145             vdrive->drive.blksize = DISK_SECTOR_SIZE;
146         }
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);
151             goto fail;
152         }
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);
156
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);
163     } else {
164         struct virtio_blk_config cfg;
165         vp_get_legacy(&vdrive->vp, 0, &cfg, sizeof(cfg));
166
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;
170
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);
175
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);
180             goto fail;
181         }
182         vdrive->drive.pchs.cylinder = cfg.cylinders;
183         vdrive->drive.pchs.head = cfg.heads;
184         vdrive->drive.pchs.sector = cfg.sectors;
185     }
186
187     char *desc = znprintf(MAXDESCSIZE, "Virtio disk PCI:%x:%x",
188                           pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf));
189
190     boot_add_hd(&vdrive->drive, desc, bootprio_find_pci_device(pci));
191
192     status |= VIRTIO_CONFIG_S_DRIVER_OK;
193     vp_set_status(&vdrive->vp, status);
194     return;
195
196 fail:
197     vp_reset(&vdrive->vp);
198     free(vdrive->vq);
199     free(vdrive);
200 }
201
202 void
203 virtio_blk_setup(void)
204 {
205     ASSERT32FLAT();
206     if (! CONFIG_VIRTIO_BLK)
207         return;
208
209     dprintf(3, "init virtio-blk\n");
210
211     struct pci_device *pci;
212     foreachpci(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))
216             continue;
217         init_virtio_blk(pci);
218     }
219 }