1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Generic SCSI-3 ALUA SCSI Device Handler
5 * Copyright (C) 2007-2010 Hannes Reinecke, SUSE Linux Products GmbH.
8 #include <linux/slab.h>
9 #include <linux/delay.h>
10 #include <linux/module.h>
11 #include <asm/unaligned.h>
12 #include <scsi/scsi.h>
13 #include <scsi/scsi_proto.h>
14 #include <scsi/scsi_dbg.h>
15 #include <scsi/scsi_eh.h>
16 #include <scsi/scsi_dh.h>
18 #define ALUA_DH_NAME "alua"
19 #define ALUA_DH_VER "2.0"
21 #define TPGS_SUPPORT_NONE 0x00
22 #define TPGS_SUPPORT_OPTIMIZED 0x01
23 #define TPGS_SUPPORT_NONOPTIMIZED 0x02
24 #define TPGS_SUPPORT_STANDBY 0x04
25 #define TPGS_SUPPORT_UNAVAILABLE 0x08
26 #define TPGS_SUPPORT_LBA_DEPENDENT 0x10
27 #define TPGS_SUPPORT_OFFLINE 0x40
28 #define TPGS_SUPPORT_TRANSITION 0x80
29 #define TPGS_SUPPORT_ALL 0xdf
31 #define RTPG_FMT_MASK 0x70
32 #define RTPG_FMT_EXT_HDR 0x10
34 #define TPGS_MODE_UNINITIALIZED -1
35 #define TPGS_MODE_NONE 0x0
36 #define TPGS_MODE_IMPLICIT 0x1
37 #define TPGS_MODE_EXPLICIT 0x2
39 #define ALUA_RTPG_SIZE 128
40 #define ALUA_FAILOVER_TIMEOUT 60
41 #define ALUA_FAILOVER_RETRIES 5
42 #define ALUA_RTPG_DELAY_MSECS 5
43 #define ALUA_RTPG_RETRY_DELAY 2
45 /* device handler flags */
46 #define ALUA_OPTIMIZE_STPG 0x01
47 #define ALUA_RTPG_EXT_HDR_UNSUPP 0x02
48 /* State machine flags */
49 #define ALUA_PG_RUN_RTPG 0x10
50 #define ALUA_PG_RUN_STPG 0x20
51 #define ALUA_PG_RUNNING 0x40
53 static uint optimize_stpg
;
54 module_param(optimize_stpg
, uint
, S_IRUGO
|S_IWUSR
);
55 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.");
57 static LIST_HEAD(port_group_list
);
58 static DEFINE_SPINLOCK(port_group_lock
);
59 static struct workqueue_struct
*kaluad_wq
;
61 struct alua_port_group
{
64 struct list_head node
;
65 struct list_head dh_list
;
66 unsigned char device_id_str
[256];
73 unsigned flags
; /* used for optimizing STPG */
74 unsigned char transition_tmo
;
76 unsigned long interval
;
77 struct delayed_work rtpg_work
;
79 struct list_head rtpg_list
;
80 struct scsi_device
*rtpg_sdev
;
84 struct list_head node
;
85 struct alua_port_group __rcu
*pg
;
88 struct scsi_device
*sdev
;
90 struct mutex init_mutex
;
93 struct alua_queue_data
{
94 struct list_head entry
;
95 activate_complete callback_fn
;
99 #define ALUA_POLICY_SWITCH_CURRENT 0
100 #define ALUA_POLICY_SWITCH_ALL 1
102 static void alua_rtpg_work(struct work_struct
*work
);
103 static bool alua_rtpg_queue(struct alua_port_group
*pg
,
104 struct scsi_device
*sdev
,
105 struct alua_queue_data
*qdata
, bool force
);
106 static void alua_check(struct scsi_device
*sdev
, bool force
);
108 static void release_port_group(struct kref
*kref
)
110 struct alua_port_group
*pg
;
112 pg
= container_of(kref
, struct alua_port_group
, kref
);
114 flush_delayed_work(&pg
->rtpg_work
);
115 spin_lock(&port_group_lock
);
117 spin_unlock(&port_group_lock
);
122 * submit_rtpg - Issue a REPORT TARGET GROUP STATES command
123 * @sdev: sdev the command should be sent to
125 static int submit_rtpg(struct scsi_device
*sdev
, unsigned char *buff
,
126 int bufflen
, struct scsi_sense_hdr
*sshdr
, int flags
)
128 u8 cdb
[MAX_COMMAND_SIZE
];
129 int req_flags
= REQ_FAILFAST_DEV
| REQ_FAILFAST_TRANSPORT
|
132 /* Prepare the command. */
133 memset(cdb
, 0x0, MAX_COMMAND_SIZE
);
134 cdb
[0] = MAINTENANCE_IN
;
135 if (!(flags
& ALUA_RTPG_EXT_HDR_UNSUPP
))
136 cdb
[1] = MI_REPORT_TARGET_PGS
| MI_EXT_HDR_PARAM_FMT
;
138 cdb
[1] = MI_REPORT_TARGET_PGS
;
139 put_unaligned_be32(bufflen
, &cdb
[6]);
141 return scsi_execute(sdev
, cdb
, DMA_FROM_DEVICE
, buff
, bufflen
, NULL
,
142 sshdr
, ALUA_FAILOVER_TIMEOUT
* HZ
,
143 ALUA_FAILOVER_RETRIES
, req_flags
, 0, NULL
);
147 * submit_stpg - Issue a SET TARGET PORT GROUP command
149 * Currently we're only setting the current target port group state
150 * to 'active/optimized' and let the array firmware figure out
151 * the states of the remaining groups.
153 static int submit_stpg(struct scsi_device
*sdev
, int group_id
,
154 struct scsi_sense_hdr
*sshdr
)
156 u8 cdb
[MAX_COMMAND_SIZE
];
157 unsigned char stpg_data
[8];
159 int req_flags
= REQ_FAILFAST_DEV
| REQ_FAILFAST_TRANSPORT
|
162 /* Prepare the data buffer */
163 memset(stpg_data
, 0, stpg_len
);
164 stpg_data
[4] = SCSI_ACCESS_STATE_OPTIMAL
;
165 put_unaligned_be16(group_id
, &stpg_data
[6]);
167 /* Prepare the command. */
168 memset(cdb
, 0x0, MAX_COMMAND_SIZE
);
169 cdb
[0] = MAINTENANCE_OUT
;
170 cdb
[1] = MO_SET_TARGET_PGS
;
171 put_unaligned_be32(stpg_len
, &cdb
[6]);
173 return scsi_execute(sdev
, cdb
, DMA_TO_DEVICE
, stpg_data
, stpg_len
, NULL
,
174 sshdr
, ALUA_FAILOVER_TIMEOUT
* HZ
,
175 ALUA_FAILOVER_RETRIES
, req_flags
, 0, NULL
);
178 static struct alua_port_group
*alua_find_get_pg(char *id_str
, size_t id_size
,
181 struct alua_port_group
*pg
;
183 if (!id_str
|| !id_size
|| !strlen(id_str
))
186 list_for_each_entry(pg
, &port_group_list
, node
) {
187 if (pg
->group_id
!= group_id
)
189 if (!pg
->device_id_len
|| pg
->device_id_len
!= id_size
)
191 if (strncmp(pg
->device_id_str
, id_str
, id_size
))
193 if (!kref_get_unless_zero(&pg
->kref
))
202 * alua_alloc_pg - Allocate a new port_group structure
204 * @group_id: port group id
205 * @tpgs: target port group settings
207 * Allocate a new port_group structure for a given
210 static struct alua_port_group
*alua_alloc_pg(struct scsi_device
*sdev
,
211 int group_id
, int tpgs
)
213 struct alua_port_group
*pg
, *tmp_pg
;
215 pg
= kzalloc(sizeof(struct alua_port_group
), GFP_KERNEL
);
217 return ERR_PTR(-ENOMEM
);
219 pg
->device_id_len
= scsi_vpd_lun_id(sdev
, pg
->device_id_str
,
220 sizeof(pg
->device_id_str
));
221 if (pg
->device_id_len
<= 0) {
223 * TPGS supported but no device identification found.
224 * Generate private device identification.
226 sdev_printk(KERN_INFO
, sdev
,
227 "%s: No device descriptors found\n",
229 pg
->device_id_str
[0] = '\0';
230 pg
->device_id_len
= 0;
232 pg
->group_id
= group_id
;
234 pg
->state
= SCSI_ACCESS_STATE_OPTIMAL
;
235 pg
->valid_states
= TPGS_SUPPORT_ALL
;
237 pg
->flags
|= ALUA_OPTIMIZE_STPG
;
238 kref_init(&pg
->kref
);
239 INIT_DELAYED_WORK(&pg
->rtpg_work
, alua_rtpg_work
);
240 INIT_LIST_HEAD(&pg
->rtpg_list
);
241 INIT_LIST_HEAD(&pg
->node
);
242 INIT_LIST_HEAD(&pg
->dh_list
);
243 spin_lock_init(&pg
->lock
);
245 spin_lock(&port_group_lock
);
246 tmp_pg
= alua_find_get_pg(pg
->device_id_str
, pg
->device_id_len
,
249 spin_unlock(&port_group_lock
);
254 list_add(&pg
->node
, &port_group_list
);
255 spin_unlock(&port_group_lock
);
261 * alua_check_tpgs - Evaluate TPGS setting
262 * @sdev: device to be checked
264 * Examine the TPGS setting of the sdev to find out if ALUA
267 static int alua_check_tpgs(struct scsi_device
*sdev
)
269 int tpgs
= TPGS_MODE_NONE
;
272 * ALUA support for non-disk devices is fraught with
273 * difficulties, so disable it for now.
275 if (sdev
->type
!= TYPE_DISK
) {
276 sdev_printk(KERN_INFO
, sdev
,
277 "%s: disable for non-disk devices\n",
282 tpgs
= scsi_device_tpgs(sdev
);
284 case TPGS_MODE_EXPLICIT
|TPGS_MODE_IMPLICIT
:
285 sdev_printk(KERN_INFO
, sdev
,
286 "%s: supports implicit and explicit TPGS\n",
289 case TPGS_MODE_EXPLICIT
:
290 sdev_printk(KERN_INFO
, sdev
, "%s: supports explicit TPGS\n",
293 case TPGS_MODE_IMPLICIT
:
294 sdev_printk(KERN_INFO
, sdev
, "%s: supports implicit TPGS\n",
298 sdev_printk(KERN_INFO
, sdev
, "%s: not supported\n",
302 sdev_printk(KERN_INFO
, sdev
,
303 "%s: unsupported TPGS setting %d\n",
305 tpgs
= TPGS_MODE_NONE
;
313 * alua_check_vpd - Evaluate INQUIRY vpd page 0x83
314 * @sdev: device to be checked
316 * Extract the relative target port and the target port group
317 * descriptor from the list of identificators.
319 static int alua_check_vpd(struct scsi_device
*sdev
, struct alua_dh_data
*h
,
322 int rel_port
= -1, group_id
;
323 struct alua_port_group
*pg
, *old_pg
= NULL
;
324 bool pg_updated
= false;
327 group_id
= scsi_vpd_tpg_id(sdev
, &rel_port
);
330 * Internal error; TPGS supported but required
331 * VPD identification descriptors not present.
332 * Disable ALUA support
334 sdev_printk(KERN_INFO
, sdev
,
335 "%s: No target port descriptors found\n",
337 return SCSI_DH_DEV_UNSUPP
;
340 pg
= alua_alloc_pg(sdev
, group_id
, tpgs
);
342 if (PTR_ERR(pg
) == -ENOMEM
)
343 return SCSI_DH_NOMEM
;
344 return SCSI_DH_DEV_UNSUPP
;
346 if (pg
->device_id_len
)
347 sdev_printk(KERN_INFO
, sdev
,
348 "%s: device %s port group %x rel port %x\n",
349 ALUA_DH_NAME
, pg
->device_id_str
,
352 sdev_printk(KERN_INFO
, sdev
,
353 "%s: port group %x rel port %x\n",
354 ALUA_DH_NAME
, group_id
, rel_port
);
356 /* Check for existing port group references */
357 spin_lock(&h
->pg_lock
);
358 old_pg
= rcu_dereference_protected(h
->pg
, lockdep_is_held(&h
->pg_lock
));
360 /* port group has changed. Update to new port group */
362 spin_lock_irqsave(&old_pg
->lock
, flags
);
363 list_del_rcu(&h
->node
);
364 spin_unlock_irqrestore(&old_pg
->lock
, flags
);
366 rcu_assign_pointer(h
->pg
, pg
);
370 spin_lock_irqsave(&pg
->lock
, flags
);
372 list_add_rcu(&h
->node
, &pg
->dh_list
);
373 spin_unlock_irqrestore(&pg
->lock
, flags
);
375 alua_rtpg_queue(rcu_dereference_protected(h
->pg
,
376 lockdep_is_held(&h
->pg_lock
)),
378 spin_unlock(&h
->pg_lock
);
381 kref_put(&old_pg
->kref
, release_port_group
);
386 static char print_alua_state(unsigned char state
)
389 case SCSI_ACCESS_STATE_OPTIMAL
:
391 case SCSI_ACCESS_STATE_ACTIVE
:
393 case SCSI_ACCESS_STATE_STANDBY
:
395 case SCSI_ACCESS_STATE_UNAVAILABLE
:
397 case SCSI_ACCESS_STATE_LBA
:
399 case SCSI_ACCESS_STATE_OFFLINE
:
401 case SCSI_ACCESS_STATE_TRANSITIONING
:
408 static int alua_check_sense(struct scsi_device
*sdev
,
409 struct scsi_sense_hdr
*sense_hdr
)
411 struct alua_dh_data
*h
= sdev
->handler_data
;
412 struct alua_port_group
*pg
;
414 switch (sense_hdr
->sense_key
) {
416 if (sense_hdr
->asc
== 0x04 && sense_hdr
->ascq
== 0x0a) {
418 * LUN Not Accessible - ALUA state transition
421 pg
= rcu_dereference(h
->pg
);
423 pg
->state
= SCSI_ACCESS_STATE_TRANSITIONING
;
425 alua_check(sdev
, false);
430 if (sense_hdr
->asc
== 0x29 && sense_hdr
->ascq
== 0x00) {
432 * Power On, Reset, or Bus Device Reset.
433 * Might have obscured a state transition,
434 * so schedule a recheck.
436 alua_check(sdev
, true);
437 return ADD_TO_MLQUEUE
;
439 if (sense_hdr
->asc
== 0x29 && sense_hdr
->ascq
== 0x04)
441 * Device internal reset
443 return ADD_TO_MLQUEUE
;
444 if (sense_hdr
->asc
== 0x2a && sense_hdr
->ascq
== 0x01)
446 * Mode Parameters Changed
448 return ADD_TO_MLQUEUE
;
449 if (sense_hdr
->asc
== 0x2a && sense_hdr
->ascq
== 0x06) {
453 alua_check(sdev
, true);
454 return ADD_TO_MLQUEUE
;
456 if (sense_hdr
->asc
== 0x2a && sense_hdr
->ascq
== 0x07) {
458 * Implicit ALUA state transition failed
460 alua_check(sdev
, true);
461 return ADD_TO_MLQUEUE
;
463 if (sense_hdr
->asc
== 0x3f && sense_hdr
->ascq
== 0x03)
465 * Inquiry data has changed
467 return ADD_TO_MLQUEUE
;
468 if (sense_hdr
->asc
== 0x3f && sense_hdr
->ascq
== 0x0e)
470 * REPORTED_LUNS_DATA_HAS_CHANGED is reported
471 * when switching controllers on targets like
472 * Intel Multi-Flex. We can just retry.
474 return ADD_TO_MLQUEUE
;
478 return SCSI_RETURN_NOT_HANDLED
;
482 * alua_tur - Send a TEST UNIT READY
483 * @sdev: device to which the TEST UNIT READY command should be send
485 * Send a TEST UNIT READY to @sdev to figure out the device state
486 * Returns SCSI_DH_RETRY if the sense code is NOT READY/ALUA TRANSITIONING,
487 * SCSI_DH_OK if no error occurred, and SCSI_DH_IO otherwise.
489 static int alua_tur(struct scsi_device
*sdev
)
491 struct scsi_sense_hdr sense_hdr
;
494 retval
= scsi_test_unit_ready(sdev
, ALUA_FAILOVER_TIMEOUT
* HZ
,
495 ALUA_FAILOVER_RETRIES
, &sense_hdr
);
496 if (sense_hdr
.sense_key
== NOT_READY
&&
497 sense_hdr
.asc
== 0x04 && sense_hdr
.ascq
== 0x0a)
498 return SCSI_DH_RETRY
;
506 * alua_rtpg - Evaluate REPORT TARGET GROUP STATES
507 * @sdev: the device to be evaluated.
509 * Evaluate the Target Port Group State.
510 * Returns SCSI_DH_DEV_OFFLINED if the path is
511 * found to be unusable.
513 static int alua_rtpg(struct scsi_device
*sdev
, struct alua_port_group
*pg
)
515 struct scsi_sense_hdr sense_hdr
;
516 struct alua_port_group
*tmp_pg
;
517 int len
, k
, off
, bufflen
= ALUA_RTPG_SIZE
;
518 unsigned char *desc
, *buff
;
519 unsigned err
, retval
;
520 unsigned int tpg_desc_tbl_off
;
521 unsigned char orig_transition_tmo
;
523 bool transitioning_sense
= false;
526 unsigned long transition_tmo
= ALUA_FAILOVER_TIMEOUT
* HZ
;
528 if (pg
->transition_tmo
)
529 transition_tmo
= pg
->transition_tmo
* HZ
;
531 pg
->expiry
= round_jiffies_up(jiffies
+ transition_tmo
);
534 buff
= kzalloc(bufflen
, GFP_KERNEL
);
536 return SCSI_DH_DEV_TEMP_BUSY
;
540 retval
= submit_rtpg(sdev
, buff
, bufflen
, &sense_hdr
, pg
->flags
);
544 * Some (broken) implementations have a habit of returning
545 * an error during things like firmware update etc.
546 * But if the target only supports active/optimized there's
547 * not much we can do; it's not that we can switch paths
549 * So ignore any errors to avoid spurious failures during
552 if ((pg
->valid_states
& ~TPGS_SUPPORT_OPTIMIZED
) == 0) {
553 sdev_printk(KERN_INFO
, sdev
,
554 "%s: ignoring rtpg result %d\n",
555 ALUA_DH_NAME
, retval
);
559 if (!scsi_sense_valid(&sense_hdr
)) {
560 sdev_printk(KERN_INFO
, sdev
,
561 "%s: rtpg failed, result %d\n",
562 ALUA_DH_NAME
, retval
);
564 if (driver_byte(retval
) == DRIVER_ERROR
)
565 return SCSI_DH_DEV_TEMP_BUSY
;
570 * submit_rtpg() has failed on existing arrays
571 * when requesting extended header info, and
572 * the array doesn't support extended headers,
573 * even though it shouldn't according to T10.
574 * The retry without rtpg_ext_hdr_req set
577 if (!(pg
->flags
& ALUA_RTPG_EXT_HDR_UNSUPP
) &&
578 sense_hdr
.sense_key
== ILLEGAL_REQUEST
&&
579 sense_hdr
.asc
== 0x24 && sense_hdr
.ascq
== 0) {
580 pg
->flags
|= ALUA_RTPG_EXT_HDR_UNSUPP
;
584 * If the array returns with 'ALUA state transition'
585 * sense code here it cannot return RTPG data during
586 * transition. So set the state to 'transitioning' directly.
588 if (sense_hdr
.sense_key
== NOT_READY
&&
589 sense_hdr
.asc
== 0x04 && sense_hdr
.ascq
== 0x0a) {
590 transitioning_sense
= true;
594 * Retry on any other UNIT ATTENTION occurred.
596 if (sense_hdr
.sense_key
== UNIT_ATTENTION
)
598 if (err
== SCSI_DH_RETRY
&&
599 pg
->expiry
!= 0 && time_before(jiffies
, pg
->expiry
)) {
600 sdev_printk(KERN_ERR
, sdev
, "%s: rtpg retry\n",
602 scsi_print_sense_hdr(sdev
, ALUA_DH_NAME
, &sense_hdr
);
606 sdev_printk(KERN_ERR
, sdev
, "%s: rtpg failed\n",
608 scsi_print_sense_hdr(sdev
, ALUA_DH_NAME
, &sense_hdr
);
614 len
= get_unaligned_be32(&buff
[0]) + 4;
617 /* Resubmit with the correct length */
620 buff
= kmalloc(bufflen
, GFP_KERNEL
);
622 sdev_printk(KERN_WARNING
, sdev
,
623 "%s: kmalloc buffer failed\n",__func__
);
624 /* Temporary failure, bypass */
626 return SCSI_DH_DEV_TEMP_BUSY
;
631 orig_transition_tmo
= pg
->transition_tmo
;
632 if ((buff
[4] & RTPG_FMT_MASK
) == RTPG_FMT_EXT_HDR
&& buff
[5] != 0)
633 pg
->transition_tmo
= buff
[5];
635 pg
->transition_tmo
= ALUA_FAILOVER_TIMEOUT
;
637 if (orig_transition_tmo
!= pg
->transition_tmo
) {
638 sdev_printk(KERN_INFO
, sdev
,
639 "%s: transition timeout set to %d seconds\n",
640 ALUA_DH_NAME
, pg
->transition_tmo
);
641 pg
->expiry
= jiffies
+ pg
->transition_tmo
* HZ
;
644 if ((buff
[4] & RTPG_FMT_MASK
) == RTPG_FMT_EXT_HDR
)
645 tpg_desc_tbl_off
= 8;
647 tpg_desc_tbl_off
= 4;
649 for (k
= tpg_desc_tbl_off
, desc
= buff
+ tpg_desc_tbl_off
;
651 k
+= off
, desc
+= off
) {
652 u16 group_id
= get_unaligned_be16(&desc
[2]);
654 spin_lock_irqsave(&port_group_lock
, flags
);
655 tmp_pg
= alua_find_get_pg(pg
->device_id_str
, pg
->device_id_len
,
657 spin_unlock_irqrestore(&port_group_lock
, flags
);
659 if (spin_trylock_irqsave(&tmp_pg
->lock
, flags
)) {
660 if ((tmp_pg
== pg
) ||
661 !(tmp_pg
->flags
& ALUA_PG_RUNNING
)) {
662 struct alua_dh_data
*h
;
664 tmp_pg
->state
= desc
[0] & 0x0f;
665 tmp_pg
->pref
= desc
[0] >> 7;
667 list_for_each_entry_rcu(h
,
668 &tmp_pg
->dh_list
, node
) {
671 h
->sdev
->access_state
= desc
[0];
676 tmp_pg
->valid_states
= desc
[1];
677 spin_unlock_irqrestore(&tmp_pg
->lock
, flags
);
679 kref_put(&tmp_pg
->kref
, release_port_group
);
681 off
= 8 + (desc
[7] * 4);
685 spin_lock_irqsave(&pg
->lock
, flags
);
686 if (transitioning_sense
)
687 pg
->state
= SCSI_ACCESS_STATE_TRANSITIONING
;
689 sdev_printk(KERN_INFO
, sdev
,
690 "%s: port group %02x state %c %s supports %c%c%c%c%c%c%c\n",
691 ALUA_DH_NAME
, pg
->group_id
, print_alua_state(pg
->state
),
692 pg
->pref
? "preferred" : "non-preferred",
693 pg
->valid_states
&TPGS_SUPPORT_TRANSITION
?'T':'t',
694 pg
->valid_states
&TPGS_SUPPORT_OFFLINE
?'O':'o',
695 pg
->valid_states
&TPGS_SUPPORT_LBA_DEPENDENT
?'L':'l',
696 pg
->valid_states
&TPGS_SUPPORT_UNAVAILABLE
?'U':'u',
697 pg
->valid_states
&TPGS_SUPPORT_STANDBY
?'S':'s',
698 pg
->valid_states
&TPGS_SUPPORT_NONOPTIMIZED
?'N':'n',
699 pg
->valid_states
&TPGS_SUPPORT_OPTIMIZED
?'A':'a');
702 case SCSI_ACCESS_STATE_TRANSITIONING
:
703 if (time_before(jiffies
, pg
->expiry
)) {
704 /* State transition, retry */
705 pg
->interval
= ALUA_RTPG_RETRY_DELAY
;
708 struct alua_dh_data
*h
;
710 /* Transitioning time exceeded, set port to standby */
712 pg
->state
= SCSI_ACCESS_STATE_STANDBY
;
715 list_for_each_entry_rcu(h
, &pg
->dh_list
, node
) {
718 h
->sdev
->access_state
=
719 (pg
->state
& SCSI_ACCESS_STATE_MASK
);
721 h
->sdev
->access_state
|=
722 SCSI_ACCESS_STATE_PREFERRED
;
727 case SCSI_ACCESS_STATE_OFFLINE
:
729 err
= SCSI_DH_DEV_OFFLINED
;
733 /* Useable path if active */
738 spin_unlock_irqrestore(&pg
->lock
, flags
);
744 * alua_stpg - Issue a SET TARGET PORT GROUP command
746 * Issue a SET TARGET PORT GROUP command and evaluate the
747 * response. Returns SCSI_DH_RETRY per default to trigger
748 * a re-evaluation of the target group state or SCSI_DH_OK
749 * if no further action needs to be taken.
751 static unsigned alua_stpg(struct scsi_device
*sdev
, struct alua_port_group
*pg
)
754 struct scsi_sense_hdr sense_hdr
;
756 if (!(pg
->tpgs
& TPGS_MODE_EXPLICIT
)) {
757 /* Only implicit ALUA supported, retry */
758 return SCSI_DH_RETRY
;
761 case SCSI_ACCESS_STATE_OPTIMAL
:
763 case SCSI_ACCESS_STATE_ACTIVE
:
764 if ((pg
->flags
& ALUA_OPTIMIZE_STPG
) &&
766 (pg
->tpgs
& TPGS_MODE_IMPLICIT
))
769 case SCSI_ACCESS_STATE_STANDBY
:
770 case SCSI_ACCESS_STATE_UNAVAILABLE
:
772 case SCSI_ACCESS_STATE_OFFLINE
:
774 case SCSI_ACCESS_STATE_TRANSITIONING
:
777 sdev_printk(KERN_INFO
, sdev
,
778 "%s: stpg failed, unhandled TPGS state %d",
779 ALUA_DH_NAME
, pg
->state
);
780 return SCSI_DH_NOSYS
;
782 retval
= submit_stpg(sdev
, pg
->group_id
, &sense_hdr
);
785 if (!scsi_sense_valid(&sense_hdr
)) {
786 sdev_printk(KERN_INFO
, sdev
,
787 "%s: stpg failed, result %d",
788 ALUA_DH_NAME
, retval
);
789 if (driver_byte(retval
) == DRIVER_ERROR
)
790 return SCSI_DH_DEV_TEMP_BUSY
;
792 sdev_printk(KERN_INFO
, sdev
, "%s: stpg failed\n",
794 scsi_print_sense_hdr(sdev
, ALUA_DH_NAME
, &sense_hdr
);
798 return SCSI_DH_RETRY
;
801 static void alua_rtpg_work(struct work_struct
*work
)
803 struct alua_port_group
*pg
=
804 container_of(work
, struct alua_port_group
, rtpg_work
.work
);
805 struct scsi_device
*sdev
;
806 LIST_HEAD(qdata_list
);
807 int err
= SCSI_DH_OK
;
808 struct alua_queue_data
*qdata
, *tmp
;
811 spin_lock_irqsave(&pg
->lock
, flags
);
812 sdev
= pg
->rtpg_sdev
;
814 WARN_ON(pg
->flags
& ALUA_PG_RUN_RTPG
);
815 WARN_ON(pg
->flags
& ALUA_PG_RUN_STPG
);
816 spin_unlock_irqrestore(&pg
->lock
, flags
);
817 kref_put(&pg
->kref
, release_port_group
);
820 pg
->flags
|= ALUA_PG_RUNNING
;
821 if (pg
->flags
& ALUA_PG_RUN_RTPG
) {
822 int state
= pg
->state
;
824 pg
->flags
&= ~ALUA_PG_RUN_RTPG
;
825 spin_unlock_irqrestore(&pg
->lock
, flags
);
826 if (state
== SCSI_ACCESS_STATE_TRANSITIONING
) {
827 if (alua_tur(sdev
) == SCSI_DH_RETRY
) {
828 spin_lock_irqsave(&pg
->lock
, flags
);
829 pg
->flags
&= ~ALUA_PG_RUNNING
;
830 pg
->flags
|= ALUA_PG_RUN_RTPG
;
832 pg
->interval
= ALUA_RTPG_RETRY_DELAY
;
833 spin_unlock_irqrestore(&pg
->lock
, flags
);
834 queue_delayed_work(kaluad_wq
, &pg
->rtpg_work
,
838 /* Send RTPG on failure or if TUR indicates SUCCESS */
840 err
= alua_rtpg(sdev
, pg
);
841 spin_lock_irqsave(&pg
->lock
, flags
);
842 if (err
== SCSI_DH_RETRY
|| pg
->flags
& ALUA_PG_RUN_RTPG
) {
843 pg
->flags
&= ~ALUA_PG_RUNNING
;
844 if (!pg
->interval
&& !(pg
->flags
& ALUA_PG_RUN_RTPG
))
845 pg
->interval
= ALUA_RTPG_RETRY_DELAY
;
846 pg
->flags
|= ALUA_PG_RUN_RTPG
;
847 spin_unlock_irqrestore(&pg
->lock
, flags
);
848 queue_delayed_work(kaluad_wq
, &pg
->rtpg_work
,
852 if (err
!= SCSI_DH_OK
)
853 pg
->flags
&= ~ALUA_PG_RUN_STPG
;
855 if (pg
->flags
& ALUA_PG_RUN_STPG
) {
856 pg
->flags
&= ~ALUA_PG_RUN_STPG
;
857 spin_unlock_irqrestore(&pg
->lock
, flags
);
858 err
= alua_stpg(sdev
, pg
);
859 spin_lock_irqsave(&pg
->lock
, flags
);
860 if (err
== SCSI_DH_RETRY
|| pg
->flags
& ALUA_PG_RUN_RTPG
) {
861 pg
->flags
|= ALUA_PG_RUN_RTPG
;
863 pg
->flags
&= ~ALUA_PG_RUNNING
;
864 spin_unlock_irqrestore(&pg
->lock
, flags
);
865 queue_delayed_work(kaluad_wq
, &pg
->rtpg_work
,
871 list_splice_init(&pg
->rtpg_list
, &qdata_list
);
872 pg
->rtpg_sdev
= NULL
;
873 spin_unlock_irqrestore(&pg
->lock
, flags
);
875 list_for_each_entry_safe(qdata
, tmp
, &qdata_list
, entry
) {
876 list_del(&qdata
->entry
);
877 if (qdata
->callback_fn
)
878 qdata
->callback_fn(qdata
->callback_data
, err
);
881 spin_lock_irqsave(&pg
->lock
, flags
);
882 pg
->flags
&= ~ALUA_PG_RUNNING
;
883 spin_unlock_irqrestore(&pg
->lock
, flags
);
884 scsi_device_put(sdev
);
885 kref_put(&pg
->kref
, release_port_group
);
889 * alua_rtpg_queue() - cause RTPG to be submitted asynchronously
890 * @pg: ALUA port group associated with @sdev.
891 * @sdev: SCSI device for which to submit an RTPG.
892 * @qdata: Information about the callback to invoke after the RTPG.
893 * @force: Whether or not to submit an RTPG if a work item that will submit an
894 * RTPG already has been scheduled.
896 * Returns true if and only if alua_rtpg_work() will be called asynchronously.
897 * That function is responsible for calling @qdata->fn().
899 static bool alua_rtpg_queue(struct alua_port_group
*pg
,
900 struct scsi_device
*sdev
,
901 struct alua_queue_data
*qdata
, bool force
)
905 if (WARN_ON_ONCE(!pg
) || scsi_device_get(sdev
))
908 spin_lock_irqsave(&pg
->lock
, flags
);
910 list_add_tail(&qdata
->entry
, &pg
->rtpg_list
);
911 pg
->flags
|= ALUA_PG_RUN_STPG
;
914 if (pg
->rtpg_sdev
== NULL
) {
916 pg
->flags
|= ALUA_PG_RUN_RTPG
;
918 pg
->rtpg_sdev
= sdev
;
920 } else if (!(pg
->flags
& ALUA_PG_RUN_RTPG
) && force
) {
921 pg
->flags
|= ALUA_PG_RUN_RTPG
;
922 /* Do not queue if the worker is already running */
923 if (!(pg
->flags
& ALUA_PG_RUNNING
)) {
929 spin_unlock_irqrestore(&pg
->lock
, flags
);
932 if (queue_delayed_work(kaluad_wq
, &pg
->rtpg_work
,
933 msecs_to_jiffies(ALUA_RTPG_DELAY_MSECS
)))
936 kref_put(&pg
->kref
, release_port_group
);
939 scsi_device_put(sdev
);
945 * alua_initialize - Initialize ALUA state
946 * @sdev: the device to be initialized
948 * For the prep_fn to work correctly we have
949 * to initialize the ALUA state for the device.
951 static int alua_initialize(struct scsi_device
*sdev
, struct alua_dh_data
*h
)
953 int err
= SCSI_DH_DEV_UNSUPP
, tpgs
;
955 mutex_lock(&h
->init_mutex
);
956 tpgs
= alua_check_tpgs(sdev
);
957 if (tpgs
!= TPGS_MODE_NONE
)
958 err
= alua_check_vpd(sdev
, h
, tpgs
);
960 mutex_unlock(&h
->init_mutex
);
964 * alua_set_params - set/unset the optimize flag
965 * @sdev: device on the path to be activated
966 * params - parameters in the following format
967 * "no_of_params\0param1\0param2\0param3\0...\0"
968 * For example, to set the flag pass the following parameters
969 * from multipath.conf
970 * hardware_handler "2 alua 1"
972 static int alua_set_params(struct scsi_device
*sdev
, const char *params
)
974 struct alua_dh_data
*h
= sdev
->handler_data
;
975 struct alua_port_group
*pg
= NULL
;
976 unsigned int optimize
= 0, argc
;
977 const char *p
= params
;
978 int result
= SCSI_DH_OK
;
981 if ((sscanf(params
, "%u", &argc
) != 1) || (argc
!= 1))
986 if ((sscanf(p
, "%u", &optimize
) != 1) || (optimize
> 1))
990 pg
= rcu_dereference(h
->pg
);
995 spin_lock_irqsave(&pg
->lock
, flags
);
997 pg
->flags
|= ALUA_OPTIMIZE_STPG
;
999 pg
->flags
&= ~ALUA_OPTIMIZE_STPG
;
1000 spin_unlock_irqrestore(&pg
->lock
, flags
);
1007 * alua_activate - activate a path
1008 * @sdev: device on the path to be activated
1010 * We're currently switching the port group to be activated only and
1011 * let the array figure out the rest.
1012 * There may be other arrays which require us to switch all port groups
1013 * based on a certain policy. But until we actually encounter them it
1016 static int alua_activate(struct scsi_device
*sdev
,
1017 activate_complete fn
, void *data
)
1019 struct alua_dh_data
*h
= sdev
->handler_data
;
1020 int err
= SCSI_DH_OK
;
1021 struct alua_queue_data
*qdata
;
1022 struct alua_port_group
*pg
;
1024 qdata
= kzalloc(sizeof(*qdata
), GFP_KERNEL
);
1026 err
= SCSI_DH_RES_TEMP_UNAVAIL
;
1029 qdata
->callback_fn
= fn
;
1030 qdata
->callback_data
= data
;
1032 mutex_lock(&h
->init_mutex
);
1034 pg
= rcu_dereference(h
->pg
);
1035 if (!pg
|| !kref_get_unless_zero(&pg
->kref
)) {
1038 err
= h
->init_error
;
1039 mutex_unlock(&h
->init_mutex
);
1043 mutex_unlock(&h
->init_mutex
);
1045 if (alua_rtpg_queue(pg
, sdev
, qdata
, true))
1048 err
= SCSI_DH_DEV_OFFLINED
;
1049 kref_put(&pg
->kref
, release_port_group
);
1057 * alua_check - check path status
1058 * @sdev: device on the path to be checked
1060 * Check the device status
1062 static void alua_check(struct scsi_device
*sdev
, bool force
)
1064 struct alua_dh_data
*h
= sdev
->handler_data
;
1065 struct alua_port_group
*pg
;
1068 pg
= rcu_dereference(h
->pg
);
1069 if (!pg
|| !kref_get_unless_zero(&pg
->kref
)) {
1075 alua_rtpg_queue(pg
, sdev
, NULL
, force
);
1076 kref_put(&pg
->kref
, release_port_group
);
1080 * alua_prep_fn - request callback
1082 * Fail I/O to all paths not in state
1083 * active/optimized or active/non-optimized.
1085 static blk_status_t
alua_prep_fn(struct scsi_device
*sdev
, struct request
*req
)
1087 struct alua_dh_data
*h
= sdev
->handler_data
;
1088 struct alua_port_group
*pg
;
1089 unsigned char state
= SCSI_ACCESS_STATE_OPTIMAL
;
1092 pg
= rcu_dereference(h
->pg
);
1098 case SCSI_ACCESS_STATE_OPTIMAL
:
1099 case SCSI_ACCESS_STATE_ACTIVE
:
1100 case SCSI_ACCESS_STATE_LBA
:
1102 case SCSI_ACCESS_STATE_TRANSITIONING
:
1103 return BLK_STS_AGAIN
;
1105 req
->rq_flags
|= RQF_QUIET
;
1106 return BLK_STS_IOERR
;
1110 static void alua_rescan(struct scsi_device
*sdev
)
1112 struct alua_dh_data
*h
= sdev
->handler_data
;
1114 alua_initialize(sdev
, h
);
1118 * alua_bus_attach - Attach device handler
1119 * @sdev: device to be attached to
1121 static int alua_bus_attach(struct scsi_device
*sdev
)
1123 struct alua_dh_data
*h
;
1126 h
= kzalloc(sizeof(*h
) , GFP_KERNEL
);
1128 return SCSI_DH_NOMEM
;
1129 spin_lock_init(&h
->pg_lock
);
1130 rcu_assign_pointer(h
->pg
, NULL
);
1131 h
->init_error
= SCSI_DH_OK
;
1133 INIT_LIST_HEAD(&h
->node
);
1135 mutex_init(&h
->init_mutex
);
1136 err
= alua_initialize(sdev
, h
);
1137 if (err
!= SCSI_DH_OK
&& err
!= SCSI_DH_DEV_OFFLINED
)
1140 sdev
->handler_data
= h
;
1148 * alua_bus_detach - Detach device handler
1149 * @sdev: device to be detached from
1151 static void alua_bus_detach(struct scsi_device
*sdev
)
1153 struct alua_dh_data
*h
= sdev
->handler_data
;
1154 struct alua_port_group
*pg
;
1156 spin_lock(&h
->pg_lock
);
1157 pg
= rcu_dereference_protected(h
->pg
, lockdep_is_held(&h
->pg_lock
));
1158 rcu_assign_pointer(h
->pg
, NULL
);
1159 spin_unlock(&h
->pg_lock
);
1161 spin_lock_irq(&pg
->lock
);
1162 list_del_rcu(&h
->node
);
1163 spin_unlock_irq(&pg
->lock
);
1164 kref_put(&pg
->kref
, release_port_group
);
1166 sdev
->handler_data
= NULL
;
1171 static struct scsi_device_handler alua_dh
= {
1172 .name
= ALUA_DH_NAME
,
1173 .module
= THIS_MODULE
,
1174 .attach
= alua_bus_attach
,
1175 .detach
= alua_bus_detach
,
1176 .prep_fn
= alua_prep_fn
,
1177 .check_sense
= alua_check_sense
,
1178 .activate
= alua_activate
,
1179 .rescan
= alua_rescan
,
1180 .set_params
= alua_set_params
,
1183 static int __init
alua_init(void)
1187 kaluad_wq
= alloc_workqueue("kaluad", WQ_MEM_RECLAIM
, 0);
1191 r
= scsi_register_device_handler(&alua_dh
);
1193 printk(KERN_ERR
"%s: Failed to register scsi device handler",
1195 destroy_workqueue(kaluad_wq
);
1200 static void __exit
alua_exit(void)
1202 scsi_unregister_device_handler(&alua_dh
);
1203 destroy_workqueue(kaluad_wq
);
1206 module_init(alua_init
);
1207 module_exit(alua_exit
);
1209 MODULE_DESCRIPTION("DM Multipath ALUA support");
1210 MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
1211 MODULE_LICENSE("GPL");
1212 MODULE_VERSION(ALUA_DH_VER
);