Skip to contentSkip to conversation

ata: fix deferred QC handling for port multipliers

PATCH Applied Niklas Cassel 5 participants 16 messages May 14 – May 27 libata

lore
Share & tools
lore

Niklas Cassel

author maintainer
#0 lore raw

v5 cover ata: fix deferred QC handling for port multipliers

Hello all,

Tommy Kelly reported a regression with PMP that use CBS:
https://lore.kernel.org/linux-ide/[email protected]/

It turns out that the ap->excl_link logic used for PMP that use CBS is incompatible with the deferred qc issuing via a workqueue. This is fixed in patch 3/4.

While looking at the code, it turns out that the deferred qc issuing via workqueue is misdesigned. It assumed that we can't mix NCQ and non-NCQ commands on the same port. The limitation is that you can not mix NCQ and non-NCQ commands on the same drive. However, with a PMP with FBS, you can issue (mixed NCQ and non-NCQ commands) to the different drives. Thus, move the saved deferred QC from struct ata_port to struct ata_link. This is fixed in patch 4/4.

Changes since v4:
-Fix ATA_DEFER_LINK_EXCL to use the correct goto label. While refactoring,
I accidentally used the wrong label goto defer_qc instead of goto free_qc, which of course broke PMP CBS in v4.

Niklas Cassel (4):
ata: libata-scsi: improve readability of ata_scsi_qc_issue()
ata: libata-scsi: do not use the deferred QC feature for
ATA_DEFER_PORT
ata: libata-scsi: do not use the deferred QC feature on PMPs with CBS
ata: libata-scsi: do not needlessly defer commands when using PMP with
FBS

 drivers/ata/libata-core.c |  9 ++--
 drivers/ata/libata-eh.c   |  8 ++--
 drivers/ata/libata-pmp.c  | 18 +++++++-
 drivers/ata/libata-scsi.c | 89 +++++++++++++++++++++++----------------
 drivers/ata/sata_sil24.c  |  6 ++-
 include/linux/libata.h    |  7 +--
 6 files changed, 87 insertions(+), 50 deletions(-)

base-commit: 0926c1b731b73e7296e4ad4f7d055918e16ae6ab

Signature · 1 line
2.54.0
author maintainer
#1 lore raw

PATCH v5 1/4 ata: libata-scsi: improve readability of ata_scsi_qc_issue()

Improve readability of ata_scsi_qc_issue().

No functional changes.

---
 drivers/ata/libata-scsi.c | 43 +++++++++++++++++++--------------------
 1 file changed, 21 insertions(+), 22 deletions(-)
drivers/ata/libata-scsi.c +21 -22
index 6733f2b14521..b9064dd4390c 100644
@@ -1767,7 +1767,7 @@ static int ata_scsi_qc_issue(struct ata_port *ap, struct ata_queued_cmd *qc)
 	int ret;
 
 	if (!ap->ops->qc_defer)
-		goto issue;
+		goto issue_qc;
 
 	/*
 	 * If we already have a deferred qc, then rely on the SCSI layer to
@@ -1786,38 +1786,37 @@ static int ata_scsi_qc_issue(struct ata_port *ap, struct ata_queued_cmd *qc)
 		break;
 	case ATA_DEFER_LINK:
 		ret = SCSI_MLQUEUE_DEVICE_BUSY;
-		break;
+		goto defer_qc;
 	case ATA_DEFER_PORT:
 		ret = SCSI_MLQUEUE_HOST_BUSY;
-		break;
+		goto defer_qc;
 	default:
 		WARN_ON_ONCE(1);
 		ret = SCSI_MLQUEUE_HOST_BUSY;
-		break;
+		goto defer_qc;
 	}
 
-	if (ret) {
-		/*
-		 * We must defer this qc: if this is not an NCQ command, keep
-		 * this qc as a deferred one and report to the SCSI layer that
-		 * we issued it so that it is not requeued. The deferred qc will
-		 * be issued with the port deferred_qc_work once all on-going
-		 * commands complete.
-		 */
-		if (!ata_is_ncq(qc->tf.protocol)) {
-			ap->deferred_qc = qc;
-			return 0;
-		}
+issue_qc:
+	ata_qc_issue(qc);
+	return 0;
 
-		/* Force a requeue of the command to defer its execution. */
-		ata_qc_free(qc);
-		return ret;
+defer_qc:
+	/*
+	 * We must defer this qc: if this is not an NCQ command, keep
+	 * this qc as a deferred one and report to the SCSI layer that
+	 * we issued it so that it is not requeued. The deferred qc will
+	 * be issued with the port deferred_qc_work once all on-going
+	 * commands complete.
+	 */
+	if (!ata_is_ncq(qc->tf.protocol)) {
+		ap->deferred_qc = qc;
+		return 0;
 	}
 
-issue:
-	ata_qc_issue(qc);
+	/* Force a requeue of the command to defer its execution. */
+	ata_qc_free(qc);
 
