xref: /openbmc/linux/drivers/scsi/scsi_sysfs.c (revision 7eff2e7a8b65c25920207324e56611150eb1cd9a)
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/module.h>
101da177e4SLinus Torvalds #include <linux/init.h>
111da177e4SLinus Torvalds #include <linux/blkdev.h>
121da177e4SLinus Torvalds #include <linux/device.h>
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds #include <scsi/scsi.h>
151da177e4SLinus Torvalds #include <scsi/scsi_device.h>
161da177e4SLinus Torvalds #include <scsi/scsi_host.h>
171da177e4SLinus Torvalds #include <scsi/scsi_tcq.h>
181da177e4SLinus Torvalds #include <scsi/scsi_transport.h>
1944818efbSAdrian Bunk #include <scsi/scsi_driver.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 
426391a113STobias Klauser 	for (i = 0; i < ARRAY_SIZE(sdev_states); 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 
686391a113STobias Klauser 	for (i = 0; i < ARRAY_SIZE(shost_states); 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 
1636391a113STobias Klauser 	for (i = 0; i < ARRAY_SIZE(shost_states); 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");
196ed632da8SJames Bottomley shost_rd_attr(can_queue, "%hd\n");
1971da177e4SLinus Torvalds shost_rd_attr(sg_tablesize, "%hu\n");
1981da177e4SLinus Torvalds shost_rd_attr(unchecked_isa_dma, "%d\n");
1991da177e4SLinus Torvalds shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds static struct class_device_attribute *scsi_sysfs_shost_attrs[] = {
2021da177e4SLinus Torvalds 	&class_device_attr_unique_id,
2031da177e4SLinus Torvalds 	&class_device_attr_host_busy,
2041da177e4SLinus Torvalds 	&class_device_attr_cmd_per_lun,
205ed632da8SJames Bottomley 	&class_device_attr_can_queue,
2061da177e4SLinus Torvalds 	&class_device_attr_sg_tablesize,
2071da177e4SLinus Torvalds 	&class_device_attr_unchecked_isa_dma,
2081da177e4SLinus Torvalds 	&class_device_attr_proc_name,
2091da177e4SLinus Torvalds 	&class_device_attr_scan,
210d3301874SMike Anderson 	&class_device_attr_state,
2111da177e4SLinus Torvalds 	NULL
2121da177e4SLinus Torvalds };
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds static void scsi_device_cls_release(struct class_device *class_dev)
2151da177e4SLinus Torvalds {
2161da177e4SLinus Torvalds 	struct scsi_device *sdev;
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	sdev = class_to_sdev(class_dev);
2191da177e4SLinus Torvalds 	put_device(&sdev->sdev_gendev);
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds 
22265f27f38SDavid Howells static void scsi_device_dev_release_usercontext(struct work_struct *work)
2231da177e4SLinus Torvalds {
2241da177e4SLinus Torvalds 	struct scsi_device *sdev;
2251da177e4SLinus Torvalds 	struct device *parent;
2261da177e4SLinus Torvalds 	struct scsi_target *starget;
2271da177e4SLinus Torvalds 	unsigned long flags;
2281da177e4SLinus Torvalds 
22965f27f38SDavid Howells 	sdev = container_of(work, struct scsi_device, ew.work);
23065f27f38SDavid Howells 
23165f27f38SDavid Howells 	parent = sdev->sdev_gendev.parent;
2321da177e4SLinus Torvalds 	starget = to_scsi_target(parent);
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	spin_lock_irqsave(sdev->host->host_lock, flags);
2351da177e4SLinus Torvalds 	starget->reap_ref++;
2361da177e4SLinus Torvalds 	list_del(&sdev->siblings);
2371da177e4SLinus Torvalds 	list_del(&sdev->same_target_siblings);
2381da177e4SLinus Torvalds 	list_del(&sdev->starved_entry);
2391da177e4SLinus Torvalds 	spin_unlock_irqrestore(sdev->host->host_lock, flags);
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	if (sdev->request_queue) {
2421da177e4SLinus Torvalds 		sdev->request_queue->queuedata = NULL;
24365110b21SJames Bottomley 		/* user context needed to free queue */
2441da177e4SLinus Torvalds 		scsi_free_queue(sdev->request_queue);
245c2a9331cS 		/* temporary expedient, try to catch use of queue lock
246c2a9331cS 		 * after free of sdev */
247c2a9331cS 		sdev->request_queue = NULL;
2481da177e4SLinus Torvalds 	}
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds 	scsi_target_reap(scsi_target(sdev));
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	kfree(sdev->inquiry);
2531da177e4SLinus Torvalds 	kfree(sdev);
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	if (parent)
2561da177e4SLinus Torvalds 		put_device(parent);
2571da177e4SLinus Torvalds }
2581da177e4SLinus Torvalds 
25965110b21SJames Bottomley static void scsi_device_dev_release(struct device *dev)
26065110b21SJames Bottomley {
261ffedb452SJames Bottomley 	struct scsi_device *sdp = to_scsi_device(dev);
26265f27f38SDavid Howells 	execute_in_process_context(scsi_device_dev_release_usercontext,
263ffedb452SJames Bottomley 				   &sdp->ew);
26465110b21SJames Bottomley }
26565110b21SJames Bottomley 
26652c1da39SAdrian Bunk static struct class sdev_class = {
2671da177e4SLinus Torvalds 	.name		= "scsi_device",
2681da177e4SLinus Torvalds 	.release	= scsi_device_cls_release,
2691da177e4SLinus Torvalds };
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds /* all probing is done in the individual ->probe routines */
2721da177e4SLinus Torvalds static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
2731da177e4SLinus Torvalds {
2741da177e4SLinus Torvalds 	struct scsi_device *sdp = to_scsi_device(dev);
2751da177e4SLinus Torvalds 	if (sdp->no_uld_attach)
2761da177e4SLinus Torvalds 		return 0;
2771da177e4SLinus Torvalds 	return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds 
280*7eff2e7aSKay Sievers static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
281d7b8bcb0SMichael Tokarev {
282d7b8bcb0SMichael Tokarev 	struct scsi_device *sdev = to_scsi_device(dev);
283d7b8bcb0SMichael Tokarev 
284*7eff2e7aSKay Sievers 	add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
285d7b8bcb0SMichael Tokarev 	return 0;
286d7b8bcb0SMichael Tokarev }
287d7b8bcb0SMichael Tokarev 
2889b847548SJens Axboe static int scsi_bus_suspend(struct device * dev, pm_message_t state)
2899b847548SJens Axboe {
290c3c94c5aSTejun Heo 	struct device_driver *drv = dev->driver;
2919b847548SJens Axboe 	struct scsi_device *sdev = to_scsi_device(dev);
2929b847548SJens Axboe 	int err;
2939b847548SJens Axboe 
2949b847548SJens Axboe 	err = scsi_device_quiesce(sdev);
2959b847548SJens Axboe 	if (err)
2969b847548SJens Axboe 		return err;
2979b847548SJens Axboe 
298c3c94c5aSTejun Heo 	if (drv && drv->suspend) {
299c3c94c5aSTejun Heo 		err = drv->suspend(dev, state);
300c3c94c5aSTejun Heo 		if (err)
3019b847548SJens Axboe 			return err;
3029b847548SJens Axboe 	}
3039b847548SJens Axboe 
304c3c94c5aSTejun Heo 	return 0;
305c3c94c5aSTejun Heo }
306c3c94c5aSTejun Heo 
3079b847548SJens Axboe static int scsi_bus_resume(struct device * dev)
3089b847548SJens Axboe {
309c3c94c5aSTejun Heo 	struct device_driver *drv = dev->driver;
3109b847548SJens Axboe 	struct scsi_device *sdev = to_scsi_device(dev);
3111dfcda06STejun Heo 	int err = 0;
3129b847548SJens Axboe 
313c3c94c5aSTejun Heo 	if (drv && drv->resume)
3141dfcda06STejun Heo 		err = drv->resume(dev);
315c3c94c5aSTejun Heo 
3169b847548SJens Axboe 	scsi_device_resume(sdev);
317c3c94c5aSTejun Heo 
3181dfcda06STejun Heo 	return err;
3199b847548SJens Axboe }
3209b847548SJens Axboe 
3211da177e4SLinus Torvalds struct bus_type scsi_bus_type = {
3221da177e4SLinus Torvalds         .name		= "scsi",
3231da177e4SLinus Torvalds         .match		= scsi_bus_match,
324d7b8bcb0SMichael Tokarev 	.uevent		= scsi_bus_uevent,
3259b847548SJens Axboe 	.suspend	= scsi_bus_suspend,
3269b847548SJens Axboe 	.resume		= scsi_bus_resume,
3271da177e4SLinus Torvalds };
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds int scsi_sysfs_register(void)
3301da177e4SLinus Torvalds {
3311da177e4SLinus Torvalds 	int error;
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	error = bus_register(&scsi_bus_type);
3341da177e4SLinus Torvalds 	if (!error) {
3351da177e4SLinus Torvalds 		error = class_register(&sdev_class);
3361da177e4SLinus Torvalds 		if (error)
3371da177e4SLinus Torvalds 			bus_unregister(&scsi_bus_type);
3381da177e4SLinus Torvalds 	}
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	return error;
3411da177e4SLinus Torvalds }
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds void scsi_sysfs_unregister(void)
3441da177e4SLinus Torvalds {
3451da177e4SLinus Torvalds 	class_unregister(&sdev_class);
3461da177e4SLinus Torvalds 	bus_unregister(&scsi_bus_type);
3471da177e4SLinus Torvalds }
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds /*
3501da177e4SLinus Torvalds  * sdev_show_function: macro to create an attr function that can be used to
3511da177e4SLinus Torvalds  * show a non-bit field.
3521da177e4SLinus Torvalds  */
3531da177e4SLinus Torvalds #define sdev_show_function(field, format_string)				\
3541da177e4SLinus Torvalds static ssize_t								\
35510523b3bSYani Ioannou sdev_show_##field (struct device *dev, struct device_attribute *attr, char *buf)				\
3561da177e4SLinus Torvalds {									\
3571da177e4SLinus Torvalds 	struct scsi_device *sdev;					\
3581da177e4SLinus Torvalds 	sdev = to_scsi_device(dev);					\
3591da177e4SLinus Torvalds 	return snprintf (buf, 20, format_string, sdev->field);		\
3601da177e4SLinus Torvalds }									\
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds /*
3631da177e4SLinus Torvalds  * sdev_rd_attr: macro to create a function and attribute variable for a
3641da177e4SLinus Torvalds  * read only field.
3651da177e4SLinus Torvalds  */
3661da177e4SLinus Torvalds #define sdev_rd_attr(field, format_string)				\
3671da177e4SLinus Torvalds 	sdev_show_function(field, format_string)			\
3681da177e4SLinus Torvalds static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds /*
3721da177e4SLinus Torvalds  * sdev_rd_attr: create a function and attribute variable for a
3731da177e4SLinus Torvalds  * read/write field.
3741da177e4SLinus Torvalds  */
3751da177e4SLinus Torvalds #define sdev_rw_attr(field, format_string)				\
3761da177e4SLinus Torvalds 	sdev_show_function(field, format_string)				\
3771da177e4SLinus Torvalds 									\
3781da177e4SLinus Torvalds static ssize_t								\
37910523b3bSYani Ioannou sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)	\
3801da177e4SLinus Torvalds {									\
3811da177e4SLinus Torvalds 	struct scsi_device *sdev;					\
3821da177e4SLinus Torvalds 	sdev = to_scsi_device(dev);					\
3831da177e4SLinus Torvalds 	snscanf (buf, 20, format_string, &sdev->field);			\
3841da177e4SLinus Torvalds 	return count;							\
3851da177e4SLinus Torvalds }									\
3861da177e4SLinus Torvalds static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds /* Currently we don't export bit fields, but we might in future,
3891da177e4SLinus Torvalds  * so leave this code in */
3901da177e4SLinus Torvalds #if 0
3911da177e4SLinus Torvalds /*
3921da177e4SLinus Torvalds  * sdev_rd_attr: create a function and attribute variable for a
3931da177e4SLinus Torvalds  * read/write bit field.
3941da177e4SLinus Torvalds  */
3951da177e4SLinus Torvalds #define sdev_rw_attr_bit(field)						\
3961da177e4SLinus Torvalds 	sdev_show_function(field, "%d\n")					\
3971da177e4SLinus Torvalds 									\
3981da177e4SLinus Torvalds static ssize_t								\
39910523b3bSYani Ioannou sdev_store_##field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)	\
4001da177e4SLinus Torvalds {									\
4011da177e4SLinus Torvalds 	int ret;							\
4021da177e4SLinus Torvalds 	struct scsi_device *sdev;					\
4031da177e4SLinus Torvalds 	ret = scsi_sdev_check_buf_bit(buf);				\
4041da177e4SLinus Torvalds 	if (ret >= 0)	{						\
4051da177e4SLinus Torvalds 		sdev = to_scsi_device(dev);				\
4061da177e4SLinus Torvalds 		sdev->field = ret;					\
4071da177e4SLinus Torvalds 		ret = count;						\
4081da177e4SLinus Torvalds 	}								\
4091da177e4SLinus Torvalds 	return ret;							\
4101da177e4SLinus Torvalds }									\
4111da177e4SLinus Torvalds static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds /*
4141da177e4SLinus Torvalds  * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
4151da177e4SLinus Torvalds  * else return -EINVAL.
4161da177e4SLinus Torvalds  */
4171da177e4SLinus Torvalds static int scsi_sdev_check_buf_bit(const char *buf)
4181da177e4SLinus Torvalds {
4191da177e4SLinus Torvalds 	if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
4201da177e4SLinus Torvalds 		if (buf[0] == '1')
4211da177e4SLinus Torvalds 			return 1;
4221da177e4SLinus Torvalds 		else if (buf[0] == '0')
4231da177e4SLinus Torvalds 			return 0;
4241da177e4SLinus Torvalds 		else
4251da177e4SLinus Torvalds 			return -EINVAL;
4261da177e4SLinus Torvalds 	} else
4271da177e4SLinus Torvalds 		return -EINVAL;
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds #endif
4301da177e4SLinus Torvalds /*
4311da177e4SLinus Torvalds  * Create the actual show/store functions and data structures.
4321da177e4SLinus Torvalds  */
4331da177e4SLinus Torvalds sdev_rd_attr (device_blocked, "%d\n");
4341da177e4SLinus Torvalds sdev_rd_attr (queue_depth, "%d\n");
4351da177e4SLinus Torvalds sdev_rd_attr (type, "%d\n");
4361da177e4SLinus Torvalds sdev_rd_attr (scsi_level, "%d\n");
4371da177e4SLinus Torvalds sdev_rd_attr (vendor, "%.8s\n");
4381da177e4SLinus Torvalds sdev_rd_attr (model, "%.16s\n");
4391da177e4SLinus Torvalds sdev_rd_attr (rev, "%.4s\n");
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds static ssize_t
44210523b3bSYani Ioannou sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
4431da177e4SLinus Torvalds {
4441da177e4SLinus Torvalds 	struct scsi_device *sdev;
4451da177e4SLinus Torvalds 	sdev = to_scsi_device(dev);
4461da177e4SLinus Torvalds 	return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
4471da177e4SLinus Torvalds }
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds static ssize_t
45010523b3bSYani Ioannou sdev_store_timeout (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
4511da177e4SLinus Torvalds {
4521da177e4SLinus Torvalds 	struct scsi_device *sdev;
4531da177e4SLinus Torvalds 	int timeout;
4541da177e4SLinus Torvalds 	sdev = to_scsi_device(dev);
4551da177e4SLinus Torvalds 	sscanf (buf, "%d\n", &timeout);
4561da177e4SLinus Torvalds 	sdev->timeout = timeout * HZ;
4571da177e4SLinus Torvalds 	return count;
4581da177e4SLinus Torvalds }
4591da177e4SLinus Torvalds static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds static ssize_t
46210523b3bSYani Ioannou store_rescan_field (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
4631da177e4SLinus Torvalds {
4641da177e4SLinus Torvalds 	scsi_rescan_device(dev);
4651da177e4SLinus Torvalds 	return count;
4661da177e4SLinus Torvalds }
4671da177e4SLinus Torvalds static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
4681da177e4SLinus Torvalds 
469d9a9cdfbSAlan Stern static void sdev_store_delete_callback(struct device *dev)
470d9a9cdfbSAlan Stern {
471d9a9cdfbSAlan Stern 	scsi_remove_device(to_scsi_device(dev));
472d9a9cdfbSAlan Stern }
473d9a9cdfbSAlan Stern 
47410523b3bSYani Ioannou static ssize_t sdev_store_delete(struct device *dev, struct device_attribute *attr, const char *buf,
4751da177e4SLinus Torvalds 				 size_t count)
4761da177e4SLinus Torvalds {
477d9a9cdfbSAlan Stern 	int rc;
478d9a9cdfbSAlan Stern 
479d9a9cdfbSAlan Stern 	/* An attribute cannot be unregistered by one of its own methods,
480d9a9cdfbSAlan Stern 	 * so we have to use this roundabout approach.
481d9a9cdfbSAlan Stern 	 */
482d9a9cdfbSAlan Stern 	rc = device_schedule_callback(dev, sdev_store_delete_callback);
483d9a9cdfbSAlan Stern 	if (rc)
484d9a9cdfbSAlan Stern 		count = rc;
4851da177e4SLinus Torvalds 	return count;
4861da177e4SLinus Torvalds };
4871da177e4SLinus Torvalds static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds static ssize_t
49010523b3bSYani Ioannou store_state_field(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
4911da177e4SLinus Torvalds {
4921da177e4SLinus Torvalds 	int i;
4931da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
4941da177e4SLinus Torvalds 	enum scsi_device_state state = 0;
4951da177e4SLinus Torvalds 
4966391a113STobias Klauser 	for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
4971da177e4SLinus Torvalds 		const int len = strlen(sdev_states[i].name);
4981da177e4SLinus Torvalds 		if (strncmp(sdev_states[i].name, buf, len) == 0 &&
4991da177e4SLinus Torvalds 		   buf[len] == '\n') {
5001da177e4SLinus Torvalds 			state = sdev_states[i].value;
5011da177e4SLinus Torvalds 			break;
5021da177e4SLinus Torvalds 		}
5031da177e4SLinus Torvalds 	}
5041da177e4SLinus Torvalds 	if (!state)
5051da177e4SLinus Torvalds 		return -EINVAL;
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 	if (scsi_device_set_state(sdev, state))
5081da177e4SLinus Torvalds 		return -EINVAL;
5091da177e4SLinus Torvalds 	return count;
5101da177e4SLinus Torvalds }
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds static ssize_t
51310523b3bSYani Ioannou show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
5141da177e4SLinus Torvalds {
5151da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
5161da177e4SLinus Torvalds 	const char *name = scsi_device_state_name(sdev->sdev_state);
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds 	if (!name)
5191da177e4SLinus Torvalds 		return -EINVAL;
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	return snprintf(buf, 20, "%s\n", name);
5221da177e4SLinus Torvalds }
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds static ssize_t
52710523b3bSYani Ioannou show_queue_type_field(struct device *dev, struct device_attribute *attr, char *buf)
5281da177e4SLinus Torvalds {
5291da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
5301da177e4SLinus Torvalds 	const char *name = "none";
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds 	if (sdev->ordered_tags)
5331da177e4SLinus Torvalds 		name = "ordered";
5341da177e4SLinus Torvalds 	else if (sdev->simple_tags)
5351da177e4SLinus Torvalds 		name = "simple";
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 	return snprintf(buf, 20, "%s\n", name);
5381da177e4SLinus Torvalds }
5391da177e4SLinus Torvalds 
5401da177e4SLinus Torvalds static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds static ssize_t
54310523b3bSYani Ioannou show_iostat_counterbits(struct device *dev, struct device_attribute *attr, char *buf)
5441da177e4SLinus Torvalds {
5451da177e4SLinus Torvalds 	return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
5461da177e4SLinus Torvalds }
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds #define show_sdev_iostat(field)						\
5511da177e4SLinus Torvalds static ssize_t								\
55210523b3bSYani Ioannou show_iostat_##field(struct device *dev, struct device_attribute *attr, char *buf)			\
5531da177e4SLinus Torvalds {									\
5541da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);			\
5551da177e4SLinus Torvalds 	unsigned long long count = atomic_read(&sdev->field);		\
5561da177e4SLinus Torvalds 	return snprintf(buf, 20, "0x%llx\n", count);			\
5571da177e4SLinus Torvalds }									\
5581da177e4SLinus Torvalds static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds show_sdev_iostat(iorequest_cnt);
5611da177e4SLinus Torvalds show_sdev_iostat(iodone_cnt);
5621da177e4SLinus Torvalds show_sdev_iostat(ioerr_cnt);
5631da177e4SLinus Torvalds 
564d7b8bcb0SMichael Tokarev static ssize_t
565d7b8bcb0SMichael Tokarev sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
566d7b8bcb0SMichael Tokarev {
567d7b8bcb0SMichael Tokarev 	struct scsi_device *sdev;
568d7b8bcb0SMichael Tokarev 	sdev = to_scsi_device(dev);
569d7b8bcb0SMichael Tokarev 	return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
570d7b8bcb0SMichael Tokarev }
571d7b8bcb0SMichael Tokarev static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds /* Default template for device attributes.  May NOT be modified */
5741da177e4SLinus Torvalds static struct device_attribute *scsi_sysfs_sdev_attrs[] = {
5751da177e4SLinus Torvalds 	&dev_attr_device_blocked,
5761da177e4SLinus Torvalds 	&dev_attr_queue_depth,
5771da177e4SLinus Torvalds 	&dev_attr_queue_type,
5781da177e4SLinus Torvalds 	&dev_attr_type,
5791da177e4SLinus Torvalds 	&dev_attr_scsi_level,
5801da177e4SLinus Torvalds 	&dev_attr_vendor,
5811da177e4SLinus Torvalds 	&dev_attr_model,
5821da177e4SLinus Torvalds 	&dev_attr_rev,
5831da177e4SLinus Torvalds 	&dev_attr_rescan,
5841da177e4SLinus Torvalds 	&dev_attr_delete,
5851da177e4SLinus Torvalds 	&dev_attr_state,
5861da177e4SLinus Torvalds 	&dev_attr_timeout,
5871da177e4SLinus Torvalds 	&dev_attr_iocounterbits,
5881da177e4SLinus Torvalds 	&dev_attr_iorequest_cnt,
5891da177e4SLinus Torvalds 	&dev_attr_iodone_cnt,
5901da177e4SLinus Torvalds 	&dev_attr_ioerr_cnt,
591d7b8bcb0SMichael Tokarev 	&dev_attr_modalias,
5921da177e4SLinus Torvalds 	NULL
5931da177e4SLinus Torvalds };
5941da177e4SLinus Torvalds 
59510523b3bSYani Ioannou static ssize_t sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr, const char *buf,
5961da177e4SLinus Torvalds 					 size_t count)
5971da177e4SLinus Torvalds {
5981da177e4SLinus Torvalds 	int depth, retval;
5991da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
6001da177e4SLinus Torvalds 	struct scsi_host_template *sht = sdev->host->hostt;
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	if (!sht->change_queue_depth)
6031da177e4SLinus Torvalds 		return -EINVAL;
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	depth = simple_strtoul(buf, NULL, 0);
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	if (depth < 1)
6081da177e4SLinus Torvalds 		return -EINVAL;
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	retval = sht->change_queue_depth(sdev, depth);
6111da177e4SLinus Torvalds 	if (retval < 0)
6121da177e4SLinus Torvalds 		return retval;
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 	return count;
6151da177e4SLinus Torvalds }
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds static struct device_attribute sdev_attr_queue_depth_rw =
6181da177e4SLinus Torvalds 	__ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
6191da177e4SLinus Torvalds 	       sdev_store_queue_depth_rw);
6201da177e4SLinus Torvalds 
62110523b3bSYani Ioannou static ssize_t sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr, const char *buf,
6221da177e4SLinus Torvalds 					size_t count)
6231da177e4SLinus Torvalds {
6241da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
6251da177e4SLinus Torvalds 	struct scsi_host_template *sht = sdev->host->hostt;
6261da177e4SLinus Torvalds 	int tag_type = 0, retval;
6271da177e4SLinus Torvalds 	int prev_tag_type = scsi_get_tag_type(sdev);
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	if (!sdev->tagged_supported || !sht->change_queue_type)
6301da177e4SLinus Torvalds 		return -EINVAL;
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds 	if (strncmp(buf, "ordered", 7) == 0)
6331da177e4SLinus Torvalds 		tag_type = MSG_ORDERED_TAG;
6341da177e4SLinus Torvalds 	else if (strncmp(buf, "simple", 6) == 0)
6351da177e4SLinus Torvalds 		tag_type = MSG_SIMPLE_TAG;
6361da177e4SLinus Torvalds 	else if (strncmp(buf, "none", 4) != 0)
6371da177e4SLinus Torvalds 		return -EINVAL;
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	if (tag_type == prev_tag_type)
6401da177e4SLinus Torvalds 		return count;
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	retval = sht->change_queue_type(sdev, tag_type);
6431da177e4SLinus Torvalds 	if (retval < 0)
6441da177e4SLinus Torvalds 		return retval;
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	return count;
6471da177e4SLinus Torvalds }
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds static struct device_attribute sdev_attr_queue_type_rw =
6501da177e4SLinus Torvalds 	__ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
6511da177e4SLinus Torvalds 	       sdev_store_queue_type_rw);
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds static struct device_attribute *attr_changed_internally(
6541da177e4SLinus Torvalds 		struct Scsi_Host *shost,
6551da177e4SLinus Torvalds 		struct device_attribute * attr)
6561da177e4SLinus Torvalds {
6571da177e4SLinus Torvalds 	if (!strcmp("queue_depth", attr->attr.name)
6581da177e4SLinus Torvalds 	    && shost->hostt->change_queue_depth)
6591da177e4SLinus Torvalds 		return &sdev_attr_queue_depth_rw;
6601da177e4SLinus Torvalds 	else if (!strcmp("queue_type", attr->attr.name)
6611da177e4SLinus Torvalds 	    && shost->hostt->change_queue_type)
6621da177e4SLinus Torvalds 		return &sdev_attr_queue_type_rw;
6631da177e4SLinus Torvalds 	return attr;
6641da177e4SLinus Torvalds }
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds static struct device_attribute *attr_overridden(
6681da177e4SLinus Torvalds 		struct device_attribute **attrs,
6691da177e4SLinus Torvalds 		struct device_attribute *attr)
6701da177e4SLinus Torvalds {
6711da177e4SLinus Torvalds 	int i;
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds 	if (!attrs)
6741da177e4SLinus Torvalds 		return NULL;
6751da177e4SLinus Torvalds 	for (i = 0; attrs[i]; i++)
6761da177e4SLinus Torvalds 		if (!strcmp(attrs[i]->attr.name, attr->attr.name))
6771da177e4SLinus Torvalds 			return attrs[i];
6781da177e4SLinus Torvalds 	return NULL;
6791da177e4SLinus Torvalds }
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds static int attr_add(struct device *dev, struct device_attribute *attr)
6821da177e4SLinus Torvalds {
6831da177e4SLinus Torvalds 	struct device_attribute *base_attr;
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 	/*
6861da177e4SLinus Torvalds 	 * Spare the caller from having to copy things it's not interested in.
6871da177e4SLinus Torvalds 	 */
6881da177e4SLinus Torvalds 	base_attr = attr_overridden(scsi_sysfs_sdev_attrs, attr);
6891da177e4SLinus Torvalds 	if (base_attr) {
6901da177e4SLinus Torvalds 		/* extend permissions */
6911da177e4SLinus Torvalds 		attr->attr.mode |= base_attr->attr.mode;
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds 		/* override null show/store with default */
6941da177e4SLinus Torvalds 		if (!attr->show)
6951da177e4SLinus Torvalds 			attr->show = base_attr->show;
6961da177e4SLinus Torvalds 		if (!attr->store)
6971da177e4SLinus Torvalds 			attr->store = base_attr->store;
6981da177e4SLinus Torvalds 	}
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 	return device_create_file(dev, attr);
7011da177e4SLinus Torvalds }
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds /**
7041da177e4SLinus Torvalds  * scsi_sysfs_add_sdev - add scsi device to sysfs
7051da177e4SLinus Torvalds  * @sdev:	scsi_device to add
7061da177e4SLinus Torvalds  *
7071da177e4SLinus Torvalds  * Return value:
7081da177e4SLinus Torvalds  * 	0 on Success / non-zero on Failure
7091da177e4SLinus Torvalds  **/
7101da177e4SLinus Torvalds int scsi_sysfs_add_sdev(struct scsi_device *sdev)
7111da177e4SLinus Torvalds {
7121da177e4SLinus Torvalds 	int error, i;
71380ed71ceSJames Bottomley 	struct request_queue *rq = sdev->request_queue;
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 	if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
7161da177e4SLinus Torvalds 		return error;
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds 	error = device_add(&sdev->sdev_gendev);
7191da177e4SLinus Torvalds 	if (error) {
7201da177e4SLinus Torvalds 		put_device(sdev->sdev_gendev.parent);
7211da177e4SLinus Torvalds 		printk(KERN_INFO "error 1\n");
7221da177e4SLinus Torvalds 		return error;
7231da177e4SLinus Torvalds 	}
7241da177e4SLinus Torvalds 	error = class_device_add(&sdev->sdev_classdev);
7251da177e4SLinus Torvalds 	if (error) {
7261da177e4SLinus Torvalds 		printk(KERN_INFO "error 2\n");
7271da177e4SLinus Torvalds 		goto clean_device;
7281da177e4SLinus Torvalds 	}
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 	/* take a reference for the sdev_classdev; this is
7311da177e4SLinus Torvalds 	 * released by the sdev_class .release */
7321da177e4SLinus Torvalds 	get_device(&sdev->sdev_gendev);
73380ed71ceSJames Bottomley 
73439dca558SJames Bottomley 	error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL);
73580ed71ceSJames Bottomley 
73680ed71ceSJames Bottomley 	if (error)
73780ed71ceSJames Bottomley 		sdev_printk(KERN_INFO, sdev,
73880ed71ceSJames Bottomley 			    "Failed to register bsg queue, errno=%d\n", error);
73980ed71ceSJames Bottomley 
74080ed71ceSJames Bottomley 	/* we're treating error on bsg register as non-fatal, so pretend
74180ed71ceSJames Bottomley 	 * nothing went wrong */
74280ed71ceSJames Bottomley 	error = 0;
74380ed71ceSJames Bottomley 
7441da177e4SLinus Torvalds 	if (sdev->host->hostt->sdev_attrs) {
7451da177e4SLinus Torvalds 		for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
7461da177e4SLinus Torvalds 			error = attr_add(&sdev->sdev_gendev,
7471da177e4SLinus Torvalds 					sdev->host->hostt->sdev_attrs[i]);
7481da177e4SLinus Torvalds 			if (error) {
749903f4fedSAlan Stern 				__scsi_remove_device(sdev);
7501da177e4SLinus Torvalds 				goto out;
7511da177e4SLinus Torvalds 			}
7521da177e4SLinus Torvalds 		}
7531da177e4SLinus Torvalds 	}
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	for (i = 0; scsi_sysfs_sdev_attrs[i]; i++) {
7561da177e4SLinus Torvalds 		if (!attr_overridden(sdev->host->hostt->sdev_attrs,
7571da177e4SLinus Torvalds 					scsi_sysfs_sdev_attrs[i])) {
7581da177e4SLinus Torvalds 			struct device_attribute * attr =
7591da177e4SLinus Torvalds 				attr_changed_internally(sdev->host,
7601da177e4SLinus Torvalds 							scsi_sysfs_sdev_attrs[i]);
7611da177e4SLinus Torvalds 			error = device_create_file(&sdev->sdev_gendev, attr);
7621da177e4SLinus Torvalds 			if (error) {
763903f4fedSAlan Stern 				__scsi_remove_device(sdev);
7641da177e4SLinus Torvalds 				goto out;
7651da177e4SLinus Torvalds 			}
7661da177e4SLinus Torvalds 		}
7671da177e4SLinus Torvalds 	}
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	transport_add_device(&sdev->sdev_gendev);
7701da177e4SLinus Torvalds  out:
7711da177e4SLinus Torvalds 	return error;
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds  clean_device:
7741da177e4SLinus Torvalds 	scsi_device_set_state(sdev, SDEV_CANCEL);
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	device_del(&sdev->sdev_gendev);
7771da177e4SLinus Torvalds 	transport_destroy_device(&sdev->sdev_gendev);
7781da177e4SLinus Torvalds 	put_device(&sdev->sdev_gendev);
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 	return error;
7811da177e4SLinus Torvalds }
7821da177e4SLinus Torvalds 
783903f4fedSAlan Stern void __scsi_remove_device(struct scsi_device *sdev)
7841da177e4SLinus Torvalds {
785df133c21SJames Bottomley 	struct device *dev = &sdev->sdev_gendev;
786df133c21SJames Bottomley 
7871da177e4SLinus Torvalds 	if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
788903f4fedSAlan Stern 		return;
7891da177e4SLinus Torvalds 
79080ed71ceSJames Bottomley 	bsg_unregister_queue(sdev->request_queue);
7911da177e4SLinus Torvalds 	class_device_unregister(&sdev->sdev_classdev);
792df133c21SJames Bottomley 	transport_remove_device(dev);
793df133c21SJames Bottomley 	device_del(dev);
7941da177e4SLinus Torvalds 	scsi_device_set_state(sdev, SDEV_DEL);
7951da177e4SLinus Torvalds 	if (sdev->host->hostt->slave_destroy)
7961da177e4SLinus Torvalds 		sdev->host->hostt->slave_destroy(sdev);
797df133c21SJames Bottomley 	transport_destroy_device(dev);
798df133c21SJames Bottomley 	put_device(dev);
799903f4fedSAlan Stern }
800903f4fedSAlan Stern 
801903f4fedSAlan Stern /**
802903f4fedSAlan Stern  * scsi_remove_device - unregister a device from the scsi bus
803903f4fedSAlan Stern  * @sdev:	scsi_device to unregister
804903f4fedSAlan Stern  **/
805903f4fedSAlan Stern void scsi_remove_device(struct scsi_device *sdev)
806903f4fedSAlan Stern {
80754195002SAlan Stern 	struct Scsi_Host *shost = sdev->host;
80854195002SAlan Stern 
8090b950672SArjan van de Ven 	mutex_lock(&shost->scan_mutex);
810903f4fedSAlan Stern 	__scsi_remove_device(sdev);
8110b950672SArjan van de Ven 	mutex_unlock(&shost->scan_mutex);
8121da177e4SLinus Torvalds }
8131da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_remove_device);
8141da177e4SLinus Torvalds 
81544818efbSAdrian Bunk static void __scsi_remove_target(struct scsi_target *starget)
8161da177e4SLinus Torvalds {
8171da177e4SLinus Torvalds 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
8181da177e4SLinus Torvalds 	unsigned long flags;
819a64358dbSAlan Stern 	struct scsi_device *sdev;
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
8221da177e4SLinus Torvalds 	starget->reap_ref++;
823a64358dbSAlan Stern  restart:
824a64358dbSAlan Stern 	list_for_each_entry(sdev, &shost->__devices, siblings) {
8251da177e4SLinus Torvalds 		if (sdev->channel != starget->channel ||
826a64358dbSAlan Stern 		    sdev->id != starget->id ||
827a64358dbSAlan Stern 		    sdev->sdev_state == SDEV_DEL)
8281da177e4SLinus Torvalds 			continue;
8291da177e4SLinus Torvalds 		spin_unlock_irqrestore(shost->host_lock, flags);
8301da177e4SLinus Torvalds 		scsi_remove_device(sdev);
8311da177e4SLinus Torvalds 		spin_lock_irqsave(shost->host_lock, flags);
832a64358dbSAlan Stern 		goto restart;
8331da177e4SLinus Torvalds 	}
8341da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
8351da177e4SLinus Torvalds 	scsi_target_reap(starget);
8361da177e4SLinus Torvalds }
8371da177e4SLinus Torvalds 
83820b1e674Smochel@digitalimplant.org static int __remove_child (struct device * dev, void * data)
83920b1e674Smochel@digitalimplant.org {
84020b1e674Smochel@digitalimplant.org 	if (scsi_is_target_device(dev))
84120b1e674Smochel@digitalimplant.org 		__scsi_remove_target(to_scsi_target(dev));
84220b1e674Smochel@digitalimplant.org 	return 0;
84320b1e674Smochel@digitalimplant.org }
84420b1e674Smochel@digitalimplant.org 
8451da177e4SLinus Torvalds /**
8461da177e4SLinus Torvalds  * scsi_remove_target - try to remove a target and all its devices
8471da177e4SLinus Torvalds  * @dev: generic starget or parent of generic stargets to be removed
8481da177e4SLinus Torvalds  *
8491da177e4SLinus Torvalds  * Note: This is slightly racy.  It is possible that if the user
8501da177e4SLinus Torvalds  * requests the addition of another device then the target won't be
8511da177e4SLinus Torvalds  * removed.
8521da177e4SLinus Torvalds  */
8531da177e4SLinus Torvalds void scsi_remove_target(struct device *dev)
8541da177e4SLinus Torvalds {
85520b1e674Smochel@digitalimplant.org 	struct device *rdev;
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds 	if (scsi_is_target_device(dev)) {
8581da177e4SLinus Torvalds 		__scsi_remove_target(to_scsi_target(dev));
8591da177e4SLinus Torvalds 		return;
8601da177e4SLinus Torvalds 	}
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds 	rdev = get_device(dev);
86320b1e674Smochel@digitalimplant.org 	device_for_each_child(dev, NULL, __remove_child);
8641da177e4SLinus Torvalds 	put_device(rdev);
8651da177e4SLinus Torvalds }
8661da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_remove_target);
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds int scsi_register_driver(struct device_driver *drv)
8691da177e4SLinus Torvalds {
8701da177e4SLinus Torvalds 	drv->bus = &scsi_bus_type;
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 	return driver_register(drv);
8731da177e4SLinus Torvalds }
8741da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_register_driver);
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds int scsi_register_interface(struct class_interface *intf)
8771da177e4SLinus Torvalds {
8781da177e4SLinus Torvalds 	intf->class = &sdev_class;
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds 	return class_interface_register(intf);
8811da177e4SLinus Torvalds }
8821da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_register_interface);
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds static struct class_device_attribute *class_attr_overridden(
8861da177e4SLinus Torvalds 		struct class_device_attribute **attrs,
8871da177e4SLinus Torvalds 		struct class_device_attribute *attr)
8881da177e4SLinus Torvalds {
8891da177e4SLinus Torvalds 	int i;
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	if (!attrs)
8921da177e4SLinus Torvalds 		return NULL;
8931da177e4SLinus Torvalds 	for (i = 0; attrs[i]; i++)
8941da177e4SLinus Torvalds 		if (!strcmp(attrs[i]->attr.name, attr->attr.name))
8951da177e4SLinus Torvalds 			return attrs[i];
8961da177e4SLinus Torvalds 	return NULL;
8971da177e4SLinus Torvalds }
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds static int class_attr_add(struct class_device *classdev,
9001da177e4SLinus Torvalds 		struct class_device_attribute *attr)
9011da177e4SLinus Torvalds {
9021da177e4SLinus Torvalds 	struct class_device_attribute *base_attr;
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds 	/*
9051da177e4SLinus Torvalds 	 * Spare the caller from having to copy things it's not interested in.
9061da177e4SLinus Torvalds 	 */
9071da177e4SLinus Torvalds 	base_attr = class_attr_overridden(scsi_sysfs_shost_attrs, attr);
9081da177e4SLinus Torvalds 	if (base_attr) {
9091da177e4SLinus Torvalds 		/* extend permissions */
9101da177e4SLinus Torvalds 		attr->attr.mode |= base_attr->attr.mode;
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds 		/* override null show/store with default */
9131da177e4SLinus Torvalds 		if (!attr->show)
9141da177e4SLinus Torvalds 			attr->show = base_attr->show;
9151da177e4SLinus Torvalds 		if (!attr->store)
9161da177e4SLinus Torvalds 			attr->store = base_attr->store;
9171da177e4SLinus Torvalds 	}
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds 	return class_device_create_file(classdev, attr);
9201da177e4SLinus Torvalds }
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds /**
9231da177e4SLinus Torvalds  * scsi_sysfs_add_host - add scsi host to subsystem
9241da177e4SLinus Torvalds  * @shost:     scsi host struct to add to subsystem
9251da177e4SLinus Torvalds  * @dev:       parent struct device pointer
9261da177e4SLinus Torvalds  **/
9271da177e4SLinus Torvalds int scsi_sysfs_add_host(struct Scsi_Host *shost)
9281da177e4SLinus Torvalds {
9291da177e4SLinus Torvalds 	int error, i;
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 	if (shost->hostt->shost_attrs) {
9321da177e4SLinus Torvalds 		for (i = 0; shost->hostt->shost_attrs[i]; i++) {
9331da177e4SLinus Torvalds 			error = class_attr_add(&shost->shost_classdev,
9341da177e4SLinus Torvalds 					shost->hostt->shost_attrs[i]);
9351da177e4SLinus Torvalds 			if (error)
9361da177e4SLinus Torvalds 				return error;
9371da177e4SLinus Torvalds 		}
9381da177e4SLinus Torvalds 	}
9391da177e4SLinus Torvalds 
9401da177e4SLinus Torvalds 	for (i = 0; scsi_sysfs_shost_attrs[i]; i++) {
9411da177e4SLinus Torvalds 		if (!class_attr_overridden(shost->hostt->shost_attrs,
9421da177e4SLinus Torvalds 					scsi_sysfs_shost_attrs[i])) {
9431da177e4SLinus Torvalds 			error = class_device_create_file(&shost->shost_classdev,
9441da177e4SLinus Torvalds 					scsi_sysfs_shost_attrs[i]);
9451da177e4SLinus Torvalds 			if (error)
9461da177e4SLinus Torvalds 				return error;
9471da177e4SLinus Torvalds 		}
9481da177e4SLinus Torvalds 	}
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds 	transport_register_device(&shost->shost_gendev);
9511da177e4SLinus Torvalds 	return 0;
9521da177e4SLinus Torvalds }
9531da177e4SLinus Torvalds 
9541da177e4SLinus Torvalds void scsi_sysfs_device_initialize(struct scsi_device *sdev)
9551da177e4SLinus Torvalds {
9561da177e4SLinus Torvalds 	unsigned long flags;
9571da177e4SLinus Torvalds 	struct Scsi_Host *shost = sdev->host;
9581da177e4SLinus Torvalds 	struct scsi_target  *starget = sdev->sdev_target;
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 	device_initialize(&sdev->sdev_gendev);
9611da177e4SLinus Torvalds 	sdev->sdev_gendev.bus = &scsi_bus_type;
9621da177e4SLinus Torvalds 	sdev->sdev_gendev.release = scsi_device_dev_release;
9631da177e4SLinus Torvalds 	sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
9641da177e4SLinus Torvalds 		sdev->host->host_no, sdev->channel, sdev->id,
9651da177e4SLinus Torvalds 		sdev->lun);
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 	class_device_initialize(&sdev->sdev_classdev);
9681da177e4SLinus Torvalds 	sdev->sdev_classdev.dev = &sdev->sdev_gendev;
9691da177e4SLinus Torvalds 	sdev->sdev_classdev.class = &sdev_class;
9701da177e4SLinus Torvalds 	snprintf(sdev->sdev_classdev.class_id, BUS_ID_SIZE,
9711da177e4SLinus Torvalds 		 "%d:%d:%d:%d", sdev->host->host_no,
9721da177e4SLinus Torvalds 		 sdev->channel, sdev->id, sdev->lun);
9737c9d6f16SAlan Stern 	sdev->scsi_level = starget->scsi_level;
9741da177e4SLinus Torvalds 	transport_setup_device(&sdev->sdev_gendev);
9751da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
9761da177e4SLinus Torvalds 	list_add_tail(&sdev->same_target_siblings, &starget->devices);
9771da177e4SLinus Torvalds 	list_add_tail(&sdev->siblings, &shost->__devices);
9781da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
9791da177e4SLinus Torvalds }
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds int scsi_is_sdev_device(const struct device *dev)
9821da177e4SLinus Torvalds {
9831da177e4SLinus Torvalds 	return dev->release == scsi_device_dev_release;
9841da177e4SLinus Torvalds }
9851da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_is_sdev_device);
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds /* A blank transport template that is used in drivers that don't
9881da177e4SLinus Torvalds  * yet implement Transport Attributes */
9891da177e4SLinus Torvalds struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };
990