blob: df71e8523addc6fa3bdd65f9b65939c1ed0d5ebf [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;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070078 unsigned char sense[SCSI_SENSE_BUFFERSIZE];
Chandra Seetharaman96e65862009-10-21 09:23:04 -070079 struct scsi_device *sdev;
80 activate_complete callback_fn;
81 void *callback_data;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070082};
83
84#define ALUA_POLICY_SWITCH_CURRENT 0
85#define ALUA_POLICY_SWITCH_ALL 1
86
Chandra Seetharaman96e65862009-10-21 09:23:04 -070087static char print_alua_state(int);
Chandra Seetharaman96e65862009-10-21 09:23:04 -070088
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070089static int realloc_buffer(struct alua_dh_data *h, unsigned len)
90{
91 if (h->buff && h->buff != h->inq)
92 kfree(h->buff);
93
94 h->buff = kmalloc(len, GFP_NOIO);
95 if (!h->buff) {
96 h->buff = h->inq;
97 h->bufflen = ALUA_INQUIRY_SIZE;
98 return 1;
99 }
100 h->bufflen = len;
101 return 0;
102}
103
104static struct request *get_alua_req(struct scsi_device *sdev,
105 void *buffer, unsigned buflen, int rw)
106{
107 struct request *rq;
108 struct request_queue *q = sdev->request_queue;
109
110 rq = blk_get_request(q, rw, GFP_NOIO);
111
Joe Lawrencea492f072014-08-28 08:15:21 -0600112 if (IS_ERR(rq)) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700113 sdev_printk(KERN_INFO, sdev,
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700114 "%s: blk_get_request failed\n", __func__);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700115 return NULL;
116 }
Jens Axboef27b0872014-06-06 07:57:37 -0600117 blk_rq_set_block_pc(rq);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700118
119 if (buflen && blk_rq_map_kern(q, rq, buffer, buflen, GFP_NOIO)) {
120 blk_put_request(rq);
121 sdev_printk(KERN_INFO, sdev,
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700122 "%s: blk_rq_map_kern failed\n", __func__);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700123 return NULL;
124 }
125
Mike Christie6000a362008-08-19 18:45:30 -0500126 rq->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
Mike Christie64f84bc2008-09-06 08:39:16 -0500127 REQ_FAILFAST_DRIVER;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700128 rq->retries = ALUA_FAILOVER_RETRIES;
Rob Evers3588c5a2012-05-18 14:08:54 -0400129 rq->timeout = ALUA_FAILOVER_TIMEOUT * HZ;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700130
131 return rq;
132}
133
134/*
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700135 * submit_rtpg - Issue a REPORT TARGET GROUP STATES command
136 * @sdev: sdev the command should be sent to
137 */
Hannes Reinecked42ae5f2016-02-19 09:16:58 +0100138static unsigned submit_rtpg(struct scsi_device *sdev, unsigned char *buff,
139 int bufflen, unsigned char *sense, int flags)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700140{
141 struct request *rq;
Hannes Reinecke5597caf2015-12-01 10:16:48 +0100142 int err = 0;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700143
Hannes Reinecked42ae5f2016-02-19 09:16:58 +0100144 rq = get_alua_req(sdev, buff, bufflen, READ);
Hannes Reinecke5597caf2015-12-01 10:16:48 +0100145 if (!rq) {
146 err = DRIVER_BUSY << 24;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700147 goto done;
Hannes Reinecke5597caf2015-12-01 10:16:48 +0100148 }
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700149
150 /* Prepare the command. */
151 rq->cmd[0] = MAINTENANCE_IN;
Hannes Reinecked42ae5f2016-02-19 09:16:58 +0100152 if (!(flags & ALUA_RTPG_EXT_HDR_UNSUPP))
Rob Evers8e67ce62012-05-18 14:08:55 -0400153 rq->cmd[1] = MI_REPORT_TARGET_PGS | MI_EXT_HDR_PARAM_FMT;
154 else
155 rq->cmd[1] = MI_REPORT_TARGET_PGS;
Hannes Reinecked42ae5f2016-02-19 09:16:58 +0100156 put_unaligned_be32(bufflen, &rq->cmd[6]);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700157 rq->cmd_len = COMMAND_SIZE(MAINTENANCE_IN);
158
Hannes Reinecked42ae5f2016-02-19 09:16:58 +0100159 rq->sense = sense;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700160 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
Hannes Reinecked3692a32015-12-01 10:16:46 +0100161 rq->sense_len = 0;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700162
Hannes Reinecke5597caf2015-12-01 10:16:48 +0100163 blk_execute_rq(rq->q, NULL, rq, 1);
164 if (rq->errors)
165 err = rq->errors;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700166 blk_put_request(rq);
167done:
168 return err;
169}
170
171/*
Hannes Reineckedac173e2015-12-01 10:16:49 +0100172 * stpg_endio - Evaluate SET TARGET GROUP STATES
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700173 * @sdev: the device to be evaluated
174 * @state: the new target group state
175 *
Hannes Reineckedac173e2015-12-01 10:16:49 +0100176 * Evaluate a SET TARGET GROUP STATES command response.
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700177 */
178static void stpg_endio(struct request *req, int error)
179{
180 struct alua_dh_data *h = req->end_io_data;
181 struct scsi_sense_hdr sense_hdr;
Joseph Gruher93499232011-01-05 16:00:22 -0500182 unsigned err = SCSI_DH_OK;
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700183
Mike Christie27db6822013-03-05 22:40:24 -0600184 if (host_byte(req->errors) != DID_OK ||
185 msg_byte(req->errors) != COMMAND_COMPLETE) {
Joseph Gruher93499232011-01-05 16:00:22 -0500186 err = SCSI_DH_IO;
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700187 goto done;
Joseph Gruher93499232011-01-05 16:00:22 -0500188 }
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700189
Hannes Reinecked3692a32015-12-01 10:16:46 +0100190 if (scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
191 &sense_hdr)) {
Hannes Reineckee2d817d2015-12-01 10:16:54 +0100192 if (sense_hdr.sense_key == NOT_READY &&
193 sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x0a) {
194 /* ALUA state transition already in progress */
195 err = SCSI_DH_OK;
196 goto done;
197 }
198 if (sense_hdr.sense_key == UNIT_ATTENTION) {
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700199 err = SCSI_DH_RETRY;
200 goto done;
201 }
Hannes Reinecke80bd68d2015-12-01 10:16:47 +0100202 sdev_printk(KERN_INFO, h->sdev, "%s: stpg failed\n",
203 ALUA_DH_NAME);
204 scsi_print_sense_hdr(h->sdev, ALUA_DH_NAME, &sense_hdr);
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700205 err = SCSI_DH_IO;
Mike Christie27db6822013-03-05 22:40:24 -0600206 } else if (error)
207 err = SCSI_DH_IO;
208
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700209 if (err == SCSI_DH_OK) {
210 h->state = TPGS_STATE_OPTIMIZED;
211 sdev_printk(KERN_INFO, h->sdev,
212 "%s: port group %02x switched to state %c\n",
213 ALUA_DH_NAME, h->group_id,
214 print_alua_state(h->state));
215 }
216done:
Joseph Gruhered0f36b2011-01-05 16:00:21 -0500217 req->end_io_data = NULL;
218 __blk_put_request(req->q, req);
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700219 if (h->callback_fn) {
220 h->callback_fn(h->callback_data, err);
221 h->callback_fn = h->callback_data = NULL;
222 }
223 return;
224}
225
226/*
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700227 * submit_stpg - Issue a SET TARGET GROUP STATES command
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700228 *
229 * Currently we're only setting the current target port group state
230 * to 'active/optimized' and let the array firmware figure out
231 * the states of the remaining groups.
232 */
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700233static unsigned submit_stpg(struct alua_dh_data *h)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700234{
235 struct request *rq;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700236 int stpg_len = 8;
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700237 struct scsi_device *sdev = h->sdev;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700238
239 /* Prepare the data buffer */
240 memset(h->buff, 0, stpg_len);
241 h->buff[4] = TPGS_STATE_OPTIMIZED & 0x0f;
Hannes Reineckea7089772015-12-01 10:16:52 +0100242 put_unaligned_be16(h->group_id, &h->buff[6]);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700243
244 rq = get_alua_req(sdev, h->buff, stpg_len, WRITE);
245 if (!rq)
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700246 return SCSI_DH_RES_TEMP_UNAVAIL;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700247
248 /* Prepare the command. */
249 rq->cmd[0] = MAINTENANCE_OUT;
250 rq->cmd[1] = MO_SET_TARGET_PGS;
Hannes Reineckea7089772015-12-01 10:16:52 +0100251 put_unaligned_be32(stpg_len, &rq->cmd[6]);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700252 rq->cmd_len = COMMAND_SIZE(MAINTENANCE_OUT);
253
254 rq->sense = h->sense;
255 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
Hannes Reinecked3692a32015-12-01 10:16:46 +0100256 rq->sense_len = 0;
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700257 rq->end_io_data = h;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700258
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700259 blk_execute_rq_nowait(rq->q, NULL, rq, 1, stpg_endio);
Joseph Gruher7c66e9a2011-01-05 16:00:20 -0500260 return SCSI_DH_OK;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700261}
262
263/*
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200264 * alua_check_tpgs - Evaluate TPGS setting
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700265 * @sdev: device to be checked
266 *
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200267 * Examine the TPGS setting of the sdev to find out if ALUA
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700268 * is supported.
269 */
Hannes Reineckead0ea642015-12-01 10:16:53 +0100270static int alua_check_tpgs(struct scsi_device *sdev)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700271{
Hannes Reineckead0ea642015-12-01 10:16:53 +0100272 int tpgs = TPGS_MODE_NONE;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700273
Hannes Reineckedb5a6a62015-12-01 10:16:43 +0100274 /*
275 * ALUA support for non-disk devices is fraught with
276 * difficulties, so disable it for now.
277 */
278 if (sdev->type != TYPE_DISK) {
Hannes Reineckedb5a6a62015-12-01 10:16:43 +0100279 sdev_printk(KERN_INFO, sdev,
280 "%s: disable for non-disk devices\n",
281 ALUA_DH_NAME);
Hannes Reineckead0ea642015-12-01 10:16:53 +0100282 return tpgs;
Hannes Reineckedb5a6a62015-12-01 10:16:43 +0100283 }
284
Hannes Reineckead0ea642015-12-01 10:16:53 +0100285 tpgs = scsi_device_tpgs(sdev);
286 switch (tpgs) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700287 case TPGS_MODE_EXPLICIT|TPGS_MODE_IMPLICIT:
288 sdev_printk(KERN_INFO, sdev,
289 "%s: supports implicit and explicit TPGS\n",
290 ALUA_DH_NAME);
291 break;
292 case TPGS_MODE_EXPLICIT:
293 sdev_printk(KERN_INFO, sdev, "%s: supports explicit TPGS\n",
294 ALUA_DH_NAME);
295 break;
296 case TPGS_MODE_IMPLICIT:
297 sdev_printk(KERN_INFO, sdev, "%s: supports implicit TPGS\n",
298 ALUA_DH_NAME);
299 break;
Hannes Reinecke6cc05d42015-12-01 10:16:45 +0100300 case TPGS_MODE_NONE:
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700301 sdev_printk(KERN_INFO, sdev, "%s: not supported\n",
302 ALUA_DH_NAME);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700303 break;
Hannes Reinecke6cc05d42015-12-01 10:16:45 +0100304 default:
305 sdev_printk(KERN_INFO, sdev,
306 "%s: unsupported TPGS setting %d\n",
Hannes Reineckead0ea642015-12-01 10:16:53 +0100307 ALUA_DH_NAME, tpgs);
308 tpgs = TPGS_MODE_NONE;
Hannes Reinecke6cc05d42015-12-01 10:16:45 +0100309 break;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700310 }
311
Hannes Reineckead0ea642015-12-01 10:16:53 +0100312 return tpgs;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700313}
314
315/*
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100316 * alua_check_vpd - Evaluate INQUIRY vpd page 0x83
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700317 * @sdev: device to be checked
318 *
319 * Extract the relative target port and the target port group
320 * descriptor from the list of identificators.
321 */
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100322static int alua_check_vpd(struct scsi_device *sdev, struct alua_dh_data *h)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700323{
Hannes Reinecke83ea0e52015-12-01 10:16:58 +0100324 int rel_port = -1, group_id;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700325
Hannes Reinecke83ea0e52015-12-01 10:16:58 +0100326 group_id = scsi_vpd_tpg_id(sdev, &rel_port);
327 if (group_id < 0) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700328 /*
329 * Internal error; TPGS supported but required
330 * VPD identification descriptors not present.
331 * Disable ALUA support
332 */
333 sdev_printk(KERN_INFO, sdev,
334 "%s: No target port descriptors found\n",
335 ALUA_DH_NAME);
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100336 return SCSI_DH_DEV_UNSUPP;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700337 }
Hannes Reinecke83ea0e52015-12-01 10:16:58 +0100338 h->state = TPGS_STATE_OPTIMIZED;
339 h->group_id = group_id;
340
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100341 sdev_printk(KERN_INFO, sdev,
342 "%s: port group %02x rel port %02x\n",
343 ALUA_DH_NAME, h->group_id, h->rel_port);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700344
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100345 return 0;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700346}
347
348static char print_alua_state(int state)
349{
350 switch (state) {
351 case TPGS_STATE_OPTIMIZED:
352 return 'A';
353 case TPGS_STATE_NONOPTIMIZED:
354 return 'N';
355 case TPGS_STATE_STANDBY:
356 return 'S';
357 case TPGS_STATE_UNAVAILABLE:
358 return 'U';
Hannes Reinecke69723d12010-09-24 15:57:04 +0200359 case TPGS_STATE_LBA_DEPENDENT:
360 return 'L';
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700361 case TPGS_STATE_OFFLINE:
362 return 'O';
363 case TPGS_STATE_TRANSITIONING:
364 return 'T';
365 default:
366 return 'X';
367 }
368}
369
370static int alua_check_sense(struct scsi_device *sdev,
371 struct scsi_sense_hdr *sense_hdr)
372{
373 switch (sense_hdr->sense_key) {
374 case NOT_READY:
375 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0a)
376 /*
377 * LUN Not Accessible - ALUA state transition
378 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700379 return ADD_TO_MLQUEUE;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700380 break;
381 case UNIT_ATTENTION:
382 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
383 /*
384 * Power On, Reset, or Bus Device Reset, just retry.
385 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700386 return ADD_TO_MLQUEUE;
Stewart, Seanc20ee7b2013-10-15 15:52:39 +0000387 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x04)
388 /*
389 * Device internal reset
390 */
391 return ADD_TO_MLQUEUE;
Moger, Babu410f02d2011-12-21 18:01:37 -0500392 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x01)
393 /*
394 * Mode Parameters Changed
395 */
396 return ADD_TO_MLQUEUE;
Moger, Babubf819732011-10-06 13:22:07 -0400397 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700398 /*
399 * ALUA state changed
400 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700401 return ADD_TO_MLQUEUE;
Moger, Babubf819732011-10-06 13:22:07 -0400402 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700403 /*
404 * Implicit ALUA state transition failed
405 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700406 return ADD_TO_MLQUEUE;
Moger, Babubf819732011-10-06 13:22:07 -0400407 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x03)
408 /*
409 * Inquiry data has changed
410 */
411 return ADD_TO_MLQUEUE;
412 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x0e)
Ilgu Hong4d086f62009-01-30 17:00:11 -0600413 /*
414 * REPORTED_LUNS_DATA_HAS_CHANGED is reported
415 * when switching controllers on targets like
416 * Intel Multi-Flex. We can just retry.
417 */
418 return ADD_TO_MLQUEUE;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700419 break;
420 }
421
422 return SCSI_RETURN_NOT_HANDLED;
423}
424
425/*
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700426 * alua_rtpg - Evaluate REPORT TARGET GROUP STATES
427 * @sdev: the device to be evaluated.
Stewart, Seana8e5a2d2013-10-15 15:52:54 +0000428 * @wait_for_transition: if nonzero, wait ALUA_FAILOVER_TIMEOUT seconds for device to exit transitioning state
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700429 *
430 * Evaluate the Target Port Group State.
431 * Returns SCSI_DH_DEV_OFFLINED if the path is
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300432 * found to be unusable.
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700433 */
Stewart, Seana8e5a2d2013-10-15 15:52:54 +0000434static int alua_rtpg(struct scsi_device *sdev, struct alua_dh_data *h, int wait_for_transition)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700435{
436 struct scsi_sense_hdr sense_hdr;
437 int len, k, off, valid_states = 0;
Hannes Reineckecfde3fa2011-08-24 10:51:18 +0200438 unsigned char *ucp;
Hannes Reinecke5597caf2015-12-01 10:16:48 +0100439 unsigned err, retval;
Rob Eversbc97f4b2012-05-18 14:08:56 -0400440 unsigned long expiry, interval = 0;
Rob Evers3588c5a2012-05-18 14:08:54 -0400441 unsigned int tpg_desc_tbl_off;
442 unsigned char orig_transition_tmo;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700443
Rob Evers3588c5a2012-05-18 14:08:54 -0400444 if (!h->transition_tmo)
445 expiry = round_jiffies_up(jiffies + ALUA_FAILOVER_TIMEOUT * HZ);
446 else
447 expiry = round_jiffies_up(jiffies + h->transition_tmo * HZ);
448
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700449 retry:
Hannes Reinecked42ae5f2016-02-19 09:16:58 +0100450 retval = submit_rtpg(sdev, h->buff, h->bufflen, h->sense, h->flags);
Hannes Reinecke5597caf2015-12-01 10:16:48 +0100451 if (retval) {
Hannes Reinecked3692a32015-12-01 10:16:46 +0100452 if (!scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
Hannes Reinecke5597caf2015-12-01 10:16:48 +0100453 &sense_hdr)) {
454 sdev_printk(KERN_INFO, sdev,
455 "%s: rtpg failed, result %d\n",
456 ALUA_DH_NAME, retval);
457 if (driver_byte(retval) == DRIVER_BUSY)
458 return SCSI_DH_DEV_TEMP_BUSY;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700459 return SCSI_DH_IO;
Hannes Reinecke5597caf2015-12-01 10:16:48 +0100460 }
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700461
Rob Evers8e67ce62012-05-18 14:08:55 -0400462 /*
463 * submit_rtpg() has failed on existing arrays
464 * when requesting extended header info, and
465 * the array doesn't support extended headers,
466 * even though it shouldn't according to T10.
467 * The retry without rtpg_ext_hdr_req set
468 * handles this.
469 */
Hannes Reinecke6c4fc042015-12-01 10:16:51 +0100470 if (!(h->flags & ALUA_RTPG_EXT_HDR_UNSUPP) &&
Rob Evers8e67ce62012-05-18 14:08:55 -0400471 sense_hdr.sense_key == ILLEGAL_REQUEST &&
472 sense_hdr.asc == 0x24 && sense_hdr.ascq == 0) {
Hannes Reinecke6c4fc042015-12-01 10:16:51 +0100473 h->flags |= ALUA_RTPG_EXT_HDR_UNSUPP;
Rob Evers8e67ce62012-05-18 14:08:55 -0400474 goto retry;
475 }
Hannes Reineckee2d817d2015-12-01 10:16:54 +0100476 /*
477 * Retry on ALUA state transition or if any
478 * UNIT ATTENTION occurred.
479 */
480 if (sense_hdr.sense_key == NOT_READY &&
481 sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x0a)
482 err = SCSI_DH_RETRY;
483 else if (sense_hdr.sense_key == UNIT_ATTENTION)
484 err = SCSI_DH_RETRY;
485 if (err == SCSI_DH_RETRY && time_before(jiffies, expiry)) {
Hannes Reinecke80bd68d2015-12-01 10:16:47 +0100486 sdev_printk(KERN_ERR, sdev, "%s: rtpg retry\n",
487 ALUA_DH_NAME);
488 scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700489 goto retry;
Hannes Reinecke80bd68d2015-12-01 10:16:47 +0100490 }
491 sdev_printk(KERN_ERR, sdev, "%s: rtpg failed\n",
492 ALUA_DH_NAME);
493 scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr);
494 return SCSI_DH_IO;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700495 }
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700496
Hannes Reineckea7089772015-12-01 10:16:52 +0100497 len = get_unaligned_be32(&h->buff[0]) + 4;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700498
499 if (len > h->bufflen) {
500 /* Resubmit with the correct length */
501 if (realloc_buffer(h, len)) {
502 sdev_printk(KERN_WARNING, sdev,
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700503 "%s: kmalloc buffer failed\n",__func__);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700504 /* Temporary failure, bypass */
505 return SCSI_DH_DEV_TEMP_BUSY;
506 }
507 goto retry;
508 }
509
Rob Evers3588c5a2012-05-18 14:08:54 -0400510 orig_transition_tmo = h->transition_tmo;
511 if ((h->buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR && h->buff[5] != 0)
512 h->transition_tmo = h->buff[5];
513 else
514 h->transition_tmo = ALUA_FAILOVER_TIMEOUT;
515
Stewart, Seana8e5a2d2013-10-15 15:52:54 +0000516 if (wait_for_transition && (orig_transition_tmo != h->transition_tmo)) {
Rob Evers3588c5a2012-05-18 14:08:54 -0400517 sdev_printk(KERN_INFO, sdev,
518 "%s: transition timeout set to %d seconds\n",
519 ALUA_DH_NAME, h->transition_tmo);
520 expiry = jiffies + h->transition_tmo * HZ;
521 }
522
523 if ((h->buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR)
524 tpg_desc_tbl_off = 8;
525 else
526 tpg_desc_tbl_off = 4;
527
528 for (k = tpg_desc_tbl_off, ucp = h->buff + tpg_desc_tbl_off;
529 k < len;
530 k += off, ucp += off) {
531
Hannes Reineckea7089772015-12-01 10:16:52 +0100532 if (h->group_id == get_unaligned_be16(&ucp[2])) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700533 h->state = ucp[0] & 0x0f;
Moger, Babudcd3a752012-03-27 20:56:08 +0000534 h->pref = ucp[0] >> 7;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700535 valid_states = ucp[1];
536 }
537 off = 8 + (ucp[7] * 4);
538 }
539
540 sdev_printk(KERN_INFO, sdev,
Moger, Babudcd3a752012-03-27 20:56:08 +0000541 "%s: port group %02x state %c %s supports %c%c%c%c%c%c%c\n",
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700542 ALUA_DH_NAME, h->group_id, print_alua_state(h->state),
Moger, Babudcd3a752012-03-27 20:56:08 +0000543 h->pref ? "preferred" : "non-preferred",
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700544 valid_states&TPGS_SUPPORT_TRANSITION?'T':'t',
545 valid_states&TPGS_SUPPORT_OFFLINE?'O':'o',
Hannes Reinecke69723d12010-09-24 15:57:04 +0200546 valid_states&TPGS_SUPPORT_LBA_DEPENDENT?'L':'l',
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700547 valid_states&TPGS_SUPPORT_UNAVAILABLE?'U':'u',
548 valid_states&TPGS_SUPPORT_STANDBY?'S':'s',
549 valid_states&TPGS_SUPPORT_NONOPTIMIZED?'N':'n',
550 valid_states&TPGS_SUPPORT_OPTIMIZED?'A':'a');
551
Hannes Reinecke69723d12010-09-24 15:57:04 +0200552 switch (h->state) {
553 case TPGS_STATE_TRANSITIONING:
Stewart, Seana8e5a2d2013-10-15 15:52:54 +0000554 if (wait_for_transition) {
555 if (time_before(jiffies, expiry)) {
556 /* State transition, retry */
557 interval += 2000;
558 msleep(interval);
559 goto retry;
560 }
561 err = SCSI_DH_RETRY;
562 } else {
563 err = SCSI_DH_OK;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700564 }
Stewart, Seana8e5a2d2013-10-15 15:52:54 +0000565
Hannes Reinecke69723d12010-09-24 15:57:04 +0200566 /* Transitioning time exceeded, set port to standby */
Hannes Reinecke69723d12010-09-24 15:57:04 +0200567 h->state = TPGS_STATE_STANDBY;
568 break;
569 case TPGS_STATE_OFFLINE:
Bart Van Asschee47f8972012-08-24 09:08:41 +0000570 /* Path unusable */
Hannes Reinecke69723d12010-09-24 15:57:04 +0200571 err = SCSI_DH_DEV_OFFLINED;
572 break;
573 default:
574 /* Useable path if active */
575 err = SCSI_DH_OK;
576 break;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700577 }
578 return err;
579}
580
581/*
582 * alua_initialize - Initialize ALUA state
583 * @sdev: the device to be initialized
584 *
585 * For the prep_fn to work correctly we have
586 * to initialize the ALUA state for the device.
587 */
588static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h)
589{
Hannes Reineckead0ea642015-12-01 10:16:53 +0100590 int err = SCSI_DH_DEV_UNSUPP;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700591
Hannes Reineckead0ea642015-12-01 10:16:53 +0100592 h->tpgs = alua_check_tpgs(sdev);
593 if (h->tpgs == TPGS_MODE_NONE)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700594 goto out;
595
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100596 err = alua_check_vpd(sdev, h);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700597 if (err != SCSI_DH_OK)
598 goto out;
599
Stewart, Seana8e5a2d2013-10-15 15:52:54 +0000600 err = alua_rtpg(sdev, h, 0);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700601 if (err != SCSI_DH_OK)
602 goto out;
603
604out:
605 return err;
606}
Moger, Babu4335d092012-03-27 20:55:49 +0000607/*
608 * alua_set_params - set/unset the optimize flag
609 * @sdev: device on the path to be activated
610 * params - parameters in the following format
611 * "no_of_params\0param1\0param2\0param3\0...\0"
612 * For example, to set the flag pass the following parameters
613 * from multipath.conf
614 * hardware_handler "2 alua 1"
615 */
616static int alua_set_params(struct scsi_device *sdev, const char *params)
617{
Christoph Hellwigee14c672015-08-27 14:16:59 +0200618 struct alua_dh_data *h = sdev->handler_data;
Moger, Babu4335d092012-03-27 20:55:49 +0000619 unsigned int optimize = 0, argc;
620 const char *p = params;
621 int result = SCSI_DH_OK;
622
623 if ((sscanf(params, "%u", &argc) != 1) || (argc != 1))
624 return -EINVAL;
625
626 while (*p++)
627 ;
628 if ((sscanf(p, "%u", &optimize) != 1) || (optimize > 1))
629 return -EINVAL;
630
631 if (optimize)
632 h->flags |= ALUA_OPTIMIZE_STPG;
633 else
634 h->flags &= ~ALUA_OPTIMIZE_STPG;
635
636 return result;
637}
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700638
Stewart, Sean7a3ad392013-04-04 14:54:47 +0000639static uint optimize_stpg;
640module_param(optimize_stpg, uint, S_IRUGO|S_IWUSR);
641MODULE_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.");
642
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700643/*
644 * alua_activate - activate a path
645 * @sdev: device on the path to be activated
646 *
647 * We're currently switching the port group to be activated only and
648 * let the array figure out the rest.
649 * There may be other arrays which require us to switch all port groups
650 * based on a certain policy. But until we actually encounter them it
651 * should be okay.
652 */
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700653static int alua_activate(struct scsi_device *sdev,
654 activate_complete fn, void *data)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700655{
Christoph Hellwigee14c672015-08-27 14:16:59 +0200656 struct alua_dh_data *h = sdev->handler_data;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700657 int err = SCSI_DH_OK;
Moger, Babu72d9e0f2012-03-27 20:56:20 +0000658 int stpg = 0;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700659
Stewart, Seana8e5a2d2013-10-15 15:52:54 +0000660 err = alua_rtpg(sdev, h, 1);
Hannes Reinecke46ccf6b2011-08-24 10:51:16 +0200661 if (err != SCSI_DH_OK)
662 goto out;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700663
Stewart, Sean7a3ad392013-04-04 14:54:47 +0000664 if (optimize_stpg)
665 h->flags |= ALUA_OPTIMIZE_STPG;
666
Moger, Babu72d9e0f2012-03-27 20:56:20 +0000667 if (h->tpgs & TPGS_MODE_EXPLICIT) {
668 switch (h->state) {
669 case TPGS_STATE_NONOPTIMIZED:
670 stpg = 1;
671 if ((h->flags & ALUA_OPTIMIZE_STPG) &&
672 (!h->pref) &&
673 (h->tpgs & TPGS_MODE_IMPLICIT))
674 stpg = 0;
675 break;
676 case TPGS_STATE_STANDBY:
Bart Van Asschebb2c94a2012-06-22 08:49:38 +0000677 case TPGS_STATE_UNAVAILABLE:
Moger, Babu72d9e0f2012-03-27 20:56:20 +0000678 stpg = 1;
679 break;
Moger, Babu72d9e0f2012-03-27 20:56:20 +0000680 case TPGS_STATE_OFFLINE:
681 err = SCSI_DH_IO;
682 break;
683 case TPGS_STATE_TRANSITIONING:
684 err = SCSI_DH_RETRY;
685 break;
686 default:
687 break;
688 }
689 }
690
691 if (stpg) {
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700692 h->callback_fn = fn;
693 h->callback_data = data;
694 err = submit_stpg(h);
695 if (err == SCSI_DH_OK)
696 return 0;
697 h->callback_fn = h->callback_data = NULL;
698 }
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700699
700out:
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700701 if (fn)
702 fn(data, err);
703 return 0;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700704}
705
706/*
707 * alua_prep_fn - request callback
708 *
709 * Fail I/O to all paths not in state
710 * active/optimized or active/non-optimized.
711 */
712static int alua_prep_fn(struct scsi_device *sdev, struct request *req)
713{
Christoph Hellwigee14c672015-08-27 14:16:59 +0200714 struct alua_dh_data *h = sdev->handler_data;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700715 int ret = BLKPREP_OK;
716
Hannes Reinecke69723d12010-09-24 15:57:04 +0200717 if (h->state == TPGS_STATE_TRANSITIONING)
718 ret = BLKPREP_DEFER;
719 else if (h->state != TPGS_STATE_OPTIMIZED &&
720 h->state != TPGS_STATE_NONOPTIMIZED &&
721 h->state != TPGS_STATE_LBA_DEPENDENT) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700722 ret = BLKPREP_KILL;
723 req->cmd_flags |= REQ_QUIET;
724 }
725 return ret;
726
727}
728
Christoph Hellwig1d520322014-09-14 11:08:21 -0700729/*
730 * alua_bus_attach - Attach device handler
731 * @sdev: device to be attached to
732 */
Christoph Hellwigee14c672015-08-27 14:16:59 +0200733static int alua_bus_attach(struct scsi_device *sdev)
Christoph Hellwig1d520322014-09-14 11:08:21 -0700734{
735 struct alua_dh_data *h;
736 int err;
737
738 h = kzalloc(sizeof(*h) , GFP_KERNEL);
739 if (!h)
Christoph Hellwigee14c672015-08-27 14:16:59 +0200740 return -ENOMEM;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700741 h->tpgs = TPGS_MODE_UNINITIALIZED;
742 h->state = TPGS_STATE_OPTIMIZED;
743 h->group_id = -1;
744 h->rel_port = -1;
745 h->buff = h->inq;
746 h->bufflen = ALUA_INQUIRY_SIZE;
747 h->sdev = sdev;
748
749 err = alua_initialize(sdev, h);
750 if (err != SCSI_DH_OK && err != SCSI_DH_DEV_OFFLINED)
751 goto failed;
752
Christoph Hellwigee14c672015-08-27 14:16:59 +0200753 sdev->handler_data = h;
754 return 0;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700755failed:
756 kfree(h);
Christoph Hellwigee14c672015-08-27 14:16:59 +0200757 return -EINVAL;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700758}
759
760/*
761 * alua_bus_detach - Detach device handler
762 * @sdev: device to be detached from
763 */
764static void alua_bus_detach(struct scsi_device *sdev)
765{
Christoph Hellwigee14c672015-08-27 14:16:59 +0200766 struct alua_dh_data *h = sdev->handler_data;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700767
768 if (h->buff && h->inq != h->buff)
769 kfree(h->buff);
Christoph Hellwigee14c672015-08-27 14:16:59 +0200770 sdev->handler_data = NULL;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700771 kfree(h);
772}
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700773
774static struct scsi_device_handler alua_dh = {
775 .name = ALUA_DH_NAME,
776 .module = THIS_MODULE,
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700777 .attach = alua_bus_attach,
778 .detach = alua_bus_detach,
779 .prep_fn = alua_prep_fn,
780 .check_sense = alua_check_sense,
781 .activate = alua_activate,
Moger, Babu4335d092012-03-27 20:55:49 +0000782 .set_params = alua_set_params,
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700783};
784
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700785static int __init alua_init(void)
786{
787 int r;
788
789 r = scsi_register_device_handler(&alua_dh);
790 if (r != 0)
791 printk(KERN_ERR "%s: Failed to register scsi device handler",
792 ALUA_DH_NAME);
793 return r;
794}
795
796static void __exit alua_exit(void)
797{
798 scsi_unregister_device_handler(&alua_dh);
799}
800
801module_init(alua_init);
802module_exit(alua_exit);
803
804MODULE_DESCRIPTION("DM Multipath ALUA support");
805MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
806MODULE_LICENSE("GPL");
807MODULE_VERSION(ALUA_DH_VER);