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