From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 05/10] dma-helpers: add dma_buf_read and dma_buf_write
Date: Thu, 4 Aug 2011 19:14:43 +0200 [thread overview]
Message-ID: <1312478089-806-6-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1312478089-806-1-git-send-email-pbonzini@redhat.com>
These helpers do a full transfer from an in-memory buffer to
target memory, with full support for MMIO areas. It will be used to store
the reply of an emulated command into a QEMUSGList provided by the
adapter.
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
cutils.c | 8 +++---
dma-helpers.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
dma.h | 5 ++++
3 files changed, 72 insertions(+), 4 deletions(-)
diff --git a/cutils.c b/cutils.c
index f9a7e36..969fd2e 100644
--- a/cutils.c
+++ b/cutils.c
@@ -215,14 +215,14 @@ void qemu_iovec_concat(QEMUIOVector *dst, QEMUIOVector *src, size_t size)
void qemu_iovec_destroy(QEMUIOVector *qiov)
{
- assert(qiov->nalloc != -1);
-
- qemu_free(qiov->iov);
+ if (qiov->nalloc != -1) {
+ qemu_free(qiov->iov);
+ }
}
void qemu_iovec_reset(QEMUIOVector *qiov)
{
- assert(qiov->nalloc != -1);
+ assert(qiov->nalloc != -1 || qiov->niov == 0);
qiov->niov = 0;
qiov->size = 0;
diff --git a/dma-helpers.c b/dma-helpers.c
index 4469ce2..10dc8ca 100644
--- a/dma-helpers.c
+++ b/dma-helpers.c
@@ -84,6 +84,7 @@ struct DMAAIOCB {
BlockDriverAIOCB common;
union {
BlockDriverState *bs;
+ uint8_t *ptr;
} u;
BlockDriverAIOCB *acb;
QEMUSGList *sg;
@@ -245,3 +246,65 @@ BlockDriverAIOCB *dma_bdrv_write(BlockDriverState *bs,
{
return dma_bdrv_io(bs, sg, sector, bdrv_aio_writev, cb, opaque, 1);
}
+
+
+static int dma_copy_cb(DMAAIOCB *dbs, int ret)
+{
+ void *mem;
+ target_phys_addr_t cur_len;
+
+ assert(ret == 0);
+
+ /* sector_num is the residual number of bytes to copy. */
+ while (dbs->sector_num > 0 &&
+ (mem = qemu_sglist_map_segment(dbs->sg, &cur_len,
+ !dbs->is_write)) != NULL) {
+ if (dbs->is_write) {
+ memcpy(dbs->u.ptr, mem, cur_len);
+ } else {
+ memcpy(mem, dbs->u.ptr, cur_len);
+ }
+ cpu_physical_memory_unmap(mem, cur_len, !dbs->is_write, cur_len);
+ dbs->u.ptr += cur_len;
+ dbs->sector_num -= cur_len;
+ }
+
+ if (dbs->sg->resid > 0) {
+ cpu_register_map_client(dbs, continue_after_map_failure);
+ return -EAGAIN;
+ }
+
+ return 0;
+}
+
+static BlockDriverAIOCB *dma_copy(
+ uint8_t *ptr, int32_t len, QEMUSGList *sg,
+ void (*cb)(void *opaque, int ret), void *opaque, int is_write)
+{
+ DMAAIOCB *dbs = qemu_aio_get(&dma_aio_pool, NULL, cb, opaque);
+
+ dbs->acb = NULL;
+ dbs->u.ptr = ptr;
+ dbs->sg = sg;
+ dbs->sector_num = MIN(len, sg->size);
+ dbs->is_write = is_write;
+ dbs->bh = NULL;
+ dbs->cb = dma_copy_cb;
+ qemu_iovec_init_external(&dbs->iov, NULL, 0);
+ qemu_sglist_rewind(sg);
+ return dma_continue(dbs, 0);
+}
+
+BlockDriverAIOCB *dma_buf_read(
+ uint8_t *ptr, int32_t len, QEMUSGList *sg,
+ void (*cb)(void *opaque, int ret), void *opaque)
+{
+ return dma_copy(ptr, len, sg, cb, opaque, 0);
+}
+
+BlockDriverAIOCB *dma_buf_write(
+ uint8_t *ptr, int32_t len, QEMUSGList *sg,
+ void (*cb)(void *opaque, int ret), void *opaque)
+{
+ return dma_copy(ptr, len, sg, cb, opaque, 1);
+}
diff --git a/dma.h b/dma.h
index 363e932..6bf51ee 100644
--- a/dma.h
+++ b/dma.h
@@ -58,4 +58,9 @@ BlockDriverAIOCB *dma_bdrv_read(BlockDriverState *bs,
BlockDriverAIOCB *dma_bdrv_write(BlockDriverState *bs,
QEMUSGList *sg, uint64_t sector,
BlockDriverCompletionFunc *cb, void *opaque);
+BlockDriverAIOCB *dma_buf_read(uint8_t *ptr, int32_t len, QEMUSGList *sg,
+ BlockDriverCompletionFunc *cb, void *opaque);
+BlockDriverAIOCB *dma_buf_write(uint8_t *ptr, int32_t len, QEMUSGList *sg,
+ BlockDriverCompletionFunc *cb, void *opaque);
+
#endif
--
1.7.6
next prev parent reply other threads:[~2011-08-04 17:15 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-08-04 17:14 [Qemu-devel] [PATCH 00/10] SCSI scatter/gather support Paolo Bonzini
2011-08-04 17:14 ` [Qemu-devel] [PATCH 01/10] dma-helpers: allow including from target-independent code Paolo Bonzini
2011-08-04 17:14 ` [Qemu-devel] [PATCH 02/10] dma-helpers: track position in the QEMUSGList Paolo Bonzini
2011-08-04 17:14 ` [Qemu-devel] [PATCH 03/10] dma-helpers: rewrite completion/cancellation Paolo Bonzini
2011-08-04 17:14 ` [Qemu-devel] [PATCH 04/10] dma-helpers: prepare for adding dma_buf_* functions Paolo Bonzini
2011-08-04 17:14 ` Paolo Bonzini [this message]
2011-08-11 7:58 ` [Qemu-devel] [PATCH 05/10] dma-helpers: add dma_buf_read and dma_buf_write Stefan Hajnoczi
2011-08-11 12:10 ` Paolo Bonzini
2011-08-11 13:29 ` Stefan Hajnoczi
2011-08-11 14:24 ` Paolo Bonzini
2011-08-11 14:37 ` Kevin Wolf
2011-08-11 15:05 ` Paolo Bonzini
2011-08-11 15:12 ` Kevin Wolf
2011-08-11 15:27 ` Paolo Bonzini
2011-08-11 20:06 ` Stefan Hajnoczi
2011-08-04 17:14 ` [Qemu-devel] [PATCH 06/10] scsi: pass residual amount to command_complete Paolo Bonzini
2011-08-04 17:14 ` [Qemu-devel] [PATCH 07/10] scsi: add scatter/gather functionality Paolo Bonzini
2011-08-04 17:14 ` [Qemu-devel] [PATCH 08/10] scsi-disk: commonize iovec creation between reads and writes Paolo Bonzini
2011-08-04 17:14 ` [Qemu-devel] [PATCH 09/10] scsi-disk: lazily allocate bounce buffer Paolo Bonzini
2011-08-04 17:14 ` [Qemu-devel] [PATCH 10/10] scsi-disk: enable scatter/gather functionality Paolo Bonzini
2011-08-04 17:14 ` [Qemu-devel] [PATCH 11/11] sample pvscsi driver with s/g support Paolo Bonzini
2011-08-11 7:57 ` [Qemu-devel] [PATCH 00/10] SCSI scatter/gather support Stefan Hajnoczi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1312478089-806-6-git-send-email-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).