git: 764761d3169d - main - nvme: program the arbitration burst from Identify RAB

From: Abdelkader Boudih <seuros_at_FreeBSD.org>
Date: Fri, 06 Nov 2026 09:07:14 UTC
The branch main has been updated by seuros:

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

commit 764761d3169d37ebbf24566344e50bfca5f013b9
Author:     Abdelkader Boudih <seuros@FreeBSD.org>
AuthorDate: 2026-08-24 18:15:15 +0000
Commit:     Abdelkader Boudih <seuros@FreeBSD.org>
CommitDate: 2026-11-06 08:58:08 +0000

    nvme: program the arbitration burst from Identify RAB
    
    The controller's Recommended Arbitration Burst was ignored; the
    specification advises matching the Arbitration feature's burst to it
    even under plain round robin. Some controllers report unusable
    values: programming the Apple S3X with its reported RAB wedged
    sustained I/O, so quirked controllers keep their default
    configuration. The hw.nvme.arb_burst tunable overrides the
    recommendation.
    
    Reviewed by:    imp
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D59155
---
 share/man/man4/nvme.4         |  9 ++++++++-
 sys/dev/nvme/nvme.h           | 11 +++++++++++
 sys/dev/nvme/nvme_ctrlr.c     | 38 ++++++++++++++++++++++++++++++++++++++
 sys/dev/nvme/nvme_ctrlr_cmd.c |  9 +++++++++
 sys/dev/nvme/nvme_pci.c       |  5 +++--
 sys/dev/nvme/nvme_private.h   |  3 +++
 6 files changed, 72 insertions(+), 3 deletions(-)

diff --git a/share/man/man4/nvme.4 b/share/man/man4/nvme.4
index 10f0a067f48a..a45babfa5442 100644
--- a/share/man/man4/nvme.4
+++ b/share/man/man4/nvme.4
@@ -31,7 +31,7 @@
 .\"
 .\" Author: Jim Harris <jimharris@FreeBSD.org>
 .\"
-.Dd August 27, 2026
+.Dd September 1, 2026
 .Dt NVME 4
 .Os
 .Sh NAME
@@ -48,6 +48,7 @@ In
 .Cd hw.nvme.min_cpus_per_ioq= Ns Aq Ar X
 .Cd hw.nvme.force_intx= Ns Ar 1
 .Cd hw.nvme.hmb_max
+.Cd hw.nvme.arb_burst= Ns Aq Ar X
 .Cd hw.nvme.apst_enable= Ns Ar 1
 .Cd hw.nvme.apst_data
 .Cd hw.nvme.use_nvd= Ns Ar 0
@@ -166,6 +167,12 @@ Note that use of INTx implies disabling of per-CPU I/O queue pairs.
 Control maximum amount of system RAM in bytes
 to use as Host Memory Buffer for capable devices.
 The default value is 5% of physical memory size per device.
+.It Va hw.nvme.arb_burst=X
+Override the arbitration burst,
+which is otherwise programmed from the controller's recommendation
+when it publishes one.
+Values 0 through 6 request a burst of 2^X commands and 7 requests no
+limit; -1, the default, follows the controller's recommendation.
 .It Va hw.nvme.apst_enable=1
 Enable Autonomous Power State Transition
 .Pq APST .
