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