{"id":2221588,"url":"http://patchwork.ozlabs.org/api/1.1/patches/2221588/?format=json","web_url":"http://patchwork.ozlabs.org/project/linux-ide/patch/20260409210559.155864-3-philpem@philpem.me.uk/","project":{"id":13,"url":"http://patchwork.ozlabs.org/api/1.1/projects/13/?format=json","name":"Linux IDE development","link_name":"linux-ide","list_id":"linux-ide.vger.kernel.org","list_email":"linux-ide@vger.kernel.org","web_url":null,"scm_url":null,"webscm_url":null},"msgid":"<20260409210559.155864-3-philpem@philpem.me.uk>","date":"2026-04-09T21:05:58","name":"[2/3] ata: libata-scsi: enable multi-LUN support for ATAPI devices","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"f54c883bca9dff90c98c3a70d71c0a4ed739d890","submitter":{"id":93108,"url":"http://patchwork.ozlabs.org/api/1.1/people/93108/?format=json","name":"Phil Pemberton","email":"philpem@philpem.me.uk"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/linux-ide/patch/20260409210559.155864-3-philpem@philpem.me.uk/mbox/","series":[{"id":499361,"url":"http://patchwork.ozlabs.org/api/1.1/series/499361/?format=json","web_url":"http://patchwork.ozlabs.org/project/linux-ide/list/?series=499361","date":"2026-04-09T21:05:58","name":"ata: libata-scsi: add multi-LUN support for ATAPI devices","version":1,"mbox":"http://patchwork.ozlabs.org/series/499361/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2221588/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2221588/checks/","tags":{},"headers":{"Return-Path":"\n <linux-ide+bounces-5466-incoming=patchwork.ozlabs.org@vger.kernel.org>","X-Original-To":["incoming@patchwork.ozlabs.org","linux-ide@vger.kernel.org"],"Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (1024-bit key;\n unprotected) header.d=philpem.me.uk header.i=@philpem.me.uk\n header.a=rsa-sha256 header.s=mail header.b=pWkacvkm;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org\n (client-ip=2600:3c0a:e001:db::12fc:5321; helo=sea.lore.kernel.org;\n envelope-from=linux-ide+bounces-5466-incoming=patchwork.ozlabs.org@vger.kernel.org;\n receiver=patchwork.ozlabs.org)","smtp.subspace.kernel.org;\n\tdkim=pass (1024-bit key) header.d=philpem.me.uk header.i=@philpem.me.uk\n header.b=\"pWkacvkm\"","smtp.subspace.kernel.org;\n arc=none smtp.client-ip=178.62.38.78","smtp.subspace.kernel.org;\n dmarc=pass (p=reject dis=none) header.from=philpem.me.uk","smtp.subspace.kernel.org;\n spf=pass smtp.mailfrom=philpem.me.uk"],"Received":["from sea.lore.kernel.org (sea.lore.kernel.org\n [IPv6:2600:3c0a:e001:db::12fc:5321])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4fsCCY040Pz1yGb\n\tfor <incoming@patchwork.ozlabs.org>; Fri, 10 Apr 2026 07:06:16 +1000 (AEST)","from smtp.subspace.kernel.org (conduit.subspace.kernel.org\n [100.90.174.1])\n\tby sea.lore.kernel.org (Postfix) with ESMTP id 0F70E300EA96\n\tfor <incoming@patchwork.ozlabs.org>; Thu,  9 Apr 2026 21:06:13 +0000 (UTC)","from localhost.localdomain (localhost.localdomain [127.0.0.1])\n\tby smtp.subspace.kernel.org (Postfix) with ESMTP id EA75C36EA88;\n\tThu,  9 Apr 2026 21:06:11 +0000 (UTC)","from nick.sneptech.io (nick.sneptech.io [178.62.38.78])\n\t(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby smtp.subspace.kernel.org (Postfix) with ESMTPS id 408BA29992A;\n\tThu,  9 Apr 2026 21:06:10 +0000 (UTC)","from wolf.philpem.me.uk (81-187-163-148.ip4.reverse-dns.uk\n [81.187.163.148])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest\n SHA256)\n\t(No client certificate requested)\n\t(Authenticated sender: mailrelay_wolf@philpem.me.uk)\n\tby nick.sneptech.io (Postfix) with ESMTPSA id B5EE7BE5E2;\n\tThu,  9 Apr 2026 21:06:08 +0000 (UTC)","from cheetah.homenet.philpem.me.uk (cheetah.homenet.philpem.me.uk\n [10.0.0.32])\n\tby wolf.philpem.me.uk (Postfix) with ESMTPSA id 55FCB5FC51;\n\tThu,  9 Apr 2026 22:06:08 +0100 (BST)"],"ARC-Seal":"i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;\n\tt=1775768771; cv=none;\n b=iXJW30kti5tqWm7RIr/42+C1XX7YYXI9T7fz6toME0Q9164WLwYkSKoBOd31OXFbVTASZtIo9h4SyJ8Pq8PrBjM0bpaKz7J/SfmLFeI1RG4j3r7owClN5qE74JoSSryQ4XtmziXfMPsFtNQfN3+QpcM496MvIVQ2MlRWy2dQ7yA=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=subspace.kernel.org;\n\ts=arc-20240116; t=1775768771; c=relaxed/simple;\n\tbh=rtFbVp8/UJ4Lu7BUwpQ9HQdvekknIbl/+cNf7dMuamo=;\n\th=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:\n\t MIME-Version;\n b=kRgIp2AvulpxWOGJTXNvqBPwLcAZ1jkfamE3W//6o5RdlZbocTT9lSeK9t55ofvDUx9fUFYAXZTWNWLGsCRRe7qvu2eM2tYqjra2j/z9cGYgHR6g29LnFx28cNQc6PdUHyUIhoDPeQ6t6vAJ1c3xOpx/Kth86e/f/0u4kmX/m1I=","ARC-Authentication-Results":"i=1; smtp.subspace.kernel.org;\n dmarc=pass (p=reject dis=none) header.from=philpem.me.uk;\n spf=pass smtp.mailfrom=philpem.me.uk;\n dkim=pass (1024-bit key) header.d=philpem.me.uk header.i=@philpem.me.uk\n header.b=pWkacvkm; arc=none smtp.client-ip=178.62.38.78","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=philpem.me.uk;\n\ts=mail; t=1775768768;\n\tbh=rtFbVp8/UJ4Lu7BUwpQ9HQdvekknIbl/+cNf7dMuamo=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=pWkacvkm9gHK+bBO0ON7i9XVI6LUSeIwdC5mfyeSw5a0S/fGF1zgLySXKquO4FGl9\n\t 8QvLtL+XOUtBhBdA2jZoDaA87kXI0lwENzYXtG9aNhltwyWkUKIZKWvNKqXUscurh6\n\t qZMOdvEo39HKfb0glitRBk/CL+pVlArcMxIz+P+0=","From":"Phil Pemberton <philpem@philpem.me.uk>","To":"dlemoal@kernel.org,\n\tcassel@kernel.org,\n\tJames.Bottomley@HansenPartnership.com,\n\tmartin.petersen@oracle.com","Cc":"linux-ide@vger.kernel.org,\n\tlinux-scsi@vger.kernel.org,\n\tlinux-kernel@vger.kernel.org,\n\tPhil Pemberton <philpem@philpem.me.uk>","Subject":"[PATCH 2/3] ata: libata-scsi: enable multi-LUN support for ATAPI\n devices","Date":"Thu,  9 Apr 2026 22:05:58 +0100","Message-ID":"<20260409210559.155864-3-philpem@philpem.me.uk>","X-Mailer":"git-send-email 2.43.0","In-Reply-To":"<20260409210559.155864-1-philpem@philpem.me.uk>","References":"<20260409210559.155864-1-philpem@philpem.me.uk>","Precedence":"bulk","X-Mailing-List":"linux-ide@vger.kernel.org","List-Id":"<linux-ide.vger.kernel.org>","List-Subscribe":"<mailto:linux-ide+subscribe@vger.kernel.org>","List-Unsubscribe":"<mailto:linux-ide+unsubscribe@vger.kernel.org>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit"},"content":"libata has never supported multi-LUN ATAPI devices like the Panasonic\nand NEC PD/CD combo drives due to three limitations:\n\n  - shost->max_lun is hardcoded to 1 in ata_scsi_add_hosts(), which\n    stops the SCSI layer from probing any LUN other than 0.\n\n  - __ata_scsi_find_dev() rejects all commands where scsidev->lun != 0,\n    returning NULL which causes DID_BAD_TARGET.\n\n  - The SCSI-2 CDB LUN field (byte 1, bits 7:5) is never set. Older\n    multi-LUN ATAPI devices rely on this field to route commands to the\n    correct LUN, as transport-layer LUN addressing (per SPC-3+) is not\n    available over the ATA PACKET interface.\n\nTo fix all three, this change:\n\n  - Raises max_lun from 1 to 8 (matching the SCSI host default).\n    Sequential LUN scanning stops at the first non-responding LUN, so\n    single-LUN devices are unaffected.\n\n  - In __ata_scsi_find_dev(), allow non-zero LUNs for ATAPI devices by\n    routing them to the same ata_device as LUN 0.\n\n  - In atapi_xlat(), encode the target LUN into CDB byte 1 bits 7:5\n    before passing the command packet to the device.\n\nThese changes are prerequisites for probing additional LUNs during\nhost scanning, which is done in a subsequent patch.\n\nAdditionally, fix two related issues exposed by multi-LUN scanning:\n\n  - ata_scsi_dev_config() previously assigned dev->sdev = sdev for every\n    LUN configured.  With multiple LUNs sharing one ata_device, this\n    caused dev->sdev to be overwritten by each non-LUN-0 sdev.  Restrict\n    the assignment to LUN 0 so that dev->sdev always tracks the\n    canonical scsi_device for the underlying ATA device.\n\n  - ata_scsi_sdev_destroy() detached the entire ATA device whenever\n    dev->sdev was non-NULL.  When a spurious multi-LUN scan result was\n    removed, this incorrectly tore down the underlying device.  Detach\n    only when the canonical (LUN 0) sdev is being destroyed.\n\nAssisted-by: Claude:claude-opus-4-6\nSigned-off-by: Phil Pemberton <philpem@philpem.me.uk>\n---\n drivers/ata/libata-scsi.c | 38 ++++++++++++++++++++++++++++++++++----\n 1 file changed, 34 insertions(+), 4 deletions(-)","diff":"diff --git a/drivers/ata/libata-scsi.c b/drivers/ata/libata-scsi.c\nindex 3b65df914ebb..dc6829e60fb3 100644\n--- a/drivers/ata/libata-scsi.c\n+++ b/drivers/ata/libata-scsi.c\n@@ -25,6 +25,7 @@\n #include <scsi/scsi_eh.h>\n #include <scsi/scsi_device.h>\n #include <scsi/scsi_tcq.h>\n+#include <scsi/scsi_devinfo.h>\n #include <scsi/scsi_transport.h>\n #include <linux/libata.h>\n #include <linux/hdreg.h>\n@@ -1131,7 +1132,14 @@ int ata_scsi_dev_config(struct scsi_device *sdev, struct queue_limits *lim,\n \tif (dev->flags & ATA_DFLAG_TRUSTED)\n \t\tsdev->security_supported = 1;\n \n-\tdev->sdev = sdev;\n+\t/*\n+\t * Only LUN 0 is treated as the canonical scsi_device for the ATA\n+\t * device.  Multi-LUN ATAPI devices share a single ata_device, so\n+\t * dev->sdev must continue to track LUN 0 even when additional LUNs\n+\t * are added or removed.\n+\t */\n+\tif (sdev->lun == 0)\n+\t\tdev->sdev = sdev;\n \treturn 0;\n }\n \n@@ -1220,7 +1228,12 @@ void ata_scsi_sdev_destroy(struct scsi_device *sdev)\n \n \tspin_lock_irqsave(ap->lock, flags);\n \tdev = __ata_scsi_find_dev(ap, sdev);\n-\tif (dev && dev->sdev) {\n+\t/*\n+\t * Only detach when the canonical (LUN 0) scsi_device is going away.\n+\t * Removing a non-LUN-0 sdev (e.g. a spurious multi-LUN scan result)\n+\t * must not tear down the underlying ATA device.\n+\t */\n+\tif (dev && dev->sdev == sdev) {\n \t\t/* SCSI device already in CANCEL state, no need to offline it */\n \t\tdev->sdev = NULL;\n \t\tdev->flags |= ATA_DFLAG_DETACH;\n@@ -2950,6 +2963,15 @@ static unsigned int atapi_xlat(struct ata_queued_cmd *qc)\n \tmemset(qc->cdb, 0, dev->cdb_len);\n \tmemcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);\n \n+\t/*\n+\t * Encode LUN in CDB byte 1 bits 7:5 for multi-LUN ATAPI devices\n+\t * that use the SCSI-2 CDB LUN convention (e.g. Panasonic PD/CD\n+\t * combo drives).\n+\t */\n+\tif (scmd->device->lun)\n+\t\tqc->cdb[1] = (qc->cdb[1] & 0x1f) |\n+\t\t\t      ((scmd->device->lun & 0x7) << 5);\n+\n \tqc->complete_fn = atapi_qc_complete;\n \n \tqc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;\n@@ -3062,9 +3084,17 @@ static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,\n \n \t/* skip commands not addressed to targets we simulate */\n \tif (!sata_pmp_attached(ap)) {\n-\t\tif (unlikely(scsidev->channel || scsidev->lun))\n+\t\tif (unlikely(scsidev->channel))\n \t\t\treturn NULL;\n \t\tdevno = scsidev->id;\n+\t\t/* Allow non-zero LUNs for ATAPI devices (e.g. PD/CD combos) */\n+\t\tif (unlikely(scsidev->lun)) {\n+\t\t\tstruct ata_device *dev = ata_find_dev(ap, devno);\n+\n+\t\t\tif (!dev || dev->class != ATA_DEV_ATAPI)\n+\t\t\t\treturn NULL;\n+\t\t\treturn dev;\n+\t\t}\n \t} else {\n \t\tif (unlikely(scsidev->id || scsidev->lun))\n \t\t\treturn NULL;\n@@ -4620,7 +4650,7 @@ int ata_scsi_add_hosts(struct ata_host *host, const struct scsi_host_template *s\n \t\tshost->transportt = ata_scsi_transport_template;\n \t\tshost->unique_id = ap->print_id;\n \t\tshost->max_id = 16;\n-\t\tshost->max_lun = 1;\n+\t\tshost->max_lun = 8;\n \t\tshost->max_channel = 1;\n \t\tshost->max_cmd_len = 32;\n \n","prefixes":["2/3"]}