git: a70b5660f379 - main - nvme: MPS is a power of two, not a size / 8k

From: Warner Losh <imp_at_FreeBSD.org>
Date: Fri, 01 Apr 2022 03:17:57 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=a70b5660f379d6400d8238cbdec6309d2990a0f9

commit a70b5660f379d6400d8238cbdec6309d2990a0f9
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2022-03-29 19:21:55 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2022-04-01 03:12:38 +0000

    nvme: MPS is a power of two, not a size / 8k
    
    Setting MPS in the CC should be a power of 2 number (it specifies the
    page size of the host is 2^(12+MPS)), so adjust the calcuation. There is
    no functional change because we do not support any architecutres != 4k
    pages (yet). Other changes are needed for architectures with 16k or 64k
    pages, especially when the underlying NVMe drive doesn't support that
    page size (Most drives support a range that's small, and many only
    support 4k), but let's at least do this calculation correctly. 12 - 12
    is just as much 0 as 4096 >> 13 is :)
    
    Sponsored by:           Netflix
    Reviewed by:            mav
    Differential Revision:  https://reviews.freebsd.org/D34707
---
 sys/dev/nvme/nvme_ctrlr.c   | 2 +-
 sys/dev/nvme/nvme_private.h | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/sys/dev/nvme/nvme_ctrlr.c b/sys/dev/nvme/nvme_ctrlr.c
index aa41c8fe5540..a632d7fb68e9 100644
--- a/sys/dev/nvme/nvme_ctrlr.c
+++ b/sys/dev/nvme/nvme_ctrlr.c
@@ -389,7 +389,7 @@ nvme_ctrlr_enable(struct nvme_controller *ctrlr)
 	cc |= 4 << NVME_CC_REG_IOCQES_SHIFT; /* CQ entry size == 16 == 2^4 */
 
 	/* This evaluates to 0, which is according to spec. */
-	cc |= (PAGE_SIZE >> 13) << NVME_CC_REG_MPS_SHIFT;
+	cc |= (PAGE_SHIFT - NVME_BASE_SHIFT) << NVME_CC_REG_MPS_SHIFT;
 
 	nvme_ctrlr_barrier(ctrlr, BUS_SPACE_BARRIER_WRITE);
 	nvme_mmio_write_4(ctrlr, cc, cc);
diff --git a/sys/dev/nvme/nvme_private.h b/sys/dev/nvme/nvme_private.h
index c889246f9350..977cc2c8d30d 100644
--- a/sys/dev/nvme/nvme_private.h
+++ b/sys/dev/nvme/nvme_private.h
@@ -96,6 +96,11 @@ MALLOC_DECLARE(M_NVME);
 /* Maximum log page size to fetch for AERs. */
 #define NVME_MAX_AER_LOG_SIZE		(4096)
 
+/*
+ * Page size parameters
+ */
+#define NVME_BASE_SHIFT		12	/* Several parameters (MSP) are 2^(12+x) */
+
 /*
  * Define CACHE_LINE_SIZE here for older FreeBSD versions that do not define
  *  it.