2 * SCSI Enclosure Services
4 * Copyright (C) 2008 James Bottomley <James.Bottomley@HansenPartnership.com>
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;
106 unsigned int retries = SES_RETRIES;
107 struct scsi_sense_hdr sshdr;
110 ret = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, bufflen,
111 &sshdr, SES_TIMEOUT, 1, NULL);
112 } while (ret > 0 && --retries && scsi_sense_valid(&sshdr) &&
113 (sshdr.sense_key == NOT_READY ||
114 (sshdr.sense_key == UNIT_ATTENTION && sshdr.asc == 0x29)));
119 recv_page_code = ((unsigned char *)buf)[0];
121 if (likely(recv_page_code == page_code))
124 /* successful diagnostic but wrong page code. This happens to some
125 * USB devices, just print a message and pretend there was an error */
127 sdev_printk(KERN_ERR, sdev,
128 "Wrong diagnostic page; asked for %d got %u\n",
129 page_code, recv_page_code);
134 static int ses_send_diag(struct scsi_device *sdev, int page_code,
135 void *buf, int bufflen)
139 unsigned char cmd[] = {
141 0x10, /* Set PF bit */
147 struct scsi_sense_hdr sshdr;
148 unsigned int retries = SES_RETRIES;
151 result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, buf, bufflen,
152 &sshdr, SES_TIMEOUT, 1, NULL);
153 } while (result > 0 && --retries && scsi_sense_valid(&sshdr) &&
154 (sshdr.sense_key == NOT_READY ||
155 (sshdr.sense_key == UNIT_ATTENTION && sshdr.asc == 0x29)));
158 sdev_printk(KERN_ERR, sdev, "SEND DIAGNOSTIC result: %8x\n",
163 static int ses_set_page2_descriptor(struct enclosure_device *edev,
164 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 /* Clear everything */
174 memset(desc_ptr, 0, ses_dev->page2_len - 8);
175 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
176 for (j = 0; j < type_ptr[1]; j++) {
178 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
179 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
181 if (count++ == descriptor) {
182 memcpy(desc_ptr, desc, 4);
185 /* clear reserved, just in case */
191 return ses_send_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len);
194 static unsigned char *ses_get_page2_descriptor(struct enclosure_device *edev,
195 struct enclosure_component *ecomp)
197 int i, j, count = 0, descriptor = ecomp->number;
198 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
199 struct ses_device *ses_dev = edev->scratch;
200 unsigned char *type_ptr = ses_dev->page1_types;
201 unsigned char *desc_ptr = ses_dev->page2 + 8;
203 if (ses_recv_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len) < 0)
206 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
207 for (j = 0; j < type_ptr[1]; j++) {
209 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
210 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
212 if (count++ == descriptor)
219 /* For device slot and array device slot elements, byte 3 bit 6
220 * is "fault sensed" while byte 3 bit 5 is "fault reqstd". As this
221 * code stands these bits are shifted 4 positions right so in
222 * sysfs they will appear as bits 2 and 1 respectively. Strange. */
223 static void ses_get_fault(struct enclosure_device *edev,
224 struct enclosure_component *ecomp)
228 if (!ses_page2_supported(edev)) {
232 desc = ses_get_page2_descriptor(edev, ecomp);
234 ecomp->fault = (desc[3] & 0x60) >> 4;
237 static int ses_set_fault(struct enclosure_device *edev,
238 struct enclosure_component *ecomp,
239 enum enclosure_component_setting val)
241 unsigned char desc[4];
242 unsigned char *desc_ptr;
244 if (!ses_page2_supported(edev))
247 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
252 init_device_slot_control(desc, ecomp, desc_ptr);
255 case ENCLOSURE_SETTING_DISABLED:
258 case ENCLOSURE_SETTING_ENABLED:
262 /* SES doesn't do the SGPIO blink settings */
266 return ses_set_page2_descriptor(edev, ecomp, desc);
269 static void ses_get_status(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->status = (desc[0] & 0x0f);
283 static void ses_get_locate(struct enclosure_device *edev,
284 struct enclosure_component *ecomp)
288 if (!ses_page2_supported(edev)) {
292 desc = ses_get_page2_descriptor(edev, ecomp);
294 ecomp->locate = (desc[2] & 0x02) ? 1 : 0;
297 static int ses_set_locate(struct enclosure_device *edev,
298 struct enclosure_component *ecomp,
299 enum enclosure_component_setting val)
301 unsigned char desc[4];
302 unsigned char *desc_ptr;
304 if (!ses_page2_supported(edev))
307 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
312 init_device_slot_control(desc, ecomp, desc_ptr);
315 case ENCLOSURE_SETTING_DISABLED:
318 case ENCLOSURE_SETTING_ENABLED:
322 /* SES doesn't do the SGPIO blink settings */
325 return ses_set_page2_descriptor(edev, ecomp, desc);
328 static int ses_set_active(struct enclosure_device *edev,
329 struct enclosure_component *ecomp,
330 enum enclosure_component_setting val)
332 unsigned char desc[4];
333 unsigned char *desc_ptr;
335 if (!ses_page2_supported(edev))
338 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
343 init_device_slot_control(desc, ecomp, desc_ptr);
346 case ENCLOSURE_SETTING_DISABLED:
350 case ENCLOSURE_SETTING_ENABLED:
355 /* SES doesn't do the SGPIO blink settings */
358 return ses_set_page2_descriptor(edev, ecomp, desc);
361 static int ses_show_id(struct enclosure_device *edev, char *buf)
363 struct ses_device *ses_dev = edev->scratch;
364 unsigned long long id = get_unaligned_be64(ses_dev->page1+8+4);
366 return sprintf(buf, "%#llx\n", id);
369 static void ses_get_power_status(struct enclosure_device *edev,
370 struct enclosure_component *ecomp)
374 if (!ses_page2_supported(edev)) {
375 ecomp->power_status = 0;
379 desc = ses_get_page2_descriptor(edev, ecomp);
381 ecomp->power_status = (desc[3] & 0x10) ? 0 : 1;
384 static int ses_set_power_status(struct enclosure_device *edev,
385 struct enclosure_component *ecomp,
388 unsigned char desc[4];
389 unsigned char *desc_ptr;
391 if (!ses_page2_supported(edev))
394 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
399 init_device_slot_control(desc, ecomp, desc_ptr);
402 /* power = 1 is device_off = 0 and vice versa */
412 ecomp->power_status = val;
413 return ses_set_page2_descriptor(edev, ecomp, desc);
416 static struct enclosure_component_callbacks ses_enclosure_callbacks = {
417 .get_fault = ses_get_fault,
418 .set_fault = ses_set_fault,
419 .get_status = ses_get_status,
420 .get_locate = ses_get_locate,
421 .set_locate = ses_set_locate,
422 .get_power_status = ses_get_power_status,
423 .set_power_status = ses_set_power_status,
424 .set_active = ses_set_active,
425 .show_id = ses_show_id,
428 struct ses_host_edev {
429 struct Scsi_Host *shost;
430 struct enclosure_device *edev;
434 int ses_match_host(struct enclosure_device *edev, void *data)
436 struct ses_host_edev *sed = data;
437 struct scsi_device *sdev;
439 if (!scsi_is_sdev_device(edev->edev.parent))
442 sdev = to_scsi_device(edev->edev.parent);
444 if (sdev->host != sed->shost)
452 static void ses_process_descriptor(struct enclosure_component *ecomp,
455 int eip = desc[0] & 0x10;
456 int invalid = desc[0] & 0x80;
457 enum scsi_protocol proto = desc[0] & 0x0f;
460 struct ses_component *scomp = ecomp->scratch;
467 case SCSI_PROTOCOL_FCP:
473 case SCSI_PROTOCOL_SAS:
480 /* only take the phy0 addr */
481 addr = (u64)d[12] << 56 |
491 /* FIXME: Need to add more protocols than just SAS */
503 static int ses_enclosure_find_by_addr(struct enclosure_device *edev,
506 struct efd *efd = data;
508 struct ses_component *scomp;
510 if (!edev->component[0].scratch)
513 for (i = 0; i < edev->components; i++) {
514 scomp = edev->component[i].scratch;
515 if (scomp->addr != efd->addr)
518 if (enclosure_add_device(edev, i, efd->dev) == 0)
519 kobject_uevent(&efd->dev->kobj, KOBJ_CHANGE);
525 #define INIT_ALLOC_SIZE 32
527 static void ses_enclosure_data_process(struct enclosure_device *edev,
528 struct scsi_device *sdev,
532 unsigned char *buf = NULL, *type_ptr, *desc_ptr, *addl_desc_ptr = NULL;
533 int i, j, page7_len, len, components;
534 struct ses_device *ses_dev = edev->scratch;
535 int types = ses_dev->page1_num_types;
536 unsigned char *hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
539 goto simple_populate;
541 /* re-read page 10 */
543 ses_recv_diag(sdev, 10, ses_dev->page10, ses_dev->page10_len);
544 /* Page 7 for the descriptors is optional */
545 result = ses_recv_diag(sdev, 7, hdr_buf, INIT_ALLOC_SIZE);
547 goto simple_populate;
549 page7_len = len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
550 /* add 1 for trailing '\0' we'll use */
551 buf = kzalloc(len + 1, GFP_KERNEL);
553 goto simple_populate;
554 result = ses_recv_diag(sdev, 7, buf, len);
564 len = (desc_ptr[2] << 8) + desc_ptr[3];
565 /* skip past overall descriptor */
569 addl_desc_ptr = ses_dev->page10 + 8;
570 type_ptr = ses_dev->page1_types;
572 for (i = 0; i < types; i++, type_ptr += 4) {
573 for (j = 0; j < type_ptr[1]; j++) {
575 struct enclosure_component *ecomp;
578 if (desc_ptr >= buf + page7_len) {
581 len = (desc_ptr[2] << 8) + desc_ptr[3];
583 /* Add trailing zero - pushes into
585 desc_ptr[len] = '\0';
589 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
590 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) {
593 ecomp = enclosure_component_alloc(
599 ecomp = &edev->component[components++];
601 if (!IS_ERR(ecomp)) {
603 ses_process_descriptor(
607 enclosure_component_register(
615 /* only find additional descriptions for specific devices */
616 (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
617 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE ||
618 type_ptr[0] == ENCLOSURE_COMPONENT_SAS_EXPANDER ||
619 /* these elements are optional */
620 type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_TARGET_PORT ||
621 type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_INITIATOR_PORT ||
622 type_ptr[0] == ENCLOSURE_COMPONENT_CONTROLLER_ELECTRONICS))
623 addl_desc_ptr += addl_desc_ptr[1] + 2;
631 static void ses_match_to_enclosure(struct enclosure_device *edev,
632 struct scsi_device *sdev,
635 struct scsi_device *edev_sdev = to_scsi_device(edev->edev.parent);
641 ses_enclosure_data_process(edev, edev_sdev, 0);
643 if (scsi_is_sas_rphy(sdev->sdev_target->dev.parent))
644 efd.addr = sas_get_address(sdev);
647 efd.dev = &sdev->sdev_gendev;
649 enclosure_for_each_device(ses_enclosure_find_by_addr, &efd);
653 static int ses_intf_add(struct device *cdev,
654 struct class_interface *intf)
656 struct scsi_device *sdev = to_scsi_device(cdev->parent);
657 struct scsi_device *tmp_sdev;
658 unsigned char *buf = NULL, *hdr_buf, *type_ptr, page;
659 struct ses_device *ses_dev;
661 int i, types, len, components = 0;
664 struct enclosure_device *edev;
665 struct ses_component *scomp = NULL;
667 if (!scsi_device_enclosure(sdev)) {
668 /* not an enclosure, but might be in one */
669 struct enclosure_device *prev = NULL;
671 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
672 ses_match_to_enclosure(edev, sdev, 1);
678 /* TYPE_ENCLOSURE prints a message in probe */
679 if (sdev->type != TYPE_ENCLOSURE)
680 sdev_printk(KERN_NOTICE, sdev, "Embedded Enclosure Device\n");
682 ses_dev = kzalloc(sizeof(*ses_dev), GFP_KERNEL);
683 hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
684 if (!hdr_buf || !ses_dev)
688 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
692 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
693 buf = kzalloc(len, GFP_KERNEL);
697 result = ses_recv_diag(sdev, page, buf, len);
703 /* we always have one main enclosure and the rest are referred
704 * to as secondary subenclosures */
705 num_enclosures = buf[1] + 1;
707 /* begin at the enclosure descriptor */
709 /* skip all the enclosure descriptors */
710 for (i = 0; i < num_enclosures && type_ptr < buf + len; i++) {
711 types += type_ptr[2];
712 type_ptr += type_ptr[3] + 4;
715 ses_dev->page1_types = type_ptr;
716 ses_dev->page1_num_types = types;
718 for (i = 0; i < types && type_ptr < buf + len; i++, type_ptr += 4) {
719 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
720 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE)
721 components += type_ptr[1];
723 ses_dev->page1 = buf;
724 ses_dev->page1_len = len;
728 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
730 goto page2_not_supported;
732 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
733 buf = kzalloc(len, GFP_KERNEL);
737 /* make sure getting page 2 actually works */
738 result = ses_recv_diag(sdev, 2, buf, len);
741 ses_dev->page2 = buf;
742 ses_dev->page2_len = len;
745 /* The additional information page --- allows us
746 * to match up the devices */
748 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
751 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
752 buf = kzalloc(len, GFP_KERNEL);
756 result = ses_recv_diag(sdev, page, buf, len);
759 ses_dev->page10 = buf;
760 ses_dev->page10_len = len;
764 scomp = kcalloc(components, sizeof(struct ses_component), GFP_KERNEL);
768 edev = enclosure_register(cdev->parent, dev_name(&sdev->sdev_gendev),
769 components, &ses_enclosure_callbacks);
777 edev->scratch = ses_dev;
778 for (i = 0; i < components; i++)
779 edev->component[i].scratch = scomp + i;
781 ses_enclosure_data_process(edev, sdev, 1);
783 /* see if there are any devices matching before
784 * we found the enclosure */
785 shost_for_each_device(tmp_sdev, sdev->host) {
786 if (tmp_sdev->lun != 0 || scsi_device_enclosure(tmp_sdev))
788 ses_match_to_enclosure(edev, tmp_sdev, 0);
794 sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n",
800 kfree(ses_dev->page10);
801 kfree(ses_dev->page2);
802 kfree(ses_dev->page1);
806 sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err);
810 static int ses_remove(struct device *dev)
815 static void ses_intf_remove_component(struct scsi_device *sdev)
817 struct enclosure_device *edev, *prev = NULL;
819 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
821 if (!enclosure_remove_device(edev, &sdev->sdev_gendev))
825 put_device(&edev->edev);
828 static void ses_intf_remove_enclosure(struct scsi_device *sdev)
830 struct enclosure_device *edev;
831 struct ses_device *ses_dev;
833 /* exact match to this enclosure */
834 edev = enclosure_find(&sdev->sdev_gendev, NULL);
838 ses_dev = edev->scratch;
839 edev->scratch = NULL;
841 kfree(ses_dev->page10);
842 kfree(ses_dev->page1);
843 kfree(ses_dev->page2);
846 kfree(edev->component[0].scratch);
848 put_device(&edev->edev);
849 enclosure_unregister(edev);
852 static void ses_intf_remove(struct device *cdev,
853 struct class_interface *intf)
855 struct scsi_device *sdev = to_scsi_device(cdev->parent);
857 if (!scsi_device_enclosure(sdev))
858 ses_intf_remove_component(sdev);
860 ses_intf_remove_enclosure(sdev);
863 static struct class_interface ses_interface = {
864 .add_dev = ses_intf_add,
865 .remove_dev = ses_intf_remove,
868 static struct scsi_driver ses_template = {
871 .owner = THIS_MODULE,
873 .remove = ses_remove,
877 static int __init ses_init(void)
881 err = scsi_register_interface(&ses_interface);
885 err = scsi_register_driver(&ses_template.gendrv);
892 scsi_unregister_interface(&ses_interface);
896 static void __exit ses_exit(void)
898 scsi_unregister_driver(&ses_template.gendrv);
899 scsi_unregister_interface(&ses_interface);
902 module_init(ses_init);
903 module_exit(ses_exit);
905 MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE);
907 MODULE_AUTHOR("James Bottomley");
908 MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
909 MODULE_LICENSE("GPL v2");