-	return 0;
+	return ret;
 }
 
 /**
Signature · 1 line
2.54.0
author maintainer
#3 lore raw

PATCH v5 2/4 ata: libata-scsi: do not use the deferred QC feature for ATA_DEFER_PORT

In reply to Niklas Cassel

The deferred QC feature was meant to handle mixed NCQ and non-NCQ commands, i.e. for return value ATA_DEFER_LINK.

ATA_DEFER_PORT is returned by PATA drivers, but also certain SATA drivers like sata_mv and sata_sil24 that uses ap->excl_link to workaround hardware bugs in these HBAs. Regardless of the reason, using the deferred QC feature for ATA_DEFER_PORT is always wrong, and will break the ap->excl_link usage of the SATA drivers that rely on that feature.

Modify ata_scsi_qc_issue() to only use the deferred QC feature when mixing NCQ and non-NCQ commands, i.e. ATA_DEFER_LINK.

Fixes 0ea84089dbf6 ("ata: libata-scsi: avoid Non-NCQ command starvation")
---
 drivers/ata/libata-scsi.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/ata/libata-scsi.c +3 -2
index b9064dd4390c..22642a0e6b84 100644
@@ -1789,11 +1789,11 @@ static int ata_scsi_qc_issue(struct ata_port *ap, struct ata_queued_cmd *qc)
 		goto defer_qc;
 	case ATA_DEFER_PORT:
 		ret = SCSI_MLQUEUE_HOST_BUSY;
-		goto defer_qc;
+		goto free_qc;
 	default:
 		WARN_ON_ONCE(1);
 		ret = SCSI_MLQUEUE_HOST_BUSY;
-		goto defer_qc;
+		goto free_qc;
 	}
 
 issue_qc:
@@ -1813,6 +1813,7 @@ static int ata_scsi_qc_issue(struct ata_port *ap, struct ata_queued_cmd *qc)
 		return 0;
 	}
 
+free_qc:
 	/* Force a requeue of the command to defer its execution. */
 	ata_qc_free(qc);
 
Signature · 1 line
2.54.0
author maintainer
#5 lore raw

PATCH v5 3/4 ata: libata-scsi: do not use the deferred QC feature on PMPs with CBS

In reply to Niklas Cassel

When using Port Multipliers (PMPs) with Command-Based Switching (CBS), you can only issue commands to one link at a time. For PMPs with CBS, there is already code to handle commands being sent to different links in sata_pmp_qc_defer_cmd_switch() using ap->excl_link. sata_sil24 also makes use of ap->excl_link.

