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