scsi_dh_alua: Disable ALUA handling for non-disk devices
[linux-2.6-block.git] / drivers / scsi / device_handler / scsi_dh_alua.c
CommitLineData
057ea7c9
HR
1/*
2 * Generic SCSI-3 ALUA SCSI Device Handler
3 *
69723d17 4 * Copyright (C) 2007-2010 Hannes Reinecke, SUSE Linux Products GmbH.
057ea7c9
HR
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 */
5a0e3ad6 22#include <linux/slab.h>
69723d17 23#include <linux/delay.h>
acf3368f 24#include <linux/module.h>
057ea7c9
HR
25#include <scsi/scsi.h>
26#include <scsi/scsi_eh.h>
27#include <scsi/scsi_dh.h>
28
29#define ALUA_DH_NAME "alua"
69723d17 30#define ALUA_DH_VER "1.3"
057ea7c9
HR
31
32#define TPGS_STATE_OPTIMIZED 0x0
33#define TPGS_STATE_NONOPTIMIZED 0x1
34#define TPGS_STATE_STANDBY 0x2
35#define TPGS_STATE_UNAVAILABLE 0x3
69723d17 36#define TPGS_STATE_LBA_DEPENDENT 0x4
057ea7c9
HR
37#define TPGS_STATE_OFFLINE 0xe
38#define TPGS_STATE_TRANSITIONING 0xf
39
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
69723d17 45#define TPGS_SUPPORT_LBA_DEPENDENT 0x10
057ea7c9
HR
46#define TPGS_SUPPORT_OFFLINE 0x40
47#define TPGS_SUPPORT_TRANSITION 0x80
48
3588c5a2
RE
49#define RTPG_FMT_MASK 0x70
50#define RTPG_FMT_EXT_HDR 0x10
51
057ea7c9
HR
52#define TPGS_MODE_UNINITIALIZED -1
53#define TPGS_MODE_NONE 0x0
54#define TPGS_MODE_IMPLICIT 0x1
55#define TPGS_MODE_EXPLICIT 0x2
56
57#define ALUA_INQUIRY_SIZE 36
3588c5a2 58#define ALUA_FAILOVER_TIMEOUT 60
057ea7c9
HR
59#define ALUA_FAILOVER_RETRIES 5
60
4335d092
MB
61/* flags passed from user level */
62#define ALUA_OPTIMIZE_STPG 1
63
057ea7c9
HR
64struct alua_dh_data {
65 int group_id;
66 int rel_port;
67 int tpgs;
68 int state;
dcd3a754 69 int pref;
4335d092 70 unsigned flags; /* used for optimizing STPG */
057ea7c9
HR
71 unsigned char inq[ALUA_INQUIRY_SIZE];
72 unsigned char *buff;
73 int bufflen;
3588c5a2 74 unsigned char transition_tmo;
057ea7c9
HR
75 unsigned char sense[SCSI_SENSE_BUFFERSIZE];
76 int senselen;
96e65865
CS
77 struct scsi_device *sdev;
78 activate_complete callback_fn;
79 void *callback_data;
057ea7c9
HR
80};
81
82#define ALUA_POLICY_SWITCH_CURRENT 0
83#define ALUA_POLICY_SWITCH_ALL 1
84
96e65865
CS
85static char print_alua_state(int);
86static int alua_check_sense(struct scsi_device *, struct scsi_sense_hdr *);
87
057ea7c9
HR
88static int realloc_buffer(struct alua_dh_data *h, unsigned len)
89{
90 if (h->buff && h->buff != h->inq)
91 kfree(h->buff);
92
93 h->buff = kmalloc(len, GFP_NOIO);
94 if (!h->buff) {
95 h->buff = h->inq;
96 h->bufflen = ALUA_INQUIRY_SIZE;
97 return 1;
98 }
99 h->bufflen = len;
100 return 0;
101}
102
103static struct request *get_alua_req(struct scsi_device *sdev,
104 void *buffer, unsigned buflen, int rw)
105{
106 struct request *rq;
107 struct request_queue *q = sdev->request_queue;
108
109 rq = blk_get_request(q, rw, GFP_NOIO);
110
a492f075 111 if (IS_ERR(rq)) {
057ea7c9 112 sdev_printk(KERN_INFO, sdev,
cadbd4a5 113 "%s: blk_get_request failed\n", __func__);
057ea7c9
HR
114 return NULL;
115 }
f27b087b 116 blk_rq_set_block_pc(rq);
057ea7c9
HR
117
118 if (buflen && blk_rq_map_kern(q, rq, buffer, buflen, GFP_NOIO)) {
119 blk_put_request(rq);
120 sdev_printk(KERN_INFO, sdev,
cadbd4a5 121 "%s: blk_rq_map_kern failed\n", __func__);
057ea7c9
HR
122 return NULL;
123 }
124
6000a368 125 rq->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT |
64f84bc1 126 REQ_FAILFAST_DRIVER;
057ea7c9 127 rq->retries = ALUA_FAILOVER_RETRIES;
3588c5a2 128 rq->timeout = ALUA_FAILOVER_TIMEOUT * HZ;
057ea7c9
HR
129
130 return rq;
131}
132
057ea7c9
HR
133/*
134 * submit_vpd_inquiry - Issue an INQUIRY VPD page 0x83 command
135 * @sdev: sdev the command should be sent to
136 */
137static int submit_vpd_inquiry(struct scsi_device *sdev, struct alua_dh_data *h)
138{
139 struct request *rq;
140 int err = SCSI_DH_RES_TEMP_UNAVAIL;
141
142 rq = get_alua_req(sdev, h->buff, h->bufflen, READ);
143 if (!rq)
144 goto done;
145
146 /* Prepare the command. */
147 rq->cmd[0] = INQUIRY;
148 rq->cmd[1] = 1;
149 rq->cmd[2] = 0x83;
150 rq->cmd[4] = h->bufflen;
151 rq->cmd_len = COMMAND_SIZE(INQUIRY);
152
153 rq->sense = h->sense;
154 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
155 rq->sense_len = h->senselen = 0;
156
157 err = blk_execute_rq(rq->q, NULL, rq, 1);
158 if (err == -EIO) {
159 sdev_printk(KERN_INFO, sdev,
160 "%s: evpd inquiry failed with %x\n",
161 ALUA_DH_NAME, rq->errors);
162 h->senselen = rq->sense_len;
163 err = SCSI_DH_IO;
164 }
165 blk_put_request(rq);
166done:
167 return err;
168}
169
170/*
171 * submit_rtpg - Issue a REPORT TARGET GROUP STATES command
172 * @sdev: sdev the command should be sent to
173 */
8e67ce60
RE
174static unsigned submit_rtpg(struct scsi_device *sdev, struct alua_dh_data *h,
175 bool rtpg_ext_hdr_req)
057ea7c9
HR
176{
177 struct request *rq;
178 int err = SCSI_DH_RES_TEMP_UNAVAIL;
179
180 rq = get_alua_req(sdev, h->buff, h->bufflen, READ);
181 if (!rq)
182 goto done;
183
184 /* Prepare the command. */
185 rq->cmd[0] = MAINTENANCE_IN;
8e67ce60
RE
186 if (rtpg_ext_hdr_req)
187 rq->cmd[1] = MI_REPORT_TARGET_PGS | MI_EXT_HDR_PARAM_FMT;
188 else
189 rq->cmd[1] = MI_REPORT_TARGET_PGS;
057ea7c9
HR
190 rq->cmd[6] = (h->bufflen >> 24) & 0xff;
191 rq->cmd[7] = (h->bufflen >> 16) & 0xff;
192 rq->cmd[8] = (h->bufflen >> 8) & 0xff;
193 rq->cmd[9] = h->bufflen & 0xff;
194 rq->cmd_len = COMMAND_SIZE(MAINTENANCE_IN);
195
196 rq->sense = h->sense;
197 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
198 rq->sense_len = h->senselen = 0;
199
200 err = blk_execute_rq(rq->q, NULL, rq, 1);
201 if (err == -EIO) {
202 sdev_printk(KERN_INFO, sdev,
203 "%s: rtpg failed with %x\n",
204 ALUA_DH_NAME, rq->errors);
205 h->senselen = rq->sense_len;
206 err = SCSI_DH_IO;
207 }
208 blk_put_request(rq);
209done:
210 return err;
211}
212
96e65865
CS
213/*
214 * alua_stpg - Evaluate SET TARGET GROUP STATES
215 * @sdev: the device to be evaluated
216 * @state: the new target group state
217 *
218 * Send a SET TARGET GROUP STATES command to the device.
219 * We only have to test here if we should resubmit the command;
220 * any other error is assumed as a failure.
221 */
222static void stpg_endio(struct request *req, int error)
223{
224 struct alua_dh_data *h = req->end_io_data;
225 struct scsi_sense_hdr sense_hdr;
9349923d 226 unsigned err = SCSI_DH_OK;
96e65865 227
27db682b
MC
228 if (host_byte(req->errors) != DID_OK ||
229 msg_byte(req->errors) != COMMAND_COMPLETE) {
9349923d 230 err = SCSI_DH_IO;
96e65865 231 goto done;
9349923d 232 }
96e65865 233
27db682b 234 if (req->sense_len > 0) {
96e65865
CS
235 err = scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
236 &sense_hdr);
237 if (!err) {
238 err = SCSI_DH_IO;
239 goto done;
240 }
241 err = alua_check_sense(h->sdev, &sense_hdr);
242 if (err == ADD_TO_MLQUEUE) {
243 err = SCSI_DH_RETRY;
244 goto done;
245 }
246 sdev_printk(KERN_INFO, h->sdev,
247 "%s: stpg sense code: %02x/%02x/%02x\n",
248 ALUA_DH_NAME, sense_hdr.sense_key,
249 sense_hdr.asc, sense_hdr.ascq);
250 err = SCSI_DH_IO;
27db682b
MC
251 } else if (error)
252 err = SCSI_DH_IO;
253
96e65865
CS
254 if (err == SCSI_DH_OK) {
255 h->state = TPGS_STATE_OPTIMIZED;
256 sdev_printk(KERN_INFO, h->sdev,
257 "%s: port group %02x switched to state %c\n",
258 ALUA_DH_NAME, h->group_id,
259 print_alua_state(h->state));
260 }
261done:
ed0f36bc
JG
262 req->end_io_data = NULL;
263 __blk_put_request(req->q, req);
96e65865
CS
264 if (h->callback_fn) {
265 h->callback_fn(h->callback_data, err);
266 h->callback_fn = h->callback_data = NULL;
267 }
268 return;
269}
270
057ea7c9
HR
271/*
272 * submit_stpg - Issue a SET TARGET GROUP STATES command
057ea7c9
HR
273 *
274 * Currently we're only setting the current target port group state
275 * to 'active/optimized' and let the array firmware figure out
276 * the states of the remaining groups.
277 */
96e65865 278static unsigned submit_stpg(struct alua_dh_data *h)
057ea7c9
HR
279{
280 struct request *rq;
057ea7c9 281 int stpg_len = 8;
96e65865 282 struct scsi_device *sdev = h->sdev;
057ea7c9
HR
283
284 /* Prepare the data buffer */
285 memset(h->buff, 0, stpg_len);
286 h->buff[4] = TPGS_STATE_OPTIMIZED & 0x0f;
ef3fa8c6
IH
287 h->buff[6] = (h->group_id >> 8) & 0xff;
288 h->buff[7] = h->group_id & 0xff;
057ea7c9
HR
289
290 rq = get_alua_req(sdev, h->buff, stpg_len, WRITE);
291 if (!rq)
96e65865 292 return SCSI_DH_RES_TEMP_UNAVAIL;
057ea7c9
HR
293
294 /* Prepare the command. */
295 rq->cmd[0] = MAINTENANCE_OUT;
296 rq->cmd[1] = MO_SET_TARGET_PGS;
297 rq->cmd[6] = (stpg_len >> 24) & 0xff;
298 rq->cmd[7] = (stpg_len >> 16) & 0xff;
299 rq->cmd[8] = (stpg_len >> 8) & 0xff;
300 rq->cmd[9] = stpg_len & 0xff;
301 rq->cmd_len = COMMAND_SIZE(MAINTENANCE_OUT);
302
303 rq->sense = h->sense;
304 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE);
305 rq->sense_len = h->senselen = 0;
96e65865 306 rq->end_io_data = h;
057ea7c9 307
96e65865 308 blk_execute_rq_nowait(rq->q, NULL, rq, 1, stpg_endio);
7c66e9a5 309 return SCSI_DH_OK;
057ea7c9
HR
310}
311
312/*
d7c48feb 313 * alua_check_tpgs - Evaluate TPGS setting
057ea7c9
HR
314 * @sdev: device to be checked
315 *
d7c48feb 316 * Examine the TPGS setting of the sdev to find out if ALUA
057ea7c9
HR
317 * is supported.
318 */
d7c48feb 319static int alua_check_tpgs(struct scsi_device *sdev, struct alua_dh_data *h)
057ea7c9 320{
d7c48feb 321 int err = SCSI_DH_OK;
057ea7c9 322
db5a6a60
HR
323 /*
324 * ALUA support for non-disk devices is fraught with
325 * difficulties, so disable it for now.
326 */
327 if (sdev->type != TYPE_DISK) {
328 h->tpgs = TPGS_MODE_NONE;
329 sdev_printk(KERN_INFO, sdev,
330 "%s: disable for non-disk devices\n",
331 ALUA_DH_NAME);
332 return SCSI_DH_DEV_UNSUPP;
333 }
334
d7c48feb 335 h->tpgs = scsi_device_tpgs(sdev);
057ea7c9
HR
336 switch (h->tpgs) {
337 case TPGS_MODE_EXPLICIT|TPGS_MODE_IMPLICIT:
338 sdev_printk(KERN_INFO, sdev,
339 "%s: supports implicit and explicit TPGS\n",
340 ALUA_DH_NAME);
341 break;
342 case TPGS_MODE_EXPLICIT:
343 sdev_printk(KERN_INFO, sdev, "%s: supports explicit TPGS\n",
344 ALUA_DH_NAME);
345 break;
346 case TPGS_MODE_IMPLICIT:
347 sdev_printk(KERN_INFO, sdev, "%s: supports implicit TPGS\n",
348 ALUA_DH_NAME);
349 break;
350 default:
351 h->tpgs = TPGS_MODE_NONE;
352 sdev_printk(KERN_INFO, sdev, "%s: not supported\n",
353 ALUA_DH_NAME);
354 err = SCSI_DH_DEV_UNSUPP;
355 break;
356 }
357
358 return err;
359}
360
361/*
362 * alua_vpd_inquiry - Evaluate INQUIRY vpd page 0x83
363 * @sdev: device to be checked
364 *
365 * Extract the relative target port and the target port group
366 * descriptor from the list of identificators.
367 */
368static int alua_vpd_inquiry(struct scsi_device *sdev, struct alua_dh_data *h)
369{
370 int len;
371 unsigned err;
372 unsigned char *d;
373
374 retry:
375 err = submit_vpd_inquiry(sdev, h);
376
377 if (err != SCSI_DH_OK)
378 return err;
379
380 /* Check if vpd page exceeds initial buffer */
381 len = (h->buff[2] << 8) + h->buff[3] + 4;
382 if (len > h->bufflen) {
383 /* Resubmit with the correct length */
384 if (realloc_buffer(h, len)) {
385 sdev_printk(KERN_WARNING, sdev,
386 "%s: kmalloc buffer failed\n",
387 ALUA_DH_NAME);
388 /* Temporary failure, bypass */
389 return SCSI_DH_DEV_TEMP_BUSY;
390 }
391 goto retry;
392 }
393
394 /*
395 * Now look for the correct descriptor.
396 */
397 d = h->buff + 4;
398 while (d < h->buff + len) {
399 switch (d[1] & 0xf) {
400 case 0x4:
401 /* Relative target port */
402 h->rel_port = (d[6] << 8) + d[7];
403 break;
404 case 0x5:
405 /* Target port group */
406 h->group_id = (d[6] << 8) + d[7];
407 break;
408 default:
409 break;
410 }
411 d += d[3] + 4;
412 }
413
414 if (h->group_id == -1) {
415 /*
416 * Internal error; TPGS supported but required
417 * VPD identification descriptors not present.
418 * Disable ALUA support
419 */
420 sdev_printk(KERN_INFO, sdev,
421 "%s: No target port descriptors found\n",
422 ALUA_DH_NAME);
423 h->state = TPGS_STATE_OPTIMIZED;
424 h->tpgs = TPGS_MODE_NONE;
425 err = SCSI_DH_DEV_UNSUPP;
426 } else {
427 sdev_printk(KERN_INFO, sdev,
428 "%s: port group %02x rel port %02x\n",
429 ALUA_DH_NAME, h->group_id, h->rel_port);
430 }
431
432 return err;
433}
434
435static char print_alua_state(int state)
436{
437 switch (state) {
438 case TPGS_STATE_OPTIMIZED:
439 return 'A';
440 case TPGS_STATE_NONOPTIMIZED:
441 return 'N';
442 case TPGS_STATE_STANDBY:
443 return 'S';
444 case TPGS_STATE_UNAVAILABLE:
445 return 'U';
69723d17
HR
446 case TPGS_STATE_LBA_DEPENDENT:
447 return 'L';
057ea7c9
HR
448 case TPGS_STATE_OFFLINE:
449 return 'O';
450 case TPGS_STATE_TRANSITIONING:
451 return 'T';
452 default:
453 return 'X';
454 }
455}
456
457static int alua_check_sense(struct scsi_device *sdev,
458 struct scsi_sense_hdr *sense_hdr)
459{
460 switch (sense_hdr->sense_key) {
461 case NOT_READY:
462 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0a)
463 /*
464 * LUN Not Accessible - ALUA state transition
465 */
c7dbb627 466 return ADD_TO_MLQUEUE;
057ea7c9
HR
467 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0b)
468 /*
469 * LUN Not Accessible -- Target port in standby state
470 */
471 return SUCCESS;
472 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x0c)
473 /*
474 * LUN Not Accessible -- Target port in unavailable state
475 */
476 return SUCCESS;
477 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x12)
478 /*
479 * LUN Not Ready -- Offline
480 */
481 return SUCCESS;
333b2448 482 if (sdev->allow_restart &&
483 sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x02)
484 /*
485 * if the device is not started, we need to wake
486 * the error handler to start the motor
487 */
488 return FAILED;
057ea7c9
HR
489 break;
490 case UNIT_ATTENTION:
491 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00)
492 /*
493 * Power On, Reset, or Bus Device Reset, just retry.
494 */
c7dbb627 495 return ADD_TO_MLQUEUE;
c20ee7b5
SS
496 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x04)
497 /*
498 * Device internal reset
499 */
500 return ADD_TO_MLQUEUE;
410f02d8
MB
501 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x01)
502 /*
503 * Mode Parameters Changed
504 */
505 return ADD_TO_MLQUEUE;
bf81973a 506 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x06)
057ea7c9
HR
507 /*
508 * ALUA state changed
509 */
c7dbb627 510 return ADD_TO_MLQUEUE;
bf81973a 511 if (sense_hdr->asc == 0x2a && sense_hdr->ascq == 0x07)
057ea7c9
HR
512 /*
513 * Implicit ALUA state transition failed
514 */
c7dbb627 515 return ADD_TO_MLQUEUE;
bf81973a
MB
516 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x03)
517 /*
518 * Inquiry data has changed
519 */
520 return ADD_TO_MLQUEUE;
521 if (sense_hdr->asc == 0x3f && sense_hdr->ascq == 0x0e)
4d086f6b
IH
522 /*
523 * REPORTED_LUNS_DATA_HAS_CHANGED is reported
524 * when switching controllers on targets like
525 * Intel Multi-Flex. We can just retry.
526 */
527 return ADD_TO_MLQUEUE;
057ea7c9
HR
528 break;
529 }
530
531 return SCSI_RETURN_NOT_HANDLED;
532}
533
057ea7c9
HR
534/*
535 * alua_rtpg - Evaluate REPORT TARGET GROUP STATES
536 * @sdev: the device to be evaluated.
a8e5a2d5 537 * @wait_for_transition: if nonzero, wait ALUA_FAILOVER_TIMEOUT seconds for device to exit transitioning state
057ea7c9
HR
538 *
539 * Evaluate the Target Port Group State.
540 * Returns SCSI_DH_DEV_OFFLINED if the path is
25985edc 541 * found to be unusable.
057ea7c9 542 */
a8e5a2d5 543static int alua_rtpg(struct scsi_device *sdev, struct alua_dh_data *h, int wait_for_transition)
057ea7c9
HR
544{
545 struct scsi_sense_hdr sense_hdr;
546 int len, k, off, valid_states = 0;
cfde3fa1 547 unsigned char *ucp;
057ea7c9 548 unsigned err;
8e67ce60 549 bool rtpg_ext_hdr_req = 1;
bc97f4bb 550 unsigned long expiry, interval = 0;
3588c5a2
RE
551 unsigned int tpg_desc_tbl_off;
552 unsigned char orig_transition_tmo;
553
554 if (!h->transition_tmo)
555 expiry = round_jiffies_up(jiffies + ALUA_FAILOVER_TIMEOUT * HZ);
556 else
557 expiry = round_jiffies_up(jiffies + h->transition_tmo * HZ);
057ea7c9
HR
558
559 retry:
8e67ce60 560 err = submit_rtpg(sdev, h, rtpg_ext_hdr_req);
057ea7c9
HR
561
562 if (err == SCSI_DH_IO && h->senselen > 0) {
563 err = scsi_normalize_sense(h->sense, SCSI_SENSE_BUFFERSIZE,
564 &sense_hdr);
565 if (!err)
566 return SCSI_DH_IO;
567
8e67ce60
RE
568 /*
569 * submit_rtpg() has failed on existing arrays
570 * when requesting extended header info, and
571 * the array doesn't support extended headers,
572 * even though it shouldn't according to T10.
573 * The retry without rtpg_ext_hdr_req set
574 * handles this.
575 */
576 if (rtpg_ext_hdr_req == 1 &&
577 sense_hdr.sense_key == ILLEGAL_REQUEST &&
578 sense_hdr.asc == 0x24 && sense_hdr.ascq == 0) {
579 rtpg_ext_hdr_req = 0;
580 goto retry;
581 }
582
057ea7c9 583 err = alua_check_sense(sdev, &sense_hdr);
69723d17 584 if (err == ADD_TO_MLQUEUE && time_before(jiffies, expiry))
057ea7c9
HR
585 goto retry;
586 sdev_printk(KERN_INFO, sdev,
587 "%s: rtpg sense code %02x/%02x/%02x\n",
588 ALUA_DH_NAME, sense_hdr.sense_key,
589 sense_hdr.asc, sense_hdr.ascq);
590 err = SCSI_DH_IO;
591 }
592 if (err != SCSI_DH_OK)
593 return err;
594
595 len = (h->buff[0] << 24) + (h->buff[1] << 16) +
596 (h->buff[2] << 8) + h->buff[3] + 4;
597
598 if (len > h->bufflen) {
599 /* Resubmit with the correct length */
600 if (realloc_buffer(h, len)) {
601 sdev_printk(KERN_WARNING, sdev,
cadbd4a5 602 "%s: kmalloc buffer failed\n",__func__);
057ea7c9
HR
603 /* Temporary failure, bypass */
604 return SCSI_DH_DEV_TEMP_BUSY;
605 }
606 goto retry;
607 }
608
3588c5a2
RE
609 orig_transition_tmo = h->transition_tmo;
610 if ((h->buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR && h->buff[5] != 0)
611 h->transition_tmo = h->buff[5];
612 else
613 h->transition_tmo = ALUA_FAILOVER_TIMEOUT;
614
a8e5a2d5 615 if (wait_for_transition && (orig_transition_tmo != h->transition_tmo)) {
3588c5a2
RE
616 sdev_printk(KERN_INFO, sdev,
617 "%s: transition timeout set to %d seconds\n",
618 ALUA_DH_NAME, h->transition_tmo);
619 expiry = jiffies + h->transition_tmo * HZ;
620 }
621
622 if ((h->buff[4] & RTPG_FMT_MASK) == RTPG_FMT_EXT_HDR)
623 tpg_desc_tbl_off = 8;
624 else
625 tpg_desc_tbl_off = 4;
626
627 for (k = tpg_desc_tbl_off, ucp = h->buff + tpg_desc_tbl_off;
628 k < len;
629 k += off, ucp += off) {
630
057ea7c9
HR
631 if (h->group_id == (ucp[2] << 8) + ucp[3]) {
632 h->state = ucp[0] & 0x0f;
dcd3a754 633 h->pref = ucp[0] >> 7;
057ea7c9
HR
634 valid_states = ucp[1];
635 }
636 off = 8 + (ucp[7] * 4);
637 }
638
639 sdev_printk(KERN_INFO, sdev,
dcd3a754 640 "%s: port group %02x state %c %s supports %c%c%c%c%c%c%c\n",
057ea7c9 641 ALUA_DH_NAME, h->group_id, print_alua_state(h->state),
dcd3a754 642 h->pref ? "preferred" : "non-preferred",
057ea7c9
HR
643 valid_states&TPGS_SUPPORT_TRANSITION?'T':'t',
644 valid_states&TPGS_SUPPORT_OFFLINE?'O':'o',
69723d17 645 valid_states&TPGS_SUPPORT_LBA_DEPENDENT?'L':'l',
057ea7c9
HR
646 valid_states&TPGS_SUPPORT_UNAVAILABLE?'U':'u',
647 valid_states&TPGS_SUPPORT_STANDBY?'S':'s',
648 valid_states&TPGS_SUPPORT_NONOPTIMIZED?'N':'n',
649 valid_states&TPGS_SUPPORT_OPTIMIZED?'A':'a');
650
69723d17
HR
651 switch (h->state) {
652 case TPGS_STATE_TRANSITIONING:
a8e5a2d5
SS
653 if (wait_for_transition) {
654 if (time_before(jiffies, expiry)) {
655 /* State transition, retry */
656 interval += 2000;
657 msleep(interval);
658 goto retry;
659 }
660 err = SCSI_DH_RETRY;
661 } else {
662 err = SCSI_DH_OK;
057ea7c9 663 }
a8e5a2d5 664
69723d17 665 /* Transitioning time exceeded, set port to standby */
69723d17
HR
666 h->state = TPGS_STATE_STANDBY;
667 break;
668 case TPGS_STATE_OFFLINE:
e47f8976 669 /* Path unusable */
69723d17
HR
670 err = SCSI_DH_DEV_OFFLINED;
671 break;
672 default:
673 /* Useable path if active */
674 err = SCSI_DH_OK;
675 break;
057ea7c9
HR
676 }
677 return err;
678}
679
680/*
681 * alua_initialize - Initialize ALUA state
682 * @sdev: the device to be initialized
683 *
684 * For the prep_fn to work correctly we have
685 * to initialize the ALUA state for the device.
686 */
687static int alua_initialize(struct scsi_device *sdev, struct alua_dh_data *h)
688{
689 int err;
690
d7c48feb 691 err = alua_check_tpgs(sdev, h);
057ea7c9
HR
692 if (err != SCSI_DH_OK)
693 goto out;
694
695 err = alua_vpd_inquiry(sdev, h);
696 if (err != SCSI_DH_OK)
697 goto out;
698
a8e5a2d5 699 err = alua_rtpg(sdev, h, 0);
057ea7c9
HR
700 if (err != SCSI_DH_OK)
701 goto out;
702
703out:
704 return err;
705}
4335d092
MB
706/*
707 * alua_set_params - set/unset the optimize flag
708 * @sdev: device on the path to be activated
709 * params - parameters in the following format
710 * "no_of_params\0param1\0param2\0param3\0...\0"
711 * For example, to set the flag pass the following parameters
712 * from multipath.conf
713 * hardware_handler "2 alua 1"
714 */
715static int alua_set_params(struct scsi_device *sdev, const char *params)
716{
ee14c674 717 struct alua_dh_data *h = sdev->handler_data;
4335d092
MB
718 unsigned int optimize = 0, argc;
719 const char *p = params;
720 int result = SCSI_DH_OK;
721
722 if ((sscanf(params, "%u", &argc) != 1) || (argc != 1))
723 return -EINVAL;
724
725 while (*p++)
726 ;
727 if ((sscanf(p, "%u", &optimize) != 1) || (optimize > 1))
728 return -EINVAL;
729
730 if (optimize)
731 h->flags |= ALUA_OPTIMIZE_STPG;
732 else
733 h->flags &= ~ALUA_OPTIMIZE_STPG;
734
735 return result;
736}
057ea7c9 737
7a3ad392
SS
738static uint optimize_stpg;
739module_param(optimize_stpg, uint, S_IRUGO|S_IWUSR);
740MODULE_PARM_DESC(optimize_stpg, "Allow use of a non-optimized path, rather than sending a STPG, when implicit TPGS is supported (0=No,1=Yes). Default is 0.");
741
057ea7c9
HR
742/*
743 * alua_activate - activate a path
744 * @sdev: device on the path to be activated
745 *
746 * We're currently switching the port group to be activated only and
747 * let the array figure out the rest.
748 * There may be other arrays which require us to switch all port groups
749 * based on a certain policy. But until we actually encounter them it
750 * should be okay.
751 */
3ae31f6a
CS
752static int alua_activate(struct scsi_device *sdev,
753 activate_complete fn, void *data)
057ea7c9 754{
ee14c674 755 struct alua_dh_data *h = sdev->handler_data;
057ea7c9 756 int err = SCSI_DH_OK;
72d9e0f3 757 int stpg = 0;
057ea7c9 758
a8e5a2d5 759 err = alua_rtpg(sdev, h, 1);
46ccf6b5
HR
760 if (err != SCSI_DH_OK)
761 goto out;
057ea7c9 762
7a3ad392
SS
763 if (optimize_stpg)
764 h->flags |= ALUA_OPTIMIZE_STPG;
765
72d9e0f3
MB
766 if (h->tpgs & TPGS_MODE_EXPLICIT) {
767 switch (h->state) {
768 case TPGS_STATE_NONOPTIMIZED:
769 stpg = 1;
770 if ((h->flags & ALUA_OPTIMIZE_STPG) &&
771 (!h->pref) &&
772 (h->tpgs & TPGS_MODE_IMPLICIT))
773 stpg = 0;
774 break;
775 case TPGS_STATE_STANDBY:
bb2c94a3 776 case TPGS_STATE_UNAVAILABLE:
72d9e0f3
MB
777 stpg = 1;
778 break;
72d9e0f3
MB
779 case TPGS_STATE_OFFLINE:
780 err = SCSI_DH_IO;
781 break;
782 case TPGS_STATE_TRANSITIONING:
783 err = SCSI_DH_RETRY;
784 break;
785 default:
786 break;
787 }
788 }
789
790 if (stpg) {
96e65865
CS
791 h->callback_fn = fn;
792 h->callback_data = data;
793 err = submit_stpg(h);
794 if (err == SCSI_DH_OK)
795 return 0;
796 h->callback_fn = h->callback_data = NULL;
797 }
057ea7c9
HR
798
799out:
3ae31f6a
CS
800 if (fn)
801 fn(data, err);
802 return 0;
057ea7c9
HR
803}
804
805/*
806 * alua_prep_fn - request callback
807 *
808 * Fail I/O to all paths not in state
809 * active/optimized or active/non-optimized.
810 */
811static int alua_prep_fn(struct scsi_device *sdev, struct request *req)
812{
ee14c674 813 struct alua_dh_data *h = sdev->handler_data;
057ea7c9
HR
814 int ret = BLKPREP_OK;
815
69723d17
HR
816 if (h->state == TPGS_STATE_TRANSITIONING)
817 ret = BLKPREP_DEFER;
818 else if (h->state != TPGS_STATE_OPTIMIZED &&
819 h->state != TPGS_STATE_NONOPTIMIZED &&
820 h->state != TPGS_STATE_LBA_DEPENDENT) {
057ea7c9
HR
821 ret = BLKPREP_KILL;
822 req->cmd_flags |= REQ_QUIET;
823 }
824 return ret;
825
826}
827
057ea7c9
HR
828/*
829 * alua_bus_attach - Attach device handler
830 * @sdev: device to be attached to
831 */
ee14c674 832static int alua_bus_attach(struct scsi_device *sdev)
057ea7c9 833{
057ea7c9 834 struct alua_dh_data *h;
1d520328 835 int err;
057ea7c9 836
cd37743f 837 h = kzalloc(sizeof(*h) , GFP_KERNEL);
1d520328 838 if (!h)
ee14c674 839 return -ENOMEM;
057ea7c9
HR
840 h->tpgs = TPGS_MODE_UNINITIALIZED;
841 h->state = TPGS_STATE_OPTIMIZED;
842 h->group_id = -1;
843 h->rel_port = -1;
844 h->buff = h->inq;
845 h->bufflen = ALUA_INQUIRY_SIZE;
96e65865 846 h->sdev = sdev;
057ea7c9
HR
847
848 err = alua_initialize(sdev, h);
1d520328 849 if (err != SCSI_DH_OK && err != SCSI_DH_DEV_OFFLINED)
057ea7c9
HR
850 goto failed;
851
ee14c674
CH
852 sdev->handler_data = h;
853 return 0;
057ea7c9 854failed:
cd37743f 855 kfree(h);
ee14c674 856 return -EINVAL;
057ea7c9
HR
857}
858
859/*
860 * alua_bus_detach - Detach device handler
861 * @sdev: device to be detached from
862 */
863static void alua_bus_detach(struct scsi_device *sdev)
864{
ee14c674 865 struct alua_dh_data *h = sdev->handler_data;
057ea7c9 866
057ea7c9
HR
867 if (h->buff && h->inq != h->buff)
868 kfree(h->buff);
ee14c674 869 sdev->handler_data = NULL;
cd37743f 870 kfree(h);
057ea7c9
HR
871}
872
1d520328
CH
873static struct scsi_device_handler alua_dh = {
874 .name = ALUA_DH_NAME,
875 .module = THIS_MODULE,
876 .attach = alua_bus_attach,
877 .detach = alua_bus_detach,
878 .prep_fn = alua_prep_fn,
879 .check_sense = alua_check_sense,
880 .activate = alua_activate,
881 .set_params = alua_set_params,
1d520328
CH
882};
883
057ea7c9
HR
884static int __init alua_init(void)
885{
886 int r;
887
888 r = scsi_register_device_handler(&alua_dh);
889 if (r != 0)
890 printk(KERN_ERR "%s: Failed to register scsi device handler",
891 ALUA_DH_NAME);
892 return r;
893}
894
895static void __exit alua_exit(void)
896{
897 scsi_unregister_device_handler(&alua_dh);
898}
899
900module_init(alua_init);
901module_exit(alua_exit);
902
903MODULE_DESCRIPTION("DM Multipath ALUA support");
904MODULE_AUTHOR("Hannes Reinecke <hare@suse.de>");
905MODULE_LICENSE("GPL");
906MODULE_VERSION(ALUA_DH_VER);