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"
31 * These struct definitions and the forming of the
32 * mode page were taken from the LSI RDAC 2.4 GPL'd
33 * driver, and then converted to Linux conventions.
35 #define RDAC_QUIESCENCE_TIME 20;
39 #define RDAC_PAGE_CODE_REDUNDANT_CONTROLLER 0x2c
42 * Controller modes definitions
44 #define RDAC_MODE_TRANSFER_SPECIFIED_LUNS 0x02
49 #define RDAC_FORCED_QUIESENCE 0x02
51 #define RDAC_TIMEOUT (60 * HZ)
52 #define RDAC_RETRIES 3
54 struct rdac_mode_6_hdr {
61 struct rdac_mode_10_hdr {
69 struct rdac_mode_common {
70 u8 controller_serial[16];
71 u8 alt_controller_serial[16];
74 u8 quiescence_timeout;
78 struct rdac_pg_legacy {
79 struct rdac_mode_6_hdr hdr;
82 struct rdac_mode_common common;
83 #define MODE6_MAX_LUN 32
84 u8 lun_table[MODE6_MAX_LUN];
90 struct rdac_pg_expanded {
91 struct rdac_mode_10_hdr hdr;
95 struct rdac_mode_common common;
103 u8 page_code; /* 0xC9 */
106 u8 page_id[4]; /* "vace" */
112 #define SUBSYS_ID_LEN 16
113 #define SLOT_ID_LEN 2
117 u8 page_code; /* 0xC4 */
120 u8 page_id[4]; /* "subs" */
121 u8 subsys_id[SUBSYS_ID_LEN];
123 u8 slot_id[SLOT_ID_LEN];
127 struct rdac_controller {
128 u8 subsys_id[SUBSYS_ID_LEN];
129 u8 slot_id[SLOT_ID_LEN];
132 struct list_head node; /* list of all controllers */
134 struct rdac_pg_legacy legacy;
135 struct rdac_pg_expanded expanded;
140 u8 page_code; /* 0xC8 */
143 u8 page_id[4]; /* "edid" */
147 u8 vol_user_label_len;
148 u8 vol_user_label[60];
149 u8 array_uniq_id_len;
150 u8 array_unique_id[16];
151 u8 array_user_label_len;
152 u8 array_user_label[60];
158 u8 page_code; /* 0xC2 */
161 u8 page_id[4]; /* "swr4" */
165 u8 max_lun_supported;
166 u8 partitions[239]; /* Total allocation length should be 0xFF */
169 struct rdac_dh_data {
170 struct rdac_controller *ctlr;
171 #define UNINITIALIZED_LUN (1 << 8)
173 #define RDAC_STATE_ACTIVE 0
174 #define RDAC_STATE_PASSIVE 1
177 #define RDAC_LUN_UNOWNED 0
178 #define RDAC_LUN_OWNED 1
179 #define RDAC_LUN_AVT 2
181 unsigned char sense[SCSI_SENSE_BUFFERSIZE];
183 struct c2_inquiry c2;
184 struct c4_inquiry c4;
185 struct c8_inquiry c8;
186 struct c9_inquiry c9;
190 static const char *lun_state[] =
197 static LIST_HEAD(ctlr_list);
198 static DEFINE_SPINLOCK(list_lock);
200 static inline struct rdac_dh_data *get_rdac_data(struct scsi_device *sdev)
202 struct scsi_dh_data *scsi_dh_data = sdev->scsi_dh_data;
203 BUG_ON(scsi_dh_data == NULL);
204 return ((struct rdac_dh_data *) scsi_dh_data->buf);
207 static struct request *get_rdac_req(struct scsi_device *sdev,
208 void *buffer, unsigned buflen, int rw)
211 struct request_queue *q = sdev->request_queue;
213 rq = blk_get_request(q, rw, GFP_NOIO);
216 sdev_printk(KERN_INFO, sdev,
217 "get_rdac_req: blk_get_request failed.\n");
221 if (buflen && blk_rq_map_kern(q, rq, buffer, buflen, GFP_NOIO)) {
223 sdev_printk(KERN_INFO, sdev,
224 "get_rdac_req: blk_rq_map_kern failed.\n");
228 rq->cmd_type = REQ_TYPE_BLOCK_PC;
229 rq->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
231 rq->retries = RDAC_RETRIES;
232 rq->timeout = RDAC_TIMEOUT;
237 static struct request *rdac_failover_get(struct scsi_device *sdev,
238 struct rdac_dh_data *h)
241 struct rdac_mode_common *common;
244 if (h->ctlr->use_ms10) {
245 struct rdac_pg_expanded *rdac_pg;
247 data_size = sizeof(struct rdac_pg_expanded);
248 rdac_pg = &h->ctlr->mode_select.expanded;
249 memset(rdac_pg, 0, data_size);
250 common = &rdac_pg->common;
251 rdac_pg->page_code = RDAC_PAGE_CODE_REDUNDANT_CONTROLLER + 0x40;
252 rdac_pg->subpage_code = 0x1;
253 rdac_pg->page_len[0] = 0x01;
254 rdac_pg->page_len[1] = 0x28;
255 rdac_pg->lun_table[h->lun] = 0x81;
257 struct rdac_pg_legacy *rdac_pg;
259 data_size = sizeof(struct rdac_pg_legacy);
260 rdac_pg = &h->ctlr->mode_select.legacy;
261 memset(rdac_pg, 0, data_size);
262 common = &rdac_pg->common;
263 rdac_pg->page_code = RDAC_PAGE_CODE_REDUNDANT_CONTROLLER;
264 rdac_pg->page_len = 0x68;
265 rdac_pg->lun_table[h->lun] = 0x81;
267 common->rdac_mode[1] = RDAC_MODE_TRANSFER_SPECIFIED_LUNS;
268 common->quiescence_timeout = RDAC_QUIESCENCE_TIME;
269 common->rdac_options = RDAC_FORCED_QUIESENCE;
271 /* get request for block layer packet command */
272 rq = get_rdac_req(sdev, &h->ctlr->mode_select, data_size, WRITE);
276 /* Prepare the command. */
277 if (h->ctlr->use_ms10) {
278 rq->cmd[0] = MODE_SELECT_10;
279 rq->cmd[7] = data_size >> 8;
280 rq->cmd[8] = data_size & 0xff;
282 rq->cmd[0] = MODE_SELECT;
283 rq->cmd[4] = data_size;
285 rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
287 rq->sense = h->sense;
288 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
294 static void release_controller(struct kref *kref)
296 struct rdac_controller *ctlr;
297 ctlr = container_of(kref, struct rdac_controller, kref);
299 spin_lock(&list_lock);
300 list_del(&ctlr->node);
301 spin_unlock(&list_lock);
305 static struct rdac_controller *get_controller(u8 *subsys_id, u8 *slot_id)
307 struct rdac_controller *ctlr, *tmp;
309 spin_lock(&list_lock);
311 list_for_each_entry(tmp, &ctlr_list, node) {
312 if ((memcmp(tmp->subsys_id, subsys_id, SUBSYS_ID_LEN) == 0) &&
313 (memcmp(tmp->slot_id, slot_id, SLOT_ID_LEN) == 0)) {
314 kref_get(&tmp->kref);
315 spin_unlock(&list_lock);
319 ctlr = kmalloc(sizeof(*ctlr), GFP_ATOMIC);
323 /* initialize fields of controller */
324 memcpy(ctlr->subsys_id, subsys_id, SUBSYS_ID_LEN);
325 memcpy(ctlr->slot_id, slot_id, SLOT_ID_LEN);
326 kref_init(&ctlr->kref);
328 list_add(&ctlr->node, &ctlr_list);
330 spin_unlock(&list_lock);
334 static int submit_inquiry(struct scsi_device *sdev, int page_code,
335 unsigned int len, struct rdac_dh_data *h)
338 struct request_queue *q = sdev->request_queue;
339 int err = SCSI_DH_RES_TEMP_UNAVAIL;
341 rq = get_rdac_req(sdev, &h->inq, len, READ);
345 /* Prepare the command. */
346 rq->cmd[0] = INQUIRY;
348 rq->cmd[2] = page_code;
350 rq->cmd_len = COMMAND_SIZE(INQUIRY);
352 rq->sense = h->sense;
353 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
356 err = blk_execute_rq(q, NULL, rq, 1);
365 static int get_lun(struct scsi_device *sdev, struct rdac_dh_data *h)
368 struct c8_inquiry *inqp;
370 err = submit_inquiry(sdev, 0xC8, sizeof(struct c8_inquiry), h);
371 if (err == SCSI_DH_OK) {
373 if (inqp->page_code != 0xc8)
374 return SCSI_DH_NOSYS;
375 if (inqp->page_id[0] != 'e' || inqp->page_id[1] != 'd' ||
376 inqp->page_id[2] != 'i' || inqp->page_id[3] != 'd')
377 return SCSI_DH_NOSYS;
378 h->lun = inqp->lun[7]; /* Uses only the last byte */
383 static int check_ownership(struct scsi_device *sdev, struct rdac_dh_data *h)
386 struct c9_inquiry *inqp;
388 h->lun_state = RDAC_LUN_UNOWNED;
389 err = submit_inquiry(sdev, 0xC9, sizeof(struct c9_inquiry), h);
390 if (err == SCSI_DH_OK) {
392 if ((inqp->avte_cvp >> 7) == 0x1) {
393 /* LUN in AVT mode */
394 sdev_printk(KERN_NOTICE, sdev,
395 "%s: AVT mode detected\n",
397 h->lun_state = RDAC_LUN_AVT;
398 } else if ((inqp->avte_cvp & 0x1) != 0) {
399 /* LUN was owned by the controller */
400 h->lun_state = RDAC_LUN_OWNED;
404 if (h->lun_state == RDAC_LUN_UNOWNED)
405 h->state = RDAC_STATE_PASSIVE;
410 static int initialize_controller(struct scsi_device *sdev,
411 struct rdac_dh_data *h)
414 struct c4_inquiry *inqp;
416 err = submit_inquiry(sdev, 0xC4, sizeof(struct c4_inquiry), h);
417 if (err == SCSI_DH_OK) {
419 h->ctlr = get_controller(inqp->subsys_id, inqp->slot_id);
421 err = SCSI_DH_RES_TEMP_UNAVAIL;
426 static int set_mode_select(struct scsi_device *sdev, struct rdac_dh_data *h)
429 struct c2_inquiry *inqp;
431 err = submit_inquiry(sdev, 0xC2, sizeof(struct c2_inquiry), h);
432 if (err == SCSI_DH_OK) {
435 * If more than MODE6_MAX_LUN luns are supported, use
438 if (inqp->max_lun_supported >= MODE6_MAX_LUN)
439 h->ctlr->use_ms10 = 1;
441 h->ctlr->use_ms10 = 0;
446 static int mode_select_handle_sense(struct scsi_device *sdev,
447 unsigned char *sensebuf)
449 struct scsi_sense_hdr sense_hdr;
450 int sense, err = SCSI_DH_IO, ret;
452 ret = scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE, &sense_hdr);
457 sense = (sense_hdr.sense_key << 16) | (sense_hdr.asc << 8) |
459 /* If it is retryable failure, submit the c9 inquiry again */
460 if (sense == 0x59136 || sense == 0x68b02 || sense == 0xb8b02 ||
462 /* 0x59136 - Command lock contention
463 * 0x[6b]8b02 - Quiesense in progress or achieved
464 * 0x62900 - Power On, Reset, or Bus Device Reset
470 sdev_printk(KERN_INFO, sdev,
471 "MODE_SELECT failed with sense 0x%x.\n", sense);
476 static int send_mode_select(struct scsi_device *sdev, struct rdac_dh_data *h)
479 struct request_queue *q = sdev->request_queue;
480 int err = SCSI_DH_RES_TEMP_UNAVAIL;
482 rq = rdac_failover_get(sdev, h);
486 sdev_printk(KERN_INFO, sdev, "queueing MODE_SELECT command.\n");
488 err = blk_execute_rq(q, NULL, rq, 1);
489 if (err != SCSI_DH_OK)
490 err = mode_select_handle_sense(sdev, h->sense);
491 if (err == SCSI_DH_OK)
492 h->state = RDAC_STATE_ACTIVE;
499 static int rdac_activate(struct scsi_device *sdev)
501 struct rdac_dh_data *h = get_rdac_data(sdev);
502 int err = SCSI_DH_OK;
504 err = check_ownership(sdev, h);
505 if (err != SCSI_DH_OK)
509 err = initialize_controller(sdev, h);
510 if (err != SCSI_DH_OK)
514 if (h->ctlr->use_ms10 == -1) {
515 err = set_mode_select(sdev, h);
516 if (err != SCSI_DH_OK)
519 if (h->lun_state == RDAC_LUN_UNOWNED)
520 err = send_mode_select(sdev, h);
525 static int rdac_prep_fn(struct scsi_device *sdev, struct request *req)
527 struct rdac_dh_data *h = get_rdac_data(sdev);
528 int ret = BLKPREP_OK;
530 if (h->state != RDAC_STATE_ACTIVE) {
532 req->cmd_flags |= REQ_QUIET;
538 static int rdac_check_sense(struct scsi_device *sdev,
539 struct scsi_sense_hdr *sense_hdr)
541 struct rdac_dh_data *h = get_rdac_data(sdev);
542 switch (sense_hdr->sense_key) {
544 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x81)
545 /* LUN Not Ready - Storage firmware incompatible
546 * Manual code synchonisation required.
548 * Nothing we can do here. Try to bypass the path.
551 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0xA1)
552 /* LUN Not Ready - Quiescense in progress
554 * Just retry and wait.
556 return ADD_TO_MLQUEUE;
558 case ILLEGAL_REQUEST:
559 if (sense_hdr->asc == 0x94 && sense_hdr->ascq == 0x01) {
560 /* Invalid Request - Current Logical Unit Ownership.
561 * Controller is not the current owner of the LUN,
562 * Fail the path, so that the other path be used.
564 h->state = RDAC_STATE_PASSIVE;
569 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
571 * Power On, Reset, or Bus Device Reset, just retry.
573 return ADD_TO_MLQUEUE;
576 /* success just means we do not care what scsi-ml does */
577 return SCSI_RETURN_NOT_HANDLED;
580 static const struct scsi_dh_devlist rdac_dev_list[] = {
592 {"STK", "OPENstorage D280"},
594 {"SUN", "LCSM100_F"},
600 static int rdac_bus_attach(struct scsi_device *sdev);
601 static void rdac_bus_detach(struct scsi_device *sdev);
603 static struct scsi_device_handler rdac_dh = {
605 .module = THIS_MODULE,
606 .devlist = rdac_dev_list,
607 .prep_fn = rdac_prep_fn,
608 .check_sense = rdac_check_sense,
609 .attach = rdac_bus_attach,
610 .detach = rdac_bus_detach,
611 .activate = rdac_activate,
614 static int rdac_bus_attach(struct scsi_device *sdev)
616 struct scsi_dh_data *scsi_dh_data;
617 struct rdac_dh_data *h;
621 scsi_dh_data = kzalloc(sizeof(struct scsi_device_handler *)
622 + sizeof(*h) , GFP_KERNEL);
624 sdev_printk(KERN_ERR, sdev, "%s: Attach failed\n",
629 scsi_dh_data->scsi_dh = &rdac_dh;
630 h = (struct rdac_dh_data *) scsi_dh_data->buf;
631 h->lun = UNINITIALIZED_LUN;
632 h->state = RDAC_STATE_ACTIVE;
634 err = get_lun(sdev, h);
635 if (err != SCSI_DH_OK)
638 err = check_ownership(sdev, h);
639 if (err != SCSI_DH_OK)
642 if (!try_module_get(THIS_MODULE))
645 spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
646 sdev->scsi_dh_data = scsi_dh_data;
647 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
649 sdev_printk(KERN_NOTICE, sdev,
651 RDAC_NAME, h->lun, lun_state[(int)h->lun_state]);
657 sdev_printk(KERN_ERR, sdev, "%s: not attached\n",
662 static void rdac_bus_detach( struct scsi_device *sdev )
664 struct scsi_dh_data *scsi_dh_data;
665 struct rdac_dh_data *h;
668 spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
669 scsi_dh_data = sdev->scsi_dh_data;
670 sdev->scsi_dh_data = NULL;
671 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
673 h = (struct rdac_dh_data *) scsi_dh_data->buf;
675 kref_put(&h->ctlr->kref, release_controller);
677 module_put(THIS_MODULE);
678 sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", RDAC_NAME);
683 static int __init rdac_init(void)
687 r = scsi_register_device_handler(&rdac_dh);
689 printk(KERN_ERR "Failed to register scsi device handler.");
693 static void __exit rdac_exit(void)
695 scsi_unregister_device_handler(&rdac_dh);
698 module_init(rdac_init);
699 module_exit(rdac_exit);
701 MODULE_DESCRIPTION("Multipath LSI/Engenio RDAC driver");
702 MODULE_AUTHOR("Mike Christie, Chandra Seetharaman");
703 MODULE_LICENSE("GPL");