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;
91 ret = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, bufflen,
92 NULL, SES_TIMEOUT, SES_RETRIES, NULL);
96 recv_page_code = ((unsigned char *)buf)[0];
98 if (likely(recv_page_code == page_code))
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);
111 static int ses_send_diag(struct scsi_device *sdev, int page_code,
112 void *buf, int bufflen)
116 unsigned char cmd[] = {
118 0x10, /* Set PF bit */
125 result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, buf, bufflen,
126 NULL, SES_TIMEOUT, SES_RETRIES, NULL);
128 sdev_printk(KERN_ERR, sdev, "SEND DIAGNOSTIC result: %8x\n",
133 static int ses_set_page2_descriptor(struct enclosure_device *edev,
134 struct enclosure_component *ecomp,
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++) {
148 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
149 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
151 if (count++ == descriptor) {
152 memcpy(desc_ptr, desc, 4);
155 /* clear reserved, just in case */
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)
176 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
177 for (j = 0; j < type_ptr[1]; j++) {
179 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
180 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
182 if (count++ == descriptor)
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)
198 if (!ses_page2_supported(edev)) {
202 desc = ses_get_page2_descriptor(edev, ecomp);
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))
217 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
222 init_device_slot_control(desc, ecomp, desc_ptr);
225 case ENCLOSURE_SETTING_DISABLED:
228 case ENCLOSURE_SETTING_ENABLED:
232 /* SES doesn't do the SGPIO blink settings */
236 return ses_set_page2_descriptor(edev, ecomp, desc);
239 static void ses_get_status(struct enclosure_device *edev,
240 struct enclosure_component *ecomp)
244 if (!ses_page2_supported(edev)) {
248 desc = ses_get_page2_descriptor(edev, ecomp);
250 ecomp->status = (desc[0] & 0x0f);
253 static void ses_get_locate(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->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))
277 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
282 init_device_slot_control(desc, ecomp, desc_ptr);
285 case ENCLOSURE_SETTING_DISABLED:
288 case ENCLOSURE_SETTING_ENABLED:
292 /* SES doesn't do the SGPIO blink settings */
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))
308 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
313 init_device_slot_control(desc, ecomp, desc_ptr);
316 case ENCLOSURE_SETTING_DISABLED:
320 case ENCLOSURE_SETTING_ENABLED:
325 /* SES doesn't do the SGPIO blink settings */
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)
344 if (!ses_page2_supported(edev)) {
345 ecomp->power_status = 0;
349 desc = ses_get_page2_descriptor(edev, ecomp);
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,
358 unsigned char desc[4];
359 unsigned char *desc_ptr;
361 if (!ses_page2_supported(edev))
364 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
369 init_device_slot_control(desc, ecomp, desc_ptr);
372 /* power = 1 is device_off = 0 and vice versa */
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;
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))
412 sdev = to_scsi_device(edev->edev.parent);
414 if (sdev->host != sed->shost)
422 static void ses_process_descriptor(struct enclosure_component *ecomp,
425 int eip = desc[0] & 0x10;
426 int invalid = desc[0] & 0x80;
427 enum scsi_protocol proto = desc[0] & 0x0f;
430 struct ses_component *scomp = ecomp->scratch;
437 case SCSI_PROTOCOL_FCP:
443 case SCSI_PROTOCOL_SAS:
450 /* only take the phy0 addr */
451 addr = (u64)d[12] << 56 |
461 /* FIXME: Need to add more protocols than just SAS */
473 static int ses_enclosure_find_by_addr(struct enclosure_device *edev,
476 struct efd *efd = data;
478 struct ses_component *scomp;
480 if (!edev->component[0].scratch)
483 for (i = 0; i < edev->components; i++) {
484 scomp = edev->component[i].scratch;
485 if (scomp->addr != efd->addr)
488 if (enclosure_add_device(edev, i, efd->dev) == 0)
489 kobject_uevent(&efd->dev->kobj, KOBJ_CHANGE);
495 #define INIT_ALLOC_SIZE 32
497 static void ses_enclosure_data_process(struct enclosure_device *edev,
498 struct scsi_device *sdev,
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);
509 goto simple_populate;
511 /* re-read page 10 */
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);
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);
523 goto simple_populate;
524 result = ses_recv_diag(sdev, 7, buf, len);
534 len = (desc_ptr[2] << 8) + desc_ptr[3];
535 /* skip past overall descriptor */
539 addl_desc_ptr = ses_dev->page10 + 8;
540 type_ptr = ses_dev->page1_types;
542 for (i = 0; i < types; i++, type_ptr += 4) {
543 for (j = 0; j < type_ptr[1]; j++) {
545 struct enclosure_component *ecomp;
548 if (desc_ptr >= buf + page7_len) {
551 len = (desc_ptr[2] << 8) + desc_ptr[3];
553 /* Add trailing zero - pushes into
555 desc_ptr[len] = '\0';
559 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
560 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) {
563 ecomp = enclosure_component_alloc(
569 ecomp = &edev->component[components++];
571 if (!IS_ERR(ecomp)) {
573 ses_process_descriptor(
577 enclosure_component_register(
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;
601 static void ses_match_to_enclosure(struct enclosure_device *edev,
602 struct scsi_device *sdev,
605 struct scsi_device *edev_sdev = to_scsi_device(edev->edev.parent);
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);
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;
631 int i, types, len, components = 0;
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);
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)
658 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
662 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
663 buf = kzalloc(len, GFP_KERNEL);
667 result = ses_recv_diag(sdev, page, buf, len);
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 */
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;
698 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
700 goto page2_not_supported;
702 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
703 buf = kzalloc(len, GFP_KERNEL);
707 /* make sure getting page 2 actually works */
708 result = ses_recv_diag(sdev, 2, buf, len);
711 ses_dev->page2 = buf;
712 ses_dev->page2_len = len;
715 /* The additional information page --- allows us
716 * to match up the devices */
718 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
721 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
722 buf = kzalloc(len, GFP_KERNEL);
726 result = ses_recv_diag(sdev, page, buf, len);
729 ses_dev->page10 = buf;
730 ses_dev->page10_len = len;
734 scomp = kcalloc(components, sizeof(struct ses_component), GFP_KERNEL);
738 edev = enclosure_register(cdev->parent, dev_name(&sdev->sdev_gendev),
739 components, &ses_enclosure_callbacks);
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))
758 ses_match_to_enclosure(edev, tmp_sdev, 0);
764 sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n",
770 kfree(ses_dev->page10);
771 kfree(ses_dev->page2);
772 kfree(ses_dev->page1);
776 sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err);
780 static int ses_remove(struct device *dev)
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) {
791 if (!enclosure_remove_device(edev, &sdev->sdev_gendev))
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);
808 ses_dev = edev->scratch;
809 edev->scratch = NULL;
811 kfree(ses_dev->page10);
812 kfree(ses_dev->page1);
813 kfree(ses_dev->page2);
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);
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 = {
841 .owner = THIS_MODULE,
843 .remove = ses_remove,
847 static int __init ses_init(void)
851 err = scsi_register_interface(&ses_interface);
855 err = scsi_register_driver(&ses_template.gendrv);
862 scsi_unregister_interface(&ses_interface);
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");