Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / drivers / scsi / device_handler / scsi_dh_rdac.c
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
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
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
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
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
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
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
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
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
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
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
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_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
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         }
622         if (err == SCSI_DH_OK) {
623                 h->state = RDAC_STATE_ACTIVE;
624                 RDAC_LOG(RDAC_LOG_FAILOVER, sdev, "array %s, ctlr %d, "
625                                 "MODE_SELECT completed",
626                                 (char *) h->ctlr->array_name, h->ctlr->index);
627         }
628
629 done:
630         list_for_each_entry_safe(qdata, tmp, &list, entry) {
631                 list_del(&qdata->entry);
632                 if (err == SCSI_DH_OK)
633                         qdata->h->state = RDAC_STATE_ACTIVE;
634                 if (qdata->callback_fn)
635                         qdata->callback_fn(qdata->callback_data, err);
636                 kfree(qdata);
637         }
638         return;
639 }
640
641 static int queue_mode_select(struct scsi_device *sdev,
642                                 activate_complete fn, void *data)
643 {
644         struct rdac_queue_data *qdata;
645         struct rdac_controller *ctlr;
646
647         qdata = kzalloc(sizeof(*qdata), GFP_KERNEL);
648         if (!qdata)
649                 return SCSI_DH_RETRY;
650
651         qdata->h = get_rdac_data(sdev);
652         qdata->callback_fn = fn;
653         qdata->callback_data = data;
654
655         ctlr = qdata->h->ctlr;
656         spin_lock(&ctlr->ms_lock);
657         list_add_tail(&qdata->entry, &ctlr->ms_head);
658         if (!ctlr->ms_queued) {
659                 ctlr->ms_queued = 1;
660                 ctlr->ms_sdev = sdev;
661                 queue_work(kmpath_rdacd, &ctlr->ms_work);
662         }
663         spin_unlock(&ctlr->ms_lock);
664         return SCSI_DH_OK;
665 }
666
667 static int rdac_activate(struct scsi_device *sdev,
668                         activate_complete fn, void *data)
669 {
670         struct rdac_dh_data *h = get_rdac_data(sdev);
671         int err = SCSI_DH_OK;
672         int act = 0;
673
674         err = check_ownership(sdev, h);
675         if (err != SCSI_DH_OK)
676                 goto done;
677
678         switch (h->mode) {
679         case RDAC_MODE:
680                 if (h->lun_state == RDAC_LUN_UNOWNED)
681                         act = 1;
682                 break;
683         case RDAC_MODE_IOSHIP:
684                 if ((h->lun_state == RDAC_LUN_UNOWNED) &&
685                     (h->preferred == RDAC_PREFERRED))
686                         act = 1;
687                 break;
688         default:
689                 break;
690         }
691
692         if (act) {
693                 err = queue_mode_select(sdev, fn, data);
694                 if (err == SCSI_DH_OK)
695                         return 0;
696         }
697 done:
698         if (fn)
699                 fn(data, err);
700         return 0;
701 }
702
703 static int rdac_prep_fn(struct scsi_device *sdev, struct request *req)
704 {
705         struct rdac_dh_data *h = get_rdac_data(sdev);
706         int ret = BLKPREP_OK;
707
708         if (h->state != RDAC_STATE_ACTIVE) {
709                 ret = BLKPREP_KILL;
710                 req->cmd_flags |= REQ_QUIET;
711         }
712         return ret;
713
714 }
715
716 static int rdac_check_sense(struct scsi_device *sdev,
717                                 struct scsi_sense_hdr *sense_hdr)
718 {
719         struct rdac_dh_data *h = get_rdac_data(sdev);
720
721         RDAC_LOG(RDAC_LOG_SENSE, sdev, "array %s, ctlr %d, "
722                         "I/O returned with sense %02x/%02x/%02x",
723                         (char *) h->ctlr->array_name, h->ctlr->index,
724                         sense_hdr->sense_key, sense_hdr->asc, sense_hdr->ascq);
725
726         switch (sense_hdr->sense_key) {
727         case NOT_READY:
728                 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x01)
729                         /* LUN Not Ready - Logical Unit Not Ready and is in
730                         * the process of becoming ready
731                         * Just retry.
732                         */
733                         return ADD_TO_MLQUEUE;
734                 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x81)
735                         /* LUN Not Ready - Storage firmware incompatible
736                          * Manual code synchonisation required.
737                          *
738                          * Nothing we can do here. Try to bypass the path.
739                          */
740                         return SUCCESS;
741                 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0xA1)
742                         /* LUN Not Ready - Quiescense in progress
743                          *
744                          * Just retry and wait.
745                          */
746                         return ADD_TO_MLQUEUE;
747                 if (sense_hdr->asc == 0xA1  && sense_hdr->ascq == 0x02)
748                         /* LUN Not Ready - Quiescense in progress
749                          * or has been achieved
750                          * Just retry.
751                          */
752                         return ADD_TO_MLQUEUE;
753                 break;
754         case ILLEGAL_REQUEST:
755                 if (sense_hdr->asc == 0x94 && sense_hdr->ascq == 0x01) {
756                         /* Invalid Request - Current Logical Unit Ownership.
757                          * Controller is not the current owner of the LUN,
758                          * Fail the path, so that the other path be used.
759                          */
760                         h->state = RDAC_STATE_PASSIVE;
761                         return SUCCESS;
762                 }
763                 break;
764         case UNIT_ATTENTION:
765                 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
766                         /*
767                          * Power On, Reset, or Bus Device Reset, just retry.
768                          */
769                         return ADD_TO_MLQUEUE;
770                 if (sense_hdr->asc == 0x8b && sense_hdr->ascq == 0x02)
771                         /*
772                          * Quiescence in progress , just retry.
773                          */
774                         return ADD_TO_MLQUEUE;
775                 break;
776         }
777         /* success just means we do not care what scsi-ml does */
778         return SCSI_RETURN_NOT_HANDLED;
779 }
780
781 static const struct {
782         char *vendor;
783         char *model;
784 } rdac_dev_list[] = {
785         {"IBM", "1722"},
786         {"IBM", "1724"},
787         {"IBM", "1726"},
788         {"IBM", "1742"},
789         {"IBM", "1745"},
790         {"IBM", "1746"},
791         {"IBM", "1813"},
792         {"IBM", "1814"},
793         {"IBM", "1815"},
794         {"IBM", "1818"},
795         {"IBM", "3526"},
796         {"SGI", "TP9"},
797         {"SGI", "IS"},
798         {"STK", "OPENstorage D280"},
799         {"STK", "FLEXLINE 380"},
800         {"SUN", "CSM"},
801         {"SUN", "LCSM100"},
802         {"SUN", "STK6580_6780"},
803         {"SUN", "SUN_6180"},
804         {"SUN", "ArrayStorage"},
805         {"DELL", "MD3"},
806         {"NETAPP", "INF-01-00"},
807         {"LSI", "INF-01-00"},
808         {"ENGENIO", "INF-01-00"},
809         {NULL, NULL},
810 };
811
812 static bool rdac_match(struct scsi_device *sdev)
813 {
814         int i;
815
816         if (scsi_device_tpgs(sdev))
817                 return false;
818
819         for (i = 0; rdac_dev_list[i].vendor; i++) {
820                 if (!strncmp(sdev->vendor, rdac_dev_list[i].vendor,
821                         strlen(rdac_dev_list[i].vendor)) &&
822                     !strncmp(sdev->model, rdac_dev_list[i].model,
823                         strlen(rdac_dev_list[i].model))) {
824                         return true;
825                 }
826         }
827         return false;
828 }
829
830 static struct scsi_dh_data *rdac_bus_attach(struct scsi_device *sdev)
831 {
832         struct rdac_dh_data *h;
833         int err;
834         char array_name[ARRAY_LABEL_LEN];
835         char array_id[UNIQUE_ID_LEN];
836
837         h = kzalloc(sizeof(*h) , GFP_KERNEL);
838         if (!h)
839                 return ERR_PTR(-ENOMEM);
840         h->lun = UNINITIALIZED_LUN;
841         h->state = RDAC_STATE_ACTIVE;
842
843         err = get_lun_info(sdev, h, array_name, array_id);
844         if (err != SCSI_DH_OK)
845                 goto failed;
846
847         err = initialize_controller(sdev, h, array_name, array_id);
848         if (err != SCSI_DH_OK)
849                 goto failed;
850
851         err = check_ownership(sdev, h);
852         if (err != SCSI_DH_OK)
853                 goto clean_ctlr;
854
855         err = set_mode_select(sdev, h);
856         if (err != SCSI_DH_OK)
857                 goto clean_ctlr;
858
859         sdev_printk(KERN_NOTICE, sdev,
860                     "%s: LUN %d (%s) (%s)\n",
861                     RDAC_NAME, h->lun, mode[(int)h->mode],
862                     lun_state[(int)h->lun_state]);
863
864         return &h->dh_data;
865
866 clean_ctlr:
867         spin_lock(&list_lock);
868         kref_put(&h->ctlr->kref, release_controller);
869         spin_unlock(&list_lock);
870
871 failed:
872         kfree(h);
873         return ERR_PTR(-EINVAL);
874 }
875
876 static void rdac_bus_detach( struct scsi_device *sdev )
877 {
878         struct rdac_dh_data *h = get_rdac_data(sdev);
879
880         if (h->ctlr && h->ctlr->ms_queued)
881                 flush_workqueue(kmpath_rdacd);
882
883         spin_lock(&list_lock);
884         if (h->ctlr)
885                 kref_put(&h->ctlr->kref, release_controller);
886         spin_unlock(&list_lock);
887         kfree(h);
888 }
889
890 static struct scsi_device_handler rdac_dh = {
891         .name = RDAC_NAME,
892         .module = THIS_MODULE,
893         .prep_fn = rdac_prep_fn,
894         .check_sense = rdac_check_sense,
895         .attach = rdac_bus_attach,
896         .detach = rdac_bus_detach,
897         .activate = rdac_activate,
898         .match = rdac_match,
899 };
900
901 static int __init rdac_init(void)
902 {
903         int r;
904
905         r = scsi_register_device_handler(&rdac_dh);
906         if (r != 0) {
907                 printk(KERN_ERR "Failed to register scsi device handler.");
908                 goto done;
909         }
910
911         /*
912          * Create workqueue to handle mode selects for rdac
913          */
914         kmpath_rdacd = create_singlethread_workqueue("kmpath_rdacd");
915         if (!kmpath_rdacd) {
916                 scsi_unregister_device_handler(&rdac_dh);
917                 printk(KERN_ERR "kmpath_rdacd creation failed.\n");
918
919                 r = -EINVAL;
920         }
921 done:
922         return r;
923 }
924
925 static void __exit rdac_exit(void)
926 {
927         destroy_workqueue(kmpath_rdacd);
928         scsi_unregister_device_handler(&rdac_dh);
929 }
930
931 module_init(rdac_init);
932 module_exit(rdac_exit);
933
934 MODULE_DESCRIPTION("Multipath LSI/Engenio/NetApp E-Series RDAC driver");
935 MODULE_AUTHOR("Mike Christie, Chandra Seetharaman");
936 MODULE_VERSION("01.00.0000.0000");
937 MODULE_LICENSE("GPL");