diff --git a/sys/dev/nvme/nvme.h b/sys/dev/nvme/nvme.h
index ae31170f5379..98caf9a860d0 100644
--- a/sys/dev/nvme/nvme.h
+++ b/sys/dev/nvme/nvme.h
@@ -258,6 +258,17 @@ enum nvme_psdt {
 #define NVME_QPRIO_MEDIUM				(2)
 #define NVME_QPRIO_LOW					(3)
 
+/* Arbitration feature, cdw11 */
+#define NVME_ARB_AB_SHIFT				(0)
+#define NVME_ARB_AB_MASK				(0x7)
+#define NVME_ARB_AB_NO_LIMIT				(0x7)
+#define NVME_ARB_LPW_SHIFT				(8)
+#define NVME_ARB_LPW_MASK				(0xFF)
+#define NVME_ARB_MPW_SHIFT				(16)
+#define NVME_ARB_MPW_MASK				(0xFF)
+#define NVME_ARB_HPW_SHIFT				(24)
+#define NVME_ARB_HPW_MASK				(0xFF)
+
 #define NVME_STATUS_P_SHIFT				(0)
 #define NVME_STATUS_P_MASK				(0x1)
 #define NVME_STATUS_SC_SHIFT				(1)
diff --git a/sys/dev/nvme/nvme_ctrlr.c b/sys/dev/nvme/nvme_ctrlr.c
index 354ca7a78e51..03f24a01c7ca 100644
--- a/sys/dev/nvme/nvme_ctrlr.c
+++ b/sys/dev/nvme/nvme_ctrlr.c
@@ -967,6 +967,43 @@ nvme_ctrlr_configure_timestamp(struct nvme_controller *ctrlr)
 	nvme_ctrlr_set_timestamp(ctrlr);
 }
 
+static void
+nvme_ctrlr_arbitration_done(void *arg, const struct nvme_completion *cpl)
+{
+	struct nvme_controller *ctrlr = arg;
+
+	if (nvme_completion_is_error(cpl))
+		nvme_printf(ctrlr, "setting arbitration burst failed\n");
+}
+
+static void
+nvme_ctrlr_configure_arbitration(struct nvme_controller *ctrlr)
+{
+	int burst;
+
+	burst = -1;
+	TUNABLE_INT_FETCH("hw.nvme.arb_burst", &burst);
+	if (burst < -1 || burst > NVME_ARB_AB_NO_LIMIT) {
+		nvme_printf(ctrlr, "invalid hw.nvme.arb_burst=%d specified\n",
+		    burst);
+		burst = -1;
+	}
+
+	if (burst == -1) {
+		if (ctrlr->cdata.rab == 0 ||
+		    (ctrlr->quirks & QUIRK_IGNORE_RAB) != 0)
+			return;
+		burst = min(ctrlr->cdata.rab, NVME_ARB_AB_NO_LIMIT);
+	} else if ((ctrlr->quirks & QUIRK_IGNORE_RAB) != 0) {
+		nvme_printf(ctrlr,
+		    "forcing arbitration burst %d despite controller quirk\n",
+		    burst);
+	}
+
+	nvme_ctrlr_cmd_set_arbitration(ctrlr, NVMEF(NVME_ARB_AB, burst),
+	    nvme_ctrlr_arbitration_done, ctrlr);
+}
+
 static void
 nvme_ctrlr_configure_int_coalescing(struct nvme_controller *ctrlr)
 {
@@ -1234,6 +1271,7 @@ nvme_ctrlr_start(void *ctrlr_arg, bool resetting)
 
 	nvme_ctrlr_configure_aer(ctrlr);
 	nvme_ctrlr_configure_apst(ctrlr);
+	nvme_ctrlr_configure_arbitration(ctrlr);
 	nvme_ctrlr_configure_int_coalescing(ctrlr);
 	nvme_ctrlr_configure_timestamp(ctrlr);
 
diff --git a/sys/dev/nvme/nvme_ctrlr_cmd.c b/sys/dev/nvme/nvme_ctrlr_cmd.c
index 27a2848038d5..b9a60e10cb72 100644
--- a/sys/dev/nvme/nvme_ctrlr_cmd.c
+++ b/sys/dev/nvme/nvme_ctrlr_cmd.c
@@ -254,6 +254,15 @@ nvme_ctrlr_cmd_set_interrupt_coalescing(struct nvme_controller *ctrlr,
 	    0, 0, 0, 0, NULL, 0, cb_fn, cb_arg);
 }
 
