From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 57535606D81A for ; Sun, 8 Nov 2026 20:48:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=vWFsEmNo; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=RM9uX5ns DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Unsubscribe-Post: List-Owner; bh=q9CXgk1Sbc+mG7aFleGXoII7jr2VTpt2NpoubHxsf7U=; b=vWFsEmNoYch/mk nrg6hpppGSMTpGATJNQy/5zzPoTrbM2gflI/YJtld328hgI5l8QALD6drw1LnsCL7pShVKsK0ehDC I4iI81JjCQHgAjWeaDSn0oJk6dWGWMU2IdaTfa7ykpfjyveu9umhJnJDl7ulATG95NcU88Nj2L3Hf Rg67Rgh9VTtdoIob4ePP9AL/gGT3iwJ8xypO9/rUqCdvviiES0PxcR39yalU0C/FWVfb7hHpkoIwS BC38PVB/XYSMz1EQeYy9UUM2ttT2sP0uPty8JCEqqeLFixswtYwxW3n8JGgF5fx0Mxe8gQYoy4Hcj XSTLUkPIylH+JhHTClhQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xQ9oI-0000000Gc7p-07en; Sun, 08 Nov 2026 20:48:18 +0000 Received: from out-78.mta1.migadu.com ([95.215.58.78] helo=mta1.migadu.com) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xQ9o9-0000000GbrU-25is for linux-nvme@lists.infradead.org; Sun, 08 Nov 2026 20:48:10 +0000 X-Envelope-To: linux-nvme@lists.infradead.org DKIM-Signature: a=rsa-sha256; bh=uo2fuVUT6RwART8wxhjQCWV0d8kp+kN+b7O6Im4nr9o=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1794170887; v=1; x=1794775687; b=RM9uX5ns60UXe+iGvwgHEumWNwS4DOCeDl1+UlkbIjOdvltg4odDUOCIj5n5XooMTEsDXrY1 IDlop8kX3TylfDVDFnnCDPEVP92/lyyzkW6PEETGN2/UMFRktn6gLqks4NkaKkU5TO9d8valDgX 6vyIk5aE1N546OIGedBr4R7A= X-Envelope-To: linux-nvme@lists.infradead.org Received: by mta10.migadu.com with ESMTPS id 248345ec56a406a5; Sun, 08 Nov 2026 20:48:07 +0000 X-Mizu-Trace-ID: 248345ec56a406a5 X-Migadu-Flow: FLOW_OUT From: John Garry To: james.bottomley@hansenpartnership.com, mkp@kernel.org Cc: hare@suse.com, linux-nvme@lists.infradead.org, linux-scsi@vger.kernel.org, snitzer@kernel.org, jmeneghi@redhat.com, bmarzins@redhat.com, dm-devel@lists.linux.dev, hch@lst.de, michael.christie@oracle.com, John Garry Subject: [PATCH v9 12/29] scsi-multipath: introduce basic SCSI device support Date: Sun, 8 Nov 2026 20:47:16 +0000 Message-ID: <20261108204733.557995-13-john.garry@linux.dev> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261108204733.557995-1-john.garry@linux.dev> References: <20261108204733.557995-1-john.garry@linux.dev> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261108_124809_695875_6C228427 X-CRM114-Status: GOOD ( 28.08 ) X-Spam-Score: -2.1 (--) X-BeenThere: linux-nvme@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-nvme" X-Spam-Status: No, score=-18.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 For a scsi_device to support multipath, introduce structure scsi_mpath_device to hold multipath-specific details. Like NS structure for NVME, scsi_mpath_device holds the mpath_device structure to device management and path selection. A module param are introduced to enable multipath - the following modes are available: - on - off - always SCSI multipath will only be available until the following conditions: - scsi_multipath enabled and implicit ALUA supported and unique ID available in VPD page 83. - scsi_multipath always mode and unique ID available in VPD page 83 The scsi_device structure contains a pointer to scsi_mpath_device; having this pointer set or unset indicates whether multipath is enabled or disabled for the scsi_device. Signed-off-by: John Garry --- drivers/scsi/Kconfig | 10 +++ drivers/scsi/Makefile | 1 + drivers/scsi/scsi.c | 8 +- drivers/scsi/scsi_multipath.c | 150 ++++++++++++++++++++++++++++++++++ drivers/scsi/scsi_scan.c | 4 + drivers/scsi/scsi_sysfs.c | 2 + include/scsi/scsi_device.h | 2 + include/scsi/scsi_multipath.h | 55 +++++++++++++ 8 files changed, 231 insertions(+), 1 deletion(-) create mode 100644 drivers/scsi/scsi_multipath.c create mode 100644 include/scsi/scsi_multipath.h diff --git a/drivers/scsi/Kconfig b/drivers/scsi/Kconfig index 90b65bebec70..2491fdf6f563 100644 --- a/drivers/scsi/Kconfig +++ b/drivers/scsi/Kconfig @@ -76,6 +76,16 @@ config SCSI_LIB_KUNIT_TEST If unsure say N. +config SCSI_MULTIPATH + bool "SCSI multipath support (EXPERIMENTAL)" + depends on SCSI_MOD + select LIBMULTIPATH + help + This option enables support for native SCSI multipath support for + SCSI host. + + If unsure say N. + comment "SCSI support type (disk, tape, CD-ROM)" depends on SCSI diff --git a/drivers/scsi/Makefile b/drivers/scsi/Makefile index 5b9328dc3b62..989b2455dcc5 100644 --- a/drivers/scsi/Makefile +++ b/drivers/scsi/Makefile @@ -171,6 +171,7 @@ scsi_mod-y += scsi_trace.o scsi_logging.o scsi_mod-$(CONFIG_PM) += scsi_pm.o scsi_mod-$(CONFIG_SCSI_DH) += scsi_dh.o scsi_mod-$(CONFIG_BLK_DEV_BSG) += scsi_bsg.o +scsi_mod-$(CONFIG_SCSI_MULTIPATH) += scsi_multipath.o hv_storvsc-y := storvsc_drv.o diff --git a/drivers/scsi/scsi.c b/drivers/scsi/scsi.c index 05232ab6366f..685297c84755 100644 --- a/drivers/scsi/scsi.c +++ b/drivers/scsi/scsi.c @@ -64,6 +64,7 @@ #include #include #include +#include #include #include "scsi_priv.h" @@ -1057,12 +1058,16 @@ static int __init init_scsi(void) error = scsi_sysfs_register(); if (error) goto cleanup_sysctl; + error = scsi_multipath_init(); + if (error) + goto cleanup_sysfs; scsi_netlink_init(); printk(KERN_NOTICE "SCSI subsystem initialized\n"); return 0; - +cleanup_sysfs: + scsi_sysfs_unregister(); cleanup_sysctl: scsi_exit_sysctl(); cleanup_hosts: @@ -1081,6 +1086,7 @@ static int __init init_scsi(void) static void __exit exit_scsi(void) { scsi_netlink_exit(); + scsi_multipath_exit(); scsi_sysfs_unregister(); scsi_exit_sysctl(); scsi_exit_hosts(); diff --git a/drivers/scsi/scsi_multipath.c b/drivers/scsi/scsi_multipath.c new file mode 100644 index 000000000000..97f7513b4712 --- /dev/null +++ b/drivers/scsi/scsi_multipath.c @@ -0,0 +1,150 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (c) 2026 Oracle Corp + * + */ + +#include +#include +#include +#include +#include +#include + +#include "scsi_priv.h" + +#define TPGS_MODE_IMPLICIT 0x1 + +enum { + SCSI_MULTIPATH_OFF, + SCSI_MULTIPATH_ON, + SCSI_MULTIPATH_ALWAYS, +}; + +static const char *scsi_multipath_modes[] = { + [SCSI_MULTIPATH_OFF] = "off", + [SCSI_MULTIPATH_ON] = "on", + [SCSI_MULTIPATH_ALWAYS] = "always", +}; + +static int scsi_multipath = SCSI_MULTIPATH_OFF; + +static int scsi_multipath_param_set(const char *val, const struct kernel_param *kp) +{ + int mode; + + if (!val) + return -EINVAL; + mode = __sysfs_match_string(scsi_multipath_modes, + ARRAY_SIZE(scsi_multipath_modes), val); + + if (mode < 0) + return mode; + scsi_multipath = mode; + return 0; +} + +static int scsi_multipath_param_get(char *buf, const struct kernel_param *kp) +{ + return sprintf(buf, "%s\n", scsi_multipath_modes[scsi_multipath]); +} + +static const struct kernel_param_ops multipath_param_ops = { + .set = scsi_multipath_param_set, + .get = scsi_multipath_param_get, +}; + +module_param_cb(multipath, &multipath_param_ops, &scsi_multipath, 0444); +MODULE_PARM_DESC(multipath, "turn on native multipath support, options: on, off, always"); + +static int scsi_mpath_unique_lun_id(struct scsi_device *sdev) +{ + struct scsi_mpath_device *scsi_mpath_dev = sdev->scsi_mpath_dev; + int ret; + + ret = scsi_vpd_lun_id(sdev, scsi_mpath_dev->device_id_str, + SCSI_MPATH_DEVICE_ID_LEN); + if (ret < 0) + return ret; + else if (ret == 0) + return -EINVAL; + else if (ret > SCSI_MPATH_DEVICE_ID_LEN) + return -EINVAL; + + return 0; +} + +static int scsi_multipath_sdev_init(struct scsi_device *sdev) +{ + struct Scsi_Host *shost = sdev->host; + struct scsi_mpath_device *scsi_mpath_dev; + struct mpath_device *mpath_device; + + scsi_mpath_dev = kzalloc(sizeof(*scsi_mpath_dev), GFP_KERNEL); + if (!scsi_mpath_dev) + return -ENOMEM; + scsi_mpath_dev->sdev = sdev; + sdev->scsi_mpath_dev = scsi_mpath_dev; + + mpath_device = &scsi_mpath_dev->mpath_device; + mpath_device->numa_node = dev_to_node(shost->dma_dev); + mpath_device->access_state = MPATH_STATE_OPTIMIZED; + + return 0; +} + +static void scsi_multipath_sdev_uninit(struct scsi_device *sdev) +{ + kfree(sdev->scsi_mpath_dev); + sdev->scsi_mpath_dev = NULL; +} + +int scsi_mpath_dev_alloc(struct scsi_device *sdev) +{ + int ret; + + if (scsi_multipath == SCSI_MULTIPATH_OFF) + return 0; + + if (!(scsi_device_tpgs(sdev) & TPGS_MODE_IMPLICIT) && + (scsi_multipath != SCSI_MULTIPATH_ALWAYS)) { + sdev_printk(KERN_DEBUG, sdev, "IMPLICIT TPGS are required for multipath support\n"); + return 0; + } + + ret = scsi_multipath_sdev_init(sdev); + if (ret) + return ret; + + ret = scsi_mpath_unique_lun_id(sdev); + if (ret < 0) + goto out_uninit; + + return 0; + +out_uninit: + scsi_multipath_sdev_uninit(sdev); + return 0; +} + +void scsi_mpath_dev_release(struct scsi_device *sdev) +{ + struct scsi_mpath_device *scsi_mpath_dev = sdev->scsi_mpath_dev; + + if (!scsi_mpath_dev) + return; + + scsi_multipath_sdev_uninit(sdev); +} + +int __init scsi_multipath_init(void) +{ + return 0; +} + +void __exit scsi_multipath_exit(void) +{ +} + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("scsi_multipath"); diff --git a/drivers/scsi/scsi_scan.c b/drivers/scsi/scsi_scan.c index 8aae12fd38f3..b89bb1442a9e 100644 --- a/drivers/scsi/scsi_scan.c +++ b/drivers/scsi/scsi_scan.c @@ -46,6 +46,7 @@ #include #include #include +#include #include "scsi_priv.h" #include "scsi_logging.h" @@ -1129,6 +1130,9 @@ static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result, sdev->max_queue_depth = sdev->queue_depth; WARN_ON_ONCE(sdev->max_queue_depth > sdev->budget_map.depth); + if (scsi_mpath_dev_alloc(sdev)) + return SCSI_SCAN_NO_RESPONSE; + /* * Ok, the device is now all set up, we can * register it and tell the rest of the kernel diff --git a/drivers/scsi/scsi_sysfs.c b/drivers/scsi/scsi_sysfs.c index fe54792ed69e..6a68da42df91 100644 --- a/drivers/scsi/scsi_sysfs.c +++ b/drivers/scsi/scsi_sysfs.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "scsi_priv.h" #include "scsi_logging.h" @@ -456,6 +457,7 @@ static void scsi_device_dev_release(struct device *dev) might_sleep(); scsi_dh_release_device(sdev); + scsi_mpath_dev_release(sdev); parent = sdev->sdev_gendev.parent; diff --git a/include/scsi/scsi_device.h b/include/scsi/scsi_device.h index e6fd6bc30644..1111fb267154 100644 --- a/include/scsi/scsi_device.h +++ b/include/scsi/scsi_device.h @@ -280,6 +280,8 @@ struct scsi_device { struct device sdev_gendev, sdev_dev; + struct scsi_mpath_device *scsi_mpath_dev; + struct work_struct requeue_work; struct scsi_device_handler *handler; diff --git a/include/scsi/scsi_multipath.h b/include/scsi/scsi_multipath.h new file mode 100644 index 000000000000..d3d410dafd17 --- /dev/null +++ b/include/scsi/scsi_multipath.h @@ -0,0 +1,55 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _SCSI_SCSI_MULTIPATH_H +#define _SCSI_SCSI_MULTIPATH_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef CONFIG_SCSI_MULTIPATH +#define SCSI_MPATH_DEVICE_ID_LEN 256 + +struct scsi_mpath_device { + struct mpath_device mpath_device; + struct scsi_device *sdev; + + char device_id_str[SCSI_MPATH_DEVICE_ID_LEN]; +}; +#define to_scsi_mpath_device(d) \ + container_of(d, struct scsi_mpath_device, mpath_device) + +int scsi_mpath_dev_alloc(struct scsi_device *sdev); +void scsi_mpath_dev_release(struct scsi_device *sdev); +int scsi_multipath_init(void); +void scsi_multipath_exit(void); +#else /* CONFIG_SCSI_MULTIPATH */ + +struct scsi_mpath_device { +}; + +static inline int scsi_mpath_dev_alloc(struct scsi_device *sdev) +{ + return 0; +} +static inline void scsi_mpath_dev_release(struct scsi_device *sdev) +{ +} +static inline int scsi_multipath_init(void) +{ + return 0; +} +static inline void scsi_multipath_exit(void) +{ +} +#endif /* CONFIG_SCSI_MULTIPATH */ +#endif /* _SCSI_SCSI_MULTIPATH_H */ -- 2.43.0