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>
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
67 static int ses_recv_diag(struct scsi_device *sdev, int page_code,
68 void *buf, int bufflen)
79 return scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, bufflen,
80 NULL, SES_TIMEOUT, SES_RETRIES);
83 static int ses_send_diag(struct scsi_device *sdev, int page_code,
84 void *buf, int bufflen)
90 0x10, /* Set PF bit */
97 result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, buf, bufflen,
98 NULL, SES_TIMEOUT, SES_RETRIES);
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->cdev.dev);
111 struct ses_device *ses_dev = edev->scratch;
112 char *type_ptr = ses_dev->page1 + 12 + ses_dev->page1[11];
113 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 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->cdev.dev);
141 struct ses_device *ses_dev = edev->scratch;
142 char *type_ptr = ses_dev->page1 + 12 + ses_dev->page1[11];
143 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 static void ses_get_fault(struct enclosure_device *edev,
161 struct enclosure_component *ecomp)
165 desc = ses_get_page2_descriptor(edev, ecomp);
166 ecomp->fault = (desc[3] & 0x60) >> 4;
169 static int ses_set_fault(struct enclosure_device *edev,
170 struct enclosure_component *ecomp,
171 enum enclosure_component_setting val)
176 case ENCLOSURE_SETTING_DISABLED:
177 /* zero is disabled */
179 case ENCLOSURE_SETTING_ENABLED:
183 /* SES doesn't do the SGPIO blink settings */
187 return ses_set_page2_descriptor(edev, ecomp, desc);
190 static void ses_get_status(struct enclosure_device *edev,
191 struct enclosure_component *ecomp)
195 desc = ses_get_page2_descriptor(edev, ecomp);
196 ecomp->status = (desc[0] & 0x0f);
199 static void ses_get_locate(struct enclosure_device *edev,
200 struct enclosure_component *ecomp)
204 desc = ses_get_page2_descriptor(edev, ecomp);
205 ecomp->locate = (desc[2] & 0x02) ? 1 : 0;
208 static int ses_set_locate(struct enclosure_device *edev,
209 struct enclosure_component *ecomp,
210 enum enclosure_component_setting val)
215 case ENCLOSURE_SETTING_DISABLED:
216 /* zero is disabled */
218 case ENCLOSURE_SETTING_ENABLED:
222 /* SES doesn't do the SGPIO blink settings */
225 return ses_set_page2_descriptor(edev, ecomp, desc);
228 static int ses_set_active(struct enclosure_device *edev,
229 struct enclosure_component *ecomp,
230 enum enclosure_component_setting val)
235 case ENCLOSURE_SETTING_DISABLED:
236 /* zero is disabled */
239 case ENCLOSURE_SETTING_ENABLED:
244 /* SES doesn't do the SGPIO blink settings */
247 return ses_set_page2_descriptor(edev, ecomp, desc);
250 static struct enclosure_component_callbacks ses_enclosure_callbacks = {
251 .get_fault = ses_get_fault,
252 .set_fault = ses_set_fault,
253 .get_status = ses_get_status,
254 .get_locate = ses_get_locate,
255 .set_locate = ses_set_locate,
256 .set_active = ses_set_active,
259 struct ses_host_edev {
260 struct Scsi_Host *shost;
261 struct enclosure_device *edev;
264 int ses_match_host(struct enclosure_device *edev, void *data)
266 struct ses_host_edev *sed = data;
267 struct scsi_device *sdev;
269 if (!scsi_is_sdev_device(edev->cdev.dev))
272 sdev = to_scsi_device(edev->cdev.dev);
274 if (sdev->host != sed->shost)
281 static void ses_process_descriptor(struct enclosure_component *ecomp,
284 int eip = desc[0] & 0x10;
285 int invalid = desc[0] & 0x80;
286 enum scsi_protocol proto = desc[0] & 0x0f;
288 struct ses_component *scomp = ecomp->scratch;
297 case SCSI_PROTOCOL_SAS:
302 /* only take the phy0 addr */
303 addr = (u64)d[12] << 56 |
313 /* FIXME: Need to add more protocols than just SAS */
324 static int ses_enclosure_find_by_addr(struct enclosure_device *edev,
327 struct efd *efd = data;
329 struct ses_component *scomp;
331 if (!edev->component[0].scratch)
334 for (i = 0; i < edev->components; i++) {
335 scomp = edev->component[i].scratch;
336 if (scomp->addr != efd->addr)
339 enclosure_add_device(edev, i, efd->dev);
345 #define VPD_INQUIRY_SIZE 512
347 static void ses_match_to_enclosure(struct enclosure_device *edev,
348 struct scsi_device *sdev)
350 unsigned char *buf = kmalloc(VPD_INQUIRY_SIZE, GFP_KERNEL);
356 unsigned char cmd[] = {
360 VPD_INQUIRY_SIZE >> 8,
361 VPD_INQUIRY_SIZE & 0xff,
368 if (scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf,
369 VPD_INQUIRY_SIZE, NULL, SES_TIMEOUT, SES_RETRIES))
372 len = (buf[2] << 8) + buf[3];
374 while (desc < buf + len) {
375 enum scsi_protocol proto = desc[0] >> 4;
376 u8 code_set = desc[0] & 0x0f;
377 u8 piv = desc[1] & 0x80;
378 u8 assoc = (desc[1] & 0x30) >> 4;
379 u8 type = desc[1] & 0x0f;
382 if (piv && code_set == 1 && assoc == 1 && code_set == 1
383 && proto == SCSI_PROTOCOL_SAS && type == 3 && len == 8)
384 efd.addr = (u64)desc[4] << 56 |
398 efd.dev = &sdev->sdev_gendev;
400 enclosure_for_each_device(ses_enclosure_find_by_addr, &efd);
405 #define INIT_ALLOC_SIZE 32
407 static int ses_intf_add(struct class_device *cdev,
408 struct class_interface *intf)
410 struct scsi_device *sdev = to_scsi_device(cdev->dev);
411 struct scsi_device *tmp_sdev;
412 unsigned char *buf = NULL, *hdr_buf, *type_ptr, *desc_ptr,
414 struct ses_device *ses_dev;
416 int i, j, types, len, components = 0;
418 struct enclosure_device *edev;
419 struct ses_component *scomp;
421 if (!scsi_device_enclosure(sdev)) {
422 /* not an enclosure, but might be in one */
423 edev = enclosure_find(&sdev->host->shost_gendev);
425 ses_match_to_enclosure(edev, sdev);
426 class_device_put(&edev->cdev);
431 /* TYPE_ENCLOSURE prints a message in probe */
432 if (sdev->type != TYPE_ENCLOSURE)
433 sdev_printk(KERN_NOTICE, sdev, "Embedded Enclosure Device\n");
435 ses_dev = kzalloc(sizeof(*ses_dev), GFP_KERNEL);
436 hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
437 if (!hdr_buf || !ses_dev)
440 result = ses_recv_diag(sdev, 1, hdr_buf, INIT_ALLOC_SIZE);
444 if (hdr_buf[1] != 0) {
445 /* FIXME: need subenclosure support; I've just never
446 * seen a device with subenclosures and it makes the
447 * traversal routines more complex */
448 sdev_printk(KERN_ERR, sdev,
449 "FIXME driver has no support for subenclosures (%d)\n",
454 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
455 buf = kzalloc(len, GFP_KERNEL);
459 ses_dev->page1 = buf;
460 ses_dev->page1_len = len;
462 result = ses_recv_diag(sdev, 1, buf, len);
469 type_ptr = buf + 12 + len;
471 for (i = 0; i < types; i++, type_ptr += 4) {
472 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
473 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE)
474 components += type_ptr[1];
477 result = ses_recv_diag(sdev, 2, hdr_buf, INIT_ALLOC_SIZE);
481 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
482 buf = kzalloc(len, GFP_KERNEL);
486 /* make sure getting page 2 actually works */
487 result = ses_recv_diag(sdev, 2, buf, len);
490 ses_dev->page2 = buf;
491 ses_dev->page2_len = len;
493 /* The additional information page --- allows us
494 * to match up the devices */
495 result = ses_recv_diag(sdev, 10, hdr_buf, INIT_ALLOC_SIZE);
499 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
500 buf = kzalloc(len, GFP_KERNEL);
504 result = ses_recv_diag(sdev, 10, buf, len);
507 ses_dev->page10 = buf;
508 ses_dev->page10_len = len;
511 scomp = kmalloc(sizeof(struct ses_component) * components, GFP_KERNEL);
515 edev = enclosure_register(cdev->dev, sdev->sdev_gendev.bus_id,
516 components, &ses_enclosure_callbacks);
522 edev->scratch = ses_dev;
523 for (i = 0; i < components; i++)
524 edev->component[i].scratch = scomp++;
526 /* Page 7 for the descriptors is optional */
528 result = ses_recv_diag(sdev, 7, hdr_buf, INIT_ALLOC_SIZE);
530 goto simple_populate;
532 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
533 /* add 1 for trailing '\0' we'll use */
534 buf = kzalloc(len + 1, GFP_KERNEL);
535 result = ses_recv_diag(sdev, 7, buf, len);
541 addl_desc_ptr = NULL;
544 len = (desc_ptr[2] << 8) + desc_ptr[3];
545 /* skip past overall descriptor */
547 addl_desc_ptr = ses_dev->page10 + 8;
549 type_ptr = ses_dev->page1 + 12 + ses_dev->page1[11];
551 for (i = 0; i < types; i++, type_ptr += 4) {
552 for (j = 0; j < type_ptr[1]; j++) {
554 struct enclosure_component *ecomp;
557 len = (desc_ptr[2] << 8) + desc_ptr[3];
559 /* Add trailing zero - pushes into
561 desc_ptr[len] = '\0';
564 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
565 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
567 ecomp = enclosure_component_register(edev,
574 ses_process_descriptor(ecomp,
578 addl_desc_ptr += addl_desc_ptr[1] + 2;
585 /* see if there are any devices matching before
586 * we found the enclosure */
587 shost_for_each_device(tmp_sdev, sdev->host) {
588 if (tmp_sdev->lun != 0 || scsi_device_enclosure(tmp_sdev))
590 ses_match_to_enclosure(edev, tmp_sdev);
596 sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n",
601 kfree(ses_dev->page10);
602 kfree(ses_dev->page2);
603 kfree(ses_dev->page1);
607 sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err);
611 static int ses_remove(struct device *dev)
616 static void ses_intf_remove(struct class_device *cdev,
617 struct class_interface *intf)
619 struct scsi_device *sdev = to_scsi_device(cdev->dev);
620 struct enclosure_device *edev;
621 struct ses_device *ses_dev;
623 if (!scsi_device_enclosure(sdev))
626 edev = enclosure_find(cdev->dev);
630 ses_dev = edev->scratch;
631 edev->scratch = NULL;
633 kfree(ses_dev->page1);
634 kfree(ses_dev->page2);
637 kfree(edev->component[0].scratch);
639 class_device_put(&edev->cdev);
640 enclosure_unregister(edev);
643 static struct class_interface ses_interface = {
645 .remove = ses_intf_remove,
648 static struct scsi_driver ses_template = {
649 .owner = THIS_MODULE,
653 .remove = ses_remove,
657 static int __init ses_init(void)
661 err = scsi_register_interface(&ses_interface);
665 err = scsi_register_driver(&ses_template.gendrv);
672 scsi_unregister_interface(&ses_interface);
676 static void __exit ses_exit(void)
678 scsi_unregister_driver(&ses_template.gendrv);
679 scsi_unregister_interface(&ses_interface);
682 module_init(ses_init);
683 module_exit(ses_exit);
685 MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE);
687 MODULE_AUTHOR("James Bottomley");
688 MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
689 MODULE_LICENSE("GPL v2");