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