1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * SCSI Enclosure Services 4 * 5 * Copyright (C) 2008 James Bottomley <James.Bottomley@HansenPartnership.com> 6 * 7 **----------------------------------------------------------------------------- 8 ** 9 ** 10 **----------------------------------------------------------------------------- 11 */ 12 13 #include <linux/slab.h> 14 #include <linux/module.h> 15 #include <linux/kernel.h> 16 #include <linux/enclosure.h> 17 #include <asm/unaligned.h> 18 19 #include <scsi/scsi.h> 20 #include <scsi/scsi_cmnd.h> 21 #include <scsi/scsi_dbg.h> 22 #include <scsi/scsi_device.h> 23 #include <scsi/scsi_driver.h> 24 #include <scsi/scsi_host.h> 25 26 #include <scsi/scsi_transport_sas.h> 27 28 struct ses_device { 29 unsigned char *page1; 30 unsigned char *page1_types; 31 unsigned char *page2; 32 unsigned char *page10; 33 short page1_len; 34 short page1_num_types; 35 short page2_len; 36 short page10_len; 37 }; 38 39 struct ses_component { 40 u64 addr; 41 }; 42 43 static bool ses_page2_supported(struct enclosure_device *edev) 44 { 45 struct ses_device *ses_dev = edev->scratch; 46 47 return (ses_dev->page2 != NULL); 48 } 49 50 static int ses_probe(struct device *dev) 51 { 52 struct scsi_device *sdev = to_scsi_device(dev); 53 int err = -ENODEV; 54 55 if (sdev->type != TYPE_ENCLOSURE) 56 goto out; 57 58 err = 0; 59 sdev_printk(KERN_NOTICE, sdev, "Attached Enclosure device\n"); 60 61 out: 62 return err; 63 } 64 65 #define SES_TIMEOUT (30 * HZ) 66 #define SES_RETRIES 3 67 68 static void init_device_slot_control(unsigned char *dest_desc, 69 struct enclosure_component *ecomp, 70 unsigned char *status) 71 { 72 memcpy(dest_desc, status, 4); 73 dest_desc[0] = 0; 74 /* only clear byte 1 for ENCLOSURE_COMPONENT_DEVICE */ 75 if (ecomp->type == ENCLOSURE_COMPONENT_DEVICE) 76 dest_desc[1] = 0; 77 dest_desc[2] &= 0xde; 78 dest_desc[3] &= 0x3c; 79 } 80 81 82 static int ses_recv_diag(struct scsi_device *sdev, int page_code, 83 void *buf, int bufflen) 84 { 85 int ret; 86 unsigned char cmd[] = { 87 RECEIVE_DIAGNOSTIC, 88 1, /* Set PCV bit */ 89 page_code, 90 bufflen >> 8, 91 bufflen & 0xff, 92 0 93 }; 94 unsigned char recv_page_code; 95 96 ret = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, bufflen, 97 NULL, SES_TIMEOUT, SES_RETRIES, NULL); 98 if (unlikely(ret)) 99 return ret; 100 101 recv_page_code = ((unsigned char *)buf)[0]; 102 103 if (likely(recv_page_code == page_code)) 104 return ret; 105 106 /* successful diagnostic but wrong page code. This happens to some 107 * USB devices, just print a message and pretend there was an error */ 108 109 sdev_printk(KERN_ERR, sdev, 110 "Wrong diagnostic page; asked for %d got %u\n", 111 page_code, recv_page_code); 112 113 return -EINVAL; 114 } 115 116 static int ses_send_diag(struct scsi_device *sdev, int page_code, 117 void *buf, int bufflen) 118 { 119 u32 result; 120 121 unsigned char cmd[] = { 122 SEND_DIAGNOSTIC, 123 0x10, /* Set PF bit */ 124 0, 125 bufflen >> 8, 126 bufflen & 0xff, 127 0 128 }; 129 130 result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, buf, bufflen, 131 NULL, SES_TIMEOUT, SES_RETRIES, NULL); 132 if (result) 133 sdev_printk(KERN_ERR, sdev, "SEND DIAGNOSTIC result: %8x\n", 134 result); 135 return result; 136 } 137 138 static int ses_set_page2_descriptor(struct enclosure_device *edev, 139 struct enclosure_component *ecomp, 140 unsigned char *desc) 141 { 142 int i, j, count = 0, descriptor = ecomp->number; 143 struct scsi_device *sdev = to_scsi_device(edev->edev.parent); 144 struct ses_device *ses_dev = edev->scratch; 145 unsigned char *type_ptr = ses_dev->page1_types; 146 unsigned char *desc_ptr = ses_dev->page2 + 8; 147 148 /* Clear everything */ 149 memset(desc_ptr, 0, ses_dev->page2_len - 8); 150 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) { 151 for (j = 0; j < type_ptr[1]; j++) { 152 desc_ptr += 4; 153 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE && 154 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE) 155 continue; 156 if (count++ == descriptor) { 157 memcpy(desc_ptr, desc, 4); 158 /* set select */ 159 desc_ptr[0] |= 0x80; 160 /* clear reserved, just in case */ 161 desc_ptr[0] &= 0xf0; 162 } 163 } 164 } 165 166 return ses_send_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len); 167 } 168 169 static unsigned char *ses_get_page2_descriptor(struct enclosure_device *edev, 170 struct enclosure_component *ecomp) 171 { 172 int i, j, count = 0, descriptor = ecomp->number; 173 struct scsi_device *sdev = to_scsi_device(edev->edev.parent); 174 struct ses_device *ses_dev = edev->scratch; 175 unsigned char *type_ptr = ses_dev->page1_types; 176 unsigned char *desc_ptr = ses_dev->page2 + 8; 177 178 if (ses_recv_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len) < 0) 179 return NULL; 180 181 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) { 182 for (j = 0; j < type_ptr[1]; j++) { 183 desc_ptr += 4; 184 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE && 185 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE) 186 continue; 187 if (count++ == descriptor) 188 return desc_ptr; 189 } 190 } 191 return NULL; 192 } 193 194 /* For device slot and array device slot elements, byte 3 bit 6 195 * is "fault sensed" while byte 3 bit 5 is "fault reqstd". As this 196 * code stands these bits are shifted 4 positions right so in 197 * sysfs they will appear as bits 2 and 1 respectively. Strange. */ 198 static void ses_get_fault(struct enclosure_device *edev, 199 struct enclosure_component *ecomp) 200 { 201 unsigned char *desc; 202 203 if (!ses_page2_supported(edev)) { 204 ecomp->fault = 0; 205 return; 206 } 207 desc = ses_get_page2_descriptor(edev, ecomp); 208 if (desc) 209 ecomp->fault = (desc[3] & 0x60) >> 4; 210 } 211 212 static int ses_set_fault(struct enclosure_device *edev, 213 struct enclosure_component *ecomp, 214 enum enclosure_component_setting val) 215 { 216 unsigned char desc[4]; 217 unsigned char *desc_ptr; 218 219 if (!ses_page2_supported(edev)) 220 return -EINVAL; 221 222 desc_ptr = ses_get_page2_descriptor(edev, ecomp); 223 224 if (!desc_ptr) 225 return -EIO; 226 227 init_device_slot_control(desc, ecomp, desc_ptr); 228 229 switch (val) { 230 case ENCLOSURE_SETTING_DISABLED: 231 desc[3] &= 0xdf; 232 break; 233 case ENCLOSURE_SETTING_ENABLED: 234 desc[3] |= 0x20; 235 break; 236 default: 237 /* SES doesn't do the SGPIO blink settings */ 238 return -EINVAL; 239 } 240 241 return ses_set_page2_descriptor(edev, ecomp, desc); 242 } 243 244 static void ses_get_status(struct enclosure_device *edev, 245 struct enclosure_component *ecomp) 246 { 247 unsigned char *desc; 248 249 if (!ses_page2_supported(edev)) { 250 ecomp->status = 0; 251 return; 252 } 253 desc = ses_get_page2_descriptor(edev, ecomp); 254 if (desc) 255 ecomp->status = (desc[0] & 0x0f); 256 } 257 258 static void ses_get_locate(struct enclosure_device *edev, 259 struct enclosure_component *ecomp) 260 { 261 unsigned char *desc; 262 263 if (!ses_page2_supported(edev)) { 264 ecomp->locate = 0; 265 return; 266 } 267 desc = ses_get_page2_descriptor(edev, ecomp); 268 if (desc) 269 ecomp->locate = (desc[2] & 0x02) ? 1 : 0; 270 } 271 272 static int ses_set_locate(struct enclosure_device *edev, 273 struct enclosure_component *ecomp, 274 enum enclosure_component_setting val) 275 { 276 unsigned char desc[4]; 277 unsigned char *desc_ptr; 278 279 if (!ses_page2_supported(edev)) 280 return -EINVAL; 281 282 desc_ptr = ses_get_page2_descriptor(edev, ecomp); 283 284 if (!desc_ptr) 285 return -EIO; 286 287 init_device_slot_control(desc, ecomp, desc_ptr); 288 289 switch (val) { 290 case ENCLOSURE_SETTING_DISABLED: 291 desc[2] &= 0xfd; 292 break; 293 case ENCLOSURE_SETTING_ENABLED: 294 desc[2] |= 0x02; 295 break; 296 default: 297 /* SES doesn't do the SGPIO blink settings */ 298 return -EINVAL; 299 } 300 return ses_set_page2_descriptor(edev, ecomp, desc); 301 } 302 303 static int ses_set_active(struct enclosure_device *edev, 304 struct enclosure_component *ecomp, 305 enum enclosure_component_setting val) 306 { 307 unsigned char desc[4]; 308 unsigned char *desc_ptr; 309 310 if (!ses_page2_supported(edev)) 311 return -EINVAL; 312 313 desc_ptr = ses_get_page2_descriptor(edev, ecomp); 314 315 if (!desc_ptr) 316 return -EIO; 317 318 init_device_slot_control(desc, ecomp, desc_ptr); 319 320 switch (val) { 321 case ENCLOSURE_SETTING_DISABLED: 322 desc[2] &= 0x7f; 323 ecomp->active = 0; 324 break; 325 case ENCLOSURE_SETTING_ENABLED: 326 desc[2] |= 0x80; 327 ecomp->active = 1; 328 break; 329 default: 330 /* SES doesn't do the SGPIO blink settings */ 331 return -EINVAL; 332 } 333 return ses_set_page2_descriptor(edev, ecomp, desc); 334 } 335 336 static int ses_show_id(struct enclosure_device *edev, char *buf) 337 { 338 struct ses_device *ses_dev = edev->scratch; 339 unsigned long long id = get_unaligned_be64(ses_dev->page1+8+4); 340 341 return sprintf(buf, "%#llx\n", id); 342 } 343 344 static void ses_get_power_status(struct enclosure_device *edev, 345 struct enclosure_component *ecomp) 346 { 347 unsigned char *desc; 348 349 if (!ses_page2_supported(edev)) { 350 ecomp->power_status = 0; 351 return; 352 } 353 354 desc = ses_get_page2_descriptor(edev, ecomp); 355 if (desc) 356 ecomp->power_status = (desc[3] & 0x10) ? 0 : 1; 357 } 358 359 static int ses_set_power_status(struct enclosure_device *edev, 360 struct enclosure_component *ecomp, 361 int val) 362 { 363 unsigned char desc[4]; 364 unsigned char *desc_ptr; 365 366 if (!ses_page2_supported(edev)) 367 return -EINVAL; 368 369 desc_ptr = ses_get_page2_descriptor(edev, ecomp); 370 371 if (!desc_ptr) 372 return -EIO; 373 374 init_device_slot_control(desc, ecomp, desc_ptr); 375 376 switch (val) { 377 /* power = 1 is device_off = 0 and vice versa */ 378 case 0: 379 desc[3] |= 0x10; 380 break; 381 case 1: 382 desc[3] &= 0xef; 383 break; 384 default: 385 return -EINVAL; 386 } 387 ecomp->power_status = val; 388 return ses_set_page2_descriptor(edev, ecomp, desc); 389 } 390 391 static struct enclosure_component_callbacks ses_enclosure_callbacks = { 392 .get_fault = ses_get_fault, 393 .set_fault = ses_set_fault, 394 .get_status = ses_get_status, 395 .get_locate = ses_get_locate, 396 .set_locate = ses_set_locate, 397 .get_power_status = ses_get_power_status, 398 .set_power_status = ses_set_power_status, 399 .set_active = ses_set_active, 400 .show_id = ses_show_id, 401 }; 402 403 struct ses_host_edev { 404 struct Scsi_Host *shost; 405 struct enclosure_device *edev; 406 }; 407 408 #if 0 409 int ses_match_host(struct enclosure_device *edev, void *data) 410 { 411 struct ses_host_edev *sed = data; 412 struct scsi_device *sdev; 413 414 if (!scsi_is_sdev_device(edev->edev.parent)) 415 return 0; 416 417 sdev = to_scsi_device(edev->edev.parent); 418 419 if (sdev->host != sed->shost) 420 return 0; 421 422 sed->edev = edev; 423 return 1; 424 } 425 #endif /* 0 */ 426 427 static void ses_process_descriptor(struct enclosure_component *ecomp, 428 unsigned char *desc) 429 { 430 int eip = desc[0] & 0x10; 431 int invalid = desc[0] & 0x80; 432 enum scsi_protocol proto = desc[0] & 0x0f; 433 u64 addr = 0; 434 int slot = -1; 435 struct ses_component *scomp = ecomp->scratch; 436 unsigned char *d; 437 438 if (invalid) 439 return; 440 441 switch (proto) { 442 case SCSI_PROTOCOL_FCP: 443 if (eip) { 444 d = desc + 4; 445 slot = d[3]; 446 } 447 break; 448 case SCSI_PROTOCOL_SAS: 449 if (eip) { 450 d = desc + 4; 451 slot = d[3]; 452 d = desc + 8; 453 } else 454 d = desc + 4; 455 /* only take the phy0 addr */ 456 addr = (u64)d[12] << 56 | 457 (u64)d[13] << 48 | 458 (u64)d[14] << 40 | 459 (u64)d[15] << 32 | 460 (u64)d[16] << 24 | 461 (u64)d[17] << 16 | 462 (u64)d[18] << 8 | 463 (u64)d[19]; 464 break; 465 default: 466 /* FIXME: Need to add more protocols than just SAS */ 467 break; 468 } 469 ecomp->slot = slot; 470 scomp->addr = addr; 471 } 472 473 struct efd { 474 u64 addr; 475 struct device *dev; 476 }; 477 478 static int ses_enclosure_find_by_addr(struct enclosure_device *edev, 479 void *data) 480 { 481 struct efd *efd = data; 482 int i; 483 struct ses_component *scomp; 484 485 if (!edev->component[0].scratch) 486 return 0; 487 488 for (i = 0; i < edev->components; i++) { 489 scomp = edev->component[i].scratch; 490 if (scomp->addr != efd->addr) 491 continue; 492 493 if (enclosure_add_device(edev, i, efd->dev) == 0) 494 kobject_uevent(&efd->dev->kobj, KOBJ_CHANGE); 495 return 1; 496 } 497 return 0; 498 } 499 500 #define INIT_ALLOC_SIZE 32 501 502 static void ses_enclosure_data_process(struct enclosure_device *edev, 503 struct scsi_device *sdev, 504 int create) 505 { 506 u32 result; 507 unsigned char *buf = NULL, *type_ptr, *desc_ptr, *addl_desc_ptr = NULL; 508 int i, j, page7_len, len, components; 509 struct ses_device *ses_dev = edev->scratch; 510 int types = ses_dev->page1_num_types; 511 unsigned char *hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL); 512 513 if (!hdr_buf) 514 goto simple_populate; 515 516 /* re-read page 10 */ 517 if (ses_dev->page10) 518 ses_recv_diag(sdev, 10, ses_dev->page10, ses_dev->page10_len); 519 /* Page 7 for the descriptors is optional */ 520 result = ses_recv_diag(sdev, 7, hdr_buf, INIT_ALLOC_SIZE); 521 if (result) 522 goto simple_populate; 523 524 page7_len = len = (hdr_buf[2] << 8) + hdr_buf[3] + 4; 525 /* add 1 for trailing '\0' we'll use */ 526 buf = kzalloc(len + 1, GFP_KERNEL); 527 if (!buf) 528 goto simple_populate; 529 result = ses_recv_diag(sdev, 7, buf, len); 530 if (result) { 531 simple_populate: 532 kfree(buf); 533 buf = NULL; 534 desc_ptr = NULL; 535 len = 0; 536 page7_len = 0; 537 } else { 538 desc_ptr = buf + 8; 539 len = (desc_ptr[2] << 8) + desc_ptr[3]; 540 /* skip past overall descriptor */ 541 desc_ptr += len + 4; 542 } 543 if (ses_dev->page10) 544 addl_desc_ptr = ses_dev->page10 + 8; 545 type_ptr = ses_dev->page1_types; 546 components = 0; 547 for (i = 0; i < types; i++, type_ptr += 4) { 548 for (j = 0; j < type_ptr[1]; j++) { 549 char *name = NULL; 550 struct enclosure_component *ecomp; 551 552 if (desc_ptr) { 553 if (desc_ptr >= buf + page7_len) { 554 desc_ptr = NULL; 555 } else { 556 len = (desc_ptr[2] << 8) + desc_ptr[3]; 557 desc_ptr += 4; 558 /* Add trailing zero - pushes into 559 * reserved space */ 560 desc_ptr[len] = '\0'; 561 name = desc_ptr; 562 } 563 } 564 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE || 565 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) { 566 567 if (create) 568 ecomp = enclosure_component_alloc( 569 edev, 570 components++, 571 type_ptr[0], 572 name); 573 else 574 ecomp = &edev->component[components++]; 575 576 if (!IS_ERR(ecomp)) { 577 if (addl_desc_ptr) 578 ses_process_descriptor( 579 ecomp, 580 addl_desc_ptr); 581 if (create) 582 enclosure_component_register( 583 ecomp); 584 } 585 } 586 if (desc_ptr) 587 desc_ptr += len; 588 589 if (addl_desc_ptr && 590 /* only find additional descriptions for specific devices */ 591 (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE || 592 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE || 593 type_ptr[0] == ENCLOSURE_COMPONENT_SAS_EXPANDER || 594 /* these elements are optional */ 595 type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_TARGET_PORT || 596 type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_INITIATOR_PORT || 597 type_ptr[0] == ENCLOSURE_COMPONENT_CONTROLLER_ELECTRONICS)) 598 addl_desc_ptr += addl_desc_ptr[1] + 2; 599 600 } 601 } 602 kfree(buf); 603 kfree(hdr_buf); 604 } 605 606 static void ses_match_to_enclosure(struct enclosure_device *edev, 607 struct scsi_device *sdev, 608 int refresh) 609 { 610 struct scsi_device *edev_sdev = to_scsi_device(edev->edev.parent); 611 struct efd efd = { 612 .addr = 0, 613 }; 614 615 if (refresh) 616 ses_enclosure_data_process(edev, edev_sdev, 0); 617 618 if (scsi_is_sas_rphy(sdev->sdev_target->dev.parent)) 619 efd.addr = sas_get_address(sdev); 620 621 if (efd.addr) { 622 efd.dev = &sdev->sdev_gendev; 623 624 enclosure_for_each_device(ses_enclosure_find_by_addr, &efd); 625 } 626 } 627 628 static int ses_intf_add(struct device *cdev, 629 struct class_interface *intf) 630 { 631 struct scsi_device *sdev = to_scsi_device(cdev->parent); 632 struct scsi_device *tmp_sdev; 633 unsigned char *buf = NULL, *hdr_buf, *type_ptr, page; 634 struct ses_device *ses_dev; 635 u32 result; 636 int i, types, len, components = 0; 637 int err = -ENOMEM; 638 int num_enclosures; 639 struct enclosure_device *edev; 640 struct ses_component *scomp = NULL; 641 642 if (!scsi_device_enclosure(sdev)) { 643 /* not an enclosure, but might be in one */ 644 struct enclosure_device *prev = NULL; 645 646 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) { 647 ses_match_to_enclosure(edev, sdev, 1); 648 prev = edev; 649 } 650 return -ENODEV; 651 } 652 653 /* TYPE_ENCLOSURE prints a message in probe */ 654 if (sdev->type != TYPE_ENCLOSURE) 655 sdev_printk(KERN_NOTICE, sdev, "Embedded Enclosure Device\n"); 656 657 ses_dev = kzalloc(sizeof(*ses_dev), GFP_KERNEL); 658 hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL); 659 if (!hdr_buf || !ses_dev) 660 goto err_init_free; 661 662 page = 1; 663 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE); 664 if (result) 665 goto recv_failed; 666 667 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4; 668 buf = kzalloc(len, GFP_KERNEL); 669 if (!buf) 670 goto err_free; 671 672 result = ses_recv_diag(sdev, page, buf, len); 673 if (result) 674 goto recv_failed; 675 676 types = 0; 677 678 /* we always have one main enclosure and the rest are referred 679 * to as secondary subenclosures */ 680 num_enclosures = buf[1] + 1; 681 682 /* begin at the enclosure descriptor */ 683 type_ptr = buf + 8; 684 /* skip all the enclosure descriptors */ 685 for (i = 0; i < num_enclosures && type_ptr < buf + len; i++) { 686 types += type_ptr[2]; 687 type_ptr += type_ptr[3] + 4; 688 } 689 690 ses_dev->page1_types = type_ptr; 691 ses_dev->page1_num_types = types; 692 693 for (i = 0; i < types && type_ptr < buf + len; i++, type_ptr += 4) { 694 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE || 695 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) 696 components += type_ptr[1]; 697 } 698 ses_dev->page1 = buf; 699 ses_dev->page1_len = len; 700 buf = NULL; 701 702 page = 2; 703 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE); 704 if (result) 705 goto page2_not_supported; 706 707 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4; 708 buf = kzalloc(len, GFP_KERNEL); 709 if (!buf) 710 goto err_free; 711 712 /* make sure getting page 2 actually works */ 713 result = ses_recv_diag(sdev, 2, buf, len); 714 if (result) 715 goto recv_failed; 716 ses_dev->page2 = buf; 717 ses_dev->page2_len = len; 718 buf = NULL; 719 720 /* The additional information page --- allows us 721 * to match up the devices */ 722 page = 10; 723 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE); 724 if (!result) { 725 726 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4; 727 buf = kzalloc(len, GFP_KERNEL); 728 if (!buf) 729 goto err_free; 730 731 result = ses_recv_diag(sdev, page, buf, len); 732 if (result) 733 goto recv_failed; 734 ses_dev->page10 = buf; 735 ses_dev->page10_len = len; 736 buf = NULL; 737 } 738 page2_not_supported: 739 scomp = kcalloc(components, sizeof(struct ses_component), GFP_KERNEL); 740 if (!scomp) 741 goto err_free; 742 743 edev = enclosure_register(cdev->parent, dev_name(&sdev->sdev_gendev), 744 components, &ses_enclosure_callbacks); 745 if (IS_ERR(edev)) { 746 err = PTR_ERR(edev); 747 goto err_free; 748 } 749 750 kfree(hdr_buf); 751 752 edev->scratch = ses_dev; 753 for (i = 0; i < components; i++) 754 edev->component[i].scratch = scomp + i; 755 756 ses_enclosure_data_process(edev, sdev, 1); 757 758 /* see if there are any devices matching before 759 * we found the enclosure */ 760 shost_for_each_device(tmp_sdev, sdev->host) { 761 if (tmp_sdev->lun != 0 || scsi_device_enclosure(tmp_sdev)) 762 continue; 763 ses_match_to_enclosure(edev, tmp_sdev, 0); 764 } 765 766 return 0; 767 768 recv_failed: 769 sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n", 770 page); 771 err = -ENODEV; 772 err_free: 773 kfree(buf); 774 kfree(scomp); 775 kfree(ses_dev->page10); 776 kfree(ses_dev->page2); 777 kfree(ses_dev->page1); 778 err_init_free: 779 kfree(ses_dev); 780 kfree(hdr_buf); 781 sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err); 782 return err; 783 } 784 785 static int ses_remove(struct device *dev) 786 { 787 return 0; 788 } 789 790 static void ses_intf_remove_component(struct scsi_device *sdev) 791 { 792 struct enclosure_device *edev, *prev = NULL; 793 794 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) { 795 prev = edev; 796 if (!enclosure_remove_device(edev, &sdev->sdev_gendev)) 797 break; 798 } 799 if (edev) 800 put_device(&edev->edev); 801 } 802 803 static void ses_intf_remove_enclosure(struct scsi_device *sdev) 804 { 805 struct enclosure_device *edev; 806 struct ses_device *ses_dev; 807 808 /* exact match to this enclosure */ 809 edev = enclosure_find(&sdev->sdev_gendev, NULL); 810 if (!edev) 811 return; 812 813 ses_dev = edev->scratch; 814 edev->scratch = NULL; 815 816 kfree(ses_dev->page10); 817 kfree(ses_dev->page1); 818 kfree(ses_dev->page2); 819 kfree(ses_dev); 820 821 kfree(edev->component[0].scratch); 822 823 put_device(&edev->edev); 824 enclosure_unregister(edev); 825 } 826 827 static void ses_intf_remove(struct device *cdev, 828 struct class_interface *intf) 829 { 830 struct scsi_device *sdev = to_scsi_device(cdev->parent); 831 832 if (!scsi_device_enclosure(sdev)) 833 ses_intf_remove_component(sdev); 834 else 835 ses_intf_remove_enclosure(sdev); 836 } 837 838 static struct class_interface ses_interface = { 839 .add_dev = ses_intf_add, 840 .remove_dev = ses_intf_remove, 841 }; 842 843 static struct scsi_driver ses_template = { 844 .gendrv = { 845 .name = "ses", 846 .owner = THIS_MODULE, 847 .probe = ses_probe, 848 .remove = ses_remove, 849 }, 850 }; 851 852 static int __init ses_init(void) 853 { 854 int err; 855 856 err = scsi_register_interface(&ses_interface); 857 if (err) 858 return err; 859 860 err = scsi_register_driver(&ses_template.gendrv); 861 if (err) 862 goto out_unreg; 863 864 return 0; 865 866 out_unreg: 867 scsi_unregister_interface(&ses_interface); 868 return err; 869 } 870 871 static void __exit ses_exit(void) 872 { 873 scsi_unregister_driver(&ses_template.gendrv); 874 scsi_unregister_interface(&ses_interface); 875 } 876 877 module_init(ses_init); 878 module_exit(ses_exit); 879 880 MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE); 881 882 MODULE_AUTHOR("James Bottomley"); 883 MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver"); 884 MODULE_LICENSE("GPL v2"); 885