ata_scsi_remove_element_and_truncate_xlat() copies the REQUESTED CAPACITY field, req_capacity, to REQUESTED MAX LBA for every block count except 1. However, SAT-6 specifies that if REQUESTED CAPACITY is greater than one, the REQUESTED MAX LBA shall be set to REQUESTED CAPACITY minus one, since the max LBA = count - 1 (see SAT-6 rev2, 9.16.1, Table 74). For example, if a capacity C > 1 is requested, the capacity ends up one block larger if the drive can provide C + 1. If the drive can provide C but not C + 1, a request that should be valid is instead rejected with INVALID FIELD IN CDB (see ACS-5 rev10, 7.33.3.3). Modify ata_scsi_remove_element_and_truncate_xlat() to decrement req_capacity, except when equal to 1 or 0 (rejected if 1, let the device choose if 0). Tested by issuing sg_rem_rest_elem on a Seagate drive ST32000NM001F, firmware db02 and checking the taskfile. Fixes: db496721cb0d ("ata: libata-scsi: add support for the REMOVE ELEMENT AND TRUNCATE command") Cc: stable@vger.kernel.org Signed-off-by: Nick Spooner --- Tested on libata/for-7.4-fixes by capturing the trace of what libata sends to the drive. sg_rem_rest_elem --remove --element=0x30 --capacity= /dev/sda Unpatched, C=1000000: ata_qc_issue: ata_port=2 ata_dev=0 tag=12 proto=ATA_PROT_NODATA cmd=0x7c tf=(7c/00:30:40:42:0f/00:00:00:00:00/40) Patched, C=1000000, 0, 2: ata_qc_issue: ata_port=2 ata_dev=0 tag=0 proto=ATA_PROT_NODATA cmd=0x7c tf=(7c/00:30:3f:42:0f/00:00:00:00:00/40) ... tf=(7c/00:30:00:00:00/00:00:00:00:00/40) ... tf=(7c/00:30:01:00:00/00:00:00:00:00/40) drivers/ata/libata-scsi.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/ata/libata-scsi.c b/drivers/ata/libata-scsi.c index 87a57495d2e4..20ea1c997d56 100644 --- a/drivers/ata/libata-scsi.c +++ b/drivers/ata/libata-scsi.c @@ -4991,6 +4991,9 @@ ata_scsi_remove_element_and_truncate_xlat(struct ata_queued_cmd *qc) return 1; } + if (req_capacity > 1) + req_capacity--; + id = get_unaligned_be32(&cdb[10]); tf->protocol = ATA_PROT_NODATA; -- 2.56.0