treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / scsi / ses.c
blobc2afba2a5414df7115ed16a289b33fde9002487a
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * SCSI Enclosure Services
5 * Copyright (C) 2008 James Bottomley <James.Bottomley@HansenPartnership.com>
6 */
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>
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>
21 #include <scsi/scsi_transport_sas.h>
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;
34 struct ses_component {
35 u64 addr;
38 static bool ses_page2_supported(struct enclosure_device *edev)
40 struct ses_device *ses_dev = edev->scratch;
42 return (ses_dev->page2 != NULL);
45 static int ses_probe(struct device *dev)
47 struct scsi_device *sdev = to_scsi_device(dev);
48 int err = -ENODEV;
50 if (sdev->type != TYPE_ENCLOSURE)
51 goto out;
53 err = 0;
54 sdev_printk(KERN_NOTICE, sdev, "Attached Enclosure device\n");
56 out:
57 return err;
60 #define SES_TIMEOUT (30 * HZ)
61 #define SES_RETRIES 3
63 static void init_device_slot_control(unsigned char *dest_desc,
64 struct enclosure_component *ecomp,
65 unsigned char *status)
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;
77 static int ses_recv_diag(struct scsi_device *sdev, int page_code,
78 void *buf, int bufflen)
80 int ret;
81 unsigned char cmd[] = {
82 RECEIVE_DIAGNOSTIC,
83 1, /* Set PCV bit */
84 page_code,
85 bufflen >> 8,
86 bufflen & 0xff,
89 unsigned char recv_page_code;
91 ret = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, bufflen,
92 NULL, SES_TIMEOUT, SES_RETRIES, NULL);
93 if (unlikely(ret))
94 return ret;
96 recv_page_code = ((unsigned char *)buf)[0];
98 if (likely(recv_page_code == page_code))
99 return ret;
101 /* successful diagnostic but wrong page code. This happens to some
102 * USB devices, just print a message and pretend there was an error */
104 sdev_printk(KERN_ERR, sdev,
105 "Wrong diagnostic page; asked for %d got %u\n",
106 page_code, recv_page_code);
108 return -EINVAL;
111 static int ses_send_diag(struct scsi_device *sdev, int page_code,
112 void *buf, int bufflen)
114 u32 result;
116 unsigned char cmd[] = {
117 SEND_DIAGNOSTIC,
118 0x10, /* Set PF bit */
120 bufflen >> 8,
121 bufflen & 0xff,
125 result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, buf, bufflen,
126 NULL, SES_TIMEOUT, SES_RETRIES, NULL);
127 if (result)
128 sdev_printk(KERN_ERR, sdev, "SEND DIAGNOSTIC result: %8x\n",
129 result);
130 return result;
133 static int ses_set_page2_descriptor(struct enclosure_device *edev,
134 struct enclosure_component *ecomp,
135 unsigned char *desc)
137 int i, j, count = 0, descriptor = ecomp->number;
138 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
139 struct ses_device *ses_dev = edev->scratch;
140 unsigned char *type_ptr = ses_dev->page1_types;
141 unsigned char *desc_ptr = ses_dev->page2 + 8;
143 /* Clear everything */
144 memset(desc_ptr, 0, ses_dev->page2_len - 8);
145 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
146 for (j = 0; j < type_ptr[1]; j++) {
147 desc_ptr += 4;
148 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
149 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
150 continue;
151 if (count++ == descriptor) {
152 memcpy(desc_ptr, desc, 4);
153 /* set select */
154 desc_ptr[0] |= 0x80;
155 /* clear reserved, just in case */
156 desc_ptr[0] &= 0xf0;
161 return ses_send_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len);
164 static unsigned char *ses_get_page2_descriptor(struct enclosure_device *edev,
165 struct enclosure_component *ecomp)
167 int i, j, count = 0, descriptor = ecomp->number;
168 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
169 struct ses_device *ses_dev = edev->scratch;
170 unsigned char *type_ptr = ses_dev->page1_types;
171 unsigned char *desc_ptr = ses_dev->page2 + 8;
173 if (ses_recv_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len) < 0)
174 return NULL;
176 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
177 for (j = 0; j < type_ptr[1]; j++) {
178 desc_ptr += 4;
179 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
180 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
181 continue;
182 if (count++ == descriptor)
183 return desc_ptr;
186 return NULL;
189 /* For device slot and array device slot elements, byte 3 bit 6
190 * is "fault sensed" while byte 3 bit 5 is "fault reqstd". As this
191 * code stands these bits are shifted 4 positions right so in
192 * sysfs they will appear as bits 2 and 1 respectively. Strange. */
193 static void ses_get_fault(struct enclosure_device *edev,
194 struct enclosure_component *ecomp)
196 unsigned char *desc;
198 if (!ses_page2_supported(edev)) {
199 ecomp->fault = 0;
200 return;
202 desc = ses_get_page2_descriptor(edev, ecomp);
203 if (desc)
204 ecomp->fault = (desc[3] & 0x60) >> 4;
207 static int ses_set_fault(struct enclosure_device *edev,
208 struct enclosure_component *ecomp,
209 enum enclosure_component_setting val)
211 unsigned char desc[4];
212 unsigned char *desc_ptr;
214 if (!ses_page2_supported(edev))
215 return -EINVAL;
217 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
219 if (!desc_ptr)
220 return -EIO;
222 init_device_slot_control(desc, ecomp, desc_ptr);
224 switch (val) {
225 case ENCLOSURE_SETTING_DISABLED:
226 desc[3] &= 0xdf;
227 break;
228 case ENCLOSURE_SETTING_ENABLED:
229 desc[3] |= 0x20;
230 break;
231 default:
232 /* SES doesn't do the SGPIO blink settings */
233 return -EINVAL;
236 return ses_set_page2_descriptor(edev, ecomp, desc);
239 static void ses_get_status(struct enclosure_device *edev,
240 struct enclosure_component *ecomp)
242 unsigned char *desc;
244 if (!ses_page2_supported(edev)) {
245 ecomp->status = 0;
246 return;
248 desc = ses_get_page2_descriptor(edev, ecomp);
249 if (desc)
250 ecomp->status = (desc[0] & 0x0f);
253 static void ses_get_locate(struct enclosure_device *edev,
254 struct enclosure_component *ecomp)
256 unsigned char *desc;
258 if (!ses_page2_supported(edev)) {
259 ecomp->locate = 0;
260 return;
262 desc = ses_get_page2_descriptor(edev, ecomp);
263 if (desc)
264 ecomp->locate = (desc[2] & 0x02) ? 1 : 0;
267 static int ses_set_locate(struct enclosure_device *edev,
268 struct enclosure_component *ecomp,
269 enum enclosure_component_setting val)
271 unsigned char desc[4];
272 unsigned char *desc_ptr;
274 if (!ses_page2_supported(edev))
275 return -EINVAL;
277 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
279 if (!desc_ptr)
280 return -EIO;
282 init_device_slot_control(desc, ecomp, desc_ptr);
284 switch (val) {
285 case ENCLOSURE_SETTING_DISABLED:
286 desc[2] &= 0xfd;
287 break;
288 case ENCLOSURE_SETTING_ENABLED:
289 desc[2] |= 0x02;
290 break;
291 default:
292 /* SES doesn't do the SGPIO blink settings */
293 return -EINVAL;
295 return ses_set_page2_descriptor(edev, ecomp, desc);
298 static int ses_set_active(struct enclosure_device *edev,
299 struct enclosure_component *ecomp,
300 enum enclosure_component_setting val)
302 unsigned char desc[4];
303 unsigned char *desc_ptr;
305 if (!ses_page2_supported(edev))
306 return -EINVAL;
308 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
310 if (!desc_ptr)
311 return -EIO;
313 init_device_slot_control(desc, ecomp, desc_ptr);
315 switch (val) {
316 case ENCLOSURE_SETTING_DISABLED:
317 desc[2] &= 0x7f;
318 ecomp->active = 0;
319 break;
320 case ENCLOSURE_SETTING_ENABLED:
321 desc[2] |= 0x80;
322 ecomp->active = 1;
323 break;
324 default:
325 /* SES doesn't do the SGPIO blink settings */
326 return -EINVAL;
328 return ses_set_page2_descriptor(edev, ecomp, desc);
331 static int ses_show_id(struct enclosure_device *edev, char *buf)
333 struct ses_device *ses_dev = edev->scratch;
334 unsigned long long id = get_unaligned_be64(ses_dev->page1+8+4);
336 return sprintf(buf, "%#llx\n", id);
339 static void ses_get_power_status(struct enclosure_device *edev,
340 struct enclosure_component *ecomp)
342 unsigned char *desc;
344 if (!ses_page2_supported(edev)) {
345 ecomp->power_status = 0;
346 return;
349 desc = ses_get_page2_descriptor(edev, ecomp);
350 if (desc)
351 ecomp->power_status = (desc[3] & 0x10) ? 0 : 1;
354 static int ses_set_power_status(struct enclosure_device *edev,
355 struct enclosure_component *ecomp,
356 int val)
358 unsigned char desc[4];
359 unsigned char *desc_ptr;
361 if (!ses_page2_supported(edev))
362 return -EINVAL;
364 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
366 if (!desc_ptr)
367 return -EIO;
369 init_device_slot_control(desc, ecomp, desc_ptr);
371 switch (val) {
372 /* power = 1 is device_off = 0 and vice versa */
373 case 0:
374 desc[3] |= 0x10;
375 break;
376 case 1:
377 desc[3] &= 0xef;
378 break;
379 default:
380 return -EINVAL;
382 ecomp->power_status = val;
383 return ses_set_page2_descriptor(edev, ecomp, desc);
386 static struct enclosure_component_callbacks ses_enclosure_callbacks = {
387 .get_fault = ses_get_fault,
388 .set_fault = ses_set_fault,
389 .get_status = ses_get_status,
390 .get_locate = ses_get_locate,
391 .set_locate = ses_set_locate,
392 .get_power_status = ses_get_power_status,
393 .set_power_status = ses_set_power_status,
394 .set_active = ses_set_active,
395 .show_id = ses_show_id,
398 struct ses_host_edev {
399 struct Scsi_Host *shost;
400 struct enclosure_device *edev;
403 #if 0
404 int ses_match_host(struct enclosure_device *edev, void *data)
406 struct ses_host_edev *sed = data;
407 struct scsi_device *sdev;
409 if (!scsi_is_sdev_device(edev->edev.parent))
410 return 0;
412 sdev = to_scsi_device(edev->edev.parent);
414 if (sdev->host != sed->shost)
415 return 0;
417 sed->edev = edev;
418 return 1;
420 #endif /* 0 */
422 static void ses_process_descriptor(struct enclosure_component *ecomp,
423 unsigned char *desc)
425 int eip = desc[0] & 0x10;
426 int invalid = desc[0] & 0x80;
427 enum scsi_protocol proto = desc[0] & 0x0f;
428 u64 addr = 0;
429 int slot = -1;
430 struct ses_component *scomp = ecomp->scratch;
431 unsigned char *d;
433 if (invalid)
434 return;
436 switch (proto) {
437 case SCSI_PROTOCOL_FCP:
438 if (eip) {
439 d = desc + 4;
440 slot = d[3];
442 break;
443 case SCSI_PROTOCOL_SAS:
444 if (eip) {
445 d = desc + 4;
446 slot = d[3];
447 d = desc + 8;
448 } else
449 d = desc + 4;
450 /* only take the phy0 addr */
451 addr = (u64)d[12] << 56 |
452 (u64)d[13] << 48 |
453 (u64)d[14] << 40 |
454 (u64)d[15] << 32 |
455 (u64)d[16] << 24 |
456 (u64)d[17] << 16 |
457 (u64)d[18] << 8 |
458 (u64)d[19];
459 break;
460 default:
461 /* FIXME: Need to add more protocols than just SAS */
462 break;
464 ecomp->slot = slot;
465 scomp->addr = addr;
468 struct efd {
469 u64 addr;
470 struct device *dev;
473 static int ses_enclosure_find_by_addr(struct enclosure_device *edev,
474 void *data)
476 struct efd *efd = data;
477 int i;
478 struct ses_component *scomp;
480 if (!edev->component[0].scratch)
481 return 0;
483 for (i = 0; i < edev->components; i++) {
484 scomp = edev->component[i].scratch;
485 if (scomp->addr != efd->addr)
486 continue;
488 if (enclosure_add_device(edev, i, efd->dev) == 0)
489 kobject_uevent(&efd->dev->kobj, KOBJ_CHANGE);
490 return 1;
492 return 0;
495 #define INIT_ALLOC_SIZE 32
497 static void ses_enclosure_data_process(struct enclosure_device *edev,
498 struct scsi_device *sdev,
499 int create)
501 u32 result;
502 unsigned char *buf = NULL, *type_ptr, *desc_ptr, *addl_desc_ptr = NULL;
503 int i, j, page7_len, len, components;
504 struct ses_device *ses_dev = edev->scratch;
505 int types = ses_dev->page1_num_types;
506 unsigned char *hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
508 if (!hdr_buf)
509 goto simple_populate;
511 /* re-read page 10 */
512 if (ses_dev->page10)
513 ses_recv_diag(sdev, 10, ses_dev->page10, ses_dev->page10_len);
514 /* Page 7 for the descriptors is optional */
515 result = ses_recv_diag(sdev, 7, hdr_buf, INIT_ALLOC_SIZE);
516 if (result)
517 goto simple_populate;
519 page7_len = len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
520 /* add 1 for trailing '\0' we'll use */
521 buf = kzalloc(len + 1, GFP_KERNEL);
522 if (!buf)
523 goto simple_populate;
524 result = ses_recv_diag(sdev, 7, buf, len);
525 if (result) {
526 simple_populate:
527 kfree(buf);
528 buf = NULL;
529 desc_ptr = NULL;
530 len = 0;
531 page7_len = 0;
532 } else {
533 desc_ptr = buf + 8;
534 len = (desc_ptr[2] << 8) + desc_ptr[3];
535 /* skip past overall descriptor */
536 desc_ptr += len + 4;
538 if (ses_dev->page10)
539 addl_desc_ptr = ses_dev->page10 + 8;
540 type_ptr = ses_dev->page1_types;
541 components = 0;
542 for (i = 0; i < types; i++, type_ptr += 4) {
543 for (j = 0; j < type_ptr[1]; j++) {
544 char *name = NULL;
545 struct enclosure_component *ecomp;
547 if (desc_ptr) {
548 if (desc_ptr >= buf + page7_len) {
549 desc_ptr = NULL;
550 } else {
551 len = (desc_ptr[2] << 8) + desc_ptr[3];
552 desc_ptr += 4;
553 /* Add trailing zero - pushes into
554 * reserved space */
555 desc_ptr[len] = '\0';
556 name = desc_ptr;
559 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
560 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) {
562 if (create)
563 ecomp = enclosure_component_alloc(
564 edev,
565 components++,
566 type_ptr[0],
567 name);
568 else
569 ecomp = &edev->component[components++];
571 if (!IS_ERR(ecomp)) {
572 if (addl_desc_ptr)
573 ses_process_descriptor(
574 ecomp,
575 addl_desc_ptr);
576 if (create)
577 enclosure_component_register(
578 ecomp);
581 if (desc_ptr)
582 desc_ptr += len;
584 if (addl_desc_ptr &&
585 /* only find additional descriptions for specific devices */
586 (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
587 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE ||
588 type_ptr[0] == ENCLOSURE_COMPONENT_SAS_EXPANDER ||
589 /* these elements are optional */
590 type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_TARGET_PORT ||
591 type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_INITIATOR_PORT ||
592 type_ptr[0] == ENCLOSURE_COMPONENT_CONTROLLER_ELECTRONICS))
593 addl_desc_ptr += addl_desc_ptr[1] + 2;
597 kfree(buf);
598 kfree(hdr_buf);
601 static void ses_match_to_enclosure(struct enclosure_device *edev,
602 struct scsi_device *sdev,
603 int refresh)
605 struct scsi_device *edev_sdev = to_scsi_device(edev->edev.parent);
606 struct efd efd = {
607 .addr = 0,
610 if (refresh)
611 ses_enclosure_data_process(edev, edev_sdev, 0);
613 if (scsi_is_sas_rphy(sdev->sdev_target->dev.parent))
614 efd.addr = sas_get_address(sdev);
616 if (efd.addr) {
617 efd.dev = &sdev->sdev_gendev;
619 enclosure_for_each_device(ses_enclosure_find_by_addr, &efd);
623 static int ses_intf_add(struct device *cdev,
624 struct class_interface *intf)
626 struct scsi_device *sdev = to_scsi_device(cdev->parent);
627 struct scsi_device *tmp_sdev;
628 unsigned char *buf = NULL, *hdr_buf, *type_ptr, page;
629 struct ses_device *ses_dev;
630 u32 result;
631 int i, types, len, components = 0;
632 int err = -ENOMEM;
633 int num_enclosures;
634 struct enclosure_device *edev;
635 struct ses_component *scomp = NULL;
637 if (!scsi_device_enclosure(sdev)) {
638 /* not an enclosure, but might be in one */
639 struct enclosure_device *prev = NULL;
641 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
642 ses_match_to_enclosure(edev, sdev, 1);
643 prev = edev;
645 return -ENODEV;
648 /* TYPE_ENCLOSURE prints a message in probe */
649 if (sdev->type != TYPE_ENCLOSURE)
650 sdev_printk(KERN_NOTICE, sdev, "Embedded Enclosure Device\n");
652 ses_dev = kzalloc(sizeof(*ses_dev), GFP_KERNEL);
653 hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
654 if (!hdr_buf || !ses_dev)
655 goto err_init_free;
657 page = 1;
658 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
659 if (result)
660 goto recv_failed;
662 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
663 buf = kzalloc(len, GFP_KERNEL);
664 if (!buf)
665 goto err_free;
667 result = ses_recv_diag(sdev, page, buf, len);
668 if (result)
669 goto recv_failed;
671 types = 0;
673 /* we always have one main enclosure and the rest are referred
674 * to as secondary subenclosures */
675 num_enclosures = buf[1] + 1;
677 /* begin at the enclosure descriptor */
678 type_ptr = buf + 8;
679 /* skip all the enclosure descriptors */
680 for (i = 0; i < num_enclosures && type_ptr < buf + len; i++) {
681 types += type_ptr[2];
682 type_ptr += type_ptr[3] + 4;
685 ses_dev->page1_types = type_ptr;
686 ses_dev->page1_num_types = types;
688 for (i = 0; i < types && type_ptr < buf + len; i++, type_ptr += 4) {
689 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
690 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE)
691 components += type_ptr[1];
693 ses_dev->page1 = buf;
694 ses_dev->page1_len = len;
695 buf = NULL;
697 page = 2;
698 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
699 if (result)
700 goto page2_not_supported;
702 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
703 buf = kzalloc(len, GFP_KERNEL);
704 if (!buf)
705 goto err_free;
707 /* make sure getting page 2 actually works */
708 result = ses_recv_diag(sdev, 2, buf, len);
709 if (result)
710 goto recv_failed;
711 ses_dev->page2 = buf;
712 ses_dev->page2_len = len;
713 buf = NULL;
715 /* The additional information page --- allows us
716 * to match up the devices */
717 page = 10;
718 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
719 if (!result) {
721 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
722 buf = kzalloc(len, GFP_KERNEL);
723 if (!buf)
724 goto err_free;
726 result = ses_recv_diag(sdev, page, buf, len);
727 if (result)
728 goto recv_failed;
729 ses_dev->page10 = buf;
730 ses_dev->page10_len = len;
731 buf = NULL;
733 page2_not_supported:
734 scomp = kcalloc(components, sizeof(struct ses_component), GFP_KERNEL);
735 if (!scomp)
736 goto err_free;
738 edev = enclosure_register(cdev->parent, dev_name(&sdev->sdev_gendev),
739 components, &ses_enclosure_callbacks);
740 if (IS_ERR(edev)) {
741 err = PTR_ERR(edev);
742 goto err_free;
745 kfree(hdr_buf);
747 edev->scratch = ses_dev;
748 for (i = 0; i < components; i++)
749 edev->component[i].scratch = scomp + i;
751 ses_enclosure_data_process(edev, sdev, 1);
753 /* see if there are any devices matching before
754 * we found the enclosure */
755 shost_for_each_device(tmp_sdev, sdev->host) {
756 if (tmp_sdev->lun != 0 || scsi_device_enclosure(tmp_sdev))
757 continue;
758 ses_match_to_enclosure(edev, tmp_sdev, 0);
761 return 0;
763 recv_failed:
764 sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n",
765 page);
766 err = -ENODEV;
767 err_free:
768 kfree(buf);
769 kfree(scomp);
770 kfree(ses_dev->page10);
771 kfree(ses_dev->page2);
772 kfree(ses_dev->page1);
773 err_init_free:
774 kfree(ses_dev);
775 kfree(hdr_buf);
776 sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err);
777 return err;
780 static int ses_remove(struct device *dev)
782 return 0;
785 static void ses_intf_remove_component(struct scsi_device *sdev)
787 struct enclosure_device *edev, *prev = NULL;
789 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
790 prev = edev;
791 if (!enclosure_remove_device(edev, &sdev->sdev_gendev))
792 break;
794 if (edev)
795 put_device(&edev->edev);
798 static void ses_intf_remove_enclosure(struct scsi_device *sdev)
800 struct enclosure_device *edev;
801 struct ses_device *ses_dev;
803 /* exact match to this enclosure */
804 edev = enclosure_find(&sdev->sdev_gendev, NULL);
805 if (!edev)
806 return;
808 ses_dev = edev->scratch;
809 edev->scratch = NULL;
811 kfree(ses_dev->page10);
812 kfree(ses_dev->page1);
813 kfree(ses_dev->page2);
814 kfree(ses_dev);
816 kfree(edev->component[0].scratch);
818 put_device(&edev->edev);
819 enclosure_unregister(edev);
822 static void ses_intf_remove(struct device *cdev,
823 struct class_interface *intf)
825 struct scsi_device *sdev = to_scsi_device(cdev->parent);
827 if (!scsi_device_enclosure(sdev))
828 ses_intf_remove_component(sdev);
829 else
830 ses_intf_remove_enclosure(sdev);
833 static struct class_interface ses_interface = {
834 .add_dev = ses_intf_add,
835 .remove_dev = ses_intf_remove,
838 static struct scsi_driver ses_template = {
839 .gendrv = {
840 .name = "ses",
841 .owner = THIS_MODULE,
842 .probe = ses_probe,
843 .remove = ses_remove,
847 static int __init ses_init(void)
849 int err;
851 err = scsi_register_interface(&ses_interface);
852 if (err)
853 return err;
855 err = scsi_register_driver(&ses_template.gendrv);
856 if (err)
857 goto out_unreg;
859 return 0;
861 out_unreg:
862 scsi_unregister_interface(&ses_interface);
863 return err;
866 static void __exit ses_exit(void)
868 scsi_unregister_driver(&ses_template.gendrv);
869 scsi_unregister_interface(&ses_interface);
872 module_init(ses_init);
873 module_exit(ses_exit);
875 MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE);
877 MODULE_AUTHOR("James Bottomley");
878 MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
879 MODULE_LICENSE("GPL v2");