include: replace linux/module.h with "struct module" wherever possible
[linux-2.6/next.git] / drivers / scsi / device_handler / scsi_dh_alua.c
blob7cbf2384437007976115cc2c0d7de3d3602f8f6f
1 /*
2 * Generic SCSI-3 ALUA SCSI Device Handler
4 * Copyright (C) 2007-2010 Hannes Reinecke, SUSE Linux Products GmbH.
5 * All rights reserved.
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.
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.
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.
22 #include <linux/slab.h>
23 #include <linux/delay.h>
24 #include <linux/module.h>
25 #include <scsi/scsi.h>
26 #include <scsi/scsi_eh.h>
27 #include <scsi/scsi_dh.h>
29 #define ALUA_DH_NAME "alua"
30 #define ALUA_DH_VER "1.3"
32 #define TPGS_STATE_OPTIMIZED 0x0
33 #define TPGS_STATE_NONOPTIMIZED 0x1
34 #define TPGS_STATE_STANDBY 0x2
35 #define TPGS_STATE_UNAVAILABLE 0x3
36 #define TPGS_STATE_LBA_DEPENDENT 0x4
37 #define TPGS_STATE_OFFLINE 0xe
38 #define TPGS_STATE_TRANSITIONING 0xf
40 #define TPGS_SUPPORT_NONE 0x00
41 #define TPGS_SUPPORT_OPTIMIZED 0x01
42 #define TPGS_SUPPORT_NONOPTIMIZED 0x02
43 #define TPGS_SUPPORT_STANDBY 0x04
44 #define TPGS_SUPPORT_UNAVAILABLE 0x08
45 #define TPGS_SUPPORT_LBA_DEPENDENT 0x10
46 #define TPGS_SUPPORT_OFFLINE 0x40
47 #define TPGS_SUPPORT_TRANSITION 0x80
49 #define TPGS_MODE_UNINITIALIZED -1
50 #define TPGS_MODE_NONE 0x0
51 #define TPGS_MODE_IMPLICIT 0x1
52 #define TPGS_MODE_EXPLICIT 0x2
54 #define ALUA_INQUIRY_SIZE 36
55 #define ALUA_FAILOVER_TIMEOUT (60 * HZ)
56 #define ALUA_FAILOVER_RETRIES 5
58 struct alua_dh_data {
59 int group_id;
60 int rel_port;
61 int tpgs;
62 int state;
63 unsigned char inq[ALUA_INQUIRY_SIZE];
64 unsigned char *buff;
65 int bufflen;
66 unsigned char sense[SCSI_SENSE_BUFFERSIZE];
67 int senselen;
68 struct scsi_device *sdev;
69 activate_complete callback_fn;
70 void *callback_data;
73 #define ALUA_POLICY_SWITCH_CURRENT 0
74 #define ALUA_POLICY_SWITCH_ALL 1
76 static char print_alua_state(int);
77 static int alua_check_sense(struct scsi_device *, struct scsi_sense_hdr *);
79 static inline struct alua_dh_data *get_alua_data(struct scsi_device *sdev)
81 struct scsi_dh_data *scsi_dh_data = sdev->scsi_dh_data;
82 BUG_ON(scsi_dh_data == NULL);
83 return ((struct alua_dh_data *) scsi_dh_data->buf);
86 static int realloc_buffer(struct alua_dh_data *h, unsigned len)
88 if (h->buff && h->buff != h->inq)
89 kfree(h->buff);
91 h->buff = kmalloc(len, GFP_NOIO);
92 if (!h->buff) {
93 h->buff = h->inq;
94 h->bufflen = ALUA_INQUIRY_SIZE;
95 return 1;
97 h->bufflen = len;
98 return 0;
101 static struct request *get_alua_req(struct scsi_device *sdev,
102 void *buffer, unsigned buflen, int rw)
104 struct request *rq;
105 struct request_queue *q = sdev->request_queue;
107 rq = blk_get_request(q, rw, GFP_NOIO);
109 if (!rq) {
110 sdev_printk(KERN_INFO, sdev,
111 "%s: blk_get_request failed\n", __func__);
112 return NULL;
115 if (buflen && blk_rq_map_kern(q, rq, buffer, buflen, GFP_NOIO)) {
116 blk_put_request(rq);
117 sdev_printk(KERN_INFO, sdev,
118 "%s: blk_rq_map_kern failed\n", __func__);
119 return NULL;
122 rq->cmd_type = REQ_TYPE_BLOCK_PC;
123 rq->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
124 REQ_FAILFAST_DRIVER;
125 rq->retries = ALUA_FAILOVER_RETRIES;
126 rq->timeout = ALUA_FAILOVER_TIMEOUT;
128 return rq;
132 * submit_std_inquiry - Issue a standard INQUIRY command
133 * @sdev: sdev the command should be send to
135 static int submit_std_inquiry(struct scsi_device *sdev, struct alua_dh_data *h)
137 struct request *rq;
138 int err = SCSI_DH_RES_TEMP_UNAVAIL;
140 rq = get_alua_req(sdev, h->inq, ALUA_INQUIRY_SIZE, READ);
141 if (!rq)
142 goto done;
144 /* Prepare the command. */
145 rq->cmd[0] = INQUIRY;
146 rq->cmd[1] = 0;
147 rq->cmd[2] = 0;
148 rq->cmd[4] = ALUA_INQUIRY_SIZE;
149 rq->cmd_len = COMMAND_SIZE(INQUIRY);
151 rq->sense = h->sense;
152 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
153 rq->sense_len = h->senselen = 0;
155 err = blk_execute_rq(rq->q, NULL, rq, 1);
156 if (err == -EIO) {
157 sdev_printk(KERN_INFO, sdev,
158 "%s: std inquiry failed with %x\n",
159 ALUA_DH_NAME, rq->errors);
160 h->senselen = rq->sense_len;
161 err = SCSI_DH_IO;
163 blk_put_request(rq);
164 done:
165 return err;
169 * submit_vpd_inquiry - Issue an INQUIRY VPD page 0x83 command
170 * @sdev: sdev the command should be sent to
172 static int submit_vpd_inquiry(struct scsi_device *sdev, struct alua_dh_data *h)
174 struct request *rq;
175 int err = SCSI_DH_RES_TEMP_UNAVAIL;
177 rq = get_alua_req(sdev, h->buff, h->bufflen, READ);
178 if (!rq)
179 goto done;
181 /* Prepare the command. */
182 rq->cmd[0] = INQUIRY;
183 rq->cmd[1] = 1;
184 rq->cmd[2] = 0x83;
185 rq->cmd[4] = h->bufflen;
186 rq->cmd_len = COMMAND_SIZE(INQUIRY);
188 rq->sense = h->sense;
189 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
190 rq->sense_len = h->senselen = 0;
192 err = blk_execute_rq(rq->q, NULL, rq, 1);
193 if (err == -EIO) {
194 sdev_printk(KERN_INFO, sdev,
195 "%s: evpd inquiry failed with %x\n",
196 ALUA_DH_NAME, rq->errors);
197 h->senselen = rq->sense_len;
198 err = SCSI_DH_IO;
200 blk_put_request(rq);
201 done:
202 return err;
206 * submit_rtpg - Issue a REPORT TARGET GROUP STATES command
207 * @sdev: sdev the command should be sent to
209 static unsigned submit_rtpg(struct scsi_device *sdev, struct alua_dh_data *h)
211 struct request *rq;
212 int err = SCSI_DH_RES_TEMP_UNAVAIL;
214 rq = get_alua_req(sdev, h->buff, h->bufflen, READ);
215 if (!rq)
216 goto done;
218 /* Prepare the command. */
219 rq->cmd[0] = MAINTENANCE_IN;
220 rq->cmd[1] = MI_REPORT_TARGET_PGS;
221 rq->cmd[6] = (h->bufflen >> 24) & 0xff;
222 rq->cmd[7] = (h->bufflen >> 16) & 0xff;
223 rq->cmd[8] = (h->bufflen >> 8) & 0xff;
224 rq->cmd[9] = h->bufflen & 0xff;
225 rq->cmd_len = COMMAND_SIZE(MAINTENANCE_IN);
227 rq->sense = h->sense;
228 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
229 rq->sense_len = h->senselen = 0;
231 err = blk_execute_rq(rq->q, NULL, rq, 1);
232 if (err == -EIO) {
233 sdev_printk(KERN_INFO, sdev,
234 "%s: rtpg failed with %x\n",
235 ALUA_DH_NAME, rq->errors);
236 h->senselen = rq->sense_len;
237 err = SCSI_DH_IO;
239 blk_put_request(rq);
240 done:
241 return err;
245 * alua_stpg - Evaluate SET TARGET GROUP STATES
246 * @sdev: the device to be evaluated
247 * @state: the new target group state
249 * Send a SET TARGET GROUP STATES command to the device.
250 * We only have to test here if we should resubmit the command;
251 * any other error is assumed as a failure.
253 static void stpg_endio(struct request *req, int error)
255 struct alua_dh_data *h = req->end_io_data;
256 struct scsi_sense_hdr sense_hdr;
257 unsigned err = SCSI_DH_OK;
259 if (error || host_byte(req->errors) != DID_OK ||
260 msg_byte(req->errors) != COMMAND_COMPLETE) {
261 err = SCSI_DH_IO;
262 goto done;
265 if (h->senselen > 0) {
266 err = scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
267 &sense_hdr);
268 if (!err) {
269 err = SCSI_DH_IO;
270 goto done;
272 err = alua_check_sense(h->sdev, &sense_hdr);
273 if (err == ADD_TO_MLQUEUE) {
274 err = SCSI_DH_RETRY;
275 goto done;
277 sdev_printk(KERN_INFO, h->sdev,
278 "%s: stpg sense code: %02x/%02x/%02x\n",
279 ALUA_DH_NAME, sense_hdr.sense_key,
280 sense_hdr.asc, sense_hdr.ascq);
281 err = SCSI_DH_IO;
283 if (err == SCSI_DH_OK) {
284 h->state = TPGS_STATE_OPTIMIZED;
285 sdev_printk(KERN_INFO, h->sdev,
286 "%s: port group %02x switched to state %c\n",
287 ALUA_DH_NAME, h->group_id,
288 print_alua_state(h->state));
290 done:
291 req->end_io_data = NULL;
292 __blk_put_request(req->q, req);
293 if (h->callback_fn) {
294 h->callback_fn(h->callback_data, err);
295 h->callback_fn = h->callback_data = NULL;
297 return;
301 * submit_stpg - Issue a SET TARGET GROUP STATES command
303 * Currently we're only setting the current target port group state
304 * to 'active/optimized' and let the array firmware figure out
305 * the states of the remaining groups.
307 static unsigned submit_stpg(struct alua_dh_data *h)
309 struct request *rq;
310 int stpg_len = 8;
311 struct scsi_device *sdev = h->sdev;
313 /* Prepare the data buffer */
314 memset(h->buff, 0, stpg_len);
315 h->buff[4] = TPGS_STATE_OPTIMIZED & 0x0f;
316 h->buff[6] = (h->group_id >> 8) & 0xff;
317 h->buff[7] = h->group_id & 0xff;
319 rq = get_alua_req(sdev, h->buff, stpg_len, WRITE);
320 if (!rq)
321 return SCSI_DH_RES_TEMP_UNAVAIL;
323 /* Prepare the command. */
324 rq->cmd[0] = MAINTENANCE_OUT;
325 rq->cmd[1] = MO_SET_TARGET_PGS;
326 rq->cmd[6] = (stpg_len >> 24) & 0xff;
327 rq->cmd[7] = (stpg_len >> 16) & 0xff;
328 rq->cmd[8] = (stpg_len >> 8) & 0xff;
329 rq->cmd[9] = stpg_len & 0xff;
330 rq->cmd_len = COMMAND_SIZE(MAINTENANCE_OUT);
332 rq->sense = h->sense;
333 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
334 rq->sense_len = h->senselen = 0;
335 rq->end_io_data = h;
337 blk_execute_rq_nowait(rq->q, NULL, rq, 1, stpg_endio);
338 return SCSI_DH_OK;
342 * alua_std_inquiry - Evaluate standard INQUIRY command
343 * @sdev: device to be checked
345 * Just extract the TPGS setting to find out if ALUA
346 * is supported.
348 static int alua_std_inquiry(struct scsi_device *sdev, struct alua_dh_data *h)
350 int err;
352 err = submit_std_inquiry(sdev, h);
354 if (err != SCSI_DH_OK)
355 return err;
357 /* Check TPGS setting */
358 h->tpgs = (h->inq[5] >> 4) & 0x3;
359 switch (h->tpgs) {
360 case TPGS_MODE_EXPLICIT|TPGS_MODE_IMPLICIT:
361 sdev_printk(KERN_INFO, sdev,
362 "%s: supports implicit and explicit TPGS\n",
363 ALUA_DH_NAME);
364 break;
365 case TPGS_MODE_EXPLICIT:
366 sdev_printk(KERN_INFO, sdev, "%s: supports explicit TPGS\n",
367 ALUA_DH_NAME);
368 break;
369 case TPGS_MODE_IMPLICIT:
370 sdev_printk(KERN_INFO, sdev, "%s: supports implicit TPGS\n",
371 ALUA_DH_NAME);
372 break;
373 default:
374 h->tpgs = TPGS_MODE_NONE;
375 sdev_printk(KERN_INFO, sdev, "%s: not supported\n",
376 ALUA_DH_NAME);
377 err = SCSI_DH_DEV_UNSUPP;
378 break;
381 return err;
385 * alua_vpd_inquiry - Evaluate INQUIRY vpd page 0x83
386 * @sdev: device to be checked
388 * Extract the relative target port and the target port group
389 * descriptor from the list of identificators.
391 static int alua_vpd_inquiry(struct scsi_device *sdev, struct alua_dh_data *h)
393 int len;
394 unsigned err;
395 unsigned char *d;
397 retry:
398 err = submit_vpd_inquiry(sdev, h);
400 if (err != SCSI_DH_OK)
401 return err;
403 /* Check if vpd page exceeds initial buffer */
404 len = (h->buff[2] << 8) + h->buff[3] + 4;
405 if (len > h->bufflen) {
406 /* Resubmit with the correct length */
407 if (realloc_buffer(h, len)) {
408 sdev_printk(KERN_WARNING, sdev,
409 "%s: kmalloc buffer failed\n",
410 ALUA_DH_NAME);
411 /* Temporary failure, bypass */
412 return SCSI_DH_DEV_TEMP_BUSY;
414 goto retry;
418 * Now look for the correct descriptor.
420 d = h->buff + 4;
421 while (d < h->buff + len) {
422 switch (d[1] & 0xf) {
423 case 0x4:
424 /* Relative target port */
425 h->rel_port = (d[6] << 8) + d[7];
426 break;
427 case 0x5:
428 /* Target port group */
429 h->group_id = (d[6] << 8) + d[7];
430 break;
431 default:
432 break;
434 d += d[3] + 4;
437 if (h->group_id == -1) {
439 * Internal error; TPGS supported but required
440 * VPD identification descriptors not present.
441 * Disable ALUA support
443 sdev_printk(KERN_INFO, sdev,
444 "%s: No target port descriptors found\n",
445 ALUA_DH_NAME);
446 h->state = TPGS_STATE_OPTIMIZED;
447 h->tpgs = TPGS_MODE_NONE;
448 err = SCSI_DH_DEV_UNSUPP;
449 } else {
450 sdev_printk(KERN_INFO, sdev,
451 "%s: port group %02x rel port %02x\n",
452 ALUA_DH_NAME, h->group_id, h->rel_port);
455 return err;
458 static char print_alua_state(int state)
460 switch (state) {
461 case TPGS_STATE_OPTIMIZED:
462 return 'A';
463 case TPGS_STATE_NONOPTIMIZED:
464 return 'N';
465 case TPGS_STATE_STANDBY:
466 return 'S';
467 case TPGS_STATE_UNAVAILABLE:
468 return 'U';
469 case TPGS_STATE_LBA_DEPENDENT:
470 return 'L';
471 case TPGS_STATE_OFFLINE:
472 return 'O';
473 case TPGS_STATE_TRANSITIONING:
474 return 'T';
475 default:
476 return 'X';
480 static int alua_check_sense(struct scsi_device *sdev,
481 struct scsi_sense_hdr *sense_hdr)
483 switch (sense_hdr->sense_key) {
484 case NOT_READY:
485 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0a)
487 * LUN Not Accessible - ALUA state transition
489 return ADD_TO_MLQUEUE;
490 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0b)
492 * LUN Not Accessible -- Target port in standby state
494 return SUCCESS;
495 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0c)
497 * LUN Not Accessible -- Target port in unavailable state
499 return SUCCESS;
500 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x12)
502 * LUN Not Ready -- Offline
504 return SUCCESS;
505 break;
506 case UNIT_ATTENTION:
507 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
509 * Power On, Reset, or Bus Device Reset, just retry.
511 return ADD_TO_MLQUEUE;
512 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06) {
514 * ALUA state changed
516 return ADD_TO_MLQUEUE;
518 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07) {
520 * Implicit ALUA state transition failed
522 return ADD_TO_MLQUEUE;
524 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x0e) {
526 * REPORTED_LUNS_DATA_HAS_CHANGED is reported
527 * when switching controllers on targets like
528 * Intel Multi-Flex. We can just retry.
530 return ADD_TO_MLQUEUE;
533 break;
536 return SCSI_RETURN_NOT_HANDLED;
540 * alua_rtpg - Evaluate REPORT TARGET GROUP STATES
541 * @sdev: the device to be evaluated.
543 * Evaluate the Target Port Group State.
544 * Returns SCSI_DH_DEV_OFFLINED if the path is
545 * found to be unusable.
547 static int alua_rtpg(struct scsi_device *sdev, struct alua_dh_data *h)
549 struct scsi_sense_hdr sense_hdr;
550 int len, k, off, valid_states = 0;
551 char *ucp;
552 unsigned err;
553 unsigned long expiry, interval = 10;
555 expiry = round_jiffies_up(jiffies + ALUA_FAILOVER_TIMEOUT);
556 retry:
557 err = submit_rtpg(sdev, h);
559 if (err == SCSI_DH_IO && h->senselen > 0) {
560 err = scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
561 &sense_hdr);
562 if (!err)
563 return SCSI_DH_IO;
565 err = alua_check_sense(sdev, &sense_hdr);
566 if (err == ADD_TO_MLQUEUE && time_before(jiffies, expiry))
567 goto retry;
568 sdev_printk(KERN_INFO, sdev,
569 "%s: rtpg sense code %02x/%02x/%02x\n",
570 ALUA_DH_NAME, sense_hdr.sense_key,
571 sense_hdr.asc, sense_hdr.ascq);
572 err = SCSI_DH_IO;
574 if (err != SCSI_DH_OK)
575 return err;
577 len = (h->buff[0] << 24) + (h->buff[1] << 16) +
578 (h->buff[2] << 8) + h->buff[3] + 4;
580 if (len > h->bufflen) {
581 /* Resubmit with the correct length */
582 if (realloc_buffer(h, len)) {
583 sdev_printk(KERN_WARNING, sdev,
584 "%s: kmalloc buffer failed\n",__func__);
585 /* Temporary failure, bypass */
586 return SCSI_DH_DEV_TEMP_BUSY;
588 goto retry;
591 for (k = 4, ucp = h->buff + 4; k < len; k += off, ucp += off) {
592 if (h->group_id == (ucp[2] << 8) + ucp[3]) {
593 h->state = ucp[0] & 0x0f;
594 valid_states = ucp[1];
596 off = 8 + (ucp[7] * 4);
599 sdev_printk(KERN_INFO, sdev,
600 "%s: port group %02x state %c supports %c%c%c%c%c%c%c\n",
601 ALUA_DH_NAME, h->group_id, print_alua_state(h->state),
602 valid_states&TPGS_SUPPORT_TRANSITION?'T':'t',
603 valid_states&TPGS_SUPPORT_OFFLINE?'O':'o',
604 valid_states&TPGS_SUPPORT_LBA_DEPENDENT?'L':'l',
605 valid_states&TPGS_SUPPORT_UNAVAILABLE?'U':'u',
606 valid_states&TPGS_SUPPORT_STANDBY?'S':'s',
607 valid_states&TPGS_SUPPORT_NONOPTIMIZED?'N':'n',
608 valid_states&TPGS_SUPPORT_OPTIMIZED?'A':'a');
610 switch (h->state) {
611 case TPGS_STATE_TRANSITIONING:
612 if (time_before(jiffies, expiry)) {
613 /* State transition, retry */
614 interval *= 10;
615 msleep(interval);
616 goto retry;
618 /* Transitioning time exceeded, set port to standby */
619 err = SCSI_DH_RETRY;
620 h->state = TPGS_STATE_STANDBY;
621 break;
622 case TPGS_STATE_OFFLINE:
623 case TPGS_STATE_UNAVAILABLE:
624 /* Path unusable for unavailable/offline */
625 err = SCSI_DH_DEV_OFFLINED;
626 break;
627 default:
628 /* Useable path if active */
629 err = SCSI_DH_OK;
630 break;
632 return err;
636 * alua_initialize - Initialize ALUA state
637 * @sdev: the device to be initialized
639 * For the prep_fn to work correctly we have
640 * to initialize the ALUA state for the device.
642 static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h)
644 int err;
646 err = alua_std_inquiry(sdev, h);
647 if (err != SCSI_DH_OK)
648 goto out;
650 err = alua_vpd_inquiry(sdev, h);
651 if (err != SCSI_DH_OK)
652 goto out;
654 err = alua_rtpg(sdev, h);
655 if (err != SCSI_DH_OK)
656 goto out;
658 out:
659 return err;
663 * alua_activate - activate a path
664 * @sdev: device on the path to be activated
666 * We're currently switching the port group to be activated only and
667 * let the array figure out the rest.
668 * There may be other arrays which require us to switch all port groups
669 * based on a certain policy. But until we actually encounter them it
670 * should be okay.
672 static int alua_activate(struct scsi_device *sdev,
673 activate_complete fn, void *data)
675 struct alua_dh_data *h = get_alua_data(sdev);
676 int err = SCSI_DH_OK;
678 if (h->group_id != -1) {
679 err = alua_rtpg(sdev, h);
680 if (err != SCSI_DH_OK)
681 goto out;
684 if (h->tpgs & TPGS_MODE_EXPLICIT &&
685 h->state != TPGS_STATE_OPTIMIZED &&
686 h->state != TPGS_STATE_LBA_DEPENDENT) {
687 h->callback_fn = fn;
688 h->callback_data = data;
689 err = submit_stpg(h);
690 if (err == SCSI_DH_OK)
691 return 0;
692 h->callback_fn = h->callback_data = NULL;
695 out:
696 if (fn)
697 fn(data, err);
698 return 0;
702 * alua_prep_fn - request callback
704 * Fail I/O to all paths not in state
705 * active/optimized or active/non-optimized.
707 static int alua_prep_fn(struct scsi_device *sdev, struct request *req)
709 struct alua_dh_data *h = get_alua_data(sdev);
710 int ret = BLKPREP_OK;
712 if (h->state == TPGS_STATE_TRANSITIONING)
713 ret = BLKPREP_DEFER;
714 else if (h->state != TPGS_STATE_OPTIMIZED &&
715 h->state != TPGS_STATE_NONOPTIMIZED &&
716 h->state != TPGS_STATE_LBA_DEPENDENT) {
717 ret = BLKPREP_KILL;
718 req->cmd_flags |= REQ_QUIET;
720 return ret;
724 static const struct scsi_dh_devlist alua_dev_list[] = {
725 {"HP", "MSA VOLUME" },
726 {"HP", "HSV101" },
727 {"HP", "HSV111" },
728 {"HP", "HSV200" },
729 {"HP", "HSV210" },
730 {"HP", "HSV300" },
731 {"IBM", "2107900" },
732 {"IBM", "2145" },
733 {"Pillar", "Axiom" },
734 {"Intel", "Multi-Flex"},
735 {"NETAPP", "LUN"},
736 {"NETAPP", "LUN C-Mode"},
737 {"AIX", "NVDISK"},
738 {"Promise", "VTrak"},
739 {NULL, NULL}
742 static int alua_bus_attach(struct scsi_device *sdev);
743 static void alua_bus_detach(struct scsi_device *sdev);
745 static struct scsi_device_handler alua_dh = {
746 .name = ALUA_DH_NAME,
747 .module = THIS_MODULE,
748 .devlist = alua_dev_list,
749 .attach = alua_bus_attach,
750 .detach = alua_bus_detach,
751 .prep_fn = alua_prep_fn,
752 .check_sense = alua_check_sense,
753 .activate = alua_activate,
757 * alua_bus_attach - Attach device handler
758 * @sdev: device to be attached to
760 static int alua_bus_attach(struct scsi_device *sdev)
762 struct scsi_dh_data *scsi_dh_data;
763 struct alua_dh_data *h;
764 unsigned long flags;
765 int err = SCSI_DH_OK;
767 scsi_dh_data = kzalloc(sizeof(*scsi_dh_data)
768 + sizeof(*h) , GFP_KERNEL);
769 if (!scsi_dh_data) {
770 sdev_printk(KERN_ERR, sdev, "%s: Attach failed\n",
771 ALUA_DH_NAME);
772 return -ENOMEM;
775 scsi_dh_data->scsi_dh = &alua_dh;
776 h = (struct alua_dh_data *) scsi_dh_data->buf;
777 h->tpgs = TPGS_MODE_UNINITIALIZED;
778 h->state = TPGS_STATE_OPTIMIZED;
779 h->group_id = -1;
780 h->rel_port = -1;
781 h->buff = h->inq;
782 h->bufflen = ALUA_INQUIRY_SIZE;
783 h->sdev = sdev;
785 err = alua_initialize(sdev, h);
786 if ((err != SCSI_DH_OK) && (err != SCSI_DH_DEV_OFFLINED))
787 goto failed;
789 if (!try_module_get(THIS_MODULE))
790 goto failed;
792 spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
793 sdev->scsi_dh_data = scsi_dh_data;
794 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
796 return 0;
798 failed:
799 kfree(scsi_dh_data);
800 sdev_printk(KERN_ERR, sdev, "%s: not attached\n", ALUA_DH_NAME);
801 return -EINVAL;
805 * alua_bus_detach - Detach device handler
806 * @sdev: device to be detached from
808 static void alua_bus_detach(struct scsi_device *sdev)
810 struct scsi_dh_data *scsi_dh_data;
811 struct alua_dh_data *h;
812 unsigned long flags;
814 spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
815 scsi_dh_data = sdev->scsi_dh_data;
816 sdev->scsi_dh_data = NULL;
817 spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
819 h = (struct alua_dh_data *) scsi_dh_data->buf;
820 if (h->buff && h->inq != h->buff)
821 kfree(h->buff);
822 kfree(scsi_dh_data);
823 module_put(THIS_MODULE);
824 sdev_printk(KERN_NOTICE, sdev, "%s: Detached\n", ALUA_DH_NAME);
827 static int __init alua_init(void)
829 int r;
831 r = scsi_register_device_handler(&alua_dh);
832 if (r != 0)
833 printk(KERN_ERR "%s: Failed to register scsi device handler",
834 ALUA_DH_NAME);
835 return r;
838 static void __exit alua_exit(void)
840 scsi_unregister_device_handler(&alua_dh);
843 module_init(alua_init);
844 module_exit(alua_exit);
846 MODULE_DESCRIPTION("DM Multipath ALUA support");
847 MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
848 MODULE_LICENSE("GPL");
849 MODULE_VERSION(ALUA_DH_VER);