2 * SCSI Enclosure Services
6 **-----------------------------------------------------------------------------
8 ** This program is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License
10 ** version 2 as published by the Free Software Foundation.
12 ** This program is distributed in the hope that it will be useful,
13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ** GNU General Public License for more details.
17 ** You should have received a copy of the GNU General Public License
18 ** along with this program; if not, write to the Free Software
19 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 **-----------------------------------------------------------------------------
24 #include <linux/slab.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/enclosure.h>
28 #include <asm/unaligned.h>
30 #include <scsi/scsi.h>
31 #include <scsi/scsi_cmnd.h>
32 #include <scsi/scsi_dbg.h>
33 #include <scsi/scsi_device.h>
34 #include <scsi/scsi_driver.h>
35 #include <scsi/scsi_host.h>
37 #include <scsi/scsi_transport_sas.h>
41 unsigned char *page1_types;
43 unsigned char *page10;
45 short page1_num_types;
50 struct ses_component {
54 static bool ses_page2_supported(struct enclosure_device *edev)
56 struct ses_device *ses_dev = edev->scratch;
58 return (ses_dev->page2 != NULL);
61 static int ses_probe(struct device *dev)
63 struct scsi_device *sdev = to_scsi_device(dev);
66 if (sdev->type != TYPE_ENCLOSURE)
70 sdev_printk(KERN_NOTICE, sdev, "Attached Enclosure device\n");
76 #define SES_TIMEOUT (30 * HZ)
79 static void init_device_slot_control(unsigned char *dest_desc,
80 struct enclosure_component *ecomp,
81 unsigned char *status)
83 memcpy(dest_desc, status, 4);
85 /* only clear byte 1 for ENCLOSURE_COMPONENT_DEVICE */
86 if (ecomp->type == ENCLOSURE_COMPONENT_DEVICE)
93 static int ses_recv_diag(struct scsi_device *sdev, int page_code,
94 void *buf, int bufflen)
97 unsigned char cmd[] = {
105 unsigned char recv_page_code;
107 ret = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, bufflen,
108 NULL, SES_TIMEOUT, SES_RETRIES, NULL);
112 recv_page_code = ((unsigned char *)buf)[0];
114 if (likely(recv_page_code == page_code))
117 /* successful diagnostic but wrong page code. This happens to some
118 * USB devices, just print a message and pretend there was an error */
120 sdev_printk(KERN_ERR, sdev,
121 "Wrong diagnostic page; asked for %d got %u\n",
122 page_code, recv_page_code);
127 static int ses_send_diag(struct scsi_device *sdev, int page_code,
128 void *buf, int bufflen)
132 unsigned char cmd[] = {
134 0x10, /* Set PF bit */
141 result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, buf, bufflen,
142 NULL, SES_TIMEOUT, SES_RETRIES, NULL);
144 sdev_printk(KERN_ERR, sdev, "SEND DIAGNOSTIC result: %8x\n",
149 static int ses_set_page2_descriptor(struct enclosure_device *edev,
150 struct enclosure_component *ecomp,
153 int i, j, count = 0, descriptor = ecomp->number;
154 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
155 struct ses_device *ses_dev = edev->scratch;
156 unsigned char *type_ptr = ses_dev->page1_types;
157 unsigned char *desc_ptr = ses_dev->page2 + 8;
159 /* Clear everything */
160 memset(desc_ptr, 0, ses_dev->page2_len - 8);
161 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
162 for (j = 0; j < type_ptr[1]; j++) {
164 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
165 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
167 if (count++ == descriptor) {
168 memcpy(desc_ptr, desc, 4);
171 /* clear reserved, just in case */
177 return ses_send_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len);
180 static unsigned char *ses_get_page2_descriptor(struct enclosure_device *edev,
181 struct enclosure_component *ecomp)
183 int i, j, count = 0, descriptor = ecomp->number;
184 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
185 struct ses_device *ses_dev = edev->scratch;
186 unsigned char *type_ptr = ses_dev->page1_types;
187 unsigned char *desc_ptr = ses_dev->page2 + 8;
189 if (ses_recv_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len) < 0)
192 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
193 for (j = 0; j < type_ptr[1]; j++) {
195 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
196 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
198 if (count++ == descriptor)
205 /* For device slot and array device slot elements, byte 3 bit 6
206 * is "fault sensed" while byte 3 bit 5 is "fault reqstd". As this
207 * code stands these bits are shifted 4 positions right so in
208 * sysfs they will appear as bits 2 and 1 respectively. Strange. */
209 static void ses_get_fault(struct enclosure_device *edev,
210 struct enclosure_component *ecomp)
214 if (!ses_page2_supported(edev)) {
218 desc = ses_get_page2_descriptor(edev, ecomp);
220 ecomp->fault = (desc[3] & 0x60) >> 4;
223 static int ses_set_fault(struct enclosure_device *edev,
224 struct enclosure_component *ecomp,
225 enum enclosure_component_setting val)
227 unsigned char desc[4];
228 unsigned char *desc_ptr;
230 if (!ses_page2_supported(edev))
233 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
238 init_device_slot_control(desc, ecomp, desc_ptr);
241 case ENCLOSURE_SETTING_DISABLED:
244 case ENCLOSURE_SETTING_ENABLED:
248 /* SES doesn't do the SGPIO blink settings */
252 return ses_set_page2_descriptor(edev, ecomp, desc);
255 static void ses_get_status(struct enclosure_device *edev,
256 struct enclosure_component *ecomp)
260 if (!ses_page2_supported(edev)) {
264 desc = ses_get_page2_descriptor(edev, ecomp);
266 ecomp->status = (desc[0] & 0x0f);
269 static void ses_get_locate(struct enclosure_device *edev,
270 struct enclosure_component *ecomp)
274 if (!ses_page2_supported(edev)) {
278 desc = ses_get_page2_descriptor(edev, ecomp);
280 ecomp->locate = (desc[2] & 0x02) ? 1 : 0;
283 static int ses_set_locate(struct enclosure_device *edev,
284 struct enclosure_component *ecomp,
285 enum enclosure_component_setting val)
287 unsigned char desc[4];
288 unsigned char *desc_ptr;
290 if (!ses_page2_supported(edev))
293 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
298 init_device_slot_control(desc, ecomp, desc_ptr);
301 case ENCLOSURE_SETTING_DISABLED:
304 case ENCLOSURE_SETTING_ENABLED:
308 /* SES doesn't do the SGPIO blink settings */
311 return ses_set_page2_descriptor(edev, ecomp, desc);
314 static int ses_set_active(struct enclosure_device *edev,
315 struct enclosure_component *ecomp,
316 enum enclosure_component_setting val)
318 unsigned char desc[4];
319 unsigned char *desc_ptr;
321 if (!ses_page2_supported(edev))
324 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
329 init_device_slot_control(desc, ecomp, desc_ptr);
332 case ENCLOSURE_SETTING_DISABLED:
336 case ENCLOSURE_SETTING_ENABLED:
341 /* SES doesn't do the SGPIO blink settings */
344 return ses_set_page2_descriptor(edev, ecomp, desc);
347 static int ses_show_id(struct enclosure_device *edev, char *buf)
349 struct ses_device *ses_dev = edev->scratch;
350 unsigned long long id = get_unaligned_be64(ses_dev->page1+8+4);
352 return sprintf(buf, "%#llx\n", id);
355 static void ses_get_power_status(struct enclosure_device *edev,
356 struct enclosure_component *ecomp)
360 if (!ses_page2_supported(edev)) {
361 ecomp->power_status = 0;
365 desc = ses_get_page2_descriptor(edev, ecomp);
367 ecomp->power_status = (desc[3] & 0x10) ? 0 : 1;
370 static int ses_set_power_status(struct enclosure_device *edev,
371 struct enclosure_component *ecomp,
374 unsigned char desc[4];
375 unsigned char *desc_ptr;
377 if (!ses_page2_supported(edev))
380 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
385 init_device_slot_control(desc, ecomp, desc_ptr);
388 /* power = 1 is device_off = 0 and vice versa */
398 ecomp->power_status = val;
399 return ses_set_page2_descriptor(edev, ecomp, desc);
402 static struct enclosure_component_callbacks ses_enclosure_callbacks = {
403 .get_fault = ses_get_fault,
404 .set_fault = ses_set_fault,
405 .get_status = ses_get_status,
406 .get_locate = ses_get_locate,
407 .set_locate = ses_set_locate,
408 .get_power_status = ses_get_power_status,
409 .set_power_status = ses_set_power_status,
410 .set_active = ses_set_active,
411 .show_id = ses_show_id,
414 struct ses_host_edev {
415 struct Scsi_Host *shost;
416 struct enclosure_device *edev;
420 int ses_match_host(struct enclosure_device *edev, void *data)
422 struct ses_host_edev *sed = data;
423 struct scsi_device *sdev;
425 if (!scsi_is_sdev_device(edev->edev.parent))
428 sdev = to_scsi_device(edev->edev.parent);
430 if (sdev->host != sed->shost)
438 static void ses_process_descriptor(struct enclosure_component *ecomp,
441 int eip = desc[0] & 0x10;
442 int invalid = desc[0] & 0x80;
443 enum scsi_protocol proto = desc[0] & 0x0f;
446 struct ses_component *scomp = ecomp->scratch;
453 case SCSI_PROTOCOL_FCP:
459 case SCSI_PROTOCOL_SAS:
466 /* only take the phy0 addr */
467 addr = (u64)d[12] << 56 |
477 /* FIXME: Need to add more protocols than just SAS */
489 static int ses_enclosure_find_by_addr(struct enclosure_device *edev,
492 struct efd *efd = data;
494 struct ses_component *scomp;
496 if (!edev->component[0].scratch)
499 for (i = 0; i < edev->components; i++) {
500 scomp = edev->component[i].scratch;
501 if (scomp->addr != efd->addr)
504 if (enclosure_add_device(edev, i, efd->dev) == 0)
505 kobject_uevent(&efd->dev->kobj, KOBJ_CHANGE);
511 #define INIT_ALLOC_SIZE 32
513 static void ses_enclosure_data_process(struct enclosure_device *edev,
514 struct scsi_device *sdev,
518 unsigned char *buf = NULL, *type_ptr, *desc_ptr, *addl_desc_ptr = NULL;
519 int i, j, page7_len, len, components;
520 struct ses_device *ses_dev = edev->scratch;
521 int types = ses_dev->page1_num_types;
522 unsigned char *hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
525 goto simple_populate;
527 /* re-read page 10 */
529 ses_recv_diag(sdev, 10, ses_dev->page10, ses_dev->page10_len);
530 /* Page 7 for the descriptors is optional */
531 result = ses_recv_diag(sdev, 7, hdr_buf, INIT_ALLOC_SIZE);
533 goto simple_populate;
535 page7_len = len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
536 /* add 1 for trailing '\0' we'll use */
537 buf = kzalloc(len + 1, GFP_KERNEL);
539 goto simple_populate;
540 result = ses_recv_diag(sdev, 7, buf, len);
550 len = (desc_ptr[2] << 8) + desc_ptr[3];
551 /* skip past overall descriptor */
555 addl_desc_ptr = ses_dev->page10 + 8;
556 type_ptr = ses_dev->page1_types;
558 for (i = 0; i < types; i++, type_ptr += 4) {
559 for (j = 0; j < type_ptr[1]; j++) {
561 struct enclosure_component *ecomp;
564 if (desc_ptr >= buf + page7_len) {
567 len = (desc_ptr[2] << 8) + desc_ptr[3];
569 /* Add trailing zero - pushes into
571 desc_ptr[len] = '\0';
575 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
576 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) {
579 ecomp = enclosure_component_alloc(
585 ecomp = &edev->component[components++];
587 if (!IS_ERR(ecomp)) {
589 ses_process_descriptor(
593 enclosure_component_register(
601 /* only find additional descriptions for specific devices */
602 (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
603 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE ||
604 type_ptr[0] == ENCLOSURE_COMPONENT_SAS_EXPANDER ||
605 /* these elements are optional */
606 type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_TARGET_PORT ||
607 type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_INITIATOR_PORT ||
608 type_ptr[0] == ENCLOSURE_COMPONENT_CONTROLLER_ELECTRONICS))
609 addl_desc_ptr += addl_desc_ptr[1] + 2;
617 static void ses_match_to_enclosure(struct enclosure_device *edev,
618 struct scsi_device *sdev)
624 ses_enclosure_data_process(edev, to_scsi_device(edev->edev.parent), 0);
626 if (scsi_is_sas_rphy(sdev->sdev_target->dev.parent))
627 efd.addr = sas_get_address(sdev);
630 efd.dev = &sdev->sdev_gendev;
632 enclosure_for_each_device(ses_enclosure_find_by_addr, &efd);
636 static int ses_intf_add(struct device *cdev,
637 struct class_interface *intf)
639 struct scsi_device *sdev = to_scsi_device(cdev->parent);
640 struct scsi_device *tmp_sdev;
641 unsigned char *buf = NULL, *hdr_buf, *type_ptr, page;
642 struct ses_device *ses_dev;
644 int i, types, len, components = 0;
647 struct enclosure_device *edev;
648 struct ses_component *scomp = NULL;
650 if (!scsi_device_enclosure(sdev)) {
651 /* not an enclosure, but might be in one */
652 struct enclosure_device *prev = NULL;
654 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
655 ses_match_to_enclosure(edev, sdev);
661 /* TYPE_ENCLOSURE prints a message in probe */
662 if (sdev->type != TYPE_ENCLOSURE)
663 sdev_printk(KERN_NOTICE, sdev, "Embedded Enclosure Device\n");
665 ses_dev = kzalloc(sizeof(*ses_dev), GFP_KERNEL);
666 hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
667 if (!hdr_buf || !ses_dev)
671 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
675 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
676 buf = kzalloc(len, GFP_KERNEL);
680 result = ses_recv_diag(sdev, page, buf, len);
686 /* we always have one main enclosure and the rest are referred
687 * to as secondary subenclosures */
688 num_enclosures = buf[1] + 1;
690 /* begin at the enclosure descriptor */
692 /* skip all the enclosure descriptors */
693 for (i = 0; i < num_enclosures && type_ptr < buf + len; i++) {
694 types += type_ptr[2];
695 type_ptr += type_ptr[3] + 4;
698 ses_dev->page1_types = type_ptr;
699 ses_dev->page1_num_types = types;
701 for (i = 0; i < types && type_ptr < buf + len; i++, type_ptr += 4) {
702 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
703 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE)
704 components += type_ptr[1];
706 ses_dev->page1 = buf;
707 ses_dev->page1_len = len;
711 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
713 goto page2_not_supported;
715 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
716 buf = kzalloc(len, GFP_KERNEL);
720 /* make sure getting page 2 actually works */
721 result = ses_recv_diag(sdev, 2, buf, len);
724 ses_dev->page2 = buf;
725 ses_dev->page2_len = len;
728 /* The additional information page --- allows us
729 * to match up the devices */
731 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
734 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
735 buf = kzalloc(len, GFP_KERNEL);
739 result = ses_recv_diag(sdev, page, buf, len);
742 ses_dev->page10 = buf;
743 ses_dev->page10_len = len;
747 scomp = kzalloc(sizeof(struct ses_component) * components, GFP_KERNEL);
751 edev = enclosure_register(cdev->parent, dev_name(&sdev->sdev_gendev),
752 components, &ses_enclosure_callbacks);
760 edev->scratch = ses_dev;
761 for (i = 0; i < components; i++)
762 edev->component[i].scratch = scomp + i;
764 ses_enclosure_data_process(edev, sdev, 1);
766 /* see if there are any devices matching before
767 * we found the enclosure */
768 shost_for_each_device(tmp_sdev, sdev->host) {
769 if (tmp_sdev->lun != 0 || scsi_device_enclosure(tmp_sdev))
771 ses_match_to_enclosure(edev, tmp_sdev);
777 sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n",
783 kfree(ses_dev->page10);
784 kfree(ses_dev->page2);
785 kfree(ses_dev->page1);
789 sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err);
793 static int ses_remove(struct device *dev)
798 static void ses_intf_remove_component(struct scsi_device *sdev)
800 struct enclosure_device *edev, *prev = NULL;
802 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
804 if (!enclosure_remove_device(edev, &sdev->sdev_gendev))
808 put_device(&edev->edev);
811 static void ses_intf_remove_enclosure(struct scsi_device *sdev)
813 struct enclosure_device *edev;
814 struct ses_device *ses_dev;
816 /* exact match to this enclosure */
817 edev = enclosure_find(&sdev->sdev_gendev, NULL);
821 ses_dev = edev->scratch;
822 edev->scratch = NULL;
824 kfree(ses_dev->page10);
825 kfree(ses_dev->page1);
826 kfree(ses_dev->page2);
829 kfree(edev->component[0].scratch);
831 put_device(&edev->edev);
832 enclosure_unregister(edev);
835 static void ses_intf_remove(struct device *cdev,
836 struct class_interface *intf)
838 struct scsi_device *sdev = to_scsi_device(cdev->parent);
840 if (!scsi_device_enclosure(sdev))
841 ses_intf_remove_component(sdev);
843 ses_intf_remove_enclosure(sdev);
846 static struct class_interface ses_interface = {
847 .add_dev = ses_intf_add,
848 .remove_dev = ses_intf_remove,
851 static struct scsi_driver ses_template = {
854 .owner = THIS_MODULE,
856 .remove = ses_remove,
860 static int __init ses_init(void)
864 err = scsi_register_interface(&ses_interface);
868 err = scsi_register_driver(&ses_template.gendrv);
875 scsi_unregister_interface(&ses_interface);
879 static void __exit ses_exit(void)
881 scsi_unregister_driver(&ses_template.gendrv);
882 scsi_unregister_interface(&ses_interface);
885 module_init(ses_init);
886 module_exit(ses_exit);
888 MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE);
890 MODULE_AUTHOR("James Bottomley");
891 MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
892 MODULE_LICENSE("GPL v2");