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