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_RTPG_SIZE 128
60 #define ALUA_FAILOVER_TIMEOUT 60
61 #define ALUA_FAILOVER_RETRIES 5
62 #define ALUA_RTPG_DELAY_MSECS 5
64 /* device handler flags */
65 #define ALUA_OPTIMIZE_STPG 0x01
66 #define ALUA_RTPG_EXT_HDR_UNSUPP 0x02
67 #define ALUA_SYNC_STPG 0x04
68 /* State machine flags */
69 #define ALUA_PG_RUN_RTPG 0x10
70 #define ALUA_PG_RUN_STPG 0x20
71 #define ALUA_PG_RUNNING 0x40
73 static uint optimize_stpg
;
74 module_param(optimize_stpg
, uint
, S_IRUGO
|S_IWUSR
);
75 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.");
77 static LIST_HEAD(port_group_list
);
78 static DEFINE_SPINLOCK(port_group_lock
);
79 static struct workqueue_struct
*kaluad_wq
;
80 static struct workqueue_struct
*kaluad_sync_wq
;
82 struct alua_port_group
{
85 struct list_head node
;
86 unsigned char device_id_str
[256];
92 unsigned flags
; /* used for optimizing STPG */
93 unsigned char transition_tmo
;
95 unsigned long interval
;
96 struct delayed_work rtpg_work
;
98 struct list_head rtpg_list
;
99 struct scsi_device
*rtpg_sdev
;
102 struct alua_dh_data
{
103 struct alua_port_group
*pg
;
106 struct scsi_device
*sdev
;
108 struct mutex init_mutex
;
111 struct alua_queue_data
{
112 struct list_head entry
;
113 activate_complete callback_fn
;
117 #define ALUA_POLICY_SWITCH_CURRENT 0
118 #define ALUA_POLICY_SWITCH_ALL 1
120 static void alua_rtpg_work(struct work_struct
*work
);
121 static void alua_rtpg_queue(struct alua_port_group
*pg
,
122 struct scsi_device
*sdev
,
123 struct alua_queue_data
*qdata
);
125 static void release_port_group(struct kref
*kref
)
127 struct alua_port_group
*pg
;
129 pg
= container_of(kref
, struct alua_port_group
, kref
);
131 flush_delayed_work(&pg
->rtpg_work
);
132 spin_lock(&port_group_lock
);
134 spin_unlock(&port_group_lock
);
139 * submit_rtpg - Issue a REPORT TARGET GROUP STATES command
140 * @sdev: sdev the command should be sent to
142 static int submit_rtpg(struct scsi_device
*sdev
, unsigned char *buff
,
143 int bufflen
, struct scsi_sense_hdr
*sshdr
, int flags
)
145 u8 cdb
[COMMAND_SIZE(MAINTENANCE_IN
)];
146 int req_flags
= REQ_FAILFAST_DEV
| REQ_FAILFAST_TRANSPORT
|
149 /* Prepare the command. */
150 memset(cdb
, 0x0, COMMAND_SIZE(MAINTENANCE_IN
));
151 cdb
[0] = MAINTENANCE_IN
;
152 if (!(flags
& ALUA_RTPG_EXT_HDR_UNSUPP
))
153 cdb
[1] = MI_REPORT_TARGET_PGS
| MI_EXT_HDR_PARAM_FMT
;
155 cdb
[1] = MI_REPORT_TARGET_PGS
;
156 put_unaligned_be32(bufflen
, &cdb
[6]);
158 return scsi_execute_req_flags(sdev
, cdb
, DMA_FROM_DEVICE
,
159 buff
, bufflen
, sshdr
,
160 ALUA_FAILOVER_TIMEOUT
* HZ
,
161 ALUA_FAILOVER_RETRIES
, NULL
, req_flags
);
165 * submit_stpg - Issue a SET TARGET PORT GROUP command
167 * Currently we're only setting the current target port group state
168 * to 'active/optimized' and let the array firmware figure out
169 * the states of the remaining groups.
171 static int submit_stpg(struct scsi_device
*sdev
, int group_id
,
172 struct scsi_sense_hdr
*sshdr
)
174 u8 cdb
[COMMAND_SIZE(MAINTENANCE_OUT
)];
175 unsigned char stpg_data
[8];
177 int req_flags
= REQ_FAILFAST_DEV
| REQ_FAILFAST_TRANSPORT
|
180 /* Prepare the data buffer */
181 memset(stpg_data
, 0, stpg_len
);
182 stpg_data
[4] = TPGS_STATE_OPTIMIZED
& 0x0f;
183 put_unaligned_be16(group_id
, &stpg_data
[6]);
185 /* Prepare the command. */
186 memset(cdb
, 0x0, COMMAND_SIZE(MAINTENANCE_OUT
));
187 cdb
[0] = MAINTENANCE_OUT
;
188 cdb
[1] = MO_SET_TARGET_PGS
;
189 put_unaligned_be32(stpg_len
, &cdb
[6]);
191 return scsi_execute_req_flags(sdev
, cdb
, DMA_TO_DEVICE
,
193 sshdr
, ALUA_FAILOVER_TIMEOUT
* HZ
,
194 ALUA_FAILOVER_RETRIES
, NULL
, req_flags
);
197 struct alua_port_group
*alua_find_get_pg(char *id_str
, size_t id_size
,
200 struct alua_port_group
*pg
;
202 list_for_each_entry(pg
, &port_group_list
, node
) {
203 if (pg
->group_id
!= group_id
)
205 if (pg
->device_id_len
!= id_size
)
207 if (strncmp(pg
->device_id_str
, id_str
, id_size
))
209 if (!kref_get_unless_zero(&pg
->kref
))
218 * alua_alloc_pg - Allocate a new port_group structure
220 * @h: alua device_handler data
221 * @group_id: port group id
223 * Allocate a new port_group structure for a given
226 struct alua_port_group
*alua_alloc_pg(struct scsi_device
*sdev
,
227 int group_id
, int tpgs
)
229 struct alua_port_group
*pg
, *tmp_pg
;
231 pg
= kzalloc(sizeof(struct alua_port_group
), GFP_KERNEL
);
233 return ERR_PTR(-ENOMEM
);
235 pg
->device_id_len
= scsi_vpd_lun_id(sdev
, pg
->device_id_str
,
236 sizeof(pg
->device_id_str
));
237 if (pg
->device_id_len
<= 0) {
239 * Internal error: TPGS supported but no device
240 * identifcation found. Disable ALUA support.
243 sdev_printk(KERN_INFO
, sdev
,
244 "%s: No device descriptors found\n",
246 return ERR_PTR(-ENXIO
);
248 pg
->group_id
= group_id
;
250 pg
->state
= TPGS_STATE_OPTIMIZED
;
252 pg
->flags
|= ALUA_OPTIMIZE_STPG
;
253 kref_init(&pg
->kref
);
254 INIT_DELAYED_WORK(&pg
->rtpg_work
, alua_rtpg_work
);
255 INIT_LIST_HEAD(&pg
->rtpg_list
);
256 INIT_LIST_HEAD(&pg
->node
);
257 spin_lock_init(&pg
->lock
);
259 spin_lock(&port_group_lock
);
260 tmp_pg
= alua_find_get_pg(pg
->device_id_str
, pg
->device_id_len
,
263 spin_unlock(&port_group_lock
);
268 list_add(&pg
->node
, &port_group_list
);
269 spin_unlock(&port_group_lock
);
275 * alua_check_tpgs - Evaluate TPGS setting
276 * @sdev: device to be checked
278 * Examine the TPGS setting of the sdev to find out if ALUA
281 static int alua_check_tpgs(struct scsi_device
*sdev
)
283 int tpgs
= TPGS_MODE_NONE
;
286 * ALUA support for non-disk devices is fraught with
287 * difficulties, so disable it for now.
289 if (sdev
->type
!= TYPE_DISK
) {
290 sdev_printk(KERN_INFO
, sdev
,
291 "%s: disable for non-disk devices\n",
296 tpgs
= scsi_device_tpgs(sdev
);
298 case TPGS_MODE_EXPLICIT
|TPGS_MODE_IMPLICIT
:
299 sdev_printk(KERN_INFO
, sdev
,
300 "%s: supports implicit and explicit TPGS\n",
303 case TPGS_MODE_EXPLICIT
:
304 sdev_printk(KERN_INFO
, sdev
, "%s: supports explicit TPGS\n",
307 case TPGS_MODE_IMPLICIT
:
308 sdev_printk(KERN_INFO
, sdev
, "%s: supports implicit TPGS\n",
312 sdev_printk(KERN_INFO
, sdev
, "%s: not supported\n",
316 sdev_printk(KERN_INFO
, sdev
,
317 "%s: unsupported TPGS setting %d\n",
319 tpgs
= TPGS_MODE_NONE
;
327 * alua_check_vpd - Evaluate INQUIRY vpd page 0x83
328 * @sdev: device to be checked
330 * Extract the relative target port and the target port group
331 * descriptor from the list of identificators.
333 static int alua_check_vpd(struct scsi_device
*sdev
, struct alua_dh_data
*h
,
336 int rel_port
= -1, group_id
;
337 struct alua_port_group
*pg
, *old_pg
= NULL
;
339 group_id
= scsi_vpd_tpg_id(sdev
, &rel_port
);
342 * Internal error; TPGS supported but required
343 * VPD identification descriptors not present.
344 * Disable ALUA support
346 sdev_printk(KERN_INFO
, sdev
,
347 "%s: No target port descriptors found\n",
349 return SCSI_DH_DEV_UNSUPP
;
352 pg
= alua_alloc_pg(sdev
, group_id
, tpgs
);
354 if (PTR_ERR(pg
) == -ENOMEM
)
355 return SCSI_DH_NOMEM
;
356 return SCSI_DH_DEV_UNSUPP
;
358 sdev_printk(KERN_INFO
, sdev
,
359 "%s: device %s port group %x rel port %x\n",
360 ALUA_DH_NAME
, pg
->device_id_str
, group_id
, rel_port
);
362 /* Check for existing port group references */
363 spin_lock(&h
->pg_lock
);
366 /* port group has changed. Update to new port group */
367 rcu_assign_pointer(h
->pg
, pg
);
369 alua_rtpg_queue(h
->pg
, sdev
, NULL
);
370 spin_unlock(&h
->pg_lock
);
373 kref_put(&old_pg
->kref
, release_port_group
);
378 static char print_alua_state(int state
)
381 case TPGS_STATE_OPTIMIZED
:
383 case TPGS_STATE_NONOPTIMIZED
:
385 case TPGS_STATE_STANDBY
:
387 case TPGS_STATE_UNAVAILABLE
:
389 case TPGS_STATE_LBA_DEPENDENT
:
391 case TPGS_STATE_OFFLINE
:
393 case TPGS_STATE_TRANSITIONING
:
400 static int alua_check_sense(struct scsi_device
*sdev
,
401 struct scsi_sense_hdr
*sense_hdr
)
403 switch (sense_hdr
->sense_key
) {
405 if (sense_hdr
->asc
== 0x04 && sense_hdr
->ascq
== 0x0a)
407 * LUN Not Accessible - ALUA state transition
409 return ADD_TO_MLQUEUE
;
412 if (sense_hdr
->asc
== 0x29 && sense_hdr
->ascq
== 0x00)
414 * Power On, Reset, or Bus Device Reset, just retry.
416 return ADD_TO_MLQUEUE
;
417 if (sense_hdr
->asc
== 0x29 && sense_hdr
->ascq
== 0x04)
419 * Device internal reset
421 return ADD_TO_MLQUEUE
;
422 if (sense_hdr
->asc
== 0x2a && sense_hdr
->ascq
== 0x01)
424 * Mode Parameters Changed
426 return ADD_TO_MLQUEUE
;
427 if (sense_hdr
->asc
== 0x2a && sense_hdr
->ascq
== 0x06)
431 return ADD_TO_MLQUEUE
;
432 if (sense_hdr
->asc
== 0x2a && sense_hdr
->ascq
== 0x07)
434 * Implicit ALUA state transition failed
436 return ADD_TO_MLQUEUE
;
437 if (sense_hdr
->asc
== 0x3f && sense_hdr
->ascq
== 0x03)
439 * Inquiry data has changed
441 return ADD_TO_MLQUEUE
;
442 if (sense_hdr
->asc
== 0x3f && sense_hdr
->ascq
== 0x0e)
444 * REPORTED_LUNS_DATA_HAS_CHANGED is reported
445 * when switching controllers on targets like
446 * Intel Multi-Flex. We can just retry.
448 return ADD_TO_MLQUEUE
;
452 return SCSI_RETURN_NOT_HANDLED
;
456 * alua_rtpg - Evaluate REPORT TARGET GROUP STATES
457 * @sdev: the device to be evaluated.
459 * Evaluate the Target Port Group State.
460 * Returns SCSI_DH_DEV_OFFLINED if the path is
461 * found to be unusable.
463 static int alua_rtpg(struct scsi_device
*sdev
, struct alua_port_group
*pg
)
465 struct scsi_sense_hdr sense_hdr
;
466 int len
, k
, off
, valid_states
= 0, bufflen
= ALUA_RTPG_SIZE
;
467 unsigned char *ucp
, *buff
;
468 unsigned err
, retval
;
469 unsigned int tpg_desc_tbl_off
;
470 unsigned char orig_transition_tmo
;
473 unsigned long transition_tmo
= ALUA_FAILOVER_TIMEOUT
* HZ
;
475 if (pg
->transition_tmo
)
476 transition_tmo
= pg
->transition_tmo
* HZ
;
478 pg
->expiry
= round_jiffies_up(jiffies
+ transition_tmo
);
481 buff
= kzalloc(bufflen
, GFP_KERNEL
);
483 return SCSI_DH_DEV_TEMP_BUSY
;
486 retval
= submit_rtpg(sdev
, buff
, bufflen
, &sense_hdr
, pg
->flags
);
489 if (!scsi_sense_valid(&sense_hdr
)) {
490 sdev_printk(KERN_INFO
, sdev
,
491 "%s: rtpg failed, result %d\n",
492 ALUA_DH_NAME
, retval
);
494 if (driver_byte(retval
) == DRIVER_ERROR
)
495 return SCSI_DH_DEV_TEMP_BUSY
;
500 * submit_rtpg() has failed on existing arrays
501 * when requesting extended header info, and
502 * the array doesn't support extended headers,
503 * even though it shouldn't according to T10.
504 * The retry without rtpg_ext_hdr_req set
507 if (!(pg
->flags
& ALUA_RTPG_EXT_HDR_UNSUPP
) &&
508 sense_hdr
.sense_key
== ILLEGAL_REQUEST
&&
509 sense_hdr
.asc
== 0x24 && sense_hdr
.ascq
== 0) {
510 pg
->flags
|= ALUA_RTPG_EXT_HDR_UNSUPP
;
514 * Retry on ALUA state transition or if any
515 * UNIT ATTENTION occurred.
517 if (sense_hdr
.sense_key
== NOT_READY
&&
518 sense_hdr
.asc
== 0x04 && sense_hdr
.ascq
== 0x0a)
520 else if (sense_hdr
.sense_key
== UNIT_ATTENTION
)
522 if (err
== SCSI_DH_RETRY
&&
523 pg
->expiry
!= 0 && time_before(jiffies
, pg
->expiry
)) {
524 sdev_printk(KERN_ERR
, sdev
, "%s: rtpg retry\n",
526 scsi_print_sense_hdr(sdev
, ALUA_DH_NAME
, &sense_hdr
);
529 sdev_printk(KERN_ERR
, sdev
, "%s: rtpg failed\n",
531 scsi_print_sense_hdr(sdev
, ALUA_DH_NAME
, &sense_hdr
);
537 len
= get_unaligned_be32(&buff
[0]) + 4;
540 /* Resubmit with the correct length */
543 buff
= kmalloc(bufflen
, GFP_KERNEL
);
545 sdev_printk(KERN_WARNING
, sdev
,
546 "%s: kmalloc buffer failed\n",__func__
);
547 /* Temporary failure, bypass */
549 return SCSI_DH_DEV_TEMP_BUSY
;
554 orig_transition_tmo
= pg
->transition_tmo
;
555 if ((buff
[4] & RTPG_FMT_MASK
) == RTPG_FMT_EXT_HDR
&& buff
[5] != 0)
556 pg
->transition_tmo
= buff
[5];
558 pg
->transition_tmo
= ALUA_FAILOVER_TIMEOUT
;
560 if (orig_transition_tmo
!= pg
->transition_tmo
) {
561 sdev_printk(KERN_INFO
, sdev
,
562 "%s: transition timeout set to %d seconds\n",
563 ALUA_DH_NAME
, pg
->transition_tmo
);
564 pg
->expiry
= jiffies
+ pg
->transition_tmo
* HZ
;
567 if ((buff
[4] & RTPG_FMT_MASK
) == RTPG_FMT_EXT_HDR
)
568 tpg_desc_tbl_off
= 8;
570 tpg_desc_tbl_off
= 4;
572 for (k
= tpg_desc_tbl_off
, ucp
= buff
+ tpg_desc_tbl_off
;
574 k
+= off
, ucp
+= off
) {
576 if (pg
->group_id
== get_unaligned_be16(&ucp
[2])) {
577 pg
->state
= ucp
[0] & 0x0f;
578 pg
->pref
= ucp
[0] >> 7;
579 valid_states
= ucp
[1];
581 off
= 8 + (ucp
[7] * 4);
584 sdev_printk(KERN_INFO
, sdev
,
585 "%s: port group %02x state %c %s supports %c%c%c%c%c%c%c\n",
586 ALUA_DH_NAME
, pg
->group_id
, print_alua_state(pg
->state
),
587 pg
->pref
? "preferred" : "non-preferred",
588 valid_states
&TPGS_SUPPORT_TRANSITION
?'T':'t',
589 valid_states
&TPGS_SUPPORT_OFFLINE
?'O':'o',
590 valid_states
&TPGS_SUPPORT_LBA_DEPENDENT
?'L':'l',
591 valid_states
&TPGS_SUPPORT_UNAVAILABLE
?'U':'u',
592 valid_states
&TPGS_SUPPORT_STANDBY
?'S':'s',
593 valid_states
&TPGS_SUPPORT_NONOPTIMIZED
?'N':'n',
594 valid_states
&TPGS_SUPPORT_OPTIMIZED
?'A':'a');
597 case TPGS_STATE_TRANSITIONING
:
598 if (time_before(jiffies
, pg
->expiry
)) {
599 /* State transition, retry */
603 /* Transitioning time exceeded, set port to standby */
605 pg
->state
= TPGS_STATE_STANDBY
;
609 case TPGS_STATE_OFFLINE
:
611 err
= SCSI_DH_DEV_OFFLINED
;
615 /* Useable path if active */
625 * alua_stpg - Issue a SET TARGET PORT GROUP command
627 * Issue a SET TARGET PORT GROUP command and evaluate the
628 * response. Returns SCSI_DH_RETRY per default to trigger
629 * a re-evaluation of the target group state or SCSI_DH_OK
630 * if no further action needs to be taken.
632 static unsigned alua_stpg(struct scsi_device
*sdev
, struct alua_port_group
*pg
)
635 struct scsi_sense_hdr sense_hdr
;
637 if (!(pg
->tpgs
& TPGS_MODE_EXPLICIT
)) {
638 /* Only implicit ALUA supported, retry */
639 return SCSI_DH_RETRY
;
642 case TPGS_STATE_OPTIMIZED
:
644 case TPGS_STATE_NONOPTIMIZED
:
645 if ((pg
->flags
& ALUA_OPTIMIZE_STPG
) &&
647 (pg
->tpgs
& TPGS_MODE_IMPLICIT
))
650 case TPGS_STATE_STANDBY
:
651 case TPGS_STATE_UNAVAILABLE
:
653 case TPGS_STATE_OFFLINE
:
655 case TPGS_STATE_TRANSITIONING
:
658 sdev_printk(KERN_INFO
, sdev
,
659 "%s: stpg failed, unhandled TPGS state %d",
660 ALUA_DH_NAME
, pg
->state
);
661 return SCSI_DH_NOSYS
;
663 retval
= submit_stpg(sdev
, pg
->group_id
, &sense_hdr
);
666 if (!scsi_sense_valid(&sense_hdr
)) {
667 sdev_printk(KERN_INFO
, sdev
,
668 "%s: stpg failed, result %d",
669 ALUA_DH_NAME
, retval
);
670 if (driver_byte(retval
) == DRIVER_ERROR
)
671 return SCSI_DH_DEV_TEMP_BUSY
;
673 sdev_printk(KERN_INFO
, sdev
, "%s: stpg failed\n",
675 scsi_print_sense_hdr(sdev
, ALUA_DH_NAME
, &sense_hdr
);
679 return SCSI_DH_RETRY
;
682 static void alua_rtpg_work(struct work_struct
*work
)
684 struct alua_port_group
*pg
=
685 container_of(work
, struct alua_port_group
, rtpg_work
.work
);
686 struct scsi_device
*sdev
;
687 LIST_HEAD(qdata_list
);
688 int err
= SCSI_DH_OK
;
689 struct alua_queue_data
*qdata
, *tmp
;
691 struct workqueue_struct
*alua_wq
= kaluad_wq
;
693 spin_lock_irqsave(&pg
->lock
, flags
);
694 sdev
= pg
->rtpg_sdev
;
696 WARN_ON(pg
->flags
& ALUA_PG_RUN_RTPG
);
697 WARN_ON(pg
->flags
& ALUA_PG_RUN_STPG
);
698 spin_unlock_irqrestore(&pg
->lock
, flags
);
701 if (pg
->flags
& ALUA_SYNC_STPG
)
702 alua_wq
= kaluad_sync_wq
;
703 pg
->flags
|= ALUA_PG_RUNNING
;
704 if (pg
->flags
& ALUA_PG_RUN_RTPG
) {
705 pg
->flags
&= ~ALUA_PG_RUN_RTPG
;
706 spin_unlock_irqrestore(&pg
->lock
, flags
);
707 err
= alua_rtpg(sdev
, pg
);
708 spin_lock_irqsave(&pg
->lock
, flags
);
709 if (err
== SCSI_DH_RETRY
) {
710 pg
->flags
&= ~ALUA_PG_RUNNING
;
711 pg
->flags
|= ALUA_PG_RUN_RTPG
;
712 spin_unlock_irqrestore(&pg
->lock
, flags
);
713 queue_delayed_work(alua_wq
, &pg
->rtpg_work
,
717 if (err
!= SCSI_DH_OK
)
718 pg
->flags
&= ~ALUA_PG_RUN_STPG
;
720 if (pg
->flags
& ALUA_PG_RUN_STPG
) {
721 pg
->flags
&= ~ALUA_PG_RUN_STPG
;
722 spin_unlock_irqrestore(&pg
->lock
, flags
);
723 err
= alua_stpg(sdev
, pg
);
724 spin_lock_irqsave(&pg
->lock
, flags
);
725 if (err
== SCSI_DH_RETRY
) {
726 pg
->flags
|= ALUA_PG_RUN_RTPG
;
728 pg
->flags
&= ~ALUA_PG_RUNNING
;
729 spin_unlock_irqrestore(&pg
->lock
, flags
);
730 queue_delayed_work(alua_wq
, &pg
->rtpg_work
,
736 list_splice_init(&pg
->rtpg_list
, &qdata_list
);
737 pg
->rtpg_sdev
= NULL
;
738 spin_unlock_irqrestore(&pg
->lock
, flags
);
740 list_for_each_entry_safe(qdata
, tmp
, &qdata_list
, entry
) {
741 list_del(&qdata
->entry
);
742 if (qdata
->callback_fn
)
743 qdata
->callback_fn(qdata
->callback_data
, err
);
746 spin_lock_irqsave(&pg
->lock
, flags
);
747 pg
->flags
&= ~ALUA_PG_RUNNING
;
748 spin_unlock_irqrestore(&pg
->lock
, flags
);
749 scsi_device_put(sdev
);
750 kref_put(&pg
->kref
, release_port_group
);
753 static void alua_rtpg_queue(struct alua_port_group
*pg
,
754 struct scsi_device
*sdev
,
755 struct alua_queue_data
*qdata
)
759 struct workqueue_struct
*alua_wq
= kaluad_wq
;
764 spin_lock_irqsave(&pg
->lock
, flags
);
766 list_add_tail(&qdata
->entry
, &pg
->rtpg_list
);
767 pg
->flags
|= ALUA_PG_RUN_STPG
;
769 if (pg
->rtpg_sdev
== NULL
) {
771 pg
->flags
|= ALUA_PG_RUN_RTPG
;
773 pg
->rtpg_sdev
= sdev
;
774 scsi_device_get(sdev
);
777 if (pg
->flags
& ALUA_SYNC_STPG
)
778 alua_wq
= kaluad_sync_wq
;
779 spin_unlock_irqrestore(&pg
->lock
, flags
);
782 !queue_delayed_work(alua_wq
, &pg
->rtpg_work
,
783 msecs_to_jiffies(ALUA_RTPG_DELAY_MSECS
))) {
784 scsi_device_put(sdev
);
785 kref_put(&pg
->kref
, release_port_group
);
790 * alua_initialize - Initialize ALUA state
791 * @sdev: the device to be initialized
793 * For the prep_fn to work correctly we have
794 * to initialize the ALUA state for the device.
796 static int alua_initialize(struct scsi_device
*sdev
, struct alua_dh_data
*h
)
798 int err
= SCSI_DH_DEV_UNSUPP
, tpgs
;
800 mutex_lock(&h
->init_mutex
);
801 tpgs
= alua_check_tpgs(sdev
);
802 if (tpgs
!= TPGS_MODE_NONE
)
803 err
= alua_check_vpd(sdev
, h
, tpgs
);
805 mutex_unlock(&h
->init_mutex
);
809 * alua_set_params - set/unset the optimize flag
810 * @sdev: device on the path to be activated
811 * params - parameters in the following format
812 * "no_of_params\0param1\0param2\0param3\0...\0"
813 * For example, to set the flag pass the following parameters
814 * from multipath.conf
815 * hardware_handler "2 alua 1"
817 static int alua_set_params(struct scsi_device
*sdev
, const char *params
)
819 struct alua_dh_data
*h
= sdev
->handler_data
;
820 struct alua_port_group __rcu
*pg
= NULL
;
821 unsigned int optimize
= 0, argc
;
822 const char *p
= params
;
823 int result
= SCSI_DH_OK
;
826 if ((sscanf(params
, "%u", &argc
) != 1) || (argc
!= 1))
831 if ((sscanf(p
, "%u", &optimize
) != 1) || (optimize
> 1))
835 pg
= rcu_dereference(h
->pg
);
840 spin_lock_irqsave(&pg
->lock
, flags
);
842 pg
->flags
|= ALUA_OPTIMIZE_STPG
;
844 pg
->flags
&= ~ALUA_OPTIMIZE_STPG
;
845 spin_unlock_irqrestore(&pg
->lock
, flags
);
852 * alua_activate - activate a path
853 * @sdev: device on the path to be activated
855 * We're currently switching the port group to be activated only and
856 * let the array figure out the rest.
857 * There may be other arrays which require us to switch all port groups
858 * based on a certain policy. But until we actually encounter them it
861 static int alua_activate(struct scsi_device
*sdev
,
862 activate_complete fn
, void *data
)
864 struct alua_dh_data
*h
= sdev
->handler_data
;
865 int err
= SCSI_DH_OK
;
866 struct alua_queue_data
*qdata
;
867 struct alua_port_group __rcu
*pg
;
869 qdata
= kzalloc(sizeof(*qdata
), GFP_KERNEL
);
871 err
= SCSI_DH_RES_TEMP_UNAVAIL
;
874 qdata
->callback_fn
= fn
;
875 qdata
->callback_data
= data
;
877 mutex_lock(&h
->init_mutex
);
879 pg
= rcu_dereference(h
->pg
);
880 if (!pg
|| !kref_get_unless_zero(&pg
->kref
)) {
884 mutex_unlock(&h
->init_mutex
);
889 mutex_unlock(&h
->init_mutex
);
891 alua_rtpg_queue(pg
, sdev
, qdata
);
892 kref_put(&pg
->kref
, release_port_group
);
900 * alua_prep_fn - request callback
902 * Fail I/O to all paths not in state
903 * active/optimized or active/non-optimized.
905 static int alua_prep_fn(struct scsi_device
*sdev
, struct request
*req
)
907 struct alua_dh_data
*h
= sdev
->handler_data
;
908 struct alua_port_group __rcu
*pg
;
909 int state
= TPGS_STATE_OPTIMIZED
;
910 int ret
= BLKPREP_OK
;
913 pg
= rcu_dereference(h
->pg
);
917 if (state
== TPGS_STATE_TRANSITIONING
)
919 else if (state
!= TPGS_STATE_OPTIMIZED
&&
920 state
!= TPGS_STATE_NONOPTIMIZED
&&
921 state
!= TPGS_STATE_LBA_DEPENDENT
) {
923 req
->cmd_flags
|= REQ_QUIET
;
930 * alua_bus_attach - Attach device handler
931 * @sdev: device to be attached to
933 static int alua_bus_attach(struct scsi_device
*sdev
)
935 struct alua_dh_data
*h
;
936 int err
, ret
= -EINVAL
;
938 h
= kzalloc(sizeof(*h
) , GFP_KERNEL
);
941 spin_lock_init(&h
->pg_lock
);
942 rcu_assign_pointer(h
->pg
, NULL
);
943 h
->init_error
= SCSI_DH_OK
;
946 mutex_init(&h
->init_mutex
);
947 err
= alua_initialize(sdev
, h
);
948 if (err
== SCSI_DH_NOMEM
)
950 if (err
!= SCSI_DH_OK
&& err
!= SCSI_DH_DEV_OFFLINED
)
953 sdev
->handler_data
= h
;
961 * alua_bus_detach - Detach device handler
962 * @sdev: device to be detached from
964 static void alua_bus_detach(struct scsi_device
*sdev
)
966 struct alua_dh_data
*h
= sdev
->handler_data
;
967 struct alua_port_group
*pg
;
969 spin_lock(&h
->pg_lock
);
971 rcu_assign_pointer(h
->pg
, NULL
);
973 spin_unlock(&h
->pg_lock
);
975 kref_put(&pg
->kref
, release_port_group
);
977 sdev
->handler_data
= NULL
;
981 static struct scsi_device_handler alua_dh
= {
982 .name
= ALUA_DH_NAME
,
983 .module
= THIS_MODULE
,
984 .attach
= alua_bus_attach
,
985 .detach
= alua_bus_detach
,
986 .prep_fn
= alua_prep_fn
,
987 .check_sense
= alua_check_sense
,
988 .activate
= alua_activate
,
989 .set_params
= alua_set_params
,
992 static int __init
alua_init(void)
996 kaluad_wq
= alloc_workqueue("kaluad", WQ_MEM_RECLAIM
, 0);
998 /* Temporary failure, bypass */
999 return SCSI_DH_DEV_TEMP_BUSY
;
1001 kaluad_sync_wq
= create_workqueue("kaluad_sync");
1002 if (!kaluad_sync_wq
) {
1003 destroy_workqueue(kaluad_wq
);
1004 return SCSI_DH_DEV_TEMP_BUSY
;
1006 r
= scsi_register_device_handler(&alua_dh
);
1008 printk(KERN_ERR
"%s: Failed to register scsi device handler",
1010 destroy_workqueue(kaluad_sync_wq
);
1011 destroy_workqueue(kaluad_wq
);
1016 static void __exit
alua_exit(void)
1018 scsi_unregister_device_handler(&alua_dh
);
1019 destroy_workqueue(kaluad_sync_wq
);
1020 destroy_workqueue(kaluad_wq
);
1023 module_init(alua_init
);
1024 module_exit(alua_exit
);
1026 MODULE_DESCRIPTION("DM Multipath ALUA support");
1027 MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
1028 MODULE_LICENSE("GPL");
1029 MODULE_VERSION(ALUA_DH_VER
);