Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 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_* |
Kevin O'Connor | 7d09d0e | 2010-05-10 21:51:38 -0400 | [diff] [blame] | 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 |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 17 | #include "virtio-pci.h" |
Kevin O'Connor | 7d09d0e | 2010-05-10 21:51:38 -0400 | [diff] [blame] | 18 | #include "virtio-ring.h" |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 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)) |
Kevin O'Connor | ea8ac63 | 2010-05-16 11:34:38 -0400 | [diff] [blame^] | 61 | usleep(5); |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 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 | switch (op->command) { |
| 72 | case CMD_READ: |
| 73 | return virtio_blk_read(op); |
| 74 | case CMD_FORMAT: |
| 75 | case CMD_WRITE: |
| 76 | return DISK_RET_EWRITEPROTECT; |
| 77 | case CMD_RESET: |
| 78 | case CMD_ISREADY: |
| 79 | case CMD_VERIFY: |
| 80 | case CMD_SEEK: |
| 81 | return DISK_RET_SUCCESS; |
| 82 | default: |
| 83 | op->count = 0; |
| 84 | return DISK_RET_EPARAM; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | virtio_blk_setup(void) |
| 90 | { |
| 91 | ASSERT32FLAT(); |
| 92 | if (! CONFIG_VIRTIO_BLK) |
| 93 | return; |
| 94 | |
| 95 | dprintf(3, "init virtio-blk\n"); |
| 96 | |
| 97 | int bdf, max; |
| 98 | u32 id = PCI_VENDOR_ID_REDHAT_QUMRANET | (PCI_DEVICE_ID_VIRTIO_BLK << 16); |
| 99 | foreachpci(bdf, max) { |
| 100 | u32 v = pci_config_readl(bdf, PCI_VENDOR_ID); |
| 101 | if (v != id) |
| 102 | continue; |
| 103 | dprintf(3, "found virtio-blk at %x:%x\n", pci_bdf_to_bus(bdf), |
| 104 | pci_bdf_to_dev(bdf)); |
| 105 | char *desc = malloc_tmphigh(MAXDESCSIZE); |
| 106 | struct virtiodrive_s *vdrive_g = malloc_fseg(sizeof(*vdrive_g)); |
Kevin O'Connor | ea8ac63 | 2010-05-16 11:34:38 -0400 | [diff] [blame^] | 107 | struct vring_virtqueue *vq = memalign_low(PAGE_SIZE, sizeof(*vq)); |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 108 | if (!vdrive_g || !desc || !vq) { |
| 109 | free(vdrive_g); |
| 110 | free(desc); |
| 111 | free(vq); |
| 112 | warn_noalloc(); |
| 113 | return; |
| 114 | } |
| 115 | memset(vdrive_g, 0, sizeof(*vdrive_g)); |
| 116 | vdrive_g->drive.type = DTYPE_VIRTIO; |
| 117 | vdrive_g->drive.cntl_id = bdf; |
| 118 | vdrive_g->vq = vq; |
| 119 | |
| 120 | u16 ioaddr = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) & |
| 121 | PCI_BASE_ADDRESS_IO_MASK; |
| 122 | |
| 123 | vdrive_g->ioaddr = ioaddr; |
| 124 | |
| 125 | vp_reset(ioaddr); |
| 126 | vp_set_status(ioaddr, VIRTIO_CONFIG_S_ACKNOWLEDGE | |
| 127 | VIRTIO_CONFIG_S_DRIVER ); |
| 128 | |
| 129 | if (vp_find_vq(ioaddr, 0, vdrive_g->vq) < 0 ) { |
| 130 | free(vdrive_g); |
| 131 | free(desc); |
| 132 | free(vq); |
| 133 | dprintf(1, "fail to find vq for virtio-blk %x:%x\n", |
Kevin O'Connor | ea8ac63 | 2010-05-16 11:34:38 -0400 | [diff] [blame^] | 134 | pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)); |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 135 | continue; |
| 136 | } |
| 137 | |
| 138 | struct virtio_blk_config cfg; |
| 139 | vp_get(ioaddr, 0, &cfg, sizeof(cfg)); |
| 140 | |
| 141 | vdrive_g->drive.blksize = cfg.blk_size; |
| 142 | vdrive_g->drive.sectors = cfg.capacity; |
| 143 | dprintf(3, "virtio-blk %x:%x blksize=%d sectors=%u\n", |
Kevin O'Connor | ea8ac63 | 2010-05-16 11:34:38 -0400 | [diff] [blame^] | 144 | pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 145 | vdrive_g->drive.blksize, (u32)vdrive_g->drive.sectors); |
| 146 | |
| 147 | vdrive_g->drive.pchs.cylinders = cfg.cylinders; |
| 148 | vdrive_g->drive.pchs.heads = cfg.heads; |
| 149 | vdrive_g->drive.pchs.spt = cfg.sectors; |
| 150 | |
| 151 | setup_translation(&vdrive_g->drive); |
| 152 | add_bcv_internal(&vdrive_g->drive); |
| 153 | |
| 154 | snprintf(desc, MAXDESCSIZE, "Virtio disk PCI:%x:%x", |
| 155 | pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)); |
| 156 | |
| 157 | vdrive_g->drive.desc = desc; |
| 158 | |
| 159 | vp_set_status(ioaddr, VIRTIO_CONFIG_S_ACKNOWLEDGE | |
| 160 | VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_DRIVER_OK); |
| 161 | } |
| 162 | } |