blob: 3404f61eba5ffdb10f9ec64631c1c51e2eece2d5 [file] [log] [blame]
Tuomas Tynkkynenf4802202018-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 Glassd472e932023-01-28 15:00:17 -07007#define LOG_CATEGORY UCLASS_VIRTIO
8
Tuomas Tynkkynenf4802202018-10-15 02:21:11 -07009#include <blk.h>
10#include <dm.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060011#include <part.h>
Tuomas Tynkkynenf4802202018-10-15 02:21:11 -070012#include <virtio_types.h>
13#include <virtio.h>
14#include <virtio_ring.h>
15#include "virtio_blk.h"
16
17struct virtio_blk_priv {
18 struct virtqueue *vq;
19};
20
21static 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 Glassd472e932023-01-28 15:00:17 -070046 log_debug("dev=%s, active=%d, priv=%p, priv->vq=%p\n", dev->name,
47 device_active(dev), priv, priv->vq);
Tuomas Tynkkynenf4802202018-10-15 02:21:11 -070048
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 Glassd472e932023-01-28 15:00:17 -070055 log_debug("wait...");
Tuomas Tynkkynenf4802202018-10-15 02:21:11 -070056 while (!virtqueue_get_buf(priv->vq, NULL))
57 ;
Simon Glassd472e932023-01-28 15:00:17 -070058 log_debug("done\n");
Tuomas Tynkkynenf4802202018-10-15 02:21:11 -070059
60 return status == VIRTIO_BLK_S_OK ? blkcnt : -EIO;
61}
62
63static ulong virtio_blk_read(struct udevice *dev, lbaint_t start,
64 lbaint_t blkcnt, void *buffer)
65{
Simon Glassd472e932023-01-28 15:00:17 -070066 log_debug("read %s\n", dev->name);
Tuomas Tynkkynenf4802202018-10-15 02:21:11 -070067 return virtio_blk_do_req(dev, start, blkcnt, buffer,
68 VIRTIO_BLK_T_IN);
69}
70
71static 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
78static int virtio_blk_bind(struct udevice *dev)
79{
80 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent);
Simon Glasscaa4daa2020-12-03 16:55:18 -070081 struct blk_desc *desc = dev_get_uclass_plat(dev);
Tuomas Tynkkynenf4802202018-10-15 02:21:11 -070082 int devnum;
83
Simon Glass8149b152022-09-17 09:00:09 -060084 desc->uclass_id = UCLASS_VIRTIO;
Tuomas Tynkkynenf4802202018-10-15 02:21:11 -070085 /*
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 Glasse33a5c62022-08-11 19:34:59 -060091 devnum = blk_next_free_devnum(UCLASS_VIRTIO);
Tuomas Tynkkynenf4802202018-10-15 02:21:11 -070092 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
112static int virtio_blk_probe(struct udevice *dev)
113{
114 struct virtio_blk_priv *priv = dev_get_priv(dev);
Simon Glasscaa4daa2020-12-03 16:55:18 -0700115 struct blk_desc *desc = dev_get_uclass_plat(dev);
Tuomas Tynkkynenf4802202018-10-15 02:21:11 -0700116 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 Takahiro6b0ddd12020-08-19 18:07:32 +0900124 desc->log2blksz = 9;
Tuomas Tynkkynenf4802202018-10-15 02:21:11 -0700125 virtio_cread(dev, struct virtio_blk_config, capacity, &cap);
126 desc->lba = cap;
127
128 return 0;
129}
130
131static const struct blk_ops virtio_blk_ops = {
132 .read = virtio_blk_read,
133 .write = virtio_blk_write,
134};
135
136U_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 Glass41575d82020-12-03 16:55:17 -0700143 .priv_auto = sizeof(struct virtio_blk_priv),
Tuomas Tynkkynenf4802202018-10-15 02:21:11 -0700144 .flags = DM_FLAG_ACTIVE_DMA,
145};