1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * SCSI Enclosure Services
4 *
5 * Copyright (C) 2008 James Bottomley <James.Bottomley@HansenPartnership.com>
6 */
7
8 #include <linux/slab.h>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/enclosure.h>
12 #include <asm/unaligned.h>
13
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>
20
21 #include <scsi/scsi_transport_sas.h>
22
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;
32 };
33
34 struct ses_component {
35 u64 addr;
36 };
37
ses_page2_supported(struct enclosure_device * edev)38 static bool ses_page2_supported(struct enclosure_device *edev)
39 {
40 struct ses_device *ses_dev = edev->scratch;
41
42 return (ses_dev->page2 != NULL);
43 }
44
ses_probe(struct device * dev)45 static int ses_probe(struct device *dev)
46 {
47 struct scsi_device *sdev = to_scsi_device(dev);
48 int err = -ENODEV;
49
50 if (sdev->type != TYPE_ENCLOSURE)
51 goto out;
52
53 err = 0;
54 sdev_printk(KERN_NOTICE, sdev, "Attached Enclosure device\n");
55
56 out:
57 return err;
58 }
59
60 #define SES_TIMEOUT (30 * HZ)
61 #define SES_RETRIES 3
62
init_device_slot_control(unsigned char * dest_desc,struct enclosure_component * ecomp,unsigned char * status)63 static void init_device_slot_control(unsigned char *dest_desc,
64 struct enclosure_component *ecomp,
65 unsigned char *status)
66 {
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;
74 }
75
76
ses_recv_diag(struct scsi_device * sdev,int page_code,void * buf,int bufflen)77 static int ses_recv_diag(struct scsi_device *sdev, int page_code,
78 void *buf, int bufflen)
79 {
80 int ret;
81 unsigned char cmd[] = {
82 RECEIVE_DIAGNOSTIC,
83 1, /* Set PCV bit */
84 page_code,
85 bufflen >> 8,
86 bufflen & 0xff,
87 0
88 };
89 unsigned char recv_page_code;
90 unsigned int retries = SES_RETRIES;
91 struct scsi_sense_hdr sshdr;
92
93 do {
94 ret = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buf, bufflen,
95 &sshdr, SES_TIMEOUT, 1, NULL);
96 } while (ret > 0 && --retries && scsi_sense_valid(&sshdr) &&
97 (sshdr.sense_key == NOT_READY ||
98 (sshdr.sense_key == UNIT_ATTENTION && sshdr.asc == 0x29)));
99
100 if (unlikely(ret))
101 return ret;
102
103 recv_page_code = ((unsigned char *)buf)[0];
104
105 if (likely(recv_page_code == page_code))
106 return ret;
107
108 /* successful diagnostic but wrong page code. This happens to some
109 * USB devices, just print a message and pretend there was an error */
110
111 sdev_printk(KERN_ERR, sdev,
112 "Wrong diagnostic page; asked for %d got %u\n",
113 page_code, recv_page_code);
114
115 return -EINVAL;
116 }
117
ses_send_diag(struct scsi_device * sdev,int page_code,void * buf,int bufflen)118 static int ses_send_diag(struct scsi_device *sdev, int page_code,
119 void *buf, int bufflen)
120 {
121 int result;
122
123 unsigned char cmd[] = {
124 SEND_DIAGNOSTIC,
125 0x10, /* Set PF bit */
126 0,
127 bufflen >> 8,
128 bufflen & 0xff,
129 0
130 };
131 struct scsi_sense_hdr sshdr;
132 unsigned int retries = SES_RETRIES;
133
134 do {
135 result = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, buf, bufflen,
136 &sshdr, SES_TIMEOUT, 1, NULL);
137 } while (result > 0 && --retries && scsi_sense_valid(&sshdr) &&
138 (sshdr.sense_key == NOT_READY ||
139 (sshdr.sense_key == UNIT_ATTENTION && sshdr.asc == 0x29)));
140
141 if (result)
142 sdev_printk(KERN_ERR, sdev, "SEND DIAGNOSTIC result: %8x\n",
143 result);
144 return result;
145 }
146
ses_set_page2_descriptor(struct enclosure_device * edev,struct enclosure_component * ecomp,unsigned char * desc)147 static int ses_set_page2_descriptor(struct enclosure_device *edev,
148 struct enclosure_component *ecomp,
149 unsigned char *desc)
150 {
151 int i, j, count = 0, descriptor = ecomp->number;
152 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
153 struct ses_device *ses_dev = edev->scratch;
154 unsigned char *type_ptr = ses_dev->page1_types;
155 unsigned char *desc_ptr = ses_dev->page2 + 8;
156
157 /* Clear everything */
158 memset(desc_ptr, 0, ses_dev->page2_len - 8);
159 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
160 for (j = 0; j < type_ptr[1]; j++) {
161 desc_ptr += 4;
162 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
163 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
164 continue;
165 if (count++ == descriptor) {
166 memcpy(desc_ptr, desc, 4);
167 /* set select */
168 desc_ptr[0] |= 0x80;
169 /* clear reserved, just in case */
170 desc_ptr[0] &= 0xf0;
171 }
172 }
173 }
174
175 return ses_send_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len);
176 }
177
ses_get_page2_descriptor(struct enclosure_device * edev,struct enclosure_component * ecomp)178 static unsigned char *ses_get_page2_descriptor(struct enclosure_device *edev,
179 struct enclosure_component *ecomp)
180 {
181 int i, j, count = 0, descriptor = ecomp->number;
182 struct scsi_device *sdev = to_scsi_device(edev->edev.parent);
183 struct ses_device *ses_dev = edev->scratch;
184 unsigned char *type_ptr = ses_dev->page1_types;
185 unsigned char *desc_ptr = ses_dev->page2 + 8;
186
187 if (ses_recv_diag(sdev, 2, ses_dev->page2, ses_dev->page2_len) < 0)
188 return NULL;
189
190 for (i = 0; i < ses_dev->page1_num_types; i++, type_ptr += 4) {
191 for (j = 0; j < type_ptr[1]; j++) {
192 desc_ptr += 4;
193 if (type_ptr[0] != ENCLOSURE_COMPONENT_DEVICE &&
194 type_ptr[0] != ENCLOSURE_COMPONENT_ARRAY_DEVICE)
195 continue;
196 if (count++ == descriptor)
197 return desc_ptr;
198 }
199 }
200 return NULL;
201 }
202
203 /* For device slot and array device slot elements, byte 3 bit 6
204 * is "fault sensed" while byte 3 bit 5 is "fault reqstd". As this
205 * code stands these bits are shifted 4 positions right so in
206 * sysfs they will appear as bits 2 and 1 respectively. Strange. */
ses_get_fault(struct enclosure_device * edev,struct enclosure_component * ecomp)207 static void ses_get_fault(struct enclosure_device *edev,
208 struct enclosure_component *ecomp)
209 {
210 unsigned char *desc;
211
212 if (!ses_page2_supported(edev)) {
213 ecomp->fault = 0;
214 return;
215 }
216 desc = ses_get_page2_descriptor(edev, ecomp);
217 if (desc)
218 ecomp->fault = (desc[3] & 0x60) >> 4;
219 }
220
ses_set_fault(struct enclosure_device * edev,struct enclosure_component * ecomp,enum enclosure_component_setting val)221 static int ses_set_fault(struct enclosure_device *edev,
222 struct enclosure_component *ecomp,
223 enum enclosure_component_setting val)
224 {
225 unsigned char desc[4];
226 unsigned char *desc_ptr;
227
228 if (!ses_page2_supported(edev))
229 return -EINVAL;
230
231 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
232
233 if (!desc_ptr)
234 return -EIO;
235
236 init_device_slot_control(desc, ecomp, desc_ptr);
237
238 switch (val) {
239 case ENCLOSURE_SETTING_DISABLED:
240 desc[3] &= 0xdf;
241 break;
242 case ENCLOSURE_SETTING_ENABLED:
243 desc[3] |= 0x20;
244 break;
245 default:
246 /* SES doesn't do the SGPIO blink settings */
247 return -EINVAL;
248 }
249
250 return ses_set_page2_descriptor(edev, ecomp, desc);
251 }
252
ses_get_status(struct enclosure_device * edev,struct enclosure_component * ecomp)253 static void ses_get_status(struct enclosure_device *edev,
254 struct enclosure_component *ecomp)
255 {
256 unsigned char *desc;
257
258 if (!ses_page2_supported(edev)) {
259 ecomp->status = 0;
260 return;
261 }
262 desc = ses_get_page2_descriptor(edev, ecomp);
263 if (desc)
264 ecomp->status = (desc[0] & 0x0f);
265 }
266
ses_get_locate(struct enclosure_device * edev,struct enclosure_component * ecomp)267 static void ses_get_locate(struct enclosure_device *edev,
268 struct enclosure_component *ecomp)
269 {
270 unsigned char *desc;
271
272 if (!ses_page2_supported(edev)) {
273 ecomp->locate = 0;
274 return;
275 }
276 desc = ses_get_page2_descriptor(edev, ecomp);
277 if (desc)
278 ecomp->locate = (desc[2] & 0x02) ? 1 : 0;
279 }
280
ses_set_locate(struct enclosure_device * edev,struct enclosure_component * ecomp,enum enclosure_component_setting val)281 static int ses_set_locate(struct enclosure_device *edev,
282 struct enclosure_component *ecomp,
283 enum enclosure_component_setting val)
284 {
285 unsigned char desc[4];
286 unsigned char *desc_ptr;
287
288 if (!ses_page2_supported(edev))
289 return -EINVAL;
290
291 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
292
293 if (!desc_ptr)
294 return -EIO;
295
296 init_device_slot_control(desc, ecomp, desc_ptr);
297
298 switch (val) {
299 case ENCLOSURE_SETTING_DISABLED:
300 desc[2] &= 0xfd;
301 break;
302 case ENCLOSURE_SETTING_ENABLED:
303 desc[2] |= 0x02;
304 break;
305 default:
306 /* SES doesn't do the SGPIO blink settings */
307 return -EINVAL;
308 }
309 return ses_set_page2_descriptor(edev, ecomp, desc);
310 }
311
ses_set_active(struct enclosure_device * edev,struct enclosure_component * ecomp,enum enclosure_component_setting val)312 static int ses_set_active(struct enclosure_device *edev,
313 struct enclosure_component *ecomp,
314 enum enclosure_component_setting val)
315 {
316 unsigned char desc[4];
317 unsigned char *desc_ptr;
318
319 if (!ses_page2_supported(edev))
320 return -EINVAL;
321
322 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
323
324 if (!desc_ptr)
325 return -EIO;
326
327 init_device_slot_control(desc, ecomp, desc_ptr);
328
329 switch (val) {
330 case ENCLOSURE_SETTING_DISABLED:
331 desc[2] &= 0x7f;
332 ecomp->active = 0;
333 break;
334 case ENCLOSURE_SETTING_ENABLED:
335 desc[2] |= 0x80;
336 ecomp->active = 1;
337 break;
338 default:
339 /* SES doesn't do the SGPIO blink settings */
340 return -EINVAL;
341 }
342 return ses_set_page2_descriptor(edev, ecomp, desc);
343 }
344
ses_show_id(struct enclosure_device * edev,char * buf)345 static int ses_show_id(struct enclosure_device *edev, char *buf)
346 {
347 struct ses_device *ses_dev = edev->scratch;
348 unsigned long long id = get_unaligned_be64(ses_dev->page1+8+4);
349
350 return sprintf(buf, "%#llx\n", id);
351 }
352
ses_get_power_status(struct enclosure_device * edev,struct enclosure_component * ecomp)353 static void ses_get_power_status(struct enclosure_device *edev,
354 struct enclosure_component *ecomp)
355 {
356 unsigned char *desc;
357
358 if (!ses_page2_supported(edev)) {
359 ecomp->power_status = 0;
360 return;
361 }
362
363 desc = ses_get_page2_descriptor(edev, ecomp);
364 if (desc)
365 ecomp->power_status = (desc[3] & 0x10) ? 0 : 1;
366 }
367
ses_set_power_status(struct enclosure_device * edev,struct enclosure_component * ecomp,int val)368 static int ses_set_power_status(struct enclosure_device *edev,
369 struct enclosure_component *ecomp,
370 int val)
371 {
372 unsigned char desc[4];
373 unsigned char *desc_ptr;
374
375 if (!ses_page2_supported(edev))
376 return -EINVAL;
377
378 desc_ptr = ses_get_page2_descriptor(edev, ecomp);
379
380 if (!desc_ptr)
381 return -EIO;
382
383 init_device_slot_control(desc, ecomp, desc_ptr);
384
385 switch (val) {
386 /* power = 1 is device_off = 0 and vice versa */
387 case 0:
388 desc[3] |= 0x10;
389 break;
390 case 1:
391 desc[3] &= 0xef;
392 break;
393 default:
394 return -EINVAL;
395 }
396 ecomp->power_status = val;
397 return ses_set_page2_descriptor(edev, ecomp, desc);
398 }
399
400 static struct enclosure_component_callbacks ses_enclosure_callbacks = {
401 .get_fault = ses_get_fault,
402 .set_fault = ses_set_fault,
403 .get_status = ses_get_status,
404 .get_locate = ses_get_locate,
405 .set_locate = ses_set_locate,
406 .get_power_status = ses_get_power_status,
407 .set_power_status = ses_set_power_status,
408 .set_active = ses_set_active,
409 .show_id = ses_show_id,
410 };
411
412 struct ses_host_edev {
413 struct Scsi_Host *shost;
414 struct enclosure_device *edev;
415 };
416
417 #if 0
418 int ses_match_host(struct enclosure_device *edev, void *data)
419 {
420 struct ses_host_edev *sed = data;
421 struct scsi_device *sdev;
422
423 if (!scsi_is_sdev_device(edev->edev.parent))
424 return 0;
425
426 sdev = to_scsi_device(edev->edev.parent);
427
428 if (sdev->host != sed->shost)
429 return 0;
430
431 sed->edev = edev;
432 return 1;
433 }
434 #endif /* 0 */
435
ses_process_descriptor(struct enclosure_component * ecomp,unsigned char * desc)436 static void ses_process_descriptor(struct enclosure_component *ecomp,
437 unsigned char *desc)
438 {
439 int eip = desc[0] & 0x10;
440 int invalid = desc[0] & 0x80;
441 enum scsi_protocol proto = desc[0] & 0x0f;
442 u64 addr = 0;
443 int slot = -1;
444 struct ses_component *scomp = ecomp->scratch;
445 unsigned char *d;
446
447 if (invalid)
448 return;
449
450 switch (proto) {
451 case SCSI_PROTOCOL_FCP:
452 if (eip) {
453 d = desc + 4;
454 slot = d[3];
455 }
456 break;
457 case SCSI_PROTOCOL_SAS:
458 if (eip) {
459 d = desc + 4;
460 slot = d[3];
461 d = desc + 8;
462 } else
463 d = desc + 4;
464 /* only take the phy0 addr */
465 addr = (u64)d[12] << 56 |
466 (u64)d[13] << 48 |
467 (u64)d[14] << 40 |
468 (u64)d[15] << 32 |
469 (u64)d[16] << 24 |
470 (u64)d[17] << 16 |
471 (u64)d[18] << 8 |
472 (u64)d[19];
473 break;
474 default:
475 /* FIXME: Need to add more protocols than just SAS */
476 break;
477 }
478 ecomp->slot = slot;
479 scomp->addr = addr;
480 }
481
482 struct efd {
483 u64 addr;
484 struct device *dev;
485 };
486
ses_enclosure_find_by_addr(struct enclosure_device * edev,void * data)487 static int ses_enclosure_find_by_addr(struct enclosure_device *edev,
488 void *data)
489 {
490 struct efd *efd = data;
491 int i;
492 struct ses_component *scomp;
493
494 if (!edev->component[0].scratch)
495 return 0;
496
497 for (i = 0; i < edev->components; i++) {
498 scomp = edev->component[i].scratch;
499 if (scomp->addr != efd->addr)
500 continue;
501
502 if (enclosure_add_device(edev, i, efd->dev) == 0)
503 kobject_uevent(&efd->dev->kobj, KOBJ_CHANGE);
504 return 1;
505 }
506 return 0;
507 }
508
509 #define INIT_ALLOC_SIZE 32
510
ses_enclosure_data_process(struct enclosure_device * edev,struct scsi_device * sdev,int create)511 static void ses_enclosure_data_process(struct enclosure_device *edev,
512 struct scsi_device *sdev,
513 int create)
514 {
515 u32 result;
516 unsigned char *buf = NULL, *type_ptr, *desc_ptr, *addl_desc_ptr = NULL;
517 int i, j, page7_len, len, components;
518 struct ses_device *ses_dev = edev->scratch;
519 int types = ses_dev->page1_num_types;
520 unsigned char *hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
521
522 if (!hdr_buf)
523 goto simple_populate;
524
525 /* re-read page 10 */
526 if (ses_dev->page10)
527 ses_recv_diag(sdev, 10, ses_dev->page10, ses_dev->page10_len);
528 /* Page 7 for the descriptors is optional */
529 result = ses_recv_diag(sdev, 7, hdr_buf, INIT_ALLOC_SIZE);
530 if (result)
531 goto simple_populate;
532
533 page7_len = len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
534 /* add 1 for trailing '\0' we'll use */
535 buf = kzalloc(len + 1, GFP_KERNEL);
536 if (!buf)
537 goto simple_populate;
538 result = ses_recv_diag(sdev, 7, buf, len);
539 if (result) {
540 simple_populate:
541 kfree(buf);
542 buf = NULL;
543 desc_ptr = NULL;
544 len = 0;
545 page7_len = 0;
546 } else {
547 desc_ptr = buf + 8;
548 len = (desc_ptr[2] << 8) + desc_ptr[3];
549 /* skip past overall descriptor */
550 desc_ptr += len + 4;
551 }
552 if (ses_dev->page10)
553 addl_desc_ptr = ses_dev->page10 + 8;
554 type_ptr = ses_dev->page1_types;
555 components = 0;
556 for (i = 0; i < types; i++, type_ptr += 4) {
557 for (j = 0; j < type_ptr[1]; j++) {
558 char *name = NULL;
559 struct enclosure_component *ecomp;
560
561 if (desc_ptr) {
562 if (desc_ptr >= buf + page7_len) {
563 desc_ptr = NULL;
564 } else {
565 len = (desc_ptr[2] << 8) + desc_ptr[3];
566 desc_ptr += 4;
567 /* Add trailing zero - pushes into
568 * reserved space */
569 desc_ptr[len] = '\0';
570 name = desc_ptr;
571 }
572 }
573 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
574 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) {
575
576 if (create)
577 ecomp = enclosure_component_alloc(
578 edev,
579 components++,
580 type_ptr[0],
581 name);
582 else
583 ecomp = &edev->component[components++];
584
585 if (!IS_ERR(ecomp)) {
586 if (addl_desc_ptr)
587 ses_process_descriptor(
588 ecomp,
589 addl_desc_ptr);
590 if (create)
591 enclosure_component_register(
592 ecomp);
593 }
594 }
595 if (desc_ptr)
596 desc_ptr += len;
597
598 if (addl_desc_ptr &&
599 /* only find additional descriptions for specific devices */
600 (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
601 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE ||
602 type_ptr[0] == ENCLOSURE_COMPONENT_SAS_EXPANDER ||
603 /* these elements are optional */
604 type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_TARGET_PORT ||
605 type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_INITIATOR_PORT ||
606 type_ptr[0] == ENCLOSURE_COMPONENT_CONTROLLER_ELECTRONICS))
607 addl_desc_ptr += addl_desc_ptr[1] + 2;
608
609 }
610 }
611 kfree(buf);
612 kfree(hdr_buf);
613 }
614
ses_match_to_enclosure(struct enclosure_device * edev,struct scsi_device * sdev,int refresh)615 static void ses_match_to_enclosure(struct enclosure_device *edev,
616 struct scsi_device *sdev,
617 int refresh)
618 {
619 struct scsi_device *edev_sdev = to_scsi_device(edev->edev.parent);
620 struct efd efd = {
621 .addr = 0,
622 };
623
624 if (refresh)
625 ses_enclosure_data_process(edev, edev_sdev, 0);
626
627 if (scsi_is_sas_rphy(sdev->sdev_target->dev.parent))
628 efd.addr = sas_get_address(sdev);
629
630 if (efd.addr) {
631 efd.dev = &sdev->sdev_gendev;
632
633 enclosure_for_each_device(ses_enclosure_find_by_addr, &efd);
634 }
635 }
636
ses_intf_add(struct device * cdev,struct class_interface * intf)637 static int ses_intf_add(struct device *cdev,
638 struct class_interface *intf)
639 {
640 struct scsi_device *sdev = to_scsi_device(cdev->parent);
641 struct scsi_device *tmp_sdev;
642 unsigned char *buf = NULL, *hdr_buf, *type_ptr, page;
643 struct ses_device *ses_dev;
644 u32 result;
645 int i, types, len, components = 0;
646 int err = -ENOMEM;
647 int num_enclosures;
648 struct enclosure_device *edev;
649 struct ses_component *scomp = NULL;
650
651 if (!scsi_device_enclosure(sdev)) {
652 /* not an enclosure, but might be in one */
653 struct enclosure_device *prev = NULL;
654
655 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
656 ses_match_to_enclosure(edev, sdev, 1);
657 prev = edev;
658 }
659 return -ENODEV;
660 }
661
662 /* TYPE_ENCLOSURE prints a message in probe */
663 if (sdev->type != TYPE_ENCLOSURE)
664 sdev_printk(KERN_NOTICE, sdev, "Embedded Enclosure Device\n");
665
666 ses_dev = kzalloc(sizeof(*ses_dev), GFP_KERNEL);
667 hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
668 if (!hdr_buf || !ses_dev)
669 goto err_init_free;
670
671 page = 1;
672 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
673 if (result)
674 goto recv_failed;
675
676 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
677 buf = kzalloc(len, GFP_KERNEL);
678 if (!buf)
679 goto err_free;
680
681 result = ses_recv_diag(sdev, page, buf, len);
682 if (result)
683 goto recv_failed;
684
685 types = 0;
686
687 /* we always have one main enclosure and the rest are referred
688 * to as secondary subenclosures */
689 num_enclosures = buf[1] + 1;
690
691 /* begin at the enclosure descriptor */
692 type_ptr = buf + 8;
693 /* skip all the enclosure descriptors */
694 for (i = 0; i < num_enclosures && type_ptr < buf + len; i++) {
695 types += type_ptr[2];
696 type_ptr += type_ptr[3] + 4;
697 }
698
699 ses_dev->page1_types = type_ptr;
700 ses_dev->page1_num_types = types;
701
702 for (i = 0; i < types && type_ptr < buf + len; i++, type_ptr += 4) {
703 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
704 type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE)
705 components += type_ptr[1];
706 }
707 ses_dev->page1 = buf;
708 ses_dev->page1_len = len;
709 buf = NULL;
710
711 page = 2;
712 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
713 if (result)
714 goto page2_not_supported;
715
716 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
717 buf = kzalloc(len, GFP_KERNEL);
718 if (!buf)
719 goto err_free;
720
721 /* make sure getting page 2 actually works */
722 result = ses_recv_diag(sdev, 2, buf, len);
723 if (result)
724 goto recv_failed;
725 ses_dev->page2 = buf;
726 ses_dev->page2_len = len;
727 buf = NULL;
728
729 /* The additional information page --- allows us
730 * to match up the devices */
731 page = 10;
732 result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
733 if (!result) {
734
735 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
736 buf = kzalloc(len, GFP_KERNEL);
737 if (!buf)
738 goto err_free;
739
740 result = ses_recv_diag(sdev, page, buf, len);
741 if (result)
742 goto recv_failed;
743 ses_dev->page10 = buf;
744 ses_dev->page10_len = len;
745 buf = NULL;
746 }
747 page2_not_supported:
748 scomp = kcalloc(components, sizeof(struct ses_component), GFP_KERNEL);
749 if (!scomp)
750 goto err_free;
751
752 edev = enclosure_register(cdev->parent, dev_name(&sdev->sdev_gendev),
753 components, &ses_enclosure_callbacks);
754 if (IS_ERR(edev)) {
755 err = PTR_ERR(edev);
756 goto err_free;
757 }
758
759 kfree(hdr_buf);
760
761 edev->scratch = ses_dev;
762 for (i = 0; i < components; i++)
763 edev->component[i].scratch = scomp + i;
764
765 ses_enclosure_data_process(edev, sdev, 1);
766
767 /* see if there are any devices matching before
768 * we found the enclosure */
769 shost_for_each_device(tmp_sdev, sdev->host) {
770 if (tmp_sdev->lun != 0 || scsi_device_enclosure(tmp_sdev))
771 continue;
772 ses_match_to_enclosure(edev, tmp_sdev, 0);
773 }
774
775 return 0;
776
777 recv_failed:
778 sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n",
779 page);
780 err = -ENODEV;
781 err_free:
782 kfree(buf);
783 kfree(scomp);
784 kfree(ses_dev->page10);
785 kfree(ses_dev->page2);
786 kfree(ses_dev->page1);
787 err_init_free:
788 kfree(ses_dev);
789 kfree(hdr_buf);
790 sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err);
791 return err;
792 }
793
ses_remove(struct device * dev)794 static int ses_remove(struct device *dev)
795 {
796 return 0;
797 }
798
ses_intf_remove_component(struct scsi_device * sdev)799 static void ses_intf_remove_component(struct scsi_device *sdev)
800 {
801 struct enclosure_device *edev, *prev = NULL;
802
803 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
804 prev = edev;
805 if (!enclosure_remove_device(edev, &sdev->sdev_gendev))
806 break;
807 }
808 if (edev)
809 put_device(&edev->edev);
810 }
811
ses_intf_remove_enclosure(struct scsi_device * sdev)812 static void ses_intf_remove_enclosure(struct scsi_device *sdev)
813 {
814 struct enclosure_device *edev;
815 struct ses_device *ses_dev;
816
817 /* exact match to this enclosure */
818 edev = enclosure_find(&sdev->sdev_gendev, NULL);
819 if (!edev)
820 return;
821
822 ses_dev = edev->scratch;
823 edev->scratch = NULL;
824
825 kfree(ses_dev->page10);
826 kfree(ses_dev->page1);
827 kfree(ses_dev->page2);
828 kfree(ses_dev);
829
830 kfree(edev->component[0].scratch);
831
832 put_device(&edev->edev);
833 enclosure_unregister(edev);
834 }
835
ses_intf_remove(struct device * cdev,struct class_interface * intf)836 static void ses_intf_remove(struct device *cdev,
837 struct class_interface *intf)
838 {
839 struct scsi_device *sdev = to_scsi_device(cdev->parent);
840
841 if (!scsi_device_enclosure(sdev))
842 ses_intf_remove_component(sdev);
843 else
844 ses_intf_remove_enclosure(sdev);
845 }
846
847 static struct class_interface ses_interface = {
848 .add_dev = ses_intf_add,
849 .remove_dev = ses_intf_remove,
850 };
851
852 static struct scsi_driver ses_template = {
853 .gendrv = {
854 .name = "ses",
855 .owner = THIS_MODULE,
856 .probe = ses_probe,
857 .remove = ses_remove,
858 },
859 };
860
ses_init(void)861 static int __init ses_init(void)
862 {
863 int err;
864
865 err = scsi_register_interface(&ses_interface);
866 if (err)
867 return err;
868
869 err = scsi_register_driver(&ses_template.gendrv);
870 if (err)
871 goto out_unreg;
872
873 return 0;
874
875 out_unreg:
876 scsi_unregister_interface(&ses_interface);
877 return err;
878 }
879
ses_exit(void)880 static void __exit ses_exit(void)
881 {
882 scsi_unregister_driver(&ses_template.gendrv);
883 scsi_unregister_interface(&ses_interface);
884 }
885
886 module_init(ses_init);
887 module_exit(ses_exit);
888
889 MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE);
890
891 MODULE_AUTHOR("James Bottomley");
892 MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
893 MODULE_LICENSE("GPL v2");
894