2 * Copyright (c) 2014 ARM Limited
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * Authors: Andreas Sandberg
40 #ifndef __DEV_VIRTIO_BLOCK_HH__
41 #define __DEV_VIRTIO_BLOCK_HH__
43 #include "dev/virtio/base.hh"
44 #include "dev/storage/disk_image.hh"
45 #include "dev/terminal.hh"
47 struct VirtIOBlockParams;
52 * The block device uses the following queues:
55 * A guest issues a request by creating a descriptor chain that starts
56 * with a BlkRequest. Immediately after the BlkRequest follows the
57 * data for the request. The data buffer(s) are either input or output
58 * descriptors depending on the request type. The last byte in the
59 * descriptor chain should always be writable by the host and contains
60 * the request status code (OK/Error).
62 * The protocol supports asynchronous request completion by returning
63 * descriptor chains when they have been populated by the backing
64 * store. However, there is little point in doing so here.
66 * @see https://github.com/rustyrussell/virtio-spec
67 * @see http://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.html
69 class VirtIOBlock : public VirtIODeviceBase
72 typedef VirtIOBlockParams Params;
73 VirtIOBlock(Params *params);
74 virtual ~VirtIOBlock();
76 void readConfig(PacketPtr pkt, Addr cfgOffset);
79 static const DeviceId ID_BLOCK = 0x02;
82 * Block device configuration structure
84 * @note This needs to be changed if the supported feature set
95 static const FeatureBits F_SIZE_MAX = (1 << 1);
96 static const FeatureBits F_SEG_MAX = (1 << 2);
97 static const FeatureBits F_GEOMETRY = (1 << 4);
98 static const FeatureBits F_RO = (1 << 5);
99 static const FeatureBits F_BLK_SIZE = (1 << 6);
100 static const FeatureBits F_TOPOLOGY = (1 << 10);
104 * @name VirtIO block requests
106 typedef uint32_t RequestType;
108 static const RequestType T_IN = 0;
110 static const RequestType T_OUT = 1;
111 /** Flush device buffers */
112 static const RequestType T_FLUSH = 4;
116 * @name VirtIO block request status
118 typedef uint8_t Status;
119 /** Request succeeded */
120 static const Status S_OK = 0;
121 /** Request failed due to a device error */
122 static const Status S_IOERR = 1;
123 /** Request not supported */
124 static const Status S_UNSUPP = 2;
127 /** VirtIO block device request as sent by guest */
135 * Device read request.
137 * @param req Disk request from guest.
138 * @param desc_chain Request descriptor chain (from start of
140 * @param off_data Offset into the descriptor chain where data
142 * @param size Request data size.
144 Status read(const BlkRequest &req, VirtDescriptor *desc_chain,
145 size_t off_data, size_t size);
147 * Device write request.
149 * @param req Disk request from guest.
150 * @param desc_chain Request descriptor chain (from start of
152 * @param off_data Offset into the descriptor chain where data
154 * @param size Request data size.
156 Status write(const BlkRequest &req, VirtDescriptor *desc_chain,
157 size_t off_data, size_t size);
161 * Virtqueue for disk requests.
167 RequestQueue(PortProxy &proxy, uint16_t size, VirtIOBlock &_parent)
168 : VirtQueue(proxy, size), parent(_parent) {}
169 virtual ~RequestQueue() {}
171 void onNotifyDescriptor(VirtDescriptor *desc);
173 std::string name() const { return parent.name() + ".qRequests"; }
179 /** Device I/O request queue */
180 RequestQueue qRequests;
182 /** Image backing this device */
186 #endif // __DEV_VIRTIO_BLOCK_HH__