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