blob: 3ff74f4a9759efe48fc5ec622782c091d7825f24 [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
7#include <common.h>
8#include <blk.h>
9#include <dm.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060010#include <part.h>
Tuomas Tynkkynenf4802202018-10-15 02:21:11 -070011#include <virtio_types.h>
12#include <virtio.h>
13#include <virtio_ring.h>
14#include "virtio_blk.h"
15
16struct virtio_blk_priv {
17 struct virtqueue *vq;
18};
19
20static 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
58static 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
65static 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
72static int virtio_blk_bind(struct udevice *dev)
73{
74 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent);
Simon Glasscaa4daa2020-12-03 16:55:18 -070075 struct blk_desc *desc = dev_get_uclass_plat(dev);
Tuomas Tynkkynenf4802202018-10-15 02:21:11 -070076 int devnum;
77
78 desc->if_type = IF_TYPE_VIRTIO;
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;
85 devnum = blk_next_free_devnum(IF_TYPE_VIRTIO);
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
106static int virtio_blk_probe(struct udevice *dev)
107{
108 struct virtio_blk_priv *priv = dev_get_priv(dev);
Simon Glasscaa4daa2020-12-03 16:55:18 -0700109 struct blk_desc *desc = dev_get_uclass_plat(dev);
Tuomas Tynkkynenf4802202018-10-15 02:21:11 -0700110 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 Takahiro6b0ddd12020-08-19 18:07:32 +0900118 desc->log2blksz = 9;
Tuomas Tynkkynenf4802202018-10-15 02:21:11 -0700119 virtio_cread(dev, struct virtio_blk_config, capacity, &cap);
120 desc->lba = cap;
121
122 return 0;
123}
124
125static const struct blk_ops virtio_blk_ops = {
126 .read = virtio_blk_read,
127 .write = virtio_blk_write,
128};
129
130U_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 Glass41575d82020-12-03 16:55:17 -0700137 .priv_auto = sizeof(struct virtio_blk_priv),
Tuomas Tynkkynenf4802202018-10-15 02:21:11 -0700138 .flags = DM_FLAG_ACTIVE_DMA,
139};