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/module.h>
25 #include <linux/kernel.h>
26 #include <linux/enclosure.h>
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_dbg.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_driver.h>
33 #include <scsi/scsi_host.h>
38 unsigned char *page10
;
44 struct ses_component
{
49 static int ses_probe(struct device
*dev
)
51 struct scsi_device
*sdev
= to_scsi_device(dev
);
54 if (sdev
->type
!= TYPE_ENCLOSURE
)
58 sdev_printk(KERN_NOTICE
, sdev
, "Attached Enclosure device\n");
64 #define SES_TIMEOUT (30 * HZ)
67 static int ses_recv_diag(struct scsi_device
*sdev
, int page_code
,
68 void *buf
, int bufflen
)
70 unsigned char cmd
[] = {
79 return scsi_execute_req(sdev
, cmd
, DMA_FROM_DEVICE
, buf
, bufflen
,
80 NULL
, SES_TIMEOUT
, SES_RETRIES
, NULL
);
83 static int ses_send_diag(struct scsi_device
*sdev
, int page_code
,
84 void *buf
, int bufflen
)
88 unsigned char cmd
[] = {
90 0x10, /* Set PF bit */
97 result
= scsi_execute_req(sdev
, cmd
, DMA_TO_DEVICE
, buf
, bufflen
,
98 NULL
, SES_TIMEOUT
, SES_RETRIES
, NULL
);
100 sdev_printk(KERN_ERR
, sdev
, "SEND DIAGNOSTIC result: %8x\n",
105 static int ses_set_page2_descriptor(struct enclosure_device
*edev
,
106 struct enclosure_component
*ecomp
,
109 int i
, j
, count
= 0, descriptor
= ecomp
->number
;
110 struct scsi_device
*sdev
= to_scsi_device(edev
->edev
.parent
);
111 struct ses_device
*ses_dev
= edev
->scratch
;
112 unsigned char *type_ptr
= ses_dev
->page1
+ 12 + ses_dev
->page1
[11];
113 unsigned char *desc_ptr
= ses_dev
->page2
+ 8;
115 /* Clear everything */
116 memset(desc_ptr
, 0, ses_dev
->page2_len
- 8);
117 for (i
= 0; i
< ses_dev
->page1
[10]; i
++, type_ptr
+= 4) {
118 for (j
= 0; j
< type_ptr
[1]; j
++) {
120 if (type_ptr
[0] != ENCLOSURE_COMPONENT_DEVICE
&&
121 type_ptr
[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE
)
123 if (count
++ == descriptor
) {
124 memcpy(desc_ptr
, desc
, 4);
127 /* clear reserved, just in case */
133 return ses_send_diag(sdev
, 2, ses_dev
->page2
, ses_dev
->page2_len
);
136 static unsigned char *ses_get_page2_descriptor(struct enclosure_device
*edev
,
137 struct enclosure_component
*ecomp
)
139 int i
, j
, count
= 0, descriptor
= ecomp
->number
;
140 struct scsi_device
*sdev
= to_scsi_device(edev
->edev
.parent
);
141 struct ses_device
*ses_dev
= edev
->scratch
;
142 unsigned char *type_ptr
= ses_dev
->page1
+ 12 + ses_dev
->page1
[11];
143 unsigned char *desc_ptr
= ses_dev
->page2
+ 8;
145 ses_recv_diag(sdev
, 2, ses_dev
->page2
, ses_dev
->page2_len
);
147 for (i
= 0; i
< ses_dev
->page1
[10]; i
++, type_ptr
+= 4) {
148 for (j
= 0; j
< type_ptr
[1]; j
++) {
150 if (type_ptr
[0] != ENCLOSURE_COMPONENT_DEVICE
&&
151 type_ptr
[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE
)
153 if (count
++ == descriptor
)
160 /* For device slot and array device slot elements, byte 3 bit 6
161 * is "fault sensed" while byte 3 bit 5 is "fault reqstd". As this
162 * code stands these bits are shifted 4 positions right so in
163 * sysfs they will appear as bits 2 and 1 respectively. Strange. */
164 static void ses_get_fault(struct enclosure_device
*edev
,
165 struct enclosure_component
*ecomp
)
169 desc
= ses_get_page2_descriptor(edev
, ecomp
);
171 ecomp
->fault
= (desc
[3] & 0x60) >> 4;
174 static int ses_set_fault(struct enclosure_device
*edev
,
175 struct enclosure_component
*ecomp
,
176 enum enclosure_component_setting val
)
178 unsigned char desc
[4] = {0 };
181 case ENCLOSURE_SETTING_DISABLED
:
182 /* zero is disabled */
184 case ENCLOSURE_SETTING_ENABLED
:
188 /* SES doesn't do the SGPIO blink settings */
192 return ses_set_page2_descriptor(edev
, ecomp
, desc
);
195 static void ses_get_status(struct enclosure_device
*edev
,
196 struct enclosure_component
*ecomp
)
200 desc
= ses_get_page2_descriptor(edev
, ecomp
);
202 ecomp
->status
= (desc
[0] & 0x0f);
205 static void ses_get_locate(struct enclosure_device
*edev
,
206 struct enclosure_component
*ecomp
)
210 desc
= ses_get_page2_descriptor(edev
, ecomp
);
212 ecomp
->locate
= (desc
[2] & 0x02) ? 1 : 0;
215 static int ses_set_locate(struct enclosure_device
*edev
,
216 struct enclosure_component
*ecomp
,
217 enum enclosure_component_setting val
)
219 unsigned char desc
[4] = {0 };
222 case ENCLOSURE_SETTING_DISABLED
:
223 /* zero is disabled */
225 case ENCLOSURE_SETTING_ENABLED
:
229 /* SES doesn't do the SGPIO blink settings */
232 return ses_set_page2_descriptor(edev
, ecomp
, desc
);
235 static int ses_set_active(struct enclosure_device
*edev
,
236 struct enclosure_component
*ecomp
,
237 enum enclosure_component_setting val
)
239 unsigned char desc
[4] = {0 };
242 case ENCLOSURE_SETTING_DISABLED
:
243 /* zero is disabled */
246 case ENCLOSURE_SETTING_ENABLED
:
251 /* SES doesn't do the SGPIO blink settings */
254 return ses_set_page2_descriptor(edev
, ecomp
, desc
);
257 static struct enclosure_component_callbacks ses_enclosure_callbacks
= {
258 .get_fault
= ses_get_fault
,
259 .set_fault
= ses_set_fault
,
260 .get_status
= ses_get_status
,
261 .get_locate
= ses_get_locate
,
262 .set_locate
= ses_set_locate
,
263 .set_active
= ses_set_active
,
266 struct ses_host_edev
{
267 struct Scsi_Host
*shost
;
268 struct enclosure_device
*edev
;
272 int ses_match_host(struct enclosure_device
*edev
, void *data
)
274 struct ses_host_edev
*sed
= data
;
275 struct scsi_device
*sdev
;
277 if (!scsi_is_sdev_device(edev
->edev
.parent
))
280 sdev
= to_scsi_device(edev
->edev
.parent
);
282 if (sdev
->host
!= sed
->shost
)
290 static void ses_process_descriptor(struct enclosure_component
*ecomp
,
293 int eip
= desc
[0] & 0x10;
294 int invalid
= desc
[0] & 0x80;
295 enum scsi_protocol proto
= desc
[0] & 0x0f;
297 struct ses_component
*scomp
= ecomp
->scratch
;
306 case SCSI_PROTOCOL_SAS
:
311 /* only take the phy0 addr */
312 addr
= (u64
)d
[12] << 56 |
322 /* FIXME: Need to add more protocols than just SAS */
333 static int ses_enclosure_find_by_addr(struct enclosure_device
*edev
,
336 struct efd
*efd
= data
;
338 struct ses_component
*scomp
;
340 if (!edev
->component
[0].scratch
)
343 for (i
= 0; i
< edev
->components
; i
++) {
344 scomp
= edev
->component
[i
].scratch
;
345 if (scomp
->addr
!= efd
->addr
)
348 enclosure_add_device(edev
, i
, efd
->dev
);
354 #define INIT_ALLOC_SIZE 32
356 static void ses_enclosure_data_process(struct enclosure_device
*edev
,
357 struct scsi_device
*sdev
,
361 unsigned char *buf
= NULL
, *type_ptr
, *desc_ptr
, *addl_desc_ptr
= NULL
;
362 int i
, j
, page7_len
, len
, components
;
363 struct ses_device
*ses_dev
= edev
->scratch
;
364 int types
= ses_dev
->page1
[10];
365 unsigned char *hdr_buf
= kzalloc(INIT_ALLOC_SIZE
, GFP_KERNEL
);
368 goto simple_populate
;
370 /* re-read page 10 */
372 ses_recv_diag(sdev
, 10, ses_dev
->page10
, ses_dev
->page10_len
);
373 /* Page 7 for the descriptors is optional */
374 result
= ses_recv_diag(sdev
, 7, hdr_buf
, INIT_ALLOC_SIZE
);
376 goto simple_populate
;
378 page7_len
= len
= (hdr_buf
[2] << 8) + hdr_buf
[3] + 4;
379 /* add 1 for trailing '\0' we'll use */
380 buf
= kzalloc(len
+ 1, GFP_KERNEL
);
382 goto simple_populate
;
383 result
= ses_recv_diag(sdev
, 7, buf
, len
);
393 len
= (desc_ptr
[2] << 8) + desc_ptr
[3];
394 /* skip past overall descriptor */
398 addl_desc_ptr
= ses_dev
->page10
+ 8;
399 type_ptr
= ses_dev
->page1
+ 12 + ses_dev
->page1
[11];
401 for (i
= 0; i
< types
; i
++, type_ptr
+= 4) {
402 for (j
= 0; j
< type_ptr
[1]; j
++) {
404 struct enclosure_component
*ecomp
;
407 if (desc_ptr
>= buf
+ page7_len
) {
410 len
= (desc_ptr
[2] << 8) + desc_ptr
[3];
412 /* Add trailing zero - pushes into
414 desc_ptr
[len
] = '\0';
418 if (type_ptr
[0] == ENCLOSURE_COMPONENT_DEVICE
||
419 type_ptr
[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE
) {
422 ecomp
= enclosure_component_register(edev
,
427 ecomp
= &edev
->component
[components
++];
429 if (!IS_ERR(ecomp
) && addl_desc_ptr
)
430 ses_process_descriptor(ecomp
,
437 addl_desc_ptr
+= addl_desc_ptr
[1] + 2;
445 static void ses_match_to_enclosure(struct enclosure_device
*edev
,
446 struct scsi_device
*sdev
)
450 unsigned int vpd_len
;
455 buf
= scsi_get_vpd_page(sdev
, 0x83);
459 ses_enclosure_data_process(edev
, to_scsi_device(edev
->edev
.parent
), 0);
461 vpd_len
= ((buf
[2] << 8) | buf
[3]) + 4;
464 while (desc
< buf
+ vpd_len
) {
465 enum scsi_protocol proto
= desc
[0] >> 4;
466 u8 code_set
= desc
[0] & 0x0f;
467 u8 piv
= desc
[1] & 0x80;
468 u8 assoc
= (desc
[1] & 0x30) >> 4;
469 u8 type
= desc
[1] & 0x0f;
472 if (piv
&& code_set
== 1 && assoc
== 1
473 && proto
== SCSI_PROTOCOL_SAS
&& type
== 3 && len
== 8)
474 efd
.addr
= (u64
)desc
[4] << 56 |
488 efd
.dev
= &sdev
->sdev_gendev
;
490 enclosure_for_each_device(ses_enclosure_find_by_addr
, &efd
);
495 static int ses_intf_add(struct device
*cdev
,
496 struct class_interface
*intf
)
498 struct scsi_device
*sdev
= to_scsi_device(cdev
->parent
);
499 struct scsi_device
*tmp_sdev
;
500 unsigned char *buf
= NULL
, *hdr_buf
, *type_ptr
;
501 struct ses_device
*ses_dev
;
503 int i
, types
, len
, components
= 0;
505 struct enclosure_device
*edev
;
506 struct ses_component
*scomp
= NULL
;
508 if (!scsi_device_enclosure(sdev
)) {
509 /* not an enclosure, but might be in one */
510 struct enclosure_device
*prev
= NULL
;
512 while ((edev
= enclosure_find(&sdev
->host
->shost_gendev
, prev
)) != NULL
) {
513 ses_match_to_enclosure(edev
, sdev
);
519 /* TYPE_ENCLOSURE prints a message in probe */
520 if (sdev
->type
!= TYPE_ENCLOSURE
)
521 sdev_printk(KERN_NOTICE
, sdev
, "Embedded Enclosure Device\n");
523 ses_dev
= kzalloc(sizeof(*ses_dev
), GFP_KERNEL
);
524 hdr_buf
= kzalloc(INIT_ALLOC_SIZE
, GFP_KERNEL
);
525 if (!hdr_buf
|| !ses_dev
)
528 result
= ses_recv_diag(sdev
, 1, hdr_buf
, INIT_ALLOC_SIZE
);
532 if (hdr_buf
[1] != 0) {
533 /* FIXME: need subenclosure support; I've just never
534 * seen a device with subenclosures and it makes the
535 * traversal routines more complex */
536 sdev_printk(KERN_ERR
, sdev
,
537 "FIXME driver has no support for subenclosures (%d)\n",
542 len
= (hdr_buf
[2] << 8) + hdr_buf
[3] + 4;
543 buf
= kzalloc(len
, GFP_KERNEL
);
547 result
= ses_recv_diag(sdev
, 1, buf
, len
);
553 type_ptr
= buf
+ 12 + buf
[11];
555 for (i
= 0; i
< types
; i
++, type_ptr
+= 4) {
556 if (type_ptr
[0] == ENCLOSURE_COMPONENT_DEVICE
||
557 type_ptr
[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE
)
558 components
+= type_ptr
[1];
560 ses_dev
->page1
= buf
;
561 ses_dev
->page1_len
= len
;
564 result
= ses_recv_diag(sdev
, 2, hdr_buf
, INIT_ALLOC_SIZE
);
568 len
= (hdr_buf
[2] << 8) + hdr_buf
[3] + 4;
569 buf
= kzalloc(len
, GFP_KERNEL
);
573 /* make sure getting page 2 actually works */
574 result
= ses_recv_diag(sdev
, 2, buf
, len
);
577 ses_dev
->page2
= buf
;
578 ses_dev
->page2_len
= len
;
581 /* The additional information page --- allows us
582 * to match up the devices */
583 result
= ses_recv_diag(sdev
, 10, hdr_buf
, INIT_ALLOC_SIZE
);
586 len
= (hdr_buf
[2] << 8) + hdr_buf
[3] + 4;
587 buf
= kzalloc(len
, GFP_KERNEL
);
591 result
= ses_recv_diag(sdev
, 10, buf
, len
);
594 ses_dev
->page10
= buf
;
595 ses_dev
->page10_len
= len
;
598 scomp
= kzalloc(sizeof(struct ses_component
) * components
, GFP_KERNEL
);
602 edev
= enclosure_register(cdev
->parent
, dev_name(&sdev
->sdev_gendev
),
603 components
, &ses_enclosure_callbacks
);
611 edev
->scratch
= ses_dev
;
612 for (i
= 0; i
< components
; i
++)
613 edev
->component
[i
].scratch
= scomp
+ i
;
615 ses_enclosure_data_process(edev
, sdev
, 1);
617 /* see if there are any devices matching before
618 * we found the enclosure */
619 shost_for_each_device(tmp_sdev
, sdev
->host
) {
620 if (tmp_sdev
->lun
!= 0 || scsi_device_enclosure(tmp_sdev
))
622 ses_match_to_enclosure(edev
, tmp_sdev
);
628 sdev_printk(KERN_ERR
, sdev
, "Failed to get diagnostic page 0x%x\n",
634 kfree(ses_dev
->page10
);
635 kfree(ses_dev
->page2
);
636 kfree(ses_dev
->page1
);
640 sdev_printk(KERN_ERR
, sdev
, "Failed to bind enclosure %d\n", err
);
644 static int ses_remove(struct device
*dev
)
649 static void ses_intf_remove_component(struct scsi_device
*sdev
)
651 struct enclosure_device
*edev
, *prev
= NULL
;
653 while ((edev
= enclosure_find(&sdev
->host
->shost_gendev
, prev
)) != NULL
) {
655 if (!enclosure_remove_device(edev
, &sdev
->sdev_gendev
))
659 put_device(&edev
->edev
);
662 static void ses_intf_remove_enclosure(struct scsi_device
*sdev
)
664 struct enclosure_device
*edev
;
665 struct ses_device
*ses_dev
;
667 /* exact match to this enclosure */
668 edev
= enclosure_find(&sdev
->sdev_gendev
, NULL
);
672 ses_dev
= edev
->scratch
;
673 edev
->scratch
= NULL
;
675 kfree(ses_dev
->page10
);
676 kfree(ses_dev
->page1
);
677 kfree(ses_dev
->page2
);
680 kfree(edev
->component
[0].scratch
);
682 put_device(&edev
->edev
);
683 enclosure_unregister(edev
);
686 static void ses_intf_remove(struct device
*cdev
,
687 struct class_interface
*intf
)
689 struct scsi_device
*sdev
= to_scsi_device(cdev
->parent
);
691 if (!scsi_device_enclosure(sdev
))
692 ses_intf_remove_component(sdev
);
694 ses_intf_remove_enclosure(sdev
);
697 static struct class_interface ses_interface
= {
698 .add_dev
= ses_intf_add
,
699 .remove_dev
= ses_intf_remove
,
702 static struct scsi_driver ses_template
= {
703 .owner
= THIS_MODULE
,
707 .remove
= ses_remove
,
711 static int __init
ses_init(void)
715 err
= scsi_register_interface(&ses_interface
);
719 err
= scsi_register_driver(&ses_template
.gendrv
);
726 scsi_unregister_interface(&ses_interface
);
730 static void __exit
ses_exit(void)
732 scsi_unregister_driver(&ses_template
.gendrv
);
733 scsi_unregister_interface(&ses_interface
);
736 module_init(ses_init
);
737 module_exit(ses_exit
);
739 MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE
);
741 MODULE_AUTHOR("James Bottomley");
742 MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
743 MODULE_LICENSE("GPL v2");