1 // SPDX-License-Identifier: GPL-2.0-only
3 * SCSI Enclosure Services
5 * Copyright (C) 2008 James Bottomley <James.Bottomley@HansenPartnership.com>
8 #include <linux/slab.h>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/enclosure.h>
12 #include <linux/unaligned.h>
14 #include <scsi/scsi.h>
15 #include <scsi/scsi_cmnd.h>
16 #include <scsi/scsi_dbg.h>
17 #include <scsi/scsi_device.h>
18 #include <scsi/scsi_driver.h>
19 #include <scsi/scsi_host.h>
21 #include <scsi/scsi_transport_sas.h>
25 unsigned char *page1_types
;
27 unsigned char *page10
;
29 short page1_num_types
;
34 struct ses_component
{
38 static bool ses_page2_supported(struct enclosure_device
*edev
)
40 struct ses_device
*ses_dev
= edev
->scratch
;
42 return (ses_dev
->page2
!= NULL
);
45 static int ses_probe(struct device
*dev
)
47 struct scsi_device
*sdev
= to_scsi_device(dev
);
50 if (sdev
->type
!= TYPE_ENCLOSURE
)
54 sdev_printk(KERN_NOTICE
, sdev
, "Attached Enclosure device\n");
60 #define SES_TIMEOUT (30 * HZ)
63 static void init_device_slot_control(unsigned char *dest_desc
,
64 struct enclosure_component
*ecomp
,
65 unsigned char *status
)
67 memcpy(dest_desc
, status
, 4);
69 /* only clear byte 1 for ENCLOSURE_COMPONENT_DEVICE */
70 if (ecomp
->type
== ENCLOSURE_COMPONENT_DEVICE
)
77 static int ses_recv_diag(struct scsi_device
*sdev
, int page_code
,
78 void *buf
, int bufflen
)
81 unsigned char cmd
[] = {
89 unsigned char recv_page_code
;
90 struct scsi_failure failure_defs
[] = {
92 .sense
= UNIT_ATTENTION
,
94 .ascq
= SCMD_FAILURE_ASCQ_ANY
,
95 .allowed
= SES_RETRIES
,
96 .result
= SAM_STAT_CHECK_CONDITION
,
100 .asc
= SCMD_FAILURE_ASC_ANY
,
101 .ascq
= SCMD_FAILURE_ASCQ_ANY
,
102 .allowed
= SES_RETRIES
,
103 .result
= SAM_STAT_CHECK_CONDITION
,
107 struct scsi_failures failures
= {
108 .failure_definitions
= failure_defs
,
110 const struct scsi_exec_args exec_args
= {
111 .failures
= &failures
,
114 ret
= scsi_execute_cmd(sdev
, cmd
, REQ_OP_DRV_IN
, buf
, bufflen
,
115 SES_TIMEOUT
, 1, &exec_args
);
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_failure failure_defs
[] = {
149 .sense
= UNIT_ATTENTION
,
151 .ascq
= SCMD_FAILURE_ASCQ_ANY
,
152 .allowed
= SES_RETRIES
,
153 .result
= SAM_STAT_CHECK_CONDITION
,
157 .asc
= SCMD_FAILURE_ASC_ANY
,
158 .ascq
= SCMD_FAILURE_ASCQ_ANY
,
159 .allowed
= SES_RETRIES
,
160 .result
= SAM_STAT_CHECK_CONDITION
,
164 struct scsi_failures failures
= {
165 .failure_definitions
= failure_defs
,
167 const struct scsi_exec_args exec_args
= {
168 .failures
= &failures
,
171 result
= scsi_execute_cmd(sdev
, cmd
, REQ_OP_DRV_OUT
, buf
, bufflen
,
172 SES_TIMEOUT
, 1, &exec_args
);
174 sdev_printk(KERN_ERR
, sdev
, "SEND DIAGNOSTIC result: %8x\n",
179 static int ses_set_page2_descriptor(struct enclosure_device
*edev
,
180 struct enclosure_component
*ecomp
,
183 int i
, j
, count
= 0, descriptor
= ecomp
->number
;
184 struct scsi_device
*sdev
= to_scsi_device(edev
->edev
.parent
);
185 struct ses_device
*ses_dev
= edev
->scratch
;
186 unsigned char *type_ptr
= ses_dev
->page1_types
;
187 unsigned char *desc_ptr
= ses_dev
->page2
+ 8;
189 /* Clear everything */
190 memset(desc_ptr
, 0, ses_dev
->page2_len
- 8);
191 for (i
= 0; i
< ses_dev
->page1_num_types
; i
++, type_ptr
+= 4) {
192 for (j
= 0; j
< type_ptr
[1]; j
++) {
194 if (type_ptr
[0] != ENCLOSURE_COMPONENT_DEVICE
&&
195 type_ptr
[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE
)
197 if (count
++ == descriptor
) {
198 memcpy(desc_ptr
, desc
, 4);
201 /* clear reserved, just in case */
207 return ses_send_diag(sdev
, 2, ses_dev
->page2
, ses_dev
->page2_len
);
210 static unsigned char *ses_get_page2_descriptor(struct enclosure_device
*edev
,
211 struct enclosure_component
*ecomp
)
213 int i
, j
, count
= 0, descriptor
= ecomp
->number
;
214 struct scsi_device
*sdev
= to_scsi_device(edev
->edev
.parent
);
215 struct ses_device
*ses_dev
= edev
->scratch
;
216 unsigned char *type_ptr
= ses_dev
->page1_types
;
217 unsigned char *desc_ptr
= ses_dev
->page2
+ 8;
219 if (ses_recv_diag(sdev
, 2, ses_dev
->page2
, ses_dev
->page2_len
) < 0)
222 for (i
= 0; i
< ses_dev
->page1_num_types
; i
++, type_ptr
+= 4) {
223 for (j
= 0; j
< type_ptr
[1]; j
++) {
225 if (type_ptr
[0] != ENCLOSURE_COMPONENT_DEVICE
&&
226 type_ptr
[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE
)
228 if (count
++ == descriptor
)
235 /* For device slot and array device slot elements, byte 3 bit 6
236 * is "fault sensed" while byte 3 bit 5 is "fault reqstd". As this
237 * code stands these bits are shifted 4 positions right so in
238 * sysfs they will appear as bits 2 and 1 respectively. Strange. */
239 static void ses_get_fault(struct enclosure_device
*edev
,
240 struct enclosure_component
*ecomp
)
244 if (!ses_page2_supported(edev
)) {
248 desc
= ses_get_page2_descriptor(edev
, ecomp
);
250 ecomp
->fault
= (desc
[3] & 0x60) >> 4;
253 static int ses_set_fault(struct enclosure_device
*edev
,
254 struct enclosure_component
*ecomp
,
255 enum enclosure_component_setting val
)
257 unsigned char desc
[4];
258 unsigned char *desc_ptr
;
260 if (!ses_page2_supported(edev
))
263 desc_ptr
= ses_get_page2_descriptor(edev
, ecomp
);
268 init_device_slot_control(desc
, ecomp
, desc_ptr
);
271 case ENCLOSURE_SETTING_DISABLED
:
274 case ENCLOSURE_SETTING_ENABLED
:
278 /* SES doesn't do the SGPIO blink settings */
282 return ses_set_page2_descriptor(edev
, ecomp
, desc
);
285 static void ses_get_status(struct enclosure_device
*edev
,
286 struct enclosure_component
*ecomp
)
290 if (!ses_page2_supported(edev
)) {
294 desc
= ses_get_page2_descriptor(edev
, ecomp
);
296 ecomp
->status
= (desc
[0] & 0x0f);
299 static void ses_get_locate(struct enclosure_device
*edev
,
300 struct enclosure_component
*ecomp
)
304 if (!ses_page2_supported(edev
)) {
308 desc
= ses_get_page2_descriptor(edev
, ecomp
);
310 ecomp
->locate
= (desc
[2] & 0x02) ? 1 : 0;
313 static int ses_set_locate(struct enclosure_device
*edev
,
314 struct enclosure_component
*ecomp
,
315 enum enclosure_component_setting val
)
317 unsigned char desc
[4];
318 unsigned char *desc_ptr
;
320 if (!ses_page2_supported(edev
))
323 desc_ptr
= ses_get_page2_descriptor(edev
, ecomp
);
328 init_device_slot_control(desc
, ecomp
, desc_ptr
);
331 case ENCLOSURE_SETTING_DISABLED
:
334 case ENCLOSURE_SETTING_ENABLED
:
338 /* SES doesn't do the SGPIO blink settings */
341 return ses_set_page2_descriptor(edev
, ecomp
, desc
);
344 static int ses_set_active(struct enclosure_device
*edev
,
345 struct enclosure_component
*ecomp
,
346 enum enclosure_component_setting val
)
348 unsigned char desc
[4];
349 unsigned char *desc_ptr
;
351 if (!ses_page2_supported(edev
))
354 desc_ptr
= ses_get_page2_descriptor(edev
, ecomp
);
359 init_device_slot_control(desc
, ecomp
, desc_ptr
);
362 case ENCLOSURE_SETTING_DISABLED
:
366 case ENCLOSURE_SETTING_ENABLED
:
371 /* SES doesn't do the SGPIO blink settings */
374 return ses_set_page2_descriptor(edev
, ecomp
, desc
);
377 static int ses_show_id(struct enclosure_device
*edev
, char *buf
)
379 struct ses_device
*ses_dev
= edev
->scratch
;
380 unsigned long long id
= get_unaligned_be64(ses_dev
->page1
+8+4);
382 return sprintf(buf
, "%#llx\n", id
);
385 static void ses_get_power_status(struct enclosure_device
*edev
,
386 struct enclosure_component
*ecomp
)
390 if (!ses_page2_supported(edev
)) {
391 ecomp
->power_status
= 0;
395 desc
= ses_get_page2_descriptor(edev
, ecomp
);
397 ecomp
->power_status
= (desc
[3] & 0x10) ? 0 : 1;
400 static int ses_set_power_status(struct enclosure_device
*edev
,
401 struct enclosure_component
*ecomp
,
404 unsigned char desc
[4];
405 unsigned char *desc_ptr
;
407 if (!ses_page2_supported(edev
))
410 desc_ptr
= ses_get_page2_descriptor(edev
, ecomp
);
415 init_device_slot_control(desc
, ecomp
, desc_ptr
);
418 /* power = 1 is device_off = 0 and vice versa */
428 ecomp
->power_status
= val
;
429 return ses_set_page2_descriptor(edev
, ecomp
, desc
);
432 static struct enclosure_component_callbacks ses_enclosure_callbacks
= {
433 .get_fault
= ses_get_fault
,
434 .set_fault
= ses_set_fault
,
435 .get_status
= ses_get_status
,
436 .get_locate
= ses_get_locate
,
437 .set_locate
= ses_set_locate
,
438 .get_power_status
= ses_get_power_status
,
439 .set_power_status
= ses_set_power_status
,
440 .set_active
= ses_set_active
,
441 .show_id
= ses_show_id
,
444 struct ses_host_edev
{
445 struct Scsi_Host
*shost
;
446 struct enclosure_device
*edev
;
450 int ses_match_host(struct enclosure_device
*edev
, void *data
)
452 struct ses_host_edev
*sed
= data
;
453 struct scsi_device
*sdev
;
455 if (!scsi_is_sdev_device(edev
->edev
.parent
))
458 sdev
= to_scsi_device(edev
->edev
.parent
);
460 if (sdev
->host
!= sed
->shost
)
468 static int ses_process_descriptor(struct enclosure_component
*ecomp
,
469 unsigned char *desc
, int max_desc_len
)
471 int eip
= desc
[0] & 0x10;
472 int invalid
= desc
[0] & 0x80;
473 enum scsi_protocol proto
= desc
[0] & 0x0f;
476 struct ses_component
*scomp
= ecomp
->scratch
;
483 case SCSI_PROTOCOL_FCP
:
485 if (max_desc_len
<= 7)
491 case SCSI_PROTOCOL_SAS
:
494 if (max_desc_len
<= 27)
500 if (max_desc_len
<= 23)
506 /* only take the phy0 addr */
507 addr
= (u64
)d
[12] << 56 |
517 /* FIXME: Need to add more protocols than just SAS */
531 static int ses_enclosure_find_by_addr(struct enclosure_device
*edev
,
534 struct efd
*efd
= data
;
536 struct ses_component
*scomp
;
538 for (i
= 0; i
< edev
->components
; i
++) {
539 scomp
= edev
->component
[i
].scratch
;
540 if (scomp
->addr
!= efd
->addr
)
543 if (enclosure_add_device(edev
, i
, efd
->dev
) == 0)
544 kobject_uevent(&efd
->dev
->kobj
, KOBJ_CHANGE
);
550 #define INIT_ALLOC_SIZE 32
552 static void ses_enclosure_data_process(struct enclosure_device
*edev
,
553 struct scsi_device
*sdev
,
557 unsigned char *buf
= NULL
, *type_ptr
, *desc_ptr
, *addl_desc_ptr
= NULL
;
558 int i
, j
, page7_len
, len
, components
;
559 struct ses_device
*ses_dev
= edev
->scratch
;
560 int types
= ses_dev
->page1_num_types
;
561 unsigned char *hdr_buf
= kzalloc(INIT_ALLOC_SIZE
, GFP_KERNEL
);
564 goto simple_populate
;
566 /* re-read page 10 */
568 ses_recv_diag(sdev
, 10, ses_dev
->page10
, ses_dev
->page10_len
);
569 /* Page 7 for the descriptors is optional */
570 result
= ses_recv_diag(sdev
, 7, hdr_buf
, INIT_ALLOC_SIZE
);
572 goto simple_populate
;
574 page7_len
= len
= (hdr_buf
[2] << 8) + hdr_buf
[3] + 4;
575 /* add 1 for trailing '\0' we'll use */
576 buf
= kzalloc(len
+ 1, GFP_KERNEL
);
578 goto simple_populate
;
579 result
= ses_recv_diag(sdev
, 7, buf
, len
);
589 len
= (desc_ptr
[2] << 8) + desc_ptr
[3];
590 /* skip past overall descriptor */
593 if (ses_dev
->page10
&& ses_dev
->page10_len
> 9)
594 addl_desc_ptr
= ses_dev
->page10
+ 8;
595 type_ptr
= ses_dev
->page1_types
;
597 for (i
= 0; i
< types
; i
++, type_ptr
+= 4) {
598 for (j
= 0; j
< type_ptr
[1]; j
++) {
600 struct enclosure_component
*ecomp
;
604 if (desc_ptr
+ 3 >= buf
+ page7_len
) {
607 len
= (desc_ptr
[2] << 8) + desc_ptr
[3];
609 if (desc_ptr
+ len
> buf
+ page7_len
)
612 /* Add trailing zero - pushes into
614 desc_ptr
[len
] = '\0';
619 if (type_ptr
[0] == ENCLOSURE_COMPONENT_DEVICE
||
620 type_ptr
[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE
) {
623 ecomp
= enclosure_component_alloc(
628 else if (components
< edev
->components
)
629 ecomp
= &edev
->component
[components
++];
631 ecomp
= ERR_PTR(-EINVAL
);
633 if (!IS_ERR(ecomp
)) {
635 max_desc_len
= ses_dev
->page10_len
-
636 (addl_desc_ptr
- ses_dev
->page10
);
637 if (ses_process_descriptor(ecomp
,
640 addl_desc_ptr
= NULL
;
643 enclosure_component_register(
651 /* only find additional descriptions for specific devices */
652 (type_ptr
[0] == ENCLOSURE_COMPONENT_DEVICE
||
653 type_ptr
[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE
||
654 type_ptr
[0] == ENCLOSURE_COMPONENT_SAS_EXPANDER
||
655 /* these elements are optional */
656 type_ptr
[0] == ENCLOSURE_COMPONENT_SCSI_TARGET_PORT
||
657 type_ptr
[0] == ENCLOSURE_COMPONENT_SCSI_INITIATOR_PORT
||
658 type_ptr
[0] == ENCLOSURE_COMPONENT_CONTROLLER_ELECTRONICS
)) {
659 addl_desc_ptr
+= addl_desc_ptr
[1] + 2;
660 if (addl_desc_ptr
+ 1 >= ses_dev
->page10
+ ses_dev
->page10_len
)
661 addl_desc_ptr
= NULL
;
669 static void ses_match_to_enclosure(struct enclosure_device
*edev
,
670 struct scsi_device
*sdev
,
673 struct scsi_device
*edev_sdev
= to_scsi_device(edev
->edev
.parent
);
679 ses_enclosure_data_process(edev
, edev_sdev
, 0);
681 if (scsi_is_sas_rphy(sdev
->sdev_target
->dev
.parent
))
682 efd
.addr
= sas_get_address(sdev
);
685 efd
.dev
= &sdev
->sdev_gendev
;
687 enclosure_for_each_device(ses_enclosure_find_by_addr
, &efd
);
691 static int ses_intf_add(struct device
*cdev
)
693 struct scsi_device
*sdev
= to_scsi_device(cdev
->parent
);
694 struct scsi_device
*tmp_sdev
;
695 unsigned char *buf
= NULL
, *hdr_buf
, *type_ptr
, page
;
696 struct ses_device
*ses_dev
;
698 int i
, types
, len
, components
= 0;
701 struct enclosure_device
*edev
;
702 struct ses_component
*scomp
= NULL
;
704 if (!scsi_device_enclosure(sdev
)) {
705 /* not an enclosure, but might be in one */
706 struct enclosure_device
*prev
= NULL
;
708 while ((edev
= enclosure_find(&sdev
->host
->shost_gendev
, prev
)) != NULL
) {
709 ses_match_to_enclosure(edev
, sdev
, 1);
715 /* TYPE_ENCLOSURE prints a message in probe */
716 if (sdev
->type
!= TYPE_ENCLOSURE
)
717 sdev_printk(KERN_NOTICE
, sdev
, "Embedded Enclosure Device\n");
719 ses_dev
= kzalloc(sizeof(*ses_dev
), GFP_KERNEL
);
720 hdr_buf
= kzalloc(INIT_ALLOC_SIZE
, GFP_KERNEL
);
721 if (!hdr_buf
|| !ses_dev
)
725 result
= ses_recv_diag(sdev
, page
, hdr_buf
, INIT_ALLOC_SIZE
);
729 len
= (hdr_buf
[2] << 8) + hdr_buf
[3] + 4;
730 buf
= kzalloc(len
, GFP_KERNEL
);
734 result
= ses_recv_diag(sdev
, page
, buf
, len
);
740 /* we always have one main enclosure and the rest are referred
741 * to as secondary subenclosures */
742 num_enclosures
= buf
[1] + 1;
744 /* begin at the enclosure descriptor */
746 /* skip all the enclosure descriptors */
747 for (i
= 0; i
< num_enclosures
&& type_ptr
< buf
+ len
; i
++) {
748 types
+= type_ptr
[2];
749 type_ptr
+= type_ptr
[3] + 4;
752 ses_dev
->page1_types
= type_ptr
;
753 ses_dev
->page1_num_types
= types
;
755 for (i
= 0; i
< types
&& type_ptr
< buf
+ len
; i
++, type_ptr
+= 4) {
756 if (type_ptr
[0] == ENCLOSURE_COMPONENT_DEVICE
||
757 type_ptr
[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE
)
758 components
+= type_ptr
[1];
761 ses_dev
->page1
= buf
;
762 ses_dev
->page1_len
= len
;
766 result
= ses_recv_diag(sdev
, page
, hdr_buf
, INIT_ALLOC_SIZE
);
768 goto page2_not_supported
;
770 len
= (hdr_buf
[2] << 8) + hdr_buf
[3] + 4;
771 buf
= kzalloc(len
, GFP_KERNEL
);
775 /* make sure getting page 2 actually works */
776 result
= ses_recv_diag(sdev
, 2, buf
, len
);
779 ses_dev
->page2
= buf
;
780 ses_dev
->page2_len
= len
;
783 /* The additional information page --- allows us
784 * to match up the devices */
786 result
= ses_recv_diag(sdev
, page
, hdr_buf
, INIT_ALLOC_SIZE
);
789 len
= (hdr_buf
[2] << 8) + hdr_buf
[3] + 4;
790 buf
= kzalloc(len
, GFP_KERNEL
);
794 result
= ses_recv_diag(sdev
, page
, buf
, len
);
797 ses_dev
->page10
= buf
;
798 ses_dev
->page10_len
= len
;
802 if (components
> 0) {
803 scomp
= kcalloc(components
, sizeof(struct ses_component
), GFP_KERNEL
);
808 edev
= enclosure_register(cdev
->parent
, dev_name(&sdev
->sdev_gendev
),
809 components
, &ses_enclosure_callbacks
);
817 edev
->scratch
= ses_dev
;
818 for (i
= 0; i
< components
; i
++)
819 edev
->component
[i
].scratch
= scomp
+ i
;
821 ses_enclosure_data_process(edev
, sdev
, 1);
823 /* see if there are any devices matching before
824 * we found the enclosure */
825 shost_for_each_device(tmp_sdev
, sdev
->host
) {
826 if (tmp_sdev
->lun
!= 0 || scsi_device_enclosure(tmp_sdev
))
828 ses_match_to_enclosure(edev
, tmp_sdev
, 0);
834 sdev_printk(KERN_ERR
, sdev
, "Failed to get diagnostic page 0x%x\n",
840 kfree(ses_dev
->page10
);
841 kfree(ses_dev
->page2
);
842 kfree(ses_dev
->page1
);
846 sdev_printk(KERN_ERR
, sdev
, "Failed to bind enclosure %d\n", err
);
850 static int ses_remove(struct device
*dev
)
855 static void ses_intf_remove_component(struct scsi_device
*sdev
)
857 struct enclosure_device
*edev
, *prev
= NULL
;
859 while ((edev
= enclosure_find(&sdev
->host
->shost_gendev
, prev
)) != NULL
) {
861 if (!enclosure_remove_device(edev
, &sdev
->sdev_gendev
))
865 put_device(&edev
->edev
);
868 static void ses_intf_remove_enclosure(struct scsi_device
*sdev
)
870 struct enclosure_device
*edev
;
871 struct ses_device
*ses_dev
;
873 /* exact match to this enclosure */
874 edev
= enclosure_find(&sdev
->sdev_gendev
, NULL
);
878 ses_dev
= edev
->scratch
;
879 edev
->scratch
= NULL
;
881 kfree(ses_dev
->page10
);
882 kfree(ses_dev
->page1
);
883 kfree(ses_dev
->page2
);
886 if (edev
->components
)
887 kfree(edev
->component
[0].scratch
);
889 put_device(&edev
->edev
);
890 enclosure_unregister(edev
);
893 static void ses_intf_remove(struct device
*cdev
)
895 struct scsi_device
*sdev
= to_scsi_device(cdev
->parent
);
897 if (!scsi_device_enclosure(sdev
))
898 ses_intf_remove_component(sdev
);
900 ses_intf_remove_enclosure(sdev
);
903 static struct class_interface ses_interface
= {
904 .add_dev
= ses_intf_add
,
905 .remove_dev
= ses_intf_remove
,
908 static struct scsi_driver ses_template
= {
912 .remove
= ses_remove
,
916 static int __init
ses_init(void)
920 err
= scsi_register_interface(&ses_interface
);
924 err
= scsi_register_driver(&ses_template
.gendrv
);
931 scsi_unregister_interface(&ses_interface
);
935 static void __exit
ses_exit(void)
937 scsi_unregister_driver(&ses_template
.gendrv
);
938 scsi_unregister_interface(&ses_interface
);
941 module_init(ses_init
);
942 module_exit(ses_exit
);
944 MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE
);
946 MODULE_AUTHOR("James Bottomley");
947 MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
948 MODULE_LICENSE("GPL v2");