sparc: Use big endian packet accessors.
[gem5.git] / src / dev / virtio / block.hh
1 /*
2 * Copyright (c) 2014 ARM Limited
3 * All rights reserved
4 *
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.
13 *
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.
24 *
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.
36 *
37 * Authors: Andreas Sandberg
38 */
39
40 #ifndef __DEV_VIRTIO_BLOCK_HH__
41 #define __DEV_VIRTIO_BLOCK_HH__
42
43 #include "dev/virtio/base.hh"
44 #include "dev/storage/disk_image.hh"
45
46 struct VirtIOBlockParams;
47
48 /**
49 * VirtIO block device
50 *
51 * The block device uses the following queues:
52 * -# Requests
53 *
54 * A guest issues a request by creating a descriptor chain that starts
55 * with a BlkRequest. Immediately after the BlkRequest follows the
56 * data for the request. The data buffer(s) are either input or output
57 * descriptors depending on the request type. The last byte in the
58 * descriptor chain should always be writable by the host and contains
59 * the request status code (OK/Error).
60 *
61 * The protocol supports asynchronous request completion by returning
62 * descriptor chains when they have been populated by the backing
63 * store. However, there is little point in doing so here.
64 *
65 * @see https://github.com/rustyrussell/virtio-spec
66 * @see http://docs.oasis-open.org/virtio/virtio/v1.0/virtio-v1.0.html
67 */
68 class VirtIOBlock : public VirtIODeviceBase
69 {
70 public:
71 typedef VirtIOBlockParams Params;
72 VirtIOBlock(Params *params);
73 virtual ~VirtIOBlock();
74
75 void readConfig(PacketPtr pkt, Addr cfgOffset);
76
77 protected:
78 static const DeviceId ID_BLOCK = 0x02;
79
80 /**
81 * Block device configuration structure
82 *
83 * @note This needs to be changed if the supported feature set
84 * changes!
85 */
86 struct Config {
87 uint64_t capacity;
88 } M5_ATTR_PACKED;
89 Config config;
90
91 /** @{
92 * @name Feature bits
93 */
94 static const FeatureBits F_SIZE_MAX = (1 << 1);
95 static const FeatureBits F_SEG_MAX = (1 << 2);
96 static const FeatureBits F_GEOMETRY = (1 << 4);
97 static const FeatureBits F_RO = (1 << 5);
98 static const FeatureBits F_BLK_SIZE = (1 << 6);
99 static const FeatureBits F_TOPOLOGY = (1 << 10);
100 /** @} */
101
102 /** @{
103 * @name VirtIO block requests
104 */
105 typedef uint32_t RequestType;
106 /** Read request */
107 static const RequestType T_IN = 0;
108 /** Write request */
109 static const RequestType T_OUT = 1;
110 /** Flush device buffers */
111 static const RequestType T_FLUSH = 4;
112 /** @} */
113
114 /** @{
115 * @name VirtIO block request status
116 */
117 typedef uint8_t Status;
118 /** Request succeeded */
119 static const Status S_OK = 0;
120 /** Request failed due to a device error */
121 static const Status S_IOERR = 1;
122 /** Request not supported */
123 static const Status S_UNSUPP = 2;
124 /** @} */
125
126 /** VirtIO block device request as sent by guest */
127 struct BlkRequest {
128 RequestType type;
129 uint32_t reserved;
130 uint64_t sector;
131 } M5_ATTR_PACKED;
132
133 /**
134 * Device read request.
135 *
136 * @param req Disk request from guest.
137 * @param desc_chain Request descriptor chain (from start of
138 * request)
139 * @param off_data Offset into the descriptor chain where data
140 * should be written.
141 * @param size Request data size.
142 */
143 Status read(const BlkRequest &req, VirtDescriptor *desc_chain,
144 size_t off_data, size_t size);
145 /**
146 * Device write request.
147 *
148 * @param req Disk request from guest.
149 * @param desc_chain Request descriptor chain (from start of
150 * request)
151 * @param off_data Offset into the descriptor chain where data
152 * should be read.
153 * @param size Request data size.
154 */
155 Status write(const BlkRequest &req, VirtDescriptor *desc_chain,
156 size_t off_data, size_t size);
157
158 protected:
159 /**
160 * Virtqueue for disk requests.
161 */
162 class RequestQueue
163 : public VirtQueue
164 {
165 public:
166 RequestQueue(PortProxy &proxy, uint16_t size, VirtIOBlock &_parent)
167 : VirtQueue(proxy, size), parent(_parent) {}
168 virtual ~RequestQueue() {}
169
170 void onNotifyDescriptor(VirtDescriptor *desc);
171
172 std::string name() const { return parent.name() + ".qRequests"; }
173
174 protected:
175 VirtIOBlock &parent;
176 };
177
178 /** Device I/O request queue */
179 RequestQueue qRequests;
180
181 /** Image backing this device */
182 DiskImage &image;
183 };
184
185 #endif // __DEV_VIRTIO_BLOCK_HH__