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 void ses_process_descriptor(struct enclosure_component *ecomp,
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:
457 case SCSI_PROTOCOL_SAS:
464 /* only take the phy0 addr */
465 addr = (u64)d[12] << 56 |
475 /* FIXME: Need to add more protocols than just SAS */
487 static int ses_enclosure_find_by_addr(struct enclosure_device *edev,
490 struct efd *efd = data;
492 struct ses_component *scomp;
494 if (!edev->component[0].scratch)
497 for (i = 0; i < edev->components; i++) {
498 scomp = edev->component[i].scratch;
499 if (scomp->addr != efd->addr)
502 if (enclosure_add_device(edev, i, efd->dev) == 0)
503 kobject_uevent(&efd->dev->kobj, KOBJ_CHANGE);
509 #define INIT_ALLOC_SIZE 32
511 static void ses_enclosure_data_process(struct enclosure_device *edev,
512 struct scsi_device *sdev,
516 unsigned char *buf = NULL, *type_ptr, *desc_ptr, *addl_desc_ptr = NULL;
517 int i, j, page7_len, len, components;
518 struct ses_device *ses_dev = edev->scratch;
519 int types = ses_dev->page1_num_types;
520 unsigned char *hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
523 goto simple_populate;
525 /* re-read page 10 */
527 ses_recv_diag(sdev, 10, ses_dev->page10, ses_dev->page10_len);
528 /* Page 7 for the descriptors is optional */
529 result = ses_recv_diag(sdev, 7, hdr_buf, INIT_ALLOC_SIZE);
531 goto simple_populate;
533 page7_len = len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
534 /* add 1 for trailing '\0' we'll use */
535 buf = kzalloc(len + 1, GFP_KERNEL);
537 goto simple_populate;
538 result = ses_recv_diag(sdev, 7, buf, len);
548 len = (desc_ptr[2] << 8) + desc_ptr[3];
549 /* skip past overall descriptor */
553 addl_desc_ptr = ses_dev->page10 + 8;
554 type_ptr = ses_dev->page1_types;
556 for (i = 0; i < types; i++, type_ptr += 4) {
557 for (j = 0; j < type_ptr[1]; j++) {
559 struct enclosure_component *ecomp;
562 if (desc_ptr >= buf + page7_len) {
565 len = (desc_ptr[2] << 8) + desc_ptr[3];
567 /* Add trailing zero - pushes into
569 desc_ptr[len] = '\0';
573 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
574 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) {
577 ecomp = enclosure_component_alloc(
583 ecomp = &edev->component[components++];
585 if (!IS_ERR(ecomp)) {
587 ses_process_descriptor(
591 enclosure_component_register(
599 /* only find additional descriptions for specific devices */
600 (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
601 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE ||
602 type_ptr[0] == ENCLOSURE_COMPONENT_SAS_EXPANDER ||
603 /* these elements are optional */
604 type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_TARGET_PORT ||
605 type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_INITIATOR_PORT ||
606 type_ptr[0] == ENCLOSURE_COMPONENT_CONTROLLER_ELECTRONICS))
607 addl_desc_ptr += addl_desc_ptr[1] + 2;
615 static void ses_match_to_enclosure(struct enclosure_device *edev,
616 struct scsi_device *sdev,
619 struct scsi_device *edev_sdev = to_scsi_device(edev->edev.parent);
625 ses_enclosure_data_process(edev, edev_sdev, 0);
627 if (scsi_is_sas_rphy(sdev->sdev_target->dev.parent))
628 efd.addr = sas_get_address(sdev);
631 efd.dev = &sdev->sdev_gendev;
633 enclosure_for_each_device(ses_enclosure_find_by_addr, &efd);
637 static int ses_intf_add(struct device *cdev,
638 struct class_interface *intf)
640 struct scsi_device *sdev = to_scsi_device(cdev->parent);
641 struct scsi_device *tmp_sdev;
642 unsigned char *buf = NULL, *hdr_buf, *type_ptr, page;
643 struct ses_device *ses_dev;
645 int i, types, len, components = 0;
648 struct enclosure_device *edev;
649 struct ses_component *scomp = NULL;
651 if (!scsi_device_enclosure(sdev)) {
652 /* not an enclosure, but might be in one */
653 struct enclosure_device *prev = NULL;
655 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
656 ses_match_to_enclosure(edev, sdev, 1);
662 /* TYPE_ENCLOSURE prints a message in probe */
663 if (sdev->type != TYPE_ENCLOSURE)
664 sdev_printk(KERN_NOTICE, sdev, "Embedded Enclosure Device\n");
666 ses_dev = kzalloc(sizeof(*ses_dev), GFP_KERNEL);
667 hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
668 if (!hdr_buf || !ses_dev)
672 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
676 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
677 buf = kzalloc(len, GFP_KERNEL);
681 result = ses_recv_diag(sdev, page, buf, len);
687 /* we always have one main enclosure and the rest are referred
688 * to as secondary subenclosures */
689 num_enclosures = buf[1] + 1;
691 /* begin at the enclosure descriptor */
693 /* skip all the enclosure descriptors */
694 for (i = 0; i < num_enclosures && type_ptr < buf + len; i++) {
695 types += type_ptr[2];
696 type_ptr += type_ptr[3] + 4;
699 ses_dev->page1_types = type_ptr;
700 ses_dev->page1_num_types = types;
702 for (i = 0; i < types && type_ptr < buf + len; i++, type_ptr += 4) {
703 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
704 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE)
705 components += type_ptr[1];
707 ses_dev->page1 = buf;
708 ses_dev->page1_len = len;
712 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
714 goto page2_not_supported;
716 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
717 buf = kzalloc(len, GFP_KERNEL);
721 /* make sure getting page 2 actually works */
722 result = ses_recv_diag(sdev, 2, buf, len);
725 ses_dev->page2 = buf;
726 ses_dev->page2_len = len;
729 /* The additional information page --- allows us
730 * to match up the devices */
732 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
735 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
736 buf = kzalloc(len, GFP_KERNEL);
740 result = ses_recv_diag(sdev, page, buf, len);
743 ses_dev->page10 = buf;
744 ses_dev->page10_len = len;
748 scomp = kcalloc(components, sizeof(struct ses_component), GFP_KERNEL);
752 edev = enclosure_register(cdev->parent, dev_name(&sdev->sdev_gendev),
753 components, &ses_enclosure_callbacks);
761 edev->scratch = ses_dev;
762 for (i = 0; i < components; i++)
763 edev->component[i].scratch = scomp + i;
765 ses_enclosure_data_process(edev, sdev, 1);
767 /* see if there are any devices matching before
768 * we found the enclosure */
769 shost_for_each_device(tmp_sdev, sdev->host) {
770 if (tmp_sdev->lun != 0 || scsi_device_enclosure(tmp_sdev))
772 ses_match_to_enclosure(edev, tmp_sdev, 0);
778 sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n",
784 kfree(ses_dev->page10);
785 kfree(ses_dev->page2);
786 kfree(ses_dev->page1);
790 sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err);
794 static int ses_remove(struct device *dev)
799 static void ses_intf_remove_component(struct scsi_device *sdev)
801 struct enclosure_device *edev, *prev = NULL;
803 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
805 if (!enclosure_remove_device(edev, &sdev->sdev_gendev))
809 put_device(&edev->edev);
812 static void ses_intf_remove_enclosure(struct scsi_device *sdev)
814 struct enclosure_device *edev;
815 struct ses_device *ses_dev;
817 /* exact match to this enclosure */
818 edev = enclosure_find(&sdev->sdev_gendev, NULL);
822 ses_dev = edev->scratch;
823 edev->scratch = NULL;
825 kfree(ses_dev->page10);
826 kfree(ses_dev->page1);
827 kfree(ses_dev->page2);
830 kfree(edev->component[0].scratch);
832 put_device(&edev->edev);
833 enclosure_unregister(edev);
836 static void ses_intf_remove(struct device *cdev,
837 struct class_interface *intf)
839 struct scsi_device *sdev = to_scsi_device(cdev->parent);
841 if (!scsi_device_enclosure(sdev))
842 ses_intf_remove_component(sdev);
844 ses_intf_remove_enclosure(sdev);
847 static struct class_interface ses_interface = {
848 .add_dev = ses_intf_add,
849 .remove_dev = ses_intf_remove,
852 static struct scsi_driver ses_template = {
855 .owner = THIS_MODULE,
857 .remove = ses_remove,
861 static int __init ses_init(void)
865 err = scsi_register_interface(&ses_interface);
869 err = scsi_register_driver(&ses_template.gendrv);
876 scsi_unregister_interface(&ses_interface);
880 static void __exit ses_exit(void)
882 scsi_unregister_driver(&ses_template.gendrv);
883 scsi_unregister_interface(&ses_interface);
886 module_init(ses_init);
887 module_exit(ses_exit);
889 MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE);
891 MODULE_AUTHOR("James Bottomley");
892 MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
893 MODULE_LICENSE("GPL v2");