2 * Engenio/LSI RDAC SCSI Device Handler
4 * Copyright (C) 2005 Mike Christie. All rights reserved.
5 * Copyright (C) Chandra Seetharaman, IBM Corp. 2007
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 <scsi/scsi.h>
23 #include <scsi/scsi_eh.h>
24 #include <scsi/scsi_dh.h>
26 #define RDAC_NAME "rdac"
27 #define RDAC_RETRY_COUNT 5
32 * These struct definitions and the forming of the
33 * mode page were taken from the LSI RDAC 2.4 GPL'd
34 * driver, and then converted to Linux conventions.
36 #define RDAC_QUIESCENCE_TIME 20;
40 #define RDAC_PAGE_CODE_REDUNDANT_CONTROLLER 0x2c
43 * Controller modes definitions
45 #define RDAC_MODE_TRANSFER_SPECIFIED_LUNS 0x02
50 #define RDAC_FORCED_QUIESENCE 0x02
52 #define RDAC_TIMEOUT (60 * HZ)
53 #define RDAC_RETRIES 3
55 struct rdac_mode_6_hdr
{
62 struct rdac_mode_10_hdr
{
70 struct rdac_mode_common
{
71 u8 controller_serial
[16];
72 u8 alt_controller_serial
[16];
75 u8 quiescence_timeout
;
79 struct rdac_pg_legacy
{
80 struct rdac_mode_6_hdr hdr
;
83 struct rdac_mode_common common
;
84 #define MODE6_MAX_LUN 32
85 u8 lun_table
[MODE6_MAX_LUN
];
91 struct rdac_pg_expanded
{
92 struct rdac_mode_10_hdr hdr
;
96 struct rdac_mode_common common
;
104 u8 page_code
; /* 0xC9 */
107 u8 page_id
[4]; /* "vace" */
113 #define SUBSYS_ID_LEN 16
114 #define SLOT_ID_LEN 2
118 u8 page_code
; /* 0xC4 */
121 u8 page_id
[4]; /* "subs" */
122 u8 subsys_id
[SUBSYS_ID_LEN
];
124 u8 slot_id
[SLOT_ID_LEN
];
128 struct rdac_controller
{
129 u8 subsys_id
[SUBSYS_ID_LEN
];
130 u8 slot_id
[SLOT_ID_LEN
];
133 struct list_head node
; /* list of all controllers */
135 struct rdac_pg_legacy legacy
;
136 struct rdac_pg_expanded expanded
;
141 u8 page_code
; /* 0xC8 */
144 u8 page_id
[4]; /* "edid" */
148 u8 vol_user_label_len
;
149 u8 vol_user_label
[60];
150 u8 array_uniq_id_len
;
151 u8 array_unique_id
[16];
152 u8 array_user_label_len
;
153 u8 array_user_label
[60];
159 u8 page_code
; /* 0xC2 */
162 u8 page_id
[4]; /* "swr4" */
166 u8 max_lun_supported
;
167 u8 partitions
[239]; /* Total allocation length should be 0xFF */
170 struct rdac_dh_data
{
171 struct rdac_controller
*ctlr
;
172 #define UNINITIALIZED_LUN (1 << 8)
174 #define RDAC_STATE_ACTIVE 0
175 #define RDAC_STATE_PASSIVE 1
178 #define RDAC_LUN_UNOWNED 0
179 #define RDAC_LUN_OWNED 1
180 #define RDAC_LUN_AVT 2
182 unsigned char sense
[SCSI_SENSE_BUFFERSIZE
];
184 struct c2_inquiry c2
;
185 struct c4_inquiry c4
;
186 struct c8_inquiry c8
;
187 struct c9_inquiry c9
;
191 static const char *lun_state
[] =
198 static LIST_HEAD(ctlr_list
);
199 static DEFINE_SPINLOCK(list_lock
);
201 static inline struct rdac_dh_data
*get_rdac_data(struct scsi_device
*sdev
)
203 struct scsi_dh_data
*scsi_dh_data
= sdev
->scsi_dh_data
;
204 BUG_ON(scsi_dh_data
== NULL
);
205 return ((struct rdac_dh_data
*) scsi_dh_data
->buf
);
208 static struct request
*get_rdac_req(struct scsi_device
*sdev
,
209 void *buffer
, unsigned buflen
, int rw
)
212 struct request_queue
*q
= sdev
->request_queue
;
214 rq
= blk_get_request(q
, rw
, GFP_NOIO
);
217 sdev_printk(KERN_INFO
, sdev
,
218 "get_rdac_req: blk_get_request failed.\n");
222 if (buflen
&& blk_rq_map_kern(q
, rq
, buffer
, buflen
, GFP_NOIO
)) {
224 sdev_printk(KERN_INFO
, sdev
,
225 "get_rdac_req: blk_rq_map_kern failed.\n");
229 rq
->cmd_type
= REQ_TYPE_BLOCK_PC
;
230 rq
->cmd_flags
|= REQ_FAILFAST_DEV
| REQ_FAILFAST_TRANSPORT
|
232 rq
->retries
= RDAC_RETRIES
;
233 rq
->timeout
= RDAC_TIMEOUT
;
238 static struct request
*rdac_failover_get(struct scsi_device
*sdev
,
239 struct rdac_dh_data
*h
)
242 struct rdac_mode_common
*common
;
245 if (h
->ctlr
->use_ms10
) {
246 struct rdac_pg_expanded
*rdac_pg
;
248 data_size
= sizeof(struct rdac_pg_expanded
);
249 rdac_pg
= &h
->ctlr
->mode_select
.expanded
;
250 memset(rdac_pg
, 0, data_size
);
251 common
= &rdac_pg
->common
;
252 rdac_pg
->page_code
= RDAC_PAGE_CODE_REDUNDANT_CONTROLLER
+ 0x40;
253 rdac_pg
->subpage_code
= 0x1;
254 rdac_pg
->page_len
[0] = 0x01;
255 rdac_pg
->page_len
[1] = 0x28;
256 rdac_pg
->lun_table
[h
->lun
] = 0x81;
258 struct rdac_pg_legacy
*rdac_pg
;
260 data_size
= sizeof(struct rdac_pg_legacy
);
261 rdac_pg
= &h
->ctlr
->mode_select
.legacy
;
262 memset(rdac_pg
, 0, data_size
);
263 common
= &rdac_pg
->common
;
264 rdac_pg
->page_code
= RDAC_PAGE_CODE_REDUNDANT_CONTROLLER
;
265 rdac_pg
->page_len
= 0x68;
266 rdac_pg
->lun_table
[h
->lun
] = 0x81;
268 common
->rdac_mode
[1] = RDAC_MODE_TRANSFER_SPECIFIED_LUNS
;
269 common
->quiescence_timeout
= RDAC_QUIESCENCE_TIME
;
270 common
->rdac_options
= RDAC_FORCED_QUIESENCE
;
272 /* get request for block layer packet command */
273 rq
= get_rdac_req(sdev
, &h
->ctlr
->mode_select
, data_size
, WRITE
);
277 /* Prepare the command. */
278 if (h
->ctlr
->use_ms10
) {
279 rq
->cmd
[0] = MODE_SELECT_10
;
280 rq
->cmd
[7] = data_size
>> 8;
281 rq
->cmd
[8] = data_size
& 0xff;
283 rq
->cmd
[0] = MODE_SELECT
;
284 rq
->cmd
[4] = data_size
;
286 rq
->cmd_len
= COMMAND_SIZE(rq
->cmd
[0]);
288 rq
->sense
= h
->sense
;
289 memset(rq
->sense
, 0, SCSI_SENSE_BUFFERSIZE
);
295 static void release_controller(struct kref
*kref
)
297 struct rdac_controller
*ctlr
;
298 ctlr
= container_of(kref
, struct rdac_controller
, kref
);
300 spin_lock(&list_lock
);
301 list_del(&ctlr
->node
);
302 spin_unlock(&list_lock
);
306 static struct rdac_controller
*get_controller(u8
*subsys_id
, u8
*slot_id
)
308 struct rdac_controller
*ctlr
, *tmp
;
310 spin_lock(&list_lock
);
312 list_for_each_entry(tmp
, &ctlr_list
, node
) {
313 if ((memcmp(tmp
->subsys_id
, subsys_id
, SUBSYS_ID_LEN
) == 0) &&
314 (memcmp(tmp
->slot_id
, slot_id
, SLOT_ID_LEN
) == 0)) {
315 kref_get(&tmp
->kref
);
316 spin_unlock(&list_lock
);
320 ctlr
= kmalloc(sizeof(*ctlr
), GFP_ATOMIC
);
324 /* initialize fields of controller */
325 memcpy(ctlr
->subsys_id
, subsys_id
, SUBSYS_ID_LEN
);
326 memcpy(ctlr
->slot_id
, slot_id
, SLOT_ID_LEN
);
327 kref_init(&ctlr
->kref
);
329 list_add(&ctlr
->node
, &ctlr_list
);
331 spin_unlock(&list_lock
);
335 static int submit_inquiry(struct scsi_device
*sdev
, int page_code
,
336 unsigned int len
, struct rdac_dh_data
*h
)
339 struct request_queue
*q
= sdev
->request_queue
;
340 int err
= SCSI_DH_RES_TEMP_UNAVAIL
;
342 rq
= get_rdac_req(sdev
, &h
->inq
, len
, READ
);
346 /* Prepare the command. */
347 rq
->cmd
[0] = INQUIRY
;
349 rq
->cmd
[2] = page_code
;
351 rq
->cmd_len
= COMMAND_SIZE(INQUIRY
);
353 rq
->sense
= h
->sense
;
354 memset(rq
->sense
, 0, SCSI_SENSE_BUFFERSIZE
);
357 err
= blk_execute_rq(q
, NULL
, rq
, 1);
366 static int get_lun(struct scsi_device
*sdev
, struct rdac_dh_data
*h
)
369 struct c8_inquiry
*inqp
;
371 err
= submit_inquiry(sdev
, 0xC8, sizeof(struct c8_inquiry
), h
);
372 if (err
== SCSI_DH_OK
) {
374 if (inqp
->page_code
!= 0xc8)
375 return SCSI_DH_NOSYS
;
376 if (inqp
->page_id
[0] != 'e' || inqp
->page_id
[1] != 'd' ||
377 inqp
->page_id
[2] != 'i' || inqp
->page_id
[3] != 'd')
378 return SCSI_DH_NOSYS
;
379 h
->lun
= inqp
->lun
[7]; /* Uses only the last byte */
384 static int check_ownership(struct scsi_device
*sdev
, struct rdac_dh_data
*h
)
387 struct c9_inquiry
*inqp
;
389 h
->lun_state
= RDAC_LUN_UNOWNED
;
390 h
->state
= RDAC_STATE_ACTIVE
;
391 err
= submit_inquiry(sdev
, 0xC9, sizeof(struct c9_inquiry
), h
);
392 if (err
== SCSI_DH_OK
) {
394 if ((inqp
->avte_cvp
>> 7) == 0x1) {
395 /* LUN in AVT mode */
396 sdev_printk(KERN_NOTICE
, sdev
,
397 "%s: AVT mode detected\n",
399 h
->lun_state
= RDAC_LUN_AVT
;
400 } else if ((inqp
->avte_cvp
& 0x1) != 0) {
401 /* LUN was owned by the controller */
402 h
->lun_state
= RDAC_LUN_OWNED
;
406 if (h
->lun_state
== RDAC_LUN_UNOWNED
)
407 h
->state
= RDAC_STATE_PASSIVE
;
412 static int initialize_controller(struct scsi_device
*sdev
,
413 struct rdac_dh_data
*h
)
416 struct c4_inquiry
*inqp
;
418 err
= submit_inquiry(sdev
, 0xC4, sizeof(struct c4_inquiry
), h
);
419 if (err
== SCSI_DH_OK
) {
421 h
->ctlr
= get_controller(inqp
->subsys_id
, inqp
->slot_id
);
423 err
= SCSI_DH_RES_TEMP_UNAVAIL
;
428 static int set_mode_select(struct scsi_device
*sdev
, struct rdac_dh_data
*h
)
431 struct c2_inquiry
*inqp
;
433 err
= submit_inquiry(sdev
, 0xC2, sizeof(struct c2_inquiry
), h
);
434 if (err
== SCSI_DH_OK
) {
437 * If more than MODE6_MAX_LUN luns are supported, use
440 if (inqp
->max_lun_supported
>= MODE6_MAX_LUN
)
441 h
->ctlr
->use_ms10
= 1;
443 h
->ctlr
->use_ms10
= 0;
448 static int mode_select_handle_sense(struct scsi_device
*sdev
,
449 unsigned char *sensebuf
)
451 struct scsi_sense_hdr sense_hdr
;
452 int err
= SCSI_DH_IO
, ret
;
454 ret
= scsi_normalize_sense(sensebuf
, SCSI_SENSE_BUFFERSIZE
, &sense_hdr
);
460 switch (sense_hdr
.sense_key
) {
462 case ABORTED_COMMAND
:
467 if (sense_hdr
.asc
== 0x04 && sense_hdr
.ascq
== 0x01)
468 /* LUN Not Ready and is in the Process of Becoming
473 case ILLEGAL_REQUEST
:
474 if (sense_hdr
.asc
== 0x91 && sense_hdr
.ascq
== 0x36)
476 * Command Lock contention
481 sdev_printk(KERN_INFO
, sdev
,
482 "MODE_SELECT failed with sense %02x/%02x/%02x.\n",
483 sense_hdr
.sense_key
, sense_hdr
.asc
, sense_hdr
.ascq
);
490 static int send_mode_select(struct scsi_device
*sdev
, struct rdac_dh_data
*h
)
493 struct request_queue
*q
= sdev
->request_queue
;
494 int err
, retry_cnt
= RDAC_RETRY_COUNT
;
497 err
= SCSI_DH_RES_TEMP_UNAVAIL
;
498 rq
= rdac_failover_get(sdev
, h
);
502 sdev_printk(KERN_INFO
, sdev
, "%s MODE_SELECT command.\n",
503 (retry_cnt
== RDAC_RETRY_COUNT
) ? "queueing" : "retrying");
505 err
= blk_execute_rq(q
, NULL
, rq
, 1);
507 if (err
!= SCSI_DH_OK
) {
508 err
= mode_select_handle_sense(sdev
, h
->sense
);
509 if (err
== SCSI_DH_RETRY
&& retry_cnt
--)
512 if (err
== SCSI_DH_OK
)
513 h
->state
= RDAC_STATE_ACTIVE
;
519 static int rdac_activate(struct scsi_device
*sdev
)
521 struct rdac_dh_data
*h
= get_rdac_data(sdev
);
522 int err
= SCSI_DH_OK
;
524 err
= check_ownership(sdev
, h
);
525 if (err
!= SCSI_DH_OK
)
529 err
= initialize_controller(sdev
, h
);
530 if (err
!= SCSI_DH_OK
)
534 if (h
->ctlr
->use_ms10
== -1) {
535 err
= set_mode_select(sdev
, h
);
536 if (err
!= SCSI_DH_OK
)
539 if (h
->lun_state
== RDAC_LUN_UNOWNED
)
540 err
= send_mode_select(sdev
, h
);
545 static int rdac_prep_fn(struct scsi_device
*sdev
, struct request
*req
)
547 struct rdac_dh_data
*h
= get_rdac_data(sdev
);
548 int ret
= BLKPREP_OK
;
550 if (h
->state
!= RDAC_STATE_ACTIVE
) {
552 req
->cmd_flags
|= REQ_QUIET
;
558 static int rdac_check_sense(struct scsi_device
*sdev
,
559 struct scsi_sense_hdr
*sense_hdr
)
561 struct rdac_dh_data
*h
= get_rdac_data(sdev
);
562 switch (sense_hdr
->sense_key
) {
564 if (sense_hdr
->asc
== 0x04 && sense_hdr
->ascq
== 0x81)
565 /* LUN Not Ready - Storage firmware incompatible
566 * Manual code synchonisation required.
568 * Nothing we can do here. Try to bypass the path.
571 if (sense_hdr
->asc
== 0x04 && sense_hdr
->ascq
== 0xA1)
572 /* LUN Not Ready - Quiescense in progress
574 * Just retry and wait.
576 return ADD_TO_MLQUEUE
;
577 if (sense_hdr
->asc
== 0xA1 && sense_hdr
->ascq
== 0x02)
578 /* LUN Not Ready - Quiescense in progress
579 * or has been achieved
582 return ADD_TO_MLQUEUE
;
584 case ILLEGAL_REQUEST
:
585 if (sense_hdr
->asc
== 0x94 && sense_hdr
->ascq
== 0x01) {
586 /* Invalid Request - Current Logical Unit Ownership.
587 * Controller is not the current owner of the LUN,
588 * Fail the path, so that the other path be used.
590 h
->state
= RDAC_STATE_PASSIVE
;
595 if (sense_hdr
->asc
== 0x29 && sense_hdr
->ascq
== 0x00)
597 * Power On, Reset, or Bus Device Reset, just retry.
599 return ADD_TO_MLQUEUE
;
600 if (sense_hdr
->asc
== 0x8b && sense_hdr
->ascq
== 0x02)
602 * Quiescence in progress , just retry.
604 return ADD_TO_MLQUEUE
;
607 /* success just means we do not care what scsi-ml does */
608 return SCSI_RETURN_NOT_HANDLED
;
611 static const struct scsi_dh_devlist rdac_dev_list
[] = {
623 {"STK", "OPENstorage D280"},
625 {"SUN", "LCSM100_F"},
628 {"LSI", "INF-01-00"},
629 {"ENGENIO", "INF-01-00"},
633 static int rdac_bus_attach(struct scsi_device
*sdev
);
634 static void rdac_bus_detach(struct scsi_device
*sdev
);
636 static struct scsi_device_handler rdac_dh
= {
638 .module
= THIS_MODULE
,
639 .devlist
= rdac_dev_list
,
640 .prep_fn
= rdac_prep_fn
,
641 .check_sense
= rdac_check_sense
,
642 .attach
= rdac_bus_attach
,
643 .detach
= rdac_bus_detach
,
644 .activate
= rdac_activate
,
647 static int rdac_bus_attach(struct scsi_device
*sdev
)
649 struct scsi_dh_data
*scsi_dh_data
;
650 struct rdac_dh_data
*h
;
654 scsi_dh_data
= kzalloc(sizeof(struct scsi_device_handler
*)
655 + sizeof(*h
) , GFP_KERNEL
);
657 sdev_printk(KERN_ERR
, sdev
, "%s: Attach failed\n",
662 scsi_dh_data
->scsi_dh
= &rdac_dh
;
663 h
= (struct rdac_dh_data
*) scsi_dh_data
->buf
;
664 h
->lun
= UNINITIALIZED_LUN
;
665 h
->state
= RDAC_STATE_ACTIVE
;
667 err
= get_lun(sdev
, h
);
668 if (err
!= SCSI_DH_OK
)
671 err
= check_ownership(sdev
, h
);
672 if (err
!= SCSI_DH_OK
)
675 if (!try_module_get(THIS_MODULE
))
678 spin_lock_irqsave(sdev
->request_queue
->queue_lock
, flags
);
679 sdev
->scsi_dh_data
= scsi_dh_data
;
680 spin_unlock_irqrestore(sdev
->request_queue
->queue_lock
, flags
);
682 sdev_printk(KERN_NOTICE
, sdev
,
684 RDAC_NAME
, h
->lun
, lun_state
[(int)h
->lun_state
]);
690 sdev_printk(KERN_ERR
, sdev
, "%s: not attached\n",
695 static void rdac_bus_detach( struct scsi_device
*sdev
)
697 struct scsi_dh_data
*scsi_dh_data
;
698 struct rdac_dh_data
*h
;
701 spin_lock_irqsave(sdev
->request_queue
->queue_lock
, flags
);
702 scsi_dh_data
= sdev
->scsi_dh_data
;
703 sdev
->scsi_dh_data
= NULL
;
704 spin_unlock_irqrestore(sdev
->request_queue
->queue_lock
, flags
);
706 h
= (struct rdac_dh_data
*) scsi_dh_data
->buf
;
708 kref_put(&h
->ctlr
->kref
, release_controller
);
710 module_put(THIS_MODULE
);
711 sdev_printk(KERN_NOTICE
, sdev
, "%s: Detached\n", RDAC_NAME
);
716 static int __init
rdac_init(void)
720 r
= scsi_register_device_handler(&rdac_dh
);
722 printk(KERN_ERR
"Failed to register scsi device handler.");
726 static void __exit
rdac_exit(void)
728 scsi_unregister_device_handler(&rdac_dh
);
731 module_init(rdac_init
);
732 module_exit(rdac_exit
);
734 MODULE_DESCRIPTION("Multipath LSI/Engenio RDAC driver");
735 MODULE_AUTHOR("Mike Christie, Chandra Seetharaman");
736 MODULE_LICENSE("GPL");