1 /*
2 * LSI/Engenio/NetApp E-Series 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>
25 #include <linux/workqueue.h>
26 #include <linux/slab.h>
27 #include <linux/module.h>
28
29 #define RDAC_NAME "rdac"
30 #define RDAC_RETRY_COUNT 5
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 */
39 #define RDAC_QUIESCENCE_TIME 20
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
58 struct rdac_mode_6_hdr {
59 u8 data_len;
60 u8 medium_type;
61 u8 device_params;
62 u8 block_desc_len;
63 };
64
65 struct 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
73 struct 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
82 struct 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
94 struct 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
105 struct 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
118 #define ARRAY_LABEL_LEN 31
119
120 struct 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
132 #define UNIQUE_ID_LEN 16
133 struct 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;
145 u8 array_unique_id[UNIQUE_ID_LEN];
146 u8 array_user_label_len;
147 u8 array_user_label[60];
148 u8 lun[8];
149 };
150
151 struct 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];
162 struct Scsi_Host *host;
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
170 struct 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
183 struct rdac_dh_data {
184 struct scsi_dh_data dh_data;
185 struct rdac_controller *ctlr;
186 #define UNINITIALIZED_LUN (1 << 8)
187 unsigned lun;
188
189 #define RDAC_MODE 0
190 #define RDAC_MODE_AVT 1
191 #define RDAC_MODE_IOSHIP 2
192 unsigned char mode;
193
194 #define RDAC_STATE_ACTIVE 0
195 #define RDAC_STATE_PASSIVE 1
196 unsigned char state;
197
198 #define RDAC_LUN_UNOWNED 0
199 #define RDAC_LUN_OWNED 1
200 char lun_state;
201
202 #define RDAC_PREFERRED 0
203 #define RDAC_NON_PREFERRED 1
204 char preferred;
205
206 unsigned char sense[SCSI_SENSE_BUFFERSIZE];
207 union {
208 struct c2_inquiry c2;
209 struct c4_inquiry c4;
210 struct c8_inquiry c8;
211 struct c9_inquiry c9;
212 } inq;
213 };
214
215 static const char *mode[] = {
216 "RDAC",
217 "AVT",
218 "IOSHIP",
219 };
220 static const char *lun_state[] =
221 {
222 "unowned",
223 "owned",
224 };
225
226 struct rdac_queue_data {
227 struct list_head entry;
228 struct rdac_dh_data *h;
229 activate_complete callback_fn;
230 void *callback_data;
231 };
232
233 static LIST_HEAD(ctlr_list);
234 static DEFINE_SPINLOCK(list_lock);
235 static struct workqueue_struct *kmpath_rdacd;
236 static void send_mode_select(struct work_struct *work);
237
238 /*
239 * module parameter to enable rdac debug logging.
240 * 2 bits for each type of logging, only two types defined for now
241 * Can be enhanced if required at later point
242 */
243 static int rdac_logging = 1;
244 module_param(rdac_logging, int, S_IRUGO|S_IWUSR);
245 MODULE_PARM_DESC(rdac_logging, "A bit mask of rdac logging levels, "
246 "Default is 1 - failover logging enabled, "
247 "set it to 0xF to enable all the logs");
248
249 #define RDAC_LOG_FAILOVER 0
250 #define RDAC_LOG_SENSE 2
251
252 #define RDAC_LOG_BITS 2
253
254 #define RDAC_LOG_LEVEL(SHIFT) \
255 ((rdac_logging >> (SHIFT)) & ((1 << (RDAC_LOG_BITS)) - 1))
256
257 #define RDAC_LOG(SHIFT, sdev, f, arg...) \
258 do { \
259 if (unlikely(RDAC_LOG_LEVEL(SHIFT))) \
260 sdev_printk(KERN_INFO, sdev, RDAC_NAME ": " f "\n", ## arg); \
261 } while (0);
262
get_rdac_data(struct scsi_device * sdev)263 static inline struct rdac_dh_data *get_rdac_data(struct scsi_device *sdev)
264 {
265 return container_of(sdev->scsi_dh_data, struct rdac_dh_data, dh_data);
266 }
267
get_rdac_req(struct scsi_device * sdev,void * buffer,unsigned buflen,int rw)268 static 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;
273
274 rq = blk_get_request(q, rw, GFP_NOIO);
275
276 if (IS_ERR(rq)) {
277 sdev_printk(KERN_INFO, sdev,
278 "get_rdac_req: blk_get_request failed.\n");
279 return NULL;
280 }
281 blk_rq_set_block_pc(rq);
282
283 if (buflen && blk_rq_map_kern(q, rq, buffer, buflen, GFP_NOIO)) {
284 blk_put_request(rq);
285 sdev_printk(KERN_INFO, sdev,
286 "get_rdac_req: blk_rq_map_kern failed.\n");
287 return NULL;
288 }
289
290 rq->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
291 REQ_FAILFAST_DRIVER;
292 rq->retries = RDAC_RETRIES;
293 rq->timeout = RDAC_TIMEOUT;
294
295 return rq;
296 }
297
rdac_failover_get(struct scsi_device * sdev,struct rdac_dh_data * h,struct list_head * list)298 static struct request *rdac_failover_get(struct scsi_device *sdev,
299 struct rdac_dh_data *h, struct list_head *list)
300 {
301 struct request *rq;
302 struct rdac_mode_common *common;
303 unsigned data_size;
304 struct rdac_queue_data *qdata;
305 u8 *lun_table;
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;
318 lun_table = rdac_pg->lun_table;
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;
328 lun_table = rdac_pg->lun_table;
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
334 list_for_each_entry(qdata, list, entry) {
335 lun_table[qdata->h->lun] = 0x81;
336 }
337
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
354 rq->sense = h->sense;
355 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
356 rq->sense_len = 0;
357
358 return rq;
359 }
360
release_controller(struct kref * kref)361 static void release_controller(struct kref *kref)
362 {
363 struct rdac_controller *ctlr;
364 ctlr = container_of(kref, struct rdac_controller, kref);
365
366 list_del(&ctlr->node);
367 kfree(ctlr);
368 }
369
get_controller(int index,char * array_name,u8 * array_id,struct scsi_device * sdev)370 static struct rdac_controller *get_controller(int index, char *array_name,
371 u8 *array_id, struct scsi_device *sdev)
372 {
373 struct rdac_controller *ctlr, *tmp;
374
375 list_for_each_entry(tmp, &ctlr_list, node) {
376 if ((memcmp(tmp->array_id, array_id, UNIQUE_ID_LEN) == 0) &&
377 (tmp->index == index) &&
378 (tmp->host == sdev->host)) {
379 kref_get(&tmp->kref);
380 return tmp;
381 }
382 }
383 ctlr = kmalloc(sizeof(*ctlr), GFP_ATOMIC);
384 if (!ctlr)
385 return NULL;
386
387 /* initialize fields of controller */
388 memcpy(ctlr->array_id, array_id, UNIQUE_ID_LEN);
389 ctlr->index = index;
390 ctlr->host = sdev->host;
391 memcpy(ctlr->array_name, array_name, ARRAY_LABEL_LEN);
392
393 kref_init(&ctlr->kref);
394 ctlr->use_ms10 = -1;
395 ctlr->ms_queued = 0;
396 ctlr->ms_sdev = NULL;
397 spin_lock_init(&ctlr->ms_lock);
398 INIT_WORK(&ctlr->ms_work, send_mode_select);
399 INIT_LIST_HEAD(&ctlr->ms_head);
400 list_add(&ctlr->node, &ctlr_list);
401
402 return ctlr;
403 }
404
submit_inquiry(struct scsi_device * sdev,int page_code,unsigned int len,struct rdac_dh_data * h)405 static int submit_inquiry(struct scsi_device *sdev, int page_code,
406 unsigned int len, struct rdac_dh_data *h)
407 {
408 struct request *rq;
409 struct request_queue *q = sdev->request_queue;
410 int err = SCSI_DH_RES_TEMP_UNAVAIL;
411
412 rq = get_rdac_req(sdev, &h->inq, len, READ);
413 if (!rq)
414 goto done;
415
416 /* Prepare the command. */
417 rq->cmd[0] = INQUIRY;
418 rq->cmd[1] = 1;
419 rq->cmd[2] = page_code;
420 rq->cmd[4] = len;
421 rq->cmd_len = COMMAND_SIZE(INQUIRY);
422
423 rq->sense = h->sense;
424 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
425 rq->sense_len = 0;
426
427 err = blk_execute_rq(q, NULL, rq, 1);
428 if (err == -EIO)
429 err = SCSI_DH_IO;
430
431 blk_put_request(rq);
432 done:
433 return err;
434 }
435
get_lun_info(struct scsi_device * sdev,struct rdac_dh_data * h,char * array_name,u8 * array_id)436 static int get_lun_info(struct scsi_device *sdev, struct rdac_dh_data *h,
437 char *array_name, u8 *array_id)
438 {
439 int err, i;
440 struct c8_inquiry *inqp;
441
442 err = submit_inquiry(sdev, 0xC8, sizeof(struct c8_inquiry), h);
443 if (err == SCSI_DH_OK) {
444 inqp = &h->inq.c8;
445 if (inqp->page_code != 0xc8)
446 return SCSI_DH_NOSYS;
447 if (inqp->page_id[0] != 'e' || inqp->page_id[1] != 'd' ||
448 inqp->page_id[2] != 'i' || inqp->page_id[3] != 'd')
449 return SCSI_DH_NOSYS;
450 h->lun = inqp->lun[7]; /* Uses only the last byte */
451
452 for(i=0; i<ARRAY_LABEL_LEN-1; ++i)
453 *(array_name+i) = inqp->array_user_label[(2*i)+1];
454
455 *(array_name+ARRAY_LABEL_LEN-1) = '\0';
456 memset(array_id, 0, UNIQUE_ID_LEN);
457 memcpy(array_id, inqp->array_unique_id, inqp->array_uniq_id_len);
458 }
459 return err;
460 }
461
check_ownership(struct scsi_device * sdev,struct rdac_dh_data * h)462 static int check_ownership(struct scsi_device *sdev, struct rdac_dh_data *h)
463 {
464 int err;
465 struct c9_inquiry *inqp;
466
467 h->state = RDAC_STATE_ACTIVE;
468 err = submit_inquiry(sdev, 0xC9, sizeof(struct c9_inquiry), h);
469 if (err == SCSI_DH_OK) {
470 inqp = &h->inq.c9;
471 /* detect the operating mode */
472 if ((inqp->avte_cvp >> 5) & 0x1)
473 h->mode = RDAC_MODE_IOSHIP; /* LUN in IOSHIP mode */
474 else if (inqp->avte_cvp >> 7)
475 h->mode = RDAC_MODE_AVT; /* LUN in AVT mode */
476 else
477 h->mode = RDAC_MODE; /* LUN in RDAC mode */
478
479 /* Update ownership */
480 if (inqp->avte_cvp & 0x1)
481 h->lun_state = RDAC_LUN_OWNED;
482 else {
483 h->lun_state = RDAC_LUN_UNOWNED;
484 if (h->mode == RDAC_MODE)
485 h->state = RDAC_STATE_PASSIVE;
486 }
487
488 /* Update path prio*/
489 if (inqp->path_prio & 0x1)
490 h->preferred = RDAC_PREFERRED;
491 else
492 h->preferred = RDAC_NON_PREFERRED;
493 }
494
495 return err;
496 }
497
initialize_controller(struct scsi_device * sdev,struct rdac_dh_data * h,char * array_name,u8 * array_id)498 static int initialize_controller(struct scsi_device *sdev,
499 struct rdac_dh_data *h, char *array_name, u8 *array_id)
500 {
501 int err, index;
502 struct c4_inquiry *inqp;
503
504 err = submit_inquiry(sdev, 0xC4, sizeof(struct c4_inquiry), h);
505 if (err == SCSI_DH_OK) {
506 inqp = &h->inq.c4;
507 /* get the controller index */
508 if (inqp->slot_id[1] == 0x31)
509 index = 0;
510 else
511 index = 1;
512
513 spin_lock(&list_lock);
514 h->ctlr = get_controller(index, array_name, array_id, sdev);
515 if (!h->ctlr)
516 err = SCSI_DH_RES_TEMP_UNAVAIL;
517 spin_unlock(&list_lock);
518 }
519 return err;
520 }
521
set_mode_select(struct scsi_device * sdev,struct rdac_dh_data * h)522 static int set_mode_select(struct scsi_device *sdev, struct rdac_dh_data *h)
523 {
524 int err;
525 struct c2_inquiry *inqp;
526
527 err = submit_inquiry(sdev, 0xC2, sizeof(struct c2_inquiry), h);
528 if (err == SCSI_DH_OK) {
529 inqp = &h->inq.c2;
530 /*
531 * If more than MODE6_MAX_LUN luns are supported, use
532 * mode select 10
533 */
534 if (inqp->max_lun_supported >= MODE6_MAX_LUN)
535 h->ctlr->use_ms10 = 1;
536 else
537 h->ctlr->use_ms10 = 0;
538 }
539 return err;
540 }
541
mode_select_handle_sense(struct scsi_device * sdev,unsigned char * sensebuf)542 static int mode_select_handle_sense(struct scsi_device *sdev,
543 unsigned char *sensebuf)
544 {
545 struct scsi_sense_hdr sense_hdr;
546 int err = SCSI_DH_IO, ret;
547 struct rdac_dh_data *h = get_rdac_data(sdev);
548
549 ret = scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE, &sense_hdr);
550 if (!ret)
551 goto done;
552
553 switch (sense_hdr.sense_key) {
554 case NO_SENSE:
555 case ABORTED_COMMAND:
556 case UNIT_ATTENTION:
557 err = SCSI_DH_RETRY;
558 break;
559 case NOT_READY:
560 if (sense_hdr.asc == 0x04 && sense_hdr.ascq == 0x01)
561 /* LUN Not Ready and is in the Process of Becoming
562 * Ready
563 */
564 err = SCSI_DH_RETRY;
565 break;
566 case ILLEGAL_REQUEST:
567 if (sense_hdr.asc == 0x91 && sense_hdr.ascq == 0x36)
568 /*
569 * Command Lock contention
570 */
571 err = SCSI_DH_IMM_RETRY;
572 break;
573 default:
574 break;
575 }
576
577 RDAC_LOG(RDAC_LOG_FAILOVER, sdev, "array %s, ctlr %d, "
578 "MODE_SELECT returned with sense %02x/%02x/%02x",
579 (char *) h->ctlr->array_name, h->ctlr->index,
580 sense_hdr.sense_key, sense_hdr.asc, sense_hdr.ascq);
581
582 done:
583 return err;
584 }
585
send_mode_select(struct work_struct * work)586 static void send_mode_select(struct work_struct *work)
587 {
588 struct rdac_controller *ctlr =
589 container_of(work, struct rdac_controller, ms_work);
590 struct request *rq;
591 struct scsi_device *sdev = ctlr->ms_sdev;
592 struct rdac_dh_data *h = get_rdac_data(sdev);
593 struct request_queue *q = sdev->request_queue;
594 int err, retry_cnt = RDAC_RETRY_COUNT;
595 struct rdac_queue_data *tmp, *qdata;
596 LIST_HEAD(list);
597
598 spin_lock(&ctlr->ms_lock);
599 list_splice_init(&ctlr->ms_head, &list);
600 ctlr->ms_queued = 0;
601 ctlr->ms_sdev = NULL;
602 spin_unlock(&ctlr->ms_lock);
603
604 retry:
605 err = SCSI_DH_RES_TEMP_UNAVAIL;
606 rq = rdac_failover_get(sdev, h, &list);
607 if (!rq)
608 goto done;
609
610 RDAC_LOG(RDAC_LOG_FAILOVER, sdev, "array %s, ctlr %d, "
611 "%s MODE_SELECT command",
612 (char *) h->ctlr->array_name, h->ctlr->index,
613 (retry_cnt == RDAC_RETRY_COUNT) ? "queueing" : "retrying");
614
615 err = blk_execute_rq(q, NULL, rq, 1);
616 blk_put_request(rq);
617 if (err != SCSI_DH_OK) {
618 err = mode_select_handle_sense(sdev, h->sense);
619 if (err == SCSI_DH_RETRY && retry_cnt--)
620 goto retry;
621 if (err == SCSI_DH_IMM_RETRY)
622 goto retry;
623 }
624 if (err == SCSI_DH_OK) {
625 h->state = RDAC_STATE_ACTIVE;
626 RDAC_LOG(RDAC_LOG_FAILOVER, sdev, "array %s, ctlr %d, "
627 "MODE_SELECT completed",
628 (char *) h->ctlr->array_name, h->ctlr->index);
629 }
630
631 done:
632 list_for_each_entry_safe(qdata, tmp, &list, entry) {
633 list_del(&qdata->entry);
634 if (err == SCSI_DH_OK)
635 qdata->h->state = RDAC_STATE_ACTIVE;
636 if (qdata->callback_fn)
637 qdata->callback_fn(qdata->callback_data, err);
638 kfree(qdata);
639 }
640 return;
641 }
642
queue_mode_select(struct scsi_device * sdev,activate_complete fn,void * data)643 static int queue_mode_select(struct scsi_device *sdev,
644 activate_complete fn, void *data)
645 {
646 struct rdac_queue_data *qdata;
647 struct rdac_controller *ctlr;
648
649 qdata = kzalloc(sizeof(*qdata), GFP_KERNEL);
650 if (!qdata)
651 return SCSI_DH_RETRY;
652
653 qdata->h = get_rdac_data(sdev);
654 qdata->callback_fn = fn;
655 qdata->callback_data = data;
656
657 ctlr = qdata->h->ctlr;
658 spin_lock(&ctlr->ms_lock);
659 list_add_tail(&qdata->entry, &ctlr->ms_head);
660 if (!ctlr->ms_queued) {
661 ctlr->ms_queued = 1;
662 ctlr->ms_sdev = sdev;
663 queue_work(kmpath_rdacd, &ctlr->ms_work);
664 }
665 spin_unlock(&ctlr->ms_lock);
666 return SCSI_DH_OK;
667 }
668
rdac_activate(struct scsi_device * sdev,activate_complete fn,void * data)669 static int rdac_activate(struct scsi_device *sdev,
670 activate_complete fn, void *data)
671 {
672 struct rdac_dh_data *h = get_rdac_data(sdev);
673 int err = SCSI_DH_OK;
674 int act = 0;
675
676 err = check_ownership(sdev, h);
677 if (err != SCSI_DH_OK)
678 goto done;
679
680 switch (h->mode) {
681 case RDAC_MODE:
682 if (h->lun_state == RDAC_LUN_UNOWNED)
683 act = 1;
684 break;
685 case RDAC_MODE_IOSHIP:
686 if ((h->lun_state == RDAC_LUN_UNOWNED) &&
687 (h->preferred == RDAC_PREFERRED))
688 act = 1;
689 break;
690 default:
691 break;
692 }
693
694 if (act) {
695 err = queue_mode_select(sdev, fn, data);
696 if (err == SCSI_DH_OK)
697 return 0;
698 }
699 done:
700 if (fn)
701 fn(data, err);
702 return 0;
703 }
704
rdac_prep_fn(struct scsi_device * sdev,struct request * req)705 static int rdac_prep_fn(struct scsi_device *sdev, struct request *req)
706 {
707 struct rdac_dh_data *h = get_rdac_data(sdev);
708 int ret = BLKPREP_OK;
709
710 if (h->state != RDAC_STATE_ACTIVE) {
711 ret = BLKPREP_KILL;
712 req->cmd_flags |= REQ_QUIET;
713 }
714 return ret;
715
716 }
717
rdac_check_sense(struct scsi_device * sdev,struct scsi_sense_hdr * sense_hdr)718 static int rdac_check_sense(struct scsi_device *sdev,
719 struct scsi_sense_hdr *sense_hdr)
720 {
721 struct rdac_dh_data *h = get_rdac_data(sdev);
722
723 RDAC_LOG(RDAC_LOG_SENSE, sdev, "array %s, ctlr %d, "
724 "I/O returned with sense %02x/%02x/%02x",
725 (char *) h->ctlr->array_name, h->ctlr->index,
726 sense_hdr->sense_key, sense_hdr->asc, sense_hdr->ascq);
727
728 switch (sense_hdr->sense_key) {
729 case NOT_READY:
730 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x01)
731 /* LUN Not Ready - Logical Unit Not Ready and is in
732 * the process of becoming ready
733 * Just retry.
734 */
735 return ADD_TO_MLQUEUE;
736 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x81)
737 /* LUN Not Ready - Storage firmware incompatible
738 * Manual code synchonisation required.
739 *
740 * Nothing we can do here. Try to bypass the path.
741 */
742 return SUCCESS;
743 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0xA1)
744 /* LUN Not Ready - Quiescense in progress
745 *
746 * Just retry and wait.
747 */
748 return ADD_TO_MLQUEUE;
749 if (sense_hdr->asc == 0xA1 && sense_hdr->ascq == 0x02)
750 /* LUN Not Ready - Quiescense in progress
751 * or has been achieved
752 * Just retry.
753 */
754 return ADD_TO_MLQUEUE;
755 break;
756 case ILLEGAL_REQUEST:
757 if (sense_hdr->asc == 0x94 && sense_hdr->ascq == 0x01) {
758 /* Invalid Request - Current Logical Unit Ownership.
759 * Controller is not the current owner of the LUN,
760 * Fail the path, so that the other path be used.
761 */
762 h->state = RDAC_STATE_PASSIVE;
763 return SUCCESS;
764 }
765 break;
766 case UNIT_ATTENTION:
767 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
768 /*
769 * Power On, Reset, or Bus Device Reset, just retry.
770 */
771 return ADD_TO_MLQUEUE;
772 if (sense_hdr->asc == 0x8b && sense_hdr->ascq == 0x02)
773 /*
774 * Quiescence in progress , just retry.
775 */
776 return ADD_TO_MLQUEUE;
777 break;
778 }
779 /* success just means we do not care what scsi-ml does */
780 return SCSI_RETURN_NOT_HANDLED;
781 }
782
783 static const struct {
784 char *vendor;
785 char *model;
786 } rdac_dev_list[] = {
787 {"IBM", "1722"},
788 {"IBM", "1724"},
789 {"IBM", "1726"},
790 {"IBM", "1742"},
791 {"IBM", "1745"},
792 {"IBM", "1746"},
793 {"IBM", "1813"},
794 {"IBM", "1814"},
795 {"IBM", "1815"},
796 {"IBM", "1818"},
797 {"IBM", "3526"},
798 {"SGI", "TP9"},
799 {"SGI", "IS"},
800 {"STK", "OPENstorage D280"},
801 {"STK", "FLEXLINE 380"},
802 {"SUN", "CSM"},
803 {"SUN", "LCSM100"},
804 {"SUN", "STK6580_6780"},
805 {"SUN", "SUN_6180"},
806 {"SUN", "ArrayStorage"},
807 {"DELL", "MD3"},
808 {"NETAPP", "INF-01-00"},
809 {"LSI", "INF-01-00"},
810 {"ENGENIO", "INF-01-00"},
811 {NULL, NULL},
812 };
813
rdac_match(struct scsi_device * sdev)814 static bool rdac_match(struct scsi_device *sdev)
815 {
816 int i;
817
818 if (scsi_device_tpgs(sdev))
819 return false;
820
821 for (i = 0; rdac_dev_list[i].vendor; i++) {
822 if (!strncmp(sdev->vendor, rdac_dev_list[i].vendor,
823 strlen(rdac_dev_list[i].vendor)) &&
824 !strncmp(sdev->model, rdac_dev_list[i].model,
825 strlen(rdac_dev_list[i].model))) {
826 return true;
827 }
828 }
829 return false;
830 }
831
rdac_bus_attach(struct scsi_device * sdev)832 static struct scsi_dh_data *rdac_bus_attach(struct scsi_device *sdev)
833 {
834 struct rdac_dh_data *h;
835 int err;
836 char array_name[ARRAY_LABEL_LEN];
837 char array_id[UNIQUE_ID_LEN];
838
839 h = kzalloc(sizeof(*h) , GFP_KERNEL);
840 if (!h)
841 return ERR_PTR(-ENOMEM);
842 h->lun = UNINITIALIZED_LUN;
843 h->state = RDAC_STATE_ACTIVE;
844
845 err = get_lun_info(sdev, h, array_name, array_id);
846 if (err != SCSI_DH_OK)
847 goto failed;
848
849 err = initialize_controller(sdev, h, array_name, array_id);
850 if (err != SCSI_DH_OK)
851 goto failed;
852
853 err = check_ownership(sdev, h);
854 if (err != SCSI_DH_OK)
855 goto clean_ctlr;
856
857 err = set_mode_select(sdev, h);
858 if (err != SCSI_DH_OK)
859 goto clean_ctlr;
860
861 sdev_printk(KERN_NOTICE, sdev,
862 "%s: LUN %d (%s) (%s)\n",
863 RDAC_NAME, h->lun, mode[(int)h->mode],
864 lun_state[(int)h->lun_state]);
865
866 return &h->dh_data;
867
868 clean_ctlr:
869 spin_lock(&list_lock);
870 kref_put(&h->ctlr->kref, release_controller);
871 spin_unlock(&list_lock);
872
873 failed:
874 kfree(h);
875 return ERR_PTR(-EINVAL);
876 }
877
rdac_bus_detach(struct scsi_device * sdev)878 static void rdac_bus_detach( struct scsi_device *sdev )
879 {
880 struct rdac_dh_data *h = get_rdac_data(sdev);
881
882 if (h->ctlr && h->ctlr->ms_queued)
883 flush_workqueue(kmpath_rdacd);
884
885 spin_lock(&list_lock);
886 if (h->ctlr)
887 kref_put(&h->ctlr->kref, release_controller);
888 spin_unlock(&list_lock);
889 kfree(h);
890 }
891
892 static struct scsi_device_handler rdac_dh = {
893 .name = RDAC_NAME,
894 .module = THIS_MODULE,
895 .prep_fn = rdac_prep_fn,
896 .check_sense = rdac_check_sense,
897 .attach = rdac_bus_attach,
898 .detach = rdac_bus_detach,
899 .activate = rdac_activate,
900 .match = rdac_match,
901 };
902
rdac_init(void)903 static int __init rdac_init(void)
904 {
905 int r;
906
907 r = scsi_register_device_handler(&rdac_dh);
908 if (r != 0) {
909 printk(KERN_ERR "Failed to register scsi device handler.");
910 goto done;
911 }
912
913 /*
914 * Create workqueue to handle mode selects for rdac
915 */
916 kmpath_rdacd = create_singlethread_workqueue("kmpath_rdacd");
917 if (!kmpath_rdacd) {
918 scsi_unregister_device_handler(&rdac_dh);
919 printk(KERN_ERR "kmpath_rdacd creation failed.\n");
920
921 r = -EINVAL;
922 }
923 done:
924 return r;
925 }
926
rdac_exit(void)927 static void __exit rdac_exit(void)
928 {
929 destroy_workqueue(kmpath_rdacd);
930 scsi_unregister_device_handler(&rdac_dh);
931 }
932
933 module_init(rdac_init);
934 module_exit(rdac_exit);
935
936 MODULE_DESCRIPTION("Multipath LSI/Engenio/NetApp E-Series RDAC driver");
937 MODULE_AUTHOR("Mike Christie, Chandra Seetharaman");
938 MODULE_VERSION("01.00.0000.0000");
939 MODULE_LICENSE("GPL");
940