1 // SPDX-License-Identifier: GPL-2.0-only
3 * SCSI Enclosure Services
5 * Copyright (C) 2008 James Bottomley <James.Bottomley@HansenPartnership.com>
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>
25 unsigned char *page1_types;
27 unsigned char *page10;
29 short page1_num_types;
34 struct ses_component {
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);
50 if (sdev->type != TYPE_ENCLOSURE)
54 sdev_printk(KERN_NOTICE, sdev, "Attached Enclosure device\n");
60 #define SES_TIMEOUT (30 * HZ)
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);
69 /* only clear byte 1 for ENCLOSURE_COMPONENT_DEVICE */
70 if (ecomp->type == ENCLOSURE_COMPONENT_DEVICE)
77 static int ses_recv_diag(struct scsi_device *sdev, int page_code,
78 void *buf, int bufflen)
81 unsigned char cmd[] = {
89 unsigned char recv_page_code;
90 unsigned int retries = SES_RETRIES;
91 struct scsi_sense_hdr sshdr;
94 ret = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, bufflen,
95 &sshdr, SES_TIMEOUT, 1, NULL);
96 } while (ret > 0 && --retries && scsi_sense_valid(&sshdr) &&
97 (sshdr.sense_key == NOT_READY ||
98 (sshdr.sense_key == UNIT_ATTENTION && sshdr.asc == 0x29)));
103 recv_page_code = ((unsigned char *)buf)[0];
105 if (likely(recv_page_code == page_code))
108 /* successful diagnostic but wrong page code. This happens to some
109 * USB devices, just print a message and pretend there was an error */
111 sdev_printk(KERN_ERR, sdev,
112 "Wrong diagnostic page; asked for %d got %u\n",
113 page_code, recv_page_code);
118 static int ses_send_diag(struct scsi_device *sdev, int page_code,
119 void *buf, int bufflen)
123 unsigned char cmd[] = {
125 0x10, /* Set PF bit */
131 struct scsi_sense_hdr sshdr;
132 unsigned int retries = SES_RETRIES;
135 result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, buf, bufflen,
136 &sshdr, SES_TIMEOUT, 1, NULL);
137 } while (result > 0 && --retries && scsi_sense_valid(&sshdr) &&
138 (sshdr.sense_key == NOT_READY ||
139 (sshdr.sense_key == UNIT_ATTENTION && sshdr.asc == 0x29)));
142 sdev_printk(KERN_ERR, sdev, "SEND DIAGNOSTIC result: %8x\n",
147 static int ses_set_page2_descriptor(struct enclosure_device *edev,
148 struct enclosure_component *ecomp,
151 int i, j, count = 0, descriptor = ecomp->number;
152 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
153 struct ses_device *ses_dev = edev->scratch;
154 unsigned char *type_ptr = ses_dev->page1_types;
155 unsigned char *desc_ptr = ses_dev->page2 + 8;
157 /* Clear everything */
158 memset(desc_ptr, 0, ses_dev->page2_len - 8);
159 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
160 for (j = 0; j < type_ptr[1]; j++) {
162 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
163 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
165 if (count++ == descriptor) {
166 memcpy(desc_ptr, desc, 4);
169 /* clear reserved, just in case */
175 return ses_send_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len);
178 static unsigned char *ses_get_page2_descriptor(struct enclosure_device *edev,
179 struct enclosure_component *ecomp)
181 int i, j, count = 0, descriptor = ecomp->number;
182 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
183 struct ses_device *ses_dev = edev->scratch;
184 unsigned char *type_ptr = ses_dev->page1_types;
185 unsigned char *desc_ptr = ses_dev->page2 + 8;
187 if (ses_recv_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len) < 0)
190 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
191 for (j = 0; j < type_ptr[1]; j++) {
193 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
194 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
196 if (count++ == descriptor)
203 /* For device slot and array device slot elements, byte 3 bit 6
204 * is "fault sensed" while byte 3 bit 5 is "fault reqstd". As this
205 * code stands these bits are shifted 4 positions right so in
206 * sysfs they will appear as bits 2 and 1 respectively. Strange. */
207 static void ses_get_fault(struct enclosure_device *edev,
208 struct enclosure_component *ecomp)
212 if (!ses_page2_supported(edev)) {
216 desc = ses_get_page2_descriptor(edev, ecomp);
218 ecomp->fault = (desc[3] & 0x60) >> 4;
221 static int ses_set_fault(struct enclosure_device *edev,
222 struct enclosure_component *ecomp,
223 enum enclosure_component_setting val)
225 unsigned char desc[4];
226 unsigned char *desc_ptr;
228 if (!ses_page2_supported(edev))
231 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
236 init_device_slot_control(desc, ecomp, desc_ptr);
239 case ENCLOSURE_SETTING_DISABLED:
242 case ENCLOSURE_SETTING_ENABLED:
246 /* SES doesn't do the SGPIO blink settings */
250 return ses_set_page2_descriptor(edev, ecomp, desc);
253 static void ses_get_status(struct enclosure_device *edev,
254 struct enclosure_component *ecomp)
258 if (!ses_page2_supported(edev)) {
262 desc = ses_get_page2_descriptor(edev, ecomp);
264 ecomp->status = (desc[0] & 0x0f);
267 static void ses_get_locate(struct enclosure_device *edev,
268 struct enclosure_component *ecomp)
272 if (!ses_page2_supported(edev)) {
276 desc = ses_get_page2_descriptor(edev, ecomp);
278 ecomp->locate = (desc[2] & 0x02) ? 1 : 0;
281 static int ses_set_locate(struct enclosure_device *edev,
282 struct enclosure_component *ecomp,
283 enum enclosure_component_setting val)
285 unsigned char desc[4];
286 unsigned char *desc_ptr;
288 if (!ses_page2_supported(edev))
291 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
296 init_device_slot_control(desc, ecomp, desc_ptr);
299 case ENCLOSURE_SETTING_DISABLED:
302 case ENCLOSURE_SETTING_ENABLED:
306 /* SES doesn't do the SGPIO blink settings */
309 return ses_set_page2_descriptor(edev, ecomp, desc);
312 static int ses_set_active(struct enclosure_device *edev,
313 struct enclosure_component *ecomp,
314 enum enclosure_component_setting val)
316 unsigned char desc[4];
317 unsigned char *desc_ptr;
319 if (!ses_page2_supported(edev))
322 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
327 init_device_slot_control(desc, ecomp, desc_ptr);
330 case ENCLOSURE_SETTING_DISABLED:
334 case ENCLOSURE_SETTING_ENABLED:
339 /* SES doesn't do the SGPIO blink settings */
342 return ses_set_page2_descriptor(edev, ecomp, desc);
345 static int ses_show_id(struct enclosure_device *edev, char *buf)
347 struct ses_device *ses_dev = edev->scratch;
348 unsigned long long id = get_unaligned_be64(ses_dev->page1+8+4);
350 return sprintf(buf, "%#llx\n", id);
353 static void ses_get_power_status(struct enclosure_device *edev,
354 struct enclosure_component *ecomp)
358 if (!ses_page2_supported(edev)) {
359 ecomp->power_status = 0;
363 desc = ses_get_page2_descriptor(edev, ecomp);
365 ecomp->power_status = (desc[3] & 0x10) ? 0 : 1;
368 static int ses_set_power_status(struct enclosure_device *edev,
369 struct enclosure_component *ecomp,
372 unsigned char desc[4];
373 unsigned char *desc_ptr;
375 if (!ses_page2_supported(edev))
378 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
383 init_device_slot_control(desc, ecomp, desc_ptr);
386 /* power = 1 is device_off = 0 and vice versa */
396 ecomp->power_status = val;
397 return ses_set_page2_descriptor(edev, ecomp, desc);
400 static struct enclosure_component_callbacks ses_enclosure_callbacks = {
401 .get_fault = ses_get_fault,
402 .set_fault = ses_set_fault,
403 .get_status = ses_get_status,
404 .get_locate = ses_get_locate,
405 .set_locate = ses_set_locate,
406 .get_power_status = ses_get_power_status,
407 .set_power_status = ses_set_power_status,
408 .set_active = ses_set_active,
409 .show_id = ses_show_id,
412 struct ses_host_edev {
413 struct Scsi_Host *shost;
414 struct enclosure_device *edev;
418 int ses_match_host(struct enclosure_device *edev, void *data)
420 struct ses_host_edev *sed = data;
421 struct scsi_device *sdev;
423 if (!scsi_is_sdev_device(edev->edev.parent))
426 sdev = to_scsi_device(edev->edev.parent);
428 if (sdev->host != sed->shost)
436 static int ses_process_descriptor(struct enclosure_component *ecomp,
437 unsigned char *desc, int max_desc_len)
439 int eip = desc[0] & 0x10;
440 int invalid = desc[0] & 0x80;
441 enum scsi_protocol proto = desc[0] & 0x0f;
444 struct ses_component *scomp = ecomp->scratch;
451 case SCSI_PROTOCOL_FCP:
453 if (max_desc_len <= 7)
459 case SCSI_PROTOCOL_SAS:
462 if (max_desc_len <= 27)
468 if (max_desc_len <= 23)
474 /* only take the phy0 addr */
475 addr = (u64)d[12] << 56 |
485 /* FIXME: Need to add more protocols than just SAS */
499 static int ses_enclosure_find_by_addr(struct enclosure_device *edev,
502 struct efd *efd = data;
504 struct ses_component *scomp;
506 for (i = 0; i < edev->components; i++) {
507 scomp = edev->component[i].scratch;
508 if (scomp->addr != efd->addr)
511 if (enclosure_add_device(edev, i, efd->dev) == 0)
512 kobject_uevent(&efd->dev->kobj, KOBJ_CHANGE);
518 #define INIT_ALLOC_SIZE 32
520 static void ses_enclosure_data_process(struct enclosure_device *edev,
521 struct scsi_device *sdev,
525 unsigned char *buf = NULL, *type_ptr, *desc_ptr, *addl_desc_ptr = NULL;
526 int i, j, page7_len, len, components;
527 struct ses_device *ses_dev = edev->scratch;
528 int types = ses_dev->page1_num_types;
529 unsigned char *hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
532 goto simple_populate;
534 /* re-read page 10 */
536 ses_recv_diag(sdev, 10, ses_dev->page10, ses_dev->page10_len);
537 /* Page 7 for the descriptors is optional */
538 result = ses_recv_diag(sdev, 7, hdr_buf, INIT_ALLOC_SIZE);
540 goto simple_populate;
542 page7_len = len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
543 /* add 1 for trailing '\0' we'll use */
544 buf = kzalloc(len + 1, GFP_KERNEL);
546 goto simple_populate;
547 result = ses_recv_diag(sdev, 7, buf, len);
557 len = (desc_ptr[2] << 8) + desc_ptr[3];
558 /* skip past overall descriptor */
561 if (ses_dev->page10 && ses_dev->page10_len > 9)
562 addl_desc_ptr = ses_dev->page10 + 8;
563 type_ptr = ses_dev->page1_types;
565 for (i = 0; i < types; i++, type_ptr += 4) {
566 for (j = 0; j < type_ptr[1]; j++) {
568 struct enclosure_component *ecomp;
572 if (desc_ptr + 3 >= buf + page7_len) {
575 len = (desc_ptr[2] << 8) + desc_ptr[3];
577 if (desc_ptr + len > buf + page7_len)
580 /* Add trailing zero - pushes into
582 desc_ptr[len] = '\0';
587 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
588 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) {
591 ecomp = enclosure_component_alloc(
596 else if (components < edev->components)
597 ecomp = &edev->component[components++];
599 ecomp = ERR_PTR(-EINVAL);
601 if (!IS_ERR(ecomp)) {
603 max_desc_len = ses_dev->page10_len -
604 (addl_desc_ptr - ses_dev->page10);
605 if (ses_process_descriptor(ecomp,
608 addl_desc_ptr = NULL;
611 enclosure_component_register(
619 /* only find additional descriptions for specific devices */
620 (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
621 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE ||
622 type_ptr[0] == ENCLOSURE_COMPONENT_SAS_EXPANDER ||
623 /* these elements are optional */
624 type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_TARGET_PORT ||
625 type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_INITIATOR_PORT ||
626 type_ptr[0] == ENCLOSURE_COMPONENT_CONTROLLER_ELECTRONICS)) {
627 addl_desc_ptr += addl_desc_ptr[1] + 2;
628 if (addl_desc_ptr + 1 >= ses_dev->page10 + ses_dev->page10_len)
629 addl_desc_ptr = NULL;
637 static void ses_match_to_enclosure(struct enclosure_device *edev,
638 struct scsi_device *sdev,
641 struct scsi_device *edev_sdev = to_scsi_device(edev->edev.parent);
647 ses_enclosure_data_process(edev, edev_sdev, 0);
649 if (scsi_is_sas_rphy(sdev->sdev_target->dev.parent))
650 efd.addr = sas_get_address(sdev);
653 efd.dev = &sdev->sdev_gendev;
655 enclosure_for_each_device(ses_enclosure_find_by_addr, &efd);
659 static int ses_intf_add(struct device *cdev,
660 struct class_interface *intf)
662 struct scsi_device *sdev = to_scsi_device(cdev->parent);
663 struct scsi_device *tmp_sdev;
664 unsigned char *buf = NULL, *hdr_buf, *type_ptr, page;
665 struct ses_device *ses_dev;
667 int i, types, len, components = 0;
670 struct enclosure_device *edev;
671 struct ses_component *scomp = NULL;
673 if (!scsi_device_enclosure(sdev)) {
674 /* not an enclosure, but might be in one */
675 struct enclosure_device *prev = NULL;
677 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
678 ses_match_to_enclosure(edev, sdev, 1);
684 /* TYPE_ENCLOSURE prints a message in probe */
685 if (sdev->type != TYPE_ENCLOSURE)
686 sdev_printk(KERN_NOTICE, sdev, "Embedded Enclosure Device\n");
688 ses_dev = kzalloc(sizeof(*ses_dev), GFP_KERNEL);
689 hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
690 if (!hdr_buf || !ses_dev)
694 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
698 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
699 buf = kzalloc(len, GFP_KERNEL);
703 result = ses_recv_diag(sdev, page, buf, len);
709 /* we always have one main enclosure and the rest are referred
710 * to as secondary subenclosures */
711 num_enclosures = buf[1] + 1;
713 /* begin at the enclosure descriptor */
715 /* skip all the enclosure descriptors */
716 for (i = 0; i < num_enclosures && type_ptr < buf + len; i++) {
717 types += type_ptr[2];
718 type_ptr += type_ptr[3] + 4;
721 ses_dev->page1_types = type_ptr;
722 ses_dev->page1_num_types = types;
724 for (i = 0; i < types && type_ptr < buf + len; i++, type_ptr += 4) {
725 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
726 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE)
727 components += type_ptr[1];
730 ses_dev->page1 = buf;
731 ses_dev->page1_len = len;
735 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
737 goto page2_not_supported;
739 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
740 buf = kzalloc(len, GFP_KERNEL);
744 /* make sure getting page 2 actually works */
745 result = ses_recv_diag(sdev, 2, buf, len);
748 ses_dev->page2 = buf;
749 ses_dev->page2_len = len;
752 /* The additional information page --- allows us
753 * to match up the devices */
755 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
758 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
759 buf = kzalloc(len, GFP_KERNEL);
763 result = ses_recv_diag(sdev, page, buf, len);
766 ses_dev->page10 = buf;
767 ses_dev->page10_len = len;
771 if (components > 0) {
772 scomp = kcalloc(components, sizeof(struct ses_component), GFP_KERNEL);
777 edev = enclosure_register(cdev->parent, dev_name(&sdev->sdev_gendev),
778 components, &ses_enclosure_callbacks);
786 edev->scratch = ses_dev;
787 for (i = 0; i < components; i++)
788 edev->component[i].scratch = scomp + i;
790 ses_enclosure_data_process(edev, sdev, 1);
792 /* see if there are any devices matching before
793 * we found the enclosure */
794 shost_for_each_device(tmp_sdev, sdev->host) {
795 if (tmp_sdev->lun != 0 || scsi_device_enclosure(tmp_sdev))
797 ses_match_to_enclosure(edev, tmp_sdev, 0);
803 sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n",
809 kfree(ses_dev->page10);
810 kfree(ses_dev->page2);
811 kfree(ses_dev->page1);
815 sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err);
819 static int ses_remove(struct device *dev)
824 static void ses_intf_remove_component(struct scsi_device *sdev)
826 struct enclosure_device *edev, *prev = NULL;
828 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
830 if (!enclosure_remove_device(edev, &sdev->sdev_gendev))
834 put_device(&edev->edev);
837 static void ses_intf_remove_enclosure(struct scsi_device *sdev)
839 struct enclosure_device *edev;
840 struct ses_device *ses_dev;
842 /* exact match to this enclosure */
843 edev = enclosure_find(&sdev->sdev_gendev, NULL);
847 ses_dev = edev->scratch;
848 edev->scratch = NULL;
850 kfree(ses_dev->page10);
851 kfree(ses_dev->page1);
852 kfree(ses_dev->page2);
855 if (edev->components)
856 kfree(edev->component[0].scratch);
858 put_device(&edev->edev);
859 enclosure_unregister(edev);
862 static void ses_intf_remove(struct device *cdev,
863 struct class_interface *intf)
865 struct scsi_device *sdev = to_scsi_device(cdev->parent);
867 if (!scsi_device_enclosure(sdev))
868 ses_intf_remove_component(sdev);
870 ses_intf_remove_enclosure(sdev);
873 static struct class_interface ses_interface = {
874 .add_dev = ses_intf_add,
875 .remove_dev = ses_intf_remove,
878 static struct scsi_driver ses_template = {
881 .owner = THIS_MODULE,
883 .remove = ses_remove,
887 static int __init ses_init(void)
891 err = scsi_register_interface(&ses_interface);
895 err = scsi_register_driver(&ses_template.gendrv);
902 scsi_unregister_interface(&ses_interface);
906 static void __exit ses_exit(void)
908 scsi_unregister_driver(&ses_template.gendrv);
909 scsi_unregister_interface(&ses_interface);
912 module_init(ses_init);
913 module_exit(ses_exit);
915 MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE);
917 MODULE_AUTHOR("James Bottomley");
918 MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
919 MODULE_LICENSE("GPL v2");