Tuomas Tynkkynen | f480220 | 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 | d472e93 | 2023-01-28 15:00:17 -0700 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_VIRTIO |
| 8 | |
Tuomas Tynkkynen | f480220 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 9 | #include <blk.h> |
| 10 | #include <dm.h> |
Simon Glass | e6f6f9e | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 11 | #include <part.h> |
Tuomas Tynkkynen | f480220 | 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 | |
| 21 | static ulong virtio_blk_do_req(struct udevice *dev, u64 sector, |
| 22 | lbaint_t blkcnt, void *buffer, u32 type) |
| 23 | { |
| 24 | struct virtio_blk_priv *priv = dev_get_priv(dev); |
| 25 | unsigned int num_out = 0, num_in = 0; |
| 26 | struct virtio_sg *sgs[3]; |
| 27 | u8 status; |
| 28 | int ret; |
| 29 | |
| 30 | struct virtio_blk_outhdr out_hdr = { |
| 31 | .type = cpu_to_virtio32(dev, type), |
| 32 | .sector = cpu_to_virtio64(dev, sector), |
| 33 | }; |
| 34 | struct virtio_sg hdr_sg = { &out_hdr, sizeof(out_hdr) }; |
| 35 | struct virtio_sg data_sg = { buffer, blkcnt * 512 }; |
| 36 | struct virtio_sg status_sg = { &status, sizeof(status) }; |
| 37 | |
| 38 | sgs[num_out++] = &hdr_sg; |
| 39 | |
| 40 | if (type & VIRTIO_BLK_T_OUT) |
| 41 | sgs[num_out++] = &data_sg; |
| 42 | else |
| 43 | sgs[num_out + num_in++] = &data_sg; |
| 44 | |
| 45 | sgs[num_out + num_in++] = &status_sg; |
Simon Glass | d472e93 | 2023-01-28 15:00:17 -0700 | [diff] [blame] | 46 | log_debug("dev=%s, active=%d, priv=%p, priv->vq=%p\n", dev->name, |
| 47 | device_active(dev), priv, priv->vq); |
Tuomas Tynkkynen | f480220 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 48 | |
| 49 | ret = virtqueue_add(priv->vq, sgs, num_out, num_in); |
| 50 | if (ret) |
| 51 | return ret; |
| 52 | |
| 53 | virtqueue_kick(priv->vq); |
| 54 | |
Simon Glass | d472e93 | 2023-01-28 15:00:17 -0700 | [diff] [blame] | 55 | log_debug("wait..."); |
Tuomas Tynkkynen | f480220 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 56 | while (!virtqueue_get_buf(priv->vq, NULL)) |
| 57 | ; |
Simon Glass | d472e93 | 2023-01-28 15:00:17 -0700 | [diff] [blame] | 58 | log_debug("done\n"); |
Tuomas Tynkkynen | f480220 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 59 | |
| 60 | return status == VIRTIO_BLK_S_OK ? blkcnt : -EIO; |
| 61 | } |
| 62 | |
| 63 | static ulong virtio_blk_read(struct udevice *dev, lbaint_t start, |
| 64 | lbaint_t blkcnt, void *buffer) |
| 65 | { |
Simon Glass | d472e93 | 2023-01-28 15:00:17 -0700 | [diff] [blame] | 66 | log_debug("read %s\n", dev->name); |
Tuomas Tynkkynen | f480220 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 67 | return virtio_blk_do_req(dev, start, blkcnt, buffer, |
| 68 | VIRTIO_BLK_T_IN); |
| 69 | } |
| 70 | |
| 71 | static ulong virtio_blk_write(struct udevice *dev, lbaint_t start, |
| 72 | lbaint_t blkcnt, const void *buffer) |
| 73 | { |
| 74 | return virtio_blk_do_req(dev, start, blkcnt, (void *)buffer, |
| 75 | VIRTIO_BLK_T_OUT); |
| 76 | } |
| 77 | |
| 78 | static int virtio_blk_bind(struct udevice *dev) |
| 79 | { |
| 80 | struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent); |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 81 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Tuomas Tynkkynen | f480220 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 82 | int devnum; |
| 83 | |
Simon Glass | 8149b15 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 84 | desc->uclass_id = UCLASS_VIRTIO; |
Tuomas Tynkkynen | f480220 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 85 | /* |
| 86 | * Initialize the devnum to -ENODEV. This is to make sure that |
| 87 | * blk_next_free_devnum() works as expected, since the default |
| 88 | * value 0 is a valid devnum. |
| 89 | */ |
| 90 | desc->devnum = -ENODEV; |
Simon Glass | e33a5c6 | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 91 | devnum = blk_next_free_devnum(UCLASS_VIRTIO); |
Tuomas Tynkkynen | f480220 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 92 | if (devnum < 0) |
| 93 | return devnum; |
| 94 | desc->devnum = devnum; |
| 95 | desc->part_type = PART_TYPE_UNKNOWN; |
| 96 | /* |
| 97 | * virtio mmio transport supplies string identification for us, |
| 98 | * while pci trnasport uses a 2-byte subvendor value. |
| 99 | */ |
| 100 | if (uc_priv->vendor >> 16) |
| 101 | sprintf(desc->vendor, "%s", (char *)&uc_priv->vendor); |
| 102 | else |
| 103 | sprintf(desc->vendor, "%04x", uc_priv->vendor); |
| 104 | desc->bdev = dev; |
| 105 | |
| 106 | /* Indicate what driver features we support */ |
| 107 | virtio_driver_features_init(uc_priv, NULL, 0, NULL, 0); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
| 112 | static int virtio_blk_probe(struct udevice *dev) |
| 113 | { |
| 114 | struct virtio_blk_priv *priv = dev_get_priv(dev); |
Simon Glass | caa4daa | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 115 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Tuomas Tynkkynen | f480220 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 116 | u64 cap; |
| 117 | int ret; |
| 118 | |
| 119 | ret = virtio_find_vqs(dev, 1, &priv->vq); |
| 120 | if (ret) |
| 121 | return ret; |
| 122 | |
| 123 | desc->blksz = 512; |
AKASHI Takahiro | 6b0ddd1 | 2020-08-19 18:07:32 +0900 | [diff] [blame] | 124 | desc->log2blksz = 9; |
Tuomas Tynkkynen | f480220 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 125 | virtio_cread(dev, struct virtio_blk_config, capacity, &cap); |
| 126 | desc->lba = cap; |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | static const struct blk_ops virtio_blk_ops = { |
| 132 | .read = virtio_blk_read, |
| 133 | .write = virtio_blk_write, |
| 134 | }; |
| 135 | |
| 136 | U_BOOT_DRIVER(virtio_blk) = { |
| 137 | .name = VIRTIO_BLK_DRV_NAME, |
| 138 | .id = UCLASS_BLK, |
| 139 | .ops = &virtio_blk_ops, |
| 140 | .bind = virtio_blk_bind, |
| 141 | .probe = virtio_blk_probe, |
| 142 | .remove = virtio_reset, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 143 | .priv_auto = sizeof(struct virtio_blk_priv), |
Tuomas Tynkkynen | f480220 | 2018-10-15 02:21:11 -0700 | [diff] [blame] | 144 | .flags = DM_FLAG_ACTIVE_DMA, |
| 145 | }; |