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
)
88 unsigned char cmd
[] = {
96 unsigned char recv_page_code
;
98 ret
= scsi_execute_req(sdev
, cmd
, DMA_FROM_DEVICE
, buf
, bufflen
,
99 NULL
, SES_TIMEOUT
, SES_RETRIES
, NULL
);
103 recv_page_code
= ((unsigned char *)buf
)[0];
105 if (likely(recv_page_code
== page_code
))
108 /* successful diagnostic but wrong page code. This happens to some
109 * USB devices, just print a message and pretend there was an error */
111 sdev_printk(KERN_ERR
, sdev
,
112 "Wrong diagnostic page; asked for %d got %u\n",
113 page_code
, recv_page_code
);
118 static int ses_send_diag(struct scsi_device
*sdev
, int page_code
,
119 void *buf
, int bufflen
)
123 unsigned char cmd
[] = {
125 0x10, /* Set PF bit */
132 result
= scsi_execute_req(sdev
, cmd
, DMA_TO_DEVICE
, buf
, bufflen
,
133 NULL
, SES_TIMEOUT
, SES_RETRIES
, NULL
);
135 sdev_printk(KERN_ERR
, sdev
, "SEND DIAGNOSTIC result: %8x\n",
140 static int ses_set_page2_descriptor(struct enclosure_device
*edev
,
141 struct enclosure_component
*ecomp
,
144 int i
, j
, count
= 0, descriptor
= ecomp
->number
;
145 struct scsi_device
*sdev
= to_scsi_device(edev
->edev
.parent
);
146 struct ses_device
*ses_dev
= edev
->scratch
;
147 unsigned char *type_ptr
= ses_dev
->page1_types
;
148 unsigned char *desc_ptr
= ses_dev
->page2
+ 8;
150 /* Clear everything */
151 memset(desc_ptr
, 0, ses_dev
->page2_len
- 8);
152 for (i
= 0; i
< ses_dev
->page1_num_types
; i
++, type_ptr
+= 4) {
153 for (j
= 0; j
< type_ptr
[1]; j
++) {
155 if (type_ptr
[0] != ENCLOSURE_COMPONENT_DEVICE
&&
156 type_ptr
[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE
)
158 if (count
++ == descriptor
) {
159 memcpy(desc_ptr
, desc
, 4);
162 /* clear reserved, just in case */
168 return ses_send_diag(sdev
, 2, ses_dev
->page2
, ses_dev
->page2_len
);
171 static unsigned char *ses_get_page2_descriptor(struct enclosure_device
*edev
,
172 struct enclosure_component
*ecomp
)
174 int i
, j
, count
= 0, descriptor
= ecomp
->number
;
175 struct scsi_device
*sdev
= to_scsi_device(edev
->edev
.parent
);
176 struct ses_device
*ses_dev
= edev
->scratch
;
177 unsigned char *type_ptr
= ses_dev
->page1_types
;
178 unsigned char *desc_ptr
= ses_dev
->page2
+ 8;
180 ses_recv_diag(sdev
, 2, ses_dev
->page2
, ses_dev
->page2_len
);
182 for (i
= 0; i
< ses_dev
->page1_num_types
; i
++, type_ptr
+= 4) {
183 for (j
= 0; j
< type_ptr
[1]; j
++) {
185 if (type_ptr
[0] != ENCLOSURE_COMPONENT_DEVICE
&&
186 type_ptr
[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE
)
188 if (count
++ == descriptor
)
195 /* For device slot and array device slot elements, byte 3 bit 6
196 * is "fault sensed" while byte 3 bit 5 is "fault reqstd". As this
197 * code stands these bits are shifted 4 positions right so in
198 * sysfs they will appear as bits 2 and 1 respectively. Strange. */
199 static void ses_get_fault(struct enclosure_device
*edev
,
200 struct enclosure_component
*ecomp
)
204 desc
= ses_get_page2_descriptor(edev
, ecomp
);
206 ecomp
->fault
= (desc
[3] & 0x60) >> 4;
209 static int ses_set_fault(struct enclosure_device
*edev
,
210 struct enclosure_component
*ecomp
,
211 enum enclosure_component_setting val
)
213 unsigned char desc
[4];
214 unsigned char *desc_ptr
;
216 desc_ptr
= ses_get_page2_descriptor(edev
, ecomp
);
221 init_device_slot_control(desc
, ecomp
, desc_ptr
);
224 case ENCLOSURE_SETTING_DISABLED
:
227 case ENCLOSURE_SETTING_ENABLED
:
231 /* SES doesn't do the SGPIO blink settings */
235 return ses_set_page2_descriptor(edev
, ecomp
, desc
);
238 static void ses_get_status(struct enclosure_device
*edev
,
239 struct enclosure_component
*ecomp
)
243 desc
= ses_get_page2_descriptor(edev
, ecomp
);
245 ecomp
->status
= (desc
[0] & 0x0f);
248 static void ses_get_locate(struct enclosure_device
*edev
,
249 struct enclosure_component
*ecomp
)
253 desc
= ses_get_page2_descriptor(edev
, ecomp
);
255 ecomp
->locate
= (desc
[2] & 0x02) ? 1 : 0;
258 static int ses_set_locate(struct enclosure_device
*edev
,
259 struct enclosure_component
*ecomp
,
260 enum enclosure_component_setting val
)
262 unsigned char desc
[4];
263 unsigned char *desc_ptr
;
265 desc_ptr
= ses_get_page2_descriptor(edev
, ecomp
);
270 init_device_slot_control(desc
, ecomp
, desc_ptr
);
273 case ENCLOSURE_SETTING_DISABLED
:
276 case ENCLOSURE_SETTING_ENABLED
:
280 /* SES doesn't do the SGPIO blink settings */
283 return ses_set_page2_descriptor(edev
, ecomp
, desc
);
286 static int ses_set_active(struct enclosure_device
*edev
,
287 struct enclosure_component
*ecomp
,
288 enum enclosure_component_setting val
)
290 unsigned char desc
[4];
291 unsigned char *desc_ptr
;
293 desc_ptr
= ses_get_page2_descriptor(edev
, ecomp
);
298 init_device_slot_control(desc
, ecomp
, desc_ptr
);
301 case ENCLOSURE_SETTING_DISABLED
:
305 case ENCLOSURE_SETTING_ENABLED
:
310 /* SES doesn't do the SGPIO blink settings */
313 return ses_set_page2_descriptor(edev
, ecomp
, desc
);
316 static int ses_show_id(struct enclosure_device
*edev
, char *buf
)
318 struct ses_device
*ses_dev
= edev
->scratch
;
319 unsigned long long id
= get_unaligned_be64(ses_dev
->page1
+8+4);
321 return sprintf(buf
, "%#llx\n", id
);
324 static void ses_get_power_status(struct enclosure_device
*edev
,
325 struct enclosure_component
*ecomp
)
329 desc
= ses_get_page2_descriptor(edev
, ecomp
);
331 ecomp
->power_status
= (desc
[3] & 0x10) ? 0 : 1;
334 static int ses_set_power_status(struct enclosure_device
*edev
,
335 struct enclosure_component
*ecomp
,
338 unsigned char desc
[4];
339 unsigned char *desc_ptr
;
341 desc_ptr
= ses_get_page2_descriptor(edev
, ecomp
);
346 init_device_slot_control(desc
, ecomp
, desc_ptr
);
349 /* power = 1 is device_off = 0 and vice versa */
359 ecomp
->power_status
= val
;
360 return ses_set_page2_descriptor(edev
, ecomp
, desc
);
363 static struct enclosure_component_callbacks ses_enclosure_callbacks
= {
364 .get_fault
= ses_get_fault
,
365 .set_fault
= ses_set_fault
,
366 .get_status
= ses_get_status
,
367 .get_locate
= ses_get_locate
,
368 .set_locate
= ses_set_locate
,
369 .get_power_status
= ses_get_power_status
,
370 .set_power_status
= ses_set_power_status
,
371 .set_active
= ses_set_active
,
372 .show_id
= ses_show_id
,
375 struct ses_host_edev
{
376 struct Scsi_Host
*shost
;
377 struct enclosure_device
*edev
;
381 int ses_match_host(struct enclosure_device
*edev
, void *data
)
383 struct ses_host_edev
*sed
= data
;
384 struct scsi_device
*sdev
;
386 if (!scsi_is_sdev_device(edev
->edev
.parent
))
389 sdev
= to_scsi_device(edev
->edev
.parent
);
391 if (sdev
->host
!= sed
->shost
)
399 static void ses_process_descriptor(struct enclosure_component
*ecomp
,
402 int eip
= desc
[0] & 0x10;
403 int invalid
= desc
[0] & 0x80;
404 enum scsi_protocol proto
= desc
[0] & 0x0f;
407 struct ses_component
*scomp
= ecomp
->scratch
;
414 case SCSI_PROTOCOL_FCP
:
420 case SCSI_PROTOCOL_SAS
:
427 /* only take the phy0 addr */
428 addr
= (u64
)d
[12] << 56 |
438 /* FIXME: Need to add more protocols than just SAS */
450 static int ses_enclosure_find_by_addr(struct enclosure_device
*edev
,
453 struct efd
*efd
= data
;
455 struct ses_component
*scomp
;
457 if (!edev
->component
[0].scratch
)
460 for (i
= 0; i
< edev
->components
; i
++) {
461 scomp
= edev
->component
[i
].scratch
;
462 if (scomp
->addr
!= efd
->addr
)
465 if (enclosure_add_device(edev
, i
, efd
->dev
) == 0)
466 kobject_uevent(&efd
->dev
->kobj
, KOBJ_CHANGE
);
472 #define INIT_ALLOC_SIZE 32
474 static void ses_enclosure_data_process(struct enclosure_device
*edev
,
475 struct scsi_device
*sdev
,
479 unsigned char *buf
= NULL
, *type_ptr
, *desc_ptr
, *addl_desc_ptr
= NULL
;
480 int i
, j
, page7_len
, len
, components
;
481 struct ses_device
*ses_dev
= edev
->scratch
;
482 int types
= ses_dev
->page1_num_types
;
483 unsigned char *hdr_buf
= kzalloc(INIT_ALLOC_SIZE
, GFP_KERNEL
);
486 goto simple_populate
;
488 /* re-read page 10 */
490 ses_recv_diag(sdev
, 10, ses_dev
->page10
, ses_dev
->page10_len
);
491 /* Page 7 for the descriptors is optional */
492 result
= ses_recv_diag(sdev
, 7, hdr_buf
, INIT_ALLOC_SIZE
);
494 goto simple_populate
;
496 page7_len
= len
= (hdr_buf
[2] << 8) + hdr_buf
[3] + 4;
497 /* add 1 for trailing '\0' we'll use */
498 buf
= kzalloc(len
+ 1, GFP_KERNEL
);
500 goto simple_populate
;
501 result
= ses_recv_diag(sdev
, 7, buf
, len
);
511 len
= (desc_ptr
[2] << 8) + desc_ptr
[3];
512 /* skip past overall descriptor */
516 addl_desc_ptr
= ses_dev
->page10
+ 8;
517 type_ptr
= ses_dev
->page1_types
;
519 for (i
= 0; i
< types
; i
++, type_ptr
+= 4) {
520 for (j
= 0; j
< type_ptr
[1]; j
++) {
522 struct enclosure_component
*ecomp
;
525 if (desc_ptr
>= buf
+ page7_len
) {
528 len
= (desc_ptr
[2] << 8) + desc_ptr
[3];
530 /* Add trailing zero - pushes into
532 desc_ptr
[len
] = '\0';
536 if (type_ptr
[0] == ENCLOSURE_COMPONENT_DEVICE
||
537 type_ptr
[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE
) {
540 ecomp
= enclosure_component_alloc(
546 ecomp
= &edev
->component
[components
++];
548 if (!IS_ERR(ecomp
)) {
550 ses_process_descriptor(
554 enclosure_component_register(
562 /* only find additional descriptions for specific devices */
563 (type_ptr
[0] == ENCLOSURE_COMPONENT_DEVICE
||
564 type_ptr
[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE
||
565 type_ptr
[0] == ENCLOSURE_COMPONENT_SAS_EXPANDER
||
566 /* these elements are optional */
567 type_ptr
[0] == ENCLOSURE_COMPONENT_SCSI_TARGET_PORT
||
568 type_ptr
[0] == ENCLOSURE_COMPONENT_SCSI_INITIATOR_PORT
||
569 type_ptr
[0] == ENCLOSURE_COMPONENT_CONTROLLER_ELECTRONICS
))
570 addl_desc_ptr
+= addl_desc_ptr
[1] + 2;
578 static void ses_match_to_enclosure(struct enclosure_device
*edev
,
579 struct scsi_device
*sdev
)
586 ses_enclosure_data_process(edev
, to_scsi_device(edev
->edev
.parent
), 0);
588 if (!sdev
->vpd_pg83_len
)
591 desc
= sdev
->vpd_pg83
+ 4;
592 while (desc
< sdev
->vpd_pg83
+ sdev
->vpd_pg83_len
) {
593 enum scsi_protocol proto
= desc
[0] >> 4;
594 u8 code_set
= desc
[0] & 0x0f;
595 u8 piv
= desc
[1] & 0x80;
596 u8 assoc
= (desc
[1] & 0x30) >> 4;
597 u8 type
= desc
[1] & 0x0f;
600 if (piv
&& code_set
== 1 && assoc
== 1
601 && proto
== SCSI_PROTOCOL_SAS
&& type
== 3 && len
== 8)
602 efd
.addr
= get_unaligned_be64(&desc
[4]);
607 efd
.dev
= &sdev
->sdev_gendev
;
609 enclosure_for_each_device(ses_enclosure_find_by_addr
, &efd
);
613 static int ses_intf_add(struct device
*cdev
,
614 struct class_interface
*intf
)
616 struct scsi_device
*sdev
= to_scsi_device(cdev
->parent
);
617 struct scsi_device
*tmp_sdev
;
618 unsigned char *buf
= NULL
, *hdr_buf
, *type_ptr
;
619 struct ses_device
*ses_dev
;
621 int i
, types
, len
, components
= 0;
624 struct enclosure_device
*edev
;
625 struct ses_component
*scomp
= NULL
;
627 if (!scsi_device_enclosure(sdev
)) {
628 /* not an enclosure, but might be in one */
629 struct enclosure_device
*prev
= NULL
;
631 while ((edev
= enclosure_find(&sdev
->host
->shost_gendev
, prev
)) != NULL
) {
632 ses_match_to_enclosure(edev
, sdev
);
638 /* TYPE_ENCLOSURE prints a message in probe */
639 if (sdev
->type
!= TYPE_ENCLOSURE
)
640 sdev_printk(KERN_NOTICE
, sdev
, "Embedded Enclosure Device\n");
642 ses_dev
= kzalloc(sizeof(*ses_dev
), GFP_KERNEL
);
643 hdr_buf
= kzalloc(INIT_ALLOC_SIZE
, GFP_KERNEL
);
644 if (!hdr_buf
|| !ses_dev
)
647 result
= ses_recv_diag(sdev
, 1, hdr_buf
, INIT_ALLOC_SIZE
);
651 len
= (hdr_buf
[2] << 8) + hdr_buf
[3] + 4;
652 buf
= kzalloc(len
, GFP_KERNEL
);
656 result
= ses_recv_diag(sdev
, 1, buf
, len
);
662 /* we always have one main enclosure and the rest are referred
663 * to as secondary subenclosures */
664 num_enclosures
= buf
[1] + 1;
666 /* begin at the enclosure descriptor */
668 /* skip all the enclosure descriptors */
669 for (i
= 0; i
< num_enclosures
&& type_ptr
< buf
+ len
; i
++) {
670 types
+= type_ptr
[2];
671 type_ptr
+= type_ptr
[3] + 4;
674 ses_dev
->page1_types
= type_ptr
;
675 ses_dev
->page1_num_types
= types
;
677 for (i
= 0; i
< types
&& type_ptr
< buf
+ len
; i
++, type_ptr
+= 4) {
678 if (type_ptr
[0] == ENCLOSURE_COMPONENT_DEVICE
||
679 type_ptr
[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE
)
680 components
+= type_ptr
[1];
682 ses_dev
->page1
= buf
;
683 ses_dev
->page1_len
= len
;
686 result
= ses_recv_diag(sdev
, 2, hdr_buf
, INIT_ALLOC_SIZE
);
690 len
= (hdr_buf
[2] << 8) + hdr_buf
[3] + 4;
691 buf
= kzalloc(len
, GFP_KERNEL
);
695 /* make sure getting page 2 actually works */
696 result
= ses_recv_diag(sdev
, 2, buf
, len
);
699 ses_dev
->page2
= buf
;
700 ses_dev
->page2_len
= len
;
703 /* The additional information page --- allows us
704 * to match up the devices */
705 result
= ses_recv_diag(sdev
, 10, hdr_buf
, INIT_ALLOC_SIZE
);
708 len
= (hdr_buf
[2] << 8) + hdr_buf
[3] + 4;
709 buf
= kzalloc(len
, GFP_KERNEL
);
713 result
= ses_recv_diag(sdev
, 10, buf
, len
);
716 ses_dev
->page10
= buf
;
717 ses_dev
->page10_len
= len
;
720 scomp
= kzalloc(sizeof(struct ses_component
) * components
, GFP_KERNEL
);
724 edev
= enclosure_register(cdev
->parent
, dev_name(&sdev
->sdev_gendev
),
725 components
, &ses_enclosure_callbacks
);
733 edev
->scratch
= ses_dev
;
734 for (i
= 0; i
< components
; i
++)
735 edev
->component
[i
].scratch
= scomp
+ i
;
737 ses_enclosure_data_process(edev
, sdev
, 1);
739 /* see if there are any devices matching before
740 * we found the enclosure */
741 shost_for_each_device(tmp_sdev
, sdev
->host
) {
742 if (tmp_sdev
->lun
!= 0 || scsi_device_enclosure(tmp_sdev
))
744 ses_match_to_enclosure(edev
, tmp_sdev
);
750 sdev_printk(KERN_ERR
, sdev
, "Failed to get diagnostic page 0x%x\n",
756 kfree(ses_dev
->page10
);
757 kfree(ses_dev
->page2
);
758 kfree(ses_dev
->page1
);
762 sdev_printk(KERN_ERR
, sdev
, "Failed to bind enclosure %d\n", err
);
766 static int ses_remove(struct device
*dev
)
771 static void ses_intf_remove_component(struct scsi_device
*sdev
)
773 struct enclosure_device
*edev
, *prev
= NULL
;
775 while ((edev
= enclosure_find(&sdev
->host
->shost_gendev
, prev
)) != NULL
) {
777 if (!enclosure_remove_device(edev
, &sdev
->sdev_gendev
))
781 put_device(&edev
->edev
);
784 static void ses_intf_remove_enclosure(struct scsi_device
*sdev
)
786 struct enclosure_device
*edev
;
787 struct ses_device
*ses_dev
;
789 /* exact match to this enclosure */
790 edev
= enclosure_find(&sdev
->sdev_gendev
, NULL
);
794 ses_dev
= edev
->scratch
;
795 edev
->scratch
= NULL
;
797 kfree(ses_dev
->page10
);
798 kfree(ses_dev
->page1
);
799 kfree(ses_dev
->page2
);
802 kfree(edev
->component
[0].scratch
);
804 put_device(&edev
->edev
);
805 enclosure_unregister(edev
);
808 static void ses_intf_remove(struct device
*cdev
,
809 struct class_interface
*intf
)
811 struct scsi_device
*sdev
= to_scsi_device(cdev
->parent
);
813 if (!scsi_device_enclosure(sdev
))
814 ses_intf_remove_component(sdev
);
816 ses_intf_remove_enclosure(sdev
);
819 static struct class_interface ses_interface
= {
820 .add_dev
= ses_intf_add
,
821 .remove_dev
= ses_intf_remove
,
824 static struct scsi_driver ses_template
= {
827 .owner
= THIS_MODULE
,
829 .remove
= ses_remove
,
833 static int __init
ses_init(void)
837 err
= scsi_register_interface(&ses_interface
);
841 err
= scsi_register_driver(&ses_template
.gendrv
);
848 scsi_unregister_interface(&ses_interface
);
852 static void __exit
ses_exit(void)
854 scsi_unregister_driver(&ses_template
.gendrv
);
855 scsi_unregister_interface(&ses_interface
);
858 module_init(ses_init
);
859 module_exit(ses_exit
);
861 MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE
);
863 MODULE_AUTHOR("James Bottomley");
864 MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
865 MODULE_LICENSE("GPL v2");