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