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