xref: /openbmc/linux/drivers/scsi/scsi_sysfs.c (revision 2542fc9578d49b3b376231269d4c7c7d4cffd62a)
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 
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 };
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 
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 
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 
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
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
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
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
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
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
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 
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
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);
29929443691SVikas Chaudhary 	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
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
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
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
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
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 
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 
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 
454*2542fc95SBart Van Assche 	might_sleep();
455*2542fc95SBart 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 */
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 
5397eff2e7aSKay Sievers static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
540d7b8bcb0SMichael Tokarev {
5411f42ea7bSJames Bottomley 	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 
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 
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");
6731da177e4SLinus Torvalds 
67471e75c97SChristoph Hellwig static ssize_t
67571e75c97SChristoph Hellwig sdev_show_device_busy(struct device *dev, struct device_attribute *attr,
67671e75c97SChristoph Hellwig 		char *buf)
67771e75c97SChristoph Hellwig {
67871e75c97SChristoph Hellwig 	struct scsi_device *sdev = to_scsi_device(dev);
6798278807aSMing Lei 	return snprintf(buf, 20, "%d\n", scsi_device_busy(sdev));
68071e75c97SChristoph Hellwig }
68171e75c97SChristoph Hellwig static DEVICE_ATTR(device_busy, S_IRUGO, sdev_show_device_busy, NULL);
68271e75c97SChristoph Hellwig 
683cd9070c9SChristoph Hellwig static ssize_t
684cd9070c9SChristoph Hellwig sdev_show_device_blocked(struct device *dev, struct device_attribute *attr,
685cd9070c9SChristoph Hellwig 		char *buf)
686cd9070c9SChristoph Hellwig {
687cd9070c9SChristoph Hellwig 	struct scsi_device *sdev = to_scsi_device(dev);
688cd9070c9SChristoph Hellwig 	return snprintf(buf, 20, "%d\n", atomic_read(&sdev->device_blocked));
689cd9070c9SChristoph Hellwig }
690cd9070c9SChristoph Hellwig static DEVICE_ATTR(device_blocked, S_IRUGO, sdev_show_device_blocked, NULL);
691cd9070c9SChristoph Hellwig 
692242f9dcbSJens Axboe /*
693242f9dcbSJens Axboe  * TODO: can we make these symlinks to the block layer ones?
694242f9dcbSJens Axboe  */
6951da177e4SLinus Torvalds static ssize_t
69610523b3bSYani Ioannou sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
6971da177e4SLinus Torvalds {
6981da177e4SLinus Torvalds 	struct scsi_device *sdev;
6991da177e4SLinus Torvalds 	sdev = to_scsi_device(dev);
700242f9dcbSJens Axboe 	return snprintf(buf, 20, "%d\n", sdev->request_queue->rq_timeout / HZ);
7011da177e4SLinus Torvalds }
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds static ssize_t
704ee959b00STony Jones sdev_store_timeout (struct device *dev, struct device_attribute *attr,
705ee959b00STony Jones 		    const char *buf, size_t count)
7061da177e4SLinus Torvalds {
7071da177e4SLinus Torvalds 	struct scsi_device *sdev;
7081da177e4SLinus Torvalds 	int timeout;
7091da177e4SLinus Torvalds 	sdev = to_scsi_device(dev);
7101da177e4SLinus Torvalds 	sscanf (buf, "%d\n", &timeout);
711242f9dcbSJens Axboe 	blk_queue_rq_timeout(sdev->request_queue, timeout * HZ);
7121da177e4SLinus Torvalds 	return count;
7131da177e4SLinus Torvalds }
7141da177e4SLinus Torvalds static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds static ssize_t
7170816c925SMartin K. Petersen sdev_show_eh_timeout(struct device *dev, struct device_attribute *attr, char *buf)
7180816c925SMartin K. Petersen {
7190816c925SMartin K. Petersen 	struct scsi_device *sdev;
7200816c925SMartin K. Petersen 	sdev = to_scsi_device(dev);
7210816c925SMartin K. Petersen 	return snprintf(buf, 20, "%u\n", sdev->eh_timeout / HZ);
7220816c925SMartin K. Petersen }
7230816c925SMartin K. Petersen 
7240816c925SMartin K. Petersen static ssize_t
7250816c925SMartin K. Petersen sdev_store_eh_timeout(struct device *dev, struct device_attribute *attr,
7260816c925SMartin K. Petersen 		    const char *buf, size_t count)
7270816c925SMartin K. Petersen {
7280816c925SMartin K. Petersen 	struct scsi_device *sdev;
7290816c925SMartin K. Petersen 	unsigned int eh_timeout;
7300816c925SMartin K. Petersen 	int err;
7310816c925SMartin K. Petersen 
7320816c925SMartin K. Petersen 	if (!capable(CAP_SYS_ADMIN))
7330816c925SMartin K. Petersen 		return -EACCES;
7340816c925SMartin K. Petersen 
7350816c925SMartin K. Petersen 	sdev = to_scsi_device(dev);
7360816c925SMartin K. Petersen 	err = kstrtouint(buf, 10, &eh_timeout);
7370816c925SMartin K. Petersen 	if (err)
7380816c925SMartin K. Petersen 		return err;
7390816c925SMartin K. Petersen 	sdev->eh_timeout = eh_timeout * HZ;
7400816c925SMartin K. Petersen 
7410816c925SMartin K. Petersen 	return count;
7420816c925SMartin K. Petersen }
7430816c925SMartin K. Petersen static DEVICE_ATTR(eh_timeout, S_IRUGO | S_IWUSR, sdev_show_eh_timeout, sdev_store_eh_timeout);
7440816c925SMartin K. Petersen 
7450816c925SMartin K. Petersen static ssize_t
746ee959b00STony Jones store_rescan_field (struct device *dev, struct device_attribute *attr,
747ee959b00STony Jones 		    const char *buf, size_t count)
7481da177e4SLinus Torvalds {
7491da177e4SLinus Torvalds 	scsi_rescan_device(dev);
7501da177e4SLinus Torvalds 	return count;
7511da177e4SLinus Torvalds }
7521da177e4SLinus Torvalds static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
7531da177e4SLinus Torvalds 
754ee959b00STony Jones static ssize_t
755ee959b00STony Jones sdev_store_delete(struct device *dev, struct device_attribute *attr,
756ee959b00STony Jones 		  const char *buf, size_t count)
7571da177e4SLinus Torvalds {
7580ee223b2SBart Van Assche 	struct kernfs_node *kn;
75977c30128SYufen Yu 	struct scsi_device *sdev = to_scsi_device(dev);
76077c30128SYufen Yu 
76177c30128SYufen Yu 	/*
76277c30128SYufen Yu 	 * We need to try to get module, avoiding the module been removed
76377c30128SYufen Yu 	 * during delete.
76477c30128SYufen Yu 	 */
76577c30128SYufen Yu 	if (scsi_device_get(sdev))
76677c30128SYufen Yu 		return -ENODEV;
7670ee223b2SBart Van Assche 
7680ee223b2SBart Van Assche 	kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
7690ee223b2SBart Van Assche 	WARN_ON_ONCE(!kn);
7700ee223b2SBart Van Assche 	/*
7710ee223b2SBart Van Assche 	 * Concurrent writes into the "delete" sysfs attribute may trigger
7720ee223b2SBart Van Assche 	 * concurrent calls to device_remove_file() and scsi_remove_device().
7730ee223b2SBart Van Assche 	 * device_remove_file() handles concurrent removal calls by
7740ee223b2SBart Van Assche 	 * serializing these and by ignoring the second and later removal
7750ee223b2SBart Van Assche 	 * attempts.  Concurrent calls of scsi_remove_device() are
7760ee223b2SBart Van Assche 	 * serialized. The second and later calls of scsi_remove_device() are
7770ee223b2SBart Van Assche 	 * ignored because the first call of that function changes the device
7780ee223b2SBart Van Assche 	 * state into SDEV_DEL.
7790ee223b2SBart Van Assche 	 */
7800ee223b2SBart Van Assche 	device_remove_file(dev, attr);
78177c30128SYufen Yu 	scsi_remove_device(sdev);
7820ee223b2SBart Van Assche 	if (kn)
7830ee223b2SBart Van Assche 		sysfs_unbreak_active_protection(kn);
78477c30128SYufen Yu 	scsi_device_put(sdev);
7851da177e4SLinus Torvalds 	return count;
7861da177e4SLinus Torvalds };
7871da177e4SLinus Torvalds static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds static ssize_t
790ee959b00STony Jones store_state_field(struct device *dev, struct device_attribute *attr,
791ee959b00STony Jones 		  const char *buf, size_t count)
7921da177e4SLinus Torvalds {
7930db6ca8aSBart Van Assche 	int i, ret;
7941da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
7951da177e4SLinus Torvalds 	enum scsi_device_state state = 0;
7964edd8cd4SMike Christie 	bool rescan_dev = false;
7971da177e4SLinus Torvalds 
7986391a113STobias Klauser 	for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
7991da177e4SLinus Torvalds 		const int len = strlen(sdev_states[i].name);
8001da177e4SLinus Torvalds 		if (strncmp(sdev_states[i].name, buf, len) == 0 &&
8011da177e4SLinus Torvalds 		   buf[len] == '\n') {
8021da177e4SLinus Torvalds 			state = sdev_states[i].value;
8031da177e4SLinus Torvalds 			break;
8041da177e4SLinus Torvalds 		}
8051da177e4SLinus Torvalds 	}
806ac88c1f6SBart Van Assche 	switch (state) {
807ac88c1f6SBart Van Assche 	case SDEV_RUNNING:
808ac88c1f6SBart Van Assche 	case SDEV_OFFLINE:
809ac88c1f6SBart Van Assche 		break;
810ac88c1f6SBart Van Assche 	default:
8111da177e4SLinus Torvalds 		return -EINVAL;
812ac88c1f6SBart Van Assche 	}
8131da177e4SLinus Torvalds 
8140db6ca8aSBart Van Assche 	mutex_lock(&sdev->state_mutex);
8152331ce61SUday Shankar 	switch (sdev->sdev_state) {
8162331ce61SUday Shankar 	case SDEV_RUNNING:
8172331ce61SUday Shankar 	case SDEV_OFFLINE:
8182331ce61SUday Shankar 		break;
8192331ce61SUday Shankar 	default:
8202331ce61SUday Shankar 		mutex_unlock(&sdev->state_mutex);
8212331ce61SUday Shankar 		return -EINVAL;
8222331ce61SUday Shankar 	}
8234edd8cd4SMike Christie 	if (sdev->sdev_state == SDEV_RUNNING && state == SDEV_RUNNING) {
824eb97545dSMike Christie 		ret = 0;
8254edd8cd4SMike Christie 	} else {
8260db6ca8aSBart Van Assche 		ret = scsi_device_set_state(sdev, state);
8274edd8cd4SMike Christie 		if (ret == 0 && state == SDEV_RUNNING)
8284edd8cd4SMike Christie 			rescan_dev = true;
8294edd8cd4SMike Christie 	}
8304edd8cd4SMike Christie 	mutex_unlock(&sdev->state_mutex);
8314edd8cd4SMike Christie 
8324edd8cd4SMike Christie 	if (rescan_dev) {
83370fc085cSzhengbin 		/*
834f0f82e24Slijinlin 		 * If the device state changes to SDEV_RUNNING, we need to
83502c6dcd5SLi Jinlin 		 * run the queue to avoid I/O hang, and rescan the device
83602c6dcd5SLi Jinlin 		 * to revalidate it. Running the queue first is necessary
83702c6dcd5SLi Jinlin 		 * because another thread may be waiting inside
83802c6dcd5SLi Jinlin 		 * blk_mq_freeze_queue_wait() and because that call may be
83902c6dcd5SLi Jinlin 		 * waiting for pending I/O to finish.
84070fc085cSzhengbin 		 */
84170fc085cSzhengbin 		blk_mq_run_hw_queues(sdev->request_queue, true);
84202c6dcd5SLi Jinlin 		scsi_rescan_device(dev);
843f0f82e24Slijinlin 	}
8440db6ca8aSBart Van Assche 
8450db6ca8aSBart Van Assche 	return ret == 0 ? count : -EINVAL;
8461da177e4SLinus Torvalds }
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds static ssize_t
84910523b3bSYani Ioannou show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
8501da177e4SLinus Torvalds {
8511da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
8521da177e4SLinus Torvalds 	const char *name = scsi_device_state_name(sdev->sdev_state);
8531da177e4SLinus Torvalds 
8541da177e4SLinus Torvalds 	if (!name)
8551da177e4SLinus Torvalds 		return -EINVAL;
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds 	return snprintf(buf, 20, "%s\n", name);
8581da177e4SLinus Torvalds }
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds static ssize_t
863ee959b00STony Jones show_queue_type_field(struct device *dev, struct device_attribute *attr,
864ee959b00STony Jones 		      char *buf)
8651da177e4SLinus Torvalds {
8661da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
8671da177e4SLinus Torvalds 	const char *name = "none";
8681da177e4SLinus Torvalds 
869609aa22fSChristoph Hellwig 	if (sdev->simple_tags)
8701da177e4SLinus Torvalds 		name = "simple";
8711da177e4SLinus Torvalds 
8721da177e4SLinus Torvalds 	return snprintf(buf, 20, "%s\n", name);
8731da177e4SLinus Torvalds }
8741da177e4SLinus Torvalds 
875276b20d0SHannes Reinecke static ssize_t
876276b20d0SHannes Reinecke store_queue_type_field(struct device *dev, struct device_attribute *attr,
877276b20d0SHannes Reinecke 		       const char *buf, size_t count)
878276b20d0SHannes Reinecke {
879276b20d0SHannes Reinecke 	struct scsi_device *sdev = to_scsi_device(dev);
880276b20d0SHannes Reinecke 
881efc3c1dfSChristoph Hellwig 	if (!sdev->tagged_supported)
882276b20d0SHannes Reinecke 		return -EINVAL;
883276b20d0SHannes Reinecke 
884efc3c1dfSChristoph Hellwig 	sdev_printk(KERN_INFO, sdev,
885efc3c1dfSChristoph Hellwig 		    "ignoring write to deprecated queue_type attribute");
886276b20d0SHannes Reinecke 	return count;
887276b20d0SHannes Reinecke }
888276b20d0SHannes Reinecke 
889276b20d0SHannes Reinecke static DEVICE_ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
890276b20d0SHannes Reinecke 		   store_queue_type_field);
8911da177e4SLinus Torvalds 
892b3ae8780SHannes Reinecke #define sdev_vpd_pg_attr(_page)						\
893b3ae8780SHannes Reinecke static ssize_t							\
894b3ae8780SHannes Reinecke show_vpd_##_page(struct file *filp, struct kobject *kobj,	\
895b3ae8780SHannes Reinecke 		 struct bin_attribute *bin_attr,			\
896b3ae8780SHannes Reinecke 		 char *buf, loff_t off, size_t count)			\
897b3ae8780SHannes Reinecke {									\
8989e7bd945SGuosong Su 	struct device *dev = kobj_to_dev(kobj);				\
899b3ae8780SHannes Reinecke 	struct scsi_device *sdev = to_scsi_device(dev);			\
900ccf1e004SBart Van Assche 	struct scsi_vpd *vpd_page;					\
901ccf1e004SBart Van Assche 	int ret = -EINVAL;						\
902ccf1e004SBart Van Assche 									\
90309e2b0b1SHannes Reinecke 	rcu_read_lock();						\
904ccf1e004SBart Van Assche 	vpd_page = rcu_dereference(sdev->vpd_##_page);			\
905ccf1e004SBart Van Assche 	if (vpd_page)							\
90609e2b0b1SHannes Reinecke 		ret = memory_read_from_buffer(buf, count, &off,		\
907ccf1e004SBart Van Assche 				vpd_page->data, vpd_page->len);		\
90809e2b0b1SHannes Reinecke 	rcu_read_unlock();						\
90909e2b0b1SHannes Reinecke 	return ret;							\
910b3ae8780SHannes Reinecke }									\
911b3ae8780SHannes Reinecke static struct bin_attribute dev_attr_vpd_##_page = {		\
912b3ae8780SHannes Reinecke 	.attr =	{.name = __stringify(vpd_##_page), .mode = S_IRUGO },	\
913b3ae8780SHannes Reinecke 	.size = 0,							\
914b3ae8780SHannes Reinecke 	.read = show_vpd_##_page,					\
915b3ae8780SHannes Reinecke };
916b3ae8780SHannes Reinecke 
917b3ae8780SHannes Reinecke sdev_vpd_pg_attr(pg83);
918b3ae8780SHannes Reinecke sdev_vpd_pg_attr(pg80);
919d188b067SRyan Attard sdev_vpd_pg_attr(pg89);
920e60ac0b9SMartin K. Petersen sdev_vpd_pg_attr(pgb0);
921e60ac0b9SMartin K. Petersen sdev_vpd_pg_attr(pgb1);
922e60ac0b9SMartin K. Petersen sdev_vpd_pg_attr(pgb2);
923d188b067SRyan Attard sdev_vpd_pg_attr(pg0);
9241da177e4SLinus Torvalds 
92592e6246cSJohannes Thumshirn static ssize_t show_inquiry(struct file *filep, struct kobject *kobj,
92692e6246cSJohannes Thumshirn 			    struct bin_attribute *bin_attr,
92792e6246cSJohannes Thumshirn 			    char *buf, loff_t off, size_t count)
92892e6246cSJohannes Thumshirn {
9299e7bd945SGuosong Su 	struct device *dev = kobj_to_dev(kobj);
93092e6246cSJohannes Thumshirn 	struct scsi_device *sdev = to_scsi_device(dev);
93192e6246cSJohannes Thumshirn 
93292e6246cSJohannes Thumshirn 	if (!sdev->inquiry)
93392e6246cSJohannes Thumshirn 		return -EINVAL;
93492e6246cSJohannes Thumshirn 
93592e6246cSJohannes Thumshirn 	return memory_read_from_buffer(buf, count, &off, sdev->inquiry,
93692e6246cSJohannes Thumshirn 				       sdev->inquiry_len);
93792e6246cSJohannes Thumshirn }
93892e6246cSJohannes Thumshirn 
93992e6246cSJohannes Thumshirn static struct bin_attribute dev_attr_inquiry = {
94092e6246cSJohannes Thumshirn 	.attr = {
94192e6246cSJohannes Thumshirn 		.name = "inquiry",
94292e6246cSJohannes Thumshirn 		.mode = S_IRUGO,
94392e6246cSJohannes Thumshirn 	},
94492e6246cSJohannes Thumshirn 	.size = 0,
94592e6246cSJohannes Thumshirn 	.read = show_inquiry,
94692e6246cSJohannes Thumshirn };
94792e6246cSJohannes Thumshirn 
9481da177e4SLinus Torvalds static ssize_t
949b3ae8780SHannes Reinecke show_iostat_counterbits(struct device *dev, struct device_attribute *attr,
950b3ae8780SHannes Reinecke 			char *buf)
9511da177e4SLinus Torvalds {
9521da177e4SLinus Torvalds 	return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
9531da177e4SLinus Torvalds }
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds #define show_sdev_iostat(field)						\
9581da177e4SLinus Torvalds static ssize_t								\
959ee959b00STony Jones show_iostat_##field(struct device *dev, struct device_attribute *attr,	\
960ee959b00STony Jones 		    char *buf)						\
9611da177e4SLinus Torvalds {									\
9621da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);			\
9631da177e4SLinus Torvalds 	unsigned long long count = atomic_read(&sdev->field);		\
9641da177e4SLinus Torvalds 	return snprintf(buf, 20, "0x%llx\n", count);			\
9651da177e4SLinus Torvalds }									\
9661da177e4SLinus Torvalds static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds show_sdev_iostat(iorequest_cnt);
9691da177e4SLinus Torvalds show_sdev_iostat(iodone_cnt);
9701da177e4SLinus Torvalds show_sdev_iostat(ioerr_cnt);
97148517eefSWu Bo show_sdev_iostat(iotmo_cnt);
9721da177e4SLinus Torvalds 
973d7b8bcb0SMichael Tokarev static ssize_t
974d7b8bcb0SMichael Tokarev sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
975d7b8bcb0SMichael Tokarev {
976d7b8bcb0SMichael Tokarev 	struct scsi_device *sdev;
977d7b8bcb0SMichael Tokarev 	sdev = to_scsi_device(dev);
978d7b8bcb0SMichael Tokarev 	return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
979d7b8bcb0SMichael Tokarev }
980d7b8bcb0SMichael Tokarev static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
9811da177e4SLinus Torvalds 
982a341cd0fSJeff Garzik #define DECLARE_EVT_SHOW(name, Cap_name)				\
983a341cd0fSJeff Garzik static ssize_t								\
984a341cd0fSJeff Garzik sdev_show_evt_##name(struct device *dev, struct device_attribute *attr,	\
985a341cd0fSJeff Garzik 		     char *buf)						\
986a341cd0fSJeff Garzik {									\
987a341cd0fSJeff Garzik 	struct scsi_device *sdev = to_scsi_device(dev);			\
988a341cd0fSJeff Garzik 	int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
989a341cd0fSJeff Garzik 	return snprintf(buf, 20, "%d\n", val);				\
990a341cd0fSJeff Garzik }
991a341cd0fSJeff Garzik 
992a341cd0fSJeff Garzik #define DECLARE_EVT_STORE(name, Cap_name)				\
993a341cd0fSJeff Garzik static ssize_t								\
994a341cd0fSJeff Garzik sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
995a341cd0fSJeff Garzik 		      const char *buf, size_t count)			\
996a341cd0fSJeff Garzik {									\
997a341cd0fSJeff Garzik 	struct scsi_device *sdev = to_scsi_device(dev);			\
998a341cd0fSJeff Garzik 	int val = simple_strtoul(buf, NULL, 0);				\
999a341cd0fSJeff Garzik 	if (val == 0)							\
1000a341cd0fSJeff Garzik 		clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events);	\
1001a341cd0fSJeff Garzik 	else if (val == 1)						\
1002a341cd0fSJeff Garzik 		set_bit(SDEV_EVT_##Cap_name, sdev->supported_events);	\
1003a341cd0fSJeff Garzik 	else								\
1004a341cd0fSJeff Garzik 		return -EINVAL;						\
1005a341cd0fSJeff Garzik 	return count;							\
1006a341cd0fSJeff Garzik }
1007a341cd0fSJeff Garzik 
1008a341cd0fSJeff Garzik #define DECLARE_EVT(name, Cap_name)					\
1009a341cd0fSJeff Garzik 	DECLARE_EVT_SHOW(name, Cap_name)				\
1010a341cd0fSJeff Garzik 	DECLARE_EVT_STORE(name, Cap_name)				\
1011a341cd0fSJeff Garzik 	static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name,	\
1012a341cd0fSJeff Garzik 			   sdev_store_evt_##name);
1013a341cd0fSJeff Garzik #define REF_EVT(name) &dev_attr_evt_##name.attr
1014a341cd0fSJeff Garzik 
1015a341cd0fSJeff Garzik DECLARE_EVT(media_change, MEDIA_CHANGE)
1016279afdfeSEwan D. Milne DECLARE_EVT(inquiry_change_reported, INQUIRY_CHANGE_REPORTED)
1017279afdfeSEwan D. Milne DECLARE_EVT(capacity_change_reported, CAPACITY_CHANGE_REPORTED)
1018279afdfeSEwan D. Milne DECLARE_EVT(soft_threshold_reached, SOFT_THRESHOLD_REACHED_REPORTED)
1019279afdfeSEwan D. Milne DECLARE_EVT(mode_parameter_change_reported, MODE_PARAMETER_CHANGE_REPORTED)
1020279afdfeSEwan D. Milne DECLARE_EVT(lun_change_reported, LUN_CHANGE_REPORTED)
1021a341cd0fSJeff Garzik 
1022ee959b00STony Jones static ssize_t
1023276b20d0SHannes Reinecke sdev_store_queue_depth(struct device *dev, struct device_attribute *attr,
1024ee959b00STony Jones 		       const char *buf, size_t count)
10251da177e4SLinus Torvalds {
10261da177e4SLinus Torvalds 	int depth, retval;
10271da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
10281da177e4SLinus Torvalds 	struct scsi_host_template *sht = sdev->host->hostt;
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds 	if (!sht->change_queue_depth)
10311da177e4SLinus Torvalds 		return -EINVAL;
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds 	depth = simple_strtoul(buf, NULL, 0);
10341da177e4SLinus Torvalds 
10351278dd68SJens Axboe 	if (depth < 1 || depth > sdev->host->can_queue)
10361da177e4SLinus Torvalds 		return -EINVAL;
10371da177e4SLinus Torvalds 
1038db5ed4dfSChristoph Hellwig 	retval = sht->change_queue_depth(sdev, depth);
10391da177e4SLinus Torvalds 	if (retval < 0)
10401da177e4SLinus Torvalds 		return retval;
10411da177e4SLinus Torvalds 
10424a84067dSVasu Dev 	sdev->max_queue_depth = sdev->queue_depth;
10434a84067dSVasu Dev 
10441da177e4SLinus Torvalds 	return count;
10451da177e4SLinus Torvalds }
1046276b20d0SHannes Reinecke sdev_show_function(queue_depth, "%d\n");
10471da177e4SLinus Torvalds 
1048276b20d0SHannes Reinecke static DEVICE_ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
1049276b20d0SHannes Reinecke 		   sdev_store_queue_depth);
10501da177e4SLinus Torvalds 
1051248d4fe9SHannes Reinecke static ssize_t
1052248d4fe9SHannes Reinecke sdev_show_wwid(struct device *dev, struct device_attribute *attr,
1053248d4fe9SHannes Reinecke 		    char *buf)
1054248d4fe9SHannes Reinecke {
1055248d4fe9SHannes Reinecke 	struct scsi_device *sdev = to_scsi_device(dev);
1056248d4fe9SHannes Reinecke 	ssize_t count;
1057248d4fe9SHannes Reinecke 
1058248d4fe9SHannes Reinecke 	count = scsi_vpd_lun_id(sdev, buf, PAGE_SIZE);
1059248d4fe9SHannes Reinecke 	if (count > 0) {
1060248d4fe9SHannes Reinecke 		buf[count] = '\n';
1061248d4fe9SHannes Reinecke 		count++;
1062248d4fe9SHannes Reinecke 	}
1063248d4fe9SHannes Reinecke 	return count;
1064248d4fe9SHannes Reinecke }
1065248d4fe9SHannes Reinecke static DEVICE_ATTR(wwid, S_IRUGO, sdev_show_wwid, NULL);
1066248d4fe9SHannes Reinecke 
1067093b8886SBart Van Assche #define BLIST_FLAG_NAME(name)					\
106814098803SMartin Wilck 	[const_ilog2((__force __u64)BLIST_##name)] = #name
1069345e2960SHannes Reinecke static const char *const sdev_bflags_name[] = {
1070345e2960SHannes Reinecke #include "scsi_devinfo_tbl.c"
1071345e2960SHannes Reinecke };
1072345e2960SHannes Reinecke #undef BLIST_FLAG_NAME
1073345e2960SHannes Reinecke 
1074345e2960SHannes Reinecke static ssize_t
1075345e2960SHannes Reinecke sdev_show_blacklist(struct device *dev, struct device_attribute *attr,
1076345e2960SHannes Reinecke 		    char *buf)
1077345e2960SHannes Reinecke {
1078345e2960SHannes Reinecke 	struct scsi_device *sdev = to_scsi_device(dev);
1079345e2960SHannes Reinecke 	int i;
1080345e2960SHannes Reinecke 	ssize_t len = 0;
1081345e2960SHannes Reinecke 
1082345e2960SHannes Reinecke 	for (i = 0; i < sizeof(sdev->sdev_bflags) * BITS_PER_BYTE; i++) {
1083345e2960SHannes Reinecke 		const char *name = NULL;
1084345e2960SHannes Reinecke 
1085093b8886SBart Van Assche 		if (!(sdev->sdev_bflags & (__force blist_flags_t)BIT(i)))
1086345e2960SHannes Reinecke 			continue;
1087345e2960SHannes Reinecke 		if (i < ARRAY_SIZE(sdev_bflags_name) && sdev_bflags_name[i])
1088345e2960SHannes Reinecke 			name = sdev_bflags_name[i];
1089345e2960SHannes Reinecke 
1090345e2960SHannes Reinecke 		if (name)
109181546b32STakashi Iwai 			len += scnprintf(buf + len, PAGE_SIZE - len,
1092345e2960SHannes Reinecke 					 "%s%s", len ? " " : "", name);
1093345e2960SHannes Reinecke 		else
109481546b32STakashi Iwai 			len += scnprintf(buf + len, PAGE_SIZE - len,
1095345e2960SHannes Reinecke 					 "%sINVALID_BIT(%d)", len ? " " : "", i);
1096345e2960SHannes Reinecke 	}
1097345e2960SHannes Reinecke 	if (len)
109881546b32STakashi Iwai 		len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
1099345e2960SHannes Reinecke 	return len;
1100345e2960SHannes Reinecke }
1101345e2960SHannes Reinecke static DEVICE_ATTR(blacklist, S_IRUGO, sdev_show_blacklist, NULL);
1102345e2960SHannes Reinecke 
110341f95dd2SHannes Reinecke #ifdef CONFIG_SCSI_DH
110441f95dd2SHannes Reinecke static ssize_t
110541f95dd2SHannes Reinecke sdev_show_dh_state(struct device *dev, struct device_attribute *attr,
110641f95dd2SHannes Reinecke 		   char *buf)
110741f95dd2SHannes Reinecke {
110841f95dd2SHannes Reinecke 	struct scsi_device *sdev = to_scsi_device(dev);
110941f95dd2SHannes Reinecke 
111041f95dd2SHannes Reinecke 	if (!sdev->handler)
111141f95dd2SHannes Reinecke 		return snprintf(buf, 20, "detached\n");
111241f95dd2SHannes Reinecke 
111341f95dd2SHannes Reinecke 	return snprintf(buf, 20, "%s\n", sdev->handler->name);
111441f95dd2SHannes Reinecke }
111541f95dd2SHannes Reinecke 
111641f95dd2SHannes Reinecke static ssize_t
111741f95dd2SHannes Reinecke sdev_store_dh_state(struct device *dev, struct device_attribute *attr,
111841f95dd2SHannes Reinecke 		    const char *buf, size_t count)
111941f95dd2SHannes Reinecke {
112041f95dd2SHannes Reinecke 	struct scsi_device *sdev = to_scsi_device(dev);
112141f95dd2SHannes Reinecke 	int err = -EINVAL;
112241f95dd2SHannes Reinecke 
112341f95dd2SHannes Reinecke 	if (sdev->sdev_state == SDEV_CANCEL ||
112441f95dd2SHannes Reinecke 	    sdev->sdev_state == SDEV_DEL)
112541f95dd2SHannes Reinecke 		return -ENODEV;
112641f95dd2SHannes Reinecke 
112741f95dd2SHannes Reinecke 	if (!sdev->handler) {
112841f95dd2SHannes Reinecke 		/*
112941f95dd2SHannes Reinecke 		 * Attach to a device handler
113041f95dd2SHannes Reinecke 		 */
113141f95dd2SHannes Reinecke 		err = scsi_dh_attach(sdev->request_queue, buf);
113241f95dd2SHannes Reinecke 	} else if (!strncmp(buf, "activate", 8)) {
113341f95dd2SHannes Reinecke 		/*
113441f95dd2SHannes Reinecke 		 * Activate a device handler
113541f95dd2SHannes Reinecke 		 */
113641f95dd2SHannes Reinecke 		if (sdev->handler->activate)
113741f95dd2SHannes Reinecke 			err = sdev->handler->activate(sdev, NULL, NULL);
113841f95dd2SHannes Reinecke 		else
113941f95dd2SHannes Reinecke 			err = 0;
114041f95dd2SHannes Reinecke 	} else if (!strncmp(buf, "detach", 6)) {
114141f95dd2SHannes Reinecke 		/*
114241f95dd2SHannes Reinecke 		 * Detach from a device handler
114341f95dd2SHannes Reinecke 		 */
114441f95dd2SHannes Reinecke 		sdev_printk(KERN_WARNING, sdev,
114541f95dd2SHannes Reinecke 			    "can't detach handler %s.\n",
114641f95dd2SHannes Reinecke 			    sdev->handler->name);
114741f95dd2SHannes Reinecke 		err = -EINVAL;
114841f95dd2SHannes Reinecke 	}
114941f95dd2SHannes Reinecke 
115041f95dd2SHannes Reinecke 	return err < 0 ? err : count;
115141f95dd2SHannes Reinecke }
115241f95dd2SHannes Reinecke 
115341f95dd2SHannes Reinecke static DEVICE_ATTR(dh_state, S_IRUGO | S_IWUSR, sdev_show_dh_state,
115441f95dd2SHannes Reinecke 		   sdev_store_dh_state);
115577c9df96SHannes Reinecke 
115677c9df96SHannes Reinecke static ssize_t
115777c9df96SHannes Reinecke sdev_show_access_state(struct device *dev,
115877c9df96SHannes Reinecke 		       struct device_attribute *attr,
115977c9df96SHannes Reinecke 		       char *buf)
116077c9df96SHannes Reinecke {
116177c9df96SHannes Reinecke 	struct scsi_device *sdev = to_scsi_device(dev);
116277c9df96SHannes Reinecke 	unsigned char access_state;
116377c9df96SHannes Reinecke 	const char *access_state_name;
116477c9df96SHannes Reinecke 
116577c9df96SHannes Reinecke 	if (!sdev->handler)
116677c9df96SHannes Reinecke 		return -EINVAL;
116777c9df96SHannes Reinecke 
116877c9df96SHannes Reinecke 	access_state = (sdev->access_state & SCSI_ACCESS_STATE_MASK);
116977c9df96SHannes Reinecke 	access_state_name = scsi_access_state_name(access_state);
117077c9df96SHannes Reinecke 
117177c9df96SHannes Reinecke 	return sprintf(buf, "%s\n",
117277c9df96SHannes Reinecke 		       access_state_name ? access_state_name : "unknown");
117377c9df96SHannes Reinecke }
117477c9df96SHannes Reinecke static DEVICE_ATTR(access_state, S_IRUGO, sdev_show_access_state, NULL);
117577c9df96SHannes Reinecke 
117677c9df96SHannes Reinecke static ssize_t
117777c9df96SHannes Reinecke sdev_show_preferred_path(struct device *dev,
117877c9df96SHannes Reinecke 			 struct device_attribute *attr,
117977c9df96SHannes Reinecke 			 char *buf)
118077c9df96SHannes Reinecke {
118177c9df96SHannes Reinecke 	struct scsi_device *sdev = to_scsi_device(dev);
118277c9df96SHannes Reinecke 
118377c9df96SHannes Reinecke 	if (!sdev->handler)
118477c9df96SHannes Reinecke 		return -EINVAL;
118577c9df96SHannes Reinecke 
118677c9df96SHannes Reinecke 	if (sdev->access_state & SCSI_ACCESS_STATE_PREFERRED)
118777c9df96SHannes Reinecke 		return sprintf(buf, "1\n");
118877c9df96SHannes Reinecke 	else
118977c9df96SHannes Reinecke 		return sprintf(buf, "0\n");
119077c9df96SHannes Reinecke }
119177c9df96SHannes Reinecke static DEVICE_ATTR(preferred_path, S_IRUGO, sdev_show_preferred_path, NULL);
119241f95dd2SHannes Reinecke #endif
119341f95dd2SHannes Reinecke 
1194ee959b00STony Jones static ssize_t
11954a84067dSVasu Dev sdev_show_queue_ramp_up_period(struct device *dev,
11964a84067dSVasu Dev 			       struct device_attribute *attr,
11974a84067dSVasu Dev 			       char *buf)
11984a84067dSVasu Dev {
11994a84067dSVasu Dev 	struct scsi_device *sdev;
12004a84067dSVasu Dev 	sdev = to_scsi_device(dev);
12014a84067dSVasu Dev 	return snprintf(buf, 20, "%u\n",
12024a84067dSVasu Dev 			jiffies_to_msecs(sdev->queue_ramp_up_period));
12034a84067dSVasu Dev }
12044a84067dSVasu Dev 
12054a84067dSVasu Dev static ssize_t
12064a84067dSVasu Dev sdev_store_queue_ramp_up_period(struct device *dev,
12074a84067dSVasu Dev 				struct device_attribute *attr,
12084a84067dSVasu Dev 				const char *buf, size_t count)
12094a84067dSVasu Dev {
12104a84067dSVasu Dev 	struct scsi_device *sdev = to_scsi_device(dev);
1211f7c65af5SDaniel Walter 	unsigned int period;
12124a84067dSVasu Dev 
1213f7c65af5SDaniel Walter 	if (kstrtouint(buf, 10, &period))
12144a84067dSVasu Dev 		return -EINVAL;
12154a84067dSVasu Dev 
12164a84067dSVasu Dev 	sdev->queue_ramp_up_period = msecs_to_jiffies(period);
12175cb9b40dSPeter Oberparleiter 	return count;
12184a84067dSVasu Dev }
12194a84067dSVasu Dev 
1220276b20d0SHannes Reinecke static DEVICE_ATTR(queue_ramp_up_period, S_IRUGO | S_IWUSR,
12214a84067dSVasu Dev 		   sdev_show_queue_ramp_up_period,
12224a84067dSVasu Dev 		   sdev_store_queue_ramp_up_period);
12234a84067dSVasu Dev 
1224276b20d0SHannes Reinecke static umode_t scsi_sdev_attr_is_visible(struct kobject *kobj,
1225276b20d0SHannes Reinecke 					 struct attribute *attr, int i)
12261da177e4SLinus Torvalds {
12279e7bd945SGuosong Su 	struct device *dev = kobj_to_dev(kobj);
12281da177e4SLinus Torvalds 	struct scsi_device *sdev = to_scsi_device(dev);
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds 
1231276b20d0SHannes Reinecke 	if (attr == &dev_attr_queue_depth.attr &&
1232276b20d0SHannes Reinecke 	    !sdev->host->hostt->change_queue_depth)
1233276b20d0SHannes Reinecke 		return S_IRUGO;
12341da177e4SLinus Torvalds 
1235276b20d0SHannes Reinecke 	if (attr == &dev_attr_queue_ramp_up_period.attr &&
1236276b20d0SHannes Reinecke 	    !sdev->host->hostt->change_queue_depth)
1237276b20d0SHannes Reinecke 		return 0;
12381da177e4SLinus Torvalds 
1239276b20d0SHannes Reinecke 	return attr->mode;
12401da177e4SLinus Torvalds }
12411da177e4SLinus Torvalds 
12427e47976bSHannes Reinecke static umode_t scsi_sdev_bin_attr_is_visible(struct kobject *kobj,
12437e47976bSHannes Reinecke 					     struct bin_attribute *attr, int i)
12447e47976bSHannes Reinecke {
12459e7bd945SGuosong Su 	struct device *dev = kobj_to_dev(kobj);
12467e47976bSHannes Reinecke 	struct scsi_device *sdev = to_scsi_device(dev);
12477e47976bSHannes Reinecke 
12487e47976bSHannes Reinecke 
1249d188b067SRyan Attard 	if (attr == &dev_attr_vpd_pg0 && !sdev->vpd_pg0)
1250d188b067SRyan Attard 		return 0;
1251d188b067SRyan Attard 
12527e47976bSHannes Reinecke 	if (attr == &dev_attr_vpd_pg80 && !sdev->vpd_pg80)
12537e47976bSHannes Reinecke 		return 0;
12547e47976bSHannes Reinecke 
125568887582SHannes Reinecke 	if (attr == &dev_attr_vpd_pg83 && !sdev->vpd_pg83)
12567e47976bSHannes Reinecke 		return 0;
12577e47976bSHannes Reinecke 
1258d188b067SRyan Attard 	if (attr == &dev_attr_vpd_pg89 && !sdev->vpd_pg89)
1259d188b067SRyan Attard 		return 0;
1260d188b067SRyan Attard 
1261e60ac0b9SMartin K. Petersen 	if (attr == &dev_attr_vpd_pgb0 && !sdev->vpd_pgb0)
1262e60ac0b9SMartin K. Petersen 		return 0;
1263e60ac0b9SMartin K. Petersen 
1264e60ac0b9SMartin K. Petersen 	if (attr == &dev_attr_vpd_pgb1 && !sdev->vpd_pgb1)
1265e60ac0b9SMartin K. Petersen 		return 0;
1266e60ac0b9SMartin K. Petersen 
1267e60ac0b9SMartin K. Petersen 	if (attr == &dev_attr_vpd_pgb2 && !sdev->vpd_pgb2)
1268e60ac0b9SMartin K. Petersen 		return 0;
1269e60ac0b9SMartin K. Petersen 
12707e47976bSHannes Reinecke 	return S_IRUGO;
12717e47976bSHannes Reinecke }
12727e47976bSHannes Reinecke 
1273276b20d0SHannes Reinecke /* Default template for device attributes.  May NOT be modified */
1274276b20d0SHannes Reinecke static struct attribute *scsi_sdev_attrs[] = {
1275276b20d0SHannes Reinecke 	&dev_attr_device_blocked.attr,
1276276b20d0SHannes Reinecke 	&dev_attr_type.attr,
1277276b20d0SHannes Reinecke 	&dev_attr_scsi_level.attr,
1278276b20d0SHannes Reinecke 	&dev_attr_device_busy.attr,
1279276b20d0SHannes Reinecke 	&dev_attr_vendor.attr,
1280276b20d0SHannes Reinecke 	&dev_attr_model.attr,
1281276b20d0SHannes Reinecke 	&dev_attr_rev.attr,
1282276b20d0SHannes Reinecke 	&dev_attr_rescan.attr,
1283276b20d0SHannes Reinecke 	&dev_attr_delete.attr,
1284276b20d0SHannes Reinecke 	&dev_attr_state.attr,
1285276b20d0SHannes Reinecke 	&dev_attr_timeout.attr,
1286276b20d0SHannes Reinecke 	&dev_attr_eh_timeout.attr,
1287276b20d0SHannes Reinecke 	&dev_attr_iocounterbits.attr,
1288276b20d0SHannes Reinecke 	&dev_attr_iorequest_cnt.attr,
1289276b20d0SHannes Reinecke 	&dev_attr_iodone_cnt.attr,
1290276b20d0SHannes Reinecke 	&dev_attr_ioerr_cnt.attr,
129148517eefSWu Bo 	&dev_attr_iotmo_cnt.attr,
1292276b20d0SHannes Reinecke 	&dev_attr_modalias.attr,
1293276b20d0SHannes Reinecke 	&dev_attr_queue_depth.attr,
1294276b20d0SHannes Reinecke 	&dev_attr_queue_type.attr,
1295248d4fe9SHannes Reinecke 	&dev_attr_wwid.attr,
1296345e2960SHannes Reinecke 	&dev_attr_blacklist.attr,
129741f95dd2SHannes Reinecke #ifdef CONFIG_SCSI_DH
129841f95dd2SHannes Reinecke 	&dev_attr_dh_state.attr,
129977c9df96SHannes Reinecke 	&dev_attr_access_state.attr,
130077c9df96SHannes Reinecke 	&dev_attr_preferred_path.attr,
130141f95dd2SHannes Reinecke #endif
1302276b20d0SHannes Reinecke 	&dev_attr_queue_ramp_up_period.attr,
1303276b20d0SHannes Reinecke 	REF_EVT(media_change),
1304276b20d0SHannes Reinecke 	REF_EVT(inquiry_change_reported),
1305276b20d0SHannes Reinecke 	REF_EVT(capacity_change_reported),
1306276b20d0SHannes Reinecke 	REF_EVT(soft_threshold_reached),
1307276b20d0SHannes Reinecke 	REF_EVT(mode_parameter_change_reported),
1308276b20d0SHannes Reinecke 	REF_EVT(lun_change_reported),
1309276b20d0SHannes Reinecke 	NULL
1310276b20d0SHannes Reinecke };
1311276b20d0SHannes Reinecke 
1312b3ae8780SHannes Reinecke static struct bin_attribute *scsi_sdev_bin_attrs[] = {
1313d188b067SRyan Attard 	&dev_attr_vpd_pg0,
1314b3ae8780SHannes Reinecke 	&dev_attr_vpd_pg83,
1315b3ae8780SHannes Reinecke 	&dev_attr_vpd_pg80,
1316d188b067SRyan Attard 	&dev_attr_vpd_pg89,
1317e60ac0b9SMartin K. Petersen 	&dev_attr_vpd_pgb0,
1318e60ac0b9SMartin K. Petersen 	&dev_attr_vpd_pgb1,
1319e60ac0b9SMartin K. Petersen 	&dev_attr_vpd_pgb2,
132092e6246cSJohannes Thumshirn 	&dev_attr_inquiry,
1321b3ae8780SHannes Reinecke 	NULL
1322b3ae8780SHannes Reinecke };
1323276b20d0SHannes Reinecke static struct attribute_group scsi_sdev_attr_group = {
1324276b20d0SHannes Reinecke 	.attrs =	scsi_sdev_attrs,
1325b3ae8780SHannes Reinecke 	.bin_attrs =	scsi_sdev_bin_attrs,
1326276b20d0SHannes Reinecke 	.is_visible =	scsi_sdev_attr_is_visible,
13277e47976bSHannes Reinecke 	.is_bin_visible = scsi_sdev_bin_attr_is_visible,
1328276b20d0SHannes Reinecke };
1329276b20d0SHannes Reinecke 
1330276b20d0SHannes Reinecke static const struct attribute_group *scsi_sdev_attr_groups[] = {
1331276b20d0SHannes Reinecke 	&scsi_sdev_attr_group,
1332276b20d0SHannes Reinecke 	NULL
1333276b20d0SHannes Reinecke };
1334276b20d0SHannes Reinecke 
1335643eb2d9SJames Bottomley static int scsi_target_add(struct scsi_target *starget)
1336643eb2d9SJames Bottomley {
1337643eb2d9SJames Bottomley 	int error;
1338643eb2d9SJames Bottomley 
1339643eb2d9SJames Bottomley 	if (starget->state != STARGET_CREATED)
1340643eb2d9SJames Bottomley 		return 0;
1341643eb2d9SJames Bottomley 
1342643eb2d9SJames Bottomley 	error = device_add(&starget->dev);
1343643eb2d9SJames Bottomley 	if (error) {
1344643eb2d9SJames Bottomley 		dev_err(&starget->dev, "target device_add failed, error %d\n", error);
1345643eb2d9SJames Bottomley 		return error;
1346643eb2d9SJames Bottomley 	}
1347643eb2d9SJames Bottomley 	transport_add_device(&starget->dev);
1348643eb2d9SJames Bottomley 	starget->state = STARGET_RUNNING;
1349643eb2d9SJames Bottomley 
1350bc4f2401SAlan Stern 	pm_runtime_set_active(&starget->dev);
1351bc4f2401SAlan Stern 	pm_runtime_enable(&starget->dev);
1352bc4f2401SAlan Stern 	device_enable_async_suspend(&starget->dev);
1353bc4f2401SAlan Stern 
1354643eb2d9SJames Bottomley 	return 0;
1355643eb2d9SJames Bottomley }
1356643eb2d9SJames Bottomley 
13571da177e4SLinus Torvalds /**
13581da177e4SLinus Torvalds  * scsi_sysfs_add_sdev - add scsi device to sysfs
13591da177e4SLinus Torvalds  * @sdev:	scsi_device to add
13601da177e4SLinus Torvalds  *
13611da177e4SLinus Torvalds  * Return value:
13621da177e4SLinus Torvalds  * 	0 on Success / non-zero on Failure
13631da177e4SLinus Torvalds  **/
13641da177e4SLinus Torvalds int scsi_sysfs_add_sdev(struct scsi_device *sdev)
13651da177e4SLinus Torvalds {
136692c4b58bSBart Van Assche 	int error;
1367643eb2d9SJames Bottomley 	struct scsi_target *starget = sdev->sdev_target;
13681da177e4SLinus Torvalds 
1369643eb2d9SJames Bottomley 	error = scsi_target_add(starget);
1370643eb2d9SJames Bottomley 	if (error)
1371643eb2d9SJames Bottomley 		return error;
1372643eb2d9SJames Bottomley 
1373643eb2d9SJames Bottomley 	transport_configure_device(&starget->dev);
1374bc4f2401SAlan Stern 
13754cb077d9SRafael J. Wysocki 	device_enable_async_suspend(&sdev->sdev_gendev);
1376bc4f2401SAlan Stern 	scsi_autopm_get_target(starget);
1377bc4f2401SAlan Stern 	pm_runtime_set_active(&sdev->sdev_gendev);
1378c74f8056SStanley Chu 	if (!sdev->rpm_autosuspend)
1379bc4f2401SAlan Stern 		pm_runtime_forbid(&sdev->sdev_gendev);
1380bc4f2401SAlan Stern 	pm_runtime_enable(&sdev->sdev_gendev);
1381bc4f2401SAlan Stern 	scsi_autopm_put_target(starget);
1382bc4f2401SAlan Stern 
1383bc4f2401SAlan Stern 	scsi_autopm_get_device(sdev);
1384bc4f2401SAlan Stern 
13852930f817SHannes Reinecke 	scsi_dh_add_device(sdev);
1386086b91d0SChristoph Hellwig 
13874cd2459cSHannes Reinecke 	error = device_add(&sdev->sdev_gendev);
13884cd2459cSHannes Reinecke 	if (error) {
13894cd2459cSHannes Reinecke 		sdev_printk(KERN_INFO, sdev,
13904cd2459cSHannes Reinecke 				"failed to add device: %d\n", error);
13914cd2459cSHannes Reinecke 		return error;
13924cd2459cSHannes Reinecke 	}
13934cd2459cSHannes Reinecke 
13944cb077d9SRafael J. Wysocki 	device_enable_async_suspend(&sdev->sdev_dev);
1395ee959b00STony Jones 	error = device_add(&sdev->sdev_dev);
13961da177e4SLinus Torvalds 	if (error) {
139773d8c34fSAlan Stern 		sdev_printk(KERN_INFO, sdev,
139873d8c34fSAlan Stern 				"failed to add class device: %d\n", error);
1399860dc736SJames Bottomley 		device_del(&sdev->sdev_gendev);
1400ee37e09dSAlan Stern 		return error;
14011da177e4SLinus Torvalds 	}
1402860dc736SJames Bottomley 	transport_add_device(&sdev->sdev_gendev);
1403860dc736SJames Bottomley 	sdev->is_visible = 1;
14041da177e4SLinus Torvalds 
1405ead09dd3SChristoph Hellwig 	if (IS_ENABLED(CONFIG_BLK_DEV_BSG)) {
1406ead09dd3SChristoph Hellwig 		sdev->bsg_dev = scsi_bsg_register_queue(sdev);
1407ead09dd3SChristoph Hellwig 		if (IS_ERR(sdev->bsg_dev)) {
14085f7cf82cSJackie Liu 			error = PTR_ERR(sdev->bsg_dev);
140980ed71ceSJames Bottomley 			sdev_printk(KERN_INFO, sdev,
1410ead09dd3SChristoph Hellwig 				    "Failed to register bsg queue, errno=%d\n",
1411ead09dd3SChristoph Hellwig 				    error);
1412ead09dd3SChristoph Hellwig 			sdev->bsg_dev = NULL;
1413ead09dd3SChristoph Hellwig 		}
1414ead09dd3SChristoph Hellwig 	}
141580ed71ceSJames Bottomley 
14166fe8c1dbSSubhash Jadavani 	scsi_autopm_put_device(sdev);
1417ee37e09dSAlan Stern 	return error;
14181da177e4SLinus Torvalds }
14191da177e4SLinus Torvalds 
1420903f4fedSAlan Stern void __scsi_remove_device(struct scsi_device *sdev)
14211da177e4SLinus Torvalds {
1422df133c21SJames Bottomley 	struct device *dev = &sdev->sdev_gendev;
14230db6ca8aSBart Van Assche 	int res;
1424df133c21SJames Bottomley 
1425be821fd8SVitaly Kuznetsov 	/*
1426be821fd8SVitaly Kuznetsov 	 * This cleanup path is not reentrant and while it is impossible
1427be821fd8SVitaly Kuznetsov 	 * to get a new reference with scsi_device_get() someone can still
1428be821fd8SVitaly Kuznetsov 	 * hold a previously acquired one.
1429be821fd8SVitaly Kuznetsov 	 */
1430be821fd8SVitaly Kuznetsov 	if (sdev->sdev_state == SDEV_DEL)
1431be821fd8SVitaly Kuznetsov 		return;
1432be821fd8SVitaly Kuznetsov 
1433860dc736SJames Bottomley 	if (sdev->is_visible) {
14340db6ca8aSBart Van Assche 		/*
14350db6ca8aSBart Van Assche 		 * If scsi_internal_target_block() is running concurrently,
14360db6ca8aSBart Van Assche 		 * wait until it has finished before changing the device state.
14370db6ca8aSBart Van Assche 		 */
14380db6ca8aSBart Van Assche 		mutex_lock(&sdev->state_mutex);
1439255ee932SBart Van Assche 		/*
1440255ee932SBart Van Assche 		 * If blocked, we go straight to DEL and restart the queue so
1441255ee932SBart Van Assche 		 * any commands issued during driver shutdown (like sync
1442255ee932SBart Van Assche 		 * cache) are errored immediately.
1443255ee932SBart Van Assche 		 */
14440db6ca8aSBart Van Assche 		res = scsi_device_set_state(sdev, SDEV_CANCEL);
1445255ee932SBart Van Assche 		if (res != 0) {
1446255ee932SBart Van Assche 			res = scsi_device_set_state(sdev, SDEV_DEL);
1447255ee932SBart Van Assche 			if (res == 0)
1448255ee932SBart Van Assche 				scsi_start_queue(sdev);
1449255ee932SBart Van Assche 		}
14500db6ca8aSBart Van Assche 		mutex_unlock(&sdev->state_mutex);
14510db6ca8aSBart Van Assche 
14520db6ca8aSBart Van Assche 		if (res != 0)
1453903f4fedSAlan Stern 			return;
14541da177e4SLinus Torvalds 
1455ead09dd3SChristoph Hellwig 		if (IS_ENABLED(CONFIG_BLK_DEV_BSG) && sdev->bsg_dev)
1456ead09dd3SChristoph Hellwig 			bsg_unregister_queue(sdev->bsg_dev);
1457ee959b00STony Jones 		device_unregister(&sdev->sdev_dev);
1458df133c21SJames Bottomley 		transport_remove_device(dev);
1459e619e6cbSBart Van Assche 		device_del(dev);
1460e619e6cbSBart Van Assche 	} else
1461e619e6cbSBart Van Assche 		put_device(&sdev->sdev_dev);
1462b485462aSBart Van Assche 
1463b485462aSBart Van Assche 	/*
1464b485462aSBart Van Assche 	 * Stop accepting new requests and wait until all queuecommand() and
1465b485462aSBart Van Assche 	 * scsi_run_queue() invocations have finished before tearing down the
1466b485462aSBart Van Assche 	 * device.
1467b485462aSBart Van Assche 	 */
14680db6ca8aSBart Van Assche 	mutex_lock(&sdev->state_mutex);
14691da177e4SLinus Torvalds 	scsi_device_set_state(sdev, SDEV_DEL);
14700db6ca8aSBart Van Assche 	mutex_unlock(&sdev->state_mutex);
14710db6ca8aSBart Van Assche 
14726f8191fdSChristoph Hellwig 	blk_mq_destroy_queue(sdev->request_queue);
14738fe4ce58SBart Van Assche 	kref_put(&sdev->host->tagset_refcnt, scsi_mq_free_tags);
1474b485462aSBart Van Assche 	cancel_work_sync(&sdev->requeue_work);
1475b485462aSBart Van Assche 
14761da177e4SLinus Torvalds 	if (sdev->host->hostt->slave_destroy)
14771da177e4SLinus Torvalds 		sdev->host->hostt->slave_destroy(sdev);
1478df133c21SJames Bottomley 	transport_destroy_device(dev);
147986cbfb56SJames Bottomley 
1480e63ed0d7SJames Bottomley 	/*
1481e63ed0d7SJames Bottomley 	 * Paired with the kref_get() in scsi_sysfs_initialize().  We have
1482eee8910fSdudengke 	 * removed sysfs visibility from the device, so make the target
1483e63ed0d7SJames Bottomley 	 * invisible if this was the last device underneath it.
1484e63ed0d7SJames Bottomley 	 */
1485e63ed0d7SJames Bottomley 	scsi_target_reap(scsi_target(sdev));
1486e63ed0d7SJames Bottomley 
1487df133c21SJames Bottomley 	put_device(dev);
1488903f4fedSAlan Stern }
1489903f4fedSAlan Stern 
1490903f4fedSAlan Stern /**
1491903f4fedSAlan Stern  * scsi_remove_device - unregister a device from the scsi bus
1492903f4fedSAlan Stern  * @sdev:	scsi_device to unregister
1493903f4fedSAlan Stern  **/
1494903f4fedSAlan Stern void scsi_remove_device(struct scsi_device *sdev)
1495903f4fedSAlan Stern {
149654195002SAlan Stern 	struct Scsi_Host *shost = sdev->host;
149754195002SAlan Stern 
14980b950672SArjan van de Ven 	mutex_lock(&shost->scan_mutex);
1499903f4fedSAlan Stern 	__scsi_remove_device(sdev);
15000b950672SArjan van de Ven 	mutex_unlock(&shost->scan_mutex);
15011da177e4SLinus Torvalds }
15021da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_remove_device);
15031da177e4SLinus Torvalds 
150444818efbSAdrian Bunk static void __scsi_remove_target(struct scsi_target *starget)
15051da177e4SLinus Torvalds {
15061da177e4SLinus Torvalds 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
15071da177e4SLinus Torvalds 	unsigned long flags;
1508a64358dbSAlan Stern 	struct scsi_device *sdev;
15091da177e4SLinus Torvalds 
15101da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
1511a64358dbSAlan Stern  restart:
1512a64358dbSAlan Stern 	list_for_each_entry(sdev, &shost->__devices, siblings) {
1513fbce4d97SHannes Reinecke 		/*
1514fbce4d97SHannes Reinecke 		 * We cannot call scsi_device_get() here, as
1515fbce4d97SHannes Reinecke 		 * we might've been called from rmmod() causing
1516fbce4d97SHannes Reinecke 		 * scsi_device_get() to fail the module_is_live()
1517fbce4d97SHannes Reinecke 		 * check.
1518fbce4d97SHannes Reinecke 		 */
15191da177e4SLinus Torvalds 		if (sdev->channel != starget->channel ||
152081b6c999SHannes Reinecke 		    sdev->id != starget->id)
152181b6c999SHannes Reinecke 			continue;
152281b6c999SHannes Reinecke 		if (sdev->sdev_state == SDEV_DEL ||
152381b6c999SHannes Reinecke 		    sdev->sdev_state == SDEV_CANCEL ||
1524fbce4d97SHannes Reinecke 		    !get_device(&sdev->sdev_gendev))
15251da177e4SLinus Torvalds 			continue;
15261da177e4SLinus Torvalds 		spin_unlock_irqrestore(shost->host_lock, flags);
15271da177e4SLinus Torvalds 		scsi_remove_device(sdev);
1528fbce4d97SHannes Reinecke 		put_device(&sdev->sdev_gendev);
15291da177e4SLinus Torvalds 		spin_lock_irqsave(shost->host_lock, flags);
1530a64358dbSAlan Stern 		goto restart;
15311da177e4SLinus Torvalds 	}
15321da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
153320b1e674Smochel@digitalimplant.org }
153420b1e674Smochel@digitalimplant.org 
15351da177e4SLinus Torvalds /**
15361da177e4SLinus Torvalds  * scsi_remove_target - try to remove a target and all its devices
15371da177e4SLinus Torvalds  * @dev: generic starget or parent of generic stargets to be removed
15381da177e4SLinus Torvalds  *
15391da177e4SLinus Torvalds  * Note: This is slightly racy.  It is possible that if the user
15401da177e4SLinus Torvalds  * requests the addition of another device then the target won't be
15411da177e4SLinus Torvalds  * removed.
15421da177e4SLinus Torvalds  */
15431da177e4SLinus Torvalds void scsi_remove_target(struct device *dev)
15441da177e4SLinus Torvalds {
15453b661a92SDan Williams 	struct Scsi_Host *shost = dev_to_shost(dev->parent);
1546305c2e71SJohannes Thumshirn 	struct scsi_target *starget;
15473b661a92SDan Williams 	unsigned long flags;
15481da177e4SLinus Torvalds 
154940998193SChristoph Hellwig restart:
15503b661a92SDan Williams 	spin_lock_irqsave(shost->host_lock, flags);
15513b661a92SDan Williams 	list_for_each_entry(starget, &shost->__targets, siblings) {
155290a88d6eSJames Bottomley 		if (starget->state == STARGET_DEL ||
1553f9279c96SEwan D. Milne 		    starget->state == STARGET_REMOVE ||
1554f9279c96SEwan D. Milne 		    starget->state == STARGET_CREATED_REMOVE)
15553b661a92SDan Williams 			continue;
15563b661a92SDan Williams 		if (starget->dev.parent == dev || &starget->dev == dev) {
1557e63ed0d7SJames Bottomley 			kref_get(&starget->reap_ref);
1558f9279c96SEwan D. Milne 			if (starget->state == STARGET_CREATED)
1559f9279c96SEwan D. Milne 				starget->state = STARGET_CREATED_REMOVE;
1560f9279c96SEwan D. Milne 			else
1561f05795d3SJohannes Thumshirn 				starget->state = STARGET_REMOVE;
1562bc3f02a7SDan Williams 			spin_unlock_irqrestore(shost->host_lock, flags);
1563bc3f02a7SDan Williams 			__scsi_remove_target(starget);
156440998193SChristoph Hellwig 			scsi_target_reap(starget);
156540998193SChristoph Hellwig 			goto restart;
15663b661a92SDan Williams 		}
15673b661a92SDan Williams 	}
15683b661a92SDan Williams 	spin_unlock_irqrestore(shost->host_lock, flags);
15691da177e4SLinus Torvalds }
15701da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_remove_target);
15711da177e4SLinus Torvalds 
15721da177e4SLinus Torvalds int scsi_register_driver(struct device_driver *drv)
15731da177e4SLinus Torvalds {
15741da177e4SLinus Torvalds 	drv->bus = &scsi_bus_type;
15751da177e4SLinus Torvalds 
15761da177e4SLinus Torvalds 	return driver_register(drv);
15771da177e4SLinus Torvalds }
15781da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_register_driver);
15791da177e4SLinus Torvalds 
15801da177e4SLinus Torvalds int scsi_register_interface(struct class_interface *intf)
15811da177e4SLinus Torvalds {
15821da177e4SLinus Torvalds 	intf->class = &sdev_class;
15831da177e4SLinus Torvalds 
15841da177e4SLinus Torvalds 	return class_interface_register(intf);
15851da177e4SLinus Torvalds }
15861da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_register_interface);
15871da177e4SLinus Torvalds 
15881da177e4SLinus Torvalds /**
15891da177e4SLinus Torvalds  * scsi_sysfs_add_host - add scsi host to subsystem
15901da177e4SLinus Torvalds  * @shost:     scsi host struct to add to subsystem
15911da177e4SLinus Torvalds  **/
15921da177e4SLinus Torvalds int scsi_sysfs_add_host(struct Scsi_Host *shost)
15931da177e4SLinus Torvalds {
15941da177e4SLinus Torvalds 	transport_register_device(&shost->shost_gendev);
1595d52b3815SJames Bottomley 	transport_configure_device(&shost->shost_gendev);
15961da177e4SLinus Torvalds 	return 0;
15971da177e4SLinus Torvalds }
15981da177e4SLinus Torvalds 
1599bfd12944SKay Sievers static struct device_type scsi_dev_type = {
1600bfd12944SKay Sievers 	.name =		"scsi_device",
1601bfd12944SKay Sievers 	.release =	scsi_device_dev_release,
1602bfd12944SKay Sievers 	.groups =	scsi_sdev_attr_groups,
1603bfd12944SKay Sievers };
1604bfd12944SKay Sievers 
16051da177e4SLinus Torvalds void scsi_sysfs_device_initialize(struct scsi_device *sdev)
16061da177e4SLinus Torvalds {
16071da177e4SLinus Torvalds 	unsigned long flags;
16081da177e4SLinus Torvalds 	struct Scsi_Host *shost = sdev->host;
160992c4b58bSBart Van Assche 	struct scsi_host_template *hostt = shost->hostt;
16101da177e4SLinus Torvalds 	struct scsi_target  *starget = sdev->sdev_target;
16111da177e4SLinus Torvalds 
16121da177e4SLinus Torvalds 	device_initialize(&sdev->sdev_gendev);
16131da177e4SLinus Torvalds 	sdev->sdev_gendev.bus = &scsi_bus_type;
1614bfd12944SKay Sievers 	sdev->sdev_gendev.type = &scsi_dev_type;
1615a19a93e4SBart Van Assche 	scsi_enable_async_suspend(&sdev->sdev_gendev);
16169cb78c16SHannes Reinecke 	dev_set_name(&sdev->sdev_gendev, "%d:%d:%d:%llu",
161771610f55SKay Sievers 		     sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
16183a71f0f7SSteffen Maier 	sdev->sdev_gendev.groups = hostt->sdev_groups;
16191da177e4SLinus Torvalds 
1620ee959b00STony Jones 	device_initialize(&sdev->sdev_dev);
162137e6ba00SJames Bottomley 	sdev->sdev_dev.parent = get_device(&sdev->sdev_gendev);
1622ee959b00STony Jones 	sdev->sdev_dev.class = &sdev_class;
16239cb78c16SHannes Reinecke 	dev_set_name(&sdev->sdev_dev, "%d:%d:%d:%llu",
162471610f55SKay Sievers 		     sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
162550c4e964SAlan Stern 	/*
162650c4e964SAlan Stern 	 * Get a default scsi_level from the target (derived from sibling
162750c4e964SAlan Stern 	 * devices).  This is the best we can do for guessing how to set
162850c4e964SAlan Stern 	 * sdev->lun_in_cdb for the initial INQUIRY command.  For LUN 0 the
162950c4e964SAlan Stern 	 * setting doesn't matter, because all the bits are zero anyway.
163050c4e964SAlan Stern 	 * But it does matter for higher LUNs.
163150c4e964SAlan Stern 	 */
16327c9d6f16SAlan Stern 	sdev->scsi_level = starget->scsi_level;
163350c4e964SAlan Stern 	if (sdev->scsi_level <= SCSI_2 &&
163450c4e964SAlan Stern 			sdev->scsi_level != SCSI_UNKNOWN &&
163550c4e964SAlan Stern 			!shost->no_scsi2_lun_in_cdb)
163650c4e964SAlan Stern 		sdev->lun_in_cdb = 1;
163750c4e964SAlan Stern 
16381da177e4SLinus Torvalds 	transport_setup_device(&sdev->sdev_gendev);
16391da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
16401da177e4SLinus Torvalds 	list_add_tail(&sdev->same_target_siblings, &starget->devices);
16411da177e4SLinus Torvalds 	list_add_tail(&sdev->siblings, &shost->__devices);
16421da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
1643e63ed0d7SJames Bottomley 	/*
1644e63ed0d7SJames Bottomley 	 * device can now only be removed via __scsi_remove_device() so hold
1645e63ed0d7SJames Bottomley 	 * the target.  Target will be held in CREATED state until something
1646e63ed0d7SJames Bottomley 	 * beneath it becomes visible (in which case it moves to RUNNING)
1647e63ed0d7SJames Bottomley 	 */
1648e63ed0d7SJames Bottomley 	kref_get(&starget->reap_ref);
16491da177e4SLinus Torvalds }
16501da177e4SLinus Torvalds 
16511da177e4SLinus Torvalds int scsi_is_sdev_device(const struct device *dev)
16521da177e4SLinus Torvalds {
165313ba9bcbSKay Sievers 	return dev->type == &scsi_dev_type;
16541da177e4SLinus Torvalds }
16551da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_is_sdev_device);
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds /* A blank transport template that is used in drivers that don't
16581da177e4SLinus Torvalds  * yet implement Transport Attributes */
16591da177e4SLinus Torvalds struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };
1660