linux/drivers/s390/cio/css.c
<<
>>
Prefs
   1/*
   2 * driver for channel subsystem
   3 *
   4 * Copyright IBM Corp. 2002, 2010
   5 *
   6 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
   7 *            Cornelia Huck (cornelia.huck@de.ibm.com)
   8 */
   9
  10#define KMSG_COMPONENT "cio"
  11#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  12
  13#include <linux/module.h>
  14#include <linux/init.h>
  15#include <linux/device.h>
  16#include <linux/slab.h>
  17#include <linux/errno.h>
  18#include <linux/list.h>
  19#include <linux/reboot.h>
  20#include <linux/suspend.h>
  21#include <linux/proc_fs.h>
  22#include <asm/isc.h>
  23#include <asm/crw.h>
  24
  25#include "css.h"
  26#include "cio.h"
  27#include "cio_debug.h"
  28#include "ioasm.h"
  29#include "chsc.h"
  30#include "device.h"
  31#include "idset.h"
  32#include "chp.h"
  33
  34int css_init_done = 0;
  35int max_ssid;
  36
  37struct channel_subsystem *channel_subsystems[__MAX_CSSID + 1];
  38static struct bus_type css_bus_type;
  39
  40int
  41for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
  42{
  43        struct subchannel_id schid;
  44        int ret;
  45
  46        init_subchannel_id(&schid);
  47        do {
  48                do {
  49                        ret = fn(schid, data);
  50                        if (ret)
  51                                break;
  52                } while (schid.sch_no++ < __MAX_SUBCHANNEL);
  53                schid.sch_no = 0;
  54        } while (schid.ssid++ < max_ssid);
  55        return ret;
  56}
  57
  58struct cb_data {
  59        void *data;
  60        struct idset *set;
  61        int (*fn_known_sch)(struct subchannel *, void *);
  62        int (*fn_unknown_sch)(struct subchannel_id, void *);
  63};
  64
  65static int call_fn_known_sch(struct device *dev, void *data)
  66{
  67        struct subchannel *sch = to_subchannel(dev);
  68        struct cb_data *cb = data;
  69        int rc = 0;
  70
  71        if (cb->set)
  72                idset_sch_del(cb->set, sch->schid);
  73        if (cb->fn_known_sch)
  74                rc = cb->fn_known_sch(sch, cb->data);
  75        return rc;
  76}
  77
  78static int call_fn_unknown_sch(struct subchannel_id schid, void *data)
  79{
  80        struct cb_data *cb = data;
  81        int rc = 0;
  82
  83        if (idset_sch_contains(cb->set, schid))
  84                rc = cb->fn_unknown_sch(schid, cb->data);
  85        return rc;
  86}
  87
  88static int call_fn_all_sch(struct subchannel_id schid, void *data)
  89{
  90        struct cb_data *cb = data;
  91        struct subchannel *sch;
  92        int rc = 0;
  93
  94        sch = get_subchannel_by_schid(schid);
  95        if (sch) {
  96                if (cb->fn_known_sch)
  97                        rc = cb->fn_known_sch(sch, cb->data);
  98                put_device(&sch->dev);
  99        } else {
 100                if (cb->fn_unknown_sch)
 101                        rc = cb->fn_unknown_sch(schid, cb->data);
 102        }
 103
 104        return rc;
 105}
 106
 107int for_each_subchannel_staged(int (*fn_known)(struct subchannel *, void *),
 108                               int (*fn_unknown)(struct subchannel_id,
 109                               void *), void *data)
 110{
 111        struct cb_data cb;
 112        int rc;
 113
 114        cb.data = data;
 115        cb.fn_known_sch = fn_known;
 116        cb.fn_unknown_sch = fn_unknown;
 117
 118        if (fn_known && !fn_unknown) {
 119                /* Skip idset allocation in case of known-only loop. */
 120                cb.set = NULL;
 121                return bus_for_each_dev(&css_bus_type, NULL, &cb,
 122                                        call_fn_known_sch);
 123        }
 124
 125        cb.set = idset_sch_new();
 126        if (!cb.set)
 127                /* fall back to brute force scanning in case of oom */
 128                return for_each_subchannel(call_fn_all_sch, &cb);
 129
 130        idset_fill(cb.set);
 131
 132        /* Process registered subchannels. */
 133        rc = bus_for_each_dev(&css_bus_type, NULL, &cb, call_fn_known_sch);
 134        if (rc)
 135                goto out;
 136        /* Process unregistered subchannels. */
 137        if (fn_unknown)
 138                rc = for_each_subchannel(call_fn_unknown_sch, &cb);
 139out:
 140        idset_free(cb.set);
 141
 142        return rc;
 143}
 144
 145static void css_sch_todo(struct work_struct *work);
 146
 147static int css_sch_create_locks(struct subchannel *sch)
 148{
 149        sch->lock = kmalloc(sizeof(*sch->lock), GFP_KERNEL);
 150        if (!sch->lock)
 151                return -ENOMEM;
 152
 153        spin_lock_init(sch->lock);
 154        mutex_init(&sch->reg_mutex);
 155
 156        return 0;
 157}
 158
 159static void css_subchannel_release(struct device *dev)
 160{
 161        struct subchannel *sch = to_subchannel(dev);
 162
 163        sch->config.intparm = 0;
 164        cio_commit_config(sch);
 165        kfree(sch->lock);
 166        kfree(sch);
 167}
 168
 169struct subchannel *css_alloc_subchannel(struct subchannel_id schid)
 170{
 171        struct subchannel *sch;
 172        int ret;
 173
 174        sch = kzalloc(sizeof(*sch), GFP_KERNEL | GFP_DMA);
 175        if (!sch)
 176                return ERR_PTR(-ENOMEM);
 177
 178        ret = cio_validate_subchannel(sch, schid);
 179        if (ret < 0)
 180                goto err;
 181
 182        ret = css_sch_create_locks(sch);
 183        if (ret)
 184                goto err;
 185
 186        INIT_WORK(&sch->todo_work, css_sch_todo);
 187        sch->dev.release = &css_subchannel_release;
 188        device_initialize(&sch->dev);
 189        return sch;
 190
 191err:
 192        kfree(sch);
 193        return ERR_PTR(ret);
 194}
 195
 196static int css_sch_device_register(struct subchannel *sch)
 197{
 198        int ret;
 199
 200        mutex_lock(&sch->reg_mutex);
 201        dev_set_name(&sch->dev, "0.%x.%04x", sch->schid.ssid,
 202                     sch->schid.sch_no);
 203        ret = device_add(&sch->dev);
 204        mutex_unlock(&sch->reg_mutex);
 205        return ret;
 206}
 207
 208/**
 209 * css_sch_device_unregister - unregister a subchannel
 210 * @sch: subchannel to be unregistered
 211 */
 212void css_sch_device_unregister(struct subchannel *sch)
 213{
 214        mutex_lock(&sch->reg_mutex);
 215        if (device_is_registered(&sch->dev))
 216                device_unregister(&sch->dev);
 217        mutex_unlock(&sch->reg_mutex);
 218}
 219EXPORT_SYMBOL_GPL(css_sch_device_unregister);
 220
 221static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
 222{
 223        int i;
 224        int mask;
 225
 226        memset(ssd, 0, sizeof(struct chsc_ssd_info));
 227        ssd->path_mask = pmcw->pim;
 228        for (i = 0; i < 8; i++) {
 229                mask = 0x80 >> i;
 230                if (pmcw->pim & mask) {
 231                        chp_id_init(&ssd->chpid[i]);
 232                        ssd->chpid[i].id = pmcw->chpid[i];
 233                }
 234        }
 235}
 236
 237static void ssd_register_chpids(struct chsc_ssd_info *ssd)
 238{
 239        int i;
 240        int mask;
 241
 242        for (i = 0; i < 8; i++) {
 243                mask = 0x80 >> i;
 244                if (ssd->path_mask & mask)
 245                        if (!chp_is_registered(ssd->chpid[i]))
 246                                chp_new(ssd->chpid[i]);
 247        }
 248}
 249
 250void css_update_ssd_info(struct subchannel *sch)
 251{
 252        int ret;
 253
 254        ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
 255        if (ret)
 256                ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
 257
 258        ssd_register_chpids(&sch->ssd_info);
 259}
 260
 261static ssize_t type_show(struct device *dev, struct device_attribute *attr,
 262                         char *buf)
 263{
 264        struct subchannel *sch = to_subchannel(dev);
 265
 266        return sprintf(buf, "%01x\n", sch->st);
 267}
 268
 269static DEVICE_ATTR(type, 0444, type_show, NULL);
 270
 271static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
 272                             char *buf)
 273{
 274        struct subchannel *sch = to_subchannel(dev);
 275
 276        return sprintf(buf, "css:t%01X\n", sch->st);
 277}
 278
 279static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
 280
 281static struct attribute *subch_attrs[] = {
 282        &dev_attr_type.attr,
 283        &dev_attr_modalias.attr,
 284        NULL,
 285};
 286
 287static struct attribute_group subch_attr_group = {
 288        .attrs = subch_attrs,
 289};
 290
 291static const struct attribute_group *default_subch_attr_groups[] = {
 292        &subch_attr_group,
 293        NULL,
 294};
 295
 296int css_register_subchannel(struct subchannel *sch)
 297{
 298        int ret;
 299
 300        /* Initialize the subchannel structure */
 301        sch->dev.parent = &channel_subsystems[0]->device;
 302        sch->dev.bus = &css_bus_type;
 303        sch->dev.groups = default_subch_attr_groups;
 304        /*
 305         * We don't want to generate uevents for I/O subchannels that don't
 306         * have a working ccw device behind them since they will be
 307         * unregistered before they can be used anyway, so we delay the add
 308         * uevent until after device recognition was successful.
 309         * Note that we suppress the uevent for all subchannel types;
 310         * the subchannel driver can decide itself when it wants to inform
 311         * userspace of its existence.
 312         */
 313        dev_set_uevent_suppress(&sch->dev, 1);
 314        css_update_ssd_info(sch);
 315        /* make it known to the system */
 316        ret = css_sch_device_register(sch);
 317        if (ret) {
 318                CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
 319                              sch->schid.ssid, sch->schid.sch_no, ret);
 320                return ret;
 321        }
 322        if (!sch->driver) {
 323                /*
 324                 * No driver matched. Generate the uevent now so that
 325                 * a fitting driver module may be loaded based on the
 326                 * modalias.
 327                 */
 328                dev_set_uevent_suppress(&sch->dev, 0);
 329                kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
 330        }
 331        return ret;
 332}
 333
 334static int css_probe_device(struct subchannel_id schid)
 335{
 336        struct subchannel *sch;
 337        int ret;
 338
 339        sch = css_alloc_subchannel(schid);
 340        if (IS_ERR(sch))
 341                return PTR_ERR(sch);
 342
 343        ret = css_register_subchannel(sch);
 344        if (ret)
 345                put_device(&sch->dev);
 346
 347        return ret;
 348}
 349
 350static int
 351check_subchannel(struct device * dev, void * data)
 352{
 353        struct subchannel *sch;
 354        struct subchannel_id *schid = data;
 355
 356        sch = to_subchannel(dev);
 357        return schid_equal(&sch->schid, schid);
 358}
 359
 360struct subchannel *
 361get_subchannel_by_schid(struct subchannel_id schid)
 362{
 363        struct device *dev;
 364
 365        dev = bus_find_device(&css_bus_type, NULL,
 366                              &schid, check_subchannel);
 367
 368        return dev ? to_subchannel(dev) : NULL;
 369}
 370
 371/**
 372 * css_sch_is_valid() - check if a subchannel is valid
 373 * @schib: subchannel information block for the subchannel
 374 */
 375int css_sch_is_valid(struct schib *schib)
 376{
 377        if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
 378                return 0;
 379        if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w)
 380                return 0;
 381        return 1;
 382}
 383EXPORT_SYMBOL_GPL(css_sch_is_valid);
 384
 385static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
 386{
 387        struct schib schib;
 388
 389        if (!slow) {
 390                /* Will be done on the slow path. */
 391                return -EAGAIN;
 392        }
 393        if (stsch(schid, &schib)) {
 394                /* Subchannel is not provided. */
 395                return -ENXIO;
 396        }
 397        if (!css_sch_is_valid(&schib)) {
 398                /* Unusable - ignore. */
 399                return 0;
 400        }
 401        CIO_MSG_EVENT(4, "event: sch 0.%x.%04x, new\n", schid.ssid,
 402                      schid.sch_no);
 403
 404        return css_probe_device(schid);
 405}
 406
 407static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
 408{
 409        int ret = 0;
 410
 411        if (sch->driver) {
 412                if (sch->driver->sch_event)
 413                        ret = sch->driver->sch_event(sch, slow);
 414                else
 415                        dev_dbg(&sch->dev,
 416                                "Got subchannel machine check but "
 417                                "no sch_event handler provided.\n");
 418        }
 419        if (ret != 0 && ret != -EAGAIN) {
 420                CIO_MSG_EVENT(2, "eval: sch 0.%x.%04x, rc=%d\n",
 421                              sch->schid.ssid, sch->schid.sch_no, ret);
 422        }
 423        return ret;
 424}
 425
 426static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
 427{
 428        struct subchannel *sch;
 429        int ret;
 430
 431        sch = get_subchannel_by_schid(schid);
 432        if (sch) {
 433                ret = css_evaluate_known_subchannel(sch, slow);
 434                put_device(&sch->dev);
 435        } else
 436                ret = css_evaluate_new_subchannel(schid, slow);
 437        if (ret == -EAGAIN)
 438                css_schedule_eval(schid);
 439}
 440
 441/**
 442 * css_sched_sch_todo - schedule a subchannel operation
 443 * @sch: subchannel
 444 * @todo: todo
 445 *
 446 * Schedule the operation identified by @todo to be performed on the slow path
 447 * workqueue. Do nothing if another operation with higher priority is already
 448 * scheduled. Needs to be called with subchannel lock held.
 449 */
 450void css_sched_sch_todo(struct subchannel *sch, enum sch_todo todo)
 451{
 452        CIO_MSG_EVENT(4, "sch_todo: sched sch=0.%x.%04x todo=%d\n",
 453                      sch->schid.ssid, sch->schid.sch_no, todo);
 454        if (sch->todo >= todo)
 455                return;
 456        /* Get workqueue ref. */
 457        if (!get_device(&sch->dev))
 458                return;
 459        sch->todo = todo;
 460        if (!queue_work(cio_work_q, &sch->todo_work)) {
 461                /* Already queued, release workqueue ref. */
 462                put_device(&sch->dev);
 463        }
 464}
 465EXPORT_SYMBOL_GPL(css_sched_sch_todo);
 466
 467static void css_sch_todo(struct work_struct *work)
 468{
 469        struct subchannel *sch;
 470        enum sch_todo todo;
 471        int ret;
 472
 473        sch = container_of(work, struct subchannel, todo_work);
 474        /* Find out todo. */
 475        spin_lock_irq(sch->lock);
 476        todo = sch->todo;
 477        CIO_MSG_EVENT(4, "sch_todo: sch=0.%x.%04x, todo=%d\n", sch->schid.ssid,
 478                      sch->schid.sch_no, todo);
 479        sch->todo = SCH_TODO_NOTHING;
 480        spin_unlock_irq(sch->lock);
 481        /* Perform todo. */
 482        switch (todo) {
 483        case SCH_TODO_NOTHING:
 484                break;
 485        case SCH_TODO_EVAL:
 486                ret = css_evaluate_known_subchannel(sch, 1);
 487                if (ret == -EAGAIN) {
 488                        spin_lock_irq(sch->lock);
 489                        css_sched_sch_todo(sch, todo);
 490                        spin_unlock_irq(sch->lock);
 491                }
 492                break;
 493        case SCH_TODO_UNREG:
 494                css_sch_device_unregister(sch);
 495                break;
 496        }
 497        /* Release workqueue ref. */
 498        put_device(&sch->dev);
 499}
 500
 501static struct idset *slow_subchannel_set;
 502static spinlock_t slow_subchannel_lock;
 503static wait_queue_head_t css_eval_wq;
 504static atomic_t css_eval_scheduled;
 505
 506static int __init slow_subchannel_init(void)
 507{
 508        spin_lock_init(&slow_subchannel_lock);
 509        atomic_set(&css_eval_scheduled, 0);
 510        init_waitqueue_head(&css_eval_wq);
 511        slow_subchannel_set = idset_sch_new();
 512        if (!slow_subchannel_set) {
 513                CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
 514                return -ENOMEM;
 515        }
 516        return 0;
 517}
 518
 519static int slow_eval_known_fn(struct subchannel *sch, void *data)
 520{
 521        int eval;
 522        int rc;
 523
 524        spin_lock_irq(&slow_subchannel_lock);
 525        eval = idset_sch_contains(slow_subchannel_set, sch->schid);
 526        idset_sch_del(slow_subchannel_set, sch->schid);
 527        spin_unlock_irq(&slow_subchannel_lock);
 528        if (eval) {
 529                rc = css_evaluate_known_subchannel(sch, 1);
 530                if (rc == -EAGAIN)
 531                        css_schedule_eval(sch->schid);
 532        }
 533        return 0;
 534}
 535
 536static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
 537{
 538        int eval;
 539        int rc = 0;
 540
 541        spin_lock_irq(&slow_subchannel_lock);
 542        eval = idset_sch_contains(slow_subchannel_set, schid);
 543        idset_sch_del(slow_subchannel_set, schid);
 544        spin_unlock_irq(&slow_subchannel_lock);
 545        if (eval) {
 546                rc = css_evaluate_new_subchannel(schid, 1);
 547                switch (rc) {
 548                case -EAGAIN:
 549                        css_schedule_eval(schid);
 550                        rc = 0;
 551                        break;
 552                case -ENXIO:
 553                case -ENOMEM:
 554                case -EIO:
 555                        /* These should abort looping */
 556                        spin_lock_irq(&slow_subchannel_lock);
 557                        idset_sch_del_subseq(slow_subchannel_set, schid);
 558                        spin_unlock_irq(&slow_subchannel_lock);
 559                        break;
 560                default:
 561                        rc = 0;
 562                }
 563                /* Allow scheduling here since the containing loop might
 564                 * take a while.  */
 565                cond_resched();
 566        }
 567        return rc;
 568}
 569
 570static void css_slow_path_func(struct work_struct *unused)
 571{
 572        unsigned long flags;
 573
 574        CIO_TRACE_EVENT(4, "slowpath");
 575        for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
 576                                   NULL);
 577        spin_lock_irqsave(&slow_subchannel_lock, flags);
 578        if (idset_is_empty(slow_subchannel_set)) {
 579                atomic_set(&css_eval_scheduled, 0);
 580                wake_up(&css_eval_wq);
 581        }
 582        spin_unlock_irqrestore(&slow_subchannel_lock, flags);
 583}
 584
 585static DECLARE_DELAYED_WORK(slow_path_work, css_slow_path_func);
 586struct workqueue_struct *cio_work_q;
 587
 588void css_schedule_eval(struct subchannel_id schid)
 589{
 590        unsigned long flags;
 591
 592        spin_lock_irqsave(&slow_subchannel_lock, flags);
 593        idset_sch_add(slow_subchannel_set, schid);
 594        atomic_set(&css_eval_scheduled, 1);
 595        queue_delayed_work(cio_work_q, &slow_path_work, 0);
 596        spin_unlock_irqrestore(&slow_subchannel_lock, flags);
 597}
 598
 599void css_schedule_eval_all(void)
 600{
 601        unsigned long flags;
 602
 603        spin_lock_irqsave(&slow_subchannel_lock, flags);
 604        idset_fill(slow_subchannel_set);
 605        atomic_set(&css_eval_scheduled, 1);
 606        queue_delayed_work(cio_work_q, &slow_path_work, 0);
 607        spin_unlock_irqrestore(&slow_subchannel_lock, flags);
 608}
 609
 610static int __unset_registered(struct device *dev, void *data)
 611{
 612        struct idset *set = data;
 613        struct subchannel *sch = to_subchannel(dev);
 614
 615        idset_sch_del(set, sch->schid);
 616        return 0;
 617}
 618
 619void css_schedule_eval_all_unreg(unsigned long delay)
 620{
 621        unsigned long flags;
 622        struct idset *unreg_set;
 623
 624        /* Find unregistered subchannels. */
 625        unreg_set = idset_sch_new();
 626        if (!unreg_set) {
 627                /* Fallback. */
 628                css_schedule_eval_all();
 629                return;
 630        }
 631        idset_fill(unreg_set);
 632        bus_for_each_dev(&css_bus_type, NULL, unreg_set, __unset_registered);
 633        /* Apply to slow_subchannel_set. */
 634        spin_lock_irqsave(&slow_subchannel_lock, flags);
 635        idset_add_set(slow_subchannel_set, unreg_set);
 636        atomic_set(&css_eval_scheduled, 1);
 637        queue_delayed_work(cio_work_q, &slow_path_work, delay);
 638        spin_unlock_irqrestore(&slow_subchannel_lock, flags);
 639        idset_free(unreg_set);
 640}
 641
 642void css_wait_for_slow_path(void)
 643{
 644        flush_workqueue(cio_work_q);
 645}
 646
 647/* Schedule reprobing of all unregistered subchannels. */
 648void css_schedule_reprobe(void)
 649{
 650        /* Schedule with a delay to allow merging of subsequent calls. */
 651        css_schedule_eval_all_unreg(1 * HZ);
 652}
 653EXPORT_SYMBOL_GPL(css_schedule_reprobe);
 654
 655/*
 656 * Called from the machine check handler for subchannel report words.
 657 */
 658static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
 659{
 660        struct subchannel_id mchk_schid;
 661        struct subchannel *sch;
 662
 663        if (overflow) {
 664                css_schedule_eval_all();
 665                return;
 666        }
 667        CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
 668                      "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
 669                      crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
 670                      crw0->erc, crw0->rsid);
 671        if (crw1)
 672                CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
 673                              "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
 674                              crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
 675                              crw1->anc, crw1->erc, crw1->rsid);
 676        init_subchannel_id(&mchk_schid);
 677        mchk_schid.sch_no = crw0->rsid;
 678        if (crw1)
 679                mchk_schid.ssid = (crw1->rsid >> 4) & 3;
 680
 681        if (crw0->erc == CRW_ERC_PMOD) {
 682                sch = get_subchannel_by_schid(mchk_schid);
 683                if (sch) {
 684                        css_update_ssd_info(sch);
 685                        put_device(&sch->dev);
 686                }
 687        }
 688        /*
 689         * Since we are always presented with IPI in the CRW, we have to
 690         * use stsch() to find out if the subchannel in question has come
 691         * or gone.
 692         */
 693        css_evaluate_subchannel(mchk_schid, 0);
 694}
 695
 696static void __init
 697css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
 698{
 699        struct cpuid cpu_id;
 700
 701        if (css_general_characteristics.mcss) {
 702                css->global_pgid.pgid_high.ext_cssid.version = 0x80;
 703                css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
 704        } else {
 705                css->global_pgid.pgid_high.cpu_addr = stap();
 706        }
 707        get_cpu_id(&cpu_id);
 708        css->global_pgid.cpu_id = cpu_id.ident;
 709        css->global_pgid.cpu_model = cpu_id.machine;
 710        css->global_pgid.tod_high = tod_high;
 711}
 712
 713static void
 714channel_subsystem_release(struct device *dev)
 715{
 716        struct channel_subsystem *css;
 717
 718        css = to_css(dev);
 719        mutex_destroy(&css->mutex);
 720        if (css->pseudo_subchannel) {
 721                /* Implies that it has been generated but never registered. */
 722                css_subchannel_release(&css->pseudo_subchannel->dev);
 723                css->pseudo_subchannel = NULL;
 724        }
 725        kfree(css);
 726}
 727
 728static ssize_t
 729css_cm_enable_show(struct device *dev, struct device_attribute *attr,
 730                   char *buf)
 731{
 732        struct channel_subsystem *css = to_css(dev);
 733        int ret;
 734
 735        if (!css)
 736                return 0;
 737        mutex_lock(&css->mutex);
 738        ret = sprintf(buf, "%x\n", css->cm_enabled);
 739        mutex_unlock(&css->mutex);
 740        return ret;
 741}
 742
 743static ssize_t
 744css_cm_enable_store(struct device *dev, struct device_attribute *attr,
 745                    const char *buf, size_t count)
 746{
 747        struct channel_subsystem *css = to_css(dev);
 748        int ret;
 749        unsigned long val;
 750
 751        ret = kstrtoul(buf, 16, &val);
 752        if (ret)
 753                return ret;
 754        mutex_lock(&css->mutex);
 755        switch (val) {
 756        case 0:
 757                ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
 758                break;
 759        case 1:
 760                ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
 761                break;
 762        default:
 763                ret = -EINVAL;
 764        }
 765        mutex_unlock(&css->mutex);
 766        return ret < 0 ? ret : count;
 767}
 768
 769static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
 770
 771static int __init setup_css(int nr)
 772{
 773        u32 tod_high;
 774        int ret;
 775        struct channel_subsystem *css;
 776
 777        css = channel_subsystems[nr];
 778        memset(css, 0, sizeof(struct channel_subsystem));
 779        css->pseudo_subchannel =
 780                kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
 781        if (!css->pseudo_subchannel)
 782                return -ENOMEM;
 783        css->pseudo_subchannel->dev.parent = &css->device;
 784        css->pseudo_subchannel->dev.release = css_subchannel_release;
 785        dev_set_name(&css->pseudo_subchannel->dev, "defunct");
 786        mutex_init(&css->pseudo_subchannel->reg_mutex);
 787        ret = css_sch_create_locks(css->pseudo_subchannel);
 788        if (ret) {
 789                kfree(css->pseudo_subchannel);
 790                return ret;
 791        }
 792        mutex_init(&css->mutex);
 793        css->valid = 1;
 794        css->cssid = nr;
 795        dev_set_name(&css->device, "css%x", nr);
 796        css->device.release = channel_subsystem_release;
 797        tod_high = (u32) (get_tod_clock() >> 32);
 798        css_generate_pgid(css, tod_high);
 799        return 0;
 800}
 801
 802static int css_reboot_event(struct notifier_block *this,
 803                            unsigned long event,
 804                            void *ptr)
 805{
 806        int ret, i;
 807
 808        ret = NOTIFY_DONE;
 809        for (i = 0; i <= __MAX_CSSID; i++) {
 810                struct channel_subsystem *css;
 811
 812                css = channel_subsystems[i];
 813                mutex_lock(&css->mutex);
 814                if (css->cm_enabled)
 815                        if (chsc_secm(css, 0))
 816                                ret = NOTIFY_BAD;
 817                mutex_unlock(&css->mutex);
 818        }
 819
 820        return ret;
 821}
 822
 823static struct notifier_block css_reboot_notifier = {
 824        .notifier_call = css_reboot_event,
 825};
 826
 827/*
 828 * Since the css devices are neither on a bus nor have a class
 829 * nor have a special device type, we cannot stop/restart channel
 830 * path measurements via the normal suspend/resume callbacks, but have
 831 * to use notifiers.
 832 */
 833static int css_power_event(struct notifier_block *this, unsigned long event,
 834                           void *ptr)
 835{
 836        int ret, i;
 837
 838        switch (event) {
 839        case PM_HIBERNATION_PREPARE:
 840        case PM_SUSPEND_PREPARE:
 841                ret = NOTIFY_DONE;
 842                for (i = 0; i <= __MAX_CSSID; i++) {
 843                        struct channel_subsystem *css;
 844
 845                        css = channel_subsystems[i];
 846                        mutex_lock(&css->mutex);
 847                        if (!css->cm_enabled) {
 848                                mutex_unlock(&css->mutex);
 849                                continue;
 850                        }
 851                        ret = __chsc_do_secm(css, 0);
 852                        ret = notifier_from_errno(ret);
 853                        mutex_unlock(&css->mutex);
 854                }
 855                break;
 856        case PM_POST_HIBERNATION:
 857        case PM_POST_SUSPEND:
 858                ret = NOTIFY_DONE;
 859                for (i = 0; i <= __MAX_CSSID; i++) {
 860                        struct channel_subsystem *css;
 861
 862                        css = channel_subsystems[i];
 863                        mutex_lock(&css->mutex);
 864                        if (!css->cm_enabled) {
 865                                mutex_unlock(&css->mutex);
 866                                continue;
 867                        }
 868                        ret = __chsc_do_secm(css, 1);
 869                        ret = notifier_from_errno(ret);
 870                        mutex_unlock(&css->mutex);
 871                }
 872                /* search for subchannels, which appeared during hibernation */
 873                css_schedule_reprobe();
 874                break;
 875        default:
 876                ret = NOTIFY_DONE;
 877        }
 878        return ret;
 879
 880}
 881static struct notifier_block css_power_notifier = {
 882        .notifier_call = css_power_event,
 883};
 884
 885/*
 886 * Now that the driver core is running, we can setup our channel subsystem.
 887 * The struct subchannel's are created during probing.
 888 */
 889static int __init css_bus_init(void)
 890{
 891        int ret, i;
 892
 893        ret = chsc_init();
 894        if (ret)
 895                return ret;
 896
 897        chsc_determine_css_characteristics();
 898        /* Try to enable MSS. */
 899        ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
 900        if (ret)
 901                max_ssid = 0;
 902        else /* Success. */
 903                max_ssid = __MAX_SSID;
 904
 905        ret = slow_subchannel_init();
 906        if (ret)
 907                goto out;
 908
 909        ret = crw_register_handler(CRW_RSC_SCH, css_process_crw);
 910        if (ret)
 911                goto out;
 912
 913        if ((ret = bus_register(&css_bus_type)))
 914                goto out;
 915
 916        /* Setup css structure. */
 917        for (i = 0; i <= __MAX_CSSID; i++) {
 918                struct channel_subsystem *css;
 919
 920                css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
 921                if (!css) {
 922                        ret = -ENOMEM;
 923                        goto out_unregister;
 924                }
 925                channel_subsystems[i] = css;
 926                ret = setup_css(i);
 927                if (ret) {
 928                        kfree(channel_subsystems[i]);
 929                        goto out_unregister;
 930                }
 931                ret = device_register(&css->device);
 932                if (ret) {
 933                        put_device(&css->device);
 934                        goto out_unregister;
 935                }
 936                if (css_chsc_characteristics.secm) {
 937                        ret = device_create_file(&css->device,
 938                                                 &dev_attr_cm_enable);
 939                        if (ret)
 940                                goto out_device;
 941                }
 942                ret = device_register(&css->pseudo_subchannel->dev);
 943                if (ret) {
 944                        put_device(&css->pseudo_subchannel->dev);
 945                        goto out_file;
 946                }
 947        }
 948        ret = register_reboot_notifier(&css_reboot_notifier);
 949        if (ret)
 950                goto out_unregister;
 951        ret = register_pm_notifier(&css_power_notifier);
 952        if (ret) {
 953                unregister_reboot_notifier(&css_reboot_notifier);
 954                goto out_unregister;
 955        }
 956        css_init_done = 1;
 957
 958        /* Enable default isc for I/O subchannels. */
 959        isc_register(IO_SCH_ISC);
 960
 961        return 0;
 962out_file:
 963        if (css_chsc_characteristics.secm)
 964                device_remove_file(&channel_subsystems[i]->device,
 965                                   &dev_attr_cm_enable);
 966out_device:
 967        device_unregister(&channel_subsystems[i]->device);
 968out_unregister:
 969        while (i > 0) {
 970                struct channel_subsystem *css;
 971
 972                i--;
 973                css = channel_subsystems[i];
 974                device_unregister(&css->pseudo_subchannel->dev);
 975                css->pseudo_subchannel = NULL;
 976                if (css_chsc_characteristics.secm)
 977                        device_remove_file(&css->device,
 978                                           &dev_attr_cm_enable);
 979                device_unregister(&css->device);
 980        }
 981        bus_unregister(&css_bus_type);
 982out:
 983        crw_unregister_handler(CRW_RSC_SCH);
 984        idset_free(slow_subchannel_set);
 985        chsc_init_cleanup();
 986        pr_alert("The CSS device driver initialization failed with "
 987                 "errno=%d\n", ret);
 988        return ret;
 989}
 990
 991static void __init css_bus_cleanup(void)
 992{
 993        struct channel_subsystem *css;
 994        int i;
 995
 996        for (i = 0; i <= __MAX_CSSID; i++) {
 997                css = channel_subsystems[i];
 998                device_unregister(&css->pseudo_subchannel->dev);
 999                css->pseudo_subchannel = NULL;
1000                if (css_chsc_characteristics.secm)
1001                        device_remove_file(&css->device, &dev_attr_cm_enable);
1002                device_unregister(&css->device);
1003        }
1004        bus_unregister(&css_bus_type);
1005        crw_unregister_handler(CRW_RSC_SCH);
1006        idset_free(slow_subchannel_set);
1007        chsc_init_cleanup();
1008        isc_unregister(IO_SCH_ISC);
1009}
1010
1011static int __init channel_subsystem_init(void)
1012{
1013        int ret;
1014
1015        ret = css_bus_init();
1016        if (ret)
1017                return ret;
1018        cio_work_q = create_singlethread_workqueue("cio");
1019        if (!cio_work_q) {
1020                ret = -ENOMEM;
1021                goto out_bus;
1022        }
1023        ret = io_subchannel_init();
1024        if (ret)
1025                goto out_wq;
1026
1027        return ret;
1028out_wq:
1029        destroy_workqueue(cio_work_q);
1030out_bus:
1031        css_bus_cleanup();
1032        return ret;
1033}
1034subsys_initcall(channel_subsystem_init);
1035
1036static int css_settle(struct device_driver *drv, void *unused)
1037{
1038        struct css_driver *cssdrv = to_cssdriver(drv);
1039
1040        if (cssdrv->settle)
1041                return cssdrv->settle();
1042        return 0;
1043}
1044
1045int css_complete_work(void)
1046{
1047        int ret;
1048
1049        /* Wait for the evaluation of subchannels to finish. */
1050        ret = wait_event_interruptible(css_eval_wq,
1051                                       atomic_read(&css_eval_scheduled) == 0);
1052        if (ret)
1053                return -EINTR;
1054        flush_workqueue(cio_work_q);
1055        /* Wait for the subchannel type specific initialization to finish */
1056        return bus_for_each_drv(&css_bus_type, NULL, NULL, css_settle);
1057}
1058
1059
1060/*
1061 * Wait for the initialization of devices to finish, to make sure we are
1062 * done with our setup if the search for the root device starts.
1063 */
1064static int __init channel_subsystem_init_sync(void)
1065{
1066        /* Register subchannels which are already in use. */
1067        cio_register_early_subchannels();
1068        /* Start initial subchannel evaluation. */
1069        css_schedule_eval_all();
1070        css_complete_work();
1071        return 0;
1072}
1073subsys_initcall_sync(channel_subsystem_init_sync);
1074
1075void channel_subsystem_reinit(void)
1076{
1077        struct channel_path *chp;
1078        struct chp_id chpid;
1079
1080        chsc_enable_facility(CHSC_SDA_OC_MSS);
1081        chp_id_for_each(&chpid) {
1082                chp = chpid_to_chp(chpid);
1083                if (chp)
1084                        chp_update_desc(chp);
1085        }
1086        cmf_reactivate();
1087}
1088
1089#ifdef CONFIG_PROC_FS
1090static ssize_t cio_settle_write(struct file *file, const char __user *buf,
1091                                size_t count, loff_t *ppos)
1092{
1093        int ret;
1094
1095        /* Handle pending CRW's. */
1096        crw_wait_for_channel_report();
1097        ret = css_complete_work();
1098
1099        return ret ? ret : count;
1100}
1101
1102static const struct file_operations cio_settle_proc_fops = {
1103        .open = nonseekable_open,
1104        .write = cio_settle_write,
1105        .llseek = no_llseek,
1106};
1107
1108static int __init cio_settle_init(void)
1109{
1110        struct proc_dir_entry *entry;
1111
1112        entry = proc_create("cio_settle", S_IWUSR, NULL,
1113                            &cio_settle_proc_fops);
1114        if (!entry)
1115                return -ENOMEM;
1116        return 0;
1117}
1118device_initcall(cio_settle_init);
1119#endif /*CONFIG_PROC_FS*/
1120
1121int sch_is_pseudo_sch(struct subchannel *sch)
1122{
1123        return sch == to_css(sch->dev.parent)->pseudo_subchannel;
1124}
1125
1126static int css_bus_match(struct device *dev, struct device_driver *drv)
1127{
1128        struct subchannel *sch = to_subchannel(dev);
1129        struct css_driver *driver = to_cssdriver(drv);
1130        struct css_device_id *id;
1131
1132        for (id = driver->subchannel_type; id->match_flags; id++) {
1133                if (sch->st == id->type)
1134                        return 1;
1135        }
1136
1137        return 0;
1138}
1139
1140static int css_probe(struct device *dev)
1141{
1142        struct subchannel *sch;
1143        int ret;
1144
1145        sch = to_subchannel(dev);
1146        sch->driver = to_cssdriver(dev->driver);
1147        ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
1148        if (ret)
1149                sch->driver = NULL;
1150        return ret;
1151}
1152
1153static int css_remove(struct device *dev)
1154{
1155        struct subchannel *sch;
1156        int ret;
1157
1158        sch = to_subchannel(dev);
1159        ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
1160        sch->driver = NULL;
1161        return ret;
1162}
1163
1164static void css_shutdown(struct device *dev)
1165{
1166        struct subchannel *sch;
1167
1168        sch = to_subchannel(dev);
1169        if (sch->driver && sch->driver->shutdown)
1170                sch->driver->shutdown(sch);
1171}
1172
1173static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
1174{
1175        struct subchannel *sch = to_subchannel(dev);
1176        int ret;
1177
1178        ret = add_uevent_var(env, "ST=%01X", sch->st);
1179        if (ret)
1180                return ret;
1181        ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
1182        return ret;
1183}
1184
1185static int css_pm_prepare(struct device *dev)
1186{
1187        struct subchannel *sch = to_subchannel(dev);
1188        struct css_driver *drv;
1189
1190        if (mutex_is_locked(&sch->reg_mutex))
1191                return -EAGAIN;
1192        if (!sch->dev.driver)
1193                return 0;
1194        drv = to_cssdriver(sch->dev.driver);
1195        /* Notify drivers that they may not register children. */
1196        return drv->prepare ? drv->prepare(sch) : 0;
1197}
1198
1199static void css_pm_complete(struct device *dev)
1200{
1201        struct subchannel *sch = to_subchannel(dev);
1202        struct css_driver *drv;
1203
1204        if (!sch->dev.driver)
1205                return;
1206        drv = to_cssdriver(sch->dev.driver);
1207        if (drv->complete)
1208                drv->complete(sch);
1209}
1210
1211static int css_pm_freeze(struct device *dev)
1212{
1213        struct subchannel *sch = to_subchannel(dev);
1214        struct css_driver *drv;
1215
1216        if (!sch->dev.driver)
1217                return 0;
1218        drv = to_cssdriver(sch->dev.driver);
1219        return drv->freeze ? drv->freeze(sch) : 0;
1220}
1221
1222static int css_pm_thaw(struct device *dev)
1223{
1224        struct subchannel *sch = to_subchannel(dev);
1225        struct css_driver *drv;
1226
1227        if (!sch->dev.driver)
1228                return 0;
1229        drv = to_cssdriver(sch->dev.driver);
1230        return drv->thaw ? drv->thaw(sch) : 0;
1231}
1232
1233static int css_pm_restore(struct device *dev)
1234{
1235        struct subchannel *sch = to_subchannel(dev);
1236        struct css_driver *drv;
1237
1238        css_update_ssd_info(sch);
1239        if (!sch->dev.driver)
1240                return 0;
1241        drv = to_cssdriver(sch->dev.driver);
1242        return drv->restore ? drv->restore(sch) : 0;
1243}
1244
1245static const struct dev_pm_ops css_pm_ops = {
1246        .prepare = css_pm_prepare,
1247        .complete = css_pm_complete,
1248        .freeze = css_pm_freeze,
1249        .thaw = css_pm_thaw,
1250        .restore = css_pm_restore,
1251};
1252
1253static struct bus_type css_bus_type = {
1254        .name     = "css",
1255        .match    = css_bus_match,
1256        .probe    = css_probe,
1257        .remove   = css_remove,
1258        .shutdown = css_shutdown,
1259        .uevent   = css_uevent,
1260        .pm = &css_pm_ops,
1261};
1262
1263/**
1264 * css_driver_register - register a css driver
1265 * @cdrv: css driver to register
1266 *
1267 * This is mainly a wrapper around driver_register that sets name
1268 * and bus_type in the embedded struct device_driver correctly.
1269 */
1270int css_driver_register(struct css_driver *cdrv)
1271{
1272        cdrv->drv.bus = &css_bus_type;
1273        return driver_register(&cdrv->drv);
1274}
1275EXPORT_SYMBOL_GPL(css_driver_register);
1276
1277/**
1278 * css_driver_unregister - unregister a css driver
1279 * @cdrv: css driver to unregister
1280 *
1281 * This is a wrapper around driver_unregister.
1282 */
1283void css_driver_unregister(struct css_driver *cdrv)
1284{
1285        driver_unregister(&cdrv->drv);
1286}
1287EXPORT_SYMBOL_GPL(css_driver_unregister);
1288
1289MODULE_LICENSE("GPL");
1290