[SCSI] scsi_dh_rdac : Add definitions for different RDAC operating modes
[linux-2.6-block.git] / drivers / scsi / device_handler / scsi_dh_rdac.c
CommitLineData
fbd7ab3e
CS
1/*
2 * Engenio/LSI RDAC SCSI Device Handler
3 *
4 * Copyright (C) 2005 Mike Christie. All rights reserved.
5 * Copyright (C) Chandra Seetharaman, IBM Corp. 2007
6 *
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.
11 *
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.
16 *
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.
20 *
21 */
22#include <scsi/scsi.h>
23#include <scsi/scsi_eh.h>
24#include <scsi/scsi_dh.h>
970f3f47 25#include <linux/workqueue.h>
5a0e3ad6 26#include <linux/slab.h>
fbd7ab3e
CS
27
28#define RDAC_NAME "rdac"
c85f8cb9 29#define RDAC_RETRY_COUNT 5
fbd7ab3e
CS
30
31/*
32 * LSI mode page stuff
33 *
34 * These struct definitions and the forming of the
35 * mode page were taken from the LSI RDAC 2.4 GPL'd
36 * driver, and then converted to Linux conventions.
37 */
38#define RDAC_QUIESCENCE_TIME 20;
39/*
40 * Page Codes
41 */
42#define RDAC_PAGE_CODE_REDUNDANT_CONTROLLER 0x2c
43
44/*
45 * Controller modes definitions
46 */
47#define RDAC_MODE_TRANSFER_SPECIFIED_LUNS 0x02
48
49/*
50 * RDAC Options field
51 */
52#define RDAC_FORCED_QUIESENCE 0x02
53
54#define RDAC_TIMEOUT (60 * HZ)
55#define RDAC_RETRIES 3
56
57struct rdac_mode_6_hdr {
58 u8 data_len;
59 u8 medium_type;
60 u8 device_params;
61 u8 block_desc_len;
62};
63
64struct rdac_mode_10_hdr {
65 u16 data_len;
66 u8 medium_type;
67 u8 device_params;
68 u16 reserved;
69 u16 block_desc_len;
70};
71
72struct rdac_mode_common {
73 u8 controller_serial[16];
74 u8 alt_controller_serial[16];
75 u8 rdac_mode[2];
76 u8 alt_rdac_mode[2];
77 u8 quiescence_timeout;
78 u8 rdac_options;
79};
80
81struct rdac_pg_legacy {
82 struct rdac_mode_6_hdr hdr;
83 u8 page_code;
84 u8 page_len;
85 struct rdac_mode_common common;
86#define MODE6_MAX_LUN 32
87 u8 lun_table[MODE6_MAX_LUN];
88 u8 reserved2[32];
89 u8 reserved3;
90 u8 reserved4;
91};
92
93struct rdac_pg_expanded {
94 struct rdac_mode_10_hdr hdr;
95 u8 page_code;
96 u8 subpage_code;
97 u8 page_len[2];
98 struct rdac_mode_common common;
99 u8 lun_table[256];
100 u8 reserved3;
101 u8 reserved4;
102};
103
104struct c9_inquiry {
105 u8 peripheral_info;
106 u8 page_code; /* 0xC9 */
107 u8 reserved1;
108 u8 page_len;
109 u8 page_id[4]; /* "vace" */
110 u8 avte_cvp;
111 u8 path_prio;
112 u8 reserved2[38];
113};
114
115#define SUBSYS_ID_LEN 16
116#define SLOT_ID_LEN 2
1527666e 117#define ARRAY_LABEL_LEN 31
fbd7ab3e
CS
118
119struct c4_inquiry {
120 u8 peripheral_info;
121 u8 page_code; /* 0xC4 */
122 u8 reserved1;
123 u8 page_len;
124 u8 page_id[4]; /* "subs" */
125 u8 subsys_id[SUBSYS_ID_LEN];
126 u8 revision[4];
127 u8 slot_id[SLOT_ID_LEN];
128 u8 reserved[2];
129};
130
131struct rdac_controller {
132 u8 subsys_id[SUBSYS_ID_LEN];
133 u8 slot_id[SLOT_ID_LEN];
134 int use_ms10;
135 struct kref kref;
136 struct list_head node; /* list of all controllers */
137 union {
138 struct rdac_pg_legacy legacy;
139 struct rdac_pg_expanded expanded;
140 } mode_select;
1527666e
MB
141 u8 index;
142 u8 array_name[ARRAY_LABEL_LEN];
970f3f47
CS
143 spinlock_t ms_lock;
144 int ms_queued;
145 struct work_struct ms_work;
146 struct scsi_device *ms_sdev;
147 struct list_head ms_head;
fbd7ab3e 148};
970f3f47 149
fbd7ab3e
CS
150struct c8_inquiry {
151 u8 peripheral_info;
152 u8 page_code; /* 0xC8 */
153 u8 reserved1;
154 u8 page_len;
155 u8 page_id[4]; /* "edid" */
156 u8 reserved2[3];
157 u8 vol_uniq_id_len;
158 u8 vol_uniq_id[16];
159 u8 vol_user_label_len;
160 u8 vol_user_label[60];
161 u8 array_uniq_id_len;
162 u8 array_unique_id[16];
163 u8 array_user_label_len;
164 u8 array_user_label[60];
165 u8 lun[8];
166};
167
168struct c2_inquiry {
169 u8 peripheral_info;
170 u8 page_code; /* 0xC2 */
171 u8 reserved1;
172 u8 page_len;
173 u8 page_id[4]; /* "swr4" */
174 u8 sw_version[3];
175 u8 sw_date[3];
176 u8 features_enabled;
177 u8 max_lun_supported;
178 u8 partitions[239]; /* Total allocation length should be 0xFF */
179};
180
181struct rdac_dh_data {
182 struct rdac_controller *ctlr;
183#define UNINITIALIZED_LUN (1 << 8)
184 unsigned lun;
eebe9b96
MB
185
186#define RDAC_MODE 0
187#define RDAC_MODE_AVT 1
188#define RDAC_MODE_IOSHIP 2
189 unsigned char mode;
190
fbd7ab3e
CS
191#define RDAC_STATE_ACTIVE 0
192#define RDAC_STATE_PASSIVE 1
193 unsigned char state;
ca9f0089
HR
194
195#define RDAC_LUN_UNOWNED 0
196#define RDAC_LUN_OWNED 1
197#define RDAC_LUN_AVT 2
198 char lun_state;
eebe9b96
MB
199
200#define RDAC_PREFERRED 0
201#define RDAC_NON_PREFERRED 1
202 char preferred;
203
fbd7ab3e
CS
204 unsigned char sense[SCSI_SENSE_BUFFERSIZE];
205 union {
206 struct c2_inquiry c2;
207 struct c4_inquiry c4;
208 struct c8_inquiry c8;
209 struct c9_inquiry c9;
210 } inq;
211};
212
eebe9b96
MB
213static const char *mode[] = {
214 "RDAC",
215 "AVT",
216 "IOSHIP",
217};
ca9f0089
HR
218static const char *lun_state[] =
219{
220 "unowned",
221 "owned",
ca9f0089
HR
222};
223
970f3f47
CS
224struct rdac_queue_data {
225 struct list_head entry;
226 struct rdac_dh_data *h;
227 activate_complete callback_fn;
228 void *callback_data;
229};
230
fbd7ab3e
CS
231static LIST_HEAD(ctlr_list);
232static DEFINE_SPINLOCK(list_lock);
970f3f47
CS
233static struct workqueue_struct *kmpath_rdacd;
234static void send_mode_select(struct work_struct *work);
fbd7ab3e 235
dd784edc
MB
236/*
237 * module parameter to enable rdac debug logging.
238 * 2 bits for each type of logging, only two types defined for now
239 * Can be enhanced if required at later point
240 */
241static int rdac_logging = 1;
242module_param(rdac_logging, int, S_IRUGO|S_IWUSR);
243MODULE_PARM_DESC(rdac_logging, "A bit mask of rdac logging levels, "
244 "Default is 1 - failover logging enabled, "
245 "set it to 0xF to enable all the logs");
246
247#define RDAC_LOG_FAILOVER 0
248#define RDAC_LOG_SENSE 2
249
250#define RDAC_LOG_BITS 2
251
252#define RDAC_LOG_LEVEL(SHIFT) \
253 ((rdac_logging >> (SHIFT)) & ((1 << (RDAC_LOG_BITS)) - 1))
254
255#define RDAC_LOG(SHIFT, sdev, f, arg...) \
256do { \
257 if (unlikely(RDAC_LOG_LEVEL(SHIFT))) \
258 sdev_printk(KERN_INFO, sdev, RDAC_NAME ": " f "\n", ## arg); \
259} while (0);
260
fbd7ab3e
CS
261static inline struct rdac_dh_data *get_rdac_data(struct scsi_device *sdev)
262{
263 struct scsi_dh_data *scsi_dh_data = sdev->scsi_dh_data;
264 BUG_ON(scsi_dh_data == NULL);
265 return ((struct rdac_dh_data *) scsi_dh_data->buf);
266}
267
268static struct request *get_rdac_req(struct scsi_device *sdev,
269 void *buffer, unsigned buflen, int rw)
270{
271 struct request *rq;
272 struct request_queue *q = sdev->request_queue;
fbd7ab3e 273
ca9f0089 274 rq = blk_get_request(q, rw, GFP_NOIO);
fbd7ab3e
CS
275
276 if (!rq) {
277 sdev_printk(KERN_INFO, sdev,
278 "get_rdac_req: blk_get_request failed.\n");
279 return NULL;
280 }
281
ca9f0089 282 if (buflen && blk_rq_map_kern(q, rq, buffer, buflen, GFP_NOIO)) {
fbd7ab3e
CS
283 blk_put_request(rq);
284 sdev_printk(KERN_INFO, sdev,
285 "get_rdac_req: blk_rq_map_kern failed.\n");
286 return NULL;
287 }
288
fbd7ab3e 289 rq->cmd_type = REQ_TYPE_BLOCK_PC;
6000a368
MC
290 rq->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
291 REQ_FAILFAST_DRIVER;
fbd7ab3e
CS
292 rq->retries = RDAC_RETRIES;
293 rq->timeout = RDAC_TIMEOUT;
294
295 return rq;
296}
297
ca9f0089 298static struct request *rdac_failover_get(struct scsi_device *sdev,
04b6e153 299 struct rdac_dh_data *h, struct list_head *list)
fbd7ab3e
CS
300{
301 struct request *rq;
302 struct rdac_mode_common *common;
303 unsigned data_size;
04b6e153
MB
304 struct rdac_queue_data *qdata;
305 u8 *lun_table;
fbd7ab3e
CS
306
307 if (h->ctlr->use_ms10) {
308 struct rdac_pg_expanded *rdac_pg;
309
310 data_size = sizeof(struct rdac_pg_expanded);
311 rdac_pg = &h->ctlr->mode_select.expanded;
312 memset(rdac_pg, 0, data_size);
313 common = &rdac_pg->common;
314 rdac_pg->page_code = RDAC_PAGE_CODE_REDUNDANT_CONTROLLER + 0x40;
315 rdac_pg->subpage_code = 0x1;
316 rdac_pg->page_len[0] = 0x01;
317 rdac_pg->page_len[1] = 0x28;
04b6e153 318 lun_table = rdac_pg->lun_table;
fbd7ab3e
CS
319 } else {
320 struct rdac_pg_legacy *rdac_pg;
321
322 data_size = sizeof(struct rdac_pg_legacy);
323 rdac_pg = &h->ctlr->mode_select.legacy;
324 memset(rdac_pg, 0, data_size);
325 common = &rdac_pg->common;
326 rdac_pg->page_code = RDAC_PAGE_CODE_REDUNDANT_CONTROLLER;
327 rdac_pg->page_len = 0x68;
04b6e153 328 lun_table = rdac_pg->lun_table;
fbd7ab3e
CS
329 }
330 common->rdac_mode[1] = RDAC_MODE_TRANSFER_SPECIFIED_LUNS;
331 common->quiescence_timeout = RDAC_QUIESCENCE_TIME;
332 common->rdac_options = RDAC_FORCED_QUIESENCE;
333
04b6e153
MB
334 list_for_each_entry(qdata, list, entry) {
335 lun_table[qdata->h->lun] = 0x81;
336 }
337
fbd7ab3e
CS
338 /* get request for block layer packet command */
339 rq = get_rdac_req(sdev, &h->ctlr->mode_select, data_size, WRITE);
340 if (!rq)
341 return NULL;
342
343 /* Prepare the command. */
344 if (h->ctlr->use_ms10) {
345 rq->cmd[0] = MODE_SELECT_10;
346 rq->cmd[7] = data_size >> 8;
347 rq->cmd[8] = data_size & 0xff;
348 } else {
349 rq->cmd[0] = MODE_SELECT;
350 rq->cmd[4] = data_size;
351 }
352 rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
353
ca9f0089
HR
354 rq->sense = h->sense;
355 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
356 rq->sense_len = 0;
357
fbd7ab3e
CS
358 return rq;
359}
360
361static void release_controller(struct kref *kref)
362{
363 struct rdac_controller *ctlr;
364 ctlr = container_of(kref, struct rdac_controller, kref);
365
970f3f47 366 flush_workqueue(kmpath_rdacd);
fbd7ab3e
CS
367 spin_lock(&list_lock);
368 list_del(&ctlr->node);
369 spin_unlock(&list_lock);
370 kfree(ctlr);
371}
372
1527666e
MB
373static struct rdac_controller *get_controller(u8 *subsys_id, u8 *slot_id,
374 char *array_name)
fbd7ab3e
CS
375{
376 struct rdac_controller *ctlr, *tmp;
377
378 spin_lock(&list_lock);
379
380 list_for_each_entry(tmp, &ctlr_list, node) {
381 if ((memcmp(tmp->subsys_id, subsys_id, SUBSYS_ID_LEN) == 0) &&
382 (memcmp(tmp->slot_id, slot_id, SLOT_ID_LEN) == 0)) {
383 kref_get(&tmp->kref);
384 spin_unlock(&list_lock);
385 return tmp;
386 }
387 }
388 ctlr = kmalloc(sizeof(*ctlr), GFP_ATOMIC);
389 if (!ctlr)
390 goto done;
391
392 /* initialize fields of controller */
393 memcpy(ctlr->subsys_id, subsys_id, SUBSYS_ID_LEN);
394 memcpy(ctlr->slot_id, slot_id, SLOT_ID_LEN);
1527666e
MB
395 memcpy(ctlr->array_name, array_name, ARRAY_LABEL_LEN);
396
397 /* update the controller index */
398 if (slot_id[1] == 0x31)
399 ctlr->index = 0;
400 else
401 ctlr->index = 1;
402
fbd7ab3e
CS
403 kref_init(&ctlr->kref);
404 ctlr->use_ms10 = -1;
970f3f47
CS
405 ctlr->ms_queued = 0;
406 ctlr->ms_sdev = NULL;
407 spin_lock_init(&ctlr->ms_lock);
408 INIT_WORK(&ctlr->ms_work, send_mode_select);
409 INIT_LIST_HEAD(&ctlr->ms_head);
fbd7ab3e
CS
410 list_add(&ctlr->node, &ctlr_list);
411done:
412 spin_unlock(&list_lock);
413 return ctlr;
414}
415
416static int submit_inquiry(struct scsi_device *sdev, int page_code,
ca9f0089 417 unsigned int len, struct rdac_dh_data *h)
fbd7ab3e
CS
418{
419 struct request *rq;
420 struct request_queue *q = sdev->request_queue;
fbd7ab3e
CS
421 int err = SCSI_DH_RES_TEMP_UNAVAIL;
422
423 rq = get_rdac_req(sdev, &h->inq, len, READ);
424 if (!rq)
425 goto done;
426
427 /* Prepare the command. */
428 rq->cmd[0] = INQUIRY;
429 rq->cmd[1] = 1;
430 rq->cmd[2] = page_code;
431 rq->cmd[4] = len;
432 rq->cmd_len = COMMAND_SIZE(INQUIRY);
ca9f0089
HR
433
434 rq->sense = h->sense;
435 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
436 rq->sense_len = 0;
437
fbd7ab3e
CS
438 err = blk_execute_rq(q, NULL, rq, 1);
439 if (err == -EIO)
440 err = SCSI_DH_IO;
ca9f0089
HR
441
442 blk_put_request(rq);
fbd7ab3e
CS
443done:
444 return err;
445}
446
1527666e
MB
447static int get_lun_info(struct scsi_device *sdev, struct rdac_dh_data *h,
448 char *array_name)
fbd7ab3e 449{
1527666e 450 int err, i;
fbd7ab3e 451 struct c8_inquiry *inqp;
fbd7ab3e 452
ca9f0089 453 err = submit_inquiry(sdev, 0xC8, sizeof(struct c8_inquiry), h);
fbd7ab3e
CS
454 if (err == SCSI_DH_OK) {
455 inqp = &h->inq.c8;
ca9f0089
HR
456 if (inqp->page_code != 0xc8)
457 return SCSI_DH_NOSYS;
458 if (inqp->page_id[0] != 'e' || inqp->page_id[1] != 'd' ||
459 inqp->page_id[2] != 'i' || inqp->page_id[3] != 'd')
460 return SCSI_DH_NOSYS;
8479fca1 461 h->lun = inqp->lun[7]; /* Uses only the last byte */
1527666e
MB
462
463 for(i=0; i<ARRAY_LABEL_LEN-1; ++i)
464 *(array_name+i) = inqp->array_user_label[(2*i)+1];
465
466 *(array_name+ARRAY_LABEL_LEN-1) = '\0';
fbd7ab3e
CS
467 }
468 return err;
469}
470
ca9f0089 471static int check_ownership(struct scsi_device *sdev, struct rdac_dh_data *h)
fbd7ab3e
CS
472{
473 int err;
474 struct c9_inquiry *inqp;
fbd7ab3e 475
fe42625c 476 h->lun_state = RDAC_LUN_UNOWNED;
9eece961 477 h->state = RDAC_STATE_ACTIVE;
ca9f0089 478 err = submit_inquiry(sdev, 0xC9, sizeof(struct c9_inquiry), h);
fbd7ab3e 479 if (err == SCSI_DH_OK) {
fbd7ab3e 480 inqp = &h->inq.c9;
ca9f0089
HR
481 if ((inqp->avte_cvp >> 7) == 0x1) {
482 /* LUN in AVT mode */
483 sdev_printk(KERN_NOTICE, sdev,
484 "%s: AVT mode detected\n",
485 RDAC_NAME);
486 h->lun_state = RDAC_LUN_AVT;
487 } else if ((inqp->avte_cvp & 0x1) != 0) {
488 /* LUN was owned by the controller */
489 h->lun_state = RDAC_LUN_OWNED;
490 }
491 }
492
5a36756b
CS
493 if (h->lun_state == RDAC_LUN_UNOWNED)
494 h->state = RDAC_STATE_PASSIVE;
495
fbd7ab3e
CS
496 return err;
497}
498
ca9f0089 499static int initialize_controller(struct scsi_device *sdev,
1527666e 500 struct rdac_dh_data *h, char *array_name)
fbd7ab3e
CS
501{
502 int err;
503 struct c4_inquiry *inqp;
fbd7ab3e 504
ca9f0089 505 err = submit_inquiry(sdev, 0xC4, sizeof(struct c4_inquiry), h);
fbd7ab3e
CS
506 if (err == SCSI_DH_OK) {
507 inqp = &h->inq.c4;
1527666e
MB
508 h->ctlr = get_controller(inqp->subsys_id, inqp->slot_id,
509 array_name);
fbd7ab3e
CS
510 if (!h->ctlr)
511 err = SCSI_DH_RES_TEMP_UNAVAIL;
512 }
513 return err;
514}
515
ca9f0089 516static int set_mode_select(struct scsi_device *sdev, struct rdac_dh_data *h)
fbd7ab3e
CS
517{
518 int err;
519 struct c2_inquiry *inqp;
fbd7ab3e 520
ca9f0089 521 err = submit_inquiry(sdev, 0xC2, sizeof(struct c2_inquiry), h);
fbd7ab3e
CS
522 if (err == SCSI_DH_OK) {
523 inqp = &h->inq.c2;
524 /*
525 * If more than MODE6_MAX_LUN luns are supported, use
526 * mode select 10
527 */
528 if (inqp->max_lun_supported >= MODE6_MAX_LUN)
529 h->ctlr->use_ms10 = 1;
530 else
531 h->ctlr->use_ms10 = 0;
532 }
533 return err;
534}
535
ca9f0089 536static int mode_select_handle_sense(struct scsi_device *sdev,
970f3f47 537 unsigned char *sensebuf)
fbd7ab3e
CS
538{
539 struct scsi_sense_hdr sense_hdr;
7687fb92 540 int err = SCSI_DH_IO, ret;
dd784edc 541 struct rdac_dh_data *h = get_rdac_data(sdev);
fbd7ab3e 542
ca9f0089 543 ret = scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE, &sense_hdr);
fbd7ab3e
CS
544 if (!ret)
545 goto done;
546
7687fb92
CV
547 switch (sense_hdr.sense_key) {
548 case NO_SENSE:
549 case ABORTED_COMMAND:
550 case UNIT_ATTENTION:
fbd7ab3e 551 err = SCSI_DH_RETRY;
7687fb92
CV
552 break;
553 case NOT_READY:
554 if (sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x01)
555 /* LUN Not Ready and is in the Process of Becoming
556 * Ready
557 */
558 err = SCSI_DH_RETRY;
559 break;
560 case ILLEGAL_REQUEST:
561 if (sense_hdr.asc == 0x91 && sense_hdr.ascq == 0x36)
562 /*
563 * Command Lock contention
564 */
565 err = SCSI_DH_RETRY;
566 break;
567 default:
dd784edc 568 break;
fbd7ab3e
CS
569 }
570
dd784edc
MB
571 RDAC_LOG(RDAC_LOG_FAILOVER, sdev, "array %s, ctlr %d, "
572 "MODE_SELECT returned with sense %02x/%02x/%02x",
573 (char *) h->ctlr->array_name, h->ctlr->index,
574 sense_hdr.sense_key, sense_hdr.asc, sense_hdr.ascq);
575
fbd7ab3e
CS
576done:
577 return err;
578}
579
970f3f47 580static void send_mode_select(struct work_struct *work)
fbd7ab3e 581{
970f3f47
CS
582 struct rdac_controller *ctlr =
583 container_of(work, struct rdac_controller, ms_work);
fbd7ab3e 584 struct request *rq;
970f3f47
CS
585 struct scsi_device *sdev = ctlr->ms_sdev;
586 struct rdac_dh_data *h = get_rdac_data(sdev);
fbd7ab3e 587 struct request_queue *q = sdev->request_queue;
c85f8cb9 588 int err, retry_cnt = RDAC_RETRY_COUNT;
970f3f47
CS
589 struct rdac_queue_data *tmp, *qdata;
590 LIST_HEAD(list);
970f3f47
CS
591
592 spin_lock(&ctlr->ms_lock);
593 list_splice_init(&ctlr->ms_head, &list);
594 ctlr->ms_queued = 0;
595 ctlr->ms_sdev = NULL;
596 spin_unlock(&ctlr->ms_lock);
597
c85f8cb9
CS
598retry:
599 err = SCSI_DH_RES_TEMP_UNAVAIL;
04b6e153 600 rq = rdac_failover_get(sdev, h, &list);
fbd7ab3e
CS
601 if (!rq)
602 goto done;
603
dd784edc
MB
604 RDAC_LOG(RDAC_LOG_FAILOVER, sdev, "array %s, ctlr %d, "
605 "%s MODE_SELECT command",
606 (char *) h->ctlr->array_name, h->ctlr->index,
c85f8cb9 607 (retry_cnt == RDAC_RETRY_COUNT) ? "queueing" : "retrying");
fbd7ab3e
CS
608
609 err = blk_execute_rq(q, NULL, rq, 1);
c85f8cb9
CS
610 blk_put_request(rq);
611 if (err != SCSI_DH_OK) {
ca9f0089 612 err = mode_select_handle_sense(sdev, h->sense);
c85f8cb9
CS
613 if (err == SCSI_DH_RETRY && retry_cnt--)
614 goto retry;
615 }
dd784edc 616 if (err == SCSI_DH_OK) {
fbd7ab3e 617 h->state = RDAC_STATE_ACTIVE;
dd784edc
MB
618 RDAC_LOG(RDAC_LOG_FAILOVER, sdev, "array %s, ctlr %d, "
619 "MODE_SELECT completed",
620 (char *) h->ctlr->array_name, h->ctlr->index);
621 }
ca9f0089 622
fbd7ab3e 623done:
970f3f47
CS
624 list_for_each_entry_safe(qdata, tmp, &list, entry) {
625 list_del(&qdata->entry);
626 if (err == SCSI_DH_OK)
627 qdata->h->state = RDAC_STATE_ACTIVE;
628 if (qdata->callback_fn)
629 qdata->callback_fn(qdata->callback_data, err);
630 kfree(qdata);
631 }
632 return;
633}
634
635static int queue_mode_select(struct scsi_device *sdev,
636 activate_complete fn, void *data)
637{
638 struct rdac_queue_data *qdata;
639 struct rdac_controller *ctlr;
640
641 qdata = kzalloc(sizeof(*qdata), GFP_KERNEL);
642 if (!qdata)
643 return SCSI_DH_RETRY;
644
645 qdata->h = get_rdac_data(sdev);
646 qdata->callback_fn = fn;
647 qdata->callback_data = data;
648
649 ctlr = qdata->h->ctlr;
650 spin_lock(&ctlr->ms_lock);
651 list_add_tail(&qdata->entry, &ctlr->ms_head);
652 if (!ctlr->ms_queued) {
653 ctlr->ms_queued = 1;
654 ctlr->ms_sdev = sdev;
655 queue_work(kmpath_rdacd, &ctlr->ms_work);
656 }
657 spin_unlock(&ctlr->ms_lock);
658 return SCSI_DH_OK;
fbd7ab3e
CS
659}
660
3ae31f6a
CS
661static int rdac_activate(struct scsi_device *sdev,
662 activate_complete fn, void *data)
fbd7ab3e
CS
663{
664 struct rdac_dh_data *h = get_rdac_data(sdev);
665 int err = SCSI_DH_OK;
666
ca9f0089
HR
667 err = check_ownership(sdev, h);
668 if (err != SCSI_DH_OK)
fbd7ab3e 669 goto done;
fbd7ab3e 670
970f3f47
CS
671 if (h->lun_state == RDAC_LUN_UNOWNED) {
672 err = queue_mode_select(sdev, fn, data);
673 if (err == SCSI_DH_OK)
674 return 0;
675 }
fbd7ab3e 676done:
3ae31f6a
CS
677 if (fn)
678 fn(data, err);
679 return 0;
fbd7ab3e
CS
680}
681
682static int rdac_prep_fn(struct scsi_device *sdev, struct request *req)
683{
684 struct rdac_dh_data *h = get_rdac_data(sdev);
685 int ret = BLKPREP_OK;
686
687 if (h->state != RDAC_STATE_ACTIVE) {
688 ret = BLKPREP_KILL;
689 req->cmd_flags |= REQ_QUIET;
690 }
691 return ret;
692
693}
694
695static int rdac_check_sense(struct scsi_device *sdev,
696 struct scsi_sense_hdr *sense_hdr)
697{
698 struct rdac_dh_data *h = get_rdac_data(sdev);
dd784edc
MB
699
700 RDAC_LOG(RDAC_LOG_SENSE, sdev, "array %s, ctlr %d, "
701 "I/O returned with sense %02x/%02x/%02x",
702 (char *) h->ctlr->array_name, h->ctlr->index,
703 sense_hdr->sense_key, sense_hdr->asc, sense_hdr->ascq);
704
fbd7ab3e
CS
705 switch (sense_hdr->sense_key) {
706 case NOT_READY:
8f032263
CV
707 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x01)
708 /* LUN Not Ready - Logical Unit Not Ready and is in
709 * the process of becoming ready
710 * Just retry.
711 */
712 return ADD_TO_MLQUEUE;
fbd7ab3e
CS
713 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x81)
714 /* LUN Not Ready - Storage firmware incompatible
715 * Manual code synchonisation required.
716 *
717 * Nothing we can do here. Try to bypass the path.
718 */
719 return SUCCESS;
720 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0xA1)
721 /* LUN Not Ready - Quiescense in progress
722 *
723 * Just retry and wait.
724 */
c7dbb627 725 return ADD_TO_MLQUEUE;
af50bb99
CV
726 if (sense_hdr->asc == 0xA1 && sense_hdr->ascq == 0x02)
727 /* LUN Not Ready - Quiescense in progress
728 * or has been achieved
729 * Just retry.
730 */
731 return ADD_TO_MLQUEUE;
fbd7ab3e
CS
732 break;
733 case ILLEGAL_REQUEST:
734 if (sense_hdr->asc == 0x94 && sense_hdr->ascq == 0x01) {
735 /* Invalid Request - Current Logical Unit Ownership.
736 * Controller is not the current owner of the LUN,
737 * Fail the path, so that the other path be used.
738 */
739 h->state = RDAC_STATE_PASSIVE;
740 return SUCCESS;
741 }
742 break;
743 case UNIT_ATTENTION:
744 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
745 /*
746 * Power On, Reset, or Bus Device Reset, just retry.
ea41e415
CV
747 */
748 return ADD_TO_MLQUEUE;
749 if (sense_hdr->asc == 0x8b && sense_hdr->ascq == 0x02)
750 /*
751 * Quiescence in progress , just retry.
fbd7ab3e 752 */
c7dbb627 753 return ADD_TO_MLQUEUE;
fbd7ab3e
CS
754 break;
755 }
756 /* success just means we do not care what scsi-ml does */
757 return SCSI_RETURN_NOT_HANDLED;
758}
759
f08c0761 760static const struct scsi_dh_devlist rdac_dev_list[] = {
fbd7ab3e
CS
761 {"IBM", "1722"},
762 {"IBM", "1724"},
763 {"IBM", "1726"},
764 {"IBM", "1742"},
6f4fdda4
MB
765 {"IBM", "1745"},
766 {"IBM", "1746"},
fbd7ab3e
CS
767 {"IBM", "1814"},
768 {"IBM", "1815"},
769 {"IBM", "1818"},
770 {"IBM", "3526"},
771 {"SGI", "TP9400"},
772 {"SGI", "TP9500"},
773 {"SGI", "IS"},
774 {"STK", "OPENstorage D280"},
775 {"SUN", "CSM200_R"},
5bab0885
CB
776 {"SUN", "LCSM100_I"},
777 {"SUN", "LCSM100_S"},
778 {"SUN", "LCSM100_E"},
fbd7ab3e 779 {"SUN", "LCSM100_F"},
650849d7
YD
780 {"DELL", "MD3000"},
781 {"DELL", "MD3000i"},
cdf69bb9
YD
782 {"DELL", "MD32xx"},
783 {"DELL", "MD32xxi"},
2cf75f1c 784 {"DELL", "MD36xxi"},
bc898c97 785 {"DELL", "MD36xxf"},
273c4781
BM
786 {"LSI", "INF-01-00"},
787 {"ENGENIO", "INF-01-00"},
4dbfb544
CS
788 {"STK", "FLEXLINE 380"},
789 {"SUN", "CSM100_R_FC"},
15660676
CV
790 {"SUN", "STK6580_6780"},
791 {"SUN", "SUN_6180"},
fbd7ab3e
CS
792 {NULL, NULL},
793};
794
765cbc6d
HR
795static int rdac_bus_attach(struct scsi_device *sdev);
796static void rdac_bus_detach(struct scsi_device *sdev);
fbd7ab3e
CS
797
798static struct scsi_device_handler rdac_dh = {
799 .name = RDAC_NAME,
800 .module = THIS_MODULE,
765cbc6d 801 .devlist = rdac_dev_list,
fbd7ab3e
CS
802 .prep_fn = rdac_prep_fn,
803 .check_sense = rdac_check_sense,
765cbc6d
HR
804 .attach = rdac_bus_attach,
805 .detach = rdac_bus_detach,
fbd7ab3e
CS
806 .activate = rdac_activate,
807};
808
765cbc6d 809static int rdac_bus_attach(struct scsi_device *sdev)
fbd7ab3e 810{
fbd7ab3e
CS
811 struct scsi_dh_data *scsi_dh_data;
812 struct rdac_dh_data *h;
fbd7ab3e 813 unsigned long flags;
ca9f0089 814 int err;
1527666e 815 char array_name[ARRAY_LABEL_LEN];
fbd7ab3e 816
9dfeb315 817 scsi_dh_data = kzalloc(sizeof(*scsi_dh_data)
765cbc6d
HR
818 + sizeof(*h) , GFP_KERNEL);
819 if (!scsi_dh_data) {
ca9f0089 820 sdev_printk(KERN_ERR, sdev, "%s: Attach failed\n",
765cbc6d 821 RDAC_NAME);
33af79d1 822 return 0;
765cbc6d 823 }
33af79d1 824
765cbc6d
HR
825 scsi_dh_data->scsi_dh = &rdac_dh;
826 h = (struct rdac_dh_data *) scsi_dh_data->buf;
827 h->lun = UNINITIALIZED_LUN;
828 h->state = RDAC_STATE_ACTIVE;
ca9f0089 829
1527666e 830 err = get_lun_info(sdev, h, array_name);
ca9f0089
HR
831 if (err != SCSI_DH_OK)
832 goto failed;
833
1527666e 834 err = initialize_controller(sdev, h, array_name);
ca9f0089
HR
835 if (err != SCSI_DH_OK)
836 goto failed;
837
87b79a53
MB
838 err = check_ownership(sdev, h);
839 if (err != SCSI_DH_OK)
840 goto clean_ctlr;
841
842 err = set_mode_select(sdev, h);
843 if (err != SCSI_DH_OK)
844 goto clean_ctlr;
845
ca9f0089 846 if (!try_module_get(THIS_MODULE))
87b79a53 847 goto clean_ctlr;
ca9f0089 848
765cbc6d
HR
849 spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
850 sdev->scsi_dh_data = scsi_dh_data;
851 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
fbd7ab3e 852
ca9f0089 853 sdev_printk(KERN_NOTICE, sdev,
eebe9b96
MB
854 "%s: LUN %d (%s) (%s)\n",
855 RDAC_NAME, h->lun, mode[(int)h->mode],
856 lun_state[(int)h->lun_state]);
fbd7ab3e 857
fbd7ab3e 858 return 0;
ca9f0089 859
87b79a53
MB
860clean_ctlr:
861 kref_put(&h->ctlr->kref, release_controller);
862
ca9f0089
HR
863failed:
864 kfree(scsi_dh_data);
865 sdev_printk(KERN_ERR, sdev, "%s: not attached\n",
866 RDAC_NAME);
867 return -EINVAL;
fbd7ab3e
CS
868}
869
765cbc6d
HR
870static void rdac_bus_detach( struct scsi_device *sdev )
871{
872 struct scsi_dh_data *scsi_dh_data;
873 struct rdac_dh_data *h;
874 unsigned long flags;
875
876 spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
877 scsi_dh_data = sdev->scsi_dh_data;
878 sdev->scsi_dh_data = NULL;
879 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
880
881 h = (struct rdac_dh_data *) scsi_dh_data->buf;
882 if (h->ctlr)
883 kref_put(&h->ctlr->kref, release_controller);
884 kfree(scsi_dh_data);
885 module_put(THIS_MODULE);
ca9f0089 886 sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", RDAC_NAME);
765cbc6d
HR
887}
888
889
890
fbd7ab3e
CS
891static int __init rdac_init(void)
892{
893 int r;
894
895 r = scsi_register_device_handler(&rdac_dh);
970f3f47 896 if (r != 0) {
fbd7ab3e 897 printk(KERN_ERR "Failed to register scsi device handler.");
970f3f47
CS
898 goto done;
899 }
900
901 /*
902 * Create workqueue to handle mode selects for rdac
903 */
904 kmpath_rdacd = create_singlethread_workqueue("kmpath_rdacd");
905 if (!kmpath_rdacd) {
906 scsi_unregister_device_handler(&rdac_dh);
907 printk(KERN_ERR "kmpath_rdacd creation failed.\n");
908 }
909done:
fbd7ab3e
CS
910 return r;
911}
912
913static void __exit rdac_exit(void)
914{
970f3f47 915 destroy_workqueue(kmpath_rdacd);
fbd7ab3e
CS
916 scsi_unregister_device_handler(&rdac_dh);
917}
918
919module_init(rdac_init);
920module_exit(rdac_exit);
921
922MODULE_DESCRIPTION("Multipath LSI/Engenio RDAC driver");
923MODULE_AUTHOR("Mike Christie, Chandra Seetharaman");
a0b990c6 924MODULE_VERSION("01.00.0000.0000");
fbd7ab3e 925MODULE_LICENSE("GPL");