Skip to contentSkip to conversation

scsi-multipath: Basic ALUA support

PATCH In review John Garry 3 participants 21 messages Mar 10 – Mar 16

Subsystems (4)
lore
Share & tools
lore

John Garry

author
#0 lore raw

cover scsi-multipath: Basic ALUA support

This series adds basic ALUA support for native SCSI multipath. Only support to send a one-time RTPG is added to get per-path information.

I want to add more ALUA support, for things like ALUA configuration from device rescan. The DH-based ALUA code already supports this. However separating the DH ALUA port group management code from the rest of the DH code is difficult, so I am looking for suggestions on this. There is a consensus to not reuse the device handler code, but it is intertwined with the ALUA driver code in scsi_dh_alua.c

An initial framework is also added to send a periodic TUR per path, to keep path information up-to-date.

This series is based on https://lore.kernel.org/linux-scsi/[email protected]/T/#m76b3a2756124e13b5564c434a38f9c51f64f0bbc
and may be found at https://github.com/johnpgarry/linux/tree/scsi-multipath-pre-7.0-upstream-alua

John Garry (8):
libmultipath: add mpath_call_for_all_devices()
scsi: scsi_dh_alua: Do not attach for SCSI native multipath
scsi: scsi_dh_alua: Pass submit_rtpg() a bool for extended header
support
scsi: Create a core ALUA driver
scsi: scsi-multipath: Add basic ALUA support
scsi: scsi-multipath: Maintain sdev->access_state
scsi: scsi-multipath: Issue a periodic TUR per path
scsi: scsi-multipath: Add stubbed scsi_multipath_dev_rescan()

 drivers/scsi/Kconfig                       |   9 +
 drivers/scsi/Makefile                      |   1 +
 drivers/scsi/device_handler/Kconfig        |   1 +
 drivers/scsi/device_handler/scsi_dh_alua.c | 202 +-------------------
 drivers/scsi/scsi_alua.c                   | 204 ++++++++++++++++++++
 drivers/scsi/scsi_multipath.c              | 206 ++++++++++++++++++++-
 drivers/scsi/scsi_scan.c                   |   2 +
 drivers/scsi/scsi_sysfs.c                  |   2 +-
 include/linux/multipath.h                  |   2 +
 include/scsi/scsi_alua.h                   |  50 +++++
 include/scsi/scsi_multipath.h              |   8 +
 lib/multipath.c                            |  15 ++
 12 files changed, 501 insertions(+), 201 deletions(-)
 create mode 100644 drivers/scsi/scsi_alua.c
 create mode 100644 include/scsi/scsi_alua.h
Signature · 1 line
2.43.5
author
#1 lore raw

PATCH 1/8 libmultipath: add mpath_call_for_all_devices()

Add mpath_call_for_all_devices(), which iteratively calls a CB function for all devices.

---
 include/linux/multipath.h |  2 ++
 lib/multipath.c           | 15 +++++++++++++++
 2 files changed, 17 insertions(+)
include/linux/multipath.h +2 -0
index 1aa70ae11a195..153eb4c0258ef 100644
@@ -153,6 +153,8 @@ void mpath_delete_device(struct mpath_head *mpath_head,
 			struct mpath_device *mpath_device);
 int mpath_call_for_device(struct mpath_head *mpath_head,
 			int (*cb)(struct mpath_device *mpath_device));
+void mpath_call_for_all_devices(struct mpath_head *mpath_head,
+			void (*cb)(struct mpath_device *mpath_device));
 void mpath_clear_paths(struct mpath_head *mpath_head);
 void mpath_revalidate_paths(struct mpath_disk *mpath_disk,
 	void (*cb)(struct mpath_device *mpath_device, sector_t capacity));
lib/multipath.c +15 -0
index bba13b18215ee..96c6730680729 100644
@@ -105,6 +105,21 @@ int mpath_call_for_device(struct mpath_head *mpath_head,
 }
 EXPORT_SYMBOL_GPL(mpath_call_for_device);
 
