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