A user on the list reported that commit 0ea84089dbf6 ("ata: libata-scsi:
avoid Non-NCQ command starvation") broke PMPs with CBS. The commit introduced code that stores a deferred qc in ap->deferred_qc, to later be issued via a workqueue. It turns out that this change is incompatible with the existing ap->excl_link handling used by PMPs with CBS.

Thus, modify sata_pmp_qc_defer_cmd_switch() and sil24_qc_defer() to return ATA_DEFER_LINK_EXCL, and make sure that the deferred QC handling via workqueue is not used for this return value.

This way, PMPs with CBS will work once again. Note that the starvation referenced in commit 0ea84089dbf6 ("ata: libata-scsi: avoid Non-NCQ command starvation") can only happen on libsas ports, and libsas does not support Port Multipliers, thus there is no harm of reverting back to the previous way of deferring commands for PMPs with CBS.

Non-libsas ports connected to anything but a PMP with CBS (e.g. a normal drive or a PMP with FBS) will continue using the deferred workqueue, since it does result in lower completion latencies for non-NCQ commands, even though the workqueue is not strictly needed to avoid starvation for non-libsas ports.

If we want to modify the scope of the workqueue issuing to also handle PMPs with CBS, then we should ensure that we can save both NCQ and non-NCQ commands in ap->deferred_qc, while also removing the existing PMP CBS handling using ap->excl_link, such that we don't duplicate features.

While at it, also add a comment explaining how the ap->excl_link mechanism works.

Fixes 0ea84089dbf6 ("ata: libata-scsi: avoid Non-NCQ command starvation")
---
 drivers/ata/libata-pmp.c  | 13 ++++++++++++-
 drivers/ata/libata-scsi.c |  8 ++++++++
 drivers/ata/sata_sil24.c  |  6 +++++-
 include/linux/libata.h    |  1 +
 4 files changed, 26 insertions(+), 2 deletions(-)
drivers/ata/libata-pmp.c +12 -1
index e3adc008fed1..7e889534d73b 100644
@@ -110,13 +110,24 @@ int sata_pmp_qc_defer_cmd_switch(struct ata_queued_cmd *qc)
 {
 	struct ata_link *link = qc->dev->link;
 	struct ata_port *ap = link->ap;
+	int ret;
 
 	if (ap->excl_link == NULL || ap->excl_link == link) {
 		if (ap->nr_active_links == 0 || ata_link_active(link)) {
 			qc->flags |= ATA_QCFLAG_CLEAR_EXCL;
-			return ata_std_qc_defer(qc);
+			ret = ata_std_qc_defer(qc);
+			if (ret == ATA_DEFER_LINK)
+				return ATA_DEFER_LINK_EXCL;
+			return ret;
 		}
 
+		/*
+		 * Note: ap->excl_link contains the link that is next in line,
+		 * i.e. implicit round robin. If there is only one link
+		 * dispatching, ap->excl_link will be left unclaimed, allowing
+		 * other links to set ap->excl_link, ensuring that the currently
+		 * active link cannot queue any more.
+		 */
 		ap->excl_link = link;
 	}
 
drivers/ata/libata-scsi.c +8 -0
index 22642a0e6b84..10f1ee8acc37 100644
@@ -1787,6 +1787,14 @@ static int ata_scsi_qc_issue(struct ata_port *ap, struct ata_queued_cmd *qc)
 	case ATA_DEFER_LINK:
 		ret = SCSI_MLQUEUE_DEVICE_BUSY;
 		goto defer_qc;
+	case ATA_DEFER_LINK_EXCL:
+		/*
+		 * Drivers making use of ap->excl_link cannot store the QC in
+		 * ap->deferred_qc, because the ap->excl_link handling is
+		 * incompatible with the ap->deferred_qc workqueue handling.
+		 */
+		ret = SCSI_MLQUEUE_DEVICE_BUSY;
+		goto free_qc;
 	case ATA_DEFER_PORT:
 		ret = SCSI_MLQUEUE_HOST_BUSY;
 		goto free_qc;
drivers/ata/sata_sil24.c +5 -1
index d642ece9f07a..57f1081b86db 100644
@@ -789,6 +789,7 @@ static int sil24_qc_defer(struct ata_queued_cmd *qc)
 	struct ata_link *link = qc->dev->link;
 	struct ata_port *ap = link->ap;
 	u8 prot = qc->tf.protocol;
+	int ret;
 
 	/*
 	 * There is a bug in the chip:
@@ -826,7 +827,10 @@ static int sil24_qc_defer(struct ata_queued_cmd *qc)
 		qc->flags |= ATA_QCFLAG_CLEAR_EXCL;
 	}
 
-	return ata_std_qc_defer(qc);
+	ret = ata_std_qc_defer(qc);
+	if (ret == ATA_DEFER_LINK)
+		return ATA_DEFER_LINK_EXCL;
+	return ret;
 }
 
 static enum ata_completion_errors sil24_qc_prep(struct ata_queued_cmd *qc)
include/linux/libata.h +1 -0
index 5c085ef4eda7..360776016b50 100644
@@ -371,6 +371,7 @@ enum {
 	/* return values for ->qc_defer */
 	ATA_DEFER_LINK		= 1,
 	ATA_DEFER_PORT		= 2,
+	ATA_DEFER_LINK_EXCL	= 3,
 
 	/* desc_len for ata_eh_info and context */
 	ATA_EH_DESC_LEN		= 80,
Signature · 1 line
2.54.0
author maintainer
#7 lore raw

PATCH v5 4/4 ata: libata-scsi: do not needlessly defer commands when using PMP with FBS

In reply to Niklas Cassel

The SATA specification does not allow a non-NCQ command to be issued while an NCQ command is outstanding.

Commit 0ea84089dbf6 ("ata: libata-scsi: avoid Non-NCQ command starvation") introduced a feature where a deferred non-NCQ command gets issued from a workqueue. The design stores a single non-NCQ command per port.

However, when using Port Multipliers (PMPs), specifically PMPs that support FIS-Based Switching (FBS), non-NCQ and NCQ commands can be mixed on the same port, just not for the same link, see e.g. ata_std_qc_defer() which is, and always has operated on a per-link basis.

Therefore, move the deferred_qc from struct ata_port to struct ata_link. This way, when using a PMP with FBS, we will not needlessly defer commands to all other links, just because one link issued a non-NCQ command while having an NCQ command outstanding. Only commands for that specific link will be deferred.

Fixes 0ea84089dbf6 ("ata: libata-scsi: avoid Non-NCQ command starvation")
---
 drivers/ata/libata-core.c |  9 +++++---
 drivers/ata/libata-eh.c   |  8 ++++----
 drivers/ata/libata-pmp.c  |  5 ++++-
 drivers/ata/libata-scsi.c | 43 +++++++++++++++++++++++----------------
 include/linux/libata.h    |  6 +++---
 5 files changed, 42 insertions(+), 29 deletions(-)
drivers/ata/libata-core.c +6 -3
index e76d15411e2a..3d0027ec33c2 100644
@@ -5584,6 +5584,7 @@ void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
 	link->pmp = pmp;
 	link->active_tag = ATA_TAG_POISON;
 	link->hw_sata_spd_limit = UINT_MAX;
+	INIT_WORK(&link->deferred_qc_work, ata_scsi_deferred_qc_work);
 
 	/* can't use iterator, ap isn't initialized yet */
 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
@@ -5666,7 +5667,6 @@ struct ata_port *ata_port_alloc(struct ata_host *host)
 	mutex_init(&ap->scsi_scan_mutex);
 	INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
 	INIT_DELAYED_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
-	INIT_WORK(&ap->deferred_qc_work, ata_scsi_deferred_qc_work);
 	INIT_LIST_HEAD(&ap->eh_done_q);
 	init_waitqueue_head(&ap->eh_wait_q);
 	init_completion(&ap->park_req_pending);
@@ -6291,12 +6291,15 @@ static void ata_port_detach(struct ata_port *ap)
 
 	/* It better be dead now and not have any remaining deferred qc. */
 	WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
-	WARN_ON(ap->deferred_qc);
 
-	cancel_work_sync(&ap->deferred_qc_work);
 	cancel_delayed_work_sync(&ap->hotplug_task);
 	cancel_delayed_work_sync(&ap->scsi_rescan_task);
 
+	ata_for_each_link(link, ap, PMP_FIRST) {
+		WARN_ON(link->deferred_qc);
+		cancel_work_sync(&link->deferred_qc_work);
+	}
+
 	/* Delete port multiplier link transport devices */
 	if (ap->pmp_link) {
 		int i;
drivers/ata/libata-eh.c +4 -4
index 6cb79a09423d..715bc525b38f 100644
@@ -651,11 +651,11 @@ int ata_scsi_cmd_error_handler(struct Scsi_Host *host, struct ata_port *ap,
 			if (qc->scsicmd != scmd)
 				continue;
 			if ((qc->flags & ATA_QCFLAG_ACTIVE) ||
-			    qc == ap->deferred_qc)
+			    qc == qc->dev->link->deferred_qc)
 				break;
 		}
 
-		if (i < ATA_MAX_QUEUE && qc == ap->deferred_qc) {
+		if (i < ATA_MAX_QUEUE && qc == qc->dev->link->deferred_qc) {
 			/*
 			 * This is a deferred command that timed out while
 			 * waiting for the command queue to drain. Since the qc
@@ -666,8 +666,8 @@ int ata_scsi_cmd_error_handler(struct Scsi_Host *host, struct ata_port *ap,
 			 * deferred qc work from issuing this qc.
 			 */
 			WARN_ON_ONCE(qc->flags & ATA_QCFLAG_ACTIVE);
-			ap->deferred_qc = NULL;
-			cancel_work(&ap->deferred_qc_work);
+			qc->dev->link->deferred_qc = NULL;
+			cancel_work(&qc->dev->link->deferred_qc_work);
 			set_host_byte(scmd, DID_TIME_OUT);
 			scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
 		} else if (i < ATA_MAX_QUEUE) {
drivers/ata/libata-pmp.c +4 -1
index 7e889534d73b..e8540931b4a1 100644
@@ -582,8 +582,11 @@ static void sata_pmp_detach(struct ata_device *dev)
 	if (ap->ops->pmp_detach)
 		ap->ops->pmp_detach(ap);
 
-	ata_for_each_link(tlink, ap, EDGE)
+	ata_for_each_link(tlink, ap, EDGE) {
+		WARN_ON(tlink->deferred_qc);
+		cancel_work_sync(&tlink->deferred_qc_work);
 		ata_eh_detach_dev(tlink->device);
+	}
 
 	spin_lock_irqsave(ap->lock, flags);
 	ap->nr_pmp_links = 0;
drivers/ata/libata-scsi.c +25 -18

111 lines not on this page · raw message

include/linux/libata.h +3 -3
index 360776016b50..127229fbd1a6 100644
@@ -855,6 +855,9 @@ struct ata_link {
 	unsigned int		sata_spd;	/* current SATA PHY speed */
 	enum ata_lpm_policy	lpm_policy;
 
+	struct work_struct	deferred_qc_work;
+	struct ata_queued_cmd	*deferred_qc;
+
 	/* record runtime error info, protected by host_set lock */
 	struct ata_eh_info	eh_info;
 	/* EH context */
@@ -900,9 +903,6 @@ struct ata_port {
 	u64			qc_active;
 	int			nr_active_links; /* #links with active qcs */
 
-	struct work_struct	deferred_qc_work;
-	struct ata_queued_cmd	*deferred_qc;
-
 	struct ata_link		link;		/* host default link */
 	struct ata_link		*slave_link;	/* see ata_slave_link_init() */
 
Signature · 1 line
2.54.0
#9 lore raw
In reply to Niklas Cassel
Quoted 5 lines · Niklas Cassel

Changes since v4:
-Fix ATA_DEFER_LINK_EXCL to use the correct goto label. While refactoring,
I accidentally used the wrong label goto defer_qc instead of goto free_qc, which of course broke PMP CBS in v4.

Good news!

Tested with Linux v7.1-rc3 + v5 patch series.

Both FBS + CBS worked fine, no errors/warnings or visible issues.

Thanks!
- Tommy

author maintainer
#10 lore raw

Hello Tommy,

Quoted 7 lines · Tommy Kelly
Quoted 5 lines · Niklas Cassel

Changes since v4:
-Fix ATA_DEFER_LINK_EXCL to use the correct goto label. While refactoring,
I accidentally used the wrong label goto defer_qc instead of goto free_qc, which of course broke PMP CBS in v4.

Quoted 5 lines

Good news!

Tested with Linux v7.1-rc3 + v5 patch series.

Both FBS + CBS worked fine, no errors/warnings or visible issues.

Thank you very much for all your help with reporting the issue and also helping out with testing. Much appreciated!

If there are no new review comments on V5, this will most likely be queued up in a few days.

Considering how broken the PMP CBS support is in Linus's tree right now, this series should probably go into 7.1, even though it is quite big, but I don't see any other option.

Kind regards,
Niklas

maintainer Reviewed-by on v5 2/4
#4 lore raw

Re: [PATCH v5 2/4] ata: libata-scsi: do not use the deferred QC feature for ATA_DEFER_PORT

In reply to Niklas Cassel
Quoted 9 lines · Niklas Cassel

The deferred QC feature was meant to handle mixed NCQ and non-NCQ commands, i.e. for return value ATA_DEFER_LINK.

ATA_DEFER_PORT is returned by PATA drivers, but also certain SATA drivers like sata_mv and sata_sil24 that uses ap->excl_link to workaround hardware bugs in these HBAs. Regardless of the reason, using the deferred QC feature for ATA_DEFER_PORT is always wrong, and will break the ap->excl_link usage of the SATA drivers that rely on that feature.

Quoted 5 lines

Modify ata_scsi_qc_issue() to only use the deferred QC feature when mixing NCQ and non-NCQ commands, i.e. ATA_DEFER_LINK.

Fixes 0ea84089dbf6 ("ata: libata-scsi: avoid Non-NCQ command starvation")

Looks good.

Signature · 2 lines
Damien Le Moal
Western Digital Research
maintainer Reviewed-by on v5 3/4
#6 lore raw

Re: [PATCH v5 3/4] ata: libata-scsi: do not use the deferred QC feature on PMPs with CBS

In reply to Niklas Cassel
Quoted 33 lines · Niklas Cassel

When using Port Multipliers (PMPs) with Command-Based Switching (CBS), you can only issue commands to one link at a time. For PMPs with CBS, there is already code to handle commands being sent to different links in sata_pmp_qc_defer_cmd_switch() using ap->excl_link. sata_sil24 also makes use of ap->excl_link.

A user on the list reported that commit 0ea84089dbf6 ("ata: libata-scsi:
avoid Non-NCQ command starvation") broke PMPs with CBS. The commit introduced code that stores a deferred qc in ap->deferred_qc, to later be issued via a workqueue. It turns out that this change is incompatible with the existing ap->excl_link handling used by PMPs with CBS.

Thus, modify sata_pmp_qc_defer_cmd_switch() and sil24_qc_defer() to return ATA_DEFER_LINK_EXCL, and make sure that the deferred QC handling via workqueue is not used for this return value.

This way, PMPs with CBS will work once again. Note that the starvation referenced in commit 0ea84089dbf6 ("ata: libata-scsi: avoid Non-NCQ command starvation") can only happen on libsas ports, and libsas does not support Port Multipliers, thus there is no harm of reverting back to the previous way of deferring commands for PMPs with CBS.

Non-libsas ports connected to anything but a PMP with CBS (e.g. a normal drive or a PMP with FBS) will continue using the deferred workqueue, since it does result in lower completion latencies for non-NCQ commands, even though the workqueue is not strictly needed to avoid starvation for non-libsas ports.

If we want to modify the scope of the workqueue issuing to also handle PMPs with CBS, then we should ensure that we can save both NCQ and non-NCQ commands in ap->deferred_qc, while also removing the existing PMP CBS handling using ap->excl_link, such that we don't duplicate features.

Quoted 5 lines

While at it, also add a comment explaining how the ap->excl_link mechanism works.

Fixes 0ea84089dbf6 ("ata: libata-scsi: avoid Non-NCQ command starvation")

Looks good.

Signature · 2 lines
Damien Le Moal
Western Digital Research
maintainer Reviewed-by on v5 4/4
#8 lore raw

Re: [PATCH v5 4/4] ata: libata-scsi: do not needlessly defer commands when using PMP with FBS

In reply to Niklas Cassel
Quoted 1 line · Niklas Cassel

The SATA specification does not allow a non-NCQ command to be issued while

s/SATA/ATA (or ACS)

Quoted 11 lines

an NCQ command is outstanding.

Commit 0ea84089dbf6 ("ata: libata-scsi: avoid Non-NCQ command starvation") introduced a feature where a deferred non-NCQ command gets issued from a workqueue. The design stores a single non-NCQ command per port.

However, when using Port Multipliers (PMPs), specifically PMPs that support FIS-Based Switching (FBS), non-NCQ and NCQ commands can be mixed on the same port, just not for the same link, see e.g. ata_std_qc_defer() which is, and always has operated on a per-link basis.

Quoted 8 lines

Therefore, move the deferred_qc from struct ata_port to struct ata_link. This way, when using a PMP with FBS, we will not needlessly defer commands to all other links, just because one link issued a non-NCQ command while having an NCQ command outstanding. Only commands for that specific link will be deferred.

Fixes 0ea84089dbf6 ("ata: libata-scsi: avoid Non-NCQ command starvation")

Looks good.

Signature · 2 lines
Damien Le Moal
Western Digital Research
author maintainer
#11 lore raw
In reply to Tommy Kelly

Hello Tommy,

Quoted 7 lines · Tommy Kelly
Quoted 5 lines · Niklas Cassel

Changes since v4:
-Fix ATA_DEFER_LINK_EXCL to use the correct goto label. While refactoring,
I accidentally used the wrong label goto defer_qc instead of goto free_qc, which of course broke PMP CBS in v4.

Quoted 5 lines

Good news!

Tested with Linux v7.1-rc3 + v5 patch series.

Both FBS + CBS worked fine, no errors/warnings or visible issues.

I added your Tested-by: on all patches while applying, thank you!

Kind regards,
Niklas

author maintainer Applied
#12 lore raw
In reply to Niklas Cassel
Quoted 8 lines · Niklas Cassel

Tommy Kelly reported a regression with PMP that use CBS:
https://lore.kernel.org/linux-ide/[email protected]/

It turns out that the ap->excl_link logic used for PMP that use CBS is incompatible with the deferred qc issuing via a workqueue. This is fixed in patch 3/4.

[...]

Applied to libata/linux.git (for-7.1-fixes), thanks!

[1/4] ata: libata-scsi: improve readability of ata_scsi_qc_issue()

      https://git.kernel.org/libata/linux/c/360190bd
[2/4] ata: libata-scsi: do not use the deferred QC feature for ATA_DEFER_PORT
      https://git.kernel.org/libata/linux/c/ce454880
[3/4] ata: libata-scsi: do not use the deferred QC feature on PMPs with CBS
      https://git.kernel.org/libata/linux/c/f233124f
[4/4] ata: libata-scsi: do not needlessly defer commands when using PMP with FBS
      https://git.kernel.org/libata/linux/c/759e8756

Kind regards,
Niklas

#13 lore raw
In reply to Niklas Cassel

Hello Niklas,

thank you very much for fixing the issue!

I tested the first three patches manually on my Debian 13 machine with kernel 6.12.90 and can indeed confirm that your patches fixes the issue. The fourth patch had some merge-conflicts and were therefore skipped - I'm unable to test the changes anyway (my HBA only supports CBS).

Machine: HP MicroServer Gen8
HBA: HP Dynamic Smart Array B120i
Port multiplier: StarTech S322M225R (SATA to 2x M.2 SATA using ASM1092R chipset in JBOD mode)

Could you please make sure that the fixes are being backported to all versions that contain the faulty commit 0ea84089dbf6? Looks like that is necessary for v7.0, v6.18, v6.12 and v6.6.

If not, it would be great to get the fixes at least merged into v6.12, as I'm otherwise forced to manually build any new stable kernel that's being pushed by the Debian team.

Thanks again and greetings from Germany!
David

Quoted 42 lines · Niklas Cassel

Hello all,

Tommy Kelly reported a regression with PMP that use CBS:
https://lore.kernel.org/linux-ide/[email protected]/

It turns out that the ap->excl_link logic used for PMP that use CBS is incompatible with the deferred qc issuing via a workqueue. This is fixed in patch 3/4.

While looking at the code, it turns out that the deferred qc issuing via workqueue is misdesigned. It assumed that we can't mix NCQ and non-NCQ commands on the same port. The limitation is that you can not mix NCQ and non-NCQ commands on the same drive. However, with a PMP with FBS, you can issue (mixed NCQ and non-NCQ commands) to the different drives. Thus, move the saved deferred QC from struct ata_port to struct ata_link. This is fixed in patch 4/4.

Changes since v4:
-Fix ATA_DEFER_LINK_EXCL to use the correct goto label. While refactoring,
I accidentally used the wrong label goto defer_qc instead of goto free_qc, which of course broke PMP CBS in v4.

Niklas Cassel (4):
ata: libata-scsi: improve readability of ata_scsi_qc_issue()
ata: libata-scsi: do not use the deferred QC feature for
ATA_DEFER_PORT
ata: libata-scsi: do not use the deferred QC feature on PMPs with CBS
ata: libata-scsi: do not needlessly defer commands when using PMP with
FBS

  drivers/ata/libata-core.c |  9 ++--
  drivers/ata/libata-eh.c   |  8 ++--
  drivers/ata/libata-pmp.c  | 18 +++++++-
  drivers/ata/libata-scsi.c | 89 +++++++++++++++++++++++----------------
  drivers/ata/sata_sil24.c  |  6 ++-
  include/linux/libata.h    |  7 +--
  6 files changed, 87 insertions(+), 50 deletions(-)

base-commit: 0926c1b731b73e7296e4ad4f7d055918e16ae6ab

author maintainer Tested-by Tommy Kelly Reviewed-by Damien Le Moal
#14 lore raw
Quoted 18 lines · David Dreschner

Hello Niklas,

thank you very much for fixing the issue!

I tested the first three patches manually on my Debian 13 machine with kernel 6.12.90 and can indeed confirm that your patches fixes the issue. The fourth patch had some merge-conflicts and were therefore skipped - I'm unable to test the changes anyway (my HBA only supports CBS).

Machine: HP MicroServer Gen8
HBA: HP Dynamic Smart Array B120i
Port multiplier: StarTech S322M225R (SATA to 2x M.2 SATA using ASM1092R chipset in JBOD mode)

Could you please make sure that the fixes are being backported to all versions that contain the faulty commit 0ea84089dbf6? Looks like that is necessary for v7.0, v6.18, v6.12 and v6.6.

Quoted 6 lines

If not, it would be great to get the fixes at least merged into v6.12, as I'm otherwise forced to manually build any new stable kernel that's being pushed by the Debian team.

Thanks again and greetings from Germany!
David

Dear stable maintainers,

Sorry for the extra work put on you.

Could your please help us backport:

$ git log --reverse --oneline v7.1-rc4..v7.1-rc5 drivers/ata/
360190bd965f ata: libata-scsi: improve readability of ata_scsi_qc_issue()
ce4548807d2e ata: libata-scsi: do not use the deferred QC feature for ATA_DEFER_PORT
f233124fb36c ata: libata-scsi: do not use the deferred QC feature on PMPs with CBS
759e8756da00 ata: libata-scsi: do not needlessly defer commands when using PMP with FBS

The first three patches apply cleanly to at least v6.12.

The final commit does not apply cleanly, attaching a backported version.

Again, sorry for the trouble.
Hopefully our AI overlords / Sashiko will manage to find some of these problems that was missed during the review of the faulty commit.

Kind regards,
Niklas

From bd5d8ab6f84dc19918704dad6c669bd29c8df30c Mon Sep 17 00:00:00 2001
From: Niklas Cassel <[email protected]>
Date: Thu, 14 May 2026 09:39:02 +0200
Subject: [PATCH] ata: libata-scsi: do not needlessly defer commands when using
PMP with FBS

[ Upstream commit 759e8756da00aa115d504a18155b1d1ee1cc12e8 ]

The ACS specification does not allow a non-NCQ command to be issued while an NCQ command is outstanding.

Commit 0ea84089dbf6 ("ata: libata-scsi: avoid Non-NCQ command starvation") introduced a feature where a deferred non-NCQ command gets issued from a workqueue. The design stores a single non-NCQ command per port.

However, when using Port Multipliers (PMPs), specifically PMPs that support FIS-Based Switching (FBS), non-NCQ and NCQ commands can be mixed on the same port, just not for the same link, see e.g. ata_std_qc_defer() which is, and always has operated on a per-link basis.

Therefore, move the deferred_qc from struct ata_port to struct ata_link. This way, when using a PMP with FBS, we will not needlessly defer commands to all other links, just because one link issued a non-NCQ command while having an NCQ command outstanding. Only commands for that specific link will be deferred. This is in line with how PMPs with FBS worked before commit 0ea84089dbf6 ("ata: libata-scsi: avoid Non-NCQ command starvation").

Fixes 0ea84089dbf6 ("ata: libata-scsi: avoid Non-NCQ command starvation")
---
 drivers/ata/libata-core.c |  9 ++++---
 drivers/ata/libata-eh.c   |  8 +++---
 drivers/ata/libata-pmp.c  |  5 +++-
 drivers/ata/libata-scsi.c | 54 ++++++++++++++++++++++-----------------
 include/linux/libata.h    |  6 ++---
 5 files changed, 47 insertions(+), 35 deletions(-)
drivers/ata/libata-core.c +6 -3
index 3e02402329a6..434934927928 100644
@@ -5444,6 +5444,7 @@ void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
 	link->pmp = pmp;
 	link->active_tag = ATA_TAG_POISON;
 	link->hw_sata_spd_limit = UINT_MAX;
+	INIT_WORK(&link->deferred_qc_work, ata_scsi_deferred_qc_work);
 
 	/* can't use iterator, ap isn't initialized yet */
 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
@@ -5526,7 +5527,6 @@ struct ata_port *ata_port_alloc(struct ata_host *host)
 	mutex_init(&ap->scsi_scan_mutex);
 	INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
 	INIT_DELAYED_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
-	INIT_WORK(&ap->deferred_qc_work, ata_scsi_deferred_qc_work);
 	INIT_LIST_HEAD(&ap->eh_done_q);
 	init_waitqueue_head(&ap->eh_wait_q);
 	init_completion(&ap->park_req_pending);
@@ -6149,12 +6149,15 @@ static void ata_port_detach(struct ata_port *ap)
 
 	/* It better be dead now and not have any remaining deferred qc. */
 	WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
-	WARN_ON(ap->deferred_qc);
 
-	cancel_work_sync(&ap->deferred_qc_work);
 	cancel_delayed_work_sync(&ap->hotplug_task);
 	cancel_delayed_work_sync(&ap->scsi_rescan_task);
 
+	ata_for_each_link(link, ap, PMP_FIRST) {
+		WARN_ON(link->deferred_qc);
+		cancel_work_sync(&link->deferred_qc_work);
+	}
+
 	/* Delete port multiplier link transport devices */
 	if (ap->pmp_link) {
 		int i;
drivers/ata/libata-eh.c +4 -4
index 59788a34871a..e9d2d84034fd 100644
@@ -645,11 +645,11 @@ void ata_scsi_cmd_error_handler(struct Scsi_Host *host, struct ata_port *ap,
 			if (qc->scsicmd != scmd)
 				continue;
 			if ((qc->flags & ATA_QCFLAG_ACTIVE) ||
-			    qc == ap->deferred_qc)
+			    qc == qc->dev->link->deferred_qc)
 				break;
 		}
 
-		if (i < ATA_MAX_QUEUE && qc == ap->deferred_qc) {
+		if (i < ATA_MAX_QUEUE && qc == qc->dev->link->deferred_qc) {
 			/*
 			 * This is a deferred command that timed out while
 			 * waiting for the command queue to drain. Since the qc
@@ -660,8 +660,8 @@ void ata_scsi_cmd_error_handler(struct Scsi_Host *host, struct ata_port *ap,
 			 * deferred qc work from issuing this qc.
 			 */
 			WARN_ON_ONCE(qc->flags & ATA_QCFLAG_ACTIVE);
-			ap->deferred_qc = NULL;
-			cancel_work(&ap->deferred_qc_work);
+			qc->dev->link->deferred_qc = NULL;
+			cancel_work(&qc->dev->link->deferred_qc_work);
 			set_host_byte(scmd, DID_TIME_OUT);
 			scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
 		} else if (i < ATA_MAX_QUEUE) {
drivers/ata/libata-pmp.c +4 -1
index a489f1b33a08..e954fc07ddbb 100644
@@ -582,8 +582,11 @@ static void sata_pmp_detach(struct ata_device *dev)
 	if (ap->ops->pmp_detach)
 		ap->ops->pmp_detach(ap);
 
-	ata_for_each_link(tlink, ap, EDGE)
+	ata_for_each_link(tlink, ap, EDGE) {
+		WARN_ON(tlink->deferred_qc);
+		cancel_work_sync(&tlink->deferred_qc_work);
 		ata_eh_detach_dev(tlink->device);
+	}
 
 	spin_lock_irqsave(ap->lock, flags);
 	ap->nr_pmp_links = 0;
drivers/ata/libata-scsi.c +30 -24

121 lines not on this page · raw message

include/linux/libata.h +3 -3
index eee76f3eea25..a78227fb66a8 100644
@@ -853,6 +853,9 @@ struct ata_link {
 	unsigned int		sata_spd;	/* current SATA PHY speed */
 	enum ata_lpm_policy	lpm_policy;
 
+	struct work_struct	deferred_qc_work;
+	struct ata_queued_cmd	*deferred_qc;
+
 	/* record runtime error info, protected by host_set lock */
 	struct ata_eh_info	eh_info;
 	/* EH context */
@@ -898,9 +901,6 @@ struct ata_port {
 	u64			qc_active;
 	int			nr_active_links; /* #links with active qcs */
 
-	struct work_struct	deferred_qc_work;
-	struct ata_queued_cmd	*deferred_qc;
-
 	struct ata_link		link;		/* host default link */
 	struct ata_link		*slave_link;	/* see ata_slave_link_init() */
 
Signature · 1 line
2.54.0
#15 lore raw
Quoted 39 lines · Niklas Cassel
Quoted 18 lines · David Dreschner

Hello Niklas,

thank you very much for fixing the issue!

I tested the first three patches manually on my Debian 13 machine with kernel 6.12.90 and can indeed confirm that your patches fixes the issue. The fourth patch had some merge-conflicts and were therefore skipped - I'm unable to test the changes anyway (my HBA only supports CBS).

Machine: HP MicroServer Gen8
HBA: HP Dynamic Smart Array B120i
Port multiplier: StarTech S322M225R (SATA to 2x M.2 SATA using ASM1092R chipset in JBOD mode)

Could you please make sure that the fixes are being backported to all versions that contain the faulty commit 0ea84089dbf6? Looks like that is necessary for v7.0, v6.18, v6.12 and v6.6.

Quoted 6 lines

If not, it would be great to get the fixes at least merged into v6.12, as I'm otherwise forced to manually build any new stable kernel that's being pushed by the Debian team.

Thanks again and greetings from Germany!
David

Dear stable maintainers,

Sorry for the extra work put on you.

Could your please help us backport:

$ git log --reverse --oneline v7.1-rc4..v7.1-rc5 drivers/ata/
360190bd965f ata: libata-scsi: improve readability of ata_scsi_qc_issue()
ce4548807d2e ata: libata-scsi: do not use the deferred QC feature for ATA_DEFER_PORT
f233124fb36c ata: libata-scsi: do not use the deferred QC feature on PMPs with CBS
759e8756da00 ata: libata-scsi: do not needlessly defer commands when using PMP with FBS
Quoted 7 lines

The first three patches apply cleanly to at least v6.12.

The final commit does not apply cleanly, attaching a backported version.

Again, sorry for the trouble.
Hopefully our AI overlords / Sashiko will manage to find some of these problems that was missed during the review of the faulty commit.

Now queued up, thanks.

greg k-h