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 |
Kevin O'Connor | 5d74ce0 | 2011-06-20 22:22:08 -0400 | [diff] [blame] | 11 | #include "pci.h" // foreachpci |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 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 |
Kevin O'Connor | 72eee3e | 2010-12-27 19:07:49 -0500 | [diff] [blame] | 16 | #include "boot.h" // boot_add_hd |
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 |
Gleb Natapov | 5feb83c | 2010-08-23 10:23:45 +0300 | [diff] [blame] | 29 | virtio_blk_op(struct disk_op_s *op, int write) |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 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 = { |
Gleb Natapov | 5feb83c | 2010-08-23 10:23:45 +0300 | [diff] [blame] | 35 | .type = write ? VIRTIO_BLK_T_OUT : VIRTIO_BLK_T_IN, |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 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 */ |
Gleb Natapov | 5feb83c | 2010-08-23 10:23:45 +0300 | [diff] [blame] | 56 | if (write) |
| 57 | vring_add_buf(vq, sg, 2, 1, 0, 0); |
| 58 | else |
| 59 | vring_add_buf(vq, sg, 1, 2, 0, 0); |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 60 | vring_kick(GET_GLOBAL(vdrive_g->ioaddr), vq, 1); |
| 61 | |
| 62 | /* Wait for reply */ |
| 63 | while (!vring_more_used(vq)) |
Kevin O'Connor | ea8ac63 | 2010-05-16 11:34:38 -0400 | [diff] [blame] | 64 | usleep(5); |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 65 | |
| 66 | /* Reclaim virtqueue element */ |
| 67 | vring_get_buf(vq, NULL); |
Stefan Hajnoczi | 4e0daae | 2010-07-07 13:34:22 +0100 | [diff] [blame] | 68 | |
| 69 | /* Clear interrupt status register. Avoid leaving interrupts stuck if |
| 70 | * VRING_AVAIL_F_NO_INTERRUPT was ignored and interrupts were raised. |
| 71 | */ |
| 72 | vp_get_isr(GET_GLOBAL(vdrive_g->ioaddr)); |
| 73 | |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 74 | return status == VIRTIO_BLK_S_OK ? DISK_RET_SUCCESS : DISK_RET_EBADTRACK; |
| 75 | } |
| 76 | |
| 77 | int |
Paolo Bonzini | 0e7fb5f | 2011-11-16 13:02:55 +0100 | [diff] [blame^] | 78 | process_virtio_blk_op(struct disk_op_s *op) |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 79 | { |
Kevin O'Connor | 2515a72 | 2010-05-23 10:19:03 -0400 | [diff] [blame] | 80 | if (! CONFIG_VIRTIO_BLK || CONFIG_COREBOOT) |
| 81 | return 0; |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 82 | switch (op->command) { |
| 83 | case CMD_READ: |
Gleb Natapov | 5feb83c | 2010-08-23 10:23:45 +0300 | [diff] [blame] | 84 | return virtio_blk_op(op, 0); |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 85 | case CMD_WRITE: |
Gleb Natapov | 5feb83c | 2010-08-23 10:23:45 +0300 | [diff] [blame] | 86 | return virtio_blk_op(op, 1); |
| 87 | case CMD_FORMAT: |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 88 | case CMD_RESET: |
| 89 | case CMD_ISREADY: |
| 90 | case CMD_VERIFY: |
| 91 | case CMD_SEEK: |
| 92 | return DISK_RET_SUCCESS; |
| 93 | default: |
| 94 | op->count = 0; |
| 95 | return DISK_RET_EPARAM; |
| 96 | } |
| 97 | } |
| 98 | |
Kevin O'Connor | c4fe135 | 2010-05-20 00:22:02 -0400 | [diff] [blame] | 99 | static void |
Kevin O'Connor | dc3a7d6 | 2011-07-09 14:33:56 -0400 | [diff] [blame] | 100 | init_virtio_blk(struct pci_device *pci) |
Kevin O'Connor | c4fe135 | 2010-05-20 00:22:02 -0400 | [diff] [blame] | 101 | { |
Kevin O'Connor | dc3a7d6 | 2011-07-09 14:33:56 -0400 | [diff] [blame] | 102 | u16 bdf = pci->bdf; |
Kevin O'Connor | c4fe135 | 2010-05-20 00:22:02 -0400 | [diff] [blame] | 103 | dprintf(1, "found virtio-blk at %x:%x\n", pci_bdf_to_bus(bdf), |
| 104 | pci_bdf_to_dev(bdf)); |
Kevin O'Connor | c4fe135 | 2010-05-20 00:22:02 -0400 | [diff] [blame] | 105 | struct virtiodrive_s *vdrive_g = malloc_fseg(sizeof(*vdrive_g)); |
| 106 | struct vring_virtqueue *vq = memalign_low(PAGE_SIZE, sizeof(*vq)); |
Kevin O'Connor | ca2bc1c | 2010-12-29 21:41:19 -0500 | [diff] [blame] | 107 | if (!vdrive_g || !vq) { |
Kevin O'Connor | c4fe135 | 2010-05-20 00:22:02 -0400 | [diff] [blame] | 108 | warn_noalloc(); |
| 109 | goto fail; |
| 110 | } |
| 111 | memset(vdrive_g, 0, sizeof(*vdrive_g)); |
Gleb Natapov | 0f67397 | 2010-09-15 18:31:44 +0200 | [diff] [blame] | 112 | memset(vq, 0, sizeof(*vq)); |
Paolo Bonzini | 0e7fb5f | 2011-11-16 13:02:55 +0100 | [diff] [blame^] | 113 | vdrive_g->drive.type = DTYPE_VIRTIO_BLK; |
Kevin O'Connor | c4fe135 | 2010-05-20 00:22:02 -0400 | [diff] [blame] | 114 | vdrive_g->drive.cntl_id = bdf; |
| 115 | vdrive_g->vq = vq; |
| 116 | |
| 117 | u16 ioaddr = pci_config_readl(bdf, PCI_BASE_ADDRESS_0) & |
| 118 | PCI_BASE_ADDRESS_IO_MASK; |
| 119 | |
| 120 | vdrive_g->ioaddr = ioaddr; |
| 121 | |
| 122 | vp_reset(ioaddr); |
| 123 | vp_set_status(ioaddr, VIRTIO_CONFIG_S_ACKNOWLEDGE | |
| 124 | VIRTIO_CONFIG_S_DRIVER ); |
| 125 | |
| 126 | if (vp_find_vq(ioaddr, 0, vdrive_g->vq) < 0 ) { |
| 127 | dprintf(1, "fail to find vq for virtio-blk %x:%x\n", |
| 128 | pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)); |
| 129 | goto fail; |
| 130 | } |
| 131 | |
| 132 | struct virtio_blk_config cfg; |
| 133 | vp_get(ioaddr, 0, &cfg, sizeof(cfg)); |
| 134 | |
| 135 | u32 f = vp_get_features(ioaddr); |
| 136 | vdrive_g->drive.blksize = (f & (1 << VIRTIO_BLK_F_BLK_SIZE)) ? |
| 137 | cfg.blk_size : DISK_SECTOR_SIZE; |
| 138 | |
| 139 | vdrive_g->drive.sectors = cfg.capacity; |
| 140 | dprintf(3, "virtio-blk %x:%x blksize=%d sectors=%u\n", |
| 141 | pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), |
| 142 | vdrive_g->drive.blksize, (u32)vdrive_g->drive.sectors); |
| 143 | |
| 144 | if (vdrive_g->drive.blksize != DISK_SECTOR_SIZE) { |
| 145 | dprintf(1, "virtio-blk %x:%x block size %d is unsupported\n", |
| 146 | pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), |
| 147 | vdrive_g->drive.blksize); |
| 148 | goto fail; |
| 149 | } |
| 150 | |
| 151 | vdrive_g->drive.pchs.cylinders = cfg.cylinders; |
| 152 | vdrive_g->drive.pchs.heads = cfg.heads; |
| 153 | vdrive_g->drive.pchs.spt = cfg.sectors; |
Kevin O'Connor | ca2bc1c | 2010-12-29 21:41:19 -0500 | [diff] [blame] | 154 | char *desc = znprintf(MAXDESCSIZE, "Virtio disk PCI:%x:%x", |
| 155 | pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)); |
Kevin O'Connor | c4fe135 | 2010-05-20 00:22:02 -0400 | [diff] [blame] | 156 | |
Kevin O'Connor | dc3a7d6 | 2011-07-09 14:33:56 -0400 | [diff] [blame] | 157 | boot_add_hd(&vdrive_g->drive, desc, bootprio_find_pci_device(pci)); |
Kevin O'Connor | c4fe135 | 2010-05-20 00:22:02 -0400 | [diff] [blame] | 158 | |
Kevin O'Connor | c4fe135 | 2010-05-20 00:22:02 -0400 | [diff] [blame] | 159 | vp_set_status(ioaddr, VIRTIO_CONFIG_S_ACKNOWLEDGE | |
| 160 | VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_DRIVER_OK); |
| 161 | return; |
| 162 | |
| 163 | fail: |
| 164 | free(vdrive_g); |
Kevin O'Connor | c4fe135 | 2010-05-20 00:22:02 -0400 | [diff] [blame] | 165 | free(vq); |
| 166 | } |
| 167 | |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 168 | void |
| 169 | virtio_blk_setup(void) |
| 170 | { |
| 171 | ASSERT32FLAT(); |
Kevin O'Connor | 2515a72 | 2010-05-23 10:19:03 -0400 | [diff] [blame] | 172 | if (! CONFIG_VIRTIO_BLK || CONFIG_COREBOOT) |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 173 | return; |
| 174 | |
| 175 | dprintf(3, "init virtio-blk\n"); |
| 176 | |
Kevin O'Connor | 5d74ce0 | 2011-06-20 22:22:08 -0400 | [diff] [blame] | 177 | struct pci_device *pci; |
| 178 | foreachpci(pci) { |
| 179 | if (pci->vendor != PCI_VENDOR_ID_REDHAT_QUMRANET |
| 180 | || pci->device != PCI_DEVICE_ID_VIRTIO_BLK) |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 181 | continue; |
Kevin O'Connor | dc3a7d6 | 2011-07-09 14:33:56 -0400 | [diff] [blame] | 182 | init_virtio_blk(pci); |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 183 | } |
| 184 | } |