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 <scsi/scsi.h>
25 #include <scsi/scsi_eh.h>
26 #include <scsi/scsi_dh.h>
28 #define ALUA_DH_NAME "alua"
29 #define ALUA_DH_VER "1.3"
31 #define TPGS_STATE_OPTIMIZED 0x0
32 #define TPGS_STATE_NONOPTIMIZED 0x1
33 #define TPGS_STATE_STANDBY 0x2
34 #define TPGS_STATE_UNAVAILABLE 0x3
35 #define TPGS_STATE_LBA_DEPENDENT 0x4
36 #define TPGS_STATE_OFFLINE 0xe
37 #define TPGS_STATE_TRANSITIONING 0xf
39 #define TPGS_SUPPORT_NONE 0x00
40 #define TPGS_SUPPORT_OPTIMIZED 0x01
41 #define TPGS_SUPPORT_NONOPTIMIZED 0x02
42 #define TPGS_SUPPORT_STANDBY 0x04
43 #define TPGS_SUPPORT_UNAVAILABLE 0x08
44 #define TPGS_SUPPORT_LBA_DEPENDENT 0x10
45 #define TPGS_SUPPORT_OFFLINE 0x40
46 #define TPGS_SUPPORT_TRANSITION 0x80
48 #define TPGS_MODE_UNINITIALIZED -1
49 #define TPGS_MODE_NONE 0x0
50 #define TPGS_MODE_IMPLICIT 0x1
51 #define TPGS_MODE_EXPLICIT 0x2
53 #define ALUA_INQUIRY_SIZE 36
54 #define ALUA_FAILOVER_TIMEOUT (60 * HZ)
55 #define ALUA_FAILOVER_RETRIES 5
62 unsigned char inq[ALUA_INQUIRY_SIZE];
65 unsigned char sense[SCSI_SENSE_BUFFERSIZE];
67 struct scsi_device *sdev;
68 activate_complete callback_fn;
72 #define ALUA_POLICY_SWITCH_CURRENT 0
73 #define ALUA_POLICY_SWITCH_ALL 1
75 static char print_alua_state(int);
76 static int alua_check_sense(struct scsi_device *, struct scsi_sense_hdr *);
78 static inline struct alua_dh_data *get_alua_data(struct scsi_device *sdev)
80 struct scsi_dh_data *scsi_dh_data = sdev->scsi_dh_data;
81 BUG_ON(scsi_dh_data == NULL);
82 return ((struct alua_dh_data *) scsi_dh_data->buf);
85 static int realloc_buffer(struct alua_dh_data *h, unsigned len)
87 if (h->buff && h->buff != h->inq)
90 h->buff = kmalloc(len, GFP_NOIO);
93 h->bufflen = ALUA_INQUIRY_SIZE;
100 static struct request *get_alua_req(struct scsi_device *sdev,
101 void *buffer, unsigned buflen, int rw)
104 struct request_queue *q = sdev->request_queue;
106 rq = blk_get_request(q, rw, GFP_NOIO);
109 sdev_printk(KERN_INFO, sdev,
110 "%s: blk_get_request failed\n", __func__);
114 if (buflen && blk_rq_map_kern(q, rq, buffer, buflen, GFP_NOIO)) {
116 sdev_printk(KERN_INFO, sdev,
117 "%s: blk_rq_map_kern failed\n", __func__);
121 rq->cmd_type = REQ_TYPE_BLOCK_PC;
122 rq->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
124 rq->retries = ALUA_FAILOVER_RETRIES;
125 rq->timeout = ALUA_FAILOVER_TIMEOUT;
131 * submit_vpd_inquiry - Issue an INQUIRY VPD page 0x83 command
132 * @sdev: sdev the command should be sent to
134 static int submit_vpd_inquiry(struct scsi_device *sdev, struct alua_dh_data *h)
137 int err = SCSI_DH_RES_TEMP_UNAVAIL;
139 rq = get_alua_req(sdev, h->buff, h->bufflen, READ);
143 /* Prepare the command. */
144 rq->cmd[0] = INQUIRY;
147 rq->cmd[4] = h->bufflen;
148 rq->cmd_len = COMMAND_SIZE(INQUIRY);
150 rq->sense = h->sense;
151 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
152 rq->sense_len = h->senselen = 0;
154 err = blk_execute_rq(rq->q, NULL, rq, 1);
156 sdev_printk(KERN_INFO, sdev,
157 "%s: evpd inquiry failed with %x\n",
158 ALUA_DH_NAME, rq->errors);
159 h->senselen = rq->sense_len;
168 * submit_rtpg - Issue a REPORT TARGET GROUP STATES command
169 * @sdev: sdev the command should be sent to
171 static unsigned submit_rtpg(struct scsi_device *sdev, struct alua_dh_data *h)
174 int err = SCSI_DH_RES_TEMP_UNAVAIL;
176 rq = get_alua_req(sdev, h->buff, h->bufflen, READ);
180 /* Prepare the command. */
181 rq->cmd[0] = MAINTENANCE_IN;
182 rq->cmd[1] = MI_REPORT_TARGET_PGS;
183 rq->cmd[6] = (h->bufflen >> 24) & 0xff;
184 rq->cmd[7] = (h->bufflen >> 16) & 0xff;
185 rq->cmd[8] = (h->bufflen >> 8) & 0xff;
186 rq->cmd[9] = h->bufflen & 0xff;
187 rq->cmd_len = COMMAND_SIZE(MAINTENANCE_IN);
189 rq->sense = h->sense;
190 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
191 rq->sense_len = h->senselen = 0;
193 err = blk_execute_rq(rq->q, NULL, rq, 1);
195 sdev_printk(KERN_INFO, sdev,
196 "%s: rtpg failed with %x\n",
197 ALUA_DH_NAME, rq->errors);
198 h->senselen = rq->sense_len;
207 * alua_stpg - Evaluate SET TARGET GROUP STATES
208 * @sdev: the device to be evaluated
209 * @state: the new target group state
211 * Send a SET TARGET GROUP STATES command to the device.
212 * We only have to test here if we should resubmit the command;
213 * any other error is assumed as a failure.
215 static void stpg_endio(struct request *req, int error)
217 struct alua_dh_data *h = req->end_io_data;
218 struct scsi_sense_hdr sense_hdr;
219 unsigned err = SCSI_DH_OK;
221 if (error || host_byte(req->errors) != DID_OK ||
222 msg_byte(req->errors) != COMMAND_COMPLETE) {
227 if (h->senselen > 0) {
228 err = scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
234 err = alua_check_sense(h->sdev, &sense_hdr);
235 if (err == ADD_TO_MLQUEUE) {
239 sdev_printk(KERN_INFO, h->sdev,
240 "%s: stpg sense code: %02x/%02x/%02x\n",
241 ALUA_DH_NAME, sense_hdr.sense_key,
242 sense_hdr.asc, sense_hdr.ascq);
245 if (err == SCSI_DH_OK) {
246 h->state = TPGS_STATE_OPTIMIZED;
247 sdev_printk(KERN_INFO, h->sdev,
248 "%s: port group %02x switched to state %c\n",
249 ALUA_DH_NAME, h->group_id,
250 print_alua_state(h->state));
253 req->end_io_data = NULL;
254 __blk_put_request(req->q, req);
255 if (h->callback_fn) {
256 h->callback_fn(h->callback_data, err);
257 h->callback_fn = h->callback_data = NULL;
263 * submit_stpg - Issue a SET TARGET GROUP STATES command
265 * Currently we're only setting the current target port group state
266 * to 'active/optimized' and let the array firmware figure out
267 * the states of the remaining groups.
269 static unsigned submit_stpg(struct alua_dh_data *h)
273 struct scsi_device *sdev = h->sdev;
275 /* Prepare the data buffer */
276 memset(h->buff, 0, stpg_len);
277 h->buff[4] = TPGS_STATE_OPTIMIZED & 0x0f;
278 h->buff[6] = (h->group_id >> 8) & 0xff;
279 h->buff[7] = h->group_id & 0xff;
281 rq = get_alua_req(sdev, h->buff, stpg_len, WRITE);
283 return SCSI_DH_RES_TEMP_UNAVAIL;
285 /* Prepare the command. */
286 rq->cmd[0] = MAINTENANCE_OUT;
287 rq->cmd[1] = MO_SET_TARGET_PGS;
288 rq->cmd[6] = (stpg_len >> 24) & 0xff;
289 rq->cmd[7] = (stpg_len >> 16) & 0xff;
290 rq->cmd[8] = (stpg_len >> 8) & 0xff;
291 rq->cmd[9] = stpg_len & 0xff;
292 rq->cmd_len = COMMAND_SIZE(MAINTENANCE_OUT);
294 rq->sense = h->sense;
295 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
296 rq->sense_len = h->senselen = 0;
299 blk_execute_rq_nowait(rq->q, NULL, rq, 1, stpg_endio);
304 * alua_check_tpgs - Evaluate TPGS setting
305 * @sdev: device to be checked
307 * Examine the TPGS setting of the sdev to find out if ALUA
310 static int alua_check_tpgs(struct scsi_device *sdev, struct alua_dh_data *h)
312 int err = SCSI_DH_OK;
314 h->tpgs = scsi_device_tpgs(sdev);
316 case TPGS_MODE_EXPLICIT|TPGS_MODE_IMPLICIT:
317 sdev_printk(KERN_INFO, sdev,
318 "%s: supports implicit and explicit TPGS\n",
321 case TPGS_MODE_EXPLICIT:
322 sdev_printk(KERN_INFO, sdev, "%s: supports explicit TPGS\n",
325 case TPGS_MODE_IMPLICIT:
326 sdev_printk(KERN_INFO, sdev, "%s: supports implicit TPGS\n",
330 h->tpgs = TPGS_MODE_NONE;
331 sdev_printk(KERN_INFO, sdev, "%s: not supported\n",
333 err = SCSI_DH_DEV_UNSUPP;
341 * alua_vpd_inquiry - Evaluate INQUIRY vpd page 0x83
342 * @sdev: device to be checked
344 * Extract the relative target port and the target port group
345 * descriptor from the list of identificators.
347 static int alua_vpd_inquiry(struct scsi_device *sdev, struct alua_dh_data *h)
354 err = submit_vpd_inquiry(sdev, h);
356 if (err != SCSI_DH_OK)
359 /* Check if vpd page exceeds initial buffer */
360 len = (h->buff[2] << 8) + h->buff[3] + 4;
361 if (len > h->bufflen) {
362 /* Resubmit with the correct length */
363 if (realloc_buffer(h, len)) {
364 sdev_printk(KERN_WARNING, sdev,
365 "%s: kmalloc buffer failed\n",
367 /* Temporary failure, bypass */
368 return SCSI_DH_DEV_TEMP_BUSY;
374 * Now look for the correct descriptor.
377 while (d < h->buff + len) {
378 switch (d[1] & 0xf) {
380 /* Relative target port */
381 h->rel_port = (d[6] << 8) + d[7];
384 /* Target port group */
385 h->group_id = (d[6] << 8) + d[7];
393 if (h->group_id == -1) {
395 * Internal error; TPGS supported but required
396 * VPD identification descriptors not present.
397 * Disable ALUA support
399 sdev_printk(KERN_INFO, sdev,
400 "%s: No target port descriptors found\n",
402 h->state = TPGS_STATE_OPTIMIZED;
403 h->tpgs = TPGS_MODE_NONE;
404 err = SCSI_DH_DEV_UNSUPP;
406 sdev_printk(KERN_INFO, sdev,
407 "%s: port group %02x rel port %02x\n",
408 ALUA_DH_NAME, h->group_id, h->rel_port);
414 static char print_alua_state(int state)
417 case TPGS_STATE_OPTIMIZED:
419 case TPGS_STATE_NONOPTIMIZED:
421 case TPGS_STATE_STANDBY:
423 case TPGS_STATE_UNAVAILABLE:
425 case TPGS_STATE_LBA_DEPENDENT:
427 case TPGS_STATE_OFFLINE:
429 case TPGS_STATE_TRANSITIONING:
436 static int alua_check_sense(struct scsi_device *sdev,
437 struct scsi_sense_hdr *sense_hdr)
439 switch (sense_hdr->sense_key) {
441 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0a)
443 * LUN Not Accessible - ALUA state transition
445 return ADD_TO_MLQUEUE;
446 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0b)
448 * LUN Not Accessible -- Target port in standby state
451 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0c)
453 * LUN Not Accessible -- Target port in unavailable state
456 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x12)
458 * LUN Not Ready -- Offline
463 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
465 * Power On, Reset, or Bus Device Reset, just retry.
467 return ADD_TO_MLQUEUE;
468 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06)
472 return ADD_TO_MLQUEUE;
473 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07)
475 * Implicit ALUA state transition failed
477 return ADD_TO_MLQUEUE;
478 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x03)
480 * Inquiry data has changed
482 return ADD_TO_MLQUEUE;
483 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x0e)
485 * REPORTED_LUNS_DATA_HAS_CHANGED is reported
486 * when switching controllers on targets like
487 * Intel Multi-Flex. We can just retry.
489 return ADD_TO_MLQUEUE;
493 return SCSI_RETURN_NOT_HANDLED;
497 * alua_rtpg - Evaluate REPORT TARGET GROUP STATES
498 * @sdev: the device to be evaluated.
500 * Evaluate the Target Port Group State.
501 * Returns SCSI_DH_DEV_OFFLINED if the path is
502 * found to be unusable.
504 static int alua_rtpg(struct scsi_device *sdev, struct alua_dh_data *h)
506 struct scsi_sense_hdr sense_hdr;
507 int len, k, off, valid_states = 0;
510 unsigned long expiry, interval = 1000;
512 expiry = round_jiffies_up(jiffies + ALUA_FAILOVER_TIMEOUT);
514 err = submit_rtpg(sdev, h);
516 if (err == SCSI_DH_IO && h->senselen > 0) {
517 err = scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
522 err = alua_check_sense(sdev, &sense_hdr);
523 if (err == ADD_TO_MLQUEUE && time_before(jiffies, expiry))
525 sdev_printk(KERN_INFO, sdev,
526 "%s: rtpg sense code %02x/%02x/%02x\n",
527 ALUA_DH_NAME, sense_hdr.sense_key,
528 sense_hdr.asc, sense_hdr.ascq);
531 if (err != SCSI_DH_OK)
534 len = (h->buff[0] << 24) + (h->buff[1] << 16) +
535 (h->buff[2] << 8) + h->buff[3] + 4;
537 if (len > h->bufflen) {
538 /* Resubmit with the correct length */
539 if (realloc_buffer(h, len)) {
540 sdev_printk(KERN_WARNING, sdev,
541 "%s: kmalloc buffer failed\n",__func__);
542 /* Temporary failure, bypass */
543 return SCSI_DH_DEV_TEMP_BUSY;
548 for (k = 4, ucp = h->buff + 4; k < len; k += off, ucp += off) {
549 if (h->group_id == (ucp[2] << 8) + ucp[3]) {
550 h->state = ucp[0] & 0x0f;
551 valid_states = ucp[1];
553 off = 8 + (ucp[7] * 4);
556 sdev_printk(KERN_INFO, sdev,
557 "%s: port group %02x state %c supports %c%c%c%c%c%c%c\n",
558 ALUA_DH_NAME, h->group_id, print_alua_state(h->state),
559 valid_states&TPGS_SUPPORT_TRANSITION?'T':'t',
560 valid_states&TPGS_SUPPORT_OFFLINE?'O':'o',
561 valid_states&TPGS_SUPPORT_LBA_DEPENDENT?'L':'l',
562 valid_states&TPGS_SUPPORT_UNAVAILABLE?'U':'u',
563 valid_states&TPGS_SUPPORT_STANDBY?'S':'s',
564 valid_states&TPGS_SUPPORT_NONOPTIMIZED?'N':'n',
565 valid_states&TPGS_SUPPORT_OPTIMIZED?'A':'a');
568 case TPGS_STATE_TRANSITIONING:
569 if (time_before(jiffies, expiry)) {
570 /* State transition, retry */
575 /* Transitioning time exceeded, set port to standby */
577 h->state = TPGS_STATE_STANDBY;
579 case TPGS_STATE_OFFLINE:
580 case TPGS_STATE_UNAVAILABLE:
581 /* Path unusable for unavailable/offline */
582 err = SCSI_DH_DEV_OFFLINED;
585 /* Useable path if active */
593 * alua_initialize - Initialize ALUA state
594 * @sdev: the device to be initialized
596 * For the prep_fn to work correctly we have
597 * to initialize the ALUA state for the device.
599 static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h)
603 err = alua_check_tpgs(sdev, h);
604 if (err != SCSI_DH_OK)
607 err = alua_vpd_inquiry(sdev, h);
608 if (err != SCSI_DH_OK)
611 err = alua_rtpg(sdev, h);
612 if (err != SCSI_DH_OK)
620 * alua_activate - activate a path
621 * @sdev: device on the path to be activated
623 * We're currently switching the port group to be activated only and
624 * let the array figure out the rest.
625 * There may be other arrays which require us to switch all port groups
626 * based on a certain policy. But until we actually encounter them it
629 static int alua_activate(struct scsi_device *sdev,
630 activate_complete fn, void *data)
632 struct alua_dh_data *h = get_alua_data(sdev);
633 int err = SCSI_DH_OK;
635 err = alua_rtpg(sdev, h);
636 if (err != SCSI_DH_OK)
639 if (h->tpgs & TPGS_MODE_EXPLICIT &&
640 h->state != TPGS_STATE_OPTIMIZED &&
641 h->state != TPGS_STATE_LBA_DEPENDENT) {
643 h->callback_data = data;
644 err = submit_stpg(h);
645 if (err == SCSI_DH_OK)
647 h->callback_fn = h->callback_data = NULL;
657 * alua_prep_fn - request callback
659 * Fail I/O to all paths not in state
660 * active/optimized or active/non-optimized.
662 static int alua_prep_fn(struct scsi_device *sdev, struct request *req)
664 struct alua_dh_data *h = get_alua_data(sdev);
665 int ret = BLKPREP_OK;
667 if (h->state == TPGS_STATE_TRANSITIONING)
669 else if (h->state != TPGS_STATE_OPTIMIZED &&
670 h->state != TPGS_STATE_NONOPTIMIZED &&
671 h->state != TPGS_STATE_LBA_DEPENDENT) {
673 req->cmd_flags |= REQ_QUIET;
679 static bool alua_match(struct scsi_device *sdev)
681 return (scsi_device_tpgs(sdev) != 0);
684 static int alua_bus_attach(struct scsi_device *sdev);
685 static void alua_bus_detach(struct scsi_device *sdev);
687 static struct scsi_device_handler alua_dh = {
688 .name = ALUA_DH_NAME,
689 .module = THIS_MODULE,
690 .attach = alua_bus_attach,
691 .detach = alua_bus_detach,
692 .prep_fn = alua_prep_fn,
693 .check_sense = alua_check_sense,
694 .activate = alua_activate,
699 * alua_bus_attach - Attach device handler
700 * @sdev: device to be attached to
702 static int alua_bus_attach(struct scsi_device *sdev)
704 struct scsi_dh_data *scsi_dh_data;
705 struct alua_dh_data *h;
707 int err = SCSI_DH_OK;
709 scsi_dh_data = kzalloc(sizeof(*scsi_dh_data)
710 + sizeof(*h) , GFP_KERNEL);
712 sdev_printk(KERN_ERR, sdev, "%s: Attach failed\n",
717 scsi_dh_data->scsi_dh = &alua_dh;
718 h = (struct alua_dh_data *) scsi_dh_data->buf;
719 h->tpgs = TPGS_MODE_UNINITIALIZED;
720 h->state = TPGS_STATE_OPTIMIZED;
724 h->bufflen = ALUA_INQUIRY_SIZE;
727 err = alua_initialize(sdev, h);
728 if ((err != SCSI_DH_OK) && (err != SCSI_DH_DEV_OFFLINED))
731 if (!try_module_get(THIS_MODULE))
734 spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
735 sdev->scsi_dh_data = scsi_dh_data;
736 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
737 sdev_printk(KERN_NOTICE, sdev, "%s: Attached\n", ALUA_DH_NAME);
743 sdev_printk(KERN_ERR, sdev, "%s: not attached\n", ALUA_DH_NAME);
748 * alua_bus_detach - Detach device handler
749 * @sdev: device to be detached from
751 static void alua_bus_detach(struct scsi_device *sdev)
753 struct scsi_dh_data *scsi_dh_data;
754 struct alua_dh_data *h;
757 spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
758 scsi_dh_data = sdev->scsi_dh_data;
759 sdev->scsi_dh_data = NULL;
760 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
762 h = (struct alua_dh_data *) scsi_dh_data->buf;
763 if (h->buff && h->inq != h->buff)
766 module_put(THIS_MODULE);
767 sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", ALUA_DH_NAME);
770 static int __init alua_init(void)
774 r = scsi_register_device_handler(&alua_dh);
776 printk(KERN_ERR "%s: Failed to register scsi device handler",
781 static void __exit alua_exit(void)
783 scsi_unregister_device_handler(&alua_dh);
786 module_init(alua_init);
787 module_exit(alua_exit);
789 MODULE_DESCRIPTION("DM Multipath ALUA support");
790 MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
791 MODULE_LICENSE("GPL");
792 MODULE_VERSION(ALUA_DH_VER);