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/init.h>
  11#include <linux/blkdev.h>
  12#include <linux/device.h>
  13
  14#include <scsi/scsi.h>
  15#include <scsi/scsi_device.h>
  16#include <scsi/scsi_host.h>
  17#include <scsi/scsi_tcq.h>
  18#include <scsi/scsi_transport.h>
  19#include <scsi/scsi_driver.h>
  20
  21#include "scsi_priv.h"
  22#include "scsi_logging.h"
  23
  24static struct device_type scsi_dev_type;
  25
  26static const struct {
  27        enum scsi_device_state  value;
  28        char                    *name;
  29} sdev_states[] = {
  30        { SDEV_CREATED, "created" },
  31        { SDEV_RUNNING, "running" },
  32        { SDEV_CANCEL, "cancel" },
  33        { SDEV_DEL, "deleted" },
  34        { SDEV_QUIESCE, "quiesce" },
  35        { SDEV_OFFLINE, "offline" },
  36        { SDEV_BLOCK,   "blocked" },
  37};
  38
  39const char *scsi_device_state_name(enum scsi_device_state state)
  40{
  41        int i;
  42        char *name = NULL;
  43
  44        for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
  45                if (sdev_states[i].value == state) {
  46                        name = sdev_states[i].name;
  47                        break;
  48                }
  49        }
  50        return name;
  51}
  52
  53static const struct {
  54        enum scsi_host_state    value;
  55        char                    *name;
  56} shost_states[] = {
  57        { SHOST_CREATED, "created" },
  58        { SHOST_RUNNING, "running" },
  59        { SHOST_CANCEL, "cancel" },
  60        { SHOST_DEL, "deleted" },
  61        { SHOST_RECOVERY, "recovery" },
  62        { SHOST_CANCEL_RECOVERY, "cancel/recovery" },
  63        { SHOST_DEL_RECOVERY, "deleted/recovery", },
  64};
  65const char *scsi_host_state_name(enum scsi_host_state state)
  66{
  67        int i;
  68        char *name = NULL;
  69
  70        for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
  71                if (shost_states[i].value == state) {
  72                        name = shost_states[i].name;
  73                        break;
  74                }
  75        }
  76        return name;
  77}
  78
  79static int check_set(unsigned int *val, char *src)
  80{
  81        char *last;
  82
  83        if (strncmp(src, "-", 20) == 0) {
  84                *val = SCAN_WILD_CARD;
  85        } else {
  86                /*
  87                 * Doesn't check for int overflow
  88                 */
  89                *val = simple_strtoul(src, &last, 0);
  90                if (*last != '\0')
  91                        return 1;
  92        }
  93        return 0;
  94}
  95
  96static int scsi_scan(struct Scsi_Host *shost, const char *str)
  97{
  98        char s1[15], s2[15], s3[15], junk;
  99        unsigned int channel, id, lun;
 100        int res;
 101
 102        res = sscanf(str, "%10s %10s %10s %c", s1, s2, s3, &junk);
 103        if (res != 3)
 104                return -EINVAL;
 105        if (check_set(&channel, s1))
 106                return -EINVAL;
 107        if (check_set(&id, s2))
 108                return -EINVAL;
 109        if (check_set(&lun, s3))
 110                return -EINVAL;
 111        if (shost->transportt->user_scan)
 112                res = shost->transportt->user_scan(shost, channel, id, lun);
 113        else
 114                res = scsi_scan_host_selected(shost, channel, id, lun, 1);
 115        return res;
 116}
 117
 118/*
 119 * shost_show_function: macro to create an attr function that can be used to
 120 * show a non-bit field.
 121 */
 122#define shost_show_function(name, field, format_string)                 \
 123static ssize_t                                                          \
 124show_##name (struct device *dev, struct device_attribute *attr,         \
 125             char *buf)                                                 \
 126{                                                                       \
 127        struct Scsi_Host *shost = class_to_shost(dev);                  \
 128        return snprintf (buf, 20, format_string, shost->field);         \
 129}
 130
 131/*
 132 * shost_rd_attr: macro to create a function and attribute variable for a
 133 * read only field.
 134 */
 135#define shost_rd_attr2(name, field, format_string)                      \
 136        shost_show_function(name, field, format_string)                 \
 137static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
 138
 139#define shost_rd_attr(field, format_string) \
 140shost_rd_attr2(field, field, format_string)
 141
 142/*
 143 * Create the actual show/store functions and data structures.
 144 */
 145
 146static ssize_t
 147store_scan(struct device *dev, struct device_attribute *attr,
 148           const char *buf, size_t count)
 149{
 150        struct Scsi_Host *shost = class_to_shost(dev);
 151        int res;
 152
 153        res = scsi_scan(shost, buf);
 154        if (res == 0)
 155                res = count;
 156        return res;
 157};
 158static DEVICE_ATTR(scan, S_IWUSR, NULL, store_scan);
 159
 160static ssize_t
 161store_shost_state(struct device *dev, struct device_attribute *attr,
 162                  const char *buf, size_t count)
 163{
 164        int i;
 165        struct Scsi_Host *shost = class_to_shost(dev);
 166        enum scsi_host_state state = 0;
 167
 168        for (i = 0; i < ARRAY_SIZE(shost_states); i++) {
 169                const int len = strlen(shost_states[i].name);
 170                if (strncmp(shost_states[i].name, buf, len) == 0 &&
 171                   buf[len] == '\n') {
 172                        state = shost_states[i].value;
 173                        break;
 174                }
 175        }
 176        if (!state)
 177                return -EINVAL;
 178
 179        if (scsi_host_set_state(shost, state))
 180                return -EINVAL;
 181        return count;
 182}
 183
 184static ssize_t
 185show_shost_state(struct device *dev, struct device_attribute *attr, char *buf)
 186{
 187        struct Scsi_Host *shost = class_to_shost(dev);
 188        const char *name = scsi_host_state_name(shost->shost_state);
 189
 190        if (!name)
 191                return -EINVAL;
 192
 193        return snprintf(buf, 20, "%s\n", name);
 194}
 195
 196/* DEVICE_ATTR(state) clashes with dev_attr_state for sdev */
 197struct device_attribute dev_attr_hstate =
 198        __ATTR(state, S_IRUGO | S_IWUSR, show_shost_state, store_shost_state);
 199
 200static ssize_t
 201show_shost_mode(unsigned int mode, char *buf)
 202{
 203        ssize_t len = 0;
 204
 205        if (mode & MODE_INITIATOR)
 206                len = sprintf(buf, "%s", "Initiator");
 207
 208        if (mode & MODE_TARGET)
 209                len += sprintf(buf + len, "%s%s", len ? ", " : "", "Target");
 210
 211        len += sprintf(buf + len, "\n");
 212
 213        return len;
 214}
 215
 216static ssize_t
 217show_shost_supported_mode(struct device *dev, struct device_attribute *attr,
 218                          char *buf)
 219{
 220        struct Scsi_Host *shost = class_to_shost(dev);
 221        unsigned int supported_mode = shost->hostt->supported_mode;
 222
 223        if (supported_mode == MODE_UNKNOWN)
 224                /* by default this should be initiator */
 225                supported_mode = MODE_INITIATOR;
 226
 227        return show_shost_mode(supported_mode, buf);
 228}
 229
 230static DEVICE_ATTR(supported_mode, S_IRUGO | S_IWUSR, show_shost_supported_mode, NULL);
 231
 232static ssize_t
 233show_shost_active_mode(struct device *dev,
 234                       struct device_attribute *attr, char *buf)
 235{
 236        struct Scsi_Host *shost = class_to_shost(dev);
 237
 238        if (shost->active_mode == MODE_UNKNOWN)
 239                return snprintf(buf, 20, "unknown\n");
 240        else
 241                return show_shost_mode(shost->active_mode, buf);
 242}
 243
 244static DEVICE_ATTR(active_mode, S_IRUGO | S_IWUSR, show_shost_active_mode, NULL);
 245
 246shost_rd_attr(unique_id, "%u\n");
 247shost_rd_attr(host_busy, "%hu\n");
 248shost_rd_attr(cmd_per_lun, "%hd\n");
 249shost_rd_attr(can_queue, "%hd\n");
 250shost_rd_attr(sg_tablesize, "%hu\n");
 251shost_rd_attr(unchecked_isa_dma, "%d\n");
 252shost_rd_attr(prot_capabilities, "%u\n");
 253shost_rd_attr(prot_guard_type, "%hd\n");
 254shost_rd_attr2(proc_name, hostt->proc_name, "%s\n");
 255
 256static struct attribute *scsi_sysfs_shost_attrs[] = {
 257        &dev_attr_unique_id.attr,
 258        &dev_attr_host_busy.attr,
 259        &dev_attr_cmd_per_lun.attr,
 260        &dev_attr_can_queue.attr,
 261        &dev_attr_sg_tablesize.attr,
 262        &dev_attr_unchecked_isa_dma.attr,
 263        &dev_attr_proc_name.attr,
 264        &dev_attr_scan.attr,
 265        &dev_attr_hstate.attr,
 266        &dev_attr_supported_mode.attr,
 267        &dev_attr_active_mode.attr,
 268        &dev_attr_prot_capabilities.attr,
 269        &dev_attr_prot_guard_type.attr,
 270        NULL
 271};
 272
 273struct attribute_group scsi_shost_attr_group = {
 274        .attrs =        scsi_sysfs_shost_attrs,
 275};
 276
 277struct attribute_group *scsi_sysfs_shost_attr_groups[] = {
 278        &scsi_shost_attr_group,
 279        NULL
 280};
 281
 282static void scsi_device_cls_release(struct device *class_dev)
 283{
 284        struct scsi_device *sdev;
 285
 286        sdev = class_to_sdev(class_dev);
 287        put_device(&sdev->sdev_gendev);
 288}
 289
 290static void scsi_device_dev_release_usercontext(struct work_struct *work)
 291{
 292        struct scsi_device *sdev;
 293        struct device *parent;
 294        struct scsi_target *starget;
 295        struct list_head *this, *tmp;
 296        unsigned long flags;
 297
 298        sdev = container_of(work, struct scsi_device, ew.work);
 299
 300        parent = sdev->sdev_gendev.parent;
 301        starget = to_scsi_target(parent);
 302
 303        spin_lock_irqsave(sdev->host->host_lock, flags);
 304        starget->reap_ref++;
 305        list_del(&sdev->siblings);
 306        list_del(&sdev->same_target_siblings);
 307        list_del(&sdev->starved_entry);
 308        spin_unlock_irqrestore(sdev->host->host_lock, flags);
 309
 310        cancel_work_sync(&sdev->event_work);
 311
 312        list_for_each_safe(this, tmp, &sdev->event_list) {
 313                struct scsi_event *evt;
 314
 315                evt = list_entry(this, struct scsi_event, node);
 316                list_del(&evt->node);
 317                kfree(evt);
 318        }
 319
 320        /* NULL queue means the device can't be used */
 321        sdev->request_queue = NULL;
 322
 323        scsi_target_reap(scsi_target(sdev));
 324
 325        kfree(sdev->inquiry);
 326        kfree(sdev);
 327
 328        if (parent)
 329                put_device(parent);
 330}
 331
 332static void scsi_device_dev_release(struct device *dev)
 333{
 334        struct scsi_device *sdp = to_scsi_device(dev);
 335        execute_in_process_context(scsi_device_dev_release_usercontext,
 336                                   &sdp->ew);
 337}
 338
 339static struct class sdev_class = {
 340        .name           = "scsi_device",
 341        .dev_release    = scsi_device_cls_release,
 342};
 343
 344/* all probing is done in the individual ->probe routines */
 345static int scsi_bus_match(struct device *dev, struct device_driver *gendrv)
 346{
 347        struct scsi_device *sdp;
 348
 349        if (dev->type != &scsi_dev_type)
 350                return 0;
 351
 352        sdp = to_scsi_device(dev);
 353        if (sdp->no_uld_attach)
 354                return 0;
 355        return (sdp->inq_periph_qual == SCSI_INQ_PQ_CON)? 1: 0;
 356}
 357
 358static int scsi_bus_uevent(struct device *dev, struct kobj_uevent_env *env)
 359{
 360        struct scsi_device *sdev;
 361
 362        if (dev->type != &scsi_dev_type)
 363                return 0;
 364
 365        sdev = to_scsi_device(dev);
 366
 367        add_uevent_var(env, "MODALIAS=" SCSI_DEVICE_MODALIAS_FMT, sdev->type);
 368        return 0;
 369}
 370
 371static int scsi_bus_suspend(struct device * dev, pm_message_t state)
 372{
 373        struct device_driver *drv;
 374        struct scsi_device *sdev;
 375        int err;
 376
 377        if (dev->type != &scsi_dev_type)
 378                return 0;
 379
 380        drv = dev->driver;
 381        sdev = to_scsi_device(dev);
 382
 383        err = scsi_device_quiesce(sdev);
 384        if (err)
 385                return err;
 386
 387        if (drv && drv->suspend) {
 388                err = drv->suspend(dev, state);
 389                if (err)
 390                        return err;
 391        }
 392
 393        return 0;
 394}
 395
 396static int scsi_bus_resume(struct device * dev)
 397{
 398        struct device_driver *drv;
 399        struct scsi_device *sdev;
 400        int err = 0;
 401
 402        if (dev->type != &scsi_dev_type)
 403                return 0;
 404
 405        drv = dev->driver;
 406        sdev = to_scsi_device(dev);
 407
 408        if (drv && drv->resume)
 409                err = drv->resume(dev);
 410
 411        scsi_device_resume(sdev);
 412
 413        return err;
 414}
 415
 416static int scsi_bus_remove(struct device *dev)
 417{
 418        struct device_driver *drv = dev->driver;
 419        struct scsi_device *sdev = to_scsi_device(dev);
 420        int err = 0;
 421
 422        /* reset the prep_fn back to the default since the
 423         * driver may have altered it and it's being removed */
 424        blk_queue_prep_rq(sdev->request_queue, scsi_prep_fn);
 425
 426        if (drv && drv->remove)
 427                err = drv->remove(dev);
 428
 429        return 0;
 430}
 431
 432struct bus_type scsi_bus_type = {
 433        .name           = "scsi",
 434        .match          = scsi_bus_match,
 435        .uevent         = scsi_bus_uevent,
 436        .suspend        = scsi_bus_suspend,
 437        .resume         = scsi_bus_resume,
 438        .remove         = scsi_bus_remove,
 439};
 440EXPORT_SYMBOL_GPL(scsi_bus_type);
 441
 442int scsi_sysfs_register(void)
 443{
 444        int error;
 445
 446        error = bus_register(&scsi_bus_type);
 447        if (!error) {
 448                error = class_register(&sdev_class);
 449                if (error)
 450                        bus_unregister(&scsi_bus_type);
 451        }
 452
 453        return error;
 454}
 455
 456void scsi_sysfs_unregister(void)
 457{
 458        class_unregister(&sdev_class);
 459        bus_unregister(&scsi_bus_type);
 460}
 461
 462/*
 463 * sdev_show_function: macro to create an attr function that can be used to
 464 * show a non-bit field.
 465 */
 466#define sdev_show_function(field, format_string)                                \
 467static ssize_t                                                          \
 468sdev_show_##field (struct device *dev, struct device_attribute *attr,   \
 469                   char *buf)                                           \
 470{                                                                       \
 471        struct scsi_device *sdev;                                       \
 472        sdev = to_scsi_device(dev);                                     \
 473        return snprintf (buf, 20, format_string, sdev->field);          \
 474}                                                                       \
 475
 476/*
 477 * sdev_rd_attr: macro to create a function and attribute variable for a
 478 * read only field.
 479 */
 480#define sdev_rd_attr(field, format_string)                              \
 481        sdev_show_function(field, format_string)                        \
 482static DEVICE_ATTR(field, S_IRUGO, sdev_show_##field, NULL);
 483
 484
 485/*
 486 * sdev_rd_attr: create a function and attribute variable for a
 487 * read/write field.
 488 */
 489#define sdev_rw_attr(field, format_string)                              \
 490        sdev_show_function(field, format_string)                                \
 491                                                                        \
 492static ssize_t                                                          \
 493sdev_store_##field (struct device *dev, struct device_attribute *attr,  \
 494                    const char *buf, size_t count)                      \
 495{                                                                       \
 496        struct scsi_device *sdev;                                       \
 497        sdev = to_scsi_device(dev);                                     \
 498        snscanf (buf, 20, format_string, &sdev->field);                 \
 499        return count;                                                   \
 500}                                                                       \
 501static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
 502
 503/* Currently we don't export bit fields, but we might in future,
 504 * so leave this code in */
 505#if 0
 506/*
 507 * sdev_rd_attr: create a function and attribute variable for a
 508 * read/write bit field.
 509 */
 510#define sdev_rw_attr_bit(field)                                         \
 511        sdev_show_function(field, "%d\n")                                       \
 512                                                                        \
 513static ssize_t                                                          \
 514sdev_store_##field (struct device *dev, struct device_attribute *attr,  \
 515                    const char *buf, size_t count)                      \
 516{                                                                       \
 517        int ret;                                                        \
 518        struct scsi_device *sdev;                                       \
 519        ret = scsi_sdev_check_buf_bit(buf);                             \
 520        if (ret >= 0)   {                                               \
 521                sdev = to_scsi_device(dev);                             \
 522                sdev->field = ret;                                      \
 523                ret = count;                                            \
 524        }                                                               \
 525        return ret;                                                     \
 526}                                                                       \
 527static DEVICE_ATTR(field, S_IRUGO | S_IWUSR, sdev_show_##field, sdev_store_##field);
 528
 529/*
 530 * scsi_sdev_check_buf_bit: return 0 if buf is "0", return 1 if buf is "1",
 531 * else return -EINVAL.
 532 */
 533static int scsi_sdev_check_buf_bit(const char *buf)
 534{
 535        if ((buf[1] == '\0') || ((buf[1] == '\n') && (buf[2] == '\0'))) {
 536                if (buf[0] == '1')
 537                        return 1;
 538                else if (buf[0] == '0')
 539                        return 0;
 540                else 
 541                        return -EINVAL;
 542        } else
 543                return -EINVAL;
 544}
 545#endif
 546/*
 547 * Create the actual show/store functions and data structures.
 548 */
 549sdev_rd_attr (device_blocked, "%d\n");
 550sdev_rd_attr (queue_depth, "%d\n");
 551sdev_rd_attr (type, "%d\n");
 552sdev_rd_attr (scsi_level, "%d\n");
 553sdev_rd_attr (vendor, "%.8s\n");
 554sdev_rd_attr (model, "%.16s\n");
 555sdev_rd_attr (rev, "%.4s\n");
 556
 557static ssize_t
 558sdev_show_timeout (struct device *dev, struct device_attribute *attr, char *buf)
 559{
 560        struct scsi_device *sdev;
 561        sdev = to_scsi_device(dev);
 562        return snprintf (buf, 20, "%d\n", sdev->timeout / HZ);
 563}
 564
 565static ssize_t
 566sdev_store_timeout (struct device *dev, struct device_attribute *attr,
 567                    const char *buf, size_t count)
 568{
 569        struct scsi_device *sdev;
 570        int timeout;
 571        sdev = to_scsi_device(dev);
 572        sscanf (buf, "%d\n", &timeout);
 573        sdev->timeout = timeout * HZ;
 574        return count;
 575}
 576static DEVICE_ATTR(timeout, S_IRUGO | S_IWUSR, sdev_show_timeout, sdev_store_timeout);
 577
 578static ssize_t
 579store_rescan_field (struct device *dev, struct device_attribute *attr,
 580                    const char *buf, size_t count)
 581{
 582        scsi_rescan_device(dev);
 583        return count;
 584}
 585static DEVICE_ATTR(rescan, S_IWUSR, NULL, store_rescan_field);
 586
 587static void sdev_store_delete_callback(struct device *dev)
 588{
 589        scsi_remove_device(to_scsi_device(dev));
 590}
 591
 592static ssize_t
 593sdev_store_delete(struct device *dev, struct device_attribute *attr,
 594                  const char *buf, size_t count)
 595{
 596        int rc;
 597
 598        /* An attribute cannot be unregistered by one of its own methods,
 599         * so we have to use this roundabout approach.
 600         */
 601        rc = device_schedule_callback(dev, sdev_store_delete_callback);
 602        if (rc)
 603                count = rc;
 604        return count;
 605};
 606static DEVICE_ATTR(delete, S_IWUSR, NULL, sdev_store_delete);
 607
 608static ssize_t
 609store_state_field(struct device *dev, struct device_attribute *attr,
 610                  const char *buf, size_t count)
 611{
 612        int i;
 613        struct scsi_device *sdev = to_scsi_device(dev);
 614        enum scsi_device_state state = 0;
 615
 616        for (i = 0; i < ARRAY_SIZE(sdev_states); i++) {
 617                const int len = strlen(sdev_states[i].name);
 618                if (strncmp(sdev_states[i].name, buf, len) == 0 &&
 619                   buf[len] == '\n') {
 620                        state = sdev_states[i].value;
 621                        break;
 622                }
 623        }
 624        if (!state)
 625                return -EINVAL;
 626
 627        if (scsi_device_set_state(sdev, state))
 628                return -EINVAL;
 629        return count;
 630}
 631
 632static ssize_t
 633show_state_field(struct device *dev, struct device_attribute *attr, char *buf)
 634{
 635        struct scsi_device *sdev = to_scsi_device(dev);
 636        const char *name = scsi_device_state_name(sdev->sdev_state);
 637
 638        if (!name)
 639                return -EINVAL;
 640
 641        return snprintf(buf, 20, "%s\n", name);
 642}
 643
 644static DEVICE_ATTR(state, S_IRUGO | S_IWUSR, show_state_field, store_state_field);
 645
 646static ssize_t
 647show_queue_type_field(struct device *dev, struct device_attribute *attr,
 648                      char *buf)
 649{
 650        struct scsi_device *sdev = to_scsi_device(dev);
 651        const char *name = "none";
 652
 653        if (sdev->ordered_tags)
 654                name = "ordered";
 655        else if (sdev->simple_tags)
 656                name = "simple";
 657
 658        return snprintf(buf, 20, "%s\n", name);
 659}
 660
 661static DEVICE_ATTR(queue_type, S_IRUGO, show_queue_type_field, NULL);
 662
 663static ssize_t
 664show_iostat_counterbits(struct device *dev, struct device_attribute *attr,                              char *buf)
 665{
 666        return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
 667}
 668
 669static DEVICE_ATTR(iocounterbits, S_IRUGO, show_iostat_counterbits, NULL);
 670
 671#define show_sdev_iostat(field)                                         \
 672static ssize_t                                                          \
 673show_iostat_##field(struct device *dev, struct device_attribute *attr,  \
 674                    char *buf)                                          \
 675{                                                                       \
 676        struct scsi_device *sdev = to_scsi_device(dev);                 \
 677        unsigned long long count = atomic_read(&sdev->field);           \
 678        return snprintf(buf, 20, "0x%llx\n", count);                    \
 679}                                                                       \
 680static DEVICE_ATTR(field, S_IRUGO, show_iostat_##field, NULL)
 681
 682show_sdev_iostat(iorequest_cnt);
 683show_sdev_iostat(iodone_cnt);
 684show_sdev_iostat(ioerr_cnt);
 685
 686static ssize_t
 687sdev_show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
 688{
 689        struct scsi_device *sdev;
 690        sdev = to_scsi_device(dev);
 691        return snprintf (buf, 20, SCSI_DEVICE_MODALIAS_FMT "\n", sdev->type);
 692}
 693static DEVICE_ATTR(modalias, S_IRUGO, sdev_show_modalias, NULL);
 694
 695#define DECLARE_EVT_SHOW(name, Cap_name)                                \
 696static ssize_t                                                          \
 697sdev_show_evt_##name(struct device *dev, struct device_attribute *attr, \
 698                     char *buf)                                         \
 699{                                                                       \
 700        struct scsi_device *sdev = to_scsi_device(dev);                 \
 701        int val = test_bit(SDEV_EVT_##Cap_name, sdev->supported_events);\
 702        return snprintf(buf, 20, "%d\n", val);                          \
 703}
 704
 705#define DECLARE_EVT_STORE(name, Cap_name)                               \
 706static ssize_t                                                          \
 707sdev_store_evt_##name(struct device *dev, struct device_attribute *attr,\
 708                      const char *buf, size_t count)                    \
 709{                                                                       \
 710        struct scsi_device *sdev = to_scsi_device(dev);                 \
 711        int val = simple_strtoul(buf, NULL, 0);                         \
 712        if (val == 0)                                                   \
 713                clear_bit(SDEV_EVT_##Cap_name, sdev->supported_events); \
 714        else if (val == 1)                                              \
 715                set_bit(SDEV_EVT_##Cap_name, sdev->supported_events);   \
 716        else                                                            \
 717                return -EINVAL;                                         \
 718        return count;                                                   \
 719}
 720
 721#define DECLARE_EVT(name, Cap_name)                                     \
 722        DECLARE_EVT_SHOW(name, Cap_name)                                \
 723        DECLARE_EVT_STORE(name, Cap_name)                               \
 724        static DEVICE_ATTR(evt_##name, S_IRUGO, sdev_show_evt_##name,   \
 725                           sdev_store_evt_##name);
 726#define REF_EVT(name) &dev_attr_evt_##name.attr
 727
 728DECLARE_EVT(media_change, MEDIA_CHANGE)
 729
 730/* Default template for device attributes.  May NOT be modified */
 731static struct attribute *scsi_sdev_attrs[] = {
 732        &dev_attr_device_blocked.attr,
 733        &dev_attr_type.attr,
 734        &dev_attr_scsi_level.attr,
 735        &dev_attr_vendor.attr,
 736        &dev_attr_model.attr,
 737        &dev_attr_rev.attr,
 738        &dev_attr_rescan.attr,
 739        &dev_attr_delete.attr,
 740        &dev_attr_state.attr,
 741        &dev_attr_timeout.attr,
 742        &dev_attr_iocounterbits.attr,
 743        &dev_attr_iorequest_cnt.attr,
 744        &dev_attr_iodone_cnt.attr,
 745        &dev_attr_ioerr_cnt.attr,
 746        &dev_attr_modalias.attr,
 747        REF_EVT(media_change),
 748        NULL
 749};
 750
 751static struct attribute_group scsi_sdev_attr_group = {
 752        .attrs =        scsi_sdev_attrs,
 753};
 754
 755static struct attribute_group *scsi_sdev_attr_groups[] = {
 756        &scsi_sdev_attr_group,
 757        NULL
 758};
 759
 760static ssize_t
 761sdev_store_queue_depth_rw(struct device *dev, struct device_attribute *attr,
 762                          const char *buf, size_t count)
 763{
 764        int depth, retval;
 765        struct scsi_device *sdev = to_scsi_device(dev);
 766        struct scsi_host_template *sht = sdev->host->hostt;
 767
 768        if (!sht->change_queue_depth)
 769                return -EINVAL;
 770
 771        depth = simple_strtoul(buf, NULL, 0);
 772
 773        if (depth < 1)
 774                return -EINVAL;
 775
 776        retval = sht->change_queue_depth(sdev, depth);
 777        if (retval < 0)
 778                return retval;
 779
 780        return count;
 781}
 782
 783static struct device_attribute sdev_attr_queue_depth_rw =
 784        __ATTR(queue_depth, S_IRUGO | S_IWUSR, sdev_show_queue_depth,
 785               sdev_store_queue_depth_rw);
 786
 787static ssize_t
 788sdev_store_queue_type_rw(struct device *dev, struct device_attribute *attr,
 789                         const char *buf, size_t count)
 790{
 791        struct scsi_device *sdev = to_scsi_device(dev);
 792        struct scsi_host_template *sht = sdev->host->hostt;
 793        int tag_type = 0, retval;
 794        int prev_tag_type = scsi_get_tag_type(sdev);
 795
 796        if (!sdev->tagged_supported || !sht->change_queue_type)
 797                return -EINVAL;
 798
 799        if (strncmp(buf, "ordered", 7) == 0)
 800                tag_type = MSG_ORDERED_TAG;
 801        else if (strncmp(buf, "simple", 6) == 0)
 802                tag_type = MSG_SIMPLE_TAG;
 803        else if (strncmp(buf, "none", 4) != 0)
 804                return -EINVAL;
 805
 806        if (tag_type == prev_tag_type)
 807                return count;
 808
 809        retval = sht->change_queue_type(sdev, tag_type);
 810        if (retval < 0)
 811                return retval;
 812
 813        return count;
 814}
 815
 816static int scsi_target_add(struct scsi_target *starget)
 817{
 818        int error;
 819
 820        if (starget->state != STARGET_CREATED)
 821                return 0;
 822
 823        error = device_add(&starget->dev);
 824        if (error) {
 825                dev_err(&starget->dev, "target device_add failed, error %d\n", error);
 826                get_device(&starget->dev);
 827                scsi_target_reap(starget);
 828                put_device(&starget->dev);
 829                return error;
 830        }
 831        transport_add_device(&starget->dev);
 832        starget->state = STARGET_RUNNING;
 833
 834        return 0;
 835}
 836
 837static struct device_attribute sdev_attr_queue_type_rw =
 838        __ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
 839               sdev_store_queue_type_rw);
 840
 841/**
 842 * scsi_sysfs_add_sdev - add scsi device to sysfs
 843 * @sdev:       scsi_device to add
 844 *
 845 * Return value:
 846 *      0 on Success / non-zero on Failure
 847 **/
 848int scsi_sysfs_add_sdev(struct scsi_device *sdev)
 849{
 850        int error, i;
 851        struct request_queue *rq = sdev->request_queue;
 852        struct scsi_target *starget = sdev->sdev_target;
 853
 854        if ((error = scsi_device_set_state(sdev, SDEV_RUNNING)) != 0)
 855                return error;
 856
 857        error = scsi_target_add(starget);
 858        if (error)
 859                return error;
 860
 861        transport_configure_device(&starget->dev);
 862        error = device_add(&sdev->sdev_gendev);
 863        if (error) {
 864                put_device(sdev->sdev_gendev.parent);
 865                printk(KERN_INFO "error 1\n");
 866                return error;
 867        }
 868        error = device_add(&sdev->sdev_dev);
 869        if (error) {
 870                printk(KERN_INFO "error 2\n");
 871                goto clean_device;
 872        }
 873
 874        /* take a reference for the sdev_dev; this is
 875         * released by the sdev_class .release */
 876        get_device(&sdev->sdev_gendev);
 877
 878        /* create queue files, which may be writable, depending on the host */
 879        if (sdev->host->hostt->change_queue_depth)
 880                error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_depth_rw);
 881        else
 882                error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_depth);
 883        if (error) {
 884                __scsi_remove_device(sdev);
 885                goto out;
 886        }
 887        if (sdev->host->hostt->change_queue_type)
 888                error = device_create_file(&sdev->sdev_gendev, &sdev_attr_queue_type_rw);
 889        else
 890                error = device_create_file(&sdev->sdev_gendev, &dev_attr_queue_type);
 891        if (error) {
 892                __scsi_remove_device(sdev);
 893                goto out;
 894        }
 895
 896        error = bsg_register_queue(rq, &sdev->sdev_gendev, NULL, NULL);
 897
 898        if (error)
 899                sdev_printk(KERN_INFO, sdev,
 900                            "Failed to register bsg queue, errno=%d\n", error);
 901
 902        /* we're treating error on bsg register as non-fatal, so pretend
 903         * nothing went wrong */
 904        error = 0;
 905
 906        /* add additional host specific attributes */
 907        if (sdev->host->hostt->sdev_attrs) {
 908                for (i = 0; sdev->host->hostt->sdev_attrs[i]; i++) {
 909                        error = device_create_file(&sdev->sdev_gendev,
 910                                        sdev->host->hostt->sdev_attrs[i]);
 911                        if (error) {
 912                                __scsi_remove_device(sdev);
 913                                goto out;
 914                        }
 915                }
 916        }
 917
 918        transport_add_device(&sdev->sdev_gendev);
 919 out:
 920        return error;
 921
 922 clean_device:
 923        scsi_device_set_state(sdev, SDEV_CANCEL);
 924
 925        device_del(&sdev->sdev_gendev);
 926        transport_destroy_device(&sdev->sdev_gendev);
 927        put_device(&sdev->sdev_gendev);
 928
 929        return error;
 930}
 931
 932void __scsi_remove_device(struct scsi_device *sdev)
 933{
 934        struct device *dev = &sdev->sdev_gendev;
 935
 936        if (scsi_device_set_state(sdev, SDEV_CANCEL) != 0)
 937                return;
 938
 939        bsg_unregister_queue(sdev->request_queue);
 940        device_unregister(&sdev->sdev_dev);
 941        transport_remove_device(dev);
 942        device_del(dev);
 943        scsi_device_set_state(sdev, SDEV_DEL);
 944        if (sdev->host->hostt->slave_destroy)
 945                sdev->host->hostt->slave_destroy(sdev);
 946        transport_destroy_device(dev);
 947
 948        /* cause the request function to reject all I/O requests */
 949        sdev->request_queue->queuedata = NULL;
 950
 951        /* Freeing the queue signals to block that we're done */
 952        scsi_free_queue(sdev->request_queue);
 953        put_device(dev);
 954}
 955
 956/**
 957 * scsi_remove_device - unregister a device from the scsi bus
 958 * @sdev:       scsi_device to unregister
 959 **/
 960void scsi_remove_device(struct scsi_device *sdev)
 961{
 962        struct Scsi_Host *shost = sdev->host;
 963
 964        mutex_lock(&shost->scan_mutex);
 965        __scsi_remove_device(sdev);
 966        mutex_unlock(&shost->scan_mutex);
 967}
 968EXPORT_SYMBOL(scsi_remove_device);
 969
 970static void __scsi_remove_target(struct scsi_target *starget)
 971{
 972        struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
 973        unsigned long flags;
 974        struct scsi_device *sdev;
 975
 976        spin_lock_irqsave(shost->host_lock, flags);
 977        starget->reap_ref++;
 978 restart:
 979        list_for_each_entry(sdev, &shost->__devices, siblings) {
 980                if (sdev->channel != starget->channel ||
 981                    sdev->id != starget->id ||
 982                    scsi_device_get(sdev))
 983                        continue;
 984                spin_unlock_irqrestore(shost->host_lock, flags);
 985                scsi_remove_device(sdev);
 986                scsi_device_put(sdev);
 987                spin_lock_irqsave(shost->host_lock, flags);
 988                goto restart;
 989        }
 990        spin_unlock_irqrestore(shost->host_lock, flags);
 991        scsi_target_reap(starget);
 992}
 993
 994static int __remove_child (struct device * dev, void * data)
 995{
 996        if (scsi_is_target_device(dev))
 997                __scsi_remove_target(to_scsi_target(dev));
 998        return 0;
 999}
