26ba25
From d1046c844b369fb9cebc8f4f64274a9642152526 Mon Sep 17 00:00:00 2001
26ba25
From: Fam Zheng <famz@redhat.com>
26ba25
Date: Fri, 29 Jun 2018 06:11:48 +0200
26ba25
Subject: [PATCH 174/268] iscsi: Implement copy offloading
26ba25
26ba25
RH-Author: Fam Zheng <famz@redhat.com>
26ba25
Message-id: <20180629061153.12687-9-famz@redhat.com>
26ba25
Patchwork-id: 81159
26ba25
O-Subject: [RHEL-7.6 qemu-kvm-rhev PATCH v2 08/13] iscsi: Implement copy offloading
26ba25
Bugzilla: 1482537
26ba25
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
26ba25
RH-Acked-by: Max Reitz <mreitz@redhat.com>
26ba25
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
26ba25
26ba25
Issue EXTENDED COPY (LID1) command to implement the copy_range API.
26ba25
26ba25
The parameter data construction code is modified from libiscsi's
26ba25
iscsi-dd.c.
26ba25
26ba25
Signed-off-by: Fam Zheng <famz@redhat.com>
26ba25
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
26ba25
Message-id: 20180601092648.24614-9-famz@redhat.com
26ba25
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
26ba25
(cherry picked from commit 604dfaaa3270081da689991afe83d94d3e8231df)
26ba25
Signed-off-by: Fam Zheng <famz@redhat.com>
26ba25
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
26ba25
---
26ba25
 block/iscsi.c            | 219 +++++++++++++++++++++++++++++++++++++++++++++++
26ba25
 include/scsi/constants.h |   4 +
26ba25
 2 files changed, 223 insertions(+)
26ba25
26ba25
diff --git a/block/iscsi.c b/block/iscsi.c
26ba25
index 338f3dd..fbcd5bb 100644
26ba25
--- a/block/iscsi.c
26ba25
+++ b/block/iscsi.c
26ba25
@@ -2187,6 +2187,221 @@ static void coroutine_fn iscsi_co_invalidate_cache(BlockDriverState *bs,
26ba25
     iscsi_allocmap_invalidate(iscsilun);
26ba25
 }
26ba25
 
