blob: 78205cc2059c4e640bec01787cff8aef26d9ebd6 [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 inline struct alua_dh_data *get_alua_data(struct scsi_device *sdev)
89{
90 struct scsi_dh_data *scsi_dh_data = sdev->scsi_dh_data;
91 BUG_ON(scsi_dh_data == NULL);
92 return ((struct alua_dh_data *) scsi_dh_data->buf);
93}
94
95static int realloc_buffer(struct alua_dh_data *h, unsigned len)
96{
97 if (h->buff && h->buff != h->inq)
98 kfree(h->buff);
99
100 h->buff = kmalloc(len, GFP_NOIO);
101 if (!h->buff) {
102 h->buff = h->inq;
103 h->bufflen = ALUA_INQUIRY_SIZE;
104 return 1;
105 }
106 h->bufflen = len;
107 return 0;
108}
109
110static struct request *get_alua_req(struct scsi_device *sdev,
111 void *buffer, unsigned buflen, int rw)
112{
113 struct request *rq;
114 struct request_queue *q = sdev->request_queue;
115
116 rq = blk_get_request(q, rw, GFP_NOIO);
117
118 if (!rq) {
119 sdev_printk(KERN_INFO, sdev,
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700120 "%s: blk_get_request failed\n", __func__);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700121 return NULL;
122 }
123
124 if (buflen && blk_rq_map_kern(q, rq, buffer, buflen, GFP_NOIO)) {
125 blk_put_request(rq);
126 sdev_printk(KERN_INFO, sdev,
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700127 "%s: blk_rq_map_kern failed\n", __func__);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700128 return NULL;
129 }
130
131 rq->cmd_type = REQ_TYPE_BLOCK_PC;
Mike Christie6000a362008-08-19 18:45:30 -0500132 rq->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
Mike Christie64f84bc2008-09-06 08:39:16 -0500133 REQ_FAILFAST_DRIVER;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700134 rq->retries = ALUA_FAILOVER_RETRIES;
Rob Evers3588c5a2012-05-18 14:08:54 -0400135 rq->timeout = ALUA_FAILOVER_TIMEOUT * HZ;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700136
137 return rq;
138}
139
140/*
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700141 * submit_vpd_inquiry - Issue an INQUIRY VPD page 0x83 command
142 * @sdev: sdev the command should be sent to
143 */
144static int submit_vpd_inquiry(struct scsi_device *sdev, struct alua_dh_data *h)
145{
146 struct request *rq;
147 int err = SCSI_DH_RES_TEMP_UNAVAIL;
148
149 rq = get_alua_req(sdev, h->buff, h->bufflen, READ);
150 if (!rq)
151 goto done;
152
153 /* Prepare the command. */
154 rq->cmd[0] = INQUIRY;
155 rq->cmd[1] = 1;
156 rq->cmd[2] = 0x83;
157 rq->cmd[4] = h->bufflen;
158 rq->cmd_len = COMMAND_SIZE(INQUIRY);
159
160 rq->sense = h->sense;
161 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
162 rq->sense_len = h->senselen = 0;
163
164 err = blk_execute_rq(rq->q, NULL, rq, 1);
165 if (err == -EIO) {
166 sdev_printk(KERN_INFO, sdev,
167 "%s: evpd inquiry failed with %x\n",
168 ALUA_DH_NAME, rq->errors);
169 h->senselen = rq->sense_len;
170 err = SCSI_DH_IO;
171 }
172 blk_put_request(rq);
173done:
174 return err;
175}
176
177/*
178 * submit_rtpg - Issue a REPORT TARGET GROUP STATES command
179 * @sdev: sdev the command should be sent to
180 */
Rob Evers8e67ce62012-05-18 14:08:55 -0400181static unsigned submit_rtpg(struct scsi_device *sdev, struct alua_dh_data *h,
182 bool rtpg_ext_hdr_req)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700183{
184 struct request *rq;
185 int err = SCSI_DH_RES_TEMP_UNAVAIL;
186
187 rq = get_alua_req(sdev, h->buff, h->bufflen, READ);
188 if (!rq)
189 goto done;
190
191 /* Prepare the command. */
192 rq->cmd[0] = MAINTENANCE_IN;
Rob Evers8e67ce62012-05-18 14:08:55 -0400193 if (rtpg_ext_hdr_req)
194 rq->cmd[1] = MI_REPORT_TARGET_PGS | MI_EXT_HDR_PARAM_FMT;
195 else
196 rq->cmd[1] = MI_REPORT_TARGET_PGS;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700197 rq->cmd[6] = (h->bufflen >> 24) & 0xff;
198 rq->cmd[7] = (h->bufflen >> 16) & 0xff;
199 rq->cmd[8] = (h->bufflen >> 8) & 0xff;
200 rq->cmd[9] = h->bufflen & 0xff;
201 rq->cmd_len = COMMAND_SIZE(MAINTENANCE_IN);
202
203 rq->sense = h->sense;
204 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
205 rq->sense_len = h->senselen = 0;
206
207 err = blk_execute_rq(rq->q, NULL, rq, 1);
208 if (err == -EIO) {
209 sdev_printk(KERN_INFO, sdev,
210 "%s: rtpg failed with %x\n",
211 ALUA_DH_NAME, rq->errors);
212 h->senselen = rq->sense_len;
213 err = SCSI_DH_IO;
214 }
215 blk_put_request(rq);
216done:
217 return err;
218}
219
220/*
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700221 * alua_stpg - Evaluate SET TARGET GROUP STATES
222 * @sdev: the device to be evaluated
223 * @state: the new target group state
224 *
225 * Send a SET TARGET GROUP STATES command to the device.
226 * We only have to test here if we should resubmit the command;
227 * any other error is assumed as a failure.
228 */
229static void stpg_endio(struct request *req, int error)
230{
231 struct alua_dh_data *h = req->end_io_data;
232 struct scsi_sense_hdr sense_hdr;
Joseph Gruher93499232011-01-05 16:00:22 -0500233 unsigned err = SCSI_DH_OK;
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700234
Mike Christie27db6822013-03-05 22:40:24 -0600235 if (host_byte(req->errors) != DID_OK ||
236 msg_byte(req->errors) != COMMAND_COMPLETE) {
Joseph Gruher93499232011-01-05 16:00:22 -0500237 err = SCSI_DH_IO;
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700238 goto done;
Joseph Gruher93499232011-01-05 16:00:22 -0500239 }
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700240
Mike Christie27db6822013-03-05 22:40:24 -0600241 if (req->sense_len > 0) {
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700242 err = scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
243 &sense_hdr);
244 if (!err) {
245 err = SCSI_DH_IO;
246 goto done;
247 }
248 err = alua_check_sense(h->sdev, &sense_hdr);
249 if (err == ADD_TO_MLQUEUE) {
250 err = SCSI_DH_RETRY;
251 goto done;
252 }
253 sdev_printk(KERN_INFO, h->sdev,
254 "%s: stpg sense code: %02x/%02x/%02x\n",
255 ALUA_DH_NAME, sense_hdr.sense_key,
256 sense_hdr.asc, sense_hdr.ascq);
257 err = SCSI_DH_IO;
Mike Christie27db6822013-03-05 22:40:24 -0600258 } else if (error)
259 err = SCSI_DH_IO;
260
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700261 if (err == SCSI_DH_OK) {
262 h->state = TPGS_STATE_OPTIMIZED;
263 sdev_printk(KERN_INFO, h->sdev,
264 "%s: port group %02x switched to state %c\n",
265 ALUA_DH_NAME, h->group_id,
266 print_alua_state(h->state));
267 }
268done:
Joseph Gruhered0f36b2011-01-05 16:00:21 -0500269 req->end_io_data = NULL;
270 __blk_put_request(req->q, req);
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700271 if (h->callback_fn) {
272 h->callback_fn(h->callback_data, err);
273 h->callback_fn = h->callback_data = NULL;
274 }
275 return;
276}
277
278/*
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700279 * submit_stpg - Issue a SET TARGET GROUP STATES command
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700280 *
281 * Currently we're only setting the current target port group state
282 * to 'active/optimized' and let the array firmware figure out
283 * the states of the remaining groups.
284 */
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700285static unsigned submit_stpg(struct alua_dh_data *h)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700286{
287 struct request *rq;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700288 int stpg_len = 8;
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700289 struct scsi_device *sdev = h->sdev;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700290
291 /* Prepare the data buffer */
292 memset(h->buff, 0, stpg_len);
293 h->buff[4] = TPGS_STATE_OPTIMIZED & 0x0f;
Ilgu Hongef3fa8c2009-01-30 17:00:09 -0600294 h->buff[6] = (h->group_id >> 8) & 0xff;
295 h->buff[7] = h->group_id & 0xff;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700296
297 rq = get_alua_req(sdev, h->buff, stpg_len, WRITE);
298 if (!rq)
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700299 return SCSI_DH_RES_TEMP_UNAVAIL;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700300
301 /* Prepare the command. */
302 rq->cmd[0] = MAINTENANCE_OUT;
303 rq->cmd[1] = MO_SET_TARGET_PGS;
304 rq->cmd[6] = (stpg_len >> 24) & 0xff;
305 rq->cmd[7] = (stpg_len >> 16) & 0xff;
306 rq->cmd[8] = (stpg_len >> 8) & 0xff;
307 rq->cmd[9] = stpg_len & 0xff;
308 rq->cmd_len = COMMAND_SIZE(MAINTENANCE_OUT);
309
310 rq->sense = h->sense;
311 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
312 rq->sense_len = h->senselen = 0;
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700313 rq->end_io_data = h;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700314
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700315 blk_execute_rq_nowait(rq->q, NULL, rq, 1, stpg_endio);
Joseph Gruher7c66e9a2011-01-05 16:00:20 -0500316 return SCSI_DH_OK;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700317}
318
319/*
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200320 * alua_check_tpgs - Evaluate TPGS setting
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700321 * @sdev: device to be checked
322 *
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200323 * Examine the TPGS setting of the sdev to find out if ALUA
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700324 * is supported.
325 */
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200326static int alua_check_tpgs(struct scsi_device *sdev, struct alua_dh_data *h)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700327{
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200328 int err = SCSI_DH_OK;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700329
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200330 h->tpgs = scsi_device_tpgs(sdev);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700331 switch (h->tpgs) {
332 case TPGS_MODE_EXPLICIT|TPGS_MODE_IMPLICIT:
333 sdev_printk(KERN_INFO, sdev,
334 "%s: supports implicit and explicit TPGS\n",
335 ALUA_DH_NAME);
336 break;
337 case TPGS_MODE_EXPLICIT:
338 sdev_printk(KERN_INFO, sdev, "%s: supports explicit TPGS\n",
339 ALUA_DH_NAME);
340 break;
341 case TPGS_MODE_IMPLICIT:
342 sdev_printk(KERN_INFO, sdev, "%s: supports implicit TPGS\n",
343 ALUA_DH_NAME);
344 break;
345 default:
346 h->tpgs = TPGS_MODE_NONE;
347 sdev_printk(KERN_INFO, sdev, "%s: not supported\n",
348 ALUA_DH_NAME);
349 err = SCSI_DH_DEV_UNSUPP;
350 break;
351 }
352
353 return err;
354}
355
356/*
357 * alua_vpd_inquiry - Evaluate INQUIRY vpd page 0x83
358 * @sdev: device to be checked
359 *
360 * Extract the relative target port and the target port group
361 * descriptor from the list of identificators.
362 */
363static int alua_vpd_inquiry(struct scsi_device *sdev, struct alua_dh_data *h)
364{
365 int len;
366 unsigned err;
367 unsigned char *d;
368
369 retry:
370 err = submit_vpd_inquiry(sdev, h);
371
372 if (err != SCSI_DH_OK)
373 return err;
374
375 /* Check if vpd page exceeds initial buffer */
376 len = (h->buff[2] << 8) + h->buff[3] + 4;
377 if (len > h->bufflen) {
378 /* Resubmit with the correct length */
379 if (realloc_buffer(h, len)) {
380 sdev_printk(KERN_WARNING, sdev,
381 "%s: kmalloc buffer failed\n",
382 ALUA_DH_NAME);
383 /* Temporary failure, bypass */
384 return SCSI_DH_DEV_TEMP_BUSY;
385 }
386 goto retry;
387 }
388
389 /*
390 * Now look for the correct descriptor.
391 */
392 d = h->buff + 4;
393 while (d < h->buff + len) {
394 switch (d[1] & 0xf) {
395 case 0x4:
396 /* Relative target port */
397 h->rel_port = (d[6] << 8) + d[7];
398 break;
399 case 0x5:
400 /* Target port group */
401 h->group_id = (d[6] << 8) + d[7];
402 break;
403 default:
404 break;
405 }
406 d += d[3] + 4;
407 }
408
409 if (h->group_id == -1) {
410 /*
411 * Internal error; TPGS supported but required
412 * VPD identification descriptors not present.
413 * Disable ALUA support
414 */
415 sdev_printk(KERN_INFO, sdev,
416 "%s: No target port descriptors found\n",
417 ALUA_DH_NAME);
418 h->state = TPGS_STATE_OPTIMIZED;
419 h->tpgs = TPGS_MODE_NONE;
420 err = SCSI_DH_DEV_UNSUPP;
421 } else {
422 sdev_printk(KERN_INFO, sdev,
423 "%s: port group %02x rel port %02x\n",
424 ALUA_DH_NAME, h->group_id, h->rel_port);
425 }
426
427 return err;
428}
429
430static char print_alua_state(int state)
431{
432 switch (state) {
433 case TPGS_STATE_OPTIMIZED:
434 return 'A';
435 case TPGS_STATE_NONOPTIMIZED:
436 return 'N';
437 case TPGS_STATE_STANDBY:
438 return 'S';
439 case TPGS_STATE_UNAVAILABLE:
440 return 'U';
Hannes Reinecke69723d12010-09-24 15:57:04 +0200441 case TPGS_STATE_LBA_DEPENDENT:
442 return 'L';
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700443 case TPGS_STATE_OFFLINE:
444 return 'O';
445 case TPGS_STATE_TRANSITIONING:
446 return 'T';
447 default:
448 return 'X';
449 }
450}
451
452static int alua_check_sense(struct scsi_device *sdev,
453 struct scsi_sense_hdr *sense_hdr)
454{
455 switch (sense_hdr->sense_key) {
456 case NOT_READY:
457 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0a)
458 /*
459 * LUN Not Accessible - ALUA state transition
460 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700461 return ADD_TO_MLQUEUE;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700462 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0b)
463 /*
464 * LUN Not Accessible -- Target port in standby state
465 */
466 return SUCCESS;
467 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0c)
468 /*
469 * LUN Not Accessible -- Target port in unavailable state
470 */
471 return SUCCESS;
472 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x12)
473 /*
474 * LUN Not Ready -- Offline
475 */
476 return SUCCESS;
477 break;
478 case UNIT_ATTENTION:
479 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
480 /*
481 * Power On, Reset, or Bus Device Reset, just retry.
482 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700483 return ADD_TO_MLQUEUE;
Stewart, Seanc20ee7b2013-10-15 15:52:39 +0000484 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x04)
485 /*
486 * Device internal reset
487 */
488 return ADD_TO_MLQUEUE;
Moger, Babu410f02d2011-12-21 18:01:37 -0500489 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x01)
490 /*
491 * Mode Parameters Changed
492 */
493 return ADD_TO_MLQUEUE;
Moger, Babubf819732011-10-06 13:22:07 -0400494 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700495 /*
496 * ALUA state changed
497 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700498 return ADD_TO_MLQUEUE;
Moger, Babubf819732011-10-06 13:22:07 -0400499 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700500 /*
501 * Implicit ALUA state transition failed
502 */
Mike Andersonc7dbb622008-08-12 12:11:57 -0700503 return ADD_TO_MLQUEUE;
Moger, Babubf819732011-10-06 13:22:07 -0400504 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x03)
505 /*
506 * Inquiry data has changed
507 */
508 return ADD_TO_MLQUEUE;
509 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x0e)
Ilgu Hong4d086f62009-01-30 17:00:11 -0600510 /*
511 * REPORTED_LUNS_DATA_HAS_CHANGED is reported
512 * when switching controllers on targets like
513 * Intel Multi-Flex. We can just retry.
514 */
515 return ADD_TO_MLQUEUE;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700516 break;
517 }
518
519 return SCSI_RETURN_NOT_HANDLED;
520}
521
522/*
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700523 * alua_rtpg - Evaluate REPORT TARGET GROUP STATES
524 * @sdev: the device to be evaluated.
525 *
526 * Evaluate the Target Port Group State.
527 * Returns SCSI_DH_DEV_OFFLINED if the path is
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300528 * found to be unusable.
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700529 */
530static int alua_rtpg(struct scsi_device *sdev, struct alua_dh_data *h)
531{
532 struct scsi_sense_hdr sense_hdr;
533 int len, k, off, valid_states = 0;
Hannes Reineckecfde3fa2011-08-24 10:51:18 +0200534 unsigned char *ucp;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700535 unsigned err;
Rob Evers8e67ce62012-05-18 14:08:55 -0400536 bool rtpg_ext_hdr_req = 1;
Rob Eversbc97f4b2012-05-18 14:08:56 -0400537 unsigned long expiry, interval = 0;
Rob Evers3588c5a2012-05-18 14:08:54 -0400538 unsigned int tpg_desc_tbl_off;
539 unsigned char orig_transition_tmo;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700540
Rob Evers3588c5a2012-05-18 14:08:54 -0400541 if (!h->transition_tmo)
542 expiry = round_jiffies_up(jiffies + ALUA_FAILOVER_TIMEOUT * HZ);
543 else
544 expiry = round_jiffies_up(jiffies + h->transition_tmo * HZ);
545
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700546 retry:
Rob Evers8e67ce62012-05-18 14:08:55 -0400547 err = submit_rtpg(sdev, h, rtpg_ext_hdr_req);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700548
549 if (err == SCSI_DH_IO && h->senselen > 0) {
550 err = scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
551 &sense_hdr);
552 if (!err)
553 return SCSI_DH_IO;
554
Rob Evers8e67ce62012-05-18 14:08:55 -0400555 /*
556 * submit_rtpg() has failed on existing arrays
557 * when requesting extended header info, and
558 * the array doesn't support extended headers,
559 * even though it shouldn't according to T10.
560 * The retry without rtpg_ext_hdr_req set
561 * handles this.
562 */
563 if (rtpg_ext_hdr_req == 1 &&
564 sense_hdr.sense_key == ILLEGAL_REQUEST &&
565 sense_hdr.asc == 0x24 && sense_hdr.ascq == 0) {
566 rtpg_ext_hdr_req = 0;
567 goto retry;
568 }
569
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700570 err = alua_check_sense(sdev, &sense_hdr);
Hannes Reinecke69723d12010-09-24 15:57:04 +0200571 if (err == ADD_TO_MLQUEUE && time_before(jiffies, expiry))
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700572 goto retry;
573 sdev_printk(KERN_INFO, sdev,
574 "%s: rtpg sense code %02x/%02x/%02x\n",
575 ALUA_DH_NAME, sense_hdr.sense_key,
576 sense_hdr.asc, sense_hdr.ascq);
577 err = SCSI_DH_IO;
578 }
579 if (err != SCSI_DH_OK)
580 return err;
581
582 len = (h->buff[0] << 24) + (h->buff[1] << 16) +
583 (h->buff[2] << 8) + h->buff[3] + 4;
584
585 if (len > h->bufflen) {
586 /* Resubmit with the correct length */
587 if (realloc_buffer(h, len)) {
588 sdev_printk(KERN_WARNING, sdev,
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700589 "%s: kmalloc buffer failed\n",__func__);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700590 /* Temporary failure, bypass */
591 return SCSI_DH_DEV_TEMP_BUSY;
592 }
593 goto retry;
594 }
595
Rob Evers3588c5a2012-05-18 14:08:54 -0400596 orig_transition_tmo = h->transition_tmo;
597 if ((h->buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR && h->buff[5] != 0)
598 h->transition_tmo = h->buff[5];
599 else
600 h->transition_tmo = ALUA_FAILOVER_TIMEOUT;
601
602 if (orig_transition_tmo != h->transition_tmo) {
603 sdev_printk(KERN_INFO, sdev,
604 "%s: transition timeout set to %d seconds\n",
605 ALUA_DH_NAME, h->transition_tmo);
606 expiry = jiffies + h->transition_tmo * HZ;
607 }
608
609 if ((h->buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR)
610 tpg_desc_tbl_off = 8;
611 else
612 tpg_desc_tbl_off = 4;
613
614 for (k = tpg_desc_tbl_off, ucp = h->buff + tpg_desc_tbl_off;
615 k < len;
616 k += off, ucp += off) {
617
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700618 if (h->group_id == (ucp[2] << 8) + ucp[3]) {
619 h->state = ucp[0] & 0x0f;
Moger, Babudcd3a752012-03-27 20:56:08 +0000620 h->pref = ucp[0] >> 7;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700621 valid_states = ucp[1];
622 }
623 off = 8 + (ucp[7] * 4);
624 }
625
626 sdev_printk(KERN_INFO, sdev,
Moger, Babudcd3a752012-03-27 20:56:08 +0000627 "%s: port group %02x state %c %s supports %c%c%c%c%c%c%c\n",
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700628 ALUA_DH_NAME, h->group_id, print_alua_state(h->state),
Moger, Babudcd3a752012-03-27 20:56:08 +0000629 h->pref ? "preferred" : "non-preferred",
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700630 valid_states&TPGS_SUPPORT_TRANSITION?'T':'t',
631 valid_states&TPGS_SUPPORT_OFFLINE?'O':'o',
Hannes Reinecke69723d12010-09-24 15:57:04 +0200632 valid_states&TPGS_SUPPORT_LBA_DEPENDENT?'L':'l',
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700633 valid_states&TPGS_SUPPORT_UNAVAILABLE?'U':'u',
634 valid_states&TPGS_SUPPORT_STANDBY?'S':'s',
635 valid_states&TPGS_SUPPORT_NONOPTIMIZED?'N':'n',
636 valid_states&TPGS_SUPPORT_OPTIMIZED?'A':'a');
637
Hannes Reinecke69723d12010-09-24 15:57:04 +0200638 switch (h->state) {
639 case TPGS_STATE_TRANSITIONING:
640 if (time_before(jiffies, expiry)) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700641 /* State transition, retry */
Rob Eversbc97f4b2012-05-18 14:08:56 -0400642 interval += 2000;
Hannes Reinecke69723d12010-09-24 15:57:04 +0200643 msleep(interval);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700644 goto retry;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700645 }
Hannes Reinecke69723d12010-09-24 15:57:04 +0200646 /* Transitioning time exceeded, set port to standby */
647 err = SCSI_DH_RETRY;
648 h->state = TPGS_STATE_STANDBY;
649 break;
650 case TPGS_STATE_OFFLINE:
Bart Van Asschee47f8972012-08-24 09:08:41 +0000651 /* Path unusable */
Hannes Reinecke69723d12010-09-24 15:57:04 +0200652 err = SCSI_DH_DEV_OFFLINED;
653 break;
654 default:
655 /* Useable path if active */
656 err = SCSI_DH_OK;
657 break;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700658 }
659 return err;
660}
661
662/*
663 * alua_initialize - Initialize ALUA state
664 * @sdev: the device to be initialized
665 *
666 * For the prep_fn to work correctly we have
667 * to initialize the ALUA state for the device.
668 */
669static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h)
670{
671 int err;
672
Hannes Reinecked7c48fe2011-08-24 10:51:13 +0200673 err = alua_check_tpgs(sdev, h);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700674 if (err != SCSI_DH_OK)
675 goto out;
676
677 err = alua_vpd_inquiry(sdev, h);
678 if (err != SCSI_DH_OK)
679 goto out;
680
681 err = alua_rtpg(sdev, h);
682 if (err != SCSI_DH_OK)
683 goto out;
684
685out:
686 return err;
687}
Moger, Babu4335d092012-03-27 20:55:49 +0000688/*
689 * alua_set_params - set/unset the optimize flag
690 * @sdev: device on the path to be activated
691 * params - parameters in the following format
692 * "no_of_params\0param1\0param2\0param3\0...\0"
693 * For example, to set the flag pass the following parameters
694 * from multipath.conf
695 * hardware_handler "2 alua 1"
696 */
697static int alua_set_params(struct scsi_device *sdev, const char *params)
698{
699 struct alua_dh_data *h = get_alua_data(sdev);
700 unsigned int optimize = 0, argc;
701 const char *p = params;
702 int result = SCSI_DH_OK;
703
704 if ((sscanf(params, "%u", &argc) != 1) || (argc != 1))
705 return -EINVAL;
706
707 while (*p++)
708 ;
709 if ((sscanf(p, "%u", &optimize) != 1) || (optimize > 1))
710 return -EINVAL;
711
712 if (optimize)
713 h->flags |= ALUA_OPTIMIZE_STPG;
714 else
715 h->flags &= ~ALUA_OPTIMIZE_STPG;
716
717 return result;
718}
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700719
Stewart, Sean7a3ad392013-04-04 14:54:47 +0000720static uint optimize_stpg;
721module_param(optimize_stpg, uint, S_IRUGO|S_IWUSR);
722MODULE_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.");
723
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700724/*
725 * alua_activate - activate a path
726 * @sdev: device on the path to be activated
727 *
728 * We're currently switching the port group to be activated only and
729 * let the array figure out the rest.
730 * There may be other arrays which require us to switch all port groups
731 * based on a certain policy. But until we actually encounter them it
732 * should be okay.
733 */
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700734static int alua_activate(struct scsi_device *sdev,
735 activate_complete fn, void *data)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700736{
737 struct alua_dh_data *h = get_alua_data(sdev);
738 int err = SCSI_DH_OK;
Moger, Babu72d9e0f2012-03-27 20:56:20 +0000739 int stpg = 0;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700740
Hannes Reinecke46ccf6b2011-08-24 10:51:16 +0200741 err = alua_rtpg(sdev, h);
742 if (err != SCSI_DH_OK)
743 goto out;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700744
Stewart, Sean7a3ad392013-04-04 14:54:47 +0000745 if (optimize_stpg)
746 h->flags |= ALUA_OPTIMIZE_STPG;
747
Moger, Babu72d9e0f2012-03-27 20:56:20 +0000748 if (h->tpgs & TPGS_MODE_EXPLICIT) {
749 switch (h->state) {
750 case TPGS_STATE_NONOPTIMIZED:
751 stpg = 1;
752 if ((h->flags & ALUA_OPTIMIZE_STPG) &&
753 (!h->pref) &&
754 (h->tpgs & TPGS_MODE_IMPLICIT))
755 stpg = 0;
756 break;
757 case TPGS_STATE_STANDBY:
Bart Van Asschebb2c94a2012-06-22 08:49:38 +0000758 case TPGS_STATE_UNAVAILABLE:
Moger, Babu72d9e0f2012-03-27 20:56:20 +0000759 stpg = 1;
760 break;
Moger, Babu72d9e0f2012-03-27 20:56:20 +0000761 case TPGS_STATE_OFFLINE:
762 err = SCSI_DH_IO;
763 break;
764 case TPGS_STATE_TRANSITIONING:
765 err = SCSI_DH_RETRY;
766 break;
767 default:
768 break;
769 }
770 }
771
772 if (stpg) {
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700773 h->callback_fn = fn;
774 h->callback_data = data;
775 err = submit_stpg(h);
776 if (err == SCSI_DH_OK)
777 return 0;
778 h->callback_fn = h->callback_data = NULL;
779 }
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700780
781out:
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -0700782 if (fn)
783 fn(data, err);
784 return 0;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700785}
786
787/*
788 * alua_prep_fn - request callback
789 *
790 * Fail I/O to all paths not in state
791 * active/optimized or active/non-optimized.
792 */
793static int alua_prep_fn(struct scsi_device *sdev, struct request *req)
794{
795 struct alua_dh_data *h = get_alua_data(sdev);
796 int ret = BLKPREP_OK;
797
Hannes Reinecke69723d12010-09-24 15:57:04 +0200798 if (h->state == TPGS_STATE_TRANSITIONING)
799 ret = BLKPREP_DEFER;
800 else if (h->state != TPGS_STATE_OPTIMIZED &&
801 h->state != TPGS_STATE_NONOPTIMIZED &&
802 h->state != TPGS_STATE_LBA_DEPENDENT) {
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700803 ret = BLKPREP_KILL;
804 req->cmd_flags |= REQ_QUIET;
805 }
806 return ret;
807
808}
809
Hannes Reinecke6c3633d2011-08-24 10:51:15 +0200810static bool alua_match(struct scsi_device *sdev)
811{
812 return (scsi_device_tpgs(sdev) != 0);
813}
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700814
815static int alua_bus_attach(struct scsi_device *sdev);
816static void alua_bus_detach(struct scsi_device *sdev);
817
818static struct scsi_device_handler alua_dh = {
819 .name = ALUA_DH_NAME,
820 .module = THIS_MODULE,
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700821 .attach = alua_bus_attach,
822 .detach = alua_bus_detach,
823 .prep_fn = alua_prep_fn,
824 .check_sense = alua_check_sense,
825 .activate = alua_activate,
Moger, Babu4335d092012-03-27 20:55:49 +0000826 .set_params = alua_set_params,
Hannes Reinecke6c3633d2011-08-24 10:51:15 +0200827 .match = alua_match,
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700828};
829
830/*
831 * alua_bus_attach - Attach device handler
832 * @sdev: device to be attached to
833 */
834static int alua_bus_attach(struct scsi_device *sdev)
835{
836 struct scsi_dh_data *scsi_dh_data;
837 struct alua_dh_data *h;
838 unsigned long flags;
839 int err = SCSI_DH_OK;
840
Hillf Danton9dfeb312011-02-11 15:17:33 -0700841 scsi_dh_data = kzalloc(sizeof(*scsi_dh_data)
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700842 + sizeof(*h) , GFP_KERNEL);
843 if (!scsi_dh_data) {
844 sdev_printk(KERN_ERR, sdev, "%s: Attach failed\n",
845 ALUA_DH_NAME);
846 return -ENOMEM;
847 }
848
849 scsi_dh_data->scsi_dh = &alua_dh;
850 h = (struct alua_dh_data *) scsi_dh_data->buf;
851 h->tpgs = TPGS_MODE_UNINITIALIZED;
852 h->state = TPGS_STATE_OPTIMIZED;
853 h->group_id = -1;
854 h->rel_port = -1;
855 h->buff = h->inq;
856 h->bufflen = ALUA_INQUIRY_SIZE;
Chandra Seetharaman96e65862009-10-21 09:23:04 -0700857 h->sdev = sdev;
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700858
859 err = alua_initialize(sdev, h);
Martin Georgec0d289b2011-04-26 18:27:05 +0530860 if ((err != SCSI_DH_OK) && (err != SCSI_DH_DEV_OFFLINED))
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700861 goto failed;
862
863 if (!try_module_get(THIS_MODULE))
864 goto failed;
865
866 spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
867 sdev->scsi_dh_data = scsi_dh_data;
868 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
Moger, Babuab720022011-10-27 14:36:32 -0400869 sdev_printk(KERN_NOTICE, sdev, "%s: Attached\n", ALUA_DH_NAME);
Hannes Reinecke057ea7c2008-07-17 16:53:21 -0700870
871 return 0;
872
873failed:
874 kfree(scsi_dh_data);
875 sdev_printk(KERN_ERR, sdev, "%s: not attached\n", ALUA_DH_NAME);
876 return -EINVAL;
877}
878
879/*
880 * alua_bus_detach - Detach device handler
881 * @sdev: device to be detached from
882 */
883static void alua_bus_detach(struct scsi_device *sdev)
884{
885 struct scsi_dh_data *scsi_dh_data;
886 struct alua_dh_data *h;
887 unsigned long flags;
888
889 spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
890 scsi_dh_data = sdev->scsi_dh_data;
891 sdev->scsi_dh_data = NULL;
892 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
893
894 h = (struct alua_dh_data *) scsi_dh_data->buf;
895 if (h->buff && h->inq != h->buff)
896 kfree(h->buff);
897 kfree(scsi_dh_data);
898 module_put(THIS_MODULE);
899 sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", ALUA_DH_NAME);
900}
901
902static int __init alua_init(void)
903{
904 int r;
905
906 r = scsi_register_device_handler(&alua_dh);
907 if (r != 0)
908 printk(KERN_ERR "%s: Failed to register scsi device handler",
909 ALUA_DH_NAME);
910 return r;
911}
912
913static void __exit alua_exit(void)
914{
915 scsi_unregister_device_handler(&alua_dh);
916}
917
918module_init(alua_init);
919module_exit(alua_exit);
920
921MODULE_DESCRIPTION("DM Multipath ALUA support");
922MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
923MODULE_LICENSE("GPL");
924MODULE_VERSION(ALUA_DH_VER);