xref: /openbmc/linux/drivers/scsi/scsi_sysfs.c (revision ffedb4522571ac170f941678d138a31bc0884ab4)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * scsi_sysfs.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * SCSI sysfs interface routines.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Created to pull SCSI mid layer sysfs routines into one file.
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/config.h>
101da177e4SLinus Torvalds #include <linux/module.h>
111da177e4SLinus Torvalds #include <linux/init.h>
121da177e4SLinus Torvalds #include <linux/blkdev.h>
131da177e4SLinus Torvalds #include <linux/device.h>
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds #include <scsi/scsi.h>
161da177e4SLinus Torvalds #include <scsi/scsi_device.h>
171da177e4SLinus Torvalds #include <scsi/scsi_host.h>
181da177e4SLinus Torvalds #include <scsi/scsi_tcq.h>
191da177e4SLinus Torvalds #include <scsi/scsi_transport.h>
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #include "scsi_priv.h"
221da177e4SLinus Torvalds #include "scsi_logging.h"
231da177e4SLinus Torvalds 
240ad78200SArjan van de Ven static const struct {
251da177e4SLinus Torvalds 	enum scsi_device_state	value;
261da177e4SLinus Torvalds 	char			*name;
271da177e4SLinus Torvalds } sdev_states[] = {
281da177e4SLinus Torvalds 	{ SDEV_CREATED, "created" },
291da177e4SLinus Torvalds 	{ SDEV_RUNNING, "running" },
301da177e4SLinus Torvalds 	{ SDEV_CANCEL, "cancel" },
311da177e4SLinus Torvalds 	{ SDEV_DEL, "deleted" },
321da177e4SLinus Torvalds 	{ SDEV_QUIESCE, "quiesce" },
331da177e4SLinus Torvalds 	{ SDEV_OFFLINE,	"offline" },
341da177e4SLinus Torvalds 	{ SDEV_BLOCK,	"blocked" },
351da177e4SLinus Torvalds };
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds const char *scsi_device_state_name(enum scsi_device_state state)
381da177e4SLinus Torvalds {
391da177e4SLinus Torvalds 	int i;
401da177e4SLinus Torvalds 	char *name = NULL;
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds 	for (i = 0; i < sizeof(sdev_states)/sizeof(sdev_states[0]); i++) {
431da177e4SLinus Torvalds 		if (sdev_states[i].value == state) {
441da177e4SLinus Torvalds 			name = sdev_states[i].name;
451da177e4SLinus Torvalds 			break;
461da177e4SLinus Torvalds 		}
471da177e4SLinus Torvalds 	}
481da177e4SLinus Torvalds 	return name;
491da177e4SLinus Torvalds }
501da177e4SLinus Torvalds 
510ad78200SArjan van de Ven static const struct {
52d3301874SMike Anderson 	enum scsi_host_state	value;
53d3301874SMike Anderson 	char			*name;
54d3301874SMike Anderson } shost_states[] = {
55d3301874SMike Anderson 	{ SHOST_CREATED, "created" },
56d3301874SMike Anderson 	{ SHOST_RUNNING, "running" },
57d3301874SMike Anderson 	{ SHOST_CANCEL, "cancel" },
58d3301874SMike Anderson 	{ SHOST_DEL, "deleted" },
59d3301874SMike Anderson 	{ SHOST_RECOVERY, "recovery" },
60939647eeSJames Bottomley 	{ SHOST_CANCEL_RECOVERY, "cancel/recovery" },
61939647eeSJames Bottomley 	{ SHOST_DEL_RECOVERY, "deleted/recovery", },
62d3301874SMike Anderson };
63d3301874SMike Anderson const char *scsi_host_state_name(enum scsi_host_state state)
64d3301874SMike Anderson {
65d3301874SMike Anderson 	int i;
66d3301874SMike Anderson 	char *name = NULL;
67d3301874SMike Anderson 
68d3301874SMike Anderson 	for (i = 0; i < sizeof(shost_states)/sizeof(shost_states[0]); i++) {
69d3301874SMike Anderson 		if (shost_states[i].value == state) {
70d3301874SMike Anderson 			name = shost_states[i].name;
71d3301874SMike Anderson 			break;
72d3301874SMike Anderson 		}
73d3301874SMike Anderson 	}
74d3301874SMike Anderson 	return name;
75d3301874SMike Anderson }
76d3301874SMike Anderson 
771da177e4SLinus Torvalds static int check_set(unsigned int *val, char *src)
781da177e4SLinus Torvalds {
791da177e4SLinus Torvalds 	char *last;
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds 	if (strncmp(src, "-", 20) == 0) {
821da177e4SLinus Torvalds 		*val = SCAN_WILD_CARD;
831da177e4SLinus Torvalds 	} else {
841da177e4SLinus Torvalds 		/*
851da177e4SLinus Torvalds 		 * Doesn't check for int overflow
861da177e4SLinus Torvalds 		 */
871da177e4SLinus Torvalds 		*val = simple_strtoul(src, &last, 0);
881da177e4SLinus Torvalds 		if (*last != '\0')
891da177e4SLinus Torvalds 			return 1;
901da177e4SLinus Torvalds 	}
911da177e4SLinus Torvalds 	return 0;
921da177e4SLinus Torvalds }
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds static int scsi_scan(struct Scsi_Host *shost, const char *str)
951da177e4SLinus Torvalds {
961da177e4SLinus Torvalds 	char s1[15], s2[15], s3[15], junk;
971da177e4SLinus Torvalds 	unsigned int channel, id, lun;
981da177e4SLinus Torvalds 	int res;
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 	res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk);
1011da177e4SLinus Torvalds 	if (res != 3)
1021da177e4SLinus Torvalds 		return -EINVAL;
1031da177e4SLinus Torvalds 	if (check_set(&channel, s1))
1041da177e4SLinus Torvalds 		return -EINVAL;
1051da177e4SLinus Torvalds 	if (check_set(&id, s2))
1061da177e4SLinus Torvalds 		return -EINVAL;
1071da177e4SLinus Torvalds 	if (check_set(&lun, s3))
1081da177e4SLinus Torvalds 		return -EINVAL;
109e02f3f59SChristoph Hellwig 	if (shost->transportt->user_scan)
110e02f3f59SChristoph Hellwig 		res = shost->transportt->user_scan(shost, channel, id, lun);
111e02f3f59SChristoph Hellwig 	else
1121da177e4SLinus Torvalds 		res = scsi_scan_host_selected(shost, channel, id, lun, 1);
1131da177e4SLinus Torvalds 	return res;
1141da177e4SLinus Torvalds }
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds /*
1171da177e4SLinus Torvalds  * shost_show_function: macro to create an attr function that can be used to
1181da177e4SLinus Torvalds  * show a non-bit field.
1191da177e4SLinus Torvalds  */
1201da177e4SLinus Torvalds #define shost_show_function(name, field, format_string)			\
1211da177e4SLinus Torvalds static ssize_t								\
1221da177e4SLinus Torvalds show_##name (struct class_device *class_dev, char *buf)			\
1231da177e4SLinus Torvalds {									\
1241da177e4SLinus Torvalds 	struct Scsi_Host *shost = class_to_shost(class_dev);		\
1251da177e4SLinus Torvalds 	return snprintf (buf, 20, format_string, shost->field);		\
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds /*
1291da177e4SLinus Torvalds  * shost_rd_attr: macro to create a function and attribute variable for a
1301da177e4SLinus Torvalds  * read only field.
1311da177e4SLinus Torvalds  */
1321da177e4SLinus Torvalds #define shost_rd_attr2(name, field, format_string)			\
1331da177e4SLinus Torvalds 	shost_show_function(name, field, format_string)			\
1341da177e4SLinus Torvalds static CLASS_DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds #define shost_rd_attr(field, format_string) \
1371da177e4SLinus Torvalds shost_rd_attr2(field, field, format_string)
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds /*
1401da177e4SLinus Torvalds  * Create the actual show/store functions and data structures.
1411da177e4SLinus Torvalds  */
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds static ssize_t store_scan(struct class_device *class_dev, const char *buf,
1441da177e4SLinus Torvalds 			  size_t count)
1451da177e4SLinus Torvalds {
1461da177e4SLinus Torvalds 	struct Scsi_Host *shost = class_to_shost(class_dev);
1471da177e4SLinus Torvalds 	int res;
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 	res = scsi_scan(shost, buf);
1501da177e4SLinus Torvalds 	if (res == 0)
1511da177e4SLinus Torvalds 		res = count;
1521da177e4SLinus Torvalds 	return res;
1531da177e4SLinus Torvalds };
1541da177e4SLinus Torvalds static CLASS_DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
1551da177e4SLinus Torvalds 
156d3301874SMike Anderson static ssize_t
157d3301874SMike Anderson store_shost_state(struct class_device *class_dev, const char *buf, size_t count)
158d3301874SMike Anderson {
159d3301874SMike Anderson 	int i;
160d3301874SMike Anderson 	struct Scsi_Host *shost = class_to_shost(class_dev);
161d3301874SMike Anderson 	enum scsi_host_state state = 0;
162d3301874SMike Anderson 
163d3301874SMike Anderson 	for (i = 0; i < sizeof(shost_states)/sizeof(shost_states[0]); i++) {
164d3301874SMike Anderson 		const int len = strlen(shost_states[i].name);
165d3301874SMike Anderson 		if (strncmp(shost_states[i].name, buf, len) == 0 &&
166d3301874SMike Anderson 		   buf[len] == '\n') {
167d3301874SMike Anderson 			state = shost_states[i].value;
168d3301874SMike Anderson 			break;
169d3301874SMike Anderson 		}
170d3301874SMike Anderson 	}
171d3301874SMike Anderson 	if (!state)
172d3301874SMike Anderson 		return -EINVAL;
173d3301874SMike Anderson 
174d3301874SMike Anderson 	if (scsi_host_set_state(shost, state))
175d3301874SMike Anderson 		return -EINVAL;
176d3301874SMike Anderson 	return count;
177d3301874SMike Anderson }
178d3301874SMike Anderson 
179d3301874SMike Anderson static ssize_t
180d3301874SMike Anderson show_shost_state(struct class_device *class_dev, char *buf)
181d3301874SMike Anderson {
182d3301874SMike Anderson 	struct Scsi_Host *shost = class_to_shost(class_dev);
183d3301874SMike Anderson 	const char *name = scsi_host_state_name(shost->shost_state);
184d3301874SMike Anderson 
185d3301874SMike Anderson 	if (!name)
186d3301874SMike Anderson 		return -EINVAL;
187d3301874SMike Anderson 
188d3301874SMike Anderson 	return snprintf(buf, 20, "%s\n", name);
189d3301874SMike Anderson }
190d3301874SMike Anderson 
191d3301874SMike Anderson static CLASS_DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
192d3301874SMike Anderson 
1931da177e4SLinus Torvalds shost_rd_attr(unique_id, "%u\n");
1941da177e4SLinus Torvalds shost_rd_attr(host_busy, "%hu\n");
1951da177e4SLinus Torvalds shost_rd_attr(cmd_per_lun, "%hd\n");
1961da177e4SLinus Torvalds shost_rd_attr(sg_tablesize, "%hu\n");
1971da177e4SLinus Torvalds shost_rd_attr(unchecked_isa_dma, "%d\n");
1981da177e4SLinus Torvalds shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds static struct class_device_attribute *scsi_sysfs_shost_attrs[] = {
2011da177e4SLinus Torvalds 	&class_device_attr_unique_id,
2021da177e4SLinus Torvalds 	&class_device_attr_host_busy,
2031da177e4SLinus Torvalds 	&class_device_attr_cmd_per_lun,
2041da177e4SLinus Torvalds 	&class_device_attr_sg_tablesize,
2051da177e4SLinus Torvalds 	&class_device_attr_unchecked_isa_dma,
2061da177e4SLinus Torvalds 	&class_device_attr_proc_name,
2071da177e4SLinus Torvalds 	&class_device_attr_scan,
208d3301874SMike Anderson 	&class_device_attr_state,
2091da177e4SLinus Torvalds 	NULL
2101da177e4SLinus Torvalds };
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds static void scsi_device_cls_release(struct class_device *class_dev)
2131da177e4SLinus Torvalds {
2141da177e4SLinus Torvalds 	struct scsi_device *sdev;
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	sdev = class_to_sdev(class_dev);
2171da177e4SLinus Torvalds 	put_device(&sdev->sdev_gendev);
2181da177e4SLinus Torvalds }
2191da177e4SLinus Torvalds 
22065110b21SJames Bottomley static void scsi_device_dev_release_usercontext(void *data)
2211da177e4SLinus Torvalds {
22265110b21SJames Bottomley 	struct device *dev = data;
2231da177e4SLinus Torvalds 	struct scsi_device *sdev;
2241da177e4SLinus Torvalds 	struct device *parent;
2251da177e4SLinus Torvalds 	struct scsi_target *starget;
2261da177e4SLinus Torvalds 	unsigned long flags;
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	parent = dev->parent;
2291da177e4SLinus Torvalds 	sdev = to_scsi_device(dev);
2301da177e4SLinus Torvalds 	starget = to_scsi_target(parent);
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	spin_lock_irqsave(sdev->host->host_lock, flags);
2331da177e4SLinus Torvalds 	starget->reap_ref++;
2341da177e4SLinus Torvalds 	list_del(&sdev->siblings);
2351da177e4SLinus Torvalds 	list_del(&sdev->same_target_siblings);
2361da177e4SLinus Torvalds 	list_del(&sdev->starved_entry);
2371da177e4SLinus Torvalds 	spin_unlock_irqrestore(sdev->host->host_lock, flags);
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	if (sdev->request_queue) {
2401da177e4SLinus Torvalds 		sdev->request_queue->queuedata = NULL;
24165110b21SJames Bottomley 		/* user context needed to free queue */
2421da177e4SLinus Torvalds 		scsi_free_queue(sdev->request_queue);
243c2a9331cS 		/* temporary expedient, try to catch use of queue lock
244c2a9331cS 		 * after free of sdev */
245c2a9331cS 		sdev->request_queue = NULL;
2461da177e4SLinus Torvalds 	}
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 	scsi_target_reap(scsi_target(sdev));
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 	kfree(sdev->inquiry);
2511da177e4SLinus Torvalds 	kfree(sdev);
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 	if (parent)
2541da177e4SLinus Torvalds 		put_device(parent);
2551da177e4SLinus Torvalds }
2561da177e4SLinus Torvalds 
25765110b21SJames Bottomley static void scsi_device_dev_release(struct device *dev)
25865110b21SJames Bottomley {
259*ffedb452SJames Bottomley 	struct scsi_device *sdp = to_scsi_device(dev);
260*ffedb452SJames Bottomley 	execute_in_process_context(scsi_device_dev_release_usercontext, dev,
261*ffedb452SJames Bottomley 				   &sdp->ew);
26265110b21SJames Bottomley }
26365110b21SJames Bottomley 
26452c1da39SAdrian Bunk static struct class sdev_class = {
2651da177e4SLinus Torvalds 	.name		= "scsi_device",
2661da177e4SLinus Torvalds 	.release	= scsi_device_cls_release,
2671da177e4SLinus Torvalds };
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds /* all probing is done in the individual ->probe routines */
2701da177e4SLinus Torvalds static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
2711da177e4SLinus Torvalds {
2721da177e4SLinus Torvalds 	struct scsi_device *sdp = to_scsi_device(dev);
2731da177e4SLinus Torvalds 	if (sdp->no_uld_attach)
2741da177e4SLinus Torvalds 		return 0;
2751da177e4SLinus Torvalds 	return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
2761da177e4SLinus Torvalds }
2771da177e4SLinus Torvalds 
2789b847548SJens Axboe static int scsi_bus_suspend(struct device * dev, pm_message_t state)
2799b847548SJens Axboe {
2809b847548SJens Axboe 	struct scsi_device *sdev = to_scsi_device(dev);
2819b847548SJens Axboe 	struct scsi_host_template *sht = sdev->host->hostt;
2829b847548SJens Axboe 	int err;
2839b847548SJens Axboe 
2849b847548SJens Axboe 	err = scsi_device_quiesce(sdev);
2859b847548SJens Axboe 	if (err)
2869b847548SJens Axboe 		return err;
2879b847548SJens Axboe 
2889b847548SJens Axboe 	if (sht->suspend)
2899b847548SJens Axboe 		err = sht->suspend(sdev);
2909b847548SJens Axboe 
2919b847548SJens Axboe 	return err;
2929b847548SJens Axboe }
2939b847548SJens Axboe 
2949b847548SJens Axboe static int scsi_bus_resume(struct device * dev)
2959b847548SJens Axboe {
2969b847548SJens Axboe 	struct scsi_device *sdev = to_scsi_device(dev);
2979b847548SJens Axboe 	struct scsi_host_template *sht = sdev->host->hostt;
2989b847548SJens Axboe 	int err = 0;
2999b847548SJens Axboe 
3009b847548SJens Axboe 	if (sht->resume)
3019b847548SJens Axboe 		err = sht->resume(sdev);
3029b847548SJens Axboe 
3039b847548SJens Axboe 	scsi_device_resume(sdev);
3049b847548SJens Axboe 	return err;
3059b847548SJens Axboe }
3069b847548SJens Axboe 
3071da177e4SLinus Torvalds struct bus_type scsi_bus_type = {
3081da177e4SLinus Torvalds         .name		= "scsi",
3091da177e4SLinus Torvalds         .match		= scsi_bus_match,
3109b847548SJens Axboe 	.suspend	= scsi_bus_suspend,
3119b847548SJens Axboe 	.resume		= scsi_bus_resume,
3121da177e4SLinus Torvalds };
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds int scsi_sysfs_register(void)
3151da177e4SLinus Torvalds {
3161da177e4SLinus Torvalds 	int error;
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 	error = bus_register(&scsi_bus_type);
3191da177e4SLinus Torvalds 	if (!error) {
3201da177e4SLinus Torvalds 		error = class_register(&sdev_class);
3211da177e4SLinus Torvalds 		if (error)
3221da177e4SLinus Torvalds 			bus_unregister(&scsi_bus_type);
3231da177e4SLinus Torvalds 	}
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds 	return error;
3261da177e4SLinus Torvalds }
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds void scsi_sysfs_unregister(void)
3291da177e4SLinus Torvalds {
3301da177e4SLinus Torvalds 	class_unregister(&sdev_class);
3311da177e4SLinus Torvalds 	bus_unregister(&scsi_bus_type);
3321da177e4SLinus Torvalds }
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds /*
3351da177e4SLinus Torvalds  * sdev_show_function: macro to create an attr function that can be used to
3361da177e4SLinus Torvalds  * show a non-bit field.
3371da177e4SLinus Torvalds  */
3381da177e4SLinus Torvalds #define sdev_show_function(field, format_string)				\
3391da177e4SLinus Torvalds static ssize_t								\
34010523b3bSYani Ioannou sdev_show_##field (struct device *dev, struct device_attribute *attr, char *buf)				\
3411da177e4SLinus Torvalds {									\
3421da177e4SLinus Torvalds 	struct scsi_device *sdev;					\
3431da177e4SLinus Torvalds 	sdev = to_scsi_device(dev);					\
3441da177e4SLinus Torvalds 	return snprintf (buf, 20, format_string, sdev->field);		\
3451da177e4SLinus Torvalds }									\
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds /*
3481da177e4SLinus Torvalds  * sdev_rd_attr: macro to create a function and attribute variable for a
3491da177e4SLinus Torvalds  * read only field.
3501da177e4SLinus Torvalds  */
3511da177e4SLinus Torvalds #define sdev_rd_attr(field, format_string)				\
3521da177e4SLinus Torvalds 	sdev_show_function(field, format_string)			\
3531da177e4SLinus Torvalds static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds /*
3571da177e4SLinus Torvalds  * sdev_rd_attr: create a function and attribute variable for a
3581da177e4SLinus Torvalds  * read/write field.
3591da177e4SLinus Torvalds  */
3601da177e4SLinus Torvalds #define sdev_rw_attr(field, format_string)				\
3611da177e4SLinus Torvalds 	sdev_show_function(field, format_string)				\
3621da177e4SLinus Torvalds 									\
3631da177e4SLinus Torvalds static ssize_t								\
36410523b3bSYani Ioannou sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)	\
3651da177e4SLinus Torvalds {									\
3661da177e4SLinus Torvalds 	struct scsi_device *sdev;					\
3671da177e4SLinus Torvalds 	sdev = to_scsi_device(dev);					\
3681da177e4SLinus Torvalds 	snscanf (buf, 20, format_string, &sdev->field);			\
3691da177e4SLinus Torvalds 	return count;							\
3701da177e4SLinus Torvalds }									\
3711da177e4SLinus Torvalds static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds /* Currently we don't export bit fields, but we might in future,
3741da177e4SLinus Torvalds  * so leave this code in */
3751da177e4SLinus Torvalds #if 0
3761da177e4SLinus Torvalds /*
3771da177e4SLinus Torvalds  * sdev_rd_attr: create a function and attribute variable for a
3781da177e4SLinus Torvalds  * read/write bit field.
3791da177e4SLinus Torvalds  */
3801da177e4SLinus Torvalds #define sdev_rw_attr_bit(field)						\
3811da177e4SLinus Torvalds 	sdev_show_function(field, "%d\n")					\
3821da177e4SLinus Torvalds 									\
3831da177e4SLinus Torvalds static ssize_t								\
38410523b3bSYani Ioannou sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)	\
3851da177e4SLinus Torvalds {									\
3861da177e4SLinus Torvalds 	int ret;							\
3871da177e4SLinus Torvalds 	struct scsi_device *sdev;					\
3881da177e4SLinus Torvalds 	ret = scsi_sdev_check_buf_bit(buf);				\
3891da177e4SLinus Torvalds 	if (ret >= 0)	{						\
3901da177e4SLinus Torvalds 		sdev = to_scsi_device(dev);				\
3911da177e4SLinus Torvalds 		sdev->field = ret;					\
3921da177e4SLinus Torvalds 		ret = count;						\
3931da177e4SLinus Torvalds 	}								\
3941da177e4SLinus Torvalds 	return ret;							\
3951da177e4SLinus Torvalds }									\
3961da177e4SLinus Torvalds static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds /*
3991da177e4SLinus Torvalds  * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
4001da177e4SLinus Torvalds  * else return -EINVAL.
4011da177e4SLinus Torvalds  */
4021da177e4SLinus Torvalds static int scsi_sdev_check_buf_bit(const char *buf)
4031da177e4SLinus Torvalds {
4041da177e4SLinus Torvalds 	if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
4051da177e4SLinus Torvalds 		if (buf[0] == '1')
4061da177e4SLinus Torvalds 			return 1;
4071da177e4SLinus Torvalds 		else if (buf[0] == '0')
4081da177e4SLinus Torvalds 			return 0;
4091da177e4SLinus Torvalds 		else
4101da177e4SLinus Torvalds 			return -EINVAL;
4111da177e4SLinus Torvalds 	} else
4121da177e4SLinus Torvalds 		return -EINVAL;
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds #endif
4151da177e4SLinus Torvalds /*
4161da177e4SLinus Torvalds  * Create the actual show/store functions and data structures.
4171da177e4SLinus Torvalds  */
4181da177e4SLinus Torvalds sdev_rd_attr (device_blocked, "%d\n");
4191da177e4SLinus Torvalds sdev_rd_attr (queue_depth, "%d\n");
4201da177e4SLinus Torvalds sdev_rd_attr (type, "%d\n");
4211da177e4SLinus Torvalds sdev_rd_attr (scsi_level, "%d\n");
4221da177e4SLinus Torvalds sdev_rd_attr (vendor, "%.8s\n");
4231da177e4SLinus Torvalds sdev_rd_attr (model, "%.16s\n");
4241da177e4SLinus Torvalds sdev_rd_attr (rev, "%.4s\n");
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds static ssize_t
42710523b3bSYani Ioannou sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
4281da177e4SLinus Torvalds {
4291da177e4SLinus Torvalds 	struct scsi_device *sdev;
4301da177e4SLinus Torvalds 	sdev = to_scsi_device(dev);
4311da177e4SLinus Torvalds 	return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
4321da177e4SLinus Torvalds }
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds static ssize_t
43510523b3bSYani Ioannou sdev_store_timeout (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
4361da177e4SLinus Torvalds {
4371da177e4SLinus Torvalds 	struct scsi_device *sdev;
4381da177e4SLinus Torvalds 	int timeout;
4391da177e4SLinus Torvalds 	sdev = to_scsi_device(dev);
4401da177e4SLinus Torvalds 	sscanf (buf, "%d\n", &timeout);
4411da177e4SLinus Torvalds 	sdev->timeout = timeout * HZ;
4421da177e4SLinus Torvalds 	return count;
4431da177e4SLinus Torvalds }
4441da177e4SLinus Torvalds static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds static ssize_t
44710523b3bSYani Ioannou store_rescan_field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
4481da177e4SLinus Torvalds {
4491da177e4SLinus Torvalds 	scsi_rescan_device(dev);
4501da177e4SLinus Torvalds 	return count;
4511da177e4SLinus Torvalds }
4521da177e4SLinus Torvalds static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
4531da177e4SLinus Torvalds 
45410523b3bSYani Ioannou static ssize_t sdev_store_delete(struct device *dev, struct device_attribute *attr, const char *buf,
4551da177e4SLinus Torvalds 				 size_t count)
4561da177e4SLinus Torvalds {
4571da177e4SLinus Torvalds 	scsi_remove_device(to_scsi_device(dev));
4581da177e4SLinus Torvalds 	return count;
4591da177e4SLinus Torvalds };
4601da177e4SLinus Torvalds static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds static ssize_t
46310523b3bSYani Ioannou store_state_field(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
4641da177e4SLinus Torvalds {
4651da177e4SLinus Torvalds 	int i;
4661da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
4671da177e4SLinus Torvalds 	enum scsi_device_state state = 0;
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	for (i = 0; i < sizeof(sdev_states)/sizeof(sdev_states[0]); i++) {
4701da177e4SLinus Torvalds 		const int len = strlen(sdev_states[i].name);
4711da177e4SLinus Torvalds 		if (strncmp(sdev_states[i].name, buf, len) == 0 &&
4721da177e4SLinus Torvalds 		   buf[len] == '\n') {
4731da177e4SLinus Torvalds 			state = sdev_states[i].value;
4741da177e4SLinus Torvalds 			break;
4751da177e4SLinus Torvalds 		}
4761da177e4SLinus Torvalds 	}
4771da177e4SLinus Torvalds 	if (!state)
4781da177e4SLinus Torvalds 		return -EINVAL;
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	if (scsi_device_set_state(sdev, state))
4811da177e4SLinus Torvalds 		return -EINVAL;
4821da177e4SLinus Torvalds 	return count;
4831da177e4SLinus Torvalds }
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds static ssize_t
48610523b3bSYani Ioannou show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
4871da177e4SLinus Torvalds {
4881da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
4891da177e4SLinus Torvalds 	const char *name = scsi_device_state_name(sdev->sdev_state);
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	if (!name)
4921da177e4SLinus Torvalds 		return -EINVAL;
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 	return snprintf(buf, 20, "%s\n", name);
4951da177e4SLinus Torvalds }
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds static ssize_t
50010523b3bSYani Ioannou show_queue_type_field(struct device *dev, struct device_attribute *attr, char *buf)
5011da177e4SLinus Torvalds {
5021da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
5031da177e4SLinus Torvalds 	const char *name = "none";
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	if (sdev->ordered_tags)
5061da177e4SLinus Torvalds 		name = "ordered";
5071da177e4SLinus Torvalds 	else if (sdev->simple_tags)
5081da177e4SLinus Torvalds 		name = "simple";
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	return snprintf(buf, 20, "%s\n", name);
5111da177e4SLinus Torvalds }
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds static ssize_t
51610523b3bSYani Ioannou show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
5171da177e4SLinus Torvalds {
5181da177e4SLinus Torvalds 	return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
5191da177e4SLinus Torvalds }
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds #define show_sdev_iostat(field)						\
5241da177e4SLinus Torvalds static ssize_t								\
52510523b3bSYani Ioannou show_iostat_##field(struct device *dev, struct device_attribute *attr, char *buf)			\
5261da177e4SLinus Torvalds {									\
5271da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);			\
5281da177e4SLinus Torvalds 	unsigned long long count = atomic_read(&sdev->field);		\
5291da177e4SLinus Torvalds 	return snprintf(buf, 20, "0x%llx\n", count);			\
5301da177e4SLinus Torvalds }									\
5311da177e4SLinus Torvalds static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds show_sdev_iostat(iorequest_cnt);
5341da177e4SLinus Torvalds show_sdev_iostat(iodone_cnt);
5351da177e4SLinus Torvalds show_sdev_iostat(ioerr_cnt);
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds /* Default template for device attributes.  May NOT be modified */
5391da177e4SLinus Torvalds static struct device_attribute *scsi_sysfs_sdev_attrs[] = {
5401da177e4SLinus Torvalds 	&dev_attr_device_blocked,
5411da177e4SLinus Torvalds 	&dev_attr_queue_depth,
5421da177e4SLinus Torvalds 	&dev_attr_queue_type,
5431da177e4SLinus Torvalds 	&dev_attr_type,
5441da177e4SLinus Torvalds 	&dev_attr_scsi_level,
5451da177e4SLinus Torvalds 	&dev_attr_vendor,
5461da177e4SLinus Torvalds 	&dev_attr_model,
5471da177e4SLinus Torvalds 	&dev_attr_rev,
5481da177e4SLinus Torvalds 	&dev_attr_rescan,
5491da177e4SLinus Torvalds 	&dev_attr_delete,
5501da177e4SLinus Torvalds 	&dev_attr_state,
5511da177e4SLinus Torvalds 	&dev_attr_timeout,
5521da177e4SLinus Torvalds 	&dev_attr_iocounterbits,
5531da177e4SLinus Torvalds 	&dev_attr_iorequest_cnt,
5541da177e4SLinus Torvalds 	&dev_attr_iodone_cnt,
5551da177e4SLinus Torvalds 	&dev_attr_ioerr_cnt,
5561da177e4SLinus Torvalds 	NULL
5571da177e4SLinus Torvalds };
5581da177e4SLinus Torvalds 
55910523b3bSYani Ioannou static ssize_t sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr, const char *buf,
5601da177e4SLinus Torvalds 					 size_t count)
5611da177e4SLinus Torvalds {
5621da177e4SLinus Torvalds 	int depth, retval;
5631da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
5641da177e4SLinus Torvalds 	struct scsi_host_template *sht = sdev->host->hostt;
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 	if (!sht->change_queue_depth)
5671da177e4SLinus Torvalds 		return -EINVAL;
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 	depth = simple_strtoul(buf, NULL, 0);
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 	if (depth < 1)
5721da177e4SLinus Torvalds 		return -EINVAL;
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 	retval = sht->change_queue_depth(sdev, depth);
5751da177e4SLinus Torvalds 	if (retval < 0)
5761da177e4SLinus Torvalds 		return retval;
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	return count;
5791da177e4SLinus Torvalds }
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds static struct device_attribute sdev_attr_queue_depth_rw =
5821da177e4SLinus Torvalds 	__ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
5831da177e4SLinus Torvalds 	       sdev_store_queue_depth_rw);
5841da177e4SLinus Torvalds 
58510523b3bSYani Ioannou static ssize_t sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr, const char *buf,
5861da177e4SLinus Torvalds 					size_t count)
5871da177e4SLinus Torvalds {
5881da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
5891da177e4SLinus Torvalds 	struct scsi_host_template *sht = sdev->host->hostt;
5901da177e4SLinus Torvalds 	int tag_type = 0, retval;
5911da177e4SLinus Torvalds 	int prev_tag_type = scsi_get_tag_type(sdev);
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds 	if (!sdev->tagged_supported || !sht->change_queue_type)
5941da177e4SLinus Torvalds 		return -EINVAL;
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	if (strncmp(buf, "ordered", 7) == 0)
5971da177e4SLinus Torvalds 		tag_type = MSG_ORDERED_TAG;
5981da177e4SLinus Torvalds 	else if (strncmp(buf, "simple", 6) == 0)
5991da177e4SLinus Torvalds 		tag_type = MSG_SIMPLE_TAG;
6001da177e4SLinus Torvalds 	else if (strncmp(buf, "none", 4) != 0)
6011da177e4SLinus Torvalds 		return -EINVAL;
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	if (tag_type == prev_tag_type)
6041da177e4SLinus Torvalds 		return count;
6051da177e4SLinus Torvalds 
6061da177e4SLinus Torvalds 	retval = sht->change_queue_type(sdev, tag_type);
6071da177e4SLinus Torvalds 	if (retval < 0)
6081da177e4SLinus Torvalds 		return retval;
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	return count;
6111da177e4SLinus Torvalds }
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds static struct device_attribute sdev_attr_queue_type_rw =
6141da177e4SLinus Torvalds 	__ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
6151da177e4SLinus Torvalds 	       sdev_store_queue_type_rw);
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds static struct device_attribute *attr_changed_internally(
6181da177e4SLinus Torvalds 		struct Scsi_Host *shost,
6191da177e4SLinus Torvalds 		struct device_attribute * attr)
6201da177e4SLinus Torvalds {
6211da177e4SLinus Torvalds 	if (!strcmp("queue_depth", attr->attr.name)
6221da177e4SLinus Torvalds 	    && shost->hostt->change_queue_depth)
6231da177e4SLinus Torvalds 		return &sdev_attr_queue_depth_rw;
6241da177e4SLinus Torvalds 	else if (!strcmp("queue_type", attr->attr.name)
6251da177e4SLinus Torvalds 	    && shost->hostt->change_queue_type)
6261da177e4SLinus Torvalds 		return &sdev_attr_queue_type_rw;
6271da177e4SLinus Torvalds 	return attr;
6281da177e4SLinus Torvalds }
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds static struct device_attribute *attr_overridden(
6321da177e4SLinus Torvalds 		struct device_attribute **attrs,
6331da177e4SLinus Torvalds 		struct device_attribute *attr)
6341da177e4SLinus Torvalds {
6351da177e4SLinus Torvalds 	int i;
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 	if (!attrs)
6381da177e4SLinus Torvalds 		return NULL;
6391da177e4SLinus Torvalds 	for (i = 0; attrs[i]; i++)
6401da177e4SLinus Torvalds 		if (!strcmp(attrs[i]->attr.name, attr->attr.name))
6411da177e4SLinus Torvalds 			return attrs[i];
6421da177e4SLinus Torvalds 	return NULL;
6431da177e4SLinus Torvalds }
6441da177e4SLinus Torvalds 
6451da177e4SLinus Torvalds static int attr_add(struct device *dev, struct device_attribute *attr)
6461da177e4SLinus Torvalds {
6471da177e4SLinus Torvalds 	struct device_attribute *base_attr;
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	/*
6501da177e4SLinus Torvalds 	 * Spare the caller from having to copy things it's not interested in.
6511da177e4SLinus Torvalds 	 */
6521da177e4SLinus Torvalds 	base_attr = attr_overridden(scsi_sysfs_sdev_attrs, attr);
6531da177e4SLinus Torvalds 	if (base_attr) {
6541da177e4SLinus Torvalds 		/* extend permissions */
6551da177e4SLinus Torvalds 		attr->attr.mode |= base_attr->attr.mode;
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 		/* override null show/store with default */
6581da177e4SLinus Torvalds 		if (!attr->show)
6591da177e4SLinus Torvalds 			attr->show = base_attr->show;
6601da177e4SLinus Torvalds 		if (!attr->store)
6611da177e4SLinus Torvalds 			attr->store = base_attr->store;
6621da177e4SLinus Torvalds 	}
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	return device_create_file(dev, attr);
6651da177e4SLinus Torvalds }
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds /**
6681da177e4SLinus Torvalds  * scsi_sysfs_add_sdev - add scsi device to sysfs
6691da177e4SLinus Torvalds  * @sdev:	scsi_device to add
6701da177e4SLinus Torvalds  *
6711da177e4SLinus Torvalds  * Return value:
6721da177e4SLinus Torvalds  * 	0 on Success / non-zero on Failure
6731da177e4SLinus Torvalds  **/
6741da177e4SLinus Torvalds int scsi_sysfs_add_sdev(struct scsi_device *sdev)
6751da177e4SLinus Torvalds {
6761da177e4SLinus Torvalds 	int error, i;
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds 	if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
6791da177e4SLinus Torvalds 		return error;
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 	error = device_add(&sdev->sdev_gendev);
6821da177e4SLinus Torvalds 	if (error) {
6831da177e4SLinus Torvalds 		put_device(sdev->sdev_gendev.parent);
6841da177e4SLinus Torvalds 		printk(KERN_INFO "error 1\n");
6851da177e4SLinus Torvalds 		return error;
6861da177e4SLinus Torvalds 	}
6871da177e4SLinus Torvalds 	error = class_device_add(&sdev->sdev_classdev);
6881da177e4SLinus Torvalds 	if (error) {
6891da177e4SLinus Torvalds 		printk(KERN_INFO "error 2\n");
6901da177e4SLinus Torvalds 		goto clean_device;
6911da177e4SLinus Torvalds 	}
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds 	/* take a reference for the sdev_classdev; this is
6941da177e4SLinus Torvalds 	 * released by the sdev_class .release */
6951da177e4SLinus Torvalds 	get_device(&sdev->sdev_gendev);
6961da177e4SLinus Torvalds 	if (sdev->host->hostt->sdev_attrs) {
6971da177e4SLinus Torvalds 		for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
6981da177e4SLinus Torvalds 			error = attr_add(&sdev->sdev_gendev,
6991da177e4SLinus Torvalds 					sdev->host->hostt->sdev_attrs[i]);
7001da177e4SLinus Torvalds 			if (error) {
701903f4fedSAlan Stern 				__scsi_remove_device(sdev);
7021da177e4SLinus Torvalds 				goto out;
7031da177e4SLinus Torvalds 			}
7041da177e4SLinus Torvalds 		}
7051da177e4SLinus Torvalds 	}
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	for (i = 0; scsi_sysfs_sdev_attrs[i]; i++) {
7081da177e4SLinus Torvalds 		if (!attr_overridden(sdev->host->hostt->sdev_attrs,
7091da177e4SLinus Torvalds 					scsi_sysfs_sdev_attrs[i])) {
7101da177e4SLinus Torvalds 			struct device_attribute * attr =
7111da177e4SLinus Torvalds 				attr_changed_internally(sdev->host,
7121da177e4SLinus Torvalds 							scsi_sysfs_sdev_attrs[i]);
7131da177e4SLinus Torvalds 			error = device_create_file(&sdev->sdev_gendev, attr);
7141da177e4SLinus Torvalds 			if (error) {
715903f4fedSAlan Stern 				__scsi_remove_device(sdev);
7161da177e4SLinus Torvalds 				goto out;
7171da177e4SLinus Torvalds 			}
7181da177e4SLinus Torvalds 		}
7191da177e4SLinus Torvalds 	}
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 	transport_add_device(&sdev->sdev_gendev);
7221da177e4SLinus Torvalds  out:
7231da177e4SLinus Torvalds 	return error;
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds  clean_device:
7261da177e4SLinus Torvalds 	scsi_device_set_state(sdev, SDEV_CANCEL);
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 	device_del(&sdev->sdev_gendev);
7291da177e4SLinus Torvalds 	transport_destroy_device(&sdev->sdev_gendev);
7301da177e4SLinus Torvalds 	put_device(&sdev->sdev_gendev);
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 	return error;
7331da177e4SLinus Torvalds }
7341da177e4SLinus Torvalds 
735903f4fedSAlan Stern void __scsi_remove_device(struct scsi_device *sdev)
7361da177e4SLinus Torvalds {
737df133c21SJames Bottomley 	struct device *dev = &sdev->sdev_gendev;
738df133c21SJames Bottomley 
7391da177e4SLinus Torvalds 	if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
740903f4fedSAlan Stern 		return;
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 	class_device_unregister(&sdev->sdev_classdev);
743df133c21SJames Bottomley 	transport_remove_device(dev);
744df133c21SJames Bottomley 	device_del(dev);
7451da177e4SLinus Torvalds 	scsi_device_set_state(sdev, SDEV_DEL);
7461da177e4SLinus Torvalds 	if (sdev->host->hostt->slave_destroy)
7471da177e4SLinus Torvalds 		sdev->host->hostt->slave_destroy(sdev);
748df133c21SJames Bottomley 	transport_destroy_device(dev);
749df133c21SJames Bottomley 	put_device(dev);
750903f4fedSAlan Stern }
751903f4fedSAlan Stern 
752903f4fedSAlan Stern /**
753903f4fedSAlan Stern  * scsi_remove_device - unregister a device from the scsi bus
754903f4fedSAlan Stern  * @sdev:	scsi_device to unregister
755903f4fedSAlan Stern  **/
756903f4fedSAlan Stern void scsi_remove_device(struct scsi_device *sdev)
757903f4fedSAlan Stern {
75854195002SAlan Stern 	struct Scsi_Host *shost = sdev->host;
75954195002SAlan Stern 
7600b950672SArjan van de Ven 	mutex_lock(&shost->scan_mutex);
761903f4fedSAlan Stern 	__scsi_remove_device(sdev);
7620b950672SArjan van de Ven 	mutex_unlock(&shost->scan_mutex);
7631da177e4SLinus Torvalds }
7641da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_remove_device);
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds void __scsi_remove_target(struct scsi_target *starget)
7671da177e4SLinus Torvalds {
7681da177e4SLinus Torvalds 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
7691da177e4SLinus Torvalds 	unsigned long flags;
770a64358dbSAlan Stern 	struct scsi_device *sdev;
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
7731da177e4SLinus Torvalds 	starget->reap_ref++;
774a64358dbSAlan Stern  restart:
775a64358dbSAlan Stern 	list_for_each_entry(sdev, &shost->__devices, siblings) {
7761da177e4SLinus Torvalds 		if (sdev->channel != starget->channel ||
777a64358dbSAlan Stern 		    sdev->id != starget->id ||
778a64358dbSAlan Stern 		    sdev->sdev_state == SDEV_DEL)
7791da177e4SLinus Torvalds 			continue;
7801da177e4SLinus Torvalds 		spin_unlock_irqrestore(shost->host_lock, flags);
7811da177e4SLinus Torvalds 		scsi_remove_device(sdev);
7821da177e4SLinus Torvalds 		spin_lock_irqsave(shost->host_lock, flags);
783a64358dbSAlan Stern 		goto restart;
7841da177e4SLinus Torvalds 	}
7851da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
7861da177e4SLinus Torvalds 	scsi_target_reap(starget);
7871da177e4SLinus Torvalds }
7881da177e4SLinus Torvalds 
78920b1e674Smochel@digitalimplant.org static int __remove_child (struct device * dev, void * data)
79020b1e674Smochel@digitalimplant.org {
79120b1e674Smochel@digitalimplant.org 	if (scsi_is_target_device(dev))
79220b1e674Smochel@digitalimplant.org 		__scsi_remove_target(to_scsi_target(dev));
79320b1e674Smochel@digitalimplant.org 	return 0;
79420b1e674Smochel@digitalimplant.org }
79520b1e674Smochel@digitalimplant.org 
7961da177e4SLinus Torvalds /**
7971da177e4SLinus Torvalds  * scsi_remove_target - try to remove a target and all its devices
7981da177e4SLinus Torvalds  * @dev: generic starget or parent of generic stargets to be removed
7991da177e4SLinus Torvalds  *
8001da177e4SLinus Torvalds  * Note: This is slightly racy.  It is possible that if the user
8011da177e4SLinus Torvalds  * requests the addition of another device then the target won't be
8021da177e4SLinus Torvalds  * removed.
8031da177e4SLinus Torvalds  */
8041da177e4SLinus Torvalds void scsi_remove_target(struct device *dev)
8051da177e4SLinus Torvalds {
80620b1e674Smochel@digitalimplant.org 	struct device *rdev;
8071da177e4SLinus Torvalds 
8081da177e4SLinus Torvalds 	if (scsi_is_target_device(dev)) {
8091da177e4SLinus Torvalds 		__scsi_remove_target(to_scsi_target(dev));
8101da177e4SLinus Torvalds 		return;
8111da177e4SLinus Torvalds 	}
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 	rdev = get_device(dev);
81420b1e674Smochel@digitalimplant.org 	device_for_each_child(dev, NULL, __remove_child);
8151da177e4SLinus Torvalds 	put_device(rdev);
8161da177e4SLinus Torvalds }
8171da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_remove_target);
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds int scsi_register_driver(struct device_driver *drv)
8201da177e4SLinus Torvalds {
8211da177e4SLinus Torvalds 	drv->bus = &scsi_bus_type;
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 	return driver_register(drv);
8241da177e4SLinus Torvalds }
8251da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_register_driver);
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds int scsi_register_interface(struct class_interface *intf)
8281da177e4SLinus Torvalds {
8291da177e4SLinus Torvalds 	intf->class = &sdev_class;
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds 	return class_interface_register(intf);
8321da177e4SLinus Torvalds }
8331da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_register_interface);
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds static struct class_device_attribute *class_attr_overridden(
8371da177e4SLinus Torvalds 		struct class_device_attribute **attrs,
8381da177e4SLinus Torvalds 		struct class_device_attribute *attr)
8391da177e4SLinus Torvalds {
8401da177e4SLinus Torvalds 	int i;
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 	if (!attrs)
8431da177e4SLinus Torvalds 		return NULL;
8441da177e4SLinus Torvalds 	for (i = 0; attrs[i]; i++)
8451da177e4SLinus Torvalds 		if (!strcmp(attrs[i]->attr.name, attr->attr.name))
8461da177e4SLinus Torvalds 			return attrs[i];
8471da177e4SLinus Torvalds 	return NULL;
8481da177e4SLinus Torvalds }
8491da177e4SLinus Torvalds 
8501da177e4SLinus Torvalds static int class_attr_add(struct class_device *classdev,
8511da177e4SLinus Torvalds 		struct class_device_attribute *attr)
8521da177e4SLinus Torvalds {
8531da177e4SLinus Torvalds 	struct class_device_attribute *base_attr;
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	/*
8561da177e4SLinus Torvalds 	 * Spare the caller from having to copy things it's not interested in.
8571da177e4SLinus Torvalds 	 */
8581da177e4SLinus Torvalds 	base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
8591da177e4SLinus Torvalds 	if (base_attr) {
8601da177e4SLinus Torvalds 		/* extend permissions */
8611da177e4SLinus Torvalds 		attr->attr.mode |= base_attr->attr.mode;
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 		/* override null show/store with default */
8641da177e4SLinus Torvalds 		if (!attr->show)
8651da177e4SLinus Torvalds 			attr->show = base_attr->show;
8661da177e4SLinus Torvalds 		if (!attr->store)
8671da177e4SLinus Torvalds 			attr->store = base_attr->store;
8681da177e4SLinus Torvalds 	}
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 	return class_device_create_file(classdev, attr);
8711da177e4SLinus Torvalds }
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds /**
8741da177e4SLinus Torvalds  * scsi_sysfs_add_host - add scsi host to subsystem
8751da177e4SLinus Torvalds  * @shost:     scsi host struct to add to subsystem
8761da177e4SLinus Torvalds  * @dev:       parent struct device pointer
8771da177e4SLinus Torvalds  **/
8781da177e4SLinus Torvalds int scsi_sysfs_add_host(struct Scsi_Host *shost)
8791da177e4SLinus Torvalds {
8801da177e4SLinus Torvalds 	int error, i;
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	if (shost->hostt->shost_attrs) {
8831da177e4SLinus Torvalds 		for (i = 0; shost->hostt->shost_attrs[i]; i++) {
8841da177e4SLinus Torvalds 			error = class_attr_add(&shost->shost_classdev,
8851da177e4SLinus Torvalds 					shost->hostt->shost_attrs[i]);
8861da177e4SLinus Torvalds 			if (error)
8871da177e4SLinus Torvalds 				return error;
8881da177e4SLinus Torvalds 		}
8891da177e4SLinus Torvalds 	}
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
8921da177e4SLinus Torvalds 		if (!class_attr_overridden(shost->hostt->shost_attrs,
8931da177e4SLinus Torvalds 					scsi_sysfs_shost_attrs[i])) {
8941da177e4SLinus Torvalds 			error = class_device_create_file(&shost->shost_classdev,
8951da177e4SLinus Torvalds 					scsi_sysfs_shost_attrs[i]);
8961da177e4SLinus Torvalds 			if (error)
8971da177e4SLinus Torvalds 				return error;
8981da177e4SLinus Torvalds 		}
8991da177e4SLinus Torvalds 	}
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 	transport_register_device(&shost->shost_gendev);
9021da177e4SLinus Torvalds 	return 0;
9031da177e4SLinus Torvalds }
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds void scsi_sysfs_device_initialize(struct scsi_device *sdev)
9061da177e4SLinus Torvalds {
9071da177e4SLinus Torvalds 	unsigned long flags;
9081da177e4SLinus Torvalds 	struct Scsi_Host *shost = sdev->host;
9091da177e4SLinus Torvalds 	struct scsi_target  *starget = sdev->sdev_target;
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	device_initialize(&sdev->sdev_gendev);
9121da177e4SLinus Torvalds 	sdev->sdev_gendev.bus = &scsi_bus_type;
9131da177e4SLinus Torvalds 	sdev->sdev_gendev.release = scsi_device_dev_release;
9141da177e4SLinus Torvalds 	sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
9151da177e4SLinus Torvalds 		sdev->host->host_no, sdev->channel, sdev->id,
9161da177e4SLinus Torvalds 		sdev->lun);
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	class_device_initialize(&sdev->sdev_classdev);
9191da177e4SLinus Torvalds 	sdev->sdev_classdev.dev = &sdev->sdev_gendev;
9201da177e4SLinus Torvalds 	sdev->sdev_classdev.class = &sdev_class;
9211da177e4SLinus Torvalds 	snprintf(sdev->sdev_classdev.class_id, BUS_ID_SIZE,
9221da177e4SLinus Torvalds 		 "%d:%d:%d:%d", sdev->host->host_no,
9231da177e4SLinus Torvalds 		 sdev->channel, sdev->id, sdev->lun);
9241da177e4SLinus Torvalds 	sdev->scsi_level = SCSI_2;
9251da177e4SLinus Torvalds 	transport_setup_device(&sdev->sdev_gendev);
9261da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
9271da177e4SLinus Torvalds 	list_add_tail(&sdev->same_target_siblings, &starget->devices);
9281da177e4SLinus Torvalds 	list_add_tail(&sdev->siblings, &shost->__devices);
9291da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
9301da177e4SLinus Torvalds }
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds int scsi_is_sdev_device(const struct device *dev)
9331da177e4SLinus Torvalds {
9341da177e4SLinus Torvalds 	return dev->release == scsi_device_dev_release;
9351da177e4SLinus Torvalds }
9361da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_is_sdev_device);
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds /* A blank transport template that is used in drivers that don't
9391da177e4SLinus Torvalds  * yet implement Transport Attributes */
9401da177e4SLinus Torvalds struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };
941