]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
nvme: add from0based() helper
authorCaleb Sander Mateos <csander@purestorage.com>
Fri, 27 Feb 2026 20:23:50 +0000 (13:23 -0700)
committerKeith Busch <kbusch@kernel.org>
Fri, 27 Mar 2026 14:35:04 +0000 (07:35 -0700)
The NVMe specifications are big fans of "0's based"/"0-based" fields for
encoding values that must be positive. The encoded value is 1 less than
the value it represents. nvmet already provides a helper to0based() for
encoding 0's based values, so add a corresponding helper to decode these
fields on the host side.

Suggested-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Caleb Sander Mateos <csander@purestorage.com>
Signed-off-by: Keith Busch <kbusch@kernel.org>
drivers/nvme/host/nvme.h

index 9971045dbc05e9bb9d7fa32ad540fd107d8c8b83..ccd5e05dac98f05f7c4149ce8288eec205e5d891 100644 (file)
@@ -762,6 +762,12 @@ static inline u32 nvme_bytes_to_numd(size_t len)
        return (len >> 2) - 1;
 }
 
+/* Decode a 2-byte "0's based"/"0-based" field */
+static inline u32 from0based(__le16 value)
+{
+       return (u32)le16_to_cpu(value) + 1;
+}
+
 static inline bool nvme_is_ana_error(u16 status)
 {
        switch (status & NVME_SCT_SC_MASK) {