Skip to contentSkip to conversation

Successful NCQ commands sense_valid cleanups/fixes

PATCH In review Niklas Cassel 2 participants 6 messages Apr 16, 2025 – Apr 21, 2025 libata

lore
Share & tools
lore

Niklas Cassel

author maintainer
#0 lore raw

v3 cover Successful NCQ commands sense_valid cleanups/fixes

Hello all,

Here comes some minor cleanups/fixes related to the sense_valid field in the Successful NCQ commands log.

Kind regards,
Niklas

Changes since v2:
-Picked up tags.
-Changed "1ULL << tag" to "1 << tag" in patch 2/3 (Damien). -Improved subject in patch 3/3.

Niklas Cassel (3):
ata: libata-sata: Save all fields from sense data descriptor
ata: libata-sata: Simplify sense_valid fetching
ata: libata-sata: Use BIT() macro to convert tag to bit field

 drivers/ata/libata-sata.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)
Signature · 1 line
2.49.0
author maintainer
#1 lore raw

PATCH v3 1/3 ata: libata-sata: Save all fields from sense data descriptor

When filling the taskfile result for a successful NCQ command, we use the SDB FIS from the FIS Receive Area, see e.g. ahci_qc_ncq_fill_rtf().

However, the SDB FIS only has fields STATUS and ERROR.

For a successful NCQ command that has sense data, we will have a successful sense data descriptor, in the Sense Data for Successful NCQ Commands log.

Since we have access to additional taskfile result fields, fill in these additional fields in qc->result_tf.

This matches how for failing/aborted NCQ commands, we will use e.g. ahci_qc_fill_rtf() to fill in some fields, but then for the command that actually caused the NCQ error, we will use ata_eh_read_log_10h(), which provides additional fields, saving additional fields/overriding the qc->result_tf that was fetched using ahci_qc_fill_rtf().

Fixes 18bd7718b5c4 ("scsi: ata: libata: Handle completion of CDL commands using policy 0xD")
---
 drivers/ata/libata-sata.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
drivers/ata/libata-sata.c +15 -0
index ba300cc0a3a3..2e4463d3a356 100644
@@ -1510,6 +1510,8 @@ int ata_eh_get_ncq_success_sense(struct ata_link *link)
 	unsigned int err_mask, tag;
 	u8 *sense, sk = 0, asc = 0, ascq = 0;
 	u64 sense_valid, val;
+	u16 extended_sense;
+	bool aux_icc_valid;
 	int ret = 0;
 
 	err_mask = ata_read_log_page(dev, ATA_LOG_SENSE_NCQ, 0, buf, 2);
@@ -1529,6 +1531,8 @@ int ata_eh_get_ncq_success_sense(struct ata_link *link)
 
 	sense_valid = (u64)buf[8] | ((u64)buf[9] << 8) |
 		((u64)buf[10] << 16) | ((u64)buf[11] << 24);
