Re: [PATCH v2 3/5] ata: libata-scsi: route non-zero LUN commands for multi-LUN ATAPI

From: Phil Pemberton

Date: Thu Apr 23 2026 - 11:52:00 EST


On 23/04/2026 12:08, Hannes Reinecke wrote:
On 4/20/26 14:23, Phil Pemberton wrote:
Two changes are required to route commands to ATAPI LUNs other than 0:

1. __ata_scsi_find_dev():  The existing code rejects any scsi_device
    with a non-zero LUN, returning NULL and dropping the command on
    the floor.  Relax both the PMP and non-PMP branches to allow
    non-zero LUNs through when the underlying ata_device is ATAPI
    class, since ATAPI devices can legitimately expose multiple LUNs.

2. atapi_xlat():  Older ATAPI devices (SCSI-2 era) expect the LUN in
    CDB byte 1 bits 7:5 rather than relying on transport-level LUN
    addressing.  Encode scmd->device->lun into those bits, preserving
    the existing command-specific bits in 4:0.  This is required by
    both the Panasonic PD/CD combos and Nakamichi CD changers.

Signed-off-by: Phil Pemberton <philpem@xxxxxxxxxxxxx>
---
  drivers/ata/libata-scsi.c | 17 +++++++++++++++--
  1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/drivers/ata/libata-scsi.c b/drivers/ata/libata-scsi.c
index 317883bac25f..4e88ae7d94c3 100644
--- a/drivers/ata/libata-scsi.c
+++ b/drivers/ata/libata-scsi.c
@@ -2951,6 +2951,11 @@ static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
      memset(qc->cdb, 0, dev->cdb_len);
      memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
+    /* SCSI-2 CDB LUN encoding: bits 7:5 of byte 1 */
+    if (scmd->device->lun < 8)
+        qc->cdb[1] = (qc->cdb[1] & 0x1f) |
+                  ((u8)scmd->device->lun << 5);
+

Hmm. And what happens when scmd->device->lun is _greater_ than 7?
We surely should abort that command, shouldn't we?

I was about to say "you can't have LUNs greater than seven" until I saw it's been extended in later versions of the standard.

What's the preferred way to abort the command in this context?

--
Phil.
philpem@xxxxxxxxxxxxx
https://www.philpem.me.uk/