root / hw / virtio-blk.c @ ef816d83
History | View | Annotate | Download (14.3 kB)
1 | 6e02c38d | aliguori | /*
|
---|---|---|---|
2 | 6e02c38d | aliguori | * Virtio Block Device
|
3 | 6e02c38d | aliguori | *
|
4 | 6e02c38d | aliguori | * Copyright IBM, Corp. 2007
|
5 | 6e02c38d | aliguori | *
|
6 | 6e02c38d | aliguori | * Authors:
|
7 | 6e02c38d | aliguori | * Anthony Liguori <aliguori@us.ibm.com>
|
8 | 6e02c38d | aliguori | *
|
9 | 6e02c38d | aliguori | * This work is licensed under the terms of the GNU GPL, version 2. See
|
10 | 6e02c38d | aliguori | * the COPYING file in the top-level directory.
|
11 | 6e02c38d | aliguori | *
|
12 | 6e02c38d | aliguori | */
|
13 | 6e02c38d | aliguori | |
14 | 869a5c6d | aliguori | #include <qemu-common.h> |
15 | 869a5c6d | aliguori | #include <sysemu.h> |
16 | 6e02c38d | aliguori | #include "virtio-blk.h" |
17 | 6e02c38d | aliguori | #include "block_int.h" |
18 | 1063b8b1 | Christoph Hellwig | #ifdef __linux__
|
19 | 1063b8b1 | Christoph Hellwig | # include <scsi/sg.h> |
20 | 1063b8b1 | Christoph Hellwig | #endif
|
21 | 6e02c38d | aliguori | |
22 | 6e02c38d | aliguori | typedef struct VirtIOBlock |
23 | 6e02c38d | aliguori | { |
24 | 6e02c38d | aliguori | VirtIODevice vdev; |
25 | 6e02c38d | aliguori | BlockDriverState *bs; |
26 | 6e02c38d | aliguori | VirtQueue *vq; |
27 | 869a5c6d | aliguori | void *rq;
|
28 | bf011293 | john cooper | char serial_str[BLOCK_SERIAL_STRLEN + 1]; |
29 | 213189ab | Markus Armbruster | QEMUBH *bh; |
30 | 711bf3d9 | Michael S. Tsirkin | size_t config_size; |
31 | 6e02c38d | aliguori | } VirtIOBlock; |
32 | 6e02c38d | aliguori | |
33 | 6e02c38d | aliguori | static VirtIOBlock *to_virtio_blk(VirtIODevice *vdev)
|
34 | 6e02c38d | aliguori | { |
35 | 6e02c38d | aliguori | return (VirtIOBlock *)vdev;
|
36 | 6e02c38d | aliguori | } |
37 | 6e02c38d | aliguori | |
38 | bf011293 | john cooper | /* store identify data in little endian format
|
39 | bf011293 | john cooper | */
|
40 | bf011293 | john cooper | static inline void put_le16(uint16_t *p, unsigned int v) |
41 | bf011293 | john cooper | { |
42 | bf011293 | john cooper | *p = cpu_to_le16(v); |
43 | bf011293 | john cooper | } |
44 | bf011293 | john cooper | |
45 | bf011293 | john cooper | /* copy to *dst from *src, nul pad dst tail as needed to len bytes
|
46 | bf011293 | john cooper | */
|
47 | bf011293 | john cooper | static inline void padstr(char *dst, const char *src, int len) |
48 | bf011293 | john cooper | { |
49 | bf011293 | john cooper | while (len--)
|
50 | bf011293 | john cooper | *dst++ = *src ? *src++ : '\0';
|
51 | bf011293 | john cooper | } |
52 | bf011293 | john cooper | |
53 | bf011293 | john cooper | /* setup simulated identify data as appropriate for virtio block device
|
54 | bf011293 | john cooper | *
|
55 | bf011293 | john cooper | * ref: AT Attachment 8 - ATA/ATAPI Command Set (ATA8-ACS)
|
56 | bf011293 | john cooper | */
|
57 | bf011293 | john cooper | static inline void virtio_identify_template(struct virtio_blk_config *bc) |
58 | bf011293 | john cooper | { |
59 | bf011293 | john cooper | uint16_t *p = &bc->identify[0];
|
60 | bf011293 | john cooper | uint64_t lba_sectors = bc->capacity; |
61 | bf011293 | john cooper | |
62 | bf011293 | john cooper | memset(p, 0, sizeof(bc->identify)); |
63 | bf011293 | john cooper | put_le16(p + 0, 0x0); /* ATA device */ |
64 | bf011293 | john cooper | padstr((char *)(p + 23), QEMU_VERSION, 8); /* firmware revision */ |
65 | bf011293 | john cooper | padstr((char *)(p + 27), "QEMU VIRT_BLK", 40); /* model# */ |
66 | bf011293 | john cooper | put_le16(p + 47, 0x80ff); /* max xfer 255 sectors */ |
67 | bf011293 | john cooper | put_le16(p + 49, 0x0b00); /* support IORDY/LBA/DMA */ |
68 | bf011293 | john cooper | put_le16(p + 59, 0x1ff); /* cur xfer 255 sectors */ |
69 | bf011293 | john cooper | put_le16(p + 80, 0x1f0); /* support ATA8/7/6/5/4 */ |
70 | bf011293 | john cooper | put_le16(p + 81, 0x16); |
71 | bf011293 | john cooper | put_le16(p + 82, 0x400); |
72 | bf011293 | john cooper | put_le16(p + 83, 0x400); |
73 | bf011293 | john cooper | put_le16(p + 100, lba_sectors);
|
74 | bf011293 | john cooper | put_le16(p + 101, lba_sectors >> 16); |
75 | bf011293 | john cooper | put_le16(p + 102, lba_sectors >> 32); |
76 | bf011293 | john cooper | put_le16(p + 103, lba_sectors >> 48); |
77 | bf011293 | john cooper | } |
78 | bf011293 | john cooper | |
79 | 6e02c38d | aliguori | typedef struct VirtIOBlockReq |
80 | 6e02c38d | aliguori | { |
81 | 6e02c38d | aliguori | VirtIOBlock *dev; |
82 | 6e02c38d | aliguori | VirtQueueElement elem; |
83 | 6e02c38d | aliguori | struct virtio_blk_inhdr *in;
|
84 | 6e02c38d | aliguori | struct virtio_blk_outhdr *out;
|
85 | 1063b8b1 | Christoph Hellwig | struct virtio_scsi_inhdr *scsi;
|
86 | d28a1b6e | aliguori | QEMUIOVector qiov; |
87 | 869a5c6d | aliguori | struct VirtIOBlockReq *next;
|
88 | 6e02c38d | aliguori | } VirtIOBlockReq; |
89 | 6e02c38d | aliguori | |
90 | 869a5c6d | aliguori | static void virtio_blk_req_complete(VirtIOBlockReq *req, int status) |
91 | 869a5c6d | aliguori | { |
92 | 869a5c6d | aliguori | VirtIOBlock *s = req->dev; |
93 | 869a5c6d | aliguori | |
94 | 869a5c6d | aliguori | req->in->status = status; |
95 | d28a1b6e | aliguori | virtqueue_push(s->vq, &req->elem, req->qiov.size + sizeof(*req->in));
|
96 | 869a5c6d | aliguori | virtio_notify(&s->vdev, s->vq); |
97 | 869a5c6d | aliguori | |
98 | 869a5c6d | aliguori | qemu_free(req); |
99 | 869a5c6d | aliguori | } |
100 | 869a5c6d | aliguori | |
101 | 869a5c6d | aliguori | static int virtio_blk_handle_write_error(VirtIOBlockReq *req, int error) |
102 | 869a5c6d | aliguori | { |
103 | 869a5c6d | aliguori | BlockInterfaceErrorAction action = drive_get_onerror(req->dev->bs); |
104 | 869a5c6d | aliguori | VirtIOBlock *s = req->dev; |
105 | 869a5c6d | aliguori | |
106 | 869a5c6d | aliguori | if (action == BLOCK_ERR_IGNORE)
|
107 | 869a5c6d | aliguori | return 0; |
108 | 869a5c6d | aliguori | |
109 | 869a5c6d | aliguori | if ((error == ENOSPC && action == BLOCK_ERR_STOP_ENOSPC)
|
110 | 869a5c6d | aliguori | || action == BLOCK_ERR_STOP_ANY) { |
111 | 869a5c6d | aliguori | req->next = s->rq; |
112 | 869a5c6d | aliguori | s->rq = req; |
113 | 869a5c6d | aliguori | vm_stop(0);
|
114 | 869a5c6d | aliguori | } else {
|
115 | 869a5c6d | aliguori | virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR); |
116 | 869a5c6d | aliguori | } |
117 | 869a5c6d | aliguori | |
118 | 869a5c6d | aliguori | return 1; |
119 | 869a5c6d | aliguori | } |
120 | 869a5c6d | aliguori | |
121 | 6e02c38d | aliguori | static void virtio_blk_rw_complete(void *opaque, int ret) |
122 | 6e02c38d | aliguori | { |
123 | 6e02c38d | aliguori | VirtIOBlockReq *req = opaque; |
124 | 6e02c38d | aliguori | |
125 | d28a1b6e | aliguori | if (ret && (req->out->type & VIRTIO_BLK_T_OUT)) {
|
126 | 869a5c6d | aliguori | if (virtio_blk_handle_write_error(req, -ret))
|
127 | 869a5c6d | aliguori | return;
|
128 | 6e02c38d | aliguori | } |
129 | 6e02c38d | aliguori | |
130 | 869a5c6d | aliguori | virtio_blk_req_complete(req, VIRTIO_BLK_S_OK); |
131 | 869a5c6d | aliguori | } |
132 | 6e02c38d | aliguori | |
133 | aa659be3 | Christoph Hellwig | static void virtio_blk_flush_complete(void *opaque, int ret) |
134 | aa659be3 | Christoph Hellwig | { |
135 | aa659be3 | Christoph Hellwig | VirtIOBlockReq *req = opaque; |
136 | aa659be3 | Christoph Hellwig | |
137 | aa659be3 | Christoph Hellwig | virtio_blk_req_complete(req, ret ? VIRTIO_BLK_S_IOERR : VIRTIO_BLK_S_OK); |
138 | aa659be3 | Christoph Hellwig | } |
139 | aa659be3 | Christoph Hellwig | |
140 | 869a5c6d | aliguori | static VirtIOBlockReq *virtio_blk_alloc_request(VirtIOBlock *s)
|
141 | 869a5c6d | aliguori | { |
142 | 869a5c6d | aliguori | VirtIOBlockReq *req = qemu_mallocz(sizeof(*req));
|
143 | 487414f1 | aliguori | req->dev = s; |
144 | 869a5c6d | aliguori | return req;
|
145 | 6e02c38d | aliguori | } |
146 | 6e02c38d | aliguori | |
147 | 6e02c38d | aliguori | static VirtIOBlockReq *virtio_blk_get_request(VirtIOBlock *s)
|
148 | 6e02c38d | aliguori | { |
149 | 869a5c6d | aliguori | VirtIOBlockReq *req = virtio_blk_alloc_request(s); |
150 | 6e02c38d | aliguori | |
151 | 869a5c6d | aliguori | if (req != NULL) { |
152 | 869a5c6d | aliguori | if (!virtqueue_pop(s->vq, &req->elem)) {
|
153 | 869a5c6d | aliguori | qemu_free(req); |
154 | 869a5c6d | aliguori | return NULL; |
155 | 869a5c6d | aliguori | } |
156 | 6e02c38d | aliguori | } |
157 | 6e02c38d | aliguori | |
158 | 6e02c38d | aliguori | return req;
|
159 | 6e02c38d | aliguori | } |
160 | 6e02c38d | aliguori | |
161 | 1063b8b1 | Christoph Hellwig | #ifdef __linux__
|
162 | 1063b8b1 | Christoph Hellwig | static void virtio_blk_handle_scsi(VirtIOBlockReq *req) |
163 | 1063b8b1 | Christoph Hellwig | { |
164 | 1063b8b1 | Christoph Hellwig | struct sg_io_hdr hdr;
|
165 | 1063b8b1 | Christoph Hellwig | int ret, size = 0; |
166 | 1063b8b1 | Christoph Hellwig | int status;
|
167 | 1063b8b1 | Christoph Hellwig | int i;
|
168 | 1063b8b1 | Christoph Hellwig | |
169 | 1063b8b1 | Christoph Hellwig | /*
|
170 | 1063b8b1 | Christoph Hellwig | * We require at least one output segment each for the virtio_blk_outhdr
|
171 | 1063b8b1 | Christoph Hellwig | * and the SCSI command block.
|
172 | 1063b8b1 | Christoph Hellwig | *
|
173 | 1063b8b1 | Christoph Hellwig | * We also at least require the virtio_blk_inhdr, the virtio_scsi_inhdr
|
174 | 1063b8b1 | Christoph Hellwig | * and the sense buffer pointer in the input segments.
|
175 | 1063b8b1 | Christoph Hellwig | */
|
176 | 1063b8b1 | Christoph Hellwig | if (req->elem.out_num < 2 || req->elem.in_num < 3) { |
177 | 1063b8b1 | Christoph Hellwig | virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR); |
178 | 1063b8b1 | Christoph Hellwig | return;
|
179 | 1063b8b1 | Christoph Hellwig | } |
180 | 1063b8b1 | Christoph Hellwig | |
181 | 1063b8b1 | Christoph Hellwig | /*
|
182 | 1063b8b1 | Christoph Hellwig | * No support for bidirection commands yet.
|
183 | 1063b8b1 | Christoph Hellwig | */
|
184 | 1063b8b1 | Christoph Hellwig | if (req->elem.out_num > 2 && req->elem.in_num > 3) { |
185 | 1063b8b1 | Christoph Hellwig | virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP); |
186 | 1063b8b1 | Christoph Hellwig | return;
|
187 | 1063b8b1 | Christoph Hellwig | } |
188 | 1063b8b1 | Christoph Hellwig | |
189 | 1063b8b1 | Christoph Hellwig | /*
|
190 | 1063b8b1 | Christoph Hellwig | * The scsi inhdr is placed in the second-to-last input segment, just
|
191 | 1063b8b1 | Christoph Hellwig | * before the regular inhdr.
|
192 | 1063b8b1 | Christoph Hellwig | */
|
193 | 1063b8b1 | Christoph Hellwig | req->scsi = (void *)req->elem.in_sg[req->elem.in_num - 2].iov_base; |
194 | 1063b8b1 | Christoph Hellwig | size = sizeof(*req->in) + sizeof(*req->scsi); |
195 | 1063b8b1 | Christoph Hellwig | |
196 | 1063b8b1 | Christoph Hellwig | memset(&hdr, 0, sizeof(struct sg_io_hdr)); |
197 | 1063b8b1 | Christoph Hellwig | hdr.interface_id = 'S';
|
198 | 1063b8b1 | Christoph Hellwig | hdr.cmd_len = req->elem.out_sg[1].iov_len;
|
199 | 1063b8b1 | Christoph Hellwig | hdr.cmdp = req->elem.out_sg[1].iov_base;
|
200 | 1063b8b1 | Christoph Hellwig | hdr.dxfer_len = 0;
|
201 | 1063b8b1 | Christoph Hellwig | |
202 | 1063b8b1 | Christoph Hellwig | if (req->elem.out_num > 2) { |
203 | 1063b8b1 | Christoph Hellwig | /*
|
204 | 1063b8b1 | Christoph Hellwig | * If there are more than the minimally required 2 output segments
|
205 | 1063b8b1 | Christoph Hellwig | * there is write payload starting from the third iovec.
|
206 | 1063b8b1 | Christoph Hellwig | */
|
207 | 1063b8b1 | Christoph Hellwig | hdr.dxfer_direction = SG_DXFER_TO_DEV; |
208 | 1063b8b1 | Christoph Hellwig | hdr.iovec_count = req->elem.out_num - 2;
|
209 | 1063b8b1 | Christoph Hellwig | |
210 | 1063b8b1 | Christoph Hellwig | for (i = 0; i < hdr.iovec_count; i++) |
211 | 1063b8b1 | Christoph Hellwig | hdr.dxfer_len += req->elem.out_sg[i + 2].iov_len;
|
212 | 1063b8b1 | Christoph Hellwig | |
213 | 1063b8b1 | Christoph Hellwig | hdr.dxferp = req->elem.out_sg + 2;
|
214 | 1063b8b1 | Christoph Hellwig | |
215 | 1063b8b1 | Christoph Hellwig | } else if (req->elem.in_num > 3) { |
216 | 1063b8b1 | Christoph Hellwig | /*
|
217 | 1063b8b1 | Christoph Hellwig | * If we have more than 3 input segments the guest wants to actually
|
218 | 1063b8b1 | Christoph Hellwig | * read data.
|
219 | 1063b8b1 | Christoph Hellwig | */
|
220 | 1063b8b1 | Christoph Hellwig | hdr.dxfer_direction = SG_DXFER_FROM_DEV; |
221 | 1063b8b1 | Christoph Hellwig | hdr.iovec_count = req->elem.in_num - 3;
|
222 | 1063b8b1 | Christoph Hellwig | for (i = 0; i < hdr.iovec_count; i++) |
223 | 1063b8b1 | Christoph Hellwig | hdr.dxfer_len += req->elem.in_sg[i].iov_len; |
224 | 1063b8b1 | Christoph Hellwig | |
225 | 1063b8b1 | Christoph Hellwig | hdr.dxferp = req->elem.in_sg; |
226 | 1063b8b1 | Christoph Hellwig | size += hdr.dxfer_len; |
227 | 1063b8b1 | Christoph Hellwig | } else {
|
228 | 1063b8b1 | Christoph Hellwig | /*
|
229 | 1063b8b1 | Christoph Hellwig | * Some SCSI commands don't actually transfer any data.
|
230 | 1063b8b1 | Christoph Hellwig | */
|
231 | 1063b8b1 | Christoph Hellwig | hdr.dxfer_direction = SG_DXFER_NONE; |
232 | 1063b8b1 | Christoph Hellwig | } |
233 | 1063b8b1 | Christoph Hellwig | |
234 | 1063b8b1 | Christoph Hellwig | hdr.sbp = req->elem.in_sg[req->elem.in_num - 3].iov_base;
|
235 | 1063b8b1 | Christoph Hellwig | hdr.mx_sb_len = req->elem.in_sg[req->elem.in_num - 3].iov_len;
|
236 | 1063b8b1 | Christoph Hellwig | size += hdr.mx_sb_len; |
237 | 1063b8b1 | Christoph Hellwig | |
238 | 1063b8b1 | Christoph Hellwig | ret = bdrv_ioctl(req->dev->bs, SG_IO, &hdr); |
239 | 1063b8b1 | Christoph Hellwig | if (ret) {
|
240 | 1063b8b1 | Christoph Hellwig | status = VIRTIO_BLK_S_UNSUPP; |
241 | 1063b8b1 | Christoph Hellwig | hdr.status = ret; |
242 | 1063b8b1 | Christoph Hellwig | hdr.resid = hdr.dxfer_len; |
243 | 1063b8b1 | Christoph Hellwig | } else if (hdr.status) { |
244 | 1063b8b1 | Christoph Hellwig | status = VIRTIO_BLK_S_IOERR; |
245 | 1063b8b1 | Christoph Hellwig | } else {
|
246 | 1063b8b1 | Christoph Hellwig | status = VIRTIO_BLK_S_OK; |
247 | 1063b8b1 | Christoph Hellwig | } |
248 | 1063b8b1 | Christoph Hellwig | |
249 | 1063b8b1 | Christoph Hellwig | req->scsi->errors = hdr.status; |
250 | 1063b8b1 | Christoph Hellwig | req->scsi->residual = hdr.resid; |
251 | 1063b8b1 | Christoph Hellwig | req->scsi->sense_len = hdr.sb_len_wr; |
252 | 1063b8b1 | Christoph Hellwig | req->scsi->data_len = hdr.dxfer_len; |
253 | 1063b8b1 | Christoph Hellwig | |
254 | 1063b8b1 | Christoph Hellwig | virtio_blk_req_complete(req, status); |
255 | 1063b8b1 | Christoph Hellwig | } |
256 | 1063b8b1 | Christoph Hellwig | #else
|
257 | 1063b8b1 | Christoph Hellwig | static void virtio_blk_handle_scsi(VirtIOBlockReq *req) |
258 | 1063b8b1 | Christoph Hellwig | { |
259 | 1063b8b1 | Christoph Hellwig | virtio_blk_req_complete(req, VIRTIO_BLK_S_UNSUPP); |
260 | 1063b8b1 | Christoph Hellwig | } |
261 | 1063b8b1 | Christoph Hellwig | #endif /* __linux__ */ |
262 | 1063b8b1 | Christoph Hellwig | |
263 | 91553dcc | Kevin Wolf | static void do_multiwrite(BlockDriverState *bs, BlockRequest *blkreq, |
264 | 91553dcc | Kevin Wolf | int num_writes)
|
265 | 869a5c6d | aliguori | { |
266 | 91553dcc | Kevin Wolf | int i, ret;
|
267 | 91553dcc | Kevin Wolf | ret = bdrv_aio_multiwrite(bs, blkreq, num_writes); |
268 | 91553dcc | Kevin Wolf | |
269 | 91553dcc | Kevin Wolf | if (ret != 0) { |
270 | 91553dcc | Kevin Wolf | for (i = 0; i < num_writes; i++) { |
271 | 91553dcc | Kevin Wolf | if (blkreq[i].error) {
|
272 | 91553dcc | Kevin Wolf | virtio_blk_req_complete(blkreq[i].opaque, VIRTIO_BLK_S_IOERR); |
273 | 91553dcc | Kevin Wolf | } |
274 | 91553dcc | Kevin Wolf | } |
275 | 91553dcc | Kevin Wolf | } |
276 | 91553dcc | Kevin Wolf | } |
277 | 87b245db | Christoph Hellwig | |
278 | aa659be3 | Christoph Hellwig | static void virtio_blk_handle_flush(VirtIOBlockReq *req) |
279 | aa659be3 | Christoph Hellwig | { |
280 | aa659be3 | Christoph Hellwig | BlockDriverAIOCB *acb; |
281 | aa659be3 | Christoph Hellwig | |
282 | aa659be3 | Christoph Hellwig | acb = bdrv_aio_flush(req->dev->bs, virtio_blk_flush_complete, req); |
283 | aa659be3 | Christoph Hellwig | if (!acb) {
|
284 | aa659be3 | Christoph Hellwig | virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR); |
285 | aa659be3 | Christoph Hellwig | } |
286 | aa659be3 | Christoph Hellwig | } |
287 | aa659be3 | Christoph Hellwig | |
288 | 91553dcc | Kevin Wolf | static void virtio_blk_handle_write(BlockRequest *blkreq, int *num_writes, |
289 | 91553dcc | Kevin Wolf | VirtIOBlockReq *req, BlockDriverState **old_bs) |
290 | 91553dcc | Kevin Wolf | { |
291 | 91553dcc | Kevin Wolf | if (req->dev->bs != *old_bs || *num_writes == 32) { |
292 | 91553dcc | Kevin Wolf | if (*old_bs != NULL) { |
293 | 91553dcc | Kevin Wolf | do_multiwrite(*old_bs, blkreq, *num_writes); |
294 | 91553dcc | Kevin Wolf | } |
295 | 91553dcc | Kevin Wolf | *num_writes = 0;
|
296 | 91553dcc | Kevin Wolf | *old_bs = req->dev->bs; |
297 | 87b245db | Christoph Hellwig | } |
298 | 91553dcc | Kevin Wolf | |
299 | 91553dcc | Kevin Wolf | blkreq[*num_writes].sector = req->out->sector; |
300 | 91553dcc | Kevin Wolf | blkreq[*num_writes].nb_sectors = req->qiov.size / 512;
|
301 | 91553dcc | Kevin Wolf | blkreq[*num_writes].qiov = &req->qiov; |
302 | 91553dcc | Kevin Wolf | blkreq[*num_writes].cb = virtio_blk_rw_complete; |
303 | 91553dcc | Kevin Wolf | blkreq[*num_writes].opaque = req; |
304 | 91553dcc | Kevin Wolf | blkreq[*num_writes].error = 0;
|
305 | 91553dcc | Kevin Wolf | |
306 | 91553dcc | Kevin Wolf | (*num_writes)++; |
307 | d28a1b6e | aliguori | } |
308 | 869a5c6d | aliguori | |
309 | d28a1b6e | aliguori | static void virtio_blk_handle_read(VirtIOBlockReq *req) |
310 | d28a1b6e | aliguori | { |
311 | 87b245db | Christoph Hellwig | BlockDriverAIOCB *acb; |
312 | 87b245db | Christoph Hellwig | |
313 | 87b245db | Christoph Hellwig | acb = bdrv_aio_readv(req->dev->bs, req->out->sector, &req->qiov, |
314 | 87b245db | Christoph Hellwig | req->qiov.size / 512, virtio_blk_rw_complete, req);
|
315 | 87b245db | Christoph Hellwig | if (!acb) {
|
316 | 87b245db | Christoph Hellwig | virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR); |
317 | 87b245db | Christoph Hellwig | } |
318 | 869a5c6d | aliguori | } |
319 | 869a5c6d | aliguori | |
320 | 6e02c38d | aliguori | static void virtio_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq) |
321 | 6e02c38d | aliguori | { |
322 | 6e02c38d | aliguori | VirtIOBlock *s = to_virtio_blk(vdev); |
323 | 6e02c38d | aliguori | VirtIOBlockReq *req; |
324 | 91553dcc | Kevin Wolf | BlockRequest blkreq[32];
|
325 | 91553dcc | Kevin Wolf | int num_writes = 0; |
326 | 91553dcc | Kevin Wolf | BlockDriverState *old_bs = NULL;
|
327 | 6e02c38d | aliguori | |
328 | 6e02c38d | aliguori | while ((req = virtio_blk_get_request(s))) {
|
329 | 6e02c38d | aliguori | if (req->elem.out_num < 1 || req->elem.in_num < 1) { |
330 | 6e02c38d | aliguori | fprintf(stderr, "virtio-blk missing headers\n");
|
331 | 6e02c38d | aliguori | exit(1);
|
332 | 6e02c38d | aliguori | } |
333 | 6e02c38d | aliguori | |
334 | 6e02c38d | aliguori | if (req->elem.out_sg[0].iov_len < sizeof(*req->out) || |
335 | 6e02c38d | aliguori | req->elem.in_sg[req->elem.in_num - 1].iov_len < sizeof(*req->in)) { |
336 | 6e02c38d | aliguori | fprintf(stderr, "virtio-blk header not in correct element\n");
|
337 | 6e02c38d | aliguori | exit(1);
|
338 | 6e02c38d | aliguori | } |
339 | 6e02c38d | aliguori | |
340 | 6e02c38d | aliguori | req->out = (void *)req->elem.out_sg[0].iov_base; |
341 | 6e02c38d | aliguori | req->in = (void *)req->elem.in_sg[req->elem.in_num - 1].iov_base; |
342 | 6e02c38d | aliguori | |
343 | aa659be3 | Christoph Hellwig | if (req->out->type & VIRTIO_BLK_T_FLUSH) {
|
344 | aa659be3 | Christoph Hellwig | virtio_blk_handle_flush(req); |
345 | aa659be3 | Christoph Hellwig | } else if (req->out->type & VIRTIO_BLK_T_SCSI_CMD) { |
346 | 1063b8b1 | Christoph Hellwig | virtio_blk_handle_scsi(req); |
347 | 6e02c38d | aliguori | } else if (req->out->type & VIRTIO_BLK_T_OUT) { |
348 | d28a1b6e | aliguori | qemu_iovec_init_external(&req->qiov, &req->elem.out_sg[1],
|
349 | d28a1b6e | aliguori | req->elem.out_num - 1);
|
350 | 91553dcc | Kevin Wolf | virtio_blk_handle_write(blkreq, &num_writes, req, &old_bs); |
351 | 6e02c38d | aliguori | } else {
|
352 | d28a1b6e | aliguori | qemu_iovec_init_external(&req->qiov, &req->elem.in_sg[0],
|
353 | d28a1b6e | aliguori | req->elem.in_num - 1);
|
354 | d28a1b6e | aliguori | virtio_blk_handle_read(req); |
355 | 6e02c38d | aliguori | } |
356 | 6e02c38d | aliguori | } |
357 | 91553dcc | Kevin Wolf | |
358 | 91553dcc | Kevin Wolf | if (num_writes > 0) { |
359 | 91553dcc | Kevin Wolf | do_multiwrite(old_bs, blkreq, num_writes); |
360 | 91553dcc | Kevin Wolf | } |
361 | 91553dcc | Kevin Wolf | |
362 | 6e02c38d | aliguori | /*
|
363 | 6e02c38d | aliguori | * FIXME: Want to check for completions before returning to guest mode,
|
364 | 6e02c38d | aliguori | * so cached reads and writes are reported as quickly as possible. But
|
365 | 6e02c38d | aliguori | * that should be done in the generic block layer.
|
366 | 6e02c38d | aliguori | */
|
367 | 6e02c38d | aliguori | } |
368 | 6e02c38d | aliguori | |
369 | 213189ab | Markus Armbruster | static void virtio_blk_dma_restart_bh(void *opaque) |
370 | 869a5c6d | aliguori | { |
371 | 869a5c6d | aliguori | VirtIOBlock *s = opaque; |
372 | 869a5c6d | aliguori | VirtIOBlockReq *req = s->rq; |
373 | 869a5c6d | aliguori | |
374 | 213189ab | Markus Armbruster | qemu_bh_delete(s->bh); |
375 | 213189ab | Markus Armbruster | s->bh = NULL;
|
376 | 869a5c6d | aliguori | |
377 | 869a5c6d | aliguori | s->rq = NULL;
|
378 | 869a5c6d | aliguori | |
379 | 869a5c6d | aliguori | while (req) {
|
380 | 91553dcc | Kevin Wolf | bdrv_aio_writev(req->dev->bs, req->out->sector, &req->qiov, |
381 | 91553dcc | Kevin Wolf | req->qiov.size / 512, virtio_blk_rw_complete, req);
|
382 | 869a5c6d | aliguori | req = req->next; |
383 | 869a5c6d | aliguori | } |
384 | 869a5c6d | aliguori | } |
385 | 869a5c6d | aliguori | |
386 | 213189ab | Markus Armbruster | static void virtio_blk_dma_restart_cb(void *opaque, int running, int reason) |
387 | 213189ab | Markus Armbruster | { |
388 | 213189ab | Markus Armbruster | VirtIOBlock *s = opaque; |
389 | 213189ab | Markus Armbruster | |
390 | 213189ab | Markus Armbruster | if (!running)
|
391 | 213189ab | Markus Armbruster | return;
|
392 | 213189ab | Markus Armbruster | |
393 | 213189ab | Markus Armbruster | if (!s->bh) {
|
394 | 213189ab | Markus Armbruster | s->bh = qemu_bh_new(virtio_blk_dma_restart_bh, s); |
395 | 213189ab | Markus Armbruster | qemu_bh_schedule(s->bh); |
396 | 213189ab | Markus Armbruster | } |
397 | 213189ab | Markus Armbruster | } |
398 | 213189ab | Markus Armbruster | |
399 | 6e02c38d | aliguori | static void virtio_blk_reset(VirtIODevice *vdev) |
400 | 6e02c38d | aliguori | { |
401 | 6e02c38d | aliguori | /*
|
402 | 6e02c38d | aliguori | * This should cancel pending requests, but can't do nicely until there
|
403 | 6e02c38d | aliguori | * are per-device request lists.
|
404 | 6e02c38d | aliguori | */
|
405 | 6e02c38d | aliguori | qemu_aio_flush(); |
406 | 6e02c38d | aliguori | } |
407 | 6e02c38d | aliguori | |
408 | bf011293 | john cooper | /* coalesce internal state, copy to pci i/o region 0
|
409 | bf011293 | john cooper | */
|
410 | 6e02c38d | aliguori | static void virtio_blk_update_config(VirtIODevice *vdev, uint8_t *config) |
411 | 6e02c38d | aliguori | { |
412 | 6e02c38d | aliguori | VirtIOBlock *s = to_virtio_blk(vdev); |
413 | 6e02c38d | aliguori | struct virtio_blk_config blkcfg;
|
414 | 6e02c38d | aliguori | uint64_t capacity; |
415 | 6e02c38d | aliguori | int cylinders, heads, secs;
|
416 | 6e02c38d | aliguori | |
417 | 6e02c38d | aliguori | bdrv_get_geometry(s->bs, &capacity); |
418 | 6e02c38d | aliguori | bdrv_get_geometry_hint(s->bs, &cylinders, &heads, &secs); |
419 | 5c5dafdc | Gerd Hoffmann | memset(&blkcfg, 0, sizeof(blkcfg)); |
420 | 6e02c38d | aliguori | stq_raw(&blkcfg.capacity, capacity); |
421 | 6e02c38d | aliguori | stl_raw(&blkcfg.seg_max, 128 - 2); |
422 | 6e02c38d | aliguori | stw_raw(&blkcfg.cylinders, cylinders); |
423 | 6e02c38d | aliguori | blkcfg.heads = heads; |
424 | 6e02c38d | aliguori | blkcfg.sectors = secs; |
425 | c7085da7 | Blue Swirl | blkcfg.size_max = 0;
|
426 | bf011293 | john cooper | virtio_identify_template(&blkcfg); |
427 | bf011293 | john cooper | memcpy(&blkcfg.identify[VIRTIO_BLK_ID_SN], s->serial_str, |
428 | bf011293 | john cooper | VIRTIO_BLK_ID_SN_BYTES); |
429 | 711bf3d9 | Michael S. Tsirkin | memcpy(config, &blkcfg, s->config_size); |
430 | 6e02c38d | aliguori | } |
431 | 6e02c38d | aliguori | |
432 | 6e02c38d | aliguori | static uint32_t virtio_blk_get_features(VirtIODevice *vdev)
|
433 | 6e02c38d | aliguori | { |
434 | bf011293 | john cooper | VirtIOBlock *s = to_virtio_blk(vdev); |
435 | 1063b8b1 | Christoph Hellwig | uint32_t features = 0;
|
436 | 1063b8b1 | Christoph Hellwig | |
437 | 1063b8b1 | Christoph Hellwig | features |= (1 << VIRTIO_BLK_F_SEG_MAX);
|
438 | 1063b8b1 | Christoph Hellwig | features |= (1 << VIRTIO_BLK_F_GEOMETRY);
|
439 | aa659be3 | Christoph Hellwig | |
440 | aa659be3 | Christoph Hellwig | if (bdrv_enable_write_cache(s->bs))
|
441 | aa659be3 | Christoph Hellwig | features |= (1 << VIRTIO_BLK_F_WCACHE);
|
442 | 1063b8b1 | Christoph Hellwig | #ifdef __linux__
|
443 | 1063b8b1 | Christoph Hellwig | features |= (1 << VIRTIO_BLK_F_SCSI);
|
444 | 1063b8b1 | Christoph Hellwig | #endif
|
445 | bf011293 | john cooper | if (strcmp(s->serial_str, "0")) |
446 | bf011293 | john cooper | features |= 1 << VIRTIO_BLK_F_IDENTIFY;
|
447 | 1063b8b1 | Christoph Hellwig | |
448 | 1063b8b1 | Christoph Hellwig | return features;
|
449 | 6e02c38d | aliguori | } |
450 | 6e02c38d | aliguori | |
451 | 6e02c38d | aliguori | static void virtio_blk_save(QEMUFile *f, void *opaque) |
452 | 6e02c38d | aliguori | { |
453 | 6e02c38d | aliguori | VirtIOBlock *s = opaque; |
454 | 869a5c6d | aliguori | VirtIOBlockReq *req = s->rq; |
455 | 869a5c6d | aliguori | |
456 | 6e02c38d | aliguori | virtio_save(&s->vdev, f); |
457 | 869a5c6d | aliguori | |
458 | 869a5c6d | aliguori | while (req) {
|
459 | 869a5c6d | aliguori | qemu_put_sbyte(f, 1);
|
460 | 869a5c6d | aliguori | qemu_put_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem)); |
461 | 869a5c6d | aliguori | req = req->next; |
462 | 869a5c6d | aliguori | } |
463 | 869a5c6d | aliguori | qemu_put_sbyte(f, 0);
|
464 | 6e02c38d | aliguori | } |
465 | 6e02c38d | aliguori | |
466 | 6e02c38d | aliguori | static int virtio_blk_load(QEMUFile *f, void *opaque, int version_id) |
467 | 6e02c38d | aliguori | { |
468 | 6e02c38d | aliguori | VirtIOBlock *s = opaque; |
469 | 6e02c38d | aliguori | |
470 | 869a5c6d | aliguori | if (version_id != 2) |
471 | 6e02c38d | aliguori | return -EINVAL;
|
472 | 6e02c38d | aliguori | |
473 | 6e02c38d | aliguori | virtio_load(&s->vdev, f); |
474 | 869a5c6d | aliguori | while (qemu_get_sbyte(f)) {
|
475 | 869a5c6d | aliguori | VirtIOBlockReq *req = virtio_blk_alloc_request(s); |
476 | 869a5c6d | aliguori | qemu_get_buffer(f, (unsigned char*)&req->elem, sizeof(req->elem)); |
477 | 869a5c6d | aliguori | req->next = s->rq; |
478 | 869a5c6d | aliguori | s->rq = req->next; |
479 | 869a5c6d | aliguori | } |
480 | 6e02c38d | aliguori | |
481 | 6e02c38d | aliguori | return 0; |
482 | 6e02c38d | aliguori | } |
483 | 6e02c38d | aliguori | |
484 | d176c495 | Gerd Hoffmann | VirtIODevice *virtio_blk_init(DeviceState *dev, DriveInfo *dinfo) |
485 | 6e02c38d | aliguori | { |
486 | 6e02c38d | aliguori | VirtIOBlock *s; |
487 | 6e02c38d | aliguori | int cylinders, heads, secs;
|
488 | 6e02c38d | aliguori | static int virtio_blk_id; |
489 | 711bf3d9 | Michael S. Tsirkin | char *ps = (char *)drive_get_serial(dinfo->bdrv); |
490 | 711bf3d9 | Michael S. Tsirkin | size_t size = strlen(ps) ? sizeof(struct virtio_blk_config) : |
491 | 711bf3d9 | Michael S. Tsirkin | offsetof(struct virtio_blk_config, _blk_size);
|
492 | cf21e106 | Paul Brook | |
493 | 53c25cea | Paul Brook | s = (VirtIOBlock *)virtio_common_init("virtio-blk", VIRTIO_ID_BLOCK,
|
494 | 711bf3d9 | Michael S. Tsirkin | size, |
495 | 53c25cea | Paul Brook | sizeof(VirtIOBlock));
|
496 | 6e02c38d | aliguori | |
497 | 711bf3d9 | Michael S. Tsirkin | s->config_size = size; |
498 | 6e02c38d | aliguori | s->vdev.get_config = virtio_blk_update_config; |
499 | 6e02c38d | aliguori | s->vdev.get_features = virtio_blk_get_features; |
500 | 6e02c38d | aliguori | s->vdev.reset = virtio_blk_reset; |
501 | d176c495 | Gerd Hoffmann | s->bs = dinfo->bdrv; |
502 | 869a5c6d | aliguori | s->rq = NULL;
|
503 | 711bf3d9 | Michael S. Tsirkin | if (strlen(ps))
|
504 | bf011293 | john cooper | strncpy(s->serial_str, ps, sizeof(s->serial_str));
|
505 | bf011293 | john cooper | else
|
506 | bf011293 | john cooper | snprintf(s->serial_str, sizeof(s->serial_str), "0"); |
507 | d176c495 | Gerd Hoffmann | s->bs->private = dev; |
508 | 6e02c38d | aliguori | bdrv_guess_geometry(s->bs, &cylinders, &heads, &secs); |
509 | 6e02c38d | aliguori | bdrv_set_geometry_hint(s->bs, cylinders, heads, secs); |
510 | 6e02c38d | aliguori | |
511 | 6e02c38d | aliguori | s->vq = virtio_add_queue(&s->vdev, 128, virtio_blk_handle_output);
|
512 | 6e02c38d | aliguori | |
513 | 869a5c6d | aliguori | qemu_add_vm_change_state_handler(virtio_blk_dma_restart_cb, s); |
514 | 869a5c6d | aliguori | register_savevm("virtio-blk", virtio_blk_id++, 2, |
515 | 6e02c38d | aliguori | virtio_blk_save, virtio_blk_load, s); |
516 | 6e02c38d | aliguori | |
517 | 53c25cea | Paul Brook | return &s->vdev;
|
518 | 6e02c38d | aliguori | } |