Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Generic SCSI-3 ALUA SCSI Device Handler |
| 3 | * |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 4 | * Copyright (C) 2007-2010 Hannes Reinecke, SUSE Linux Products GmbH. |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 5 | * All rights reserved. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 20 | * |
| 21 | */ |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 23 | #include <linux/delay.h> |
Paul Gortmaker | acf3368f | 2011-05-27 09:47:43 -0400 | [diff] [blame] | 24 | #include <linux/module.h> |
Hannes Reinecke | a708977 | 2015-12-01 10:16:52 +0100 | [diff] [blame] | 25 | #include <asm/unaligned.h> |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 26 | #include <scsi/scsi.h> |
Hannes Reinecke | 80bd68d | 2015-12-01 10:16:47 +0100 | [diff] [blame] | 27 | #include <scsi/scsi_dbg.h> |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 28 | #include <scsi/scsi_eh.h> |
| 29 | #include <scsi/scsi_dh.h> |
| 30 | |
| 31 | #define ALUA_DH_NAME "alua" |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 32 | #define ALUA_DH_VER "1.3" |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 33 | |
| 34 | #define TPGS_STATE_OPTIMIZED 0x0 |
| 35 | #define TPGS_STATE_NONOPTIMIZED 0x1 |
| 36 | #define TPGS_STATE_STANDBY 0x2 |
| 37 | #define TPGS_STATE_UNAVAILABLE 0x3 |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 38 | #define TPGS_STATE_LBA_DEPENDENT 0x4 |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 39 | #define TPGS_STATE_OFFLINE 0xe |
| 40 | #define TPGS_STATE_TRANSITIONING 0xf |
| 41 | |
| 42 | #define TPGS_SUPPORT_NONE 0x00 |
| 43 | #define TPGS_SUPPORT_OPTIMIZED 0x01 |
| 44 | #define TPGS_SUPPORT_NONOPTIMIZED 0x02 |
| 45 | #define TPGS_SUPPORT_STANDBY 0x04 |
| 46 | #define TPGS_SUPPORT_UNAVAILABLE 0x08 |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 47 | #define TPGS_SUPPORT_LBA_DEPENDENT 0x10 |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 48 | #define TPGS_SUPPORT_OFFLINE 0x40 |
| 49 | #define TPGS_SUPPORT_TRANSITION 0x80 |
| 50 | |
Rob Evers | 3588c5a | 2012-05-18 14:08:54 -0400 | [diff] [blame] | 51 | #define RTPG_FMT_MASK 0x70 |
| 52 | #define RTPG_FMT_EXT_HDR 0x10 |
| 53 | |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 54 | #define TPGS_MODE_UNINITIALIZED -1 |
| 55 | #define TPGS_MODE_NONE 0x0 |
| 56 | #define TPGS_MODE_IMPLICIT 0x1 |
| 57 | #define TPGS_MODE_EXPLICIT 0x2 |
| 58 | |
Hannes Reinecke | c49c834 | 2016-02-19 09:17:03 +0100 | [diff] [blame] | 59 | #define ALUA_RTPG_SIZE 128 |
Rob Evers | 3588c5a | 2012-05-18 14:08:54 -0400 | [diff] [blame] | 60 | #define ALUA_FAILOVER_TIMEOUT 60 |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 61 | #define ALUA_FAILOVER_RETRIES 5 |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 62 | #define ALUA_RTPG_DELAY_MSECS 5 |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 63 | |
Hannes Reinecke | 6c4fc04 | 2015-12-01 10:16:51 +0100 | [diff] [blame] | 64 | /* device handler flags */ |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 65 | #define ALUA_OPTIMIZE_STPG 0x01 |
| 66 | #define ALUA_RTPG_EXT_HDR_UNSUPP 0x02 |
Hannes Reinecke | 00642a1 | 2016-02-19 09:17:11 +0100 | [diff] [blame^] | 67 | #define ALUA_SYNC_STPG 0x04 |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 68 | /* State machine flags */ |
| 69 | #define ALUA_PG_RUN_RTPG 0x10 |
| 70 | #define ALUA_PG_RUN_STPG 0x20 |
| 71 | #define ALUA_PG_RUNNING 0x40 |
Moger, Babu | 4335d09 | 2012-03-27 20:55:49 +0000 | [diff] [blame] | 72 | |
Hannes Reinecke | aa90f49 | 2016-02-19 09:17:08 +0100 | [diff] [blame] | 73 | static uint optimize_stpg; |
| 74 | module_param(optimize_stpg, uint, S_IRUGO|S_IWUSR); |
| 75 | MODULE_PARM_DESC(optimize_stpg, "Allow use of a non-optimized path, rather than sending a STPG, when implicit TPGS is supported (0=No,1=Yes). Default is 0."); |
| 76 | |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 77 | static LIST_HEAD(port_group_list); |
| 78 | static DEFINE_SPINLOCK(port_group_lock); |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 79 | static struct workqueue_struct *kaluad_wq; |
Hannes Reinecke | 00642a1 | 2016-02-19 09:17:11 +0100 | [diff] [blame^] | 80 | static struct workqueue_struct *kaluad_sync_wq; |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 81 | |
| 82 | struct alua_port_group { |
| 83 | struct kref kref; |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 84 | struct rcu_head rcu; |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 85 | struct list_head node; |
Hannes Reinecke | 0047220 | 2016-02-19 09:17:05 +0100 | [diff] [blame] | 86 | unsigned char device_id_str[256]; |
| 87 | int device_id_len; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 88 | int group_id; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 89 | int tpgs; |
| 90 | int state; |
Moger, Babu | dcd3a75 | 2012-03-27 20:56:08 +0000 | [diff] [blame] | 91 | int pref; |
Moger, Babu | 4335d09 | 2012-03-27 20:55:49 +0000 | [diff] [blame] | 92 | unsigned flags; /* used for optimizing STPG */ |
Rob Evers | 3588c5a | 2012-05-18 14:08:54 -0400 | [diff] [blame] | 93 | unsigned char transition_tmo; |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 94 | unsigned long expiry; |
| 95 | unsigned long interval; |
| 96 | struct delayed_work rtpg_work; |
| 97 | spinlock_t lock; |
| 98 | struct list_head rtpg_list; |
| 99 | struct scsi_device *rtpg_sdev; |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | struct alua_dh_data { |
| 103 | struct alua_port_group *pg; |
| 104 | int group_id; |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 105 | spinlock_t pg_lock; |
Chandra Seetharaman | 96e6586 | 2009-10-21 09:23:04 -0700 | [diff] [blame] | 106 | struct scsi_device *sdev; |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 107 | int init_error; |
| 108 | struct mutex init_mutex; |
| 109 | }; |
| 110 | |
| 111 | struct alua_queue_data { |
| 112 | struct list_head entry; |
Chandra Seetharaman | 96e6586 | 2009-10-21 09:23:04 -0700 | [diff] [blame] | 113 | activate_complete callback_fn; |
| 114 | void *callback_data; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | #define ALUA_POLICY_SWITCH_CURRENT 0 |
| 118 | #define ALUA_POLICY_SWITCH_ALL 1 |
| 119 | |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 120 | static void alua_rtpg_work(struct work_struct *work); |
| 121 | static void alua_rtpg_queue(struct alua_port_group *pg, |
| 122 | struct scsi_device *sdev, |
| 123 | struct alua_queue_data *qdata); |
Chandra Seetharaman | 96e6586 | 2009-10-21 09:23:04 -0700 | [diff] [blame] | 124 | |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 125 | static void release_port_group(struct kref *kref) |
| 126 | { |
| 127 | struct alua_port_group *pg; |
| 128 | |
| 129 | pg = container_of(kref, struct alua_port_group, kref); |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 130 | if (pg->rtpg_sdev) |
| 131 | flush_delayed_work(&pg->rtpg_work); |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 132 | spin_lock(&port_group_lock); |
| 133 | list_del(&pg->node); |
| 134 | spin_unlock(&port_group_lock); |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 135 | kfree_rcu(pg, rcu); |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 138 | /* |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 139 | * submit_rtpg - Issue a REPORT TARGET GROUP STATES command |
| 140 | * @sdev: sdev the command should be sent to |
| 141 | */ |
Hannes Reinecke | 40bb61a | 2016-02-19 09:17:02 +0100 | [diff] [blame] | 142 | static int submit_rtpg(struct scsi_device *sdev, unsigned char *buff, |
| 143 | int bufflen, struct scsi_sense_hdr *sshdr, int flags) |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 144 | { |
Hannes Reinecke | 40bb61a | 2016-02-19 09:17:02 +0100 | [diff] [blame] | 145 | u8 cdb[COMMAND_SIZE(MAINTENANCE_IN)]; |
| 146 | int req_flags = REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT | |
| 147 | REQ_FAILFAST_DRIVER; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 148 | |
| 149 | /* Prepare the command. */ |
Hannes Reinecke | 40bb61a | 2016-02-19 09:17:02 +0100 | [diff] [blame] | 150 | memset(cdb, 0x0, COMMAND_SIZE(MAINTENANCE_IN)); |
| 151 | cdb[0] = MAINTENANCE_IN; |
Hannes Reinecke | d42ae5f | 2016-02-19 09:16:58 +0100 | [diff] [blame] | 152 | if (!(flags & ALUA_RTPG_EXT_HDR_UNSUPP)) |
Hannes Reinecke | 40bb61a | 2016-02-19 09:17:02 +0100 | [diff] [blame] | 153 | cdb[1] = MI_REPORT_TARGET_PGS | MI_EXT_HDR_PARAM_FMT; |
Rob Evers | 8e67ce6 | 2012-05-18 14:08:55 -0400 | [diff] [blame] | 154 | else |
Hannes Reinecke | 40bb61a | 2016-02-19 09:17:02 +0100 | [diff] [blame] | 155 | cdb[1] = MI_REPORT_TARGET_PGS; |
| 156 | put_unaligned_be32(bufflen, &cdb[6]); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 157 | |
Hannes Reinecke | 40bb61a | 2016-02-19 09:17:02 +0100 | [diff] [blame] | 158 | return scsi_execute_req_flags(sdev, cdb, DMA_FROM_DEVICE, |
| 159 | buff, bufflen, sshdr, |
| 160 | ALUA_FAILOVER_TIMEOUT * HZ, |
| 161 | ALUA_FAILOVER_RETRIES, NULL, req_flags); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /* |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame] | 165 | * submit_stpg - Issue a SET TARGET PORT GROUP command |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 166 | * |
| 167 | * Currently we're only setting the current target port group state |
| 168 | * to 'active/optimized' and let the array firmware figure out |
| 169 | * the states of the remaining groups. |
| 170 | */ |
Hannes Reinecke | 40bb61a | 2016-02-19 09:17:02 +0100 | [diff] [blame] | 171 | static int submit_stpg(struct scsi_device *sdev, int group_id, |
| 172 | struct scsi_sense_hdr *sshdr) |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 173 | { |
Hannes Reinecke | 40bb61a | 2016-02-19 09:17:02 +0100 | [diff] [blame] | 174 | u8 cdb[COMMAND_SIZE(MAINTENANCE_OUT)]; |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame] | 175 | unsigned char stpg_data[8]; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 176 | int stpg_len = 8; |
Hannes Reinecke | 40bb61a | 2016-02-19 09:17:02 +0100 | [diff] [blame] | 177 | int req_flags = REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT | |
| 178 | REQ_FAILFAST_DRIVER; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 179 | |
| 180 | /* Prepare the data buffer */ |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame] | 181 | memset(stpg_data, 0, stpg_len); |
| 182 | stpg_data[4] = TPGS_STATE_OPTIMIZED & 0x0f; |
| 183 | put_unaligned_be16(group_id, &stpg_data[6]); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 184 | |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 185 | /* Prepare the command. */ |
Hannes Reinecke | 40bb61a | 2016-02-19 09:17:02 +0100 | [diff] [blame] | 186 | memset(cdb, 0x0, COMMAND_SIZE(MAINTENANCE_OUT)); |
| 187 | cdb[0] = MAINTENANCE_OUT; |
| 188 | cdb[1] = MO_SET_TARGET_PGS; |
| 189 | put_unaligned_be32(stpg_len, &cdb[6]); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 190 | |
Hannes Reinecke | 40bb61a | 2016-02-19 09:17:02 +0100 | [diff] [blame] | 191 | return scsi_execute_req_flags(sdev, cdb, DMA_TO_DEVICE, |
| 192 | stpg_data, stpg_len, |
| 193 | sshdr, ALUA_FAILOVER_TIMEOUT * HZ, |
| 194 | ALUA_FAILOVER_RETRIES, NULL, req_flags); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Hannes Reinecke | 0047220 | 2016-02-19 09:17:05 +0100 | [diff] [blame] | 197 | struct alua_port_group *alua_find_get_pg(char *id_str, size_t id_size, |
| 198 | int group_id) |
| 199 | { |
| 200 | struct alua_port_group *pg; |
| 201 | |
| 202 | list_for_each_entry(pg, &port_group_list, node) { |
| 203 | if (pg->group_id != group_id) |
| 204 | continue; |
| 205 | if (pg->device_id_len != id_size) |
| 206 | continue; |
| 207 | if (strncmp(pg->device_id_str, id_str, id_size)) |
| 208 | continue; |
| 209 | if (!kref_get_unless_zero(&pg->kref)) |
| 210 | continue; |
| 211 | return pg; |
| 212 | } |
| 213 | |
| 214 | return NULL; |
| 215 | } |
| 216 | |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 217 | /* |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 218 | * alua_alloc_pg - Allocate a new port_group structure |
| 219 | * @sdev: scsi device |
| 220 | * @h: alua device_handler data |
| 221 | * @group_id: port group id |
| 222 | * |
| 223 | * Allocate a new port_group structure for a given |
| 224 | * device. |
| 225 | */ |
| 226 | struct alua_port_group *alua_alloc_pg(struct scsi_device *sdev, |
| 227 | int group_id, int tpgs) |
| 228 | { |
Hannes Reinecke | 0047220 | 2016-02-19 09:17:05 +0100 | [diff] [blame] | 229 | struct alua_port_group *pg, *tmp_pg; |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 230 | |
| 231 | pg = kzalloc(sizeof(struct alua_port_group), GFP_KERNEL); |
| 232 | if (!pg) |
Hannes Reinecke | 0047220 | 2016-02-19 09:17:05 +0100 | [diff] [blame] | 233 | return ERR_PTR(-ENOMEM); |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 234 | |
Hannes Reinecke | 0047220 | 2016-02-19 09:17:05 +0100 | [diff] [blame] | 235 | pg->device_id_len = scsi_vpd_lun_id(sdev, pg->device_id_str, |
| 236 | sizeof(pg->device_id_str)); |
| 237 | if (pg->device_id_len <= 0) { |
| 238 | /* |
| 239 | * Internal error: TPGS supported but no device |
| 240 | * identifcation found. Disable ALUA support. |
| 241 | */ |
| 242 | kfree(pg); |
| 243 | sdev_printk(KERN_INFO, sdev, |
| 244 | "%s: No device descriptors found\n", |
| 245 | ALUA_DH_NAME); |
| 246 | return ERR_PTR(-ENXIO); |
| 247 | } |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 248 | pg->group_id = group_id; |
| 249 | pg->tpgs = tpgs; |
| 250 | pg->state = TPGS_STATE_OPTIMIZED; |
Hannes Reinecke | aa90f49 | 2016-02-19 09:17:08 +0100 | [diff] [blame] | 251 | if (optimize_stpg) |
| 252 | pg->flags |= ALUA_OPTIMIZE_STPG; |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 253 | kref_init(&pg->kref); |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 254 | INIT_DELAYED_WORK(&pg->rtpg_work, alua_rtpg_work); |
| 255 | INIT_LIST_HEAD(&pg->rtpg_list); |
| 256 | INIT_LIST_HEAD(&pg->node); |
| 257 | spin_lock_init(&pg->lock); |
Hannes Reinecke | 0047220 | 2016-02-19 09:17:05 +0100 | [diff] [blame] | 258 | |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 259 | spin_lock(&port_group_lock); |
Hannes Reinecke | 0047220 | 2016-02-19 09:17:05 +0100 | [diff] [blame] | 260 | tmp_pg = alua_find_get_pg(pg->device_id_str, pg->device_id_len, |
| 261 | group_id); |
| 262 | if (tmp_pg) { |
| 263 | spin_unlock(&port_group_lock); |
| 264 | kfree(pg); |
| 265 | return tmp_pg; |
| 266 | } |
| 267 | |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 268 | list_add(&pg->node, &port_group_list); |
| 269 | spin_unlock(&port_group_lock); |
| 270 | |
| 271 | return pg; |
| 272 | } |
| 273 | |
| 274 | /* |
Hannes Reinecke | d7c48fe | 2011-08-24 10:51:13 +0200 | [diff] [blame] | 275 | * alua_check_tpgs - Evaluate TPGS setting |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 276 | * @sdev: device to be checked |
| 277 | * |
Hannes Reinecke | d7c48fe | 2011-08-24 10:51:13 +0200 | [diff] [blame] | 278 | * Examine the TPGS setting of the sdev to find out if ALUA |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 279 | * is supported. |
| 280 | */ |
Hannes Reinecke | ad0ea64 | 2015-12-01 10:16:53 +0100 | [diff] [blame] | 281 | static int alua_check_tpgs(struct scsi_device *sdev) |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 282 | { |
Hannes Reinecke | ad0ea64 | 2015-12-01 10:16:53 +0100 | [diff] [blame] | 283 | int tpgs = TPGS_MODE_NONE; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 284 | |
Hannes Reinecke | db5a6a6 | 2015-12-01 10:16:43 +0100 | [diff] [blame] | 285 | /* |
| 286 | * ALUA support for non-disk devices is fraught with |
| 287 | * difficulties, so disable it for now. |
| 288 | */ |
| 289 | if (sdev->type != TYPE_DISK) { |
Hannes Reinecke | db5a6a6 | 2015-12-01 10:16:43 +0100 | [diff] [blame] | 290 | sdev_printk(KERN_INFO, sdev, |
| 291 | "%s: disable for non-disk devices\n", |
| 292 | ALUA_DH_NAME); |
Hannes Reinecke | ad0ea64 | 2015-12-01 10:16:53 +0100 | [diff] [blame] | 293 | return tpgs; |
Hannes Reinecke | db5a6a6 | 2015-12-01 10:16:43 +0100 | [diff] [blame] | 294 | } |
| 295 | |
Hannes Reinecke | ad0ea64 | 2015-12-01 10:16:53 +0100 | [diff] [blame] | 296 | tpgs = scsi_device_tpgs(sdev); |
| 297 | switch (tpgs) { |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 298 | case TPGS_MODE_EXPLICIT|TPGS_MODE_IMPLICIT: |
| 299 | sdev_printk(KERN_INFO, sdev, |
| 300 | "%s: supports implicit and explicit TPGS\n", |
| 301 | ALUA_DH_NAME); |
| 302 | break; |
| 303 | case TPGS_MODE_EXPLICIT: |
| 304 | sdev_printk(KERN_INFO, sdev, "%s: supports explicit TPGS\n", |
| 305 | ALUA_DH_NAME); |
| 306 | break; |
| 307 | case TPGS_MODE_IMPLICIT: |
| 308 | sdev_printk(KERN_INFO, sdev, "%s: supports implicit TPGS\n", |
| 309 | ALUA_DH_NAME); |
| 310 | break; |
Hannes Reinecke | 6cc05d4 | 2015-12-01 10:16:45 +0100 | [diff] [blame] | 311 | case TPGS_MODE_NONE: |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 312 | sdev_printk(KERN_INFO, sdev, "%s: not supported\n", |
| 313 | ALUA_DH_NAME); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 314 | break; |
Hannes Reinecke | 6cc05d4 | 2015-12-01 10:16:45 +0100 | [diff] [blame] | 315 | default: |
| 316 | sdev_printk(KERN_INFO, sdev, |
| 317 | "%s: unsupported TPGS setting %d\n", |
Hannes Reinecke | ad0ea64 | 2015-12-01 10:16:53 +0100 | [diff] [blame] | 318 | ALUA_DH_NAME, tpgs); |
| 319 | tpgs = TPGS_MODE_NONE; |
Hannes Reinecke | 6cc05d4 | 2015-12-01 10:16:45 +0100 | [diff] [blame] | 320 | break; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Hannes Reinecke | ad0ea64 | 2015-12-01 10:16:53 +0100 | [diff] [blame] | 323 | return tpgs; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /* |
Hannes Reinecke | 9b80dce | 2015-12-01 10:16:44 +0100 | [diff] [blame] | 327 | * alua_check_vpd - Evaluate INQUIRY vpd page 0x83 |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 328 | * @sdev: device to be checked |
| 329 | * |
| 330 | * Extract the relative target port and the target port group |
| 331 | * descriptor from the list of identificators. |
| 332 | */ |
Hannes Reinecke | a4253fd | 2016-02-19 09:17:06 +0100 | [diff] [blame] | 333 | static int alua_check_vpd(struct scsi_device *sdev, struct alua_dh_data *h, |
| 334 | int tpgs) |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 335 | { |
Hannes Reinecke | 83ea0e5 | 2015-12-01 10:16:58 +0100 | [diff] [blame] | 336 | int rel_port = -1, group_id; |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 337 | struct alua_port_group *pg, *old_pg = NULL; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 338 | |
Hannes Reinecke | 83ea0e5 | 2015-12-01 10:16:58 +0100 | [diff] [blame] | 339 | group_id = scsi_vpd_tpg_id(sdev, &rel_port); |
| 340 | if (group_id < 0) { |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 341 | /* |
| 342 | * Internal error; TPGS supported but required |
| 343 | * VPD identification descriptors not present. |
| 344 | * Disable ALUA support |
| 345 | */ |
| 346 | sdev_printk(KERN_INFO, sdev, |
| 347 | "%s: No target port descriptors found\n", |
| 348 | ALUA_DH_NAME); |
Hannes Reinecke | 9b80dce | 2015-12-01 10:16:44 +0100 | [diff] [blame] | 349 | return SCSI_DH_DEV_UNSUPP; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 350 | } |
Hannes Reinecke | a4253fd | 2016-02-19 09:17:06 +0100 | [diff] [blame] | 351 | |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 352 | pg = alua_alloc_pg(sdev, group_id, tpgs); |
| 353 | if (IS_ERR(pg)) { |
| 354 | if (PTR_ERR(pg) == -ENOMEM) |
Hannes Reinecke | a4253fd | 2016-02-19 09:17:06 +0100 | [diff] [blame] | 355 | return SCSI_DH_NOMEM; |
| 356 | return SCSI_DH_DEV_UNSUPP; |
| 357 | } |
Hannes Reinecke | 9b80dce | 2015-12-01 10:16:44 +0100 | [diff] [blame] | 358 | sdev_printk(KERN_INFO, sdev, |
Hannes Reinecke | a4253fd | 2016-02-19 09:17:06 +0100 | [diff] [blame] | 359 | "%s: device %s port group %x rel port %x\n", |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 360 | ALUA_DH_NAME, pg->device_id_str, group_id, rel_port); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 361 | |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 362 | /* Check for existing port group references */ |
| 363 | spin_lock(&h->pg_lock); |
| 364 | old_pg = h->pg; |
| 365 | if (old_pg != pg) { |
| 366 | /* port group has changed. Update to new port group */ |
| 367 | rcu_assign_pointer(h->pg, pg); |
| 368 | } |
| 369 | alua_rtpg_queue(h->pg, sdev, NULL); |
| 370 | spin_unlock(&h->pg_lock); |
| 371 | |
| 372 | if (old_pg) |
| 373 | kref_put(&old_pg->kref, release_port_group); |
| 374 | |
| 375 | return SCSI_DH_OK; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | static char print_alua_state(int state) |
| 379 | { |
| 380 | switch (state) { |
| 381 | case TPGS_STATE_OPTIMIZED: |
| 382 | return 'A'; |
| 383 | case TPGS_STATE_NONOPTIMIZED: |
| 384 | return 'N'; |
| 385 | case TPGS_STATE_STANDBY: |
| 386 | return 'S'; |
| 387 | case TPGS_STATE_UNAVAILABLE: |
| 388 | return 'U'; |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 389 | case TPGS_STATE_LBA_DEPENDENT: |
| 390 | return 'L'; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 391 | case TPGS_STATE_OFFLINE: |
| 392 | return 'O'; |
| 393 | case TPGS_STATE_TRANSITIONING: |
| 394 | return 'T'; |
| 395 | default: |
| 396 | return 'X'; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | static int alua_check_sense(struct scsi_device *sdev, |
| 401 | struct scsi_sense_hdr *sense_hdr) |
| 402 | { |
| 403 | switch (sense_hdr->sense_key) { |
| 404 | case NOT_READY: |
| 405 | if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0a) |
| 406 | /* |
| 407 | * LUN Not Accessible - ALUA state transition |
| 408 | */ |
Mike Anderson | c7dbb62 | 2008-08-12 12:11:57 -0700 | [diff] [blame] | 409 | return ADD_TO_MLQUEUE; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 410 | break; |
| 411 | case UNIT_ATTENTION: |
| 412 | if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00) |
| 413 | /* |
| 414 | * Power On, Reset, or Bus Device Reset, just retry. |
| 415 | */ |
Mike Anderson | c7dbb62 | 2008-08-12 12:11:57 -0700 | [diff] [blame] | 416 | return ADD_TO_MLQUEUE; |
Stewart, Sean | c20ee7b | 2013-10-15 15:52:39 +0000 | [diff] [blame] | 417 | if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x04) |
| 418 | /* |
| 419 | * Device internal reset |
| 420 | */ |
| 421 | return ADD_TO_MLQUEUE; |
Moger, Babu | 410f02d | 2011-12-21 18:01:37 -0500 | [diff] [blame] | 422 | if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x01) |
| 423 | /* |
| 424 | * Mode Parameters Changed |
| 425 | */ |
| 426 | return ADD_TO_MLQUEUE; |
Moger, Babu | bf81973 | 2011-10-06 13:22:07 -0400 | [diff] [blame] | 427 | if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06) |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 428 | /* |
| 429 | * ALUA state changed |
| 430 | */ |
Mike Anderson | c7dbb62 | 2008-08-12 12:11:57 -0700 | [diff] [blame] | 431 | return ADD_TO_MLQUEUE; |
Moger, Babu | bf81973 | 2011-10-06 13:22:07 -0400 | [diff] [blame] | 432 | if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07) |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 433 | /* |
| 434 | * Implicit ALUA state transition failed |
| 435 | */ |
Mike Anderson | c7dbb62 | 2008-08-12 12:11:57 -0700 | [diff] [blame] | 436 | return ADD_TO_MLQUEUE; |
Moger, Babu | bf81973 | 2011-10-06 13:22:07 -0400 | [diff] [blame] | 437 | if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x03) |
| 438 | /* |
| 439 | * Inquiry data has changed |
| 440 | */ |
| 441 | return ADD_TO_MLQUEUE; |
| 442 | if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x0e) |
Ilgu Hong | 4d086f6 | 2009-01-30 17:00:11 -0600 | [diff] [blame] | 443 | /* |
| 444 | * REPORTED_LUNS_DATA_HAS_CHANGED is reported |
| 445 | * when switching controllers on targets like |
| 446 | * Intel Multi-Flex. We can just retry. |
| 447 | */ |
| 448 | return ADD_TO_MLQUEUE; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 449 | break; |
| 450 | } |
| 451 | |
| 452 | return SCSI_RETURN_NOT_HANDLED; |
| 453 | } |
| 454 | |
| 455 | /* |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 456 | * alua_rtpg - Evaluate REPORT TARGET GROUP STATES |
| 457 | * @sdev: the device to be evaluated. |
| 458 | * |
| 459 | * Evaluate the Target Port Group State. |
| 460 | * Returns SCSI_DH_DEV_OFFLINED if the path is |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 461 | * found to be unusable. |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 462 | */ |
Hannes Reinecke | 2826140 | 2016-02-19 09:17:07 +0100 | [diff] [blame] | 463 | static int alua_rtpg(struct scsi_device *sdev, struct alua_port_group *pg) |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 464 | { |
| 465 | struct scsi_sense_hdr sense_hdr; |
Hannes Reinecke | c49c834 | 2016-02-19 09:17:03 +0100 | [diff] [blame] | 466 | int len, k, off, valid_states = 0, bufflen = ALUA_RTPG_SIZE; |
| 467 | unsigned char *ucp, *buff; |
Hannes Reinecke | 5597caf | 2015-12-01 10:16:48 +0100 | [diff] [blame] | 468 | unsigned err, retval; |
Rob Evers | 3588c5a | 2012-05-18 14:08:54 -0400 | [diff] [blame] | 469 | unsigned int tpg_desc_tbl_off; |
| 470 | unsigned char orig_transition_tmo; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 471 | |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 472 | if (!pg->expiry) { |
| 473 | unsigned long transition_tmo = ALUA_FAILOVER_TIMEOUT * HZ; |
| 474 | |
| 475 | if (pg->transition_tmo) |
| 476 | transition_tmo = pg->transition_tmo * HZ; |
| 477 | |
| 478 | pg->expiry = round_jiffies_up(jiffies + transition_tmo); |
| 479 | } |
Rob Evers | 3588c5a | 2012-05-18 14:08:54 -0400 | [diff] [blame] | 480 | |
Hannes Reinecke | c49c834 | 2016-02-19 09:17:03 +0100 | [diff] [blame] | 481 | buff = kzalloc(bufflen, GFP_KERNEL); |
| 482 | if (!buff) |
| 483 | return SCSI_DH_DEV_TEMP_BUSY; |
| 484 | |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 485 | retry: |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 486 | retval = submit_rtpg(sdev, buff, bufflen, &sense_hdr, pg->flags); |
Hannes Reinecke | 40bb61a | 2016-02-19 09:17:02 +0100 | [diff] [blame] | 487 | |
Hannes Reinecke | 5597caf | 2015-12-01 10:16:48 +0100 | [diff] [blame] | 488 | if (retval) { |
Hannes Reinecke | 40bb61a | 2016-02-19 09:17:02 +0100 | [diff] [blame] | 489 | if (!scsi_sense_valid(&sense_hdr)) { |
Hannes Reinecke | 5597caf | 2015-12-01 10:16:48 +0100 | [diff] [blame] | 490 | sdev_printk(KERN_INFO, sdev, |
| 491 | "%s: rtpg failed, result %d\n", |
| 492 | ALUA_DH_NAME, retval); |
Hannes Reinecke | c49c834 | 2016-02-19 09:17:03 +0100 | [diff] [blame] | 493 | kfree(buff); |
Hannes Reinecke | 40bb61a | 2016-02-19 09:17:02 +0100 | [diff] [blame] | 494 | if (driver_byte(retval) == DRIVER_ERROR) |
Hannes Reinecke | 5597caf | 2015-12-01 10:16:48 +0100 | [diff] [blame] | 495 | return SCSI_DH_DEV_TEMP_BUSY; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 496 | return SCSI_DH_IO; |
Hannes Reinecke | 5597caf | 2015-12-01 10:16:48 +0100 | [diff] [blame] | 497 | } |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 498 | |
Rob Evers | 8e67ce6 | 2012-05-18 14:08:55 -0400 | [diff] [blame] | 499 | /* |
| 500 | * submit_rtpg() has failed on existing arrays |
| 501 | * when requesting extended header info, and |
| 502 | * the array doesn't support extended headers, |
| 503 | * even though it shouldn't according to T10. |
| 504 | * The retry without rtpg_ext_hdr_req set |
| 505 | * handles this. |
| 506 | */ |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 507 | if (!(pg->flags & ALUA_RTPG_EXT_HDR_UNSUPP) && |
Rob Evers | 8e67ce6 | 2012-05-18 14:08:55 -0400 | [diff] [blame] | 508 | sense_hdr.sense_key == ILLEGAL_REQUEST && |
| 509 | sense_hdr.asc == 0x24 && sense_hdr.ascq == 0) { |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 510 | pg->flags |= ALUA_RTPG_EXT_HDR_UNSUPP; |
Rob Evers | 8e67ce6 | 2012-05-18 14:08:55 -0400 | [diff] [blame] | 511 | goto retry; |
| 512 | } |
Hannes Reinecke | e2d817d | 2015-12-01 10:16:54 +0100 | [diff] [blame] | 513 | /* |
| 514 | * Retry on ALUA state transition or if any |
| 515 | * UNIT ATTENTION occurred. |
| 516 | */ |
| 517 | if (sense_hdr.sense_key == NOT_READY && |
| 518 | sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x0a) |
| 519 | err = SCSI_DH_RETRY; |
| 520 | else if (sense_hdr.sense_key == UNIT_ATTENTION) |
| 521 | err = SCSI_DH_RETRY; |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 522 | if (err == SCSI_DH_RETRY && |
| 523 | pg->expiry != 0 && time_before(jiffies, pg->expiry)) { |
Hannes Reinecke | 80bd68d | 2015-12-01 10:16:47 +0100 | [diff] [blame] | 524 | sdev_printk(KERN_ERR, sdev, "%s: rtpg retry\n", |
| 525 | ALUA_DH_NAME); |
| 526 | scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr); |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 527 | return err; |
Hannes Reinecke | 80bd68d | 2015-12-01 10:16:47 +0100 | [diff] [blame] | 528 | } |
| 529 | sdev_printk(KERN_ERR, sdev, "%s: rtpg failed\n", |
| 530 | ALUA_DH_NAME); |
| 531 | scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr); |
Hannes Reinecke | c49c834 | 2016-02-19 09:17:03 +0100 | [diff] [blame] | 532 | kfree(buff); |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 533 | pg->expiry = 0; |
Hannes Reinecke | 80bd68d | 2015-12-01 10:16:47 +0100 | [diff] [blame] | 534 | return SCSI_DH_IO; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 535 | } |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 536 | |
Hannes Reinecke | c49c834 | 2016-02-19 09:17:03 +0100 | [diff] [blame] | 537 | len = get_unaligned_be32(&buff[0]) + 4; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 538 | |
Hannes Reinecke | c49c834 | 2016-02-19 09:17:03 +0100 | [diff] [blame] | 539 | if (len > bufflen) { |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 540 | /* Resubmit with the correct length */ |
Hannes Reinecke | c49c834 | 2016-02-19 09:17:03 +0100 | [diff] [blame] | 541 | kfree(buff); |
| 542 | bufflen = len; |
| 543 | buff = kmalloc(bufflen, GFP_KERNEL); |
| 544 | if (!buff) { |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 545 | sdev_printk(KERN_WARNING, sdev, |
Harvey Harrison | cadbd4a | 2008-07-03 23:47:27 -0700 | [diff] [blame] | 546 | "%s: kmalloc buffer failed\n",__func__); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 547 | /* Temporary failure, bypass */ |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 548 | pg->expiry = 0; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 549 | return SCSI_DH_DEV_TEMP_BUSY; |
| 550 | } |
| 551 | goto retry; |
| 552 | } |
| 553 | |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 554 | orig_transition_tmo = pg->transition_tmo; |
Hannes Reinecke | c49c834 | 2016-02-19 09:17:03 +0100 | [diff] [blame] | 555 | if ((buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR && buff[5] != 0) |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 556 | pg->transition_tmo = buff[5]; |
Rob Evers | 3588c5a | 2012-05-18 14:08:54 -0400 | [diff] [blame] | 557 | else |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 558 | pg->transition_tmo = ALUA_FAILOVER_TIMEOUT; |
Rob Evers | 3588c5a | 2012-05-18 14:08:54 -0400 | [diff] [blame] | 559 | |
Hannes Reinecke | 2826140 | 2016-02-19 09:17:07 +0100 | [diff] [blame] | 560 | if (orig_transition_tmo != pg->transition_tmo) { |
Rob Evers | 3588c5a | 2012-05-18 14:08:54 -0400 | [diff] [blame] | 561 | sdev_printk(KERN_INFO, sdev, |
| 562 | "%s: transition timeout set to %d seconds\n", |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 563 | ALUA_DH_NAME, pg->transition_tmo); |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 564 | pg->expiry = jiffies + pg->transition_tmo * HZ; |
Rob Evers | 3588c5a | 2012-05-18 14:08:54 -0400 | [diff] [blame] | 565 | } |
| 566 | |
Hannes Reinecke | c49c834 | 2016-02-19 09:17:03 +0100 | [diff] [blame] | 567 | if ((buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR) |
Rob Evers | 3588c5a | 2012-05-18 14:08:54 -0400 | [diff] [blame] | 568 | tpg_desc_tbl_off = 8; |
| 569 | else |
| 570 | tpg_desc_tbl_off = 4; |
| 571 | |
Hannes Reinecke | c49c834 | 2016-02-19 09:17:03 +0100 | [diff] [blame] | 572 | for (k = tpg_desc_tbl_off, ucp = buff + tpg_desc_tbl_off; |
Rob Evers | 3588c5a | 2012-05-18 14:08:54 -0400 | [diff] [blame] | 573 | k < len; |
| 574 | k += off, ucp += off) { |
| 575 | |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 576 | if (pg->group_id == get_unaligned_be16(&ucp[2])) { |
| 577 | pg->state = ucp[0] & 0x0f; |
| 578 | pg->pref = ucp[0] >> 7; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 579 | valid_states = ucp[1]; |
| 580 | } |
| 581 | off = 8 + (ucp[7] * 4); |
| 582 | } |
| 583 | |
| 584 | sdev_printk(KERN_INFO, sdev, |
Moger, Babu | dcd3a75 | 2012-03-27 20:56:08 +0000 | [diff] [blame] | 585 | "%s: port group %02x state %c %s supports %c%c%c%c%c%c%c\n", |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 586 | ALUA_DH_NAME, pg->group_id, print_alua_state(pg->state), |
| 587 | pg->pref ? "preferred" : "non-preferred", |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 588 | valid_states&TPGS_SUPPORT_TRANSITION?'T':'t', |
| 589 | valid_states&TPGS_SUPPORT_OFFLINE?'O':'o', |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 590 | valid_states&TPGS_SUPPORT_LBA_DEPENDENT?'L':'l', |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 591 | valid_states&TPGS_SUPPORT_UNAVAILABLE?'U':'u', |
| 592 | valid_states&TPGS_SUPPORT_STANDBY?'S':'s', |
| 593 | valid_states&TPGS_SUPPORT_NONOPTIMIZED?'N':'n', |
| 594 | valid_states&TPGS_SUPPORT_OPTIMIZED?'A':'a'); |
| 595 | |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 596 | switch (pg->state) { |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 597 | case TPGS_STATE_TRANSITIONING: |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 598 | if (time_before(jiffies, pg->expiry)) { |
Hannes Reinecke | 2826140 | 2016-02-19 09:17:07 +0100 | [diff] [blame] | 599 | /* State transition, retry */ |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 600 | pg->interval = 2; |
| 601 | err = SCSI_DH_RETRY; |
| 602 | } else { |
| 603 | /* Transitioning time exceeded, set port to standby */ |
| 604 | err = SCSI_DH_IO; |
| 605 | pg->state = TPGS_STATE_STANDBY; |
| 606 | pg->expiry = 0; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 607 | } |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 608 | break; |
| 609 | case TPGS_STATE_OFFLINE: |
Bart Van Assche | e47f897 | 2012-08-24 09:08:41 +0000 | [diff] [blame] | 610 | /* Path unusable */ |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 611 | err = SCSI_DH_DEV_OFFLINED; |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 612 | pg->expiry = 0; |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 613 | break; |
| 614 | default: |
| 615 | /* Useable path if active */ |
| 616 | err = SCSI_DH_OK; |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 617 | pg->expiry = 0; |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 618 | break; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 619 | } |
Hannes Reinecke | c49c834 | 2016-02-19 09:17:03 +0100 | [diff] [blame] | 620 | kfree(buff); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 621 | return err; |
| 622 | } |
| 623 | |
| 624 | /* |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 625 | * alua_stpg - Issue a SET TARGET PORT GROUP command |
| 626 | * |
| 627 | * Issue a SET TARGET PORT GROUP command and evaluate the |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame] | 628 | * response. Returns SCSI_DH_RETRY per default to trigger |
| 629 | * a re-evaluation of the target group state or SCSI_DH_OK |
| 630 | * if no further action needs to be taken. |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 631 | */ |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 632 | static unsigned alua_stpg(struct scsi_device *sdev, struct alua_port_group *pg) |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 633 | { |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame] | 634 | int retval; |
| 635 | struct scsi_sense_hdr sense_hdr; |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 636 | |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 637 | if (!(pg->tpgs & TPGS_MODE_EXPLICIT)) { |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame] | 638 | /* Only implicit ALUA supported, retry */ |
| 639 | return SCSI_DH_RETRY; |
| 640 | } |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 641 | switch (pg->state) { |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame] | 642 | case TPGS_STATE_OPTIMIZED: |
| 643 | return SCSI_DH_OK; |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 644 | case TPGS_STATE_NONOPTIMIZED: |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 645 | if ((pg->flags & ALUA_OPTIMIZE_STPG) && |
| 646 | !pg->pref && |
| 647 | (pg->tpgs & TPGS_MODE_IMPLICIT)) |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame] | 648 | return SCSI_DH_OK; |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 649 | break; |
| 650 | case TPGS_STATE_STANDBY: |
| 651 | case TPGS_STATE_UNAVAILABLE: |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 652 | break; |
| 653 | case TPGS_STATE_OFFLINE: |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame] | 654 | return SCSI_DH_IO; |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 655 | case TPGS_STATE_TRANSITIONING: |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 656 | break; |
| 657 | default: |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame] | 658 | sdev_printk(KERN_INFO, sdev, |
| 659 | "%s: stpg failed, unhandled TPGS state %d", |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 660 | ALUA_DH_NAME, pg->state); |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame] | 661 | return SCSI_DH_NOSYS; |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 662 | } |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 663 | retval = submit_stpg(sdev, pg->group_id, &sense_hdr); |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 664 | |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame] | 665 | if (retval) { |
Hannes Reinecke | 40bb61a | 2016-02-19 09:17:02 +0100 | [diff] [blame] | 666 | if (!scsi_sense_valid(&sense_hdr)) { |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame] | 667 | sdev_printk(KERN_INFO, sdev, |
| 668 | "%s: stpg failed, result %d", |
| 669 | ALUA_DH_NAME, retval); |
Hannes Reinecke | 40bb61a | 2016-02-19 09:17:02 +0100 | [diff] [blame] | 670 | if (driver_byte(retval) == DRIVER_ERROR) |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame] | 671 | return SCSI_DH_DEV_TEMP_BUSY; |
| 672 | } else { |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 673 | sdev_printk(KERN_INFO, sdev, "%s: stpg failed\n", |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame] | 674 | ALUA_DH_NAME); |
| 675 | scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr); |
| 676 | } |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 677 | } |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame] | 678 | /* Retry RTPG */ |
| 679 | return SCSI_DH_RETRY; |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 680 | } |
| 681 | |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 682 | static void alua_rtpg_work(struct work_struct *work) |
| 683 | { |
| 684 | struct alua_port_group *pg = |
| 685 | container_of(work, struct alua_port_group, rtpg_work.work); |
| 686 | struct scsi_device *sdev; |
| 687 | LIST_HEAD(qdata_list); |
| 688 | int err = SCSI_DH_OK; |
| 689 | struct alua_queue_data *qdata, *tmp; |
| 690 | unsigned long flags; |
Hannes Reinecke | 00642a1 | 2016-02-19 09:17:11 +0100 | [diff] [blame^] | 691 | struct workqueue_struct *alua_wq = kaluad_wq; |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 692 | |
| 693 | spin_lock_irqsave(&pg->lock, flags); |
| 694 | sdev = pg->rtpg_sdev; |
| 695 | if (!sdev) { |
| 696 | WARN_ON(pg->flags & ALUA_PG_RUN_RTPG); |
| 697 | WARN_ON(pg->flags & ALUA_PG_RUN_STPG); |
| 698 | spin_unlock_irqrestore(&pg->lock, flags); |
| 699 | return; |
| 700 | } |
Hannes Reinecke | 00642a1 | 2016-02-19 09:17:11 +0100 | [diff] [blame^] | 701 | if (pg->flags & ALUA_SYNC_STPG) |
| 702 | alua_wq = kaluad_sync_wq; |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 703 | pg->flags |= ALUA_PG_RUNNING; |
| 704 | if (pg->flags & ALUA_PG_RUN_RTPG) { |
| 705 | pg->flags &= ~ALUA_PG_RUN_RTPG; |
| 706 | spin_unlock_irqrestore(&pg->lock, flags); |
| 707 | err = alua_rtpg(sdev, pg); |
| 708 | spin_lock_irqsave(&pg->lock, flags); |
| 709 | if (err == SCSI_DH_RETRY) { |
| 710 | pg->flags &= ~ALUA_PG_RUNNING; |
| 711 | pg->flags |= ALUA_PG_RUN_RTPG; |
| 712 | spin_unlock_irqrestore(&pg->lock, flags); |
Hannes Reinecke | 00642a1 | 2016-02-19 09:17:11 +0100 | [diff] [blame^] | 713 | queue_delayed_work(alua_wq, &pg->rtpg_work, |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 714 | pg->interval * HZ); |
| 715 | return; |
| 716 | } |
| 717 | if (err != SCSI_DH_OK) |
| 718 | pg->flags &= ~ALUA_PG_RUN_STPG; |
| 719 | } |
| 720 | if (pg->flags & ALUA_PG_RUN_STPG) { |
| 721 | pg->flags &= ~ALUA_PG_RUN_STPG; |
| 722 | spin_unlock_irqrestore(&pg->lock, flags); |
| 723 | err = alua_stpg(sdev, pg); |
| 724 | spin_lock_irqsave(&pg->lock, flags); |
| 725 | if (err == SCSI_DH_RETRY) { |
| 726 | pg->flags |= ALUA_PG_RUN_RTPG; |
| 727 | pg->interval = 0; |
| 728 | pg->flags &= ~ALUA_PG_RUNNING; |
| 729 | spin_unlock_irqrestore(&pg->lock, flags); |
Hannes Reinecke | 00642a1 | 2016-02-19 09:17:11 +0100 | [diff] [blame^] | 730 | queue_delayed_work(alua_wq, &pg->rtpg_work, |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 731 | pg->interval * HZ); |
| 732 | return; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | list_splice_init(&pg->rtpg_list, &qdata_list); |
| 737 | pg->rtpg_sdev = NULL; |
| 738 | spin_unlock_irqrestore(&pg->lock, flags); |
| 739 | |
| 740 | list_for_each_entry_safe(qdata, tmp, &qdata_list, entry) { |
| 741 | list_del(&qdata->entry); |
| 742 | if (qdata->callback_fn) |
| 743 | qdata->callback_fn(qdata->callback_data, err); |
| 744 | kfree(qdata); |
| 745 | } |
| 746 | spin_lock_irqsave(&pg->lock, flags); |
| 747 | pg->flags &= ~ALUA_PG_RUNNING; |
| 748 | spin_unlock_irqrestore(&pg->lock, flags); |
| 749 | scsi_device_put(sdev); |
| 750 | kref_put(&pg->kref, release_port_group); |
| 751 | } |
| 752 | |
| 753 | static void alua_rtpg_queue(struct alua_port_group *pg, |
| 754 | struct scsi_device *sdev, |
| 755 | struct alua_queue_data *qdata) |
| 756 | { |
| 757 | int start_queue = 0; |
| 758 | unsigned long flags; |
Hannes Reinecke | 00642a1 | 2016-02-19 09:17:11 +0100 | [diff] [blame^] | 759 | struct workqueue_struct *alua_wq = kaluad_wq; |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 760 | |
| 761 | if (!pg) |
| 762 | return; |
| 763 | |
| 764 | spin_lock_irqsave(&pg->lock, flags); |
| 765 | if (qdata) { |
| 766 | list_add_tail(&qdata->entry, &pg->rtpg_list); |
| 767 | pg->flags |= ALUA_PG_RUN_STPG; |
| 768 | } |
| 769 | if (pg->rtpg_sdev == NULL) { |
| 770 | pg->interval = 0; |
| 771 | pg->flags |= ALUA_PG_RUN_RTPG; |
| 772 | kref_get(&pg->kref); |
| 773 | pg->rtpg_sdev = sdev; |
| 774 | scsi_device_get(sdev); |
| 775 | start_queue = 1; |
| 776 | } |
Hannes Reinecke | 00642a1 | 2016-02-19 09:17:11 +0100 | [diff] [blame^] | 777 | if (pg->flags & ALUA_SYNC_STPG) |
| 778 | alua_wq = kaluad_sync_wq; |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 779 | spin_unlock_irqrestore(&pg->lock, flags); |
| 780 | |
| 781 | if (start_queue && |
Hannes Reinecke | 00642a1 | 2016-02-19 09:17:11 +0100 | [diff] [blame^] | 782 | !queue_delayed_work(alua_wq, &pg->rtpg_work, |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 783 | msecs_to_jiffies(ALUA_RTPG_DELAY_MSECS))) { |
| 784 | scsi_device_put(sdev); |
| 785 | kref_put(&pg->kref, release_port_group); |
| 786 | } |
| 787 | } |
| 788 | |
Hannes Reinecke | f2ecf13 | 2016-02-19 09:16:59 +0100 | [diff] [blame] | 789 | /* |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 790 | * alua_initialize - Initialize ALUA state |
| 791 | * @sdev: the device to be initialized |
| 792 | * |
| 793 | * For the prep_fn to work correctly we have |
| 794 | * to initialize the ALUA state for the device. |
| 795 | */ |
| 796 | static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h) |
| 797 | { |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 798 | int err = SCSI_DH_DEV_UNSUPP, tpgs; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 799 | |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 800 | mutex_lock(&h->init_mutex); |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 801 | tpgs = alua_check_tpgs(sdev); |
Hannes Reinecke | a4253fd | 2016-02-19 09:17:06 +0100 | [diff] [blame] | 802 | if (tpgs != TPGS_MODE_NONE) |
| 803 | err = alua_check_vpd(sdev, h, tpgs); |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 804 | h->init_error = err; |
| 805 | mutex_unlock(&h->init_mutex); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 806 | return err; |
| 807 | } |
Moger, Babu | 4335d09 | 2012-03-27 20:55:49 +0000 | [diff] [blame] | 808 | /* |
| 809 | * alua_set_params - set/unset the optimize flag |
| 810 | * @sdev: device on the path to be activated |
| 811 | * params - parameters in the following format |
| 812 | * "no_of_params\0param1\0param2\0param3\0...\0" |
| 813 | * For example, to set the flag pass the following parameters |
| 814 | * from multipath.conf |
| 815 | * hardware_handler "2 alua 1" |
| 816 | */ |
| 817 | static int alua_set_params(struct scsi_device *sdev, const char *params) |
| 818 | { |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 819 | struct alua_dh_data *h = sdev->handler_data; |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 820 | struct alua_port_group __rcu *pg = NULL; |
Moger, Babu | 4335d09 | 2012-03-27 20:55:49 +0000 | [diff] [blame] | 821 | unsigned int optimize = 0, argc; |
| 822 | const char *p = params; |
| 823 | int result = SCSI_DH_OK; |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 824 | unsigned long flags; |
Moger, Babu | 4335d09 | 2012-03-27 20:55:49 +0000 | [diff] [blame] | 825 | |
| 826 | if ((sscanf(params, "%u", &argc) != 1) || (argc != 1)) |
| 827 | return -EINVAL; |
| 828 | |
| 829 | while (*p++) |
| 830 | ; |
| 831 | if ((sscanf(p, "%u", &optimize) != 1) || (optimize > 1)) |
| 832 | return -EINVAL; |
| 833 | |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 834 | rcu_read_lock(); |
| 835 | pg = rcu_dereference(h->pg); |
| 836 | if (!pg) { |
| 837 | rcu_read_unlock(); |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 838 | return -ENXIO; |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 839 | } |
| 840 | spin_lock_irqsave(&pg->lock, flags); |
Moger, Babu | 4335d09 | 2012-03-27 20:55:49 +0000 | [diff] [blame] | 841 | if (optimize) |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 842 | pg->flags |= ALUA_OPTIMIZE_STPG; |
Moger, Babu | 4335d09 | 2012-03-27 20:55:49 +0000 | [diff] [blame] | 843 | else |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 844 | pg->flags &= ~ALUA_OPTIMIZE_STPG; |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 845 | spin_unlock_irqrestore(&pg->lock, flags); |
| 846 | rcu_read_unlock(); |
Moger, Babu | 4335d09 | 2012-03-27 20:55:49 +0000 | [diff] [blame] | 847 | |
| 848 | return result; |
| 849 | } |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 850 | |
| 851 | /* |
| 852 | * alua_activate - activate a path |
| 853 | * @sdev: device on the path to be activated |
| 854 | * |
| 855 | * We're currently switching the port group to be activated only and |
| 856 | * let the array figure out the rest. |
| 857 | * There may be other arrays which require us to switch all port groups |
| 858 | * based on a certain policy. But until we actually encounter them it |
| 859 | * should be okay. |
| 860 | */ |
Chandra Seetharaman | 3ae31f6 | 2009-10-21 09:22:46 -0700 | [diff] [blame] | 861 | static int alua_activate(struct scsi_device *sdev, |
| 862 | activate_complete fn, void *data) |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 863 | { |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 864 | struct alua_dh_data *h = sdev->handler_data; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 865 | int err = SCSI_DH_OK; |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 866 | struct alua_queue_data *qdata; |
| 867 | struct alua_port_group __rcu *pg; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 868 | |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 869 | qdata = kzalloc(sizeof(*qdata), GFP_KERNEL); |
| 870 | if (!qdata) { |
| 871 | err = SCSI_DH_RES_TEMP_UNAVAIL; |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 872 | goto out; |
| 873 | } |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 874 | qdata->callback_fn = fn; |
| 875 | qdata->callback_data = data; |
| 876 | |
| 877 | mutex_lock(&h->init_mutex); |
| 878 | rcu_read_lock(); |
| 879 | pg = rcu_dereference(h->pg); |
| 880 | if (!pg || !kref_get_unless_zero(&pg->kref)) { |
| 881 | rcu_read_unlock(); |
| 882 | kfree(qdata); |
| 883 | err = h->init_error; |
| 884 | mutex_unlock(&h->init_mutex); |
| 885 | goto out; |
| 886 | } |
| 887 | fn = NULL; |
| 888 | rcu_read_unlock(); |
| 889 | mutex_unlock(&h->init_mutex); |
| 890 | |
| 891 | alua_rtpg_queue(pg, sdev, qdata); |
| 892 | kref_put(&pg->kref, release_port_group); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 893 | out: |
Hannes Reinecke | b246075 | 2016-02-19 09:17:00 +0100 | [diff] [blame] | 894 | if (fn) |
Chandra Seetharaman | 3ae31f6 | 2009-10-21 09:22:46 -0700 | [diff] [blame] | 895 | fn(data, err); |
| 896 | return 0; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | /* |
| 900 | * alua_prep_fn - request callback |
| 901 | * |
| 902 | * Fail I/O to all paths not in state |
| 903 | * active/optimized or active/non-optimized. |
| 904 | */ |
| 905 | static int alua_prep_fn(struct scsi_device *sdev, struct request *req) |
| 906 | { |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 907 | struct alua_dh_data *h = sdev->handler_data; |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 908 | struct alua_port_group __rcu *pg; |
| 909 | int state = TPGS_STATE_OPTIMIZED; |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 910 | int ret = BLKPREP_OK; |
| 911 | |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 912 | rcu_read_lock(); |
| 913 | pg = rcu_dereference(h->pg); |
| 914 | if (pg) |
| 915 | state = pg->state; |
| 916 | rcu_read_unlock(); |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 917 | if (state == TPGS_STATE_TRANSITIONING) |
Hannes Reinecke | 69723d1 | 2010-09-24 15:57:04 +0200 | [diff] [blame] | 918 | ret = BLKPREP_DEFER; |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 919 | else if (state != TPGS_STATE_OPTIMIZED && |
| 920 | state != TPGS_STATE_NONOPTIMIZED && |
| 921 | state != TPGS_STATE_LBA_DEPENDENT) { |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 922 | ret = BLKPREP_KILL; |
| 923 | req->cmd_flags |= REQ_QUIET; |
| 924 | } |
| 925 | return ret; |
| 926 | |
| 927 | } |
| 928 | |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 929 | /* |
| 930 | * alua_bus_attach - Attach device handler |
| 931 | * @sdev: device to be attached to |
| 932 | */ |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 933 | static int alua_bus_attach(struct scsi_device *sdev) |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 934 | { |
| 935 | struct alua_dh_data *h; |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 936 | int err, ret = -EINVAL; |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 937 | |
| 938 | h = kzalloc(sizeof(*h) , GFP_KERNEL); |
| 939 | if (!h) |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 940 | return -ENOMEM; |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 941 | spin_lock_init(&h->pg_lock); |
| 942 | rcu_assign_pointer(h->pg, NULL); |
| 943 | h->init_error = SCSI_DH_OK; |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 944 | h->sdev = sdev; |
| 945 | |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 946 | mutex_init(&h->init_mutex); |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 947 | err = alua_initialize(sdev, h); |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 948 | if (err == SCSI_DH_NOMEM) |
| 949 | ret = -ENOMEM; |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 950 | if (err != SCSI_DH_OK && err != SCSI_DH_DEV_OFFLINED) |
| 951 | goto failed; |
| 952 | |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 953 | sdev->handler_data = h; |
| 954 | return 0; |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 955 | failed: |
| 956 | kfree(h); |
Hannes Reinecke | 43394c6 | 2016-02-19 09:17:04 +0100 | [diff] [blame] | 957 | return ret; |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | /* |
| 961 | * alua_bus_detach - Detach device handler |
| 962 | * @sdev: device to be detached from |
| 963 | */ |
| 964 | static void alua_bus_detach(struct scsi_device *sdev) |
| 965 | { |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 966 | struct alua_dh_data *h = sdev->handler_data; |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 967 | struct alua_port_group *pg; |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 968 | |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 969 | spin_lock(&h->pg_lock); |
| 970 | pg = h->pg; |
| 971 | rcu_assign_pointer(h->pg, NULL); |
| 972 | h->sdev = NULL; |
| 973 | spin_unlock(&h->pg_lock); |
| 974 | if (pg) |
| 975 | kref_put(&pg->kref, release_port_group); |
| 976 | |
Christoph Hellwig | ee14c67 | 2015-08-27 14:16:59 +0200 | [diff] [blame] | 977 | sdev->handler_data = NULL; |
Christoph Hellwig | 1d52032 | 2014-09-14 11:08:21 -0700 | [diff] [blame] | 978 | kfree(h); |
| 979 | } |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 980 | |
| 981 | static struct scsi_device_handler alua_dh = { |
| 982 | .name = ALUA_DH_NAME, |
| 983 | .module = THIS_MODULE, |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 984 | .attach = alua_bus_attach, |
| 985 | .detach = alua_bus_detach, |
| 986 | .prep_fn = alua_prep_fn, |
| 987 | .check_sense = alua_check_sense, |
| 988 | .activate = alua_activate, |
Moger, Babu | 4335d09 | 2012-03-27 20:55:49 +0000 | [diff] [blame] | 989 | .set_params = alua_set_params, |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 990 | }; |
| 991 | |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 992 | static int __init alua_init(void) |
| 993 | { |
| 994 | int r; |
| 995 | |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 996 | kaluad_wq = alloc_workqueue("kaluad", WQ_MEM_RECLAIM, 0); |
| 997 | if (!kaluad_wq) { |
| 998 | /* Temporary failure, bypass */ |
| 999 | return SCSI_DH_DEV_TEMP_BUSY; |
| 1000 | } |
Hannes Reinecke | 00642a1 | 2016-02-19 09:17:11 +0100 | [diff] [blame^] | 1001 | kaluad_sync_wq = create_workqueue("kaluad_sync"); |
| 1002 | if (!kaluad_sync_wq) { |
| 1003 | destroy_workqueue(kaluad_wq); |
| 1004 | return SCSI_DH_DEV_TEMP_BUSY; |
| 1005 | } |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 1006 | r = scsi_register_device_handler(&alua_dh); |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 1007 | if (r != 0) { |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 1008 | printk(KERN_ERR "%s: Failed to register scsi device handler", |
| 1009 | ALUA_DH_NAME); |
Hannes Reinecke | 00642a1 | 2016-02-19 09:17:11 +0100 | [diff] [blame^] | 1010 | destroy_workqueue(kaluad_sync_wq); |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 1011 | destroy_workqueue(kaluad_wq); |
| 1012 | } |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 1013 | return r; |
| 1014 | } |
| 1015 | |
| 1016 | static void __exit alua_exit(void) |
| 1017 | { |
| 1018 | scsi_unregister_device_handler(&alua_dh); |
Hannes Reinecke | 00642a1 | 2016-02-19 09:17:11 +0100 | [diff] [blame^] | 1019 | destroy_workqueue(kaluad_sync_wq); |
Hannes Reinecke | 03197b6 | 2016-02-19 09:17:10 +0100 | [diff] [blame] | 1020 | destroy_workqueue(kaluad_wq); |
Hannes Reinecke | 057ea7c | 2008-07-17 16:53:21 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | module_init(alua_init); |
| 1024 | module_exit(alua_exit); |
| 1025 | |
| 1026 | MODULE_DESCRIPTION("DM Multipath ALUA support"); |
| 1027 | MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>"); |
| 1028 | MODULE_LICENSE("GPL"); |
| 1029 | MODULE_VERSION(ALUA_DH_VER); |