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