Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / scsi / ses.c
blob2c61624cb4b038b07073f5538378c126bbd44c29
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * SCSI Enclosure Services
5 * Copyright (C) 2008 James Bottomley <James.Bottomley@HansenPartnership.com>
6 */
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>
23 struct ses_device {
24 unsigned char *page1;
25 unsigned char *page1_types;
26 unsigned char *page2;
27 unsigned char *page10;
28 short page1_len;
29 short page1_num_types;
30 short page2_len;
31 short page10_len;
34 struct ses_component {
35 u64 addr;
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);
48 int err = -ENODEV;
50 if (sdev->type != TYPE_ENCLOSURE)
51 goto out;
53 err = 0;
54 sdev_printk(KERN_NOTICE, sdev, "Attached Enclosure device\n");
56 out:
57 return err;
60 #define SES_TIMEOUT (30 * HZ)
61 #define SES_RETRIES 3
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);
68 dest_desc[0] = 0;
69 /* only clear byte 1 for ENCLOSURE_COMPONENT_DEVICE */
70 if (ecomp->type == ENCLOSURE_COMPONENT_DEVICE)
71 dest_desc[1] = 0;
72 dest_desc[2] &= 0xde;
73 dest_desc[3] &= 0x3c;
77 static int ses_recv_diag(struct scsi_device *sdev, int page_code,
78 void *buf, int bufflen)
80 int ret;
81 unsigned char cmd[] = {
82 RECEIVE_DIAGNOSTIC,
83 1, /* Set PCV bit */
84 page_code,
85 bufflen >> 8,
86 bufflen & 0xff,
89 unsigned char recv_page_code;
90 struct scsi_failure failure_defs[] = {
92 .sense = UNIT_ATTENTION,
93 .asc = 0x29,
94 .ascq = SCMD_FAILURE_ASCQ_ANY,
95 .allowed = SES_RETRIES,
96 .result = SAM_STAT_CHECK_CONDITION,
99 .sense = NOT_READY,
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);
116 if (unlikely(ret))
117 return ret;
119 recv_page_code = ((unsigned char *)buf)[0];
121 if (likely(recv_page_code == page_code))
122 return ret;
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);
131 return -EINVAL;
134 static int ses_send_diag(struct scsi_device *sdev, int page_code,
135 void *buf, int bufflen)
137 int result;
139 unsigned char cmd[] = {
140 SEND_DIAGNOSTIC,
141 0x10, /* Set PF bit */
143 bufflen >> 8,
144 bufflen & 0xff,
147 struct scsi_failure failure_defs[] = {
149 .sense = UNIT_ATTENTION,
150 .asc = 0x29,
151 .ascq = SCMD_FAILURE_ASCQ_ANY,
152 .allowed = SES_RETRIES,
153 .result = SAM_STAT_CHECK_CONDITION,
156 .sense = NOT_READY,
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);
173 if (result)
174 sdev_printk(KERN_ERR, sdev, "SEND DIAGNOSTIC result: %8x\n",
175 result);
176 return result;
179 static int ses_set_page2_descriptor(struct enclosure_device *edev,
180 struct enclosure_component *ecomp,
181 unsigned char *desc)
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++) {
193 desc_ptr += 4;
194 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
195 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
196 continue;
197 if (count++ == descriptor) {
198 memcpy(desc_ptr, desc, 4);
199 /* set select */
200 desc_ptr[0] |= 0x80;
201 /* clear reserved, just in case */
202 desc_ptr[0] &= 0xf0;
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)
220 return NULL;
222 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
223 for (j = 0; j < type_ptr[1]; j++) {
224 desc_ptr += 4;
225 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
226 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
227 continue;
228 if (count++ == descriptor)
229 return desc_ptr;
232 return NULL;
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)
242 unsigned char *desc;
244 if (!ses_page2_supported(edev)) {
245 ecomp->fault = 0;
246 return;
248 desc = ses_get_page2_descriptor(edev, ecomp);
249 if (desc)
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))
261 return -EINVAL;
263 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
265 if (!desc_ptr)
266 return -EIO;
268 init_device_slot_control(desc, ecomp, desc_ptr);
270 switch (val) {
271 case ENCLOSURE_SETTING_DISABLED:
272 desc[3] &= 0xdf;
273 break;
274 case ENCLOSURE_SETTING_ENABLED:
275 desc[3] |= 0x20;
276 break;
277 default:
278 /* SES doesn't do the SGPIO blink settings */
279 return -EINVAL;
282 return ses_set_page2_descriptor(edev, ecomp, desc);
285 static void ses_get_status(struct enclosure_device *edev,
286 struct enclosure_component *ecomp)
288 unsigned char *desc;
290 if (!ses_page2_supported(edev)) {
291 ecomp->status = 0;
292 return;
294 desc = ses_get_page2_descriptor(edev, ecomp);
295 if (desc)
296 ecomp->status = (desc[0] & 0x0f);
299 static void ses_get_locate(struct enclosure_device *edev,
300 struct enclosure_component *ecomp)
302 unsigned char *desc;
304 if (!ses_page2_supported(edev)) {
305 ecomp->locate = 0;
306 return;
308 desc = ses_get_page2_descriptor(edev, ecomp);
309 if (desc)
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))
321 return -EINVAL;
323 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
325 if (!desc_ptr)
326 return -EIO;
328 init_device_slot_control(desc, ecomp, desc_ptr);
330 switch (val) {
331 case ENCLOSURE_SETTING_DISABLED:
332 desc[2] &= 0xfd;
333 break;
334 case ENCLOSURE_SETTING_ENABLED:
335 desc[2] |= 0x02;
336 break;
337 default:
338 /* SES doesn't do the SGPIO blink settings */
339 return -EINVAL;
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))
352 return -EINVAL;
354 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
356 if (!desc_ptr)
357 return -EIO;
359 init_device_slot_control(desc, ecomp, desc_ptr);
361 switch (val) {
362 case ENCLOSURE_SETTING_DISABLED:
363 desc[2] &= 0x7f;
364 ecomp->active = 0;
365 break;
366 case ENCLOSURE_SETTING_ENABLED:
367 desc[2] |= 0x80;
368 ecomp->active = 1;
369 break;
370 default:
371 /* SES doesn't do the SGPIO blink settings */
372 return -EINVAL;
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)
388 unsigned char *desc;
390 if (!ses_page2_supported(edev)) {
391 ecomp->power_status = 0;
392 return;
395 desc = ses_get_page2_descriptor(edev, ecomp);
396 if (desc)
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,
402 int val)
404 unsigned char desc[4];
405 unsigned char *desc_ptr;
407 if (!ses_page2_supported(edev))
408 return -EINVAL;
410 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
412 if (!desc_ptr)
413 return -EIO;
415 init_device_slot_control(desc, ecomp, desc_ptr);
417 switch (val) {
418 /* power = 1 is device_off = 0 and vice versa */
419 case 0:
420 desc[3] |= 0x10;
421 break;
422 case 1:
423 desc[3] &= 0xef;
424 break;
425 default:
426 return -EINVAL;
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;
449 #if 0
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))
456 return 0;
458 sdev = to_scsi_device(edev->edev.parent);
460 if (sdev->host != sed->shost)
461 return 0;
463 sed->edev = edev;
464 return 1;
466 #endif /* 0 */
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;
474 u64 addr = 0;
475 int slot = -1;
476 struct ses_component *scomp = ecomp->scratch;
477 unsigned char *d;
479 if (invalid)
480 return 0;
482 switch (proto) {
483 case SCSI_PROTOCOL_FCP:
484 if (eip) {
485 if (max_desc_len <= 7)
486 return 1;
487 d = desc + 4;
488 slot = d[3];
490 break;
491 case SCSI_PROTOCOL_SAS:
493 if (eip) {
494 if (max_desc_len <= 27)
495 return 1;
496 d = desc + 4;
497 slot = d[3];
498 d = desc + 8;
499 } else {
500 if (max_desc_len <= 23)
501 return 1;
502 d = desc + 4;
506 /* only take the phy0 addr */
507 addr = (u64)d[12] << 56 |
508 (u64)d[13] << 48 |
509 (u64)d[14] << 40 |
510 (u64)d[15] << 32 |
511 (u64)d[16] << 24 |
512 (u64)d[17] << 16 |
513 (u64)d[18] << 8 |
514 (u64)d[19];
515 break;
516 default:
517 /* FIXME: Need to add more protocols than just SAS */
518 break;
520 ecomp->slot = slot;
521 scomp->addr = addr;
523 return 0;
526 struct efd {
527 u64 addr;
528 struct device *dev;
531 static int ses_enclosure_find_by_addr(struct enclosure_device *edev,
532 void *data)
534 struct efd *efd = data;
535 int i;
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)
541 continue;
543 if (enclosure_add_device(edev, i, efd->dev) == 0)
544 kobject_uevent(&efd->dev->kobj, KOBJ_CHANGE);
545 return 1;
547 return 0;
550 #define INIT_ALLOC_SIZE 32
552 static void ses_enclosure_data_process(struct enclosure_device *edev,
553 struct scsi_device *sdev,
554 int create)
556 u32 result;
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);
563 if (!hdr_buf)
564 goto simple_populate;
566 /* re-read page 10 */
567 if (ses_dev->page10)
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);
571 if (result)
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);
577 if (!buf)
578 goto simple_populate;
579 result = ses_recv_diag(sdev, 7, buf, len);
580 if (result) {
581 simple_populate:
582 kfree(buf);
583 buf = NULL;
584 desc_ptr = NULL;
585 len = 0;
586 page7_len = 0;
587 } else {
588 desc_ptr = buf + 8;
589 len = (desc_ptr[2] << 8) + desc_ptr[3];
590 /* skip past overall descriptor */
591 desc_ptr += len + 4;
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;
596 components = 0;
597 for (i = 0; i < types; i++, type_ptr += 4) {
598 for (j = 0; j < type_ptr[1]; j++) {
599 char *name = NULL;
600 struct enclosure_component *ecomp;
601 int max_desc_len;
603 if (desc_ptr) {
604 if (desc_ptr + 3 >= buf + page7_len) {
605 desc_ptr = NULL;
606 } else {
607 len = (desc_ptr[2] << 8) + desc_ptr[3];
608 desc_ptr += 4;
609 if (desc_ptr + len > buf + page7_len)
610 desc_ptr = NULL;
611 else {
612 /* Add trailing zero - pushes into
613 * reserved space */
614 desc_ptr[len] = '\0';
615 name = desc_ptr;
619 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
620 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) {
622 if (create)
623 ecomp = enclosure_component_alloc(
624 edev,
625 components++,
626 type_ptr[0],
627 name);
628 else if (components < edev->components)
629 ecomp = &edev->component[components++];
630 else
631 ecomp = ERR_PTR(-EINVAL);
633 if (!IS_ERR(ecomp)) {
634 if (addl_desc_ptr) {
635 max_desc_len = ses_dev->page10_len -
636 (addl_desc_ptr - ses_dev->page10);
637 if (ses_process_descriptor(ecomp,
638 addl_desc_ptr,
639 max_desc_len))
640 addl_desc_ptr = NULL;
642 if (create)
643 enclosure_component_register(
644 ecomp);
647 if (desc_ptr)
648 desc_ptr += len;
650 if (addl_desc_ptr &&
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;
665 kfree(buf);
666 kfree(hdr_buf);
669 static void ses_match_to_enclosure(struct enclosure_device *edev,
670 struct scsi_device *sdev,
671 int refresh)
673 struct scsi_device *edev_sdev = to_scsi_device(edev->edev.parent);
674 struct efd efd = {
675 .addr = 0,
678 if (refresh)
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);
684 if (efd.addr) {
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;
697 u32 result;
698 int i, types, len, components = 0;
699 int err = -ENOMEM;
700 int num_enclosures;
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);
710 prev = edev;
712 return -ENODEV;
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)
722 goto err_init_free;
724 page = 1;
725 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
726 if (result)
727 goto recv_failed;
729 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
730 buf = kzalloc(len, GFP_KERNEL);
731 if (!buf)
732 goto err_free;
734 result = ses_recv_diag(sdev, page, buf, len);
735 if (result)
736 goto recv_failed;
738 types = 0;
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 */
745 type_ptr = buf + 8;
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;
763 buf = NULL;
765 page = 2;
766 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
767 if (result)
768 goto page2_not_supported;
770 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
771 buf = kzalloc(len, GFP_KERNEL);
772 if (!buf)
773 goto err_free;
775 /* make sure getting page 2 actually works */
776 result = ses_recv_diag(sdev, 2, buf, len);
777 if (result)
778 goto recv_failed;
779 ses_dev->page2 = buf;
780 ses_dev->page2_len = len;
781 buf = NULL;
783 /* The additional information page --- allows us
784 * to match up the devices */
785 page = 10;
786 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
787 if (!result) {
789 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
790 buf = kzalloc(len, GFP_KERNEL);
791 if (!buf)
792 goto err_free;
794 result = ses_recv_diag(sdev, page, buf, len);
795 if (result)
796 goto recv_failed;
797 ses_dev->page10 = buf;
798 ses_dev->page10_len = len;
799 buf = NULL;
801 page2_not_supported:
802 if (components > 0) {
803 scomp = kcalloc(components, sizeof(struct ses_component), GFP_KERNEL);
804 if (!scomp)
805 goto err_free;
808 edev = enclosure_register(cdev->parent, dev_name(&sdev->sdev_gendev),
809 components, &ses_enclosure_callbacks);
810 if (IS_ERR(edev)) {
811 err = PTR_ERR(edev);
812 goto err_free;
815 kfree(hdr_buf);
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))
827 continue;
828 ses_match_to_enclosure(edev, tmp_sdev, 0);
831 return 0;
833 recv_failed:
834 sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n",
835 page);
836 err = -ENODEV;
837 err_free:
838 kfree(buf);
839 kfree(scomp);
840 kfree(ses_dev->page10);
841 kfree(ses_dev->page2);
842 kfree(ses_dev->page1);
843 err_init_free:
844 kfree(ses_dev);
845 kfree(hdr_buf);
846 sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err);
847 return err;
850 static int ses_remove(struct device *dev)
852 return 0;
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) {
860 prev = edev;
861 if (!enclosure_remove_device(edev, &sdev->sdev_gendev))
862 break;
864 if (edev)
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);
875 if (!edev)
876 return;
878 ses_dev = edev->scratch;
879 edev->scratch = NULL;
881 kfree(ses_dev->page10);
882 kfree(ses_dev->page1);
883 kfree(ses_dev->page2);
884 kfree(ses_dev);
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);
899 else
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 = {
909 .gendrv = {
910 .name = "ses",
911 .probe = ses_probe,
912 .remove = ses_remove,
916 static int __init ses_init(void)
918 int err;
920 err = scsi_register_interface(&ses_interface);
921 if (err)
922 return err;
924 err = scsi_register_driver(&ses_template.gendrv);
925 if (err)
926 goto out_unreg;
928 return 0;
930 out_unreg:
931 scsi_unregister_interface(&ses_interface);
932 return err;
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");