hw/nvme: enhanced protection information (64-bit guard)
cover hw/nvme: enhanced protection information (64-bit guard)
From: Klaus Jensen <[email protected]>
This adds support for one possible new protection information format introduced in TP4068 (and integrated in NVMe 2.0): the 64-bit CRC guard and 48-bit reference tag. This version does not support storage tags.
Like the CRC16 support already present, this uses a software implementation of CRC64 (so it is naturally pretty slow). But its good enough for verification purposes.
This goes hand-in-hand with the support that Keith submitted for the Linux kernel[1].
[1]: https://lore.kernel.org/linux-nvme/[email protected]/
Klaus Jensen (3):
hw/nvme: move dif/pi prototypes into dif.h
hw/nvme: move format parameter parsing
hw/nvme: add pi tuple size helper
Naveen Nagar (3):
hw/nvme: add host behavior support feature
hw/nvme: add support for the lbafee hbs feature
hw/nvme: 64-bit pi support
hw/nvme/ctrl.c | 235 +++++++++++++++++++++------ hw/nvme/dif.c | 378 +++++++++++++++++++++++++++++++++---------- hw/nvme/dif.h | 191 ++++++++++++++++++++++ hw/nvme/ns.c | 27 +++- hw/nvme/nvme.h | 58 +------ hw/nvme/trace-events | 12 +- include/block/nvme.h | 81 ++++++++-- 7 files changed, 774 insertions(+), 208 deletions(-) create mode 100644 hw/nvme/dif.h
Signature · 1 line
2.35.1
PATCH 1/6 hw/nvme: move dif/pi prototypes into dif.h
From: Klaus Jensen <[email protected]>
Move dif/pi data structures and inlines to dif.h.
--- hw/nvme/ctrl.c | 1 + hw/nvme/dif.c | 1 + hw/nvme/dif.h | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ hw/nvme/nvme.h | 50 ----------------------------------------------- 4 files changed, 55 insertions(+), 50 deletions(-) create mode 100644 hw/nvme/dif.h
hw/nvme/ctrl.c +1 -0
@@ -163,6 +163,7 @@#include "migration/vmstate.h" #include "nvme.h" +#include "dif.h" #include "trace.h" #define NVME_MAX_IOQPAIRS 0xffff
hw/nvme/dif.c +1 -0
@@ -13,6 +13,7 @@#include "sysemu/block-backend.h" #include "nvme.h" +#include "dif.h" #include "trace.h" uint16_t nvme_check_prinfo(NvmeNamespace *ns, uint8_t prinfo, uint64_t slba,
hw/nvme/dif.h new file +53 -0
53 lines not on this page · raw message
hw/nvme/nvme.h +0 -50
54 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH 2/6 hw/nvme: add host behavior support feature
In reply to Klaus JensenFrom: Naveen Nagar <[email protected]>
Add support for getting and setting the Host Behavior Support feature.
--- hw/nvme/ctrl.c | 8 ++++++++ hw/nvme/nvme.h | 4 +++- include/block/nvme.h | 9 +++++++++ 3 files changed, 20 insertions(+), 1 deletion(-)
hw/nvme/ctrl.c +8 -0
@@ -196,6 +196,7 @@ static const bool nvme_feature_support[NVME_FID_MAX] = { [NVME_WRITE_ATOMICITY] = true, [NVME_ASYNCHRONOUS_EVENT_CONF] = true, [NVME_TIMESTAMP] = true, + [NVME_HOST_BEHAVIOR_SUPPORT] = true, [NVME_COMMAND_SET_PROFILE] = true, };
@@ -206,6 +207,7 @@ static const uint32_t nvme_feature_cap[NVME_FID_MAX] = { [NVME_NUMBER_OF_QUEUES] = NVME_FEAT_CAP_CHANGE, [NVME_ASYNCHRONOUS_EVENT_CONF] = NVME_FEAT_CAP_CHANGE, [NVME_TIMESTAMP] = NVME_FEAT_CAP_CHANGE, + [NVME_HOST_BEHAVIOR_SUPPORT] = NVME_FEAT_CAP_CHANGE, [NVME_COMMAND_SET_PROFILE] = NVME_FEAT_CAP_CHANGE, };
@@ -5091,6 +5093,9 @@ static uint16_t nvme_get_feature(NvmeCtrl *n, NvmeRequest *req) goto out;case NVME_TIMESTAMP :return nvme_get_feature_timestamp (n, req); +case NVME_HOST_BEHAVIOR_SUPPORT : +return nvme_c2h (n, (uint8_t *)&n->features.hbs, +sizeof (n->features.hbs), req);default :break ; }
@@ -5281,6 +5286,9 @@ static uint16_t nvme_set_feature(NvmeCtrl *n, NvmeRequest *req)break ;case NVME_TIMESTAMP :return nvme_set_feature_timestamp (n, req); +case NVME_HOST_BEHAVIOR_SUPPORT : +return nvme_h2c (n, (uint8_t *)&n->features.hbs, +sizeof (n->features.hbs), req);case NVME_COMMAND_SET_PROFILE :if (dw11 &0x1ff ) {trace_pci_nvme_err_invalid_iocsci (dw11 &0x1ff );
hw/nvme/nvme.h +3 -1
@@ -468,7 +468,9 @@ typedef struct NvmeCtrl {uint16_t temp_thresh_hi;uint16_t temp_thresh_low; }; -uint32_t async_config; + +uint32_t async_config; +NvmeHostBehaviorSupport hbs; } features; } NvmeCtrl;
include/block/nvme.h +9 -0
@@ -1216,6 +1216,7 @@ enum NvmeFeatureIds {NVME_WRITE_ATOMICITY =0xa ,NVME_ASYNCHRONOUS_EVENT_CONF =0xb ,NVME_TIMESTAMP =0xe , +NVME_HOST_BEHAVIOR_SUPPORT =0x16 ,NVME_COMMAND_SET_PROFILE =0x19 ,NVME_SOFTWARE_PROGRESS_MARKER =0x80 , NVME_FID_MAX = 0x100,
@@ -1257,6 +1258,13 @@ typedef struct QEMU_PACKED NvmeRangeType {uint8_t rsvd48[16 ]; } NvmeRangeType; +typedef struct NvmeHostBehaviorSupport { +uint8_t acre; +uint8_t etdas; +uint8_t lbafee; +uint8_t rsvd3[509 ]; +}NvmeHostBehaviorSupport ; + typedef struct QEMU_PACKED NvmeLBAF {uint16_t ms;uint8_t ds;
@@ -1520,6 +1528,7 @@ static inline void _nvme_check_size(void)QEMU_BUILD_BUG_ON (sizeof (NvmeDsmCmd) !=64 );QEMU_BUILD_BUG_ON (sizeof (NvmeCopyCmd) !=64 );QEMU_BUILD_BUG_ON (sizeof (NvmeRangeType) !=64 ); +QEMU_BUILD_BUG_ON (sizeof (NvmeHostBehaviorSupport) !=512 );QEMU_BUILD_BUG_ON (sizeof (NvmeErrorLog) !=64 );QEMU_BUILD_BUG_ON (sizeof (NvmeFwSlotInfoLog) !=512 );QEMU_BUILD_BUG_ON (sizeof (NvmeSmartLog) !=512 );
Signature · 1 line
2.35.1
PATCH 3/6 hw/nvme: move format parameter parsing
In reply to Klaus JensenFrom: Klaus Jensen <[email protected]>
There is no need to extract the format command parameters for each namespace. Move it to the entry point.
--- hw/nvme/ctrl.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-)
hw/nvme/ctrl.c +18 -13
@@ -5452,6 +5452,11 @@ typedef struct NvmeFormatAIOCB {uint32_t nsid;bool broadcast;int64_t offset; + +uint8_t lbaf; +uint8_t mset; +uint8_t pi; +uint8_t pil; } NvmeFormatAIOCB;static void nvme_format_bh (void *opaque);
@@ -5471,14 +5476,9 @@ static const AIOCBInfo nvme_format_aiocb_info = { .get_aio_context = nvme_get_aio_context, }; -static void nvme_format_set (NvmeNamespace *ns,NvmeCmd *cmd) +static void nvme_format_set (NvmeNamespace *ns,uint8_t lbaf,uint8_t mset, +uint8_t pi,uint8_t pil) { -uint32_t dw10 =le32_to_cpu (cmd->cdw10); -uint8_t lbaf = dw10 &0xf ; -uint8_t pi = (dw10 >>5 ) &0x7 ; -uint8_t mset = (dw10 >>4 ) &0x1 ; -uint8_t pil = (dw10 >>8 ) &0x1 ; -trace_pci_nvme_format_set (ns->params.nsid, lbaf, mset, pi, pil); ns->id_ns.dps = (pil <<3 ) | pi;
@@ -5490,7 +5490,6 @@ static void nvme_format_set(NvmeNamespace *ns, NvmeCmd *cmd)static void nvme_format_ns_cb (void *opaque,int ret) {NvmeFormatAIOCB *iocb = opaque; -NvmeRequest *req = iocb->req;NvmeNamespace *ns = iocb->ns;int bytes;
@@ -5512,7 +5511,7 @@ static void nvme_format_ns_cb(void *opaque, int ret)return ; } -nvme_format_set (ns, &req->cmd); +nvme_format_set (ns, iocb->lbaf, iocb->mset, iocb->pi, iocb->pil); ns->status =0x0 ; iocb->ns =NULL ; iocb->offset =0 ;
@@ -5548,9 +5547,6 @@ static void nvme_format_bh(void *opaque)NvmeFormatAIOCB *iocb = opaque;NvmeRequest *req = iocb->req;NvmeCtrl *n =nvme_ctrl (req); -uint32_t dw10 =le32_to_cpu (req->cmd.cdw10); -uint8_t lbaf = dw10 &0xf ; -uint8_t pi = (dw10 >>5 ) &0x7 ;uint16_t status;int i;
@@ -5572,7 +5568,7 @@ static void nvme_format_bh(void *opaque) goto done; } - status =nvme_format_check (iocb->ns, lbaf, pi); + status =nvme_format_check (iocb->ns, iocb->lbaf, iocb->pi);if (status) { req->status = status; goto done;
@@ -5595,6 +5591,11 @@ static uint16_t nvme_format(NvmeCtrl *n, NvmeRequest *req) {NvmeFormatAIOCB *iocb;uint32_t nsid =le32_to_cpu (req->cmd.nsid); +uint32_t dw10 =le32_to_cpu (req->cmd.cdw10); +uint8_t lbaf = dw10 &0xf ; +uint8_t mset = (dw10 >>4 ) &0x1 ; +uint8_t pi = (dw10 >>5 ) &0x7 ; +uint8_t pil = (dw10 >>8 ) &0x1 ;uint16_t status; iocb =qemu_aio_get (&nvme_format_aiocb_info,NULL , nvme_misc_cb, req);
@@ -5604,6 +5605,10 @@ static uint16_t nvme_format(NvmeCtrl *n, NvmeRequest *req) iocb->ret =0 ; iocb->ns =NULL ; iocb->nsid =0 ; + iocb->lbaf = lbaf; + iocb->mset = mset; + iocb->pi = pi; + iocb->pil = pil; iocb->broadcast = (nsid ==NVME_NSID_BROADCAST ); iocb->offset =0 ;
Signature · 1 line
2.35.1
PATCH 4/6 hw/nvme: add support for the lbafee hbs feature
In reply to Klaus JensenFrom: Naveen Nagar <[email protected]>
Add support for up to 64 LBA formats through the LBAFEE field of the Host Behavior Support feature.
--- hw/nvme/ctrl.c | 34 +++++++++++++++++++++++++++++++--- hw/nvme/ns.c | 15 +++++++++------ hw/nvme/nvme.h | 1 + include/block/nvme.h | 7 +++++-- 4 files changed, 46 insertions(+), 11 deletions(-)
hw/nvme/ctrl.c +31 -3
@@ -5165,6 +5165,7 @@ static uint16_t nvme_set_feature(NvmeCtrl *n, NvmeRequest *req)uint32_t nsid =le32_to_cpu (cmd->nsid);uint8_t fid =NVME_GETSETFEAT_FID (dw10);uint8_t save =NVME_SETFEAT_SAVE (dw10); +uint16_t status;int i;trace_pci_nvme_setfeat (nvme_cid (req), nsid, fid, save, dw11);
@@ -5287,8 +5288,26 @@ static uint16_t nvme_set_feature(NvmeCtrl *n, NvmeRequest *req)case NVME_TIMESTAMP :return nvme_set_feature_timestamp (n, req);case NVME_HOST_BEHAVIOR_SUPPORT : -return nvme_h2c (n, (uint8_t *)&n->features.hbs, -sizeof (n->features.hbs), req); + status =nvme_h2c (n, (uint8_t *)&n->features.hbs, +sizeof (n->features.hbs), req); +if (status) { +return status; + } + +for (i =1 ; i <=NVME_MAX_NAMESPACES ; i++) { + ns =nvme_ns (n, i); + +if (!ns) { +continue ; + } + + ns->id_ns.nlbaf = ns->nlbaf -1 ; +if (!n->features.hbs.lbafee) { + ns->id_ns.nlbaf =MIN (ns->id_ns.nlbaf,15 ); + } + } + +return status;case NVME_COMMAND_SET_PROFILE :if (dw11 &0x1ff ) {trace_pci_nvme_err_invalid_iocsci (dw11 &0x1ff );
@@ -5479,10 +5498,13 @@ static const AIOCBInfo nvme_format_aiocb_info = {static void nvme_format_set (NvmeNamespace *ns,uint8_t lbaf,uint8_t mset,uint8_t pi,uint8_t pil) { +uint8_t lbafl = lbaf &0xf ; +uint8_t lbafu = lbaf >>4 ; +trace_pci_nvme_format_set (ns->params.nsid, lbaf, mset, pi, pil); ns->id_ns.dps = (pil <<3 ) | pi; - ns->id_ns.flbas = lbaf | (mset <<4 ); + ns->id_ns.flbas = (lbafu <<5 ) | (mset <<4 ) | lbafl;nvme_ns_init_format (ns); }
@@ -5596,6 +5618,7 @@ static uint16_t nvme_format(NvmeCtrl *n, NvmeRequest *req)uint8_t mset = (dw10 >>4 ) &0x1 ;uint8_t pi = (dw10 >>5 ) &0x7 ;uint8_t pil = (dw10 >>8 ) &0x1 ; +uint8_t lbafu = (dw10 >>12 ) &0x3 ;uint16_t status; iocb =qemu_aio_get (&nvme_format_aiocb_info,NULL , nvme_misc_cb, req);
@@ -5612,6 +5635,10 @@ static uint16_t nvme_format(NvmeCtrl *n, NvmeRequest *req) iocb->broadcast = (nsid ==NVME_NSID_BROADCAST ); iocb->offset =0 ; +if (n->features.hbs.lbafee) { + iocb->lbaf |= lbafu <<4 ; + } +if (!iocb->broadcast) {if (!nvme_nsid_valid (n, nsid)) { status =NVME_INVALID_NSID |NVME_DNR ;
@@ -6587,6 +6614,7 @@ static void nvme_init_ctrl(NvmeCtrl *n, PCIDevice *pci_dev) id->cntlid =cpu_to_le16 (n->cntlid); id->oaes =cpu_to_le32 (NVME_OAES_NS_ATTR ); + id->ctratt |=cpu_to_le32 (NVME_CTRATT_ELBAS ); id->rab =6 ;
hw/nvme/ns.c +9 -6
@@ -112,10 +112,11 @@ static int nvme_ns_init(NvmeNamespace *ns, Error **errp) [7] = { .ds = 12, .ms = 64 }, }; + ns->nlbaf =8 ; +memcpy (&id_ns->lbaf, &lbaf,sizeof (lbaf)); - id_ns->nlbaf =7 ; -for (i =0 ; i <= id_ns->nlbaf; i++) { +for (i =0 ; i < ns->nlbaf; i++) {NvmeLBAF *lbaf = &id_ns->lbaf[i];if (lbaf->ds == ds) {if (lbaf->ms == ms) {
@@ -126,12 +127,14 @@ static int nvme_ns_init(NvmeNamespace *ns, Error **errp) }/* add non-standard lba format */ - id_ns->nlbaf++; - id_ns->lbaf[id_ns->nlbaf].ds = ds; - id_ns->lbaf[id_ns->nlbaf].ms = ms; - id_ns->flbas |= id_ns->nlbaf; + id_ns->lbaf[ns->nlbaf].ds = ds; + id_ns->lbaf[ns->nlbaf].ms = ms; + ns->nlbaf++; + + id_ns->flbas |= i; lbaf_found: + id_ns->nlbaf = ns->nlbaf -1 ;nvme_ns_init_format (ns);return 0 ;
hw/nvme/nvme.h +1 -0
@@ -128,6 +128,7 @@ typedef struct NvmeNamespace {int64_t moff;NvmeIdNs id_ns;NvmeLBAF lbaf; +unsigned int nlbaf;size_t lbasz;const uint32_t *iocs;uint8_t csi;
include/block/nvme.h +5 -2
@@ -1111,6 +1111,10 @@ enum NvmeIdCtrlOaes { NVME_OAES_NS_ATTR = 1 << 8, }; +enum NvmeIdCtrlCtratt { +NVME_CTRATT_ELBAS =1 <<15 , +}; +enum NvmeIdCtrlOacs {NVME_OACS_SECURITY =1 <<0 ,NVME_OACS_FORMAT =1 <<1 ,
@@ -1312,8 +1316,7 @@ typedef struct QEMU_PACKED NvmeIdNs {uint8_t rsvd81[23 ];uint8_t nguid[16 ];uint64_t eui64; -NvmeLBAF lbaf[16 ]; -uint8_t rsvd192[192 ]; +NvmeLBAF lbaf[64 ];uint8_t vs[3712 ]; } NvmeIdNs;
Signature · 1 line
2.35.1
PATCH 5/6 hw/nvme: add pi tuple size helper
In reply to Klaus JensenFrom: Klaus Jensen <[email protected]>
A subsequent patch will introduce a new tuple size; so add a helper and use that instead of sizeof() and magic numbers.
--- hw/nvme/ctrl.c | 14 ++++++++------ hw/nvme/dif.c | 16 ++++++++-------- hw/nvme/dif.h | 5 +++++ 3 files changed, 21 insertions(+), 14 deletions(-)
hw/nvme/ctrl.c +8 -6
@@ -1068,7 +1068,8 @@ static uint16_t nvme_map_data(NvmeCtrl *n, uint32_t nlb, NvmeRequest *req)size_t len =nvme_l2b (ns, nlb);uint16_t status; -if (nvme_ns_ext (ns) && !(pi && pract && ns->lbaf.ms ==8 )) { +if (nvme_ns_ext (ns) && + !(pi && pract && ns->lbaf.ms ==nvme_pi_tuple_size (ns))) {NvmeSg sg; len +=nvme_m2b (ns, nlb);
@@ -1247,7 +1248,8 @@ uint16_t nvme_bounce_data(NvmeCtrl *n, void *ptr, uint32_t len,bool pi = !!NVME_ID_NS_DPS_TYPE (ns->id_ns.dps);bool pract = !!(le16_to_cpu (rw->control) &NVME_RW_PRINFO_PRACT ); -if (nvme_ns_ext (ns) && !(pi && pract && ns->lbaf.ms ==8 )) { +if (nvme_ns_ext (ns) && + !(pi && pract && ns->lbaf.ms ==nvme_pi_tuple_size (ns))) {return nvme_tx_interleaved (n, &req->sg, ptr, len, ns->lbasz, ns->lbaf.ms,0 , dir); }
@@ -2184,7 +2186,7 @@ static void nvme_compare_mdata_cb(void *opaque, int ret)* tuple. */ if (!(ns->id_ns.dps &NVME_ID_NS_DPS_FIRST_EIGHT )) { - pil = ns->lbaf.ms -sizeof (NvmeDifTuple); + pil = ns->lbaf.ms -nvme_pi_tuple_size (ns); }for (bufp = buf; mbufp < end; bufp += ns->lbaf.ms, mbufp += ns->lbaf.ms) {
@@ -3167,7 +3169,7 @@ static uint16_t nvme_read(NvmeCtrl *n, NvmeRequest *req)if (NVME_ID_NS_DPS_TYPE (ns->id_ns.dps)) {bool pract = prinfo &NVME_PRINFO_PRACT ; -if (pract && ns->lbaf.ms ==8 ) { +if (pract && ns->lbaf.ms ==nvme_pi_tuple_size (ns)) { mapped_size = data_size; } }
@@ -3244,7 +3246,7 @@ static uint16_t nvme_do_write(NvmeCtrl *n, NvmeRequest *req, bool append,if (NVME_ID_NS_DPS_TYPE (ns->id_ns.dps)) {bool pract = prinfo &NVME_PRINFO_PRACT ; -if (pract && ns->lbaf.ms ==8 ) { +if (pract && ns->lbaf.ms ==nvme_pi_tuple_size (ns)) { mapped_size -=nvme_m2b (ns, nlb); } }
@@ -5553,7 +5555,7 @@ static uint16_t nvme_format_check(NvmeNamespace *ns, uint8_t lbaf, uint8_t pi)return NVME_INVALID_FORMAT |NVME_DNR ; } -if (pi && (ns->id_ns.lbaf[lbaf].ms <sizeof (NvmeDifTuple))) { +if (pi && (ns->id_ns.lbaf[lbaf].ms <nvme_pi_tuple_size (ns))) {return NVME_INVALID_FORMAT |NVME_DNR ; }
hw/nvme/dif.c +8 -8
@@ -48,7 +48,7 @@ void nvme_dif_pract_generate_dif(NvmeNamespace *ns, uint8_t *buf, size_t len,int16_t pil =0 ;if (!(ns->id_ns.dps &NVME_ID_NS_DPS_FIRST_EIGHT )) { - pil = ns->lbaf.ms -sizeof (NvmeDifTuple); + pil = ns->lbaf.ms -nvme_pi_tuple_size (ns); } trace_pci_nvme_dif_pract_generate_dif(len, ns->lbasz, ns->lbasz + pil,
@@ -145,7 +145,7 @@ uint16_t nvme_dif_check(NvmeNamespace *ns, uint8_t *buf, size_t len, }if (!(ns->id_ns.dps &NVME_ID_NS_DPS_FIRST_EIGHT )) { - pil = ns->lbaf.ms -sizeof (NvmeDifTuple); + pil = ns->lbaf.ms -nvme_pi_tuple_size (ns); }trace_pci_nvme_dif_check (prinfo, ns->lbasz + pil);
@@ -184,7 +184,7 @@ uint16_t nvme_dif_mangle_mdata(NvmeNamespace *ns, uint8_t *mbuf, size_t mlen,if (!(ns->id_ns.dps &NVME_ID_NS_DPS_FIRST_EIGHT )) { - pil = ns->lbaf.ms -sizeof (NvmeDifTuple); + pil = ns->lbaf.ms -nvme_pi_tuple_size (ns); } do {
@@ -210,7 +210,7 @@ uint16_t nvme_dif_mangle_mdata(NvmeNamespace *ns, uint8_t *mbuf, size_t mlen, end = mbufp + mlen;for (; mbufp < end; mbufp += ns->lbaf.ms) { -memset (mbufp + pil,0xff ,sizeof (NvmeDifTuple)); +memset (mbufp + pil,0xff ,nvme_pi_tuple_size (ns)); } }
@@ -284,7 +284,7 @@ static void nvme_dif_rw_check_cb(void *opaque, int ret) goto out; } -if (prinfo &NVME_PRINFO_PRACT && ns->lbaf.ms ==8 ) { +if (prinfo &NVME_PRINFO_PRACT && ns->lbaf.ms ==nvme_pi_tuple_size (ns)) { goto out; }
@@ -388,7 +388,7 @@ uint16_t nvme_dif_rw(NvmeCtrl *n, NvmeRequest *req)if (pract) {uint8_t *mbuf, *end; -int16_t pil = ns->lbaf.ms -sizeof (NvmeDifTuple); +int16_t pil = ns->lbaf.ms -nvme_pi_tuple_size (ns); status =nvme_check_prinfo (ns, prinfo, slba, reftag);if (status) {
@@ -428,7 +428,7 @@ uint16_t nvme_dif_rw(NvmeCtrl *n, NvmeRequest *req)return NVME_NO_COMPLETE ; } -if (nvme_ns_ext (ns) && !(pract && ns->lbaf.ms ==8 )) { +if (nvme_ns_ext (ns) && !(pract && ns->lbaf.ms ==nvme_pi_tuple_size (ns))) { mapped_len += mlen; }
@@ -462,7 +462,7 @@ uint16_t nvme_dif_rw(NvmeCtrl *n, NvmeRequest *req)qemu_iovec_init (&ctx->mdata.iov,1 );qemu_iovec_add (&ctx->mdata.iov, ctx->mdata.bounce, mlen); -if (!(pract && ns->lbaf.ms ==8 )) { +if (!(pract && ns->lbaf.ms ==nvme_pi_tuple_size (ns))) { status =nvme_bounce_mdata (n, ctx->mdata.bounce, ctx->mdata.iov.size,NVME_TX_DIRECTION_TO_DEVICE , req);if (status) {
hw/nvme/dif.h +5 -0
@@ -37,6 +37,11 @@ static const uint16_t t10_dif_crc_table[256] = {0xF0D8 ,0x7B6F ,0x6C01 ,0xE7B6 ,0x42DD ,0xC96A ,0xDE04 ,0x55B3 }; +static inline size_t nvme_pi_tuple_size (NvmeNamespace *ns) +{ +return sizeof (NvmeDifTuple); +} +uint16_t nvme_check_prinfo (NvmeNamespace *ns,uint8_t prinfo,uint64_t slba,uint32_t reftag); uint16_t nvme_dif_mangle_mdata(NvmeNamespace *ns, uint8_t *mbuf, size_t mlen,
Signature · 1 line
2.35.1
PATCH 6/6 hw/nvme: 64-bit pi support
In reply to Klaus JensenFrom: Naveen Nagar <[email protected]>
This adds support for one possible new protection information format introduced in TP4068 (and integrated in NVMe 2.0): the 64-bit CRC guard and 48-bit reference tag. This version does not support storage tags.
Like the CRC16 support already present, this uses a software implementation of CRC64 (so it is naturally pretty slow). But its good enough for verification purposes.
This may go nicely hand-in-hand with the support that Keith submitted for the Linux kernel[1].
[1]: https://lore.kernel.org/linux-nvme/[email protected]/T/
--- hw/nvme/ctrl.c | 163 +++++++++++++++---- hw/nvme/dif.c | 363 +++++++++++++++++++++++++++++++++---------- hw/nvme/dif.h | 143 ++++++++++++++++- hw/nvme/ns.c | 12 ++ hw/nvme/nvme.h | 3 + hw/nvme/trace-events | 12 +- include/block/nvme.h | 67 ++++++-- 7 files changed, 629 insertions(+), 134 deletions(-)
hw/nvme/ctrl.c +132 -31
@@ -2050,9 +2050,12 @@ static void nvme_verify_cb(void *opaque, int ret)uint8_t prinfo =NVME_RW_PRINFO (le16_to_cpu (rw->control));uint16_t apptag =le16_to_cpu (rw->apptag);uint16_t appmask =le16_to_cpu (rw->appmask); -uint32_t reftag =le32_to_cpu (rw->reftag); +uint64_t reftag =le32_to_cpu (rw->reftag); +uint64_t cdw3 =le32_to_cpu (rw->cdw3);uint16_t status; + reftag |= cdw3 <<32 ; +trace_pci_nvme_verify_cb (nvme_cid (req), prinfo, apptag, appmask, reftag);if (ret) {
@@ -2141,7 +2144,8 @@ static void nvme_compare_mdata_cb(void *opaque, int ret)uint8_t prinfo =NVME_RW_PRINFO (le16_to_cpu (rw->control));uint16_t apptag =le16_to_cpu (rw->apptag);uint16_t appmask =le16_to_cpu (rw->appmask); -uint32_t reftag =le32_to_cpu (rw->reftag); +uint64_t reftag =le32_to_cpu (rw->reftag); +uint64_t cdw3 =le32_to_cpu (rw->cdw3);struct nvme_compare_ctx *ctx = req->opaque; g_autofree uint8_t *buf = NULL;BlockBackend *blk = ns->blkconf.blk;
@@ -2149,6 +2153,8 @@ static void nvme_compare_mdata_cb(void *opaque, int ret)BlockAcctStats *stats =blk_get_stats (blk);uint16_t status =NVME_SUCCESS ; + reftag |= cdw3 <<32 ; +trace_pci_nvme_compare_mdata_cb (nvme_cid (req));if (ret) {
@@ -2527,7 +2533,8 @@ typedef struct NvmeCopyAIOCB {QEMUBH *bh;int ret; -NvmeCopySourceRange *ranges; +void *ranges; +unsigned int format;int nr;int idx;
@@ -2538,7 +2545,7 @@ typedef struct NvmeCopyAIOCB {BlockAcctCookie write; } acct; -uint32_t reftag; +uint64_t reftag;uint64_t slba;NvmeZone *zone;
@@ -2592,13 +2599,101 @@ static void nvme_copy_bh(void *opaque)static void nvme_copy_cb (void *opaque,int ret); +static void nvme_copy_source_range_parse_format0 (void *ranges,int idx, +uint64_t *slba,uint32_t *nlb, +uint16_t *apptag, +uint16_t *appmask, +uint64_t *reftag) +{ +NvmeCopySourceRangeFormat0 *_ranges = ranges; + +if (slba) { + *slba =le64_to_cpu (_ranges[idx].slba); + } + +if (nlb) { + *nlb =le16_to_cpu (_ranges[idx].nlb) +1 ; + } + +if (apptag) { + *apptag =le16_to_cpu (_ranges[idx].apptag); + } + +if (appmask) { + *appmask =le16_to_cpu (_ranges[idx].appmask); + } + +if (reftag) { + *reftag =le32_to_cpu (_ranges[idx].reftag); + } +} + +static void nvme_copy_source_range_parse_format1 (void *ranges,int idx, +uint64_t *slba,uint32_t *nlb, +uint16_t *apptag, +uint16_t *appmask, +uint64_t *reftag) +{ +NvmeCopySourceRangeFormat1 *_ranges = ranges; + +if (slba) { + *slba =le64_to_cpu (_ranges[idx].slba); + } + +if (nlb) { + *nlb =le16_to_cpu (_ranges[idx].nlb) +1 ; + } + +if (apptag) { + *apptag =le16_to_cpu (_ranges[idx].apptag); + } + +if (appmask) { + *appmask =le16_to_cpu (_ranges[idx].appmask); + } + +if (reftag) { + *reftag =0 ; + + *reftag |= (uint64_t )_ranges[idx].sr[4 ] <<40 ; + *reftag |= (uint64_t )_ranges[idx].sr[5 ] <<32 ; + *reftag |= (uint64_t )_ranges[idx].sr[6 ] <<24 ; + *reftag |= (uint64_t )_ranges[idx].sr[7 ] <<16 ; + *reftag |= (uint64_t )_ranges[idx].sr[8 ] <<8 ; + *reftag |= (uint64_t )_ranges[idx].sr[9 ]; + } +} + +static void nvme_copy_source_range_parse (void *ranges,int idx,uint8_t format, +uint64_t *slba,uint32_t *nlb, +uint16_t *apptag,uint16_t *appmask, +uint64_t *reftag) +{ +switch (format) { +case NVME_COPY_FORMAT_0 : +nvme_copy_source_range_parse_format0 (ranges, idx, slba, nlb, apptag, + appmask, reftag); +break ; + +case NVME_COPY_FORMAT_1 : +nvme_copy_source_range_parse_format1 (ranges, idx, slba, nlb, apptag, + appmask, reftag); +break ; + +default : +abort (); + } +} +static void nvme_copy_out_completed_cb (void *opaque,int ret) {NvmeCopyAIOCB *iocb = opaque;NvmeRequest *req = iocb->req;NvmeNamespace *ns = req->ns; -NvmeCopySourceRange *range = &iocb->ranges[iocb->idx]; -uint32_t nlb =le32_to_cpu (range->nlb) +1 ; +uint32_t nlb; + +nvme_copy_source_range_parse (iocb->ranges, iocb->idx, iocb->format,NULL , + &nlb,NULL ,NULL ,NULL );if (ret <0 ) { iocb->ret = ret;
@@ -2622,7 +2717,6 @@ static void nvme_copy_out_cb(void *opaque, int ret)NvmeCopyAIOCB *iocb = opaque;NvmeRequest *req = iocb->req;NvmeNamespace *ns = req->ns; -NvmeCopySourceRange *range;uint32_t nlb;size_t mlen;uint8_t *mbounce;
@@ -2639,8 +2733,8 @@ static void nvme_copy_out_cb(void *opaque, int ret)return ; } - range = &iocb->ranges[iocb->idx]; - nlb =le32_to_cpu (range->nlb) +1 ; +nvme_copy_source_range_parse (iocb->ranges, iocb->idx, iocb->format,NULL , + &nlb,NULL ,NULL ,NULL ); mlen =nvme_m2b (ns, nlb); mbounce = iocb->bounce +nvme_l2b (ns, nlb);
@@ -2663,8 +2757,10 @@ static void nvme_copy_in_completed_cb(void *opaque, int ret)NvmeCopyAIOCB *iocb = opaque;NvmeRequest *req = iocb->req;NvmeNamespace *ns = req->ns; -NvmeCopySourceRange *range;uint32_t nlb; +uint64_t slba; +uint16_t apptag, appmask; +uint64_t reftag;size_t len;uint16_t status;
@@ -2675,8 +2771,8 @@ static void nvme_copy_in_completed_cb(void *opaque, int ret) goto out; } - range = &iocb->ranges[iocb->idx]; - nlb =le32_to_cpu (range->nlb) +1 ; +nvme_copy_source_range_parse (iocb->ranges, iocb->idx, iocb->format, &slba, + &nlb, &apptag, &appmask, &reftag); len =nvme_l2b (ns, nlb);trace_pci_nvme_copy_out (iocb->slba, nlb);
@@ -2687,11 +2783,6 @@ static void nvme_copy_in_completed_cb(void *opaque, int ret)uint16_t prinfor = ((copy->control[0 ] >>4 ) &0xf );uint16_t prinfow = ((copy->control[2 ] >>2 ) &0xf ); -uint16_t apptag =le16_to_cpu (range->apptag); -uint16_t appmask =le16_to_cpu (range->appmask); -uint32_t reftag =le32_to_cpu (range->reftag); - -uint64_t slba =le64_to_cpu (range->slba);size_t mlen =nvme_m2b (ns, nlb);uint8_t *mbounce = iocb->bounce +nvme_l2b (ns, nlb);
@@ -2764,7 +2855,6 @@ static void nvme_copy_in_cb(void *opaque, int ret)NvmeCopyAIOCB *iocb = opaque;NvmeRequest *req = iocb->req;NvmeNamespace *ns = req->ns; -NvmeCopySourceRange *range;uint64_t slba;uint32_t nlb;
@@ -2780,9 +2870,8 @@ static void nvme_copy_in_cb(void *opaque, int ret)return ; } - range = &iocb->ranges[iocb->idx]; - slba =le64_to_cpu (range->slba); - nlb =le32_to_cpu (range->nlb) +1 ; +nvme_copy_source_range_parse (iocb->ranges, iocb->idx, iocb->format, &slba, + &nlb,NULL ,NULL ,NULL );qemu_iovec_reset (&iocb->iov); qemu_iovec_add(&iocb->iov, iocb->bounce + nvme_l2b(ns, nlb),
@@ -2802,7 +2891,6 @@ static void nvme_copy_cb(void *opaque, int ret)NvmeCopyAIOCB *iocb = opaque;NvmeRequest *req = iocb->req;NvmeNamespace *ns = req->ns; -NvmeCopySourceRange *range;uint64_t slba;uint32_t nlb;size_t len;
@@ -2819,9 +2907,8 @@ static void nvme_copy_cb(void *opaque, int ret) goto done; } - range = &iocb->ranges[iocb->idx]; - slba =le64_to_cpu (range->slba); - nlb =le32_to_cpu (range->nlb) +1 ; +nvme_copy_source_range_parse (iocb->ranges, iocb->idx, iocb->format, &slba, + &nlb,NULL ,NULL ,NULL ); len =nvme_l2b (ns, nlb);trace_pci_nvme_copy_source_range (slba, nlb);
@@ -2877,6 +2964,7 @@ static uint16_t nvme_copy(NvmeCtrl *n, NvmeRequest *req)uint8_t format = copy->control[0 ] &0xf ;uint16_t prinfor = ((copy->control[0 ] >>4 ) &0xf );uint16_t prinfow = ((copy->control[2 ] >>2 ) &0xf ); +size_t len =sizeof (NvmeCopySourceRangeFormat0);uint16_t status;
@@ -2902,10 +2990,18 @@ static uint16_t nvme_copy(NvmeCtrl *n, NvmeRequest *req) goto invalid; } - iocb->ranges =g_new (NvmeCopySourceRange, nr); +if (ns->pif && format !=0x1 ) { + status =NVME_INVALID_FORMAT |NVME_DNR ; + goto invalid; + } - status =nvme_h2c (n, (uint8_t *)iocb->ranges, -sizeof (NvmeCopySourceRange) * nr, req); +if (ns->pif) { + len =sizeof (NvmeCopySourceRangeFormat1); + } + + iocb->format = format; + iocb->ranges =g_malloc_n (nr, len); + status =nvme_h2c (n, (uint8_t *)iocb->ranges, len * nr, req);if (status) { goto invalid; }
@@ -2931,6 +3027,7 @@ static uint16_t nvme_copy(NvmeCtrl *n, NvmeRequest *req) iocb->nr = nr; iocb->idx =0 ; iocb->reftag =le32_to_cpu (copy->reftag); + iocb->reftag |= (uint64_t )le32_to_cpu (copy->cdw3) <<32 ; iocb->bounce =g_malloc_n (le16_to_cpu (ns->id_ns.mssrl), ns->lbasz + ns->lbaf.ms);
@@ -4717,7 +4814,8 @@ static uint16_t nvme_identify_ns_csi(NvmeCtrl *n, NvmeRequest *req, }if (c->csi ==NVME_CSI_NVM ) { -return nvme_rpt_empty_id_struct (n, req); +return nvme_c2h (n, (uint8_t *)&ns->id_ns_nvm,sizeof (NvmeIdNsNvm), + req); }else if (c->csi ==NVME_CSI_ZONED && ns->csi ==NVME_CSI_ZONED ) {return nvme_c2h (n, (uint8_t *)ns->id_ns_zoned,sizeof (NvmeIdNsZoned), req);
@@ -5571,6 +5669,9 @@ static void nvme_format_bh(void *opaque)NvmeFormatAIOCB *iocb = opaque;NvmeRequest *req = iocb->req;NvmeCtrl *n =nvme_ctrl (req); +uint32_t dw10 =le32_to_cpu (req->cmd.cdw10); +uint8_t lbaf = dw10 &0xf ; +uint8_t pi = (dw10 >>5 ) &0x7 ;uint16_t status;int i;
@@ -5592,7 +5693,7 @@ static void nvme_format_bh(void *opaque) goto done; } - status =nvme_format_check (iocb->ns, iocb->lbaf, iocb->pi); + status =nvme_format_check (iocb->ns, lbaf, pi);if (status) { req->status = status; goto done;
@@ -6671,7 +6772,7 @@ static void nvme_init_ctrl(NvmeCtrl *n, PCIDevice *pci_dev)*/ id->vwc =NVME_VWC_NSID_BROADCAST_SUPPORT |NVME_VWC_PRESENT ; - id->ocfs =cpu_to_le16 (NVME_OCFS_COPY_FORMAT_0 ); + id->ocfs =cpu_to_le16 (NVME_OCFS_COPY_FORMAT_0 |NVME_OCFS_COPY_FORMAT_1 ); id->sgls =cpu_to_le32 (NVME_CTRL_SGLS_SUPPORT_NO_ALIGN |NVME_CTRL_SGLS_BITBUCKET );
hw/nvme/dif.c +282 -81
447 lines not on this page · raw message
hw/nvme/dif.h +138 -5
167 lines not on this page · raw message
hw/nvme/ns.c +12 -0
@@ -58,6 +58,7 @@ static int nvme_ns_init(NvmeNamespace *ns, Error **errp) {static uint64_t ns_count;NvmeIdNs *id_ns = &ns->id_ns; +NvmeIdNsNvm *id_ns_nvm = &ns->id_ns_nvm;uint8_t ds;uint16_t ms;int i;
@@ -101,6 +102,8 @@ static int nvme_ns_init(NvmeNamespace *ns, Error **errp) id_ns->dps |=NVME_ID_NS_DPS_FIRST_EIGHT ; } + ns->pif = ns->params.pif; +static const NvmeLBAF lbaf[16 ] = { [0 ] = { .ds =9 }, [1 ] = { .ds =9 , .ms =8 },
@@ -133,7 +136,9 @@ static int nvme_ns_init(NvmeNamespace *ns, Error **errp) id_ns->flbas |= i; + lbaf_found: + id_ns_nvm->elbaf[i] = (ns->pif &0x3 ) <<7 ; id_ns->nlbaf = ns->nlbaf -1 ;nvme_ns_init_format (ns);
@@ -384,6 +389,12 @@ static int nvme_ns_check_constraints(NvmeNamespace *ns, Error **errp)return -1 ; } +if (ns->params.pif !=NVME_PI_GUARD_16 && + ns->params.pif !=NVME_PI_GUARD_64 ) { +error_setg (errp,"invalid 'pif'" ); +return -1 ; + } +if (ns->params.nsid >NVME_MAX_NAMESPACES ) {error_setg (errp,"invalid namespace id (must be between 0 and %d)" ,NVME_MAX_NAMESPACES );
@@ -593,6 +604,7 @@ static Property nvme_ns_props[] = {DEFINE_PROP_UINT8 ("mset" , NvmeNamespace, params.mset,0 ),DEFINE_PROP_UINT8 ("pi" , NvmeNamespace, params.pi,0 ),DEFINE_PROP_UINT8 ("pil" , NvmeNamespace, params.pil,0 ), +DEFINE_PROP_UINT8 ("pif" , NvmeNamespace, params.pif,0 ),DEFINE_PROP_UINT16 ("mssrl" , NvmeNamespace, params.mssrl,128 ),DEFINE_PROP_UINT32 ("mcl" , NvmeNamespace, params.mcl,128 ), DEFINE_PROP_UINT8("msrc", NvmeNamespace, params.msrc, 127),
hw/nvme/nvme.h +3 -0
@@ -102,6 +102,7 @@ typedef struct NvmeNamespaceParams {uint8_t mset;uint8_t pi;uint8_t pil; +uint8_t pif;uint16_t mssrl;uint32_t mcl;
@@ -127,6 +128,7 @@ typedef struct NvmeNamespace {int64_t size;int64_t moff;NvmeIdNs id_ns; +NvmeIdNsNvm id_ns_nvm;NvmeLBAF lbaf;unsigned int nlbaf;size_t lbasz;
@@ -134,6 +136,7 @@ typedef struct NvmeNamespace {uint8_t csi;uint16_t status;int attached; +uint8_t pif;struct {uint16_t zrwas;
hw/nvme/trace-events +8 -4
@@ -20,12 +20,16 @@ pci_nvme_dif_rw_cb(uint16_t cid, const char *blkname) "cid %"PRIu16" blk '%s'" pci_nvme_dif_rw_mdata_in_cb(uint16_t cid, const char *blkname) "cid %"PRIu16" blk '%s'" pci_nvme_dif_rw_mdata_out_cb(uint16_t cid, const char *blkname) "cid %"PRIu16" blk '%s'" pci_nvme_dif_rw_check_cb(uint16_t cid, uint8_t prinfo, uint16_t apptag, uint16_t appmask, uint32_t reftag) "cid %"PRIu16" prinfo 0x%"PRIx8" apptag 0x%"PRIx16" appmask 0x%"PRIx16" reftag 0x%"PRIx32"" -pci_nvme_dif_pract_generate_dif(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_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(uint16_t apptag, uint32_t reftag) "apptag 0x%"PRIx16" reftag 0x%"PRIx32"" -pci_nvme_dif_prchk_guard(uint16_t guard, uint16_t crc) "guard 0x%"PRIx16" crc 0x%"PRIx16"" +pci_nvme_dif_prchk_disabled_crc16(uint16_t apptag, uint32_t reftag) "apptag 0x%"PRIx16" reftag 0x%"PRIx32"" +pci_nvme_dif_prchk_disabled_crc64(uint16_t apptag, uint64_t reftag) "apptag 0x%"PRIx16" reftag 0x%"PRIx64"" +pci_nvme_dif_prchk_guard_crc16(uint16_t guard, uint16_t crc) "guard 0x%"PRIx16" crc 0x%"PRIx16"" +pci_nvme_dif_prchk_guard_crc64(uint64_t guard, uint64_t crc) "guard 0x%"PRIx64" 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"" -pci_nvme_dif_prchk_reftag(uint32_t reftag, uint32_t elbrt) "reftag 0x%"PRIx32" elbrt 0x%"PRIx32"" +pci_nvme_dif_prchk_reftag_crc16(uint32_t reftag, uint32_t elbrt) "reftag 0x%"PRIx32" elbrt 0x%"PRIx32"" +pci_nvme_dif_prchk_reftag_crc64(uint64_t reftag, uint64_t elbrt) "reftag 0x%"PRIx64" elbrt 0x%"PRIx64"" pci_nvme_copy(uint16_t cid, uint32_t nsid, uint16_t nr, uint8_t format) "cid %"PRIu16" nsid %"PRIu32" nr %"PRIu16" format 0x%"PRIx8"" pci_nvme_copy_source_range(uint64_t slba, uint32_t nlb) "slba 0x%"PRIx64" nlb %"PRIu32"" pci_nvme_copy_out(uint64_t slba, uint32_t nlb) "slba 0x%"PRIx64" nlb %"PRIu32""
include/block/nvme.h +54 -13
151 lines not on this page · raw message
Signature · 1 line
2.35.1
Re: [PATCH 6/6] hw/nvme: 64-bit pi support
hw/nvme/ns.c static int nvme_ns_check_constraints(NvmeNamespace *ns, Error **errp) · line 396
Quoted 9 lines · Klaus Jensen
hw/nvme/ns.c +5
-0@@ -384,6 +389,12 @@ static int nvme_ns_check_constraints(NvmeNamespace *ns, Error **errp)return -1 ; } +if (ns->params.pif !=NVME_PI_GUARD_16 && + ns->params.pif !=NVME_PI_GUARD_64 ) { +error_setg (errp,"invalid 'pif'" ); +return -1 ; + }
In addition, the requested metadata size ('params.ms') should be checked against the requested PI option. The function currently just checks against 8 bytes, but the 64b guard requires at least 16 bytes.
Otherwise, looks great.
Quoted 6 lines · Klaus Jensen
From: Klaus Jensen <[email protected]>
This adds support for one possible new protection information format introduced in TP4068 (and integrated in NVMe 2.0): the 64-bit CRC guard and 48-bit reference tag. This version does not support storage tags.
Quoted 8 lines
Like the CRC16 support already present, this uses a software implementation of CRC64 (so it is naturally pretty slow). But its good enough for verification purposes.
This goes hand-in-hand with the support that Keith submitted for the Linux kernel[1].
Other than comment on 6/6, series looks good to me.