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 | |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 10 | #include "biosvar.h" // GET_GLOBALFLAT |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 11 | #include "config.h" // CONFIG_* |
Kevin O'Connor | 135f3f6 | 2013-09-14 23:57:26 -0400 | [diff] [blame] | 12 | #include "block.h" // struct drive_s |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 13 | #include "malloc.h" // free |
| 14 | #include "output.h" // dprintf |
| 15 | #include "pci.h" // foreachpci |
Kevin O'Connor | 7d09d0e | 2010-05-10 21:51:38 -0400 | [diff] [blame] | 16 | #include "pci_ids.h" // PCI_DEVICE_ID_VIRTIO_BLK |
| 17 | #include "pci_regs.h" // PCI_VENDOR_ID |
Kevin O'Connor | 135f3f6 | 2013-09-14 23:57:26 -0400 | [diff] [blame] | 18 | #include "std/disk.h" // DISK_RET_SUCCESS |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 19 | #include "string.h" // memset |
| 20 | #include "util.h" // usleep |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 21 | #include "virtio-pci.h" |
Kevin O'Connor | 7d09d0e | 2010-05-10 21:51:38 -0400 | [diff] [blame] | 22 | #include "virtio-ring.h" |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 23 | #include "virtio-blk.h" |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 24 | |
| 25 | struct virtiodrive_s { |
| 26 | struct drive_s drive; |
| 27 | struct vring_virtqueue *vq; |
Gerd Hoffmann | daf5cc9 | 2015-06-25 09:36:16 +0200 | [diff] [blame] | 28 | struct vp_device vp; |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | static int |
Gleb Natapov | 5feb83c | 2010-08-23 10:23:45 +0300 | [diff] [blame] | 32 | virtio_blk_op(struct disk_op_s *op, int write) |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 33 | { |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 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); |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 37 | struct virtio_blk_outhdr hdr = { |
Gleb Natapov | 5feb83c | 2010-08-23 10:23:45 +0300 | [diff] [blame] | 38 | .type = write ? VIRTIO_BLK_T_OUT : VIRTIO_BLK_T_IN, |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 39 | .ioprio = 0, |
| 40 | .sector = op->lba, |
| 41 | }; |
| 42 | u8 status = VIRTIO_BLK_S_UNSUPP; |
| 43 | struct vring_list sg[] = { |
| 44 | { |
Kevin O'Connor | 7a08ae7 | 2012-03-14 21:11:39 -0400 | [diff] [blame] | 45 | .addr = MAKE_FLATPTR(GET_SEG(SS), &hdr), |
| 46 | .length = sizeof(hdr), |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 47 | }, |
| 48 | { |
Kevin O'Connor | 7a08ae7 | 2012-03-14 21:11:39 -0400 | [diff] [blame] | 49 | .addr = op->buf_fl, |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 50 | .length = GET_GLOBALFLAT(vdrive_gf->drive.blksize) * op->count, |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 51 | }, |
| 52 | { |
Kevin O'Connor | 7a08ae7 | 2012-03-14 21:11:39 -0400 | [diff] [blame] | 53 | .addr = MAKE_FLATPTR(GET_SEG(SS), &status), |
| 54 | .length = sizeof(status), |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 55 | }, |
| 56 | }; |
| 57 | |
| 58 | /* Add to virtqueue and kick host */ |
Gleb Natapov | 5feb83c | 2010-08-23 10:23:45 +0300 | [diff] [blame] | 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); |
Gerd Hoffmann | daf5cc9 | 2015-06-25 09:36:16 +0200 | [diff] [blame] | 63 | vring_kick(&vdrive_gf->vp, vq, 1); |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 64 | |
| 65 | /* Wait for reply */ |
| 66 | while (!vring_more_used(vq)) |
Kevin O'Connor | ea8ac63 | 2010-05-16 11:34:38 -0400 | [diff] [blame] | 67 | usleep(5); |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 68 | |
| 69 | /* Reclaim virtqueue element */ |
| 70 | vring_get_buf(vq, NULL); |
Stefan Hajnoczi | 4e0daae | 2010-07-07 13:34:22 +0100 | [diff] [blame] | 71 | |
| 72 | /* Clear interrupt status register. Avoid leaving interrupts stuck if |
| 73 | * VRING_AVAIL_F_NO_INTERRUPT was ignored and interrupts were raised. |
| 74 | */ |
Gerd Hoffmann | daf5cc9 | 2015-06-25 09:36:16 +0200 | [diff] [blame] | 75 | vp_get_isr(&vdrive_gf->vp); |
Stefan Hajnoczi | 4e0daae | 2010-07-07 13:34:22 +0100 | [diff] [blame] | 76 | |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 77 | return status == VIRTIO_BLK_S_OK ? DISK_RET_SUCCESS : DISK_RET_EBADTRACK; |
| 78 | } |
| 79 | |
Gerd Hoffmann | 1d9e87b | 2015-06-26 09:44:00 +0200 | [diff] [blame] | 80 | int VISIBLE32FLAT |
Paolo Bonzini | 0e7fb5f | 2011-11-16 13:02:55 +0100 | [diff] [blame] | 81 | process_virtio_blk_op(struct disk_op_s *op) |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 82 | { |
Kevin O'Connor | 897fb11 | 2013-02-07 23:32:48 -0500 | [diff] [blame] | 83 | if (! CONFIG_VIRTIO_BLK) |
Kevin O'Connor | 2515a72 | 2010-05-23 10:19:03 -0400 | [diff] [blame] | 84 | return 0; |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 85 | switch (op->command) { |
| 86 | case CMD_READ: |
Gleb Natapov | 5feb83c | 2010-08-23 10:23:45 +0300 | [diff] [blame] | 87 | return virtio_blk_op(op, 0); |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 88 | case CMD_WRITE: |
Gleb Natapov | 5feb83c | 2010-08-23 10:23:45 +0300 | [diff] [blame] | 89 | return virtio_blk_op(op, 1); |
| 90 | case CMD_FORMAT: |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 91 | case CMD_RESET: |
| 92 | case CMD_ISREADY: |
| 93 | case CMD_VERIFY: |
| 94 | case CMD_SEEK: |
| 95 | return DISK_RET_SUCCESS; |
| 96 | default: |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 97 | return DISK_RET_EPARAM; |
| 98 | } |
| 99 | } |
| 100 | |
Kevin O'Connor | c4fe135 | 2010-05-20 00:22:02 -0400 | [diff] [blame] | 101 | static void |
Kevin O'Connor | dc3a7d6 | 2011-07-09 14:33:56 -0400 | [diff] [blame] | 102 | init_virtio_blk(struct pci_device *pci) |
Kevin O'Connor | c4fe135 | 2010-05-20 00:22:02 -0400 | [diff] [blame] | 103 | { |
Kevin O'Connor | dc3a7d6 | 2011-07-09 14:33:56 -0400 | [diff] [blame] | 104 | u16 bdf = pci->bdf; |
Kevin O'Connor | c4fe135 | 2010-05-20 00:22:02 -0400 | [diff] [blame] | 105 | dprintf(1, "found virtio-blk at %x:%x\n", pci_bdf_to_bus(bdf), |
| 106 | pci_bdf_to_dev(bdf)); |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 107 | struct virtiodrive_s *vdrive = malloc_fseg(sizeof(*vdrive)); |
| 108 | if (!vdrive) { |
Kevin O'Connor | c4fe135 | 2010-05-20 00:22:02 -0400 | [diff] [blame] | 109 | warn_noalloc(); |
Paolo Bonzini | e1a17bb | 2011-11-16 13:02:57 +0100 | [diff] [blame] | 110 | return; |
Kevin O'Connor | c4fe135 | 2010-05-20 00:22:02 -0400 | [diff] [blame] | 111 | } |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 112 | memset(vdrive, 0, sizeof(*vdrive)); |
| 113 | vdrive->drive.type = DTYPE_VIRTIO_BLK; |
| 114 | vdrive->drive.cntl_id = bdf; |
Kevin O'Connor | c4fe135 | 2010-05-20 00:22:02 -0400 | [diff] [blame] | 115 | |
Gerd Hoffmann | 74d0cdc | 2015-06-25 10:24:27 +0200 | [diff] [blame] | 116 | vp_init_simple(&vdrive->vp, pci); |
Gerd Hoffmann | daf5cc9 | 2015-06-25 09:36:16 +0200 | [diff] [blame] | 117 | if (vp_find_vq(&vdrive->vp, 0, &vdrive->vq) < 0 ) { |
Kevin O'Connor | c4fe135 | 2010-05-20 00:22:02 -0400 | [diff] [blame] | 118 | 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; |
Gerd Hoffmann | daf5cc9 | 2015-06-25 09:36:16 +0200 | [diff] [blame] | 124 | vp_get(&vdrive->vp, 0, &cfg, sizeof(cfg)); |
Kevin O'Connor | c4fe135 | 2010-05-20 00:22:02 -0400 | [diff] [blame] | 125 | |
Gerd Hoffmann | 46d1792 | 2015-06-25 12:28:46 +0200 | [diff] [blame^] | 126 | u64 f = vp_get_features(&vdrive->vp); |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 127 | vdrive->drive.blksize = (f & (1 << VIRTIO_BLK_F_BLK_SIZE)) ? |
Kevin O'Connor | c4fe135 | 2010-05-20 00:22:02 -0400 | [diff] [blame] | 128 | cfg.blk_size : DISK_SECTOR_SIZE; |
| 129 | |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 130 | vdrive->drive.sectors = cfg.capacity; |
Kevin O'Connor | c4fe135 | 2010-05-20 00:22:02 -0400 | [diff] [blame] | 131 | dprintf(3, "virtio-blk %x:%x blksize=%d sectors=%u\n", |
| 132 | pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 133 | vdrive->drive.blksize, (u32)vdrive->drive.sectors); |
Kevin O'Connor | c4fe135 | 2010-05-20 00:22:02 -0400 | [diff] [blame] | 134 | |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 135 | if (vdrive->drive.blksize != DISK_SECTOR_SIZE) { |
Kevin O'Connor | c4fe135 | 2010-05-20 00:22:02 -0400 | [diff] [blame] | 136 | dprintf(1, "virtio-blk %x:%x block size %d is unsupported\n", |
| 137 | pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 138 | vdrive->drive.blksize); |
Kevin O'Connor | c4fe135 | 2010-05-20 00:22:02 -0400 | [diff] [blame] | 139 | goto fail; |
| 140 | } |
| 141 | |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 142 | vdrive->drive.pchs.cylinder = cfg.cylinders; |
| 143 | vdrive->drive.pchs.head = cfg.heads; |
| 144 | vdrive->drive.pchs.sector = cfg.sectors; |
Kevin O'Connor | ca2bc1c | 2010-12-29 21:41:19 -0500 | [diff] [blame] | 145 | char *desc = znprintf(MAXDESCSIZE, "Virtio disk PCI:%x:%x", |
| 146 | pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf)); |
Kevin O'Connor | c4fe135 | 2010-05-20 00:22:02 -0400 | [diff] [blame] | 147 | |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 148 | boot_add_hd(&vdrive->drive, desc, bootprio_find_pci_device(pci)); |
Kevin O'Connor | c4fe135 | 2010-05-20 00:22:02 -0400 | [diff] [blame] | 149 | |
Gerd Hoffmann | daf5cc9 | 2015-06-25 09:36:16 +0200 | [diff] [blame] | 150 | vp_set_status(&vdrive->vp, VIRTIO_CONFIG_S_ACKNOWLEDGE | |
Kevin O'Connor | c4fe135 | 2010-05-20 00:22:02 -0400 | [diff] [blame] | 151 | VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_DRIVER_OK); |
| 152 | return; |
| 153 | |
| 154 | fail: |
Gerd Hoffmann | daf5cc9 | 2015-06-25 09:36:16 +0200 | [diff] [blame] | 155 | vp_reset(&vdrive->vp); |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 156 | free(vdrive->vq); |
| 157 | free(vdrive); |
Kevin O'Connor | c4fe135 | 2010-05-20 00:22:02 -0400 | [diff] [blame] | 158 | } |
| 159 | |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 160 | void |
| 161 | virtio_blk_setup(void) |
| 162 | { |
| 163 | ASSERT32FLAT(); |
Kevin O'Connor | 897fb11 | 2013-02-07 23:32:48 -0500 | [diff] [blame] | 164 | if (! CONFIG_VIRTIO_BLK) |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 165 | return; |
| 166 | |
| 167 | dprintf(3, "init virtio-blk\n"); |
| 168 | |
Kevin O'Connor | 5d74ce0 | 2011-06-20 22:22:08 -0400 | [diff] [blame] | 169 | struct pci_device *pci; |
| 170 | foreachpci(pci) { |
| 171 | if (pci->vendor != PCI_VENDOR_ID_REDHAT_QUMRANET |
| 172 | || pci->device != PCI_DEVICE_ID_VIRTIO_BLK) |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 173 | continue; |
Kevin O'Connor | dc3a7d6 | 2011-07-09 14:33:56 -0400 | [diff] [blame] | 174 | init_virtio_blk(pci); |
Gleb Natapov | 89acfa3 | 2010-05-10 11:36:37 +0300 | [diff] [blame] | 175 | } |
| 176 | } |