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 <scsi/scsi.h>
26 #include <scsi/scsi_eh.h>
27 #include <scsi/scsi_dh.h>
29 #define ALUA_DH_NAME "alua"
30 #define ALUA_DH_VER "1.3"
32 #define TPGS_STATE_OPTIMIZED 0x0
33 #define TPGS_STATE_NONOPTIMIZED 0x1
34 #define TPGS_STATE_STANDBY 0x2
35 #define TPGS_STATE_UNAVAILABLE 0x3
36 #define TPGS_STATE_LBA_DEPENDENT 0x4
37 #define TPGS_STATE_OFFLINE 0xe
38 #define TPGS_STATE_TRANSITIONING 0xf
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
45 #define TPGS_SUPPORT_LBA_DEPENDENT 0x10
46 #define TPGS_SUPPORT_OFFLINE 0x40
47 #define TPGS_SUPPORT_TRANSITION 0x80
49 #define RTPG_FMT_MASK 0x70
50 #define RTPG_FMT_EXT_HDR 0x10
52 #define TPGS_MODE_UNINITIALIZED -1
53 #define TPGS_MODE_NONE 0x0
54 #define TPGS_MODE_IMPLICIT 0x1
55 #define TPGS_MODE_EXPLICIT 0x2
57 #define ALUA_INQUIRY_SIZE 36
58 #define ALUA_FAILOVER_TIMEOUT 60
59 #define ALUA_FAILOVER_RETRIES 5
61 /* flags passed from user level */
62 #define ALUA_OPTIMIZE_STPG 1
70 unsigned flags
; /* used for optimizing STPG */
71 unsigned char inq
[ALUA_INQUIRY_SIZE
];
74 unsigned char transition_tmo
;
75 unsigned char sense
[SCSI_SENSE_BUFFERSIZE
];
77 struct scsi_device
*sdev
;
78 activate_complete callback_fn
;
82 #define ALUA_POLICY_SWITCH_CURRENT 0
83 #define ALUA_POLICY_SWITCH_ALL 1
85 static char print_alua_state(int);
86 static int alua_check_sense(struct scsi_device
*, struct scsi_sense_hdr
*);
88 static inline struct alua_dh_data
*get_alua_data(struct scsi_device
*sdev
)
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
);
95 static int realloc_buffer(struct alua_dh_data
*h
, unsigned len
)
97 if (h
->buff
&& h
->buff
!= h
->inq
)
100 h
->buff
= kmalloc(len
, GFP_NOIO
);
103 h
->bufflen
= ALUA_INQUIRY_SIZE
;
110 static struct request
*get_alua_req(struct scsi_device
*sdev
,
111 void *buffer
, unsigned buflen
, int rw
)
114 struct request_queue
*q
= sdev
->request_queue
;
116 rq
= blk_get_request(q
, rw
, GFP_NOIO
);
119 sdev_printk(KERN_INFO
, sdev
,
120 "%s: blk_get_request failed\n", __func__
);
124 if (buflen
&& blk_rq_map_kern(q
, rq
, buffer
, buflen
, GFP_NOIO
)) {
126 sdev_printk(KERN_INFO
, sdev
,
127 "%s: blk_rq_map_kern failed\n", __func__
);
131 rq
->cmd_type
= REQ_TYPE_BLOCK_PC
;
132 rq
->cmd_flags
|= REQ_FAILFAST_DEV
| REQ_FAILFAST_TRANSPORT
|
134 rq
->retries
= ALUA_FAILOVER_RETRIES
;
135 rq
->timeout
= ALUA_FAILOVER_TIMEOUT
* HZ
;
141 * submit_vpd_inquiry - Issue an INQUIRY VPD page 0x83 command
142 * @sdev: sdev the command should be sent to
144 static int submit_vpd_inquiry(struct scsi_device
*sdev
, struct alua_dh_data
*h
)
147 int err
= SCSI_DH_RES_TEMP_UNAVAIL
;
149 rq
= get_alua_req(sdev
, h
->buff
, h
->bufflen
, READ
);
153 /* Prepare the command. */
154 rq
->cmd
[0] = INQUIRY
;
157 rq
->cmd
[4] = h
->bufflen
;
158 rq
->cmd_len
= COMMAND_SIZE(INQUIRY
);
160 rq
->sense
= h
->sense
;
161 memset(rq
->sense
, 0, SCSI_SENSE_BUFFERSIZE
);
162 rq
->sense_len
= h
->senselen
= 0;
164 err
= blk_execute_rq(rq
->q
, NULL
, rq
, 1);
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
;
178 * submit_rtpg - Issue a REPORT TARGET GROUP STATES command
179 * @sdev: sdev the command should be sent to
181 static unsigned submit_rtpg(struct scsi_device
*sdev
, struct alua_dh_data
*h
,
182 bool rtpg_ext_hdr_req
)
185 int err
= SCSI_DH_RES_TEMP_UNAVAIL
;
187 rq
= get_alua_req(sdev
, h
->buff
, h
->bufflen
, READ
);
191 /* Prepare the command. */
192 rq
->cmd
[0] = MAINTENANCE_IN
;
193 if (rtpg_ext_hdr_req
)
194 rq
->cmd
[1] = MI_REPORT_TARGET_PGS
| MI_EXT_HDR_PARAM_FMT
;
196 rq
->cmd
[1] = MI_REPORT_TARGET_PGS
;
197 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
);
203 rq
->sense
= h
->sense
;
204 memset(rq
->sense
, 0, SCSI_SENSE_BUFFERSIZE
);
205 rq
->sense_len
= h
->senselen
= 0;
207 err
= blk_execute_rq(rq
->q
, NULL
, rq
, 1);
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
;
221 * alua_stpg - Evaluate SET TARGET GROUP STATES
222 * @sdev: the device to be evaluated
223 * @state: the new target group state
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.
229 static void stpg_endio(struct request
*req
, int error
)
231 struct alua_dh_data
*h
= req
->end_io_data
;
232 struct scsi_sense_hdr sense_hdr
;
233 unsigned err
= SCSI_DH_OK
;
235 if (error
|| host_byte(req
->errors
) != DID_OK
||
236 msg_byte(req
->errors
) != COMMAND_COMPLETE
) {
241 if (h
->senselen
> 0) {
242 err
= scsi_normalize_sense(h
->sense
, SCSI_SENSE_BUFFERSIZE
,
248 err
= alua_check_sense(h
->sdev
, &sense_hdr
);
249 if (err
== ADD_TO_MLQUEUE
) {
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
);
259 if (err
== SCSI_DH_OK
) {
260 h
->state
= TPGS_STATE_OPTIMIZED
;
261 sdev_printk(KERN_INFO
, h
->sdev
,
262 "%s: port group %02x switched to state %c\n",
263 ALUA_DH_NAME
, h
->group_id
,
264 print_alua_state(h
->state
));
267 req
->end_io_data
= NULL
;
268 __blk_put_request(req
->q
, req
);
269 if (h
->callback_fn
) {
270 h
->callback_fn(h
->callback_data
, err
);
271 h
->callback_fn
= h
->callback_data
= NULL
;
277 * submit_stpg - Issue a SET TARGET GROUP STATES command
279 * Currently we're only setting the current target port group state
280 * to 'active/optimized' and let the array firmware figure out
281 * the states of the remaining groups.
283 static unsigned submit_stpg(struct alua_dh_data
*h
)
287 struct scsi_device
*sdev
= h
->sdev
;
289 /* Prepare the data buffer */
290 memset(h
->buff
, 0, stpg_len
);
291 h
->buff
[4] = TPGS_STATE_OPTIMIZED
& 0x0f;
292 h
->buff
[6] = (h
->group_id
>> 8) & 0xff;
293 h
->buff
[7] = h
->group_id
& 0xff;
295 rq
= get_alua_req(sdev
, h
->buff
, stpg_len
, WRITE
);
297 return SCSI_DH_RES_TEMP_UNAVAIL
;
299 /* Prepare the command. */
300 rq
->cmd
[0] = MAINTENANCE_OUT
;
301 rq
->cmd
[1] = MO_SET_TARGET_PGS
;
302 rq
->cmd
[6] = (stpg_len
>> 24) & 0xff;
303 rq
->cmd
[7] = (stpg_len
>> 16) & 0xff;
304 rq
->cmd
[8] = (stpg_len
>> 8) & 0xff;
305 rq
->cmd
[9] = stpg_len
& 0xff;
306 rq
->cmd_len
= COMMAND_SIZE(MAINTENANCE_OUT
);
308 rq
->sense
= h
->sense
;
309 memset(rq
->sense
, 0, SCSI_SENSE_BUFFERSIZE
);
310 rq
->sense_len
= h
->senselen
= 0;
313 blk_execute_rq_nowait(rq
->q
, NULL
, rq
, 1, stpg_endio
);
318 * alua_check_tpgs - Evaluate TPGS setting
319 * @sdev: device to be checked
321 * Examine the TPGS setting of the sdev to find out if ALUA
324 static int alua_check_tpgs(struct scsi_device
*sdev
, struct alua_dh_data
*h
)
326 int err
= SCSI_DH_OK
;
328 h
->tpgs
= scsi_device_tpgs(sdev
);
330 case TPGS_MODE_EXPLICIT
|TPGS_MODE_IMPLICIT
:
331 sdev_printk(KERN_INFO
, sdev
,
332 "%s: supports implicit and explicit TPGS\n",
335 case TPGS_MODE_EXPLICIT
:
336 sdev_printk(KERN_INFO
, sdev
, "%s: supports explicit TPGS\n",
339 case TPGS_MODE_IMPLICIT
:
340 sdev_printk(KERN_INFO
, sdev
, "%s: supports implicit TPGS\n",
344 h
->tpgs
= TPGS_MODE_NONE
;
345 sdev_printk(KERN_INFO
, sdev
, "%s: not supported\n",
347 err
= SCSI_DH_DEV_UNSUPP
;
355 * alua_vpd_inquiry - Evaluate INQUIRY vpd page 0x83
356 * @sdev: device to be checked
358 * Extract the relative target port and the target port group
359 * descriptor from the list of identificators.
361 static int alua_vpd_inquiry(struct scsi_device
*sdev
, struct alua_dh_data
*h
)
368 err
= submit_vpd_inquiry(sdev
, h
);
370 if (err
!= SCSI_DH_OK
)
373 /* Check if vpd page exceeds initial buffer */
374 len
= (h
->buff
[2] << 8) + h
->buff
[3] + 4;
375 if (len
> h
->bufflen
) {
376 /* Resubmit with the correct length */
377 if (realloc_buffer(h
, len
)) {
378 sdev_printk(KERN_WARNING
, sdev
,
379 "%s: kmalloc buffer failed\n",
381 /* Temporary failure, bypass */
382 return SCSI_DH_DEV_TEMP_BUSY
;
388 * Now look for the correct descriptor.
391 while (d
< h
->buff
+ len
) {
392 switch (d
[1] & 0xf) {
394 /* Relative target port */
395 h
->rel_port
= (d
[6] << 8) + d
[7];
398 /* Target port group */
399 h
->group_id
= (d
[6] << 8) + d
[7];
407 if (h
->group_id
== -1) {
409 * Internal error; TPGS supported but required
410 * VPD identification descriptors not present.
411 * Disable ALUA support
413 sdev_printk(KERN_INFO
, sdev
,
414 "%s: No target port descriptors found\n",
416 h
->state
= TPGS_STATE_OPTIMIZED
;
417 h
->tpgs
= TPGS_MODE_NONE
;
418 err
= SCSI_DH_DEV_UNSUPP
;
420 sdev_printk(KERN_INFO
, sdev
,
421 "%s: port group %02x rel port %02x\n",
422 ALUA_DH_NAME
, h
->group_id
, h
->rel_port
);
428 static char print_alua_state(int state
)
431 case TPGS_STATE_OPTIMIZED
:
433 case TPGS_STATE_NONOPTIMIZED
:
435 case TPGS_STATE_STANDBY
:
437 case TPGS_STATE_UNAVAILABLE
:
439 case TPGS_STATE_LBA_DEPENDENT
:
441 case TPGS_STATE_OFFLINE
:
443 case TPGS_STATE_TRANSITIONING
:
450 static int alua_check_sense(struct scsi_device
*sdev
,
451 struct scsi_sense_hdr
*sense_hdr
)
453 switch (sense_hdr
->sense_key
) {
455 if (sense_hdr
->asc
== 0x04 && sense_hdr
->ascq
== 0x0a)
457 * LUN Not Accessible - ALUA state transition
459 return ADD_TO_MLQUEUE
;
460 if (sense_hdr
->asc
== 0x04 && sense_hdr
->ascq
== 0x0b)
462 * LUN Not Accessible -- Target port in standby state
465 if (sense_hdr
->asc
== 0x04 && sense_hdr
->ascq
== 0x0c)
467 * LUN Not Accessible -- Target port in unavailable state
470 if (sense_hdr
->asc
== 0x04 && sense_hdr
->ascq
== 0x12)
472 * LUN Not Ready -- Offline
477 if (sense_hdr
->asc
== 0x29 && sense_hdr
->ascq
== 0x00)
479 * Power On, Reset, or Bus Device Reset, just retry.
481 return ADD_TO_MLQUEUE
;
482 if (sense_hdr
->asc
== 0x2a && sense_hdr
->ascq
== 0x01)
484 * Mode Parameters Changed
486 return ADD_TO_MLQUEUE
;
487 if (sense_hdr
->asc
== 0x2a && sense_hdr
->ascq
== 0x06)
491 return ADD_TO_MLQUEUE
;
492 if (sense_hdr
->asc
== 0x2a && sense_hdr
->ascq
== 0x07)
494 * Implicit ALUA state transition failed
496 return ADD_TO_MLQUEUE
;
497 if (sense_hdr
->asc
== 0x3f && sense_hdr
->ascq
== 0x03)
499 * Inquiry data has changed
501 return ADD_TO_MLQUEUE
;
502 if (sense_hdr
->asc
== 0x3f && sense_hdr
->ascq
== 0x0e)
504 * REPORTED_LUNS_DATA_HAS_CHANGED is reported
505 * when switching controllers on targets like
506 * Intel Multi-Flex. We can just retry.
508 return ADD_TO_MLQUEUE
;
512 return SCSI_RETURN_NOT_HANDLED
;
516 * alua_rtpg - Evaluate REPORT TARGET GROUP STATES
517 * @sdev: the device to be evaluated.
519 * Evaluate the Target Port Group State.
520 * Returns SCSI_DH_DEV_OFFLINED if the path is
521 * found to be unusable.
523 static int alua_rtpg(struct scsi_device
*sdev
, struct alua_dh_data
*h
)
525 struct scsi_sense_hdr sense_hdr
;
526 int len
, k
, off
, valid_states
= 0;
529 bool rtpg_ext_hdr_req
= 1;
530 unsigned long expiry
, interval
= 0;
531 unsigned int tpg_desc_tbl_off
;
532 unsigned char orig_transition_tmo
;
534 if (!h
->transition_tmo
)
535 expiry
= round_jiffies_up(jiffies
+ ALUA_FAILOVER_TIMEOUT
* HZ
);
537 expiry
= round_jiffies_up(jiffies
+ h
->transition_tmo
* HZ
);
540 err
= submit_rtpg(sdev
, h
, rtpg_ext_hdr_req
);
542 if (err
== SCSI_DH_IO
&& h
->senselen
> 0) {
543 err
= scsi_normalize_sense(h
->sense
, SCSI_SENSE_BUFFERSIZE
,
549 * submit_rtpg() has failed on existing arrays
550 * when requesting extended header info, and
551 * the array doesn't support extended headers,
552 * even though it shouldn't according to T10.
553 * The retry without rtpg_ext_hdr_req set
556 if (rtpg_ext_hdr_req
== 1 &&
557 sense_hdr
.sense_key
== ILLEGAL_REQUEST
&&
558 sense_hdr
.asc
== 0x24 && sense_hdr
.ascq
== 0) {
559 rtpg_ext_hdr_req
= 0;
563 err
= alua_check_sense(sdev
, &sense_hdr
);
564 if (err
== ADD_TO_MLQUEUE
&& time_before(jiffies
, expiry
))
566 sdev_printk(KERN_INFO
, sdev
,
567 "%s: rtpg sense code %02x/%02x/%02x\n",
568 ALUA_DH_NAME
, sense_hdr
.sense_key
,
569 sense_hdr
.asc
, sense_hdr
.ascq
);
572 if (err
!= SCSI_DH_OK
)
575 len
= (h
->buff
[0] << 24) + (h
->buff
[1] << 16) +
576 (h
->buff
[2] << 8) + h
->buff
[3] + 4;
578 if (len
> h
->bufflen
) {
579 /* Resubmit with the correct length */
580 if (realloc_buffer(h
, len
)) {
581 sdev_printk(KERN_WARNING
, sdev
,
582 "%s: kmalloc buffer failed\n",__func__
);
583 /* Temporary failure, bypass */
584 return SCSI_DH_DEV_TEMP_BUSY
;
589 orig_transition_tmo
= h
->transition_tmo
;
590 if ((h
->buff
[4] & RTPG_FMT_MASK
) == RTPG_FMT_EXT_HDR
&& h
->buff
[5] != 0)
591 h
->transition_tmo
= h
->buff
[5];
593 h
->transition_tmo
= ALUA_FAILOVER_TIMEOUT
;
595 if (orig_transition_tmo
!= h
->transition_tmo
) {
596 sdev_printk(KERN_INFO
, sdev
,
597 "%s: transition timeout set to %d seconds\n",
598 ALUA_DH_NAME
, h
->transition_tmo
);
599 expiry
= jiffies
+ h
->transition_tmo
* HZ
;
602 if ((h
->buff
[4] & RTPG_FMT_MASK
) == RTPG_FMT_EXT_HDR
)
603 tpg_desc_tbl_off
= 8;
605 tpg_desc_tbl_off
= 4;
607 for (k
= tpg_desc_tbl_off
, ucp
= h
->buff
+ tpg_desc_tbl_off
;
609 k
+= off
, ucp
+= off
) {
611 if (h
->group_id
== (ucp
[2] << 8) + ucp
[3]) {
612 h
->state
= ucp
[0] & 0x0f;
613 h
->pref
= ucp
[0] >> 7;
614 valid_states
= ucp
[1];
616 off
= 8 + (ucp
[7] * 4);
619 sdev_printk(KERN_INFO
, sdev
,
620 "%s: port group %02x state %c %s supports %c%c%c%c%c%c%c\n",
621 ALUA_DH_NAME
, h
->group_id
, print_alua_state(h
->state
),
622 h
->pref
? "preferred" : "non-preferred",
623 valid_states
&TPGS_SUPPORT_TRANSITION
?'T':'t',
624 valid_states
&TPGS_SUPPORT_OFFLINE
?'O':'o',
625 valid_states
&TPGS_SUPPORT_LBA_DEPENDENT
?'L':'l',
626 valid_states
&TPGS_SUPPORT_UNAVAILABLE
?'U':'u',
627 valid_states
&TPGS_SUPPORT_STANDBY
?'S':'s',
628 valid_states
&TPGS_SUPPORT_NONOPTIMIZED
?'N':'n',
629 valid_states
&TPGS_SUPPORT_OPTIMIZED
?'A':'a');
632 case TPGS_STATE_TRANSITIONING
:
633 if (time_before(jiffies
, expiry
)) {
634 /* State transition, retry */
639 /* Transitioning time exceeded, set port to standby */
641 h
->state
= TPGS_STATE_STANDBY
;
643 case TPGS_STATE_OFFLINE
:
645 err
= SCSI_DH_DEV_OFFLINED
;
648 /* Useable path if active */
656 * alua_initialize - Initialize ALUA state
657 * @sdev: the device to be initialized
659 * For the prep_fn to work correctly we have
660 * to initialize the ALUA state for the device.
662 static int alua_initialize(struct scsi_device
*sdev
, struct alua_dh_data
*h
)
666 err
= alua_check_tpgs(sdev
, h
);
667 if (err
!= SCSI_DH_OK
)
670 err
= alua_vpd_inquiry(sdev
, h
);
671 if (err
!= SCSI_DH_OK
)
674 err
= alua_rtpg(sdev
, h
);
675 if (err
!= SCSI_DH_OK
)
682 * alua_set_params - set/unset the optimize flag
683 * @sdev: device on the path to be activated
684 * params - parameters in the following format
685 * "no_of_params\0param1\0param2\0param3\0...\0"
686 * For example, to set the flag pass the following parameters
687 * from multipath.conf
688 * hardware_handler "2 alua 1"
690 static int alua_set_params(struct scsi_device
*sdev
, const char *params
)
692 struct alua_dh_data
*h
= get_alua_data(sdev
);
693 unsigned int optimize
= 0, argc
;
694 const char *p
= params
;
695 int result
= SCSI_DH_OK
;
697 if ((sscanf(params
, "%u", &argc
) != 1) || (argc
!= 1))
702 if ((sscanf(p
, "%u", &optimize
) != 1) || (optimize
> 1))
706 h
->flags
|= ALUA_OPTIMIZE_STPG
;
708 h
->flags
&= ~ALUA_OPTIMIZE_STPG
;
714 * alua_activate - activate a path
715 * @sdev: device on the path to be activated
717 * We're currently switching the port group to be activated only and
718 * let the array figure out the rest.
719 * There may be other arrays which require us to switch all port groups
720 * based on a certain policy. But until we actually encounter them it
723 static int alua_activate(struct scsi_device
*sdev
,
724 activate_complete fn
, void *data
)
726 struct alua_dh_data
*h
= get_alua_data(sdev
);
727 int err
= SCSI_DH_OK
;
730 err
= alua_rtpg(sdev
, h
);
731 if (err
!= SCSI_DH_OK
)
734 if (h
->tpgs
& TPGS_MODE_EXPLICIT
) {
736 case TPGS_STATE_NONOPTIMIZED
:
738 if ((h
->flags
& ALUA_OPTIMIZE_STPG
) &&
740 (h
->tpgs
& TPGS_MODE_IMPLICIT
))
743 case TPGS_STATE_STANDBY
:
744 case TPGS_STATE_UNAVAILABLE
:
747 case TPGS_STATE_OFFLINE
:
750 case TPGS_STATE_TRANSITIONING
:
760 h
->callback_data
= data
;
761 err
= submit_stpg(h
);
762 if (err
== SCSI_DH_OK
)
764 h
->callback_fn
= h
->callback_data
= NULL
;
774 * alua_prep_fn - request callback
776 * Fail I/O to all paths not in state
777 * active/optimized or active/non-optimized.
779 static int alua_prep_fn(struct scsi_device
*sdev
, struct request
*req
)
781 struct alua_dh_data
*h
= get_alua_data(sdev
);
782 int ret
= BLKPREP_OK
;
784 if (h
->state
== TPGS_STATE_TRANSITIONING
)
786 else if (h
->state
!= TPGS_STATE_OPTIMIZED
&&
787 h
->state
!= TPGS_STATE_NONOPTIMIZED
&&
788 h
->state
!= TPGS_STATE_LBA_DEPENDENT
) {
790 req
->cmd_flags
|= REQ_QUIET
;
796 static bool alua_match(struct scsi_device
*sdev
)
798 return (scsi_device_tpgs(sdev
) != 0);
801 static int alua_bus_attach(struct scsi_device
*sdev
);
802 static void alua_bus_detach(struct scsi_device
*sdev
);
804 static struct scsi_device_handler alua_dh
= {
805 .name
= ALUA_DH_NAME
,
806 .module
= THIS_MODULE
,
807 .attach
= alua_bus_attach
,
808 .detach
= alua_bus_detach
,
809 .prep_fn
= alua_prep_fn
,
810 .check_sense
= alua_check_sense
,
811 .activate
= alua_activate
,
812 .set_params
= alua_set_params
,
817 * alua_bus_attach - Attach device handler
818 * @sdev: device to be attached to
820 static int alua_bus_attach(struct scsi_device
*sdev
)
822 struct scsi_dh_data
*scsi_dh_data
;
823 struct alua_dh_data
*h
;
825 int err
= SCSI_DH_OK
;
827 scsi_dh_data
= kzalloc(sizeof(*scsi_dh_data
)
828 + sizeof(*h
) , GFP_KERNEL
);
830 sdev_printk(KERN_ERR
, sdev
, "%s: Attach failed\n",
835 scsi_dh_data
->scsi_dh
= &alua_dh
;
836 h
= (struct alua_dh_data
*) scsi_dh_data
->buf
;
837 h
->tpgs
= TPGS_MODE_UNINITIALIZED
;
838 h
->state
= TPGS_STATE_OPTIMIZED
;
842 h
->bufflen
= ALUA_INQUIRY_SIZE
;
845 err
= alua_initialize(sdev
, h
);
846 if ((err
!= SCSI_DH_OK
) && (err
!= SCSI_DH_DEV_OFFLINED
))
849 if (!try_module_get(THIS_MODULE
))
852 spin_lock_irqsave(sdev
->request_queue
->queue_lock
, flags
);
853 sdev
->scsi_dh_data
= scsi_dh_data
;
854 spin_unlock_irqrestore(sdev
->request_queue
->queue_lock
, flags
);
855 sdev_printk(KERN_NOTICE
, sdev
, "%s: Attached\n", ALUA_DH_NAME
);
861 sdev_printk(KERN_ERR
, sdev
, "%s: not attached\n", ALUA_DH_NAME
);
866 * alua_bus_detach - Detach device handler
867 * @sdev: device to be detached from
869 static void alua_bus_detach(struct scsi_device
*sdev
)
871 struct scsi_dh_data
*scsi_dh_data
;
872 struct alua_dh_data
*h
;
875 spin_lock_irqsave(sdev
->request_queue
->queue_lock
, flags
);
876 scsi_dh_data
= sdev
->scsi_dh_data
;
877 sdev
->scsi_dh_data
= NULL
;
878 spin_unlock_irqrestore(sdev
->request_queue
->queue_lock
, flags
);
880 h
= (struct alua_dh_data
*) scsi_dh_data
->buf
;
881 if (h
->buff
&& h
->inq
!= h
->buff
)
884 module_put(THIS_MODULE
);
885 sdev_printk(KERN_NOTICE
, sdev
, "%s: Detached\n", ALUA_DH_NAME
);
888 static int __init
alua_init(void)
892 r
= scsi_register_device_handler(&alua_dh
);
894 printk(KERN_ERR
"%s: Failed to register scsi device handler",
899 static void __exit
alua_exit(void)
901 scsi_unregister_device_handler(&alua_dh
);
904 module_init(alua_init
);
905 module_exit(alua_exit
);
907 MODULE_DESCRIPTION("DM Multipath ALUA support");
908 MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
909 MODULE_LICENSE("GPL");
910 MODULE_VERSION(ALUA_DH_VER
);