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>
29 #include <scsi/scsi.h>
30 #include <scsi/scsi_cmnd.h>
31 #include <scsi/scsi_dbg.h>
32 #include <scsi/scsi_device.h>
33 #include <scsi/scsi_driver.h>
34 #include <scsi/scsi_host.h>
38 unsigned char *page1_types
;
40 unsigned char *page10
;
42 short page1_num_types
;
47 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 int ses_recv_diag(struct scsi_device
*sdev
, int page_code
,
71 void *buf
, int bufflen
)
73 unsigned char cmd
[] = {
82 return scsi_execute_req(sdev
, cmd
, DMA_FROM_DEVICE
, buf
, bufflen
,
83 NULL
, SES_TIMEOUT
, SES_RETRIES
, NULL
);
86 static int ses_send_diag(struct scsi_device
*sdev
, int page_code
,
87 void *buf
, int bufflen
)
91 unsigned char cmd
[] = {
93 0x10, /* Set PF bit */
100 result
= scsi_execute_req(sdev
, cmd
, DMA_TO_DEVICE
, buf
, bufflen
,
101 NULL
, SES_TIMEOUT
, SES_RETRIES
, NULL
);
103 sdev_printk(KERN_ERR
, sdev
, "SEND DIAGNOSTIC result: %8x\n",
108 static int ses_set_page2_descriptor(struct enclosure_device
*edev
,
109 struct enclosure_component
*ecomp
,
112 int i
, j
, count
= 0, descriptor
= ecomp
->number
;
113 struct scsi_device
*sdev
= to_scsi_device(edev
->edev
.parent
);
114 struct ses_device
*ses_dev
= edev
->scratch
;
115 unsigned char *type_ptr
= ses_dev
->page1_types
;
116 unsigned char *desc_ptr
= ses_dev
->page2
+ 8;
118 /* Clear everything */
119 memset(desc_ptr
, 0, ses_dev
->page2_len
- 8);
120 for (i
= 0; i
< ses_dev
->page1_num_types
; i
++, type_ptr
+= 4) {
121 for (j
= 0; j
< type_ptr
[1]; j
++) {
123 if (type_ptr
[0] != ENCLOSURE_COMPONENT_DEVICE
&&
124 type_ptr
[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE
)
126 if (count
++ == descriptor
) {
127 memcpy(desc_ptr
, desc
, 4);
130 /* clear reserved, just in case */
136 return ses_send_diag(sdev
, 2, ses_dev
->page2
, ses_dev
->page2_len
);
139 static unsigned char *ses_get_page2_descriptor(struct enclosure_device
*edev
,
140 struct enclosure_component
*ecomp
)
142 int i
, j
, count
= 0, descriptor
= ecomp
->number
;
143 struct scsi_device
*sdev
= to_scsi_device(edev
->edev
.parent
);
144 struct ses_device
*ses_dev
= edev
->scratch
;
145 unsigned char *type_ptr
= ses_dev
->page1_types
;
146 unsigned char *desc_ptr
= ses_dev
->page2
+ 8;
148 ses_recv_diag(sdev
, 2, ses_dev
->page2
, ses_dev
->page2_len
);
150 for (i
= 0; i
< ses_dev
->page1_num_types
; i
++, type_ptr
+= 4) {
151 for (j
= 0; j
< type_ptr
[1]; j
++) {
153 if (type_ptr
[0] != ENCLOSURE_COMPONENT_DEVICE
&&
154 type_ptr
[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE
)
156 if (count
++ == descriptor
)
163 static void ses_get_fault(struct enclosure_device
*edev
,
164 struct enclosure_component
*ecomp
)
168 desc
= ses_get_page2_descriptor(edev
, ecomp
);
170 ecomp
->fault
= (desc
[3] & 0x60) >> 4;
173 static int ses_set_fault(struct enclosure_device
*edev
,
174 struct enclosure_component
*ecomp
,
175 enum enclosure_component_setting val
)
177 unsigned char desc
[4] = {0 };
180 case ENCLOSURE_SETTING_DISABLED
:
181 /* zero is disabled */
183 case ENCLOSURE_SETTING_ENABLED
:
187 /* SES doesn't do the SGPIO blink settings */
191 return ses_set_page2_descriptor(edev
, ecomp
, desc
);
194 static void ses_get_status(struct enclosure_device
*edev
,
195 struct enclosure_component
*ecomp
)
199 desc
= ses_get_page2_descriptor(edev
, ecomp
);
201 ecomp
->status
= (desc
[0] & 0x0f);
204 static void ses_get_locate(struct enclosure_device
*edev
,
205 struct enclosure_component
*ecomp
)
209 desc
= ses_get_page2_descriptor(edev
, ecomp
);
211 ecomp
->locate
= (desc
[2] & 0x02) ? 1 : 0;
214 static int ses_set_locate(struct enclosure_device
*edev
,
215 struct enclosure_component
*ecomp
,
216 enum enclosure_component_setting val
)
218 unsigned char desc
[4] = {0 };
221 case ENCLOSURE_SETTING_DISABLED
:
222 /* zero is disabled */
224 case ENCLOSURE_SETTING_ENABLED
:
228 /* SES doesn't do the SGPIO blink settings */
231 return ses_set_page2_descriptor(edev
, ecomp
, desc
);
234 static int ses_set_active(struct enclosure_device
*edev
,
235 struct enclosure_component
*ecomp
,
236 enum enclosure_component_setting val
)
238 unsigned char desc
[4] = {0 };
241 case ENCLOSURE_SETTING_DISABLED
:
242 /* zero is disabled */
245 case ENCLOSURE_SETTING_ENABLED
:
250 /* SES doesn't do the SGPIO blink settings */
253 return ses_set_page2_descriptor(edev
, ecomp
, desc
);
256 static struct enclosure_component_callbacks ses_enclosure_callbacks
= {
257 .get_fault
= ses_get_fault
,
258 .set_fault
= ses_set_fault
,
259 .get_status
= ses_get_status
,
260 .get_locate
= ses_get_locate
,
261 .set_locate
= ses_set_locate
,
262 .set_active
= ses_set_active
,
265 struct ses_host_edev
{
266 struct Scsi_Host
*shost
;
267 struct enclosure_device
*edev
;
271 int ses_match_host(struct enclosure_device
*edev
, void *data
)
273 struct ses_host_edev
*sed
= data
;
274 struct scsi_device
*sdev
;
276 if (!scsi_is_sdev_device(edev
->edev
.parent
))
279 sdev
= to_scsi_device(edev
->edev
.parent
);
281 if (sdev
->host
!= sed
->shost
)
289 static void ses_process_descriptor(struct enclosure_component
*ecomp
,
292 int eip
= desc
[0] & 0x10;
293 int invalid
= desc
[0] & 0x80;
294 enum scsi_protocol proto
= desc
[0] & 0x0f;
296 struct ses_component
*scomp
= ecomp
->scratch
;
305 case SCSI_PROTOCOL_SAS
:
310 /* only take the phy0 addr */
311 addr
= (u64
)d
[12] << 56 |
321 /* FIXME: Need to add more protocols than just SAS */
332 static int ses_enclosure_find_by_addr(struct enclosure_device
*edev
,
335 struct efd
*efd
= data
;
337 struct ses_component
*scomp
;
339 if (!edev
->component
[0].scratch
)
342 for (i
= 0; i
< edev
->components
; i
++) {
343 scomp
= edev
->component
[i
].scratch
;
344 if (scomp
->addr
!= efd
->addr
)
347 enclosure_add_device(edev
, i
, efd
->dev
);
353 #define INIT_ALLOC_SIZE 32
355 static void ses_enclosure_data_process(struct enclosure_device
*edev
,
356 struct scsi_device
*sdev
,
360 unsigned char *buf
= NULL
, *type_ptr
, *desc_ptr
, *addl_desc_ptr
= NULL
;
361 int i
, j
, page7_len
, len
, components
;
362 struct ses_device
*ses_dev
= edev
->scratch
;
363 int types
= ses_dev
->page1_num_types
;
364 unsigned char *hdr_buf
= kzalloc(INIT_ALLOC_SIZE
, GFP_KERNEL
);
367 goto simple_populate
;
369 /* re-read page 10 */
371 ses_recv_diag(sdev
, 10, ses_dev
->page10
, ses_dev
->page10_len
);
372 /* Page 7 for the descriptors is optional */
373 result
= ses_recv_diag(sdev
, 7, hdr_buf
, INIT_ALLOC_SIZE
);
375 goto simple_populate
;
377 page7_len
= len
= (hdr_buf
[2] << 8) + hdr_buf
[3] + 4;
378 /* add 1 for trailing '\0' we'll use */
379 buf
= kzalloc(len
+ 1, GFP_KERNEL
);
381 goto simple_populate
;
382 result
= ses_recv_diag(sdev
, 7, buf
, len
);
392 len
= (desc_ptr
[2] << 8) + desc_ptr
[3];
393 /* skip past overall descriptor */
397 addl_desc_ptr
= ses_dev
->page10
+ 8;
398 type_ptr
= ses_dev
->page1_types
;
400 for (i
= 0; i
< types
; i
++, type_ptr
+= 4) {
401 for (j
= 0; j
< type_ptr
[1]; j
++) {
403 struct enclosure_component
*ecomp
;
406 if (desc_ptr
>= buf
+ page7_len
) {
409 len
= (desc_ptr
[2] << 8) + desc_ptr
[3];
411 /* Add trailing zero - pushes into
413 desc_ptr
[len
] = '\0';
417 if (type_ptr
[0] == ENCLOSURE_COMPONENT_DEVICE
||
418 type_ptr
[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE
) {
421 ecomp
= enclosure_component_register(edev
,
426 ecomp
= &edev
->component
[components
++];
428 if (!IS_ERR(ecomp
) && addl_desc_ptr
)
429 ses_process_descriptor(ecomp
,
436 addl_desc_ptr
+= addl_desc_ptr
[1] + 2;
444 static void ses_match_to_enclosure(struct enclosure_device
*edev
,
445 struct scsi_device
*sdev
)
449 unsigned int vpd_len
;
454 buf
= kmalloc(INIT_ALLOC_SIZE
, GFP_KERNEL
);
455 if (!buf
|| scsi_get_vpd_page(sdev
, 0x83, buf
, INIT_ALLOC_SIZE
))
458 ses_enclosure_data_process(edev
, to_scsi_device(edev
->edev
.parent
), 0);
460 vpd_len
= ((buf
[2] << 8) | buf
[3]) + 4;
462 buf
= kmalloc(vpd_len
, GFP_KERNEL
);
463 if (!buf
||scsi_get_vpd_page(sdev
, 0x83, buf
, vpd_len
))
467 while (desc
< buf
+ vpd_len
) {
468 enum scsi_protocol proto
= desc
[0] >> 4;
469 u8 code_set
= desc
[0] & 0x0f;
470 u8 piv
= desc
[1] & 0x80;
471 u8 assoc
= (desc
[1] & 0x30) >> 4;
472 u8 type
= desc
[1] & 0x0f;
475 if (piv
&& code_set
== 1 && assoc
== 1
476 && proto
== SCSI_PROTOCOL_SAS
&& type
== 3 && len
== 8)
477 efd
.addr
= (u64
)desc
[4] << 56 |
491 efd
.dev
= &sdev
->sdev_gendev
;
493 enclosure_for_each_device(ses_enclosure_find_by_addr
, &efd
);
498 static int ses_intf_add(struct device
*cdev
,
499 struct class_interface
*intf
)
501 struct scsi_device
*sdev
= to_scsi_device(cdev
->parent
);
502 struct scsi_device
*tmp_sdev
;
503 unsigned char *buf
= NULL
, *hdr_buf
, *type_ptr
;
504 struct ses_device
*ses_dev
;
506 int i
, types
, len
, components
= 0;
509 struct enclosure_device
*edev
;
510 struct ses_component
*scomp
= NULL
;
512 if (!scsi_device_enclosure(sdev
)) {
513 /* not an enclosure, but might be in one */
514 struct enclosure_device
*prev
= NULL
;
516 while ((edev
= enclosure_find(&sdev
->host
->shost_gendev
, prev
)) != NULL
) {
517 ses_match_to_enclosure(edev
, sdev
);
523 /* TYPE_ENCLOSURE prints a message in probe */
524 if (sdev
->type
!= TYPE_ENCLOSURE
)
525 sdev_printk(KERN_NOTICE
, sdev
, "Embedded Enclosure Device\n");
527 ses_dev
= kzalloc(sizeof(*ses_dev
), GFP_KERNEL
);
528 hdr_buf
= kzalloc(INIT_ALLOC_SIZE
, GFP_KERNEL
);
529 if (!hdr_buf
|| !ses_dev
)
532 result
= ses_recv_diag(sdev
, 1, hdr_buf
, INIT_ALLOC_SIZE
);
536 len
= (hdr_buf
[2] << 8) + hdr_buf
[3] + 4;
537 buf
= kzalloc(len
, GFP_KERNEL
);
541 result
= ses_recv_diag(sdev
, 1, buf
, len
);
547 /* we always have one main enclosure and the rest are referred
548 * to as secondary subenclosures */
549 num_enclosures
= buf
[1] + 1;
551 /* begin at the enclosure descriptor */
553 /* skip all the enclosure descriptors */
554 for (i
= 0; i
< num_enclosures
&& type_ptr
< buf
+ len
; i
++) {
555 types
+= type_ptr
[2];
556 type_ptr
+= type_ptr
[3] + 4;
559 ses_dev
->page1_types
= type_ptr
;
560 ses_dev
->page1_num_types
= types
;
562 for (i
= 0; i
< types
&& type_ptr
< buf
+ len
; i
++, type_ptr
+= 4) {
563 if (type_ptr
[0] == ENCLOSURE_COMPONENT_DEVICE
||
564 type_ptr
[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE
)
565 components
+= type_ptr
[1];
567 ses_dev
->page1
= buf
;
568 ses_dev
->page1_len
= len
;
571 result
= ses_recv_diag(sdev
, 2, hdr_buf
, INIT_ALLOC_SIZE
);
575 len
= (hdr_buf
[2] << 8) + hdr_buf
[3] + 4;
576 buf
= kzalloc(len
, GFP_KERNEL
);
580 /* make sure getting page 2 actually works */
581 result
= ses_recv_diag(sdev
, 2, buf
, len
);
584 ses_dev
->page2
= buf
;
585 ses_dev
->page2_len
= len
;
588 /* The additional information page --- allows us
589 * to match up the devices */
590 result
= ses_recv_diag(sdev
, 10, hdr_buf
, INIT_ALLOC_SIZE
);
593 len
= (hdr_buf
[2] << 8) + hdr_buf
[3] + 4;
594 buf
= kzalloc(len
, GFP_KERNEL
);
598 result
= ses_recv_diag(sdev
, 10, buf
, len
);
601 ses_dev
->page10
= buf
;
602 ses_dev
->page10_len
= len
;
605 scomp
= kzalloc(sizeof(struct ses_component
) * components
, GFP_KERNEL
);
609 edev
= enclosure_register(cdev
->parent
, dev_name(&sdev
->sdev_gendev
),
610 components
, &ses_enclosure_callbacks
);
618 edev
->scratch
= ses_dev
;
619 for (i
= 0; i
< components
; i
++)
620 edev
->component
[i
].scratch
= scomp
+ i
;
622 ses_enclosure_data_process(edev
, sdev
, 1);
624 /* see if there are any devices matching before
625 * we found the enclosure */
626 shost_for_each_device(tmp_sdev
, sdev
->host
) {
627 if (tmp_sdev
->lun
!= 0 || scsi_device_enclosure(tmp_sdev
))
629 ses_match_to_enclosure(edev
, tmp_sdev
);
635 sdev_printk(KERN_ERR
, sdev
, "Failed to get diagnostic page 0x%x\n",
641 kfree(ses_dev
->page10
);
642 kfree(ses_dev
->page2
);
643 kfree(ses_dev
->page1
);
647 sdev_printk(KERN_ERR
, sdev
, "Failed to bind enclosure %d\n", err
);
651 static int ses_remove(struct device
*dev
)
656 static void ses_intf_remove_component(struct scsi_device
*sdev
)
658 struct enclosure_device
*edev
, *prev
= NULL
;
660 while ((edev
= enclosure_find(&sdev
->host
->shost_gendev
, prev
)) != NULL
) {
662 if (!enclosure_remove_device(edev
, &sdev
->sdev_gendev
))
666 put_device(&edev
->edev
);
669 static void ses_intf_remove_enclosure(struct scsi_device
*sdev
)
671 struct enclosure_device
*edev
;
672 struct ses_device
*ses_dev
;
674 /* exact match to this enclosure */
675 edev
= enclosure_find(&sdev
->sdev_gendev
, NULL
);
679 ses_dev
= edev
->scratch
;
680 edev
->scratch
= NULL
;
682 kfree(ses_dev
->page10
);
683 kfree(ses_dev
->page1
);
684 kfree(ses_dev
->page2
);
687 kfree(edev
->component
[0].scratch
);
689 put_device(&edev
->edev
);
690 enclosure_unregister(edev
);
693 static void ses_intf_remove(struct device
*cdev
,
694 struct class_interface
*intf
)
696 struct scsi_device
*sdev
= to_scsi_device(cdev
->parent
);
698 if (!scsi_device_enclosure(sdev
))
699 ses_intf_remove_component(sdev
);
701 ses_intf_remove_enclosure(sdev
);
704 static struct class_interface ses_interface
= {
705 .add_dev
= ses_intf_add
,
706 .remove_dev
= ses_intf_remove
,
709 static struct scsi_driver ses_template
= {
710 .owner
= THIS_MODULE
,
714 .remove
= ses_remove
,
718 static int __init
ses_init(void)
722 err
= scsi_register_interface(&ses_interface
);
726 err
= scsi_register_driver(&ses_template
.gendrv
);
733 scsi_unregister_interface(&ses_interface
);
737 static void __exit
ses_exit(void)
739 scsi_unregister_driver(&ses_template
.gendrv
);
740 scsi_unregister_interface(&ses_interface
);
743 module_init(ses_init
);
744 module_exit(ses_exit
);
746 MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE
);
748 MODULE_AUTHOR("James Bottomley");
749 MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
750 MODULE_LICENSE("GPL v2");