2 * xen paravirt block device backend
4 * (c) Gerd Hoffmann <kraxel@redhat.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; under version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, see <http://www.gnu.org/licenses/>.
18 * Contributions after 2012-01-13 are licensed under the terms of the
19 * GNU GPL, version 2 or (at your option) any later version.
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
39 #include "qemu-char.h"
40 #include "xen_backend.h"
41 #include "xen_blkif.h"
44 /* ------------------------------------------------------------- */
46 static int batch_maps
= 0;
48 static int max_requests
= 32;
50 /* ------------------------------------------------------------- */
52 #define BLOCK_SIZE 512
53 #define IOCB_COUNT (BLKIF_MAX_SEGMENTS_PER_REQUEST + 2)
67 uint32_t domids
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
68 uint32_t refs
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
70 void *page
[BLKIF_MAX_SEGMENTS_PER_REQUEST
];
77 struct XenBlkDev
*blkdev
;
78 QLIST_ENTRY(ioreq
) list
;
83 struct XenDevice xendev
; /* must be first */
89 const char *fileproto
;
96 blkif_back_rings_t rings
;
101 QLIST_HEAD(inflight_head
, ioreq
) inflight
;
102 QLIST_HEAD(finished_head
, ioreq
) finished
;
103 QLIST_HEAD(freelist_head
, ioreq
) freelist
;
105 int requests_inflight
;
106 int requests_finished
;
108 /* qemu block driver */
110 BlockDriverState
*bs
;
114 /* ------------------------------------------------------------- */
116 static struct ioreq
*ioreq_start(struct XenBlkDev
*blkdev
)
118 struct ioreq
*ioreq
= NULL
;
120 if (QLIST_EMPTY(&blkdev
->freelist
)) {
121 if (blkdev
->requests_total
>= max_requests
) {
124 /* allocate new struct */
125 ioreq
= g_malloc0(sizeof(*ioreq
));
126 ioreq
->blkdev
= blkdev
;
127 blkdev
->requests_total
++;
128 qemu_iovec_init(&ioreq
->v
, BLKIF_MAX_SEGMENTS_PER_REQUEST
);
130 /* get one from freelist */
131 ioreq
= QLIST_FIRST(&blkdev
->freelist
);
132 QLIST_REMOVE(ioreq
, list
);
133 qemu_iovec_reset(&ioreq
->v
);
135 QLIST_INSERT_HEAD(&blkdev
->inflight
, ioreq
, list
);
136 blkdev
->requests_inflight
++;
142 static void ioreq_finish(struct ioreq
*ioreq
)
144 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
146 QLIST_REMOVE(ioreq
, list
);
147 QLIST_INSERT_HEAD(&blkdev
->finished
, ioreq
, list
);
148 blkdev
->requests_inflight
--;
149 blkdev
->requests_finished
++;
152 static void ioreq_release(struct ioreq
*ioreq
, bool finish
)
154 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
156 QLIST_REMOVE(ioreq
, list
);
157 memset(ioreq
, 0, sizeof(*ioreq
));
158 ioreq
->blkdev
= blkdev
;
159 QLIST_INSERT_HEAD(&blkdev
->freelist
, ioreq
, list
);
161 blkdev
->requests_finished
--;
163 blkdev
->requests_inflight
--;
168 * translate request into iovec + start offset
169 * do sanity checks along the way
171 static int ioreq_parse(struct ioreq
*ioreq
)
173 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
178 xen_be_printf(&blkdev
->xendev
, 3,
179 "op %d, nr %d, handle %d, id %" PRId64
", sector %" PRId64
"\n",
180 ioreq
->req
.operation
, ioreq
->req
.nr_segments
,
181 ioreq
->req
.handle
, ioreq
->req
.id
, ioreq
->req
.sector_number
);
182 switch (ioreq
->req
.operation
) {
184 ioreq
->prot
= PROT_WRITE
; /* to memory */
186 case BLKIF_OP_WRITE_BARRIER
:
187 if (!ioreq
->req
.nr_segments
) {
191 ioreq
->presync
= ioreq
->postsync
= 1;
194 ioreq
->prot
= PROT_READ
; /* from memory */
197 xen_be_printf(&blkdev
->xendev
, 0, "error: unknown operation (%d)\n",
198 ioreq
->req
.operation
);
202 if (ioreq
->req
.operation
!= BLKIF_OP_READ
&& blkdev
->mode
[0] != 'w') {
203 xen_be_printf(&blkdev
->xendev
, 0, "error: write req for ro device\n");
207 ioreq
->start
= ioreq
->req
.sector_number
* blkdev
->file_blk
;
208 for (i
= 0; i
< ioreq
->req
.nr_segments
; i
++) {
209 if (i
== BLKIF_MAX_SEGMENTS_PER_REQUEST
) {
210 xen_be_printf(&blkdev
->xendev
, 0, "error: nr_segments too big\n");
213 if (ioreq
->req
.seg
[i
].first_sect
> ioreq
->req
.seg
[i
].last_sect
) {
214 xen_be_printf(&blkdev
->xendev
, 0, "error: first > last sector\n");
217 if (ioreq
->req
.seg
[i
].last_sect
* BLOCK_SIZE
>= XC_PAGE_SIZE
) {
218 xen_be_printf(&blkdev
->xendev
, 0, "error: page crossing\n");
222 ioreq
->domids
[i
] = blkdev
->xendev
.dom
;
223 ioreq
->refs
[i
] = ioreq
->req
.seg
[i
].gref
;
225 mem
= ioreq
->req
.seg
[i
].first_sect
* blkdev
->file_blk
;
226 len
= (ioreq
->req
.seg
[i
].last_sect
- ioreq
->req
.seg
[i
].first_sect
+ 1) * blkdev
->file_blk
;
227 qemu_iovec_add(&ioreq
->v
, (void*)mem
, len
);
229 if (ioreq
->start
+ ioreq
->v
.size
> blkdev
->file_size
) {
230 xen_be_printf(&blkdev
->xendev
, 0, "error: access beyond end of file\n");
236 ioreq
->status
= BLKIF_RSP_ERROR
;
240 static void ioreq_unmap(struct ioreq
*ioreq
)
242 XenGnttab gnt
= ioreq
->blkdev
->xendev
.gnttabdev
;
245 if (ioreq
->v
.niov
== 0 || ioreq
->mapped
== 0) {
252 if (xc_gnttab_munmap(gnt
, ioreq
->pages
, ioreq
->v
.niov
) != 0) {
253 xen_be_printf(&ioreq
->blkdev
->xendev
, 0, "xc_gnttab_munmap failed: %s\n",
256 ioreq
->blkdev
->cnt_map
-= ioreq
->v
.niov
;
259 for (i
= 0; i
< ioreq
->v
.niov
; i
++) {
260 if (!ioreq
->page
[i
]) {
263 if (xc_gnttab_munmap(gnt
, ioreq
->page
[i
], 1) != 0) {
264 xen_be_printf(&ioreq
->blkdev
->xendev
, 0, "xc_gnttab_munmap failed: %s\n",
267 ioreq
->blkdev
->cnt_map
--;
268 ioreq
->page
[i
] = NULL
;
274 static int ioreq_map(struct ioreq
*ioreq
)
276 XenGnttab gnt
= ioreq
->blkdev
->xendev
.gnttabdev
;
279 if (ioreq
->v
.niov
== 0 || ioreq
->mapped
== 1) {
283 ioreq
->pages
= xc_gnttab_map_grant_refs
284 (gnt
, ioreq
->v
.niov
, ioreq
->domids
, ioreq
->refs
, ioreq
->prot
);
285 if (ioreq
->pages
== NULL
) {
286 xen_be_printf(&ioreq
->blkdev
->xendev
, 0,
287 "can't map %d grant refs (%s, %d maps)\n",
288 ioreq
->v
.niov
, strerror(errno
), ioreq
->blkdev
->cnt_map
);
291 for (i
= 0; i
< ioreq
->v
.niov
; i
++) {
292 ioreq
->v
.iov
[i
].iov_base
= ioreq
->pages
+ i
* XC_PAGE_SIZE
+
293 (uintptr_t)ioreq
->v
.iov
[i
].iov_base
;
295 ioreq
->blkdev
->cnt_map
+= ioreq
->v
.niov
;
297 for (i
= 0; i
< ioreq
->v
.niov
; i
++) {
298 ioreq
->page
[i
] = xc_gnttab_map_grant_ref
299 (gnt
, ioreq
->domids
[i
], ioreq
->refs
[i
], ioreq
->prot
);
300 if (ioreq
->page
[i
] == NULL
) {
301 xen_be_printf(&ioreq
->blkdev
->xendev
, 0,
302 "can't map grant ref %d (%s, %d maps)\n",
303 ioreq
->refs
[i
], strerror(errno
), ioreq
->blkdev
->cnt_map
);
307 ioreq
->v
.iov
[i
].iov_base
= ioreq
->page
[i
] + (uintptr_t)ioreq
->v
.iov
[i
].iov_base
;
308 ioreq
->blkdev
->cnt_map
++;
315 static int ioreq_runio_qemu_aio(struct ioreq
*ioreq
);
317 static void qemu_aio_complete(void *opaque
, int ret
)
319 struct ioreq
*ioreq
= opaque
;
322 xen_be_printf(&ioreq
->blkdev
->xendev
, 0, "%s I/O error\n",
323 ioreq
->req
.operation
== BLKIF_OP_READ
? "read" : "write");
327 ioreq
->aio_inflight
--;
328 if (ioreq
->presync
) {
330 ioreq_runio_qemu_aio(ioreq
);
333 if (ioreq
->aio_inflight
> 0) {
336 if (ioreq
->postsync
) {
338 ioreq
->aio_inflight
++;
339 bdrv_aio_flush(ioreq
->blkdev
->bs
, qemu_aio_complete
, ioreq
);
343 ioreq
->status
= ioreq
->aio_errors
? BLKIF_RSP_ERROR
: BLKIF_RSP_OKAY
;
346 bdrv_acct_done(ioreq
->blkdev
->bs
, &ioreq
->acct
);
347 qemu_bh_schedule(ioreq
->blkdev
->bh
);
350 static int ioreq_runio_qemu_aio(struct ioreq
*ioreq
)
352 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
354 if (ioreq
->req
.nr_segments
&& ioreq_map(ioreq
) == -1) {
358 ioreq
->aio_inflight
++;
359 if (ioreq
->presync
) {
360 bdrv_aio_flush(ioreq
->blkdev
->bs
, qemu_aio_complete
, ioreq
);
364 switch (ioreq
->req
.operation
) {
366 bdrv_acct_start(blkdev
->bs
, &ioreq
->acct
, ioreq
->v
.size
, BDRV_ACCT_READ
);
367 ioreq
->aio_inflight
++;
368 bdrv_aio_readv(blkdev
->bs
, ioreq
->start
/ BLOCK_SIZE
,
369 &ioreq
->v
, ioreq
->v
.size
/ BLOCK_SIZE
,
370 qemu_aio_complete
, ioreq
);
373 case BLKIF_OP_WRITE_BARRIER
:
374 if (!ioreq
->req
.nr_segments
) {
378 bdrv_acct_start(blkdev
->bs
, &ioreq
->acct
, ioreq
->v
.size
, BDRV_ACCT_WRITE
);
379 ioreq
->aio_inflight
++;
380 bdrv_aio_writev(blkdev
->bs
, ioreq
->start
/ BLOCK_SIZE
,
381 &ioreq
->v
, ioreq
->v
.size
/ BLOCK_SIZE
,
382 qemu_aio_complete
, ioreq
);
385 /* unknown operation (shouldn't happen -- parse catches this) */
389 qemu_aio_complete(ioreq
, 0);
397 ioreq
->status
= BLKIF_RSP_ERROR
;
401 static int blk_send_response_one(struct ioreq
*ioreq
)
403 struct XenBlkDev
*blkdev
= ioreq
->blkdev
;
405 int have_requests
= 0;
406 blkif_response_t resp
;
409 resp
.id
= ioreq
->req
.id
;
410 resp
.operation
= ioreq
->req
.operation
;
411 resp
.status
= ioreq
->status
;
413 /* Place on the response ring for the relevant domain. */
414 switch (blkdev
->protocol
) {
415 case BLKIF_PROTOCOL_NATIVE
:
416 dst
= RING_GET_RESPONSE(&blkdev
->rings
.native
, blkdev
->rings
.native
.rsp_prod_pvt
);
418 case BLKIF_PROTOCOL_X86_32
:
419 dst
= RING_GET_RESPONSE(&blkdev
->rings
.x86_32_part
,
420 blkdev
->rings
.x86_32_part
.rsp_prod_pvt
);
422 case BLKIF_PROTOCOL_X86_64
:
423 dst
= RING_GET_RESPONSE(&blkdev
->rings
.x86_64_part
,
424 blkdev
->rings
.x86_64_part
.rsp_prod_pvt
);
429 memcpy(dst
, &resp
, sizeof(resp
));
430 blkdev
->rings
.common
.rsp_prod_pvt
++;
432 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blkdev
->rings
.common
, send_notify
);
433 if (blkdev
->rings
.common
.rsp_prod_pvt
== blkdev
->rings
.common
.req_cons
) {
435 * Tail check for pending requests. Allows frontend to avoid
436 * notifications if requests are already in flight (lower
437 * overheads and promotes batching).
439 RING_FINAL_CHECK_FOR_REQUESTS(&blkdev
->rings
.common
, have_requests
);
440 } else if (RING_HAS_UNCONSUMED_REQUESTS(&blkdev
->rings
.common
)) {
450 /* walk finished list, send outstanding responses, free requests */
451 static void blk_send_response_all(struct XenBlkDev
*blkdev
)
456 while (!QLIST_EMPTY(&blkdev
->finished
)) {
457 ioreq
= QLIST_FIRST(&blkdev
->finished
);
458 send_notify
+= blk_send_response_one(ioreq
);
459 ioreq_release(ioreq
, true);
462 xen_be_send_notify(&blkdev
->xendev
);
466 static int blk_get_request(struct XenBlkDev
*blkdev
, struct ioreq
*ioreq
, RING_IDX rc
)
468 switch (blkdev
->protocol
) {
469 case BLKIF_PROTOCOL_NATIVE
:
470 memcpy(&ioreq
->req
, RING_GET_REQUEST(&blkdev
->rings
.native
, rc
),
473 case BLKIF_PROTOCOL_X86_32
:
474 blkif_get_x86_32_req(&ioreq
->req
,
475 RING_GET_REQUEST(&blkdev
->rings
.x86_32_part
, rc
));
477 case BLKIF_PROTOCOL_X86_64
:
478 blkif_get_x86_64_req(&ioreq
->req
,
479 RING_GET_REQUEST(&blkdev
->rings
.x86_64_part
, rc
));
485 static void blk_handle_requests(struct XenBlkDev
*blkdev
)
490 blkdev
->more_work
= 0;
492 rc
= blkdev
->rings
.common
.req_cons
;
493 rp
= blkdev
->rings
.common
.sring
->req_prod
;
494 xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
496 blk_send_response_all(blkdev
);
498 /* pull request from ring */
499 if (RING_REQUEST_CONS_OVERFLOW(&blkdev
->rings
.common
, rc
)) {
502 ioreq
= ioreq_start(blkdev
);
507 blk_get_request(blkdev
, ioreq
, rc
);
508 blkdev
->rings
.common
.req_cons
= ++rc
;
511 if (ioreq_parse(ioreq
) != 0) {
512 if (blk_send_response_one(ioreq
)) {
513 xen_be_send_notify(&blkdev
->xendev
);
515 ioreq_release(ioreq
, false);
519 ioreq_runio_qemu_aio(ioreq
);
522 if (blkdev
->more_work
&& blkdev
->requests_inflight
< max_requests
) {
523 qemu_bh_schedule(blkdev
->bh
);
527 /* ------------------------------------------------------------- */
529 static void blk_bh(void *opaque
)
531 struct XenBlkDev
*blkdev
= opaque
;
532 blk_handle_requests(blkdev
);
536 * We need to account for the grant allocations requiring contiguous
537 * chunks; the worst case number would be
538 * max_req * max_seg + (max_req - 1) * (max_seg - 1) + 1,
539 * but in order to keep things simple just use
540 * 2 * max_req * max_seg.
542 #define MAX_GRANTS(max_req, max_seg) (2 * (max_req) * (max_seg))
544 static void blk_alloc(struct XenDevice
*xendev
)
546 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
548 QLIST_INIT(&blkdev
->inflight
);
549 QLIST_INIT(&blkdev
->finished
);
550 QLIST_INIT(&blkdev
->freelist
);
551 blkdev
->bh
= qemu_bh_new(blk_bh
, blkdev
);
552 if (xen_mode
!= XEN_EMULATE
) {
555 if (xc_gnttab_set_max_grants(xendev
->gnttabdev
,
556 MAX_GRANTS(max_requests
, BLKIF_MAX_SEGMENTS_PER_REQUEST
)) < 0) {
557 xen_be_printf(xendev
, 0, "xc_gnttab_set_max_grants failed: %s\n",
562 static int blk_init(struct XenDevice
*xendev
)
564 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
565 int index
, qflags
, info
= 0;
567 /* read xenstore entries */
568 if (blkdev
->params
== NULL
) {
570 blkdev
->params
= xenstore_read_be_str(&blkdev
->xendev
, "params");
571 if (blkdev
->params
!= NULL
) {
572 h
= strchr(blkdev
->params
, ':');
575 blkdev
->fileproto
= blkdev
->params
;
576 blkdev
->filename
= h
+1;
579 blkdev
->fileproto
= "<unset>";
580 blkdev
->filename
= blkdev
->params
;
583 if (!strcmp("aio", blkdev
->fileproto
)) {
584 blkdev
->fileproto
= "raw";
586 if (blkdev
->mode
== NULL
) {
587 blkdev
->mode
= xenstore_read_be_str(&blkdev
->xendev
, "mode");
589 if (blkdev
->type
== NULL
) {
590 blkdev
->type
= xenstore_read_be_str(&blkdev
->xendev
, "type");
592 if (blkdev
->dev
== NULL
) {
593 blkdev
->dev
= xenstore_read_be_str(&blkdev
->xendev
, "dev");
595 if (blkdev
->devtype
== NULL
) {
596 blkdev
->devtype
= xenstore_read_be_str(&blkdev
->xendev
, "device-type");
599 /* do we have all we need? */
600 if (blkdev
->params
== NULL
||
601 blkdev
->mode
== NULL
||
602 blkdev
->type
== NULL
||
603 blkdev
->dev
== NULL
) {
608 qflags
= BDRV_O_NOCACHE
| BDRV_O_CACHE_WB
| BDRV_O_NATIVE_AIO
;
609 if (strcmp(blkdev
->mode
, "w") == 0) {
610 qflags
|= BDRV_O_RDWR
;
612 info
|= VDISK_READONLY
;
616 if (blkdev
->devtype
&& !strcmp(blkdev
->devtype
, "cdrom")) {
620 /* init qemu block driver */
621 index
= (blkdev
->xendev
.dev
- 202 * 256) / 16;
622 blkdev
->dinfo
= drive_get(IF_XEN
, 0, index
);
623 if (!blkdev
->dinfo
) {
624 /* setup via xenbus -> create new block driver instance */
625 xen_be_printf(&blkdev
->xendev
, 2, "create new bdrv (xenbus setup)\n");
626 blkdev
->bs
= bdrv_new(blkdev
->dev
);
628 if (bdrv_open(blkdev
->bs
, blkdev
->filename
, qflags
,
629 bdrv_find_whitelisted_format(blkdev
->fileproto
)) != 0) {
630 bdrv_delete(blkdev
->bs
);
638 /* setup via qemu cmdline -> already setup for us */
639 xen_be_printf(&blkdev
->xendev
, 2, "get configured bdrv (cmdline setup)\n");
640 blkdev
->bs
= blkdev
->dinfo
->bdrv
;
642 bdrv_attach_dev_nofail(blkdev
->bs
, blkdev
);
643 blkdev
->file_blk
= BLOCK_SIZE
;
644 blkdev
->file_size
= bdrv_getlength(blkdev
->bs
);
645 if (blkdev
->file_size
< 0) {
646 xen_be_printf(&blkdev
->xendev
, 1, "bdrv_getlength: %d (%s) | drv %s\n",
647 (int)blkdev
->file_size
, strerror(-blkdev
->file_size
),
648 bdrv_get_format_name(blkdev
->bs
) ?: "-");
649 blkdev
->file_size
= 0;
652 xen_be_printf(xendev
, 1, "type \"%s\", fileproto \"%s\", filename \"%s\","
653 " size %" PRId64
" (%" PRId64
" MB)\n",
654 blkdev
->type
, blkdev
->fileproto
, blkdev
->filename
,
655 blkdev
->file_size
, blkdev
->file_size
>> 20);
658 xenstore_write_be_int(&blkdev
->xendev
, "feature-barrier", 1);
659 xenstore_write_be_int(&blkdev
->xendev
, "info", info
);
660 xenstore_write_be_int(&blkdev
->xendev
, "sector-size", blkdev
->file_blk
);
661 xenstore_write_be_int(&blkdev
->xendev
, "sectors",
662 blkdev
->file_size
/ blkdev
->file_blk
);
666 g_free(blkdev
->params
);
667 blkdev
->params
= NULL
;
668 g_free(blkdev
->mode
);
670 g_free(blkdev
->type
);
674 g_free(blkdev
->devtype
);
675 blkdev
->devtype
= NULL
;
679 static int blk_connect(struct XenDevice
*xendev
)
681 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
683 if (xenstore_read_fe_int(&blkdev
->xendev
, "ring-ref", &blkdev
->ring_ref
) == -1) {
686 if (xenstore_read_fe_int(&blkdev
->xendev
, "event-channel",
687 &blkdev
->xendev
.remote_port
) == -1) {
691 blkdev
->protocol
= BLKIF_PROTOCOL_NATIVE
;
692 if (blkdev
->xendev
.protocol
) {
693 if (strcmp(blkdev
->xendev
.protocol
, XEN_IO_PROTO_ABI_X86_32
) == 0) {
694 blkdev
->protocol
= BLKIF_PROTOCOL_X86_32
;
696 if (strcmp(blkdev
->xendev
.protocol
, XEN_IO_PROTO_ABI_X86_64
) == 0) {
697 blkdev
->protocol
= BLKIF_PROTOCOL_X86_64
;
701 blkdev
->sring
= xc_gnttab_map_grant_ref(blkdev
->xendev
.gnttabdev
,
704 PROT_READ
| PROT_WRITE
);
705 if (!blkdev
->sring
) {
710 switch (blkdev
->protocol
) {
711 case BLKIF_PROTOCOL_NATIVE
:
713 blkif_sring_t
*sring_native
= blkdev
->sring
;
714 BACK_RING_INIT(&blkdev
->rings
.native
, sring_native
, XC_PAGE_SIZE
);
717 case BLKIF_PROTOCOL_X86_32
:
719 blkif_x86_32_sring_t
*sring_x86_32
= blkdev
->sring
;
721 BACK_RING_INIT(&blkdev
->rings
.x86_32_part
, sring_x86_32
, XC_PAGE_SIZE
);
724 case BLKIF_PROTOCOL_X86_64
:
726 blkif_x86_64_sring_t
*sring_x86_64
= blkdev
->sring
;
728 BACK_RING_INIT(&blkdev
->rings
.x86_64_part
, sring_x86_64
, XC_PAGE_SIZE
);
733 xen_be_bind_evtchn(&blkdev
->xendev
);
735 xen_be_printf(&blkdev
->xendev
, 1, "ok: proto %s, ring-ref %d, "
736 "remote port %d, local port %d\n",
737 blkdev
->xendev
.protocol
, blkdev
->ring_ref
,
738 blkdev
->xendev
.remote_port
, blkdev
->xendev
.local_port
);
742 static void blk_disconnect(struct XenDevice
*xendev
)
744 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
747 if (!blkdev
->dinfo
) {
748 /* close/delete only if we created it ourself */
749 bdrv_close(blkdev
->bs
);
750 bdrv_detach_dev(blkdev
->bs
, blkdev
);
751 bdrv_delete(blkdev
->bs
);
755 xen_be_unbind_evtchn(&blkdev
->xendev
);
758 xc_gnttab_munmap(blkdev
->xendev
.gnttabdev
, blkdev
->sring
, 1);
760 blkdev
->sring
= NULL
;
764 static int blk_free(struct XenDevice
*xendev
)
766 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
769 if (blkdev
->bs
|| blkdev
->sring
) {
770 blk_disconnect(xendev
);
773 while (!QLIST_EMPTY(&blkdev
->freelist
)) {
774 ioreq
= QLIST_FIRST(&blkdev
->freelist
);
775 QLIST_REMOVE(ioreq
, list
);
776 qemu_iovec_destroy(&ioreq
->v
);
780 g_free(blkdev
->params
);
781 g_free(blkdev
->mode
);
782 g_free(blkdev
->type
);
784 g_free(blkdev
->devtype
);
785 qemu_bh_delete(blkdev
->bh
);
789 static void blk_event(struct XenDevice
*xendev
)
791 struct XenBlkDev
*blkdev
= container_of(xendev
, struct XenBlkDev
, xendev
);
793 qemu_bh_schedule(blkdev
->bh
);
796 struct XenDevOps xen_blkdev_ops
= {
797 .size
= sizeof(struct XenBlkDev
),
798 .flags
= DEVOPS_FLAG_NEED_GNTDEV
,
801 .initialise
= blk_connect
,
802 .disconnect
= blk_disconnect
,