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