+	extended_sense = get_unaligned_le16(&buf[14]);
+	aux_icc_valid = extended_sense & BIT(15);
 
 	ata_qc_for_each_raw(ap, qc, tag) {
 		if (!(qc->flags & ATA_QCFLAG_EH) ||
@@ -1556,6 +1560,17 @@ int ata_eh_get_ncq_success_sense(struct ata_link *link)
 			continue;
 		}
 
+		qc->result_tf.nsect = sense[6];
+		qc->result_tf.hob_nsect = sense[7];
+		qc->result_tf.lbal = sense[8];
+		qc->result_tf.lbam = sense[9];
+		qc->result_tf.lbah = sense[10];
+		qc->result_tf.hob_lbal = sense[11];
+		qc->result_tf.hob_lbam = sense[12];
+		qc->result_tf.hob_lbah = sense[13];
+		if (aux_icc_valid)
+			qc->result_tf.auxiliary = get_unaligned_le32(&sense[16]);
+
 		/* Set sense without also setting scsicmd->result */
 		scsi_build_sense_buffer(dev->flags & ATA_DFLAG_D_SENSE,
 					qc->scsicmd->sense_buffer, sk,
Signature · 1 line
2.49.0
author maintainer
#2 lore raw

PATCH v3 2/3 ata: libata-sata: Simplify sense_valid fetching

In reply to Niklas Cassel

While the SENSE DATA VALID field in the ACS-6 specification is 47 bits, we are currently only fetching 32 bits, because these are the only bits that we care about (these bits represent the tags (which can be 0-31)).

Thus, replace the existing logic with a simple get_unaligned_le32().

While at it, change the type of sense_valid to u32.

---
 drivers/ata/libata-sata.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/ata/libata-sata.c +4 -4
index 2e4463d3a356..89d3b784706b 100644
@@ -1509,9 +1509,10 @@ int ata_eh_get_ncq_success_sense(struct ata_link *link)
 	struct ata_queued_cmd *qc;
 	unsigned int err_mask, tag;
 	u8 *sense, sk = 0, asc = 0, ascq = 0;
-	u64 sense_valid, val;
 	u16 extended_sense;
 	bool aux_icc_valid;
+	u32 sense_valid;
+	u64 val;
 	int ret = 0;
 
 	err_mask = ata_read_log_page(dev, ATA_LOG_SENSE_NCQ, 0, buf, 2);
@@ -1529,8 +1530,7 @@ int ata_eh_get_ncq_success_sense(struct ata_link *link)
 		return -EIO;
 	}
 
-	sense_valid = (u64)buf[8] | ((u64)buf[9] << 8) |
-		((u64)buf[10] << 16) | ((u64)buf[11] << 24);
+	sense_valid = get_unaligned_le32(&buf[8]);
 	extended_sense = get_unaligned_le16(&buf[14]);
 	aux_icc_valid = extended_sense & BIT(15);
 
@@ -1545,7 +1545,7 @@ int ata_eh_get_ncq_success_sense(struct ata_link *link)
 		 * If the command does not have any sense data, clear ATA_SENSE.
 		 * Keep ATA_QCFLAG_EH_SUCCESS_CMD so that command is finished.
 		 */
-		if (!(sense_valid & (1ULL << tag))) {
+		if (!(sense_valid & (1 << tag))) {
 			qc->result_tf.status &= ~ATA_SENSE;
 			continue;
 		}
Signature · 1 line
2.49.0
author maintainer
#3 lore raw

PATCH v3 3/3 ata: libata-sata: Use BIT() macro to convert tag to bit field

In reply to Niklas Cassel

The BIT() macro is commonly used in the kernel.
Make use of it when converting a tag, fetched from the Successful NCQ Commands log or the NCQ Command Error log, to a bit field. This makes the code easier to read.

---
 drivers/ata/libata-sata.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/ata/libata-sata.c +2 -2
index 89d3b784706b..4e3034af285e 100644
@@ -1545,7 +1545,7 @@ int ata_eh_get_ncq_success_sense(struct ata_link *link)
 		 * If the command does not have any sense data, clear ATA_SENSE.
 		 * Keep ATA_QCFLAG_EH_SUCCESS_CMD so that command is finished.
 		 */
-		if (!(sense_valid & (1 << tag))) {
+		if (!(sense_valid & BIT(tag))) {
 			qc->result_tf.status &= ~ATA_SENSE;
 			continue;
 		}
@@ -1634,7 +1634,7 @@ void ata_eh_analyze_ncq_error(struct ata_link *link)
 		return;
 	}
 
-	if (!(link->sactive & (1 << tag))) {
+	if (!(link->sactive & BIT(tag))) {
 		ata_link_err(link, "log page 10h reported inactive tag %d\n",
 			     tag);
 		return;
Signature · 1 line
2.49.0
maintainer
#4 lore raw
In reply to Niklas Cassel
Quoted 16 lines · Niklas Cassel

Hello all,

Here comes some minor cleanups/fixes related to the sense_valid field in the Successful NCQ commands log.

Kind regards,
Niklas

Changes since v2:

(quoted diff) +0 -3
-Picked up tags.
-Changed "1ULL << tag" to "1 << tag" in patch 2/3 (Damien).
-Improved subject in patch 3/3.
Quoted 4 lines

Niklas Cassel (3):
ata: libata-sata: Save all fields from sense data descriptor
ata: libata-sata: Simplify sense_valid fetching
ata: libata-sata: Use BIT() macro to convert tag to bit field

Looks good. I will rebase for-6.16 on rc3 next Monday and apply 2/3 and 3/3. Thanks !

Signature · 2 lines
Damien Le Moal
Western Digital Research
maintainer
#5 lore raw
In reply to Niklas Cassel
Quoted 16 lines · Niklas Cassel

Hello all,

Here comes some minor cleanups/fixes related to the sense_valid field in the Successful NCQ commands log.

Kind regards,
Niklas

Changes since v2:

(quoted diff) +0 -3
-Picked up tags.
-Changed "1ULL << tag" to "1 << tag" in patch 2/3 (Damien).
-Improved subject in patch 3/3.
Quoted 4 lines

Niklas Cassel (3):
ata: libata-sata: Save all fields from sense data descriptor
ata: libata-sata: Simplify sense_valid fetching
ata: libata-sata: Use BIT() macro to convert tag to bit field

Applied 2/3 and 3/3 to for-6.16. Thanks !

Signature · 2 lines
Damien Le Moal
Western Digital Research