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>
39 unsigned char *page1_types;
41 unsigned char *page10;
43 short page1_num_types;
48 struct ses_component {
53 static int ses_probe(struct device *dev)
55 struct scsi_device *sdev = to_scsi_device(dev);
58 if (sdev->type != TYPE_ENCLOSURE)
62 sdev_printk(KERN_NOTICE, sdev, "Attached Enclosure device\n");
68 #define SES_TIMEOUT (30 * HZ)
71 static int ses_recv_diag(struct scsi_device *sdev, int page_code,
72 void *buf, int bufflen)
74 unsigned char cmd[] = {
83 return scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, bufflen,
84 NULL, SES_TIMEOUT, SES_RETRIES, NULL);
87 static int ses_send_diag(struct scsi_device *sdev, int page_code,
88 void *buf, int bufflen)
92 unsigned char cmd[] = {
94 0x10, /* Set PF bit */
101 result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, buf, bufflen,
102 NULL, SES_TIMEOUT, SES_RETRIES, NULL);
104 sdev_printk(KERN_ERR, sdev, "SEND DIAGNOSTIC result: %8x\n",
109 static int ses_set_page2_descriptor(struct enclosure_device *edev,
110 struct enclosure_component *ecomp,
113 int i, j, count = 0, descriptor = ecomp->number;
114 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
115 struct ses_device *ses_dev = edev->scratch;
116 unsigned char *type_ptr = ses_dev->page1_types;
117 unsigned char *desc_ptr = ses_dev->page2 + 8;
119 /* Clear everything */
120 memset(desc_ptr, 0, ses_dev->page2_len - 8);
121 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
122 for (j = 0; j < type_ptr[1]; j++) {
124 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
125 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
127 if (count++ == descriptor) {
128 memcpy(desc_ptr, desc, 4);
131 /* clear reserved, just in case */
137 return ses_send_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len);
140 static unsigned char *ses_get_page2_descriptor(struct enclosure_device *edev,
141 struct enclosure_component *ecomp)
143 int i, j, count = 0, descriptor = ecomp->number;
144 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
145 struct ses_device *ses_dev = edev->scratch;
146 unsigned char *type_ptr = ses_dev->page1_types;
147 unsigned char *desc_ptr = ses_dev->page2 + 8;
149 ses_recv_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len);
151 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
152 for (j = 0; j < type_ptr[1]; j++) {
154 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
155 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
157 if (count++ == descriptor)
164 /* For device slot and array device slot elements, byte 3 bit 6
165 * is "fault sensed" while byte 3 bit 5 is "fault reqstd". As this
166 * code stands these bits are shifted 4 positions right so in
167 * sysfs they will appear as bits 2 and 1 respectively. Strange. */
168 static void ses_get_fault(struct enclosure_device *edev,
169 struct enclosure_component *ecomp)
173 desc = ses_get_page2_descriptor(edev, ecomp);
175 ecomp->fault = (desc[3] & 0x60) >> 4;
178 static int ses_set_fault(struct enclosure_device *edev,
179 struct enclosure_component *ecomp,
180 enum enclosure_component_setting val)
182 unsigned char desc[4] = {0 };
185 case ENCLOSURE_SETTING_DISABLED:
186 /* zero is disabled */
188 case ENCLOSURE_SETTING_ENABLED:
192 /* SES doesn't do the SGPIO blink settings */
196 return ses_set_page2_descriptor(edev, ecomp, desc);
199 static void ses_get_status(struct enclosure_device *edev,
200 struct enclosure_component *ecomp)
204 desc = ses_get_page2_descriptor(edev, ecomp);
206 ecomp->status = (desc[0] & 0x0f);
209 static void ses_get_locate(struct enclosure_device *edev,
210 struct enclosure_component *ecomp)
214 desc = ses_get_page2_descriptor(edev, ecomp);
216 ecomp->locate = (desc[2] & 0x02) ? 1 : 0;
219 static int ses_set_locate(struct enclosure_device *edev,
220 struct enclosure_component *ecomp,
221 enum enclosure_component_setting val)
223 unsigned char desc[4] = {0 };
226 case ENCLOSURE_SETTING_DISABLED:
227 /* zero is disabled */
229 case ENCLOSURE_SETTING_ENABLED:
233 /* SES doesn't do the SGPIO blink settings */
236 return ses_set_page2_descriptor(edev, ecomp, desc);
239 static int ses_set_active(struct enclosure_device *edev,
240 struct enclosure_component *ecomp,
241 enum enclosure_component_setting val)
243 unsigned char desc[4] = {0 };
246 case ENCLOSURE_SETTING_DISABLED:
247 /* zero is disabled */
250 case ENCLOSURE_SETTING_ENABLED:
255 /* SES doesn't do the SGPIO blink settings */
258 return ses_set_page2_descriptor(edev, ecomp, desc);
261 static struct enclosure_component_callbacks ses_enclosure_callbacks = {
262 .get_fault = ses_get_fault,
263 .set_fault = ses_set_fault,
264 .get_status = ses_get_status,
265 .get_locate = ses_get_locate,
266 .set_locate = ses_set_locate,
267 .set_active = ses_set_active,
270 struct ses_host_edev {
271 struct Scsi_Host *shost;
272 struct enclosure_device *edev;
276 int ses_match_host(struct enclosure_device *edev, void *data)
278 struct ses_host_edev *sed = data;
279 struct scsi_device *sdev;
281 if (!scsi_is_sdev_device(edev->edev.parent))
284 sdev = to_scsi_device(edev->edev.parent);
286 if (sdev->host != sed->shost)
294 static void ses_process_descriptor(struct enclosure_component *ecomp,
297 int eip = desc[0] & 0x10;
298 int invalid = desc[0] & 0x80;
299 enum scsi_protocol proto = desc[0] & 0x0f;
301 struct ses_component *scomp = ecomp->scratch;
310 case SCSI_PROTOCOL_SAS:
315 /* only take the phy0 addr */
316 addr = (u64)d[12] << 56 |
326 /* FIXME: Need to add more protocols than just SAS */
337 static int ses_enclosure_find_by_addr(struct enclosure_device *edev,
340 struct efd *efd = data;
342 struct ses_component *scomp;
344 if (!edev->component[0].scratch)
347 for (i = 0; i < edev->components; i++) {
348 scomp = edev->component[i].scratch;
349 if (scomp->addr != efd->addr)
352 enclosure_add_device(edev, i, efd->dev);
358 #define INIT_ALLOC_SIZE 32
360 static void ses_enclosure_data_process(struct enclosure_device *edev,
361 struct scsi_device *sdev,
365 unsigned char *buf = NULL, *type_ptr, *desc_ptr, *addl_desc_ptr = NULL;
366 int i, j, page7_len, len, components;
367 struct ses_device *ses_dev = edev->scratch;
368 int types = ses_dev->page1_num_types;
369 unsigned char *hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
372 goto simple_populate;
374 /* re-read page 10 */
376 ses_recv_diag(sdev, 10, ses_dev->page10, ses_dev->page10_len);
377 /* Page 7 for the descriptors is optional */
378 result = ses_recv_diag(sdev, 7, hdr_buf, INIT_ALLOC_SIZE);
380 goto simple_populate;
382 page7_len = len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
383 /* add 1 for trailing '\0' we'll use */
384 buf = kzalloc(len + 1, GFP_KERNEL);
386 goto simple_populate;
387 result = ses_recv_diag(sdev, 7, buf, len);
397 len = (desc_ptr[2] << 8) + desc_ptr[3];
398 /* skip past overall descriptor */
402 addl_desc_ptr = ses_dev->page10 + 8;
403 type_ptr = ses_dev->page1_types;
405 for (i = 0; i < types; i++, type_ptr += 4) {
406 for (j = 0; j < type_ptr[1]; j++) {
408 struct enclosure_component *ecomp;
411 if (desc_ptr >= buf + page7_len) {
414 len = (desc_ptr[2] << 8) + desc_ptr[3];
416 /* Add trailing zero - pushes into
418 desc_ptr[len] = '\0';
422 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
423 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) {
426 ecomp = enclosure_component_register(edev,
431 ecomp = &edev->component[components++];
433 if (!IS_ERR(ecomp) && addl_desc_ptr)
434 ses_process_descriptor(ecomp,
441 addl_desc_ptr += addl_desc_ptr[1] + 2;
449 static void ses_match_to_enclosure(struct enclosure_device *edev,
450 struct scsi_device *sdev)
457 ses_enclosure_data_process(edev, to_scsi_device(edev->edev.parent), 0);
459 if (!sdev->vpd_pg83_len)
462 desc = sdev->vpd_pg83 + 4;
463 while (desc < sdev->vpd_pg83 + sdev->vpd_pg83_len) {
464 enum scsi_protocol proto = desc[0] >> 4;
465 u8 code_set = desc[0] & 0x0f;
466 u8 piv = desc[1] & 0x80;
467 u8 assoc = (desc[1] & 0x30) >> 4;
468 u8 type = desc[1] & 0x0f;
471 if (piv && code_set == 1 && assoc == 1
472 && proto == SCSI_PROTOCOL_SAS && type == 3 && len == 8)
473 efd.addr = get_unaligned_be64(&desc[4]);
478 efd.dev = &sdev->sdev_gendev;
480 enclosure_for_each_device(ses_enclosure_find_by_addr, &efd);
484 static int ses_intf_add(struct device *cdev,
485 struct class_interface *intf)
487 struct scsi_device *sdev = to_scsi_device(cdev->parent);
488 struct scsi_device *tmp_sdev;
489 unsigned char *buf = NULL, *hdr_buf, *type_ptr;
490 struct ses_device *ses_dev;
492 int i, types, len, components = 0;
495 struct enclosure_device *edev;
496 struct ses_component *scomp = NULL;
498 if (!scsi_device_enclosure(sdev)) {
499 /* not an enclosure, but might be in one */
500 struct enclosure_device *prev = NULL;
502 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
503 ses_match_to_enclosure(edev, sdev);
509 /* TYPE_ENCLOSURE prints a message in probe */
510 if (sdev->type != TYPE_ENCLOSURE)
511 sdev_printk(KERN_NOTICE, sdev, "Embedded Enclosure Device\n");
513 ses_dev = kzalloc(sizeof(*ses_dev), GFP_KERNEL);
514 hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
515 if (!hdr_buf || !ses_dev)
518 result = ses_recv_diag(sdev, 1, hdr_buf, INIT_ALLOC_SIZE);
522 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
523 buf = kzalloc(len, GFP_KERNEL);
527 result = ses_recv_diag(sdev, 1, buf, len);
533 /* we always have one main enclosure and the rest are referred
534 * to as secondary subenclosures */
535 num_enclosures = buf[1] + 1;
537 /* begin at the enclosure descriptor */
539 /* skip all the enclosure descriptors */
540 for (i = 0; i < num_enclosures && type_ptr < buf + len; i++) {
541 types += type_ptr[2];
542 type_ptr += type_ptr[3] + 4;
545 ses_dev->page1_types = type_ptr;
546 ses_dev->page1_num_types = types;
548 for (i = 0; i < types && type_ptr < buf + len; i++, type_ptr += 4) {
549 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
550 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE)
551 components += type_ptr[1];
553 ses_dev->page1 = buf;
554 ses_dev->page1_len = len;
557 result = ses_recv_diag(sdev, 2, hdr_buf, INIT_ALLOC_SIZE);
561 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
562 buf = kzalloc(len, GFP_KERNEL);
566 /* make sure getting page 2 actually works */
567 result = ses_recv_diag(sdev, 2, buf, len);
570 ses_dev->page2 = buf;
571 ses_dev->page2_len = len;
574 /* The additional information page --- allows us
575 * to match up the devices */
576 result = ses_recv_diag(sdev, 10, hdr_buf, INIT_ALLOC_SIZE);
579 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
580 buf = kzalloc(len, GFP_KERNEL);
584 result = ses_recv_diag(sdev, 10, buf, len);
587 ses_dev->page10 = buf;
588 ses_dev->page10_len = len;
591 scomp = kzalloc(sizeof(struct ses_component) * components, GFP_KERNEL);
595 edev = enclosure_register(cdev->parent, dev_name(&sdev->sdev_gendev),
596 components, &ses_enclosure_callbacks);
604 edev->scratch = ses_dev;
605 for (i = 0; i < components; i++)
606 edev->component[i].scratch = scomp + i;
608 ses_enclosure_data_process(edev, sdev, 1);
610 /* see if there are any devices matching before
611 * we found the enclosure */
612 shost_for_each_device(tmp_sdev, sdev->host) {
613 if (tmp_sdev->lun != 0 || scsi_device_enclosure(tmp_sdev))
615 ses_match_to_enclosure(edev, tmp_sdev);
621 sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n",
627 kfree(ses_dev->page10);
628 kfree(ses_dev->page2);
629 kfree(ses_dev->page1);
633 sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err);
637 static int ses_remove(struct device *dev)
642 static void ses_intf_remove_component(struct scsi_device *sdev)
644 struct enclosure_device *edev, *prev = NULL;
646 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
648 if (!enclosure_remove_device(edev, &sdev->sdev_gendev))
652 put_device(&edev->edev);
655 static void ses_intf_remove_enclosure(struct scsi_device *sdev)
657 struct enclosure_device *edev;
658 struct ses_device *ses_dev;
660 /* exact match to this enclosure */
661 edev = enclosure_find(&sdev->sdev_gendev, NULL);
665 ses_dev = edev->scratch;
666 edev->scratch = NULL;
668 kfree(ses_dev->page10);
669 kfree(ses_dev->page1);
670 kfree(ses_dev->page2);
673 kfree(edev->component[0].scratch);
675 put_device(&edev->edev);
676 enclosure_unregister(edev);
679 static void ses_intf_remove(struct device *cdev,
680 struct class_interface *intf)
682 struct scsi_device *sdev = to_scsi_device(cdev->parent);
684 if (!scsi_device_enclosure(sdev))
685 ses_intf_remove_component(sdev);
687 ses_intf_remove_enclosure(sdev);
690 static struct class_interface ses_interface = {
691 .add_dev = ses_intf_add,
692 .remove_dev = ses_intf_remove,
695 static struct scsi_driver ses_template = {
698 .owner = THIS_MODULE,
700 .remove = ses_remove,
704 static int __init ses_init(void)
708 err = scsi_register_interface(&ses_interface);
712 err = scsi_register_driver(&ses_template.gendrv);
719 scsi_unregister_interface(&ses_interface);
723 static void __exit ses_exit(void)
725 scsi_unregister_driver(&ses_template.gendrv);
726 scsi_unregister_interface(&ses_interface);
729 module_init(ses_init);
730 module_exit(ses_exit);
732 MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE);
734 MODULE_AUTHOR("James Bottomley");
735 MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
736 MODULE_LICENSE("GPL v2");