xref: /openbmc/linux/drivers/scsi/ses.c (revision c8e22b7a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * SCSI Enclosure Services
4  *
5  * Copyright (C) 2008 James Bottomley <James.Bottomley@HansenPartnership.com>
6  */
7 
8 #include <linux/slab.h>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/enclosure.h>
12 #include <asm/unaligned.h>
13 
14 #include <scsi/scsi.h>
15 #include <scsi/scsi_cmnd.h>
16 #include <scsi/scsi_dbg.h>
17 #include <scsi/scsi_device.h>
18 #include <scsi/scsi_driver.h>
19 #include <scsi/scsi_host.h>
20 
21 #include <scsi/scsi_transport_sas.h>
22 
23 struct ses_device {
24 	unsigned char *page1;
25 	unsigned char *page1_types;
26 	unsigned char *page2;
27 	unsigned char *page10;
28 	short page1_len;
29 	short page1_num_types;
30 	short page2_len;
31 	short page10_len;
32 };
33 
34 struct ses_component {
35 	u64 addr;
36 };
37 
ses_page2_supported(struct enclosure_device * edev)38 static bool ses_page2_supported(struct enclosure_device *edev)
39 {
40 	struct ses_device *ses_dev = edev->scratch;
41 
42 	return (ses_dev->page2 != NULL);
43 }
44 
ses_probe(struct device * dev)45 static int ses_probe(struct device *dev)
46 {
47 	struct scsi_device *sdev = to_scsi_device(dev);
48 	int err = -ENODEV;
49 
50 	if (sdev->type != TYPE_ENCLOSURE)
51 		goto out;
52 
53 	err = 0;
54 	sdev_printk(KERN_NOTICE, sdev, "Attached Enclosure device\n");
55 
56  out:
57 	return err;
58 }
59 
60 #define SES_TIMEOUT (30 * HZ)
61 #define SES_RETRIES 3
62 
init_device_slot_control(unsigned char * dest_desc,struct enclosure_component * ecomp,unsigned char * status)63 static void init_device_slot_control(unsigned char *dest_desc,
64 				     struct enclosure_component *ecomp,
65 				     unsigned char *status)
66 {
67 	memcpy(dest_desc, status, 4);
68 	dest_desc[0] = 0;
69 	/* only clear byte 1 for ENCLOSURE_COMPONENT_DEVICE */
70 	if (ecomp->type == ENCLOSURE_COMPONENT_DEVICE)
71 		dest_desc[1] = 0;
72 	dest_desc[2] &= 0xde;
73 	dest_desc[3] &= 0x3c;
74 }
75 
76 
ses_recv_diag(struct scsi_device * sdev,int page_code,void * buf,int bufflen)77 static int ses_recv_diag(struct scsi_device *sdev, int page_code,
78 			 void *buf, int bufflen)
79 {
80 	int ret;
81 	unsigned char cmd[] = {
82 		RECEIVE_DIAGNOSTIC,
83 		1,		/* Set PCV bit */
84 		page_code,
85 		bufflen >> 8,
86 		bufflen & 0xff,
87 		0
88 	};
89 	unsigned char recv_page_code;
90 	unsigned int retries = SES_RETRIES;
91 	struct scsi_sense_hdr sshdr;
92 	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 
ses_send_diag(struct scsi_device * sdev,int page_code,void * buf,int bufflen)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 
ses_set_page2_descriptor(struct enclosure_device * edev,struct enclosure_component * ecomp,unsigned char * desc)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 
ses_get_page2_descriptor(struct enclosure_device * edev,struct enclosure_component * ecomp)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. */
ses_get_fault(struct enclosure_device * edev,struct enclosure_component * ecomp)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 
ses_set_fault(struct enclosure_device * edev,struct enclosure_component * ecomp,enum enclosure_component_setting val)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 
ses_get_status(struct enclosure_device * edev,struct enclosure_component * ecomp)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 
ses_get_locate(struct enclosure_device * edev,struct enclosure_component * ecomp)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 
ses_set_locate(struct enclosure_device * edev,struct enclosure_component * ecomp,enum enclosure_component_setting val)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 
ses_set_active(struct enclosure_device * edev,struct enclosure_component * ecomp,enum enclosure_component_setting val)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 
ses_show_id(struct enclosure_device * edev,char * buf)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 
ses_get_power_status(struct enclosure_device * edev,struct enclosure_component * ecomp)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 
ses_set_power_status(struct enclosure_device * edev,struct enclosure_component * ecomp,int val)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 
ses_process_descriptor(struct enclosure_component * ecomp,unsigned char * desc,int max_desc_len)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 
ses_enclosure_find_by_addr(struct enclosure_device * edev,void * data)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 
ses_enclosure_data_process(struct enclosure_device * edev,struct scsi_device * sdev,int create)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 
ses_match_to_enclosure(struct enclosure_device * edev,struct scsi_device * sdev,int refresh)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 
ses_intf_add(struct device * cdev)665 static int ses_intf_add(struct device *cdev)
666 {
667 	struct scsi_device *sdev = to_scsi_device(cdev->parent);
668 	struct scsi_device *tmp_sdev;
669 	unsigned char *buf = NULL, *hdr_buf, *type_ptr, page;
670 	struct ses_device *ses_dev;
671 	u32 result;
672 	int i, types, len, components = 0;
673 	int err = -ENOMEM;
674 	int num_enclosures;
675 	struct enclosure_device *edev;
676 	struct ses_component *scomp = NULL;
677 
678 	if (!scsi_device_enclosure(sdev)) {
679 		/* not an enclosure, but might be in one */
680 		struct enclosure_device *prev = NULL;
681 
682 		while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
683 			ses_match_to_enclosure(edev, sdev, 1);
684 			prev = edev;
685 		}
686 		return -ENODEV;
687 	}
688 
689 	/* TYPE_ENCLOSURE prints a message in probe */
690 	if (sdev->type != TYPE_ENCLOSURE)
691 		sdev_printk(KERN_NOTICE, sdev, "Embedded Enclosure Device\n");
692 
693 	ses_dev = kzalloc(sizeof(*ses_dev), GFP_KERNEL);
694 	hdr_buf = kzalloc(INIT_ALLOC_SIZE, GFP_KERNEL);
695 	if (!hdr_buf || !ses_dev)
696 		goto err_init_free;
697 
698 	page = 1;
699 	result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
700 	if (result)
701 		goto recv_failed;
702 
703 	len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
704 	buf = kzalloc(len, GFP_KERNEL);
705 	if (!buf)
706 		goto err_free;
707 
708 	result = ses_recv_diag(sdev, page, buf, len);
709 	if (result)
710 		goto recv_failed;
711 
712 	types = 0;
713 
714 	/* we always have one main enclosure and the rest are referred
715 	 * to as secondary subenclosures */
716 	num_enclosures = buf[1] + 1;
717 
718 	/* begin at the enclosure descriptor */
719 	type_ptr = buf + 8;
720 	/* skip all the enclosure descriptors */
721 	for (i = 0; i < num_enclosures && type_ptr < buf + len; i++) {
722 		types += type_ptr[2];
723 		type_ptr += type_ptr[3] + 4;
724 	}
725 
726 	ses_dev->page1_types = type_ptr;
727 	ses_dev->page1_num_types = types;
728 
729 	for (i = 0; i < types && type_ptr < buf + len; i++, type_ptr += 4) {
730 		if (type_ptr[0] == ENCLOSURE_COMPONENT_DEVICE ||
731 		    type_ptr[0] == ENCLOSURE_COMPONENT_ARRAY_DEVICE)
732 			components += type_ptr[1];
733 	}
734 
735 	ses_dev->page1 = buf;
736 	ses_dev->page1_len = len;
737 	buf = NULL;
738 
739 	page = 2;
740 	result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
741 	if (result)
742 		goto page2_not_supported;
743 
744 	len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
745 	buf = kzalloc(len, GFP_KERNEL);
746 	if (!buf)
747 		goto err_free;
748 
749 	/* make sure getting page 2 actually works */
750 	result = ses_recv_diag(sdev, 2, buf, len);
751 	if (result)
752 		goto recv_failed;
753 	ses_dev->page2 = buf;
754 	ses_dev->page2_len = len;
755 	buf = NULL;
756 
757 	/* The additional information page --- allows us
758 	 * to match up the devices */
759 	page = 10;
760 	result = ses_recv_diag(sdev, page, hdr_buf, INIT_ALLOC_SIZE);
761 	if (!result) {
762 
763 		len = (hdr_buf[2] << 8) + hdr_buf[3] + 4;
764 		buf = kzalloc(len, GFP_KERNEL);
765 		if (!buf)
766 			goto err_free;
767 
768 		result = ses_recv_diag(sdev, page, buf, len);
769 		if (result)
770 			goto recv_failed;
771 		ses_dev->page10 = buf;
772 		ses_dev->page10_len = len;
773 		buf = NULL;
774 	}
775 page2_not_supported:
776 	if (components > 0) {
777 		scomp = kcalloc(components, sizeof(struct ses_component), GFP_KERNEL);
778 		if (!scomp)
779 			goto err_free;
780 	}
781 
782 	edev = enclosure_register(cdev->parent, dev_name(&sdev->sdev_gendev),
783 				  components, &ses_enclosure_callbacks);
784 	if (IS_ERR(edev)) {
785 		err = PTR_ERR(edev);
786 		goto err_free;
787 	}
788 
789 	kfree(hdr_buf);
790 
791 	edev->scratch = ses_dev;
792 	for (i = 0; i < components; i++)
793 		edev->component[i].scratch = scomp + i;
794 
795 	ses_enclosure_data_process(edev, sdev, 1);
796 
797 	/* see if there are any devices matching before
798 	 * we found the enclosure */
799 	shost_for_each_device(tmp_sdev, sdev->host) {
800 		if (tmp_sdev->lun != 0 || scsi_device_enclosure(tmp_sdev))
801 			continue;
802 		ses_match_to_enclosure(edev, tmp_sdev, 0);
803 	}
804 
805 	return 0;
806 
807  recv_failed:
808 	sdev_printk(KERN_ERR, sdev, "Failed to get diagnostic page 0x%x\n",
809 		    page);
810 	err = -ENODEV;
811  err_free:
812 	kfree(buf);
813 	kfree(scomp);
814 	kfree(ses_dev->page10);
815 	kfree(ses_dev->page2);
816 	kfree(ses_dev->page1);
817  err_init_free:
818 	kfree(ses_dev);
819 	kfree(hdr_buf);
820 	sdev_printk(KERN_ERR, sdev, "Failed to bind enclosure %d\n", err);
821 	return err;
822 }
823 
ses_remove(struct device * dev)824 static int ses_remove(struct device *dev)
825 {
826 	return 0;
827 }
828 
ses_intf_remove_component(struct scsi_device * sdev)829 static void ses_intf_remove_component(struct scsi_device *sdev)
830 {
831 	struct enclosure_device *edev, *prev = NULL;
832 
833 	while ((edev = enclosure_find(&sdev->host->shost_gendev, prev)) != NULL) {
834 		prev = edev;
835 		if (!enclosure_remove_device(edev, &sdev->sdev_gendev))
836 			break;
837 	}
838 	if (edev)
839 		put_device(&edev->edev);
840 }
841 
ses_intf_remove_enclosure(struct scsi_device * sdev)842 static void ses_intf_remove_enclosure(struct scsi_device *sdev)
843 {
844 	struct enclosure_device *edev;
845 	struct ses_device *ses_dev;
846 
847 	/*  exact match to this enclosure */
848 	edev = enclosure_find(&sdev->sdev_gendev, NULL);
849 	if (!edev)
850 		return;
851 
852 	ses_dev = edev->scratch;
853 	edev->scratch = NULL;
854 
855 	kfree(ses_dev->page10);
856 	kfree(ses_dev->page1);
857 	kfree(ses_dev->page2);
858 	kfree(ses_dev);
859 
860 	if (edev->components)
861 		kfree(edev->component[0].scratch);
862 
863 	put_device(&edev->edev);
864 	enclosure_unregister(edev);
865 }
866 
ses_intf_remove(struct device * cdev)867 static void ses_intf_remove(struct device *cdev)
868 {
869 	struct scsi_device *sdev = to_scsi_device(cdev->parent);
870 
871 	if (!scsi_device_enclosure(sdev))
872 		ses_intf_remove_component(sdev);
873 	else
874 		ses_intf_remove_enclosure(sdev);
875 }
876 
877 static struct class_interface ses_interface = {
878 	.add_dev	= ses_intf_add,
879 	.remove_dev	= ses_intf_remove,
880 };
881 
882 static struct scsi_driver ses_template = {
883 	.gendrv = {
884 		.name		= "ses",
885 		.owner		= THIS_MODULE,
886 		.probe		= ses_probe,
887 		.remove		= ses_remove,
888 	},
889 };
890 
ses_init(void)891 static int __init ses_init(void)
892 {
893 	int err;
894 
895 	err = scsi_register_interface(&ses_interface);
896 	if (err)
897 		return err;
898 
899 	err = scsi_register_driver(&ses_template.gendrv);
900 	if (err)
901 		goto out_unreg;
902 
903 	return 0;
904 
905  out_unreg:
906 	scsi_unregister_interface(&ses_interface);
907 	return err;
908 }
909 
ses_exit(void)910 static void __exit ses_exit(void)
911 {
912 	scsi_unregister_driver(&ses_template.gendrv);
913 	scsi_unregister_interface(&ses_interface);
914 }
915 
916 module_init(ses_init);
917 module_exit(ses_exit);
918 
919 MODULE_ALIAS_SCSI_DEVICE(TYPE_ENCLOSURE);
920 
921 MODULE_AUTHOR("James Bottomley");
922 MODULE_DESCRIPTION("SCSI Enclosure Services (ses) driver");
923 MODULE_LICENSE("GPL v2");
924