blob: 73df60e83e4d552c4d81c805384d6f0857630569 [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
59#define ALUA_INQUIRY_SIZE 36
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
62
Hannes Reinecke6c4fc042015-12-01 10:16:51 +010063/* device handler flags */
Moger, Babu4335d092012-03-27 20:55:49 +000064#define ALUA_OPTIMIZE_STPG 1
Hannes Reinecke6c4fc042015-12-01 10:16:51 +010065#define ALUA_RTPG_EXT_HDR_UNSUPP 2
Moger, Babu4335d092012-03-27 20:55:49 +000066
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070067struct alua_dh_data {
68 int group_id;
69 int rel_port;
70 int tpgs;
71 int state;
Moger, Babudcd3a752012-03-27 20:56:08 +000072 int pref;
Moger, Babu4335d092012-03-27 20:55:49 +000073 unsigned flags; /* used for optimizing STPG */
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070074 unsigned char inq[ALUA_INQUIRY_SIZE];
75 unsigned char *buff;
76 int bufflen;
Rob Evers3588c5a2012-05-18 14:08:54 -040077 unsigned char transition_tmo;
Chandra Seetharaman96e65862009-10-21 09:23:04 -070078 struct scsi_device *sdev;
79 activate_complete callback_fn;
80 void *callback_data;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070081};
82
83#define ALUA_POLICY_SWITCH_CURRENT 0
84#define ALUA_POLICY_SWITCH_ALL 1
85
Chandra Seetharaman96e65862009-10-21 09:23:04 -070086static char print_alua_state(int);
Chandra Seetharaman96e65862009-10-21 09:23:04 -070087
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070088static int realloc_buffer(struct alua_dh_data *h, unsigned len)
89{
90 if (h->buff && h->buff != h->inq)
91 kfree(h->buff);
92
93 h->buff = kmalloc(len, GFP_NOIO);
94 if (!h->buff) {
95 h->buff = h->inq;
96 h->bufflen = ALUA_INQUIRY_SIZE;
97 return 1;
98 }
99 h->bufflen = len;
100 return 0;
101}
102
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700103/*
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700104 * submit_rtpg - Issue a REPORT TARGET GROUP STATES command
105 * @sdev: sdev the command should be sent to
106 */
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100107static int submit_rtpg(struct scsi_device *sdev, unsigned char *buff,
108 int bufflen, struct scsi_sense_hdr *sshdr, int flags)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700109{
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100110 u8 cdb[COMMAND_SIZE(MAINTENANCE_IN)];
111 int req_flags = REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
112 REQ_FAILFAST_DRIVER;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700113
114 /* Prepare the command. */
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100115 memset(cdb, 0x0, COMMAND_SIZE(MAINTENANCE_IN));
116 cdb[0] = MAINTENANCE_IN;
Hannes Reinecked42ae5f2016-02-19 09:16:58 +0100117 if (!(flags & ALUA_RTPG_EXT_HDR_UNSUPP))
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100118 cdb[1] = MI_REPORT_TARGET_PGS | MI_EXT_HDR_PARAM_FMT;
Rob Evers8e67ce62012-05-18 14:08:55 -0400119 else
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100120 cdb[1] = MI_REPORT_TARGET_PGS;
121 put_unaligned_be32(bufflen, &cdb[6]);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700122
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100123 return scsi_execute_req_flags(sdev, cdb, DMA_FROM_DEVICE,
124 buff, bufflen, sshdr,
125 ALUA_FAILOVER_TIMEOUT * HZ,
126 ALUA_FAILOVER_RETRIES, NULL, req_flags);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700127}
128
129/*
Hannes Reineckeb2460752016-02-19 09:17:00 +0100130 * submit_stpg - Issue a SET TARGET PORT GROUP command
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700131 *
132 * Currently we're only setting the current target port group state
133 * to 'active/optimized' and let the array firmware figure out
134 * the states of the remaining groups.
135 */
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100136static int submit_stpg(struct scsi_device *sdev, int group_id,
137 struct scsi_sense_hdr *sshdr)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700138{
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100139 u8 cdb[COMMAND_SIZE(MAINTENANCE_OUT)];
Hannes Reineckeb2460752016-02-19 09:17:00 +0100140 unsigned char stpg_data[8];
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700141 int stpg_len = 8;
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100142 int req_flags = REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
143 REQ_FAILFAST_DRIVER;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700144
145 /* Prepare the data buffer */
Hannes Reineckeb2460752016-02-19 09:17:00 +0100146 memset(stpg_data, 0, stpg_len);
147 stpg_data[4] = TPGS_STATE_OPTIMIZED & 0x0f;
148 put_unaligned_be16(group_id, &stpg_data[6]);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700149
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700150 /* Prepare the command. */
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100151 memset(cdb, 0x0, COMMAND_SIZE(MAINTENANCE_OUT));
152 cdb[0] = MAINTENANCE_OUT;
153 cdb[1] = MO_SET_TARGET_PGS;
154 put_unaligned_be32(stpg_len, &cdb[6]);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700155
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100156 return scsi_execute_req_flags(sdev, cdb, DMA_TO_DEVICE,
157 stpg_data, stpg_len,
158 sshdr, ALUA_FAILOVER_TIMEOUT * HZ,
159 ALUA_FAILOVER_RETRIES, NULL, req_flags);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700160}
161
162/*
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200163 * alua_check_tpgs - Evaluate TPGS setting
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700164 * @sdev: device to be checked
165 *
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200166 * Examine the TPGS setting of the sdev to find out if ALUA
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700167 * is supported.
168 */
Hannes Reineckead0ea642015-12-01 10:16:53 +0100169static int alua_check_tpgs(struct scsi_device *sdev)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700170{
Hannes Reineckead0ea642015-12-01 10:16:53 +0100171 int tpgs = TPGS_MODE_NONE;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700172
Hannes Reineckedb5a6a62015-12-01 10:16:43 +0100173 /*
174 * ALUA support for non-disk devices is fraught with
175 * difficulties, so disable it for now.
176 */
177 if (sdev->type != TYPE_DISK) {
Hannes Reineckedb5a6a62015-12-01 10:16:43 +0100178 sdev_printk(KERN_INFO, sdev,
179 "%s: disable for non-disk devices\n",
180 ALUA_DH_NAME);
Hannes Reineckead0ea642015-12-01 10:16:53 +0100181 return tpgs;
Hannes Reineckedb5a6a62015-12-01 10:16:43 +0100182 }
183
Hannes Reineckead0ea642015-12-01 10:16:53 +0100184 tpgs = scsi_device_tpgs(sdev);
185 switch (tpgs) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700186 case TPGS_MODE_EXPLICIT|TPGS_MODE_IMPLICIT:
187 sdev_printk(KERN_INFO, sdev,
188 "%s: supports implicit and explicit TPGS\n",
189 ALUA_DH_NAME);
190 break;
191 case TPGS_MODE_EXPLICIT:
192 sdev_printk(KERN_INFO, sdev, "%s: supports explicit TPGS\n",
193 ALUA_DH_NAME);
194 break;
195 case TPGS_MODE_IMPLICIT:
196 sdev_printk(KERN_INFO, sdev, "%s: supports implicit TPGS\n",
197 ALUA_DH_NAME);
198 break;
Hannes Reinecke6cc05d42015-12-01 10:16:45 +0100199 case TPGS_MODE_NONE:
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700200 sdev_printk(KERN_INFO, sdev, "%s: not supported\n",
201 ALUA_DH_NAME);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700202 break;
Hannes Reinecke6cc05d42015-12-01 10:16:45 +0100203 default:
204 sdev_printk(KERN_INFO, sdev,
205 "%s: unsupported TPGS setting %d\n",
Hannes Reineckead0ea642015-12-01 10:16:53 +0100206 ALUA_DH_NAME, tpgs);
207 tpgs = TPGS_MODE_NONE;
Hannes Reinecke6cc05d42015-12-01 10:16:45 +0100208 break;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700209 }
210
Hannes Reineckead0ea642015-12-01 10:16:53 +0100211 return tpgs;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700212}
213
214/*
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100215 * alua_check_vpd - Evaluate INQUIRY vpd page 0x83
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700216 * @sdev: device to be checked
217 *
218 * Extract the relative target port and the target port group
219 * descriptor from the list of identificators.
220 */
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100221static int alua_check_vpd(struct scsi_device *sdev, struct alua_dh_data *h)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700222{
Hannes Reinecke83ea0e52015-12-01 10:16:58 +0100223 int rel_port = -1, group_id;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700224
Hannes Reinecke83ea0e52015-12-01 10:16:58 +0100225 group_id = scsi_vpd_tpg_id(sdev, &rel_port);
226 if (group_id < 0) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700227 /*
228 * Internal error; TPGS supported but required
229 * VPD identification descriptors not present.
230 * Disable ALUA support
231 */
232 sdev_printk(KERN_INFO, sdev,
233 "%s: No target port descriptors found\n",
234 ALUA_DH_NAME);
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100235 return SCSI_DH_DEV_UNSUPP;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700236 }
Hannes Reinecke83ea0e52015-12-01 10:16:58 +0100237 h->state = TPGS_STATE_OPTIMIZED;
238 h->group_id = group_id;
239
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100240 sdev_printk(KERN_INFO, sdev,
241 "%s: port group %02x rel port %02x\n",
242 ALUA_DH_NAME, h->group_id, h->rel_port);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700243
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100244 return 0;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700245}
246
247static char print_alua_state(int state)
248{
249 switch (state) {
250 case TPGS_STATE_OPTIMIZED:
251 return 'A';
252 case TPGS_STATE_NONOPTIMIZED:
253 return 'N';
254 case TPGS_STATE_STANDBY:
255 return 'S';
256 case TPGS_STATE_UNAVAILABLE:
257 return 'U';
Hannes Reinecke69723d12010-09-24 15:57:04 +0200258 case TPGS_STATE_LBA_DEPENDENT:
259 return 'L';
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700260 case TPGS_STATE_OFFLINE:
261 return 'O';
262 case TPGS_STATE_TRANSITIONING:
263 return 'T';
264 default:
265 return 'X';
266 }
267}
268
269static int alua_check_sense(struct scsi_device *sdev,
270 struct scsi_sense_hdr *sense_hdr)
271{
272 switch (sense_hdr->sense_key) {
273 case NOT_READY:
274 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0a)
275 /*
276 * LUN Not Accessible - ALUA state transition
277 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700278 return ADD_TO_MLQUEUE;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700279 break;
280 case UNIT_ATTENTION:
281 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
282 /*
283 * Power On, Reset, or Bus Device Reset, just retry.
284 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700285 return ADD_TO_MLQUEUE;
Stewart, Seanc20ee7b2013-10-15 15:52:39 +0000286 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x04)
287 /*
288 * Device internal reset
289 */
290 return ADD_TO_MLQUEUE;
Moger, Babu410f02d2011-12-21 18:01:37 -0500291 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x01)
292 /*
293 * Mode Parameters Changed
294 */
295 return ADD_TO_MLQUEUE;
Moger, Babubf819732011-10-06 13:22:07 -0400296 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700297 /*
298 * ALUA state changed
299 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700300 return ADD_TO_MLQUEUE;
Moger, Babubf819732011-10-06 13:22:07 -0400301 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700302 /*
303 * Implicit ALUA state transition failed
304 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700305 return ADD_TO_MLQUEUE;
Moger, Babubf819732011-10-06 13:22:07 -0400306 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x03)
307 /*
308 * Inquiry data has changed
309 */
310 return ADD_TO_MLQUEUE;
311 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x0e)
Ilgu Hong4d086f62009-01-30 17:00:11 -0600312 /*
313 * REPORTED_LUNS_DATA_HAS_CHANGED is reported
314 * when switching controllers on targets like
315 * Intel Multi-Flex. We can just retry.
316 */
317 return ADD_TO_MLQUEUE;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700318 break;
319 }
320
321 return SCSI_RETURN_NOT_HANDLED;
322}
323
324/*
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700325 * alua_rtpg - Evaluate REPORT TARGET GROUP STATES
326 * @sdev: the device to be evaluated.
Stewart, Seana8e5a2d2013-10-15 15:52:54 +0000327 * @wait_for_transition: if nonzero, wait ALUA_FAILOVER_TIMEOUT seconds for device to exit transitioning state
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700328 *
329 * Evaluate the Target Port Group State.
330 * Returns SCSI_DH_DEV_OFFLINED if the path is
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300331 * found to be unusable.
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700332 */
Stewart, Seana8e5a2d2013-10-15 15:52:54 +0000333static int alua_rtpg(struct scsi_device *sdev, struct alua_dh_data *h, int wait_for_transition)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700334{
335 struct scsi_sense_hdr sense_hdr;
336 int len, k, off, valid_states = 0;
Hannes Reineckecfde3fa2011-08-24 10:51:18 +0200337 unsigned char *ucp;
Hannes Reinecke5597caf2015-12-01 10:16:48 +0100338 unsigned err, retval;
Rob Eversbc97f4b2012-05-18 14:08:56 -0400339 unsigned long expiry, interval = 0;
Rob Evers3588c5a2012-05-18 14:08:54 -0400340 unsigned int tpg_desc_tbl_off;
341 unsigned char orig_transition_tmo;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700342
Rob Evers3588c5a2012-05-18 14:08:54 -0400343 if (!h->transition_tmo)
344 expiry = round_jiffies_up(jiffies + ALUA_FAILOVER_TIMEOUT * HZ);
345 else
346 expiry = round_jiffies_up(jiffies + h->transition_tmo * HZ);
347
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700348 retry:
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100349 retval = submit_rtpg(sdev, h->buff, h->bufflen, &sense_hdr, h->flags);
350
Hannes Reinecke5597caf2015-12-01 10:16:48 +0100351 if (retval) {
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100352 if (!scsi_sense_valid(&sense_hdr)) {
Hannes Reinecke5597caf2015-12-01 10:16:48 +0100353 sdev_printk(KERN_INFO, sdev,
354 "%s: rtpg failed, result %d\n",
355 ALUA_DH_NAME, retval);
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100356 if (driver_byte(retval) == DRIVER_ERROR)
Hannes Reinecke5597caf2015-12-01 10:16:48 +0100357 return SCSI_DH_DEV_TEMP_BUSY;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700358 return SCSI_DH_IO;
Hannes Reinecke5597caf2015-12-01 10:16:48 +0100359 }
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700360
Rob Evers8e67ce62012-05-18 14:08:55 -0400361 /*
362 * submit_rtpg() has failed on existing arrays
363 * when requesting extended header info, and
364 * the array doesn't support extended headers,
365 * even though it shouldn't according to T10.
366 * The retry without rtpg_ext_hdr_req set
367 * handles this.
368 */
Hannes Reinecke6c4fc042015-12-01 10:16:51 +0100369 if (!(h->flags & ALUA_RTPG_EXT_HDR_UNSUPP) &&
Rob Evers8e67ce62012-05-18 14:08:55 -0400370 sense_hdr.sense_key == ILLEGAL_REQUEST &&
371 sense_hdr.asc == 0x24 && sense_hdr.ascq == 0) {
Hannes Reinecke6c4fc042015-12-01 10:16:51 +0100372 h->flags |= ALUA_RTPG_EXT_HDR_UNSUPP;
Rob Evers8e67ce62012-05-18 14:08:55 -0400373 goto retry;
374 }
Hannes Reineckee2d817d2015-12-01 10:16:54 +0100375 /*
376 * Retry on ALUA state transition or if any
377 * UNIT ATTENTION occurred.
378 */
379 if (sense_hdr.sense_key == NOT_READY &&
380 sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x0a)
381 err = SCSI_DH_RETRY;
382 else if (sense_hdr.sense_key == UNIT_ATTENTION)
383 err = SCSI_DH_RETRY;
384 if (err == SCSI_DH_RETRY && time_before(jiffies, expiry)) {
Hannes Reinecke80bd68d2015-12-01 10:16:47 +0100385 sdev_printk(KERN_ERR, sdev, "%s: rtpg retry\n",
386 ALUA_DH_NAME);
387 scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700388 goto retry;
Hannes Reinecke80bd68d2015-12-01 10:16:47 +0100389 }
390 sdev_printk(KERN_ERR, sdev, "%s: rtpg failed\n",
391 ALUA_DH_NAME);
392 scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr);
393 return SCSI_DH_IO;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700394 }
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700395
Hannes Reineckea7089772015-12-01 10:16:52 +0100396 len = get_unaligned_be32(&h->buff[0]) + 4;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700397
398 if (len > h->bufflen) {
399 /* Resubmit with the correct length */
400 if (realloc_buffer(h, len)) {
401 sdev_printk(KERN_WARNING, sdev,
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700402 "%s: kmalloc buffer failed\n",__func__);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700403 /* Temporary failure, bypass */
404 return SCSI_DH_DEV_TEMP_BUSY;
405 }
406 goto retry;
407 }
408
Rob Evers3588c5a2012-05-18 14:08:54 -0400409 orig_transition_tmo = h->transition_tmo;
410 if ((h->buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR && h->buff[5] != 0)
411 h->transition_tmo = h->buff[5];
412 else
413 h->transition_tmo = ALUA_FAILOVER_TIMEOUT;
414
Stewart, Seana8e5a2d2013-10-15 15:52:54 +0000415 if (wait_for_transition && (orig_transition_tmo != h->transition_tmo)) {
Rob Evers3588c5a2012-05-18 14:08:54 -0400416 sdev_printk(KERN_INFO, sdev,
417 "%s: transition timeout set to %d seconds\n",
418 ALUA_DH_NAME, h->transition_tmo);
419 expiry = jiffies + h->transition_tmo * HZ;
420 }
421
422 if ((h->buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR)
423 tpg_desc_tbl_off = 8;
424 else
425 tpg_desc_tbl_off = 4;
426
427 for (k = tpg_desc_tbl_off, ucp = h->buff + tpg_desc_tbl_off;
428 k < len;
429 k += off, ucp += off) {
430
Hannes Reineckea7089772015-12-01 10:16:52 +0100431 if (h->group_id == get_unaligned_be16(&ucp[2])) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700432 h->state = ucp[0] & 0x0f;
Moger, Babudcd3a752012-03-27 20:56:08 +0000433 h->pref = ucp[0] >> 7;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700434 valid_states = ucp[1];
435 }
436 off = 8 + (ucp[7] * 4);
437 }
438
439 sdev_printk(KERN_INFO, sdev,
Moger, Babudcd3a752012-03-27 20:56:08 +0000440 "%s: port group %02x state %c %s supports %c%c%c%c%c%c%c\n",
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700441 ALUA_DH_NAME, h->group_id, print_alua_state(h->state),
Moger, Babudcd3a752012-03-27 20:56:08 +0000442 h->pref ? "preferred" : "non-preferred",
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700443 valid_states&TPGS_SUPPORT_TRANSITION?'T':'t',
444 valid_states&TPGS_SUPPORT_OFFLINE?'O':'o',
Hannes Reinecke69723d12010-09-24 15:57:04 +0200445 valid_states&TPGS_SUPPORT_LBA_DEPENDENT?'L':'l',
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700446 valid_states&TPGS_SUPPORT_UNAVAILABLE?'U':'u',
447 valid_states&TPGS_SUPPORT_STANDBY?'S':'s',
448 valid_states&TPGS_SUPPORT_NONOPTIMIZED?'N':'n',
449 valid_states&TPGS_SUPPORT_OPTIMIZED?'A':'a');
450
Hannes Reinecke69723d12010-09-24 15:57:04 +0200451 switch (h->state) {
452 case TPGS_STATE_TRANSITIONING:
Stewart, Seana8e5a2d2013-10-15 15:52:54 +0000453 if (wait_for_transition) {
454 if (time_before(jiffies, expiry)) {
455 /* State transition, retry */
456 interval += 2000;
457 msleep(interval);
458 goto retry;
459 }
460 err = SCSI_DH_RETRY;
461 } else {
462 err = SCSI_DH_OK;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700463 }
Stewart, Seana8e5a2d2013-10-15 15:52:54 +0000464
Hannes Reinecke69723d12010-09-24 15:57:04 +0200465 /* Transitioning time exceeded, set port to standby */
Hannes Reinecke69723d12010-09-24 15:57:04 +0200466 h->state = TPGS_STATE_STANDBY;
467 break;
468 case TPGS_STATE_OFFLINE:
Bart Van Asschee47f8972012-08-24 09:08:41 +0000469 /* Path unusable */
Hannes Reinecke69723d12010-09-24 15:57:04 +0200470 err = SCSI_DH_DEV_OFFLINED;
471 break;
472 default:
473 /* Useable path if active */
474 err = SCSI_DH_OK;
475 break;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700476 }
477 return err;
478}
479
480/*
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100481 * alua_stpg - Issue a SET TARGET PORT GROUP command
482 *
483 * Issue a SET TARGET PORT GROUP command and evaluate the
Hannes Reineckeb2460752016-02-19 09:17:00 +0100484 * response. Returns SCSI_DH_RETRY per default to trigger
485 * a re-evaluation of the target group state or SCSI_DH_OK
486 * if no further action needs to be taken.
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100487 */
Hannes Reineckeb2460752016-02-19 09:17:00 +0100488static unsigned alua_stpg(struct scsi_device *sdev, struct alua_dh_data *h)
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100489{
Hannes Reineckeb2460752016-02-19 09:17:00 +0100490 int retval;
491 struct scsi_sense_hdr sense_hdr;
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100492
Hannes Reineckeb2460752016-02-19 09:17:00 +0100493 if (!(h->tpgs & TPGS_MODE_EXPLICIT)) {
494 /* Only implicit ALUA supported, retry */
495 return SCSI_DH_RETRY;
496 }
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100497 switch (h->state) {
Hannes Reineckeb2460752016-02-19 09:17:00 +0100498 case TPGS_STATE_OPTIMIZED:
499 return SCSI_DH_OK;
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100500 case TPGS_STATE_NONOPTIMIZED:
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100501 if ((h->flags & ALUA_OPTIMIZE_STPG) &&
502 !h->pref &&
503 (h->tpgs & TPGS_MODE_IMPLICIT))
Hannes Reineckeb2460752016-02-19 09:17:00 +0100504 return SCSI_DH_OK;
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100505 break;
506 case TPGS_STATE_STANDBY:
507 case TPGS_STATE_UNAVAILABLE:
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100508 break;
509 case TPGS_STATE_OFFLINE:
Hannes Reineckeb2460752016-02-19 09:17:00 +0100510 return SCSI_DH_IO;
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100511 case TPGS_STATE_TRANSITIONING:
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100512 break;
513 default:
Hannes Reineckeb2460752016-02-19 09:17:00 +0100514 sdev_printk(KERN_INFO, sdev,
515 "%s: stpg failed, unhandled TPGS state %d",
516 ALUA_DH_NAME, h->state);
517 return SCSI_DH_NOSYS;
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100518 }
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100519 retval = submit_stpg(sdev, h->group_id, &sense_hdr);
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100520
Hannes Reineckeb2460752016-02-19 09:17:00 +0100521 if (retval) {
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100522 if (!scsi_sense_valid(&sense_hdr)) {
Hannes Reineckeb2460752016-02-19 09:17:00 +0100523 sdev_printk(KERN_INFO, sdev,
524 "%s: stpg failed, result %d",
525 ALUA_DH_NAME, retval);
Hannes Reinecke40bb61a2016-02-19 09:17:02 +0100526 if (driver_byte(retval) == DRIVER_ERROR)
Hannes Reineckeb2460752016-02-19 09:17:00 +0100527 return SCSI_DH_DEV_TEMP_BUSY;
528 } else {
529 sdev_printk(KERN_INFO, h->sdev, "%s: stpg failed\n",
530 ALUA_DH_NAME);
531 scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr);
532 }
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100533 }
Hannes Reineckeb2460752016-02-19 09:17:00 +0100534 /* Retry RTPG */
535 return SCSI_DH_RETRY;
Hannes Reineckef2ecf132016-02-19 09:16:59 +0100536}
537
538/*
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700539 * alua_initialize - Initialize ALUA state
540 * @sdev: the device to be initialized
541 *
542 * For the prep_fn to work correctly we have
543 * to initialize the ALUA state for the device.
544 */
545static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h)
546{
Hannes Reineckead0ea642015-12-01 10:16:53 +0100547 int err = SCSI_DH_DEV_UNSUPP;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700548
Hannes Reineckead0ea642015-12-01 10:16:53 +0100549 h->tpgs = alua_check_tpgs(sdev);
550 if (h->tpgs == TPGS_MODE_NONE)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700551 goto out;
552
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100553 err = alua_check_vpd(sdev, h);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700554 if (err != SCSI_DH_OK)
555 goto out;
556
Stewart, Seana8e5a2d2013-10-15 15:52:54 +0000557 err = alua_rtpg(sdev, h, 0);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700558 if (err != SCSI_DH_OK)
559 goto out;
560
561out:
562 return err;
563}
Moger, Babu4335d092012-03-27 20:55:49 +0000564/*
565 * alua_set_params - set/unset the optimize flag
566 * @sdev: device on the path to be activated
567 * params - parameters in the following format
568 * "no_of_params\0param1\0param2\0param3\0...\0"
569 * For example, to set the flag pass the following parameters
570 * from multipath.conf
571 * hardware_handler "2 alua 1"
572 */
573static int alua_set_params(struct scsi_device *sdev, const char *params)
574{
Christoph Hellwigee14c672015-08-27 14:16:59 +0200575 struct alua_dh_data *h = sdev->handler_data;
Moger, Babu4335d092012-03-27 20:55:49 +0000576 unsigned int optimize = 0, argc;
577 const char *p = params;
578 int result = SCSI_DH_OK;
579
580 if ((sscanf(params, "%u", &argc) != 1) || (argc != 1))
581 return -EINVAL;
582
583 while (*p++)
584 ;
585 if ((sscanf(p, "%u", &optimize) != 1) || (optimize > 1))
586 return -EINVAL;
587
588 if (optimize)
589 h->flags |= ALUA_OPTIMIZE_STPG;
590 else
591 h->flags &= ~ALUA_OPTIMIZE_STPG;
592
593 return result;
594}
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700595
Stewart, Sean7a3ad392013-04-04 14:54:47 +0000596static uint optimize_stpg;
597module_param(optimize_stpg, uint, S_IRUGO|S_IWUSR);
598MODULE_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.");
599
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700600/*
601 * alua_activate - activate a path
602 * @sdev: device on the path to be activated
603 *
604 * We're currently switching the port group to be activated only and
605 * let the array figure out the rest.
606 * There may be other arrays which require us to switch all port groups
607 * based on a certain policy. But until we actually encounter them it
608 * should be okay.
609 */
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700610static int alua_activate(struct scsi_device *sdev,
611 activate_complete fn, void *data)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700612{
Christoph Hellwigee14c672015-08-27 14:16:59 +0200613 struct alua_dh_data *h = sdev->handler_data;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700614 int err = SCSI_DH_OK;
615
Stewart, Seana8e5a2d2013-10-15 15:52:54 +0000616 err = alua_rtpg(sdev, h, 1);
Hannes Reinecke46ccf6b2011-08-24 10:51:16 +0200617 if (err != SCSI_DH_OK)
618 goto out;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700619
Stewart, Sean7a3ad392013-04-04 14:54:47 +0000620 if (optimize_stpg)
621 h->flags |= ALUA_OPTIMIZE_STPG;
622
Hannes Reineckeb2460752016-02-19 09:17:00 +0100623 err = alua_stpg(sdev, h);
Hannes Reineckedd5cc402016-02-19 09:17:01 +0100624 if (err == SCSI_DH_RETRY)
625 err = alua_rtpg(sdev, h, 1);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700626out:
Hannes Reineckeb2460752016-02-19 09:17:00 +0100627 if (fn)
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700628 fn(data, err);
629 return 0;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700630}
631
632/*
633 * alua_prep_fn - request callback
634 *
635 * Fail I/O to all paths not in state
636 * active/optimized or active/non-optimized.
637 */
638static int alua_prep_fn(struct scsi_device *sdev, struct request *req)
639{
Christoph Hellwigee14c672015-08-27 14:16:59 +0200640 struct alua_dh_data *h = sdev->handler_data;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700641 int ret = BLKPREP_OK;
642
Hannes Reinecke69723d12010-09-24 15:57:04 +0200643 if (h->state == TPGS_STATE_TRANSITIONING)
644 ret = BLKPREP_DEFER;
645 else if (h->state != TPGS_STATE_OPTIMIZED &&
646 h->state != TPGS_STATE_NONOPTIMIZED &&
647 h->state != TPGS_STATE_LBA_DEPENDENT) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700648 ret = BLKPREP_KILL;
649 req->cmd_flags |= REQ_QUIET;
650 }
651 return ret;
652
653}
654
Christoph Hellwig1d520322014-09-14 11:08:21 -0700655/*
656 * alua_bus_attach - Attach device handler
657 * @sdev: device to be attached to
658 */
Christoph Hellwigee14c672015-08-27 14:16:59 +0200659static int alua_bus_attach(struct scsi_device *sdev)
Christoph Hellwig1d520322014-09-14 11:08:21 -0700660{
661 struct alua_dh_data *h;
662 int err;
663
664 h = kzalloc(sizeof(*h) , GFP_KERNEL);
665 if (!h)
Christoph Hellwigee14c672015-08-27 14:16:59 +0200666 return -ENOMEM;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700667 h->tpgs = TPGS_MODE_UNINITIALIZED;
668 h->state = TPGS_STATE_OPTIMIZED;
669 h->group_id = -1;
670 h->rel_port = -1;
671 h->buff = h->inq;
672 h->bufflen = ALUA_INQUIRY_SIZE;
673 h->sdev = sdev;
674
675 err = alua_initialize(sdev, h);
676 if (err != SCSI_DH_OK && err != SCSI_DH_DEV_OFFLINED)
677 goto failed;
678
Christoph Hellwigee14c672015-08-27 14:16:59 +0200679 sdev->handler_data = h;
680 return 0;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700681failed:
682 kfree(h);
Christoph Hellwigee14c672015-08-27 14:16:59 +0200683 return -EINVAL;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700684}
685
686/*
687 * alua_bus_detach - Detach device handler
688 * @sdev: device to be detached from
689 */
690static void alua_bus_detach(struct scsi_device *sdev)
691{
Christoph Hellwigee14c672015-08-27 14:16:59 +0200692 struct alua_dh_data *h = sdev->handler_data;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700693
694 if (h->buff && h->inq != h->buff)
695 kfree(h->buff);
Christoph Hellwigee14c672015-08-27 14:16:59 +0200696 sdev->handler_data = NULL;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700697 kfree(h);
698}
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700699
700static struct scsi_device_handler alua_dh = {
701 .name = ALUA_DH_NAME,
702 .module = THIS_MODULE,
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700703 .attach = alua_bus_attach,
704 .detach = alua_bus_detach,
705 .prep_fn = alua_prep_fn,
706 .check_sense = alua_check_sense,
707 .activate = alua_activate,
Moger, Babu4335d092012-03-27 20:55:49 +0000708 .set_params = alua_set_params,
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700709};
710
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700711static int __init alua_init(void)
712{
713 int r;
714
715 r = scsi_register_device_handler(&alua_dh);
716 if (r != 0)
717 printk(KERN_ERR "%s: Failed to register scsi device handler",
718 ALUA_DH_NAME);
719 return r;
720}
721
722static void __exit alua_exit(void)
723{
724 scsi_unregister_device_handler(&alua_dh);
725}
726
727module_init(alua_init);
728module_exit(alua_exit);
729
730MODULE_DESCRIPTION("DM Multipath ALUA support");
731MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
732MODULE_LICENSE("GPL");
733MODULE_VERSION(ALUA_DH_VER);