Tuomas Tynkkynen | 2b3b9e9 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2018, Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi> |
| 4 | * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com> |
| 5 | */ |
| 6 | |
Simon Glass | 1ffa43c | 2023-01-28 15:00:17 -0700 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_VIRTIO |
| 8 | |
Tuomas Tynkkynen | 2b3b9e9 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 9 | #include <blk.h> |
| 10 | #include <dm.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 11 | #include <part.h> |
Tuomas Tynkkynen | 2b3b9e9 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 12 | #include <virtio_types.h> |
| 13 | #include <virtio.h> |
| 14 | #include <virtio_ring.h> |
| 15 | #include "virtio_blk.h" |
| 16 | |
| 17 | struct virtio_blk_priv { |
| 18 | struct virtqueue *vq; |
| 19 | }; |
| 20 | |
Dmitrii Merkurev | 4c0d8ec | 2024-03-06 18:59:18 +0000 | [diff] [blame] | 21 | static const u32 feature[] = { |
| 22 | VIRTIO_BLK_F_WRITE_ZEROES |
| 23 | }; |
| 24 | |
| 25 | static void virtio_blk_init_header_sg(struct udevice *dev, u64 sector, u32 type, |
| 26 | struct virtio_blk_outhdr *out_hdr, struct virtio_sg *sg) |
| 27 | { |
| 28 | const bool sector_is_needed = type == VIRTIO_BLK_T_IN || |
| 29 | type == VIRTIO_BLK_T_OUT; |
| 30 | |
| 31 | out_hdr->type = cpu_to_virtio32(dev, type); |
| 32 | out_hdr->sector = cpu_to_virtio64(dev, sector_is_needed ? sector : 0); |
| 33 | |
| 34 | sg->addr = out_hdr; |
| 35 | sg->length = sizeof(*out_hdr); |
| 36 | } |
| 37 | |
| 38 | static void virtio_blk_init_write_zeroes_sg(struct udevice *dev, u64 sector, lbaint_t blkcnt, |
| 39 | struct virtio_blk_discard_write_zeroes *wz, |
| 40 | struct virtio_sg *sg) |
| 41 | { |
| 42 | wz->sector = cpu_to_virtio64(dev, sector); |
| 43 | wz->num_sectors = cpu_to_virtio32(dev, blkcnt); |
| 44 | wz->flags = cpu_to_virtio32(dev, 0); |
| 45 | |
| 46 | sg->addr = wz; |
| 47 | sg->length = sizeof(*wz); |
| 48 | } |
| 49 | |
| 50 | static void virtio_blk_init_status_sg(u8 *status, struct virtio_sg *sg) |
| 51 | { |
| 52 | sg->addr = status; |
| 53 | sg->length = sizeof(*status); |
| 54 | } |
| 55 | |
| 56 | static void virtio_blk_init_data_sg(void *buffer, lbaint_t blkcnt, struct virtio_sg *sg) |
| 57 | { |
| 58 | sg->addr = buffer; |
| 59 | sg->length = blkcnt * 512; |
| 60 | } |
| 61 | |
Tuomas Tynkkynen | 2b3b9e9 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 62 | static ulong virtio_blk_do_req(struct udevice *dev, u64 sector, |
| 63 | lbaint_t blkcnt, void *buffer, u32 type) |
| 64 | { |
| 65 | struct virtio_blk_priv *priv = dev_get_priv(dev); |
Dmitrii Merkurev | 4c0d8ec | 2024-03-06 18:59:18 +0000 | [diff] [blame] | 66 | struct virtio_blk_outhdr out_hdr; |
| 67 | struct virtio_blk_discard_write_zeroes wz_hdr; |
Tuomas Tynkkynen | 2b3b9e9 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 68 | unsigned int num_out = 0, num_in = 0; |
Dmitrii Merkurev | 4c0d8ec | 2024-03-06 18:59:18 +0000 | [diff] [blame] | 69 | struct virtio_sg hdr_sg, wz_sg, data_sg, status_sg; |
Tuomas Tynkkynen | 2b3b9e9 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 70 | struct virtio_sg *sgs[3]; |
| 71 | u8 status; |
| 72 | int ret; |
| 73 | |
Dmitrii Merkurev | 4c0d8ec | 2024-03-06 18:59:18 +0000 | [diff] [blame] | 74 | virtio_blk_init_header_sg(dev, sector, type, &out_hdr, &hdr_sg); |
Tuomas Tynkkynen | 2b3b9e9 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 75 | sgs[num_out++] = &hdr_sg; |
| 76 | |
Dmitrii Merkurev | 4c0d8ec | 2024-03-06 18:59:18 +0000 | [diff] [blame] | 77 | switch (type) { |
| 78 | case VIRTIO_BLK_T_IN: |
| 79 | case VIRTIO_BLK_T_OUT: |
| 80 | virtio_blk_init_data_sg(buffer, blkcnt, &data_sg); |
| 81 | if (type & VIRTIO_BLK_T_OUT) |
| 82 | sgs[num_out++] = &data_sg; |
| 83 | else |
| 84 | sgs[num_out + num_in++] = &data_sg; |
| 85 | break; |
Tuomas Tynkkynen | 2b3b9e9 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 86 | |
Dmitrii Merkurev | 4c0d8ec | 2024-03-06 18:59:18 +0000 | [diff] [blame] | 87 | case VIRTIO_BLK_T_WRITE_ZEROES: |
| 88 | virtio_blk_init_write_zeroes_sg(dev, sector, blkcnt, &wz_hdr, &wz_sg); |
| 89 | sgs[num_out++] = &wz_sg; |
| 90 | break; |
| 91 | |
| 92 | default: |
| 93 | return -EINVAL; |
| 94 | } |
| 95 | |
| 96 | virtio_blk_init_status_sg(&status, &status_sg); |
Tuomas Tynkkynen | 2b3b9e9 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 97 | sgs[num_out + num_in++] = &status_sg; |
Simon Glass | 1ffa43c | 2023-01-28 15:00:17 -0700 | [diff] [blame] | 98 | log_debug("dev=%s, active=%d, priv=%p, priv->vq=%p\n", dev->name, |
| 99 | device_active(dev), priv, priv->vq); |
Tuomas Tynkkynen | 2b3b9e9 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 100 | |
| 101 | ret = virtqueue_add(priv->vq, sgs, num_out, num_in); |
| 102 | if (ret) |
| 103 | return ret; |
| 104 | |
| 105 | virtqueue_kick(priv->vq); |
| 106 | |
Simon Glass | 1ffa43c | 2023-01-28 15:00:17 -0700 | [diff] [blame] | 107 | log_debug("wait..."); |
Tuomas Tynkkynen | 2b3b9e9 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 108 | while (!virtqueue_get_buf(priv->vq, NULL)) |
| 109 | ; |
Simon Glass | 1ffa43c | 2023-01-28 15:00:17 -0700 | [diff] [blame] | 110 | log_debug("done\n"); |
Tuomas Tynkkynen | 2b3b9e9 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 111 | |
| 112 | return status == VIRTIO_BLK_S_OK ? blkcnt : -EIO; |
| 113 | } |
| 114 | |
| 115 | static ulong virtio_blk_read(struct udevice *dev, lbaint_t start, |
| 116 | lbaint_t blkcnt, void *buffer) |
| 117 | { |
Simon Glass | 1ffa43c | 2023-01-28 15:00:17 -0700 | [diff] [blame] | 118 | log_debug("read %s\n", dev->name); |
Tuomas Tynkkynen | 2b3b9e9 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 119 | return virtio_blk_do_req(dev, start, blkcnt, buffer, |
| 120 | VIRTIO_BLK_T_IN); |
| 121 | } |
| 122 | |
| 123 | static ulong virtio_blk_write(struct udevice *dev, lbaint_t start, |
| 124 | lbaint_t blkcnt, const void *buffer) |
| 125 | { |
| 126 | return virtio_blk_do_req(dev, start, blkcnt, (void *)buffer, |
| 127 | VIRTIO_BLK_T_OUT); |
| 128 | } |
| 129 | |
Dmitrii Merkurev | 4c0d8ec | 2024-03-06 18:59:18 +0000 | [diff] [blame] | 130 | static ulong virtio_blk_erase(struct udevice *dev, lbaint_t start, |
| 131 | lbaint_t blkcnt) |
| 132 | { |
| 133 | if (!virtio_has_feature(dev, VIRTIO_BLK_F_WRITE_ZEROES)) |
| 134 | return -EOPNOTSUPP; |
| 135 | |
| 136 | return virtio_blk_do_req(dev, start, blkcnt, NULL, VIRTIO_BLK_T_WRITE_ZEROES); |
| 137 | } |
| 138 | |
Tuomas Tynkkynen | 2b3b9e9 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 139 | static int virtio_blk_bind(struct udevice *dev) |
| 140 | { |
| 141 | struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent); |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 142 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Tuomas Tynkkynen | 2b3b9e9 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 143 | int devnum; |
| 144 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 145 | desc->uclass_id = UCLASS_VIRTIO; |
Tuomas Tynkkynen | 2b3b9e9 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 146 | /* |
| 147 | * Initialize the devnum to -ENODEV. This is to make sure that |
| 148 | * blk_next_free_devnum() works as expected, since the default |
| 149 | * value 0 is a valid devnum. |
| 150 | */ |
| 151 | desc->devnum = -ENODEV; |
Simon Glass | dbfa32c | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 152 | devnum = blk_next_free_devnum(UCLASS_VIRTIO); |
Tuomas Tynkkynen | 2b3b9e9 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 153 | if (devnum < 0) |
| 154 | return devnum; |
| 155 | desc->devnum = devnum; |
| 156 | desc->part_type = PART_TYPE_UNKNOWN; |
| 157 | /* |
| 158 | * virtio mmio transport supplies string identification for us, |
| 159 | * while pci trnasport uses a 2-byte subvendor value. |
| 160 | */ |
| 161 | if (uc_priv->vendor >> 16) |
| 162 | sprintf(desc->vendor, "%s", (char *)&uc_priv->vendor); |
| 163 | else |
| 164 | sprintf(desc->vendor, "%04x", uc_priv->vendor); |
| 165 | desc->bdev = dev; |
| 166 | |
| 167 | /* Indicate what driver features we support */ |
Dmitrii Merkurev | 4c0d8ec | 2024-03-06 18:59:18 +0000 | [diff] [blame] | 168 | virtio_driver_features_init(uc_priv, feature, ARRAY_SIZE(feature), |
| 169 | NULL, 0); |
Tuomas Tynkkynen | 2b3b9e9 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static int virtio_blk_probe(struct udevice *dev) |
| 175 | { |
| 176 | struct virtio_blk_priv *priv = dev_get_priv(dev); |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 177 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Tuomas Tynkkynen | 2b3b9e9 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 178 | u64 cap; |
| 179 | int ret; |
| 180 | |
| 181 | ret = virtio_find_vqs(dev, 1, &priv->vq); |
| 182 | if (ret) |
| 183 | return ret; |
| 184 | |
| 185 | desc->blksz = 512; |
AKASHI Takahiro | 16630a8 | 2020-08-19 18:07:32 +0900 | [diff] [blame] | 186 | desc->log2blksz = 9; |
Tuomas Tynkkynen | 2b3b9e9 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 187 | virtio_cread(dev, struct virtio_blk_config, capacity, &cap); |
| 188 | desc->lba = cap; |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static const struct blk_ops virtio_blk_ops = { |
| 194 | .read = virtio_blk_read, |
| 195 | .write = virtio_blk_write, |
Dmitrii Merkurev | 4c0d8ec | 2024-03-06 18:59:18 +0000 | [diff] [blame] | 196 | .erase = virtio_blk_erase, |
Tuomas Tynkkynen | 2b3b9e9 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | U_BOOT_DRIVER(virtio_blk) = { |
| 200 | .name = VIRTIO_BLK_DRV_NAME, |
| 201 | .id = UCLASS_BLK, |
| 202 | .ops = &virtio_blk_ops, |
| 203 | .bind = virtio_blk_bind, |
| 204 | .probe = virtio_blk_probe, |
| 205 | .remove = virtio_reset, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 206 | .priv_auto = sizeof(struct virtio_blk_priv), |
Tuomas Tynkkynen | 2b3b9e9 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 207 | .flags = DM_FLAG_ACTIVE_DMA, |
| 208 | }; |