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>
39 unsigned char *page10
;
45 struct ses_component
{
50 static int ses_probe(struct device
*dev
)
52 struct scsi_device
*sdev
= to_scsi_device(dev
);
55 if (sdev
->type
!= TYPE_ENCLOSURE
)
59 sdev_printk(KERN_NOTICE
, sdev
, "Attached Enclosure device\n");
65 #define SES_TIMEOUT (30 * HZ)
68 static int ses_recv_diag(struct scsi_device
*sdev
, int page_code
,
69 void *buf
, int bufflen
)
71 unsigned char cmd
[] = {
80 return scsi_execute_req(sdev
, cmd
, DMA_FROM_DEVICE
, buf
, bufflen
,
81 NULL
, SES_TIMEOUT
, SES_RETRIES
, NULL
);
84 static int ses_send_diag(struct scsi_device
*sdev
, int page_code
,
85 void *buf
, int bufflen
)
89 unsigned char cmd
[] = {
91 0x10, /* Set PF bit */
98 result
= scsi_execute_req(sdev
, cmd
, DMA_TO_DEVICE
, buf
, bufflen
,
99 NULL
, SES_TIMEOUT
, SES_RETRIES
, NULL
);
101 sdev_printk(KERN_ERR
, sdev
, "SEND DIAGNOSTIC result: %8x\n",
106 static int ses_set_page2_descriptor(struct enclosure_device
*edev
,
107 struct enclosure_component
*ecomp
,
110 int i
, j
, count
= 0, descriptor
= ecomp
->number
;
111 struct scsi_device
*sdev
= to_scsi_device(edev
->edev
.parent
);
112 struct ses_device
*ses_dev
= edev
->scratch
;
113 unsigned char *type_ptr
= ses_dev
->page1
+ 12 + ses_dev
->page1
[11];
114 unsigned char *desc_ptr
= ses_dev
->page2
+ 8;
116 /* Clear everything */
117 memset(desc_ptr
, 0, ses_dev
->page2_len
- 8);
118 for (i
= 0; i
< ses_dev
->page1
[10]; i
++, type_ptr
+= 4) {
119 for (j
= 0; j
< type_ptr
[1]; j
++) {
121 if (type_ptr
[0] != ENCLOSURE_COMPONENT_DEVICE
&&
122 type_ptr
[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE
)
124 if (count
++ == descriptor
) {
125 memcpy(desc_ptr
, desc
, 4);
128 /* clear reserved, just in case */
134 return ses_send_diag(sdev
, 2, ses_dev
->page2
, ses_dev
->page2_len
);
137 static unsigned char *ses_get_page2_descriptor(struct enclosure_device
*edev
,
138 struct enclosure_component
*ecomp
)
140 int i
, j
, count
= 0, descriptor
= ecomp
->number
;
141 struct scsi_device
*sdev
= to_scsi_device(edev
->edev
.parent
);
142 struct ses_device
*ses_dev
= edev
->scratch
;
143 unsigned char *type_ptr
= ses_dev
->page1
+ 12 + ses_dev
->page1
[11];
144 unsigned char *desc_ptr
= ses_dev
->page2
+ 8;
146 ses_recv_diag(sdev
, 2, ses_dev
->page2
, ses_dev
->page2_len
);
148 for (i
= 0; i
< ses_dev
->page1
[10]; i
++, type_ptr
+= 4) {
149 for (j
= 0; j
< type_ptr
[1]; j
++) {
151 if (type_ptr
[0] != ENCLOSURE_COMPONENT_DEVICE
&&
152 type_ptr
[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE
)
154 if (count
++ == descriptor
)
161 static void ses_get_fault(struct enclosure_device
*edev
,
162 struct enclosure_component
*ecomp
)
166 desc
= ses_get_page2_descriptor(edev
, ecomp
);
168 ecomp
->fault
= (desc
[3] & 0x60) >> 4;
171 static int ses_set_fault(struct enclosure_device
*edev
,
172 struct enclosure_component
*ecomp
,
173 enum enclosure_component_setting val
)
175 unsigned char desc
[4] = {0 };
178 case ENCLOSURE_SETTING_DISABLED
:
179 /* zero is disabled */
181 case ENCLOSURE_SETTING_ENABLED
:
185 /* SES doesn't do the SGPIO blink settings */
189 return ses_set_page2_descriptor(edev
, ecomp
, desc
);
192 static void ses_get_status(struct enclosure_device
*edev
,
193 struct enclosure_component
*ecomp
)
197 desc
= ses_get_page2_descriptor(edev
, ecomp
);
199 ecomp
->status
= (desc
[0] & 0x0f);
202 static void ses_get_locate(struct enclosure_device
*edev
,
203 struct enclosure_component
*ecomp
)
207 desc
= ses_get_page2_descriptor(edev
, ecomp
);
209 ecomp
->locate
= (desc
[2] & 0x02) ? 1 : 0;
212 static int ses_set_locate(struct enclosure_device
*edev
,
213 struct enclosure_component
*ecomp
,
214 enum enclosure_component_setting val
)
216 unsigned char desc
[4] = {0 };
219 case ENCLOSURE_SETTING_DISABLED
:
220 /* zero is disabled */
222 case ENCLOSURE_SETTING_ENABLED
:
226 /* SES doesn't do the SGPIO blink settings */
229 return ses_set_page2_descriptor(edev
, ecomp
, desc
);
232 static int ses_set_active(struct enclosure_device
*edev
,
233 struct enclosure_component
*ecomp
,
234 enum enclosure_component_setting val
)
236 unsigned char desc
[4] = {0 };
239 case ENCLOSURE_SETTING_DISABLED
:
240 /* zero is disabled */
243 case ENCLOSURE_SETTING_ENABLED
:
248 /* SES doesn't do the SGPIO blink settings */
251 return ses_set_page2_descriptor(edev
, ecomp
, desc
);
254 static struct enclosure_component_callbacks ses_enclosure_callbacks
= {
255 .get_fault
= ses_get_fault
,
256 .set_fault
= ses_set_fault
,
257 .get_status
= ses_get_status
,
258 .get_locate
= ses_get_locate
,
259 .set_locate
= ses_set_locate
,
260 .set_active
= ses_set_active
,
263 struct ses_host_edev
{
264 struct Scsi_Host
*shost
;
265 struct enclosure_device
*edev
;
269 int ses_match_host(struct enclosure_device
*edev
, void *data
)
271 struct ses_host_edev
*sed
= data
;
272 struct scsi_device
*sdev
;
274 if (!scsi_is_sdev_device(edev
->edev
.parent
))
277 sdev
= to_scsi_device(edev
->edev
.parent
);
279 if (sdev
->host
!= sed
->shost
)
287 static void ses_process_descriptor(struct enclosure_component
*ecomp
,
290 int eip
= desc
[0] & 0x10;
291 int invalid
= desc
[0] & 0x80;
292 enum scsi_protocol proto
= desc
[0] & 0x0f;
294 struct ses_component
*scomp
= ecomp
->scratch
;
303 case SCSI_PROTOCOL_SAS
:
308 /* only take the phy0 addr */
309 addr
= (u64
)d
[12] << 56 |
319 /* FIXME: Need to add more protocols than just SAS */
330 static int ses_enclosure_find_by_addr(struct enclosure_device
*edev
,
333 struct efd
*efd
= data
;
335 struct ses_component
*scomp
;
337 if (!edev
->component
[0].scratch
)
340 for (i
= 0; i
< edev
->components
; i
++) {
341 scomp
= edev
->component
[i
].scratch
;
342 if (scomp
->addr
!= efd
->addr
)
345 enclosure_add_device(edev
, i
, efd
->dev
);
351 #define INIT_ALLOC_SIZE 32
353 static void ses_enclosure_data_process(struct enclosure_device
*edev
,
354 struct scsi_device
*sdev
,
358 unsigned char *buf
= NULL
, *type_ptr
, *desc_ptr
, *addl_desc_ptr
= NULL
;
359 int i
, j
, page7_len
, len
, components
;
360 struct ses_device
*ses_dev
= edev
->scratch
;
361 int types
= ses_dev
->page1
[10];
362 unsigned char *hdr_buf
= kzalloc(INIT_ALLOC_SIZE
, GFP_KERNEL
);
365 goto simple_populate
;
367 /* re-read page 10 */
369 ses_recv_diag(sdev
, 10, ses_dev
->page10
, ses_dev
->page10_len
);
370 /* Page 7 for the descriptors is optional */
371 result
= ses_recv_diag(sdev
, 7, hdr_buf
, INIT_ALLOC_SIZE
);
373 goto simple_populate
;
375 page7_len
= len
= (hdr_buf
[2] << 8) + hdr_buf
[3] + 4;
376 /* add 1 for trailing '\0' we'll use */
377 buf
= kzalloc(len
+ 1, GFP_KERNEL
);
379 goto simple_populate
;
380 result
= ses_recv_diag(sdev
, 7, buf
, len
);
390 len
= (desc_ptr
[2] << 8) + desc_ptr
[3];
391 /* skip past overall descriptor */
394 addl_desc_ptr
= ses_dev
->page10
+ 8;
396 type_ptr
= ses_dev
->page1
+ 12 + ses_dev
->page1
[11];
398 for (i
= 0; i
< types
; i
++, type_ptr
+= 4) {
399 for (j
= 0; j
< type_ptr
[1]; j
++) {
401 struct enclosure_component
*ecomp
;
404 if (desc_ptr
>= buf
+ page7_len
) {
407 len
= (desc_ptr
[2] << 8) + desc_ptr
[3];
409 /* Add trailing zero - pushes into
411 desc_ptr
[len
] = '\0';
415 if (type_ptr
[0] == ENCLOSURE_COMPONENT_DEVICE
||
416 type_ptr
[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE
) {
419 ecomp
= enclosure_component_register(edev
,
424 ecomp
= &edev
->component
[components
++];
426 if (!IS_ERR(ecomp
) && addl_desc_ptr
)
427 ses_process_descriptor(ecomp
,
434 addl_desc_ptr
+= addl_desc_ptr
[1] + 2;
442 static void ses_match_to_enclosure(struct enclosure_device
*edev
,
443 struct scsi_device
*sdev
)
447 unsigned int vpd_len
;
452 buf
= kmalloc(INIT_ALLOC_SIZE
, GFP_KERNEL
);
453 if (!buf
|| scsi_get_vpd_page(sdev
, 0x83, buf
, INIT_ALLOC_SIZE
))
456 ses_enclosure_data_process(edev
, to_scsi_device(edev
->edev
.parent
), 0);
458 vpd_len
= ((buf
[2] << 8) | buf
[3]) + 4;
460 buf
= kmalloc(vpd_len
, GFP_KERNEL
);
461 if (!buf
||scsi_get_vpd_page(sdev
, 0x83, buf
, vpd_len
))
465 while (desc
< buf
+ vpd_len
) {
466 enum scsi_protocol proto
= desc
[0] >> 4;
467 u8 code_set
= desc
[0] & 0x0f;
468 u8 piv
= desc
[1] & 0x80;
469 u8 assoc
= (desc
[1] & 0x30) >> 4;
470 u8 type
= desc
[1] & 0x0f;
473 if (piv
&& code_set
== 1 && assoc
== 1
474 && proto
== SCSI_PROTOCOL_SAS
&& type
== 3 && len
== 8)
475 efd
.addr
= (u64
)desc
[4] << 56 |
489 efd
.dev
= &sdev
->sdev_gendev
;
491 enclosure_for_each_device(ses_enclosure_find_by_addr
, &efd
);
496 static int ses_intf_add(struct device
*cdev
,
497 struct class_interface
*intf
)
499 struct scsi_device
*sdev
= to_scsi_device(cdev
->parent
);
500 struct scsi_device
*tmp_sdev
;
501 unsigned char *buf
= NULL
, *hdr_buf
, *type_ptr
;
502 struct ses_device
*ses_dev
;
504 int i
, types
, len
, components
= 0;
506 struct enclosure_device
*edev
;
507 struct ses_component
*scomp
= NULL
;
509 if (!scsi_device_enclosure(sdev
)) {
510 /* not an enclosure, but might be in one */
511 struct enclosure_device
*prev
= NULL
;
513 while ((edev
= enclosure_find(&sdev
->host
->shost_gendev
, prev
)) != NULL
) {
514 ses_match_to_enclosure(edev
, sdev
);
520 /* TYPE_ENCLOSURE prints a message in probe */
521 if (sdev
->type
!= TYPE_ENCLOSURE
)
522 sdev_printk(KERN_NOTICE
, sdev
, "Embedded Enclosure Device\n");
524 ses_dev
= kzalloc(sizeof(*ses_dev
), GFP_KERNEL
);
525 hdr_buf
= kzalloc(INIT_ALLOC_SIZE
, GFP_KERNEL
);
526 if (!hdr_buf
|| !ses_dev
)
529 result
= ses_recv_diag(sdev
, 1, hdr_buf
, INIT_ALLOC_SIZE
);
533 if (hdr_buf
[1] != 0) {
534 /* FIXME: need subenclosure support; I've just never
535 * seen a device with subenclosures and it makes the
536 * traversal routines more complex */
537 sdev_printk(KERN_ERR
, sdev
,
538 "FIXME driver has no support for subenclosures (%d)\n",
543 len
= (hdr_buf
[2] << 8) + hdr_buf
[3] + 4;
544 buf
= kzalloc(len
, GFP_KERNEL
);
548 result
= ses_recv_diag(sdev
, 1, buf
, len
);
554 type_ptr
= buf
+ 12 + buf
[11];
556 for (i
= 0; i
< types
; i
++, type_ptr
+= 4) {
557 if (type_ptr
[0] == ENCLOSURE_COMPONENT_DEVICE
||
558 type_ptr
[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE
)
559 components
+= type_ptr
[1];
561 ses_dev
->page1
= buf
;
562 ses_dev
->page1_len
= len
;
565 result
= ses_recv_diag(sdev
, 2, hdr_buf
, INIT_ALLOC_SIZE
);
569 len
= (hdr_buf
[2] << 8) + hdr_buf
[3] + 4;
570 buf
= kzalloc(len
, GFP_KERNEL
);
574 /* make sure getting page 2 actually works */
575 result
= ses_recv_diag(sdev
, 2, buf
, len
);
578 ses_dev
->page2
= buf
;
579 ses_dev
->page2_len
= len
;
582 /* The additional information page --- allows us
583 * to match up the devices */
584 result
= ses_recv_diag(sdev
, 10, hdr_buf
, INIT_ALLOC_SIZE
);
587 len
= (hdr_buf
[2] << 8) + hdr_buf
[3] + 4;
588 buf
= kzalloc(len
, GFP_KERNEL
);
592 result
= ses_recv_diag(sdev
, 10, buf
, len
);
595 ses_dev
->page10
= buf
;
596 ses_dev
->page10_len
= len
;
599 scomp
= kzalloc(sizeof(struct ses_component
) * components
, GFP_KERNEL
);
603 edev
= enclosure_register(cdev
->parent
, dev_name(&sdev
->sdev_gendev
),
604 components
, &ses_enclosure_callbacks
);
612 edev
->scratch
= ses_dev
;
613 for (i
= 0; i
< components
; i
++)
614 edev
->component
[i
].scratch
= scomp
+ i
;
616 ses_enclosure_data_process(edev
, sdev
, 1);
618 /* see if there are any devices matching before
619 * we found the enclosure */
620 shost_for_each_device(tmp_sdev
, sdev
->host
) {
621 if (tmp_sdev
->lun
!= 0 || scsi_device_enclosure(tmp_sdev
))
623 ses_match_to_enclosure(edev
, tmp_sdev
);
629 sdev_printk(KERN_ERR
, sdev
, "Failed to get diagnostic page 0x%x\n",
635 kfree(ses_dev
->page10
);
636 kfree(ses_dev
->page2
);
637 kfree(ses_dev
->page1
);
641 sdev_printk(KERN_ERR
, sdev
, "Failed to bind enclosure %d\n", err
);
645 static int ses_remove(struct device
*dev
)
650 static void ses_intf_remove_component(struct scsi_device
*sdev
)
652 struct enclosure_device
*edev
, *prev
= NULL
;
654 while ((edev
= enclosure_find(&sdev
->host
->shost_gendev
, prev
)) != NULL
) {
656 if (!enclosure_remove_device(edev
, &sdev
->sdev_gendev
))
660 put_device(&edev
->edev
);
663 static void ses_intf_remove_enclosure(struct scsi_device
*sdev
)
665 struct enclosure_device
*edev
;
666 struct ses_device
*ses_dev
;
668 /* exact match to this enclosure */
669 edev
= enclosure_find(&sdev
->sdev_gendev
, NULL
);
673 ses_dev
= edev
->scratch
;
674 edev
->scratch
= NULL
;
676 kfree(ses_dev
->page10
);
677 kfree(ses_dev
->page1
);
678 kfree(ses_dev
->page2
);
681 kfree(edev
->component
[0].scratch
);
683 put_device(&edev
->edev
);
684 enclosure_unregister(edev
);
687 static void ses_intf_remove(struct device
*cdev
,
688 struct class_interface
*intf
)
690 struct scsi_device
*sdev
= to_scsi_device(cdev
->parent
);
692 if (!scsi_device_enclosure(sdev
))
693 ses_intf_remove_component(sdev
);
695 ses_intf_remove_enclosure(sdev
);
698 static struct class_interface ses_interface
= {
699 .add_dev
= ses_intf_add
,
700 .remove_dev
= ses_intf_remove
,
703 static struct scsi_driver ses_template
= {
704 .owner
= THIS_MODULE
,
708 .remove
= ses_remove
,
712 static int __init
ses_init(void)
716 err
= scsi_register_interface(&ses_interface
);
720 err
= scsi_register_driver(&ses_template
.gendrv
);
727 scsi_unregister_interface(&ses_interface
);
731 static void __exit
ses_exit(void)
733 scsi_unregister_driver(&ses_template
.gendrv
);
734 scsi_unregister_interface(&ses_interface
);
737 module_init(ses_init
);
738 module_exit(ses_exit
);
740 MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE
);
742 MODULE_AUTHOR("James Bottomley");
743 MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
744 MODULE_LICENSE("GPL v2");