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