blob: a3cb06955adce1cb8c77b57c47ed747ad5f64af0 [file] [log] [blame]
Hannes Reinecke057ea7c2008-07-17 16:53:21 -07001/*
2 * Generic SCSI-3 ALUA SCSI Device Handler
3 *
Hannes Reinecke69723d12010-09-24 15:57:04 +02004 * Copyright (C) 2007-2010 Hannes Reinecke, SUSE Linux Products GmbH.
Hannes Reinecke057ea7c2008-07-17 16:53:21 -07005 * 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 Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Hannes Reinecke69723d12010-09-24 15:57:04 +020023#include <linux/delay.h>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040024#include <linux/module.h>
Hannes Reineckea7089772015-12-01 10:16:52 +010025#include <asm/unaligned.h>
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070026#include <scsi/scsi.h>
Hannes Reinecke80bd68d2015-12-01 10:16:47 +010027#include <scsi/scsi_dbg.h>
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070028#include <scsi/scsi_eh.h>
29#include <scsi/scsi_dh.h>
30
31#define ALUA_DH_NAME "alua"
Hannes Reinecke69723d12010-09-24 15:57:04 +020032#define ALUA_DH_VER "1.3"
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070033
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 Reinecke69723d12010-09-24 15:57:04 +020038#define TPGS_STATE_LBA_DEPENDENT 0x4
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070039#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 Reinecke69723d12010-09-24 15:57:04 +020047#define TPGS_SUPPORT_LBA_DEPENDENT 0x10
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070048#define TPGS_SUPPORT_OFFLINE 0x40
49#define TPGS_SUPPORT_TRANSITION 0x80
50
Rob Evers3588c5a2012-05-18 14:08:54 -040051#define RTPG_FMT_MASK 0x70
52#define RTPG_FMT_EXT_HDR 0x10
53
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070054#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 Reineckec49c8342016-02-19 09:17:03 +010059#define ALUA_RTPG_SIZE 128
Rob Evers3588c5a2012-05-18 14:08:54 -040060#define ALUA_FAILOVER_TIMEOUT 60
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070061#define ALUA_FAILOVER_RETRIES 5
Hannes Reinecke03197b62016-02-19 09:17:10 +010062#define ALUA_RTPG_DELAY_MSECS 5
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070063
Hannes Reinecke6c4fc042015-12-01 10:16:51 +010064/* device handler flags */
Hannes Reinecke03197b62016-02-19 09:17:10 +010065#define ALUA_OPTIMIZE_STPG 0x01
66#define ALUA_RTPG_EXT_HDR_UNSUPP 0x02
Hannes Reinecke00642a12016-02-19 09:17:11 +010067#define ALUA_SYNC_STPG 0x04
Hannes Reinecke03197b62016-02-19 09:17:10 +010068/* State machine flags */
69#define ALUA_PG_RUN_RTPG 0x10
70#define ALUA_PG_RUN_STPG 0x20
71#define ALUA_PG_RUNNING 0x40
Moger, Babu4335d092012-03-27 20:55:49 +000072
Hannes Reineckeaa90f492016-02-19 09:17:08 +010073static uint optimize_stpg;
74module_param(optimize_stpg, uint, S_IRUGO|S_IWUSR);
75MODULE_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 Reinecke43394c62016-02-19 09:17:04 +010077static LIST_HEAD(port_group_list);
78static DEFINE_SPINLOCK(port_group_lock);
Hannes Reinecke03197b62016-02-19 09:17:10 +010079static struct workqueue_struct *kaluad_wq;
Hannes Reinecke00642a12016-02-19 09:17:11 +010080static struct workqueue_struct *kaluad_sync_wq;
Hannes Reinecke43394c62016-02-19 09:17:04 +010081
82struct alua_port_group {
83 struct kref kref;
Hannes Reinecke03197b62016-02-19 09:17:10 +010084 struct rcu_head rcu;
Hannes Reinecke43394c62016-02-19 09:17:04 +010085 struct list_head node;
Hannes Reinecke00472202016-02-19 09:17:05 +010086 unsigned char device_id_str[256];
87 int device_id_len;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070088 int group_id;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070089 int tpgs;
90 int state;
Moger, Babudcd3a752012-03-27 20:56:08 +000091 int pref;
Moger, Babu4335d092012-03-27 20:55:49 +000092 unsigned flags; /* used for optimizing STPG */
Rob Evers3588c5a2012-05-18 14:08:54 -040093 unsigned char transition_tmo;
Hannes Reinecke03197b62016-02-19 09:17:10 +010094 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 Reinecke43394c62016-02-19 09:17:04 +0100100};
101
102struct alua_dh_data {
103 struct alua_port_group *pg;
104 int group_id;
Hannes Reinecke03197b62016-02-19 09:17:10 +0100105 spinlock_t pg_lock;
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700106 struct scsi_device *sdev;
Hannes Reinecke03197b62016-02-19 09:17:10 +0100107 int init_error;
108 struct mutex init_mutex;
109};
110
111struct alua_queue_data {
112 struct list_head entry;
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700113 activate_complete callback_fn;
114 void *callback_data;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700115};
116
117#define ALUA_POLICY_SWITCH_CURRENT 0
118#define ALUA_POLICY_SWITCH_ALL 1
119
Hannes Reinecke03197b62016-02-19 09:17:10 +0100120static void alua_rtpg_work(struct work_struct *work);
121static void alua_rtpg_queue(struct alua_port_group *pg,
122 struct scsi_device *sdev,
123 struct alua_queue_data *qdata);
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700124
Hannes Reinecke43394c62016-02-19 09:17:04 +0100125static 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 Reinecke03197b62016-02-19 09:17:10 +0100130 if (pg->rtpg_sdev)
131 flush_delayed_work(&pg->rtpg_work);
Hannes Reinecke43394c62016-02-19 09:17:04 +0100132 spin_lock(&port_group_lock);
133 list_del(&pg->node);
134 spin_unlock(&port_group_lock);
Hannes Reinecke03197b62016-02-19 09:17:10 +0100135 kfree_rcu(pg, rcu);
Hannes Reinecke43394c62016-02-19 09:17:04 +0100136}
137
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700138/*
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700139 * submit_rtpg - Issue a REPORT TARGET GROUP STATES command
140 * @sdev: sdev the command should be sent to
141 */
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100142static int submit_rtpg(struct scsi_device *sdev, unsigned char *buff,
143 int bufflen, struct scsi_sense_hdr *sshdr, int flags)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700144{
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100145 u8 cdb[COMMAND_SIZE(MAINTENANCE_IN)];
146 int req_flags = REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
147 REQ_FAILFAST_DRIVER;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700148
149 /* Prepare the command. */
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100150 memset(cdb, 0x0, COMMAND_SIZE(MAINTENANCE_IN));
151 cdb[0] = MAINTENANCE_IN;
Hannes Reinecked42ae5f2016-02-19 09:16:58 +0100152 if (!(flags & ALUA_RTPG_EXT_HDR_UNSUPP))
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100153 cdb[1] = MI_REPORT_TARGET_PGS | MI_EXT_HDR_PARAM_FMT;
Rob Evers8e67ce62012-05-18 14:08:55 -0400154 else
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100155 cdb[1] = MI_REPORT_TARGET_PGS;
156 put_unaligned_be32(bufflen, &cdb[6]);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700157
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100158 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 Reinecke057ea7c2008-07-17 16:53:21 -0700162}
163
164/*
Hannes Reineckeb2460752016-02-19 09:17:00 +0100165 * submit_stpg - Issue a SET TARGET PORT GROUP command
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700166 *
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 Reinecke40bb61a2016-02-19 09:17:02 +0100171static int submit_stpg(struct scsi_device *sdev, int group_id,
172 struct scsi_sense_hdr *sshdr)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700173{
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100174 u8 cdb[COMMAND_SIZE(MAINTENANCE_OUT)];
Hannes Reineckeb2460752016-02-19 09:17:00 +0100175 unsigned char stpg_data[8];
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700176 int stpg_len = 8;
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100177 int req_flags = REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
178 REQ_FAILFAST_DRIVER;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700179
180 /* Prepare the data buffer */
Hannes Reineckeb2460752016-02-19 09:17:00 +0100181 memset(stpg_data, 0, stpg_len);
182 stpg_data[4] = TPGS_STATE_OPTIMIZED & 0x0f;
183 put_unaligned_be16(group_id, &stpg_data[6]);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700184
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700185 /* Prepare the command. */
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100186 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 Reinecke057ea7c2008-07-17 16:53:21 -0700190
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100191 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 Reinecke057ea7c2008-07-17 16:53:21 -0700195}
196
Hannes Reinecke00472202016-02-19 09:17:05 +0100197struct 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 Reinecke057ea7c2008-07-17 16:53:21 -0700217/*
Hannes Reinecke43394c62016-02-19 09:17:04 +0100218 * 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 */
226struct alua_port_group *alua_alloc_pg(struct scsi_device *sdev,
227 int group_id, int tpgs)
228{
Hannes Reinecke00472202016-02-19 09:17:05 +0100229 struct alua_port_group *pg, *tmp_pg;
Hannes Reinecke43394c62016-02-19 09:17:04 +0100230
231 pg = kzalloc(sizeof(struct alua_port_group), GFP_KERNEL);
232 if (!pg)
Hannes Reinecke00472202016-02-19 09:17:05 +0100233 return ERR_PTR(-ENOMEM);
Hannes Reinecke43394c62016-02-19 09:17:04 +0100234
Hannes Reinecke00472202016-02-19 09:17:05 +0100235 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 Reinecke43394c62016-02-19 09:17:04 +0100248 pg->group_id = group_id;
249 pg->tpgs = tpgs;
250 pg->state = TPGS_STATE_OPTIMIZED;
Hannes Reineckeaa90f492016-02-19 09:17:08 +0100251 if (optimize_stpg)
252 pg->flags |= ALUA_OPTIMIZE_STPG;
Hannes Reinecke43394c62016-02-19 09:17:04 +0100253 kref_init(&pg->kref);
Hannes Reinecke03197b62016-02-19 09:17:10 +0100254 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 Reinecke00472202016-02-19 09:17:05 +0100258
Hannes Reinecke43394c62016-02-19 09:17:04 +0100259 spin_lock(&port_group_lock);
Hannes Reinecke00472202016-02-19 09:17:05 +0100260 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 Reinecke43394c62016-02-19 09:17:04 +0100268 list_add(&pg->node, &port_group_list);
269 spin_unlock(&port_group_lock);
270
271 return pg;
272}
273
274/*
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200275 * alua_check_tpgs - Evaluate TPGS setting
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700276 * @sdev: device to be checked
277 *
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200278 * Examine the TPGS setting of the sdev to find out if ALUA
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700279 * is supported.
280 */
Hannes Reineckead0ea642015-12-01 10:16:53 +0100281static int alua_check_tpgs(struct scsi_device *sdev)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700282{
Hannes Reineckead0ea642015-12-01 10:16:53 +0100283 int tpgs = TPGS_MODE_NONE;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700284
Hannes Reineckedb5a6a62015-12-01 10:16:43 +0100285 /*
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 Reineckedb5a6a62015-12-01 10:16:43 +0100290 sdev_printk(KERN_INFO, sdev,
291 "%s: disable for non-disk devices\n",
292 ALUA_DH_NAME);
Hannes Reineckead0ea642015-12-01 10:16:53 +0100293 return tpgs;
Hannes Reineckedb5a6a62015-12-01 10:16:43 +0100294 }
295
Hannes Reineckead0ea642015-12-01 10:16:53 +0100296 tpgs = scsi_device_tpgs(sdev);
297 switch (tpgs) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700298 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 Reinecke6cc05d42015-12-01 10:16:45 +0100311 case TPGS_MODE_NONE:
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700312 sdev_printk(KERN_INFO, sdev, "%s: not supported\n",
313 ALUA_DH_NAME);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700314 break;
Hannes Reinecke6cc05d42015-12-01 10:16:45 +0100315 default:
316 sdev_printk(KERN_INFO, sdev,
317 "%s: unsupported TPGS setting %d\n",
Hannes Reineckead0ea642015-12-01 10:16:53 +0100318 ALUA_DH_NAME, tpgs);
319 tpgs = TPGS_MODE_NONE;
Hannes Reinecke6cc05d42015-12-01 10:16:45 +0100320 break;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700321 }
322
Hannes Reineckead0ea642015-12-01 10:16:53 +0100323 return tpgs;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700324}
325
326/*
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100327 * alua_check_vpd - Evaluate INQUIRY vpd page 0x83
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700328 * @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 Reineckea4253fd2016-02-19 09:17:06 +0100333static int alua_check_vpd(struct scsi_device *sdev, struct alua_dh_data *h,
334 int tpgs)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700335{
Hannes Reinecke83ea0e52015-12-01 10:16:58 +0100336 int rel_port = -1, group_id;
Hannes Reinecke03197b62016-02-19 09:17:10 +0100337 struct alua_port_group *pg, *old_pg = NULL;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700338
Hannes Reinecke83ea0e52015-12-01 10:16:58 +0100339 group_id = scsi_vpd_tpg_id(sdev, &rel_port);
340 if (group_id < 0) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700341 /*
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 Reinecke9b80dce2015-12-01 10:16:44 +0100349 return SCSI_DH_DEV_UNSUPP;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700350 }
Hannes Reineckea4253fd2016-02-19 09:17:06 +0100351
Hannes Reinecke03197b62016-02-19 09:17:10 +0100352 pg = alua_alloc_pg(sdev, group_id, tpgs);
353 if (IS_ERR(pg)) {
354 if (PTR_ERR(pg) == -ENOMEM)
Hannes Reineckea4253fd2016-02-19 09:17:06 +0100355 return SCSI_DH_NOMEM;
356 return SCSI_DH_DEV_UNSUPP;
357 }
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100358 sdev_printk(KERN_INFO, sdev,
Hannes Reineckea4253fd2016-02-19 09:17:06 +0100359 "%s: device %s port group %x rel port %x\n",
Hannes Reinecke03197b62016-02-19 09:17:10 +0100360 ALUA_DH_NAME, pg->device_id_str, group_id, rel_port);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700361
Hannes Reinecke03197b62016-02-19 09:17:10 +0100362 /* 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 Reinecke057ea7c2008-07-17 16:53:21 -0700376}
377
378static 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 Reinecke69723d12010-09-24 15:57:04 +0200389 case TPGS_STATE_LBA_DEPENDENT:
390 return 'L';
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700391 case TPGS_STATE_OFFLINE:
392 return 'O';
393 case TPGS_STATE_TRANSITIONING:
394 return 'T';
395 default:
396 return 'X';
397 }
398}
399
400static 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 Andersonc7dbb622008-08-12 12:11:57 -0700409 return ADD_TO_MLQUEUE;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700410 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 Andersonc7dbb622008-08-12 12:11:57 -0700416 return ADD_TO_MLQUEUE;
Stewart, Seanc20ee7b2013-10-15 15:52:39 +0000417 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x04)
418 /*
419 * Device internal reset
420 */
421 return ADD_TO_MLQUEUE;
Moger, Babu410f02d2011-12-21 18:01:37 -0500422 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x01)
423 /*
424 * Mode Parameters Changed
425 */
426 return ADD_TO_MLQUEUE;
Moger, Babubf819732011-10-06 13:22:07 -0400427 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700428 /*
429 * ALUA state changed
430 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700431 return ADD_TO_MLQUEUE;
Moger, Babubf819732011-10-06 13:22:07 -0400432 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700433 /*
434 * Implicit ALUA state transition failed
435 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700436 return ADD_TO_MLQUEUE;
Moger, Babubf819732011-10-06 13:22:07 -0400437 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 Hong4d086f62009-01-30 17:00:11 -0600443 /*
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 Reinecke057ea7c2008-07-17 16:53:21 -0700449 break;
450 }
451
452 return SCSI_RETURN_NOT_HANDLED;
453}
454
455/*
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700456 * 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 Marchi25985ed2011-03-30 22:57:33 -0300461 * found to be unusable.
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700462 */
Hannes Reinecke28261402016-02-19 09:17:07 +0100463static int alua_rtpg(struct scsi_device *sdev, struct alua_port_group *pg)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700464{
465 struct scsi_sense_hdr sense_hdr;
Hannes Reineckec49c8342016-02-19 09:17:03 +0100466 int len, k, off, valid_states = 0, bufflen = ALUA_RTPG_SIZE;
467 unsigned char *ucp, *buff;
Hannes Reinecke5597caf2015-12-01 10:16:48 +0100468 unsigned err, retval;
Rob Evers3588c5a2012-05-18 14:08:54 -0400469 unsigned int tpg_desc_tbl_off;
470 unsigned char orig_transition_tmo;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700471
Hannes Reinecke03197b62016-02-19 09:17:10 +0100472 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 Evers3588c5a2012-05-18 14:08:54 -0400480
Hannes Reineckec49c8342016-02-19 09:17:03 +0100481 buff = kzalloc(bufflen, GFP_KERNEL);
482 if (!buff)
483 return SCSI_DH_DEV_TEMP_BUSY;
484
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700485 retry:
Hannes Reinecke43394c62016-02-19 09:17:04 +0100486 retval = submit_rtpg(sdev, buff, bufflen, &sense_hdr, pg->flags);
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100487
Hannes Reinecke5597caf2015-12-01 10:16:48 +0100488 if (retval) {
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100489 if (!scsi_sense_valid(&sense_hdr)) {
Hannes Reinecke5597caf2015-12-01 10:16:48 +0100490 sdev_printk(KERN_INFO, sdev,
491 "%s: rtpg failed, result %d\n",
492 ALUA_DH_NAME, retval);
Hannes Reineckec49c8342016-02-19 09:17:03 +0100493 kfree(buff);
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100494 if (driver_byte(retval) == DRIVER_ERROR)
Hannes Reinecke5597caf2015-12-01 10:16:48 +0100495 return SCSI_DH_DEV_TEMP_BUSY;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700496 return SCSI_DH_IO;
Hannes Reinecke5597caf2015-12-01 10:16:48 +0100497 }
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700498
Rob Evers8e67ce62012-05-18 14:08:55 -0400499 /*
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 Reinecke43394c62016-02-19 09:17:04 +0100507 if (!(pg->flags & ALUA_RTPG_EXT_HDR_UNSUPP) &&
Rob Evers8e67ce62012-05-18 14:08:55 -0400508 sense_hdr.sense_key == ILLEGAL_REQUEST &&
509 sense_hdr.asc == 0x24 && sense_hdr.ascq == 0) {
Hannes Reinecke43394c62016-02-19 09:17:04 +0100510 pg->flags |= ALUA_RTPG_EXT_HDR_UNSUPP;
Rob Evers8e67ce62012-05-18 14:08:55 -0400511 goto retry;
512 }
Hannes Reineckee2d817d2015-12-01 10:16:54 +0100513 /*
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 Reinecke03197b62016-02-19 09:17:10 +0100522 if (err == SCSI_DH_RETRY &&
523 pg->expiry != 0 && time_before(jiffies, pg->expiry)) {
Hannes Reinecke80bd68d2015-12-01 10:16:47 +0100524 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 Reinecke03197b62016-02-19 09:17:10 +0100527 return err;
Hannes Reinecke80bd68d2015-12-01 10:16:47 +0100528 }
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 Reineckec49c8342016-02-19 09:17:03 +0100532 kfree(buff);
Hannes Reinecke03197b62016-02-19 09:17:10 +0100533 pg->expiry = 0;
Hannes Reinecke80bd68d2015-12-01 10:16:47 +0100534 return SCSI_DH_IO;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700535 }
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700536
Hannes Reineckec49c8342016-02-19 09:17:03 +0100537 len = get_unaligned_be32(&buff[0]) + 4;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700538
Hannes Reineckec49c8342016-02-19 09:17:03 +0100539 if (len > bufflen) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700540 /* Resubmit with the correct length */
Hannes Reineckec49c8342016-02-19 09:17:03 +0100541 kfree(buff);
542 bufflen = len;
543 buff = kmalloc(bufflen, GFP_KERNEL);
544 if (!buff) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700545 sdev_printk(KERN_WARNING, sdev,
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700546 "%s: kmalloc buffer failed\n",__func__);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700547 /* Temporary failure, bypass */
Hannes Reinecke03197b62016-02-19 09:17:10 +0100548 pg->expiry = 0;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700549 return SCSI_DH_DEV_TEMP_BUSY;
550 }
551 goto retry;
552 }
553
Hannes Reinecke43394c62016-02-19 09:17:04 +0100554 orig_transition_tmo = pg->transition_tmo;
Hannes Reineckec49c8342016-02-19 09:17:03 +0100555 if ((buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR && buff[5] != 0)
Hannes Reinecke43394c62016-02-19 09:17:04 +0100556 pg->transition_tmo = buff[5];
Rob Evers3588c5a2012-05-18 14:08:54 -0400557 else
Hannes Reinecke43394c62016-02-19 09:17:04 +0100558 pg->transition_tmo = ALUA_FAILOVER_TIMEOUT;
Rob Evers3588c5a2012-05-18 14:08:54 -0400559
Hannes Reinecke28261402016-02-19 09:17:07 +0100560 if (orig_transition_tmo != pg->transition_tmo) {
Rob Evers3588c5a2012-05-18 14:08:54 -0400561 sdev_printk(KERN_INFO, sdev,
562 "%s: transition timeout set to %d seconds\n",
Hannes Reinecke43394c62016-02-19 09:17:04 +0100563 ALUA_DH_NAME, pg->transition_tmo);
Hannes Reinecke03197b62016-02-19 09:17:10 +0100564 pg->expiry = jiffies + pg->transition_tmo * HZ;
Rob Evers3588c5a2012-05-18 14:08:54 -0400565 }
566
Hannes Reineckec49c8342016-02-19 09:17:03 +0100567 if ((buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR)
Rob Evers3588c5a2012-05-18 14:08:54 -0400568 tpg_desc_tbl_off = 8;
569 else
570 tpg_desc_tbl_off = 4;
571
Hannes Reineckec49c8342016-02-19 09:17:03 +0100572 for (k = tpg_desc_tbl_off, ucp = buff + tpg_desc_tbl_off;
Rob Evers3588c5a2012-05-18 14:08:54 -0400573 k < len;
574 k += off, ucp += off) {
575
Hannes Reinecke43394c62016-02-19 09:17:04 +0100576 if (pg->group_id == get_unaligned_be16(&ucp[2])) {
577 pg->state = ucp[0] & 0x0f;
578 pg->pref = ucp[0] >> 7;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700579 valid_states = ucp[1];
580 }
581 off = 8 + (ucp[7] * 4);
582 }
583
584 sdev_printk(KERN_INFO, sdev,
Moger, Babudcd3a752012-03-27 20:56:08 +0000585 "%s: port group %02x state %c %s supports %c%c%c%c%c%c%c\n",
Hannes Reinecke43394c62016-02-19 09:17:04 +0100586 ALUA_DH_NAME, pg->group_id, print_alua_state(pg->state),
587 pg->pref ? "preferred" : "non-preferred",
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700588 valid_states&TPGS_SUPPORT_TRANSITION?'T':'t',
589 valid_states&TPGS_SUPPORT_OFFLINE?'O':'o',
Hannes Reinecke69723d12010-09-24 15:57:04 +0200590 valid_states&TPGS_SUPPORT_LBA_DEPENDENT?'L':'l',
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700591 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 Reinecke43394c62016-02-19 09:17:04 +0100596 switch (pg->state) {
Hannes Reinecke69723d12010-09-24 15:57:04 +0200597 case TPGS_STATE_TRANSITIONING:
Hannes Reinecke03197b62016-02-19 09:17:10 +0100598 if (time_before(jiffies, pg->expiry)) {
Hannes Reinecke28261402016-02-19 09:17:07 +0100599 /* State transition, retry */
Hannes Reinecke03197b62016-02-19 09:17:10 +0100600 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 Reinecke057ea7c2008-07-17 16:53:21 -0700607 }
Hannes Reinecke69723d12010-09-24 15:57:04 +0200608 break;
609 case TPGS_STATE_OFFLINE:
Bart Van Asschee47f8972012-08-24 09:08:41 +0000610 /* Path unusable */
Hannes Reinecke69723d12010-09-24 15:57:04 +0200611 err = SCSI_DH_DEV_OFFLINED;
Hannes Reinecke03197b62016-02-19 09:17:10 +0100612 pg->expiry = 0;
Hannes Reinecke69723d12010-09-24 15:57:04 +0200613 break;
614 default:
615 /* Useable path if active */
616 err = SCSI_DH_OK;
Hannes Reinecke03197b62016-02-19 09:17:10 +0100617 pg->expiry = 0;
Hannes Reinecke69723d12010-09-24 15:57:04 +0200618 break;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700619 }
Hannes Reineckec49c8342016-02-19 09:17:03 +0100620 kfree(buff);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700621 return err;
622}
623
624/*
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100625 * alua_stpg - Issue a SET TARGET PORT GROUP command
626 *
627 * Issue a SET TARGET PORT GROUP command and evaluate the
Hannes Reineckeb2460752016-02-19 09:17:00 +0100628 * 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 Reineckef2ecf132016-02-19 09:16:59 +0100631 */
Hannes Reinecke43394c62016-02-19 09:17:04 +0100632static unsigned alua_stpg(struct scsi_device *sdev, struct alua_port_group *pg)
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100633{
Hannes Reineckeb2460752016-02-19 09:17:00 +0100634 int retval;
635 struct scsi_sense_hdr sense_hdr;
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100636
Hannes Reinecke43394c62016-02-19 09:17:04 +0100637 if (!(pg->tpgs & TPGS_MODE_EXPLICIT)) {
Hannes Reineckeb2460752016-02-19 09:17:00 +0100638 /* Only implicit ALUA supported, retry */
639 return SCSI_DH_RETRY;
640 }
Hannes Reinecke43394c62016-02-19 09:17:04 +0100641 switch (pg->state) {
Hannes Reineckeb2460752016-02-19 09:17:00 +0100642 case TPGS_STATE_OPTIMIZED:
643 return SCSI_DH_OK;
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100644 case TPGS_STATE_NONOPTIMIZED:
Hannes Reinecke43394c62016-02-19 09:17:04 +0100645 if ((pg->flags & ALUA_OPTIMIZE_STPG) &&
646 !pg->pref &&
647 (pg->tpgs & TPGS_MODE_IMPLICIT))
Hannes Reineckeb2460752016-02-19 09:17:00 +0100648 return SCSI_DH_OK;
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100649 break;
650 case TPGS_STATE_STANDBY:
651 case TPGS_STATE_UNAVAILABLE:
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100652 break;
653 case TPGS_STATE_OFFLINE:
Hannes Reineckeb2460752016-02-19 09:17:00 +0100654 return SCSI_DH_IO;
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100655 case TPGS_STATE_TRANSITIONING:
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100656 break;
657 default:
Hannes Reineckeb2460752016-02-19 09:17:00 +0100658 sdev_printk(KERN_INFO, sdev,
659 "%s: stpg failed, unhandled TPGS state %d",
Hannes Reinecke43394c62016-02-19 09:17:04 +0100660 ALUA_DH_NAME, pg->state);
Hannes Reineckeb2460752016-02-19 09:17:00 +0100661 return SCSI_DH_NOSYS;
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100662 }
Hannes Reinecke43394c62016-02-19 09:17:04 +0100663 retval = submit_stpg(sdev, pg->group_id, &sense_hdr);
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100664
Hannes Reineckeb2460752016-02-19 09:17:00 +0100665 if (retval) {
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100666 if (!scsi_sense_valid(&sense_hdr)) {
Hannes Reineckeb2460752016-02-19 09:17:00 +0100667 sdev_printk(KERN_INFO, sdev,
668 "%s: stpg failed, result %d",
669 ALUA_DH_NAME, retval);
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100670 if (driver_byte(retval) == DRIVER_ERROR)
Hannes Reineckeb2460752016-02-19 09:17:00 +0100671 return SCSI_DH_DEV_TEMP_BUSY;
672 } else {
Hannes Reinecke43394c62016-02-19 09:17:04 +0100673 sdev_printk(KERN_INFO, sdev, "%s: stpg failed\n",
Hannes Reineckeb2460752016-02-19 09:17:00 +0100674 ALUA_DH_NAME);
675 scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr);
676 }
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100677 }
Hannes Reineckeb2460752016-02-19 09:17:00 +0100678 /* Retry RTPG */
679 return SCSI_DH_RETRY;
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100680}
681
Hannes Reinecke03197b62016-02-19 09:17:10 +0100682static 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 Reinecke00642a12016-02-19 09:17:11 +0100691 struct workqueue_struct *alua_wq = kaluad_wq;
Hannes Reinecke03197b62016-02-19 09:17:10 +0100692
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 Reinecke00642a12016-02-19 09:17:11 +0100701 if (pg->flags & ALUA_SYNC_STPG)
702 alua_wq = kaluad_sync_wq;
Hannes Reinecke03197b62016-02-19 09:17:10 +0100703 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 Reinecke00642a12016-02-19 09:17:11 +0100713 queue_delayed_work(alua_wq, &pg->rtpg_work,
Hannes Reinecke03197b62016-02-19 09:17:10 +0100714 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 Reinecke00642a12016-02-19 09:17:11 +0100730 queue_delayed_work(alua_wq, &pg->rtpg_work,
Hannes Reinecke03197b62016-02-19 09:17:10 +0100731 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
753static 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 Reinecke00642a12016-02-19 09:17:11 +0100759 struct workqueue_struct *alua_wq = kaluad_wq;
Hannes Reinecke03197b62016-02-19 09:17:10 +0100760
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 Reinecke00642a12016-02-19 09:17:11 +0100777 if (pg->flags & ALUA_SYNC_STPG)
778 alua_wq = kaluad_sync_wq;
Hannes Reinecke03197b62016-02-19 09:17:10 +0100779 spin_unlock_irqrestore(&pg->lock, flags);
780
781 if (start_queue &&
Hannes Reinecke00642a12016-02-19 09:17:11 +0100782 !queue_delayed_work(alua_wq, &pg->rtpg_work,
Hannes Reinecke03197b62016-02-19 09:17:10 +0100783 msecs_to_jiffies(ALUA_RTPG_DELAY_MSECS))) {
784 scsi_device_put(sdev);
785 kref_put(&pg->kref, release_port_group);
786 }
787}
788
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100789/*
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700790 * 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 */
796static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h)
797{
Hannes Reinecke43394c62016-02-19 09:17:04 +0100798 int err = SCSI_DH_DEV_UNSUPP, tpgs;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700799
Hannes Reinecke03197b62016-02-19 09:17:10 +0100800 mutex_lock(&h->init_mutex);
Hannes Reinecke43394c62016-02-19 09:17:04 +0100801 tpgs = alua_check_tpgs(sdev);
Hannes Reineckea4253fd2016-02-19 09:17:06 +0100802 if (tpgs != TPGS_MODE_NONE)
803 err = alua_check_vpd(sdev, h, tpgs);
Hannes Reinecke03197b62016-02-19 09:17:10 +0100804 h->init_error = err;
805 mutex_unlock(&h->init_mutex);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700806 return err;
807}
Moger, Babu4335d092012-03-27 20:55:49 +0000808/*
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 */
817static int alua_set_params(struct scsi_device *sdev, const char *params)
818{
Christoph Hellwigee14c672015-08-27 14:16:59 +0200819 struct alua_dh_data *h = sdev->handler_data;
Hannes Reinecke03197b62016-02-19 09:17:10 +0100820 struct alua_port_group __rcu *pg = NULL;
Moger, Babu4335d092012-03-27 20:55:49 +0000821 unsigned int optimize = 0, argc;
822 const char *p = params;
823 int result = SCSI_DH_OK;
Hannes Reinecke03197b62016-02-19 09:17:10 +0100824 unsigned long flags;
Moger, Babu4335d092012-03-27 20:55:49 +0000825
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 Reinecke03197b62016-02-19 09:17:10 +0100834 rcu_read_lock();
835 pg = rcu_dereference(h->pg);
836 if (!pg) {
837 rcu_read_unlock();
Hannes Reinecke43394c62016-02-19 09:17:04 +0100838 return -ENXIO;
Hannes Reinecke03197b62016-02-19 09:17:10 +0100839 }
840 spin_lock_irqsave(&pg->lock, flags);
Moger, Babu4335d092012-03-27 20:55:49 +0000841 if (optimize)
Hannes Reinecke43394c62016-02-19 09:17:04 +0100842 pg->flags |= ALUA_OPTIMIZE_STPG;
Moger, Babu4335d092012-03-27 20:55:49 +0000843 else
Hannes Reinecke43394c62016-02-19 09:17:04 +0100844 pg->flags &= ~ALUA_OPTIMIZE_STPG;
Hannes Reinecke03197b62016-02-19 09:17:10 +0100845 spin_unlock_irqrestore(&pg->lock, flags);
846 rcu_read_unlock();
Moger, Babu4335d092012-03-27 20:55:49 +0000847
848 return result;
849}
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700850
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 Seetharaman3ae31f62009-10-21 09:22:46 -0700861static int alua_activate(struct scsi_device *sdev,
862 activate_complete fn, void *data)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700863{
Christoph Hellwigee14c672015-08-27 14:16:59 +0200864 struct alua_dh_data *h = sdev->handler_data;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700865 int err = SCSI_DH_OK;
Hannes Reinecke03197b62016-02-19 09:17:10 +0100866 struct alua_queue_data *qdata;
867 struct alua_port_group __rcu *pg;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700868
Hannes Reinecke03197b62016-02-19 09:17:10 +0100869 qdata = kzalloc(sizeof(*qdata), GFP_KERNEL);
870 if (!qdata) {
871 err = SCSI_DH_RES_TEMP_UNAVAIL;
Hannes Reinecke43394c62016-02-19 09:17:04 +0100872 goto out;
873 }
Hannes Reinecke03197b62016-02-19 09:17:10 +0100874 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 Reinecke057ea7c2008-07-17 16:53:21 -0700893out:
Hannes Reineckeb2460752016-02-19 09:17:00 +0100894 if (fn)
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700895 fn(data, err);
896 return 0;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700897}
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 */
905static int alua_prep_fn(struct scsi_device *sdev, struct request *req)
906{
Christoph Hellwigee14c672015-08-27 14:16:59 +0200907 struct alua_dh_data *h = sdev->handler_data;
Hannes Reinecke03197b62016-02-19 09:17:10 +0100908 struct alua_port_group __rcu *pg;
909 int state = TPGS_STATE_OPTIMIZED;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700910 int ret = BLKPREP_OK;
911
Hannes Reinecke03197b62016-02-19 09:17:10 +0100912 rcu_read_lock();
913 pg = rcu_dereference(h->pg);
914 if (pg)
915 state = pg->state;
916 rcu_read_unlock();
Hannes Reinecke43394c62016-02-19 09:17:04 +0100917 if (state == TPGS_STATE_TRANSITIONING)
Hannes Reinecke69723d12010-09-24 15:57:04 +0200918 ret = BLKPREP_DEFER;
Hannes Reinecke43394c62016-02-19 09:17:04 +0100919 else if (state != TPGS_STATE_OPTIMIZED &&
920 state != TPGS_STATE_NONOPTIMIZED &&
921 state != TPGS_STATE_LBA_DEPENDENT) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700922 ret = BLKPREP_KILL;
923 req->cmd_flags |= REQ_QUIET;
924 }
925 return ret;
926
927}
928
Christoph Hellwig1d520322014-09-14 11:08:21 -0700929/*
930 * alua_bus_attach - Attach device handler
931 * @sdev: device to be attached to
932 */
Christoph Hellwigee14c672015-08-27 14:16:59 +0200933static int alua_bus_attach(struct scsi_device *sdev)
Christoph Hellwig1d520322014-09-14 11:08:21 -0700934{
935 struct alua_dh_data *h;
Hannes Reinecke43394c62016-02-19 09:17:04 +0100936 int err, ret = -EINVAL;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700937
938 h = kzalloc(sizeof(*h) , GFP_KERNEL);
939 if (!h)
Christoph Hellwigee14c672015-08-27 14:16:59 +0200940 return -ENOMEM;
Hannes Reinecke03197b62016-02-19 09:17:10 +0100941 spin_lock_init(&h->pg_lock);
942 rcu_assign_pointer(h->pg, NULL);
943 h->init_error = SCSI_DH_OK;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700944 h->sdev = sdev;
945
Hannes Reinecke03197b62016-02-19 09:17:10 +0100946 mutex_init(&h->init_mutex);
Christoph Hellwig1d520322014-09-14 11:08:21 -0700947 err = alua_initialize(sdev, h);
Hannes Reinecke43394c62016-02-19 09:17:04 +0100948 if (err == SCSI_DH_NOMEM)
949 ret = -ENOMEM;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700950 if (err != SCSI_DH_OK && err != SCSI_DH_DEV_OFFLINED)
951 goto failed;
952
Christoph Hellwigee14c672015-08-27 14:16:59 +0200953 sdev->handler_data = h;
954 return 0;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700955failed:
956 kfree(h);
Hannes Reinecke43394c62016-02-19 09:17:04 +0100957 return ret;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700958}
959
960/*
961 * alua_bus_detach - Detach device handler
962 * @sdev: device to be detached from
963 */
964static void alua_bus_detach(struct scsi_device *sdev)
965{
Christoph Hellwigee14c672015-08-27 14:16:59 +0200966 struct alua_dh_data *h = sdev->handler_data;
Hannes Reinecke03197b62016-02-19 09:17:10 +0100967 struct alua_port_group *pg;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700968
Hannes Reinecke03197b62016-02-19 09:17:10 +0100969 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 Hellwigee14c672015-08-27 14:16:59 +0200977 sdev->handler_data = NULL;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700978 kfree(h);
979}
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700980
981static struct scsi_device_handler alua_dh = {
982 .name = ALUA_DH_NAME,
983 .module = THIS_MODULE,
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700984 .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, Babu4335d092012-03-27 20:55:49 +0000989 .set_params = alua_set_params,
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700990};
991
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700992static int __init alua_init(void)
993{
994 int r;
995
Hannes Reinecke03197b62016-02-19 09:17:10 +0100996 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 Reinecke00642a12016-02-19 09:17:11 +01001001 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 Reinecke057ea7c2008-07-17 16:53:21 -07001006 r = scsi_register_device_handler(&alua_dh);
Hannes Reinecke03197b62016-02-19 09:17:10 +01001007 if (r != 0) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -07001008 printk(KERN_ERR "%s: Failed to register scsi device handler",
1009 ALUA_DH_NAME);
Hannes Reinecke00642a12016-02-19 09:17:11 +01001010 destroy_workqueue(kaluad_sync_wq);
Hannes Reinecke03197b62016-02-19 09:17:10 +01001011 destroy_workqueue(kaluad_wq);
1012 }
Hannes Reinecke057ea7c2008-07-17 16:53:21 -07001013 return r;
1014}
1015
1016static void __exit alua_exit(void)
1017{
1018 scsi_unregister_device_handler(&alua_dh);
Hannes Reinecke00642a12016-02-19 09:17:11 +01001019 destroy_workqueue(kaluad_sync_wq);
Hannes Reinecke03197b62016-02-19 09:17:10 +01001020 destroy_workqueue(kaluad_wq);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -07001021}
1022
1023module_init(alua_init);
1024module_exit(alua_exit);
1025
1026MODULE_DESCRIPTION("DM Multipath ALUA support");
1027MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
1028MODULE_LICENSE("GPL");
1029MODULE_VERSION(ALUA_DH_VER);