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