blob: 992118c60758470c05c19cb3ac4b5ff1d67a4c00 [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);
75 struct blk_desc *desc = dev_get_uclass_platdata(dev);
76 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);
109 struct blk_desc *desc = dev_get_uclass_platdata(dev);
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;
118 virtio_cread(dev, struct virtio_blk_config, capacity, &cap);
119 desc->lba = cap;
120
121 return 0;
122}
123
124static const struct blk_ops virtio_blk_ops = {
125 .read = virtio_blk_read,
126 .write = virtio_blk_write,
127};
128
129U_BOOT_DRIVER(virtio_blk) = {
130 .name = VIRTIO_BLK_DRV_NAME,
131 .id = UCLASS_BLK,
132 .ops = &virtio_blk_ops,
133 .bind = virtio_blk_bind,
134 .probe = virtio_blk_probe,
135 .remove = virtio_reset,
136 .priv_auto_alloc_size = sizeof(struct virtio_blk_priv),
137 .flags = DM_FLAG_ACTIVE_DMA,
138};