From 4b89ad8e5e1233bfe642e0185e34ad5e1cefb19e Mon Sep 17 00:00:00 2001 From: Niklas Cassel Date: Mon, 26 Sep 2022 20:53:07 +0000 Subject: ata: libata: only set sense valid flag if sense data is valid While this shouldn't be needed if all devices that claim that they support NCQ autosense (ata_id_has_ncq_autosense()) and/or the sense data reporting feature (ata_id_has_sense_reporting()), actually supported those features. However, there might be some old ATA devices that either have these bits set, even when they don't support those features, or they simply return malformed data when using those features. These devices should be quirked, but in order to try to minimize the impact for the users of these such devices, it was suggested by Damien Le Moal that it might be a good idea to sanity check the sense data received from the device. If the sense data looks bogus, then the sense data is never added to the scsi_cmnd command. Introduce a new function, ata_scsi_sense_is_valid(), and use it in all places where sense data is received from the device. Suggested-by: Damien Le Moal Signed-off-by: Niklas Cassel Signed-off-by: Damien Le Moal --- drivers/ata/libata-scsi.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'drivers/ata/libata-scsi.c') diff --git a/drivers/ata/libata-scsi.c b/drivers/ata/libata-scsi.c index e2ebb0b065e2..1287386fd029 100644 --- a/drivers/ata/libata-scsi.c +++ b/drivers/ata/libata-scsi.c @@ -188,6 +188,22 @@ DEVICE_ATTR(unload_heads, S_IRUGO | S_IWUSR, ata_scsi_park_show, ata_scsi_park_store); EXPORT_SYMBOL_GPL(dev_attr_unload_heads); +bool ata_scsi_sense_is_valid(u8 sk, u8 asc, u8 ascq) +{ + /* + * If sk == NO_SENSE, and asc + ascq == NO ADDITIONAL SENSE INFORMATION, + * then there is no sense data to add. + */ + if (sk == 0 && asc == 0 && ascq == 0) + return false; + + /* If sk > COMPLETED, sense data is bogus. */ + if (sk > COMPLETED) + return false; + + return true; +} + void ata_scsi_set_sense(struct ata_device *dev, struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq) { -- cgit v1.2.3