blob: 95810582867c78185cac41831bca7309c3198f6e [file] [log] [blame]
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -07001// 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 Glass1ffa43c2023-01-28 15:00:17 -07007#define LOG_CATEGORY UCLASS_VIRTIO
8
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -07009#include <common.h>
10#include <blk.h>
11#include <dm.h>
Simon Glass655306c2020-05-10 11:39:58 -060012#include <part.h>
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -070013#include <virtio_types.h>
14#include <virtio.h>
15#include <virtio_ring.h>
16#include "virtio_blk.h"
17
18struct virtio_blk_priv {
19 struct virtqueue *vq;
20};
21
22static 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 Glass1ffa43c2023-01-28 15:00:17 -070047 log_debug("dev=%s, active=%d, priv=%p, priv->vq=%p\n", dev->name,
48 device_active(dev), priv, priv->vq);
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -070049
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 Glass1ffa43c2023-01-28 15:00:17 -070056 log_debug("wait...");
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -070057 while (!virtqueue_get_buf(priv->vq, NULL))
58 ;
Simon Glass1ffa43c2023-01-28 15:00:17 -070059 log_debug("done\n");
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -070060
61 return status == VIRTIO_BLK_S_OK ? blkcnt : -EIO;
62}
63
64static ulong virtio_blk_read(struct udevice *dev, lbaint_t start,
65 lbaint_t blkcnt, void *buffer)
66{
Simon Glass1ffa43c2023-01-28 15:00:17 -070067 log_debug("read %s\n", dev->name);
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -070068 return virtio_blk_do_req(dev, start, blkcnt, buffer,
69 VIRTIO_BLK_T_IN);
70}
71
72static 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
79static int virtio_blk_bind(struct udevice *dev)
80{
81 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -070082 struct blk_desc *desc = dev_get_uclass_plat(dev);
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -070083 int devnum;
84
Simon Glassfada3f92022-09-17 09:00:09 -060085 desc->uclass_id = UCLASS_VIRTIO;
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -070086 /*
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 Glassdbfa32c2022-08-11 19:34:59 -060092 devnum = blk_next_free_devnum(UCLASS_VIRTIO);
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -070093 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
113static int virtio_blk_probe(struct udevice *dev)
114{
115 struct virtio_blk_priv *priv = dev_get_priv(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -0700116 struct blk_desc *desc = dev_get_uclass_plat(dev);
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -0700117 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 Takahiro16630a82020-08-19 18:07:32 +0900125 desc->log2blksz = 9;
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -0700126 virtio_cread(dev, struct virtio_blk_config, capacity, &cap);
127 desc->lba = cap;
128
129 return 0;
130}
131
132static const struct blk_ops virtio_blk_ops = {
133 .read = virtio_blk_read,
134 .write = virtio_blk_write,
135};
136
137U_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 Glass8a2b47f2020-12-03 16:55:17 -0700144 .priv_auto = sizeof(struct virtio_blk_priv),
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -0700145 .flags = DM_FLAG_ACTIVE_DMA,
146};