lib/test_meminit: fix off-by-one error in test_pages()
[platform/kernel/linux-starfive.git] / drivers / scsi / ses.c
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
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
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
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
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
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
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
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. */
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
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
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
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
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
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
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
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
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
436 static int ses_process_descriptor(struct enclosure_component *ecomp,
437                                    unsigned char *desc, int max_desc_len)
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 0;
449
450         switch (proto) {
451         case SCSI_PROTOCOL_FCP:
452                 if (eip) {
453                         if (max_desc_len <= 7)
454                                 return 1;
455                         d = desc + 4;
456                         slot = d[3];
457                 }
458                 break;
459         case SCSI_PROTOCOL_SAS:
460
461                 if (eip) {
462                         if (max_desc_len <= 27)
463                                 return 1;
464                         d = desc + 4;
465                         slot = d[3];
466                         d = desc + 8;
467                 } else {
468                         if (max_desc_len <= 23)
469                                 return 1;
470                         d = desc + 4;
471                 }
472
473
474                 /* only take the phy0 addr */
475                 addr = (u64)d[12] << 56 |
476                         (u64)d[13] << 48 |
477                         (u64)d[14] << 40 |
478                         (u64)d[15] << 32 |
479                         (u64)d[16] << 24 |
480                         (u64)d[17] << 16 |
481                         (u64)d[18] << 8 |
482                         (u64)d[19];
483                 break;
484         default:
485                 /* FIXME: Need to add more protocols than just SAS */
486                 break;
487         }
488         ecomp->slot = slot;
489         scomp->addr = addr;
490
491         return 0;
492 }
493
494 struct efd {
495         u64 addr;
496         struct device *dev;
497 };
498
499 static int ses_enclosure_find_by_addr(struct enclosure_device *edev,
500                                       void *data)
501 {
502         struct efd *efd = data;
503         int i;
504         struct ses_component *scomp;
505
506         for (i = 0; i < edev->components; i++) {
507                 scomp = edev->component[i].scratch;
508                 if (scomp->addr != efd->addr)
509                         continue;
510
511                 if (enclosure_add_device(edev, i, efd->dev) == 0)
512                         kobject_uevent(&efd->dev->kobj, KOBJ_CHANGE);
513                 return 1;
514         }
515         return 0;
516 }
517
518 #define INIT_ALLOC_SIZE 32
519
520 static void ses_enclosure_data_process(struct enclosure_device *edev,
521                                        struct scsi_device *sdev,
522                                        int create)
523 {
524         u32 result;
525         unsigned char *buf = NULL, *type_ptr, *desc_ptr, *addl_desc_ptr = NULL;
526         int i, j, page7_len, len, components;
527         struct ses_device *ses_dev = edev->scratch;
528         int types = ses_dev->page1_num_types;
529         unsigned char *hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
530
531         if (!hdr_buf)
532                 goto simple_populate;
533
534         /* re-read page 10 */
535         if (ses_dev->page10)
536                 ses_recv_diag(sdev, 10, ses_dev->page10, ses_dev->page10_len);
537         /* Page 7 for the descriptors is optional */
538         result = ses_recv_diag(sdev, 7, hdr_buf, INIT_ALLOC_SIZE);
539         if (result)
540                 goto simple_populate;
541
542         page7_len = len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
543         /* add 1 for trailing '\0' we'll use */
544         buf = kzalloc(len + 1, GFP_KERNEL);
545         if (!buf)
546                 goto simple_populate;
547         result = ses_recv_diag(sdev, 7, buf, len);
548         if (result) {
549  simple_populate:
550                 kfree(buf);
551                 buf = NULL;
552                 desc_ptr = NULL;
553                 len = 0;
554                 page7_len = 0;
555         } else {
556                 desc_ptr = buf + 8;
557                 len = (desc_ptr[2] << 8) + desc_ptr[3];
558                 /* skip past overall descriptor */
559                 desc_ptr += len + 4;
560         }
561         if (ses_dev->page10 && ses_dev->page10_len > 9)
562                 addl_desc_ptr = ses_dev->page10 + 8;
563         type_ptr = ses_dev->page1_types;
564         components = 0;
565         for (i = 0; i < types; i++, type_ptr += 4) {
566                 for (j = 0; j < type_ptr[1]; j++) {
567                         char *name = NULL;
568                         struct enclosure_component *ecomp;
569                         int max_desc_len;
570
571                         if (desc_ptr) {
572                                 if (desc_ptr + 3 >= buf + page7_len) {
573                                         desc_ptr = NULL;
574                                 } else {
575                                         len = (desc_ptr[2] << 8) + desc_ptr[3];
576                                         desc_ptr += 4;
577                                         if (desc_ptr + len > buf + page7_len)
578                                                 desc_ptr = NULL;
579                                         else {
580                                                 /* Add trailing zero - pushes into
581                                                  * reserved space */
582                                                 desc_ptr[len] = '\0';
583                                                 name = desc_ptr;
584                                         }
585                                 }
586                         }
587                         if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
588                             type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE) {
589
590                                 if (create)
591                                         ecomp = enclosure_component_alloc(
592                                                 edev,
593                                                 components++,
594                                                 type_ptr[0],
595                                                 name);
596                                 else if (components < edev->components)
597                                         ecomp = &edev->component[components++];
598                                 else
599                                         ecomp = ERR_PTR(-EINVAL);
600
601                                 if (!IS_ERR(ecomp)) {
602                                         if (addl_desc_ptr) {
603                                                 max_desc_len = ses_dev->page10_len -
604                                                     (addl_desc_ptr - ses_dev->page10);
605                                                 if (ses_process_descriptor(ecomp,
606                                                     addl_desc_ptr,
607                                                     max_desc_len))
608                                                         addl_desc_ptr = NULL;
609                                         }
610                                         if (create)
611                                                 enclosure_component_register(
612                                                         ecomp);
613                                 }
614                         }
615                         if (desc_ptr)
616                                 desc_ptr += len;
617
618                         if (addl_desc_ptr &&
619                             /* only find additional descriptions for specific devices */
620                             (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
621                              type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE ||
622                              type_ptr[0] == ENCLOSURE_COMPONENT_SAS_EXPANDER ||
623                              /* these elements are optional */
624                              type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_TARGET_PORT ||
625                              type_ptr[0] == ENCLOSURE_COMPONENT_SCSI_INITIATOR_PORT ||
626                              type_ptr[0] == ENCLOSURE_COMPONENT_CONTROLLER_ELECTRONICS)) {
627                                 addl_desc_ptr += addl_desc_ptr[1] + 2;
628                                 if (addl_desc_ptr + 1 >= ses_dev->page10 + ses_dev->page10_len)
629                                         addl_desc_ptr = NULL;
630                         }
631                 }
632         }
633         kfree(buf);
634         kfree(hdr_buf);
635 }
636
637 static void ses_match_to_enclosure(struct enclosure_device *edev,
638                                    struct scsi_device *sdev,
639                                    int refresh)
640 {
641         struct scsi_device *edev_sdev = to_scsi_device(edev->edev.parent);
642         struct efd efd = {
643                 .addr = 0,
644         };
645
646         if (refresh)
647                 ses_enclosure_data_process(edev, edev_sdev, 0);
648
649         if (scsi_is_sas_rphy(sdev->sdev_target->dev.parent))
650                 efd.addr = sas_get_address(sdev);
651
652         if (efd.addr) {
653                 efd.dev = &sdev->sdev_gendev;
654
655                 enclosure_for_each_device(ses_enclosure_find_by_addr, &efd);
656         }
657 }
658
659 static int ses_intf_add(struct device *cdev,
660                         struct class_interface *intf)
661 {
662         struct scsi_device *sdev = to_scsi_device(cdev->parent);
663         struct scsi_device *tmp_sdev;
664         unsigned char *buf = NULL, *hdr_buf, *type_ptr, page;
665         struct ses_device *ses_dev;
666         u32 result;
667         int i, types, len, components = 0;
668         int err = -ENOMEM;
669         int num_enclosures;
670         struct enclosure_device *edev;
671         struct ses_component *scomp = NULL;
672
673         if (!scsi_device_enclosure(sdev)) {
674                 /* not an enclosure, but might be in one */
675                 struct enclosure_device *prev = NULL;
676
677                 while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
678                         ses_match_to_enclosure(edev, sdev, 1);
679                         prev = edev;
680                 }
681                 return -ENODEV;
682         }
683
684         /* TYPE_ENCLOSURE prints a message in probe */
685         if (sdev->type != TYPE_ENCLOSURE)
686                 sdev_printk(KERN_NOTICE, sdev, "Embedded Enclosure Device\n");
687
688         ses_dev = kzalloc(sizeof(*ses_dev), GFP_KERNEL);
689         hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
690         if (!hdr_buf || !ses_dev)
691                 goto err_init_free;
692
693         page = 1;
694         result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
695         if (result)
696                 goto recv_failed;
697
698         len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
699         buf = kzalloc(len, GFP_KERNEL);
700         if (!buf)
701                 goto err_free;
702
703         result = ses_recv_diag(sdev, page, buf, len);
704         if (result)
705                 goto recv_failed;
706
707         types = 0;
708
709         /* we always have one main enclosure and the rest are referred
710          * to as secondary subenclosures */
711         num_enclosures = buf[1] + 1;
712
713         /* begin at the enclosure descriptor */
714         type_ptr = buf + 8;
715         /* skip all the enclosure descriptors */
716         for (i = 0; i < num_enclosures && type_ptr < buf + len; i++) {
717                 types += type_ptr[2];
718                 type_ptr += type_ptr[3] + 4;
719         }
720
721         ses_dev->page1_types = type_ptr;
722         ses_dev->page1_num_types = types;
723
724         for (i = 0; i < types && type_ptr < buf + len; i++, type_ptr += 4) {
725                 if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
726                     type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE)
727                         components += type_ptr[1];
728         }
729
730         ses_dev->page1 = buf;
731         ses_dev->page1_len = len;
732         buf = NULL;
733
734         page = 2;
735         result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
736         if (result)
737                 goto page2_not_supported;
738
739         len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
740         buf = kzalloc(len, GFP_KERNEL);
741         if (!buf)
742                 goto err_free;
743
744         /* make sure getting page 2 actually works */
745         result = ses_recv_diag(sdev, 2, buf, len);
746         if (result)
747                 goto recv_failed;
748         ses_dev->page2 = buf;
749         ses_dev->page2_len = len;
750         buf = NULL;
751
752         /* The additional information page --- allows us
753          * to match up the devices */
754         page = 10;
755         result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
756         if (!result) {
757
758                 len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
759                 buf = kzalloc(len, GFP_KERNEL);
760                 if (!buf)
761                         goto err_free;
762
763                 result = ses_recv_diag(sdev, page, buf, len);
764                 if (result)
765                         goto recv_failed;
766                 ses_dev->page10 = buf;
767                 ses_dev->page10_len = len;
768                 buf = NULL;
769         }
770 page2_not_supported:
771         if (components > 0) {
772                 scomp = kcalloc(components, sizeof(struct ses_component), GFP_KERNEL);
773                 if (!scomp)
774                         goto err_free;
775         }
776
777         edev = enclosure_register(cdev->parent, dev_name(&sdev->sdev_gendev),
778                                   components, &ses_enclosure_callbacks);
779         if (IS_ERR(edev)) {
780                 err = PTR_ERR(edev);
781                 goto err_free;
782         }
783
784         kfree(hdr_buf);
785
786         edev->scratch = ses_dev;
787         for (i = 0; i < components; i++)
788                 edev->component[i].scratch = scomp + i;
789
790         ses_enclosure_data_process(edev, sdev, 1);
791
792         /* see if there are any devices matching before
793          * we found the enclosure */
794         shost_for_each_device(tmp_sdev, sdev->host) {
795                 if (tmp_sdev->lun != 0 || scsi_device_enclosure(tmp_sdev))
796                         continue;
797                 ses_match_to_enclosure(edev, tmp_sdev, 0);
798         }
799
800         return 0;
801
802  recv_failed:
803         sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n",
804                     page);
805         err = -ENODEV;
806  err_free:
807         kfree(buf);
808         kfree(scomp);
809         kfree(ses_dev->page10);
810         kfree(ses_dev->page2);
811         kfree(ses_dev->page1);
812  err_init_free:
813         kfree(ses_dev);
814         kfree(hdr_buf);
815         sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err);
816         return err;
817 }
818
819 static int ses_remove(struct device *dev)
820 {
821         return 0;
822 }
823
824 static void ses_intf_remove_component(struct scsi_device *sdev)
825 {
826         struct enclosure_device *edev, *prev = NULL;
827
828         while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
829                 prev = edev;
830                 if (!enclosure_remove_device(edev, &sdev->sdev_gendev))
831                         break;
832         }
833         if (edev)
834                 put_device(&edev->edev);
835 }
836
837 static void ses_intf_remove_enclosure(struct scsi_device *sdev)
838 {
839         struct enclosure_device *edev;
840         struct ses_device *ses_dev;
841
842         /*  exact match to this enclosure */
843         edev = enclosure_find(&sdev->sdev_gendev, NULL);
844         if (!edev)
845                 return;
846
847         ses_dev = edev->scratch;
848         edev->scratch = NULL;
849
850         kfree(ses_dev->page10);
851         kfree(ses_dev->page1);
852         kfree(ses_dev->page2);
853         kfree(ses_dev);
854
855         if (edev->components)
856                 kfree(edev->component[0].scratch);
857
858         put_device(&edev->edev);
859         enclosure_unregister(edev);
860 }
861
862 static void ses_intf_remove(struct device *cdev,
863                             struct class_interface *intf)
864 {
865         struct scsi_device *sdev = to_scsi_device(cdev->parent);
866
867         if (!scsi_device_enclosure(sdev))
868                 ses_intf_remove_component(sdev);
869         else
870                 ses_intf_remove_enclosure(sdev);
871 }
872
873 static struct class_interface ses_interface = {
874         .add_dev        = ses_intf_add,
875         .remove_dev     = ses_intf_remove,
876 };
877
878 static struct scsi_driver ses_template = {
879         .gendrv = {
880                 .name           = "ses",
881                 .owner          = THIS_MODULE,
882                 .probe          = ses_probe,
883                 .remove         = ses_remove,
884         },
885 };
886
887 static int __init ses_init(void)
888 {
889         int err;
890
891         err = scsi_register_interface(&ses_interface);
892         if (err)
893                 return err;
894
895         err = scsi_register_driver(&ses_template.gendrv);
896         if (err)
897                 goto out_unreg;
898
899         return 0;
900
901  out_unreg:
902         scsi_unregister_interface(&ses_interface);
903         return err;
904 }
905
906 static void __exit ses_exit(void)
907 {
908         scsi_unregister_driver(&ses_template.gendrv);
909         scsi_unregister_interface(&ses_interface);
910 }
911
912 module_init(ses_init);
913 module_exit(ses_exit);
914
915 MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE);
916
917 MODULE_AUTHOR("James Bottomley");
918 MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
919 MODULE_LICENSE("GPL v2");