blob: 2f999fc8bbecf8d2d9f61c60990a442f3d54c9a6 [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 <blk.h>
10#include <dm.h>
Simon Glass655306c2020-05-10 11:39:58 -060011#include <part.h>
Tuomas Tynkkynen2b3b9e92018-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
Dmitrii Merkurev4c0d8ec2024-03-06 18:59:18 +000021static const u32 feature[] = {
22 VIRTIO_BLK_F_WRITE_ZEROES
23};
24
25static void virtio_blk_init_header_sg(struct udevice *dev, u64 sector, u32 type,
26 struct virtio_blk_outhdr *out_hdr, struct virtio_sg *sg)
27{
28 const bool sector_is_needed = type == VIRTIO_BLK_T_IN ||
29 type == VIRTIO_BLK_T_OUT;
30
31 out_hdr->type = cpu_to_virtio32(dev, type);
32 out_hdr->sector = cpu_to_virtio64(dev, sector_is_needed ? sector : 0);
33
34 sg->addr = out_hdr;
35 sg->length = sizeof(*out_hdr);
36}
37
38static void virtio_blk_init_write_zeroes_sg(struct udevice *dev, u64 sector, lbaint_t blkcnt,
39 struct virtio_blk_discard_write_zeroes *wz,
40 struct virtio_sg *sg)
41{
42 wz->sector = cpu_to_virtio64(dev, sector);
43 wz->num_sectors = cpu_to_virtio32(dev, blkcnt);
44 wz->flags = cpu_to_virtio32(dev, 0);
45
46 sg->addr = wz;
47 sg->length = sizeof(*wz);
48}
49
50static void virtio_blk_init_status_sg(u8 *status, struct virtio_sg *sg)
51{
52 sg->addr = status;
53 sg->length = sizeof(*status);
54}
55
56static void virtio_blk_init_data_sg(void *buffer, lbaint_t blkcnt, struct virtio_sg *sg)
57{
58 sg->addr = buffer;
59 sg->length = blkcnt * 512;
60}
61
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -070062static ulong virtio_blk_do_req(struct udevice *dev, u64 sector,
63 lbaint_t blkcnt, void *buffer, u32 type)
64{
65 struct virtio_blk_priv *priv = dev_get_priv(dev);
Dmitrii Merkurev4c0d8ec2024-03-06 18:59:18 +000066 struct virtio_blk_outhdr out_hdr;
67 struct virtio_blk_discard_write_zeroes wz_hdr;
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -070068 unsigned int num_out = 0, num_in = 0;
Dmitrii Merkurev4c0d8ec2024-03-06 18:59:18 +000069 struct virtio_sg hdr_sg, wz_sg, data_sg, status_sg;
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -070070 struct virtio_sg *sgs[3];
71 u8 status;
72 int ret;
73
Dmitrii Merkurev4c0d8ec2024-03-06 18:59:18 +000074 virtio_blk_init_header_sg(dev, sector, type, &out_hdr, &hdr_sg);
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -070075 sgs[num_out++] = &hdr_sg;
76
Dmitrii Merkurev4c0d8ec2024-03-06 18:59:18 +000077 switch (type) {
78 case VIRTIO_BLK_T_IN:
79 case VIRTIO_BLK_T_OUT:
80 virtio_blk_init_data_sg(buffer, blkcnt, &data_sg);
81 if (type & VIRTIO_BLK_T_OUT)
82 sgs[num_out++] = &data_sg;
83 else
84 sgs[num_out + num_in++] = &data_sg;
85 break;
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -070086
Dmitrii Merkurev4c0d8ec2024-03-06 18:59:18 +000087 case VIRTIO_BLK_T_WRITE_ZEROES:
88 virtio_blk_init_write_zeroes_sg(dev, sector, blkcnt, &wz_hdr, &wz_sg);
89 sgs[num_out++] = &wz_sg;
90 break;
91
92 default:
93 return -EINVAL;
94 }
95
96 virtio_blk_init_status_sg(&status, &status_sg);
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -070097 sgs[num_out + num_in++] = &status_sg;
Simon Glass1ffa43c2023-01-28 15:00:17 -070098 log_debug("dev=%s, active=%d, priv=%p, priv->vq=%p\n", dev->name,
99 device_active(dev), priv, priv->vq);
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -0700100
101 ret = virtqueue_add(priv->vq, sgs, num_out, num_in);
102 if (ret)
103 return ret;
104
105 virtqueue_kick(priv->vq);
106
Simon Glass1ffa43c2023-01-28 15:00:17 -0700107 log_debug("wait...");
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -0700108 while (!virtqueue_get_buf(priv->vq, NULL))
109 ;
Simon Glass1ffa43c2023-01-28 15:00:17 -0700110 log_debug("done\n");
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -0700111
112 return status == VIRTIO_BLK_S_OK ? blkcnt : -EIO;
113}
114
115static ulong virtio_blk_read(struct udevice *dev, lbaint_t start,
116 lbaint_t blkcnt, void *buffer)
117{
Simon Glass1ffa43c2023-01-28 15:00:17 -0700118 log_debug("read %s\n", dev->name);
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -0700119 return virtio_blk_do_req(dev, start, blkcnt, buffer,
120 VIRTIO_BLK_T_IN);
121}
122
123static ulong virtio_blk_write(struct udevice *dev, lbaint_t start,
124 lbaint_t blkcnt, const void *buffer)
125{
126 return virtio_blk_do_req(dev, start, blkcnt, (void *)buffer,
127 VIRTIO_BLK_T_OUT);
128}
129
Dmitrii Merkurev4c0d8ec2024-03-06 18:59:18 +0000130static ulong virtio_blk_erase(struct udevice *dev, lbaint_t start,
131 lbaint_t blkcnt)
132{
133 if (!virtio_has_feature(dev, VIRTIO_BLK_F_WRITE_ZEROES))
134 return -EOPNOTSUPP;
135
136 return virtio_blk_do_req(dev, start, blkcnt, NULL, VIRTIO_BLK_T_WRITE_ZEROES);
137}
138
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -0700139static int virtio_blk_bind(struct udevice *dev)
140{
141 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent);
Simon Glass71fa5b42020-12-03 16:55:18 -0700142 struct blk_desc *desc = dev_get_uclass_plat(dev);
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -0700143 int devnum;
144
Simon Glassfada3f92022-09-17 09:00:09 -0600145 desc->uclass_id = UCLASS_VIRTIO;
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -0700146 /*
147 * Initialize the devnum to -ENODEV. This is to make sure that
148 * blk_next_free_devnum() works as expected, since the default
149 * value 0 is a valid devnum.
150 */
151 desc->devnum = -ENODEV;
Simon Glassdbfa32c2022-08-11 19:34:59 -0600152 devnum = blk_next_free_devnum(UCLASS_VIRTIO);
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -0700153 if (devnum < 0)
154 return devnum;
155 desc->devnum = devnum;
156 desc->part_type = PART_TYPE_UNKNOWN;
157 /*
158 * virtio mmio transport supplies string identification for us,
159 * while pci trnasport uses a 2-byte subvendor value.
160 */
161 if (uc_priv->vendor >> 16)
162 sprintf(desc->vendor, "%s", (char *)&uc_priv->vendor);
163 else
164 sprintf(desc->vendor, "%04x", uc_priv->vendor);
165 desc->bdev = dev;
166
167 /* Indicate what driver features we support */
Dmitrii Merkurev4c0d8ec2024-03-06 18:59:18 +0000168 virtio_driver_features_init(uc_priv, feature, ARRAY_SIZE(feature),
169 NULL, 0);
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -0700170
171 return 0;
172}
173
174static int virtio_blk_probe(struct udevice *dev)
175{
176 struct virtio_blk_priv *priv = dev_get_priv(dev);
Simon Glass71fa5b42020-12-03 16:55:18 -0700177 struct blk_desc *desc = dev_get_uclass_plat(dev);
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -0700178 u64 cap;
179 int ret;
180
181 ret = virtio_find_vqs(dev, 1, &priv->vq);
182 if (ret)
183 return ret;
184
185 desc->blksz = 512;
AKASHI Takahiro16630a82020-08-19 18:07:32 +0900186 desc->log2blksz = 9;
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -0700187 virtio_cread(dev, struct virtio_blk_config, capacity, &cap);
188 desc->lba = cap;
189
190 return 0;
191}
192
193static const struct blk_ops virtio_blk_ops = {
194 .read = virtio_blk_read,
195 .write = virtio_blk_write,
Dmitrii Merkurev4c0d8ec2024-03-06 18:59:18 +0000196 .erase = virtio_blk_erase,
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -0700197};
198
199U_BOOT_DRIVER(virtio_blk) = {
200 .name = VIRTIO_BLK_DRV_NAME,
201 .id = UCLASS_BLK,
202 .ops = &virtio_blk_ops,
203 .bind = virtio_blk_bind,
204 .probe = virtio_blk_probe,
205 .remove = virtio_reset,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700206 .priv_auto = sizeof(struct virtio_blk_priv),
Tuomas Tynkkynen2b3b9e92018-10-15 02:21:11 -0700207 .flags = DM_FLAG_ACTIVE_DMA,
208};