Make sure virtio-blk is fully compiled out if not wanted.
[seabios.git] / src / 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 "util.h" // dprintf
11 #include "pci.h" // foreachpci
12 #include "config.h" // CONFIG_*
13 #include "biosvar.h" // GET_GLOBAL
14 #include "pci_ids.h" // PCI_DEVICE_ID_VIRTIO_BLK
15 #include "pci_regs.h" // PCI_VENDOR_ID
16 #include "boot.h" // add_bcv_internal
17 #include "virtio-pci.h"
18 #include "virtio-ring.h"
19 #include "virtio-blk.h"
20 #include "disk.h"
21
22 struct virtiodrive_s {
23     struct drive_s drive;
24     struct vring_virtqueue *vq;
25     u16 ioaddr;
26 };
27
28 static int
29 virtio_blk_read(struct disk_op_s *op)
30 {
31     struct virtiodrive_s *vdrive_g =
32         container_of(op->drive_g, struct virtiodrive_s, drive);
33     struct vring_virtqueue *vq = GET_GLOBAL(vdrive_g->vq);
34     struct virtio_blk_outhdr hdr = {
35         .type = VIRTIO_BLK_T_IN,
36         .ioprio = 0,
37         .sector = op->lba,
38     };
39     u8 status = VIRTIO_BLK_S_UNSUPP;
40     struct vring_list sg[] = {
41         {
42             .addr       = MAKE_FLATPTR(GET_SEG(SS), &hdr),
43             .length     = sizeof(hdr),
44         },
45         {
46             .addr       = op->buf_fl,
47             .length     = GET_GLOBAL(vdrive_g->drive.blksize) * op->count,
48         },
49         {
50             .addr       = MAKE_FLATPTR(GET_SEG(SS), &status),
51             .length     = sizeof(status),
52         },
53     };
54
55     /* Add to virtqueue and kick host */
56     vring_add_buf(vq, sg, 1, 2, 0, 0);
57     vring_kick(GET_GLOBAL(vdrive_g->ioaddr), vq, 1);
58
59     /* Wait for reply */
60     while (!vring_more_used(vq))
61         usleep(5);
62
63     /* Reclaim virtqueue element */
64     vring_get_buf(vq, NULL);
65     return status == VIRTIO_BLK_S_OK ? DISK_RET_SUCCESS : DISK_RET_EBADTRACK;
66 }
67
68 int
69 process_virtio_op(struct disk_op_s *op)
70 {
71     if (! CONFIG_VIRTIO_BLK || CONFIG_COREBOOT)
72         return 0;
73     switch (op->command) {
74     case CMD_READ:
75         return virtio_blk_read(op);
76     case CMD_FORMAT:
77     case CMD_WRITE:
78         return DISK_RET_EWRITEPROTECT;
79     case CMD_RESET:
80     case CMD_ISREADY:
81     case CMD_VERIFY:
82     case CMD_SEEK:
83         return DISK_RET_SUCCESS;
84     default:
85         op->count = 0;
86         return DISK_RET_EPARAM;
87     }
88 }
89
90 static void
91 init_virtio_blk(u16 bdf)
92 {
93     dprintf(1, "found virtio-blk at %x:%x\n", pci_bdf_to_bus(bdf),
94             pci_bdf_to_dev(bdf));
95     char *desc = malloc_tmphigh(MAXDESCSIZE);
96     struct virtiodrive_s *vdrive_g = malloc_fseg(sizeof(*vdrive_g));
97     struct vring_virtqueue *vq = memalign_low(PAGE_SIZE, sizeof(*vq));
98     if (!vdrive_g || !desc || !vq) {
99         warn_noalloc();
100         goto fail;
101     }
102     memset(vdrive_g, 0, sizeof(*vdrive_g));
103     vdrive_g->drive.type = DTYPE_VIRTIO;
104     vdrive_g->drive.cntl_id = bdf;
105     vdrive_g->vq = vq;
106
107     u16 ioaddr = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) &
108         PCI_BASE_ADDRESS_IO_MASK;
109
110     vdrive_g->ioaddr = ioaddr;
111
112     vp_reset(ioaddr);
113     vp_set_status(ioaddr, VIRTIO_CONFIG_S_ACKNOWLEDGE |
114                   VIRTIO_CONFIG_S_DRIVER );
115
116     if (vp_find_vq(ioaddr, 0, vdrive_g->vq) < 0 ) {
117         dprintf(1, "fail to find vq for virtio-blk %x:%x\n",
118                 pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf));
119         goto fail;
120     }
121
122     struct virtio_blk_config cfg;
123     vp_get(ioaddr, 0, &cfg, sizeof(cfg));
124
125     u32 f = vp_get_features(ioaddr);
126     vdrive_g->drive.blksize = (f & (1 << VIRTIO_BLK_F_BLK_SIZE)) ?
127         cfg.blk_size : DISK_SECTOR_SIZE;
128
129     vdrive_g->drive.sectors = cfg.capacity;
130     dprintf(3, "virtio-blk %x:%x blksize=%d sectors=%u\n",
131             pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf),
132             vdrive_g->drive.blksize, (u32)vdrive_g->drive.sectors);
133
134     if (vdrive_g->drive.blksize != DISK_SECTOR_SIZE) {
135         dprintf(1, "virtio-blk %x:%x block size %d is unsupported\n",
136                 pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf),
137                 vdrive_g->drive.blksize);
138         goto fail;
139     }
140
141     vdrive_g->drive.pchs.cylinders = cfg.cylinders;
142     vdrive_g->drive.pchs.heads = cfg.heads;
143     vdrive_g->drive.pchs.spt = cfg.sectors;
144
145     setup_translation(&vdrive_g->drive);
146     add_bcv_internal(&vdrive_g->drive);
147
148     snprintf(desc, MAXDESCSIZE, "Virtio disk PCI:%x:%x",
149              pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf));
150
151     vdrive_g->drive.desc = desc;
152
153     vp_set_status(ioaddr, VIRTIO_CONFIG_S_ACKNOWLEDGE |
154                   VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_DRIVER_OK);
155     return;
156
157 fail:
158     free(vdrive_g);
159     free(desc);
160     free(vq);
161 }
162
163 void
164 virtio_blk_setup(void)
165 {
166     ASSERT32FLAT();
167     if (! CONFIG_VIRTIO_BLK || CONFIG_COREBOOT)
168         return;
169
170     dprintf(3, "init virtio-blk\n");
171
172     int bdf, max;
173     u32 id = PCI_VENDOR_ID_REDHAT_QUMRANET | (PCI_DEVICE_ID_VIRTIO_BLK << 16);
174     foreachpci(bdf, max) {
175         u32 v = pci_config_readl(bdf, PCI_VENDOR_ID);
176         if (v != id)
177             continue;
178         init_virtio_blk(bdf);
179     }
180 }