+void mpath_call_for_all_devices(struct mpath_head *mpath_head,
+			void (*cb)(struct mpath_device *mpath_device))
+{
+	struct mpath_device *mpath_device;
+	int srcu_idx;
+
+	srcu_idx = srcu_read_lock(&mpath_head->srcu);
+	list_for_each_entry_srcu(mpath_device, &mpath_head->dev_list, siblings,
+				 srcu_read_lock_held(&mpath_head->srcu)) {
+		cb(mpath_device);
+	}
+	srcu_read_unlock(&mpath_head->srcu, srcu_idx);
+}
+EXPORT_SYMBOL_GPL(mpath_call_for_all_devices);
+
 bool mpath_clear_current_path(struct mpath_head *mpath_head,
 			struct mpath_device *mpath_device)
 {
Signature · 1 line
2.43.5
author
#2 lore raw

PATCH 2/8 scsi: scsi_dh_alua: Do not attach for SCSI native multipath

In reply to John Garry

For native SCSI multipath, we don't want device handlers involved, so do not attach in this instance.

For now, SCSI multipath will not maintain sdev->access_state.

---
 drivers/scsi/device_handler/scsi_dh_alua.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/scsi/device_handler/scsi_dh_alua.c +2 -0
index 6fd89ae33059f..7b360e7f11a6d 100644
@@ -1239,6 +1239,8 @@ static int alua_bus_attach(struct scsi_device *sdev)
 	struct alua_dh_data *h;
 	int err;
 
+	if (sdev->scsi_mpath_dev)
+		return SCSI_DH_DEV_UNSUPP;
 	h = kzalloc(sizeof(*h) , GFP_KERNEL);
 	if (!h)
 		return SCSI_DH_NOMEM;
Signature · 1 line
2.43.5
author
#3 lore raw

PATCH 3/8 scsi: scsi_dh_alua: Pass submit_rtpg() a bool for extended header support

In reply to John Garry

Currently the pg flags is passed to submit_rtpg() to allow submit_rtpg() know whether extended header support is available.

Pass a bool instead, as when separating the driver into a pure ALUA part, we don't want to use pg flags.

---
 drivers/scsi/device_handler/scsi_dh_alua.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/scsi/device_handler/scsi_dh_alua.c +7 -5
index 7b360e7f11a6d..e9edd45ae28a3 100644
@@ -124,7 +124,8 @@ static void release_port_group(struct kref *kref)
  * @sdev: sdev the command should be sent to
  */
 static int submit_rtpg(struct scsi_device *sdev, unsigned char *buff,
-		       int bufflen, struct scsi_sense_hdr *sshdr, int flags)
+		       int bufflen, struct scsi_sense_hdr *sshdr,
+		       bool ext_hdr_unsupp)
 {
 	u8 cdb[MAX_COMMAND_SIZE];
 	blk_opf_t opf = REQ_OP_DRV_IN | REQ_FAILFAST_DEV |
@@ -136,10 +137,10 @@ static int submit_rtpg(struct scsi_device *sdev, unsigned char *buff,
 	/* Prepare the command. */
 	memset(cdb, 0x0, MAX_COMMAND_SIZE);
 	cdb[0] = MAINTENANCE_IN;
-	if (!(flags & ALUA_RTPG_EXT_HDR_UNSUPP))
-		cdb[1] = MI_REPORT_TARGET_PGS | MI_EXT_HDR_PARAM_FMT;
-	else
+	if (ext_hdr_unsupp)
 		cdb[1] = MI_REPORT_TARGET_PGS;
+	else
+		cdb[1] = MI_REPORT_TARGET_PGS | MI_EXT_HDR_PARAM_FMT;
 	put_unaligned_be32(bufflen, &cdb[6]);
 
 	return scsi_execute_cmd(sdev, cdb, opf, buff, bufflen,
@@ -566,7 +567,8 @@ static int alua_rtpg(struct scsi_device *sdev, struct alua_port_group *pg)
 
  retry:
 	err = 0;
-	retval = submit_rtpg(sdev, buff, bufflen, &sense_hdr, pg->flags);
+	retval = submit_rtpg(sdev, buff, bufflen, &sense_hdr,
+			pg->flags & ALUA_RTPG_EXT_HDR_UNSUPP);
 
 	if (retval) {
 		/*
Signature · 1 line
2.43.5
author
#4 lore raw

PATCH 4/8 scsi: Create a core ALUA driver

In reply to John Garry

Add a dedicated ALUA driver which can be used for native SCSI multipath and also DH-based ALUA support.

Only core functions to submit a RTPG, STPG, tur, and also helper functions are added.

The code from scsi_dh_alua.c to maintain the port groups is not added, because it is quite intertwined with the DH code. However the port group management code would be quite useful.

Hannes Reinecke originally authored this code.

---
 drivers/scsi/Kconfig                       |   9 +
 drivers/scsi/Makefile                      |   1 +
 drivers/scsi/device_handler/Kconfig        |   1 +
 drivers/scsi/device_handler/scsi_dh_alua.c | 198 +-------------------
 drivers/scsi/scsi_alua.c                   | 204 +++++++++++++++++++++
 include/scsi/scsi_alua.h                   |  50 +++++
 6 files changed, 269 insertions(+), 194 deletions(-)
 create mode 100644 drivers/scsi/scsi_alua.c
 create mode 100644 include/scsi/scsi_alua.h
drivers/scsi/Kconfig +9 -0
index cfab7ad1e3c2c..1a538c84ddf0a 100644
@@ -86,6 +86,15 @@ config SCSI_MULTIPATH
 
 	  If unsure say N.
 
+config SCSI_ALUA
+	tristate "SPC-3 ALUA support"
+	depends on SCSI
+	help
+	  SCSI support for generic SPC-3 Asymmetric Logical Unit
+	  Access (ALUA).
+
+	  If unsure, say Y.
+
 comment "SCSI support type (disk, tape, CD-ROM)"
 	depends on SCSI
 
drivers/scsi/Makefile +1 -0
index 64b7a82828b81..053fb51d98092 100644
@@ -153,6 +153,7 @@ obj-$(CONFIG_SCSI_ENCLOSURE)	+= ses.o
 
 obj-$(CONFIG_SCSI_HISI_SAS) += hisi_sas/
 
+obj-$(CONFIG_SCSI_ALUA) += scsi_alua.o
 # This goes last, so that "real" scsi devices probe earlier
 obj-$(CONFIG_SCSI_DEBUG)	+= scsi_debug.o
 scsi_mod-y			+= scsi.o hosts.o scsi_ioctl.o \
drivers/scsi/device_handler/Kconfig +1 -0
index 368eb94c24562..ff06aea8c272c 100644
@@ -35,6 +35,7 @@ config SCSI_DH_EMC
 config SCSI_DH_ALUA
 	tristate "SPC-3 ALUA Device Handler"
 	depends on SCSI_DH && SCSI
+	select SCSI_ALUA
 	help
 	  SCSI Device handler for generic SPC-3 Asymmetric Logical Unit
 	  Access (ALUA).
drivers/scsi/device_handler/scsi_dh_alua.c +4 -194

244 lines not on this page · raw message

drivers/scsi/scsi_alua.c new file +204 -0

204 lines not on this page · raw message

include/scsi/scsi_alua.h new file +50 -0

50 lines not on this page · raw message

Signature · 1 line
2.43.5
#5 lore raw

Re: [PATCH 4/8] scsi: Create a core ALUA driver

Quoted 87 lines · John Garry

Add a dedicated ALUA driver which can be used for native SCSI multipath and also DH-based ALUA support.

Only core functions to submit a RTPG, STPG, tur, and also helper functions are added.

The code from scsi_dh_alua.c to maintain the port groups is not added, because it is quite intertwined with the DH code. However the port group management code would be quite useful.

Hannes Reinecke originally authored this code.

drivers/scsi/scsi_alua.c new file +68 -0
new file mode 100644
index 0000000000000..2e4102192dcb9
@@ -0,0 +1,204 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Generic SCSI-3 ALUA SCSI driver
+ *
+ * Copyright (C) 2007-2010 Hannes Reinecke, SUSE Linux Products GmbH.
+ * All rights reserved.
+ */
+
+#include <scsi/scsi.h>
+#include <scsi/scsi_proto.h>
+#include <scsi/scsi_dbg.h>
+#include <scsi/scsi_eh.h>
+#include <scsi/scsi_alua.h>
+
+#define DRV_NAME "alua"
+
+#define ALUA_FAILOVER_RETRIES		5
+
+/*
+ * alua_check_tpgs - Evaluate TPGS setting
+ * @sdev: device to be checked
+ *
+ * Examine the TPGS setting of the sdev to find out if ALUA
+ * is supported.
+ */
+int alua_check_tpgs(struct scsi_device *sdev)
+{
+	int tpgs = TPGS_MODE_NONE;
+
+	/*
+	 * ALUA support for non-disk devices is fraught with
+	 * difficulties, so disable it for now.
+	 */
+	if (sdev->type != TYPE_DISK) {
+		sdev_printk(KERN_INFO, sdev,
+			    "%s: disable for non-disk devices\n",
+			    DRV_NAME);
+		return tpgs;
+	}
+
+	tpgs = scsi_device_tpgs(sdev);
+	switch (tpgs) {
+	case TPGS_MODE_EXPLICIT|TPGS_MODE_IMPLICIT:
+		sdev_printk(KERN_INFO, sdev,
+			    "%s: supports implicit and explicit TPGS\n",
+			    DRV_NAME);
+		break;
+	case TPGS_MODE_EXPLICIT:
+		sdev_printk(KERN_INFO, sdev, "%s: supports explicit TPGS\n",
+			    DRV_NAME);
+		break;
+	case TPGS_MODE_IMPLICIT:
+		sdev_printk(KERN_INFO, sdev, "%s: supports implicit TPGS\n",
+			    DRV_NAME);
+		break;
+	case TPGS_MODE_NONE:
+		sdev_printk(KERN_INFO, sdev, "%s: not supported\n",
+			    DRV_NAME);
+		break;
+	default:
+		sdev_printk(KERN_INFO, sdev,
+			    "%s: unsupported TPGS setting %d\n",
+			    DRV_NAME, tpgs);
+		tpgs = TPGS_MODE_NONE;
+		break;
+	}
+
+	return tpgs;

drivers/scsi/scsi_alua.c line 78

Quoted 10 lines
drivers/scsi/scsi_alua.c new file +10 -0
+}
+EXPORT_SYMBOL_GPL(alua_check_tpgs);
+
+/*
+ * alua_tur - Send a TEST UNIT READY
+ * @sdev: device to which the TEST UNIT READY command should be send
+ *
+ * Send a TEST UNIT READY to @sdev to figure out the device state
+ * Returns SCSI_DH_RETRY if the sense code is NOT READY/ALUA TRANSITIONING,
+ * 0 if no error occurred, and SCSI_DH_IO otherwise.

Nitpick: The comment here still references SCSI_DH_ values

-Ben

Quoted 2 lines
drivers/scsi/scsi_alua.c new file +2 -0
+ */
+int alua_tur(struct scsi_device *sdev)
author
#6 lore raw

Re: [PATCH 4/8] scsi: Create a core ALUA driver

Quoted 9 lines · Benjamin Marzinski

drivers/scsi/scsi_alua.c line 78

Quoted 8 lines
drivers/scsi/scsi_alua.c new file +8 -0
+
+/*
+ * alua_tur - Send a TEST UNIT READY
+ * @sdev: device to which the TEST UNIT READY command should be send
+ *
+ * Send a TEST UNIT READY to @sdev to figure out the device state
+ * Returns SCSI_DH_RETRY if the sense code is NOT READY/ALUA TRANSITIONING,
+ * 0 if no error occurred, and SCSI_DH_IO otherwise.

Nitpick: The comment here still references SCSI_DH_ values

Sure, I'll pay attention to removing DH remnants.

Thanks

author
#7 lore raw

PATCH 5/8 scsi: scsi-multipath: Add basic ALUA support

In reply to John Garry

Add basic support just to get the per-port group state.

This support does not account of state transitioning, sdev port group reconfiguration, etc, required for full support.

libmultipath callbacks scsi_mpath_is_optimized() and scsi_mpath_is_disabled() are updated to take account of the ALUA-provided path information.

As before, for no ALUA support (and scsi_multipath_always on) we assume that the paths are all optimized.

Much of this code in scsi_mpath_alua_init() is copied from scsi_dh_alua.c, originally authored by Hannes Reinecke.

---
 drivers/scsi/scsi_multipath.c | 163 ++++++++++++++++++++++++++++++++--
 include/scsi/scsi_multipath.h |   3 +
 2 files changed, 160 insertions(+), 6 deletions(-)
drivers/scsi/scsi_multipath.c +157 -6
index 1489c7e979167..0a314080bf0a5 100644
@@ -4,6 +4,7 @@
  *
  */
 
+#include <scsi/scsi_alua.h>
 #include <scsi/scsi_cmnd.h>
 #include <scsi/scsi_driver.h>
 #include <scsi/scsi_proto.h>
@@ -346,18 +347,29 @@ static bool scsi_mpath_is_disabled(struct mpath_device *mpath_device)
 				to_scsi_mpath_device(mpath_device);
 	struct scsi_device *sdev = scsi_mpath_dev->sdev;
 	enum scsi_device_state sdev_state = sdev->sdev_state;
+	int alua_state = scsi_mpath_dev->alua_state;
 
 	if (sdev_state == SDEV_RUNNING || sdev_state == SDEV_CANCEL)
 		return false;
 
-	return true;
+	if (alua_state == SCSI_ACCESS_STATE_OPTIMAL ||
+	    alua_state == SCSI_ACCESS_STATE_ACTIVE)
+		return true;
+
+	return false;
 }
 
 static bool scsi_mpath_is_optimized(struct mpath_device *mpath_device)
 {
+	struct scsi_mpath_device *scsi_mpath_dev =
+				to_scsi_mpath_device(mpath_device);
+
 	if (scsi_mpath_is_disabled(mpath_device))
 		return false;
-	return true;
+	if (scsi_mpath_dev->alua_state == SCSI_ACCESS_STATE_OPTIMAL)
+		return true;
+	return false;
+
 }
 
 /* Until we have ALUA support, we're always optimised */
@@ -366,7 +378,7 @@ static enum mpath_access_state scsi_mpath_get_access_state(
 {
 	if (scsi_mpath_is_disabled(mpath_device))
 		return MPATH_STATE_INVALID;
-	return MPATH_STATE_OPTIMIZED;
+	return scsi_mpath_is_optimized(mpath_device);
 }
 
 static bool scsi_mpath_available_path(struct mpath_device *mpath_device, bool *available)
@@ -579,16 +591,147 @@ static void scsi_multipath_sdev_uninit(struct scsi_device *sdev)
 	sdev->scsi_mpath_dev = NULL;
 }
 
+static int scsi_mpath_alua_init(struct scsi_device *sdev)
+{
+	struct scsi_mpath_device *scsi_mpath_dev = sdev->scsi_mpath_dev;
+	struct scsi_sense_hdr sense_hdr;
+	int len, k, off, bufflen = ALUA_RTPG_SIZE;
+	unsigned char *desc, *buff;
+	unsigned int tpg_desc_tbl_off;
+	int group_id, rel_port = -1;
+	bool ext_hdr_unsupp = false;
+	int ret;
+
+	group_id = scsi_vpd_tpg_id(sdev, &rel_port);
+	if (group_id < 0) {
+		/*
+		 * Internal error; TPGS supported but required
+		 * VPD identification descriptors not present.
+		 * Disable ALUA support.
+		 */
+		sdev_printk(KERN_INFO, sdev,
+			    "%s: No target port descriptors found\n",
+			    __func__);
+		return -EIO;
+	}
+
+	buff = kzalloc(bufflen, GFP_KERNEL);
+	if (!buff)
+		return -ENOMEM;
+ retry:
+	ret = submit_rtpg(sdev, buff, bufflen, &sense_hdr,
+				ext_hdr_unsupp);
+
+	if (ret) {
+		if (ret < 0 || !scsi_sense_valid(&sense_hdr)) {
+			sdev_printk(KERN_INFO, sdev,
+				    "%s: rtpg failed, result %d\n",
+				    __func__, ret);
+			kfree(buff);
+			if (ret < 0)
+				return -EBUSY;
+			if (host_byte(ret) == DID_NO_CONNECT)
+				return -ENODEV;
+			return -EIO;
+		}
+
+		/*
+		 * submit_rtpg() has failed on existing arrays
+		 * when requesting extended header info, and
+		 * the array doesn't support extended headers,
+		 * even though it shouldn't according to T10.
+		 * The retry without rtpg_ext_hdr_req set
+		 * handles this.
+		 * Note:  some arrays return a sense key of ILLEGAL_REQUEST
+		 * with ASC 00h if they don't support the extended header.
+		 */
+		if (ext_hdr_unsupp &&
+		    sense_hdr.sense_key == ILLEGAL_REQUEST) {
+			ext_hdr_unsupp = true;
+			goto retry;
+		}
+		/*
+		 * If the array returns with 'ALUA state transition'
+		 * sense code here it cannot return RTPG data during
+		 * transition. So set the state to 'transitioning' directly.
+		 */
+		if (sense_hdr.sense_key == NOT_READY &&
+		    sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x0a)
+			goto out;
+
+		/*
+		 * Retry on any other UNIT ATTENTION occurred.
+		 */
+		if (sense_hdr.sense_key == UNIT_ATTENTION) {
+			scsi_print_sense_hdr(sdev, __func__, &sense_hdr);
+			kfree(buff);
+			return -EAGAIN;
+		}
+		sdev_printk(KERN_ERR, sdev, "%s: rtpg failed\n",
+			    __func__);
+		scsi_print_sense_hdr(sdev, __func__, &sense_hdr);
+		kfree(buff);
+		return -EIO;
+	}
+
+	len = get_unaligned_be32(&buff[0]) + 4;
+
+	if (len > bufflen) {
+		/* Resubmit with the correct length */
+		kfree(buff);
+		bufflen = len;
+		buff = kmalloc(bufflen, GFP_KERNEL);
+		if (!buff) {
+			/* Temporary failure, bypass */
+			return -EBUSY;
+		}
+		goto retry;
+	}
+
+	if ((buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR)
+		tpg_desc_tbl_off = 8;
+	else
+		tpg_desc_tbl_off = 4;
+
+	for (k = tpg_desc_tbl_off, desc = buff + tpg_desc_tbl_off;
+	     k < len;
+	     k += off, desc += off) {
+		u16 group_id_found = get_unaligned_be16(&desc[2]);
+
+		if (group_id_found == group_id) {
+			int valid_states, state, pref;
+
+			state = desc[0] & 0x0f;
+			pref = desc[0] >> 7;
+			valid_states = desc[1];
+
+			alua_print_info(sdev, group_id, state, pref, valid_states);
+
+			scsi_mpath_dev->alua_state = state;
+			scsi_mpath_dev->alua_pref = pref;
+			scsi_mpath_dev->alua_valid_states = valid_states;
+			goto out;
+		}
+
+		off = 8 + (desc[7] * 4);
+	}
+
+out:
+	kfree(buff);
+	return 0;
+}
+
 int scsi_mpath_dev_alloc(struct scsi_device *sdev)
 {
 	struct scsi_mpath_head *scsi_mpath_head;
-	int ret;
+	int ret, tpgs;
 
 	if (!scsi_multipath)
 		return 0;
 
-	if (!scsi_device_tpgs(sdev) && !scsi_multipath_always) {
-		sdev_printk(KERN_NOTICE, sdev, "tpgs are required for multipath support\n");
+	tpgs = alua_check_tpgs(sdev);
+	if (!(tpgs & TPGS_MODE_IMPLICIT) && !scsi_multipath_always) {
+		sdev_printk(KERN_DEBUG, sdev, "IMPLICIT TPGS are required for multipath support\n");
 		return 0;
 	}
 
@@ -622,6 +765,14 @@ int scsi_mpath_dev_alloc(struct scsi_device *sdev)
 	sdev->scsi_mpath_dev->scsi_mpath_head = scsi_mpath_head;
 
 found:
+	if (tpgs & TPGS_MODE_IMPLICIT) {
+		ret = scsi_mpath_alua_init(sdev);
+		if (ret)
+			goto out_put_head;
+	} else {
+		sdev->scsi_mpath_dev->alua_state = SCSI_ACCESS_STATE_OPTIMAL;
+	}
+
 	sdev->scsi_mpath_dev->index = ida_alloc(&scsi_mpath_head->ida, GFP_KERNEL);
 	if (sdev->scsi_mpath_dev->index < 0) {
 		ret = sdev->scsi_mpath_dev->index;
include/scsi/scsi_multipath.h +3 -0
index 2011447f482d6..7c7ee2fb7def7 100644
@@ -38,6 +38,9 @@ struct scsi_mpath_device {
 	int			index;
 	atomic_t		nr_active;
 	struct scsi_mpath_head	*scsi_mpath_head;
+	int			alua_state;
+	int			alua_pref;
+	int			alua_valid_states;
 
 	char			device_id_str[SCSI_MPATH_DEVICE_ID_LEN];
 };
Signature · 1 line
2.43.5
#8 lore raw

Re: [PATCH 5/8] scsi: scsi-multipath: Add basic ALUA support

Quoted 245 lines · John Garry

Add basic support just to get the per-port group state.

This support does not account of state transitioning, sdev port group reconfiguration, etc, required for full support.

libmultipath callbacks scsi_mpath_is_optimized() and scsi_mpath_is_disabled() are updated to take account of the ALUA-provided path information.

As before, for no ALUA support (and scsi_multipath_always on) we assume that the paths are all optimized.

Much of this code in scsi_mpath_alua_init() is copied from scsi_dh_alua.c, originally authored by Hannes Reinecke.

---
  drivers/scsi/scsi_multipath.c | 163 ++++++++++++++++++++++++++++++++--
  include/scsi/scsi_multipath.h |   3 +
  2 files changed, 160 insertions(+), 6 deletions(-)
drivers/scsi/scsi_multipath.c +157 -6
index 1489c7e979167..0a314080bf0a5 100644
@@ -4,6 +4,7 @@
   *
   */
 
+#include <scsi/scsi_alua.h>
  #include <scsi/scsi_cmnd.h>
  #include <scsi/scsi_driver.h>
  #include <scsi/scsi_proto.h>
@@ -346,18 +347,29 @@ static bool scsi_mpath_is_disabled(struct mpath_device *mpath_device)
  				to_scsi_mpath_device(mpath_device);
  	struct scsi_device *sdev = scsi_mpath_dev->sdev;
  	enum scsi_device_state sdev_state = sdev->sdev_state;
+	int alua_state = scsi_mpath_dev->alua_state;
 
  	if (sdev_state == SDEV_RUNNING || sdev_state == SDEV_CANCEL)
  		return false;
 
-	return true;
+	if (alua_state == SCSI_ACCESS_STATE_OPTIMAL ||
+	    alua_state == SCSI_ACCESS_STATE_ACTIVE)
+		return true;
+
+	return false;
  }
 
  static bool scsi_mpath_is_optimized(struct mpath_device *mpath_device)
  {
+	struct scsi_mpath_device *scsi_mpath_dev =
+				to_scsi_mpath_device(mpath_device);
+
  	if (scsi_mpath_is_disabled(mpath_device))
  		return false;
-	return true;
+	if (scsi_mpath_dev->alua_state == SCSI_ACCESS_STATE_OPTIMAL)
+		return true;
+	return false;
+
  }
 
  /* Until we have ALUA support, we're always optimised */
@@ -366,7 +378,7 @@ static enum mpath_access_state scsi_mpath_get_access_state(
  {
  	if (scsi_mpath_is_disabled(mpath_device))
  		return MPATH_STATE_INVALID;
-	return MPATH_STATE_OPTIMIZED;
+	return scsi_mpath_is_optimized(mpath_device);
  }
 
  static bool scsi_mpath_available_path(struct mpath_device *mpath_device, bool *available)
@@ -579,16 +591,147 @@ static void scsi_multipath_sdev_uninit(struct scsi_device *sdev)
  	sdev->scsi_mpath_dev = NULL;
  }
 
+static int scsi_mpath_alua_init(struct scsi_device *sdev)
+{
+	struct scsi_mpath_device *scsi_mpath_dev = sdev->scsi_mpath_dev;
+	struct scsi_sense_hdr sense_hdr;
+	int len, k, off, bufflen = ALUA_RTPG_SIZE;
+	unsigned char *desc, *buff;
+	unsigned int tpg_desc_tbl_off;
+	int group_id, rel_port = -1;
+	bool ext_hdr_unsupp = false;
+	int ret;
+
+	group_id = scsi_vpd_tpg_id(sdev, &rel_port);
+	if (group_id < 0) {
+		/*
+		 * Internal error; TPGS supported but required
+		 * VPD identification descriptors not present.
+		 * Disable ALUA support.
+		 */
+		sdev_printk(KERN_INFO, sdev,
+			    "%s: No target port descriptors found\n",
+			    __func__);
+		return -EIO;
+	}
+
+	buff = kzalloc(bufflen, GFP_KERNEL);
+	if (!buff)
+		return -ENOMEM;
+ retry:
+	ret = submit_rtpg(sdev, buff, bufflen, &sense_hdr,
+				ext_hdr_unsupp);
+
+	if (ret) {
+		if (ret < 0 || !scsi_sense_valid(&sense_hdr)) {
+			sdev_printk(KERN_INFO, sdev,
+				    "%s: rtpg failed, result %d\n",
+				    __func__, ret);
+			kfree(buff);
+			if (ret < 0)
+				return -EBUSY;
+			if (host_byte(ret) == DID_NO_CONNECT)
+				return -ENODEV;
+			return -EIO;
+		}
+
+		/*
+		 * submit_rtpg() has failed on existing arrays
+		 * when requesting extended header info, and
+		 * the array doesn't support extended headers,
+		 * even though it shouldn't according to T10.
+		 * The retry without rtpg_ext_hdr_req set
+		 * handles this.
+		 * Note:  some arrays return a sense key of ILLEGAL_REQUEST
+		 * with ASC 00h if they don't support the extended header.
+		 */
+		if (ext_hdr_unsupp &&
+		    sense_hdr.sense_key == ILLEGAL_REQUEST) {
+			ext_hdr_unsupp = true;
+			goto retry;
+		}
+		/*
+		 * If the array returns with 'ALUA state transition'
+		 * sense code here it cannot return RTPG data during
+		 * transition. So set the state to 'transitioning' directly.
+		 */
+		if (sense_hdr.sense_key == NOT_READY &&
+		    sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x0a)
+			goto out;
+
+		/*
+		 * Retry on any other UNIT ATTENTION occurred.
+		 */
+		if (sense_hdr.sense_key == UNIT_ATTENTION) {
+			scsi_print_sense_hdr(sdev, __func__, &sense_hdr);
+			kfree(buff);
+			return -EAGAIN;
+		}
+		sdev_printk(KERN_ERR, sdev, "%s: rtpg failed\n",
+			    __func__);
+		scsi_print_sense_hdr(sdev, __func__, &sense_hdr);
+		kfree(buff);
+		return -EIO;
+	}
+
+	len = get_unaligned_be32(&buff[0]) + 4;
+
+	if (len > bufflen) {
+		/* Resubmit with the correct length */
+		kfree(buff);
+		bufflen = len;
+		buff = kmalloc(bufflen, GFP_KERNEL);
+		if (!buff) {
+			/* Temporary failure, bypass */
+			return -EBUSY;
+		}
+		goto retry;
+	}
+
+	if ((buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR)
+		tpg_desc_tbl_off = 8;
+	else
+		tpg_desc_tbl_off = 4;
+
+	for (k = tpg_desc_tbl_off, desc = buff + tpg_desc_tbl_off;
+	     k < len;
+	     k += off, desc += off) {
+		u16 group_id_found = get_unaligned_be16(&desc[2]);
+
+		if (group_id_found == group_id) {
+			int valid_states, state, pref;
+
+			state = desc[0] & 0x0f;
+			pref = desc[0] >> 7;
+			valid_states = desc[1];
+
+			alua_print_info(sdev, group_id, state, pref, valid_states);
+
+			scsi_mpath_dev->alua_state = state;
+			scsi_mpath_dev->alua_pref = pref;
+			scsi_mpath_dev->alua_valid_states = valid_states;
+			goto out;
+		}
+
+		off = 8 + (desc[7] * 4);
+	}
+
+out:
+	kfree(buff);
+	return 0;
+}
+
  int scsi_mpath_dev_alloc(struct scsi_device *sdev)
  {
  	struct scsi_mpath_head *scsi_mpath_head;
-	int ret;
+	int ret, tpgs;
 
  	if (!scsi_multipath)
  		return 0;
 
-	if (!scsi_device_tpgs(sdev) && !scsi_multipath_always) {
-		sdev_printk(KERN_NOTICE, sdev, "tpgs are required for multipath support\n");
+	tpgs = alua_check_tpgs(sdev);
+	if (!(tpgs & TPGS_MODE_IMPLICIT) && !scsi_multipath_always) {
+		sdev_printk(KERN_DEBUG, sdev, "IMPLICIT TPGS are required for multipath support\n");
  		return 0;
  	}
 
@@ -622,6 +765,14 @@ int scsi_mpath_dev_alloc(struct scsi_device *sdev)
  	sdev->scsi_mpath_dev->scsi_mpath_head = scsi_mpath_head;
 
  found:
+	if (tpgs & TPGS_MODE_IMPLICIT) {
+		ret = scsi_mpath_alua_init(sdev);
+		if (ret)
+			goto out_put_head;
+	} else {
+		sdev->scsi_mpath_dev->alua_state = SCSI_ACCESS_STATE_OPTIMAL;
+	}
+
  	sdev->scsi_mpath_dev->index = ida_alloc(&scsi_mpath_head->ida, GFP_KERNEL);
  	if (sdev->scsi_mpath_dev->index < 0) {
  		ret = sdev->scsi_mpath_dev->index;
include/scsi/scsi_multipath.h +0 -0
index 2011447f482d6..7c7ee2fb7def7 100644

include/scsi/scsi_multipath.h struct scsi_mpath_device { · line 46

Quoted 9 lines
include/scsi/scsi_multipath.h +3 -0
@@ -38,6 +38,9 @@ struct scsi_mpath_device {
  	int			index;
  	atomic_t		nr_active;
  	struct scsi_mpath_head	*scsi_mpath_head;
+	int			alua_state;
+	int			alua_pref;
+	int			alua_valid_states;
 
  	char			device_id_str[SCSI_MPATH_DEVICE_ID_LEN];
  };

Is there a specific reason why this cannot be in the generic code? After all, if the device reports anything else than ALUA_STATE_OPTIMAL or ALUA_STATE_ACTIVE I/O will fail, irrespective of multipath being active.

I would love to see that in the generic SCSI code, independent on this patchset. It would allow us to simplify the device handler code, too, as then device handler really would only be required for explicit
ALUA. (And could be ignored for scsi-multipathing).

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                               +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
author
#9 lore raw

Re: [PATCH 5/8] scsi: scsi-multipath: Add basic ALUA support

Quoted 10 lines · Hannes Reinecke
Quoted 10 lines

      sdev->scsi_mpath_dev->index = ida_alloc(&scsi_mpath_head->ida, GFP_KERNEL); if (sdev->scsi_mpath_dev->index < 0) {
          ret = sdev->scsi_mpath_dev->index;

include/scsi/scsi_multipath.h +0 -0
index 2011447f482d6..7c7ee2fb7def7 100644
@@ -38,6 +38,9 @@ struct scsi_mpath_device {
       int            index;
       atomic_t        nr_active;
Quoted 8 lines

include/scsi/scsi_multipath.h struct scsi_mpath_device { · line 46

Quoted 6 lines
include/scsi/scsi_multipath.h +3 -0
       struct scsi_mpath_head    *scsi_mpath_head;
+    int            alua_state;
+    int            alua_pref;
+    int            alua_valid_states;
       char            device_id_str[SCSI_MPATH_DEVICE_ID_LEN];
   };

Is there a specific reason why this cannot be in the generic code?

Sure, it's possible....

Quoted 7 lines

After all, if the device reports anything else than ALUA_STATE_OPTIMAL or ALUA_STATE_ACTIVE I/O will fail, irrespective of multipath being active.

I would love to see that in the generic SCSI code, independent on this patchset. It would allow us to simplify the device handler code, too, as then device handler really would only be required for explicit
ALUA. (And could be ignored for scsi-multipathing).

Right, so you would like to see alua_port_group management in a core ALUA driver as well, right?

If yes, to repeat, it is hard to separate the DH stuff out...but I can try. Examples I would need to deal with (and associated handling):

- alua_port_group members like dh_list
- alua_dh_data memebers like init_error
- everything in alua_queue_data

Thanks

#10 lore raw

Re: [PATCH 5/8] scsi: scsi-multipath: Add basic ALUA support

Quoted 30 lines · John Garry
Quoted 10 lines · Hannes Reinecke
Quoted 10 lines

      sdev->scsi_mpath_dev->index = ida_alloc(&scsi_mpath_head->ida, GFP_KERNEL); if (sdev->scsi_mpath_dev->index < 0) {
          ret = sdev->scsi_mpath_dev->index;

include/scsi/scsi_multipath.h +0 -0
index 2011447f482d6..7c7ee2fb7def7 100644
@@ -38,6 +38,9 @@ struct scsi_mpath_device {
       int            index;
       atomic_t        nr_active;
Quoted 8 lines

include/scsi/scsi_multipath.h struct scsi_mpath_device { · line 46

Quoted 6 lines
include/scsi/scsi_multipath.h +3 -0
       struct scsi_mpath_head    *scsi_mpath_head;
+    int            alua_state;
+    int            alua_pref;
+    int            alua_valid_states;
       char            device_id_str[SCSI_MPATH_DEVICE_ID_LEN];
   };

Is there a specific reason why this cannot be in the generic code?

Sure, it's possible....

Quoted 7 lines

After all, if the device reports anything else than ALUA_STATE_OPTIMAL or ALUA_STATE_ACTIVE I/O will fail, irrespective of multipath being active.

I would love to see that in the generic SCSI code, independent on this patchset. It would allow us to simplify the device handler code, too, as then device handler really would only be required for explicit
ALUA. (And could be ignored for scsi-multipathing).

Quoted 8 lines

Right, so you would like to see alua_port_group management in a core ALUA driver as well, right?

If yes, to repeat, it is hard to separate the DH stuff out...but I can try. Examples I would need to deal with (and associated handling):

- alua_port_group members like dh_list
- alua_dh_data memebers like init_error
- everything in alua_queue_data

While the port group handling looks nice (and there certainly is a certain neatness to it), it kinda assumes too much about the internal layout of the hierarchy within the target. Technically, a target is only required to provide a device identifier, and a group id (such that you can match with RTPG output). However, you have no idea which of the various device IDs are part of the same enclosure; that information is not required to be present.
So you cannot assume that group ID A reported from device X is the same group as group ID A reported from device Y. The only reliable way is to check with the RTPG output, as that contains all device identifiers for the defined group IDs.
But: caching RTPG output is problematic (as it'll change whenever a path state change happens), and it'll need to contain references to the SCSI devices, introducing all sorts of locking issues and race conditions.
So probably I would not go down that way (at least initially), but rather read RTPG during scanning, and set the values directly in the scsi device.

We then need to re-read that information whenever we hit a relevant sense code, but arguably we'll need to do that anyway.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                               +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
author
#11 lore raw

Re: [PATCH 5/8] scsi: scsi-multipath: Add basic ALUA support

Quoted 28 lines · Hannes Reinecke
Quoted 7 lines

If yes, to repeat, it is hard to separate the DH stuff out...but I can try. Examples I would need to deal with (and associated handling):

- alua_port_group members like dh_list
- alua_dh_data memebers like init_error
- everything in alua_queue_data

While the port group handling looks nice (and there certainly is a certain neatness to it), it kinda assumes too much about the internal layout of the hierarchy within the target. Technically, a target is only required to provide a device identifier, and a group id (such that you can match with RTPG output). However, you have no idea which of the various device IDs are part of the same enclosure; that information is not required to be present.
So you cannot assume that group ID A reported from device X is the same group as group ID A reported from device Y. The only reliable way is to check with the RTPG output, as that contains all device identifiers for the defined group IDs.
But: caching RTPG output is problematic (as it'll change whenever a path state change happens), and it'll need to contain references to the SCSI devices, introducing all sorts of locking issues and race conditions.
So probably I would not go down that way (at least initially), but rather read RTPG during scanning, and set the values directly in the scsi device.

Quoted 3 lines

We then need to re-read that information whenever we hit a relevant sense code, but arguably we'll need to do that anyway.

Hmmm... what you are describing seems to be now more like what I have in this series except only I did not have the ALUA info stored in the scsi_device struct.

So if I go down these lines, maybe for sense-triggered updates I can have something where scsi-multipath.c or scsi_dh_alua.c can trigger the RTPG to be issued and this updates scsi_device ALUA info. It sounds simple enough, but probably isn't for scsi_dh_alua.c :)

Thanks!

#12 lore raw

Re: [PATCH 5/8] scsi: scsi-multipath: Add basic ALUA support

In reply to John Garry
Quoted 135 lines · John Garry

Add basic support just to get the per-port group state.

This support does not account of state transitioning, sdev port group reconfiguration, etc, required for full support.

libmultipath callbacks scsi_mpath_is_optimized() and scsi_mpath_is_disabled() are updated to take account of the ALUA-provided path information.

As before, for no ALUA support (and scsi_multipath_always on) we assume that the paths are all optimized.

Much of this code in scsi_mpath_alua_init() is copied from scsi_dh_alua.c, originally authored by Hannes Reinecke.

---
 drivers/scsi/scsi_multipath.c | 163 ++++++++++++++++++++++++++++++++--
 include/scsi/scsi_multipath.h |   3 +
 2 files changed, 160 insertions(+), 6 deletions(-)
drivers/scsi/scsi_multipath.c +72 -3
index 1489c7e979167..0a314080bf0a5 100644
@@ -4,6 +4,7 @@
  *
  */
 
+#include <scsi/scsi_alua.h>
 #include <scsi/scsi_cmnd.h>
 #include <scsi/scsi_driver.h>
 #include <scsi/scsi_proto.h>
@@ -346,18 +347,29 @@ static bool scsi_mpath_is_disabled(struct mpath_device *mpath_device)
 				to_scsi_mpath_device(mpath_device);
 	struct scsi_device *sdev = scsi_mpath_dev->sdev;
 	enum scsi_device_state sdev_state = sdev->sdev_state;
+	int alua_state = scsi_mpath_dev->alua_state;
 
 	if (sdev_state == SDEV_RUNNING || sdev_state == SDEV_CANCEL)
 		return false;
 
-	return true;
+	if (alua_state == SCSI_ACCESS_STATE_OPTIMAL ||
+	    alua_state == SCSI_ACCESS_STATE_ACTIVE)
+		return true;
+
+	return false;
 }
 
 static bool scsi_mpath_is_optimized(struct mpath_device *mpath_device)
 {
+	struct scsi_mpath_device *scsi_mpath_dev =
+				to_scsi_mpath_device(mpath_device);
+
 	if (scsi_mpath_is_disabled(mpath_device))
 		return false;
-	return true;
+	if (scsi_mpath_dev->alua_state == SCSI_ACCESS_STATE_OPTIMAL)
+		return true;
+	return false;
+
 }
 
 /* Until we have ALUA support, we're always optimised */
@@ -366,7 +378,7 @@ static enum mpath_access_state scsi_mpath_get_access_state(
 {
 	if (scsi_mpath_is_disabled(mpath_device))
 		return MPATH_STATE_INVALID;
-	return MPATH_STATE_OPTIMIZED;
+	return scsi_mpath_is_optimized(mpath_device);
 }
 
 static bool scsi_mpath_available_path(struct mpath_device *mpath_device, bool *available)
@@ -579,16 +591,147 @@ static void scsi_multipath_sdev_uninit(struct scsi_device *sdev)
 	sdev->scsi_mpath_dev = NULL;
 }
 
+static int scsi_mpath_alua_init(struct scsi_device *sdev)
+{
+	struct scsi_mpath_device *scsi_mpath_dev = sdev->scsi_mpath_dev;
+	struct scsi_sense_hdr sense_hdr;
+	int len, k, off, bufflen = ALUA_RTPG_SIZE;
+	unsigned char *desc, *buff;
+	unsigned int tpg_desc_tbl_off;
+	int group_id, rel_port = -1;
+	bool ext_hdr_unsupp = false;
+	int ret;
+
+	group_id = scsi_vpd_tpg_id(sdev, &rel_port);
+	if (group_id < 0) {
+		/*
+		 * Internal error; TPGS supported but required
+		 * VPD identification descriptors not present.
+		 * Disable ALUA support.
+		 */
+		sdev_printk(KERN_INFO, sdev,
+			    "%s: No target port descriptors found\n",
+			    __func__);
+		return -EIO;
+	}
+
+	buff = kzalloc(bufflen, GFP_KERNEL);
+	if (!buff)
+		return -ENOMEM;
+ retry:
+	ret = submit_rtpg(sdev, buff, bufflen, &sense_hdr,
+				ext_hdr_unsupp);
+
+	if (ret) {
+		if (ret < 0 || !scsi_sense_valid(&sense_hdr)) {
+			sdev_printk(KERN_INFO, sdev,
+				    "%s: rtpg failed, result %d\n",
+				    __func__, ret);
+			kfree(buff);
+			if (ret < 0)
+				return -EBUSY;
+			if (host_byte(ret) == DID_NO_CONNECT)
+				return -ENODEV;
+			return -EIO;
+		}
+
+		/*
+		 * submit_rtpg() has failed on existing arrays
+		 * when requesting extended header info, and
+		 * the array doesn't support extended headers,
+		 * even though it shouldn't according to T10.
+		 * The retry without rtpg_ext_hdr_req set
+		 * handles this.
+		 * Note:  some arrays return a sense key of ILLEGAL_REQUEST
+		 * with ASC 00h if they don't support the extended header.
+		 */
+		if (ext_hdr_unsupp &&
+		    sense_hdr.sense_key == ILLEGAL_REQUEST) {
+			ext_hdr_unsupp = true;

drivers/scsi/scsi_multipath.c static void scsi_multipath_sdev_uninit(struct scsi_device *sdev) · line 660

Quoted 10 lines
drivers/scsi/scsi_multipath.c +10 -0
+			goto retry;
+		}
+		/*
+		 * If the array returns with 'ALUA state transition'
+		 * sense code here it cannot return RTPG data during
+		 * transition. So set the state to 'transitioning' directly.
+		 */
+		if (sense_hdr.sense_key == NOT_READY &&
+		    sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x0a)
+			goto out;

This check is odd. First, we don't set the state to 'transitioning' like the comment says. We don't set alua_state at all, which ends up meaning that it stays as 0 (SCSI_ACCESS_STATE_OPTIMAL). It seems like we should explicitly set it and make the comment reflect that, if just to aid understanding of the logic.

Nitpick: Also, this is the only place where we goto out. All the other checks individually free the buffer and return directly. I realize that all the other checks that exit early are errors, but it seems like we could just return a variable at the end of the function.

drivers/scsi/scsi_multipath.c static void scsi_multipath_sdev_uninit(struct scsi_device *sdev) · line 669

Quoted 9 lines
drivers/scsi/scsi_multipath.c +9 -0
+
+		/*
+		 * Retry on any other UNIT ATTENTION occurred.
+		 */
+		if (sense_hdr.sense_key == UNIT_ATTENTION) {
+			scsi_print_sense_hdr(sdev, __func__, &sense_hdr);
+			kfree(buff);
+			return -EAGAIN;
+		}

If we get a UNIT ATTENTION, we end up failing scsi_mpath_dev_alloc(), not retrying. Aside from the comment being wrong, it seems very brittle to fail here, just because we got a UNIT ATTENTION.

-Ben

Quoted 102 lines
drivers/scsi/scsi_multipath.c +66 -3
+		sdev_printk(KERN_ERR, sdev, "%s: rtpg failed\n",
+			    __func__);
+		scsi_print_sense_hdr(sdev, __func__, &sense_hdr);
+		kfree(buff);
+		return -EIO;
+	}
+
+	len = get_unaligned_be32(&buff[0]) + 4;
+
+	if (len > bufflen) {
+		/* Resubmit with the correct length */
+		kfree(buff);
+		bufflen = len;
+		buff = kmalloc(bufflen, GFP_KERNEL);
+		if (!buff) {
+			/* Temporary failure, bypass */
+			return -EBUSY;
+		}
+		goto retry;
+	}
+
+	if ((buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR)
+		tpg_desc_tbl_off = 8;
+	else
+		tpg_desc_tbl_off = 4;
+
+	for (k = tpg_desc_tbl_off, desc = buff + tpg_desc_tbl_off;
+	     k < len;
+	     k += off, desc += off) {
+		u16 group_id_found = get_unaligned_be16(&desc[2]);
+
+		if (group_id_found == group_id) {
+			int valid_states, state, pref;
+
+			state = desc[0] & 0x0f;
+			pref = desc[0] >> 7;
+			valid_states = desc[1];
+
+			alua_print_info(sdev, group_id, state, pref, valid_states);
+
+			scsi_mpath_dev->alua_state = state;
+			scsi_mpath_dev->alua_pref = pref;
+			scsi_mpath_dev->alua_valid_states = valid_states;
+			goto out;
+		}
+
+		off = 8 + (desc[7] * 4);
+	}
+
+out:
+	kfree(buff);
+	return 0;
+}
+
 int scsi_mpath_dev_alloc(struct scsi_device *sdev)
 {
 	struct scsi_mpath_head *scsi_mpath_head;
-	int ret;
+	int ret, tpgs;
 
 	if (!scsi_multipath)
 		return 0;
 
-	if (!scsi_device_tpgs(sdev) && !scsi_multipath_always) {
-		sdev_printk(KERN_NOTICE, sdev, "tpgs are required for multipath support\n");
+	tpgs = alua_check_tpgs(sdev);
+	if (!(tpgs & TPGS_MODE_IMPLICIT) && !scsi_multipath_always) {
+		sdev_printk(KERN_DEBUG, sdev, "IMPLICIT TPGS are required for multipath support\n");
 		return 0;
 	}
 
@@ -622,6 +765,14 @@ int scsi_mpath_dev_alloc(struct scsi_device *sdev)
 	sdev->scsi_mpath_dev->scsi_mpath_head = scsi_mpath_head;
 
 found:
+	if (tpgs & TPGS_MODE_IMPLICIT) {
+		ret = scsi_mpath_alua_init(sdev);
+		if (ret)
+			goto out_put_head;
+	} else {
+		sdev->scsi_mpath_dev->alua_state = SCSI_ACCESS_STATE_OPTIMAL;
+	}
+
 	sdev->scsi_mpath_dev->index = ida_alloc(&scsi_mpath_head->ida, GFP_KERNEL);
 	if (sdev->scsi_mpath_dev->index < 0) {
 		ret = sdev->scsi_mpath_dev->index;
include/scsi/scsi_multipath.h +3 -0
index 2011447f482d6..7c7ee2fb7def7 100644
@@ -38,6 +38,9 @@ struct scsi_mpath_device {
 	int			index;
 	atomic_t		nr_active;
 	struct scsi_mpath_head	*scsi_mpath_head;
+	int			alua_state;
+	int			alua_pref;
+	int			alua_valid_states;
 
 	char			device_id_str[SCSI_MPATH_DEVICE_ID_LEN];
 };
Signature · 1 line
2.43.5
author
#13 lore raw

Re: [PATCH 5/8] scsi: scsi-multipath: Add basic ALUA support

Quoted 8 lines · Benjamin Marzinski
Quoted 8 lines
drivers/scsi/scsi_multipath.c +8 -0
+			ext_hdr_unsupp = true;
+			goto retry;
+		}
+		/*
+		 * If the array returns with 'ALUA state transition'
+		 * sense code here it cannot return RTPG data during
+		 * transition. So set the state to 'transitioning' directly.
+		 */
Quoted 8 lines

drivers/scsi/scsi_multipath.c static void scsi_multipath_sdev_uninit(struct scsi_device *sdev) · line 660

Quoted 3 lines
drivers/scsi/scsi_multipath.c +3 -0
+		if (sense_hdr.sense_key == NOT_READY &&
+		    sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x0a)
+			goto out;

This check is odd. First, we don't set the state to 'transitioning' like the comment says. We don't set alua_state at all, which ends up meaning that it stays as 0 (SCSI_ACCESS_STATE_OPTIMAL). It seems like we should explicitly set it and make the comment reflect that, if just to aid understanding of the logic.

Yeah, I have to admit that this is all bodged the a bit, as transitioning is not properly handled. Instead of "basic" ALUA support, this really is limited ALUA support.

I am now looking at a way to have a core scsi ALUA driver to handle all of this, but it is challenging as we need to continue ALUA DH support.

Quoted 5 lines

Nitpick: Also, this is the only place where we goto out. All the other checks individually free the buffer and return directly. I realize that all the other checks that exit early are errors, but it seems like we could just return a variable at the end of the function.

Sure, I can pay attention to this. However, as mentioned above, I am experimenting with moving any ALUA specifics into scsi core code.

Quoted 13 lines

drivers/scsi/scsi_multipath.c static void scsi_multipath_sdev_uninit(struct scsi_device *sdev) · line 669

Quoted 10 lines
drivers/scsi/scsi_multipath.c +9 -0
+
+		/*
+		 * Retry on any other UNIT ATTENTION occurred.
+		 */
+		if (sense_hdr.sense_key == UNIT_ATTENTION) {
+			scsi_print_sense_hdr(sdev, __func__, &sense_hdr);
+			kfree(buff);
+			return -EAGAIN;
+		}

If we get a UNIT ATTENTION, we end up failing scsi_mpath_dev_alloc(), not retrying. Aside from the comment being wrong, it seems very brittle to fail here, just because we got a UNIT ATTENTION.

Ack

Thanks,
John

author
#14 lore raw

PATCH 6/8 scsi: scsi-multipath: Maintain sdev->access_state

In reply to John Garry

Now that ALUA is supported, we can maintain sdev->access_state.

However, preferred_path is still not maintained as that that is related to transitioning state and we do not yet support that (for SCSI multipath).

---
 drivers/scsi/scsi_multipath.c | 1 +
 drivers/scsi/scsi_sysfs.c     | 2 +-
 2 files changed, 2 insertions(+), 1 deletion(-)
drivers/scsi/scsi_multipath.c +1 -0
index 0a314080bf0a5..0c34b1151f5bf 100644
@@ -772,6 +772,7 @@ int scsi_mpath_dev_alloc(struct scsi_device *sdev)
 	} else {
 		sdev->scsi_mpath_dev->alua_state = SCSI_ACCESS_STATE_OPTIMAL;
 	}
+	sdev->access_state = sdev->scsi_mpath_dev->alua_state;
 
 	sdev->scsi_mpath_dev->index = ida_alloc(&scsi_mpath_head->ida, GFP_KERNEL);
 	if (sdev->scsi_mpath_dev->index < 0) {
drivers/scsi/scsi_sysfs.c +1 -1
index 3b03ee00c8df3..e4fbf08e05f4f 100644
@@ -1113,7 +1113,7 @@ sdev_show_access_state(struct device *dev,
 	unsigned char access_state;
 	const char *access_state_name;
 
-	if (!sdev->handler)
+	if (!sdev->handler && !sdev->scsi_mpath_dev)
 		return -EINVAL;
 
 	access_state = (sdev->access_state & SCSI_ACCESS_STATE_MASK);
Signature · 1 line
2.43.5
#15 lore raw

Re: [PATCH 6/8] scsi: scsi-multipath: Maintain sdev->access_state

Quoted 6 lines · John Garry

Now that ALUA is supported, we can maintain sdev->access_state.

However, preferred_path is still not maintained as that that is related to transitioning state and we do not yet support that (for SCSI multipath).

There is an issue with the preferred path in general, namely that it overlays the ALUA states (ie you can have 'acive/non-optimized' _and_ the preferred path bit set). So it only makes sense for explicit ALUA
as then the preferred path bit gives us an indicator that we might /
should switch paths.

If we restrict ourselves to implicit ALUA (which I'm advocating anyway for scsi-multipath) the preferred path becomes rather pointless as we cannot influence path selection at all.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                               +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
author
#16 lore raw

Re: [PATCH 6/8] scsi: scsi-multipath: Maintain sdev->access_state

Quoted 4 lines · Hannes Reinecke
Quoted 4 lines

However, preferred_path is still not maintained as that that is related to transitioning  state and we do not yet support that (for SCSI multipath).

Quoted 8 lines

There is an issue with the preferred path in general, namely that it overlays the ALUA states (ie you can have 'acive/non-optimized' _and_ the preferred path bit set). So it only makes sense for explicit ALUA
as then the preferred path bit gives us an indicator that we might /
should switch paths.

If we restrict ourselves to implicit ALUA (which I'm advocating anyway for scsi-multipath) the preferred path becomes rather pointless as we cannot influence path selection at all.

ok, fine, I won't touch for native scsi multipath.

Thanks

author
#17 lore raw

PATCH 7/8 scsi: scsi-multipath: Issue a periodic TUR per path

In reply to John Garry

To allow the initiator know of any ALUA configuration changes, issue a periodic TUR.

multipathd does something similar for dm-multipath in terms of issuing a periodic read per path.

The purpose of the TUR is that the target can update UA info in the TUR response and the INI can handle it, but currently we don't for SCSI multipath.

---
 drivers/scsi/scsi_multipath.c | 36 +++++++++++++++++++++++++++++++++++
 include/scsi/scsi_multipath.h |  1 +
 2 files changed, 37 insertions(+)
drivers/scsi/scsi_multipath.c +36 -0
index 0c34b1151f5bf..2b916c7af4bd7 100644
@@ -4,6 +4,7 @@
  *
  */
 
+#include <linux/kthread.h>
 #include <scsi/scsi_alua.h>
 #include <scsi/scsi_cmnd.h>
 #include <scsi/scsi_driver.h>
@@ -124,6 +125,7 @@ static void scsi_mpath_head_release(struct device *dev)
 		container_of(dev, struct scsi_mpath_head, dev);
 	struct mpath_head *mpath_head = scsi_mpath_head->mpath_head;
 
+	WARN_ON_ONCE(kthread_stop(scsi_mpath_head->kua));
 	scsi_mpath_delete_head(scsi_mpath_head);
 	bioset_exit(&scsi_mpath_head->bio_pool);
 	ida_free(&scsi_multipath_dev_ida, scsi_mpath_head->index);
@@ -514,6 +516,29 @@ struct mpath_head_template smpdt_pr = {
 	.device_groups = mpath_device_groups,
 };
 
+static void scsi_mpath_cb_ua_thread(struct mpath_device *mpath_device)
+{
+	struct scsi_mpath_device *scsi_mpath_dev =
+			to_scsi_mpath_device(mpath_device);
+
+	if (alua_tur(scsi_mpath_dev->sdev))
+		sdev_printk(KERN_NOTICE, scsi_mpath_dev->sdev,
+			    "%s: No target port descriptors found\n",
+			    __func__);
+}
+
+static int scsi_mpath_ua_thread(void *data)
+{
+	struct scsi_mpath_head *scsi_mpath_head = data;
+
+	while (!kthread_should_stop()) {
+		mpath_call_for_all_devices(scsi_mpath_head->mpath_head,
+			scsi_mpath_cb_ua_thread);
+		msleep(5000);
+	}
+	return 0;
+}
+
 static struct scsi_mpath_head *scsi_mpath_alloc_head(void)
 {
 	struct scsi_mpath_head *scsi_mpath_head;
@@ -548,6 +573,17 @@ static struct scsi_mpath_head *scsi_mpath_alloc_head(void)
 		goto out_free_ida;
 	}
 
+	scsi_mpath_head->kua = kthread_create(scsi_mpath_ua_thread,
+			scsi_mpath_head, "scsi-multipath-kua-%d",
+			scsi_mpath_head->index);
+	if (IS_ERR(scsi_mpath_head->kua)) {
+		put_device(&scsi_mpath_head->dev);
+		goto out_free_ida;
+	}
+
+	set_user_nice(scsi_mpath_head->kua, 10);
+	wake_up_process(scsi_mpath_head->kua);
+
 	return scsi_mpath_head;
 
 out_free_ida:
include/scsi/scsi_multipath.h +1 -0
index 7c7ee2fb7def7..d30f2c41e17de 100644
@@ -30,6 +30,7 @@ struct scsi_mpath_head {
 	struct mpath_head	*mpath_head;
 	struct device		dev;
 	int			index;
+	struct task_struct	*kua;
 };
 
 struct scsi_mpath_device {
Signature · 1 line
2.43.5
#18 lore raw

Re: [PATCH 7/8] scsi: scsi-multipath: Issue a periodic TUR per path

Quoted 89 lines · John Garry

To allow the initiator know of any ALUA configuration changes, issue a periodic TUR.

multipathd does something similar for dm-multipath in terms of issuing a periodic read per path.

The purpose of the TUR is that the target can update UA info in the TUR response and the INI can handle it, but currently we don't for SCSI multipath.

---
  drivers/scsi/scsi_multipath.c | 36 +++++++++++++++++++++++++++++++++++
  include/scsi/scsi_multipath.h |  1 +
  2 files changed, 37 insertions(+)
drivers/scsi/scsi_multipath.c +36 -0
index 0c34b1151f5bf..2b916c7af4bd7 100644
@@ -4,6 +4,7 @@
   *
   */
 
+#include <linux/kthread.h>
  #include <scsi/scsi_alua.h>
  #include <scsi/scsi_cmnd.h>
  #include <scsi/scsi_driver.h>
@@ -124,6 +125,7 @@ static void scsi_mpath_head_release(struct device *dev)
  		container_of(dev, struct scsi_mpath_head, dev);
  	struct mpath_head *mpath_head = scsi_mpath_head->mpath_head;
 
+	WARN_ON_ONCE(kthread_stop(scsi_mpath_head->kua));
  	scsi_mpath_delete_head(scsi_mpath_head);
  	bioset_exit(&scsi_mpath_head->bio_pool);
  	ida_free(&scsi_multipath_dev_ida, scsi_mpath_head->index);
@@ -514,6 +516,29 @@ struct mpath_head_template smpdt_pr = {
  	.device_groups = mpath_device_groups,
  };
 
+static void scsi_mpath_cb_ua_thread(struct mpath_device *mpath_device)
+{
+	struct scsi_mpath_device *scsi_mpath_dev =
+			to_scsi_mpath_device(mpath_device);
+
+	if (alua_tur(scsi_mpath_dev->sdev))
+		sdev_printk(KERN_NOTICE, scsi_mpath_dev->sdev,
+			    "%s: No target port descriptors found\n",
+			    __func__);
+}
+
+static int scsi_mpath_ua_thread(void *data)
+{
+	struct scsi_mpath_head *scsi_mpath_head = data;
+
+	while (!kthread_should_stop()) {
+		mpath_call_for_all_devices(scsi_mpath_head->mpath_head,
+			scsi_mpath_cb_ua_thread);
+		msleep(5000);
+	}
+	return 0;
+}
+
  static struct scsi_mpath_head *scsi_mpath_alloc_head(void)
  {
  	struct scsi_mpath_head *scsi_mpath_head;
@@ -548,6 +573,17 @@ static struct scsi_mpath_head *scsi_mpath_alloc_head(void)
  		goto out_free_ida;
  	}
 
+	scsi_mpath_head->kua = kthread_create(scsi_mpath_ua_thread,
+			scsi_mpath_head, "scsi-multipath-kua-%d",
+			scsi_mpath_head->index);
+	if (IS_ERR(scsi_mpath_head->kua)) {
+		put_device(&scsi_mpath_head->dev);
+		goto out_free_ida;
+	}
+
+	set_user_nice(scsi_mpath_head->kua, 10);
+	wake_up_process(scsi_mpath_head->kua);
+
  	return scsi_mpath_head;
 
  out_free_ida:
include/scsi/scsi_multipath.h +0 -0
index 7c7ee2fb7def7..d30f2c41e17de 100644

include/scsi/scsi_multipath.h struct scsi_mpath_head { · line 36

Quoted 7 lines
include/scsi/scsi_multipath.h +1 -0
@@ -30,6 +30,7 @@ struct scsi_mpath_head {
  	struct mpath_head	*mpath_head;
  	struct device		dev;
  	int			index;
+	struct task_struct	*kua;
  };
 
  struct scsi_mpath_device {

Please, don't. We should _not_ go into the business of doing TUR path checkers. Path checkers turned out to be a major issue for multipathing, and are mostly pointless for things like FC where you get reliable path information via RSCNs.
Additionally I would advocate for scsi-multipath to be a _simple_ implementation, restricting to the most common scenarios. Namely implicit ALUA only and reliable fabric notifications.
If you have anything else, fine, use dm-multipath.

Cheers,

Hannes

Signature · 4 lines
Dr. Hannes Reinecke                  Kernel Storage Architect
[email protected]                               +49 911 74053 688
SUSE Software Solutions GmbH, Frankenstr. 146, 90461 Nürnberg
HRB 36809 (AG Nürnberg), GF: I. Totev, A. McDonald, W. Knoblich
author
#19 lore raw

Re: [PATCH 7/8] scsi: scsi-multipath: Issue a periodic TUR per path

Quoted 6 lines · Hannes Reinecke
Quoted 5 lines

      struct device        dev;
      int            index;
+    struct task_struct    *kua;
  };
  struct scsi_mpath_device {

Quoted 6 lines

Please, don't. We should _not_ go into the business of doing TUR path checkers. Path checkers turned out to be a major issue for multipathing, and are mostly pointless for things like FC where you get reliable path information via RSCNs.
Additionally I would advocate for scsi-multipath to be a _simple_ implementation, restricting to the most common scenarios.

Sure,

Quoted 3 lines

Namely
implicit ALUA only and reliable fabric notifications. If you have anything else, fine, use dm-multipath.

ok, fine

I was just testing with linux target for this, which uses this in-band method AFAICS

Cheers

author
#20 lore raw

PATCH 8/8 scsi: scsi-multipath: Add stubbed scsi_multipath_dev_rescan()

In reply to John Garry

Add an empty function scsi_multipath_dev_rescan() to handle sdev rescans.

It should handle ALUA reconfiguration, and that will be possible when the core ALUA driver can handle that.

---
 drivers/scsi/scsi_multipath.c | 6 ++++++
 drivers/scsi/scsi_scan.c      | 2 ++
 include/scsi/scsi_multipath.h | 4 ++++
 3 files changed, 12 insertions(+)
drivers/scsi/scsi_multipath.c +6 -0
index 2b916c7af4bd7..97d835f1b6aaf 100644
@@ -539,6 +539,12 @@ static int scsi_mpath_ua_thread(void *data)
 	return 0;
 }
 
+void scsi_multipath_dev_rescan(struct scsi_device *sdev)
+{
+	/* Handle ALUA reconfig */
+	dev_warn_once(&sdev->sdev_gendev, "mulitpath rescan not handled\n");
+}
+
 static struct scsi_mpath_head *scsi_mpath_alloc_head(void)
 {
 	struct scsi_mpath_head *scsi_mpath_head;
drivers/scsi/scsi_scan.c +2 -0
index e22d3245d4b65..bf602daeac1db 100644
@@ -1743,6 +1743,8 @@ int scsi_rescan_device(struct scsi_device *sdev)
 
 	if (sdev->handler && sdev->handler->rescan)
 		sdev->handler->rescan(sdev);
+	else if (sdev->scsi_mpath_dev)
+		scsi_multipath_dev_rescan(sdev);
 
 	if (dev->driver && try_module_get(dev->driver->owner)) {
 		struct scsi_driver *drv = to_scsi_driver(dev->driver);
include/scsi/scsi_multipath.h +4 -0
index d30f2c41e17de..9dc02a56e6342 100644
@@ -78,6 +78,7 @@ void scsi_mpath_add_sysfs_link(struct scsi_device *sdev);
 void scsi_mpath_remove_sysfs_link(struct scsi_device *sdev);
 int scsi_mpath_get_head(struct scsi_mpath_head *);
 void scsi_mpath_put_head(struct scsi_mpath_head *);
+void scsi_multipath_dev_rescan(struct scsi_device *sdev);
 
 static inline void scsi_mpath_start_request(struct request *req)
 {
@@ -156,5 +157,8 @@ static inline void scsi_mpath_add_sysfs_link(struct scsi_device *sdev)
 static inline void scsi_mpath_remove_sysfs_link(struct scsi_device *sdev)
 {
 }
+static inline void scsi_multipath_dev_rescan(struct scsi_device *sdev)
+{
+}
 #endif /* CONFIG_SCSI_MULTIPATH */
 #endif /* _SCSI_SCSI_MULTIPATH_H */
Signature · 1 line
2.43.5