Merge tag 'scsi-sg' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[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;
1a5dc166 168 struct list_head dh_list;
a53becc9
CS
169};
170
fbd7ab3e
CS
171struct c2_inquiry {
172 u8 peripheral_info;
173 u8 page_code; /* 0xC2 */
174 u8 reserved1;
175 u8 page_len;
176 u8 page_id[4]; /* "swr4" */
177 u8 sw_version[3];
178 u8 sw_date[3];
179 u8 features_enabled;
180 u8 max_lun_supported;
181 u8 partitions[239]; /* Total allocation length should be 0xFF */
182};
183
184struct rdac_dh_data {
1a5dc166 185 struct list_head node;
fbd7ab3e 186 struct rdac_controller *ctlr;
1a5dc166 187 struct scsi_device *sdev;
fbd7ab3e
CS
188#define UNINITIALIZED_LUN (1 << 8)
189 unsigned lun;
eebe9b96
MB
190
191#define RDAC_MODE 0
192#define RDAC_MODE_AVT 1
193#define RDAC_MODE_IOSHIP 2
194 unsigned char mode;
195
fbd7ab3e
CS
196#define RDAC_STATE_ACTIVE 0
197#define RDAC_STATE_PASSIVE 1
198 unsigned char state;
ca9f0089
HR
199
200#define RDAC_LUN_UNOWNED 0
201#define RDAC_LUN_OWNED 1
ca9f0089 202 char lun_state;
eebe9b96
MB
203
204#define RDAC_PREFERRED 0
205#define RDAC_NON_PREFERRED 1
206 char preferred;
207
fbd7ab3e
CS
208 union {
209 struct c2_inquiry c2;
210 struct c4_inquiry c4;
211 struct c8_inquiry c8;
212 struct c9_inquiry c9;
213 } inq;
214};
215
eebe9b96
MB
216static const char *mode[] = {
217 "RDAC",
218 "AVT",
219 "IOSHIP",
220};
ca9f0089
HR
221static const char *lun_state[] =
222{
223 "unowned",
224 "owned",
ca9f0089
HR
225};
226
970f3f47
CS
227struct rdac_queue_data {
228 struct list_head entry;
229 struct rdac_dh_data *h;
230 activate_complete callback_fn;
231 void *callback_data;
232};
233
fbd7ab3e
CS
234static LIST_HEAD(ctlr_list);
235static DEFINE_SPINLOCK(list_lock);
970f3f47
CS
236static struct workqueue_struct *kmpath_rdacd;
237static void send_mode_select(struct work_struct *work);
fbd7ab3e 238
dd784edc
MB
239/*
240 * module parameter to enable rdac debug logging.
241 * 2 bits for each type of logging, only two types defined for now
242 * Can be enhanced if required at later point
243 */
244static int rdac_logging = 1;
245module_param(rdac_logging, int, S_IRUGO|S_IWUSR);
246MODULE_PARM_DESC(rdac_logging, "A bit mask of rdac logging levels, "
247 "Default is 1 - failover logging enabled, "
248 "set it to 0xF to enable all the logs");
249
250#define RDAC_LOG_FAILOVER 0
251#define RDAC_LOG_SENSE 2
252
253#define RDAC_LOG_BITS 2
254
255#define RDAC_LOG_LEVEL(SHIFT) \
256 ((rdac_logging >> (SHIFT)) & ((1 << (RDAC_LOG_BITS)) - 1))
257
258#define RDAC_LOG(SHIFT, sdev, f, arg...) \
259do { \
260 if (unlikely(RDAC_LOG_LEVEL(SHIFT))) \
261 sdev_printk(KERN_INFO, sdev, RDAC_NAME ": " f "\n", ## arg); \
262} while (0);
263
32782557
HR
264static unsigned int rdac_failover_get(struct rdac_controller *ctlr,
265 struct list_head *list,
266 unsigned char *cdb)
fbd7ab3e 267{
fbd7ab3e
CS
268 struct rdac_mode_common *common;
269 unsigned data_size;
04b6e153
MB
270 struct rdac_queue_data *qdata;
271 u8 *lun_table;
fbd7ab3e 272
0648a07c 273 if (ctlr->use_ms10) {
fbd7ab3e
CS
274 struct rdac_pg_expanded *rdac_pg;
275
276 data_size = sizeof(struct rdac_pg_expanded);
0648a07c 277 rdac_pg = &ctlr->mode_select.expanded;
fbd7ab3e
CS
278 memset(rdac_pg, 0, data_size);
279 common = &rdac_pg->common;
280 rdac_pg->page_code = RDAC_PAGE_CODE_REDUNDANT_CONTROLLER + 0x40;
281 rdac_pg->subpage_code = 0x1;
282 rdac_pg->page_len[0] = 0x01;
283 rdac_pg->page_len[1] = 0x28;
04b6e153 284 lun_table = rdac_pg->lun_table;
fbd7ab3e
CS
285 } else {
286 struct rdac_pg_legacy *rdac_pg;
287
288 data_size = sizeof(struct rdac_pg_legacy);
0648a07c 289 rdac_pg = &ctlr->mode_select.legacy;
fbd7ab3e
CS
290 memset(rdac_pg, 0, data_size);
291 common = &rdac_pg->common;
292 rdac_pg->page_code = RDAC_PAGE_CODE_REDUNDANT_CONTROLLER;
293 rdac_pg->page_len = 0x68;
04b6e153 294 lun_table = rdac_pg->lun_table;
fbd7ab3e
CS
295 }
296 common->rdac_mode[1] = RDAC_MODE_TRANSFER_SPECIFIED_LUNS;
297 common->quiescence_timeout = RDAC_QUIESCENCE_TIME;
298 common->rdac_options = RDAC_FORCED_QUIESENCE;
299
04b6e153
MB
300 list_for_each_entry(qdata, list, entry) {
301 lun_table[qdata->h->lun] = 0x81;
302 }
303
fbd7ab3e 304 /* Prepare the command. */
0648a07c 305 if (ctlr->use_ms10) {
32782557
HR
306 cdb[0] = MODE_SELECT_10;
307 cdb[7] = data_size >> 8;
308 cdb[8] = data_size & 0xff;
fbd7ab3e 309 } else {
32782557
HR
310 cdb[0] = MODE_SELECT;
311 cdb[4] = data_size;
fbd7ab3e 312 }
fbd7ab3e 313
32782557 314 return data_size;
fbd7ab3e
CS
315}
316
317static void release_controller(struct kref *kref)
318{
319 struct rdac_controller *ctlr;
320 ctlr = container_of(kref, struct rdac_controller, kref);
321
fbd7ab3e 322 list_del(&ctlr->node);
fbd7ab3e
CS
323 kfree(ctlr);
324}
325
a53becc9 326static struct rdac_controller *get_controller(int index, char *array_name,
d6857595 327 u8 *array_id, struct scsi_device *sdev)
fbd7ab3e
CS
328{
329 struct rdac_controller *ctlr, *tmp;
330
fbd7ab3e 331 list_for_each_entry(tmp, &ctlr_list, node) {
a53becc9 332 if ((memcmp(tmp->array_id, array_id, UNIQUE_ID_LEN) == 0) &&
d6857595
CS
333 (tmp->index == index) &&
334 (tmp->host == sdev->host)) {
fbd7ab3e 335 kref_get(&tmp->kref);
fbd7ab3e
CS
336 return tmp;
337 }
338 }
339 ctlr = kmalloc(sizeof(*ctlr), GFP_ATOMIC);
340 if (!ctlr)
3569e537 341 return NULL;
fbd7ab3e
CS
342
343 /* initialize fields of controller */
a53becc9
CS
344 memcpy(ctlr->array_id, array_id, UNIQUE_ID_LEN);
345 ctlr->index = index;
d6857595 346 ctlr->host = sdev->host;
1527666e
MB
347 memcpy(ctlr->array_name, array_name, ARRAY_LABEL_LEN);
348
fbd7ab3e
CS
349 kref_init(&ctlr->kref);
350 ctlr->use_ms10 = -1;
970f3f47
CS
351 ctlr->ms_queued = 0;
352 ctlr->ms_sdev = NULL;
353 spin_lock_init(&ctlr->ms_lock);
354 INIT_WORK(&ctlr->ms_work, send_mode_select);
355 INIT_LIST_HEAD(&ctlr->ms_head);
fbd7ab3e 356 list_add(&ctlr->node, &ctlr_list);
1a5dc166 357 INIT_LIST_HEAD(&ctlr->dh_list);
3569e537 358
fbd7ab3e
CS
359 return ctlr;
360}
361
1527666e 362static int get_lun_info(struct scsi_device *sdev, struct rdac_dh_data *h,
a53becc9 363 char *array_name, u8 *array_id)
fbd7ab3e 364{
32782557
HR
365 int err = SCSI_DH_IO, i;
366 struct c8_inquiry *inqp = &h->inq.c8;
fbd7ab3e 367
32782557
HR
368 if (!scsi_get_vpd_page(sdev, 0xC8, (unsigned char *)inqp,
369 sizeof(struct c8_inquiry))) {
ca9f0089
HR
370 if (inqp->page_code != 0xc8)
371 return SCSI_DH_NOSYS;
372 if (inqp->page_id[0] != 'e' || inqp->page_id[1] != 'd' ||
373 inqp->page_id[2] != 'i' || inqp->page_id[3] != 'd')
374 return SCSI_DH_NOSYS;
8479fca1 375 h->lun = inqp->lun[7]; /* Uses only the last byte */
1527666e
MB
376
377 for(i=0; i<ARRAY_LABEL_LEN-1; ++i)
378 *(array_name+i) = inqp->array_user_label[(2*i)+1];
379
380 *(array_name+ARRAY_LABEL_LEN-1) = '\0';
a53becc9
CS
381 memset(array_id, 0, UNIQUE_ID_LEN);
382 memcpy(array_id, inqp->array_unique_id, inqp->array_uniq_id_len);
32782557 383 err = SCSI_DH_OK;
fbd7ab3e
CS
384 }
385 return err;
386}
387
ca9f0089 388static int check_ownership(struct scsi_device *sdev, struct rdac_dh_data *h)
fbd7ab3e 389{
32782557 390 int err = SCSI_DH_IO, access_state;
1a5dc166 391 struct rdac_dh_data *tmp;
32782557 392 struct c9_inquiry *inqp = &h->inq.c9;
fbd7ab3e 393
9eece961 394 h->state = RDAC_STATE_ACTIVE;
32782557
HR
395 if (!scsi_get_vpd_page(sdev, 0xC9, (unsigned char *)inqp,
396 sizeof(struct c9_inquiry))) {
1c3afc42
MB
397 /* detect the operating mode */
398 if ((inqp->avte_cvp >> 5) & 0x1)
399 h->mode = RDAC_MODE_IOSHIP; /* LUN in IOSHIP mode */
400 else if (inqp->avte_cvp >> 7)
401 h->mode = RDAC_MODE_AVT; /* LUN in AVT mode */
402 else
403 h->mode = RDAC_MODE; /* LUN in RDAC mode */
404
405 /* Update ownership */
1a5dc166 406 if (inqp->avte_cvp & 0x1) {
ca9f0089 407 h->lun_state = RDAC_LUN_OWNED;
1a5dc166
HR
408 access_state = SCSI_ACCESS_STATE_OPTIMAL;
409 } else {
1c3afc42 410 h->lun_state = RDAC_LUN_UNOWNED;
1a5dc166 411 if (h->mode == RDAC_MODE) {
1c3afc42 412 h->state = RDAC_STATE_PASSIVE;
1a5dc166
HR
413 access_state = SCSI_ACCESS_STATE_STANDBY;
414 } else
415 access_state = SCSI_ACCESS_STATE_ACTIVE;
ca9f0089 416 }
ca9f0089 417
1c3afc42 418 /* Update path prio*/
1a5dc166 419 if (inqp->path_prio & 0x1) {
1c3afc42 420 h->preferred = RDAC_PREFERRED;
1a5dc166
HR
421 access_state |= SCSI_ACCESS_STATE_PREFERRED;
422 } else
1c3afc42 423 h->preferred = RDAC_NON_PREFERRED;
1a5dc166
HR
424 rcu_read_lock();
425 list_for_each_entry_rcu(tmp, &h->ctlr->dh_list, node) {
426 /* h->sdev should always be valid */
427 BUG_ON(!tmp->sdev);
428 tmp->sdev->access_state = access_state;
429 }
430 rcu_read_unlock();
32782557 431 err = SCSI_DH_OK;
1c3afc42 432 }
5a36756b 433
fbd7ab3e
CS
434 return err;
435}
436
ca9f0089 437static int initialize_controller(struct scsi_device *sdev,
a53becc9 438 struct rdac_dh_data *h, char *array_name, u8 *array_id)
fbd7ab3e 439{
32782557
HR
440 int err = SCSI_DH_IO, index;
441 struct c4_inquiry *inqp = &h->inq.c4;
fbd7ab3e 442
32782557
HR
443 if (!scsi_get_vpd_page(sdev, 0xC4, (unsigned char *)inqp,
444 sizeof(struct c4_inquiry))) {
a53becc9
CS
445 /* get the controller index */
446 if (inqp->slot_id[1] == 0x31)
447 index = 0;
448 else
449 index = 1;
3569e537
MB
450
451 spin_lock(&list_lock);
d6857595 452 h->ctlr = get_controller(index, array_name, array_id, sdev);
fbd7ab3e
CS
453 if (!h->ctlr)
454 err = SCSI_DH_RES_TEMP_UNAVAIL;
1a5dc166
HR
455 else {
456 list_add_rcu(&h->node, &h->ctlr->dh_list);
457 h->sdev = sdev;
458 }
3569e537 459 spin_unlock(&list_lock);
32782557 460 err = SCSI_DH_OK;
fbd7ab3e
CS
461 }
462 return err;
463}
464
ca9f0089 465static int set_mode_select(struct scsi_device *sdev, struct rdac_dh_data *h)
fbd7ab3e 466{
32782557
HR
467 int err = SCSI_DH_IO;
468 struct c2_inquiry *inqp = &h->inq.c2;
fbd7ab3e 469
32782557
HR
470 if (!scsi_get_vpd_page(sdev, 0xC2, (unsigned char *)inqp,
471 sizeof(struct c2_inquiry))) {
fbd7ab3e
CS
472 /*
473 * If more than MODE6_MAX_LUN luns are supported, use
474 * mode select 10
475 */
476 if (inqp->max_lun_supported >= MODE6_MAX_LUN)
477 h->ctlr->use_ms10 = 1;
478 else
479 h->ctlr->use_ms10 = 0;
32782557 480 err = SCSI_DH_OK;
fbd7ab3e
CS
481 }
482 return err;
483}
484
ca9f0089 485static int mode_select_handle_sense(struct scsi_device *sdev,
32782557 486 struct scsi_sense_hdr *sense_hdr)
fbd7ab3e 487{
32782557 488 int err = SCSI_DH_IO;
ee14c674 489 struct rdac_dh_data *h = sdev->handler_data;
fbd7ab3e 490
32782557 491 if (!scsi_sense_valid(sense_hdr))
fbd7ab3e
CS
492 goto done;
493
32782557 494 switch (sense_hdr->sense_key) {
7687fb92
CV
495 case NO_SENSE:
496 case ABORTED_COMMAND:
497 case UNIT_ATTENTION:
fbd7ab3e 498 err = SCSI_DH_RETRY;
7687fb92
CV
499 break;
500 case NOT_READY:
32782557 501 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x01)
7687fb92
CV
502 /* LUN Not Ready and is in the Process of Becoming
503 * Ready
504 */
505 err = SCSI_DH_RETRY;
506 break;
507 case ILLEGAL_REQUEST:
32782557 508 if (sense_hdr->asc == 0x91 && sense_hdr->ascq == 0x36)
7687fb92
CV
509 /*
510 * Command Lock contention
511 */
d2d06d4f 512 err = SCSI_DH_IMM_RETRY;
7687fb92
CV
513 break;
514 default:
dd784edc 515 break;
fbd7ab3e
CS
516 }
517
dd784edc
MB
518 RDAC_LOG(RDAC_LOG_FAILOVER, sdev, "array %s, ctlr %d, "
519 "MODE_SELECT returned with sense %02x/%02x/%02x",
520 (char *) h->ctlr->array_name, h->ctlr->index,
32782557 521 sense_hdr->sense_key, sense_hdr->asc, sense_hdr->ascq);
dd784edc 522
fbd7ab3e
CS
523done:
524 return err;
525}
526
970f3f47 527static void send_mode_select(struct work_struct *work)
fbd7ab3e 528{
970f3f47
CS
529 struct rdac_controller *ctlr =
530 container_of(work, struct rdac_controller, ms_work);
970f3f47 531 struct scsi_device *sdev = ctlr->ms_sdev;
ee14c674 532 struct rdac_dh_data *h = sdev->handler_data;
32782557 533 int err = SCSI_DH_OK, retry_cnt = RDAC_RETRY_COUNT;
970f3f47
CS
534 struct rdac_queue_data *tmp, *qdata;
535 LIST_HEAD(list);
92eb5062 536 unsigned char cdb[MAX_COMMAND_SIZE];
32782557
HR
537 struct scsi_sense_hdr sshdr;
538 unsigned int data_size;
539 u64 req_flags = REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
540 REQ_FAILFAST_DRIVER;
970f3f47
CS
541
542 spin_lock(&ctlr->ms_lock);
543 list_splice_init(&ctlr->ms_head, &list);
544 ctlr->ms_queued = 0;
545 ctlr->ms_sdev = NULL;
546 spin_unlock(&ctlr->ms_lock);
547
32782557
HR
548 retry:
549 data_size = rdac_failover_get(ctlr, &list, cdb);
fbd7ab3e 550
dd784edc
MB
551 RDAC_LOG(RDAC_LOG_FAILOVER, sdev, "array %s, ctlr %d, "
552 "%s MODE_SELECT command",
553 (char *) h->ctlr->array_name, h->ctlr->index,
c85f8cb9 554 (retry_cnt == RDAC_RETRY_COUNT) ? "queueing" : "retrying");
fbd7ab3e 555
fcbfffe2
CH
556 if (scsi_execute(sdev, cdb, DMA_TO_DEVICE, &h->ctlr->mode_select,
557 data_size, NULL, &sshdr, RDAC_TIMEOUT * HZ,
558 RDAC_RETRIES, req_flags, 0, NULL)) {
32782557 559 err = mode_select_handle_sense(sdev, &sshdr);
c85f8cb9
CS
560 if (err == SCSI_DH_RETRY && retry_cnt--)
561 goto retry;
d2d06d4f
HR
562 if (err == SCSI_DH_IMM_RETRY)
563 goto retry;
c85f8cb9 564 }
dd784edc 565 if (err == SCSI_DH_OK) {
fbd7ab3e 566 h->state = RDAC_STATE_ACTIVE;
dd784edc
MB
567 RDAC_LOG(RDAC_LOG_FAILOVER, sdev, "array %s, ctlr %d, "
568 "MODE_SELECT completed",
569 (char *) h->ctlr->array_name, h->ctlr->index);
570 }
ca9f0089 571
970f3f47
CS
572 list_for_each_entry_safe(qdata, tmp, &list, entry) {
573 list_del(&qdata->entry);
574 if (err == SCSI_DH_OK)
575 qdata->h->state = RDAC_STATE_ACTIVE;
576 if (qdata->callback_fn)
577 qdata->callback_fn(qdata->callback_data, err);
578 kfree(qdata);
579 }
580 return;
581}
582
583static int queue_mode_select(struct scsi_device *sdev,
584 activate_complete fn, void *data)
585{
586 struct rdac_queue_data *qdata;
587 struct rdac_controller *ctlr;
588
589 qdata = kzalloc(sizeof(*qdata), GFP_KERNEL);
590 if (!qdata)
591 return SCSI_DH_RETRY;
592
ee14c674 593 qdata->h = sdev->handler_data;
970f3f47
CS
594 qdata->callback_fn = fn;
595 qdata->callback_data = data;
596
597 ctlr = qdata->h->ctlr;
598 spin_lock(&ctlr->ms_lock);
599 list_add_tail(&qdata->entry, &ctlr->ms_head);
600 if (!ctlr->ms_queued) {
601 ctlr->ms_queued = 1;
602 ctlr->ms_sdev = sdev;
603 queue_work(kmpath_rdacd, &ctlr->ms_work);
604 }
605 spin_unlock(&ctlr->ms_lock);
606 return SCSI_DH_OK;
fbd7ab3e
CS
607}
608
3ae31f6a
CS
609static int rdac_activate(struct scsi_device *sdev,
610 activate_complete fn, void *data)
fbd7ab3e 611{
ee14c674 612 struct rdac_dh_data *h = sdev->handler_data;
fbd7ab3e 613 int err = SCSI_DH_OK;
3425fbfe 614 int act = 0;
fbd7ab3e 615
ca9f0089
HR
616 err = check_ownership(sdev, h);
617 if (err != SCSI_DH_OK)
fbd7ab3e 618 goto done;
fbd7ab3e 619
3425fbfe
MB
620 switch (h->mode) {
621 case RDAC_MODE:
622 if (h->lun_state == RDAC_LUN_UNOWNED)
623 act = 1;
624 break;
625 case RDAC_MODE_IOSHIP:
626 if ((h->lun_state == RDAC_LUN_UNOWNED) &&
627 (h->preferred == RDAC_PREFERRED))
628 act = 1;
629 break;
630 default:
631 break;
632 }
633
634 if (act) {
970f3f47
CS
635 err = queue_mode_select(sdev, fn, data);
636 if (err == SCSI_DH_OK)
637 return 0;
638 }
fbd7ab3e 639done:
3ae31f6a
CS
640 if (fn)
641 fn(data, err);
642 return 0;
fbd7ab3e
CS
643}
644
4c1cb67c 645static blk_status_t rdac_prep_fn(struct scsi_device *sdev, struct request *req)
fbd7ab3e 646{
ee14c674 647 struct rdac_dh_data *h = sdev->handler_data;
fbd7ab3e
CS
648
649 if (h->state != RDAC_STATE_ACTIVE) {
e8064021 650 req->rq_flags |= RQF_QUIET;
4c1cb67c 651 return BLK_STS_IOERR;
fbd7ab3e 652 }
fbd7ab3e 653
4c1cb67c 654 return BLK_STS_OK;
fbd7ab3e
CS
655}
656
657static int rdac_check_sense(struct scsi_device *sdev,
658 struct scsi_sense_hdr *sense_hdr)
659{
ee14c674 660 struct rdac_dh_data *h = sdev->handler_data;
dd784edc
MB
661
662 RDAC_LOG(RDAC_LOG_SENSE, sdev, "array %s, ctlr %d, "
663 "I/O returned with sense %02x/%02x/%02x",
664 (char *) h->ctlr->array_name, h->ctlr->index,
665 sense_hdr->sense_key, sense_hdr->asc, sense_hdr->ascq);
666
fbd7ab3e
CS
667 switch (sense_hdr->sense_key) {
668 case NOT_READY:
8f032263
CV
669 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x01)
670 /* LUN Not Ready - Logical Unit Not Ready and is in
671 * the process of becoming ready
672 * Just retry.
673 */
674 return ADD_TO_MLQUEUE;
fbd7ab3e
CS
675 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x81)
676 /* LUN Not Ready - Storage firmware incompatible
677 * Manual code synchonisation required.
678 *
679 * Nothing we can do here. Try to bypass the path.
680 */
681 return SUCCESS;
682 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0xA1)
683 /* LUN Not Ready - Quiescense in progress
684 *
685 * Just retry and wait.
686 */
c7dbb627 687 return ADD_TO_MLQUEUE;
af50bb99
CV
688 if (sense_hdr->asc == 0xA1 && sense_hdr->ascq == 0x02)
689 /* LUN Not Ready - Quiescense in progress
690 * or has been achieved
691 * Just retry.
692 */
693 return ADD_TO_MLQUEUE;
fbd7ab3e
CS
694 break;
695 case ILLEGAL_REQUEST:
696 if (sense_hdr->asc == 0x94 && sense_hdr->ascq == 0x01) {
697 /* Invalid Request - Current Logical Unit Ownership.
698 * Controller is not the current owner of the LUN,
699 * Fail the path, so that the other path be used.
700 */
701 h->state = RDAC_STATE_PASSIVE;
702 return SUCCESS;
703 }
704 break;
705 case UNIT_ATTENTION:
706 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
707 /*
708 * Power On, Reset, or Bus Device Reset, just retry.
ea41e415
CV
709 */
710 return ADD_TO_MLQUEUE;
711 if (sense_hdr->asc == 0x8b && sense_hdr->ascq == 0x02)
712 /*
713 * Quiescence in progress , just retry.
fbd7ab3e 714 */
c7dbb627 715 return ADD_TO_MLQUEUE;
fbd7ab3e
CS
716 break;
717 }
718 /* success just means we do not care what scsi-ml does */
719 return SCSI_RETURN_NOT_HANDLED;
720}
721
ee14c674 722static int rdac_bus_attach(struct scsi_device *sdev)
fbd7ab3e 723{
fbd7ab3e 724 struct rdac_dh_data *h;
ca9f0089 725 int err;
1527666e 726 char array_name[ARRAY_LABEL_LEN];
a53becc9 727 char array_id[UNIQUE_ID_LEN];
fbd7ab3e 728
cd37743f 729 h = kzalloc(sizeof(*h) , GFP_KERNEL);
1d520328 730 if (!h)
2a8f7a03 731 return SCSI_DH_NOMEM;
765cbc6d
HR
732 h->lun = UNINITIALIZED_LUN;
733 h->state = RDAC_STATE_ACTIVE;
ca9f0089 734
a53becc9 735 err = get_lun_info(sdev, h, array_name, array_id);
ca9f0089
HR
736 if (err != SCSI_DH_OK)
737 goto failed;
738
a53becc9 739 err = initialize_controller(sdev, h, array_name, array_id);
ca9f0089
HR
740 if (err != SCSI_DH_OK)
741 goto failed;
742
87b79a53
MB
743 err = check_ownership(sdev, h);
744 if (err != SCSI_DH_OK)
745 goto clean_ctlr;
746
747 err = set_mode_select(sdev, h);
748 if (err != SCSI_DH_OK)
749 goto clean_ctlr;
750
ca9f0089 751 sdev_printk(KERN_NOTICE, sdev,
eebe9b96
MB
752 "%s: LUN %d (%s) (%s)\n",
753 RDAC_NAME, h->lun, mode[(int)h->mode],
754 lun_state[(int)h->lun_state]);
fbd7ab3e 755
ee14c674 756 sdev->handler_data = h;
2a8f7a03 757 return SCSI_DH_OK;
ca9f0089 758
87b79a53 759clean_ctlr:
3569e537 760 spin_lock(&list_lock);
87b79a53 761 kref_put(&h->ctlr->kref, release_controller);
3569e537 762 spin_unlock(&list_lock);
87b79a53 763
ca9f0089 764failed:
cd37743f 765 kfree(h);
2a8f7a03 766 return err;
fbd7ab3e
CS
767}
768
765cbc6d
HR
769static void rdac_bus_detach( struct scsi_device *sdev )
770{
ee14c674 771 struct rdac_dh_data *h = sdev->handler_data;
765cbc6d 772
3569e537
MB
773 if (h->ctlr && h->ctlr->ms_queued)
774 flush_workqueue(kmpath_rdacd);
775
3569e537 776 spin_lock(&list_lock);
1a5dc166
HR
777 if (h->ctlr) {
778 list_del_rcu(&h->node);
779 h->sdev = NULL;
765cbc6d 780 kref_put(&h->ctlr->kref, release_controller);
1a5dc166 781 }
3569e537 782 spin_unlock(&list_lock);
ee14c674 783 sdev->handler_data = NULL;
cd37743f 784 kfree(h);
765cbc6d
HR
785}
786
1d520328
CH
787static struct scsi_device_handler rdac_dh = {
788 .name = RDAC_NAME,
789 .module = THIS_MODULE,
790 .prep_fn = rdac_prep_fn,
791 .check_sense = rdac_check_sense,
792 .attach = rdac_bus_attach,
793 .detach = rdac_bus_detach,
794 .activate = rdac_activate,
1d520328 795};
765cbc6d 796
fbd7ab3e
CS
797static int __init rdac_init(void)
798{
799 int r;
800
801 r = scsi_register_device_handler(&rdac_dh);
970f3f47 802 if (r != 0) {
fbd7ab3e 803 printk(KERN_ERR "Failed to register scsi device handler.");
970f3f47
CS
804 goto done;
805 }
806
807 /*
808 * Create workqueue to handle mode selects for rdac
809 */
810 kmpath_rdacd = create_singlethread_workqueue("kmpath_rdacd");
811 if (!kmpath_rdacd) {
812 scsi_unregister_device_handler(&rdac_dh);
813 printk(KERN_ERR "kmpath_rdacd creation failed.\n");
9fc397fc
RW
814
815 r = -EINVAL;
970f3f47
CS
816 }
817done:
fbd7ab3e
CS
818 return r;
819}
820
821static void __exit rdac_exit(void)
822{
970f3f47 823 destroy_workqueue(kmpath_rdacd);
fbd7ab3e
CS
824 scsi_unregister_device_handler(&rdac_dh);
825}
826
827module_init(rdac_init);
828module_exit(rdac_exit);
829
5f7a6433 830MODULE_DESCRIPTION("Multipath LSI/Engenio/NetApp E-Series RDAC driver");
fbd7ab3e 831MODULE_AUTHOR("Mike Christie, Chandra Seetharaman");
a0b990c6 832MODULE_VERSION("01.00.0000.0000");
fbd7ab3e 833MODULE_LICENSE("GPL");