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 int ses_probe(struct device *dev)
56 struct scsi_device *sdev = to_scsi_device(dev);
59 if (sdev->type != TYPE_ENCLOSURE)
63 sdev_printk(KERN_NOTICE, sdev, "Attached Enclosure device\n");
69 #define SES_TIMEOUT (30 * HZ)
72 static void init_device_slot_control(unsigned char *dest_desc,
73 struct enclosure_component *ecomp,
74 unsigned char *status)
76 memcpy(dest_desc, status, 4);
78 /* only clear byte 1 for ENCLOSURE_COMPONENT_DEVICE */
79 if (ecomp->type == ENCLOSURE_COMPONENT_DEVICE)
86 static int ses_recv_diag(struct scsi_device *sdev, int page_code,
87 void *buf, int bufflen)
90 unsigned char cmd[] = {
98 unsigned char recv_page_code;
100 ret = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, bufflen,
101 NULL, SES_TIMEOUT, SES_RETRIES, NULL);
105 recv_page_code = ((unsigned char *)buf)[0];
107 if (likely(recv_page_code == page_code))
110 /* successful diagnostic but wrong page code. This happens to some
111 * USB devices, just print a message and pretend there was an error */
113 sdev_printk(KERN_ERR, sdev,
114 "Wrong diagnostic page; asked for %d got %u\n",
115 page_code, recv_page_code);
120 static int ses_send_diag(struct scsi_device *sdev, int page_code,
121 void *buf, int bufflen)
125 unsigned char cmd[] = {
127 0x10, /* Set PF bit */
134 result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, buf, bufflen,
135 NULL, SES_TIMEOUT, SES_RETRIES, NULL);
137 sdev_printk(KERN_ERR, sdev, "SEND DIAGNOSTIC result: %8x\n",
142 static int ses_set_page2_descriptor(struct enclosure_device *edev,
143 struct enclosure_component *ecomp,
146 int i, j, count = 0, descriptor = ecomp->number;
147 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
148 struct ses_device *ses_dev = edev->scratch;
149 unsigned char *type_ptr = ses_dev->page1_types;
150 unsigned char *desc_ptr = ses_dev->page2 + 8;
152 /* Clear everything */
153 memset(desc_ptr, 0, ses_dev->page2_len - 8);
154 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
155 for (j = 0; j < type_ptr[1]; j++) {
157 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
158 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
160 if (count++ == descriptor) {
161 memcpy(desc_ptr, desc, 4);
164 /* clear reserved, just in case */
170 return ses_send_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len);
173 static unsigned char *ses_get_page2_descriptor(struct enclosure_device *edev,
174 struct enclosure_component *ecomp)
176 int i, j, count = 0, descriptor = ecomp->number;
177 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
178 struct ses_device *ses_dev = edev->scratch;
179 unsigned char *type_ptr = ses_dev->page1_types;
180 unsigned char *desc_ptr = ses_dev->page2 + 8;
182 ses_recv_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len);
184 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
185 for (j = 0; j < type_ptr[1]; j++) {
187 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
188 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
190 if (count++ == descriptor)
197 /* For device slot and array device slot elements, byte 3 bit 6
198 * is "fault sensed" while byte 3 bit 5 is "fault reqstd". As this
199 * code stands these bits are shifted 4 positions right so in
200 * sysfs they will appear as bits 2 and 1 respectively. Strange. */
201 static void ses_get_fault(struct enclosure_device *edev,
202 struct enclosure_component *ecomp)
206 desc = ses_get_page2_descriptor(edev, ecomp);
208 ecomp->fault = (desc[3] & 0x60) >> 4;
211 static int ses_set_fault(struct enclosure_device *edev,
212 struct enclosure_component *ecomp,
213 enum enclosure_component_setting val)
215 unsigned char desc[4];
216 unsigned char *desc_ptr;
218 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
223 init_device_slot_control(desc, ecomp, desc_ptr);
226 case ENCLOSURE_SETTING_DISABLED:
229 case ENCLOSURE_SETTING_ENABLED:
233 /* SES doesn't do the SGPIO blink settings */
237 return ses_set_page2_descriptor(edev, ecomp, desc);
240 static void ses_get_status(struct enclosure_device *edev,
241 struct enclosure_component *ecomp)
245 desc = ses_get_page2_descriptor(edev, ecomp);
247 ecomp->status = (desc[0] & 0x0f);
250 static void ses_get_locate(struct enclosure_device *edev,
251 struct enclosure_component *ecomp)
255 desc = ses_get_page2_descriptor(edev, ecomp);
257 ecomp->locate = (desc[2] & 0x02) ? 1 : 0;
260 static int ses_set_locate(struct enclosure_device *edev,
261 struct enclosure_component *ecomp,
262 enum enclosure_component_setting val)
264 unsigned char desc[4];
265 unsigned char *desc_ptr;
267 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
272 init_device_slot_control(desc, ecomp, desc_ptr);
275 case ENCLOSURE_SETTING_DISABLED:
278 case ENCLOSURE_SETTING_ENABLED:
282 /* SES doesn't do the SGPIO blink settings */
285 return ses_set_page2_descriptor(edev, ecomp, desc);
288 static int ses_set_active(struct enclosure_device *edev,
289 struct enclosure_component *ecomp,
290 enum enclosure_component_setting val)
292 unsigned char desc[4];
293 unsigned char *desc_ptr;
295 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
300 init_device_slot_control(desc, ecomp, desc_ptr);
303 case ENCLOSURE_SETTING_DISABLED:
307 case ENCLOSURE_SETTING_ENABLED:
312 /* SES doesn't do the SGPIO blink settings */
315 return ses_set_page2_descriptor(edev, ecomp, desc);
318 static int ses_show_id(struct enclosure_device *edev, char *buf)
320 struct ses_device *ses_dev = edev->scratch;
321 unsigned long long id = get_unaligned_be64(ses_dev->page1+8+4);
323 return sprintf(buf, "%#llx\n", id);
326 static void ses_get_power_status(struct enclosure_device *edev,
327 struct enclosure_component *ecomp)
331 desc = ses_get_page2_descriptor(edev, ecomp);
333 ecomp->power_status = (desc[3] & 0x10) ? 0 : 1;
336 static int ses_set_power_status(struct enclosure_device *edev,
337 struct enclosure_component *ecomp,
340 unsigned char desc[4];
341 unsigned char *desc_ptr;
343 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
348 init_device_slot_control(desc, ecomp, desc_ptr);
351 /* power = 1 is device_off = 0 and vice versa */
361 ecomp->power_status = val;
362 return ses_set_page2_descriptor(edev, ecomp, desc);
365 static struct enclosure_component_callbacks ses_enclosure_callbacks = {
366 .get_fault = ses_get_fault,
367 .set_fault = ses_set_fault,
368 .get_status = ses_get_status,
369 .get_locate = ses_get_locate,
370 .set_locate = ses_set_locate,
371 .get_power_status = ses_get_power_status,
372 .set_power_status = ses_set_power_status,
373 .set_active = ses_set_active,
374 .show_id = ses_show_id,
377 struct ses_host_edev {
378 struct Scsi_Host *shost;
379 struct enclosure_device *edev;
383 int ses_match_host(struct enclosure_device *edev, void *data)
385 struct ses_host_edev *sed = data;
386 struct scsi_device *sdev;
388 if (!scsi_is_sdev_device(edev->edev.parent))
391 sdev = to_scsi_device(edev->edev.parent);
393 if (sdev->host != sed->shost)
401 static void ses_process_descriptor(struct enclosure_component *ecomp,
404 int eip = desc[0] & 0x10;
405 int invalid = desc[0] & 0x80;
406 enum scsi_protocol proto = desc[0] & 0x0f;
409 struct ses_component *scomp = ecomp->scratch;
416 case SCSI_PROTOCOL_FCP:
422 case SCSI_PROTOCOL_SAS:
429 /* only take the phy0 addr */
430 addr = (u64)d[12] << 56 |
440 /* FIXME: Need to add more protocols than just SAS */
452 static int ses_enclosure_find_by_addr(struct enclosure_device *edev,
455 struct efd *efd = data;
457 struct ses_component *scomp;
459 if (!edev->component[0].scratch)
462 for (i = 0; i < edev->components; i++) {
463 scomp = edev->component[i].scratch;
464 if (scomp->addr != efd->addr)
467 if (enclosure_add_device(edev, i, efd->dev) == 0)
468 kobject_uevent(&efd->dev->kobj, KOBJ_CHANGE);
474 #define INIT_ALLOC_SIZE 32
476 static void ses_enclosure_data_process(struct enclosure_device *edev,
477 struct scsi_device *sdev,
481 unsigned char *buf = NULL, *type_ptr, *desc_ptr, *addl_desc_ptr = NULL;
482 int i, j, page7_len, len, components;
483 struct ses_device *ses_dev = edev->scratch;
484 int types = ses_dev->page1_num_types;
485 unsigned char *hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
488 goto simple_populate;
490 /* re-read page 10 */
492 ses_recv_diag(sdev, 10, ses_dev->page10, ses_dev->page10_len);
493 /* Page 7 for the descriptors is optional */
494 result = ses_recv_diag(sdev, 7, hdr_buf, INIT_ALLOC_SIZE);
496 goto simple_populate;
498 page7_len = len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
499 /* add 1 for trailing '\0' we'll use */
500 buf = kzalloc(len + 1, GFP_KERNEL);
502 goto simple_populate;
503 result = ses_recv_diag(sdev, 7, buf, len);
513 len = (desc_ptr[2] << 8) + desc_ptr[3];
514 /* skip past overall descriptor */
518 addl_desc_ptr = ses_dev->page10 + 8;
519 type_ptr = ses_dev->page1_types;
521 for (i = 0; i < types; i++, type_ptr += 4) {
522 for (j = 0; j < type_ptr[1]; j++) {
524 struct enclosure_component *ecomp;
527 if (desc_ptr >= buf + page7_len) {
530 len = (desc_ptr[2] << 8) + desc_ptr[3];
532 /* Add trailing zero - pushes into
534 desc_ptr[len] = '\0';
538 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
539 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) {
542 ecomp = enclosure_component_alloc(
548 ecomp = &edev->component[components++];
550 if (!IS_ERR(ecomp)) {
551 ses_get_power_status(edev, ecomp);
553 ses_process_descriptor(
557 enclosure_component_register(
565 /* only find additional descriptions for specific devices */
566 (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
567 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE ||
568 type_ptr[0] == ENCLOSURE_COMPONENT_SAS_EXPANDER ||
569 /* these elements are optional */
570 type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_TARGET_PORT ||
571 type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_INITIATOR_PORT ||
572 type_ptr[0] == ENCLOSURE_COMPONENT_CONTROLLER_ELECTRONICS))
573 addl_desc_ptr += addl_desc_ptr[1] + 2;
581 static void ses_match_to_enclosure(struct enclosure_device *edev,
582 struct scsi_device *sdev)
588 ses_enclosure_data_process(edev, to_scsi_device(edev->edev.parent), 0);
590 if (scsi_is_sas_rphy(&sdev->sdev_gendev))
591 efd.addr = sas_get_address(sdev);
594 efd.dev = &sdev->sdev_gendev;
596 enclosure_for_each_device(ses_enclosure_find_by_addr, &efd);
600 static int ses_intf_add(struct device *cdev,
601 struct class_interface *intf)
603 struct scsi_device *sdev = to_scsi_device(cdev->parent);
604 struct scsi_device *tmp_sdev;
605 unsigned char *buf = NULL, *hdr_buf, *type_ptr;
606 struct ses_device *ses_dev;
608 int i, types, len, components = 0;
611 struct enclosure_device *edev;
612 struct ses_component *scomp = NULL;
614 if (!scsi_device_enclosure(sdev)) {
615 /* not an enclosure, but might be in one */
616 struct enclosure_device *prev = NULL;
618 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
619 ses_match_to_enclosure(edev, sdev);
625 /* TYPE_ENCLOSURE prints a message in probe */
626 if (sdev->type != TYPE_ENCLOSURE)
627 sdev_printk(KERN_NOTICE, sdev, "Embedded Enclosure Device\n");
629 ses_dev = kzalloc(sizeof(*ses_dev), GFP_KERNEL);
630 hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
631 if (!hdr_buf || !ses_dev)
634 result = ses_recv_diag(sdev, 1, hdr_buf, INIT_ALLOC_SIZE);
638 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
639 buf = kzalloc(len, GFP_KERNEL);
643 result = ses_recv_diag(sdev, 1, buf, len);
649 /* we always have one main enclosure and the rest are referred
650 * to as secondary subenclosures */
651 num_enclosures = buf[1] + 1;
653 /* begin at the enclosure descriptor */
655 /* skip all the enclosure descriptors */
656 for (i = 0; i < num_enclosures && type_ptr < buf + len; i++) {
657 types += type_ptr[2];
658 type_ptr += type_ptr[3] + 4;
661 ses_dev->page1_types = type_ptr;
662 ses_dev->page1_num_types = types;
664 for (i = 0; i < types && type_ptr < buf + len; i++, type_ptr += 4) {
665 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
666 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE)
667 components += type_ptr[1];
669 ses_dev->page1 = buf;
670 ses_dev->page1_len = len;
673 result = ses_recv_diag(sdev, 2, hdr_buf, INIT_ALLOC_SIZE);
677 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
678 buf = kzalloc(len, GFP_KERNEL);
682 /* make sure getting page 2 actually works */
683 result = ses_recv_diag(sdev, 2, buf, len);
686 ses_dev->page2 = buf;
687 ses_dev->page2_len = len;
690 /* The additional information page --- allows us
691 * to match up the devices */
692 result = ses_recv_diag(sdev, 10, hdr_buf, INIT_ALLOC_SIZE);
695 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
696 buf = kzalloc(len, GFP_KERNEL);
700 result = ses_recv_diag(sdev, 10, buf, len);
703 ses_dev->page10 = buf;
704 ses_dev->page10_len = len;
707 scomp = kzalloc(sizeof(struct ses_component) * components, GFP_KERNEL);
711 edev = enclosure_register(cdev->parent, dev_name(&sdev->sdev_gendev),
712 components, &ses_enclosure_callbacks);
720 edev->scratch = ses_dev;
721 for (i = 0; i < components; i++)
722 edev->component[i].scratch = scomp + i;
724 ses_enclosure_data_process(edev, sdev, 1);
726 /* see if there are any devices matching before
727 * we found the enclosure */
728 shost_for_each_device(tmp_sdev, sdev->host) {
729 if (tmp_sdev->lun != 0 || scsi_device_enclosure(tmp_sdev))
731 ses_match_to_enclosure(edev, tmp_sdev);
737 sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n",
743 kfree(ses_dev->page10);
744 kfree(ses_dev->page2);
745 kfree(ses_dev->page1);
749 sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err);
753 static int ses_remove(struct device *dev)
758 static void ses_intf_remove_component(struct scsi_device *sdev)
760 struct enclosure_device *edev, *prev = NULL;
762 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
764 if (!enclosure_remove_device(edev, &sdev->sdev_gendev))
768 put_device(&edev->edev);
771 static void ses_intf_remove_enclosure(struct scsi_device *sdev)
773 struct enclosure_device *edev;
774 struct ses_device *ses_dev;
776 /* exact match to this enclosure */
777 edev = enclosure_find(&sdev->sdev_gendev, NULL);
781 enclosure_unregister(edev);
783 ses_dev = edev->scratch;
784 edev->scratch = NULL;
786 kfree(ses_dev->page10);
787 kfree(ses_dev->page1);
788 kfree(ses_dev->page2);
791 kfree(edev->component[0].scratch);
793 put_device(&edev->edev);
796 static void ses_intf_remove(struct device *cdev,
797 struct class_interface *intf)
799 struct scsi_device *sdev = to_scsi_device(cdev->parent);
801 if (!scsi_device_enclosure(sdev))
802 ses_intf_remove_component(sdev);
804 ses_intf_remove_enclosure(sdev);
807 static struct class_interface ses_interface = {
808 .add_dev = ses_intf_add,
809 .remove_dev = ses_intf_remove,
812 static struct scsi_driver ses_template = {
815 .owner = THIS_MODULE,
817 .remove = ses_remove,
821 static int __init ses_init(void)
825 err = scsi_register_interface(&ses_interface);
829 err = scsi_register_driver(&ses_template.gendrv);
836 scsi_unregister_interface(&ses_interface);
840 static void __exit ses_exit(void)
842 scsi_unregister_driver(&ses_template.gendrv);
843 scsi_unregister_interface(&ses_interface);
846 module_init(ses_init);
847 module_exit(ses_exit);
849 MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE);
851 MODULE_AUTHOR("James Bottomley");
852 MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
853 MODULE_LICENSE("GPL v2");