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