From nobody Mon Feb 9 19:52:12 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1612856599; cv=none; d=zohomail.com; s=zohoarc; b=gHlEEIsbtmhJHqYnxEZ+LFTSoNG8vHybeUb4HCk7Yqg4hv7RZspJO7NCNHiILVTczoDG1VjRrKWiCDSOTteI4SmaMG4zd9REGP3PgH6rpIctdRG1KpK6jGuld5G4IPy6I2R4XF+rXxYZbkYGQLnA8rpnMCcKpahZ/tyWbP6NfDo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1612856599; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=yr+ODXq0ar8WmPLirTI08uQrB++W1cT3xbvsWymg0no=; b=CfUd1fUCyEunJ2wvTUw4bz1ugzZNXW2aOV/Yj0m3SNYVQcYPtsLOWi+y709iX4xdKfPUVoofVRgYacBZff/4XH7JSt28Y2N3GFuo80kNoz3C/hKwWSN5rwQgP2Jlc43NrI8+gQUc84GA17xH0iyf9C8wq7hNt7e6diE+gr8vb+4= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1612856598835827.3545053756241; Mon, 8 Feb 2021 23:43:18 -0800 (PST) Received: from localhost ([::1]:37734 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l9Ng5-000307-BP for importer@patchew.org; Tue, 09 Feb 2021 02:43:17 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:46840) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l9NUk-0004sh-Ip; Tue, 09 Feb 2021 02:31:34 -0500 Received: from wnew1-smtp.messagingengine.com ([64.147.123.26]:37271) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l9NUi-0005Uk-Lv; Tue, 09 Feb 2021 02:31:34 -0500 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailnew.west.internal (Postfix) with ESMTP id 50588C10; Tue, 9 Feb 2021 02:31:30 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Tue, 09 Feb 2021 02:31:31 -0500 Received: from apples.local (80-167-98-190-cable.dk.customer.tdc.net [80.167.98.190]) by mail.messagingengine.com (Postfix) with ESMTPA id 5F8351080057; Tue, 9 Feb 2021 02:31:28 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=irrelevant.dk; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; s=fm2; bh=yr+ODXq0ar8Wm PLirTI08uQrB++W1cT3xbvsWymg0no=; b=1Ov3Ue3h5VMk0D9R3/sTQoqeOgVbb GYfABsSOE07gE05mCWat9xGwRzSdUfxk51MQlmkZqUl0nVrvoGTE2BrOjfzJ7syH kqsXX+KRqqcDfwwldtATaDXAdrszghzRMOTUrXzpltq1ho6qYO7ui6u4v7nWQ7s2 fQU4CZx37uaBvMZb+bdtR+BcUew/NKoXMcqUaWtKvioXeT6ivi7OCQE/Aey3EbTF CB2HCO4adQU0wOWIlhYgQdiqxjLrGNtFyPorJ/4ybESWBP2wxnxYjOt9M0hmnCAi IbqoPjwzFodGEQanOl6bx4LlOgSIvgd9EakgBVNeOG6DHH9vaVqQOeZ7g== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:date:from :in-reply-to:message-id:mime-version:references:subject:to :x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=yr+ODXq0ar8WmPLirTI08uQrB++W1cT3xbvsWymg0no=; b=Bg4ERhJW yC07IVj3q4VYxZ3GeC9Ks264R4q+SMJyLREH/xaL5gCpjmBBoVHmrwEwZ4ClLpX8 c5pa6nnJdFyWHbINL4EBgIXfK3bLrKCLJFkP+EzKkTgrNEMnTPn/68ZXvUgkfiJo ZKsJjOQxXHJIyb0ivJHJJNV70M2U52znxfCE0+2AWpiJuCWANDwmV9fx6oBl2lJh fUVapW1tyTndWAhppGTw5bkUyQ5vFnmr7OqqcCi1UkfNLNLK62sFhyT4giQ+1Ztw Vmj2PmDi5zAoqWkh//E7MMcD/aXwS79Py3osWLFhVm0/7jexwInVscOqcz7+5hga m7Z+w0kuZctG1g== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrheeggddutdelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpefmlhgruhhs ucflvghnshgvnhcuoehithhssehirhhrvghlvghvrghnthdrughkqeenucggtffrrghtth gvrhhnpeeuleetgeeiuefhgfekfefgveejiefgteekiedtgfdtieefhfdthfefueffvefg keenucfkphepkedtrdduieejrdelkedrudeltdenucevlhhushhtvghrufhiiigvpeehne curfgrrhgrmhepmhgrihhlfhhrohhmpehithhssehirhhrvghlvghvrghnthdrughk X-ME-Proxy: From: Klaus Jensen To: qemu-devel@nongnu.org, Peter Maydell Subject: [PULL 06/56] hw/block/nvme: add compare command Date: Tue, 9 Feb 2021 08:30:11 +0100 Message-Id: <20210209073101.548811-7-its@irrelevant.dk> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210209073101.548811-1-its@irrelevant.dk> References: <20210209073101.548811-1-its@irrelevant.dk> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=64.147.123.26; envelope-from=its@irrelevant.dk; helo=wnew1-smtp.messagingengine.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Fam Zheng , Kevin Wolf , qemu-block@nongnu.org, Klaus Jensen , Gollu Appalanaidu , Max Reitz , Klaus Jensen , Minwoo Im , Stefan Hajnoczi , Keith Busch Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" From: Gollu Appalanaidu Add the Compare command. This implementation uses a bounce buffer to read in the data from storage and then compare with the host supplied buffer. Signed-off-by: Gollu Appalanaidu [k.jensen: rebased] Signed-off-by: Klaus Jensen Reviewed-by: Minwoo Im Reviewed-by: Keith Busch --- hw/block/nvme.c | 101 +++++++++++++++++++++++++++++++++++++++++- hw/block/trace-events | 2 + 2 files changed, 102 insertions(+), 1 deletion(-) diff --git a/hw/block/nvme.c b/hw/block/nvme.c index f019d43788ac..b9313fdc4762 100644 --- a/hw/block/nvme.c +++ b/hw/block/nvme.c @@ -999,6 +999,51 @@ static void nvme_aio_discard_cb(void *opaque, int ret) nvme_enqueue_req_completion(nvme_cq(req), req); } =20 +struct nvme_compare_ctx { + QEMUIOVector iov; + uint8_t *bounce; + size_t len; +}; + +static void nvme_compare_cb(void *opaque, int ret) +{ + NvmeRequest *req =3D opaque; + NvmeNamespace *ns =3D req->ns; + struct nvme_compare_ctx *ctx =3D req->opaque; + g_autofree uint8_t *buf =3D NULL; + uint16_t status; + + trace_pci_nvme_compare_cb(nvme_cid(req)); + + if (!ret) { + block_acct_done(blk_get_stats(ns->blkconf.blk), &req->acct); + } else { + block_acct_failed(blk_get_stats(ns->blkconf.blk), &req->acct); + nvme_aio_err(req, ret); + goto out; + } + + buf =3D g_malloc(ctx->len); + + status =3D nvme_dma(nvme_ctrl(req), buf, ctx->len, DMA_DIRECTION_TO_DE= VICE, + req); + if (status) { + req->status =3D status; + goto out; + } + + if (memcmp(buf, ctx->bounce, ctx->len)) { + req->status =3D NVME_CMP_FAILURE; + } + +out: + qemu_iovec_destroy(&ctx->iov); + g_free(ctx->bounce); + g_free(ctx); + + nvme_enqueue_req_completion(nvme_cq(req), req); +} + static uint16_t nvme_dsm(NvmeCtrl *n, NvmeRequest *req) { NvmeNamespace *ns =3D req->ns; @@ -1072,6 +1117,57 @@ static uint16_t nvme_dsm(NvmeCtrl *n, NvmeRequest *r= eq) return status; } =20 +static uint16_t nvme_compare(NvmeCtrl *n, NvmeRequest *req) +{ + NvmeRwCmd *rw =3D (NvmeRwCmd *)&req->cmd; + NvmeNamespace *ns =3D req->ns; + BlockBackend *blk =3D ns->blkconf.blk; + uint64_t slba =3D le64_to_cpu(rw->slba); + uint32_t nlb =3D le16_to_cpu(rw->nlb) + 1; + size_t len =3D nvme_l2b(ns, nlb); + int64_t offset =3D nvme_l2b(ns, slba); + uint8_t *bounce =3D NULL; + struct nvme_compare_ctx *ctx =3D NULL; + uint16_t status; + + trace_pci_nvme_compare(nvme_cid(req), nvme_nsid(ns), slba, nlb); + + status =3D nvme_check_mdts(n, len); + if (status) { + trace_pci_nvme_err_mdts(nvme_cid(req), len); + return status; + } + + status =3D nvme_check_bounds(ns, slba, nlb); + if (status) { + trace_pci_nvme_err_invalid_lba_range(slba, nlb, ns->id_ns.nsze); + return status; + } + + if (NVME_ERR_REC_DULBE(ns->features.err_rec)) { + status =3D nvme_check_dulbe(ns, slba, nlb); + if (status) { + return status; + } + } + + bounce =3D g_malloc(len); + + ctx =3D g_new(struct nvme_compare_ctx, 1); + ctx->bounce =3D bounce; + ctx->len =3D len; + + req->opaque =3D ctx; + + qemu_iovec_init(&ctx->iov, 1); + qemu_iovec_add(&ctx->iov, bounce, len); + + block_acct_start(blk_get_stats(blk), &req->acct, len, BLOCK_ACCT_READ); + blk_aio_preadv(blk, offset, &ctx->iov, 0, nvme_compare_cb, req); + + return NVME_NO_COMPLETE; +} + static uint16_t nvme_flush(NvmeCtrl *n, NvmeRequest *req) { block_acct_start(blk_get_stats(req->ns->blkconf.blk), &req->acct, 0, @@ -1201,6 +1297,8 @@ static uint16_t nvme_io_cmd(NvmeCtrl *n, NvmeRequest = *req) case NVME_CMD_WRITE: case NVME_CMD_READ: return nvme_rw(n, req); + case NVME_CMD_COMPARE: + return nvme_compare(n, req); case NVME_CMD_DSM: return nvme_dsm(n, req); default: @@ -2925,7 +3023,8 @@ static void nvme_init_ctrl(NvmeCtrl *n, PCIDevice *pc= i_dev) id->cqes =3D (0x4 << 4) | 0x4; id->nn =3D cpu_to_le32(n->num_namespaces); id->oncs =3D cpu_to_le16(NVME_ONCS_WRITE_ZEROES | NVME_ONCS_TIMESTAMP | - NVME_ONCS_FEATURES | NVME_ONCS_DSM); + NVME_ONCS_FEATURES | NVME_ONCS_DSM | + NVME_ONCS_COMPARE); =20 id->vwc =3D 0x1; id->sgls =3D cpu_to_le32(NVME_CTRL_SGLS_SUPPORT_NO_ALIGN | diff --git a/hw/block/trace-events b/hw/block/trace-events index 1ffe0b3f76b5..68a4c8ed35e0 100644 --- a/hw/block/trace-events +++ b/hw/block/trace-events @@ -46,6 +46,8 @@ pci_nvme_write_zeroes(uint16_t cid, uint32_t nsid, uint64= _t slba, uint32_t nlb) pci_nvme_block_status(int64_t offset, int64_t bytes, int64_t pnum, int ret= , bool zeroed) "offset %"PRId64" bytes %"PRId64" pnum %"PRId64" ret 0x%x ze= roed %d" pci_nvme_dsm(uint16_t cid, uint32_t nsid, uint32_t nr, uint32_t attr) "cid= %"PRIu16" nsid %"PRIu32" nr %"PRIu32" attr 0x%"PRIx32"" pci_nvme_dsm_deallocate(uint16_t cid, uint32_t nsid, uint64_t slba, uint32= _t nlb) "cid %"PRIu16" nsid %"PRIu32" slba %"PRIu64" nlb %"PRIu32"" +pci_nvme_compare(uint16_t cid, uint32_t nsid, uint64_t slba, uint32_t nlb)= "cid %"PRIu16" nsid %"PRIu32" slba 0x%"PRIx64" nlb %"PRIu32"" +pci_nvme_compare_cb(uint16_t cid) "cid %"PRIu16"" pci_nvme_aio_discard_cb(uint16_t cid) "cid %"PRIu16"" pci_nvme_create_sq(uint64_t addr, uint16_t sqid, uint16_t cqid, uint16_t q= size, uint16_t qflags) "create submission queue, addr=3D0x%"PRIx64", sqid= =3D%"PRIu16", cqid=3D%"PRIu16", qsize=3D%"PRIu16", qflags=3D%"PRIu16"" pci_nvme_create_cq(uint64_t addr, uint16_t cqid, uint16_t vector, uint16_t= size, uint16_t qflags, int ien) "create completion queue, addr=3D0x%"PRIx6= 4", cqid=3D%"PRIu16", vector=3D%"PRIu16", qsize=3D%"PRIu16", qflags=3D%"PRI= u16", ien=3D%d" --=20 2.30.0