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