From: Caleb Sander Mateos Date: Fri, 27 Feb 2026 20:23:50 +0000 (-0700) Subject: nvme: add from0based() helper X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b465046c8cca9e418c7b39cfb41bb8e3b62f22f6;p=thirdparty%2Fkernel%2Flinux.git nvme: add from0based() helper 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 Signed-off-by: Caleb Sander Mateos Signed-off-by: Keith Busch --- diff --git a/drivers/nvme/host/nvme.h b/drivers/nvme/host/nvme.h index 9971045dbc05e..ccd5e05dac98f 100644 --- a/drivers/nvme/host/nvme.h +++ b/drivers/nvme/host/nvme.h @@ -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) {