blob: 761e64fa5a2d3f35068913381018701f73c2b56d [file] [log] [blame]
Gleb Natapov89acfa32010-05-10 11:36:37 +03001// 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
Kevin O'Connor5d74ce02011-06-20 22:22:08 -040011#include "pci.h" // foreachpci
Gleb Natapov89acfa32010-05-10 11:36:37 +030012#include "config.h" // CONFIG_*
Kevin O'Connor7d09d0e2010-05-10 21:51:38 -040013#include "biosvar.h" // GET_GLOBAL
14#include "pci_ids.h" // PCI_DEVICE_ID_VIRTIO_BLK
15#include "pci_regs.h" // PCI_VENDOR_ID
Kevin O'Connor72eee3e2010-12-27 19:07:49 -050016#include "boot.h" // boot_add_hd
Gleb Natapov89acfa32010-05-10 11:36:37 +030017#include "virtio-pci.h"
Kevin O'Connor7d09d0e2010-05-10 21:51:38 -040018#include "virtio-ring.h"
Gleb Natapov89acfa32010-05-10 11:36:37 +030019#include "virtio-blk.h"
20#include "disk.h"
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040021#include "string.h" // memset
Gleb Natapov89acfa32010-05-10 11:36:37 +030022
23struct virtiodrive_s {
24 struct drive_s drive;
25 struct vring_virtqueue *vq;
26 u16 ioaddr;
27};
28
29static int
Gleb Natapov5feb83c2010-08-23 10:23:45 +030030virtio_blk_op(struct disk_op_s *op, int write)
Gleb Natapov89acfa32010-05-10 11:36:37 +030031{
32 struct virtiodrive_s *vdrive_g =
33 container_of(op->drive_g, struct virtiodrive_s, drive);
34 struct vring_virtqueue *vq = GET_GLOBAL(vdrive_g->vq);
35 struct virtio_blk_outhdr hdr = {
Gleb Natapov5feb83c2010-08-23 10:23:45 +030036 .type = write ? VIRTIO_BLK_T_OUT : VIRTIO_BLK_T_IN,
Gleb Natapov89acfa32010-05-10 11:36:37 +030037 .ioprio = 0,
38 .sector = op->lba,
39 };
40 u8 status = VIRTIO_BLK_S_UNSUPP;
41 struct vring_list sg[] = {
42 {
Kevin O'Connor7a08ae72012-03-14 21:11:39 -040043 .addr = MAKE_FLATPTR(GET_SEG(SS), &hdr),
44 .length = sizeof(hdr),
Gleb Natapov89acfa32010-05-10 11:36:37 +030045 },
46 {
Kevin O'Connor7a08ae72012-03-14 21:11:39 -040047 .addr = op->buf_fl,
48 .length = GET_GLOBAL(vdrive_g->drive.blksize) * op->count,
Gleb Natapov89acfa32010-05-10 11:36:37 +030049 },
50 {
Kevin O'Connor7a08ae72012-03-14 21:11:39 -040051 .addr = MAKE_FLATPTR(GET_SEG(SS), &status),
52 .length = sizeof(status),
Gleb Natapov89acfa32010-05-10 11:36:37 +030053 },
54 };
55
56 /* Add to virtqueue and kick host */
Gleb Natapov5feb83c2010-08-23 10:23:45 +030057 if (write)
58 vring_add_buf(vq, sg, 2, 1, 0, 0);
59 else
60 vring_add_buf(vq, sg, 1, 2, 0, 0);
Gleb Natapov89acfa32010-05-10 11:36:37 +030061 vring_kick(GET_GLOBAL(vdrive_g->ioaddr), vq, 1);
62
63 /* Wait for reply */
64 while (!vring_more_used(vq))
Kevin O'Connorea8ac632010-05-16 11:34:38 -040065 usleep(5);
Gleb Natapov89acfa32010-05-10 11:36:37 +030066
67 /* Reclaim virtqueue element */
68 vring_get_buf(vq, NULL);
Stefan Hajnoczi4e0daae2010-07-07 13:34:22 +010069
70 /* Clear interrupt status register. Avoid leaving interrupts stuck if
71 * VRING_AVAIL_F_NO_INTERRUPT was ignored and interrupts were raised.
72 */
73 vp_get_isr(GET_GLOBAL(vdrive_g->ioaddr));
74
Gleb Natapov89acfa32010-05-10 11:36:37 +030075 return status == VIRTIO_BLK_S_OK ? DISK_RET_SUCCESS : DISK_RET_EBADTRACK;
76}
77
78int
Paolo Bonzini0e7fb5f2011-11-16 13:02:55 +010079process_virtio_blk_op(struct disk_op_s *op)
Gleb Natapov89acfa32010-05-10 11:36:37 +030080{
Kevin O'Connor897fb112013-02-07 23:32:48 -050081 if (! CONFIG_VIRTIO_BLK)
Kevin O'Connor2515a722010-05-23 10:19:03 -040082 return 0;
Gleb Natapov89acfa32010-05-10 11:36:37 +030083 switch (op->command) {
84 case CMD_READ:
Gleb Natapov5feb83c2010-08-23 10:23:45 +030085 return virtio_blk_op(op, 0);
Gleb Natapov89acfa32010-05-10 11:36:37 +030086 case CMD_WRITE:
Gleb Natapov5feb83c2010-08-23 10:23:45 +030087 return virtio_blk_op(op, 1);
88 case CMD_FORMAT:
Gleb Natapov89acfa32010-05-10 11:36:37 +030089 case CMD_RESET:
90 case CMD_ISREADY:
91 case CMD_VERIFY:
92 case CMD_SEEK:
93 return DISK_RET_SUCCESS;
94 default:
95 op->count = 0;
96 return DISK_RET_EPARAM;
97 }
98}
99
Kevin O'Connorc4fe1352010-05-20 00:22:02 -0400100static void
Kevin O'Connordc3a7d62011-07-09 14:33:56 -0400101init_virtio_blk(struct pci_device *pci)
Kevin O'Connorc4fe1352010-05-20 00:22:02 -0400102{
Kevin O'Connordc3a7d62011-07-09 14:33:56 -0400103 u16 bdf = pci->bdf;
Kevin O'Connorc4fe1352010-05-20 00:22:02 -0400104 dprintf(1, "found virtio-blk at %x:%x\n", pci_bdf_to_bus(bdf),
105 pci_bdf_to_dev(bdf));
Kevin O'Connorc4fe1352010-05-20 00:22:02 -0400106 struct virtiodrive_s *vdrive_g = malloc_fseg(sizeof(*vdrive_g));
Paolo Bonzinie1a17bb2011-11-16 13:02:57 +0100107 if (!vdrive_g) {
Kevin O'Connorc4fe1352010-05-20 00:22:02 -0400108 warn_noalloc();
Paolo Bonzinie1a17bb2011-11-16 13:02:57 +0100109 return;
Kevin O'Connorc4fe1352010-05-20 00:22:02 -0400110 }
111 memset(vdrive_g, 0, sizeof(*vdrive_g));
Paolo Bonzini0e7fb5f2011-11-16 13:02:55 +0100112 vdrive_g->drive.type = DTYPE_VIRTIO_BLK;
Kevin O'Connorc4fe1352010-05-20 00:22:02 -0400113 vdrive_g->drive.cntl_id = bdf;
Kevin O'Connorc4fe1352010-05-20 00:22:02 -0400114
Paolo Bonzini4e343322011-11-16 13:02:56 +0100115 u16 ioaddr = vp_init_simple(bdf);
Kevin O'Connorc4fe1352010-05-20 00:22:02 -0400116 vdrive_g->ioaddr = ioaddr;
Paolo Bonzinie1a17bb2011-11-16 13:02:57 +0100117 if (vp_find_vq(ioaddr, 0, &vdrive_g->vq) < 0 ) {
Kevin O'Connorc4fe1352010-05-20 00:22:02 -0400118 dprintf(1, "fail to find vq for virtio-blk %x:%x\n",
119 pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf));
120 goto fail;
121 }
122
123 struct virtio_blk_config cfg;
124 vp_get(ioaddr, 0, &cfg, sizeof(cfg));
125
126 u32 f = vp_get_features(ioaddr);
127 vdrive_g->drive.blksize = (f & (1 << VIRTIO_BLK_F_BLK_SIZE)) ?
128 cfg.blk_size : DISK_SECTOR_SIZE;
129
130 vdrive_g->drive.sectors = cfg.capacity;
131 dprintf(3, "virtio-blk %x:%x blksize=%d sectors=%u\n",
132 pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf),
133 vdrive_g->drive.blksize, (u32)vdrive_g->drive.sectors);
134
135 if (vdrive_g->drive.blksize != DISK_SECTOR_SIZE) {
136 dprintf(1, "virtio-blk %x:%x block size %d is unsupported\n",
137 pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf),
138 vdrive_g->drive.blksize);
139 goto fail;
140 }
141
142 vdrive_g->drive.pchs.cylinders = cfg.cylinders;
143 vdrive_g->drive.pchs.heads = cfg.heads;
144 vdrive_g->drive.pchs.spt = cfg.sectors;
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500145 char *desc = znprintf(MAXDESCSIZE, "Virtio disk PCI:%x:%x",
146 pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf));
Kevin O'Connorc4fe1352010-05-20 00:22:02 -0400147
Kevin O'Connordc3a7d62011-07-09 14:33:56 -0400148 boot_add_hd(&vdrive_g->drive, desc, bootprio_find_pci_device(pci));
Kevin O'Connorc4fe1352010-05-20 00:22:02 -0400149
Kevin O'Connorc4fe1352010-05-20 00:22:02 -0400150 vp_set_status(ioaddr, VIRTIO_CONFIG_S_ACKNOWLEDGE |
151 VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_DRIVER_OK);
152 return;
153
154fail:
Paolo Bonzinie1a17bb2011-11-16 13:02:57 +0100155 free(vdrive_g->vq);
Kevin O'Connorc4fe1352010-05-20 00:22:02 -0400156 free(vdrive_g);
Kevin O'Connorc4fe1352010-05-20 00:22:02 -0400157}
158
Gleb Natapov89acfa32010-05-10 11:36:37 +0300159void
160virtio_blk_setup(void)
161{
162 ASSERT32FLAT();
Kevin O'Connor897fb112013-02-07 23:32:48 -0500163 if (! CONFIG_VIRTIO_BLK)
Gleb Natapov89acfa32010-05-10 11:36:37 +0300164 return;
165
166 dprintf(3, "init virtio-blk\n");
167
Kevin O'Connor5d74ce02011-06-20 22:22:08 -0400168 struct pci_device *pci;
169 foreachpci(pci) {
170 if (pci->vendor != PCI_VENDOR_ID_REDHAT_QUMRANET
171 || pci->device != PCI_DEVICE_ID_VIRTIO_BLK)
Gleb Natapov89acfa32010-05-10 11:36:37 +0300172 continue;
Kevin O'Connordc3a7d62011-07-09 14:33:56 -0400173 init_virtio_blk(pci);
Gleb Natapov89acfa32010-05-10 11:36:37 +0300174 }
175}