linux/drivers/scsi/scsi_sysfs.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * scsi_sysfs.c
   4 *
   5 * SCSI sysfs interface routines.
   6 *
   7 * Created to pull SCSI mid layer sysfs routines into one file.
   8 */
   9
  10#include <linux/module.h>
  11#include <linux/slab.h>
  12#include <linux/init.h>
  13#include <linux/blkdev.h>
  14#include <linux/device.h>
  15#include <linux/pm_runtime.h>
  16
  17#include <scsi/scsi.h>
  18#include <scsi/scsi_device.h>
  19#include <scsi/scsi_host.h>
  20#include <scsi/scsi_tcq.h>
  21#include <scsi/scsi_dh.h>
  22#include <scsi/scsi_transport.h>
  23#include <scsi/scsi_driver.h>
  24#include <scsi/scsi_devinfo.h>
  25
  26#include "scsi_priv.h"
  27#include "scsi_logging.h"
  28
  29static struct device_type scsi_dev_type;
  30
  31static const struct {
  32        enum scsi_device_state  value;
  33        char                    *name;
  34} sdev_states[] = {
  35        { SDEV_CREATED, "created" },
  36        { SDEV_RUNNING, "running" },
  37        { SDEV_CANCEL, "cancel" },
  38        { SDEV_DEL, "deleted" },
  39        { SDEV_QUIESCE, "quiesce" },
  40        { SDEV_OFFLINE, "offline" },
  41        { SDEV_TRANSPORT_OFFLINE, "transport-offline" },
  42        { SDEV_BLOCK,   "blocked" },
  43        { SDEV_CREATED_BLOCK, "created-blocked" },
  44};
  45
  46const char *scsi_device_state_name(enum scsi_device_state state)
  47{
  48        int i;
  49        char *name = NULL;
  50
  51        for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
  52                if (sdev_states[i].value == state) {
  53                        name = sdev_states[i].name;
  54                        break;
  55                }
  56        }
  57        return name;
  58}
  59
  60static const struct {
  61        enum scsi_host_state    value;
  62        char                    *name;
  63} shost_states[] = {
  64        { SHOST_CREATED, "created" },
  65        { SHOST_RUNNING, "running" },
  66        { SHOST_CANCEL, "cancel" },
  67        { SHOST_DEL, "deleted" },
  68        { SHOST_RECOVERY, "recovery" },
  69        { SHOST_CANCEL_RECOVERY, "cancel/recovery" },
  70        { SHOST_DEL_RECOVERY, "deleted/recovery", },
  71};
  72const char *scsi_host_state_name(enum scsi_host_state state)
  73{
  74        int i;
  75        char *name = NULL;
  76
  77        for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
  78                if (shost_states[i].value == state) {
  79                        name = shost_states[i].name;
  80                        break;
  81                }
  82        }
  83        return name;
  84}
  85
  86#ifdef CONFIG_SCSI_DH
  87static const struct {
  88        unsigned char   value;
  89        char            *name;
  90} sdev_access_states[] = {
  91        { SCSI_ACCESS_STATE_OPTIMAL, "active/optimized" },
  92        { SCSI_ACCESS_STATE_ACTIVE, "active/non-optimized" },
  93        { SCSI_ACCESS_STATE_STANDBY, "standby" },
  94        { SCSI_ACCESS_STATE_UNAVAILABLE, "unavailable" },
  95        { SCSI_ACCESS_STATE_LBA, "lba-dependent" },
  96        { SCSI_ACCESS_STATE_OFFLINE, "offline" },
  97        { SCSI_ACCESS_STATE_TRANSITIONING, "transitioning" },
  98};
  99
 100static const char *scsi_access_state_name(unsigned char state)
 101{
 102        int i;
 103        char *name = NULL;
 104
 105        for (i = 0; i < ARRAY_SIZE(sdev_access_states); i++) {
 106                if (sdev_access_states[i].value == state) {
 107                        name = sdev_access_states[i].name;
 108                        break;
 109                }
 110        }
 111        return name;
 112}
 113#endif
 114
 115static int check_set(unsigned long long *val, char *src)
 116{
 117        char *last;
 118
 119        if (strcmp(src, "-") == 0) {
 120                *val = SCAN_WILD_CARD;
 121        } else {
 122                /*
 123                 * Doesn't check for int overflow
 124                 */
 125                *val = simple_strtoull(src, &last, 0);
 126                if (*last != '\0')
 127                        return 1;
 128        }
 129        return 0;
 130}
 131
 132static int scsi_scan(struct Scsi_Host *shost, const char *str)
 133{
 134        char s1[15], s2[15], s3[17], junk;
 135        unsigned long long channel, id, lun;
 136        int res;
 137
 138        res = sscanf(str, "%10s %10s %16s %c", s1, s2, s3, &junk);
 139        if (res != 3)
 140                return -EINVAL;
 141        if (check_set(&channel, s1))
 142                return -EINVAL;
 143        if (check_set(&id, s2))
 144                return -EINVAL;
 145        if (check_set(&lun, s3))
 146                return -EINVAL;
 147        if (shost->transportt->user_scan)
 148                res = shost->transportt->user_scan(shost, channel, id, lun);
 149        else
 150                res = scsi_scan_host_selected(shost, channel, id, lun,
 151                                              SCSI_SCAN_MANUAL);
 152        return res;
 153}
 154
 155/*
 156 * shost_show_function: macro to create an attr function that can be used to
 157 * show a non-bit field.
 158 */
 159#define shost_show_function(name, field, format_string)                 \
 160static ssize_t                                                          \
 161show_##name (struct device *dev, struct device_attribute *attr,         \
 162             char *buf)                                                 \
 163{                                                                       \
 164        struct Scsi_Host *shost = class_to_shost(dev);                  \
 165        return snprintf (buf, 20, format_string, shost->field);         \
 166}
 167
 168/*
 169 * shost_rd_attr: macro to create a function and attribute variable for a
 170 * read only field.
 171 */
 172#define shost_rd_attr2(name, field, format_string)                      \
 173        shost_show_function(name, field, format_string)                 \
 174static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
 175
 176#define shost_rd_attr(field, format_string) \
 177shost_rd_attr2(field, field, format_string)
 178
 179/*
 180 * Create the actual show/store functions and data structures.
 181 */
 182
 183static ssize_t
 184store_scan(struct device *dev, struct device_attribute *attr,
 185           const char *buf, size_t count)
 186{
 187        struct Scsi_Host *shost = class_to_shost(dev);
 188        int res;
 189
 190        res = scsi_scan(shost, buf);
 191        if (res == 0)
 192                res = count;
 193        return res;
 194};
 195static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
 196
 197static ssize_t
 198store_shost_state(struct device *dev, struct device_attribute *attr,
 199                  const char *buf, size_t count)
 200{
 201        int i;
 202        struct Scsi_Host *shost = class_to_shost(dev);
 203        enum scsi_host_state state = 0;
 204
 205        for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
 206                const int len = strlen(shost_states[i].name);
 207                if (strncmp(shost_states[i].name, buf, len) == 0 &&
 208                   buf[len] == '\n') {
 209                        state = shost_states[i].value;
 210                        break;
 211                }
 212        }
 213        if (!state)
 214                return -EINVAL;
 215
 216        if (scsi_host_set_state(shost, state))
 217                return -EINVAL;
 218        return count;
 219}
 220
 221static ssize_t
 222show_shost_state(struct device *dev, struct device_attribute *attr, char *buf)
 223{
 224        struct Scsi_Host *shost = class_to_shost(dev);
 225        const char *name = scsi_host_state_name(shost->shost_state);
 226
 227        if (!name)
 228                return -EINVAL;
 229
 230        return snprintf(buf, 20, "%s\n", name);
 231}
 232
 233/* DEVICE_ATTR(state) clashes with dev_attr_state for sdev */
 234static struct device_attribute dev_attr_hstate =
 235        __ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
 236
 237static ssize_t
 238show_shost_mode(unsigned int mode, char *buf)
 239{
 240        ssize_t len = 0;
 241
 242        if (mode & MODE_INITIATOR)
 243                len = sprintf(buf, "%s", "Initiator");
 244
 245        if (mode & MODE_TARGET)
 246                len += sprintf(buf + len, "%s%s", len ? ", " : "", "Target");
 247
 248        len += sprintf(buf + len, "\n");
 249
 250        return len;
 251}
 252
 253static ssize_t
 254show_shost_supported_mode(struct device *dev, struct device_attribute *attr,
 255                          char *buf)
 256{
 257        struct Scsi_Host *shost = class_to_shost(dev);
 258        unsigned int supported_mode = shost->hostt->supported_mode;
 259
 260        if (supported_mode == MODE_UNKNOWN)
 261                /* by default this should be initiator */
 262                supported_mode = MODE_INITIATOR;
 263
 264        return show_shost_mode(supported_mode, buf);
 265}
 266
 267static DEVICE_ATTR(supported_mode, S_IRUGO | S_IWUSR, show_shost_supported_mode, NULL);
 268
 269static ssize_t
 270show_shost_active_mode(struct device *dev,
 271                       struct device_attribute *attr, char *buf)
 272{
 273        struct Scsi_Host *shost = class_to_shost(dev);
 274
 275        if (shost->active_mode == MODE_UNKNOWN)
 276                return snprintf(buf, 20, "unknown\n");
 277        else
 278                return show_shost_mode(shost->active_mode, buf);
 279}
 280
 281static DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL);
 282
 283static int check_reset_type(const char *str)
 284{
 285        if (sysfs_streq(str, "adapter"))
 286                return SCSI_ADAPTER_RESET;
 287        else if (sysfs_streq(str, "firmware"))
 288                return SCSI_FIRMWARE_RESET;
 289        else
 290                return 0;
 291}
 292
 293static ssize_t
 294store_host_reset(struct device *dev, struct device_attribute *attr,
 295                const char *buf, size_t count)
 296{
 297        struct Scsi_Host *shost = class_to_shost(dev);
 298        struct scsi_host_template *sht = shost->hostt;
 299        int ret = -EINVAL;
 300        int type;
 301
 302        type = check_reset_type(buf);
 303        if (!type)
 304                goto exit_store_host_reset;
 305
 306        if (sht->host_reset)
 307                ret = sht->host_reset(shost, type);
 308        else
 309                ret = -EOPNOTSUPP;
 310
 311exit_store_host_reset:
 312        if (ret == 0)
 313                ret = count;
 314        return ret;
 315}
 316
 317static DEVICE_ATTR(host_reset, S_IWUSR, NULL, store_host_reset);
 318
 319static ssize_t
 320show_shost_eh_deadline(struct device *dev,
 321                      struct device_attribute *attr, char *buf)
 322{
 323        struct Scsi_Host *shost = class_to_shost(dev);
 324
 325        if (shost->eh_deadline == -1)
 326                return snprintf(buf, strlen("off") + 2, "off\n");
 327        return sprintf(buf, "%u\n", shost->eh_deadline / HZ);
 328}
 329
 330static ssize_t
 331store_shost_eh_deadline(struct device *dev, struct device_attribute *attr,
 332                const char *buf, size_t count)
 333{
 334        struct Scsi_Host *shost = class_to_shost(dev);
 335        int ret = -EINVAL;
 336        unsigned long deadline, flags;
 337
 338        if (shost->transportt &&
 339            (shost->transportt->eh_strategy_handler ||
 340             !shost->hostt->eh_host_reset_handler))
 341                return ret;
 342
 343        if (!strncmp(buf, "off", strlen("off")))
 344                deadline = -1;
 345        else {
 346                ret = kstrtoul(buf, 10, &deadline);
 347                if (ret)
 348                        return ret;
 349                if (deadline * HZ > UINT_MAX)
 350                        return -EINVAL;
 351        }
 352
 353        spin_lock_irqsave(shost->host_lock, flags);
 354        if (scsi_host_in_recovery(shost))
 355                ret = -EBUSY;
 356        else {
 357                if (deadline == -1)
 358                        shost->eh_deadline = -1;
 359                else
 360                        shost->eh_deadline = deadline * HZ;
 361
 362                ret = count;
 363        }
 364        spin_unlock_irqrestore(shost->host_lock, flags);
 365
 366        return ret;
 367}
 368
 369static DEVICE_ATTR(eh_deadline, S_IRUGO | S_IWUSR, show_shost_eh_deadline, store_shost_eh_deadline);
 370
 371shost_rd_attr(unique_id, "%u\n");
 372shost_rd_attr(cmd_per_lun, "%hd\n");
 373shost_rd_attr(can_queue, "%d\n");
 374shost_rd_attr(sg_tablesize, "%hu\n");
 375shost_rd_attr(sg_prot_tablesize, "%hu\n");
 376shost_rd_attr(unchecked_isa_dma, "%d\n");
 377shost_rd_attr(prot_capabilities, "%u\n");
 378shost_rd_attr(prot_guard_type, "%hd\n");
 379shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
 380
 381static ssize_t
 382show_host_busy(struct device *dev, struct device_attribute *attr, char *buf)
 383{
 384        struct Scsi_Host *shost = class_to_shost(dev);
 385        return snprintf(buf, 20, "%d\n", scsi_host_busy(shost));
 386}
 387static DEVICE_ATTR(host_busy, S_IRUGO, show_host_busy, NULL);
 388
 389static ssize_t
 390show_use_blk_mq(struct device *dev, struct device_attribute *attr, char *buf)
 391{
 392        return sprintf(buf, "1\n");
 393}
 394static DEVICE_ATTR(use_blk_mq, S_IRUGO, show_use_blk_mq, NULL);
 395
 396static ssize_t
 397show_nr_hw_queues(struct device *dev, struct device_attribute *attr, char *buf)
 398{
 399        struct Scsi_Host *shost = class_to_shost(dev);
 400        struct blk_mq_tag_set *tag_set = &shost->tag_set;
 401
 402        return snprintf(buf, 20, "%d\n", tag_set->nr_hw_queues);
 403}
 404static DEVICE_ATTR(nr_hw_queues, S_IRUGO, show_nr_hw_queues, NULL);
 405
 406static struct attribute *scsi_sysfs_shost_attrs[] = {
 407        &dev_attr_use_blk_mq.attr,
 408        &dev_attr_unique_id.attr,
 409        &dev_attr_host_busy.attr,
 410        &dev_attr_cmd_per_lun.attr,
 411        &dev_attr_can_queue.attr,
 412        &dev_attr_sg_tablesize.attr,
 413        &dev_attr_sg_prot_tablesize.attr,
 414        &dev_attr_unchecked_isa_dma.attr,
 415        &dev_attr_proc_name.attr,
 416        &dev_attr_scan.attr,
 417        &dev_attr_hstate.attr,
 418        &dev_attr_supported_mode.attr,
 419        &dev_attr_active_mode.attr,
 420        &dev_attr_prot_capabilities.attr,
 421        &dev_attr_prot_guard_type.attr,
 422        &dev_attr_host_reset.attr,
 423        &dev_attr_eh_deadline.attr,
 424        &dev_attr_nr_hw_queues.attr,
 425        NULL
 426};
 427
 428static struct attribute_group scsi_shost_attr_group = {
 429        .attrs =        scsi_sysfs_shost_attrs,
 430};
 431
 432const struct attribute_group *scsi_sysfs_shost_attr_groups[] = {
 433        &scsi_shost_attr_group,
 434        NULL
 435};
 436
 437static void scsi_device_cls_release(struct device *class_dev)
 438{
 439        struct scsi_device *sdev;
 440
 441        sdev = class_to_sdev(class_dev);
 442        put_device(&sdev->sdev_gendev);
 443}
 444
 445static void scsi_device_dev_release_usercontext(struct work_struct *work)
 446{
 447        struct scsi_device *sdev;
 448        struct device *parent;
 449        struct list_head *this, *tmp;
 450        struct scsi_vpd *vpd_pg80 = NULL, *vpd_pg83 = NULL;
 451        struct scsi_vpd *vpd_pg0 = NULL, *vpd_pg89 = NULL;
 452        unsigned long flags;
 453        struct module *mod;
 454
 455        sdev = container_of(work, struct scsi_device, ew.work);
 456
 457        mod = sdev->host->hostt->module;
 458
 459        scsi_dh_release_device(sdev);
 460
 461        parent = sdev->sdev_gendev.parent;
 462
 463        spin_lock_irqsave(sdev->host->host_lock, flags);
 464        list_del(&sdev->siblings);
 465        list_del(&sdev->same_target_siblings);
 466        list_del(&sdev->starved_entry);
 467        spin_unlock_irqrestore(sdev->host->host_lock, flags);
 468
 469        cancel_work_sync(&sdev->event_work);
 470
 471        list_for_each_safe(this, tmp, &sdev->event_list) {
 472                struct scsi_event *evt;
 473
 474                evt = list_entry(this, struct scsi_event, node);
 475                list_del(&evt->node);
 476                kfree(evt);
 477        }
 478
 479        blk_put_queue(sdev->request_queue);
 480        /* NULL queue means the device can't be used */
 481        sdev->request_queue = NULL;
 482
 483        sbitmap_free(sdev->budget_map);
 484        kfree(sdev->budget_map);
 485
 486        mutex_lock(&sdev->inquiry_mutex);
 487        vpd_pg0 = rcu_replace_pointer(sdev->vpd_pg0, vpd_pg0,
 488                                       lockdep_is_held(&sdev->inquiry_mutex));
 489        vpd_pg80 = rcu_replace_pointer(sdev->vpd_pg80, vpd_pg80,
 490                                       lockdep_is_held(&sdev->inquiry_mutex));
 491        vpd_pg83 = rcu_replace_pointer(sdev->vpd_pg83, vpd_pg83,
 492                                       lockdep_is_held(&sdev->inquiry_mutex));
 493        vpd_pg89 = rcu_replace_pointer(sdev->vpd_pg89, vpd_pg89,
 494                                       lockdep_is_held(&sdev->inquiry_mutex));
 495        mutex_unlock(&sdev->inquiry_mutex);
 496
 497        if (vpd_pg0)
 498                kfree_rcu(vpd_pg0, rcu);
 499        if (vpd_pg83)
 500                kfree_rcu(vpd_pg83, rcu);
 501        if (vpd_pg80)
 502                kfree_rcu(vpd_pg80, rcu);
 503        if (vpd_pg89)
 504                kfree_rcu(vpd_pg89, rcu);
 505        kfree(sdev->inquiry);
 506        kfree(sdev);
 507
 508        if (parent)
 509                put_device(parent);
 510        module_put(mod);
 511}
 512
 513static void scsi_device_dev_release(struct device *dev)
 514{
 515        struct scsi_device *sdp = to_scsi_device(dev);
 516
 517        /* Set module pointer as NULL in case of module unloading */
 518        if (!try_module_get(sdp->host->hostt->module))
 519                sdp->host->hostt->module = NULL;
 520
 521        execute_in_process_context(scsi_device_dev_release_usercontext,
 522                                   &sdp->ew);
 523}
 524
 525static struct class sdev_class = {
 526        .name           = "scsi_device",
 527        .dev_release    = scsi_device_cls_release,
 528};
 529
 530/* all probing is done in the individual ->probe routines */
 531static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
 532{
 533        struct scsi_device *sdp;
 534
 535        if (dev->type != &scsi_dev_type)
 536                return 0;
 537
 538        sdp = to_scsi_device(dev);
 539        if (sdp->no_uld_attach)
 540                return 0;
 541        return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
 542}
 543
 544static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
 545{
 546        struct scsi_device *sdev;
 547
 548        if (dev->type != &scsi_dev_type)
 549                return 0;
 550
 551        sdev = to_scsi_device(dev);
 552
 553        add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
 554        return 0;
 555}
 556
 557struct bus_type scsi_bus_type = {
 558        .name           = "scsi",
 559        .match          = scsi_bus_match,
 560        .uevent         = scsi_bus_uevent,
 561#ifdef CONFIG_PM
 562        .pm             = &scsi_bus_pm_ops,
 563#endif
 564};
 565EXPORT_SYMBOL_GPL(scsi_bus_type);
 566
 567int scsi_sysfs_register(void)
 568{
 569        int error;
 570
 571        error = bus_register(&scsi_bus_type);
 572        if (!error) {
 573                error = class_register(&sdev_class);
 574                if (error)
 575                        bus_unregister(&scsi_bus_type);
 576        }
 577
 578        return error;
 579}
 580
 581void scsi_sysfs_unregister(void)
 582{
 583        class_unregister(&sdev_class);
 584        bus_unregister(&scsi_bus_type);
 585}
 586
 587/*
 588 * sdev_show_function: macro to create an attr function that can be used to
 589 * show a non-bit field.
 590 */
 591#define sdev_show_function(field, format_string)                                \
 592static ssize_t                                                          \
 593sdev_show_##field (struct device *dev, struct device_attribute *attr,   \
 594                   char *buf)                                           \
 595{                                                                       \
 596        struct scsi_device *sdev;                                       \
 597        sdev = to_scsi_device(dev);                                     \
 598        return snprintf (buf, 20, format_string, sdev->field);          \
 599}                                                                       \
 600
 601/*
 602 * sdev_rd_attr: macro to create a function and attribute variable for a
 603 * read only field.
 604 */
 605#define sdev_rd_attr(field, format_string)                              \
 606        sdev_show_function(field, format_string)                        \
 607static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
 608
 609
 610/*
 611 * sdev_rw_attr: create a function and attribute variable for a
 612 * read/write field.
 613 */
 614#define sdev_rw_attr(field, format_string)                              \
 615        sdev_show_function(field, format_string)                                \
 616                                                                        \
 617static ssize_t                                                          \
 618sdev_store_##field (struct device *dev, struct device_attribute *attr,  \
 619                    const char *buf, size_t count)                      \
 620{                                                                       \
 621        struct scsi_device *sdev;                                       \
 622        sdev = to_scsi_device(dev);                                     \
 623        sscanf (buf, format_string, &sdev->field);                      \
 624        return count;                                                   \
 625}                                                                       \
 626static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
 627
 628/* Currently we don't export bit fields, but we might in future,
 629 * so leave this code in */
 630#if 0
 631/*
 632 * sdev_rd_attr: create a function and attribute variable for a
 633 * read/write bit field.
 634 */
 635#define sdev_rw_attr_bit(field)                                         \
 636        sdev_show_function(field, "%d\n")                                       \
 637                                                                        \
 638static ssize_t                                                          \
 639sdev_store_##field (struct device *dev, struct device_attribute *attr,  \
 640                    const char *buf, size_t count)                      \
 641{                                                                       \
 642        int ret;                                                        \
 643        struct scsi_device *sdev;                                       \
 644        ret = scsi_sdev_check_buf_bit(buf);                             \
 645        if (ret >= 0)   {                                               \
 646                sdev = to_scsi_device(dev);                             \
 647                sdev->field = ret;                                      \
 648                ret = count;                                            \
 649        }                                                               \
 650        return ret;                                                     \
 651}                                                                       \
 652static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
 653
 654/*
 655 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
 656 * else return -EINVAL.
 657 */
 658static int scsi_sdev_check_buf_bit(const char *buf)
 659{
 660        if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
 661                if (buf[0] == '1')
 662                        return 1;
 663                else if (buf[0] == '0')
 664                        return 0;
 665                else 
 666                        return -EINVAL;
 667        } else
 668                return -EINVAL;
 669}
 670#endif
 671/*
 672 * Create the actual show/store functions and data structures.
 673 */
 674sdev_rd_attr (type, "%d\n");
 675sdev_rd_attr (scsi_level, "%d\n");
 676sdev_rd_attr (vendor, "%.8s\n");
 677sdev_rd_attr (model, "%.16s\n");
 678sdev_rd_attr (rev, "%.4s\n");
 679
 680static ssize_t
 681sdev_show_device_busy(struct device *dev, struct device_attribute *attr,
 682                char *buf)
 683{
 684        struct scsi_device *sdev = to_scsi_device(dev);
 685        return snprintf(buf, 20, "%d\n", scsi_device_busy(sdev));
 686}
 687static DEVICE_ATTR(device_busy, S_IRUGO, sdev_show_device_busy, NULL);
 688
 689static ssize_t
 690sdev_show_device_blocked(struct device *dev, struct device_attribute *attr,
 691                char *buf)
 692{
 693        struct scsi_device *sdev = to_scsi_device(dev);
 694        return snprintf(buf, 20, "%d\n", atomic_read(&sdev->device_blocked));
 695}
 696static DEVICE_ATTR(device_blocked, S_IRUGO, sdev_show_device_blocked, NULL);
 697
 698static ssize_t
 699sdev_show_unpriv_sgio (struct device *dev, struct device_attribute *attr, char *buf)
 700{
 701        struct scsi_device *sdev;
 702        int bit;
 703
 704        sdev = to_scsi_device(dev);
 705        bit = test_bit(QUEUE_FLAG_UNPRIV_SGIO, &sdev->request_queue->queue_flags);
 706        return snprintf(buf, 20, "%d\n", bit);
 707}
 708
 709static ssize_t
 710sdev_store_unpriv_sgio (struct device *dev, struct device_attribute *attr,
 711                        const char *buf, size_t count)
 712{
 713        struct scsi_device *sdev;
 714        struct request_queue *q;
 715        int err;
 716        unsigned long val;
 717
 718        if (!capable(CAP_SYS_ADMIN))
 719                return -EPERM;
 720
 721        sdev = to_scsi_device(dev);
 722        q = sdev->request_queue;
 723        err = kstrtoul(buf, 10, &val);
 724        if (err)
 725                return -EINVAL;
 726
 727        if (val)
 728                blk_queue_flag_set(QUEUE_FLAG_UNPRIV_SGIO, q);
 729        else
 730                blk_queue_flag_clear(QUEUE_FLAG_UNPRIV_SGIO, q);
 731        return count;
 732}
 733static DEVICE_ATTR(unpriv_sgio, S_IRUGO | S_IWUSR, sdev_show_unpriv_sgio, sdev_store_unpriv_sgio);
 734
 735/*
 736 * TODO: can we make these symlinks to the block layer ones?
 737 */
 738static ssize_t
 739sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
 740{
 741        struct scsi_device *sdev;
 742        sdev = to_scsi_device(dev);
 743        return snprintf(buf, 20, "%d\n", sdev->request_queue->rq_timeout / HZ);
 744}
 745
 746static ssize_t
 747sdev_store_timeout (struct device *dev, struct device_attribute *attr,
 748                    const char *buf, size_t count)
 749{
 750        struct scsi_device *sdev;
 751        int timeout;
 752        sdev = to_scsi_device(dev);
 753        sscanf (buf, "%d\n", &timeout);
 754        blk_queue_rq_timeout(sdev->request_queue, timeout * HZ);
 755        return count;
 756}
 757static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
 758
 759static ssize_t
 760sdev_show_eh_timeout(struct device *dev, struct device_attribute *attr, char *buf)
 761{
 762        struct scsi_device *sdev;
 763        sdev = to_scsi_device(dev);
 764        return snprintf(buf, 20, "%u\n", sdev->eh_timeout / HZ);
 765}
 766
 767static ssize_t
 768sdev_store_eh_timeout(struct device *dev, struct device_attribute *attr,
 769                    const char *buf, size_t count)
 770{
 771        struct scsi_device *sdev;
 772        unsigned int eh_timeout;
 773        int err;
 774
 775        if (!capable(CAP_SYS_ADMIN))
 776                return -EACCES;
 777
 778        sdev = to_scsi_device(dev);
 779        err = kstrtouint(buf, 10, &eh_timeout);
 780        if (err)
 781                return err;
 782        sdev->eh_timeout = eh_timeout * HZ;
 783
 784        return count;
 785}
 786static DEVICE_ATTR(eh_timeout, S_IRUGO | S_IWUSR, sdev_show_eh_timeout, sdev_store_eh_timeout);
 787
 788static ssize_t
 789store_rescan_field (struct device *dev, struct device_attribute *attr,
 790                    const char *buf, size_t count)
 791{
 792        scsi_rescan_device(dev);
 793        return count;
 794}
 795static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
 796
 797static ssize_t
 798sdev_store_delete(struct device *dev, struct device_attribute *attr,
 799                  const char *buf, size_t count)
 800{
 801        struct kernfs_node *kn;
 802        struct scsi_device *sdev = to_scsi_device(dev);
 803
 804        /*
 805         * We need to try to get module, avoiding the module been removed
 806         * during delete.
 807         */
 808        if (scsi_device_get(sdev))
 809                return -ENODEV;
 810
 811        kn = sysfs_break_active_protection(&dev->kobj, &attr->attr);
 812        WARN_ON_ONCE(!kn);
 813        /*
 814         * Concurrent writes into the "delete" sysfs attribute may trigger
 815         * concurrent calls to device_remove_file() and scsi_remove_device().
 816         * device_remove_file() handles concurrent removal calls by
 817         * serializing these and by ignoring the second and later removal
 818         * attempts.  Concurrent calls of scsi_remove_device() are
 819         * serialized. The second and later calls of scsi_remove_device() are
 820         * ignored because the first call of that function changes the device
 821         * state into SDEV_DEL.
 822         */
 823        device_remove_file(dev, attr);
 824        scsi_remove_device(sdev);
 825        if (kn)
 826                sysfs_unbreak_active_protection(kn);
 827        scsi_device_put(sdev);
 828        return count;
 829};
 830static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
 831
 832static ssize_t
 833store_state_field(struct device *dev, struct device_attribute *attr,
 834                  const char *buf, size_t count)
 835{
 836        int i, ret;
 837        struct scsi_device *sdev = to_scsi_device(dev);
 838        enum scsi_device_state state = 0;
 839
 840        for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
 841                const int len = strlen(sdev_states[i].name);
 842                if (strncmp(sdev_states[i].name, buf, len) == 0 &&
 843                   buf[len] == '\n') {
 844                        state = sdev_states[i].value;
 845                        break;
 846                }
 847        }
 848        if (!state)
 849                return -EINVAL;
 850
 851        mutex_lock(&sdev->state_mutex);
 852        ret = scsi_device_set_state(sdev, state);
 853        /*
 854         * If the device state changes to SDEV_RUNNING, we need to
 855         * run the queue to avoid I/O hang, and rescan the device
 856         * to revalidate it. Running the queue first is necessary
 857         * because another thread may be waiting inside
 858         * blk_mq_freeze_queue_wait() and because that call may be
 859         * waiting for pending I/O to finish.
 860         */
 861        if (ret == 0 && state == SDEV_RUNNING) {
 862                blk_mq_run_hw_queues(sdev->request_queue, true);
 863                scsi_rescan_device(dev);
 864        }
 865        mutex_unlock(&sdev->state_mutex);
 866
 867        return ret == 0 ? count : -EINVAL;
 868}
 869
 870static ssize_t
 871show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
 872{
 873        struct scsi_device *sdev = to_scsi_device(dev);
 874        const char *name = scsi_device_state_name(sdev->sdev_state);
 875
 876        if (!name)
 877                return -EINVAL;
 878
 879        return snprintf(buf, 20, "%s\n", name);
 880}
 881
 882static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
 883
 884static ssize_t
 885show_queue_type_field(struct device *dev, struct device_attribute *attr,
 886                      char *buf)
 887{
 888        struct scsi_device *sdev = to_scsi_device(dev);
 889        const char *name = "none";
 890
 891        if (sdev->simple_tags)
 892                name = "simple";
 893
 894        return snprintf(buf, 20, "%s\n", name);
 895}
 896
 897static ssize_t
 898store_queue_type_field(struct device *dev, struct device_attribute *attr,
 899                       const char *buf, size_t count)
 900{
 901        struct scsi_device *sdev = to_scsi_device(dev);
 902
 903        if (!sdev->tagged_supported)
 904                return -EINVAL;
 905                
 906        sdev_printk(KERN_INFO, sdev,
 907                    "ignoring write to deprecated queue_type attribute");
 908        return count;
 909}
 910
 911static DEVICE_ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
 912                   store_queue_type_field);
 913
 914#define sdev_vpd_pg_attr(_page)                                         \
 915static ssize_t                                                  \
 916show_vpd_##_page(struct file *filp, struct kobject *kobj,       \
 917                 struct bin_attribute *bin_attr,                        \
 918                 char *buf, loff_t off, size_t count)                   \
 919{                                                                       \
 920        struct device *dev = kobj_to_dev(kobj);                         \
 921        struct scsi_device *sdev = to_scsi_device(dev);                 \
 922        struct scsi_vpd *vpd_page;                                      \
 923        int ret = -EINVAL;                                              \
 924                                                                        \
 925        rcu_read_lock();                                                \
 926        vpd_page = rcu_dereference(sdev->vpd_##_page);                  \
 927        if (vpd_page)                                                   \
 928                ret = memory_read_from_buffer(buf, count, &off,         \
 929                                vpd_page->data, vpd_page->len);         \
 930        rcu_read_unlock();                                              \
 931        return ret;                                                     \
 932}                                                                       \
 933static struct bin_attribute dev_attr_vpd_##_page = {            \
 934        .attr = {.name = __stringify(vpd_##_page), .mode = S_IRUGO },   \
 935        .size = 0,                                                      \
 936        .read = show_vpd_##_page,                                       \
 937};
 938
 939sdev_vpd_pg_attr(pg83);
 940sdev_vpd_pg_attr(pg80);
 941sdev_vpd_pg_attr(pg89);
 942sdev_vpd_pg_attr(pg0);
 943
 944static ssize_t show_inquiry(struct file *filep, struct kobject *kobj,
 945                            struct bin_attribute *bin_attr,
 946                            char *buf, loff_t off, size_t count)
 947{
 948        struct device *dev = kobj_to_dev(kobj);
 949        struct scsi_device *sdev = to_scsi_device(dev);
 950
 951        if (!sdev->inquiry)
 952                return -EINVAL;
 953
 954        return memory_read_from_buffer(buf, count, &off, sdev->inquiry,
 955                                       sdev->inquiry_len);
 956}
 957
 958static struct bin_attribute dev_attr_inquiry = {
 959        .attr = {
 960                .name = "inquiry",
 961                .mode = S_IRUGO,
 962        },
 963        .size = 0,
 964        .read = show_inquiry,
 965};
 966
 967static ssize_t
 968show_iostat_counterbits(struct device *dev, struct device_attribute *attr,
 969                        char *buf)
 970{
 971        return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
 972}
 973
 974static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
 975
 976#define show_sdev_iostat(field)                                         \
 977static ssize_t                                                          \
 978show_iostat_##field(struct device *dev, struct device_attribute *attr,  \
 979                    char *buf)                                          \
 980{                                                                       \
 981        struct scsi_device *sdev = to_scsi_device(dev);                 \
 982        unsigned long long count = atomic_read(&sdev->field);           \
 983        return snprintf(buf, 20, "0x%llx\n", count);                    \
 984}                                                                       \
 985static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
 986
 987show_sdev_iostat(iorequest_cnt);
 988show_sdev_iostat(iodone_cnt);
 989show_sdev_iostat(ioerr_cnt);
 990
 991static ssize_t
 992sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
 993{
 994        struct scsi_device *sdev;
 995        sdev = to_scsi_device(dev);
 996        return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
 997}
 998static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
 999
1000#define DECLARE_EVT_SHOW(name, Cap_name)                                \
1001static ssize_t                                                          \
1002sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
1003                     char *buf)                                         \
1004{                                                                       \
1005        struct scsi_device *sdev = to_scsi_device(dev);                 \
1006        int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
1007        return snprintf(buf, 20, "%d\n", val);                          \
1008}
1009
1010#define DECLARE_EVT_STORE(name, Cap_name)                               \
1011static ssize_t                                                          \
1012sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
1013                      const char *buf, size_t count)                    \
1014{                                                                       \
1015        struct scsi_device *sdev = to_scsi_device(dev);                 \
1016        int val = simple_strtoul(buf, NULL, 0);                         \
1017        if (val == 0)                                                   \
1018                clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
1019        else if (val == 1)                                              \
1020                set_bit(SDEV_EVT_##Cap_name, sdev->supported_events);   \
1021        else                                                            \
1022                return -EINVAL;                                         \
1023        return count;                                                   \
1024}
1025
1026#define DECLARE_EVT(name, Cap_name)                                     \
1027        DECLARE_EVT_SHOW(name, Cap_name)                                \
1028        DECLARE_EVT_STORE(name, Cap_name)                               \
1029        static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name,   \
1030                           sdev_store_evt_##name);
1031#define REF_EVT(name) &dev_attr_evt_##name.attr
1032
1033DECLARE_EVT(media_change, MEDIA_CHANGE)
1034DECLARE_EVT(inquiry_change_reported, INQUIRY_CHANGE_REPORTED)
1035DECLARE_EVT(capacity_change_reported, CAPACITY_CHANGE_REPORTED)
1036DECLARE_EVT(soft_threshold_reached, SOFT_THRESHOLD_REACHED_REPORTED)
1037DECLARE_EVT(mode_parameter_change_reported, MODE_PARAMETER_CHANGE_REPORTED)
1038DECLARE_EVT(lun_change_reported, LUN_CHANGE_REPORTED)
1039
1040static ssize_t
1041sdev_store_queue_depth(struct device *dev, struct device_attribute *attr,
1042                       const char *buf, size_t count)
1043{
1044        int depth, retval;
1045        struct scsi_device *sdev = to_scsi_device(dev);
1046        struct scsi_host_template *sht = sdev->host->hostt;
1047
1048        if (!sht->change_queue_depth)
1049                return -EINVAL;
1050
1051        depth = simple_strtoul(buf, NULL, 0);
1052
1053        if (depth < 1 || depth > sdev->host->can_queue)
1054                return -EINVAL;
1055
1056        retval = sht->change_queue_depth(sdev, depth);
1057        if (retval < 0)
1058                return retval;
1059
1060        sdev->max_queue_depth = sdev->queue_depth;
1061
1062        return count;
1063}
1064sdev_show_function(queue_depth, "%d\n");
1065
1066static DEVICE_ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
1067                   sdev_store_queue_depth);
1068
1069static ssize_t
1070sdev_show_wwid(struct device *dev, struct device_attribute *attr,
1071                    char *buf)
1072{
1073        struct scsi_device *sdev = to_scsi_device(dev);
1074        ssize_t count;
1075
1076        count = scsi_vpd_lun_id(sdev, buf, PAGE_SIZE);
1077        if (count > 0) {
1078                buf[count] = '\n';
1079                count++;
1080        }
1081        return count;
1082}
1083static DEVICE_ATTR(wwid, S_IRUGO, sdev_show_wwid, NULL);
1084
1085#define BLIST_FLAG_NAME(name)                                   \
1086        [const_ilog2((__force __u64)BLIST_##name)] = #name
1087static const char *const sdev_bflags_name[] = {
1088#include "scsi_devinfo_tbl.c"
1089};
1090#undef BLIST_FLAG_NAME
1091
1092static ssize_t
1093sdev_show_blacklist(struct device *dev, struct device_attribute *attr,
1094                    char *buf)
1095{
1096        struct scsi_device *sdev = to_scsi_device(dev);
1097        int i;
1098        ssize_t len = 0;
1099
1100        for (i = 0; i < sizeof(sdev->sdev_bflags) * BITS_PER_BYTE; i++) {
1101                const char *name = NULL;
1102
1103                if (!(sdev->sdev_bflags & (__force blist_flags_t)BIT(i)))
1104                        continue;
1105                if (i < ARRAY_SIZE(sdev_bflags_name) && sdev_bflags_name[i])
1106                        name = sdev_bflags_name[i];
1107
1108                if (name)
1109                        len += scnprintf(buf + len, PAGE_SIZE - len,
1110                                         "%s%s", len ? " " : "", name);
1111                else
1112                        len += scnprintf(buf + len, PAGE_SIZE - len,
1113                                         "%sINVALID_BIT(%d)", len ? " " : "", i);
1114        }
1115        if (len)
1116                len += scnprintf(buf + len, PAGE_SIZE - len, "\n");
1117        return len;
1118}
1119static DEVICE_ATTR(blacklist, S_IRUGO, sdev_show_blacklist, NULL);
1120
1121#ifdef CONFIG_SCSI_DH
1122static ssize_t
1123sdev_show_dh_state(struct device *dev, struct device_attribute *attr,
1124                   char *buf)
1125{
1126        struct scsi_device *sdev = to_scsi_device(dev);
1127
1128        if (!sdev->handler)
1129                return snprintf(buf, 20, "detached\n");
1130
1131        return snprintf(buf, 20, "%s\n", sdev->handler->name);
1132}
1133
1134static ssize_t
1135sdev_store_dh_state(struct device *dev, struct device_attribute *attr,
1136                    const char *buf, size_t count)
1137{
1138        struct scsi_device *sdev = to_scsi_device(dev);
1139        int err = -EINVAL;
1140
1141        if (sdev->sdev_state == SDEV_CANCEL ||
1142            sdev->sdev_state == SDEV_DEL)
1143                return -ENODEV;
1144
1145        if (!sdev->handler) {
1146                /*
1147                 * Attach to a device handler
1148                 */
1149                err = scsi_dh_attach(sdev->request_queue, buf);
1150        } else if (!strncmp(buf, "activate", 8)) {
1151                /*
1152                 * Activate a device handler
1153                 */
1154                if (sdev->handler->activate)
1155                        err = sdev->handler->activate(sdev, NULL, NULL);
1156                else
1157                        err = 0;
1158        } else if (!strncmp(buf, "detach", 6)) {
1159                /*
1160                 * Detach from a device handler
1161                 */
1162                sdev_printk(KERN_WARNING, sdev,
1163                            "can't detach handler %s.\n",
1164                            sdev->handler->name);
1165                err = -EINVAL;
1166        }
1167
1168        return err < 0 ? err : count;
1169}
1170
1171static DEVICE_ATTR(dh_state, S_IRUGO | S_IWUSR, sdev_show_dh_state,
1172                   sdev_store_dh_state);
1173
1174static ssize_t
1175sdev_show_access_state(struct device *dev,
1176                       struct device_attribute *attr,
1177                       char *buf)
1178{
1179        struct scsi_device *sdev = to_scsi_device(dev);
1180        unsigned char access_state;
1181        const char *access_state_name;
1182
1183        if (!sdev->handler)
1184                return -EINVAL;
1185
1186        access_state = (sdev->access_state & SCSI_ACCESS_STATE_MASK);
1187        access_state_name = scsi_access_state_name(access_state);
1188
1189        return sprintf(buf, "%s\n",
1190                       access_state_name ? access_state_name : "unknown");
1191}
1192static DEVICE_ATTR(access_state, S_IRUGO, sdev_show_access_state, NULL);
1193
1194static ssize_t
1195sdev_show_preferred_path(struct device *dev,
1196                         struct device_attribute *attr,
1197                         char *buf)
1198{
1199        struct scsi_device *sdev = to_scsi_device(dev);
1200
1201        if (!sdev->handler)
1202                return -EINVAL;
1203
1204        if (sdev->access_state & SCSI_ACCESS_STATE_PREFERRED)
1205                return sprintf(buf, "1\n");
1206        else
1207                return sprintf(buf, "0\n");
1208}
1209static DEVICE_ATTR(preferred_path, S_IRUGO, sdev_show_preferred_path, NULL);
1210#endif
1211
1212static ssize_t
1213sdev_show_queue_ramp_up_period(struct device *dev,
1214                               struct device_attribute *attr,
1215                               char *buf)
1216{
1217        struct scsi_device *sdev;
1218        sdev = to_scsi_device(dev);
1219        return snprintf(buf, 20, "%u\n",
1220                        jiffies_to_msecs(sdev->queue_ramp_up_period));
1221}
1222
1223static ssize_t
1224sdev_store_queue_ramp_up_period(struct device *dev,
1225                                struct device_attribute *attr,
1226                                const char *buf, size_t count)
1227{
1228        struct scsi_device *sdev = to_scsi_device(dev);
1229        unsigned int period;
1230
1231        if (kstrtouint(buf, 10, &period))
1232                return -EINVAL;
1233
1234        sdev->queue_ramp_up_period = msecs_to_jiffies(period);
1235        return count;
1236}
1237
1238static DEVICE_ATTR(queue_ramp_up_period, S_IRUGO | S_IWUSR,
1239                   sdev_show_queue_ramp_up_period,
1240                   sdev_store_queue_ramp_up_period);
1241
1242static umode_t scsi_sdev_attr_is_visible(struct kobject *kobj,
1243                                         struct attribute *attr, int i)
1244{
1245        struct device *dev = kobj_to_dev(kobj);
1246        struct scsi_device *sdev = to_scsi_device(dev);
1247
1248
1249        if (attr == &dev_attr_queue_depth.attr &&
1250            !sdev->host->hostt->change_queue_depth)
1251                return S_IRUGO;
1252
1253        if (attr == &dev_attr_queue_ramp_up_period.attr &&
1254            !sdev->host->hostt->change_queue_depth)
1255                return 0;
1256
1257#ifdef CONFIG_SCSI_DH
1258        if (attr == &dev_attr_access_state.attr &&
1259            !sdev->handler)
1260                return 0;
1261        if (attr == &dev_attr_preferred_path.attr &&
1262            !sdev->handler)
1263                return 0;
1264#endif
1265        return attr->mode;
1266}
1267
1268static umode_t scsi_sdev_bin_attr_is_visible(struct kobject *kobj,
1269                                             struct bin_attribute *attr, int i)
1270{
1271        struct device *dev = kobj_to_dev(kobj);
1272        struct scsi_device *sdev = to_scsi_device(dev);
1273
1274
1275        if (attr == &dev_attr_vpd_pg0 && !sdev->vpd_pg0)
1276                return 0;
1277
1278        if (attr == &dev_attr_vpd_pg80 && !sdev->vpd_pg80)
1279                return 0;
1280
1281        if (attr == &dev_attr_vpd_pg83 && !sdev->vpd_pg83)
1282                return 0;
1283
1284        if (attr == &dev_attr_vpd_pg89 && !sdev->vpd_pg89)
1285                return 0;
1286
1287        return S_IRUGO;
1288}
1289
1290/* Default template for device attributes.  May NOT be modified */
1291static struct attribute *scsi_sdev_attrs[] = {
1292        &dev_attr_device_blocked.attr,
1293        &dev_attr_type.attr,
1294        &dev_attr_scsi_level.attr,
1295        &dev_attr_device_busy.attr,
1296        &dev_attr_vendor.attr,
1297        &dev_attr_model.attr,
1298        &dev_attr_rev.attr,
1299        &dev_attr_rescan.attr,
1300        &dev_attr_delete.attr,
1301        &dev_attr_state.attr,
1302        &dev_attr_unpriv_sgio.attr,
1303        &dev_attr_timeout.attr,
1304        &dev_attr_eh_timeout.attr,
1305        &dev_attr_iocounterbits.attr,
1306        &dev_attr_iorequest_cnt.attr,
1307        &dev_attr_iodone_cnt.attr,
1308        &dev_attr_ioerr_cnt.attr,
1309        &dev_attr_modalias.attr,
1310        &dev_attr_queue_depth.attr,
1311        &dev_attr_queue_type.attr,
1312        &dev_attr_wwid.attr,
1313        &dev_attr_blacklist.attr,
1314#ifdef CONFIG_SCSI_DH
1315        &dev_attr_dh_state.attr,
1316        &dev_attr_access_state.attr,
1317        &dev_attr_preferred_path.attr,
1318#endif
1319        &dev_attr_queue_ramp_up_period.attr,
1320        REF_EVT(media_change),
1321        REF_EVT(inquiry_change_reported),
1322        REF_EVT(capacity_change_reported),
1323        REF_EVT(soft_threshold_reached),
1324        REF_EVT(mode_parameter_change_reported),
1325        REF_EVT(lun_change_reported),
1326        NULL
1327};
1328
1329static struct bin_attribute *scsi_sdev_bin_attrs[] = {
1330        &dev_attr_vpd_pg0,
1331        &dev_attr_vpd_pg83,
1332        &dev_attr_vpd_pg80,
1333        &dev_attr_vpd_pg89,
1334        &dev_attr_inquiry,
1335        NULL
1336};
1337static struct attribute_group scsi_sdev_attr_group = {
1338        .attrs =        scsi_sdev_attrs,
1339        .bin_attrs =    scsi_sdev_bin_attrs,
1340        .is_visible =   scsi_sdev_attr_is_visible,
1341        .is_bin_visible = scsi_sdev_bin_attr_is_visible,
1342};
1343
1344static const struct attribute_group *scsi_sdev_attr_groups[] = {
1345        &scsi_sdev_attr_group,
1346        NULL
1347};
1348
1349static int scsi_target_add(struct scsi_target *starget)
1350{
1351        int error;
1352
1353        if (starget->state != STARGET_CREATED)
1354                return 0;
1355
1356        error = device_add(&starget->dev);
1357        if (error) {
1358                dev_err(&starget->dev, "target device_add failed, error %d\n", error);
1359                return error;
1360        }
1361        transport_add_device(&starget->dev);
1362        starget->state = STARGET_RUNNING;
1363
1364        pm_runtime_set_active(&starget->dev);
1365        pm_runtime_enable(&starget->dev);
1366        device_enable_async_suspend(&starget->dev);
1367
1368        return 0;
1369}
1370
1371/**
1372 * scsi_sysfs_add_sdev - add scsi device to sysfs
1373 * @sdev:       scsi_device to add
1374 *
1375 * Return value:
1376 *      0 on Success / non-zero on Failure
1377 **/
1378int scsi_sysfs_add_sdev(struct scsi_device *sdev)
1379{
1380        int error, i;
1381        struct request_queue *rq = sdev->request_queue;
1382        struct scsi_target *starget = sdev->sdev_target;
1383
1384        error = scsi_target_add(starget);
1385        if (error)
1386                return error;
1387
1388        transport_configure_device(&starget->dev);
1389
1390        device_enable_async_suspend(&sdev->sdev_gendev);
1391        scsi_autopm_get_target(starget);
1392        pm_runtime_set_active(&sdev->sdev_gendev);
1393        pm_runtime_forbid(&sdev->sdev_gendev);
1394        pm_runtime_enable(&sdev->sdev_gendev);
1395        scsi_autopm_put_target(starget);
1396
1397        scsi_autopm_get_device(sdev);
1398
1399        scsi_dh_add_device(sdev);
1400
1401        error = device_add(&sdev->sdev_gendev);
1402        if (error) {
1403                sdev_printk(KERN_INFO, sdev,
1404                                "failed to add device: %d\n", error);
1405                return error;
1406        }
1407
1408        device_enable_async_suspend(&sdev->sdev_dev);
1409        error = device_add(&sdev->sdev_dev);
1410        if (error) {
1411                sdev_printk(KERN_INFO, sdev,
1412                                "failed to add class device: %d\n", error);
1413                device_del(&sdev->sdev_gendev);
1414                return error;
1415        }
1416        transport_add_device(&sdev->sdev_gendev);
1417        sdev->is_visible = 1;
1418
1419        error = bsg_scsi_register_queue(rq, &sdev->sdev_gendev);
1420        if (error)
1421                /* we're treating error on bsg register as non-fatal,
1422                 * so pretend nothing went wrong */
1423                sdev_printk(KERN_INFO, sdev,
1424                            "Failed to register bsg queue, errno=%d\n", error);
1425
1426        /* add additional host specific attributes */
1427        if (sdev->host->hostt->sdev_attrs) {
1428                for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
1429                        error = device_create_file(&sdev->sdev_gendev,
1430                                        sdev->host->hostt->sdev_attrs[i]);
1431                        if (error)
1432                                return error;
1433                }
1434        }
1435
1436        if (sdev->host->hostt->sdev_groups) {
1437                error = sysfs_create_groups(&sdev->sdev_gendev.kobj,
1438                                sdev->host->hostt->sdev_groups);
1439                if (error)
1440                        return error;
1441        }
1442
1443        scsi_autopm_put_device(sdev);
1444        return error;
1445}
1446
1447void __scsi_remove_device(struct scsi_device *sdev)
1448{
1449        struct device *dev = &sdev->sdev_gendev;
1450        int res;
1451
1452        /*
1453         * This cleanup path is not reentrant and while it is impossible
1454         * to get a new reference with scsi_device_get() someone can still
1455         * hold a previously acquired one.
1456         */
1457        if (sdev->sdev_state == SDEV_DEL)
1458                return;
1459
1460        if (sdev->is_visible) {
1461                /*
1462                 * If scsi_internal_target_block() is running concurrently,
1463                 * wait until it has finished before changing the device state.
1464                 */
1465                mutex_lock(&sdev->state_mutex);
1466                /*
1467                 * If blocked, we go straight to DEL and restart the queue so
1468                 * any commands issued during driver shutdown (like sync
1469                 * cache) are errored immediately.
1470                 */
1471                res = scsi_device_set_state(sdev, SDEV_CANCEL);
1472                if (res != 0) {
1473                        res = scsi_device_set_state(sdev, SDEV_DEL);
1474                        if (res == 0)
1475                                scsi_start_queue(sdev);
1476                }
1477                mutex_unlock(&sdev->state_mutex);
1478
1479                if (res != 0)
1480                        return;
1481
1482                if (sdev->host->hostt->sdev_groups)
1483                        sysfs_remove_groups(&sdev->sdev_gendev.kobj,
1484                                        sdev->host->hostt->sdev_groups);
1485
1486                bsg_unregister_queue(sdev->request_queue);
1487                device_unregister(&sdev->sdev_dev);
1488                transport_remove_device(dev);
1489                device_del(dev);
1490        } else
1491                put_device(&sdev->sdev_dev);
1492
1493        /*
1494         * Stop accepting new requests and wait until all queuecommand() and
1495         * scsi_run_queue() invocations have finished before tearing down the
1496         * device.
1497         */
1498        mutex_lock(&sdev->state_mutex);
1499        scsi_device_set_state(sdev, SDEV_DEL);
1500        mutex_unlock(&sdev->state_mutex);
1501
1502        blk_cleanup_queue(sdev->request_queue);
1503        cancel_work_sync(&sdev->requeue_work);
1504
1505        if (sdev->host->hostt->slave_destroy)
1506                sdev->host->hostt->slave_destroy(sdev);
1507        transport_destroy_device(dev);
1508
1509        /*
1510         * Paired with the kref_get() in scsi_sysfs_initialize().  We have
1511         * removed sysfs visibility from the device, so make the target
1512         * invisible if this was the last device underneath it.
1513         */
1514        scsi_target_reap(scsi_target(sdev));
1515
1516        put_device(dev);
1517}
1518
1519/**
1520 * scsi_remove_device - unregister a device from the scsi bus
1521 * @sdev:       scsi_device to unregister
1522 **/
1523void scsi_remove_device(struct scsi_device *sdev)
1524{
1525        struct Scsi_Host *shost = sdev->host;
1526
1527        mutex_lock(&shost->scan_mutex);
1528        __scsi_remove_device(sdev);
1529        mutex_unlock(&shost->scan_mutex);
1530}
1531EXPORT_SYMBOL(scsi_remove_device);
1532
1533static void __scsi_remove_target(struct scsi_target *starget)
1534{
1535        struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1536        unsigned long flags;
1537        struct scsi_device *sdev;
1538
1539        spin_lock_irqsave(shost->host_lock, flags);
1540 restart:
1541        list_for_each_entry(sdev, &shost->__devices, siblings) {
1542                /*
1543                 * We cannot call scsi_device_get() here, as
1544                 * we might've been called from rmmod() causing
1545                 * scsi_device_get() to fail the module_is_live()
1546                 * check.
1547                 */
1548                if (sdev->channel != starget->channel ||
1549                    sdev->id != starget->id)
1550                        continue;
1551                if (sdev->sdev_state == SDEV_DEL ||
1552                    sdev->sdev_state == SDEV_CANCEL ||
1553                    !get_device(&sdev->sdev_gendev))
1554                        continue;
1555                spin_unlock_irqrestore(shost->host_lock, flags);
1556                scsi_remove_device(sdev);
1557                put_device(&sdev->sdev_gendev);
1558                spin_lock_irqsave(shost->host_lock, flags);
1559                goto restart;
1560        }
1561        spin_unlock_irqrestore(shost->host_lock, flags);
1562}
1563
1564/**
1565 * scsi_remove_target - try to remove a target and all its devices
1566 * @dev: generic starget or parent of generic stargets to be removed
1567 *
1568 * Note: This is slightly racy.  It is possible that if the user
1569 * requests the addition of another device then the target won't be
1570 * removed.
1571 */
1572void scsi_remove_target(struct device *dev)
1573{
1574        struct Scsi_Host *shost = dev_to_shost(dev->parent);
1575        struct scsi_target *starget;
1576        unsigned long flags;
1577
1578restart:
1579        spin_lock_irqsave(shost->host_lock, flags);
1580        list_for_each_entry(starget, &shost->__targets, siblings) {
1581                if (starget->state == STARGET_DEL ||
1582                    starget->state == STARGET_REMOVE ||
1583                    starget->state == STARGET_CREATED_REMOVE)
1584                        continue;
1585                if (starget->dev.parent == dev || &starget->dev == dev) {
1586                        kref_get(&starget->reap_ref);
1587                        if (starget->state == STARGET_CREATED)
1588                                starget->state = STARGET_CREATED_REMOVE;
1589                        else
1590                                starget->state = STARGET_REMOVE;
1591                        spin_unlock_irqrestore(shost->host_lock, flags);
1592                        __scsi_remove_target(starget);
1593                        scsi_target_reap(starget);
1594                        goto restart;
1595                }
1596        }
1597        spin_unlock_irqrestore(shost->host_lock, flags);
1598}
1599EXPORT_SYMBOL(scsi_remove_target);
1600
1601int scsi_register_driver(struct device_driver *drv)
1602{
1603        drv->bus = &scsi_bus_type;
1604
1605        return driver_register(drv);
1606}
1607EXPORT_SYMBOL(scsi_register_driver);
1608
1609int scsi_register_interface(struct class_interface *intf)
1610{
1611        intf->class = &sdev_class;
1612
1613        return class_interface_register(intf);
1614}
1615EXPORT_SYMBOL(scsi_register_interface);
1616
1617/**
1618 * scsi_sysfs_add_host - add scsi host to subsystem
1619 * @shost:     scsi host struct to add to subsystem
1620 **/
1621int scsi_sysfs_add_host(struct Scsi_Host *shost)
1622{
1623        int error, i;
1624
1625        /* add host specific attributes */
1626        if (shost->hostt->shost_attrs) {
1627                for (i = 0; shost->hostt->shost_attrs[i]; i++) {
1628                        error = device_create_file(&shost->shost_dev,
1629                                        shost->hostt->shost_attrs[i]);
1630                        if (error)
1631                                return error;
1632                }
1633        }
1634
1635        transport_register_device(&shost->shost_gendev);
1636        transport_configure_device(&shost->shost_gendev);
1637        return 0;
1638}
1639
1640static struct device_type scsi_dev_type = {
1641        .name =         "scsi_device",
1642        .release =      scsi_device_dev_release,
1643        .groups =       scsi_sdev_attr_groups,
1644};
1645
1646void scsi_sysfs_device_initialize(struct scsi_device *sdev)
1647{
1648        unsigned long flags;
1649        struct Scsi_Host *shost = sdev->host;
1650        struct scsi_target  *starget = sdev->sdev_target;
1651
1652        device_initialize(&sdev->sdev_gendev);
1653        sdev->sdev_gendev.bus = &scsi_bus_type;
1654        sdev->sdev_gendev.type = &scsi_dev_type;
1655        dev_set_name(&sdev->sdev_gendev, "%d:%d:%d:%llu",
1656                     sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
1657
1658        device_initialize(&sdev->sdev_dev);
1659        sdev->sdev_dev.parent = get_device(&sdev->sdev_gendev);
1660        sdev->sdev_dev.class = &sdev_class;
1661        dev_set_name(&sdev->sdev_dev, "%d:%d:%d:%llu",
1662                     sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
1663        /*
1664         * Get a default scsi_level from the target (derived from sibling
1665         * devices).  This is the best we can do for guessing how to set
1666         * sdev->lun_in_cdb for the initial INQUIRY command.  For LUN 0 the
1667         * setting doesn't matter, because all the bits are zero anyway.
1668         * But it does matter for higher LUNs.
1669         */
1670        sdev->scsi_level = starget->scsi_level;
1671        if (sdev->scsi_level <= SCSI_2 &&
1672                        sdev->scsi_level != SCSI_UNKNOWN &&
1673                        !shost->no_scsi2_lun_in_cdb)
1674                sdev->lun_in_cdb = 1;
1675
1676        transport_setup_device(&sdev->sdev_gendev);
1677        spin_lock_irqsave(shost->host_lock, flags);
1678        list_add_tail(&sdev->same_target_siblings, &starget->devices);
1679        list_add_tail(&sdev->siblings, &shost->__devices);
1680        spin_unlock_irqrestore(shost->host_lock, flags);
1681        /*
1682         * device can now only be removed via __scsi_remove_device() so hold
1683         * the target.  Target will be held in CREATED state until something
1684         * beneath it becomes visible (in which case it moves to RUNNING)
1685         */
1686        kref_get(&starget->reap_ref);
1687}
1688
1689int scsi_is_sdev_device(const struct device *dev)
1690{
1691        return dev->type == &scsi_dev_type;
1692}
1693EXPORT_SYMBOL(scsi_is_sdev_device);
1694
1695/* A blank transport template that is used in drivers that don't
1696 * yet implement Transport Attributes */
1697struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };
1698