2 * Generic SCSI-3 ALUA SCSI Device Handler
4 * Copyright (C) 2007-2010 Hannes Reinecke, SUSE Linux Products GmbH.
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.
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.
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.
22 #include <linux/slab.h>
23 #include <linux/delay.h>
24 #include <linux/module.h>
25 #include <asm/unaligned.h>
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_dbg.h>
28 #include <scsi/scsi_eh.h>
29 #include <scsi/scsi_dh.h>
31 #define ALUA_DH_NAME "alua"
32 #define ALUA_DH_VER "1.3"
34 #define TPGS_STATE_OPTIMIZED 0x0
35 #define TPGS_STATE_NONOPTIMIZED 0x1
36 #define TPGS_STATE_STANDBY 0x2
37 #define TPGS_STATE_UNAVAILABLE 0x3
38 #define TPGS_STATE_LBA_DEPENDENT 0x4
39 #define TPGS_STATE_OFFLINE 0xe
40 #define TPGS_STATE_TRANSITIONING 0xf
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
47 #define TPGS_SUPPORT_LBA_DEPENDENT 0x10
48 #define TPGS_SUPPORT_OFFLINE 0x40
49 #define TPGS_SUPPORT_TRANSITION 0x80
51 #define RTPG_FMT_MASK 0x70
52 #define RTPG_FMT_EXT_HDR 0x10
54 #define TPGS_MODE_UNINITIALIZED -1
55 #define TPGS_MODE_NONE 0x0
56 #define TPGS_MODE_IMPLICIT 0x1
57 #define TPGS_MODE_EXPLICIT 0x2
59 #define ALUA_INQUIRY_SIZE 36
60 #define ALUA_FAILOVER_TIMEOUT 60
61 #define ALUA_FAILOVER_RETRIES 5
63 /* device handler flags */
64 #define ALUA_OPTIMIZE_STPG 1
65 #define ALUA_RTPG_EXT_HDR_UNSUPP 2
73 unsigned flags; /* used for optimizing STPG */
74 unsigned char inq[ALUA_INQUIRY_SIZE];
77 unsigned char transition_tmo;
78 unsigned char sense[SCSI_SENSE_BUFFERSIZE];
79 struct scsi_device *sdev;
80 activate_complete callback_fn;
84 #define ALUA_POLICY_SWITCH_CURRENT 0
85 #define ALUA_POLICY_SWITCH_ALL 1
87 static char print_alua_state(int);
89 static int realloc_buffer(struct alua_dh_data *h, unsigned len)
91 if (h->buff && h->buff != h->inq)
94 h->buff = kmalloc(len, GFP_NOIO);
97 h->bufflen = ALUA_INQUIRY_SIZE;
104 static struct request *get_alua_req(struct scsi_device *sdev,
105 void *buffer, unsigned buflen, int rw)
108 struct request_queue *q = sdev->request_queue;
110 rq = blk_get_request(q, rw, GFP_NOIO);
113 sdev_printk(KERN_INFO, sdev,
114 "%s: blk_get_request failed\n", __func__);
117 blk_rq_set_block_pc(rq);
119 if (buflen && blk_rq_map_kern(q, rq, buffer, buflen, GFP_NOIO)) {
121 sdev_printk(KERN_INFO, sdev,
122 "%s: blk_rq_map_kern failed\n", __func__);
126 rq->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
128 rq->retries = ALUA_FAILOVER_RETRIES;
129 rq->timeout = ALUA_FAILOVER_TIMEOUT * HZ;
135 * submit_rtpg - Issue a REPORT TARGET GROUP STATES command
136 * @sdev: sdev the command should be sent to
138 static unsigned submit_rtpg(struct scsi_device *sdev, struct alua_dh_data *h)
143 rq = get_alua_req(sdev, h->buff, h->bufflen, READ);
145 err = DRIVER_BUSY << 24;
149 /* Prepare the command. */
150 rq->cmd[0] = MAINTENANCE_IN;
151 if (!(h->flags & ALUA_RTPG_EXT_HDR_UNSUPP))
152 rq->cmd[1] = MI_REPORT_TARGET_PGS | MI_EXT_HDR_PARAM_FMT;
154 rq->cmd[1] = MI_REPORT_TARGET_PGS;
155 put_unaligned_be32(h->bufflen, &rq->cmd[6]);
156 rq->cmd_len = COMMAND_SIZE(MAINTENANCE_IN);
158 rq->sense = h->sense;
159 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
162 blk_execute_rq(rq->q, NULL, rq, 1);
171 * stpg_endio - Evaluate SET TARGET GROUP STATES
172 * @sdev: the device to be evaluated
173 * @state: the new target group state
175 * Evaluate a SET TARGET GROUP STATES command response.
177 static void stpg_endio(struct request *req, int error)
179 struct alua_dh_data *h = req->end_io_data;
180 struct scsi_sense_hdr sense_hdr;
181 unsigned err = SCSI_DH_OK;
183 if (host_byte(req->errors) != DID_OK ||
184 msg_byte(req->errors) != COMMAND_COMPLETE) {
189 if (scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
191 if (sense_hdr.sense_key == NOT_READY &&
192 sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x0a) {
193 /* ALUA state transition already in progress */
197 if (sense_hdr.sense_key == UNIT_ATTENTION) {
201 sdev_printk(KERN_INFO, h->sdev, "%s: stpg failed\n",
203 scsi_print_sense_hdr(h->sdev, ALUA_DH_NAME, &sense_hdr);
208 if (err == SCSI_DH_OK) {
209 h->state = TPGS_STATE_OPTIMIZED;
210 sdev_printk(KERN_INFO, h->sdev,
211 "%s: port group %02x switched to state %c\n",
212 ALUA_DH_NAME, h->group_id,
213 print_alua_state(h->state));
216 req->end_io_data = NULL;
217 __blk_put_request(req->q, req);
218 if (h->callback_fn) {
219 h->callback_fn(h->callback_data, err);
220 h->callback_fn = h->callback_data = NULL;
226 * submit_stpg - Issue a SET TARGET GROUP STATES command
228 * Currently we're only setting the current target port group state
229 * to 'active/optimized' and let the array firmware figure out
230 * the states of the remaining groups.
232 static unsigned submit_stpg(struct alua_dh_data *h)
236 struct scsi_device *sdev = h->sdev;
238 /* Prepare the data buffer */
239 memset(h->buff, 0, stpg_len);
240 h->buff[4] = TPGS_STATE_OPTIMIZED & 0x0f;
241 put_unaligned_be16(h->group_id, &h->buff[6]);
243 rq = get_alua_req(sdev, h->buff, stpg_len, WRITE);
245 return SCSI_DH_RES_TEMP_UNAVAIL;
247 /* Prepare the command. */
248 rq->cmd[0] = MAINTENANCE_OUT;
249 rq->cmd[1] = MO_SET_TARGET_PGS;
250 put_unaligned_be32(stpg_len, &rq->cmd[6]);
251 rq->cmd_len = COMMAND_SIZE(MAINTENANCE_OUT);
253 rq->sense = h->sense;
254 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
258 blk_execute_rq_nowait(rq->q, NULL, rq, 1, stpg_endio);
263 * alua_check_tpgs - Evaluate TPGS setting
264 * @sdev: device to be checked
266 * Examine the TPGS setting of the sdev to find out if ALUA
269 static int alua_check_tpgs(struct scsi_device *sdev)
271 int tpgs = TPGS_MODE_NONE;
274 * ALUA support for non-disk devices is fraught with
275 * difficulties, so disable it for now.
277 if (sdev->type != TYPE_DISK) {
278 sdev_printk(KERN_INFO, sdev,
279 "%s: disable for non-disk devices\n",
284 tpgs = scsi_device_tpgs(sdev);
286 case TPGS_MODE_EXPLICIT|TPGS_MODE_IMPLICIT:
287 sdev_printk(KERN_INFO, sdev,
288 "%s: supports implicit and explicit TPGS\n",
291 case TPGS_MODE_EXPLICIT:
292 sdev_printk(KERN_INFO, sdev, "%s: supports explicit TPGS\n",
295 case TPGS_MODE_IMPLICIT:
296 sdev_printk(KERN_INFO, sdev, "%s: supports implicit TPGS\n",
300 sdev_printk(KERN_INFO, sdev, "%s: not supported\n",
304 sdev_printk(KERN_INFO, sdev,
305 "%s: unsupported TPGS setting %d\n",
307 tpgs = TPGS_MODE_NONE;
315 * alua_check_vpd - Evaluate INQUIRY vpd page 0x83
316 * @sdev: device to be checked
318 * Extract the relative target port and the target port group
319 * descriptor from the list of identificators.
321 static int alua_check_vpd(struct scsi_device *sdev, struct alua_dh_data *h)
324 unsigned char __rcu *vpd_pg83;
327 if (!rcu_dereference(sdev->vpd_pg83)) {
329 return SCSI_DH_DEV_UNSUPP;
333 * Look for the correct descriptor.
335 vpd_pg83 = rcu_dereference(sdev->vpd_pg83);
337 while (d < vpd_pg83 + sdev->vpd_pg83_len) {
338 switch (d[1] & 0xf) {
340 /* Relative target port */
341 h->rel_port = get_unaligned_be16(&d[6]);
344 /* Target port group */
345 h->group_id = get_unaligned_be16(&d[6]);
354 if (h->group_id == -1) {
356 * Internal error; TPGS supported but required
357 * VPD identification descriptors not present.
358 * Disable ALUA support
360 sdev_printk(KERN_INFO, sdev,
361 "%s: No target port descriptors found\n",
363 h->state = TPGS_STATE_OPTIMIZED;
364 h->tpgs = TPGS_MODE_NONE;
365 return SCSI_DH_DEV_UNSUPP;
367 sdev_printk(KERN_INFO, sdev,
368 "%s: port group %02x rel port %02x\n",
369 ALUA_DH_NAME, h->group_id, h->rel_port);
374 static char print_alua_state(int state)
377 case TPGS_STATE_OPTIMIZED:
379 case TPGS_STATE_NONOPTIMIZED:
381 case TPGS_STATE_STANDBY:
383 case TPGS_STATE_UNAVAILABLE:
385 case TPGS_STATE_LBA_DEPENDENT:
387 case TPGS_STATE_OFFLINE:
389 case TPGS_STATE_TRANSITIONING:
396 static int alua_check_sense(struct scsi_device *sdev,
397 struct scsi_sense_hdr *sense_hdr)
399 switch (sense_hdr->sense_key) {
401 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0a)
403 * LUN Not Accessible - ALUA state transition
405 return ADD_TO_MLQUEUE;
408 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
410 * Power On, Reset, or Bus Device Reset, just retry.
412 return ADD_TO_MLQUEUE;
413 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x04)
415 * Device internal reset
417 return ADD_TO_MLQUEUE;
418 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x01)
420 * Mode Parameters Changed
422 return ADD_TO_MLQUEUE;
423 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06)
427 return ADD_TO_MLQUEUE;
428 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07)
430 * Implicit ALUA state transition failed
432 return ADD_TO_MLQUEUE;
433 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x03)
435 * Inquiry data has changed
437 return ADD_TO_MLQUEUE;
438 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x0e)
440 * REPORTED_LUNS_DATA_HAS_CHANGED is reported
441 * when switching controllers on targets like
442 * Intel Multi-Flex. We can just retry.
444 return ADD_TO_MLQUEUE;
448 return SCSI_RETURN_NOT_HANDLED;
452 * alua_rtpg - Evaluate REPORT TARGET GROUP STATES
453 * @sdev: the device to be evaluated.
454 * @wait_for_transition: if nonzero, wait ALUA_FAILOVER_TIMEOUT seconds for device to exit transitioning state
456 * Evaluate the Target Port Group State.
457 * Returns SCSI_DH_DEV_OFFLINED if the path is
458 * found to be unusable.
460 static int alua_rtpg(struct scsi_device *sdev, struct alua_dh_data *h, int wait_for_transition)
462 struct scsi_sense_hdr sense_hdr;
463 int len, k, off, valid_states = 0;
465 unsigned err, retval;
466 unsigned long expiry, interval = 0;
467 unsigned int tpg_desc_tbl_off;
468 unsigned char orig_transition_tmo;
470 if (!h->transition_tmo)
471 expiry = round_jiffies_up(jiffies + ALUA_FAILOVER_TIMEOUT * HZ);
473 expiry = round_jiffies_up(jiffies + h->transition_tmo * HZ);
476 retval = submit_rtpg(sdev, h);
478 if (!scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
480 sdev_printk(KERN_INFO, sdev,
481 "%s: rtpg failed, result %d\n",
482 ALUA_DH_NAME, retval);
483 if (driver_byte(retval) == DRIVER_BUSY)
484 return SCSI_DH_DEV_TEMP_BUSY;
489 * submit_rtpg() has failed on existing arrays
490 * when requesting extended header info, and
491 * the array doesn't support extended headers,
492 * even though it shouldn't according to T10.
493 * The retry without rtpg_ext_hdr_req set
496 if (!(h->flags & ALUA_RTPG_EXT_HDR_UNSUPP) &&
497 sense_hdr.sense_key == ILLEGAL_REQUEST &&
498 sense_hdr.asc == 0x24 && sense_hdr.ascq == 0) {
499 h->flags |= ALUA_RTPG_EXT_HDR_UNSUPP;
503 * Retry on ALUA state transition or if any
504 * UNIT ATTENTION occurred.
506 if (sense_hdr.sense_key == NOT_READY &&
507 sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x0a)
509 else if (sense_hdr.sense_key == UNIT_ATTENTION)
511 if (err == SCSI_DH_RETRY && time_before(jiffies, expiry)) {
512 sdev_printk(KERN_ERR, sdev, "%s: rtpg retry\n",
514 scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr);
517 sdev_printk(KERN_ERR, sdev, "%s: rtpg failed\n",
519 scsi_print_sense_hdr(sdev, ALUA_DH_NAME, &sense_hdr);
523 len = get_unaligned_be32(&h->buff[0]) + 4;
525 if (len > h->bufflen) {
526 /* Resubmit with the correct length */
527 if (realloc_buffer(h, len)) {
528 sdev_printk(KERN_WARNING, sdev,
529 "%s: kmalloc buffer failed\n",__func__);
530 /* Temporary failure, bypass */
531 return SCSI_DH_DEV_TEMP_BUSY;
536 orig_transition_tmo = h->transition_tmo;
537 if ((h->buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR && h->buff[5] != 0)
538 h->transition_tmo = h->buff[5];
540 h->transition_tmo = ALUA_FAILOVER_TIMEOUT;
542 if (wait_for_transition && (orig_transition_tmo != h->transition_tmo)) {
543 sdev_printk(KERN_INFO, sdev,
544 "%s: transition timeout set to %d seconds\n",
545 ALUA_DH_NAME, h->transition_tmo);
546 expiry = jiffies + h->transition_tmo * HZ;
549 if ((h->buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR)
550 tpg_desc_tbl_off = 8;
552 tpg_desc_tbl_off = 4;
554 for (k = tpg_desc_tbl_off, ucp = h->buff + tpg_desc_tbl_off;
556 k += off, ucp += off) {
558 if (h->group_id == get_unaligned_be16(&ucp[2])) {
559 h->state = ucp[0] & 0x0f;
560 h->pref = ucp[0] >> 7;
561 valid_states = ucp[1];
563 off = 8 + (ucp[7] * 4);
566 sdev_printk(KERN_INFO, sdev,
567 "%s: port group %02x state %c %s supports %c%c%c%c%c%c%c\n",
568 ALUA_DH_NAME, h->group_id, print_alua_state(h->state),
569 h->pref ? "preferred" : "non-preferred",
570 valid_states&TPGS_SUPPORT_TRANSITION?'T':'t',
571 valid_states&TPGS_SUPPORT_OFFLINE?'O':'o',
572 valid_states&TPGS_SUPPORT_LBA_DEPENDENT?'L':'l',
573 valid_states&TPGS_SUPPORT_UNAVAILABLE?'U':'u',
574 valid_states&TPGS_SUPPORT_STANDBY?'S':'s',
575 valid_states&TPGS_SUPPORT_NONOPTIMIZED?'N':'n',
576 valid_states&TPGS_SUPPORT_OPTIMIZED?'A':'a');
579 case TPGS_STATE_TRANSITIONING:
580 if (wait_for_transition) {
581 if (time_before(jiffies, expiry)) {
582 /* State transition, retry */
592 /* Transitioning time exceeded, set port to standby */
593 h->state = TPGS_STATE_STANDBY;
595 case TPGS_STATE_OFFLINE:
597 err = SCSI_DH_DEV_OFFLINED;
600 /* Useable path if active */
608 * alua_initialize - Initialize ALUA state
609 * @sdev: the device to be initialized
611 * For the prep_fn to work correctly we have
612 * to initialize the ALUA state for the device.
614 static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h)
616 int err = SCSI_DH_DEV_UNSUPP;
618 h->tpgs = alua_check_tpgs(sdev);
619 if (h->tpgs == TPGS_MODE_NONE)
622 err = alua_check_vpd(sdev, h);
623 if (err != SCSI_DH_OK)
626 err = alua_rtpg(sdev, h, 0);
627 if (err != SCSI_DH_OK)
634 * alua_set_params - set/unset the optimize flag
635 * @sdev: device on the path to be activated
636 * params - parameters in the following format
637 * "no_of_params\0param1\0param2\0param3\0...\0"
638 * For example, to set the flag pass the following parameters
639 * from multipath.conf
640 * hardware_handler "2 alua 1"
642 static int alua_set_params(struct scsi_device *sdev, const char *params)
644 struct alua_dh_data *h = sdev->handler_data;
645 unsigned int optimize = 0, argc;
646 const char *p = params;
647 int result = SCSI_DH_OK;
649 if ((sscanf(params, "%u", &argc) != 1) || (argc != 1))
654 if ((sscanf(p, "%u", &optimize) != 1) || (optimize > 1))
658 h->flags |= ALUA_OPTIMIZE_STPG;
660 h->flags &= ~ALUA_OPTIMIZE_STPG;
665 static uint optimize_stpg;
666 module_param(optimize_stpg, uint, S_IRUGO|S_IWUSR);
667 MODULE_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.");
670 * alua_activate - activate a path
671 * @sdev: device on the path to be activated
673 * We're currently switching the port group to be activated only and
674 * let the array figure out the rest.
675 * There may be other arrays which require us to switch all port groups
676 * based on a certain policy. But until we actually encounter them it
679 static int alua_activate(struct scsi_device *sdev,
680 activate_complete fn, void *data)
682 struct alua_dh_data *h = sdev->handler_data;
683 int err = SCSI_DH_OK;
686 err = alua_rtpg(sdev, h, 1);
687 if (err != SCSI_DH_OK)
691 h->flags |= ALUA_OPTIMIZE_STPG;
693 if (h->tpgs & TPGS_MODE_EXPLICIT) {
695 case TPGS_STATE_NONOPTIMIZED:
697 if ((h->flags & ALUA_OPTIMIZE_STPG) &&
699 (h->tpgs & TPGS_MODE_IMPLICIT))
702 case TPGS_STATE_STANDBY:
703 case TPGS_STATE_UNAVAILABLE:
706 case TPGS_STATE_OFFLINE:
709 case TPGS_STATE_TRANSITIONING:
719 h->callback_data = data;
720 err = submit_stpg(h);
721 if (err == SCSI_DH_OK)
723 h->callback_fn = h->callback_data = NULL;
733 * alua_prep_fn - request callback
735 * Fail I/O to all paths not in state
736 * active/optimized or active/non-optimized.
738 static int alua_prep_fn(struct scsi_device *sdev, struct request *req)
740 struct alua_dh_data *h = sdev->handler_data;
741 int ret = BLKPREP_OK;
743 if (h->state == TPGS_STATE_TRANSITIONING)
745 else if (h->state != TPGS_STATE_OPTIMIZED &&
746 h->state != TPGS_STATE_NONOPTIMIZED &&
747 h->state != TPGS_STATE_LBA_DEPENDENT) {
749 req->cmd_flags |= REQ_QUIET;
756 * alua_bus_attach - Attach device handler
757 * @sdev: device to be attached to
759 static int alua_bus_attach(struct scsi_device *sdev)
761 struct alua_dh_data *h;
764 h = kzalloc(sizeof(*h) , GFP_KERNEL);
767 h->tpgs = TPGS_MODE_UNINITIALIZED;
768 h->state = TPGS_STATE_OPTIMIZED;
772 h->bufflen = ALUA_INQUIRY_SIZE;
775 err = alua_initialize(sdev, h);
776 if (err != SCSI_DH_OK && err != SCSI_DH_DEV_OFFLINED)
779 sdev->handler_data = h;
787 * alua_bus_detach - Detach device handler
788 * @sdev: device to be detached from
790 static void alua_bus_detach(struct scsi_device *sdev)
792 struct alua_dh_data *h = sdev->handler_data;
794 if (h->buff && h->inq != h->buff)
796 sdev->handler_data = NULL;
800 static struct scsi_device_handler alua_dh = {
801 .name = ALUA_DH_NAME,
802 .module = THIS_MODULE,
803 .attach = alua_bus_attach,
804 .detach = alua_bus_detach,
805 .prep_fn = alua_prep_fn,
806 .check_sense = alua_check_sense,
807 .activate = alua_activate,
808 .set_params = alua_set_params,
811 static int __init alua_init(void)
815 r = scsi_register_device_handler(&alua_dh);
817 printk(KERN_ERR "%s: Failed to register scsi device handler",
822 static void __exit alua_exit(void)
824 scsi_unregister_device_handler(&alua_dh);
827 module_init(alua_init);
828 module_exit(alua_exit);
830 MODULE_DESCRIPTION("DM Multipath ALUA support");
831 MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
832 MODULE_LICENSE("GPL");
833 MODULE_VERSION(ALUA_DH_VER);