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>
39 unsigned char *page1_types
;
41 unsigned char *page10
;
43 short page1_num_types
;
48 struct ses_component
{
52 static int ses_probe(struct device
*dev
)
54 struct scsi_device
*sdev
= to_scsi_device(dev
);
57 if (sdev
->type
!= TYPE_ENCLOSURE
)
61 sdev_printk(KERN_NOTICE
, sdev
, "Attached Enclosure device\n");
67 #define SES_TIMEOUT (30 * HZ)
70 static void init_device_slot_control(unsigned char *dest_desc
,
71 struct enclosure_component
*ecomp
,
72 unsigned char *status
)
74 memcpy(dest_desc
, status
, 4);
76 /* only clear byte 1 for ENCLOSURE_COMPONENT_DEVICE */
77 if (ecomp
->type
== ENCLOSURE_COMPONENT_DEVICE
)
84 static int ses_recv_diag(struct scsi_device
*sdev
, int page_code
,
85 void *buf
, int bufflen
)
87 unsigned char cmd
[] = {
96 return scsi_execute_req(sdev
, cmd
, DMA_FROM_DEVICE
, buf
, bufflen
,
97 NULL
, SES_TIMEOUT
, SES_RETRIES
, NULL
);
100 static int ses_send_diag(struct scsi_device
*sdev
, int page_code
,
101 void *buf
, int bufflen
)
105 unsigned char cmd
[] = {
107 0x10, /* Set PF bit */
114 result
= scsi_execute_req(sdev
, cmd
, DMA_TO_DEVICE
, buf
, bufflen
,
115 NULL
, SES_TIMEOUT
, SES_RETRIES
, NULL
);
117 sdev_printk(KERN_ERR
, sdev
, "SEND DIAGNOSTIC result: %8x\n",
122 static int ses_set_page2_descriptor(struct enclosure_device
*edev
,
123 struct enclosure_component
*ecomp
,
126 int i
, j
, count
= 0, descriptor
= ecomp
->number
;
127 struct scsi_device
*sdev
= to_scsi_device(edev
->edev
.parent
);
128 struct ses_device
*ses_dev
= edev
->scratch
;
129 unsigned char *type_ptr
= ses_dev
->page1_types
;
130 unsigned char *desc_ptr
= ses_dev
->page2
+ 8;
132 /* Clear everything */
133 memset(desc_ptr
, 0, ses_dev
->page2_len
- 8);
134 for (i
= 0; i
< ses_dev
->page1_num_types
; i
++, type_ptr
+= 4) {
135 for (j
= 0; j
< type_ptr
[1]; j
++) {
137 if (type_ptr
[0] != ENCLOSURE_COMPONENT_DEVICE
&&
138 type_ptr
[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE
)
140 if (count
++ == descriptor
) {
141 memcpy(desc_ptr
, desc
, 4);
144 /* clear reserved, just in case */
150 return ses_send_diag(sdev
, 2, ses_dev
->page2
, ses_dev
->page2_len
);
153 static unsigned char *ses_get_page2_descriptor(struct enclosure_device
*edev
,
154 struct enclosure_component
*ecomp
)
156 int i
, j
, count
= 0, descriptor
= ecomp
->number
;
157 struct scsi_device
*sdev
= to_scsi_device(edev
->edev
.parent
);
158 struct ses_device
*ses_dev
= edev
->scratch
;
159 unsigned char *type_ptr
= ses_dev
->page1_types
;
160 unsigned char *desc_ptr
= ses_dev
->page2
+ 8;
162 ses_recv_diag(sdev
, 2, ses_dev
->page2
, ses_dev
->page2_len
);
164 for (i
= 0; i
< ses_dev
->page1_num_types
; i
++, type_ptr
+= 4) {
165 for (j
= 0; j
< type_ptr
[1]; j
++) {
167 if (type_ptr
[0] != ENCLOSURE_COMPONENT_DEVICE
&&
168 type_ptr
[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE
)
170 if (count
++ == descriptor
)
177 /* For device slot and array device slot elements, byte 3 bit 6
178 * is "fault sensed" while byte 3 bit 5 is "fault reqstd". As this
179 * code stands these bits are shifted 4 positions right so in
180 * sysfs they will appear as bits 2 and 1 respectively. Strange. */
181 static void ses_get_fault(struct enclosure_device
*edev
,
182 struct enclosure_component
*ecomp
)
186 desc
= ses_get_page2_descriptor(edev
, ecomp
);
188 ecomp
->fault
= (desc
[3] & 0x60) >> 4;
191 static int ses_set_fault(struct enclosure_device
*edev
,
192 struct enclosure_component
*ecomp
,
193 enum enclosure_component_setting val
)
195 unsigned char desc
[4];
196 unsigned char *desc_ptr
;
198 desc_ptr
= ses_get_page2_descriptor(edev
, ecomp
);
203 init_device_slot_control(desc
, ecomp
, desc_ptr
);
206 case ENCLOSURE_SETTING_DISABLED
:
209 case ENCLOSURE_SETTING_ENABLED
:
213 /* SES doesn't do the SGPIO blink settings */
217 return ses_set_page2_descriptor(edev
, ecomp
, desc
);
220 static void ses_get_status(struct enclosure_device
*edev
,
221 struct enclosure_component
*ecomp
)
225 desc
= ses_get_page2_descriptor(edev
, ecomp
);
227 ecomp
->status
= (desc
[0] & 0x0f);
230 static void ses_get_locate(struct enclosure_device
*edev
,
231 struct enclosure_component
*ecomp
)
235 desc
= ses_get_page2_descriptor(edev
, ecomp
);
237 ecomp
->locate
= (desc
[2] & 0x02) ? 1 : 0;
240 static int ses_set_locate(struct enclosure_device
*edev
,
241 struct enclosure_component
*ecomp
,
242 enum enclosure_component_setting val
)
244 unsigned char desc
[4];
245 unsigned char *desc_ptr
;
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 */
265 return ses_set_page2_descriptor(edev
, ecomp
, desc
);
268 static int ses_set_active(struct enclosure_device
*edev
,
269 struct enclosure_component
*ecomp
,
270 enum enclosure_component_setting val
)
272 unsigned char desc
[4];
273 unsigned char *desc_ptr
;
275 desc_ptr
= ses_get_page2_descriptor(edev
, ecomp
);
280 init_device_slot_control(desc
, ecomp
, desc_ptr
);
283 case ENCLOSURE_SETTING_DISABLED
:
287 case ENCLOSURE_SETTING_ENABLED
:
292 /* SES doesn't do the SGPIO blink settings */
295 return ses_set_page2_descriptor(edev
, ecomp
, desc
);
298 static int ses_show_id(struct enclosure_device
*edev
, char *buf
)
300 struct ses_device
*ses_dev
= edev
->scratch
;
301 unsigned long long id
= get_unaligned_be64(ses_dev
->page1
+8+4);
303 return sprintf(buf
, "%#llx\n", id
);
306 static void ses_get_power_status(struct enclosure_device
*edev
,
307 struct enclosure_component
*ecomp
)
311 desc
= ses_get_page2_descriptor(edev
, ecomp
);
313 ecomp
->power_status
= (desc
[3] & 0x10) ? 0 : 1;
316 static int ses_set_power_status(struct enclosure_device
*edev
,
317 struct enclosure_component
*ecomp
,
320 unsigned char desc
[4];
321 unsigned char *desc_ptr
;
323 desc_ptr
= ses_get_page2_descriptor(edev
, ecomp
);
328 init_device_slot_control(desc
, ecomp
, desc_ptr
);
331 /* power = 1 is device_off = 0 and vice versa */
341 ecomp
->power_status
= val
;
342 return ses_set_page2_descriptor(edev
, ecomp
, desc
);
345 static struct enclosure_component_callbacks ses_enclosure_callbacks
= {
346 .get_fault
= ses_get_fault
,
347 .set_fault
= ses_set_fault
,
348 .get_status
= ses_get_status
,
349 .get_locate
= ses_get_locate
,
350 .set_locate
= ses_set_locate
,
351 .get_power_status
= ses_get_power_status
,
352 .set_power_status
= ses_set_power_status
,
353 .set_active
= ses_set_active
,
354 .show_id
= ses_show_id
,
357 struct ses_host_edev
{
358 struct Scsi_Host
*shost
;
359 struct enclosure_device
*edev
;
363 int ses_match_host(struct enclosure_device
*edev
, void *data
)
365 struct ses_host_edev
*sed
= data
;
366 struct scsi_device
*sdev
;
368 if (!scsi_is_sdev_device(edev
->edev
.parent
))
371 sdev
= to_scsi_device(edev
->edev
.parent
);
373 if (sdev
->host
!= sed
->shost
)
381 static void ses_process_descriptor(struct enclosure_component
*ecomp
,
384 int eip
= desc
[0] & 0x10;
385 int invalid
= desc
[0] & 0x80;
386 enum scsi_protocol proto
= desc
[0] & 0x0f;
389 struct ses_component
*scomp
= ecomp
->scratch
;
396 case SCSI_PROTOCOL_FCP
:
402 case SCSI_PROTOCOL_SAS
:
409 /* only take the phy0 addr */
410 addr
= (u64
)d
[12] << 56 |
420 /* FIXME: Need to add more protocols than just SAS */
432 static int ses_enclosure_find_by_addr(struct enclosure_device
*edev
,
435 struct efd
*efd
= data
;
437 struct ses_component
*scomp
;
439 if (!edev
->component
[0].scratch
)
442 for (i
= 0; i
< edev
->components
; i
++) {
443 scomp
= edev
->component
[i
].scratch
;
444 if (scomp
->addr
!= efd
->addr
)
447 if (enclosure_add_device(edev
, i
, efd
->dev
) == 0)
448 kobject_uevent(&efd
->dev
->kobj
, KOBJ_CHANGE
);
454 #define INIT_ALLOC_SIZE 32
456 static void ses_enclosure_data_process(struct enclosure_device
*edev
,
457 struct scsi_device
*sdev
,
461 unsigned char *buf
= NULL
, *type_ptr
, *desc_ptr
, *addl_desc_ptr
= NULL
;
462 int i
, j
, page7_len
, len
, components
;
463 struct ses_device
*ses_dev
= edev
->scratch
;
464 int types
= ses_dev
->page1_num_types
;
465 unsigned char *hdr_buf
= kzalloc(INIT_ALLOC_SIZE
, GFP_KERNEL
);
468 goto simple_populate
;
470 /* re-read page 10 */
472 ses_recv_diag(sdev
, 10, ses_dev
->page10
, ses_dev
->page10_len
);
473 /* Page 7 for the descriptors is optional */
474 result
= ses_recv_diag(sdev
, 7, hdr_buf
, INIT_ALLOC_SIZE
);
476 goto simple_populate
;
478 page7_len
= len
= (hdr_buf
[2] << 8) + hdr_buf
[3] + 4;
479 /* add 1 for trailing '\0' we'll use */
480 buf
= kzalloc(len
+ 1, GFP_KERNEL
);
482 goto simple_populate
;
483 result
= ses_recv_diag(sdev
, 7, buf
, len
);
493 len
= (desc_ptr
[2] << 8) + desc_ptr
[3];
494 /* skip past overall descriptor */
498 addl_desc_ptr
= ses_dev
->page10
+ 8;
499 type_ptr
= ses_dev
->page1_types
;
501 for (i
= 0; i
< types
; i
++, type_ptr
+= 4) {
502 for (j
= 0; j
< type_ptr
[1]; j
++) {
504 struct enclosure_component
*ecomp
;
507 if (desc_ptr
>= buf
+ page7_len
) {
510 len
= (desc_ptr
[2] << 8) + desc_ptr
[3];
512 /* Add trailing zero - pushes into
514 desc_ptr
[len
] = '\0';
518 if (type_ptr
[0] == ENCLOSURE_COMPONENT_DEVICE
||
519 type_ptr
[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE
) {
522 ecomp
= enclosure_component_alloc(
528 ecomp
= &edev
->component
[components
++];
530 if (!IS_ERR(ecomp
)) {
531 ses_get_power_status(edev
, ecomp
);
533 ses_process_descriptor(
537 enclosure_component_register(
545 addl_desc_ptr
+= addl_desc_ptr
[1] + 2;
553 static void ses_match_to_enclosure(struct enclosure_device
*edev
,
554 struct scsi_device
*sdev
)
561 ses_enclosure_data_process(edev
, to_scsi_device(edev
->edev
.parent
), 0);
563 if (!sdev
->vpd_pg83_len
)
566 desc
= sdev
->vpd_pg83
+ 4;
567 while (desc
< sdev
->vpd_pg83
+ sdev
->vpd_pg83_len
) {
568 enum scsi_protocol proto
= desc
[0] >> 4;
569 u8 code_set
= desc
[0] & 0x0f;
570 u8 piv
= desc
[1] & 0x80;
571 u8 assoc
= (desc
[1] & 0x30) >> 4;
572 u8 type
= desc
[1] & 0x0f;
575 if (piv
&& code_set
== 1 && assoc
== 1
576 && proto
== SCSI_PROTOCOL_SAS
&& type
== 3 && len
== 8)
577 efd
.addr
= get_unaligned_be64(&desc
[4]);
582 efd
.dev
= &sdev
->sdev_gendev
;
584 enclosure_for_each_device(ses_enclosure_find_by_addr
, &efd
);
588 static int ses_intf_add(struct device
*cdev
,
589 struct class_interface
*intf
)
591 struct scsi_device
*sdev
= to_scsi_device(cdev
->parent
);
592 struct scsi_device
*tmp_sdev
;
593 unsigned char *buf
= NULL
, *hdr_buf
, *type_ptr
;
594 struct ses_device
*ses_dev
;
596 int i
, types
, len
, components
= 0;
599 struct enclosure_device
*edev
;
600 struct ses_component
*scomp
= NULL
;
602 if (!scsi_device_enclosure(sdev
)) {
603 /* not an enclosure, but might be in one */
604 struct enclosure_device
*prev
= NULL
;
606 while ((edev
= enclosure_find(&sdev
->host
->shost_gendev
, prev
)) != NULL
) {
607 ses_match_to_enclosure(edev
, sdev
);
613 /* TYPE_ENCLOSURE prints a message in probe */
614 if (sdev
->type
!= TYPE_ENCLOSURE
)
615 sdev_printk(KERN_NOTICE
, sdev
, "Embedded Enclosure Device\n");
617 ses_dev
= kzalloc(sizeof(*ses_dev
), GFP_KERNEL
);
618 hdr_buf
= kzalloc(INIT_ALLOC_SIZE
, GFP_KERNEL
);
619 if (!hdr_buf
|| !ses_dev
)
622 result
= ses_recv_diag(sdev
, 1, hdr_buf
, INIT_ALLOC_SIZE
);
626 len
= (hdr_buf
[2] << 8) + hdr_buf
[3] + 4;
627 buf
= kzalloc(len
, GFP_KERNEL
);
631 result
= ses_recv_diag(sdev
, 1, buf
, len
);
637 /* we always have one main enclosure and the rest are referred
638 * to as secondary subenclosures */
639 num_enclosures
= buf
[1] + 1;
641 /* begin at the enclosure descriptor */
643 /* skip all the enclosure descriptors */
644 for (i
= 0; i
< num_enclosures
&& type_ptr
< buf
+ len
; i
++) {
645 types
+= type_ptr
[2];
646 type_ptr
+= type_ptr
[3] + 4;
649 ses_dev
->page1_types
= type_ptr
;
650 ses_dev
->page1_num_types
= types
;
652 for (i
= 0; i
< types
&& type_ptr
< buf
+ len
; i
++, type_ptr
+= 4) {
653 if (type_ptr
[0] == ENCLOSURE_COMPONENT_DEVICE
||
654 type_ptr
[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE
)
655 components
+= type_ptr
[1];
657 ses_dev
->page1
= buf
;
658 ses_dev
->page1_len
= len
;
661 result
= ses_recv_diag(sdev
, 2, hdr_buf
, INIT_ALLOC_SIZE
);
665 len
= (hdr_buf
[2] << 8) + hdr_buf
[3] + 4;
666 buf
= kzalloc(len
, GFP_KERNEL
);
670 /* make sure getting page 2 actually works */
671 result
= ses_recv_diag(sdev
, 2, buf
, len
);
674 ses_dev
->page2
= buf
;
675 ses_dev
->page2_len
= len
;
678 /* The additional information page --- allows us
679 * to match up the devices */
680 result
= ses_recv_diag(sdev
, 10, hdr_buf
, INIT_ALLOC_SIZE
);
683 len
= (hdr_buf
[2] << 8) + hdr_buf
[3] + 4;
684 buf
= kzalloc(len
, GFP_KERNEL
);
688 result
= ses_recv_diag(sdev
, 10, buf
, len
);
691 ses_dev
->page10
= buf
;
692 ses_dev
->page10_len
= len
;
695 scomp
= kzalloc(sizeof(struct ses_component
) * components
, GFP_KERNEL
);
699 edev
= enclosure_register(cdev
->parent
, dev_name(&sdev
->sdev_gendev
),
700 components
, &ses_enclosure_callbacks
);
708 edev
->scratch
= ses_dev
;
709 for (i
= 0; i
< components
; i
++)
710 edev
->component
[i
].scratch
= scomp
+ i
;
712 ses_enclosure_data_process(edev
, sdev
, 1);
714 /* see if there are any devices matching before
715 * we found the enclosure */
716 shost_for_each_device(tmp_sdev
, sdev
->host
) {
717 if (tmp_sdev
->lun
!= 0 || scsi_device_enclosure(tmp_sdev
))
719 ses_match_to_enclosure(edev
, tmp_sdev
);
725 sdev_printk(KERN_ERR
, sdev
, "Failed to get diagnostic page 0x%x\n",
731 kfree(ses_dev
->page10
);
732 kfree(ses_dev
->page2
);
733 kfree(ses_dev
->page1
);
737 sdev_printk(KERN_ERR
, sdev
, "Failed to bind enclosure %d\n", err
);
741 static int ses_remove(struct device
*dev
)
746 static void ses_intf_remove_component(struct scsi_device
*sdev
)
748 struct enclosure_device
*edev
, *prev
= NULL
;
750 while ((edev
= enclosure_find(&sdev
->host
->shost_gendev
, prev
)) != NULL
) {
752 if (!enclosure_remove_device(edev
, &sdev
->sdev_gendev
))
756 put_device(&edev
->edev
);
759 static void ses_intf_remove_enclosure(struct scsi_device
*sdev
)
761 struct enclosure_device
*edev
;
762 struct ses_device
*ses_dev
;
764 /* exact match to this enclosure */
765 edev
= enclosure_find(&sdev
->sdev_gendev
, NULL
);
769 ses_dev
= edev
->scratch
;
770 edev
->scratch
= NULL
;
772 kfree(ses_dev
->page10
);
773 kfree(ses_dev
->page1
);
774 kfree(ses_dev
->page2
);
777 kfree(edev
->component
[0].scratch
);
779 put_device(&edev
->edev
);
780 enclosure_unregister(edev
);
783 static void ses_intf_remove(struct device
*cdev
,
784 struct class_interface
*intf
)
786 struct scsi_device
*sdev
= to_scsi_device(cdev
->parent
);
788 if (!scsi_device_enclosure(sdev
))
789 ses_intf_remove_component(sdev
);
791 ses_intf_remove_enclosure(sdev
);
794 static struct class_interface ses_interface
= {
795 .add_dev
= ses_intf_add
,
796 .remove_dev
= ses_intf_remove
,
799 static struct scsi_driver ses_template
= {
802 .owner
= THIS_MODULE
,
804 .remove
= ses_remove
,
808 static int __init
ses_init(void)
812 err
= scsi_register_interface(&ses_interface
);
816 err
= scsi_register_driver(&ses_template
.gendrv
);
823 scsi_unregister_interface(&ses_interface
);
827 static void __exit
ses_exit(void)
829 scsi_unregister_driver(&ses_template
.gendrv
);
830 scsi_unregister_interface(&ses_interface
);
833 module_init(ses_init
);
834 module_exit(ses_exit
);
836 MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE
);
838 MODULE_AUTHOR("James Bottomley");
839 MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
840 MODULE_LICENSE("GPL v2");