26ba25
+static int coroutine_fn iscsi_co_copy_range_from(BlockDriverState *bs,
26ba25
+                                                 BdrvChild *src,
26ba25
+                                                 uint64_t src_offset,
26ba25
+                                                 BdrvChild *dst,
26ba25
+                                                 uint64_t dst_offset,
26ba25
+                                                 uint64_t bytes,
26ba25
+                                                 BdrvRequestFlags flags)
26ba25
+{
26ba25
+    return bdrv_co_copy_range_to(src, src_offset, dst, dst_offset, bytes, flags);
26ba25
+}
26ba25
+
26ba25
+static struct scsi_task *iscsi_xcopy_task(int param_len)
26ba25
+{
26ba25
+    struct scsi_task *task;
26ba25
+
26ba25
+    task = g_new0(struct scsi_task, 1);
26ba25
+
26ba25
+    task->cdb[0]     = EXTENDED_COPY;
26ba25
+    task->cdb[10]    = (param_len >> 24) & 0xFF;
26ba25
+    task->cdb[11]    = (param_len >> 16) & 0xFF;
26ba25
+    task->cdb[12]    = (param_len >> 8) & 0xFF;
26ba25
+    task->cdb[13]    = param_len & 0xFF;
26ba25
+    task->cdb_size   = 16;
26ba25
+    task->xfer_dir   = SCSI_XFER_WRITE;
26ba25
+    task->expxferlen = param_len;
26ba25
+
26ba25
+    return task;
26ba25
+}
26ba25
+
26ba25
+static void iscsi_populate_target_desc(unsigned char *desc, IscsiLun *lun)
26ba25
+{
26ba25
+    struct scsi_inquiry_device_designator *dd = lun->dd;
26ba25
+
26ba25
+    memset(desc, 0, 32);
26ba25
+    desc[0] = 0xE4; /* IDENT_DESCR_TGT_DESCR */
26ba25
+    desc[4] = dd->code_set;
26ba25
+    desc[5] = (dd->designator_type & 0xF)
26ba25
+        | ((dd->association & 3) << 4);
26ba25
+    desc[7] = dd->designator_length;
26ba25
+    memcpy(desc + 8, dd->designator, dd->designator_length);
26ba25
+
26ba25
+    desc[28] = 0;
26ba25
+    desc[29] = (lun->block_size >> 16) & 0xFF;
26ba25
+    desc[30] = (lun->block_size >> 8) & 0xFF;
26ba25
+    desc[31] = lun->block_size & 0xFF;
26ba25
+}
26ba25
+
26ba25
+static void iscsi_xcopy_desc_hdr(uint8_t *hdr, int dc, int cat, int src_index,
26ba25
+                                 int dst_index)
26ba25
+{
26ba25
+    hdr[0] = 0x02; /* BLK_TO_BLK_SEG_DESCR */
26ba25
+    hdr[1] = ((dc << 1) | cat) & 0xFF;
26ba25
+    hdr[2] = (XCOPY_BLK2BLK_SEG_DESC_SIZE >> 8) & 0xFF;
26ba25
+    /* don't account for the first 4 bytes in descriptor header*/
26ba25
+    hdr[3] = (XCOPY_BLK2BLK_SEG_DESC_SIZE - 4 /* SEG_DESC_SRC_INDEX_OFFSET */) & 0xFF;
26ba25
+    hdr[4] = (src_index >> 8) & 0xFF;
26ba25
+    hdr[5] = src_index & 0xFF;
26ba25
+    hdr[6] = (dst_index >> 8) & 0xFF;
26ba25
+    hdr[7] = dst_index & 0xFF;
26ba25
+}
26ba25
+
26ba25
+static void iscsi_xcopy_populate_desc(uint8_t *desc, int dc, int cat,
26ba25
+                                      int src_index, int dst_index, int num_blks,
26ba25
+                                      uint64_t src_lba, uint64_t dst_lba)
26ba25
+{
26ba25
+    iscsi_xcopy_desc_hdr(desc, dc, cat, src_index, dst_index);
26ba25
+
26ba25
+    /* The caller should verify the request size */
26ba25
+    assert(num_blks < 65536);
26ba25
+    desc[10] = (num_blks >> 8) & 0xFF;
26ba25
+    desc[11] = num_blks & 0xFF;
26ba25
+    desc[12] = (src_lba >> 56) & 0xFF;
26ba25
+    desc[13] = (src_lba >> 48) & 0xFF;
26ba25
+    desc[14] = (src_lba >> 40) & 0xFF;
26ba25
+    desc[15] = (src_lba >> 32) & 0xFF;
26ba25
+    desc[16] = (src_lba >> 24) & 0xFF;
26ba25
+    desc[17] = (src_lba >> 16) & 0xFF;
26ba25
+    desc[18] = (src_lba >> 8) & 0xFF;
26ba25
+    desc[19] = src_lba & 0xFF;
26ba25
+    desc[20] = (dst_lba >> 56) & 0xFF;
26ba25
+    desc[21] = (dst_lba >> 48) & 0xFF;
26ba25
+    desc[22] = (dst_lba >> 40) & 0xFF;
26ba25
+    desc[23] = (dst_lba >> 32) & 0xFF;
26ba25
+    desc[24] = (dst_lba >> 24) & 0xFF;
26ba25
+    desc[25] = (dst_lba >> 16) & 0xFF;
26ba25
+    desc[26] = (dst_lba >> 8) & 0xFF;
26ba25
+    desc[27] = dst_lba & 0xFF;
26ba25
+}
26ba25
+
26ba25
+static void iscsi_xcopy_populate_header(unsigned char *buf, int list_id, int str,
26ba25
+                                        int list_id_usage, int prio,
26ba25
+                                        int tgt_desc_len,
26ba25
+                                        int seg_desc_len, int inline_data_len)
26ba25
+{
26ba25
+    buf[0] = list_id;
26ba25
+    buf[1] = ((str & 1) << 5) | ((list_id_usage & 3) << 3) | (prio & 7);
26ba25
+    buf[2] = (tgt_desc_len >> 8) & 0xFF;
26ba25
+    buf[3] = tgt_desc_len & 0xFF;
26ba25
+    buf[8] = (seg_desc_len >> 24) & 0xFF;
26ba25
+    buf[9] = (seg_desc_len >> 16) & 0xFF;
26ba25
+    buf[10] = (seg_desc_len >> 8) & 0xFF;
26ba25
+    buf[11] = seg_desc_len & 0xFF;
26ba25
+    buf[12] = (inline_data_len >> 24) & 0xFF;
26ba25
+    buf[13] = (inline_data_len >> 16) & 0xFF;
26ba25
+    buf[14] = (inline_data_len >> 8) & 0xFF;
26ba25
+    buf[15] = inline_data_len & 0xFF;
26ba25
+}
26ba25
+
26ba25
+static void iscsi_xcopy_data(struct iscsi_data *data,
26ba25
+                             IscsiLun *src, int64_t src_lba,
26ba25
+                             IscsiLun *dst, int64_t dst_lba,
26ba25
+                             uint16_t num_blocks)
26ba25
+{
26ba25
+    uint8_t *buf;
26ba25
+    const int src_offset = XCOPY_DESC_OFFSET;
26ba25
+    const int dst_offset = XCOPY_DESC_OFFSET + IDENT_DESCR_TGT_DESCR_SIZE;
26ba25
+    const int seg_offset = dst_offset + IDENT_DESCR_TGT_DESCR_SIZE;
26ba25
+
26ba25
+    data->size = XCOPY_DESC_OFFSET +
26ba25
+                 IDENT_DESCR_TGT_DESCR_SIZE * 2 +
26ba25
+                 XCOPY_BLK2BLK_SEG_DESC_SIZE;
26ba25
+    data->data = g_malloc0(data->size);
26ba25
+    buf = data->data;
26ba25
+
26ba25
+    /* Initialise the parameter list header */
26ba25
+    iscsi_xcopy_populate_header(buf, 1, 0, 2 /* LIST_ID_USAGE_DISCARD */,
26ba25
+                                0, 2 * IDENT_DESCR_TGT_DESCR_SIZE,
26ba25
+                                XCOPY_BLK2BLK_SEG_DESC_SIZE,
26ba25
+                                0);
26ba25
+
26ba25
+    /* Initialise CSCD list with one src + one dst descriptor */
26ba25
+    iscsi_populate_target_desc(&buf[src_offset], src);
26ba25
+    iscsi_populate_target_desc(&buf[dst_offset], dst);
26ba25
+
26ba25
+    /* Initialise one segment descriptor */
26ba25
+    iscsi_xcopy_populate_desc(&buf[seg_offset], 0, 0, 0, 1, num_blocks,
26ba25
+                              src_lba, dst_lba);
26ba25
+}
26ba25
+
26ba25
+static int coroutine_fn iscsi_co_copy_range_to(BlockDriverState *bs,
26ba25
+                                               BdrvChild *src,
26ba25
+                                               uint64_t src_offset,
26ba25
+                                               BdrvChild *dst,
26ba25
+                                               uint64_t dst_offset,
26ba25
+                                               uint64_t bytes,
26ba25
+                                               BdrvRequestFlags flags)
26ba25
+{
26ba25
+    IscsiLun *dst_lun = dst->bs->opaque;
26ba25
+    IscsiLun *src_lun;
26ba25
+    struct IscsiTask iscsi_task;
26ba25
+    struct iscsi_data data;
26ba25
+    int r = 0;
26ba25
+    int block_size;
26ba25
+
26ba25
+    if (src->bs->drv->bdrv_co_copy_range_to != iscsi_co_copy_range_to) {
26ba25
+        return -ENOTSUP;
26ba25
+    }
26ba25
+    src_lun = src->bs->opaque;
26ba25
+
26ba25
+    if (!src_lun->dd || !dst_lun->dd) {
26ba25
+        return -ENOTSUP;
26ba25
+    }
26ba25
+    if (!is_byte_request_lun_aligned(dst_offset, bytes, dst_lun)) {
26ba25
+        return -ENOTSUP;
26ba25
+    }
26ba25
+    if (!is_byte_request_lun_aligned(src_offset, bytes, src_lun)) {
26ba25
+        return -ENOTSUP;
26ba25
+    }
26ba25
+    if (dst_lun->block_size != src_lun->block_size ||
26ba25
+        !dst_lun->block_size) {
26ba25
+        return -ENOTSUP;
26ba25
+    }
26ba25
+
26ba25
+    block_size = dst_lun->block_size;
26ba25
+    if (bytes / block_size > 65535) {
26ba25
+        return -ENOTSUP;
26ba25
+    }
26ba25
+
26ba25
+    iscsi_xcopy_data(&data,
26ba25
+                     src_lun, src_offset / block_size,
26ba25
+                     dst_lun, dst_offset / block_size,
26ba25
+                     bytes / block_size);
26ba25
+
26ba25
+    iscsi_co_init_iscsitask(dst_lun, &iscsi_task);
26ba25
+
26ba25
+    qemu_mutex_lock(&dst_lun->mutex);
26ba25
+    iscsi_task.task = iscsi_xcopy_task(data.size);
26ba25
+retry:
26ba25
+    if (iscsi_scsi_command_async(dst_lun->iscsi, dst_lun->lun,
26ba25
+                                 iscsi_task.task, iscsi_co_generic_cb,
26ba25
+                                 &data,
26ba25
+                                 &iscsi_task) != 0) {
26ba25
+        r = -EIO;
26ba25
+        goto out_unlock;
26ba25
+    }
26ba25
+
26ba25
+    iscsi_co_wait_for_task(&iscsi_task, dst_lun);
26ba25
+
26ba25
+    if (iscsi_task.do_retry) {
26ba25
+        iscsi_task.complete = 0;
26ba25
+        goto retry;
26ba25
+    }
26ba25
+
26ba25
+    if (iscsi_task.status != SCSI_STATUS_GOOD) {
26ba25
+        r = iscsi_task.err_code;
26ba25
+        goto out_unlock;
26ba25
+    }
26ba25
+
26ba25
+out_unlock:
26ba25
+    g_free(iscsi_task.task);
26ba25
+    qemu_mutex_unlock(&dst_lun->mutex);
26ba25
+    g_free(iscsi_task.err_str);
26ba25
+    return r;
26ba25
+}
26ba25
+
26ba25
 static QemuOptsList iscsi_create_opts = {
26ba25
     .name = "iscsi-create-opts",
26ba25
     .head = QTAILQ_HEAD_INITIALIZER(iscsi_create_opts.head),
26ba25
@@ -2221,6 +2436,8 @@ static BlockDriver bdrv_iscsi = {
26ba25
 
26ba25
     .bdrv_co_block_status  = iscsi_co_block_status,
26ba25
     .bdrv_co_pdiscard      = iscsi_co_pdiscard,
26ba25
+    .bdrv_co_copy_range_from = iscsi_co_copy_range_from,
26ba25
+    .bdrv_co_copy_range_to  = iscsi_co_copy_range_to,
26ba25
     .bdrv_co_pwrite_zeroes = iscsi_co_pwrite_zeroes,
26ba25
     .bdrv_co_readv         = iscsi_co_readv,
26ba25
     .bdrv_co_writev_flags  = iscsi_co_writev_flags,
26ba25
@@ -2256,6 +2473,8 @@ static BlockDriver bdrv_iser = {
26ba25
 
26ba25
     .bdrv_co_block_status  = iscsi_co_block_status,
26ba25
     .bdrv_co_pdiscard      = iscsi_co_pdiscard,
26ba25
+    .bdrv_co_copy_range_from = iscsi_co_copy_range_from,
26ba25
+    .bdrv_co_copy_range_to  = iscsi_co_copy_range_to,
26ba25
     .bdrv_co_pwrite_zeroes = iscsi_co_pwrite_zeroes,
26ba25
     .bdrv_co_readv         = iscsi_co_readv,
26ba25
     .bdrv_co_writev_flags  = iscsi_co_writev_flags,
26ba25
diff --git a/include/scsi/constants.h b/include/scsi/constants.h
26ba25
index a141dd7..083a8e8 100644
26ba25
--- a/include/scsi/constants.h
26ba25
+++ b/include/scsi/constants.h
26ba25
@@ -311,4 +311,8 @@
26ba25
 #define MMC_PROFILE_HDDVD_RW_DL         0x005A
26ba25
 #define MMC_PROFILE_INVALID             0xFFFF
26ba25
 
26ba25
+#define XCOPY_DESC_OFFSET 16
26ba25
+#define IDENT_DESCR_TGT_DESCR_SIZE 32
26ba25
+#define XCOPY_BLK2BLK_SEG_DESC_SIZE 28
26ba25
+
26ba25
 #endif
26ba25
-- 
26ba25
1.8.3.1
26ba25