Add qemu 2.4.0
[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     u16 ioaddr;
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 = GET_GLOBALFLAT(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       = MAKE_FLATPTR(GET_SEG(SS), &hdr),
46             .length     = sizeof(hdr),
47         },
48         {
49             .addr       = op->buf_fl,
50             .length     = GET_GLOBALFLAT(vdrive_gf->drive.blksize) * op->count,
51         },
52         {
53             .addr       = MAKE_FLATPTR(GET_SEG(SS), &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(GET_GLOBALFLAT(vdrive_gf->ioaddr), 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(GET_GLOBALFLAT(vdrive_gf->ioaddr));
76
77     return status == VIRTIO_BLK_S_OK ? DISK_RET_SUCCESS : DISK_RET_EBADTRACK;
78 }
79
80 int
81 process_virtio_blk_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     case CMD_FORMAT:
91     case CMD_RESET:
92     case CMD_ISREADY:
93     case CMD_VERIFY:
94     case CMD_SEEK:
95         return DISK_RET_SUCCESS;
96     default:
97         return DISK_RET_EPARAM;
98     }
99 }
100
101 static void
102 init_virtio_blk(struct pci_device *pci)
103 {
104     u16 bdf = pci->bdf;
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));
108     if (!vdrive) {
109         warn_noalloc();
110         return;
111     }
112     memset(vdrive, 0, sizeof(*vdrive));
113     vdrive->drive.type = DTYPE_VIRTIO_BLK;
114     vdrive->drive.cntl_id = bdf;
115
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));
121         goto fail;
122     }
123
124     struct virtio_blk_config cfg;
125     vp_get(ioaddr, 0, &cfg, sizeof(cfg));
126
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;
130
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);
135
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);
140         goto fail;
141     }
142
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));
148
149     boot_add_hd(&vdrive->drive, desc, bootprio_find_pci_device(pci));
150
151     vp_set_status(ioaddr, VIRTIO_CONFIG_S_ACKNOWLEDGE |
152                   VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_DRIVER_OK);
153     return;
154
155 fail:
156     vp_reset(ioaddr);
157     free(vdrive->vq);
158     free(vdrive);
159 }
160
161 void
162 virtio_blk_setup(void)
163 {
164     ASSERT32FLAT();
165     if (! CONFIG_VIRTIO_BLK)
166         return;
167
168     dprintf(3, "init virtio-blk\n");
169
170     struct pci_device *pci;
171     foreachpci(pci) {
172         if (pci->vendor != PCI_VENDOR_ID_REDHAT_QUMRANET
173             || pci->device != PCI_DEVICE_ID_VIRTIO_BLK)
174             continue;
175         init_virtio_blk(pci);
176     }
177 }