+void
+nvme_ctrlr_cmd_set_arbitration(struct nvme_controller *ctrlr,
+    uint32_t cdw11, nvme_cb_fn_t cb_fn, void *cb_arg)
+{
+
+	nvme_ctrlr_cmd_set_feature(ctrlr, NVME_FEAT_ARBITRATION, cdw11,
+	    0, 0, 0, 0, NULL, 0, cb_fn, cb_arg);
+}
+
 void
 nvme_ctrlr_cmd_get_log_page(struct nvme_controller *ctrlr, uint8_t log_page,
     uint32_t nsid, void *payload, uint32_t payload_size, nvme_cb_fn_t cb_fn,
diff --git a/sys/dev/nvme/nvme_pci.c b/sys/dev/nvme/nvme_pci.c
index 5d4fab3b2ab1..cdc60f4a9a3c 100644
--- a/sys/dev/nvme/nvme_pci.c
+++ b/sys/dev/nvme/nvme_pci.c
@@ -99,11 +99,12 @@ static struct _pcsid
 	{ 0x07f015ad,		0, 0, "VMware NVMe Controller" },
 	{ 0x2003106b,		0, 0, "Apple S3X NVMe Controller",
 	    QUIRK_APPLE_NO_ASYNC_EVENT | QUIRK_APPLE_S3X_NS1_ONLY |
-	    QUIRK_PCIE_FLR_ON_FATAL | QUIRK_APPLE_S3X_SERIALIZE },
+	    QUIRK_PCIE_FLR_ON_FATAL | QUIRK_APPLE_S3X_SERIALIZE |
+	    QUIRK_IGNORE_RAB },
 	{ 0x2005106b,		0, 0, "Apple ANS2 NVMe Controller (T2)",
 	    QUIRK_APPLE_IDENTIFY_CNS_BROKEN | QUIRK_APPLE_SHARED_CID_SPACE |
 	    QUIRK_APPLE_NO_ASYNC_EVENT | QUIRK_APPLE_SINGLE_VECTOR |
-	    QUIRK_APPLE_128_BYTE_SQES },
+	    QUIRK_APPLE_128_BYTE_SQES | QUIRK_IGNORE_RAB },
 	{ 0x80611d0f,		0, 0, "Amazon EBS NVMe Controller",
 	    QUIRK_EMPTY_NAMESPACE_CHANGED_LOG },
 	{ 0x00000000,		0, 0, NULL  }
diff --git a/sys/dev/nvme/nvme_private.h b/sys/dev/nvme/nvme_private.h
index b55159145de9..aad9c2b7d7d8 100644
--- a/sys/dev/nvme/nvme_private.h
+++ b/sys/dev/nvme/nvme_private.h
@@ -249,6 +249,7 @@ struct nvme_controller {
 #define	QUIRK_APPLE_128_BYTE_SQES		0x400	/* T2 uses 128-byte I/O SQEs */
 #define	QUIRK_PCIE_FLR_ON_FATAL			0x800	/* Use FLR for a fatal controller */
 #define	QUIRK_APPLE_S3X_SERIALIZE		0x1000	/* One S3X I/O at a time */
+#define	QUIRK_IGNORE_RAB			0x2000	/* Don't configure AB from RAB */
 
 /* Values programmed into CC.IOSQES (log2 of the SQE size in bytes). */
 #define	NVME_IOSQES_64				6
@@ -411,6 +412,8 @@ void	nvme_ctrlr_cmd_set_interrupt_coalescing(struct nvme_controller *ctrlr,
 						uint32_t threshold,
 						nvme_cb_fn_t cb_fn,
 						void *cb_arg);
+void	nvme_ctrlr_cmd_set_arbitration(struct nvme_controller *ctrlr,
+	    uint32_t cdw11, nvme_cb_fn_t cb_fn, void *cb_arg);
 void	nvme_ctrlr_cmd_get_error_page(struct nvme_controller *ctrlr,
 				      struct nvme_error_information_entry *payload,
 				      uint32_t num_entries, /* 0 = max */