The mapping of scatter-gather lists from physical addresses (as perceived by the guest kernel) to the virtual address space of the QEMU process is a vital step for a DMA operation. This step is currently implemented, amongst other things, in dma_blk_cb(), making it impossible to be used by anyone else.
In order to pave the way for the DMA support of ioctl commands, expose the aforementioned logic in a separate function called "dma_map_sg". Also, expose some other important pieces too, such as the initialization of the dbs structure. Signed-off-by: Alex Pyrgiotis <apyr...@arrikto.com> Signed-off-by: Dimitris Aragiorgis <dim...@arrikto.com> diff --git a/dma-helpers.c b/dma-helpers.c index 4faec5d..c38661e 100644 --- a/dma-helpers.c +++ b/dma-helpers.c @@ -79,9 +79,10 @@ typedef struct { QEMUIOVector iov; QEMUBH *bh; DMAIOFunc *io_func; + BlockCompletionFunc *dma_cb; } DMAAIOCB; -static void dma_blk_cb(void *opaque, int ret); +static void dma_blk_io_cb(void *opaque, int ret); static void reschedule_dma(void *opaque) { @@ -89,7 +90,7 @@ static void reschedule_dma(void *opaque) qemu_bh_delete(dbs->bh); dbs->bh = NULL; - dma_blk_cb(dbs, 0); + dbs->dma_cb(dbs, 0); } static void dma_blk_unmap(DMAAIOCB *dbs) @@ -120,21 +121,19 @@ static void dma_complete(DMAAIOCB *dbs, int ret) qemu_aio_unref(dbs); } -static void dma_blk_cb(void *opaque, int ret) +/* + * Create a QEMUIOVector from a scatter-gather list. + * + * This function does not copy the data of the scatter-gather list. Instead, it + * uses the dma_memory_map() function to map physical memory regions of the + * virtual device (as interpreted by the guest kernel) into the address space + * of the QEMU process, in order to have access to the data. + */ +static void dma_map_sg(DMAAIOCB *dbs) { - DMAAIOCB *dbs = (DMAAIOCB *)opaque; dma_addr_t cur_addr, cur_len; void *mem; - trace_dma_blk_cb(dbs, ret); - - dbs->acb = NULL; - dbs->sector_num += dbs->iov.size / 512; - - if (dbs->sg_cur_index == dbs->sg->nsg || ret < 0) { - dma_complete(dbs, ret); - return; - } dma_blk_unmap(dbs); while (dbs->sg_cur_index < dbs->sg->nsg) { @@ -162,9 +161,38 @@ static void dma_blk_cb(void *opaque, int ret) if (dbs->iov.size & ~BDRV_SECTOR_MASK) { qemu_iovec_discard_back(&dbs->iov, dbs->iov.size & ~BDRV_SECTOR_MASK); } +} + +/* + * Callback function for DMA read/write operations. + * + * This function initiates the read/write operation and also acts as a + * completion callback. It uses the dma_map_sg() function to map the + * scatter-gather list to a QEMUIOVector and then passes this iovec to the + * underlying read/write I/O function. + * + * If the DMA operation cannot take place in one step, e.g. it couldn't map all + * the scatter-gather entries, then this function will do a partial I/O + * operation and once done, it will be called and will retry the I/O operation. + */ +static void dma_blk_io_cb(void *opaque, int ret) +{ + DMAAIOCB *dbs = (DMAAIOCB *)opaque; + + trace_dma_blk_io_cb(dbs, ret); + + dbs->acb = NULL; + dbs->sector_num += dbs->iov.size / 512; + + if (dbs->sg_cur_index == dbs->sg->nsg || ret < 0) { + dma_complete(dbs, ret); + return; + } + + dma_map_sg(dbs); dbs->acb = dbs->io_func(dbs->blk, dbs->sector_num, &dbs->iov, - dbs->iov.size / 512, dma_blk_cb, dbs); + dbs->iov.size / 512, dma_blk_io_cb, dbs); assert(dbs->acb); } @@ -190,6 +218,22 @@ static const AIOCBInfo dma_aiocb_info = { .cancel_async = dma_aio_cancel, }; +/* + * Initialize the dbs structure and the QEMUIOVector to sane defaults. + */ +static void dma_init_dbs(DMAAIOCB *dbs, BlockBackend *blk, QEMUSGList *sg, + DMADirection dir) +{ + dbs->acb = NULL; + dbs->blk = blk; + dbs->sg = sg; + dbs->sg_cur_index = 0; + dbs->sg_cur_byte = 0; + dbs->dir = dir; + dbs->bh = NULL; + qemu_iovec_init(&dbs->iov, sg->nsg); +} + BlockAIOCB *dma_blk_io( BlockBackend *blk, QEMUSGList *sg, uint64_t sector_num, DMAIOFunc *io_func, BlockCompletionFunc *cb, @@ -199,21 +243,14 @@ BlockAIOCB *dma_blk_io( trace_dma_blk_io(dbs, blk, sector_num, (dir == DMA_DIRECTION_TO_DEVICE)); - dbs->acb = NULL; - dbs->blk = blk; - dbs->sg = sg; + dma_init_dbs(dbs, blk, sg, dir); dbs->sector_num = sector_num; - dbs->sg_cur_index = 0; - dbs->sg_cur_byte = 0; - dbs->dir = dir; dbs->io_func = io_func; - dbs->bh = NULL; - qemu_iovec_init(&dbs->iov, sg->nsg); - dma_blk_cb(dbs, 0); + dbs->dma_cb = dma_blk_io_cb; + dbs->dma_cb(dbs, 0); return &dbs->common; } - BlockAIOCB *dma_blk_read(BlockBackend *blk, QEMUSGList *sg, uint64_t sector, void (*cb)(void *opaque, int ret), void *opaque) diff --git a/trace-events b/trace-events index 2fce98e..120cdd4 100644 --- a/trace-events +++ b/trace-events @@ -1127,7 +1127,7 @@ win_helper_retry(uint32_t tl) "tl=%d" dma_blk_io(void *dbs, void *bs, int64_t sector_num, bool to_dev) "dbs=%p bs=%p sector_num=%" PRId64 " to_dev=%d" dma_aio_cancel(void *dbs) "dbs=%p" dma_complete(void *dbs, int ret, void *cb) "dbs=%p ret=%d cb=%p" -dma_blk_cb(void *dbs, int ret) "dbs=%p ret=%d" +dma_blk_io_cb(void *dbs, int ret) "dbs=%p ret=%d" dma_map_wait(void *dbs) "dbs=%p" # ui/console.c -- 2.6.2