xref: /linux/drivers/scsi/scsi_sysfs.c (revision 54a8a2220c936a47840c9a3d74910c5a56fae2ed)
1 /*
2  * scsi_sysfs.c
3  *
4  * SCSI sysfs interface routines.
5  *
6  * Created to pull SCSI mid layer sysfs routines into one file.
7  */
8 
9 #include <linux/config.h>
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/blkdev.h>
13 #include <linux/device.h>
14 
15 #include <scsi/scsi.h>
16 #include <scsi/scsi_device.h>
17 #include <scsi/scsi_host.h>
18 #include <scsi/scsi_tcq.h>
19 #include <scsi/scsi_transport.h>
20 
21 #include "scsi_priv.h"
22 #include "scsi_logging.h"
23 
24 static struct {
25 	enum scsi_device_state	value;
26 	char			*name;
27 } sdev_states[] = {
28 	{ SDEV_CREATED, "created" },
29 	{ SDEV_RUNNING, "running" },
30 	{ SDEV_CANCEL, "cancel" },
31 	{ SDEV_DEL, "deleted" },
32 	{ SDEV_QUIESCE, "quiesce" },
33 	{ SDEV_OFFLINE,	"offline" },
34 	{ SDEV_BLOCK,	"blocked" },
35 };
36 
37 const char *scsi_device_state_name(enum scsi_device_state state)
38 {
39 	int i;
40 	char *name = NULL;
41 
42 	for (i = 0; i < sizeof(sdev_states)/sizeof(sdev_states[0]); i++) {
43 		if (sdev_states[i].value == state) {
44 			name = sdev_states[i].name;
45 			break;
46 		}
47 	}
48 	return name;
49 }
50 
51 static struct {
52 	enum scsi_host_state	value;
53 	char			*name;
54 } shost_states[] = {
55 	{ SHOST_CREATED, "created" },
56 	{ SHOST_RUNNING, "running" },
57 	{ SHOST_CANCEL, "cancel" },
58 	{ SHOST_DEL, "deleted" },
59 	{ SHOST_RECOVERY, "recovery" },
60 	{ SHOST_CANCEL_RECOVERY, "cancel/recovery" },
61 	{ SHOST_DEL_RECOVERY, "deleted/recovery", },
62 };
63 const char *scsi_host_state_name(enum scsi_host_state state)
64 {
65 	int i;
66 	char *name = NULL;
67 
68 	for (i = 0; i < sizeof(shost_states)/sizeof(shost_states[0]); i++) {
69 		if (shost_states[i].value == state) {
70 			name = shost_states[i].name;
71 			break;
72 		}
73 	}
74 	return name;
75 }
76 
77 static int check_set(unsigned int *val, char *src)
78 {
79 	char *last;
80 
81 	if (strncmp(src, "-", 20) == 0) {
82 		*val = SCAN_WILD_CARD;
83 	} else {
84 		/*
85 		 * Doesn't check for int overflow
86 		 */
87 		*val = simple_strtoul(src, &last, 0);
88 		if (*last != '\0')
89 			return 1;
90 	}
91 	return 0;
92 }
93 
94 static int scsi_scan(struct Scsi_Host *shost, const char *str)
95 {
96 	char s1[15], s2[15], s3[15], junk;
97 	unsigned int channel, id, lun;
98 	int res;
99 
100 	res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk);
101 	if (res != 3)
102 		return -EINVAL;
103 	if (check_set(&channel, s1))
104 		return -EINVAL;
105 	if (check_set(&id, s2))
106 		return -EINVAL;
107 	if (check_set(&lun, s3))
108 		return -EINVAL;
109 	res = scsi_scan_host_selected(shost, channel, id, lun, 1);
110 	return res;
111 }
112 
113 /*
114  * shost_show_function: macro to create an attr function that can be used to
115  * show a non-bit field.
116  */
117 #define shost_show_function(name, field, format_string)			\
118 static ssize_t								\
119 show_##name (struct class_device *class_dev, char *buf)			\
120 {									\
121 	struct Scsi_Host *shost = class_to_shost(class_dev);		\
122 	return snprintf (buf, 20, format_string, shost->field);		\
123 }
124 
125 /*
126  * shost_rd_attr: macro to create a function and attribute variable for a
127  * read only field.
128  */
129 #define shost_rd_attr2(name, field, format_string)			\
130 	shost_show_function(name, field, format_string)			\
131 static CLASS_DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
132 
133 #define shost_rd_attr(field, format_string) \
134 shost_rd_attr2(field, field, format_string)
135 
136 /*
137  * Create the actual show/store functions and data structures.
138  */
139 
140 static ssize_t store_scan(struct class_device *class_dev, const char *buf,
141 			  size_t count)
142 {
143 	struct Scsi_Host *shost = class_to_shost(class_dev);
144 	int res;
145 
146 	res = scsi_scan(shost, buf);
147 	if (res == 0)
148 		res = count;
149 	return res;
150 };
151 static CLASS_DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
152 
153 static ssize_t
154 store_shost_state(struct class_device *class_dev, const char *buf, size_t count)
155 {
156 	int i;
157 	struct Scsi_Host *shost = class_to_shost(class_dev);
158 	enum scsi_host_state state = 0;
159 
160 	for (i = 0; i < sizeof(shost_states)/sizeof(shost_states[0]); i++) {
161 		const int len = strlen(shost_states[i].name);
162 		if (strncmp(shost_states[i].name, buf, len) == 0 &&
163 		   buf[len] == '\n') {
164 			state = shost_states[i].value;
165 			break;
166 		}
167 	}
168 	if (!state)
169 		return -EINVAL;
170 
171 	if (scsi_host_set_state(shost, state))
172 		return -EINVAL;
173 	return count;
174 }
175 
176 static ssize_t
177 show_shost_state(struct class_device *class_dev, char *buf)
178 {
179 	struct Scsi_Host *shost = class_to_shost(class_dev);
180 	const char *name = scsi_host_state_name(shost->shost_state);
181 
182 	if (!name)
183 		return -EINVAL;
184 
185 	return snprintf(buf, 20, "%s\n", name);
186 }
187 
188 static CLASS_DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
189 
190 shost_rd_attr(unique_id, "%u\n");
191 shost_rd_attr(host_busy, "%hu\n");
192 shost_rd_attr(cmd_per_lun, "%hd\n");
193 shost_rd_attr(sg_tablesize, "%hu\n");
194 shost_rd_attr(unchecked_isa_dma, "%d\n");
195 shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
196 
197 static struct class_device_attribute *scsi_sysfs_shost_attrs[] = {
198 	&class_device_attr_unique_id,
199 	&class_device_attr_host_busy,
200 	&class_device_attr_cmd_per_lun,
201 	&class_device_attr_sg_tablesize,
202 	&class_device_attr_unchecked_isa_dma,
203 	&class_device_attr_proc_name,
204 	&class_device_attr_scan,
205 	&class_device_attr_state,
206 	NULL
207 };
208 
209 static void scsi_device_cls_release(struct class_device *class_dev)
210 {
211 	struct scsi_device *sdev;
212 
213 	sdev = class_to_sdev(class_dev);
214 	put_device(&sdev->sdev_gendev);
215 }
216 
217 static void scsi_device_dev_release(struct device *dev)
218 {
219 	struct scsi_device *sdev;
220 	struct device *parent;
221 	struct scsi_target *starget;
222 	unsigned long flags;
223 
224 	parent = dev->parent;
225 	sdev = to_scsi_device(dev);
226 	starget = to_scsi_target(parent);
227 
228 	spin_lock_irqsave(sdev->host->host_lock, flags);
229 	starget->reap_ref++;
230 	list_del(&sdev->siblings);
231 	list_del(&sdev->same_target_siblings);
232 	list_del(&sdev->starved_entry);
233 	spin_unlock_irqrestore(sdev->host->host_lock, flags);
234 
235 	if (sdev->request_queue) {
236 		sdev->request_queue->queuedata = NULL;
237 		scsi_free_queue(sdev->request_queue);
238 		/* temporary expedient, try to catch use of queue lock
239 		 * after free of sdev */
240 		sdev->request_queue = NULL;
241 	}
242 
243 	scsi_target_reap(scsi_target(sdev));
244 
245 	kfree(sdev->inquiry);
246 	kfree(sdev);
247 
248 	if (parent)
249 		put_device(parent);
250 }
251 
252 static struct class sdev_class = {
253 	.name		= "scsi_device",
254 	.release	= scsi_device_cls_release,
255 };
256 
257 /* all probing is done in the individual ->probe routines */
258 static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
259 {
260 	struct scsi_device *sdp = to_scsi_device(dev);
261 	if (sdp->no_uld_attach)
262 		return 0;
263 	return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
264 }
265 
266 struct bus_type scsi_bus_type = {
267         .name		= "scsi",
268         .match		= scsi_bus_match,
269 };
270 
271 int scsi_sysfs_register(void)
272 {
273 	int error;
274 
275 	error = bus_register(&scsi_bus_type);
276 	if (!error) {
277 		error = class_register(&sdev_class);
278 		if (error)
279 			bus_unregister(&scsi_bus_type);
280 	}
281 
282 	return error;
283 }
284 
285 void scsi_sysfs_unregister(void)
286 {
287 	class_unregister(&sdev_class);
288 	bus_unregister(&scsi_bus_type);
289 }
290 
291 /*
292  * sdev_show_function: macro to create an attr function that can be used to
293  * show a non-bit field.
294  */
295 #define sdev_show_function(field, format_string)				\
296 static ssize_t								\
297 sdev_show_##field (struct device *dev, struct device_attribute *attr, char *buf)				\
298 {									\
299 	struct scsi_device *sdev;					\
300 	sdev = to_scsi_device(dev);					\
301 	return snprintf (buf, 20, format_string, sdev->field);		\
302 }									\
303 
304 /*
305  * sdev_rd_attr: macro to create a function and attribute variable for a
306  * read only field.
307  */
308 #define sdev_rd_attr(field, format_string)				\
309 	sdev_show_function(field, format_string)			\
310 static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
311 
312 
313 /*
314  * sdev_rd_attr: create a function and attribute variable for a
315  * read/write field.
316  */
317 #define sdev_rw_attr(field, format_string)				\
318 	sdev_show_function(field, format_string)				\
319 									\
320 static ssize_t								\
321 sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)	\
322 {									\
323 	struct scsi_device *sdev;					\
324 	sdev = to_scsi_device(dev);					\
325 	snscanf (buf, 20, format_string, &sdev->field);			\
326 	return count;							\
327 }									\
328 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
329 
330 /* Currently we don't export bit fields, but we might in future,
331  * so leave this code in */
332 #if 0
333 /*
334  * sdev_rd_attr: create a function and attribute variable for a
335  * read/write bit field.
336  */
337 #define sdev_rw_attr_bit(field)						\
338 	sdev_show_function(field, "%d\n")					\
339 									\
340 static ssize_t								\
341 sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)	\
342 {									\
343 	int ret;							\
344 	struct scsi_device *sdev;					\
345 	ret = scsi_sdev_check_buf_bit(buf);				\
346 	if (ret >= 0)	{						\
347 		sdev = to_scsi_device(dev);				\
348 		sdev->field = ret;					\
349 		ret = count;						\
350 	}								\
351 	return ret;							\
352 }									\
353 static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
354 
355 /*
356  * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
357  * else return -EINVAL.
358  */
359 static int scsi_sdev_check_buf_bit(const char *buf)
360 {
361 	if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
362 		if (buf[0] == '1')
363 			return 1;
364 		else if (buf[0] == '0')
365 			return 0;
366 		else
367 			return -EINVAL;
368 	} else
369 		return -EINVAL;
370 }
371 #endif
372 /*
373  * Create the actual show/store functions and data structures.
374  */
375 sdev_rd_attr (device_blocked, "%d\n");
376 sdev_rd_attr (queue_depth, "%d\n");
377 sdev_rd_attr (type, "%d\n");
378 sdev_rd_attr (scsi_level, "%d\n");
379 sdev_rd_attr (vendor, "%.8s\n");
380 sdev_rd_attr (model, "%.16s\n");
381 sdev_rd_attr (rev, "%.4s\n");
382 
383 static ssize_t
384 sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
385 {
386 	struct scsi_device *sdev;
387 	sdev = to_scsi_device(dev);
388 	return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
389 }
390 
391 static ssize_t
392 sdev_store_timeout (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
393 {
394 	struct scsi_device *sdev;
395 	int timeout;
396 	sdev = to_scsi_device(dev);
397 	sscanf (buf, "%d\n", &timeout);
398 	sdev->timeout = timeout * HZ;
399 	return count;
400 }
401 static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
402 
403 static ssize_t
404 store_rescan_field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
405 {
406 	scsi_rescan_device(dev);
407 	return count;
408 }
409 static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
410 
411 static ssize_t sdev_store_delete(struct device *dev, struct device_attribute *attr, const char *buf,
412 				 size_t count)
413 {
414 	scsi_remove_device(to_scsi_device(dev));
415 	return count;
416 };
417 static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
418 
419 static ssize_t
420 store_state_field(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
421 {
422 	int i;
423 	struct scsi_device *sdev = to_scsi_device(dev);
424 	enum scsi_device_state state = 0;
425 
426 	for (i = 0; i < sizeof(sdev_states)/sizeof(sdev_states[0]); i++) {
427 		const int len = strlen(sdev_states[i].name);
428 		if (strncmp(sdev_states[i].name, buf, len) == 0 &&
429 		   buf[len] == '\n') {
430 			state = sdev_states[i].value;
431 			break;
432 		}
433 	}
434 	if (!state)
435 		return -EINVAL;
436 
437 	if (scsi_device_set_state(sdev, state))
438 		return -EINVAL;
439 	return count;
440 }
441 
442 static ssize_t
443 show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
444 {
445 	struct scsi_device *sdev = to_scsi_device(dev);
446 	const char *name = scsi_device_state_name(sdev->sdev_state);
447 
448 	if (!name)
449 		return -EINVAL;
450 
451 	return snprintf(buf, 20, "%s\n", name);
452 }
453 
454 static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
455 
456 static ssize_t
457 show_queue_type_field(struct device *dev, struct device_attribute *attr, char *buf)
458 {
459 	struct scsi_device *sdev = to_scsi_device(dev);
460 	const char *name = "none";
461 
462 	if (sdev->ordered_tags)
463 		name = "ordered";
464 	else if (sdev->simple_tags)
465 		name = "simple";
466 
467 	return snprintf(buf, 20, "%s\n", name);
468 }
469 
470 static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
471 
472 static ssize_t
473 show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
474 {
475 	return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
476 }
477 
478 static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
479 
480 #define show_sdev_iostat(field)						\
481 static ssize_t								\
482 show_iostat_##field(struct device *dev, struct device_attribute *attr, char *buf)			\
483 {									\
484 	struct scsi_device *sdev = to_scsi_device(dev);			\
485 	unsigned long long count = atomic_read(&sdev->field);		\
486 	return snprintf(buf, 20, "0x%llx\n", count);			\
487 }									\
488 static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
489 
490 show_sdev_iostat(iorequest_cnt);
491 show_sdev_iostat(iodone_cnt);
492 show_sdev_iostat(ioerr_cnt);
493 
494 
495 /* Default template for device attributes.  May NOT be modified */
496 static struct device_attribute *scsi_sysfs_sdev_attrs[] = {
497 	&dev_attr_device_blocked,
498 	&dev_attr_queue_depth,
499 	&dev_attr_queue_type,
500 	&dev_attr_type,
501 	&dev_attr_scsi_level,
502 	&dev_attr_vendor,
503 	&dev_attr_model,
504 	&dev_attr_rev,
505 	&dev_attr_rescan,
506 	&dev_attr_delete,
507 	&dev_attr_state,
508 	&dev_attr_timeout,
509 	&dev_attr_iocounterbits,
510 	&dev_attr_iorequest_cnt,
511 	&dev_attr_iodone_cnt,
512 	&dev_attr_ioerr_cnt,
513 	NULL
514 };
515 
516 static ssize_t sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr, const char *buf,
517 					 size_t count)
518 {
519 	int depth, retval;
520 	struct scsi_device *sdev = to_scsi_device(dev);
521 	struct scsi_host_template *sht = sdev->host->hostt;
522 
523 	if (!sht->change_queue_depth)
524 		return -EINVAL;
525 
526 	depth = simple_strtoul(buf, NULL, 0);
527 
528 	if (depth < 1)
529 		return -EINVAL;
530 
531 	retval = sht->change_queue_depth(sdev, depth);
532 	if (retval < 0)
533 		return retval;
534 
535 	return count;
536 }
537 
538 static struct device_attribute sdev_attr_queue_depth_rw =
539 	__ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
540 	       sdev_store_queue_depth_rw);
541 
542 static ssize_t sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr, const char *buf,
543 					size_t count)
544 {
545 	struct scsi_device *sdev = to_scsi_device(dev);
546 	struct scsi_host_template *sht = sdev->host->hostt;
547 	int tag_type = 0, retval;
548 	int prev_tag_type = scsi_get_tag_type(sdev);
549 
550 	if (!sdev->tagged_supported || !sht->change_queue_type)
551 		return -EINVAL;
552 
553 	if (strncmp(buf, "ordered", 7) == 0)
554 		tag_type = MSG_ORDERED_TAG;
555 	else if (strncmp(buf, "simple", 6) == 0)
556 		tag_type = MSG_SIMPLE_TAG;
557 	else if (strncmp(buf, "none", 4) != 0)
558 		return -EINVAL;
559 
560 	if (tag_type == prev_tag_type)
561 		return count;
562 
563 	retval = sht->change_queue_type(sdev, tag_type);
564 	if (retval < 0)
565 		return retval;
566 
567 	return count;
568 }
569 
570 static struct device_attribute sdev_attr_queue_type_rw =
571 	__ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
572 	       sdev_store_queue_type_rw);
573 
574 static struct device_attribute *attr_changed_internally(
575 		struct Scsi_Host *shost,
576 		struct device_attribute * attr)
577 {
578 	if (!strcmp("queue_depth", attr->attr.name)
579 	    && shost->hostt->change_queue_depth)
580 		return &sdev_attr_queue_depth_rw;
581 	else if (!strcmp("queue_type", attr->attr.name)
582 	    && shost->hostt->change_queue_type)
583 		return &sdev_attr_queue_type_rw;
584 	return attr;
585 }
586 
587 
588 static struct device_attribute *attr_overridden(
589 		struct device_attribute **attrs,
590 		struct device_attribute *attr)
591 {
592 	int i;
593 
594 	if (!attrs)
595 		return NULL;
596 	for (i = 0; attrs[i]; i++)
597 		if (!strcmp(attrs[i]->attr.name, attr->attr.name))
598 			return attrs[i];
599 	return NULL;
600 }
601 
602 static int attr_add(struct device *dev, struct device_attribute *attr)
603 {
604 	struct device_attribute *base_attr;
605 
606 	/*
607 	 * Spare the caller from having to copy things it's not interested in.
608 	 */
609 	base_attr = attr_overridden(scsi_sysfs_sdev_attrs, attr);
610 	if (base_attr) {
611 		/* extend permissions */
612 		attr->attr.mode |= base_attr->attr.mode;
613 
614 		/* override null show/store with default */
615 		if (!attr->show)
616 			attr->show = base_attr->show;
617 		if (!attr->store)
618 			attr->store = base_attr->store;
619 	}
620 
621 	return device_create_file(dev, attr);
622 }
623 
624 /**
625  * scsi_sysfs_add_sdev - add scsi device to sysfs
626  * @sdev:	scsi_device to add
627  *
628  * Return value:
629  * 	0 on Success / non-zero on Failure
630  **/
631 int scsi_sysfs_add_sdev(struct scsi_device *sdev)
632 {
633 	int error, i;
634 
635 	if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
636 		return error;
637 
638 	error = device_add(&sdev->sdev_gendev);
639 	if (error) {
640 		put_device(sdev->sdev_gendev.parent);
641 		printk(KERN_INFO "error 1\n");
642 		return error;
643 	}
644 	error = class_device_add(&sdev->sdev_classdev);
645 	if (error) {
646 		printk(KERN_INFO "error 2\n");
647 		goto clean_device;
648 	}
649 
650 	/* take a reference for the sdev_classdev; this is
651 	 * released by the sdev_class .release */
652 	get_device(&sdev->sdev_gendev);
653 	if (sdev->host->hostt->sdev_attrs) {
654 		for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
655 			error = attr_add(&sdev->sdev_gendev,
656 					sdev->host->hostt->sdev_attrs[i]);
657 			if (error) {
658 				__scsi_remove_device(sdev);
659 				goto out;
660 			}
661 		}
662 	}
663 
664 	for (i = 0; scsi_sysfs_sdev_attrs[i]; i++) {
665 		if (!attr_overridden(sdev->host->hostt->sdev_attrs,
666 					scsi_sysfs_sdev_attrs[i])) {
667 			struct device_attribute * attr =
668 				attr_changed_internally(sdev->host,
669 							scsi_sysfs_sdev_attrs[i]);
670 			error = device_create_file(&sdev->sdev_gendev, attr);
671 			if (error) {
672 				__scsi_remove_device(sdev);
673 				goto out;
674 			}
675 		}
676 	}
677 
678 	transport_add_device(&sdev->sdev_gendev);
679  out:
680 	return error;
681 
682  clean_device:
683 	scsi_device_set_state(sdev, SDEV_CANCEL);
684 
685 	device_del(&sdev->sdev_gendev);
686 	transport_destroy_device(&sdev->sdev_gendev);
687 	put_device(&sdev->sdev_gendev);
688 
689 	return error;
690 }
691 
692 void __scsi_remove_device(struct scsi_device *sdev)
693 {
694 	if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
695 		return;
696 
697 	class_device_unregister(&sdev->sdev_classdev);
698 	device_del(&sdev->sdev_gendev);
699 	scsi_device_set_state(sdev, SDEV_DEL);
700 	if (sdev->host->hostt->slave_destroy)
701 		sdev->host->hostt->slave_destroy(sdev);
702 	transport_unregister_device(&sdev->sdev_gendev);
703 	put_device(&sdev->sdev_gendev);
704 }
705 
706 /**
707  * scsi_remove_device - unregister a device from the scsi bus
708  * @sdev:	scsi_device to unregister
709  **/
710 void scsi_remove_device(struct scsi_device *sdev)
711 {
712 	struct Scsi_Host *shost = sdev->host;
713 
714 	down(&shost->scan_mutex);
715 	__scsi_remove_device(sdev);
716 	up(&shost->scan_mutex);
717 }
718 EXPORT_SYMBOL(scsi_remove_device);
719 
720 void __scsi_remove_target(struct scsi_target *starget)
721 {
722 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
723 	unsigned long flags;
724 	struct scsi_device *sdev;
725 
726 	spin_lock_irqsave(shost->host_lock, flags);
727 	starget->reap_ref++;
728  restart:
729 	list_for_each_entry(sdev, &shost->__devices, siblings) {
730 		if (sdev->channel != starget->channel ||
731 		    sdev->id != starget->id ||
732 		    sdev->sdev_state == SDEV_DEL)
733 			continue;
734 		spin_unlock_irqrestore(shost->host_lock, flags);
735 		scsi_remove_device(sdev);
736 		spin_lock_irqsave(shost->host_lock, flags);
737 		goto restart;
738 	}
739 	spin_unlock_irqrestore(shost->host_lock, flags);
740 	scsi_target_reap(starget);
741 }
742 
743 static int __remove_child (struct device * dev, void * data)
744 {
745 	if (scsi_is_target_device(dev))
746 		__scsi_remove_target(to_scsi_target(dev));
747 	return 0;
748 }
749 
750 /**
751  * scsi_remove_target - try to remove a target and all its devices
752  * @dev: generic starget or parent of generic stargets to be removed
753  *
754  * Note: This is slightly racy.  It is possible that if the user
755  * requests the addition of another device then the target won't be
756  * removed.
757  */
758 void scsi_remove_target(struct device *dev)
759 {
760 	struct device *rdev;
761 
762 	if (scsi_is_target_device(dev)) {
763 		__scsi_remove_target(to_scsi_target(dev));
764 		return;
765 	}
766 
767 	rdev = get_device(dev);
768 	device_for_each_child(dev, NULL, __remove_child);
769 	put_device(rdev);
770 }
771 EXPORT_SYMBOL(scsi_remove_target);
772 
773 int scsi_register_driver(struct device_driver *drv)
774 {
775 	drv->bus = &scsi_bus_type;
776 
777 	return driver_register(drv);
778 }
779 EXPORT_SYMBOL(scsi_register_driver);
780 
781 int scsi_register_interface(struct class_interface *intf)
782 {
783 	intf->class = &sdev_class;
784 
785 	return class_interface_register(intf);
786 }
787 EXPORT_SYMBOL(scsi_register_interface);
788 
789 
790 static struct class_device_attribute *class_attr_overridden(
791 		struct class_device_attribute **attrs,
792 		struct class_device_attribute *attr)
793 {
794 	int i;
795 
796 	if (!attrs)
797 		return NULL;
798 	for (i = 0; attrs[i]; i++)
799 		if (!strcmp(attrs[i]->attr.name, attr->attr.name))
800 			return attrs[i];
801 	return NULL;
802 }
803 
804 static int class_attr_add(struct class_device *classdev,
805 		struct class_device_attribute *attr)
806 {
807 	struct class_device_attribute *base_attr;
808 
809 	/*
810 	 * Spare the caller from having to copy things it's not interested in.
811 	 */
812 	base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
813 	if (base_attr) {
814 		/* extend permissions */
815 		attr->attr.mode |= base_attr->attr.mode;
816 
817 		/* override null show/store with default */
818 		if (!attr->show)
819 			attr->show = base_attr->show;
820 		if (!attr->store)
821 			attr->store = base_attr->store;
822 	}
823 
824 	return class_device_create_file(classdev, attr);
825 }
826 
827 /**
828  * scsi_sysfs_add_host - add scsi host to subsystem
829  * @shost:     scsi host struct to add to subsystem
830  * @dev:       parent struct device pointer
831  **/
832 int scsi_sysfs_add_host(struct Scsi_Host *shost)
833 {
834 	int error, i;
835 
836 	if (shost->hostt->shost_attrs) {
837 		for (i = 0; shost->hostt->shost_attrs[i]; i++) {
838 			error = class_attr_add(&shost->shost_classdev,
839 					shost->hostt->shost_attrs[i]);
840 			if (error)
841 				return error;
842 		}
843 	}
844 
845 	for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
846 		if (!class_attr_overridden(shost->hostt->shost_attrs,
847 					scsi_sysfs_shost_attrs[i])) {
848 			error = class_device_create_file(&shost->shost_classdev,
849 					scsi_sysfs_shost_attrs[i]);
850 			if (error)
851 				return error;
852 		}
853 	}
854 
855 	transport_register_device(&shost->shost_gendev);
856 	return 0;
857 }
858 
859 void scsi_sysfs_device_initialize(struct scsi_device *sdev)
860 {
861 	unsigned long flags;
862 	struct Scsi_Host *shost = sdev->host;
863 	struct scsi_target  *starget = sdev->sdev_target;
864 
865 	device_initialize(&sdev->sdev_gendev);
866 	sdev->sdev_gendev.bus = &scsi_bus_type;
867 	sdev->sdev_gendev.release = scsi_device_dev_release;
868 	sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
869 		sdev->host->host_no, sdev->channel, sdev->id,
870 		sdev->lun);
871 
872 	class_device_initialize(&sdev->sdev_classdev);
873 	sdev->sdev_classdev.dev = &sdev->sdev_gendev;
874 	sdev->sdev_classdev.class = &sdev_class;
875 	snprintf(sdev->sdev_classdev.class_id, BUS_ID_SIZE,
876 		 "%d:%d:%d:%d", sdev->host->host_no,
877 		 sdev->channel, sdev->id, sdev->lun);
878 	sdev->scsi_level = SCSI_2;
879 	transport_setup_device(&sdev->sdev_gendev);
880 	spin_lock_irqsave(shost->host_lock, flags);
881 	list_add_tail(&sdev->same_target_siblings, &starget->devices);
882 	list_add_tail(&sdev->siblings, &shost->__devices);
883 	spin_unlock_irqrestore(shost->host_lock, flags);
884 }
885 
886 int scsi_is_sdev_device(const struct device *dev)
887 {
888 	return dev->release == scsi_device_dev_release;
889 }
890 EXPORT_SYMBOL(scsi_is_sdev_device);
891 
892 /* A blank transport template that is used in drivers that don't
893  * yet implement Transport Attributes */
894 struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };
895