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