1 /* 2 * Target driver for EMC CLARiiON AX/CX-series hardware. 3 * Based on code from Lars Marowsky-Bree <lmb@suse.de> 4 * and Ed Goggin <egoggin@emc.com>. 5 * 6 * Copyright (C) 2006 Red Hat, Inc. All rights reserved. 7 * Copyright (C) 2006 Mike Christie 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2, or (at your option) 12 * any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; see the file COPYING. If not, write to 21 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 22 */ 23 #include <linux/slab.h> 24 #include <linux/module.h> 25 #include <scsi/scsi.h> 26 #include <scsi/scsi_eh.h> 27 #include <scsi/scsi_dh.h> 28 #include <scsi/scsi_device.h> 29 30 #define CLARIION_NAME "emc" 31 32 #define CLARIION_TRESPASS_PAGE 0x22 33 #define CLARIION_BUFFER_SIZE 0xFC 34 #define CLARIION_TIMEOUT (60 * HZ) 35 #define CLARIION_RETRIES 3 36 #define CLARIION_UNBOUND_LU -1 37 #define CLARIION_SP_A 0 38 #define CLARIION_SP_B 1 39 40 /* Flags */ 41 #define CLARIION_SHORT_TRESPASS 1 42 #define CLARIION_HONOR_RESERVATIONS 2 43 44 /* LUN states */ 45 #define CLARIION_LUN_UNINITIALIZED -1 46 #define CLARIION_LUN_UNBOUND 0 47 #define CLARIION_LUN_BOUND 1 48 #define CLARIION_LUN_OWNED 2 49 50 static unsigned char long_trespass[] = { 51 0, 0, 0, 0, 0, 0, 0, 0, 52 CLARIION_TRESPASS_PAGE, /* Page code */ 53 0x09, /* Page length - 2 */ 54 0x01, /* Trespass code */ 55 0xff, 0xff, /* Trespass target */ 56 0, 0, 0, 0, 0, 0 /* Reserved bytes / unknown */ 57 }; 58 59 static unsigned char short_trespass[] = { 60 0, 0, 0, 0, 61 CLARIION_TRESPASS_PAGE, /* Page code */ 62 0x02, /* Page length - 2 */ 63 0x01, /* Trespass code */ 64 0xff, /* Trespass target */ 65 }; 66 67 static const char * lun_state[] = 68 { 69 "not bound", 70 "bound", 71 "owned", 72 }; 73 74 struct clariion_dh_data { 75 /* 76 * Flags: 77 * CLARIION_SHORT_TRESPASS 78 * Use short trespass command (FC-series) or the long version 79 * (default for AX/CX CLARiiON arrays). 80 * 81 * CLARIION_HONOR_RESERVATIONS 82 * Whether or not (default) to honor SCSI reservations when 83 * initiating a switch-over. 84 */ 85 unsigned flags; 86 /* 87 * I/O buffer for both MODE_SELECT and INQUIRY commands. 88 */ 89 unsigned char buffer[CLARIION_BUFFER_SIZE]; 90 /* 91 * SCSI sense buffer for commands -- assumes serial issuance 92 * and completion sequence of all commands for same multipath. 93 */ 94 unsigned char sense[SCSI_SENSE_BUFFERSIZE]; 95 unsigned int senselen; 96 /* 97 * LUN state 98 */ 99 int lun_state; 100 /* 101 * SP Port number 102 */ 103 int port; 104 /* 105 * which SP (A=0,B=1,UNBOUND=-1) is the default SP for this 106 * path's mapped LUN 107 */ 108 int default_sp; 109 /* 110 * which SP (A=0,B=1,UNBOUND=-1) is the active SP for this 111 * path's mapped LUN 112 */ 113 int current_sp; 114 }; 115 116 /* 117 * Parse MODE_SELECT cmd reply. 118 */ 119 static int trespass_endio(struct scsi_device *sdev, char *sense) 120 { 121 int err = SCSI_DH_IO; 122 struct scsi_sense_hdr sshdr; 123 124 if (!scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, &sshdr)) { 125 sdev_printk(KERN_ERR, sdev, "%s: Found valid sense data 0x%2x, " 126 "0x%2x, 0x%2x while sending CLARiiON trespass " 127 "command.\n", CLARIION_NAME, sshdr.sense_key, 128 sshdr.asc, sshdr.ascq); 129 130 if ((sshdr.sense_key == 0x05) && (sshdr.asc == 0x04) && 131 (sshdr.ascq == 0x00)) { 132 /* 133 * Array based copy in progress -- do not send 134 * mode_select or copy will be aborted mid-stream. 135 */ 136 sdev_printk(KERN_INFO, sdev, "%s: Array Based Copy in " 137 "progress while sending CLARiiON trespass " 138 "command.\n", CLARIION_NAME); 139 err = SCSI_DH_DEV_TEMP_BUSY; 140 } else if ((sshdr.sense_key == 0x02) && (sshdr.asc == 0x04) && 141 (sshdr.ascq == 0x03)) { 142 /* 143 * LUN Not Ready - Manual Intervention Required 144 * indicates in-progress ucode upgrade (NDU). 145 */ 146 sdev_printk(KERN_INFO, sdev, "%s: Detected in-progress " 147 "ucode upgrade NDU operation while sending " 148 "CLARiiON trespass command.\n", CLARIION_NAME); 149 err = SCSI_DH_DEV_TEMP_BUSY; 150 } else 151 err = SCSI_DH_DEV_FAILED; 152 } else { 153 sdev_printk(KERN_INFO, sdev, 154 "%s: failed to send MODE SELECT, no sense available\n", 155 CLARIION_NAME); 156 } 157 return err; 158 } 159 160 static int parse_sp_info_reply(struct scsi_device *sdev, 161 struct clariion_dh_data *csdev) 162 { 163 int err = SCSI_DH_OK; 164 165 /* check for in-progress ucode upgrade (NDU) */ 166 if (csdev->buffer[48] != 0) { 167 sdev_printk(KERN_NOTICE, sdev, "%s: Detected in-progress " 168 "ucode upgrade NDU operation while finding " 169 "current active SP.", CLARIION_NAME); 170 err = SCSI_DH_DEV_TEMP_BUSY; 171 goto out; 172 } 173 if (csdev->buffer[4] > 2) { 174 /* Invalid buffer format */ 175 sdev_printk(KERN_NOTICE, sdev, 176 "%s: invalid VPD page 0xC0 format\n", 177 CLARIION_NAME); 178 err = SCSI_DH_NOSYS; 179 goto out; 180 } 181 switch (csdev->buffer[28] & 0x0f) { 182 case 6: 183 sdev_printk(KERN_NOTICE, sdev, 184 "%s: ALUA failover mode detected\n", 185 CLARIION_NAME); 186 break; 187 case 4: 188 /* Linux failover */ 189 break; 190 default: 191 sdev_printk(KERN_WARNING, sdev, 192 "%s: Invalid failover mode %d\n", 193 CLARIION_NAME, csdev->buffer[28] & 0x0f); 194 err = SCSI_DH_NOSYS; 195 goto out; 196 } 197 198 csdev->default_sp = csdev->buffer[5]; 199 csdev->lun_state = csdev->buffer[4]; 200 csdev->current_sp = csdev->buffer[8]; 201 csdev->port = csdev->buffer[7]; 202 203 out: 204 return err; 205 } 206 207 #define emc_default_str "FC (Legacy)" 208 209 static char * parse_sp_model(struct scsi_device *sdev, unsigned char *buffer) 210 { 211 unsigned char len = buffer[4] + 5; 212 char *sp_model = NULL; 213 unsigned char sp_len, serial_len; 214 215 if (len < 160) { 216 sdev_printk(KERN_WARNING, sdev, 217 "%s: Invalid information section length %d\n", 218 CLARIION_NAME, len); 219 /* Check for old FC arrays */ 220 if (!strncmp(buffer + 8, "DGC", 3)) { 221 /* Old FC array, not supporting extended information */ 222 sp_model = emc_default_str; 223 } 224 goto out; 225 } 226 227 /* 228 * Parse extended information for SP model number 229 */ 230 serial_len = buffer[160]; 231 if (serial_len == 0 || serial_len + 161 > len) { 232 sdev_printk(KERN_WARNING, sdev, 233 "%s: Invalid array serial number length %d\n", 234 CLARIION_NAME, serial_len); 235 goto out; 236 } 237 sp_len = buffer[99]; 238 if (sp_len == 0 || serial_len + sp_len + 161 > len) { 239 sdev_printk(KERN_WARNING, sdev, 240 "%s: Invalid model number length %d\n", 241 CLARIION_NAME, sp_len); 242 goto out; 243 } 244 sp_model = &buffer[serial_len + 161]; 245 /* Strip whitespace at the end */ 246 while (sp_len > 1 && sp_model[sp_len - 1] == ' ') 247 sp_len--; 248 249 sp_model[sp_len] = '\0'; 250 251 out: 252 return sp_model; 253 } 254 255 /* 256 * Get block request for REQ_BLOCK_PC command issued to path. Currently 257 * limited to MODE_SELECT (trespass) and INQUIRY (VPD page 0xC0) commands. 258 * 259 * Uses data and sense buffers in hardware handler context structure and 260 * assumes serial servicing of commands, both issuance and completion. 261 */ 262 static struct request *get_req(struct scsi_device *sdev, int cmd, 263 unsigned char *buffer) 264 { 265 struct request *rq; 266 int len = 0; 267 268 rq = blk_get_request(sdev->request_queue, 269 (cmd != INQUIRY) ? WRITE : READ, GFP_NOIO); 270 if (IS_ERR(rq)) { 271 sdev_printk(KERN_INFO, sdev, "get_req: blk_get_request failed"); 272 return NULL; 273 } 274 275 blk_rq_set_block_pc(rq); 276 rq->cmd_len = COMMAND_SIZE(cmd); 277 rq->cmd[0] = cmd; 278 279 switch (cmd) { 280 case MODE_SELECT: 281 len = sizeof(short_trespass); 282 rq->cmd[1] = 0x10; 283 rq->cmd[4] = len; 284 break; 285 case MODE_SELECT_10: 286 len = sizeof(long_trespass); 287 rq->cmd[1] = 0x10; 288 rq->cmd[8] = len; 289 break; 290 case INQUIRY: 291 len = CLARIION_BUFFER_SIZE; 292 rq->cmd[4] = len; 293 memset(buffer, 0, len); 294 break; 295 default: 296 BUG_ON(1); 297 break; 298 } 299 300 rq->cmd_flags |= REQ_FAILFAST_DEV | REQ_FAILFAST_TRANSPORT | 301 REQ_FAILFAST_DRIVER; 302 rq->timeout = CLARIION_TIMEOUT; 303 rq->retries = CLARIION_RETRIES; 304 305 if (blk_rq_map_kern(rq->q, rq, buffer, len, GFP_NOIO)) { 306 blk_put_request(rq); 307 return NULL; 308 } 309 310 return rq; 311 } 312 313 static int send_inquiry_cmd(struct scsi_device *sdev, int page, 314 struct clariion_dh_data *csdev) 315 { 316 struct request *rq = get_req(sdev, INQUIRY, csdev->buffer); 317 int err; 318 319 if (!rq) 320 return SCSI_DH_RES_TEMP_UNAVAIL; 321 322 rq->sense = csdev->sense; 323 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE); 324 rq->sense_len = csdev->senselen = 0; 325 326 rq->cmd[0] = INQUIRY; 327 if (page != 0) { 328 rq->cmd[1] = 1; 329 rq->cmd[2] = page; 330 } 331 err = blk_execute_rq(sdev->request_queue, NULL, rq, 1); 332 if (err == -EIO) { 333 sdev_printk(KERN_INFO, sdev, 334 "%s: failed to send %s INQUIRY: %x\n", 335 CLARIION_NAME, page?"EVPD":"standard", 336 rq->errors); 337 csdev->senselen = rq->sense_len; 338 err = SCSI_DH_IO; 339 } 340 341 blk_put_request(rq); 342 343 return err; 344 } 345 346 static int send_trespass_cmd(struct scsi_device *sdev, 347 struct clariion_dh_data *csdev) 348 { 349 struct request *rq; 350 unsigned char *page22; 351 int err, len, cmd; 352 353 if (csdev->flags & CLARIION_SHORT_TRESPASS) { 354 page22 = short_trespass; 355 if (!(csdev->flags & CLARIION_HONOR_RESERVATIONS)) 356 /* Set Honor Reservations bit */ 357 page22[6] |= 0x80; 358 len = sizeof(short_trespass); 359 cmd = MODE_SELECT; 360 } else { 361 page22 = long_trespass; 362 if (!(csdev->flags & CLARIION_HONOR_RESERVATIONS)) 363 /* Set Honor Reservations bit */ 364 page22[10] |= 0x80; 365 len = sizeof(long_trespass); 366 cmd = MODE_SELECT_10; 367 } 368 BUG_ON((len > CLARIION_BUFFER_SIZE)); 369 memcpy(csdev->buffer, page22, len); 370 371 rq = get_req(sdev, cmd, csdev->buffer); 372 if (!rq) 373 return SCSI_DH_RES_TEMP_UNAVAIL; 374 375 rq->sense = csdev->sense; 376 memset(rq->sense, 0, SCSI_SENSE_BUFFERSIZE); 377 rq->sense_len = csdev->senselen = 0; 378 379 err = blk_execute_rq(sdev->request_queue, NULL, rq, 1); 380 if (err == -EIO) { 381 if (rq->sense_len) { 382 err = trespass_endio(sdev, csdev->sense); 383 } else { 384 sdev_printk(KERN_INFO, sdev, 385 "%s: failed to send MODE SELECT: %x\n", 386 CLARIION_NAME, rq->errors); 387 } 388 } 389 390 blk_put_request(rq); 391 392 return err; 393 } 394 395 static int clariion_check_sense(struct scsi_device *sdev, 396 struct scsi_sense_hdr *sense_hdr) 397 { 398 switch (sense_hdr->sense_key) { 399 case NOT_READY: 400 if (sense_hdr->asc == 0x04 && sense_hdr->ascq == 0x03) 401 /* 402 * LUN Not Ready - Manual Intervention Required 403 * indicates this is a passive path. 404 * 405 * FIXME: However, if this is seen and EVPD C0 406 * indicates that this is due to a NDU in 407 * progress, we should set FAIL_PATH too. 408 * This indicates we might have to do a SCSI 409 * inquiry in the end_io path. Ugh. 410 * 411 * Can return FAILED only when we want the error 412 * recovery process to kick in. 413 */ 414 return SUCCESS; 415 break; 416 case ILLEGAL_REQUEST: 417 if (sense_hdr->asc == 0x25 && sense_hdr->ascq == 0x01) 418 /* 419 * An array based copy is in progress. Do not 420 * fail the path, do not bypass to another PG, 421 * do not retry. Fail the IO immediately. 422 * (Actually this is the same conclusion as in 423 * the default handler, but lets make sure.) 424 * 425 * Can return FAILED only when we want the error 426 * recovery process to kick in. 427 */ 428 return SUCCESS; 429 break; 430 case UNIT_ATTENTION: 431 if (sense_hdr->asc == 0x29 && sense_hdr->ascq == 0x00) 432 /* 433 * Unit Attention Code. This is the first IO 434 * to the new path, so just retry. 435 */ 436 return ADD_TO_MLQUEUE; 437 break; 438 } 439 440 return SCSI_RETURN_NOT_HANDLED; 441 } 442 443 static int clariion_prep_fn(struct scsi_device *sdev, struct request *req) 444 { 445 struct clariion_dh_data *h = sdev->handler_data; 446 int ret = BLKPREP_OK; 447 448 if (h->lun_state != CLARIION_LUN_OWNED) { 449 ret = BLKPREP_KILL; 450 req->cmd_flags |= REQ_QUIET; 451 } 452 return ret; 453 454 } 455 456 static int clariion_std_inquiry(struct scsi_device *sdev, 457 struct clariion_dh_data *csdev) 458 { 459 int err; 460 char *sp_model; 461 462 err = send_inquiry_cmd(sdev, 0, csdev); 463 if (err != SCSI_DH_OK && csdev->senselen) { 464 struct scsi_sense_hdr sshdr; 465 466 if (scsi_normalize_sense(csdev->sense, SCSI_SENSE_BUFFERSIZE, 467 &sshdr)) { 468 sdev_printk(KERN_ERR, sdev, "%s: INQUIRY sense code " 469 "%02x/%02x/%02x\n", CLARIION_NAME, 470 sshdr.sense_key, sshdr.asc, sshdr.ascq); 471 } 472 err = SCSI_DH_IO; 473 goto out; 474 } 475 476 sp_model = parse_sp_model(sdev, csdev->buffer); 477 if (!sp_model) { 478 err = SCSI_DH_DEV_UNSUPP; 479 goto out; 480 } 481 482 /* 483 * FC Series arrays do not support long trespass 484 */ 485 if (!strlen(sp_model) || !strncmp(sp_model, "FC",2)) 486 csdev->flags |= CLARIION_SHORT_TRESPASS; 487 488 sdev_printk(KERN_INFO, sdev, 489 "%s: detected Clariion %s, flags %x\n", 490 CLARIION_NAME, sp_model, csdev->flags); 491 out: 492 return err; 493 } 494 495 static int clariion_send_inquiry(struct scsi_device *sdev, 496 struct clariion_dh_data *csdev) 497 { 498 int err, retry = CLARIION_RETRIES; 499 500 retry: 501 err = send_inquiry_cmd(sdev, 0xC0, csdev); 502 if (err != SCSI_DH_OK && csdev->senselen) { 503 struct scsi_sense_hdr sshdr; 504 505 err = scsi_normalize_sense(csdev->sense, SCSI_SENSE_BUFFERSIZE, 506 &sshdr); 507 if (!err) 508 return SCSI_DH_IO; 509 510 err = clariion_check_sense(sdev, &sshdr); 511 if (retry > 0 && err == ADD_TO_MLQUEUE) { 512 retry--; 513 goto retry; 514 } 515 sdev_printk(KERN_ERR, sdev, "%s: INQUIRY sense code " 516 "%02x/%02x/%02x\n", CLARIION_NAME, 517 sshdr.sense_key, sshdr.asc, sshdr.ascq); 518 err = SCSI_DH_IO; 519 } else { 520 err = parse_sp_info_reply(sdev, csdev); 521 } 522 return err; 523 } 524 525 static int clariion_activate(struct scsi_device *sdev, 526 activate_complete fn, void *data) 527 { 528 struct clariion_dh_data *csdev = sdev->handler_data; 529 int result; 530 531 result = clariion_send_inquiry(sdev, csdev); 532 if (result != SCSI_DH_OK) 533 goto done; 534 535 if (csdev->lun_state == CLARIION_LUN_OWNED) 536 goto done; 537 538 result = send_trespass_cmd(sdev, csdev); 539 if (result != SCSI_DH_OK) 540 goto done; 541 sdev_printk(KERN_INFO, sdev,"%s: %s trespass command sent\n", 542 CLARIION_NAME, 543 csdev->flags&CLARIION_SHORT_TRESPASS?"short":"long" ); 544 545 /* Update status */ 546 result = clariion_send_inquiry(sdev, csdev); 547 if (result != SCSI_DH_OK) 548 goto done; 549 550 done: 551 sdev_printk(KERN_INFO, sdev, 552 "%s: at SP %c Port %d (%s, default SP %c)\n", 553 CLARIION_NAME, csdev->current_sp + 'A', 554 csdev->port, lun_state[csdev->lun_state], 555 csdev->default_sp + 'A'); 556 557 if (fn) 558 fn(data, result); 559 return 0; 560 } 561 /* 562 * params - parameters in the following format 563 * "no_of_params\0param1\0param2\0param3\0...\0" 564 * for example, string for 2 parameters with value 10 and 21 565 * is specified as "2\010\021\0". 566 */ 567 static int clariion_set_params(struct scsi_device *sdev, const char *params) 568 { 569 struct clariion_dh_data *csdev = sdev->handler_data; 570 unsigned int hr = 0, st = 0, argc; 571 const char *p = params; 572 int result = SCSI_DH_OK; 573 574 if ((sscanf(params, "%u", &argc) != 1) || (argc != 2)) 575 return -EINVAL; 576 577 while (*p++) 578 ; 579 if ((sscanf(p, "%u", &st) != 1) || (st > 1)) 580 return -EINVAL; 581 582 while (*p++) 583 ; 584 if ((sscanf(p, "%u", &hr) != 1) || (hr > 1)) 585 return -EINVAL; 586 587 if (st) 588 csdev->flags |= CLARIION_SHORT_TRESPASS; 589 else 590 csdev->flags &= ~CLARIION_SHORT_TRESPASS; 591 592 if (hr) 593 csdev->flags |= CLARIION_HONOR_RESERVATIONS; 594 else 595 csdev->flags &= ~CLARIION_HONOR_RESERVATIONS; 596 597 /* 598 * If this path is owned, we have to send a trespass command 599 * with the new parameters. If not, simply return. Next trespass 600 * command would use the parameters. 601 */ 602 if (csdev->lun_state != CLARIION_LUN_OWNED) 603 goto done; 604 605 csdev->lun_state = CLARIION_LUN_UNINITIALIZED; 606 result = send_trespass_cmd(sdev, csdev); 607 if (result != SCSI_DH_OK) 608 goto done; 609 610 /* Update status */ 611 result = clariion_send_inquiry(sdev, csdev); 612 613 done: 614 return result; 615 } 616 617 static int clariion_bus_attach(struct scsi_device *sdev) 618 { 619 struct clariion_dh_data *h; 620 int err; 621 622 h = kzalloc(sizeof(*h) , GFP_KERNEL); 623 if (!h) 624 return -ENOMEM; 625 h->lun_state = CLARIION_LUN_UNINITIALIZED; 626 h->default_sp = CLARIION_UNBOUND_LU; 627 h->current_sp = CLARIION_UNBOUND_LU; 628 629 err = clariion_std_inquiry(sdev, h); 630 if (err != SCSI_DH_OK) 631 goto failed; 632 633 err = clariion_send_inquiry(sdev, h); 634 if (err != SCSI_DH_OK) 635 goto failed; 636 637 sdev_printk(KERN_INFO, sdev, 638 "%s: connected to SP %c Port %d (%s, default SP %c)\n", 639 CLARIION_NAME, h->current_sp + 'A', 640 h->port, lun_state[h->lun_state], 641 h->default_sp + 'A'); 642 643 sdev->handler_data = h; 644 return 0; 645 646 failed: 647 kfree(h); 648 return -EINVAL; 649 } 650 651 static void clariion_bus_detach(struct scsi_device *sdev) 652 { 653 kfree(sdev->handler_data); 654 sdev->handler_data = NULL; 655 } 656 657 static struct scsi_device_handler clariion_dh = { 658 .name = CLARIION_NAME, 659 .module = THIS_MODULE, 660 .attach = clariion_bus_attach, 661 .detach = clariion_bus_detach, 662 .check_sense = clariion_check_sense, 663 .activate = clariion_activate, 664 .prep_fn = clariion_prep_fn, 665 .set_params = clariion_set_params, 666 }; 667 668 static int __init clariion_init(void) 669 { 670 int r; 671 672 r = scsi_register_device_handler(&clariion_dh); 673 if (r != 0) 674 printk(KERN_ERR "%s: Failed to register scsi device handler.", 675 CLARIION_NAME); 676 return r; 677 } 678 679 static void __exit clariion_exit(void) 680 { 681 scsi_unregister_device_handler(&clariion_dh); 682 } 683 684 module_init(clariion_init); 685 module_exit(clariion_exit); 686 687 MODULE_DESCRIPTION("EMC CX/AX/FC-family driver"); 688 MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, Chandra Seetharaman <sekharan@us.ibm.com>"); 689 MODULE_LICENSE("GPL"); 690