11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * scsi_sysfs.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * SCSI sysfs interface routines. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Created to pull SCSI mid layer sysfs routines into one file. 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 91da177e4SLinus Torvalds #include <linux/module.h> 105a0e3ad6STejun Heo #include <linux/slab.h> 111da177e4SLinus Torvalds #include <linux/init.h> 121da177e4SLinus Torvalds #include <linux/blkdev.h> 131da177e4SLinus Torvalds #include <linux/device.h> 14bc4f2401SAlan Stern #include <linux/pm_runtime.h> 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds #include <scsi/scsi.h> 171da177e4SLinus Torvalds #include <scsi/scsi_device.h> 181da177e4SLinus Torvalds #include <scsi/scsi_host.h> 191da177e4SLinus Torvalds #include <scsi/scsi_tcq.h> 2041f95dd2SHannes Reinecke #include <scsi/scsi_dh.h> 211da177e4SLinus Torvalds #include <scsi/scsi_transport.h> 2244818efbSAdrian Bunk #include <scsi/scsi_driver.h> 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds #include "scsi_priv.h" 251da177e4SLinus Torvalds #include "scsi_logging.h" 261da177e4SLinus Torvalds 27b0ed4336SHannes Reinecke static struct device_type scsi_dev_type; 28b0ed4336SHannes Reinecke 290ad78200SArjan van de Ven static const struct { 301da177e4SLinus Torvalds enum scsi_device_state value; 311da177e4SLinus Torvalds char *name; 321da177e4SLinus Torvalds } sdev_states[] = { 331da177e4SLinus Torvalds { SDEV_CREATED, "created" }, 341da177e4SLinus Torvalds { SDEV_RUNNING, "running" }, 351da177e4SLinus Torvalds { SDEV_CANCEL, "cancel" }, 361da177e4SLinus Torvalds { SDEV_DEL, "deleted" }, 371da177e4SLinus Torvalds { SDEV_QUIESCE, "quiesce" }, 381da177e4SLinus Torvalds { SDEV_OFFLINE, "offline" }, 391b8d2620SMike Christie { SDEV_TRANSPORT_OFFLINE, "transport-offline" }, 401da177e4SLinus Torvalds { SDEV_BLOCK, "blocked" }, 416f4267e3SJames Bottomley { SDEV_CREATED_BLOCK, "created-blocked" }, 421da177e4SLinus Torvalds }; 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds const char *scsi_device_state_name(enum scsi_device_state state) 451da177e4SLinus Torvalds { 461da177e4SLinus Torvalds int i; 471da177e4SLinus Torvalds char *name = NULL; 481da177e4SLinus Torvalds 496391a113STobias Klauser for (i = 0; i < ARRAY_SIZE(sdev_states); i++) { 501da177e4SLinus Torvalds if (sdev_states[i].value == state) { 511da177e4SLinus Torvalds name = sdev_states[i].name; 521da177e4SLinus Torvalds break; 531da177e4SLinus Torvalds } 541da177e4SLinus Torvalds } 551da177e4SLinus Torvalds return name; 561da177e4SLinus Torvalds } 571da177e4SLinus Torvalds 580ad78200SArjan van de Ven static const struct { 59d3301874SMike Anderson enum scsi_host_state value; 60d3301874SMike Anderson char *name; 61d3301874SMike Anderson } shost_states[] = { 62d3301874SMike Anderson { SHOST_CREATED, "created" }, 63d3301874SMike Anderson { SHOST_RUNNING, "running" }, 64d3301874SMike Anderson { SHOST_CANCEL, "cancel" }, 65d3301874SMike Anderson { SHOST_DEL, "deleted" }, 66d3301874SMike Anderson { SHOST_RECOVERY, "recovery" }, 67939647eeSJames Bottomley { SHOST_CANCEL_RECOVERY, "cancel/recovery" }, 68939647eeSJames Bottomley { SHOST_DEL_RECOVERY, "deleted/recovery", }, 69d3301874SMike Anderson }; 70d3301874SMike Anderson const char *scsi_host_state_name(enum scsi_host_state state) 71d3301874SMike Anderson { 72d3301874SMike Anderson int i; 73d3301874SMike Anderson char *name = NULL; 74d3301874SMike Anderson 756391a113STobias Klauser for (i = 0; i < ARRAY_SIZE(shost_states); i++) { 76d3301874SMike Anderson if (shost_states[i].value == state) { 77d3301874SMike Anderson name = shost_states[i].name; 78d3301874SMike Anderson break; 79d3301874SMike Anderson } 80d3301874SMike Anderson } 81d3301874SMike Anderson return name; 82d3301874SMike Anderson } 83d3301874SMike Anderson 849cb78c16SHannes Reinecke static int check_set(unsigned long long *val, char *src) 851da177e4SLinus Torvalds { 861da177e4SLinus Torvalds char *last; 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds if (strncmp(src, "-", 20) == 0) { 891da177e4SLinus Torvalds *val = SCAN_WILD_CARD; 901da177e4SLinus Torvalds } else { 911da177e4SLinus Torvalds /* 921da177e4SLinus Torvalds * Doesn't check for int overflow 931da177e4SLinus Torvalds */ 949cb78c16SHannes Reinecke *val = simple_strtoull(src, &last, 0); 951da177e4SLinus Torvalds if (*last != '\0') 961da177e4SLinus Torvalds return 1; 971da177e4SLinus Torvalds } 981da177e4SLinus Torvalds return 0; 991da177e4SLinus Torvalds } 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds static int scsi_scan(struct Scsi_Host *shost, const char *str) 1021da177e4SLinus Torvalds { 1039cb78c16SHannes Reinecke char s1[15], s2[15], s3[17], junk; 1049cb78c16SHannes Reinecke unsigned long long channel, id, lun; 1051da177e4SLinus Torvalds int res; 1061da177e4SLinus Torvalds 1079cb78c16SHannes Reinecke res = sscanf(str, "%10s %10s %16s %c", s1, s2, s3, &junk); 1081da177e4SLinus Torvalds if (res != 3) 1091da177e4SLinus Torvalds return -EINVAL; 1101da177e4SLinus Torvalds if (check_set(&channel, s1)) 1111da177e4SLinus Torvalds return -EINVAL; 1121da177e4SLinus Torvalds if (check_set(&id, s2)) 1131da177e4SLinus Torvalds return -EINVAL; 1141da177e4SLinus Torvalds if (check_set(&lun, s3)) 1151da177e4SLinus Torvalds return -EINVAL; 116e02f3f59SChristoph Hellwig if (shost->transportt->user_scan) 117e02f3f59SChristoph Hellwig res = shost->transportt->user_scan(shost, channel, id, lun); 118e02f3f59SChristoph Hellwig else 1191da177e4SLinus Torvalds res = scsi_scan_host_selected(shost, channel, id, lun, 1); 1201da177e4SLinus Torvalds return res; 1211da177e4SLinus Torvalds } 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds /* 1241da177e4SLinus Torvalds * shost_show_function: macro to create an attr function that can be used to 1251da177e4SLinus Torvalds * show a non-bit field. 1261da177e4SLinus Torvalds */ 1271da177e4SLinus Torvalds #define shost_show_function(name, field, format_string) \ 1281da177e4SLinus Torvalds static ssize_t \ 129ee959b00STony Jones show_##name (struct device *dev, struct device_attribute *attr, \ 130ee959b00STony Jones char *buf) \ 1311da177e4SLinus Torvalds { \ 132ee959b00STony Jones struct Scsi_Host *shost = class_to_shost(dev); \ 1331da177e4SLinus Torvalds return snprintf (buf, 20, format_string, shost->field); \ 1341da177e4SLinus Torvalds } 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds /* 1371da177e4SLinus Torvalds * shost_rd_attr: macro to create a function and attribute variable for a 1381da177e4SLinus Torvalds * read only field. 1391da177e4SLinus Torvalds */ 1401da177e4SLinus Torvalds #define shost_rd_attr2(name, field, format_string) \ 1411da177e4SLinus Torvalds shost_show_function(name, field, format_string) \ 142ee959b00STony Jones static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL); 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds #define shost_rd_attr(field, format_string) \ 1451da177e4SLinus Torvalds shost_rd_attr2(field, field, format_string) 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds /* 1481da177e4SLinus Torvalds * Create the actual show/store functions and data structures. 1491da177e4SLinus Torvalds */ 1501da177e4SLinus Torvalds 151ee959b00STony Jones static ssize_t 152ee959b00STony Jones store_scan(struct device *dev, struct device_attribute *attr, 153ee959b00STony Jones const char *buf, size_t count) 1541da177e4SLinus Torvalds { 155ee959b00STony Jones struct Scsi_Host *shost = class_to_shost(dev); 1561da177e4SLinus Torvalds int res; 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds res = scsi_scan(shost, buf); 1591da177e4SLinus Torvalds if (res == 0) 1601da177e4SLinus Torvalds res = count; 1611da177e4SLinus Torvalds return res; 1621da177e4SLinus Torvalds }; 163ee959b00STony Jones static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan); 1641da177e4SLinus Torvalds 165d3301874SMike Anderson static ssize_t 166ee959b00STony Jones store_shost_state(struct device *dev, struct device_attribute *attr, 167ee959b00STony Jones const char *buf, size_t count) 168d3301874SMike Anderson { 169d3301874SMike Anderson int i; 170ee959b00STony Jones struct Scsi_Host *shost = class_to_shost(dev); 171d3301874SMike Anderson enum scsi_host_state state = 0; 172d3301874SMike Anderson 1736391a113STobias Klauser for (i = 0; i < ARRAY_SIZE(shost_states); i++) { 174d3301874SMike Anderson const int len = strlen(shost_states[i].name); 175d3301874SMike Anderson if (strncmp(shost_states[i].name, buf, len) == 0 && 176d3301874SMike Anderson buf[len] == '\n') { 177d3301874SMike Anderson state = shost_states[i].value; 178d3301874SMike Anderson break; 179d3301874SMike Anderson } 180d3301874SMike Anderson } 181d3301874SMike Anderson if (!state) 182d3301874SMike Anderson return -EINVAL; 183d3301874SMike Anderson 184d3301874SMike Anderson if (scsi_host_set_state(shost, state)) 185d3301874SMike Anderson return -EINVAL; 186d3301874SMike Anderson return count; 187d3301874SMike Anderson } 188d3301874SMike Anderson 189d3301874SMike Anderson static ssize_t 190ee959b00STony Jones show_shost_state(struct device *dev, struct device_attribute *attr, char *buf) 191d3301874SMike Anderson { 192ee959b00STony Jones struct Scsi_Host *shost = class_to_shost(dev); 193d3301874SMike Anderson const char *name = scsi_host_state_name(shost->shost_state); 194d3301874SMike Anderson 195d3301874SMike Anderson if (!name) 196d3301874SMike Anderson return -EINVAL; 197d3301874SMike Anderson 198d3301874SMike Anderson return snprintf(buf, 20, "%s\n", name); 199d3301874SMike Anderson } 200d3301874SMike Anderson 201ee959b00STony Jones /* DEVICE_ATTR(state) clashes with dev_attr_state for sdev */ 202ee959b00STony Jones struct device_attribute dev_attr_hstate = 203ee959b00STony Jones __ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state); 204d3301874SMike Anderson 2055dc2b89eSFUJITA Tomonori static ssize_t 2065dc2b89eSFUJITA Tomonori show_shost_mode(unsigned int mode, char *buf) 2075dc2b89eSFUJITA Tomonori { 2085dc2b89eSFUJITA Tomonori ssize_t len = 0; 2095dc2b89eSFUJITA Tomonori 2105dc2b89eSFUJITA Tomonori if (mode & MODE_INITIATOR) 2115dc2b89eSFUJITA Tomonori len = sprintf(buf, "%s", "Initiator"); 2125dc2b89eSFUJITA Tomonori 2135dc2b89eSFUJITA Tomonori if (mode & MODE_TARGET) 2145dc2b89eSFUJITA Tomonori len += sprintf(buf + len, "%s%s", len ? ", " : "", "Target"); 2155dc2b89eSFUJITA Tomonori 2165dc2b89eSFUJITA Tomonori len += sprintf(buf + len, "\n"); 2175dc2b89eSFUJITA Tomonori 2185dc2b89eSFUJITA Tomonori return len; 2195dc2b89eSFUJITA Tomonori } 2205dc2b89eSFUJITA Tomonori 221ee959b00STony Jones static ssize_t 222ee959b00STony Jones show_shost_supported_mode(struct device *dev, struct device_attribute *attr, 223ee959b00STony Jones char *buf) 2245dc2b89eSFUJITA Tomonori { 225ee959b00STony Jones struct Scsi_Host *shost = class_to_shost(dev); 2267a39ac3fSJames Bottomley unsigned int supported_mode = shost->hostt->supported_mode; 2275dc2b89eSFUJITA Tomonori 2287a39ac3fSJames Bottomley if (supported_mode == MODE_UNKNOWN) 2297a39ac3fSJames Bottomley /* by default this should be initiator */ 2307a39ac3fSJames Bottomley supported_mode = MODE_INITIATOR; 2317a39ac3fSJames Bottomley 2327a39ac3fSJames Bottomley return show_shost_mode(supported_mode, buf); 2335dc2b89eSFUJITA Tomonori } 2345dc2b89eSFUJITA Tomonori 235ee959b00STony Jones static DEVICE_ATTR(supported_mode, S_IRUGO | S_IWUSR, show_shost_supported_mode, NULL); 2365dc2b89eSFUJITA Tomonori 237ee959b00STony Jones static ssize_t 238ee959b00STony Jones show_shost_active_mode(struct device *dev, 239ee959b00STony Jones struct device_attribute *attr, char *buf) 2405dc2b89eSFUJITA Tomonori { 241ee959b00STony Jones struct Scsi_Host *shost = class_to_shost(dev); 2425dc2b89eSFUJITA Tomonori 2435dc2b89eSFUJITA Tomonori if (shost->active_mode == MODE_UNKNOWN) 2445dc2b89eSFUJITA Tomonori return snprintf(buf, 20, "unknown\n"); 2455dc2b89eSFUJITA Tomonori else 2465dc2b89eSFUJITA Tomonori return show_shost_mode(shost->active_mode, buf); 2475dc2b89eSFUJITA Tomonori } 2485dc2b89eSFUJITA Tomonori 249ee959b00STony Jones static DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL); 2505dc2b89eSFUJITA Tomonori 251072f19b4SSasha Levin static int check_reset_type(const char *str) 25229443691SVikas Chaudhary { 253072f19b4SSasha Levin if (sysfs_streq(str, "adapter")) 25429443691SVikas Chaudhary return SCSI_ADAPTER_RESET; 255072f19b4SSasha Levin else if (sysfs_streq(str, "firmware")) 25629443691SVikas Chaudhary return SCSI_FIRMWARE_RESET; 25729443691SVikas Chaudhary else 25829443691SVikas Chaudhary return 0; 25929443691SVikas Chaudhary } 26029443691SVikas Chaudhary 26129443691SVikas Chaudhary static ssize_t 26229443691SVikas Chaudhary store_host_reset(struct device *dev, struct device_attribute *attr, 26329443691SVikas Chaudhary const char *buf, size_t count) 26429443691SVikas Chaudhary { 26529443691SVikas Chaudhary struct Scsi_Host *shost = class_to_shost(dev); 26629443691SVikas Chaudhary struct scsi_host_template *sht = shost->hostt; 26729443691SVikas Chaudhary int ret = -EINVAL; 26829443691SVikas Chaudhary int type; 26929443691SVikas Chaudhary 270072f19b4SSasha Levin type = check_reset_type(buf); 27129443691SVikas Chaudhary if (!type) 27229443691SVikas Chaudhary goto exit_store_host_reset; 27329443691SVikas Chaudhary 27429443691SVikas Chaudhary if (sht->host_reset) 27529443691SVikas Chaudhary ret = sht->host_reset(shost, type); 27629443691SVikas Chaudhary 27729443691SVikas Chaudhary exit_store_host_reset: 27829443691SVikas Chaudhary if (ret == 0) 27929443691SVikas Chaudhary ret = count; 28029443691SVikas Chaudhary return ret; 28129443691SVikas Chaudhary } 28229443691SVikas Chaudhary 28329443691SVikas Chaudhary static DEVICE_ATTR(host_reset, S_IWUSR, NULL, store_host_reset); 28429443691SVikas Chaudhary 285b4562022SHannes Reinecke static ssize_t 286b4562022SHannes Reinecke show_shost_eh_deadline(struct device *dev, 287b4562022SHannes Reinecke struct device_attribute *attr, char *buf) 288b4562022SHannes Reinecke { 289b4562022SHannes Reinecke struct Scsi_Host *shost = class_to_shost(dev); 290b4562022SHannes Reinecke 291bb3b621aSRen Mingxin if (shost->eh_deadline == -1) 292bb3b621aSRen Mingxin return snprintf(buf, strlen("off") + 2, "off\n"); 293bb3b621aSRen Mingxin return sprintf(buf, "%u\n", shost->eh_deadline / HZ); 294b4562022SHannes Reinecke } 295b4562022SHannes Reinecke 296b4562022SHannes Reinecke static ssize_t 297b4562022SHannes Reinecke store_shost_eh_deadline(struct device *dev, struct device_attribute *attr, 298b4562022SHannes Reinecke const char *buf, size_t count) 299b4562022SHannes Reinecke { 300b4562022SHannes Reinecke struct Scsi_Host *shost = class_to_shost(dev); 301b4562022SHannes Reinecke int ret = -EINVAL; 302bb3b621aSRen Mingxin unsigned long deadline, flags; 303b4562022SHannes Reinecke 304ad469a57SHannes Reinecke if (shost->transportt && 305ad469a57SHannes Reinecke (shost->transportt->eh_strategy_handler || 306ad469a57SHannes Reinecke !shost->hostt->eh_host_reset_handler)) 307b4562022SHannes Reinecke return ret; 308b4562022SHannes Reinecke 309bb3b621aSRen Mingxin if (!strncmp(buf, "off", strlen("off"))) 310bb3b621aSRen Mingxin deadline = -1; 311bb3b621aSRen Mingxin else { 312bb3b621aSRen Mingxin ret = kstrtoul(buf, 10, &deadline); 313bb3b621aSRen Mingxin if (ret) 314bb3b621aSRen Mingxin return ret; 315bb3b621aSRen Mingxin if (deadline * HZ > UINT_MAX) 316bb3b621aSRen Mingxin return -EINVAL; 317bb3b621aSRen Mingxin } 318bb3b621aSRen Mingxin 319b4562022SHannes Reinecke spin_lock_irqsave(shost->host_lock, flags); 320b4562022SHannes Reinecke if (scsi_host_in_recovery(shost)) 321b4562022SHannes Reinecke ret = -EBUSY; 322b4562022SHannes Reinecke else { 323bb3b621aSRen Mingxin if (deadline == -1) 324bb3b621aSRen Mingxin shost->eh_deadline = -1; 325bb3b621aSRen Mingxin else 326b4562022SHannes Reinecke shost->eh_deadline = deadline * HZ; 327bb3b621aSRen Mingxin 328b4562022SHannes Reinecke ret = count; 329b4562022SHannes Reinecke } 330b4562022SHannes Reinecke spin_unlock_irqrestore(shost->host_lock, flags); 331bb3b621aSRen Mingxin 332b4562022SHannes Reinecke return ret; 333b4562022SHannes Reinecke } 334b4562022SHannes Reinecke 335b4562022SHannes Reinecke static DEVICE_ATTR(eh_deadline, S_IRUGO | S_IWUSR, show_shost_eh_deadline, store_shost_eh_deadline); 336b4562022SHannes Reinecke 337d285203cSChristoph Hellwig shost_rd_attr(use_blk_mq, "%d\n"); 3381da177e4SLinus Torvalds shost_rd_attr(unique_id, "%u\n"); 3391da177e4SLinus Torvalds shost_rd_attr(cmd_per_lun, "%hd\n"); 340ed632da8SJames Bottomley shost_rd_attr(can_queue, "%hd\n"); 3411da177e4SLinus Torvalds shost_rd_attr(sg_tablesize, "%hu\n"); 34213f05c8dSMartin K. Petersen shost_rd_attr(sg_prot_tablesize, "%hu\n"); 3431da177e4SLinus Torvalds shost_rd_attr(unchecked_isa_dma, "%d\n"); 3444469f987SMartin K. Petersen shost_rd_attr(prot_capabilities, "%u\n"); 3454469f987SMartin K. Petersen shost_rd_attr(prot_guard_type, "%hd\n"); 3461da177e4SLinus Torvalds shost_rd_attr2(proc_name, hostt->proc_name, "%s\n"); 3471da177e4SLinus Torvalds 34874665016SChristoph Hellwig static ssize_t 34974665016SChristoph Hellwig show_host_busy(struct device *dev, struct device_attribute *attr, char *buf) 35074665016SChristoph Hellwig { 35174665016SChristoph Hellwig struct Scsi_Host *shost = class_to_shost(dev); 35274665016SChristoph Hellwig return snprintf(buf, 20, "%d\n", atomic_read(&shost->host_busy)); 35374665016SChristoph Hellwig } 35474665016SChristoph Hellwig static DEVICE_ATTR(host_busy, S_IRUGO, show_host_busy, NULL); 35574665016SChristoph Hellwig 356f7120a4fSHannes Reinecke static struct attribute *scsi_sysfs_shost_attrs[] = { 357d285203cSChristoph Hellwig &dev_attr_use_blk_mq.attr, 358f7120a4fSHannes Reinecke &dev_attr_unique_id.attr, 359f7120a4fSHannes Reinecke &dev_attr_host_busy.attr, 360f7120a4fSHannes Reinecke &dev_attr_cmd_per_lun.attr, 361f7120a4fSHannes Reinecke &dev_attr_can_queue.attr, 362f7120a4fSHannes Reinecke &dev_attr_sg_tablesize.attr, 36313f05c8dSMartin K. Petersen &dev_attr_sg_prot_tablesize.attr, 364f7120a4fSHannes Reinecke &dev_attr_unchecked_isa_dma.attr, 365f7120a4fSHannes Reinecke &dev_attr_proc_name.attr, 366f7120a4fSHannes Reinecke &dev_attr_scan.attr, 367f7120a4fSHannes Reinecke &dev_attr_hstate.attr, 368f7120a4fSHannes Reinecke &dev_attr_supported_mode.attr, 369f7120a4fSHannes Reinecke &dev_attr_active_mode.attr, 3704469f987SMartin K. Petersen &dev_attr_prot_capabilities.attr, 3714469f987SMartin K. Petersen &dev_attr_prot_guard_type.attr, 37229443691SVikas Chaudhary &dev_attr_host_reset.attr, 373b4562022SHannes Reinecke &dev_attr_eh_deadline.attr, 374f7120a4fSHannes Reinecke NULL 375f7120a4fSHannes Reinecke }; 376f7120a4fSHannes Reinecke 377f7120a4fSHannes Reinecke struct attribute_group scsi_shost_attr_group = { 378f7120a4fSHannes Reinecke .attrs = scsi_sysfs_shost_attrs, 379f7120a4fSHannes Reinecke }; 380f7120a4fSHannes Reinecke 381a4dbd674SDavid Brownell const struct attribute_group *scsi_sysfs_shost_attr_groups[] = { 382f7120a4fSHannes Reinecke &scsi_shost_attr_group, 3831da177e4SLinus Torvalds NULL 3841da177e4SLinus Torvalds }; 3851da177e4SLinus Torvalds 386ee959b00STony Jones static void scsi_device_cls_release(struct device *class_dev) 3871da177e4SLinus Torvalds { 3881da177e4SLinus Torvalds struct scsi_device *sdev; 3891da177e4SLinus Torvalds 3901da177e4SLinus Torvalds sdev = class_to_sdev(class_dev); 3911da177e4SLinus Torvalds put_device(&sdev->sdev_gendev); 3921da177e4SLinus Torvalds } 3931da177e4SLinus Torvalds 39465f27f38SDavid Howells static void scsi_device_dev_release_usercontext(struct work_struct *work) 3951da177e4SLinus Torvalds { 3961da177e4SLinus Torvalds struct scsi_device *sdev; 3971da177e4SLinus Torvalds struct device *parent; 398a341cd0fSJeff Garzik struct list_head *this, *tmp; 3991da177e4SLinus Torvalds unsigned long flags; 4001da177e4SLinus Torvalds 40165f27f38SDavid Howells sdev = container_of(work, struct scsi_device, ew.work); 40265f27f38SDavid Howells 40323695e41SJunichi Nomura scsi_dh_release_device(sdev); 40423695e41SJunichi Nomura 40565f27f38SDavid Howells parent = sdev->sdev_gendev.parent; 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds spin_lock_irqsave(sdev->host->host_lock, flags); 4081da177e4SLinus Torvalds list_del(&sdev->siblings); 4091da177e4SLinus Torvalds list_del(&sdev->same_target_siblings); 4101da177e4SLinus Torvalds list_del(&sdev->starved_entry); 4111da177e4SLinus Torvalds spin_unlock_irqrestore(sdev->host->host_lock, flags); 4121da177e4SLinus Torvalds 413a341cd0fSJeff Garzik cancel_work_sync(&sdev->event_work); 414a341cd0fSJeff Garzik 415a341cd0fSJeff Garzik list_for_each_safe(this, tmp, &sdev->event_list) { 416a341cd0fSJeff Garzik struct scsi_event *evt; 417a341cd0fSJeff Garzik 418a341cd0fSJeff Garzik evt = list_entry(this, struct scsi_event, node); 419a341cd0fSJeff Garzik list_del(&evt->node); 420a341cd0fSJeff Garzik kfree(evt); 421a341cd0fSJeff Garzik } 422a341cd0fSJeff Garzik 423e73e079bSJames Bottomley blk_put_queue(sdev->request_queue); 42486cbfb56SJames Bottomley /* NULL queue means the device can't be used */ 425c2a9331cS sdev->request_queue = NULL; 4261da177e4SLinus Torvalds 427b3ae8780SHannes Reinecke kfree(sdev->vpd_pg83); 428b3ae8780SHannes Reinecke kfree(sdev->vpd_pg80); 4291da177e4SLinus Torvalds kfree(sdev->inquiry); 4301da177e4SLinus Torvalds kfree(sdev); 4311da177e4SLinus Torvalds 4321da177e4SLinus Torvalds if (parent) 4331da177e4SLinus Torvalds put_device(parent); 4341da177e4SLinus Torvalds } 4351da177e4SLinus Torvalds 43665110b21SJames Bottomley static void scsi_device_dev_release(struct device *dev) 43765110b21SJames Bottomley { 438ffedb452SJames Bottomley struct scsi_device *sdp = to_scsi_device(dev); 43965f27f38SDavid Howells execute_in_process_context(scsi_device_dev_release_usercontext, 440ffedb452SJames Bottomley &sdp->ew); 44165110b21SJames Bottomley } 44265110b21SJames Bottomley 44352c1da39SAdrian Bunk static struct class sdev_class = { 4441da177e4SLinus Torvalds .name = "scsi_device", 445ee959b00STony Jones .dev_release = scsi_device_cls_release, 4461da177e4SLinus Torvalds }; 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds /* all probing is done in the individual ->probe routines */ 4491da177e4SLinus Torvalds static int scsi_bus_match(struct device *dev, struct device_driver *gendrv) 4501da177e4SLinus Torvalds { 451b0ed4336SHannes Reinecke struct scsi_device *sdp; 452b0ed4336SHannes Reinecke 453b0ed4336SHannes Reinecke if (dev->type != &scsi_dev_type) 454b0ed4336SHannes Reinecke return 0; 455b0ed4336SHannes Reinecke 456b0ed4336SHannes Reinecke sdp = to_scsi_device(dev); 4571da177e4SLinus Torvalds if (sdp->no_uld_attach) 4581da177e4SLinus Torvalds return 0; 4591da177e4SLinus Torvalds return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0; 4601da177e4SLinus Torvalds } 4611da177e4SLinus Torvalds 4627eff2e7aSKay Sievers static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env) 463d7b8bcb0SMichael Tokarev { 4641f42ea7bSJames Bottomley struct scsi_device *sdev; 4651f42ea7bSJames Bottomley 4661f42ea7bSJames Bottomley if (dev->type != &scsi_dev_type) 4671f42ea7bSJames Bottomley return 0; 4681f42ea7bSJames Bottomley 4691f42ea7bSJames Bottomley sdev = to_scsi_device(dev); 470d7b8bcb0SMichael Tokarev 4717eff2e7aSKay Sievers add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type); 472d7b8bcb0SMichael Tokarev return 0; 473d7b8bcb0SMichael Tokarev } 474d7b8bcb0SMichael Tokarev 4751da177e4SLinus Torvalds struct bus_type scsi_bus_type = { 4761da177e4SLinus Torvalds .name = "scsi", 4771da177e4SLinus Torvalds .match = scsi_bus_match, 478d7b8bcb0SMichael Tokarev .uevent = scsi_bus_uevent, 479aa338601SRafael J. Wysocki #ifdef CONFIG_PM 480db5bd1e0SAlan Stern .pm = &scsi_bus_pm_ops, 481e6da54d8SAlan Stern #endif 4821da177e4SLinus Torvalds }; 483a6a8d9f8SChandra Seetharaman EXPORT_SYMBOL_GPL(scsi_bus_type); 4841da177e4SLinus Torvalds 4851da177e4SLinus Torvalds int scsi_sysfs_register(void) 4861da177e4SLinus Torvalds { 4871da177e4SLinus Torvalds int error; 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds error = bus_register(&scsi_bus_type); 4901da177e4SLinus Torvalds if (!error) { 4911da177e4SLinus Torvalds error = class_register(&sdev_class); 4921da177e4SLinus Torvalds if (error) 4931da177e4SLinus Torvalds bus_unregister(&scsi_bus_type); 4941da177e4SLinus Torvalds } 4951da177e4SLinus Torvalds 4961da177e4SLinus Torvalds return error; 4971da177e4SLinus Torvalds } 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds void scsi_sysfs_unregister(void) 5001da177e4SLinus Torvalds { 5011da177e4SLinus Torvalds class_unregister(&sdev_class); 5021da177e4SLinus Torvalds bus_unregister(&scsi_bus_type); 5031da177e4SLinus Torvalds } 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds /* 5061da177e4SLinus Torvalds * sdev_show_function: macro to create an attr function that can be used to 5071da177e4SLinus Torvalds * show a non-bit field. 5081da177e4SLinus Torvalds */ 5091da177e4SLinus Torvalds #define sdev_show_function(field, format_string) \ 5101da177e4SLinus Torvalds static ssize_t \ 511ee959b00STony Jones sdev_show_##field (struct device *dev, struct device_attribute *attr, \ 512ee959b00STony Jones char *buf) \ 5131da177e4SLinus Torvalds { \ 5141da177e4SLinus Torvalds struct scsi_device *sdev; \ 5151da177e4SLinus Torvalds sdev = to_scsi_device(dev); \ 5161da177e4SLinus Torvalds return snprintf (buf, 20, format_string, sdev->field); \ 5171da177e4SLinus Torvalds } \ 5181da177e4SLinus Torvalds 5191da177e4SLinus Torvalds /* 5201da177e4SLinus Torvalds * sdev_rd_attr: macro to create a function and attribute variable for a 5211da177e4SLinus Torvalds * read only field. 5221da177e4SLinus Torvalds */ 5231da177e4SLinus Torvalds #define sdev_rd_attr(field, format_string) \ 5241da177e4SLinus Torvalds sdev_show_function(field, format_string) \ 5251da177e4SLinus Torvalds static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL); 5261da177e4SLinus Torvalds 5271da177e4SLinus Torvalds 5281da177e4SLinus Torvalds /* 529160e7f67STomohiro Kusumi * sdev_rw_attr: create a function and attribute variable for a 5301da177e4SLinus Torvalds * read/write field. 5311da177e4SLinus Torvalds */ 5321da177e4SLinus Torvalds #define sdev_rw_attr(field, format_string) \ 5331da177e4SLinus Torvalds sdev_show_function(field, format_string) \ 5341da177e4SLinus Torvalds \ 5351da177e4SLinus Torvalds static ssize_t \ 536ee959b00STony Jones sdev_store_##field (struct device *dev, struct device_attribute *attr, \ 537ee959b00STony Jones const char *buf, size_t count) \ 5381da177e4SLinus Torvalds { \ 5391da177e4SLinus Torvalds struct scsi_device *sdev; \ 5401da177e4SLinus Torvalds sdev = to_scsi_device(dev); \ 541160e7f67STomohiro Kusumi sscanf (buf, format_string, &sdev->field); \ 5421da177e4SLinus Torvalds return count; \ 5431da177e4SLinus Torvalds } \ 5441da177e4SLinus Torvalds static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field); 5451da177e4SLinus Torvalds 5461da177e4SLinus Torvalds /* Currently we don't export bit fields, but we might in future, 5471da177e4SLinus Torvalds * so leave this code in */ 5481da177e4SLinus Torvalds #if 0 5491da177e4SLinus Torvalds /* 5501da177e4SLinus Torvalds * sdev_rd_attr: create a function and attribute variable for a 5511da177e4SLinus Torvalds * read/write bit field. 5521da177e4SLinus Torvalds */ 5531da177e4SLinus Torvalds #define sdev_rw_attr_bit(field) \ 5541da177e4SLinus Torvalds sdev_show_function(field, "%d\n") \ 5551da177e4SLinus Torvalds \ 5561da177e4SLinus Torvalds static ssize_t \ 557ee959b00STony Jones sdev_store_##field (struct device *dev, struct device_attribute *attr, \ 558ee959b00STony Jones const char *buf, size_t count) \ 5591da177e4SLinus Torvalds { \ 5601da177e4SLinus Torvalds int ret; \ 5611da177e4SLinus Torvalds struct scsi_device *sdev; \ 5621da177e4SLinus Torvalds ret = scsi_sdev_check_buf_bit(buf); \ 5631da177e4SLinus Torvalds if (ret >= 0) { \ 5641da177e4SLinus Torvalds sdev = to_scsi_device(dev); \ 5651da177e4SLinus Torvalds sdev->field = ret; \ 5661da177e4SLinus Torvalds ret = count; \ 5671da177e4SLinus Torvalds } \ 5681da177e4SLinus Torvalds return ret; \ 5691da177e4SLinus Torvalds } \ 5701da177e4SLinus Torvalds static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field); 5711da177e4SLinus Torvalds 5721da177e4SLinus Torvalds /* 5731da177e4SLinus Torvalds * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1", 5741da177e4SLinus Torvalds * else return -EINVAL. 5751da177e4SLinus Torvalds */ 5761da177e4SLinus Torvalds static int scsi_sdev_check_buf_bit(const char *buf) 5771da177e4SLinus Torvalds { 5781da177e4SLinus Torvalds if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) { 5791da177e4SLinus Torvalds if (buf[0] == '1') 5801da177e4SLinus Torvalds return 1; 5811da177e4SLinus Torvalds else if (buf[0] == '0') 5821da177e4SLinus Torvalds return 0; 5831da177e4SLinus Torvalds else 5841da177e4SLinus Torvalds return -EINVAL; 5851da177e4SLinus Torvalds } else 5861da177e4SLinus Torvalds return -EINVAL; 5871da177e4SLinus Torvalds } 5881da177e4SLinus Torvalds #endif 5891da177e4SLinus Torvalds /* 5901da177e4SLinus Torvalds * Create the actual show/store functions and data structures. 5911da177e4SLinus Torvalds */ 5921da177e4SLinus Torvalds sdev_rd_attr (type, "%d\n"); 5931da177e4SLinus Torvalds sdev_rd_attr (scsi_level, "%d\n"); 5941da177e4SLinus Torvalds sdev_rd_attr (vendor, "%.8s\n"); 5951da177e4SLinus Torvalds sdev_rd_attr (model, "%.16s\n"); 5961da177e4SLinus Torvalds sdev_rd_attr (rev, "%.4s\n"); 5971da177e4SLinus Torvalds 59871e75c97SChristoph Hellwig static ssize_t 59971e75c97SChristoph Hellwig sdev_show_device_busy(struct device *dev, struct device_attribute *attr, 60071e75c97SChristoph Hellwig char *buf) 60171e75c97SChristoph Hellwig { 60271e75c97SChristoph Hellwig struct scsi_device *sdev = to_scsi_device(dev); 60371e75c97SChristoph Hellwig return snprintf(buf, 20, "%d\n", atomic_read(&sdev->device_busy)); 60471e75c97SChristoph Hellwig } 60571e75c97SChristoph Hellwig static DEVICE_ATTR(device_busy, S_IRUGO, sdev_show_device_busy, NULL); 60671e75c97SChristoph Hellwig 607cd9070c9SChristoph Hellwig static ssize_t 608cd9070c9SChristoph Hellwig sdev_show_device_blocked(struct device *dev, struct device_attribute *attr, 609cd9070c9SChristoph Hellwig char *buf) 610cd9070c9SChristoph Hellwig { 611cd9070c9SChristoph Hellwig struct scsi_device *sdev = to_scsi_device(dev); 612cd9070c9SChristoph Hellwig return snprintf(buf, 20, "%d\n", atomic_read(&sdev->device_blocked)); 613cd9070c9SChristoph Hellwig } 614cd9070c9SChristoph Hellwig static DEVICE_ATTR(device_blocked, S_IRUGO, sdev_show_device_blocked, NULL); 615cd9070c9SChristoph Hellwig 616242f9dcbSJens Axboe /* 617242f9dcbSJens Axboe * TODO: can we make these symlinks to the block layer ones? 618242f9dcbSJens Axboe */ 6191da177e4SLinus Torvalds static ssize_t 62010523b3bSYani Ioannou sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf) 6211da177e4SLinus Torvalds { 6221da177e4SLinus Torvalds struct scsi_device *sdev; 6231da177e4SLinus Torvalds sdev = to_scsi_device(dev); 624242f9dcbSJens Axboe return snprintf(buf, 20, "%d\n", sdev->request_queue->rq_timeout / HZ); 6251da177e4SLinus Torvalds } 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds static ssize_t 628ee959b00STony Jones sdev_store_timeout (struct device *dev, struct device_attribute *attr, 629ee959b00STony Jones const char *buf, size_t count) 6301da177e4SLinus Torvalds { 6311da177e4SLinus Torvalds struct scsi_device *sdev; 6321da177e4SLinus Torvalds int timeout; 6331da177e4SLinus Torvalds sdev = to_scsi_device(dev); 6341da177e4SLinus Torvalds sscanf (buf, "%d\n", &timeout); 635242f9dcbSJens Axboe blk_queue_rq_timeout(sdev->request_queue, timeout * HZ); 6361da177e4SLinus Torvalds return count; 6371da177e4SLinus Torvalds } 6381da177e4SLinus Torvalds static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout); 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds static ssize_t 6410816c925SMartin K. Petersen sdev_show_eh_timeout(struct device *dev, struct device_attribute *attr, char *buf) 6420816c925SMartin K. Petersen { 6430816c925SMartin K. Petersen struct scsi_device *sdev; 6440816c925SMartin K. Petersen sdev = to_scsi_device(dev); 6450816c925SMartin K. Petersen return snprintf(buf, 20, "%u\n", sdev->eh_timeout / HZ); 6460816c925SMartin K. Petersen } 6470816c925SMartin K. Petersen 6480816c925SMartin K. Petersen static ssize_t 6490816c925SMartin K. Petersen sdev_store_eh_timeout(struct device *dev, struct device_attribute *attr, 6500816c925SMartin K. Petersen const char *buf, size_t count) 6510816c925SMartin K. Petersen { 6520816c925SMartin K. Petersen struct scsi_device *sdev; 6530816c925SMartin K. Petersen unsigned int eh_timeout; 6540816c925SMartin K. Petersen int err; 6550816c925SMartin K. Petersen 6560816c925SMartin K. Petersen if (!capable(CAP_SYS_ADMIN)) 6570816c925SMartin K. Petersen return -EACCES; 6580816c925SMartin K. Petersen 6590816c925SMartin K. Petersen sdev = to_scsi_device(dev); 6600816c925SMartin K. Petersen err = kstrtouint(buf, 10, &eh_timeout); 6610816c925SMartin K. Petersen if (err) 6620816c925SMartin K. Petersen return err; 6630816c925SMartin K. Petersen sdev->eh_timeout = eh_timeout * HZ; 6640816c925SMartin K. Petersen 6650816c925SMartin K. Petersen return count; 6660816c925SMartin K. Petersen } 6670816c925SMartin K. Petersen static DEVICE_ATTR(eh_timeout, S_IRUGO | S_IWUSR, sdev_show_eh_timeout, sdev_store_eh_timeout); 6680816c925SMartin K. Petersen 6690816c925SMartin K. Petersen static ssize_t 670ee959b00STony Jones store_rescan_field (struct device *dev, struct device_attribute *attr, 671ee959b00STony Jones const char *buf, size_t count) 6721da177e4SLinus Torvalds { 6731da177e4SLinus Torvalds scsi_rescan_device(dev); 6741da177e4SLinus Torvalds return count; 6751da177e4SLinus Torvalds } 6761da177e4SLinus Torvalds static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field); 6771da177e4SLinus Torvalds 678ee959b00STony Jones static ssize_t 679ee959b00STony Jones sdev_store_delete(struct device *dev, struct device_attribute *attr, 680ee959b00STony Jones const char *buf, size_t count) 6811da177e4SLinus Torvalds { 682ac0ece91STejun Heo if (device_remove_file_self(dev, attr)) 683ac0ece91STejun Heo scsi_remove_device(to_scsi_device(dev)); 6841da177e4SLinus Torvalds return count; 6851da177e4SLinus Torvalds }; 6861da177e4SLinus Torvalds static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete); 6871da177e4SLinus Torvalds 6881da177e4SLinus Torvalds static ssize_t 689ee959b00STony Jones store_state_field(struct device *dev, struct device_attribute *attr, 690ee959b00STony Jones const char *buf, size_t count) 6911da177e4SLinus Torvalds { 6921da177e4SLinus Torvalds int i; 6931da177e4SLinus Torvalds struct scsi_device *sdev = to_scsi_device(dev); 6941da177e4SLinus Torvalds enum scsi_device_state state = 0; 6951da177e4SLinus Torvalds 6966391a113STobias Klauser for (i = 0; i < ARRAY_SIZE(sdev_states); i++) { 6971da177e4SLinus Torvalds const int len = strlen(sdev_states[i].name); 6981da177e4SLinus Torvalds if (strncmp(sdev_states[i].name, buf, len) == 0 && 6991da177e4SLinus Torvalds buf[len] == '\n') { 7001da177e4SLinus Torvalds state = sdev_states[i].value; 7011da177e4SLinus Torvalds break; 7021da177e4SLinus Torvalds } 7031da177e4SLinus Torvalds } 7041da177e4SLinus Torvalds if (!state) 7051da177e4SLinus Torvalds return -EINVAL; 7061da177e4SLinus Torvalds 7071da177e4SLinus Torvalds if (scsi_device_set_state(sdev, state)) 7081da177e4SLinus Torvalds return -EINVAL; 7091da177e4SLinus Torvalds return count; 7101da177e4SLinus Torvalds } 7111da177e4SLinus Torvalds 7121da177e4SLinus Torvalds static ssize_t 71310523b3bSYani Ioannou show_state_field(struct device *dev, struct device_attribute *attr, char *buf) 7141da177e4SLinus Torvalds { 7151da177e4SLinus Torvalds struct scsi_device *sdev = to_scsi_device(dev); 7161da177e4SLinus Torvalds const char *name = scsi_device_state_name(sdev->sdev_state); 7171da177e4SLinus Torvalds 7181da177e4SLinus Torvalds if (!name) 7191da177e4SLinus Torvalds return -EINVAL; 7201da177e4SLinus Torvalds 7211da177e4SLinus Torvalds return snprintf(buf, 20, "%s\n", name); 7221da177e4SLinus Torvalds } 7231da177e4SLinus Torvalds 7241da177e4SLinus Torvalds static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field); 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds static ssize_t 727ee959b00STony Jones show_queue_type_field(struct device *dev, struct device_attribute *attr, 728ee959b00STony Jones char *buf) 7291da177e4SLinus Torvalds { 7301da177e4SLinus Torvalds struct scsi_device *sdev = to_scsi_device(dev); 7311da177e4SLinus Torvalds const char *name = "none"; 7321da177e4SLinus Torvalds 733609aa22fSChristoph Hellwig if (sdev->simple_tags) 7341da177e4SLinus Torvalds name = "simple"; 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds return snprintf(buf, 20, "%s\n", name); 7371da177e4SLinus Torvalds } 7381da177e4SLinus Torvalds 739276b20d0SHannes Reinecke static ssize_t 740276b20d0SHannes Reinecke store_queue_type_field(struct device *dev, struct device_attribute *attr, 741276b20d0SHannes Reinecke const char *buf, size_t count) 742276b20d0SHannes Reinecke { 743276b20d0SHannes Reinecke struct scsi_device *sdev = to_scsi_device(dev); 744276b20d0SHannes Reinecke 745efc3c1dfSChristoph Hellwig if (!sdev->tagged_supported) 746276b20d0SHannes Reinecke return -EINVAL; 747276b20d0SHannes Reinecke 748efc3c1dfSChristoph Hellwig sdev_printk(KERN_INFO, sdev, 749efc3c1dfSChristoph Hellwig "ignoring write to deprecated queue_type attribute"); 750276b20d0SHannes Reinecke return count; 751276b20d0SHannes Reinecke } 752276b20d0SHannes Reinecke 753276b20d0SHannes Reinecke static DEVICE_ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field, 754276b20d0SHannes Reinecke store_queue_type_field); 7551da177e4SLinus Torvalds 756b3ae8780SHannes Reinecke #define sdev_vpd_pg_attr(_page) \ 757b3ae8780SHannes Reinecke static ssize_t \ 758b3ae8780SHannes Reinecke show_vpd_##_page(struct file *filp, struct kobject *kobj, \ 759b3ae8780SHannes Reinecke struct bin_attribute *bin_attr, \ 760b3ae8780SHannes Reinecke char *buf, loff_t off, size_t count) \ 761b3ae8780SHannes Reinecke { \ 762b3ae8780SHannes Reinecke struct device *dev = container_of(kobj, struct device, kobj); \ 763b3ae8780SHannes Reinecke struct scsi_device *sdev = to_scsi_device(dev); \ 76409e2b0b1SHannes Reinecke int ret; \ 765b3ae8780SHannes Reinecke if (!sdev->vpd_##_page) \ 766b3ae8780SHannes Reinecke return -EINVAL; \ 76709e2b0b1SHannes Reinecke rcu_read_lock(); \ 76809e2b0b1SHannes Reinecke ret = memory_read_from_buffer(buf, count, &off, \ 76909e2b0b1SHannes Reinecke rcu_dereference(sdev->vpd_##_page), \ 770b3ae8780SHannes Reinecke sdev->vpd_##_page##_len); \ 77109e2b0b1SHannes Reinecke rcu_read_unlock(); \ 77209e2b0b1SHannes Reinecke return ret; \ 773b3ae8780SHannes Reinecke } \ 774b3ae8780SHannes Reinecke static struct bin_attribute dev_attr_vpd_##_page = { \ 775b3ae8780SHannes Reinecke .attr = {.name = __stringify(vpd_##_page), .mode = S_IRUGO }, \ 776b3ae8780SHannes Reinecke .size = 0, \ 777b3ae8780SHannes Reinecke .read = show_vpd_##_page, \ 778b3ae8780SHannes Reinecke }; 779b3ae8780SHannes Reinecke 780b3ae8780SHannes Reinecke sdev_vpd_pg_attr(pg83); 781b3ae8780SHannes Reinecke sdev_vpd_pg_attr(pg80); 7821da177e4SLinus Torvalds 78392e6246cSJohannes Thumshirn static ssize_t show_inquiry(struct file *filep, struct kobject *kobj, 78492e6246cSJohannes Thumshirn struct bin_attribute *bin_attr, 78592e6246cSJohannes Thumshirn char *buf, loff_t off, size_t count) 78692e6246cSJohannes Thumshirn { 78792e6246cSJohannes Thumshirn struct device *dev = container_of(kobj, struct device, kobj); 78892e6246cSJohannes Thumshirn struct scsi_device *sdev = to_scsi_device(dev); 78992e6246cSJohannes Thumshirn 79092e6246cSJohannes Thumshirn if (!sdev->inquiry) 79192e6246cSJohannes Thumshirn return -EINVAL; 79292e6246cSJohannes Thumshirn 79392e6246cSJohannes Thumshirn return memory_read_from_buffer(buf, count, &off, sdev->inquiry, 79492e6246cSJohannes Thumshirn sdev->inquiry_len); 79592e6246cSJohannes Thumshirn } 79692e6246cSJohannes Thumshirn 79792e6246cSJohannes Thumshirn static struct bin_attribute dev_attr_inquiry = { 79892e6246cSJohannes Thumshirn .attr = { 79992e6246cSJohannes Thumshirn .name = "inquiry", 80092e6246cSJohannes Thumshirn .mode = S_IRUGO, 80192e6246cSJohannes Thumshirn }, 80292e6246cSJohannes Thumshirn .size = 0, 80392e6246cSJohannes Thumshirn .read = show_inquiry, 80492e6246cSJohannes Thumshirn }; 80592e6246cSJohannes Thumshirn 8061da177e4SLinus Torvalds static ssize_t 807b3ae8780SHannes Reinecke show_iostat_counterbits(struct device *dev, struct device_attribute *attr, 808b3ae8780SHannes Reinecke char *buf) 8091da177e4SLinus Torvalds { 8101da177e4SLinus Torvalds return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8); 8111da177e4SLinus Torvalds } 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL); 8141da177e4SLinus Torvalds 8151da177e4SLinus Torvalds #define show_sdev_iostat(field) \ 8161da177e4SLinus Torvalds static ssize_t \ 817ee959b00STony Jones show_iostat_##field(struct device *dev, struct device_attribute *attr, \ 818ee959b00STony Jones char *buf) \ 8191da177e4SLinus Torvalds { \ 8201da177e4SLinus Torvalds struct scsi_device *sdev = to_scsi_device(dev); \ 8211da177e4SLinus Torvalds unsigned long long count = atomic_read(&sdev->field); \ 8221da177e4SLinus Torvalds return snprintf(buf, 20, "0x%llx\n", count); \ 8231da177e4SLinus Torvalds } \ 8241da177e4SLinus Torvalds static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL) 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds show_sdev_iostat(iorequest_cnt); 8271da177e4SLinus Torvalds show_sdev_iostat(iodone_cnt); 8281da177e4SLinus Torvalds show_sdev_iostat(ioerr_cnt); 8291da177e4SLinus Torvalds 830d7b8bcb0SMichael Tokarev static ssize_t 831d7b8bcb0SMichael Tokarev sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf) 832d7b8bcb0SMichael Tokarev { 833d7b8bcb0SMichael Tokarev struct scsi_device *sdev; 834d7b8bcb0SMichael Tokarev sdev = to_scsi_device(dev); 835d7b8bcb0SMichael Tokarev return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type); 836d7b8bcb0SMichael Tokarev } 837d7b8bcb0SMichael Tokarev static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL); 8381da177e4SLinus Torvalds 839a341cd0fSJeff Garzik #define DECLARE_EVT_SHOW(name, Cap_name) \ 840a341cd0fSJeff Garzik static ssize_t \ 841a341cd0fSJeff Garzik sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \ 842a341cd0fSJeff Garzik char *buf) \ 843a341cd0fSJeff Garzik { \ 844a341cd0fSJeff Garzik struct scsi_device *sdev = to_scsi_device(dev); \ 845a341cd0fSJeff Garzik int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\ 846a341cd0fSJeff Garzik return snprintf(buf, 20, "%d\n", val); \ 847a341cd0fSJeff Garzik } 848a341cd0fSJeff Garzik 849a341cd0fSJeff Garzik #define DECLARE_EVT_STORE(name, Cap_name) \ 850a341cd0fSJeff Garzik static ssize_t \ 851a341cd0fSJeff Garzik sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\ 852a341cd0fSJeff Garzik const char *buf, size_t count) \ 853a341cd0fSJeff Garzik { \ 854a341cd0fSJeff Garzik struct scsi_device *sdev = to_scsi_device(dev); \ 855a341cd0fSJeff Garzik int val = simple_strtoul(buf, NULL, 0); \ 856a341cd0fSJeff Garzik if (val == 0) \ 857a341cd0fSJeff Garzik clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \ 858a341cd0fSJeff Garzik else if (val == 1) \ 859a341cd0fSJeff Garzik set_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \ 860a341cd0fSJeff Garzik else \ 861a341cd0fSJeff Garzik return -EINVAL; \ 862a341cd0fSJeff Garzik return count; \ 863a341cd0fSJeff Garzik } 864a341cd0fSJeff Garzik 865a341cd0fSJeff Garzik #define DECLARE_EVT(name, Cap_name) \ 866a341cd0fSJeff Garzik DECLARE_EVT_SHOW(name, Cap_name) \ 867a341cd0fSJeff Garzik DECLARE_EVT_STORE(name, Cap_name) \ 868a341cd0fSJeff Garzik static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name, \ 869a341cd0fSJeff Garzik sdev_store_evt_##name); 870a341cd0fSJeff Garzik #define REF_EVT(name) &dev_attr_evt_##name.attr 871a341cd0fSJeff Garzik 872a341cd0fSJeff Garzik DECLARE_EVT(media_change, MEDIA_CHANGE) 873279afdfeSEwan D. Milne DECLARE_EVT(inquiry_change_reported, INQUIRY_CHANGE_REPORTED) 874279afdfeSEwan D. Milne DECLARE_EVT(capacity_change_reported, CAPACITY_CHANGE_REPORTED) 875279afdfeSEwan D. Milne DECLARE_EVT(soft_threshold_reached, SOFT_THRESHOLD_REACHED_REPORTED) 876279afdfeSEwan D. Milne DECLARE_EVT(mode_parameter_change_reported, MODE_PARAMETER_CHANGE_REPORTED) 877279afdfeSEwan D. Milne DECLARE_EVT(lun_change_reported, LUN_CHANGE_REPORTED) 878a341cd0fSJeff Garzik 879ee959b00STony Jones static ssize_t 880276b20d0SHannes Reinecke sdev_store_queue_depth(struct device *dev, struct device_attribute *attr, 881ee959b00STony Jones const char *buf, size_t count) 8821da177e4SLinus Torvalds { 8831da177e4SLinus Torvalds int depth, retval; 8841da177e4SLinus Torvalds struct scsi_device *sdev = to_scsi_device(dev); 8851da177e4SLinus Torvalds struct scsi_host_template *sht = sdev->host->hostt; 8861da177e4SLinus Torvalds 8871da177e4SLinus Torvalds if (!sht->change_queue_depth) 8881da177e4SLinus Torvalds return -EINVAL; 8891da177e4SLinus Torvalds 8901da177e4SLinus Torvalds depth = simple_strtoul(buf, NULL, 0); 8911da177e4SLinus Torvalds 8921278dd68SJens Axboe if (depth < 1 || depth > sdev->host->can_queue) 8931da177e4SLinus Torvalds return -EINVAL; 8941da177e4SLinus Torvalds 895db5ed4dfSChristoph Hellwig retval = sht->change_queue_depth(sdev, depth); 8961da177e4SLinus Torvalds if (retval < 0) 8971da177e4SLinus Torvalds return retval; 8981da177e4SLinus Torvalds 8994a84067dSVasu Dev sdev->max_queue_depth = sdev->queue_depth; 9004a84067dSVasu Dev 9011da177e4SLinus Torvalds return count; 9021da177e4SLinus Torvalds } 903276b20d0SHannes Reinecke sdev_show_function(queue_depth, "%d\n"); 9041da177e4SLinus Torvalds 905276b20d0SHannes Reinecke static DEVICE_ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth, 906276b20d0SHannes Reinecke sdev_store_queue_depth); 9071da177e4SLinus Torvalds 908*248d4fe9SHannes Reinecke static ssize_t 909*248d4fe9SHannes Reinecke sdev_show_wwid(struct device *dev, struct device_attribute *attr, 910*248d4fe9SHannes Reinecke char *buf) 911*248d4fe9SHannes Reinecke { 912*248d4fe9SHannes Reinecke struct scsi_device *sdev = to_scsi_device(dev); 913*248d4fe9SHannes Reinecke ssize_t count; 914*248d4fe9SHannes Reinecke 915*248d4fe9SHannes Reinecke count = scsi_vpd_lun_id(sdev, buf, PAGE_SIZE); 916*248d4fe9SHannes Reinecke if (count > 0) { 917*248d4fe9SHannes Reinecke buf[count] = '\n'; 918*248d4fe9SHannes Reinecke count++; 919*248d4fe9SHannes Reinecke } 920*248d4fe9SHannes Reinecke return count; 921*248d4fe9SHannes Reinecke } 922*248d4fe9SHannes Reinecke static DEVICE_ATTR(wwid, S_IRUGO, sdev_show_wwid, NULL); 923*248d4fe9SHannes Reinecke 92441f95dd2SHannes Reinecke #ifdef CONFIG_SCSI_DH 92541f95dd2SHannes Reinecke static ssize_t 92641f95dd2SHannes Reinecke sdev_show_dh_state(struct device *dev, struct device_attribute *attr, 92741f95dd2SHannes Reinecke char *buf) 92841f95dd2SHannes Reinecke { 92941f95dd2SHannes Reinecke struct scsi_device *sdev = to_scsi_device(dev); 93041f95dd2SHannes Reinecke 93141f95dd2SHannes Reinecke if (!sdev->handler) 93241f95dd2SHannes Reinecke return snprintf(buf, 20, "detached\n"); 93341f95dd2SHannes Reinecke 93441f95dd2SHannes Reinecke return snprintf(buf, 20, "%s\n", sdev->handler->name); 93541f95dd2SHannes Reinecke } 93641f95dd2SHannes Reinecke 93741f95dd2SHannes Reinecke static ssize_t 93841f95dd2SHannes Reinecke sdev_store_dh_state(struct device *dev, struct device_attribute *attr, 93941f95dd2SHannes Reinecke const char *buf, size_t count) 94041f95dd2SHannes Reinecke { 94141f95dd2SHannes Reinecke struct scsi_device *sdev = to_scsi_device(dev); 94241f95dd2SHannes Reinecke int err = -EINVAL; 94341f95dd2SHannes Reinecke 94441f95dd2SHannes Reinecke if (sdev->sdev_state == SDEV_CANCEL || 94541f95dd2SHannes Reinecke sdev->sdev_state == SDEV_DEL) 94641f95dd2SHannes Reinecke return -ENODEV; 94741f95dd2SHannes Reinecke 94841f95dd2SHannes Reinecke if (!sdev->handler) { 94941f95dd2SHannes Reinecke /* 95041f95dd2SHannes Reinecke * Attach to a device handler 95141f95dd2SHannes Reinecke */ 95241f95dd2SHannes Reinecke err = scsi_dh_attach(sdev->request_queue, buf); 95341f95dd2SHannes Reinecke } else if (!strncmp(buf, "activate", 8)) { 95441f95dd2SHannes Reinecke /* 95541f95dd2SHannes Reinecke * Activate a device handler 95641f95dd2SHannes Reinecke */ 95741f95dd2SHannes Reinecke if (sdev->handler->activate) 95841f95dd2SHannes Reinecke err = sdev->handler->activate(sdev, NULL, NULL); 95941f95dd2SHannes Reinecke else 96041f95dd2SHannes Reinecke err = 0; 96141f95dd2SHannes Reinecke } else if (!strncmp(buf, "detach", 6)) { 96241f95dd2SHannes Reinecke /* 96341f95dd2SHannes Reinecke * Detach from a device handler 96441f95dd2SHannes Reinecke */ 96541f95dd2SHannes Reinecke sdev_printk(KERN_WARNING, sdev, 96641f95dd2SHannes Reinecke "can't detach handler %s.\n", 96741f95dd2SHannes Reinecke sdev->handler->name); 96841f95dd2SHannes Reinecke err = -EINVAL; 96941f95dd2SHannes Reinecke } 97041f95dd2SHannes Reinecke 97141f95dd2SHannes Reinecke return err < 0 ? err : count; 97241f95dd2SHannes Reinecke } 97341f95dd2SHannes Reinecke 97441f95dd2SHannes Reinecke static DEVICE_ATTR(dh_state, S_IRUGO | S_IWUSR, sdev_show_dh_state, 97541f95dd2SHannes Reinecke sdev_store_dh_state); 97641f95dd2SHannes Reinecke #endif 97741f95dd2SHannes Reinecke 978ee959b00STony Jones static ssize_t 9794a84067dSVasu Dev sdev_show_queue_ramp_up_period(struct device *dev, 9804a84067dSVasu Dev struct device_attribute *attr, 9814a84067dSVasu Dev char *buf) 9824a84067dSVasu Dev { 9834a84067dSVasu Dev struct scsi_device *sdev; 9844a84067dSVasu Dev sdev = to_scsi_device(dev); 9854a84067dSVasu Dev return snprintf(buf, 20, "%u\n", 9864a84067dSVasu Dev jiffies_to_msecs(sdev->queue_ramp_up_period)); 9874a84067dSVasu Dev } 9884a84067dSVasu Dev 9894a84067dSVasu Dev static ssize_t 9904a84067dSVasu Dev sdev_store_queue_ramp_up_period(struct device *dev, 9914a84067dSVasu Dev struct device_attribute *attr, 9924a84067dSVasu Dev const char *buf, size_t count) 9934a84067dSVasu Dev { 9944a84067dSVasu Dev struct scsi_device *sdev = to_scsi_device(dev); 995f7c65af5SDaniel Walter unsigned int period; 9964a84067dSVasu Dev 997f7c65af5SDaniel Walter if (kstrtouint(buf, 10, &period)) 9984a84067dSVasu Dev return -EINVAL; 9994a84067dSVasu Dev 10004a84067dSVasu Dev sdev->queue_ramp_up_period = msecs_to_jiffies(period); 10015cb9b40dSPeter Oberparleiter return count; 10024a84067dSVasu Dev } 10034a84067dSVasu Dev 1004276b20d0SHannes Reinecke static DEVICE_ATTR(queue_ramp_up_period, S_IRUGO | S_IWUSR, 10054a84067dSVasu Dev sdev_show_queue_ramp_up_period, 10064a84067dSVasu Dev sdev_store_queue_ramp_up_period); 10074a84067dSVasu Dev 1008276b20d0SHannes Reinecke static umode_t scsi_sdev_attr_is_visible(struct kobject *kobj, 1009276b20d0SHannes Reinecke struct attribute *attr, int i) 10101da177e4SLinus Torvalds { 1011276b20d0SHannes Reinecke struct device *dev = container_of(kobj, struct device, kobj); 10121da177e4SLinus Torvalds struct scsi_device *sdev = to_scsi_device(dev); 10131da177e4SLinus Torvalds 10141da177e4SLinus Torvalds 1015276b20d0SHannes Reinecke if (attr == &dev_attr_queue_depth.attr && 1016276b20d0SHannes Reinecke !sdev->host->hostt->change_queue_depth) 1017276b20d0SHannes Reinecke return S_IRUGO; 10181da177e4SLinus Torvalds 1019276b20d0SHannes Reinecke if (attr == &dev_attr_queue_ramp_up_period.attr && 1020276b20d0SHannes Reinecke !sdev->host->hostt->change_queue_depth) 1021276b20d0SHannes Reinecke return 0; 10221da177e4SLinus Torvalds 1023276b20d0SHannes Reinecke return attr->mode; 10241da177e4SLinus Torvalds } 10251da177e4SLinus Torvalds 1026276b20d0SHannes Reinecke /* Default template for device attributes. May NOT be modified */ 1027276b20d0SHannes Reinecke static struct attribute *scsi_sdev_attrs[] = { 1028276b20d0SHannes Reinecke &dev_attr_device_blocked.attr, 1029276b20d0SHannes Reinecke &dev_attr_type.attr, 1030276b20d0SHannes Reinecke &dev_attr_scsi_level.attr, 1031276b20d0SHannes Reinecke &dev_attr_device_busy.attr, 1032276b20d0SHannes Reinecke &dev_attr_vendor.attr, 1033276b20d0SHannes Reinecke &dev_attr_model.attr, 1034276b20d0SHannes Reinecke &dev_attr_rev.attr, 1035276b20d0SHannes Reinecke &dev_attr_rescan.attr, 1036276b20d0SHannes Reinecke &dev_attr_delete.attr, 1037276b20d0SHannes Reinecke &dev_attr_state.attr, 1038276b20d0SHannes Reinecke &dev_attr_timeout.attr, 1039276b20d0SHannes Reinecke &dev_attr_eh_timeout.attr, 1040276b20d0SHannes Reinecke &dev_attr_iocounterbits.attr, 1041276b20d0SHannes Reinecke &dev_attr_iorequest_cnt.attr, 1042276b20d0SHannes Reinecke &dev_attr_iodone_cnt.attr, 1043276b20d0SHannes Reinecke &dev_attr_ioerr_cnt.attr, 1044276b20d0SHannes Reinecke &dev_attr_modalias.attr, 1045276b20d0SHannes Reinecke &dev_attr_queue_depth.attr, 1046276b20d0SHannes Reinecke &dev_attr_queue_type.attr, 1047*248d4fe9SHannes Reinecke &dev_attr_wwid.attr, 104841f95dd2SHannes Reinecke #ifdef CONFIG_SCSI_DH 104941f95dd2SHannes Reinecke &dev_attr_dh_state.attr, 105041f95dd2SHannes Reinecke #endif 1051276b20d0SHannes Reinecke &dev_attr_queue_ramp_up_period.attr, 1052276b20d0SHannes Reinecke REF_EVT(media_change), 1053276b20d0SHannes Reinecke REF_EVT(inquiry_change_reported), 1054276b20d0SHannes Reinecke REF_EVT(capacity_change_reported), 1055276b20d0SHannes Reinecke REF_EVT(soft_threshold_reached), 1056276b20d0SHannes Reinecke REF_EVT(mode_parameter_change_reported), 1057276b20d0SHannes Reinecke REF_EVT(lun_change_reported), 1058276b20d0SHannes Reinecke NULL 1059276b20d0SHannes Reinecke }; 1060276b20d0SHannes Reinecke 1061b3ae8780SHannes Reinecke static struct bin_attribute *scsi_sdev_bin_attrs[] = { 1062b3ae8780SHannes Reinecke &dev_attr_vpd_pg83, 1063b3ae8780SHannes Reinecke &dev_attr_vpd_pg80, 106492e6246cSJohannes Thumshirn &dev_attr_inquiry, 1065b3ae8780SHannes Reinecke NULL 1066b3ae8780SHannes Reinecke }; 1067276b20d0SHannes Reinecke static struct attribute_group scsi_sdev_attr_group = { 1068276b20d0SHannes Reinecke .attrs = scsi_sdev_attrs, 1069b3ae8780SHannes Reinecke .bin_attrs = scsi_sdev_bin_attrs, 1070276b20d0SHannes Reinecke .is_visible = scsi_sdev_attr_is_visible, 1071276b20d0SHannes Reinecke }; 1072276b20d0SHannes Reinecke 1073276b20d0SHannes Reinecke static const struct attribute_group *scsi_sdev_attr_groups[] = { 1074276b20d0SHannes Reinecke &scsi_sdev_attr_group, 1075276b20d0SHannes Reinecke NULL 1076276b20d0SHannes Reinecke }; 1077276b20d0SHannes Reinecke 1078643eb2d9SJames Bottomley static int scsi_target_add(struct scsi_target *starget) 1079643eb2d9SJames Bottomley { 1080643eb2d9SJames Bottomley int error; 1081643eb2d9SJames Bottomley 1082643eb2d9SJames Bottomley if (starget->state != STARGET_CREATED) 1083643eb2d9SJames Bottomley return 0; 1084643eb2d9SJames Bottomley 1085643eb2d9SJames Bottomley error = device_add(&starget->dev); 1086643eb2d9SJames Bottomley if (error) { 1087643eb2d9SJames Bottomley dev_err(&starget->dev, "target device_add failed, error %d\n", error); 1088643eb2d9SJames Bottomley return error; 1089643eb2d9SJames Bottomley } 1090643eb2d9SJames Bottomley transport_add_device(&starget->dev); 1091643eb2d9SJames Bottomley starget->state = STARGET_RUNNING; 1092643eb2d9SJames Bottomley 1093bc4f2401SAlan Stern pm_runtime_set_active(&starget->dev); 1094bc4f2401SAlan Stern pm_runtime_enable(&starget->dev); 1095bc4f2401SAlan Stern device_enable_async_suspend(&starget->dev); 1096bc4f2401SAlan Stern 1097643eb2d9SJames Bottomley return 0; 1098643eb2d9SJames Bottomley } 1099643eb2d9SJames Bottomley 11001da177e4SLinus Torvalds /** 11011da177e4SLinus Torvalds * scsi_sysfs_add_sdev - add scsi device to sysfs 11021da177e4SLinus Torvalds * @sdev: scsi_device to add 11031da177e4SLinus Torvalds * 11041da177e4SLinus Torvalds * Return value: 11051da177e4SLinus Torvalds * 0 on Success / non-zero on Failure 11061da177e4SLinus Torvalds **/ 11071da177e4SLinus Torvalds int scsi_sysfs_add_sdev(struct scsi_device *sdev) 11081da177e4SLinus Torvalds { 11091da177e4SLinus Torvalds int error, i; 111080ed71ceSJames Bottomley struct request_queue *rq = sdev->request_queue; 1111643eb2d9SJames Bottomley struct scsi_target *starget = sdev->sdev_target; 11121da177e4SLinus Torvalds 1113ee37e09dSAlan Stern error = scsi_device_set_state(sdev, SDEV_RUNNING); 1114ee37e09dSAlan Stern if (error) 11151da177e4SLinus Torvalds return error; 11161da177e4SLinus Torvalds 1117643eb2d9SJames Bottomley error = scsi_target_add(starget); 1118643eb2d9SJames Bottomley if (error) 1119643eb2d9SJames Bottomley return error; 1120643eb2d9SJames Bottomley 1121643eb2d9SJames Bottomley transport_configure_device(&starget->dev); 1122bc4f2401SAlan Stern 11234cb077d9SRafael J. Wysocki device_enable_async_suspend(&sdev->sdev_gendev); 1124bc4f2401SAlan Stern scsi_autopm_get_target(starget); 1125bc4f2401SAlan Stern pm_runtime_set_active(&sdev->sdev_gendev); 1126bc4f2401SAlan Stern pm_runtime_forbid(&sdev->sdev_gendev); 1127bc4f2401SAlan Stern pm_runtime_enable(&sdev->sdev_gendev); 1128bc4f2401SAlan Stern scsi_autopm_put_target(starget); 1129bc4f2401SAlan Stern 1130bc4f2401SAlan Stern scsi_autopm_get_device(sdev); 1131bc4f2401SAlan Stern 11321da177e4SLinus Torvalds error = device_add(&sdev->sdev_gendev); 11331da177e4SLinus Torvalds if (error) { 113473d8c34fSAlan Stern sdev_printk(KERN_INFO, sdev, 113573d8c34fSAlan Stern "failed to add device: %d\n", error); 1136ee37e09dSAlan Stern return error; 11371da177e4SLinus Torvalds } 1138086b91d0SChristoph Hellwig 1139086b91d0SChristoph Hellwig error = scsi_dh_add_device(sdev); 1140221255aeSHannes Reinecke if (error) 1141221255aeSHannes Reinecke /* 1142221255aeSHannes Reinecke * device_handler is optional, so any error can be ignored 1143221255aeSHannes Reinecke */ 1144086b91d0SChristoph Hellwig sdev_printk(KERN_INFO, sdev, 1145086b91d0SChristoph Hellwig "failed to add device handler: %d\n", error); 1146086b91d0SChristoph Hellwig 11474cb077d9SRafael J. Wysocki device_enable_async_suspend(&sdev->sdev_dev); 1148ee959b00STony Jones error = device_add(&sdev->sdev_dev); 11491da177e4SLinus Torvalds if (error) { 115073d8c34fSAlan Stern sdev_printk(KERN_INFO, sdev, 115173d8c34fSAlan Stern "failed to add class device: %d\n", error); 1152086b91d0SChristoph Hellwig scsi_dh_remove_device(sdev); 1153860dc736SJames Bottomley device_del(&sdev->sdev_gendev); 1154ee37e09dSAlan Stern return error; 11551da177e4SLinus Torvalds } 1156860dc736SJames Bottomley transport_add_device(&sdev->sdev_gendev); 1157860dc736SJames Bottomley sdev->is_visible = 1; 11581da177e4SLinus Torvalds 115997f46ae4SFUJITA Tomonori error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL, NULL); 116080ed71ceSJames Bottomley 116180ed71ceSJames Bottomley if (error) 1162860dc736SJames Bottomley /* we're treating error on bsg register as non-fatal, 1163860dc736SJames Bottomley * so pretend nothing went wrong */ 116480ed71ceSJames Bottomley sdev_printk(KERN_INFO, sdev, 116580ed71ceSJames Bottomley "Failed to register bsg queue, errno=%d\n", error); 116680ed71ceSJames Bottomley 1167bfd12944SKay Sievers /* add additional host specific attributes */ 11681da177e4SLinus Torvalds if (sdev->host->hostt->sdev_attrs) { 11691da177e4SLinus Torvalds for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) { 1170bfd12944SKay Sievers error = device_create_file(&sdev->sdev_gendev, 11711da177e4SLinus Torvalds sdev->host->hostt->sdev_attrs[i]); 1172860dc736SJames Bottomley if (error) 11731da177e4SLinus Torvalds return error; 1174ee37e09dSAlan Stern } 1175ee37e09dSAlan Stern } 11761da177e4SLinus Torvalds 11776fe8c1dbSSubhash Jadavani scsi_autopm_put_device(sdev); 1178ee37e09dSAlan Stern return error; 11791da177e4SLinus Torvalds } 11801da177e4SLinus Torvalds 1181903f4fedSAlan Stern void __scsi_remove_device(struct scsi_device *sdev) 11821da177e4SLinus Torvalds { 1183df133c21SJames Bottomley struct device *dev = &sdev->sdev_gendev; 1184df133c21SJames Bottomley 1185860dc736SJames Bottomley if (sdev->is_visible) { 11861da177e4SLinus Torvalds if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0) 1187903f4fedSAlan Stern return; 11881da177e4SLinus Torvalds 11890feed274SJames Bottomley bsg_unregister_queue(sdev->request_queue); 1190ee959b00STony Jones device_unregister(&sdev->sdev_dev); 1191df133c21SJames Bottomley transport_remove_device(dev); 1192086b91d0SChristoph Hellwig scsi_dh_remove_device(sdev); 1193bf2cf3baSBart Van Assche } 1194b485462aSBart Van Assche 1195b485462aSBart Van Assche /* 1196b485462aSBart Van Assche * Stop accepting new requests and wait until all queuecommand() and 1197b485462aSBart Van Assche * scsi_run_queue() invocations have finished before tearing down the 1198b485462aSBart Van Assche * device. 1199b485462aSBart Van Assche */ 12001da177e4SLinus Torvalds scsi_device_set_state(sdev, SDEV_DEL); 1201b485462aSBart Van Assche blk_cleanup_queue(sdev->request_queue); 1202b485462aSBart Van Assche cancel_work_sync(&sdev->requeue_work); 1203b485462aSBart Van Assche 1204bf2cf3baSBart Van Assche /* 1205bf2cf3baSBart Van Assche * Remove the device after blk_cleanup_queue() has been called such 1206bf2cf3baSBart Van Assche * a possible bdi_register() call with the same name occurs after 1207bf2cf3baSBart Van Assche * blk_cleanup_queue() has called bdi_destroy(). 1208bf2cf3baSBart Van Assche */ 1209bf2cf3baSBart Van Assche if (sdev->is_visible) 1210bf2cf3baSBart Van Assche device_del(dev); 1211bf2cf3baSBart Van Assche else 1212bf2cf3baSBart Van Assche put_device(&sdev->sdev_dev); 1213bf2cf3baSBart Van Assche 12141da177e4SLinus Torvalds if (sdev->host->hostt->slave_destroy) 12151da177e4SLinus Torvalds sdev->host->hostt->slave_destroy(sdev); 1216df133c21SJames Bottomley transport_destroy_device(dev); 121786cbfb56SJames Bottomley 1218e63ed0d7SJames Bottomley /* 1219e63ed0d7SJames Bottomley * Paired with the kref_get() in scsi_sysfs_initialize(). We have 1220e63ed0d7SJames Bottomley * remoed sysfs visibility from the device, so make the target 1221e63ed0d7SJames Bottomley * invisible if this was the last device underneath it. 1222e63ed0d7SJames Bottomley */ 1223e63ed0d7SJames Bottomley scsi_target_reap(scsi_target(sdev)); 1224e63ed0d7SJames Bottomley 1225df133c21SJames Bottomley put_device(dev); 1226903f4fedSAlan Stern } 1227903f4fedSAlan Stern 1228903f4fedSAlan Stern /** 1229903f4fedSAlan Stern * scsi_remove_device - unregister a device from the scsi bus 1230903f4fedSAlan Stern * @sdev: scsi_device to unregister 1231903f4fedSAlan Stern **/ 1232903f4fedSAlan Stern void scsi_remove_device(struct scsi_device *sdev) 1233903f4fedSAlan Stern { 123454195002SAlan Stern struct Scsi_Host *shost = sdev->host; 123554195002SAlan Stern 12360b950672SArjan van de Ven mutex_lock(&shost->scan_mutex); 1237903f4fedSAlan Stern __scsi_remove_device(sdev); 12380b950672SArjan van de Ven mutex_unlock(&shost->scan_mutex); 12391da177e4SLinus Torvalds } 12401da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_remove_device); 12411da177e4SLinus Torvalds 124244818efbSAdrian Bunk static void __scsi_remove_target(struct scsi_target *starget) 12431da177e4SLinus Torvalds { 12441da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 12451da177e4SLinus Torvalds unsigned long flags; 1246a64358dbSAlan Stern struct scsi_device *sdev; 12471da177e4SLinus Torvalds 12481da177e4SLinus Torvalds spin_lock_irqsave(shost->host_lock, flags); 1249a64358dbSAlan Stern restart: 1250a64358dbSAlan Stern list_for_each_entry(sdev, &shost->__devices, siblings) { 12511da177e4SLinus Torvalds if (sdev->channel != starget->channel || 1252a64358dbSAlan Stern sdev->id != starget->id || 1253546ae796SChristof Schmitt scsi_device_get(sdev)) 12541da177e4SLinus Torvalds continue; 12551da177e4SLinus Torvalds spin_unlock_irqrestore(shost->host_lock, flags); 12561da177e4SLinus Torvalds scsi_remove_device(sdev); 1257546ae796SChristof Schmitt scsi_device_put(sdev); 12581da177e4SLinus Torvalds spin_lock_irqsave(shost->host_lock, flags); 1259a64358dbSAlan Stern goto restart; 12601da177e4SLinus Torvalds } 12611da177e4SLinus Torvalds spin_unlock_irqrestore(shost->host_lock, flags); 126220b1e674Smochel@digitalimplant.org } 126320b1e674Smochel@digitalimplant.org 12641da177e4SLinus Torvalds /** 12651da177e4SLinus Torvalds * scsi_remove_target - try to remove a target and all its devices 12661da177e4SLinus Torvalds * @dev: generic starget or parent of generic stargets to be removed 12671da177e4SLinus Torvalds * 12681da177e4SLinus Torvalds * Note: This is slightly racy. It is possible that if the user 12691da177e4SLinus Torvalds * requests the addition of another device then the target won't be 12701da177e4SLinus Torvalds * removed. 12711da177e4SLinus Torvalds */ 12721da177e4SLinus Torvalds void scsi_remove_target(struct device *dev) 12731da177e4SLinus Torvalds { 12743b661a92SDan Williams struct Scsi_Host *shost = dev_to_shost(dev->parent); 127540998193SChristoph Hellwig struct scsi_target *starget; 12763b661a92SDan Williams unsigned long flags; 12771da177e4SLinus Torvalds 127840998193SChristoph Hellwig restart: 12793b661a92SDan Williams spin_lock_irqsave(shost->host_lock, flags); 12803b661a92SDan Williams list_for_each_entry(starget, &shost->__targets, siblings) { 12813b661a92SDan Williams if (starget->state == STARGET_DEL) 12823b661a92SDan Williams continue; 12833b661a92SDan Williams if (starget->dev.parent == dev || &starget->dev == dev) { 1284e63ed0d7SJames Bottomley kref_get(&starget->reap_ref); 1285bc3f02a7SDan Williams spin_unlock_irqrestore(shost->host_lock, flags); 1286bc3f02a7SDan Williams __scsi_remove_target(starget); 128740998193SChristoph Hellwig scsi_target_reap(starget); 128840998193SChristoph Hellwig goto restart; 12893b661a92SDan Williams } 12903b661a92SDan Williams } 12913b661a92SDan Williams spin_unlock_irqrestore(shost->host_lock, flags); 12921da177e4SLinus Torvalds } 12931da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_remove_target); 12941da177e4SLinus Torvalds 12951da177e4SLinus Torvalds int scsi_register_driver(struct device_driver *drv) 12961da177e4SLinus Torvalds { 12971da177e4SLinus Torvalds drv->bus = &scsi_bus_type; 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds return driver_register(drv); 13001da177e4SLinus Torvalds } 13011da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_register_driver); 13021da177e4SLinus Torvalds 13031da177e4SLinus Torvalds int scsi_register_interface(struct class_interface *intf) 13041da177e4SLinus Torvalds { 13051da177e4SLinus Torvalds intf->class = &sdev_class; 13061da177e4SLinus Torvalds 13071da177e4SLinus Torvalds return class_interface_register(intf); 13081da177e4SLinus Torvalds } 13091da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_register_interface); 13101da177e4SLinus Torvalds 13111da177e4SLinus Torvalds /** 13121da177e4SLinus Torvalds * scsi_sysfs_add_host - add scsi host to subsystem 13131da177e4SLinus Torvalds * @shost: scsi host struct to add to subsystem 13141da177e4SLinus Torvalds **/ 13151da177e4SLinus Torvalds int scsi_sysfs_add_host(struct Scsi_Host *shost) 13161da177e4SLinus Torvalds { 13171da177e4SLinus Torvalds int error, i; 13181da177e4SLinus Torvalds 1319f7120a4fSHannes Reinecke /* add host specific attributes */ 13201da177e4SLinus Torvalds if (shost->hostt->shost_attrs) { 13211da177e4SLinus Torvalds for (i = 0; shost->hostt->shost_attrs[i]; i++) { 1322ee959b00STony Jones error = device_create_file(&shost->shost_dev, 1323f7120a4fSHannes Reinecke shost->hostt->shost_attrs[i]); 13241da177e4SLinus Torvalds if (error) 13251da177e4SLinus Torvalds return error; 13261da177e4SLinus Torvalds } 13271da177e4SLinus Torvalds } 13281da177e4SLinus Torvalds 13291da177e4SLinus Torvalds transport_register_device(&shost->shost_gendev); 1330d52b3815SJames Bottomley transport_configure_device(&shost->shost_gendev); 13311da177e4SLinus Torvalds return 0; 13321da177e4SLinus Torvalds } 13331da177e4SLinus Torvalds 1334bfd12944SKay Sievers static struct device_type scsi_dev_type = { 1335bfd12944SKay Sievers .name = "scsi_device", 1336bfd12944SKay Sievers .release = scsi_device_dev_release, 1337bfd12944SKay Sievers .groups = scsi_sdev_attr_groups, 1338bfd12944SKay Sievers }; 1339bfd12944SKay Sievers 13401da177e4SLinus Torvalds void scsi_sysfs_device_initialize(struct scsi_device *sdev) 13411da177e4SLinus Torvalds { 13421da177e4SLinus Torvalds unsigned long flags; 13431da177e4SLinus Torvalds struct Scsi_Host *shost = sdev->host; 13441da177e4SLinus Torvalds struct scsi_target *starget = sdev->sdev_target; 13451da177e4SLinus Torvalds 13461da177e4SLinus Torvalds device_initialize(&sdev->sdev_gendev); 13471da177e4SLinus Torvalds sdev->sdev_gendev.bus = &scsi_bus_type; 1348bfd12944SKay Sievers sdev->sdev_gendev.type = &scsi_dev_type; 13499cb78c16SHannes Reinecke dev_set_name(&sdev->sdev_gendev, "%d:%d:%d:%llu", 135071610f55SKay Sievers sdev->host->host_no, sdev->channel, sdev->id, sdev->lun); 13511da177e4SLinus Torvalds 1352ee959b00STony Jones device_initialize(&sdev->sdev_dev); 135337e6ba00SJames Bottomley sdev->sdev_dev.parent = get_device(&sdev->sdev_gendev); 1354ee959b00STony Jones sdev->sdev_dev.class = &sdev_class; 13559cb78c16SHannes Reinecke dev_set_name(&sdev->sdev_dev, "%d:%d:%d:%llu", 135671610f55SKay Sievers sdev->host->host_no, sdev->channel, sdev->id, sdev->lun); 135750c4e964SAlan Stern /* 135850c4e964SAlan Stern * Get a default scsi_level from the target (derived from sibling 135950c4e964SAlan Stern * devices). This is the best we can do for guessing how to set 136050c4e964SAlan Stern * sdev->lun_in_cdb for the initial INQUIRY command. For LUN 0 the 136150c4e964SAlan Stern * setting doesn't matter, because all the bits are zero anyway. 136250c4e964SAlan Stern * But it does matter for higher LUNs. 136350c4e964SAlan Stern */ 13647c9d6f16SAlan Stern sdev->scsi_level = starget->scsi_level; 136550c4e964SAlan Stern if (sdev->scsi_level <= SCSI_2 && 136650c4e964SAlan Stern sdev->scsi_level != SCSI_UNKNOWN && 136750c4e964SAlan Stern !shost->no_scsi2_lun_in_cdb) 136850c4e964SAlan Stern sdev->lun_in_cdb = 1; 136950c4e964SAlan Stern 13701da177e4SLinus Torvalds transport_setup_device(&sdev->sdev_gendev); 13711da177e4SLinus Torvalds spin_lock_irqsave(shost->host_lock, flags); 13721da177e4SLinus Torvalds list_add_tail(&sdev->same_target_siblings, &starget->devices); 13731da177e4SLinus Torvalds list_add_tail(&sdev->siblings, &shost->__devices); 13741da177e4SLinus Torvalds spin_unlock_irqrestore(shost->host_lock, flags); 1375e63ed0d7SJames Bottomley /* 1376e63ed0d7SJames Bottomley * device can now only be removed via __scsi_remove_device() so hold 1377e63ed0d7SJames Bottomley * the target. Target will be held in CREATED state until something 1378e63ed0d7SJames Bottomley * beneath it becomes visible (in which case it moves to RUNNING) 1379e63ed0d7SJames Bottomley */ 1380e63ed0d7SJames Bottomley kref_get(&starget->reap_ref); 13811da177e4SLinus Torvalds } 13821da177e4SLinus Torvalds 13831da177e4SLinus Torvalds int scsi_is_sdev_device(const struct device *dev) 13841da177e4SLinus Torvalds { 138513ba9bcbSKay Sievers return dev->type == &scsi_dev_type; 13861da177e4SLinus Torvalds } 13871da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_is_sdev_device); 13881da177e4SLinus Torvalds 13891da177e4SLinus Torvalds /* A blank transport template that is used in drivers that don't 13901da177e4SLinus Torvalds * yet implement Transport Attributes */ 13911da177e4SLinus Torvalds struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, }; 1392