bc9e94f5915e1227a14262b907f14858711c134b
[linux-flexiantxendom0-3.2.10.git] / drivers / scsi / device_handler / scsi_dh_alua.c
1 /*
2  * Generic SCSI-3 ALUA SCSI Device Handler
3  *
4  * Copyright (C) 2007, 2008 Hannes Reinecke, SUSE Linux Products GmbH.
5  * All rights reserved.
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
26 #define ALUA_DH_NAME "alua"
27 #define ALUA_DH_VER "1.2"
28
29 #define TPGS_STATE_OPTIMIZED            0x0
30 #define TPGS_STATE_NONOPTIMIZED         0x1
31 #define TPGS_STATE_STANDBY              0x2
32 #define TPGS_STATE_UNAVAILABLE          0x3
33 #define TPGS_STATE_OFFLINE              0xe
34 #define TPGS_STATE_TRANSITIONING        0xf
35
36 #define TPGS_SUPPORT_NONE               0x00
37 #define TPGS_SUPPORT_OPTIMIZED          0x01
38 #define TPGS_SUPPORT_NONOPTIMIZED       0x02
39 #define TPGS_SUPPORT_STANDBY            0x04
40 #define TPGS_SUPPORT_UNAVAILABLE        0x08
41 #define TPGS_SUPPORT_OFFLINE            0x40
42 #define TPGS_SUPPORT_TRANSITION         0x80
43
44 #define TPGS_MODE_UNINITIALIZED          -1
45 #define TPGS_MODE_NONE                  0x0
46 #define TPGS_MODE_IMPLICIT              0x1
47 #define TPGS_MODE_EXPLICIT              0x2
48
49 #define ALUA_INQUIRY_SIZE               36
50 #define ALUA_FAILOVER_TIMEOUT           (60 * HZ)
51 #define ALUA_FAILOVER_RETRIES           5
52
53 struct alua_dh_data {
54         int                     group_id;
55         int                     rel_port;
56         int                     tpgs;
57         int                     state;
58         unsigned char           inq[ALUA_INQUIRY_SIZE];
59         unsigned char           *buff;
60         int                     bufflen;
61         unsigned char           sense[SCSI_SENSE_BUFFERSIZE];
62         int                     senselen;
63         struct scsi_device      *sdev;
64         activate_complete       callback_fn;
65         void                    *callback_data;
66 };
67
68 #define ALUA_POLICY_SWITCH_CURRENT      0
69 #define ALUA_POLICY_SWITCH_ALL          1
70
71 static char print_alua_state(int);
72 static int alua_check_sense(struct scsi_device *, struct scsi_sense_hdr *);
73
74 static inline struct alua_dh_data *get_alua_data(struct scsi_device *sdev)
75 {
76         struct scsi_dh_data *scsi_dh_data = sdev->scsi_dh_data;
77         BUG_ON(scsi_dh_data == NULL);
78         return ((struct alua_dh_data *) scsi_dh_data->buf);
79 }
80
81 static int realloc_buffer(struct alua_dh_data *h, unsigned len)
82 {
83         if (h->buff && h->buff != h->inq)
84                 kfree(h->buff);
85
86         h->buff = kmalloc(len, GFP_NOIO);
87         if (!h->buff) {
88                 h->buff = h->inq;
89                 h->bufflen = ALUA_INQUIRY_SIZE;
90                 return 1;
91         }
92         h->bufflen = len;
93         return 0;
94 }
95
96 static struct request *get_alua_req(struct scsi_device *sdev,
97                                     void *buffer, unsigned buflen, int rw)
98 {
99         struct request *rq;
100         struct request_queue *q = sdev->request_queue;
101
102         rq = blk_get_request(q, rw, GFP_NOIO);
103
104         if (!rq) {
105                 sdev_printk(KERN_INFO, sdev,
106                             "%s: blk_get_request failed\n", __func__);
107                 return NULL;
108         }
109
110         if (buflen && blk_rq_map_kern(q, rq, buffer, buflen, GFP_NOIO)) {
111                 blk_put_request(rq);
112                 sdev_printk(KERN_INFO, sdev,
113                             "%s: blk_rq_map_kern failed\n", __func__);
114                 return NULL;
115         }
116
117         rq->cmd_type = REQ_TYPE_BLOCK_PC;
118         rq->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
119                          REQ_FAILFAST_DRIVER;
120         rq->retries = ALUA_FAILOVER_RETRIES;
121         rq->timeout = ALUA_FAILOVER_TIMEOUT;
122
123         return rq;
124 }
125
126 /*
127  * submit_std_inquiry - Issue a standard INQUIRY command
128  * @sdev: sdev the command should be send to
129  */
130 static int submit_std_inquiry(struct scsi_device *sdev, struct alua_dh_data *h)
131 {
132         struct request *rq;
133         int err = SCSI_DH_RES_TEMP_UNAVAIL;
134
135         rq = get_alua_req(sdev, h->inq, ALUA_INQUIRY_SIZE, READ);
136         if (!rq)
137                 goto done;
138
139         /* Prepare the command. */
140         rq->cmd[0] = INQUIRY;
141         rq->cmd[1] = 0;
142         rq->cmd[2] = 0;
143         rq->cmd[4] = ALUA_INQUIRY_SIZE;
144         rq->cmd_len = COMMAND_SIZE(INQUIRY);
145
146         rq->sense = h->sense;
147         memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
148         rq->sense_len = h->senselen = 0;
149
150         err = blk_execute_rq(rq->q, NULL, rq, 1);
151         if (err == -EIO) {
152                 sdev_printk(KERN_INFO, sdev,
153                             "%s: std inquiry failed with %x\n",
154                             ALUA_DH_NAME, rq->errors);
155                 h->senselen = rq->sense_len;
156                 err = SCSI_DH_IO;
157         }
158         blk_put_request(rq);
159 done:
160         return err;
161 }
162
163 /*
164  * submit_vpd_inquiry - Issue an INQUIRY VPD page 0x83 command
165  * @sdev: sdev the command should be sent to
166  */
167 static int submit_vpd_inquiry(struct scsi_device *sdev, struct alua_dh_data *h)
168 {
169         struct request *rq;
170         int err = SCSI_DH_RES_TEMP_UNAVAIL;
171
172         rq = get_alua_req(sdev, h->buff, h->bufflen, READ);
173         if (!rq)
174                 goto done;
175
176         /* Prepare the command. */
177         rq->cmd[0] = INQUIRY;
178         rq->cmd[1] = 1;
179         rq->cmd[2] = 0x83;
180         rq->cmd[4] = h->bufflen;
181         rq->cmd_len = COMMAND_SIZE(INQUIRY);
182
183         rq->sense = h->sense;
184         memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
185         rq->sense_len = h->senselen = 0;
186
187         err = blk_execute_rq(rq->q, NULL, rq, 1);
188         if (err == -EIO) {
189                 sdev_printk(KERN_INFO, sdev,
190                             "%s: evpd inquiry failed with %x\n",
191                             ALUA_DH_NAME, rq->errors);
192                 h->senselen = rq->sense_len;
193                 err = SCSI_DH_IO;
194         }
195         blk_put_request(rq);
196 done:
197         return err;
198 }
199
200 /*
201  * submit_rtpg - Issue a REPORT TARGET GROUP STATES command
202  * @sdev: sdev the command should be sent to
203  */
204 static unsigned submit_rtpg(struct scsi_device *sdev, struct alua_dh_data *h)
205 {
206         struct request *rq;
207         int err = SCSI_DH_RES_TEMP_UNAVAIL;
208
209         rq = get_alua_req(sdev, h->buff, h->bufflen, READ);
210         if (!rq)
211                 goto done;
212
213         /* Prepare the command. */
214         rq->cmd[0] = MAINTENANCE_IN;
215         rq->cmd[1] = MI_REPORT_TARGET_PGS;
216         rq->cmd[6] = (h->bufflen >> 24) & 0xff;
217         rq->cmd[7] = (h->bufflen >> 16) & 0xff;
218         rq->cmd[8] = (h->bufflen >>  8) & 0xff;
219         rq->cmd[9] = h->bufflen & 0xff;
220         rq->cmd_len = COMMAND_SIZE(MAINTENANCE_IN);
221
222         rq->sense = h->sense;
223         memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
224         rq->sense_len = h->senselen = 0;
225
226         err = blk_execute_rq(rq->q, NULL, rq, 1);
227         if (err == -EIO) {
228                 sdev_printk(KERN_INFO, sdev,
229                             "%s: rtpg failed with %x\n",
230                             ALUA_DH_NAME, rq->errors);
231                 h->senselen = rq->sense_len;
232                 err = SCSI_DH_IO;
233         }
234         blk_put_request(rq);
235 done:
236         return err;
237 }
238
239 /*
240  * alua_stpg - Evaluate SET TARGET GROUP STATES
241  * @sdev: the device to be evaluated
242  * @state: the new target group state
243  *
244  * Send a SET TARGET GROUP STATES command to the device.
245  * We only have to test here if we should resubmit the command;
246  * any other error is assumed as a failure.
247  */
248 static void stpg_endio(struct request *req, int error)
249 {
250         struct alua_dh_data *h = req->end_io_data;
251         struct scsi_sense_hdr sense_hdr;
252         unsigned err = SCSI_DH_IO;
253
254         if (error || host_byte(req->errors) != DID_OK ||
255                         msg_byte(req->errors) != COMMAND_COMPLETE)
256                 goto done;
257
258         if (err == SCSI_DH_IO && h->senselen > 0) {
259                 err = scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
260                                            &sense_hdr);
261                 if (!err) {
262                         err = SCSI_DH_IO;
263                         goto done;
264                 }
265                 err = alua_check_sense(h->sdev, &sense_hdr);
266                 if (err == ADD_TO_MLQUEUE) {
267                         err = SCSI_DH_RETRY;
268                         goto done;
269                 }
270                 sdev_printk(KERN_INFO, h->sdev,
271                             "%s: stpg sense code: %02x/%02x/%02x\n",
272                             ALUA_DH_NAME, sense_hdr.sense_key,
273                             sense_hdr.asc, sense_hdr.ascq);
274                 err = SCSI_DH_IO;
275         }
276         if (err == SCSI_DH_OK) {
277                 h->state = TPGS_STATE_OPTIMIZED;
278                 sdev_printk(KERN_INFO, h->sdev,
279                             "%s: port group %02x switched to state %c\n",
280                             ALUA_DH_NAME, h->group_id,
281                             print_alua_state(h->state));
282         }
283 done:
284         blk_put_request(req);
285         if (h->callback_fn) {
286                 h->callback_fn(h->callback_data, err);
287                 h->callback_fn = h->callback_data = NULL;
288         }
289         return;
290 }
291
292 /*
293  * submit_stpg - Issue a SET TARGET GROUP STATES command
294  *
295  * Currently we're only setting the current target port group state
296  * to 'active/optimized' and let the array firmware figure out
297  * the states of the remaining groups.
298  */
299 static unsigned submit_stpg(struct alua_dh_data *h)
300 {
301         struct request *rq;
302         int err = SCSI_DH_RES_TEMP_UNAVAIL;
303         int stpg_len = 8;
304         struct scsi_device *sdev = h->sdev;
305
306         /* Prepare the data buffer */
307         memset(h->buff, 0, stpg_len);
308         h->buff[4] = TPGS_STATE_OPTIMIZED & 0x0f;
309         h->buff[6] = (h->group_id >> 8) & 0xff;
310         h->buff[7] = h->group_id & 0xff;
311
312         rq = get_alua_req(sdev, h->buff, stpg_len, WRITE);
313         if (!rq)
314                 return SCSI_DH_RES_TEMP_UNAVAIL;
315
316         /* Prepare the command. */
317         rq->cmd[0] = MAINTENANCE_OUT;
318         rq->cmd[1] = MO_SET_TARGET_PGS;
319         rq->cmd[6] = (stpg_len >> 24) & 0xff;
320         rq->cmd[7] = (stpg_len >> 16) & 0xff;
321         rq->cmd[8] = (stpg_len >>  8) & 0xff;
322         rq->cmd[9] = stpg_len & 0xff;
323         rq->cmd_len = COMMAND_SIZE(MAINTENANCE_OUT);
324
325         rq->sense = h->sense;
326         memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
327         rq->sense_len = h->senselen = 0;
328         rq->end_io_data = h;
329
330         blk_execute_rq_nowait(rq->q, NULL, rq, 1, stpg_endio);
331         return err;
332 }
333
334 /*
335  * alua_std_inquiry - Evaluate standard INQUIRY command
336  * @sdev: device to be checked
337  *
338  * Just extract the TPGS setting to find out if ALUA
339  * is supported.
340  */
341 static int alua_std_inquiry(struct scsi_device *sdev, struct alua_dh_data *h)
342 {
343         int err;
344
345         err = submit_std_inquiry(sdev, h);
346
347         if (err != SCSI_DH_OK)
348                 return err;
349
350         /* Check TPGS setting */
351         h->tpgs = (h->inq[5] >> 4) & 0x3;
352         switch (h->tpgs) {
353         case TPGS_MODE_EXPLICIT|TPGS_MODE_IMPLICIT:
354                 sdev_printk(KERN_INFO, sdev,
355                             "%s: supports implicit and explicit TPGS\n",
356                             ALUA_DH_NAME);
357                 break;
358         case TPGS_MODE_EXPLICIT:
359                 sdev_printk(KERN_INFO, sdev, "%s: supports explicit TPGS\n",
360                             ALUA_DH_NAME);
361                 break;
362         case TPGS_MODE_IMPLICIT:
363                 sdev_printk(KERN_INFO, sdev, "%s: supports implicit TPGS\n",
364                             ALUA_DH_NAME);
365                 break;
366         default:
367                 h->tpgs = TPGS_MODE_NONE;
368                 sdev_printk(KERN_INFO, sdev, "%s: not supported\n",
369                             ALUA_DH_NAME);
370                 err = SCSI_DH_DEV_UNSUPP;
371                 break;
372         }
373
374         return err;
375 }
376
377 /*
378  * alua_vpd_inquiry - Evaluate INQUIRY vpd page 0x83
379  * @sdev: device to be checked
380  *
381  * Extract the relative target port and the target port group
382  * descriptor from the list of identificators.
383  */
384 static int alua_vpd_inquiry(struct scsi_device *sdev, struct alua_dh_data *h)
385 {
386         int len;
387         unsigned err;
388         unsigned char *d;
389
390  retry:
391         err = submit_vpd_inquiry(sdev, h);
392
393         if (err != SCSI_DH_OK)
394                 return err;
395
396         /* Check if vpd page exceeds initial buffer */
397         len = (h->buff[2] << 8) + h->buff[3] + 4;
398         if (len > h->bufflen) {
399                 /* Resubmit with the correct length */
400                 if (realloc_buffer(h, len)) {
401                         sdev_printk(KERN_WARNING, sdev,
402                                     "%s: kmalloc buffer failed\n",
403                                     ALUA_DH_NAME);
404                         /* Temporary failure, bypass */
405                         return SCSI_DH_DEV_TEMP_BUSY;
406                 }
407                 goto retry;
408         }
409
410         /*
411          * Now look for the correct descriptor.
412          */
413         d = h->buff + 4;
414         while (d < h->buff + len) {
415                 switch (d[1] & 0xf) {
416                 case 0x4:
417                         /* Relative target port */
418                         h->rel_port = (d[6] << 8) + d[7];
419                         break;
420                 case 0x5:
421                         /* Target port group */
422                         h->group_id = (d[6] << 8) + d[7];
423                         break;
424                 default:
425                         break;
426                 }
427                 d += d[3] + 4;
428         }
429
430         if (h->group_id == -1) {
431                 /*
432                  * Internal error; TPGS supported but required
433                  * VPD identification descriptors not present.
434                  * Disable ALUA support
435                  */
436                 sdev_printk(KERN_INFO, sdev,
437                             "%s: No target port descriptors found\n",
438                             ALUA_DH_NAME);
439                 h->state = TPGS_STATE_OPTIMIZED;
440                 h->tpgs = TPGS_MODE_NONE;
441                 err = SCSI_DH_DEV_UNSUPP;
442         } else {
443                 sdev_printk(KERN_INFO, sdev,
444                             "%s: port group %02x rel port %02x\n",
445                             ALUA_DH_NAME, h->group_id, h->rel_port);
446         }
447
448         return err;
449 }
450
451 static char print_alua_state(int state)
452 {
453         switch (state) {
454         case TPGS_STATE_OPTIMIZED:
455                 return 'A';
456         case TPGS_STATE_NONOPTIMIZED:
457                 return 'N';
458         case TPGS_STATE_STANDBY:
459                 return 'S';
460         case TPGS_STATE_UNAVAILABLE:
461                 return 'U';
462         case TPGS_STATE_OFFLINE:
463                 return 'O';
464         case TPGS_STATE_TRANSITIONING:
465                 return 'T';
466         default:
467                 return 'X';
468         }
469 }
470
471 static int alua_check_sense(struct scsi_device *sdev,
472                             struct scsi_sense_hdr *sense_hdr)
473 {
474         switch (sense_hdr->sense_key) {
475         case NOT_READY:
476                 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0a)
477                         /*
478                          * LUN Not Accessible - ALUA state transition
479                          */
480                         return ADD_TO_MLQUEUE;
481                 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0b)
482                         /*
483                          * LUN Not Accessible -- Target port in standby state
484                          */
485                         return SUCCESS;
486                 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0c)
487                         /*
488                          * LUN Not Accessible -- Target port in unavailable state
489                          */
490                         return SUCCESS;
491                 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x12)
492                         /*
493                          * LUN Not Ready -- Offline
494                          */
495                         return SUCCESS;
496                 break;
497         case UNIT_ATTENTION:
498                 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
499                         /*
500                          * Power On, Reset, or Bus Device Reset, just retry.
501                          */
502                         return ADD_TO_MLQUEUE;
503                 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06) {
504                         /*
505                          * ALUA state changed
506                          */
507                         return ADD_TO_MLQUEUE;
508                 }
509                 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07) {
510                         /*
511                          * Implicit ALUA state transition failed
512                          */
513                         return ADD_TO_MLQUEUE;
514                 }
515                 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x0e) {
516                         /*
517                          * REPORTED_LUNS_DATA_HAS_CHANGED is reported
518                          * when switching controllers on targets like
519                          * Intel Multi-Flex. We can just retry.
520                          */
521                         return ADD_TO_MLQUEUE;
522                 }
523
524                 break;
525         }
526
527         return SCSI_RETURN_NOT_HANDLED;
528 }
529
530 /*
531  * alua_rtpg - Evaluate REPORT TARGET GROUP STATES
532  * @sdev: the device to be evaluated.
533  *
534  * Evaluate the Target Port Group State.
535  * Returns SCSI_DH_DEV_OFFLINED if the path is
536  * found to be unuseable.
537  */
538 static int alua_rtpg(struct scsi_device *sdev, struct alua_dh_data *h)
539 {
540         struct scsi_sense_hdr sense_hdr;
541         int len, k, off, valid_states = 0;
542         char *ucp;
543         unsigned err;
544
545  retry:
546         err = submit_rtpg(sdev, h);
547
548         if (err == SCSI_DH_IO && h->senselen > 0) {
549                 err = scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
550                                            &sense_hdr);
551                 if (!err)
552                         return SCSI_DH_IO;
553
554                 err = alua_check_sense(sdev, &sense_hdr);
555                 if (err == ADD_TO_MLQUEUE)
556                         goto retry;
557                 sdev_printk(KERN_INFO, sdev,
558                             "%s: rtpg sense code %02x/%02x/%02x\n",
559                             ALUA_DH_NAME, sense_hdr.sense_key,
560                             sense_hdr.asc, sense_hdr.ascq);
561                 err = SCSI_DH_IO;
562         }
563         if (err != SCSI_DH_OK)
564                 return err;
565
566         len = (h->buff[0] << 24) + (h->buff[1] << 16) +
567                 (h->buff[2] << 8) + h->buff[3] + 4;
568
569         if (len > h->bufflen) {
570                 /* Resubmit with the correct length */
571                 if (realloc_buffer(h, len)) {
572                         sdev_printk(KERN_WARNING, sdev,
573                                     "%s: kmalloc buffer failed\n",__func__);
574                         /* Temporary failure, bypass */
575                         return SCSI_DH_DEV_TEMP_BUSY;
576                 }
577                 goto retry;
578         }
579
580         for (k = 4, ucp = h->buff + 4; k < len; k += off, ucp += off) {
581                 if (h->group_id == (ucp[2] << 8) + ucp[3]) {
582                         h->state = ucp[0] & 0x0f;
583                         valid_states = ucp[1];
584                 }
585                 off = 8 + (ucp[7] * 4);
586         }
587
588         sdev_printk(KERN_INFO, sdev,
589                     "%s: port group %02x state %c supports %c%c%c%c%c%c\n",
590                     ALUA_DH_NAME, h->group_id, print_alua_state(h->state),
591                     valid_states&TPGS_SUPPORT_TRANSITION?'T':'t',
592                     valid_states&TPGS_SUPPORT_OFFLINE?'O':'o',
593                     valid_states&TPGS_SUPPORT_UNAVAILABLE?'U':'u',
594                     valid_states&TPGS_SUPPORT_STANDBY?'S':'s',
595                     valid_states&TPGS_SUPPORT_NONOPTIMIZED?'N':'n',
596                     valid_states&TPGS_SUPPORT_OPTIMIZED?'A':'a');
597
598         if (h->tpgs & TPGS_MODE_EXPLICIT) {
599                 switch (h->state) {
600                 case TPGS_STATE_TRANSITIONING:
601                         /* State transition, retry */
602                         goto retry;
603                         break;
604                 case TPGS_STATE_OFFLINE:
605                         /* Path is offline, fail */
606                         err = SCSI_DH_DEV_OFFLINED;
607                         break;
608                 default:
609                         break;
610                 }
611         } else {
612                 /* Only Implicit ALUA support */
613                 if (h->state == TPGS_STATE_OPTIMIZED ||
614                     h->state == TPGS_STATE_NONOPTIMIZED ||
615                     h->state == TPGS_STATE_STANDBY)
616                         /* Useable path if active */
617                         err = SCSI_DH_OK;
618                 else
619                         /* Path unuseable for unavailable/offline */
620                         err = SCSI_DH_DEV_OFFLINED;
621         }
622         return err;
623 }
624
625 /*
626  * alua_initialize - Initialize ALUA state
627  * @sdev: the device to be initialized
628  *
629  * For the prep_fn to work correctly we have
630  * to initialize the ALUA state for the device.
631  */
632 static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h)
633 {
634         int err;
635
636         err = alua_std_inquiry(sdev, h);
637         if (err != SCSI_DH_OK)
638                 goto out;
639
640         err = alua_vpd_inquiry(sdev, h);
641         if (err != SCSI_DH_OK)
642                 goto out;
643
644         err = alua_rtpg(sdev, h);
645         if (err != SCSI_DH_OK)
646                 goto out;
647
648 out:
649         return err;
650 }
651
652 /*
653  * alua_activate - activate a path
654  * @sdev: device on the path to be activated
655  *
656  * We're currently switching the port group to be activated only and
657  * let the array figure out the rest.
658  * There may be other arrays which require us to switch all port groups
659  * based on a certain policy. But until we actually encounter them it
660  * should be okay.
661  */
662 static int alua_activate(struct scsi_device *sdev,
663                         activate_complete fn, void *data)
664 {
665         struct alua_dh_data *h = get_alua_data(sdev);
666         int err = SCSI_DH_OK;
667
668         if (h->group_id != -1) {
669                 err = alua_rtpg(sdev, h);
670                 if (err != SCSI_DH_OK)
671                         goto out;
672         }
673
674         if (h->tpgs & TPGS_MODE_EXPLICIT && h->state != TPGS_STATE_OPTIMIZED) {
675                 h->callback_fn = fn;
676                 h->callback_data = data;
677                 err = submit_stpg(h);
678                 if (err == SCSI_DH_OK)
679                         return 0;
680                 h->callback_fn = h->callback_data = NULL;
681         }
682
683 out:
684         if (fn)
685                 fn(data, err);
686         return 0;
687 }
688
689 /*
690  * alua_prep_fn - request callback
691  *
692  * Fail I/O to all paths not in state
693  * active/optimized or active/non-optimized.
694  */
695 static int alua_prep_fn(struct scsi_device *sdev, struct request *req)
696 {
697         struct alua_dh_data *h = get_alua_data(sdev);
698         int ret = BLKPREP_OK;
699
700         if (h->state != TPGS_STATE_OPTIMIZED &&
701             h->state != TPGS_STATE_NONOPTIMIZED) {
702                 ret = BLKPREP_KILL;
703                 req->cmd_flags |= REQ_QUIET;
704         }
705         return ret;
706
707 }
708
709 static const struct scsi_dh_devlist alua_dev_list[] = {
710         {"HP", "MSA VOLUME" },
711         {"HP", "HSV101" },
712         {"HP", "HSV111" },
713         {"HP", "HSV200" },
714         {"HP", "HSV210" },
715         {"HP", "HSV300" },
716         {"IBM", "2107900" },
717         {"IBM", "2145" },
718         {"Pillar", "Axiom" },
719         {"Intel", "Multi-Flex"},
720         {"NETAPP", "LUN"},
721         {"AIX", "NVDISK"},
722         {NULL, NULL}
723 };
724
725 static int alua_bus_attach(struct scsi_device *sdev);
726 static void alua_bus_detach(struct scsi_device *sdev);
727
728 static struct scsi_device_handler alua_dh = {
729         .name = ALUA_DH_NAME,
730         .module = THIS_MODULE,
731         .devlist = alua_dev_list,
732         .attach = alua_bus_attach,
733         .detach = alua_bus_detach,
734         .prep_fn = alua_prep_fn,
735         .check_sense = alua_check_sense,
736         .activate = alua_activate,
737 };
738
739 /*
740  * alua_bus_attach - Attach device handler
741  * @sdev: device to be attached to
742  */
743 static int alua_bus_attach(struct scsi_device *sdev)
744 {
745         struct scsi_dh_data *scsi_dh_data;
746         struct alua_dh_data *h;
747         unsigned long flags;
748         int err = SCSI_DH_OK;
749
750         scsi_dh_data = kzalloc(sizeof(struct scsi_device_handler *)
751                                + sizeof(*h) , GFP_KERNEL);
752         if (!scsi_dh_data) {
753                 sdev_printk(KERN_ERR, sdev, "%s: Attach failed\n",
754                             ALUA_DH_NAME);
755                 return -ENOMEM;
756         }
757
758         scsi_dh_data->scsi_dh = &alua_dh;
759         h = (struct alua_dh_data *) scsi_dh_data->buf;
760         h->tpgs = TPGS_MODE_UNINITIALIZED;
761         h->state = TPGS_STATE_OPTIMIZED;
762         h->group_id = -1;
763         h->rel_port = -1;
764         h->buff = h->inq;
765         h->bufflen = ALUA_INQUIRY_SIZE;
766         h->sdev = sdev;
767
768         err = alua_initialize(sdev, h);
769         if (err != SCSI_DH_OK)
770                 goto failed;
771
772         if (!try_module_get(THIS_MODULE))
773                 goto failed;
774
775         spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
776         sdev->scsi_dh_data = scsi_dh_data;
777         spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
778
779         return 0;
780
781 failed:
782         kfree(scsi_dh_data);
783         sdev_printk(KERN_ERR, sdev, "%s: not attached\n", ALUA_DH_NAME);
784         return -EINVAL;
785 }
786
787 /*
788  * alua_bus_detach - Detach device handler
789  * @sdev: device to be detached from
790  */
791 static void alua_bus_detach(struct scsi_device *sdev)
792 {
793         struct scsi_dh_data *scsi_dh_data;
794         struct alua_dh_data *h;
795         unsigned long flags;
796
797         spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
798         scsi_dh_data = sdev->scsi_dh_data;
799         sdev->scsi_dh_data = NULL;
800         spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
801
802         h = (struct alua_dh_data *) scsi_dh_data->buf;
803         if (h->buff && h->inq != h->buff)
804                 kfree(h->buff);
805         kfree(scsi_dh_data);
806         module_put(THIS_MODULE);
807         sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", ALUA_DH_NAME);
808 }
809
810 static int __init alua_init(void)
811 {
812         int r;
813
814         r = scsi_register_device_handler(&alua_dh);
815         if (r != 0)
816                 printk(KERN_ERR "%s: Failed to register scsi device handler",
817                         ALUA_DH_NAME);
818         return r;
819 }
820
821 static void __exit alua_exit(void)
822 {
823         scsi_unregister_device_handler(&alua_dh);
824 }
825
826 module_init(alua_init);
827 module_exit(alua_exit);
828
829 MODULE_DESCRIPTION("DM Multipath ALUA support");
830 MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
831 MODULE_LICENSE("GPL");
832 MODULE_VERSION(ALUA_DH_VER);