xref: /openbmc/linux/drivers/scsi/scsi_sysfs.c (revision 2944369144548432f3a5ffce7a2926bfb4ce4f0a)
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>
105a0e3ad6STejun Heo #include <linux/slab.h>
111da177e4SLinus Torvalds #include <linux/init.h>
121da177e4SLinus Torvalds #include <linux/blkdev.h>
131da177e4SLinus Torvalds #include <linux/device.h>
14bc4f2401SAlan Stern #include <linux/pm_runtime.h>
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #include <scsi/scsi.h>
171da177e4SLinus Torvalds #include <scsi/scsi_device.h>
181da177e4SLinus Torvalds #include <scsi/scsi_host.h>
191da177e4SLinus Torvalds #include <scsi/scsi_tcq.h>
201da177e4SLinus Torvalds #include <scsi/scsi_transport.h>
2144818efbSAdrian Bunk #include <scsi/scsi_driver.h>
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #include "scsi_priv.h"
241da177e4SLinus Torvalds #include "scsi_logging.h"
251da177e4SLinus Torvalds 
26b0ed4336SHannes Reinecke static struct device_type scsi_dev_type;
27b0ed4336SHannes Reinecke 
280ad78200SArjan van de Ven static const struct {
291da177e4SLinus Torvalds 	enum scsi_device_state	value;
301da177e4SLinus Torvalds 	char			*name;
311da177e4SLinus Torvalds } sdev_states[] = {
321da177e4SLinus Torvalds 	{ SDEV_CREATED, "created" },
331da177e4SLinus Torvalds 	{ SDEV_RUNNING, "running" },
341da177e4SLinus Torvalds 	{ SDEV_CANCEL, "cancel" },
351da177e4SLinus Torvalds 	{ SDEV_DEL, "deleted" },
361da177e4SLinus Torvalds 	{ SDEV_QUIESCE, "quiesce" },
371da177e4SLinus Torvalds 	{ SDEV_OFFLINE,	"offline" },
381da177e4SLinus Torvalds 	{ SDEV_BLOCK,	"blocked" },
396f4267e3SJames Bottomley 	{ SDEV_CREATED_BLOCK, "created-blocked" },
401da177e4SLinus Torvalds };
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds const char *scsi_device_state_name(enum scsi_device_state state)
431da177e4SLinus Torvalds {
441da177e4SLinus Torvalds 	int i;
451da177e4SLinus Torvalds 	char *name = NULL;
461da177e4SLinus Torvalds 
476391a113STobias Klauser 	for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
481da177e4SLinus Torvalds 		if (sdev_states[i].value == state) {
491da177e4SLinus Torvalds 			name = sdev_states[i].name;
501da177e4SLinus Torvalds 			break;
511da177e4SLinus Torvalds 		}
521da177e4SLinus Torvalds 	}
531da177e4SLinus Torvalds 	return name;
541da177e4SLinus Torvalds }
551da177e4SLinus Torvalds 
560ad78200SArjan van de Ven static const struct {
57d3301874SMike Anderson 	enum scsi_host_state	value;
58d3301874SMike Anderson 	char			*name;
59d3301874SMike Anderson } shost_states[] = {
60d3301874SMike Anderson 	{ SHOST_CREATED, "created" },
61d3301874SMike Anderson 	{ SHOST_RUNNING, "running" },
62d3301874SMike Anderson 	{ SHOST_CANCEL, "cancel" },
63d3301874SMike Anderson 	{ SHOST_DEL, "deleted" },
64d3301874SMike Anderson 	{ SHOST_RECOVERY, "recovery" },
65939647eeSJames Bottomley 	{ SHOST_CANCEL_RECOVERY, "cancel/recovery" },
66939647eeSJames Bottomley 	{ SHOST_DEL_RECOVERY, "deleted/recovery", },
67d3301874SMike Anderson };
68d3301874SMike Anderson const char *scsi_host_state_name(enum scsi_host_state state)
69d3301874SMike Anderson {
70d3301874SMike Anderson 	int i;
71d3301874SMike Anderson 	char *name = NULL;
72d3301874SMike Anderson 
736391a113STobias Klauser 	for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
74d3301874SMike Anderson 		if (shost_states[i].value == state) {
75d3301874SMike Anderson 			name = shost_states[i].name;
76d3301874SMike Anderson 			break;
77d3301874SMike Anderson 		}
78d3301874SMike Anderson 	}
79d3301874SMike Anderson 	return name;
80d3301874SMike Anderson }
81d3301874SMike Anderson 
821da177e4SLinus Torvalds static int check_set(unsigned int *val, char *src)
831da177e4SLinus Torvalds {
841da177e4SLinus Torvalds 	char *last;
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds 	if (strncmp(src, "-", 20) == 0) {
871da177e4SLinus Torvalds 		*val = SCAN_WILD_CARD;
881da177e4SLinus Torvalds 	} else {
891da177e4SLinus Torvalds 		/*
901da177e4SLinus Torvalds 		 * Doesn't check for int overflow
911da177e4SLinus Torvalds 		 */
921da177e4SLinus Torvalds 		*val = simple_strtoul(src, &last, 0);
931da177e4SLinus Torvalds 		if (*last != '\0')
941da177e4SLinus Torvalds 			return 1;
951da177e4SLinus Torvalds 	}
961da177e4SLinus Torvalds 	return 0;
971da177e4SLinus Torvalds }
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds static int scsi_scan(struct Scsi_Host *shost, const char *str)
1001da177e4SLinus Torvalds {
1011da177e4SLinus Torvalds 	char s1[15], s2[15], s3[15], junk;
1021da177e4SLinus Torvalds 	unsigned int channel, id, lun;
1031da177e4SLinus Torvalds 	int res;
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 	res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk);
1061da177e4SLinus Torvalds 	if (res != 3)
1071da177e4SLinus Torvalds 		return -EINVAL;
1081da177e4SLinus Torvalds 	if (check_set(&channel, s1))
1091da177e4SLinus Torvalds 		return -EINVAL;
1101da177e4SLinus Torvalds 	if (check_set(&id, s2))
1111da177e4SLinus Torvalds 		return -EINVAL;
1121da177e4SLinus Torvalds 	if (check_set(&lun, s3))
1131da177e4SLinus Torvalds 		return -EINVAL;
114e02f3f59SChristoph Hellwig 	if (shost->transportt->user_scan)
115e02f3f59SChristoph Hellwig 		res = shost->transportt->user_scan(shost, channel, id, lun);
116e02f3f59SChristoph Hellwig 	else
1171da177e4SLinus Torvalds 		res = scsi_scan_host_selected(shost, channel, id, lun, 1);
1181da177e4SLinus Torvalds 	return res;
1191da177e4SLinus Torvalds }
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds /*
1221da177e4SLinus Torvalds  * shost_show_function: macro to create an attr function that can be used to
1231da177e4SLinus Torvalds  * show a non-bit field.
1241da177e4SLinus Torvalds  */
1251da177e4SLinus Torvalds #define shost_show_function(name, field, format_string)			\
1261da177e4SLinus Torvalds static ssize_t								\
127ee959b00STony Jones show_##name (struct device *dev, struct device_attribute *attr, 	\
128ee959b00STony Jones 	     char *buf)							\
1291da177e4SLinus Torvalds {									\
130ee959b00STony Jones 	struct Scsi_Host *shost = class_to_shost(dev);			\
1311da177e4SLinus Torvalds 	return snprintf (buf, 20, format_string, shost->field);		\
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds /*
1351da177e4SLinus Torvalds  * shost_rd_attr: macro to create a function and attribute variable for a
1361da177e4SLinus Torvalds  * read only field.
1371da177e4SLinus Torvalds  */
1381da177e4SLinus Torvalds #define shost_rd_attr2(name, field, format_string)			\
1391da177e4SLinus Torvalds 	shost_show_function(name, field, format_string)			\
140ee959b00STony Jones static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds #define shost_rd_attr(field, format_string) \
1431da177e4SLinus Torvalds shost_rd_attr2(field, field, format_string)
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds /*
1461da177e4SLinus Torvalds  * Create the actual show/store functions and data structures.
1471da177e4SLinus Torvalds  */
1481da177e4SLinus Torvalds 
149ee959b00STony Jones static ssize_t
150ee959b00STony Jones store_scan(struct device *dev, struct device_attribute *attr,
151ee959b00STony Jones 	   const char *buf, size_t count)
1521da177e4SLinus Torvalds {
153ee959b00STony Jones 	struct Scsi_Host *shost = class_to_shost(dev);
1541da177e4SLinus Torvalds 	int res;
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	res = scsi_scan(shost, buf);
1571da177e4SLinus Torvalds 	if (res == 0)
1581da177e4SLinus Torvalds 		res = count;
1591da177e4SLinus Torvalds 	return res;
1601da177e4SLinus Torvalds };
161ee959b00STony Jones static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
1621da177e4SLinus Torvalds 
163d3301874SMike Anderson static ssize_t
164ee959b00STony Jones store_shost_state(struct device *dev, struct device_attribute *attr,
165ee959b00STony Jones 		  const char *buf, size_t count)
166d3301874SMike Anderson {
167d3301874SMike Anderson 	int i;
168ee959b00STony Jones 	struct Scsi_Host *shost = class_to_shost(dev);
169d3301874SMike Anderson 	enum scsi_host_state state = 0;
170d3301874SMike Anderson 
1716391a113STobias Klauser 	for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
172d3301874SMike Anderson 		const int len = strlen(shost_states[i].name);
173d3301874SMike Anderson 		if (strncmp(shost_states[i].name, buf, len) == 0 &&
174d3301874SMike Anderson 		   buf[len] == '\n') {
175d3301874SMike Anderson 			state = shost_states[i].value;
176d3301874SMike Anderson 			break;
177d3301874SMike Anderson 		}
178d3301874SMike Anderson 	}
179d3301874SMike Anderson 	if (!state)
180d3301874SMike Anderson 		return -EINVAL;
181d3301874SMike Anderson 
182d3301874SMike Anderson 	if (scsi_host_set_state(shost, state))
183d3301874SMike Anderson 		return -EINVAL;
184d3301874SMike Anderson 	return count;
185d3301874SMike Anderson }
186d3301874SMike Anderson 
187d3301874SMike Anderson static ssize_t
188ee959b00STony Jones show_shost_state(struct device *dev, struct device_attribute *attr, char *buf)
189d3301874SMike Anderson {
190ee959b00STony Jones 	struct Scsi_Host *shost = class_to_shost(dev);
191d3301874SMike Anderson 	const char *name = scsi_host_state_name(shost->shost_state);
192d3301874SMike Anderson 
193d3301874SMike Anderson 	if (!name)
194d3301874SMike Anderson 		return -EINVAL;
195d3301874SMike Anderson 
196d3301874SMike Anderson 	return snprintf(buf, 20, "%s\n", name);
197d3301874SMike Anderson }
198d3301874SMike Anderson 
199ee959b00STony Jones /* DEVICE_ATTR(state) clashes with dev_attr_state for sdev */
200ee959b00STony Jones struct device_attribute dev_attr_hstate =
201ee959b00STony Jones 	__ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
202d3301874SMike Anderson 
2035dc2b89eSFUJITA Tomonori static ssize_t
2045dc2b89eSFUJITA Tomonori show_shost_mode(unsigned int mode, char *buf)
2055dc2b89eSFUJITA Tomonori {
2065dc2b89eSFUJITA Tomonori 	ssize_t len = 0;
2075dc2b89eSFUJITA Tomonori 
2085dc2b89eSFUJITA Tomonori 	if (mode & MODE_INITIATOR)
2095dc2b89eSFUJITA Tomonori 		len = sprintf(buf, "%s", "Initiator");
2105dc2b89eSFUJITA Tomonori 
2115dc2b89eSFUJITA Tomonori 	if (mode & MODE_TARGET)
2125dc2b89eSFUJITA Tomonori 		len += sprintf(buf + len, "%s%s", len ? ", " : "", "Target");
2135dc2b89eSFUJITA Tomonori 
2145dc2b89eSFUJITA Tomonori 	len += sprintf(buf + len, "\n");
2155dc2b89eSFUJITA Tomonori 
2165dc2b89eSFUJITA Tomonori 	return len;
2175dc2b89eSFUJITA Tomonori }
2185dc2b89eSFUJITA Tomonori 
219ee959b00STony Jones static ssize_t
220ee959b00STony Jones show_shost_supported_mode(struct device *dev, struct device_attribute *attr,
221ee959b00STony Jones 			  char *buf)
2225dc2b89eSFUJITA Tomonori {
223ee959b00STony Jones 	struct Scsi_Host *shost = class_to_shost(dev);
2247a39ac3fSJames Bottomley 	unsigned int supported_mode = shost->hostt->supported_mode;
2255dc2b89eSFUJITA Tomonori 
2267a39ac3fSJames Bottomley 	if (supported_mode == MODE_UNKNOWN)
2277a39ac3fSJames Bottomley 		/* by default this should be initiator */
2287a39ac3fSJames Bottomley 		supported_mode = MODE_INITIATOR;
2297a39ac3fSJames Bottomley 
2307a39ac3fSJames Bottomley 	return show_shost_mode(supported_mode, buf);
2315dc2b89eSFUJITA Tomonori }
2325dc2b89eSFUJITA Tomonori 
233ee959b00STony Jones static DEVICE_ATTR(supported_mode, S_IRUGO | S_IWUSR, show_shost_supported_mode, NULL);
2345dc2b89eSFUJITA Tomonori 
235ee959b00STony Jones static ssize_t
236ee959b00STony Jones show_shost_active_mode(struct device *dev,
237ee959b00STony Jones 		       struct device_attribute *attr, char *buf)
2385dc2b89eSFUJITA Tomonori {
239ee959b00STony Jones 	struct Scsi_Host *shost = class_to_shost(dev);
2405dc2b89eSFUJITA Tomonori 
2415dc2b89eSFUJITA Tomonori 	if (shost->active_mode == MODE_UNKNOWN)
2425dc2b89eSFUJITA Tomonori 		return snprintf(buf, 20, "unknown\n");
2435dc2b89eSFUJITA Tomonori 	else
2445dc2b89eSFUJITA Tomonori 		return show_shost_mode(shost->active_mode, buf);
2455dc2b89eSFUJITA Tomonori }
2465dc2b89eSFUJITA Tomonori 
247ee959b00STony Jones static DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL);
2485dc2b89eSFUJITA Tomonori 
249*29443691SVikas Chaudhary static int check_reset_type(char *str)
250*29443691SVikas Chaudhary {
251*29443691SVikas Chaudhary 	if (strncmp(str, "adapter", 10) == 0)
252*29443691SVikas Chaudhary 		return SCSI_ADAPTER_RESET;
253*29443691SVikas Chaudhary 	else if (strncmp(str, "firmware", 10) == 0)
254*29443691SVikas Chaudhary 		return SCSI_FIRMWARE_RESET;
255*29443691SVikas Chaudhary 	else
256*29443691SVikas Chaudhary 		return 0;
257*29443691SVikas Chaudhary }
258*29443691SVikas Chaudhary 
259*29443691SVikas Chaudhary static ssize_t
260*29443691SVikas Chaudhary store_host_reset(struct device *dev, struct device_attribute *attr,
261*29443691SVikas Chaudhary 		const char *buf, size_t count)
262*29443691SVikas Chaudhary {
263*29443691SVikas Chaudhary 	struct Scsi_Host *shost = class_to_shost(dev);
264*29443691SVikas Chaudhary 	struct scsi_host_template *sht = shost->hostt;
265*29443691SVikas Chaudhary 	int ret = -EINVAL;
266*29443691SVikas Chaudhary 	char str[10];
267*29443691SVikas Chaudhary 	int type;
268*29443691SVikas Chaudhary 
269*29443691SVikas Chaudhary 	sscanf(buf, "%s", str);
270*29443691SVikas Chaudhary 	type = check_reset_type(str);
271*29443691SVikas Chaudhary 
272*29443691SVikas Chaudhary 	if (!type)
273*29443691SVikas Chaudhary 		goto exit_store_host_reset;
274*29443691SVikas Chaudhary 
275*29443691SVikas Chaudhary 	if (sht->host_reset)
276*29443691SVikas Chaudhary 		ret = sht->host_reset(shost, type);
277*29443691SVikas Chaudhary 
278*29443691SVikas Chaudhary exit_store_host_reset:
279*29443691SVikas Chaudhary 	if (ret == 0)
280*29443691SVikas Chaudhary 		ret = count;
281*29443691SVikas Chaudhary 	return ret;
282*29443691SVikas Chaudhary }
283*29443691SVikas Chaudhary 
284*29443691SVikas Chaudhary static DEVICE_ATTR(host_reset, S_IWUSR, NULL, store_host_reset);
285*29443691SVikas Chaudhary 
2861da177e4SLinus Torvalds shost_rd_attr(unique_id, "%u\n");
2871da177e4SLinus Torvalds shost_rd_attr(host_busy, "%hu\n");
2881da177e4SLinus Torvalds shost_rd_attr(cmd_per_lun, "%hd\n");
289ed632da8SJames Bottomley shost_rd_attr(can_queue, "%hd\n");
2901da177e4SLinus Torvalds shost_rd_attr(sg_tablesize, "%hu\n");
29113f05c8dSMartin K. Petersen shost_rd_attr(sg_prot_tablesize, "%hu\n");
2921da177e4SLinus Torvalds shost_rd_attr(unchecked_isa_dma, "%d\n");
2934469f987SMartin K. Petersen shost_rd_attr(prot_capabilities, "%u\n");
2944469f987SMartin K. Petersen shost_rd_attr(prot_guard_type, "%hd\n");
2951da177e4SLinus Torvalds shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
2961da177e4SLinus Torvalds 
297f7120a4fSHannes Reinecke static struct attribute *scsi_sysfs_shost_attrs[] = {
298f7120a4fSHannes Reinecke 	&dev_attr_unique_id.attr,
299f7120a4fSHannes Reinecke 	&dev_attr_host_busy.attr,
300f7120a4fSHannes Reinecke 	&dev_attr_cmd_per_lun.attr,
301f7120a4fSHannes Reinecke 	&dev_attr_can_queue.attr,
302f7120a4fSHannes Reinecke 	&dev_attr_sg_tablesize.attr,
30313f05c8dSMartin K. Petersen 	&dev_attr_sg_prot_tablesize.attr,
304f7120a4fSHannes Reinecke 	&dev_attr_unchecked_isa_dma.attr,
305f7120a4fSHannes Reinecke 	&dev_attr_proc_name.attr,
306f7120a4fSHannes Reinecke 	&dev_attr_scan.attr,
307f7120a4fSHannes Reinecke 	&dev_attr_hstate.attr,
308f7120a4fSHannes Reinecke 	&dev_attr_supported_mode.attr,
309f7120a4fSHannes Reinecke 	&dev_attr_active_mode.attr,
3104469f987SMartin K. Petersen 	&dev_attr_prot_capabilities.attr,
3114469f987SMartin K. Petersen 	&dev_attr_prot_guard_type.attr,
312*29443691SVikas Chaudhary 	&dev_attr_host_reset.attr,
313f7120a4fSHannes Reinecke 	NULL
314f7120a4fSHannes Reinecke };
315f7120a4fSHannes Reinecke 
316f7120a4fSHannes Reinecke struct attribute_group scsi_shost_attr_group = {
317f7120a4fSHannes Reinecke 	.attrs =	scsi_sysfs_shost_attrs,
318f7120a4fSHannes Reinecke };
319f7120a4fSHannes Reinecke 
320a4dbd674SDavid Brownell const struct attribute_group *scsi_sysfs_shost_attr_groups[] = {
321f7120a4fSHannes Reinecke 	&scsi_shost_attr_group,
3221da177e4SLinus Torvalds 	NULL
3231da177e4SLinus Torvalds };
3241da177e4SLinus Torvalds 
325ee959b00STony Jones static void scsi_device_cls_release(struct device *class_dev)
3261da177e4SLinus Torvalds {
3271da177e4SLinus Torvalds 	struct scsi_device *sdev;
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	sdev = class_to_sdev(class_dev);
3301da177e4SLinus Torvalds 	put_device(&sdev->sdev_gendev);
3311da177e4SLinus Torvalds }
3321da177e4SLinus Torvalds 
33365f27f38SDavid Howells static void scsi_device_dev_release_usercontext(struct work_struct *work)
3341da177e4SLinus Torvalds {
3351da177e4SLinus Torvalds 	struct scsi_device *sdev;
3361da177e4SLinus Torvalds 	struct device *parent;
3371da177e4SLinus Torvalds 	struct scsi_target *starget;
338a341cd0fSJeff Garzik 	struct list_head *this, *tmp;
3391da177e4SLinus Torvalds 	unsigned long flags;
3401da177e4SLinus Torvalds 
34165f27f38SDavid Howells 	sdev = container_of(work, struct scsi_device, ew.work);
34265f27f38SDavid Howells 
34365f27f38SDavid Howells 	parent = sdev->sdev_gendev.parent;
3441da177e4SLinus Torvalds 	starget = to_scsi_target(parent);
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	spin_lock_irqsave(sdev->host->host_lock, flags);
3471da177e4SLinus Torvalds 	starget->reap_ref++;
3481da177e4SLinus Torvalds 	list_del(&sdev->siblings);
3491da177e4SLinus Torvalds 	list_del(&sdev->same_target_siblings);
3501da177e4SLinus Torvalds 	list_del(&sdev->starved_entry);
3511da177e4SLinus Torvalds 	spin_unlock_irqrestore(sdev->host->host_lock, flags);
3521da177e4SLinus Torvalds 
353a341cd0fSJeff Garzik 	cancel_work_sync(&sdev->event_work);
354a341cd0fSJeff Garzik 
355a341cd0fSJeff Garzik 	list_for_each_safe(this, tmp, &sdev->event_list) {
356a341cd0fSJeff Garzik 		struct scsi_event *evt;
357a341cd0fSJeff Garzik 
358a341cd0fSJeff Garzik 		evt = list_entry(this, struct scsi_event, node);
359a341cd0fSJeff Garzik 		list_del(&evt->node);
360a341cd0fSJeff Garzik 		kfree(evt);
361a341cd0fSJeff Garzik 	}
362a341cd0fSJeff Garzik 
363e73e079bSJames Bottomley 	blk_put_queue(sdev->request_queue);
36486cbfb56SJames Bottomley 	/* NULL queue means the device can't be used */
365c2a9331cS 	sdev->request_queue = NULL;
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	scsi_target_reap(scsi_target(sdev));
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	kfree(sdev->inquiry);
3701da177e4SLinus Torvalds 	kfree(sdev);
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	if (parent)
3731da177e4SLinus Torvalds 		put_device(parent);
3741da177e4SLinus Torvalds }
3751da177e4SLinus Torvalds 
37665110b21SJames Bottomley static void scsi_device_dev_release(struct device *dev)
37765110b21SJames Bottomley {
378ffedb452SJames Bottomley 	struct scsi_device *sdp = to_scsi_device(dev);
37965f27f38SDavid Howells 	execute_in_process_context(scsi_device_dev_release_usercontext,
380ffedb452SJames Bottomley 				   &sdp->ew);
38165110b21SJames Bottomley }
38265110b21SJames Bottomley 
38352c1da39SAdrian Bunk static struct class sdev_class = {
3841da177e4SLinus Torvalds 	.name		= "scsi_device",
385ee959b00STony Jones 	.dev_release	= scsi_device_cls_release,
3861da177e4SLinus Torvalds };
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds /* all probing is done in the individual ->probe routines */
3891da177e4SLinus Torvalds static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
3901da177e4SLinus Torvalds {
391b0ed4336SHannes Reinecke 	struct scsi_device *sdp;
392b0ed4336SHannes Reinecke 
393b0ed4336SHannes Reinecke 	if (dev->type != &scsi_dev_type)
394b0ed4336SHannes Reinecke 		return 0;
395b0ed4336SHannes Reinecke 
396b0ed4336SHannes Reinecke 	sdp = to_scsi_device(dev);
3971da177e4SLinus Torvalds 	if (sdp->no_uld_attach)
3981da177e4SLinus Torvalds 		return 0;
3991da177e4SLinus Torvalds 	return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
4001da177e4SLinus Torvalds }
4011da177e4SLinus Torvalds 
4027eff2e7aSKay Sievers static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
403d7b8bcb0SMichael Tokarev {
4041f42ea7bSJames Bottomley 	struct scsi_device *sdev;
4051f42ea7bSJames Bottomley 
4061f42ea7bSJames Bottomley 	if (dev->type != &scsi_dev_type)
4071f42ea7bSJames Bottomley 		return 0;
4081f42ea7bSJames Bottomley 
4091f42ea7bSJames Bottomley 	sdev = to_scsi_device(dev);
410d7b8bcb0SMichael Tokarev 
4117eff2e7aSKay Sievers 	add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
412d7b8bcb0SMichael Tokarev 	return 0;
413d7b8bcb0SMichael Tokarev }
414d7b8bcb0SMichael Tokarev 
4151da177e4SLinus Torvalds struct bus_type scsi_bus_type = {
4161da177e4SLinus Torvalds         .name		= "scsi",
4171da177e4SLinus Torvalds         .match		= scsi_bus_match,
418d7b8bcb0SMichael Tokarev 	.uevent		= scsi_bus_uevent,
419aa338601SRafael J. Wysocki #ifdef CONFIG_PM
420db5bd1e0SAlan Stern 	.pm		= &scsi_bus_pm_ops,
421e6da54d8SAlan Stern #endif
4221da177e4SLinus Torvalds };
423a6a8d9f8SChandra Seetharaman EXPORT_SYMBOL_GPL(scsi_bus_type);
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds int scsi_sysfs_register(void)
4261da177e4SLinus Torvalds {
4271da177e4SLinus Torvalds 	int error;
4281da177e4SLinus Torvalds 
4291da177e4SLinus Torvalds 	error = bus_register(&scsi_bus_type);
4301da177e4SLinus Torvalds 	if (!error) {
4311da177e4SLinus Torvalds 		error = class_register(&sdev_class);
4321da177e4SLinus Torvalds 		if (error)
4331da177e4SLinus Torvalds 			bus_unregister(&scsi_bus_type);
4341da177e4SLinus Torvalds 	}
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	return error;
4371da177e4SLinus Torvalds }
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds void scsi_sysfs_unregister(void)
4401da177e4SLinus Torvalds {
4411da177e4SLinus Torvalds 	class_unregister(&sdev_class);
4421da177e4SLinus Torvalds 	bus_unregister(&scsi_bus_type);
4431da177e4SLinus Torvalds }
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds /*
4461da177e4SLinus Torvalds  * sdev_show_function: macro to create an attr function that can be used to
4471da177e4SLinus Torvalds  * show a non-bit field.
4481da177e4SLinus Torvalds  */
4491da177e4SLinus Torvalds #define sdev_show_function(field, format_string)				\
4501da177e4SLinus Torvalds static ssize_t								\
451ee959b00STony Jones sdev_show_##field (struct device *dev, struct device_attribute *attr,	\
452ee959b00STony Jones 		   char *buf)						\
4531da177e4SLinus Torvalds {									\
4541da177e4SLinus Torvalds 	struct scsi_device *sdev;					\
4551da177e4SLinus Torvalds 	sdev = to_scsi_device(dev);					\
4561da177e4SLinus Torvalds 	return snprintf (buf, 20, format_string, sdev->field);		\
4571da177e4SLinus Torvalds }									\
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds /*
4601da177e4SLinus Torvalds  * sdev_rd_attr: macro to create a function and attribute variable for a
4611da177e4SLinus Torvalds  * read only field.
4621da177e4SLinus Torvalds  */
4631da177e4SLinus Torvalds #define sdev_rd_attr(field, format_string)				\
4641da177e4SLinus Torvalds 	sdev_show_function(field, format_string)			\
4651da177e4SLinus Torvalds static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds /*
469160e7f67STomohiro Kusumi  * sdev_rw_attr: create a function and attribute variable for a
4701da177e4SLinus Torvalds  * read/write field.
4711da177e4SLinus Torvalds  */
4721da177e4SLinus Torvalds #define sdev_rw_attr(field, format_string)				\
4731da177e4SLinus Torvalds 	sdev_show_function(field, format_string)				\
4741da177e4SLinus Torvalds 									\
4751da177e4SLinus Torvalds static ssize_t								\
476ee959b00STony Jones sdev_store_##field (struct device *dev, struct device_attribute *attr,	\
477ee959b00STony Jones 		    const char *buf, size_t count)			\
4781da177e4SLinus Torvalds {									\
4791da177e4SLinus Torvalds 	struct scsi_device *sdev;					\
4801da177e4SLinus Torvalds 	sdev = to_scsi_device(dev);					\
481160e7f67STomohiro Kusumi 	sscanf (buf, format_string, &sdev->field);			\
4821da177e4SLinus Torvalds 	return count;							\
4831da177e4SLinus Torvalds }									\
4841da177e4SLinus Torvalds static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds /* Currently we don't export bit fields, but we might in future,
4871da177e4SLinus Torvalds  * so leave this code in */
4881da177e4SLinus Torvalds #if 0
4891da177e4SLinus Torvalds /*
4901da177e4SLinus Torvalds  * sdev_rd_attr: create a function and attribute variable for a
4911da177e4SLinus Torvalds  * read/write bit field.
4921da177e4SLinus Torvalds  */
4931da177e4SLinus Torvalds #define sdev_rw_attr_bit(field)						\
4941da177e4SLinus Torvalds 	sdev_show_function(field, "%d\n")					\
4951da177e4SLinus Torvalds 									\
4961da177e4SLinus Torvalds static ssize_t								\
497ee959b00STony Jones sdev_store_##field (struct device *dev, struct device_attribute *attr,	\
498ee959b00STony Jones 		    const char *buf, size_t count)			\
4991da177e4SLinus Torvalds {									\
5001da177e4SLinus Torvalds 	int ret;							\
5011da177e4SLinus Torvalds 	struct scsi_device *sdev;					\
5021da177e4SLinus Torvalds 	ret = scsi_sdev_check_buf_bit(buf);				\
5031da177e4SLinus Torvalds 	if (ret >= 0)	{						\
5041da177e4SLinus Torvalds 		sdev = to_scsi_device(dev);				\
5051da177e4SLinus Torvalds 		sdev->field = ret;					\
5061da177e4SLinus Torvalds 		ret = count;						\
5071da177e4SLinus Torvalds 	}								\
5081da177e4SLinus Torvalds 	return ret;							\
5091da177e4SLinus Torvalds }									\
5101da177e4SLinus Torvalds static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds /*
5131da177e4SLinus Torvalds  * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
5141da177e4SLinus Torvalds  * else return -EINVAL.
5151da177e4SLinus Torvalds  */
5161da177e4SLinus Torvalds static int scsi_sdev_check_buf_bit(const char *buf)
5171da177e4SLinus Torvalds {
5181da177e4SLinus Torvalds 	if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
5191da177e4SLinus Torvalds 		if (buf[0] == '1')
5201da177e4SLinus Torvalds 			return 1;
5211da177e4SLinus Torvalds 		else if (buf[0] == '0')
5221da177e4SLinus Torvalds 			return 0;
5231da177e4SLinus Torvalds 		else
5241da177e4SLinus Torvalds 			return -EINVAL;
5251da177e4SLinus Torvalds 	} else
5261da177e4SLinus Torvalds 		return -EINVAL;
5271da177e4SLinus Torvalds }
5281da177e4SLinus Torvalds #endif
5291da177e4SLinus Torvalds /*
5301da177e4SLinus Torvalds  * Create the actual show/store functions and data structures.
5311da177e4SLinus Torvalds  */
5321da177e4SLinus Torvalds sdev_rd_attr (device_blocked, "%d\n");
5331da177e4SLinus Torvalds sdev_rd_attr (queue_depth, "%d\n");
5341da177e4SLinus Torvalds sdev_rd_attr (type, "%d\n");
5351da177e4SLinus Torvalds sdev_rd_attr (scsi_level, "%d\n");
5361da177e4SLinus Torvalds sdev_rd_attr (vendor, "%.8s\n");
5371da177e4SLinus Torvalds sdev_rd_attr (model, "%.16s\n");
5381da177e4SLinus Torvalds sdev_rd_attr (rev, "%.4s\n");
5391da177e4SLinus Torvalds 
540242f9dcbSJens Axboe /*
541242f9dcbSJens Axboe  * TODO: can we make these symlinks to the block layer ones?
542242f9dcbSJens Axboe  */
5431da177e4SLinus Torvalds static ssize_t
54410523b3bSYani Ioannou sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
5451da177e4SLinus Torvalds {
5461da177e4SLinus Torvalds 	struct scsi_device *sdev;
5471da177e4SLinus Torvalds 	sdev = to_scsi_device(dev);
548242f9dcbSJens Axboe 	return snprintf(buf, 20, "%d\n", sdev->request_queue->rq_timeout / HZ);
5491da177e4SLinus Torvalds }
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds static ssize_t
552ee959b00STony Jones sdev_store_timeout (struct device *dev, struct device_attribute *attr,
553ee959b00STony Jones 		    const char *buf, size_t count)
5541da177e4SLinus Torvalds {
5551da177e4SLinus Torvalds 	struct scsi_device *sdev;
5561da177e4SLinus Torvalds 	int timeout;
5571da177e4SLinus Torvalds 	sdev = to_scsi_device(dev);
5581da177e4SLinus Torvalds 	sscanf (buf, "%d\n", &timeout);
559242f9dcbSJens Axboe 	blk_queue_rq_timeout(sdev->request_queue, timeout * HZ);
5601da177e4SLinus Torvalds 	return count;
5611da177e4SLinus Torvalds }
5621da177e4SLinus Torvalds static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds static ssize_t
565ee959b00STony Jones store_rescan_field (struct device *dev, struct device_attribute *attr,
566ee959b00STony Jones 		    const char *buf, size_t count)
5671da177e4SLinus Torvalds {
5681da177e4SLinus Torvalds 	scsi_rescan_device(dev);
5691da177e4SLinus Torvalds 	return count;
5701da177e4SLinus Torvalds }
5711da177e4SLinus Torvalds static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
5721da177e4SLinus Torvalds 
573d9a9cdfbSAlan Stern static void sdev_store_delete_callback(struct device *dev)
574d9a9cdfbSAlan Stern {
575d9a9cdfbSAlan Stern 	scsi_remove_device(to_scsi_device(dev));
576d9a9cdfbSAlan Stern }
577d9a9cdfbSAlan Stern 
578ee959b00STony Jones static ssize_t
579ee959b00STony Jones sdev_store_delete(struct device *dev, struct device_attribute *attr,
580ee959b00STony Jones 		  const char *buf, size_t count)
5811da177e4SLinus Torvalds {
582d9a9cdfbSAlan Stern 	int rc;
583d9a9cdfbSAlan Stern 
584d9a9cdfbSAlan Stern 	/* An attribute cannot be unregistered by one of its own methods,
585d9a9cdfbSAlan Stern 	 * so we have to use this roundabout approach.
586d9a9cdfbSAlan Stern 	 */
587d9a9cdfbSAlan Stern 	rc = device_schedule_callback(dev, sdev_store_delete_callback);
588d9a9cdfbSAlan Stern 	if (rc)
589d9a9cdfbSAlan Stern 		count = rc;
5901da177e4SLinus Torvalds 	return count;
5911da177e4SLinus Torvalds };
5921da177e4SLinus Torvalds static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds static ssize_t
595ee959b00STony Jones store_state_field(struct device *dev, struct device_attribute *attr,
596ee959b00STony Jones 		  const char *buf, size_t count)
5971da177e4SLinus Torvalds {
5981da177e4SLinus Torvalds 	int i;
5991da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
6001da177e4SLinus Torvalds 	enum scsi_device_state state = 0;
6011da177e4SLinus Torvalds 
6026391a113STobias Klauser 	for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
6031da177e4SLinus Torvalds 		const int len = strlen(sdev_states[i].name);
6041da177e4SLinus Torvalds 		if (strncmp(sdev_states[i].name, buf, len) == 0 &&
6051da177e4SLinus Torvalds 		   buf[len] == '\n') {
6061da177e4SLinus Torvalds 			state = sdev_states[i].value;
6071da177e4SLinus Torvalds 			break;
6081da177e4SLinus Torvalds 		}
6091da177e4SLinus Torvalds 	}
6101da177e4SLinus Torvalds 	if (!state)
6111da177e4SLinus Torvalds 		return -EINVAL;
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds 	if (scsi_device_set_state(sdev, state))
6141da177e4SLinus Torvalds 		return -EINVAL;
6151da177e4SLinus Torvalds 	return count;
6161da177e4SLinus Torvalds }
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds static ssize_t
61910523b3bSYani Ioannou show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
6201da177e4SLinus Torvalds {
6211da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
6221da177e4SLinus Torvalds 	const char *name = scsi_device_state_name(sdev->sdev_state);
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	if (!name)
6251da177e4SLinus Torvalds 		return -EINVAL;
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 	return snprintf(buf, 20, "%s\n", name);
6281da177e4SLinus Torvalds }
6291da177e4SLinus Torvalds 
6301da177e4SLinus Torvalds static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds static ssize_t
633ee959b00STony Jones show_queue_type_field(struct device *dev, struct device_attribute *attr,
634ee959b00STony Jones 		      char *buf)
6351da177e4SLinus Torvalds {
6361da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
6371da177e4SLinus Torvalds 	const char *name = "none";
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	if (sdev->ordered_tags)
6401da177e4SLinus Torvalds 		name = "ordered";
6411da177e4SLinus Torvalds 	else if (sdev->simple_tags)
6421da177e4SLinus Torvalds 		name = "simple";
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 	return snprintf(buf, 20, "%s\n", name);
6451da177e4SLinus Torvalds }
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds static ssize_t
65010523b3bSYani Ioannou show_iostat_counterbits(struct device *dev, struct device_attribute *attr, 				char *buf)
6511da177e4SLinus Torvalds {
6521da177e4SLinus Torvalds 	return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
6531da177e4SLinus Torvalds }
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds #define show_sdev_iostat(field)						\
6581da177e4SLinus Torvalds static ssize_t								\
659ee959b00STony Jones show_iostat_##field(struct device *dev, struct device_attribute *attr,	\
660ee959b00STony Jones 		    char *buf)						\
6611da177e4SLinus Torvalds {									\
6621da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);			\
6631da177e4SLinus Torvalds 	unsigned long long count = atomic_read(&sdev->field);		\
6641da177e4SLinus Torvalds 	return snprintf(buf, 20, "0x%llx\n", count);			\
6651da177e4SLinus Torvalds }									\
6661da177e4SLinus Torvalds static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds show_sdev_iostat(iorequest_cnt);
6691da177e4SLinus Torvalds show_sdev_iostat(iodone_cnt);
6701da177e4SLinus Torvalds show_sdev_iostat(ioerr_cnt);
6711da177e4SLinus Torvalds 
672d7b8bcb0SMichael Tokarev static ssize_t
673d7b8bcb0SMichael Tokarev sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
674d7b8bcb0SMichael Tokarev {
675d7b8bcb0SMichael Tokarev 	struct scsi_device *sdev;
676d7b8bcb0SMichael Tokarev 	sdev = to_scsi_device(dev);
677d7b8bcb0SMichael Tokarev 	return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
678d7b8bcb0SMichael Tokarev }
679d7b8bcb0SMichael Tokarev static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
6801da177e4SLinus Torvalds 
681a341cd0fSJeff Garzik #define DECLARE_EVT_SHOW(name, Cap_name)				\
682a341cd0fSJeff Garzik static ssize_t								\
683a341cd0fSJeff Garzik sdev_show_evt_##name(struct device *dev, struct device_attribute *attr,	\
684a341cd0fSJeff Garzik 		     char *buf)						\
685a341cd0fSJeff Garzik {									\
686a341cd0fSJeff Garzik 	struct scsi_device *sdev = to_scsi_device(dev);			\
687a341cd0fSJeff Garzik 	int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
688a341cd0fSJeff Garzik 	return snprintf(buf, 20, "%d\n", val);				\
689a341cd0fSJeff Garzik }
690a341cd0fSJeff Garzik 
691a341cd0fSJeff Garzik #define DECLARE_EVT_STORE(name, Cap_name)				\
692a341cd0fSJeff Garzik static ssize_t								\
693a341cd0fSJeff Garzik sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
694a341cd0fSJeff Garzik 		      const char *buf, size_t count)			\
695a341cd0fSJeff Garzik {									\
696a341cd0fSJeff Garzik 	struct scsi_device *sdev = to_scsi_device(dev);			\
697a341cd0fSJeff Garzik 	int val = simple_strtoul(buf, NULL, 0);				\
698a341cd0fSJeff Garzik 	if (val == 0)							\
699a341cd0fSJeff Garzik 		clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events);	\
700a341cd0fSJeff Garzik 	else if (val == 1)						\
701a341cd0fSJeff Garzik 		set_bit(SDEV_EVT_##Cap_name, sdev->supported_events);	\
702a341cd0fSJeff Garzik 	else								\
703a341cd0fSJeff Garzik 		return -EINVAL;						\
704a341cd0fSJeff Garzik 	return count;							\
705a341cd0fSJeff Garzik }
706a341cd0fSJeff Garzik 
707a341cd0fSJeff Garzik #define DECLARE_EVT(name, Cap_name)					\
708a341cd0fSJeff Garzik 	DECLARE_EVT_SHOW(name, Cap_name)				\
709a341cd0fSJeff Garzik 	DECLARE_EVT_STORE(name, Cap_name)				\
710a341cd0fSJeff Garzik 	static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name,	\
711a341cd0fSJeff Garzik 			   sdev_store_evt_##name);
712a341cd0fSJeff Garzik #define REF_EVT(name) &dev_attr_evt_##name.attr
713a341cd0fSJeff Garzik 
714a341cd0fSJeff Garzik DECLARE_EVT(media_change, MEDIA_CHANGE)
715a341cd0fSJeff Garzik 
7161da177e4SLinus Torvalds /* Default template for device attributes.  May NOT be modified */
717bfd12944SKay Sievers static struct attribute *scsi_sdev_attrs[] = {
718bfd12944SKay Sievers 	&dev_attr_device_blocked.attr,
719bfd12944SKay Sievers 	&dev_attr_type.attr,
720bfd12944SKay Sievers 	&dev_attr_scsi_level.attr,
721bfd12944SKay Sievers 	&dev_attr_vendor.attr,
722bfd12944SKay Sievers 	&dev_attr_model.attr,
723bfd12944SKay Sievers 	&dev_attr_rev.attr,
724bfd12944SKay Sievers 	&dev_attr_rescan.attr,
725bfd12944SKay Sievers 	&dev_attr_delete.attr,
726bfd12944SKay Sievers 	&dev_attr_state.attr,
727bfd12944SKay Sievers 	&dev_attr_timeout.attr,
728bfd12944SKay Sievers 	&dev_attr_iocounterbits.attr,
729bfd12944SKay Sievers 	&dev_attr_iorequest_cnt.attr,
730bfd12944SKay Sievers 	&dev_attr_iodone_cnt.attr,
731bfd12944SKay Sievers 	&dev_attr_ioerr_cnt.attr,
732bfd12944SKay Sievers 	&dev_attr_modalias.attr,
733a341cd0fSJeff Garzik 	REF_EVT(media_change),
734bfd12944SKay Sievers 	NULL
735bfd12944SKay Sievers };
736bfd12944SKay Sievers 
737bfd12944SKay Sievers static struct attribute_group scsi_sdev_attr_group = {
738bfd12944SKay Sievers 	.attrs =	scsi_sdev_attrs,
739bfd12944SKay Sievers };
740bfd12944SKay Sievers 
741a4dbd674SDavid Brownell static const struct attribute_group *scsi_sdev_attr_groups[] = {
742bfd12944SKay Sievers 	&scsi_sdev_attr_group,
7431da177e4SLinus Torvalds 	NULL
7441da177e4SLinus Torvalds };
7451da177e4SLinus Torvalds 
746ee959b00STony Jones static ssize_t
747ee959b00STony Jones sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr,
748ee959b00STony Jones 			  const char *buf, size_t count)
7491da177e4SLinus Torvalds {
7501da177e4SLinus Torvalds 	int depth, retval;
7511da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
7521da177e4SLinus Torvalds 	struct scsi_host_template *sht = sdev->host->hostt;
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds 	if (!sht->change_queue_depth)
7551da177e4SLinus Torvalds 		return -EINVAL;
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 	depth = simple_strtoul(buf, NULL, 0);
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	if (depth < 1)
7601da177e4SLinus Torvalds 		return -EINVAL;
7611da177e4SLinus Torvalds 
762e881a172SMike Christie 	retval = sht->change_queue_depth(sdev, depth,
763e881a172SMike Christie 					 SCSI_QDEPTH_DEFAULT);
7641da177e4SLinus Torvalds 	if (retval < 0)
7651da177e4SLinus Torvalds 		return retval;
7661da177e4SLinus Torvalds 
7674a84067dSVasu Dev 	sdev->max_queue_depth = sdev->queue_depth;
7684a84067dSVasu Dev 
7691da177e4SLinus Torvalds 	return count;
7701da177e4SLinus Torvalds }
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds static struct device_attribute sdev_attr_queue_depth_rw =
7731da177e4SLinus Torvalds 	__ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
7741da177e4SLinus Torvalds 	       sdev_store_queue_depth_rw);
7751da177e4SLinus Torvalds 
776ee959b00STony Jones static ssize_t
7774a84067dSVasu Dev sdev_show_queue_ramp_up_period(struct device *dev,
7784a84067dSVasu Dev 			       struct device_attribute *attr,
7794a84067dSVasu Dev 			       char *buf)
7804a84067dSVasu Dev {
7814a84067dSVasu Dev 	struct scsi_device *sdev;
7824a84067dSVasu Dev 	sdev = to_scsi_device(dev);
7834a84067dSVasu Dev 	return snprintf(buf, 20, "%u\n",
7844a84067dSVasu Dev 			jiffies_to_msecs(sdev->queue_ramp_up_period));
7854a84067dSVasu Dev }
7864a84067dSVasu Dev 
7874a84067dSVasu Dev static ssize_t
7884a84067dSVasu Dev sdev_store_queue_ramp_up_period(struct device *dev,
7894a84067dSVasu Dev 				struct device_attribute *attr,
7904a84067dSVasu Dev 				const char *buf, size_t count)
7914a84067dSVasu Dev {
7924a84067dSVasu Dev 	struct scsi_device *sdev = to_scsi_device(dev);
7934a84067dSVasu Dev 	unsigned long period;
7944a84067dSVasu Dev 
7954a84067dSVasu Dev 	if (strict_strtoul(buf, 10, &period))
7964a84067dSVasu Dev 		return -EINVAL;
7974a84067dSVasu Dev 
7984a84067dSVasu Dev 	sdev->queue_ramp_up_period = msecs_to_jiffies(period);
7994a84067dSVasu Dev 	return period;
8004a84067dSVasu Dev }
8014a84067dSVasu Dev 
8024a84067dSVasu Dev static struct device_attribute sdev_attr_queue_ramp_up_period =
8034a84067dSVasu Dev 	__ATTR(queue_ramp_up_period, S_IRUGO | S_IWUSR,
8044a84067dSVasu Dev 	       sdev_show_queue_ramp_up_period,
8054a84067dSVasu Dev 	       sdev_store_queue_ramp_up_period);
8064a84067dSVasu Dev 
8074a84067dSVasu Dev static ssize_t
808ee959b00STony Jones sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr,
809ee959b00STony Jones 			 const char *buf, size_t count)
8101da177e4SLinus Torvalds {
8111da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
8121da177e4SLinus Torvalds 	struct scsi_host_template *sht = sdev->host->hostt;
8131da177e4SLinus Torvalds 	int tag_type = 0, retval;
8141da177e4SLinus Torvalds 	int prev_tag_type = scsi_get_tag_type(sdev);
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds 	if (!sdev->tagged_supported || !sht->change_queue_type)
8171da177e4SLinus Torvalds 		return -EINVAL;
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 	if (strncmp(buf, "ordered", 7) == 0)
8201da177e4SLinus Torvalds 		tag_type = MSG_ORDERED_TAG;
8211da177e4SLinus Torvalds 	else if (strncmp(buf, "simple", 6) == 0)
8221da177e4SLinus Torvalds 		tag_type = MSG_SIMPLE_TAG;
8231da177e4SLinus Torvalds 	else if (strncmp(buf, "none", 4) != 0)
8241da177e4SLinus Torvalds 		return -EINVAL;
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 	if (tag_type == prev_tag_type)
8271da177e4SLinus Torvalds 		return count;
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds 	retval = sht->change_queue_type(sdev, tag_type);
8301da177e4SLinus Torvalds 	if (retval < 0)
8311da177e4SLinus Torvalds 		return retval;
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds 	return count;
8341da177e4SLinus Torvalds }
8351da177e4SLinus Torvalds 
836643eb2d9SJames Bottomley static int scsi_target_add(struct scsi_target *starget)
837643eb2d9SJames Bottomley {
838643eb2d9SJames Bottomley 	int error;
839643eb2d9SJames Bottomley 
840643eb2d9SJames Bottomley 	if (starget->state != STARGET_CREATED)
841643eb2d9SJames Bottomley 		return 0;
842643eb2d9SJames Bottomley 
843643eb2d9SJames Bottomley 	error = device_add(&starget->dev);
844643eb2d9SJames Bottomley 	if (error) {
845643eb2d9SJames Bottomley 		dev_err(&starget->dev, "target device_add failed, error %d\n", error);
846643eb2d9SJames Bottomley 		return error;
847643eb2d9SJames Bottomley 	}
848643eb2d9SJames Bottomley 	transport_add_device(&starget->dev);
849643eb2d9SJames Bottomley 	starget->state = STARGET_RUNNING;
850643eb2d9SJames Bottomley 
851bc4f2401SAlan Stern 	pm_runtime_set_active(&starget->dev);
852bc4f2401SAlan Stern 	pm_runtime_enable(&starget->dev);
853bc4f2401SAlan Stern 	device_enable_async_suspend(&starget->dev);
854bc4f2401SAlan Stern 
855643eb2d9SJames Bottomley 	return 0;
856643eb2d9SJames Bottomley }
857643eb2d9SJames Bottomley 
8581da177e4SLinus Torvalds static struct device_attribute sdev_attr_queue_type_rw =
8591da177e4SLinus Torvalds 	__ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
8601da177e4SLinus Torvalds 	       sdev_store_queue_type_rw);
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds /**
8631da177e4SLinus Torvalds  * scsi_sysfs_add_sdev - add scsi device to sysfs
8641da177e4SLinus Torvalds  * @sdev:	scsi_device to add
8651da177e4SLinus Torvalds  *
8661da177e4SLinus Torvalds  * Return value:
8671da177e4SLinus Torvalds  * 	0 on Success / non-zero on Failure
8681da177e4SLinus Torvalds  **/
8691da177e4SLinus Torvalds int scsi_sysfs_add_sdev(struct scsi_device *sdev)
8701da177e4SLinus Torvalds {
8711da177e4SLinus Torvalds 	int error, i;
87280ed71ceSJames Bottomley 	struct request_queue *rq = sdev->request_queue;
873643eb2d9SJames Bottomley 	struct scsi_target *starget = sdev->sdev_target;
8741da177e4SLinus Torvalds 
875ee37e09dSAlan Stern 	error = scsi_device_set_state(sdev, SDEV_RUNNING);
876ee37e09dSAlan Stern 	if (error)
8771da177e4SLinus Torvalds 		return error;
8781da177e4SLinus Torvalds 
879643eb2d9SJames Bottomley 	error = scsi_target_add(starget);
880643eb2d9SJames Bottomley 	if (error)
881643eb2d9SJames Bottomley 		return error;
882643eb2d9SJames Bottomley 
883643eb2d9SJames Bottomley 	transport_configure_device(&starget->dev);
884bc4f2401SAlan Stern 
8854cb077d9SRafael J. Wysocki 	device_enable_async_suspend(&sdev->sdev_gendev);
886bc4f2401SAlan Stern 	scsi_autopm_get_target(starget);
887bc4f2401SAlan Stern 	pm_runtime_set_active(&sdev->sdev_gendev);
888bc4f2401SAlan Stern 	pm_runtime_forbid(&sdev->sdev_gendev);
889bc4f2401SAlan Stern 	pm_runtime_enable(&sdev->sdev_gendev);
890bc4f2401SAlan Stern 	scsi_autopm_put_target(starget);
891bc4f2401SAlan Stern 
892bc4f2401SAlan Stern 	/* The following call will keep sdev active indefinitely, until
893bc4f2401SAlan Stern 	 * its driver does a corresponding scsi_autopm_pm_device().  Only
894bc4f2401SAlan Stern 	 * drivers supporting autosuspend will do this.
895bc4f2401SAlan Stern 	 */
896bc4f2401SAlan Stern 	scsi_autopm_get_device(sdev);
897bc4f2401SAlan Stern 
8981da177e4SLinus Torvalds 	error = device_add(&sdev->sdev_gendev);
8991da177e4SLinus Torvalds 	if (error) {
90073d8c34fSAlan Stern 		sdev_printk(KERN_INFO, sdev,
90173d8c34fSAlan Stern 				"failed to add device: %d\n", error);
902ee37e09dSAlan Stern 		return error;
9031da177e4SLinus Torvalds 	}
9044cb077d9SRafael J. Wysocki 	device_enable_async_suspend(&sdev->sdev_dev);
905ee959b00STony Jones 	error = device_add(&sdev->sdev_dev);
9061da177e4SLinus Torvalds 	if (error) {
90773d8c34fSAlan Stern 		sdev_printk(KERN_INFO, sdev,
90873d8c34fSAlan Stern 				"failed to add class device: %d\n", error);
909860dc736SJames Bottomley 		device_del(&sdev->sdev_gendev);
910ee37e09dSAlan Stern 		return error;
9111da177e4SLinus Torvalds 	}
912860dc736SJames Bottomley 	transport_add_device(&sdev->sdev_gendev);
913860dc736SJames Bottomley 	sdev->is_visible = 1;
9141da177e4SLinus Torvalds 
915bfd12944SKay Sievers 	/* create queue files, which may be writable, depending on the host */
9164a84067dSVasu Dev 	if (sdev->host->hostt->change_queue_depth) {
9174a84067dSVasu Dev 		error = device_create_file(&sdev->sdev_gendev,
9184a84067dSVasu Dev 					   &sdev_attr_queue_depth_rw);
9194a84067dSVasu Dev 		error = device_create_file(&sdev->sdev_gendev,
9204a84067dSVasu Dev 					   &sdev_attr_queue_ramp_up_period);
9214a84067dSVasu Dev 	}
922bfd12944SKay Sievers 	else
923bfd12944SKay Sievers 		error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_depth);
924860dc736SJames Bottomley 	if (error)
925ee37e09dSAlan Stern 		return error;
926860dc736SJames Bottomley 
927bfd12944SKay Sievers 	if (sdev->host->hostt->change_queue_type)
928bfd12944SKay Sievers 		error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_type_rw);
929bfd12944SKay Sievers 	else
930bfd12944SKay Sievers 		error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_type);
931860dc736SJames Bottomley 	if (error)
932ee37e09dSAlan Stern 		return error;
933bfd12944SKay Sievers 
93497f46ae4SFUJITA Tomonori 	error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL, NULL);
93580ed71ceSJames Bottomley 
93680ed71ceSJames Bottomley 	if (error)
937860dc736SJames Bottomley 		/* we're treating error on bsg register as non-fatal,
938860dc736SJames Bottomley 		 * so pretend nothing went wrong */
93980ed71ceSJames Bottomley 		sdev_printk(KERN_INFO, sdev,
94080ed71ceSJames Bottomley 			    "Failed to register bsg queue, errno=%d\n", error);
94180ed71ceSJames Bottomley 
942bfd12944SKay Sievers 	/* add additional host specific attributes */
9431da177e4SLinus Torvalds 	if (sdev->host->hostt->sdev_attrs) {
9441da177e4SLinus Torvalds 		for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
945bfd12944SKay Sievers 			error = device_create_file(&sdev->sdev_gendev,
9461da177e4SLinus Torvalds 					sdev->host->hostt->sdev_attrs[i]);
947860dc736SJames Bottomley 			if (error)
9481da177e4SLinus Torvalds 				return error;
949ee37e09dSAlan Stern 		}
950ee37e09dSAlan Stern 	}
9511da177e4SLinus Torvalds 
952ee37e09dSAlan Stern 	return error;
9531da177e4SLinus Torvalds }
9541da177e4SLinus Torvalds 
955903f4fedSAlan Stern void __scsi_remove_device(struct scsi_device *sdev)
9561da177e4SLinus Torvalds {
957df133c21SJames Bottomley 	struct device *dev = &sdev->sdev_gendev;
958df133c21SJames Bottomley 
959860dc736SJames Bottomley 	if (sdev->is_visible) {
9601da177e4SLinus Torvalds 		if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
961903f4fedSAlan Stern 			return;
9621da177e4SLinus Torvalds 
9630feed274SJames Bottomley 		bsg_unregister_queue(sdev->request_queue);
964ee959b00STony Jones 		device_unregister(&sdev->sdev_dev);
965df133c21SJames Bottomley 		transport_remove_device(dev);
966df133c21SJames Bottomley 		device_del(dev);
967860dc736SJames Bottomley 	} else
968860dc736SJames Bottomley 		put_device(&sdev->sdev_dev);
9691da177e4SLinus Torvalds 	scsi_device_set_state(sdev, SDEV_DEL);
9701da177e4SLinus Torvalds 	if (sdev->host->hostt->slave_destroy)
9711da177e4SLinus Torvalds 		sdev->host->hostt->slave_destroy(sdev);
972df133c21SJames Bottomley 	transport_destroy_device(dev);
97386cbfb56SJames Bottomley 
97486cbfb56SJames Bottomley 	/* cause the request function to reject all I/O requests */
97586cbfb56SJames Bottomley 	sdev->request_queue->queuedata = NULL;
97686cbfb56SJames Bottomley 
97786cbfb56SJames Bottomley 	/* Freeing the queue signals to block that we're done */
97886cbfb56SJames Bottomley 	scsi_free_queue(sdev->request_queue);
979df133c21SJames Bottomley 	put_device(dev);
980903f4fedSAlan Stern }
981903f4fedSAlan Stern 
982903f4fedSAlan Stern /**
983903f4fedSAlan Stern  * scsi_remove_device - unregister a device from the scsi bus
984903f4fedSAlan Stern  * @sdev:	scsi_device to unregister
985903f4fedSAlan Stern  **/
986903f4fedSAlan Stern void scsi_remove_device(struct scsi_device *sdev)
987903f4fedSAlan Stern {
98854195002SAlan Stern 	struct Scsi_Host *shost = sdev->host;
98954195002SAlan Stern 
9900b950672SArjan van de Ven 	mutex_lock(&shost->scan_mutex);
991903f4fedSAlan Stern 	__scsi_remove_device(sdev);
9920b950672SArjan van de Ven 	mutex_unlock(&shost->scan_mutex);
9931da177e4SLinus Torvalds }
9941da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_remove_device);
9951da177e4SLinus Torvalds 
99644818efbSAdrian Bunk static void __scsi_remove_target(struct scsi_target *starget)
9971da177e4SLinus Torvalds {
9981da177e4SLinus Torvalds 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
9991da177e4SLinus Torvalds 	unsigned long flags;
1000a64358dbSAlan Stern 	struct scsi_device *sdev;
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
10031da177e4SLinus Torvalds 	starget->reap_ref++;
1004a64358dbSAlan Stern  restart:
1005a64358dbSAlan Stern 	list_for_each_entry(sdev, &shost->__devices, siblings) {
10061da177e4SLinus Torvalds 		if (sdev->channel != starget->channel ||
1007a64358dbSAlan Stern 		    sdev->id != starget->id ||
1008546ae796SChristof Schmitt 		    scsi_device_get(sdev))
10091da177e4SLinus Torvalds 			continue;
10101da177e4SLinus Torvalds 		spin_unlock_irqrestore(shost->host_lock, flags);
10111da177e4SLinus Torvalds 		scsi_remove_device(sdev);
1012546ae796SChristof Schmitt 		scsi_device_put(sdev);
10131da177e4SLinus Torvalds 		spin_lock_irqsave(shost->host_lock, flags);
1014a64358dbSAlan Stern 		goto restart;
10151da177e4SLinus Torvalds 	}
10161da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
10171da177e4SLinus Torvalds 	scsi_target_reap(starget);
10181da177e4SLinus Torvalds }
10191da177e4SLinus Torvalds 
102020b1e674Smochel@digitalimplant.org static int __remove_child (struct device * dev, void * data)
102120b1e674Smochel@digitalimplant.org {
102220b1e674Smochel@digitalimplant.org 	if (scsi_is_target_device(dev))
102320b1e674Smochel@digitalimplant.org 		__scsi_remove_target(to_scsi_target(dev));
102420b1e674Smochel@digitalimplant.org 	return 0;
102520b1e674Smochel@digitalimplant.org }
102620b1e674Smochel@digitalimplant.org 
10271da177e4SLinus Torvalds /**
10281da177e4SLinus Torvalds  * scsi_remove_target - try to remove a target and all its devices
10291da177e4SLinus Torvalds  * @dev: generic starget or parent of generic stargets to be removed
10301da177e4SLinus Torvalds  *
10311da177e4SLinus Torvalds  * Note: This is slightly racy.  It is possible that if the user
10321da177e4SLinus Torvalds  * requests the addition of another device then the target won't be
10331da177e4SLinus Torvalds  * removed.
10341da177e4SLinus Torvalds  */
10351da177e4SLinus Torvalds void scsi_remove_target(struct device *dev)
10361da177e4SLinus Torvalds {
10371da177e4SLinus Torvalds 	if (scsi_is_target_device(dev)) {
10381da177e4SLinus Torvalds 		__scsi_remove_target(to_scsi_target(dev));
10391da177e4SLinus Torvalds 		return;
10401da177e4SLinus Torvalds 	}
10411da177e4SLinus Torvalds 
104241511704SAlan Stern 	get_device(dev);
104320b1e674Smochel@digitalimplant.org 	device_for_each_child(dev, NULL, __remove_child);
104441511704SAlan Stern 	put_device(dev);
10451da177e4SLinus Torvalds }
10461da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_remove_target);
10471da177e4SLinus Torvalds 
10481da177e4SLinus Torvalds int scsi_register_driver(struct device_driver *drv)
10491da177e4SLinus Torvalds {
10501da177e4SLinus Torvalds 	drv->bus = &scsi_bus_type;
10511da177e4SLinus Torvalds 
10521da177e4SLinus Torvalds 	return driver_register(drv);
10531da177e4SLinus Torvalds }
10541da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_register_driver);
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds int scsi_register_interface(struct class_interface *intf)
10571da177e4SLinus Torvalds {
10581da177e4SLinus Torvalds 	intf->class = &sdev_class;
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds 	return class_interface_register(intf);
10611da177e4SLinus Torvalds }
10621da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_register_interface);
10631da177e4SLinus Torvalds 
10641da177e4SLinus Torvalds /**
10651da177e4SLinus Torvalds  * scsi_sysfs_add_host - add scsi host to subsystem
10661da177e4SLinus Torvalds  * @shost:     scsi host struct to add to subsystem
10671da177e4SLinus Torvalds  **/
10681da177e4SLinus Torvalds int scsi_sysfs_add_host(struct Scsi_Host *shost)
10691da177e4SLinus Torvalds {
10701da177e4SLinus Torvalds 	int error, i;
10711da177e4SLinus Torvalds 
1072f7120a4fSHannes Reinecke 	/* add host specific attributes */
10731da177e4SLinus Torvalds 	if (shost->hostt->shost_attrs) {
10741da177e4SLinus Torvalds 		for (i = 0; shost->hostt->shost_attrs[i]; i++) {
1075ee959b00STony Jones 			error = device_create_file(&shost->shost_dev,
1076f7120a4fSHannes Reinecke 					shost->hostt->shost_attrs[i]);
10771da177e4SLinus Torvalds 			if (error)
10781da177e4SLinus Torvalds 				return error;
10791da177e4SLinus Torvalds 		}
10801da177e4SLinus Torvalds 	}
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds 	transport_register_device(&shost->shost_gendev);
1083d52b3815SJames Bottomley 	transport_configure_device(&shost->shost_gendev);
10841da177e4SLinus Torvalds 	return 0;
10851da177e4SLinus Torvalds }
10861da177e4SLinus Torvalds 
1087bfd12944SKay Sievers static struct device_type scsi_dev_type = {
1088bfd12944SKay Sievers 	.name =		"scsi_device",
1089bfd12944SKay Sievers 	.release =	scsi_device_dev_release,
1090bfd12944SKay Sievers 	.groups =	scsi_sdev_attr_groups,
1091bfd12944SKay Sievers };
1092bfd12944SKay Sievers 
10931da177e4SLinus Torvalds void scsi_sysfs_device_initialize(struct scsi_device *sdev)
10941da177e4SLinus Torvalds {
10951da177e4SLinus Torvalds 	unsigned long flags;
10961da177e4SLinus Torvalds 	struct Scsi_Host *shost = sdev->host;
10971da177e4SLinus Torvalds 	struct scsi_target  *starget = sdev->sdev_target;
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds 	device_initialize(&sdev->sdev_gendev);
11001da177e4SLinus Torvalds 	sdev->sdev_gendev.bus = &scsi_bus_type;
1101bfd12944SKay Sievers 	sdev->sdev_gendev.type = &scsi_dev_type;
110271610f55SKay Sievers 	dev_set_name(&sdev->sdev_gendev, "%d:%d:%d:%d",
110371610f55SKay Sievers 		     sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
11041da177e4SLinus Torvalds 
1105ee959b00STony Jones 	device_initialize(&sdev->sdev_dev);
110637e6ba00SJames Bottomley 	sdev->sdev_dev.parent = get_device(&sdev->sdev_gendev);
1107ee959b00STony Jones 	sdev->sdev_dev.class = &sdev_class;
110871610f55SKay Sievers 	dev_set_name(&sdev->sdev_dev, "%d:%d:%d:%d",
110971610f55SKay Sievers 		     sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
11107c9d6f16SAlan Stern 	sdev->scsi_level = starget->scsi_level;
11111da177e4SLinus Torvalds 	transport_setup_device(&sdev->sdev_gendev);
11121da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
11131da177e4SLinus Torvalds 	list_add_tail(&sdev->same_target_siblings, &starget->devices);
11141da177e4SLinus Torvalds 	list_add_tail(&sdev->siblings, &shost->__devices);
11151da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
11161da177e4SLinus Torvalds }
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds int scsi_is_sdev_device(const struct device *dev)
11191da177e4SLinus Torvalds {
112013ba9bcbSKay Sievers 	return dev->type == &scsi_dev_type;
11211da177e4SLinus Torvalds }
11221da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_is_sdev_device);
11231da177e4SLinus Torvalds 
11241da177e4SLinus Torvalds /* A blank transport template that is used in drivers that don't
11251da177e4SLinus Torvalds  * yet implement Transport Attributes */
11261da177e4SLinus Torvalds struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };
1127