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
176 unsigned char sense[SCSI_SENSE_BUFFERSIZE];
178 struct c2_inquiry c2;
179 struct c4_inquiry c4;
180 struct c8_inquiry c8;
181 struct c9_inquiry c9;
185 static LIST_HEAD(ctlr_list);
186 static DEFINE_SPINLOCK(list_lock);
188 static inline struct rdac_dh_data *get_rdac_data(struct scsi_device *sdev)
190 struct scsi_dh_data *scsi_dh_data = sdev->scsi_dh_data;
191 BUG_ON(scsi_dh_data == NULL);
192 return ((struct rdac_dh_data *) scsi_dh_data->buf);
195 static struct request *get_rdac_req(struct scsi_device *sdev,
196 void *buffer, unsigned buflen, int rw)
199 struct request_queue *q = sdev->request_queue;
200 struct rdac_dh_data *h = get_rdac_data(sdev);
202 rq = blk_get_request(q, rw, GFP_KERNEL);
205 sdev_printk(KERN_INFO, sdev,
206 "get_rdac_req: blk_get_request failed.\n");
210 if (buflen && blk_rq_map_kern(q, rq, buffer, buflen, GFP_KERNEL)) {
212 sdev_printk(KERN_INFO, sdev,
213 "get_rdac_req: blk_rq_map_kern failed.\n");
217 memset(&rq->cmd, 0, BLK_MAX_CDB);
218 rq->sense = h->sense;
219 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
222 rq->cmd_type = REQ_TYPE_BLOCK_PC;
223 rq->cmd_flags |= REQ_FAILFAST | REQ_NOMERGE;
224 rq->retries = RDAC_RETRIES;
225 rq->timeout = RDAC_TIMEOUT;
230 static struct request *rdac_failover_get(struct scsi_device *sdev)
233 struct rdac_mode_common *common;
235 struct rdac_dh_data *h = get_rdac_data(sdev);
237 if (h->ctlr->use_ms10) {
238 struct rdac_pg_expanded *rdac_pg;
240 data_size = sizeof(struct rdac_pg_expanded);
241 rdac_pg = &h->ctlr->mode_select.expanded;
242 memset(rdac_pg, 0, data_size);
243 common = &rdac_pg->common;
244 rdac_pg->page_code = RDAC_PAGE_CODE_REDUNDANT_CONTROLLER + 0x40;
245 rdac_pg->subpage_code = 0x1;
246 rdac_pg->page_len[0] = 0x01;
247 rdac_pg->page_len[1] = 0x28;
248 rdac_pg->lun_table[h->lun] = 0x81;
250 struct rdac_pg_legacy *rdac_pg;
252 data_size = sizeof(struct rdac_pg_legacy);
253 rdac_pg = &h->ctlr->mode_select.legacy;
254 memset(rdac_pg, 0, data_size);
255 common = &rdac_pg->common;
256 rdac_pg->page_code = RDAC_PAGE_CODE_REDUNDANT_CONTROLLER;
257 rdac_pg->page_len = 0x68;
258 rdac_pg->lun_table[h->lun] = 0x81;
260 common->rdac_mode[1] = RDAC_MODE_TRANSFER_SPECIFIED_LUNS;
261 common->quiescence_timeout = RDAC_QUIESCENCE_TIME;
262 common->rdac_options = RDAC_FORCED_QUIESENCE;
264 /* get request for block layer packet command */
265 rq = get_rdac_req(sdev, &h->ctlr->mode_select, data_size, WRITE);
269 /* Prepare the command. */
270 if (h->ctlr->use_ms10) {
271 rq->cmd[0] = MODE_SELECT_10;
272 rq->cmd[7] = data_size >> 8;
273 rq->cmd[8] = data_size & 0xff;
275 rq->cmd[0] = MODE_SELECT;
276 rq->cmd[4] = data_size;
278 rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
283 static void release_controller(struct kref *kref)
285 struct rdac_controller *ctlr;
286 ctlr = container_of(kref, struct rdac_controller, kref);
288 spin_lock(&list_lock);
289 list_del(&ctlr->node);
290 spin_unlock(&list_lock);
294 static struct rdac_controller *get_controller(u8 *subsys_id, u8 *slot_id)
296 struct rdac_controller *ctlr, *tmp;
298 spin_lock(&list_lock);
300 list_for_each_entry(tmp, &ctlr_list, node) {
301 if ((memcmp(tmp->subsys_id, subsys_id, SUBSYS_ID_LEN) == 0) &&
302 (memcmp(tmp->slot_id, slot_id, SLOT_ID_LEN) == 0)) {
303 kref_get(&tmp->kref);
304 spin_unlock(&list_lock);
308 ctlr = kmalloc(sizeof(*ctlr), GFP_ATOMIC);
312 /* initialize fields of controller */
313 memcpy(ctlr->subsys_id, subsys_id, SUBSYS_ID_LEN);
314 memcpy(ctlr->slot_id, slot_id, SLOT_ID_LEN);
315 kref_init(&ctlr->kref);
317 list_add(&ctlr->node, &ctlr_list);
319 spin_unlock(&list_lock);
323 static int submit_inquiry(struct scsi_device *sdev, int page_code,
327 struct request_queue *q = sdev->request_queue;
328 struct rdac_dh_data *h = get_rdac_data(sdev);
329 int err = SCSI_DH_RES_TEMP_UNAVAIL;
331 rq = get_rdac_req(sdev, &h->inq, len, READ);
335 /* Prepare the command. */
336 rq->cmd[0] = INQUIRY;
338 rq->cmd[2] = page_code;
340 rq->cmd_len = COMMAND_SIZE(INQUIRY);
341 err = blk_execute_rq(q, NULL, rq, 1);
348 static int get_lun(struct scsi_device *sdev)
351 struct c8_inquiry *inqp;
352 struct rdac_dh_data *h = get_rdac_data(sdev);
354 err = submit_inquiry(sdev, 0xC8, sizeof(struct c8_inquiry));
355 if (err == SCSI_DH_OK) {
357 h->lun = inqp->lun[7]; /* currently it uses only one byte */
363 #define RDAC_UNOWNED 1
364 #define RDAC_FAILED 2
365 static int check_ownership(struct scsi_device *sdev)
368 struct c9_inquiry *inqp;
369 struct rdac_dh_data *h = get_rdac_data(sdev);
371 err = submit_inquiry(sdev, 0xC9, sizeof(struct c9_inquiry));
372 if (err == SCSI_DH_OK) {
376 * If in AVT mode or if the path already owns the LUN,
379 if (((inqp->avte_cvp >> 7) == 0x1) ||
380 ((inqp->avte_cvp & 0x1) != 0))
387 static int initialize_controller(struct scsi_device *sdev)
390 struct c4_inquiry *inqp;
391 struct rdac_dh_data *h = get_rdac_data(sdev);
393 err = submit_inquiry(sdev, 0xC4, sizeof(struct c4_inquiry));
394 if (err == SCSI_DH_OK) {
396 h->ctlr = get_controller(inqp->subsys_id, inqp->slot_id);
398 err = SCSI_DH_RES_TEMP_UNAVAIL;
403 static int set_mode_select(struct scsi_device *sdev)
406 struct c2_inquiry *inqp;
407 struct rdac_dh_data *h = get_rdac_data(sdev);
409 err = submit_inquiry(sdev, 0xC2, sizeof(struct c2_inquiry));
410 if (err == SCSI_DH_OK) {
413 * If more than MODE6_MAX_LUN luns are supported, use
416 if (inqp->max_lun_supported >= MODE6_MAX_LUN)
417 h->ctlr->use_ms10 = 1;
419 h->ctlr->use_ms10 = 0;
424 static int mode_select_handle_sense(struct scsi_device *sdev)
426 struct scsi_sense_hdr sense_hdr;
427 struct rdac_dh_data *h = get_rdac_data(sdev);
428 int sense, err = SCSI_DH_IO, ret;
430 ret = scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE, &sense_hdr);
435 sense = (sense_hdr.sense_key << 16) | (sense_hdr.asc << 8) |
437 /* If it is retryable failure, submit the c9 inquiry again */
438 if (sense == 0x59136 || sense == 0x68b02 || sense == 0xb8b02 ||
440 /* 0x59136 - Command lock contention
441 * 0x[6b]8b02 - Quiesense in progress or achieved
442 * 0x62900 - Power On, Reset, or Bus Device Reset
448 sdev_printk(KERN_INFO, sdev,
449 "MODE_SELECT failed with sense 0x%x.\n", sense);
454 static int send_mode_select(struct scsi_device *sdev)
457 struct request_queue *q = sdev->request_queue;
458 struct rdac_dh_data *h = get_rdac_data(sdev);
459 int err = SCSI_DH_RES_TEMP_UNAVAIL;
461 rq = rdac_failover_get(sdev);
465 sdev_printk(KERN_INFO, sdev, "queueing MODE_SELECT command.\n");
467 err = blk_execute_rq(q, NULL, rq, 1);
468 if (err != SCSI_DH_OK)
469 err = mode_select_handle_sense(sdev);
470 if (err == SCSI_DH_OK)
471 h->state = RDAC_STATE_ACTIVE;
476 static int rdac_activate(struct scsi_device *sdev)
478 struct rdac_dh_data *h = get_rdac_data(sdev);
479 int err = SCSI_DH_OK;
481 if (h->lun == UNINITIALIZED_LUN) {
483 if (err != SCSI_DH_OK)
487 err = check_ownership(sdev);
501 err = initialize_controller(sdev);
502 if (err != SCSI_DH_OK)
506 if (h->ctlr->use_ms10 == -1) {
507 err = set_mode_select(sdev);
508 if (err != SCSI_DH_OK)
512 err = send_mode_select(sdev);
517 static int rdac_prep_fn(struct scsi_device *sdev, struct request *req)
519 struct rdac_dh_data *h = get_rdac_data(sdev);
520 int ret = BLKPREP_OK;
522 if (h->state != RDAC_STATE_ACTIVE) {
524 req->cmd_flags |= REQ_QUIET;
530 static int rdac_check_sense(struct scsi_device *sdev,
531 struct scsi_sense_hdr *sense_hdr)
533 struct rdac_dh_data *h = get_rdac_data(sdev);
534 switch (sense_hdr->sense_key) {
536 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x81)
537 /* LUN Not Ready - Storage firmware incompatible
538 * Manual code synchonisation required.
540 * Nothing we can do here. Try to bypass the path.
543 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0xA1)
544 /* LUN Not Ready - Quiescense in progress
546 * Just retry and wait.
550 case ILLEGAL_REQUEST:
551 if (sense_hdr->asc == 0x94 && sense_hdr->ascq == 0x01) {
552 /* Invalid Request - Current Logical Unit Ownership.
553 * Controller is not the current owner of the LUN,
554 * Fail the path, so that the other path be used.
556 h->state = RDAC_STATE_PASSIVE;
561 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
563 * Power On, Reset, or Bus Device Reset, just retry.
568 /* success just means we do not care what scsi-ml does */
569 return SCSI_RETURN_NOT_HANDLED;
572 static const struct {
575 } rdac_dev_list[] = {
587 {"STK", "OPENstorage D280"},
589 {"SUN", "LCSM100_F"},
593 static int rdac_bus_notify(struct notifier_block *, unsigned long, void *);
595 static struct scsi_device_handler rdac_dh = {
597 .module = THIS_MODULE,
598 .nb.notifier_call = rdac_bus_notify,
599 .prep_fn = rdac_prep_fn,
600 .check_sense = rdac_check_sense,
601 .activate = rdac_activate,
605 * TODO: need some interface so we can set trespass values
607 static int rdac_bus_notify(struct notifier_block *nb,
608 unsigned long action, void *data)
610 struct device *dev = data;
611 struct scsi_device *sdev = to_scsi_device(dev);
612 struct scsi_dh_data *scsi_dh_data;
613 struct rdac_dh_data *h;
617 if (action == BUS_NOTIFY_ADD_DEVICE) {
618 for (i = 0; rdac_dev_list[i].vendor; i++) {
619 if (!strncmp(sdev->vendor, rdac_dev_list[i].vendor,
620 strlen(rdac_dev_list[i].vendor)) &&
621 !strncmp(sdev->model, rdac_dev_list[i].model,
622 strlen(rdac_dev_list[i].model))) {
630 scsi_dh_data = kzalloc(sizeof(struct scsi_device_handler *)
631 + sizeof(*h) , GFP_KERNEL);
633 sdev_printk(KERN_ERR, sdev, "Attach failed %s.\n",
638 scsi_dh_data->scsi_dh = &rdac_dh;
639 h = (struct rdac_dh_data *) scsi_dh_data->buf;
640 h->lun = UNINITIALIZED_LUN;
641 h->state = RDAC_STATE_ACTIVE;
642 spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
643 sdev->scsi_dh_data = scsi_dh_data;
644 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
645 try_module_get(THIS_MODULE);
647 sdev_printk(KERN_NOTICE, sdev, "Attached %s.\n", RDAC_NAME);
649 } else if (action == BUS_NOTIFY_DEL_DEVICE) {
650 if (sdev->scsi_dh_data == NULL ||
651 sdev->scsi_dh_data->scsi_dh != &rdac_dh)
654 spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
655 scsi_dh_data = sdev->scsi_dh_data;
656 sdev->scsi_dh_data = NULL;
657 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
659 h = (struct rdac_dh_data *) scsi_dh_data->buf;
661 kref_put(&h->ctlr->kref, release_controller);
663 module_put(THIS_MODULE);
664 sdev_printk(KERN_NOTICE, sdev, "Dettached %s.\n", RDAC_NAME);
671 static int __init rdac_init(void)
675 r = scsi_register_device_handler(&rdac_dh);
677 printk(KERN_ERR "Failed to register scsi device handler.");
681 static void __exit rdac_exit(void)
683 scsi_unregister_device_handler(&rdac_dh);
686 module_init(rdac_init);
687 module_exit(rdac_exit);
689 MODULE_DESCRIPTION("Multipath LSI/Engenio RDAC driver");
690 MODULE_AUTHOR("Mike Christie, Chandra Seetharaman");
691 MODULE_LICENSE("GPL");