1000
1001/**
1002 * scsi_remove_target - try to remove a target and all its devices
1003 * @dev: generic starget or parent of generic stargets to be removed
1004 *
1005 * Note: This is slightly racy.  It is possible that if the user
1006 * requests the addition of another device then the target won't be
1007 * removed.
1008 */
1009void scsi_remove_target(struct device *dev)
1010{
1011        struct device *rdev;
1012
1013        if (scsi_is_target_device(dev)) {
1014                __scsi_remove_target(to_scsi_target(dev));
1015                return;
1016        }
1017
1018        rdev = get_device(dev);
1019        device_for_each_child(dev, NULL, __remove_child);
1020        put_device(rdev);
1021}
1022EXPORT_SYMBOL(scsi_remove_target);
1023
1024int scsi_register_driver(struct device_driver *drv)
1025{
1026        drv->bus = &scsi_bus_type;
1027
1028        return driver_register(drv);
1029}
1030EXPORT_SYMBOL(scsi_register_driver);
1031
1032int scsi_register_interface(struct class_interface *intf)
1033{
1034        intf->class = &sdev_class;
1035
1036        return class_interface_register(intf);
1037}
1038EXPORT_SYMBOL(scsi_register_interface);
1039
1040/**
1041 * scsi_sysfs_add_host - add scsi host to subsystem
1042 * @shost:     scsi host struct to add to subsystem
1043 * @dev:       parent struct device pointer
1044 **/
1045int scsi_sysfs_add_host(struct Scsi_Host *shost)
1046{
1047        int error, i;
1048
1049        /* add host specific attributes */
1050        if (shost->hostt->shost_attrs) {
1051                for (i = 0; shost->hostt->shost_attrs[i]; i++) {
1052                        error = device_create_file(&shost->shost_dev,
1053                                        shost->hostt->shost_attrs[i]);
1054                        if (error)
1055                                return error;
1056                }
1057        }
1058
1059        transport_register_device(&shost->shost_gendev);
1060        transport_configure_device(&shost->shost_gendev);
1061        return 0;
1062}
1063
1064static struct device_type scsi_dev_type = {
1065        .name =         "scsi_device",
1066        .release =      scsi_device_dev_release,
1067        .groups =       scsi_sdev_attr_groups,
1068};
1069
1070void scsi_sysfs_device_initialize(struct scsi_device *sdev)
1071{
1072        unsigned long flags;
1073        struct Scsi_Host *shost = sdev->host;
1074        struct scsi_target  *starget = sdev->sdev_target;
1075
1076        device_initialize(&sdev->sdev_gendev);
1077        sdev->sdev_gendev.bus = &scsi_bus_type;
1078        sdev->sdev_gendev.type = &scsi_dev_type;
1079        sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
1080                sdev->host->host_no, sdev->channel, sdev->id,
1081                sdev->lun);
1082        
1083        device_initialize(&sdev->sdev_dev);
1084        sdev->sdev_dev.parent = &sdev->sdev_gendev;
1085        sdev->sdev_dev.class = &sdev_class;
1086        snprintf(sdev->sdev_dev.bus_id, BUS_ID_SIZE,
1087                 "%d:%d:%d:%d", sdev->host->host_no,
1088                 sdev->channel, sdev->id, sdev->lun);
1089        sdev->scsi_level = starget->scsi_level;
1090        transport_setup_device(&sdev->sdev_gendev);
1091        spin_lock_irqsave(shost->host_lock, flags);
1092        list_add_tail(&sdev->same_target_siblings, &starget->devices);
1093        list_add_tail(&sdev->siblings, &shost->__devices);
1094        spin_unlock_irqrestore(shost->host_lock, flags);
1095}
1096
1097int scsi_is_sdev_device(const struct device *dev)
1098{
1099        return dev->type == &scsi_dev_type;
1100}
1101EXPORT_SYMBOL(scsi_is_sdev_device);
1102
1103/* A blank transport template that is used in drivers that don't
1104 * yet implement Transport Attributes */
1105struct scsi_transport_template blank_transport_template = { { { {NULL, }, }, }, };
1106
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.