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