xref: /openbmc/linux/drivers/scsi/scsi_sysfs.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * scsi_sysfs.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * SCSI sysfs interface routines.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Created to pull SCSI mid layer sysfs routines into one file.
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #include <linux/module.h>
115a0e3ad6STejun Heo #include <linux/slab.h>
121da177e4SLinus Torvalds #include <linux/init.h>
131da177e4SLinus Torvalds #include <linux/blkdev.h>
141da177e4SLinus Torvalds #include <linux/device.h>
15bc4f2401SAlan Stern #include <linux/pm_runtime.h>
16ead09dd3SChristoph Hellwig #include <linux/bsg.h>
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #include <scsi/scsi.h>
191da177e4SLinus Torvalds #include <scsi/scsi_device.h>
201da177e4SLinus Torvalds #include <scsi/scsi_host.h>
211da177e4SLinus Torvalds #include <scsi/scsi_tcq.h>
2241f95dd2SHannes Reinecke #include <scsi/scsi_dh.h>
231da177e4SLinus Torvalds #include <scsi/scsi_transport.h>
2444818efbSAdrian Bunk #include <scsi/scsi_driver.h>
25345e2960SHannes Reinecke #include <scsi/scsi_devinfo.h>
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #include "scsi_priv.h"
281da177e4SLinus Torvalds #include "scsi_logging.h"
291da177e4SLinus Torvalds 
30b0ed4336SHannes Reinecke static struct device_type scsi_dev_type;
31b0ed4336SHannes Reinecke 
320ad78200SArjan van de Ven static const struct {
331da177e4SLinus Torvalds 	enum scsi_device_state	value;
341da177e4SLinus Torvalds 	char			*name;
351da177e4SLinus Torvalds } sdev_states[] = {
361da177e4SLinus Torvalds 	{ SDEV_CREATED, "created" },
371da177e4SLinus Torvalds 	{ SDEV_RUNNING, "running" },
381da177e4SLinus Torvalds 	{ SDEV_CANCEL, "cancel" },
391da177e4SLinus Torvalds 	{ SDEV_DEL, "deleted" },
401da177e4SLinus Torvalds 	{ SDEV_QUIESCE, "quiesce" },
411da177e4SLinus Torvalds 	{ SDEV_OFFLINE,	"offline" },
421b8d2620SMike Christie 	{ SDEV_TRANSPORT_OFFLINE, "transport-offline" },
431da177e4SLinus Torvalds 	{ SDEV_BLOCK,	"blocked" },
446f4267e3SJames Bottomley 	{ SDEV_CREATED_BLOCK, "created-blocked" },
451da177e4SLinus Torvalds };
461da177e4SLinus Torvalds 
scsi_device_state_name(enum scsi_device_state state)471da177e4SLinus Torvalds const char *scsi_device_state_name(enum scsi_device_state state)
481da177e4SLinus Torvalds {
491da177e4SLinus Torvalds 	int i;
501da177e4SLinus Torvalds 	char *name = NULL;
511da177e4SLinus Torvalds 
526391a113STobias Klauser 	for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
531da177e4SLinus Torvalds 		if (sdev_states[i].value == state) {
541da177e4SLinus Torvalds 			name = sdev_states[i].name;
551da177e4SLinus Torvalds 			break;
561da177e4SLinus Torvalds 		}
571da177e4SLinus Torvalds 	}
581da177e4SLinus Torvalds 	return name;
591da177e4SLinus Torvalds }
601da177e4SLinus Torvalds 
610ad78200SArjan van de Ven static const struct {
62d3301874SMike Anderson 	enum scsi_host_state	value;
63d3301874SMike Anderson 	char			*name;
64d3301874SMike Anderson } shost_states[] = {
65d3301874SMike Anderson 	{ SHOST_CREATED, "created" },
66d3301874SMike Anderson 	{ SHOST_RUNNING, "running" },
67d3301874SMike Anderson 	{ SHOST_CANCEL, "cancel" },
68d3301874SMike Anderson 	{ SHOST_DEL, "deleted" },
69d3301874SMike Anderson 	{ SHOST_RECOVERY, "recovery" },
70939647eeSJames Bottomley 	{ SHOST_CANCEL_RECOVERY, "cancel/recovery" },
71939647eeSJames Bottomley 	{ SHOST_DEL_RECOVERY, "deleted/recovery", },
72d3301874SMike Anderson };
scsi_host_state_name(enum scsi_host_state state)73d3301874SMike Anderson const char *scsi_host_state_name(enum scsi_host_state state)
74d3301874SMike Anderson {
75d3301874SMike Anderson 	int i;
76d3301874SMike Anderson 	char *name = NULL;
77d3301874SMike Anderson 
786391a113STobias Klauser 	for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
79d3301874SMike Anderson 		if (shost_states[i].value == state) {
80d3301874SMike Anderson 			name = shost_states[i].name;
81d3301874SMike Anderson 			break;
82d3301874SMike Anderson 		}
83d3301874SMike Anderson 	}
84d3301874SMike Anderson 	return name;
85d3301874SMike Anderson }
86d3301874SMike Anderson 
87d78540daSBart Van Assche #ifdef CONFIG_SCSI_DH
8877c9df96SHannes Reinecke static const struct {
8977c9df96SHannes Reinecke 	unsigned char	value;
9077c9df96SHannes Reinecke 	char		*name;
9177c9df96SHannes Reinecke } sdev_access_states[] = {
9277c9df96SHannes Reinecke 	{ SCSI_ACCESS_STATE_OPTIMAL, "active/optimized" },
9377c9df96SHannes Reinecke 	{ SCSI_ACCESS_STATE_ACTIVE, "active/non-optimized" },
9477c9df96SHannes Reinecke 	{ SCSI_ACCESS_STATE_STANDBY, "standby" },
9577c9df96SHannes Reinecke 	{ SCSI_ACCESS_STATE_UNAVAILABLE, "unavailable" },
9677c9df96SHannes Reinecke 	{ SCSI_ACCESS_STATE_LBA, "lba-dependent" },
9777c9df96SHannes Reinecke 	{ SCSI_ACCESS_STATE_OFFLINE, "offline" },
9877c9df96SHannes Reinecke 	{ SCSI_ACCESS_STATE_TRANSITIONING, "transitioning" },
9977c9df96SHannes Reinecke };
10077c9df96SHannes Reinecke 
scsi_access_state_name(unsigned char state)101d78540daSBart Van Assche static const char *scsi_access_state_name(unsigned char state)
10277c9df96SHannes Reinecke {
10377c9df96SHannes Reinecke 	int i;
10477c9df96SHannes Reinecke 	char *name = NULL;
10577c9df96SHannes Reinecke 
10677c9df96SHannes Reinecke 	for (i = 0; i < ARRAY_SIZE(sdev_access_states); i++) {
10777c9df96SHannes Reinecke 		if (sdev_access_states[i].value == state) {
10877c9df96SHannes Reinecke 			name = sdev_access_states[i].name;
10977c9df96SHannes Reinecke 			break;
11077c9df96SHannes Reinecke 		}
11177c9df96SHannes Reinecke 	}
11277c9df96SHannes Reinecke 	return name;
11377c9df96SHannes Reinecke }
114d78540daSBart Van Assche #endif
11577c9df96SHannes Reinecke 
check_set(unsigned long long * val,char * src)1169cb78c16SHannes Reinecke static int check_set(unsigned long long *val, char *src)
1171da177e4SLinus Torvalds {
1181da177e4SLinus Torvalds 	char *last;
1191da177e4SLinus Torvalds 
1203991e460SBart Van Assche 	if (strcmp(src, "-") == 0) {
1211da177e4SLinus Torvalds 		*val = SCAN_WILD_CARD;
1221da177e4SLinus Torvalds 	} else {
1231da177e4SLinus Torvalds 		/*
1241da177e4SLinus Torvalds 		 * Doesn't check for int overflow
1251da177e4SLinus Torvalds 		 */
1269cb78c16SHannes Reinecke 		*val = simple_strtoull(src, &last, 0);
1271da177e4SLinus Torvalds 		if (*last != '\0')
1281da177e4SLinus Torvalds 			return 1;
1291da177e4SLinus Torvalds 	}
1301da177e4SLinus Torvalds 	return 0;
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
scsi_scan(struct Scsi_Host * shost,const char * str)1331da177e4SLinus Torvalds static int scsi_scan(struct Scsi_Host *shost, const char *str)
1341da177e4SLinus Torvalds {
1359cb78c16SHannes Reinecke 	char s1[15], s2[15], s3[17], junk;
1369cb78c16SHannes Reinecke 	unsigned long long channel, id, lun;
1371da177e4SLinus Torvalds 	int res;
1381da177e4SLinus Torvalds 
1399cb78c16SHannes Reinecke 	res = sscanf(str, "%10s %10s %16s %c", s1, s2, s3, &junk);
1401da177e4SLinus Torvalds 	if (res != 3)
1411da177e4SLinus Torvalds 		return -EINVAL;
1421da177e4SLinus Torvalds 	if (check_set(&channel, s1))
1431da177e4SLinus Torvalds 		return -EINVAL;
1441da177e4SLinus Torvalds 	if (check_set(&id, s2))
1451da177e4SLinus Torvalds 		return -EINVAL;
1461da177e4SLinus Torvalds 	if (check_set(&lun, s3))
1471da177e4SLinus Torvalds 		return -EINVAL;
148e02f3f59SChristoph Hellwig 	if (shost->transportt->user_scan)
149e02f3f59SChristoph Hellwig 		res = shost->transportt->user_scan(shost, channel, id, lun);
150e02f3f59SChristoph Hellwig 	else
1511d645088SHannes Reinecke 		res = scsi_scan_host_selected(shost, channel, id, lun,
1521d645088SHannes Reinecke 					      SCSI_SCAN_MANUAL);
1531da177e4SLinus Torvalds 	return res;
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds /*
1571da177e4SLinus Torvalds  * shost_show_function: macro to create an attr function that can be used to
1581da177e4SLinus Torvalds  * show a non-bit field.
1591da177e4SLinus Torvalds  */
1601da177e4SLinus Torvalds #define shost_show_function(name, field, format_string)			\
1611da177e4SLinus Torvalds static ssize_t								\
162ee959b00STony Jones show_##name (struct device *dev, struct device_attribute *attr, 	\
163ee959b00STony Jones 	     char *buf)							\
1641da177e4SLinus Torvalds {									\
165ee959b00STony Jones 	struct Scsi_Host *shost = class_to_shost(dev);			\
1661da177e4SLinus Torvalds 	return snprintf (buf, 20, format_string, shost->field);		\
1671da177e4SLinus Torvalds }
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds /*
1701da177e4SLinus Torvalds  * shost_rd_attr: macro to create a function and attribute variable for a
1711da177e4SLinus Torvalds  * read only field.
1721da177e4SLinus Torvalds  */
1731da177e4SLinus Torvalds #define shost_rd_attr2(name, field, format_string)			\
1741da177e4SLinus Torvalds 	shost_show_function(name, field, format_string)			\
175ee959b00STony Jones static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds #define shost_rd_attr(field, format_string) \
1781da177e4SLinus Torvalds shost_rd_attr2(field, field, format_string)
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds /*
1811da177e4SLinus Torvalds  * Create the actual show/store functions and data structures.
1821da177e4SLinus Torvalds  */
1831da177e4SLinus Torvalds 
184ee959b00STony Jones static ssize_t
store_scan(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)185ee959b00STony Jones store_scan(struct device *dev, struct device_attribute *attr,
186ee959b00STony Jones 	   const char *buf, size_t count)
1871da177e4SLinus Torvalds {
188ee959b00STony Jones 	struct Scsi_Host *shost = class_to_shost(dev);
1891da177e4SLinus Torvalds 	int res;
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 	res = scsi_scan(shost, buf);
1921da177e4SLinus Torvalds 	if (res == 0)
1931da177e4SLinus Torvalds 		res = count;
1941da177e4SLinus Torvalds 	return res;
1951da177e4SLinus Torvalds };
196ee959b00STony Jones static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
1971da177e4SLinus Torvalds 
198d3301874SMike Anderson static ssize_t
store_shost_state(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)199ee959b00STony Jones store_shost_state(struct device *dev, struct device_attribute *attr,
200ee959b00STony Jones 		  const char *buf, size_t count)
201d3301874SMike Anderson {
202d3301874SMike Anderson 	int i;
203ee959b00STony Jones 	struct Scsi_Host *shost = class_to_shost(dev);
204d3301874SMike Anderson 	enum scsi_host_state state = 0;
205d3301874SMike Anderson 
2066391a113STobias Klauser 	for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
207d3301874SMike Anderson 		const int len = strlen(shost_states[i].name);
208d3301874SMike Anderson 		if (strncmp(shost_states[i].name, buf, len) == 0 &&
209d3301874SMike Anderson 		   buf[len] == '\n') {
210d3301874SMike Anderson 			state = shost_states[i].value;
211d3301874SMike Anderson 			break;
212d3301874SMike Anderson 		}
213d3301874SMike Anderson 	}
214d3301874SMike Anderson 	if (!state)
215d3301874SMike Anderson 		return -EINVAL;
216d3301874SMike Anderson 
217d3301874SMike Anderson 	if (scsi_host_set_state(shost, state))
218d3301874SMike Anderson 		return -EINVAL;
219d3301874SMike Anderson 	return count;
220d3301874SMike Anderson }
221d3301874SMike Anderson 
222d3301874SMike Anderson static ssize_t
show_shost_state(struct device * dev,struct device_attribute * attr,char * buf)223ee959b00STony Jones show_shost_state(struct device *dev, struct device_attribute *attr, char *buf)
224d3301874SMike Anderson {
225ee959b00STony Jones 	struct Scsi_Host *shost = class_to_shost(dev);
226d3301874SMike Anderson 	const char *name = scsi_host_state_name(shost->shost_state);
227d3301874SMike Anderson 
228d3301874SMike Anderson 	if (!name)
229d3301874SMike Anderson 		return -EINVAL;
230d3301874SMike Anderson 
231d3301874SMike Anderson 	return snprintf(buf, 20, "%s\n", name);
232d3301874SMike Anderson }
233d3301874SMike Anderson 
234ee959b00STony Jones /* DEVICE_ATTR(state) clashes with dev_attr_state for sdev */
235d78540daSBart Van Assche static struct device_attribute dev_attr_hstate =
236ee959b00STony Jones 	__ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
237d3301874SMike Anderson 
2385dc2b89eSFUJITA Tomonori static ssize_t
show_shost_mode(unsigned int mode,char * buf)2395dc2b89eSFUJITA Tomonori show_shost_mode(unsigned int mode, char *buf)
2405dc2b89eSFUJITA Tomonori {
2415dc2b89eSFUJITA Tomonori 	ssize_t len = 0;
2425dc2b89eSFUJITA Tomonori 
2435dc2b89eSFUJITA Tomonori 	if (mode & MODE_INITIATOR)
2445dc2b89eSFUJITA Tomonori 		len = sprintf(buf, "%s", "Initiator");
2455dc2b89eSFUJITA Tomonori 
2465dc2b89eSFUJITA Tomonori 	if (mode & MODE_TARGET)
2475dc2b89eSFUJITA Tomonori 		len += sprintf(buf + len, "%s%s", len ? ", " : "", "Target");
2485dc2b89eSFUJITA Tomonori 
2495dc2b89eSFUJITA Tomonori 	len += sprintf(buf + len, "\n");
2505dc2b89eSFUJITA Tomonori 
2515dc2b89eSFUJITA Tomonori 	return len;
2525dc2b89eSFUJITA Tomonori }
2535dc2b89eSFUJITA Tomonori 
254ee959b00STony Jones static ssize_t
show_shost_supported_mode(struct device * dev,struct device_attribute * attr,char * buf)255ee959b00STony Jones show_shost_supported_mode(struct device *dev, struct device_attribute *attr,
256ee959b00STony Jones 			  char *buf)
2575dc2b89eSFUJITA Tomonori {
258ee959b00STony Jones 	struct Scsi_Host *shost = class_to_shost(dev);
2597a39ac3fSJames Bottomley 	unsigned int supported_mode = shost->hostt->supported_mode;
2605dc2b89eSFUJITA Tomonori 
2617a39ac3fSJames Bottomley 	if (supported_mode == MODE_UNKNOWN)
2627a39ac3fSJames Bottomley 		/* by default this should be initiator */
2637a39ac3fSJames Bottomley 		supported_mode = MODE_INITIATOR;
2647a39ac3fSJames Bottomley 
2657a39ac3fSJames Bottomley 	return show_shost_mode(supported_mode, buf);
2665dc2b89eSFUJITA Tomonori }
2675dc2b89eSFUJITA Tomonori 
268ee959b00STony Jones static DEVICE_ATTR(supported_mode, S_IRUGO | S_IWUSR, show_shost_supported_mode, NULL);
2695dc2b89eSFUJITA Tomonori 
270ee959b00STony Jones static ssize_t
show_shost_active_mode(struct device * dev,struct device_attribute * attr,char * buf)271ee959b00STony Jones show_shost_active_mode(struct device *dev,
272ee959b00STony Jones 		       struct device_attribute *attr, char *buf)
2735dc2b89eSFUJITA Tomonori {
274ee959b00STony Jones 	struct Scsi_Host *shost = class_to_shost(dev);
2755dc2b89eSFUJITA Tomonori 
2765dc2b89eSFUJITA Tomonori 	if (shost->active_mode == MODE_UNKNOWN)
2775dc2b89eSFUJITA Tomonori 		return snprintf(buf, 20, "unknown\n");
2785dc2b89eSFUJITA Tomonori 	else
2795dc2b89eSFUJITA Tomonori 		return show_shost_mode(shost->active_mode, buf);
2805dc2b89eSFUJITA Tomonori }
2815dc2b89eSFUJITA Tomonori 
282ee959b00STony Jones static DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL);
2835dc2b89eSFUJITA Tomonori 
check_reset_type(const char * str)284072f19b4SSasha Levin static int check_reset_type(const char *str)
28529443691SVikas Chaudhary {
286072f19b4SSasha Levin 	if (sysfs_streq(str, "adapter"))
28729443691SVikas Chaudhary 		return SCSI_ADAPTER_RESET;
288072f19b4SSasha Levin 	else if (sysfs_streq(str, "firmware"))
28929443691SVikas Chaudhary 		return SCSI_FIRMWARE_RESET;
29029443691SVikas Chaudhary 	else
29129443691SVikas Chaudhary 		return 0;
29229443691SVikas Chaudhary }
29329443691SVikas Chaudhary 
29429443691SVikas Chaudhary static ssize_t
store_host_reset(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)29529443691SVikas Chaudhary store_host_reset(struct device *dev, struct device_attribute *attr,
29629443691SVikas Chaudhary 		const char *buf, size_t count)
29729443691SVikas Chaudhary {
29829443691SVikas Chaudhary 	struct Scsi_Host *shost = class_to_shost(dev);
29931435de9SBart Van Assche 	const struct scsi_host_template *sht = shost->hostt;
30029443691SVikas Chaudhary 	int ret = -EINVAL;
30129443691SVikas Chaudhary 	int type;
30229443691SVikas Chaudhary 
303072f19b4SSasha Levin 	type = check_reset_type(buf);
30429443691SVikas Chaudhary 	if (!type)
30529443691SVikas Chaudhary 		goto exit_store_host_reset;
30629443691SVikas Chaudhary 
30729443691SVikas Chaudhary 	if (sht->host_reset)
30829443691SVikas Chaudhary 		ret = sht->host_reset(shost, type);
30992227b8dSweiping zhang 	else
31092227b8dSweiping zhang 		ret = -EOPNOTSUPP;
31129443691SVikas Chaudhary 
31229443691SVikas Chaudhary exit_store_host_reset:
31329443691SVikas Chaudhary 	if (ret == 0)
31429443691SVikas Chaudhary 		ret = count;
31529443691SVikas Chaudhary 	return ret;
31629443691SVikas Chaudhary }
31729443691SVikas Chaudhary 
31829443691SVikas Chaudhary static DEVICE_ATTR(host_reset, S_IWUSR, NULL, store_host_reset);
31929443691SVikas Chaudhary 
320b4562022SHannes Reinecke static ssize_t
show_shost_eh_deadline(struct device * dev,struct device_attribute * attr,char * buf)321b4562022SHannes Reinecke show_shost_eh_deadline(struct device *dev,
322b4562022SHannes Reinecke 		      struct device_attribute *attr, char *buf)
323b4562022SHannes Reinecke {
324b4562022SHannes Reinecke 	struct Scsi_Host *shost = class_to_shost(dev);
325b4562022SHannes Reinecke 
326bb3b621aSRen Mingxin 	if (shost->eh_deadline == -1)
327bb3b621aSRen Mingxin 		return snprintf(buf, strlen("off") + 2, "off\n");
328bb3b621aSRen Mingxin 	return sprintf(buf, "%u\n", shost->eh_deadline / HZ);
329b4562022SHannes Reinecke }
330b4562022SHannes Reinecke 
331b4562022SHannes Reinecke static ssize_t
store_shost_eh_deadline(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)332b4562022SHannes Reinecke store_shost_eh_deadline(struct device *dev, struct device_attribute *attr,
333b4562022SHannes Reinecke 		const char *buf, size_t count)
334b4562022SHannes Reinecke {
335b4562022SHannes Reinecke 	struct Scsi_Host *shost = class_to_shost(dev);
336b4562022SHannes Reinecke 	int ret = -EINVAL;
337bb3b621aSRen Mingxin 	unsigned long deadline, flags;
338b4562022SHannes Reinecke 
339ad469a57SHannes Reinecke 	if (shost->transportt &&
340ad469a57SHannes Reinecke 	    (shost->transportt->eh_strategy_handler ||
341ad469a57SHannes Reinecke 	     !shost->hostt->eh_host_reset_handler))
342b4562022SHannes Reinecke 		return ret;
343b4562022SHannes Reinecke 
344bb3b621aSRen Mingxin 	if (!strncmp(buf, "off", strlen("off")))
345bb3b621aSRen Mingxin 		deadline = -1;
346bb3b621aSRen Mingxin 	else {
347bb3b621aSRen Mingxin 		ret = kstrtoul(buf, 10, &deadline);
348bb3b621aSRen Mingxin 		if (ret)
349bb3b621aSRen Mingxin 			return ret;
350bb3b621aSRen Mingxin 		if (deadline * HZ > UINT_MAX)
351bb3b621aSRen Mingxin 			return -EINVAL;
352bb3b621aSRen Mingxin 	}
353bb3b621aSRen Mingxin 
354b4562022SHannes Reinecke 	spin_lock_irqsave(shost->host_lock, flags);
355b4562022SHannes Reinecke 	if (scsi_host_in_recovery(shost))
356b4562022SHannes Reinecke 		ret = -EBUSY;
357b4562022SHannes Reinecke 	else {
358bb3b621aSRen Mingxin 		if (deadline == -1)
359bb3b621aSRen Mingxin 			shost->eh_deadline = -1;
360bb3b621aSRen Mingxin 		else
361b4562022SHannes Reinecke 			shost->eh_deadline = deadline * HZ;
362bb3b621aSRen Mingxin 
363b4562022SHannes Reinecke 		ret = count;
364b4562022SHannes Reinecke 	}
365b4562022SHannes Reinecke 	spin_unlock_irqrestore(shost->host_lock, flags);
366bb3b621aSRen Mingxin 
367b4562022SHannes Reinecke 	return ret;
368b4562022SHannes Reinecke }
369b4562022SHannes Reinecke 
370b4562022SHannes Reinecke static DEVICE_ATTR(eh_deadline, S_IRUGO | S_IWUSR, show_shost_eh_deadline, store_shost_eh_deadline);
371b4562022SHannes Reinecke 
3721da177e4SLinus Torvalds shost_rd_attr(unique_id, "%u\n");
3731da177e4SLinus Torvalds shost_rd_attr(cmd_per_lun, "%hd\n");
37488392820SNick Desaulniers shost_rd_attr(can_queue, "%d\n");
3751da177e4SLinus Torvalds shost_rd_attr(sg_tablesize, "%hu\n");
37613f05c8dSMartin K. Petersen shost_rd_attr(sg_prot_tablesize, "%hu\n");
3774469f987SMartin K. Petersen shost_rd_attr(prot_capabilities, "%u\n");
3784469f987SMartin K. Petersen shost_rd_attr(prot_guard_type, "%hd\n");
3791da177e4SLinus Torvalds shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
3801da177e4SLinus Torvalds 
38174665016SChristoph Hellwig static ssize_t
show_host_busy(struct device * dev,struct device_attribute * attr,char * buf)38274665016SChristoph Hellwig show_host_busy(struct device *dev, struct device_attribute *attr, char *buf)
38374665016SChristoph Hellwig {
38474665016SChristoph Hellwig 	struct Scsi_Host *shost = class_to_shost(dev);
385c84b023aSMing Lei 	return snprintf(buf, 20, "%d\n", scsi_host_busy(shost));
38674665016SChristoph Hellwig }
38774665016SChristoph Hellwig static DEVICE_ATTR(host_busy, S_IRUGO, show_host_busy, NULL);
38874665016SChristoph Hellwig 
389f664a3ccSJens Axboe static ssize_t
show_use_blk_mq(struct device * dev,struct device_attribute * attr,char * buf)390f664a3ccSJens Axboe show_use_blk_mq(struct device *dev, struct device_attribute *attr, char *buf)
391f664a3ccSJens Axboe {
392f664a3ccSJens Axboe 	return sprintf(buf, "1\n");
393f664a3ccSJens Axboe }
394f664a3ccSJens Axboe static DEVICE_ATTR(use_blk_mq, S_IRUGO, show_use_blk_mq, NULL);
395f664a3ccSJens Axboe 
39664f1501bSJohn Garry static ssize_t
show_nr_hw_queues(struct device * dev,struct device_attribute * attr,char * buf)39764f1501bSJohn Garry show_nr_hw_queues(struct device *dev, struct device_attribute *attr, char *buf)
39864f1501bSJohn Garry {
39964f1501bSJohn Garry 	struct Scsi_Host *shost = class_to_shost(dev);
40064f1501bSJohn Garry 	struct blk_mq_tag_set *tag_set = &shost->tag_set;
40164f1501bSJohn Garry 
40264f1501bSJohn Garry 	return snprintf(buf, 20, "%d\n", tag_set->nr_hw_queues);
40364f1501bSJohn Garry }
40464f1501bSJohn Garry static DEVICE_ATTR(nr_hw_queues, S_IRUGO, show_nr_hw_queues, NULL);
40564f1501bSJohn Garry 
406f7120a4fSHannes Reinecke static struct attribute *scsi_sysfs_shost_attrs[] = {
407d285203cSChristoph Hellwig 	&dev_attr_use_blk_mq.attr,
408f7120a4fSHannes Reinecke 	&dev_attr_unique_id.attr,
409f7120a4fSHannes Reinecke 	&dev_attr_host_busy.attr,
410f7120a4fSHannes Reinecke 	&dev_attr_cmd_per_lun.attr,
411f7120a4fSHannes Reinecke 	&dev_attr_can_queue.attr,
412f7120a4fSHannes Reinecke 	&dev_attr_sg_tablesize.attr,
41313f05c8dSMartin K. Petersen 	&dev_attr_sg_prot_tablesize.attr,
414f7120a4fSHannes Reinecke 	&dev_attr_proc_name.attr,
415f7120a4fSHannes Reinecke 	&dev_attr_scan.attr,
416f7120a4fSHannes Reinecke 	&dev_attr_hstate.attr,
417f7120a4fSHannes Reinecke 	&dev_attr_supported_mode.attr,
418f7120a4fSHannes Reinecke 	&dev_attr_active_mode.attr,
4194469f987SMartin K. Petersen 	&dev_attr_prot_capabilities.attr,
4204469f987SMartin K. Petersen 	&dev_attr_prot_guard_type.attr,
42129443691SVikas Chaudhary 	&dev_attr_host_reset.attr,
422b4562022SHannes Reinecke 	&dev_attr_eh_deadline.attr,
42364f1501bSJohn Garry 	&dev_attr_nr_hw_queues.attr,
424f7120a4fSHannes Reinecke 	NULL
425f7120a4fSHannes Reinecke };
426f7120a4fSHannes Reinecke 
4270a84486dSBart Van Assche static const struct attribute_group scsi_shost_attr_group = {
428f7120a4fSHannes Reinecke 	.attrs =	scsi_sysfs_shost_attrs,
429f7120a4fSHannes Reinecke };
430f7120a4fSHannes Reinecke 
4310a84486dSBart Van Assche const struct attribute_group *scsi_shost_groups[] = {
4320a84486dSBart Van Assche 	&scsi_shost_attr_group,
4330a84486dSBart Van Assche 	NULL
4340a84486dSBart Van Assche };
4350a84486dSBart Van Assche 
scsi_device_cls_release(struct device * class_dev)436ee959b00STony Jones static void scsi_device_cls_release(struct device *class_dev)
4371da177e4SLinus Torvalds {
4381da177e4SLinus Torvalds 	struct scsi_device *sdev;
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	sdev = class_to_sdev(class_dev);
4411da177e4SLinus Torvalds 	put_device(&sdev->sdev_gendev);
4421da177e4SLinus Torvalds }
4431da177e4SLinus Torvalds 
scsi_device_dev_release(struct device * dev)444f93ed747SBart Van Assche static void scsi_device_dev_release(struct device *dev)
4451da177e4SLinus Torvalds {
446f93ed747SBart Van Assche 	struct scsi_device *sdev = to_scsi_device(dev);
4471da177e4SLinus Torvalds 	struct device *parent;
448a341cd0fSJeff Garzik 	struct list_head *this, *tmp;
449ccf1e004SBart Van Assche 	struct scsi_vpd *vpd_pg80 = NULL, *vpd_pg83 = NULL;
450d188b067SRyan Attard 	struct scsi_vpd *vpd_pg0 = NULL, *vpd_pg89 = NULL;
451e60ac0b9SMartin K. Petersen 	struct scsi_vpd *vpd_pgb0 = NULL, *vpd_pgb1 = NULL, *vpd_pgb2 = NULL;
4521da177e4SLinus Torvalds 	unsigned long flags;
453f2b85040SMing Lei 
4542542fc95SBart Van Assche 	might_sleep();
4552542fc95SBart Van Assche 
45623695e41SJunichi Nomura 	scsi_dh_release_device(sdev);
45723695e41SJunichi Nomura 
45865f27f38SDavid Howells 	parent = sdev->sdev_gendev.parent;
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	spin_lock_irqsave(sdev->host->host_lock, flags);
4611da177e4SLinus Torvalds 	list_del(&sdev->siblings);
4621da177e4SLinus Torvalds 	list_del(&sdev->same_target_siblings);
4631da177e4SLinus Torvalds 	list_del(&sdev->starved_entry);
4641da177e4SLinus Torvalds 	spin_unlock_irqrestore(sdev->host->host_lock, flags);
4651da177e4SLinus Torvalds 
466a341cd0fSJeff Garzik 	cancel_work_sync(&sdev->event_work);
467a341cd0fSJeff Garzik 
468a341cd0fSJeff Garzik 	list_for_each_safe(this, tmp, &sdev->event_list) {
469a341cd0fSJeff Garzik 		struct scsi_event *evt;
470a341cd0fSJeff Garzik 
471a341cd0fSJeff Garzik 		evt = list_entry(this, struct scsi_event, node);
472a341cd0fSJeff Garzik 		list_del(&evt->node);
473a341cd0fSJeff Garzik 		kfree(evt);
474a341cd0fSJeff Garzik 	}
475a341cd0fSJeff Garzik 
476e73e079bSJames Bottomley 	blk_put_queue(sdev->request_queue);
47786cbfb56SJames Bottomley 	/* NULL queue means the device can't be used */
478c2a9331cS 	sdev->request_queue = NULL;
4791da177e4SLinus Torvalds 
480020b0f0aSMing Lei 	sbitmap_free(&sdev->budget_map);
481020b0f0aSMing Lei 
482ccf1e004SBart Van Assche 	mutex_lock(&sdev->inquiry_mutex);
483ef2cc88eSLinus Torvalds 	vpd_pg0 = rcu_replace_pointer(sdev->vpd_pg0, vpd_pg0,
484d188b067SRyan Attard 				       lockdep_is_held(&sdev->inquiry_mutex));
485c0eaf15cSPaul E. McKenney 	vpd_pg80 = rcu_replace_pointer(sdev->vpd_pg80, vpd_pg80,
486ccf1e004SBart Van Assche 				       lockdep_is_held(&sdev->inquiry_mutex));
487c0eaf15cSPaul E. McKenney 	vpd_pg83 = rcu_replace_pointer(sdev->vpd_pg83, vpd_pg83,
488ccf1e004SBart Van Assche 				       lockdep_is_held(&sdev->inquiry_mutex));
489ef2cc88eSLinus Torvalds 	vpd_pg89 = rcu_replace_pointer(sdev->vpd_pg89, vpd_pg89,
490d188b067SRyan Attard 				       lockdep_is_held(&sdev->inquiry_mutex));
491e60ac0b9SMartin K. Petersen 	vpd_pgb0 = rcu_replace_pointer(sdev->vpd_pgb0, vpd_pgb0,
492e60ac0b9SMartin K. Petersen 				       lockdep_is_held(&sdev->inquiry_mutex));
493e60ac0b9SMartin K. Petersen 	vpd_pgb1 = rcu_replace_pointer(sdev->vpd_pgb1, vpd_pgb1,
494e60ac0b9SMartin K. Petersen 				       lockdep_is_held(&sdev->inquiry_mutex));
495e60ac0b9SMartin K. Petersen 	vpd_pgb2 = rcu_replace_pointer(sdev->vpd_pgb2, vpd_pgb2,
496e60ac0b9SMartin K. Petersen 				       lockdep_is_held(&sdev->inquiry_mutex));
497ccf1e004SBart Van Assche 	mutex_unlock(&sdev->inquiry_mutex);
498ccf1e004SBart Van Assche 
499d188b067SRyan Attard 	if (vpd_pg0)
500d188b067SRyan Attard 		kfree_rcu(vpd_pg0, rcu);
501ccf1e004SBart Van Assche 	if (vpd_pg83)
502ccf1e004SBart Van Assche 		kfree_rcu(vpd_pg83, rcu);
503ccf1e004SBart Van Assche 	if (vpd_pg80)
504ccf1e004SBart Van Assche 		kfree_rcu(vpd_pg80, rcu);
505d188b067SRyan Attard 	if (vpd_pg89)
506d188b067SRyan Attard 		kfree_rcu(vpd_pg89, rcu);
507e60ac0b9SMartin K. Petersen 	if (vpd_pgb0)
508e60ac0b9SMartin K. Petersen 		kfree_rcu(vpd_pgb0, rcu);
509e60ac0b9SMartin K. Petersen 	if (vpd_pgb1)
510e60ac0b9SMartin K. Petersen 		kfree_rcu(vpd_pgb1, rcu);
511e60ac0b9SMartin K. Petersen 	if (vpd_pgb2)
512e60ac0b9SMartin K. Petersen 		kfree_rcu(vpd_pgb2, rcu);
5131da177e4SLinus Torvalds 	kfree(sdev->inquiry);
5141da177e4SLinus Torvalds 	kfree(sdev);
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	if (parent)
5171da177e4SLinus Torvalds 		put_device(parent);
51865110b21SJames Bottomley }
51965110b21SJames Bottomley 
52052c1da39SAdrian Bunk static struct class sdev_class = {
5211da177e4SLinus Torvalds 	.name		= "scsi_device",
522ee959b00STony Jones 	.dev_release	= scsi_device_cls_release,
5231da177e4SLinus Torvalds };
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds /* all probing is done in the individual ->probe routines */
scsi_bus_match(struct device * dev,struct device_driver * gendrv)5261da177e4SLinus Torvalds static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
5271da177e4SLinus Torvalds {
528b0ed4336SHannes Reinecke 	struct scsi_device *sdp;
529b0ed4336SHannes Reinecke 
530b0ed4336SHannes Reinecke 	if (dev->type != &scsi_dev_type)
531b0ed4336SHannes Reinecke 		return 0;
532b0ed4336SHannes Reinecke 
533b0ed4336SHannes Reinecke 	sdp = to_scsi_device(dev);
5341da177e4SLinus Torvalds 	if (sdp->no_uld_attach)
5351da177e4SLinus Torvalds 		return 0;
5361da177e4SLinus Torvalds 	return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
5371da177e4SLinus Torvalds }
5381da177e4SLinus Torvalds 
scsi_bus_uevent(const struct device * dev,struct kobj_uevent_env * env)5392a81ada3SGreg Kroah-Hartman static int scsi_bus_uevent(const struct device *dev, struct kobj_uevent_env *env)
540d7b8bcb0SMichael Tokarev {
5412a81ada3SGreg Kroah-Hartman 	const struct scsi_device *sdev;
5421f42ea7bSJames Bottomley 
5431f42ea7bSJames Bottomley 	if (dev->type != &scsi_dev_type)
5441f42ea7bSJames Bottomley 		return 0;
5451f42ea7bSJames Bottomley 
5461f42ea7bSJames Bottomley 	sdev = to_scsi_device(dev);
547d7b8bcb0SMichael Tokarev 
5487eff2e7aSKay Sievers 	add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
549d7b8bcb0SMichael Tokarev 	return 0;
550d7b8bcb0SMichael Tokarev }
551d7b8bcb0SMichael Tokarev 
5521da177e4SLinus Torvalds struct bus_type scsi_bus_type = {
5531da177e4SLinus Torvalds         .name		= "scsi",
5541da177e4SLinus Torvalds         .match		= scsi_bus_match,
555d7b8bcb0SMichael Tokarev 	.uevent		= scsi_bus_uevent,
556aa338601SRafael J. Wysocki #ifdef CONFIG_PM
557db5bd1e0SAlan Stern 	.pm		= &scsi_bus_pm_ops,
558e6da54d8SAlan Stern #endif
5591da177e4SLinus Torvalds };
5601da177e4SLinus Torvalds 
scsi_sysfs_register(void)5611da177e4SLinus Torvalds int scsi_sysfs_register(void)
5621da177e4SLinus Torvalds {
5631da177e4SLinus Torvalds 	int error;
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	error = bus_register(&scsi_bus_type);
5661da177e4SLinus Torvalds 	if (!error) {
5671da177e4SLinus Torvalds 		error = class_register(&sdev_class);
5681da177e4SLinus Torvalds 		if (error)
5691da177e4SLinus Torvalds 			bus_unregister(&scsi_bus_type);
5701da177e4SLinus Torvalds 	}
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	return error;
5731da177e4SLinus Torvalds }
5741da177e4SLinus Torvalds 
scsi_sysfs_unregister(void)5751da177e4SLinus Torvalds void scsi_sysfs_unregister(void)
5761da177e4SLinus Torvalds {
5771da177e4SLinus Torvalds 	class_unregister(&sdev_class);
5781da177e4SLinus Torvalds 	bus_unregister(&scsi_bus_type);
5791da177e4SLinus Torvalds }
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds /*
5821da177e4SLinus Torvalds  * sdev_show_function: macro to create an attr function that can be used to
5831da177e4SLinus Torvalds  * show a non-bit field.
5841da177e4SLinus Torvalds  */
5851da177e4SLinus Torvalds #define sdev_show_function(field, format_string)				\
5861da177e4SLinus Torvalds static ssize_t								\
587ee959b00STony Jones sdev_show_##field (struct device *dev, struct device_attribute *attr,	\
588ee959b00STony Jones 		   char *buf)						\
5891da177e4SLinus Torvalds {									\
5901da177e4SLinus Torvalds 	struct scsi_device *sdev;					\
5911da177e4SLinus Torvalds 	sdev = to_scsi_device(dev);					\
5921da177e4SLinus Torvalds 	return snprintf (buf, 20, format_string, sdev->field);		\
5931da177e4SLinus Torvalds }									\
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds /*
5961da177e4SLinus Torvalds  * sdev_rd_attr: macro to create a function and attribute variable for a
5971da177e4SLinus Torvalds  * read only field.
5981da177e4SLinus Torvalds  */
5991da177e4SLinus Torvalds #define sdev_rd_attr(field, format_string)				\
6001da177e4SLinus Torvalds 	sdev_show_function(field, format_string)			\
6011da177e4SLinus Torvalds static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds /*
605160e7f67STomohiro Kusumi  * sdev_rw_attr: create a function and attribute variable for a
6061da177e4SLinus Torvalds  * read/write field.
6071da177e4SLinus Torvalds  */
6081da177e4SLinus Torvalds #define sdev_rw_attr(field, format_string)				\
6091da177e4SLinus Torvalds 	sdev_show_function(field, format_string)				\
6101da177e4SLinus Torvalds 									\
6111da177e4SLinus Torvalds static ssize_t								\
612ee959b00STony Jones sdev_store_##field (struct device *dev, struct device_attribute *attr,	\
613ee959b00STony Jones 		    const char *buf, size_t count)			\
6141da177e4SLinus Torvalds {									\
6151da177e4SLinus Torvalds 	struct scsi_device *sdev;					\
6161da177e4SLinus Torvalds 	sdev = to_scsi_device(dev);					\
617160e7f67STomohiro Kusumi 	sscanf (buf, format_string, &sdev->field);			\
6181da177e4SLinus Torvalds 	return count;							\
6191da177e4SLinus Torvalds }									\
6201da177e4SLinus Torvalds static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds /* Currently we don't export bit fields, but we might in future,
6231da177e4SLinus Torvalds  * so leave this code in */
6241da177e4SLinus Torvalds #if 0
6251da177e4SLinus Torvalds /*
6261da177e4SLinus Torvalds  * sdev_rd_attr: create a function and attribute variable for a
6271da177e4SLinus Torvalds  * read/write bit field.
6281da177e4SLinus Torvalds  */
6291da177e4SLinus Torvalds #define sdev_rw_attr_bit(field)						\
6301da177e4SLinus Torvalds 	sdev_show_function(field, "%d\n")					\
6311da177e4SLinus Torvalds 									\
6321da177e4SLinus Torvalds static ssize_t								\
633ee959b00STony Jones sdev_store_##field (struct device *dev, struct device_attribute *attr,	\
634ee959b00STony Jones 		    const char *buf, size_t count)			\
6351da177e4SLinus Torvalds {									\
6361da177e4SLinus Torvalds 	int ret;							\
6371da177e4SLinus Torvalds 	struct scsi_device *sdev;					\
6381da177e4SLinus Torvalds 	ret = scsi_sdev_check_buf_bit(buf);				\
6391da177e4SLinus Torvalds 	if (ret >= 0)	{						\
6401da177e4SLinus Torvalds 		sdev = to_scsi_device(dev);				\
6411da177e4SLinus Torvalds 		sdev->field = ret;					\
6421da177e4SLinus Torvalds 		ret = count;						\
6431da177e4SLinus Torvalds 	}								\
6441da177e4SLinus Torvalds 	return ret;							\
6451da177e4SLinus Torvalds }									\
6461da177e4SLinus Torvalds static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds /*
6491da177e4SLinus Torvalds  * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
6501da177e4SLinus Torvalds  * else return -EINVAL.
6511da177e4SLinus Torvalds  */
6521da177e4SLinus Torvalds static int scsi_sdev_check_buf_bit(const char *buf)
6531da177e4SLinus Torvalds {
6541da177e4SLinus Torvalds 	if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
6551da177e4SLinus Torvalds 		if (buf[0] == '1')
6561da177e4SLinus Torvalds 			return 1;
6571da177e4SLinus Torvalds 		else if (buf[0] == '0')
6581da177e4SLinus Torvalds 			return 0;
6591da177e4SLinus Torvalds 		else
6601da177e4SLinus Torvalds 			return -EINVAL;
6611da177e4SLinus Torvalds 	} else
6621da177e4SLinus Torvalds 		return -EINVAL;
6631da177e4SLinus Torvalds }
6641da177e4SLinus Torvalds #endif
6651da177e4SLinus Torvalds /*
6661da177e4SLinus Torvalds  * Create the actual show/store functions and data structures.
6671da177e4SLinus Torvalds  */
6681da177e4SLinus Torvalds sdev_rd_attr (type, "%d\n");
6691da177e4SLinus Torvalds sdev_rd_attr (scsi_level, "%d\n");
6701da177e4SLinus Torvalds sdev_rd_attr (vendor, "%.8s\n");
6711da177e4SLinus Torvalds sdev_rd_attr (model, "%.16s\n");
6721da177e4SLinus Torvalds sdev_rd_attr (rev, "%.4s\n");
67362488520SDamien Le Moal sdev_rd_attr (cdl_supported, "%d\n");
6741da177e4SLinus Torvalds 
67571e75c97SChristoph Hellwig static ssize_t
sdev_show_device_busy(struct device * dev,struct device_attribute * attr,char * buf)67671e75c97SChristoph Hellwig sdev_show_device_busy(struct device *dev, struct device_attribute *attr,
67771e75c97SChristoph Hellwig 		char *buf)
67871e75c97SChristoph Hellwig {
67971e75c97SChristoph Hellwig 	struct scsi_device *sdev = to_scsi_device(dev);
6808278807aSMing Lei 	return snprintf(buf, 20, "%d\n", scsi_device_busy(sdev));
68171e75c97SChristoph Hellwig }
68271e75c97SChristoph Hellwig static DEVICE_ATTR(device_busy, S_IRUGO, sdev_show_device_busy, NULL);
68371e75c97SChristoph Hellwig 
684cd9070c9SChristoph Hellwig static ssize_t
sdev_show_device_blocked(struct device * dev,struct device_attribute * attr,char * buf)685cd9070c9SChristoph Hellwig sdev_show_device_blocked(struct device *dev, struct device_attribute *attr,
686cd9070c9SChristoph Hellwig 		char *buf)
687cd9070c9SChristoph Hellwig {
688cd9070c9SChristoph Hellwig 	struct scsi_device *sdev = to_scsi_device(dev);
689cd9070c9SChristoph Hellwig 	return snprintf(buf, 20, "%d\n", atomic_read(&sdev->device_blocked));
690cd9070c9SChristoph Hellwig }
691cd9070c9SChristoph Hellwig static DEVICE_ATTR(device_blocked, S_IRUGO, sdev_show_device_blocked, NULL);
692cd9070c9SChristoph Hellwig 
693242f9dcbSJens Axboe /*
694242f9dcbSJens Axboe  * TODO: can we make these symlinks to the block layer ones?
695242f9dcbSJens Axboe  */
6961da177e4SLinus Torvalds static ssize_t
sdev_show_timeout(struct device * dev,struct device_attribute * attr,char * buf)69710523b3bSYani Ioannou sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
6981da177e4SLinus Torvalds {
6991da177e4SLinus Torvalds 	struct scsi_device *sdev;
7001da177e4SLinus Torvalds 	sdev = to_scsi_device(dev);
701242f9dcbSJens Axboe 	return snprintf(buf, 20, "%d\n", sdev->request_queue->rq_timeout / HZ);
7021da177e4SLinus Torvalds }
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds static ssize_t
sdev_store_timeout(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)705ee959b00STony Jones sdev_store_timeout (struct device *dev, struct device_attribute *attr,
706ee959b00STony Jones 		    const char *buf, size_t count)
7071da177e4SLinus Torvalds {
7081da177e4SLinus Torvalds 	struct scsi_device *sdev;
7091da177e4SLinus Torvalds 	int timeout;
7101da177e4SLinus Torvalds 	sdev = to_scsi_device(dev);
7111da177e4SLinus Torvalds 	sscanf (buf, "%d\n", &timeout);
712242f9dcbSJens Axboe 	blk_queue_rq_timeout(sdev->request_queue, timeout * HZ);
7131da177e4SLinus Torvalds 	return count;
7141da177e4SLinus Torvalds }
7151da177e4SLinus Torvalds static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds static ssize_t
sdev_show_eh_timeout(struct device * dev,struct device_attribute * attr,char * buf)7180816c925SMartin K. Petersen sdev_show_eh_timeout(struct device *dev, struct device_attribute *attr, char *buf)
7190816c925SMartin K. Petersen {
7200816c925SMartin K. Petersen 	struct scsi_device *sdev;
7210816c925SMartin K. Petersen 	sdev = to_scsi_device(dev);
7220816c925SMartin K. Petersen 	return snprintf(buf, 20, "%u\n", sdev->eh_timeout / HZ);
7230816c925SMartin K. Petersen }
7240816c925SMartin K. Petersen 
7250816c925SMartin K. Petersen static ssize_t
sdev_store_eh_timeout(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)7260816c925SMartin K. Petersen sdev_store_eh_timeout(struct device *dev, struct device_attribute *attr,
7270816c925SMartin K. Petersen 		    const char *buf, size_t count)
7280816c925SMartin K. Petersen {
7290816c925SMartin K. Petersen 	struct scsi_device *sdev;
7300816c925SMartin K. Petersen 	unsigned int eh_timeout;
7310816c925SMartin K. Petersen 	int err;
7320816c925SMartin K. Petersen 
7330816c925SMartin K. Petersen 	if (!capable(CAP_SYS_ADMIN))
7340816c925SMartin K. Petersen 		return -EACCES;
7350816c925SMartin K. Petersen 
7360816c925SMartin K. Petersen 	sdev = to_scsi_device(dev);
7370816c925SMartin K. Petersen 	err = kstrtouint(buf, 10, &eh_timeout);
7380816c925SMartin K. Petersen 	if (err)
7390816c925SMartin K. Petersen 		return err;
7400816c925SMartin K. Petersen 	sdev->eh_timeout = eh_timeout * HZ;
7410816c925SMartin K. Petersen 
7420816c925SMartin K. Petersen 	return count;
7430816c925SMartin K. Petersen }
7440816c925SMartin K. Petersen static DEVICE_ATTR(eh_timeout, S_IRUGO | S_IWUSR, sdev_show_eh_timeout, sdev_store_eh_timeout);
7450816c925SMartin K. Petersen 
7460816c925SMartin K. Petersen static ssize_t
store_rescan_field(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)747ee959b00STony Jones store_rescan_field (struct device *dev, struct device_attribute *attr,
748ee959b00STony Jones 		    const char *buf, size_t count)
7491da177e4SLinus Torvalds {
750*79519528SBart Van Assche 	scsi_rescan_device(to_scsi_device(dev));
7511da177e4SLinus Torvalds 	return count;
7521da177e4SLinus Torvalds }
7531da177e4SLinus Torvalds static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
7541da177e4SLinus Torvalds 
755ee959b00STony Jones static ssize_t
sdev_store_delete(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)756ee959b00STony Jones sdev_store_delete(struct device *dev, struct device_attribute *attr,
757ee959b00STony Jones 		  const char *buf, size_t count)
7581da177e4SLinus Torvalds {
7590ee223b2SBart Van Assche 	struct kernfs_node *kn;
76077c30128SYufen Yu 	struct scsi_device *sdev = to_scsi_device(dev);
76177c30128SYufen Yu 
76277c30128SYufen Yu 	/*
76377c30128SYufen Yu 	 * We need to try to get module, avoiding the module been removed
76477c30128SYufen Yu 	 * during delete.
76577c30128SYufen Yu 	 */
76677c30128SYufen Yu 	if (scsi_device_get(sdev))
76777c30128SYufen Yu 		return -ENODEV;
7680ee223b2SBart Van Assche 
7690ee223b2SBart Van Assche 	kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
7700ee223b2SBart Van Assche 	WARN_ON_ONCE(!kn);
7710ee223b2SBart Van Assche 	/*
7720ee223b2SBart Van Assche 	 * Concurrent writes into the "delete" sysfs attribute may trigger
7730ee223b2SBart Van Assche 	 * concurrent calls to device_remove_file() and scsi_remove_device().
7740ee223b2SBart Van Assche 	 * device_remove_file() handles concurrent removal calls by
7750ee223b2SBart Van Assche 	 * serializing these and by ignoring the second and later removal
7760ee223b2SBart Van Assche 	 * attempts.  Concurrent calls of scsi_remove_device() are
7770ee223b2SBart Van Assche 	 * serialized. The second and later calls of scsi_remove_device() are
7780ee223b2SBart Van Assche 	 * ignored because the first call of that function changes the device
7790ee223b2SBart Van Assche 	 * state into SDEV_DEL.
7800ee223b2SBart Van Assche 	 */
7810ee223b2SBart Van Assche 	device_remove_file(dev, attr);
78277c30128SYufen Yu 	scsi_remove_device(sdev);
7830ee223b2SBart Van Assche 	if (kn)
7840ee223b2SBart Van Assche 		sysfs_unbreak_active_protection(kn);
78577c30128SYufen Yu 	scsi_device_put(sdev);
7861da177e4SLinus Torvalds 	return count;
7871da177e4SLinus Torvalds };
7881da177e4SLinus Torvalds static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds static ssize_t
store_state_field(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)791ee959b00STony Jones store_state_field(struct device *dev, struct device_attribute *attr,
792ee959b00STony Jones 		  const char *buf, size_t count)
7931da177e4SLinus Torvalds {
7940db6ca8aSBart Van Assche 	int i, ret;
7951da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
7961da177e4SLinus Torvalds 	enum scsi_device_state state = 0;
7974edd8cd4SMike Christie 	bool rescan_dev = false;
7981da177e4SLinus Torvalds 
7996391a113STobias Klauser 	for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
8001da177e4SLinus Torvalds 		const int len = strlen(sdev_states[i].name);
8011da177e4SLinus Torvalds 		if (strncmp(sdev_states[i].name, buf, len) == 0 &&
8021da177e4SLinus Torvalds 		   buf[len] == '\n') {
8031da177e4SLinus Torvalds 			state = sdev_states[i].value;
8041da177e4SLinus Torvalds 			break;
8051da177e4SLinus Torvalds 		}
8061da177e4SLinus Torvalds 	}
807ac88c1f6SBart Van Assche 	switch (state) {
808ac88c1f6SBart Van Assche 	case SDEV_RUNNING:
809ac88c1f6SBart Van Assche 	case SDEV_OFFLINE:
810ac88c1f6SBart Van Assche 		break;
811ac88c1f6SBart Van Assche 	default:
8121da177e4SLinus Torvalds 		return -EINVAL;
813ac88c1f6SBart Van Assche 	}
8141da177e4SLinus Torvalds 
8150db6ca8aSBart Van Assche 	mutex_lock(&sdev->state_mutex);
8162331ce61SUday Shankar 	switch (sdev->sdev_state) {
8172331ce61SUday Shankar 	case SDEV_RUNNING:
8182331ce61SUday Shankar 	case SDEV_OFFLINE:
8192331ce61SUday Shankar 		break;
8202331ce61SUday Shankar 	default:
8212331ce61SUday Shankar 		mutex_unlock(&sdev->state_mutex);
8222331ce61SUday Shankar 		return -EINVAL;
8232331ce61SUday Shankar 	}
8244edd8cd4SMike Christie 	if (sdev->sdev_state == SDEV_RUNNING && state == SDEV_RUNNING) {
825eb97545dSMike Christie 		ret = 0;
8264edd8cd4SMike Christie 	} else {
8270db6ca8aSBart Van Assche 		ret = scsi_device_set_state(sdev, state);
8284edd8cd4SMike Christie 		if (ret == 0 && state == SDEV_RUNNING)
8294edd8cd4SMike Christie 			rescan_dev = true;
8304edd8cd4SMike Christie 	}
8314edd8cd4SMike Christie 	mutex_unlock(&sdev->state_mutex);
8324edd8cd4SMike Christie 
8334edd8cd4SMike Christie 	if (rescan_dev) {
83470fc085cSzhengbin 		/*
835f0f82e24Slijinlin 		 * If the device state changes to SDEV_RUNNING, we need to
83602c6dcd5SLi Jinlin 		 * run the queue to avoid I/O hang, and rescan the device
83702c6dcd5SLi Jinlin 		 * to revalidate it. Running the queue first is necessary
83802c6dcd5SLi Jinlin 		 * because another thread may be waiting inside
83902c6dcd5SLi Jinlin 		 * blk_mq_freeze_queue_wait() and because that call may be
84002c6dcd5SLi Jinlin 		 * waiting for pending I/O to finish.
84170fc085cSzhengbin 		 */
84270fc085cSzhengbin 		blk_mq_run_hw_queues(sdev->request_queue, true);
843*79519528SBart Van Assche 		scsi_rescan_device(sdev);
844f0f82e24Slijinlin 	}
8450db6ca8aSBart Van Assche 
8460db6ca8aSBart Van Assche 	return ret == 0 ? count : -EINVAL;
8471da177e4SLinus Torvalds }
8481da177e4SLinus Torvalds 
8491da177e4SLinus Torvalds static ssize_t
show_state_field(struct device * dev,struct device_attribute * attr,char * buf)85010523b3bSYani Ioannou show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
8511da177e4SLinus Torvalds {
8521da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
8531da177e4SLinus Torvalds 	const char *name = scsi_device_state_name(sdev->sdev_state);
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	if (!name)
8561da177e4SLinus Torvalds 		return -EINVAL;
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds 	return snprintf(buf, 20, "%s\n", name);
8591da177e4SLinus Torvalds }
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds static ssize_t
show_queue_type_field(struct device * dev,struct device_attribute * attr,char * buf)864ee959b00STony Jones show_queue_type_field(struct device *dev, struct device_attribute *attr,
865ee959b00STony Jones 		      char *buf)
8661da177e4SLinus Torvalds {
8671da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
8681da177e4SLinus Torvalds 	const char *name = "none";
8691da177e4SLinus Torvalds 
870609aa22fSChristoph Hellwig 	if (sdev->simple_tags)
8711da177e4SLinus Torvalds 		name = "simple";
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	return snprintf(buf, 20, "%s\n", name);
8741da177e4SLinus Torvalds }
8751da177e4SLinus Torvalds 
876276b20d0SHannes Reinecke static ssize_t
store_queue_type_field(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)877276b20d0SHannes Reinecke store_queue_type_field(struct device *dev, struct device_attribute *attr,
878276b20d0SHannes Reinecke 		       const char *buf, size_t count)
879276b20d0SHannes Reinecke {
880276b20d0SHannes Reinecke 	struct scsi_device *sdev = to_scsi_device(dev);
881276b20d0SHannes Reinecke 
882efc3c1dfSChristoph Hellwig 	if (!sdev->tagged_supported)
883276b20d0SHannes Reinecke 		return -EINVAL;
884276b20d0SHannes Reinecke 
885efc3c1dfSChristoph Hellwig 	sdev_printk(KERN_INFO, sdev,
886efc3c1dfSChristoph Hellwig 		    "ignoring write to deprecated queue_type attribute");
887276b20d0SHannes Reinecke 	return count;
888276b20d0SHannes Reinecke }
889276b20d0SHannes Reinecke 
890276b20d0SHannes Reinecke static DEVICE_ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
891276b20d0SHannes Reinecke 		   store_queue_type_field);
8921da177e4SLinus Torvalds 
893b3ae8780SHannes Reinecke #define sdev_vpd_pg_attr(_page)						\
894b3ae8780SHannes Reinecke static ssize_t							\
895b3ae8780SHannes Reinecke show_vpd_##_page(struct file *filp, struct kobject *kobj,	\
896b3ae8780SHannes Reinecke 		 struct bin_attribute *bin_attr,			\
897b3ae8780SHannes Reinecke 		 char *buf, loff_t off, size_t count)			\
898b3ae8780SHannes Reinecke {									\
8999e7bd945SGuosong Su 	struct device *dev = kobj_to_dev(kobj);				\
900b3ae8780SHannes Reinecke 	struct scsi_device *sdev = to_scsi_device(dev);			\
901ccf1e004SBart Van Assche 	struct scsi_vpd *vpd_page;					\
902ccf1e004SBart Van Assche 	int ret = -EINVAL;						\
903ccf1e004SBart Van Assche 									\
90409e2b0b1SHannes Reinecke 	rcu_read_lock();						\
905ccf1e004SBart Van Assche 	vpd_page = rcu_dereference(sdev->vpd_##_page);			\
906ccf1e004SBart Van Assche 	if (vpd_page)							\
90709e2b0b1SHannes Reinecke 		ret = memory_read_from_buffer(buf, count, &off,		\
908ccf1e004SBart Van Assche 				vpd_page->data, vpd_page->len);		\
90909e2b0b1SHannes Reinecke 	rcu_read_unlock();						\
91009e2b0b1SHannes Reinecke 	return ret;							\
911b3ae8780SHannes Reinecke }									\
912b3ae8780SHannes Reinecke static struct bin_attribute dev_attr_vpd_##_page = {		\
913b3ae8780SHannes Reinecke 	.attr =	{.name = __stringify(vpd_##_page), .mode = S_IRUGO },	\
914b3ae8780SHannes Reinecke 	.size = 0,							\
915b3ae8780SHannes Reinecke 	.read = show_vpd_##_page,					\
916b3ae8780SHannes Reinecke };
917b3ae8780SHannes Reinecke 
918b3ae8780SHannes Reinecke sdev_vpd_pg_attr(pg83);
919b3ae8780SHannes Reinecke sdev_vpd_pg_attr(pg80);
920d188b067SRyan Attard sdev_vpd_pg_attr(pg89);
921e60ac0b9SMartin K. Petersen sdev_vpd_pg_attr(pgb0);
922e60ac0b9SMartin K. Petersen sdev_vpd_pg_attr(pgb1);
923e60ac0b9SMartin K. Petersen sdev_vpd_pg_attr(pgb2);
924d188b067SRyan Attard sdev_vpd_pg_attr(pg0);
9251da177e4SLinus Torvalds 
show_inquiry(struct file * filep,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)92692e6246cSJohannes Thumshirn static ssize_t show_inquiry(struct file *filep, struct kobject *kobj,
92792e6246cSJohannes Thumshirn 			    struct bin_attribute *bin_attr,
92892e6246cSJohannes Thumshirn 			    char *buf, loff_t off, size_t count)
92992e6246cSJohannes Thumshirn {
9309e7bd945SGuosong Su 	struct device *dev = kobj_to_dev(kobj);
93192e6246cSJohannes Thumshirn 	struct scsi_device *sdev = to_scsi_device(dev);
93292e6246cSJohannes Thumshirn 
93392e6246cSJohannes Thumshirn 	if (!sdev->inquiry)
93492e6246cSJohannes Thumshirn 		return -EINVAL;
93592e6246cSJohannes Thumshirn 
93692e6246cSJohannes Thumshirn 	return memory_read_from_buffer(buf, count, &off, sdev->inquiry,
93792e6246cSJohannes Thumshirn 				       sdev->inquiry_len);
93892e6246cSJohannes Thumshirn }
93992e6246cSJohannes Thumshirn 
94092e6246cSJohannes Thumshirn static struct bin_attribute dev_attr_inquiry = {
94192e6246cSJohannes Thumshirn 	.attr = {
94292e6246cSJohannes Thumshirn 		.name = "inquiry",
94392e6246cSJohannes Thumshirn 		.mode = S_IRUGO,
94492e6246cSJohannes Thumshirn 	},
94592e6246cSJohannes Thumshirn 	.size = 0,
94692e6246cSJohannes Thumshirn 	.read = show_inquiry,
94792e6246cSJohannes Thumshirn };
94892e6246cSJohannes Thumshirn 
9491da177e4SLinus Torvalds static ssize_t
show_iostat_counterbits(struct device * dev,struct device_attribute * attr,char * buf)950b3ae8780SHannes Reinecke show_iostat_counterbits(struct device *dev, struct device_attribute *attr,
951b3ae8780SHannes Reinecke 			char *buf)
9521da177e4SLinus Torvalds {
9531da177e4SLinus Torvalds 	return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
9541da177e4SLinus Torvalds }
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds #define show_sdev_iostat(field)						\
9591da177e4SLinus Torvalds static ssize_t								\
960ee959b00STony Jones show_iostat_##field(struct device *dev, struct device_attribute *attr,	\
961ee959b00STony Jones 		    char *buf)						\
9621da177e4SLinus Torvalds {									\
9631da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);			\
9641da177e4SLinus Torvalds 	unsigned long long count = atomic_read(&sdev->field);		\
9651da177e4SLinus Torvalds 	return snprintf(buf, 20, "0x%llx\n", count);			\
9661da177e4SLinus Torvalds }									\
9671da177e4SLinus Torvalds static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds show_sdev_iostat(iorequest_cnt);
9701da177e4SLinus Torvalds show_sdev_iostat(iodone_cnt);
9711da177e4SLinus Torvalds show_sdev_iostat(ioerr_cnt);
97248517eefSWu Bo show_sdev_iostat(iotmo_cnt);
9731da177e4SLinus Torvalds 
974d7b8bcb0SMichael Tokarev static ssize_t
sdev_show_modalias(struct device * dev,struct device_attribute * attr,char * buf)975d7b8bcb0SMichael Tokarev sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
976d7b8bcb0SMichael Tokarev {
977d7b8bcb0SMichael Tokarev 	struct scsi_device *sdev;
978d7b8bcb0SMichael Tokarev 	sdev = to_scsi_device(dev);
979d7b8bcb0SMichael Tokarev 	return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
980d7b8bcb0SMichael Tokarev }
981d7b8bcb0SMichael Tokarev static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
9821da177e4SLinus Torvalds 
983a341cd0fSJeff Garzik #define DECLARE_EVT_SHOW(name, Cap_name)				\
984a341cd0fSJeff Garzik static ssize_t								\
985a341cd0fSJeff Garzik sdev_show_evt_##name(struct device *dev, struct device_attribute *attr,	\
986a341cd0fSJeff Garzik 		     char *buf)						\
987a341cd0fSJeff Garzik {									\
988a341cd0fSJeff Garzik 	struct scsi_device *sdev = to_scsi_device(dev);			\
989a341cd0fSJeff Garzik 	int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
990a341cd0fSJeff Garzik 	return snprintf(buf, 20, "%d\n", val);				\
991a341cd0fSJeff Garzik }
992a341cd0fSJeff Garzik 
993a341cd0fSJeff Garzik #define DECLARE_EVT_STORE(name, Cap_name)				\
994a341cd0fSJeff Garzik static ssize_t								\
995a341cd0fSJeff Garzik sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
996a341cd0fSJeff Garzik 		      const char *buf, size_t count)			\
997a341cd0fSJeff Garzik {									\
998a341cd0fSJeff Garzik 	struct scsi_device *sdev = to_scsi_device(dev);			\
999a341cd0fSJeff Garzik 	int val = simple_strtoul(buf, NULL, 0);				\
1000a341cd0fSJeff Garzik 	if (val == 0)							\
1001a341cd0fSJeff Garzik 		clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events);	\
1002a341cd0fSJeff Garzik 	else if (val == 1)						\
1003a341cd0fSJeff Garzik 		set_bit(SDEV_EVT_##Cap_name, sdev->supported_events);	\
1004a341cd0fSJeff Garzik 	else								\
1005a341cd0fSJeff Garzik 		return -EINVAL;						\
1006a341cd0fSJeff Garzik 	return count;							\
1007a341cd0fSJeff Garzik }
1008a341cd0fSJeff Garzik 
1009a341cd0fSJeff Garzik #define DECLARE_EVT(name, Cap_name)					\
1010a341cd0fSJeff Garzik 	DECLARE_EVT_SHOW(name, Cap_name)				\
1011a341cd0fSJeff Garzik 	DECLARE_EVT_STORE(name, Cap_name)				\
1012a341cd0fSJeff Garzik 	static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name,	\
1013a341cd0fSJeff Garzik 			   sdev_store_evt_##name);
1014a341cd0fSJeff Garzik #define REF_EVT(name) &dev_attr_evt_##name.attr
1015a341cd0fSJeff Garzik 
DECLARE_EVT(media_change,MEDIA_CHANGE)1016a341cd0fSJeff Garzik DECLARE_EVT(media_change, MEDIA_CHANGE)
1017279afdfeSEwan D. Milne DECLARE_EVT(inquiry_change_reported, INQUIRY_CHANGE_REPORTED)
1018279afdfeSEwan D. Milne DECLARE_EVT(capacity_change_reported, CAPACITY_CHANGE_REPORTED)
1019279afdfeSEwan D. Milne DECLARE_EVT(soft_threshold_reached, SOFT_THRESHOLD_REACHED_REPORTED)
1020279afdfeSEwan D. Milne DECLARE_EVT(mode_parameter_change_reported, MODE_PARAMETER_CHANGE_REPORTED)
1021279afdfeSEwan D. Milne DECLARE_EVT(lun_change_reported, LUN_CHANGE_REPORTED)
1022a341cd0fSJeff Garzik 
1023ee959b00STony Jones static ssize_t
1024276b20d0SHannes Reinecke sdev_store_queue_depth(struct device *dev, struct device_attribute *attr,
1025ee959b00STony Jones 		       const char *buf, size_t count)
10261da177e4SLinus Torvalds {
10271da177e4SLinus Torvalds 	int depth, retval;
10281da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
102931435de9SBart Van Assche 	const struct scsi_host_template *sht = sdev->host->hostt;
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds 	if (!sht->change_queue_depth)
10321da177e4SLinus Torvalds 		return -EINVAL;
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds 	depth = simple_strtoul(buf, NULL, 0);
10351da177e4SLinus Torvalds 
10361278dd68SJens Axboe 	if (depth < 1 || depth > sdev->host->can_queue)
10371da177e4SLinus Torvalds 		return -EINVAL;
10381da177e4SLinus Torvalds 
1039db5ed4dfSChristoph Hellwig 	retval = sht->change_queue_depth(sdev, depth);
10401da177e4SLinus Torvalds 	if (retval < 0)
10411da177e4SLinus Torvalds 		return retval;
10421da177e4SLinus Torvalds 
10434a84067dSVasu Dev 	sdev->max_queue_depth = sdev->queue_depth;
10444a84067dSVasu Dev 
10451da177e4SLinus Torvalds 	return count;
10461da177e4SLinus Torvalds }
1047276b20d0SHannes Reinecke sdev_show_function(queue_depth, "%d\n");
10481da177e4SLinus Torvalds 
1049276b20d0SHannes Reinecke static DEVICE_ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
1050276b20d0SHannes Reinecke 		   sdev_store_queue_depth);
10511da177e4SLinus Torvalds 
1052248d4fe9SHannes Reinecke static ssize_t
sdev_show_wwid(struct device * dev,struct device_attribute * attr,char * buf)1053248d4fe9SHannes Reinecke sdev_show_wwid(struct device *dev, struct device_attribute *attr,
1054248d4fe9SHannes Reinecke 		    char *buf)
1055248d4fe9SHannes Reinecke {
1056248d4fe9SHannes Reinecke 	struct scsi_device *sdev = to_scsi_device(dev);
1057248d4fe9SHannes Reinecke 	ssize_t count;
1058248d4fe9SHannes Reinecke 
1059248d4fe9SHannes Reinecke 	count = scsi_vpd_lun_id(sdev, buf, PAGE_SIZE);
1060248d4fe9SHannes Reinecke 	if (count > 0) {
1061248d4fe9SHannes Reinecke 		buf[count] = '\n';
1062248d4fe9SHannes Reinecke 		count++;
1063248d4fe9SHannes Reinecke 	}
1064248d4fe9SHannes Reinecke 	return count;
1065248d4fe9SHannes Reinecke }
1066248d4fe9SHannes Reinecke static DEVICE_ATTR(wwid, S_IRUGO, sdev_show_wwid, NULL);
1067248d4fe9SHannes Reinecke 
1068093b8886SBart Van Assche #define BLIST_FLAG_NAME(name)					\
106914098803SMartin Wilck 	[const_ilog2((__force __u64)BLIST_##name)] = #name
1070345e2960SHannes Reinecke static const char *const sdev_bflags_name[] = {
1071345e2960SHannes Reinecke #include "scsi_devinfo_tbl.c"
1072345e2960SHannes Reinecke };
1073345e2960SHannes Reinecke #undef BLIST_FLAG_NAME
1074345e2960SHannes Reinecke 
1075345e2960SHannes Reinecke static ssize_t
sdev_show_blacklist(struct device * dev,struct device_attribute * attr,char * buf)1076345e2960SHannes Reinecke sdev_show_blacklist(struct device *dev, struct device_attribute *attr,
1077345e2960SHannes Reinecke 		    char *buf)
1078345e2960SHannes Reinecke {
1079345e2960SHannes Reinecke 	struct scsi_device *sdev = to_scsi_device(dev);
1080345e2960SHannes Reinecke 	int i;
1081345e2960SHannes Reinecke 	ssize_t len = 0;
1082345e2960SHannes Reinecke 
1083345e2960SHannes Reinecke 	for (i = 0; i < sizeof(sdev->sdev_bflags) * BITS_PER_BYTE; i++) {
1084345e2960SHannes Reinecke 		const char *name = NULL;
1085345e2960SHannes Reinecke 
1086093b8886SBart Van Assche 		if (!(sdev->sdev_bflags & (__force blist_flags_t)BIT(i)))
1087345e2960SHannes Reinecke 			continue;
1088345e2960SHannes Reinecke 		if (i < ARRAY_SIZE(sdev_bflags_name) && sdev_bflags_name[i])
1089345e2960SHannes Reinecke 			name = sdev_bflags_name[i];
1090345e2960SHannes Reinecke 
1091345e2960SHannes Reinecke 		if (name)
109281546b32STakashi Iwai 			len += scnprintf(buf + len, PAGE_SIZE - len,
1093345e2960SHannes Reinecke 					 "%s%s", len ? " " : "", name);
1094345e2960SHannes Reinecke 		else
109581546b32STakashi Iwai 			len += scnprintf(buf + len, PAGE_SIZE - len,
1096345e2960SHannes Reinecke 					 "%sINVALID_BIT(%d)", len ? " " : "", i);
1097345e2960SHannes Reinecke 	}
1098345e2960SHannes Reinecke 	if (len)
109981546b32STakashi Iwai 		len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
1100345e2960SHannes Reinecke 	return len;
1101345e2960SHannes Reinecke }
1102345e2960SHannes Reinecke static DEVICE_ATTR(blacklist, S_IRUGO, sdev_show_blacklist, NULL);
1103345e2960SHannes Reinecke 
110441f95dd2SHannes Reinecke #ifdef CONFIG_SCSI_DH
110541f95dd2SHannes Reinecke static ssize_t
sdev_show_dh_state(struct device * dev,struct device_attribute * attr,char * buf)110641f95dd2SHannes Reinecke sdev_show_dh_state(struct device *dev, struct device_attribute *attr,
110741f95dd2SHannes Reinecke 		   char *buf)
110841f95dd2SHannes Reinecke {
110941f95dd2SHannes Reinecke 	struct scsi_device *sdev = to_scsi_device(dev);
111041f95dd2SHannes Reinecke 
111141f95dd2SHannes Reinecke 	if (!sdev->handler)
111241f95dd2SHannes Reinecke 		return snprintf(buf, 20, "detached\n");
111341f95dd2SHannes Reinecke 
111441f95dd2SHannes Reinecke 	return snprintf(buf, 20, "%s\n", sdev->handler->name);
111541f95dd2SHannes Reinecke }
111641f95dd2SHannes Reinecke 
111741f95dd2SHannes Reinecke static ssize_t
sdev_store_dh_state(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)111841f95dd2SHannes Reinecke sdev_store_dh_state(struct device *dev, struct device_attribute *attr,
111941f95dd2SHannes Reinecke 		    const char *buf, size_t count)
112041f95dd2SHannes Reinecke {
112141f95dd2SHannes Reinecke 	struct scsi_device *sdev = to_scsi_device(dev);
112241f95dd2SHannes Reinecke 	int err = -EINVAL;
112341f95dd2SHannes Reinecke 
112441f95dd2SHannes Reinecke 	if (sdev->sdev_state == SDEV_CANCEL ||
112541f95dd2SHannes Reinecke 	    sdev->sdev_state == SDEV_DEL)
112641f95dd2SHannes Reinecke 		return -ENODEV;
112741f95dd2SHannes Reinecke 
112841f95dd2SHannes Reinecke 	if (!sdev->handler) {
112941f95dd2SHannes Reinecke 		/*
113041f95dd2SHannes Reinecke 		 * Attach to a device handler
113141f95dd2SHannes Reinecke 		 */
113241f95dd2SHannes Reinecke 		err = scsi_dh_attach(sdev->request_queue, buf);
113341f95dd2SHannes Reinecke 	} else if (!strncmp(buf, "activate", 8)) {
113441f95dd2SHannes Reinecke 		/*
113541f95dd2SHannes Reinecke 		 * Activate a device handler
113641f95dd2SHannes Reinecke 		 */
113741f95dd2SHannes Reinecke 		if (sdev->handler->activate)
113841f95dd2SHannes Reinecke 			err = sdev->handler->activate(sdev, NULL, NULL);
113941f95dd2SHannes Reinecke 		else
114041f95dd2SHannes Reinecke 			err = 0;
114141f95dd2SHannes Reinecke 	} else if (!strncmp(buf, "detach", 6)) {
114241f95dd2SHannes Reinecke 		/*
114341f95dd2SHannes Reinecke 		 * Detach from a device handler
114441f95dd2SHannes Reinecke 		 */
114541f95dd2SHannes Reinecke 		sdev_printk(KERN_WARNING, sdev,
114641f95dd2SHannes Reinecke 			    "can't detach handler %s.\n",
114741f95dd2SHannes Reinecke 			    sdev->handler->name);
114841f95dd2SHannes Reinecke 		err = -EINVAL;
114941f95dd2SHannes Reinecke 	}
115041f95dd2SHannes Reinecke 
115141f95dd2SHannes Reinecke 	return err < 0 ? err : count;
115241f95dd2SHannes Reinecke }
115341f95dd2SHannes Reinecke 
115441f95dd2SHannes Reinecke static DEVICE_ATTR(dh_state, S_IRUGO | S_IWUSR, sdev_show_dh_state,
115541f95dd2SHannes Reinecke 		   sdev_store_dh_state);
115677c9df96SHannes Reinecke 
115777c9df96SHannes Reinecke static ssize_t
sdev_show_access_state(struct device * dev,struct device_attribute * attr,char * buf)115877c9df96SHannes Reinecke sdev_show_access_state(struct device *dev,
115977c9df96SHannes Reinecke 		       struct device_attribute *attr,
116077c9df96SHannes Reinecke 		       char *buf)
116177c9df96SHannes Reinecke {
116277c9df96SHannes Reinecke 	struct scsi_device *sdev = to_scsi_device(dev);
116377c9df96SHannes Reinecke 	unsigned char access_state;
116477c9df96SHannes Reinecke 	const char *access_state_name;
116577c9df96SHannes Reinecke 
116677c9df96SHannes Reinecke 	if (!sdev->handler)
116777c9df96SHannes Reinecke 		return -EINVAL;
116877c9df96SHannes Reinecke 
116977c9df96SHannes Reinecke 	access_state = (sdev->access_state & SCSI_ACCESS_STATE_MASK);
117077c9df96SHannes Reinecke 	access_state_name = scsi_access_state_name(access_state);
117177c9df96SHannes Reinecke 
117277c9df96SHannes Reinecke 	return sprintf(buf, "%s\n",
117377c9df96SHannes Reinecke 		       access_state_name ? access_state_name : "unknown");
117477c9df96SHannes Reinecke }
117577c9df96SHannes Reinecke static DEVICE_ATTR(access_state, S_IRUGO, sdev_show_access_state, NULL);
117677c9df96SHannes Reinecke 
117777c9df96SHannes Reinecke static ssize_t
sdev_show_preferred_path(struct device * dev,struct device_attribute * attr,char * buf)117877c9df96SHannes Reinecke sdev_show_preferred_path(struct device *dev,
117977c9df96SHannes Reinecke 			 struct device_attribute *attr,
118077c9df96SHannes Reinecke 			 char *buf)
118177c9df96SHannes Reinecke {
118277c9df96SHannes Reinecke 	struct scsi_device *sdev = to_scsi_device(dev);
118377c9df96SHannes Reinecke 
118477c9df96SHannes Reinecke 	if (!sdev->handler)
118577c9df96SHannes Reinecke 		return -EINVAL;
118677c9df96SHannes Reinecke 
118777c9df96SHannes Reinecke 	if (sdev->access_state & SCSI_ACCESS_STATE_PREFERRED)
118877c9df96SHannes Reinecke 		return sprintf(buf, "1\n");
118977c9df96SHannes Reinecke 	else
119077c9df96SHannes Reinecke 		return sprintf(buf, "0\n");
119177c9df96SHannes Reinecke }
119277c9df96SHannes Reinecke static DEVICE_ATTR(preferred_path, S_IRUGO, sdev_show_preferred_path, NULL);
119341f95dd2SHannes Reinecke #endif
119441f95dd2SHannes Reinecke 
1195ee959b00STony Jones static ssize_t
sdev_show_queue_ramp_up_period(struct device * dev,struct device_attribute * attr,char * buf)11964a84067dSVasu Dev sdev_show_queue_ramp_up_period(struct device *dev,
11974a84067dSVasu Dev 			       struct device_attribute *attr,
11984a84067dSVasu Dev 			       char *buf)
11994a84067dSVasu Dev {
12004a84067dSVasu Dev 	struct scsi_device *sdev;
12014a84067dSVasu Dev 	sdev = to_scsi_device(dev);
12024a84067dSVasu Dev 	return snprintf(buf, 20, "%u\n",
12034a84067dSVasu Dev 			jiffies_to_msecs(sdev->queue_ramp_up_period));
12044a84067dSVasu Dev }
12054a84067dSVasu Dev 
12064a84067dSVasu Dev static ssize_t
sdev_store_queue_ramp_up_period(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)12074a84067dSVasu Dev sdev_store_queue_ramp_up_period(struct device *dev,
12084a84067dSVasu Dev 				struct device_attribute *attr,
12094a84067dSVasu Dev 				const char *buf, size_t count)
12104a84067dSVasu Dev {
12114a84067dSVasu Dev 	struct scsi_device *sdev = to_scsi_device(dev);
1212f7c65af5SDaniel Walter 	unsigned int period;
12134a84067dSVasu Dev 
1214f7c65af5SDaniel Walter 	if (kstrtouint(buf, 10, &period))
12154a84067dSVasu Dev 		return -EINVAL;
12164a84067dSVasu Dev 
12174a84067dSVasu Dev 	sdev->queue_ramp_up_period = msecs_to_jiffies(period);
12185cb9b40dSPeter Oberparleiter 	return count;
12194a84067dSVasu Dev }
12204a84067dSVasu Dev 
1221276b20d0SHannes Reinecke static DEVICE_ATTR(queue_ramp_up_period, S_IRUGO | S_IWUSR,
12224a84067dSVasu Dev 		   sdev_show_queue_ramp_up_period,
12234a84067dSVasu Dev 		   sdev_store_queue_ramp_up_period);
12244a84067dSVasu Dev 
sdev_show_cdl_enable(struct device * dev,struct device_attribute * attr,char * buf)12251b22cfb1SDamien Le Moal static ssize_t sdev_show_cdl_enable(struct device *dev,
12261b22cfb1SDamien Le Moal 				    struct device_attribute *attr, char *buf)
12271b22cfb1SDamien Le Moal {
12281b22cfb1SDamien Le Moal 	struct scsi_device *sdev = to_scsi_device(dev);
12291b22cfb1SDamien Le Moal 
12301b22cfb1SDamien Le Moal 	return sysfs_emit(buf, "%d\n", (int)sdev->cdl_enable);
12311b22cfb1SDamien Le Moal }
12321b22cfb1SDamien Le Moal 
sdev_store_cdl_enable(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)12331b22cfb1SDamien Le Moal static ssize_t sdev_store_cdl_enable(struct device *dev,
12341b22cfb1SDamien Le Moal 				     struct device_attribute *attr,
12351b22cfb1SDamien Le Moal 				     const char *buf, size_t count)
12361b22cfb1SDamien Le Moal {
12371b22cfb1SDamien Le Moal 	int ret;
12381b22cfb1SDamien Le Moal 	bool v;
12391b22cfb1SDamien Le Moal 
12401b22cfb1SDamien Le Moal 	if (kstrtobool(buf, &v))
12411b22cfb1SDamien Le Moal 		return -EINVAL;
12421b22cfb1SDamien Le Moal 
12431b22cfb1SDamien Le Moal 	ret = scsi_cdl_enable(to_scsi_device(dev), v);
12441b22cfb1SDamien Le Moal 	if (ret)
12451b22cfb1SDamien Le Moal 		return ret;
12461b22cfb1SDamien Le Moal 
12471b22cfb1SDamien Le Moal 	return count;
12481b22cfb1SDamien Le Moal }
12491b22cfb1SDamien Le Moal static DEVICE_ATTR(cdl_enable, S_IRUGO | S_IWUSR,
12501b22cfb1SDamien Le Moal 		   sdev_show_cdl_enable, sdev_store_cdl_enable);
12511b22cfb1SDamien Le Moal 
scsi_sdev_attr_is_visible(struct kobject * kobj,struct attribute * attr,int i)1252276b20d0SHannes Reinecke static umode_t scsi_sdev_attr_is_visible(struct kobject *kobj,
1253276b20d0SHannes Reinecke 					 struct attribute *attr, int i)
12541da177e4SLinus Torvalds {
12559e7bd945SGuosong Su 	struct device *dev = kobj_to_dev(kobj);
12561da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 
1259276b20d0SHannes Reinecke 	if (attr == &dev_attr_queue_depth.attr &&
1260276b20d0SHannes Reinecke 	    !sdev->host->hostt->change_queue_depth)
1261276b20d0SHannes Reinecke 		return S_IRUGO;
12621da177e4SLinus Torvalds 
1263276b20d0SHannes Reinecke 	if (attr == &dev_attr_queue_ramp_up_period.attr &&
1264276b20d0SHannes Reinecke 	    !sdev->host->hostt->change_queue_depth)
1265276b20d0SHannes Reinecke 		return 0;
12661da177e4SLinus Torvalds 
1267276b20d0SHannes Reinecke 	return attr->mode;
12681da177e4SLinus Torvalds }
12691da177e4SLinus Torvalds 
scsi_sdev_bin_attr_is_visible(struct kobject * kobj,struct bin_attribute * attr,int i)12707e47976bSHannes Reinecke static umode_t scsi_sdev_bin_attr_is_visible(struct kobject *kobj,
12717e47976bSHannes Reinecke 					     struct bin_attribute *attr, int i)
12727e47976bSHannes Reinecke {
12739e7bd945SGuosong Su 	struct device *dev = kobj_to_dev(kobj);
12747e47976bSHannes Reinecke 	struct scsi_device *sdev = to_scsi_device(dev);
12757e47976bSHannes Reinecke 
12767e47976bSHannes Reinecke 
1277d188b067SRyan Attard 	if (attr == &dev_attr_vpd_pg0 && !sdev->vpd_pg0)
1278d188b067SRyan Attard 		return 0;
1279d188b067SRyan Attard 
12807e47976bSHannes Reinecke 	if (attr == &dev_attr_vpd_pg80 && !sdev->vpd_pg80)
12817e47976bSHannes Reinecke 		return 0;
12827e47976bSHannes Reinecke 
128368887582SHannes Reinecke 	if (attr == &dev_attr_vpd_pg83 && !sdev->vpd_pg83)
12847e47976bSHannes Reinecke 		return 0;
12857e47976bSHannes Reinecke 
1286d188b067SRyan Attard 	if (attr == &dev_attr_vpd_pg89 && !sdev->vpd_pg89)
1287d188b067SRyan Attard 		return 0;
1288d188b067SRyan Attard 
1289e60ac0b9SMartin K. Petersen 	if (attr == &dev_attr_vpd_pgb0 && !sdev->vpd_pgb0)
1290e60ac0b9SMartin K. Petersen 		return 0;
1291e60ac0b9SMartin K. Petersen 
1292e60ac0b9SMartin K. Petersen 	if (attr == &dev_attr_vpd_pgb1 && !sdev->vpd_pgb1)
1293e60ac0b9SMartin K. Petersen 		return 0;
1294e60ac0b9SMartin K. Petersen 
1295e60ac0b9SMartin K. Petersen 	if (attr == &dev_attr_vpd_pgb2 && !sdev->vpd_pgb2)
1296e60ac0b9SMartin K. Petersen 		return 0;
1297e60ac0b9SMartin K. Petersen 
12987e47976bSHannes Reinecke 	return S_IRUGO;
12997e47976bSHannes Reinecke }
13007e47976bSHannes Reinecke 
1301276b20d0SHannes Reinecke /* Default template for device attributes.  May NOT be modified */
1302276b20d0SHannes Reinecke static struct attribute *scsi_sdev_attrs[] = {
1303276b20d0SHannes Reinecke 	&dev_attr_device_blocked.attr,
1304276b20d0SHannes Reinecke 	&dev_attr_type.attr,
1305276b20d0SHannes Reinecke 	&dev_attr_scsi_level.attr,
1306276b20d0SHannes Reinecke 	&dev_attr_device_busy.attr,
1307276b20d0SHannes Reinecke 	&dev_attr_vendor.attr,
1308276b20d0SHannes Reinecke 	&dev_attr_model.attr,
1309276b20d0SHannes Reinecke 	&dev_attr_rev.attr,
1310276b20d0SHannes Reinecke 	&dev_attr_rescan.attr,
1311276b20d0SHannes Reinecke 	&dev_attr_delete.attr,
1312276b20d0SHannes Reinecke 	&dev_attr_state.attr,
1313276b20d0SHannes Reinecke 	&dev_attr_timeout.attr,
1314276b20d0SHannes Reinecke 	&dev_attr_eh_timeout.attr,
1315276b20d0SHannes Reinecke 	&dev_attr_iocounterbits.attr,
1316276b20d0SHannes Reinecke 	&dev_attr_iorequest_cnt.attr,
1317276b20d0SHannes Reinecke 	&dev_attr_iodone_cnt.attr,
1318276b20d0SHannes Reinecke 	&dev_attr_ioerr_cnt.attr,
131948517eefSWu Bo 	&dev_attr_iotmo_cnt.attr,
1320276b20d0SHannes Reinecke 	&dev_attr_modalias.attr,
1321276b20d0SHannes Reinecke 	&dev_attr_queue_depth.attr,
1322276b20d0SHannes Reinecke 	&dev_attr_queue_type.attr,
1323248d4fe9SHannes Reinecke 	&dev_attr_wwid.attr,
1324345e2960SHannes Reinecke 	&dev_attr_blacklist.attr,
132541f95dd2SHannes Reinecke #ifdef CONFIG_SCSI_DH
132641f95dd2SHannes Reinecke 	&dev_attr_dh_state.attr,
132777c9df96SHannes Reinecke 	&dev_attr_access_state.attr,
132877c9df96SHannes Reinecke 	&dev_attr_preferred_path.attr,
132941f95dd2SHannes Reinecke #endif
1330276b20d0SHannes Reinecke 	&dev_attr_queue_ramp_up_period.attr,
133162488520SDamien Le Moal 	&dev_attr_cdl_supported.attr,
13321b22cfb1SDamien Le Moal 	&dev_attr_cdl_enable.attr,
1333276b20d0SHannes Reinecke 	REF_EVT(media_change),
1334276b20d0SHannes Reinecke 	REF_EVT(inquiry_change_reported),
1335276b20d0SHannes Reinecke 	REF_EVT(capacity_change_reported),
1336276b20d0SHannes Reinecke 	REF_EVT(soft_threshold_reached),
1337276b20d0SHannes Reinecke 	REF_EVT(mode_parameter_change_reported),
1338276b20d0SHannes Reinecke 	REF_EVT(lun_change_reported),
1339276b20d0SHannes Reinecke 	NULL
1340276b20d0SHannes Reinecke };
1341276b20d0SHannes Reinecke 
1342b3ae8780SHannes Reinecke static struct bin_attribute *scsi_sdev_bin_attrs[] = {
1343d188b067SRyan Attard 	&dev_attr_vpd_pg0,
1344b3ae8780SHannes Reinecke 	&dev_attr_vpd_pg83,
1345b3ae8780SHannes Reinecke 	&dev_attr_vpd_pg80,
1346d188b067SRyan Attard 	&dev_attr_vpd_pg89,
1347e60ac0b9SMartin K. Petersen 	&dev_attr_vpd_pgb0,
1348e60ac0b9SMartin K. Petersen 	&dev_attr_vpd_pgb1,
1349e60ac0b9SMartin K. Petersen 	&dev_attr_vpd_pgb2,
135092e6246cSJohannes Thumshirn 	&dev_attr_inquiry,
1351b3ae8780SHannes Reinecke 	NULL
1352b3ae8780SHannes Reinecke };
1353276b20d0SHannes Reinecke static struct attribute_group scsi_sdev_attr_group = {
1354276b20d0SHannes Reinecke 	.attrs =	scsi_sdev_attrs,
1355b3ae8780SHannes Reinecke 	.bin_attrs =	scsi_sdev_bin_attrs,
1356276b20d0SHannes Reinecke 	.is_visible =	scsi_sdev_attr_is_visible,
13577e47976bSHannes Reinecke 	.is_bin_visible = scsi_sdev_bin_attr_is_visible,
1358276b20d0SHannes Reinecke };
1359276b20d0SHannes Reinecke 
1360276b20d0SHannes Reinecke static const struct attribute_group *scsi_sdev_attr_groups[] = {
1361276b20d0SHannes Reinecke 	&scsi_sdev_attr_group,
1362276b20d0SHannes Reinecke 	NULL
1363276b20d0SHannes Reinecke };
1364276b20d0SHannes Reinecke 
scsi_target_add(struct scsi_target * starget)1365643eb2d9SJames Bottomley static int scsi_target_add(struct scsi_target *starget)
1366643eb2d9SJames Bottomley {
1367643eb2d9SJames Bottomley 	int error;
1368643eb2d9SJames Bottomley 
1369643eb2d9SJames Bottomley 	if (starget->state != STARGET_CREATED)
1370643eb2d9SJames Bottomley 		return 0;
1371643eb2d9SJames Bottomley 
1372643eb2d9SJames Bottomley 	error = device_add(&starget->dev);
1373643eb2d9SJames Bottomley 	if (error) {
1374643eb2d9SJames Bottomley 		dev_err(&starget->dev, "target device_add failed, error %d\n", error);
1375643eb2d9SJames Bottomley 		return error;
1376643eb2d9SJames Bottomley 	}
1377643eb2d9SJames Bottomley 	transport_add_device(&starget->dev);
1378643eb2d9SJames Bottomley 	starget->state = STARGET_RUNNING;
1379643eb2d9SJames Bottomley 
1380bc4f2401SAlan Stern 	pm_runtime_set_active(&starget->dev);
1381bc4f2401SAlan Stern 	pm_runtime_enable(&starget->dev);
1382bc4f2401SAlan Stern 	device_enable_async_suspend(&starget->dev);
1383bc4f2401SAlan Stern 
1384643eb2d9SJames Bottomley 	return 0;
1385643eb2d9SJames Bottomley }
1386643eb2d9SJames Bottomley 
13871da177e4SLinus Torvalds /**
13881da177e4SLinus Torvalds  * scsi_sysfs_add_sdev - add scsi device to sysfs
13891da177e4SLinus Torvalds  * @sdev:	scsi_device to add
13901da177e4SLinus Torvalds  *
13911da177e4SLinus Torvalds  * Return value:
13921da177e4SLinus Torvalds  * 	0 on Success / non-zero on Failure
13931da177e4SLinus Torvalds  **/
scsi_sysfs_add_sdev(struct scsi_device * sdev)13941da177e4SLinus Torvalds int scsi_sysfs_add_sdev(struct scsi_device *sdev)
13951da177e4SLinus Torvalds {
139692c4b58bSBart Van Assche 	int error;
1397643eb2d9SJames Bottomley 	struct scsi_target *starget = sdev->sdev_target;
13981da177e4SLinus Torvalds 
1399643eb2d9SJames Bottomley 	error = scsi_target_add(starget);
1400643eb2d9SJames Bottomley 	if (error)
1401643eb2d9SJames Bottomley 		return error;
1402643eb2d9SJames Bottomley 
1403643eb2d9SJames Bottomley 	transport_configure_device(&starget->dev);
1404bc4f2401SAlan Stern 
14054cb077d9SRafael J. Wysocki 	device_enable_async_suspend(&sdev->sdev_gendev);
1406bc4f2401SAlan Stern 	scsi_autopm_get_target(starget);
1407bc4f2401SAlan Stern 	pm_runtime_set_active(&sdev->sdev_gendev);
1408c74f8056SStanley Chu 	if (!sdev->rpm_autosuspend)
1409bc4f2401SAlan Stern 		pm_runtime_forbid(&sdev->sdev_gendev);
1410bc4f2401SAlan Stern 	pm_runtime_enable(&sdev->sdev_gendev);
1411bc4f2401SAlan Stern 	scsi_autopm_put_target(starget);
1412bc4f2401SAlan Stern 
1413bc4f2401SAlan Stern 	scsi_autopm_get_device(sdev);
1414bc4f2401SAlan Stern 
14152930f817SHannes Reinecke 	scsi_dh_add_device(sdev);
1416086b91d0SChristoph Hellwig 
14174cd2459cSHannes Reinecke 	error = device_add(&sdev->sdev_gendev);
14184cd2459cSHannes Reinecke 	if (error) {
14194cd2459cSHannes Reinecke 		sdev_printk(KERN_INFO, sdev,
14204cd2459cSHannes Reinecke 				"failed to add device: %d\n", error);
14214cd2459cSHannes Reinecke 		return error;
14224cd2459cSHannes Reinecke 	}
14234cd2459cSHannes Reinecke 
14244cb077d9SRafael J. Wysocki 	device_enable_async_suspend(&sdev->sdev_dev);
1425ee959b00STony Jones 	error = device_add(&sdev->sdev_dev);
14261da177e4SLinus Torvalds 	if (error) {
142773d8c34fSAlan Stern 		sdev_printk(KERN_INFO, sdev,
142873d8c34fSAlan Stern 				"failed to add class device: %d\n", error);
1429860dc736SJames Bottomley 		device_del(&sdev->sdev_gendev);
1430ee37e09dSAlan Stern 		return error;
14311da177e4SLinus Torvalds 	}
1432860dc736SJames Bottomley 	transport_add_device(&sdev->sdev_gendev);
1433860dc736SJames Bottomley 	sdev->is_visible = 1;
14341da177e4SLinus Torvalds 
1435ead09dd3SChristoph Hellwig 	if (IS_ENABLED(CONFIG_BLK_DEV_BSG)) {
1436ead09dd3SChristoph Hellwig 		sdev->bsg_dev = scsi_bsg_register_queue(sdev);
1437ead09dd3SChristoph Hellwig 		if (IS_ERR(sdev->bsg_dev)) {
14385f7cf82cSJackie Liu 			error = PTR_ERR(sdev->bsg_dev);
143980ed71ceSJames Bottomley 			sdev_printk(KERN_INFO, sdev,
1440ead09dd3SChristoph Hellwig 				    "Failed to register bsg queue, errno=%d\n",
1441ead09dd3SChristoph Hellwig 				    error);
1442ead09dd3SChristoph Hellwig 			sdev->bsg_dev = NULL;
1443ead09dd3SChristoph Hellwig 		}
1444ead09dd3SChristoph Hellwig 	}
144580ed71ceSJames Bottomley 
14466fe8c1dbSSubhash Jadavani 	scsi_autopm_put_device(sdev);
1447ee37e09dSAlan Stern 	return error;
14481da177e4SLinus Torvalds }
14491da177e4SLinus Torvalds 
__scsi_remove_device(struct scsi_device * sdev)1450903f4fedSAlan Stern void __scsi_remove_device(struct scsi_device *sdev)
14511da177e4SLinus Torvalds {
1452df133c21SJames Bottomley 	struct device *dev = &sdev->sdev_gendev;
14530db6ca8aSBart Van Assche 	int res;
1454df133c21SJames Bottomley 
1455be821fd8SVitaly Kuznetsov 	/*
1456be821fd8SVitaly Kuznetsov 	 * This cleanup path is not reentrant and while it is impossible
1457be821fd8SVitaly Kuznetsov 	 * to get a new reference with scsi_device_get() someone can still
1458be821fd8SVitaly Kuznetsov 	 * hold a previously acquired one.
1459be821fd8SVitaly Kuznetsov 	 */
1460be821fd8SVitaly Kuznetsov 	if (sdev->sdev_state == SDEV_DEL)
1461be821fd8SVitaly Kuznetsov 		return;
1462be821fd8SVitaly Kuznetsov 
1463860dc736SJames Bottomley 	if (sdev->is_visible) {
14640db6ca8aSBart Van Assche 		/*
14650db6ca8aSBart Van Assche 		 * If scsi_internal_target_block() is running concurrently,
14660db6ca8aSBart Van Assche 		 * wait until it has finished before changing the device state.
14670db6ca8aSBart Van Assche 		 */
14680db6ca8aSBart Van Assche 		mutex_lock(&sdev->state_mutex);
1469255ee932SBart Van Assche 		/*
1470255ee932SBart Van Assche 		 * If blocked, we go straight to DEL and restart the queue so
1471255ee932SBart Van Assche 		 * any commands issued during driver shutdown (like sync
1472255ee932SBart Van Assche 		 * cache) are errored immediately.
1473255ee932SBart Van Assche 		 */
14740db6ca8aSBart Van Assche 		res = scsi_device_set_state(sdev, SDEV_CANCEL);
1475255ee932SBart Van Assche 		if (res != 0) {
1476255ee932SBart Van Assche 			res = scsi_device_set_state(sdev, SDEV_DEL);
1477255ee932SBart Van Assche 			if (res == 0)
1478255ee932SBart Van Assche 				scsi_start_queue(sdev);
1479255ee932SBart Van Assche 		}
14800db6ca8aSBart Van Assche 		mutex_unlock(&sdev->state_mutex);
14810db6ca8aSBart Van Assche 
14820db6ca8aSBart Van Assche 		if (res != 0)
1483903f4fedSAlan Stern 			return;
14841da177e4SLinus Torvalds 
1485ead09dd3SChristoph Hellwig 		if (IS_ENABLED(CONFIG_BLK_DEV_BSG) && sdev->bsg_dev)
1486ead09dd3SChristoph Hellwig 			bsg_unregister_queue(sdev->bsg_dev);
1487ee959b00STony Jones 		device_unregister(&sdev->sdev_dev);
1488df133c21SJames Bottomley 		transport_remove_device(dev);
1489e619e6cbSBart Van Assche 		device_del(dev);
1490e619e6cbSBart Van Assche 	} else
1491e619e6cbSBart Van Assche 		put_device(&sdev->sdev_dev);
1492b485462aSBart Van Assche 
1493b485462aSBart Van Assche 	/*
1494b485462aSBart Van Assche 	 * Stop accepting new requests and wait until all queuecommand() and
1495b485462aSBart Van Assche 	 * scsi_run_queue() invocations have finished before tearing down the
1496b485462aSBart Van Assche 	 * device.
1497b485462aSBart Van Assche 	 */
14980db6ca8aSBart Van Assche 	mutex_lock(&sdev->state_mutex);
14991da177e4SLinus Torvalds 	scsi_device_set_state(sdev, SDEV_DEL);
15000db6ca8aSBart Van Assche 	mutex_unlock(&sdev->state_mutex);
15010db6ca8aSBart Van Assche 
15026f8191fdSChristoph Hellwig 	blk_mq_destroy_queue(sdev->request_queue);
15038fe4ce58SBart Van Assche 	kref_put(&sdev->host->tagset_refcnt, scsi_mq_free_tags);
1504b485462aSBart Van Assche 	cancel_work_sync(&sdev->requeue_work);
1505b485462aSBart Van Assche 
15061da177e4SLinus Torvalds 	if (sdev->host->hostt->slave_destroy)
15071da177e4SLinus Torvalds 		sdev->host->hostt->slave_destroy(sdev);
1508df133c21SJames Bottomley 	transport_destroy_device(dev);
150986cbfb56SJames Bottomley 
1510e63ed0d7SJames Bottomley 	/*
1511e63ed0d7SJames Bottomley 	 * Paired with the kref_get() in scsi_sysfs_initialize().  We have
1512eee8910fSdudengke 	 * removed sysfs visibility from the device, so make the target
1513e63ed0d7SJames Bottomley 	 * invisible if this was the last device underneath it.
1514e63ed0d7SJames Bottomley 	 */
1515e63ed0d7SJames Bottomley 	scsi_target_reap(scsi_target(sdev));
1516e63ed0d7SJames Bottomley 
1517df133c21SJames Bottomley 	put_device(dev);
1518903f4fedSAlan Stern }
1519903f4fedSAlan Stern 
1520903f4fedSAlan Stern /**
1521903f4fedSAlan Stern  * scsi_remove_device - unregister a device from the scsi bus
1522903f4fedSAlan Stern  * @sdev:	scsi_device to unregister
1523903f4fedSAlan Stern  **/
scsi_remove_device(struct scsi_device * sdev)1524903f4fedSAlan Stern void scsi_remove_device(struct scsi_device *sdev)
1525903f4fedSAlan Stern {
152654195002SAlan Stern 	struct Scsi_Host *shost = sdev->host;
152754195002SAlan Stern 
15280b950672SArjan van de Ven 	mutex_lock(&shost->scan_mutex);
1529903f4fedSAlan Stern 	__scsi_remove_device(sdev);
15300b950672SArjan van de Ven 	mutex_unlock(&shost->scan_mutex);
15311da177e4SLinus Torvalds }
15321da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_remove_device);
15331da177e4SLinus Torvalds 
__scsi_remove_target(struct scsi_target * starget)153444818efbSAdrian Bunk static void __scsi_remove_target(struct scsi_target *starget)
15351da177e4SLinus Torvalds {
15361da177e4SLinus Torvalds 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
15371da177e4SLinus Torvalds 	unsigned long flags;
1538a64358dbSAlan Stern 	struct scsi_device *sdev;
15391da177e4SLinus Torvalds 
15401da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
1541a64358dbSAlan Stern  restart:
1542a64358dbSAlan Stern 	list_for_each_entry(sdev, &shost->__devices, siblings) {
1543fbce4d97SHannes Reinecke 		/*
1544fbce4d97SHannes Reinecke 		 * We cannot call scsi_device_get() here, as
1545fbce4d97SHannes Reinecke 		 * we might've been called from rmmod() causing
1546fbce4d97SHannes Reinecke 		 * scsi_device_get() to fail the module_is_live()
1547fbce4d97SHannes Reinecke 		 * check.
1548fbce4d97SHannes Reinecke 		 */
15491da177e4SLinus Torvalds 		if (sdev->channel != starget->channel ||
155081b6c999SHannes Reinecke 		    sdev->id != starget->id)
155181b6c999SHannes Reinecke 			continue;
155281b6c999SHannes Reinecke 		if (sdev->sdev_state == SDEV_DEL ||
155381b6c999SHannes Reinecke 		    sdev->sdev_state == SDEV_CANCEL ||
1554fbce4d97SHannes Reinecke 		    !get_device(&sdev->sdev_gendev))
15551da177e4SLinus Torvalds 			continue;
15561da177e4SLinus Torvalds 		spin_unlock_irqrestore(shost->host_lock, flags);
15571da177e4SLinus Torvalds 		scsi_remove_device(sdev);
1558fbce4d97SHannes Reinecke 		put_device(&sdev->sdev_gendev);
15591da177e4SLinus Torvalds 		spin_lock_irqsave(shost->host_lock, flags);
1560a64358dbSAlan Stern 		goto restart;
15611da177e4SLinus Torvalds 	}
15621da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
156320b1e674Smochel@digitalimplant.org }
156420b1e674Smochel@digitalimplant.org 
15651da177e4SLinus Torvalds /**
15661da177e4SLinus Torvalds  * scsi_remove_target - try to remove a target and all its devices
15671da177e4SLinus Torvalds  * @dev: generic starget or parent of generic stargets to be removed
15681da177e4SLinus Torvalds  *
15691da177e4SLinus Torvalds  * Note: This is slightly racy.  It is possible that if the user
15701da177e4SLinus Torvalds  * requests the addition of another device then the target won't be
15711da177e4SLinus Torvalds  * removed.
15721da177e4SLinus Torvalds  */
scsi_remove_target(struct device * dev)15731da177e4SLinus Torvalds void scsi_remove_target(struct device *dev)
15741da177e4SLinus Torvalds {
15753b661a92SDan Williams 	struct Scsi_Host *shost = dev_to_shost(dev->parent);
1576305c2e71SJohannes Thumshirn 	struct scsi_target *starget;
15773b661a92SDan Williams 	unsigned long flags;
15781da177e4SLinus Torvalds 
157940998193SChristoph Hellwig restart:
15803b661a92SDan Williams 	spin_lock_irqsave(shost->host_lock, flags);
15813b661a92SDan Williams 	list_for_each_entry(starget, &shost->__targets, siblings) {
158290a88d6eSJames Bottomley 		if (starget->state == STARGET_DEL ||
1583f9279c96SEwan D. Milne 		    starget->state == STARGET_REMOVE ||
1584f9279c96SEwan D. Milne 		    starget->state == STARGET_CREATED_REMOVE)
15853b661a92SDan Williams 			continue;
15863b661a92SDan Williams 		if (starget->dev.parent == dev || &starget->dev == dev) {
1587e63ed0d7SJames Bottomley 			kref_get(&starget->reap_ref);
1588f9279c96SEwan D. Milne 			if (starget->state == STARGET_CREATED)
1589f9279c96SEwan D. Milne 				starget->state = STARGET_CREATED_REMOVE;
1590f9279c96SEwan D. Milne 			else
1591f05795d3SJohannes Thumshirn 				starget->state = STARGET_REMOVE;
1592bc3f02a7SDan Williams 			spin_unlock_irqrestore(shost->host_lock, flags);
1593bc3f02a7SDan Williams 			__scsi_remove_target(starget);
159440998193SChristoph Hellwig 			scsi_target_reap(starget);
159540998193SChristoph Hellwig 			goto restart;
15963b661a92SDan Williams 		}
15973b661a92SDan Williams 	}
15983b661a92SDan Williams 	spin_unlock_irqrestore(shost->host_lock, flags);
15991da177e4SLinus Torvalds }
16001da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_remove_target);
16011da177e4SLinus Torvalds 
scsi_register_driver(struct device_driver * drv)16021da177e4SLinus Torvalds int scsi_register_driver(struct device_driver *drv)
16031da177e4SLinus Torvalds {
16041da177e4SLinus Torvalds 	drv->bus = &scsi_bus_type;
16051da177e4SLinus Torvalds 
16061da177e4SLinus Torvalds 	return driver_register(drv);
16071da177e4SLinus Torvalds }
16081da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_register_driver);
16091da177e4SLinus Torvalds 
scsi_register_interface(struct class_interface * intf)16101da177e4SLinus Torvalds int scsi_register_interface(struct class_interface *intf)
16111da177e4SLinus Torvalds {
16121da177e4SLinus Torvalds 	intf->class = &sdev_class;
16131da177e4SLinus Torvalds 
16141da177e4SLinus Torvalds 	return class_interface_register(intf);
16151da177e4SLinus Torvalds }
16161da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_register_interface);
16171da177e4SLinus Torvalds 
16181da177e4SLinus Torvalds /**
16191da177e4SLinus Torvalds  * scsi_sysfs_add_host - add scsi host to subsystem
16201da177e4SLinus Torvalds  * @shost:     scsi host struct to add to subsystem
16211da177e4SLinus Torvalds  **/
scsi_sysfs_add_host(struct Scsi_Host * shost)16221da177e4SLinus Torvalds int scsi_sysfs_add_host(struct Scsi_Host *shost)
16231da177e4SLinus Torvalds {
16241da177e4SLinus Torvalds 	transport_register_device(&shost->shost_gendev);
1625d52b3815SJames Bottomley 	transport_configure_device(&shost->shost_gendev);
16261da177e4SLinus Torvalds 	return 0;
16271da177e4SLinus Torvalds }
16281da177e4SLinus Torvalds 
1629bfd12944SKay Sievers static struct device_type scsi_dev_type = {
1630bfd12944SKay Sievers 	.name =		"scsi_device",
1631bfd12944SKay Sievers 	.release =	scsi_device_dev_release,
1632bfd12944SKay Sievers 	.groups =	scsi_sdev_attr_groups,
1633bfd12944SKay Sievers };
1634bfd12944SKay Sievers 
scsi_sysfs_device_initialize(struct scsi_device * sdev)16351da177e4SLinus Torvalds void scsi_sysfs_device_initialize(struct scsi_device *sdev)
16361da177e4SLinus Torvalds {
16371da177e4SLinus Torvalds 	unsigned long flags;
16381da177e4SLinus Torvalds 	struct Scsi_Host *shost = sdev->host;
163931435de9SBart Van Assche 	const struct scsi_host_template *hostt = shost->hostt;
16401da177e4SLinus Torvalds 	struct scsi_target  *starget = sdev->sdev_target;
16411da177e4SLinus Torvalds 
16421da177e4SLinus Torvalds 	device_initialize(&sdev->sdev_gendev);
16431da177e4SLinus Torvalds 	sdev->sdev_gendev.bus = &scsi_bus_type;
1644bfd12944SKay Sievers 	sdev->sdev_gendev.type = &scsi_dev_type;
1645a19a93e4SBart Van Assche 	scsi_enable_async_suspend(&sdev->sdev_gendev);
16469cb78c16SHannes Reinecke 	dev_set_name(&sdev->sdev_gendev, "%d:%d:%d:%llu",
164771610f55SKay Sievers 		     sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
16483a71f0f7SSteffen Maier 	sdev->sdev_gendev.groups = hostt->sdev_groups;
16491da177e4SLinus Torvalds 
1650ee959b00STony Jones 	device_initialize(&sdev->sdev_dev);
165137e6ba00SJames Bottomley 	sdev->sdev_dev.parent = get_device(&sdev->sdev_gendev);
1652ee959b00STony Jones 	sdev->sdev_dev.class = &sdev_class;
16539cb78c16SHannes Reinecke 	dev_set_name(&sdev->sdev_dev, "%d:%d:%d:%llu",
165471610f55SKay Sievers 		     sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
165550c4e964SAlan Stern 	/*
165650c4e964SAlan Stern 	 * Get a default scsi_level from the target (derived from sibling
165750c4e964SAlan Stern 	 * devices).  This is the best we can do for guessing how to set
165850c4e964SAlan Stern 	 * sdev->lun_in_cdb for the initial INQUIRY command.  For LUN 0 the
165950c4e964SAlan Stern 	 * setting doesn't matter, because all the bits are zero anyway.
166050c4e964SAlan Stern 	 * But it does matter for higher LUNs.
166150c4e964SAlan Stern 	 */
16627c9d6f16SAlan Stern 	sdev->scsi_level = starget->scsi_level;
166350c4e964SAlan Stern 	if (sdev->scsi_level <= SCSI_2 &&
166450c4e964SAlan Stern 			sdev->scsi_level != SCSI_UNKNOWN &&
166550c4e964SAlan Stern 			!shost->no_scsi2_lun_in_cdb)
166650c4e964SAlan Stern 		sdev->lun_in_cdb = 1;
166750c4e964SAlan Stern 
16681da177e4SLinus Torvalds 	transport_setup_device(&sdev->sdev_gendev);
16691da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
16701da177e4SLinus Torvalds 	list_add_tail(&sdev->same_target_siblings, &starget->devices);
16711da177e4SLinus Torvalds 	list_add_tail(&sdev->siblings, &shost->__devices);
16721da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
1673e63ed0d7SJames Bottomley 	/*
1674e63ed0d7SJames Bottomley 	 * device can now only be removed via __scsi_remove_device() so hold
1675e63ed0d7SJames Bottomley 	 * the target.  Target will be held in CREATED state until something
1676e63ed0d7SJames Bottomley 	 * beneath it becomes visible (in which case it moves to RUNNING)
1677e63ed0d7SJames Bottomley 	 */
1678e63ed0d7SJames Bottomley 	kref_get(&starget->reap_ref);
16791da177e4SLinus Torvalds }
16801da177e4SLinus Torvalds 
scsi_is_sdev_device(const struct device * dev)16811da177e4SLinus Torvalds int scsi_is_sdev_device(const struct device *dev)
16821da177e4SLinus Torvalds {
168313ba9bcbSKay Sievers 	return dev->type == &scsi_dev_type;
16841da177e4SLinus Torvalds }
16851da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_is_sdev_device);
16861da177e4SLinus Torvalds 
16871da177e4SLinus Torvalds /* A blank transport template that is used in drivers that don't
16881da177e4SLinus Torvalds  * yet implement Transport Attributes */
16891da177e4SLinus Torvalds struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };
1690