From nobody Sun May 19 18:10:07 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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; dmarc=pass(p=none dis=none) header.from=yadro.com ARC-Seal: i=1; a=rsa-sha256; t=1650445745; cv=none; d=zohomail.com; s=zohoarc; b=Ic+o6bmOGska0V+b5Ml9bgmO1twSWOnpgMOnHE7WfTSY6F8FSH+FJdJ5cd2GJ2sqUAIPe9Lm1+2MG/6w2uckzdHr+U2FuJ7g/rfJj1kFBu5U5qtLKcZCHYtnzkKK21dk8H534HHsQz9wb9VsL9dwNPVDov8rOmBb208elHqsun8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1650445745; h=Content-Type: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=Mm5oKgftcKVA4+VLED7nM7K5li3x464CriDEP1mnRsg=; b=EGEz39H/GzyTDRyUldYYf+d81cYTT+UUBFhTA9s8hiOorYnOexO9dngEPA8m8zIl2AKwoq8C12V7kX3Kl0Xe2xhpak+u6Opr/h7stkkXGTxS7TMX1kQ+iAmVj/jblc1Af2HkYqDvdTUvizaLEa5BFMxUFWqjlqt0a05giOPyQSc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 165044574554869.32495331133543; Wed, 20 Apr 2022 02:09:05 -0700 (PDT) Received: from localhost ([::1]:56828 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nh6Ke-0003Vf-BJ for importer@patchew.org; Wed, 20 Apr 2022 05:09:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43378) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nh6Fo-0000jS-LU; Wed, 20 Apr 2022 05:04:06 -0400 Received: from mta-02.yadro.com ([89.207.88.252]:51242 helo=mta-01.yadro.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nh6Fl-0001eB-Pq; Wed, 20 Apr 2022 05:04:03 -0400 Received: from localhost (unknown [127.0.0.1]) by mta-01.yadro.com (Postfix) with ESMTP id 681E3437E1; Wed, 20 Apr 2022 09:03:59 +0000 (UTC) Received: from mta-01.yadro.com ([127.0.0.1]) by localhost (mta-01.yadro.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id zUIm6GqpXoIV; Wed, 20 Apr 2022 12:03:58 +0300 (MSK) Received: from T-EXCH-04.corp.yadro.com (t-exch-04.corp.yadro.com [172.17.100.104]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mta-01.yadro.com (Postfix) with ESMTPS id F089A45968; Wed, 20 Apr 2022 12:03:57 +0300 (MSK) Received: from localhost.yadro.com (10.178.113.54) by T-EXCH-04.corp.yadro.com (172.17.100.104) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.669.32; Wed, 20 Apr 2022 12:03:56 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=yadro.com; h= content-type:content-type:content-transfer-encoding:mime-version :references:in-reply-to:x-mailer:message-id:date:date:subject :subject:from:from:received:received:received; s=mta-01; t= 1650445438; x=1652259839; bh=oEIwg/t6jcEJQ3VmD7U4uXuxt9llDvFarwr +dLCahes=; b=a5KKhQexDbZbI9ARQg/DrY1EYyp04Vvd6CbEbK80iesxXlOdfxu Bl+OBygnYB2iQpJjElt81OJD/Y8LWoA24ueJTz4rlIuSkvtw8oCHkg/lJqNWVdDF grpxJOGalSTJ8/N+RbmWTuVHSRAJ2Cno67CTrncYWFGt7Ck9t/hS7X2c= X-Virus-Scanned: amavisd-new at yadro.com From: Dmitry Tikhov To: Subject: [PATCH 1/2] hw/nvme: refactor check of disabled dif Date: Wed, 20 Apr 2022 12:03:35 +0300 Message-ID: <20220420090336.10124-2-d.tihov@yadro.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220420090336.10124-1-d.tihov@yadro.com> References: <20220420090336.10124-1-d.tihov@yadro.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.178.113.54] X-ClientProxiedBy: T-EXCH-01.corp.yadro.com (172.17.10.101) To T-EXCH-04.corp.yadro.com (172.17.100.104) 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=89.207.88.252; envelope-from=d.tihov@yadro.com; helo=mta-01.yadro.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kbusch@kernel.org, its@irrelevant.dk, ddtikhov@gmail.com, qemu-block@nongnu.org, linux@yadro.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @yadro.com) X-ZM-MESSAGEID: 1650445746582100001 Content-Type: text/plain; charset="utf-8" Move to a separate function code determining whether protection checking in end-to-end data protection enabled namespace should be done. Currently this code is used only in nvme_dif_prchk_crc16 and nvme_dif_prchk_crc64 functions. Signed-off-by: Dmitry Tikhov --- hw/nvme/dif.c | 121 ++++++++++++++++++++++++++++--------------- hw/nvme/dif.h | 1 + hw/nvme/trace-events | 4 +- 3 files changed, 83 insertions(+), 43 deletions(-) diff --git a/hw/nvme/dif.c b/hw/nvme/dif.c index 63c44c86ab..0f65687396 100644 --- a/hw/nvme/dif.c +++ b/hw/nvme/dif.c @@ -60,6 +60,75 @@ static uint64_t crc64_nvme(uint64_t crc, const unsigned = char *buffer, return crc ^ (uint64_t)~0; } =20 +static bool nvme_dif_is_disabled_crc16(NvmeNamespace *ns, NvmeDifTuple *di= f) +{ + switch (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) { + case NVME_ID_NS_DPS_TYPE_3: + if (be32_to_cpu(dif->g16.reftag) !=3D 0xffffffff) { + break; + } + + /* fallthrough */ + case NVME_ID_NS_DPS_TYPE_1: + case NVME_ID_NS_DPS_TYPE_2: + if (be16_to_cpu(dif->g16.apptag) !=3D 0xffff) { + break; + } + + trace_pci_nvme_dif_is_disabled_crc16(be16_to_cpu(dif->g16.apptag), + be32_to_cpu(dif->g16.reftag)); + + return true; + } + + return false; +} + +static bool nvme_dif_is_disabled_crc64(NvmeNamespace *ns, NvmeDifTuple *di= f) +{ + uint64_t r =3D 0; + + r |=3D (uint64_t)dif->g64.sr[0] << 40; + r |=3D (uint64_t)dif->g64.sr[1] << 32; + r |=3D (uint64_t)dif->g64.sr[2] << 24; + r |=3D (uint64_t)dif->g64.sr[3] << 16; + r |=3D (uint64_t)dif->g64.sr[4] << 8; + r |=3D (uint64_t)dif->g64.sr[5]; + + switch (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) { + case NVME_ID_NS_DPS_TYPE_3: + if (r !=3D 0xffffffffffff) { + break; + } + + /* fallthrough */ + case NVME_ID_NS_DPS_TYPE_1: + case NVME_ID_NS_DPS_TYPE_2: + if (be16_to_cpu(dif->g64.apptag) !=3D 0xffff) { + break; + } + + trace_pci_nvme_dif_is_disabled_crc64(be16_to_cpu(dif->g16.apptag), + r); + + return true; + } + + return false; +} + +bool nvme_dif_is_disabled(NvmeNamespace *ns, NvmeDifTuple *dif) +{ + switch (ns->pif) { + case NVME_PI_GUARD_16: + return nvme_dif_is_disabled_crc16(ns, dif); + case NVME_PI_GUARD_64: + return nvme_dif_is_disabled_crc64(ns, dif); + default: + abort(); + } +} + static void nvme_dif_pract_generate_dif_crc16(NvmeNamespace *ns, uint8_t *= buf, size_t len, uint8_t *mbuf, size_t mlen, uint16_t apptag, @@ -155,22 +224,7 @@ static uint16_t nvme_dif_prchk_crc16(NvmeNamespace *ns= , NvmeDifTuple *dif, uint8_t prinfo, uint16_t apptag, uint16_t appmask, uint64_t reftag) { - switch (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) { - case NVME_ID_NS_DPS_TYPE_3: - if (be32_to_cpu(dif->g16.reftag) !=3D 0xffffffff) { - break; - } - - /* fallthrough */ - case NVME_ID_NS_DPS_TYPE_1: - case NVME_ID_NS_DPS_TYPE_2: - if (be16_to_cpu(dif->g16.apptag) !=3D 0xffff) { - break; - } - - trace_pci_nvme_dif_prchk_disabled_crc16(be16_to_cpu(dif->g16.appta= g), - be32_to_cpu(dif->g16.refta= g)); - + if (nvme_dif_is_disabled_crc16(ns, dif)) { return NVME_SUCCESS; } =20 @@ -214,31 +268,7 @@ static uint16_t nvme_dif_prchk_crc64(NvmeNamespace *ns= , NvmeDifTuple *dif, uint8_t prinfo, uint16_t apptag, uint16_t appmask, uint64_t reftag) { - uint64_t r =3D 0; - - r |=3D (uint64_t)dif->g64.sr[0] << 40; - r |=3D (uint64_t)dif->g64.sr[1] << 32; - r |=3D (uint64_t)dif->g64.sr[2] << 24; - r |=3D (uint64_t)dif->g64.sr[3] << 16; - r |=3D (uint64_t)dif->g64.sr[4] << 8; - r |=3D (uint64_t)dif->g64.sr[5]; - - switch (NVME_ID_NS_DPS_TYPE(ns->id_ns.dps)) { - case NVME_ID_NS_DPS_TYPE_3: - if (r !=3D 0xffffffffffff) { - break; - } - - /* fallthrough */ - case NVME_ID_NS_DPS_TYPE_1: - case NVME_ID_NS_DPS_TYPE_2: - if (be16_to_cpu(dif->g64.apptag) !=3D 0xffff) { - break; - } - - trace_pci_nvme_dif_prchk_disabled_crc64(be16_to_cpu(dif->g16.appta= g), - r); - + if (nvme_dif_is_disabled_crc64(ns, dif)) { return NVME_SUCCESS; } =20 @@ -266,6 +296,15 @@ static uint16_t nvme_dif_prchk_crc64(NvmeNamespace *ns= , NvmeDifTuple *dif, } =20 if (prinfo & NVME_PRINFO_PRCHK_REF) { + uint64_t r =3D 0; + + r |=3D (uint64_t)dif->g64.sr[0] << 40; + r |=3D (uint64_t)dif->g64.sr[1] << 32; + r |=3D (uint64_t)dif->g64.sr[2] << 24; + r |=3D (uint64_t)dif->g64.sr[3] << 16; + r |=3D (uint64_t)dif->g64.sr[4] << 8; + r |=3D (uint64_t)dif->g64.sr[5]; + trace_pci_nvme_dif_prchk_reftag_crc64(r, reftag); =20 if (r !=3D reftag) { diff --git a/hw/nvme/dif.h b/hw/nvme/dif.h index f12e312250..fe1e5828d7 100644 --- a/hw/nvme/dif.h +++ b/hw/nvme/dif.h @@ -186,6 +186,7 @@ uint16_t nvme_dif_check(NvmeNamespace *ns, uint8_t *buf= , size_t len, uint8_t *mbuf, size_t mlen, uint8_t prinfo, uint64_t slba, uint16_t apptag, uint16_t appmask, uint64_t *reftag); +bool nvme_dif_is_disabled(NvmeNamespace *ns, NvmeDifTuple *dif); uint16_t nvme_dif_rw(NvmeCtrl *n, NvmeRequest *req); =20 #endif /* HW_NVME_DIF_H */ diff --git a/hw/nvme/trace-events b/hw/nvme/trace-events index ff1b458969..c4e75b1f5d 100644 --- a/hw/nvme/trace-events +++ b/hw/nvme/trace-events @@ -23,8 +23,8 @@ pci_nvme_dif_rw_check_cb(uint16_t cid, uint8_t prinfo, ui= nt16_t apptag, uint16_t pci_nvme_dif_pract_generate_dif_crc16(size_t len, size_t lba_size, size_t = chksum_len, uint16_t apptag, uint32_t reftag) "len %zu lba_size %zu chksum_= len %zu apptag 0x%"PRIx16" reftag 0x%"PRIx32"" pci_nvme_dif_pract_generate_dif_crc64(size_t len, size_t lba_size, size_t = chksum_len, uint16_t apptag, uint64_t reftag) "len %zu lba_size %zu chksum_= len %zu apptag 0x%"PRIx16" reftag 0x%"PRIx64"" pci_nvme_dif_check(uint8_t prinfo, uint16_t chksum_len) "prinfo 0x%"PRIx8"= chksum_len %"PRIu16"" -pci_nvme_dif_prchk_disabled_crc16(uint16_t apptag, uint32_t reftag) "appta= g 0x%"PRIx16" reftag 0x%"PRIx32"" -pci_nvme_dif_prchk_disabled_crc64(uint16_t apptag, uint64_t reftag) "appta= g 0x%"PRIx16" reftag 0x%"PRIx64"" +pci_nvme_dif_is_disabled_crc16(uint16_t apptag, uint32_t reftag) "apptag 0= x%"PRIx16" reftag 0x%"PRIx32"" +pci_nvme_dif_is_disabled_crc64(uint16_t apptag, uint64_t reftag) "apptag 0= x%"PRIx16" reftag 0x%"PRIx64"" pci_nvme_dif_prchk_guard_crc16(uint16_t guard, uint16_t crc) "guard 0x%"PR= Ix16" crc 0x%"PRIx16"" pci_nvme_dif_prchk_guard_crc64(uint64_t guard, uint64_t crc) "guard 0x%"PR= Ix64" crc 0x%"PRIx64"" pci_nvme_dif_prchk_apptag(uint16_t apptag, uint16_t elbat, uint16_t elbatm= ) "apptag 0x%"PRIx16" elbat 0x%"PRIx16" elbatm 0x%"PRIx16"" --=20 2.35.1 From nobody Sun May 19 18:10:07 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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; dmarc=pass(p=none dis=none) header.from=yadro.com ARC-Seal: i=1; a=rsa-sha256; t=1650445745; cv=none; d=zohomail.com; s=zohoarc; b=A+Efa4LVP1AzOXk7RZF0jt2KdNpacL5chVPuIpyiV2sKD+BIHuNUoTsyE1msAcs9lTbGyUrxiipNYtLIiaY4wdYQtJws0Uh+ca9LjYHQJ0138iyzer5xrd55yV4oUngGBYq8+idXMMjMsDySGbgwRYc4uy0swNjJy/eLh34AzUg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1650445745; h=Content-Type: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=A8uefsgKpr8n8Hdnqd51t2SHYB65SLEB/YF8QXQTPF4=; b=agyRfdoGjqgG+Ssx/aM06U4nlTJE6Y9aTHbN3o/kPX51kuCPUT8maifJK0214mChnHS6GDYjVMg3HdRP1Nle3enbUF/Sij8sbz1l2lO4n3pMzNWVdQG1jmp+ler7HKDmCGqRseUSNArGjELiAg+9eCgL6zK1LAWIovGJtsKGuCY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1650445745537357.09651314323526; Wed, 20 Apr 2022 02:09:05 -0700 (PDT) Received: from localhost ([::1]:56782 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nh6Kd-0003Tj-CF for importer@patchew.org; Wed, 20 Apr 2022 05:09:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:43376) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nh6Fo-0000jR-Lz; Wed, 20 Apr 2022 05:04:06 -0400 Received: from mta-02.yadro.com ([89.207.88.252]:51260 helo=mta-01.yadro.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nh6Fm-0001eE-Ft; Wed, 20 Apr 2022 05:04:04 -0400 Received: from localhost (unknown [127.0.0.1]) by mta-01.yadro.com (Postfix) with ESMTP id BBBEB45968; Wed, 20 Apr 2022 09:04:00 +0000 (UTC) Received: from mta-01.yadro.com ([127.0.0.1]) by localhost (mta-01.yadro.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 1-8Zizt8tskT; Wed, 20 Apr 2022 12:03:59 +0300 (MSK) Received: from T-EXCH-04.corp.yadro.com (t-exch-04.corp.yadro.com [172.17.100.104]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mta-01.yadro.com (Postfix) with ESMTPS id C46AE45969; Wed, 20 Apr 2022 12:03:58 +0300 (MSK) Received: from localhost.yadro.com (10.178.113.54) by T-EXCH-04.corp.yadro.com (172.17.100.104) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.669.32; Wed, 20 Apr 2022 12:03:57 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=yadro.com; h= content-type:content-type:content-transfer-encoding:mime-version :references:in-reply-to:x-mailer:message-id:date:date:subject :subject:from:from:received:received:received; s=mta-01; t= 1650445439; x=1652259840; bh=R8YQcsiORkAxKQC8mN3WQNntuJEXmRFPZ5k /sD78hew=; b=p1GK/3OCESJtnM/TqUUUtnRQs8dcb+ahHGd7tG/ML6SpNs9tTcq 08RTcCsf5nkfI0n5WwuUdY7xXrQ9FS6ehVS+39i8JlS7fqbP73gUaS24lRqiJXl7 uLTaap/ho4uyi5ScqLzDbz8iWhlHQnc9JYfHu0pq25806o1hfFc816GA= X-Virus-Scanned: amavisd-new at yadro.com From: Dmitry Tikhov To: Subject: [PATCH 2/2] hw/nvme: fix copy cmd for pi enabled namespaces Date: Wed, 20 Apr 2022 12:03:36 +0300 Message-ID: <20220420090336.10124-3-d.tihov@yadro.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220420090336.10124-1-d.tihov@yadro.com> References: <20220420090336.10124-1-d.tihov@yadro.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.178.113.54] X-ClientProxiedBy: T-EXCH-01.corp.yadro.com (172.17.10.101) To T-EXCH-04.corp.yadro.com (172.17.100.104) 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=89.207.88.252; envelope-from=d.tihov@yadro.com; helo=mta-01.yadro.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kbusch@kernel.org, its@irrelevant.dk, ddtikhov@gmail.com, qemu-block@nongnu.org, linux@yadro.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @yadro.com) X-ZM-MESSAGEID: 1650445746930100003 Content-Type: text/plain; charset="utf-8" Current implementation have two problems: First in the read part of copy command. Because there is no metadata mangling before nvme_dif_check invocation, reftag error is thrown for blocks of namespace that have not been previously written to. Second in the write part. Reftag in the protection information section of the source metadata should not be copied as is to the destination. Source range start lba and destination range start lba could differ so recalculation of reftag is always needed. Signed-off-by: Dmitry Tikhov --- hw/nvme/ctrl.c | 5 ++++ hw/nvme/dif.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++ hw/nvme/dif.h | 2 ++ 3 files changed, 72 insertions(+) diff --git a/hw/nvme/ctrl.c b/hw/nvme/ctrl.c index 74540a03d5..cb493f4506 100644 --- a/hw/nvme/ctrl.c +++ b/hw/nvme/ctrl.c @@ -2787,6 +2787,10 @@ static void nvme_copy_in_completed_cb(void *opaque, = int ret) size_t mlen =3D nvme_m2b(ns, nlb); uint8_t *mbounce =3D iocb->bounce + nvme_l2b(ns, nlb); =20 + status =3D nvme_dif_mangle_mdata(ns, mbounce, mlen, reftag); + if (status) { + goto invalid; + } status =3D nvme_dif_check(ns, iocb->bounce, len, mbounce, mlen, pr= infor, slba, apptag, appmask, &reftag); if (status) { @@ -2805,6 +2809,7 @@ static void nvme_copy_in_completed_cb(void *opaque, i= nt ret) nvme_dif_pract_generate_dif(ns, iocb->bounce, len, mbounce, ml= en, apptag, &iocb->reftag); } else { + nvme_dif_restore_reftag(ns, mbounce, mlen, iocb->reftag); status =3D nvme_dif_check(ns, iocb->bounce, len, mbounce, mlen, prinfow, iocb->slba, apptag, appmask, &iocb->reftag); diff --git a/hw/nvme/dif.c b/hw/nvme/dif.c index 0f65687396..f29c5893e2 100644 --- a/hw/nvme/dif.c +++ b/hw/nvme/dif.c @@ -385,6 +385,71 @@ uint16_t nvme_dif_check(NvmeNamespace *ns, uint8_t *bu= f, size_t len, return NVME_SUCCESS; } =20 +static void nvme_dif_restore_reftag_crc16(NvmeNamespace *ns, uint8_t *mbuf, + size_t mlen, uint64_t reftag, + int16_t pil) +{ + uint8_t *mbufp, *end =3D mbuf + mlen; + + for (mbufp =3D mbuf; mbufp < end; mbufp +=3D ns->lbaf.ms) { + NvmeDifTuple *dif =3D (NvmeDifTuple *)(mbufp + pil); + + if (!nvme_dif_is_disabled_crc16(ns, dif)) { + dif->g16.reftag =3D cpu_to_be32(reftag++); + } + + } + + return; +} + +static void nvme_dif_restore_reftag_crc64(NvmeNamespace *ns, uint8_t *mbuf, + size_t mlen, uint64_t reftag, + int16_t pil) +{ + uint8_t *mbufp, *end =3D mbuf + mlen; + + for (mbufp =3D mbuf; mbufp < end; mbufp +=3D ns->lbaf.ms) { + NvmeDifTuple *dif =3D (NvmeDifTuple *)(mbufp + pil); + + if (!nvme_dif_is_disabled_crc64(ns, dif)) { + dif->g64.sr[0] =3D reftag >> 40; + dif->g64.sr[1] =3D reftag >> 32; + dif->g64.sr[2] =3D reftag >> 24; + dif->g64.sr[3] =3D reftag >> 16; + dif->g64.sr[4] =3D reftag >> 8; + dif->g64.sr[5] =3D reftag; + + reftag++; + } + + } + + return; +} + +void nvme_dif_restore_reftag(NvmeNamespace *ns, uint8_t *mbuf, + size_t mlen, uint64_t reftag) +{ + int16_t pil =3D 0; + + if (!(ns->id_ns.dps & NVME_ID_NS_DPS_FIRST_EIGHT)) { + pil =3D ns->lbaf.ms - nvme_pi_tuple_size(ns); + } + + switch (ns->pif) { + case NVME_PI_GUARD_16: + nvme_dif_restore_reftag_crc16(ns, mbuf, mlen, reftag, pil); + return; + case NVME_PI_GUARD_64: + nvme_dif_restore_reftag_crc64(ns, mbuf, mlen, reftag, pil); + return; + default: + abort(); + } + +} + uint16_t nvme_dif_mangle_mdata(NvmeNamespace *ns, uint8_t *mbuf, size_t ml= en, uint64_t slba) { diff --git a/hw/nvme/dif.h b/hw/nvme/dif.h index fe1e5828d7..3203837658 100644 --- a/hw/nvme/dif.h +++ b/hw/nvme/dif.h @@ -186,6 +186,8 @@ uint16_t nvme_dif_check(NvmeNamespace *ns, uint8_t *buf= , size_t len, uint8_t *mbuf, size_t mlen, uint8_t prinfo, uint64_t slba, uint16_t apptag, uint16_t appmask, uint64_t *reftag); +void nvme_dif_restore_reftag(NvmeNamespace *ns, uint8_t *mbuf, + size_t mlen, uint64_t reftag); bool nvme_dif_is_disabled(NvmeNamespace *ns, NvmeDifTuple *dif); uint16_t nvme_dif_rw(NvmeCtrl *n, NvmeRequest *req); =20 --=20 2.35.1