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