blob: 31a971f35d831826295d9d525ffe4c0f4455a78e [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 Reinecke057ea7c2008-07-17 16:53:21 -070025#include <scsi/scsi.h>
26#include <scsi/scsi_eh.h>
27#include <scsi/scsi_dh.h>
28
29#define ALUA_DH_NAME "alua"
Hannes Reinecke69723d12010-09-24 15:57:04 +020030#define ALUA_DH_VER "1.3"
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070031
32#define TPGS_STATE_OPTIMIZED 0x0
33#define TPGS_STATE_NONOPTIMIZED 0x1
34#define TPGS_STATE_STANDBY 0x2
35#define TPGS_STATE_UNAVAILABLE 0x3
Hannes Reinecke69723d12010-09-24 15:57:04 +020036#define TPGS_STATE_LBA_DEPENDENT 0x4
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070037#define TPGS_STATE_OFFLINE 0xe
38#define TPGS_STATE_TRANSITIONING 0xf
39
40#define TPGS_SUPPORT_NONE 0x00
41#define TPGS_SUPPORT_OPTIMIZED 0x01
42#define TPGS_SUPPORT_NONOPTIMIZED 0x02
43#define TPGS_SUPPORT_STANDBY 0x04
44#define TPGS_SUPPORT_UNAVAILABLE 0x08
Hannes Reinecke69723d12010-09-24 15:57:04 +020045#define TPGS_SUPPORT_LBA_DEPENDENT 0x10
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070046#define TPGS_SUPPORT_OFFLINE 0x40
47#define TPGS_SUPPORT_TRANSITION 0x80
48
Rob Evers3588c5a2012-05-18 14:08:54 -040049#define RTPG_FMT_MASK 0x70
50#define RTPG_FMT_EXT_HDR 0x10
51
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070052#define TPGS_MODE_UNINITIALIZED -1
53#define TPGS_MODE_NONE 0x0
54#define TPGS_MODE_IMPLICIT 0x1
55#define TPGS_MODE_EXPLICIT 0x2
56
57#define ALUA_INQUIRY_SIZE 36
Rob Evers3588c5a2012-05-18 14:08:54 -040058#define ALUA_FAILOVER_TIMEOUT 60
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070059#define ALUA_FAILOVER_RETRIES 5
60
Moger, Babu4335d092012-03-27 20:55:49 +000061/* flags passed from user level */
62#define ALUA_OPTIMIZE_STPG 1
63
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070064struct alua_dh_data {
65 int group_id;
66 int rel_port;
67 int tpgs;
68 int state;
Moger, Babudcd3a752012-03-27 20:56:08 +000069 int pref;
Moger, Babu4335d092012-03-27 20:55:49 +000070 unsigned flags; /* used for optimizing STPG */
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070071 unsigned char inq[ALUA_INQUIRY_SIZE];
72 unsigned char *buff;
73 int bufflen;
Rob Evers3588c5a2012-05-18 14:08:54 -040074 unsigned char transition_tmo;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070075 unsigned char sense[SCSI_SENSE_BUFFERSIZE];
76 int senselen;
Chandra Seetharaman96e65862009-10-21 09:23:04 -070077 struct scsi_device *sdev;
78 activate_complete callback_fn;
79 void *callback_data;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -070080};
81
82#define ALUA_POLICY_SWITCH_CURRENT 0
83#define ALUA_POLICY_SWITCH_ALL 1
84
Chandra Seetharaman96e65862009-10-21 09:23:04 -070085static char print_alua_state(int);
86static int alua_check_sense(struct scsi_device *, struct scsi_sense_hdr *);
87
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
103static struct request *get_alua_req(struct scsi_device *sdev,
104 void *buffer, unsigned buflen, int rw)
105{
106 struct request *rq;
107 struct request_queue *q = sdev->request_queue;
108
109 rq = blk_get_request(q, rw, GFP_NOIO);
110
Joe Lawrencea492f072014-08-28 08:15:21 -0600111 if (IS_ERR(rq)) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700112 sdev_printk(KERN_INFO, sdev,
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700113 "%s: blk_get_request failed\n", __func__);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700114 return NULL;
115 }
Jens Axboef27b0872014-06-06 07:57:37 -0600116 blk_rq_set_block_pc(rq);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700117
118 if (buflen && blk_rq_map_kern(q, rq, buffer, buflen, GFP_NOIO)) {
119 blk_put_request(rq);
120 sdev_printk(KERN_INFO, sdev,
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700121 "%s: blk_rq_map_kern failed\n", __func__);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700122 return NULL;
123 }
124
Mike Christie6000a362008-08-19 18:45:30 -0500125 rq->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
Mike Christie64f84bc2008-09-06 08:39:16 -0500126 REQ_FAILFAST_DRIVER;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700127 rq->retries = ALUA_FAILOVER_RETRIES;
Rob Evers3588c5a2012-05-18 14:08:54 -0400128 rq->timeout = ALUA_FAILOVER_TIMEOUT * HZ;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700129
130 return rq;
131}
132
133/*
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700134 * submit_rtpg - Issue a REPORT TARGET GROUP STATES command
135 * @sdev: sdev the command should be sent to
136 */
Rob Evers8e67ce62012-05-18 14:08:55 -0400137static unsigned submit_rtpg(struct scsi_device *sdev, struct alua_dh_data *h,
138 bool rtpg_ext_hdr_req)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700139{
140 struct request *rq;
141 int err = SCSI_DH_RES_TEMP_UNAVAIL;
142
143 rq = get_alua_req(sdev, h->buff, h->bufflen, READ);
144 if (!rq)
145 goto done;
146
147 /* Prepare the command. */
148 rq->cmd[0] = MAINTENANCE_IN;
Rob Evers8e67ce62012-05-18 14:08:55 -0400149 if (rtpg_ext_hdr_req)
150 rq->cmd[1] = MI_REPORT_TARGET_PGS | MI_EXT_HDR_PARAM_FMT;
151 else
152 rq->cmd[1] = MI_REPORT_TARGET_PGS;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700153 rq->cmd[6] = (h->bufflen >> 24) & 0xff;
154 rq->cmd[7] = (h->bufflen >> 16) & 0xff;
155 rq->cmd[8] = (h->bufflen >> 8) & 0xff;
156 rq->cmd[9] = h->bufflen & 0xff;
157 rq->cmd_len = COMMAND_SIZE(MAINTENANCE_IN);
158
159 rq->sense = h->sense;
160 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
161 rq->sense_len = h->senselen = 0;
162
163 err = blk_execute_rq(rq->q, NULL, rq, 1);
164 if (err == -EIO) {
165 sdev_printk(KERN_INFO, sdev,
166 "%s: rtpg failed with %x\n",
167 ALUA_DH_NAME, rq->errors);
168 h->senselen = rq->sense_len;
169 err = SCSI_DH_IO;
170 }
171 blk_put_request(rq);
172done:
173 return err;
174}
175
176/*
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700177 * alua_stpg - Evaluate SET TARGET GROUP STATES
178 * @sdev: the device to be evaluated
179 * @state: the new target group state
180 *
181 * Send a SET TARGET GROUP STATES command to the device.
182 * We only have to test here if we should resubmit the command;
183 * any other error is assumed as a failure.
184 */
185static void stpg_endio(struct request *req, int error)
186{
187 struct alua_dh_data *h = req->end_io_data;
188 struct scsi_sense_hdr sense_hdr;
Joseph Gruher93499232011-01-05 16:00:22 -0500189 unsigned err = SCSI_DH_OK;
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700190
Mike Christie27db6822013-03-05 22:40:24 -0600191 if (host_byte(req->errors) != DID_OK ||
192 msg_byte(req->errors) != COMMAND_COMPLETE) {
Joseph Gruher93499232011-01-05 16:00:22 -0500193 err = SCSI_DH_IO;
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700194 goto done;
Joseph Gruher93499232011-01-05 16:00:22 -0500195 }
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700196
Mike Christie27db6822013-03-05 22:40:24 -0600197 if (req->sense_len > 0) {
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700198 err = scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
199 &sense_hdr);
200 if (!err) {
201 err = SCSI_DH_IO;
202 goto done;
203 }
204 err = alua_check_sense(h->sdev, &sense_hdr);
205 if (err == ADD_TO_MLQUEUE) {
206 err = SCSI_DH_RETRY;
207 goto done;
208 }
209 sdev_printk(KERN_INFO, h->sdev,
210 "%s: stpg sense code: %02x/%02x/%02x\n",
211 ALUA_DH_NAME, sense_hdr.sense_key,
212 sense_hdr.asc, sense_hdr.ascq);
213 err = SCSI_DH_IO;
Mike Christie27db6822013-03-05 22:40:24 -0600214 } else if (error)
215 err = SCSI_DH_IO;
216
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700217 if (err == SCSI_DH_OK) {
218 h->state = TPGS_STATE_OPTIMIZED;
219 sdev_printk(KERN_INFO, h->sdev,
220 "%s: port group %02x switched to state %c\n",
221 ALUA_DH_NAME, h->group_id,
222 print_alua_state(h->state));
223 }
224done:
Joseph Gruhered0f36b2011-01-05 16:00:21 -0500225 req->end_io_data = NULL;
226 __blk_put_request(req->q, req);
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700227 if (h->callback_fn) {
228 h->callback_fn(h->callback_data, err);
229 h->callback_fn = h->callback_data = NULL;
230 }
231 return;
232}
233
234/*
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700235 * submit_stpg - Issue a SET TARGET GROUP STATES command
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700236 *
237 * Currently we're only setting the current target port group state
238 * to 'active/optimized' and let the array firmware figure out
239 * the states of the remaining groups.
240 */
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700241static unsigned submit_stpg(struct alua_dh_data *h)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700242{
243 struct request *rq;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700244 int stpg_len = 8;
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700245 struct scsi_device *sdev = h->sdev;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700246
247 /* Prepare the data buffer */
248 memset(h->buff, 0, stpg_len);
249 h->buff[4] = TPGS_STATE_OPTIMIZED & 0x0f;
Ilgu Hongef3fa8c2009-01-30 17:00:09 -0600250 h->buff[6] = (h->group_id >> 8) & 0xff;
251 h->buff[7] = h->group_id & 0xff;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700252
253 rq = get_alua_req(sdev, h->buff, stpg_len, WRITE);
254 if (!rq)
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700255 return SCSI_DH_RES_TEMP_UNAVAIL;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700256
257 /* Prepare the command. */
258 rq->cmd[0] = MAINTENANCE_OUT;
259 rq->cmd[1] = MO_SET_TARGET_PGS;
260 rq->cmd[6] = (stpg_len >> 24) & 0xff;
261 rq->cmd[7] = (stpg_len >> 16) & 0xff;
262 rq->cmd[8] = (stpg_len >> 8) & 0xff;
263 rq->cmd[9] = stpg_len & 0xff;
264 rq->cmd_len = COMMAND_SIZE(MAINTENANCE_OUT);
265
266 rq->sense = h->sense;
267 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
268 rq->sense_len = h->senselen = 0;
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700269 rq->end_io_data = h;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700270
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700271 blk_execute_rq_nowait(rq->q, NULL, rq, 1, stpg_endio);
Joseph Gruher7c66e9a2011-01-05 16:00:20 -0500272 return SCSI_DH_OK;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700273}
274
275/*
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200276 * alua_check_tpgs - Evaluate TPGS setting
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700277 * @sdev: device to be checked
278 *
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200279 * Examine the TPGS setting of the sdev to find out if ALUA
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700280 * is supported.
281 */
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200282static int alua_check_tpgs(struct scsi_device *sdev, struct alua_dh_data *h)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700283{
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200284 int err = SCSI_DH_OK;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700285
Hannes Reineckedb5a6a62015-12-01 10:16:43 +0100286 /*
287 * ALUA support for non-disk devices is fraught with
288 * difficulties, so disable it for now.
289 */
290 if (sdev->type != TYPE_DISK) {
291 h->tpgs = TPGS_MODE_NONE;
292 sdev_printk(KERN_INFO, sdev,
293 "%s: disable for non-disk devices\n",
294 ALUA_DH_NAME);
295 return SCSI_DH_DEV_UNSUPP;
296 }
297
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200298 h->tpgs = scsi_device_tpgs(sdev);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700299 switch (h->tpgs) {
300 case TPGS_MODE_EXPLICIT|TPGS_MODE_IMPLICIT:
301 sdev_printk(KERN_INFO, sdev,
302 "%s: supports implicit and explicit TPGS\n",
303 ALUA_DH_NAME);
304 break;
305 case TPGS_MODE_EXPLICIT:
306 sdev_printk(KERN_INFO, sdev, "%s: supports explicit TPGS\n",
307 ALUA_DH_NAME);
308 break;
309 case TPGS_MODE_IMPLICIT:
310 sdev_printk(KERN_INFO, sdev, "%s: supports implicit TPGS\n",
311 ALUA_DH_NAME);
312 break;
Hannes Reinecke6cc05d42015-12-01 10:16:45 +0100313 case TPGS_MODE_NONE:
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700314 sdev_printk(KERN_INFO, sdev, "%s: not supported\n",
315 ALUA_DH_NAME);
316 err = SCSI_DH_DEV_UNSUPP;
317 break;
Hannes Reinecke6cc05d42015-12-01 10:16:45 +0100318 default:
319 sdev_printk(KERN_INFO, sdev,
320 "%s: unsupported TPGS setting %d\n",
321 ALUA_DH_NAME, h->tpgs);
322 h->tpgs = TPGS_MODE_NONE;
323 err = SCSI_DH_DEV_UNSUPP;
324 break;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700325 }
326
327 return err;
328}
329
330/*
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100331 * alua_check_vpd - Evaluate INQUIRY vpd page 0x83
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700332 * @sdev: device to be checked
333 *
334 * Extract the relative target port and the target port group
335 * descriptor from the list of identificators.
336 */
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100337static int alua_check_vpd(struct scsi_device *sdev, struct alua_dh_data *h)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700338{
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700339 unsigned char *d;
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100340 unsigned char __rcu *vpd_pg83;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700341
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100342 rcu_read_lock();
343 if (!rcu_dereference(sdev->vpd_pg83)) {
344 rcu_read_unlock();
345 return SCSI_DH_DEV_UNSUPP;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700346 }
347
348 /*
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100349 * Look for the correct descriptor.
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700350 */
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100351 vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
352 d = vpd_pg83 + 4;
353 while (d < vpd_pg83 + sdev->vpd_pg83_len) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700354 switch (d[1] & 0xf) {
355 case 0x4:
356 /* Relative target port */
357 h->rel_port = (d[6] << 8) + d[7];
358 break;
359 case 0x5:
360 /* Target port group */
361 h->group_id = (d[6] << 8) + d[7];
362 break;
363 default:
364 break;
365 }
366 d += d[3] + 4;
367 }
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100368 rcu_read_unlock();
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700369
370 if (h->group_id == -1) {
371 /*
372 * Internal error; TPGS supported but required
373 * VPD identification descriptors not present.
374 * Disable ALUA support
375 */
376 sdev_printk(KERN_INFO, sdev,
377 "%s: No target port descriptors found\n",
378 ALUA_DH_NAME);
379 h->state = TPGS_STATE_OPTIMIZED;
380 h->tpgs = TPGS_MODE_NONE;
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100381 return SCSI_DH_DEV_UNSUPP;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700382 }
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100383 sdev_printk(KERN_INFO, sdev,
384 "%s: port group %02x rel port %02x\n",
385 ALUA_DH_NAME, h->group_id, h->rel_port);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700386
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100387 return 0;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700388}
389
390static char print_alua_state(int state)
391{
392 switch (state) {
393 case TPGS_STATE_OPTIMIZED:
394 return 'A';
395 case TPGS_STATE_NONOPTIMIZED:
396 return 'N';
397 case TPGS_STATE_STANDBY:
398 return 'S';
399 case TPGS_STATE_UNAVAILABLE:
400 return 'U';
Hannes Reinecke69723d12010-09-24 15:57:04 +0200401 case TPGS_STATE_LBA_DEPENDENT:
402 return 'L';
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700403 case TPGS_STATE_OFFLINE:
404 return 'O';
405 case TPGS_STATE_TRANSITIONING:
406 return 'T';
407 default:
408 return 'X';
409 }
410}
411
412static int alua_check_sense(struct scsi_device *sdev,
413 struct scsi_sense_hdr *sense_hdr)
414{
415 switch (sense_hdr->sense_key) {
416 case NOT_READY:
417 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0a)
418 /*
419 * LUN Not Accessible - ALUA state transition
420 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700421 return ADD_TO_MLQUEUE;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700422 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0b)
423 /*
424 * LUN Not Accessible -- Target port in standby state
425 */
426 return SUCCESS;
427 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0c)
428 /*
429 * LUN Not Accessible -- Target port in unavailable state
430 */
431 return SUCCESS;
432 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x12)
433 /*
434 * LUN Not Ready -- Offline
435 */
436 return SUCCESS;
wenxiong@linux.vnet.ibm.com333b2442014-11-06 15:11:23 -0600437 if (sdev->allow_restart &&
438 sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x02)
439 /*
440 * if the device is not started, we need to wake
441 * the error handler to start the motor
442 */
443 return FAILED;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700444 break;
445 case UNIT_ATTENTION:
446 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
447 /*
448 * Power On, Reset, or Bus Device Reset, just retry.
449 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700450 return ADD_TO_MLQUEUE;
Stewart, Seanc20ee7b2013-10-15 15:52:39 +0000451 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x04)
452 /*
453 * Device internal reset
454 */
455 return ADD_TO_MLQUEUE;
Moger, Babu410f02d2011-12-21 18:01:37 -0500456 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x01)
457 /*
458 * Mode Parameters Changed
459 */
460 return ADD_TO_MLQUEUE;
Moger, Babubf819732011-10-06 13:22:07 -0400461 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700462 /*
463 * ALUA state changed
464 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700465 return ADD_TO_MLQUEUE;
Moger, Babubf819732011-10-06 13:22:07 -0400466 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700467 /*
468 * Implicit ALUA state transition failed
469 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700470 return ADD_TO_MLQUEUE;
Moger, Babubf819732011-10-06 13:22:07 -0400471 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x03)
472 /*
473 * Inquiry data has changed
474 */
475 return ADD_TO_MLQUEUE;
476 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x0e)
Ilgu Hong4d086f62009-01-30 17:00:11 -0600477 /*
478 * REPORTED_LUNS_DATA_HAS_CHANGED is reported
479 * when switching controllers on targets like
480 * Intel Multi-Flex. We can just retry.
481 */
482 return ADD_TO_MLQUEUE;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700483 break;
484 }
485
486 return SCSI_RETURN_NOT_HANDLED;
487}
488
489/*
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700490 * alua_rtpg - Evaluate REPORT TARGET GROUP STATES
491 * @sdev: the device to be evaluated.
Stewart, Seana8e5a2d2013-10-15 15:52:54 +0000492 * @wait_for_transition: if nonzero, wait ALUA_FAILOVER_TIMEOUT seconds for device to exit transitioning state
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700493 *
494 * Evaluate the Target Port Group State.
495 * Returns SCSI_DH_DEV_OFFLINED if the path is
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300496 * found to be unusable.
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700497 */
Stewart, Seana8e5a2d2013-10-15 15:52:54 +0000498static int alua_rtpg(struct scsi_device *sdev, struct alua_dh_data *h, int wait_for_transition)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700499{
500 struct scsi_sense_hdr sense_hdr;
501 int len, k, off, valid_states = 0;
Hannes Reineckecfde3fa2011-08-24 10:51:18 +0200502 unsigned char *ucp;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700503 unsigned err;
Rob Evers8e67ce62012-05-18 14:08:55 -0400504 bool rtpg_ext_hdr_req = 1;
Rob Eversbc97f4b2012-05-18 14:08:56 -0400505 unsigned long expiry, interval = 0;
Rob Evers3588c5a2012-05-18 14:08:54 -0400506 unsigned int tpg_desc_tbl_off;
507 unsigned char orig_transition_tmo;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700508
Rob Evers3588c5a2012-05-18 14:08:54 -0400509 if (!h->transition_tmo)
510 expiry = round_jiffies_up(jiffies + ALUA_FAILOVER_TIMEOUT * HZ);
511 else
512 expiry = round_jiffies_up(jiffies + h->transition_tmo * HZ);
513
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700514 retry:
Rob Evers8e67ce62012-05-18 14:08:55 -0400515 err = submit_rtpg(sdev, h, rtpg_ext_hdr_req);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700516
517 if (err == SCSI_DH_IO && h->senselen > 0) {
518 err = scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
519 &sense_hdr);
520 if (!err)
521 return SCSI_DH_IO;
522
Rob Evers8e67ce62012-05-18 14:08:55 -0400523 /*
524 * submit_rtpg() has failed on existing arrays
525 * when requesting extended header info, and
526 * the array doesn't support extended headers,
527 * even though it shouldn't according to T10.
528 * The retry without rtpg_ext_hdr_req set
529 * handles this.
530 */
531 if (rtpg_ext_hdr_req == 1 &&
532 sense_hdr.sense_key == ILLEGAL_REQUEST &&
533 sense_hdr.asc == 0x24 && sense_hdr.ascq == 0) {
534 rtpg_ext_hdr_req = 0;
535 goto retry;
536 }
537
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700538 err = alua_check_sense(sdev, &sense_hdr);
Hannes Reinecke69723d12010-09-24 15:57:04 +0200539 if (err == ADD_TO_MLQUEUE && time_before(jiffies, expiry))
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700540 goto retry;
541 sdev_printk(KERN_INFO, sdev,
542 "%s: rtpg sense code %02x/%02x/%02x\n",
543 ALUA_DH_NAME, sense_hdr.sense_key,
544 sense_hdr.asc, sense_hdr.ascq);
545 err = SCSI_DH_IO;
546 }
547 if (err != SCSI_DH_OK)
548 return err;
549
550 len = (h->buff[0] << 24) + (h->buff[1] << 16) +
551 (h->buff[2] << 8) + h->buff[3] + 4;
552
553 if (len > h->bufflen) {
554 /* Resubmit with the correct length */
555 if (realloc_buffer(h, len)) {
556 sdev_printk(KERN_WARNING, sdev,
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700557 "%s: kmalloc buffer failed\n",__func__);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700558 /* Temporary failure, bypass */
559 return SCSI_DH_DEV_TEMP_BUSY;
560 }
561 goto retry;
562 }
563
Rob Evers3588c5a2012-05-18 14:08:54 -0400564 orig_transition_tmo = h->transition_tmo;
565 if ((h->buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR && h->buff[5] != 0)
566 h->transition_tmo = h->buff[5];
567 else
568 h->transition_tmo = ALUA_FAILOVER_TIMEOUT;
569
Stewart, Seana8e5a2d2013-10-15 15:52:54 +0000570 if (wait_for_transition && (orig_transition_tmo != h->transition_tmo)) {
Rob Evers3588c5a2012-05-18 14:08:54 -0400571 sdev_printk(KERN_INFO, sdev,
572 "%s: transition timeout set to %d seconds\n",
573 ALUA_DH_NAME, h->transition_tmo);
574 expiry = jiffies + h->transition_tmo * HZ;
575 }
576
577 if ((h->buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR)
578 tpg_desc_tbl_off = 8;
579 else
580 tpg_desc_tbl_off = 4;
581
582 for (k = tpg_desc_tbl_off, ucp = h->buff + tpg_desc_tbl_off;
583 k < len;
584 k += off, ucp += off) {
585
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700586 if (h->group_id == (ucp[2] << 8) + ucp[3]) {
587 h->state = ucp[0] & 0x0f;
Moger, Babudcd3a752012-03-27 20:56:08 +0000588 h->pref = ucp[0] >> 7;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700589 valid_states = ucp[1];
590 }
591 off = 8 + (ucp[7] * 4);
592 }
593
594 sdev_printk(KERN_INFO, sdev,
Moger, Babudcd3a752012-03-27 20:56:08 +0000595 "%s: port group %02x state %c %s supports %c%c%c%c%c%c%c\n",
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700596 ALUA_DH_NAME, h->group_id, print_alua_state(h->state),
Moger, Babudcd3a752012-03-27 20:56:08 +0000597 h->pref ? "preferred" : "non-preferred",
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700598 valid_states&TPGS_SUPPORT_TRANSITION?'T':'t',
599 valid_states&TPGS_SUPPORT_OFFLINE?'O':'o',
Hannes Reinecke69723d12010-09-24 15:57:04 +0200600 valid_states&TPGS_SUPPORT_LBA_DEPENDENT?'L':'l',
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700601 valid_states&TPGS_SUPPORT_UNAVAILABLE?'U':'u',
602 valid_states&TPGS_SUPPORT_STANDBY?'S':'s',
603 valid_states&TPGS_SUPPORT_NONOPTIMIZED?'N':'n',
604 valid_states&TPGS_SUPPORT_OPTIMIZED?'A':'a');
605
Hannes Reinecke69723d12010-09-24 15:57:04 +0200606 switch (h->state) {
607 case TPGS_STATE_TRANSITIONING:
Stewart, Seana8e5a2d2013-10-15 15:52:54 +0000608 if (wait_for_transition) {
609 if (time_before(jiffies, expiry)) {
610 /* State transition, retry */
611 interval += 2000;
612 msleep(interval);
613 goto retry;
614 }
615 err = SCSI_DH_RETRY;
616 } else {
617 err = SCSI_DH_OK;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700618 }
Stewart, Seana8e5a2d2013-10-15 15:52:54 +0000619
Hannes Reinecke69723d12010-09-24 15:57:04 +0200620 /* Transitioning time exceeded, set port to standby */
Hannes Reinecke69723d12010-09-24 15:57:04 +0200621 h->state = TPGS_STATE_STANDBY;
622 break;
623 case TPGS_STATE_OFFLINE:
Bart Van Asschee47f8972012-08-24 09:08:41 +0000624 /* Path unusable */
Hannes Reinecke69723d12010-09-24 15:57:04 +0200625 err = SCSI_DH_DEV_OFFLINED;
626 break;
627 default:
628 /* Useable path if active */
629 err = SCSI_DH_OK;
630 break;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700631 }
632 return err;
633}
634
635/*
636 * alua_initialize - Initialize ALUA state
637 * @sdev: the device to be initialized
638 *
639 * For the prep_fn to work correctly we have
640 * to initialize the ALUA state for the device.
641 */
642static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h)
643{
644 int err;
645
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200646 err = alua_check_tpgs(sdev, h);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700647 if (err != SCSI_DH_OK)
648 goto out;
649
Hannes Reinecke9b80dce2015-12-01 10:16:44 +0100650 err = alua_check_vpd(sdev, h);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700651 if (err != SCSI_DH_OK)
652 goto out;
653
Stewart, Seana8e5a2d2013-10-15 15:52:54 +0000654 err = alua_rtpg(sdev, h, 0);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700655 if (err != SCSI_DH_OK)
656 goto out;
657
658out:
659 return err;
660}
Moger, Babu4335d092012-03-27 20:55:49 +0000661/*
662 * alua_set_params - set/unset the optimize flag
663 * @sdev: device on the path to be activated
664 * params - parameters in the following format
665 * "no_of_params\0param1\0param2\0param3\0...\0"
666 * For example, to set the flag pass the following parameters
667 * from multipath.conf
668 * hardware_handler "2 alua 1"
669 */
670static int alua_set_params(struct scsi_device *sdev, const char *params)
671{
Christoph Hellwigee14c672015-08-27 14:16:59 +0200672 struct alua_dh_data *h = sdev->handler_data;
Moger, Babu4335d092012-03-27 20:55:49 +0000673 unsigned int optimize = 0, argc;
674 const char *p = params;
675 int result = SCSI_DH_OK;
676
677 if ((sscanf(params, "%u", &argc) != 1) || (argc != 1))
678 return -EINVAL;
679
680 while (*p++)
681 ;
682 if ((sscanf(p, "%u", &optimize) != 1) || (optimize > 1))
683 return -EINVAL;
684
685 if (optimize)
686 h->flags |= ALUA_OPTIMIZE_STPG;
687 else
688 h->flags &= ~ALUA_OPTIMIZE_STPG;
689
690 return result;
691}
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700692
Stewart, Sean7a3ad392013-04-04 14:54:47 +0000693static uint optimize_stpg;
694module_param(optimize_stpg, uint, S_IRUGO|S_IWUSR);
695MODULE_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.");
696
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700697/*
698 * alua_activate - activate a path
699 * @sdev: device on the path to be activated
700 *
701 * We're currently switching the port group to be activated only and
702 * let the array figure out the rest.
703 * There may be other arrays which require us to switch all port groups
704 * based on a certain policy. But until we actually encounter them it
705 * should be okay.
706 */
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700707static int alua_activate(struct scsi_device *sdev,
708 activate_complete fn, void *data)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700709{
Christoph Hellwigee14c672015-08-27 14:16:59 +0200710 struct alua_dh_data *h = sdev->handler_data;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700711 int err = SCSI_DH_OK;
Moger, Babu72d9e0f2012-03-27 20:56:20 +0000712 int stpg = 0;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700713
Stewart, Seana8e5a2d2013-10-15 15:52:54 +0000714 err = alua_rtpg(sdev, h, 1);
Hannes Reinecke46ccf6b2011-08-24 10:51:16 +0200715 if (err != SCSI_DH_OK)
716 goto out;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700717
Stewart, Sean7a3ad392013-04-04 14:54:47 +0000718 if (optimize_stpg)
719 h->flags |= ALUA_OPTIMIZE_STPG;
720
Moger, Babu72d9e0f2012-03-27 20:56:20 +0000721 if (h->tpgs & TPGS_MODE_EXPLICIT) {
722 switch (h->state) {
723 case TPGS_STATE_NONOPTIMIZED:
724 stpg = 1;
725 if ((h->flags & ALUA_OPTIMIZE_STPG) &&
726 (!h->pref) &&
727 (h->tpgs & TPGS_MODE_IMPLICIT))
728 stpg = 0;
729 break;
730 case TPGS_STATE_STANDBY:
Bart Van Asschebb2c94a2012-06-22 08:49:38 +0000731 case TPGS_STATE_UNAVAILABLE:
Moger, Babu72d9e0f2012-03-27 20:56:20 +0000732 stpg = 1;
733 break;
Moger, Babu72d9e0f2012-03-27 20:56:20 +0000734 case TPGS_STATE_OFFLINE:
735 err = SCSI_DH_IO;
736 break;
737 case TPGS_STATE_TRANSITIONING:
738 err = SCSI_DH_RETRY;
739 break;
740 default:
741 break;
742 }
743 }
744
745 if (stpg) {
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700746 h->callback_fn = fn;
747 h->callback_data = data;
748 err = submit_stpg(h);
749 if (err == SCSI_DH_OK)
750 return 0;
751 h->callback_fn = h->callback_data = NULL;
752 }
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700753
754out:
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700755 if (fn)
756 fn(data, err);
757 return 0;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700758}
759
760/*
761 * alua_prep_fn - request callback
762 *
763 * Fail I/O to all paths not in state
764 * active/optimized or active/non-optimized.
765 */
766static int alua_prep_fn(struct scsi_device *sdev, struct request *req)
767{
Christoph Hellwigee14c672015-08-27 14:16:59 +0200768 struct alua_dh_data *h = sdev->handler_data;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700769 int ret = BLKPREP_OK;
770
Hannes Reinecke69723d12010-09-24 15:57:04 +0200771 if (h->state == TPGS_STATE_TRANSITIONING)
772 ret = BLKPREP_DEFER;
773 else if (h->state != TPGS_STATE_OPTIMIZED &&
774 h->state != TPGS_STATE_NONOPTIMIZED &&
775 h->state != TPGS_STATE_LBA_DEPENDENT) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700776 ret = BLKPREP_KILL;
777 req->cmd_flags |= REQ_QUIET;
778 }
779 return ret;
780
781}
782
Christoph Hellwig1d520322014-09-14 11:08:21 -0700783/*
784 * alua_bus_attach - Attach device handler
785 * @sdev: device to be attached to
786 */
Christoph Hellwigee14c672015-08-27 14:16:59 +0200787static int alua_bus_attach(struct scsi_device *sdev)
Christoph Hellwig1d520322014-09-14 11:08:21 -0700788{
789 struct alua_dh_data *h;
790 int err;
791
792 h = kzalloc(sizeof(*h) , GFP_KERNEL);
793 if (!h)
Christoph Hellwigee14c672015-08-27 14:16:59 +0200794 return -ENOMEM;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700795 h->tpgs = TPGS_MODE_UNINITIALIZED;
796 h->state = TPGS_STATE_OPTIMIZED;
797 h->group_id = -1;
798 h->rel_port = -1;
799 h->buff = h->inq;
800 h->bufflen = ALUA_INQUIRY_SIZE;
801 h->sdev = sdev;
802
803 err = alua_initialize(sdev, h);
804 if (err != SCSI_DH_OK && err != SCSI_DH_DEV_OFFLINED)
805 goto failed;
806
Christoph Hellwigee14c672015-08-27 14:16:59 +0200807 sdev->handler_data = h;
808 return 0;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700809failed:
810 kfree(h);
Christoph Hellwigee14c672015-08-27 14:16:59 +0200811 return -EINVAL;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700812}
813
814/*
815 * alua_bus_detach - Detach device handler
816 * @sdev: device to be detached from
817 */
818static void alua_bus_detach(struct scsi_device *sdev)
819{
Christoph Hellwigee14c672015-08-27 14:16:59 +0200820 struct alua_dh_data *h = sdev->handler_data;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700821
822 if (h->buff && h->inq != h->buff)
823 kfree(h->buff);
Christoph Hellwigee14c672015-08-27 14:16:59 +0200824 sdev->handler_data = NULL;
Christoph Hellwig1d520322014-09-14 11:08:21 -0700825 kfree(h);
826}
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700827
828static struct scsi_device_handler alua_dh = {
829 .name = ALUA_DH_NAME,
830 .module = THIS_MODULE,
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700831 .attach = alua_bus_attach,
832 .detach = alua_bus_detach,
833 .prep_fn = alua_prep_fn,
834 .check_sense = alua_check_sense,
835 .activate = alua_activate,
Moger, Babu4335d092012-03-27 20:55:49 +0000836 .set_params = alua_set_params,
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700837};
838
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700839static int __init alua_init(void)
840{
841 int r;
842
843 r = scsi_register_device_handler(&alua_dh);
844 if (r != 0)
845 printk(KERN_ERR "%s: Failed to register scsi device handler",
846 ALUA_DH_NAME);
847 return r;
848}
849
850static void __exit alua_exit(void)
851{
852 scsi_unregister_device_handler(&alua_dh);
853}
854
855module_init(alua_init);
856module_exit(alua_exit);
857
858MODULE_DESCRIPTION("DM Multipath ALUA support");
859MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
860MODULE_LICENSE("GPL");
861MODULE_VERSION(ALUA_DH_VER);